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