1 /* $FreeBSD$ */ 2 /* $KAME: key_debug.c,v 1.26 2001/06/27 10:46:50 sakane Exp $ */ 3 4 /*- 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 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. 19 * 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. 31 */ 32 33 #ifdef _KERNEL 34 #include "opt_inet.h" 35 #include "opt_inet6.h" 36 #include "opt_ipsec.h" 37 #endif 38 39 #include <sys/param.h> 40 #ifdef _KERNEL 41 #include <sys/systm.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/mutex.h> 46 #include <sys/queue.h> 47 #endif 48 #include <sys/socket.h> 49 50 #include <net/vnet.h> 51 52 #include <netipsec/key_var.h> 53 #include <netipsec/key_debug.h> 54 55 #include <netinet/in.h> 56 #include <netipsec/ipsec.h> 57 #ifdef _KERNEL 58 #include <netipsec/keydb.h> 59 #include <netipsec/xform.h> 60 #endif 61 62 #ifndef _KERNEL 63 #include <ctype.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #endif /* !_KERNEL */ 67 68 static void kdebug_sadb_prop(struct sadb_ext *); 69 static void kdebug_sadb_identity(struct sadb_ext *); 70 static void kdebug_sadb_supported(struct sadb_ext *); 71 static void kdebug_sadb_lifetime(struct sadb_ext *); 72 static void kdebug_sadb_sa(struct sadb_ext *); 73 static void kdebug_sadb_address(struct sadb_ext *); 74 static void kdebug_sadb_key(struct sadb_ext *); 75 static void kdebug_sadb_x_sa2(struct sadb_ext *); 76 77 #ifdef _KERNEL 78 static void kdebug_secreplay(struct secreplay *); 79 #endif 80 81 #ifndef _KERNEL 82 #define panic(fmt, ...) { printf(fmt, ## __VA_ARGS__); exit(-1); } 83 #endif 84 85 /* NOTE: host byte order */ 86 87 /* %%%: about struct sadb_msg */ 88 void 89 kdebug_sadb(struct sadb_msg *base) 90 { 91 struct sadb_ext *ext; 92 int tlen, extlen; 93 94 /* sanity check */ 95 if (base == NULL) 96 panic("%s: NULL pointer was passed.\n", __func__); 97 98 printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n", 99 base->sadb_msg_version, base->sadb_msg_type, 100 base->sadb_msg_errno, base->sadb_msg_satype); 101 printf(" len=%u reserved=%u seq=%u pid=%u\n", 102 base->sadb_msg_len, base->sadb_msg_reserved, 103 base->sadb_msg_seq, base->sadb_msg_pid); 104 105 tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg); 106 ext = (struct sadb_ext *)((caddr_t)base + sizeof(struct sadb_msg)); 107 108 while (tlen > 0) { 109 printf("sadb_ext{ len=%u type=%u }\n", 110 ext->sadb_ext_len, ext->sadb_ext_type); 111 112 if (ext->sadb_ext_len == 0) { 113 printf("%s: invalid ext_len=0 was passed.\n", __func__); 114 return; 115 } 116 if (ext->sadb_ext_len > tlen) { 117 printf("%s: ext_len too big (%u > %u).\n", 118 __func__, ext->sadb_ext_len, tlen); 119 return; 120 } 121 122 switch (ext->sadb_ext_type) { 123 case SADB_EXT_SA: 124 kdebug_sadb_sa(ext); 125 break; 126 case SADB_EXT_LIFETIME_CURRENT: 127 case SADB_EXT_LIFETIME_HARD: 128 case SADB_EXT_LIFETIME_SOFT: 129 kdebug_sadb_lifetime(ext); 130 break; 131 case SADB_EXT_ADDRESS_SRC: 132 case SADB_EXT_ADDRESS_DST: 133 case SADB_EXT_ADDRESS_PROXY: 134 kdebug_sadb_address(ext); 135 break; 136 case SADB_EXT_KEY_AUTH: 137 case SADB_EXT_KEY_ENCRYPT: 138 kdebug_sadb_key(ext); 139 break; 140 case SADB_EXT_IDENTITY_SRC: 141 case SADB_EXT_IDENTITY_DST: 142 kdebug_sadb_identity(ext); 143 break; 144 case SADB_EXT_SENSITIVITY: 145 break; 146 case SADB_EXT_PROPOSAL: 147 kdebug_sadb_prop(ext); 148 break; 149 case SADB_EXT_SUPPORTED_AUTH: 150 case SADB_EXT_SUPPORTED_ENCRYPT: 151 kdebug_sadb_supported(ext); 152 break; 153 case SADB_EXT_SPIRANGE: 154 case SADB_X_EXT_KMPRIVATE: 155 break; 156 case SADB_X_EXT_POLICY: 157 kdebug_sadb_x_policy(ext); 158 break; 159 case SADB_X_EXT_SA2: 160 kdebug_sadb_x_sa2(ext); 161 break; 162 default: 163 printf("%s: invalid ext_type %u\n", __func__, 164 ext->sadb_ext_type); 165 return; 166 } 167 168 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len); 169 tlen -= extlen; 170 ext = (struct sadb_ext *)((caddr_t)ext + extlen); 171 } 172 173 return; 174 } 175 176 static void 177 kdebug_sadb_prop(struct sadb_ext *ext) 178 { 179 struct sadb_prop *prop = (struct sadb_prop *)ext; 180 struct sadb_comb *comb; 181 int len; 182 183 /* sanity check */ 184 if (ext == NULL) 185 panic("%s: NULL pointer was passed.\n", __func__); 186 187 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop)) 188 / sizeof(*comb); 189 comb = (struct sadb_comb *)(prop + 1); 190 printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay); 191 192 while (len--) { 193 printf("sadb_comb{ auth=%u encrypt=%u " 194 "flags=0x%04x reserved=0x%08x\n", 195 comb->sadb_comb_auth, comb->sadb_comb_encrypt, 196 comb->sadb_comb_flags, comb->sadb_comb_reserved); 197 198 printf(" auth_minbits=%u auth_maxbits=%u " 199 "encrypt_minbits=%u encrypt_maxbits=%u\n", 200 comb->sadb_comb_auth_minbits, 201 comb->sadb_comb_auth_maxbits, 202 comb->sadb_comb_encrypt_minbits, 203 comb->sadb_comb_encrypt_maxbits); 204 205 printf(" soft_alloc=%u hard_alloc=%u " 206 "soft_bytes=%lu hard_bytes=%lu\n", 207 comb->sadb_comb_soft_allocations, 208 comb->sadb_comb_hard_allocations, 209 (unsigned long)comb->sadb_comb_soft_bytes, 210 (unsigned long)comb->sadb_comb_hard_bytes); 211 212 printf(" soft_alloc=%lu hard_alloc=%lu " 213 "soft_bytes=%lu hard_bytes=%lu }\n", 214 (unsigned long)comb->sadb_comb_soft_addtime, 215 (unsigned long)comb->sadb_comb_hard_addtime, 216 (unsigned long)comb->sadb_comb_soft_usetime, 217 (unsigned long)comb->sadb_comb_hard_usetime); 218 comb++; 219 } 220 printf("}\n"); 221 222 return; 223 } 224 225 static void 226 kdebug_sadb_identity(struct sadb_ext *ext) 227 { 228 struct sadb_ident *id = (struct sadb_ident *)ext; 229 int len; 230 231 /* sanity check */ 232 if (ext == NULL) 233 panic("%s: NULL pointer was passed.\n", __func__); 234 235 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id); 236 printf("sadb_ident_%s{", 237 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst"); 238 switch (id->sadb_ident_type) { 239 default: 240 printf(" type=%d id=%lu", 241 id->sadb_ident_type, (u_long)id->sadb_ident_id); 242 if (len) { 243 #ifdef _KERNEL 244 ipsec_hexdump((caddr_t)(id + 1), len); /*XXX cast ?*/ 245 #else 246 char *p, *ep; 247 printf("\n str=\""); 248 p = (char *)(id + 1); 249 ep = p + len; 250 for (/*nothing*/; *p && p < ep; p++) { 251 if (isprint(*p)) 252 printf("%c", *p & 0xff); 253 else 254 printf("\\%03o", *p & 0xff); 255 } 256 #endif 257 printf("\""); 258 } 259 break; 260 } 261 262 printf(" }\n"); 263 264 return; 265 } 266 267 static void 268 kdebug_sadb_supported(struct sadb_ext *ext) 269 { 270 struct sadb_supported *sup = (struct sadb_supported *)ext; 271 struct sadb_alg *alg; 272 int len; 273 274 /* sanity check */ 275 if (ext == NULL) 276 panic("%s: NULL pointer was passed.\n", __func__); 277 278 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup)) 279 / sizeof(*alg); 280 alg = (struct sadb_alg *)(sup + 1); 281 printf("sadb_sup{\n"); 282 while (len--) { 283 printf(" { id=%d ivlen=%d min=%d max=%d }\n", 284 alg->sadb_alg_id, alg->sadb_alg_ivlen, 285 alg->sadb_alg_minbits, alg->sadb_alg_maxbits); 286 alg++; 287 } 288 printf("}\n"); 289 290 return; 291 } 292 293 static void 294 kdebug_sadb_lifetime(struct sadb_ext *ext) 295 { 296 struct sadb_lifetime *lft = (struct sadb_lifetime *)ext; 297 298 /* sanity check */ 299 if (ext == NULL) 300 panic("%s: NULL pointer was passed.\n", __func__); 301 302 printf("sadb_lifetime{ alloc=%u, bytes=%u\n", 303 lft->sadb_lifetime_allocations, 304 (u_int32_t)lft->sadb_lifetime_bytes); 305 printf(" addtime=%u, usetime=%u }\n", 306 (u_int32_t)lft->sadb_lifetime_addtime, 307 (u_int32_t)lft->sadb_lifetime_usetime); 308 309 return; 310 } 311 312 static void 313 kdebug_sadb_sa(struct sadb_ext *ext) 314 { 315 struct sadb_sa *sa = (struct sadb_sa *)ext; 316 317 /* sanity check */ 318 if (ext == NULL) 319 panic("%s: NULL pointer was passed.\n", __func__); 320 321 printf("sadb_sa{ spi=%u replay=%u state=%u\n", 322 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay, 323 sa->sadb_sa_state); 324 printf(" auth=%u encrypt=%u flags=0x%08x }\n", 325 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags); 326 327 return; 328 } 329 330 static void 331 kdebug_sadb_address(struct sadb_ext *ext) 332 { 333 struct sadb_address *addr = (struct sadb_address *)ext; 334 335 /* sanity check */ 336 if (ext == NULL) 337 panic("%s: NULL pointer was passed.\n", __func__); 338 339 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n", 340 addr->sadb_address_proto, addr->sadb_address_prefixlen, 341 ((u_char *)&addr->sadb_address_reserved)[0], 342 ((u_char *)&addr->sadb_address_reserved)[1]); 343 344 kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr))); 345 346 return; 347 } 348 349 static void 350 kdebug_sadb_key(struct sadb_ext *ext) 351 { 352 struct sadb_key *key = (struct sadb_key *)ext; 353 354 /* sanity check */ 355 if (ext == NULL) 356 panic("%s: NULL pointer was passed.\n", __func__); 357 358 printf("sadb_key{ bits=%u reserved=%u\n", 359 key->sadb_key_bits, key->sadb_key_reserved); 360 printf(" key="); 361 362 /* sanity check 2 */ 363 if ((key->sadb_key_bits >> 3) > 364 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) { 365 printf("%s: key length mismatch, bit:%d len:%ld.\n", 366 __func__, 367 key->sadb_key_bits >> 3, 368 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key)); 369 } 370 371 ipsec_hexdump((caddr_t)key + sizeof(struct sadb_key), 372 key->sadb_key_bits >> 3); 373 printf(" }\n"); 374 return; 375 } 376 377 static void 378 kdebug_sadb_x_sa2(struct sadb_ext *ext) 379 { 380 struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext; 381 382 /* sanity check */ 383 if (ext == NULL) 384 panic("%s: NULL pointer was passed.\n", __func__); 385 386 printf("sadb_x_sa2{ mode=%u reqid=%u\n", 387 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid); 388 printf(" reserved1=%u reserved2=%u sequence=%u }\n", 389 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2, 390 sa2->sadb_x_sa2_sequence); 391 392 return; 393 } 394 395 void 396 kdebug_sadb_x_policy(struct sadb_ext *ext) 397 { 398 struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext; 399 struct sockaddr *addr; 400 401 /* sanity check */ 402 if (ext == NULL) 403 panic("%s: NULL pointer was passed.\n", __func__); 404 405 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n", 406 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir, 407 xpl->sadb_x_policy_id); 408 409 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) { 410 int tlen; 411 struct sadb_x_ipsecrequest *xisr; 412 413 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl); 414 xisr = (struct sadb_x_ipsecrequest *)(xpl + 1); 415 416 while (tlen > 0) { 417 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n", 418 xisr->sadb_x_ipsecrequest_len, 419 xisr->sadb_x_ipsecrequest_proto, 420 xisr->sadb_x_ipsecrequest_mode, 421 xisr->sadb_x_ipsecrequest_level, 422 xisr->sadb_x_ipsecrequest_reqid); 423 424 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) { 425 addr = (struct sockaddr *)(xisr + 1); 426 kdebug_sockaddr(addr); 427 addr = (struct sockaddr *)((caddr_t)addr 428 + addr->sa_len); 429 kdebug_sockaddr(addr); 430 } 431 432 printf(" }\n"); 433 434 /* prevent infinite loop */ 435 if (xisr->sadb_x_ipsecrequest_len <= 0) { 436 printf("%s: wrong policy struct.\n", __func__); 437 return; 438 } 439 /* prevent overflow */ 440 if (xisr->sadb_x_ipsecrequest_len > tlen) { 441 printf("%s: invalid ipsec policy length " 442 "(%u > %u)\n", __func__, 443 xisr->sadb_x_ipsecrequest_len, tlen); 444 return; 445 } 446 447 tlen -= xisr->sadb_x_ipsecrequest_len; 448 449 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr 450 + xisr->sadb_x_ipsecrequest_len); 451 } 452 453 if (tlen != 0) 454 panic("%s: wrong policy struct.\n", __func__); 455 } 456 457 return; 458 } 459 460 #ifdef _KERNEL 461 /* %%%: about SPD and SAD */ 462 const char* 463 kdebug_secpolicy_state(u_int state) 464 { 465 466 switch (state) { 467 case IPSEC_SPSTATE_DEAD: 468 return ("dead"); 469 case IPSEC_SPSTATE_LARVAL: 470 return ("larval"); 471 case IPSEC_SPSTATE_ALIVE: 472 return ("alive"); 473 case IPSEC_SPSTATE_PCB: 474 return ("pcb"); 475 case IPSEC_SPSTATE_IFNET: 476 return ("ifnet"); 477 } 478 return ("unknown"); 479 } 480 481 const char* 482 kdebug_secpolicy_policy(u_int policy) 483 { 484 485 switch (policy) { 486 case IPSEC_POLICY_DISCARD: 487 return ("discard"); 488 case IPSEC_POLICY_NONE: 489 return ("none"); 490 case IPSEC_POLICY_IPSEC: 491 return ("ipsec"); 492 case IPSEC_POLICY_ENTRUST: 493 return ("entrust"); 494 case IPSEC_POLICY_BYPASS: 495 return ("bypass"); 496 } 497 return ("unknown"); 498 } 499 500 const char* 501 kdebug_secpolicyindex_dir(u_int dir) 502 { 503 504 switch (dir) { 505 case IPSEC_DIR_ANY: 506 return ("any"); 507 case IPSEC_DIR_INBOUND: 508 return ("in"); 509 case IPSEC_DIR_OUTBOUND: 510 return ("out"); 511 } 512 return ("unknown"); 513 } 514 515 const char* 516 kdebug_ipsecrequest_level(u_int level) 517 { 518 519 switch (level) { 520 case IPSEC_LEVEL_DEFAULT: 521 return ("default"); 522 case IPSEC_LEVEL_USE: 523 return ("use"); 524 case IPSEC_LEVEL_REQUIRE: 525 return ("require"); 526 case IPSEC_LEVEL_UNIQUE: 527 return ("unique"); 528 } 529 return ("unknown"); 530 } 531 532 const char* 533 kdebug_secasindex_mode(u_int mode) 534 { 535 536 switch (mode) { 537 case IPSEC_MODE_ANY: 538 return ("any"); 539 case IPSEC_MODE_TRANSPORT: 540 return ("transport"); 541 case IPSEC_MODE_TUNNEL: 542 return ("tunnel"); 543 case IPSEC_MODE_TCPMD5: 544 return ("tcp-md5"); 545 } 546 return ("unknown"); 547 } 548 549 const char* 550 kdebug_secasv_state(u_int state) 551 { 552 553 switch (state) { 554 case SADB_SASTATE_LARVAL: 555 return ("larval"); 556 case SADB_SASTATE_MATURE: 557 return ("mature"); 558 case SADB_SASTATE_DYING: 559 return ("dying"); 560 case SADB_SASTATE_DEAD: 561 return ("dead"); 562 } 563 return ("unknown"); 564 } 565 566 static char* 567 kdebug_port2str(const struct sockaddr *sa, char *buf, size_t len) 568 { 569 uint16_t port; 570 571 IPSEC_ASSERT(sa != NULL, ("null sa")); 572 switch (sa->sa_family) { 573 #ifdef INET 574 case AF_INET: 575 port = ntohs(((const struct sockaddr_in *)sa)->sin_port); 576 break; 577 #endif 578 #ifdef INET6 579 case AF_INET6: 580 port = ntohs(((const struct sockaddr_in6 *)sa)->sin6_port); 581 break; 582 #endif 583 default: 584 port = 0; 585 } 586 if (port == 0) 587 return ("*"); 588 snprintf(buf, len, "%u", port); 589 return (buf); 590 } 591 592 void 593 kdebug_secpolicy(struct secpolicy *sp) 594 { 595 u_int idx; 596 597 IPSEC_ASSERT(sp != NULL, ("null sp")); 598 printf("SP { refcnt=%u id=%u priority=%u state=%s policy=%s\n", 599 sp->refcnt, sp->id, sp->priority, 600 kdebug_secpolicy_state(sp->state), 601 kdebug_secpolicy_policy(sp->policy)); 602 kdebug_secpolicyindex(&sp->spidx, " "); 603 for (idx = 0; idx < sp->tcount; idx++) { 604 printf(" req[%u]{ level=%s ", idx, 605 kdebug_ipsecrequest_level(sp->req[idx]->level)); 606 kdebug_secasindex(&sp->req[idx]->saidx, NULL); 607 printf(" }\n"); 608 } 609 printf("}\n"); 610 } 611 612 void 613 kdebug_secpolicyindex(struct secpolicyindex *spidx, const char *indent) 614 { 615 char buf[IPSEC_ADDRSTRLEN]; 616 617 IPSEC_ASSERT(spidx != NULL, ("null spidx")); 618 if (indent != NULL) 619 printf("%s", indent); 620 printf("spidx { dir=%s ul_proto=", 621 kdebug_secpolicyindex_dir(spidx->dir)); 622 if (spidx->ul_proto == IPSEC_ULPROTO_ANY) 623 printf("* "); 624 else 625 printf("%u ", spidx->ul_proto); 626 printf("%s/%u -> ", ipsec_address(&spidx->src, buf, sizeof(buf)), 627 spidx->prefs); 628 printf("%s/%u }\n", ipsec_address(&spidx->dst, buf, sizeof(buf)), 629 spidx->prefd); 630 } 631 632 void 633 kdebug_secasindex(const struct secasindex *saidx, const char *indent) 634 { 635 char buf[IPSEC_ADDRSTRLEN], port[6]; 636 637 IPSEC_ASSERT(saidx != NULL, ("null saidx")); 638 if (indent != NULL) 639 printf("%s", indent); 640 printf("saidx { mode=%s proto=%u reqid=%u ", 641 kdebug_secasindex_mode(saidx->mode), saidx->proto, saidx->reqid); 642 printf("%s:%s -> ", ipsec_address(&saidx->src, buf, sizeof(buf)), 643 kdebug_port2str(&saidx->src.sa, port, sizeof(port))); 644 printf("%s:%s }\n", ipsec_address(&saidx->dst, buf, sizeof(buf)), 645 kdebug_port2str(&saidx->dst.sa, port, sizeof(port))); 646 } 647 648 static void 649 kdebug_sec_lifetime(struct seclifetime *lft, const char *indent) 650 { 651 652 IPSEC_ASSERT(lft != NULL, ("null lft")); 653 if (indent != NULL) 654 printf("%s", indent); 655 printf("lifetime { alloc=%u, bytes=%ju addtime=%ju usetime=%ju }\n", 656 lft->allocations, (uintmax_t)lft->bytes, (uintmax_t)lft->addtime, 657 (uintmax_t)lft->usetime); 658 } 659 660 void 661 kdebug_secash(struct secashead *sah, const char *indent) 662 { 663 664 IPSEC_ASSERT(sah != NULL, ("null sah")); 665 if (indent != NULL) 666 printf("%s", indent); 667 printf("SAH { refcnt=%u state=%s\n", sah->refcnt, 668 kdebug_secasv_state(sah->state)); 669 if (indent != NULL) 670 printf("%s", indent); 671 kdebug_secasindex(&sah->saidx, indent); 672 if (indent != NULL) 673 printf("%s", indent); 674 printf("}\n"); 675 } 676 677 static void 678 kdebug_secreplay(struct secreplay *rpl) 679 { 680 int len, l; 681 682 IPSEC_ASSERT(rpl != NULL, ("null rpl")); 683 printf(" secreplay{ count=%u bitmap_size=%u wsize=%u seq=%u lastseq=%u", 684 rpl->count, rpl->bitmap_size, rpl->wsize, rpl->seq, rpl->lastseq); 685 686 if (rpl->bitmap == NULL) { 687 printf(" }\n"); 688 return; 689 } 690 691 printf("\n bitmap { "); 692 for (len = 0; len < rpl->bitmap_size*4; len++) { 693 for (l = 7; l >= 0; l--) 694 printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0); 695 } 696 printf(" }\n"); 697 } 698 699 static void 700 kdebug_secnatt(struct secnatt *natt) 701 { 702 char buf[IPSEC_ADDRSTRLEN]; 703 704 IPSEC_ASSERT(natt != NULL, ("null natt")); 705 printf(" natt{ sport=%u dport=%u ", ntohs(natt->sport), 706 ntohs(natt->dport)); 707 if (natt->flags & IPSEC_NATT_F_OAI) 708 printf("oai=%s ", ipsec_address(&natt->oai, buf, sizeof(buf))); 709 if (natt->flags & IPSEC_NATT_F_OAR) 710 printf("oar=%s ", ipsec_address(&natt->oar, buf, sizeof(buf))); 711 printf("}\n"); 712 } 713 714 void 715 kdebug_secasv(struct secasvar *sav) 716 { 717 struct seclifetime lft_c; 718 719 IPSEC_ASSERT(sav != NULL, ("null sav")); 720 721 printf("SA { refcnt=%u spi=%u seq=%u pid=%u flags=0x%x state=%s\n", 722 sav->refcnt, ntohl(sav->spi), sav->seq, (uint32_t)sav->pid, 723 sav->flags, kdebug_secasv_state(sav->state)); 724 kdebug_secash(sav->sah, " "); 725 726 lft_c.addtime = sav->created; 727 lft_c.allocations = (uint32_t)counter_u64_fetch( 728 sav->lft_c_allocations); 729 lft_c.bytes = counter_u64_fetch(sav->lft_c_bytes); 730 lft_c.usetime = sav->firstused; 731 kdebug_sec_lifetime(&lft_c, " c_"); 732 if (sav->lft_h != NULL) 733 kdebug_sec_lifetime(sav->lft_h, " h_"); 734 if (sav->lft_s != NULL) 735 kdebug_sec_lifetime(sav->lft_s, " s_"); 736 737 if (sav->tdb_authalgxform != NULL) 738 printf(" alg_auth=%s\n", sav->tdb_authalgxform->name); 739 if (sav->key_auth != NULL) 740 KEYDBG(DUMP, 741 kdebug_sadb_key((struct sadb_ext *)sav->key_auth)); 742 if (sav->tdb_encalgxform != NULL) 743 printf(" alg_enc=%s\n", sav->tdb_encalgxform->name); 744 if (sav->key_enc != NULL) 745 KEYDBG(DUMP, 746 kdebug_sadb_key((struct sadb_ext *)sav->key_enc)); 747 if (sav->natt != NULL) 748 kdebug_secnatt(sav->natt); 749 if (sav->replay != NULL) { 750 KEYDBG(DUMP, 751 SECASVAR_LOCK(sav); 752 kdebug_secreplay(sav->replay); 753 SECASVAR_UNLOCK(sav)); 754 } 755 printf("}\n"); 756 } 757 758 void 759 kdebug_mbufhdr(const struct mbuf *m) 760 { 761 /* sanity check */ 762 if (m == NULL) 763 return; 764 765 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p " 766 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n", 767 m, m->m_next, m->m_nextpkt, m->m_data, 768 m->m_len, m->m_type, m->m_flags); 769 770 if (m->m_flags & M_PKTHDR) { 771 printf(" m_pkthdr{ len:%d rcvif:%p }\n", 772 m->m_pkthdr.len, m->m_pkthdr.rcvif); 773 } 774 775 if (m->m_flags & M_EXT) { 776 printf(" m_ext{ ext_buf:%p ext_free:%p " 777 "ext_size:%u ext_cnt:%p }\n", 778 m->m_ext.ext_buf, m->m_ext.ext_free, 779 m->m_ext.ext_size, m->m_ext.ext_cnt); 780 } 781 782 return; 783 } 784 785 void 786 kdebug_mbuf(const struct mbuf *m0) 787 { 788 const struct mbuf *m = m0; 789 int i, j; 790 791 for (j = 0; m; m = m->m_next) { 792 kdebug_mbufhdr(m); 793 printf(" m_data:\n"); 794 for (i = 0; i < m->m_len; i++) { 795 if (i && i % 32 == 0) 796 printf("\n"); 797 if (i % 4 == 0) 798 printf(" "); 799 printf("%02x", mtod(m, const u_char *)[i]); 800 j++; 801 } 802 printf("\n"); 803 } 804 805 return; 806 } 807 808 /* Return a printable string for the address. */ 809 char * 810 ipsec_address(const union sockaddr_union* sa, char *buf, socklen_t size) 811 { 812 813 switch (sa->sa.sa_family) { 814 #ifdef INET 815 case AF_INET: 816 return (inet_ntop(AF_INET, &sa->sin.sin_addr, buf, size)); 817 #endif /* INET */ 818 #ifdef INET6 819 case AF_INET6: 820 if (IN6_IS_SCOPE_LINKLOCAL(&sa->sin6.sin6_addr)) { 821 snprintf(buf, size, "%s%%%u", inet_ntop(AF_INET6, 822 &sa->sin6.sin6_addr, buf, size), 823 sa->sin6.sin6_scope_id); 824 return (buf); 825 } else 826 return (inet_ntop(AF_INET6, &sa->sin6.sin6_addr, 827 buf, size)); 828 #endif /* INET6 */ 829 case 0: 830 return ("*"); 831 default: 832 return ("(unknown address family)"); 833 } 834 } 835 836 char * 837 ipsec_sa2str(struct secasvar *sav, char *buf, size_t size) 838 { 839 char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN]; 840 841 snprintf(buf, size, "SA(SPI=%08lx src=%s dst=%s)", 842 (u_long)ntohl(sav->spi), 843 ipsec_address(&sav->sah->saidx.src, sbuf, sizeof(sbuf)), 844 ipsec_address(&sav->sah->saidx.dst, dbuf, sizeof(dbuf))); 845 return (buf); 846 } 847 848 #endif /* _KERNEL */ 849 850 void 851 kdebug_sockaddr(struct sockaddr *addr) 852 { 853 struct sockaddr_in *sin4; 854 #ifdef INET6 855 struct sockaddr_in6 *sin6; 856 #endif 857 858 /* sanity check */ 859 if (addr == NULL) 860 panic("%s: NULL pointer was passed.\n", __func__); 861 862 /* NOTE: We deal with port number as host byte order. */ 863 printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family); 864 865 switch (addr->sa_family) { 866 case AF_INET: 867 sin4 = (struct sockaddr_in *)addr; 868 printf(" port=%u\n", ntohs(sin4->sin_port)); 869 ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr)); 870 break; 871 #ifdef INET6 872 case AF_INET6: 873 sin6 = (struct sockaddr_in6 *)addr; 874 printf(" port=%u\n", ntohs(sin6->sin6_port)); 875 printf(" flowinfo=0x%08x, scope_id=0x%08x\n", 876 sin6->sin6_flowinfo, sin6->sin6_scope_id); 877 ipsec_hexdump((caddr_t)&sin6->sin6_addr, 878 sizeof(sin6->sin6_addr)); 879 break; 880 #endif 881 } 882 883 printf(" }\n"); 884 885 return; 886 } 887 888 void 889 ipsec_bindump(caddr_t buf, int len) 890 { 891 int i; 892 893 for (i = 0; i < len; i++) 894 printf("%c", (unsigned char)buf[i]); 895 896 return; 897 } 898 899 900 void 901 ipsec_hexdump(caddr_t buf, int len) 902 { 903 int i; 904 905 for (i = 0; i < len; i++) { 906 if (i != 0 && i % 32 == 0) printf("\n"); 907 if (i % 4 == 0) printf(" "); 908 printf("%02x", (unsigned char)buf[i]); 909 } 910 #if 0 911 if (i % 32 != 0) printf("\n"); 912 #endif 913 914 return; 915 } 916