1 /* $KAME: pfkey_dump.c,v 1.28 2001/06/27 10:46:51 sakane 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 <netinet6/ipsec.h> 39 #include <net/pfkeyv2.h> 40 #include <netkey/key_var.h> 41 #include <netkey/key_debug.h> 42 43 #include <netinet/in.h> 44 #include <netinet6/ipsec.h> 45 #include <arpa/inet.h> 46 47 #include <stdlib.h> 48 #include <unistd.h> 49 #include <stdio.h> 50 #include <string.h> 51 #include <time.h> 52 #include <netdb.h> 53 54 #include "ipsec_strerror.h" 55 #include "libpfkey.h" 56 57 /* cope with old kame headers - ugly */ 58 #ifndef SADB_X_AALG_MD5 59 #define SADB_X_AALG_MD5 SADB_AALG_MD5 60 #endif 61 #ifndef SADB_X_AALG_SHA 62 #define SADB_X_AALG_SHA SADB_AALG_SHA 63 #endif 64 #ifndef SADB_X_AALG_NULL 65 #define SADB_X_AALG_NULL SADB_AALG_NULL 66 #endif 67 68 #ifndef SADB_X_EALG_BLOWFISHCBC 69 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC 70 #endif 71 #ifndef SADB_X_EALG_CAST128CBC 72 #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC 73 #endif 74 #ifndef SADB_X_EALG_RC5CBC 75 #ifdef SADB_EALG_RC5CBC 76 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC 77 #endif 78 #endif 79 80 #define GETMSGSTR(str, num) \ 81 do { \ 82 if (sizeof((str)[0]) == 0 \ 83 || num >= sizeof(str)/sizeof((str)[0])) \ 84 printf("%d ", (num)); \ 85 else if (strlen((str)[(num)]) == 0) \ 86 printf("%d ", (num)); \ 87 else \ 88 printf("%s ", (str)[(num)]); \ 89 } while (0) 90 91 #define GETMSGV2S(v2s, num) \ 92 do { \ 93 struct val2str *p; \ 94 for (p = (v2s); p && p->str; p++) { \ 95 if (p->val == (num)) \ 96 break; \ 97 } \ 98 if (p && p->str) \ 99 printf("%s ", p->str); \ 100 else \ 101 printf("%d ", (num)); \ 102 } while (0) 103 104 static char *str_ipaddr(struct sockaddr *); 105 static char *str_prefport(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 }; 129 130 static char *str_mode[] = { 131 "any", 132 "transport", 133 "tunnel", 134 }; 135 136 static char *str_upper[] = { 137 /*0*/ "ip", "icmp", "igmp", "ggp", "ip4", 138 "", "tcp", "", "egp", "", 139 /*10*/ "", "", "", "", "", 140 "", "", "udp", "", "", 141 /*20*/ "", "", "idp", "", "", 142 "", "", "", "", "tp", 143 /*30*/ "", "", "", "", "", 144 "", "", "", "", "", 145 /*40*/ "", "ip6", "", "rt6", "frag6", 146 "", "rsvp", "gre", "", "", 147 /*50*/ "esp", "ah", "", "", "", 148 "", "", "", "icmp6", "none", 149 /*60*/ "dst6", 150 }; 151 152 static char *str_state[] = { 153 "larval", 154 "mature", 155 "dying", 156 "dead", 157 }; 158 159 static struct val2str str_alg_auth[] = { 160 { SADB_AALG_NONE, "none", }, 161 { SADB_AALG_MD5HMAC, "hmac-md5", }, 162 { SADB_AALG_SHA1HMAC, "hmac-sha1", }, 163 { SADB_X_AALG_MD5, "md5", }, 164 { SADB_X_AALG_SHA, "sha", }, 165 { SADB_X_AALG_NULL, "null", }, 166 #ifdef SADB_X_AALG_SHA2_256 167 { SADB_X_AALG_SHA2_256, "hmac-sha2-256", }, 168 #endif 169 #ifdef SADB_X_AALG_SHA2_384 170 { SADB_X_AALG_SHA2_384, "hmac-sha2-384", }, 171 #endif 172 #ifdef SADB_X_AALG_SHA2_512 173 { SADB_X_AALG_SHA2_512, "hmac-sha2-512", }, 174 #endif 175 #ifdef SADB_X_AALG_RIPEMD160HMAC 176 { SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", }, 177 #endif 178 #ifdef SADB_X_AALG_AES_XCBC_MAC 179 { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", }, 180 #endif 181 { -1, NULL, }, 182 }; 183 184 static struct val2str str_alg_enc[] = { 185 { SADB_EALG_NONE, "none", }, 186 { SADB_EALG_DESCBC, "des-cbc", }, 187 { SADB_EALG_3DESCBC, "3des-cbc", }, 188 { SADB_EALG_NULL, "null", }, 189 #ifdef SADB_X_EALG_RC5CBC 190 { SADB_X_EALG_RC5CBC, "rc5-cbc", }, 191 #endif 192 { SADB_X_EALG_CAST128CBC, "cast128-cbc", }, 193 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", }, 194 #ifdef SADB_X_EALG_RIJNDAELCBC 195 { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", }, 196 #endif 197 #ifdef SADB_X_EALG_TWOFISHCBC 198 { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", }, 199 #endif 200 #ifdef SADB_X_EALG_AESCTR 201 { SADB_X_EALG_AESCTR, "aes-ctr", }, 202 #endif 203 { -1, NULL, }, 204 }; 205 206 static struct val2str str_alg_comp[] = { 207 { SADB_X_CALG_NONE, "none", }, 208 { SADB_X_CALG_OUI, "oui", }, 209 { SADB_X_CALG_DEFLATE, "deflate", }, 210 { SADB_X_CALG_LZS, "lzs", }, 211 { -1, NULL, }, 212 }; 213 214 /* 215 * dump SADB_MSG formated. For debugging, you should use kdebug_sadb(). 216 */ 217 void 218 pfkey_sadump(m) 219 struct sadb_msg *m; 220 { 221 caddr_t mhp[SADB_EXT_MAX + 1]; 222 struct sadb_sa *m_sa; 223 struct sadb_x_sa2 *m_sa2; 224 struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts; 225 struct sadb_address *m_saddr, *m_daddr, *m_paddr; 226 struct sadb_key *m_auth, *m_enc; 227 struct sadb_ident *m_sid, *m_did; 228 struct sadb_sens *m_sens; 229 230 /* check pfkey message. */ 231 if (pfkey_align(m, mhp)) { 232 printf("%s\n", ipsec_strerror()); 233 return; 234 } 235 if (pfkey_check(mhp)) { 236 printf("%s\n", ipsec_strerror()); 237 return; 238 } 239 240 m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA]; 241 m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2]; 242 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT]; 243 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 244 m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT]; 245 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; 246 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; 247 m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY]; 248 m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH]; 249 m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT]; 250 m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC]; 251 m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST]; 252 m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY]; 253 254 /* source address */ 255 if (m_saddr == NULL) { 256 printf("no ADDRESS_SRC extension.\n"); 257 return; 258 } 259 printf("%s ", str_ipaddr((struct sockaddr *)(m_saddr + 1))); 260 261 /* destination address */ 262 if (m_daddr == NULL) { 263 printf("no ADDRESS_DST extension.\n"); 264 return; 265 } 266 printf("%s ", str_ipaddr((struct sockaddr *)(m_daddr + 1))); 267 268 /* SA type */ 269 if (m_sa == NULL) { 270 printf("no SA extension.\n"); 271 return; 272 } 273 if (m_sa2 == NULL) { 274 printf("no SA2 extension.\n"); 275 return; 276 } 277 printf("\n\t"); 278 279 GETMSGSTR(str_satype, m->sadb_msg_satype); 280 281 printf("mode="); 282 GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode); 283 284 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n", 285 (u_int32_t)ntohl(m_sa->sadb_sa_spi), 286 (u_int32_t)ntohl(m_sa->sadb_sa_spi), 287 (u_int32_t)m_sa2->sadb_x_sa2_reqid, 288 (u_int32_t)m_sa2->sadb_x_sa2_reqid); 289 290 /* encryption key */ 291 if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { 292 printf("\tC: "); 293 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt); 294 } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) { 295 if (m_enc != NULL) { 296 printf("\tE: "); 297 GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt); 298 ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc), 299 m_enc->sadb_key_bits / 8); 300 printf("\n"); 301 } 302 } 303 304 /* authentication key */ 305 if (m_auth != NULL) { 306 printf("\tA: "); 307 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth); 308 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth), 309 m_auth->sadb_key_bits / 8); 310 printf("\n"); 311 } 312 313 /* replay windoe size & flags */ 314 printf("\tseq=0x%08x replay=%u flags=0x%08x ", 315 m_sa2->sadb_x_sa2_sequence, 316 m_sa->sadb_sa_replay, 317 m_sa->sadb_sa_flags); 318 319 /* state */ 320 printf("state="); 321 GETMSGSTR(str_state, m_sa->sadb_sa_state); 322 printf("\n"); 323 324 /* lifetime */ 325 if (m_lftc != NULL) { 326 time_t tmp_time = time(0); 327 328 printf("\tcreated: %s", 329 str_time(m_lftc->sadb_lifetime_addtime)); 330 printf("\tcurrent: %s\n", str_time(tmp_time)); 331 printf("\tdiff: %lu(s)", 332 (u_long)(m_lftc->sadb_lifetime_addtime == 0 ? 333 0 : (tmp_time - m_lftc->sadb_lifetime_addtime))); 334 335 printf("\thard: %lu(s)", 336 (u_long)(m_lfth == NULL ? 337 0 : m_lfth->sadb_lifetime_addtime)); 338 printf("\tsoft: %lu(s)\n", 339 (u_long)(m_lfts == NULL ? 340 0 : m_lfts->sadb_lifetime_addtime)); 341 342 printf("\tlast: %s", 343 str_time(m_lftc->sadb_lifetime_usetime)); 344 printf("\thard: %lu(s)", 345 (u_long)(m_lfth == NULL ? 346 0 : m_lfth->sadb_lifetime_usetime)); 347 printf("\tsoft: %lu(s)\n", 348 (u_long)(m_lfts == NULL ? 349 0 : m_lfts->sadb_lifetime_usetime)); 350 351 str_lifetime_byte(m_lftc, "current"); 352 str_lifetime_byte(m_lfth, "hard"); 353 str_lifetime_byte(m_lfts, "soft"); 354 printf("\n"); 355 356 printf("\tallocated: %lu", 357 (unsigned long)m_lftc->sadb_lifetime_allocations); 358 printf("\thard: %lu", 359 (u_long)(m_lfth == NULL ? 360 0 : m_lfth->sadb_lifetime_allocations)); 361 printf("\tsoft: %lu\n", 362 (u_long)(m_lfts == NULL ? 363 0 : m_lfts->sadb_lifetime_allocations)); 364 } 365 366 printf("\tsadb_seq=%lu pid=%lu ", 367 (u_long)m->sadb_msg_seq, 368 (u_long)m->sadb_msg_pid); 369 370 /* XXX DEBUG */ 371 printf("refcnt=%u\n", m->sadb_msg_reserved); 372 373 return; 374 } 375 376 void 377 pfkey_spdump(m) 378 struct sadb_msg *m; 379 { 380 char pbuf[NI_MAXSERV]; 381 caddr_t mhp[SADB_EXT_MAX + 1]; 382 struct sadb_address *m_saddr, *m_daddr; 383 struct sadb_x_policy *m_xpl; 384 struct sadb_lifetime *m_lft = NULL; 385 struct sockaddr *sa; 386 u_int16_t port; 387 388 /* check pfkey message. */ 389 if (pfkey_align(m, mhp)) { 390 printf("%s\n", ipsec_strerror()); 391 return; 392 } 393 if (pfkey_check(mhp)) { 394 printf("%s\n", ipsec_strerror()); 395 return; 396 } 397 398 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; 399 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; 400 m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY]; 401 m_lft = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 402 403 /* source address */ 404 if (m_saddr == NULL) { 405 printf("no ADDRESS_SRC extension.\n"); 406 return; 407 } 408 sa = (struct sockaddr *)(m_saddr + 1); 409 switch (sa->sa_family) { 410 case AF_INET: 411 case AF_INET6: 412 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf), 413 NI_NUMERICSERV) != 0) 414 port = 0; /*XXX*/ 415 else 416 port = atoi(pbuf); 417 printf("%s%s ", str_ipaddr(sa), 418 str_prefport(sa->sa_family, 419 m_saddr->sadb_address_prefixlen, port)); 420 break; 421 default: 422 printf("unknown-af "); 423 break; 424 } 425 426 /* destination address */ 427 if (m_daddr == NULL) { 428 printf("no ADDRESS_DST extension.\n"); 429 return; 430 } 431 sa = (struct sockaddr *)(m_daddr + 1); 432 switch (sa->sa_family) { 433 case AF_INET: 434 case AF_INET6: 435 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf), 436 NI_NUMERICSERV) != 0) 437 port = 0; /*XXX*/ 438 else 439 port = atoi(pbuf); 440 printf("%s%s ", str_ipaddr(sa), 441 str_prefport(sa->sa_family, 442 m_daddr->sadb_address_prefixlen, port)); 443 break; 444 default: 445 printf("unknown-af "); 446 break; 447 } 448 449 /* upper layer protocol */ 450 if (m_saddr->sadb_address_proto != m_daddr->sadb_address_proto) { 451 printf("upper layer protocol mismatched.\n"); 452 return; 453 } 454 if (m_saddr->sadb_address_proto == IPSEC_ULPROTO_ANY) 455 printf("any"); 456 else 457 GETMSGSTR(str_upper, m_saddr->sadb_address_proto); 458 459 /* policy */ 460 { 461 char *d_xpl; 462 463 if (m_xpl == NULL) { 464 printf("no X_POLICY extension.\n"); 465 return; 466 } 467 d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t"); 468 469 /* dump SPD */ 470 printf("\n\t%s\n", d_xpl); 471 free(d_xpl); 472 } 473 474 /* lifetime */ 475 if (m_lft) { 476 printf("\tlifetime:%lu validtime:%lu\n", 477 (u_long)m_lft->sadb_lifetime_addtime, 478 (u_long)m_lft->sadb_lifetime_usetime); 479 } 480 481 printf("\tspid=%ld seq=%ld pid=%ld\n", 482 (u_long)m_xpl->sadb_x_policy_id, 483 (u_long)m->sadb_msg_seq, 484 (u_long)m->sadb_msg_pid); 485 486 /* XXX TEST */ 487 printf("\trefcnt=%u\n", m->sadb_msg_reserved); 488 489 return; 490 } 491 492 /* 493 * set "ipaddress" to buffer. 494 */ 495 static char * 496 str_ipaddr(sa) 497 struct sockaddr *sa; 498 { 499 static char buf[NI_MAXHOST]; 500 #ifdef NI_WITHSCOPEID 501 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID; 502 #else 503 const int niflag = NI_NUMERICHOST; 504 #endif 505 506 if (sa == NULL) 507 return ""; 508 509 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0) 510 return buf; 511 return NULL; 512 } 513 514 /* 515 * set "/prefix[port number]" to buffer. 516 */ 517 static char * 518 str_prefport(family, pref, port) 519 u_int family, pref, port; 520 { 521 static char buf[128]; 522 char prefbuf[128]; 523 char portbuf[128]; 524 int plen; 525 526 switch (family) { 527 case AF_INET: 528 plen = sizeof(struct in_addr) << 3; 529 break; 530 case AF_INET6: 531 plen = sizeof(struct in6_addr) << 3; 532 break; 533 default: 534 return "?"; 535 } 536 537 if (pref == plen) 538 prefbuf[0] = '\0'; 539 else 540 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref); 541 542 if (port == IPSEC_PORT_ANY) 543 snprintf(portbuf, sizeof(portbuf), "[%s]", "any"); 544 else 545 snprintf(portbuf, sizeof(portbuf), "[%u]", port); 546 547 snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf); 548 549 return buf; 550 } 551 552 /* 553 * set "Mon Day Time Year" to buffer 554 */ 555 static char * 556 str_time(t) 557 time_t t; 558 { 559 static char buf[128]; 560 561 if (t == 0) { 562 int i = 0; 563 for (;i < 20;) buf[i++] = ' '; 564 } else { 565 char *t0; 566 t0 = ctime(&t); 567 memcpy(buf, t0 + 4, 20); 568 } 569 570 buf[20] = '\0'; 571 572 return(buf); 573 } 574 575 static void 576 str_lifetime_byte(x, str) 577 struct sadb_lifetime *x; 578 char *str; 579 { 580 double y; 581 char *unit; 582 int w; 583 584 if (x == NULL) { 585 printf("\t%s: 0(bytes)", str); 586 return; 587 } 588 589 #if 0 590 if ((x->sadb_lifetime_bytes) / 1024 / 1024) { 591 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024; 592 unit = "M"; 593 w = 1; 594 } else if ((x->sadb_lifetime_bytes) / 1024) { 595 y = (x->sadb_lifetime_bytes) * 1.0 / 1024; 596 unit = "K"; 597 w = 1; 598 } else { 599 y = (x->sadb_lifetime_bytes) * 1.0; 600 unit = ""; 601 w = 0; 602 } 603 #else 604 y = (x->sadb_lifetime_bytes) * 1.0; 605 unit = ""; 606 w = 0; 607 #endif 608 printf("\t%s: %.*f(%sbytes)", str, w, y, unit); 609 } 610