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