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