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