]> git.saurik.com Git - apple/ipsec.git/blame - ipsec-tools/Common/key_debug.c
ipsec-146.1.tar.gz
[apple/ipsec.git] / ipsec-tools / Common / key_debug.c
CommitLineData
52b7d2ce
A
1/* $KAME: key_debug.c,v 1.29 2001/08/16 14:25:41 itojun Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
36#ifdef _KERNEL
37#if defined(__FreeBSD__) && __FreeBSD__ >= 3
38#include "opt_inet.h"
39#include "opt_inet6.h"
40#include "opt_ipsec.h"
41#endif
42#ifdef __NetBSD__
43#include "opt_inet.h"
44#endif
45#endif
46
47#include <sys/types.h>
48#include <sys/param.h>
49#ifdef _KERNEL
50#include <sys/systm.h>
51#include <sys/mbuf.h>
52#include <sys/queue.h>
53#endif
54#include <sys/socket.h>
55
56#include <netinet/in.h>
57#ifdef HAVE_NETINET6_IPSEC
58# include <netinet6/ipsec.h>
59#else
60# include <netinet/ipsec.h>
61#endif
62
63#ifndef _KERNEL
64#include <ctype.h>
65#include <stdio.h>
66#include <stdlib.h>
67#endif /* !_KERNEL */
68
69#include "config.h"
70#include "libpfkey.h"
71
72static void kdebug_sadb_prop __P((struct sadb_ext *));
73static void kdebug_sadb_identity __P((struct sadb_ext *));
74static void kdebug_sadb_supported __P((struct sadb_ext *));
75static void kdebug_sadb_lifetime __P((struct sadb_ext *));
76static void kdebug_sadb_sa __P((struct sadb_ext *));
77static void kdebug_sadb_address __P((struct sadb_ext *));
78static void kdebug_sadb_key __P((struct sadb_ext *));
79static void kdebug_sadb_x_sa2 __P((struct sadb_ext *));
d1e348cf
A
80static void kdebug_sadb_session_id __P((struct sadb_ext *));
81static void kdebug_sadb_sastat __P((struct sadb_ext *));
52b7d2ce
A
82static void kdebug_sadb_x_policy __P((struct sadb_ext *ext));
83static void kdebug_sockaddr __P((struct sockaddr *addr));
84
85#ifdef SADB_X_EXT_NAT_T_TYPE
86static void kdebug_sadb_x_nat_t_type __P((struct sadb_ext *ext));
87static void kdebug_sadb_x_nat_t_port __P((struct sadb_ext *ext));
88#endif
89
90#ifdef _KERNEL
91static 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 */
102void
103kdebug_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;
d1e348cf
A
176 case SADB_EXT_SESSION_ID:
177 kdebug_sadb_session_id(ext);
178 break;
179 case SADB_EXT_SASTAT:
180 kdebug_sadb_sastat(ext);
181 break;
52b7d2ce
A
182#ifdef SADB_X_EXT_NAT_T_TYPE
183 case SADB_X_EXT_NAT_T_TYPE:
184 kdebug_sadb_x_nat_t_type(ext);
185 break;
186 case SADB_X_EXT_NAT_T_SPORT:
187 case SADB_X_EXT_NAT_T_DPORT:
188 kdebug_sadb_x_nat_t_port(ext);
189 break;
190 case SADB_X_EXT_NAT_T_OA:
191 kdebug_sadb_address(ext);
192 break;
193#endif
194 default:
195 printf("kdebug_sadb: invalid ext_type %u was passed.\n",
196 ext->sadb_ext_type);
197 return;
198 }
199
200 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
201 tlen -= extlen;
202 ext = (void *)((caddr_t)(void *)ext + extlen);
203 }
204
205 return;
206}
207
208static void
209kdebug_sadb_prop(ext)
210 struct sadb_ext *ext;
211{
212 struct sadb_prop *prop = (void *)ext;
213 struct sadb_comb *comb;
214 int len;
215
216 /* sanity check */
217 if (ext == NULL)
218 panic("kdebug_sadb_prop: NULL pointer was passed.\n");
219
220 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
221 / sizeof(*comb);
222 comb = (void *)(prop + 1);
223 printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
224
225 while (len--) {
226 printf("sadb_comb{ auth=%u encrypt=%u "
227 "flags=0x%04x reserved=0x%08x\n",
228 comb->sadb_comb_auth, comb->sadb_comb_encrypt,
229 comb->sadb_comb_flags, comb->sadb_comb_reserved);
230
231 printf(" auth_minbits=%u auth_maxbits=%u "
232 "encrypt_minbits=%u encrypt_maxbits=%u\n",
233 comb->sadb_comb_auth_minbits,
234 comb->sadb_comb_auth_maxbits,
235 comb->sadb_comb_encrypt_minbits,
236 comb->sadb_comb_encrypt_maxbits);
237
238 printf(" soft_alloc=%u hard_alloc=%u "
239 "soft_bytes=%lu hard_bytes=%lu\n",
240 comb->sadb_comb_soft_allocations,
241 comb->sadb_comb_hard_allocations,
242 (unsigned long)comb->sadb_comb_soft_bytes,
243 (unsigned long)comb->sadb_comb_hard_bytes);
244
245 printf(" soft_alloc=%lu hard_alloc=%lu "
246 "soft_bytes=%lu hard_bytes=%lu }\n",
247 (unsigned long)comb->sadb_comb_soft_addtime,
248 (unsigned long)comb->sadb_comb_hard_addtime,
249 (unsigned long)comb->sadb_comb_soft_usetime,
250 (unsigned long)comb->sadb_comb_hard_usetime);
251 comb++;
252 }
253 printf("}\n");
254
255 return;
256}
257
258static void
259kdebug_sadb_identity(ext)
260 struct sadb_ext *ext;
261{
262 struct sadb_ident *id = (void *)ext;
263 int len;
264
265 /* sanity check */
266 if (ext == NULL)
267 panic("kdebug_sadb_identity: NULL pointer was passed.\n");
268
269 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
270 printf("sadb_ident_%s{",
271 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
272 switch (id->sadb_ident_type) {
273 default:
274 printf(" type=%d id=%lu",
275 id->sadb_ident_type, (u_long)id->sadb_ident_id);
276 if (len) {
277#ifdef _KERNEL
278 ipsec_hexdump((caddr_t)(id + 1), len); /*XXX cast ?*/
279#else
280 char *p, *ep;
281 printf("\n str=\"");
282 p = (void *)(id + 1);
283 ep = p + len;
284 for (/*nothing*/; *p && p < ep; p++) {
285 if (isprint((int)*p))
286 printf("%c", *p & 0xff);
287 else
288 printf("\\%03o", *p & 0xff);
289 }
290#endif
291 printf("\"");
292 }
293 break;
294 }
295
296 printf(" }\n");
297
298 return;
299}
300
301static void
302kdebug_sadb_supported(ext)
303 struct sadb_ext *ext;
304{
305 struct sadb_supported *sup = (void *)ext;
306 struct sadb_alg *alg;
307 int len;
308
309 /* sanity check */
310 if (ext == NULL)
311 panic("kdebug_sadb_supported: NULL pointer was passed.\n");
312
313 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
314 / sizeof(*alg);
315 alg = (void *)(sup + 1);
316 printf("sadb_sup{\n");
317 while (len--) {
318 printf(" { id=%d ivlen=%d min=%d max=%d }\n",
319 alg->sadb_alg_id, alg->sadb_alg_ivlen,
320 alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
321 alg++;
322 }
323 printf("}\n");
324
325 return;
326}
327
328static void
329kdebug_sadb_lifetime(ext)
330 struct sadb_ext *ext;
331{
332 struct sadb_lifetime *lft = (void *)ext;
333
334 /* sanity check */
335 if (ext == NULL)
336 printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
337
338 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
339 lft->sadb_lifetime_allocations,
340 (u_int32_t)lft->sadb_lifetime_bytes);
341 printf(" addtime=%u, usetime=%u }\n",
342 (u_int32_t)lft->sadb_lifetime_addtime,
343 (u_int32_t)lft->sadb_lifetime_usetime);
344
345 return;
346}
347
348static void
349kdebug_sadb_sa(ext)
350 struct sadb_ext *ext;
351{
352 struct sadb_sa *sa = (void *)ext;
353
354 /* sanity check */
355 if (ext == NULL)
356 panic("kdebug_sadb_sa: NULL pointer was passed.\n");
357
358 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
359 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
360 sa->sadb_sa_state);
361 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
362 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
363
364 return;
365}
366
367static void
368kdebug_sadb_address(ext)
369 struct sadb_ext *ext;
370{
371 struct sadb_address *addr = (void *)ext;
372
373 /* sanity check */
374 if (ext == NULL)
375 panic("kdebug_sadb_address: NULL pointer was passed.\n");
376
377 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
378 addr->sadb_address_proto, addr->sadb_address_prefixlen,
379 ((u_char *)(void *)&addr->sadb_address_reserved)[0],
380 ((u_char *)(void *)&addr->sadb_address_reserved)[1]);
381
382 kdebug_sockaddr((void *)((caddr_t)(void *)ext + sizeof(*addr)));
383
384 return;
385}
386
387static void
388kdebug_sadb_key(ext)
389 struct sadb_ext *ext;
390{
391 struct sadb_key *key = (void *)ext;
392
393 /* sanity check */
394 if (ext == NULL)
395 panic("kdebug_sadb_key: NULL pointer was passed.\n");
396
397 printf("sadb_key{ bits=%u reserved=%u\n",
398 key->sadb_key_bits, key->sadb_key_reserved);
399 printf(" key=");
400
401 /* sanity check 2 */
402 if (((uint32_t)key->sadb_key_bits >> 3) >
403 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
404 printf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
405 (uint32_t)key->sadb_key_bits >> 3,
406 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
407 }
408
409 ipsec_hexdump(key + sizeof(struct sadb_key),
410 (int)((uint32_t)key->sadb_key_bits >> 3));
411 printf(" }\n");
412 return;
413}
414
415static void
416kdebug_sadb_x_sa2(ext)
417 struct sadb_ext *ext;
418{
419 struct sadb_x_sa2 *sa2 = (void *)ext;
420
421 /* sanity check */
422 if (ext == NULL)
423 panic("kdebug_sadb_x_sa2: NULL pointer was passed.\n");
424
425 printf("sadb_x_sa2{ mode=%u reqid=%u\n",
426 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
427 printf(" reserved1=%u reserved2=%u sequence=%u }\n",
428 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
429 sa2->sadb_x_sa2_sequence);
430
431 return;
432}
433
d1e348cf
A
434static void
435kdebug_sadb_session_id(ext)
436struct sadb_ext *ext;
437{
438 struct sadb_session_id *p = (__typeof__(p))ext;
439
440 /* sanity check */
441 if (ext == NULL) {
442 printf("kdebug_sadb_session_id: NULL pointer was passed.\n");
443 return;
444 }
445
446 printf("sadb_session_id{ id0=%llx, id1=%llx}\n",
447 p->sadb_session_id_v[0],
448 p->sadb_session_id_v[1]);
449}
450
451static void
452kdebug_sadb_sastat(ext)
453struct sadb_ext *ext;
454{
455 struct sadb_sastat *p = (__typeof__(p))ext;
456 struct sastat *stats;
457 int i;
458
459 /* sanity check */
460 if (ext == NULL) {
461 printf("kdebug_sadb_sastat: NULL pointer was passed.\n");
462 return;
463 }
464
465 printf("sadb_sastat{ dir=%u num=%u\n",
466 p->sadb_sastat_dir, p->sadb_sastat_list_len);
467 stats = (__typeof__(stats))(p + 1);
468 for (i = 0; i < p->sadb_sastat_list_len; i++) {
469 printf(" spi=%x,\n",
470 stats[i].spi);
471 }
472 printf("}\n");
473}
474
52b7d2ce
A
475void
476kdebug_sadb_x_policy(ext)
477 struct sadb_ext *ext;
478{
479 struct sadb_x_policy *xpl = (void *)ext;
480 struct sockaddr *addr;
481
482 /* sanity check */
483 if (ext == NULL)
484 panic("kdebug_sadb_x_policy: NULL pointer was passed.\n");
485
486#ifdef HAVE_PFKEY_POLICY_PRIORITY
487 printf("sadb_x_policy{ type=%u dir=%u id=%x priority=%u }\n",
488#else
489 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
490#endif
491 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
492#ifdef HAVE_PFKEY_POLICY_PRIORITY
493 xpl->sadb_x_policy_id, xpl->sadb_x_policy_priority);
494#else
495 xpl->sadb_x_policy_id);
496#endif
497
498 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
499 int tlen;
500 struct sadb_x_ipsecrequest *xisr;
501
502 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
503 xisr = (void *)(xpl + 1);
504
505 while (tlen > 0) {
506 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
507 xisr->sadb_x_ipsecrequest_len,
508 xisr->sadb_x_ipsecrequest_proto,
509 xisr->sadb_x_ipsecrequest_mode,
510 xisr->sadb_x_ipsecrequest_level,
511 xisr->sadb_x_ipsecrequest_reqid);
512
513 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
514 addr = (void *)(xisr + 1);
515 kdebug_sockaddr(addr);
516 addr = (void *)((caddr_t)(void *)addr
517 + sysdep_sa_len(addr));
518 kdebug_sockaddr(addr);
519 }
520
521 printf(" }\n");
522
523 /* prevent infinite loop */
524 if (xisr->sadb_x_ipsecrequest_len == 0) {
525 printf("kdebug_sadb_x_policy: wrong policy struct.\n");
526 return;
527 }
528 /* prevent overflow */
529 if (xisr->sadb_x_ipsecrequest_len > tlen) {
530 printf("invalid ipsec policy length\n");
531 return;
532 }
533
534 tlen -= xisr->sadb_x_ipsecrequest_len;
535
536 xisr = (void *)((caddr_t)(void *)xisr
537 + xisr->sadb_x_ipsecrequest_len);
538 }
539
540 if (tlen != 0)
541 panic("kdebug_sadb_x_policy: wrong policy struct.\n");
542 }
543
544 return;
545}
546
547#ifdef SADB_X_EXT_NAT_T_TYPE
548static void
549kdebug_sadb_x_nat_t_type(struct sadb_ext *ext)
550{
551 struct sadb_x_nat_t_type *ntt = (void *)ext;
552
553 /* sanity check */
554 if (ext == NULL)
555 panic("kdebug_sadb_x_nat_t_type: NULL pointer was passed.\n");
556
557 printf("sadb_x_nat_t_type{ type=%u }\n", ntt->sadb_x_nat_t_type_type);
558
559 return;
560}
561
562static void
563kdebug_sadb_x_nat_t_port(struct sadb_ext *ext)
564{
565 struct sadb_x_nat_t_port *ntp = (void *)ext;
566
567 /* sanity check */
568 if (ext == NULL)
569 panic("kdebug_sadb_x_nat_t_port: NULL pointer was passed.\n");
570
571 printf("sadb_x_nat_t_port{ port=%u }\n", ntohs(ntp->sadb_x_nat_t_port_port));
572
573 return;
574}
575#endif
576
577#ifdef _KERNEL
578/* %%%: about SPD and SAD */
579void
580kdebug_secpolicy(sp)
581 struct secpolicy *sp;
582{
583 /* sanity check */
584 if (sp == NULL)
585 panic("kdebug_secpolicy: NULL pointer was passed.\n");
586
587 printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
588 sp->refcnt, sp->state, sp->policy);
589
590 kdebug_secpolicyindex(&sp->spidx);
591
592 switch (sp->policy) {
593 case IPSEC_POLICY_DISCARD:
594 printf(" type=discard }\n");
595 break;
596 case IPSEC_POLICY_GENERATE:
597 printf(" type=generate }\n");
598 break;
599 case IPSEC_POLICY_NONE:
600 printf(" type=none }\n");
601 break;
602 case IPSEC_POLICY_IPSEC:
603 {
604 struct ipsecrequest *isr;
605 for (isr = sp->req; isr != NULL; isr = isr->next) {
606
607 printf(" level=%u\n", isr->level);
608 kdebug_secasindex(&isr->saidx);
609
610 if (isr->sav != NULL)
611 kdebug_secasv(isr->sav);
612 }
613 printf(" }\n");
614 }
615 break;
616 case IPSEC_POLICY_BYPASS:
617 printf(" type=bypass }\n");
618 break;
619 case IPSEC_POLICY_ENTRUST:
620 printf(" type=entrust }\n");
621 break;
622 default:
623 printf("kdebug_secpolicy: Invalid policy found. %d\n",
624 sp->policy);
625 break;
626 }
627
628 return;
629}
630
631void
632kdebug_secpolicyindex(spidx)
633 struct secpolicyindex *spidx;
634{
635 /* sanity check */
636 if (spidx == NULL)
637 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
638
639 printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
640 spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
641
642 ipsec_hexdump((caddr_t)&spidx->src,
643 sysdep_sa_len((struct sockaddr *)&spidx->src));
644 printf("\n");
645 ipsec_hexdump((caddr_t)&spidx->dst,
646 sysdep_sa_len((struct sockaddr *)&spidx->dst));
647 printf("}\n");
648
649 return;
650}
651
652void
653kdebug_secasindex(saidx)
654 struct secasindex *saidx;
655{
656 /* sanity check */
657 if (saidx == NULL)
658 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
659
660 printf("secasindex{ mode=%u proto=%u\n",
661 saidx->mode, saidx->proto);
662
663 ipsec_hexdump((caddr_t)&saidx->src,
664 sysdep_sa_len((struct sockaddr *)&saidx->src));
665 printf("\n");
666 ipsec_hexdump((caddr_t)&saidx->dst,
667 sysdep_sa_len((struct sockaddr *)&saidx->dst));
668 printf("\n");
669
670 return;
671}
672
673void
674kdebug_secasv(sav)
675 struct secasvar *sav;
676{
677 /* sanity check */
678 if (sav == NULL)
679 panic("kdebug_secasv: NULL pointer was passed.\n");
680
681 printf("secas{");
682 kdebug_secasindex(&sav->sah->saidx);
683
684 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
685 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
686 printf(" spi=%u flags=%u\n",
687 (u_int32_t)ntohl(sav->spi), sav->flags);
688
689 if (sav->key_auth != NULL)
690 kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
691 if (sav->key_enc != NULL)
692 kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
693 if (sav->iv != NULL) {
694 printf(" iv=");
695 ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
696 printf("\n");
697 }
698
699 if (sav->replay != NULL)
700 kdebug_secreplay(sav->replay);
701 if (sav->lft_c != NULL)
702 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
703 if (sav->lft_h != NULL)
704 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
705 if (sav->lft_s != NULL)
706 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
707
708#if notyet
709 /* XXX: misc[123] ? */
710#endif
711
712 return;
713}
714
715static void
716kdebug_secreplay(rpl)
717 struct secreplay *rpl;
718{
719 int len, l;
720
721 /* sanity check */
722 if (rpl == NULL)
723 panic("kdebug_secreplay: NULL pointer was passed.\n");
724
725 printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
726 rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
727
728 if (rpl->bitmap == NULL) {
729 printf(" }\n");
730 return;
731 }
732
733 printf("\n bitmap { ");
734
735 for (len = 0; len < rpl->wsize; len++) {
736 for (l = 7; l >= 0; l--)
737 printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
738 }
739 printf(" }\n");
740
741 return;
742}
743
744void
745kdebug_mbufhdr(m)
746 struct mbuf *m;
747{
748 /* sanity check */
749 if (m == NULL)
750 return;
751
752 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
753 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
754 m, m->m_next, m->m_nextpkt, m->m_data,
755 m->m_len, m->m_type, m->m_flags);
756
757 if (m->m_flags & M_PKTHDR) {
758 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
759 m->m_pkthdr.len, m->m_pkthdr.rcvif);
760 }
761
762#ifdef __FreeBSD__
763 if (m->m_flags & M_EXT) {
764 printf(" m_ext{ ext_buf:%p ext_free:%p "
765 "ext_size:%u ext_ref:%p }\n",
766 m->m_ext.ext_buf, m->m_ext.ext_free,
767 m->m_ext.ext_size, m->m_ext.ext_ref);
768 }
769#endif
770
771 return;
772}
773
774void
775kdebug_mbuf(m0)
776 struct mbuf *m0;
777{
778 struct mbuf *m = m0;
779 int i, j;
780
781 for (j = 0; m; m = m->m_next) {
782 kdebug_mbufhdr(m);
783 printf(" m_data:\n");
784 for (i = 0; i < m->m_len; i++) {
785 if (i && i % 32 == 0)
786 printf("\n");
787 if (i % 4 == 0)
788 printf(" ");
789 printf("%02x", mtod(m, u_char *)[i]);
790 j++;
791 }
792 printf("\n");
793 }
794
795 return;
796}
797#endif /* _KERNEL */
798
799static void
800kdebug_sockaddr(addr)
801 struct sockaddr *addr;
802{
803 struct sockaddr_in *sin4;
804#ifdef INET6
805 struct sockaddr_in6 *sin6;
806#endif
807
808 /* sanity check */
809 if (addr == NULL)
810 panic("kdebug_sockaddr: NULL pointer was passed.\n");
811
812 /* NOTE: We deal with port number as host byte order. */
813 printf("sockaddr{ len=%u family=%u", sysdep_sa_len(addr), addr->sa_family);
814
815 switch (addr->sa_family) {
816 case AF_INET:
817 sin4 = (void *)addr;
818 printf(" port=%u\n", ntohs(sin4->sin_port));
819 ipsec_hexdump(&sin4->sin_addr, sizeof(sin4->sin_addr));
820 break;
821#ifdef INET6
822 case AF_INET6:
823 sin6 = (void *)addr;
824 printf(" port=%u\n", ntohs(sin6->sin6_port));
825 printf(" flowinfo=0x%08x, scope_id=0x%08x\n",
826 sin6->sin6_flowinfo, sin6->sin6_scope_id);
827 ipsec_hexdump(&sin6->sin6_addr, sizeof(sin6->sin6_addr));
828 break;
829#endif
830 }
831
832 printf(" }\n");
833
834 return;
835}
836
837void
838ipsec_bindump(buf, len)
839 caddr_t buf;
840 int len;
841{
842 int i;
843
844 for (i = 0; i < len; i++)
845 printf("%c", (unsigned char)buf[i]);
846
847 return;
848}
849
850
851void
852ipsec_hexdump(buf, len)
853 const void *buf;
854 int len;
855{
856 int i;
857
858 for (i = 0; i < len; i++) {
859 if (i != 0 && i % 32 == 0) printf("\n");
860 if (i % 4 == 0) printf(" ");
861 printf("%02x", ((const unsigned char *)buf)[i]);
862 }
863#if 0
864 if (i % 32 != 0) printf("\n");
865#endif
866
867 return;
868}