xref: /freebsd/lib/libipsec/pfkey_dump.c (revision 1b1cd327d905fab5920e7af2582d15d48ad26999)
1bd9f52d5SHajimu UMEMOTO /*	$KAME: pfkey_dump.c,v 1.45 2003/09/08 10:14:56 itojun Exp $	*/
23c62e87aSJun-ichiro itojun Hagino 
38a16b7a1SPedro F. Giffuni /*-
48a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
58a16b7a1SPedro F. Giffuni  *
69a4365d0SYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
79a4365d0SYoshinobu Inoue  * All rights reserved.
89a4365d0SYoshinobu Inoue  *
99a4365d0SYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
109a4365d0SYoshinobu Inoue  * modification, are permitted provided that the following conditions
119a4365d0SYoshinobu Inoue  * are met:
129a4365d0SYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
139a4365d0SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
149a4365d0SYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
159a4365d0SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
169a4365d0SYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
179a4365d0SYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
189a4365d0SYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
199a4365d0SYoshinobu Inoue  *    without specific prior written permission.
209a4365d0SYoshinobu Inoue  *
219a4365d0SYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
229a4365d0SYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239a4365d0SYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249a4365d0SYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
259a4365d0SYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269a4365d0SYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279a4365d0SYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289a4365d0SYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299a4365d0SYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309a4365d0SYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319a4365d0SYoshinobu Inoue  * SUCH DAMAGE.
329a4365d0SYoshinobu Inoue  */
339a4365d0SYoshinobu Inoue 
349a4365d0SYoshinobu Inoue #include <sys/types.h>
359a4365d0SYoshinobu Inoue #include <sys/param.h>
369a4365d0SYoshinobu Inoue #include <sys/socket.h>
3722986c67SAndrey V. Elsukov #include <net/if.h>
389a4365d0SYoshinobu Inoue #include <net/pfkeyv2.h>
3922986c67SAndrey V. Elsukov #include <netipsec/ipsec.h>
408409aedfSGeorge V. Neville-Neil #include <netipsec/key_var.h>
418409aedfSGeorge V. Neville-Neil #include <netipsec/key_debug.h>
429a4365d0SYoshinobu Inoue 
439a4365d0SYoshinobu Inoue #include <netinet/in.h>
449a4365d0SYoshinobu Inoue #include <arpa/inet.h>
459a4365d0SYoshinobu Inoue 
469a4365d0SYoshinobu Inoue #include <stdlib.h>
479a4365d0SYoshinobu Inoue #include <unistd.h>
489a4365d0SYoshinobu Inoue #include <stdio.h>
499a4365d0SYoshinobu Inoue #include <string.h>
509a4365d0SYoshinobu Inoue #include <time.h>
513c62e87aSJun-ichiro itojun Hagino #include <netdb.h>
529a4365d0SYoshinobu Inoue 
539a4365d0SYoshinobu Inoue #include "ipsec_strerror.h"
543c62e87aSJun-ichiro itojun Hagino #include "libpfkey.h"
559a4365d0SYoshinobu Inoue 
5633841545SHajimu UMEMOTO /* cope with old kame headers - ugly */
5733841545SHajimu UMEMOTO #ifndef SADB_X_AALG_NULL
5833841545SHajimu UMEMOTO #define SADB_X_AALG_NULL	SADB_AALG_NULL
5933841545SHajimu UMEMOTO #endif
6033841545SHajimu UMEMOTO 
6133841545SHajimu UMEMOTO #ifndef SADB_X_EALG_RC5CBC
6233841545SHajimu UMEMOTO #ifdef SADB_EALG_RC5CBC
6333841545SHajimu UMEMOTO #define SADB_X_EALG_RC5CBC	SADB_EALG_RC5CBC
6433841545SHajimu UMEMOTO #endif
6533841545SHajimu UMEMOTO #endif
6633841545SHajimu UMEMOTO 
679a4365d0SYoshinobu Inoue #define GETMSGSTR(str, num) \
683c62e87aSJun-ichiro itojun Hagino do { \
699a4365d0SYoshinobu Inoue 	if (sizeof((str)[0]) == 0 \
709a4365d0SYoshinobu Inoue 	 || num >= sizeof(str)/sizeof((str)[0])) \
719713f5c1SHajimu UMEMOTO 		printf("%u ", (num)); \
729a4365d0SYoshinobu Inoue 	else if (strlen((str)[(num)]) == 0) \
739713f5c1SHajimu UMEMOTO 		printf("%u ", (num)); \
749a4365d0SYoshinobu Inoue 	else \
759a4365d0SYoshinobu Inoue 		printf("%s ", (str)[(num)]); \
763c62e87aSJun-ichiro itojun Hagino } while (0)
779a4365d0SYoshinobu Inoue 
7833841545SHajimu UMEMOTO #define GETMSGV2S(v2s, num) \
7933841545SHajimu UMEMOTO do { \
8033841545SHajimu UMEMOTO 	struct val2str *p;  \
8133841545SHajimu UMEMOTO 	for (p = (v2s); p && p->str; p++) { \
8233841545SHajimu UMEMOTO 		if (p->val == (num)) \
8333841545SHajimu UMEMOTO 			break; \
8433841545SHajimu UMEMOTO 	} \
8533841545SHajimu UMEMOTO 	if (p && p->str) \
8633841545SHajimu UMEMOTO 		printf("%s ", p->str); \
8733841545SHajimu UMEMOTO 	else \
889713f5c1SHajimu UMEMOTO 		printf("%u ", (num)); \
8933841545SHajimu UMEMOTO } while (0)
9033841545SHajimu UMEMOTO 
9169160b1eSDavid E. O'Brien static char *str_ipaddr(struct sockaddr *);
92bd9f52d5SHajimu UMEMOTO static char *str_prefport(u_int, u_int, u_int, u_int);
93bd9f52d5SHajimu UMEMOTO static void str_upperspec(u_int, u_int, u_int);
9469160b1eSDavid E. O'Brien static char *str_time(time_t);
9569160b1eSDavid E. O'Brien static void str_lifetime_byte(struct sadb_lifetime *, char *);
969a4365d0SYoshinobu Inoue 
9733841545SHajimu UMEMOTO struct val2str {
9833841545SHajimu UMEMOTO 	int val;
9933841545SHajimu UMEMOTO 	const char *str;
10033841545SHajimu UMEMOTO };
10133841545SHajimu UMEMOTO 
1029a4365d0SYoshinobu Inoue /*
1039a4365d0SYoshinobu Inoue  * Must to be re-written about following strings.
1049a4365d0SYoshinobu Inoue  */
10533841545SHajimu UMEMOTO static char *str_satype[] = {
1069a4365d0SYoshinobu Inoue 	"unspec",
1079a4365d0SYoshinobu Inoue 	"unknown",
1089a4365d0SYoshinobu Inoue 	"ah",
1099a4365d0SYoshinobu Inoue 	"esp",
1109a4365d0SYoshinobu Inoue 	"unknown",
1119a4365d0SYoshinobu Inoue 	"rsvp",
1129a4365d0SYoshinobu Inoue 	"ospfv2",
1139a4365d0SYoshinobu Inoue 	"ripv2",
1149a4365d0SYoshinobu Inoue 	"mip",
1159a4365d0SYoshinobu Inoue 	"ipcomp",
1161922fd12SBruce M Simpson 	"policy",
1171922fd12SBruce M Simpson 	"tcp"
1189a4365d0SYoshinobu Inoue };
1199a4365d0SYoshinobu Inoue 
12033841545SHajimu UMEMOTO static char *str_mode[] = {
1219a4365d0SYoshinobu Inoue 	"any",
1229a4365d0SYoshinobu Inoue 	"transport",
1239a4365d0SYoshinobu Inoue 	"tunnel",
1249a4365d0SYoshinobu Inoue };
1259a4365d0SYoshinobu Inoue 
12633841545SHajimu UMEMOTO static char *str_state[] = {
1279a4365d0SYoshinobu Inoue 	"larval",
1289a4365d0SYoshinobu Inoue 	"mature",
1299a4365d0SYoshinobu Inoue 	"dying",
1309a4365d0SYoshinobu Inoue 	"dead",
1319a4365d0SYoshinobu Inoue };
1329a4365d0SYoshinobu Inoue 
13333841545SHajimu UMEMOTO static struct val2str str_alg_auth[] = {
13433841545SHajimu UMEMOTO 	{ SADB_AALG_NONE, "none", },
13533841545SHajimu UMEMOTO 	{ SADB_AALG_SHA1HMAC, "hmac-sha1", },
13633841545SHajimu UMEMOTO 	{ SADB_X_AALG_NULL, "null", },
1371922fd12SBruce M Simpson 	{ SADB_X_AALG_TCP_MD5, "tcp-md5", },
13833841545SHajimu UMEMOTO #ifdef SADB_X_AALG_SHA2_256
13933841545SHajimu UMEMOTO 	{ SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
14033841545SHajimu UMEMOTO #endif
14133841545SHajimu UMEMOTO #ifdef SADB_X_AALG_SHA2_384
14233841545SHajimu UMEMOTO 	{ SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
14333841545SHajimu UMEMOTO #endif
14433841545SHajimu UMEMOTO #ifdef SADB_X_AALG_SHA2_512
14533841545SHajimu UMEMOTO 	{ SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
14633841545SHajimu UMEMOTO #endif
147c65ee7c7SHajimu UMEMOTO #ifdef SADB_X_AALG_AES_XCBC_MAC
148c65ee7c7SHajimu UMEMOTO 	{ SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", },
149c65ee7c7SHajimu UMEMOTO #endif
1509f8f3a8eSKristof Provost #ifdef SADB_X_AALG_CHACHA20POLY1305
1519f8f3a8eSKristof Provost 	{ SADB_X_AALG_CHACHA20POLY1305, "chacha20-poly1305", },
1529f8f3a8eSKristof Provost #endif
15333841545SHajimu UMEMOTO 	{ -1, NULL, },
1549a4365d0SYoshinobu Inoue };
1559a4365d0SYoshinobu Inoue 
15633841545SHajimu UMEMOTO static struct val2str str_alg_enc[] = {
15733841545SHajimu UMEMOTO 	{ SADB_EALG_NONE, "none", },
15833841545SHajimu UMEMOTO 	{ SADB_EALG_NULL, "null", },
15933841545SHajimu UMEMOTO #ifdef SADB_X_EALG_RC5CBC
16033841545SHajimu UMEMOTO 	{ SADB_X_EALG_RC5CBC, "rc5-cbc", },
16133841545SHajimu UMEMOTO #endif
16200a4311aSJohn Baldwin #ifdef SADB_X_EALG_AESCBC
16300a4311aSJohn Baldwin 	{ SADB_X_EALG_AESCBC, "aes-cbc", },
16433841545SHajimu UMEMOTO #endif
16533841545SHajimu UMEMOTO #ifdef SADB_X_EALG_TWOFISHCBC
16633841545SHajimu UMEMOTO 	{ SADB_X_EALG_TWOFISHCBC, "twofish-cbc", },
16733841545SHajimu UMEMOTO #endif
168b42ac57fSHajimu UMEMOTO #ifdef SADB_X_EALG_AESCTR
169b42ac57fSHajimu UMEMOTO 	{ SADB_X_EALG_AESCTR, "aes-ctr", },
170b42ac57fSHajimu UMEMOTO #endif
171987de844SGeorge V. Neville-Neil #ifdef SADB_X_EALG_AESGCM16
172987de844SGeorge V. Neville-Neil 	{ SADB_X_EALG_AESGCM16, "aes-gcm-16", },
173987de844SGeorge V. Neville-Neil #endif
1749f8f3a8eSKristof Provost #ifdef SADB_X_EALG_CHACHA20POLY1305
1759f8f3a8eSKristof Provost 	{ SADB_X_EALG_CHACHA20POLY1305, "chacha20-poly1305", },
1769f8f3a8eSKristof Provost #endif
17733841545SHajimu UMEMOTO 	{ -1, NULL, },
1789a4365d0SYoshinobu Inoue };
1799a4365d0SYoshinobu Inoue 
18033841545SHajimu UMEMOTO static struct val2str str_alg_comp[] = {
18133841545SHajimu UMEMOTO 	{ SADB_X_CALG_NONE, "none", },
18233841545SHajimu UMEMOTO 	{ SADB_X_CALG_OUI, "oui", },
18333841545SHajimu UMEMOTO 	{ SADB_X_CALG_DEFLATE, "deflate", },
18433841545SHajimu UMEMOTO 	{ SADB_X_CALG_LZS, "lzs", },
18533841545SHajimu UMEMOTO 	{ -1, NULL, },
1869a4365d0SYoshinobu Inoue };
1879a4365d0SYoshinobu Inoue 
18822986c67SAndrey V. Elsukov static struct val2str str_sp_scope[] = {
18922986c67SAndrey V. Elsukov 	{ IPSEC_POLICYSCOPE_GLOBAL, "global" },
19022986c67SAndrey V. Elsukov 	{ IPSEC_POLICYSCOPE_IFNET, "ifnet" },
19122986c67SAndrey V. Elsukov 	{ IPSEC_POLICYSCOPE_PCB, "pcb"},
19222986c67SAndrey V. Elsukov 	{ -1, NULL },
19322986c67SAndrey V. Elsukov };
19422986c67SAndrey V. Elsukov 
1959a4365d0SYoshinobu Inoue /*
1967a33c6bfSGordon Bergling  * dump SADB_MSG formatted.  For debugging, you should use kdebug_sadb().
1979a4365d0SYoshinobu Inoue  */
1989a4365d0SYoshinobu Inoue void
199650d6cc1SKonstantin Belousov pfkey_sadump(struct sadb_msg *m)
2009a4365d0SYoshinobu Inoue {
2019a4365d0SYoshinobu Inoue 	caddr_t mhp[SADB_EXT_MAX + 1];
2029a4365d0SYoshinobu Inoue 	struct sadb_sa *m_sa;
2033c62e87aSJun-ichiro itojun Hagino 	struct sadb_x_sa2 *m_sa2;
204*1b1cd327SKonstantin Belousov 	struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts, *m_lft_sw, *m_lft_hw;
2059a4365d0SYoshinobu Inoue 	struct sadb_address *m_saddr, *m_daddr, *m_paddr;
2069a4365d0SYoshinobu Inoue 	struct sadb_key *m_auth, *m_enc;
2079a4365d0SYoshinobu Inoue 	struct sadb_ident *m_sid, *m_did;
2089a4365d0SYoshinobu Inoue 	struct sadb_sens *m_sens;
209bf435626SFabien Thomas 	struct sadb_x_sa_replay *m_sa_replay;
210fcf59617SAndrey V. Elsukov 	struct sadb_x_nat_t_type *natt_type;
211fcf59617SAndrey V. Elsukov 	struct sadb_x_nat_t_port *natt_sport, *natt_dport;
212fcf59617SAndrey V. Elsukov 	struct sadb_address *natt_oai, *natt_oar;
2139a4365d0SYoshinobu Inoue 
2149a4365d0SYoshinobu Inoue 	/* check pfkey message. */
2159a4365d0SYoshinobu Inoue 	if (pfkey_align(m, mhp)) {
2169a4365d0SYoshinobu Inoue 		printf("%s\n", ipsec_strerror());
2179a4365d0SYoshinobu Inoue 		return;
2189a4365d0SYoshinobu Inoue 	}
2199a4365d0SYoshinobu Inoue 	if (pfkey_check(mhp)) {
2209a4365d0SYoshinobu Inoue 		printf("%s\n", ipsec_strerror());
2219a4365d0SYoshinobu Inoue 		return;
2229a4365d0SYoshinobu Inoue 	}
2239a4365d0SYoshinobu Inoue 
2249a4365d0SYoshinobu Inoue 	m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
2253c62e87aSJun-ichiro itojun Hagino 	m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
2269a4365d0SYoshinobu Inoue 	m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
2279a4365d0SYoshinobu Inoue 	m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
2289a4365d0SYoshinobu Inoue 	m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
2299a4365d0SYoshinobu Inoue 	m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
2309a4365d0SYoshinobu Inoue 	m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
2319a4365d0SYoshinobu Inoue 	m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY];
2329a4365d0SYoshinobu Inoue 	m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
2339a4365d0SYoshinobu Inoue 	m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
2349a4365d0SYoshinobu Inoue 	m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
2353c62e87aSJun-ichiro itojun Hagino 	m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
2369a4365d0SYoshinobu Inoue 	m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
237bf435626SFabien Thomas 	m_sa_replay = (struct sadb_x_sa_replay *)mhp[SADB_X_EXT_SA_REPLAY];
238fcf59617SAndrey V. Elsukov 	natt_type = (struct sadb_x_nat_t_type *)mhp[SADB_X_EXT_NAT_T_TYPE];
239fcf59617SAndrey V. Elsukov 	natt_sport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_SPORT];
240fcf59617SAndrey V. Elsukov 	natt_dport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_DPORT];
241fcf59617SAndrey V. Elsukov 	natt_oai = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAI];
242fcf59617SAndrey V. Elsukov 	natt_oar = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAR];
243*1b1cd327SKonstantin Belousov 	m_lft_sw = (struct sadb_lifetime *)mhp[SADB_X_EXT_LFT_CUR_SW_OFFL];
244*1b1cd327SKonstantin Belousov 	m_lft_hw = (struct sadb_lifetime *)mhp[SADB_X_EXT_LFT_CUR_HW_OFFL];
2459a4365d0SYoshinobu Inoue 
2469a4365d0SYoshinobu Inoue 	/* source address */
2479a4365d0SYoshinobu Inoue 	if (m_saddr == NULL) {
2489a4365d0SYoshinobu Inoue 		printf("no ADDRESS_SRC extension.\n");
2499a4365d0SYoshinobu Inoue 		return;
2509a4365d0SYoshinobu Inoue 	}
2513c62e87aSJun-ichiro itojun Hagino 	printf("%s", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
252fcf59617SAndrey V. Elsukov 	if (natt_type != NULL && natt_sport != NULL)
253fcf59617SAndrey V. Elsukov 		printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port));
2549a4365d0SYoshinobu Inoue 
2559a4365d0SYoshinobu Inoue 	/* destination address */
2569a4365d0SYoshinobu Inoue 	if (m_daddr == NULL) {
257fcf59617SAndrey V. Elsukov 		printf("\nno ADDRESS_DST extension.\n");
2589a4365d0SYoshinobu Inoue 		return;
2599a4365d0SYoshinobu Inoue 	}
2603c62e87aSJun-ichiro itojun Hagino 	printf(" %s", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
261fcf59617SAndrey V. Elsukov 	if (natt_type != NULL && natt_dport != NULL)
262fcf59617SAndrey V. Elsukov 		printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port));
2639a4365d0SYoshinobu Inoue 
2649a4365d0SYoshinobu Inoue 	/* SA type */
2659a4365d0SYoshinobu Inoue 	if (m_sa == NULL) {
266fcf59617SAndrey V. Elsukov 		printf("\nno SA extension.\n");
2679a4365d0SYoshinobu Inoue 		return;
2689a4365d0SYoshinobu Inoue 	}
2693c62e87aSJun-ichiro itojun Hagino 	if (m_sa2 == NULL) {
270fcf59617SAndrey V. Elsukov 		printf("\nno SA2 extension.\n");
2713c62e87aSJun-ichiro itojun Hagino 		return;
2723c62e87aSJun-ichiro itojun Hagino 	}
2739a4365d0SYoshinobu Inoue 	printf("\n\t");
2749a4365d0SYoshinobu Inoue 
275fcf59617SAndrey V. Elsukov 	if (m->sadb_msg_satype == SADB_SATYPE_ESP && natt_type != NULL)
276fcf59617SAndrey V. Elsukov 		printf("esp-udp ");
277fcf59617SAndrey V. Elsukov 	else
27833841545SHajimu UMEMOTO 		GETMSGSTR(str_satype, m->sadb_msg_satype);
2799a4365d0SYoshinobu Inoue 
2809a4365d0SYoshinobu Inoue 	printf("mode=");
28133841545SHajimu UMEMOTO 	GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
2829a4365d0SYoshinobu Inoue 
2833c62e87aSJun-ichiro itojun Hagino 	printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
2849a4365d0SYoshinobu Inoue 		(u_int32_t)ntohl(m_sa->sadb_sa_spi),
2859a4365d0SYoshinobu Inoue 		(u_int32_t)ntohl(m_sa->sadb_sa_spi),
2863c62e87aSJun-ichiro itojun Hagino 		(u_int32_t)m_sa2->sadb_x_sa2_reqid,
2873c62e87aSJun-ichiro itojun Hagino 		(u_int32_t)m_sa2->sadb_x_sa2_reqid);
2889a4365d0SYoshinobu Inoue 
289fcf59617SAndrey V. Elsukov 	/* other NAT-T information */
290fcf59617SAndrey V. Elsukov 	if (natt_type != NULL && (natt_oai != NULL || natt_oar != NULL)) {
291fcf59617SAndrey V. Elsukov 		printf("\tNAT:");
292fcf59617SAndrey V. Elsukov 		if (natt_oai != NULL)
293fcf59617SAndrey V. Elsukov 			printf(" OAI=%s",
294fcf59617SAndrey V. Elsukov 			    str_ipaddr((struct sockaddr *)(natt_oai + 1)));
295fcf59617SAndrey V. Elsukov 		if (natt_oar != NULL)
296fcf59617SAndrey V. Elsukov 			printf(" OAR=%s",
297fcf59617SAndrey V. Elsukov 			    str_ipaddr((struct sockaddr *)(natt_oar + 1)));
298fcf59617SAndrey V. Elsukov 		printf("\n");
299fcf59617SAndrey V. Elsukov 	}
300fcf59617SAndrey V. Elsukov 
3019a4365d0SYoshinobu Inoue 	/* encryption key */
3029a4365d0SYoshinobu Inoue 	if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
3039a4365d0SYoshinobu Inoue 		printf("\tC: ");
30433841545SHajimu UMEMOTO 		GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
3059a4365d0SYoshinobu Inoue 	} else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
3069a4365d0SYoshinobu Inoue 		if (m_enc != NULL) {
3079a4365d0SYoshinobu Inoue 			printf("\tE: ");
30833841545SHajimu UMEMOTO 			GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
3099a4365d0SYoshinobu Inoue 			ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
3109a4365d0SYoshinobu Inoue 				      m_enc->sadb_key_bits / 8);
3119a4365d0SYoshinobu Inoue 			printf("\n");
3129a4365d0SYoshinobu Inoue 		}
3139a4365d0SYoshinobu Inoue 	}
3149a4365d0SYoshinobu Inoue 
3159a4365d0SYoshinobu Inoue 	/* authentication key */
3169a4365d0SYoshinobu Inoue 	if (m_auth != NULL) {
3179a4365d0SYoshinobu Inoue 		printf("\tA: ");
31833841545SHajimu UMEMOTO 		GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
3199a4365d0SYoshinobu Inoue 		ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
3209a4365d0SYoshinobu Inoue 		              m_auth->sadb_key_bits / 8);
3219a4365d0SYoshinobu Inoue 		printf("\n");
3229a4365d0SYoshinobu Inoue 	}
3239a4365d0SYoshinobu Inoue 
3243c62e87aSJun-ichiro itojun Hagino 	/* replay windoe size & flags */
325232bdaf6SHajimu UMEMOTO 	printf("\tseq=0x%08x replay=%u flags=0x%08x ",
326232bdaf6SHajimu UMEMOTO 		m_sa2->sadb_x_sa2_sequence,
327bf435626SFabien Thomas 		m_sa_replay ? (m_sa_replay->sadb_x_sa_replay_replay >> 3) :
3283c62e87aSJun-ichiro itojun Hagino 			m_sa->sadb_sa_replay,
3293c62e87aSJun-ichiro itojun Hagino 		m_sa->sadb_sa_flags);
3303c62e87aSJun-ichiro itojun Hagino 
3319a4365d0SYoshinobu Inoue 	/* state */
3323c62e87aSJun-ichiro itojun Hagino 	printf("state=");
33333841545SHajimu UMEMOTO 	GETMSGSTR(str_state, m_sa->sadb_sa_state);
334232bdaf6SHajimu UMEMOTO 	printf("\n");
3359a4365d0SYoshinobu Inoue 
3369a4365d0SYoshinobu Inoue 	/* lifetime */
3379a4365d0SYoshinobu Inoue 	if (m_lftc != NULL) {
3389a4365d0SYoshinobu Inoue 		time_t tmp_time = time(0);
3399a4365d0SYoshinobu Inoue 
3409a4365d0SYoshinobu Inoue 		printf("\tcreated: %s",
3413c62e87aSJun-ichiro itojun Hagino 			str_time(m_lftc->sadb_lifetime_addtime));
3423c62e87aSJun-ichiro itojun Hagino 		printf("\tcurrent: %s\n", str_time(tmp_time));
3439a4365d0SYoshinobu Inoue 		printf("\tdiff: %lu(s)",
3449a4365d0SYoshinobu Inoue 			(u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
3459a4365d0SYoshinobu Inoue 			0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
3469a4365d0SYoshinobu Inoue 
3479a4365d0SYoshinobu Inoue 		printf("\thard: %lu(s)",
3489a4365d0SYoshinobu Inoue 			(u_long)(m_lfth == NULL ?
3499a4365d0SYoshinobu Inoue 			0 : m_lfth->sadb_lifetime_addtime));
3509a4365d0SYoshinobu Inoue 		printf("\tsoft: %lu(s)\n",
3519a4365d0SYoshinobu Inoue 			(u_long)(m_lfts == NULL ?
3529a4365d0SYoshinobu Inoue 			0 : m_lfts->sadb_lifetime_addtime));
3539a4365d0SYoshinobu Inoue 
3549a4365d0SYoshinobu Inoue 		printf("\tlast: %s",
3553c62e87aSJun-ichiro itojun Hagino 			str_time(m_lftc->sadb_lifetime_usetime));
3569a4365d0SYoshinobu Inoue 		printf("\thard: %lu(s)",
3579a4365d0SYoshinobu Inoue 			(u_long)(m_lfth == NULL ?
3589a4365d0SYoshinobu Inoue 			0 : m_lfth->sadb_lifetime_usetime));
3599a4365d0SYoshinobu Inoue 		printf("\tsoft: %lu(s)\n",
3609a4365d0SYoshinobu Inoue 			(u_long)(m_lfts == NULL ?
3619a4365d0SYoshinobu Inoue 			0 : m_lfts->sadb_lifetime_usetime));
3629a4365d0SYoshinobu Inoue 
3633c62e87aSJun-ichiro itojun Hagino 		str_lifetime_byte(m_lftc, "current");
3643c62e87aSJun-ichiro itojun Hagino 		str_lifetime_byte(m_lfth, "hard");
3653c62e87aSJun-ichiro itojun Hagino 		str_lifetime_byte(m_lfts, "soft");
3669a4365d0SYoshinobu Inoue 		printf("\n");
3679a4365d0SYoshinobu Inoue 
3689a4365d0SYoshinobu Inoue 		printf("\tallocated: %lu",
3699a4365d0SYoshinobu Inoue 			(unsigned long)m_lftc->sadb_lifetime_allocations);
3709a4365d0SYoshinobu Inoue 		printf("\thard: %lu",
3719a4365d0SYoshinobu Inoue 			(u_long)(m_lfth == NULL ?
3729a4365d0SYoshinobu Inoue 			0 : m_lfth->sadb_lifetime_allocations));
3739a4365d0SYoshinobu Inoue 		printf("\tsoft: %lu\n",
3749a4365d0SYoshinobu Inoue 			(u_long)(m_lfts == NULL ?
3759a4365d0SYoshinobu Inoue 			0 : m_lfts->sadb_lifetime_allocations));
3769a4365d0SYoshinobu Inoue 	}
3779a4365d0SYoshinobu Inoue 
378232bdaf6SHajimu UMEMOTO 	printf("\tsadb_seq=%lu pid=%lu ",
379232bdaf6SHajimu UMEMOTO 		(u_long)m->sadb_msg_seq,
380232bdaf6SHajimu UMEMOTO 		(u_long)m->sadb_msg_pid);
381232bdaf6SHajimu UMEMOTO 
3829a4365d0SYoshinobu Inoue 	/* XXX DEBUG */
383232bdaf6SHajimu UMEMOTO 	printf("refcnt=%u\n", m->sadb_msg_reserved);
3849a4365d0SYoshinobu Inoue 
385*1b1cd327SKonstantin Belousov 	if (m_lft_sw != NULL) {
386*1b1cd327SKonstantin Belousov 		printf("\tsw offl use: %s",
387*1b1cd327SKonstantin Belousov 		    str_time(m_lft_sw->sadb_lifetime_usetime));
388*1b1cd327SKonstantin Belousov 		printf("\tsw offl allocated: %lu",
389*1b1cd327SKonstantin Belousov 		    (unsigned long)m_lft_sw->sadb_lifetime_allocations);
390*1b1cd327SKonstantin Belousov 		str_lifetime_byte(m_lft_sw, "sw offl");
391*1b1cd327SKonstantin Belousov 		printf("\n");
392*1b1cd327SKonstantin Belousov 	}
393*1b1cd327SKonstantin Belousov 
394*1b1cd327SKonstantin Belousov 	if (m_lft_hw != NULL) {
395*1b1cd327SKonstantin Belousov 		printf("\thw offl use: %s",
396*1b1cd327SKonstantin Belousov 		    str_time(m_lft_hw->sadb_lifetime_usetime));
397*1b1cd327SKonstantin Belousov 		printf("\thw offl allocated: %lu",
398*1b1cd327SKonstantin Belousov 		    (unsigned long)m_lft_hw->sadb_lifetime_allocations);
399*1b1cd327SKonstantin Belousov 		str_lifetime_byte(m_lft_hw, "hw offl");
400*1b1cd327SKonstantin Belousov 		printf("\n");
401*1b1cd327SKonstantin Belousov 	}
4029a4365d0SYoshinobu Inoue }
4039a4365d0SYoshinobu Inoue 
4049a4365d0SYoshinobu Inoue void
40522986c67SAndrey V. Elsukov pfkey_spdump(struct sadb_msg *m)
4069a4365d0SYoshinobu Inoue {
4073c62e87aSJun-ichiro itojun Hagino 	char pbuf[NI_MAXSERV];
4089a4365d0SYoshinobu Inoue 	caddr_t mhp[SADB_EXT_MAX + 1];
4099a4365d0SYoshinobu Inoue 	struct sadb_address *m_saddr, *m_daddr;
4109a4365d0SYoshinobu Inoue 	struct sadb_x_policy *m_xpl;
411bd9f52d5SHajimu UMEMOTO 	struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL;
4123c62e87aSJun-ichiro itojun Hagino 	struct sockaddr *sa;
413bd9f52d5SHajimu UMEMOTO 	u_int16_t sport = 0, dport = 0;
4149a4365d0SYoshinobu Inoue 
4159a4365d0SYoshinobu Inoue 	/* check pfkey message. */
4169a4365d0SYoshinobu Inoue 	if (pfkey_align(m, mhp)) {
4179a4365d0SYoshinobu Inoue 		printf("%s\n", ipsec_strerror());
4189a4365d0SYoshinobu Inoue 		return;
4199a4365d0SYoshinobu Inoue 	}
4209a4365d0SYoshinobu Inoue 	if (pfkey_check(mhp)) {
4219a4365d0SYoshinobu Inoue 		printf("%s\n", ipsec_strerror());
4229a4365d0SYoshinobu Inoue 		return;
4239a4365d0SYoshinobu Inoue 	}
4249a4365d0SYoshinobu Inoue 
4259a4365d0SYoshinobu Inoue 	m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
4269a4365d0SYoshinobu Inoue 	m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
4279a4365d0SYoshinobu Inoue 	m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
428bd9f52d5SHajimu UMEMOTO 	m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
429bd9f52d5SHajimu UMEMOTO 	m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
4309a4365d0SYoshinobu Inoue 
431bd9f52d5SHajimu UMEMOTO 	if (m_saddr && m_daddr) {
4329a4365d0SYoshinobu Inoue 		/* source address */
4333c62e87aSJun-ichiro itojun Hagino 		sa = (struct sockaddr *)(m_saddr + 1);
4343c62e87aSJun-ichiro itojun Hagino 		switch (sa->sa_family) {
4353c62e87aSJun-ichiro itojun Hagino 		case AF_INET:
4363c62e87aSJun-ichiro itojun Hagino 		case AF_INET6:
437bd9f52d5SHajimu UMEMOTO 			if (getnameinfo(sa, sa->sa_len, NULL, 0,
438bd9f52d5SHajimu UMEMOTO 			    pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
439bd9f52d5SHajimu UMEMOTO 				sport = 0;	/*XXX*/
4403c62e87aSJun-ichiro itojun Hagino 			else
441bd9f52d5SHajimu UMEMOTO 				sport = atoi(pbuf);
4423c62e87aSJun-ichiro itojun Hagino 			printf("%s%s ", str_ipaddr(sa),
4433c62e87aSJun-ichiro itojun Hagino 				str_prefport(sa->sa_family,
444bd9f52d5SHajimu UMEMOTO 				    m_saddr->sadb_address_prefixlen, sport,
445bd9f52d5SHajimu UMEMOTO 				    m_saddr->sadb_address_proto));
4463c62e87aSJun-ichiro itojun Hagino 			break;
4473c62e87aSJun-ichiro itojun Hagino 		default:
4483c62e87aSJun-ichiro itojun Hagino 			printf("unknown-af ");
4493c62e87aSJun-ichiro itojun Hagino 			break;
4503c62e87aSJun-ichiro itojun Hagino 		}
4519a4365d0SYoshinobu Inoue 
4529a4365d0SYoshinobu Inoue 		/* destination address */
4533c62e87aSJun-ichiro itojun Hagino 		sa = (struct sockaddr *)(m_daddr + 1);
4543c62e87aSJun-ichiro itojun Hagino 		switch (sa->sa_family) {
4553c62e87aSJun-ichiro itojun Hagino 		case AF_INET:
4563c62e87aSJun-ichiro itojun Hagino 		case AF_INET6:
457bd9f52d5SHajimu UMEMOTO 			if (getnameinfo(sa, sa->sa_len, NULL, 0,
458bd9f52d5SHajimu UMEMOTO 			    pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
459bd9f52d5SHajimu UMEMOTO 				dport = 0;	/*XXX*/
4603c62e87aSJun-ichiro itojun Hagino 			else
461bd9f52d5SHajimu UMEMOTO 				dport = atoi(pbuf);
4623c62e87aSJun-ichiro itojun Hagino 			printf("%s%s ", str_ipaddr(sa),
4633c62e87aSJun-ichiro itojun Hagino 				str_prefport(sa->sa_family,
464bd9f52d5SHajimu UMEMOTO 				    m_daddr->sadb_address_prefixlen, dport,
465bd9f52d5SHajimu UMEMOTO 				    m_saddr->sadb_address_proto));
4663c62e87aSJun-ichiro itojun Hagino 			break;
4673c62e87aSJun-ichiro itojun Hagino 		default:
4683c62e87aSJun-ichiro itojun Hagino 			printf("unknown-af ");
4693c62e87aSJun-ichiro itojun Hagino 			break;
4703c62e87aSJun-ichiro itojun Hagino 		}
4719a4365d0SYoshinobu Inoue 
4729a4365d0SYoshinobu Inoue 		/* upper layer protocol */
473bd9f52d5SHajimu UMEMOTO 		if (m_saddr->sadb_address_proto !=
474bd9f52d5SHajimu UMEMOTO 		    m_daddr->sadb_address_proto) {
4759a4365d0SYoshinobu Inoue 			printf("upper layer protocol mismatched.\n");
4769a4365d0SYoshinobu Inoue 			return;
4779a4365d0SYoshinobu Inoue 		}
478bd9f52d5SHajimu UMEMOTO 		str_upperspec(m_saddr->sadb_address_proto, sport, dport);
479bd9f52d5SHajimu UMEMOTO 	}
4809a4365d0SYoshinobu Inoue 	else
481bd9f52d5SHajimu UMEMOTO 		printf("(no selector, probably per-socket policy) ");
4829a4365d0SYoshinobu Inoue 
4839a4365d0SYoshinobu Inoue 	/* policy */
4849a4365d0SYoshinobu Inoue     {
4859a4365d0SYoshinobu Inoue 	char *d_xpl;
4869a4365d0SYoshinobu Inoue 
4879a4365d0SYoshinobu Inoue 	if (m_xpl == NULL) {
4889a4365d0SYoshinobu Inoue 		printf("no X_POLICY extension.\n");
4899a4365d0SYoshinobu Inoue 		return;
4909a4365d0SYoshinobu Inoue 	}
4919a4365d0SYoshinobu Inoue 	d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
4929a4365d0SYoshinobu Inoue 
4939a4365d0SYoshinobu Inoue 	/* dump SPD */
4949a4365d0SYoshinobu Inoue 	printf("\n\t%s\n", d_xpl);
4959a4365d0SYoshinobu Inoue 	free(d_xpl);
4969a4365d0SYoshinobu Inoue     }
4979a4365d0SYoshinobu Inoue 
49833841545SHajimu UMEMOTO 	/* lifetime */
499bd9f52d5SHajimu UMEMOTO 	if (m_lftc) {
500bd9f52d5SHajimu UMEMOTO 		printf("\tcreated: %s  ",
501bd9f52d5SHajimu UMEMOTO 			str_time(m_lftc->sadb_lifetime_addtime));
502bd9f52d5SHajimu UMEMOTO 		printf("lastused: %s\n",
503bd9f52d5SHajimu UMEMOTO 			str_time(m_lftc->sadb_lifetime_usetime));
50433841545SHajimu UMEMOTO 	}
505bd9f52d5SHajimu UMEMOTO 	if (m_lfth) {
506bd9f52d5SHajimu UMEMOTO 		printf("\tlifetime: %lu(s) ",
507bd9f52d5SHajimu UMEMOTO 			(u_long)m_lfth->sadb_lifetime_addtime);
508bd9f52d5SHajimu UMEMOTO 		printf("validtime: %lu(s)\n",
509bd9f52d5SHajimu UMEMOTO 			(u_long)m_lfth->sadb_lifetime_usetime);
510bd9f52d5SHajimu UMEMOTO 	}
511bd9f52d5SHajimu UMEMOTO 
51233841545SHajimu UMEMOTO 
51322986c67SAndrey V. Elsukov 	printf("\tspid=%ld seq=%ld pid=%ld scope=",
5143c62e87aSJun-ichiro itojun Hagino 		(u_long)m_xpl->sadb_x_policy_id,
5159a4365d0SYoshinobu Inoue 		(u_long)m->sadb_msg_seq,
5169a4365d0SYoshinobu Inoue 		(u_long)m->sadb_msg_pid);
51722986c67SAndrey V. Elsukov 	GETMSGV2S(str_sp_scope, m_xpl->sadb_x_policy_scope);
51822986c67SAndrey V. Elsukov 	if (m_xpl->sadb_x_policy_scope == IPSEC_POLICYSCOPE_IFNET &&
51922986c67SAndrey V. Elsukov 	    if_indextoname(m_xpl->sadb_x_policy_ifindex, pbuf) != NULL)
52022986c67SAndrey V. Elsukov 		printf("ifname=%s", pbuf);
52122986c67SAndrey V. Elsukov 	printf("\n");
5229a4365d0SYoshinobu Inoue 
5239a4365d0SYoshinobu Inoue 	/* XXX TEST */
5243c62e87aSJun-ichiro itojun Hagino 	printf("\trefcnt=%u\n", m->sadb_msg_reserved);
5259a4365d0SYoshinobu Inoue 
5269a4365d0SYoshinobu Inoue 	return;
5279a4365d0SYoshinobu Inoue }
5289a4365d0SYoshinobu Inoue 
5299a4365d0SYoshinobu Inoue /*
5309a4365d0SYoshinobu Inoue  * set "ipaddress" to buffer.
5319a4365d0SYoshinobu Inoue  */
5329a4365d0SYoshinobu Inoue static char *
533650d6cc1SKonstantin Belousov str_ipaddr(struct sockaddr *sa)
5349a4365d0SYoshinobu Inoue {
5353c62e87aSJun-ichiro itojun Hagino 	static char buf[NI_MAXHOST];
5363c62e87aSJun-ichiro itojun Hagino 	const int niflag = NI_NUMERICHOST;
5379a4365d0SYoshinobu Inoue 
5383c62e87aSJun-ichiro itojun Hagino 	if (sa == NULL)
5399a4365d0SYoshinobu Inoue 		return "";
5409a4365d0SYoshinobu Inoue 
5413c62e87aSJun-ichiro itojun Hagino 	if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0)
5429a4365d0SYoshinobu Inoue 		return buf;
5433c62e87aSJun-ichiro itojun Hagino 	return NULL;
5449a4365d0SYoshinobu Inoue }
5459a4365d0SYoshinobu Inoue 
5469a4365d0SYoshinobu Inoue /*
5479a4365d0SYoshinobu Inoue  * set "/prefix[port number]" to buffer.
5489a4365d0SYoshinobu Inoue  */
5499a4365d0SYoshinobu Inoue static char *
550650d6cc1SKonstantin Belousov str_prefport(u_int family, u_int pref, u_int port, u_int ulp)
5519a4365d0SYoshinobu Inoue {
5529a4365d0SYoshinobu Inoue 	static char buf[128];
553296e054fSMunechika SUMIKAWA 	char prefbuf[128];
554296e054fSMunechika SUMIKAWA 	char portbuf[128];
5553c62e87aSJun-ichiro itojun Hagino 	int plen;
5569a4365d0SYoshinobu Inoue 
5573c62e87aSJun-ichiro itojun Hagino 	switch (family) {
5583c62e87aSJun-ichiro itojun Hagino 	case AF_INET:
5593c62e87aSJun-ichiro itojun Hagino 		plen = sizeof(struct in_addr) << 3;
5603c62e87aSJun-ichiro itojun Hagino 		break;
5613c62e87aSJun-ichiro itojun Hagino 	case AF_INET6:
5623c62e87aSJun-ichiro itojun Hagino 		plen = sizeof(struct in6_addr) << 3;
5633c62e87aSJun-ichiro itojun Hagino 		break;
5643c62e87aSJun-ichiro itojun Hagino 	default:
5653c62e87aSJun-ichiro itojun Hagino 		return "?";
5663c62e87aSJun-ichiro itojun Hagino 	}
5673c62e87aSJun-ichiro itojun Hagino 
5683c62e87aSJun-ichiro itojun Hagino 	if (pref == plen)
5699a4365d0SYoshinobu Inoue 		prefbuf[0] = '\0';
5709a4365d0SYoshinobu Inoue 	else
5719a4365d0SYoshinobu Inoue 		snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
5729a4365d0SYoshinobu Inoue 
573bd9f52d5SHajimu UMEMOTO 	if (ulp == IPPROTO_ICMPV6)
574bd9f52d5SHajimu UMEMOTO 		memset(portbuf, 0, sizeof(portbuf));
575bd9f52d5SHajimu UMEMOTO 	else {
5769a4365d0SYoshinobu Inoue 		if (port == IPSEC_PORT_ANY)
5779a4365d0SYoshinobu Inoue 			snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
5789a4365d0SYoshinobu Inoue 		else
5793c62e87aSJun-ichiro itojun Hagino 			snprintf(portbuf, sizeof(portbuf), "[%u]", port);
580bd9f52d5SHajimu UMEMOTO 	}
5819a4365d0SYoshinobu Inoue 
5829a4365d0SYoshinobu Inoue 	snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
5839a4365d0SYoshinobu Inoue 
5849a4365d0SYoshinobu Inoue 	return buf;
5859a4365d0SYoshinobu Inoue }
5869a4365d0SYoshinobu Inoue 
587bd9f52d5SHajimu UMEMOTO static void
588650d6cc1SKonstantin Belousov str_upperspec(u_int ulp, u_int p1, u_int p2)
589bd9f52d5SHajimu UMEMOTO {
590bd9f52d5SHajimu UMEMOTO 	if (ulp == IPSEC_ULPROTO_ANY)
591bd9f52d5SHajimu UMEMOTO 		printf("any");
592bd9f52d5SHajimu UMEMOTO 	else if (ulp == IPPROTO_ICMPV6) {
593bd9f52d5SHajimu UMEMOTO 		printf("icmp6");
594bd9f52d5SHajimu UMEMOTO 		if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY))
595bd9f52d5SHajimu UMEMOTO 			printf(" %u,%u", p1, p2);
596bd9f52d5SHajimu UMEMOTO 	} else {
597bd9f52d5SHajimu UMEMOTO 		struct protoent *ent;
598bd9f52d5SHajimu UMEMOTO 
599bd9f52d5SHajimu UMEMOTO 		switch (ulp) {
600bd9f52d5SHajimu UMEMOTO 		case IPPROTO_IPV4:
601bd9f52d5SHajimu UMEMOTO 			printf("ip4");
602bd9f52d5SHajimu UMEMOTO 			break;
603bd9f52d5SHajimu UMEMOTO 		default:
604bd9f52d5SHajimu UMEMOTO 			ent = getprotobynumber(ulp);
605bd9f52d5SHajimu UMEMOTO 			if (ent)
606bd9f52d5SHajimu UMEMOTO 				printf("%s", ent->p_name);
607bd9f52d5SHajimu UMEMOTO 			else
608bd9f52d5SHajimu UMEMOTO 				printf("%u", ulp);
609bd9f52d5SHajimu UMEMOTO 
610bd9f52d5SHajimu UMEMOTO 			endprotoent();
611bd9f52d5SHajimu UMEMOTO 			break;
612bd9f52d5SHajimu UMEMOTO 		}
613bd9f52d5SHajimu UMEMOTO 	}
614bd9f52d5SHajimu UMEMOTO }
615bd9f52d5SHajimu UMEMOTO 
6169a4365d0SYoshinobu Inoue /*
6179a4365d0SYoshinobu Inoue  * set "Mon Day Time Year" to buffer
6189a4365d0SYoshinobu Inoue  */
6199a4365d0SYoshinobu Inoue static char *
620650d6cc1SKonstantin Belousov str_time(time_t t)
6219a4365d0SYoshinobu Inoue {
6229a4365d0SYoshinobu Inoue 	static char buf[128];
6239a4365d0SYoshinobu Inoue 
6249a4365d0SYoshinobu Inoue 	if (t == 0) {
6259a4365d0SYoshinobu Inoue 		int i = 0;
6269a4365d0SYoshinobu Inoue 		for (;i < 20;) buf[i++] = ' ';
6279a4365d0SYoshinobu Inoue 	} else {
6289a4365d0SYoshinobu Inoue 		char *t0;
6299a4365d0SYoshinobu Inoue 		t0 = ctime(&t);
6309a4365d0SYoshinobu Inoue 		memcpy(buf, t0 + 4, 20);
6319a4365d0SYoshinobu Inoue 	}
6329a4365d0SYoshinobu Inoue 
6339a4365d0SYoshinobu Inoue 	buf[20] = '\0';
6349a4365d0SYoshinobu Inoue 
6359a4365d0SYoshinobu Inoue 	return(buf);
6369a4365d0SYoshinobu Inoue }
6379a4365d0SYoshinobu Inoue 
6389a4365d0SYoshinobu Inoue static void
639650d6cc1SKonstantin Belousov str_lifetime_byte(struct sadb_lifetime *x, char *str)
6409a4365d0SYoshinobu Inoue {
6419a4365d0SYoshinobu Inoue 	double y;
6429a4365d0SYoshinobu Inoue 	char *unit;
6439a4365d0SYoshinobu Inoue 	int w;
6449a4365d0SYoshinobu Inoue 
6459a4365d0SYoshinobu Inoue 	if (x == NULL) {
6469a4365d0SYoshinobu Inoue 		printf("\t%s: 0(bytes)", str);
6479a4365d0SYoshinobu Inoue 		return;
6489a4365d0SYoshinobu Inoue 	}
6499a4365d0SYoshinobu Inoue 
6503c62e87aSJun-ichiro itojun Hagino #if 0
6513c62e87aSJun-ichiro itojun Hagino 	if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
6523c62e87aSJun-ichiro itojun Hagino 		y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
6533c62e87aSJun-ichiro itojun Hagino 		unit = "M";
6543c62e87aSJun-ichiro itojun Hagino 		w = 1;
6553c62e87aSJun-ichiro itojun Hagino 	} else if ((x->sadb_lifetime_bytes) / 1024) {
6563c62e87aSJun-ichiro itojun Hagino 		y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
6573c62e87aSJun-ichiro itojun Hagino 		unit = "K";
6583c62e87aSJun-ichiro itojun Hagino 		w = 1;
6593c62e87aSJun-ichiro itojun Hagino 	} else {
6609a4365d0SYoshinobu Inoue 		y = (x->sadb_lifetime_bytes) * 1.0;
6619a4365d0SYoshinobu Inoue 		unit = "";
6629a4365d0SYoshinobu Inoue 		w = 0;
6633c62e87aSJun-ichiro itojun Hagino 	}
6643c62e87aSJun-ichiro itojun Hagino #else
6653c62e87aSJun-ichiro itojun Hagino 	y = (x->sadb_lifetime_bytes) * 1.0;
6663c62e87aSJun-ichiro itojun Hagino 	unit = "";
6673c62e87aSJun-ichiro itojun Hagino 	w = 0;
6683c62e87aSJun-ichiro itojun Hagino #endif
6699a4365d0SYoshinobu Inoue 	printf("\t%s: %.*f(%sbytes)", str, w, y, unit);
6709a4365d0SYoshinobu Inoue }
671