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