2 /* $KAME: setkey.c,v 1.28 2003/06/27 07:15:45 itojun Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
40 #include <net/route.h>
41 #include <netinet/in.h>
42 #include <net/pfkeyv2.h>
43 #include <netipsec/keydb.h>
44 #include <netipsec/key_debug.h>
45 #include <netipsec/ipsec.h>
59 int main(int, char **);
60 int get_supported(void);
61 void sendkeyshort(u_int, uint8_t);
63 int sendkeymsg(char *, size_t);
64 int postproc(struct sadb_msg *, int);
65 const char *numstr(int);
66 void shortdump_hdr(void);
67 void shortdump(struct sadb_msg *);
68 static void printdate(void);
69 static int32_t gmt2local(time_t);
72 #define MODE_CMDDUMP 2
73 #define MODE_CMDFLUSH 3
74 #define MODE_PROMISC 4
87 static time_t thiszone;
91 extern int parse(FILE **);
97 printf("usage: setkey [-v] -c\n");
98 printf(" setkey [-v] -f filename\n");
99 printf(" setkey [-Pagltv] -D\n");
100 printf(" setkey [-Pv] -F\n");
101 printf(" setkey [-h] -x\n");
118 thiszone = gmt2local(0);
120 while ((c = getopt(ac, av, "acdf:ghltvxDFP")) != -1) {
123 f_mode = MODE_SCRIPT;
127 f_mode = MODE_SCRIPT;
128 if ((fp = fopen(optarg, "r")) == NULL) {
134 f_mode = MODE_CMDDUMP;
137 f_mode = MODE_CMDFLUSH;
149 f_mode = MODE_PROMISC;
155 case 'g': /* global */
156 f_scope |= IPSEC_POLICYSCOPE_GLOBAL;
158 case 't': /* tunnel */
159 f_scope |= IPSEC_POLICYSCOPE_IFNET;
172 perror("pfkey_open");
178 sendkeyshort(f_policy ? SADB_X_SPDDUMP: SADB_DUMP,
179 f_policy ? f_scope: SADB_SATYPE_UNSPEC);
182 sendkeyshort(f_policy ? SADB_X_SPDFLUSH: SADB_FLUSH,
186 if (get_supported() < 0) {
187 errx(-1, "%s", ipsec_strerror());
208 if (pfkey_send_register(so, SADB_SATYPE_UNSPEC) < 0)
211 if (pfkey_recv_register(so) < 0)
218 sendkeyshort(u_int type, uint8_t satype)
222 msg.sadb_msg_version = PF_KEY_V2;
223 msg.sadb_msg_type = type;
224 msg.sadb_msg_errno = 0;
225 msg.sadb_msg_satype = satype;
226 msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg));
227 msg.sadb_msg_reserved = 0;
228 msg.sadb_msg_seq = 0;
229 msg.sadb_msg_pid = getpid();
231 sendkeymsg((char *)&msg, sizeof(msg));
240 u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
243 msg.sadb_msg_version = PF_KEY_V2;
244 msg.sadb_msg_type = SADB_X_PROMISC;
245 msg.sadb_msg_errno = 0;
246 msg.sadb_msg_satype = 1;
247 msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg));
248 msg.sadb_msg_reserved = 0;
249 msg.sadb_msg_seq = 0;
250 msg.sadb_msg_pid = getpid();
252 if ((l = send(so, &msg, sizeof(msg), 0)) < 0) {
258 struct sadb_msg *base;
260 if ((l = recv(so, rbuf, sizeof(*base), MSG_PEEK)) < 0) {
265 if (l != sizeof(*base))
268 base = (struct sadb_msg *)rbuf;
269 if ((l = recv(so, rbuf, PFKEY_UNUNIT64(base->sadb_msg_len),
277 for (i = 0; i < l; i++) {
280 printf("%02x ", rbuf[i] & 0xff);
287 /* adjust base pointer for promisc mode */
288 if (base->sadb_msg_type == SADB_X_PROMISC) {
289 if ((ssize_t)sizeof(*base) < l)
307 u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
309 struct sadb_msg *msg;
315 if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
316 perror("setsockopt");
325 kdebug_sadb((struct sadb_msg *)buf);
330 for (i = 0; i < len; i++) {
333 printf("%02x ", buf[i] & 0xff);
341 if ((l = send(so, buf, len, 0)) < 0) {
346 msg = (struct sadb_msg *)rbuf;
348 if ((l = recv(so, rbuf, sizeof(rbuf), 0)) < 0) {
353 if (PFKEY_UNUNIT64(msg->sadb_msg_len) != l) {
354 warnx("invalid keymsg length");
359 kdebug_sadb((struct sadb_msg *)rbuf);
362 if (postproc(msg, l) < 0)
364 } while (msg->sadb_msg_errno || msg->sadb_msg_seq);
378 struct sadb_msg *msg;
382 if (msg->sadb_msg_errno != 0) {
384 const char *errmsg = NULL;
386 if (f_mode == MODE_SCRIPT)
387 snprintf(inf, sizeof(inf), "The result of line %d: ", lineno);
391 switch (msg->sadb_msg_errno) {
393 switch (msg->sadb_msg_type) {
396 case SADB_X_SPDDELETE:
400 errmsg = "No SAD entries";
403 errmsg = "No SPD entries";
408 errmsg = strerror(msg->sadb_msg_errno);
410 printf("%s%s.\n", inf, errmsg);
414 switch (msg->sadb_msg_type) {
420 /* filter out DEAD SAs */
422 caddr_t mhp[SADB_EXT_MAX + 1];
424 pfkey_align(msg, mhp);
426 if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) {
427 if (sa->sadb_sa_state == SADB_SASTATE_DEAD)
435 msg = (struct sadb_msg *)((caddr_t)msg +
436 PFKEY_UNUNIT64(msg->sadb_msg_len));
438 kdebug_sadb((struct sadb_msg *)msg);
445 if (msg->sadb_msg_seq == 0) break;
446 msg = (struct sadb_msg *)((caddr_t)msg +
447 PFKEY_UNUNIT64(msg->sadb_msg_len));
449 kdebug_sadb((struct sadb_msg *)msg);
458 /*------------------------------------------------------------*/
459 static const char *satype[] = {
460 NULL, NULL, "ah", "esp"
462 static const char *sastate[] = {
465 static const char *ipproto[] = {
466 /*0*/ "ip", "icmp", "igmp", "ggp", "ip4",
467 NULL, "tcp", NULL, "egp", NULL,
468 /*10*/ NULL, NULL, NULL, NULL, NULL,
469 NULL, NULL, "udp", NULL, NULL,
470 /*20*/ NULL, NULL, "idp", NULL, NULL,
471 NULL, NULL, NULL, NULL, "tp",
472 /*30*/ NULL, NULL, NULL, NULL, NULL,
473 NULL, NULL, NULL, NULL, NULL,
474 /*40*/ NULL, "ip6", NULL, "rt6", "frag6",
475 NULL, "rsvp", "gre", NULL, NULL,
476 /*50*/ "esp", "ah", NULL, NULL, NULL,
477 NULL, NULL, NULL, "icmp6", "none",
481 #define STR_OR_ID(x, tab) \
482 (((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
489 snprintf(buf, sizeof(buf), "#%d", x);
496 printf("%-4s %-3s %-1s %-8s %-7s %s -> %s\n",
497 "time", "p", "s", "spi", "ltime", "src", "dst");
502 struct sadb_msg *msg;
504 caddr_t mhp[SADB_EXT_MAX + 1];
505 char buf[NI_MAXHOST], pbuf[NI_MAXSERV];
507 struct sadb_address *saddr;
508 struct sadb_lifetime *lts, *lth, *ltc;
511 time_t cur = time(0);
513 pfkey_align(msg, mhp);
516 printf("%02lu%02lu", (u_long)(cur % 3600) / 60, (u_long)(cur % 60));
518 printf(" %-3s", STR_OR_ID(msg->sadb_msg_satype, satype));
520 if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) {
521 printf(" %-1s", STR_OR_ID(sa->sadb_sa_state, sastate));
522 printf(" %08x", (u_int32_t)ntohl(sa->sadb_sa_spi));
524 printf("%-1s %-8s", "?", "?");
526 lts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
527 lth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
528 ltc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
529 if (lts && lth && ltc) {
530 if (ltc->sadb_lifetime_addtime == 0)
533 t = (u_long)(cur - ltc->sadb_lifetime_addtime);
535 strlcpy(buf, " big/", sizeof(buf));
537 snprintf(buf, sizeof(buf), " %3lu/", (u_long)t);
540 t = (u_long)lth->sadb_lifetime_addtime;
542 strlcpy(buf, "big", sizeof(buf));
544 snprintf(buf, sizeof(buf), "%-3lu", (u_long)t);
547 printf(" ??\?/???"); /* backslash to avoid trigraph ??/ */
551 if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]) != NULL) {
552 if (saddr->sadb_address_proto)
553 printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto));
554 s = (struct sockaddr *)(saddr + 1);
555 getnameinfo(s, s->sa_len, buf, sizeof(buf),
556 pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV);
557 if (strcmp(pbuf, "0") != 0)
558 printf("%s[%s]", buf, pbuf);
566 if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]) != NULL) {
567 if (saddr->sadb_address_proto)
568 printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto));
570 s = (struct sockaddr *)(saddr + 1);
571 getnameinfo(s, s->sa_len, buf, sizeof(buf),
572 pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV);
573 if (strcmp(pbuf, "0") != 0)
574 printf("%s[%s]", buf, pbuf);
583 /* From: tcpdump(1):gmt2local.c and util.c */
585 * Print the timestamp
593 if (gettimeofday(&tp, NULL) == -1) {
594 perror("gettimeofday");
600 s = (tp.tv_sec + thiszone ) % 86400;
601 (void)printf("%02d:%02d:%02d.%06u ",
602 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tp.tv_usec);
603 } else if (f_tflag > 1) {
604 /* Unix timeval style */
605 (void)printf("%u.%06u ",
606 (u_int32_t)tp.tv_sec, (u_int32_t)tp.tv_usec);
613 * Returns the difference between gmt and local time in seconds.
614 * Use gmtime() and localtime() to keep things simple.
619 register int dt, dir;
620 register struct tm *gmt, *loc;
628 dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
629 (loc->tm_min - gmt->tm_min) * 60;
632 * If the year or julian day is different, we span 00:00 GMT
633 * and must add or subtract a day. Check the year first to
634 * avoid problems when the julian day wraps.
636 dir = loc->tm_year - gmt->tm_year;
638 dir = loc->tm_yday - gmt->tm_yday;
639 dt += dir * 24 * 60 * 60;