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