Home | History | Annotate | Line # | Download | only in netipsec
key_debug.c revision 1.14
      1 /*	$NetBSD: key_debug.c,v 1.14 2017/04/06 09:20:07 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.14 2017/04/06 09:20:07 ozaki-r Exp $");
     37 #endif
     38 
     39 #if defined(_KERNEL_OPT)
     40 #include "opt_inet.h"
     41 #ifdef __FreeBSD__
     42 #include "opt_inet6.h"
     43 #endif
     44 #endif
     45 
     46 #include <sys/types.h>
     47 #include <sys/param.h>
     48 #ifdef _KERNEL
     49 #include <sys/systm.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/queue.h>
     52 #endif
     53 #include <sys/socket.h>
     54 
     55 #include <net/route.h>
     56 
     57 #include <netipsec/key_var.h>
     58 #include <netipsec/key_debug.h>
     59 
     60 #include <netinet/in.h>
     61 #include <netipsec/ipsec.h>
     62 
     63 #ifndef _KERNEL
     64 #include <ctype.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #endif /* !_KERNEL */
     68 
     69 static void kdebug_sadb_prop (const struct sadb_ext *);
     70 static void kdebug_sadb_identity (const struct sadb_ext *);
     71 static void kdebug_sadb_supported (const struct sadb_ext *);
     72 static void kdebug_sadb_lifetime (const struct sadb_ext *);
     73 static void kdebug_sadb_sa (const struct sadb_ext *);
     74 static void kdebug_sadb_address (const struct sadb_ext *);
     75 static void kdebug_sadb_key (const struct sadb_ext *);
     76 static void kdebug_sadb_x_sa2 (const struct sadb_ext *);
     77 
     78 #ifdef _KERNEL
     79 static void kdebug_secreplay (const struct secreplay *);
     80 #endif
     81 
     82 #ifndef _KERNEL
     83 #define panic(param)	{ printf(param); exit(-1); }
     84 #endif
     85 
     86 /* NOTE: host byte order */
     87 
     88 /* %%%: about struct sadb_msg */
     89 void
     90 kdebug_sadb(const struct sadb_msg *base)
     91 {
     92 	const struct sadb_ext *ext;
     93 	int tlen, extlen;
     94 
     95 	/* sanity check */
     96 	if (base == NULL)
     97 		panic("kdebug_sadb: NULL pointer was passed");
     98 
     99 	printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
    100 	    base->sadb_msg_version, base->sadb_msg_type,
    101 	    base->sadb_msg_errno, base->sadb_msg_satype);
    102 	printf("  len=%u reserved=%u seq=%u pid=%u\n",
    103 	    base->sadb_msg_len, base->sadb_msg_reserved,
    104 	    base->sadb_msg_seq, base->sadb_msg_pid);
    105 
    106 	tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
    107 	ext = (const struct sadb_ext *)((const char *)base + sizeof(struct sadb_msg));
    108 
    109 	while (tlen > 0) {
    110 		printf("sadb_ext{ len=%u type=%u }\n",
    111 		    ext->sadb_ext_len, ext->sadb_ext_type);
    112 
    113 		if (ext->sadb_ext_len == 0) {
    114 			printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
    115 			return;
    116 		}
    117 		if (ext->sadb_ext_len > tlen) {
    118 			printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
    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("kdebug_sadb: invalid ext_type %u was passed.\n",
    164 			    ext->sadb_ext_type);
    165 			return;
    166 		}
    167 
    168 		extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
    169 		tlen -= extlen;
    170 		ext = (const struct sadb_ext *)((const char *)ext + extlen);
    171 	}
    172 
    173 	return;
    174 }
    175 
    176 static void
    177 kdebug_sadb_prop(const struct sadb_ext *ext)
    178 {
    179 	const struct sadb_prop *prop = (const struct sadb_prop *)ext;
    180 	const struct sadb_comb *comb;
    181 	int len;
    182 
    183 	/* sanity check */
    184 	if (ext == NULL)
    185 		panic("kdebug_sadb_prop: NULL pointer was passed");
    186 
    187 	len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
    188 		/ sizeof(*comb);
    189 	comb = (const 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(const struct sadb_ext *ext)
    227 {
    228 	const struct sadb_ident *id = (const struct sadb_ident *)ext;
    229 	int len;
    230 
    231 	/* sanity check */
    232 	if (ext == NULL)
    233 		panic("kdebug_sadb_identity: NULL pointer was passed");
    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((const char *)(id + 1), len); /*XXX cast ?*/
    245 #else
    246 			const char *p, *ep;
    247 			printf("\n  str=\"");
    248 			p = (const 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(const struct sadb_ext *ext)
    269 {
    270 	const struct sadb_supported *sup = (const struct sadb_supported *)ext;
    271 	const struct sadb_alg *alg;
    272 	int len;
    273 
    274 	/* sanity check */
    275 	if (ext == NULL)
    276 		panic("kdebug_sadb_supported: NULL pointer was passed");
    277 
    278 	len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
    279 		/ sizeof(*alg);
    280 	alg = (const 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(const struct sadb_ext *ext)
    295 {
    296 	const struct sadb_lifetime *lft = (const struct sadb_lifetime *)ext;
    297 
    298 	/* sanity check */
    299 	if (ext == NULL)
    300 		printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
    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(const struct sadb_ext *ext)
    314 {
    315 	const struct sadb_sa *sa = (const struct sadb_sa *)ext;
    316 
    317 	/* sanity check */
    318 	if (ext == NULL)
    319 		panic("kdebug_sadb_sa: NULL pointer was passed");
    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(const struct sadb_ext *ext)
    332 {
    333 	const struct sadb_address *addr = (const struct sadb_address *)ext;
    334 
    335 	/* sanity check */
    336 	if (ext == NULL)
    337 		panic("kdebug_sadb_address: NULL pointer was passed");
    338 
    339 	printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
    340 	    addr->sadb_address_proto, addr->sadb_address_prefixlen,
    341 	    ((const u_char *)&addr->sadb_address_reserved)[0],
    342 	    ((const u_char *)&addr->sadb_address_reserved)[1]);
    343 
    344 	kdebug_sockaddr((const struct sockaddr *)((const char *)ext + sizeof(*addr)));
    345 
    346 	return;
    347 }
    348 
    349 static void
    350 kdebug_sadb_key(const struct sadb_ext *ext)
    351 {
    352 	const struct sadb_key *key = (const struct sadb_key *)ext;
    353 
    354 	/* sanity check */
    355 	if (ext == NULL)
    356 		panic("kdebug_sadb_key: NULL pointer was passed");
    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("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
    366 			key->sadb_key_bits >> 3,
    367 			(long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
    368 	}
    369 
    370 	ipsec_hexdump((const char *)key + sizeof(struct sadb_key),
    371 	              key->sadb_key_bits >> 3);
    372 	printf(" }\n");
    373 	return;
    374 }
    375 
    376 static void
    377 kdebug_sadb_x_sa2(const struct sadb_ext *ext)
    378 {
    379 	const struct sadb_x_sa2 *sa2 = (const struct sadb_x_sa2 *)ext;
    380 
    381 	/* sanity check */
    382 	if (ext == NULL)
    383 		panic("kdebug_sadb_x_sa2: NULL pointer was passed");
    384 
    385 	printf("sadb_x_sa2{ mode=%u reqid=%u\n",
    386 	    sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
    387 	printf("  reserved1=%u reserved2=%u sequence=%u }\n",
    388 	    sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
    389 	    sa2->sadb_x_sa2_sequence);
    390 
    391 	return;
    392 }
    393 
    394 void
    395 kdebug_sadb_x_policy(const struct sadb_ext *ext)
    396 {
    397 	const struct sadb_x_policy *xpl = (const struct sadb_x_policy *)ext;
    398 	const struct sockaddr *addr;
    399 
    400 	/* sanity check */
    401 	if (ext == NULL)
    402 		panic("kdebug_sadb_x_policy: NULL pointer was passed");
    403 
    404 	printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
    405 		xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
    406 		xpl->sadb_x_policy_id);
    407 
    408 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
    409 		int tlen;
    410 		const struct sadb_x_ipsecrequest *xisr;
    411 
    412 		tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
    413 		xisr = (const struct sadb_x_ipsecrequest *)(xpl + 1);
    414 
    415 		while (tlen > 0) {
    416 			printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
    417 				xisr->sadb_x_ipsecrequest_len,
    418 				xisr->sadb_x_ipsecrequest_proto,
    419 				xisr->sadb_x_ipsecrequest_mode,
    420 				xisr->sadb_x_ipsecrequest_level,
    421 				xisr->sadb_x_ipsecrequest_reqid);
    422 
    423 			if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
    424 				addr = (const struct sockaddr *)(xisr + 1);
    425 				kdebug_sockaddr(addr);
    426 				addr = (const struct sockaddr *)((const char *)addr
    427 							+ addr->sa_len);
    428 				kdebug_sockaddr(addr);
    429 			}
    430 
    431 			printf(" }\n");
    432 
    433 			/* prevent infinite loop */
    434 			if (xisr->sadb_x_ipsecrequest_len <= 0) {
    435 				printf("kdebug_sadb_x_policy: wrong policy struct.\n");
    436 				return;
    437 			}
    438 			/* prevent overflow */
    439 			if (xisr->sadb_x_ipsecrequest_len > tlen) {
    440 				printf("invalid ipsec policy length\n");
    441 				return;
    442 			}
    443 
    444 			tlen -= xisr->sadb_x_ipsecrequest_len;
    445 
    446 			xisr = (const struct sadb_x_ipsecrequest *)((const char *)xisr
    447 			                + xisr->sadb_x_ipsecrequest_len);
    448 		}
    449 
    450 		if (tlen != 0)
    451 			panic("kdebug_sadb_x_policy: wrong policy struct");
    452 	}
    453 
    454 	return;
    455 }
    456 
    457 #ifdef _KERNEL
    458 /* %%%: about SPD and SAD */
    459 void
    460 kdebug_secpolicy(const struct secpolicy *sp)
    461 {
    462 	/* sanity check */
    463 	if (sp == NULL)
    464 		panic("kdebug_secpolicy: NULL pointer was passed");
    465 
    466 	printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
    467 		sp->refcnt, sp->state, sp->policy);
    468 
    469 	kdebug_secpolicyindex(&sp->spidx);
    470 
    471 	switch (sp->policy) {
    472 	case IPSEC_POLICY_DISCARD:
    473 		printf("  type=discard }\n");
    474 		break;
    475 	case IPSEC_POLICY_NONE:
    476 		printf("  type=none }\n");
    477 		break;
    478 	case IPSEC_POLICY_IPSEC:
    479 	    {
    480 		struct ipsecrequest *isr;
    481 		for (isr = sp->req; isr != NULL; isr = isr->next) {
    482 
    483 			printf("  level=%u\n", isr->level);
    484 			kdebug_secasindex(&isr->saidx);
    485 
    486 			if (isr->sav != NULL)
    487 				kdebug_secasv(isr->sav);
    488 		}
    489 		printf("  }\n");
    490 	    }
    491 		break;
    492 	case IPSEC_POLICY_BYPASS:
    493 		printf("  type=bypass }\n");
    494 		break;
    495 	case IPSEC_POLICY_ENTRUST:
    496 		printf("  type=entrust }\n");
    497 		break;
    498 	default:
    499 		printf("kdebug_secpolicy: Invalid policy found. %d\n",
    500 			sp->policy);
    501 		break;
    502 	}
    503 
    504 	return;
    505 }
    506 
    507 void
    508 kdebug_secpolicyindex(const struct secpolicyindex *spidx)
    509 {
    510 	/* sanity check */
    511 	if (spidx == NULL)
    512 		panic("kdebug_secpolicyindex: NULL pointer was passed");
    513 
    514 	printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
    515 		spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
    516 
    517 	ipsec_hexdump((const char *)&spidx->src,
    518 		spidx->src.sa.sa_len);
    519 	printf("\n");
    520 	ipsec_hexdump((const char *)&spidx->dst,
    521 		spidx->dst.sa.sa_len);
    522 	printf("}\n");
    523 
    524 	return;
    525 }
    526 
    527 void
    528 kdebug_secasindex(const struct secasindex *saidx)
    529 {
    530 	/* sanity check */
    531 	if (saidx == NULL)
    532 		panic("kdebug_secpolicyindex: NULL pointer was passed");
    533 
    534 	printf("secasindex{ mode=%u proto=%u\n",
    535 		saidx->mode, saidx->proto);
    536 
    537 	ipsec_hexdump((const char *)&saidx->src,
    538 		saidx->src.sa.sa_len);
    539 	printf("\n");
    540 	ipsec_hexdump((const char *)&saidx->dst,
    541 		saidx->dst.sa.sa_len);
    542 	printf("\n");
    543 
    544 	return;
    545 }
    546 
    547 void
    548 kdebug_secasv(const struct secasvar *sav)
    549 {
    550 	/* sanity check */
    551 	if (sav == NULL)
    552 		panic("kdebug_secasv: NULL pointer was passed");
    553 
    554 	printf("secas{");
    555 	kdebug_secasindex(&sav->sah->saidx);
    556 
    557 	printf("  refcnt=%u state=%u auth=%u enc=%u\n",
    558 	    sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
    559 	printf("  spi=%u flags=%u\n",
    560 	    (u_int32_t)ntohl(sav->spi), sav->flags);
    561 
    562 	if (sav->key_auth != NULL)
    563 		kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
    564 	if (sav->key_enc != NULL)
    565 		kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
    566 
    567 	if (sav->replay != NULL)
    568 		kdebug_secreplay(sav->replay);
    569 	if (sav->lft_c != NULL)
    570 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
    571 	if (sav->lft_h != NULL)
    572 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
    573 	if (sav->lft_s != NULL)
    574 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
    575 
    576 #if notyet
    577 	/* XXX: misc[123] ? */
    578 #endif
    579 
    580 	return;
    581 }
    582 
    583 static void
    584 kdebug_secreplay(const struct secreplay *rpl)
    585 {
    586 	int len, l;
    587 
    588 	/* sanity check */
    589 	if (rpl == NULL)
    590 		panic("kdebug_secreplay: NULL pointer was passed");
    591 
    592 	printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
    593 	    rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
    594 
    595 	if (rpl->bitmap == NULL) {
    596 		printf(" }\n");
    597 		return;
    598 	}
    599 
    600 	printf("\n   bitmap { ");
    601 
    602 	for (len = 0; len < rpl->wsize; len++) {
    603 		for (l = 7; l >= 0; l--)
    604 			printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
    605 	}
    606 	printf(" }\n");
    607 
    608 	return;
    609 }
    610 
    611 void
    612 kdebug_mbufhdr(const struct mbuf *m)
    613 {
    614 	/* sanity check */
    615 	if (m == NULL)
    616 		return;
    617 
    618 	printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
    619 	       "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
    620 		m, m->m_next, m->m_nextpkt, m->m_data,
    621 		m->m_len, m->m_type, m->m_flags);
    622 
    623 	if (m->m_flags & M_PKTHDR) {
    624 		printf("  m_pkthdr{ len:%d rcvif:%p }\n",
    625 		    m->m_pkthdr.len, m_get_rcvif_NOMPSAFE(m));
    626 	}
    627 
    628 	if (m->m_flags & M_EXT) {
    629 #ifdef __FreeBSD__ /* mbuf differences */
    630 		printf("  m_ext{ ext_buf:%p ext_free:%p "
    631 		       "ext_size:%u ext_ref:%p }\n",
    632 			m->m_ext.ext_buf, m->m_ext.ext_free,
    633 			m->m_ext.ext_size, m->m_ext.ext_ref);
    634 #endif /* __FreeBSD__ */
    635 	}
    636 
    637 	return;
    638 }
    639 
    640 void
    641 kdebug_mbuf(const struct mbuf *m0)
    642 {
    643 	const struct mbuf *m = m0;
    644 	int i, j;
    645 
    646 	for (j = 0; m; m = m->m_next) {
    647 		kdebug_mbufhdr(m);
    648 		printf("  m_data:\n");
    649 		for (i = 0; i < m->m_len; i++) {
    650 			if (i && i % 32 == 0)
    651 				printf("\n");
    652 			if (i % 4 == 0)
    653 				printf(" ");
    654 			printf("%02x", mtod(m, u_char *)[i]);
    655 			j++;
    656 		}
    657 		printf("\n");
    658 	}
    659 
    660 	return;
    661 }
    662 #endif /* _KERNEL */
    663 
    664 void
    665 kdebug_sockaddr(const struct sockaddr *addr)
    666 {
    667 	const struct sockaddr_in *sin4;
    668 #ifdef INET6
    669 	const struct sockaddr_in6 *sin6;
    670 #endif
    671 
    672 	/* sanity check */
    673 	if (addr == NULL)
    674 		panic("kdebug_sockaddr: NULL pointer was passed");
    675 
    676 	/* NOTE: We deal with port number as host byte order. */
    677 	printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
    678 
    679 	switch (addr->sa_family) {
    680 	case AF_INET:
    681 		sin4 = (const struct sockaddr_in *)addr;
    682 		printf(" port=%u\n", ntohs(sin4->sin_port));
    683 		ipsec_hexdump((const char *)&sin4->sin_addr, sizeof(sin4->sin_addr));
    684 		break;
    685 #ifdef INET6
    686 	case AF_INET6:
    687 		sin6 = (const struct sockaddr_in6 *)addr;
    688 		printf(" port=%u\n", ntohs(sin6->sin6_port));
    689 		printf("  flowinfo=0x%08x, scope_id=0x%08x\n",
    690 		    sin6->sin6_flowinfo, sin6->sin6_scope_id);
    691 		ipsec_hexdump((const char *)&sin6->sin6_addr, sizeof(sin6->sin6_addr));
    692 		break;
    693 #endif
    694 	}
    695 
    696 	printf("  }\n");
    697 
    698 	return;
    699 }
    700 
    701 void
    702 ipsec_bindump(const char *buf, int len)
    703 {
    704 	int i;
    705 
    706 	for (i = 0; i < len; i++)
    707 		printf("%c", (unsigned char)buf[i]);
    708 
    709 	return;
    710 }
    711 
    712 
    713 void
    714 ipsec_hexdump(const char *buf, int len)
    715 {
    716 	int i;
    717 
    718 	for (i = 0; i < len; i++) {
    719 		if (i != 0 && i % 32 == 0) printf("\n");
    720 		if (i % 4 == 0) printf(" ");
    721 		printf("%02x", (unsigned char)buf[i]);
    722 	}
    723 #if 0
    724 	if (i % 32 != 0) printf("\n");
    725 #endif
    726 
    727 	return;
    728 }
    729