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>
50 #include <netipsec/key_var.h>
51 #include <netipsec/key_debug.h>
53 #include <netinet/in.h>
54 #include <netipsec/ipsec.h>
62 static void kdebug_sadb_prop __P((struct sadb_ext *));
63 static void kdebug_sadb_identity __P((struct sadb_ext *));
64 static void kdebug_sadb_supported __P((struct sadb_ext *));
65 static void kdebug_sadb_lifetime __P((struct sadb_ext *));
66 static void kdebug_sadb_sa __P((struct sadb_ext *));
67 static void kdebug_sadb_address __P((struct sadb_ext *));
68 static void kdebug_sadb_key __P((struct sadb_ext *));
69 static void kdebug_sadb_x_sa2 __P((struct sadb_ext *));
72 static void kdebug_secreplay __P((struct secreplay *));
76 #define panic(param) { printf(param); exit(-1); }
79 /* NOTE: host byte order */
81 /* %%%: about struct sadb_msg */
84 struct sadb_msg *base;
91 panic("%s: NULL pointer was passed.\n", __func__);
93 printf("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 printf(" 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 printf("sadb_ext{ len=%u type=%u }\n",
105 ext->sadb_ext_len, ext->sadb_ext_type);
107 if (ext->sadb_ext_len == 0) {
108 printf("%s: invalid ext_len=0 was passed.\n", __func__);
111 if (ext->sadb_ext_len > tlen) {
112 printf("%s: ext_len too big (%u > %u).\n",
113 __func__, ext->sadb_ext_len, tlen);
117 switch (ext->sadb_ext_type) {
121 case SADB_EXT_LIFETIME_CURRENT:
122 case SADB_EXT_LIFETIME_HARD:
123 case SADB_EXT_LIFETIME_SOFT:
124 kdebug_sadb_lifetime(ext);
126 case SADB_EXT_ADDRESS_SRC:
127 case SADB_EXT_ADDRESS_DST:
128 case SADB_EXT_ADDRESS_PROXY:
129 kdebug_sadb_address(ext);
131 case SADB_EXT_KEY_AUTH:
132 case SADB_EXT_KEY_ENCRYPT:
133 kdebug_sadb_key(ext);
135 case SADB_EXT_IDENTITY_SRC:
136 case SADB_EXT_IDENTITY_DST:
137 kdebug_sadb_identity(ext);
139 case SADB_EXT_SENSITIVITY:
141 case SADB_EXT_PROPOSAL:
142 kdebug_sadb_prop(ext);
144 case SADB_EXT_SUPPORTED_AUTH:
145 case SADB_EXT_SUPPORTED_ENCRYPT:
146 kdebug_sadb_supported(ext);
148 case SADB_EXT_SPIRANGE:
149 case SADB_X_EXT_KMPRIVATE:
151 case SADB_X_EXT_POLICY:
152 kdebug_sadb_x_policy(ext);
155 kdebug_sadb_x_sa2(ext);
158 printf("%s: invalid ext_type %u\n", __func__,
163 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
165 ext = (struct sadb_ext *)((caddr_t)ext + extlen);
172 kdebug_sadb_prop(ext)
173 struct sadb_ext *ext;
175 struct sadb_prop *prop = (struct sadb_prop *)ext;
176 struct sadb_comb *comb;
181 panic("%s: NULL pointer was passed.\n", __func__);
183 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
185 comb = (struct sadb_comb *)(prop + 1);
186 printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
189 printf("sadb_comb{ auth=%u encrypt=%u "
190 "flags=0x%04x reserved=0x%08x\n",
191 comb->sadb_comb_auth, comb->sadb_comb_encrypt,
192 comb->sadb_comb_flags, comb->sadb_comb_reserved);
194 printf(" auth_minbits=%u auth_maxbits=%u "
195 "encrypt_minbits=%u encrypt_maxbits=%u\n",
196 comb->sadb_comb_auth_minbits,
197 comb->sadb_comb_auth_maxbits,
198 comb->sadb_comb_encrypt_minbits,
199 comb->sadb_comb_encrypt_maxbits);
201 printf(" soft_alloc=%u hard_alloc=%u "
202 "soft_bytes=%lu hard_bytes=%lu\n",
203 comb->sadb_comb_soft_allocations,
204 comb->sadb_comb_hard_allocations,
205 (unsigned long)comb->sadb_comb_soft_bytes,
206 (unsigned long)comb->sadb_comb_hard_bytes);
208 printf(" soft_alloc=%lu hard_alloc=%lu "
209 "soft_bytes=%lu hard_bytes=%lu }\n",
210 (unsigned long)comb->sadb_comb_soft_addtime,
211 (unsigned long)comb->sadb_comb_hard_addtime,
212 (unsigned long)comb->sadb_comb_soft_usetime,
213 (unsigned long)comb->sadb_comb_hard_usetime);
222 kdebug_sadb_identity(ext)
223 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(ext)
266 struct sadb_ext *ext;
268 struct sadb_supported *sup = (struct sadb_supported *)ext;
269 struct sadb_alg *alg;
274 panic("%s: NULL pointer was passed.\n", __func__);
276 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
278 alg = (struct sadb_alg *)(sup + 1);
279 printf("sadb_sup{\n");
281 printf(" { id=%d ivlen=%d min=%d max=%d }\n",
282 alg->sadb_alg_id, alg->sadb_alg_ivlen,
283 alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
292 kdebug_sadb_lifetime(ext)
293 struct sadb_ext *ext;
295 struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
299 printf("%s: NULL pointer was passed.\n", __func__);
301 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
302 lft->sadb_lifetime_allocations,
303 (u_int32_t)lft->sadb_lifetime_bytes);
304 printf(" addtime=%u, usetime=%u }\n",
305 (u_int32_t)lft->sadb_lifetime_addtime,
306 (u_int32_t)lft->sadb_lifetime_usetime);
313 struct sadb_ext *ext;
315 struct sadb_sa *sa = (struct sadb_sa *)ext;
319 panic("%s: NULL pointer was passed.\n", __func__);
321 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
322 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
324 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
325 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
331 kdebug_sadb_address(ext)
332 struct sadb_ext *ext;
334 struct sadb_address *addr = (struct sadb_address *)ext;
338 panic("%s: NULL pointer was passed.\n", __func__);
340 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
341 addr->sadb_address_proto, addr->sadb_address_prefixlen,
342 ((u_char *)&addr->sadb_address_reserved)[0],
343 ((u_char *)&addr->sadb_address_reserved)[1]);
345 kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr)));
352 struct sadb_ext *ext;
354 struct sadb_key *key = (struct sadb_key *)ext;
358 panic("%s: NULL pointer was passed.\n", __func__);
360 printf("sadb_key{ bits=%u reserved=%u\n",
361 key->sadb_key_bits, key->sadb_key_reserved);
365 if ((key->sadb_key_bits >> 3) >
366 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
367 printf("%s: key length mismatch, bit:%d len:%ld.\n",
369 key->sadb_key_bits >> 3,
370 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
373 ipsec_hexdump((caddr_t)key + sizeof(struct sadb_key),
374 key->sadb_key_bits >> 3);
380 kdebug_sadb_x_sa2(ext)
381 struct sadb_ext *ext;
383 struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext;
387 panic("%s: NULL pointer was passed.\n", __func__);
389 printf("sadb_x_sa2{ mode=%u reqid=%u\n",
390 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
391 printf(" reserved1=%u reserved2=%u sequence=%u }\n",
392 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
393 sa2->sadb_x_sa2_sequence);
399 kdebug_sadb_x_policy(ext)
400 struct sadb_ext *ext;
402 struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
403 struct sockaddr *addr;
407 panic("%s: NULL pointer was passed.\n", __func__);
409 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
410 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
411 xpl->sadb_x_policy_id);
413 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
415 struct sadb_x_ipsecrequest *xisr;
417 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
418 xisr = (struct sadb_x_ipsecrequest *)(xpl + 1);
421 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
422 xisr->sadb_x_ipsecrequest_len,
423 xisr->sadb_x_ipsecrequest_proto,
424 xisr->sadb_x_ipsecrequest_mode,
425 xisr->sadb_x_ipsecrequest_level,
426 xisr->sadb_x_ipsecrequest_reqid);
428 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
429 addr = (struct sockaddr *)(xisr + 1);
430 kdebug_sockaddr(addr);
431 addr = (struct sockaddr *)((caddr_t)addr
433 kdebug_sockaddr(addr);
438 /* prevent infinite loop */
439 if (xisr->sadb_x_ipsecrequest_len <= 0) {
440 printf("%s: wrong policy struct.\n", __func__);
443 /* prevent overflow */
444 if (xisr->sadb_x_ipsecrequest_len > tlen) {
445 printf("%s: invalid ipsec policy length "
446 "(%u > %u)\n", __func__,
447 xisr->sadb_x_ipsecrequest_len, tlen);
451 tlen -= xisr->sadb_x_ipsecrequest_len;
453 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
454 + xisr->sadb_x_ipsecrequest_len);
458 panic("%s: wrong policy struct.\n", __func__);
465 /* %%%: about SPD and SAD */
468 struct secpolicy *sp;
472 panic("%s: NULL pointer was passed.\n", __func__);
474 printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
475 sp->refcnt, sp->state, sp->policy);
477 kdebug_secpolicyindex(&sp->spidx);
479 switch (sp->policy) {
480 case IPSEC_POLICY_DISCARD:
481 printf(" type=discard }\n");
483 case IPSEC_POLICY_NONE:
484 printf(" type=none }\n");
486 case IPSEC_POLICY_IPSEC:
488 struct ipsecrequest *isr;
489 for (isr = sp->req; isr != NULL; isr = isr->next) {
491 printf(" level=%u\n", isr->level);
492 kdebug_secasindex(&isr->saidx);
494 if (isr->sav != NULL)
495 kdebug_secasv(isr->sav);
500 case IPSEC_POLICY_BYPASS:
501 printf(" type=bypass }\n");
503 case IPSEC_POLICY_ENTRUST:
504 printf(" type=entrust }\n");
507 printf("%s: Invalid policy found. %d\n", __func__, sp->policy);
515 kdebug_secpolicyindex(spidx)
516 struct secpolicyindex *spidx;
520 panic("%s: NULL pointer was passed.\n", __func__);
522 printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
523 spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
525 ipsec_hexdump((caddr_t)&spidx->src,
526 ((struct sockaddr *)&spidx->src)->sa_len);
528 ipsec_hexdump((caddr_t)&spidx->dst,
529 ((struct sockaddr *)&spidx->dst)->sa_len);
536 kdebug_secasindex(saidx)
537 struct secasindex *saidx;
541 panic("%s: NULL pointer was passed.\n", __func__);
543 printf("secasindex{ mode=%u proto=%u\n",
544 saidx->mode, saidx->proto);
546 ipsec_hexdump((caddr_t)&saidx->src,
547 ((struct sockaddr *)&saidx->src)->sa_len);
549 ipsec_hexdump((caddr_t)&saidx->dst,
550 ((struct sockaddr *)&saidx->dst)->sa_len);
558 struct secasvar *sav;
562 panic("%s: NULL pointer was passed.\n", __func__);
565 kdebug_secasindex(&sav->sah->saidx);
567 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
568 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
569 printf(" spi=%u flags=%u\n",
570 (u_int32_t)ntohl(sav->spi), sav->flags);
572 if (sav->key_auth != NULL)
573 kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
574 if (sav->key_enc != NULL)
575 kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
576 if (sav->iv != NULL) {
578 ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
582 if (sav->replay != NULL)
583 kdebug_secreplay(sav->replay);
584 if (sav->lft_c != NULL)
585 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
586 if (sav->lft_h != NULL)
587 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
588 if (sav->lft_s != NULL)
589 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
592 /* XXX: misc[123] ? */
599 kdebug_secreplay(rpl)
600 struct secreplay *rpl;
606 panic("%s: NULL pointer was passed.\n", __func__);
608 printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
609 rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
611 if (rpl->bitmap == NULL) {
616 printf("\n bitmap { ");
618 for (len = 0; len < rpl->wsize; len++) {
619 for (l = 7; l >= 0; l--)
620 printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
635 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
636 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
637 m, m->m_next, m->m_nextpkt, m->m_data,
638 m->m_len, m->m_type, m->m_flags);
640 if (m->m_flags & M_PKTHDR) {
641 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
642 m->m_pkthdr.len, m->m_pkthdr.rcvif);
645 if (m->m_flags & M_EXT) {
646 printf(" m_ext{ ext_buf:%p ext_free:%p "
647 "ext_size:%u ref_cnt:%p }\n",
648 m->m_ext.ext_buf, m->m_ext.ext_free,
649 m->m_ext.ext_size, m->m_ext.ref_cnt);
662 for (j = 0; m; m = m->m_next) {
664 printf(" m_data:\n");
665 for (i = 0; i < m->m_len; i++) {
666 if (i && i % 32 == 0)
670 printf("%02x", mtod(m, u_char *)[i]);
681 kdebug_sockaddr(addr)
682 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(buf, len)
726 for (i = 0; i < len; i++)
727 printf("%c", (unsigned char)buf[i]);
734 ipsec_hexdump(buf, len)
740 for (i = 0; i < len; i++) {
741 if (i != 0 && i % 32 == 0) printf("\n");
742 if (i % 4 == 0) printf(" ");
743 printf("%02x", (unsigned char)buf[i]);
746 if (i % 32 != 0) printf("\n");