ipsec_dump_policy.c revision 62583
1/*	$FreeBSD: head/lib/libipsec/ipsec_dump_policy.c 62583 2000-07-04 16:22:05Z itojun $	*/
2/*	$KAME: ipsec_dump_policy.c,v 1.11 2000/05/07 05:29:47 itojun Exp $	*/
3
4/*
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/types.h>
34#include <sys/param.h>
35#include <sys/socket.h>
36
37#include <netkey/key_var.h>
38#include <netinet/in.h>
39#include <netinet6/ipsec.h>
40
41#include <arpa/inet.h>
42
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <netdb.h>
47
48#include "ipsec_strerror.h"
49
50static const char *ipsp_dir_strs[] = {
51	"any", "in", "out",
52};
53
54static const char *ipsp_policy_strs[] = {
55	"discard", "none", "ipsec", "entrust", "bypass",
56};
57
58static char *ipsec_dump_ipsecrequest __P((char *, size_t,
59	struct sadb_x_ipsecrequest *, size_t));
60static int set_addresses __P((char *, size_t, struct sockaddr *,
61	struct sockaddr *));
62static char *set_address __P((char *, size_t, struct sockaddr *));
63
64/*
65 * policy is sadb_x_policy buffer.
66 * Must call free() later.
67 * When delimiter == NULL, alternatively ' '(space) is applied.
68 */
69char *
70ipsec_dump_policy(policy, delimiter)
71	caddr_t policy;
72	char *delimiter;
73{
74	struct sadb_x_policy *xpl = (struct sadb_x_policy *)policy;
75	struct sadb_x_ipsecrequest *xisr;
76	size_t off, buflen;
77	char *buf;
78	char isrbuf[1024];
79	char *newbuf;
80
81	/* sanity check */
82	if (policy == NULL)
83		return NULL;
84	if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
85		__ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
86		return NULL;
87	}
88
89	/* set delimiter */
90	if (delimiter == NULL)
91		delimiter = " ";
92
93	switch (xpl->sadb_x_policy_dir) {
94	case IPSEC_DIR_ANY:
95	case IPSEC_DIR_INBOUND:
96	case IPSEC_DIR_OUTBOUND:
97		break;
98	default:
99		__ipsec_errcode = EIPSEC_INVAL_DIR;
100		return NULL;
101	}
102
103	switch (xpl->sadb_x_policy_type) {
104	case IPSEC_POLICY_DISCARD:
105	case IPSEC_POLICY_NONE:
106	case IPSEC_POLICY_IPSEC:
107	case IPSEC_POLICY_BYPASS:
108	case IPSEC_POLICY_ENTRUST:
109		break;
110	default:
111		__ipsec_errcode = EIPSEC_INVAL_POLICY;
112		return NULL;
113	}
114
115	buflen = strlen(ipsp_dir_strs[xpl->sadb_x_policy_dir])
116		+ 1	/* space */
117		+ strlen(ipsp_policy_strs[xpl->sadb_x_policy_type])
118		+ 1;	/* NUL */
119
120	if ((buf = malloc(buflen)) == NULL) {
121		__ipsec_errcode = EIPSEC_NO_BUFS;
122		return NULL;
123	}
124	snprintf(buf, buflen, "%s %s", ipsp_dir_strs[xpl->sadb_x_policy_dir],
125	    ipsp_policy_strs[xpl->sadb_x_policy_type]);
126
127	if (xpl->sadb_x_policy_type != IPSEC_POLICY_IPSEC) {
128		__ipsec_errcode = EIPSEC_NO_ERROR;
129		return buf;
130	}
131
132	/* count length of buffer for use */
133	off = sizeof(*xpl);
134	while (off < PFKEY_EXTLEN(xpl)) {
135		xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off);
136		off += xisr->sadb_x_ipsecrequest_len;
137	}
138
139	/* validity check */
140	if (off != PFKEY_EXTLEN(xpl)) {
141		__ipsec_errcode = EIPSEC_INVAL_SADBMSG;
142		free(buf);
143		return NULL;
144	}
145
146	off = sizeof(*xpl);
147	while (off < PFKEY_EXTLEN(xpl)) {
148		xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off);
149
150		if (ipsec_dump_ipsecrequest(isrbuf, sizeof(isrbuf), xisr,
151		    PFKEY_EXTLEN(xpl) - off) == NULL) {
152			free(buf);
153			return NULL;
154		}
155
156		buflen = strlen(buf) + strlen(delimiter) + strlen(isrbuf) + 1;
157		newbuf = (char *)realloc(buf, buflen);
158		if (newbuf == NULL) {
159			__ipsec_errcode = EIPSEC_NO_BUFS;
160			free(buf);
161			return NULL;
162		}
163		buf = newbuf;
164		snprintf(buf, buflen, "%s%s%s", buf, delimiter, isrbuf);
165
166		off += xisr->sadb_x_ipsecrequest_len;
167	}
168
169	__ipsec_errcode = EIPSEC_NO_ERROR;
170	return buf;
171}
172
173static char *
174ipsec_dump_ipsecrequest(buf, len, xisr, bound)
175	char *buf;
176	size_t len;
177	struct sadb_x_ipsecrequest *xisr;
178	size_t bound;	/* boundary */
179{
180	const char *proto, *mode, *level;
181	char abuf[NI_MAXHOST * 2 + 2];
182
183	if (xisr->sadb_x_ipsecrequest_len > bound) {
184		__ipsec_errcode = EIPSEC_INVAL_PROTO;
185		return NULL;
186	}
187
188	switch (xisr->sadb_x_ipsecrequest_proto) {
189	case IPPROTO_ESP:
190		proto = "esp";
191		break;
192	case IPPROTO_AH:
193		proto = "ah";
194		break;
195	case IPPROTO_IPCOMP:
196		proto = "ipcomp";
197		break;
198	default:
199		__ipsec_errcode = EIPSEC_INVAL_PROTO;
200		return NULL;
201	}
202
203	switch (xisr->sadb_x_ipsecrequest_mode) {
204	case IPSEC_MODE_ANY:
205		mode = "any";
206		break;
207	case IPSEC_MODE_TRANSPORT:
208		mode = "transport";
209		break;
210	case IPSEC_MODE_TUNNEL:
211		mode = "tunnel";
212		break;
213	default:
214		__ipsec_errcode = EIPSEC_INVAL_MODE;
215		return NULL;
216	}
217
218	abuf[0] = '\0';
219	if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
220		struct sockaddr *sa1, *sa2;
221		caddr_t p;
222
223		p = (caddr_t)(xisr + 1);
224		sa1 = (struct sockaddr *)p;
225		sa2 = (struct sockaddr *)(p + sa1->sa_len);
226		if (sizeof(*xisr) + sa1->sa_len + sa2->sa_len !=
227		    xisr->sadb_x_ipsecrequest_len) {
228			__ipsec_errcode = EIPSEC_INVAL_ADDRESS;
229			return NULL;
230		}
231		if (set_addresses(abuf, sizeof(abuf), sa1, sa2) != 0) {
232			__ipsec_errcode = EIPSEC_INVAL_ADDRESS;
233			return NULL;
234		}
235	}
236
237	switch (xisr->sadb_x_ipsecrequest_level) {
238	case IPSEC_LEVEL_DEFAULT:
239		level = "default";
240		break;
241	case IPSEC_LEVEL_USE:
242		level = "use";
243		break;
244	case IPSEC_LEVEL_REQUIRE:
245		level = "require";
246		break;
247	case IPSEC_LEVEL_UNIQUE:
248		level = "unique";
249		break;
250	default:
251		__ipsec_errcode = EIPSEC_INVAL_LEVEL;
252		return NULL;
253	}
254
255	if (xisr->sadb_x_ipsecrequest_reqid == 0)
256		snprintf(buf, len, "%s/%s/%s/%s", proto, mode, abuf, level);
257	else {
258		int ch;
259
260		if (xisr->sadb_x_ipsecrequest_reqid > IPSEC_MANUAL_REQID_MAX)
261			ch = '#';
262		else
263			ch = ':';
264		snprintf(buf, len, "%s/%s/%s/%s%c%d", proto, mode, abuf, level,
265		    ch, xisr->sadb_x_ipsecrequest_reqid);
266	}
267
268	return buf;
269}
270
271static int
272set_addresses(buf, len, sa1, sa2)
273	char *buf;
274	size_t len;
275	struct sockaddr *sa1;
276	struct sockaddr *sa2;
277{
278	char tmp1[NI_MAXHOST], tmp2[NI_MAXHOST];
279
280	if (set_address(tmp1, sizeof(tmp1), sa1) == NULL ||
281	    set_address(tmp2, sizeof(tmp2), sa2) == NULL)
282		return -1;
283	if (strlen(tmp1) + 1 + strlen(tmp2) + 1 > len)
284		return -1;
285	snprintf(buf, len, "%s-%s", tmp1, tmp2);
286	return 0;
287}
288
289static char *
290set_address(buf, len, sa)
291	char *buf;
292	size_t len;
293	struct sockaddr *sa;
294{
295#ifdef NI_WITHSCOPEID
296	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
297#else
298	const int niflags = NI_NUMERICHOST;
299#endif
300
301	if (len < 1)
302		return NULL;
303	buf[0] = '\0';
304	if (getnameinfo(sa, sa->sa_len, buf, len, NULL, 0, niflags) != 0)
305		return NULL;
306	return buf;
307}
308