Home | History | Annotate | Line # | Download | only in npfctl
npf_data.c revision 1.12
      1 /*	$NetBSD: npf_data.c,v 1.12 2012/05/30 21:30:07 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.12 2012/05/30 21:30:07 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 #include <netinet/tcp.h>
     45 #include <net/if.h>
     46 
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <err.h>
     50 #include <errno.h>
     51 #include <ifaddrs.h>
     52 #include <netdb.h>
     53 
     54 #include "npfctl.h"
     55 
     56 static struct ifaddrs *		ifs_list = NULL;
     57 
     58 unsigned long
     59 npfctl_find_ifindex(const char *ifname)
     60 {
     61 	return if_nametoindex(ifname);
     62 }
     63 
     64 static bool
     65 npfctl_copy_address(sa_family_t fam, npf_addr_t *addr, const void *ptr)
     66 {
     67 	switch (fam) {
     68 	case AF_INET: {
     69 		const struct sockaddr_in *sin = ptr;
     70 		memcpy(addr, &sin->sin_addr, sizeof(sin->sin_addr));
     71 		return true;
     72 	}
     73 	case AF_INET6: {
     74 		const struct sockaddr_in6 *sin6 = ptr;
     75 		memcpy(addr, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
     76 		return true;
     77 	}
     78 	default:
     79 		yyerror("unknown address family %u", fam);
     80 		return false;
     81 	}
     82 }
     83 
     84 static bool
     85 npfctl_parse_fam_addr(const char *name, sa_family_t *fam, npf_addr_t *addr)
     86 {
     87 	static const struct addrinfo hint = {
     88 		.ai_family = AF_UNSPEC,
     89 		.ai_flags = AI_NUMERICHOST
     90 	};
     91 	struct addrinfo *ai;
     92 	int ret;
     93 
     94 	ret = getaddrinfo(name, NULL, &hint, &ai);
     95 	if (ret) {
     96 		yyerror("cannot parse '%s' (%s)", name, gai_strerror(ret));
     97 		return false;
     98 	}
     99 	if (fam) {
    100 		*fam = ai->ai_family;
    101 	}
    102 	if (!npfctl_copy_address(*fam, addr, ai->ai_addr)) {
    103 		return false;
    104 	}
    105 	freeaddrinfo(ai);
    106 	return true;
    107 }
    108 
    109 static bool
    110 npfctl_parse_mask(const char *s, sa_family_t fam, npf_netmask_t *mask)
    111 {
    112 	char *ep = NULL;
    113 	npf_addr_t addr;
    114 	uint8_t *ap;
    115 
    116 	if (s) {
    117 		errno = 0;
    118 		*mask = (npf_netmask_t)strtol(s, &ep, 0);
    119 		if (*ep == '\0' && s != ep && errno != ERANGE)
    120 			return true;
    121 		if (!npfctl_parse_fam_addr(s, &fam, &addr))
    122 			return false;
    123 	}
    124 
    125 	switch (fam) {
    126 	case AF_INET:
    127 		*mask = 32;
    128 		break;
    129 	case AF_INET6:
    130 		*mask = 128;
    131 		break;
    132 	default:
    133 		yyerror("unknown address family %u", fam);
    134 		return false;
    135 	}
    136 
    137 	if (ep == NULL) {
    138 		return true;
    139 	}
    140 	ap = addr.s6_addr + (*mask / 8) - 1;
    141 	while (ap >= addr.s6_addr) {
    142 		for (int j = 8; j > 0; j--) {
    143 			if (*ap & 1)
    144 				return true;
    145 			*ap >>= 1;
    146 			(*mask)--;
    147 			if (*mask == 0)
    148 				return true;
    149 		}
    150 		ap--;
    151 	}
    152 	return true;
    153 }
    154 
    155 /*
    156  * npfctl_parse_fam_addr_mask: return address family, address and mask.
    157  *
    158  * => Mask is optional and can be NULL.
    159  * => Returns true on success or false if unable to parse.
    160  */
    161 npfvar_t *
    162 npfctl_parse_fam_addr_mask(const char *addr, const char *mask,
    163     unsigned long *nummask)
    164 {
    165 	npfvar_t *vp = npfvar_create(".addr");
    166 	fam_addr_mask_t fam;
    167 
    168 	memset(&fam, 0, sizeof(fam));
    169 
    170 	if (!npfctl_parse_fam_addr(addr, &fam.fam_family, &fam.fam_addr))
    171 		goto out;
    172 
    173 	/*
    174 	 * Note: both mask and nummask may be NULL.  In such case,
    175 	 * npfctl_parse_mask() will handle and will set full mask.
    176 	 */
    177 	if (nummask) {
    178 		fam.fam_mask = *nummask;
    179 	} else if (!npfctl_parse_mask(mask, fam.fam_family, &fam.fam_mask)) {
    180 		goto out;
    181 	}
    182 
    183 	if (!npfvar_add_element(vp, NPFVAR_FAM, &fam, sizeof(fam)))
    184 		goto out;
    185 
    186 	return vp;
    187 out:
    188 	npfvar_destroy(vp);
    189 	return NULL;
    190 }
    191 
    192 npfvar_t *
    193 npfctl_parse_table_id(const char *id)
    194 {
    195 	npfvar_t *vp;
    196 
    197 	if (!npfctl_table_exists_p(id)) {
    198 		yyerror("table '%s' is not defined", id);
    199 		return NULL;
    200 	}
    201 	vp = npfvar_create(".table");
    202 
    203 	if (!npfvar_add_element(vp, NPFVAR_TABLE, id, strlen(id) + 1))
    204 		goto out;
    205 
    206 	return vp;
    207 out:
    208 	npfvar_destroy(vp);
    209 	return NULL;
    210 }
    211 
    212 /*
    213  * npfctl_parse_port_range: create a port-range variable.  Note that the
    214  * passed port numbers should be in host byte order.
    215  */
    216 npfvar_t *
    217 npfctl_parse_port_range(in_port_t s, in_port_t e)
    218 {
    219 	npfvar_t *vp = npfvar_create(".port_range");
    220 	port_range_t pr;
    221 
    222 	pr.pr_start = htons(s);
    223 	pr.pr_end = htons(e);
    224 
    225 	if (!npfvar_add_element(vp, NPFVAR_PORT_RANGE, &pr, sizeof(pr)))
    226 		goto out;
    227 
    228 	return vp;
    229 out:
    230 	npfvar_destroy(vp);
    231 	return NULL;
    232 }
    233 
    234 npfvar_t *
    235 npfctl_parse_port_range_variable(const char *v)
    236 {
    237 	npfvar_t *vp = npfvar_lookup(v);
    238 	size_t count = npfvar_get_count(vp);
    239 	npfvar_t *pvp = npfvar_create(".port_range");
    240 	port_range_t *pr;
    241 	in_port_t p;
    242 
    243 	for (size_t i = 0; i < count; i++) {
    244 		int type = npfvar_get_type(vp, i);
    245 		void *data = npfvar_get_data(vp, type, i);
    246 
    247 		switch (type) {
    248 		case NPFVAR_IDENTIFIER:
    249 		case NPFVAR_STRING:
    250 			p = npfctl_portno(data);
    251 			npfvar_add_elements(pvp, npfctl_parse_port_range(p, p));
    252 			break;
    253 		case NPFVAR_PORT_RANGE:
    254 			pr = data;
    255 			npfvar_add_element(pvp, NPFVAR_PORT_RANGE, pr,
    256 			    sizeof(*pr));
    257 			break;
    258 		case NPFVAR_NUM:
    259 			p = *(unsigned long *)data;
    260 			npfvar_add_elements(pvp, npfctl_parse_port_range(p, p));
    261 			break;
    262 		default:
    263 			yyerror("wrong variable '%s' type '%s' for port range",
    264 			    v, npfvar_type(type));
    265 			npfvar_destroy(pvp);
    266 			return NULL;
    267 		}
    268 	}
    269 	return pvp;
    270 }
    271 
    272 npfvar_t *
    273 npfctl_parse_iface(const char *ifname)
    274 {
    275 	npfvar_t *vp = npfvar_create(".iface");
    276 	struct ifaddrs *ifa;
    277 	fam_addr_mask_t fam;
    278 	bool gotif = false;
    279 
    280 	if (ifs_list == NULL && getifaddrs(&ifs_list) == -1) {
    281 		err(EXIT_FAILURE, "getifaddrs");
    282 	}
    283 	memset(&fam, 0, sizeof(fam));
    284 
    285 	npfvar_t *ip = npfvar_create(".ifname");
    286 	if (!npfvar_add_element(ip, NPFVAR_STRING, ifname, strlen(ifname) + 1))
    287 		goto out;
    288 
    289 	for (ifa = ifs_list; ifa != NULL; ifa = ifa->ifa_next) {
    290 		struct sockaddr *sa;
    291 		sa_family_t family;
    292 
    293 		if (strcmp(ifa->ifa_name, ifname) != 0)
    294 			continue;
    295 
    296 		gotif = true;
    297 		if ((ifa->ifa_flags & IFF_UP) == 0)
    298 			warnx("interface '%s' is down", ifname);
    299 
    300 		sa = ifa->ifa_addr;
    301 		family = sa->sa_family;
    302 		if (family != AF_INET && family != AF_INET6)
    303 			continue;
    304 
    305 		fam.fam_family = family;
    306 		fam.fam_interface = ip;
    307 
    308 		if (!npfctl_copy_address(family, &fam.fam_addr, sa))
    309 			goto out;
    310 
    311 		if (!npfctl_parse_mask(NULL, fam.fam_family, &fam.fam_mask))
    312 			goto out;
    313 
    314 		if (!npfvar_add_element(vp, NPFVAR_FAM, &fam, sizeof(fam)))
    315 			goto out;
    316 	}
    317 	if (!gotif) {
    318 		yyerror("interface '%s' not found", ifname);
    319 		goto out;
    320 	}
    321 	if (npfvar_get_count(vp) == 0) {
    322 		yyerror("no addresses matched for interface '%s'", ifname);
    323 		goto out;
    324 	}
    325 	return vp;
    326 out:
    327 	npfvar_destroy(vp);
    328 	npfvar_destroy(ip);
    329 	return NULL;
    330 }
    331 
    332 fam_addr_mask_t *
    333 npfctl_parse_cidr(char *cidr)
    334 {
    335 	npfvar_t *vp;
    336 	char *p;
    337 
    338 	p = strchr(cidr, '/');
    339 	if (p) {
    340 		*p++ = '\0';
    341 	}
    342 	vp = npfctl_parse_fam_addr_mask(cidr, p, NULL);
    343 	if (vp == NULL) {
    344 		return NULL;
    345 	}
    346 	return npfvar_get_data(vp, NPFVAR_FAM, 0);
    347 }
    348 
    349 /*
    350  * npfctl_portno: convert port identifier (string) to a number.
    351  *
    352  * => Returns port number in host byte order.
    353  */
    354 in_port_t
    355 npfctl_portno(const char *port)
    356 {
    357 	struct addrinfo *ai, *rai;
    358 	in_port_t p = 0;
    359 	int e;
    360 
    361 	e = getaddrinfo(NULL, port, NULL, &rai);
    362 	if (e != 0) {
    363 		yyerror("invalid port name: '%s' (%s)", port, gai_strerror(e));
    364 		return 0;
    365 	}
    366 
    367 	for (ai = rai; ai; ai = ai->ai_next) {
    368 		switch (ai->ai_family) {
    369 		case AF_INET: {
    370 			struct sockaddr_in *sin = (void *)ai->ai_addr;
    371 			p = sin->sin_port;
    372 			goto out;
    373 		}
    374 		case AF_INET6: {
    375 			struct sockaddr_in6 *sin6 = (void *)ai->ai_addr;
    376 			p = sin6->sin6_port;
    377 			goto out;
    378 		}
    379 		default:
    380 			break;
    381 		}
    382 	}
    383 out:
    384 	freeaddrinfo(rai);
    385 	return ntohs(p);
    386 }
    387 
    388 npfvar_t *
    389 npfctl_parse_tcpflag(const char *s)
    390 {
    391 	uint8_t tfl = 0;
    392 
    393 	while (*s) {
    394 		switch (*s) {
    395 		case 'F': tfl |= TH_FIN; break;
    396 		case 'S': tfl |= TH_SYN; break;
    397 		case 'R': tfl |= TH_RST; break;
    398 		case 'P': tfl |= TH_PUSH; break;
    399 		case 'A': tfl |= TH_ACK; break;
    400 		case 'U': tfl |= TH_URG; break;
    401 		case 'E': tfl |= TH_ECE; break;
    402 		case 'W': tfl |= TH_CWR; break;
    403 		default:
    404 			yyerror("invalid flag '%c'", *s);
    405 			return NULL;
    406 		}
    407 		s++;
    408 	}
    409 
    410 	npfvar_t *vp = npfvar_create(".tcp_flag");
    411 	if (!npfvar_add_element(vp, NPFVAR_TCPFLAG, &tfl, sizeof(tfl))) {
    412 		npfvar_destroy(vp);
    413 		return NULL;
    414 	}
    415 
    416 	return vp;
    417 }
    418 
    419 uint8_t
    420 npfctl_icmptype(const char *type)
    421 {
    422 	for (uint8_t ul = 0; icmp_type[ul]; ul++)
    423 		if (strcmp(icmp_type[ul], type) == 0)
    424 			return ul;
    425 	return ~0;
    426 }
    427 
    428 uint8_t
    429 npfctl_icmpcode(uint8_t type, const char *code)
    430 {
    431 	const char **arr;
    432 
    433 	switch (type) {
    434 	case ICMP_ECHOREPLY:
    435 	case ICMP_SOURCEQUENCH:
    436 	case ICMP_ALTHOSTADDR:
    437 	case ICMP_ECHO:
    438 	case ICMP_ROUTERSOLICIT:
    439 	case ICMP_TSTAMP:
    440 	case ICMP_TSTAMPREPLY:
    441 	case ICMP_IREQ:
    442 	case ICMP_IREQREPLY:
    443 	case ICMP_MASKREQ:
    444 	case ICMP_MASKREPLY:
    445 		arr = icmp_code_none;
    446 		break;
    447 	case ICMP_ROUTERADVERT:
    448 		arr = icmp_code_routeradvert;
    449 		break;
    450 	case ICMP_UNREACH:
    451 		arr = icmp_code_unreach;
    452 		break;
    453 	case ICMP_REDIRECT:
    454 		arr = icmp_code_redirect;
    455 		break;
    456 	case ICMP_TIMXCEED:
    457 		arr = icmp_code_timxceed;
    458 		break;
    459 	case ICMP_PARAMPROB:
    460 		arr = icmp_code_paramprob;
    461 		break;
    462 	case ICMP_PHOTURIS:
    463 		arr = icmp_code_photuris;
    464 		break;
    465 	default:
    466 		return ~0;
    467 	}
    468 
    469 	for (uint8_t ul = 0; arr[ul]; ul++) {
    470 		if (strcmp(arr[ul], code) == 0)
    471 			return ul;
    472 	}
    473 	return ~0;
    474 }
    475 
    476 npfvar_t *
    477 npfctl_parse_icmp(uint8_t type, uint8_t code)
    478 {
    479 	npfvar_t *vp = npfvar_create(".icmp");
    480 
    481 	if (!npfvar_add_element(vp, NPFVAR_ICMP, &type, sizeof(type)))
    482 		goto out;
    483 
    484 	if (!npfvar_add_element(vp, NPFVAR_ICMP, &code, sizeof(code)))
    485 		goto out;
    486 
    487 	return vp;
    488 out:
    489 	npfvar_destroy(vp);
    490 	return NULL;
    491 }
    492