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