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