xref: /netbsd-src/crypto/dist/ipsec-tools/src/libipsec/pfkey_dump.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: pfkey_dump.c,v 1.19 2011/05/26 21:50:02 drochner Exp $	*/
2 
3 /*	$KAME: pfkey_dump.c,v 1.45 2003/09/08 10:14:56 itojun Exp $	*/
4 
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 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include PATH_IPSEC_H
42 #include <net/pfkeyv2.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 #if defined(SADB_X_EALG_AES) && ! defined(SADB_X_EALG_AESCBC)
80 #define SADB_X_EALG_AESCBC  SADB_X_EALG_AES
81 #endif
82 
83 #define GETMSGSTR(str, num) \
84 do { \
85 	/*CONSTCOND*/ \
86 	if (sizeof((str)[0]) == 0 \
87 	 || num >= sizeof(str)/sizeof((str)[0])) \
88 		printf("%u ", (num)); \
89 	else if (strlen((str)[(num)]) == 0) \
90 		printf("%u ", (num)); \
91 	else \
92 		printf("%s ", (str)[(num)]); \
93 } while (/*CONSTCOND*/0)
94 
95 #define GETMSGV2S(v2s, num) \
96 do { \
97 	struct val2str *p;  \
98 	for (p = (v2s); p && p->str; p++) { \
99 		if (p->val == (num)) \
100 			break; \
101 	} \
102 	if (p && p->str) \
103 		printf("%s ", p->str); \
104 	else \
105 		printf("%u ", (num)); \
106 } while (/*CONSTCOND*/0)
107 
108 static char *str_ipaddr __P((struct sockaddr *));
109 static char *str_ipport __P((struct sockaddr *));
110 static char *str_prefport __P((u_int, u_int, u_int, u_int));
111 static void str_upperspec __P((u_int, u_int, u_int));
112 static char *str_time __P((time_t));
113 static void str_lifetime_byte __P((struct sadb_lifetime *, char *));
114 static void pfkey_sadump1(struct sadb_msg *, int);
115 static void pfkey_spdump1(struct sadb_msg *, int);
116 
117 struct val2str {
118 	int val;
119 	const char *str;
120 };
121 
122 /*
123  * Must to be re-written about following strings.
124  */
125 static char *str_satype[] = {
126 	"unspec",
127 	"unknown",
128 	"ah",
129 	"esp",
130 	"unknown",
131 	"rsvp",
132 	"ospfv2",
133 	"ripv2",
134 	"mip",
135 	"ipcomp",
136 	"policy",
137 	"tcp",
138 };
139 
140 static char *str_mode[] = {
141 	"any",
142 	"transport",
143 	"tunnel",
144 };
145 
146 static char *str_state[] = {
147 	"larval",
148 	"mature",
149 	"dying",
150 	"dead",
151 };
152 
153 static struct val2str str_alg_auth[] = {
154 	{ SADB_AALG_NONE, "none", },
155 	{ SADB_AALG_MD5HMAC, "hmac-md5", },
156 	{ SADB_AALG_SHA1HMAC, "hmac-sha1", },
157 	{ SADB_X_AALG_MD5, "md5", },
158 	{ SADB_X_AALG_SHA, "sha", },
159 	{ SADB_X_AALG_NULL, "null", },
160 #ifdef SADB_X_AALG_TCP_MD5
161 	{ SADB_X_AALG_TCP_MD5, "tcp-md5", },
162 #endif
163 #ifdef SADB_X_AALG_SHA2_256
164 	{ SADB_X_AALG_SHA2_256, "hmac-sha256", },
165 #endif
166 #ifdef SADB_X_AALG_SHA2_384
167 	{ SADB_X_AALG_SHA2_384, "hmac-sha384", },
168 #endif
169 #ifdef SADB_X_AALG_SHA2_512
170 	{ SADB_X_AALG_SHA2_512, "hmac-sha512", },
171 #endif
172 #ifdef SADB_X_AALG_RIPEMD160HMAC
173 	{ SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", },
174 #endif
175 #ifdef SADB_X_AALG_AES_XCBC_MAC
176 	{ SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", },
177 #endif
178 	{ -1, NULL, },
179 };
180 
181 static struct val2str str_alg_enc[] = {
182 	{ SADB_EALG_NONE, "none", },
183 	{ SADB_EALG_DESCBC, "des-cbc", },
184 	{ SADB_EALG_3DESCBC, "3des-cbc", },
185 	{ SADB_EALG_NULL, "null", },
186 #ifdef SADB_X_EALG_RC5CBC
187 	{ SADB_X_EALG_RC5CBC, "rc5-cbc", },
188 #endif
189 	{ SADB_X_EALG_CAST128CBC, "cast128-cbc", },
190 	{ SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
191 #ifdef SADB_X_EALG_AESCBC
192 	{ SADB_X_EALG_AESCBC, "aes-cbc", },
193 #endif
194 #ifdef SADB_X_EALG_TWOFISHCBC
195 	{ SADB_X_EALG_TWOFISHCBC, "twofish-cbc", },
196 #endif
197 #ifdef SADB_X_EALG_AESCTR
198 	{ SADB_X_EALG_AESCTR, "aes-ctr", },
199 #endif
200 #ifdef SADB_X_EALG_AESGCM16
201 	{ SADB_X_EALG_AESGCM16, "aes-gcm-16", },
202 #endif
203 #ifdef SADB_X_EALG_AESGMAC
204 	{ SADB_X_EALG_AESGMAC, "aes-gmac", },
205 #endif
206 #ifdef SADB_X_EALG_CAMELLIACBC
207 	{ SADB_X_EALG_CAMELLIACBC, "camellia-cbc", },
208 #endif
209 	{ -1, NULL, },
210 };
211 
212 static struct val2str str_alg_comp[] = {
213 	{ SADB_X_CALG_NONE, "none", },
214 	{ SADB_X_CALG_OUI, "oui", },
215 	{ SADB_X_CALG_DEFLATE, "deflate", },
216 	{ SADB_X_CALG_LZS, "lzs", },
217 	{ -1, NULL, },
218 };
219 
220 /*
221  * dump SADB_MSG formated.  For debugging, you should use kdebug_sadb().
222  */
223 
224 void
225 pfkey_sadump(m)
226 	struct sadb_msg *m;
227 {
228 	pfkey_sadump1(m, 0);
229 }
230 
231 void
232 pfkey_sadump_withports(m)
233 	struct sadb_msg *m;
234 {
235 	pfkey_sadump1(m, 1);
236 }
237 
238 void
239 pfkey_sadump1(m, withports)
240 	struct sadb_msg *m;
241 	int withports;
242 {
243 	caddr_t mhp[SADB_EXT_MAX + 1];
244 	struct sadb_sa *m_sa;
245 	struct sadb_x_sa2 *m_sa2;
246 	struct sadb_lifetime *m_lftc, *m_lfth, *m_lfts;
247 	struct sadb_address *m_saddr, *m_daddr;
248 #ifdef notdef
249 	struct sadb_address *m_paddr;
250 #endif
251 	struct sadb_key *m_auth, *m_enc;
252 #ifdef notdef
253 	struct sadb_ident *m_sid, *m_did;
254 	struct sadb_sens *m_sens;
255 #endif
256 #ifdef SADB_X_EXT_SEC_CTX
257 	struct sadb_x_sec_ctx *m_sec_ctx;
258 #endif
259 #ifdef SADB_X_EXT_NAT_T_TYPE
260 	struct sadb_x_nat_t_type *natt_type;
261 	struct sadb_x_nat_t_port *natt_sport, *natt_dport;
262 	struct sadb_address *natt_oa;
263 
264 	int use_natt = 0;
265 #endif
266 	struct sockaddr *sa;
267 
268 	/* check pfkey message. */
269 	if (pfkey_align(m, mhp)) {
270 		printf("%s\n", ipsec_strerror());
271 		return;
272 	}
273 	if (pfkey_check(mhp)) {
274 		printf("%s\n", ipsec_strerror());
275 		return;
276 	}
277 
278 	m_sa = (void *)mhp[SADB_EXT_SA];
279 	m_sa2 = (void *)mhp[SADB_X_EXT_SA2];
280 	m_lftc = (void *)mhp[SADB_EXT_LIFETIME_CURRENT];
281 	m_lfth = (void *)mhp[SADB_EXT_LIFETIME_HARD];
282 	m_lfts = (void *)mhp[SADB_EXT_LIFETIME_SOFT];
283 	m_saddr = (void *)mhp[SADB_EXT_ADDRESS_SRC];
284 	m_daddr = (void *)mhp[SADB_EXT_ADDRESS_DST];
285 #ifdef notdef
286 	m_paddr = (void *)mhp[SADB_EXT_ADDRESS_PROXY];
287 #endif
288 	m_auth = (void *)mhp[SADB_EXT_KEY_AUTH];
289 	m_enc = (void *)mhp[SADB_EXT_KEY_ENCRYPT];
290 #ifdef notdef
291 	m_sid = (void *)mhp[SADB_EXT_IDENTITY_SRC];
292 	m_did = (void *)mhp[SADB_EXT_IDENTITY_DST];
293 	m_sens = (void *)mhp[SADB_EXT_SENSITIVITY];
294 #endif
295 #ifdef SADB_X_EXT_SEC_CTX
296 	m_sec_ctx = (struct sadb_x_sec_ctx *)mhp[SADB_X_EXT_SEC_CTX];
297 #endif
298 #ifdef SADB_X_EXT_NAT_T_TYPE
299 	natt_type = (void *)mhp[SADB_X_EXT_NAT_T_TYPE];
300 	natt_sport = (void *)mhp[SADB_X_EXT_NAT_T_SPORT];
301 	natt_dport = (void *)mhp[SADB_X_EXT_NAT_T_DPORT];
302 	natt_oa = (void *)mhp[SADB_X_EXT_NAT_T_OA];
303 
304 	if (natt_type && natt_type->sadb_x_nat_t_type_type)
305 		use_natt = 1;
306 #endif
307 	/* source address */
308 	if (m_saddr == NULL) {
309 		printf("no ADDRESS_SRC extension.\n");
310 		return;
311 	}
312 	sa = (void *)(m_saddr + 1);
313 	if (withports)
314 		printf("%s[%s]", str_ipaddr(sa), str_ipport(sa));
315 	else
316 		printf("%s", str_ipaddr(sa));
317 #ifdef SADB_X_EXT_NAT_T_TYPE
318 	if (use_natt && natt_sport)
319 		printf("[%u]", ntohs(natt_sport->sadb_x_nat_t_port_port));
320 #endif
321 	printf(" ");
322 
323 	/* destination address */
324 	if (m_daddr == NULL) {
325 		printf(" no ADDRESS_DST extension.\n");
326 		return;
327 	}
328 	sa = (void *)(m_daddr + 1);
329 	if (withports)
330 		printf("%s[%s]", str_ipaddr(sa), str_ipport(sa));
331 	else
332 		printf("%s", str_ipaddr(sa));
333 #ifdef SADB_X_EXT_NAT_T_TYPE
334 	if (use_natt && natt_dport)
335 		printf("[%u]", ntohs(natt_dport->sadb_x_nat_t_port_port));
336 #endif
337 	printf(" ");
338 
339 	/* SA type */
340 	if (m_sa == NULL) {
341 		printf("no SA extension.\n");
342 		return;
343 	}
344 	if (m_sa2 == NULL) {
345 		printf("no SA2 extension.\n");
346 		return;
347 	}
348 	printf("\n\t");
349 
350 #ifdef SADB_X_EXT_NAT_T_TYPE
351 	if (use_natt && m->sadb_msg_satype == SADB_SATYPE_ESP)
352 		printf("esp-udp ");
353 	else if (use_natt)
354 		printf("natt+");
355 
356 	if (!use_natt || m->sadb_msg_satype != SADB_SATYPE_ESP)
357 #endif
358 	GETMSGSTR(str_satype, m->sadb_msg_satype);
359 
360 	printf("mode=");
361 	GETMSGSTR(str_mode, m_sa2->sadb_x_sa2_mode);
362 
363 	printf("spi=%u(0x%08x) reqid=%u(0x%08x)\n",
364 		(u_int32_t)ntohl(m_sa->sadb_sa_spi),
365 		(u_int32_t)ntohl(m_sa->sadb_sa_spi),
366 		(u_int32_t)m_sa2->sadb_x_sa2_reqid,
367 		(u_int32_t)m_sa2->sadb_x_sa2_reqid);
368 
369 #ifdef SADB_X_EXT_NAT_T_TYPE
370 	/* other NAT-T information */
371 	if (use_natt && natt_oa)
372 		printf("\tNAT OA=%s\n",
373 		       str_ipaddr((void *)(natt_oa + 1)));
374 #endif
375 
376 	/* encryption key */
377 	if (m->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
378 		printf("\tC: ");
379 		GETMSGV2S(str_alg_comp, m_sa->sadb_sa_encrypt);
380 	} else if (m->sadb_msg_satype == SADB_SATYPE_ESP) {
381 		if (m_enc != NULL) {
382 			printf("\tE: ");
383 			GETMSGV2S(str_alg_enc, m_sa->sadb_sa_encrypt);
384 			ipsec_hexdump((caddr_t)(void *)m_enc + sizeof(*m_enc),
385 				      m_enc->sadb_key_bits / 8);
386 			printf("\n");
387 		}
388 	}
389 
390 	/* authentication key */
391 	if (m_auth != NULL) {
392 		printf("\tA: ");
393 		GETMSGV2S(str_alg_auth, m_sa->sadb_sa_auth);
394 		ipsec_hexdump((caddr_t)(void *)m_auth + sizeof(*m_auth),
395 		              m_auth->sadb_key_bits / 8);
396 		printf("\n");
397 	}
398 
399 	/* replay windoe size & flags */
400 	printf("\tseq=0x%08x replay=%u flags=0x%08x ",
401 		m_sa2->sadb_x_sa2_sequence,
402 		m_sa->sadb_sa_replay,
403 		m_sa->sadb_sa_flags);
404 
405 	/* state */
406 	printf("state=");
407 	GETMSGSTR(str_state, m_sa->sadb_sa_state);
408 	printf("\n");
409 
410 	/* lifetime */
411 	if (m_lftc != NULL) {
412 		time_t tmp_time = time(0);
413 
414 		printf("\tcreated: %s",
415 			str_time((long)m_lftc->sadb_lifetime_addtime));
416 		printf("\tcurrent: %s\n", str_time(tmp_time));
417 		printf("\tdiff: %lu(s)",
418 			(u_long)(m_lftc->sadb_lifetime_addtime == 0 ?
419 			0 : (tmp_time - m_lftc->sadb_lifetime_addtime)));
420 
421 		printf("\thard: %lu(s)",
422 			(u_long)(m_lfth == NULL ?
423 			0 : m_lfth->sadb_lifetime_addtime));
424 		printf("\tsoft: %lu(s)\n",
425 			(u_long)(m_lfts == NULL ?
426 			0 : m_lfts->sadb_lifetime_addtime));
427 
428 		printf("\tlast: %s",
429 			str_time((long)m_lftc->sadb_lifetime_usetime));
430 		printf("\thard: %lu(s)",
431 			(u_long)(m_lfth == NULL ?
432 			0 : m_lfth->sadb_lifetime_usetime));
433 		printf("\tsoft: %lu(s)\n",
434 			(u_long)(m_lfts == NULL ?
435 			0 : m_lfts->sadb_lifetime_usetime));
436 
437 		str_lifetime_byte(m_lftc, "current");
438 		str_lifetime_byte(m_lfth, "hard");
439 		str_lifetime_byte(m_lfts, "soft");
440 		printf("\n");
441 
442 		printf("\tallocated: %lu",
443 			(unsigned long)m_lftc->sadb_lifetime_allocations);
444 		printf("\thard: %lu",
445 			(u_long)(m_lfth == NULL ?
446 			0 : m_lfth->sadb_lifetime_allocations));
447 		printf("\tsoft: %lu\n",
448 			(u_long)(m_lfts == NULL ?
449 			0 : m_lfts->sadb_lifetime_allocations));
450 	}
451 
452 #ifdef SADB_X_EXT_SEC_CTX
453 	if (m_sec_ctx != NULL) {
454 		printf("\tsecurity context doi: %u\n",
455 					m_sec_ctx->sadb_x_ctx_doi);
456 		printf("\tsecurity context algorithm: %u\n",
457 					m_sec_ctx->sadb_x_ctx_alg);
458 		printf("\tsecurity context length: %u\n",
459 					m_sec_ctx->sadb_x_ctx_len);
460 		printf("\tsecurity context: %s\n",
461 			(char *)m_sec_ctx + sizeof(struct sadb_x_sec_ctx));
462 	}
463 #endif
464 
465 	printf("\tsadb_seq=%lu pid=%lu ",
466 		(u_long)m->sadb_msg_seq,
467 		(u_long)m->sadb_msg_pid);
468 
469 	/* XXX DEBUG */
470 	printf("refcnt=%u\n", m->sadb_msg_reserved);
471 
472 	return;
473 }
474 
475 void
476 pfkey_spdump(m)
477 	struct sadb_msg *m;
478 {
479 	pfkey_spdump1(m, 0);
480 }
481 
482 void
483 pfkey_spdump_withports(m)
484 	struct sadb_msg *m;
485 {
486 	pfkey_spdump1(m, 1);
487 }
488 
489 static void
490 pfkey_spdump1(m, withports)
491 	struct sadb_msg *m;
492 	int withports;
493 {
494 	char pbuf[NI_MAXSERV];
495 	caddr_t mhp[SADB_EXT_MAX + 1];
496 	struct sadb_address *m_saddr, *m_daddr;
497 #ifdef SADB_X_EXT_TAG
498 	struct sadb_x_tag *m_tag;
499 #endif
500 	struct sadb_x_policy *m_xpl;
501 	struct sadb_lifetime *m_lftc = NULL, *m_lfth = NULL;
502 #ifdef SADB_X_EXT_SEC_CTX
503 	struct sadb_x_sec_ctx *m_sec_ctx;
504 #endif
505 	struct sockaddr *sa;
506 	u_int16_t sport = 0, dport = 0;
507 
508 	/* check pfkey message. */
509 	if (pfkey_align(m, mhp)) {
510 		printf("%s\n", ipsec_strerror());
511 		return;
512 	}
513 	if (pfkey_check(mhp)) {
514 		printf("%s\n", ipsec_strerror());
515 		return;
516 	}
517 
518 	m_saddr = (void *)mhp[SADB_EXT_ADDRESS_SRC];
519 	m_daddr = (void *)mhp[SADB_EXT_ADDRESS_DST];
520 #ifdef SADB_X_EXT_TAG
521 	m_tag = (void *)mhp[SADB_X_EXT_TAG];
522 #endif
523 	m_xpl = (void *)mhp[SADB_X_EXT_POLICY];
524 	m_lftc = (void *)mhp[SADB_EXT_LIFETIME_CURRENT];
525 	m_lfth = (void *)mhp[SADB_EXT_LIFETIME_HARD];
526 
527 #ifdef SADB_X_EXT_SEC_CTX
528 	m_sec_ctx = (struct sadb_x_sec_ctx *)mhp[SADB_X_EXT_SEC_CTX];
529 #endif
530 #ifdef __linux__
531 	/* *bsd indicates per-socket policies by omiting src and dst
532 	 * extensions. Linux always includes them, but we can catch it
533 	 * by checkin for policy id.
534 	 */
535 	if (m_xpl->sadb_x_policy_id % 8 >= 3) {
536 		printf("(per-socket policy) ");
537 	} else
538 #endif
539 	if (m_saddr && m_daddr) {
540 		/* source address */
541 		sa = (void *)(m_saddr + 1);
542 		switch (sa->sa_family) {
543 		case AF_INET:
544 		case AF_INET6:
545 			if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), NULL,
546 			    0, pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
547 				sport = 0;	/*XXX*/
548 			else
549 				sport = atoi(pbuf);
550 			printf("%s%s ", str_ipaddr(sa),
551 				str_prefport((u_int)sa->sa_family,
552 				    (u_int)m_saddr->sadb_address_prefixlen,
553 				    (u_int)sport,
554 				    (u_int)m_saddr->sadb_address_proto));
555 			break;
556 		default:
557 			printf("unknown-af ");
558 			break;
559 		}
560 
561 		/* destination address */
562 		sa = (void *)(m_daddr + 1);
563 		switch (sa->sa_family) {
564 		case AF_INET:
565 		case AF_INET6:
566 			if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), NULL,
567 			    0, pbuf, sizeof(pbuf), NI_NUMERICSERV) != 0)
568 				dport = 0;	/*XXX*/
569 			else
570 				dport = atoi(pbuf);
571 			printf("%s%s ", str_ipaddr(sa),
572 				str_prefport((u_int)sa->sa_family,
573 				    (u_int)m_daddr->sadb_address_prefixlen,
574 				    (u_int)dport,
575 				    (u_int)m_saddr->sadb_address_proto));
576 			break;
577 		default:
578 			printf("unknown-af ");
579 			break;
580 		}
581 
582 		/* upper layer protocol */
583 		if (m_saddr->sadb_address_proto !=
584 		    m_daddr->sadb_address_proto) {
585 			printf("upper layer protocol mismatched.\n");
586 			return;
587 		}
588 		str_upperspec((u_int)m_saddr->sadb_address_proto, (u_int)sport,
589 		    (u_int)dport);
590 	}
591 #ifdef SADB_X_EXT_TAG
592 	else if (m_tag)
593 		printf("tagged \"%s\" ", m_tag->sadb_x_tag_name);
594 #endif
595 	else
596 		printf("(no selector, probably per-socket policy) ");
597 
598 	/* policy */
599     {
600 	char *d_xpl;
601 
602 	if (m_xpl == NULL) {
603 		printf("no X_POLICY extension.\n");
604 		return;
605 	}
606 	if (withports)
607 		d_xpl = ipsec_dump_policy_withports(m_xpl, "\n\t");
608 	else
609 		d_xpl = ipsec_dump_policy((ipsec_policy_t)m_xpl, "\n\t");
610 
611 	if (!d_xpl)
612 		printf("\n\tPolicy:[%s]\n", ipsec_strerror());
613 	else {
614 		/* dump SPD */
615 		printf("\n\t%s\n", d_xpl);
616 		free(d_xpl);
617 	}
618     }
619 
620 	/* lifetime */
621 	if (m_lftc) {
622 		printf("\tcreated: %s  ",
623 			str_time((long)m_lftc->sadb_lifetime_addtime));
624 		printf("lastused: %s\n",
625 			str_time((long)m_lftc->sadb_lifetime_usetime));
626 	}
627 	if (m_lfth) {
628 		printf("\tlifetime: %lu(s) ",
629 			(u_long)m_lfth->sadb_lifetime_addtime);
630 		printf("validtime: %lu(s)\n",
631 			(u_long)m_lfth->sadb_lifetime_usetime);
632 	}
633 
634 #ifdef SADB_X_EXT_SEC_CTX
635 	if (m_sec_ctx != NULL) {
636 		printf("\tsecurity context doi: %u\n",
637 					m_sec_ctx->sadb_x_ctx_doi);
638 		printf("\tsecurity context algorithm: %u\n",
639 					m_sec_ctx->sadb_x_ctx_alg);
640 		printf("\tsecurity context length: %u\n",
641 					m_sec_ctx->sadb_x_ctx_len);
642 		printf("\tsecurity context: %s\n",
643 			(char *)m_sec_ctx + sizeof(struct sadb_x_sec_ctx));
644 	}
645 #endif
646 
647 	printf("\tspid=%ld seq=%ld pid=%ld\n",
648 		(u_long)m_xpl->sadb_x_policy_id,
649 		(u_long)m->sadb_msg_seq,
650 		(u_long)m->sadb_msg_pid);
651 
652 	/* XXX TEST */
653 	printf("\trefcnt=%u\n", m->sadb_msg_reserved);
654 
655 	return;
656 }
657 
658 /*
659  * set "ipaddress" to buffer.
660  */
661 static char *
662 str_ipaddr(sa)
663 	struct sockaddr *sa;
664 {
665 	static char buf[NI_MAXHOST];
666 	const int niflag = NI_NUMERICHOST;
667 
668 	if (sa == NULL)
669 		return "";
670 
671 	if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), buf, sizeof(buf),
672 	    NULL, 0, niflag) == 0)
673 		return buf;
674 	return NULL;
675 }
676 
677 /*
678  * set "port" to buffer.
679  */
680 static char *
681 str_ipport(sa)
682 	struct sockaddr *sa;
683 {
684 	static char buf[NI_MAXHOST];
685 	const int niflag = NI_NUMERICSERV;
686 
687 	if (sa == NULL)
688 		return "";
689 
690 	if (getnameinfo(sa, (socklen_t)sysdep_sa_len(sa), NULL, 0,
691 	    buf, sizeof(buf), niflag) == 0)
692 		return buf;
693 	return NULL;
694 }
695 
696 
697 /*
698  * set "/prefix[port number]" to buffer.
699  */
700 static char *
701 str_prefport(family, pref, port, ulp)
702 	u_int family, pref, port, ulp;
703 {
704 	static char buf[128];
705 	char prefbuf[128];
706 	char portbuf[128];
707 	int plen;
708 
709 	switch (family) {
710 	case AF_INET:
711 		plen = sizeof(struct in_addr) << 3;
712 		break;
713 	case AF_INET6:
714 		plen = sizeof(struct in6_addr) << 3;
715 		break;
716 	default:
717 		return "?";
718 	}
719 
720 	if (pref == plen)
721 		prefbuf[0] = '\0';
722 	else
723 		snprintf(prefbuf, sizeof(prefbuf), "/%u", pref);
724 
725 	switch (ulp) {
726 	case IPPROTO_ICMP:
727 	case IPPROTO_ICMPV6:
728 	case IPPROTO_MH:
729 	case IPPROTO_GRE:
730 		memset(portbuf, 0, sizeof(portbuf));
731 		break;
732 	default:
733 		if (port == IPSEC_PORT_ANY)
734 			strcpy(portbuf, "[any]");
735 		else
736 			snprintf(portbuf, sizeof(portbuf), "[%u]", port);
737 		break;
738 	}
739 
740 	snprintf(buf, sizeof(buf), "%s%s", prefbuf, portbuf);
741 
742 	return buf;
743 }
744 
745 static void
746 str_upperspec(ulp, p1, p2)
747 	u_int ulp, p1, p2;
748 {
749 	struct protoent *ent;
750 
751 	ent = getprotobynumber((int)ulp);
752 	if (ent)
753 		printf("%s", ent->p_name);
754 	else
755 		printf("%u", ulp);
756 
757 	if (p1 == IPSEC_PORT_ANY && p2 == IPSEC_PORT_ANY)
758 		return;
759 
760 	switch (ulp) {
761 	case IPPROTO_ICMP:
762 	case IPPROTO_ICMPV6:
763 	case IPPROTO_MH:
764 		printf(" %u,%u", p1, p2);
765 		break;
766 	case IPPROTO_GRE:
767 		printf(" %u", (p1 << 16) + p2);
768 		break;
769 	}
770 }
771 
772 /*
773  * set "Mon Day Time Year" to buffer
774  */
775 static char *
776 str_time(t)
777 	time_t t;
778 {
779 	static char buf[128];
780 
781 	if (t == 0) {
782 		int i = 0;
783 		for (;i < 20;) buf[i++] = ' ';
784 	} else {
785 		char *t0;
786 		if ((t0 = ctime(&t)) == NULL)
787 			memset(buf, '?', 20);
788 		else
789 			memcpy(buf, t0 + 4, 20);
790 	}
791 
792 	buf[20] = '\0';
793 
794 	return(buf);
795 }
796 
797 static void
798 str_lifetime_byte(x, str)
799 	struct sadb_lifetime *x;
800 	char *str;
801 {
802 	double y;
803 	char *unit;
804 	int w;
805 
806 	if (x == NULL) {
807 		printf("\t%s: 0(bytes)", str);
808 		return;
809 	}
810 
811 #if 0
812 	if ((x->sadb_lifetime_bytes) / 1024 / 1024) {
813 		y = (x->sadb_lifetime_bytes) * 1.0 / 1024 / 1024;
814 		unit = "M";
815 		w = 1;
816 	} else if ((x->sadb_lifetime_bytes) / 1024) {
817 		y = (x->sadb_lifetime_bytes) * 1.0 / 1024;
818 		unit = "K";
819 		w = 1;
820 	} else {
821 		y = (x->sadb_lifetime_bytes) * 1.0;
822 		unit = "";
823 		w = 0;
824 	}
825 #else
826 	y = (x->sadb_lifetime_bytes) * 1.0;
827 	unit = "";
828 	w = 0;
829 #endif
830 	printf("\t%s: %.*f(%sbytes)", str, w, y, unit);
831 }
832