Import mdocml-1.9.14:
[netbsd-mini2440.git] / sys / netkey / key_debug.c
blobc9c85add77dad2c99570dfa5381ab807ad705d6d
1 /* $NetBSD: key_debug.c,v 1.34 2009/03/14 14:46:11 dsl Exp $ */
2 /* $KAME: key_debug.c,v 1.36 2003/06/27 06:46:01 itojun Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: key_debug.c,v 1.34 2009/03/14 14:46:11 dsl Exp $");
36 #ifdef _KERNEL
37 #include "opt_inet.h"
38 #endif
40 #include <sys/param.h>
41 #ifdef _KERNEL
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #endif
45 #include <sys/socket.h>
47 #include <net/route.h>
49 #include <netkey/key_var.h>
50 #include <netkey/key_debug.h>
52 #include <netinet/in.h>
53 #include <netinet6/ipsec.h>
55 #ifndef _KERNEL
56 #include <ctype.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #endif /* !_KERNEL */
61 struct typestr {
62 const char *string;
63 u_int type;
65 #define TYPESTR(x) { "SADB_" #x, SADB_ ## x }
67 static const char *kdebug_typestr(u_int, const struct typestr *);
68 static const char *kdebug_sadb_msg_typestr(u_int);
69 static const char *kdebug_sadb_ext_typestr(u_int);
70 static void kdebug_sadb_prop(struct sadb_ext *);
71 static void kdebug_sadb_identity(struct sadb_ext *);
72 static void kdebug_sadb_supported(struct sadb_ext *);
73 static void kdebug_sadb_lifetime(struct sadb_ext *);
74 static void kdebug_sadb_sa(struct sadb_ext *);
75 static void kdebug_sadb_address(struct sadb_ext *);
76 static void kdebug_sadb_key(struct sadb_ext *);
77 static void kdebug_sadb_x_sa2(struct sadb_ext *);
78 #ifdef SADB_X_EXT_TAG
79 static void kdebug_sadb_x_tag(struct sadb_ext *);
80 #endif
82 #ifdef _KERNEL
83 static void kdebug_secreplay(struct secreplay *);
84 #endif
86 #ifndef _KERNEL
87 #define panic(param) { printf(param); exit(1); }
88 #endif
90 static const char *
91 kdebug_typestr(u_int type, const struct typestr *list)
93 static char buf[32];
95 while (list->string != NULL) {
96 if (type == list->type)
97 return (list->string);
98 list++;
100 snprintf(buf, sizeof(buf), "%u", type);
102 return (buf);
105 static const char *
106 kdebug_sadb_msg_typestr(u_int type)
108 static const struct typestr list[] = {
109 TYPESTR(RESERVED),
110 TYPESTR(GETSPI),
111 TYPESTR(UPDATE),
112 TYPESTR(ADD),
113 TYPESTR(DELETE),
114 TYPESTR(GET),
115 TYPESTR(ACQUIRE),
116 TYPESTR(REGISTER),
117 TYPESTR(EXPIRE),
118 TYPESTR(FLUSH),
119 TYPESTR(DUMP),
120 TYPESTR(X_PROMISC),
121 TYPESTR(X_PCHANGE),
122 TYPESTR(X_SPDUPDATE),
123 TYPESTR(X_SPDADD),
124 TYPESTR(X_SPDDELETE),
125 TYPESTR(X_SPDGET),
126 TYPESTR(X_SPDACQUIRE),
127 TYPESTR(X_SPDDUMP),
128 TYPESTR(X_SPDFLUSH),
129 TYPESTR(X_SPDSETIDX),
130 TYPESTR(X_SPDEXPIRE),
131 TYPESTR(X_SPDDELETE2),
132 { NULL, 0 },
135 return kdebug_typestr(type, list);
138 static const char *
139 kdebug_sadb_ext_typestr(u_int type)
141 static const struct typestr list[] = {
142 TYPESTR(EXT_RESERVED),
143 TYPESTR(EXT_SA),
144 TYPESTR(EXT_LIFETIME_CURRENT),
145 TYPESTR(EXT_LIFETIME_HARD),
146 TYPESTR(EXT_LIFETIME_SOFT),
147 TYPESTR(EXT_ADDRESS_SRC),
148 TYPESTR(EXT_ADDRESS_DST),
149 TYPESTR(EXT_ADDRESS_PROXY),
150 TYPESTR(EXT_KEY_AUTH),
151 TYPESTR(EXT_KEY_ENCRYPT),
152 TYPESTR(EXT_IDENTITY_SRC),
153 TYPESTR(EXT_IDENTITY_DST),
154 TYPESTR(EXT_SENSITIVITY),
155 TYPESTR(EXT_PROPOSAL),
156 TYPESTR(EXT_SUPPORTED_AUTH),
157 TYPESTR(EXT_SUPPORTED_ENCRYPT),
158 TYPESTR(EXT_SPIRANGE),
159 TYPESTR(X_EXT_KMPRIVATE),
160 TYPESTR(X_EXT_POLICY),
161 TYPESTR(X_EXT_SA2),
162 #ifdef SADB_X_EXT_TAG
163 TYPESTR(X_EXT_TAG),
164 #endif
165 { NULL, 0 },
168 return kdebug_typestr(type, list);
171 /* NOTE: host byte order */
173 /* %%%: about struct sadb_msg */
174 void
175 kdebug_sadb(struct sadb_msg *base)
177 struct sadb_ext *ext;
178 int tlen, extlen;
180 /* sanity check */
181 if (base == NULL)
182 panic("kdebug_sadb: NULL pointer was passed.");
184 printf("sadb_msg{ version=%u type=%s errno=%u satype=%u\n",
185 base->sadb_msg_version,
186 kdebug_sadb_msg_typestr(base->sadb_msg_type),
187 base->sadb_msg_errno, base->sadb_msg_satype);
188 printf(" len=%u reserved=%u seq=%u pid=%u\n",
189 base->sadb_msg_len, base->sadb_msg_reserved,
190 base->sadb_msg_seq, base->sadb_msg_pid);
192 tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
193 ext = (struct sadb_ext *)((char *)base + sizeof(struct sadb_msg));
195 while (tlen > 0) {
196 printf("sadb_ext{ len=%u type=%s }\n",
197 ext->sadb_ext_len,
198 kdebug_sadb_ext_typestr(ext->sadb_ext_type));
200 if (ext->sadb_ext_len == 0) {
201 printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
202 return;
204 if (ext->sadb_ext_len > tlen) {
205 printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
206 return;
209 switch (ext->sadb_ext_type) {
210 case SADB_EXT_SA:
211 kdebug_sadb_sa(ext);
212 break;
213 case SADB_EXT_LIFETIME_CURRENT:
214 case SADB_EXT_LIFETIME_HARD:
215 case SADB_EXT_LIFETIME_SOFT:
216 kdebug_sadb_lifetime(ext);
217 break;
218 case SADB_EXT_ADDRESS_SRC:
219 case SADB_EXT_ADDRESS_DST:
220 case SADB_EXT_ADDRESS_PROXY:
221 kdebug_sadb_address(ext);
222 break;
223 case SADB_EXT_KEY_AUTH:
224 case SADB_EXT_KEY_ENCRYPT:
225 kdebug_sadb_key(ext);
226 break;
227 case SADB_EXT_IDENTITY_SRC:
228 case SADB_EXT_IDENTITY_DST:
229 kdebug_sadb_identity(ext);
230 break;
231 case SADB_EXT_SENSITIVITY:
232 break;
233 case SADB_EXT_PROPOSAL:
234 kdebug_sadb_prop(ext);
235 break;
236 case SADB_EXT_SUPPORTED_AUTH:
237 case SADB_EXT_SUPPORTED_ENCRYPT:
238 kdebug_sadb_supported(ext);
239 break;
240 case SADB_EXT_SPIRANGE:
241 case SADB_X_EXT_KMPRIVATE:
242 break;
243 case SADB_X_EXT_POLICY:
244 kdebug_sadb_x_policy(ext);
245 break;
246 case SADB_X_EXT_SA2:
247 kdebug_sadb_x_sa2(ext);
248 break;
249 #ifdef SADB_X_EXT_TAG
250 case SADB_X_EXT_TAG:
251 kdebug_sadb_x_tag(ext);
252 break;
253 #endif
254 default:
255 printf("kdebug_sadb: invalid ext_type %u was passed.\n",
256 ext->sadb_ext_type);
257 return;
260 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
261 tlen -= extlen;
262 ext = (struct sadb_ext *)((char *)ext + extlen);
265 return;
268 static void
269 kdebug_sadb_prop(struct sadb_ext *ext)
271 struct sadb_prop *prop = (struct sadb_prop *)ext;
272 struct sadb_comb *comb;
273 int len;
275 /* sanity check */
276 if (ext == NULL)
277 panic("kdebug_sadb_prop: NULL pointer was passed.");
279 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
280 / sizeof(*comb);
281 comb = (struct sadb_comb *)(prop + 1);
282 printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
284 while (len--) {
285 printf("sadb_comb{ auth=%u encrypt=%u "
286 "flags=0x%04x reserved=0x%08x\n",
287 comb->sadb_comb_auth, comb->sadb_comb_encrypt,
288 comb->sadb_comb_flags, comb->sadb_comb_reserved);
290 printf(" auth_minbits=%u auth_maxbits=%u "
291 "encrypt_minbits=%u encrypt_maxbits=%u\n",
292 comb->sadb_comb_auth_minbits,
293 comb->sadb_comb_auth_maxbits,
294 comb->sadb_comb_encrypt_minbits,
295 comb->sadb_comb_encrypt_maxbits);
297 printf(" soft_alloc=%u hard_alloc=%u "
298 "soft_bytes=%lu hard_bytes=%lu\n",
299 comb->sadb_comb_soft_allocations,
300 comb->sadb_comb_hard_allocations,
301 (unsigned long)comb->sadb_comb_soft_bytes,
302 (unsigned long)comb->sadb_comb_hard_bytes);
304 printf(" soft_alloc=%lu hard_alloc=%lu "
305 "soft_bytes=%lu hard_bytes=%lu }\n",
306 (unsigned long)comb->sadb_comb_soft_addtime,
307 (unsigned long)comb->sadb_comb_hard_addtime,
308 (unsigned long)comb->sadb_comb_soft_usetime,
309 (unsigned long)comb->sadb_comb_hard_usetime);
310 comb++;
312 printf("}\n");
314 return;
317 static void
318 kdebug_sadb_identity(struct sadb_ext *ext)
320 struct sadb_ident *id = (struct sadb_ident *)ext;
321 int len;
323 /* sanity check */
324 if (ext == NULL)
325 panic("kdebug_sadb_identity: NULL pointer was passed.");
327 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
328 printf("sadb_ident_%s{",
329 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
330 switch (id->sadb_ident_type) {
331 default:
332 printf(" type=%u id=%lu",
333 id->sadb_ident_type, (u_long)id->sadb_ident_id);
334 if (len) {
335 #ifdef _KERNEL
336 ipsec_hexdump((void *)(id + 1), len); /*XXX cast ?*/
337 #else
338 unsigned char *p, *ep;
339 printf("\n str=\"");
340 p = (void *)(id + 1);
341 ep = p + len;
342 for (/*nothing*/; *p && p < ep; p++) {
343 if (isprint(*p))
344 printf("%c", *p);
345 else
346 printf("\\%03o", *p);
348 #endif
349 printf("\"");
351 break;
354 printf(" }\n");
356 return;
359 static void
360 kdebug_sadb_supported(struct sadb_ext *ext)
362 struct sadb_supported *sup = (struct sadb_supported *)ext;
363 struct sadb_alg *alg;
364 int len;
366 /* sanity check */
367 if (ext == NULL)
368 panic("kdebug_sadb_supported: NULL pointer was passed.");
370 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
371 / sizeof(*alg);
372 alg = (struct sadb_alg *)(sup + 1);
373 printf("sadb_sup{\n");
374 while (len--) {
375 printf(" { id=%u ivlen=%u min=%u max=%u }\n",
376 alg->sadb_alg_id, alg->sadb_alg_ivlen,
377 alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
378 alg++;
380 printf("}\n");
382 return;
385 static void
386 kdebug_sadb_lifetime(struct sadb_ext *ext)
388 struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
390 /* sanity check */
391 if (ext == NULL)
392 printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
394 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
395 lft->sadb_lifetime_allocations,
396 (u_int32_t)lft->sadb_lifetime_bytes);
397 printf(" addtime=%u, usetime=%u }\n",
398 (u_int32_t)lft->sadb_lifetime_addtime,
399 (u_int32_t)lft->sadb_lifetime_usetime);
401 return;
404 static void
405 kdebug_sadb_sa(struct sadb_ext *ext)
407 struct sadb_sa *sa = (struct sadb_sa *)ext;
409 /* sanity check */
410 if (ext == NULL)
411 panic("kdebug_sadb_sa: NULL pointer was passed.");
413 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
414 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
415 sa->sadb_sa_state);
416 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
417 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
419 return;
422 static void
423 kdebug_sadb_address(struct sadb_ext *ext)
425 struct sadb_address *addr = (struct sadb_address *)ext;
427 /* sanity check */
428 if (ext == NULL)
429 panic("kdebug_sadb_address: NULL pointer was passed.");
431 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
432 addr->sadb_address_proto, addr->sadb_address_prefixlen,
433 ((u_char *)&addr->sadb_address_reserved)[0],
434 ((u_char *)&addr->sadb_address_reserved)[1]);
436 kdebug_sockaddr((struct sockaddr *)((char *)ext + sizeof(*addr)));
438 return;
441 static void
442 kdebug_sadb_key(struct sadb_ext *ext)
444 struct sadb_key *key = (struct sadb_key *)ext;
446 /* sanity check */
447 if (ext == NULL)
448 panic("kdebug_sadb_key: NULL pointer was passed.");
450 printf("sadb_key{ bits=%u reserved=%u\n",
451 key->sadb_key_bits, key->sadb_key_reserved);
452 printf(" key=");
454 /* sanity check 2 */
455 if ((key->sadb_key_bits >> 3) >
456 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
457 printf("kdebug_sadb_key: key length mismatch, bit:%u len:%ld.\n",
458 key->sadb_key_bits >> 3,
459 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
462 ipsec_hexdump((char *)key + sizeof(struct sadb_key),
463 key->sadb_key_bits >> 3);
464 printf(" }\n");
465 return;
468 static void
469 kdebug_sadb_x_sa2(struct sadb_ext *ext)
471 struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext;
473 /* sanity check */
474 if (ext == NULL)
475 panic("kdebug_sadb_x_sa2: NULL pointer was passed.");
477 printf("sadb_x_sa2{ mode=%u reqid=%u\n",
478 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
479 printf(" reserved1=%u reserved2=%u sequence=%u }\n",
480 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
481 sa2->sadb_x_sa2_sequence);
483 return;
486 #ifdef SADB_X_EXT_TAG
487 static void
488 kdebug_sadb_x_tag(struct sadb_ext *ext)
490 struct sadb_x_tag *tag = (struct sadb_x_tag *)ext;
492 /* sanity check */
493 if (ext == NULL)
494 panic("kdebug_sadb_x_tag: NULL pointer was passed.");
496 printf("sadb_x_sa2{ tag=\"%s\" }\n", tag->sadb_x_tag_name);
498 return;
500 #endif
502 void
503 kdebug_sadb_x_policy(struct sadb_ext *ext)
505 struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
506 struct sockaddr *addr;
508 /* sanity check */
509 if (ext == NULL)
510 panic("kdebug_sadb_x_policy: NULL pointer was passed.");
512 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
513 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
514 xpl->sadb_x_policy_id);
516 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
517 int tlen;
518 struct sadb_x_ipsecrequest *xisr;
520 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
521 xisr = (struct sadb_x_ipsecrequest *)(xpl + 1);
523 while (tlen > 0) {
524 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
525 xisr->sadb_x_ipsecrequest_len,
526 xisr->sadb_x_ipsecrequest_proto,
527 xisr->sadb_x_ipsecrequest_mode,
528 xisr->sadb_x_ipsecrequest_level,
529 xisr->sadb_x_ipsecrequest_reqid);
531 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
532 addr = (struct sockaddr *)(xisr + 1);
533 kdebug_sockaddr(addr);
534 addr = (struct sockaddr *)((char *)addr
535 + addr->sa_len);
536 kdebug_sockaddr(addr);
539 printf(" }\n");
541 /* prevent infinite loop */
542 if (xisr->sadb_x_ipsecrequest_len <= 0) {
543 printf("kdebug_sadb_x_policy: wrong policy struct.\n");
544 return;
546 /* prevent overflow */
547 if (xisr->sadb_x_ipsecrequest_len > tlen) {
548 printf("invalid ipsec policy length\n");
549 return;
552 tlen -= xisr->sadb_x_ipsecrequest_len;
554 xisr = (struct sadb_x_ipsecrequest *)((char *)xisr
555 + xisr->sadb_x_ipsecrequest_len);
558 if (tlen != 0)
559 panic("kdebug_sadb_x_policy: wrong policy struct.");
562 return;
565 #ifdef _KERNEL
566 /* %%%: about SPD and SAD */
567 void
568 kdebug_secpolicy(struct secpolicy *sp)
570 /* sanity check */
571 if (sp == NULL)
572 panic("kdebug_secpolicy: NULL pointer was passed.");
574 printf("secpolicy{ refcnt=%u state=%u policy=%u dir=%u\n",
575 sp->refcnt, sp->state, sp->policy, sp->dir);
577 if (sp->spidx)
578 kdebug_secpolicyindex(sp->spidx);
580 switch (sp->policy) {
581 case IPSEC_POLICY_DISCARD:
582 printf(" type=discard }\n");
583 break;
584 case IPSEC_POLICY_NONE:
585 printf(" type=none }\n");
586 break;
587 case IPSEC_POLICY_IPSEC:
589 struct ipsecrequest *isr;
590 for (isr = sp->req; isr != NULL; isr = isr->next) {
592 printf(" level=%u\n", isr->level);
593 kdebug_secasindex(&isr->saidx);
595 if (isr->sav != NULL)
596 kdebug_secasv(isr->sav);
598 printf(" }\n");
600 break;
601 case IPSEC_POLICY_BYPASS:
602 printf(" type=bypass }\n");
603 break;
604 case IPSEC_POLICY_ENTRUST:
605 printf(" type=entrust }\n");
606 break;
607 default:
608 printf("kdebug_secpolicy: Invalid policy found. %u\n",
609 sp->policy);
610 break;
613 return;
616 void
617 kdebug_secpolicyindex(struct secpolicyindex *spidx)
619 /* sanity check */
620 if (spidx == NULL)
621 panic("kdebug_secpolicyindex: NULL pointer was passed.");
623 printf("secpolicyindex{ prefs=%u prefd=%u ul_proto=%u\n",
624 spidx->prefs, spidx->prefd, spidx->ul_proto);
626 ipsec_hexdump((void *)&spidx->src,
627 ((struct sockaddr *)&spidx->src)->sa_len);
628 printf("\n");
629 ipsec_hexdump((void *)&spidx->dst,
630 ((struct sockaddr *)&spidx->dst)->sa_len);
631 printf("}\n");
633 return;
636 void
637 kdebug_secasindex(struct secasindex *saidx)
639 /* sanity check */
640 if (saidx == NULL)
641 panic("kdebug_secpolicyindex: NULL pointer was passed.");
643 printf("secasindex{ mode=%u proto=%u\n",
644 saidx->mode, saidx->proto);
646 ipsec_hexdump((void *)&saidx->src,
647 ((struct sockaddr *)&saidx->src)->sa_len);
648 printf("\n");
649 ipsec_hexdump((void *)&saidx->dst,
650 ((struct sockaddr *)&saidx->dst)->sa_len);
651 printf("\n");
653 return;
656 void
657 kdebug_secasv(struct secasvar *sav)
659 /* sanity check */
660 if (sav == NULL)
661 panic("kdebug_secasv: NULL pointer was passed.");
663 printf("secas{");
664 kdebug_secasindex(&sav->sah->saidx);
666 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
667 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
668 printf(" spi=%u flags=%u\n",
669 (u_int32_t)ntohl(sav->spi), sav->flags);
671 if (sav->key_auth != NULL)
672 kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
673 if (sav->key_enc != NULL)
674 kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
675 if (sav->iv != NULL) {
676 printf(" iv=");
677 ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
678 printf("\n");
681 if (sav->replay != NULL)
682 kdebug_secreplay(sav->replay);
683 if (sav->lft_c != NULL)
684 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
685 if (sav->lft_h != NULL)
686 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
687 if (sav->lft_s != NULL)
688 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
690 #if notyet
691 /* XXX: misc[123] ? */
692 #endif
694 return;
697 static void
698 kdebug_secreplay(struct secreplay *rpl)
700 int len, l;
702 /* sanity check */
703 if (rpl == NULL)
704 panic("kdebug_secreplay: NULL pointer was passed.");
706 printf(" secreplay{ count=%llu wsize=%u seq=%llu lastseq=%llu",
707 (unsigned long long)rpl->count, rpl->wsize,
708 (unsigned long long)rpl->seq, (unsigned long long)rpl->lastseq);
710 if (rpl->bitmap == NULL) {
711 printf(" }\n");
712 return;
715 printf("\n bitmap { ");
717 for (len = 0; len < rpl->wsize; len++) {
718 for (l = 7; l >= 0; l--)
719 printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
721 printf(" }\n");
723 return;
726 void
727 kdebug_mbufhdr(struct mbuf *m)
729 /* sanity check */
730 if (m == NULL)
731 return;
733 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
734 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
735 m, m->m_next, m->m_nextpkt, m->m_data,
736 m->m_len, m->m_type, m->m_flags);
738 if (m->m_flags & M_PKTHDR) {
739 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
740 m->m_pkthdr.len, m->m_pkthdr.rcvif);
744 return;
747 void
748 kdebug_mbuf(struct mbuf *m0)
750 struct mbuf *m = m0;
751 int i, j;
753 for (j = 0; m; m = m->m_next) {
754 kdebug_mbufhdr(m);
755 printf(" m_data:\n");
756 for (i = 0; i < m->m_len; i++) {
757 if (i && i % 32 == 0)
758 printf("\n");
759 if (i % 4 == 0)
760 printf(" ");
761 printf("%02x", mtod(m, u_char *)[i]);
762 j++;
764 printf("\n");
767 return;
769 #endif /* _KERNEL */
771 void
772 kdebug_sockaddr(struct sockaddr *addr)
774 struct sockaddr_in *sin4;
775 #ifdef INET6
776 struct sockaddr_in6 *sin6;
777 #endif
779 /* sanity check */
780 if (addr == NULL)
781 panic("kdebug_sockaddr: NULL pointer was passed.");
783 /* NOTE: We deal with port number as host byte order. */
784 printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
786 switch (addr->sa_family) {
787 case AF_INET:
788 sin4 = (struct sockaddr_in *)addr;
789 printf(" port=%u\n", ntohs(sin4->sin_port));
790 ipsec_hexdump((void *)&sin4->sin_addr, sizeof(sin4->sin_addr));
791 break;
792 #ifdef INET6
793 case AF_INET6:
794 sin6 = (struct sockaddr_in6 *)addr;
795 printf(" port=%u\n", ntohs(sin6->sin6_port));
796 printf(" flowinfo=0x%08x, scope_id=0x%08x\n",
797 sin6->sin6_flowinfo, sin6->sin6_scope_id);
798 ipsec_hexdump((void *)&sin6->sin6_addr,
799 sizeof(sin6->sin6_addr));
800 break;
801 #endif
804 printf(" }\n");
806 return;
809 void
810 ipsec_bindump(void *bufv, int len)
812 char *buf = bufv;
813 int i;
815 for (i = 0; i < len; i++)
816 printf("%c", (unsigned char)buf[i]);
818 return;
822 void
823 ipsec_hexdump(void *bufv, int len)
825 char *buf = bufv;
826 int i;
828 for (i = 0; i < len; i++) {
829 if (i != 0 && i % 32 == 0) printf("\n");
830 if (i % 4 == 0) printf(" ");
831 printf("%02x", (unsigned char)buf[i]);
833 #if 0
834 if (i % 32 != 0) printf("\n");
835 #endif
837 return;