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