1 /* $KAME: key_debug.c,v 1.38 2003/09/06 05:15:44 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
41 #include <sys/types.h>
42 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/queue.h>
48 #include <sys/socket.h>
50 #include <net/route.h>
52 #include <netkey/key_var.h>
53 #include <netkey/key_debug.h>
55 #include <netinet/in.h>
56 #include <netinet6/ipsec.h>
68 #define TYPESTR(x) { "SADB_" #x, SADB_ ## x }
70 static const char *kdebug_typestr(u_int, const struct typestr *);
71 static const char *kdebug_sadb_msg_typestr(u_int);
72 static const char *kdebug_sadb_ext_typestr(u_int);
73 static void kdebug_sadb_prop(struct sadb_ext *);
74 static void kdebug_sadb_identity(struct sadb_ext *);
75 static void kdebug_sadb_supported(struct sadb_ext *);
76 static void kdebug_sadb_lifetime(struct sadb_ext *);
77 static void kdebug_sadb_sa(struct sadb_ext *);
78 static void kdebug_sadb_address(struct sadb_ext *);
79 static void kdebug_sadb_key(struct sadb_ext *);
80 static void kdebug_sadb_x_sa2(struct sadb_ext *);
83 static void kdebug_secreplay(struct secreplay *);
87 #define panic(param) { printf(param); exit(1); }
91 kdebug_typestr(type, list)
93 const struct typestr *list;
97 while (list->string != NULL) {
98 if (type == list->type)
99 return (list->string);
102 snprintf(buf, sizeof(buf), "%u", type);
108 kdebug_sadb_msg_typestr(type)
111 static const struct typestr list[] = {
125 TYPESTR(X_SPDUPDATE),
127 TYPESTR(X_SPDDELETE),
129 TYPESTR(X_SPDACQUIRE),
132 TYPESTR(X_SPDSETIDX),
133 TYPESTR(X_SPDEXPIRE),
134 TYPESTR(X_SPDDELETE2),
138 return kdebug_typestr(type, list);
142 kdebug_sadb_ext_typestr(type)
145 static const struct typestr list[] = {
146 TYPESTR(EXT_RESERVED),
148 TYPESTR(EXT_LIFETIME_CURRENT),
149 TYPESTR(EXT_LIFETIME_HARD),
150 TYPESTR(EXT_LIFETIME_SOFT),
151 TYPESTR(EXT_ADDRESS_SRC),
152 TYPESTR(EXT_ADDRESS_DST),
153 TYPESTR(EXT_ADDRESS_PROXY),
154 TYPESTR(EXT_KEY_AUTH),
155 TYPESTR(EXT_KEY_ENCRYPT),
156 TYPESTR(EXT_IDENTITY_SRC),
157 TYPESTR(EXT_IDENTITY_DST),
158 TYPESTR(EXT_SENSITIVITY),
159 TYPESTR(EXT_PROPOSAL),
160 TYPESTR(EXT_SUPPORTED_AUTH),
161 TYPESTR(EXT_SUPPORTED_ENCRYPT),
162 TYPESTR(EXT_SPIRANGE),
163 TYPESTR(X_EXT_KMPRIVATE),
164 TYPESTR(X_EXT_POLICY),
169 return kdebug_typestr(type, list);
172 /* NOTE: host byte order */
174 /* %%%: about struct sadb_msg */
177 struct sadb_msg *base;
179 struct sadb_ext *ext;
184 panic("kdebug_sadb: NULL pointer was passed.");
186 printf("sadb_msg{ version=%u type=%s errno=%u satype=%u\n",
187 base->sadb_msg_version,
188 kdebug_sadb_msg_typestr(base->sadb_msg_type),
189 base->sadb_msg_errno, base->sadb_msg_satype);
190 printf(" len=%u reserved=%u seq=%u pid=%u\n",
191 base->sadb_msg_len, base->sadb_msg_reserved,
192 base->sadb_msg_seq, base->sadb_msg_pid);
194 tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
195 ext = (struct sadb_ext *)((caddr_t)base + sizeof(struct sadb_msg));
198 printf("sadb_ext{ len=%u type=%s }\n",
200 kdebug_sadb_ext_typestr(ext->sadb_ext_type));
202 if (ext->sadb_ext_len == 0) {
203 printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
206 if (ext->sadb_ext_len > tlen) {
207 printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
211 switch (ext->sadb_ext_type) {
215 case SADB_EXT_LIFETIME_CURRENT:
216 case SADB_EXT_LIFETIME_HARD:
217 case SADB_EXT_LIFETIME_SOFT:
218 kdebug_sadb_lifetime(ext);
220 case SADB_EXT_ADDRESS_SRC:
221 case SADB_EXT_ADDRESS_DST:
222 case SADB_EXT_ADDRESS_PROXY:
223 kdebug_sadb_address(ext);
225 case SADB_EXT_KEY_AUTH:
226 case SADB_EXT_KEY_ENCRYPT:
227 kdebug_sadb_key(ext);
229 case SADB_EXT_IDENTITY_SRC:
230 case SADB_EXT_IDENTITY_DST:
231 kdebug_sadb_identity(ext);
233 case SADB_EXT_SENSITIVITY:
235 case SADB_EXT_PROPOSAL:
236 kdebug_sadb_prop(ext);
238 case SADB_EXT_SUPPORTED_AUTH:
239 case SADB_EXT_SUPPORTED_ENCRYPT:
240 kdebug_sadb_supported(ext);
242 case SADB_EXT_SPIRANGE:
243 case SADB_X_EXT_KMPRIVATE:
245 case SADB_X_EXT_POLICY:
246 kdebug_sadb_x_policy(ext);
249 kdebug_sadb_x_sa2(ext);
252 printf("kdebug_sadb: invalid ext_type %u was passed.\n",
257 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
259 ext = (struct sadb_ext *)((caddr_t)ext + extlen);
266 kdebug_sadb_prop(ext)
267 struct sadb_ext *ext;
269 struct sadb_prop *prop = (struct sadb_prop *)ext;
270 struct sadb_comb *comb;
275 panic("kdebug_sadb_prop: NULL pointer was passed.");
277 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
279 comb = (struct sadb_comb *)(prop + 1);
280 printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
283 printf("sadb_comb{ auth=%u encrypt=%u "
284 "flags=0x%04x reserved=0x%08x\n",
285 comb->sadb_comb_auth, comb->sadb_comb_encrypt,
286 comb->sadb_comb_flags, comb->sadb_comb_reserved);
288 printf(" auth_minbits=%u auth_maxbits=%u "
289 "encrypt_minbits=%u encrypt_maxbits=%u\n",
290 comb->sadb_comb_auth_minbits,
291 comb->sadb_comb_auth_maxbits,
292 comb->sadb_comb_encrypt_minbits,
293 comb->sadb_comb_encrypt_maxbits);
295 printf(" soft_alloc=%u hard_alloc=%u "
296 "soft_bytes=%lu hard_bytes=%lu\n",
297 comb->sadb_comb_soft_allocations,
298 comb->sadb_comb_hard_allocations,
299 (unsigned long)comb->sadb_comb_soft_bytes,
300 (unsigned long)comb->sadb_comb_hard_bytes);
302 printf(" soft_alloc=%lu hard_alloc=%lu "
303 "soft_bytes=%lu hard_bytes=%lu }\n",
304 (unsigned long)comb->sadb_comb_soft_addtime,
305 (unsigned long)comb->sadb_comb_hard_addtime,
306 (unsigned long)comb->sadb_comb_soft_usetime,
307 (unsigned long)comb->sadb_comb_hard_usetime);
316 kdebug_sadb_identity(ext)
317 struct sadb_ext *ext;
319 struct sadb_ident *id = (struct sadb_ident *)ext;
324 panic("kdebug_sadb_identity: NULL pointer was passed.");
326 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
327 printf("sadb_ident_%s{",
328 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
329 switch (id->sadb_ident_type) {
331 printf(" type=%u id=%lu",
332 id->sadb_ident_type, (u_long)id->sadb_ident_id);
335 ipsec_hexdump((caddr_t)(id + 1), len); /*XXX cast ?*/
339 p = (char *)(id + 1);
341 for (/*nothing*/; *p && p < ep; p++) {
343 printf("%c", *p & 0xff);
345 printf("\\%03o", *p & 0xff);
359 kdebug_sadb_supported(ext)
360 struct sadb_ext *ext;
362 struct sadb_supported *sup = (struct sadb_supported *)ext;
363 struct sadb_alg *alg;
368 panic("kdebug_sadb_supported: NULL pointer was passed.");
370 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
372 alg = (struct sadb_alg *)(sup + 1);
373 printf("sadb_sup{\n");
375 printf(" { id=%u ivlen=%u min=%u max=%u }\n",
376 alg->sadb_alg_id, alg->sadb_alg_ivlen,
377 alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
386 kdebug_sadb_lifetime(ext)
387 struct sadb_ext *ext;
389 struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
393 printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
395 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
396 lft->sadb_lifetime_allocations,
397 (u_int32_t)lft->sadb_lifetime_bytes);
398 printf(" addtime=%u, usetime=%u }\n",
399 (u_int32_t)lft->sadb_lifetime_addtime,
400 (u_int32_t)lft->sadb_lifetime_usetime);
407 struct sadb_ext *ext;
409 struct sadb_sa *sa = (struct sadb_sa *)ext;
413 panic("kdebug_sadb_sa: NULL pointer was passed.");
415 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
416 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
418 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
419 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
425 kdebug_sadb_address(ext)
426 struct sadb_ext *ext;
428 struct sadb_address *addr = (struct sadb_address *)ext;
432 panic("kdebug_sadb_address: NULL pointer was passed.");
434 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
435 addr->sadb_address_proto, addr->sadb_address_prefixlen,
436 ((u_char *)&addr->sadb_address_reserved)[0],
437 ((u_char *)&addr->sadb_address_reserved)[1]);
439 kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr)));
446 struct sadb_ext *ext;
448 struct sadb_key *key = (struct sadb_key *)ext;
452 panic("kdebug_sadb_key: NULL pointer was passed.");
454 printf("sadb_key{ bits=%u reserved=%u\n",
455 key->sadb_key_bits, key->sadb_key_reserved);
459 if ((key->sadb_key_bits >> 3) >
460 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
461 printf("kdebug_sadb_key: key length mismatch, bit:%u len:%ld.\n",
462 key->sadb_key_bits >> 3,
463 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
466 ipsec_hexdump((caddr_t)key + sizeof(struct sadb_key),
467 key->sadb_key_bits >> 3);
473 kdebug_sadb_x_sa2(ext)
474 struct sadb_ext *ext;
476 struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext;
480 panic("kdebug_sadb_x_sa2: NULL pointer was passed.");
482 printf("sadb_x_sa2{ mode=%u reqid=%u\n",
483 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
484 printf(" reserved1=%u reserved2=%u sequence=%u }\n",
485 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
486 sa2->sadb_x_sa2_sequence);
492 kdebug_sadb_x_policy(ext)
493 struct sadb_ext *ext;
495 struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
496 struct sockaddr *addr;
500 panic("kdebug_sadb_x_policy: NULL pointer was passed.");
502 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
503 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
504 xpl->sadb_x_policy_id);
506 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
508 struct sadb_x_ipsecrequest *xisr;
510 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
511 xisr = (struct sadb_x_ipsecrequest *)(xpl + 1);
514 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
515 xisr->sadb_x_ipsecrequest_len,
516 xisr->sadb_x_ipsecrequest_proto,
517 xisr->sadb_x_ipsecrequest_mode,
518 xisr->sadb_x_ipsecrequest_level,
519 xisr->sadb_x_ipsecrequest_reqid);
521 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
522 addr = (struct sockaddr *)(xisr + 1);
523 kdebug_sockaddr(addr);
524 addr = (struct sockaddr *)((caddr_t)addr
526 kdebug_sockaddr(addr);
531 /* prevent infinite loop */
532 if (xisr->sadb_x_ipsecrequest_len <= 0) {
533 printf("kdebug_sadb_x_policy: wrong policy struct.\n");
536 /* prevent overflow */
537 if (xisr->sadb_x_ipsecrequest_len > tlen) {
538 printf("invalid ipsec policy length\n");
542 tlen -= xisr->sadb_x_ipsecrequest_len;
544 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
545 + xisr->sadb_x_ipsecrequest_len);
549 panic("kdebug_sadb_x_policy: wrong policy struct.");
556 /* %%%: about SPD and SAD */
559 struct secpolicy *sp;
563 panic("kdebug_secpolicy: NULL pointer was passed.");
565 printf("secpolicy{ refcnt=%u state=%u policy=%u dir=%u\n",
566 sp->refcnt, sp->state, sp->policy, sp->dir);
569 kdebug_secpolicyindex(sp->spidx);
571 switch (sp->policy) {
572 case IPSEC_POLICY_DISCARD:
573 printf(" type=discard }\n");
575 case IPSEC_POLICY_NONE:
576 printf(" type=none }\n");
578 case IPSEC_POLICY_IPSEC:
580 struct ipsecrequest *isr;
581 for (isr = sp->req; isr != NULL; isr = isr->next) {
583 printf(" level=%u\n", isr->level);
584 kdebug_secasindex(&isr->saidx);
586 if (isr->sav != NULL)
587 kdebug_secasv(isr->sav);
592 case IPSEC_POLICY_BYPASS:
593 printf(" type=bypass }\n");
595 case IPSEC_POLICY_ENTRUST:
596 printf(" type=entrust }\n");
599 printf("kdebug_secpolicy: Invalid policy found. %u\n",
608 kdebug_secpolicyindex(spidx)
609 struct secpolicyindex *spidx;
613 panic("kdebug_secpolicyindex: NULL pointer was passed.");
615 printf("secpolicyindex{ prefs=%u prefd=%u ul_proto=%u\n",
616 spidx->prefs, spidx->prefd, spidx->ul_proto);
618 ipsec_hexdump((caddr_t)&spidx->src,
619 ((struct sockaddr *)&spidx->src)->sa_len);
621 ipsec_hexdump((caddr_t)&spidx->dst,
622 ((struct sockaddr *)&spidx->dst)->sa_len);
629 kdebug_secasindex(saidx)
630 struct secasindex *saidx;
634 panic("kdebug_secpolicyindex: NULL pointer was passed.");
636 printf("secasindex{ mode=%u proto=%u\n", saidx->mode, saidx->proto);
638 ipsec_hexdump((caddr_t)&saidx->src,
639 ((struct sockaddr *)&saidx->src)->sa_len);
641 ipsec_hexdump((caddr_t)&saidx->dst,
642 ((struct sockaddr *)&saidx->dst)->sa_len);
650 struct secasvar *sav;
654 panic("kdebug_secasv: NULL pointer was passed.");
657 kdebug_secasindex(&sav->sah->saidx);
659 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
660 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
661 printf(" spi=%u flags=%u\n",
662 (u_int32_t)ntohl(sav->spi), sav->flags);
664 if (sav->key_auth != NULL)
665 kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
666 if (sav->key_enc != NULL)
667 kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
668 if (sav->iv != NULL) {
670 ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
674 if (sav->replay != NULL)
675 kdebug_secreplay(sav->replay);
676 if (sav->lft_c != NULL)
677 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
678 if (sav->lft_h != NULL)
679 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
680 if (sav->lft_s != NULL)
681 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
684 /* XXX: misc[123] ? */
691 kdebug_secreplay(rpl)
692 struct secreplay *rpl;
698 panic("kdebug_secreplay: NULL pointer was passed.");
700 printf(" secreplay{ count=%llu wsize=%u seq=%llu lastseq=%llu",
701 (unsigned long long)rpl->count, rpl->wsize,
702 (unsigned long long)rpl->seq, (unsigned long long)rpl->lastseq);
704 if (rpl->bitmap == NULL) {
709 printf("\n bitmap { ");
711 for (len = 0; len < rpl->wsize; len++) {
712 for (l = 7; l >= 0; l--)
713 printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
728 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
729 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
730 m, m->m_next, m->m_nextpkt, m->m_data,
731 m->m_len, m->m_type, m->m_flags);
733 if (m->m_flags & M_PKTHDR) {
734 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
735 m->m_pkthdr.len, m->m_pkthdr.rcvif);
738 if (m->m_flags & M_EXT) {
739 printf(" m_ext{ ext_buf:%p ext_free:%p "
741 m->m_ext.ext_buf, m->m_ext.ext_free,
755 for (j = 0; m; m = m->m_next) {
757 printf(" m_data:\n");
758 for (i = 0; i < m->m_len; i++) {
759 if (i && i % 32 == 0)
763 printf("%02x", mtod(m, u_char *)[i]);
774 kdebug_sockaddr(addr)
775 struct sockaddr *addr;
777 struct sockaddr_in *sin4;
779 struct sockaddr_in6 *sin6;
784 panic("kdebug_sockaddr: NULL pointer was passed.");
786 /* NOTE: We deal with port number as host byte order. */
787 printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
789 switch (addr->sa_family) {
791 sin4 = (struct sockaddr_in *)addr;
792 printf(" port=%u\n", ntohs(sin4->sin_port));
793 ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr));
797 sin6 = (struct sockaddr_in6 *)addr;
798 printf(" port=%u\n", ntohs(sin6->sin6_port));
799 printf(" flowinfo=0x%08x, scope_id=0x%08x\n",
800 sin6->sin6_flowinfo, sin6->sin6_scope_id);
801 ipsec_hexdump((caddr_t)&sin6->sin6_addr,
802 sizeof(sin6->sin6_addr));
813 ipsec_bindump(buf, len)
819 for (i = 0; i < len; i++)
820 printf("%c", (unsigned char)buf[i]);
827 ipsec_hexdump(buf, len)
833 for (i = 0; i < len; i++) {
834 if (i != 0 && i % 32 == 0) printf("\n");
835 if (i % 4 == 0) printf(" ");
836 printf("%02x", (unsigned char)buf[i]);
839 if (i % 32 != 0) printf("\n");