1 /* $KAME: pfkey_dump.c,v 1.45 2003/09/08 10:14:56 itojun Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/types.h> 36 #include <sys/param.h> 37 #include <sys/socket.h> 38 #include <netipsec/ipsec.h> 39 #include <net/pfkeyv2.h> 40 #include <netipsec/key_var.h> 41 #include <netipsec/key_debug.h> 42 43 #include <netinet/in.h> 44 #include <arpa/inet.h> 45 46 #include <stdlib.h> 47 #include <unistd.h> 48 #include <stdio.h> 49 #include <string.h> 50 #include <time.h> 51 #include <netdb.h> 52 53 #include "ipsec_strerror.h" 54 #include "libpfkey.h" 55 56 /* cope with old kame headers - ugly */ 57 #ifndef SADB_X_AALG_MD5 58 #define SADB_X_AALG_MD5 SADB_AALG_MD5 59 #endif 60 #ifndef SADB_X_AALG_SHA 61 #define SADB_X_AALG_SHA SADB_AALG_SHA 62 #endif 63 #ifndef SADB_X_AALG_NULL 64 #define SADB_X_AALG_NULL SADB_AALG_NULL 65 #endif 66 67 #ifndef SADB_X_EALG_BLOWFISHCBC 68 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC 69 #endif 70 #ifndef SADB_X_EALG_CAST128CBC 71 #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC 72 #endif 73 #ifndef SADB_X_EALG_RC5CBC 74 #ifdef SADB_EALG_RC5CBC 75 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC 76 #endif 77 #endif 78 79 #define GETMSGSTR(str, num) \ 80 do { \ 81 if (sizeof((str)[0]) == 0 \ 82 || num >= sizeof(str)/sizeof((str)[0])) \ 83 printf("%u ", (num)); \ 84 else if (strlen((str)[(num)]) == 0) \ 85 printf("%u ", (num)); \ 86 else \ 87 printf("%s ", (str)[(num)]); \ 88 } while (0) 89 90 #define GETMSGV2S(v2s, num) \ 91 do { \ 92 struct val2str *p; \ 93 for (p = (v2s); p && p->str; p++) { \ 94 if (p->val == (num)) \ 95 break; \ 96 } \ 97 if (p && p->str) \ 98 printf("%s ", p->str); \ 99 else \ 100 printf("%u ", (num)); \ 101 } while (0) 102 103 static char *str_ipaddr(struct sockaddr *); 104 static char *str_prefport(u_int, u_int, u_int, u_int); 105 static void str_upperspec(u_int, u_int, u_int); 106 static char *str_time(time_t); 107 static void str_lifetime_byte(struct sadb_lifetime *, char *); 108 109 struct val2str { 110 int val; 111 const char *str; 112 }; 113 114 /* 115 * Must to be re-written about following strings. 116 */ 117 static char *str_satype[] = { 118 "unspec", 119 "unknown", 120 "ah", 121 "esp", 122 "unknown", 123 "rsvp", 124 "ospfv2", 125 "ripv2", 126 "mip", 127 "ipcomp", 128 "policy", 129 "tcp" 130 }; 131 132 static char *str_mode[] = { 133 "any", 134 "transport", 135 "tunnel", 136 }; 137 138 static char *str_state[] = { 139 "larval", 140 "mature", 141 "dying", 142 "dead", 143 }; 144 145 static struct val2str str_alg_auth[] = { 146 { SADB_AALG_NONE, "none", }, 147 { SADB_AALG_MD5HMAC, "hmac-md5", }, 148 { SADB_AALG_SHA1HMAC, "hmac-sha1", }, 149 { SADB_X_AALG_MD5, "md5", }, 150 { SADB_X_AALG_SHA, "sha", }, 151 { SADB_X_AALG_NULL, "null", }, 152 { SADB_X_AALG_TCP_MD5, "tcp-md5", }, 153 #ifdef SADB_X_AALG_SHA2_256 154 { SADB_X_AALG_SHA2_256, "hmac-sha2-256", }, 155 #endif 156 #ifdef SADB_X_AALG_SHA2_384 157 { SADB_X_AALG_SHA2_384, "hmac-sha2-384", }, 158 #endif 159 #ifdef SADB_X_AALG_SHA2_512 160 { SADB_X_AALG_SHA2_512, "hmac-sha2-512", }, 161 #endif 162 #ifdef SADB_X_AALG_RIPEMD160HMAC 163 { SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", }, 164 #endif 165 #ifdef SADB_X_AALG_AES_XCBC_MAC 166 { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", }, 167 #endif 168 { -1, NULL, }, 169 }; 170 171 static struct val2str str_alg_enc[] = { 172 { SADB_EALG_NONE, "none", }, 173 { SADB_EALG_DESCBC, "des-cbc", }, 174 { SADB_EALG_3DESCBC, "3des-cbc", }, 175 { SADB_EALG_NULL, "null", }, 176 #ifdef SADB_X_EALG_RC5CBC 177 { SADB_X_EALG_RC5CBC, "rc5-cbc", }, 178 #endif 179 { SADB_X_EALG_CAST128CBC, "cast128-cbc", }, 180 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", }, 181 #ifdef SADB_X_EALG_RIJNDAELCBC 182 { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", }, 183 #endif 184 #ifdef SADB_X_EALG_TWOFISHCBC 185 { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", }, 186 #endif 187 #ifdef SADB_X_EALG_AESCTR 188 { SADB_X_EALG_AESCTR, "aes-ctr", }, 189 #endif 190 #ifdef SADB_X_EALG_AESGCM16 191 { SADB_X_EALG_AESGCM16, "aes-gcm-16", }, 192 #endif 193 #ifdef SADB_X_EALG_CAMELLIACBC 194 { SADB_X_EALG_CAMELLIACBC, "camellia-cbc", }, 195 #endif 196 { -1, NULL, }, 197 }; 198 199 static struct val2str str_alg_comp[] = { 200 { SADB_X_CALG_NONE, "none", }, 201 { SADB_X_CALG_OUI, "oui", }, 202 { SADB_X_CALG_DEFLATE, "deflate", }, 203 { SADB_X_CALG_LZS, "lzs", }, 204 { -1, NULL, }, 205 }; 206 207 /* 208 * dump SADB_MSG formated. For debugging, you should use kdebug_sadb(). 209 */ 210 void 211 pfkey_sadump(m) 212 struct sadb_msg *m; 213 { 214 caddr_t mhp[SADB_EXT_MAX + 1]; 215 struct sadb_sa *m_sa; 216 struct sadb_x_sa2 *m_sa2; 217 struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts; 218 struct sadb_address *m_saddr, *m_daddr, *m_paddr; 219 struct sadb_key *m_auth, *m_enc; 220 struct sadb_ident *m_sid, *m_did; 221 struct sadb_sens *m_sens; 222 struct sadb_x_sa_replay *m_sa_replay; 223 224 /* check pfkey message. */ 225 if (pfkey_align(m, mhp)) { 226 printf("%s\n", ipsec_strerror()); 227 return; 228 } 229 if (pfkey_check(mhp)) { 230 printf("%s\n", ipsec_strerror()); 231 return; 232 } 233 234 m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA]; 235 m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2]; 236 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT]; 237 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 238 m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT]; 239 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; 240 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; 241 m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY]; 242 m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH]; 243 m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT]; 244 m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC]; 245 m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST]; 246 m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY]; 247 m_sa_replay = (struct sadb_x_sa_replay *)mhp[SADB_X_EXT_SA_REPLAY]; 248 249 /* source address */ 250 if (m_saddr == NULL) { 251 printf("no ADDRESS_SRC extension.\n"); 252 return; 253 } 254 printf("%s ", str_ipaddr((struct sockaddr *)(m_saddr + 1))); 255 256 /* destination address */ 257 if (m_daddr == NULL) { 258 printf("no ADDRESS_DST extension.\n"); 259 return; 260 } 261 printf("%s ", str_ipaddr((struct sockaddr *)(m_daddr + 1))); 262 263 /* SA type */ 264 if (m_sa == NULL) { 265 printf("no SA extension.\n"); 266 return; 267 } 268 if (m_sa2 == NULL) { 269 printf("no SA2 extension.\n"); 270 return; 271 } 272 printf("\n\t"); 273 274 GETMSGSTR(str_satype, m->sadb_msg_satype); 275 276 printf("mode="); 277 GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode); 278 279 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n", 280 (u_int32_t)ntohl(m_sa->sadb_sa_spi), 281 (u_int32_t)ntohl(m_sa->sadb_sa_spi), 282 (u_int32_t)m_sa2->sadb_x_sa2_reqid, 283 (u_int32_t)m_sa2->sadb_x_sa2_reqid); 284 285 /* encryption key */ 286 if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { 287 printf("\tC: "); 288 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt); 289 } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) { 290 if (m_enc != NULL) { 291 printf("\tE: "); 292 GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt); 293 ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc), 294 m_enc->sadb_key_bits / 8); 295 printf("\n"); 296 } 297 } 298 299 /* authentication key */ 300 if (m_auth != NULL) { 301 printf("\tA: "); 302 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth); 303 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth), 304 m_auth->sadb_key_bits / 8); 305 printf("\n"); 306 } 307 308 /* replay windoe size & flags */ 309 printf("\tseq=0x%08x replay=%u flags=0x%08x ", 310 m_sa2->sadb_x_sa2_sequence, 311 m_sa_replay ? (m_sa_replay->sadb_x_sa_replay_replay >> 3) : 312 m_sa->sadb_sa_replay, 313 m_sa->sadb_sa_flags); 314 315 /* state */ 316 printf("state="); 317 GETMSGSTR(str_state, m_sa->sadb_sa_state); 318 printf("\n"); 319 320 /* lifetime */ 321 if (m_lftc != NULL) { 322 time_t tmp_time = time(0); 323 324 printf("\tcreated: %s", 325 str_time(m_lftc->sadb_lifetime_addtime)); 326 printf("\tcurrent: %s\n", str_time(tmp_time)); 327 printf("\tdiff: %lu(s)", 328 (u_long)(m_lftc->sadb_lifetime_addtime == 0 ? 329 0 : (tmp_time - m_lftc->sadb_lifetime_addtime))); 330 331 printf("\thard: %lu(s)", 332 (u_long)(m_lfth == NULL ? 333 0 : m_lfth->sadb_lifetime_addtime)); 334 printf("\tsoft: %lu(s)\n", 335 (u_long)(m_lfts == NULL ? 336 0 : m_lfts->sadb_lifetime_addtime)); 337 338 printf("\tlast: %s", 339 str_time(m_lftc->sadb_lifetime_usetime)); 340 printf("\thard: %lu(s)", 341 (u_long)(m_lfth == NULL ? 342 0 : m_lfth->sadb_lifetime_usetime)); 343 printf("\tsoft: %lu(s)\n", 344 (u_long)(m_lfts == NULL ? 345 0 : m_lfts->sadb_lifetime_usetime)); 346 347 str_lifetime_byte(m_lftc, "current"); 348 str_lifetime_byte(m_lfth, "hard"); 349 str_lifetime_byte(m_lfts, "soft"); 350 printf("\n"); 351 352 printf("\tallocated: %lu", 353 (unsigned long)m_lftc->sadb_lifetime_allocations); 354 printf("\thard: %lu", 355 (u_long)(m_lfth == NULL ? 356 0 : m_lfth->sadb_lifetime_allocations)); 357 printf("\tsoft: %lu\n", 358 (u_long)(m_lfts == NULL ? 359 0 : m_lfts->sadb_lifetime_allocations)); 360 } 361 362 printf("\tsadb_seq=%lu pid=%lu ", 363 (u_long)m->sadb_msg_seq, 364 (u_long)m->sadb_msg_pid); 365 366 /* XXX DEBUG */ 367 printf("refcnt=%u\n", m->sadb_msg_reserved); 368 369 return; 370 } 371 372 void 373 pfkey_spdump(m) 374 struct sadb_msg *m; 375 { 376 char pbuf[NI_MAXSERV]; 377 caddr_t mhp[SADB_EXT_MAX + 1]; 378 struct sadb_address *m_saddr, *m_daddr; 379 struct sadb_x_policy *m_xpl; 380 struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL; 381 struct sockaddr *sa; 382 u_int16_t sport = 0, dport = 0; 383 384 /* check pfkey message. */ 385 if (pfkey_align(m, mhp)) { 386 printf("%s\n", ipsec_strerror()); 387 return; 388 } 389 if (pfkey_check(mhp)) { 390 printf("%s\n", ipsec_strerror()); 391 return; 392 } 393 394 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; 395 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; 396 m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY]; 397 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT]; 398 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 399 400 if (m_saddr && m_daddr) { 401 /* source address */ 402 sa = (struct sockaddr *)(m_saddr + 1); 403 switch (sa->sa_family) { 404 case AF_INET: 405 case AF_INET6: 406 if (getnameinfo(sa, sa->sa_len, NULL, 0, 407 pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0) 408 sport = 0; /*XXX*/ 409 else 410 sport = atoi(pbuf); 411 printf("%s%s ", str_ipaddr(sa), 412 str_prefport(sa->sa_family, 413 m_saddr->sadb_address_prefixlen, sport, 414 m_saddr->sadb_address_proto)); 415 break; 416 default: 417 printf("unknown-af "); 418 break; 419 } 420 421 /* destination address */ 422 sa = (struct sockaddr *)(m_daddr + 1); 423 switch (sa->sa_family) { 424 case AF_INET: 425 case AF_INET6: 426 if (getnameinfo(sa, sa->sa_len, NULL, 0, 427 pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0) 428 dport = 0; /*XXX*/ 429 else 430 dport = atoi(pbuf); 431 printf("%s%s ", str_ipaddr(sa), 432 str_prefport(sa->sa_family, 433 m_daddr->sadb_address_prefixlen, dport, 434 m_saddr->sadb_address_proto)); 435 break; 436 default: 437 printf("unknown-af "); 438 break; 439 } 440 441 /* upper layer protocol */ 442 if (m_saddr->sadb_address_proto != 443 m_daddr->sadb_address_proto) { 444 printf("upper layer protocol mismatched.\n"); 445 return; 446 } 447 str_upperspec(m_saddr->sadb_address_proto, sport, dport); 448 } 449 else 450 printf("(no selector, probably per-socket policy) "); 451 452 /* policy */ 453 { 454 char *d_xpl; 455 456 if (m_xpl == NULL) { 457 printf("no X_POLICY extension.\n"); 458 return; 459 } 460 d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t"); 461 462 /* dump SPD */ 463 printf("\n\t%s\n", d_xpl); 464 free(d_xpl); 465 } 466 467 /* lifetime */ 468 if (m_lftc) { 469 printf("\tcreated: %s ", 470 str_time(m_lftc->sadb_lifetime_addtime)); 471 printf("lastused: %s\n", 472 str_time(m_lftc->sadb_lifetime_usetime)); 473 } 474 if (m_lfth) { 475 printf("\tlifetime: %lu(s) ", 476 (u_long)m_lfth->sadb_lifetime_addtime); 477 printf("validtime: %lu(s)\n", 478 (u_long)m_lfth->sadb_lifetime_usetime); 479 } 480 481 482 printf("\tspid=%ld seq=%ld pid=%ld\n", 483 (u_long)m_xpl->sadb_x_policy_id, 484 (u_long)m->sadb_msg_seq, 485 (u_long)m->sadb_msg_pid); 486 487 /* XXX TEST */ 488 printf("\trefcnt=%u\n", m->sadb_msg_reserved); 489 490 return; 491 } 492 493 /* 494 * set "ipaddress" to buffer. 495 */ 496 static char * 497 str_ipaddr(sa) 498 struct sockaddr *sa; 499 { 500 static char buf[NI_MAXHOST]; 501 const int niflag = NI_NUMERICHOST; 502 503 if (sa == NULL) 504 return ""; 505 506 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0) 507 return buf; 508 return NULL; 509 } 510 511 /* 512 * set "/prefix[port number]" to buffer. 513 */ 514 static char * 515 str_prefport(family, pref, port, ulp) 516 u_int family, pref, port, ulp; 517 { 518 static char buf[128]; 519 char prefbuf[128]; 520 char portbuf[128]; 521 int plen; 522 523 switch (family) { 524 case AF_INET: 525 plen = sizeof(struct in_addr) << 3; 526 break; 527 case AF_INET6: 528 plen = sizeof(struct in6_addr) << 3; 529 break; 530 default: 531 return "?"; 532 } 533 534 if (pref == plen) 535 prefbuf[0] = '\0'; 536 else 537 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref); 538 539 if (ulp == IPPROTO_ICMPV6) 540 memset(portbuf, 0, sizeof(portbuf)); 541 else { 542 if (port == IPSEC_PORT_ANY) 543 snprintf(portbuf, sizeof(portbuf), "[%s]", "any"); 544 else 545 snprintf(portbuf, sizeof(portbuf), "[%u]", port); 546 } 547 548 snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf); 549 550 return buf; 551 } 552 553 static void 554 str_upperspec(ulp, p1, p2) 555 u_int ulp, p1, p2; 556 { 557 if (ulp == IPSEC_ULPROTO_ANY) 558 printf("any"); 559 else if (ulp == IPPROTO_ICMPV6) { 560 printf("icmp6"); 561 if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY)) 562 printf(" %u,%u", p1, p2); 563 } else { 564 struct protoent *ent; 565 566 switch (ulp) { 567 case IPPROTO_IPV4: 568 printf("ip4"); 569 break; 570 default: 571 ent = getprotobynumber(ulp); 572 if (ent) 573 printf("%s", ent->p_name); 574 else 575 printf("%u", ulp); 576 577 endprotoent(); 578 break; 579 } 580 } 581 } 582 583 /* 584 * set "Mon Day Time Year" to buffer 585 */ 586 static char * 587 str_time(t) 588 time_t t; 589 { 590 static char buf[128]; 591 592 if (t == 0) { 593 int i = 0; 594 for (;i < 20;) buf[i++] = ' '; 595 } else { 596 char *t0; 597 t0 = ctime(&t); 598 memcpy(buf, t0 + 4, 20); 599 } 600 601 buf[20] = '\0'; 602 603 return(buf); 604 } 605 606 static void 607 str_lifetime_byte(x, str) 608 struct sadb_lifetime *x; 609 char *str; 610 { 611 double y; 612 char *unit; 613 int w; 614 615 if (x == NULL) { 616 printf("\t%s: 0(bytes)", str); 617 return; 618 } 619 620 #if 0 621 if ((x->sadb_lifetime_bytes) / 1024 / 1024) { 622 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024; 623 unit = "M"; 624 w = 1; 625 } else if ((x->sadb_lifetime_bytes) / 1024) { 626 y = (x->sadb_lifetime_bytes) * 1.0 / 1024; 627 unit = "K"; 628 w = 1; 629 } else { 630 y = (x->sadb_lifetime_bytes) * 1.0; 631 unit = ""; 632 w = 0; 633 } 634 #else 635 y = (x->sadb_lifetime_bytes) * 1.0; 636 unit = ""; 637 w = 0; 638 #endif 639 printf("\t%s: %.*f(%sbytes)", str, w, y, unit); 640 } 641