Sync usage with man page.
[netbsd-mini2440.git] / crypto / dist / ipsec-tools / src / libipsec / key_debug.c
bloba31bbff47f9b294db1a34301bbd31fc9d5749dbd
1 /* $NetBSD: key_debug.c,v 1.8 2007/07/18 12:07:50 vanhu Exp $ */
3 /* $KAME: key_debug.c,v 1.29 2001/08/16 14:25:41 itojun Exp $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
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.
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.
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
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
49 #if HAVE_STDINT_H
50 #include <stdint.h>
51 #endif
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>
62 #include <netinet/in.h>
63 #include PATH_IPSEC_H
65 #ifndef _KERNEL
66 #include <ctype.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #endif /* !_KERNEL */
71 #include "config.h"
72 #include "libpfkey.h"
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));
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
90 #ifdef SADB_X_EXT_PACKET
91 static void kdebug_sadb_x_packet __P((struct sadb_ext *));
92 #endif
94 #ifdef SADB_X_EXT_KMADDRESS
95 static void kdebug_sadb_x_kmaddress __P((struct sadb_ext *));
96 #endif
98 #ifdef _KERNEL
99 static void kdebug_secreplay __P((struct secreplay *));
100 #endif
102 #ifndef _KERNEL
103 #define panic(param) { printf(param); exit(1); }
104 #endif
106 #include "libpfkey.h"
107 /* NOTE: host byte order */
109 /* %%%: about struct sadb_msg */
110 void
111 kdebug_sadb(base)
112 struct sadb_msg *base;
114 struct sadb_ext *ext;
115 int tlen, extlen;
117 /* sanity check */
118 if (base == NULL)
119 panic("kdebug_sadb: NULL pointer was passed.\n");
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);
128 tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
129 ext = (void *)((caddr_t)(void *)base + sizeof(struct sadb_msg));
131 while (tlen > 0) {
132 printf("sadb_ext{ len=%u type=%u }\n",
133 ext->sadb_ext_len, ext->sadb_ext_type);
135 if (ext->sadb_ext_len == 0) {
136 printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
137 return;
139 if (ext->sadb_ext_len > tlen) {
140 printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
141 return;
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 #ifdef SADB_X_EXT_KMADDRESS
202 case SADB_X_EXT_KMADDRESS:
203 kdebug_sadb_x_kmaddress(ext);
204 break;
205 #endif
206 default:
207 printf("kdebug_sadb: invalid ext_type %u was passed.\n",
208 ext->sadb_ext_type);
209 return;
212 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
213 tlen -= extlen;
214 ext = (void *)((caddr_t)(void *)ext + extlen);
217 return;
220 static void
221 kdebug_sadb_prop(ext)
222 struct sadb_ext *ext;
224 struct sadb_prop *prop = (void *)ext;
225 struct sadb_comb *comb;
226 int len;
228 /* sanity check */
229 if (ext == NULL)
230 panic("kdebug_sadb_prop: NULL pointer was passed.\n");
232 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
233 / sizeof(*comb);
234 comb = (void *)(prop + 1);
235 printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
237 while (len--) {
238 printf("sadb_comb{ auth=%u encrypt=%u "
239 "flags=0x%04x reserved=0x%08x\n",
240 comb->sadb_comb_auth, comb->sadb_comb_encrypt,
241 comb->sadb_comb_flags, comb->sadb_comb_reserved);
243 printf(" auth_minbits=%u auth_maxbits=%u "
244 "encrypt_minbits=%u encrypt_maxbits=%u\n",
245 comb->sadb_comb_auth_minbits,
246 comb->sadb_comb_auth_maxbits,
247 comb->sadb_comb_encrypt_minbits,
248 comb->sadb_comb_encrypt_maxbits);
250 printf(" soft_alloc=%u hard_alloc=%u "
251 "soft_bytes=%lu hard_bytes=%lu\n",
252 comb->sadb_comb_soft_allocations,
253 comb->sadb_comb_hard_allocations,
254 (unsigned long)comb->sadb_comb_soft_bytes,
255 (unsigned long)comb->sadb_comb_hard_bytes);
257 printf(" soft_alloc=%lu hard_alloc=%lu "
258 "soft_bytes=%lu hard_bytes=%lu }\n",
259 (unsigned long)comb->sadb_comb_soft_addtime,
260 (unsigned long)comb->sadb_comb_hard_addtime,
261 (unsigned long)comb->sadb_comb_soft_usetime,
262 (unsigned long)comb->sadb_comb_hard_usetime);
263 comb++;
265 printf("}\n");
267 return;
270 static void
271 kdebug_sadb_identity(ext)
272 struct sadb_ext *ext;
274 struct sadb_ident *id = (void *)ext;
275 int len;
277 /* sanity check */
278 if (ext == NULL)
279 panic("kdebug_sadb_identity: NULL pointer was passed.\n");
281 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
282 printf("sadb_ident_%s{",
283 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
284 switch (id->sadb_ident_type) {
285 default:
286 printf(" type=%d id=%lu",
287 id->sadb_ident_type, (u_long)id->sadb_ident_id);
288 if (len) {
289 #ifdef _KERNEL
290 ipsec_hexdump((caddr_t)(id + 1), len); /*XXX cast ?*/
291 #else
292 char *p, *ep;
293 printf("\n str=\"");
294 p = (void *)(id + 1);
295 ep = p + len;
296 for (/*nothing*/; *p && p < ep; p++) {
297 if (isprint((int)*p))
298 printf("%c", *p & 0xff);
299 else
300 printf("\\%03o", *p & 0xff);
302 #endif
303 printf("\"");
305 break;
308 printf(" }\n");
310 return;
313 static void
314 kdebug_sadb_supported(ext)
315 struct sadb_ext *ext;
317 struct sadb_supported *sup = (void *)ext;
318 struct sadb_alg *alg;
319 int len;
321 /* sanity check */
322 if (ext == NULL)
323 panic("kdebug_sadb_supported: NULL pointer was passed.\n");
325 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
326 / sizeof(*alg);
327 alg = (void *)(sup + 1);
328 printf("sadb_sup{\n");
329 while (len--) {
330 printf(" { id=%d ivlen=%d min=%d max=%d }\n",
331 alg->sadb_alg_id, alg->sadb_alg_ivlen,
332 alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
333 alg++;
335 printf("}\n");
337 return;
340 static void
341 kdebug_sadb_lifetime(ext)
342 struct sadb_ext *ext;
344 struct sadb_lifetime *lft = (void *)ext;
346 /* sanity check */
347 if (ext == NULL)
348 printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
350 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
351 lft->sadb_lifetime_allocations,
352 (u_int32_t)lft->sadb_lifetime_bytes);
353 printf(" addtime=%u, usetime=%u }\n",
354 (u_int32_t)lft->sadb_lifetime_addtime,
355 (u_int32_t)lft->sadb_lifetime_usetime);
357 return;
360 static void
361 kdebug_sadb_sa(ext)
362 struct sadb_ext *ext;
364 struct sadb_sa *sa = (void *)ext;
366 /* sanity check */
367 if (ext == NULL)
368 panic("kdebug_sadb_sa: NULL pointer was passed.\n");
370 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
371 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
372 sa->sadb_sa_state);
373 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
374 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
376 return;
379 static void
380 kdebug_sadb_address(ext)
381 struct sadb_ext *ext;
383 struct sadb_address *addr = (void *)ext;
385 /* sanity check */
386 if (ext == NULL)
387 panic("kdebug_sadb_address: NULL pointer was passed.\n");
389 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
390 addr->sadb_address_proto, addr->sadb_address_prefixlen,
391 ((u_char *)(void *)&addr->sadb_address_reserved)[0],
392 ((u_char *)(void *)&addr->sadb_address_reserved)[1]);
394 kdebug_sockaddr((void *)((caddr_t)(void *)ext + sizeof(*addr)));
396 return;
399 static void
400 kdebug_sadb_key(ext)
401 struct sadb_ext *ext;
403 struct sadb_key *key = (void *)ext;
405 /* sanity check */
406 if (ext == NULL)
407 panic("kdebug_sadb_key: NULL pointer was passed.\n");
409 printf("sadb_key{ bits=%u reserved=%u\n",
410 key->sadb_key_bits, key->sadb_key_reserved);
411 printf(" key=");
413 /* sanity check 2 */
414 if (((uint32_t)key->sadb_key_bits >> 3) >
415 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
416 printf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
417 (uint32_t)key->sadb_key_bits >> 3,
418 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
421 ipsec_hexdump(key + sizeof(struct sadb_key),
422 (int)((uint32_t)key->sadb_key_bits >> 3));
423 printf(" }\n");
424 return;
427 static void
428 kdebug_sadb_x_sa2(ext)
429 struct sadb_ext *ext;
431 struct sadb_x_sa2 *sa2 = (void *)ext;
433 /* sanity check */
434 if (ext == NULL)
435 panic("kdebug_sadb_x_sa2: NULL pointer was passed.\n");
437 printf("sadb_x_sa2{ mode=%u reqid=%u\n",
438 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
439 printf(" reserved1=%u reserved2=%u sequence=%u }\n",
440 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
441 sa2->sadb_x_sa2_sequence);
443 return;
446 void
447 kdebug_sadb_x_policy(ext)
448 struct sadb_ext *ext;
450 struct sadb_x_policy *xpl = (void *)ext;
451 struct sockaddr *addr;
453 /* sanity check */
454 if (ext == NULL)
455 panic("kdebug_sadb_x_policy: NULL pointer was passed.\n");
457 #ifdef HAVE_PFKEY_POLICY_PRIORITY
458 printf("sadb_x_policy{ type=%u dir=%u id=%x priority=%u }\n",
459 #else
460 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
461 #endif
462 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
463 #ifdef HAVE_PFKEY_POLICY_PRIORITY
464 xpl->sadb_x_policy_id, xpl->sadb_x_policy_priority);
465 #else
466 xpl->sadb_x_policy_id);
467 #endif
469 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
470 int tlen;
471 struct sadb_x_ipsecrequest *xisr;
473 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
474 xisr = (void *)(xpl + 1);
476 while (tlen > 0) {
477 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
478 xisr->sadb_x_ipsecrequest_len,
479 xisr->sadb_x_ipsecrequest_proto,
480 xisr->sadb_x_ipsecrequest_mode,
481 xisr->sadb_x_ipsecrequest_level,
482 xisr->sadb_x_ipsecrequest_reqid);
484 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
485 addr = (void *)(xisr + 1);
486 kdebug_sockaddr(addr);
487 addr = (void *)((caddr_t)(void *)addr
488 + sysdep_sa_len(addr));
489 kdebug_sockaddr(addr);
492 printf(" }\n");
494 /* prevent infinite loop */
495 if (xisr->sadb_x_ipsecrequest_len == 0) {
496 printf("kdebug_sadb_x_policy: wrong policy struct.\n");
497 return;
499 /* prevent overflow */
500 if (xisr->sadb_x_ipsecrequest_len > tlen) {
501 printf("invalid ipsec policy length\n");
502 return;
505 tlen -= xisr->sadb_x_ipsecrequest_len;
507 xisr = (void *)((caddr_t)(void *)xisr
508 + xisr->sadb_x_ipsecrequest_len);
511 if (tlen != 0)
512 panic("kdebug_sadb_x_policy: wrong policy struct.\n");
515 return;
518 #ifdef SADB_X_EXT_NAT_T_TYPE
519 static void
520 kdebug_sadb_x_nat_t_type(struct sadb_ext *ext)
522 struct sadb_x_nat_t_type *ntt = (void *)ext;
524 /* sanity check */
525 if (ext == NULL)
526 panic("kdebug_sadb_x_nat_t_type: NULL pointer was passed.\n");
528 printf("sadb_x_nat_t_type{ type=%u }\n", ntt->sadb_x_nat_t_type_type);
530 return;
533 static void
534 kdebug_sadb_x_nat_t_port(struct sadb_ext *ext)
536 struct sadb_x_nat_t_port *ntp = (void *)ext;
538 /* sanity check */
539 if (ext == NULL)
540 panic("kdebug_sadb_x_nat_t_port: NULL pointer was passed.\n");
542 printf("sadb_x_nat_t_port{ port=%u }\n", ntohs(ntp->sadb_x_nat_t_port_port));
544 return;
546 #endif
548 #ifdef SADB_X_EXT_PACKET
549 static void
550 kdebug_sadb_x_packet(ext)
551 struct sadb_ext *ext;
553 struct sadb_x_packet *pkt = (struct sadb_x_packet *)ext;
555 /* sanity check */
556 if (ext == NULL)
557 panic("kdebug_sadb_x_packet: NULL pointer was passed.\n");
559 printf("sadb_x_packet{ copylen=%u\n", pkt->sadb_x_packet_copylen);
560 printf(" packet=");
561 ipsec_hexdump((caddr_t)pkt + sizeof(struct sadb_x_packet),
562 pkt->sadb_x_packet_copylen);
563 printf(" }\n");
564 return;
566 #endif
568 #ifdef SADB_X_EXT_KMADDRESS
569 static void
570 kdebug_sadb_x_kmaddress(ext)
571 struct sadb_ext *ext;
573 struct sadb_x_kmaddress *kma = (struct sadb_x_kmaddress *)ext;
574 struct sockaddr * sa;
575 sa_family_t family;
576 int len, sa_len;
578 /* sanity check */
579 if (ext == NULL)
580 panic("kdebug_sadb_x_kmaddress: NULL pointer was passed.\n");
582 len = (PFKEY_UNUNIT64(kma->sadb_x_kmaddress_len) - sizeof(*kma));
584 printf("sadb_x_kmaddress{ reserved=0x%02x%02x%02x%02x }\n",
585 ((u_char *)(void *)&kma->sadb_x_kmaddress_reserved)[0],
586 ((u_char *)(void *)&kma->sadb_x_kmaddress_reserved)[1],
587 ((u_char *)(void *)&kma->sadb_x_kmaddress_reserved)[2],
588 ((u_char *)(void *)&kma->sadb_x_kmaddress_reserved)[3]);
590 sa = (struct sockaddr *)(kma + 1);
591 if (len < sizeof(struct sockaddr) || (sa_len = sysdep_sa_len(sa)) > len)
592 panic("kdebug_sadb_x_kmaddress: not enough data to read"
593 " first sockaddr.\n");
594 kdebug_sockaddr((void *)sa); /* local address */
595 family = sa->sa_family;
597 len -= sa_len;
598 sa = (struct sockaddr *)((char *)sa + sa_len);
599 if (len < sizeof(struct sockaddr) || sysdep_sa_len(sa) > len)
600 panic("kdebug_sadb_x_kmaddress: not enough data to read"
601 " second sockaddr.\n");
602 kdebug_sockaddr((void *)sa); /* remote address */
604 if (family != sa->sa_family)
605 printf("kdebug_sadb_x_kmaddress: !!!! Please, note the "
606 "unexpected mismatch in address family.\n");
608 #endif
611 #ifdef _KERNEL
612 /* %%%: about SPD and SAD */
613 void
614 kdebug_secpolicy(sp)
615 struct secpolicy *sp;
617 /* sanity check */
618 if (sp == NULL)
619 panic("kdebug_secpolicy: NULL pointer was passed.\n");
621 printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
622 sp->refcnt, sp->state, sp->policy);
624 kdebug_secpolicyindex(&sp->spidx);
626 switch (sp->policy) {
627 case IPSEC_POLICY_DISCARD:
628 printf(" type=discard }\n");
629 break;
630 case IPSEC_POLICY_NONE:
631 printf(" type=none }\n");
632 break;
633 case IPSEC_POLICY_IPSEC:
635 struct ipsecrequest *isr;
636 for (isr = sp->req; isr != NULL; isr = isr->next) {
638 printf(" level=%u\n", isr->level);
639 kdebug_secasindex(&isr->saidx);
641 if (isr->sav != NULL)
642 kdebug_secasv(isr->sav);
644 printf(" }\n");
646 break;
647 case IPSEC_POLICY_BYPASS:
648 printf(" type=bypass }\n");
649 break;
650 case IPSEC_POLICY_ENTRUST:
651 printf(" type=entrust }\n");
652 break;
653 default:
654 printf("kdebug_secpolicy: Invalid policy found. %d\n",
655 sp->policy);
656 break;
659 return;
662 void
663 kdebug_secpolicyindex(spidx)
664 struct secpolicyindex *spidx;
666 /* sanity check */
667 if (spidx == NULL)
668 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
670 printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
671 spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
673 ipsec_hexdump((caddr_t)&spidx->src,
674 sysdep_sa_len((struct sockaddr *)&spidx->src));
675 printf("\n");
676 ipsec_hexdump((caddr_t)&spidx->dst,
677 sysdep_sa_len((struct sockaddr *)&spidx->dst));
678 printf("}\n");
680 return;
683 void
684 kdebug_secasindex(saidx)
685 struct secasindex *saidx;
687 /* sanity check */
688 if (saidx == NULL)
689 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
691 printf("secasindex{ mode=%u proto=%u\n",
692 saidx->mode, saidx->proto);
694 ipsec_hexdump((caddr_t)&saidx->src,
695 sysdep_sa_len((struct sockaddr *)&saidx->src));
696 printf("\n");
697 ipsec_hexdump((caddr_t)&saidx->dst,
698 sysdep_sa_len((struct sockaddr *)&saidx->dst));
699 printf("\n");
701 return;
704 void
705 kdebug_secasv(sav)
706 struct secasvar *sav;
708 /* sanity check */
709 if (sav == NULL)
710 panic("kdebug_secasv: NULL pointer was passed.\n");
712 printf("secas{");
713 kdebug_secasindex(&sav->sah->saidx);
715 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
716 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
717 printf(" spi=%u flags=%u\n",
718 (u_int32_t)ntohl(sav->spi), sav->flags);
720 if (sav->key_auth != NULL)
721 kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
722 if (sav->key_enc != NULL)
723 kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
724 if (sav->iv != NULL) {
725 printf(" iv=");
726 ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
727 printf("\n");
730 if (sav->replay != NULL)
731 kdebug_secreplay(sav->replay);
732 if (sav->lft_c != NULL)
733 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
734 if (sav->lft_h != NULL)
735 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
736 if (sav->lft_s != NULL)
737 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
739 #if notyet
740 /* XXX: misc[123] ? */
741 #endif
743 return;
746 static void
747 kdebug_secreplay(rpl)
748 struct secreplay *rpl;
750 int len, l;
752 /* sanity check */
753 if (rpl == NULL)
754 panic("kdebug_secreplay: NULL pointer was passed.\n");
756 printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
757 rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
759 if (rpl->bitmap == NULL) {
760 printf(" }\n");
761 return;
764 printf("\n bitmap { ");
766 for (len = 0; len < rpl->wsize; len++) {
767 for (l = 7; l >= 0; l--)
768 printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
770 printf(" }\n");
772 return;
775 void
776 kdebug_mbufhdr(m)
777 struct mbuf *m;
779 /* sanity check */
780 if (m == NULL)
781 return;
783 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
784 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
785 m, m->m_next, m->m_nextpkt, m->m_data,
786 m->m_len, m->m_type, m->m_flags);
788 if (m->m_flags & M_PKTHDR) {
789 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
790 m->m_pkthdr.len, m->m_pkthdr.rcvif);
793 #ifdef __FreeBSD__
794 if (m->m_flags & M_EXT) {
795 printf(" m_ext{ ext_buf:%p ext_free:%p "
796 "ext_size:%u ext_ref:%p }\n",
797 m->m_ext.ext_buf, m->m_ext.ext_free,
798 m->m_ext.ext_size, m->m_ext.ext_ref);
800 #endif
802 return;
805 void
806 kdebug_mbuf(m0)
807 struct mbuf *m0;
809 struct mbuf *m = m0;
810 int i, j;
812 for (j = 0; m; m = m->m_next) {
813 kdebug_mbufhdr(m);
814 printf(" m_data:\n");
815 for (i = 0; i < m->m_len; i++) {
816 if (i && i % 32 == 0)
817 printf("\n");
818 if (i % 4 == 0)
819 printf(" ");
820 printf("%02x", mtod(m, u_char *)[i]);
821 j++;
823 printf("\n");
826 return;
828 #endif /* _KERNEL */
830 static void
831 kdebug_sockaddr(addr)
832 struct sockaddr *addr;
834 struct sockaddr_in *sin4;
835 #ifdef INET6
836 struct sockaddr_in6 *sin6;
837 #endif
839 /* sanity check */
840 if (addr == NULL)
841 panic("kdebug_sockaddr: NULL pointer was passed.\n");
843 /* NOTE: We deal with port number as host byte order. */
844 printf("sockaddr{ len=%u family=%u", sysdep_sa_len(addr), addr->sa_family);
846 switch (addr->sa_family) {
847 case AF_INET:
848 sin4 = (void *)addr;
849 printf(" port=%u\n", ntohs(sin4->sin_port));
850 ipsec_hexdump(&sin4->sin_addr, sizeof(sin4->sin_addr));
851 break;
852 #ifdef INET6
853 case AF_INET6:
854 sin6 = (void *)addr;
855 printf(" port=%u\n", ntohs(sin6->sin6_port));
856 printf(" flowinfo=0x%08x, scope_id=0x%08x\n",
857 sin6->sin6_flowinfo, sin6->sin6_scope_id);
858 ipsec_hexdump(&sin6->sin6_addr, sizeof(sin6->sin6_addr));
859 break;
860 #endif
863 printf(" }\n");
865 return;
868 void
869 ipsec_bindump(buf, len)
870 caddr_t buf;
871 int len;
873 int i;
875 for (i = 0; i < len; i++)
876 printf("%c", (unsigned char)buf[i]);
878 return;
882 void
883 ipsec_hexdump(buf, len)
884 const void *buf;
885 int len;
887 int i;
889 for (i = 0; i < len; i++) {
890 if (i != 0 && i % 32 == 0) printf("\n");
891 if (i % 4 == 0) printf(" ");
892 printf("%02x", ((const unsigned char *)buf)[i]);
894 #if 0
895 if (i % 32 != 0) printf("\n");
896 #endif
898 return;