1 /* $NetBSD: setkey.c,v 1.13 2009/03/06 11:45:03 tteras Exp $ */ 2 3 /* $KAME: setkey.c,v 1.36 2003/09/24 23:52:51 itojun Exp $ */ 4 5 /* 6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 34 #ifdef HAVE_CONFIG_H 35 #include "config.h" 36 #endif 37 38 #include <sys/types.h> 39 #include <sys/param.h> 40 #include <sys/socket.h> 41 #include <sys/time.h> 42 #include <sys/stat.h> 43 #include <sys/sysctl.h> 44 #include <err.h> 45 #include <netinet/in.h> 46 #include <net/pfkeyv2.h> 47 #include PATH_IPSEC_H 48 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <limits.h> 52 #include <string.h> 53 #include <ctype.h> 54 #include <unistd.h> 55 #include <errno.h> 56 #include <netdb.h> 57 #include <fcntl.h> 58 #include <dirent.h> 59 #include <time.h> 60 61 #ifdef HAVE_READLINE 62 #include <readline/readline.h> 63 #include <readline/history.h> 64 #endif 65 66 #include "config.h" 67 #include "libpfkey.h" 68 #include "package_version.h" 69 #define extern /* so that variables in extern.h are not extern... */ 70 #include "extern.h" 71 72 #define strlcpy(d,s,l) (strncpy(d,s,l), (d)[(l)-1] = '\0') 73 74 void usage __P((int)); 75 int main __P((int, char **)); 76 int get_supported __P((void)); 77 void sendkeyshort __P((u_int)); 78 void promisc __P((void)); 79 int postproc __P((struct sadb_msg *, int)); 80 int verifypriority __P((struct sadb_msg *m)); 81 int fileproc __P((const char *)); 82 const char *numstr __P((int)); 83 void shortdump_hdr __P((void)); 84 void shortdump __P((struct sadb_msg *)); 85 static void printdate __P((void)); 86 static int32_t gmt2local __P((time_t)); 87 void stdin_loop __P((void)); 88 89 #define MODE_SCRIPT 1 90 #define MODE_CMDDUMP 2 91 #define MODE_CMDFLUSH 3 92 #define MODE_PROMISC 4 93 #define MODE_STDIN 5 94 95 int so; 96 97 int f_forever = 0; 98 int f_all = 0; 99 int f_verbose = 0; 100 int f_mode = 0; 101 int f_cmddump = 0; 102 int f_policy = 0; 103 int f_hexdump = 0; 104 int f_tflag = 0; 105 int f_notreally = 0; 106 int f_withports = 0; 107 #ifdef HAVE_POLICY_FWD 108 int f_rfcmode = 1; 109 #define RK_OPTS "rk" 110 #else 111 int f_rkwarn = 0; 112 #define RK_OPTS "" 113 static void rkwarn(void); 114 static void 115 rkwarn(void) 116 { 117 if (!f_rkwarn) { 118 f_rkwarn = 1; 119 printf("warning: -r and -k options are not supported in this environment\n"); 120 } 121 } 122 123 #endif 124 static time_t thiszone; 125 126 void 127 usage(int only_version) 128 { 129 printf("setkey @(#) %s (%s)\n", TOP_PACKAGE_STRING, TOP_PACKAGE_URL); 130 if (! only_version) { 131 printf("usage: setkey [-v" RK_OPTS "] file ...\n"); 132 printf(" setkey [-nv" RK_OPTS "] -c\n"); 133 printf(" setkey [-nv" RK_OPTS "] -f filename\n"); 134 printf(" setkey [-Palpv" RK_OPTS "] -D\n"); 135 printf(" setkey [-Pv] -F\n"); 136 printf(" setkey [-H] -x\n"); 137 printf(" setkey [-V] [-h]\n"); 138 } 139 exit(1); 140 } 141 142 int 143 main(argc, argv) 144 int argc; 145 char **argv; 146 { 147 FILE *fp = stdin; 148 int c; 149 150 if (argc == 1) { 151 usage(0); 152 /* NOTREACHED */ 153 } 154 155 thiszone = gmt2local(0); 156 157 while ((c = getopt(argc, argv, "acdf:HlnvxDFPphVrk?")) != -1) { 158 switch (c) { 159 case 'c': 160 f_mode = MODE_STDIN; 161 #ifdef HAVE_READLINE 162 /* disable filename completion */ 163 rl_bind_key('\t', rl_insert); 164 #endif 165 break; 166 case 'f': 167 f_mode = MODE_SCRIPT; 168 if ((fp = fopen(optarg, "r")) == NULL) { 169 err(1, "fopen"); 170 /*NOTREACHED*/ 171 } 172 break; 173 case 'D': 174 f_mode = MODE_CMDDUMP; 175 break; 176 case 'F': 177 f_mode = MODE_CMDFLUSH; 178 break; 179 case 'a': 180 f_all = 1; 181 break; 182 case 'l': 183 f_forever = 1; 184 break; 185 case 'n': 186 f_notreally = 1; 187 break; 188 #ifdef __NetBSD__ 189 case 'h': 190 #endif 191 case 'H': 192 f_hexdump = 1; 193 break; 194 case 'x': 195 f_mode = MODE_PROMISC; 196 f_tflag++; 197 break; 198 case 'P': 199 f_policy = 1; 200 break; 201 case 'p': 202 f_withports = 1; 203 break; 204 case 'v': 205 f_verbose = 1; 206 break; 207 case 'r': 208 #ifdef HAVE_POLICY_FWD 209 f_rfcmode = 1; 210 #else 211 rkwarn(); 212 #endif 213 break; 214 case 'k': 215 #ifdef HAVE_POLICY_FWD 216 f_rfcmode = 0; 217 #else 218 rkwarn(); 219 #endif 220 break; 221 case 'V': 222 usage(1); 223 break; 224 /*NOTREACHED*/ 225 #ifndef __NetBSD__ 226 case 'h': 227 #endif 228 case '?': 229 default: 230 usage(0); 231 /*NOTREACHED*/ 232 } 233 } 234 235 argc -= optind; 236 argv += optind; 237 238 if (argc > 0) { 239 while (argc--) 240 if (fileproc(*argv++) < 0) { 241 err(1, "%s", argv[-1]); 242 /*NOTREACHED*/ 243 } 244 exit(0); 245 } 246 247 so = pfkey_open(); 248 if (so < 0) { 249 perror("pfkey_open"); 250 exit(1); 251 } 252 253 switch (f_mode) { 254 case MODE_CMDDUMP: 255 sendkeyshort(f_policy ? SADB_X_SPDDUMP : SADB_DUMP); 256 break; 257 case MODE_CMDFLUSH: 258 sendkeyshort(f_policy ? SADB_X_SPDFLUSH: SADB_FLUSH); 259 break; 260 case MODE_SCRIPT: 261 if (get_supported() < 0) { 262 errx(1, "%s", ipsec_strerror()); 263 /*NOTREACHED*/ 264 } 265 if (parse(&fp)) 266 exit (1); 267 break; 268 case MODE_STDIN: 269 if (get_supported() < 0) { 270 errx(1, "%s", ipsec_strerror()); 271 /*NOTREACHED*/ 272 } 273 stdin_loop(); 274 break; 275 case MODE_PROMISC: 276 promisc(); 277 /*NOTREACHED*/ 278 default: 279 usage(0); 280 /*NOTREACHED*/ 281 } 282 283 exit(0); 284 } 285 286 int 287 get_supported() 288 { 289 290 if (pfkey_send_register(so, SADB_SATYPE_UNSPEC) < 0) 291 return -1; 292 293 if (pfkey_recv_register(so) < 0) 294 return -1; 295 296 return (0); 297 } 298 299 void 300 stdin_loop() 301 { 302 char line[1024], *semicolon, *comment; 303 size_t linelen = 0; 304 305 memset (line, 0, sizeof(line)); 306 307 parse_init(); 308 while (1) { 309 #ifdef HAVE_READLINE 310 char *rbuf; 311 rbuf = readline (""); 312 if (! rbuf) 313 break; 314 #else 315 char rbuf[1024]; 316 rbuf[0] = '\0'; 317 fgets (rbuf, sizeof(rbuf), stdin); 318 if (!rbuf[0]) 319 break; 320 if (rbuf[strlen(rbuf)-1] == '\n') 321 rbuf[strlen(rbuf)-1] = '\0'; 322 #endif 323 comment = strchr(rbuf, '#'); 324 if (comment) 325 *comment = '\0'; 326 327 if (!rbuf[0]) 328 continue; 329 330 linelen += snprintf (&line[linelen], sizeof(line) - linelen, 331 "%s%s", linelen > 0 ? " " : "", rbuf); 332 333 semicolon = strchr(line, ';'); 334 while (semicolon) { 335 char saved_char = *++semicolon; 336 *semicolon = '\0'; 337 #ifdef HAVE_READLINE 338 add_history (line); 339 #endif 340 341 #ifdef HAVE_PFKEY_POLICY_PRIORITY 342 last_msg_type = -1; /* invalid message type */ 343 #endif 344 345 parse_string (line); 346 if (exit_now) 347 return; 348 if (saved_char) { 349 *semicolon = saved_char; 350 linelen = strlen (semicolon); 351 memmove (line, semicolon, linelen + 1); 352 semicolon = strchr(line, ';'); 353 } 354 else { 355 semicolon = NULL; 356 linelen = 0; 357 } 358 } 359 } 360 } 361 362 void 363 sendkeyshort(type) 364 u_int type; 365 { 366 struct sadb_msg msg; 367 368 msg.sadb_msg_version = PF_KEY_V2; 369 msg.sadb_msg_type = type; 370 msg.sadb_msg_errno = 0; 371 msg.sadb_msg_satype = SADB_SATYPE_UNSPEC; 372 msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg)); 373 msg.sadb_msg_reserved = 0; 374 msg.sadb_msg_seq = 0; 375 msg.sadb_msg_pid = getpid(); 376 377 sendkeymsg((char *)&msg, sizeof(msg)); 378 379 return; 380 } 381 382 void 383 promisc() 384 { 385 struct sadb_msg msg; 386 u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */ 387 ssize_t l; 388 389 msg.sadb_msg_version = PF_KEY_V2; 390 msg.sadb_msg_type = SADB_X_PROMISC; 391 msg.sadb_msg_errno = 0; 392 msg.sadb_msg_satype = 1; 393 msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg)); 394 msg.sadb_msg_reserved = 0; 395 msg.sadb_msg_seq = 0; 396 msg.sadb_msg_pid = getpid(); 397 398 if ((l = send(so, &msg, sizeof(msg), 0)) < 0) { 399 err(1, "send"); 400 /*NOTREACHED*/ 401 } 402 403 while (1) { 404 struct sadb_msg *base; 405 406 if ((l = recv(so, rbuf, sizeof(*base), MSG_PEEK)) < 0) { 407 err(1, "recv"); 408 /*NOTREACHED*/ 409 } 410 411 if (l != sizeof(*base)) 412 continue; 413 414 base = (struct sadb_msg *)rbuf; 415 if ((l = recv(so, rbuf, PFKEY_UNUNIT64(base->sadb_msg_len), 416 0)) < 0) { 417 err(1, "recv"); 418 /*NOTREACHED*/ 419 } 420 printdate(); 421 if (f_hexdump) { 422 int i; 423 for (i = 0; i < l; i++) { 424 if (i % 16 == 0) 425 printf("%08x: ", i); 426 printf("%02x ", rbuf[i] & 0xff); 427 if (i % 16 == 15) 428 printf("\n"); 429 } 430 if (l % 16) 431 printf("\n"); 432 } 433 /* adjust base pointer for promisc mode */ 434 if (base->sadb_msg_type == SADB_X_PROMISC) { 435 if ((ssize_t)sizeof(*base) < l) 436 base++; 437 else 438 base = NULL; 439 } 440 if (base) { 441 kdebug_sadb(base); 442 printf("\n"); 443 fflush(stdout); 444 } 445 } 446 } 447 448 /* Generate 'spi' array with SPIs matching 'satype', 'srcs', and 'dsts' 449 * Return value is dynamically generated array of SPIs, also number of 450 * SPIs through num_spi pointer. 451 * On any error, set *num_spi to 0 and return NULL. 452 */ 453 u_int32_t * 454 sendkeymsg_spigrep(satype, srcs, dsts, num_spi) 455 unsigned int satype; 456 struct addrinfo *srcs; 457 struct addrinfo *dsts; 458 int *num_spi; 459 { 460 struct sadb_msg msg, *m; 461 char *buf; 462 size_t len; 463 ssize_t l; 464 u_char rbuf[1024 * 32]; 465 caddr_t mhp[SADB_EXT_MAX + 1]; 466 struct sadb_address *saddr; 467 struct sockaddr *s; 468 struct addrinfo *a; 469 struct sadb_sa *sa; 470 u_int32_t *spi = NULL; 471 int max_spi = 0, fail = 0; 472 473 *num_spi = 0; 474 475 if (f_notreally) { 476 return NULL; 477 } 478 479 { 480 struct timeval tv; 481 tv.tv_sec = 1; 482 tv.tv_usec = 0; 483 if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { 484 perror("setsockopt"); 485 return NULL; 486 } 487 } 488 489 msg.sadb_msg_version = PF_KEY_V2; 490 msg.sadb_msg_type = SADB_DUMP; 491 msg.sadb_msg_errno = 0; 492 msg.sadb_msg_satype = satype; 493 msg.sadb_msg_len = PFKEY_UNIT64(sizeof(msg)); 494 msg.sadb_msg_reserved = 0; 495 msg.sadb_msg_seq = 0; 496 msg.sadb_msg_pid = getpid(); 497 buf = (char *)&msg; 498 len = sizeof(msg); 499 500 if (f_verbose) { 501 kdebug_sadb(&msg); 502 printf("\n"); 503 } 504 if (f_hexdump) { 505 int i; 506 for (i = 0; i < len; i++) { 507 if (i % 16 == 0) 508 printf("%08x: ", i); 509 printf("%02x ", buf[i] & 0xff); 510 if (i % 16 == 15) 511 printf("\n"); 512 } 513 if (len % 16) 514 printf("\n"); 515 } 516 517 if ((l = send(so, buf, len, 0)) < 0) { 518 perror("send"); 519 return NULL; 520 } 521 522 m = (struct sadb_msg *)rbuf; 523 do { 524 if ((l = recv(so, rbuf, sizeof(rbuf), 0)) < 0) { 525 perror("recv"); 526 fail = 1; 527 break; 528 } 529 530 if (PFKEY_UNUNIT64(m->sadb_msg_len) != l) { 531 warnx("invalid keymsg length"); 532 fail = 1; 533 break; 534 } 535 536 if (f_verbose) { 537 kdebug_sadb(m); 538 printf("\n"); 539 } 540 541 if (m->sadb_msg_type != SADB_DUMP) { 542 warnx("unexpected message type"); 543 fail = 1; 544 break; 545 } 546 547 if (m->sadb_msg_errno != 0) { 548 warnx("error encountered"); 549 fail = 1; 550 break; 551 } 552 553 /* match satype */ 554 if (m->sadb_msg_satype != satype) 555 continue; 556 557 pfkey_align(m, mhp); 558 pfkey_check(mhp); 559 560 /* match src */ 561 saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; 562 if (saddr == NULL) 563 continue; 564 s = (struct sockaddr *)(saddr + 1); 565 for (a = srcs; a; a = a->ai_next) 566 if (memcmp(a->ai_addr, s, a->ai_addrlen) == 0) 567 break; 568 if (a == NULL) 569 continue; 570 571 /* match dst */ 572 saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; 573 if (saddr == NULL) 574 continue; 575 s = (struct sockaddr *)(saddr + 1); 576 for (a = dsts; a; a = a->ai_next) 577 if (memcmp(a->ai_addr, s, a->ai_addrlen) == 0) 578 break; 579 if (a == NULL) 580 continue; 581 582 if (*num_spi >= max_spi) { 583 max_spi += 512; 584 spi = realloc(spi, max_spi * sizeof(u_int32_t)); 585 } 586 587 sa = (struct sadb_sa *)mhp[SADB_EXT_SA]; 588 if (sa != NULL) 589 spi[(*num_spi)++] = (u_int32_t)ntohl(sa->sadb_sa_spi); 590 591 m = (struct sadb_msg *)((caddr_t)m + PFKEY_UNUNIT64(m->sadb_msg_len)); 592 593 if (f_verbose) { 594 kdebug_sadb(m); 595 printf("\n"); 596 } 597 598 } while (m->sadb_msg_seq); 599 600 if (fail) { 601 free(spi); 602 *num_spi = 0; 603 return NULL; 604 } 605 606 return spi; 607 } 608 609 int 610 sendkeymsg(buf, len) 611 char *buf; 612 size_t len; 613 { 614 u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */ 615 ssize_t l; 616 struct sadb_msg *msg; 617 618 if (f_notreally) { 619 goto end; 620 } 621 622 { 623 struct timeval tv; 624 tv.tv_sec = 1; 625 tv.tv_usec = 0; 626 if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { 627 perror("setsockopt"); 628 goto end; 629 } 630 } 631 632 if (f_forever) 633 shortdump_hdr(); 634 again: 635 if (f_verbose) { 636 kdebug_sadb((struct sadb_msg *)buf); 637 printf("\n"); 638 } 639 if (f_hexdump) { 640 int i; 641 for (i = 0; i < len; i++) { 642 if (i % 16 == 0) 643 printf("%08x: ", i); 644 printf("%02x ", buf[i] & 0xff); 645 if (i % 16 == 15) 646 printf("\n"); 647 } 648 if (len % 16) 649 printf("\n"); 650 } 651 652 if ((l = send(so, buf, len, 0)) < 0) { 653 perror("send"); 654 goto end; 655 } 656 657 msg = (struct sadb_msg *)rbuf; 658 do { 659 if ((l = recv(so, rbuf, sizeof(rbuf), 0)) < 0) { 660 perror("recv"); 661 goto end; 662 } 663 664 if (PFKEY_UNUNIT64(msg->sadb_msg_len) != l) { 665 warnx("invalid keymsg length"); 666 break; 667 } 668 669 if (f_verbose) { 670 kdebug_sadb(msg); 671 printf("\n"); 672 } 673 if (postproc(msg, l) < 0) 674 break; 675 } while (msg->sadb_msg_errno || msg->sadb_msg_seq); 676 677 if (f_forever) { 678 fflush(stdout); 679 sleep(1); 680 goto again; 681 } 682 683 end: 684 return (0); 685 } 686 687 int 688 postproc(msg, len) 689 struct sadb_msg *msg; 690 int len; 691 { 692 #ifdef HAVE_PFKEY_POLICY_PRIORITY 693 static int priority_support_check = 0; 694 #endif 695 696 if (msg->sadb_msg_errno != 0) { 697 char inf[80]; 698 const char *errmsg = NULL; 699 700 if (f_mode == MODE_SCRIPT) 701 snprintf(inf, sizeof(inf), "The result of line %d: ", lineno); 702 else 703 inf[0] = '\0'; 704 705 switch (msg->sadb_msg_errno) { 706 case ENOENT: 707 switch (msg->sadb_msg_type) { 708 case SADB_DELETE: 709 case SADB_GET: 710 case SADB_X_SPDDELETE: 711 errmsg = "No entry"; 712 break; 713 case SADB_DUMP: 714 errmsg = "No SAD entries"; 715 break; 716 case SADB_X_SPDDUMP: 717 errmsg = "No SPD entries"; 718 break; 719 } 720 break; 721 default: 722 errmsg = strerror(msg->sadb_msg_errno); 723 } 724 printf("%s%s.\n", inf, errmsg); 725 return (-1); 726 } 727 728 switch (msg->sadb_msg_type) { 729 case SADB_GET: 730 if (f_withports) 731 pfkey_sadump_withports(msg); 732 else 733 pfkey_sadump(msg); 734 break; 735 736 case SADB_DUMP: 737 /* filter out DEAD SAs */ 738 if (!f_all) { 739 caddr_t mhp[SADB_EXT_MAX + 1]; 740 struct sadb_sa *sa; 741 pfkey_align(msg, mhp); 742 pfkey_check(mhp); 743 if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) { 744 if (sa->sadb_sa_state == SADB_SASTATE_DEAD) 745 break; 746 } 747 } 748 if (f_forever) { 749 /* TODO: f_withports */ 750 shortdump(msg); 751 } else { 752 if (f_withports) 753 pfkey_sadump_withports(msg); 754 else 755 pfkey_sadump(msg); 756 } 757 msg = (struct sadb_msg *)((caddr_t)msg + 758 PFKEY_UNUNIT64(msg->sadb_msg_len)); 759 if (f_verbose) { 760 kdebug_sadb((struct sadb_msg *)msg); 761 printf("\n"); 762 } 763 break; 764 765 case SADB_X_SPDGET: 766 if (f_withports) 767 pfkey_spdump_withports(msg); 768 else 769 pfkey_spdump(msg); 770 break; 771 772 case SADB_X_SPDDUMP: 773 if (f_withports) 774 pfkey_spdump_withports(msg); 775 else 776 pfkey_spdump(msg); 777 if (msg->sadb_msg_seq == 0) break; 778 msg = (struct sadb_msg *)((caddr_t)msg + 779 PFKEY_UNUNIT64(msg->sadb_msg_len)); 780 if (f_verbose) { 781 kdebug_sadb((struct sadb_msg *)msg); 782 printf("\n"); 783 } 784 break; 785 #ifdef HAVE_PFKEY_POLICY_PRIORITY 786 case SADB_X_SPDADD: 787 if (last_msg_type == SADB_X_SPDADD && last_priority != 0 && 788 msg->sadb_msg_pid == getpid() && !priority_support_check) { 789 priority_support_check = 1; 790 if (!verifypriority(msg)) 791 printf ("WARNING: Kernel does not support policy priorities\n"); 792 } 793 break; 794 #endif 795 } 796 797 return (0); 798 } 799 800 #ifdef HAVE_PFKEY_POLICY_PRIORITY 801 int 802 verifypriority(m) 803 struct sadb_msg *m; 804 { 805 caddr_t mhp[SADB_EXT_MAX + 1]; 806 struct sadb_x_policy *xpl; 807 808 /* check pfkey message. */ 809 if (pfkey_align(m, mhp)) { 810 printf("(%s\n", ipsec_strerror()); 811 return 0; 812 } 813 if (pfkey_check(mhp)) { 814 printf("%s\n", ipsec_strerror()); 815 return 0; 816 } 817 818 xpl = (struct sadb_x_policy *) mhp[SADB_X_EXT_POLICY]; 819 820 if (xpl == NULL) { 821 printf("no X_POLICY extension.\n"); 822 return 0; 823 } 824 825 /* now make sure they match */ 826 if (last_priority != xpl->sadb_x_policy_priority) 827 return 0; 828 829 return 1; 830 } 831 #endif 832 833 int 834 fileproc(filename) 835 const char *filename; 836 { 837 int fd; 838 ssize_t len, l; 839 u_char *p, *ep; 840 struct sadb_msg *msg; 841 u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */ 842 843 fd = open(filename, O_RDONLY); 844 if (fd < 0) 845 return -1; 846 847 l = 0; 848 while (1) { 849 len = read(fd, rbuf + l, sizeof(rbuf) - l); 850 if (len < 0) { 851 close(fd); 852 return -1; 853 } else if (len == 0) 854 break; 855 l += len; 856 } 857 858 if (l < sizeof(struct sadb_msg)) { 859 close(fd); 860 errno = EINVAL; 861 return -1; 862 } 863 close(fd); 864 865 p = rbuf; 866 ep = rbuf + l; 867 868 while (p < ep) { 869 msg = (struct sadb_msg *)p; 870 len = PFKEY_UNUNIT64(msg->sadb_msg_len); 871 postproc(msg, len); 872 p += len; 873 } 874 875 return (0); 876 } 877 878 879 /*------------------------------------------------------------*/ 880 static const char *satype[] = { 881 NULL, NULL, "ah", "esp" 882 }; 883 static const char *sastate[] = { 884 "L", "M", "D", "d" 885 }; 886 static const char *ipproto[] = { 887 /*0*/ "ip", "icmp", "igmp", "ggp", "ip4", 888 NULL, "tcp", NULL, "egp", NULL, 889 /*10*/ NULL, NULL, NULL, NULL, NULL, 890 NULL, NULL, "udp", NULL, NULL, 891 /*20*/ NULL, NULL, "idp", NULL, NULL, 892 NULL, NULL, NULL, NULL, "tp", 893 /*30*/ NULL, NULL, NULL, NULL, NULL, 894 NULL, NULL, NULL, NULL, NULL, 895 /*40*/ NULL, "ip6", NULL, "rt6", "frag6", 896 NULL, "rsvp", "gre", NULL, NULL, 897 /*50*/ "esp", "ah", NULL, NULL, NULL, 898 NULL, NULL, NULL, "icmp6", "none", 899 /*60*/ "dst6", 900 }; 901 902 #define STR_OR_ID(x, tab) \ 903 (((x) < sizeof(tab)/sizeof(tab[0]) && tab[(x)]) ? tab[(x)] : numstr(x)) 904 905 const char * 906 numstr(x) 907 int x; 908 { 909 static char buf[20]; 910 snprintf(buf, sizeof(buf), "#%d", x); 911 return buf; 912 } 913 914 void 915 shortdump_hdr() 916 { 917 printf("%-4s %-3s %-1s %-8s %-7s %s -> %s\n", 918 "time", "p", "s", "spi", "ltime", "src", "dst"); 919 } 920 921 void 922 shortdump(msg) 923 struct sadb_msg *msg; 924 { 925 caddr_t mhp[SADB_EXT_MAX + 1]; 926 char buf[NI_MAXHOST], pbuf[NI_MAXSERV]; 927 struct sadb_sa *sa; 928 struct sadb_address *saddr; 929 struct sadb_lifetime *lts, *lth, *ltc; 930 struct sockaddr *s; 931 u_int t; 932 time_t cur = time(0); 933 934 pfkey_align(msg, mhp); 935 pfkey_check(mhp); 936 937 printf("%02lu%02lu", (u_long)(cur % 3600) / 60, (u_long)(cur % 60)); 938 939 printf(" %-3s", STR_OR_ID(msg->sadb_msg_satype, satype)); 940 941 if ((sa = (struct sadb_sa *)mhp[SADB_EXT_SA]) != NULL) { 942 printf(" %-1s", STR_OR_ID(sa->sadb_sa_state, sastate)); 943 printf(" %08x", (u_int32_t)ntohl(sa->sadb_sa_spi)); 944 } else 945 printf("%-1s %-8s", "?", "?"); 946 947 lts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT]; 948 lth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 949 ltc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT]; 950 if (lts && lth && ltc) { 951 if (ltc->sadb_lifetime_addtime == 0) 952 t = (u_long)0; 953 else 954 t = (u_long)(cur - ltc->sadb_lifetime_addtime); 955 if (t >= 1000) 956 strlcpy(buf, " big/", sizeof(buf)); 957 else 958 snprintf(buf, sizeof(buf), " %3lu/", (u_long)t); 959 printf("%s", buf); 960 961 t = (u_long)lth->sadb_lifetime_addtime; 962 if (t >= 1000) 963 strlcpy(buf, "big", sizeof(buf)); 964 else 965 snprintf(buf, sizeof(buf), "%-3lu", (u_long)t); 966 printf("%s", buf); 967 } else 968 printf(" ??\?/???"); /* backslash to avoid trigraph ??/ */ 969 970 printf(" "); 971 972 if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]) != NULL) { 973 if (saddr->sadb_address_proto) 974 printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto)); 975 s = (struct sockaddr *)(saddr + 1); 976 getnameinfo(s, sysdep_sa_len(s), buf, sizeof(buf), 977 pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV); 978 if (strcmp(pbuf, "0") != 0) 979 printf("%s[%s]", buf, pbuf); 980 else 981 printf("%s", buf); 982 } else 983 printf("?"); 984 985 printf(" -> "); 986 987 if ((saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]) != NULL) { 988 if (saddr->sadb_address_proto) 989 printf("%s ", STR_OR_ID(saddr->sadb_address_proto, ipproto)); 990 991 s = (struct sockaddr *)(saddr + 1); 992 getnameinfo(s, sysdep_sa_len(s), buf, sizeof(buf), 993 pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV); 994 if (strcmp(pbuf, "0") != 0) 995 printf("%s[%s]", buf, pbuf); 996 else 997 printf("%s", buf); 998 } else 999 printf("?"); 1000 1001 printf("\n"); 1002 } 1003 1004 /* From: tcpdump(1):gmt2local.c and util.c */ 1005 /* 1006 * Print the timestamp 1007 */ 1008 static void 1009 printdate() 1010 { 1011 struct timeval tp; 1012 int s; 1013 1014 if (gettimeofday(&tp, NULL) == -1) { 1015 perror("gettimeofday"); 1016 return; 1017 } 1018 1019 if (f_tflag == 1) { 1020 /* Default */ 1021 s = (tp.tv_sec + thiszone ) % 86400; 1022 (void)printf("%02d:%02d:%02d.%06u ", 1023 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tp.tv_usec); 1024 } else if (f_tflag > 1) { 1025 /* Unix timeval style */ 1026 (void)printf("%u.%06u ", 1027 (u_int32_t)tp.tv_sec, (u_int32_t)tp.tv_usec); 1028 } 1029 1030 printf("\n"); 1031 } 1032 1033 /* 1034 * Returns the difference between gmt and local time in seconds. 1035 * Use gmtime() and localtime() to keep things simple. 1036 */ 1037 int32_t 1038 gmt2local(time_t t) 1039 { 1040 register int dt, dir; 1041 register struct tm *gmt, *loc; 1042 struct tm sgmt; 1043 1044 if (t == 0) 1045 t = time(NULL); 1046 gmt = &sgmt; 1047 *gmt = *gmtime(&t); 1048 loc = localtime(&t); 1049 dt = (loc->tm_hour - gmt->tm_hour) * 60 * 60 + 1050 (loc->tm_min - gmt->tm_min) * 60; 1051 1052 /* 1053 * If the year or julian day is different, we span 00:00 GMT 1054 * and must add or subtract a day. Check the year first to 1055 * avoid problems when the julian day wraps. 1056 */ 1057 dir = loc->tm_year - gmt->tm_year; 1058 if (dir == 0) 1059 dir = loc->tm_yday - gmt->tm_yday; 1060 dt += dir * 24 * 60 * 60; 1061 1062 return (dt); 1063 } 1064