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