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