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