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