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