xref: /freebsd/lib/libipsec/pfkey_dump.c (revision b59017c5cad90d0f09a59e68c00457b7faf93e7c)
1 /*	$KAME: pfkey_dump.c,v 1.45 2003/09/08 10:14:56 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <net/if.h>
38 #include <net/pfkeyv2.h>
39 #include <netipsec/ipsec.h>
40 #include <netipsec/key_var.h>
41 #include <netipsec/key_debug.h>
42 
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 
46 #include <stdbool.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <time.h>
52 #include <netdb.h>
53 
54 #include "ipsec_strerror.h"
55 #include "libpfkey.h"
56 
57 /* cope with old kame headers - ugly */
58 #ifndef SADB_X_AALG_NULL
59 #define SADB_X_AALG_NULL	SADB_AALG_NULL
60 #endif
61 
62 #ifndef SADB_X_EALG_RC5CBC
63 #ifdef SADB_EALG_RC5CBC
64 #define SADB_X_EALG_RC5CBC	SADB_EALG_RC5CBC
65 #endif
66 #endif
67 
68 #define GETMSGSTR(str, num) \
69 do { \
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)); \
75 	else \
76 		printf("%s ", (str)[(num)]); \
77 } while (0)
78 
79 #define GETMSGV2S(v2s, num) \
80 do { \
81 	struct val2str *p;  \
82 	for (p = (v2s); p && p->str; p++) { \
83 		if (p->val == (num)) \
84 			break; \
85 	} \
86 	if (p && p->str) \
87 		printf("%s ", p->str); \
88 	else \
89 		printf("%u ", (num)); \
90 } while (0)
91 
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 *);
97 
98 struct val2str {
99 	int val;
100 	const char *str;
101 };
102 
103 /*
104  * Must to be re-written about following strings.
105  */
106 static char *str_satype[] = {
107 	"unspec",
108 	"unknown",
109 	"ah",
110 	"esp",
111 	"unknown",
112 	"rsvp",
113 	"ospfv2",
114 	"ripv2",
115 	"mip",
116 	"ipcomp",
117 	"policy",
118 	"tcp"
119 };
120 
121 static char *str_mode[] = {
122 	"any",
123 	"transport",
124 	"tunnel",
125 };
126 
127 static char *str_state[] = {
128 	"larval",
129 	"mature",
130 	"dying",
131 	"dead",
132 };
133 
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", },
141 #endif
142 #ifdef SADB_X_AALG_SHA2_384
143 	{ SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
144 #endif
145 #ifdef SADB_X_AALG_SHA2_512
146 	{ SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
147 #endif
148 #ifdef SADB_X_AALG_AES_XCBC_MAC
149 	{ SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", },
150 #endif
151 #ifdef SADB_X_AALG_CHACHA20POLY1305
152 	{ SADB_X_AALG_CHACHA20POLY1305, "chacha20-poly1305", },
153 #endif
154 	{ -1, NULL, },
155 };
156 
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", },
162 #endif
163 #ifdef SADB_X_EALG_AESCBC
164 	{ SADB_X_EALG_AESCBC, "aes-cbc", },
165 #endif
166 #ifdef SADB_X_EALG_TWOFISHCBC
167 	{ SADB_X_EALG_TWOFISHCBC, "twofish-cbc", },
168 #endif
169 #ifdef SADB_X_EALG_AESCTR
170 	{ SADB_X_EALG_AESCTR, "aes-ctr", },
171 #endif
172 #ifdef SADB_X_EALG_AESGCM16
173 	{ SADB_X_EALG_AESGCM16, "aes-gcm-16", },
174 #endif
175 #ifdef SADB_X_EALG_CHACHA20POLY1305
176 	{ SADB_X_EALG_CHACHA20POLY1305, "chacha20-poly1305", },
177 #endif
178 	{ -1, NULL, },
179 };
180 
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", },
186 	{ -1, NULL, },
187 };
188 
189 static struct val2str str_sp_scope[] = {
190 	{ IPSEC_POLICYSCOPE_GLOBAL, "global" },
191 	{ IPSEC_POLICYSCOPE_IFNET, "ifnet" },
192 	{ IPSEC_POLICYSCOPE_PCB, "pcb"},
193 	{ -1, NULL },
194 };
195 
196 /*
197  * dump SADB_MSG formatted.  For debugging, you should use kdebug_sadb().
198  */
199 void
200 pfkey_sadump(struct sadb_msg *m)
201 {
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, *m_lft_sw, *m_lft_hw;
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;
214 	struct sadb_x_if_hw_offl *if_hw_offl;
215 	caddr_t p, ep;
216 	struct sadb_ext *ext;
217 	bool first;
218 
219 	/* check pfkey message. */
220 	if (pfkey_align(m, mhp)) {
221 		printf("%s\n", ipsec_strerror());
222 		return;
223 	}
224 	if (pfkey_check(mhp)) {
225 		printf("%s\n", ipsec_strerror());
226 		return;
227 	}
228 
229 	m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
230 	m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
231 	m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
232 	m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
233 	m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
234 	m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
235 	m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
236 	m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY];
237 	m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
238 	m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
239 	m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
240 	m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
241 	m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
242 	m_sa_replay = (struct sadb_x_sa_replay *)mhp[SADB_X_EXT_SA_REPLAY];
243 	natt_type = (struct sadb_x_nat_t_type *)mhp[SADB_X_EXT_NAT_T_TYPE];
244 	natt_sport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_SPORT];
245 	natt_dport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_DPORT];
246 	natt_oai = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAI];
247 	natt_oar = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAR];
248 	m_lft_sw = (struct sadb_lifetime *)mhp[SADB_X_EXT_LFT_CUR_SW_OFFL];
249 	m_lft_hw = (struct sadb_lifetime *)mhp[SADB_X_EXT_LFT_CUR_HW_OFFL];
250 	if_hw_offl = (struct sadb_x_if_hw_offl *)mhp[SADB_X_EXT_IF_HW_OFFL];
251 
252 	/* source address */
253 	if (m_saddr == NULL) {
254 		printf("no ADDRESS_SRC extension.\n");
255 		return;
256 	}
257 	printf("%s", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
258 	if (natt_type != NULL && natt_sport != NULL)
259 		printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port));
260 
261 	/* destination address */
262 	if (m_daddr == NULL) {
263 		printf("\nno ADDRESS_DST extension.\n");
264 		return;
265 	}
266 	printf(" %s", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
267 	if (natt_type != NULL && natt_dport != NULL)
268 		printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port));
269 
270 	/* SA type */
271 	if (m_sa == NULL) {
272 		printf("\nno SA extension.\n");
273 		return;
274 	}
275 	if (m_sa2 == NULL) {
276 		printf("\nno SA2 extension.\n");
277 		return;
278 	}
279 	printf("\n\t");
280 
281 	if (m->sadb_msg_satype == SADB_SATYPE_ESP && natt_type != NULL)
282 		printf("esp-udp ");
283 	else
284 		GETMSGSTR(str_satype, m->sadb_msg_satype);
285 
286 	printf("mode=");
287 	GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
288 
289 	printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
290 		(u_int32_t)ntohl(m_sa->sadb_sa_spi),
291 		(u_int32_t)ntohl(m_sa->sadb_sa_spi),
292 		(u_int32_t)m_sa2->sadb_x_sa2_reqid,
293 		(u_int32_t)m_sa2->sadb_x_sa2_reqid);
294 
295 	/* other NAT-T information */
296 	if (natt_type != NULL && (natt_oai != NULL || natt_oar != NULL)) {
297 		printf("\tNAT:");
298 		if (natt_oai != NULL)
299 			printf(" OAI=%s",
300 			    str_ipaddr((struct sockaddr *)(natt_oai + 1)));
301 		if (natt_oar != NULL)
302 			printf(" OAR=%s",
303 			    str_ipaddr((struct sockaddr *)(natt_oar + 1)));
304 		printf("\n");
305 	}
306 
307 	/* encryption key */
308 	if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
309 		printf("\tC: ");
310 		GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
311 	} else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
312 		if (m_enc != NULL) {
313 			printf("\tE: ");
314 			GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
315 			ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
316 				      m_enc->sadb_key_bits / 8);
317 			printf("\n");
318 		}
319 	}
320 
321 	/* authentication key */
322 	if (m_auth != NULL) {
323 		printf("\tA: ");
324 		GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
325 		ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
326 		              m_auth->sadb_key_bits / 8);
327 		printf("\n");
328 	}
329 
330 	/* replay windoe size & flags */
331 	printf("\tseq=0x%08x replay=%u flags=0x%08x ",
332 		m_sa2->sadb_x_sa2_sequence,
333 		m_sa_replay ? (m_sa_replay->sadb_x_sa_replay_replay >> 3) :
334 			m_sa->sadb_sa_replay,
335 		m_sa->sadb_sa_flags);
336 
337 	/* state */
338 	printf("state=");
339 	GETMSGSTR(str_state, m_sa->sadb_sa_state);
340 	printf("\n");
341 
342 	/* hw offload interface */
343 	if (if_hw_offl != NULL) {
344 		p = (caddr_t)m;
345 		ep = p + PFKEY_UNUNIT64(m->sadb_msg_len);
346 		p += sizeof(struct sadb_msg);
347 		printf("\thw offl if: ");
348 
349 		for (first = true; p < ep; p += PFKEY_EXTLEN(ext)) {
350 			ext = (struct sadb_ext *)p;
351 			if (ext->sadb_ext_type != SADB_X_EXT_IF_HW_OFFL)
352 				continue;
353 			if_hw_offl = (struct sadb_x_if_hw_offl *)ext;
354 			if (first)
355 				first = false;
356 			else
357 				printf(",");
358 			printf("%s", if_hw_offl->sadb_x_if_hw_offl_if);
359 		}
360 		printf("\n");
361 	}
362 
363 	/* lifetime */
364 	if (m_lftc != NULL) {
365 		time_t tmp_time = time(0);
366 
367 		printf("\tcreated: %s",
368 			str_time(m_lftc->sadb_lifetime_addtime));
369 		printf("\tcurrent: %s\n", str_time(tmp_time));
370 		printf("\tdiff: %lu(s)",
371 			(u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
372 			0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
373 
374 		printf("\thard: %lu(s)",
375 			(u_long)(m_lfth == NULL ?
376 			0 : m_lfth->sadb_lifetime_addtime));
377 		printf("\tsoft: %lu(s)\n",
378 			(u_long)(m_lfts == NULL ?
379 			0 : m_lfts->sadb_lifetime_addtime));
380 
381 		printf("\tlast: %s",
382 			str_time(m_lftc->sadb_lifetime_usetime));
383 		printf("\thard: %lu(s)",
384 			(u_long)(m_lfth == NULL ?
385 			0 : m_lfth->sadb_lifetime_usetime));
386 		printf("\tsoft: %lu(s)\n",
387 			(u_long)(m_lfts == NULL ?
388 			0 : m_lfts->sadb_lifetime_usetime));
389 
390 		str_lifetime_byte(m_lftc, "current");
391 		str_lifetime_byte(m_lfth, "hard");
392 		str_lifetime_byte(m_lfts, "soft");
393 		printf("\n");
394 
395 		printf("\tallocated: %lu",
396 			(unsigned long)m_lftc->sadb_lifetime_allocations);
397 		printf("\thard: %lu",
398 			(u_long)(m_lfth == NULL ?
399 			0 : m_lfth->sadb_lifetime_allocations));
400 		printf("\tsoft: %lu\n",
401 			(u_long)(m_lfts == NULL ?
402 			0 : m_lfts->sadb_lifetime_allocations));
403 	}
404 
405 	printf("\tsadb_seq=%lu pid=%lu ",
406 		(u_long)m->sadb_msg_seq,
407 		(u_long)m->sadb_msg_pid);
408 
409 	/* XXX DEBUG */
410 	printf("refcnt=%u\n", m->sadb_msg_reserved);
411 
412 	if (m_lft_sw != NULL) {
413 		printf("\tsw offl use: %s",
414 		    str_time(m_lft_sw->sadb_lifetime_usetime));
415 		printf("\tsw offl allocated: %lu",
416 		    (unsigned long)m_lft_sw->sadb_lifetime_allocations);
417 		str_lifetime_byte(m_lft_sw, "sw offl");
418 		printf("\n");
419 	}
420 
421 	if (m_lft_hw != NULL) {
422 		printf("\thw offl use: %s",
423 		    str_time(m_lft_hw->sadb_lifetime_usetime));
424 		printf("\thw offl allocated: %lu",
425 		    (unsigned long)m_lft_hw->sadb_lifetime_allocations);
426 		str_lifetime_byte(m_lft_hw, "hw offl");
427 		printf("\n");
428 	}
429 }
430 
431 void
432 pfkey_spdump(struct sadb_msg *m)
433 {
434 	char pbuf[NI_MAXSERV];
435 	caddr_t mhp[SADB_EXT_MAX + 1];
436 	struct sadb_address *m_saddr, *m_daddr;
437 	struct sadb_x_policy *m_xpl;
438 	struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL;
439 	struct sockaddr *sa;
440 	u_int16_t sport = 0, dport = 0;
441 
442 	/* check pfkey message. */
443 	if (pfkey_align(m, mhp)) {
444 		printf("%s\n", ipsec_strerror());
445 		return;
446 	}
447 	if (pfkey_check(mhp)) {
448 		printf("%s\n", ipsec_strerror());
449 		return;
450 	}
451 
452 	m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
453 	m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
454 	m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
455 	m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
456 	m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
457 
458 	if (m_saddr && m_daddr) {
459 		/* source address */
460 		sa = (struct sockaddr *)(m_saddr + 1);
461 		switch (sa->sa_family) {
462 		case AF_INET:
463 		case AF_INET6:
464 			if (getnameinfo(sa, sa->sa_len, NULL, 0,
465 			    pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
466 				sport = 0;	/*XXX*/
467 			else
468 				sport = atoi(pbuf);
469 			printf("%s%s ", str_ipaddr(sa),
470 				str_prefport(sa->sa_family,
471 				    m_saddr->sadb_address_prefixlen, sport,
472 				    m_saddr->sadb_address_proto));
473 			break;
474 		default:
475 			printf("unknown-af ");
476 			break;
477 		}
478 
479 		/* destination address */
480 		sa = (struct sockaddr *)(m_daddr + 1);
481 		switch (sa->sa_family) {
482 		case AF_INET:
483 		case AF_INET6:
484 			if (getnameinfo(sa, sa->sa_len, NULL, 0,
485 			    pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
486 				dport = 0;	/*XXX*/
487 			else
488 				dport = atoi(pbuf);
489 			printf("%s%s ", str_ipaddr(sa),
490 				str_prefport(sa->sa_family,
491 				    m_daddr->sadb_address_prefixlen, dport,
492 				    m_saddr->sadb_address_proto));
493 			break;
494 		default:
495 			printf("unknown-af ");
496 			break;
497 		}
498 
499 		/* upper layer protocol */
500 		if (m_saddr->sadb_address_proto !=
501 		    m_daddr->sadb_address_proto) {
502 			printf("upper layer protocol mismatched.\n");
503 			return;
504 		}
505 		str_upperspec(m_saddr->sadb_address_proto, sport, dport);
506 	}
507 	else
508 		printf("(no selector, probably per-socket policy) ");
509 
510 	/* policy */
511     {
512 	char *d_xpl;
513 
514 	if (m_xpl == NULL) {
515 		printf("no X_POLICY extension.\n");
516 		return;
517 	}
518 	d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
519 
520 	/* dump SPD */
521 	printf("\n\t%s\n", d_xpl);
522 	free(d_xpl);
523     }
524 
525 	/* lifetime */
526 	if (m_lftc) {
527 		printf("\tcreated: %s  ",
528 			str_time(m_lftc->sadb_lifetime_addtime));
529 		printf("lastused: %s\n",
530 			str_time(m_lftc->sadb_lifetime_usetime));
531 	}
532 	if (m_lfth) {
533 		printf("\tlifetime: %lu(s) ",
534 			(u_long)m_lfth->sadb_lifetime_addtime);
535 		printf("validtime: %lu(s)\n",
536 			(u_long)m_lfth->sadb_lifetime_usetime);
537 	}
538 
539 
540 	printf("\tspid=%ld seq=%ld pid=%ld scope=",
541 		(u_long)m_xpl->sadb_x_policy_id,
542 		(u_long)m->sadb_msg_seq,
543 		(u_long)m->sadb_msg_pid);
544 	GETMSGV2S(str_sp_scope, m_xpl->sadb_x_policy_scope);
545 	if (m_xpl->sadb_x_policy_scope == IPSEC_POLICYSCOPE_IFNET &&
546 	    if_indextoname(m_xpl->sadb_x_policy_ifindex, pbuf) != NULL)
547 		printf("ifname=%s", pbuf);
548 	printf("\n");
549 
550 	/* XXX TEST */
551 	printf("\trefcnt=%u\n", m->sadb_msg_reserved);
552 
553 	return;
554 }
555 
556 /*
557  * set "ipaddress" to buffer.
558  */
559 static char *
560 str_ipaddr(struct sockaddr *sa)
561 {
562 	static char buf[NI_MAXHOST];
563 	const int niflag = NI_NUMERICHOST;
564 
565 	if (sa == NULL)
566 		return "";
567 
568 	if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0)
569 		return buf;
570 	return NULL;
571 }
572 
573 /*
574  * set "/prefix[port number]" to buffer.
575  */
576 static char *
577 str_prefport(u_int family, u_int pref, u_int port, u_int ulp)
578 {
579 	static char buf[128];
580 	char prefbuf[128];
581 	char portbuf[128];
582 	int plen;
583 
584 	switch (family) {
585 	case AF_INET:
586 		plen = sizeof(struct in_addr) << 3;
587 		break;
588 	case AF_INET6:
589 		plen = sizeof(struct in6_addr) << 3;
590 		break;
591 	default:
592 		return "?";
593 	}
594 
595 	if (pref == plen)
596 		prefbuf[0] = '\0';
597 	else
598 		snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
599 
600 	if (ulp == IPPROTO_ICMPV6)
601 		memset(portbuf, 0, sizeof(portbuf));
602 	else {
603 		if (port == IPSEC_PORT_ANY)
604 			snprintf(portbuf, sizeof(portbuf), "[%s]", "any");
605 		else
606 			snprintf(portbuf, sizeof(portbuf), "[%u]", port);
607 	}
608 
609 	snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
610 
611 	return buf;
612 }
613 
614 static void
615 str_upperspec(u_int ulp, u_int p1, u_int p2)
616 {
617 	if (ulp == IPSEC_ULPROTO_ANY)
618 		printf("any");
619 	else if (ulp == IPPROTO_ICMPV6) {
620 		printf("icmp6");
621 		if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY))
622 			printf(" %u,%u", p1, p2);
623 	} else {
624 		struct protoent *ent;
625 
626 		switch (ulp) {
627 		case IPPROTO_IPV4:
628 			printf("ip4");
629 			break;
630 		default:
631 			ent = getprotobynumber(ulp);
632 			if (ent)
633 				printf("%s", ent->p_name);
634 			else
635 				printf("%u", ulp);
636 
637 			endprotoent();
638 			break;
639 		}
640 	}
641 }
642 
643 /*
644  * set "Mon Day Time Year" to buffer
645  */
646 static char *
647 str_time(time_t t)
648 {
649 	static char buf[128];
650 
651 	if (t == 0) {
652 		int i = 0;
653 		for (;i < 20;) buf[i++] = ' ';
654 	} else {
655 		char *t0;
656 		t0 = ctime(&t);
657 		memcpy(buf, t0 + 4, 20);
658 	}
659 
660 	buf[20] = '\0';
661 
662 	return(buf);
663 }
664 
665 static void
666 str_lifetime_byte(struct sadb_lifetime *x, char *str)
667 {
668 	double y;
669 	char *unit;
670 	int w;
671 
672 	if (x == NULL) {
673 		printf("\t%s: 0(bytes)", str);
674 		return;
675 	}
676 
677 #if 0
678 	if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
679 		y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
680 		unit = "M";
681 		w = 1;
682 	} else if ((x->sadb_lifetime_bytes) / 1024) {
683 		y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
684 		unit = "K";
685 		w = 1;
686 	} else {
687 		y = (x->sadb_lifetime_bytes) * 1.0;
688 		unit = "";
689 		w = 0;
690 	}
691 #else
692 	y = (x->sadb_lifetime_bytes) * 1.0;
693 	unit = "";
694 	w = 0;
695 #endif
696 	printf("\t%s: %.*f(%sbytes)", str, w, y, unit);
697 }
698