1 /* $KAME: pfkey_dump.c,v 1.45 2003/09/08 10:14:56 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #ifdef HAVE_NETINET6_IPSEC
40 # include <netinet6/ipsec.h>
42 # include <netinet/ipsec.h>
44 #include <net/pfkeyv2.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
56 #include "ipsec_strerror.h"
59 /* cope with old kame headers - ugly */
60 #ifndef SADB_X_AALG_MD5
61 #define SADB_X_AALG_MD5 SADB_AALG_MD5
63 #ifndef SADB_X_AALG_SHA
64 #define SADB_X_AALG_SHA SADB_AALG_SHA
66 #ifndef SADB_X_AALG_NULL
67 #define SADB_X_AALG_NULL SADB_AALG_NULL
70 #ifndef SADB_X_EALG_BLOWFISHCBC
71 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC
73 #ifndef SADB_X_EALG_CAST128CBC
74 #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC
76 #ifndef SADB_X_EALG_RC5CBC
77 #ifdef SADB_EALG_RC5CBC
78 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC
82 #define GETMSGSTR(str, num) \
84 if (sizeof((str)[0]) == 0 \
85 || num >= sizeof(str)/sizeof((str)[0])) \
86 printf("%u ", (num)); \
87 else if (strlen((str)[(num)]) == 0) \
88 printf("%u ", (num)); \
90 printf("%s ", (str)[(num)]); \
93 #define GETMSGV2S(v2s, num) \
96 for (p = (v2s); p && p->str; p++) { \
97 if (p->val == (num)) \
101 printf("%s ", p->str); \
103 printf("%u ", (num)); \
106 static char *str_ipaddr __P((struct sockaddr *));
107 static char *str_prefport __P((u_int, u_int, u_int, u_int));
108 static void str_upperspec __P((u_int, u_int, u_int));
109 static char *str_time __P((time_t));
110 static void str_lifetime_byte __P((struct sadb_lifetime *, char *));
118 * Must to be re-written about following strings.
120 static char *str_satype[] = {
133 static char *str_mode[] = {
139 static char *str_state[] = {
146 static struct val2str str_alg_auth[] = {
147 { SADB_AALG_NONE, "none", },
148 { SADB_AALG_MD5HMAC, "hmac-md5", },
149 { SADB_AALG_SHA1HMAC, "hmac-sha1", },
150 { SADB_X_AALG_MD5, "md5", },
151 { SADB_X_AALG_SHA, "sha", },
152 { SADB_X_AALG_NULL, "null", },
153 #ifdef SADB_X_AALG_SHA2_256
154 { SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
156 #ifdef SADB_X_AALG_SHA2_384
157 { SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
159 #ifdef SADB_X_AALG_SHA2_512
160 { SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
162 #ifdef SADB_X_AALG_RIPEMD160HMAC
163 { SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", },
165 #ifdef SADB_X_AALG_AES_XCBC_MAC
166 { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", },
171 static struct val2str str_alg_enc[] = {
172 { SADB_EALG_NONE, "none", },
173 { SADB_EALG_DESCBC, "des-cbc", },
174 { SADB_EALG_3DESCBC, "3des-cbc", },
175 { SADB_EALG_NULL, "null", },
176 #ifdef SADB_X_EALG_RC5CBC
177 { SADB_X_EALG_RC5CBC, "rc5-cbc", },
179 { SADB_X_EALG_CAST128CBC, "cast128-cbc", },
180 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
181 #ifdef SADB_X_EALG_AESCBC
182 { SADB_X_EALG_AESCBC, "aes-cbc", },
184 #ifdef SADB_X_EALG_TWOFISHCBC
185 { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", },
187 #ifdef SADB_X_EALG_AESCTR
188 { SADB_X_EALG_AESCTR, "aes-ctr", },
193 static struct val2str str_alg_comp[] = {
194 { SADB_X_CALG_NONE, "none", },
195 { SADB_X_CALG_OUI, "oui", },
196 { SADB_X_CALG_DEFLATE, "deflate", },
197 { SADB_X_CALG_LZS, "lzs", },
202 * dump SADB_MSG formated. For debugging, you should use kdebug_sadb().
208 caddr_t mhp[SADB_EXT_MAX + 1];
209 struct sadb_sa *m_sa;
210 struct sadb_x_sa2 *m_sa2;
211 struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
212 struct sadb_address *m_saddr, *m_daddr, *m_paddr;
213 struct sadb_key *m_auth, *m_enc;
214 struct sadb_ident *m_sid, *m_did;
215 struct sadb_sens *m_sens;
216 #ifdef SADB_X_EXT_NAT_T_TYPE
217 struct sadb_x_nat_t_type *natt_type;
218 struct sadb_x_nat_t_port *natt_sport, *natt_dport;
219 struct sadb_address *natt_oa;
224 /* check pfkey message. */
225 if (pfkey_align(m, mhp)) {
226 printf("%s\n", ipsec_strerror());
229 if (pfkey_check(mhp)) {
230 printf("%s\n", ipsec_strerror());
234 m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
235 m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
236 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
237 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
238 m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
239 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
240 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
241 m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY];
242 m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
243 m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
244 m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
245 m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
246 m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
247 #ifdef SADB_X_EXT_NAT_T_TYPE
248 natt_type = (struct sadb_x_nat_t_type *)mhp[SADB_X_EXT_NAT_T_TYPE];
249 natt_sport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_SPORT];
250 natt_dport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_DPORT];
251 natt_oa = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OA];
253 if (natt_type && natt_type->sadb_x_nat_t_type_type)
258 if (m_saddr == NULL) {
259 printf("no ADDRESS_SRC extension.\n");
262 printf("%s", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
263 #ifdef SADB_X_EXT_NAT_T_TYPE
264 if (use_natt && natt_sport)
265 printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port));
269 /* destination address */
270 if (m_daddr == NULL) {
271 printf(" no ADDRESS_DST extension.\n");
274 printf("%s", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
275 #ifdef SADB_X_EXT_NAT_T_TYPE
276 if (use_natt && natt_dport)
277 printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port));
283 printf("no SA extension.\n");
287 printf("no SA2 extension.\n");
292 #ifdef SADB_X_EXT_NAT_T_TYPE
293 if (use_natt && m->sadb_msg_satype == SADB_SATYPE_ESP)
298 if (!use_natt || m->sadb_msg_satype != SADB_SATYPE_ESP)
300 GETMSGSTR(str_satype, m->sadb_msg_satype);
303 GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
305 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
306 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
307 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
308 (u_int32_t)m_sa2->sadb_x_sa2_reqid,
309 (u_int32_t)m_sa2->sadb_x_sa2_reqid);
311 #ifdef SADB_X_EXT_NAT_T_TYPE
312 /* other NAT-T information */
313 if (use_natt && natt_oa)
314 printf("\tNAT OA=%s\n",
315 str_ipaddr((struct sockaddr *)(natt_oa + 1)));
319 if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
321 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
322 } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
325 GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
326 ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
327 m_enc->sadb_key_bits / 8);
332 /* authentication key */
333 if (m_auth != NULL) {
335 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
336 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
337 m_auth->sadb_key_bits / 8);
341 /* replay windoe size & flags */
342 printf("\tseq=0x%08x replay=%u flags=0x%08x ",
343 m_sa2->sadb_x_sa2_sequence,
344 m_sa->sadb_sa_replay,
345 m_sa->sadb_sa_flags);
349 GETMSGSTR(str_state, m_sa->sadb_sa_state);
353 if (m_lftc != NULL) {
354 time_t tmp_time = time(0);
356 printf("\tcreated: %s",
357 str_time(m_lftc->sadb_lifetime_addtime));
358 printf("\tcurrent: %s\n", str_time(tmp_time));
359 printf("\tdiff: %lu(s)",
360 (u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
361 0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
363 printf("\thard: %lu(s)",
364 (u_long)(m_lfth == NULL ?
365 0 : m_lfth->sadb_lifetime_addtime));
366 printf("\tsoft: %lu(s)\n",
367 (u_long)(m_lfts == NULL ?
368 0 : m_lfts->sadb_lifetime_addtime));
371 str_time(m_lftc->sadb_lifetime_usetime));
372 printf("\thard: %lu(s)",
373 (u_long)(m_lfth == NULL ?
374 0 : m_lfth->sadb_lifetime_usetime));
375 printf("\tsoft: %lu(s)\n",
376 (u_long)(m_lfts == NULL ?
377 0 : m_lfts->sadb_lifetime_usetime));
379 str_lifetime_byte(m_lftc, "current");
380 str_lifetime_byte(m_lfth, "hard");
381 str_lifetime_byte(m_lfts, "soft");
384 printf("\tallocated: %lu",
385 (unsigned long)m_lftc->sadb_lifetime_allocations);
386 printf("\thard: %lu",
387 (u_long)(m_lfth == NULL ?
388 0 : m_lfth->sadb_lifetime_allocations));
389 printf("\tsoft: %lu\n",
390 (u_long)(m_lfts == NULL ?
391 0 : m_lfts->sadb_lifetime_allocations));
394 printf("\tsadb_seq=%lu pid=%lu ",
395 (u_long)m->sadb_msg_seq,
396 (u_long)m->sadb_msg_pid);
399 printf("refcnt=%u\n", m->sadb_msg_reserved);
408 char pbuf[NI_MAXSERV];
409 caddr_t mhp[SADB_EXT_MAX + 1];
410 struct sadb_address *m_saddr, *m_daddr;
411 #ifdef SADB_X_EXT_TAG
412 struct sadb_x_tag *m_tag;
414 struct sadb_x_policy *m_xpl;
415 struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL;
417 u_int16_t sport = 0, dport = 0;
419 /* check pfkey message. */
420 if (pfkey_align(m, mhp)) {
421 printf("%s\n", ipsec_strerror());
424 if (pfkey_check(mhp)) {
425 printf("%s\n", ipsec_strerror());
429 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
430 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
431 #ifdef SADB_X_EXT_TAG
432 m_tag = (struct sadb_x_tag *)mhp[SADB_X_EXT_TAG];
434 m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
435 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
436 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
439 /* *bsd indicates per-socket policies by omiting src and dst
440 * extensions. Linux always includes them, but we can catch it
441 * by checkin for policy id.
443 if (m_xpl->sadb_x_policy_id % 8 >= 3) {
444 printf("(per-socket policy) ");
447 if (m_saddr && m_daddr) {
449 sa = (struct sockaddr *)(m_saddr + 1);
450 switch (sa->sa_family) {
453 if (getnameinfo(sa, sysdep_sa_len(sa), NULL, 0,
454 pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
458 printf("%s%s ", str_ipaddr(sa),
459 str_prefport(sa->sa_family,
460 m_saddr->sadb_address_prefixlen, sport,
461 m_saddr->sadb_address_proto));
464 printf("unknown-af ");
468 /* destination address */
469 sa = (struct sockaddr *)(m_daddr + 1);
470 switch (sa->sa_family) {
473 if (getnameinfo(sa, sysdep_sa_len(sa), NULL, 0,
474 pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
478 printf("%s%s ", str_ipaddr(sa),
479 str_prefport(sa->sa_family,
480 m_daddr->sadb_address_prefixlen, dport,
481 m_saddr->sadb_address_proto));
484 printf("unknown-af ");
488 /* upper layer protocol */
489 if (m_saddr->sadb_address_proto !=
490 m_daddr->sadb_address_proto) {
491 printf("upper layer protocol mismatched.\n");
494 str_upperspec(m_saddr->sadb_address_proto, sport, dport);
496 #ifdef SADB_X_EXT_TAG
498 printf("tagged \"%s\" ", m_tag->sadb_x_tag_name);
501 printf("(no selector, probably per-socket policy) ");
508 printf("no X_POLICY extension.\n");
511 d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
513 printf("\n\tPolicy:[%s]\n", ipsec_strerror());
516 printf("\n\t%s\n", d_xpl);
523 printf("\tcreated: %s ",
524 str_time(m_lftc->sadb_lifetime_addtime));
525 printf("lastused: %s\n",
526 str_time(m_lftc->sadb_lifetime_usetime));
529 printf("\tlifetime: %lu(s) ",
530 (u_long)m_lfth->sadb_lifetime_addtime);
531 printf("validtime: %lu(s)\n",
532 (u_long)m_lfth->sadb_lifetime_usetime);
536 printf("\tspid=%ld seq=%ld pid=%ld\n",
537 (u_long)m_xpl->sadb_x_policy_id,
538 (u_long)m->sadb_msg_seq,
539 (u_long)m->sadb_msg_pid);
542 printf("\trefcnt=%u\n", m->sadb_msg_reserved);
548 * set "ipaddress" to buffer.
554 static char buf[NI_MAXHOST];
555 const int niflag = NI_NUMERICHOST;
560 if (getnameinfo(sa, sysdep_sa_len(sa), buf, sizeof(buf), NULL, 0, niflag) == 0)
566 * set "/prefix[port number]" to buffer.
569 str_prefport(family, pref, port, ulp)
570 u_int family, pref, port, ulp;
572 static char buf[128];
579 plen = sizeof(struct in_addr) << 3;
582 plen = sizeof(struct in6_addr) << 3;
591 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
593 if (ulp == IPPROTO_ICMPV6)
594 memset(portbuf, 0, sizeof(portbuf));
596 if (port == IPSEC_PORT_ANY)
597 snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
599 snprintf(portbuf, sizeof(portbuf), "[%u]", port);
602 snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
608 str_upperspec(ulp, p1, p2)
611 if (ulp == IPSEC_ULPROTO_ANY)
613 else if (ulp == IPPROTO_ICMPV6) {
615 if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY))
616 printf(" %u,%u", p1, p2);
618 struct protoent *ent;
625 ent = getprotobynumber(ulp);
627 printf("%s", ent->p_name);
638 * set "Mon Day Time Year" to buffer
644 static char buf[128];
648 for (;i < 20;) buf[i++] = ' ';
652 memcpy(buf, t0 + 4, 20);
661 str_lifetime_byte(x, str)
662 struct sadb_lifetime *x;
670 printf("\t%s: 0(bytes)", str);
675 if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
676 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
679 } else if ((x->sadb_lifetime_bytes) / 1024) {
680 y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
684 y = (x->sadb_lifetime_bytes) * 1.0;
689 y = (x->sadb_lifetime_bytes) * 1.0;
693 printf("\t%s: %.*f(%sbytes)", str, w, y, unit);