prefix.c revision 1.9 1 1.9 sevan /* $NetBSD: prefix.c,v 1.9 2018/01/23 21:06:24 sevan Exp $ */
2 1.6 itojun /* $KAME: prefix.c,v 1.13 2003/09/02 22:50:17 itojun Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 2000 WIDE Project.
6 1.1 itojun * All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun * 1. Redistributions of source code must retain the above copyright
12 1.1 itojun * notice, this list of conditions and the following disclaimer.
13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 itojun * notice, this list of conditions and the following disclaimer in the
15 1.1 itojun * documentation and/or other materials provided with the distribution.
16 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.1 itojun * may be used to endorse or promote products derived from this software
18 1.1 itojun * without specific prior written permission.
19 1.1 itojun *
20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 itojun * SUCH DAMAGE.
31 1.1 itojun */
32 1.1 itojun
33 1.1 itojun #include <sys/types.h>
34 1.1 itojun #include <sys/socket.h>
35 1.1 itojun #include <netinet/in.h>
36 1.1 itojun #include <stdio.h>
37 1.1 itojun #include <netdb.h>
38 1.1 itojun #include <string.h>
39 1.1 itojun #include <stddef.h>
40 1.1 itojun #include <stdlib.h>
41 1.1 itojun #include <limits.h>
42 1.1 itojun
43 1.1 itojun #ifndef offsetof
44 1.1 itojun #define offsetof(type, member) ((size_t)(u_long)(&((type *)0)->member))
45 1.1 itojun #endif
46 1.1 itojun
47 1.1 itojun #include "faithd.h"
48 1.1 itojun #include "prefix.h"
49 1.1 itojun
50 1.9 sevan static int prefix_set(const char *, struct prefix *, int);
51 1.9 sevan static struct config *config_load1(const char *);
52 1.1 itojun #if 0
53 1.9 sevan static void config_show1(const struct config *);
54 1.9 sevan static void config_show(void);
55 1.1 itojun #endif
56 1.1 itojun
57 1.1 itojun struct config *config_list = NULL;
58 1.1 itojun const int niflags = NI_NUMERICHOST;
59 1.1 itojun
60 1.1 itojun static int
61 1.5 itojun prefix_set(const char *s, struct prefix *prefix, int slash)
62 1.1 itojun {
63 1.3 itojun char *p = NULL, *q, *r;
64 1.1 itojun struct addrinfo hints, *res = NULL;
65 1.1 itojun int max;
66 1.1 itojun
67 1.1 itojun p = strdup(s);
68 1.3 itojun if (!p)
69 1.3 itojun goto fail;
70 1.1 itojun q = strchr(p, '/');
71 1.1 itojun if (q) {
72 1.1 itojun if (!slash)
73 1.1 itojun goto fail;
74 1.1 itojun *q++ = '\0';
75 1.1 itojun }
76 1.1 itojun
77 1.1 itojun memset(&hints, 0, sizeof(hints));
78 1.1 itojun hints.ai_family = PF_UNSPEC;
79 1.1 itojun hints.ai_socktype = SOCK_DGRAM; /*dummy*/
80 1.1 itojun hints.ai_flags = AI_NUMERICHOST;
81 1.1 itojun if (getaddrinfo(p, "0", &hints, &res))
82 1.1 itojun goto fail;
83 1.1 itojun if (res->ai_next || res->ai_addrlen > sizeof(prefix->a))
84 1.1 itojun goto fail;
85 1.1 itojun memcpy(&prefix->a, res->ai_addr, res->ai_addrlen);
86 1.1 itojun
87 1.1 itojun switch (prefix->a.ss_family) {
88 1.1 itojun case AF_INET:
89 1.1 itojun max = 32;
90 1.1 itojun break;
91 1.1 itojun case AF_INET6:
92 1.1 itojun max = 128;
93 1.1 itojun break;
94 1.1 itojun default:
95 1.1 itojun max = -1;
96 1.1 itojun break;
97 1.1 itojun }
98 1.1 itojun
99 1.1 itojun if (q) {
100 1.1 itojun r = NULL;
101 1.1 itojun prefix->l = (int)strtoul(q, &r, 10);
102 1.1 itojun if (!*q || *r)
103 1.1 itojun goto fail;
104 1.1 itojun if (prefix->l < 0 || prefix->l > max)
105 1.1 itojun goto fail;
106 1.1 itojun } else
107 1.1 itojun prefix->l = max;
108 1.1 itojun
109 1.1 itojun if (p)
110 1.1 itojun free(p);
111 1.1 itojun if (res)
112 1.1 itojun freeaddrinfo(res);
113 1.1 itojun return 0;
114 1.1 itojun
115 1.1 itojun fail:
116 1.1 itojun if (p)
117 1.1 itojun free(p);
118 1.1 itojun if (res)
119 1.1 itojun freeaddrinfo(res);
120 1.1 itojun return -1;
121 1.1 itojun }
122 1.1 itojun
123 1.1 itojun const char *
124 1.5 itojun prefix_string(const struct prefix *prefix)
125 1.1 itojun {
126 1.1 itojun static char buf[NI_MAXHOST + 20];
127 1.1 itojun char hbuf[NI_MAXHOST];
128 1.1 itojun
129 1.8 christos if (getnameinfo((const void *)&prefix->a, (socklen_t)prefix->a.ss_len,
130 1.8 christos hbuf, (socklen_t)sizeof(hbuf), NULL, 0, niflags))
131 1.1 itojun return NULL;
132 1.8 christos (void)snprintf(buf, sizeof(buf), "%s/%d", hbuf, prefix->l);
133 1.1 itojun return buf;
134 1.1 itojun }
135 1.1 itojun
136 1.1 itojun int
137 1.5 itojun prefix_match(const struct prefix *prefix, const struct sockaddr *sa)
138 1.1 itojun {
139 1.1 itojun struct sockaddr_storage a, b;
140 1.1 itojun char *pa, *pb;
141 1.1 itojun int off, l;
142 1.1 itojun
143 1.1 itojun if (prefix->a.ss_family != sa->sa_family ||
144 1.1 itojun prefix->a.ss_len != sa->sa_len)
145 1.1 itojun return 0;
146 1.1 itojun
147 1.1 itojun if (prefix->a.ss_len > sizeof(a) || sa->sa_len > sizeof(b))
148 1.1 itojun return 0;
149 1.1 itojun
150 1.1 itojun switch (prefix->a.ss_family) {
151 1.1 itojun case AF_INET:
152 1.1 itojun off = offsetof(struct sockaddr_in, sin_addr);
153 1.1 itojun break;
154 1.1 itojun case AF_INET6:
155 1.1 itojun off = offsetof(struct sockaddr_in6, sin6_addr);
156 1.1 itojun break;
157 1.1 itojun default:
158 1.1 itojun if (memcmp(&prefix->a, sa, prefix->a.ss_len) != 0)
159 1.1 itojun return 0;
160 1.1 itojun else
161 1.1 itojun return 1;
162 1.1 itojun }
163 1.1 itojun
164 1.1 itojun memcpy(&a, &prefix->a, prefix->a.ss_len);
165 1.1 itojun memcpy(&b, sa, sa->sa_len);
166 1.1 itojun l = prefix->l / 8 + (prefix->l % 8 ? 1 : 0);
167 1.1 itojun
168 1.1 itojun /* overrun check */
169 1.1 itojun if (off + l > a.ss_len)
170 1.1 itojun return 0;
171 1.1 itojun
172 1.8 christos pa = ((char *)(void *)&a) + off;
173 1.8 christos pb = ((char *)(void *)&b) + off;
174 1.1 itojun if (prefix->l % 8) {
175 1.1 itojun pa[prefix->l / 8] &= 0xff00 >> (prefix->l % 8);
176 1.1 itojun pb[prefix->l / 8] &= 0xff00 >> (prefix->l % 8);
177 1.1 itojun }
178 1.1 itojun if (memcmp(pa, pb, l) != 0)
179 1.1 itojun return 0;
180 1.1 itojun else
181 1.1 itojun return 1;
182 1.1 itojun }
183 1.1 itojun
184 1.1 itojun /*
185 1.1 itojun * prefix/prefixlen permit/deny prefix/prefixlen [srcaddr]
186 1.1 itojun * 3ffe::/16 permit 10.0.0.0/8 10.1.1.1
187 1.1 itojun */
188 1.1 itojun static struct config *
189 1.5 itojun config_load1(const char *line)
190 1.1 itojun {
191 1.1 itojun struct config *conf;
192 1.1 itojun char buf[BUFSIZ];
193 1.1 itojun char *p;
194 1.1 itojun char *token[4];
195 1.7 lukem size_t i;
196 1.1 itojun
197 1.1 itojun if (strlen(line) + 1 > sizeof(buf))
198 1.1 itojun return NULL;
199 1.8 christos (void)strlcpy(buf, line, sizeof(buf));
200 1.1 itojun
201 1.1 itojun p = strchr(buf, '\n');
202 1.1 itojun if (!p)
203 1.1 itojun return NULL;
204 1.1 itojun *p = '\0';
205 1.1 itojun p = strchr(buf, '#');
206 1.1 itojun if (p)
207 1.1 itojun *p = '\0';
208 1.1 itojun if (strlen(buf) == 0)
209 1.1 itojun return NULL;
210 1.1 itojun
211 1.1 itojun p = buf;
212 1.1 itojun memset(token, 0, sizeof(token));
213 1.1 itojun for (i = 0; i < sizeof(token) / sizeof(token[0]); i++) {
214 1.1 itojun token[i] = strtok(p, "\t ");
215 1.1 itojun p = NULL;
216 1.1 itojun if (token[i] == NULL)
217 1.1 itojun break;
218 1.1 itojun }
219 1.1 itojun /* extra tokens? */
220 1.1 itojun if (strtok(p, "\t ") != NULL)
221 1.1 itojun return NULL;
222 1.1 itojun /* insufficient tokens */
223 1.1 itojun switch (i) {
224 1.1 itojun case 3:
225 1.1 itojun case 4:
226 1.1 itojun break;
227 1.1 itojun default:
228 1.1 itojun return NULL;
229 1.1 itojun }
230 1.1 itojun
231 1.1 itojun conf = (struct config *)malloc(sizeof(*conf));
232 1.1 itojun if (conf == NULL)
233 1.1 itojun return NULL;
234 1.1 itojun memset(conf, 0, sizeof(*conf));
235 1.1 itojun
236 1.1 itojun if (strcasecmp(token[1], "permit") == 0)
237 1.1 itojun conf->permit = 1;
238 1.1 itojun else if (strcasecmp(token[1], "deny") == 0)
239 1.1 itojun conf->permit = 0;
240 1.1 itojun else {
241 1.1 itojun /* invalid keyword is considered as "deny" */
242 1.1 itojun conf->permit = 0;
243 1.1 itojun }
244 1.1 itojun
245 1.1 itojun if (prefix_set(token[0], &conf->match, 1) < 0)
246 1.1 itojun goto fail;
247 1.1 itojun if (prefix_set(token[2], &conf->dest, 1) < 0)
248 1.1 itojun goto fail;
249 1.1 itojun if (token[3]) {
250 1.1 itojun if (prefix_set(token[3], &conf->src, 0) < 0)
251 1.1 itojun goto fail;
252 1.1 itojun }
253 1.1 itojun
254 1.1 itojun return conf;
255 1.1 itojun
256 1.1 itojun fail:
257 1.1 itojun free(conf);
258 1.1 itojun return NULL;
259 1.1 itojun }
260 1.1 itojun
261 1.1 itojun int
262 1.5 itojun config_load(const char *configfile)
263 1.1 itojun {
264 1.1 itojun FILE *fp;
265 1.1 itojun char buf[BUFSIZ];
266 1.1 itojun struct config *conf, *p;
267 1.1 itojun struct config sentinel;
268 1.1 itojun
269 1.1 itojun config_list = NULL;
270 1.1 itojun
271 1.1 itojun if (!configfile)
272 1.1 itojun configfile = _PATH_PREFIX_CONF;
273 1.1 itojun fp = fopen(configfile, "r");
274 1.1 itojun if (fp == NULL)
275 1.1 itojun return -1;
276 1.1 itojun
277 1.1 itojun p = &sentinel;
278 1.6 itojun sentinel.next = NULL;
279 1.8 christos while (fgets(buf, (int)sizeof(buf), fp) != NULL) {
280 1.1 itojun conf = config_load1(buf);
281 1.1 itojun if (conf) {
282 1.1 itojun p->next = conf;
283 1.1 itojun p = p->next;
284 1.1 itojun }
285 1.1 itojun }
286 1.1 itojun config_list = sentinel.next;
287 1.1 itojun
288 1.8 christos (void)fclose(fp);
289 1.1 itojun return 0;
290 1.1 itojun }
291 1.1 itojun
292 1.1 itojun #if 0
293 1.1 itojun static void
294 1.5 itojun config_show1(const struct config *conf)
295 1.1 itojun {
296 1.1 itojun const char *p;
297 1.1 itojun
298 1.1 itojun p = prefix_string(&conf->match);
299 1.1 itojun printf("%s", p ? p : "?");
300 1.1 itojun
301 1.1 itojun if (conf->permit)
302 1.1 itojun printf(" permit");
303 1.1 itojun else
304 1.1 itojun printf(" deny");
305 1.1 itojun
306 1.1 itojun p = prefix_string(&conf->dest);
307 1.1 itojun printf(" %s", p ? p : "?");
308 1.1 itojun
309 1.1 itojun printf("\n");
310 1.1 itojun }
311 1.1 itojun
312 1.1 itojun static void
313 1.1 itojun config_show()
314 1.1 itojun {
315 1.1 itojun struct config *conf;
316 1.1 itojun
317 1.1 itojun for (conf = config_list; conf; conf = conf->next)
318 1.1 itojun config_show1(conf);
319 1.1 itojun }
320 1.1 itojun #endif
321 1.1 itojun
322 1.1 itojun const struct config *
323 1.5 itojun config_match(struct sockaddr *sa1, struct sockaddr *sa2)
324 1.1 itojun {
325 1.1 itojun static struct config conf;
326 1.1 itojun const struct config *p;
327 1.1 itojun
328 1.1 itojun if (sa1->sa_len > sizeof(conf.match.a) ||
329 1.1 itojun sa2->sa_len > sizeof(conf.dest.a))
330 1.1 itojun return NULL;
331 1.1 itojun
332 1.1 itojun memset(&conf, 0, sizeof(conf));
333 1.1 itojun if (!config_list) {
334 1.1 itojun conf.permit = 1;
335 1.1 itojun memcpy(&conf.match.a, sa1, sa1->sa_len);
336 1.1 itojun memcpy(&conf.dest.a, sa2, sa2->sa_len);
337 1.1 itojun return &conf;
338 1.1 itojun }
339 1.1 itojun
340 1.1 itojun for (p = config_list; p; p = p->next)
341 1.1 itojun if (prefix_match(&p->match, sa1) && prefix_match(&p->dest, sa2))
342 1.1 itojun return p;
343 1.1 itojun
344 1.1 itojun return NULL;
345 1.1 itojun }
346