1139823Simp/*-
2200838Sluigi * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
398943Sluigi *
498943Sluigi * Redistribution and use in source and binary forms, with or without
598943Sluigi * modification, are permitted provided that the following conditions
698943Sluigi * are met:
798943Sluigi * 1. Redistributions of source code must retain the above copyright
898943Sluigi *    notice, this list of conditions and the following disclaimer.
9116763Sluigi * 2. Redistributions in binary form must reproduce the above copyright
1098943Sluigi *    notice, this list of conditions and the following disclaimer in the
1198943Sluigi *    documentation and/or other materials provided with the distribution.
12105775Smaxim *
1398943Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1498943Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1598943Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1698943Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1798943Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1898943Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1998943Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2098943Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2198943Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2298943Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2398943Sluigi * SUCH DAMAGE.
2498943Sluigi */
2598943Sluigi
26172467Ssilby#include <sys/cdefs.h>
27172467Ssilby__FBSDID("$FreeBSD$");
28172467Ssilby
2998943Sluigi/*
30200601Sluigi * The FreeBSD IP packet firewall, main file
3198943Sluigi */
3298943Sluigi
33225518Sjhb#include "opt_ipfw.h"
34166479Salc#include "opt_ipdivert.h"
3598943Sluigi#include "opt_inet.h"
3698943Sluigi#ifndef INET
3798943Sluigi#error IPFIREWALL requires INET.
3898943Sluigi#endif /* INET */
39152928Sume#include "opt_inet6.h"
40152928Sume#include "opt_ipsec.h"
4198943Sluigi
4298943Sluigi#include <sys/param.h>
4398943Sluigi#include <sys/systm.h>
44138642Scsjp#include <sys/condvar.h>
45165648Spiso#include <sys/eventhandler.h>
4698943Sluigi#include <sys/malloc.h>
4798943Sluigi#include <sys/mbuf.h>
4898943Sluigi#include <sys/kernel.h>
49155201Scsjp#include <sys/lock.h>
50133600Scsjp#include <sys/jail.h>
51129876Sphk#include <sys/module.h>
52164033Srwatson#include <sys/priv.h>
5398943Sluigi#include <sys/proc.h>
54155201Scsjp#include <sys/rwlock.h>
5598943Sluigi#include <sys/socket.h>
5698943Sluigi#include <sys/socketvar.h>
5798943Sluigi#include <sys/sysctl.h>
5898943Sluigi#include <sys/syslog.h>
5998943Sluigi#include <sys/ucred.h>
60188676Sluigi#include <net/ethernet.h> /* for ETHERTYPE_IP */
6198943Sluigi#include <net/if.h>
6298943Sluigi#include <net/route.h>
63171173Smlaier#include <net/pf_mtag.h>
64243586Sae#include <net/pfil.h>
65185571Sbz#include <net/vnet.h>
66175659Srwatson
6798943Sluigi#include <netinet/in.h>
6898943Sluigi#include <netinet/in_var.h>
6998943Sluigi#include <netinet/in_pcb.h>
7098943Sluigi#include <netinet/ip.h>
7198943Sluigi#include <netinet/ip_var.h>
7298943Sluigi#include <netinet/ip_icmp.h>
7398943Sluigi#include <netinet/ip_fw.h>
74163069Sbz#include <netinet/ip_carp.h>
75161767Sjhay#include <netinet/pim.h>
7698943Sluigi#include <netinet/tcp_var.h>
7798943Sluigi#include <netinet/udp.h>
7898943Sluigi#include <netinet/udp_var.h>
79164258Sbz#include <netinet/sctp.h>
80185571Sbz
81145246Sbrooks#include <netinet/ip6.h>
82145246Sbrooks#include <netinet/icmp6.h>
83148414Sume#ifdef INET6
84223073Sae#include <netinet6/in6_pcb.h>
85148414Sume#include <netinet6/scope6_var.h>
86200027Sume#include <netinet6/ip6_var.h>
87148414Sume#endif
88145246Sbrooks
89243401Sglebius#include <netpfil/ipfw/ip_fw_private.h>
90243401Sglebius
9199475Sluigi#include <machine/in_cksum.h>	/* XXX for in_cksum */
9299475Sluigi
93188580Sluigi#ifdef MAC
94163606Srwatson#include <security/mac/mac_framework.h>
95188580Sluigi#endif
96163606Srwatson
97101628Sluigi/*
98200601Sluigi * static variables followed by global ones.
99200601Sluigi * All ipfw global variables are here.
100101628Sluigi */
10198943Sluigi
102200601Sluigi/* ipfw_vnet_ready controls when we are open for business */
103215701Sdimstatic VNET_DEFINE(int, ipfw_vnet_ready) = 0;
104200601Sluigi#define	V_ipfw_vnet_ready	VNET(ipfw_vnet_ready)
105195699Srwatson
106215701Sdimstatic VNET_DEFINE(int, fw_deny_unknown_exthdrs);
107200601Sluigi#define	V_fw_deny_unknown_exthdrs	VNET(fw_deny_unknown_exthdrs)
108200601Sluigi
109225030Sbzstatic VNET_DEFINE(int, fw_permit_single_frag6) = 1;
110225030Sbz#define	V_fw_permit_single_frag6	VNET(fw_permit_single_frag6)
111225030Sbz
112191932Sjhb#ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
113191932Sjhbstatic int default_to_accept = 1;
114191932Sjhb#else
115191932Sjhbstatic int default_to_accept;
116191932Sjhb#endif
11798943Sluigi
118200601SluigiVNET_DEFINE(int, autoinc_step);
119220878SbzVNET_DEFINE(int, fw_one_pass) = 1;
120193859Soleg
121234597SmelifaroVNET_DEFINE(unsigned int, fw_tables_max);
122234597Smelifaro/* Use 128 tables by default */
123234597Smelifarostatic unsigned int default_fw_tables = IPFW_TABLES_DEFAULT;
124234597Smelifaro
125130363Scsjp/*
126200601Sluigi * Each rule belongs to one of 32 different sets (0..31).
127200601Sluigi * The variable set_disable contains one bit per set.
128200601Sluigi * If the bit is set, all rules in the corresponding set
129200601Sluigi * are disabled. Set RESVD_SET(31) is reserved for the default rule
130200601Sluigi * and rules that are not deleted by the flush command,
131200601Sluigi * and CANNOT be disabled.
132200601Sluigi * Rules in set RESVD_SET can only be deleted individually.
13398943Sluigi */
134200601SluigiVNET_DEFINE(u_int32_t, set_disable);
135200601Sluigi#define	V_set_disable			VNET(set_disable)
136200601Sluigi
137200601SluigiVNET_DEFINE(int, fw_verbose);
138200601Sluigi/* counter for ipfw_log(NULL...) */
139200601SluigiVNET_DEFINE(u_int64_t, norule_counter);
140200601SluigiVNET_DEFINE(int, verbose_limit);
141200601Sluigi
142200601Sluigi/* layer3_chain contains the list of rules for layer 3 */
143195699SrwatsonVNET_DEFINE(struct ip_fw_chain, layer3_chain);
14498943Sluigi
145255395StrocinyVNET_DEFINE(int, ipfw_nat_ready) = 0;
146255395Strociny
147176669Spisoipfw_nat_t *ipfw_nat_ptr = NULL;
148200580Sluigistruct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
149176669Spisoipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
150176669Spisoipfw_nat_cfg_t *ipfw_nat_del_ptr;
151176669Spisoipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
152176669Spisoipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
15398943Sluigi
15498943Sluigi#ifdef SYSCTL_NODE
155204591Sluigiuint32_t dummy_def = IPFW_DEFAULT_RULE;
156234597Smelifarostatic int sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS);
157204591Sluigi
158204591SluigiSYSBEGIN(f3)
159204591Sluigi
16098943SluigiSYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
161200601SluigiSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, one_pass,
162200601Sluigi    CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0,
163200601Sluigi    "Only do a single pass through ipfw when using dummynet(4)");
164195699SrwatsonSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step,
165195862Sjulian    CTLFLAG_RW, &VNET_NAME(autoinc_step), 0,
166195862Sjulian    "Rule number auto-increment step");
167195699SrwatsonSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose,
168195862Sjulian    CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0,
169195862Sjulian    "Log matches to ipfw rules");
170195699SrwatsonSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit,
171195699Srwatson    CTLFLAG_RW, &VNET_NAME(verbose_limit), 0,
172186048Sbz    "Set upper limit of matches of ipfw rules logged");
173182818SrikSYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD,
174204591Sluigi    &dummy_def, 0,
175195862Sjulian    "The default/max possible rule number.");
176234597SmelifaroSYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, tables_max,
177234597Smelifaro    CTLTYPE_UINT|CTLFLAG_RW, 0, 0, sysctl_ipfw_table_num, "IU",
178234597Smelifaro    "Maximum number of tables");
179191932SjhbSYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN,
180195862Sjulian    &default_to_accept, 0,
181195862Sjulian    "Make the default rule accept all packets.");
182191932SjhbTUNABLE_INT("net.inet.ip.fw.default_to_accept", &default_to_accept);
183234597SmelifaroTUNABLE_INT("net.inet.ip.fw.tables_max", &default_fw_tables);
184200838SluigiSYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, static_count,
185200838Sluigi    CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0,
186200838Sluigi    "Number of static rules");
187200040Sluigi
188195862Sjulian#ifdef INET6
189195862SjulianSYSCTL_DECL(_net_inet6_ip6);
190195862SjulianSYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
191195862SjulianSYSCTL_VNET_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs,
192195862Sjulian    CTLFLAG_RW | CTLFLAG_SECURE, &VNET_NAME(fw_deny_unknown_exthdrs), 0,
193195862Sjulian    "Deny packets with unknown IPv6 Extension Headers");
194225030SbzSYSCTL_VNET_INT(_net_inet6_ip6_fw, OID_AUTO, permit_single_frag6,
195225030Sbz    CTLFLAG_RW | CTLFLAG_SECURE, &VNET_NAME(fw_permit_single_frag6), 0,
196225030Sbz    "Permit single packet IPv6 fragments");
197200040Sluigi#endif /* INET6 */
19898943Sluigi
199204591SluigiSYSEND
200204591Sluigi
201187821Sluigi#endif /* SYSCTL_NODE */
20298943Sluigi
203200580Sluigi
204149020Sbz/*
205200601Sluigi * Some macros used in the various matching options.
206145093Sbrooks * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
207145093Sbrooks * Other macros just cast void * into the appropriate type
20898943Sluigi */
209145093Sbrooks#define	L3HDR(T, ip)	((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
210145093Sbrooks#define	TCP(p)		((struct tcphdr *)(p))
211164258Sbz#define	SCTP(p)		((struct sctphdr *)(p))
212145093Sbrooks#define	UDP(p)		((struct udphdr *)(p))
213145565Sbrooks#define	ICMP(p)		((struct icmphdr *)(p))
214145246Sbrooks#define	ICMP6(p)	((struct icmp6_hdr *)(p))
21598943Sluigi
21699622Sluigistatic __inline int
217145565Sbrooksicmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd)
21898943Sluigi{
219145093Sbrooks	int type = icmp->icmp_type;
22098943Sluigi
22198943Sluigi	return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) );
22298943Sluigi}
22398943Sluigi
22498943Sluigi#define TT	( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
22598943Sluigi    (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
22698943Sluigi
22798943Sluigistatic int
228145565Sbrooksis_icmp_query(struct icmphdr *icmp)
22998943Sluigi{
230145093Sbrooks	int type = icmp->icmp_type;
231145093Sbrooks
23298943Sluigi	return (type <= ICMP_MAXTYPE && (TT & (1<<type)) );
23398943Sluigi}
23498943Sluigi#undef TT
23598943Sluigi
23698943Sluigi/*
23798943Sluigi * The following checks use two arrays of 8 or 16 bits to store the
23898943Sluigi * bits that we want set or clear, respectively. They are in the
23998943Sluigi * low and high half of cmd->arg1 or cmd->d[0].
24098943Sluigi *
24198943Sluigi * We scan options and store the bits we find set. We succeed if
24298943Sluigi *
24398943Sluigi *	(want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
24498943Sluigi *
24598943Sluigi * The code is sometimes optimized not to store additional variables.
24698943Sluigi */
24798943Sluigi
24898943Sluigistatic int
24998943Sluigiflags_match(ipfw_insn *cmd, u_int8_t bits)
25098943Sluigi{
25198943Sluigi	u_char want_clear;
25298943Sluigi	bits = ~bits;
25398943Sluigi
25498943Sluigi	if ( ((cmd->arg1 & 0xff) & bits) != 0)
25598943Sluigi		return 0; /* some bits we want set were clear */
25698943Sluigi	want_clear = (cmd->arg1 >> 8) & 0xff;
25798943Sluigi	if ( (want_clear & bits) != want_clear)
25898943Sluigi		return 0; /* some bits we want clear were set */
25998943Sluigi	return 1;
26098943Sluigi}
26198943Sluigi
26298943Sluigistatic int
26398943Sluigiipopts_match(struct ip *ip, ipfw_insn *cmd)
26498943Sluigi{
26598943Sluigi	int optlen, bits = 0;
26698943Sluigi	u_char *cp = (u_char *)(ip + 1);
26798943Sluigi	int x = (ip->ip_hl << 2) - sizeof (struct ip);
26898943Sluigi
26998943Sluigi	for (; x > 0; x -= optlen, cp += optlen) {
27098943Sluigi		int opt = cp[IPOPT_OPTVAL];
27198943Sluigi
27298943Sluigi		if (opt == IPOPT_EOL)
27398943Sluigi			break;
27498943Sluigi		if (opt == IPOPT_NOP)
27598943Sluigi			optlen = 1;
27698943Sluigi		else {
27798943Sluigi			optlen = cp[IPOPT_OLEN];
27898943Sluigi			if (optlen <= 0 || optlen > x)
27998943Sluigi				return 0; /* invalid or truncated */
28098943Sluigi		}
28198943Sluigi		switch (opt) {
28298943Sluigi
28398943Sluigi		default:
28498943Sluigi			break;
28598943Sluigi
28698943Sluigi		case IPOPT_LSRR:
28798943Sluigi			bits |= IP_FW_IPOPT_LSRR;
28898943Sluigi			break;
28998943Sluigi
29098943Sluigi		case IPOPT_SSRR:
29198943Sluigi			bits |= IP_FW_IPOPT_SSRR;
29298943Sluigi			break;
29398943Sluigi
29498943Sluigi		case IPOPT_RR:
29598943Sluigi			bits |= IP_FW_IPOPT_RR;
29698943Sluigi			break;
29798943Sluigi
29898943Sluigi		case IPOPT_TS:
29998943Sluigi			bits |= IP_FW_IPOPT_TS;
30098943Sluigi			break;
30198943Sluigi		}
30298943Sluigi	}
30398943Sluigi	return (flags_match(cmd, bits));
30498943Sluigi}
30598943Sluigi
30698943Sluigistatic int
307145093Sbrookstcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd)
30898943Sluigi{
30998943Sluigi	int optlen, bits = 0;
31098943Sluigi	u_char *cp = (u_char *)(tcp + 1);
31198943Sluigi	int x = (tcp->th_off << 2) - sizeof(struct tcphdr);
31298943Sluigi
31398943Sluigi	for (; x > 0; x -= optlen, cp += optlen) {
31498943Sluigi		int opt = cp[0];
31598943Sluigi		if (opt == TCPOPT_EOL)
31698943Sluigi			break;
31798943Sluigi		if (opt == TCPOPT_NOP)
31898943Sluigi			optlen = 1;
31998943Sluigi		else {
32098943Sluigi			optlen = cp[1];
32198943Sluigi			if (optlen <= 0)
32298943Sluigi				break;
32398943Sluigi		}
32498943Sluigi
32598943Sluigi		switch (opt) {
32698943Sluigi
32798943Sluigi		default:
32898943Sluigi			break;
32998943Sluigi
33098943Sluigi		case TCPOPT_MAXSEG:
33198943Sluigi			bits |= IP_FW_TCPOPT_MSS;
33298943Sluigi			break;
33398943Sluigi
33498943Sluigi		case TCPOPT_WINDOW:
33598943Sluigi			bits |= IP_FW_TCPOPT_WINDOW;
33698943Sluigi			break;
33798943Sluigi
33898943Sluigi		case TCPOPT_SACK_PERMITTED:
33998943Sluigi		case TCPOPT_SACK:
34098943Sluigi			bits |= IP_FW_TCPOPT_SACK;
34198943Sluigi			break;
34298943Sluigi
34398943Sluigi		case TCPOPT_TIMESTAMP:
34498943Sluigi			bits |= IP_FW_TCPOPT_TS;
34598943Sluigi			break;
34698943Sluigi
34798943Sluigi		}
34898943Sluigi	}
34998943Sluigi	return (flags_match(cmd, bits));
35098943Sluigi}
35198943Sluigi
35298943Sluigistatic int
353234597Smelifaroiface_match(struct ifnet *ifp, ipfw_insn_if *cmd, struct ip_fw_chain *chain, uint32_t *tablearg)
35498943Sluigi{
35598943Sluigi	if (ifp == NULL)	/* no iface with this packet, match fails */
35698943Sluigi		return 0;
35798943Sluigi	/* Check by name or by IP address */
35899475Sluigi	if (cmd->name[0] != '\0') { /* match by name */
359234597Smelifaro		if (cmd->name[0] == '\1') /* use tablearg to match */
360234597Smelifaro			return ipfw_lookup_table_extended(chain, cmd->p.glob,
361234597Smelifaro				ifp->if_xname, tablearg, IPFW_TABLE_INTERFACE);
36298943Sluigi		/* Check name */
363121816Sbrooks		if (cmd->p.glob) {
364121816Sbrooks			if (fnmatch(cmd->name, ifp->if_xname, 0) == 0)
365121816Sbrooks				return(1);
366121816Sbrooks		} else {
367121816Sbrooks			if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
368121816Sbrooks				return(1);
369121816Sbrooks		}
37098943Sluigi	} else {
371210120Sluigi#ifdef __FreeBSD__	/* and OSX too ? */
37298943Sluigi		struct ifaddr *ia;
37398943Sluigi
374195023Srwatson		if_addr_rlock(ifp);
37598943Sluigi		TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
37698943Sluigi			if (ia->ifa_addr->sa_family != AF_INET)
37798943Sluigi				continue;
37898943Sluigi			if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
379191288Srwatson			    (ia->ifa_addr))->sin_addr.s_addr) {
380195023Srwatson				if_addr_runlock(ifp);
38198943Sluigi				return(1);	/* match */
382191288Srwatson			}
38398943Sluigi		}
384195023Srwatson		if_addr_runlock(ifp);
385204591Sluigi#endif /* __FreeBSD__ */
38698943Sluigi	}
38798943Sluigi	return(0);	/* no match, fail ... */
38898943Sluigi}
38998943Sluigi
390112250Scjc/*
391128575Sandre * The verify_path function checks if a route to the src exists and
392128575Sandre * if it is reachable via ifp (when provided).
393128575Sandre *
394112250Scjc * The 'verrevpath' option checks that the interface that an IP packet
395116763Sluigi * arrives on is the same interface that traffic destined for the
396200601Sluigi * packet's source address would be routed out of.
397200601Sluigi * The 'versrcreach' option just checks that the source address is
398200601Sluigi * reachable via any route (except default) in the routing table.
399200601Sluigi * These two are a measure to block forged packets. This is also
400200601Sluigi * commonly known as "anti-spoofing" or Unicast Reverse Path
401200601Sluigi * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs
402128575Sandre * is purposely reminiscent of the Cisco IOS command,
403112250Scjc *
404112250Scjc *   ip verify unicast reverse-path
405128575Sandre *   ip verify unicast source reachable-via any
406112250Scjc *
407200601Sluigi * which implements the same functionality. But note that the syntax
408200601Sluigi * is misleading, and the check may be performed on all IP packets
409200601Sluigi * whether unicast, multicast, or broadcast.
410112250Scjc */
411112250Scjcstatic int
412178888Sjulianverify_path(struct in_addr src, struct ifnet *ifp, u_int fib)
413112250Scjc{
414204591Sluigi#ifndef __FreeBSD__
415204591Sluigi	return 0;
416204591Sluigi#else
417123000Sandre	struct route ro;
418112250Scjc	struct sockaddr_in *dst;
419112250Scjc
420123000Sandre	bzero(&ro, sizeof(ro));
421123000Sandre
422112250Scjc	dst = (struct sockaddr_in *)&(ro.ro_dst);
423123000Sandre	dst->sin_family = AF_INET;
424123000Sandre	dst->sin_len = sizeof(*dst);
425123000Sandre	dst->sin_addr = src;
426186119Sqingli	in_rtalloc_ign(&ro, 0, fib);
427112250Scjc
428122922Sandre	if (ro.ro_rt == NULL)
429112250Scjc		return 0;
430128575Sandre
431154769Soleg	/*
432154769Soleg	 * If ifp is provided, check for equality with rtentry.
433154769Soleg	 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
434154769Soleg	 * in order to pass packets injected back by if_simloop():
435154769Soleg	 * if useloopback == 1 routing entry (via lo0) for our own address
436154769Soleg	 * may exist, so we need to handle routing assymetry.
437154769Soleg	 */
438154769Soleg	if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) {
439122922Sandre		RTFREE(ro.ro_rt);
440122922Sandre		return 0;
441122922Sandre	}
442128575Sandre
443128575Sandre	/* if no ifp provided, check if rtentry is not default route */
444128575Sandre	if (ifp == NULL &&
445128575Sandre	     satosin(rt_key(ro.ro_rt))->sin_addr.s_addr == INADDR_ANY) {
446128575Sandre		RTFREE(ro.ro_rt);
447128575Sandre		return 0;
448128575Sandre	}
449128575Sandre
450132510Sandre	/* or if this is a blackhole/reject route */
451132510Sandre	if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
452132510Sandre		RTFREE(ro.ro_rt);
453132510Sandre		return 0;
454132510Sandre	}
455132510Sandre
456128575Sandre	/* found valid route */
457122922Sandre	RTFREE(ro.ro_rt);
458116981Sluigi	return 1;
459204591Sluigi#endif /* __FreeBSD__ */
460112250Scjc}
461112250Scjc
462145266Sphk#ifdef INET6
463145246Sbrooks/*
464145246Sbrooks * ipv6 specific rules here...
465145246Sbrooks */
466145246Sbrooksstatic __inline int
467145246Sbrooksicmp6type_match (int type, ipfw_insn_u32 *cmd)
468145246Sbrooks{
469147415Smlaier	return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) );
470145246Sbrooks}
471112250Scjc
472145246Sbrooksstatic int
473145246Sbrooksflow6id_match( int curr_flow, ipfw_insn_u32 *cmd )
474145246Sbrooks{
475147415Smlaier	int i;
476147415Smlaier	for (i=0; i <= cmd->o.arg1; ++i )
477147415Smlaier		if (curr_flow == cmd->d[i] )
478147415Smlaier			return 1;
479147415Smlaier	return 0;
480145246Sbrooks}
481145246Sbrooks
482145246Sbrooks/* support for IP6_*_ME opcodes */
483145246Sbrooksstatic int
484145246Sbrookssearch_ip6_addr_net (struct in6_addr * ip6_addr)
485145246Sbrooks{
486147415Smlaier	struct ifnet *mdc;
487147415Smlaier	struct ifaddr *mdc2;
488147415Smlaier	struct in6_ifaddr *fdm;
489147415Smlaier	struct in6_addr copia;
490145246Sbrooks
491191288Srwatson	TAILQ_FOREACH(mdc, &V_ifnet, if_link) {
492195023Srwatson		if_addr_rlock(mdc);
493191338Srwatson		TAILQ_FOREACH(mdc2, &mdc->if_addrhead, ifa_link) {
494147415Smlaier			if (mdc2->ifa_addr->sa_family == AF_INET6) {
495147415Smlaier				fdm = (struct in6_ifaddr *)mdc2;
496147415Smlaier				copia = fdm->ia_addr.sin6_addr;
497147415Smlaier				/* need for leaving scope_id in the sock_addr */
498147415Smlaier				in6_clearscope(&copia);
499191288Srwatson				if (IN6_ARE_ADDR_EQUAL(ip6_addr, &copia)) {
500195023Srwatson					if_addr_runlock(mdc);
501147415Smlaier					return 1;
502191288Srwatson				}
503147415Smlaier			}
504147415Smlaier		}
505195023Srwatson		if_addr_runlock(mdc);
506191288Srwatson	}
507147415Smlaier	return 0;
508145246Sbrooks}
509145246Sbrooks
510145246Sbrooksstatic int
511232292Sbzverify_path6(struct in6_addr *src, struct ifnet *ifp, u_int fib)
512145246Sbrooks{
513147418Smlaier	struct route_in6 ro;
514147415Smlaier	struct sockaddr_in6 *dst;
515145246Sbrooks
516147418Smlaier	bzero(&ro, sizeof(ro));
517147418Smlaier
518147415Smlaier	dst = (struct sockaddr_in6 * )&(ro.ro_dst);
519147418Smlaier	dst->sin6_family = AF_INET6;
520147418Smlaier	dst->sin6_len = sizeof(*dst);
521147418Smlaier	dst->sin6_addr = *src;
522145246Sbrooks
523232292Sbz	in6_rtalloc_ign(&ro, 0, fib);
524147418Smlaier	if (ro.ro_rt == NULL)
525147418Smlaier		return 0;
526147418Smlaier
527152288Ssuz	/*
528152288Ssuz	 * if ifp is provided, check for equality with rtentry
529152288Ssuz	 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
530152288Ssuz	 * to support the case of sending packets to an address of our own.
531152288Ssuz	 * (where the former interface is the first argument of if_simloop()
532152288Ssuz	 *  (=ifp), the latter is lo0)
533152288Ssuz	 */
534152288Ssuz	if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) {
535147418Smlaier		RTFREE(ro.ro_rt);
536147418Smlaier		return 0;
537147415Smlaier	}
538147418Smlaier
539147418Smlaier	/* if no ifp provided, check if rtentry is not default route */
540147418Smlaier	if (ifp == NULL &&
541147418Smlaier	    IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(ro.ro_rt))->sin6_addr)) {
542147418Smlaier		RTFREE(ro.ro_rt);
543147415Smlaier		return 0;
544147418Smlaier	}
545147418Smlaier
546147418Smlaier	/* or if this is a blackhole/reject route */
547147418Smlaier	if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
548147418Smlaier		RTFREE(ro.ro_rt);
549147418Smlaier		return 0;
550147418Smlaier	}
551147418Smlaier
552147418Smlaier	/* found valid route */
553147418Smlaier	RTFREE(ro.ro_rt);
554147415Smlaier	return 1;
555147418Smlaier
556145246Sbrooks}
557200601Sluigi
558149020Sbzstatic int
559149020Sbzis_icmp6_query(int icmp6_type)
560149020Sbz{
561149020Sbz	if ((icmp6_type <= ICMP6_MAXTYPE) &&
562149020Sbz	    (icmp6_type == ICMP6_ECHO_REQUEST ||
563149020Sbz	    icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
564149020Sbz	    icmp6_type == ICMP6_WRUREQUEST ||
565149020Sbz	    icmp6_type == ICMP6_FQDN_QUERY ||
566149020Sbz	    icmp6_type == ICMP6_NI_QUERY))
567149020Sbz		return (1);
568149020Sbz
569149020Sbz	return (0);
570149020Sbz}
571149020Sbz
572149020Sbzstatic void
573165738Sjuliansend_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6)
574149020Sbz{
575165738Sjulian	struct mbuf *m;
576165738Sjulian
577165738Sjulian	m = args->m;
578160025Sbz	if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) {
579149020Sbz		struct tcphdr *tcp;
580165738Sjulian		tcp = (struct tcphdr *)((char *)ip6 + hlen);
581149020Sbz
582200027Sume		if ((tcp->th_flags & TH_RST) == 0) {
583200027Sume			struct mbuf *m0;
584200601Sluigi			m0 = ipfw_send_pkt(args->m, &(args->f_id),
585200027Sume			    ntohl(tcp->th_seq), ntohl(tcp->th_ack),
586200027Sume			    tcp->th_flags | TH_RST);
587200027Sume			if (m0 != NULL)
588200027Sume				ip6_output(m0, NULL, NULL, 0, NULL, NULL,
589200027Sume				    NULL);
590149020Sbz		}
591201527Sluigi		FREE_PKT(m);
592149020Sbz	} else if (code != ICMP6_UNREACH_RST) { /* Send an ICMPv6 unreach. */
593165738Sjulian#if 0
594165738Sjulian		/*
595165738Sjulian		 * Unlike above, the mbufs need to line up with the ip6 hdr,
596165738Sjulian		 * as the contents are read. We need to m_adj() the
597165738Sjulian		 * needed amount.
598165738Sjulian		 * The mbuf will however be thrown away so we can adjust it.
599165738Sjulian		 * Remember we did an m_pullup on it already so we
600165738Sjulian		 * can make some assumptions about contiguousness.
601165738Sjulian		 */
602165738Sjulian		if (args->L3offset)
603165738Sjulian			m_adj(m, args->L3offset);
604165738Sjulian#endif
605165738Sjulian		icmp6_error(m, ICMP6_DST_UNREACH, code, 0);
606149020Sbz	} else
607201527Sluigi		FREE_PKT(m);
608149020Sbz
609149020Sbz	args->m = NULL;
610149020Sbz}
611149020Sbz
612145266Sphk#endif /* INET6 */
613145266Sphk
61498943Sluigi
61598943Sluigi/*
61698943Sluigi * sends a reject message, consuming the mbuf passed as an argument.
61798943Sluigi */
61898943Sluigistatic void
619201122Sluigisend_reject(struct ip_fw_args *args, int code, int iplen, struct ip *ip)
62098943Sluigi{
621101843Sphk
622165738Sjulian#if 0
623165738Sjulian	/* XXX When ip is not guaranteed to be at mtod() we will
624165738Sjulian	 * need to account for this */
625165738Sjulian	 * The mbuf will however be thrown away so we can adjust it.
626165738Sjulian	 * Remember we did an m_pullup on it already so we
627165738Sjulian	 * can make some assumptions about contiguousness.
628165738Sjulian	 */
629165738Sjulian	if (args->L3offset)
630165738Sjulian		m_adj(m, args->L3offset);
631165738Sjulian#endif
632108327Siedowse	if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */
633108327Siedowse		/* We need the IP header in host order for icmp_error(). */
634201527Sluigi		SET_HOST_IPLEN(ip);
63599475Sluigi		icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
636160025Sbz	} else if (args->f_id.proto == IPPROTO_TCP) {
63799475Sluigi		struct tcphdr *const tcp =
63899475Sluigi		    L3HDR(struct tcphdr, mtod(args->m, struct ip *));
639147247Sgreen		if ( (tcp->th_flags & TH_RST) == 0) {
640147247Sgreen			struct mbuf *m;
641200601Sluigi			m = ipfw_send_pkt(args->m, &(args->f_id),
642162238Scsjp				ntohl(tcp->th_seq), ntohl(tcp->th_ack),
643100004Sluigi				tcp->th_flags | TH_RST);
644147247Sgreen			if (m != NULL)
645147247Sgreen				ip_output(m, NULL, NULL, 0, NULL, NULL);
646147247Sgreen		}
647201527Sluigi		FREE_PKT(args->m);
64899475Sluigi	} else
649201527Sluigi		FREE_PKT(args->m);
65099475Sluigi	args->m = NULL;
65198943Sluigi}
65298943Sluigi
653200601Sluigi/*
654200601Sluigi * Support for uid/gid/jail lookup. These tests are expensive
655200601Sluigi * (because we may need to look into the list of active sockets)
656200601Sluigi * so we cache the results. ugid_lookupp is 0 if we have not
657200601Sluigi * yet done a lookup, 1 if we succeeded, and -1 if we tried
658200601Sluigi * and failed. The function always returns the match value.
659200601Sluigi * We could actually spare the variable and use *uc, setting
660200601Sluigi * it to '(void *)check_uidgid if we have no info, NULL if
661200601Sluigi * we tried and failed, or any other value if successful.
662200601Sluigi */
663122265Ssamstatic int
664223073Saecheck_uidgid(ipfw_insn_u32 *insn, struct ip_fw_args *args, int *ugid_lookupp,
665223073Sae    struct ucred **uc)
666122265Ssam{
667204591Sluigi#ifndef __FreeBSD__
668223073Sae	/* XXX */
669204591Sluigi	return cred_check(insn, proto, oif,
670204591Sluigi	    dst_ip, dst_port, src_ip, src_port,
671204591Sluigi	    (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb);
672204591Sluigi#else  /* FreeBSD */
673223073Sae	struct in_addr src_ip, dst_ip;
674122265Ssam	struct inpcbinfo *pi;
675223073Sae	struct ipfw_flow_id *id;
676223073Sae	struct inpcb *pcb, *inp;
677223073Sae	struct ifnet *oif;
678222488Srwatson	int lookupflags;
679122265Ssam	int match;
680122265Ssam
681223073Sae	id = &args->f_id;
682223073Sae	inp = args->inp;
683223073Sae	oif = args->oif;
684223073Sae
685130363Scsjp	/*
686135920Smlaier	 * Check to see if the UDP or TCP stack supplied us with
687135920Smlaier	 * the PCB. If so, rather then holding a lock and looking
688135920Smlaier	 * up the PCB, we can use the one that was supplied.
689135920Smlaier	 */
690183398Srwatson	if (inp && *ugid_lookupp == 0) {
691178325Srwatson		INP_LOCK_ASSERT(inp);
692135920Smlaier		if (inp->inp_socket != NULL) {
693194498Sbrooks			*uc = crhold(inp->inp_cred);
694183398Srwatson			*ugid_lookupp = 1;
695183418Srwatson		} else
696183418Srwatson			*ugid_lookupp = -1;
697135920Smlaier	}
698135920Smlaier	/*
699130363Scsjp	 * If we have already been here and the packet has no
700130363Scsjp	 * PCB entry associated with it, then we can safely
701130363Scsjp	 * assume that this is a no match.
702130363Scsjp	 */
703183398Srwatson	if (*ugid_lookupp == -1)
704130363Scsjp		return (0);
705223073Sae	if (id->proto == IPPROTO_TCP) {
706222488Srwatson		lookupflags = 0;
707181803Sbz		pi = &V_tcbinfo;
708223073Sae	} else if (id->proto == IPPROTO_UDP) {
709222488Srwatson		lookupflags = INPLOOKUP_WILDCARD;
710181803Sbz		pi = &V_udbinfo;
711122265Ssam	} else
712122265Ssam		return 0;
713222488Srwatson	lookupflags |= INPLOOKUP_RLOCKPCB;
714122265Ssam	match = 0;
715183398Srwatson	if (*ugid_lookupp == 0) {
716223073Sae		if (id->addr_type == 6) {
717223073Sae#ifdef INET6
718223073Sae			if (oif == NULL)
719223073Sae				pcb = in6_pcblookup_mbuf(pi,
720223073Sae				    &id->src_ip6, htons(id->src_port),
721223073Sae				    &id->dst_ip6, htons(id->dst_port),
722223073Sae				    lookupflags, oif, args->m);
723223073Sae			else
724223073Sae				pcb = in6_pcblookup_mbuf(pi,
725223073Sae				    &id->dst_ip6, htons(id->dst_port),
726223073Sae				    &id->src_ip6, htons(id->src_port),
727223073Sae				    lookupflags, oif, args->m);
728223073Sae#else
729223073Sae			*ugid_lookupp = -1;
730223073Sae			return (0);
731223073Sae#endif
732223073Sae		} else {
733223073Sae			src_ip.s_addr = htonl(id->src_ip);
734223073Sae			dst_ip.s_addr = htonl(id->dst_ip);
735223073Sae			if (oif == NULL)
736223073Sae				pcb = in_pcblookup_mbuf(pi,
737223073Sae				    src_ip, htons(id->src_port),
738223073Sae				    dst_ip, htons(id->dst_port),
739223073Sae				    lookupflags, oif, args->m);
740223073Sae			else
741223073Sae				pcb = in_pcblookup_mbuf(pi,
742223073Sae				    dst_ip, htons(id->dst_port),
743223073Sae				    src_ip, htons(id->src_port),
744223073Sae				    lookupflags, oif, args->m);
745223073Sae		}
746130363Scsjp		if (pcb != NULL) {
747222488Srwatson			INP_RLOCK_ASSERT(pcb);
748196201Sjulian			*uc = crhold(pcb->inp_cred);
749183606Sbz			*ugid_lookupp = 1;
750222488Srwatson			INP_RUNLOCK(pcb);
751122265Ssam		}
752183398Srwatson		if (*ugid_lookupp == 0) {
753130363Scsjp			/*
754200601Sluigi			 * We tried and failed, set the variable to -1
755200601Sluigi			 * so we will not try again on this packet.
756130363Scsjp			 */
757183398Srwatson			*ugid_lookupp = -1;
758130363Scsjp			return (0);
759130363Scsjp		}
760223073Sae	}
761130363Scsjp	if (insn->o.opcode == O_UID)
762194498Sbrooks		match = ((*uc)->cr_uid == (uid_t)insn->d[0]);
763194498Sbrooks	else if (insn->o.opcode == O_GID)
764194498Sbrooks		match = groupmember((gid_t)insn->d[0], *uc);
765194498Sbrooks	else if (insn->o.opcode == O_JAIL)
766194498Sbrooks		match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]);
767223073Sae	return (match);
768204591Sluigi#endif /* __FreeBSD__ */
769122265Ssam}
770122265Ssam
77198943Sluigi/*
772201527Sluigi * Helper function to set args with info on the rule after the matching
773201527Sluigi * one. slot is precise, whereas we guess rule_id as they are
774201527Sluigi * assigned sequentially.
775200838Sluigi */
776200838Sluigistatic inline void
777200855Sluigiset_match(struct ip_fw_args *args, int slot,
778200855Sluigi	struct ip_fw_chain *chain)
779200838Sluigi{
780201527Sluigi	args->rule.chain_id = chain->id;
781201527Sluigi	args->rule.slot = slot + 1; /* we use 0 as a marker */
782201527Sluigi	args->rule.rule_id = 1 + chain->map[slot]->id;
783201527Sluigi	args->rule.rulenum = chain->map[slot]->rulenum;
784200838Sluigi}
785200838Sluigi
786200838Sluigi/*
787250760Smelifaro * Helper function to enable cached rule lookups using
788250760Smelifaro * x_next and next_rule fields in ipfw rule.
789250760Smelifaro */
790250760Smelifarostatic int
791250760Smelifarojump_fast(struct ip_fw_chain *chain, struct ip_fw *f, int num,
792250760Smelifaro    int tablearg, int jump_backwards)
793250760Smelifaro{
794250760Smelifaro	int f_pos;
795250760Smelifaro
796250760Smelifaro	/* If possible use cached f_pos (in f->next_rule),
797250760Smelifaro	 * whose version is written in f->next_rule
798250760Smelifaro	 * (horrible hacks to avoid changing the ABI).
799250760Smelifaro	 */
800250760Smelifaro	if (num != IP_FW_TABLEARG && (uintptr_t)f->x_next == chain->id)
801250760Smelifaro		f_pos = (uintptr_t)f->next_rule;
802250760Smelifaro	else {
803250760Smelifaro		int i = IP_FW_ARG_TABLEARG(num);
804250760Smelifaro		/* make sure we do not jump backward */
805250760Smelifaro		if (jump_backwards == 0 && i <= f->rulenum)
806250760Smelifaro			i = f->rulenum + 1;
807250760Smelifaro		f_pos = ipfw_find_rule(chain, i, 0);
808250760Smelifaro		/* update the cache */
809250760Smelifaro		if (num != IP_FW_TABLEARG) {
810250760Smelifaro			f->next_rule = (void *)(uintptr_t)f_pos;
811250760Smelifaro			f->x_next = (void *)(uintptr_t)chain->id;
812250760Smelifaro		}
813250760Smelifaro	}
814250760Smelifaro
815250760Smelifaro	return (f_pos);
816250760Smelifaro}
817250760Smelifaro
818250760Smelifaro/*
81998943Sluigi * The main check routine for the firewall.
82098943Sluigi *
82198943Sluigi * All arguments are in args so we can modify them and return them
82298943Sluigi * back to the caller.
82398943Sluigi *
82498943Sluigi * Parameters:
82598943Sluigi *
82698943Sluigi *	args->m	(in/out) The packet; we set to NULL when/if we nuke it.
82798943Sluigi *		Starts with the IP header.
828200601Sluigi *	args->eh (in)	Mac header if present, NULL for layer3 packet.
829165738Sjulian *	args->L3offset	Number of bytes bypassed if we came from L2.
830165738Sjulian *			e.g. often sizeof(eh)  ** NOTYET **
831200601Sluigi *	args->oif	Outgoing interface, NULL if packet is incoming.
83298943Sluigi *		The incoming interface is in the mbuf. (in)
83398943Sluigi *	args->divert_rule (in/out)
83498943Sluigi *		Skip up to the first rule past this rule number;
83598943Sluigi *		upon return, non-zero port number for divert or tee.
83698943Sluigi *
83798943Sluigi *	args->rule	Pointer to the last matching rule (in/out)
83898943Sluigi *	args->next_hop	Socket we are forwarding to (out).
839225044Sbz *	args->next_hop6	IPv6 next hop we are forwarding to (out).
84098943Sluigi *	args->f_id	Addresses grabbed from the packet (out)
841201527Sluigi * 	args->rule.info	a cookie depending on rule action
84298943Sluigi *
84398943Sluigi * Return value:
84498943Sluigi *
845140224Sglebius *	IP_FW_PASS	the packet must be accepted
846140224Sglebius *	IP_FW_DENY	the packet must be dropped
847140224Sglebius *	IP_FW_DIVERT	divert packet, port in m_tag
848140224Sglebius *	IP_FW_TEE	tee packet, port in m_tag
849140224Sglebius *	IP_FW_DUMMYNET	to dummynet, pipe in args->cookie
850140224Sglebius *	IP_FW_NETGRAPH	into netgraph, cookie args->cookie
851201527Sluigi *		args->rule contains the matching rule,
852201527Sluigi *		args->rule.info has additional information.
85398943Sluigi *
85498943Sluigi */
855133920Sandreint
85698943Sluigiipfw_chk(struct ip_fw_args *args)
85798943Sluigi{
858183550Szec
85998943Sluigi	/*
860200601Sluigi	 * Local variables holding state while processing a packet:
86198943Sluigi	 *
86298943Sluigi	 * IMPORTANT NOTE: to speed up the processing of rules, there
86398943Sluigi	 * are some assumption on the values of the variables, which
86498943Sluigi	 * are documented here. Should you change them, please check
86598943Sluigi	 * the implementation of the various instructions to make sure
86698943Sluigi	 * that they still work.
867101978Sluigi	 *
86898943Sluigi	 * args->eh	The MAC header. It is non-null for a layer2
86998943Sluigi	 *	packet, it is NULL for a layer-3 packet.
870165738Sjulian	 * **notyet**
871165738Sjulian	 * args->L3offset Offset in the packet to the L3 (IP or equiv.) header.
87298943Sluigi	 *
87398943Sluigi	 * m | args->m	Pointer to the mbuf, as received from the caller.
87498943Sluigi	 *	It may change if ipfw_chk() does an m_pullup, or if it
87598943Sluigi	 *	consumes the packet because it calls send_reject().
87698943Sluigi	 *	XXX This has to change, so that ipfw_chk() never modifies
87798943Sluigi	 *	or consumes the buffer.
878165738Sjulian	 * ip	is the beginning of the ip(4 or 6) header.
879165738Sjulian	 *	Calculated by adding the L3offset to the start of data.
880165738Sjulian	 *	(Until we start using L3offset, the packet is
881165738Sjulian	 *	supposed to start with the ip header).
88298943Sluigi	 */
88398943Sluigi	struct mbuf *m = args->m;
88498943Sluigi	struct ip *ip = mtod(m, struct ip *);
88598943Sluigi
88698943Sluigi	/*
887130363Scsjp	 * For rules which contain uid/gid or jail constraints, cache
888130363Scsjp	 * a copy of the users credentials after the pcb lookup has been
889130363Scsjp	 * executed. This will speed up the processing of rules with
890130363Scsjp	 * these types of constraints, as well as decrease contention
891130363Scsjp	 * on pcb related locks.
892130363Scsjp	 */
893204591Sluigi#ifndef __FreeBSD__
894204591Sluigi	struct bsd_ucred ucred_cache;
895204591Sluigi#else
896194498Sbrooks	struct ucred *ucred_cache = NULL;
897204591Sluigi#endif
898194498Sbrooks	int ucred_lookup = 0;
899130363Scsjp
900130363Scsjp	/*
90198943Sluigi	 * oif | args->oif	If NULL, ipfw_chk has been called on the
902150636Smlaier	 *	inbound path (ether_input, ip_input).
90398943Sluigi	 *	If non-NULL, ipfw_chk has been called on the outbound path
90498943Sluigi	 *	(ether_output, ip_output).
90598943Sluigi	 */
90698943Sluigi	struct ifnet *oif = args->oif;
90798943Sluigi
908200855Sluigi	int f_pos = 0;		/* index of current rule in the array */
90998943Sluigi	int retval = 0;
91098943Sluigi
91198943Sluigi	/*
912147758Smlaier	 * hlen	The length of the IP header.
91398943Sluigi	 */
91498943Sluigi	u_int hlen = 0;		/* hlen >0 means we have an IP pkt */
91598943Sluigi
91698943Sluigi	/*
91798943Sluigi	 * offset	The offset of a fragment. offset != 0 means that
91898943Sluigi	 *	we have a fragment at this offset of an IPv4 packet.
91998943Sluigi	 *	offset == 0 means that (if this is an IPv4 packet)
92098943Sluigi	 *	this is the first or only fragment.
921225032Sbz	 *	For IPv6 offset|ip6f_mf == 0 means there is no Fragment Header
922225032Sbz	 *	or there is a single packet fragement (fragement header added
923225032Sbz	 *	without needed).  We will treat a single packet fragment as if
924225032Sbz	 *	there was no fragment header (or log/block depending on the
925225030Sbz	 *	V_fw_permit_single_frag6 sysctl setting).
92698943Sluigi	 */
92798943Sluigi	u_short offset = 0;
928225032Sbz	u_short ip6f_mf = 0;
92998943Sluigi
93098943Sluigi	/*
93198943Sluigi	 * Local copies of addresses. They are only valid if we have
93298943Sluigi	 * an IP packet.
93398943Sluigi	 *
93498943Sluigi	 * proto	The protocol. Set to 0 for non-ip packets,
93598943Sluigi	 *	or to the protocol read from the packet otherwise.
93698943Sluigi	 *	proto != 0 means that we have an IPv4 packet.
93798943Sluigi	 *
93898943Sluigi	 * src_port, dst_port	port numbers, in HOST format. Only
93998943Sluigi	 *	valid for TCP and UDP packets.
94098943Sluigi	 *
94198943Sluigi	 * src_ip, dst_ip	ip addresses, in NETWORK format.
94298943Sluigi	 *	Only valid for IPv4 packets.
94398943Sluigi	 */
944201122Sluigi	uint8_t proto;
945201122Sluigi	uint16_t src_port = 0, dst_port = 0;	/* NOTE: host format	*/
94698943Sluigi	struct in_addr src_ip, dst_ip;		/* NOTE: network format	*/
947201122Sluigi	uint16_t iplen=0;
948115750Skbyanc	int pktlen;
949201122Sluigi	uint16_t	etype = 0;	/* Host order stored ether type */
950145093Sbrooks
951145093Sbrooks	/*
952145093Sbrooks	 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
953145093Sbrooks	 * 	MATCH_NONE when checked and not matched (q = NULL),
954145093Sbrooks	 *	MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
955145093Sbrooks	 */
95698943Sluigi	int dyn_dir = MATCH_UNKNOWN;
95798943Sluigi	ipfw_dyn_rule *q = NULL;
958181803Sbz	struct ip_fw_chain *chain = &V_layer3_chain;
95998943Sluigi
96098943Sluigi	/*
961145093Sbrooks	 * We store in ulp a pointer to the upper layer protocol header.
962145093Sbrooks	 * In the ipv4 case this is easy to determine from the header,
963145093Sbrooks	 * but for ipv6 we might have some additional headers in the middle.
964145093Sbrooks	 * ulp is NULL if not found.
96598943Sluigi	 */
966145093Sbrooks	void *ulp = NULL;		/* upper layer protocol pointer. */
967205173Sluigi
968145246Sbrooks	/* XXX ipv6 variables */
969145246Sbrooks	int is_ipv6 = 0;
970205173Sluigi	uint8_t	icmp6_type = 0;
971205173Sluigi	uint16_t ext_hd = 0;	/* bits vector for extension header filtering */
972145246Sbrooks	/* end of ipv6 variables */
973205173Sluigi
974146894Smlaier	int is_ipv4 = 0;
97598943Sluigi
976200059Sluigi	int done = 0;		/* flag to exit the outer loop */
977200059Sluigi
978196423Sjulian	if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready))
979145093Sbrooks		return (IP_FW_PASS);	/* accept */
980145093Sbrooks
981187822Sluigi	dst_ip.s_addr = 0;		/* make sure it is initialized */
982200040Sluigi	src_ip.s_addr = 0;		/* make sure it is initialized */
983115750Skbyanc	pktlen = m->m_pkthdr.len;
984178888Sjulian	args->f_id.fib = M_GETFIB(m); /* note mbuf not altered) */
985145093Sbrooks	proto = args->f_id.proto = 0;	/* mark f_id invalid */
986149020Sbz		/* XXX 0 is a valid proto: IP/IPv6 Hop-by-Hop Option */
98798943Sluigi
988145093Sbrooks/*
989145093Sbrooks * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous,
990145093Sbrooks * then it sets p to point at the offset "len" in the mbuf. WARNING: the
991145093Sbrooks * pointer might become stale after other pullups (but we never use it
992145093Sbrooks * this way).
993145093Sbrooks */
994220796Sglebius#define PULLUP_TO(_len, p, T)	PULLUP_LEN(_len, p, sizeof(T))
995220796Sglebius#define PULLUP_LEN(_len, p, T)					\
996200601Sluigido {								\
997220796Sglebius	int x = (_len) + T;					\
998200601Sluigi	if ((m)->m_len < x) {					\
999200601Sluigi		args->m = m = m_pullup(m, x);			\
1000200601Sluigi		if (m == NULL)					\
1001200601Sluigi			goto pullup_failed;			\
1002200601Sluigi	}							\
1003200601Sluigi	p = (mtod(m, char *) + (_len));				\
1004145093Sbrooks} while (0)
100598943Sluigi
1006165738Sjulian	/*
1007165738Sjulian	 * if we have an ether header,
1008165738Sjulian	 */
1009165738Sjulian	if (args->eh)
1010165738Sjulian		etype = ntohs(args->eh->ether_type);
1011165738Sjulian
1012145246Sbrooks	/* Identify IP packets and fill up variables. */
1013145246Sbrooks	if (pktlen >= sizeof(struct ip6_hdr) &&
1014165738Sjulian	    (args->eh == NULL || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) {
1015165738Sjulian		struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
1016145246Sbrooks		is_ipv6 = 1;
1017145246Sbrooks		args->f_id.addr_type = 6;
1018145246Sbrooks		hlen = sizeof(struct ip6_hdr);
1019165738Sjulian		proto = ip6->ip6_nxt;
1020145246Sbrooks
1021145246Sbrooks		/* Search extension headers to find upper layer protocols */
1022225033Sbz		while (ulp == NULL && offset == 0) {
1023145246Sbrooks			switch (proto) {
1024145246Sbrooks			case IPPROTO_ICMPV6:
1025145246Sbrooks				PULLUP_TO(hlen, ulp, struct icmp6_hdr);
1026205173Sluigi				icmp6_type = ICMP6(ulp)->icmp6_type;
1027145246Sbrooks				break;
1028145246Sbrooks
1029145246Sbrooks			case IPPROTO_TCP:
1030145246Sbrooks				PULLUP_TO(hlen, ulp, struct tcphdr);
1031145246Sbrooks				dst_port = TCP(ulp)->th_dport;
1032145246Sbrooks				src_port = TCP(ulp)->th_sport;
1033205173Sluigi				/* save flags for dynamic rules */
1034205173Sluigi				args->f_id._flags = TCP(ulp)->th_flags;
1035145246Sbrooks				break;
1036145246Sbrooks
1037164258Sbz			case IPPROTO_SCTP:
1038164258Sbz				PULLUP_TO(hlen, ulp, struct sctphdr);
1039164258Sbz				src_port = SCTP(ulp)->src_port;
1040164258Sbz				dst_port = SCTP(ulp)->dest_port;
1041164258Sbz				break;
1042164258Sbz
1043145246Sbrooks			case IPPROTO_UDP:
1044145246Sbrooks				PULLUP_TO(hlen, ulp, struct udphdr);
1045145246Sbrooks				dst_port = UDP(ulp)->uh_dport;
1046145246Sbrooks				src_port = UDP(ulp)->uh_sport;
1047145246Sbrooks				break;
1048145246Sbrooks
1049149020Sbz			case IPPROTO_HOPOPTS:	/* RFC 2460 */
1050145246Sbrooks				PULLUP_TO(hlen, ulp, struct ip6_hbh);
1051145246Sbrooks				ext_hd |= EXT_HOPOPTS;
1052149020Sbz				hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1053145246Sbrooks				proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1054145246Sbrooks				ulp = NULL;
1055145246Sbrooks				break;
1056145246Sbrooks
1057149020Sbz			case IPPROTO_ROUTING:	/* RFC 2460 */
1058145246Sbrooks				PULLUP_TO(hlen, ulp, struct ip6_rthdr);
1059159857Sume				switch (((struct ip6_rthdr *)ulp)->ip6r_type) {
1060159857Sume				case 0:
1061169245Sbz					ext_hd |= EXT_RTHDR0;
1062159857Sume					break;
1063169245Sbz				case 2:
1064169245Sbz					ext_hd |= EXT_RTHDR2;
1065169245Sbz					break;
1066159857Sume				default:
1067225036Sbz					if (V_fw_verbose)
1068225036Sbz						printf("IPFW2: IPV6 - Unknown "
1069225036Sbz						    "Routing Header type(%d)\n",
1070225036Sbz						    ((struct ip6_rthdr *)
1071225036Sbz						    ulp)->ip6r_type);
1072181803Sbz					if (V_fw_deny_unknown_exthdrs)
1073149020Sbz					    return (IP_FW_DENY);
1074149020Sbz					break;
1075149020Sbz				}
1076145246Sbrooks				ext_hd |= EXT_ROUTING;
1077149020Sbz				hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3;
1078145246Sbrooks				proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt;
1079145246Sbrooks				ulp = NULL;
1080145246Sbrooks				break;
1081145246Sbrooks
1082149020Sbz			case IPPROTO_FRAGMENT:	/* RFC 2460 */
1083145246Sbrooks				PULLUP_TO(hlen, ulp, struct ip6_frag);
1084145246Sbrooks				ext_hd |= EXT_FRAGMENT;
1085145246Sbrooks				hlen += sizeof (struct ip6_frag);
1086145246Sbrooks				proto = ((struct ip6_frag *)ulp)->ip6f_nxt;
1087149020Sbz				offset = ((struct ip6_frag *)ulp)->ip6f_offlg &
1088149020Sbz					IP6F_OFF_MASK;
1089225032Sbz				ip6f_mf = ((struct ip6_frag *)ulp)->ip6f_offlg &
1090149020Sbz					IP6F_MORE_FRAG;
1091225030Sbz				if (V_fw_permit_single_frag6 == 0 &&
1092225032Sbz				    offset == 0 && ip6f_mf == 0) {
1093225036Sbz					if (V_fw_verbose)
1094225036Sbz						printf("IPFW2: IPV6 - Invalid "
1095225036Sbz						    "Fragment Header\n");
1096181803Sbz					if (V_fw_deny_unknown_exthdrs)
1097149020Sbz					    return (IP_FW_DENY);
1098149020Sbz					break;
1099149020Sbz				}
1100205173Sluigi				args->f_id.extra =
1101149020Sbz				    ntohl(((struct ip6_frag *)ulp)->ip6f_ident);
1102149020Sbz				ulp = NULL;
1103145246Sbrooks				break;
1104145246Sbrooks
1105149020Sbz			case IPPROTO_DSTOPTS:	/* RFC 2460 */
1106149020Sbz				PULLUP_TO(hlen, ulp, struct ip6_hbh);
1107149020Sbz				ext_hd |= EXT_DSTOPTS;
1108149020Sbz				hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1109149020Sbz				proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1110149020Sbz				ulp = NULL;
1111149020Sbz				break;
1112149020Sbz
1113149020Sbz			case IPPROTO_AH:	/* RFC 2402 */
1114145246Sbrooks				PULLUP_TO(hlen, ulp, struct ip6_ext);
1115145246Sbrooks				ext_hd |= EXT_AH;
1116149020Sbz				hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2;
1117145246Sbrooks				proto = ((struct ip6_ext *)ulp)->ip6e_nxt;
1118145246Sbrooks				ulp = NULL;
1119145246Sbrooks				break;
1120145246Sbrooks
1121149020Sbz			case IPPROTO_ESP:	/* RFC 2406 */
1122149020Sbz				PULLUP_TO(hlen, ulp, uint32_t);	/* SPI, Seq# */
1123149020Sbz				/* Anything past Seq# is variable length and
1124149020Sbz				 * data past this ext. header is encrypted. */
1125149020Sbz				ext_hd |= EXT_ESP;
1126149020Sbz				break;
1127149020Sbz
1128149020Sbz			case IPPROTO_NONE:	/* RFC 2460 */
1129174479Sdwmalone				/*
1130174479Sdwmalone				 * Packet ends here, and IPv6 header has
1131174479Sdwmalone				 * already been pulled up. If ip6e_len!=0
1132174479Sdwmalone				 * then octets must be ignored.
1133174479Sdwmalone				 */
1134174479Sdwmalone				ulp = ip; /* non-NULL to get out of loop. */
1135149020Sbz				break;
1136149020Sbz
1137146704Stanimura			case IPPROTO_OSPFIGP:
1138146704Stanimura				/* XXX OSPF header check? */
1139146704Stanimura				PULLUP_TO(hlen, ulp, struct ip6_ext);
1140146704Stanimura				break;
1141146704Stanimura
1142161767Sjhay			case IPPROTO_PIM:
1143161767Sjhay				/* XXX PIM header check? */
1144161767Sjhay				PULLUP_TO(hlen, ulp, struct pim);
1145161767Sjhay				break;
1146161767Sjhay
1147163069Sbz			case IPPROTO_CARP:
1148163069Sbz				PULLUP_TO(hlen, ulp, struct carp_header);
1149163069Sbz				if (((struct carp_header *)ulp)->carp_version !=
1150163069Sbz				    CARP_VERSION)
1151163069Sbz					return (IP_FW_DENY);
1152163069Sbz				if (((struct carp_header *)ulp)->carp_type !=
1153163069Sbz				    CARP_ADVERTISEMENT)
1154163069Sbz					return (IP_FW_DENY);
1155163069Sbz				break;
1156163069Sbz
1157159857Sume			case IPPROTO_IPV6:	/* RFC 2893 */
1158159857Sume				PULLUP_TO(hlen, ulp, struct ip6_hdr);
1159159857Sume				break;
1160159857Sume
1161159857Sume			case IPPROTO_IPV4:	/* RFC 2893 */
1162159857Sume				PULLUP_TO(hlen, ulp, struct ip);
1163159857Sume				break;
1164159857Sume
1165145246Sbrooks			default:
1166225036Sbz				if (V_fw_verbose)
1167225036Sbz					printf("IPFW2: IPV6 - Unknown "
1168225036Sbz					    "Extension Header(%d), ext_hd=%x\n",
1169225036Sbz					     proto, ext_hd);
1170181803Sbz				if (V_fw_deny_unknown_exthdrs)
1171149020Sbz				    return (IP_FW_DENY);
1172159857Sume				PULLUP_TO(hlen, ulp, struct ip6_ext);
1173145246Sbrooks				break;
1174145246Sbrooks			} /*switch */
1175145246Sbrooks		}
1176165738Sjulian		ip = mtod(m, struct ip *);
1177165738Sjulian		ip6 = (struct ip6_hdr *)ip;
1178165738Sjulian		args->f_id.src_ip6 = ip6->ip6_src;
1179165738Sjulian		args->f_id.dst_ip6 = ip6->ip6_dst;
1180145246Sbrooks		args->f_id.src_ip = 0;
1181145246Sbrooks		args->f_id.dst_ip = 0;
1182165738Sjulian		args->f_id.flow_id6 = ntohl(ip6->ip6_flow);
1183145246Sbrooks	} else if (pktlen >= sizeof(struct ip) &&
1184165738Sjulian	    (args->eh == NULL || etype == ETHERTYPE_IP) && ip->ip_v == 4) {
1185146894Smlaier	    	is_ipv4 = 1;
1186145093Sbrooks		hlen = ip->ip_hl << 2;
1187145093Sbrooks		args->f_id.addr_type = 4;
118898943Sluigi
1189145093Sbrooks		/*
1190145093Sbrooks		 * Collect parameters into local variables for faster matching.
1191145093Sbrooks		 */
1192145093Sbrooks		proto = ip->ip_p;
1193145093Sbrooks		src_ip = ip->ip_src;
1194145093Sbrooks		dst_ip = ip->ip_dst;
1195201527Sluigi		offset = ntohs(ip->ip_off) & IP_OFFMASK;
1196201527Sluigi		iplen = ntohs(ip->ip_len);
1197201122Sluigi		pktlen = iplen < pktlen ? iplen : pktlen;
119898943Sluigi
1199145093Sbrooks		if (offset == 0) {
1200145093Sbrooks			switch (proto) {
1201145093Sbrooks			case IPPROTO_TCP:
1202145093Sbrooks				PULLUP_TO(hlen, ulp, struct tcphdr);
1203145093Sbrooks				dst_port = TCP(ulp)->th_dport;
1204145093Sbrooks				src_port = TCP(ulp)->th_sport;
1205205173Sluigi				/* save flags for dynamic rules */
1206205173Sluigi				args->f_id._flags = TCP(ulp)->th_flags;
1207145093Sbrooks				break;
120898943Sluigi
1209220211Sae			case IPPROTO_SCTP:
1210220211Sae				PULLUP_TO(hlen, ulp, struct sctphdr);
1211220211Sae				src_port = SCTP(ulp)->src_port;
1212220211Sae				dst_port = SCTP(ulp)->dest_port;
1213220211Sae				break;
1214220211Sae
1215145093Sbrooks			case IPPROTO_UDP:
1216145093Sbrooks				PULLUP_TO(hlen, ulp, struct udphdr);
1217145093Sbrooks				dst_port = UDP(ulp)->uh_dport;
1218145093Sbrooks				src_port = UDP(ulp)->uh_sport;
1219145093Sbrooks				break;
122098943Sluigi
1221145093Sbrooks			case IPPROTO_ICMP:
1222145565Sbrooks				PULLUP_TO(hlen, ulp, struct icmphdr);
1223205173Sluigi				//args->f_id.flags = ICMP(ulp)->icmp_type;
1224145093Sbrooks				break;
122598943Sluigi
1226145093Sbrooks			default:
1227145093Sbrooks				break;
122898943Sluigi			}
1229145093Sbrooks		}
123098943Sluigi
1231165738Sjulian		ip = mtod(m, struct ip *);
1232145093Sbrooks		args->f_id.src_ip = ntohl(src_ip.s_addr);
1233145093Sbrooks		args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1234145093Sbrooks	}
123598943Sluigi#undef PULLUP_TO
1236145093Sbrooks	if (proto) { /* we may have port numbers, store them */
1237145093Sbrooks		args->f_id.proto = proto;
1238145093Sbrooks		args->f_id.src_port = src_port = ntohs(src_port);
1239145093Sbrooks		args->f_id.dst_port = dst_port = ntohs(dst_port);
124098943Sluigi	}
124198943Sluigi
1242138642Scsjp	IPFW_RLOCK(chain);
1243197952Sjulian	if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */
1244197952Sjulian		IPFW_RUNLOCK(chain);
1245197952Sjulian		return (IP_FW_PASS);	/* accept */
1246197952Sjulian	}
1247201527Sluigi	if (args->rule.slot) {
124898943Sluigi		/*
1249200629Sluigi		 * Packet has already been tagged as a result of a previous
1250200629Sluigi		 * match on rule args->rule aka args->rule_id (PIPE, QUEUE,
1251201527Sluigi		 * REASS, NETGRAPH, DIVERT/TEE...)
1252200855Sluigi		 * Validate the slot and continue from the next one
1253200855Sluigi		 * if still present, otherwise do a lookup.
125498943Sluigi		 */
1255201527Sluigi		f_pos = (args->rule.chain_id == chain->id) ?
1256201527Sluigi		    args->rule.slot :
1257201527Sluigi		    ipfw_find_rule(chain, args->rule.rulenum,
1258201527Sluigi			args->rule.rule_id);
125998943Sluigi	} else {
1260200855Sluigi		f_pos = 0;
126198943Sluigi	}
126298943Sluigi
126398943Sluigi	/*
126498943Sluigi	 * Now scan the rules, and parse microinstructions for each rule.
1265200059Sluigi	 * We have two nested loops and an inner switch. Sometimes we
1266200059Sluigi	 * need to break out of one or both loops, or re-enter one of
1267200059Sluigi	 * the loops with updated variables. Loop variables are:
1268200059Sluigi	 *
1269200855Sluigi	 *	f_pos (outer loop) points to the current rule.
1270200059Sluigi	 *		On output it points to the matching rule.
1271200059Sluigi	 *	done (outer loop) is used as a flag to break the loop.
1272200059Sluigi	 *	l (inner loop)	residual length of current rule.
1273200059Sluigi	 *		cmd points to the current microinstruction.
1274200059Sluigi	 *
1275200059Sluigi	 * We break the inner loop by setting l=0 and possibly
1276200059Sluigi	 * cmdlen=0 if we don't want to advance cmd.
1277200059Sluigi	 * We break the outer loop by setting done=1
1278200855Sluigi	 * We can restart the inner loop by setting l>0 and f_pos, f, cmd
1279200059Sluigi	 * as needed.
128098943Sluigi	 */
1281200855Sluigi	for (; f_pos < chain->n_rules; f_pos++) {
128298943Sluigi		ipfw_insn *cmd;
1283153374Sglebius		uint32_t tablearg = 0;
1284153374Sglebius		int l, cmdlen, skip_or; /* skip rest of OR block */
1285200855Sluigi		struct ip_fw *f;
128698943Sluigi
1287200855Sluigi		f = chain->map[f_pos];
1288181803Sbz		if (V_set_disable & (1 << f->set) )
1289101628Sluigi			continue;
1290101628Sluigi
129198943Sluigi		skip_or = 0;
129298943Sluigi		for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
129398943Sluigi		    l -= cmdlen, cmd += cmdlen) {
129499622Sluigi			int match;
129598943Sluigi
129698943Sluigi			/*
129798943Sluigi			 * check_body is a jump target used when we find a
129898943Sluigi			 * CHECK_STATE, and need to jump to the body of
129998943Sluigi			 * the target rule.
130098943Sluigi			 */
130198943Sluigi
1302200059Sluigi/* check_body: */
130398943Sluigi			cmdlen = F_LEN(cmd);
130498943Sluigi			/*
130598943Sluigi			 * An OR block (insn_1 || .. || insn_n) has the
130698943Sluigi			 * F_OR bit set in all but the last instruction.
130798943Sluigi			 * The first match will set "skip_or", and cause
130898943Sluigi			 * the following instructions to be skipped until
130998943Sluigi			 * past the one with the F_OR bit clear.
131098943Sluigi			 */
131198943Sluigi			if (skip_or) {		/* skip this instruction */
131298943Sluigi				if ((cmd->len & F_OR) == 0)
131398943Sluigi					skip_or = 0;	/* next one is good */
131498943Sluigi				continue;
131598943Sluigi			}
131699622Sluigi			match = 0; /* set to 1 if we succeed */
131799622Sluigi
131898943Sluigi			switch (cmd->opcode) {
131999622Sluigi			/*
132099622Sluigi			 * The first set of opcodes compares the packet's
132199622Sluigi			 * fields with some pattern, setting 'match' if a
132299622Sluigi			 * match is found. At the end of the loop there is
132399622Sluigi			 * logic to deal with F_NOT and F_OR flags associated
132499622Sluigi			 * with the opcode.
132599622Sluigi			 */
132698943Sluigi			case O_NOP:
132799622Sluigi				match = 1;
132899622Sluigi				break;
132998943Sluigi
133098943Sluigi			case O_FORWARD_MAC:
133198943Sluigi				printf("ipfw: opcode %d unimplemented\n",
133298943Sluigi				    cmd->opcode);
133399622Sluigi				break;
133498943Sluigi
133598943Sluigi			case O_GID:
133698943Sluigi			case O_UID:
1337133600Scsjp			case O_JAIL:
133898943Sluigi				/*
133998943Sluigi				 * We only check offset == 0 && proto != 0,
1340145246Sbrooks				 * as this ensures that we have a
134198943Sluigi				 * packet with the ports info.
134298943Sluigi				 */
1343223073Sae				if (offset != 0)
134499622Sluigi					break;
1345122265Ssam				if (proto == IPPROTO_TCP ||
1346122265Ssam				    proto == IPPROTO_UDP)
1347122265Ssam					match = check_uidgid(
1348122265Ssam						    (ipfw_insn_u32 *)cmd,
1349223073Sae						    args, &ucred_lookup,
1350204591Sluigi#ifdef __FreeBSD__
1351223073Sae						    &ucred_cache);
1352204591Sluigi#else
1353223073Sae						    (void *)&ucred_cache);
1354204591Sluigi#endif
135599622Sluigi				break;
135698943Sluigi
135798943Sluigi			case O_RECV:
135899622Sluigi				match = iface_match(m->m_pkthdr.rcvif,
1359234597Smelifaro				    (ipfw_insn_if *)cmd, chain, &tablearg);
136099622Sluigi				break;
136198943Sluigi
136298943Sluigi			case O_XMIT:
1363234597Smelifaro				match = iface_match(oif, (ipfw_insn_if *)cmd,
1364234597Smelifaro				    chain, &tablearg);
136599622Sluigi				break;
136698943Sluigi
136798943Sluigi			case O_VIA:
136899622Sluigi				match = iface_match(oif ? oif :
1369234597Smelifaro				    m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd,
1370234597Smelifaro				    chain, &tablearg);
137199622Sluigi				break;
137298943Sluigi
137398943Sluigi			case O_MACADDR2:
137498943Sluigi				if (args->eh != NULL) {	/* have MAC header */
137598943Sluigi					u_int32_t *want = (u_int32_t *)
137698943Sluigi						((ipfw_insn_mac *)cmd)->addr;
137798943Sluigi					u_int32_t *mask = (u_int32_t *)
137898943Sluigi						((ipfw_insn_mac *)cmd)->mask;
137998943Sluigi					u_int32_t *hdr = (u_int32_t *)args->eh;
138098943Sluigi
138199622Sluigi					match =
138299622Sluigi					    ( want[0] == (hdr[0] & mask[0]) &&
138399622Sluigi					      want[1] == (hdr[1] & mask[1]) &&
138499622Sluigi					      want[2] == (hdr[2] & mask[2]) );
138598943Sluigi				}
138699622Sluigi				break;
138798943Sluigi
138898943Sluigi			case O_MAC_TYPE:
138998943Sluigi				if (args->eh != NULL) {
139098943Sluigi					u_int16_t *p =
139198943Sluigi					    ((ipfw_insn_u16 *)cmd)->ports;
139298943Sluigi					int i;
139398943Sluigi
139499622Sluigi					for (i = cmdlen - 1; !match && i>0;
139599622Sluigi					    i--, p += 2)
1396165738Sjulian						match = (etype >= p[0] &&
1397165738Sjulian						    etype <= p[1]);
139898943Sluigi				}
139999622Sluigi				break;
140098943Sluigi
140198943Sluigi			case O_FRAG:
1402145246Sbrooks				match = (offset != 0);
140399622Sluigi				break;
140498943Sluigi
140598943Sluigi			case O_IN:	/* "out" is "not in" */
140699622Sluigi				match = (oif == NULL);
140799622Sluigi				break;
140898943Sluigi
140998943Sluigi			case O_LAYER2:
141099622Sluigi				match = (args->eh != NULL);
141199622Sluigi				break;
141298943Sluigi
1413136073Sgreen			case O_DIVERTED:
1414201527Sluigi			    {
1415201527Sluigi				/* For diverted packets, args->rule.info
1416201527Sluigi				 * contains the divert port (in host format)
1417201527Sluigi				 * reason and direction.
1418210120Sluigi				 */
1419201527Sluigi				uint32_t i = args->rule.info;
1420201527Sluigi				match = (i&IPFW_IS_MASK) == IPFW_IS_DIVERT &&
1421201527Sluigi				    cmd->arg1 & ((i & IPFW_INFO_IN) ? 1 : 2);
1422201527Sluigi			    }
1423136073Sgreen				break;
1424136073Sgreen
142598943Sluigi			case O_PROTO:
142698943Sluigi				/*
142798943Sluigi				 * We do not allow an arg of 0 so the
142898943Sluigi				 * check of "proto" only suffices.
142998943Sluigi				 */
143099622Sluigi				match = (proto == cmd->arg1);
143199622Sluigi				break;
143298943Sluigi
143398943Sluigi			case O_IP_SRC:
1434147758Smlaier				match = is_ipv4 &&
1435147758Smlaier				    (((ipfw_insn_ip *)cmd)->addr.s_addr ==
143699622Sluigi				    src_ip.s_addr);
143799622Sluigi				break;
1438105775Smaxim
1439130281Sru			case O_IP_SRC_LOOKUP:
1440130281Sru			case O_IP_DST_LOOKUP:
1441147758Smlaier				if (is_ipv4) {
1442201122Sluigi				    uint32_t key =
1443130281Sru					(cmd->opcode == O_IP_DST_LOOKUP) ?
1444130281Sru					    dst_ip.s_addr : src_ip.s_addr;
1445187822Sluigi				    uint32_t v = 0;
1446130281Sru
1447200567Sluigi				    if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) {
1448201122Sluigi					/* generic lookup. The key must be
1449201122Sluigi					 * in 32bit big-endian format.
1450201122Sluigi					 */
1451200567Sluigi					v = ((ipfw_insn_u32 *)cmd)->d[1];
1452200567Sluigi					if (v == 0)
1453201122Sluigi					    key = dst_ip.s_addr;
1454200567Sluigi					else if (v == 1)
1455201122Sluigi					    key = src_ip.s_addr;
1456205173Sluigi					else if (v == 6) /* dscp */
1457205173Sluigi					    key = (ip->ip_tos >> 2) & 0x3f;
1458200567Sluigi					else if (offset != 0)
1459200567Sluigi					    break;
1460200567Sluigi					else if (proto != IPPROTO_TCP &&
1461200567Sluigi						proto != IPPROTO_UDP)
1462200567Sluigi					    break;
1463200567Sluigi					else if (v == 2)
1464201122Sluigi					    key = htonl(dst_port);
1465200567Sluigi					else if (v == 3)
1466201150Sluigi					    key = htonl(src_port);
1467200567Sluigi					else if (v == 4 || v == 5) {
1468200567Sluigi					    check_uidgid(
1469200567Sluigi						(ipfw_insn_u32 *)cmd,
1470223073Sae						args, &ucred_lookup,
1471204591Sluigi#ifdef __FreeBSD__
1472223073Sae						&ucred_cache);
1473200567Sluigi					    if (v == 4 /* O_UID */)
1474201122Sluigi						key = ucred_cache->cr_uid;
1475200567Sluigi					    else if (v == 5 /* O_JAIL */)
1476201122Sluigi						key = ucred_cache->cr_prison->pr_id;
1477204591Sluigi#else /* !__FreeBSD__ */
1478223073Sae						(void *)&ucred_cache);
1479204591Sluigi					    if (v ==4 /* O_UID */)
1480204591Sluigi						key = ucred_cache.uid;
1481204591Sluigi					    else if (v == 5 /* O_JAIL */)
1482204591Sluigi						key = ucred_cache.xid;
1483204591Sluigi#endif /* !__FreeBSD__ */
1484201122Sluigi					    key = htonl(key);
1485200567Sluigi					} else
1486200567Sluigi					    break;
1487200567Sluigi				    }
1488201122Sluigi				    match = ipfw_lookup_table(chain,
1489201122Sluigi					cmd->arg1, key, &v);
1490130281Sru				    if (!match)
1491130281Sru					break;
1492130281Sru				    if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
1493130281Sru					match =
1494130281Sru					    ((ipfw_insn_u32 *)cmd)->d[0] == v;
1495153374Sglebius				    else
1496153374Sglebius					tablearg = v;
1497234597Smelifaro				} else if (is_ipv6) {
1498234597Smelifaro					uint32_t v = 0;
1499234597Smelifaro					void *pkey = (cmd->opcode == O_IP_DST_LOOKUP) ?
1500234597Smelifaro						&args->f_id.dst_ip6: &args->f_id.src_ip6;
1501234597Smelifaro					match = ipfw_lookup_table_extended(chain,
1502234597Smelifaro							cmd->arg1, pkey, &v,
1503234597Smelifaro							IPFW_TABLE_CIDR);
1504234597Smelifaro					if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
1505234597Smelifaro						match = ((ipfw_insn_u32 *)cmd)->d[0] == v;
1506234597Smelifaro					if (match)
1507234597Smelifaro						tablearg = v;
1508130281Sru				}
1509130281Sru				break;
1510130281Sru
151198943Sluigi			case O_IP_SRC_MASK:
1512117327Sluigi			case O_IP_DST_MASK:
1513147758Smlaier				if (is_ipv4) {
1514117327Sluigi				    uint32_t a =
1515117327Sluigi					(cmd->opcode == O_IP_DST_MASK) ?
1516117327Sluigi					    dst_ip.s_addr : src_ip.s_addr;
1517117327Sluigi				    uint32_t *p = ((ipfw_insn_u32 *)cmd)->d;
1518117327Sluigi				    int i = cmdlen-1;
1519117327Sluigi
1520117327Sluigi				    for (; !match && i>0; i-= 2, p+= 2)
1521117327Sluigi					match = (p[0] == (a & p[1]));
1522117327Sluigi				}
152399622Sluigi				break;
152498943Sluigi
152598943Sluigi			case O_IP_SRC_ME:
1526147758Smlaier				if (is_ipv4) {
152799622Sluigi					struct ifnet *tif;
152898943Sluigi
152999622Sluigi					INADDR_TO_IFP(src_ip, tif);
153099622Sluigi					match = (tif != NULL);
1531202459Sume					break;
153299622Sluigi				}
1533204591Sluigi#ifdef INET6
1534202459Sume				/* FALLTHROUGH */
1535202459Sume			case O_IP6_SRC_ME:
1536204591Sluigi				match= is_ipv6 && search_ip6_addr_net(&args->f_id.src_ip6);
1537202459Sume#endif
153899622Sluigi				break;
1539105775Smaxim
154098943Sluigi			case O_IP_DST_SET:
154198943Sluigi			case O_IP_SRC_SET:
1542147758Smlaier				if (is_ipv4) {
154399622Sluigi					u_int32_t *d = (u_int32_t *)(cmd+1);
154499622Sluigi					u_int32_t addr =
154599622Sluigi					    cmd->opcode == O_IP_DST_SET ?
1546105886Sluigi						args->f_id.dst_ip :
1547105886Sluigi						args->f_id.src_ip;
154898943Sluigi
154999622Sluigi					    if (addr < d[0])
155099622Sluigi						    break;
155199622Sluigi					    addr -= d[0]; /* subtract base */
155299622Sluigi					    match = (addr < cmd->arg1) &&
155399622Sluigi						( d[ 1 + (addr>>5)] &
155499622Sluigi						  (1<<(addr & 0x1f)) );
155599622Sluigi				}
155699622Sluigi				break;
155798943Sluigi
155898943Sluigi			case O_IP_DST:
1559147758Smlaier				match = is_ipv4 &&
1560147758Smlaier				    (((ipfw_insn_ip *)cmd)->addr.s_addr ==
156199622Sluigi				    dst_ip.s_addr);
156299622Sluigi				break;
156398943Sluigi
156498943Sluigi			case O_IP_DST_ME:
1565147758Smlaier				if (is_ipv4) {
156699622Sluigi					struct ifnet *tif;
156799622Sluigi
156899622Sluigi					INADDR_TO_IFP(dst_ip, tif);
156999622Sluigi					match = (tif != NULL);
1570202459Sume					break;
157199622Sluigi				}
1572204591Sluigi#ifdef INET6
1573202459Sume				/* FALLTHROUGH */
1574202459Sume			case O_IP6_DST_ME:
1575204591Sluigi				match= is_ipv6 && search_ip6_addr_net(&args->f_id.dst_ip6);
1576202459Sume#endif
157799622Sluigi				break;
1578105775Smaxim
1579204591Sluigi
158098943Sluigi			case O_IP_SRCPORT:
158198943Sluigi			case O_IP_DSTPORT:
158298943Sluigi				/*
158398943Sluigi				 * offset == 0 && proto != 0 is enough
1584145246Sbrooks				 * to guarantee that we have a
158598943Sluigi				 * packet with port info.
158698943Sluigi				 */
158799622Sluigi				if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP)
158899622Sluigi				    && offset == 0) {
158999622Sluigi					u_int16_t x =
159098943Sluigi					    (cmd->opcode == O_IP_SRCPORT) ?
159199622Sluigi						src_port : dst_port ;
159298943Sluigi					u_int16_t *p =
159398943Sluigi					    ((ipfw_insn_u16 *)cmd)->ports;
159498943Sluigi					int i;
159598943Sluigi
159699622Sluigi					for (i = cmdlen - 1; !match && i>0;
159799622Sluigi					    i--, p += 2)
159899622Sluigi						match = (x>=p[0] && x<=p[1]);
159998943Sluigi				}
160099622Sluigi				break;
160198943Sluigi
160298943Sluigi			case O_ICMPTYPE:
160399622Sluigi				match = (offset == 0 && proto==IPPROTO_ICMP &&
1604145093Sbrooks				    icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) );
160599622Sluigi				break;
160698943Sluigi
1607145266Sphk#ifdef INET6
1608145246Sbrooks			case O_ICMP6TYPE:
1609145246Sbrooks				match = is_ipv6 && offset == 0 &&
1610145246Sbrooks				    proto==IPPROTO_ICMPV6 &&
1611145246Sbrooks				    icmp6type_match(
1612145246Sbrooks					ICMP6(ulp)->icmp6_type,
1613145246Sbrooks					(ipfw_insn_u32 *)cmd);
1614145246Sbrooks				break;
1615145266Sphk#endif /* INET6 */
1616145246Sbrooks
161798943Sluigi			case O_IPOPT:
1618147758Smlaier				match = (is_ipv4 &&
1619165738Sjulian				    ipopts_match(ip, cmd) );
162099622Sluigi				break;
162198943Sluigi
162298943Sluigi			case O_IPVER:
1623147758Smlaier				match = (is_ipv4 &&
1624165738Sjulian				    cmd->arg1 == ip->ip_v);
162599622Sluigi				break;
162698943Sluigi
1627116690Sluigi			case O_IPID:
1628116690Sluigi			case O_IPLEN:
162998943Sluigi			case O_IPTTL:
1630147758Smlaier				if (is_ipv4) {	/* only for IP packets */
1631116690Sluigi				    uint16_t x;
1632116690Sluigi				    uint16_t *p;
1633116690Sluigi				    int i;
163498943Sluigi
1635116690Sluigi				    if (cmd->opcode == O_IPLEN)
1636201122Sluigi					x = iplen;
1637116690Sluigi				    else if (cmd->opcode == O_IPTTL)
1638165738Sjulian					x = ip->ip_ttl;
1639116690Sluigi				    else /* must be IPID */
1640165738Sjulian					x = ntohs(ip->ip_id);
1641116690Sluigi				    if (cmdlen == 1) {
1642116690Sluigi					match = (cmd->arg1 == x);
1643116690Sluigi					break;
1644116690Sluigi				    }
1645116690Sluigi				    /* otherwise we have ranges */
1646116690Sluigi				    p = ((ipfw_insn_u16 *)cmd)->ports;
1647116690Sluigi				    i = cmdlen - 1;
1648116690Sluigi				    for (; !match && i>0; i--, p += 2)
1649116690Sluigi					match = (x >= p[0] && x <= p[1]);
1650116690Sluigi				}
165199622Sluigi				break;
165298943Sluigi
165399475Sluigi			case O_IPPRECEDENCE:
1654147758Smlaier				match = (is_ipv4 &&
1655165738Sjulian				    (cmd->arg1 == (ip->ip_tos & 0xe0)) );
165699622Sluigi				break;
165799475Sluigi
165898943Sluigi			case O_IPTOS:
1659147758Smlaier				match = (is_ipv4 &&
1660165738Sjulian				    flags_match(cmd, ip->ip_tos));
166199622Sluigi				break;
166298943Sluigi
1663250762Smelifaro			case O_DSCP:
1664250762Smelifaro			    {
1665250762Smelifaro				uint32_t *p;
1666250762Smelifaro				uint16_t x;
1667250762Smelifaro
1668250762Smelifaro				p = ((ipfw_insn_u32 *)cmd)->d;
1669250762Smelifaro
1670250762Smelifaro				if (is_ipv4)
1671250762Smelifaro					x = ip->ip_tos >> 2;
1672250762Smelifaro				else if (is_ipv6) {
1673250762Smelifaro					uint8_t *v;
1674250762Smelifaro					v = &((struct ip6_hdr *)ip)->ip6_vfc;
1675250762Smelifaro					x = (*v & 0x0F) << 2;
1676250762Smelifaro					v++;
1677250762Smelifaro					x |= *v >> 6;
1678250762Smelifaro				} else
1679250762Smelifaro					break;
1680250762Smelifaro
1681250762Smelifaro				/* DSCP bitmask is stored as low_u32 high_u32 */
1682250762Smelifaro				if (x > 32)
1683250762Smelifaro					match = *(p + 1) & (1 << (x - 32));
1684250762Smelifaro				else
1685250762Smelifaro					match = *p & (1 << x);
1686250762Smelifaro			    }
1687250762Smelifaro				break;
1688250762Smelifaro
1689136075Sgreen			case O_TCPDATALEN:
1690136075Sgreen				if (proto == IPPROTO_TCP && offset == 0) {
1691136075Sgreen				    struct tcphdr *tcp;
1692136075Sgreen				    uint16_t x;
1693136075Sgreen				    uint16_t *p;
1694136075Sgreen				    int i;
1695136075Sgreen
1696145093Sbrooks				    tcp = TCP(ulp);
1697201122Sluigi				    x = iplen -
1698136075Sgreen					((ip->ip_hl + tcp->th_off) << 2);
1699136075Sgreen				    if (cmdlen == 1) {
1700136075Sgreen					match = (cmd->arg1 == x);
1701136075Sgreen					break;
1702136075Sgreen				    }
1703136075Sgreen				    /* otherwise we have ranges */
1704136075Sgreen				    p = ((ipfw_insn_u16 *)cmd)->ports;
1705136075Sgreen				    i = cmdlen - 1;
1706136075Sgreen				    for (; !match && i>0; i--, p += 2)
1707136075Sgreen					match = (x >= p[0] && x <= p[1]);
1708136075Sgreen				}
1709136075Sgreen				break;
1710136075Sgreen
171198943Sluigi			case O_TCPFLAGS:
171299622Sluigi				match = (proto == IPPROTO_TCP && offset == 0 &&
1713145093Sbrooks				    flags_match(cmd, TCP(ulp)->th_flags));
171499622Sluigi				break;
171598943Sluigi
171698943Sluigi			case O_TCPOPTS:
1717220796Sglebius				PULLUP_LEN(hlen, ulp, (TCP(ulp)->th_off << 2));
171899622Sluigi				match = (proto == IPPROTO_TCP && offset == 0 &&
1719145093Sbrooks				    tcpopts_match(TCP(ulp), cmd));
172099622Sluigi				break;
172198943Sluigi
172298943Sluigi			case O_TCPSEQ:
172399622Sluigi				match = (proto == IPPROTO_TCP && offset == 0 &&
172499622Sluigi				    ((ipfw_insn_u32 *)cmd)->d[0] ==
1725145093Sbrooks					TCP(ulp)->th_seq);
172699622Sluigi				break;
172798943Sluigi
172898943Sluigi			case O_TCPACK:
172999622Sluigi				match = (proto == IPPROTO_TCP && offset == 0 &&
173099622Sluigi				    ((ipfw_insn_u32 *)cmd)->d[0] ==
1731145093Sbrooks					TCP(ulp)->th_ack);
173299622Sluigi				break;
173398943Sluigi
173498943Sluigi			case O_TCPWIN:
1735234278Sglebius				if (proto == IPPROTO_TCP && offset == 0) {
1736234278Sglebius				    uint16_t x;
1737234278Sglebius				    uint16_t *p;
1738234278Sglebius				    int i;
1739234278Sglebius
1740234278Sglebius				    x = ntohs(TCP(ulp)->th_win);
1741234278Sglebius				    if (cmdlen == 1) {
1742234278Sglebius					match = (cmd->arg1 == x);
1743234278Sglebius					break;
1744234278Sglebius				    }
1745234278Sglebius				    /* Otherwise we have ranges. */
1746234278Sglebius				    p = ((ipfw_insn_u16 *)cmd)->ports;
1747234278Sglebius				    i = cmdlen - 1;
1748234278Sglebius				    for (; !match && i > 0; i--, p += 2)
1749234278Sglebius					match = (x >= p[0] && x <= p[1]);
1750234278Sglebius				}
175199622Sluigi				break;
175298943Sluigi
175398943Sluigi			case O_ESTAB:
175498943Sluigi				/* reject packets which have SYN only */
175599622Sluigi				/* XXX should i also check for TH_ACK ? */
175699622Sluigi				match = (proto == IPPROTO_TCP && offset == 0 &&
1757145093Sbrooks				    (TCP(ulp)->th_flags &
175899622Sluigi				     (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
175999622Sluigi				break;
176098943Sluigi
1761136071Sgreen			case O_ALTQ: {
1762171173Smlaier				struct pf_mtag *at;
1763136071Sgreen				ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
1764136071Sgreen
1765136071Sgreen				match = 1;
1766171173Smlaier				at = pf_find_mtag(m);
1767171173Smlaier				if (at != NULL && at->qid != 0)
1768146962Sgreen					break;
1769171173Smlaier				at = pf_get_mtag(m);
1770171173Smlaier				if (at == NULL) {
1771136071Sgreen					/*
1772136071Sgreen					 * Let the packet fall back to the
1773136071Sgreen					 * default ALTQ.
1774136071Sgreen					 */
1775136071Sgreen					break;
1776136071Sgreen				}
1777136071Sgreen				at->qid = altq->qid;
1778136071Sgreen				at->hdr = ip;
1779136071Sgreen				break;
1780136071Sgreen			}
1781136071Sgreen
178298943Sluigi			case O_LOG:
1783200654Sluigi				ipfw_log(f, hlen, args, m,
1784225032Sbz				    oif, offset | ip6f_mf, tablearg, ip);
178599622Sluigi				match = 1;
178699622Sluigi				break;
178798943Sluigi
178899475Sluigi			case O_PROB:
178999622Sluigi				match = (random()<((ipfw_insn_u32 *)cmd)->d[0]);
179099622Sluigi				break;
179198943Sluigi
1792112250Scjc			case O_VERREVPATH:
1793112250Scjc				/* Outgoing packets automatically pass/match */
1794145246Sbrooks				match = ((oif != NULL) ||
1795116763Sluigi				    (m->m_pkthdr.rcvif == NULL) ||
1796145266Sphk				    (
1797145266Sphk#ifdef INET6
1798145266Sphk				    is_ipv6 ?
1799147418Smlaier					verify_path6(&(args->f_id.src_ip6),
1800232292Sbz					    m->m_pkthdr.rcvif, args->f_id.fib) :
1801145266Sphk#endif
1802178888Sjulian				    verify_path(src_ip, m->m_pkthdr.rcvif,
1803178888Sjulian				        args->f_id.fib)));
1804112250Scjc				break;
1805112250Scjc
1806128575Sandre			case O_VERSRCREACH:
1807128575Sandre				/* Outgoing packets automatically pass/match */
1808133485Sandre				match = (hlen > 0 && ((oif != NULL) ||
1809147418Smlaier#ifdef INET6
1810147418Smlaier				    is_ipv6 ?
1811147418Smlaier				        verify_path6(&(args->f_id.src_ip6),
1812232292Sbz				            NULL, args->f_id.fib) :
1813147418Smlaier#endif
1814178888Sjulian				    verify_path(src_ip, NULL, args->f_id.fib)));
1815128575Sandre				break;
1816128575Sandre
1817133387Sandre			case O_ANTISPOOF:
1818133387Sandre				/* Outgoing packets automatically pass/match */
1819133387Sandre				if (oif == NULL && hlen > 0 &&
1820147418Smlaier				    (  (is_ipv4 && in_localaddr(src_ip))
1821147418Smlaier#ifdef INET6
1822147418Smlaier				    || (is_ipv6 &&
1823147418Smlaier				        in6_localaddr(&(args->f_id.src_ip6)))
1824147418Smlaier#endif
1825147418Smlaier				    ))
1826147418Smlaier					match =
1827147418Smlaier#ifdef INET6
1828147418Smlaier					    is_ipv6 ? verify_path6(
1829147418Smlaier					        &(args->f_id.src_ip6),
1830232292Sbz					        m->m_pkthdr.rcvif,
1831232292Sbz						args->f_id.fib) :
1832147418Smlaier#endif
1833147418Smlaier					    verify_path(src_ip,
1834178888Sjulian					    	m->m_pkthdr.rcvif,
1835178888Sjulian					        args->f_id.fib);
1836133387Sandre				else
1837133387Sandre					match = 1;
1838133387Sandre				break;
1839133387Sandre
1840117241Sluigi			case O_IPSEC:
1841171167Sgnn#ifdef IPSEC
1842117241Sluigi				match = (m_tag_find(m,
1843117241Sluigi				    PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL);
1844117241Sluigi#endif
1845117241Sluigi				/* otherwise no match */
1846117241Sluigi				break;
1847117241Sluigi
1848150122Sbz#ifdef INET6
1849145246Sbrooks			case O_IP6_SRC:
1850145246Sbrooks				match = is_ipv6 &&
1851145246Sbrooks				    IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6,
1852145246Sbrooks				    &((ipfw_insn_ip6 *)cmd)->addr6);
1853145246Sbrooks				break;
1854145246Sbrooks
1855145246Sbrooks			case O_IP6_DST:
1856145246Sbrooks				match = is_ipv6 &&
1857145246Sbrooks				IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6,
1858145246Sbrooks				    &((ipfw_insn_ip6 *)cmd)->addr6);
1859145246Sbrooks				break;
1860145246Sbrooks			case O_IP6_SRC_MASK:
1861145246Sbrooks			case O_IP6_DST_MASK:
1862145246Sbrooks				if (is_ipv6) {
1863162351Sjhay					int i = cmdlen - 1;
1864162351Sjhay					struct in6_addr p;
1865162351Sjhay					struct in6_addr *d =
1866162351Sjhay					    &((ipfw_insn_ip6 *)cmd)->addr6;
1867145246Sbrooks
1868162351Sjhay					for (; !match && i > 0; d += 2,
1869162351Sjhay					    i -= F_INSN_SIZE(struct in6_addr)
1870162351Sjhay					    * 2) {
1871162351Sjhay						p = (cmd->opcode ==
1872162351Sjhay						    O_IP6_SRC_MASK) ?
1873162351Sjhay						    args->f_id.src_ip6:
1874162351Sjhay						    args->f_id.dst_ip6;
1875162351Sjhay						APPLY_MASK(&p, &d[1]);
1876162351Sjhay						match =
1877162351Sjhay						    IN6_ARE_ADDR_EQUAL(&d[0],
1878162351Sjhay						    &p);
1879162351Sjhay					}
1880145246Sbrooks				}
1881145246Sbrooks				break;
1882145246Sbrooks
1883145246Sbrooks			case O_FLOW6ID:
1884145246Sbrooks				match = is_ipv6 &&
1885145246Sbrooks				    flow6id_match(args->f_id.flow_id6,
1886145246Sbrooks				    (ipfw_insn_u32 *) cmd);
1887145246Sbrooks				break;
1888145246Sbrooks
1889145246Sbrooks			case O_EXT_HDR:
1890145246Sbrooks				match = is_ipv6 &&
1891145246Sbrooks				    (ext_hd & ((ipfw_insn *) cmd)->arg1);
1892145246Sbrooks				break;
1893145246Sbrooks
1894145246Sbrooks			case O_IP6:
1895145246Sbrooks				match = is_ipv6;
1896145246Sbrooks				break;
1897145266Sphk#endif
1898145246Sbrooks
1899146894Smlaier			case O_IP4:
1900146894Smlaier				match = is_ipv4;
1901146894Smlaier				break;
1902146894Smlaier
1903159636Soleg			case O_TAG: {
1904201527Sluigi				struct m_tag *mtag;
1905250760Smelifaro				uint32_t tag = IP_FW_ARG_TABLEARG(cmd->arg1);
1906159636Soleg
1907158879Soleg				/* Packet is already tagged with this tag? */
1908159636Soleg				mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL);
1909159636Soleg
1910158879Soleg				/* We have `untag' action when F_NOT flag is
1911158879Soleg				 * present. And we must remove this mtag from
1912158879Soleg				 * mbuf and reset `match' to zero (`match' will
1913158879Soleg				 * be inversed later).
1914158879Soleg				 * Otherwise we should allocate new mtag and
1915158879Soleg				 * push it into mbuf.
1916158879Soleg				 */
1917158879Soleg				if (cmd->len & F_NOT) { /* `untag' action */
1918158879Soleg					if (mtag != NULL)
1919158879Soleg						m_tag_delete(m, mtag);
1920201527Sluigi					match = 0;
1921220568Sae				} else {
1922220568Sae					if (mtag == NULL) {
1923220568Sae						mtag = m_tag_alloc( MTAG_IPFW,
1924220568Sae						    tag, 0, M_NOWAIT);
1925220568Sae						if (mtag != NULL)
1926220568Sae							m_tag_prepend(m, mtag);
1927220568Sae					}
1928201527Sluigi					match = 1;
1929158879Soleg				}
1930158879Soleg				break;
1931159636Soleg			}
1932158879Soleg
1933178888Sjulian			case O_FIB: /* try match the specified fib */
1934178888Sjulian				if (args->f_id.fib == cmd->arg1)
1935178888Sjulian					match = 1;
1936178888Sjulian				break;
1937178888Sjulian
1938215179Sluigi			case O_SOCKARG:	{
1939215179Sluigi				struct inpcb *inp = args->inp;
1940215179Sluigi				struct inpcbinfo *pi;
1941215179Sluigi
1942215179Sluigi				if (is_ipv6) /* XXX can we remove this ? */
1943215179Sluigi					break;
1944215179Sluigi
1945215179Sluigi				if (proto == IPPROTO_TCP)
1946215179Sluigi					pi = &V_tcbinfo;
1947215179Sluigi				else if (proto == IPPROTO_UDP)
1948215179Sluigi					pi = &V_udbinfo;
1949215179Sluigi				else
1950215179Sluigi					break;
1951215179Sluigi
1952222488Srwatson				/*
1953222488Srwatson				 * XXXRW: so_user_cookie should almost
1954222488Srwatson				 * certainly be inp_user_cookie?
1955222488Srwatson				 */
1956222488Srwatson
1957215179Sluigi				/* For incomming packet, lookup up the
1958215179Sluigi				inpcb using the src/dest ip/port tuple */
1959215179Sluigi				if (inp == NULL) {
1960222488Srwatson					inp = in_pcblookup(pi,
1961215179Sluigi						src_ip, htons(src_port),
1962215179Sluigi						dst_ip, htons(dst_port),
1963222488Srwatson						INPLOOKUP_RLOCKPCB, NULL);
1964222488Srwatson					if (inp != NULL) {
1965222488Srwatson						tablearg =
1966222488Srwatson						    inp->inp_socket->so_user_cookie;
1967222488Srwatson						if (tablearg)
1968222488Srwatson							match = 1;
1969222488Srwatson						INP_RUNLOCK(inp);
1970222488Srwatson					}
1971222488Srwatson				} else {
1972222488Srwatson					if (inp->inp_socket) {
1973222488Srwatson						tablearg =
1974222488Srwatson						    inp->inp_socket->so_user_cookie;
1975222488Srwatson						if (tablearg)
1976222488Srwatson							match = 1;
1977222488Srwatson					}
1978215179Sluigi				}
1979215179Sluigi				break;
1980215179Sluigi			}
1981215179Sluigi
1982159636Soleg			case O_TAGGED: {
1983201527Sluigi				struct m_tag *mtag;
1984250760Smelifaro				uint32_t tag = IP_FW_ARG_TABLEARG(cmd->arg1);
1985159636Soleg
1986158879Soleg				if (cmdlen == 1) {
1987159636Soleg					match = m_tag_locate(m, MTAG_IPFW,
1988159636Soleg					    tag, NULL) != NULL;
1989158879Soleg					break;
1990158879Soleg				}
1991158879Soleg
1992158879Soleg				/* we have ranges */
1993158879Soleg				for (mtag = m_tag_first(m);
1994158879Soleg				    mtag != NULL && !match;
1995158879Soleg				    mtag = m_tag_next(m, mtag)) {
1996158879Soleg					uint16_t *p;
1997158879Soleg					int i;
1998158879Soleg
1999158879Soleg					if (mtag->m_tag_cookie != MTAG_IPFW)
2000158879Soleg						continue;
2001158879Soleg
2002158879Soleg					p = ((ipfw_insn_u16 *)cmd)->ports;
2003158879Soleg					i = cmdlen - 1;
2004158879Soleg					for(; !match && i > 0; i--, p += 2)
2005158879Soleg						match =
2006158879Soleg						    mtag->m_tag_id >= p[0] &&
2007158879Soleg						    mtag->m_tag_id <= p[1];
2008158879Soleg				}
2009158879Soleg				break;
2010159636Soleg			}
2011158879Soleg
201299622Sluigi			/*
201399622Sluigi			 * The second set of opcodes represents 'actions',
201499622Sluigi			 * i.e. the terminal part of a rule once the packet
201599622Sluigi			 * matches all previous patterns.
201699622Sluigi			 * Typically there is only one action for each rule,
201799622Sluigi			 * and the opcode is stored at the end of the rule
201899622Sluigi			 * (but there are exceptions -- see below).
201999622Sluigi			 *
202099622Sluigi			 * In general, here we set retval and terminate the
202199622Sluigi			 * outer loop (would be a 'break 3' in some language,
2022200059Sluigi			 * but we need to set l=0, done=1)
202399622Sluigi			 *
202499622Sluigi			 * Exceptions:
202599622Sluigi			 * O_COUNT and O_SKIPTO actions:
202699622Sluigi			 *   instead of terminating, we jump to the next rule
2027200855Sluigi			 *   (setting l=0), or to the SKIPTO target (setting
2028200855Sluigi			 *   f/f_len, cmd and l as needed), respectively.
202999622Sluigi			 *
2030158879Soleg			 * O_TAG, O_LOG and O_ALTQ action parameters:
2031136071Sgreen			 *   perform some action and set match = 1;
2032136071Sgreen			 *
203399622Sluigi			 * O_LIMIT and O_KEEP_STATE: these opcodes are
203499622Sluigi			 *   not real 'actions', and are stored right
203599622Sluigi			 *   before the 'action' part of the rule.
203699622Sluigi			 *   These opcodes try to install an entry in the
203799622Sluigi			 *   state tables; if successful, we continue with
203899622Sluigi			 *   the next opcode (match=1; break;), otherwise
2039200059Sluigi			 *   the packet must be dropped (set retval,
2040200059Sluigi			 *   break loops with l=0, done=1)
204199622Sluigi			 *
204299622Sluigi			 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
204399622Sluigi			 *   cause a lookup of the state table, and a jump
204499622Sluigi			 *   to the 'action' part of the parent rule
2045200059Sluigi			 *   if an entry is found, or
204699622Sluigi			 *   (CHECK_STATE only) a jump to the next rule if
2047200059Sluigi			 *   the entry is not found.
2048200059Sluigi			 *   The result of the lookup is cached so that
2049200059Sluigi			 *   further instances of these opcodes become NOPs.
2050200059Sluigi			 *   The jump to the next rule is done by setting
2051200059Sluigi			 *   l=0, cmdlen=0.
205299622Sluigi			 */
205398943Sluigi			case O_LIMIT:
205498943Sluigi			case O_KEEP_STATE:
2055200601Sluigi				if (ipfw_install_state(f,
2056159636Soleg				    (ipfw_insn_limit *)cmd, args, tablearg)) {
2057200059Sluigi					/* error or limit violation */
2058140224Sglebius					retval = IP_FW_DENY;
2059200059Sluigi					l = 0;	/* exit inner loop */
2060200059Sluigi					done = 1; /* exit outer loop */
206199622Sluigi				}
206299622Sluigi				match = 1;
206399622Sluigi				break;
206498943Sluigi
206598943Sluigi			case O_PROBE_STATE:
206698943Sluigi			case O_CHECK_STATE:
206798943Sluigi				/*
206898943Sluigi				 * dynamic rules are checked at the first
206999622Sluigi				 * keep-state or check-state occurrence,
207099622Sluigi				 * with the result being stored in dyn_dir.
207199622Sluigi				 * The compiler introduces a PROBE_STATE
207298943Sluigi				 * instruction for us when we have a
207399622Sluigi				 * KEEP_STATE (because PROBE_STATE needs
207498943Sluigi				 * to be run first).
207598943Sluigi				 */
207699622Sluigi				if (dyn_dir == MATCH_UNKNOWN &&
2077200601Sluigi				    (q = ipfw_lookup_dyn_rule(&args->f_id,
2078100004Sluigi				     &dyn_dir, proto == IPPROTO_TCP ?
2079145093Sbrooks					TCP(ulp) : NULL))
2080100004Sluigi					!= NULL) {
208199622Sluigi					/*
208299622Sluigi					 * Found dynamic entry, update stats
208399622Sluigi					 * and jump to the 'action' part of
2084200059Sluigi					 * the parent rule by setting
2085200059Sluigi					 * f, cmd, l and clearing cmdlen.
208699622Sluigi					 */
2087250761Smelifaro					IPFW_INC_DYN_COUNTER(q, pktlen);
2088200855Sluigi					/* XXX we would like to have f_pos
2089200855Sluigi					 * readily accessible in the dynamic
2090200855Sluigi				         * rule, instead of having to
2091200855Sluigi					 * lookup q->rule.
2092200855Sluigi					 */
209399622Sluigi					f = q->rule;
2094200855Sluigi					f_pos = ipfw_find_rule(chain,
2095200855Sluigi						f->rulenum, f->id);
209699622Sluigi					cmd = ACTION_PTR(f);
209799622Sluigi					l = f->cmd_len - f->act_ofs;
2098244571Smelifaro					ipfw_dyn_unlock(q);
2099200059Sluigi					cmdlen = 0;
2100200059Sluigi					match = 1;
2101200059Sluigi					break;
210298943Sluigi				}
210399622Sluigi				/*
210499622Sluigi				 * Dynamic entry not found. If CHECK_STATE,
210599622Sluigi				 * skip to next rule, if PROBE_STATE just
210699622Sluigi				 * ignore and continue with next opcode.
210799622Sluigi				 */
210898943Sluigi				if (cmd->opcode == O_CHECK_STATE)
2109200059Sluigi					l = 0;	/* exit inner loop */
211099622Sluigi				match = 1;
211199622Sluigi				break;
211298943Sluigi
211398943Sluigi			case O_ACCEPT:
211498943Sluigi				retval = 0;	/* accept */
2115200059Sluigi				l = 0;		/* exit inner loop */
2116200059Sluigi				done = 1;	/* exit outer loop */
2117200059Sluigi				break;
211898943Sluigi
211998943Sluigi			case O_PIPE:
212098943Sluigi			case O_QUEUE:
2121200855Sluigi				set_match(args, f_pos, chain);
2122250760Smelifaro				args->rule.info = IP_FW_ARG_TABLEARG(cmd->arg1);
2123201527Sluigi				if (cmd->opcode == O_PIPE)
2124201527Sluigi					args->rule.info |= IPFW_IS_PIPE;
2125201527Sluigi				if (V_fw_one_pass)
2126201527Sluigi					args->rule.info |= IPFW_ONEPASS;
2127140224Sglebius				retval = IP_FW_DUMMYNET;
2128200059Sluigi				l = 0;          /* exit inner loop */
2129200059Sluigi				done = 1;       /* exit outer loop */
2130200059Sluigi				break;
2131105775Smaxim
213298943Sluigi			case O_DIVERT:
2133200059Sluigi			case O_TEE:
213498943Sluigi				if (args->eh) /* not on layer 2 */
2135200059Sluigi				    break;
2136200059Sluigi				/* otherwise this is terminal */
2137200059Sluigi				l = 0;		/* exit inner loop */
2138200059Sluigi				done = 1;	/* exit outer loop */
2139201527Sluigi				retval = (cmd->opcode == O_DIVERT) ?
2140200059Sluigi					IP_FW_DIVERT : IP_FW_TEE;
2141201527Sluigi				set_match(args, f_pos, chain);
2142250760Smelifaro				args->rule.info = IP_FW_ARG_TABLEARG(cmd->arg1);
2143200059Sluigi				break;
2144200059Sluigi
214598943Sluigi			case O_COUNT:
2146250761Smelifaro				IPFW_INC_RULE_COUNTER(f, pktlen);
2147200896Sluigi				l = 0;		/* exit inner loop */
2148200896Sluigi				break;
2149200855Sluigi
2150200855Sluigi			case O_SKIPTO:
2151250761Smelifaro			    IPFW_INC_RULE_COUNTER(f, pktlen);
2152250760Smelifaro			    f_pos = jump_fast(chain, f, cmd->arg1, tablearg, 0);
2153200896Sluigi			    /*
2154200855Sluigi			     * Skip disabled rules, and re-enter
2155200855Sluigi			     * the inner loop with the correct
2156200855Sluigi			     * f_pos, f, l and cmd.
2157200896Sluigi			     * Also clear cmdlen and skip_or
2158200896Sluigi			     */
2159200855Sluigi			    for (; f_pos < chain->n_rules - 1 &&
2160200855Sluigi				    (V_set_disable &
2161200855Sluigi				     (1 << chain->map[f_pos]->set));
2162200855Sluigi				    f_pos++)
2163200855Sluigi				;
2164209589Sglebius			    /* Re-enter the inner loop at the skipto rule. */
2165200855Sluigi			    f = chain->map[f_pos];
2166200896Sluigi			    l = f->cmd_len;
2167200896Sluigi			    cmd = f->cmd;
2168200896Sluigi			    match = 1;
2169200896Sluigi			    cmdlen = 0;
2170200896Sluigi			    skip_or = 0;
2171209589Sglebius			    continue;
2172209589Sglebius			    break;	/* not reached */
217398943Sluigi
2174223666Sae			case O_CALLRETURN: {
2175223666Sae				/*
2176223666Sae				 * Implementation of `subroutine' call/return,
2177223666Sae				 * in the stack carried in an mbuf tag. This
2178223666Sae				 * is different from `skipto' in that any call
2179223666Sae				 * address is possible (`skipto' must prevent
2180223666Sae				 * backward jumps to avoid endless loops).
2181223666Sae				 * We have `return' action when F_NOT flag is
2182223666Sae				 * present. The `m_tag_id' field is used as
2183223666Sae				 * stack pointer.
2184223666Sae				 */
2185223666Sae				struct m_tag *mtag;
2186223666Sae				uint16_t jmpto, *stack;
2187223666Sae
2188223666Sae#define	IS_CALL		((cmd->len & F_NOT) == 0)
2189223666Sae#define	IS_RETURN	((cmd->len & F_NOT) != 0)
2190223666Sae				/*
2191223666Sae				 * Hand-rolled version of m_tag_locate() with
2192223666Sae				 * wildcard `type'.
2193223666Sae				 * If not already tagged, allocate new tag.
2194223666Sae				 */
2195223666Sae				mtag = m_tag_first(m);
2196223666Sae				while (mtag != NULL) {
2197223666Sae					if (mtag->m_tag_cookie ==
2198223666Sae					    MTAG_IPFW_CALL)
2199223666Sae						break;
2200223666Sae					mtag = m_tag_next(m, mtag);
2201223666Sae				}
2202223666Sae				if (mtag == NULL && IS_CALL) {
2203223666Sae					mtag = m_tag_alloc(MTAG_IPFW_CALL, 0,
2204223666Sae					    IPFW_CALLSTACK_SIZE *
2205223666Sae					    sizeof(uint16_t), M_NOWAIT);
2206223666Sae					if (mtag != NULL)
2207223666Sae						m_tag_prepend(m, mtag);
2208223666Sae				}
2209223666Sae
2210223666Sae				/*
2211223666Sae				 * On error both `call' and `return' just
2212223666Sae				 * continue with next rule.
2213223666Sae				 */
2214223666Sae				if (IS_RETURN && (mtag == NULL ||
2215223666Sae				    mtag->m_tag_id == 0)) {
2216223666Sae					l = 0;		/* exit inner loop */
2217223666Sae					break;
2218223666Sae				}
2219223666Sae				if (IS_CALL && (mtag == NULL ||
2220223666Sae				    mtag->m_tag_id >= IPFW_CALLSTACK_SIZE)) {
2221223666Sae					printf("ipfw: call stack error, "
2222223666Sae					    "go to next rule\n");
2223223666Sae					l = 0;		/* exit inner loop */
2224223666Sae					break;
2225223666Sae				}
2226223666Sae
2227250761Smelifaro				IPFW_INC_RULE_COUNTER(f, pktlen);
2228223666Sae				stack = (uint16_t *)(mtag + 1);
2229223666Sae
2230223666Sae				/*
2231223666Sae				 * The `call' action may use cached f_pos
2232223666Sae				 * (in f->next_rule), whose version is written
2233223666Sae				 * in f->next_rule.
2234223666Sae				 * The `return' action, however, doesn't have
2235223666Sae				 * fixed jump address in cmd->arg1 and can't use
2236223666Sae				 * cache.
2237223666Sae				 */
2238223666Sae				if (IS_CALL) {
2239223666Sae					stack[mtag->m_tag_id] = f->rulenum;
2240223666Sae					mtag->m_tag_id++;
2241250760Smelifaro			    		f_pos = jump_fast(chain, f, cmd->arg1,
2242250760Smelifaro					    tablearg, 1);
2243223666Sae				} else {	/* `return' action */
2244223666Sae					mtag->m_tag_id--;
2245223666Sae					jmpto = stack[mtag->m_tag_id] + 1;
2246223666Sae					f_pos = ipfw_find_rule(chain, jmpto, 0);
2247223666Sae				}
2248223666Sae
2249223666Sae				/*
2250223666Sae				 * Skip disabled rules, and re-enter
2251223666Sae				 * the inner loop with the correct
2252223666Sae				 * f_pos, f, l and cmd.
2253223666Sae				 * Also clear cmdlen and skip_or
2254223666Sae				 */
2255223666Sae				for (; f_pos < chain->n_rules - 1 &&
2256223666Sae				    (V_set_disable &
2257223666Sae				    (1 << chain->map[f_pos]->set)); f_pos++)
2258223666Sae					;
2259223666Sae				/* Re-enter the inner loop at the dest rule. */
2260223666Sae				f = chain->map[f_pos];
2261223666Sae				l = f->cmd_len;
2262223666Sae				cmd = f->cmd;
2263223666Sae				cmdlen = 0;
2264223666Sae				skip_or = 0;
2265223666Sae				continue;
2266223666Sae				break;	/* NOTREACHED */
2267223666Sae			}
2268223666Sae#undef IS_CALL
2269223666Sae#undef IS_RETURN
2270223666Sae
227198943Sluigi			case O_REJECT:
227298943Sluigi				/*
227398943Sluigi				 * Drop the packet and send a reject notice
227498943Sluigi				 * if the packet is not ICMP (or is an ICMP
227598943Sluigi				 * query), and it is not multicast/broadcast.
227698943Sluigi				 */
2277154216Scperciva				if (hlen > 0 && is_ipv4 && offset == 0 &&
227898943Sluigi				    (proto != IPPROTO_ICMP ||
2279145093Sbrooks				     is_icmp_query(ICMP(ulp))) &&
228098943Sluigi				    !(m->m_flags & (M_BCAST|M_MCAST)) &&
2281123572Smaxim				    !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2282201122Sluigi					send_reject(args, cmd->arg1, iplen, ip);
228399475Sluigi					m = args->m;
228498943Sluigi				}
228599622Sluigi				/* FALLTHROUGH */
2286149020Sbz#ifdef INET6
2287149020Sbz			case O_UNREACH6:
2288149020Sbz				if (hlen > 0 && is_ipv6 &&
2289160025Sbz				    ((offset & IP6F_OFF_MASK) == 0) &&
2290149020Sbz				    (proto != IPPROTO_ICMPV6 ||
2291205173Sluigi				     (is_icmp6_query(icmp6_type) == 1)) &&
2292149020Sbz				    !(m->m_flags & (M_BCAST|M_MCAST)) &&
2293149020Sbz				    !IN6_IS_ADDR_MULTICAST(&args->f_id.dst_ip6)) {
2294165738Sjulian					send_reject6(
2295165738Sjulian					    args, cmd->arg1, hlen,
2296165738Sjulian					    (struct ip6_hdr *)ip);
2297149020Sbz					m = args->m;
2298149020Sbz				}
2299149020Sbz				/* FALLTHROUGH */
2300149020Sbz#endif
230199622Sluigi			case O_DENY:
2302140224Sglebius				retval = IP_FW_DENY;
2303200059Sluigi				l = 0;		/* exit inner loop */
2304200059Sluigi				done = 1;	/* exit outer loop */
2305200059Sluigi				break;
230698943Sluigi
2307200059Sluigi			case O_FORWARD_IP:
230898943Sluigi				if (args->eh)	/* not valid on layer2 pkts */
230999622Sluigi					break;
2310222582Sae				if (q == NULL || q->rule != f ||
2311222582Sae				    dyn_dir == MATCH_FORWARD) {
2312200059Sluigi				    struct sockaddr_in *sa;
2313200059Sluigi				    sa = &(((ipfw_insn_sa *)cmd)->sa);
2314200059Sluigi				    if (sa->sin_addr.s_addr == INADDR_ANY) {
2315200059Sluigi					bcopy(sa, &args->hopstore,
2316161424Sjulian							sizeof(*sa));
2317200059Sluigi					args->hopstore.sin_addr.s_addr =
2318161456Sjulian						    htonl(tablearg);
2319200059Sluigi					args->next_hop = &args->hopstore;
2320200059Sluigi				    } else {
2321200059Sluigi					args->next_hop = sa;
2322200059Sluigi				    }
2323161424Sjulian				}
2324140224Sglebius				retval = IP_FW_PASS;
2325200059Sluigi				l = 0;          /* exit inner loop */
2326200059Sluigi				done = 1;       /* exit outer loop */
2327200059Sluigi				break;
232898943Sluigi
2329225044Sbz#ifdef INET6
2330225044Sbz			case O_FORWARD_IP6:
2331225044Sbz				if (args->eh)	/* not valid on layer2 pkts */
2332225044Sbz					break;
2333225044Sbz				if (q == NULL || q->rule != f ||
2334225044Sbz				    dyn_dir == MATCH_FORWARD) {
2335225044Sbz					struct sockaddr_in6 *sin6;
2336225044Sbz
2337225044Sbz					sin6 = &(((ipfw_insn_sa6 *)cmd)->sa);
2338225044Sbz					args->next_hop6 = sin6;
2339225044Sbz				}
2340225044Sbz				retval = IP_FW_PASS;
2341225044Sbz				l = 0;		/* exit inner loop */
2342225044Sbz				done = 1;	/* exit outer loop */
2343225044Sbz				break;
2344225044Sbz#endif
2345225044Sbz
2346141351Sglebius			case O_NETGRAPH:
2347141351Sglebius			case O_NGTEE:
2348200855Sluigi				set_match(args, f_pos, chain);
2349250760Smelifaro				args->rule.info = IP_FW_ARG_TABLEARG(cmd->arg1);
2350210537Sglebius				if (V_fw_one_pass)
2351210537Sglebius					args->rule.info |= IPFW_ONEPASS;
2352141351Sglebius				retval = (cmd->opcode == O_NETGRAPH) ?
2353141351Sglebius				    IP_FW_NETGRAPH : IP_FW_NGTEE;
2354200059Sluigi				l = 0;          /* exit inner loop */
2355200059Sluigi				done = 1;       /* exit outer loop */
2356200059Sluigi				break;
2357141351Sglebius
2358222473Sae			case O_SETFIB: {
2359222473Sae				uint32_t fib;
2360222473Sae
2361250761Smelifaro				IPFW_INC_RULE_COUNTER(f, pktlen);
2362250760Smelifaro				fib = IP_FW_ARG_TABLEARG(cmd->arg1);
2363222473Sae				if (fib >= rt_numfibs)
2364222473Sae					fib = 0;
2365222473Sae				M_SETFIB(m, fib);
2366222473Sae				args->f_id.fib = fib;
2367200059Sluigi				l = 0;		/* exit inner loop */
2368200059Sluigi				break;
2369222473Sae		        }
2370178888Sjulian
2371250762Smelifaro			case O_SETDSCP: {
2372250762Smelifaro				uint16_t code;
2373250762Smelifaro
2374250762Smelifaro				code = IP_FW_ARG_TABLEARG(cmd->arg1) & 0x3F;
2375250762Smelifaro				l = 0;		/* exit inner loop */
2376250762Smelifaro				if (is_ipv4) {
2377250762Smelifaro					uint16_t a;
2378250762Smelifaro
2379250762Smelifaro					a = ip->ip_tos;
2380250762Smelifaro					ip->ip_tos = (code << 2) | (ip->ip_tos & 0x03);
2381250762Smelifaro					a += ntohs(ip->ip_sum) - ip->ip_tos;
2382250762Smelifaro					ip->ip_sum = htons(a);
2383250762Smelifaro				} else if (is_ipv6) {
2384250762Smelifaro					uint8_t *v;
2385250762Smelifaro
2386250762Smelifaro					v = &((struct ip6_hdr *)ip)->ip6_vfc;
2387250762Smelifaro					*v = (*v & 0xF0) | (code >> 2);
2388250762Smelifaro					v++;
2389250762Smelifaro					*v = (*v & 0x3F) | ((code & 0x03) << 6);
2390250762Smelifaro				} else
2391250762Smelifaro					break;
2392250762Smelifaro
2393250762Smelifaro				IPFW_INC_RULE_COUNTER(f, pktlen);
2394250762Smelifaro				break;
2395250762Smelifaro			}
2396250762Smelifaro
2397200059Sluigi			case O_NAT:
2398265699Smelifaro				l = 0;          /* exit inner loop */
2399265699Smelifaro				done = 1;       /* exit outer loop */
2400200059Sluigi 				if (!IPFW_NAT_LOADED) {
2401200059Sluigi				    retval = IP_FW_DENY;
2402265699Smelifaro				    break;
2403265699Smelifaro				}
2404176669Spiso
2405265699Smelifaro				struct cfg_nat *t;
2406265699Smelifaro				int nat_id;
2407265699Smelifaro
2408265699Smelifaro				set_match(args, f_pos, chain);
2409265699Smelifaro				/* Check if this is 'global' nat rule */
2410265699Smelifaro				if (cmd->arg1 == 0) {
2411265699Smelifaro					retval = ipfw_nat_ptr(args, NULL, m);
2412265699Smelifaro					break;
2413265699Smelifaro				}
2414265699Smelifaro				t = ((ipfw_insn_nat *)cmd)->nat;
2415265699Smelifaro				if (t == NULL) {
2416250760Smelifaro					nat_id = IP_FW_ARG_TABLEARG(cmd->arg1);
2417200838Sluigi					t = (*lookup_nat_ptr)(&chain->nat, nat_id);
2418200580Sluigi
2419165648Spiso					if (t == NULL) {
2420200059Sluigi					    retval = IP_FW_DENY;
2421200059Sluigi					    break;
2422176669Spiso					}
2423200059Sluigi					if (cmd->arg1 != IP_FW_TABLEARG)
2424200059Sluigi					    ((ipfw_insn_nat *)cmd)->nat = t;
2425200059Sluigi				}
2426265699Smelifaro				retval = ipfw_nat_ptr(args, t, m);
2427200059Sluigi				break;
2428165648Spiso
2429190633Spiso			case O_REASS: {
2430190633Spiso				int ip_off;
2431190633Spiso
2432250761Smelifaro				IPFW_INC_RULE_COUNTER(f, pktlen);
2433200059Sluigi				l = 0;	/* in any case exit inner loop */
2434201122Sluigi				ip_off = ntohs(ip->ip_off);
2435201122Sluigi
2436200059Sluigi				/* if not fragmented, go to next rule */
2437200059Sluigi				if ((ip_off & (IP_MF | IP_OFFMASK)) == 0)
2438200059Sluigi				    break;
2439200059Sluigi				/*
2440200059Sluigi				 * ip_reass() expects len & off in host
2441201527Sluigi				 * byte order.
2442200059Sluigi				 */
2443201527Sluigi				SET_HOST_IPLEN(ip);
2444200059Sluigi
2445200059Sluigi				args->m = m = ip_reass(m);
2446200059Sluigi
2447200059Sluigi				/*
2448201527Sluigi				 * do IP header checksum fixup.
2449200059Sluigi				 */
2450200059Sluigi				if (m == NULL) { /* fragment got swallowed */
2451200059Sluigi				    retval = IP_FW_DENY;
2452200059Sluigi				} else { /* good, packet complete */
2453200059Sluigi				    int hlen;
2454200059Sluigi
2455200059Sluigi				    ip = mtod(m, struct ip *);
2456200059Sluigi				    hlen = ip->ip_hl << 2;
2457201527Sluigi				    SET_NET_IPLEN(ip);
2458200059Sluigi				    ip->ip_sum = 0;
2459200059Sluigi				    if (hlen == sizeof(struct ip))
2460200059Sluigi					ip->ip_sum = in_cksum_hdr(ip);
2461200059Sluigi				    else
2462200059Sluigi					ip->ip_sum = in_cksum(m, hlen);
2463200059Sluigi				    retval = IP_FW_REASS;
2464200855Sluigi				    set_match(args, f_pos, chain);
2465200059Sluigi				}
2466200059Sluigi				done = 1;	/* exit outer loop */
2467200059Sluigi				break;
2468190633Spiso			}
2469190633Spiso
247098943Sluigi			default:
247198943Sluigi				panic("-- unknown opcode %d\n", cmd->opcode);
247299622Sluigi			} /* end of switch() on opcodes */
2473200059Sluigi			/*
2474200059Sluigi			 * if we get here with l=0, then match is irrelevant.
2475200059Sluigi			 */
247698943Sluigi
247799622Sluigi			if (cmd->len & F_NOT)
247899622Sluigi				match = !match;
247998943Sluigi
248099622Sluigi			if (match) {
248199622Sluigi				if (cmd->len & F_OR)
248299622Sluigi					skip_or = 1;
248399622Sluigi			} else {
248499622Sluigi				if (!(cmd->len & F_OR)) /* not an OR block, */
248599622Sluigi					break;		/* try next rule    */
248698943Sluigi			}
248798943Sluigi
2488200059Sluigi		}	/* end of inner loop, scan opcodes */
2489220796Sglebius#undef PULLUP_LEN
249098943Sluigi
2491200059Sluigi		if (done)
2492200059Sluigi			break;
2493105775Smaxim
2494200059Sluigi/* next_rule:; */	/* try next rule		*/
2495200059Sluigi
249698943Sluigi	}		/* end of outer for, scan rules */
249798943Sluigi
2498200059Sluigi	if (done) {
2499200855Sluigi		struct ip_fw *rule = chain->map[f_pos];
2500200059Sluigi		/* Update statistics */
2501250761Smelifaro		IPFW_INC_RULE_COUNTER(rule, pktlen);
2502200059Sluigi	} else {
2503200059Sluigi		retval = IP_FW_DENY;
2504200059Sluigi		printf("ipfw: ouch!, skip past end of rules, denying packet\n");
2505200059Sluigi	}
2506138642Scsjp	IPFW_RUNLOCK(chain);
2507204591Sluigi#ifdef __FreeBSD__
2508194498Sbrooks	if (ucred_cache != NULL)
2509194498Sbrooks		crfree(ucred_cache);
2510204591Sluigi#endif
2511140224Sglebius	return (retval);
251298943Sluigi
251398943Sluigipullup_failed:
2514181803Sbz	if (V_fw_verbose)
2515108258Smaxim		printf("ipfw: pullup failed\n");
2516140224Sglebius	return (IP_FW_DENY);
251798943Sluigi}
251898943Sluigi
2519200601Sluigi/*
2520234597Smelifaro * Set maximum number of tables that can be used in given VNET ipfw instance.
2521234597Smelifaro */
2522234597Smelifaro#ifdef SYSCTL_NODE
2523234597Smelifarostatic int
2524234597Smelifarosysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS)
2525234597Smelifaro{
2526234597Smelifaro	int error;
2527234597Smelifaro	unsigned int ntables;
2528234597Smelifaro
2529234597Smelifaro	ntables = V_fw_tables_max;
2530234597Smelifaro
2531234597Smelifaro	error = sysctl_handle_int(oidp, &ntables, 0, req);
2532234597Smelifaro	/* Read operation or some error */
2533234597Smelifaro	if ((error != 0) || (req->newptr == NULL))
2534234597Smelifaro		return (error);
2535234597Smelifaro
2536234597Smelifaro	return (ipfw_resize_tables(&V_layer3_chain, ntables));
2537234597Smelifaro}
2538234597Smelifaro#endif
2539234597Smelifaro/*
2540200601Sluigi * Module and VNET glue
2541200601Sluigi */
2542200601Sluigi
2543200601Sluigi/*
2544195862Sjulian * Stuff that must be initialised only on boot or module load
2545195862Sjulian */
2546196423Sjulianstatic int
254798943Sluigiipfw_init(void)
254898943Sluigi{
2549195862Sjulian	int error = 0;
2550195862Sjulian
2551195862Sjulian	/*
2552195862Sjulian 	 * Only print out this stuff the first time around,
2553195862Sjulian	 * when called from the sysinit code.
2554195862Sjulian	 */
2555195862Sjulian	printf("ipfw2 "
2556195862Sjulian#ifdef INET6
2557195862Sjulian		"(+ipv6) "
2558195862Sjulian#endif
2559195862Sjulian		"initialized, divert %s, nat %s, "
2560195862Sjulian		"default to %s, logging ",
2561195862Sjulian#ifdef IPDIVERT
2562195862Sjulian		"enabled",
2563195862Sjulian#else
2564195862Sjulian		"loadable",
2565195862Sjulian#endif
2566195862Sjulian#ifdef IPFIREWALL_NAT
2567195862Sjulian		"enabled",
2568195862Sjulian#else
2569195862Sjulian		"loadable",
2570195862Sjulian#endif
2571195862Sjulian		default_to_accept ? "accept" : "deny");
2572195862Sjulian
2573195862Sjulian	/*
2574196423Sjulian	 * Note: V_xxx variables can be accessed here but the vnet specific
2575196423Sjulian	 * initializer may not have been called yet for the VIMAGE case.
2576196423Sjulian	 * Tuneables will have been processed. We will print out values for
2577196423Sjulian	 * the default vnet.
2578196423Sjulian	 * XXX This should all be rationalized AFTER 8.0
2579195862Sjulian	 */
2580195862Sjulian	if (V_fw_verbose == 0)
2581195862Sjulian		printf("disabled\n");
2582195862Sjulian	else if (V_verbose_limit == 0)
2583195862Sjulian		printf("unlimited\n");
2584195862Sjulian	else
2585195862Sjulian		printf("limited to %d packets/entry by default\n",
2586195862Sjulian		    V_verbose_limit);
2587195862Sjulian
2588234597Smelifaro	/* Check user-supplied table count for validness */
2589234597Smelifaro	if (default_fw_tables > IPFW_TABLES_MAX)
2590234597Smelifaro	  default_fw_tables = IPFW_TABLES_MAX;
2591234597Smelifaro
2592200654Sluigi	ipfw_log_bpf(1); /* init */
2593195862Sjulian	return (error);
2594195862Sjulian}
2595195862Sjulian
2596200601Sluigi/*
2597197952Sjulian * Called for the removal of the last instance only on module unload.
2598197952Sjulian */
2599197952Sjulianstatic void
2600197952Sjulianipfw_destroy(void)
2601197952Sjulian{
2602197952Sjulian
2603200654Sluigi	ipfw_log_bpf(0); /* uninit */
2604197952Sjulian	printf("IP firewall unloaded\n");
2605197952Sjulian}
2606197952Sjulian
2607200601Sluigi/*
2608196423Sjulian * Stuff that must be initialized for every instance
2609196423Sjulian * (including the first of course).
2610195862Sjulian */
2611195862Sjulianstatic int
2612195862Sjulianvnet_ipfw_init(const void *unused)
2613195862Sjulian{
2614195862Sjulian	int error;
2615200855Sluigi	struct ip_fw *rule = NULL;
2616200838Sluigi	struct ip_fw_chain *chain;
261798943Sluigi
2618200838Sluigi	chain = &V_layer3_chain;
2619200838Sluigi
2620195862Sjulian	/* First set up some values that are compile time options */
2621200838Sluigi	V_autoinc_step = 100;	/* bounded to 1..1000 in add_rule() */
2622200838Sluigi	V_fw_deny_unknown_exthdrs = 1;
2623195862Sjulian#ifdef IPFIREWALL_VERBOSE
2624195862Sjulian	V_fw_verbose = 1;
2625195862Sjulian#endif
2626195862Sjulian#ifdef IPFIREWALL_VERBOSE_LIMIT
2627195862Sjulian	V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
2628195862Sjulian#endif
2629200855Sluigi#ifdef IPFIREWALL_NAT
2630200855Sluigi	LIST_INIT(&chain->nat);
2631200855Sluigi#endif
2632195862Sjulian
2633200855Sluigi	/* insert the default rule and create the initial map */
2634200855Sluigi	chain->n_rules = 1;
2635200855Sluigi	chain->static_len = sizeof(struct ip_fw);
2636234597Smelifaro	chain->map = malloc(sizeof(struct ip_fw *), M_IPFW, M_WAITOK | M_ZERO);
2637200855Sluigi	if (chain->map)
2638234597Smelifaro		rule = malloc(chain->static_len, M_IPFW, M_WAITOK | M_ZERO);
2639234597Smelifaro
2640234597Smelifaro	/* Set initial number of tables */
2641234597Smelifaro	V_fw_tables_max = default_fw_tables;
2642200838Sluigi	error = ipfw_init_tables(chain);
2643195862Sjulian	if (error) {
2644234597Smelifaro		printf("ipfw2: setting up tables failed\n");
2645234597Smelifaro		free(chain->map, M_IPFW);
2646234597Smelifaro		free(rule, M_IPFW);
2647234597Smelifaro		return (ENOSPC);
2648195862Sjulian	}
2649195862Sjulian
2650200855Sluigi	/* fill and insert the default rule */
2651200855Sluigi	rule->act_ofs = 0;
2652200855Sluigi	rule->rulenum = IPFW_DEFAULT_RULE;
2653200855Sluigi	rule->cmd_len = 1;
2654200855Sluigi	rule->set = RESVD_SET;
2655200855Sluigi	rule->cmd[0].len = 1;
2656200855Sluigi	rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY;
2657265699Smelifaro	chain->default_rule = chain->map[0] = rule;
2658200855Sluigi	chain->id = rule->id = 1;
2659185895Szec
2660200838Sluigi	IPFW_LOCK_INIT(chain);
2661244571Smelifaro	ipfw_dyn_init(chain);
2662195862Sjulian
2663195862Sjulian	/* First set up some values that are compile time options */
2664196423Sjulian	V_ipfw_vnet_ready = 1;		/* Open for business */
266598943Sluigi
2666200116Sluigi	/*
2667200116Sluigi	 * Hook the sockopt handler, and the layer2 (V_ip_fw_chk_ptr)
2668200116Sluigi	 * and pfil hooks for ipv4 and ipv6. Even if the latter two fail
2669200118Sluigi	 * we still keep the module alive because the sockopt and
2670200118Sluigi	 * layer2 paths are still useful.
2671200118Sluigi	 * ipfw[6]_hook return 0 on success, ENOENT on failure,
2672200118Sluigi	 * so we can ignore the exact return value and just set a flag.
2673200116Sluigi	 *
2674200118Sluigi	 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so
2675200118Sluigi	 * changes in the underlying (per-vnet) variables trigger
2676200118Sluigi	 * immediate hook()/unhook() calls.
2677200118Sluigi	 * In layer2 we have the same behaviour, except that V_ether_ipfw
2678200118Sluigi	 * is checked on each packet because there are no pfil hooks.
2679200116Sluigi	 */
2680197952Sjulian	V_ip_fw_ctl_ptr = ipfw_ctl;
2681197952Sjulian	V_ip_fw_chk_ptr = ipfw_chk;
2682201122Sluigi	error = ipfw_attach_hooks(1);
2683200116Sluigi	return (error);
2684195862Sjulian}
2685195862Sjulian
2686200601Sluigi/*
2687195862Sjulian * Called for the removal of each instance.
2688195862Sjulian */
2689195862Sjulianstatic int
2690195862Sjulianvnet_ipfw_uninit(const void *unused)
2691195862Sjulian{
2692200855Sluigi	struct ip_fw *reap, *rule;
2693200838Sluigi	struct ip_fw_chain *chain = &V_layer3_chain;
2694200855Sluigi	int i;
2695120141Ssam
2696196423Sjulian	V_ipfw_vnet_ready = 0; /* tell new callers to go away */
2697200116Sluigi	/*
2698200116Sluigi	 * disconnect from ipv4, ipv6, layer2 and sockopt.
2699200116Sluigi	 * Then grab, release and grab again the WLOCK so we make
2700200116Sluigi	 * sure the update is propagated and nobody will be in.
2701200116Sluigi	 */
2702201122Sluigi	(void)ipfw_attach_hooks(0 /* detach */);
2703197952Sjulian	V_ip_fw_chk_ptr = NULL;
2704197952Sjulian	V_ip_fw_ctl_ptr = NULL;
2705200855Sluigi	IPFW_UH_WLOCK(chain);
2706200855Sluigi	IPFW_UH_WUNLOCK(chain);
2707200855Sluigi	IPFW_UH_WLOCK(chain);
2708200116Sluigi
2709200838Sluigi	IPFW_WLOCK(chain);
2710234597Smelifaro	ipfw_dyn_uninit(0);	/* run the callout_drain */
2711200838Sluigi	IPFW_WUNLOCK(chain);
2712200116Sluigi
2713204837Sbz	ipfw_destroy_tables(chain);
2714200855Sluigi	reap = NULL;
2715234597Smelifaro	IPFW_WLOCK(chain);
2716200855Sluigi	for (i = 0; i < chain->n_rules; i++) {
2717200855Sluigi		rule = chain->map[i];
2718200855Sluigi		rule->x_next = reap;
2719200855Sluigi		reap = rule;
2720200855Sluigi	}
2721200855Sluigi	if (chain->map)
2722200855Sluigi		free(chain->map, M_IPFW);
2723200838Sluigi	IPFW_WUNLOCK(chain);
2724200855Sluigi	IPFW_UH_WUNLOCK(chain);
2725195862Sjulian	if (reap != NULL)
2726200590Sluigi		ipfw_reap_rules(reap);
2727200838Sluigi	IPFW_LOCK_DESTROY(chain);
2728200580Sluigi	ipfw_dyn_uninit(1);	/* free the remaining parts */
2729195862Sjulian	return 0;
2730195862Sjulian}
2731149020Sbz
2732196423Sjulian/*
2733196423Sjulian * Module event handler.
2734196423Sjulian * In general we have the choice of handling most of these events by the
2735196423Sjulian * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to
2736196423Sjulian * use the SYSINIT handlers as they are more capable of expressing the
2737196423Sjulian * flow of control during module and vnet operations, so this is just
2738196423Sjulian * a skeleton. Note there is no SYSINIT equivalent of the module
2739196423Sjulian * SHUTDOWN handler, but we don't have anything to do in that case anyhow.
2740196423Sjulian */
2741196423Sjulianstatic int
2742196423Sjulianipfw_modevent(module_t mod, int type, void *unused)
2743196423Sjulian{
2744196423Sjulian	int err = 0;
2745149020Sbz
2746196423Sjulian	switch (type) {
2747196423Sjulian	case MOD_LOAD:
2748196423Sjulian		/* Called once at module load or
2749196423Sjulian	 	 * system boot if compiled in. */
2750196423Sjulian		break;
2751197952Sjulian	case MOD_QUIESCE:
2752197952Sjulian		/* Called before unload. May veto unloading. */
2753197952Sjulian		break;
2754196423Sjulian	case MOD_UNLOAD:
2755196423Sjulian		/* Called during unload. */
2756196423Sjulian		break;
2757196423Sjulian	case MOD_SHUTDOWN:
2758196423Sjulian		/* Called during system shutdown. */
2759196423Sjulian		break;
2760196423Sjulian	default:
2761196423Sjulian		err = EOPNOTSUPP;
2762196423Sjulian		break;
2763196423Sjulian	}
2764196423Sjulian	return err;
2765196423Sjulian}
2766195862Sjulian
2767196423Sjulianstatic moduledata_t ipfwmod = {
2768196423Sjulian	"ipfw",
2769196423Sjulian	ipfw_modevent,
2770196423Sjulian	0
2771196423Sjulian};
2772196423Sjulian
2773196423Sjulian/* Define startup order. */
2774196423Sjulian#define	IPFW_SI_SUB_FIREWALL	SI_SUB_PROTO_IFATTACHDOMAIN
2775196423Sjulian#define	IPFW_MODEVENT_ORDER	(SI_ORDER_ANY - 255) /* On boot slot in here. */
2776196423Sjulian#define	IPFW_MODULE_ORDER	(IPFW_MODEVENT_ORDER + 1) /* A little later. */
2777196423Sjulian#define	IPFW_VNET_ORDER		(IPFW_MODEVENT_ORDER + 2) /* Later still. */
2778196423Sjulian
2779196423SjulianDECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER);
2780196423SjulianMODULE_VERSION(ipfw, 2);
2781196423Sjulian/* should declare some dependencies here */
2782196423Sjulian
2783196423Sjulian/*
2784196423Sjulian * Starting up. Done in order after ipfwmod() has been called.
2785196423Sjulian * VNET_SYSINIT is also called for each existing vnet and each new vnet.
2786196423Sjulian */
2787196423SjulianSYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
2788196423Sjulian	    ipfw_init, NULL);
2789196423SjulianVNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
2790196423Sjulian	    vnet_ipfw_init, NULL);
2791195862Sjulian
2792196423Sjulian/*
2793196423Sjulian * Closing up shop. These are done in REVERSE ORDER, but still
2794196423Sjulian * after ipfwmod() has been called. Not called on reboot.
2795196423Sjulian * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
2796196423Sjulian * or when the module is unloaded.
2797196423Sjulian */
2798196423SjulianSYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
2799196423Sjulian	    ipfw_destroy, NULL);
2800196423SjulianVNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
2801196423Sjulian	    vnet_ipfw_uninit, NULL);
2802200601Sluigi/* end of file */
2803