2003-07-01 11:20:12 +04:00
|
|
|
/* $NetBSD: setkey.c,v 1.7 2003/07/01 07:20:14 itojun Exp $ */
|
|
|
|
/* $KAME: setkey.c,v 1.28 2003/06/27 07:15:45 itojun Exp $ */
|
1999-07-06 17:13:03 +04:00
|
|
|
|
1999-07-02 21:41:23 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
1999-07-02 21:41:23 +04:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-06-12 14:40:37 +04:00
|
|
|
*
|
1999-07-02 21:41:23 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
2000-02-06 14:21:45 +03:00
|
|
|
#include <net/pfkeyv2.h>
|
1999-07-02 21:41:23 +04:00
|
|
|
#include <netkey/keydb.h>
|
|
|
|
#include <netkey/key_debug.h>
|
|
|
|
#include <netinet6/ipsec.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
2000-06-12 14:40:37 +04:00
|
|
|
#include "libpfkey.h"
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
void usage __P((void));
|
1999-07-02 21:41:23 +04:00
|
|
|
int main __P((int, char **));
|
|
|
|
int get_supported __P((void));
|
|
|
|
void sendkeyshort __P((u_int));
|
|
|
|
void promisc __P((void));
|
2001-09-07 08:12:10 +04:00
|
|
|
int sendkeymsg __P((char *, size_t));
|
1999-07-02 21:41:23 +04:00
|
|
|
int postproc __P((struct sadb_msg *, int));
|
|
|
|
const char *numstr __P((int));
|
|
|
|
void shortdump_hdr __P((void));
|
|
|
|
void shortdump __P((struct sadb_msg *));
|
2001-09-07 08:12:10 +04:00
|
|
|
static void printdate __P((void));
|
|
|
|
static int32_t gmt2local __P((time_t));
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
#define MODE_SCRIPT 1
|
|
|
|
#define MODE_CMDDUMP 2
|
|
|
|
#define MODE_CMDFLUSH 3
|
2000-04-16 20:15:59 +04:00
|
|
|
#define MODE_PROMISC 4
|
2000-01-31 17:22:41 +03:00
|
|
|
|
|
|
|
int so;
|
|
|
|
|
1999-07-02 21:41:23 +04:00
|
|
|
int f_forever = 0;
|
|
|
|
int f_all = 0;
|
|
|
|
int f_verbose = 0;
|
2000-01-31 17:22:41 +03:00
|
|
|
int f_mode = 0;
|
1999-07-02 21:41:23 +04:00
|
|
|
int f_cmddump = 0;
|
|
|
|
int f_policy = 0;
|
|
|
|
int f_hexdump = 0;
|
2001-09-07 08:12:10 +04:00
|
|
|
int f_tflag = 0;
|
|
|
|
static time_t thiszone;
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
extern int lineno;
|
|
|
|
|
|
|
|
extern int parse __P((FILE **));
|
|
|
|
|
|
|
|
void
|
2001-09-07 08:12:10 +04:00
|
|
|
usage()
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
|
|
|
|
printf("usage: setkey [-v] -c\n");
|
|
|
|
printf(" setkey [-v] -f filename\n");
|
|
|
|
printf(" setkey [-Palv] -D\n");
|
|
|
|
printf(" setkey [-Pv] -F\n");
|
|
|
|
printf(" setkey [-h] -x\n");
|
2000-01-31 17:22:41 +03:00
|
|
|
exit(1);
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(ac, av)
|
|
|
|
int ac;
|
|
|
|
char **av;
|
|
|
|
{
|
|
|
|
FILE *fp = stdin;
|
|
|
|
int c;
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
if (ac == 1) {
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
thiszone = gmt2local(0);
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-05-07 18:51:46 +04:00
|
|
|
while ((c = getopt(ac, av, "acdf:hlvxDFP")) != -1) {
|
1999-07-02 21:41:23 +04:00
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
2000-01-31 17:22:41 +03:00
|
|
|
f_mode = MODE_SCRIPT;
|
1999-07-02 21:41:23 +04:00
|
|
|
fp = stdin;
|
|
|
|
break;
|
|
|
|
case 'f':
|
2000-01-31 17:22:41 +03:00
|
|
|
f_mode = MODE_SCRIPT;
|
1999-07-02 21:41:23 +04:00
|
|
|
if ((fp = fopen(optarg, "r")) == NULL) {
|
2000-01-31 17:22:41 +03:00
|
|
|
err(-1, "fopen");
|
|
|
|
/*NOTREACHED*/
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
break;
|
2000-01-31 17:22:41 +03:00
|
|
|
case 'D':
|
|
|
|
f_mode = MODE_CMDDUMP;
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
f_mode = MODE_CMDFLUSH;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
f_all = 1;
|
|
|
|
break;
|
1999-07-02 21:41:23 +04:00
|
|
|
case 'l':
|
|
|
|
f_forever = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
f_hexdump = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
2000-04-16 20:15:59 +04:00
|
|
|
f_mode = MODE_PROMISC;
|
2001-09-07 08:12:10 +04:00
|
|
|
f_tflag++;
|
2000-04-16 20:15:59 +04:00
|
|
|
break;
|
1999-07-02 21:41:23 +04:00
|
|
|
case 'P':
|
|
|
|
f_policy = 1;
|
|
|
|
break;
|
2000-01-31 17:22:41 +03:00
|
|
|
case 'v':
|
|
|
|
f_verbose = 1;
|
|
|
|
break;
|
1999-07-02 21:41:23 +04:00
|
|
|
default:
|
2001-09-07 08:12:10 +04:00
|
|
|
usage();
|
1999-07-02 21:41:23 +04:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
so = pfkey_open();
|
|
|
|
if (so < 0) {
|
|
|
|
perror("pfkey_open");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
switch (f_mode) {
|
|
|
|
case MODE_CMDDUMP:
|
1999-07-02 21:41:23 +04:00
|
|
|
sendkeyshort(f_policy ? SADB_X_SPDDUMP: SADB_DUMP);
|
2000-01-31 17:22:41 +03:00
|
|
|
break;
|
|
|
|
case MODE_CMDFLUSH:
|
|
|
|
sendkeyshort(f_policy ? SADB_X_SPDFLUSH: SADB_FLUSH);
|
|
|
|
break;
|
|
|
|
case MODE_SCRIPT:
|
|
|
|
if (get_supported() < 0) {
|
|
|
|
errx(-1, "%s", ipsec_strerror());
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
if (parse(&fp))
|
|
|
|
exit (1);
|
|
|
|
break;
|
2000-04-16 20:15:59 +04:00
|
|
|
case MODE_PROMISC:
|
|
|
|
promisc();
|
|
|
|
/*NOTREACHED*/
|
2000-01-31 17:22:41 +03:00
|
|
|
default:
|
2001-09-07 08:12:10 +04:00
|
|
|
usage();
|
2000-04-16 20:15:59 +04:00
|
|
|
/*NOTREACHED*/
|
1999-07-02 21:41:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_supported()
|
|
|
|
{
|
|
|
|
|
2000-10-04 01:48:33 +04:00
|
|
|
if (pfkey_send_register(so, SADB_SATYPE_UNSPEC) < 0)
|
1999-07-02 21:41:23 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (pfkey_recv_register(so) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sendkeyshort(type)
|
2000-01-31 17:22:41 +03:00
|
|
|
u_int type;
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
struct sadb_msg msg;
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
msg.sadb_msg_version = PF_KEY_V2;
|
|
|
|
msg.sadb_msg_type = type;
|
|
|
|
msg.sadb_msg_errno = 0;
|
|
|
|
msg.sadb_msg_satype = SADB_SATYPE_UNSPEC;
|
|
|
|
msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg));
|
|
|
|
msg.sadb_msg_reserved = 0;
|
|
|
|
msg.sadb_msg_seq = 0;
|
|
|
|
msg.sadb_msg_pid = getpid();
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
sendkeymsg((char *)&msg, sizeof(msg));
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
promisc()
|
|
|
|
{
|
2001-09-07 08:12:10 +04:00
|
|
|
struct sadb_msg msg;
|
1999-07-02 21:41:23 +04:00
|
|
|
u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
|
2001-09-07 08:12:10 +04:00
|
|
|
ssize_t l;
|
|
|
|
|
|
|
|
msg.sadb_msg_version = PF_KEY_V2;
|
|
|
|
msg.sadb_msg_type = SADB_X_PROMISC;
|
|
|
|
msg.sadb_msg_errno = 0;
|
|
|
|
msg.sadb_msg_satype = 1;
|
|
|
|
msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg));
|
|
|
|
msg.sadb_msg_reserved = 0;
|
|
|
|
msg.sadb_msg_seq = 0;
|
|
|
|
msg.sadb_msg_pid = getpid();
|
|
|
|
|
|
|
|
if ((l = send(so, &msg, sizeof(msg), 0)) < 0) {
|
1999-07-02 21:41:23 +04:00
|
|
|
err(1, "send");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct sadb_msg *base;
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
if ((l = recv(so, rbuf, sizeof(*base), MSG_PEEK)) < 0) {
|
1999-07-02 21:41:23 +04:00
|
|
|
err(1, "recv");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
if (l != sizeof(*base))
|
1999-07-02 21:41:23 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
base = (struct sadb_msg *)rbuf;
|
2001-09-07 08:12:10 +04:00
|
|
|
if ((l = recv(so, rbuf, PFKEY_UNUNIT64(base->sadb_msg_len),
|
1999-07-02 21:41:23 +04:00
|
|
|
0)) < 0) {
|
|
|
|
err(1, "recv");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
2001-09-07 08:12:10 +04:00
|
|
|
printdate();
|
1999-07-02 21:41:23 +04:00
|
|
|
if (f_hexdump) {
|
|
|
|
int i;
|
2001-09-07 08:12:10 +04:00
|
|
|
for (i = 0; i < l; i++) {
|
1999-07-02 21:41:23 +04:00
|
|
|
if (i % 16 == 0)
|
|
|
|
printf("%08x: ", i);
|
|
|
|
printf("%02x ", rbuf[i] & 0xff);
|
|
|
|
if (i % 16 == 15)
|
|
|
|
printf("\n");
|
|
|
|
}
|
2001-09-07 08:12:10 +04:00
|
|
|
if (l % 16)
|
1999-07-02 21:41:23 +04:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
/* adjust base pointer for promisc mode */
|
|
|
|
if (base->sadb_msg_type == SADB_X_PROMISC) {
|
2003-07-01 11:20:12 +04:00
|
|
|
if ((ssize_t)sizeof(*base) < l)
|
1999-07-02 21:41:23 +04:00
|
|
|
base++;
|
|
|
|
else
|
|
|
|
base = NULL;
|
|
|
|
}
|
|
|
|
if (base) {
|
|
|
|
kdebug_sadb(base);
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-07 08:12:10 +04:00
|
|
|
sendkeymsg(buf, len)
|
|
|
|
char *buf;
|
|
|
|
size_t len;
|
1999-07-02 21:41:23 +04:00
|
|
|
{
|
|
|
|
u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
|
2001-09-07 08:12:10 +04:00
|
|
|
ssize_t l;
|
1999-07-02 21:41:23 +04:00
|
|
|
struct sadb_msg *msg;
|
|
|
|
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
|
|
|
|
perror("setsockopt");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f_forever)
|
|
|
|
shortdump_hdr();
|
|
|
|
again:
|
2000-01-31 17:22:41 +03:00
|
|
|
if (f_verbose) {
|
2001-09-07 08:12:10 +04:00
|
|
|
kdebug_sadb((struct sadb_msg *)buf);
|
2000-01-31 17:22:41 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
2003-07-01 11:20:12 +04:00
|
|
|
if (f_hexdump) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (i % 16 == 0)
|
|
|
|
printf("%08x: ", i);
|
|
|
|
printf("%02x ", buf[i] & 0xff);
|
|
|
|
if (i % 16 == 15)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
if (len % 16)
|
|
|
|
printf("\n");
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
if ((l = send(so, buf, len, 0)) < 0) {
|
1999-07-02 21:41:23 +04:00
|
|
|
perror("send");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = (struct sadb_msg *)rbuf;
|
|
|
|
do {
|
2001-09-07 08:12:10 +04:00
|
|
|
if ((l = recv(so, rbuf, sizeof(rbuf), 0)) < 0) {
|
1999-07-02 21:41:23 +04:00
|
|
|
perror("recv");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2001-09-07 08:12:10 +04:00
|
|
|
if (PFKEY_UNUNIT64(msg->sadb_msg_len) != l) {
|
2000-01-31 17:22:41 +03:00
|
|
|
warnx("invalid keymsg length");
|
1999-07-02 21:41:23 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
if (f_verbose) {
|
1999-07-02 21:41:23 +04:00
|
|
|
kdebug_sadb((struct sadb_msg *)rbuf);
|
2000-01-31 17:22:41 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
2001-09-07 08:12:10 +04:00
|
|
|
if (postproc(msg, l) < 0)
|
1999-07-02 21:41:23 +04:00
|
|
|
break;
|
|
|
|
} while (msg->sadb_msg_errno || msg->sadb_msg_seq);
|
|
|
|
|
|
|
|
if (f_forever) {
|
|
|
|
fflush(stdout);
|
|
|
|
sleep(1);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
postproc(msg, len)
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
|
|
|
|
if (msg->sadb_msg_errno != 0) {
|
|
|
|
char inf[80];
|
2001-09-07 08:12:10 +04:00
|
|
|
const char *errmsg = NULL;
|
1999-07-02 21:41:23 +04:00
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
if (f_mode == MODE_SCRIPT)
|
1999-07-02 21:41:23 +04:00
|
|
|
snprintf(inf, sizeof(inf), "The result of line %d: ", lineno);
|
|
|
|
else
|
|
|
|
inf[0] = '\0';
|
|
|
|
|
|
|
|
switch (msg->sadb_msg_errno) {
|
|
|
|
case ENOENT:
|
|
|
|
switch (msg->sadb_msg_type) {
|
|
|
|
case SADB_DELETE:
|
|
|
|
case SADB_GET:
|
|
|
|
case SADB_X_SPDDELETE:
|
|
|
|
errmsg = "No entry";
|
|
|
|
break;
|
|
|
|
case SADB_DUMP:
|
|
|
|
errmsg = "No SAD entries";
|
|
|
|
break;
|
|
|
|
case SADB_X_SPDDUMP:
|
|
|
|
errmsg = "No SPD entries";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errmsg = strerror(msg->sadb_msg_errno);
|
|
|
|
}
|
|
|
|
printf("%s%s.\n", inf, errmsg);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg->sadb_msg_type) {
|
|
|
|
case SADB_GET:
|
|
|
|
pfkey_sadump(msg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_DUMP:
|
|
|
|
/* filter out DEAD SAs */
|
|
|
|
if (!f_all) {
|
|
|
|
caddr_t mhp[SADB_EXT_MAX + 1];
|
|
|
|
struct sadb_sa *sa;
|
2000-01-31 17:22:41 +03:00
|
|
|
pfkey_align(msg, mhp);
|
|
|
|
pfkey_check(mhp);
|
1999-07-02 21:41:23 +04:00
|
|
|
if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) {
|
|
|
|
if (sa->sadb_sa_state == SADB_SASTATE_DEAD)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (f_forever)
|
|
|
|
shortdump(msg);
|
|
|
|
else
|
|
|
|
pfkey_sadump(msg);
|
|
|
|
msg = (struct sadb_msg *)((caddr_t)msg +
|
|
|
|
PFKEY_UNUNIT64(msg->sadb_msg_len));
|
2000-01-31 17:22:41 +03:00
|
|
|
if (f_verbose) {
|
1999-07-02 21:41:23 +04:00
|
|
|
kdebug_sadb((struct sadb_msg *)msg);
|
2000-01-31 17:22:41 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SADB_X_SPDDUMP:
|
|
|
|
pfkey_spdump(msg);
|
|
|
|
if (msg->sadb_msg_seq == 0) break;
|
|
|
|
msg = (struct sadb_msg *)((caddr_t)msg +
|
|
|
|
PFKEY_UNUNIT64(msg->sadb_msg_len));
|
2000-01-31 17:22:41 +03:00
|
|
|
if (f_verbose) {
|
1999-07-02 21:41:23 +04:00
|
|
|
kdebug_sadb((struct sadb_msg *)msg);
|
2000-01-31 17:22:41 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
1999-07-02 21:41:23 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
2001-09-07 08:12:10 +04:00
|
|
|
static const char *satype[] = {
|
1999-07-02 21:41:23 +04:00
|
|
|
NULL, NULL, "ah", "esp"
|
|
|
|
};
|
2001-09-07 08:12:10 +04:00
|
|
|
static const char *sastate[] = {
|
1999-07-02 21:41:23 +04:00
|
|
|
"L", "M", "D", "d"
|
|
|
|
};
|
2001-09-07 08:12:10 +04:00
|
|
|
static const char *ipproto[] = {
|
1999-07-02 21:41:23 +04:00
|
|
|
/*0*/ "ip", "icmp", "igmp", "ggp", "ip4",
|
|
|
|
NULL, "tcp", NULL, "egp", NULL,
|
|
|
|
/*10*/ NULL, NULL, NULL, NULL, NULL,
|
|
|
|
NULL, NULL, "udp", NULL, NULL,
|
|
|
|
/*20*/ NULL, NULL, "idp", NULL, NULL,
|
|
|
|
NULL, NULL, NULL, NULL, "tp",
|
|
|
|
/*30*/ NULL, NULL, NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL, NULL, NULL,
|
|
|
|
/*40*/ NULL, "ip6", NULL, "rt6", "frag6",
|
|
|
|
NULL, "rsvp", "gre", NULL, NULL,
|
|
|
|
/*50*/ "esp", "ah", NULL, NULL, NULL,
|
2000-01-31 17:22:41 +03:00
|
|
|
NULL, NULL, NULL, "icmp6", "none",
|
1999-07-02 21:41:23 +04:00
|
|
|
/*60*/ "dst6",
|
|
|
|
};
|
|
|
|
|
|
|
|
#define STR_OR_ID(x, tab) \
|
|
|
|
(((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
|
|
|
|
|
|
|
|
const char *
|
|
|
|
numstr(x)
|
|
|
|
int x;
|
|
|
|
{
|
|
|
|
static char buf[20];
|
|
|
|
snprintf(buf, sizeof(buf), "#%d", x);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
shortdump_hdr()
|
|
|
|
{
|
|
|
|
printf("%-4s %-3s %-1s %-8s %-7s %s -> %s\n",
|
|
|
|
"time", "p", "s", "spi", "ltime", "src", "dst");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
shortdump(msg)
|
|
|
|
struct sadb_msg *msg;
|
|
|
|
{
|
|
|
|
caddr_t mhp[SADB_EXT_MAX + 1];
|
2003-04-15 11:32:06 +04:00
|
|
|
char buf[NI_MAXHOST], pbuf[NI_MAXSERV];
|
1999-07-02 21:41:23 +04:00
|
|
|
struct sadb_sa *sa;
|
|
|
|
struct sadb_address *saddr;
|
|
|
|
struct sadb_lifetime *lts, *lth, *ltc;
|
|
|
|
struct sockaddr *s;
|
|
|
|
u_int t;
|
|
|
|
time_t cur = time(0);
|
|
|
|
|
2000-01-31 17:22:41 +03:00
|
|
|
pfkey_align(msg, mhp);
|
|
|
|
pfkey_check(mhp);
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
printf("%02lu%02lu", (u_long)(cur % 3600) / 60, (u_long)(cur % 60));
|
|
|
|
|
|
|
|
printf(" %-3s", STR_OR_ID(msg->sadb_msg_satype, satype));
|
|
|
|
|
|
|
|
if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) {
|
|
|
|
printf(" %-1s", STR_OR_ID(sa->sadb_sa_state, sastate));
|
|
|
|
printf(" %08x", (u_int32_t)ntohl(sa->sadb_sa_spi));
|
|
|
|
} else
|
|
|
|
printf("%-1s %-8s", "?", "?");
|
|
|
|
|
|
|
|
lts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
|
|
|
|
lth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
|
|
|
|
ltc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
|
|
|
|
if (lts && lth && ltc) {
|
|
|
|
if (ltc->sadb_lifetime_addtime == 0)
|
|
|
|
t = (u_long)0;
|
|
|
|
else
|
|
|
|
t = (u_long)(cur - ltc->sadb_lifetime_addtime);
|
|
|
|
if (t >= 1000)
|
2001-09-07 08:12:10 +04:00
|
|
|
strlcpy(buf, " big/", sizeof(buf));
|
1999-07-02 21:41:23 +04:00
|
|
|
else
|
|
|
|
snprintf(buf, sizeof(buf), " %3lu/", (u_long)t);
|
|
|
|
printf("%s", buf);
|
|
|
|
|
|
|
|
t = (u_long)lth->sadb_lifetime_addtime;
|
|
|
|
if (t >= 1000)
|
2001-09-07 08:12:10 +04:00
|
|
|
strlcpy(buf, "big", sizeof(buf));
|
1999-07-02 21:41:23 +04:00
|
|
|
else
|
|
|
|
snprintf(buf, sizeof(buf), "%-3lu", (u_long)t);
|
|
|
|
printf("%s", buf);
|
|
|
|
} else
|
2000-12-20 04:05:22 +03:00
|
|
|
printf(" ??\?/???"); /* backslash to avoid trigraph ??/ */
|
1999-07-02 21:41:23 +04:00
|
|
|
|
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]) != NULL) {
|
|
|
|
if (saddr->sadb_address_proto)
|
|
|
|
printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto));
|
|
|
|
s = (struct sockaddr *)(saddr + 1);
|
|
|
|
getnameinfo(s, s->sa_len, buf, sizeof(buf),
|
|
|
|
pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV);
|
|
|
|
if (strcmp(pbuf, "0") != 0)
|
|
|
|
printf("%s[%s]", buf, pbuf);
|
|
|
|
else
|
|
|
|
printf("%s", buf);
|
|
|
|
} else
|
|
|
|
printf("?");
|
|
|
|
|
|
|
|
printf(" -> ");
|
|
|
|
|
|
|
|
if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]) != NULL) {
|
|
|
|
if (saddr->sadb_address_proto)
|
|
|
|
printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto));
|
|
|
|
|
|
|
|
s = (struct sockaddr *)(saddr + 1);
|
|
|
|
getnameinfo(s, s->sa_len, buf, sizeof(buf),
|
|
|
|
pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV);
|
|
|
|
if (strcmp(pbuf, "0") != 0)
|
|
|
|
printf("%s[%s]", buf, pbuf);
|
|
|
|
else
|
|
|
|
printf("%s", buf);
|
|
|
|
} else
|
|
|
|
printf("?");
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
2001-09-07 08:12:10 +04:00
|
|
|
|
|
|
|
/* From: tcpdump(1):gmt2local.c and util.c */
|
|
|
|
/*
|
|
|
|
* Print the timestamp
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
printdate()
|
|
|
|
{
|
|
|
|
struct timeval tp;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if (gettimeofday(&tp, NULL) == -1) {
|
|
|
|
perror("gettimeofday");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f_tflag == 1) {
|
|
|
|
/* Default */
|
|
|
|
s = (tp.tv_sec + thiszone ) % 86400;
|
|
|
|
(void)printf("%02d:%02d:%02d.%06u ",
|
|
|
|
s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tp.tv_usec);
|
|
|
|
} else if (f_tflag > 1) {
|
|
|
|
/* Unix timeval style */
|
|
|
|
(void)printf("%u.%06u ",
|
|
|
|
(u_int32_t)tp.tv_sec, (u_int32_t)tp.tv_usec);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the difference between gmt and local time in seconds.
|
|
|
|
* Use gmtime() and localtime() to keep things simple.
|
|
|
|
*/
|
|
|
|
int32_t
|
|
|
|
gmt2local(time_t t)
|
|
|
|
{
|
|
|
|
register int dt, dir;
|
|
|
|
register struct tm *gmt, *loc;
|
|
|
|
struct tm sgmt;
|
|
|
|
|
|
|
|
if (t == 0)
|
|
|
|
t = time(NULL);
|
|
|
|
gmt = &sgmt;
|
|
|
|
*gmt = *gmtime(&t);
|
|
|
|
loc = localtime(&t);
|
|
|
|
dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
|
|
|
|
(loc->tm_min - gmt->tm_min) * 60;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the year or julian day is different, we span 00:00 GMT
|
|
|
|
* and must add or subtract a day. Check the year first to
|
|
|
|
* avoid problems when the julian day wraps.
|
|
|
|
*/
|
|
|
|
dir = loc->tm_year - gmt->tm_year;
|
|
|
|
if (dir == 0)
|
|
|
|
dir = loc->tm_yday - gmt->tm_yday;
|
|
|
|
dt += dir * 24 * 60 * 60;
|
|
|
|
|
|
|
|
return (dt);
|
|
|
|
}
|