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