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