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