1 /* $FreeBSD: src/sys/netipsec/key_debug.c,v 1.1.4.1 2003/01/24 05:11:36 sam 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>
50 #include <netproto/ipsec/key_var.h>
51 #include <netproto/ipsec/key_debug.h>
53 #include <netinet/in.h>
54 #include <netproto/ipsec/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 panic(param) { kprintf(param); exit(-1); }
79 /* NOTE: host byte order */
81 /* %%%: about struct sadb_msg */
83 kdebug_sadb(struct sadb_msg *base)
90 panic("kdebug_sadb: NULL pointer was passed.");
92 kprintf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
93 base->sadb_msg_version, base->sadb_msg_type,
94 base->sadb_msg_errno, base->sadb_msg_satype);
95 kprintf(" len=%u reserved=%u seq=%u pid=%u\n",
96 base->sadb_msg_len, base->sadb_msg_reserved,
97 base->sadb_msg_seq, base->sadb_msg_pid);
99 tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
100 ext = (struct sadb_ext *)((caddr_t)base + sizeof(struct sadb_msg));
103 kprintf("sadb_ext{ len=%u type=%u }\n",
104 ext->sadb_ext_len, ext->sadb_ext_type);
106 if (ext->sadb_ext_len == 0) {
107 kprintf("kdebug_sadb: invalid ext_len=0 was passed.\n");
110 if (ext->sadb_ext_len > tlen) {
111 kprintf("kdebug_sadb: ext_len exceeds end of buffer.\n");
115 switch (ext->sadb_ext_type) {
119 case SADB_EXT_LIFETIME_CURRENT:
120 case SADB_EXT_LIFETIME_HARD:
121 case SADB_EXT_LIFETIME_SOFT:
122 kdebug_sadb_lifetime(ext);
124 case SADB_EXT_ADDRESS_SRC:
125 case SADB_EXT_ADDRESS_DST:
126 case SADB_EXT_ADDRESS_PROXY:
127 kdebug_sadb_address(ext);
129 case SADB_EXT_KEY_AUTH:
130 case SADB_EXT_KEY_ENCRYPT:
131 kdebug_sadb_key(ext);
133 case SADB_EXT_IDENTITY_SRC:
134 case SADB_EXT_IDENTITY_DST:
135 kdebug_sadb_identity(ext);
137 case SADB_EXT_SENSITIVITY:
139 case SADB_EXT_PROPOSAL:
140 kdebug_sadb_prop(ext);
142 case SADB_EXT_SUPPORTED_AUTH:
143 case SADB_EXT_SUPPORTED_ENCRYPT:
144 kdebug_sadb_supported(ext);
146 case SADB_EXT_SPIRANGE:
147 case SADB_X_EXT_KMPRIVATE:
149 case SADB_X_EXT_POLICY:
150 kdebug_sadb_x_policy(ext);
153 kdebug_sadb_x_sa2(ext);
156 kprintf("kdebug_sadb: invalid ext_type %u was passed.\n",
161 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
163 ext = (struct sadb_ext *)((caddr_t)ext + extlen);
170 kdebug_sadb_prop(struct sadb_ext *ext)
172 struct sadb_prop *prop = (struct sadb_prop *)ext;
173 struct sadb_comb *comb;
178 panic("kdebug_sadb_prop: NULL pointer was passed.");
180 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
182 comb = (struct sadb_comb *)(prop + 1);
183 kprintf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
186 kprintf("sadb_comb{ auth=%u encrypt=%u "
187 "flags=0x%04x reserved=0x%08x\n",
188 comb->sadb_comb_auth, comb->sadb_comb_encrypt,
189 comb->sadb_comb_flags, comb->sadb_comb_reserved);
191 kprintf(" auth_minbits=%u auth_maxbits=%u "
192 "encrypt_minbits=%u encrypt_maxbits=%u\n",
193 comb->sadb_comb_auth_minbits,
194 comb->sadb_comb_auth_maxbits,
195 comb->sadb_comb_encrypt_minbits,
196 comb->sadb_comb_encrypt_maxbits);
198 kprintf(" soft_alloc=%u hard_alloc=%u "
199 "soft_bytes=%lu hard_bytes=%lu\n",
200 comb->sadb_comb_soft_allocations,
201 comb->sadb_comb_hard_allocations,
202 (unsigned long)comb->sadb_comb_soft_bytes,
203 (unsigned long)comb->sadb_comb_hard_bytes);
205 kprintf(" soft_alloc=%lu hard_alloc=%lu "
206 "soft_bytes=%lu hard_bytes=%lu }\n",
207 (unsigned long)comb->sadb_comb_soft_addtime,
208 (unsigned long)comb->sadb_comb_hard_addtime,
209 (unsigned long)comb->sadb_comb_soft_usetime,
210 (unsigned long)comb->sadb_comb_hard_usetime);
219 kdebug_sadb_identity(struct sadb_ext *ext)
221 struct sadb_ident *id = (struct sadb_ident *)ext;
226 panic("kdebug_sadb_identity: NULL pointer was passed.");
228 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
229 kprintf("sadb_ident_%s{",
230 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
231 switch (id->sadb_ident_type) {
233 kprintf(" type=%d id=%lu",
234 id->sadb_ident_type, (u_long)id->sadb_ident_id);
237 ipsec_hexdump((caddr_t)(id + 1), len); /*XXX cast ?*/
240 kprintf("\n str=\"");
241 p = (char *)(id + 1);
243 for (/*nothing*/; *p && p < ep; p++) {
245 kprintf("%c", *p & 0xff);
247 kprintf("\\%03o", *p & 0xff);
261 kdebug_sadb_supported(struct sadb_ext *ext)
263 struct sadb_supported *sup = (struct sadb_supported *)ext;
264 struct sadb_alg *alg;
269 panic("kdebug_sadb_supported: NULL pointer was passed.");
271 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
273 alg = (struct sadb_alg *)(sup + 1);
274 kprintf("sadb_sup{\n");
276 kprintf(" { id=%d ivlen=%d min=%d max=%d }\n",
277 alg->sadb_alg_id, alg->sadb_alg_ivlen,
278 alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
287 kdebug_sadb_lifetime(struct sadb_ext *ext)
289 struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
293 kprintf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
295 kprintf("sadb_lifetime{ alloc=%u, bytes=%u\n",
296 lft->sadb_lifetime_allocations,
297 (u_int32_t)lft->sadb_lifetime_bytes);
298 kprintf(" addtime=%u, usetime=%u }\n",
299 (u_int32_t)lft->sadb_lifetime_addtime,
300 (u_int32_t)lft->sadb_lifetime_usetime);
306 kdebug_sadb_sa(struct sadb_ext *ext)
308 struct sadb_sa *sa = (struct sadb_sa *)ext;
312 panic("kdebug_sadb_sa: NULL pointer was passed.");
314 kprintf("sadb_sa{ spi=%u replay=%u state=%u\n",
315 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
317 kprintf(" auth=%u encrypt=%u flags=0x%08x }\n",
318 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
324 kdebug_sadb_address(struct sadb_ext *ext)
326 struct sadb_address *addr = (struct sadb_address *)ext;
330 panic("kdebug_sadb_address: NULL pointer was passed.");
332 kprintf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
333 addr->sadb_address_proto, addr->sadb_address_prefixlen,
334 ((u_char *)&addr->sadb_address_reserved)[0],
335 ((u_char *)&addr->sadb_address_reserved)[1]);
337 kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr)));
343 kdebug_sadb_key(struct sadb_ext *ext)
345 struct sadb_key *key = (struct sadb_key *)ext;
349 panic("kdebug_sadb_key: NULL pointer was passed.");
351 kprintf("sadb_key{ bits=%u reserved=%u\n",
352 key->sadb_key_bits, key->sadb_key_reserved);
356 if ((key->sadb_key_bits >> 3) >
357 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
358 kprintf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
359 key->sadb_key_bits >> 3,
360 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
363 ipsec_hexdump((caddr_t)key + sizeof(struct sadb_key),
364 key->sadb_key_bits >> 3);
370 kdebug_sadb_x_sa2(struct sadb_ext *ext)
372 struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext;
376 panic("kdebug_sadb_x_sa2: NULL pointer was passed.");
378 kprintf("sadb_x_sa2{ mode=%u reqid=%u\n",
379 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
380 kprintf(" reserved1=%u reserved2=%u sequence=%u }\n",
381 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
382 sa2->sadb_x_sa2_sequence);
388 kdebug_sadb_x_policy(struct sadb_ext *ext)
390 struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
391 struct sockaddr *addr;
395 panic("kdebug_sadb_x_policy: NULL pointer was passed.");
397 kprintf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
398 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
399 xpl->sadb_x_policy_id);
401 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
403 struct sadb_x_ipsecrequest *xisr;
405 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
406 xisr = (struct sadb_x_ipsecrequest *)(xpl + 1);
409 kprintf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
410 xisr->sadb_x_ipsecrequest_len,
411 xisr->sadb_x_ipsecrequest_proto,
412 xisr->sadb_x_ipsecrequest_mode,
413 xisr->sadb_x_ipsecrequest_level,
414 xisr->sadb_x_ipsecrequest_reqid);
416 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
417 addr = (struct sockaddr *)(xisr + 1);
418 kdebug_sockaddr(addr);
419 addr = (struct sockaddr *)((caddr_t)addr
421 kdebug_sockaddr(addr);
426 /* prevent infinite loop */
427 if (xisr->sadb_x_ipsecrequest_len <= 0) {
428 kprintf("kdebug_sadb_x_policy: wrong policy struct.\n");
431 /* prevent overflow */
432 if (xisr->sadb_x_ipsecrequest_len > tlen) {
433 kprintf("invalid ipsec policy length\n");
437 tlen -= xisr->sadb_x_ipsecrequest_len;
439 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
440 + xisr->sadb_x_ipsecrequest_len);
444 panic("kdebug_sadb_x_policy: wrong policy struct.");
451 /* %%%: about SPD and SAD */
453 kdebug_secpolicy(struct secpolicy *sp)
457 panic("kdebug_secpolicy: NULL pointer was passed.");
459 kprintf("secpolicy{ refcnt=%u state=%u policy=%u\n",
460 sp->refcnt, sp->state, sp->policy);
462 kdebug_secpolicyindex(&sp->spidx);
464 switch (sp->policy) {
465 case IPSEC_POLICY_DISCARD:
466 kprintf(" type=discard }\n");
468 case IPSEC_POLICY_NONE:
469 kprintf(" type=none }\n");
471 case IPSEC_POLICY_IPSEC:
473 struct ipsecrequest *isr;
474 for (isr = sp->req; isr != NULL; isr = isr->next) {
476 kprintf(" level=%u\n", isr->level);
477 kdebug_secasindex(&isr->saidx);
479 if (isr->sav != NULL)
480 kdebug_secasv(isr->sav);
485 case IPSEC_POLICY_BYPASS:
486 kprintf(" type=bypass }\n");
488 case IPSEC_POLICY_ENTRUST:
489 kprintf(" type=entrust }\n");
492 kprintf("kdebug_secpolicy: Invalid policy found. %d\n",
501 kdebug_secpolicyindex(struct secpolicyindex *spidx)
505 panic("kdebug_secpolicyindex: NULL pointer was passed.");
507 kprintf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
508 spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
510 ipsec_hexdump((caddr_t)&spidx->src,
511 ((struct sockaddr *)&spidx->src)->sa_len);
513 ipsec_hexdump((caddr_t)&spidx->dst,
514 ((struct sockaddr *)&spidx->dst)->sa_len);
521 kdebug_secasindex(struct secasindex *saidx)
525 panic("kdebug_secpolicyindex: NULL pointer was passed.");
527 kprintf("secasindex{ mode=%u proto=%u\n",
528 saidx->mode, saidx->proto);
530 ipsec_hexdump((caddr_t)&saidx->src,
531 ((struct sockaddr *)&saidx->src)->sa_len);
533 ipsec_hexdump((caddr_t)&saidx->dst,
534 ((struct sockaddr *)&saidx->dst)->sa_len);
541 kdebug_secasv(struct secasvar *sav)
545 panic("kdebug_secasv: NULL pointer was passed.");
548 kdebug_secasindex(&sav->sah->saidx);
550 kprintf(" refcnt=%u state=%u auth=%u enc=%u\n",
551 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
552 kprintf(" spi=%u flags=%u\n",
553 (u_int32_t)ntohl(sav->spi), sav->flags);
555 if (sav->key_auth != NULL)
556 kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
557 if (sav->key_enc != NULL)
558 kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
559 if (sav->iv != NULL) {
561 ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
565 if (sav->replay != NULL)
566 kdebug_secreplay(sav->replay);
567 if (sav->lft_c != NULL)
568 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
569 if (sav->lft_h != NULL)
570 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
571 if (sav->lft_s != NULL)
572 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
575 /* XXX: misc[123] ? */
582 kdebug_secreplay(struct secreplay *rpl)
588 panic("kdebug_secreplay: NULL pointer was passed.");
590 kprintf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
591 rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
593 if (rpl->bitmap == NULL) {
598 kprintf("\n bitmap { ");
600 for (len = 0; len < rpl->wsize; len++) {
601 for (l = 7; l >= 0; l--)
602 kprintf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
610 kdebug_mbufhdr(struct mbuf *m)
616 kprintf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
617 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
618 m, m->m_next, m->m_nextpkt, m->m_data,
619 m->m_len, m->m_type, m->m_flags);
621 if (m->m_flags & M_PKTHDR) {
622 kprintf(" m_pkthdr{ len:%d rcvif:%p }\n",
623 m->m_pkthdr.len, m->m_pkthdr.rcvif);
626 if (m->m_flags & M_EXT) {
627 kprintf(" m_ext{ ext_buf:%p ext_free:%p "
628 "ext_size:%u ext_ref:%p }\n",
629 m->m_ext.ext_buf, m->m_ext.ext_free,
630 m->m_ext.ext_size, m->m_ext.ext_ref);
637 kdebug_mbuf(struct mbuf *m0)
642 for (j = 0; m; m = m->m_next) {
644 kprintf(" m_data:\n");
645 for (i = 0; i < m->m_len; i++) {
646 if (i && i % 32 == 0)
650 kprintf("%02x", mtod(m, u_char *)[i]);
661 kdebug_sockaddr(struct sockaddr *addr)
663 struct sockaddr_in *sin4;
665 struct sockaddr_in6 *sin6;
670 panic("kdebug_sockaddr: NULL pointer was passed.");
672 /* NOTE: We deal with port number as host byte order. */
673 kprintf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
675 switch (addr->sa_family) {
677 sin4 = (struct sockaddr_in *)addr;
678 kprintf(" port=%u\n", ntohs(sin4->sin_port));
679 ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr));
683 sin6 = (struct sockaddr_in6 *)addr;
684 kprintf(" port=%u\n", ntohs(sin6->sin6_port));
685 kprintf(" flowinfo=0x%08x, scope_id=0x%08x\n",
686 sin6->sin6_flowinfo, sin6->sin6_scope_id);
687 ipsec_hexdump((caddr_t)&sin6->sin6_addr,
688 sizeof(sin6->sin6_addr));
699 ipsec_bindump(caddr_t buf, int len)
703 for (i = 0; i < len; i++)
704 kprintf("%c", (unsigned char)buf[i]);
711 ipsec_hexdump(caddr_t buf, int len)
715 for (i = 0; i < len; i++) {
716 if (i != 0 && i % 32 == 0) kprintf("\n");
717 if (i % 4 == 0) kprintf(" ");
718 kprintf("%02x", (unsigned char)buf[i]);
721 if (i % 32 != 0) kprintf("\n");