]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libipsec/ipsec_dump_policy.c
libpcap: Update to 1.10.4
[FreeBSD/FreeBSD.git] / lib / libipsec / ipsec_dump_policy.c
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/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/socket.h>
40
41 #include <netipsec/key_var.h>
42 #include <netinet/in.h>
43 #include <netipsec/ipsec.h>
44
45 #include <arpa/inet.h>
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <netdb.h>
51
52 #include "ipsec_strerror.h"
53
54 static const char *ipsp_dir_strs[] = {
55         "any", "in", "out",
56 };
57
58 static const char *ipsp_policy_strs[] = {
59         "discard", "none", "ipsec", "entrust", "bypass",
60 };
61
62 static char *ipsec_dump_ipsecrequest(char *, size_t,
63         struct sadb_x_ipsecrequest *, size_t);
64 static int set_addresses(char *, size_t, struct sockaddr *, struct sockaddr *);
65 static char *set_address(char *, size_t, struct sockaddr *);
66
67 /*
68  * policy is sadb_x_policy buffer.
69  * Must call free() later.
70  * When delimiter == NULL, alternatively ' '(space) is applied.
71  */
72 char *
73 ipsec_dump_policy(caddr_t policy, 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
175 static char *
176 ipsec_dump_ipsecrequest(char *buf, size_t len, struct sadb_x_ipsecrequest *xisr,
177     size_t bound)
178 {
179         const char *proto, *mode, *level;
180         char abuf[NI_MAXHOST * 2 + 2];
181
182         if (xisr->sadb_x_ipsecrequest_len > bound) {
183                 __ipsec_errcode = EIPSEC_INVAL_PROTO;
184                 return NULL;
185         }
186
187         switch (xisr->sadb_x_ipsecrequest_proto) {
188         case IPPROTO_ESP:
189                 proto = "esp";
190                 break;
191         case IPPROTO_AH:
192                 proto = "ah";
193                 break;
194         case IPPROTO_IPCOMP:
195                 proto = "ipcomp";
196                 break;
197         case IPPROTO_TCP:
198                 proto = "tcp";
199                 break;
200         default:
201                 __ipsec_errcode = EIPSEC_INVAL_PROTO;
202                 return NULL;
203         }
204
205         switch (xisr->sadb_x_ipsecrequest_mode) {
206         case IPSEC_MODE_ANY:
207                 mode = "any";
208                 break;
209         case IPSEC_MODE_TRANSPORT:
210                 mode = "transport";
211                 break;
212         case IPSEC_MODE_TUNNEL:
213                 mode = "tunnel";
214                 break;
215         default:
216                 __ipsec_errcode = EIPSEC_INVAL_MODE;
217                 return NULL;
218         }
219
220         abuf[0] = '\0';
221         if (xisr->sadb_x_ipsecrequest_len > sizeof(*xisr)) {
222                 struct sockaddr *sa1, *sa2;
223                 caddr_t p;
224
225                 p = (caddr_t)(xisr + 1);
226                 sa1 = (struct sockaddr *)p;
227                 sa2 = (struct sockaddr *)(p + sa1->sa_len);
228                 if (sizeof(*xisr) + sa1->sa_len + sa2->sa_len !=
229                     xisr->sadb_x_ipsecrequest_len) {
230                         __ipsec_errcode = EIPSEC_INVAL_ADDRESS;
231                         return NULL;
232                 }
233                 if (set_addresses(abuf, sizeof(abuf), sa1, sa2) != 0) {
234                         __ipsec_errcode = EIPSEC_INVAL_ADDRESS;
235                         return NULL;
236                 }
237         }
238
239         switch (xisr->sadb_x_ipsecrequest_level) {
240         case IPSEC_LEVEL_DEFAULT:
241                 level = "default";
242                 break;
243         case IPSEC_LEVEL_USE:
244                 level = "use";
245                 break;
246         case IPSEC_LEVEL_REQUIRE:
247                 level = "require";
248                 break;
249         case IPSEC_LEVEL_UNIQUE:
250                 level = "unique";
251                 break;
252         default:
253                 __ipsec_errcode = EIPSEC_INVAL_LEVEL;
254                 return NULL;
255         }
256
257         if (xisr->sadb_x_ipsecrequest_reqid == 0)
258                 snprintf(buf, len, "%s/%s/%s/%s", proto, mode, abuf, level);
259         else {
260                 int ch;
261
262                 if (xisr->sadb_x_ipsecrequest_reqid > IPSEC_MANUAL_REQID_MAX)
263                         ch = '#';
264                 else
265                         ch = ':';
266                 snprintf(buf, len, "%s/%s/%s/%s%c%u", proto, mode, abuf, level,
267                     ch, xisr->sadb_x_ipsecrequest_reqid);
268         }
269
270         return buf;
271 }
272
273 static int
274 set_addresses(char *buf, size_t len, struct sockaddr *sa1, struct sockaddr *sa2)
275 {
276         char tmp1[NI_MAXHOST], tmp2[NI_MAXHOST];
277
278         if (set_address(tmp1, sizeof(tmp1), sa1) == NULL ||
279             set_address(tmp2, sizeof(tmp2), sa2) == NULL)
280                 return -1;
281         if (strlen(tmp1) + 1 + strlen(tmp2) + 1 > len)
282                 return -1;
283         snprintf(buf, len, "%s-%s", tmp1, tmp2);
284         return 0;
285 }
286
287 static char *
288 set_address(char *buf, size_t len, struct sockaddr *sa)
289 {
290         const int niflags = NI_NUMERICHOST;
291
292         if (len < 1)
293                 return NULL;
294         buf[0] = '\0';
295         if (getnameinfo(sa, sa->sa_len, buf, len, NULL, 0, niflags) != 0)
296                 return NULL;
297         return buf;
298 }