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