kernel - support dummy reallocblks in devfs
[dragonfly.git] / usr.sbin / setkey / setkey.c
blob468eb99b23d4602ef610b82a0d8f54d960691ecb
1 /* $FreeBSD: src/usr.sbin/setkey/setkey.c,v 1.1.2.3 2003/04/26 23:53:54 sumikawa Exp $ */
2 /* $KAME: setkey.c,v 1.18 2001/05/08 04:36:39 itojun Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #include <err.h>
38 #include <net/route.h>
39 #include <netinet/in.h>
40 #include <net/pfkeyv2.h>
41 #include <netkey/keydb.h>
42 #include <netkey/key_debug.h>
43 #include <netinet6/ipsec.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <limits.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <unistd.h>
51 #include <errno.h>
52 #include <netdb.h>
54 #include "libpfkey.h"
56 void Usage(void);
57 int get_supported(void);
58 void sendkeyshort(u_int);
59 void promisc(void);
60 int sendkeymsg(void);
61 int postproc(struct sadb_msg *, int);
62 const char *numstr(int);
63 void shortdump_hdr(void);
64 void shortdump(struct sadb_msg *);
65 static void printdate(void);
66 static int32_t gmt2local(time_t);
68 #define MODE_SCRIPT 1
69 #define MODE_CMDDUMP 2
70 #define MODE_CMDFLUSH 3
71 #define MODE_PROMISC 4
73 int so;
75 int f_forever = 0;
76 int f_all = 0;
77 int f_debug = 0;
78 int f_verbose = 0;
79 int f_mode = 0;
80 int f_cmddump = 0;
81 int f_policy = 0;
82 int f_hexdump = 0;
83 int f_tflag = 0;
84 char *pname;
86 u_char m_buf[BUFSIZ];
87 u_int m_len;
89 static time_t thiszone;
91 extern int lineno;
93 extern int parse(FILE **);
95 void
96 Usage(void)
98 printf("Usage:\t%s [-dv] -c\n", pname);
99 printf("\t%s [-dv] -f (file)\n", pname);
100 printf("\t%s [-Padlv] -D\n", pname);
101 printf("\t%s [-Pdv] -F\n", pname);
102 printf("\t%s [-h] -x\n", pname);
103 pfkey_close(so);
104 exit(1);
108 main(int argc, char **argv)
110 FILE *fp = stdin;
111 int c;
113 pname = *argv;
115 if (argc == 1) Usage();
117 thiszone = gmt2local(0);
119 while ((c = getopt(argc, argv, "acdf:hlvxDFP")) != -1) {
120 switch (c) {
121 case 'c':
122 f_mode = MODE_SCRIPT;
123 fp = stdin;
124 break;
125 case 'f':
126 f_mode = MODE_SCRIPT;
127 if ((fp = fopen(optarg, "r")) == NULL) {
128 err(-1, "fopen");
129 /*NOTREACHED*/
131 break;
132 case 'D':
133 f_mode = MODE_CMDDUMP;
134 break;
135 case 'F':
136 f_mode = MODE_CMDFLUSH;
137 break;
138 case 'a':
139 f_all = 1;
140 break;
141 case 'l':
142 f_forever = 1;
143 break;
144 case 'h':
145 f_hexdump = 1;
146 break;
147 case 'x':
148 f_mode = MODE_PROMISC;
149 f_tflag++;
150 break;
151 case 'P':
152 f_policy = 1;
153 break;
154 case 'd':
155 f_debug = 1;
156 break;
157 case 'v':
158 f_verbose = 1;
159 break;
160 default:
161 Usage();
162 /*NOTREACHED*/
166 switch (f_mode) {
167 case MODE_CMDDUMP:
168 sendkeyshort(f_policy ? SADB_X_SPDDUMP: SADB_DUMP);
169 break;
170 case MODE_CMDFLUSH:
171 sendkeyshort(f_policy ? SADB_X_SPDFLUSH: SADB_FLUSH);
172 pfkey_close(so);
173 break;
174 case MODE_SCRIPT:
175 if (get_supported() < 0) {
176 errx(-1, "%s", ipsec_strerror());
177 /*NOTREACHED*/
179 if (parse(&fp))
180 exit (1);
181 break;
182 case MODE_PROMISC:
183 promisc();
184 /*NOTREACHED*/
185 default:
186 Usage();
187 /*NOTREACHED*/
190 exit(0);
194 get_supported(void)
196 int so;
198 if ((so = pfkey_open()) < 0) {
199 perror("pfkey_open");
200 return -1;
203 /* debug mode ? */
204 if (f_debug)
205 return 0;
207 if (pfkey_send_register(so, SADB_SATYPE_UNSPEC) < 0)
208 return -1;
210 if (pfkey_recv_register(so) < 0)
211 return -1;
213 return 0;
216 void
217 sendkeyshort(u_int type)
219 struct sadb_msg *m_msg = (struct sadb_msg *)m_buf;
221 m_len = sizeof(struct sadb_msg);
223 m_msg->sadb_msg_version = PF_KEY_V2;
224 m_msg->sadb_msg_type = type;
225 m_msg->sadb_msg_errno = 0;
226 m_msg->sadb_msg_satype = SADB_SATYPE_UNSPEC;
227 m_msg->sadb_msg_len = PFKEY_UNIT64(m_len);
228 m_msg->sadb_msg_reserved = 0;
229 m_msg->sadb_msg_seq = 0;
230 m_msg->sadb_msg_pid = getpid();
232 sendkeymsg();
235 void
236 promisc(void)
238 struct sadb_msg *m_msg = (struct sadb_msg *)m_buf;
239 u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
240 int so, len;
242 m_len = sizeof(struct sadb_msg);
244 m_msg->sadb_msg_version = PF_KEY_V2;
245 m_msg->sadb_msg_type = SADB_X_PROMISC;
246 m_msg->sadb_msg_errno = 0;
247 m_msg->sadb_msg_satype = 1;
248 m_msg->sadb_msg_len = PFKEY_UNIT64(m_len);
249 m_msg->sadb_msg_reserved = 0;
250 m_msg->sadb_msg_seq = 0;
251 m_msg->sadb_msg_pid = getpid();
253 if ((so = socket(PF_KEY, SOCK_RAW, PF_KEY_V2)) < 0) {
254 err(1, "socket(PF_KEY)");
255 /*NOTREACHED*/
258 if ((len = send(so, m_buf, m_len, 0)) < 0) {
259 err(1, "send");
260 /*NOTREACHED*/
263 while (1) {
264 struct sadb_msg *base;
266 if ((len = recv(so, rbuf, sizeof(*base), MSG_PEEK)) < 0) {
267 err(1, "recv");
268 /*NOTREACHED*/
271 if (len != sizeof(*base))
272 continue;
274 base = (struct sadb_msg *)rbuf;
275 if ((len = recv(so, rbuf, PFKEY_UNUNIT64(base->sadb_msg_len),
276 0)) < 0) {
277 err(1, "recv");
278 /*NOTREACHED*/
280 printdate();
281 if (f_hexdump) {
282 int i;
283 for (i = 0; i < len; i++) {
284 if (i % 16 == 0)
285 printf("%08x: ", i);
286 printf("%02x ", rbuf[i] & 0xff);
287 if (i % 16 == 15)
288 printf("\n");
290 if (len % 16)
291 printf("\n");
293 /* adjust base pointer for promisc mode */
294 if (base->sadb_msg_type == SADB_X_PROMISC) {
295 if (sizeof(*base) < len)
296 base++;
297 else
298 base = NULL;
300 if (base) {
301 kdebug_sadb(base);
302 printf("\n");
303 fflush(stdout);
309 sendkeymsg(void)
311 int so;
312 u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
313 int len;
314 struct sadb_msg *msg;
315 struct timeval tv;
317 if ((so = pfkey_open()) < 0) {
318 perror("pfkey_open");
319 return -1;
322 tv.tv_sec = 1;
323 tv.tv_usec = 0;
324 if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
325 perror("setsockopt");
326 goto end;
329 if (f_forever)
330 shortdump_hdr();
331 again:
332 if (f_verbose) {
333 kdebug_sadb((struct sadb_msg *)m_buf);
334 printf("\n");
337 if ((len = send(so, m_buf, m_len, 0)) < 0) {
338 perror("send");
339 goto end;
342 msg = (struct sadb_msg *)rbuf;
343 do {
344 if ((len = recv(so, rbuf, sizeof(rbuf), 0)) < 0) {
345 perror("recv");
346 goto end;
349 if (PFKEY_UNUNIT64(msg->sadb_msg_len) != len) {
350 warnx("invalid keymsg length");
351 break;
354 if (f_verbose) {
355 kdebug_sadb((struct sadb_msg *)rbuf);
356 printf("\n");
358 if (postproc(msg, len) < 0)
359 break;
360 } while (msg->sadb_msg_errno || msg->sadb_msg_seq);
362 if (f_forever) {
363 fflush(stdout);
364 sleep(1);
365 goto again;
368 end:
369 pfkey_close(so);
370 return(0);
374 postproc(struct sadb_msg *msg, int len)
376 if (msg->sadb_msg_errno != 0) {
377 char inf[80];
378 char *errmsg = NULL;
380 if (f_mode == MODE_SCRIPT)
381 snprintf(inf, sizeof(inf), "The result of line %d: ", lineno);
382 else
383 inf[0] = '\0';
385 switch (msg->sadb_msg_errno) {
386 case ENOENT:
387 switch (msg->sadb_msg_type) {
388 case SADB_DELETE:
389 case SADB_GET:
390 case SADB_X_SPDDELETE:
391 errmsg = "No entry";
392 break;
393 case SADB_DUMP:
394 errmsg = "No SAD entries";
395 break;
396 case SADB_X_SPDDUMP:
397 errmsg = "No SPD entries";
398 break;
400 break;
401 default:
402 errmsg = strerror(msg->sadb_msg_errno);
404 printf("%s%s.\n", inf, errmsg);
405 return(-1);
408 switch (msg->sadb_msg_type) {
409 case SADB_GET:
410 pfkey_sadump(msg);
411 break;
413 case SADB_DUMP:
414 /* filter out DEAD SAs */
415 if (!f_all) {
416 caddr_t mhp[SADB_EXT_MAX + 1];
417 struct sadb_sa *sa;
418 pfkey_align(msg, mhp);
419 pfkey_check(mhp);
420 if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) {
421 if (sa->sadb_sa_state == SADB_SASTATE_DEAD)
422 break;
425 if (f_forever)
426 shortdump(msg);
427 else
428 pfkey_sadump(msg);
429 msg = (struct sadb_msg *)((caddr_t)msg +
430 PFKEY_UNUNIT64(msg->sadb_msg_len));
431 if (f_verbose) {
432 kdebug_sadb((struct sadb_msg *)msg);
433 printf("\n");
435 break;
437 case SADB_X_SPDDUMP:
438 pfkey_spdump(msg);
439 if (msg->sadb_msg_seq == 0) break;
440 msg = (struct sadb_msg *)((caddr_t)msg +
441 PFKEY_UNUNIT64(msg->sadb_msg_len));
442 if (f_verbose) {
443 kdebug_sadb((struct sadb_msg *)msg);
444 printf("\n");
446 break;
449 return(0);
452 /*------------------------------------------------------------*/
453 static char *satype[] = {
454 NULL, NULL, "ah", "esp"
456 static char *sastate[] = {
457 "L", "M", "D", "d"
459 static char *ipproto[] = {
460 /*0*/ "ip", "icmp", "igmp", "ggp", "ip4",
461 NULL, "tcp", NULL, "egp", NULL,
462 /*10*/ NULL, NULL, NULL, NULL, NULL,
463 NULL, NULL, "udp", NULL, NULL,
464 /*20*/ NULL, NULL, "idp", NULL, NULL,
465 NULL, NULL, NULL, NULL, "tp",
466 /*30*/ NULL, NULL, NULL, NULL, NULL,
467 NULL, NULL, NULL, NULL, NULL,
468 /*40*/ NULL, "ip6", NULL, "rt6", "frag6",
469 NULL, "rsvp", "gre", NULL, NULL,
470 /*50*/ "esp", "ah", NULL, NULL, NULL,
471 NULL, NULL, NULL, "icmp6", "none",
472 /*60*/ "dst6",
475 #define STR_OR_ID(x, tab) \
476 (((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x))
478 const char *
479 numstr(int x)
481 static char buf[20];
482 snprintf(buf, sizeof(buf), "#%d", x);
483 return buf;
486 void
487 shortdump_hdr(void)
489 printf("%-4s %-3s %-1s %-8s %-7s %s -> %s\n",
490 "time", "p", "s", "spi", "ltime", "src", "dst");
493 void
494 shortdump(struct sadb_msg *msg)
496 caddr_t mhp[SADB_EXT_MAX + 1];
497 char buf[NI_MAXHOST], pbuf[NI_MAXSERV];
498 struct sadb_sa *sa;
499 struct sadb_address *saddr;
500 struct sadb_lifetime *lts, *lth, *ltc;
501 struct sockaddr *s;
502 u_int t;
503 time_t cur;
505 cur = time(0);
507 pfkey_align(msg, mhp);
508 pfkey_check(mhp);
510 printf("%02lu%02lu", (u_long)(cur % 3600) / 60, (u_long)(cur % 60));
512 printf(" %-3s", STR_OR_ID(msg->sadb_msg_satype, satype));
514 if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) {
515 printf(" %-1s", STR_OR_ID(sa->sadb_sa_state, sastate));
516 printf(" %08x", (u_int32_t)ntohl(sa->sadb_sa_spi));
517 } else
518 printf("%-1s %-8s", "?", "?");
520 lts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
521 lth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
522 ltc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
523 if (lts && lth && ltc) {
524 if (ltc->sadb_lifetime_addtime == 0)
525 t = (u_long)0;
526 else
527 t = (u_long)(cur - ltc->sadb_lifetime_addtime);
528 if (t >= 1000)
529 strcpy(buf, " big/");
530 else
531 snprintf(buf, sizeof(buf), " %3lu/", (u_long)t);
532 printf("%s", buf);
534 t = (u_long)lth->sadb_lifetime_addtime;
535 if (t >= 1000)
536 strcpy(buf, "big");
537 else
538 snprintf(buf, sizeof(buf), "%-3lu", (u_long)t);
539 printf("%s", buf);
540 } else
541 printf(" ??\?/???"); /* backslash to avoid trigraph ??/ */
543 printf(" ");
545 if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]) != NULL) {
546 if (saddr->sadb_address_proto)
547 printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto));
548 s = (struct sockaddr *)(saddr + 1);
549 getnameinfo(s, s->sa_len, buf, sizeof(buf),
550 pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV);
551 if (strcmp(pbuf, "0") != 0)
552 printf("%s[%s]", buf, pbuf);
553 else
554 printf("%s", buf);
555 } else
556 printf("?");
558 printf(" -> ");
560 if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]) != NULL) {
561 if (saddr->sadb_address_proto)
562 printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto));
564 s = (struct sockaddr *)(saddr + 1);
565 getnameinfo(s, s->sa_len, buf, sizeof(buf),
566 pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV);
567 if (strcmp(pbuf, "0") != 0)
568 printf("%s[%s]", buf, pbuf);
569 else
570 printf("%s", buf);
571 } else
572 printf("?");
574 printf("\n");
577 /* From: tcpdump(1):gmt2local.c and util.c */
579 * Print the timestamp
581 static void
582 printdate(void)
584 struct timeval tp;
585 int s;
587 if (gettimeofday(&tp, NULL) == -1) {
588 perror("gettimeofday");
589 return;
592 if (f_tflag == 1) {
593 /* Default */
594 s = (tp.tv_sec + thiszone ) % 86400;
595 printf("%02d:%02d:%02d.%06u ",
596 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tp.tv_usec);
597 } else if (f_tflag > 1) {
598 /* Unix timeval style */
599 printf("%u.%06u ", (u_int32_t)tp.tv_sec, (u_int32_t)tp.tv_usec);
602 printf("\n");
606 * Returns the difference between gmt and local time in seconds.
607 * Use gmtime() and localtime() to keep things simple.
609 int32_t
610 gmt2local(time_t t)
612 int dt, dir;
613 struct tm *gmt, *loc;
614 struct tm sgmt;
616 if (t == 0)
617 t = time(NULL);
618 gmt = &sgmt;
619 *gmt = *gmtime(&t);
620 loc = localtime(&t);
621 dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 +
622 (loc->tm_min - gmt->tm_min) * 60;
625 * If the year or julian day is different, we span 00:00 GMT
626 * and must add or subtract a day. Check the year first to
627 * avoid problems when the julian day wraps.
629 dir = loc->tm_year - gmt->tm_year;
630 if (dir == 0)
631 dir = loc->tm_yday - gmt->tm_yday;
632 dt += dir * 24 * 60 * 60;
634 return (dt);