ipsec_dump_policy.c revision 216369
1/*	$KAME: ipsec_dump_policy.c,v 1.13 2002/06/27 14:35:11 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: head/lib/libipsec/ipsec_dump_policy.c 216369 2010-12-11 08:07:21Z kevlo $");
34
35#include <sys/types.h>
36#include <sys/param.h>
37#include <sys/socket.h>
38
39#include <netipsec/key_var.h>
40#include <netinet/in.h>
41#include <netipsec/ipsec.h>
42
43#include <arpa/inet.h>
44
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <netdb.h>
49
50#include "ipsec_strerror.h"
51
52static const char *ipsp_dir_strs[] = {
53	"any", "in", "out",
54};
55
56static const char *ipsp_policy_strs[] = {
57	"discard", "none", "ipsec", "entrust", "bypass",
58};
59
60static char *ipsec_dump_ipsecrequest(char *, size_t,
61	struct sadb_x_ipsecrequest *, size_t);
62static int set_addresses(char *, size_t, struct sockaddr *, struct sockaddr *);
63static char *set_address(char *, size_t, struct sockaddr *);
64
65/*
66 * policy is sadb_x_policy buffer.
67 * Must call free() later.
68 * When delimiter == NULL, alternatively ' '(space) is applied.
69 */
70char *
71ipsec_dump_policy(policy, delimiter)
72	caddr_t policy;
73	char *delimiter;
74{
75	struct sadb_x_policy *xpl = (struct sadb_x_policy *)policy;
76	struct sadb_x_ipsecrequest *xisr;
77	size_t off, buflen;
78	char *buf;
79	char isrbuf[1024];
80	char *newbuf;
81
82	/* sanity check */
83	if (policy == NULL)
84		return NULL;
85	if (xpl->sadb_x_policy_exttype != SADB_X_EXT_POLICY) {
86		__ipsec_errcode = EIPSEC_INVAL_EXTTYPE;
87		return NULL;
88	}
89
90	/* set delimiter */
91	if (delimiter == NULL)
92		delimiter = " ";
93
94	switch (xpl->sadb_x_policy_dir) {
95	case IPSEC_DIR_ANY:
96	case IPSEC_DIR_INBOUND:
97	case IPSEC_DIR_OUTBOUND:
98		break;
99	default:
100		__ipsec_errcode = EIPSEC_INVAL_DIR;
101		return NULL;
102	}
103
104	switch (xpl->sadb_x_policy_type) {
105	case IPSEC_POLICY_DISCARD:
106	case IPSEC_POLICY_NONE:
107	case IPSEC_POLICY_IPSEC:
108	case IPSEC_POLICY_BYPASS:
109	case IPSEC_POLICY_ENTRUST:
110		break;
111	default:
112		__ipsec_errcode = EIPSEC_INVAL_POLICY;
113		return NULL;
114	}
115
116	buflen = strlen(ipsp_dir_strs[xpl->sadb_x_policy_dir])
117		+ 1	/* space */
118		+ strlen(ipsp_policy_strs[xpl->sadb_x_policy_type])
119		+ 1;	/* NUL */
120
121	if ((buf = malloc(buflen)) == NULL) {
122		__ipsec_errcode = EIPSEC_NO_BUFS;
123		return NULL;
124	}
125	snprintf(buf, buflen, "%s %s", ipsp_dir_strs[xpl->sadb_x_policy_dir],
126	    ipsp_policy_strs[xpl->sadb_x_policy_type]);
127
128	if (xpl->sadb_x_policy_type != IPSEC_POLICY_IPSEC) {
129		__ipsec_errcode = EIPSEC_NO_ERROR;
130		return buf;
131	}
132
133	/* count length of buffer for use */
134	off = sizeof(*xpl);
135	while (off < PFKEY_EXTLEN(xpl)) {
136		xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off);
137		off += xisr->sadb_x_ipsecrequest_len;
138	}
139
140	/* validity check */
141	if (off != PFKEY_EXTLEN(xpl)) {
142		__ipsec_errcode = EIPSEC_INVAL_SADBMSG;
143		free(buf);
144		return NULL;
145	}
146
147	off = sizeof(*xpl);
148	while (off < PFKEY_EXTLEN(xpl)) {
149		xisr = (struct sadb_x_ipsecrequest *)((caddr_t)xpl + off);
150
151		if (ipsec_dump_ipsecrequest(isrbuf, sizeof(isrbuf), xisr,
152		    PFKEY_EXTLEN(xpl) - off) == NULL) {
153			free(buf);
154			return NULL;
155		}
156
157		buflen = strlen(buf) + strlen(delimiter) + strlen(isrbuf) + 1;
158		newbuf = (char *)realloc(buf, buflen);
159		if (newbuf == NULL) {
160			__ipsec_errcode = EIPSEC_NO_BUFS;
161			free(buf);
162			return NULL;
163		}
164		buf = newbuf;
165		snprintf(buf + strlen(buf), buflen - strlen(buf),
166		    "%s%s", delimiter, isrbuf);
167
168		off += xisr->sadb_x_ipsecrequest_len;
169	}
170
171	__ipsec_errcode = EIPSEC_NO_ERROR;
172	return buf;
173}
174
175static char *
176ipsec_dump_ipsecrequest(buf, len, xisr, bound)
177	char *buf;
178	size_t len;
179	struct sadb_x_ipsecrequest *xisr;
180	size_t bound;	/* boundary */
181{
182	const char *proto, *mode, *level;
183	char abuf[NI_MAXHOST * 2 + 2];
184
185	if (xisr->sadb_x_ipsecrequest_len > bound) {
186		__ipsec_errcode = EIPSEC_INVAL_PROTO;
187		return NULL;
188	}
189
190	switch (xisr->sadb_x_ipsecrequest_proto) {
191	case IPPROTO_ESP:
192		proto = "esp";
193		break;
194	case IPPROTO_AH:
195		proto = "ah";
196		break;
197	case IPPROTO_IPCOMP:
198		proto = "ipcomp";
199		break;
200	case IPPROTO_TCP:
201		proto = "tcp";
202	default:
203		__ipsec_errcode = EIPSEC_INVAL_PROTO;
204		return NULL;
205	}
206
207	switch (xisr->sadb_x_ipsecrequest_mode) {
208	case IPSEC_MODE_ANY:
209		mode = "any";
210		break;
211	case IPSEC_MODE_TRANSPORT:
212		mode = "transport";
213		break;
214	case IPSEC_MODE_TUNNEL:
215		mode = "tunnel";
216		break;
217	default:
218		__ipsec_errcode = EIPSEC_INVAL_MODE;
219		return NULL;
220	}
221
222	abuf[0] = '\0';
223	if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
224		struct sockaddr *sa1, *sa2;
225		caddr_t p;
226
227		p = (caddr_t)(xisr + 1);
228		sa1 = (struct sockaddr *)p;
229		sa2 = (struct sockaddr *)(p + sa1->sa_len);
230		if (sizeof(*xisr) + sa1->sa_len + sa2->sa_len !=
231		    xisr->sadb_x_ipsecrequest_len) {
232			__ipsec_errcode = EIPSEC_INVAL_ADDRESS;
233			return NULL;
234		}
235		if (set_addresses(abuf, sizeof(abuf), sa1, sa2) != 0) {
236			__ipsec_errcode = EIPSEC_INVAL_ADDRESS;
237			return NULL;
238		}
239	}
240
241	switch (xisr->sadb_x_ipsecrequest_level) {
242	case IPSEC_LEVEL_DEFAULT:
243		level = "default";
244		break;
245	case IPSEC_LEVEL_USE:
246		level = "use";
247		break;
248	case IPSEC_LEVEL_REQUIRE:
249		level = "require";
250		break;
251	case IPSEC_LEVEL_UNIQUE:
252		level = "unique";
253		break;
254	default:
255		__ipsec_errcode = EIPSEC_INVAL_LEVEL;
256		return NULL;
257	}
258
259	if (xisr->sadb_x_ipsecrequest_reqid == 0)
260		snprintf(buf, len, "%s/%s/%s/%s", proto, mode, abuf, level);
261	else {
262		int ch;
263
264		if (xisr->sadb_x_ipsecrequest_reqid > IPSEC_MANUAL_REQID_MAX)
265			ch = '#';
266		else
267			ch = ':';
268		snprintf(buf, len, "%s/%s/%s/%s%c%u", proto, mode, abuf, level,
269		    ch, xisr->sadb_x_ipsecrequest_reqid);
270	}
271
272	return buf;
273}
274
275static int
276set_addresses(buf, len, sa1, sa2)
277	char *buf;
278	size_t len;
279	struct sockaddr *sa1;
280	struct sockaddr *sa2;
281{
282	char tmp1[NI_MAXHOST], tmp2[NI_MAXHOST];
283
284	if (set_address(tmp1, sizeof(tmp1), sa1) == NULL ||
285	    set_address(tmp2, sizeof(tmp2), sa2) == NULL)
286		return -1;
287	if (strlen(tmp1) + 1 + strlen(tmp2) + 1 > len)
288		return -1;
289	snprintf(buf, len, "%s-%s", tmp1, tmp2);
290	return 0;
291}
292
293static char *
294set_address(buf, len, sa)
295	char *buf;
296	size_t len;
297	struct sockaddr *sa;
298{
299	const int niflags = NI_NUMERICHOST;
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