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