1 /* $FreeBSD: src/sys/netkey/key_debug.c,v 1.10.2.5 2002/04/28 05:40:28 suz Exp $ */
2 /* $DragonFly: src/sys/netproto/key/key_debug.c,v 1.9 2008/09/16 09:25:45 swildner Exp $ */
3 /* $KAME: key_debug.c,v 1.26 2001/06/27 10:46:50 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "opt_inet6.h"
37 #include "opt_ipsec.h"
40 #include <sys/types.h>
41 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/queue.h>
47 #include <sys/socket.h>
49 #include <net/route.h>
52 #include "key_debug.h"
54 #include <netinet/in.h>
55 #include <netinet6/ipsec.h>
63 static void kdebug_sadb_prop (struct sadb_ext *);
64 static void kdebug_sadb_identity (struct sadb_ext *);
65 static void kdebug_sadb_supported (struct sadb_ext *);
66 static void kdebug_sadb_lifetime (struct sadb_ext *);
67 static void kdebug_sadb_sa (struct sadb_ext *);
68 static void kdebug_sadb_address (struct sadb_ext *);
69 static void kdebug_sadb_key (struct sadb_ext *);
70 static void kdebug_sadb_x_sa2 (struct sadb_ext *);
73 static void kdebug_secreplay (struct secreplay *);
77 #define kprintf printf
78 #define panic(param) { printf(param); exit(-1); }
81 /* NOTE: host byte order */
83 /* %%%: about struct sadb_msg */
85 kdebug_sadb(struct sadb_msg *base)
92 panic("kdebug_sadb: NULL pointer was passed.\n");
94 kprintf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
95 base->sadb_msg_version, base->sadb_msg_type,
96 base->sadb_msg_errno, base->sadb_msg_satype);
97 kprintf(" len=%u reserved=%u seq=%u pid=%u\n",
98 base->sadb_msg_len, base->sadb_msg_reserved,
99 base->sadb_msg_seq, base->sadb_msg_pid);
101 tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
102 ext = (struct sadb_ext *)((caddr_t)base + sizeof(struct sadb_msg));
105 kprintf("sadb_ext{ len=%u type=%u }\n",
106 ext->sadb_ext_len, ext->sadb_ext_type);
108 if (ext->sadb_ext_len == 0) {
109 kprintf("kdebug_sadb: invalid ext_len=0 was passed.\n");
112 if (ext->sadb_ext_len > tlen) {
113 kprintf("kdebug_sadb: ext_len exceeds end of buffer.\n");
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 kprintf("kdebug_sadb: invalid ext_type %u was passed.\n",
163 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
165 ext = (struct sadb_ext *)((caddr_t)ext + extlen);
172 kdebug_sadb_prop(struct sadb_ext *ext)
174 struct sadb_prop *prop = (struct sadb_prop *)ext;
175 struct sadb_comb *comb;
180 panic("kdebug_sadb_prop: NULL pointer was passed.\n");
182 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
184 comb = (struct sadb_comb *)(prop + 1);
185 kprintf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
188 kprintf("sadb_comb{ auth=%u encrypt=%u "
189 "flags=0x%04x reserved=0x%08x\n",
190 comb->sadb_comb_auth, comb->sadb_comb_encrypt,
191 comb->sadb_comb_flags, comb->sadb_comb_reserved);
193 kprintf(" auth_minbits=%u auth_maxbits=%u "
194 "encrypt_minbits=%u encrypt_maxbits=%u\n",
195 comb->sadb_comb_auth_minbits,
196 comb->sadb_comb_auth_maxbits,
197 comb->sadb_comb_encrypt_minbits,
198 comb->sadb_comb_encrypt_maxbits);
200 kprintf(" soft_alloc=%u hard_alloc=%u "
201 "soft_bytes=%lu hard_bytes=%lu\n",
202 comb->sadb_comb_soft_allocations,
203 comb->sadb_comb_hard_allocations,
204 (unsigned long)comb->sadb_comb_soft_bytes,
205 (unsigned long)comb->sadb_comb_hard_bytes);
207 kprintf(" soft_alloc=%lu hard_alloc=%lu "
208 "soft_bytes=%lu hard_bytes=%lu }\n",
209 (unsigned long)comb->sadb_comb_soft_addtime,
210 (unsigned long)comb->sadb_comb_hard_addtime,
211 (unsigned long)comb->sadb_comb_soft_usetime,
212 (unsigned long)comb->sadb_comb_hard_usetime);
221 kdebug_sadb_identity(struct sadb_ext *ext)
223 struct sadb_ident *id = (struct sadb_ident *)ext;
228 panic("kdebug_sadb_identity: NULL pointer was passed.\n");
230 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
231 kprintf("sadb_ident_%s{",
232 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
233 switch (id->sadb_ident_type) {
235 kprintf(" type=%d id=%lu",
236 id->sadb_ident_type, (u_long)id->sadb_ident_id);
239 ipsec_hexdump((caddr_t)(id + 1), len); /*XXX cast ?*/
242 kprintf("\n str=\"");
243 p = (char *)(id + 1);
245 for (/*nothing*/; *p && p < ep; p++) {
247 kprintf("%c", *p & 0xff);
249 kprintf("\\%03o", *p & 0xff);
263 kdebug_sadb_supported(struct sadb_ext *ext)
265 struct sadb_supported *sup = (struct sadb_supported *)ext;
266 struct sadb_alg *alg;
271 panic("kdebug_sadb_supported: NULL pointer was passed.\n");
273 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
275 alg = (struct sadb_alg *)(sup + 1);
276 kprintf("sadb_sup{\n");
278 kprintf(" { id=%d ivlen=%d min=%d max=%d }\n",
279 alg->sadb_alg_id, alg->sadb_alg_ivlen,
280 alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
289 kdebug_sadb_lifetime(struct sadb_ext *ext)
291 struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
295 panic("kdebug_sadb_lifetime: NULL pointer was passed.\n");
297 kprintf("sadb_lifetime{ alloc=%u, bytes=%u\n",
298 lft->sadb_lifetime_allocations,
299 (u_int32_t)lft->sadb_lifetime_bytes);
300 kprintf(" addtime=%u, usetime=%u }\n",
301 (u_int32_t)lft->sadb_lifetime_addtime,
302 (u_int32_t)lft->sadb_lifetime_usetime);
308 kdebug_sadb_sa(struct sadb_ext *ext)
310 struct sadb_sa *sa = (struct sadb_sa *)ext;
314 panic("kdebug_sadb_sa: NULL pointer was passed.\n");
316 kprintf("sadb_sa{ spi=%u replay=%u state=%u\n",
317 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
319 kprintf(" auth=%u encrypt=%u flags=0x%08x }\n",
320 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
326 kdebug_sadb_address(struct sadb_ext *ext)
328 struct sadb_address *addr = (struct sadb_address *)ext;
332 panic("kdebug_sadb_address: NULL pointer was passed.\n");
334 kprintf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
335 addr->sadb_address_proto, addr->sadb_address_prefixlen,
336 ((u_char *)&addr->sadb_address_reserved)[0],
337 ((u_char *)&addr->sadb_address_reserved)[1]);
339 kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr)));
345 kdebug_sadb_key(struct sadb_ext *ext)
347 struct sadb_key *key = (struct sadb_key *)ext;
351 panic("kdebug_sadb_key: NULL pointer was passed.\n");
353 kprintf("sadb_key{ bits=%u reserved=%u\n",
354 key->sadb_key_bits, key->sadb_key_reserved);
358 if ((key->sadb_key_bits >> 3) >
359 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
360 kprintf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
361 key->sadb_key_bits >> 3,
362 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
365 ipsec_hexdump((caddr_t)key + sizeof(struct sadb_key),
366 key->sadb_key_bits >> 3);
372 kdebug_sadb_x_sa2(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 kprintf("sadb_x_sa2{ mode=%u reqid=%u\n",
381 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
382 kprintf(" reserved1=%u reserved2=%u sequence=%u }\n",
383 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
384 sa2->sadb_x_sa2_sequence);
390 kdebug_sadb_x_policy(struct sadb_ext *ext)
392 struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
393 struct sockaddr *addr;
397 panic("kdebug_sadb_x_policy: NULL pointer was passed.\n");
399 kprintf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
400 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
401 xpl->sadb_x_policy_id);
403 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
405 struct sadb_x_ipsecrequest *xisr;
407 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
408 xisr = (struct sadb_x_ipsecrequest *)(xpl + 1);
411 kprintf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
412 xisr->sadb_x_ipsecrequest_len,
413 xisr->sadb_x_ipsecrequest_proto,
414 xisr->sadb_x_ipsecrequest_mode,
415 xisr->sadb_x_ipsecrequest_level,
416 xisr->sadb_x_ipsecrequest_reqid);
418 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
419 addr = (struct sockaddr *)(xisr + 1);
420 kdebug_sockaddr(addr);
421 addr = (struct sockaddr *)((caddr_t)addr
423 kdebug_sockaddr(addr);
428 /* prevent infinite loop */
429 if (xisr->sadb_x_ipsecrequest_len <= 0) {
430 kprintf("kdebug_sadb_x_policy: wrong policy struct.\n");
433 /* prevent overflow */
434 if (xisr->sadb_x_ipsecrequest_len > tlen) {
435 kprintf("invalid ipsec policy length\n");
439 tlen -= xisr->sadb_x_ipsecrequest_len;
441 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
442 + xisr->sadb_x_ipsecrequest_len);
446 panic("kdebug_sadb_x_policy: wrong policy struct.\n");
453 /* %%%: about SPD and SAD */
455 kdebug_secpolicy(struct secpolicy *sp)
459 panic("kdebug_secpolicy: NULL pointer was passed.\n");
461 kprintf("secpolicy{ refcnt=%u state=%u policy=%u\n",
462 sp->refcnt, sp->state, sp->policy);
464 kdebug_secpolicyindex(&sp->spidx);
466 switch (sp->policy) {
467 case IPSEC_POLICY_DISCARD:
468 kprintf(" type=discard }\n");
470 case IPSEC_POLICY_NONE:
471 kprintf(" type=none }\n");
473 case IPSEC_POLICY_IPSEC:
475 struct ipsecrequest *isr;
476 for (isr = sp->req; isr != NULL; isr = isr->next) {
478 kprintf(" level=%u\n", isr->level);
479 kdebug_secasindex(&isr->saidx);
481 if (isr->sav != NULL)
482 kdebug_secasv(isr->sav);
487 case IPSEC_POLICY_BYPASS:
488 kprintf(" type=bypass }\n");
490 case IPSEC_POLICY_ENTRUST:
491 kprintf(" type=entrust }\n");
494 kprintf("kdebug_secpolicy: Invalid policy found. %d\n",
503 kdebug_secpolicyindex(struct secpolicyindex *spidx)
507 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
509 kprintf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
510 spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
512 ipsec_hexdump((caddr_t)&spidx->src,
513 ((struct sockaddr *)&spidx->src)->sa_len);
515 ipsec_hexdump((caddr_t)&spidx->dst,
516 ((struct sockaddr *)&spidx->dst)->sa_len);
523 kdebug_secasindex(struct secasindex *saidx)
527 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
529 kprintf("secasindex{ mode=%u proto=%u\n",
530 saidx->mode, saidx->proto);
532 ipsec_hexdump((caddr_t)&saidx->src,
533 ((struct sockaddr *)&saidx->src)->sa_len);
535 ipsec_hexdump((caddr_t)&saidx->dst,
536 ((struct sockaddr *)&saidx->dst)->sa_len);
543 kdebug_secasv(struct secasvar *sav)
547 panic("kdebug_secasv: NULL pointer was passed.\n");
550 kdebug_secasindex(&sav->sah->saidx);
552 kprintf(" refcnt=%u state=%u auth=%u enc=%u\n",
553 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
554 kprintf(" spi=%u flags=%u\n",
555 (u_int32_t)ntohl(sav->spi), sav->flags);
557 if (sav->key_auth != NULL)
558 kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
559 if (sav->key_enc != NULL)
560 kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
561 if (sav->iv != NULL) {
563 ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
567 if (sav->replay != NULL)
568 kdebug_secreplay(sav->replay);
569 if (sav->lft_c != NULL)
570 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
571 if (sav->lft_h != NULL)
572 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
573 if (sav->lft_s != NULL)
574 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
577 /* XXX: misc[123] ? */
584 kdebug_secreplay(struct secreplay *rpl)
590 panic("kdebug_secreplay: NULL pointer was passed.\n");
592 kprintf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
593 rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
595 if (rpl->bitmap == NULL) {
600 kprintf("\n bitmap { ");
602 for (len = 0; len < rpl->wsize; len++) {
603 for (l = 7; l >= 0; l--)
604 kprintf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
612 kdebug_mbufhdr(struct mbuf *m)
618 kprintf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
619 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
620 m, m->m_next, m->m_nextpkt, m->m_data,
621 m->m_len, m->m_type, m->m_flags);
623 if (m->m_flags & M_PKTHDR) {
624 kprintf(" m_pkthdr{ len:%d rcvif:%p }\n",
625 m->m_pkthdr.len, m->m_pkthdr.rcvif);
628 if (m->m_flags & M_EXT) {
629 kprintf(" m_ext{ ext_buf:%p ext_free:%p "
630 "ext_size:%u ext_ref:%p }\n",
631 m->m_ext.ext_buf, m->m_ext.ext_free,
632 m->m_ext.ext_size, m->m_ext.ext_ref);
639 kdebug_mbuf(struct mbuf *m0)
644 for (j = 0; m; m = m->m_next) {
646 kprintf(" m_data:\n");
647 for (i = 0; i < m->m_len; i++) {
648 if (i && i % 32 == 0)
652 kprintf("%02x", mtod(m, u_char *)[i]);
663 kdebug_sockaddr(struct sockaddr *addr)
665 struct sockaddr_in *sin4;
667 struct sockaddr_in6 *sin6;
672 panic("kdebug_sockaddr: NULL pointer was passed.\n");
674 /* NOTE: We deal with port number as host byte order. */
675 kprintf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
677 switch (addr->sa_family) {
679 sin4 = (struct sockaddr_in *)addr;
680 kprintf(" port=%u\n", ntohs(sin4->sin_port));
681 ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr));
685 sin6 = (struct sockaddr_in6 *)addr;
686 kprintf(" port=%u\n", ntohs(sin6->sin6_port));
687 kprintf(" flowinfo=0x%08x, scope_id=0x%08x\n",
688 sin6->sin6_flowinfo, sin6->sin6_scope_id);
689 ipsec_hexdump((caddr_t)&sin6->sin6_addr,
690 sizeof(sin6->sin6_addr));
701 ipsec_bindump(caddr_t buf, int len)
705 for (i = 0; i < len; i++)
706 kprintf("%c", (unsigned char)buf[i]);
713 ipsec_hexdump(caddr_t buf, int len)
717 for (i = 0; i < len; i++) {
718 if (i != 0 && i % 32 == 0) kprintf("\n");
719 if (i % 4 == 0) kprintf(" ");
720 kprintf("%02x", (unsigned char)buf[i]);
723 if (i % 32 != 0) kprintf("\n");