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