1 /* $FreeBSD: src/lib/libipsec/pfkey_dump.c,v 1.1.2.4 2003/04/27 00:03:36 sumikawa Exp $ */
2 /* $DragonFly: src/lib/libipsec/pfkey_dump.c,v 1.3 2003/11/12 20:21:30 eirikn Exp $ */
3 /* $KAME: pfkey_dump.c,v 1.28 2001/06/27 10:46:51 sakane Exp $ */
6 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <netinet6/ipsec.h>
38 #include <net/pfkeyv2.h>
39 #include <netkey/key_var.h>
40 #include <netkey/key_debug.h>
42 #include <netinet/in.h>
43 #include <netinet6/ipsec.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("%d ", (num)); \
84 else if (strlen((str)[(num)]) == 0) \
85 printf("%d ", (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("%d ", (num)); \
103 static char *str_ipaddr (struct sockaddr *);
104 static char *str_prefport (u_int, u_int, u_int);
105 static char *str_time (time_t);
106 static void str_lifetime_byte (struct sadb_lifetime *, char *);
114 * Must to be re-written about following strings.
116 static char *str_satype[] = {
129 static char *str_mode[] = {
135 static char *str_upper[] = {
136 /*0*/ "ip", "icmp", "igmp", "ggp", "ip4",
137 "", "tcp", "", "egp", "",
138 /*10*/ "", "", "", "", "",
139 "", "", "udp", "", "",
140 /*20*/ "", "", "idp", "", "",
141 "", "", "", "", "tp",
142 /*30*/ "", "", "", "", "",
144 /*40*/ "", "ip6", "", "rt6", "frag6",
145 "", "rsvp", "gre", "", "",
146 /*50*/ "esp", "ah", "", "", "",
147 "", "", "", "icmp6", "none",
151 static char *str_state[] = {
158 static struct val2str str_alg_auth[] = {
159 { SADB_AALG_NONE, "none", },
160 { SADB_AALG_MD5HMAC, "hmac-md5", },
161 { SADB_AALG_SHA1HMAC, "hmac-sha1", },
162 { SADB_X_AALG_MD5, "md5", },
163 { SADB_X_AALG_SHA, "sha", },
164 { SADB_X_AALG_NULL, "null", },
165 #ifdef SADB_X_AALG_SHA2_256
166 { SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
168 #ifdef SADB_X_AALG_SHA2_384
169 { SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
171 #ifdef SADB_X_AALG_SHA2_512
172 { SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
177 static struct val2str str_alg_enc[] = {
178 { SADB_EALG_NONE, "none", },
179 { SADB_EALG_DESCBC, "des-cbc", },
180 { SADB_EALG_3DESCBC, "3des-cbc", },
181 { SADB_EALG_NULL, "null", },
182 #ifdef SADB_X_EALG_RC5CBC
183 { SADB_X_EALG_RC5CBC, "rc5-cbc", },
185 { SADB_X_EALG_CAST128CBC, "cast128-cbc", },
186 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
187 #ifdef SADB_X_EALG_RIJNDAELCBC
188 { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
190 #ifdef SADB_X_EALG_TWOFISHCBC
191 { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", },
196 static struct val2str str_alg_comp[] = {
197 { SADB_X_CALG_NONE, "none", },
198 { SADB_X_CALG_OUI, "oui", },
199 { SADB_X_CALG_DEFLATE, "deflate", },
200 { SADB_X_CALG_LZS, "lzs", },
205 * dump SADB_MSG formated. For debugging, you should use kdebug_sadb().
211 caddr_t mhp[SADB_EXT_MAX + 1];
212 struct sadb_sa *m_sa;
213 struct sadb_x_sa2 *m_sa2;
214 struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
215 struct sadb_address *m_saddr, *m_daddr, *m_paddr;
216 struct sadb_key *m_auth, *m_enc;
217 struct sadb_ident *m_sid, *m_did;
218 struct sadb_sens *m_sens;
220 /* check pfkey message. */
221 if (pfkey_align(m, mhp)) {
222 printf("%s\n", ipsec_strerror());
225 if (pfkey_check(mhp)) {
226 printf("%s\n", ipsec_strerror());
230 m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
231 m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
232 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
233 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
234 m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
235 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
236 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
237 m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY];
238 m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
239 m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
240 m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
241 m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
242 m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
245 if (m_saddr == NULL) {
246 printf("no ADDRESS_SRC extension.\n");
249 printf("%s ", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
251 /* destination address */
252 if (m_daddr == NULL) {
253 printf("no ADDRESS_DST extension.\n");
256 printf("%s ", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
260 printf("no SA extension.\n");
264 printf("no SA2 extension.\n");
269 GETMSGSTR(str_satype, m->sadb_msg_satype);
272 GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
274 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
275 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
276 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
277 (u_int32_t)m_sa2->sadb_x_sa2_reqid,
278 (u_int32_t)m_sa2->sadb_x_sa2_reqid);
281 if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
283 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
284 } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
287 GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
288 ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
289 m_enc->sadb_key_bits / 8);
294 /* authentication key */
295 if (m_auth != NULL) {
297 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
298 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
299 m_auth->sadb_key_bits / 8);
303 /* replay windoe size & flags */
304 printf("\tseq=0x%08x replay=%u flags=0x%08x ",
305 m_sa2->sadb_x_sa2_sequence,
306 m_sa->sadb_sa_replay,
307 m_sa->sadb_sa_flags);
311 GETMSGSTR(str_state, m_sa->sadb_sa_state);
315 if (m_lftc != NULL) {
316 time_t tmp_time = time(0);
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)));
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));
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));
341 str_lifetime_byte(m_lftc, "current");
342 str_lifetime_byte(m_lfth, "hard");
343 str_lifetime_byte(m_lfts, "soft");
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));
356 printf("\tsadb_seq=%lu pid=%lu ",
357 (u_long)m->sadb_msg_seq,
358 (u_long)m->sadb_msg_pid);
361 printf("refcnt=%u\n", m->sadb_msg_reserved);
370 char pbuf[NI_MAXSERV];
371 caddr_t mhp[SADB_EXT_MAX + 1];
372 struct sadb_address *m_saddr, *m_daddr;
373 struct sadb_x_policy *m_xpl;
374 struct sadb_lifetime *m_lft = NULL;
378 /* check pfkey message. */
379 if (pfkey_align(m, mhp)) {
380 printf("%s\n", ipsec_strerror());
383 if (pfkey_check(mhp)) {
384 printf("%s\n", ipsec_strerror());
388 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
389 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
390 m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
391 m_lft = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
394 if (m_saddr == NULL) {
395 printf("no ADDRESS_SRC extension.\n");
398 sa = (struct sockaddr *)(m_saddr + 1);
399 switch (sa->sa_family) {
402 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf),
403 NI_NUMERICSERV) != 0)
407 printf("%s%s ", str_ipaddr(sa),
408 str_prefport(sa->sa_family,
409 m_saddr->sadb_address_prefixlen, port));
412 printf("unknown-af ");
416 /* destination address */
417 if (m_daddr == NULL) {
418 printf("no ADDRESS_DST extension.\n");
421 sa = (struct sockaddr *)(m_daddr + 1);
422 switch (sa->sa_family) {
425 if (getnameinfo(sa, sa->sa_len, NULL, 0, pbuf, sizeof(pbuf),
426 NI_NUMERICSERV) != 0)
430 printf("%s%s ", str_ipaddr(sa),
431 str_prefport(sa->sa_family,
432 m_daddr->sadb_address_prefixlen, port));
435 printf("unknown-af ");
439 /* upper layer protocol */
440 if (m_saddr->sadb_address_proto != m_daddr->sadb_address_proto) {
441 printf("upper layer protocol mismatched.\n");
444 if (m_saddr->sadb_address_proto == IPSEC_ULPROTO_ANY)
447 GETMSGSTR(str_upper, m_saddr->sadb_address_proto);
454 printf("no X_POLICY extension.\n");
457 d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
460 printf("\n\t%s\n", d_xpl);
466 printf("\tlifetime:%lu validtime:%lu\n",
467 (u_long)m_lft->sadb_lifetime_addtime,
468 (u_long)m_lft->sadb_lifetime_usetime);
471 printf("\tspid=%ld seq=%ld pid=%ld\n",
472 (u_long)m_xpl->sadb_x_policy_id,
473 (u_long)m->sadb_msg_seq,
474 (u_long)m->sadb_msg_pid);
477 printf("\trefcnt=%u\n", m->sadb_msg_reserved);
483 * set "ipaddress" to buffer.
489 static char buf[NI_MAXHOST];
490 #ifdef NI_WITHSCOPEID
491 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID;
493 const int niflag = NI_NUMERICHOST;
499 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0)
505 * set "/prefix[port number]" to buffer.
508 str_prefport(family, pref, port)
509 u_int family, pref, port;
511 static char buf[128];
518 plen = sizeof(struct in_addr) << 3;
521 plen = sizeof(struct in6_addr) << 3;
530 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
532 if (port == IPSEC_PORT_ANY)
533 snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
535 snprintf(portbuf, sizeof(portbuf), "[%u]", port);
537 snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
543 * set "Mon Day Time Year" to buffer
549 static char buf[128];
553 for (;i < 20;) buf[i++] = ' ';
557 memcpy(buf, t0 + 4, 20);
566 str_lifetime_byte(x, str)
567 struct sadb_lifetime *x;
575 printf("\t%s: 0(bytes)", str);
580 if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
581 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
584 } else if ((x->sadb_lifetime_bytes) / 1024) {
585 y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
589 y = (x->sadb_lifetime_bytes) * 1.0;
594 y = (x->sadb_lifetime_bytes) * 1.0;
598 printf("\t%s: %.*f(%sbytes)", str, w, y, unit);