1 /* $KAME: setkey.c,v 1.28 2003/06/27 07:15:45 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/linker.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
41 #include <net/route.h>
42 #include <netinet/in.h>
43 #include <net/pfkeyv2.h>
44 #include <netipsec/keydb.h>
45 #include <netipsec/key_debug.h>
46 #include <netipsec/ipsec.h>
60 int main(int, char **);
61 int get_supported(void);
62 void sendkeyshort(u_int, uint8_t);
64 int sendkeymsg(char *, size_t);
65 int postproc(struct sadb_msg *);
66 const char *numstr(int);
67 void shortdump_hdr(void);
68 void shortdump(struct sadb_msg *);
69 static void printdate(void);
70 static int32_t gmt2local(time_t);
71 static int modload(const char *name);
74 #define MODE_CMDDUMP 2
75 #define MODE_CMDFLUSH 3
76 #define MODE_PROMISC 4
80 static int f_forever = 0;
82 static int f_verbose = 0;
83 static int f_mode = 0;
84 static int f_cmddump = 0;
85 static int f_policy = 0;
86 static int f_hexdump = 0;
87 static int f_tflag = 0;
88 static int f_scope = 0;
89 static time_t thiszone;
93 extern int parse(FILE **);
99 printf("usage: setkey [-v] -c\n");
100 printf(" setkey [-v] -f filename\n");
101 printf(" setkey [-v] -e \"<script>\"\n");
102 printf(" setkey [-Pagltv] -D\n");
103 printf(" setkey [-Pv] -F\n");
104 printf(" setkey [-h] -x\n");
109 modload(const char *name)
111 if (modfind(name) < 0)
112 if (kldload(name) < 0 || modfind(name) < 0) {
113 warn("%s: module not found", name);
120 main(int ac, char **av)
130 thiszone = gmt2local(0);
132 while ((c = getopt(ac, av, "acde:f:ghltvxDFP")) != -1) {
135 f_mode = MODE_SCRIPT;
140 err(-1, "only one -f/-e option is accepted");
142 f_mode = MODE_SCRIPT;
143 fp = fmemopen(optarg, strlen(optarg), "r");
151 err(-1, "only one -f/-e option is accepted");
153 f_mode = MODE_SCRIPT;
154 if ((fp = fopen(optarg, "r")) == NULL) {
160 f_mode = MODE_CMDDUMP;
163 f_mode = MODE_CMDFLUSH;
175 f_mode = MODE_PROMISC;
181 case 'g': /* global */
182 f_scope |= IPSEC_POLICYSCOPE_GLOBAL;
184 case 't': /* tunnel */
185 f_scope |= IPSEC_POLICYSCOPE_IFNET;
199 perror("pfkey_open");
205 sendkeyshort(f_policy ? SADB_X_SPDDUMP: SADB_DUMP,
206 f_policy ? f_scope: SADB_SATYPE_UNSPEC);
209 sendkeyshort(f_policy ? SADB_X_SPDFLUSH: SADB_FLUSH,
213 if (get_supported() < 0) {
214 errx(-1, "%s", ipsec_strerror());
235 if (pfkey_send_register(so, SADB_SATYPE_UNSPEC) < 0)
238 if (pfkey_recv_register(so) < 0)
245 sendkeyshort(u_int type, uint8_t satype)
249 msg.sadb_msg_version = PF_KEY_V2;
250 msg.sadb_msg_type = type;
251 msg.sadb_msg_errno = 0;
252 msg.sadb_msg_satype = satype;
253 msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg));
254 msg.sadb_msg_reserved = 0;
255 msg.sadb_msg_seq = 0;
256 msg.sadb_msg_pid = getpid();
258 sendkeymsg((char *)&msg, sizeof(msg));
267 u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
270 msg.sadb_msg_version = PF_KEY_V2;
271 msg.sadb_msg_type = SADB_X_PROMISC;
272 msg.sadb_msg_errno = 0;
273 msg.sadb_msg_satype = 1;
274 msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg));
275 msg.sadb_msg_reserved = 0;
276 msg.sadb_msg_seq = 0;
277 msg.sadb_msg_pid = getpid();
279 if ((l = send(so, &msg, sizeof(msg), 0)) < 0) {
285 struct sadb_msg *base;
287 if ((l = recv(so, rbuf, sizeof(*base), MSG_PEEK)) < 0) {
292 if (l != sizeof(*base))
295 base = (struct sadb_msg *)rbuf;
296 if ((l = recv(so, rbuf, PFKEY_UNUNIT64(base->sadb_msg_len),
304 for (i = 0; i < l; i++) {
307 printf("%02x ", rbuf[i] & 0xff);
314 /* adjust base pointer for promisc mode */
315 if (base->sadb_msg_type == SADB_X_PROMISC) {
316 if ((ssize_t)sizeof(*base) < l)
330 sendkeymsg(char *buf, size_t len)
332 u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
334 struct sadb_msg *msg;
340 if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
341 perror("setsockopt");
350 kdebug_sadb((struct sadb_msg *)buf);
355 for (i = 0; i < len; i++) {
357 printf("%08x: ", (u_int)i);
358 printf("%02x ", buf[i] & 0xff);
366 if ((l = send(so, buf, len, 0)) < 0) {
371 msg = (struct sadb_msg *)rbuf;
373 if ((l = recv(so, rbuf, sizeof(rbuf), 0)) < 0) {
378 if (PFKEY_UNUNIT64(msg->sadb_msg_len) != l) {
379 warnx("invalid keymsg length");
384 kdebug_sadb((struct sadb_msg *)rbuf);
387 if (postproc(msg) < 0)
389 } while (msg->sadb_msg_errno || msg->sadb_msg_seq);
402 postproc(struct sadb_msg *msg)
405 if (msg->sadb_msg_errno != 0) {
407 const char *errmsg = NULL;
409 if (f_mode == MODE_SCRIPT)
410 snprintf(inf, sizeof(inf), "The result of line %d: ", lineno);
414 switch (msg->sadb_msg_errno) {
416 switch (msg->sadb_msg_type) {
419 case SADB_X_SPDDELETE:
423 errmsg = "No SAD entries";
426 errmsg = "No SPD entries";
431 errmsg = strerror(msg->sadb_msg_errno);
433 printf("%s%s.\n", inf, errmsg);
437 switch (msg->sadb_msg_type) {
443 /* filter out DEAD SAs */
445 caddr_t mhp[SADB_EXT_MAX + 1];
447 pfkey_align(msg, mhp);
449 if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) {
450 if (sa->sadb_sa_state == SADB_SASTATE_DEAD)
458 msg = (struct sadb_msg *)((caddr_t)msg +
459 PFKEY_UNUNIT64(msg->sadb_msg_len));
461 kdebug_sadb((struct sadb_msg *)msg);
468 if (msg->sadb_msg_seq == 0) break;
469 msg = (struct sadb_msg *)((caddr_t)msg +
470 PFKEY_UNUNIT64(msg->sadb_msg_len));
472 kdebug_sadb((struct sadb_msg *)msg);
481 /*------------------------------------------------------------*/
482 static const char *satype[] = {
483 NULL, NULL, "ah", "esp"
485 static const char *sastate[] = {
488 static const char *ipproto[] = {
489 /*0*/ "ip", "icmp", "igmp", "ggp", "ip4",
490 NULL, "tcp", NULL, "egp", NULL,
491 /*10*/ NULL, NULL, NULL, NULL, NULL,
492 NULL, NULL, "udp", NULL, NULL,
493 /*20*/ NULL, NULL, "idp", NULL, NULL,
494 NULL, NULL, NULL, NULL, "tp",
495 /*30*/ NULL, NULL, NULL, NULL, NULL,
496 NULL, NULL, NULL, NULL, NULL,
497 /*40*/ NULL, "ip6", NULL, "rt6", "frag6",
498 NULL, "rsvp", "gre", NULL, NULL,
499 /*50*/ "esp", "ah", NULL, NULL, NULL,
500 NULL, NULL, NULL, "icmp6", "none",
504 #define STR_OR_ID(x, tab) \
505 (((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
511 snprintf(buf, sizeof(buf), "#%d", x);
518 printf("%-4s %-3s %-1s %-8s %-7s %s -> %s\n",
519 "time", "p", "s", "spi", "ltime", "src", "dst");
523 shortdump(struct sadb_msg *msg)
525 caddr_t mhp[SADB_EXT_MAX + 1];
526 char buf[NI_MAXHOST], pbuf[NI_MAXSERV];
528 struct sadb_address *saddr;
529 struct sadb_lifetime *lts, *lth, *ltc;
532 time_t cur = time(0);
534 pfkey_align(msg, mhp);
537 printf("%02lu%02lu", (u_long)(cur % 3600) / 60, (u_long)(cur % 60));
539 printf(" %-3s", STR_OR_ID(msg->sadb_msg_satype, satype));
541 if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) {
542 printf(" %-1s", STR_OR_ID(sa->sadb_sa_state, sastate));
543 printf(" %08x", (u_int32_t)ntohl(sa->sadb_sa_spi));
545 printf("%-1s %-8s", "?", "?");
547 lts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
548 lth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
549 ltc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
550 if (lts && lth && ltc) {
551 if (ltc->sadb_lifetime_addtime == 0)
554 t = (u_long)(cur - ltc->sadb_lifetime_addtime);
556 strlcpy(buf, " big/", sizeof(buf));
558 snprintf(buf, sizeof(buf), " %3lu/", (u_long)t);
561 t = (u_long)lth->sadb_lifetime_addtime;
563 strlcpy(buf, "big", sizeof(buf));
565 snprintf(buf, sizeof(buf), "%-3lu", (u_long)t);
568 printf(" ??\?/???"); /* backslash to avoid trigraph ??/ */
572 if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]) != NULL) {
573 if (saddr->sadb_address_proto)
574 printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto));
575 s = (struct sockaddr *)(saddr + 1);
576 getnameinfo(s, s->sa_len, buf, sizeof(buf),
577 pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV);
578 if (strcmp(pbuf, "0") != 0)
579 printf("%s[%s]", buf, pbuf);
587 if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]) != NULL) {
588 if (saddr->sadb_address_proto)
589 printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto));
591 s = (struct sockaddr *)(saddr + 1);
592 getnameinfo(s, s->sa_len, buf, sizeof(buf),
593 pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV);
594 if (strcmp(pbuf, "0") != 0)
595 printf("%s[%s]", buf, pbuf);
604 /* From: tcpdump(1):gmt2local.c and util.c */
606 * Print the timestamp
614 if (gettimeofday(&tp, NULL) == -1) {
615 perror("gettimeofday");
621 s = (tp.tv_sec + thiszone ) % 86400;
622 (void)printf("%02d:%02d:%02d.%06u ",
623 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tp.tv_usec);
624 } else if (f_tflag > 1) {
625 /* Unix timeval style */
626 (void)printf("%u.%06u ",
627 (u_int32_t)tp.tv_sec, (u_int32_t)tp.tv_usec);
634 * Returns the difference between gmt and local time in seconds.
635 * Use gmtime() and localtime() to keep things simple.
640 register int dt, dir;
641 register struct tm *gmt, *loc;
649 dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
650 (loc->tm_min - gmt->tm_min) * 60;
653 * If the year or julian day is different, we span 00:00 GMT
654 * and must add or subtract a day. Check the year first to
655 * avoid problems when the julian day wraps.
657 dir = loc->tm_year - gmt->tm_year;
659 dir = loc->tm_yday - gmt->tm_yday;
660 dt += dir * 24 * 60 * 60;