1 /* $FreeBSD: src/sys/netkey/key_debug.c,v 1.10.2.5 2002/04/28 05:40:28 suz Exp $ */
2 /* $KAME: key_debug.c,v 1.26 2001/06/27 10:46:50 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
33 #include <sys/types.h>
34 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/queue.h>
40 #include <sys/socket.h>
42 #include <net/route.h>
44 #include <netkey/key_var.h>
45 #include <netkey/key_debug.h>
47 #include <netinet/in.h>
48 #include <netinet6/ipsec.h>
56 #if !defined(KERNEL) || (defined(KERNEL) && defined(IPSEC_DEBUG))
58 static void kdebug_sadb_prop(struct sadb_ext
*);
59 static void kdebug_sadb_identity(struct sadb_ext
*);
60 static void kdebug_sadb_supported(struct sadb_ext
*);
61 static void kdebug_sadb_lifetime(struct sadb_ext
*);
62 static void kdebug_sadb_sa(struct sadb_ext
*);
63 static void kdebug_sadb_address(struct sadb_ext
*);
64 static void kdebug_sadb_key(struct sadb_ext
*);
65 static void kdebug_sadb_x_sa2(struct sadb_ext
*);
68 static void kdebug_secreplay(struct secreplay
*);
72 #define panic(param) { printf(param); exit(-1); }
75 /* NOTE: host byte order */
77 /* %%%: about struct sadb_msg */
80 struct sadb_msg
*base
;
87 panic("kdebug_sadb: NULL pointer was passed.\n");
89 printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
90 base
->sadb_msg_version
, base
->sadb_msg_type
,
91 base
->sadb_msg_errno
, base
->sadb_msg_satype
);
92 printf(" len=%u reserved=%u seq=%u pid=%u\n",
93 base
->sadb_msg_len
, base
->sadb_msg_reserved
,
94 base
->sadb_msg_seq
, base
->sadb_msg_pid
);
96 tlen
= PFKEY_UNUNIT64(base
->sadb_msg_len
) - sizeof(struct sadb_msg
);
97 ext
= (struct sadb_ext
*)((caddr_t
)base
+ sizeof(struct sadb_msg
));
100 printf("sadb_ext{ len=%u type=%u }\n",
101 ext
->sadb_ext_len
, ext
->sadb_ext_type
);
103 if (ext
->sadb_ext_len
== 0) {
104 printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
107 if (ext
->sadb_ext_len
> tlen
) {
108 printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
112 switch (ext
->sadb_ext_type
) {
116 case SADB_EXT_LIFETIME_CURRENT
:
117 case SADB_EXT_LIFETIME_HARD
:
118 case SADB_EXT_LIFETIME_SOFT
:
119 kdebug_sadb_lifetime(ext
);
121 case SADB_EXT_ADDRESS_SRC
:
122 case SADB_EXT_ADDRESS_DST
:
123 case SADB_EXT_ADDRESS_PROXY
:
124 kdebug_sadb_address(ext
);
126 case SADB_EXT_KEY_AUTH
:
127 case SADB_EXT_KEY_ENCRYPT
:
128 kdebug_sadb_key(ext
);
130 case SADB_EXT_IDENTITY_SRC
:
131 case SADB_EXT_IDENTITY_DST
:
132 kdebug_sadb_identity(ext
);
134 case SADB_EXT_SENSITIVITY
:
136 case SADB_EXT_PROPOSAL
:
137 kdebug_sadb_prop(ext
);
139 case SADB_EXT_SUPPORTED_AUTH
:
140 case SADB_EXT_SUPPORTED_ENCRYPT
:
141 kdebug_sadb_supported(ext
);
143 case SADB_EXT_SPIRANGE
:
144 case SADB_X_EXT_KMPRIVATE
:
146 case SADB_X_EXT_POLICY
:
147 kdebug_sadb_x_policy(ext
);
150 kdebug_sadb_x_sa2(ext
);
153 printf("kdebug_sadb: invalid ext_type %u was passed.\n",
158 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
160 ext
= (struct sadb_ext
*)((caddr_t
)ext
+ extlen
);
167 kdebug_sadb_prop(ext
)
168 struct sadb_ext
*ext
;
170 struct sadb_prop
*prop
= (struct sadb_prop
*)ext
;
171 struct sadb_comb
*comb
;
176 panic("kdebug_sadb_prop: NULL pointer was passed.\n");
178 len
= (PFKEY_UNUNIT64(prop
->sadb_prop_len
) - sizeof(*prop
))
180 comb
= (struct sadb_comb
*)(prop
+ 1);
181 printf("sadb_prop{ replay=%u\n", prop
->sadb_prop_replay
);
184 printf("sadb_comb{ auth=%u encrypt=%u "
185 "flags=0x%04x reserved=0x%08x\n",
186 comb
->sadb_comb_auth
, comb
->sadb_comb_encrypt
,
187 comb
->sadb_comb_flags
, comb
->sadb_comb_reserved
);
189 printf(" auth_minbits=%u auth_maxbits=%u "
190 "encrypt_minbits=%u encrypt_maxbits=%u\n",
191 comb
->sadb_comb_auth_minbits
,
192 comb
->sadb_comb_auth_maxbits
,
193 comb
->sadb_comb_encrypt_minbits
,
194 comb
->sadb_comb_encrypt_maxbits
);
196 printf(" soft_alloc=%u hard_alloc=%u "
197 "soft_bytes=%lu hard_bytes=%lu\n",
198 comb
->sadb_comb_soft_allocations
,
199 comb
->sadb_comb_hard_allocations
,
200 (unsigned long)comb
->sadb_comb_soft_bytes
,
201 (unsigned long)comb
->sadb_comb_hard_bytes
);
203 printf(" soft_alloc=%lu hard_alloc=%lu "
204 "soft_bytes=%lu hard_bytes=%lu }\n",
205 (unsigned long)comb
->sadb_comb_soft_addtime
,
206 (unsigned long)comb
->sadb_comb_hard_addtime
,
207 (unsigned long)comb
->sadb_comb_soft_usetime
,
208 (unsigned long)comb
->sadb_comb_hard_usetime
);
217 kdebug_sadb_identity(ext
)
218 struct sadb_ext
*ext
;
220 struct sadb_ident
*id
= (struct sadb_ident
*)ext
;
225 panic("kdebug_sadb_identity: NULL pointer was passed.\n");
227 len
= PFKEY_UNUNIT64(id
->sadb_ident_len
) - sizeof(*id
);
228 printf("sadb_ident_%s{",
229 id
->sadb_ident_exttype
== SADB_EXT_IDENTITY_SRC
? "src" : "dst");
230 switch (id
->sadb_ident_type
) {
232 printf(" type=%d id=%lu",
233 id
->sadb_ident_type
, (u_long
)id
->sadb_ident_id
);
236 ipsec_hexdump((caddr_t
)(id
+ 1), len
); /*XXX cast ?*/
240 p
= (char *)(id
+ 1);
242 for (/*nothing*/; *p
&& p
< ep
; p
++) {
244 printf("%c", *p
& 0xff);
246 printf("\\%03o", *p
& 0xff);
260 kdebug_sadb_supported(ext
)
261 struct sadb_ext
*ext
;
263 struct sadb_supported
*sup
= (struct sadb_supported
*)ext
;
264 struct sadb_alg
*alg
;
269 panic("kdebug_sadb_supported: NULL pointer was passed.\n");
271 len
= (PFKEY_UNUNIT64(sup
->sadb_supported_len
) - sizeof(*sup
))
273 alg
= (struct sadb_alg
*)(sup
+ 1);
274 printf("sadb_sup{\n");
276 printf(" { id=%d ivlen=%d min=%d max=%d }\n",
277 alg
->sadb_alg_id
, alg
->sadb_alg_ivlen
,
278 alg
->sadb_alg_minbits
, alg
->sadb_alg_maxbits
);
287 kdebug_sadb_lifetime(ext
)
288 struct sadb_ext
*ext
;
290 struct sadb_lifetime
*lft
= (struct sadb_lifetime
*)ext
;
294 printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
296 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
297 lft
->sadb_lifetime_allocations
,
298 (u_int32_t
)lft
->sadb_lifetime_bytes
);
299 printf(" addtime=%u, usetime=%u }\n",
300 (u_int32_t
)lft
->sadb_lifetime_addtime
,
301 (u_int32_t
)lft
->sadb_lifetime_usetime
);
308 struct sadb_ext
*ext
;
310 struct sadb_sa
*sa
= (struct sadb_sa
*)ext
;
314 panic("kdebug_sadb_sa: NULL pointer was passed.\n");
316 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
317 (u_int32_t
)ntohl(sa
->sadb_sa_spi
), sa
->sadb_sa_replay
,
319 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
320 sa
->sadb_sa_auth
, sa
->sadb_sa_encrypt
, sa
->sadb_sa_flags
);
326 kdebug_sadb_address(ext
)
327 struct sadb_ext
*ext
;
329 struct sadb_address
*addr
= (struct sadb_address
*)ext
;
333 panic("kdebug_sadb_address: NULL pointer was passed.\n");
335 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
336 addr
->sadb_address_proto
, addr
->sadb_address_prefixlen
,
337 ((u_char
*)&addr
->sadb_address_reserved
)[0],
338 ((u_char
*)&addr
->sadb_address_reserved
)[1]);
340 kdebug_sockaddr((struct sockaddr
*)((caddr_t
)ext
+ sizeof(*addr
)));
347 struct sadb_ext
*ext
;
349 struct sadb_key
*key
= (struct sadb_key
*)ext
;
353 panic("kdebug_sadb_key: NULL pointer was passed.\n");
355 printf("sadb_key{ bits=%u reserved=%u\n",
356 key
->sadb_key_bits
, key
->sadb_key_reserved
);
360 if ((key
->sadb_key_bits
>> 3) >
361 (PFKEY_UNUNIT64(key
->sadb_key_len
) - sizeof(struct sadb_key
))) {
362 printf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
363 key
->sadb_key_bits
>> 3,
364 (long)PFKEY_UNUNIT64(key
->sadb_key_len
) - sizeof(struct sadb_key
));
367 ipsec_hexdump((caddr_t
)key
+ sizeof(struct sadb_key
),
368 key
->sadb_key_bits
>> 3);
374 kdebug_sadb_x_sa2(ext
)
375 struct sadb_ext
*ext
;
377 struct sadb_x_sa2
*sa2
= (struct sadb_x_sa2
*)ext
;
381 panic("kdebug_sadb_x_sa2: NULL pointer was passed.\n");
383 printf("sadb_x_sa2{ mode=%u reqid=%u\n",
384 sa2
->sadb_x_sa2_mode
, sa2
->sadb_x_sa2_reqid
);
385 printf(" reserved1=%u reserved2=%u sequence=%u }\n",
386 sa2
->sadb_x_sa2_reserved1
, sa2
->sadb_x_sa2_reserved2
,
387 sa2
->sadb_x_sa2_sequence
);
393 kdebug_sadb_x_policy(ext
)
394 struct sadb_ext
*ext
;
396 struct sadb_x_policy
*xpl
= (struct sadb_x_policy
*)ext
;
397 struct sockaddr
*addr
;
401 panic("kdebug_sadb_x_policy: NULL pointer was passed.\n");
403 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
404 xpl
->sadb_x_policy_type
, xpl
->sadb_x_policy_dir
,
405 xpl
->sadb_x_policy_id
);
407 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
) {
409 struct sadb_x_ipsecrequest
*xisr
;
411 tlen
= PFKEY_UNUNIT64(xpl
->sadb_x_policy_len
) - sizeof(*xpl
);
412 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
415 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
416 xisr
->sadb_x_ipsecrequest_len
,
417 xisr
->sadb_x_ipsecrequest_proto
,
418 xisr
->sadb_x_ipsecrequest_mode
,
419 xisr
->sadb_x_ipsecrequest_level
,
420 xisr
->sadb_x_ipsecrequest_reqid
);
422 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
423 addr
= (struct sockaddr
*)(xisr
+ 1);
424 kdebug_sockaddr(addr
);
425 addr
= (struct sockaddr
*)((caddr_t
)addr
427 kdebug_sockaddr(addr
);
432 /* prevent infinite loop */
433 if (xisr
->sadb_x_ipsecrequest_len
<= 0) {
434 printf("kdebug_sadb_x_policy: wrong policy struct.\n");
437 /* prevent overflow */
438 if (xisr
->sadb_x_ipsecrequest_len
> tlen
) {
439 printf("invalid ipsec policy length\n");
443 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
445 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
446 + xisr
->sadb_x_ipsecrequest_len
);
450 panic("kdebug_sadb_x_policy: wrong policy struct.\n");
457 /* %%%: about SPD and SAD */
460 struct secpolicy
*sp
;
464 panic("kdebug_secpolicy: NULL pointer was passed.\n");
466 printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
467 sp
->refcnt
, sp
->state
, sp
->policy
);
469 kdebug_secpolicyindex(&sp
->spidx
);
471 switch (sp
->policy
) {
472 case IPSEC_POLICY_DISCARD
:
473 printf(" type=discard }\n");
475 case IPSEC_POLICY_NONE
:
476 printf(" type=none }\n");
478 case IPSEC_POLICY_IPSEC
:
480 struct ipsecrequest
*isr
;
481 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
483 printf(" level=%u\n", isr
->level
);
484 kdebug_secasindex(&isr
->saidx
);
486 if (isr
->sav
!= NULL
)
487 kdebug_secasv(isr
->sav
);
492 case IPSEC_POLICY_BYPASS
:
493 printf(" type=bypass }\n");
495 case IPSEC_POLICY_ENTRUST
:
496 printf(" type=entrust }\n");
499 printf("kdebug_secpolicy: Invalid policy found. %d\n",
508 kdebug_secpolicyindex(spidx
)
509 struct secpolicyindex
*spidx
;
513 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
515 printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
516 spidx
->dir
, spidx
->prefs
, spidx
->prefd
, spidx
->ul_proto
);
518 ipsec_hexdump((caddr_t
)&spidx
->src
,
519 ((struct sockaddr
*)&spidx
->src
)->sa_len
);
521 ipsec_hexdump((caddr_t
)&spidx
->dst
,
522 ((struct sockaddr
*)&spidx
->dst
)->sa_len
);
529 kdebug_secasindex(saidx
)
530 struct secasindex
*saidx
;
534 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
536 printf("secasindex{ mode=%u proto=%u\n",
537 saidx
->mode
, saidx
->proto
);
539 ipsec_hexdump((caddr_t
)&saidx
->src
,
540 ((struct sockaddr
*)&saidx
->src
)->sa_len
);
542 ipsec_hexdump((caddr_t
)&saidx
->dst
,
543 ((struct sockaddr
*)&saidx
->dst
)->sa_len
);
551 struct secasvar
*sav
;
555 panic("kdebug_secasv: NULL pointer was passed.\n");
558 kdebug_secasindex(&sav
->sah
->saidx
);
560 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
561 sav
->refcnt
, sav
->state
, sav
->alg_auth
, sav
->alg_enc
);
562 printf(" spi=%u flags=%u\n",
563 (u_int32_t
)ntohl(sav
->spi
), sav
->flags
);
565 if (sav
->key_auth
!= NULL
)
566 kdebug_sadb_key((struct sadb_ext
*)sav
->key_auth
);
567 if (sav
->key_enc
!= NULL
)
568 kdebug_sadb_key((struct sadb_ext
*)sav
->key_enc
);
569 if (sav
->iv
!= NULL
) {
571 ipsec_hexdump(sav
->iv
, sav
->ivlen
? sav
->ivlen
: 8);
575 if (sav
->replay
!= NULL
)
576 kdebug_secreplay(sav
->replay
);
577 if (sav
->lft_c
!= NULL
)
578 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_c
);
579 if (sav
->lft_h
!= NULL
)
580 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_h
);
581 if (sav
->lft_s
!= NULL
)
582 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_s
);
585 /* XXX: misc[123] ? */
592 kdebug_secreplay(rpl
)
593 struct secreplay
*rpl
;
599 panic("kdebug_secreplay: NULL pointer was passed.\n");
601 printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
602 rpl
->count
, rpl
->wsize
, rpl
->seq
, rpl
->lastseq
);
604 if (rpl
->bitmap
== NULL
) {
609 printf("\n bitmap { ");
611 for (len
= 0; len
< rpl
->wsize
; len
++) {
612 for (l
= 7; l
>= 0; l
--)
613 printf("%u", (((rpl
->bitmap
)[len
] >> l
) & 1) ? 1 : 0);
628 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
629 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
630 m
, m
->m_next
, m
->m_nextpkt
, m
->m_data
,
631 m
->m_len
, m
->m_type
, m
->m_flags
);
633 if (m
->m_flags
& M_PKTHDR
) {
634 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
635 m
->m_pkthdr
.len
, m
->m_pkthdr
.rcvif
);
638 if (m
->m_flags
& M_EXT
) {
639 printf(" m_ext{ ext_buf:%p ext_free:%p "
640 "ext_size:%u ext_ref:%p }\n",
641 m
->m_ext
.ext_buf
, m
->m_ext
.ext_free
,
642 m
->m_ext
.ext_size
, m
->m_ext
.ext_refs
);
655 for (j
= 0; m
; m
= m
->m_next
) {
657 printf(" m_data:\n");
658 for (i
= 0; i
< m
->m_len
; i
++) {
659 if (i
&& i
% 32 == 0)
663 printf("%02x", mtod(m
, u_char
*)[i
]);
674 kdebug_sockaddr(addr
)
675 struct sockaddr
*addr
;
677 struct sockaddr_in
*sin4
;
679 struct sockaddr_in6
*sin6
;
684 panic("kdebug_sockaddr: NULL pointer was passed.\n");
686 /* NOTE: We deal with port number as host byte order. */
687 printf("sockaddr{ len=%u family=%u", addr
->sa_len
, addr
->sa_family
);
689 switch (addr
->sa_family
) {
691 sin4
= (struct sockaddr_in
*)addr
;
692 printf(" port=%u\n", ntohs(sin4
->sin_port
));
693 ipsec_hexdump((caddr_t
)&sin4
->sin_addr
, sizeof(sin4
->sin_addr
));
697 sin6
= (struct sockaddr_in6
*)addr
;
698 printf(" port=%u\n", ntohs(sin6
->sin6_port
));
699 printf(" flowinfo=0x%08x, scope_id=0x%08x\n",
700 sin6
->sin6_flowinfo
, sin6
->sin6_scope_id
);
701 ipsec_hexdump((caddr_t
)&sin6
->sin6_addr
,
702 sizeof(sin6
->sin6_addr
));
713 ipsec_bindump(buf
, len
)
719 for (i
= 0; i
< len
; i
++)
720 printf("%c", (unsigned char)buf
[i
]);
727 ipsec_hexdump(buf
, len
)
733 for (i
= 0; i
< len
; i
++) {
734 if (i
!= 0 && i
% 32 == 0) printf("\n");
735 if (i
% 4 == 0) printf(" ");
736 printf("%02x", (unsigned char)buf
[i
]);
739 if (i
% 32 != 0) printf("\n");
745 #endif /* !defined(KERNEL) || (defined(KERNEL) && defined(IPSEC_DEBUG)) */