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