Home | History | Annotate | Line # | Download | only in pfctl
pfctl_parser.c revision 1.1.1.4
      1 /*	$NetBSD: pfctl_parser.c,v 1.1.1.4 2009/12/01 07:03:09 martti Exp $	*/
      2 /*	$OpenBSD: pfctl_parser.c,v 1.234 2006/10/31 23:46:24 mcbride Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2001 Daniel Hartmeier
      6  * Copyright (c) 2002,2003 Henning Brauer
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  *
     13  *    - Redistributions of source code must retain the above copyright
     14  *      notice, this list of conditions and the following disclaimer.
     15  *    - Redistributions in binary form must reproduce the above
     16  *      copyright notice, this list of conditions and the following
     17  *      disclaimer in the documentation and/or other materials provided
     18  *      with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <sys/types.h>
     36 #include <sys/ioctl.h>
     37 #include <sys/socket.h>
     38 #include <sys/param.h>
     39 #include <sys/proc.h>
     40 #include <net/if.h>
     41 #include <netinet/in.h>
     42 #include <netinet/in_systm.h>
     43 #include <netinet/ip.h>
     44 #include <netinet/ip_icmp.h>
     45 #include <netinet/icmp6.h>
     46 #include <net/pfvar.h>
     47 #include <arpa/inet.h>
     48 
     49 #include <stdio.h>
     50 #include <stdlib.h>
     51 #include <string.h>
     52 #include <ctype.h>
     53 #include <netdb.h>
     54 #include <stdarg.h>
     55 #include <errno.h>
     56 #include <err.h>
     57 #include <ifaddrs.h>
     58 #include <unistd.h>
     59 
     60 #include "pfctl_parser.h"
     61 #include "pfctl.h"
     62 
     63 void		 print_op (u_int8_t, const char *, const char *);
     64 void		 print_port (u_int8_t, u_int16_t, u_int16_t, const char *);
     65 void		 print_ugid (u_int8_t, unsigned, unsigned, const char *, unsigned);
     66 void		 print_flags (u_int8_t);
     67 void		 print_fromto(struct pf_rule_addr *, pf_osfp_t,
     68 		    struct pf_rule_addr *, u_int8_t, u_int8_t, int);
     69 int		 ifa_skip_if(const char *filter, struct node_host *p);
     70 
     71 struct node_host	*ifa_grouplookup(const char *, int);
     72 struct node_host	*host_if(const char *, int);
     73 struct node_host	*host_v4(const char *, int);
     74 struct node_host	*host_v6(const char *, int);
     75 struct node_host	*host_dns(const char *, int, int);
     76 
     77 const char *tcpflags = "FSRPAUEW";
     78 
     79 static const struct icmptypeent icmp_type[] = {
     80 	{ "echoreq",	ICMP_ECHO },
     81 	{ "echorep",	ICMP_ECHOREPLY },
     82 	{ "unreach",	ICMP_UNREACH },
     83 	{ "squench",	ICMP_SOURCEQUENCH },
     84 	{ "redir",	ICMP_REDIRECT },
     85 	{ "althost",	ICMP_ALTHOSTADDR },
     86 	{ "routeradv",	ICMP_ROUTERADVERT },
     87 	{ "routersol",	ICMP_ROUTERSOLICIT },
     88 	{ "timex",	ICMP_TIMXCEED },
     89 	{ "paramprob",	ICMP_PARAMPROB },
     90 	{ "timereq",	ICMP_TSTAMP },
     91 	{ "timerep",	ICMP_TSTAMPREPLY },
     92 	{ "inforeq",	ICMP_IREQ },
     93 	{ "inforep",	ICMP_IREQREPLY },
     94 	{ "maskreq",	ICMP_MASKREQ },
     95 	{ "maskrep",	ICMP_MASKREPLY },
     96 	{ "trace",	ICMP_TRACEROUTE },
     97 	{ "dataconv",	ICMP_DATACONVERR },
     98 	{ "mobredir",	ICMP_MOBILE_REDIRECT },
     99 	{ "ipv6-where",	ICMP_IPV6_WHEREAREYOU },
    100 	{ "ipv6-here",	ICMP_IPV6_IAMHERE },
    101 	{ "mobregreq",	ICMP_MOBILE_REGREQUEST },
    102 	{ "mobregrep",	ICMP_MOBILE_REGREPLY },
    103 	{ "skip",	ICMP_SKIP },
    104 	{ "photuris",	ICMP_PHOTURIS }
    105 };
    106 
    107 static const struct icmptypeent icmp6_type[] = {
    108 	{ "unreach",	ICMP6_DST_UNREACH },
    109 	{ "toobig",	ICMP6_PACKET_TOO_BIG },
    110 	{ "timex",	ICMP6_TIME_EXCEEDED },
    111 	{ "paramprob",	ICMP6_PARAM_PROB },
    112 	{ "echoreq",	ICMP6_ECHO_REQUEST },
    113 	{ "echorep",	ICMP6_ECHO_REPLY },
    114 	{ "groupqry",	ICMP6_MEMBERSHIP_QUERY },
    115 	{ "listqry",	MLD_LISTENER_QUERY },
    116 	{ "grouprep",	ICMP6_MEMBERSHIP_REPORT },
    117 	{ "listenrep",	MLD_LISTENER_REPORT },
    118 	{ "groupterm",	ICMP6_MEMBERSHIP_REDUCTION },
    119 	{ "listendone", MLD_LISTENER_DONE },
    120 	{ "routersol",	ND_ROUTER_SOLICIT },
    121 	{ "routeradv",	ND_ROUTER_ADVERT },
    122 	{ "neighbrsol", ND_NEIGHBOR_SOLICIT },
    123 	{ "neighbradv", ND_NEIGHBOR_ADVERT },
    124 	{ "redir",	ND_REDIRECT },
    125 	{ "routrrenum", ICMP6_ROUTER_RENUMBERING },
    126 	{ "wrureq",	ICMP6_WRUREQUEST },
    127 	{ "wrurep",	ICMP6_WRUREPLY },
    128 	{ "fqdnreq",	ICMP6_FQDN_QUERY },
    129 	{ "fqdnrep",	ICMP6_FQDN_REPLY },
    130 	{ "niqry",	ICMP6_NI_QUERY },
    131 	{ "nirep",	ICMP6_NI_REPLY },
    132 	{ "mtraceresp",	MLD_MTRACE_RESP },
    133 	{ "mtrace",	MLD_MTRACE }
    134 };
    135 
    136 static const struct icmpcodeent icmp_code[] = {
    137 	{ "net-unr",		ICMP_UNREACH,	ICMP_UNREACH_NET },
    138 	{ "host-unr",		ICMP_UNREACH,	ICMP_UNREACH_HOST },
    139 	{ "proto-unr",		ICMP_UNREACH,	ICMP_UNREACH_PROTOCOL },
    140 	{ "port-unr",		ICMP_UNREACH,	ICMP_UNREACH_PORT },
    141 	{ "needfrag",		ICMP_UNREACH,	ICMP_UNREACH_NEEDFRAG },
    142 	{ "srcfail",		ICMP_UNREACH,	ICMP_UNREACH_SRCFAIL },
    143 	{ "net-unk",		ICMP_UNREACH,	ICMP_UNREACH_NET_UNKNOWN },
    144 	{ "host-unk",		ICMP_UNREACH,	ICMP_UNREACH_HOST_UNKNOWN },
    145 	{ "isolate",		ICMP_UNREACH,	ICMP_UNREACH_ISOLATED },
    146 	{ "net-prohib",		ICMP_UNREACH,	ICMP_UNREACH_NET_PROHIB },
    147 	{ "host-prohib",	ICMP_UNREACH,	ICMP_UNREACH_HOST_PROHIB },
    148 	{ "net-tos",		ICMP_UNREACH,	ICMP_UNREACH_TOSNET },
    149 	{ "host-tos",		ICMP_UNREACH,	ICMP_UNREACH_TOSHOST },
    150 	{ "filter-prohib",	ICMP_UNREACH,	ICMP_UNREACH_FILTER_PROHIB },
    151 	{ "host-preced",	ICMP_UNREACH,	ICMP_UNREACH_HOST_PRECEDENCE },
    152 	{ "cutoff-preced",	ICMP_UNREACH,	ICMP_UNREACH_PRECEDENCE_CUTOFF },
    153 	{ "redir-net",		ICMP_REDIRECT,	ICMP_REDIRECT_NET },
    154 	{ "redir-host",		ICMP_REDIRECT,	ICMP_REDIRECT_HOST },
    155 	{ "redir-tos-net",	ICMP_REDIRECT,	ICMP_REDIRECT_TOSNET },
    156 	{ "redir-tos-host",	ICMP_REDIRECT,	ICMP_REDIRECT_TOSHOST },
    157 	{ "normal-adv",		ICMP_ROUTERADVERT, ICMP_ROUTERADVERT_NORMAL },
    158 	{ "common-adv",		ICMP_ROUTERADVERT, ICMP_ROUTERADVERT_NOROUTE_COMMON },
    159 	{ "transit",		ICMP_TIMXCEED,	ICMP_TIMXCEED_INTRANS },
    160 	{ "reassemb",		ICMP_TIMXCEED,	ICMP_TIMXCEED_REASS },
    161 	{ "badhead",		ICMP_PARAMPROB,	ICMP_PARAMPROB_ERRATPTR },
    162 	{ "optmiss",		ICMP_PARAMPROB,	ICMP_PARAMPROB_OPTABSENT },
    163 	{ "badlen",		ICMP_PARAMPROB,	ICMP_PARAMPROB_LENGTH },
    164 	{ "unknown-ind",	ICMP_PHOTURIS,	ICMP_PHOTURIS_UNKNOWN_INDEX },
    165 	{ "auth-fail",		ICMP_PHOTURIS,	ICMP_PHOTURIS_AUTH_FAILED },
    166 	{ "decrypt-fail",	ICMP_PHOTURIS,	ICMP_PHOTURIS_DECRYPT_FAILED }
    167 };
    168 
    169 static const struct icmpcodeent icmp6_code[] = {
    170 	{ "admin-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADMIN },
    171 	{ "noroute-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOROUTE },
    172 	{ "notnbr-unr",	ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOTNEIGHBOR },
    173 	{ "beyond-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_BEYONDSCOPE },
    174 	{ "addr-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR },
    175 	{ "port-unr", ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT },
    176 	{ "transit", ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_TRANSIT },
    177 	{ "reassemb", ICMP6_TIME_EXCEEDED, ICMP6_TIME_EXCEED_REASSEMBLY },
    178 	{ "badhead", ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER },
    179 	{ "nxthdr", ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER },
    180 	{ "redironlink", ND_REDIRECT, ND_REDIRECT_ONLINK },
    181 	{ "redirrouter", ND_REDIRECT, ND_REDIRECT_ROUTER }
    182 };
    183 
    184 const struct pf_timeout pf_timeouts[] = {
    185 	{ "tcp.first",		PFTM_TCP_FIRST_PACKET },
    186 	{ "tcp.opening",	PFTM_TCP_OPENING },
    187 	{ "tcp.established",	PFTM_TCP_ESTABLISHED },
    188 	{ "tcp.closing",	PFTM_TCP_CLOSING },
    189 	{ "tcp.finwait",	PFTM_TCP_FIN_WAIT },
    190 	{ "tcp.closed",		PFTM_TCP_CLOSED },
    191 	{ "tcp.tsdiff",		PFTM_TS_DIFF },
    192 	{ "udp.first",		PFTM_UDP_FIRST_PACKET },
    193 	{ "udp.single",		PFTM_UDP_SINGLE },
    194 	{ "udp.multiple",	PFTM_UDP_MULTIPLE },
    195 	{ "icmp.first",		PFTM_ICMP_FIRST_PACKET },
    196 	{ "icmp.error",		PFTM_ICMP_ERROR_REPLY },
    197 	{ "other.first",	PFTM_OTHER_FIRST_PACKET },
    198 	{ "other.single",	PFTM_OTHER_SINGLE },
    199 	{ "other.multiple",	PFTM_OTHER_MULTIPLE },
    200 	{ "frag",		PFTM_FRAG },
    201 	{ "interval",		PFTM_INTERVAL },
    202 	{ "adaptive.start",	PFTM_ADAPTIVE_START },
    203 	{ "adaptive.end",	PFTM_ADAPTIVE_END },
    204 	{ "src.track",		PFTM_SRC_NODE },
    205 	{ NULL,			0 }
    206 };
    207 
    208 const struct icmptypeent *
    209 geticmptypebynumber(u_int8_t type, sa_family_t af)
    210 {
    211 	unsigned int	i;
    212 
    213 	if (af != AF_INET6) {
    214 		for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0]));
    215 		    i++) {
    216 			if (type == icmp_type[i].type)
    217 				return (&icmp_type[i]);
    218 		}
    219 	} else {
    220 		for (i=0; i < (sizeof (icmp6_type) /
    221 		    sizeof(icmp6_type[0])); i++) {
    222 			if (type == icmp6_type[i].type)
    223 				 return (&icmp6_type[i]);
    224 		}
    225 	}
    226 	return (NULL);
    227 }
    228 
    229 const struct icmptypeent *
    230 geticmptypebyname(char *w, sa_family_t af)
    231 {
    232 	unsigned int	i;
    233 
    234 	if (af != AF_INET6) {
    235 		for (i=0; i < (sizeof (icmp_type) / sizeof(icmp_type[0]));
    236 		    i++) {
    237 			if (!strcmp(w, icmp_type[i].name))
    238 				return (&icmp_type[i]);
    239 		}
    240 	} else {
    241 		for (i=0; i < (sizeof (icmp6_type) /
    242 		    sizeof(icmp6_type[0])); i++) {
    243 			if (!strcmp(w, icmp6_type[i].name))
    244 				return (&icmp6_type[i]);
    245 		}
    246 	}
    247 	return (NULL);
    248 }
    249 
    250 const struct icmpcodeent *
    251 geticmpcodebynumber(u_int8_t type, u_int8_t code, sa_family_t af)
    252 {
    253 	unsigned int	i;
    254 
    255 	if (af != AF_INET6) {
    256 		for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0]));
    257 		    i++) {
    258 			if (type == icmp_code[i].type &&
    259 			    code == icmp_code[i].code)
    260 				return (&icmp_code[i]);
    261 		}
    262 	} else {
    263 		for (i=0; i < (sizeof (icmp6_code) /
    264 		    sizeof(icmp6_code[0])); i++) {
    265 			if (type == icmp6_code[i].type &&
    266 			    code == icmp6_code[i].code)
    267 				return (&icmp6_code[i]);
    268 		}
    269 	}
    270 	return (NULL);
    271 }
    272 
    273 const struct icmpcodeent *
    274 geticmpcodebyname(u_long type, char *w, sa_family_t af)
    275 {
    276 	unsigned int	i;
    277 
    278 	if (af != AF_INET6) {
    279 		for (i=0; i < (sizeof (icmp_code) / sizeof(icmp_code[0]));
    280 		    i++) {
    281 			if (type == icmp_code[i].type &&
    282 			    !strcmp(w, icmp_code[i].name))
    283 				return (&icmp_code[i]);
    284 		}
    285 	} else {
    286 		for (i=0; i < (sizeof (icmp6_code) /
    287 		    sizeof(icmp6_code[0])); i++) {
    288 			if (type == icmp6_code[i].type &&
    289 			    !strcmp(w, icmp6_code[i].name))
    290 				return (&icmp6_code[i]);
    291 		}
    292 	}
    293 	return (NULL);
    294 }
    295 
    296 void
    297 print_op(u_int8_t op, const char *a1, const char *a2)
    298 {
    299 	if (op == PF_OP_IRG)
    300 		printf(" %s >< %s", a1, a2);
    301 	else if (op == PF_OP_XRG)
    302 		printf(" %s <> %s", a1, a2);
    303 	else if (op == PF_OP_EQ)
    304 		printf(" = %s", a1);
    305 	else if (op == PF_OP_NE)
    306 		printf(" != %s", a1);
    307 	else if (op == PF_OP_LT)
    308 		printf(" < %s", a1);
    309 	else if (op == PF_OP_LE)
    310 		printf(" <= %s", a1);
    311 	else if (op == PF_OP_GT)
    312 		printf(" > %s", a1);
    313 	else if (op == PF_OP_GE)
    314 		printf(" >= %s", a1);
    315 	else if (op == PF_OP_RRG)
    316 		printf(" %s:%s", a1, a2);
    317 }
    318 
    319 void
    320 print_port(u_int8_t op, u_int16_t p1, u_int16_t p2, const char *proto)
    321 {
    322 	char		 a1[6], a2[6];
    323 	struct servent	*s;
    324 
    325 	s = getservbyport(p1, proto);
    326 	p1 = ntohs(p1);
    327 	p2 = ntohs(p2);
    328 	snprintf(a1, sizeof(a1), "%u", p1);
    329 	snprintf(a2, sizeof(a2), "%u", p2);
    330 	printf(" port");
    331 	if (s != NULL && (op == PF_OP_EQ || op == PF_OP_NE))
    332 		print_op(op, s->s_name, a2);
    333 	else
    334 		print_op(op, a1, a2);
    335 }
    336 
    337 void
    338 print_ugid(u_int8_t op, unsigned u1, unsigned u2, const char *t, unsigned umax)
    339 {
    340 	char	a1[11], a2[11];
    341 
    342 	snprintf(a1, sizeof(a1), "%u", u1);
    343 	snprintf(a2, sizeof(a2), "%u", u2);
    344 	printf(" %s", t);
    345 	if (u1 == umax && (op == PF_OP_EQ || op == PF_OP_NE))
    346 		print_op(op, "unknown", a2);
    347 	else
    348 		print_op(op, a1, a2);
    349 }
    350 
    351 void
    352 print_flags(u_int8_t f)
    353 {
    354 	int	i;
    355 
    356 	for (i = 0; tcpflags[i]; ++i)
    357 		if (f & (1 << i))
    358 			printf("%c", tcpflags[i]);
    359 }
    360 
    361 void
    362 print_fromto(struct pf_rule_addr *src, pf_osfp_t osfp, struct pf_rule_addr *dst,
    363     sa_family_t af, u_int8_t proto, int verbose)
    364 {
    365 	char buf[PF_OSFP_LEN*3];
    366 	if (src->addr.type == PF_ADDR_ADDRMASK &&
    367 	    dst->addr.type == PF_ADDR_ADDRMASK &&
    368 	    PF_AZERO(&src->addr.v.a.addr, AF_INET6) &&
    369 	    PF_AZERO(&src->addr.v.a.mask, AF_INET6) &&
    370 	    PF_AZERO(&dst->addr.v.a.addr, AF_INET6) &&
    371 	    PF_AZERO(&dst->addr.v.a.mask, AF_INET6) &&
    372 	    !src->neg && !dst->neg &&
    373 	    !src->port_op && !dst->port_op &&
    374 	    osfp == PF_OSFP_ANY)
    375 		printf(" all");
    376 	else {
    377 		printf(" from ");
    378 		if (src->neg)
    379 			printf("! ");
    380 		print_addr(&src->addr, af, verbose);
    381 		if (src->port_op)
    382 			print_port(src->port_op, src->port[0],
    383 			    src->port[1],
    384 			    proto == IPPROTO_TCP ? "tcp" : "udp");
    385 		if (osfp != PF_OSFP_ANY)
    386 			printf(" os \"%s\"", pfctl_lookup_fingerprint(osfp, buf,
    387 			    sizeof(buf)));
    388 
    389 		printf(" to ");
    390 		if (dst->neg)
    391 			printf("! ");
    392 		print_addr(&dst->addr, af, verbose);
    393 		if (dst->port_op)
    394 			print_port(dst->port_op, dst->port[0],
    395 			    dst->port[1],
    396 			    proto == IPPROTO_TCP ? "tcp" : "udp");
    397 	}
    398 }
    399 
    400 void
    401 print_pool(struct pf_pool *pool, u_int16_t p1, u_int16_t p2,
    402     sa_family_t af, int id)
    403 {
    404 	struct pf_pooladdr	*pooladdr;
    405 
    406 	if ((TAILQ_FIRST(&pool->list) != NULL) &&
    407 	    TAILQ_NEXT(TAILQ_FIRST(&pool->list), entries) != NULL)
    408 		printf("{ ");
    409 	TAILQ_FOREACH(pooladdr, &pool->list, entries){
    410 		switch (id) {
    411 		case PF_NAT:
    412 		case PF_RDR:
    413 		case PF_BINAT:
    414 			print_addr(&pooladdr->addr, af, 0);
    415 			break;
    416 		case PF_PASS:
    417 			if (PF_AZERO(&pooladdr->addr.v.a.addr, af))
    418 				printf("%s", pooladdr->ifname);
    419 			else {
    420 				printf("(%s ", pooladdr->ifname);
    421 				print_addr(&pooladdr->addr, af, 0);
    422 				printf(")");
    423 			}
    424 			break;
    425 		default:
    426 			break;
    427 		}
    428 		if (TAILQ_NEXT(pooladdr, entries) != NULL)
    429 			printf(", ");
    430 		else if (TAILQ_NEXT(TAILQ_FIRST(&pool->list), entries) != NULL)
    431 			printf(" }");
    432 	}
    433 	switch (id) {
    434 	case PF_NAT:
    435 		if ((p1 != PF_NAT_PROXY_PORT_LOW ||
    436 		    p2 != PF_NAT_PROXY_PORT_HIGH) && (p1 != 0 || p2 != 0)) {
    437 			if (p1 == p2)
    438 				printf(" port %u", p1);
    439 			else
    440 				printf(" port %u:%u", p1, p2);
    441 		}
    442 		break;
    443 	case PF_RDR:
    444 		if (p1) {
    445 			printf(" port %u", p1);
    446 			if (p2 && (p2 != p1))
    447 				printf(":%u", p2);
    448 		}
    449 		break;
    450 	default:
    451 		break;
    452 	}
    453 	switch (pool->opts & PF_POOL_TYPEMASK) {
    454 	case PF_POOL_NONE:
    455 		break;
    456 	case PF_POOL_BITMASK:
    457 		printf(" bitmask");
    458 		break;
    459 	case PF_POOL_RANDOM:
    460 		printf(" random");
    461 		break;
    462 	case PF_POOL_SRCHASH:
    463 		printf(" source-hash 0x%08x%08x%08x%08x",
    464 		    pool->key.key32[0], pool->key.key32[1],
    465 		    pool->key.key32[2], pool->key.key32[3]);
    466 		break;
    467 	case PF_POOL_ROUNDROBIN:
    468 		printf(" round-robin");
    469 		break;
    470 	}
    471 	if (pool->opts & PF_POOL_STICKYADDR)
    472 		printf(" sticky-address");
    473 	if (id == PF_NAT && p1 == 0 && p2 == 0)
    474 		printf(" static-port");
    475 }
    476 
    477 const char	*pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
    478 const char	*pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
    479 const char	*pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
    480 const char	*pf_scounters[FCNT_MAX+1] = FCNT_NAMES;
    481 
    482 void
    483 print_status(struct pf_status *s, int opts)
    484 {
    485 	char			statline[80], *running;
    486 	time_t			runtime;
    487 	int			i;
    488 	char			buf[PF_MD5_DIGEST_LENGTH * 2 + 1];
    489 	static const char	hex[] = "0123456789abcdef";
    490 
    491 	runtime = time(NULL) - s->since;
    492 	running = s->running ? "Enabled" : "Disabled";
    493 
    494 	if (s->since) {
    495 		unsigned	sec, min, hrs, day = runtime;
    496 
    497 		sec = day % 60;
    498 		day /= 60;
    499 		min = day % 60;
    500 		day /= 60;
    501 		hrs = day % 24;
    502 		day /= 24;
    503 		snprintf(statline, sizeof(statline),
    504 		    "Status: %s for %u days %.2u:%.2u:%.2u",
    505 		    running, day, hrs, min, sec);
    506 	} else
    507 		snprintf(statline, sizeof(statline), "Status: %s", running);
    508 	printf("%-44s", statline);
    509 	switch (s->debug) {
    510 	case PF_DEBUG_NONE:
    511 		printf("%15s\n\n", "Debug: None");
    512 		break;
    513 	case PF_DEBUG_URGENT:
    514 		printf("%15s\n\n", "Debug: Urgent");
    515 		break;
    516 	case PF_DEBUG_MISC:
    517 		printf("%15s\n\n", "Debug: Misc");
    518 		break;
    519 	case PF_DEBUG_NOISY:
    520 		printf("%15s\n\n", "Debug: Loud");
    521 		break;
    522 	}
    523 
    524 	if (opts & PF_OPT_VERBOSE) {
    525 		printf("Hostid:   0x%08x\n", ntohl(s->hostid));
    526 
    527 		for (i = 0; i < PF_MD5_DIGEST_LENGTH; i++) {
    528 			buf[i + i] = hex[s->pf_chksum[i] >> 4];
    529 			buf[i + i + 1] = hex[s->pf_chksum[i] & 0x0f];
    530 		}
    531 		buf[i + i] = '\0';
    532 		printf("Checksum: 0x%s\n\n", buf);
    533 	}
    534 
    535 	if (s->ifname[0] != 0) {
    536 		printf("Interface Stats for %-16s %5s %16s\n",
    537 		    s->ifname, "IPv4", "IPv6");
    538 		printf("  %-25s %14llu %16llu\n", "Bytes In",
    539 		    (unsigned long long)s->bcounters[0][0],
    540 		    (unsigned long long)s->bcounters[1][0]);
    541 		printf("  %-25s %14llu %16llu\n", "Bytes Out",
    542 		    (unsigned long long)s->bcounters[0][1],
    543 		    (unsigned long long)s->bcounters[1][1]);
    544 		printf("  Packets In\n");
    545 		printf("    %-23s %14llu %16llu\n", "Passed",
    546 		    (unsigned long long)s->pcounters[0][0][PF_PASS],
    547 		    (unsigned long long)s->pcounters[1][0][PF_PASS]);
    548 		printf("    %-23s %14llu %16llu\n", "Blocked",
    549 		    (unsigned long long)s->pcounters[0][0][PF_DROP],
    550 		    (unsigned long long)s->pcounters[1][0][PF_DROP]);
    551 		printf("  Packets Out\n");
    552 		printf("    %-23s %14llu %16llu\n", "Passed",
    553 		    (unsigned long long)s->pcounters[0][1][PF_PASS],
    554 		    (unsigned long long)s->pcounters[1][1][PF_PASS]);
    555 		printf("    %-23s %14llu %16llu\n\n", "Blocked",
    556 		    (unsigned long long)s->pcounters[0][1][PF_DROP],
    557 		    (unsigned long long)s->pcounters[1][1][PF_DROP]);
    558 	}
    559 	printf("%-27s %14s %16s\n", "State Table", "Total", "Rate");
    560 	printf("  %-25s %14u %14s\n", "current entries", s->states, "");
    561 	for (i = 0; i < FCNT_MAX; i++) {
    562 		printf("  %-25s %14llu ", pf_fcounters[i],
    563 			    (unsigned long long)s->fcounters[i]);
    564 		if (runtime > 0)
    565 			printf("%14.1f/s\n",
    566 			    (double)s->fcounters[i] / (double)runtime);
    567 		else
    568 			printf("%14s\n", "");
    569 	}
    570 	if (opts & PF_OPT_VERBOSE) {
    571 		printf("Source Tracking Table\n");
    572 		printf("  %-25s %14u %14s\n", "current entries",
    573 		    s->src_nodes, "");
    574 		for (i = 0; i < SCNT_MAX; i++) {
    575 			printf("  %-25s %14lld ", pf_scounters[i],
    576 				    s->scounters[i]);
    577 			if (runtime > 0)
    578 				printf("%14.1f/s\n",
    579 				    (double)s->scounters[i] / (double)runtime);
    580 			else
    581 				printf("%14s\n", "");
    582 		}
    583 	}
    584 	printf("Counters\n");
    585 	for (i = 0; i < PFRES_MAX; i++) {
    586 		printf("  %-25s %14llu ", pf_reasons[i],
    587 		    (unsigned long long)s->counters[i]);
    588 		if (runtime > 0)
    589 			printf("%14.1f/s\n",
    590 			    (double)s->counters[i] / (double)runtime);
    591 		else
    592 			printf("%14s\n", "");
    593 	}
    594 	if (opts & PF_OPT_VERBOSE) {
    595 		printf("Limit Counters\n");
    596 		for (i = 0; i < LCNT_MAX; i++) {
    597 			printf("  %-25s %14lld ", pf_lcounters[i],
    598 				    s->lcounters[i]);
    599 			if (runtime > 0)
    600 				printf("%14.1f/s\n",
    601 				    (double)s->lcounters[i] / (double)runtime);
    602 			else
    603 				printf("%14s\n", "");
    604 		}
    605 	}
    606 }
    607 
    608 void
    609 print_src_node(struct pf_src_node *sn, int opts)
    610 {
    611 	struct pf_addr_wrap aw;
    612 	int min, sec;
    613 
    614 	memset(&aw, 0, sizeof(aw));
    615 	if (sn->af == AF_INET)
    616 		aw.v.a.mask.addr32[0] = 0xffffffff;
    617 	else
    618 		memset(&aw.v.a.mask, 0xff, sizeof(aw.v.a.mask));
    619 
    620 	aw.v.a.addr = sn->addr;
    621 	print_addr(&aw, sn->af, opts & PF_OPT_VERBOSE2);
    622 	printf(" -> ");
    623 	aw.v.a.addr = sn->raddr;
    624 	print_addr(&aw, sn->af, opts & PF_OPT_VERBOSE2);
    625 	printf(" ( states %u, connections %u, rate %u.%u/%us )\n", sn->states,
    626 	    sn->conn, sn->conn_rate.count / 1000,
    627 	    (sn->conn_rate.count % 1000) / 100, sn->conn_rate.seconds);
    628 	if (opts & PF_OPT_VERBOSE) {
    629 		sec = sn->creation % 60;
    630 		sn->creation /= 60;
    631 		min = sn->creation % 60;
    632 		sn->creation /= 60;
    633 		printf("   age %.2u:%.2u:%.2u", sn->creation, min, sec);
    634 		if (sn->states == 0) {
    635 			sec = sn->expire % 60;
    636 			sn->expire /= 60;
    637 			min = sn->expire % 60;
    638 			sn->expire /= 60;
    639 			printf(", expires in %.2u:%.2u:%.2u",
    640 			    sn->expire, min, sec);
    641 		}
    642 		printf(", %llu pkts, %llu bytes",
    643 		    sn->packets[0] + sn->packets[1],
    644 		    sn->bytes[0] + sn->bytes[1]);
    645 		switch (sn->ruletype) {
    646 		case PF_NAT:
    647 			if (sn->rule.nr != -1)
    648 				printf(", nat rule %u", sn->rule.nr);
    649 			break;
    650 		case PF_RDR:
    651 			if (sn->rule.nr != -1)
    652 				printf(", rdr rule %u", sn->rule.nr);
    653 			break;
    654 		case PF_PASS:
    655 			if (sn->rule.nr != -1)
    656 				printf(", filter rule %u", sn->rule.nr);
    657 			break;
    658 		}
    659 		printf("\n");
    660 	}
    661 }
    662 
    663 void
    664 print_rule(struct pf_rule *r, const char *anchor_call, int verbose)
    665 {
    666 	static const char *actiontypes[] = { "pass", "block", "scrub",
    667 	    "no scrub", "nat", "no nat", "binat", "no binat", "rdr", "no rdr" };
    668 	static const char *anchortypes[] = { "anchor", "anchor", "anchor",
    669 	    "anchor", "nat-anchor", "nat-anchor", "binat-anchor",
    670 	    "binat-anchor", "rdr-anchor", "rdr-anchor" };
    671 	int	i, opts;
    672 
    673 	if (verbose)
    674 		printf("@%d ", r->nr);
    675 	if (r->action > PF_NORDR)
    676 		printf("action(%d)", r->action);
    677 	else if (anchor_call[0]) {
    678 		if (anchor_call[0] == '_') {
    679 			printf("%s", anchortypes[r->action]);
    680 		} else
    681 			printf("%s \"%s\"", anchortypes[r->action],
    682 			    anchor_call);
    683 	} else {
    684 		printf("%s", actiontypes[r->action]);
    685 		if (r->natpass)
    686 			printf(" pass");
    687 	}
    688 	if (r->action == PF_DROP) {
    689 		if (r->rule_flag & PFRULE_RETURN)
    690 			printf(" return");
    691 		else if (r->rule_flag & PFRULE_RETURNRST) {
    692 			if (!r->return_ttl)
    693 				printf(" return-rst");
    694 			else
    695 				printf(" return-rst(ttl %d)", r->return_ttl);
    696 		} else if (r->rule_flag & PFRULE_RETURNICMP) {
    697 			const struct icmpcodeent	*ic, *ic6;
    698 
    699 			ic = geticmpcodebynumber(r->return_icmp >> 8,
    700 			    r->return_icmp & 255, AF_INET);
    701 			ic6 = geticmpcodebynumber(r->return_icmp6 >> 8,
    702 			    r->return_icmp6 & 255, AF_INET6);
    703 
    704 			switch (r->af) {
    705 			case AF_INET:
    706 				printf(" return-icmp");
    707 				if (ic == NULL)
    708 					printf("(%u)", r->return_icmp & 255);
    709 				else
    710 					printf("(%s)", ic->name);
    711 				break;
    712 			case AF_INET6:
    713 				printf(" return-icmp6");
    714 				if (ic6 == NULL)
    715 					printf("(%u)", r->return_icmp6 & 255);
    716 				else
    717 					printf("(%s)", ic6->name);
    718 				break;
    719 			default:
    720 				printf(" return-icmp");
    721 				if (ic == NULL)
    722 					printf("(%u, ", r->return_icmp & 255);
    723 				else
    724 					printf("(%s, ", ic->name);
    725 				if (ic6 == NULL)
    726 					printf("%u)", r->return_icmp6 & 255);
    727 				else
    728 					printf("%s)", ic6->name);
    729 				break;
    730 			}
    731 		} else
    732 			printf(" drop");
    733 	}
    734 	if (r->direction == PF_IN)
    735 		printf(" in");
    736 	else if (r->direction == PF_OUT)
    737 		printf(" out");
    738 	if (r->log) {
    739 		printf(" log");
    740 		if (r->log & ~PF_LOG || r->logif) {
    741 			int count = 0;
    742 
    743 			printf(" (");
    744 			if (r->log & PF_LOG_ALL)
    745 				printf("%sall", count++ ? ", " : "");
    746 			if (r->log & PF_LOG_SOCKET_LOOKUP)
    747 				printf("%suser", count++ ? ", " : "");
    748 			if (r->logif)
    749 				printf("%sto pflog%u", count++ ? ", " : "",
    750 				    r->logif);
    751 			printf(")");
    752 		}
    753 	}
    754 	if (r->quick)
    755 		printf(" quick");
    756 	if (r->ifname[0]) {
    757 		if (r->ifnot)
    758 			printf(" on ! %s", r->ifname);
    759 		else
    760 			printf(" on %s", r->ifname);
    761 	}
    762 	if (r->rt) {
    763 		if (r->rt == PF_ROUTETO)
    764 			printf(" route-to");
    765 		else if (r->rt == PF_REPLYTO)
    766 			printf(" reply-to");
    767 		else if (r->rt == PF_DUPTO)
    768 			printf(" dup-to");
    769 		else if (r->rt == PF_FASTROUTE)
    770 			printf(" fastroute");
    771 		if (r->rt != PF_FASTROUTE) {
    772 			printf(" ");
    773 			print_pool(&r->rpool, 0, 0, r->af, PF_PASS);
    774 		}
    775 	}
    776 	if (r->af) {
    777 		if (r->af == AF_INET)
    778 			printf(" inet");
    779 		else
    780 			printf(" inet6");
    781 	}
    782 	if (r->proto) {
    783 		struct protoent	*p;
    784 
    785 		if ((p = getprotobynumber(r->proto)) != NULL)
    786 			printf(" proto %s", p->p_name);
    787 		else
    788 			printf(" proto %u", r->proto);
    789 	}
    790 	print_fromto(&r->src, r->os_fingerprint, &r->dst, r->af, r->proto,
    791 	    verbose);
    792 	if (r->uid.op)
    793 		print_ugid(r->uid.op, r->uid.uid[0], r->uid.uid[1], "user",
    794 		    UID_MAX);
    795 	if (r->gid.op)
    796 		print_ugid(r->gid.op, r->gid.gid[0], r->gid.gid[1], "group",
    797 		    GID_MAX);
    798 	if (r->flags || r->flagset) {
    799 		printf(" flags ");
    800 		print_flags(r->flags);
    801 		printf("/");
    802 		print_flags(r->flagset);
    803 	} else if (r->action == PF_PASS &&
    804 	    (!r->proto || r->proto == IPPROTO_TCP) &&
    805 	    !(r->rule_flag & PFRULE_FRAGMENT) &&
    806 	    !anchor_call[0] && r->keep_state)
    807 		printf(" flags any");
    808 	if (r->type) {
    809 		const struct icmptypeent	*it;
    810 
    811 		it = geticmptypebynumber(r->type-1, r->af);
    812 		if (r->af != AF_INET6)
    813 			printf(" icmp-type");
    814 		else
    815 			printf(" icmp6-type");
    816 		if (it != NULL)
    817 			printf(" %s", it->name);
    818 		else
    819 			printf(" %u", r->type-1);
    820 		if (r->code) {
    821 			const struct icmpcodeent	*ic;
    822 
    823 			ic = geticmpcodebynumber(r->type-1, r->code-1, r->af);
    824 			if (ic != NULL)
    825 				printf(" code %s", ic->name);
    826 			else
    827 				printf(" code %u", r->code-1);
    828 		}
    829 	}
    830 	if (r->tos)
    831 		printf(" tos 0x%2.2x", r->tos);
    832 	if (!r->keep_state && r->action == PF_PASS && !anchor_call[0])
    833 		printf(" no state");
    834 	else if (r->keep_state == PF_STATE_NORMAL)
    835 		printf(" keep state");
    836 	else if (r->keep_state == PF_STATE_MODULATE)
    837 		printf(" modulate state");
    838 	else if (r->keep_state == PF_STATE_SYNPROXY)
    839 		printf(" synproxy state");
    840 	if (r->prob) {
    841 		char	buf[20];
    842 
    843 		snprintf(buf, sizeof(buf), "%f", r->prob*100.0/(UINT_MAX+1.0));
    844 		for (i = strlen(buf)-1; i > 0; i--) {
    845 			if (buf[i] == '0')
    846 				buf[i] = '\0';
    847 			else {
    848 				if (buf[i] == '.')
    849 					buf[i] = '\0';
    850 				break;
    851 			}
    852 		}
    853 		printf(" probability %s%%", buf);
    854 	}
    855 	opts = 0;
    856 	if (r->max_states || r->max_src_nodes || r->max_src_states)
    857 		opts = 1;
    858 	if (r->rule_flag & PFRULE_NOSYNC)
    859 		opts = 1;
    860 	if (r->rule_flag & PFRULE_SRCTRACK)
    861 		opts = 1;
    862 	if (r->rule_flag & PFRULE_IFBOUND)
    863 		opts = 1;
    864 	for (i = 0; !opts && i < PFTM_MAX; ++i)
    865 		if (r->timeout[i])
    866 			opts = 1;
    867 	if (opts) {
    868 		printf(" (");
    869 		if (r->max_states) {
    870 			printf("max %u", r->max_states);
    871 			opts = 0;
    872 		}
    873 		if (r->rule_flag & PFRULE_NOSYNC) {
    874 			if (!opts)
    875 				printf(", ");
    876 			printf("no-sync");
    877 			opts = 0;
    878 		}
    879 		if (r->rule_flag & PFRULE_SRCTRACK) {
    880 			if (!opts)
    881 				printf(", ");
    882 			printf("source-track");
    883 			if (r->rule_flag & PFRULE_RULESRCTRACK)
    884 				printf(" rule");
    885 			else
    886 				printf(" global");
    887 			opts = 0;
    888 		}
    889 		if (r->max_src_states) {
    890 			if (!opts)
    891 				printf(", ");
    892 			printf("max-src-states %u", r->max_src_states);
    893 			opts = 0;
    894 		}
    895 		if (r->max_src_conn) {
    896 			if (!opts)
    897 				printf(", ");
    898 			printf("max-src-conn %u", r->max_src_conn);
    899 			opts = 0;
    900 		}
    901 		if (r->max_src_conn_rate.limit) {
    902 			if (!opts)
    903 				printf(", ");
    904 			printf("max-src-conn-rate %u/%u",
    905 			    r->max_src_conn_rate.limit,
    906 			    r->max_src_conn_rate.seconds);
    907 			opts = 0;
    908 		}
    909 		if (r->max_src_nodes) {
    910 			if (!opts)
    911 				printf(", ");
    912 			printf("max-src-nodes %u", r->max_src_nodes);
    913 			opts = 0;
    914 		}
    915 		if (r->overload_tblname[0]) {
    916 			if (!opts)
    917 				printf(", ");
    918 			printf("overload <%s>", r->overload_tblname);
    919 			if (r->flush)
    920 				printf(" flush");
    921 			if (r->flush & PF_FLUSH_GLOBAL)
    922 				printf(" global");
    923 		}
    924 		if (r->rule_flag & PFRULE_IFBOUND) {
    925 			if (!opts)
    926 				printf(", ");
    927 			printf("if-bound");
    928 			opts = 0;
    929 		}
    930 		for (i = 0; i < PFTM_MAX; ++i)
    931 			if (r->timeout[i]) {
    932 				int j;
    933 
    934 				if (!opts)
    935 					printf(", ");
    936 				opts = 0;
    937 				for (j = 0; pf_timeouts[j].name != NULL;
    938 				    ++j)
    939 					if (pf_timeouts[j].timeout == i)
    940 						break;
    941 				printf("%s %u", pf_timeouts[j].name == NULL ?
    942 				    "inv.timeout" : pf_timeouts[j].name,
    943 				    r->timeout[i]);
    944 			}
    945 		printf(")");
    946 	}
    947 	if (r->rule_flag & PFRULE_FRAGMENT)
    948 		printf(" fragment");
    949 	if (r->rule_flag & PFRULE_NODF)
    950 		printf(" no-df");
    951 	if (r->rule_flag & PFRULE_RANDOMID)
    952 		printf(" random-id");
    953 	if (r->min_ttl)
    954 		printf(" min-ttl %d", r->min_ttl);
    955 	if (r->max_mss)
    956 		printf(" max-mss %d", r->max_mss);
    957 	if (r->allow_opts)
    958 		printf(" allow-opts");
    959 	if (r->action == PF_SCRUB) {
    960 		if (r->rule_flag & PFRULE_REASSEMBLE_TCP)
    961 			printf(" reassemble tcp");
    962 
    963 		if (r->rule_flag & PFRULE_FRAGDROP)
    964 			printf(" fragment drop-ovl");
    965 		else if (r->rule_flag & PFRULE_FRAGCROP)
    966 			printf(" fragment crop");
    967 		else
    968 			printf(" fragment reassemble");
    969 	}
    970 	if (r->label[0])
    971 		printf(" label \"%s\"", r->label);
    972 	if (r->qname[0] && r->pqname[0])
    973 		printf(" queue(%s, %s)", r->qname, r->pqname);
    974 	else if (r->qname[0])
    975 		printf(" queue %s", r->qname);
    976 	if (r->tagname[0])
    977 		printf(" tag %s", r->tagname);
    978 	if (r->match_tagname[0]) {
    979 		if (r->match_tag_not)
    980 			printf(" !");
    981 		printf(" tagged %s", r->match_tagname);
    982 	}
    983 	if (r->rtableid != -1)
    984 		printf(" rtable %u", r->rtableid);
    985 	if (!anchor_call[0] && (r->action == PF_NAT ||
    986 	    r->action == PF_BINAT || r->action == PF_RDR)) {
    987 		printf(" -> ");
    988 		print_pool(&r->rpool, r->rpool.proxy_port[0],
    989 		    r->rpool.proxy_port[1], r->af, r->action);
    990 	}
    991 }
    992 
    993 void
    994 print_tabledef(const char *name, int flags, int addrs,
    995     struct node_tinithead *nodes)
    996 {
    997 	struct node_tinit	*ti, *nti;
    998 	struct node_host	*h;
    999 
   1000 	printf("table <%s>", name);
   1001 	if (flags & PFR_TFLAG_CONST)
   1002 		printf(" const");
   1003 	if (flags & PFR_TFLAG_PERSIST)
   1004 		printf(" persist");
   1005 	SIMPLEQ_FOREACH(ti, nodes, entries) {
   1006 		if (ti->file) {
   1007 			printf(" file \"%s\"", ti->file);
   1008 			continue;
   1009 		}
   1010 		printf(" {");
   1011 		for (;;) {
   1012 			for (h = ti->host; h != NULL; h = h->next) {
   1013 				printf(h->not ? " !" : " ");
   1014 				print_addr(&h->addr, h->af, 0);
   1015 			}
   1016 			nti = SIMPLEQ_NEXT(ti, entries);
   1017 			if (nti != NULL && nti->file == NULL)
   1018 				ti = nti;	/* merge lists */
   1019 			else
   1020 				break;
   1021 		}
   1022 		printf(" }");
   1023 	}
   1024 	if (addrs && SIMPLEQ_EMPTY(nodes))
   1025 		printf(" { }");
   1026 	printf("\n");
   1027 }
   1028 
   1029 int
   1030 parse_flags(char *s)
   1031 {
   1032 	char		*p, *q;
   1033 	u_int8_t	 f = 0;
   1034 
   1035 	for (p = s; *p; p++) {
   1036 		if ((q = strchr(tcpflags, *p)) == NULL)
   1037 			return -1;
   1038 		else
   1039 			f |= 1 << (q - tcpflags);
   1040 	}
   1041 	return (f ? f : PF_TH_ALL);
   1042 }
   1043 
   1044 void
   1045 set_ipmask(struct node_host *h, u_int8_t b)
   1046 {
   1047 	struct pf_addr	*m, *n;
   1048 	int		 i, j = 0;
   1049 
   1050 	m = &h->addr.v.a.mask;
   1051 	memset(m, 0, sizeof(*m));
   1052 
   1053 	while (b >= 32) {
   1054 		m->addr32[j++] = 0xffffffff;
   1055 		b -= 32;
   1056 	}
   1057 	for (i = 31; i > 31-b; --i)
   1058 		m->addr32[j] |= (1 << i);
   1059 	if (b)
   1060 		m->addr32[j] = htonl(m->addr32[j]);
   1061 
   1062 	/* Mask off bits of the address that will never be used. */
   1063 	n = &h->addr.v.a.addr;
   1064 	if (h->addr.type == PF_ADDR_ADDRMASK)
   1065 		for (i = 0; i < 4; i++)
   1066 			n->addr32[i] = n->addr32[i] & m->addr32[i];
   1067 }
   1068 
   1069 int
   1070 check_netmask(struct node_host *h, sa_family_t af)
   1071 {
   1072 	struct node_host	*n = NULL;
   1073 	struct pf_addr	*m;
   1074 
   1075 	for (n = h; n != NULL; n = n->next) {
   1076 		if (h->addr.type == PF_ADDR_TABLE)
   1077 			continue;
   1078 		m = &h->addr.v.a.mask;
   1079 		/* fix up netmask for dynaddr */
   1080 		if (af == AF_INET && h->addr.type == PF_ADDR_DYNIFTL &&
   1081 		    unmask(m, AF_INET6) > 32)
   1082 			set_ipmask(n, 32);
   1083 		/* netmasks > 32 bit are invalid on v4 */
   1084 		if (af == AF_INET &&
   1085 		    (m->addr32[1] || m->addr32[2] || m->addr32[3])) {
   1086 			fprintf(stderr, "netmask %u invalid for IPv4 address\n",
   1087 			    unmask(m, AF_INET6));
   1088 			return (1);
   1089 		}
   1090 	}
   1091 	return (0);
   1092 }
   1093 
   1094 /* interface lookup routines */
   1095 
   1096 struct node_host	*iftab;
   1097 
   1098 void
   1099 ifa_load(void)
   1100 {
   1101 	struct ifaddrs		*ifap, *ifa;
   1102 	struct node_host	*n = NULL, *h = NULL;
   1103 
   1104 	if (getifaddrs(&ifap) < 0)
   1105 		err(1, "getifaddrs");
   1106 
   1107 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
   1108 		if (!(ifa->ifa_addr->sa_family == AF_INET ||
   1109 		    ifa->ifa_addr->sa_family == AF_INET6 ||
   1110 		    ifa->ifa_addr->sa_family == AF_LINK))
   1111 				continue;
   1112 		n = calloc(1, sizeof(struct node_host));
   1113 		if (n == NULL)
   1114 			err(1, "address: calloc");
   1115 		n->af = ifa->ifa_addr->sa_family;
   1116 		n->ifa_flags = ifa->ifa_flags;
   1117 #ifdef __KAME__
   1118 		if (n->af == AF_INET6 &&
   1119 		    IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
   1120 		    ifa->ifa_addr)->sin6_addr) &&
   1121 		    ((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_scope_id ==
   1122 		    0) {
   1123 			struct sockaddr_in6	*sin6;
   1124 
   1125 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
   1126 			sin6->sin6_scope_id = sin6->sin6_addr.s6_addr[2] << 8 |
   1127 			    sin6->sin6_addr.s6_addr[3];
   1128 			sin6->sin6_addr.s6_addr[2] = 0;
   1129 			sin6->sin6_addr.s6_addr[3] = 0;
   1130 		}
   1131 #endif
   1132 		n->ifindex = 0;
   1133 		if (n->af == AF_INET) {
   1134 			memcpy(&n->addr.v.a.addr, &((struct sockaddr_in *)
   1135 			    ifa->ifa_addr)->sin_addr.s_addr,
   1136 			    sizeof(struct in_addr));
   1137 			memcpy(&n->addr.v.a.mask, &((struct sockaddr_in *)
   1138 			    ifa->ifa_netmask)->sin_addr.s_addr,
   1139 			    sizeof(struct in_addr));
   1140 			if (ifa->ifa_broadaddr != NULL)
   1141 				memcpy(&n->bcast, &((struct sockaddr_in *)
   1142 				    ifa->ifa_broadaddr)->sin_addr.s_addr,
   1143 				    sizeof(struct in_addr));
   1144 			if (ifa->ifa_dstaddr != NULL)
   1145 				memcpy(&n->peer, &((struct sockaddr_in *)
   1146 				    ifa->ifa_dstaddr)->sin_addr.s_addr,
   1147 				    sizeof(struct in_addr));
   1148 		} else if (n->af == AF_INET6) {
   1149 			memcpy(&n->addr.v.a.addr, &((struct sockaddr_in6 *)
   1150 			    ifa->ifa_addr)->sin6_addr.s6_addr,
   1151 			    sizeof(struct in6_addr));
   1152 			memcpy(&n->addr.v.a.mask, &((struct sockaddr_in6 *)
   1153 			    ifa->ifa_netmask)->sin6_addr.s6_addr,
   1154 			    sizeof(struct in6_addr));
   1155 			if (ifa->ifa_broadaddr != NULL)
   1156 				memcpy(&n->bcast, &((struct sockaddr_in6 *)
   1157 				    ifa->ifa_broadaddr)->sin6_addr.s6_addr,
   1158 				    sizeof(struct in6_addr));
   1159 			if (ifa->ifa_dstaddr != NULL)
   1160 				 memcpy(&n->peer, &((struct sockaddr_in6 *)
   1161 				    ifa->ifa_dstaddr)->sin6_addr.s6_addr,
   1162 				    sizeof(struct in6_addr));
   1163 			n->ifindex = ((struct sockaddr_in6 *)
   1164 			    ifa->ifa_addr)->sin6_scope_id;
   1165 		}
   1166 		if ((n->ifname = strdup(ifa->ifa_name)) == NULL)
   1167 			err(1, "ifa_load: strdup");
   1168 		n->next = NULL;
   1169 		n->tail = n;
   1170 		if (h == NULL)
   1171 			h = n;
   1172 		else {
   1173 			h->tail->next = n;
   1174 			h->tail = n;
   1175 		}
   1176 	}
   1177 
   1178 	iftab = h;
   1179 	freeifaddrs(ifap);
   1180 }
   1181 
   1182 struct node_host *
   1183 ifa_exists(const char *ifa_name)
   1184 {
   1185 	struct node_host	*n;
   1186 	struct ifgroupreq	ifgr;
   1187 	int			s;
   1188 
   1189 	if (iftab == NULL)
   1190 		ifa_load();
   1191 
   1192 	/* check wether this is a group */
   1193 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
   1194 		err(1, "socket");
   1195 	bzero(&ifgr, sizeof(ifgr));
   1196 	strlcpy(ifgr.ifgr_name, ifa_name, sizeof(ifgr.ifgr_name));
   1197 	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == 0) {
   1198 		/* fake a node_host */
   1199 		if ((n = calloc(1, sizeof(*n))) == NULL)
   1200 			err(1, "calloc");
   1201 		if ((n->ifname = strdup(ifa_name)) == NULL)
   1202 			err(1, "strdup");
   1203 		close(s);
   1204 		return (n);
   1205 	}
   1206 	close(s);
   1207 
   1208 	for (n = iftab; n; n = n->next) {
   1209 		if (n->af == AF_LINK && !strncmp(n->ifname, ifa_name, IFNAMSIZ))
   1210 			return (n);
   1211 	}
   1212 
   1213 	return (NULL);
   1214 }
   1215 
   1216 struct node_host *
   1217 ifa_grouplookup(const char *ifa_name, int flags)
   1218 {
   1219 	struct ifg_req		*ifg;
   1220 	struct ifgroupreq	 ifgr;
   1221 	int			 s, len;
   1222 	struct node_host	*n, *h = NULL;
   1223 
   1224 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
   1225 		err(1, "socket");
   1226 	bzero(&ifgr, sizeof(ifgr));
   1227 	strlcpy(ifgr.ifgr_name, ifa_name, sizeof(ifgr.ifgr_name));
   1228 	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
   1229 		close(s);
   1230 		return (NULL);
   1231 	}
   1232 
   1233 	len = ifgr.ifgr_len;
   1234 	if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
   1235 		err(1, "calloc");
   1236 	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
   1237 		err(1, "SIOCGIFGMEMB");
   1238 
   1239 	for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
   1240 	    ifg++) {
   1241 		len -= sizeof(struct ifg_req);
   1242 		if ((n = ifa_lookup(ifg->ifgrq_member, flags)) == NULL)
   1243 			continue;
   1244 		if (h == NULL)
   1245 			h = n;
   1246 		else {
   1247 			h->tail->next = n;
   1248 			h->tail = n->tail;
   1249 		}
   1250 	}
   1251 	free(ifgr.ifgr_groups);
   1252 	close(s);
   1253 
   1254 	return (h);
   1255 }
   1256 
   1257 struct node_host *
   1258 ifa_lookup(const char *ifa_name, int flags)
   1259 {
   1260 	struct node_host	*p = NULL, *h = NULL, *n = NULL;
   1261 	int			 got4 = 0, got6 = 0;
   1262 	const char		 *last_if = NULL;
   1263 
   1264 	if ((h = ifa_grouplookup(ifa_name, flags)) != NULL)
   1265 		return (h);
   1266 
   1267 	if (!strncmp(ifa_name, "self", IFNAMSIZ))
   1268 		ifa_name = NULL;
   1269 
   1270 	if (iftab == NULL)
   1271 		ifa_load();
   1272 
   1273 	for (p = iftab; p; p = p->next) {
   1274 		if (ifa_skip_if(ifa_name, p))
   1275 			continue;
   1276 		if ((flags & PFI_AFLAG_BROADCAST) && p->af != AF_INET)
   1277 			continue;
   1278 		if ((flags & PFI_AFLAG_BROADCAST) &&
   1279 		    !(p->ifa_flags & IFF_BROADCAST))
   1280 			continue;
   1281 		if ((flags & PFI_AFLAG_PEER) &&
   1282 		    !(p->ifa_flags & IFF_POINTOPOINT))
   1283 			continue;
   1284 		if ((flags & PFI_AFLAG_NETWORK) && p->ifindex > 0)
   1285 			continue;
   1286 		if (last_if == NULL || strcmp(last_if, p->ifname))
   1287 			got4 = got6 = 0;
   1288 		last_if = p->ifname;
   1289 		if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET && got4)
   1290 			continue;
   1291 		if ((flags & PFI_AFLAG_NOALIAS) && p->af == AF_INET6 && got6)
   1292 			continue;
   1293 		if (p->af == AF_INET)
   1294 			got4 = 1;
   1295 		else
   1296 			got6 = 1;
   1297 		n = calloc(1, sizeof(struct node_host));
   1298 		if (n == NULL)
   1299 			err(1, "address: calloc");
   1300 		n->af = p->af;
   1301 		if (flags & PFI_AFLAG_BROADCAST)
   1302 			memcpy(&n->addr.v.a.addr, &p->bcast,
   1303 			    sizeof(struct pf_addr));
   1304 		else if (flags & PFI_AFLAG_PEER)
   1305 			memcpy(&n->addr.v.a.addr, &p->peer,
   1306 			    sizeof(struct pf_addr));
   1307 		else
   1308 			memcpy(&n->addr.v.a.addr, &p->addr.v.a.addr,
   1309 			    sizeof(struct pf_addr));
   1310 		if (flags & PFI_AFLAG_NETWORK)
   1311 			set_ipmask(n, unmask(&p->addr.v.a.mask, n->af));
   1312 		else {
   1313 			if (n->af == AF_INET) {
   1314 				if (p->ifa_flags & IFF_LOOPBACK &&
   1315 				    p->ifa_flags & IFF_LINK1)
   1316 					memcpy(&n->addr.v.a.mask,
   1317 					    &p->addr.v.a.mask,
   1318 					    sizeof(struct pf_addr));
   1319 				else
   1320 					set_ipmask(n, 32);
   1321 			} else
   1322 				set_ipmask(n, 128);
   1323 		}
   1324 		n->ifindex = p->ifindex;
   1325 
   1326 		n->next = NULL;
   1327 		n->tail = n;
   1328 		if (h == NULL)
   1329 			h = n;
   1330 		else {
   1331 			h->tail->next = n;
   1332 			h->tail = n;
   1333 		}
   1334 	}
   1335 	return (h);
   1336 }
   1337 
   1338 int
   1339 ifa_skip_if(const char *filter, struct node_host *p)
   1340 {
   1341 	int	n;
   1342 
   1343 	if (p->af != AF_INET && p->af != AF_INET6)
   1344 		return (1);
   1345 	if (filter == NULL || !*filter)
   1346 		return (0);
   1347 	if (!strcmp(p->ifname, filter))
   1348 		return (0);	/* exact match */
   1349 	n = strlen(filter);
   1350 	if (n < 1 || n >= IFNAMSIZ)
   1351 		return (1);	/* sanity check */
   1352 	if (filter[n-1] >= '0' && filter[n-1] <= '9')
   1353 		return (1);	/* only do exact match in that case */
   1354 	if (strncmp(p->ifname, filter, n))
   1355 		return (1);	/* prefix doesn't match */
   1356 	return (p->ifname[n] < '0' || p->ifname[n] > '9');
   1357 }
   1358 
   1359 
   1360 struct node_host *
   1361 host(const char *s)
   1362 {
   1363 	struct node_host	*h = NULL;
   1364 	int			 mask, v4mask, v6mask, cont = 1;
   1365 	char			*p, *q, *ps;
   1366 
   1367 	if ((p = strrchr(s, '/')) != NULL) {
   1368 		mask = strtol(p+1, &q, 0);
   1369 		if (!q || *q || mask > 128 || q == (p+1)) {
   1370 			fprintf(stderr, "invalid netmask '%s'\n", p);
   1371 			return (NULL);
   1372 		}
   1373 		if ((ps = malloc(strlen(s) - strlen(p) + 1)) == NULL)
   1374 			err(1, "host: malloc");
   1375 		strlcpy(ps, s, strlen(s) - strlen(p) + 1);
   1376 		v4mask = v6mask = mask;
   1377 	} else {
   1378 		if ((ps = strdup(s)) == NULL)
   1379 			err(1, "host: strdup");
   1380 		v4mask = 32;
   1381 		v6mask = 128;
   1382 		mask = -1;
   1383 	}
   1384 
   1385 	/* interface with this name exists? */
   1386 	if (cont && (h = host_if(ps, mask)) != NULL)
   1387 		cont = 0;
   1388 
   1389 	/* IPv4 address? */
   1390 	if (cont && (h = host_v4(s, mask)) != NULL)
   1391 		cont = 0;
   1392 
   1393 	/* IPv6 address? */
   1394 	if (cont && (h = host_v6(ps, v6mask)) != NULL)
   1395 		cont = 0;
   1396 
   1397 	/* dns lookup */
   1398 	if (cont && (h = host_dns(ps, v4mask, v6mask)) != NULL)
   1399 		cont = 0;
   1400 	free(ps);
   1401 
   1402 	if (h == NULL || cont == 1) {
   1403 		fprintf(stderr, "no IP address found for %s\n", s);
   1404 		return (NULL);
   1405 	}
   1406 	return (h);
   1407 }
   1408 
   1409 struct node_host *
   1410 host_if(const char *s, int mask)
   1411 {
   1412 	struct node_host	*n, *h = NULL;
   1413 	char			*p, *ps;
   1414 	int			 flags = 0;
   1415 
   1416 	if ((ps = strdup(s)) == NULL)
   1417 		err(1, "host_if: strdup");
   1418 	while ((p = strrchr(ps, ':')) != NULL) {
   1419 		if (!strcmp(p+1, "network"))
   1420 			flags |= PFI_AFLAG_NETWORK;
   1421 		else if (!strcmp(p+1, "broadcast"))
   1422 			flags |= PFI_AFLAG_BROADCAST;
   1423 		else if (!strcmp(p+1, "peer"))
   1424 			flags |= PFI_AFLAG_PEER;
   1425 		else if (!strcmp(p+1, "0"))
   1426 			flags |= PFI_AFLAG_NOALIAS;
   1427 		else {
   1428 			free(ps);
   1429 			return (NULL);
   1430 		}
   1431 		*p = '\0';
   1432 	}
   1433 	if (flags & (flags - 1) & PFI_AFLAG_MODEMASK) { /* Yep! */
   1434 		fprintf(stderr, "illegal combination of interface modifiers\n");
   1435 		free(ps);
   1436 		return (NULL);
   1437 	}
   1438 	if ((flags & (PFI_AFLAG_NETWORK|PFI_AFLAG_BROADCAST)) && mask > -1) {
   1439 		fprintf(stderr, "network or broadcast lookup, but "
   1440 		    "extra netmask given\n");
   1441 		free(ps);
   1442 		return (NULL);
   1443 	}
   1444 	if (ifa_exists(ps) || !strncmp(ps, "self", IFNAMSIZ)) {
   1445 		/* interface with this name exists */
   1446 		h = ifa_lookup(ps, flags);
   1447 		for (n = h; n != NULL && mask > -1; n = n->next)
   1448 			set_ipmask(n, mask);
   1449 	}
   1450 
   1451 	free(ps);
   1452 	return (h);
   1453 }
   1454 
   1455 struct node_host *
   1456 host_v4(const char *s, int mask)
   1457 {
   1458 	struct node_host	*h = NULL;
   1459 	struct in_addr		 ina;
   1460 	int			 bits = 32;
   1461 
   1462 	memset(&ina, 0, sizeof(struct in_addr));
   1463 	if (strrchr(s, '/') != NULL) {
   1464 		if ((bits = inet_net_pton(AF_INET, s, &ina, sizeof(ina))) == -1)
   1465 			return (NULL);
   1466 	} else {
   1467 		if (inet_pton(AF_INET, s, &ina) != 1)
   1468 			return (NULL);
   1469 	}
   1470 
   1471 	h = calloc(1, sizeof(struct node_host));
   1472 	if (h == NULL)
   1473 		err(1, "address: calloc");
   1474 	h->ifname = NULL;
   1475 	h->af = AF_INET;
   1476 	h->addr.v.a.addr.addr32[0] = ina.s_addr;
   1477 	set_ipmask(h, bits);
   1478 	h->next = NULL;
   1479 	h->tail = h;
   1480 
   1481 	return (h);
   1482 }
   1483 
   1484 struct node_host *
   1485 host_v6(const char *s, int mask)
   1486 {
   1487 	struct addrinfo		 hints, *res;
   1488 	struct node_host	*h = NULL;
   1489 
   1490 	memset(&hints, 0, sizeof(hints));
   1491 	hints.ai_family = AF_INET6;
   1492 	hints.ai_socktype = SOCK_DGRAM; /*dummy*/
   1493 	hints.ai_flags = AI_NUMERICHOST;
   1494 	if (getaddrinfo(s, "0", &hints, &res) == 0) {
   1495 		h = calloc(1, sizeof(struct node_host));
   1496 		if (h == NULL)
   1497 			err(1, "address: calloc");
   1498 		h->ifname = NULL;
   1499 		h->af = AF_INET6;
   1500 		memcpy(&h->addr.v.a.addr,
   1501 		    &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr,
   1502 		    sizeof(h->addr.v.a.addr));
   1503 		h->ifindex =
   1504 		    ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id;
   1505 		set_ipmask(h, mask);
   1506 		freeaddrinfo(res);
   1507 		h->next = NULL;
   1508 		h->tail = h;
   1509 	}
   1510 
   1511 	return (h);
   1512 }
   1513 
   1514 struct node_host *
   1515 host_dns(const char *s, int v4mask, int v6mask)
   1516 {
   1517 	struct addrinfo		 hints, *res0, *res;
   1518 	struct node_host	*n, *h = NULL;
   1519 	int			 error, noalias = 0;
   1520 	int			 got4 = 0, got6 = 0;
   1521 	char			*p, *ps;
   1522 
   1523 	if ((ps = strdup(s)) == NULL)
   1524 		err(1, "host_dns: strdup");
   1525 	if ((p = strrchr(ps, ':')) != NULL && !strcmp(p, ":0")) {
   1526 		noalias = 1;
   1527 		*p = '\0';
   1528 	}
   1529 	memset(&hints, 0, sizeof(hints));
   1530 	hints.ai_family = PF_UNSPEC;
   1531 	hints.ai_socktype = SOCK_STREAM; /* DUMMY */
   1532 	error = getaddrinfo(ps, NULL, &hints, &res0);
   1533 	if (error) {
   1534 		free(ps);
   1535 		return (h);
   1536 	}
   1537 
   1538 	for (res = res0; res; res = res->ai_next) {
   1539 		if (res->ai_family != AF_INET &&
   1540 		    res->ai_family != AF_INET6)
   1541 			continue;
   1542 		if (noalias) {
   1543 			if (res->ai_family == AF_INET) {
   1544 				if (got4)
   1545 					continue;
   1546 				got4 = 1;
   1547 			} else {
   1548 				if (got6)
   1549 					continue;
   1550 				got6 = 1;
   1551 			}
   1552 		}
   1553 		n = calloc(1, sizeof(struct node_host));
   1554 		if (n == NULL)
   1555 			err(1, "host_dns: calloc");
   1556 		n->ifname = NULL;
   1557 		n->af = res->ai_family;
   1558 		if (res->ai_family == AF_INET) {
   1559 			memcpy(&n->addr.v.a.addr,
   1560 			    &((struct sockaddr_in *)
   1561 			    res->ai_addr)->sin_addr.s_addr,
   1562 			    sizeof(struct in_addr));
   1563 			set_ipmask(n, v4mask);
   1564 		} else {
   1565 			memcpy(&n->addr.v.a.addr,
   1566 			    &((struct sockaddr_in6 *)
   1567 			    res->ai_addr)->sin6_addr.s6_addr,
   1568 			    sizeof(struct in6_addr));
   1569 			n->ifindex =
   1570 			    ((struct sockaddr_in6 *)
   1571 			    res->ai_addr)->sin6_scope_id;
   1572 			set_ipmask(n, v6mask);
   1573 		}
   1574 		n->next = NULL;
   1575 		n->tail = n;
   1576 		if (h == NULL)
   1577 			h = n;
   1578 		else {
   1579 			h->tail->next = n;
   1580 			h->tail = n;
   1581 		}
   1582 	}
   1583 	freeaddrinfo(res0);
   1584 	free(ps);
   1585 
   1586 	return (h);
   1587 }
   1588 
   1589 /*
   1590  * convert a hostname to a list of addresses and put them in the given buffer.
   1591  * test:
   1592  *	if set to 1, only simple addresses are accepted (no netblock, no "!").
   1593  */
   1594 int
   1595 append_addr(struct pfr_buffer *b, char *s, int test)
   1596 {
   1597 	char			 *r;
   1598 	struct node_host	*h, *n;
   1599 	int			 rv, not = 0;
   1600 
   1601 	for (r = s; *r == '!'; r++)
   1602 		not = !not;
   1603 	if ((n = host(r)) == NULL) {
   1604 		errno = 0;
   1605 		return (-1);
   1606 	}
   1607 	rv = append_addr_host(b, n, test, not);
   1608 	do {
   1609 		h = n;
   1610 		n = n->next;
   1611 		free(h);
   1612 	} while (n != NULL);
   1613 	return (rv);
   1614 }
   1615 
   1616 /*
   1617  * same as previous function, but with a pre-parsed input and the ability
   1618  * to "negate" the result. Does not free the node_host list.
   1619  * not:
   1620  *      setting it to 1 is equivalent to adding "!" in front of parameter s.
   1621  */
   1622 int
   1623 append_addr_host(struct pfr_buffer *b, struct node_host *n, int test, int not)
   1624 {
   1625 	int			 bits;
   1626 	struct pfr_addr		 addr;
   1627 
   1628 	do {
   1629 		bzero(&addr, sizeof(addr));
   1630 		addr.pfra_not = n->not ^ not;
   1631 		addr.pfra_af = n->af;
   1632 		addr.pfra_net = unmask(&n->addr.v.a.mask, n->af);
   1633 		switch (n->af) {
   1634 		case AF_INET:
   1635 			addr.pfra_ip4addr.s_addr = n->addr.v.a.addr.addr32[0];
   1636 			bits = 32;
   1637 			break;
   1638 		case AF_INET6:
   1639 			memcpy(&addr.pfra_ip6addr, &n->addr.v.a.addr.v6,
   1640 			    sizeof(struct in6_addr));
   1641 			bits = 128;
   1642 			break;
   1643 		default:
   1644 			errno = EINVAL;
   1645 			return (-1);
   1646 		}
   1647 		if ((test && (not || addr.pfra_net != bits)) ||
   1648 		    addr.pfra_net > bits) {
   1649 			errno = EINVAL;
   1650 			return (-1);
   1651 		}
   1652 		if (pfr_buf_add(b, &addr))
   1653 			return (-1);
   1654 	} while ((n = n->next) != NULL);
   1655 
   1656 	return (0);
   1657 }
   1658 
   1659 int
   1660 pfctl_add_trans(struct pfr_buffer *buf, int rs_num, const char *anchor)
   1661 {
   1662 	struct pfioc_trans_e trans;
   1663 
   1664 	bzero(&trans, sizeof(trans));
   1665 	trans.rs_num = rs_num;
   1666 	if (strlcpy(trans.anchor, anchor,
   1667 	    sizeof(trans.anchor)) >= sizeof(trans.anchor))
   1668 		errx(1, "pfctl_add_trans: strlcpy");
   1669 
   1670 	return pfr_buf_add(buf, &trans);
   1671 }
   1672 
   1673 u_int32_t
   1674 pfctl_get_ticket(struct pfr_buffer *buf, int rs_num, const char *anchor)
   1675 {
   1676 	struct pfioc_trans_e *p;
   1677 
   1678 	PFRB_FOREACH(p, buf)
   1679 		if (rs_num == p->rs_num && !strcmp(anchor, p->anchor))
   1680 			return (p->ticket);
   1681 	errx(1, "pfctl_get_ticket: assertion failed");
   1682 }
   1683 
   1684 int
   1685 pfctl_trans(int dev, struct pfr_buffer *buf, u_long cmd, int from)
   1686 {
   1687 	struct pfioc_trans trans;
   1688 
   1689 	bzero(&trans, sizeof(trans));
   1690 	trans.size = buf->pfrb_size - from;
   1691 	trans.esize = sizeof(struct pfioc_trans_e);
   1692 	trans.array = ((struct pfioc_trans_e *)buf->pfrb_caddr) + from;
   1693 	return ioctl(dev, cmd, &trans);
   1694 }
   1695