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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <netipsec/ipsec.h>
39 #include <net/pfkeyv2.h>
40 #include <netipsec/key_var.h>
41 #include <netipsec/key_debug.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
53 #include "ipsec_strerror.h"
56 /* cope with old kame headers - ugly */
57 #ifndef SADB_X_AALG_MD5
58 #define SADB_X_AALG_MD5 SADB_AALG_MD5
60 #ifndef SADB_X_AALG_SHA
61 #define SADB_X_AALG_SHA SADB_AALG_SHA
63 #ifndef SADB_X_AALG_NULL
64 #define SADB_X_AALG_NULL SADB_AALG_NULL
67 #ifndef SADB_X_EALG_BLOWFISHCBC
68 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC
70 #ifndef SADB_X_EALG_CAST128CBC
71 #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC
73 #ifndef SADB_X_EALG_RC5CBC
74 #ifdef SADB_EALG_RC5CBC
75 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC
79 #define GETMSGSTR(str, num) \
81 if (sizeof((str)[0]) == 0 \
82 || num >= sizeof(str)/sizeof((str)[0])) \
83 printf("%u ", (num)); \
84 else if (strlen((str)[(num)]) == 0) \
85 printf("%u ", (num)); \
87 printf("%s ", (str)[(num)]); \
90 #define GETMSGV2S(v2s, num) \
93 for (p = (v2s); p && p->str; p++) { \
94 if (p->val == (num)) \
98 printf("%s ", p->str); \
100 printf("%u ", (num)); \
103 static char *str_ipaddr(struct sockaddr *);
104 static char *str_prefport(u_int, u_int, u_int, u_int);
105 static void str_upperspec(u_int, u_int, u_int);
106 static char *str_time(time_t);
107 static void str_lifetime_byte(struct sadb_lifetime *, char *);
115 * Must to be re-written about following strings.
117 static char *str_satype[] = {
132 static char *str_mode[] = {
138 static char *str_state[] = {
145 static struct val2str str_alg_auth[] = {
146 { SADB_AALG_NONE, "none", },
147 { SADB_AALG_MD5HMAC, "hmac-md5", },
148 { SADB_AALG_SHA1HMAC, "hmac-sha1", },
149 { SADB_X_AALG_MD5, "md5", },
150 { SADB_X_AALG_SHA, "sha", },
151 { SADB_X_AALG_NULL, "null", },
152 { SADB_X_AALG_TCP_MD5, "tcp-md5", },
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_RIJNDAELCBC
182 { SADB_X_EALG_RIJNDAELCBC, "rijndael-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", },
190 #ifdef SADB_X_EALG_AESGCM16
191 { SADB_X_EALG_AESGCM16, "aes-gcm-16", },
193 #ifdef SADB_X_EALG_CAMELLIACBC
194 { SADB_X_EALG_CAMELLIACBC, "camellia-cbc", },
199 static struct val2str str_alg_comp[] = {
200 { SADB_X_CALG_NONE, "none", },
201 { SADB_X_CALG_OUI, "oui", },
202 { SADB_X_CALG_DEFLATE, "deflate", },
203 { SADB_X_CALG_LZS, "lzs", },
208 * dump SADB_MSG formated. For debugging, you should use kdebug_sadb().
214 caddr_t mhp[SADB_EXT_MAX + 1];
215 struct sadb_sa *m_sa;
216 struct sadb_x_sa2 *m_sa2;
217 struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
218 struct sadb_address *m_saddr, *m_daddr, *m_paddr;
219 struct sadb_key *m_auth, *m_enc;
220 struct sadb_ident *m_sid, *m_did;
221 struct sadb_sens *m_sens;
222 struct sadb_x_sa_replay *m_sa_replay;
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 m_sa_replay = (struct sadb_x_sa_replay *)mhp[SADB_X_EXT_SA_REPLAY];
250 if (m_saddr == NULL) {
251 printf("no ADDRESS_SRC extension.\n");
254 printf("%s ", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
256 /* destination address */
257 if (m_daddr == NULL) {
258 printf("no ADDRESS_DST extension.\n");
261 printf("%s ", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
265 printf("no SA extension.\n");
269 printf("no SA2 extension.\n");
274 GETMSGSTR(str_satype, m->sadb_msg_satype);
277 GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
279 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
280 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
281 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
282 (u_int32_t)m_sa2->sadb_x_sa2_reqid,
283 (u_int32_t)m_sa2->sadb_x_sa2_reqid);
286 if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
288 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
289 } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
292 GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
293 ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
294 m_enc->sadb_key_bits / 8);
299 /* authentication key */
300 if (m_auth != NULL) {
302 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
303 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
304 m_auth->sadb_key_bits / 8);
308 /* replay windoe size & flags */
309 printf("\tseq=0x%08x replay=%u flags=0x%08x ",
310 m_sa2->sadb_x_sa2_sequence,
311 m_sa_replay ? (m_sa_replay->sadb_x_sa_replay_replay >> 3) :
312 m_sa->sadb_sa_replay,
313 m_sa->sadb_sa_flags);
317 GETMSGSTR(str_state, m_sa->sadb_sa_state);
321 if (m_lftc != NULL) {
322 time_t tmp_time = time(0);
324 printf("\tcreated: %s",
325 str_time(m_lftc->sadb_lifetime_addtime));
326 printf("\tcurrent: %s\n", str_time(tmp_time));
327 printf("\tdiff: %lu(s)",
328 (u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
329 0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
331 printf("\thard: %lu(s)",
332 (u_long)(m_lfth == NULL ?
333 0 : m_lfth->sadb_lifetime_addtime));
334 printf("\tsoft: %lu(s)\n",
335 (u_long)(m_lfts == NULL ?
336 0 : m_lfts->sadb_lifetime_addtime));
339 str_time(m_lftc->sadb_lifetime_usetime));
340 printf("\thard: %lu(s)",
341 (u_long)(m_lfth == NULL ?
342 0 : m_lfth->sadb_lifetime_usetime));
343 printf("\tsoft: %lu(s)\n",
344 (u_long)(m_lfts == NULL ?
345 0 : m_lfts->sadb_lifetime_usetime));
347 str_lifetime_byte(m_lftc, "current");
348 str_lifetime_byte(m_lfth, "hard");
349 str_lifetime_byte(m_lfts, "soft");
352 printf("\tallocated: %lu",
353 (unsigned long)m_lftc->sadb_lifetime_allocations);
354 printf("\thard: %lu",
355 (u_long)(m_lfth == NULL ?
356 0 : m_lfth->sadb_lifetime_allocations));
357 printf("\tsoft: %lu\n",
358 (u_long)(m_lfts == NULL ?
359 0 : m_lfts->sadb_lifetime_allocations));
362 printf("\tsadb_seq=%lu pid=%lu ",
363 (u_long)m->sadb_msg_seq,
364 (u_long)m->sadb_msg_pid);
367 printf("refcnt=%u\n", m->sadb_msg_reserved);
376 char pbuf[NI_MAXSERV];
377 caddr_t mhp[SADB_EXT_MAX + 1];
378 struct sadb_address *m_saddr, *m_daddr;
379 struct sadb_x_policy *m_xpl;
380 struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL;
382 u_int16_t sport = 0, dport = 0;
384 /* check pfkey message. */
385 if (pfkey_align(m, mhp)) {
386 printf("%s\n", ipsec_strerror());
389 if (pfkey_check(mhp)) {
390 printf("%s\n", ipsec_strerror());
394 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
395 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
396 m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
397 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
398 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
400 if (m_saddr && m_daddr) {
402 sa = (struct sockaddr *)(m_saddr + 1);
403 switch (sa->sa_family) {
406 if (getnameinfo(sa, sa->sa_len, NULL, 0,
407 pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
411 printf("%s%s ", str_ipaddr(sa),
412 str_prefport(sa->sa_family,
413 m_saddr->sadb_address_prefixlen, sport,
414 m_saddr->sadb_address_proto));
417 printf("unknown-af ");
421 /* destination address */
422 sa = (struct sockaddr *)(m_daddr + 1);
423 switch (sa->sa_family) {
426 if (getnameinfo(sa, sa->sa_len, NULL, 0,
427 pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
431 printf("%s%s ", str_ipaddr(sa),
432 str_prefport(sa->sa_family,
433 m_daddr->sadb_address_prefixlen, dport,
434 m_saddr->sadb_address_proto));
437 printf("unknown-af ");
441 /* upper layer protocol */
442 if (m_saddr->sadb_address_proto !=
443 m_daddr->sadb_address_proto) {
444 printf("upper layer protocol mismatched.\n");
447 str_upperspec(m_saddr->sadb_address_proto, sport, dport);
450 printf("(no selector, probably per-socket policy) ");
457 printf("no X_POLICY extension.\n");
460 d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
463 printf("\n\t%s\n", d_xpl);
469 printf("\tcreated: %s ",
470 str_time(m_lftc->sadb_lifetime_addtime));
471 printf("lastused: %s\n",
472 str_time(m_lftc->sadb_lifetime_usetime));
475 printf("\tlifetime: %lu(s) ",
476 (u_long)m_lfth->sadb_lifetime_addtime);
477 printf("validtime: %lu(s)\n",
478 (u_long)m_lfth->sadb_lifetime_usetime);
482 printf("\tspid=%ld seq=%ld pid=%ld\n",
483 (u_long)m_xpl->sadb_x_policy_id,
484 (u_long)m->sadb_msg_seq,
485 (u_long)m->sadb_msg_pid);
488 printf("\trefcnt=%u\n", m->sadb_msg_reserved);
494 * set "ipaddress" to buffer.
500 static char buf[NI_MAXHOST];
501 const int niflag = NI_NUMERICHOST;
506 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0)
512 * set "/prefix[port number]" to buffer.
515 str_prefport(family, pref, port, ulp)
516 u_int family, pref, port, ulp;
518 static char buf[128];
525 plen = sizeof(struct in_addr) << 3;
528 plen = sizeof(struct in6_addr) << 3;
537 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
539 if (ulp == IPPROTO_ICMPV6)
540 memset(portbuf, 0, sizeof(portbuf));
542 if (port == IPSEC_PORT_ANY)
543 snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
545 snprintf(portbuf, sizeof(portbuf), "[%u]", port);
548 snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
554 str_upperspec(ulp, p1, p2)
557 if (ulp == IPSEC_ULPROTO_ANY)
559 else if (ulp == IPPROTO_ICMPV6) {
561 if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY))
562 printf(" %u,%u", p1, p2);
564 struct protoent *ent;
571 ent = getprotobynumber(ulp);
573 printf("%s", ent->p_name);
584 * set "Mon Day Time Year" to buffer
590 static char buf[128];
594 for (;i < 20;) buf[i++] = ' ';
598 memcpy(buf, t0 + 4, 20);
607 str_lifetime_byte(x, str)
608 struct sadb_lifetime *x;
616 printf("\t%s: 0(bytes)", str);
621 if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
622 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
625 } else if ((x->sadb_lifetime_bytes) / 1024) {
626 y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
630 y = (x->sadb_lifetime_bytes) * 1.0;
635 y = (x->sadb_lifetime_bytes) * 1.0;
639 printf("\t%s: %.*f(%sbytes)", str, w, y, unit);