]> git.saurik.com Git - apple/network_cmds.git/blame - racoon.tproj/pfkey_dump.c
network_cmds-176.tar.gz
[apple/network_cmds.git] / racoon.tproj / pfkey_dump.c
CommitLineData
7ba0088d
A
1/* $FreeBSD: src/lib/libipsec/pfkey_dump.c,v 1.1.2.2 2001/07/03 11:01:15 ume Exp $ */
2/* $KAME: pfkey_dump.c,v 1.27 2001/03/12 09:03:38 itojun Exp $ */
3
4/*
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 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#include <sys/types.h>
34#include <sys/param.h>
35#include <sys/socket.h>
36#include <netinet6/ipsec.h>
37#include <net/pfkeyv2.h>
38#include <netkey/key_var.h>
39#include <netkey/key_debug.h>
40
41#include <netinet/in.h>
42#include <netinet6/ipsec.h>
43#include <arpa/inet.h>
44
45#include <stdlib.h>
46#include <unistd.h>
47#include <stdio.h>
48#include <string.h>
49#include <time.h>
50#include <netdb.h>
51
52#include "ipsec_strerror.h"
53#include "libpfkey.h"
54
55/* cope with old kame headers - ugly */
56#ifndef SADB_X_AALG_MD5
57#define SADB_X_AALG_MD5 SADB_AALG_MD5
58#endif
59#ifndef SADB_X_AALG_SHA
60#define SADB_X_AALG_SHA SADB_AALG_SHA
61#endif
62#ifndef SADB_X_AALG_NULL
63#define SADB_X_AALG_NULL SADB_AALG_NULL
64#endif
65
66#ifndef SADB_X_EALG_BLOWFISHCBC
67#define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC
68#endif
69#ifndef SADB_X_EALG_CAST128CBC
70#define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC
71#endif
72#ifndef SADB_X_EALG_RC5CBC
73#ifdef SADB_EALG_RC5CBC
74#define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC
75#endif
76#endif
77
78#define GETMSGSTR(str, num) \
79do { \
80 if (sizeof((str)[0]) == 0 \
81 || num >= sizeof(str)/sizeof((str)[0])) \
82 printf("%d ", (num)); \
83 else if (strlen((str)[(num)]) == 0) \
84 printf("%d ", (num)); \
85 else \
86 printf("%s ", (str)[(num)]); \
87} while (0)
88
89#define GETMSGV2S(v2s, num) \
90do { \
91 struct val2str *p; \
92 for (p = (v2s); p && p->str; p++) { \
93 if (p->val == (num)) \
94 break; \
95 } \
96 if (p && p->str) \
97 printf("%s ", p->str); \
98 else \
99 printf("%d ", (num)); \
100} while (0)
101
102static char *str_ipaddr __P((struct sockaddr *));
103static char *str_prefport __P((u_int, u_int, u_int));
104static char *str_time __P((time_t));
105static void str_lifetime_byte __P((struct sadb_lifetime *, char *));
106
107struct val2str {
108 int val;
109 const char *str;
110};
111
112/*
113 * Must to be re-written about following strings.
114 */
115static char *str_satype[] = {
116 "unspec",
117 "unknown",
118 "ah",
119 "esp",
120 "unknown",
121 "rsvp",
122 "ospfv2",
123 "ripv2",
124 "mip",
125 "ipcomp",
126};
127
128static char *str_mode[] = {
129 "any",
130 "transport",
131 "tunnel",
132};
133
134static char *str_upper[] = {
135/*0*/ "ip", "icmp", "igmp", "ggp", "ip4",
136 "", "tcp", "", "egp", "",
137/*10*/ "", "", "", "", "",
138 "", "", "udp", "", "",
139/*20*/ "", "", "idp", "", "",
140 "", "", "", "", "tp",
141/*30*/ "", "", "", "", "",
142 "", "", "", "", "",
143/*40*/ "", "ip6", "", "rt6", "frag6",
144 "", "rsvp", "gre", "", "",
145/*50*/ "esp", "ah", "", "", "",
146 "", "", "", "icmp6", "none",
147/*60*/ "dst6",
148};
149
150static char *str_state[] = {
151 "larval",
152 "mature",
153 "dying",
154 "dead",
155};
156
157static struct val2str str_alg_auth[] = {
158 { SADB_AALG_NONE, "none", },
159 { SADB_AALG_MD5HMAC, "hmac-md5", },
160 { SADB_AALG_SHA1HMAC, "hmac-sha1", },
161 { SADB_X_AALG_MD5, "md5", },
162 { SADB_X_AALG_SHA, "sha", },
163 { SADB_X_AALG_NULL, "null", },
164#ifdef SADB_X_AALG_SHA2_256
165 { SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
166#endif
167#ifdef SADB_X_AALG_SHA2_384
168 { SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
169#endif
170#ifdef SADB_X_AALG_SHA2_512
171 { SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
172#endif
173 { -1, NULL, },
174};
175
176static struct val2str str_alg_enc[] = {
177 { SADB_EALG_NONE, "none", },
178 { SADB_EALG_DESCBC, "des-cbc", },
179 { SADB_EALG_3DESCBC, "3des-cbc", },
180 { SADB_EALG_NULL, "null", },
181#ifdef SADB_X_EALG_RC5CBC
182 { SADB_X_EALG_RC5CBC, "rc5-cbc", },
183#endif
184 { SADB_X_EALG_CAST128CBC, "cast128-cbc", },
185 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
186#ifdef SADB_X_EALG_RIJNDAELCBC
187 { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
188#endif
189#ifdef SADB_X_EALG_TWOFISHCBC
190 { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", },
191#endif
192 { -1, NULL, },
193};
194
195static struct val2str str_alg_comp[] = {
196 { SADB_X_CALG_NONE, "none", },
197 { SADB_X_CALG_OUI, "oui", },
198 { SADB_X_CALG_DEFLATE, "deflate", },
199 { SADB_X_CALG_LZS, "lzs", },
200 { -1, NULL, },
201};
202
203/*
204 * dump SADB_MSG formated. For debugging, you should use kdebug_sadb().
205 */
206void
207pfkey_sadump(m)
208 struct sadb_msg *m;
209{
210 caddr_t mhp[SADB_EXT_MAX + 1];
211 struct sadb_sa *m_sa;
212 struct sadb_x_sa2 *m_sa2;
213 struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
214 struct sadb_address *m_saddr, *m_daddr, *m_paddr;
215 struct sadb_key *m_auth, *m_enc;
216 struct sadb_ident *m_sid, *m_did;
217 struct sadb_sens *m_sens;
218
219 /* check pfkey message. */
220 if (pfkey_align(m, mhp)) {
221 printf("%s\n", ipsec_strerror());
222 return;
223 }
224 if (pfkey_check(mhp)) {
225 printf("%s\n", ipsec_strerror());
226 return;
227 }
228
229 m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
230 m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
231 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
232 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
233 m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
234 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
235 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
236 m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY];
237 m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
238 m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
239 m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
240 m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
241 m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
242
243 /* source address */
244 if (m_saddr == NULL) {
245 printf("no ADDRESS_SRC extension.\n");
246 return;
247 }
248 printf("%s ", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
249
250 /* destination address */
251 if (m_daddr == NULL) {
252 printf("no ADDRESS_DST extension.\n");
253 return;
254 }
255 printf("%s ", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
256
257 /* SA type */
258 if (m_sa == NULL) {
259 printf("no SA extension.\n");
260 return;
261 }
262 if (m_sa2 == NULL) {
263 printf("no SA2 extension.\n");
264 return;
265 }
266 printf("\n\t");
267
268 GETMSGSTR(str_satype, m->sadb_msg_satype);
269
270 printf("mode=");
271 GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
272
273 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
274 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
275 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
276 (u_int32_t)m_sa2->sadb_x_sa2_reqid,
277 (u_int32_t)m_sa2->sadb_x_sa2_reqid);
278
279 /* encryption key */
280 if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
281 printf("\tC: ");
282 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
283 } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
284 if (m_enc != NULL) {
285 printf("\tE: ");
286 GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
287 ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
288 m_enc->sadb_key_bits / 8);
289 printf("\n");
290 }
291 }
292
293 /* authentication key */
294 if (m_auth != NULL) {
295 printf("\tA: ");
296 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
297 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
298 m_auth->sadb_key_bits / 8);
299 printf("\n");
300 }
301
302 /* replay windoe size & flags */
303 printf("\treplay=%u flags=0x%08x ",
304 m_sa->sadb_sa_replay,
305 m_sa->sadb_sa_flags);
306
307 /* state */
308 printf("state=");
309 GETMSGSTR(str_state, m_sa->sadb_sa_state);
310
311 printf("seq=%lu pid=%lu\n",
312 (u_long)m->sadb_msg_seq,
313 (u_long)m->sadb_msg_pid);
314
315 /* lifetime */
316 if (m_lftc != NULL) {
317 time_t tmp_time = time(0);
318
319 printf("\tcreated: %s",
320 str_time(m_lftc->sadb_lifetime_addtime));
321 printf("\tcurrent: %s\n", str_time(tmp_time));
322 printf("\tdiff: %lu(s)",
323 (u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
324 0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
325
326 printf("\thard: %lu(s)",
327 (u_long)(m_lfth == NULL ?
328 0 : m_lfth->sadb_lifetime_addtime));
329 printf("\tsoft: %lu(s)\n",
330 (u_long)(m_lfts == NULL ?
331 0 : m_lfts->sadb_lifetime_addtime));
332
333 printf("\tlast: %s",
334 str_time(m_lftc->sadb_lifetime_usetime));
335 printf("\thard: %lu(s)",
336 (u_long)(m_lfth == NULL ?
337 0 : m_lfth->sadb_lifetime_usetime));
338 printf("\tsoft: %lu(s)\n",
339 (u_long)(m_lfts == NULL ?
340 0 : m_lfts->sadb_lifetime_usetime));
341
342 str_lifetime_byte(m_lftc, "current");
343 str_lifetime_byte(m_lfth, "hard");
344 str_lifetime_byte(m_lfts, "soft");
345 printf("\n");
346
347 printf("\tallocated: %lu",
348 (unsigned long)m_lftc->sadb_lifetime_allocations);
349 printf("\thard: %lu",
350 (u_long)(m_lfth == NULL ?
351 0 : m_lfth->sadb_lifetime_allocations));
352 printf("\tsoft: %lu\n",
353 (u_long)(m_lfts == NULL ?
354 0 : m_lfts->sadb_lifetime_allocations));
355 }
356
357 /* XXX DEBUG */
358 printf("\trefcnt=%u\n", m->sadb_msg_reserved);
359
360 return;
361}
362
363void
364pfkey_spdump(m)
365 struct sadb_msg *m;
366{
367 char pbuf[NI_MAXSERV];
368 caddr_t mhp[SADB_EXT_MAX + 1];
369 struct sadb_address *m_saddr, *m_daddr;
370 struct sadb_x_policy *m_xpl;
371 struct sadb_lifetime *m_lft = NULL;
372 struct sockaddr *sa;
373 u_int16_t port;
374
375 /* check pfkey message. */
376 if (pfkey_align(m, mhp)) {
377 printf("%s\n", ipsec_strerror());
378 return;
379 }
380 if (pfkey_check(mhp)) {
381 printf("%s\n", ipsec_strerror());
382 return;
383 }
384
385 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
386 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
387 m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
388 m_lft = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
389
390 /* source address */
391 if (m_saddr == NULL) {
392 printf("no ADDRESS_SRC extension.\n");
393 return;
394 }
395 sa = (struct sockaddr *)(m_saddr + 1);
396 switch (sa->sa_family) {
397 case AF_INET:
398 case AF_INET6:
399 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf),
400 NI_NUMERICSERV) != 0)
401 port = 0; /*XXX*/
402 else
403 port = atoi(pbuf);
404 printf("%s%s ", str_ipaddr(sa),
405 str_prefport(sa->sa_family,
406 m_saddr->sadb_address_prefixlen, port));
407 break;
408 default:
409 printf("unknown-af ");
410 break;
411 }
412
413 /* destination address */
414 if (m_daddr == NULL) {
415 printf("no ADDRESS_DST extension.\n");
416 return;
417 }
418 sa = (struct sockaddr *)(m_daddr + 1);
419 switch (sa->sa_family) {
420 case AF_INET:
421 case AF_INET6:
422 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf),
423 NI_NUMERICSERV) != 0)
424 port = 0; /*XXX*/
425 else
426 port = atoi(pbuf);
427 printf("%s%s ", str_ipaddr(sa),
428 str_prefport(sa->sa_family,
429 m_daddr->sadb_address_prefixlen, port));
430 break;
431 default:
432 printf("unknown-af ");
433 break;
434 }
435
436 /* upper layer protocol */
437 if (m_saddr->sadb_address_proto != m_daddr->sadb_address_proto) {
438 printf("upper layer protocol mismatched.\n");
439 return;
440 }
441 if (m_saddr->sadb_address_proto == IPSEC_ULPROTO_ANY)
442 printf("any");
443 else
444 GETMSGSTR(str_upper, m_saddr->sadb_address_proto);
445
446 /* policy */
447 {
448 char *d_xpl;
449
450 if (m_xpl == NULL) {
451 printf("no X_POLICY extension.\n");
452 return;
453 }
454 d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
455
456 /* dump SPD */
457 printf("\n\t%s\n", d_xpl);
458 free(d_xpl);
459 }
460
461 /* lifetime */
462 if (m_lft) {
463 printf("\tlifetime:%lu validtime:%lu\n",
464 (u_long)m_lft->sadb_lifetime_addtime,
465 (u_long)m_lft->sadb_lifetime_usetime);
466 }
467
468 printf("\tspid=%ld seq=%ld pid=%ld\n",
469 (u_long)m_xpl->sadb_x_policy_id,
470 (u_long)m->sadb_msg_seq,
471 (u_long)m->sadb_msg_pid);
472
473 /* XXX TEST */
474 printf("\trefcnt=%u\n", m->sadb_msg_reserved);
475
476 return;
477}
478
479/*
480 * set "ipaddress" to buffer.
481 */
482static char *
483str_ipaddr(sa)
484 struct sockaddr *sa;
485{
486 static char buf[NI_MAXHOST];
487#ifdef NI_WITHSCOPEID
488 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
489#else
490 const int niflag = NI_NUMERICHOST;
491#endif
492
493 if (sa == NULL)
494 return "";
495
496 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0)
497 return buf;
498 return NULL;
499}
500
501/*
502 * set "/prefix[port number]" to buffer.
503 */
504static char *
505str_prefport(family, pref, port)
506 u_int family, pref, port;
507{
508 static char buf[128];
ac2f15b3
A
509 char prefbuf[20];
510 char portbuf[20];
7ba0088d
A
511 int plen;
512
513 switch (family) {
514 case AF_INET:
515 plen = sizeof(struct in_addr) << 3;
516 break;
517 case AF_INET6:
518 plen = sizeof(struct in6_addr) << 3;
519 break;
520 default:
521 return "?";
522 }
523
524 if (pref == plen)
525 prefbuf[0] = '\0';
526 else
527 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
528
529 if (port == IPSEC_PORT_ANY)
530 snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
531 else
532 snprintf(portbuf, sizeof(portbuf), "[%u]", port);
533
534 snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
535
536 return buf;
537}
538
539/*
540 * set "Mon Day Time Year" to buffer
541 */
542static char *
543str_time(t)
544 time_t t;
545{
546 static char buf[128];
547
548 if (t == 0) {
549 int i = 0;
550 for (;i < 20;) buf[i++] = ' ';
551 } else {
552 char *t0;
553 t0 = ctime(&t);
554 memcpy(buf, t0 + 4, 20);
555 }
556
557 buf[20] = '\0';
558
559 return(buf);
560}
561
562static void
563str_lifetime_byte(x, str)
564 struct sadb_lifetime *x;
565 char *str;
566{
567 double y;
568 char *unit;
569 int w;
570
571 if (x == NULL) {
572 printf("\t%s: 0(bytes)", str);
573 return;
574 }
575
576#if 0
577 if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
578 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
579 unit = "M";
580 w = 1;
581 } else if ((x->sadb_lifetime_bytes) / 1024) {
582 y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
583 unit = "K";
584 w = 1;
585 } else {
586 y = (x->sadb_lifetime_bytes) * 1.0;
587 unit = "";
588 w = 0;
589 }
590#else
591 y = (x->sadb_lifetime_bytes) * 1.0;
592 unit = "";
593 w = 0;
594#endif
595 printf("\t%s: %.*f(%sbytes)", str, w, y, unit);
596}