2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 /* KAME @(#)$Id: key_debug.c,v 1.2 2000/09/14 20:35:26 lindak Exp $ */
37 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
42 #include <sys/types.h>
43 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <sys/socket.h>
50 #include <net/route.h>
52 #include <netkey/key_var.h>
53 #include <netkey/key_debug.h>
55 #include <netinet/in.h>
56 #include <netinet6/ipsec.h>
62 #endif /* defined(KERNEL) */
64 #if !defined(_KERNEL) || (defined(_KERNEL) && defined(IPSEC_DEBUG))
66 static void kdebug_sadb_prop
__P((struct sadb_ext
*));
67 static void kdebug_sadb_identity
__P((struct sadb_ext
*));
68 static void kdebug_sadb_supported
__P((struct sadb_ext
*));
69 static void kdebug_sadb_lifetime
__P((struct sadb_ext
*));
70 static void kdebug_sadb_sa
__P((struct sadb_ext
*));
71 static void kdebug_sadb_address
__P((struct sadb_ext
*));
72 static void kdebug_sadb_key
__P((struct sadb_ext
*));
75 static void kdebug_secreplay
__P((struct secreplay
*));
79 #define panic(param) { printf(param); exit(-1); }
82 /* NOTE: host byte order */
84 /* %%%: about struct sadb_msg */
87 struct sadb_msg
*base
;
94 panic("kdebug_sadb: NULL pointer was passed.\n");
96 printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
97 base
->sadb_msg_version
, base
->sadb_msg_type
,
98 base
->sadb_msg_errno
, base
->sadb_msg_satype
);
99 printf(" len=%u mode=%u seq=%u pid=%u reqid=%u\n",
100 base
->sadb_msg_len
, base
->sadb_msg_mode
,
101 base
->sadb_msg_seq
, base
->sadb_msg_pid
, base
->sadb_msg_reqid
);
102 printf(" reserved1=%u reserved2=%u\n",
103 base
->sadb_msg_reserved1
, base
->sadb_msg_reserved2
);
105 tlen
= PFKEY_UNUNIT64(base
->sadb_msg_len
) - sizeof(struct sadb_msg
);
106 ext
= (struct sadb_ext
*)((caddr_t
)base
+ sizeof(struct sadb_msg
));
109 printf("sadb_ext{ len=%u type=%u }\n",
110 ext
->sadb_ext_len
, ext
->sadb_ext_type
);
112 if (ext
->sadb_ext_len
== 0) {
113 printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
116 if (ext
->sadb_ext_len
> tlen
) {
117 printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
121 switch (ext
->sadb_ext_type
) {
125 case SADB_EXT_LIFETIME_CURRENT
:
126 case SADB_EXT_LIFETIME_HARD
:
127 case SADB_EXT_LIFETIME_SOFT
:
128 kdebug_sadb_lifetime(ext
);
130 case SADB_EXT_ADDRESS_SRC
:
131 case SADB_EXT_ADDRESS_DST
:
132 case SADB_EXT_ADDRESS_PROXY
:
133 kdebug_sadb_address(ext
);
135 case SADB_EXT_KEY_AUTH
:
136 case SADB_EXT_KEY_ENCRYPT
:
137 kdebug_sadb_key(ext
);
139 case SADB_EXT_IDENTITY_SRC
:
140 case SADB_EXT_IDENTITY_DST
:
141 kdebug_sadb_identity(ext
);
143 case SADB_EXT_SENSITIVITY
:
145 case SADB_EXT_PROPOSAL
:
146 kdebug_sadb_prop(ext
);
148 case SADB_EXT_SUPPORTED_AUTH
:
149 case SADB_EXT_SUPPORTED_ENCRYPT
:
150 kdebug_sadb_supported(ext
);
152 case SADB_EXT_SPIRANGE
:
153 case SADB_X_EXT_KMPRIVATE
:
155 case SADB_X_EXT_POLICY
:
156 kdebug_sadb_x_policy(ext
);
159 printf("kdebug_sadb: invalid ext_type %u was passed.\n",
164 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
166 ext
= (struct sadb_ext
*)((caddr_t
)ext
+ extlen
);
173 kdebug_sadb_prop(ext
)
174 struct sadb_ext
*ext
;
176 struct sadb_prop
*prop
= (struct sadb_prop
*)ext
;
177 struct sadb_comb
*comb
;
182 panic("kdebug_sadb_prop: NULL pointer was passed.\n");
184 len
= (PFKEY_UNUNIT64(prop
->sadb_prop_len
) - sizeof(*prop
))
186 comb
= (struct sadb_comb
*)(prop
+ 1);
187 printf("sadb_prop{ replay=%u\n", prop
->sadb_prop_replay
);
190 printf("sadb_comb{ auth=%u encrypt=%u "
191 "flags=0x%04x reserved=0x%08x\n",
192 comb
->sadb_comb_auth
, comb
->sadb_comb_encrypt
,
193 comb
->sadb_comb_flags
, comb
->sadb_comb_reserved
);
195 printf(" auth_minbits=%u auth_maxbits=%u "
196 "encrypt_minbits=%u encrypt_maxbits=%u\n",
197 comb
->sadb_comb_auth_minbits
,
198 comb
->sadb_comb_auth_maxbits
,
199 comb
->sadb_comb_encrypt_minbits
,
200 comb
->sadb_comb_encrypt_maxbits
);
202 printf(" soft_alloc=%u hard_alloc=%u "
203 "soft_bytes=%lu hard_bytes=%lu\n",
204 comb
->sadb_comb_soft_allocations
,
205 comb
->sadb_comb_hard_allocations
,
206 (unsigned long)comb
->sadb_comb_soft_bytes
,
207 (unsigned long)comb
->sadb_comb_hard_bytes
);
209 printf(" soft_alloc=%lu hard_alloc=%lu "
210 "soft_bytes=%lu hard_bytes=%lu }\n",
211 (unsigned long)comb
->sadb_comb_soft_addtime
,
212 (unsigned long)comb
->sadb_comb_hard_addtime
,
213 (unsigned long)comb
->sadb_comb_soft_usetime
,
214 (unsigned long)comb
->sadb_comb_hard_usetime
);
223 kdebug_sadb_identity(ext
)
224 struct sadb_ext
*ext
;
226 struct sadb_ident
*id
= (struct sadb_ident
*)ext
;
228 union sadb_x_ident_id
*aid
;
232 panic("kdebug_sadb_identity: NULL pointer was passed.\n");
234 len
= PFKEY_UNUNIT64(id
->sadb_ident_len
) - sizeof(*id
);
235 printf("sadb_ident_%s{",
236 id
->sadb_ident_exttype
== SADB_EXT_IDENTITY_SRC
? "src" : "dst");
237 switch (id
->sadb_ident_type
) {
238 case SADB_X_IDENTTYPE_ADDR
:
239 aid
= (union sadb_x_ident_id
*)&id
->sadb_ident_id
;
241 printf(" type=%d prefix=%u ul_proto=%u\n",
243 aid
->sadb_x_ident_id_addr
.prefix
,
244 aid
->sadb_x_ident_id_addr
.ul_proto
);
245 kdebug_sockaddr((struct sockaddr
*)(id
+ 1));
249 printf(" type=%d id=%lu",
250 id
->sadb_ident_type
, (u_long
)id
->sadb_ident_id
);
253 ipsec_hexdump((caddr_t
)(id
+ 1), len
); /*XXX cast ?*/
257 p
= (char *)(id
+ 1);
259 for (/*nothing*/; *p
&& p
< ep
; p
++) {
261 printf("%c", *p
& 0xff);
263 printf("\\%03o", *p
& 0xff);
277 kdebug_sadb_supported(ext
)
278 struct sadb_ext
*ext
;
280 struct sadb_supported
*sup
= (struct sadb_supported
*)ext
;
281 struct sadb_alg
*alg
;
286 panic("kdebug_sadb_supported: NULL pointer was passed.\n");
288 len
= (PFKEY_UNUNIT64(sup
->sadb_supported_len
) - sizeof(*sup
))
290 alg
= (struct sadb_alg
*)(sup
+ 1);
291 printf("sadb_sup{\n");
293 printf(" { id=%d ivlen=%d min=%d max=%d }\n",
294 alg
->sadb_alg_id
, alg
->sadb_alg_ivlen
,
295 alg
->sadb_alg_minbits
, alg
->sadb_alg_maxbits
);
304 kdebug_sadb_lifetime(ext
)
305 struct sadb_ext
*ext
;
307 struct sadb_lifetime
*lft
= (struct sadb_lifetime
*)ext
;
311 printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
313 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
314 lft
->sadb_lifetime_allocations
,
315 (u_int32_t
)lft
->sadb_lifetime_bytes
);
316 printf(" addtime=%u, usetime=%u }\n",
317 (u_int32_t
)lft
->sadb_lifetime_addtime
,
318 (u_int32_t
)lft
->sadb_lifetime_usetime
);
325 struct sadb_ext
*ext
;
327 struct sadb_sa
*sa
= (struct sadb_sa
*)ext
;
331 panic("kdebug_sadb_sa: NULL pointer was passed.\n");
333 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
334 (u_int32_t
)ntohl(sa
->sadb_sa_spi
), sa
->sadb_sa_replay
,
336 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
337 sa
->sadb_sa_auth
, sa
->sadb_sa_encrypt
, sa
->sadb_sa_flags
);
343 kdebug_sadb_address(ext
)
344 struct sadb_ext
*ext
;
346 struct sadb_address
*addr
= (struct sadb_address
*)ext
;
350 panic("kdebug_sadb_address: NULL pointer was passed.\n");
352 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
353 addr
->sadb_address_proto
, addr
->sadb_address_prefixlen
,
354 ((u_char
*)&addr
->sadb_address_reserved
)[0],
355 ((u_char
*)&addr
->sadb_address_reserved
)[1]);
357 kdebug_sockaddr((struct sockaddr
*)((caddr_t
)ext
+ sizeof(*addr
)));
364 struct sadb_ext
*ext
;
366 struct sadb_key
*key
= (struct sadb_key
*)ext
;
370 panic("kdebug_sadb_key: NULL pointer was passed.\n");
372 printf("sadb_key{ bits=%u reserved=%u\n",
373 key
->sadb_key_bits
, key
->sadb_key_reserved
);
377 if ((key
->sadb_key_bits
>> 3) >
378 (PFKEY_UNUNIT64(key
->sadb_key_len
) - sizeof(struct sadb_key
))) {
379 printf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
380 key
->sadb_key_bits
>> 3,
381 (long)PFKEY_UNUNIT64(key
->sadb_key_len
) - sizeof(struct sadb_key
));
384 ipsec_hexdump((caddr_t
)key
+ sizeof(struct sadb_key
),
385 key
->sadb_key_bits
>> 3);
391 kdebug_sadb_x_policy(ext
)
392 struct sadb_ext
*ext
;
394 struct sadb_x_policy
*xpl
= (struct sadb_x_policy
*)ext
;
395 struct sockaddr
*addr
;
399 panic("kdebug_sadb_x_policy: NULL pointer was passed.\n");
401 printf("sadb_x_policy{ type=%u dir=%u reserved=%x }\n",
402 xpl
->sadb_x_policy_type
, xpl
->sadb_x_policy_dir
,
403 xpl
->sadb_x_policy_reserved
);
405 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
) {
407 struct sadb_x_ipsecrequest
*xisr
;
409 tlen
= PFKEY_UNUNIT64(xpl
->sadb_x_policy_len
) - sizeof(*xpl
);
410 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
413 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
414 xisr
->sadb_x_ipsecrequest_len
,
415 xisr
->sadb_x_ipsecrequest_proto
,
416 xisr
->sadb_x_ipsecrequest_mode
,
417 xisr
->sadb_x_ipsecrequest_level
,
418 xisr
->sadb_x_ipsecrequest_reqid
);
420 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
421 addr
= (struct sockaddr
*)(xisr
+ 1);
422 kdebug_sockaddr(addr
);
423 addr
= (struct sockaddr
*)((caddr_t
)addr
425 kdebug_sockaddr(addr
);
430 /* prevent infinite loop */
431 if (xisr
->sadb_x_ipsecrequest_len
<= 0) {
432 printf("kdebug_sadb_x_policy: wrong policy struct.\n");
435 /* prevent overflow */
436 if (xisr
->sadb_x_ipsecrequest_len
> tlen
) {
437 printf("invalid ipsec policy length\n");
441 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
443 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
444 + xisr
->sadb_x_ipsecrequest_len
);
448 panic("kdebug_sadb_x_policy: wrong policy struct.\n");
455 /* %%%: about SPD and SAD */
458 struct secpolicy
*sp
;
462 panic("kdebug_secpolicy: NULL pointer was passed.\n");
464 printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
465 sp
->refcnt
, sp
->state
, sp
->policy
);
467 kdebug_secpolicyindex(&sp
->spidx
);
469 switch (sp
->policy
) {
470 case IPSEC_POLICY_DISCARD
:
471 printf(" type=discard }\n");
473 case IPSEC_POLICY_NONE
:
474 printf(" type=none }\n");
476 case IPSEC_POLICY_IPSEC
:
478 struct ipsecrequest
*isr
;
479 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
481 printf(" level=%u\n", isr
->level
);
482 kdebug_secasindex(&isr
->saidx
);
484 if (isr
->sav
!= NULL
)
485 kdebug_secasv(isr
->sav
);
490 case IPSEC_POLICY_BYPASS
:
491 printf(" type=bypass }\n");
493 case IPSEC_POLICY_ENTRUST
:
494 printf(" type=entrust }\n");
497 printf("kdebug_secpolicy: Invalid policy found. %d\n",
506 kdebug_secpolicyindex(spidx
)
507 struct secpolicyindex
*spidx
;
511 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
513 printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
514 spidx
->dir
, spidx
->prefs
, spidx
->prefd
, spidx
->ul_proto
);
516 ipsec_hexdump((caddr_t
)&spidx
->src
,
517 ((struct sockaddr
*)&spidx
->src
)->sa_len
);
519 ipsec_hexdump((caddr_t
)&spidx
->dst
,
520 ((struct sockaddr
*)&spidx
->dst
)->sa_len
);
527 kdebug_secasindex(saidx
)
528 struct secasindex
*saidx
;
532 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
534 printf("secasindex{ mode=%u proto=%u\n",
535 saidx
->mode
, saidx
->proto
);
537 ipsec_hexdump((caddr_t
)&saidx
->src
,
538 ((struct sockaddr
*)&saidx
->src
)->sa_len
);
540 ipsec_hexdump((caddr_t
)&saidx
->dst
,
541 ((struct sockaddr
*)&saidx
->dst
)->sa_len
);
549 struct secasvar
*sav
;
553 panic("kdebug_secasv: NULL pointer was passed.\n");
556 kdebug_secasindex(&sav
->sah
->saidx
);
558 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
559 sav
->refcnt
, sav
->state
, sav
->alg_auth
, sav
->alg_enc
);
560 printf(" spi=%u flags=%u\n",
561 (u_int32_t
)ntohl(sav
->spi
), sav
->flags
);
563 if (sav
->key_auth
!= NULL
)
564 kdebug_sadb_key((struct sadb_ext
*)sav
->key_auth
);
565 if (sav
->key_enc
!= NULL
)
566 kdebug_sadb_key((struct sadb_ext
*)sav
->key_enc
);
567 if (sav
->iv
!= NULL
) {
569 ipsec_hexdump(sav
->iv
, sav
->ivlen
? sav
->ivlen
: 8);
573 if (sav
->replay
!= NULL
)
574 kdebug_secreplay(sav
->replay
);
575 if (sav
->lft_c
!= NULL
)
576 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_c
);
577 if (sav
->lft_h
!= NULL
)
578 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_h
);
579 if (sav
->lft_s
!= NULL
)
580 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_s
);
583 /* XXX: misc[123] ? */
590 kdebug_secreplay(rpl
)
591 struct secreplay
*rpl
;
597 panic("kdebug_secreplay: NULL pointer was passed.\n");
599 printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
600 rpl
->count
, rpl
->wsize
, rpl
->seq
, rpl
->lastseq
);
602 if (rpl
->bitmap
== NULL
) {
607 printf("\n bitmap { ");
609 for (len
= 0; len
< rpl
->wsize
; len
++) {
610 for (l
= 7; l
>= 0; l
--)
611 printf("%u", (((rpl
->bitmap
)[len
] >> l
) & 1) ? 1 : 0);
624 panic("debug_mbufhdr: NULL pointer was passed.\n");
626 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
627 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
628 m
, m
->m_next
, m
->m_nextpkt
, m
->m_data
,
629 m
->m_len
, m
->m_type
, m
->m_flags
);
631 if (m
->m_flags
& M_PKTHDR
) {
632 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
633 m
->m_pkthdr
.len
, m
->m_pkthdr
.rcvif
);
637 if (m
->m_flags
& M_EXT
) {
638 printf(" m_ext{ ext_buf:%p ext_free:%p "
639 "ext_size:%u ext_ref:%p }\n",
640 m
->m_ext
.ext_buf
, m
->m_ext
.ext_free
,
641 m
->m_ext
.ext_size
, m
->m_ext
.ext_ref
);
656 printf(" m_data=\n");
657 for (j
= 0; m
; m
= m
->m_next
) {
658 for (i
= 0; i
< m
->m_len
; i
++) {
659 if (i
!= 0 && i
% 32 == 0) printf("\n");
660 if (i
% 4 == 0) printf(" ");
661 printf("%02x", mtod(m
, u_char
*)[i
]);
673 kdebug_sockaddr(addr
)
674 struct sockaddr
*addr
;
678 panic("kdebug_sockaddr: NULL pointer was passed.\n");
680 /* NOTE: We deal with port number as host byte order. */
681 printf("sockaddr{ len=%u family=%u port=%u\n",
682 addr
->sa_len
, addr
->sa_family
, ntohs(_INPORTBYSA(addr
)));
685 if (addr
->sa_family
== PF_INET6
) {
686 struct sockaddr_in6
*in6
= (struct sockaddr_in6
*)addr
;
687 printf(" flowinfo=0x%08x, scope_id=0x%08x\n",
688 in6
->sin6_flowinfo
, in6
->sin6_scope_id
);
692 ipsec_hexdump(_INADDRBYSA(addr
), _INALENBYAF(addr
->sa_family
));
699 #endif /* !defined(KERNEL) || (defined(KERNEL) && defined(IPSEC_DEBUG)) */
702 ipsec_bindump(buf
, len
)
708 for (i
= 0; i
< len
; i
++)
709 printf("%c", (unsigned char)buf
[i
]);
716 ipsec_hexdump(buf
, len
)
722 for (i
= 0; i
< len
; i
++) {
723 if (i
!= 0 && i
% 32 == 0) printf("\n");
724 if (i
% 4 == 0) printf(" ");
725 printf("%02x", (unsigned char)buf
[i
]);
728 if (i
% 32 != 0) printf("\n");