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 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/queue.h>
37 #include <sys/socket.h>
39 #include <net/route.h>
41 #include <netkey/key_var.h>
42 #include <netkey/key_debug.h>
44 #include <netinet/in.h>
45 #include <netinet6/ipsec.h>
53 #if !defined(KERNEL) || (defined(KERNEL) && defined(IPSEC_DEBUG))
55 static void kdebug_sadb_prop
__P((struct sadb_ext
*));
56 static void kdebug_sadb_identity
__P((struct sadb_ext
*));
57 static void kdebug_sadb_supported
__P((struct sadb_ext
*));
58 static void kdebug_sadb_lifetime
__P((struct sadb_ext
*));
59 static void kdebug_sadb_sa
__P((struct sadb_ext
*));
60 static void kdebug_sadb_address
__P((struct sadb_ext
*));
61 static void kdebug_sadb_key
__P((struct sadb_ext
*));
62 static void kdebug_sadb_x_sa2
__P((struct sadb_ext
*));
65 static void kdebug_secreplay
__P((struct secreplay
*));
69 #define panic(param) { printf(param); exit(-1); }
72 /* NOTE: host byte order */
74 /* %%%: about struct sadb_msg */
77 struct sadb_msg
*base
;
84 panic("kdebug_sadb: NULL pointer was passed.\n");
86 printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
87 base
->sadb_msg_version
, base
->sadb_msg_type
,
88 base
->sadb_msg_errno
, base
->sadb_msg_satype
);
89 printf(" len=%u reserved=%u seq=%u pid=%u\n",
90 base
->sadb_msg_len
, base
->sadb_msg_reserved
,
91 base
->sadb_msg_seq
, base
->sadb_msg_pid
);
93 tlen
= PFKEY_UNUNIT64(base
->sadb_msg_len
) - sizeof(struct sadb_msg
);
94 ext
= (struct sadb_ext
*)((caddr_t
)base
+ sizeof(struct sadb_msg
));
97 printf("sadb_ext{ len=%u type=%u }\n",
98 ext
->sadb_ext_len
, ext
->sadb_ext_type
);
100 if (ext
->sadb_ext_len
== 0) {
101 printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
104 if (ext
->sadb_ext_len
> tlen
) {
105 printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
109 switch (ext
->sadb_ext_type
) {
113 case SADB_EXT_LIFETIME_CURRENT
:
114 case SADB_EXT_LIFETIME_HARD
:
115 case SADB_EXT_LIFETIME_SOFT
:
116 kdebug_sadb_lifetime(ext
);
118 case SADB_EXT_ADDRESS_SRC
:
119 case SADB_EXT_ADDRESS_DST
:
120 case SADB_EXT_ADDRESS_PROXY
:
121 kdebug_sadb_address(ext
);
123 case SADB_EXT_KEY_AUTH
:
124 case SADB_EXT_KEY_ENCRYPT
:
125 kdebug_sadb_key(ext
);
127 case SADB_EXT_IDENTITY_SRC
:
128 case SADB_EXT_IDENTITY_DST
:
129 kdebug_sadb_identity(ext
);
131 case SADB_EXT_SENSITIVITY
:
133 case SADB_EXT_PROPOSAL
:
134 kdebug_sadb_prop(ext
);
136 case SADB_EXT_SUPPORTED_AUTH
:
137 case SADB_EXT_SUPPORTED_ENCRYPT
:
138 kdebug_sadb_supported(ext
);
140 case SADB_EXT_SPIRANGE
:
141 case SADB_X_EXT_KMPRIVATE
:
143 case SADB_X_EXT_POLICY
:
144 kdebug_sadb_x_policy(ext
);
147 kdebug_sadb_x_sa2(ext
);
150 printf("kdebug_sadb: invalid ext_type %u was passed.\n",
155 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
157 ext
= (struct sadb_ext
*)((caddr_t
)ext
+ extlen
);
164 kdebug_sadb_prop(ext
)
165 struct sadb_ext
*ext
;
167 struct sadb_prop
*prop
= (struct sadb_prop
*)ext
;
168 struct sadb_comb
*comb
;
173 panic("kdebug_sadb_prop: NULL pointer was passed.\n");
175 len
= (PFKEY_UNUNIT64(prop
->sadb_prop_len
) - sizeof(*prop
))
177 comb
= (struct sadb_comb
*)(prop
+ 1);
178 printf("sadb_prop{ replay=%u\n", prop
->sadb_prop_replay
);
181 printf("sadb_comb{ auth=%u encrypt=%u "
182 "flags=0x%04x reserved=0x%08x\n",
183 comb
->sadb_comb_auth
, comb
->sadb_comb_encrypt
,
184 comb
->sadb_comb_flags
, comb
->sadb_comb_reserved
);
186 printf(" auth_minbits=%u auth_maxbits=%u "
187 "encrypt_minbits=%u encrypt_maxbits=%u\n",
188 comb
->sadb_comb_auth_minbits
,
189 comb
->sadb_comb_auth_maxbits
,
190 comb
->sadb_comb_encrypt_minbits
,
191 comb
->sadb_comb_encrypt_maxbits
);
193 printf(" soft_alloc=%u hard_alloc=%u "
194 "soft_bytes=%lu hard_bytes=%lu\n",
195 comb
->sadb_comb_soft_allocations
,
196 comb
->sadb_comb_hard_allocations
,
197 (unsigned long)comb
->sadb_comb_soft_bytes
,
198 (unsigned long)comb
->sadb_comb_hard_bytes
);
200 printf(" soft_alloc=%lu hard_alloc=%lu "
201 "soft_bytes=%lu hard_bytes=%lu }\n",
202 (unsigned long)comb
->sadb_comb_soft_addtime
,
203 (unsigned long)comb
->sadb_comb_hard_addtime
,
204 (unsigned long)comb
->sadb_comb_soft_usetime
,
205 (unsigned long)comb
->sadb_comb_hard_usetime
);
214 kdebug_sadb_identity(ext
)
215 struct sadb_ext
*ext
;
217 struct sadb_ident
*id
= (struct sadb_ident
*)ext
;
222 panic("kdebug_sadb_identity: NULL pointer was passed.\n");
224 len
= PFKEY_UNUNIT64(id
->sadb_ident_len
) - sizeof(*id
);
225 printf("sadb_ident_%s{",
226 id
->sadb_ident_exttype
== SADB_EXT_IDENTITY_SRC
? "src" : "dst");
227 switch (id
->sadb_ident_type
) {
229 printf(" type=%d id=%lu",
230 id
->sadb_ident_type
, (u_long
)id
->sadb_ident_id
);
233 ipsec_hexdump((caddr_t
)(id
+ 1), len
); /*XXX cast ?*/
237 p
= (char *)(id
+ 1);
239 for (/*nothing*/; *p
&& p
< ep
; p
++) {
241 printf("%c", *p
& 0xff);
243 printf("\\%03o", *p
& 0xff);
257 kdebug_sadb_supported(ext
)
258 struct sadb_ext
*ext
;
260 struct sadb_supported
*sup
= (struct sadb_supported
*)ext
;
261 struct sadb_alg
*alg
;
266 panic("kdebug_sadb_supported: NULL pointer was passed.\n");
268 len
= (PFKEY_UNUNIT64(sup
->sadb_supported_len
) - sizeof(*sup
))
270 alg
= (struct sadb_alg
*)(sup
+ 1);
271 printf("sadb_sup{\n");
273 printf(" { id=%d ivlen=%d min=%d max=%d }\n",
274 alg
->sadb_alg_id
, alg
->sadb_alg_ivlen
,
275 alg
->sadb_alg_minbits
, alg
->sadb_alg_maxbits
);
284 kdebug_sadb_lifetime(ext
)
285 struct sadb_ext
*ext
;
287 struct sadb_lifetime
*lft
= (struct sadb_lifetime
*)ext
;
291 printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
293 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
294 lft
->sadb_lifetime_allocations
,
295 (u_int32_t
)lft
->sadb_lifetime_bytes
);
296 printf(" addtime=%u, usetime=%u }\n",
297 (u_int32_t
)lft
->sadb_lifetime_addtime
,
298 (u_int32_t
)lft
->sadb_lifetime_usetime
);
305 struct sadb_ext
*ext
;
307 struct sadb_sa
*sa
= (struct sadb_sa
*)ext
;
311 panic("kdebug_sadb_sa: NULL pointer was passed.\n");
313 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
314 (u_int32_t
)ntohl(sa
->sadb_sa_spi
), sa
->sadb_sa_replay
,
316 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
317 sa
->sadb_sa_auth
, sa
->sadb_sa_encrypt
, sa
->sadb_sa_flags
);
323 kdebug_sadb_address(ext
)
324 struct sadb_ext
*ext
;
326 struct sadb_address
*addr
= (struct sadb_address
*)ext
;
330 panic("kdebug_sadb_address: NULL pointer was passed.\n");
332 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
333 addr
->sadb_address_proto
, addr
->sadb_address_prefixlen
,
334 ((u_char
*)&addr
->sadb_address_reserved
)[0],
335 ((u_char
*)&addr
->sadb_address_reserved
)[1]);
337 kdebug_sockaddr((struct sockaddr
*)((caddr_t
)ext
+ sizeof(*addr
)));
344 struct sadb_ext
*ext
;
346 struct sadb_key
*key
= (struct sadb_key
*)ext
;
350 panic("kdebug_sadb_key: NULL pointer was passed.\n");
352 printf("sadb_key{ bits=%u reserved=%u\n",
353 key
->sadb_key_bits
, key
->sadb_key_reserved
);
357 if ((key
->sadb_key_bits
>> 3) >
358 (PFKEY_UNUNIT64(key
->sadb_key_len
) - sizeof(struct sadb_key
))) {
359 printf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
360 key
->sadb_key_bits
>> 3,
361 (long)PFKEY_UNUNIT64(key
->sadb_key_len
) - sizeof(struct sadb_key
));
364 ipsec_hexdump((caddr_t
)key
+ sizeof(struct sadb_key
),
365 key
->sadb_key_bits
>> 3);
371 kdebug_sadb_x_sa2(ext
)
372 struct sadb_ext
*ext
;
374 struct sadb_x_sa2
*sa2
= (struct sadb_x_sa2
*)ext
;
378 panic("kdebug_sadb_x_sa2: NULL pointer was passed.\n");
380 printf("sadb_x_sa2{ mode=%u reqid=%u\n",
381 sa2
->sadb_x_sa2_mode
, sa2
->sadb_x_sa2_reqid
);
382 printf(" reserved1=%u reserved2=%u reserved3=%u }\n",
383 sa2
->sadb_x_sa2_reserved1
, sa2
->sadb_x_sa2_reserved1
,
384 sa2
->sadb_x_sa2_reserved1
);
390 kdebug_sadb_x_policy(ext
)
391 struct sadb_ext
*ext
;
393 struct sadb_x_policy
*xpl
= (struct sadb_x_policy
*)ext
;
394 struct sockaddr
*addr
;
398 panic("kdebug_sadb_x_policy: NULL pointer was passed.\n");
400 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
401 xpl
->sadb_x_policy_type
, xpl
->sadb_x_policy_dir
,
402 xpl
->sadb_x_policy_id
);
404 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
) {
406 struct sadb_x_ipsecrequest
*xisr
;
408 tlen
= PFKEY_UNUNIT64(xpl
->sadb_x_policy_len
) - sizeof(*xpl
);
409 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
412 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
413 xisr
->sadb_x_ipsecrequest_len
,
414 xisr
->sadb_x_ipsecrequest_proto
,
415 xisr
->sadb_x_ipsecrequest_mode
,
416 xisr
->sadb_x_ipsecrequest_level
,
417 xisr
->sadb_x_ipsecrequest_reqid
);
419 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
420 addr
= (struct sockaddr
*)(xisr
+ 1);
421 kdebug_sockaddr(addr
);
422 addr
= (struct sockaddr
*)((caddr_t
)addr
424 kdebug_sockaddr(addr
);
429 /* prevent infinite loop */
430 if (xisr
->sadb_x_ipsecrequest_len
<= 0) {
431 printf("kdebug_sadb_x_policy: wrong policy struct.\n");
434 /* prevent overflow */
435 if (xisr
->sadb_x_ipsecrequest_len
> tlen
) {
436 printf("invalid ipsec policy length\n");
440 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
442 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
443 + xisr
->sadb_x_ipsecrequest_len
);
447 panic("kdebug_sadb_x_policy: wrong policy struct.\n");
454 /* %%%: about SPD and SAD */
457 struct secpolicy
*sp
;
461 panic("kdebug_secpolicy: NULL pointer was passed.\n");
463 printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
464 sp
->refcnt
, sp
->state
, sp
->policy
);
466 kdebug_secpolicyindex(&sp
->spidx
);
468 switch (sp
->policy
) {
469 case IPSEC_POLICY_DISCARD
:
470 printf(" type=discard }\n");
472 case IPSEC_POLICY_NONE
:
473 printf(" type=none }\n");
475 case IPSEC_POLICY_IPSEC
:
477 struct ipsecrequest
*isr
;
478 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
480 printf(" level=%u\n", isr
->level
);
481 kdebug_secasindex(&isr
->saidx
);
483 if (isr
->sav
!= NULL
)
484 kdebug_secasv(isr
->sav
);
489 case IPSEC_POLICY_BYPASS
:
490 printf(" type=bypass }\n");
492 case IPSEC_POLICY_ENTRUST
:
493 printf(" type=entrust }\n");
496 printf("kdebug_secpolicy: Invalid policy found. %d\n",
505 kdebug_secpolicyindex(spidx
)
506 struct secpolicyindex
*spidx
;
510 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
512 printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
513 spidx
->dir
, spidx
->prefs
, spidx
->prefd
, spidx
->ul_proto
);
515 ipsec_hexdump((caddr_t
)&spidx
->src
,
516 ((struct sockaddr
*)&spidx
->src
)->sa_len
);
518 ipsec_hexdump((caddr_t
)&spidx
->dst
,
519 ((struct sockaddr
*)&spidx
->dst
)->sa_len
);
526 kdebug_secasindex(saidx
)
527 struct secasindex
*saidx
;
531 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
533 printf("secasindex{ mode=%u proto=%u\n",
534 saidx
->mode
, saidx
->proto
);
536 ipsec_hexdump((caddr_t
)&saidx
->src
,
537 ((struct sockaddr
*)&saidx
->src
)->sa_len
);
539 ipsec_hexdump((caddr_t
)&saidx
->dst
,
540 ((struct sockaddr
*)&saidx
->dst
)->sa_len
);
548 struct secasvar
*sav
;
552 panic("kdebug_secasv: NULL pointer was passed.\n");
555 kdebug_secasindex(&sav
->sah
->saidx
);
557 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
558 sav
->refcnt
, sav
->state
, sav
->alg_auth
, sav
->alg_enc
);
559 printf(" spi=%u flags=%u\n",
560 (u_int32_t
)ntohl(sav
->spi
), sav
->flags
);
562 if (sav
->key_auth
!= NULL
)
563 kdebug_sadb_key((struct sadb_ext
*)sav
->key_auth
);
564 if (sav
->key_enc
!= NULL
)
565 kdebug_sadb_key((struct sadb_ext
*)sav
->key_enc
);
566 if (sav
->iv
!= NULL
) {
568 ipsec_hexdump(sav
->iv
, sav
->ivlen
? sav
->ivlen
: 8);
572 if (sav
->replay
!= NULL
)
573 kdebug_secreplay(sav
->replay
);
574 if (sav
->lft_c
!= NULL
)
575 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_c
);
576 if (sav
->lft_h
!= NULL
)
577 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_h
);
578 if (sav
->lft_s
!= NULL
)
579 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_s
);
582 /* XXX: misc[123] ? */
589 kdebug_secreplay(rpl
)
590 struct secreplay
*rpl
;
596 panic("kdebug_secreplay: NULL pointer was passed.\n");
598 printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
599 rpl
->count
, rpl
->wsize
, rpl
->seq
, rpl
->lastseq
);
601 if (rpl
->bitmap
== NULL
) {
606 printf("\n bitmap { ");
608 for (len
= 0; len
< rpl
->wsize
; len
++) {
609 for (l
= 7; l
>= 0; l
--)
610 printf("%u", (((rpl
->bitmap
)[len
] >> l
) & 1) ? 1 : 0);
625 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
626 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
627 m
, m
->m_next
, m
->m_nextpkt
, m
->m_data
,
628 m
->m_len
, m
->m_type
, m
->m_flags
);
630 if (m
->m_flags
& M_PKTHDR
) {
631 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
632 m
->m_pkthdr
.len
, m
->m_pkthdr
.rcvif
);
635 if (m
->m_flags
& M_EXT
) {
636 printf(" m_ext{ ext_buf:%p ext_free:%p "
637 "ext_size:%u ext_ref:%p }\n",
638 m
->m_ext
.ext_buf
, m
->m_ext
.ext_free
,
639 m
->m_ext
.ext_size
, m
->m_ext
.ext_refs
);
652 for (j
= 0; m
; m
= m
->m_next
) {
654 printf(" m_data:\n");
655 for (i
= 0; i
< m
->m_len
; i
++) {
656 if (i
&& i
% 32 == 0)
660 printf("%02x", mtod(m
, u_char
*)[i
]);
671 kdebug_sockaddr(addr
)
672 struct sockaddr
*addr
;
674 struct sockaddr_in
*sin
;
676 struct sockaddr_in6
*sin6
;
681 panic("kdebug_sockaddr: NULL pointer was passed.\n");
683 /* NOTE: We deal with port number as host byte order. */
684 printf("sockaddr{ len=%u family=%u", addr
->sa_len
, addr
->sa_family
);
686 switch (addr
->sa_family
) {
688 sin
= (struct sockaddr_in
*)addr
;
689 printf(" port=%u\n", ntohs(sin
->sin_port
));
690 ipsec_hexdump((caddr_t
)&sin
->sin_addr
, sizeof(sin
->sin_addr
));
694 sin6
= (struct sockaddr_in6
*)addr
;
695 printf(" port=%u\n", ntohs(sin6
->sin6_port
));
696 printf(" flowinfo=0x%08x, scope_id=0x%08x\n",
697 sin6
->sin6_flowinfo
, sin6
->sin6_scope_id
);
698 ipsec_hexdump((caddr_t
)&sin6
->sin6_addr
,
699 sizeof(sin6
->sin6_addr
));
710 ipsec_bindump(buf
, len
)
716 for (i
= 0; i
< len
; i
++)
717 printf("%c", (unsigned char)buf
[i
]);
724 ipsec_hexdump(buf
, len
)
730 for (i
= 0; i
< len
; i
++) {
731 if (i
!= 0 && i
% 32 == 0) printf("\n");
732 if (i
% 4 == 0) printf(" ");
733 printf("%02x", (unsigned char)buf
[i
]);
736 if (i
% 32 != 0) printf("\n");
742 #endif /* !defined(KERNEL) || (defined(KERNEL) && defined(IPSEC_DEBUG)) */