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