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
35 #include "opt_inet6.h"
36 #include "opt_ipsec.h"
39 #include <sys/types.h>
40 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/queue.h>
46 #include <sys/socket.h>
48 #include <net/route.h>
51 #include "key_debug.h"
53 #include <netinet/in.h>
54 #include <netinet6/ipsec.h>
62 static void kdebug_sadb_prop (struct sadb_ext
*);
63 static void kdebug_sadb_identity (struct sadb_ext
*);
64 static void kdebug_sadb_supported (struct sadb_ext
*);
65 static void kdebug_sadb_lifetime (struct sadb_ext
*);
66 static void kdebug_sadb_sa (struct sadb_ext
*);
67 static void kdebug_sadb_address (struct sadb_ext
*);
68 static void kdebug_sadb_key (struct sadb_ext
*);
69 static void kdebug_sadb_x_sa2 (struct sadb_ext
*);
72 static void kdebug_secreplay (struct secreplay
*);
76 #define kprintf printf
77 #define panic(param) { printf(param); exit(-1); }
80 /* NOTE: host byte order */
82 /* %%%: about struct sadb_msg */
84 kdebug_sadb(struct sadb_msg
*base
)
91 panic("kdebug_sadb: NULL pointer was passed.");
93 kprintf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
94 base
->sadb_msg_version
, base
->sadb_msg_type
,
95 base
->sadb_msg_errno
, base
->sadb_msg_satype
);
96 kprintf(" len=%u reserved=%u seq=%u pid=%u\n",
97 base
->sadb_msg_len
, base
->sadb_msg_reserved
,
98 base
->sadb_msg_seq
, base
->sadb_msg_pid
);
100 tlen
= PFKEY_UNUNIT64(base
->sadb_msg_len
) - sizeof(struct sadb_msg
);
101 ext
= (struct sadb_ext
*)((caddr_t
)base
+ sizeof(struct sadb_msg
));
104 kprintf("sadb_ext{ len=%u type=%u }\n",
105 ext
->sadb_ext_len
, ext
->sadb_ext_type
);
107 if (ext
->sadb_ext_len
== 0) {
108 kprintf("kdebug_sadb: invalid ext_len=0 was passed.\n");
111 if (ext
->sadb_ext_len
> tlen
) {
112 kprintf("kdebug_sadb: ext_len exceeds end of buffer.\n");
116 switch (ext
->sadb_ext_type
) {
120 case SADB_EXT_LIFETIME_CURRENT
:
121 case SADB_EXT_LIFETIME_HARD
:
122 case SADB_EXT_LIFETIME_SOFT
:
123 kdebug_sadb_lifetime(ext
);
125 case SADB_EXT_ADDRESS_SRC
:
126 case SADB_EXT_ADDRESS_DST
:
127 case SADB_EXT_ADDRESS_PROXY
:
128 kdebug_sadb_address(ext
);
130 case SADB_EXT_KEY_AUTH
:
131 case SADB_EXT_KEY_ENCRYPT
:
132 kdebug_sadb_key(ext
);
134 case SADB_EXT_IDENTITY_SRC
:
135 case SADB_EXT_IDENTITY_DST
:
136 kdebug_sadb_identity(ext
);
138 case SADB_EXT_SENSITIVITY
:
140 case SADB_EXT_PROPOSAL
:
141 kdebug_sadb_prop(ext
);
143 case SADB_EXT_SUPPORTED_AUTH
:
144 case SADB_EXT_SUPPORTED_ENCRYPT
:
145 kdebug_sadb_supported(ext
);
147 case SADB_EXT_SPIRANGE
:
148 case SADB_X_EXT_KMPRIVATE
:
150 case SADB_X_EXT_POLICY
:
151 kdebug_sadb_x_policy(ext
);
154 kdebug_sadb_x_sa2(ext
);
157 kprintf("kdebug_sadb: invalid ext_type %u was passed.\n",
162 extlen
= PFKEY_UNUNIT64(ext
->sadb_ext_len
);
164 ext
= (struct sadb_ext
*)((caddr_t
)ext
+ extlen
);
171 kdebug_sadb_prop(struct sadb_ext
*ext
)
173 struct sadb_prop
*prop
= (struct sadb_prop
*)ext
;
174 struct sadb_comb
*comb
;
179 panic("kdebug_sadb_prop: NULL pointer was passed.");
181 len
= (PFKEY_UNUNIT64(prop
->sadb_prop_len
) - sizeof(*prop
))
183 comb
= (struct sadb_comb
*)(prop
+ 1);
184 kprintf("sadb_prop{ replay=%u\n", prop
->sadb_prop_replay
);
187 kprintf("sadb_comb{ auth=%u encrypt=%u "
188 "flags=0x%04x reserved=0x%08x\n",
189 comb
->sadb_comb_auth
, comb
->sadb_comb_encrypt
,
190 comb
->sadb_comb_flags
, comb
->sadb_comb_reserved
);
192 kprintf(" auth_minbits=%u auth_maxbits=%u "
193 "encrypt_minbits=%u encrypt_maxbits=%u\n",
194 comb
->sadb_comb_auth_minbits
,
195 comb
->sadb_comb_auth_maxbits
,
196 comb
->sadb_comb_encrypt_minbits
,
197 comb
->sadb_comb_encrypt_maxbits
);
199 kprintf(" soft_alloc=%u hard_alloc=%u "
200 "soft_bytes=%lu hard_bytes=%lu\n",
201 comb
->sadb_comb_soft_allocations
,
202 comb
->sadb_comb_hard_allocations
,
203 (unsigned long)comb
->sadb_comb_soft_bytes
,
204 (unsigned long)comb
->sadb_comb_hard_bytes
);
206 kprintf(" soft_alloc=%lu hard_alloc=%lu "
207 "soft_bytes=%lu hard_bytes=%lu }\n",
208 (unsigned long)comb
->sadb_comb_soft_addtime
,
209 (unsigned long)comb
->sadb_comb_hard_addtime
,
210 (unsigned long)comb
->sadb_comb_soft_usetime
,
211 (unsigned long)comb
->sadb_comb_hard_usetime
);
220 kdebug_sadb_identity(struct sadb_ext
*ext
)
222 struct sadb_ident
*id
= (struct sadb_ident
*)ext
;
227 panic("kdebug_sadb_identity: NULL pointer was passed.");
229 len
= PFKEY_UNUNIT64(id
->sadb_ident_len
) - sizeof(*id
);
230 kprintf("sadb_ident_%s{",
231 id
->sadb_ident_exttype
== SADB_EXT_IDENTITY_SRC
? "src" : "dst");
232 switch (id
->sadb_ident_type
) {
234 kprintf(" type=%d id=%lu",
235 id
->sadb_ident_type
, (u_long
)id
->sadb_ident_id
);
238 ipsec_hexdump((caddr_t
)(id
+ 1), len
); /*XXX cast ?*/
241 kprintf("\n str=\"");
242 p
= (char *)(id
+ 1);
244 for (/*nothing*/; *p
&& p
< ep
; p
++) {
246 kprintf("%c", *p
& 0xff);
248 kprintf("\\%03o", *p
& 0xff);
262 kdebug_sadb_supported(struct sadb_ext
*ext
)
264 struct sadb_supported
*sup
= (struct sadb_supported
*)ext
;
265 struct sadb_alg
*alg
;
270 panic("kdebug_sadb_supported: NULL pointer was passed.");
272 len
= (PFKEY_UNUNIT64(sup
->sadb_supported_len
) - sizeof(*sup
))
274 alg
= (struct sadb_alg
*)(sup
+ 1);
275 kprintf("sadb_sup{\n");
277 kprintf(" { id=%d ivlen=%d min=%d max=%d }\n",
278 alg
->sadb_alg_id
, alg
->sadb_alg_ivlen
,
279 alg
->sadb_alg_minbits
, alg
->sadb_alg_maxbits
);
288 kdebug_sadb_lifetime(struct sadb_ext
*ext
)
290 struct sadb_lifetime
*lft
= (struct sadb_lifetime
*)ext
;
294 panic("kdebug_sadb_lifetime: NULL pointer was passed.");
296 kprintf("sadb_lifetime{ alloc=%u, bytes=%u\n",
297 lft
->sadb_lifetime_allocations
,
298 (u_int32_t
)lft
->sadb_lifetime_bytes
);
299 kprintf(" addtime=%u, usetime=%u }\n",
300 (u_int32_t
)lft
->sadb_lifetime_addtime
,
301 (u_int32_t
)lft
->sadb_lifetime_usetime
);
307 kdebug_sadb_sa(struct sadb_ext
*ext
)
309 struct sadb_sa
*sa
= (struct sadb_sa
*)ext
;
313 panic("kdebug_sadb_sa: NULL pointer was passed.");
315 kprintf("sadb_sa{ spi=%u replay=%u state=%u\n",
316 (u_int32_t
)ntohl(sa
->sadb_sa_spi
), sa
->sadb_sa_replay
,
318 kprintf(" auth=%u encrypt=%u flags=0x%08x }\n",
319 sa
->sadb_sa_auth
, sa
->sadb_sa_encrypt
, sa
->sadb_sa_flags
);
325 kdebug_sadb_address(struct sadb_ext
*ext
)
327 struct sadb_address
*addr
= (struct sadb_address
*)ext
;
331 panic("kdebug_sadb_address: NULL pointer was passed.");
333 kprintf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
334 addr
->sadb_address_proto
, addr
->sadb_address_prefixlen
,
335 ((u_char
*)&addr
->sadb_address_reserved
)[0],
336 ((u_char
*)&addr
->sadb_address_reserved
)[1]);
338 kdebug_sockaddr((struct sockaddr
*)((caddr_t
)ext
+ sizeof(*addr
)));
344 kdebug_sadb_key(struct sadb_ext
*ext
)
346 struct sadb_key
*key
= (struct sadb_key
*)ext
;
350 panic("kdebug_sadb_key: NULL pointer was passed.");
352 kprintf("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 kprintf("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(struct sadb_ext
*ext
)
373 struct sadb_x_sa2
*sa2
= (struct sadb_x_sa2
*)ext
;
377 panic("kdebug_sadb_x_sa2: NULL pointer was passed.");
379 kprintf("sadb_x_sa2{ mode=%u reqid=%u\n",
380 sa2
->sadb_x_sa2_mode
, sa2
->sadb_x_sa2_reqid
);
381 kprintf(" reserved1=%u reserved2=%u sequence=%u }\n",
382 sa2
->sadb_x_sa2_reserved1
, sa2
->sadb_x_sa2_reserved2
,
383 sa2
->sadb_x_sa2_sequence
);
389 kdebug_sadb_x_policy(struct sadb_ext
*ext
)
391 struct sadb_x_policy
*xpl
= (struct sadb_x_policy
*)ext
;
392 struct sockaddr
*addr
;
396 panic("kdebug_sadb_x_policy: NULL pointer was passed.");
398 kprintf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
399 xpl
->sadb_x_policy_type
, xpl
->sadb_x_policy_dir
,
400 xpl
->sadb_x_policy_id
);
402 if (xpl
->sadb_x_policy_type
== IPSEC_POLICY_IPSEC
) {
404 struct sadb_x_ipsecrequest
*xisr
;
406 tlen
= PFKEY_UNUNIT64(xpl
->sadb_x_policy_len
) - sizeof(*xpl
);
407 xisr
= (struct sadb_x_ipsecrequest
*)(xpl
+ 1);
410 kprintf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
411 xisr
->sadb_x_ipsecrequest_len
,
412 xisr
->sadb_x_ipsecrequest_proto
,
413 xisr
->sadb_x_ipsecrequest_mode
,
414 xisr
->sadb_x_ipsecrequest_level
,
415 xisr
->sadb_x_ipsecrequest_reqid
);
417 if (xisr
->sadb_x_ipsecrequest_len
> sizeof(*xisr
)) {
418 addr
= (struct sockaddr
*)(xisr
+ 1);
419 kdebug_sockaddr(addr
);
420 addr
= (struct sockaddr
*)((caddr_t
)addr
422 kdebug_sockaddr(addr
);
427 /* prevent infinite loop */
428 if (xisr
->sadb_x_ipsecrequest_len
<= 0) {
429 kprintf("kdebug_sadb_x_policy: wrong policy struct.\n");
432 /* prevent overflow */
433 if (xisr
->sadb_x_ipsecrequest_len
> tlen
) {
434 kprintf("invalid ipsec policy length\n");
438 tlen
-= xisr
->sadb_x_ipsecrequest_len
;
440 xisr
= (struct sadb_x_ipsecrequest
*)((caddr_t
)xisr
441 + xisr
->sadb_x_ipsecrequest_len
);
445 panic("kdebug_sadb_x_policy: wrong policy struct.");
452 /* %%%: about SPD and SAD */
454 kdebug_secpolicy(struct secpolicy
*sp
)
458 panic("kdebug_secpolicy: NULL pointer was passed.");
460 kprintf("secpolicy{ refcnt=%u state=%u policy=%u\n",
461 sp
->refcnt
, sp
->state
, sp
->policy
);
463 kdebug_secpolicyindex(&sp
->spidx
);
465 switch (sp
->policy
) {
466 case IPSEC_POLICY_DISCARD
:
467 kprintf(" type=discard }\n");
469 case IPSEC_POLICY_NONE
:
470 kprintf(" type=none }\n");
472 case IPSEC_POLICY_IPSEC
:
474 struct ipsecrequest
*isr
;
475 for (isr
= sp
->req
; isr
!= NULL
; isr
= isr
->next
) {
477 kprintf(" level=%u\n", isr
->level
);
478 kdebug_secasindex(&isr
->saidx
);
480 if (isr
->sav
!= NULL
)
481 kdebug_secasv(isr
->sav
);
486 case IPSEC_POLICY_BYPASS
:
487 kprintf(" type=bypass }\n");
489 case IPSEC_POLICY_ENTRUST
:
490 kprintf(" type=entrust }\n");
493 kprintf("kdebug_secpolicy: Invalid policy found. %d\n",
502 kdebug_secpolicyindex(struct secpolicyindex
*spidx
)
506 panic("kdebug_secpolicyindex: NULL pointer was passed.");
508 kprintf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
509 spidx
->dir
, spidx
->prefs
, spidx
->prefd
, spidx
->ul_proto
);
511 ipsec_hexdump((caddr_t
)&spidx
->src
,
512 ((struct sockaddr
*)&spidx
->src
)->sa_len
);
514 ipsec_hexdump((caddr_t
)&spidx
->dst
,
515 ((struct sockaddr
*)&spidx
->dst
)->sa_len
);
522 kdebug_secasindex(struct secasindex
*saidx
)
526 panic("kdebug_secpolicyindex: NULL pointer was passed.");
528 kprintf("secasindex{ mode=%u proto=%u\n",
529 saidx
->mode
, saidx
->proto
);
531 ipsec_hexdump((caddr_t
)&saidx
->src
,
532 ((struct sockaddr
*)&saidx
->src
)->sa_len
);
534 ipsec_hexdump((caddr_t
)&saidx
->dst
,
535 ((struct sockaddr
*)&saidx
->dst
)->sa_len
);
542 kdebug_secasv(struct secasvar
*sav
)
546 panic("kdebug_secasv: NULL pointer was passed.");
549 kdebug_secasindex(&sav
->sah
->saidx
);
551 kprintf(" refcnt=%u state=%u auth=%u enc=%u\n",
552 sav
->refcnt
, sav
->state
, sav
->alg_auth
, sav
->alg_enc
);
553 kprintf(" spi=%u flags=%u\n",
554 (u_int32_t
)ntohl(sav
->spi
), sav
->flags
);
556 if (sav
->key_auth
!= NULL
)
557 kdebug_sadb_key((struct sadb_ext
*)sav
->key_auth
);
558 if (sav
->key_enc
!= NULL
)
559 kdebug_sadb_key((struct sadb_ext
*)sav
->key_enc
);
560 if (sav
->iv
!= NULL
) {
562 ipsec_hexdump(sav
->iv
, sav
->ivlen
? sav
->ivlen
: 8);
566 if (sav
->replay
!= NULL
)
567 kdebug_secreplay(sav
->replay
);
568 if (sav
->lft_c
!= NULL
)
569 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_c
);
570 if (sav
->lft_h
!= NULL
)
571 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_h
);
572 if (sav
->lft_s
!= NULL
)
573 kdebug_sadb_lifetime((struct sadb_ext
*)sav
->lft_s
);
576 /* XXX: misc[123] ? */
583 kdebug_secreplay(struct secreplay
*rpl
)
589 panic("kdebug_secreplay: NULL pointer was passed.");
591 kprintf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
592 rpl
->count
, rpl
->wsize
, rpl
->seq
, rpl
->lastseq
);
594 if (rpl
->bitmap
== NULL
) {
599 kprintf("\n bitmap { ");
601 for (len
= 0; len
< rpl
->wsize
; len
++) {
602 for (l
= 7; l
>= 0; l
--)
603 kprintf("%u", (((rpl
->bitmap
)[len
] >> l
) & 1) ? 1 : 0);
611 kdebug_mbufhdr(struct mbuf
*m
)
617 kprintf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
618 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
619 m
, m
->m_next
, m
->m_nextpkt
, m
->m_data
,
620 m
->m_len
, m
->m_type
, m
->m_flags
);
622 if (m
->m_flags
& M_PKTHDR
) {
623 kprintf(" m_pkthdr{ len:%d rcvif:%p }\n",
624 m
->m_pkthdr
.len
, m
->m_pkthdr
.rcvif
);
627 if (m
->m_flags
& M_EXT
) {
628 kprintf(" m_ext{ ext_buf:%p ext_free:%p "
629 "ext_size:%u ext_ref:%p }\n",
630 m
->m_ext
.ext_buf
, m
->m_ext
.ext_free
,
631 m
->m_ext
.ext_size
, m
->m_ext
.ext_ref
);
638 kdebug_mbuf(struct mbuf
*m0
)
643 for (j
= 0; m
; m
= m
->m_next
) {
645 kprintf(" m_data:\n");
646 for (i
= 0; i
< m
->m_len
; i
++) {
647 if (i
&& i
% 32 == 0)
651 kprintf("%02x", mtod(m
, u_char
*)[i
]);
662 kdebug_sockaddr(struct sockaddr
*addr
)
664 struct sockaddr_in
*sin4
;
666 struct sockaddr_in6
*sin6
;
671 panic("kdebug_sockaddr: NULL pointer was passed.");
673 /* NOTE: We deal with port number as host byte order. */
674 kprintf("sockaddr{ len=%u family=%u", addr
->sa_len
, addr
->sa_family
);
676 switch (addr
->sa_family
) {
678 sin4
= (struct sockaddr_in
*)addr
;
679 kprintf(" port=%u\n", ntohs(sin4
->sin_port
));
680 ipsec_hexdump((caddr_t
)&sin4
->sin_addr
, sizeof(sin4
->sin_addr
));
684 sin6
= (struct sockaddr_in6
*)addr
;
685 kprintf(" port=%u\n", ntohs(sin6
->sin6_port
));
686 kprintf(" flowinfo=0x%08x, scope_id=0x%08x\n",
687 sin6
->sin6_flowinfo
, sin6
->sin6_scope_id
);
688 ipsec_hexdump((caddr_t
)&sin6
->sin6_addr
,
689 sizeof(sin6
->sin6_addr
));
700 ipsec_bindump(caddr_t buf
, int len
)
704 for (i
= 0; i
< len
; i
++)
705 kprintf("%c", (unsigned char)buf
[i
]);
712 ipsec_hexdump(caddr_t buf
, int len
)
716 for (i
= 0; i
< len
; i
++) {
717 if (i
!= 0 && i
% 32 == 0) kprintf("\n");
718 if (i
% 4 == 0) kprintf(" ");
719 kprintf("%02x", (unsigned char)buf
[i
]);
722 if (i
% 32 != 0) kprintf("\n");