Home | History | Annotate | Line # | Download | only in npfctl
npf_data.c revision 1.21
      1 /*	$NetBSD: npf_data.c,v 1.21 2013/11/08 00:38:26 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.21 2013/11/08 00:38:26 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 <stddef.h>
     51 #include <string.h>
     52 #include <ctype.h>
     53 #include <err.h>
     54 #include <errno.h>
     55 #include <ifaddrs.h>
     56 #include <netdb.h>
     57 
     58 #include "npfctl.h"
     59 
     60 static struct ifaddrs *		ifs_list = NULL;
     61 
     62 void
     63 npfctl_note_interface(const char *ifname)
     64 {
     65 	unsigned long if_idx = if_nametoindex(ifname);
     66 	bool testif = npfctl_debug_addif(ifname);
     67 	const char *p = ifname;
     68 
     69 	/* If such interface exists or if it is a test interface - done. */
     70 	if (if_idx || testif) {
     71 		return;
     72 	}
     73 
     74 	/*
     75 	 * Minimum sanity check.  The interface name shall be non-empty
     76 	 * string shorter than IFNAMSIZ and alphanumeric only.
     77 	 */
     78 	if (*p == '\0') {
     79 		goto invalid;
     80 	}
     81 	while (*p) {
     82 		const size_t len = (ptrdiff_t)p - (ptrdiff_t)ifname;
     83 
     84 		if (!isalnum((unsigned char)*p) || len > IFNAMSIZ) {
     85 invalid:		yyerror("illegitimate interface name '%s'", ifname);
     86 		}
     87 		p++;
     88 	}
     89 
     90 	/* Throw a warning, so that the user could double check. */
     91 	warnx("warning - unknown interface '%s'", ifname);
     92 }
     93 
     94 static unsigned long
     95 npfctl_find_ifindex(const char *ifname)
     96 {
     97 	unsigned long if_idx = if_nametoindex(ifname);
     98 	bool testif = npfctl_debug_addif(ifname);
     99 
    100 	if (!if_idx) {
    101 		if (testif) {
    102 			static u_int dummy_if_idx = (1 << 15);
    103 			return ++dummy_if_idx;
    104 		}
    105 		yyerror("unknown interface '%s'", ifname);
    106 	}
    107 	return if_idx;
    108 }
    109 
    110 static bool
    111 npfctl_copy_address(sa_family_t fam, npf_addr_t *addr, const void *ptr)
    112 {
    113 	memset(addr, 0, sizeof(npf_addr_t));
    114 
    115 	switch (fam) {
    116 	case AF_INET: {
    117 		const struct sockaddr_in *sin = ptr;
    118 		memcpy(addr, &sin->sin_addr, sizeof(sin->sin_addr));
    119 		return true;
    120 	}
    121 	case AF_INET6: {
    122 		const struct sockaddr_in6 *sin6 = ptr;
    123 		memcpy(addr, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
    124 		return true;
    125 	}
    126 	default:
    127 		yyerror("unknown address family %u", fam);
    128 		return false;
    129 	}
    130 }
    131 
    132 static bool
    133 npfctl_parse_fam_addr(const char *name, sa_family_t *fam, npf_addr_t *addr)
    134 {
    135 	static const struct addrinfo hint = {
    136 		.ai_family = AF_UNSPEC,
    137 		.ai_flags = AI_NUMERICHOST
    138 	};
    139 	struct addrinfo *ai;
    140 	int ret;
    141 
    142 	ret = getaddrinfo(name, NULL, &hint, &ai);
    143 	if (ret) {
    144 		yyerror("cannot parse '%s' (%s)", name, gai_strerror(ret));
    145 		return false;
    146 	}
    147 	if (fam) {
    148 		*fam = ai->ai_family;
    149 	}
    150 	if (!npfctl_copy_address(*fam, addr, ai->ai_addr)) {
    151 		return false;
    152 	}
    153 	freeaddrinfo(ai);
    154 	return true;
    155 }
    156 
    157 static bool
    158 npfctl_parse_mask(const char *s, sa_family_t fam, npf_netmask_t *mask)
    159 {
    160 	char *ep = NULL;
    161 	npf_addr_t addr;
    162 	uint8_t *ap;
    163 
    164 	if (s) {
    165 		errno = 0;
    166 		*mask = (npf_netmask_t)strtol(s, &ep, 0);
    167 		if (*ep == '\0' && s != ep && errno != ERANGE)
    168 			return true;
    169 		if (!npfctl_parse_fam_addr(s, &fam, &addr))
    170 			return false;
    171 	}
    172 
    173 	assert(fam == AF_INET || fam == AF_INET6);
    174 	*mask = NPF_NO_NETMASK;
    175 	if (ep == NULL) {
    176 		return true;
    177 	}
    178 
    179 	ap = addr.s6_addr + (*mask / 8) - 1;
    180 	while (ap >= addr.s6_addr) {
    181 		for (int j = 8; j > 0; j--) {
    182 			if (*ap & 1)
    183 				return true;
    184 			*ap >>= 1;
    185 			(*mask)--;
    186 			if (*mask == 0)
    187 				return true;
    188 		}
    189 		ap--;
    190 	}
    191 	return true;
    192 }
    193 
    194 /*
    195  * npfctl_parse_fam_addr_mask: return address family, address and mask.
    196  *
    197  * => Mask is optional and can be NULL.
    198  * => Returns true on success or false if unable to parse.
    199  */
    200 npfvar_t *
    201 npfctl_parse_fam_addr_mask(const char *addr, const char *mask,
    202     unsigned long *nummask)
    203 {
    204 	npfvar_t *vp = npfvar_create(".addr");
    205 	fam_addr_mask_t fam;
    206 
    207 	memset(&fam, 0, sizeof(fam));
    208 
    209 	if (!npfctl_parse_fam_addr(addr, &fam.fam_family, &fam.fam_addr))
    210 		goto out;
    211 
    212 	/*
    213 	 * Note: both mask and nummask may be NULL.  In such case,
    214 	 * npfctl_parse_mask() will handle and will set full mask.
    215 	 */
    216 	if (nummask) {
    217 		fam.fam_mask = *nummask;
    218 	} else if (!npfctl_parse_mask(mask, fam.fam_family, &fam.fam_mask)) {
    219 		goto out;
    220 	}
    221 
    222 	if (!npfvar_add_element(vp, NPFVAR_FAM, &fam, sizeof(fam)))
    223 		goto out;
    224 
    225 	return vp;
    226 out:
    227 	npfvar_destroy(vp);
    228 	return NULL;
    229 }
    230 
    231 npfvar_t *
    232 npfctl_parse_table_id(const char *id)
    233 {
    234 	npfvar_t *vp;
    235 
    236 	if (!npfctl_table_exists_p(id)) {
    237 		yyerror("table '%s' is not defined", id);
    238 		return NULL;
    239 	}
    240 	vp = npfvar_create(".table");
    241 
    242 	if (!npfvar_add_element(vp, NPFVAR_TABLE, id, strlen(id) + 1))
    243 		goto out;
    244 
    245 	return vp;
    246 out:
    247 	npfvar_destroy(vp);
    248 	return NULL;
    249 }
    250 
    251 /*
    252  * npfctl_parse_port_range: create a port-range variable.  Note that the
    253  * passed port numbers should be in host byte order.
    254  */
    255 npfvar_t *
    256 npfctl_parse_port_range(in_port_t s, in_port_t e)
    257 {
    258 	npfvar_t *vp = npfvar_create(".port_range");
    259 	port_range_t pr;
    260 
    261 	pr.pr_start = htons(s);
    262 	pr.pr_end = htons(e);
    263 
    264 	if (!npfvar_add_element(vp, NPFVAR_PORT_RANGE, &pr, sizeof(pr)))
    265 		goto out;
    266 
    267 	return vp;
    268 out:
    269 	npfvar_destroy(vp);
    270 	return NULL;
    271 }
    272 
    273 npfvar_t *
    274 npfctl_parse_port_range_variable(const char *v)
    275 {
    276 	npfvar_t *vp = npfvar_lookup(v);
    277 	size_t count = npfvar_get_count(vp);
    278 	npfvar_t *pvp = npfvar_create(".port_range");
    279 	port_range_t *pr;
    280 	in_port_t p;
    281 
    282 	for (size_t i = 0; i < count; i++) {
    283 		int type = npfvar_get_type(vp, i);
    284 		void *data = npfvar_get_data(vp, type, i);
    285 
    286 		switch (type) {
    287 		case NPFVAR_IDENTIFIER:
    288 		case NPFVAR_STRING:
    289 			p = npfctl_portno(data);
    290 			npfvar_add_elements(pvp, npfctl_parse_port_range(p, p));
    291 			break;
    292 		case NPFVAR_PORT_RANGE:
    293 			pr = data;
    294 			npfvar_add_element(pvp, NPFVAR_PORT_RANGE, pr,
    295 			    sizeof(*pr));
    296 			break;
    297 		case NPFVAR_NUM:
    298 			p = *(unsigned long *)data;
    299 			npfvar_add_elements(pvp, npfctl_parse_port_range(p, p));
    300 			break;
    301 		default:
    302 			yyerror("wrong variable '%s' type '%s' for port range",
    303 			    v, npfvar_type(type));
    304 			npfvar_destroy(pvp);
    305 			return NULL;
    306 		}
    307 	}
    308 	return pvp;
    309 }
    310 
    311 npfvar_t *
    312 npfctl_parse_ifnet(const char *ifname, const int family)
    313 {
    314 	npfvar_t *vpa, *vp;
    315 	struct ifaddrs *ifa;
    316 	ifnet_addr_t ifna;
    317 
    318 	if (ifs_list == NULL && getifaddrs(&ifs_list) == -1) {
    319 		err(EXIT_FAILURE, "getifaddrs");
    320 	}
    321 
    322 	vpa = npfvar_create(".ifaddrs");
    323 	ifna.ifna_name = estrdup(ifname);
    324 	ifna.ifna_addrs = vpa;
    325 	ifna.ifna_index = npfctl_find_ifindex(ifname);
    326 	assert(ifna.ifna_index != 0);
    327 
    328 	for (ifa = ifs_list; ifa != NULL; ifa = ifa->ifa_next) {
    329 		fam_addr_mask_t fam;
    330 		struct sockaddr *sa;
    331 
    332 		if (strcmp(ifa->ifa_name, ifname) != 0)
    333 			continue;
    334 
    335 		if ((ifa->ifa_flags & IFF_UP) == 0)
    336 			warnx("interface '%s' is down", ifname);
    337 
    338 		sa = ifa->ifa_addr;
    339 		if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
    340 			continue;
    341 		if (family != AF_UNSPEC && sa->sa_family != family)
    342 			continue;
    343 
    344 		memset(&fam, 0, sizeof(fam));
    345 		fam.fam_family = sa->sa_family;
    346 		fam.fam_ifindex = ifna.ifna_index;
    347 
    348 		if (!npfctl_copy_address(sa->sa_family, &fam.fam_addr, sa))
    349 			goto out;
    350 
    351 		if (!npfctl_parse_mask(NULL, fam.fam_family, &fam.fam_mask))
    352 			goto out;
    353 
    354 		if (!npfvar_add_element(vpa, NPFVAR_FAM, &fam, sizeof(fam)))
    355 			goto out;
    356 	}
    357 	if (npfvar_get_count(vpa) == 0) {
    358 		yyerror("no addresses matched for interface '%s'", ifname);
    359 		goto out;
    360 	}
    361 
    362 	vp = npfvar_create(".interface");
    363 	npfvar_add_element(vp, NPFVAR_INTERFACE, &ifna, sizeof(ifna));
    364 	return vp;
    365 out:
    366 	npfvar_destroy(ifna.ifna_addrs);
    367 	return NULL;
    368 }
    369 
    370 bool
    371 npfctl_parse_cidr(char *cidr, fam_addr_mask_t *fam, int *alen)
    372 {
    373 	char *mask, *p;
    374 
    375 	p = strchr(cidr, '\n');
    376 	if (p) {
    377 		*p = '\0';
    378 	}
    379 	mask = strchr(cidr, '/');
    380 	if (mask) {
    381 		*mask++ = '\0';
    382 	}
    383 
    384 	memset(fam, 0, sizeof(*fam));
    385 	if (!npfctl_parse_fam_addr(cidr, &fam->fam_family, &fam->fam_addr)) {
    386 		return false;
    387 	}
    388 	if (!npfctl_parse_mask(mask, fam->fam_family, &fam->fam_mask)) {
    389 		return false;
    390 	}
    391 	switch (fam->fam_family) {
    392 	case AF_INET:
    393 		*alen = sizeof(struct in_addr);
    394 		break;
    395 	case AF_INET6:
    396 		*alen = sizeof(struct in6_addr);
    397 		break;
    398 	default:
    399 		return false;
    400 	}
    401 	return true;
    402 }
    403 
    404 int
    405 npfctl_protono(const char *proto)
    406 {
    407 	struct protoent *pe;
    408 
    409 	pe = getprotobyname(proto);
    410 	if (pe == NULL) {
    411 		yyerror("unknown protocol '%s'", proto);
    412 		return -1;
    413 	}
    414 	return pe->p_proto;
    415 }
    416 
    417 /*
    418  * npfctl_portno: convert port identifier (string) to a number.
    419  *
    420  * => Returns port number in host byte order.
    421  */
    422 in_port_t
    423 npfctl_portno(const char *port)
    424 {
    425 	struct addrinfo *ai, *rai;
    426 	in_port_t p = 0;
    427 	int e;
    428 
    429 	e = getaddrinfo(NULL, port, NULL, &rai);
    430 	if (e != 0) {
    431 		yyerror("invalid port name '%s' (%s)", port, gai_strerror(e));
    432 		return 0;
    433 	}
    434 
    435 	for (ai = rai; ai; ai = ai->ai_next) {
    436 		switch (ai->ai_family) {
    437 		case AF_INET: {
    438 			struct sockaddr_in *sin = (void *)ai->ai_addr;
    439 			p = sin->sin_port;
    440 			goto out;
    441 		}
    442 		case AF_INET6: {
    443 			struct sockaddr_in6 *sin6 = (void *)ai->ai_addr;
    444 			p = sin6->sin6_port;
    445 			goto out;
    446 		}
    447 		default:
    448 			break;
    449 		}
    450 	}
    451 out:
    452 	freeaddrinfo(rai);
    453 	return ntohs(p);
    454 }
    455 
    456 npfvar_t *
    457 npfctl_parse_tcpflag(const char *s)
    458 {
    459 	uint8_t tfl = 0;
    460 
    461 	while (*s) {
    462 		switch (*s) {
    463 		case 'F': tfl |= TH_FIN; break;
    464 		case 'S': tfl |= TH_SYN; break;
    465 		case 'R': tfl |= TH_RST; break;
    466 		case 'P': tfl |= TH_PUSH; break;
    467 		case 'A': tfl |= TH_ACK; break;
    468 		case 'U': tfl |= TH_URG; break;
    469 		case 'E': tfl |= TH_ECE; break;
    470 		case 'W': tfl |= TH_CWR; break;
    471 		default:
    472 			yyerror("invalid flag '%c'", *s);
    473 			return NULL;
    474 		}
    475 		s++;
    476 	}
    477 
    478 	npfvar_t *vp = npfvar_create(".tcp_flag");
    479 	if (!npfvar_add_element(vp, NPFVAR_TCPFLAG, &tfl, sizeof(tfl))) {
    480 		npfvar_destroy(vp);
    481 		return NULL;
    482 	}
    483 
    484 	return vp;
    485 }
    486 
    487 uint8_t
    488 npfctl_icmptype(int proto, const char *type)
    489 {
    490 	uint8_t ul;
    491 
    492 	switch (proto) {
    493 	case IPPROTO_ICMP:
    494 		for (ul = 0; icmp_type[ul]; ul++)
    495 			if (strcmp(icmp_type[ul], type) == 0)
    496 				return ul;
    497 		break;
    498 	case IPPROTO_ICMPV6:
    499 		for (ul = 0; icmp6_type_err[ul]; ul++)
    500 			if (strcmp(icmp6_type_err[ul], type) == 0)
    501 				return ul;
    502 		for (ul = 0; icmp6_type_info[ul]; ul++)
    503 			if (strcmp(icmp6_type_info[ul], type) == 0)
    504 				return (ul+128);
    505 		break;
    506 	default:
    507 		assert(false);
    508 	}
    509 
    510 	yyerror("unknown icmp-type %s", type);
    511 	return ~0;
    512 }
    513 
    514 uint8_t
    515 npfctl_icmpcode(int proto, uint8_t type, const char *code)
    516 {
    517 	const char * const *arr;
    518 
    519 	switch (proto) {
    520 	case IPPROTO_ICMP:
    521 		switch (type) {
    522 		case ICMP_ECHOREPLY:
    523 		case ICMP_SOURCEQUENCH:
    524 		case ICMP_ALTHOSTADDR:
    525 		case ICMP_ECHO:
    526 		case ICMP_ROUTERSOLICIT:
    527 		case ICMP_TSTAMP:
    528 		case ICMP_TSTAMPREPLY:
    529 		case ICMP_IREQ:
    530 		case ICMP_IREQREPLY:
    531 		case ICMP_MASKREQ:
    532 		case ICMP_MASKREPLY:
    533 			arr = icmp_code_none;
    534 			break;
    535 		case ICMP_ROUTERADVERT:
    536 			arr = icmp_code_routeradvert;
    537 			break;
    538 		case ICMP_UNREACH:
    539 			arr = icmp_code_unreach;
    540 			break;
    541 		case ICMP_REDIRECT:
    542 			arr = icmp_code_redirect;
    543 			break;
    544 		case ICMP_TIMXCEED:
    545 			arr = icmp_code_timxceed;
    546 			break;
    547 		case ICMP_PARAMPROB:
    548 			arr = icmp_code_paramprob;
    549 			break;
    550 		case ICMP_PHOTURIS:
    551 			arr = icmp_code_photuris;
    552 			break;
    553 		default:
    554 			yyerror("unknown icmp-type %d while parsing code %s",
    555 				type, code);
    556 			return ~0;
    557 		}
    558 		break;
    559 	case IPPROTO_ICMPV6:
    560 		switch (type) {
    561 		case ICMP6_DST_UNREACH:
    562 			arr = icmp6_code_unreach;
    563 			break;
    564 		case ICMP6_TIME_EXCEEDED:
    565 			arr = icmp6_code_timxceed;
    566 			break;
    567 		case ICMP6_PARAM_PROB:
    568 			arr = icmp6_code_paramprob;
    569 			break;
    570 		case ICMP6_PACKET_TOO_BIG:
    571 		/* code-less info ICMPs */
    572 		case ICMP6_ECHO_REQUEST:
    573 		case ICMP6_ECHO_REPLY:
    574 		case MLD_LISTENER_QUERY:
    575 		case MLD_LISTENER_REPORT:
    576 		case MLD_LISTENER_DONE:
    577 		case ND_ROUTER_SOLICIT:
    578 		case ND_ROUTER_ADVERT:
    579 		case ND_NEIGHBOR_SOLICIT:
    580 		case ND_NEIGHBOR_ADVERT:
    581 		case ND_REDIRECT:
    582 			arr = icmp6_code_none;
    583 			break;
    584 		/* XXX TODO: info ICMPs with code values */
    585 		default:
    586 			yyerror("unknown icmp-type %d while parsing code %s",
    587 				type, code);
    588 			return ~0;
    589 		}
    590 		break;
    591 	default:
    592 		assert(false);
    593 	}
    594 
    595 	for (uint8_t ul = 0; arr[ul]; ul++) {
    596 		if (strcmp(arr[ul], code) == 0)
    597 			return ul;
    598 	}
    599 	yyerror("unknown code %s for icmp-type %d", code, type);
    600 	return ~0;
    601 }
    602 
    603 npfvar_t *
    604 npfctl_parse_icmp(int proto, int type, int code)
    605 {
    606 	npfvar_t *vp = npfvar_create(".icmp");
    607 
    608 	if (!npfvar_add_element(vp, NPFVAR_ICMP, &type, sizeof(type)))
    609 		goto out;
    610 
    611 	if (!npfvar_add_element(vp, NPFVAR_ICMP, &code, sizeof(code)))
    612 		goto out;
    613 
    614 	return vp;
    615 out:
    616 	npfvar_destroy(vp);
    617 	return NULL;
    618 }
    619