Home | History | Annotate | Line # | Download | only in npfctl
npf_data.c revision 1.18
      1 /*	$NetBSD: npf_data.c,v 1.18 2012/08/12 03:35:13 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * npfctl(8) data manipulation and helper routines.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __RCSID("$NetBSD: npf_data.c,v 1.18 2012/08/12 03:35:13 rmind Exp $");
     35 
     36 #include <sys/types.h>
     37 #include <sys/null.h>
     38 
     39 #include <netinet/in.h>
     40 #include <netinet/in_systm.h>
     41 #include <netinet/ip.h>
     42 #define ICMP_STRINGS
     43 #include <netinet/ip_icmp.h>
     44 #define ICMP6_STRINGS
     45 #include <netinet/icmp6.h>
     46 #include <netinet/tcp.h>
     47 #include <net/if.h>
     48 
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <err.h>
     52 #include <errno.h>
     53 #include <ifaddrs.h>
     54 #include <netdb.h>
     55 
     56 #include "npfctl.h"
     57 
     58 static struct ifaddrs *		ifs_list = NULL;
     59 
     60 unsigned long
     61 npfctl_find_ifindex(const char *ifname)
     62 {
     63 	unsigned long if_idx = if_nametoindex(ifname);
     64 
     65 	if (!if_idx) {
     66 		if ((if_idx = npfctl_debug_addif(ifname)) != 0) {
     67 			return if_idx;
     68 		}
     69 		yyerror("unknown interface '%s'", ifname);
     70 	}
     71 	return if_idx;
     72 }
     73 
     74 static bool
     75 npfctl_copy_address(sa_family_t fam, npf_addr_t *addr, const void *ptr)
     76 {
     77 	memset(addr, 0, sizeof(npf_addr_t));
     78 
     79 	switch (fam) {
     80 	case AF_INET: {
     81 		const struct sockaddr_in *sin = ptr;
     82 		memcpy(addr, &sin->sin_addr, sizeof(sin->sin_addr));
     83 		return true;
     84 	}
     85 	case AF_INET6: {
     86 		const struct sockaddr_in6 *sin6 = ptr;
     87 		memcpy(addr, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
     88 		return true;
     89 	}
     90 	default:
     91 		yyerror("unknown address family %u", fam);
     92 		return false;
     93 	}
     94 }
     95 
     96 static bool
     97 npfctl_parse_fam_addr(const char *name, sa_family_t *fam, npf_addr_t *addr)
     98 {
     99 	static const struct addrinfo hint = {
    100 		.ai_family = AF_UNSPEC,
    101 		.ai_flags = AI_NUMERICHOST
    102 	};
    103 	struct addrinfo *ai;
    104 	int ret;
    105 
    106 	ret = getaddrinfo(name, NULL, &hint, &ai);
    107 	if (ret) {
    108 		yyerror("cannot parse '%s' (%s)", name, gai_strerror(ret));
    109 		return false;
    110 	}
    111 	if (fam) {
    112 		*fam = ai->ai_family;
    113 	}
    114 	if (!npfctl_copy_address(*fam, addr, ai->ai_addr)) {
    115 		return false;
    116 	}
    117 	freeaddrinfo(ai);
    118 	return true;
    119 }
    120 
    121 static bool
    122 npfctl_parse_mask(const char *s, sa_family_t fam, npf_netmask_t *mask)
    123 {
    124 	char *ep = NULL;
    125 	npf_addr_t addr;
    126 	uint8_t *ap;
    127 
    128 	if (s) {
    129 		errno = 0;
    130 		*mask = (npf_netmask_t)strtol(s, &ep, 0);
    131 		if (*ep == '\0' && s != ep && errno != ERANGE)
    132 			return true;
    133 		if (!npfctl_parse_fam_addr(s, &fam, &addr))
    134 			return false;
    135 	}
    136 
    137 	assert(fam == AF_INET || fam == AF_INET6);
    138 	*mask = NPF_NO_NETMASK;
    139 	if (ep == NULL) {
    140 		return true;
    141 	}
    142 
    143 	ap = addr.s6_addr + (*mask / 8) - 1;
    144 	while (ap >= addr.s6_addr) {
    145 		for (int j = 8; j > 0; j--) {
    146 			if (*ap & 1)
    147 				return true;
    148 			*ap >>= 1;
    149 			(*mask)--;
    150 			if (*mask == 0)
    151 				return true;
    152 		}
    153 		ap--;
    154 	}
    155 	return true;
    156 }
    157 
    158 /*
    159  * npfctl_parse_fam_addr_mask: return address family, address and mask.
    160  *
    161  * => Mask is optional and can be NULL.
    162  * => Returns true on success or false if unable to parse.
    163  */
    164 npfvar_t *
    165 npfctl_parse_fam_addr_mask(const char *addr, const char *mask,
    166     unsigned long *nummask)
    167 {
    168 	npfvar_t *vp = npfvar_create(".addr");
    169 	fam_addr_mask_t fam;
    170 
    171 	memset(&fam, 0, sizeof(fam));
    172 
    173 	if (!npfctl_parse_fam_addr(addr, &fam.fam_family, &fam.fam_addr))
    174 		goto out;
    175 
    176 	/*
    177 	 * Note: both mask and nummask may be NULL.  In such case,
    178 	 * npfctl_parse_mask() will handle and will set full mask.
    179 	 */
    180 	if (nummask) {
    181 		fam.fam_mask = *nummask;
    182 	} else if (!npfctl_parse_mask(mask, fam.fam_family, &fam.fam_mask)) {
    183 		goto out;
    184 	}
    185 
    186 	if (!npfvar_add_element(vp, NPFVAR_FAM, &fam, sizeof(fam)))
    187 		goto out;
    188 
    189 	return vp;
    190 out:
    191 	npfvar_destroy(vp);
    192 	return NULL;
    193 }
    194 
    195 npfvar_t *
    196 npfctl_parse_table_id(const char *id)
    197 {
    198 	npfvar_t *vp;
    199 
    200 	if (!npfctl_table_exists_p(id)) {
    201 		yyerror("table '%s' is not defined", id);
    202 		return NULL;
    203 	}
    204 	vp = npfvar_create(".table");
    205 
    206 	if (!npfvar_add_element(vp, NPFVAR_TABLE, id, strlen(id) + 1))
    207 		goto out;
    208 
    209 	return vp;
    210 out:
    211 	npfvar_destroy(vp);
    212 	return NULL;
    213 }
    214 
    215 /*
    216  * npfctl_parse_port_range: create a port-range variable.  Note that the
    217  * passed port numbers should be in host byte order.
    218  */
    219 npfvar_t *
    220 npfctl_parse_port_range(in_port_t s, in_port_t e)
    221 {
    222 	npfvar_t *vp = npfvar_create(".port_range");
    223 	port_range_t pr;
    224 
    225 	pr.pr_start = htons(s);
    226 	pr.pr_end = htons(e);
    227 
    228 	if (!npfvar_add_element(vp, NPFVAR_PORT_RANGE, &pr, sizeof(pr)))
    229 		goto out;
    230 
    231 	return vp;
    232 out:
    233 	npfvar_destroy(vp);
    234 	return NULL;
    235 }
    236 
    237 npfvar_t *
    238 npfctl_parse_port_range_variable(const char *v)
    239 {
    240 	npfvar_t *vp = npfvar_lookup(v);
    241 	size_t count = npfvar_get_count(vp);
    242 	npfvar_t *pvp = npfvar_create(".port_range");
    243 	port_range_t *pr;
    244 	in_port_t p;
    245 
    246 	for (size_t i = 0; i < count; i++) {
    247 		int type = npfvar_get_type(vp, i);
    248 		void *data = npfvar_get_data(vp, type, i);
    249 
    250 		switch (type) {
    251 		case NPFVAR_IDENTIFIER:
    252 		case NPFVAR_STRING:
    253 			p = npfctl_portno(data);
    254 			npfvar_add_elements(pvp, npfctl_parse_port_range(p, p));
    255 			break;
    256 		case NPFVAR_PORT_RANGE:
    257 			pr = data;
    258 			npfvar_add_element(pvp, NPFVAR_PORT_RANGE, pr,
    259 			    sizeof(*pr));
    260 			break;
    261 		case NPFVAR_NUM:
    262 			p = *(unsigned long *)data;
    263 			npfvar_add_elements(pvp, npfctl_parse_port_range(p, p));
    264 			break;
    265 		default:
    266 			yyerror("wrong variable '%s' type '%s' for port range",
    267 			    v, npfvar_type(type));
    268 			npfvar_destroy(pvp);
    269 			return NULL;
    270 		}
    271 	}
    272 	return pvp;
    273 }
    274 
    275 npfvar_t *
    276 npfctl_parse_iface(const char *ifname)
    277 {
    278 	npfvar_t *vp = npfvar_create(".iface");
    279 	struct ifaddrs *ifa;
    280 	fam_addr_mask_t fam;
    281 	bool gotif = false;
    282 
    283 	if (ifs_list == NULL && getifaddrs(&ifs_list) == -1) {
    284 		err(EXIT_FAILURE, "getifaddrs");
    285 	}
    286 	memset(&fam, 0, sizeof(fam));
    287 
    288 	npfvar_t *ip = npfvar_create(".ifname");
    289 	if (!npfvar_add_element(ip, NPFVAR_STRING, ifname, strlen(ifname) + 1))
    290 		goto out;
    291 
    292 	for (ifa = ifs_list; ifa != NULL; ifa = ifa->ifa_next) {
    293 		struct sockaddr *sa;
    294 		sa_family_t family;
    295 
    296 		if (strcmp(ifa->ifa_name, ifname) != 0)
    297 			continue;
    298 
    299 		gotif = true;
    300 		if ((ifa->ifa_flags & IFF_UP) == 0)
    301 			warnx("interface '%s' is down", ifname);
    302 
    303 		sa = ifa->ifa_addr;
    304 		family = sa->sa_family;
    305 		if (family != AF_INET && family != AF_INET6)
    306 			continue;
    307 
    308 		fam.fam_family = family;
    309 		fam.fam_interface = ip;
    310 
    311 		if (!npfctl_copy_address(family, &fam.fam_addr, sa))
    312 			goto out;
    313 
    314 		if (!npfctl_parse_mask(NULL, fam.fam_family, &fam.fam_mask))
    315 			goto out;
    316 
    317 		if (!npfvar_add_element(vp, NPFVAR_FAM, &fam, sizeof(fam)))
    318 			goto out;
    319 
    320 	}
    321 	if (!gotif) {
    322 		yyerror("interface '%s' not found", ifname);
    323 		goto out;
    324 	}
    325 	if (npfvar_get_count(vp) == 0) {
    326 		yyerror("no addresses matched for interface '%s'", ifname);
    327 		goto out;
    328 	}
    329 	return vp;
    330 out:
    331 	npfvar_destroy(vp);
    332 	npfvar_destroy(ip);
    333 	return NULL;
    334 }
    335 
    336 bool
    337 npfctl_parse_cidr(char *cidr, fam_addr_mask_t *fam, int *alen)
    338 {
    339 	char *mask, *p;
    340 
    341 	p = strchr(cidr, '\n');
    342 	if (p) {
    343 		*p = '\0';
    344 	}
    345 	mask = strchr(cidr, '/');
    346 	if (mask) {
    347 		*mask++ = '\0';
    348 	}
    349 
    350 	memset(fam, 0, sizeof(*fam));
    351 	if (!npfctl_parse_fam_addr(cidr, &fam->fam_family, &fam->fam_addr)) {
    352 		return false;
    353 	}
    354 	if (!npfctl_parse_mask(mask, fam->fam_family, &fam->fam_mask)) {
    355 		return false;
    356 	}
    357 	switch (fam->fam_family) {
    358 	case AF_INET:
    359 		*alen = sizeof(struct in_addr);
    360 		break;
    361 	case AF_INET6:
    362 		*alen = sizeof(struct in6_addr);
    363 		break;
    364 	default:
    365 		return false;
    366 	}
    367 	return true;
    368 }
    369 
    370 int
    371 npfctl_protono(const char *proto)
    372 {
    373 	struct protoent *pe;
    374 
    375 	pe = getprotobyname(proto);
    376 	if (pe == NULL) {
    377 		yyerror("unknown protocol '%s'", proto);
    378 		return -1;
    379 	}
    380 	return pe->p_proto;
    381 }
    382 
    383 /*
    384  * npfctl_portno: convert port identifier (string) to a number.
    385  *
    386  * => Returns port number in host byte order.
    387  */
    388 in_port_t
    389 npfctl_portno(const char *port)
    390 {
    391 	struct addrinfo *ai, *rai;
    392 	in_port_t p = 0;
    393 	int e;
    394 
    395 	e = getaddrinfo(NULL, port, NULL, &rai);
    396 	if (e != 0) {
    397 		yyerror("invalid port name '%s' (%s)", port, gai_strerror(e));
    398 		return 0;
    399 	}
    400 
    401 	for (ai = rai; ai; ai = ai->ai_next) {
    402 		switch (ai->ai_family) {
    403 		case AF_INET: {
    404 			struct sockaddr_in *sin = (void *)ai->ai_addr;
    405 			p = sin->sin_port;
    406 			goto out;
    407 		}
    408 		case AF_INET6: {
    409 			struct sockaddr_in6 *sin6 = (void *)ai->ai_addr;
    410 			p = sin6->sin6_port;
    411 			goto out;
    412 		}
    413 		default:
    414 			break;
    415 		}
    416 	}
    417 out:
    418 	freeaddrinfo(rai);
    419 	return ntohs(p);
    420 }
    421 
    422 npfvar_t *
    423 npfctl_parse_tcpflag(const char *s)
    424 {
    425 	uint8_t tfl = 0;
    426 
    427 	while (*s) {
    428 		switch (*s) {
    429 		case 'F': tfl |= TH_FIN; break;
    430 		case 'S': tfl |= TH_SYN; break;
    431 		case 'R': tfl |= TH_RST; break;
    432 		case 'P': tfl |= TH_PUSH; break;
    433 		case 'A': tfl |= TH_ACK; break;
    434 		case 'U': tfl |= TH_URG; break;
    435 		case 'E': tfl |= TH_ECE; break;
    436 		case 'W': tfl |= TH_CWR; break;
    437 		default:
    438 			yyerror("invalid flag '%c'", *s);
    439 			return NULL;
    440 		}
    441 		s++;
    442 	}
    443 
    444 	npfvar_t *vp = npfvar_create(".tcp_flag");
    445 	if (!npfvar_add_element(vp, NPFVAR_TCPFLAG, &tfl, sizeof(tfl))) {
    446 		npfvar_destroy(vp);
    447 		return NULL;
    448 	}
    449 
    450 	return vp;
    451 }
    452 
    453 uint8_t
    454 npfctl_icmptype(int proto, const char *type)
    455 {
    456 	uint8_t ul;
    457 
    458 	switch (proto) {
    459 	case IPPROTO_ICMP:
    460 		for (ul = 0; icmp_type[ul]; ul++)
    461 			if (strcmp(icmp_type[ul], type) == 0)
    462 				return ul;
    463 		break;
    464 	case IPPROTO_ICMPV6:
    465 		for (ul = 0; icmp6_type_err[ul]; ul++)
    466 			if (strcmp(icmp6_type_err[ul], type) == 0)
    467 				return ul;
    468 		for (ul = 0; icmp6_type_info[ul]; ul++)
    469 			if (strcmp(icmp6_type_info[ul], type) == 0)
    470 				return (ul+128);
    471 		break;
    472 	default:
    473 		assert(false);
    474 	}
    475 
    476 	yyerror("unknown icmp-type %s", type);
    477 	return ~0;
    478 }
    479 
    480 uint8_t
    481 npfctl_icmpcode(int proto, uint8_t type, const char *code)
    482 {
    483 	const char * const *arr;
    484 
    485 	switch (proto) {
    486 	case IPPROTO_ICMP:
    487 		switch (type) {
    488 		case ICMP_ECHOREPLY:
    489 		case ICMP_SOURCEQUENCH:
    490 		case ICMP_ALTHOSTADDR:
    491 		case ICMP_ECHO:
    492 		case ICMP_ROUTERSOLICIT:
    493 		case ICMP_TSTAMP:
    494 		case ICMP_TSTAMPREPLY:
    495 		case ICMP_IREQ:
    496 		case ICMP_IREQREPLY:
    497 		case ICMP_MASKREQ:
    498 		case ICMP_MASKREPLY:
    499 			arr = icmp_code_none;
    500 			break;
    501 		case ICMP_ROUTERADVERT:
    502 			arr = icmp_code_routeradvert;
    503 			break;
    504 		case ICMP_UNREACH:
    505 			arr = icmp_code_unreach;
    506 			break;
    507 		case ICMP_REDIRECT:
    508 			arr = icmp_code_redirect;
    509 			break;
    510 		case ICMP_TIMXCEED:
    511 			arr = icmp_code_timxceed;
    512 			break;
    513 		case ICMP_PARAMPROB:
    514 			arr = icmp_code_paramprob;
    515 			break;
    516 		case ICMP_PHOTURIS:
    517 			arr = icmp_code_photuris;
    518 			break;
    519 		default:
    520 			yyerror("unknown icmp-type %d while parsing code %s",
    521 				type, code);
    522 			return ~0;
    523 		}
    524 		break;
    525 	case IPPROTO_ICMPV6:
    526 		switch (type) {
    527 		case ICMP6_DST_UNREACH:
    528 			arr = icmp6_code_unreach;
    529 			break;
    530 		case ICMP6_TIME_EXCEEDED:
    531 			arr = icmp6_code_timxceed;
    532 			break;
    533 		case ICMP6_PARAM_PROB:
    534 			arr = icmp6_code_paramprob;
    535 			break;
    536 		case ICMP6_PACKET_TOO_BIG:
    537 		/* code-less info ICMPs */
    538 		case ICMP6_ECHO_REQUEST:
    539 		case ICMP6_ECHO_REPLY:
    540 		case MLD_LISTENER_QUERY:
    541 		case MLD_LISTENER_REPORT:
    542 		case MLD_LISTENER_DONE:
    543 		case ND_ROUTER_SOLICIT:
    544 		case ND_ROUTER_ADVERT:
    545 		case ND_NEIGHBOR_SOLICIT:
    546 		case ND_NEIGHBOR_ADVERT:
    547 		case ND_REDIRECT:
    548 			arr = icmp6_code_none;
    549 			break;
    550 		/* XXX TODO: info ICMPs with code values */
    551 		default:
    552 			yyerror("unknown icmp-type %d while parsing code %s",
    553 				type, code);
    554 			return ~0;
    555 		}
    556 		break;
    557 	default:
    558 		assert(false);
    559 	}
    560 
    561 	for (uint8_t ul = 0; arr[ul]; ul++) {
    562 		if (strcmp(arr[ul], code) == 0)
    563 			return ul;
    564 	}
    565 	yyerror("unknown code %s for icmp-type %d", code, type);
    566 	return ~0;
    567 }
    568 
    569 npfvar_t *
    570 npfctl_parse_icmp(int proto, int type, int code)
    571 {
    572 	npfvar_t *vp = npfvar_create(".icmp");
    573 	int varnum;
    574 
    575 	switch (proto) {
    576 	case IPPROTO_ICMP:
    577 		varnum = NPFVAR_ICMP;
    578 		break;
    579 	case IPPROTO_ICMPV6:
    580 		varnum = NPFVAR_ICMP6;
    581 		break;
    582 	default:
    583 		assert(false);
    584 	}
    585 
    586 	if (!npfvar_add_element(vp, varnum, &type, sizeof(type)))
    587 		goto out;
    588 
    589 	if (!npfvar_add_element(vp, varnum, &code, sizeof(code)))
    590 		goto out;
    591 
    592 	return vp;
    593 out:
    594 	npfvar_destroy(vp);
    595 	return NULL;
    596 }
    597