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