Home | History | Annotate | Line # | Download | only in netipsec
key_debug.c revision 1.1
      1 /*	$NetBSD: key_debug.c,v 1.1 2003/08/13 20:06:51 jonathan 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.1 2003/08/13 20:06:51 jonathan Exp $");
     37 #endif
     38 
     39 #include "opt_inet.h"
     40 #include "opt_inet6.h"
     41 #include "opt_ipsec.h"
     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_var.h>
     55 #include <netipsec/key_debug.h>
     56 
     57 #include <netinet/in.h>
     58 #include <netipsec/ipsec.h>
     59 
     60 #ifndef _KERNEL
     61 #include <ctype.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #endif /* !_KERNEL */
     65 
     66 static void kdebug_sadb_prop __P((struct sadb_ext *));
     67 static void kdebug_sadb_identity __P((struct sadb_ext *));
     68 static void kdebug_sadb_supported __P((struct sadb_ext *));
     69 static void kdebug_sadb_lifetime __P((struct sadb_ext *));
     70 static void kdebug_sadb_sa __P((struct sadb_ext *));
     71 static void kdebug_sadb_address __P((struct sadb_ext *));
     72 static void kdebug_sadb_key __P((struct sadb_ext *));
     73 static void kdebug_sadb_x_sa2 __P((struct sadb_ext *));
     74 
     75 #ifdef _KERNEL
     76 static void kdebug_secreplay __P((struct secreplay *));
     77 #endif
     78 
     79 #ifndef _KERNEL
     80 #define panic(param)	{ printf(param); exit(-1); }
     81 #endif
     82 
     83 /* NOTE: host byte order */
     84 
     85 /* %%%: about struct sadb_msg */
     86 void
     87 kdebug_sadb(base)
     88 	struct sadb_msg *base;
     89 {
     90 	struct sadb_ext *ext;
     91 	int tlen, extlen;
     92 
     93 	/* sanity check */
     94 	if (base == NULL)
     95 		panic("kdebug_sadb: NULL pointer was passed.\n");
     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 = (struct sadb_ext *)((caddr_t)base + sizeof(struct sadb_msg));
    106 
    107 	while (tlen > 0) {
    108 		printf("sadb_ext{ len=%u type=%u }\n",
    109 		    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 = (struct sadb_ext *)((caddr_t)ext + extlen);
    169 	}
    170 
    171 	return;
    172 }
    173 
    174 static void
    175 kdebug_sadb_prop(ext)
    176 	struct sadb_ext *ext;
    177 {
    178 	struct sadb_prop *prop = (struct sadb_prop *)ext;
    179 	struct sadb_comb *comb;
    180 	int len;
    181 
    182 	/* sanity check */
    183 	if (ext == NULL)
    184 		panic("kdebug_sadb_prop: NULL pointer was passed.\n");
    185 
    186 	len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
    187 		/ sizeof(*comb);
    188 	comb = (struct sadb_comb *)(prop + 1);
    189 	printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
    190 
    191 	while (len--) {
    192 		printf("sadb_comb{ auth=%u encrypt=%u "
    193 			"flags=0x%04x reserved=0x%08x\n",
    194 			comb->sadb_comb_auth, comb->sadb_comb_encrypt,
    195 			comb->sadb_comb_flags, comb->sadb_comb_reserved);
    196 
    197 		printf("  auth_minbits=%u auth_maxbits=%u "
    198 			"encrypt_minbits=%u encrypt_maxbits=%u\n",
    199 			comb->sadb_comb_auth_minbits,
    200 			comb->sadb_comb_auth_maxbits,
    201 			comb->sadb_comb_encrypt_minbits,
    202 			comb->sadb_comb_encrypt_maxbits);
    203 
    204 		printf("  soft_alloc=%u hard_alloc=%u "
    205 			"soft_bytes=%lu hard_bytes=%lu\n",
    206 			comb->sadb_comb_soft_allocations,
    207 			comb->sadb_comb_hard_allocations,
    208 			(unsigned long)comb->sadb_comb_soft_bytes,
    209 			(unsigned long)comb->sadb_comb_hard_bytes);
    210 
    211 		printf("  soft_alloc=%lu hard_alloc=%lu "
    212 			"soft_bytes=%lu hard_bytes=%lu }\n",
    213 			(unsigned long)comb->sadb_comb_soft_addtime,
    214 			(unsigned long)comb->sadb_comb_hard_addtime,
    215 			(unsigned long)comb->sadb_comb_soft_usetime,
    216 			(unsigned long)comb->sadb_comb_hard_usetime);
    217 		comb++;
    218 	}
    219 	printf("}\n");
    220 
    221 	return;
    222 }
    223 
    224 static void
    225 kdebug_sadb_identity(ext)
    226 	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("kdebug_sadb_identity: NULL pointer was passed.\n");
    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(ext)
    269 	struct sadb_ext *ext;
    270 {
    271 	struct sadb_supported *sup = (struct sadb_supported *)ext;
    272 	struct sadb_alg *alg;
    273 	int len;
    274 
    275 	/* sanity check */
    276 	if (ext == NULL)
    277 		panic("kdebug_sadb_supported: NULL pointer was passed.\n");
    278 
    279 	len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
    280 		/ sizeof(*alg);
    281 	alg = (struct sadb_alg *)(sup + 1);
    282 	printf("sadb_sup{\n");
    283 	while (len--) {
    284 		printf("  { id=%d ivlen=%d min=%d max=%d }\n",
    285 			alg->sadb_alg_id, alg->sadb_alg_ivlen,
    286 			alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
    287 		alg++;
    288 	}
    289 	printf("}\n");
    290 
    291 	return;
    292 }
    293 
    294 static void
    295 kdebug_sadb_lifetime(ext)
    296 	struct sadb_ext *ext;
    297 {
    298 	struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
    299 
    300 	/* sanity check */
    301 	if (ext == NULL)
    302 		printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
    303 
    304 	printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
    305 		lft->sadb_lifetime_allocations,
    306 		(u_int32_t)lft->sadb_lifetime_bytes);
    307 	printf("  addtime=%u, usetime=%u }\n",
    308 		(u_int32_t)lft->sadb_lifetime_addtime,
    309 		(u_int32_t)lft->sadb_lifetime_usetime);
    310 
    311 	return;
    312 }
    313 
    314 static void
    315 kdebug_sadb_sa(ext)
    316 	struct sadb_ext *ext;
    317 {
    318 	struct sadb_sa *sa = (struct sadb_sa *)ext;
    319 
    320 	/* sanity check */
    321 	if (ext == NULL)
    322 		panic("kdebug_sadb_sa: NULL pointer was passed.\n");
    323 
    324 	printf("sadb_sa{ spi=%u replay=%u state=%u\n",
    325 	    (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
    326 	    sa->sadb_sa_state);
    327 	printf("  auth=%u encrypt=%u flags=0x%08x }\n",
    328 	    sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
    329 
    330 	return;
    331 }
    332 
    333 static void
    334 kdebug_sadb_address(ext)
    335 	struct sadb_ext *ext;
    336 {
    337 	struct sadb_address *addr = (struct sadb_address *)ext;
    338 
    339 	/* sanity check */
    340 	if (ext == NULL)
    341 		panic("kdebug_sadb_address: NULL pointer was passed.\n");
    342 
    343 	printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
    344 	    addr->sadb_address_proto, addr->sadb_address_prefixlen,
    345 	    ((u_char *)&addr->sadb_address_reserved)[0],
    346 	    ((u_char *)&addr->sadb_address_reserved)[1]);
    347 
    348 	kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr)));
    349 
    350 	return;
    351 }
    352 
    353 static void
    354 kdebug_sadb_key(ext)
    355 	struct sadb_ext *ext;
    356 {
    357 	struct sadb_key *key = (struct sadb_key *)ext;
    358 
    359 	/* sanity check */
    360 	if (ext == NULL)
    361 		panic("kdebug_sadb_key: NULL pointer was passed.\n");
    362 
    363 	printf("sadb_key{ bits=%u reserved=%u\n",
    364 	    key->sadb_key_bits, key->sadb_key_reserved);
    365 	printf("  key=");
    366 
    367 	/* sanity check 2 */
    368 	if ((key->sadb_key_bits >> 3) >
    369 		(PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
    370 		printf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
    371 			key->sadb_key_bits >> 3,
    372 			(long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
    373 	}
    374 
    375 	ipsec_hexdump((caddr_t)key + sizeof(struct sadb_key),
    376 	              key->sadb_key_bits >> 3);
    377 	printf(" }\n");
    378 	return;
    379 }
    380 
    381 static void
    382 kdebug_sadb_x_sa2(ext)
    383 	struct sadb_ext *ext;
    384 {
    385 	struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext;
    386 
    387 	/* sanity check */
    388 	if (ext == NULL)
    389 		panic("kdebug_sadb_x_sa2: NULL pointer was passed.\n");
    390 
    391 	printf("sadb_x_sa2{ mode=%u reqid=%u\n",
    392 	    sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
    393 	printf("  reserved1=%u reserved2=%u sequence=%u }\n",
    394 	    sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
    395 	    sa2->sadb_x_sa2_sequence);
    396 
    397 	return;
    398 }
    399 
    400 void
    401 kdebug_sadb_x_policy(ext)
    402 	struct sadb_ext *ext;
    403 {
    404 	struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
    405 	struct sockaddr *addr;
    406 
    407 	/* sanity check */
    408 	if (ext == NULL)
    409 		panic("kdebug_sadb_x_policy: NULL pointer was passed.\n");
    410 
    411 	printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
    412 		xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
    413 		xpl->sadb_x_policy_id);
    414 
    415 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
    416 		int tlen;
    417 		struct sadb_x_ipsecrequest *xisr;
    418 
    419 		tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
    420 		xisr = (struct sadb_x_ipsecrequest *)(xpl + 1);
    421 
    422 		while (tlen > 0) {
    423 			printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
    424 				xisr->sadb_x_ipsecrequest_len,
    425 				xisr->sadb_x_ipsecrequest_proto,
    426 				xisr->sadb_x_ipsecrequest_mode,
    427 				xisr->sadb_x_ipsecrequest_level,
    428 				xisr->sadb_x_ipsecrequest_reqid);
    429 
    430 			if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
    431 				addr = (struct sockaddr *)(xisr + 1);
    432 				kdebug_sockaddr(addr);
    433 				addr = (struct sockaddr *)((caddr_t)addr
    434 							+ addr->sa_len);
    435 				kdebug_sockaddr(addr);
    436 			}
    437 
    438 			printf(" }\n");
    439 
    440 			/* prevent infinite loop */
    441 			if (xisr->sadb_x_ipsecrequest_len <= 0) {
    442 				printf("kdebug_sadb_x_policy: wrong policy struct.\n");
    443 				return;
    444 			}
    445 			/* prevent overflow */
    446 			if (xisr->sadb_x_ipsecrequest_len > tlen) {
    447 				printf("invalid ipsec policy length\n");
    448 				return;
    449 			}
    450 
    451 			tlen -= xisr->sadb_x_ipsecrequest_len;
    452 
    453 			xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
    454 			                + xisr->sadb_x_ipsecrequest_len);
    455 		}
    456 
    457 		if (tlen != 0)
    458 			panic("kdebug_sadb_x_policy: wrong policy struct.\n");
    459 	}
    460 
    461 	return;
    462 }
    463 
    464 #ifdef _KERNEL
    465 /* %%%: about SPD and SAD */
    466 void
    467 kdebug_secpolicy(sp)
    468 	struct secpolicy *sp;
    469 {
    470 	/* sanity check */
    471 	if (sp == NULL)
    472 		panic("kdebug_secpolicy: NULL pointer was passed.\n");
    473 
    474 	printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
    475 		sp->refcnt, sp->state, sp->policy);
    476 
    477 	kdebug_secpolicyindex(&sp->spidx);
    478 
    479 	switch (sp->policy) {
    480 	case IPSEC_POLICY_DISCARD:
    481 		printf("  type=discard }\n");
    482 		break;
    483 	case IPSEC_POLICY_NONE:
    484 		printf("  type=none }\n");
    485 		break;
    486 	case IPSEC_POLICY_IPSEC:
    487 	    {
    488 		struct ipsecrequest *isr;
    489 		for (isr = sp->req; isr != NULL; isr = isr->next) {
    490 
    491 			printf("  level=%u\n", isr->level);
    492 			kdebug_secasindex(&isr->saidx);
    493 
    494 			if (isr->sav != NULL)
    495 				kdebug_secasv(isr->sav);
    496 		}
    497 		printf("  }\n");
    498 	    }
    499 		break;
    500 	case IPSEC_POLICY_BYPASS:
    501 		printf("  type=bypass }\n");
    502 		break;
    503 	case IPSEC_POLICY_ENTRUST:
    504 		printf("  type=entrust }\n");
    505 		break;
    506 	default:
    507 		printf("kdebug_secpolicy: Invalid policy found. %d\n",
    508 			sp->policy);
    509 		break;
    510 	}
    511 
    512 	return;
    513 }
    514 
    515 void
    516 kdebug_secpolicyindex(spidx)
    517 	struct secpolicyindex *spidx;
    518 {
    519 	/* sanity check */
    520 	if (spidx == NULL)
    521 		panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
    522 
    523 	printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
    524 		spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
    525 
    526 	ipsec_hexdump((caddr_t)&spidx->src,
    527 		((struct sockaddr *)&spidx->src)->sa_len);
    528 	printf("\n");
    529 	ipsec_hexdump((caddr_t)&spidx->dst,
    530 		((struct sockaddr *)&spidx->dst)->sa_len);
    531 	printf("}\n");
    532 
    533 	return;
    534 }
    535 
    536 void
    537 kdebug_secasindex(saidx)
    538 	struct secasindex *saidx;
    539 {
    540 	/* sanity check */
    541 	if (saidx == NULL)
    542 		panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
    543 
    544 	printf("secasindex{ mode=%u proto=%u\n",
    545 		saidx->mode, saidx->proto);
    546 
    547 	ipsec_hexdump((caddr_t)&saidx->src,
    548 		((struct sockaddr *)&saidx->src)->sa_len);
    549 	printf("\n");
    550 	ipsec_hexdump((caddr_t)&saidx->dst,
    551 		((struct sockaddr *)&saidx->dst)->sa_len);
    552 	printf("\n");
    553 
    554 	return;
    555 }
    556 
    557 void
    558 kdebug_secasv(sav)
    559 	struct secasvar *sav;
    560 {
    561 	/* sanity check */
    562 	if (sav == NULL)
    563 		panic("kdebug_secasv: NULL pointer was passed.\n");
    564 
    565 	printf("secas{");
    566 	kdebug_secasindex(&sav->sah->saidx);
    567 
    568 	printf("  refcnt=%u state=%u auth=%u enc=%u\n",
    569 	    sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
    570 	printf("  spi=%u flags=%u\n",
    571 	    (u_int32_t)ntohl(sav->spi), sav->flags);
    572 
    573 	if (sav->key_auth != NULL)
    574 		kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
    575 	if (sav->key_enc != NULL)
    576 		kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
    577 	if (sav->iv != NULL) {
    578 		printf("  iv=");
    579 		ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
    580 		printf("\n");
    581 	}
    582 
    583 	if (sav->replay != NULL)
    584 		kdebug_secreplay(sav->replay);
    585 	if (sav->lft_c != NULL)
    586 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
    587 	if (sav->lft_h != NULL)
    588 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
    589 	if (sav->lft_s != NULL)
    590 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
    591 
    592 #if notyet
    593 	/* XXX: misc[123] ? */
    594 #endif
    595 
    596 	return;
    597 }
    598 
    599 static void
    600 kdebug_secreplay(rpl)
    601 	struct secreplay *rpl;
    602 {
    603 	int len, l;
    604 
    605 	/* sanity check */
    606 	if (rpl == NULL)
    607 		panic("kdebug_secreplay: NULL pointer was passed.\n");
    608 
    609 	printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
    610 	    rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
    611 
    612 	if (rpl->bitmap == NULL) {
    613 		printf(" }\n");
    614 		return;
    615 	}
    616 
    617 	printf("\n   bitmap { ");
    618 
    619 	for (len = 0; len < rpl->wsize; len++) {
    620 		for (l = 7; l >= 0; l--)
    621 			printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
    622 	}
    623 	printf(" }\n");
    624 
    625 	return;
    626 }
    627 
    628 void
    629 kdebug_mbufhdr(m)
    630 	struct mbuf *m;
    631 {
    632 	/* sanity check */
    633 	if (m == NULL)
    634 		return;
    635 
    636 	printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
    637 	       "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
    638 		m, m->m_next, m->m_nextpkt, m->m_data,
    639 		m->m_len, m->m_type, m->m_flags);
    640 
    641 	if (m->m_flags & M_PKTHDR) {
    642 		printf("  m_pkthdr{ len:%d rcvif:%p }\n",
    643 		    m->m_pkthdr.len, m->m_pkthdr.rcvif);
    644 	}
    645 
    646 	if (m->m_flags & M_EXT) {
    647 #ifdef __FreeBSD__ /* mbuf differences */
    648 		printf("  m_ext{ ext_buf:%p ext_free:%p "
    649 		       "ext_size:%u ext_ref:%p }\n",
    650 			m->m_ext.ext_buf, m->m_ext.ext_free,
    651 			m->m_ext.ext_size, m->m_ext.ext_ref);
    652 #endif __FreeBSD__
    653 	}
    654 
    655 	return;
    656 }
    657 
    658 void
    659 kdebug_mbuf(m0)
    660 	struct mbuf *m0;
    661 {
    662 	struct mbuf *m = m0;
    663 	int i, j;
    664 
    665 	for (j = 0; m; m = m->m_next) {
    666 		kdebug_mbufhdr(m);
    667 		printf("  m_data:\n");
    668 		for (i = 0; i < m->m_len; i++) {
    669 			if (i && i % 32 == 0)
    670 				printf("\n");
    671 			if (i % 4 == 0)
    672 				printf(" ");
    673 			printf("%02x", mtod(m, u_char *)[i]);
    674 			j++;
    675 		}
    676 		printf("\n");
    677 	}
    678 
    679 	return;
    680 }
    681 #endif /* _KERNEL */
    682 
    683 void
    684 kdebug_sockaddr(addr)
    685 	struct sockaddr *addr;
    686 {
    687 	struct sockaddr_in *sin4;
    688 #ifdef INET6
    689 	struct sockaddr_in6 *sin6;
    690 #endif
    691 
    692 	/* sanity check */
    693 	if (addr == NULL)
    694 		panic("kdebug_sockaddr: NULL pointer was passed.\n");
    695 
    696 	/* NOTE: We deal with port number as host byte order. */
    697 	printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
    698 
    699 	switch (addr->sa_family) {
    700 	case AF_INET:
    701 		sin4 = (struct sockaddr_in *)addr;
    702 		printf(" port=%u\n", ntohs(sin4->sin_port));
    703 		ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr));
    704 		break;
    705 #ifdef INET6
    706 	case AF_INET6:
    707 		sin6 = (struct sockaddr_in6 *)addr;
    708 		printf(" port=%u\n", ntohs(sin6->sin6_port));
    709 		printf("  flowinfo=0x%08x, scope_id=0x%08x\n",
    710 		    sin6->sin6_flowinfo, sin6->sin6_scope_id);
    711 		ipsec_hexdump((caddr_t)&sin6->sin6_addr,
    712 		    sizeof(sin6->sin6_addr));
    713 		break;
    714 #endif
    715 	}
    716 
    717 	printf("  }\n");
    718 
    719 	return;
    720 }
    721 
    722 void
    723 ipsec_bindump(buf, len)
    724 	caddr_t buf;
    725 	int len;
    726 {
    727 	int i;
    728 
    729 	for (i = 0; i < len; i++)
    730 		printf("%c", (unsigned char)buf[i]);
    731 
    732 	return;
    733 }
    734 
    735 
    736 void
    737 ipsec_hexdump(buf, len)
    738 	caddr_t buf;
    739 	int len;
    740 {
    741 	int i;
    742 
    743 	for (i = 0; i < len; i++) {
    744 		if (i != 0 && i % 32 == 0) printf("\n");
    745 		if (i % 4 == 0) printf(" ");
    746 		printf("%02x", (unsigned char)buf[i]);
    747 	}
    748 #if 0
    749 	if (i % 32 != 0) printf("\n");
    750 #endif
    751 
    752 	return;
    753 }
    754