xref: /freebsd/lib/libipsec/pfkey_dump.c (revision 1dfcff294e44d4b45813288ef4095c36abb22f0e)
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 	{ -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 	{ -1, NULL, },
175 };
176 
177 static struct val2str str_alg_comp[] = {
178 	{ SADB_X_CALG_NONE, "none", },
179 	{ SADB_X_CALG_OUI, "oui", },
180 	{ SADB_X_CALG_DEFLATE, "deflate", },
181 	{ SADB_X_CALG_LZS, "lzs", },
182 	{ -1, NULL, },
183 };
184 
185 static struct val2str str_sp_scope[] = {
186 	{ IPSEC_POLICYSCOPE_GLOBAL, "global" },
187 	{ IPSEC_POLICYSCOPE_IFNET, "ifnet" },
188 	{ IPSEC_POLICYSCOPE_PCB, "pcb"},
189 	{ -1, NULL },
190 };
191 
192 /*
193  * dump SADB_MSG formatted.  For debugging, you should use kdebug_sadb().
194  */
195 void
196 pfkey_sadump(m)
197 	struct sadb_msg *m;
198 {
199 	caddr_t mhp[SADB_EXT_MAX + 1];
200 	struct sadb_sa *m_sa;
201 	struct sadb_x_sa2 *m_sa2;
202 	struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
203 	struct sadb_address *m_saddr, *m_daddr, *m_paddr;
204 	struct sadb_key *m_auth, *m_enc;
205 	struct sadb_ident *m_sid, *m_did;
206 	struct sadb_sens *m_sens;
207 	struct sadb_x_sa_replay *m_sa_replay;
208 	struct sadb_x_nat_t_type *natt_type;
209 	struct sadb_x_nat_t_port *natt_sport, *natt_dport;
210 	struct sadb_address *natt_oai, *natt_oar;
211 
212 	/* check pfkey message. */
213 	if (pfkey_align(m, mhp)) {
214 		printf("%s\n", ipsec_strerror());
215 		return;
216 	}
217 	if (pfkey_check(mhp)) {
218 		printf("%s\n", ipsec_strerror());
219 		return;
220 	}
221 
222 	m_sa = (struct sadb_sa *)mhp[SADB_EXT_SA];
223 	m_sa2 = (struct sadb_x_sa2 *)mhp[SADB_X_EXT_SA2];
224 	m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
225 	m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
226 	m_lfts = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_SOFT];
227 	m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
228 	m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
229 	m_paddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_PROXY];
230 	m_auth = (struct sadb_key *)mhp[SADB_EXT_KEY_AUTH];
231 	m_enc = (struct sadb_key *)mhp[SADB_EXT_KEY_ENCRYPT];
232 	m_sid = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_SRC];
233 	m_did = (struct sadb_ident *)mhp[SADB_EXT_IDENTITY_DST];
234 	m_sens = (struct sadb_sens *)mhp[SADB_EXT_SENSITIVITY];
235 	m_sa_replay = (struct sadb_x_sa_replay *)mhp[SADB_X_EXT_SA_REPLAY];
236 	natt_type = (struct sadb_x_nat_t_type *)mhp[SADB_X_EXT_NAT_T_TYPE];
237 	natt_sport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_SPORT];
238 	natt_dport = (struct sadb_x_nat_t_port *)mhp[SADB_X_EXT_NAT_T_DPORT];
239 	natt_oai = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAI];
240 	natt_oar = (struct sadb_address *)mhp[SADB_X_EXT_NAT_T_OAR];
241 
242 
243 	/* source address */
244 	if (m_saddr == NULL) {
245 		printf("no ADDRESS_SRC extension.\n");
246 		return;
247 	}
248 	printf("%s", str_ipaddr((struct sockaddr *)(m_saddr + 1)));
249 	if (natt_type != NULL && natt_sport != NULL)
250 		printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port));
251 
252 	/* destination address */
253 	if (m_daddr == NULL) {
254 		printf("\nno ADDRESS_DST extension.\n");
255 		return;
256 	}
257 	printf(" %s", str_ipaddr((struct sockaddr *)(m_daddr + 1)));
258 	if (natt_type != NULL && natt_dport != NULL)
259 		printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port));
260 
261 	/* SA type */
262 	if (m_sa == NULL) {
263 		printf("\nno SA extension.\n");
264 		return;
265 	}
266 	if (m_sa2 == NULL) {
267 		printf("\nno SA2 extension.\n");
268 		return;
269 	}
270 	printf("\n\t");
271 
272 	if (m->sadb_msg_satype == SADB_SATYPE_ESP && natt_type != NULL)
273 		printf("esp-udp ");
274 	else
275 		GETMSGSTR(str_satype, m->sadb_msg_satype);
276 
277 	printf("mode=");
278 	GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
279 
280 	printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
281 		(u_int32_t)ntohl(m_sa->sadb_sa_spi),
282 		(u_int32_t)ntohl(m_sa->sadb_sa_spi),
283 		(u_int32_t)m_sa2->sadb_x_sa2_reqid,
284 		(u_int32_t)m_sa2->sadb_x_sa2_reqid);
285 
286 	/* other NAT-T information */
287 	if (natt_type != NULL && (natt_oai != NULL || natt_oar != NULL)) {
288 		printf("\tNAT:");
289 		if (natt_oai != NULL)
290 			printf(" OAI=%s",
291 			    str_ipaddr((struct sockaddr *)(natt_oai + 1)));
292 		if (natt_oar != NULL)
293 			printf(" OAR=%s",
294 			    str_ipaddr((struct sockaddr *)(natt_oar + 1)));
295 		printf("\n");
296 	}
297 
298 	/* encryption key */
299 	if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
300 		printf("\tC: ");
301 		GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
302 	} else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
303 		if (m_enc != NULL) {
304 			printf("\tE: ");
305 			GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
306 			ipsec_hexdump((caddr_t)m_enc + sizeof(*m_enc),
307 				      m_enc->sadb_key_bits / 8);
308 			printf("\n");
309 		}
310 	}
311 
312 	/* authentication key */
313 	if (m_auth != NULL) {
314 		printf("\tA: ");
315 		GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
316 		ipsec_hexdump((caddr_t)m_auth + sizeof(*m_auth),
317 		              m_auth->sadb_key_bits / 8);
318 		printf("\n");
319 	}
320 
321 	/* replay windoe size & flags */
322 	printf("\tseq=0x%08x replay=%u flags=0x%08x ",
323 		m_sa2->sadb_x_sa2_sequence,
324 		m_sa_replay ? (m_sa_replay->sadb_x_sa_replay_replay >> 3) :
325 			m_sa->sadb_sa_replay,
326 		m_sa->sadb_sa_flags);
327 
328 	/* state */
329 	printf("state=");
330 	GETMSGSTR(str_state, m_sa->sadb_sa_state);
331 	printf("\n");
332 
333 	/* lifetime */
334 	if (m_lftc != NULL) {
335 		time_t tmp_time = time(0);
336 
337 		printf("\tcreated: %s",
338 			str_time(m_lftc->sadb_lifetime_addtime));
339 		printf("\tcurrent: %s\n", str_time(tmp_time));
340 		printf("\tdiff: %lu(s)",
341 			(u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
342 			0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
343 
344 		printf("\thard: %lu(s)",
345 			(u_long)(m_lfth == NULL ?
346 			0 : m_lfth->sadb_lifetime_addtime));
347 		printf("\tsoft: %lu(s)\n",
348 			(u_long)(m_lfts == NULL ?
349 			0 : m_lfts->sadb_lifetime_addtime));
350 
351 		printf("\tlast: %s",
352 			str_time(m_lftc->sadb_lifetime_usetime));
353 		printf("\thard: %lu(s)",
354 			(u_long)(m_lfth == NULL ?
355 			0 : m_lfth->sadb_lifetime_usetime));
356 		printf("\tsoft: %lu(s)\n",
357 			(u_long)(m_lfts == NULL ?
358 			0 : m_lfts->sadb_lifetime_usetime));
359 
360 		str_lifetime_byte(m_lftc, "current");
361 		str_lifetime_byte(m_lfth, "hard");
362 		str_lifetime_byte(m_lfts, "soft");
363 		printf("\n");
364 
365 		printf("\tallocated: %lu",
366 			(unsigned long)m_lftc->sadb_lifetime_allocations);
367 		printf("\thard: %lu",
368 			(u_long)(m_lfth == NULL ?
369 			0 : m_lfth->sadb_lifetime_allocations));
370 		printf("\tsoft: %lu\n",
371 			(u_long)(m_lfts == NULL ?
372 			0 : m_lfts->sadb_lifetime_allocations));
373 	}
374 
375 	printf("\tsadb_seq=%lu pid=%lu ",
376 		(u_long)m->sadb_msg_seq,
377 		(u_long)m->sadb_msg_pid);
378 
379 	/* XXX DEBUG */
380 	printf("refcnt=%u\n", m->sadb_msg_reserved);
381 
382 	return;
383 }
384 
385 void
386 pfkey_spdump(struct sadb_msg *m)
387 {
388 	char pbuf[NI_MAXSERV];
389 	caddr_t mhp[SADB_EXT_MAX + 1];
390 	struct sadb_address *m_saddr, *m_daddr;
391 	struct sadb_x_policy *m_xpl;
392 	struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL;
393 	struct sockaddr *sa;
394 	u_int16_t sport = 0, dport = 0;
395 
396 	/* check pfkey message. */
397 	if (pfkey_align(m, mhp)) {
398 		printf("%s\n", ipsec_strerror());
399 		return;
400 	}
401 	if (pfkey_check(mhp)) {
402 		printf("%s\n", ipsec_strerror());
403 		return;
404 	}
405 
406 	m_saddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_SRC];
407 	m_daddr = (struct sadb_address *)mhp[SADB_EXT_ADDRESS_DST];
408 	m_xpl = (struct sadb_x_policy *)mhp[SADB_X_EXT_POLICY];
409 	m_lftc = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_CURRENT];
410 	m_lfth = (struct sadb_lifetime *)mhp[SADB_EXT_LIFETIME_HARD];
411 
412 	if (m_saddr && m_daddr) {
413 		/* source address */
414 		sa = (struct sockaddr *)(m_saddr + 1);
415 		switch (sa->sa_family) {
416 		case AF_INET:
417 		case AF_INET6:
418 			if (getnameinfo(sa, sa->sa_len, NULL, 0,
419 			    pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
420 				sport = 0;	/*XXX*/
421 			else
422 				sport = atoi(pbuf);
423 			printf("%s%s ", str_ipaddr(sa),
424 				str_prefport(sa->sa_family,
425 				    m_saddr->sadb_address_prefixlen, sport,
426 				    m_saddr->sadb_address_proto));
427 			break;
428 		default:
429 			printf("unknown-af ");
430 			break;
431 		}
432 
433 		/* destination address */
434 		sa = (struct sockaddr *)(m_daddr + 1);
435 		switch (sa->sa_family) {
436 		case AF_INET:
437 		case AF_INET6:
438 			if (getnameinfo(sa, sa->sa_len, NULL, 0,
439 			    pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
440 				dport = 0;	/*XXX*/
441 			else
442 				dport = atoi(pbuf);
443 			printf("%s%s ", str_ipaddr(sa),
444 				str_prefport(sa->sa_family,
445 				    m_daddr->sadb_address_prefixlen, dport,
446 				    m_saddr->sadb_address_proto));
447 			break;
448 		default:
449 			printf("unknown-af ");
450 			break;
451 		}
452 
453 		/* upper layer protocol */
454 		if (m_saddr->sadb_address_proto !=
455 		    m_daddr->sadb_address_proto) {
456 			printf("upper layer protocol mismatched.\n");
457 			return;
458 		}
459 		str_upperspec(m_saddr->sadb_address_proto, sport, dport);
460 	}
461 	else
462 		printf("(no selector, probably per-socket policy) ");
463 
464 	/* policy */
465     {
466 	char *d_xpl;
467 
468 	if (m_xpl == NULL) {
469 		printf("no X_POLICY extension.\n");
470 		return;
471 	}
472 	d_xpl = ipsec_dump_policy((char *)m_xpl, "\n\t");
473 
474 	/* dump SPD */
475 	printf("\n\t%s\n", d_xpl);
476 	free(d_xpl);
477     }
478 
479 	/* lifetime */
480 	if (m_lftc) {
481 		printf("\tcreated: %s  ",
482 			str_time(m_lftc->sadb_lifetime_addtime));
483 		printf("lastused: %s\n",
484 			str_time(m_lftc->sadb_lifetime_usetime));
485 	}
486 	if (m_lfth) {
487 		printf("\tlifetime: %lu(s) ",
488 			(u_long)m_lfth->sadb_lifetime_addtime);
489 		printf("validtime: %lu(s)\n",
490 			(u_long)m_lfth->sadb_lifetime_usetime);
491 	}
492 
493 
494 	printf("\tspid=%ld seq=%ld pid=%ld scope=",
495 		(u_long)m_xpl->sadb_x_policy_id,
496 		(u_long)m->sadb_msg_seq,
497 		(u_long)m->sadb_msg_pid);
498 	GETMSGV2S(str_sp_scope, m_xpl->sadb_x_policy_scope);
499 	if (m_xpl->sadb_x_policy_scope == IPSEC_POLICYSCOPE_IFNET &&
500 	    if_indextoname(m_xpl->sadb_x_policy_ifindex, pbuf) != NULL)
501 		printf("ifname=%s", pbuf);
502 	printf("\n");
503 
504 	/* XXX TEST */
505 	printf("\trefcnt=%u\n", m->sadb_msg_reserved);
506 
507 	return;
508 }
509 
510 /*
511  * set "ipaddress" to buffer.
512  */
513 static char *
514 str_ipaddr(sa)
515 	struct sockaddr *sa;
516 {
517 	static char buf[NI_MAXHOST];
518 	const int niflag = NI_NUMERICHOST;
519 
520 	if (sa == NULL)
521 		return "";
522 
523 	if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0, niflag) == 0)
524 		return buf;
525 	return NULL;
526 }
527 
528 /*
529  * set "/prefix[port number]" to buffer.
530  */
531 static char *
532 str_prefport(family, pref, port, ulp)
533 	u_int family, pref, port, 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(ulp, p1, p2)
572 	u_int ulp, p1, p2;
573 {
574 	if (ulp == IPSEC_ULPROTO_ANY)
575 		printf("any");
576 	else if (ulp == IPPROTO_ICMPV6) {
577 		printf("icmp6");
578 		if (!(p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY))
579 			printf(" %u,%u", p1, p2);
580 	} else {
581 		struct protoent *ent;
582 
583 		switch (ulp) {
584 		case IPPROTO_IPV4:
585 			printf("ip4");
586 			break;
587 		default:
588 			ent = getprotobynumber(ulp);
589 			if (ent)
590 				printf("%s", ent->p_name);
591 			else
592 				printf("%u", ulp);
593 
594 			endprotoent();
595 			break;
596 		}
597 	}
598 }
599 
600 /*
601  * set "Mon Day Time Year" to buffer
602  */
603 static char *
604 str_time(t)
605 	time_t t;
606 {
607 	static char buf[128];
608 
609 	if (t == 0) {
610 		int i = 0;
611 		for (;i < 20;) buf[i++] = ' ';
612 	} else {
613 		char *t0;
614 		t0 = ctime(&t);
615 		memcpy(buf, t0 + 4, 20);
616 	}
617 
618 	buf[20] = '\0';
619 
620 	return(buf);
621 }
622 
623 static void
624 str_lifetime_byte(x, str)
625 	struct sadb_lifetime *x;
626 	char *str;
627 {
628 	double y;
629 	char *unit;
630 	int w;
631 
632 	if (x == NULL) {
633 		printf("\t%s: 0(bytes)", str);
634 		return;
635 	}
636 
637 #if 0
638 	if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
639 		y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
640 		unit = "M";
641 		w = 1;
642 	} else if ((x->sadb_lifetime_bytes) / 1024) {
643 		y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
644 		unit = "K";
645 		w = 1;
646 	} else {
647 		y = (x->sadb_lifetime_bytes) * 1.0;
648 		unit = "";
649 		w = 0;
650 	}
651 #else
652 	y = (x->sadb_lifetime_bytes) * 1.0;
653 	unit = "";
654 	w = 0;
655 #endif
656 	printf("\t%s: %.*f(%sbytes)", str, w, y, unit);
657 }
658