1 /* $KAME: prefix.c,v 1.9 2001/07/02 14:36:49 itojun Exp $ */
2 /* $FreeBSD: src/usr.sbin/faithd/prefix.c,v 1.1.2.2 2002/04/28 05:40:29 suz Exp $ */
5 * Copyright (C) 2000 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
44 #define offsetof(type, member) ((size_t)(u_long)(&((type *)0)->member))
50 static int prefix_set __P((const char *, struct prefix *, int));
51 static struct config *config_load1 __P((const char *));
53 static void config_show1 __P((const struct config *));
54 static void config_show __P((void));
57 struct config *config_list = NULL;
59 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
61 const int niflags = NI_NUMERICHOST;
65 prefix_set(s, prefix, slash)
67 struct prefix *prefix;
71 struct addrinfo hints, *res = NULL;
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))
89 if (res->ai_next || res->ai_addrlen > sizeof(prefix->a))
91 memcpy(&prefix->a, res->ai_addr, res->ai_addrlen);
93 switch (prefix->a.ss_family) {
96 a = (char *)&((struct sockaddr_in *)&prefix->a)->sin_addr;
100 a = (char *)&((struct sockaddr_in6 *)&prefix->a)->sin6_addr;
110 prefix->l = (int)strtoul(q, &r, 10);
113 if (prefix->l < 0 || prefix->l > max)
133 prefix_string(prefix)
134 const struct prefix *prefix;
136 static char buf[NI_MAXHOST + 20];
137 char hbuf[NI_MAXHOST];
139 if (getnameinfo((const struct sockaddr *)&prefix->a, prefix->a.ss_len,
140 hbuf, sizeof(hbuf), NULL, 0, niflags))
142 snprintf(buf, sizeof(buf), "%s/%d", hbuf, prefix->l);
147 prefix_match(prefix, sa)
148 const struct prefix *prefix;
149 const struct sockaddr *sa;
151 struct sockaddr_storage a, b;
155 if (prefix->a.ss_family != sa->sa_family ||
156 prefix->a.ss_len != sa->sa_len)
159 if (prefix->a.ss_len > sizeof(a) || sa->sa_len > sizeof(b))
162 switch (prefix->a.ss_family) {
164 off = offsetof(struct sockaddr_in, sin_addr);
167 off = offsetof(struct sockaddr_in6, sin6_addr);
170 if (memcmp(&prefix->a, sa, prefix->a.ss_len) != 0)
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);
181 if (off + l > a.ss_len)
184 pa = ((char *)&a) + off;
185 pb = ((char *)&b) + off;
187 pa[prefix->l / 8] &= 0xff00 >> (prefix->l % 8);
188 pb[prefix->l / 8] &= 0xff00 >> (prefix->l % 8);
190 if (memcmp(pa, pb, l) != 0)
197 * prefix/prefixlen permit/deny prefix/prefixlen [srcaddr]
198 * 3ffe::/16 permit 10.0.0.0/8 10.1.1.1
200 static struct config *
210 if (strlen(line) + 1 > sizeof(buf))
212 strlcpy(buf, line, sizeof(buf));
214 p = strchr(buf, '\n');
218 p = strchr(buf, '#');
221 if (strlen(buf) == 0)
225 memset(token, 0, sizeof(token));
226 for (i = 0; i < sizeof(token) / sizeof(token[0]); i++) {
227 token[i] = strtok(p, "\t ");
229 if (token[i] == NULL)
233 if (strtok(p, "\t ") != NULL)
235 /* insufficient tokens */
244 conf = (struct config *)malloc(sizeof(*conf));
247 memset(conf, 0, sizeof(*conf));
249 if (strcasecmp(token[1], "permit") == 0)
251 else if (strcasecmp(token[1], "deny") == 0)
254 /* invalid keyword is considered as "deny" */
258 if (prefix_set(token[0], &conf->match, 1) < 0)
260 if (prefix_set(token[2], &conf->dest, 1) < 0)
263 if (prefix_set(token[3], &conf->src, 0) < 0)
275 config_load(configfile)
276 const char *configfile;
280 struct config *conf, *p;
281 struct config sentinel;
286 configfile = _PATH_PREFIX_CONF;
287 fp = fopen(configfile, "r");
292 while (fgets(buf, sizeof(buf), fp) != NULL) {
293 conf = config_load1(buf);
299 config_list = sentinel.next;
308 const struct config *conf;
312 p = prefix_string(&conf->match);
313 printf("%s", p ? p : "?");
320 p = prefix_string(&conf->dest);
321 printf(" %s", p ? p : "?");
331 for (conf = config_list; conf; conf = conf->next)
336 const struct config *
337 config_match(sa1, sa2)
338 struct sockaddr *sa1, *sa2;
340 static struct config conf;
341 const struct config *p;
343 if (sa1->sa_len > sizeof(conf.match.a) ||
344 sa2->sa_len > sizeof(conf.dest.a))
347 memset(&conf, 0, sizeof(conf));
350 memcpy(&conf.match.a, sa1, sa1->sa_len);
351 memcpy(&conf.dest.a, sa2, sa2->sa_len);
355 for (p = config_list; p; p = p->next)
356 if (prefix_match(&p->match, sa1) && prefix_match(&p->dest, sa2))