1 //==========================================================================
3 // src/sys/netkey/key_debug.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
22 /* $KAME: key_debug.c,v 1.29 2001/08/16 14:25:41 itojun Exp $ */
25 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the project nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 #include <sys/types.h>
54 #include <sys/param.h>
57 #include <sys/queue.h>
59 #include <sys/socket.h>
61 #include <net/route.h>
63 #include <netkey/key_var.h>
64 #include <netkey/key_debug.h>
66 #include <netinet/in.h>
67 #include <netinet6/ipsec.h>
75 static void kdebug_sadb_prop __P((struct sadb_ext *));
76 static void kdebug_sadb_identity __P((struct sadb_ext *));
77 static void kdebug_sadb_supported __P((struct sadb_ext *));
78 static void kdebug_sadb_lifetime __P((struct sadb_ext *));
79 static void kdebug_sadb_sa __P((struct sadb_ext *));
80 static void kdebug_sadb_address __P((struct sadb_ext *));
81 static void kdebug_sadb_key __P((struct sadb_ext *));
82 static void kdebug_sadb_x_sa2 __P((struct sadb_ext *));
85 static void kdebug_secreplay __P((struct secreplay *));
89 #define panic(param) { printf(param); exit(1); }
92 /* NOTE: host byte order */
94 /* %%%: about struct sadb_msg */
97 struct sadb_msg *base;
104 panic("kdebug_sadb: NULL pointer was passed.\n");
106 printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
107 base->sadb_msg_version, base->sadb_msg_type,
108 base->sadb_msg_errno, base->sadb_msg_satype);
109 printf(" len=%u reserved=%u seq=%u pid=%u\n",
110 base->sadb_msg_len, base->sadb_msg_reserved,
111 base->sadb_msg_seq, base->sadb_msg_pid);
113 tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
114 ext = (struct sadb_ext *)((caddr_t)base + sizeof(struct sadb_msg));
117 printf("sadb_ext{ len=%u type=%u }\n",
118 ext->sadb_ext_len, ext->sadb_ext_type);
120 if (ext->sadb_ext_len == 0) {
121 printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
124 if (ext->sadb_ext_len > tlen) {
125 printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
129 switch (ext->sadb_ext_type) {
133 case SADB_EXT_LIFETIME_CURRENT:
134 case SADB_EXT_LIFETIME_HARD:
135 case SADB_EXT_LIFETIME_SOFT:
136 kdebug_sadb_lifetime(ext);
138 case SADB_EXT_ADDRESS_SRC:
139 case SADB_EXT_ADDRESS_DST:
140 case SADB_EXT_ADDRESS_PROXY:
141 kdebug_sadb_address(ext);
143 case SADB_EXT_KEY_AUTH:
144 case SADB_EXT_KEY_ENCRYPT:
145 kdebug_sadb_key(ext);
147 case SADB_EXT_IDENTITY_SRC:
148 case SADB_EXT_IDENTITY_DST:
149 kdebug_sadb_identity(ext);
151 case SADB_EXT_SENSITIVITY:
153 case SADB_EXT_PROPOSAL:
154 kdebug_sadb_prop(ext);
156 case SADB_EXT_SUPPORTED_AUTH:
157 case SADB_EXT_SUPPORTED_ENCRYPT:
158 kdebug_sadb_supported(ext);
160 case SADB_EXT_SPIRANGE:
161 case SADB_X_EXT_KMPRIVATE:
163 case SADB_X_EXT_POLICY:
164 kdebug_sadb_x_policy(ext);
167 kdebug_sadb_x_sa2(ext);
170 printf("kdebug_sadb: invalid ext_type %u was passed.\n",
175 extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
177 ext = (struct sadb_ext *)((caddr_t)ext + extlen);
184 kdebug_sadb_prop(ext)
185 struct sadb_ext *ext;
187 struct sadb_prop *prop = (struct sadb_prop *)ext;
188 struct sadb_comb *comb;
193 panic("kdebug_sadb_prop: NULL pointer was passed.\n");
195 len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
197 comb = (struct sadb_comb *)(prop + 1);
198 printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
201 printf("sadb_comb{ auth=%u encrypt=%u "
202 "flags=0x%04x reserved=0x%08x\n",
203 comb->sadb_comb_auth, comb->sadb_comb_encrypt,
204 comb->sadb_comb_flags, comb->sadb_comb_reserved);
206 printf(" auth_minbits=%u auth_maxbits=%u "
207 "encrypt_minbits=%u encrypt_maxbits=%u\n",
208 comb->sadb_comb_auth_minbits,
209 comb->sadb_comb_auth_maxbits,
210 comb->sadb_comb_encrypt_minbits,
211 comb->sadb_comb_encrypt_maxbits);
213 printf(" soft_alloc=%u hard_alloc=%u "
214 "soft_bytes=%lu hard_bytes=%lu\n",
215 comb->sadb_comb_soft_allocations,
216 comb->sadb_comb_hard_allocations,
217 (unsigned long)comb->sadb_comb_soft_bytes,
218 (unsigned long)comb->sadb_comb_hard_bytes);
220 printf(" soft_alloc=%lu hard_alloc=%lu "
221 "soft_bytes=%lu hard_bytes=%lu }\n",
222 (unsigned long)comb->sadb_comb_soft_addtime,
223 (unsigned long)comb->sadb_comb_hard_addtime,
224 (unsigned long)comb->sadb_comb_soft_usetime,
225 (unsigned long)comb->sadb_comb_hard_usetime);
234 kdebug_sadb_identity(ext)
235 struct sadb_ext *ext;
237 struct sadb_ident *id = (struct sadb_ident *)ext;
242 panic("kdebug_sadb_identity: NULL pointer was passed.\n");
244 len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
245 printf("sadb_ident_%s{",
246 id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
247 switch (id->sadb_ident_type) {
249 printf(" type=%d id=%lu",
250 id->sadb_ident_type, (u_long)id->sadb_ident_id);
253 ipsec_hexdump((caddr_t)(id + 1), len); /*XXX cast ?*/
257 p = (char *)(id + 1);
259 for (/*nothing*/; *p && p < ep; p++) {
261 printf("%c", *p & 0xff);
263 printf("\\%03o", *p & 0xff);
277 kdebug_sadb_supported(ext)
278 struct sadb_ext *ext;
280 struct sadb_supported *sup = (struct sadb_supported *)ext;
281 struct sadb_alg *alg;
286 panic("kdebug_sadb_supported: NULL pointer was passed.\n");
288 len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
290 alg = (struct sadb_alg *)(sup + 1);
291 printf("sadb_sup{\n");
293 printf(" { id=%d ivlen=%d min=%d max=%d }\n",
294 alg->sadb_alg_id, alg->sadb_alg_ivlen,
295 alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
304 kdebug_sadb_lifetime(ext)
305 struct sadb_ext *ext;
307 struct sadb_lifetime *lft = (struct sadb_lifetime *)ext;
311 printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
313 printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
314 lft->sadb_lifetime_allocations,
315 (u_int32_t)lft->sadb_lifetime_bytes);
316 printf(" addtime=%u, usetime=%u }\n",
317 (u_int32_t)lft->sadb_lifetime_addtime,
318 (u_int32_t)lft->sadb_lifetime_usetime);
325 struct sadb_ext *ext;
327 struct sadb_sa *sa = (struct sadb_sa *)ext;
331 panic("kdebug_sadb_sa: NULL pointer was passed.\n");
333 printf("sadb_sa{ spi=%u replay=%u state=%u\n",
334 (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
336 printf(" auth=%u encrypt=%u flags=0x%08x }\n",
337 sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
343 kdebug_sadb_address(ext)
344 struct sadb_ext *ext;
346 struct sadb_address *addr = (struct sadb_address *)ext;
350 panic("kdebug_sadb_address: NULL pointer was passed.\n");
352 printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
353 addr->sadb_address_proto, addr->sadb_address_prefixlen,
354 ((u_char *)&addr->sadb_address_reserved)[0],
355 ((u_char *)&addr->sadb_address_reserved)[1]);
357 kdebug_sockaddr((struct sockaddr *)((caddr_t)ext + sizeof(*addr)));
364 struct sadb_ext *ext;
366 struct sadb_key *key = (struct sadb_key *)ext;
370 panic("kdebug_sadb_key: NULL pointer was passed.\n");
372 printf("sadb_key{ bits=%u reserved=%u\n",
373 key->sadb_key_bits, key->sadb_key_reserved);
377 if ((key->sadb_key_bits >> 3) >
378 (PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
379 printf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
380 key->sadb_key_bits >> 3,
381 (long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
384 ipsec_hexdump((caddr_t)key + sizeof(struct sadb_key),
385 key->sadb_key_bits >> 3);
391 kdebug_sadb_x_sa2(ext)
392 struct sadb_ext *ext;
394 struct sadb_x_sa2 *sa2 = (struct sadb_x_sa2 *)ext;
398 panic("kdebug_sadb_x_sa2: NULL pointer was passed.\n");
400 printf("sadb_x_sa2{ mode=%u reqid=%u\n",
401 sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
402 printf(" reserved1=%u reserved2=%u sequence=%u }\n",
403 sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
404 sa2->sadb_x_sa2_sequence);
410 kdebug_sadb_x_policy(ext)
411 struct sadb_ext *ext;
413 struct sadb_x_policy *xpl = (struct sadb_x_policy *)ext;
414 struct sockaddr *addr;
418 panic("kdebug_sadb_x_policy: NULL pointer was passed.\n");
420 printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
421 xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
422 xpl->sadb_x_policy_id);
424 if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
426 struct sadb_x_ipsecrequest *xisr;
428 tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
429 xisr = (struct sadb_x_ipsecrequest *)(xpl + 1);
432 printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
433 xisr->sadb_x_ipsecrequest_len,
434 xisr->sadb_x_ipsecrequest_proto,
435 xisr->sadb_x_ipsecrequest_mode,
436 xisr->sadb_x_ipsecrequest_level,
437 xisr->sadb_x_ipsecrequest_reqid);
439 if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
440 addr = (struct sockaddr *)(xisr + 1);
441 kdebug_sockaddr(addr);
442 addr = (struct sockaddr *)((caddr_t)addr
444 kdebug_sockaddr(addr);
449 /* prevent infinite loop */
450 if (xisr->sadb_x_ipsecrequest_len <= 0) {
451 printf("kdebug_sadb_x_policy: wrong policy struct.\n");
454 /* prevent overflow */
455 if (xisr->sadb_x_ipsecrequest_len > tlen) {
456 printf("invalid ipsec policy length\n");
460 tlen -= xisr->sadb_x_ipsecrequest_len;
462 xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xisr
463 + xisr->sadb_x_ipsecrequest_len);
467 panic("kdebug_sadb_x_policy: wrong policy struct.\n");
474 /* %%%: about SPD and SAD */
477 struct secpolicy *sp;
481 panic("kdebug_secpolicy: NULL pointer was passed.\n");
483 printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
484 sp->refcnt, sp->state, sp->policy);
486 kdebug_secpolicyindex(&sp->spidx);
488 switch (sp->policy) {
489 case IPSEC_POLICY_DISCARD:
490 printf(" type=discard }\n");
492 case IPSEC_POLICY_NONE:
493 printf(" type=none }\n");
495 case IPSEC_POLICY_IPSEC:
497 struct ipsecrequest *isr;
498 for (isr = sp->req; isr != NULL; isr = isr->next) {
500 printf(" level=%u\n", isr->level);
501 kdebug_secasindex(&isr->saidx);
503 if (isr->sav != NULL)
504 kdebug_secasv(isr->sav);
509 case IPSEC_POLICY_BYPASS:
510 printf(" type=bypass }\n");
512 case IPSEC_POLICY_ENTRUST:
513 printf(" type=entrust }\n");
516 printf("kdebug_secpolicy: Invalid policy found. %d\n",
525 kdebug_secpolicyindex(spidx)
526 struct secpolicyindex *spidx;
530 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
532 printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
533 spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
535 ipsec_hexdump((caddr_t)&spidx->src,
536 ((struct sockaddr *)&spidx->src)->sa_len);
538 ipsec_hexdump((caddr_t)&spidx->dst,
539 ((struct sockaddr *)&spidx->dst)->sa_len);
546 kdebug_secasindex(saidx)
547 struct secasindex *saidx;
551 panic("kdebug_secpolicyindex: NULL pointer was passed.\n");
553 printf("secasindex{ mode=%u proto=%u\n",
554 saidx->mode, saidx->proto);
556 ipsec_hexdump((caddr_t)&saidx->src,
557 ((struct sockaddr *)&saidx->src)->sa_len);
559 ipsec_hexdump((caddr_t)&saidx->dst,
560 ((struct sockaddr *)&saidx->dst)->sa_len);
568 struct secasvar *sav;
572 panic("kdebug_secasv: NULL pointer was passed.\n");
575 kdebug_secasindex(&sav->sah->saidx);
577 printf(" refcnt=%u state=%u auth=%u enc=%u\n",
578 sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
579 printf(" spi=%u flags=%u\n",
580 (u_int32_t)ntohl(sav->spi), sav->flags);
582 if (sav->key_auth != NULL)
583 kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
584 if (sav->key_enc != NULL)
585 kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
586 if (sav->iv != NULL) {
588 ipsec_hexdump(sav->iv, sav->ivlen ? sav->ivlen : 8);
592 if (sav->replay != NULL)
593 kdebug_secreplay(sav->replay);
594 if (sav->lft_c != NULL)
595 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
596 if (sav->lft_h != NULL)
597 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
598 if (sav->lft_s != NULL)
599 kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
602 /* XXX: misc[123] ? */
609 kdebug_secreplay(rpl)
610 struct secreplay *rpl;
616 panic("kdebug_secreplay: NULL pointer was passed.\n");
618 printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
619 rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
621 if (rpl->bitmap == NULL) {
626 printf("\n bitmap { ");
628 for (len = 0; len < rpl->wsize; len++) {
629 for (l = 7; l >= 0; l--)
630 printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
645 printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
646 "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
647 m, m->m_next, m->m_nextpkt, m->m_data,
648 m->m_len, m->m_type, m->m_flags);
650 if (m->m_flags & M_PKTHDR) {
651 printf(" m_pkthdr{ len:%d rcvif:%p }\n",
652 m->m_pkthdr.len, m->m_pkthdr.rcvif);
656 if (m->m_flags & M_EXT) {
657 printf(" m_ext{ ext_buf:%p ext_free:%p "
658 "ext_size:%u ext_ref:%p }\n",
659 m->m_ext.ext_buf, m->m_ext.ext_free,
660 m->m_ext.ext_size, m->m_ext.ext_ref);
674 for (j = 0; m; m = m->m_next) {
676 printf(" m_data:\n");
677 for (i = 0; i < m->m_len; i++) {
678 if (i && i % 32 == 0)
682 printf("%02x", mtod(m, u_char *)[i]);
693 kdebug_sockaddr(addr)
694 struct sockaddr *addr;
696 struct sockaddr_in *sin4;
698 struct sockaddr_in6 *sin6;
703 panic("kdebug_sockaddr: NULL pointer was passed.\n");
705 /* NOTE: We deal with port number as host byte order. */
706 printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
708 switch (addr->sa_family) {
710 sin4 = (struct sockaddr_in *)addr;
711 printf(" port=%u\n", ntohs(sin4->sin_port));
712 ipsec_hexdump((caddr_t)&sin4->sin_addr, sizeof(sin4->sin_addr));
716 sin6 = (struct sockaddr_in6 *)addr;
717 printf(" port=%u\n", ntohs(sin6->sin6_port));
718 printf(" flowinfo=0x%08x, scope_id=0x%08x\n",
719 sin6->sin6_flowinfo, sin6->sin6_scope_id);
720 ipsec_hexdump((caddr_t)&sin6->sin6_addr,
721 sizeof(sin6->sin6_addr));
732 ipsec_bindump(buf, len)
738 for (i = 0; i < len; i++)
739 printf("%c", (unsigned char)buf[i]);
746 ipsec_hexdump(buf, len)
752 for (i = 0; i < len; i++) {
753 if (i != 0 && i % 32 == 0) printf("\n");
754 if (i % 4 == 0) printf(" ");
755 printf("%02x", (unsigned char)buf[i]);
758 if (i % 32 != 0) printf("\n");