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