prefix.c revision 1.3 1 1.3 itojun /* $NetBSD: prefix.c,v 1.3 2001/11/21 06:52:35 itojun Exp $ */
2 1.2 itojun /* $KAME: prefix.c,v 1.9 2001/07/02 14:36:49 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.1 itojun static int prefix_set __P((const char *, struct prefix *, int));
51 1.1 itojun static struct config *config_load1 __P((const char *));
52 1.1 itojun #if 0
53 1.1 itojun static void config_show1 __P((const struct config *));
54 1.1 itojun static void config_show __P((void));
55 1.1 itojun #endif
56 1.1 itojun
57 1.1 itojun struct config *config_list = NULL;
58 1.1 itojun #ifdef NI_WITHSCOPEID
59 1.1 itojun const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
60 1.1 itojun #else
61 1.1 itojun const int niflags = NI_NUMERICHOST;
62 1.1 itojun #endif
63 1.1 itojun
64 1.1 itojun static int
65 1.1 itojun prefix_set(s, prefix, slash)
66 1.1 itojun const char *s;
67 1.1 itojun struct prefix *prefix;
68 1.1 itojun int slash;
69 1.1 itojun {
70 1.3 itojun char *p = NULL, *q, *r;
71 1.1 itojun struct addrinfo hints, *res = NULL;
72 1.1 itojun int max;
73 1.1 itojun char *a;
74 1.1 itojun
75 1.1 itojun p = strdup(s);
76 1.3 itojun if (!p)
77 1.3 itojun goto fail;
78 1.1 itojun q = strchr(p, '/');
79 1.1 itojun if (q) {
80 1.1 itojun if (!slash)
81 1.1 itojun goto fail;
82 1.1 itojun *q++ = '\0';
83 1.1 itojun }
84 1.1 itojun
85 1.1 itojun memset(&hints, 0, sizeof(hints));
86 1.1 itojun hints.ai_family = PF_UNSPEC;
87 1.1 itojun hints.ai_socktype = SOCK_DGRAM; /*dummy*/
88 1.1 itojun hints.ai_flags = AI_NUMERICHOST;
89 1.1 itojun if (getaddrinfo(p, "0", &hints, &res))
90 1.1 itojun goto fail;
91 1.1 itojun if (res->ai_next || res->ai_addrlen > sizeof(prefix->a))
92 1.1 itojun goto fail;
93 1.1 itojun memcpy(&prefix->a, res->ai_addr, res->ai_addrlen);
94 1.1 itojun
95 1.1 itojun switch (prefix->a.ss_family) {
96 1.1 itojun case AF_INET:
97 1.1 itojun max = 32;
98 1.1 itojun a = (char *)&((struct sockaddr_in *)&prefix->a)->sin_addr;
99 1.1 itojun break;
100 1.1 itojun case AF_INET6:
101 1.1 itojun max = 128;
102 1.1 itojun a = (char *)&((struct sockaddr_in6 *)&prefix->a)->sin6_addr;
103 1.1 itojun break;
104 1.1 itojun default:
105 1.1 itojun a = NULL;
106 1.1 itojun max = -1;
107 1.1 itojun break;
108 1.1 itojun }
109 1.1 itojun
110 1.1 itojun if (q) {
111 1.1 itojun r = NULL;
112 1.1 itojun prefix->l = (int)strtoul(q, &r, 10);
113 1.1 itojun if (!*q || *r)
114 1.1 itojun goto fail;
115 1.1 itojun if (prefix->l < 0 || prefix->l > max)
116 1.1 itojun goto fail;
117 1.1 itojun } else
118 1.1 itojun prefix->l = max;
119 1.1 itojun
120 1.1 itojun if (p)
121 1.1 itojun free(p);
122 1.1 itojun if (res)
123 1.1 itojun freeaddrinfo(res);
124 1.1 itojun return 0;
125 1.1 itojun
126 1.1 itojun fail:
127 1.1 itojun if (p)
128 1.1 itojun free(p);
129 1.1 itojun if (res)
130 1.1 itojun freeaddrinfo(res);
131 1.1 itojun return -1;
132 1.1 itojun }
133 1.1 itojun
134 1.1 itojun const char *
135 1.1 itojun prefix_string(prefix)
136 1.1 itojun const struct prefix *prefix;
137 1.1 itojun {
138 1.1 itojun static char buf[NI_MAXHOST + 20];
139 1.1 itojun char hbuf[NI_MAXHOST];
140 1.1 itojun
141 1.2 itojun if (getnameinfo((const struct sockaddr *)&prefix->a, prefix->a.ss_len,
142 1.2 itojun hbuf, sizeof(hbuf), NULL, 0, niflags))
143 1.1 itojun return NULL;
144 1.1 itojun snprintf(buf, sizeof(buf), "%s/%d", hbuf, prefix->l);
145 1.1 itojun return buf;
146 1.1 itojun }
147 1.1 itojun
148 1.1 itojun int
149 1.1 itojun prefix_match(prefix, sa)
150 1.1 itojun const struct prefix *prefix;
151 1.1 itojun const struct sockaddr *sa;
152 1.1 itojun {
153 1.1 itojun struct sockaddr_storage a, b;
154 1.1 itojun char *pa, *pb;
155 1.1 itojun int off, l;
156 1.1 itojun
157 1.1 itojun if (prefix->a.ss_family != sa->sa_family ||
158 1.1 itojun prefix->a.ss_len != sa->sa_len)
159 1.1 itojun return 0;
160 1.1 itojun
161 1.1 itojun if (prefix->a.ss_len > sizeof(a) || sa->sa_len > sizeof(b))
162 1.1 itojun return 0;
163 1.1 itojun
164 1.1 itojun switch (prefix->a.ss_family) {
165 1.1 itojun case AF_INET:
166 1.1 itojun off = offsetof(struct sockaddr_in, sin_addr);
167 1.1 itojun break;
168 1.1 itojun case AF_INET6:
169 1.1 itojun off = offsetof(struct sockaddr_in6, sin6_addr);
170 1.1 itojun break;
171 1.1 itojun default:
172 1.1 itojun if (memcmp(&prefix->a, sa, prefix->a.ss_len) != 0)
173 1.1 itojun return 0;
174 1.1 itojun else
175 1.1 itojun return 1;
176 1.1 itojun }
177 1.1 itojun
178 1.1 itojun memcpy(&a, &prefix->a, prefix->a.ss_len);
179 1.1 itojun memcpy(&b, sa, sa->sa_len);
180 1.1 itojun l = prefix->l / 8 + (prefix->l % 8 ? 1 : 0);
181 1.1 itojun
182 1.1 itojun /* overrun check */
183 1.1 itojun if (off + l > a.ss_len)
184 1.1 itojun return 0;
185 1.1 itojun
186 1.1 itojun pa = ((char *)&a) + off;
187 1.1 itojun pb = ((char *)&b) + off;
188 1.1 itojun if (prefix->l % 8) {
189 1.1 itojun pa[prefix->l / 8] &= 0xff00 >> (prefix->l % 8);
190 1.1 itojun pb[prefix->l / 8] &= 0xff00 >> (prefix->l % 8);
191 1.1 itojun }
192 1.1 itojun if (memcmp(pa, pb, l) != 0)
193 1.1 itojun return 0;
194 1.1 itojun else
195 1.1 itojun return 1;
196 1.1 itojun }
197 1.1 itojun
198 1.1 itojun /*
199 1.1 itojun * prefix/prefixlen permit/deny prefix/prefixlen [srcaddr]
200 1.1 itojun * 3ffe::/16 permit 10.0.0.0/8 10.1.1.1
201 1.1 itojun */
202 1.1 itojun static struct config *
203 1.1 itojun config_load1(line)
204 1.1 itojun const char *line;
205 1.1 itojun {
206 1.1 itojun struct config *conf;
207 1.1 itojun char buf[BUFSIZ];
208 1.1 itojun char *p;
209 1.1 itojun char *token[4];
210 1.1 itojun int i;
211 1.1 itojun
212 1.1 itojun if (strlen(line) + 1 > sizeof(buf))
213 1.1 itojun return NULL;
214 1.1 itojun strlcpy(buf, line, sizeof(buf));
215 1.1 itojun
216 1.1 itojun p = strchr(buf, '\n');
217 1.1 itojun if (!p)
218 1.1 itojun return NULL;
219 1.1 itojun *p = '\0';
220 1.1 itojun p = strchr(buf, '#');
221 1.1 itojun if (p)
222 1.1 itojun *p = '\0';
223 1.1 itojun if (strlen(buf) == 0)
224 1.1 itojun return NULL;
225 1.1 itojun
226 1.1 itojun p = buf;
227 1.1 itojun memset(token, 0, sizeof(token));
228 1.1 itojun for (i = 0; i < sizeof(token) / sizeof(token[0]); i++) {
229 1.1 itojun token[i] = strtok(p, "\t ");
230 1.1 itojun p = NULL;
231 1.1 itojun if (token[i] == NULL)
232 1.1 itojun break;
233 1.1 itojun }
234 1.1 itojun /* extra tokens? */
235 1.1 itojun if (strtok(p, "\t ") != NULL)
236 1.1 itojun return NULL;
237 1.1 itojun /* insufficient tokens */
238 1.1 itojun switch (i) {
239 1.1 itojun case 3:
240 1.1 itojun case 4:
241 1.1 itojun break;
242 1.1 itojun default:
243 1.1 itojun return NULL;
244 1.1 itojun }
245 1.1 itojun
246 1.1 itojun conf = (struct config *)malloc(sizeof(*conf));
247 1.1 itojun if (conf == NULL)
248 1.1 itojun return NULL;
249 1.1 itojun memset(conf, 0, sizeof(*conf));
250 1.1 itojun
251 1.1 itojun if (strcasecmp(token[1], "permit") == 0)
252 1.1 itojun conf->permit = 1;
253 1.1 itojun else if (strcasecmp(token[1], "deny") == 0)
254 1.1 itojun conf->permit = 0;
255 1.1 itojun else {
256 1.1 itojun /* invalid keyword is considered as "deny" */
257 1.1 itojun conf->permit = 0;
258 1.1 itojun }
259 1.1 itojun
260 1.1 itojun if (prefix_set(token[0], &conf->match, 1) < 0)
261 1.1 itojun goto fail;
262 1.1 itojun if (prefix_set(token[2], &conf->dest, 1) < 0)
263 1.1 itojun goto fail;
264 1.1 itojun if (token[3]) {
265 1.1 itojun if (prefix_set(token[3], &conf->src, 0) < 0)
266 1.1 itojun goto fail;
267 1.1 itojun }
268 1.1 itojun
269 1.1 itojun return conf;
270 1.1 itojun
271 1.1 itojun fail:
272 1.1 itojun free(conf);
273 1.1 itojun return NULL;
274 1.1 itojun }
275 1.1 itojun
276 1.1 itojun int
277 1.1 itojun config_load(configfile)
278 1.1 itojun const char *configfile;
279 1.1 itojun {
280 1.1 itojun FILE *fp;
281 1.1 itojun char buf[BUFSIZ];
282 1.1 itojun struct config *conf, *p;
283 1.1 itojun struct config sentinel;
284 1.1 itojun
285 1.1 itojun config_list = NULL;
286 1.1 itojun
287 1.1 itojun if (!configfile)
288 1.1 itojun configfile = _PATH_PREFIX_CONF;
289 1.1 itojun fp = fopen(configfile, "r");
290 1.1 itojun if (fp == NULL)
291 1.1 itojun return -1;
292 1.1 itojun
293 1.1 itojun p = &sentinel;
294 1.1 itojun while (fgets(buf, sizeof(buf), fp) != NULL) {
295 1.1 itojun conf = config_load1(buf);
296 1.1 itojun if (conf) {
297 1.1 itojun p->next = conf;
298 1.1 itojun p = p->next;
299 1.1 itojun }
300 1.1 itojun }
301 1.1 itojun config_list = sentinel.next;
302 1.1 itojun
303 1.1 itojun fclose(fp);
304 1.1 itojun return 0;
305 1.1 itojun }
306 1.1 itojun
307 1.1 itojun #if 0
308 1.1 itojun static void
309 1.1 itojun config_show1(conf)
310 1.1 itojun const struct config *conf;
311 1.1 itojun {
312 1.1 itojun const char *p;
313 1.1 itojun
314 1.1 itojun p = prefix_string(&conf->match);
315 1.1 itojun printf("%s", p ? p : "?");
316 1.1 itojun
317 1.1 itojun if (conf->permit)
318 1.1 itojun printf(" permit");
319 1.1 itojun else
320 1.1 itojun printf(" deny");
321 1.1 itojun
322 1.1 itojun p = prefix_string(&conf->dest);
323 1.1 itojun printf(" %s", p ? p : "?");
324 1.1 itojun
325 1.1 itojun printf("\n");
326 1.1 itojun }
327 1.1 itojun
328 1.1 itojun static void
329 1.1 itojun config_show()
330 1.1 itojun {
331 1.1 itojun struct config *conf;
332 1.1 itojun
333 1.1 itojun for (conf = config_list; conf; conf = conf->next)
334 1.1 itojun config_show1(conf);
335 1.1 itojun }
336 1.1 itojun #endif
337 1.1 itojun
338 1.1 itojun const struct config *
339 1.1 itojun config_match(sa1, sa2)
340 1.1 itojun struct sockaddr *sa1, *sa2;
341 1.1 itojun {
342 1.1 itojun static struct config conf;
343 1.1 itojun const struct config *p;
344 1.1 itojun
345 1.1 itojun if (sa1->sa_len > sizeof(conf.match.a) ||
346 1.1 itojun sa2->sa_len > sizeof(conf.dest.a))
347 1.1 itojun return NULL;
348 1.1 itojun
349 1.1 itojun memset(&conf, 0, sizeof(conf));
350 1.1 itojun if (!config_list) {
351 1.1 itojun conf.permit = 1;
352 1.1 itojun memcpy(&conf.match.a, sa1, sa1->sa_len);
353 1.1 itojun memcpy(&conf.dest.a, sa2, sa2->sa_len);
354 1.1 itojun return &conf;
355 1.1 itojun }
356 1.1 itojun
357 1.1 itojun for (p = config_list; p; p = p->next)
358 1.1 itojun if (prefix_match(&p->match, sa1) && prefix_match(&p->dest, sa2))
359 1.1 itojun return p;
360 1.1 itojun
361 1.1 itojun return NULL;
362 1.1 itojun }
363