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