xref: /netbsd-src/sys/netipsec/key_debug.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: key_debug.c,v 1.11 2011/05/23 15:17:25 drochner 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.11 2011/05/23 15:17:25 drochner Exp $");
37 #endif
38 
39 #include "opt_inet.h"
40 #ifdef __FreeBSD__
41 #include "opt_inet6.h"
42 #endif
43 #include "opt_ipsec.h"
44 
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #ifdef _KERNEL
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/queue.h>
51 #endif
52 #include <sys/socket.h>
53 
54 #include <net/route.h>
55 
56 #include <netipsec/key_var.h>
57 #include <netipsec/key_debug.h>
58 
59 #include <netinet/in.h>
60 #include <netipsec/ipsec.h>
61 
62 #ifndef _KERNEL
63 #include <ctype.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #endif /* !_KERNEL */
67 
68 static void kdebug_sadb_prop (const struct sadb_ext *);
69 static void kdebug_sadb_identity (const struct sadb_ext *);
70 static void kdebug_sadb_supported (const struct sadb_ext *);
71 static void kdebug_sadb_lifetime (const struct sadb_ext *);
72 static void kdebug_sadb_sa (const struct sadb_ext *);
73 static void kdebug_sadb_address (const struct sadb_ext *);
74 static void kdebug_sadb_key (const struct sadb_ext *);
75 static void kdebug_sadb_x_sa2 (const struct sadb_ext *);
76 
77 #ifdef _KERNEL
78 static void kdebug_secreplay (const struct secreplay *);
79 #endif
80 
81 #ifndef _KERNEL
82 #define panic(param)	{ printf(param); exit(-1); }
83 #endif
84 
85 /* NOTE: host byte order */
86 
87 /* %%%: about struct sadb_msg */
88 void
89 kdebug_sadb(const struct sadb_msg *base)
90 {
91 	const struct sadb_ext *ext;
92 	int tlen, extlen;
93 
94 	/* sanity check */
95 	if (base == NULL)
96 		panic("kdebug_sadb: NULL pointer was passed");
97 
98 	printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
99 	    base->sadb_msg_version, base->sadb_msg_type,
100 	    base->sadb_msg_errno, base->sadb_msg_satype);
101 	printf("  len=%u reserved=%u seq=%u pid=%u\n",
102 	    base->sadb_msg_len, base->sadb_msg_reserved,
103 	    base->sadb_msg_seq, base->sadb_msg_pid);
104 
105 	tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
106 	ext = (const struct sadb_ext *)((const char *)base + sizeof(struct sadb_msg));
107 
108 	while (tlen > 0) {
109 		printf("sadb_ext{ len=%u type=%u }\n",
110 		    ext->sadb_ext_len, ext->sadb_ext_type);
111 
112 		if (ext->sadb_ext_len == 0) {
113 			printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
114 			return;
115 		}
116 		if (ext->sadb_ext_len > tlen) {
117 			printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
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("kdebug_sadb: invalid ext_type %u was passed.\n",
163 			    ext->sadb_ext_type);
164 			return;
165 		}
166 
167 		extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
168 		tlen -= extlen;
169 		ext = (const struct sadb_ext *)((const char *)ext + extlen);
170 	}
171 
172 	return;
173 }
174 
175 static void
176 kdebug_sadb_prop(const struct sadb_ext *ext)
177 {
178 	const struct sadb_prop *prop = (const struct sadb_prop *)ext;
179 	const struct sadb_comb *comb;
180 	int len;
181 
182 	/* sanity check */
183 	if (ext == NULL)
184 		panic("kdebug_sadb_prop: NULL pointer was passed");
185 
186 	len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
187 		/ sizeof(*comb);
188 	comb = (const 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(const struct sadb_ext *ext)
226 {
227 	const struct sadb_ident *id = (const struct sadb_ident *)ext;
228 	int len;
229 
230 	/* sanity check */
231 	if (ext == NULL)
232 		panic("kdebug_sadb_identity: NULL pointer was passed");
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((const char *)(id + 1), len); /*XXX cast ?*/
244 #else
245 			const char *p, *ep;
246 			printf("\n  str=\"");
247 			p = (const 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(const struct sadb_ext *ext)
268 {
269 	const struct sadb_supported *sup = (const struct sadb_supported *)ext;
270 	const struct sadb_alg *alg;
271 	int len;
272 
273 	/* sanity check */
274 	if (ext == NULL)
275 		panic("kdebug_sadb_supported: NULL pointer was passed");
276 
277 	len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
278 		/ sizeof(*alg);
279 	alg = (const 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(const struct sadb_ext *ext)
294 {
295 	const struct sadb_lifetime *lft = (const struct sadb_lifetime *)ext;
296 
297 	/* sanity check */
298 	if (ext == NULL)
299 		printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
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(const struct sadb_ext *ext)
313 {
314 	const struct sadb_sa *sa = (const struct sadb_sa *)ext;
315 
316 	/* sanity check */
317 	if (ext == NULL)
318 		panic("kdebug_sadb_sa: NULL pointer was passed");
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(const struct sadb_ext *ext)
331 {
332 	const struct sadb_address *addr = (const struct sadb_address *)ext;
333 
334 	/* sanity check */
335 	if (ext == NULL)
336 		panic("kdebug_sadb_address: NULL pointer was passed");
337 
338 	printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
339 	    addr->sadb_address_proto, addr->sadb_address_prefixlen,
340 	    ((const u_char *)&addr->sadb_address_reserved)[0],
341 	    ((const u_char *)&addr->sadb_address_reserved)[1]);
342 
343 	kdebug_sockaddr((const struct sockaddr *)((const char *)ext + sizeof(*addr)));
344 
345 	return;
346 }
347 
348 static void
349 kdebug_sadb_key(const struct sadb_ext *ext)
350 {
351 	const struct sadb_key *key = (const struct sadb_key *)ext;
352 
353 	/* sanity check */
354 	if (ext == NULL)
355 		panic("kdebug_sadb_key: NULL pointer was passed");
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("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
365 			key->sadb_key_bits >> 3,
366 			(long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
367 	}
368 
369 	ipsec_hexdump((const char *)key + sizeof(struct sadb_key),
370 	              key->sadb_key_bits >> 3);
371 	printf(" }\n");
372 	return;
373 }
374 
375 static void
376 kdebug_sadb_x_sa2(const struct sadb_ext *ext)
377 {
378 	const struct sadb_x_sa2 *sa2 = (const struct sadb_x_sa2 *)ext;
379 
380 	/* sanity check */
381 	if (ext == NULL)
382 		panic("kdebug_sadb_x_sa2: NULL pointer was passed");
383 
384 	printf("sadb_x_sa2{ mode=%u reqid=%u\n",
385 	    sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
386 	printf("  reserved1=%u reserved2=%u sequence=%u }\n",
387 	    sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
388 	    sa2->sadb_x_sa2_sequence);
389 
390 	return;
391 }
392 
393 void
394 kdebug_sadb_x_policy(const struct sadb_ext *ext)
395 {
396 	const struct sadb_x_policy *xpl = (const struct sadb_x_policy *)ext;
397 	const struct sockaddr *addr;
398 
399 	/* sanity check */
400 	if (ext == NULL)
401 		panic("kdebug_sadb_x_policy: NULL pointer was passed");
402 
403 	printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
404 		xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
405 		xpl->sadb_x_policy_id);
406 
407 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
408 		int tlen;
409 		const struct sadb_x_ipsecrequest *xisr;
410 
411 		tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
412 		xisr = (const struct sadb_x_ipsecrequest *)(xpl + 1);
413 
414 		while (tlen > 0) {
415 			printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
416 				xisr->sadb_x_ipsecrequest_len,
417 				xisr->sadb_x_ipsecrequest_proto,
418 				xisr->sadb_x_ipsecrequest_mode,
419 				xisr->sadb_x_ipsecrequest_level,
420 				xisr->sadb_x_ipsecrequest_reqid);
421 
422 			if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
423 				addr = (const struct sockaddr *)(xisr + 1);
424 				kdebug_sockaddr(addr);
425 				addr = (const struct sockaddr *)((const char *)addr
426 							+ addr->sa_len);
427 				kdebug_sockaddr(addr);
428 			}
429 
430 			printf(" }\n");
431 
432 			/* prevent infinite loop */
433 			if (xisr->sadb_x_ipsecrequest_len <= 0) {
434 				printf("kdebug_sadb_x_policy: wrong policy struct.\n");
435 				return;
436 			}
437 			/* prevent overflow */
438 			if (xisr->sadb_x_ipsecrequest_len > tlen) {
439 				printf("invalid ipsec policy length\n");
440 				return;
441 			}
442 
443 			tlen -= xisr->sadb_x_ipsecrequest_len;
444 
445 			xisr = (const struct sadb_x_ipsecrequest *)((const char *)xisr
446 			                + xisr->sadb_x_ipsecrequest_len);
447 		}
448 
449 		if (tlen != 0)
450 			panic("kdebug_sadb_x_policy: wrong policy struct");
451 	}
452 
453 	return;
454 }
455 
456 #ifdef _KERNEL
457 /* %%%: about SPD and SAD */
458 void
459 kdebug_secpolicy(const struct secpolicy *sp)
460 {
461 	/* sanity check */
462 	if (sp == NULL)
463 		panic("kdebug_secpolicy: NULL pointer was passed");
464 
465 	printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
466 		sp->refcnt, sp->state, sp->policy);
467 
468 	kdebug_secpolicyindex(&sp->spidx);
469 
470 	switch (sp->policy) {
471 	case IPSEC_POLICY_DISCARD:
472 		printf("  type=discard }\n");
473 		break;
474 	case IPSEC_POLICY_NONE:
475 		printf("  type=none }\n");
476 		break;
477 	case IPSEC_POLICY_IPSEC:
478 	    {
479 		struct ipsecrequest *isr;
480 		for (isr = sp->req; isr != NULL; isr = isr->next) {
481 
482 			printf("  level=%u\n", isr->level);
483 			kdebug_secasindex(&isr->saidx);
484 
485 			if (isr->sav != NULL)
486 				kdebug_secasv(isr->sav);
487 		}
488 		printf("  }\n");
489 	    }
490 		break;
491 	case IPSEC_POLICY_BYPASS:
492 		printf("  type=bypass }\n");
493 		break;
494 	case IPSEC_POLICY_ENTRUST:
495 		printf("  type=entrust }\n");
496 		break;
497 	default:
498 		printf("kdebug_secpolicy: Invalid policy found. %d\n",
499 			sp->policy);
500 		break;
501 	}
502 
503 	return;
504 }
505 
506 void
507 kdebug_secpolicyindex(const struct secpolicyindex *spidx)
508 {
509 	/* sanity check */
510 	if (spidx == NULL)
511 		panic("kdebug_secpolicyindex: NULL pointer was passed");
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((const char *)&spidx->src,
517 		spidx->src.sa.sa_len);
518 	printf("\n");
519 	ipsec_hexdump((const char *)&spidx->dst,
520 		spidx->dst.sa.sa_len);
521 	printf("}\n");
522 
523 	return;
524 }
525 
526 void
527 kdebug_secasindex(const struct secasindex *saidx)
528 {
529 	/* sanity check */
530 	if (saidx == NULL)
531 		panic("kdebug_secpolicyindex: NULL pointer was passed");
532 
533 	printf("secasindex{ mode=%u proto=%u\n",
534 		saidx->mode, saidx->proto);
535 
536 	ipsec_hexdump((const char *)&saidx->src,
537 		saidx->src.sa.sa_len);
538 	printf("\n");
539 	ipsec_hexdump((const char *)&saidx->dst,
540 		saidx->dst.sa.sa_len);
541 	printf("\n");
542 
543 	return;
544 }
545 
546 void
547 kdebug_secasv(const struct secasvar *sav)
548 {
549 	/* sanity check */
550 	if (sav == NULL)
551 		panic("kdebug_secasv: NULL pointer was passed");
552 
553 	printf("secas{");
554 	kdebug_secasindex(&sav->sah->saidx);
555 
556 	printf("  refcnt=%u state=%u auth=%u enc=%u\n",
557 	    sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
558 	printf("  spi=%u flags=%u\n",
559 	    (u_int32_t)ntohl(sav->spi), sav->flags);
560 
561 	if (sav->key_auth != NULL)
562 		kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
563 	if (sav->key_enc != NULL)
564 		kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
565 
566 	if (sav->replay != NULL)
567 		kdebug_secreplay(sav->replay);
568 	if (sav->lft_c != NULL)
569 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
570 	if (sav->lft_h != NULL)
571 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
572 	if (sav->lft_s != NULL)
573 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
574 
575 #if notyet
576 	/* XXX: misc[123] ? */
577 #endif
578 
579 	return;
580 }
581 
582 static void
583 kdebug_secreplay(const struct secreplay *rpl)
584 {
585 	int len, l;
586 
587 	/* sanity check */
588 	if (rpl == NULL)
589 		panic("kdebug_secreplay: NULL pointer was passed");
590 
591 	printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
592 	    rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
593 
594 	if (rpl->bitmap == NULL) {
595 		printf(" }\n");
596 		return;
597 	}
598 
599 	printf("\n   bitmap { ");
600 
601 	for (len = 0; len < rpl->wsize; len++) {
602 		for (l = 7; l >= 0; l--)
603 			printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
604 	}
605 	printf(" }\n");
606 
607 	return;
608 }
609 
610 void
611 kdebug_mbufhdr(const struct mbuf *m)
612 {
613 	/* sanity check */
614 	if (m == NULL)
615 		return;
616 
617 	printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
618 	       "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
619 		m, m->m_next, m->m_nextpkt, m->m_data,
620 		m->m_len, m->m_type, m->m_flags);
621 
622 	if (m->m_flags & M_PKTHDR) {
623 		printf("  m_pkthdr{ len:%d rcvif:%p }\n",
624 		    m->m_pkthdr.len, m->m_pkthdr.rcvif);
625 	}
626 
627 	if (m->m_flags & M_EXT) {
628 #ifdef __FreeBSD__ /* mbuf differences */
629 		printf("  m_ext{ ext_buf:%p ext_free:%p "
630 		       "ext_size:%u ext_ref:%p }\n",
631 			m->m_ext.ext_buf, m->m_ext.ext_free,
632 			m->m_ext.ext_size, m->m_ext.ext_ref);
633 #endif /* __FreeBSD__ */
634 	}
635 
636 	return;
637 }
638 
639 void
640 kdebug_mbuf(const struct mbuf *m0)
641 {
642 	const struct mbuf *m = m0;
643 	int i, j;
644 
645 	for (j = 0; m; m = m->m_next) {
646 		kdebug_mbufhdr(m);
647 		printf("  m_data:\n");
648 		for (i = 0; i < m->m_len; i++) {
649 			if (i && i % 32 == 0)
650 				printf("\n");
651 			if (i % 4 == 0)
652 				printf(" ");
653 			printf("%02x", mtod(m, u_char *)[i]);
654 			j++;
655 		}
656 		printf("\n");
657 	}
658 
659 	return;
660 }
661 #endif /* _KERNEL */
662 
663 void
664 kdebug_sockaddr(const struct sockaddr *addr)
665 {
666 	const struct sockaddr_in *sin4;
667 #ifdef INET6
668 	const struct sockaddr_in6 *sin6;
669 #endif
670 
671 	/* sanity check */
672 	if (addr == NULL)
673 		panic("kdebug_sockaddr: NULL pointer was passed");
674 
675 	/* NOTE: We deal with port number as host byte order. */
676 	printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
677 
678 	switch (addr->sa_family) {
679 	case AF_INET:
680 		sin4 = (const struct sockaddr_in *)addr;
681 		printf(" port=%u\n", ntohs(sin4->sin_port));
682 		ipsec_hexdump((const char *)&sin4->sin_addr, sizeof(sin4->sin_addr));
683 		break;
684 #ifdef INET6
685 	case AF_INET6:
686 		sin6 = (const struct sockaddr_in6 *)addr;
687 		printf(" port=%u\n", ntohs(sin6->sin6_port));
688 		printf("  flowinfo=0x%08x, scope_id=0x%08x\n",
689 		    sin6->sin6_flowinfo, sin6->sin6_scope_id);
690 		ipsec_hexdump((const char *)&sin6->sin6_addr, sizeof(sin6->sin6_addr));
691 		break;
692 #endif
693 	}
694 
695 	printf("  }\n");
696 
697 	return;
698 }
699 
700 void
701 ipsec_bindump(const char *buf, int len)
702 {
703 	int i;
704 
705 	for (i = 0; i < len; i++)
706 		printf("%c", (unsigned char)buf[i]);
707 
708 	return;
709 }
710 
711 
712 void
713 ipsec_hexdump(const char *buf, int len)
714 {
715 	int i;
716 
717 	for (i = 0; i < len; i++) {
718 		if (i != 0 && i % 32 == 0) printf("\n");
719 		if (i % 4 == 0) printf(" ");
720 		printf("%02x", (unsigned char)buf[i]);
721 	}
722 #if 0
723 	if (i % 32 != 0) printf("\n");
724 #endif
725 
726 	return;
727 }
728