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