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