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