1 /* $NetBSD: key_debug.c,v 1.19 2017/07/26 03:59:59 ozaki-r Exp $ */ 2 /* $FreeBSD: src/sys/netipsec/key_debug.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $ */ 3 /* $KAME: key_debug.c,v 1.26 2001/06/27 10:46:50 sakane Exp $ */ 4 5 /* 6 * Copyright (C) 1995, 1996, 1997, and 1998 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 _KERNEL 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: key_debug.c,v 1.19 2017/07/26 03:59:59 ozaki-r Exp $"); 37 #endif 38 39 #if defined(_KERNEL_OPT) 40 #include "opt_inet.h" 41 #endif 42 43 #include <sys/types.h> 44 #include <sys/param.h> 45 #ifdef _KERNEL 46 #include <sys/systm.h> 47 #include <sys/mbuf.h> 48 #include <sys/queue.h> 49 #endif 50 #include <sys/socket.h> 51 52 #include <net/route.h> 53 54 #include <netipsec/key.h> 55 #include <netipsec/key_var.h> 56 #include <netipsec/key_debug.h> 57 58 #include <netinet/in.h> 59 #include <netipsec/ipsec.h> 60 61 #ifndef _KERNEL 62 #include <ctype.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #endif /* !_KERNEL */ 66 67 static void kdebug_sadb_prop (const struct sadb_ext *); 68 static void kdebug_sadb_identity (const struct sadb_ext *); 69 static void kdebug_sadb_supported (const struct sadb_ext *); 70 static void kdebug_sadb_lifetime (const struct sadb_ext *); 71 static void kdebug_sadb_sa (const struct sadb_ext *); 72 static void kdebug_sadb_address (const struct sadb_ext *); 73 static void kdebug_sadb_key (const struct sadb_ext *); 74 static void kdebug_sadb_x_sa2 (const struct sadb_ext *); 75 76 #ifdef _KERNEL 77 static void kdebug_secreplay (const struct secreplay *); 78 #endif 79 80 #ifndef _KERNEL 81 #define panic(param) { printf(param); exit(-1); } 82 #endif 83 84 /* NOTE: host byte order */ 85 86 /* %%%: about struct sadb_msg */ 87 void 88 kdebug_sadb(const struct sadb_msg *base) 89 { 90 const struct sadb_ext *ext; 91 int tlen, extlen; 92 93 /* sanity check */ 94 if (base == NULL) 95 panic("kdebug_sadb: NULL pointer was passed"); 96 97 printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n", 98 base->sadb_msg_version, base->sadb_msg_type, 99 base->sadb_msg_errno, base->sadb_msg_satype); 100 printf(" len=%u reserved=%u seq=%u pid=%u\n", 101 base->sadb_msg_len, base->sadb_msg_reserved, 102 base->sadb_msg_seq, base->sadb_msg_pid); 103 104 tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg); 105 ext = (const struct sadb_ext *)((const char *)base + sizeof(struct sadb_msg)); 106 107 while (tlen > 0) { 108 printf("sadb_ext{ len=%u type=%u }\n", 109 PFKEY_UNUNIT64(ext->sadb_ext_len), ext->sadb_ext_type); 110 111 if (ext->sadb_ext_len == 0) { 112 printf("kdebug_sadb: invalid ext_len=0 was passed.\n"); 113 return; 114 } 115 if (ext->sadb_ext_len > tlen) { 116 printf("kdebug_sadb: ext_len exceeds end of buffer.\n"); 117 return; 118 } 119 120 switch (ext->sadb_ext_type) { 121 case SADB_EXT_SA: 122 kdebug_sadb_sa(ext); 123 break; 124 case SADB_EXT_LIFETIME_CURRENT: 125 case SADB_EXT_LIFETIME_HARD: 126 case SADB_EXT_LIFETIME_SOFT: 127 kdebug_sadb_lifetime(ext); 128 break; 129 case SADB_EXT_ADDRESS_SRC: 130 case SADB_EXT_ADDRESS_DST: 131 case SADB_EXT_ADDRESS_PROXY: 132 kdebug_sadb_address(ext); 133 break; 134 case SADB_EXT_KEY_AUTH: 135 case SADB_EXT_KEY_ENCRYPT: 136 kdebug_sadb_key(ext); 137 break; 138 case SADB_EXT_IDENTITY_SRC: 139 case SADB_EXT_IDENTITY_DST: 140 kdebug_sadb_identity(ext); 141 break; 142 case SADB_EXT_SENSITIVITY: 143 break; 144 case SADB_EXT_PROPOSAL: 145 kdebug_sadb_prop(ext); 146 break; 147 case SADB_EXT_SUPPORTED_AUTH: 148 case SADB_EXT_SUPPORTED_ENCRYPT: 149 kdebug_sadb_supported(ext); 150 break; 151 case SADB_EXT_SPIRANGE: 152 case SADB_X_EXT_KMPRIVATE: 153 break; 154 case SADB_X_EXT_POLICY: 155 kdebug_sadb_x_policy(ext); 156 break; 157 case SADB_X_EXT_SA2: 158 kdebug_sadb_x_sa2(ext); 159 break; 160 default: 161 printf("kdebug_sadb: invalid ext_type %u was passed.\n", 162 ext->sadb_ext_type); 163 return; 164 } 165 166 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len); 167 tlen -= extlen; 168 ext = (const struct sadb_ext *)((const char *)ext + extlen); 169 } 170 171 return; 172 } 173 174 static void 175 kdebug_sadb_prop(const struct sadb_ext *ext) 176 { 177 const struct sadb_prop *prop = (const struct sadb_prop *)ext; 178 const struct sadb_comb *comb; 179 int len; 180 181 /* sanity check */ 182 if (ext == NULL) 183 panic("kdebug_sadb_prop: NULL pointer was passed"); 184 185 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop)) 186 / sizeof(*comb); 187 comb = (const struct sadb_comb *)(prop + 1); 188 printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay); 189 190 while (len--) { 191 printf("sadb_comb{ auth=%u encrypt=%u " 192 "flags=0x%04x reserved=0x%08x\n", 193 comb->sadb_comb_auth, comb->sadb_comb_encrypt, 194 comb->sadb_comb_flags, comb->sadb_comb_reserved); 195 196 printf(" auth_minbits=%u auth_maxbits=%u " 197 "encrypt_minbits=%u encrypt_maxbits=%u\n", 198 comb->sadb_comb_auth_minbits, 199 comb->sadb_comb_auth_maxbits, 200 comb->sadb_comb_encrypt_minbits, 201 comb->sadb_comb_encrypt_maxbits); 202 203 printf(" soft_alloc=%u hard_alloc=%u " 204 "soft_bytes=%lu hard_bytes=%lu\n", 205 comb->sadb_comb_soft_allocations, 206 comb->sadb_comb_hard_allocations, 207 (unsigned long)comb->sadb_comb_soft_bytes, 208 (unsigned long)comb->sadb_comb_hard_bytes); 209 210 printf(" soft_alloc=%lu hard_alloc=%lu " 211 "soft_bytes=%lu hard_bytes=%lu }\n", 212 (unsigned long)comb->sadb_comb_soft_addtime, 213 (unsigned long)comb->sadb_comb_hard_addtime, 214 (unsigned long)comb->sadb_comb_soft_usetime, 215 (unsigned long)comb->sadb_comb_hard_usetime); 216 comb++; 217 } 218 printf("}\n"); 219 220 return; 221 } 222 223 static void 224 kdebug_sadb_identity(const struct sadb_ext *ext) 225 { 226 const struct sadb_ident *id = (const struct sadb_ident *)ext; 227 int len; 228 229 /* sanity check */ 230 if (ext == NULL) 231 panic("kdebug_sadb_identity: NULL pointer was passed"); 232 233 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id); 234 printf("sadb_ident_%s{", 235 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst"); 236 switch (id->sadb_ident_type) { 237 default: 238 printf(" type=%d id=%lu", 239 id->sadb_ident_type, (u_long)id->sadb_ident_id); 240 if (len) { 241 #ifdef _KERNEL 242 ipsec_hexdump((const char *)(id + 1), len); /*XXX cast ?*/ 243 #else 244 const char *p, *ep; 245 printf("\n str=\""); 246 p = (const char *)(id + 1); 247 ep = p + len; 248 for (/*nothing*/; *p && p < ep; p++) { 249 if (isprint(*p)) 250 printf("%c", *p & 0xff); 251 else 252 printf("\\%03o", *p & 0xff); 253 } 254 #endif 255 printf("\""); 256 } 257 break; 258 } 259 260 printf(" }\n"); 261 262 return; 263 } 264 265 static void 266 kdebug_sadb_supported(const struct sadb_ext *ext) 267 { 268 const struct sadb_supported *sup = (const struct sadb_supported *)ext; 269 const struct sadb_alg *alg; 270 int len; 271 272 /* sanity check */ 273 if (ext == NULL) 274 panic("kdebug_sadb_supported: NULL pointer was passed"); 275 276 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup)) 277 / sizeof(*alg); 278 alg = (const struct sadb_alg *)(sup + 1); 279 printf("sadb_sup{\n"); 280 while (len--) { 281 printf(" { id=%d ivlen=%d min=%d max=%d }\n", 282 alg->sadb_alg_id, alg->sadb_alg_ivlen, 283 alg->sadb_alg_minbits, alg->sadb_alg_maxbits); 284 alg++; 285 } 286 printf("}\n"); 287 288 return; 289 } 290 291 static void 292 kdebug_sadb_lifetime(const struct sadb_ext *ext) 293 { 294 const struct sadb_lifetime *lft = (const struct sadb_lifetime *)ext; 295 296 /* sanity check */ 297 if (ext == NULL) 298 printf("kdebug_sadb_lifetime: NULL pointer was passed.\n"); 299 300 printf("sadb_lifetime{ alloc=%u, bytes=%u\n", 301 lft->sadb_lifetime_allocations, 302 (u_int32_t)lft->sadb_lifetime_bytes); 303 printf(" addtime=%u, usetime=%u }\n", 304 (u_int32_t)lft->sadb_lifetime_addtime, 305 (u_int32_t)lft->sadb_lifetime_usetime); 306 307 return; 308 } 309 310 static void 311 kdebug_sadb_sa(const struct sadb_ext *ext) 312 { 313 const struct sadb_sa *sa = (const struct sadb_sa *)ext; 314 315 /* sanity check */ 316 if (ext == NULL) 317 panic("kdebug_sadb_sa: NULL pointer was passed"); 318 319 printf("sadb_sa{ spi=%u replay=%u state=%u\n", 320 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay, 321 sa->sadb_sa_state); 322 printf(" auth=%u encrypt=%u flags=0x%08x }\n", 323 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags); 324 325 return; 326 } 327 328 static void 329 kdebug_sadb_address(const struct sadb_ext *ext) 330 { 331 const struct sadb_address *addr = (const struct sadb_address *)ext; 332 333 /* sanity check */ 334 if (ext == NULL) 335 panic("kdebug_sadb_address: NULL pointer was passed"); 336 337 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n", 338 addr->sadb_address_proto, addr->sadb_address_prefixlen, 339 ((const u_char *)&addr->sadb_address_reserved)[0], 340 ((const u_char *)&addr->sadb_address_reserved)[1]); 341 342 kdebug_sockaddr((const struct sockaddr *)((const char *)ext + sizeof(*addr))); 343 344 return; 345 } 346 347 static void 348 kdebug_sadb_key(const struct sadb_ext *ext) 349 { 350 const struct sadb_key *key = (const struct sadb_key *)ext; 351 352 /* sanity check */ 353 if (ext == NULL) 354 panic("kdebug_sadb_key: NULL pointer was passed"); 355 356 printf("sadb_key{ bits=%u reserved=%u\n", 357 key->sadb_key_bits, key->sadb_key_reserved); 358 printf(" key="); 359 360 /* sanity check 2 */ 361 if ((key->sadb_key_bits >> 3) > 362 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) { 363 printf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n", 364 key->sadb_key_bits >> 3, 365 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key)); 366 } 367 368 ipsec_hexdump((const char *)key + sizeof(struct sadb_key), 369 key->sadb_key_bits >> 3); 370 printf(" }\n"); 371 return; 372 } 373 374 static void 375 kdebug_sadb_x_sa2(const struct sadb_ext *ext) 376 { 377 const struct sadb_x_sa2 *sa2 = (const struct sadb_x_sa2 *)ext; 378 379 /* sanity check */ 380 if (ext == NULL) 381 panic("kdebug_sadb_x_sa2: NULL pointer was passed"); 382 383 printf("sadb_x_sa2{ mode=%u reqid=%u\n", 384 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid); 385 printf(" reserved1=%u reserved2=%u sequence=%u }\n", 386 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2, 387 sa2->sadb_x_sa2_sequence); 388 389 return; 390 } 391 392 void 393 kdebug_sadb_x_policy(const struct sadb_ext *ext) 394 { 395 const struct sadb_x_policy *xpl = (const struct sadb_x_policy *)ext; 396 const struct sockaddr *addr; 397 398 /* sanity check */ 399 if (ext == NULL) 400 panic("kdebug_sadb_x_policy: NULL pointer was passed"); 401 402 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n", 403 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir, 404 xpl->sadb_x_policy_id); 405 406 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) { 407 int tlen; 408 const struct sadb_x_ipsecrequest *xisr; 409 410 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl); 411 xisr = (const struct sadb_x_ipsecrequest *)(xpl + 1); 412 413 while (tlen > 0) { 414 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n", 415 xisr->sadb_x_ipsecrequest_len, 416 xisr->sadb_x_ipsecrequest_proto, 417 xisr->sadb_x_ipsecrequest_mode, 418 xisr->sadb_x_ipsecrequest_level, 419 xisr->sadb_x_ipsecrequest_reqid); 420 421 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) { 422 addr = (const struct sockaddr *)(xisr + 1); 423 kdebug_sockaddr(addr); 424 addr = (const struct sockaddr *)((const char *)addr 425 + addr->sa_len); 426 kdebug_sockaddr(addr); 427 } 428 429 printf(" }\n"); 430 431 /* prevent infinite loop */ 432 if (xisr->sadb_x_ipsecrequest_len <= 0) { 433 printf("kdebug_sadb_x_policy: wrong policy struct.\n"); 434 return; 435 } 436 /* prevent overflow */ 437 if (xisr->sadb_x_ipsecrequest_len > tlen) { 438 printf("invalid ipsec policy length\n"); 439 return; 440 } 441 442 tlen -= xisr->sadb_x_ipsecrequest_len; 443 444 xisr = (const struct sadb_x_ipsecrequest *)((const char *)xisr 445 + xisr->sadb_x_ipsecrequest_len); 446 } 447 448 if (tlen != 0) 449 panic("kdebug_sadb_x_policy: wrong policy struct"); 450 } 451 452 return; 453 } 454 455 #ifdef _KERNEL 456 /* %%%: about SPD and SAD */ 457 void 458 kdebug_secpolicy(const struct secpolicy *sp) 459 { 460 /* sanity check */ 461 if (sp == NULL) 462 panic("kdebug_secpolicy: NULL pointer was passed"); 463 464 printf("secpolicy{ refcnt=%u state=%u policy=%u\n", 465 key_sp_refcnt(sp), sp->state, sp->policy); 466 467 kdebug_secpolicyindex(&sp->spidx); 468 469 switch (sp->policy) { 470 case IPSEC_POLICY_DISCARD: 471 printf(" type=discard }\n"); 472 break; 473 case IPSEC_POLICY_NONE: 474 printf(" type=none }\n"); 475 break; 476 case IPSEC_POLICY_IPSEC: 477 { 478 struct ipsecrequest *isr; 479 for (isr = sp->req; isr != NULL; isr = isr->next) { 480 481 printf(" level=%u\n", isr->level); 482 kdebug_secasindex(&isr->saidx); 483 } 484 printf(" }\n"); 485 } 486 break; 487 case IPSEC_POLICY_BYPASS: 488 printf(" type=bypass }\n"); 489 break; 490 case IPSEC_POLICY_ENTRUST: 491 printf(" type=entrust }\n"); 492 break; 493 default: 494 printf("kdebug_secpolicy: Invalid policy found. %d\n", 495 sp->policy); 496 break; 497 } 498 499 return; 500 } 501 502 void 503 kdebug_secpolicyindex(const struct secpolicyindex *spidx) 504 { 505 /* sanity check */ 506 if (spidx == NULL) 507 panic("kdebug_secpolicyindex: NULL pointer was passed"); 508 509 printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n", 510 spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto); 511 512 ipsec_hexdump((const char *)&spidx->src, 513 spidx->src.sa.sa_len); 514 printf("\n"); 515 ipsec_hexdump((const char *)&spidx->dst, 516 spidx->dst.sa.sa_len); 517 printf("}\n"); 518 519 return; 520 } 521 522 void 523 kdebug_secasindex(const struct secasindex *saidx) 524 { 525 /* sanity check */ 526 if (saidx == NULL) 527 panic("kdebug_secpolicyindex: NULL pointer was passed"); 528 529 printf("secasindex{ mode=%u proto=%u\n", 530 saidx->mode, saidx->proto); 531 532 ipsec_hexdump((const char *)&saidx->src, 533 saidx->src.sa.sa_len); 534 printf("\n"); 535 ipsec_hexdump((const char *)&saidx->dst, 536 saidx->dst.sa.sa_len); 537 printf("\n"); 538 539 return; 540 } 541 542 void 543 kdebug_secasv(const struct secasvar *sav) 544 { 545 /* sanity check */ 546 if (sav == NULL) 547 panic("kdebug_secasv: NULL pointer was passed"); 548 549 printf("secas{"); 550 kdebug_secasindex(&sav->sah->saidx); 551 552 printf(" refcnt=%u state=%u auth=%u enc=%u\n", 553 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc); 554 printf(" spi=%u flags=%u\n", 555 (u_int32_t)ntohl(sav->spi), sav->flags); 556 557 if (sav->key_auth != NULL) 558 kdebug_sadb_key((struct sadb_ext *)sav->key_auth); 559 if (sav->key_enc != NULL) 560 kdebug_sadb_key((struct sadb_ext *)sav->key_enc); 561 562 if (sav->replay != NULL) 563 kdebug_secreplay(sav->replay); 564 if (sav->lft_c != NULL) 565 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c); 566 if (sav->lft_h != NULL) 567 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h); 568 if (sav->lft_s != NULL) 569 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s); 570 571 #if notyet 572 /* XXX: misc[123] ? */ 573 #endif 574 575 return; 576 } 577 578 static void 579 kdebug_secreplay(const struct secreplay *rpl) 580 { 581 int len, l; 582 583 /* sanity check */ 584 if (rpl == NULL) 585 panic("kdebug_secreplay: NULL pointer was passed"); 586 587 printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u", 588 rpl->count, rpl->wsize, rpl->seq, rpl->lastseq); 589 590 if (rpl->bitmap == NULL) { 591 printf(" }\n"); 592 return; 593 } 594 595 printf("\n bitmap { "); 596 597 for (len = 0; len < rpl->wsize; len++) { 598 for (l = 7; l >= 0; l--) 599 printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0); 600 } 601 printf(" }\n"); 602 603 return; 604 } 605 606 void 607 kdebug_mbufhdr(const struct mbuf *m) 608 { 609 /* sanity check */ 610 if (m == NULL) 611 return; 612 613 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p " 614 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n", 615 m, m->m_next, m->m_nextpkt, m->m_data, 616 m->m_len, m->m_type, m->m_flags); 617 618 if (m->m_flags & M_PKTHDR) { 619 printf(" m_pkthdr{ len:%d rcvif:%p }\n", 620 m->m_pkthdr.len, m_get_rcvif_NOMPSAFE(m)); 621 } 622 623 if (m->m_flags & M_EXT) { 624 printf(" m_ext{ ext_buf:%p ext_free:%p " 625 "ext_size:%zu ext_refcnt:%u }\n", 626 m->m_ext.ext_buf, m->m_ext.ext_free, 627 m->m_ext.ext_size, m->m_ext.ext_refcnt); 628 } 629 630 return; 631 } 632 633 void 634 kdebug_mbuf(const struct mbuf *m0) 635 { 636 const struct mbuf *m = m0; 637 int i, j; 638 639 for (j = 0; m; m = m->m_next) { 640 kdebug_mbufhdr(m); 641 printf(" m_data:\n"); 642 for (i = 0; i < m->m_len; i++) { 643 if (i && i % 32 == 0) 644 printf("\n"); 645 if (i % 4 == 0) 646 printf(" "); 647 printf("%02x", mtod(m, u_char *)[i]); 648 j++; 649 } 650 printf("\n"); 651 } 652 653 return; 654 } 655 #endif /* _KERNEL */ 656 657 void 658 kdebug_sockaddr(const struct sockaddr *addr) 659 { 660 const struct sockaddr_in *sin4; 661 #ifdef INET6 662 const struct sockaddr_in6 *sin6; 663 #endif 664 665 /* sanity check */ 666 if (addr == NULL) 667 panic("kdebug_sockaddr: NULL pointer was passed"); 668 669 /* NOTE: We deal with port number as host byte order. */ 670 printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family); 671 672 switch (addr->sa_family) { 673 case AF_INET: 674 sin4 = (const struct sockaddr_in *)addr; 675 printf(" port=%u\n", ntohs(sin4->sin_port)); 676 ipsec_hexdump((const char *)&sin4->sin_addr, sizeof(sin4->sin_addr)); 677 break; 678 #ifdef INET6 679 case AF_INET6: 680 sin6 = (const struct sockaddr_in6 *)addr; 681 printf(" port=%u\n", ntohs(sin6->sin6_port)); 682 printf(" flowinfo=0x%08x, scope_id=0x%08x\n", 683 sin6->sin6_flowinfo, sin6->sin6_scope_id); 684 ipsec_hexdump((const char *)&sin6->sin6_addr, sizeof(sin6->sin6_addr)); 685 break; 686 #endif 687 } 688 689 printf(" }\n"); 690 691 return; 692 } 693 694 void 695 ipsec_bindump(const char *buf, int len) 696 { 697 int i; 698 699 for (i = 0; i < len; i++) 700 printf("%c", (unsigned char)buf[i]); 701 702 return; 703 } 704 705 706 void 707 ipsec_hexdump(const char *buf, int len) 708 { 709 int i; 710 711 for (i = 0; i < len; i++) { 712 if (i != 0 && i % 32 == 0) printf("\n"); 713 if (i % 4 == 0) printf(" "); 714 printf("%02x", (unsigned char)buf[i]); 715 } 716 #if 0 717 if (i % 32 != 0) printf("\n"); 718 #endif 719 720 return; 721 } 722