Home | History | Annotate | Line # | Download | only in netipsec
key_debug.c revision 1.16
      1  1.16     ozaki /*	$NetBSD: key_debug.c,v 1.16 2017/04/18 10:49:35 ozaki-r Exp $	*/
      2   1.1  jonathan /*	$FreeBSD: src/sys/netipsec/key_debug.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $	*/
      3   1.1  jonathan /*	$KAME: key_debug.c,v 1.26 2001/06/27 10:46:50 sakane Exp $	*/
      4   1.1  jonathan 
      5   1.1  jonathan /*
      6   1.1  jonathan  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7   1.1  jonathan  * All rights reserved.
      8   1.1  jonathan  *
      9   1.1  jonathan  * Redistribution and use in source and binary forms, with or without
     10   1.1  jonathan  * modification, are permitted provided that the following conditions
     11   1.1  jonathan  * are met:
     12   1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     13   1.1  jonathan  *    notice, this list of conditions and the following disclaimer.
     14   1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  jonathan  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  jonathan  *    documentation and/or other materials provided with the distribution.
     17   1.1  jonathan  * 3. Neither the name of the project nor the names of its contributors
     18   1.1  jonathan  *    may be used to endorse or promote products derived from this software
     19   1.1  jonathan  *    without specific prior written permission.
     20   1.1  jonathan  *
     21   1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22   1.1  jonathan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1  jonathan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1  jonathan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25   1.1  jonathan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26   1.1  jonathan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27   1.1  jonathan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28   1.1  jonathan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29   1.1  jonathan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1  jonathan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1  jonathan  * SUCH DAMAGE.
     32   1.1  jonathan  */
     33   1.1  jonathan 
     34   1.1  jonathan #ifdef _KERNEL
     35   1.1  jonathan #include <sys/cdefs.h>
     36  1.16     ozaki __KERNEL_RCSID(0, "$NetBSD: key_debug.c,v 1.16 2017/04/18 10:49:35 ozaki-r Exp $");
     37   1.1  jonathan #endif
     38   1.1  jonathan 
     39  1.14     ozaki #if defined(_KERNEL_OPT)
     40   1.1  jonathan #include "opt_inet.h"
     41  1.14     ozaki #endif
     42   1.1  jonathan 
     43   1.1  jonathan #include <sys/types.h>
     44   1.1  jonathan #include <sys/param.h>
     45   1.1  jonathan #ifdef _KERNEL
     46   1.1  jonathan #include <sys/systm.h>
     47   1.1  jonathan #include <sys/mbuf.h>
     48   1.1  jonathan #include <sys/queue.h>
     49   1.1  jonathan #endif
     50   1.1  jonathan #include <sys/socket.h>
     51   1.1  jonathan 
     52   1.1  jonathan #include <net/route.h>
     53   1.1  jonathan 
     54   1.1  jonathan #include <netipsec/key_var.h>
     55   1.1  jonathan #include <netipsec/key_debug.h>
     56   1.1  jonathan 
     57   1.1  jonathan #include <netinet/in.h>
     58   1.1  jonathan #include <netipsec/ipsec.h>
     59   1.1  jonathan 
     60   1.1  jonathan #ifndef _KERNEL
     61   1.1  jonathan #include <ctype.h>
     62   1.1  jonathan #include <stdio.h>
     63   1.1  jonathan #include <stdlib.h>
     64   1.1  jonathan #endif /* !_KERNEL */
     65   1.1  jonathan 
     66  1.10  drochner static void kdebug_sadb_prop (const struct sadb_ext *);
     67  1.10  drochner static void kdebug_sadb_identity (const struct sadb_ext *);
     68  1.10  drochner static void kdebug_sadb_supported (const struct sadb_ext *);
     69  1.10  drochner static void kdebug_sadb_lifetime (const struct sadb_ext *);
     70  1.10  drochner static void kdebug_sadb_sa (const struct sadb_ext *);
     71  1.10  drochner static void kdebug_sadb_address (const struct sadb_ext *);
     72  1.10  drochner static void kdebug_sadb_key (const struct sadb_ext *);
     73  1.10  drochner static void kdebug_sadb_x_sa2 (const struct sadb_ext *);
     74   1.1  jonathan 
     75   1.1  jonathan #ifdef _KERNEL
     76  1.10  drochner static void kdebug_secreplay (const struct secreplay *);
     77   1.1  jonathan #endif
     78   1.1  jonathan 
     79   1.1  jonathan #ifndef _KERNEL
     80   1.1  jonathan #define panic(param)	{ printf(param); exit(-1); }
     81   1.1  jonathan #endif
     82   1.1  jonathan 
     83   1.1  jonathan /* NOTE: host byte order */
     84   1.1  jonathan 
     85   1.1  jonathan /* %%%: about struct sadb_msg */
     86   1.1  jonathan void
     87  1.10  drochner kdebug_sadb(const struct sadb_msg *base)
     88   1.1  jonathan {
     89  1.10  drochner 	const struct sadb_ext *ext;
     90   1.1  jonathan 	int tlen, extlen;
     91   1.1  jonathan 
     92   1.1  jonathan 	/* sanity check */
     93   1.1  jonathan 	if (base == NULL)
     94   1.4  christos 		panic("kdebug_sadb: NULL pointer was passed");
     95   1.1  jonathan 
     96   1.1  jonathan 	printf("sadb_msg{ version=%u type=%u errno=%u satype=%u\n",
     97   1.1  jonathan 	    base->sadb_msg_version, base->sadb_msg_type,
     98   1.1  jonathan 	    base->sadb_msg_errno, base->sadb_msg_satype);
     99   1.1  jonathan 	printf("  len=%u reserved=%u seq=%u pid=%u\n",
    100   1.1  jonathan 	    base->sadb_msg_len, base->sadb_msg_reserved,
    101   1.1  jonathan 	    base->sadb_msg_seq, base->sadb_msg_pid);
    102   1.1  jonathan 
    103   1.1  jonathan 	tlen = PFKEY_UNUNIT64(base->sadb_msg_len) - sizeof(struct sadb_msg);
    104  1.10  drochner 	ext = (const struct sadb_ext *)((const char *)base + sizeof(struct sadb_msg));
    105   1.1  jonathan 
    106   1.1  jonathan 	while (tlen > 0) {
    107   1.1  jonathan 		printf("sadb_ext{ len=%u type=%u }\n",
    108   1.1  jonathan 		    ext->sadb_ext_len, ext->sadb_ext_type);
    109   1.1  jonathan 
    110   1.1  jonathan 		if (ext->sadb_ext_len == 0) {
    111   1.1  jonathan 			printf("kdebug_sadb: invalid ext_len=0 was passed.\n");
    112   1.1  jonathan 			return;
    113   1.1  jonathan 		}
    114   1.1  jonathan 		if (ext->sadb_ext_len > tlen) {
    115   1.1  jonathan 			printf("kdebug_sadb: ext_len exceeds end of buffer.\n");
    116   1.1  jonathan 			return;
    117   1.1  jonathan 		}
    118   1.1  jonathan 
    119   1.1  jonathan 		switch (ext->sadb_ext_type) {
    120   1.1  jonathan 		case SADB_EXT_SA:
    121   1.1  jonathan 			kdebug_sadb_sa(ext);
    122   1.1  jonathan 			break;
    123   1.1  jonathan 		case SADB_EXT_LIFETIME_CURRENT:
    124   1.1  jonathan 		case SADB_EXT_LIFETIME_HARD:
    125   1.1  jonathan 		case SADB_EXT_LIFETIME_SOFT:
    126   1.1  jonathan 			kdebug_sadb_lifetime(ext);
    127   1.1  jonathan 			break;
    128   1.1  jonathan 		case SADB_EXT_ADDRESS_SRC:
    129   1.1  jonathan 		case SADB_EXT_ADDRESS_DST:
    130   1.1  jonathan 		case SADB_EXT_ADDRESS_PROXY:
    131   1.1  jonathan 			kdebug_sadb_address(ext);
    132   1.1  jonathan 			break;
    133   1.1  jonathan 		case SADB_EXT_KEY_AUTH:
    134   1.1  jonathan 		case SADB_EXT_KEY_ENCRYPT:
    135   1.1  jonathan 			kdebug_sadb_key(ext);
    136   1.1  jonathan 			break;
    137   1.1  jonathan 		case SADB_EXT_IDENTITY_SRC:
    138   1.1  jonathan 		case SADB_EXT_IDENTITY_DST:
    139   1.1  jonathan 			kdebug_sadb_identity(ext);
    140   1.1  jonathan 			break;
    141   1.1  jonathan 		case SADB_EXT_SENSITIVITY:
    142   1.1  jonathan 			break;
    143   1.1  jonathan 		case SADB_EXT_PROPOSAL:
    144   1.1  jonathan 			kdebug_sadb_prop(ext);
    145   1.1  jonathan 			break;
    146   1.1  jonathan 		case SADB_EXT_SUPPORTED_AUTH:
    147   1.1  jonathan 		case SADB_EXT_SUPPORTED_ENCRYPT:
    148   1.1  jonathan 			kdebug_sadb_supported(ext);
    149   1.1  jonathan 			break;
    150   1.1  jonathan 		case SADB_EXT_SPIRANGE:
    151   1.1  jonathan 		case SADB_X_EXT_KMPRIVATE:
    152   1.1  jonathan 			break;
    153   1.1  jonathan 		case SADB_X_EXT_POLICY:
    154   1.1  jonathan 			kdebug_sadb_x_policy(ext);
    155   1.1  jonathan 			break;
    156   1.1  jonathan 		case SADB_X_EXT_SA2:
    157   1.1  jonathan 			kdebug_sadb_x_sa2(ext);
    158   1.1  jonathan 			break;
    159   1.1  jonathan 		default:
    160   1.1  jonathan 			printf("kdebug_sadb: invalid ext_type %u was passed.\n",
    161   1.1  jonathan 			    ext->sadb_ext_type);
    162   1.1  jonathan 			return;
    163   1.1  jonathan 		}
    164   1.1  jonathan 
    165   1.1  jonathan 		extlen = PFKEY_UNUNIT64(ext->sadb_ext_len);
    166   1.1  jonathan 		tlen -= extlen;
    167  1.10  drochner 		ext = (const struct sadb_ext *)((const char *)ext + extlen);
    168   1.1  jonathan 	}
    169   1.1  jonathan 
    170   1.1  jonathan 	return;
    171   1.1  jonathan }
    172   1.1  jonathan 
    173   1.1  jonathan static void
    174  1.10  drochner kdebug_sadb_prop(const struct sadb_ext *ext)
    175   1.1  jonathan {
    176  1.10  drochner 	const struct sadb_prop *prop = (const struct sadb_prop *)ext;
    177  1.10  drochner 	const struct sadb_comb *comb;
    178   1.1  jonathan 	int len;
    179   1.1  jonathan 
    180   1.1  jonathan 	/* sanity check */
    181   1.1  jonathan 	if (ext == NULL)
    182   1.4  christos 		panic("kdebug_sadb_prop: NULL pointer was passed");
    183   1.1  jonathan 
    184   1.1  jonathan 	len = (PFKEY_UNUNIT64(prop->sadb_prop_len) - sizeof(*prop))
    185   1.1  jonathan 		/ sizeof(*comb);
    186  1.10  drochner 	comb = (const struct sadb_comb *)(prop + 1);
    187   1.1  jonathan 	printf("sadb_prop{ replay=%u\n", prop->sadb_prop_replay);
    188   1.1  jonathan 
    189   1.1  jonathan 	while (len--) {
    190   1.1  jonathan 		printf("sadb_comb{ auth=%u encrypt=%u "
    191   1.1  jonathan 			"flags=0x%04x reserved=0x%08x\n",
    192   1.1  jonathan 			comb->sadb_comb_auth, comb->sadb_comb_encrypt,
    193   1.1  jonathan 			comb->sadb_comb_flags, comb->sadb_comb_reserved);
    194   1.1  jonathan 
    195   1.1  jonathan 		printf("  auth_minbits=%u auth_maxbits=%u "
    196   1.1  jonathan 			"encrypt_minbits=%u encrypt_maxbits=%u\n",
    197   1.1  jonathan 			comb->sadb_comb_auth_minbits,
    198   1.1  jonathan 			comb->sadb_comb_auth_maxbits,
    199   1.1  jonathan 			comb->sadb_comb_encrypt_minbits,
    200   1.1  jonathan 			comb->sadb_comb_encrypt_maxbits);
    201   1.1  jonathan 
    202   1.1  jonathan 		printf("  soft_alloc=%u hard_alloc=%u "
    203   1.1  jonathan 			"soft_bytes=%lu hard_bytes=%lu\n",
    204   1.1  jonathan 			comb->sadb_comb_soft_allocations,
    205   1.1  jonathan 			comb->sadb_comb_hard_allocations,
    206   1.1  jonathan 			(unsigned long)comb->sadb_comb_soft_bytes,
    207   1.1  jonathan 			(unsigned long)comb->sadb_comb_hard_bytes);
    208   1.1  jonathan 
    209   1.1  jonathan 		printf("  soft_alloc=%lu hard_alloc=%lu "
    210   1.1  jonathan 			"soft_bytes=%lu hard_bytes=%lu }\n",
    211   1.1  jonathan 			(unsigned long)comb->sadb_comb_soft_addtime,
    212   1.1  jonathan 			(unsigned long)comb->sadb_comb_hard_addtime,
    213   1.1  jonathan 			(unsigned long)comb->sadb_comb_soft_usetime,
    214   1.1  jonathan 			(unsigned long)comb->sadb_comb_hard_usetime);
    215   1.1  jonathan 		comb++;
    216   1.1  jonathan 	}
    217   1.1  jonathan 	printf("}\n");
    218   1.1  jonathan 
    219   1.1  jonathan 	return;
    220   1.1  jonathan }
    221   1.1  jonathan 
    222   1.1  jonathan static void
    223  1.10  drochner kdebug_sadb_identity(const struct sadb_ext *ext)
    224   1.1  jonathan {
    225  1.10  drochner 	const struct sadb_ident *id = (const struct sadb_ident *)ext;
    226   1.1  jonathan 	int len;
    227   1.1  jonathan 
    228   1.1  jonathan 	/* sanity check */
    229   1.1  jonathan 	if (ext == NULL)
    230   1.4  christos 		panic("kdebug_sadb_identity: NULL pointer was passed");
    231   1.1  jonathan 
    232   1.1  jonathan 	len = PFKEY_UNUNIT64(id->sadb_ident_len) - sizeof(*id);
    233   1.1  jonathan 	printf("sadb_ident_%s{",
    234   1.1  jonathan 	    id->sadb_ident_exttype == SADB_EXT_IDENTITY_SRC ? "src" : "dst");
    235   1.1  jonathan 	switch (id->sadb_ident_type) {
    236   1.1  jonathan 	default:
    237   1.1  jonathan 		printf(" type=%d id=%lu",
    238   1.1  jonathan 			id->sadb_ident_type, (u_long)id->sadb_ident_id);
    239   1.1  jonathan 		if (len) {
    240   1.1  jonathan #ifdef _KERNEL
    241  1.10  drochner 			ipsec_hexdump((const char *)(id + 1), len); /*XXX cast ?*/
    242   1.1  jonathan #else
    243  1.10  drochner 			const char *p, *ep;
    244   1.1  jonathan 			printf("\n  str=\"");
    245  1.10  drochner 			p = (const char *)(id + 1);
    246   1.1  jonathan 			ep = p + len;
    247   1.1  jonathan 			for (/*nothing*/; *p && p < ep; p++) {
    248   1.1  jonathan 				if (isprint(*p))
    249   1.1  jonathan 					printf("%c", *p & 0xff);
    250   1.1  jonathan 				else
    251   1.1  jonathan 					printf("\\%03o", *p & 0xff);
    252   1.1  jonathan 			}
    253   1.1  jonathan #endif
    254   1.1  jonathan 			printf("\"");
    255   1.1  jonathan 		}
    256   1.1  jonathan 		break;
    257   1.1  jonathan 	}
    258   1.1  jonathan 
    259   1.1  jonathan 	printf(" }\n");
    260   1.1  jonathan 
    261   1.1  jonathan 	return;
    262   1.1  jonathan }
    263   1.1  jonathan 
    264   1.1  jonathan static void
    265  1.10  drochner kdebug_sadb_supported(const struct sadb_ext *ext)
    266   1.1  jonathan {
    267  1.10  drochner 	const struct sadb_supported *sup = (const struct sadb_supported *)ext;
    268  1.10  drochner 	const struct sadb_alg *alg;
    269   1.1  jonathan 	int len;
    270   1.1  jonathan 
    271   1.1  jonathan 	/* sanity check */
    272   1.1  jonathan 	if (ext == NULL)
    273   1.4  christos 		panic("kdebug_sadb_supported: NULL pointer was passed");
    274   1.1  jonathan 
    275   1.1  jonathan 	len = (PFKEY_UNUNIT64(sup->sadb_supported_len) - sizeof(*sup))
    276   1.1  jonathan 		/ sizeof(*alg);
    277  1.10  drochner 	alg = (const struct sadb_alg *)(sup + 1);
    278   1.1  jonathan 	printf("sadb_sup{\n");
    279   1.1  jonathan 	while (len--) {
    280   1.1  jonathan 		printf("  { id=%d ivlen=%d min=%d max=%d }\n",
    281   1.1  jonathan 			alg->sadb_alg_id, alg->sadb_alg_ivlen,
    282   1.1  jonathan 			alg->sadb_alg_minbits, alg->sadb_alg_maxbits);
    283   1.1  jonathan 		alg++;
    284   1.1  jonathan 	}
    285   1.1  jonathan 	printf("}\n");
    286   1.1  jonathan 
    287   1.1  jonathan 	return;
    288   1.1  jonathan }
    289   1.1  jonathan 
    290   1.1  jonathan static void
    291  1.10  drochner kdebug_sadb_lifetime(const struct sadb_ext *ext)
    292   1.1  jonathan {
    293  1.10  drochner 	const struct sadb_lifetime *lft = (const struct sadb_lifetime *)ext;
    294   1.1  jonathan 
    295   1.1  jonathan 	/* sanity check */
    296   1.1  jonathan 	if (ext == NULL)
    297   1.1  jonathan 		printf("kdebug_sadb_lifetime: NULL pointer was passed.\n");
    298   1.1  jonathan 
    299   1.1  jonathan 	printf("sadb_lifetime{ alloc=%u, bytes=%u\n",
    300   1.1  jonathan 		lft->sadb_lifetime_allocations,
    301   1.1  jonathan 		(u_int32_t)lft->sadb_lifetime_bytes);
    302   1.1  jonathan 	printf("  addtime=%u, usetime=%u }\n",
    303   1.1  jonathan 		(u_int32_t)lft->sadb_lifetime_addtime,
    304   1.1  jonathan 		(u_int32_t)lft->sadb_lifetime_usetime);
    305   1.1  jonathan 
    306   1.1  jonathan 	return;
    307   1.1  jonathan }
    308   1.1  jonathan 
    309   1.1  jonathan static void
    310  1.10  drochner kdebug_sadb_sa(const struct sadb_ext *ext)
    311   1.1  jonathan {
    312  1.10  drochner 	const struct sadb_sa *sa = (const struct sadb_sa *)ext;
    313   1.1  jonathan 
    314   1.1  jonathan 	/* sanity check */
    315   1.1  jonathan 	if (ext == NULL)
    316   1.4  christos 		panic("kdebug_sadb_sa: NULL pointer was passed");
    317   1.1  jonathan 
    318   1.1  jonathan 	printf("sadb_sa{ spi=%u replay=%u state=%u\n",
    319   1.1  jonathan 	    (u_int32_t)ntohl(sa->sadb_sa_spi), sa->sadb_sa_replay,
    320   1.1  jonathan 	    sa->sadb_sa_state);
    321   1.1  jonathan 	printf("  auth=%u encrypt=%u flags=0x%08x }\n",
    322   1.1  jonathan 	    sa->sadb_sa_auth, sa->sadb_sa_encrypt, sa->sadb_sa_flags);
    323   1.1  jonathan 
    324   1.1  jonathan 	return;
    325   1.1  jonathan }
    326   1.1  jonathan 
    327   1.1  jonathan static void
    328  1.10  drochner kdebug_sadb_address(const struct sadb_ext *ext)
    329   1.1  jonathan {
    330  1.10  drochner 	const struct sadb_address *addr = (const struct sadb_address *)ext;
    331   1.1  jonathan 
    332   1.1  jonathan 	/* sanity check */
    333   1.1  jonathan 	if (ext == NULL)
    334   1.4  christos 		panic("kdebug_sadb_address: NULL pointer was passed");
    335   1.1  jonathan 
    336   1.1  jonathan 	printf("sadb_address{ proto=%u prefixlen=%u reserved=0x%02x%02x }\n",
    337   1.1  jonathan 	    addr->sadb_address_proto, addr->sadb_address_prefixlen,
    338  1.10  drochner 	    ((const u_char *)&addr->sadb_address_reserved)[0],
    339  1.10  drochner 	    ((const u_char *)&addr->sadb_address_reserved)[1]);
    340   1.1  jonathan 
    341  1.10  drochner 	kdebug_sockaddr((const struct sockaddr *)((const char *)ext + sizeof(*addr)));
    342   1.1  jonathan 
    343   1.1  jonathan 	return;
    344   1.1  jonathan }
    345   1.1  jonathan 
    346   1.1  jonathan static void
    347  1.10  drochner kdebug_sadb_key(const struct sadb_ext *ext)
    348   1.1  jonathan {
    349  1.10  drochner 	const struct sadb_key *key = (const struct sadb_key *)ext;
    350   1.1  jonathan 
    351   1.1  jonathan 	/* sanity check */
    352   1.1  jonathan 	if (ext == NULL)
    353   1.4  christos 		panic("kdebug_sadb_key: NULL pointer was passed");
    354   1.1  jonathan 
    355   1.1  jonathan 	printf("sadb_key{ bits=%u reserved=%u\n",
    356   1.1  jonathan 	    key->sadb_key_bits, key->sadb_key_reserved);
    357   1.1  jonathan 	printf("  key=");
    358   1.1  jonathan 
    359   1.1  jonathan 	/* sanity check 2 */
    360   1.1  jonathan 	if ((key->sadb_key_bits >> 3) >
    361   1.1  jonathan 		(PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key))) {
    362   1.1  jonathan 		printf("kdebug_sadb_key: key length mismatch, bit:%d len:%ld.\n",
    363   1.1  jonathan 			key->sadb_key_bits >> 3,
    364   1.1  jonathan 			(long)PFKEY_UNUNIT64(key->sadb_key_len) - sizeof(struct sadb_key));
    365   1.1  jonathan 	}
    366   1.1  jonathan 
    367  1.10  drochner 	ipsec_hexdump((const char *)key + sizeof(struct sadb_key),
    368   1.1  jonathan 	              key->sadb_key_bits >> 3);
    369   1.1  jonathan 	printf(" }\n");
    370   1.1  jonathan 	return;
    371   1.1  jonathan }
    372   1.1  jonathan 
    373   1.1  jonathan static void
    374  1.10  drochner kdebug_sadb_x_sa2(const struct sadb_ext *ext)
    375   1.1  jonathan {
    376  1.10  drochner 	const struct sadb_x_sa2 *sa2 = (const struct sadb_x_sa2 *)ext;
    377   1.1  jonathan 
    378   1.1  jonathan 	/* sanity check */
    379   1.1  jonathan 	if (ext == NULL)
    380   1.4  christos 		panic("kdebug_sadb_x_sa2: NULL pointer was passed");
    381   1.1  jonathan 
    382   1.1  jonathan 	printf("sadb_x_sa2{ mode=%u reqid=%u\n",
    383   1.1  jonathan 	    sa2->sadb_x_sa2_mode, sa2->sadb_x_sa2_reqid);
    384   1.1  jonathan 	printf("  reserved1=%u reserved2=%u sequence=%u }\n",
    385   1.1  jonathan 	    sa2->sadb_x_sa2_reserved1, sa2->sadb_x_sa2_reserved2,
    386   1.1  jonathan 	    sa2->sadb_x_sa2_sequence);
    387   1.1  jonathan 
    388   1.1  jonathan 	return;
    389   1.1  jonathan }
    390   1.1  jonathan 
    391   1.1  jonathan void
    392  1.10  drochner kdebug_sadb_x_policy(const struct sadb_ext *ext)
    393   1.1  jonathan {
    394  1.10  drochner 	const struct sadb_x_policy *xpl = (const struct sadb_x_policy *)ext;
    395  1.10  drochner 	const struct sockaddr *addr;
    396   1.1  jonathan 
    397   1.1  jonathan 	/* sanity check */
    398   1.1  jonathan 	if (ext == NULL)
    399   1.4  christos 		panic("kdebug_sadb_x_policy: NULL pointer was passed");
    400   1.1  jonathan 
    401   1.1  jonathan 	printf("sadb_x_policy{ type=%u dir=%u id=%x }\n",
    402   1.1  jonathan 		xpl->sadb_x_policy_type, xpl->sadb_x_policy_dir,
    403   1.1  jonathan 		xpl->sadb_x_policy_id);
    404   1.1  jonathan 
    405   1.1  jonathan 	if (xpl->sadb_x_policy_type == IPSEC_POLICY_IPSEC) {
    406   1.1  jonathan 		int tlen;
    407  1.10  drochner 		const struct sadb_x_ipsecrequest *xisr;
    408   1.1  jonathan 
    409   1.1  jonathan 		tlen = PFKEY_UNUNIT64(xpl->sadb_x_policy_len) - sizeof(*xpl);
    410  1.10  drochner 		xisr = (const struct sadb_x_ipsecrequest *)(xpl + 1);
    411   1.1  jonathan 
    412   1.1  jonathan 		while (tlen > 0) {
    413   1.1  jonathan 			printf(" { len=%u proto=%u mode=%u level=%u reqid=%u\n",
    414   1.1  jonathan 				xisr->sadb_x_ipsecrequest_len,
    415   1.1  jonathan 				xisr->sadb_x_ipsecrequest_proto,
    416   1.1  jonathan 				xisr->sadb_x_ipsecrequest_mode,
    417   1.1  jonathan 				xisr->sadb_x_ipsecrequest_level,
    418   1.1  jonathan 				xisr->sadb_x_ipsecrequest_reqid);
    419   1.1  jonathan 
    420   1.1  jonathan 			if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
    421  1.10  drochner 				addr = (const struct sockaddr *)(xisr + 1);
    422   1.1  jonathan 				kdebug_sockaddr(addr);
    423  1.10  drochner 				addr = (const struct sockaddr *)((const char *)addr
    424   1.1  jonathan 							+ addr->sa_len);
    425   1.1  jonathan 				kdebug_sockaddr(addr);
    426   1.1  jonathan 			}
    427   1.1  jonathan 
    428   1.1  jonathan 			printf(" }\n");
    429   1.1  jonathan 
    430   1.1  jonathan 			/* prevent infinite loop */
    431   1.1  jonathan 			if (xisr->sadb_x_ipsecrequest_len <= 0) {
    432   1.1  jonathan 				printf("kdebug_sadb_x_policy: wrong policy struct.\n");
    433   1.1  jonathan 				return;
    434   1.1  jonathan 			}
    435   1.1  jonathan 			/* prevent overflow */
    436   1.1  jonathan 			if (xisr->sadb_x_ipsecrequest_len > tlen) {
    437   1.1  jonathan 				printf("invalid ipsec policy length\n");
    438   1.1  jonathan 				return;
    439   1.1  jonathan 			}
    440   1.1  jonathan 
    441   1.1  jonathan 			tlen -= xisr->sadb_x_ipsecrequest_len;
    442   1.1  jonathan 
    443  1.10  drochner 			xisr = (const struct sadb_x_ipsecrequest *)((const char *)xisr
    444   1.1  jonathan 			                + xisr->sadb_x_ipsecrequest_len);
    445   1.1  jonathan 		}
    446   1.1  jonathan 
    447   1.1  jonathan 		if (tlen != 0)
    448   1.4  christos 			panic("kdebug_sadb_x_policy: wrong policy struct");
    449   1.1  jonathan 	}
    450   1.1  jonathan 
    451   1.1  jonathan 	return;
    452   1.1  jonathan }
    453   1.1  jonathan 
    454   1.1  jonathan #ifdef _KERNEL
    455   1.1  jonathan /* %%%: about SPD and SAD */
    456   1.1  jonathan void
    457  1.10  drochner kdebug_secpolicy(const struct secpolicy *sp)
    458   1.1  jonathan {
    459   1.1  jonathan 	/* sanity check */
    460   1.1  jonathan 	if (sp == NULL)
    461   1.4  christos 		panic("kdebug_secpolicy: NULL pointer was passed");
    462   1.1  jonathan 
    463   1.1  jonathan 	printf("secpolicy{ refcnt=%u state=%u policy=%u\n",
    464   1.1  jonathan 		sp->refcnt, sp->state, sp->policy);
    465   1.1  jonathan 
    466   1.1  jonathan 	kdebug_secpolicyindex(&sp->spidx);
    467   1.1  jonathan 
    468   1.1  jonathan 	switch (sp->policy) {
    469   1.1  jonathan 	case IPSEC_POLICY_DISCARD:
    470   1.1  jonathan 		printf("  type=discard }\n");
    471   1.1  jonathan 		break;
    472   1.1  jonathan 	case IPSEC_POLICY_NONE:
    473   1.1  jonathan 		printf("  type=none }\n");
    474   1.1  jonathan 		break;
    475   1.1  jonathan 	case IPSEC_POLICY_IPSEC:
    476   1.1  jonathan 	    {
    477   1.1  jonathan 		struct ipsecrequest *isr;
    478   1.1  jonathan 		for (isr = sp->req; isr != NULL; isr = isr->next) {
    479   1.1  jonathan 
    480   1.1  jonathan 			printf("  level=%u\n", isr->level);
    481   1.1  jonathan 			kdebug_secasindex(&isr->saidx);
    482   1.1  jonathan 
    483   1.1  jonathan 			if (isr->sav != NULL)
    484   1.1  jonathan 				kdebug_secasv(isr->sav);
    485   1.1  jonathan 		}
    486   1.1  jonathan 		printf("  }\n");
    487   1.1  jonathan 	    }
    488   1.1  jonathan 		break;
    489   1.1  jonathan 	case IPSEC_POLICY_BYPASS:
    490   1.1  jonathan 		printf("  type=bypass }\n");
    491   1.1  jonathan 		break;
    492   1.1  jonathan 	case IPSEC_POLICY_ENTRUST:
    493   1.1  jonathan 		printf("  type=entrust }\n");
    494   1.1  jonathan 		break;
    495   1.1  jonathan 	default:
    496   1.1  jonathan 		printf("kdebug_secpolicy: Invalid policy found. %d\n",
    497   1.1  jonathan 			sp->policy);
    498   1.1  jonathan 		break;
    499   1.1  jonathan 	}
    500   1.1  jonathan 
    501   1.1  jonathan 	return;
    502   1.1  jonathan }
    503   1.1  jonathan 
    504   1.1  jonathan void
    505  1.10  drochner kdebug_secpolicyindex(const struct secpolicyindex *spidx)
    506   1.1  jonathan {
    507   1.1  jonathan 	/* sanity check */
    508   1.1  jonathan 	if (spidx == NULL)
    509   1.4  christos 		panic("kdebug_secpolicyindex: NULL pointer was passed");
    510   1.1  jonathan 
    511   1.1  jonathan 	printf("secpolicyindex{ dir=%u prefs=%u prefd=%u ul_proto=%u\n",
    512   1.1  jonathan 		spidx->dir, spidx->prefs, spidx->prefd, spidx->ul_proto);
    513   1.1  jonathan 
    514  1.10  drochner 	ipsec_hexdump((const char *)&spidx->src,
    515  1.10  drochner 		spidx->src.sa.sa_len);
    516   1.1  jonathan 	printf("\n");
    517  1.10  drochner 	ipsec_hexdump((const char *)&spidx->dst,
    518  1.10  drochner 		spidx->dst.sa.sa_len);
    519   1.1  jonathan 	printf("}\n");
    520   1.1  jonathan 
    521   1.1  jonathan 	return;
    522   1.1  jonathan }
    523   1.1  jonathan 
    524   1.1  jonathan void
    525  1.10  drochner kdebug_secasindex(const struct secasindex *saidx)
    526   1.1  jonathan {
    527   1.1  jonathan 	/* sanity check */
    528   1.1  jonathan 	if (saidx == NULL)
    529   1.4  christos 		panic("kdebug_secpolicyindex: NULL pointer was passed");
    530   1.1  jonathan 
    531   1.1  jonathan 	printf("secasindex{ mode=%u proto=%u\n",
    532   1.1  jonathan 		saidx->mode, saidx->proto);
    533   1.1  jonathan 
    534  1.10  drochner 	ipsec_hexdump((const char *)&saidx->src,
    535  1.10  drochner 		saidx->src.sa.sa_len);
    536   1.1  jonathan 	printf("\n");
    537  1.10  drochner 	ipsec_hexdump((const char *)&saidx->dst,
    538  1.10  drochner 		saidx->dst.sa.sa_len);
    539   1.1  jonathan 	printf("\n");
    540   1.1  jonathan 
    541   1.1  jonathan 	return;
    542   1.1  jonathan }
    543   1.1  jonathan 
    544   1.1  jonathan void
    545  1.10  drochner kdebug_secasv(const struct secasvar *sav)
    546   1.1  jonathan {
    547   1.1  jonathan 	/* sanity check */
    548   1.1  jonathan 	if (sav == NULL)
    549   1.4  christos 		panic("kdebug_secasv: NULL pointer was passed");
    550   1.1  jonathan 
    551   1.1  jonathan 	printf("secas{");
    552   1.1  jonathan 	kdebug_secasindex(&sav->sah->saidx);
    553   1.1  jonathan 
    554   1.1  jonathan 	printf("  refcnt=%u state=%u auth=%u enc=%u\n",
    555   1.1  jonathan 	    sav->refcnt, sav->state, sav->alg_auth, sav->alg_enc);
    556   1.1  jonathan 	printf("  spi=%u flags=%u\n",
    557   1.1  jonathan 	    (u_int32_t)ntohl(sav->spi), sav->flags);
    558   1.1  jonathan 
    559   1.1  jonathan 	if (sav->key_auth != NULL)
    560   1.1  jonathan 		kdebug_sadb_key((struct sadb_ext *)sav->key_auth);
    561   1.1  jonathan 	if (sav->key_enc != NULL)
    562   1.1  jonathan 		kdebug_sadb_key((struct sadb_ext *)sav->key_enc);
    563   1.1  jonathan 
    564   1.1  jonathan 	if (sav->replay != NULL)
    565   1.1  jonathan 		kdebug_secreplay(sav->replay);
    566   1.1  jonathan 	if (sav->lft_c != NULL)
    567   1.1  jonathan 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_c);
    568   1.1  jonathan 	if (sav->lft_h != NULL)
    569   1.1  jonathan 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_h);
    570   1.1  jonathan 	if (sav->lft_s != NULL)
    571   1.1  jonathan 		kdebug_sadb_lifetime((struct sadb_ext *)sav->lft_s);
    572   1.1  jonathan 
    573   1.1  jonathan #if notyet
    574   1.1  jonathan 	/* XXX: misc[123] ? */
    575   1.1  jonathan #endif
    576   1.1  jonathan 
    577   1.1  jonathan 	return;
    578   1.1  jonathan }
    579   1.1  jonathan 
    580   1.1  jonathan static void
    581  1.10  drochner kdebug_secreplay(const struct secreplay *rpl)
    582   1.1  jonathan {
    583   1.1  jonathan 	int len, l;
    584   1.1  jonathan 
    585   1.1  jonathan 	/* sanity check */
    586   1.1  jonathan 	if (rpl == NULL)
    587   1.4  christos 		panic("kdebug_secreplay: NULL pointer was passed");
    588   1.1  jonathan 
    589   1.1  jonathan 	printf(" secreplay{ count=%u wsize=%u seq=%u lastseq=%u",
    590   1.1  jonathan 	    rpl->count, rpl->wsize, rpl->seq, rpl->lastseq);
    591   1.1  jonathan 
    592   1.1  jonathan 	if (rpl->bitmap == NULL) {
    593   1.1  jonathan 		printf(" }\n");
    594   1.1  jonathan 		return;
    595   1.1  jonathan 	}
    596   1.1  jonathan 
    597   1.1  jonathan 	printf("\n   bitmap { ");
    598   1.1  jonathan 
    599   1.1  jonathan 	for (len = 0; len < rpl->wsize; len++) {
    600   1.1  jonathan 		for (l = 7; l >= 0; l--)
    601   1.1  jonathan 			printf("%u", (((rpl->bitmap)[len] >> l) & 1) ? 1 : 0);
    602   1.1  jonathan 	}
    603   1.1  jonathan 	printf(" }\n");
    604   1.1  jonathan 
    605   1.1  jonathan 	return;
    606   1.1  jonathan }
    607   1.1  jonathan 
    608   1.1  jonathan void
    609  1.10  drochner kdebug_mbufhdr(const struct mbuf *m)
    610   1.1  jonathan {
    611   1.1  jonathan 	/* sanity check */
    612   1.1  jonathan 	if (m == NULL)
    613   1.1  jonathan 		return;
    614   1.1  jonathan 
    615   1.1  jonathan 	printf("mbuf(%p){ m_next:%p m_nextpkt:%p m_data:%p "
    616   1.1  jonathan 	       "m_len:%d m_type:0x%02x m_flags:0x%02x }\n",
    617   1.1  jonathan 		m, m->m_next, m->m_nextpkt, m->m_data,
    618   1.1  jonathan 		m->m_len, m->m_type, m->m_flags);
    619   1.1  jonathan 
    620   1.1  jonathan 	if (m->m_flags & M_PKTHDR) {
    621   1.1  jonathan 		printf("  m_pkthdr{ len:%d rcvif:%p }\n",
    622  1.13     ozaki 		    m->m_pkthdr.len, m_get_rcvif_NOMPSAFE(m));
    623   1.1  jonathan 	}
    624   1.1  jonathan 
    625   1.1  jonathan 	if (m->m_flags & M_EXT) {
    626   1.1  jonathan 		printf("  m_ext{ ext_buf:%p ext_free:%p "
    627  1.16     ozaki 		       "ext_size:%zu ext_refcnt:%u }\n",
    628   1.1  jonathan 			m->m_ext.ext_buf, m->m_ext.ext_free,
    629  1.15     ozaki 			m->m_ext.ext_size, m->m_ext.ext_refcnt);
    630   1.1  jonathan 	}
    631   1.1  jonathan 
    632   1.1  jonathan 	return;
    633   1.1  jonathan }
    634   1.1  jonathan 
    635   1.1  jonathan void
    636  1.10  drochner kdebug_mbuf(const struct mbuf *m0)
    637   1.1  jonathan {
    638  1.10  drochner 	const struct mbuf *m = m0;
    639   1.1  jonathan 	int i, j;
    640   1.1  jonathan 
    641   1.1  jonathan 	for (j = 0; m; m = m->m_next) {
    642   1.1  jonathan 		kdebug_mbufhdr(m);
    643   1.1  jonathan 		printf("  m_data:\n");
    644   1.1  jonathan 		for (i = 0; i < m->m_len; i++) {
    645   1.1  jonathan 			if (i && i % 32 == 0)
    646   1.1  jonathan 				printf("\n");
    647   1.1  jonathan 			if (i % 4 == 0)
    648   1.1  jonathan 				printf(" ");
    649   1.1  jonathan 			printf("%02x", mtod(m, u_char *)[i]);
    650   1.1  jonathan 			j++;
    651   1.1  jonathan 		}
    652   1.1  jonathan 		printf("\n");
    653   1.1  jonathan 	}
    654   1.1  jonathan 
    655   1.1  jonathan 	return;
    656   1.1  jonathan }
    657   1.1  jonathan #endif /* _KERNEL */
    658   1.1  jonathan 
    659   1.1  jonathan void
    660  1.10  drochner kdebug_sockaddr(const struct sockaddr *addr)
    661   1.1  jonathan {
    662  1.10  drochner 	const struct sockaddr_in *sin4;
    663   1.1  jonathan #ifdef INET6
    664  1.10  drochner 	const struct sockaddr_in6 *sin6;
    665   1.1  jonathan #endif
    666   1.1  jonathan 
    667   1.1  jonathan 	/* sanity check */
    668   1.1  jonathan 	if (addr == NULL)
    669   1.4  christos 		panic("kdebug_sockaddr: NULL pointer was passed");
    670   1.1  jonathan 
    671   1.1  jonathan 	/* NOTE: We deal with port number as host byte order. */
    672   1.1  jonathan 	printf("sockaddr{ len=%u family=%u", addr->sa_len, addr->sa_family);
    673   1.1  jonathan 
    674   1.1  jonathan 	switch (addr->sa_family) {
    675   1.1  jonathan 	case AF_INET:
    676  1.10  drochner 		sin4 = (const struct sockaddr_in *)addr;
    677   1.1  jonathan 		printf(" port=%u\n", ntohs(sin4->sin_port));
    678  1.10  drochner 		ipsec_hexdump((const char *)&sin4->sin_addr, sizeof(sin4->sin_addr));
    679   1.1  jonathan 		break;
    680   1.1  jonathan #ifdef INET6
    681   1.1  jonathan 	case AF_INET6:
    682  1.10  drochner 		sin6 = (const struct sockaddr_in6 *)addr;
    683   1.1  jonathan 		printf(" port=%u\n", ntohs(sin6->sin6_port));
    684   1.1  jonathan 		printf("  flowinfo=0x%08x, scope_id=0x%08x\n",
    685   1.1  jonathan 		    sin6->sin6_flowinfo, sin6->sin6_scope_id);
    686  1.10  drochner 		ipsec_hexdump((const char *)&sin6->sin6_addr, sizeof(sin6->sin6_addr));
    687   1.1  jonathan 		break;
    688   1.1  jonathan #endif
    689   1.1  jonathan 	}
    690   1.1  jonathan 
    691   1.1  jonathan 	printf("  }\n");
    692   1.1  jonathan 
    693   1.1  jonathan 	return;
    694   1.1  jonathan }
    695   1.1  jonathan 
    696   1.1  jonathan void
    697  1.10  drochner ipsec_bindump(const char *buf, int len)
    698   1.1  jonathan {
    699   1.1  jonathan 	int i;
    700   1.1  jonathan 
    701   1.1  jonathan 	for (i = 0; i < len; i++)
    702   1.1  jonathan 		printf("%c", (unsigned char)buf[i]);
    703   1.1  jonathan 
    704   1.1  jonathan 	return;
    705   1.1  jonathan }
    706   1.1  jonathan 
    707   1.1  jonathan 
    708   1.1  jonathan void
    709  1.10  drochner ipsec_hexdump(const char *buf, int len)
    710   1.1  jonathan {
    711   1.1  jonathan 	int i;
    712   1.1  jonathan 
    713   1.1  jonathan 	for (i = 0; i < len; i++) {
    714   1.1  jonathan 		if (i != 0 && i % 32 == 0) printf("\n");
    715   1.1  jonathan 		if (i % 4 == 0) printf(" ");
    716   1.1  jonathan 		printf("%02x", (unsigned char)buf[i]);
    717   1.1  jonathan 	}
    718   1.1  jonathan #if 0
    719   1.1  jonathan 	if (i % 32 != 0) printf("\n");
    720   1.1  jonathan #endif
    721   1.1  jonathan 
    722   1.1  jonathan 	return;
    723   1.1  jonathan }
    724