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>
50 #include <netipsec/key_var.h>
51 #include <netipsec/key_debug.h>
53 #include <netinet/in.h>
54 #include <netipsec/ipsec.h>
56 #include <netipsec/keydb.h>
65 static void kdebug_sadb_prop(struct sadb_ext *);
66 static void kdebug_sadb_identity(struct sadb_ext *);
67 static void kdebug_sadb_supported(struct sadb_ext *);
68 static void kdebug_sadb_lifetime(struct sadb_ext *);
69 static void kdebug_sadb_sa(struct sadb_ext *);
70 static void kdebug_sadb_address(struct sadb_ext *);
71 static void kdebug_sadb_key(struct sadb_ext *);
72 static void kdebug_sadb_x_sa2(struct sadb_ext *);
75 static void kdebug_secreplay(struct secreplay *);
79 #define panic(fmt, ...) { printf(fmt, ## __VA_ARGS__); exit(-1); }
82 /* NOTE: host byte order */
84 /* %%%: about struct sadb_msg */
86 kdebug_sadb(struct sadb_msg *base)
93 panic("%s: NULL pointer was passed.\n", __func__);
95 printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
96 base->sadb_msg_version, base->sadb_msg_type,
97 base->sadb_msg_errno, base->sadb_msg_satype);
98 printf(" len=%u reserved=%u seq=%u pid=%u\n",
99 base->sadb_msg_len, base->sadb_msg_reserved,
100 base->sadb_msg_seq, base->sadb_msg_pid);
102 tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
103 ext = (struct sadb_ext *)((caddr_t)base + sizeof(struct sadb_msg));
106 printf("sadb_ext{ len=%u type=%u }\n",
107 ext->sadb_ext_len, ext->sadb_ext_type);
109 if (ext->sadb_ext_len == 0) {
110 printf("%s: invalid ext_len=0 was passed.\n", __func__);
113 if (ext->sadb_ext_len > tlen) {
114 printf("%s: ext_len too big (%u > %u).\n",
115 __func__, ext->sadb_ext_len, tlen);
119 switch (ext->sadb_ext_type) {
123 case SADB_EXT_LIFETIME_CURRENT:
124 case SADB_EXT_LIFETIME_HARD:
125 case SADB_EXT_LIFETIME_SOFT:
126 kdebug_sadb_lifetime(ext);
128 case SADB_EXT_ADDRESS_SRC:
129 case SADB_EXT_ADDRESS_DST:
130 case SADB_EXT_ADDRESS_PROXY:
131 kdebug_sadb_address(ext);
133 case SADB_EXT_KEY_AUTH:
134 case SADB_EXT_KEY_ENCRYPT:
135 kdebug_sadb_key(ext);
137 case SADB_EXT_IDENTITY_SRC:
138 case SADB_EXT_IDENTITY_DST:
139 kdebug_sadb_identity(ext);
141 case SADB_EXT_SENSITIVITY:
143 case SADB_EXT_PROPOSAL:
144 kdebug_sadb_prop(ext);
146 case SADB_EXT_SUPPORTED_AUTH:
147 case SADB_EXT_SUPPORTED_ENCRYPT:
148 kdebug_sadb_supported(ext);
150 case SADB_EXT_SPIRANGE:
151 case SADB_X_EXT_KMPRIVATE:
153 case SADB_X_EXT_POLICY:
154 kdebug_sadb_x_policy(ext);
157 kdebug_sadb_x_sa2(ext);
160 printf("%s: invalid ext_type %u\n", __func__,
165 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
167 ext = (struct sadb_ext *)((caddr_t)ext + extlen);
174 kdebug_sadb_prop(struct sadb_ext *ext)
176 struct sadb_prop *prop = (struct sadb_prop *)ext;
177 struct sadb_comb *comb;
182 panic("%s: NULL pointer was passed.\n", __func__);
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(struct sadb_ext *ext)
225 struct sadb_ident *id = (struct sadb_ident *)ext;
230 panic("%s: NULL pointer was passed.\n", __func__);
232 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
233 printf("sadb_ident_%s{",
234 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
235 switch (id->sadb_ident_type) {
237 printf(" type=%d id=%lu",
238 id->sadb_ident_type, (u_long)id->sadb_ident_id);
241 ipsec_hexdump((caddr_t)(id + 1), len); /*XXX cast ?*/
245 p = (char *)(id + 1);
247 for (/*nothing*/; *p && p < ep; p++) {
249 printf("%c", *p & 0xff);
251 printf("\\%03o", *p & 0xff);
265 kdebug_sadb_supported(struct sadb_ext *ext)
267 struct sadb_supported *sup = (struct sadb_supported *)ext;
268 struct sadb_alg *alg;
273 panic("%s: NULL pointer was passed.\n", __func__);
275 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
277 alg = (struct sadb_alg *)(sup + 1);
278 printf("sadb_sup{\n");
280 printf(" { id=%d ivlen=%d min=%d max=%d }\n",
281 alg->sadb_alg_id, alg->sadb_alg_ivlen,
282 alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
291 kdebug_sadb_lifetime(struct sadb_ext *ext)
293 struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
297 panic("%s: NULL pointer was passed.\n", __func__);
299 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
300 lft->sadb_lifetime_allocations,
301 (u_int32_t)lft->sadb_lifetime_bytes);
302 printf(" addtime=%u, usetime=%u }\n",
303 (u_int32_t)lft->sadb_lifetime_addtime,
304 (u_int32_t)lft->sadb_lifetime_usetime);
310 kdebug_sadb_sa(struct sadb_ext *ext)
312 struct sadb_sa *sa = (struct sadb_sa *)ext;
316 panic("%s: NULL pointer was passed.\n", __func__);
318 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
319 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
321 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
322 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
328 kdebug_sadb_address(struct sadb_ext *ext)
330 struct sadb_address *addr = (struct sadb_address *)ext;
334 panic("%s: NULL pointer was passed.\n", __func__);
336 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
337 addr->sadb_address_proto, addr->sadb_address_prefixlen,
338 ((u_char *)&addr->sadb_address_reserved)[0],
339 ((u_char *)&addr->sadb_address_reserved)[1]);
341 kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr)));
347 kdebug_sadb_key(struct sadb_ext *ext)
349 struct sadb_key *key = (struct sadb_key *)ext;
353 panic("%s: NULL pointer was passed.\n", __func__);
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("%s: key length mismatch, bit:%d len:%ld.\n",
364 key->sadb_key_bits >> 3,
365 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
368 ipsec_hexdump((caddr_t)key + sizeof(struct sadb_key),
369 key->sadb_key_bits >> 3);
375 kdebug_sadb_x_sa2(struct sadb_ext *ext)
377 struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext;
381 panic("%s: NULL pointer was passed.\n", __func__);
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(struct sadb_ext *ext)
395 struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
396 struct sockaddr *addr;
400 panic("%s: NULL pointer was passed.\n", __func__);
402 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
403 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
404 xpl->sadb_x_policy_id);
406 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
408 struct sadb_x_ipsecrequest *xisr;
410 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
411 xisr = (struct sadb_x_ipsecrequest *)(xpl + 1);
414 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
415 xisr->sadb_x_ipsecrequest_len,
416 xisr->sadb_x_ipsecrequest_proto,
417 xisr->sadb_x_ipsecrequest_mode,
418 xisr->sadb_x_ipsecrequest_level,
419 xisr->sadb_x_ipsecrequest_reqid);
421 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
422 addr = (struct sockaddr *)(xisr + 1);
423 kdebug_sockaddr(addr);
424 addr = (struct sockaddr *)((caddr_t)addr
426 kdebug_sockaddr(addr);
431 /* prevent infinite loop */
432 if (xisr->sadb_x_ipsecrequest_len <= 0) {
433 printf("%s: wrong policy struct.\n", __func__);
436 /* prevent overflow */
437 if (xisr->sadb_x_ipsecrequest_len > tlen) {
438 printf("%s: invalid ipsec policy length "
439 "(%u > %u)\n", __func__,
440 xisr->sadb_x_ipsecrequest_len, tlen);
444 tlen -= xisr->sadb_x_ipsecrequest_len;
446 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
447 + xisr->sadb_x_ipsecrequest_len);
451 panic("%s: wrong policy struct.\n", __func__);
458 /* %%%: about SPD and SAD */
460 kdebug_secpolicy(struct secpolicy *sp)
464 panic("%s: NULL pointer was passed.\n", __func__);
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("%s: Invalid policy found. %d\n", __func__, sp->policy);
507 kdebug_secpolicyindex(struct secpolicyindex *spidx)
511 panic("%s: NULL pointer was passed.\n", __func__);
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(struct secasindex *saidx)
531 panic("%s: NULL pointer was passed.\n", __func__);
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);
547 kdebug_sec_lifetime(struct seclifetime *lft)
551 panic("%s: NULL pointer was passed.\n", __func__);
553 printf("sec_lifetime{ alloc=%u, bytes=%u\n",
554 lft->allocations, (u_int32_t)lft->bytes);
555 printf(" addtime=%u, usetime=%u }\n",
556 (u_int32_t)lft->addtime, (u_int32_t)lft->usetime);
562 kdebug_secasv(struct secasvar *sav)
566 panic("%s: NULL pointer was passed.\n", __func__);
569 kdebug_secasindex(&sav->sah->saidx);
571 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
572 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
573 printf(" spi=%u flags=%u\n",
574 (u_int32_t)ntohl(sav->spi), sav->flags);
576 if (sav->key_auth != NULL)
577 kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
578 if (sav->key_enc != NULL)
579 kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
580 if (sav->iv != NULL) {
582 ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
586 if (sav->replay != NULL)
587 kdebug_secreplay(sav->replay);
588 if (sav->lft_c != NULL)
589 kdebug_sec_lifetime(sav->lft_c);
590 if (sav->lft_h != NULL)
591 kdebug_sec_lifetime(sav->lft_h);
592 if (sav->lft_s != NULL)
593 kdebug_sec_lifetime(sav->lft_s);
596 /* XXX: misc[123] ? */
603 kdebug_secreplay(struct secreplay *rpl)
609 panic("%s: NULL pointer was passed.\n", __func__);
611 printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
612 rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
614 if (rpl->bitmap == NULL) {
619 printf("\n bitmap { ");
621 for (len = 0; len < rpl->wsize; len++) {
622 for (l = 7; l >= 0; l--)
623 printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
631 kdebug_mbufhdr(struct mbuf *m)
637 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
638 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
639 m, m->m_next, m->m_nextpkt, m->m_data,
640 m->m_len, m->m_type, m->m_flags);
642 if (m->m_flags & M_PKTHDR) {
643 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
644 m->m_pkthdr.len, m->m_pkthdr.rcvif);
647 if (m->m_flags & M_EXT) {
648 printf(" m_ext{ ext_buf:%p ext_free:%p "
649 "ext_size:%u ref_cnt:%p }\n",
650 m->m_ext.ext_buf, m->m_ext.ext_free,
651 m->m_ext.ext_size, m->m_ext.ref_cnt);
658 kdebug_mbuf(struct mbuf *m0)
663 for (j = 0; m; m = m->m_next) {
665 printf(" m_data:\n");
666 for (i = 0; i < m->m_len; i++) {
667 if (i && i % 32 == 0)
671 printf("%02x", mtod(m, u_char *)[i]);
682 kdebug_sockaddr(struct sockaddr *addr)
684 struct sockaddr_in *sin4;
686 struct sockaddr_in6 *sin6;
691 panic("%s: NULL pointer was passed.\n", __func__);
693 /* NOTE: We deal with port number as host byte order. */
694 printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
696 switch (addr->sa_family) {
698 sin4 = (struct sockaddr_in *)addr;
699 printf(" port=%u\n", ntohs(sin4->sin_port));
700 ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr));
704 sin6 = (struct sockaddr_in6 *)addr;
705 printf(" port=%u\n", ntohs(sin6->sin6_port));
706 printf(" flowinfo=0x%08x, scope_id=0x%08x\n",
707 sin6->sin6_flowinfo, sin6->sin6_scope_id);
708 ipsec_hexdump((caddr_t)&sin6->sin6_addr,
709 sizeof(sin6->sin6_addr));
720 ipsec_bindump(caddr_t buf, int len)
724 for (i = 0; i < len; i++)
725 printf("%c", (unsigned char)buf[i]);
732 ipsec_hexdump(caddr_t buf, int len)
736 for (i = 0; i < len; i++) {
737 if (i != 0 && i % 32 == 0) printf("\n");
738 if (i % 4 == 0) printf(" ");
739 printf("%02x", (unsigned char)buf[i]);
742 if (i % 32 != 0) printf("\n");