xref: /freebsd/sys/netipsec/key_debug.c (revision 9034852c84a13f0e3b5527e1c886ca94b2863b2b)
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/vnet.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 #ifdef _KERNEL
56 #include <netipsec/keydb.h>
57 #endif
58 
59 #ifndef _KERNEL
60 #include <ctype.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #endif /* !_KERNEL */
64 
65 static void kdebug_sadb_prop(struct sadb_ext *);
66 static void kdebug_sadb_identity(struct sadb_ext *);
67 static void kdebug_sadb_supported(struct sadb_ext *);
68 static void kdebug_sadb_lifetime(struct sadb_ext *);
69 static void kdebug_sadb_sa(struct sadb_ext *);
70 static void kdebug_sadb_address(struct sadb_ext *);
71 static void kdebug_sadb_key(struct sadb_ext *);
72 static void kdebug_sadb_x_sa2(struct sadb_ext *);
73 
74 #ifdef _KERNEL
75 static void kdebug_secreplay(struct secreplay *);
76 #endif
77 
78 #ifndef _KERNEL
79 #define panic(fmt, ...)	{ printf(fmt, ## __VA_ARGS__); exit(-1); }
80 #endif
81 
82 /* NOTE: host byte order */
83 
84 /* %%%: about struct sadb_msg */
85 void
86 kdebug_sadb(struct sadb_msg *base)
87 {
88 	struct sadb_ext *ext;
89 	int tlen, extlen;
90 
91 	/* sanity check */
92 	if (base == NULL)
93 		panic("%s: NULL pointer was passed.\n", __func__);
94 
95 	printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
96 	    base->sadb_msg_version, base->sadb_msg_type,
97 	    base->sadb_msg_errno, base->sadb_msg_satype);
98 	printf("  len=%u reserved=%u seq=%u pid=%u\n",
99 	    base->sadb_msg_len, base->sadb_msg_reserved,
100 	    base->sadb_msg_seq, base->sadb_msg_pid);
101 
102 	tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
103 	ext = (struct sadb_ext *)((caddr_t)base + sizeof(struct sadb_msg));
104 
105 	while (tlen > 0) {
106 		printf("sadb_ext{ len=%u type=%u }\n",
107 		    ext->sadb_ext_len, ext->sadb_ext_type);
108 
109 		if (ext->sadb_ext_len == 0) {
110 			printf("%s: invalid ext_len=0 was passed.\n", __func__);
111 			return;
112 		}
113 		if (ext->sadb_ext_len > tlen) {
114 			printf("%s: ext_len too big (%u > %u).\n",
115 				__func__, ext->sadb_ext_len, tlen);
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("%s: invalid ext_type %u\n", __func__,
161 			    ext->sadb_ext_type);
162 			return;
163 		}
164 
165 		extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
166 		tlen -= extlen;
167 		ext = (struct sadb_ext *)((caddr_t)ext + extlen);
168 	}
169 
170 	return;
171 }
172 
173 static void
174 kdebug_sadb_prop(struct sadb_ext *ext)
175 {
176 	struct sadb_prop *prop = (struct sadb_prop *)ext;
177 	struct sadb_comb *comb;
178 	int len;
179 
180 	/* sanity check */
181 	if (ext == NULL)
182 		panic("%s: NULL pointer was passed.\n", __func__);
183 
184 	len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
185 		/ sizeof(*comb);
186 	comb = (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(struct sadb_ext *ext)
224 {
225 	struct sadb_ident *id = (struct sadb_ident *)ext;
226 	int len;
227 
228 	/* sanity check */
229 	if (ext == NULL)
230 		panic("%s: NULL pointer was passed.\n", __func__);
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((caddr_t)(id + 1), len); /*XXX cast ?*/
242 #else
243 			char *p, *ep;
244 			printf("\n  str=\"");
245 			p = (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(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("%s: NULL pointer was passed.\n", __func__);
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(struct sadb_ext *ext)
292 {
293 	struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
294 
295 	/* sanity check */
296 	if (ext == NULL)
297 		panic("%s: NULL pointer was passed.\n", __func__);
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(struct sadb_ext *ext)
311 {
312 	struct sadb_sa *sa = (struct sadb_sa *)ext;
313 
314 	/* sanity check */
315 	if (ext == NULL)
316 		panic("%s: NULL pointer was passed.\n", __func__);
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(struct sadb_ext *ext)
329 {
330 	struct sadb_address *addr = (struct sadb_address *)ext;
331 
332 	/* sanity check */
333 	if (ext == NULL)
334 		panic("%s: NULL pointer was passed.\n", __func__);
335 
336 	printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
337 	    addr->sadb_address_proto, addr->sadb_address_prefixlen,
338 	    ((u_char *)&addr->sadb_address_reserved)[0],
339 	    ((u_char *)&addr->sadb_address_reserved)[1]);
340 
341 	kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr)));
342 
343 	return;
344 }
345 
346 static void
347 kdebug_sadb_key(struct sadb_ext *ext)
348 {
349 	struct sadb_key *key = (struct sadb_key *)ext;
350 
351 	/* sanity check */
352 	if (ext == NULL)
353 		panic("%s: NULL pointer was passed.\n", __func__);
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("%s: key length mismatch, bit:%d len:%ld.\n",
363 			__func__,
364 			key->sadb_key_bits >> 3,
365 			(long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
366 	}
367 
368 	ipsec_hexdump((caddr_t)key + sizeof(struct sadb_key),
369 	              key->sadb_key_bits >> 3);
370 	printf(" }\n");
371 	return;
372 }
373 
374 static void
375 kdebug_sadb_x_sa2(struct sadb_ext *ext)
376 {
377 	struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext;
378 
379 	/* sanity check */
380 	if (ext == NULL)
381 		panic("%s: NULL pointer was passed.\n", __func__);
382 
383 	printf("sadb_x_sa2{ mode=%u reqid=%u\n",
384 	    sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
385 	printf("  reserved1=%u reserved2=%u sequence=%u }\n",
386 	    sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
387 	    sa2->sadb_x_sa2_sequence);
388 
389 	return;
390 }
391 
392 void
393 kdebug_sadb_x_policy(struct sadb_ext *ext)
394 {
395 	struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
396 	struct sockaddr *addr;
397 
398 	/* sanity check */
399 	if (ext == NULL)
400 		panic("%s: NULL pointer was passed.\n", __func__);
401 
402 	printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
403 		xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
404 		xpl->sadb_x_policy_id);
405 
406 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
407 		int tlen;
408 		struct sadb_x_ipsecrequest *xisr;
409 
410 		tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
411 		xisr = (struct sadb_x_ipsecrequest *)(xpl + 1);
412 
413 		while (tlen > 0) {
414 			printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
415 				xisr->sadb_x_ipsecrequest_len,
416 				xisr->sadb_x_ipsecrequest_proto,
417 				xisr->sadb_x_ipsecrequest_mode,
418 				xisr->sadb_x_ipsecrequest_level,
419 				xisr->sadb_x_ipsecrequest_reqid);
420 
421 			if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
422 				addr = (struct sockaddr *)(xisr + 1);
423 				kdebug_sockaddr(addr);
424 				addr = (struct sockaddr *)((caddr_t)addr
425 							+ addr->sa_len);
426 				kdebug_sockaddr(addr);
427 			}
428 
429 			printf(" }\n");
430 
431 			/* prevent infinite loop */
432 			if (xisr->sadb_x_ipsecrequest_len <= 0) {
433 				printf("%s: wrong policy struct.\n", __func__);
434 				return;
435 			}
436 			/* prevent overflow */
437 			if (xisr->sadb_x_ipsecrequest_len > tlen) {
438 				printf("%s: invalid ipsec policy length "
439 					"(%u > %u)\n", __func__,
440 					xisr->sadb_x_ipsecrequest_len, tlen);
441 				return;
442 			}
443 
444 			tlen -= xisr->sadb_x_ipsecrequest_len;
445 
446 			xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
447 			                + xisr->sadb_x_ipsecrequest_len);
448 		}
449 
450 		if (tlen != 0)
451 			panic("%s: wrong policy struct.\n", __func__);
452 	}
453 
454 	return;
455 }
456 
457 #ifdef _KERNEL
458 /* %%%: about SPD and SAD */
459 void
460 kdebug_secpolicy(struct secpolicy *sp)
461 {
462 	/* sanity check */
463 	if (sp == NULL)
464 		panic("%s: NULL pointer was passed.\n", __func__);
465 
466 	printf("secpolicy{ refcnt=%u policy=%u\n",
467 		sp->refcnt, 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("%s: Invalid policy found. %d\n", __func__, sp->policy);
500 		break;
501 	}
502 
503 	return;
504 }
505 
506 void
507 kdebug_secpolicyindex(struct secpolicyindex *spidx)
508 {
509 	/* sanity check */
510 	if (spidx == NULL)
511 		panic("%s: NULL pointer was passed.\n", __func__);
512 
513 	printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
514 		spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
515 
516 	ipsec_hexdump((caddr_t)&spidx->src,
517 		((struct sockaddr *)&spidx->src)->sa_len);
518 	printf("\n");
519 	ipsec_hexdump((caddr_t)&spidx->dst,
520 		((struct sockaddr *)&spidx->dst)->sa_len);
521 	printf("}\n");
522 
523 	return;
524 }
525 
526 void
527 kdebug_secasindex(struct secasindex *saidx)
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 	ipsec_hexdump((caddr_t)&saidx->src,
537 		((struct sockaddr *)&saidx->src)->sa_len);
538 	printf("\n");
539 	ipsec_hexdump((caddr_t)&saidx->dst,
540 		((struct sockaddr *)&saidx->dst)->sa_len);
541 	printf("\n");
542 
543 	return;
544 }
545 
546 static void
547 kdebug_sec_lifetime(struct seclifetime *lft)
548 {
549 	/* sanity check */
550 	if (lft == NULL)
551 		panic("%s: NULL pointer was passed.\n", __func__);
552 
553 	printf("sec_lifetime{ alloc=%u, bytes=%u\n",
554 		lft->allocations, (u_int32_t)lft->bytes);
555 	printf("  addtime=%u, usetime=%u }\n",
556 		(u_int32_t)lft->addtime, (u_int32_t)lft->usetime);
557 
558 	return;
559 }
560 
561 void
562 kdebug_secasv(struct secasvar *sav)
563 {
564 	/* sanity check */
565 	if (sav == NULL)
566 		panic("%s: NULL pointer was passed.\n", __func__);
567 
568 	printf("secas{");
569 	kdebug_secasindex(&sav->sah->saidx);
570 
571 	printf("  refcnt=%u state=%u auth=%u enc=%u\n",
572 	    sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
573 	printf("  spi=%u flags=%u\n",
574 	    (u_int32_t)ntohl(sav->spi), sav->flags);
575 
576 	if (sav->key_auth != NULL)
577 		kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
578 	if (sav->key_enc != NULL)
579 		kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
580 
581 	if (sav->replay != NULL)
582 		kdebug_secreplay(sav->replay);
583 	if (sav->lft_c != NULL)
584 		kdebug_sec_lifetime(sav->lft_c);
585 	if (sav->lft_h != NULL)
586 		kdebug_sec_lifetime(sav->lft_h);
587 	if (sav->lft_s != NULL)
588 		kdebug_sec_lifetime(sav->lft_s);
589 
590 #ifdef notyet
591 	/* XXX: misc[123] ? */
592 #endif
593 
594 	return;
595 }
596 
597 static void
598 kdebug_secreplay(struct secreplay *rpl)
599 {
600 	int len, l;
601 
602 	/* sanity check */
603 	if (rpl == NULL)
604 		panic("%s: NULL pointer was passed.\n", __func__);
605 
606 	printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
607 	    rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
608 
609 	if (rpl->bitmap == NULL) {
610 		printf(" }\n");
611 		return;
612 	}
613 
614 	printf("\n   bitmap { ");
615 
616 	for (len = 0; len < rpl->wsize; len++) {
617 		for (l = 7; l >= 0; l--)
618 			printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
619 	}
620 	printf(" }\n");
621 
622 	return;
623 }
624 
625 void
626 kdebug_mbufhdr(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 ext_cnt:%p }\n",
645 			m->m_ext.ext_buf, m->m_ext.ext_free,
646 			m->m_ext.ext_size, m->m_ext.ext_cnt);
647 	}
648 
649 	return;
650 }
651 
652 void
653 kdebug_mbuf(struct mbuf *m0)
654 {
655 	struct mbuf *m = m0;
656 	int i, j;
657 
658 	for (j = 0; m; m = m->m_next) {
659 		kdebug_mbufhdr(m);
660 		printf("  m_data:\n");
661 		for (i = 0; i < m->m_len; i++) {
662 			if (i && i % 32 == 0)
663 				printf("\n");
664 			if (i % 4 == 0)
665 				printf(" ");
666 			printf("%02x", mtod(m, u_char *)[i]);
667 			j++;
668 		}
669 		printf("\n");
670 	}
671 
672 	return;
673 }
674 #endif /* _KERNEL */
675 
676 void
677 kdebug_sockaddr(struct sockaddr *addr)
678 {
679 	struct sockaddr_in *sin4;
680 #ifdef INET6
681 	struct sockaddr_in6 *sin6;
682 #endif
683 
684 	/* sanity check */
685 	if (addr == NULL)
686 		panic("%s: NULL pointer was passed.\n", __func__);
687 
688 	/* NOTE: We deal with port number as host byte order. */
689 	printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
690 
691 	switch (addr->sa_family) {
692 	case AF_INET:
693 		sin4 = (struct sockaddr_in *)addr;
694 		printf(" port=%u\n", ntohs(sin4->sin_port));
695 		ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr));
696 		break;
697 #ifdef INET6
698 	case AF_INET6:
699 		sin6 = (struct sockaddr_in6 *)addr;
700 		printf(" port=%u\n", ntohs(sin6->sin6_port));
701 		printf("  flowinfo=0x%08x, scope_id=0x%08x\n",
702 		    sin6->sin6_flowinfo, sin6->sin6_scope_id);
703 		ipsec_hexdump((caddr_t)&sin6->sin6_addr,
704 		    sizeof(sin6->sin6_addr));
705 		break;
706 #endif
707 	}
708 
709 	printf("  }\n");
710 
711 	return;
712 }
713 
714 void
715 ipsec_bindump(caddr_t buf, int len)
716 {
717 	int i;
718 
719 	for (i = 0; i < len; i++)
720 		printf("%c", (unsigned char)buf[i]);
721 
722 	return;
723 }
724 
725 
726 void
727 ipsec_hexdump(caddr_t buf, int len)
728 {
729 	int i;
730 
731 	for (i = 0; i < len; i++) {
732 		if (i != 0 && i % 32 == 0) printf("\n");
733 		if (i % 4 == 0) printf(" ");
734 		printf("%02x", (unsigned char)buf[i]);
735 	}
736 #if 0
737 	if (i % 32 != 0) printf("\n");
738 #endif
739 
740 	return;
741 }
742