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