xref: /freebsd/sys/netipsec/key_debug.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
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/malloc.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 	char buf[INET6_ADDRSTRLEN];
510 
511 	/* sanity check */
512 	if (spidx == NULL)
513 		panic("%s: NULL pointer was passed.\n", __func__);
514 
515 	printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
516 		spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
517 
518 	printf("%s -> ", ipsec_address(&spidx->src, buf, sizeof(buf)));
519 	printf("%s }\n", ipsec_address(&spidx->dst, buf, sizeof(buf)));
520 }
521 
522 void
523 kdebug_secasindex(struct secasindex *saidx)
524 {
525 	char buf[INET6_ADDRSTRLEN];
526 
527 	/* sanity check */
528 	if (saidx == NULL)
529 		panic("%s: NULL pointer was passed.\n", __func__);
530 
531 	printf("secasindex{ mode=%u proto=%u\n",
532 		saidx->mode, saidx->proto);
533 
534 	printf("%s -> ", ipsec_address(&saidx->src, buf, sizeof(buf)));
535 	printf("%s }\n", ipsec_address(&saidx->dst, buf, sizeof(buf)));
536 }
537 
538 static void
539 kdebug_sec_lifetime(struct seclifetime *lft)
540 {
541 	/* sanity check */
542 	if (lft == NULL)
543 		panic("%s: NULL pointer was passed.\n", __func__);
544 
545 	printf("sec_lifetime{ alloc=%u, bytes=%u\n",
546 		lft->allocations, (u_int32_t)lft->bytes);
547 	printf("  addtime=%u, usetime=%u }\n",
548 		(u_int32_t)lft->addtime, (u_int32_t)lft->usetime);
549 
550 	return;
551 }
552 
553 void
554 kdebug_secasv(struct secasvar *sav)
555 {
556 	/* sanity check */
557 	if (sav == NULL)
558 		panic("%s: NULL pointer was passed.\n", __func__);
559 
560 	printf("secas{");
561 	kdebug_secasindex(&sav->sah->saidx);
562 
563 	printf("  refcnt=%u state=%u auth=%u enc=%u\n",
564 	    sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
565 	printf("  spi=%u flags=%u\n",
566 	    (u_int32_t)ntohl(sav->spi), sav->flags);
567 
568 	if (sav->key_auth != NULL)
569 		kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
570 	if (sav->key_enc != NULL)
571 		kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
572 
573 	if (sav->replay != NULL)
574 		kdebug_secreplay(sav->replay);
575 	if (sav->lft_c != NULL)
576 		kdebug_sec_lifetime(sav->lft_c);
577 	if (sav->lft_h != NULL)
578 		kdebug_sec_lifetime(sav->lft_h);
579 	if (sav->lft_s != NULL)
580 		kdebug_sec_lifetime(sav->lft_s);
581 
582 #ifdef notyet
583 	/* XXX: misc[123] ? */
584 #endif
585 
586 	return;
587 }
588 
589 static void
590 kdebug_secreplay(struct secreplay *rpl)
591 {
592 	int len, l;
593 
594 	/* sanity check */
595 	if (rpl == NULL)
596 		panic("%s: NULL pointer was passed.\n", __func__);
597 
598 	printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
599 	    rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
600 
601 	if (rpl->bitmap == NULL) {
602 		printf(" }\n");
603 		return;
604 	}
605 
606 	printf("\n   bitmap { ");
607 
608 	for (len = 0; len < rpl->wsize; len++) {
609 		for (l = 7; l >= 0; l--)
610 			printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
611 	}
612 	printf(" }\n");
613 
614 	return;
615 }
616 
617 void
618 kdebug_mbufhdr(const struct mbuf *m)
619 {
620 	/* sanity check */
621 	if (m == NULL)
622 		return;
623 
624 	printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
625 	       "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
626 		m, m->m_next, m->m_nextpkt, m->m_data,
627 		m->m_len, m->m_type, m->m_flags);
628 
629 	if (m->m_flags & M_PKTHDR) {
630 		printf("  m_pkthdr{ len:%d rcvif:%p }\n",
631 		    m->m_pkthdr.len, m->m_pkthdr.rcvif);
632 	}
633 
634 	if (m->m_flags & M_EXT) {
635 		printf("  m_ext{ ext_buf:%p ext_free:%p "
636 		       "ext_size:%u ext_cnt:%p }\n",
637 			m->m_ext.ext_buf, m->m_ext.ext_free,
638 			m->m_ext.ext_size, m->m_ext.ext_cnt);
639 	}
640 
641 	return;
642 }
643 
644 void
645 kdebug_mbuf(const struct mbuf *m0)
646 {
647 	const struct mbuf *m = m0;
648 	int i, j;
649 
650 	for (j = 0; m; m = m->m_next) {
651 		kdebug_mbufhdr(m);
652 		printf("  m_data:\n");
653 		for (i = 0; i < m->m_len; i++) {
654 			if (i && i % 32 == 0)
655 				printf("\n");
656 			if (i % 4 == 0)
657 				printf(" ");
658 			printf("%02x", mtod(m, const u_char *)[i]);
659 			j++;
660 		}
661 		printf("\n");
662 	}
663 
664 	return;
665 }
666 #endif /* _KERNEL */
667 
668 void
669 kdebug_sockaddr(struct sockaddr *addr)
670 {
671 	struct sockaddr_in *sin4;
672 #ifdef INET6
673 	struct sockaddr_in6 *sin6;
674 #endif
675 
676 	/* sanity check */
677 	if (addr == NULL)
678 		panic("%s: NULL pointer was passed.\n", __func__);
679 
680 	/* NOTE: We deal with port number as host byte order. */
681 	printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
682 
683 	switch (addr->sa_family) {
684 	case AF_INET:
685 		sin4 = (struct sockaddr_in *)addr;
686 		printf(" port=%u\n", ntohs(sin4->sin_port));
687 		ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr));
688 		break;
689 #ifdef INET6
690 	case AF_INET6:
691 		sin6 = (struct sockaddr_in6 *)addr;
692 		printf(" port=%u\n", ntohs(sin6->sin6_port));
693 		printf("  flowinfo=0x%08x, scope_id=0x%08x\n",
694 		    sin6->sin6_flowinfo, sin6->sin6_scope_id);
695 		ipsec_hexdump((caddr_t)&sin6->sin6_addr,
696 		    sizeof(sin6->sin6_addr));
697 		break;
698 #endif
699 	}
700 
701 	printf("  }\n");
702 
703 	return;
704 }
705 
706 void
707 ipsec_bindump(caddr_t buf, int len)
708 {
709 	int i;
710 
711 	for (i = 0; i < len; i++)
712 		printf("%c", (unsigned char)buf[i]);
713 
714 	return;
715 }
716 
717 
718 void
719 ipsec_hexdump(caddr_t buf, int len)
720 {
721 	int i;
722 
723 	for (i = 0; i < len; i++) {
724 		if (i != 0 && i % 32 == 0) printf("\n");
725 		if (i % 4 == 0) printf(" ");
726 		printf("%02x", (unsigned char)buf[i]);
727 	}
728 #if 0
729 	if (i % 32 != 0) printf("\n");
730 #endif
731 
732 	return;
733 }
734