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