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