1 /* $FreeBSD: src/lib/libipsec/pfkey_dump.c,v 1.1.2.4 2003/04/27 00:03:36 sumikawa Exp $ */
2 /* $KAME: pfkey_dump.c,v 1.28 2001/06/27 10:46:51 sakane Exp $ */
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
51 #include "ipsec_strerror.h"
54 /* cope with old kame headers - ugly */
55 #ifndef SADB_X_AALG_MD5
56 #define SADB_X_AALG_MD5 SADB_AALG_MD5
58 #ifndef SADB_X_AALG_SHA
59 #define SADB_X_AALG_SHA SADB_AALG_SHA
61 #ifndef SADB_X_AALG_NULL
62 #define SADB_X_AALG_NULL SADB_AALG_NULL
65 #ifndef SADB_X_EALG_BLOWFISHCBC
66 #define SADB_X_EALG_BLOWFISHCBC SADB_EALG_BLOWFISHCBC
68 #ifndef SADB_X_EALG_CAST128CBC
69 #define SADB_X_EALG_CAST128CBC SADB_EALG_CAST128CBC
71 #ifndef SADB_X_EALG_RC5CBC
72 #ifdef SADB_EALG_RC5CBC
73 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC
77 #define GETMSGSTR(str, num) \
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)); \
85 printf("%s ", (str)[(num)]); \
88 #define GETMSGV2S(v2s, num) \
91 for (p = (v2s); p && p->str; p++) { \
92 if (p->val == (num)) \
96 printf("%s ", p->str); \
98 printf("%d ", (num)); \
101 static char *str_ipaddr (struct sockaddr
*);
102 static char *str_prefport (u_int
, u_int
, u_int
);
103 static char *str_time (time_t);
104 static void str_lifetime_byte (struct sadb_lifetime
*, char *);
112 * Must to be re-written about following strings.
114 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 { SADB_X_AALG_TCP_MD5
, "tcp-md5", },
166 #ifdef SADB_X_AALG_SHA2_256
167 { SADB_X_AALG_SHA2_256
, "hmac-sha2-256", },
169 #ifdef SADB_X_AALG_SHA2_384
170 { SADB_X_AALG_SHA2_384
, "hmac-sha2-384", },
172 #ifdef SADB_X_AALG_SHA2_512
173 { SADB_X_AALG_SHA2_512
, "hmac-sha2-512", },
178 static struct val2str str_alg_enc
[] = {
179 { SADB_EALG_NONE
, "none", },
180 { SADB_EALG_DESCBC
, "des-cbc", },
181 { SADB_EALG_3DESCBC
, "3des-cbc", },
182 { SADB_EALG_NULL
, "null", },
183 #ifdef SADB_X_EALG_RC5CBC
184 { SADB_X_EALG_RC5CBC
, "rc5-cbc", },
186 { SADB_X_EALG_CAST128CBC
, "cast128-cbc", },
187 { SADB_X_EALG_BLOWFISHCBC
, "blowfish-cbc", },
188 #ifdef SADB_X_EALG_RIJNDAELCBC
189 { SADB_X_EALG_RIJNDAELCBC
, "rijndael-cbc", },
191 #ifdef SADB_X_EALG_TWOFISHCBC
192 { SADB_X_EALG_TWOFISHCBC
, "twofish-cbc", },
197 static struct val2str str_alg_comp
[] = {
198 { SADB_X_CALG_NONE
, "none", },
199 { SADB_X_CALG_OUI
, "oui", },
200 { SADB_X_CALG_DEFLATE
, "deflate", },
201 { SADB_X_CALG_LZS
, "lzs", },
206 * dump SADB_MSG formated. For debugging, you should use kdebug_sadb().
209 pfkey_sadump(struct sadb_msg
*m
)
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
;
216 struct sadb_key
*m_auth
, *m_enc
;
218 /* check pfkey message. */
219 if (pfkey_align(m
, mhp
)) {
220 printf("%s\n", ipsec_strerror());
223 if (pfkey_check(mhp
)) {
224 printf("%s\n", ipsec_strerror());
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_auth
= (struct sadb_key
*)mhp
[SADB_EXT_KEY_AUTH
];
236 m_enc
= (struct sadb_key
*)mhp
[SADB_EXT_KEY_ENCRYPT
];
239 if (m_saddr
== NULL
) {
240 printf("no ADDRESS_SRC extension.\n");
243 printf("%s ", str_ipaddr((struct sockaddr
*)(m_saddr
+ 1)));
245 /* destination address */
246 if (m_daddr
== NULL
) {
247 printf("no ADDRESS_DST extension.\n");
250 printf("%s ", str_ipaddr((struct sockaddr
*)(m_daddr
+ 1)));
254 printf("no SA extension.\n");
258 printf("no SA2 extension.\n");
263 GETMSGSTR(str_satype
, m
->sadb_msg_satype
);
266 GETMSGSTR(str_mode
, m_sa2
->sadb_x_sa2_mode
);
268 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
269 (u_int32_t
)ntohl(m_sa
->sadb_sa_spi
),
270 (u_int32_t
)ntohl(m_sa
->sadb_sa_spi
),
271 (u_int32_t
)m_sa2
->sadb_x_sa2_reqid
,
272 (u_int32_t
)m_sa2
->sadb_x_sa2_reqid
);
275 if (m
->sadb_msg_satype
== SADB_X_SATYPE_IPCOMP
) {
277 GETMSGV2S(str_alg_comp
, m_sa
->sadb_sa_encrypt
);
278 } else if (m
->sadb_msg_satype
== SADB_SATYPE_ESP
) {
281 GETMSGV2S(str_alg_enc
, m_sa
->sadb_sa_encrypt
);
282 ipsec_hexdump((caddr_t
)m_enc
+ sizeof(*m_enc
),
283 m_enc
->sadb_key_bits
/ 8);
288 /* authentication key */
289 if (m_auth
!= NULL
) {
291 GETMSGV2S(str_alg_auth
, m_sa
->sadb_sa_auth
);
292 ipsec_hexdump((caddr_t
)m_auth
+ sizeof(*m_auth
),
293 m_auth
->sadb_key_bits
/ 8);
297 /* replay windoe size & flags */
298 printf("\tseq=0x%08x replay=%u flags=0x%08x ",
299 m_sa2
->sadb_x_sa2_sequence
,
300 m_sa
->sadb_sa_replay
,
301 m_sa
->sadb_sa_flags
);
305 GETMSGSTR(str_state
, m_sa
->sadb_sa_state
);
309 if (m_lftc
!= NULL
) {
310 time_t tmp_time
= time(0);
312 printf("\tcreated: %s",
313 str_time(m_lftc
->sadb_lifetime_addtime
));
314 printf("\tcurrent: %s\n", str_time(tmp_time
));
315 printf("\tdiff: %lu(s)",
316 (u_long
)(m_lftc
->sadb_lifetime_addtime
== 0 ?
317 0 : (tmp_time
- m_lftc
->sadb_lifetime_addtime
)));
319 printf("\thard: %lu(s)",
320 (u_long
)(m_lfth
== NULL
?
321 0 : m_lfth
->sadb_lifetime_addtime
));
322 printf("\tsoft: %lu(s)\n",
323 (u_long
)(m_lfts
== NULL
?
324 0 : m_lfts
->sadb_lifetime_addtime
));
327 str_time(m_lftc
->sadb_lifetime_usetime
));
328 printf("\thard: %lu(s)",
329 (u_long
)(m_lfth
== NULL
?
330 0 : m_lfth
->sadb_lifetime_usetime
));
331 printf("\tsoft: %lu(s)\n",
332 (u_long
)(m_lfts
== NULL
?
333 0 : m_lfts
->sadb_lifetime_usetime
));
335 str_lifetime_byte(m_lftc
, "current");
336 str_lifetime_byte(m_lfth
, "hard");
337 str_lifetime_byte(m_lfts
, "soft");
340 printf("\tallocated: %lu",
341 (unsigned long)m_lftc
->sadb_lifetime_allocations
);
342 printf("\thard: %lu",
343 (u_long
)(m_lfth
== NULL
?
344 0 : m_lfth
->sadb_lifetime_allocations
));
345 printf("\tsoft: %lu\n",
346 (u_long
)(m_lfts
== NULL
?
347 0 : m_lfts
->sadb_lifetime_allocations
));
350 printf("\tsadb_seq=%lu pid=%lu ",
351 (u_long
)m
->sadb_msg_seq
,
352 (u_long
)m
->sadb_msg_pid
);
355 printf("refcnt=%u\n", m
->sadb_msg_reserved
);
361 pfkey_spdump(struct sadb_msg
*m
)
363 char pbuf
[NI_MAXSERV
];
364 caddr_t mhp
[SADB_EXT_MAX
+ 1];
365 struct sadb_address
*m_saddr
, *m_daddr
;
366 struct sadb_x_policy
*m_xpl
;
367 struct sadb_lifetime
*m_lft
= NULL
;
371 /* check pfkey message. */
372 if (pfkey_align(m
, mhp
)) {
373 printf("%s\n", ipsec_strerror());
376 if (pfkey_check(mhp
)) {
377 printf("%s\n", ipsec_strerror());
381 m_saddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_SRC
];
382 m_daddr
= (struct sadb_address
*)mhp
[SADB_EXT_ADDRESS_DST
];
383 m_xpl
= (struct sadb_x_policy
*)mhp
[SADB_X_EXT_POLICY
];
384 m_lft
= (struct sadb_lifetime
*)mhp
[SADB_EXT_LIFETIME_HARD
];
387 if (m_saddr
== NULL
) {
388 printf("no ADDRESS_SRC extension.\n");
391 sa
= (struct sockaddr
*)(m_saddr
+ 1);
392 switch (sa
->sa_family
) {
395 if (getnameinfo(sa
, sa
->sa_len
, NULL
, 0, pbuf
, sizeof(pbuf
),
396 NI_NUMERICSERV
) != 0)
400 printf("%s%s ", str_ipaddr(sa
),
401 str_prefport(sa
->sa_family
,
402 m_saddr
->sadb_address_prefixlen
, port
));
405 printf("unknown-af ");
409 /* destination address */
410 if (m_daddr
== NULL
) {
411 printf("no ADDRESS_DST extension.\n");
414 sa
= (struct sockaddr
*)(m_daddr
+ 1);
415 switch (sa
->sa_family
) {
418 if (getnameinfo(sa
, sa
->sa_len
, NULL
, 0, pbuf
, sizeof(pbuf
),
419 NI_NUMERICSERV
) != 0)
423 printf("%s%s ", str_ipaddr(sa
),
424 str_prefport(sa
->sa_family
,
425 m_daddr
->sadb_address_prefixlen
, port
));
428 printf("unknown-af ");
432 /* upper layer protocol */
433 if (m_saddr
->sadb_address_proto
!= m_daddr
->sadb_address_proto
) {
434 printf("upper layer protocol mismatched.\n");
437 if (m_saddr
->sadb_address_proto
== IPSEC_ULPROTO_ANY
)
440 GETMSGSTR(str_upper
, m_saddr
->sadb_address_proto
);
447 printf("no X_POLICY extension.\n");
450 d_xpl
= ipsec_dump_policy((char *)m_xpl
, "\n\t");
453 printf("\n\t%s\n", d_xpl
);
459 printf("\tlifetime:%lu validtime:%lu\n",
460 (u_long
)m_lft
->sadb_lifetime_addtime
,
461 (u_long
)m_lft
->sadb_lifetime_usetime
);
464 printf("\tspid=%ld seq=%ld pid=%ld\n",
465 (u_long
)m_xpl
->sadb_x_policy_id
,
466 (u_long
)m
->sadb_msg_seq
,
467 (u_long
)m
->sadb_msg_pid
);
470 printf("\trefcnt=%u\n", m
->sadb_msg_reserved
);
476 * set "ipaddress" to buffer.
479 str_ipaddr(struct sockaddr
*sa
)
481 static char buf
[NI_MAXHOST
];
482 #ifdef NI_WITHSCOPEID
483 const int niflag
= NI_NUMERICHOST
| NI_WITHSCOPEID
;
485 const int niflag
= NI_NUMERICHOST
;
491 if (getnameinfo(sa
, sa
->sa_len
, buf
, sizeof(buf
), NULL
, 0, niflag
) == 0)
497 * set "/prefix[port number]" to buffer.
500 str_prefport(u_int family
, u_int pref
, u_int port
)
502 static char buf
[128];
509 plen
= sizeof(struct in_addr
) << 3;
512 plen
= sizeof(struct in6_addr
) << 3;
521 snprintf(prefbuf
, sizeof(prefbuf
), "/%u", pref
);
523 if (port
== IPSEC_PORT_ANY
)
524 snprintf(portbuf
, sizeof(portbuf
), "[%s]", "any");
526 snprintf(portbuf
, sizeof(portbuf
), "[%u]", port
);
528 snprintf(buf
, sizeof(buf
), "%s%s", prefbuf
, portbuf
);
534 * set "Mon Day Time Year" to buffer
539 static char buf
[128];
543 for (;i
< 20;) buf
[i
++] = ' ';
547 memcpy(buf
, t0
+ 4, 20);
556 str_lifetime_byte(struct sadb_lifetime
*x
, char *str
)
563 printf("\t%s: 0(bytes)", str
);
568 if ((x
->sadb_lifetime_bytes
) / 1024 / 1024) {
569 y
= (x
->sadb_lifetime_bytes
) * 1.0 / 1024 / 1024;
572 } else if ((x
->sadb_lifetime_bytes
) / 1024) {
573 y
= (x
->sadb_lifetime_bytes
) * 1.0 / 1024;
577 y
= (x
->sadb_lifetime_bytes
) * 1.0;
582 y
= (x
->sadb_lifetime_bytes
) * 1.0;
586 printf("\t%s: %.*f(%sbytes)", str
, w
, y
, unit
);