1 /* $KAME: pfkey_dump.c,v 1.45 2003/09/08 10:14:56 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
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/cdefs.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/socket.h>
39 #include <net/pfkeyv2.h>
40 #include <netipsec/ipsec.h>
41 #include <netipsec/key_var.h>
42 #include <netipsec/key_debug.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
54 #include "ipsec_strerror.h"
57 /* cope with old kame headers - ugly */
58 #ifndef SADB_X_AALG_NULL
59 #define SADB_X_AALG_NULL SADB_AALG_NULL
62 #ifndef SADB_X_EALG_RC5CBC
63 #ifdef SADB_EALG_RC5CBC
64 #define SADB_X_EALG_RC5CBC SADB_EALG_RC5CBC
68 #define GETMSGSTR(str, num) \
70 if (sizeof((str)[0]) == 0 \
71 || num >= sizeof(str)/sizeof((str)[0])) \
72 printf("%u ", (num)); \
73 else if (strlen((str)[(num)]) == 0) \
74 printf("%u ", (num)); \
76 printf("%s ", (str)[(num)]); \
79 #define GETMSGV2S(v2s, num) \
82 for (p = (v2s); p && p->str; p++) { \
83 if (p->val == (num)) \
87 printf("%s ", p->str); \
89 printf("%u ", (num)); \
92 static char *str_ipaddr(struct sockaddr *);
93 static char *str_prefport(u_int, u_int, u_int, u_int);
94 static void str_upperspec(u_int, u_int, u_int);
95 static char *str_time(time_t);
96 static void str_lifetime_byte(struct sadb_lifetime *, char *);
104 * Must to be re-written about following strings.
106 static char *str_satype[] = {
121 static char *str_mode[] = {
127 static char *str_state[] = {
134 static struct val2str str_alg_auth[] = {
135 { SADB_AALG_NONE, "none", },
136 { SADB_AALG_SHA1HMAC, "hmac-sha1", },
137 { SADB_X_AALG_NULL, "null", },
138 { SADB_X_AALG_TCP_MD5, "tcp-md5", },
139 #ifdef SADB_X_AALG_SHA2_256
140 { SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
142 #ifdef SADB_X_AALG_SHA2_384
143 { SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
145 #ifdef SADB_X_AALG_SHA2_512
146 { SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
148 #ifdef SADB_X_AALG_AES_XCBC_MAC
149 { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", },
151 #ifdef SADB_X_AALG_CHACHA20POLY1305
152 { SADB_X_AALG_CHACHA20POLY1305, "chacha20-poly1305", },
157 static struct val2str str_alg_enc[] = {
158 { SADB_EALG_NONE, "none", },
159 { SADB_EALG_NULL, "null", },
160 #ifdef SADB_X_EALG_RC5CBC
161 { SADB_X_EALG_RC5CBC, "rc5-cbc", },
163 #ifdef SADB_X_EALG_AESCBC
164 { SADB_X_EALG_AESCBC, "aes-cbc", },
166 #ifdef SADB_X_EALG_TWOFISHCBC
167 { SADB_X_EALG_TWOFISHCBC, "twofish-cbc", },
169 #ifdef SADB_X_EALG_AESCTR
170 { SADB_X_EALG_AESCTR, "aes-ctr", },
172 #ifdef SADB_X_EALG_AESGCM16
173 { SADB_X_EALG_AESGCM16, "aes-gcm-16", },
175 #ifdef SADB_X_EALG_CHACHA20POLY1305
176 { SADB_X_EALG_CHACHA20POLY1305, "chacha20-poly1305", },
181 static struct val2str str_alg_comp[] = {
182 { SADB_X_CALG_NONE, "none", },
183 { SADB_X_CALG_OUI, "oui", },
184 { SADB_X_CALG_DEFLATE, "deflate", },
185 { SADB_X_CALG_LZS, "lzs", },
189 static struct val2str str_sp_scope[] = {
190 { IPSEC_POLICYSCOPE_GLOBAL, "global" },
191 { IPSEC_POLICYSCOPE_IFNET, "ifnet" },
192 { IPSEC_POLICYSCOPE_PCB, "pcb"},
197 * dump SADB_MSG formatted. For debugging, you should use kdebug_sadb().
200 pfkey_sadump(struct sadb_msg *m)
202 caddr_t mhp[SADB_EXT_MAX + 1];
203 struct sadb_sa *m_sa;
204 struct sadb_x_sa2 *m_sa2;
205 struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
206 struct sadb_address *m_saddr, *m_daddr, *m_paddr;
207 struct sadb_key *m_auth, *m_enc;
208 struct sadb_ident *m_sid, *m_did;
209 struct sadb_sens *m_sens;
210 struct sadb_x_sa_replay *m_sa_replay;
211 struct sadb_x_nat_t_type *natt_type;
212 struct sadb_x_nat_t_port *natt_sport, *natt_dport;
213 struct sadb_address *natt_oai, *natt_oar;
215 /* check pfkey message. */
216 if (pfkey_align(m, mhp)) {
217 printf("%s\n", ipsec_strerror());
220 if (pfkey_check(mhp)) {
221 printf("%s\n", ipsec_strerror());
225 m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
226 m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
227 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
228 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
229 m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
230 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
231 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
232 m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY];
233 m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
234 m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
235 m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
236 m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
237 m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
238 m_sa_replay = (struct sadb_x_sa_replay *)mhp[SADB_X_EXT_SA_REPLAY];
239 natt_type = (struct sadb_x_nat_t_type *)mhp[SADB_X_EXT_NAT_T_TYPE];
240 natt_sport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_SPORT];
241 natt_dport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_DPORT];
242 natt_oai = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAI];
243 natt_oar = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAR];
247 if (m_saddr == NULL) {
248 printf("no ADDRESS_SRC extension.\n");
251 printf("%s", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
252 if (natt_type != NULL && natt_sport != NULL)
253 printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port));
255 /* destination address */
256 if (m_daddr == NULL) {
257 printf("\nno ADDRESS_DST extension.\n");
260 printf(" %s", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
261 if (natt_type != NULL && natt_dport != NULL)
262 printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port));
266 printf("\nno SA extension.\n");
270 printf("\nno SA2 extension.\n");
275 if (m->sadb_msg_satype == SADB_SATYPE_ESP && natt_type != NULL)
278 GETMSGSTR(str_satype, m->sadb_msg_satype);
281 GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
283 printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
284 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
285 (u_int32_t)ntohl(m_sa->sadb_sa_spi),
286 (u_int32_t)m_sa2->sadb_x_sa2_reqid,
287 (u_int32_t)m_sa2->sadb_x_sa2_reqid);
289 /* other NAT-T information */
290 if (natt_type != NULL && (natt_oai != NULL || natt_oar != NULL)) {
292 if (natt_oai != NULL)
294 str_ipaddr((struct sockaddr *)(natt_oai + 1)));
295 if (natt_oar != NULL)
297 str_ipaddr((struct sockaddr *)(natt_oar + 1)));
302 if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
304 GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
305 } else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
308 GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
309 ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
310 m_enc->sadb_key_bits / 8);
315 /* authentication key */
316 if (m_auth != NULL) {
318 GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
319 ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
320 m_auth->sadb_key_bits / 8);
324 /* replay windoe size & flags */
325 printf("\tseq=0x%08x replay=%u flags=0x%08x ",
326 m_sa2->sadb_x_sa2_sequence,
327 m_sa_replay ? (m_sa_replay->sadb_x_sa_replay_replay >> 3) :
328 m_sa->sadb_sa_replay,
329 m_sa->sadb_sa_flags);
333 GETMSGSTR(str_state, m_sa->sadb_sa_state);
337 if (m_lftc != NULL) {
338 time_t tmp_time = time(0);
340 printf("\tcreated: %s",
341 str_time(m_lftc->sadb_lifetime_addtime));
342 printf("\tcurrent: %s\n", str_time(tmp_time));
343 printf("\tdiff: %lu(s)",
344 (u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
345 0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
347 printf("\thard: %lu(s)",
348 (u_long)(m_lfth == NULL ?
349 0 : m_lfth->sadb_lifetime_addtime));
350 printf("\tsoft: %lu(s)\n",
351 (u_long)(m_lfts == NULL ?
352 0 : m_lfts->sadb_lifetime_addtime));
355 str_time(m_lftc->sadb_lifetime_usetime));
356 printf("\thard: %lu(s)",
357 (u_long)(m_lfth == NULL ?
358 0 : m_lfth->sadb_lifetime_usetime));
359 printf("\tsoft: %lu(s)\n",
360 (u_long)(m_lfts == NULL ?
361 0 : m_lfts->sadb_lifetime_usetime));
363 str_lifetime_byte(m_lftc, "current");
364 str_lifetime_byte(m_lfth, "hard");
365 str_lifetime_byte(m_lfts, "soft");
368 printf("\tallocated: %lu",
369 (unsigned long)m_lftc->sadb_lifetime_allocations);
370 printf("\thard: %lu",
371 (u_long)(m_lfth == NULL ?
372 0 : m_lfth->sadb_lifetime_allocations));
373 printf("\tsoft: %lu\n",
374 (u_long)(m_lfts == NULL ?
375 0 : m_lfts->sadb_lifetime_allocations));
378 printf("\tsadb_seq=%lu pid=%lu ",
379 (u_long)m->sadb_msg_seq,
380 (u_long)m->sadb_msg_pid);
383 printf("refcnt=%u\n", m->sadb_msg_reserved);
389 pfkey_spdump(struct sadb_msg *m)
391 char pbuf[NI_MAXSERV];
392 caddr_t mhp[SADB_EXT_MAX + 1];
393 struct sadb_address *m_saddr, *m_daddr;
394 struct sadb_x_policy *m_xpl;
395 struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL;
397 u_int16_t sport = 0, dport = 0;
399 /* check pfkey message. */
400 if (pfkey_align(m, mhp)) {
401 printf("%s\n", ipsec_strerror());
404 if (pfkey_check(mhp)) {
405 printf("%s\n", ipsec_strerror());
409 m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
410 m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
411 m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
412 m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
413 m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
415 if (m_saddr && m_daddr) {
417 sa = (struct sockaddr *)(m_saddr + 1);
418 switch (sa->sa_family) {
421 if (getnameinfo(sa, sa->sa_len, NULL, 0,
422 pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
426 printf("%s%s ", str_ipaddr(sa),
427 str_prefport(sa->sa_family,
428 m_saddr->sadb_address_prefixlen, sport,
429 m_saddr->sadb_address_proto));
432 printf("unknown-af ");
436 /* destination address */
437 sa = (struct sockaddr *)(m_daddr + 1);
438 switch (sa->sa_family) {
441 if (getnameinfo(sa, sa->sa_len, NULL, 0,
442 pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
446 printf("%s%s ", str_ipaddr(sa),
447 str_prefport(sa->sa_family,
448 m_daddr->sadb_address_prefixlen, dport,
449 m_saddr->sadb_address_proto));
452 printf("unknown-af ");
456 /* upper layer protocol */
457 if (m_saddr->sadb_address_proto !=
458 m_daddr->sadb_address_proto) {
459 printf("upper layer protocol mismatched.\n");
462 str_upperspec(m_saddr->sadb_address_proto, sport, dport);
465 printf("(no selector, probably per-socket policy) ");
472 printf("no X_POLICY extension.\n");
475 d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
478 printf("\n\t%s\n", d_xpl);
484 printf("\tcreated: %s ",
485 str_time(m_lftc->sadb_lifetime_addtime));
486 printf("lastused: %s\n",
487 str_time(m_lftc->sadb_lifetime_usetime));
490 printf("\tlifetime: %lu(s) ",
491 (u_long)m_lfth->sadb_lifetime_addtime);
492 printf("validtime: %lu(s)\n",
493 (u_long)m_lfth->sadb_lifetime_usetime);
497 printf("\tspid=%ld seq=%ld pid=%ld scope=",
498 (u_long)m_xpl->sadb_x_policy_id,
499 (u_long)m->sadb_msg_seq,
500 (u_long)m->sadb_msg_pid);
501 GETMSGV2S(str_sp_scope, m_xpl->sadb_x_policy_scope);
502 if (m_xpl->sadb_x_policy_scope == IPSEC_POLICYSCOPE_IFNET &&
503 if_indextoname(m_xpl->sadb_x_policy_ifindex, pbuf) != NULL)
504 printf("ifname=%s", pbuf);
508 printf("\trefcnt=%u\n", m->sadb_msg_reserved);
514 * set "ipaddress" to buffer.
517 str_ipaddr(struct sockaddr *sa)
519 static char buf[NI_MAXHOST];
520 const int niflag = NI_NUMERICHOST;
525 if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0)
531 * set "/prefix[port number]" to buffer.
534 str_prefport(u_int family, u_int pref, u_int port, u_int ulp)
536 static char buf[128];
543 plen = sizeof(struct in_addr) << 3;
546 plen = sizeof(struct in6_addr) << 3;
555 snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
557 if (ulp == IPPROTO_ICMPV6)
558 memset(portbuf, 0, sizeof(portbuf));
560 if (port == IPSEC_PORT_ANY)
561 snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
563 snprintf(portbuf, sizeof(portbuf), "[%u]", port);
566 snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
572 str_upperspec(u_int ulp, u_int p1, u_int p2)
574 if (ulp == IPSEC_ULPROTO_ANY)
576 else if (ulp == IPPROTO_ICMPV6) {
578 if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY))
579 printf(" %u,%u", p1, p2);
581 struct protoent *ent;
588 ent = getprotobynumber(ulp);
590 printf("%s", ent->p_name);
601 * set "Mon Day Time Year" to buffer
606 static char buf[128];
610 for (;i < 20;) buf[i++] = ' ';
614 memcpy(buf, t0 + 4, 20);
623 str_lifetime_byte(struct sadb_lifetime *x, char *str)
630 printf("\t%s: 0(bytes)", str);
635 if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
636 y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
639 } else if ((x->sadb_lifetime_bytes) / 1024) {
640 y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
644 y = (x->sadb_lifetime_bytes) * 1.0;
649 y = (x->sadb_lifetime_bytes) * 1.0;
653 printf("\t%s: %.*f(%sbytes)", str, w, y, unit);