xref: /freebsd/sys/netipsec/key_debug.c (revision 792bbaba989533a1fc93823df1720c8c4aaf0442)
1 /*	$FreeBSD$	*/
2 /*	$KAME: key_debug.c,v 1.26 2001/06/27 10:46:50 sakane Exp $	*/
3 
4 /*-
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifdef _KERNEL
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
37 #endif
38 
39 #include <sys/param.h>
40 #ifdef _KERNEL
41 #include <sys/systm.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/mutex.h>
46 #include <sys/queue.h>
47 #endif
48 #include <sys/socket.h>
49 
50 #include <net/vnet.h>
51 
52 #include <netipsec/key_var.h>
53 #include <netipsec/key_debug.h>
54 
55 #include <netinet/in.h>
56 #include <netipsec/ipsec.h>
57 #ifdef _KERNEL
58 #include <netipsec/keydb.h>
59 #endif
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(struct sadb_ext *);
68 static void kdebug_sadb_identity(struct sadb_ext *);
69 static void kdebug_sadb_supported(struct sadb_ext *);
70 static void kdebug_sadb_lifetime(struct sadb_ext *);
71 static void kdebug_sadb_sa(struct sadb_ext *);
72 static void kdebug_sadb_address(struct sadb_ext *);
73 static void kdebug_sadb_key(struct sadb_ext *);
74 static void kdebug_sadb_x_sa2(struct sadb_ext *);
75 
76 #ifdef _KERNEL
77 static void kdebug_secreplay(struct secreplay *);
78 #endif
79 
80 #ifndef _KERNEL
81 #define panic(fmt, ...)	{ printf(fmt, ## __VA_ARGS__); exit(-1); }
82 #endif
83 
84 /* NOTE: host byte order */
85 
86 /* %%%: about struct sadb_msg */
87 void
88 kdebug_sadb(struct sadb_msg *base)
89 {
90 	struct sadb_ext *ext;
91 	int tlen, extlen;
92 
93 	/* sanity check */
94 	if (base == NULL)
95 		panic("%s: NULL pointer was passed.\n", __func__);
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("%s: invalid ext_len=0 was passed.\n", __func__);
113 			return;
114 		}
115 		if (ext->sadb_ext_len > tlen) {
116 			printf("%s: ext_len too big (%u > %u).\n",
117 				__func__, ext->sadb_ext_len, tlen);
118 			return;
119 		}
120 
121 		switch (ext->sadb_ext_type) {
122 		case SADB_EXT_SA:
123 			kdebug_sadb_sa(ext);
124 			break;
125 		case SADB_EXT_LIFETIME_CURRENT:
126 		case SADB_EXT_LIFETIME_HARD:
127 		case SADB_EXT_LIFETIME_SOFT:
128 			kdebug_sadb_lifetime(ext);
129 			break;
130 		case SADB_EXT_ADDRESS_SRC:
131 		case SADB_EXT_ADDRESS_DST:
132 		case SADB_EXT_ADDRESS_PROXY:
133 			kdebug_sadb_address(ext);
134 			break;
135 		case SADB_EXT_KEY_AUTH:
136 		case SADB_EXT_KEY_ENCRYPT:
137 			kdebug_sadb_key(ext);
138 			break;
139 		case SADB_EXT_IDENTITY_SRC:
140 		case SADB_EXT_IDENTITY_DST:
141 			kdebug_sadb_identity(ext);
142 			break;
143 		case SADB_EXT_SENSITIVITY:
144 			break;
145 		case SADB_EXT_PROPOSAL:
146 			kdebug_sadb_prop(ext);
147 			break;
148 		case SADB_EXT_SUPPORTED_AUTH:
149 		case SADB_EXT_SUPPORTED_ENCRYPT:
150 			kdebug_sadb_supported(ext);
151 			break;
152 		case SADB_EXT_SPIRANGE:
153 		case SADB_X_EXT_KMPRIVATE:
154 			break;
155 		case SADB_X_EXT_POLICY:
156 			kdebug_sadb_x_policy(ext);
157 			break;
158 		case SADB_X_EXT_SA2:
159 			kdebug_sadb_x_sa2(ext);
160 			break;
161 		default:
162 			printf("%s: invalid ext_type %u\n", __func__,
163 			    ext->sadb_ext_type);
164 			return;
165 		}
166 
167 		extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
168 		tlen -= extlen;
169 		ext = (struct sadb_ext *)((caddr_t)ext + extlen);
170 	}
171 
172 	return;
173 }
174 
175 static void
176 kdebug_sadb_prop(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("%s: NULL pointer was passed.\n", __func__);
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(struct sadb_ext *ext)
226 {
227 	struct sadb_ident *id = (struct sadb_ident *)ext;
228 	int len;
229 
230 	/* sanity check */
231 	if (ext == NULL)
232 		panic("%s: NULL pointer was passed.\n", __func__);
233 
234 	len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
235 	printf("sadb_ident_%s{",
236 	    id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
237 	switch (id->sadb_ident_type) {
238 	default:
239 		printf(" type=%d id=%lu",
240 			id->sadb_ident_type, (u_long)id->sadb_ident_id);
241 		if (len) {
242 #ifdef _KERNEL
243 			ipsec_hexdump((caddr_t)(id + 1), len); /*XXX cast ?*/
244 #else
245 			char *p, *ep;
246 			printf("\n  str=\"");
247 			p = (char *)(id + 1);
248 			ep = p + len;
249 			for (/*nothing*/; *p && p < ep; p++) {
250 				if (isprint(*p))
251 					printf("%c", *p & 0xff);
252 				else
253 					printf("\\%03o", *p & 0xff);
254 			}
255 #endif
256 			printf("\"");
257 		}
258 		break;
259 	}
260 
261 	printf(" }\n");
262 
263 	return;
264 }
265 
266 static void
267 kdebug_sadb_supported(struct sadb_ext *ext)
268 {
269 	struct sadb_supported *sup = (struct sadb_supported *)ext;
270 	struct sadb_alg *alg;
271 	int len;
272 
273 	/* sanity check */
274 	if (ext == NULL)
275 		panic("%s: NULL pointer was passed.\n", __func__);
276 
277 	len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
278 		/ sizeof(*alg);
279 	alg = (struct sadb_alg *)(sup + 1);
280 	printf("sadb_sup{\n");
281 	while (len--) {
282 		printf("  { id=%d ivlen=%d min=%d max=%d }\n",
283 			alg->sadb_alg_id, alg->sadb_alg_ivlen,
284 			alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
285 		alg++;
286 	}
287 	printf("}\n");
288 
289 	return;
290 }
291 
292 static void
293 kdebug_sadb_lifetime(struct sadb_ext *ext)
294 {
295 	struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
296 
297 	/* sanity check */
298 	if (ext == NULL)
299 		panic("%s: NULL pointer was passed.\n", __func__);
300 
301 	printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
302 		lft->sadb_lifetime_allocations,
303 		(u_int32_t)lft->sadb_lifetime_bytes);
304 	printf("  addtime=%u, usetime=%u }\n",
305 		(u_int32_t)lft->sadb_lifetime_addtime,
306 		(u_int32_t)lft->sadb_lifetime_usetime);
307 
308 	return;
309 }
310 
311 static void
312 kdebug_sadb_sa(struct sadb_ext *ext)
313 {
314 	struct sadb_sa *sa = (struct sadb_sa *)ext;
315 
316 	/* sanity check */
317 	if (ext == NULL)
318 		panic("%s: NULL pointer was passed.\n", __func__);
319 
320 	printf("sadb_sa{ spi=%u replay=%u state=%u\n",
321 	    (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
322 	    sa->sadb_sa_state);
323 	printf("  auth=%u encrypt=%u flags=0x%08x }\n",
324 	    sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
325 
326 	return;
327 }
328 
329 static void
330 kdebug_sadb_address(struct sadb_ext *ext)
331 {
332 	struct sadb_address *addr = (struct sadb_address *)ext;
333 
334 	/* sanity check */
335 	if (ext == NULL)
336 		panic("%s: NULL pointer was passed.\n", __func__);
337 
338 	printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
339 	    addr->sadb_address_proto, addr->sadb_address_prefixlen,
340 	    ((u_char *)&addr->sadb_address_reserved)[0],
341 	    ((u_char *)&addr->sadb_address_reserved)[1]);
342 
343 	kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr)));
344 
345 	return;
346 }
347 
348 static void
349 kdebug_sadb_key(struct sadb_ext *ext)
350 {
351 	struct sadb_key *key = (struct sadb_key *)ext;
352 
353 	/* sanity check */
354 	if (ext == NULL)
355 		panic("%s: NULL pointer was passed.\n", __func__);
356 
357 	printf("sadb_key{ bits=%u reserved=%u\n",
358 	    key->sadb_key_bits, key->sadb_key_reserved);
359 	printf("  key=");
360 
361 	/* sanity check 2 */
362 	if ((key->sadb_key_bits >> 3) >
363 		(PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
364 		printf("%s: key length mismatch, bit:%d len:%ld.\n",
365 			__func__,
366 			key->sadb_key_bits >> 3,
367 			(long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
368 	}
369 
370 	ipsec_hexdump((caddr_t)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(struct sadb_ext *ext)
378 {
379 	struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext;
380 
381 	/* sanity check */
382 	if (ext == NULL)
383 		panic("%s: NULL pointer was passed.\n", __func__);
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(struct sadb_ext *ext)
396 {
397 	struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
398 	struct sockaddr *addr;
399 
400 	/* sanity check */
401 	if (ext == NULL)
402 		panic("%s: NULL pointer was passed.\n", __func__);
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 		struct sadb_x_ipsecrequest *xisr;
411 
412 		tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
413 		xisr = (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 = (struct sockaddr *)(xisr + 1);
425 				kdebug_sockaddr(addr);
426 				addr = (struct sockaddr *)((caddr_t)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("%s: wrong policy struct.\n", __func__);
436 				return;
437 			}
438 			/* prevent overflow */
439 			if (xisr->sadb_x_ipsecrequest_len > tlen) {
440 				printf("%s: invalid ipsec policy length "
441 					"(%u > %u)\n", __func__,
442 					xisr->sadb_x_ipsecrequest_len, tlen);
443 				return;
444 			}
445 
446 			tlen -= xisr->sadb_x_ipsecrequest_len;
447 
448 			xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
449 			                + xisr->sadb_x_ipsecrequest_len);
450 		}
451 
452 		if (tlen != 0)
453 			panic("%s: wrong policy struct.\n", __func__);
454 	}
455 
456 	return;
457 }
458 
459 #ifdef _KERNEL
460 /* %%%: about SPD and SAD */
461 void
462 kdebug_secpolicy(struct secpolicy *sp)
463 {
464 	/* sanity check */
465 	if (sp == NULL)
466 		panic("%s: NULL pointer was passed.\n", __func__);
467 
468 	printf("secpolicy{ refcnt=%u policy=%u\n",
469 		sp->refcnt, sp->policy);
470 
471 	kdebug_secpolicyindex(&sp->spidx);
472 
473 	switch (sp->policy) {
474 	case IPSEC_POLICY_DISCARD:
475 		printf("  type=discard }\n");
476 		break;
477 	case IPSEC_POLICY_NONE:
478 		printf("  type=none }\n");
479 		break;
480 	case IPSEC_POLICY_IPSEC:
481 	    {
482 		struct ipsecrequest *isr;
483 		for (isr = sp->req; isr != NULL; isr = isr->next) {
484 
485 			printf("  level=%u\n", isr->level);
486 			kdebug_secasindex(&isr->saidx);
487 
488 			if (isr->sav != NULL)
489 				kdebug_secasv(isr->sav);
490 		}
491 		printf("  }\n");
492 	    }
493 		break;
494 	case IPSEC_POLICY_BYPASS:
495 		printf("  type=bypass }\n");
496 		break;
497 	case IPSEC_POLICY_ENTRUST:
498 		printf("  type=entrust }\n");
499 		break;
500 	default:
501 		printf("%s: Invalid policy found. %d\n", __func__, sp->policy);
502 		break;
503 	}
504 
505 	return;
506 }
507 
508 void
509 kdebug_secpolicyindex(struct secpolicyindex *spidx)
510 {
511 	char buf[INET6_ADDRSTRLEN];
512 
513 	/* sanity check */
514 	if (spidx == NULL)
515 		panic("%s: NULL pointer was passed.\n", __func__);
516 
517 	printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
518 		spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
519 
520 	printf("%s -> ", ipsec_address(&spidx->src, buf, sizeof(buf)));
521 	printf("%s }\n", ipsec_address(&spidx->dst, buf, sizeof(buf)));
522 }
523 
524 void
525 kdebug_secasindex(struct secasindex *saidx)
526 {
527 	char buf[INET6_ADDRSTRLEN];
528 
529 	/* sanity check */
530 	if (saidx == NULL)
531 		panic("%s: NULL pointer was passed.\n", __func__);
532 
533 	printf("secasindex{ mode=%u proto=%u\n",
534 		saidx->mode, saidx->proto);
535 
536 	printf("%s -> ", ipsec_address(&saidx->src, buf, sizeof(buf)));
537 	printf("%s }\n", ipsec_address(&saidx->dst, buf, sizeof(buf)));
538 }
539 
540 static void
541 kdebug_sec_lifetime(struct seclifetime *lft)
542 {
543 	/* sanity check */
544 	if (lft == NULL)
545 		panic("%s: NULL pointer was passed.\n", __func__);
546 
547 	printf("sec_lifetime{ alloc=%u, bytes=%u\n",
548 		lft->allocations, (u_int32_t)lft->bytes);
549 	printf("  addtime=%u, usetime=%u }\n",
550 		(u_int32_t)lft->addtime, (u_int32_t)lft->usetime);
551 
552 	return;
553 }
554 
555 void
556 kdebug_secasv(struct secasvar *sav)
557 {
558 	/* sanity check */
559 	if (sav == NULL)
560 		panic("%s: NULL pointer was passed.\n", __func__);
561 
562 	printf("secas{");
563 	kdebug_secasindex(&sav->sah->saidx);
564 
565 	printf("  refcnt=%u state=%u auth=%u enc=%u\n",
566 	    sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
567 	printf("  spi=%u flags=%u\n",
568 	    (u_int32_t)ntohl(sav->spi), sav->flags);
569 
570 	if (sav->key_auth != NULL)
571 		kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
572 	if (sav->key_enc != NULL)
573 		kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
574 
575 	if (sav->replay != NULL) {
576 		SECASVAR_LOCK(sav);
577 		kdebug_secreplay(sav->replay);
578 		SECASVAR_UNLOCK(sav);
579 	}
580 	if (sav->lft_c != NULL)
581 		kdebug_sec_lifetime(sav->lft_c);
582 	if (sav->lft_h != NULL)
583 		kdebug_sec_lifetime(sav->lft_h);
584 	if (sav->lft_s != NULL)
585 		kdebug_sec_lifetime(sav->lft_s);
586 
587 #ifdef notyet
588 	/* XXX: misc[123] ? */
589 #endif
590 
591 	return;
592 }
593 
594 static void
595 kdebug_secreplay(struct secreplay *rpl)
596 {
597 	int len, l;
598 
599 	/* sanity check */
600 	if (rpl == NULL)
601 		panic("%s: NULL pointer was passed.\n", __func__);
602 
603 	printf(" secreplay{ count=%u bitmap_size=%u wsize=%u seq=%u lastseq=%u",
604 	    rpl->count, rpl->bitmap_size, rpl->wsize, rpl->seq, rpl->lastseq);
605 
606 	if (rpl->bitmap == NULL) {
607 		printf(" }\n");
608 		return;
609 	}
610 
611 	printf("\n   bitmap { ");
612 
613 	for (len = 0; len < rpl->bitmap_size*4; len++) {
614 		for (l = 7; l >= 0; l--)
615 			printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
616 	}
617 	printf(" }\n");
618 
619 	return;
620 }
621 
622 void
623 kdebug_mbufhdr(const struct mbuf *m)
624 {
625 	/* sanity check */
626 	if (m == NULL)
627 		return;
628 
629 	printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
630 	       "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
631 		m, m->m_next, m->m_nextpkt, m->m_data,
632 		m->m_len, m->m_type, m->m_flags);
633 
634 	if (m->m_flags & M_PKTHDR) {
635 		printf("  m_pkthdr{ len:%d rcvif:%p }\n",
636 		    m->m_pkthdr.len, m->m_pkthdr.rcvif);
637 	}
638 
639 	if (m->m_flags & M_EXT) {
640 		printf("  m_ext{ ext_buf:%p ext_free:%p "
641 		       "ext_size:%u ext_cnt:%p }\n",
642 			m->m_ext.ext_buf, m->m_ext.ext_free,
643 			m->m_ext.ext_size, m->m_ext.ext_cnt);
644 	}
645 
646 	return;
647 }
648 
649 void
650 kdebug_mbuf(const struct mbuf *m0)
651 {
652 	const struct mbuf *m = m0;
653 	int i, j;
654 
655 	for (j = 0; m; m = m->m_next) {
656 		kdebug_mbufhdr(m);
657 		printf("  m_data:\n");
658 		for (i = 0; i < m->m_len; i++) {
659 			if (i && i % 32 == 0)
660 				printf("\n");
661 			if (i % 4 == 0)
662 				printf(" ");
663 			printf("%02x", mtod(m, const u_char *)[i]);
664 			j++;
665 		}
666 		printf("\n");
667 	}
668 
669 	return;
670 }
671 #endif /* _KERNEL */
672 
673 void
674 kdebug_sockaddr(struct sockaddr *addr)
675 {
676 	struct sockaddr_in *sin4;
677 #ifdef INET6
678 	struct sockaddr_in6 *sin6;
679 #endif
680 
681 	/* sanity check */
682 	if (addr == NULL)
683 		panic("%s: NULL pointer was passed.\n", __func__);
684 
685 	/* NOTE: We deal with port number as host byte order. */
686 	printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
687 
688 	switch (addr->sa_family) {
689 	case AF_INET:
690 		sin4 = (struct sockaddr_in *)addr;
691 		printf(" port=%u\n", ntohs(sin4->sin_port));
692 		ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr));
693 		break;
694 #ifdef INET6
695 	case AF_INET6:
696 		sin6 = (struct sockaddr_in6 *)addr;
697 		printf(" port=%u\n", ntohs(sin6->sin6_port));
698 		printf("  flowinfo=0x%08x, scope_id=0x%08x\n",
699 		    sin6->sin6_flowinfo, sin6->sin6_scope_id);
700 		ipsec_hexdump((caddr_t)&sin6->sin6_addr,
701 		    sizeof(sin6->sin6_addr));
702 		break;
703 #endif
704 	}
705 
706 	printf("  }\n");
707 
708 	return;
709 }
710 
711 void
712 ipsec_bindump(caddr_t buf, int len)
713 {
714 	int i;
715 
716 	for (i = 0; i < len; i++)
717 		printf("%c", (unsigned char)buf[i]);
718 
719 	return;
720 }
721 
722 
723 void
724 ipsec_hexdump(caddr_t buf, int len)
725 {
726 	int i;
727 
728 	for (i = 0; i < len; i++) {
729 		if (i != 0 && i % 32 == 0) printf("\n");
730 		if (i % 4 == 0) printf(" ");
731 		printf("%02x", (unsigned char)buf[i]);
732 	}
733 #if 0
734 	if (i % 32 != 0) printf("\n");
735 #endif
736 
737 	return;
738 }
739