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 { -1, NULL, }, 201 }; 202 203 static struct val2str str_alg_comp[] = { 204 { SADB_X_CALG_NONE, "none", }, 205 { SADB_X_CALG_OUI, "oui", }, 206 { SADB_X_CALG_DEFLATE, "deflate", }, 207 { SADB_X_CALG_LZS, "lzs", }, 208 { -1, NULL, }, 209 }; 210 211 /* 212 * dump SADB_MSG formated. For debugging, you should use kdebug_sadb(). 213 */ 214 void 215 pfkey_sadump(m) 216 struct sadb_msg *m; 217 { 218 caddr_t mhp[SADB_EXT_MAX + 1]; 219 struct sadb_sa *m_sa; 220 struct sadb_x_sa2 *m_sa2; 221 struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts; 222 struct sadb_address *m_saddr, *m_daddr, *m_paddr; 223 struct sadb_key *m_auth, *m_enc; 224 struct sadb_ident *m_sid, *m_did; 225 struct sadb_sens *m_sens; 226 227 /* check pfkey message. */ 228 if (pfkey_align(m, mhp)) { 229 printf("%s\n", ipsec_strerror()); 230 return; 231 } 232 if (pfkey_check(mhp)) { 233 printf("%s\n", ipsec_strerror()); 234 return; 235 } 236 237 m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA]; 238 m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2]; 239 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT]; 240 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 241 m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT]; 242 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; 243 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; 244 m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY]; 245 m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH]; 246 m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT]; 247 m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC]; 248 m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST]; 249 m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY]; 250 251 /* source address */ 252 if (m_saddr == NULL) { 253 printf("no ADDRESS_SRC extension.\n"); 254 return; 255 } 256 printf("%s ", str_ipaddr((struct sockaddr *)(m_saddr + 1))); 257 258 /* destination address */ 259 if (m_daddr == NULL) { 260 printf("no ADDRESS_DST extension.\n"); 261 return; 262 } 263 printf("%s ", str_ipaddr((struct sockaddr *)(m_daddr + 1))); 264 265 /* SA type */ 266 if (m_sa == NULL) { 267 printf("no SA extension.\n"); 268 return; 269 } 270 if (m_sa2 == NULL) { 271 printf("no SA2 extension.\n"); 272 return; 273 } 274 printf("\n\t"); 275 276 GETMSGSTR(str_satype, m->sadb_msg_satype); 277 278 printf("mode="); 279 GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode); 280 281 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n", 282 (u_int32_t)ntohl(m_sa->sadb_sa_spi), 283 (u_int32_t)ntohl(m_sa->sadb_sa_spi), 284 (u_int32_t)m_sa2->sadb_x_sa2_reqid, 285 (u_int32_t)m_sa2->sadb_x_sa2_reqid); 286 287 /* encryption key */ 288 if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) { 289 printf("\tC: "); 290 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt); 291 } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) { 292 if (m_enc != NULL) { 293 printf("\tE: "); 294 GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt); 295 ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc), 296 m_enc->sadb_key_bits / 8); 297 printf("\n"); 298 } 299 } 300 301 /* authentication key */ 302 if (m_auth != NULL) { 303 printf("\tA: "); 304 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth); 305 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth), 306 m_auth->sadb_key_bits / 8); 307 printf("\n"); 308 } 309 310 /* replay windoe size & flags */ 311 printf("\tseq=0x%08x replay=%u flags=0x%08x ", 312 m_sa2->sadb_x_sa2_sequence, 313 m_sa->sadb_sa_replay, 314 m_sa->sadb_sa_flags); 315 316 /* state */ 317 printf("state="); 318 GETMSGSTR(str_state, m_sa->sadb_sa_state); 319 printf("\n"); 320 321 /* lifetime */ 322 if (m_lftc != NULL) { 323 time_t tmp_time = time(0); 324 325 printf("\tcreated: %s", 326 str_time(m_lftc->sadb_lifetime_addtime)); 327 printf("\tcurrent: %s\n", str_time(tmp_time)); 328 printf("\tdiff: %lu(s)", 329 (u_long)(m_lftc->sadb_lifetime_addtime == 0 ? 330 0 : (tmp_time - m_lftc->sadb_lifetime_addtime))); 331 332 printf("\thard: %lu(s)", 333 (u_long)(m_lfth == NULL ? 334 0 : m_lfth->sadb_lifetime_addtime)); 335 printf("\tsoft: %lu(s)\n", 336 (u_long)(m_lfts == NULL ? 337 0 : m_lfts->sadb_lifetime_addtime)); 338 339 printf("\tlast: %s", 340 str_time(m_lftc->sadb_lifetime_usetime)); 341 printf("\thard: %lu(s)", 342 (u_long)(m_lfth == NULL ? 343 0 : m_lfth->sadb_lifetime_usetime)); 344 printf("\tsoft: %lu(s)\n", 345 (u_long)(m_lfts == NULL ? 346 0 : m_lfts->sadb_lifetime_usetime)); 347 348 str_lifetime_byte(m_lftc, "current"); 349 str_lifetime_byte(m_lfth, "hard"); 350 str_lifetime_byte(m_lfts, "soft"); 351 printf("\n"); 352 353 printf("\tallocated: %lu", 354 (unsigned long)m_lftc->sadb_lifetime_allocations); 355 printf("\thard: %lu", 356 (u_long)(m_lfth == NULL ? 357 0 : m_lfth->sadb_lifetime_allocations)); 358 printf("\tsoft: %lu\n", 359 (u_long)(m_lfts == NULL ? 360 0 : m_lfts->sadb_lifetime_allocations)); 361 } 362 363 printf("\tsadb_seq=%lu pid=%lu ", 364 (u_long)m->sadb_msg_seq, 365 (u_long)m->sadb_msg_pid); 366 367 /* XXX DEBUG */ 368 printf("refcnt=%u\n", m->sadb_msg_reserved); 369 370 return; 371 } 372 373 void 374 pfkey_spdump(m) 375 struct sadb_msg *m; 376 { 377 char pbuf[NI_MAXSERV]; 378 caddr_t mhp[SADB_EXT_MAX + 1]; 379 struct sadb_address *m_saddr, *m_daddr; 380 struct sadb_x_policy *m_xpl; 381 struct sadb_lifetime *m_lft = NULL; 382 struct sockaddr *sa; 383 u_int16_t port; 384 385 /* check pfkey message. */ 386 if (pfkey_align(m, mhp)) { 387 printf("%s\n", ipsec_strerror()); 388 return; 389 } 390 if (pfkey_check(mhp)) { 391 printf("%s\n", ipsec_strerror()); 392 return; 393 } 394 395 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC]; 396 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST]; 397 m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY]; 398 m_lft = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD]; 399 400 /* source address */ 401 if (m_saddr == NULL) { 402 printf("no ADDRESS_SRC extension.\n"); 403 return; 404 } 405 sa = (struct sockaddr *)(m_saddr + 1); 406 switch (sa->sa_family) { 407 case AF_INET: 408 case AF_INET6: 409 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf), 410 NI_NUMERICSERV) != 0) 411 port = 0; /*XXX*/ 412 else 413 port = atoi(pbuf); 414 printf("%s%s ", str_ipaddr(sa), 415 str_prefport(sa->sa_family, 416 m_saddr->sadb_address_prefixlen, port)); 417 break; 418 default: 419 printf("unknown-af "); 420 break; 421 } 422 423 /* destination address */ 424 if (m_daddr == NULL) { 425 printf("no ADDRESS_DST extension.\n"); 426 return; 427 } 428 sa = (struct sockaddr *)(m_daddr + 1); 429 switch (sa->sa_family) { 430 case AF_INET: 431 case AF_INET6: 432 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf), 433 NI_NUMERICSERV) != 0) 434 port = 0; /*XXX*/ 435 else 436 port = atoi(pbuf); 437 printf("%s%s ", str_ipaddr(sa), 438 str_prefport(sa->sa_family, 439 m_daddr->sadb_address_prefixlen, port)); 440 break; 441 default: 442 printf("unknown-af "); 443 break; 444 } 445 446 /* upper layer protocol */ 447 if (m_saddr->sadb_address_proto != m_daddr->sadb_address_proto) { 448 printf("upper layer protocol mismatched.\n"); 449 return; 450 } 451 if (m_saddr->sadb_address_proto == IPSEC_ULPROTO_ANY) 452 printf("any"); 453 else 454 GETMSGSTR(str_upper, m_saddr->sadb_address_proto); 455 456 /* policy */ 457 { 458 char *d_xpl; 459 460 if (m_xpl == NULL) { 461 printf("no X_POLICY extension.\n"); 462 return; 463 } 464 d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t"); 465 466 /* dump SPD */ 467 printf("\n\t%s\n", d_xpl); 468 free(d_xpl); 469 } 470 471 /* lifetime */ 472 if (m_lft) { 473 printf("\tlifetime:%lu validtime:%lu\n", 474 (u_long)m_lft->sadb_lifetime_addtime, 475 (u_long)m_lft->sadb_lifetime_usetime); 476 } 477 478 printf("\tspid=%ld seq=%ld pid=%ld\n", 479 (u_long)m_xpl->sadb_x_policy_id, 480 (u_long)m->sadb_msg_seq, 481 (u_long)m->sadb_msg_pid); 482 483 /* XXX TEST */ 484 printf("\trefcnt=%u\n", m->sadb_msg_reserved); 485 486 return; 487 } 488 489 /* 490 * set "ipaddress" to buffer. 491 */ 492 static char * 493 str_ipaddr(sa) 494 struct sockaddr *sa; 495 { 496 static char buf[NI_MAXHOST]; 497 #ifdef NI_WITHSCOPEID 498 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID; 499 #else 500 const int niflag = NI_NUMERICHOST; 501 #endif 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) 516 u_int family, pref, port; 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 (port == IPSEC_PORT_ANY) 540 snprintf(portbuf, sizeof(portbuf), "[%s]", "any"); 541 else 542 snprintf(portbuf, sizeof(portbuf), "[%u]", port); 543 544 snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf); 545 546 return buf; 547 } 548 549 /* 550 * set "Mon Day Time Year" to buffer 551 */ 552 static char * 553 str_time(t) 554 time_t t; 555 { 556 static char buf[128]; 557 558 if (t == 0) { 559 int i = 0; 560 for (;i < 20;) buf[i++] = ' '; 561 } else { 562 char *t0; 563 t0 = ctime(&t); 564 memcpy(buf, t0 + 4, 20); 565 } 566 567 buf[20] = '\0'; 568 569 return(buf); 570 } 571 572 static void 573 str_lifetime_byte(x, str) 574 struct sadb_lifetime *x; 575 char *str; 576 { 577 double y; 578 char *unit; 579 int w; 580 581 if (x == NULL) { 582 printf("\t%s: 0(bytes)", str); 583 return; 584 } 585 586 #if 0 587 if ((x->sadb_lifetime_bytes) / 1024 / 1024) { 588 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024; 589 unit = "M"; 590 w = 1; 591 } else if ((x->sadb_lifetime_bytes) / 1024) { 592 y = (x->sadb_lifetime_bytes) * 1.0 / 1024; 593 unit = "K"; 594 w = 1; 595 } else { 596 y = (x->sadb_lifetime_bytes) * 1.0; 597 unit = ""; 598 w = 0; 599 } 600 #else 601 y = (x->sadb_lifetime_bytes) * 1.0; 602 unit = ""; 603 w = 0; 604 #endif 605 printf("\t%s: %.*f(%sbytes)", str, w, y, unit); 606 } 607