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