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