xref: /freebsd/sys/netpfil/ipfw/ip_fw2.c (revision 392150e6ca6662f1d74c1c1fcff94945c875f966)
13b3a8eb9SGleb Smirnoff /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
43b3a8eb9SGleb Smirnoff  * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
53b3a8eb9SGleb Smirnoff  *
63b3a8eb9SGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
73b3a8eb9SGleb Smirnoff  * modification, are permitted provided that the following conditions
83b3a8eb9SGleb Smirnoff  * are met:
93b3a8eb9SGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
103b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
113b3a8eb9SGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
123b3a8eb9SGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
133b3a8eb9SGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
143b3a8eb9SGleb Smirnoff  *
153b3a8eb9SGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163b3a8eb9SGleb Smirnoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173b3a8eb9SGleb Smirnoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183b3a8eb9SGleb Smirnoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193b3a8eb9SGleb Smirnoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203b3a8eb9SGleb Smirnoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213b3a8eb9SGleb Smirnoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223b3a8eb9SGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233b3a8eb9SGleb Smirnoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243b3a8eb9SGleb Smirnoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253b3a8eb9SGleb Smirnoff  * SUCH DAMAGE.
263b3a8eb9SGleb Smirnoff  */
273b3a8eb9SGleb Smirnoff 
283b3a8eb9SGleb Smirnoff #include <sys/cdefs.h>
293b3a8eb9SGleb Smirnoff /*
303b3a8eb9SGleb Smirnoff  * The FreeBSD IP packet firewall, main file
313b3a8eb9SGleb Smirnoff  */
323b3a8eb9SGleb Smirnoff 
333b3a8eb9SGleb Smirnoff #include "opt_ipfw.h"
343b3a8eb9SGleb Smirnoff #include "opt_ipdivert.h"
353b3a8eb9SGleb Smirnoff #include "opt_inet.h"
363b3a8eb9SGleb Smirnoff #ifndef INET
373b3a8eb9SGleb Smirnoff #error "IPFIREWALL requires INET"
383b3a8eb9SGleb Smirnoff #endif /* INET */
393b3a8eb9SGleb Smirnoff #include "opt_inet6.h"
403b3a8eb9SGleb Smirnoff 
413b3a8eb9SGleb Smirnoff #include <sys/param.h>
423b3a8eb9SGleb Smirnoff #include <sys/systm.h>
433b3a8eb9SGleb Smirnoff #include <sys/condvar.h>
447e767c79SAlexander V. Chernikov #include <sys/counter.h>
453b3a8eb9SGleb Smirnoff #include <sys/eventhandler.h>
463b3a8eb9SGleb Smirnoff #include <sys/malloc.h>
473b3a8eb9SGleb Smirnoff #include <sys/mbuf.h>
483b3a8eb9SGleb Smirnoff #include <sys/kernel.h>
493b3a8eb9SGleb Smirnoff #include <sys/lock.h>
503b3a8eb9SGleb Smirnoff #include <sys/jail.h>
513b3a8eb9SGleb Smirnoff #include <sys/module.h>
523b3a8eb9SGleb Smirnoff #include <sys/priv.h>
533b3a8eb9SGleb Smirnoff #include <sys/proc.h>
543b3a8eb9SGleb Smirnoff #include <sys/rwlock.h>
55ccba94b8SAlexander V. Chernikov #include <sys/rmlock.h>
567ec2f6bcSAndrey V. Elsukov #include <sys/sdt.h>
573b3a8eb9SGleb Smirnoff #include <sys/socket.h>
583b3a8eb9SGleb Smirnoff #include <sys/socketvar.h>
593b3a8eb9SGleb Smirnoff #include <sys/sysctl.h>
603b3a8eb9SGleb Smirnoff #include <sys/syslog.h>
613b3a8eb9SGleb Smirnoff #include <sys/ucred.h>
623b3a8eb9SGleb Smirnoff #include <net/ethernet.h> /* for ETHERTYPE_IP */
633b3a8eb9SGleb Smirnoff #include <net/if.h>
6476039bc8SGleb Smirnoff #include <net/if_var.h>
653d0d5b21SJustin Hibbits #include <net/if_private.h>
663b3a8eb9SGleb Smirnoff #include <net/route.h>
676ad7446cSAlexander V. Chernikov #include <net/route/nhop.h>
68c1de64a4SAndrey V. Elsukov #include <net/pfil.h>
693b3a8eb9SGleb Smirnoff #include <net/vnet.h>
7066f2f9eeSKristof Provost #include <net/if_pfsync.h>
713b3a8eb9SGleb Smirnoff 
7275bf2db3SGleb Smirnoff #include <netpfil/pf/pf_mtag.h>
7375bf2db3SGleb Smirnoff 
743b3a8eb9SGleb Smirnoff #include <netinet/in.h>
753b3a8eb9SGleb Smirnoff #include <netinet/in_var.h>
763b3a8eb9SGleb Smirnoff #include <netinet/in_pcb.h>
773b3a8eb9SGleb Smirnoff #include <netinet/ip.h>
783b3a8eb9SGleb Smirnoff #include <netinet/ip_var.h>
793b3a8eb9SGleb Smirnoff #include <netinet/ip_icmp.h>
803b3a8eb9SGleb Smirnoff #include <netinet/ip_fw.h>
813b3a8eb9SGleb Smirnoff #include <netinet/ip_carp.h>
823b3a8eb9SGleb Smirnoff #include <netinet/pim.h>
833b3a8eb9SGleb Smirnoff #include <netinet/tcp_var.h>
843b3a8eb9SGleb Smirnoff #include <netinet/udp.h>
853b3a8eb9SGleb Smirnoff #include <netinet/udp_var.h>
863b3a8eb9SGleb Smirnoff #include <netinet/sctp.h>
87665c8a2eSMichael Tuexen #include <netinet/sctp_crc32.h>
88665c8a2eSMichael Tuexen #include <netinet/sctp_header.h>
893b3a8eb9SGleb Smirnoff 
903b3a8eb9SGleb Smirnoff #include <netinet/ip6.h>
913b3a8eb9SGleb Smirnoff #include <netinet/icmp6.h>
9265ff3638SAlexander V. Chernikov #include <netinet/in_fib.h>
933b3a8eb9SGleb Smirnoff #ifdef INET6
9465ff3638SAlexander V. Chernikov #include <netinet6/in6_fib.h>
953b3a8eb9SGleb Smirnoff #include <netinet6/in6_pcb.h>
963b3a8eb9SGleb Smirnoff #include <netinet6/scope6_var.h>
973b3a8eb9SGleb Smirnoff #include <netinet6/ip6_var.h>
983b3a8eb9SGleb Smirnoff #endif
993b3a8eb9SGleb Smirnoff 
100b0e1660dSPhilip Paeps #include <net/if_gre.h> /* for struct grehdr */
101b0e1660dSPhilip Paeps 
1023b3a8eb9SGleb Smirnoff #include <netpfil/ipfw/ip_fw_private.h>
1033b3a8eb9SGleb Smirnoff 
1043b3a8eb9SGleb Smirnoff #include <machine/in_cksum.h>	/* XXX for in_cksum */
1053b3a8eb9SGleb Smirnoff 
1063b3a8eb9SGleb Smirnoff #ifdef MAC
1073b3a8eb9SGleb Smirnoff #include <security/mac/mac_framework.h>
1083b3a8eb9SGleb Smirnoff #endif
1093b3a8eb9SGleb Smirnoff 
1107ec2f6bcSAndrey V. Elsukov #define	IPFW_PROBE(probe, arg0, arg1, arg2, arg3, arg4, arg5)		\
1117ec2f6bcSAndrey V. Elsukov     SDT_PROBE6(ipfw, , , probe, arg0, arg1, arg2, arg3, arg4, arg5)
1127ec2f6bcSAndrey V. Elsukov 
1137ec2f6bcSAndrey V. Elsukov SDT_PROVIDER_DEFINE(ipfw);
1147ec2f6bcSAndrey V. Elsukov SDT_PROBE_DEFINE6(ipfw, , , rule__matched,
1157ec2f6bcSAndrey V. Elsukov     "int",			/* retval */
1167ec2f6bcSAndrey V. Elsukov     "int",			/* af */
1177ec2f6bcSAndrey V. Elsukov     "void *",			/* src addr */
1187ec2f6bcSAndrey V. Elsukov     "void *",			/* dst addr */
1197ec2f6bcSAndrey V. Elsukov     "struct ip_fw_args *",	/* args */
1207ec2f6bcSAndrey V. Elsukov     "struct ip_fw *"		/* rule */);
1217ec2f6bcSAndrey V. Elsukov 
1223b3a8eb9SGleb Smirnoff /*
1233b3a8eb9SGleb Smirnoff  * static variables followed by global ones.
1243b3a8eb9SGleb Smirnoff  * All ipfw global variables are here.
1253b3a8eb9SGleb Smirnoff  */
1263b3a8eb9SGleb Smirnoff 
1275f901c92SAndrew Turner VNET_DEFINE_STATIC(int, fw_deny_unknown_exthdrs);
1283b3a8eb9SGleb Smirnoff #define	V_fw_deny_unknown_exthdrs	VNET(fw_deny_unknown_exthdrs)
1293b3a8eb9SGleb Smirnoff 
1305f901c92SAndrew Turner VNET_DEFINE_STATIC(int, fw_permit_single_frag6) = 1;
1313b3a8eb9SGleb Smirnoff #define	V_fw_permit_single_frag6	VNET(fw_permit_single_frag6)
1323b3a8eb9SGleb Smirnoff 
1333b3a8eb9SGleb Smirnoff #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
1343b3a8eb9SGleb Smirnoff static int default_to_accept = 1;
1353b3a8eb9SGleb Smirnoff #else
1363b3a8eb9SGleb Smirnoff static int default_to_accept;
1373b3a8eb9SGleb Smirnoff #endif
1383b3a8eb9SGleb Smirnoff 
1393b3a8eb9SGleb Smirnoff VNET_DEFINE(int, autoinc_step);
1403b3a8eb9SGleb Smirnoff VNET_DEFINE(int, fw_one_pass) = 1;
1413b3a8eb9SGleb Smirnoff 
1423b3a8eb9SGleb Smirnoff VNET_DEFINE(unsigned int, fw_tables_max);
143b074b7bbSAlexander V. Chernikov VNET_DEFINE(unsigned int, fw_tables_sets) = 0;	/* Don't use set-aware tables */
1443b3a8eb9SGleb Smirnoff /* Use 128 tables by default */
1453b3a8eb9SGleb Smirnoff static unsigned int default_fw_tables = IPFW_TABLES_DEFAULT;
1463b3a8eb9SGleb Smirnoff 
1474a77657cSAndrey V. Elsukov #ifndef IPFIREWALL_LINEAR_SKIPTO
1484a77657cSAndrey V. Elsukov VNET_DEFINE(int, skipto_cache) = 0;
149f8350f3aSAlexander V. Chernikov #else
1504a77657cSAndrey V. Elsukov VNET_DEFINE(int, skipto_cache) = 1;
151f8350f3aSAlexander V. Chernikov #endif
152ce743e5cSAlexander V. Chernikov 
1534a77657cSAndrey V. Elsukov static uint32_t jump(struct ip_fw_chain *chain, struct ip_fw *f,
1544a77657cSAndrey V. Elsukov     uint32_t num, int tablearg, bool jump_backwards);
1554a77657cSAndrey V. Elsukov 
1563b3a8eb9SGleb Smirnoff /*
1573b3a8eb9SGleb Smirnoff  * Each rule belongs to one of 32 different sets (0..31).
1583b3a8eb9SGleb Smirnoff  * The variable set_disable contains one bit per set.
1593b3a8eb9SGleb Smirnoff  * If the bit is set, all rules in the corresponding set
1603b3a8eb9SGleb Smirnoff  * are disabled. Set RESVD_SET(31) is reserved for the default rule
1613b3a8eb9SGleb Smirnoff  * and rules that are not deleted by the flush command,
1623b3a8eb9SGleb Smirnoff  * and CANNOT be disabled.
1633b3a8eb9SGleb Smirnoff  * Rules in set RESVD_SET can only be deleted individually.
1643b3a8eb9SGleb Smirnoff  */
1653b3a8eb9SGleb Smirnoff VNET_DEFINE(u_int32_t, set_disable);
1663b3a8eb9SGleb Smirnoff #define	V_set_disable			VNET(set_disable)
1673b3a8eb9SGleb Smirnoff 
1683b3a8eb9SGleb Smirnoff VNET_DEFINE(int, fw_verbose);
1693b3a8eb9SGleb Smirnoff /* counter for ipfw_log(NULL...) */
1703b3a8eb9SGleb Smirnoff VNET_DEFINE(u_int64_t, norule_counter);
1713b3a8eb9SGleb Smirnoff VNET_DEFINE(int, verbose_limit);
1723b3a8eb9SGleb Smirnoff 
1733b3a8eb9SGleb Smirnoff /* layer3_chain contains the list of rules for layer 3 */
1743b3a8eb9SGleb Smirnoff VNET_DEFINE(struct ip_fw_chain, layer3_chain);
1753b3a8eb9SGleb Smirnoff 
17698eff10eSAlexander V. Chernikov /* ipfw_vnet_ready controls when we are open for business */
17798eff10eSAlexander V. Chernikov VNET_DEFINE(int, ipfw_vnet_ready) = 0;
17898eff10eSAlexander V. Chernikov 
1798856400bSMikolaj Golub VNET_DEFINE(int, ipfw_nat_ready) = 0;
1808856400bSMikolaj Golub 
1813b3a8eb9SGleb Smirnoff ipfw_nat_t *ipfw_nat_ptr = NULL;
1823b3a8eb9SGleb Smirnoff struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
1833b3a8eb9SGleb Smirnoff ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
1843b3a8eb9SGleb Smirnoff ipfw_nat_cfg_t *ipfw_nat_del_ptr;
1853b3a8eb9SGleb Smirnoff ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
1863b3a8eb9SGleb Smirnoff ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
1873b3a8eb9SGleb Smirnoff 
1883b3a8eb9SGleb Smirnoff #ifdef SYSCTL_NODE
1893b3a8eb9SGleb Smirnoff uint32_t dummy_def = IPFW_DEFAULT_RULE;
1903b3a8eb9SGleb Smirnoff static int sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS);
191a73d728dSAlexander V. Chernikov static int sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS);
1923b3a8eb9SGleb Smirnoff 
1933b3a8eb9SGleb Smirnoff SYSBEGIN(f3)
1943b3a8eb9SGleb Smirnoff 
1957029da5cSPawel Biernacki SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1967029da5cSPawel Biernacki     "Firewall");
1976df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, one_pass,
1986df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0,
199*392150e6SDmitry Morozovsky     "Only do a single pass through ipfw when using dummynet(4), ipfw_nat or other divert(4)-like interfaces");
2006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step,
2016df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(autoinc_step), 0,
2023b3a8eb9SGleb Smirnoff     "Rule number auto-increment step");
2036df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose,
2046df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0,
2053b3a8eb9SGleb Smirnoff     "Log matches to ipfw rules");
2066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit,
2076df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(verbose_limit), 0,
2083b3a8eb9SGleb Smirnoff     "Set upper limit of matches of ipfw rules logged");
2094a77657cSAndrey V. Elsukov SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, skipto_cache,
2104a77657cSAndrey V. Elsukov     CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(skipto_cache), 0,
2114a77657cSAndrey V. Elsukov     "Status of linear skipto cache: 1 - enabled, 0 - disabled.");
2123b3a8eb9SGleb Smirnoff SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD,
2133b3a8eb9SGleb Smirnoff     &dummy_def, 0,
2143b3a8eb9SGleb Smirnoff     "The default/max possible rule number.");
2156df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_max,
2167029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2177029da5cSPawel Biernacki     0, 0, sysctl_ipfw_table_num, "IU",
218ac35ff17SAlexander V. Chernikov     "Maximum number of concurrently used tables");
2196df8a710SGleb Smirnoff SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_sets,
2207029da5cSPawel Biernacki     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
2216df8a710SGleb Smirnoff     0, 0, sysctl_ipfw_tables_sets, "IU",
222ac35ff17SAlexander V. Chernikov     "Use per-set namespace for tables");
2233b3a8eb9SGleb Smirnoff SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN,
2243b3a8eb9SGleb Smirnoff     &default_to_accept, 0,
2253b3a8eb9SGleb Smirnoff     "Make the default rule accept all packets.");
2263b3a8eb9SGleb Smirnoff TUNABLE_INT("net.inet.ip.fw.tables_max", (int *)&default_fw_tables);
2276df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count,
2286df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0,
2293b3a8eb9SGleb Smirnoff     "Number of static rules");
2303b3a8eb9SGleb Smirnoff 
2313b3a8eb9SGleb Smirnoff #ifdef INET6
2323b3a8eb9SGleb Smirnoff SYSCTL_DECL(_net_inet6_ip6);
2337029da5cSPawel Biernacki SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
2347029da5cSPawel Biernacki     "Firewall");
2356df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs,
2366df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
2376df8a710SGleb Smirnoff     &VNET_NAME(fw_deny_unknown_exthdrs), 0,
2383b3a8eb9SGleb Smirnoff     "Deny packets with unknown IPv6 Extension Headers");
2396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, permit_single_frag6,
2406df8a710SGleb Smirnoff     CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
2416df8a710SGleb Smirnoff     &VNET_NAME(fw_permit_single_frag6), 0,
2423b3a8eb9SGleb Smirnoff     "Permit single packet IPv6 fragments");
2433b3a8eb9SGleb Smirnoff #endif /* INET6 */
2443b3a8eb9SGleb Smirnoff 
2453b3a8eb9SGleb Smirnoff SYSEND
2463b3a8eb9SGleb Smirnoff 
2473b3a8eb9SGleb Smirnoff #endif /* SYSCTL_NODE */
2483b3a8eb9SGleb Smirnoff 
2493b3a8eb9SGleb Smirnoff /*
2503b3a8eb9SGleb Smirnoff  * Some macros used in the various matching options.
2513b3a8eb9SGleb Smirnoff  * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
2523b3a8eb9SGleb Smirnoff  * Other macros just cast void * into the appropriate type
2533b3a8eb9SGleb Smirnoff  */
2543b3a8eb9SGleb Smirnoff #define	L3HDR(T, ip)	((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
2553b3a8eb9SGleb Smirnoff #define	TCP(p)		((struct tcphdr *)(p))
2563b3a8eb9SGleb Smirnoff #define	SCTP(p)		((struct sctphdr *)(p))
2573b3a8eb9SGleb Smirnoff #define	UDP(p)		((struct udphdr *)(p))
2583b3a8eb9SGleb Smirnoff #define	ICMP(p)		((struct icmphdr *)(p))
2593b3a8eb9SGleb Smirnoff #define	ICMP6(p)	((struct icmp6_hdr *)(p))
2603b3a8eb9SGleb Smirnoff 
2613b3a8eb9SGleb Smirnoff static __inline int
icmptype_match(struct icmphdr * icmp,ipfw_insn_u32 * cmd)2623b3a8eb9SGleb Smirnoff icmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd)
2633b3a8eb9SGleb Smirnoff {
2643b3a8eb9SGleb Smirnoff 	int type = icmp->icmp_type;
2653b3a8eb9SGleb Smirnoff 
2663b3a8eb9SGleb Smirnoff 	return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) );
2673b3a8eb9SGleb Smirnoff }
2683b3a8eb9SGleb Smirnoff 
2693b3a8eb9SGleb Smirnoff #define TT	( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
2703b3a8eb9SGleb Smirnoff     (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
2713b3a8eb9SGleb Smirnoff 
2723b3a8eb9SGleb Smirnoff static int
is_icmp_query(struct icmphdr * icmp)2733b3a8eb9SGleb Smirnoff is_icmp_query(struct icmphdr *icmp)
2743b3a8eb9SGleb Smirnoff {
2753b3a8eb9SGleb Smirnoff 	int type = icmp->icmp_type;
2763b3a8eb9SGleb Smirnoff 
2773b3a8eb9SGleb Smirnoff 	return (type <= ICMP_MAXTYPE && (TT & (1<<type)) );
2783b3a8eb9SGleb Smirnoff }
2793b3a8eb9SGleb Smirnoff #undef TT
2803b3a8eb9SGleb Smirnoff 
2813b3a8eb9SGleb Smirnoff /*
2823b3a8eb9SGleb Smirnoff  * The following checks use two arrays of 8 or 16 bits to store the
2833b3a8eb9SGleb Smirnoff  * bits that we want set or clear, respectively. They are in the
2843b3a8eb9SGleb Smirnoff  * low and high half of cmd->arg1 or cmd->d[0].
2853b3a8eb9SGleb Smirnoff  *
2863b3a8eb9SGleb Smirnoff  * We scan options and store the bits we find set. We succeed if
2873b3a8eb9SGleb Smirnoff  *
2883b3a8eb9SGleb Smirnoff  *	(want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
2893b3a8eb9SGleb Smirnoff  *
2903b3a8eb9SGleb Smirnoff  * The code is sometimes optimized not to store additional variables.
2913b3a8eb9SGleb Smirnoff  */
2923b3a8eb9SGleb Smirnoff 
2933b3a8eb9SGleb Smirnoff static int
flags_match(ipfw_insn * cmd,u_int8_t bits)2943b3a8eb9SGleb Smirnoff flags_match(ipfw_insn *cmd, u_int8_t bits)
2953b3a8eb9SGleb Smirnoff {
2963b3a8eb9SGleb Smirnoff 	u_char want_clear;
2973b3a8eb9SGleb Smirnoff 	bits = ~bits;
2983b3a8eb9SGleb Smirnoff 
2993b3a8eb9SGleb Smirnoff 	if ( ((cmd->arg1 & 0xff) & bits) != 0)
3003b3a8eb9SGleb Smirnoff 		return 0; /* some bits we want set were clear */
3013b3a8eb9SGleb Smirnoff 	want_clear = (cmd->arg1 >> 8) & 0xff;
3023b3a8eb9SGleb Smirnoff 	if ( (want_clear & bits) != want_clear)
3033b3a8eb9SGleb Smirnoff 		return 0; /* some bits we want clear were set */
3043b3a8eb9SGleb Smirnoff 	return 1;
3053b3a8eb9SGleb Smirnoff }
3063b3a8eb9SGleb Smirnoff 
3073b3a8eb9SGleb Smirnoff static int
ipopts_match(struct ip * ip,ipfw_insn * cmd)3083b3a8eb9SGleb Smirnoff ipopts_match(struct ip *ip, ipfw_insn *cmd)
3093b3a8eb9SGleb Smirnoff {
3103b3a8eb9SGleb Smirnoff 	int optlen, bits = 0;
3113b3a8eb9SGleb Smirnoff 	u_char *cp = (u_char *)(ip + 1);
3123b3a8eb9SGleb Smirnoff 	int x = (ip->ip_hl << 2) - sizeof (struct ip);
3133b3a8eb9SGleb Smirnoff 
3143b3a8eb9SGleb Smirnoff 	for (; x > 0; x -= optlen, cp += optlen) {
3153b3a8eb9SGleb Smirnoff 		int opt = cp[IPOPT_OPTVAL];
3163b3a8eb9SGleb Smirnoff 
3173b3a8eb9SGleb Smirnoff 		if (opt == IPOPT_EOL)
3183b3a8eb9SGleb Smirnoff 			break;
3193b3a8eb9SGleb Smirnoff 		if (opt == IPOPT_NOP)
3203b3a8eb9SGleb Smirnoff 			optlen = 1;
3213b3a8eb9SGleb Smirnoff 		else {
3223b3a8eb9SGleb Smirnoff 			optlen = cp[IPOPT_OLEN];
3233b3a8eb9SGleb Smirnoff 			if (optlen <= 0 || optlen > x)
3243b3a8eb9SGleb Smirnoff 				return 0; /* invalid or truncated */
3253b3a8eb9SGleb Smirnoff 		}
3263b3a8eb9SGleb Smirnoff 		switch (opt) {
3273b3a8eb9SGleb Smirnoff 		default:
3283b3a8eb9SGleb Smirnoff 			break;
3293b3a8eb9SGleb Smirnoff 
3303b3a8eb9SGleb Smirnoff 		case IPOPT_LSRR:
3313b3a8eb9SGleb Smirnoff 			bits |= IP_FW_IPOPT_LSRR;
3323b3a8eb9SGleb Smirnoff 			break;
3333b3a8eb9SGleb Smirnoff 
3343b3a8eb9SGleb Smirnoff 		case IPOPT_SSRR:
3353b3a8eb9SGleb Smirnoff 			bits |= IP_FW_IPOPT_SSRR;
3363b3a8eb9SGleb Smirnoff 			break;
3373b3a8eb9SGleb Smirnoff 
3383b3a8eb9SGleb Smirnoff 		case IPOPT_RR:
3393b3a8eb9SGleb Smirnoff 			bits |= IP_FW_IPOPT_RR;
3403b3a8eb9SGleb Smirnoff 			break;
3413b3a8eb9SGleb Smirnoff 
3423b3a8eb9SGleb Smirnoff 		case IPOPT_TS:
3433b3a8eb9SGleb Smirnoff 			bits |= IP_FW_IPOPT_TS;
3443b3a8eb9SGleb Smirnoff 			break;
3453b3a8eb9SGleb Smirnoff 		}
3463b3a8eb9SGleb Smirnoff 	}
3473b3a8eb9SGleb Smirnoff 	return (flags_match(cmd, bits));
3483b3a8eb9SGleb Smirnoff }
3493b3a8eb9SGleb Smirnoff 
3501ae74c13SAndrey V. Elsukov /*
3511ae74c13SAndrey V. Elsukov  * Parse TCP options. The logic copied from tcp_dooptions().
3521ae74c13SAndrey V. Elsukov  */
3533b3a8eb9SGleb Smirnoff static int
tcpopts_parse(const struct tcphdr * tcp,uint16_t * mss)3541ae74c13SAndrey V. Elsukov tcpopts_parse(const struct tcphdr *tcp, uint16_t *mss)
3553b3a8eb9SGleb Smirnoff {
3561ae74c13SAndrey V. Elsukov 	const u_char *cp = (const u_char *)(tcp + 1);
357978f2d17SAndrey V. Elsukov 	int optlen, bits = 0;
3581ae74c13SAndrey V. Elsukov 	int cnt = (tcp->th_off << 2) - sizeof(struct tcphdr);
3593b3a8eb9SGleb Smirnoff 
3601ae74c13SAndrey V. Elsukov 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3613b3a8eb9SGleb Smirnoff 		int opt = cp[0];
3623b3a8eb9SGleb Smirnoff 		if (opt == TCPOPT_EOL)
3633b3a8eb9SGleb Smirnoff 			break;
3643b3a8eb9SGleb Smirnoff 		if (opt == TCPOPT_NOP)
3653b3a8eb9SGleb Smirnoff 			optlen = 1;
3663b3a8eb9SGleb Smirnoff 		else {
3671ae74c13SAndrey V. Elsukov 			if (cnt < 2)
3681ae74c13SAndrey V. Elsukov 				break;
3693b3a8eb9SGleb Smirnoff 			optlen = cp[1];
3701ae74c13SAndrey V. Elsukov 			if (optlen < 2 || optlen > cnt)
3713b3a8eb9SGleb Smirnoff 				break;
3723b3a8eb9SGleb Smirnoff 		}
3733b3a8eb9SGleb Smirnoff 
3743b3a8eb9SGleb Smirnoff 		switch (opt) {
3753b3a8eb9SGleb Smirnoff 		default:
3763b3a8eb9SGleb Smirnoff 			break;
3773b3a8eb9SGleb Smirnoff 
3783b3a8eb9SGleb Smirnoff 		case TCPOPT_MAXSEG:
3791ae74c13SAndrey V. Elsukov 			if (optlen != TCPOLEN_MAXSEG)
3801ae74c13SAndrey V. Elsukov 				break;
3813b3a8eb9SGleb Smirnoff 			bits |= IP_FW_TCPOPT_MSS;
382978f2d17SAndrey V. Elsukov 			if (mss != NULL)
383978f2d17SAndrey V. Elsukov 				*mss = be16dec(cp + 2);
3843b3a8eb9SGleb Smirnoff 			break;
3853b3a8eb9SGleb Smirnoff 
3863b3a8eb9SGleb Smirnoff 		case TCPOPT_WINDOW:
3871ae74c13SAndrey V. Elsukov 			if (optlen == TCPOLEN_WINDOW)
3883b3a8eb9SGleb Smirnoff 				bits |= IP_FW_TCPOPT_WINDOW;
3893b3a8eb9SGleb Smirnoff 			break;
3903b3a8eb9SGleb Smirnoff 
3913b3a8eb9SGleb Smirnoff 		case TCPOPT_SACK_PERMITTED:
3921ae74c13SAndrey V. Elsukov 			if (optlen == TCPOLEN_SACK_PERMITTED)
3931ae74c13SAndrey V. Elsukov 				bits |= IP_FW_TCPOPT_SACK;
3941ae74c13SAndrey V. Elsukov 			break;
3951ae74c13SAndrey V. Elsukov 
3963b3a8eb9SGleb Smirnoff 		case TCPOPT_SACK:
3971ae74c13SAndrey V. Elsukov 			if (optlen > 2 && (optlen - 2) % TCPOLEN_SACK == 0)
3983b3a8eb9SGleb Smirnoff 				bits |= IP_FW_TCPOPT_SACK;
3993b3a8eb9SGleb Smirnoff 			break;
4003b3a8eb9SGleb Smirnoff 
4013b3a8eb9SGleb Smirnoff 		case TCPOPT_TIMESTAMP:
4021ae74c13SAndrey V. Elsukov 			if (optlen == TCPOLEN_TIMESTAMP)
4033b3a8eb9SGleb Smirnoff 				bits |= IP_FW_TCPOPT_TS;
4043b3a8eb9SGleb Smirnoff 			break;
405978f2d17SAndrey V. Elsukov 		}
406978f2d17SAndrey V. Elsukov 	}
407978f2d17SAndrey V. Elsukov 	return (bits);
408978f2d17SAndrey V. Elsukov }
4093b3a8eb9SGleb Smirnoff 
410978f2d17SAndrey V. Elsukov static int
tcpopts_match(struct tcphdr * tcp,ipfw_insn * cmd)411978f2d17SAndrey V. Elsukov tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd)
412978f2d17SAndrey V. Elsukov {
413978f2d17SAndrey V. Elsukov 
414978f2d17SAndrey V. Elsukov 	return (flags_match(cmd, tcpopts_parse(tcp, NULL)));
4153b3a8eb9SGleb Smirnoff }
4163b3a8eb9SGleb Smirnoff 
4173b3a8eb9SGleb Smirnoff static int
iface_match(struct ifnet * ifp,ipfw_insn_if * cmd,struct ip_fw_chain * chain,uint32_t * tablearg)41868394ec8SAlexander V. Chernikov iface_match(struct ifnet *ifp, ipfw_insn_if *cmd, struct ip_fw_chain *chain,
41968394ec8SAlexander V. Chernikov     uint32_t *tablearg)
4203b3a8eb9SGleb Smirnoff {
42168394ec8SAlexander V. Chernikov 
4223b3a8eb9SGleb Smirnoff 	if (ifp == NULL)	/* no iface with this packet, match fails */
42368394ec8SAlexander V. Chernikov 		return (0);
42468394ec8SAlexander V. Chernikov 
4253b3a8eb9SGleb Smirnoff 	/* Check by name or by IP address */
4263b3a8eb9SGleb Smirnoff 	if (cmd->name[0] != '\0') { /* match by name */
4273b3a8eb9SGleb Smirnoff 		if (cmd->name[0] == '\1') /* use tablearg to match */
42854e5669dSAndrey V. Elsukov 			return ipfw_lookup_table(chain, cmd->p.kidx, 0,
42968394ec8SAlexander V. Chernikov 			    &ifp->if_index, tablearg);
4303b3a8eb9SGleb Smirnoff 		/* Check name */
4313b3a8eb9SGleb Smirnoff 		if (cmd->p.glob) {
4323b3a8eb9SGleb Smirnoff 			if (fnmatch(cmd->name, ifp->if_xname, 0) == 0)
4333b3a8eb9SGleb Smirnoff 				return(1);
4343b3a8eb9SGleb Smirnoff 		} else {
4353b3a8eb9SGleb Smirnoff 			if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
4363b3a8eb9SGleb Smirnoff 				return(1);
4373b3a8eb9SGleb Smirnoff 		}
4383b3a8eb9SGleb Smirnoff 	} else {
439f783a35cSLuigi Rizzo #if !defined(USERSPACE) && defined(__FreeBSD__)	/* and OSX too ? */
4403b3a8eb9SGleb Smirnoff 		struct ifaddr *ia;
4413b3a8eb9SGleb Smirnoff 
442961c033eSGleb Smirnoff 		NET_EPOCH_ASSERT();
443961c033eSGleb Smirnoff 
444d7c5a620SMatt Macy 		CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
4453b3a8eb9SGleb Smirnoff 			if (ia->ifa_addr->sa_family != AF_INET)
4463b3a8eb9SGleb Smirnoff 				continue;
4473b3a8eb9SGleb Smirnoff 			if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
448961c033eSGleb Smirnoff 			    (ia->ifa_addr))->sin_addr.s_addr)
4493b3a8eb9SGleb Smirnoff 				return (1);	/* match */
4503b3a8eb9SGleb Smirnoff 		}
4513b3a8eb9SGleb Smirnoff #endif /* __FreeBSD__ */
4523b3a8eb9SGleb Smirnoff 	}
4533b3a8eb9SGleb Smirnoff 	return(0);	/* no match, fail ... */
4543b3a8eb9SGleb Smirnoff }
4553b3a8eb9SGleb Smirnoff 
4563b3a8eb9SGleb Smirnoff /*
4573b3a8eb9SGleb Smirnoff  * The verify_path function checks if a route to the src exists and
4583b3a8eb9SGleb Smirnoff  * if it is reachable via ifp (when provided).
4593b3a8eb9SGleb Smirnoff  *
4603b3a8eb9SGleb Smirnoff  * The 'verrevpath' option checks that the interface that an IP packet
4613b3a8eb9SGleb Smirnoff  * arrives on is the same interface that traffic destined for the
4623b3a8eb9SGleb Smirnoff  * packet's source address would be routed out of.
4633b3a8eb9SGleb Smirnoff  * The 'versrcreach' option just checks that the source address is
4643b3a8eb9SGleb Smirnoff  * reachable via any route (except default) in the routing table.
4653b3a8eb9SGleb Smirnoff  * These two are a measure to block forged packets. This is also
4663b3a8eb9SGleb Smirnoff  * commonly known as "anti-spoofing" or Unicast Reverse Path
4673b3a8eb9SGleb Smirnoff  * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs
4683b3a8eb9SGleb Smirnoff  * is purposely reminiscent of the Cisco IOS command,
4693b3a8eb9SGleb Smirnoff  *
4703b3a8eb9SGleb Smirnoff  *   ip verify unicast reverse-path
4713b3a8eb9SGleb Smirnoff  *   ip verify unicast source reachable-via any
4723b3a8eb9SGleb Smirnoff  *
4733b3a8eb9SGleb Smirnoff  * which implements the same functionality. But note that the syntax
4743b3a8eb9SGleb Smirnoff  * is misleading, and the check may be performed on all IP packets
4753b3a8eb9SGleb Smirnoff  * whether unicast, multicast, or broadcast.
4763b3a8eb9SGleb Smirnoff  */
4773b3a8eb9SGleb Smirnoff static int
verify_path(struct in_addr src,struct ifnet * ifp,u_int fib)4783b3a8eb9SGleb Smirnoff verify_path(struct in_addr src, struct ifnet *ifp, u_int fib)
4793b3a8eb9SGleb Smirnoff {
480f783a35cSLuigi Rizzo #if defined(USERSPACE) || !defined(__FreeBSD__)
4813b3a8eb9SGleb Smirnoff 	return 0;
4823b3a8eb9SGleb Smirnoff #else
4836ad7446cSAlexander V. Chernikov 	struct nhop_object *nh;
4843b3a8eb9SGleb Smirnoff 
4856ad7446cSAlexander V. Chernikov 	nh = fib4_lookup(fib, src, 0, NHR_NONE, 0);
4866ad7446cSAlexander V. Chernikov 	if (nh == NULL)
48765ff3638SAlexander V. Chernikov 		return (0);
4883b3a8eb9SGleb Smirnoff 
4893b3a8eb9SGleb Smirnoff 	/*
4903b3a8eb9SGleb Smirnoff 	 * If ifp is provided, check for equality with rtentry.
4913b3a8eb9SGleb Smirnoff 	 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
4923b3a8eb9SGleb Smirnoff 	 * in order to pass packets injected back by if_simloop():
493b1b9dcaeSGleb Smirnoff 	 * routing entry (via lo0) for our own address
4943b3a8eb9SGleb Smirnoff 	 * may exist, so we need to handle routing assymetry.
4953b3a8eb9SGleb Smirnoff 	 */
4966ad7446cSAlexander V. Chernikov 	if (ifp != NULL && ifp != nh->nh_aifp)
49765ff3638SAlexander V. Chernikov 		return (0);
4983b3a8eb9SGleb Smirnoff 
4993b3a8eb9SGleb Smirnoff 	/* if no ifp provided, check if rtentry is not default route */
5006ad7446cSAlexander V. Chernikov 	if (ifp == NULL && (nh->nh_flags & NHF_DEFAULT) != 0)
50165ff3638SAlexander V. Chernikov 		return (0);
5023b3a8eb9SGleb Smirnoff 
5033b3a8eb9SGleb Smirnoff 	/* or if this is a blackhole/reject route */
5046ad7446cSAlexander V. Chernikov 	if (ifp == NULL && (nh->nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
50565ff3638SAlexander V. Chernikov 		return (0);
5063b3a8eb9SGleb Smirnoff 
5073b3a8eb9SGleb Smirnoff 	/* found valid route */
5083b3a8eb9SGleb Smirnoff 	return 1;
5093b3a8eb9SGleb Smirnoff #endif /* __FreeBSD__ */
5103b3a8eb9SGleb Smirnoff }
5113b3a8eb9SGleb Smirnoff 
51230df59d5SAndrey V. Elsukov /*
513665c8a2eSMichael Tuexen  * Generate an SCTP packet containing an ABORT chunk. The verification tag
514665c8a2eSMichael Tuexen  * is given by vtag. The T-bit is set in the ABORT chunk if and only if
515665c8a2eSMichael Tuexen  * reflected is not 0.
516665c8a2eSMichael Tuexen  */
517665c8a2eSMichael Tuexen 
518665c8a2eSMichael Tuexen static struct mbuf *
ipfw_send_abort(struct mbuf * replyto,struct ipfw_flow_id * id,u_int32_t vtag,int reflected)519665c8a2eSMichael Tuexen ipfw_send_abort(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t vtag,
520665c8a2eSMichael Tuexen     int reflected)
521665c8a2eSMichael Tuexen {
522665c8a2eSMichael Tuexen 	struct mbuf *m;
523665c8a2eSMichael Tuexen 	struct ip *ip;
524665c8a2eSMichael Tuexen #ifdef INET6
525665c8a2eSMichael Tuexen 	struct ip6_hdr *ip6;
526665c8a2eSMichael Tuexen #endif
527665c8a2eSMichael Tuexen 	struct sctphdr *sctp;
528665c8a2eSMichael Tuexen 	struct sctp_chunkhdr *chunk;
529665c8a2eSMichael Tuexen 	u_int16_t hlen, plen, tlen;
530665c8a2eSMichael Tuexen 
531665c8a2eSMichael Tuexen 	MGETHDR(m, M_NOWAIT, MT_DATA);
532665c8a2eSMichael Tuexen 	if (m == NULL)
533665c8a2eSMichael Tuexen 		return (NULL);
534665c8a2eSMichael Tuexen 
535665c8a2eSMichael Tuexen 	M_SETFIB(m, id->fib);
536665c8a2eSMichael Tuexen #ifdef MAC
537665c8a2eSMichael Tuexen 	if (replyto != NULL)
538665c8a2eSMichael Tuexen 		mac_netinet_firewall_reply(replyto, m);
539665c8a2eSMichael Tuexen 	else
540665c8a2eSMichael Tuexen 		mac_netinet_firewall_send(m);
541665c8a2eSMichael Tuexen #else
542665c8a2eSMichael Tuexen 	(void)replyto;		/* don't warn about unused arg */
543665c8a2eSMichael Tuexen #endif
544665c8a2eSMichael Tuexen 
545665c8a2eSMichael Tuexen 	switch (id->addr_type) {
546665c8a2eSMichael Tuexen 	case 4:
547665c8a2eSMichael Tuexen 		hlen = sizeof(struct ip);
548665c8a2eSMichael Tuexen 		break;
549665c8a2eSMichael Tuexen #ifdef INET6
550665c8a2eSMichael Tuexen 	case 6:
551665c8a2eSMichael Tuexen 		hlen = sizeof(struct ip6_hdr);
552665c8a2eSMichael Tuexen 		break;
553665c8a2eSMichael Tuexen #endif
554665c8a2eSMichael Tuexen 	default:
555665c8a2eSMichael Tuexen 		/* XXX: log me?!? */
556665c8a2eSMichael Tuexen 		FREE_PKT(m);
557665c8a2eSMichael Tuexen 		return (NULL);
558665c8a2eSMichael Tuexen 	}
559665c8a2eSMichael Tuexen 	plen = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
560665c8a2eSMichael Tuexen 	tlen = hlen + plen;
561665c8a2eSMichael Tuexen 	m->m_data += max_linkhdr;
562665c8a2eSMichael Tuexen 	m->m_flags |= M_SKIP_FIREWALL;
563665c8a2eSMichael Tuexen 	m->m_pkthdr.len = m->m_len = tlen;
564665c8a2eSMichael Tuexen 	m->m_pkthdr.rcvif = NULL;
565665c8a2eSMichael Tuexen 	bzero(m->m_data, tlen);
566665c8a2eSMichael Tuexen 
567665c8a2eSMichael Tuexen 	switch (id->addr_type) {
568665c8a2eSMichael Tuexen 	case 4:
569665c8a2eSMichael Tuexen 		ip = mtod(m, struct ip *);
570665c8a2eSMichael Tuexen 
571665c8a2eSMichael Tuexen 		ip->ip_v = 4;
572665c8a2eSMichael Tuexen 		ip->ip_hl = sizeof(struct ip) >> 2;
573665c8a2eSMichael Tuexen 		ip->ip_tos = IPTOS_LOWDELAY;
574665c8a2eSMichael Tuexen 		ip->ip_len = htons(tlen);
575665c8a2eSMichael Tuexen 		ip->ip_id = htons(0);
576665c8a2eSMichael Tuexen 		ip->ip_off = htons(0);
577665c8a2eSMichael Tuexen 		ip->ip_ttl = V_ip_defttl;
578665c8a2eSMichael Tuexen 		ip->ip_p = IPPROTO_SCTP;
579665c8a2eSMichael Tuexen 		ip->ip_sum = 0;
580665c8a2eSMichael Tuexen 		ip->ip_src.s_addr = htonl(id->dst_ip);
581665c8a2eSMichael Tuexen 		ip->ip_dst.s_addr = htonl(id->src_ip);
582665c8a2eSMichael Tuexen 
583665c8a2eSMichael Tuexen 		sctp = (struct sctphdr *)(ip + 1);
584665c8a2eSMichael Tuexen 		break;
585665c8a2eSMichael Tuexen #ifdef INET6
586665c8a2eSMichael Tuexen 	case 6:
587665c8a2eSMichael Tuexen 		ip6 = mtod(m, struct ip6_hdr *);
588665c8a2eSMichael Tuexen 
589665c8a2eSMichael Tuexen 		ip6->ip6_vfc = IPV6_VERSION;
590665c8a2eSMichael Tuexen 		ip6->ip6_plen = htons(plen);
591665c8a2eSMichael Tuexen 		ip6->ip6_nxt = IPPROTO_SCTP;
592665c8a2eSMichael Tuexen 		ip6->ip6_hlim = IPV6_DEFHLIM;
593665c8a2eSMichael Tuexen 		ip6->ip6_src = id->dst_ip6;
594665c8a2eSMichael Tuexen 		ip6->ip6_dst = id->src_ip6;
595665c8a2eSMichael Tuexen 
596665c8a2eSMichael Tuexen 		sctp = (struct sctphdr *)(ip6 + 1);
597665c8a2eSMichael Tuexen 		break;
598665c8a2eSMichael Tuexen #endif
599665c8a2eSMichael Tuexen 	}
600665c8a2eSMichael Tuexen 
601665c8a2eSMichael Tuexen 	sctp->src_port = htons(id->dst_port);
602665c8a2eSMichael Tuexen 	sctp->dest_port = htons(id->src_port);
603665c8a2eSMichael Tuexen 	sctp->v_tag = htonl(vtag);
604665c8a2eSMichael Tuexen 	sctp->checksum = htonl(0);
605665c8a2eSMichael Tuexen 
606665c8a2eSMichael Tuexen 	chunk = (struct sctp_chunkhdr *)(sctp + 1);
607665c8a2eSMichael Tuexen 	chunk->chunk_type = SCTP_ABORT_ASSOCIATION;
608665c8a2eSMichael Tuexen 	chunk->chunk_flags = 0;
609665c8a2eSMichael Tuexen 	if (reflected != 0) {
610665c8a2eSMichael Tuexen 		chunk->chunk_flags |= SCTP_HAD_NO_TCB;
611665c8a2eSMichael Tuexen 	}
612665c8a2eSMichael Tuexen 	chunk->chunk_length = htons(sizeof(struct sctp_chunkhdr));
613665c8a2eSMichael Tuexen 
614665c8a2eSMichael Tuexen 	sctp->checksum = sctp_calculate_cksum(m, hlen);
615665c8a2eSMichael Tuexen 
616665c8a2eSMichael Tuexen 	return (m);
617665c8a2eSMichael Tuexen }
618665c8a2eSMichael Tuexen 
619665c8a2eSMichael Tuexen /*
62030df59d5SAndrey V. Elsukov  * Generate a TCP packet, containing either a RST or a keepalive.
62130df59d5SAndrey V. Elsukov  * When flags & TH_RST, we are sending a RST packet, because of a
62230df59d5SAndrey V. Elsukov  * "reset" action matched the packet.
62330df59d5SAndrey V. Elsukov  * Otherwise we are sending a keepalive, and flags & TH_
62430df59d5SAndrey V. Elsukov  * The 'replyto' mbuf is the mbuf being replied to, if any, and is required
62530df59d5SAndrey V. Elsukov  * so that MAC can label the reply appropriately.
62630df59d5SAndrey V. Elsukov  */
62730df59d5SAndrey V. Elsukov struct mbuf *
ipfw_send_pkt(struct mbuf * replyto,struct ipfw_flow_id * id,u_int32_t seq,u_int32_t ack,int flags)62830df59d5SAndrey V. Elsukov ipfw_send_pkt(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t seq,
62930df59d5SAndrey V. Elsukov     u_int32_t ack, int flags)
63030df59d5SAndrey V. Elsukov {
63130df59d5SAndrey V. Elsukov 	struct mbuf *m = NULL;		/* stupid compiler */
63230df59d5SAndrey V. Elsukov 	struct ip *h = NULL;		/* stupid compiler */
63330df59d5SAndrey V. Elsukov #ifdef INET6
63430df59d5SAndrey V. Elsukov 	struct ip6_hdr *h6 = NULL;
63530df59d5SAndrey V. Elsukov #endif
63630df59d5SAndrey V. Elsukov 	struct tcphdr *th = NULL;
63730df59d5SAndrey V. Elsukov 	int len, dir;
63830df59d5SAndrey V. Elsukov 
63930df59d5SAndrey V. Elsukov 	MGETHDR(m, M_NOWAIT, MT_DATA);
64030df59d5SAndrey V. Elsukov 	if (m == NULL)
64130df59d5SAndrey V. Elsukov 		return (NULL);
64230df59d5SAndrey V. Elsukov 
64330df59d5SAndrey V. Elsukov 	M_SETFIB(m, id->fib);
64430df59d5SAndrey V. Elsukov #ifdef MAC
64530df59d5SAndrey V. Elsukov 	if (replyto != NULL)
64630df59d5SAndrey V. Elsukov 		mac_netinet_firewall_reply(replyto, m);
64730df59d5SAndrey V. Elsukov 	else
64830df59d5SAndrey V. Elsukov 		mac_netinet_firewall_send(m);
64930df59d5SAndrey V. Elsukov #else
65030df59d5SAndrey V. Elsukov 	(void)replyto;		/* don't warn about unused arg */
65130df59d5SAndrey V. Elsukov #endif
65230df59d5SAndrey V. Elsukov 
65330df59d5SAndrey V. Elsukov 	switch (id->addr_type) {
65430df59d5SAndrey V. Elsukov 	case 4:
65530df59d5SAndrey V. Elsukov 		len = sizeof(struct ip) + sizeof(struct tcphdr);
65630df59d5SAndrey V. Elsukov 		break;
65730df59d5SAndrey V. Elsukov #ifdef INET6
65830df59d5SAndrey V. Elsukov 	case 6:
65930df59d5SAndrey V. Elsukov 		len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
66030df59d5SAndrey V. Elsukov 		break;
66130df59d5SAndrey V. Elsukov #endif
66230df59d5SAndrey V. Elsukov 	default:
66330df59d5SAndrey V. Elsukov 		/* XXX: log me?!? */
66430df59d5SAndrey V. Elsukov 		FREE_PKT(m);
66530df59d5SAndrey V. Elsukov 		return (NULL);
66630df59d5SAndrey V. Elsukov 	}
66730df59d5SAndrey V. Elsukov 	dir = ((flags & (TH_SYN | TH_RST)) == TH_SYN);
66830df59d5SAndrey V. Elsukov 
66930df59d5SAndrey V. Elsukov 	m->m_data += max_linkhdr;
67030df59d5SAndrey V. Elsukov 	m->m_flags |= M_SKIP_FIREWALL;
67130df59d5SAndrey V. Elsukov 	m->m_pkthdr.len = m->m_len = len;
67230df59d5SAndrey V. Elsukov 	m->m_pkthdr.rcvif = NULL;
67330df59d5SAndrey V. Elsukov 	bzero(m->m_data, len);
67430df59d5SAndrey V. Elsukov 
67530df59d5SAndrey V. Elsukov 	switch (id->addr_type) {
67630df59d5SAndrey V. Elsukov 	case 4:
67730df59d5SAndrey V. Elsukov 		h = mtod(m, struct ip *);
67830df59d5SAndrey V. Elsukov 
67930df59d5SAndrey V. Elsukov 		/* prepare for checksum */
68030df59d5SAndrey V. Elsukov 		h->ip_p = IPPROTO_TCP;
68130df59d5SAndrey V. Elsukov 		h->ip_len = htons(sizeof(struct tcphdr));
68230df59d5SAndrey V. Elsukov 		if (dir) {
68330df59d5SAndrey V. Elsukov 			h->ip_src.s_addr = htonl(id->src_ip);
68430df59d5SAndrey V. Elsukov 			h->ip_dst.s_addr = htonl(id->dst_ip);
68530df59d5SAndrey V. Elsukov 		} else {
68630df59d5SAndrey V. Elsukov 			h->ip_src.s_addr = htonl(id->dst_ip);
68730df59d5SAndrey V. Elsukov 			h->ip_dst.s_addr = htonl(id->src_ip);
68830df59d5SAndrey V. Elsukov 		}
68930df59d5SAndrey V. Elsukov 
69030df59d5SAndrey V. Elsukov 		th = (struct tcphdr *)(h + 1);
69130df59d5SAndrey V. Elsukov 		break;
69230df59d5SAndrey V. Elsukov #ifdef INET6
69330df59d5SAndrey V. Elsukov 	case 6:
69430df59d5SAndrey V. Elsukov 		h6 = mtod(m, struct ip6_hdr *);
69530df59d5SAndrey V. Elsukov 
69630df59d5SAndrey V. Elsukov 		/* prepare for checksum */
69730df59d5SAndrey V. Elsukov 		h6->ip6_nxt = IPPROTO_TCP;
69830df59d5SAndrey V. Elsukov 		h6->ip6_plen = htons(sizeof(struct tcphdr));
69930df59d5SAndrey V. Elsukov 		if (dir) {
70030df59d5SAndrey V. Elsukov 			h6->ip6_src = id->src_ip6;
70130df59d5SAndrey V. Elsukov 			h6->ip6_dst = id->dst_ip6;
70230df59d5SAndrey V. Elsukov 		} else {
70330df59d5SAndrey V. Elsukov 			h6->ip6_src = id->dst_ip6;
70430df59d5SAndrey V. Elsukov 			h6->ip6_dst = id->src_ip6;
70530df59d5SAndrey V. Elsukov 		}
70630df59d5SAndrey V. Elsukov 
70730df59d5SAndrey V. Elsukov 		th = (struct tcphdr *)(h6 + 1);
70830df59d5SAndrey V. Elsukov 		break;
70930df59d5SAndrey V. Elsukov #endif
71030df59d5SAndrey V. Elsukov 	}
71130df59d5SAndrey V. Elsukov 
71230df59d5SAndrey V. Elsukov 	if (dir) {
71330df59d5SAndrey V. Elsukov 		th->th_sport = htons(id->src_port);
71430df59d5SAndrey V. Elsukov 		th->th_dport = htons(id->dst_port);
71530df59d5SAndrey V. Elsukov 	} else {
71630df59d5SAndrey V. Elsukov 		th->th_sport = htons(id->dst_port);
71730df59d5SAndrey V. Elsukov 		th->th_dport = htons(id->src_port);
71830df59d5SAndrey V. Elsukov 	}
71930df59d5SAndrey V. Elsukov 	th->th_off = sizeof(struct tcphdr) >> 2;
72030df59d5SAndrey V. Elsukov 
72130df59d5SAndrey V. Elsukov 	if (flags & TH_RST) {
72230df59d5SAndrey V. Elsukov 		if (flags & TH_ACK) {
72330df59d5SAndrey V. Elsukov 			th->th_seq = htonl(ack);
7240fc7bdc9SRichard Scheffenegger 			tcp_set_flags(th, TH_RST);
72530df59d5SAndrey V. Elsukov 		} else {
72630df59d5SAndrey V. Elsukov 			if (flags & TH_SYN)
72730df59d5SAndrey V. Elsukov 				seq++;
72830df59d5SAndrey V. Elsukov 			th->th_ack = htonl(seq);
7290fc7bdc9SRichard Scheffenegger 			tcp_set_flags(th, TH_RST | TH_ACK);
73030df59d5SAndrey V. Elsukov 		}
73130df59d5SAndrey V. Elsukov 	} else {
73230df59d5SAndrey V. Elsukov 		/*
73330df59d5SAndrey V. Elsukov 		 * Keepalive - use caller provided sequence numbers
73430df59d5SAndrey V. Elsukov 		 */
73530df59d5SAndrey V. Elsukov 		th->th_seq = htonl(seq);
73630df59d5SAndrey V. Elsukov 		th->th_ack = htonl(ack);
7370fc7bdc9SRichard Scheffenegger 		tcp_set_flags(th, TH_ACK);
73830df59d5SAndrey V. Elsukov 	}
73930df59d5SAndrey V. Elsukov 
74030df59d5SAndrey V. Elsukov 	switch (id->addr_type) {
74130df59d5SAndrey V. Elsukov 	case 4:
74230df59d5SAndrey V. Elsukov 		th->th_sum = in_cksum(m, len);
74330df59d5SAndrey V. Elsukov 
74430df59d5SAndrey V. Elsukov 		/* finish the ip header */
74530df59d5SAndrey V. Elsukov 		h->ip_v = 4;
74630df59d5SAndrey V. Elsukov 		h->ip_hl = sizeof(*h) >> 2;
74730df59d5SAndrey V. Elsukov 		h->ip_tos = IPTOS_LOWDELAY;
74830df59d5SAndrey V. Elsukov 		h->ip_off = htons(0);
74930df59d5SAndrey V. Elsukov 		h->ip_len = htons(len);
75030df59d5SAndrey V. Elsukov 		h->ip_ttl = V_ip_defttl;
75130df59d5SAndrey V. Elsukov 		h->ip_sum = 0;
75230df59d5SAndrey V. Elsukov 		break;
75330df59d5SAndrey V. Elsukov #ifdef INET6
75430df59d5SAndrey V. Elsukov 	case 6:
75530df59d5SAndrey V. Elsukov 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(*h6),
75630df59d5SAndrey V. Elsukov 		    sizeof(struct tcphdr));
75730df59d5SAndrey V. Elsukov 
75830df59d5SAndrey V. Elsukov 		/* finish the ip6 header */
75930df59d5SAndrey V. Elsukov 		h6->ip6_vfc |= IPV6_VERSION;
76030df59d5SAndrey V. Elsukov 		h6->ip6_hlim = IPV6_DEFHLIM;
76130df59d5SAndrey V. Elsukov 		break;
76230df59d5SAndrey V. Elsukov #endif
76330df59d5SAndrey V. Elsukov 	}
76430df59d5SAndrey V. Elsukov 
76530df59d5SAndrey V. Elsukov 	return (m);
76630df59d5SAndrey V. Elsukov }
76730df59d5SAndrey V. Elsukov 
7683b3a8eb9SGleb Smirnoff #ifdef INET6
7693b3a8eb9SGleb Smirnoff /*
7703b3a8eb9SGleb Smirnoff  * ipv6 specific rules here...
7713b3a8eb9SGleb Smirnoff  */
7723b3a8eb9SGleb Smirnoff static __inline int
icmp6type_match(int type,ipfw_insn_u32 * cmd)7733b3a8eb9SGleb Smirnoff icmp6type_match(int type, ipfw_insn_u32 *cmd)
7743b3a8eb9SGleb Smirnoff {
7753b3a8eb9SGleb Smirnoff 	return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) );
7763b3a8eb9SGleb Smirnoff }
7773b3a8eb9SGleb Smirnoff 
7783b3a8eb9SGleb Smirnoff static int
flow6id_match(int curr_flow,ipfw_insn_u32 * cmd)7793b3a8eb9SGleb Smirnoff flow6id_match(int curr_flow, ipfw_insn_u32 *cmd)
7803b3a8eb9SGleb Smirnoff {
7813b3a8eb9SGleb Smirnoff 	int i;
7823b3a8eb9SGleb Smirnoff 	for (i=0; i <= cmd->o.arg1; ++i)
7833b3a8eb9SGleb Smirnoff 		if (curr_flow == cmd->d[i])
7843b3a8eb9SGleb Smirnoff 			return 1;
7853b3a8eb9SGleb Smirnoff 	return 0;
7863b3a8eb9SGleb Smirnoff }
7873b3a8eb9SGleb Smirnoff 
7883b3a8eb9SGleb Smirnoff /* support for IP6_*_ME opcodes */
789b13653baSAndrey V. Elsukov static const struct in6_addr lla_mask = {{{
790b13653baSAndrey V. Elsukov 	0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
791b13653baSAndrey V. Elsukov 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
792b13653baSAndrey V. Elsukov }}};
7933b3a8eb9SGleb Smirnoff 
794b13653baSAndrey V. Elsukov static int
ipfw_localip6(struct in6_addr * in6)795b13653baSAndrey V. Elsukov ipfw_localip6(struct in6_addr *in6)
796b13653baSAndrey V. Elsukov {
797b13653baSAndrey V. Elsukov 	struct rm_priotracker in6_ifa_tracker;
798b13653baSAndrey V. Elsukov 	struct in6_ifaddr *ia;
799b13653baSAndrey V. Elsukov 
800b13653baSAndrey V. Elsukov 	if (IN6_IS_ADDR_MULTICAST(in6))
801b13653baSAndrey V. Elsukov 		return (0);
802b13653baSAndrey V. Elsukov 
803b13653baSAndrey V. Elsukov 	if (!IN6_IS_ADDR_LINKLOCAL(in6))
804b13653baSAndrey V. Elsukov 		return (in6_localip(in6));
805b13653baSAndrey V. Elsukov 
806b13653baSAndrey V. Elsukov 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
807d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
808b13653baSAndrey V. Elsukov 		if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr))
809b13653baSAndrey V. Elsukov 			continue;
810b13653baSAndrey V. Elsukov 		if (IN6_ARE_MASKED_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
811b13653baSAndrey V. Elsukov 		    in6, &lla_mask)) {
812b13653baSAndrey V. Elsukov 			IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
813b13653baSAndrey V. Elsukov 			return (1);
8143b3a8eb9SGleb Smirnoff 		}
8153b3a8eb9SGleb Smirnoff 	}
816b13653baSAndrey V. Elsukov 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
817b13653baSAndrey V. Elsukov 	return (0);
8183b3a8eb9SGleb Smirnoff }
8193b3a8eb9SGleb Smirnoff 
8203b3a8eb9SGleb Smirnoff static int
verify_path6(struct in6_addr * src,struct ifnet * ifp,u_int fib)8213b3a8eb9SGleb Smirnoff verify_path6(struct in6_addr *src, struct ifnet *ifp, u_int fib)
8223b3a8eb9SGleb Smirnoff {
8236ad7446cSAlexander V. Chernikov 	struct nhop_object *nh;
8243b3a8eb9SGleb Smirnoff 
82565ff3638SAlexander V. Chernikov 	if (IN6_IS_SCOPE_LINKLOCAL(src))
82665ff3638SAlexander V. Chernikov 		return (1);
8273b3a8eb9SGleb Smirnoff 
8286ad7446cSAlexander V. Chernikov 	nh = fib6_lookup(fib, src, 0, NHR_NONE, 0);
8296ad7446cSAlexander V. Chernikov 	if (nh == NULL)
83065ff3638SAlexander V. Chernikov 		return (0);
8313b3a8eb9SGleb Smirnoff 
83265ff3638SAlexander V. Chernikov 	/* If ifp is provided, check for equality with route table. */
8336ad7446cSAlexander V. Chernikov 	if (ifp != NULL && ifp != nh->nh_aifp)
83465ff3638SAlexander V. Chernikov 		return (0);
8353b3a8eb9SGleb Smirnoff 
8363b3a8eb9SGleb Smirnoff 	/* if no ifp provided, check if rtentry is not default route */
8376ad7446cSAlexander V. Chernikov 	if (ifp == NULL && (nh->nh_flags & NHF_DEFAULT) != 0)
83865ff3638SAlexander V. Chernikov 		return (0);
8393b3a8eb9SGleb Smirnoff 
8403b3a8eb9SGleb Smirnoff 	/* or if this is a blackhole/reject route */
8416ad7446cSAlexander V. Chernikov 	if (ifp == NULL && (nh->nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
84265ff3638SAlexander V. Chernikov 		return (0);
8433b3a8eb9SGleb Smirnoff 
8443b3a8eb9SGleb Smirnoff 	/* found valid route */
8453b3a8eb9SGleb Smirnoff 	return 1;
8463b3a8eb9SGleb Smirnoff }
8473b3a8eb9SGleb Smirnoff 
8483b3a8eb9SGleb Smirnoff static int
is_icmp6_query(int icmp6_type)8493b3a8eb9SGleb Smirnoff is_icmp6_query(int icmp6_type)
8503b3a8eb9SGleb Smirnoff {
8513b3a8eb9SGleb Smirnoff 	if ((icmp6_type <= ICMP6_MAXTYPE) &&
8523b3a8eb9SGleb Smirnoff 	    (icmp6_type == ICMP6_ECHO_REQUEST ||
8533b3a8eb9SGleb Smirnoff 	    icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
8543b3a8eb9SGleb Smirnoff 	    icmp6_type == ICMP6_WRUREQUEST ||
8553b3a8eb9SGleb Smirnoff 	    icmp6_type == ICMP6_FQDN_QUERY ||
8563b3a8eb9SGleb Smirnoff 	    icmp6_type == ICMP6_NI_QUERY))
8573b3a8eb9SGleb Smirnoff 		return (1);
8583b3a8eb9SGleb Smirnoff 
8593b3a8eb9SGleb Smirnoff 	return (0);
8603b3a8eb9SGleb Smirnoff }
8613b3a8eb9SGleb Smirnoff 
86214a6bab1SAndrey V. Elsukov static int
map_icmp_unreach(int code)86314a6bab1SAndrey V. Elsukov map_icmp_unreach(int code)
86414a6bab1SAndrey V. Elsukov {
86514a6bab1SAndrey V. Elsukov 
86614a6bab1SAndrey V. Elsukov 	/* RFC 7915 p4.2 */
86714a6bab1SAndrey V. Elsukov 	switch (code) {
86814a6bab1SAndrey V. Elsukov 	case ICMP_UNREACH_NET:
86914a6bab1SAndrey V. Elsukov 	case ICMP_UNREACH_HOST:
87014a6bab1SAndrey V. Elsukov 	case ICMP_UNREACH_SRCFAIL:
87114a6bab1SAndrey V. Elsukov 	case ICMP_UNREACH_NET_UNKNOWN:
87214a6bab1SAndrey V. Elsukov 	case ICMP_UNREACH_HOST_UNKNOWN:
87314a6bab1SAndrey V. Elsukov 	case ICMP_UNREACH_TOSNET:
87414a6bab1SAndrey V. Elsukov 	case ICMP_UNREACH_TOSHOST:
87514a6bab1SAndrey V. Elsukov 		return (ICMP6_DST_UNREACH_NOROUTE);
87614a6bab1SAndrey V. Elsukov 	case ICMP_UNREACH_PORT:
87714a6bab1SAndrey V. Elsukov 		return (ICMP6_DST_UNREACH_NOPORT);
87814a6bab1SAndrey V. Elsukov 	default:
87914a6bab1SAndrey V. Elsukov 		/*
88014a6bab1SAndrey V. Elsukov 		 * Map the rest of codes into admit prohibited.
88114a6bab1SAndrey V. Elsukov 		 * XXX: unreach proto should be mapped into ICMPv6
88214a6bab1SAndrey V. Elsukov 		 * parameter problem, but we use only unreach type.
88314a6bab1SAndrey V. Elsukov 		 */
88414a6bab1SAndrey V. Elsukov 		return (ICMP6_DST_UNREACH_ADMIN);
88514a6bab1SAndrey V. Elsukov 	}
88614a6bab1SAndrey V. Elsukov }
88714a6bab1SAndrey V. Elsukov 
8883b3a8eb9SGleb Smirnoff static void
send_reject6(struct ip_fw_args * args,int code,u_int hlen,const struct ip6_hdr * ip6)8898e780285SRichard Scheffenegger send_reject6(struct ip_fw_args *args, int code, u_int hlen,
8908e780285SRichard Scheffenegger     const struct ip6_hdr *ip6)
8913b3a8eb9SGleb Smirnoff {
8923b3a8eb9SGleb Smirnoff 	struct mbuf *m;
8933b3a8eb9SGleb Smirnoff 
8943b3a8eb9SGleb Smirnoff 	m = args->m;
8953b3a8eb9SGleb Smirnoff 	if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) {
8968e780285SRichard Scheffenegger 		const struct tcphdr * tcp;
8978e780285SRichard Scheffenegger 		tcp = (const struct tcphdr *)((const char *)ip6 + hlen);
8983b3a8eb9SGleb Smirnoff 
8990fc7bdc9SRichard Scheffenegger 		if ((tcp_get_flags(tcp) & TH_RST) == 0) {
9003b3a8eb9SGleb Smirnoff 			struct mbuf *m0;
9013b3a8eb9SGleb Smirnoff 			m0 = ipfw_send_pkt(args->m, &(args->f_id),
9023b3a8eb9SGleb Smirnoff 			    ntohl(tcp->th_seq), ntohl(tcp->th_ack),
9030fc7bdc9SRichard Scheffenegger 			    tcp_get_flags(tcp) | TH_RST);
9043b3a8eb9SGleb Smirnoff 			if (m0 != NULL)
9053b3a8eb9SGleb Smirnoff 				ip6_output(m0, NULL, NULL, 0, NULL, NULL,
9063b3a8eb9SGleb Smirnoff 				    NULL);
9073b3a8eb9SGleb Smirnoff 		}
9083b3a8eb9SGleb Smirnoff 		FREE_PKT(m);
909665c8a2eSMichael Tuexen 	} else if (code == ICMP6_UNREACH_ABORT &&
910665c8a2eSMichael Tuexen 	    args->f_id.proto == IPPROTO_SCTP) {
911665c8a2eSMichael Tuexen 		struct mbuf *m0;
9128e780285SRichard Scheffenegger 		const struct sctphdr *sctp;
913665c8a2eSMichael Tuexen 		u_int32_t v_tag;
914665c8a2eSMichael Tuexen 		int reflected;
915665c8a2eSMichael Tuexen 
9168e780285SRichard Scheffenegger 		sctp = (const struct sctphdr *)((const char *)ip6 + hlen);
917665c8a2eSMichael Tuexen 		reflected = 1;
918665c8a2eSMichael Tuexen 		v_tag = ntohl(sctp->v_tag);
919665c8a2eSMichael Tuexen 		/* Investigate the first chunk header if available */
920665c8a2eSMichael Tuexen 		if (m->m_len >= hlen + sizeof(struct sctphdr) +
921665c8a2eSMichael Tuexen 		    sizeof(struct sctp_chunkhdr)) {
9228e780285SRichard Scheffenegger 			const struct sctp_chunkhdr *chunk;
923665c8a2eSMichael Tuexen 
9248e780285SRichard Scheffenegger 			chunk = (const struct sctp_chunkhdr *)(sctp + 1);
925665c8a2eSMichael Tuexen 			switch (chunk->chunk_type) {
926665c8a2eSMichael Tuexen 			case SCTP_INITIATION:
927665c8a2eSMichael Tuexen 				/*
928665c8a2eSMichael Tuexen 				 * Packets containing an INIT chunk MUST have
929665c8a2eSMichael Tuexen 				 * a zero v-tag.
930665c8a2eSMichael Tuexen 				 */
931665c8a2eSMichael Tuexen 				if (v_tag != 0) {
932665c8a2eSMichael Tuexen 					v_tag = 0;
933665c8a2eSMichael Tuexen 					break;
934665c8a2eSMichael Tuexen 				}
935665c8a2eSMichael Tuexen 				/* INIT chunk MUST NOT be bundled */
936665c8a2eSMichael Tuexen 				if (m->m_pkthdr.len >
937665c8a2eSMichael Tuexen 				    hlen + sizeof(struct sctphdr) +
938665c8a2eSMichael Tuexen 				    ntohs(chunk->chunk_length) + 3) {
939665c8a2eSMichael Tuexen 					break;
940665c8a2eSMichael Tuexen 				}
941665c8a2eSMichael Tuexen 				/* Use the initiate tag if available */
942665c8a2eSMichael Tuexen 				if ((m->m_len >= hlen + sizeof(struct sctphdr) +
943665c8a2eSMichael Tuexen 				    sizeof(struct sctp_chunkhdr) +
944665c8a2eSMichael Tuexen 				    offsetof(struct sctp_init, a_rwnd))) {
9458e780285SRichard Scheffenegger 					const struct sctp_init *init;
946665c8a2eSMichael Tuexen 
9478e780285SRichard Scheffenegger 					init = (const struct sctp_init *)(chunk + 1);
948665c8a2eSMichael Tuexen 					v_tag = ntohl(init->initiate_tag);
949665c8a2eSMichael Tuexen 					reflected = 0;
950665c8a2eSMichael Tuexen 				}
951665c8a2eSMichael Tuexen 				break;
952665c8a2eSMichael Tuexen 			case SCTP_ABORT_ASSOCIATION:
953665c8a2eSMichael Tuexen 				/*
954665c8a2eSMichael Tuexen 				 * If the packet contains an ABORT chunk, don't
955665c8a2eSMichael Tuexen 				 * reply.
956665c8a2eSMichael Tuexen 				 * XXX: We should search through all chunks,
957db462d94SEd Maste 				 * but do not do that to avoid attacks.
958665c8a2eSMichael Tuexen 				 */
959665c8a2eSMichael Tuexen 				v_tag = 0;
960665c8a2eSMichael Tuexen 				break;
961665c8a2eSMichael Tuexen 			}
962665c8a2eSMichael Tuexen 		}
963665c8a2eSMichael Tuexen 		if (v_tag == 0) {
964665c8a2eSMichael Tuexen 			m0 = NULL;
965665c8a2eSMichael Tuexen 		} else {
966665c8a2eSMichael Tuexen 			m0 = ipfw_send_abort(args->m, &(args->f_id), v_tag,
967665c8a2eSMichael Tuexen 			    reflected);
968665c8a2eSMichael Tuexen 		}
969665c8a2eSMichael Tuexen 		if (m0 != NULL)
970665c8a2eSMichael Tuexen 			ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
971665c8a2eSMichael Tuexen 		FREE_PKT(m);
972665c8a2eSMichael Tuexen 	} else if (code != ICMP6_UNREACH_RST && code != ICMP6_UNREACH_ABORT) {
973665c8a2eSMichael Tuexen 		/* Send an ICMPv6 unreach. */
9743b3a8eb9SGleb Smirnoff #if 0
9753b3a8eb9SGleb Smirnoff 		/*
9763b3a8eb9SGleb Smirnoff 		 * Unlike above, the mbufs need to line up with the ip6 hdr,
9773b3a8eb9SGleb Smirnoff 		 * as the contents are read. We need to m_adj() the
9783b3a8eb9SGleb Smirnoff 		 * needed amount.
9793b3a8eb9SGleb Smirnoff 		 * The mbuf will however be thrown away so we can adjust it.
9803b3a8eb9SGleb Smirnoff 		 * Remember we did an m_pullup on it already so we
9813b3a8eb9SGleb Smirnoff 		 * can make some assumptions about contiguousness.
9823b3a8eb9SGleb Smirnoff 		 */
9833b3a8eb9SGleb Smirnoff 		if (args->L3offset)
9843b3a8eb9SGleb Smirnoff 			m_adj(m, args->L3offset);
9853b3a8eb9SGleb Smirnoff #endif
9863b3a8eb9SGleb Smirnoff 		icmp6_error(m, ICMP6_DST_UNREACH, code, 0);
9873b3a8eb9SGleb Smirnoff 	} else
9883b3a8eb9SGleb Smirnoff 		FREE_PKT(m);
9893b3a8eb9SGleb Smirnoff 
9903b3a8eb9SGleb Smirnoff 	args->m = NULL;
9913b3a8eb9SGleb Smirnoff }
9923b3a8eb9SGleb Smirnoff 
9933b3a8eb9SGleb Smirnoff #endif /* INET6 */
9943b3a8eb9SGleb Smirnoff 
9953b3a8eb9SGleb Smirnoff /*
9963b3a8eb9SGleb Smirnoff  * sends a reject message, consuming the mbuf passed as an argument.
9973b3a8eb9SGleb Smirnoff  */
9983b3a8eb9SGleb Smirnoff static void
send_reject(struct ip_fw_args * args,int code,uint16_t mtu,int iplen,const struct ip * ip)9998e780285SRichard Scheffenegger send_reject(struct ip_fw_args *args, int code, uint16_t mtu, int iplen,
10008e780285SRichard Scheffenegger     const struct ip *ip)
10013b3a8eb9SGleb Smirnoff {
10023b3a8eb9SGleb Smirnoff #if 0
10033b3a8eb9SGleb Smirnoff 	/* XXX When ip is not guaranteed to be at mtod() we will
10043b3a8eb9SGleb Smirnoff 	 * need to account for this */
10053b3a8eb9SGleb Smirnoff 	 * The mbuf will however be thrown away so we can adjust it.
10063b3a8eb9SGleb Smirnoff 	 * Remember we did an m_pullup on it already so we
10073b3a8eb9SGleb Smirnoff 	 * can make some assumptions about contiguousness.
10083b3a8eb9SGleb Smirnoff 	 */
10093b3a8eb9SGleb Smirnoff 	if (args->L3offset)
10103b3a8eb9SGleb Smirnoff 		m_adj(m, args->L3offset);
10113b3a8eb9SGleb Smirnoff #endif
1012665c8a2eSMichael Tuexen 	if (code != ICMP_REJECT_RST && code != ICMP_REJECT_ABORT) {
1013665c8a2eSMichael Tuexen 		/* Send an ICMP unreach */
101405b9737fSGleb Smirnoff 		icmp_error(args->m, ICMP_UNREACH, code, 0L, mtu);
1015665c8a2eSMichael Tuexen 	} else if (code == ICMP_REJECT_RST && args->f_id.proto == IPPROTO_TCP) {
10163b3a8eb9SGleb Smirnoff 		struct tcphdr *const tcp =
10173b3a8eb9SGleb Smirnoff 		    L3HDR(struct tcphdr, mtod(args->m, struct ip *));
10180fc7bdc9SRichard Scheffenegger 		if ( (tcp_get_flags(tcp) & TH_RST) == 0) {
10193b3a8eb9SGleb Smirnoff 			struct mbuf *m;
10203b3a8eb9SGleb Smirnoff 			m = ipfw_send_pkt(args->m, &(args->f_id),
10213b3a8eb9SGleb Smirnoff 				ntohl(tcp->th_seq), ntohl(tcp->th_ack),
10220fc7bdc9SRichard Scheffenegger 				tcp_get_flags(tcp) | TH_RST);
10233b3a8eb9SGleb Smirnoff 			if (m != NULL)
10243b3a8eb9SGleb Smirnoff 				ip_output(m, NULL, NULL, 0, NULL, NULL);
10253b3a8eb9SGleb Smirnoff 		}
10263b3a8eb9SGleb Smirnoff 		FREE_PKT(args->m);
1027665c8a2eSMichael Tuexen 	} else if (code == ICMP_REJECT_ABORT &&
1028665c8a2eSMichael Tuexen 	    args->f_id.proto == IPPROTO_SCTP) {
1029665c8a2eSMichael Tuexen 		struct mbuf *m;
1030665c8a2eSMichael Tuexen 		struct sctphdr *sctp;
1031665c8a2eSMichael Tuexen 		struct sctp_chunkhdr *chunk;
1032665c8a2eSMichael Tuexen 		struct sctp_init *init;
1033665c8a2eSMichael Tuexen 		u_int32_t v_tag;
1034665c8a2eSMichael Tuexen 		int reflected;
1035665c8a2eSMichael Tuexen 
1036665c8a2eSMichael Tuexen 		sctp = L3HDR(struct sctphdr, mtod(args->m, struct ip *));
1037665c8a2eSMichael Tuexen 		reflected = 1;
1038665c8a2eSMichael Tuexen 		v_tag = ntohl(sctp->v_tag);
1039665c8a2eSMichael Tuexen 		if (iplen >= (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1040665c8a2eSMichael Tuexen 		    sizeof(struct sctp_chunkhdr)) {
1041665c8a2eSMichael Tuexen 			/* Look at the first chunk header if available */
1042665c8a2eSMichael Tuexen 			chunk = (struct sctp_chunkhdr *)(sctp + 1);
1043665c8a2eSMichael Tuexen 			switch (chunk->chunk_type) {
1044665c8a2eSMichael Tuexen 			case SCTP_INITIATION:
1045665c8a2eSMichael Tuexen 				/*
1046665c8a2eSMichael Tuexen 				 * Packets containing an INIT chunk MUST have
1047665c8a2eSMichael Tuexen 				 * a zero v-tag.
1048665c8a2eSMichael Tuexen 				 */
1049665c8a2eSMichael Tuexen 				if (v_tag != 0) {
1050665c8a2eSMichael Tuexen 					v_tag = 0;
1051665c8a2eSMichael Tuexen 					break;
1052665c8a2eSMichael Tuexen 				}
1053665c8a2eSMichael Tuexen 				/* INIT chunk MUST NOT be bundled */
1054665c8a2eSMichael Tuexen 				if (iplen >
1055665c8a2eSMichael Tuexen 				    (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1056665c8a2eSMichael Tuexen 				    ntohs(chunk->chunk_length) + 3) {
1057665c8a2eSMichael Tuexen 					break;
1058665c8a2eSMichael Tuexen 				}
1059665c8a2eSMichael Tuexen 				/* Use the initiate tag if available */
1060665c8a2eSMichael Tuexen 				if ((iplen >= (ip->ip_hl << 2) +
1061665c8a2eSMichael Tuexen 				    sizeof(struct sctphdr) +
1062665c8a2eSMichael Tuexen 				    sizeof(struct sctp_chunkhdr) +
1063665c8a2eSMichael Tuexen 				    offsetof(struct sctp_init, a_rwnd))) {
1064665c8a2eSMichael Tuexen 					init = (struct sctp_init *)(chunk + 1);
1065665c8a2eSMichael Tuexen 					v_tag = ntohl(init->initiate_tag);
1066665c8a2eSMichael Tuexen 					reflected = 0;
1067665c8a2eSMichael Tuexen 				}
1068665c8a2eSMichael Tuexen 				break;
1069665c8a2eSMichael Tuexen 			case SCTP_ABORT_ASSOCIATION:
1070665c8a2eSMichael Tuexen 				/*
1071665c8a2eSMichael Tuexen 				 * If the packet contains an ABORT chunk, don't
1072665c8a2eSMichael Tuexen 				 * reply.
1073665c8a2eSMichael Tuexen 				 * XXX: We should search through all chunks,
1074db462d94SEd Maste 				 * but do not do that to avoid attacks.
1075665c8a2eSMichael Tuexen 				 */
1076665c8a2eSMichael Tuexen 				v_tag = 0;
1077665c8a2eSMichael Tuexen 				break;
1078665c8a2eSMichael Tuexen 			}
1079665c8a2eSMichael Tuexen 		}
1080665c8a2eSMichael Tuexen 		if (v_tag == 0) {
1081665c8a2eSMichael Tuexen 			m = NULL;
1082665c8a2eSMichael Tuexen 		} else {
1083665c8a2eSMichael Tuexen 			m = ipfw_send_abort(args->m, &(args->f_id), v_tag,
1084665c8a2eSMichael Tuexen 			    reflected);
1085665c8a2eSMichael Tuexen 		}
1086665c8a2eSMichael Tuexen 		if (m != NULL)
1087665c8a2eSMichael Tuexen 			ip_output(m, NULL, NULL, 0, NULL, NULL);
1088665c8a2eSMichael Tuexen 		FREE_PKT(args->m);
10893b3a8eb9SGleb Smirnoff 	} else
10903b3a8eb9SGleb Smirnoff 		FREE_PKT(args->m);
10913b3a8eb9SGleb Smirnoff 	args->m = NULL;
10923b3a8eb9SGleb Smirnoff }
10933b3a8eb9SGleb Smirnoff 
10943b3a8eb9SGleb Smirnoff /*
10953b3a8eb9SGleb Smirnoff  * Support for uid/gid/jail lookup. These tests are expensive
10963b3a8eb9SGleb Smirnoff  * (because we may need to look into the list of active sockets)
10973b3a8eb9SGleb Smirnoff  * so we cache the results. ugid_lookupp is 0 if we have not
10983b3a8eb9SGleb Smirnoff  * yet done a lookup, 1 if we succeeded, and -1 if we tried
10993b3a8eb9SGleb Smirnoff  * and failed. The function always returns the match value.
11003b3a8eb9SGleb Smirnoff  * We could actually spare the variable and use *uc, setting
11013b3a8eb9SGleb Smirnoff  * it to '(void *)check_uidgid if we have no info, NULL if
11023b3a8eb9SGleb Smirnoff  * we tried and failed, or any other value if successful.
11033b3a8eb9SGleb Smirnoff  */
11043b3a8eb9SGleb Smirnoff static int
check_uidgid(ipfw_insn_u32 * insn,struct ip_fw_args * args,int * ugid_lookupp,struct ucred ** uc)11053b3a8eb9SGleb Smirnoff check_uidgid(ipfw_insn_u32 *insn, struct ip_fw_args *args, int *ugid_lookupp,
11063b3a8eb9SGleb Smirnoff     struct ucred **uc)
11073b3a8eb9SGleb Smirnoff {
1108f783a35cSLuigi Rizzo #if defined(USERSPACE)
1109f783a35cSLuigi Rizzo 	return 0;	// not supported in userspace
1110f783a35cSLuigi Rizzo #else
11113b3a8eb9SGleb Smirnoff #ifndef __FreeBSD__
11123b3a8eb9SGleb Smirnoff 	/* XXX */
11133b3a8eb9SGleb Smirnoff 	return cred_check(insn, proto, oif,
11143b3a8eb9SGleb Smirnoff 	    dst_ip, dst_port, src_ip, src_port,
11153b3a8eb9SGleb Smirnoff 	    (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb);
11163b3a8eb9SGleb Smirnoff #else  /* FreeBSD */
11173b3a8eb9SGleb Smirnoff 	struct in_addr src_ip, dst_ip;
11183b3a8eb9SGleb Smirnoff 	struct inpcbinfo *pi;
11193b3a8eb9SGleb Smirnoff 	struct ipfw_flow_id *id;
11203b3a8eb9SGleb Smirnoff 	struct inpcb *pcb, *inp;
11213b3a8eb9SGleb Smirnoff 	int lookupflags;
11223b3a8eb9SGleb Smirnoff 	int match;
11233b3a8eb9SGleb Smirnoff 
11243b3a8eb9SGleb Smirnoff 	id = &args->f_id;
11253b3a8eb9SGleb Smirnoff 	inp = args->inp;
11263b3a8eb9SGleb Smirnoff 
11273b3a8eb9SGleb Smirnoff 	/*
11283b3a8eb9SGleb Smirnoff 	 * Check to see if the UDP or TCP stack supplied us with
11293b3a8eb9SGleb Smirnoff 	 * the PCB. If so, rather then holding a lock and looking
11303b3a8eb9SGleb Smirnoff 	 * up the PCB, we can use the one that was supplied.
11313b3a8eb9SGleb Smirnoff 	 */
11323b3a8eb9SGleb Smirnoff 	if (inp && *ugid_lookupp == 0) {
11333b3a8eb9SGleb Smirnoff 		INP_LOCK_ASSERT(inp);
11343b3a8eb9SGleb Smirnoff 		if (inp->inp_socket != NULL) {
11353b3a8eb9SGleb Smirnoff 			*uc = crhold(inp->inp_cred);
11363b3a8eb9SGleb Smirnoff 			*ugid_lookupp = 1;
11373b3a8eb9SGleb Smirnoff 		} else
11383b3a8eb9SGleb Smirnoff 			*ugid_lookupp = -1;
11393b3a8eb9SGleb Smirnoff 	}
11403b3a8eb9SGleb Smirnoff 	/*
11413b3a8eb9SGleb Smirnoff 	 * If we have already been here and the packet has no
11423b3a8eb9SGleb Smirnoff 	 * PCB entry associated with it, then we can safely
11433b3a8eb9SGleb Smirnoff 	 * assume that this is a no match.
11443b3a8eb9SGleb Smirnoff 	 */
11453b3a8eb9SGleb Smirnoff 	if (*ugid_lookupp == -1)
11463b3a8eb9SGleb Smirnoff 		return (0);
11473b3a8eb9SGleb Smirnoff 	if (id->proto == IPPROTO_TCP) {
11483b3a8eb9SGleb Smirnoff 		lookupflags = 0;
11493b3a8eb9SGleb Smirnoff 		pi = &V_tcbinfo;
11503b3a8eb9SGleb Smirnoff 	} else if (id->proto == IPPROTO_UDP) {
11513b3a8eb9SGleb Smirnoff 		lookupflags = INPLOOKUP_WILDCARD;
11523b3a8eb9SGleb Smirnoff 		pi = &V_udbinfo;
1153d3834420SAndrey V. Elsukov 	} else if (id->proto == IPPROTO_UDPLITE) {
1154d3834420SAndrey V. Elsukov 		lookupflags = INPLOOKUP_WILDCARD;
1155d3834420SAndrey V. Elsukov 		pi = &V_ulitecbinfo;
11563b3a8eb9SGleb Smirnoff 	} else
11573b3a8eb9SGleb Smirnoff 		return 0;
11583b3a8eb9SGleb Smirnoff 	lookupflags |= INPLOOKUP_RLOCKPCB;
11593b3a8eb9SGleb Smirnoff 	match = 0;
11603b3a8eb9SGleb Smirnoff 	if (*ugid_lookupp == 0) {
11613b3a8eb9SGleb Smirnoff 		if (id->addr_type == 6) {
11623b3a8eb9SGleb Smirnoff #ifdef INET6
1163b7795b67SGleb Smirnoff 			if (args->flags & IPFW_ARGS_IN)
11643b3a8eb9SGleb Smirnoff 				pcb = in6_pcblookup_mbuf(pi,
11653b3a8eb9SGleb Smirnoff 				    &id->src_ip6, htons(id->src_port),
11663b3a8eb9SGleb Smirnoff 				    &id->dst_ip6, htons(id->dst_port),
1167b7795b67SGleb Smirnoff 				    lookupflags, NULL, args->m);
11683b3a8eb9SGleb Smirnoff 			else
11693b3a8eb9SGleb Smirnoff 				pcb = in6_pcblookup_mbuf(pi,
11703b3a8eb9SGleb Smirnoff 				    &id->dst_ip6, htons(id->dst_port),
11713b3a8eb9SGleb Smirnoff 				    &id->src_ip6, htons(id->src_port),
1172b7795b67SGleb Smirnoff 				    lookupflags, args->ifp, args->m);
11733b3a8eb9SGleb Smirnoff #else
11743b3a8eb9SGleb Smirnoff 			*ugid_lookupp = -1;
11753b3a8eb9SGleb Smirnoff 			return (0);
11763b3a8eb9SGleb Smirnoff #endif
11773b3a8eb9SGleb Smirnoff 		} else {
11783b3a8eb9SGleb Smirnoff 			src_ip.s_addr = htonl(id->src_ip);
11793b3a8eb9SGleb Smirnoff 			dst_ip.s_addr = htonl(id->dst_ip);
1180b7795b67SGleb Smirnoff 			if (args->flags & IPFW_ARGS_IN)
11813b3a8eb9SGleb Smirnoff 				pcb = in_pcblookup_mbuf(pi,
11823b3a8eb9SGleb Smirnoff 				    src_ip, htons(id->src_port),
11833b3a8eb9SGleb Smirnoff 				    dst_ip, htons(id->dst_port),
1184b7795b67SGleb Smirnoff 				    lookupflags, NULL, args->m);
11853b3a8eb9SGleb Smirnoff 			else
11863b3a8eb9SGleb Smirnoff 				pcb = in_pcblookup_mbuf(pi,
11873b3a8eb9SGleb Smirnoff 				    dst_ip, htons(id->dst_port),
11883b3a8eb9SGleb Smirnoff 				    src_ip, htons(id->src_port),
1189b7795b67SGleb Smirnoff 				    lookupflags, args->ifp, args->m);
11903b3a8eb9SGleb Smirnoff 		}
11913b3a8eb9SGleb Smirnoff 		if (pcb != NULL) {
11923b3a8eb9SGleb Smirnoff 			INP_RLOCK_ASSERT(pcb);
11933b3a8eb9SGleb Smirnoff 			*uc = crhold(pcb->inp_cred);
11943b3a8eb9SGleb Smirnoff 			*ugid_lookupp = 1;
11953b3a8eb9SGleb Smirnoff 			INP_RUNLOCK(pcb);
11963b3a8eb9SGleb Smirnoff 		}
11973b3a8eb9SGleb Smirnoff 		if (*ugid_lookupp == 0) {
11983b3a8eb9SGleb Smirnoff 			/*
11993b3a8eb9SGleb Smirnoff 			 * We tried and failed, set the variable to -1
12003b3a8eb9SGleb Smirnoff 			 * so we will not try again on this packet.
12013b3a8eb9SGleb Smirnoff 			 */
12023b3a8eb9SGleb Smirnoff 			*ugid_lookupp = -1;
12033b3a8eb9SGleb Smirnoff 			return (0);
12043b3a8eb9SGleb Smirnoff 		}
12053b3a8eb9SGleb Smirnoff 	}
12063b3a8eb9SGleb Smirnoff 	if (insn->o.opcode == O_UID)
12073b3a8eb9SGleb Smirnoff 		match = ((*uc)->cr_uid == (uid_t)insn->d[0]);
12083b3a8eb9SGleb Smirnoff 	else if (insn->o.opcode == O_GID)
12093b3a8eb9SGleb Smirnoff 		match = groupmember((gid_t)insn->d[0], *uc);
12103b3a8eb9SGleb Smirnoff 	else if (insn->o.opcode == O_JAIL)
12113b3a8eb9SGleb Smirnoff 		match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]);
12123b3a8eb9SGleb Smirnoff 	return (match);
12133b3a8eb9SGleb Smirnoff #endif /* __FreeBSD__ */
1214f783a35cSLuigi Rizzo #endif /* not supported in userspace */
12153b3a8eb9SGleb Smirnoff }
12163b3a8eb9SGleb Smirnoff 
12173b3a8eb9SGleb Smirnoff /*
12183b3a8eb9SGleb Smirnoff  * Helper function to set args with info on the rule after the matching
12193b3a8eb9SGleb Smirnoff  * one. slot is precise, whereas we guess rule_id as they are
12203b3a8eb9SGleb Smirnoff  * assigned sequentially.
12213b3a8eb9SGleb Smirnoff  */
12223b3a8eb9SGleb Smirnoff static inline void
set_match(struct ip_fw_args * args,int slot,struct ip_fw_chain * chain)12233b3a8eb9SGleb Smirnoff set_match(struct ip_fw_args *args, int slot,
12243b3a8eb9SGleb Smirnoff 	struct ip_fw_chain *chain)
12253b3a8eb9SGleb Smirnoff {
12263b3a8eb9SGleb Smirnoff 	args->rule.chain_id = chain->id;
12273b3a8eb9SGleb Smirnoff 	args->rule.slot = slot + 1; /* we use 0 as a marker */
12283b3a8eb9SGleb Smirnoff 	args->rule.rule_id = 1 + chain->map[slot]->id;
12293b3a8eb9SGleb Smirnoff 	args->rule.rulenum = chain->map[slot]->rulenum;
12301cdf23bcSAndrey V. Elsukov 	args->flags |= IPFW_ARGS_REF;
12313b3a8eb9SGleb Smirnoff }
12323b3a8eb9SGleb Smirnoff 
12334a77657cSAndrey V. Elsukov static uint32_t
jump_lookup_pos(struct ip_fw_chain * chain,struct ip_fw * f,uint32_t num,int tablearg,bool jump_backwards)12344a77657cSAndrey V. Elsukov jump_lookup_pos(struct ip_fw_chain *chain, struct ip_fw *f, uint32_t num,
12354a77657cSAndrey V. Elsukov     int tablearg, bool jump_backwards)
1236454189c1SAlexander V. Chernikov {
1237322e5efdSAndrey V. Elsukov 	int f_pos, i;
1238454189c1SAlexander V. Chernikov 
12394a77657cSAndrey V. Elsukov 	/*
12404a77657cSAndrey V. Elsukov 	 * Make sure we do not jump backward.
12414a77657cSAndrey V. Elsukov 	 */
1242322e5efdSAndrey V. Elsukov 	i = IP_FW_ARG_TABLEARG(chain, num, skipto);
12434a77657cSAndrey V. Elsukov 	if (!jump_backwards && i <= f->rulenum)
1244454189c1SAlexander V. Chernikov 		i = f->rulenum + 1;
1245322e5efdSAndrey V. Elsukov 
12464a77657cSAndrey V. Elsukov 	if (V_skipto_cache == 0)
1247454189c1SAlexander V. Chernikov 		f_pos = ipfw_find_rule(chain, i, 0);
12484a77657cSAndrey V. Elsukov 	else {
12494a77657cSAndrey V. Elsukov 		/*
12504a77657cSAndrey V. Elsukov 		 * Make sure we do not do out of bounds access.
12514a77657cSAndrey V. Elsukov 		 */
12524a77657cSAndrey V. Elsukov 		if (i >= IPFW_DEFAULT_RULE)
12534a77657cSAndrey V. Elsukov 			i = IPFW_DEFAULT_RULE - 1;
1254322e5efdSAndrey V. Elsukov 		f_pos = chain->idxmap[i];
12554a77657cSAndrey V. Elsukov 	}
1256454189c1SAlexander V. Chernikov 
1257454189c1SAlexander V. Chernikov 	return (f_pos);
1258454189c1SAlexander V. Chernikov }
1259322e5efdSAndrey V. Elsukov 
12604a77657cSAndrey V. Elsukov static uint32_t
jump(struct ip_fw_chain * chain,struct ip_fw * f,uint32_t num,int tablearg,bool jump_backwards)12614a77657cSAndrey V. Elsukov jump(struct ip_fw_chain *chain, struct ip_fw *f, uint32_t num,
12624a77657cSAndrey V. Elsukov     int tablearg, bool jump_backwards)
1263ce743e5cSAlexander V. Chernikov {
1264ce743e5cSAlexander V. Chernikov 	int f_pos;
1265ce743e5cSAlexander V. Chernikov 
1266322e5efdSAndrey V. Elsukov 	/* Can't use cache with IP_FW_TARG */
1267322e5efdSAndrey V. Elsukov 	if (num == IP_FW_TARG)
1268322e5efdSAndrey V. Elsukov 		return jump_lookup_pos(chain, f, num, tablearg, jump_backwards);
1269ce743e5cSAlexander V. Chernikov 
1270322e5efdSAndrey V. Elsukov 	/*
1271322e5efdSAndrey V. Elsukov 	 * If possible use cached f_pos (in f->cache.pos),
1272322e5efdSAndrey V. Elsukov 	 * whose version is written in f->cache.id (horrible hacks
1273322e5efdSAndrey V. Elsukov 	 * to avoid changing the ABI).
1274322e5efdSAndrey V. Elsukov 	 *
1275322e5efdSAndrey V. Elsukov 	 * Multiple threads can execute the same rule simultaneously,
1276322e5efdSAndrey V. Elsukov 	 * we need to ensure that cache.pos is updated before cache.id.
1277322e5efdSAndrey V. Elsukov 	 */
1278322e5efdSAndrey V. Elsukov 
1279322e5efdSAndrey V. Elsukov #ifdef __LP64__
1280322e5efdSAndrey V. Elsukov 	struct ip_fw_jump_cache cache;
1281322e5efdSAndrey V. Elsukov 
1282322e5efdSAndrey V. Elsukov 	cache.raw_value = f->cache.raw_value;
1283322e5efdSAndrey V. Elsukov 	if (cache.id == chain->id)
1284322e5efdSAndrey V. Elsukov 		return (cache.pos);
1285322e5efdSAndrey V. Elsukov 
1286322e5efdSAndrey V. Elsukov 	f_pos = jump_lookup_pos(chain, f, num, tablearg, jump_backwards);
1287322e5efdSAndrey V. Elsukov 
1288322e5efdSAndrey V. Elsukov 	cache.pos = f_pos;
1289322e5efdSAndrey V. Elsukov 	cache.id = chain->id;
1290322e5efdSAndrey V. Elsukov 	f->cache.raw_value = cache.raw_value;
1291322e5efdSAndrey V. Elsukov #else
1292322e5efdSAndrey V. Elsukov 	if (f->cache.id == chain->id) {
1293322e5efdSAndrey V. Elsukov 		/* Load pos after id */
1294322e5efdSAndrey V. Elsukov 		atomic_thread_fence_acq();
1295322e5efdSAndrey V. Elsukov 		return (f->cache.pos);
1296322e5efdSAndrey V. Elsukov 	}
1297322e5efdSAndrey V. Elsukov 
1298322e5efdSAndrey V. Elsukov 	f_pos = jump_lookup_pos(chain, f, num, tablearg, jump_backwards);
1299322e5efdSAndrey V. Elsukov 
1300322e5efdSAndrey V. Elsukov 	f->cache.pos = f_pos;
1301322e5efdSAndrey V. Elsukov 	/* Store id after pos */
1302322e5efdSAndrey V. Elsukov 	atomic_thread_fence_rel();
1303322e5efdSAndrey V. Elsukov 	f->cache.id = chain->id;
1304322e5efdSAndrey V. Elsukov #endif /* !__LP64__ */
1305ce743e5cSAlexander V. Chernikov 	return (f_pos);
1306ce743e5cSAlexander V. Chernikov }
1307ce743e5cSAlexander V. Chernikov 
13080cba2b28SAlexander V. Chernikov #define	TARG(k, f)	IP_FW_ARG_TABLEARG(chain, k, f)
13094a77657cSAndrey V. Elsukov 
13104a77657cSAndrey V. Elsukov static inline int
tvalue_match(struct ip_fw_chain * ch,const ipfw_insn_table * cmd,uint32_t tablearg)13114a77657cSAndrey V. Elsukov tvalue_match(struct ip_fw_chain *ch, const ipfw_insn_table *cmd,
13124a77657cSAndrey V. Elsukov     uint32_t tablearg)
13134a77657cSAndrey V. Elsukov {
13144a77657cSAndrey V. Elsukov 	uint32_t tvalue;
13154a77657cSAndrey V. Elsukov 
13164a77657cSAndrey V. Elsukov 	switch (IPFW_TVALUE_TYPE(&cmd->o)) {
13174a77657cSAndrey V. Elsukov 	case TVALUE_PIPE:
13184a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, pipe);
13194a77657cSAndrey V. Elsukov 		break;
13204a77657cSAndrey V. Elsukov 	case TVALUE_DIVERT:
13214a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, divert);
13224a77657cSAndrey V. Elsukov 		break;
13234a77657cSAndrey V. Elsukov 	case TVALUE_SKIPTO:
13244a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, skipto);
13254a77657cSAndrey V. Elsukov 		break;
13264a77657cSAndrey V. Elsukov 	case TVALUE_NETGRAPH:
13274a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, netgraph);
13284a77657cSAndrey V. Elsukov 		break;
13294a77657cSAndrey V. Elsukov 	case TVALUE_FIB:
13304a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, fib);
13314a77657cSAndrey V. Elsukov 		break;
13324a77657cSAndrey V. Elsukov 	case TVALUE_NAT:
13334a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, nat);
13344a77657cSAndrey V. Elsukov 		break;
13354a77657cSAndrey V. Elsukov 	case TVALUE_NH4:
13364a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, nh4);
13374a77657cSAndrey V. Elsukov 		break;
13384a77657cSAndrey V. Elsukov 	case TVALUE_DSCP:
13394a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, dscp);
13404a77657cSAndrey V. Elsukov 		break;
13414a77657cSAndrey V. Elsukov 	case TVALUE_LIMIT:
13424a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, limit);
13434a77657cSAndrey V. Elsukov 		break;
13444a77657cSAndrey V. Elsukov 	case TVALUE_MARK:
13454a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, mark);
13464a77657cSAndrey V. Elsukov 		break;
13474a77657cSAndrey V. Elsukov 	case TVALUE_TAG:
13484a77657cSAndrey V. Elsukov 	default:
13494a77657cSAndrey V. Elsukov 		tvalue = TARG_VAL(ch, tablearg, tag);
13504a77657cSAndrey V. Elsukov 		break;
13514a77657cSAndrey V. Elsukov 	}
13524a77657cSAndrey V. Elsukov 	return (tvalue == cmd->value);
13534a77657cSAndrey V. Elsukov }
13544a77657cSAndrey V. Elsukov 
1355ce743e5cSAlexander V. Chernikov /*
13563b3a8eb9SGleb Smirnoff  * The main check routine for the firewall.
13573b3a8eb9SGleb Smirnoff  *
13583b3a8eb9SGleb Smirnoff  * All arguments are in args so we can modify them and return them
13593b3a8eb9SGleb Smirnoff  * back to the caller.
13603b3a8eb9SGleb Smirnoff  *
13613b3a8eb9SGleb Smirnoff  * Parameters:
13623b3a8eb9SGleb Smirnoff  *
13633b3a8eb9SGleb Smirnoff  *	args->m	(in/out) The packet; we set to NULL when/if we nuke it.
13643b3a8eb9SGleb Smirnoff  *		Starts with the IP header.
13653b3a8eb9SGleb Smirnoff  *	args->L3offset	Number of bytes bypassed if we came from L2.
13663b3a8eb9SGleb Smirnoff  *			e.g. often sizeof(eh)  ** NOTYET **
1367b7795b67SGleb Smirnoff  *	args->ifp	Incoming or outgoing interface.
13683b3a8eb9SGleb Smirnoff  *	args->divert_rule (in/out)
13693b3a8eb9SGleb Smirnoff  *		Skip up to the first rule past this rule number;
13703b3a8eb9SGleb Smirnoff  *		upon return, non-zero port number for divert or tee.
13713b3a8eb9SGleb Smirnoff  *
13723b3a8eb9SGleb Smirnoff  *	args->rule	Pointer to the last matching rule (in/out)
13733b3a8eb9SGleb Smirnoff  *	args->next_hop	Socket we are forwarding to (out).
13743b3a8eb9SGleb Smirnoff  *	args->next_hop6	IPv6 next hop we are forwarding to (out).
13753b3a8eb9SGleb Smirnoff  *	args->f_id	Addresses grabbed from the packet (out)
13763b3a8eb9SGleb Smirnoff  * 	args->rule.info	a cookie depending on rule action
13773b3a8eb9SGleb Smirnoff  *
13783b3a8eb9SGleb Smirnoff  * Return value:
13793b3a8eb9SGleb Smirnoff  *
13803b3a8eb9SGleb Smirnoff  *	IP_FW_PASS	the packet must be accepted
13813b3a8eb9SGleb Smirnoff  *	IP_FW_DENY	the packet must be dropped
13823b3a8eb9SGleb Smirnoff  *	IP_FW_DIVERT	divert packet, port in m_tag
13833b3a8eb9SGleb Smirnoff  *	IP_FW_TEE	tee packet, port in m_tag
13843b3a8eb9SGleb Smirnoff  *	IP_FW_DUMMYNET	to dummynet, pipe in args->cookie
13853b3a8eb9SGleb Smirnoff  *	IP_FW_NETGRAPH	into netgraph, cookie args->cookie
13863b3a8eb9SGleb Smirnoff  *		args->rule contains the matching rule,
13873b3a8eb9SGleb Smirnoff  *		args->rule.info has additional information.
13883b3a8eb9SGleb Smirnoff  *
13893b3a8eb9SGleb Smirnoff  */
13903b3a8eb9SGleb Smirnoff int
ipfw_chk(struct ip_fw_args * args)13913b3a8eb9SGleb Smirnoff ipfw_chk(struct ip_fw_args *args)
13923b3a8eb9SGleb Smirnoff {
13933b3a8eb9SGleb Smirnoff 
13943b3a8eb9SGleb Smirnoff 	/*
13953b3a8eb9SGleb Smirnoff 	 * Local variables holding state while processing a packet:
13963b3a8eb9SGleb Smirnoff 	 *
13973b3a8eb9SGleb Smirnoff 	 * IMPORTANT NOTE: to speed up the processing of rules, there
13983b3a8eb9SGleb Smirnoff 	 * are some assumption on the values of the variables, which
13993b3a8eb9SGleb Smirnoff 	 * are documented here. Should you change them, please check
14003b3a8eb9SGleb Smirnoff 	 * the implementation of the various instructions to make sure
14013b3a8eb9SGleb Smirnoff 	 * that they still work.
14023b3a8eb9SGleb Smirnoff 	 *
14033b3a8eb9SGleb Smirnoff 	 * m | args->m	Pointer to the mbuf, as received from the caller.
14043b3a8eb9SGleb Smirnoff 	 *	It may change if ipfw_chk() does an m_pullup, or if it
14053b3a8eb9SGleb Smirnoff 	 *	consumes the packet because it calls send_reject().
14063b3a8eb9SGleb Smirnoff 	 *	XXX This has to change, so that ipfw_chk() never modifies
14073b3a8eb9SGleb Smirnoff 	 *	or consumes the buffer.
1408f355cb3eSGleb Smirnoff 	 *	OR
1409f355cb3eSGleb Smirnoff 	 * args->mem	Pointer to contigous memory chunk.
1410f355cb3eSGleb Smirnoff 	 * ip	Is the beginning of the ip(4 or 6) header.
1411f355cb3eSGleb Smirnoff 	 * eh	Ethernet header in case if input is Layer2.
14123b3a8eb9SGleb Smirnoff 	 */
1413f355cb3eSGleb Smirnoff 	struct mbuf *m;
1414f355cb3eSGleb Smirnoff 	struct ip *ip;
1415f355cb3eSGleb Smirnoff 	struct ether_header *eh;
14163b3a8eb9SGleb Smirnoff 
14173b3a8eb9SGleb Smirnoff 	/*
14183b3a8eb9SGleb Smirnoff 	 * For rules which contain uid/gid or jail constraints, cache
14193b3a8eb9SGleb Smirnoff 	 * a copy of the users credentials after the pcb lookup has been
14203b3a8eb9SGleb Smirnoff 	 * executed. This will speed up the processing of rules with
14213b3a8eb9SGleb Smirnoff 	 * these types of constraints, as well as decrease contention
14223b3a8eb9SGleb Smirnoff 	 * on pcb related locks.
14233b3a8eb9SGleb Smirnoff 	 */
14243b3a8eb9SGleb Smirnoff #ifndef __FreeBSD__
14253b3a8eb9SGleb Smirnoff 	struct bsd_ucred ucred_cache;
14263b3a8eb9SGleb Smirnoff #else
14273b3a8eb9SGleb Smirnoff 	struct ucred *ucred_cache = NULL;
14283b3a8eb9SGleb Smirnoff #endif
14294a77657cSAndrey V. Elsukov 	uint32_t f_pos = 0;	/* index of current rule in the array */
14303b3a8eb9SGleb Smirnoff 	int ucred_lookup = 0;
14313b3a8eb9SGleb Smirnoff 	int retval = 0;
1432b7795b67SGleb Smirnoff 	struct ifnet *oif, *iif;
14333b3a8eb9SGleb Smirnoff 
14343b3a8eb9SGleb Smirnoff 	/*
14353b3a8eb9SGleb Smirnoff 	 * hlen	The length of the IP header.
14363b3a8eb9SGleb Smirnoff 	 */
14373b3a8eb9SGleb Smirnoff 	u_int hlen = 0;		/* hlen >0 means we have an IP pkt */
14383b3a8eb9SGleb Smirnoff 
14393b3a8eb9SGleb Smirnoff 	/*
14403b3a8eb9SGleb Smirnoff 	 * offset	The offset of a fragment. offset != 0 means that
14413b3a8eb9SGleb Smirnoff 	 *	we have a fragment at this offset of an IPv4 packet.
14423b3a8eb9SGleb Smirnoff 	 *	offset == 0 means that (if this is an IPv4 packet)
14433b3a8eb9SGleb Smirnoff 	 *	this is the first or only fragment.
14443b3a8eb9SGleb Smirnoff 	 *	For IPv6 offset|ip6f_mf == 0 means there is no Fragment Header
1445a4641f4eSPedro F. Giffuni 	 *	or there is a single packet fragment (fragment header added
14463b3a8eb9SGleb Smirnoff 	 *	without needed).  We will treat a single packet fragment as if
14473b3a8eb9SGleb Smirnoff 	 *	there was no fragment header (or log/block depending on the
14483b3a8eb9SGleb Smirnoff 	 *	V_fw_permit_single_frag6 sysctl setting).
14493b3a8eb9SGleb Smirnoff 	 */
14503b3a8eb9SGleb Smirnoff 	u_short offset = 0;
14513b3a8eb9SGleb Smirnoff 	u_short ip6f_mf = 0;
14523b3a8eb9SGleb Smirnoff 
14533b3a8eb9SGleb Smirnoff 	/*
14543b3a8eb9SGleb Smirnoff 	 * Local copies of addresses. They are only valid if we have
14553b3a8eb9SGleb Smirnoff 	 * an IP packet.
14563b3a8eb9SGleb Smirnoff 	 *
14573b3a8eb9SGleb Smirnoff 	 * proto	The protocol. Set to 0 for non-ip packets,
14583b3a8eb9SGleb Smirnoff 	 *	or to the protocol read from the packet otherwise.
14593b3a8eb9SGleb Smirnoff 	 *	proto != 0 means that we have an IPv4 packet.
14603b3a8eb9SGleb Smirnoff 	 *
14613b3a8eb9SGleb Smirnoff 	 * src_port, dst_port	port numbers, in HOST format. Only
14623b3a8eb9SGleb Smirnoff 	 *	valid for TCP and UDP packets.
14633b3a8eb9SGleb Smirnoff 	 *
14643b3a8eb9SGleb Smirnoff 	 * src_ip, dst_ip	ip addresses, in NETWORK format.
14653b3a8eb9SGleb Smirnoff 	 *	Only valid for IPv4 packets.
14663b3a8eb9SGleb Smirnoff 	 */
14673b3a8eb9SGleb Smirnoff 	uint8_t proto;
14681cdf23bcSAndrey V. Elsukov 	uint16_t src_port, dst_port;		/* NOTE: host format	*/
14693b3a8eb9SGleb Smirnoff 	struct in_addr src_ip, dst_ip;		/* NOTE: network format	*/
14705c70ebfaSAndrey V. Elsukov 	int iplen = 0;
14713b3a8eb9SGleb Smirnoff 	int pktlen;
14723b3a8eb9SGleb Smirnoff 
1473b99a6823SAndrey V. Elsukov 	struct ipfw_dyn_info dyn_info;
14741719df1bSAndrey V. Elsukov 	struct ip_fw *q = NULL;
14753b3a8eb9SGleb Smirnoff 	struct ip_fw_chain *chain = &V_layer3_chain;
14763b3a8eb9SGleb Smirnoff 
14773b3a8eb9SGleb Smirnoff 	/*
14783b3a8eb9SGleb Smirnoff 	 * We store in ulp a pointer to the upper layer protocol header.
14793b3a8eb9SGleb Smirnoff 	 * In the ipv4 case this is easy to determine from the header,
14803b3a8eb9SGleb Smirnoff 	 * but for ipv6 we might have some additional headers in the middle.
14813b3a8eb9SGleb Smirnoff 	 * ulp is NULL if not found.
14823b3a8eb9SGleb Smirnoff 	 */
14833b3a8eb9SGleb Smirnoff 	void *ulp = NULL;		/* upper layer protocol pointer. */
14843b3a8eb9SGleb Smirnoff 
14853b3a8eb9SGleb Smirnoff 	/* XXX ipv6 variables */
14863b3a8eb9SGleb Smirnoff 	int is_ipv6 = 0;
14873320ca12SJohn Baldwin #ifdef INET6
14883b3a8eb9SGleb Smirnoff 	uint8_t	icmp6_type = 0;
14893320ca12SJohn Baldwin #endif
14903b3a8eb9SGleb Smirnoff 	uint16_t ext_hd = 0;	/* bits vector for extension header filtering */
14913b3a8eb9SGleb Smirnoff 	/* end of ipv6 variables */
14923b3a8eb9SGleb Smirnoff 
14933b3a8eb9SGleb Smirnoff 	int is_ipv4 = 0;
14943b3a8eb9SGleb Smirnoff 
14953b3a8eb9SGleb Smirnoff 	int done = 0;		/* flag to exit the outer loop */
1496f712b161SGleb Smirnoff 	IPFW_RLOCK_TRACKER;
1497f355cb3eSGleb Smirnoff 	bool mem;
14988e780285SRichard Scheffenegger 	bool need_send_reject = false;
14998e780285SRichard Scheffenegger 	int reject_code;
15008e780285SRichard Scheffenegger 	uint16_t reject_mtu;
15013b3a8eb9SGleb Smirnoff 
1502f355cb3eSGleb Smirnoff 	if ((mem = (args->flags & IPFW_ARGS_LENMASK))) {
1503f355cb3eSGleb Smirnoff 		if (args->flags & IPFW_ARGS_ETHER) {
1504f355cb3eSGleb Smirnoff 			eh = (struct ether_header *)args->mem;
1505f355cb3eSGleb Smirnoff 			if (eh->ether_type == htons(ETHERTYPE_VLAN))
1506f355cb3eSGleb Smirnoff 				ip = (struct ip *)
1507f355cb3eSGleb Smirnoff 				    ((struct ether_vlan_header *)eh + 1);
1508f355cb3eSGleb Smirnoff 			else
1509f355cb3eSGleb Smirnoff 				ip = (struct ip *)(eh + 1);
1510f355cb3eSGleb Smirnoff 		} else {
1511f355cb3eSGleb Smirnoff 			eh = NULL;
1512f355cb3eSGleb Smirnoff 			ip = (struct ip *)args->mem;
1513f355cb3eSGleb Smirnoff 		}
1514f355cb3eSGleb Smirnoff 		pktlen = IPFW_ARGS_LENGTH(args->flags);
1515f355cb3eSGleb Smirnoff 		args->f_id.fib = args->ifp->if_fib;	/* best guess */
1516f355cb3eSGleb Smirnoff 	} else {
1517f355cb3eSGleb Smirnoff 		m = args->m;
15183b3a8eb9SGleb Smirnoff 		if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready))
15193b3a8eb9SGleb Smirnoff 			return (IP_FW_PASS);	/* accept */
1520f355cb3eSGleb Smirnoff 		if (args->flags & IPFW_ARGS_ETHER) {
1521f355cb3eSGleb Smirnoff 	                /* We need some amount of data to be contiguous. */
1522f355cb3eSGleb Smirnoff 			if (m->m_len < min(m->m_pkthdr.len, max_protohdr) &&
1523f355cb3eSGleb Smirnoff 			    (args->m = m = m_pullup(m, min(m->m_pkthdr.len,
1524f355cb3eSGleb Smirnoff 			    max_protohdr))) == NULL)
1525f355cb3eSGleb Smirnoff 				goto pullup_failed;
1526f355cb3eSGleb Smirnoff 			eh = mtod(m, struct ether_header *);
1527f355cb3eSGleb Smirnoff 			ip = (struct ip *)(eh + 1);
1528f355cb3eSGleb Smirnoff 		} else {
1529f355cb3eSGleb Smirnoff 			eh = NULL;
1530f355cb3eSGleb Smirnoff 			ip = mtod(m, struct ip *);
1531f355cb3eSGleb Smirnoff 		}
1532f355cb3eSGleb Smirnoff 		pktlen = m->m_pkthdr.len;
1533f355cb3eSGleb Smirnoff 		args->f_id.fib = M_GETFIB(m); /* mbuf not altered */
1534f355cb3eSGleb Smirnoff 	}
15353b3a8eb9SGleb Smirnoff 
15363b3a8eb9SGleb Smirnoff 	dst_ip.s_addr = 0;		/* make sure it is initialized */
15373b3a8eb9SGleb Smirnoff 	src_ip.s_addr = 0;		/* make sure it is initialized */
15387664b71bSAndrey V. Elsukov 	src_port = dst_port = 0;
15393b3a8eb9SGleb Smirnoff 
1540b99a6823SAndrey V. Elsukov 	DYN_INFO_INIT(&dyn_info);
15413b3a8eb9SGleb Smirnoff /*
15423b3a8eb9SGleb Smirnoff  * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous,
15433b3a8eb9SGleb Smirnoff  * then it sets p to point at the offset "len" in the mbuf. WARNING: the
15443b3a8eb9SGleb Smirnoff  * pointer might become stale after other pullups (but we never use it
15453b3a8eb9SGleb Smirnoff  * this way).
15463b3a8eb9SGleb Smirnoff  */
15473b3a8eb9SGleb Smirnoff #define	PULLUP_TO(_len, p, T)	PULLUP_LEN(_len, p, sizeof(T))
1548f355cb3eSGleb Smirnoff #define	EHLEN	(eh != NULL ? ((char *)ip - (char *)eh) : 0)
1549d4e6a529SAndrey V. Elsukov #define	_PULLUP_LOCKED(_len, p, T, unlock)			\
15503b3a8eb9SGleb Smirnoff do {								\
1551f355cb3eSGleb Smirnoff 	int x = (_len) + T + EHLEN;				\
1552f355cb3eSGleb Smirnoff 	if (mem) {						\
1553f8b45306SGleb Smirnoff 		if (__predict_false(pktlen < x)) {		\
1554f8b45306SGleb Smirnoff 			unlock;					\
1555f8b45306SGleb Smirnoff 			goto pullup_failed;			\
1556f8b45306SGleb Smirnoff 		}						\
1557f355cb3eSGleb Smirnoff 		p = (char *)args->mem + (_len) + EHLEN;		\
1558f355cb3eSGleb Smirnoff 	} else {						\
1559f355cb3eSGleb Smirnoff 		if (__predict_false((m)->m_len < x)) {		\
15603b3a8eb9SGleb Smirnoff 			args->m = m = m_pullup(m, x);		\
1561d4e6a529SAndrey V. Elsukov 			if (m == NULL) {			\
1562d4e6a529SAndrey V. Elsukov 				unlock;				\
15633b3a8eb9SGleb Smirnoff 				goto pullup_failed;		\
15643b3a8eb9SGleb Smirnoff 			}					\
1565d4e6a529SAndrey V. Elsukov 		}						\
1566f355cb3eSGleb Smirnoff 		p = mtod(m, char *) + (_len) + EHLEN;		\
1567f355cb3eSGleb Smirnoff 	}							\
15683b3a8eb9SGleb Smirnoff } while (0)
1569d4e6a529SAndrey V. Elsukov 
1570d4e6a529SAndrey V. Elsukov #define	PULLUP_LEN(_len, p, T)	_PULLUP_LOCKED(_len, p, T, )
1571d4e6a529SAndrey V. Elsukov #define	PULLUP_LEN_LOCKED(_len, p, T)	\
1572ca0ac0a6SAndrey V. Elsukov     _PULLUP_LOCKED(_len, p, T, IPFW_PF_RUNLOCK(chain));	\
1573ca0ac0a6SAndrey V. Elsukov     UPDATE_POINTERS()
15743b3a8eb9SGleb Smirnoff /*
1575f355cb3eSGleb Smirnoff  * In case pointers got stale after pullups, update them.
15763b3a8eb9SGleb Smirnoff  */
1577f355cb3eSGleb Smirnoff #define	UPDATE_POINTERS()					\
1578f355cb3eSGleb Smirnoff do {								\
1579f355cb3eSGleb Smirnoff 	if (!mem) {						\
1580f355cb3eSGleb Smirnoff 		if (eh != NULL) {				\
1581f355cb3eSGleb Smirnoff 			eh = mtod(m, struct ether_header *);	\
1582f355cb3eSGleb Smirnoff 			ip = (struct ip *)(eh + 1);		\
1583f355cb3eSGleb Smirnoff 		} else						\
1584f355cb3eSGleb Smirnoff 			ip = mtod(m, struct ip *);		\
1585f355cb3eSGleb Smirnoff 		args->m = m;					\
1586f355cb3eSGleb Smirnoff 	}							\
1587f355cb3eSGleb Smirnoff } while (0)
15883b3a8eb9SGleb Smirnoff 
15893b3a8eb9SGleb Smirnoff 	/* Identify IP packets and fill up variables. */
15903b3a8eb9SGleb Smirnoff 	if (pktlen >= sizeof(struct ip6_hdr) &&
1591f355cb3eSGleb Smirnoff 	    (eh == NULL || eh->ether_type == htons(ETHERTYPE_IPV6)) &&
1592f355cb3eSGleb Smirnoff 	    ip->ip_v == 6) {
15933b3a8eb9SGleb Smirnoff 		struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
15941cdf23bcSAndrey V. Elsukov 
15953b3a8eb9SGleb Smirnoff 		is_ipv6 = 1;
1596dc0fa4f7SGleb Smirnoff 		args->flags |= IPFW_ARGS_IP6;
15973b3a8eb9SGleb Smirnoff 		hlen = sizeof(struct ip6_hdr);
15983b3a8eb9SGleb Smirnoff 		proto = ip6->ip6_nxt;
15993b3a8eb9SGleb Smirnoff 		/* Search extension headers to find upper layer protocols */
16003b3a8eb9SGleb Smirnoff 		while (ulp == NULL && offset == 0) {
16013b3a8eb9SGleb Smirnoff 			switch (proto) {
16023b3a8eb9SGleb Smirnoff 			case IPPROTO_ICMPV6:
16033b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct icmp6_hdr);
16043320ca12SJohn Baldwin #ifdef INET6
16053b3a8eb9SGleb Smirnoff 				icmp6_type = ICMP6(ulp)->icmp6_type;
16063320ca12SJohn Baldwin #endif
16073b3a8eb9SGleb Smirnoff 				break;
16083b3a8eb9SGleb Smirnoff 
16093b3a8eb9SGleb Smirnoff 			case IPPROTO_TCP:
16103b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct tcphdr);
16113b3a8eb9SGleb Smirnoff 				dst_port = TCP(ulp)->th_dport;
16123b3a8eb9SGleb Smirnoff 				src_port = TCP(ulp)->th_sport;
16133b3a8eb9SGleb Smirnoff 				/* save flags for dynamic rules */
16140fc7bdc9SRichard Scheffenegger 				args->f_id._flags = tcp_get_flags(TCP(ulp));
16153b3a8eb9SGleb Smirnoff 				break;
16163b3a8eb9SGleb Smirnoff 
16173b3a8eb9SGleb Smirnoff 			case IPPROTO_SCTP:
1618665c8a2eSMichael Tuexen 				if (pktlen >= hlen + sizeof(struct sctphdr) +
1619665c8a2eSMichael Tuexen 				    sizeof(struct sctp_chunkhdr) +
1620665c8a2eSMichael Tuexen 				    offsetof(struct sctp_init, a_rwnd))
1621665c8a2eSMichael Tuexen 					PULLUP_LEN(hlen, ulp,
1622665c8a2eSMichael Tuexen 					    sizeof(struct sctphdr) +
1623665c8a2eSMichael Tuexen 					    sizeof(struct sctp_chunkhdr) +
1624665c8a2eSMichael Tuexen 					    offsetof(struct sctp_init, a_rwnd));
1625665c8a2eSMichael Tuexen 				else if (pktlen >= hlen + sizeof(struct sctphdr))
1626665c8a2eSMichael Tuexen 					PULLUP_LEN(hlen, ulp, pktlen - hlen);
1627665c8a2eSMichael Tuexen 				else
1628665c8a2eSMichael Tuexen 					PULLUP_LEN(hlen, ulp,
1629665c8a2eSMichael Tuexen 					    sizeof(struct sctphdr));
16303b3a8eb9SGleb Smirnoff 				src_port = SCTP(ulp)->src_port;
16313b3a8eb9SGleb Smirnoff 				dst_port = SCTP(ulp)->dest_port;
16323b3a8eb9SGleb Smirnoff 				break;
16333b3a8eb9SGleb Smirnoff 
16343b3a8eb9SGleb Smirnoff 			case IPPROTO_UDP:
1635d3834420SAndrey V. Elsukov 			case IPPROTO_UDPLITE:
16363b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct udphdr);
16373b3a8eb9SGleb Smirnoff 				dst_port = UDP(ulp)->uh_dport;
16383b3a8eb9SGleb Smirnoff 				src_port = UDP(ulp)->uh_sport;
16393b3a8eb9SGleb Smirnoff 				break;
16403b3a8eb9SGleb Smirnoff 
16413b3a8eb9SGleb Smirnoff 			case IPPROTO_HOPOPTS:	/* RFC 2460 */
16423b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct ip6_hbh);
16433b3a8eb9SGleb Smirnoff 				ext_hd |= EXT_HOPOPTS;
16443b3a8eb9SGleb Smirnoff 				hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
16453b3a8eb9SGleb Smirnoff 				proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
16463b3a8eb9SGleb Smirnoff 				ulp = NULL;
16473b3a8eb9SGleb Smirnoff 				break;
16483b3a8eb9SGleb Smirnoff 
16493b3a8eb9SGleb Smirnoff 			case IPPROTO_ROUTING:	/* RFC 2460 */
16503b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct ip6_rthdr);
16513b3a8eb9SGleb Smirnoff 				switch (((struct ip6_rthdr *)ulp)->ip6r_type) {
16523b3a8eb9SGleb Smirnoff 				case 0:
16533b3a8eb9SGleb Smirnoff 					ext_hd |= EXT_RTHDR0;
16543b3a8eb9SGleb Smirnoff 					break;
16553b3a8eb9SGleb Smirnoff 				case 2:
16563b3a8eb9SGleb Smirnoff 					ext_hd |= EXT_RTHDR2;
16573b3a8eb9SGleb Smirnoff 					break;
16583b3a8eb9SGleb Smirnoff 				default:
16593b3a8eb9SGleb Smirnoff 					if (V_fw_verbose)
16603b3a8eb9SGleb Smirnoff 						printf("IPFW2: IPV6 - Unknown "
16613b3a8eb9SGleb Smirnoff 						    "Routing Header type(%d)\n",
16623b3a8eb9SGleb Smirnoff 						    ((struct ip6_rthdr *)
16633b3a8eb9SGleb Smirnoff 						    ulp)->ip6r_type);
16643b3a8eb9SGleb Smirnoff 					if (V_fw_deny_unknown_exthdrs)
16653b3a8eb9SGleb Smirnoff 					    return (IP_FW_DENY);
16663b3a8eb9SGleb Smirnoff 					break;
16673b3a8eb9SGleb Smirnoff 				}
16683b3a8eb9SGleb Smirnoff 				ext_hd |= EXT_ROUTING;
16693b3a8eb9SGleb Smirnoff 				hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3;
16703b3a8eb9SGleb Smirnoff 				proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt;
16713b3a8eb9SGleb Smirnoff 				ulp = NULL;
16723b3a8eb9SGleb Smirnoff 				break;
16733b3a8eb9SGleb Smirnoff 
16743b3a8eb9SGleb Smirnoff 			case IPPROTO_FRAGMENT:	/* RFC 2460 */
16753b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct ip6_frag);
16763b3a8eb9SGleb Smirnoff 				ext_hd |= EXT_FRAGMENT;
16773b3a8eb9SGleb Smirnoff 				hlen += sizeof (struct ip6_frag);
16783b3a8eb9SGleb Smirnoff 				proto = ((struct ip6_frag *)ulp)->ip6f_nxt;
16793b3a8eb9SGleb Smirnoff 				offset = ((struct ip6_frag *)ulp)->ip6f_offlg &
16803b3a8eb9SGleb Smirnoff 					IP6F_OFF_MASK;
16813b3a8eb9SGleb Smirnoff 				ip6f_mf = ((struct ip6_frag *)ulp)->ip6f_offlg &
16823b3a8eb9SGleb Smirnoff 					IP6F_MORE_FRAG;
16833b3a8eb9SGleb Smirnoff 				if (V_fw_permit_single_frag6 == 0 &&
16843b3a8eb9SGleb Smirnoff 				    offset == 0 && ip6f_mf == 0) {
16853b3a8eb9SGleb Smirnoff 					if (V_fw_verbose)
16863b3a8eb9SGleb Smirnoff 						printf("IPFW2: IPV6 - Invalid "
16873b3a8eb9SGleb Smirnoff 						    "Fragment Header\n");
16883b3a8eb9SGleb Smirnoff 					if (V_fw_deny_unknown_exthdrs)
16893b3a8eb9SGleb Smirnoff 					    return (IP_FW_DENY);
16903b3a8eb9SGleb Smirnoff 					break;
16913b3a8eb9SGleb Smirnoff 				}
16923b3a8eb9SGleb Smirnoff 				args->f_id.extra =
16933b3a8eb9SGleb Smirnoff 				    ntohl(((struct ip6_frag *)ulp)->ip6f_ident);
16943b3a8eb9SGleb Smirnoff 				ulp = NULL;
16953b3a8eb9SGleb Smirnoff 				break;
16963b3a8eb9SGleb Smirnoff 
16973b3a8eb9SGleb Smirnoff 			case IPPROTO_DSTOPTS:	/* RFC 2460 */
16983b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct ip6_hbh);
16993b3a8eb9SGleb Smirnoff 				ext_hd |= EXT_DSTOPTS;
17003b3a8eb9SGleb Smirnoff 				hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
17013b3a8eb9SGleb Smirnoff 				proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
17023b3a8eb9SGleb Smirnoff 				ulp = NULL;
17033b3a8eb9SGleb Smirnoff 				break;
17043b3a8eb9SGleb Smirnoff 
17053b3a8eb9SGleb Smirnoff 			case IPPROTO_AH:	/* RFC 2402 */
17063b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct ip6_ext);
17073b3a8eb9SGleb Smirnoff 				ext_hd |= EXT_AH;
17083b3a8eb9SGleb Smirnoff 				hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2;
17093b3a8eb9SGleb Smirnoff 				proto = ((struct ip6_ext *)ulp)->ip6e_nxt;
17103b3a8eb9SGleb Smirnoff 				ulp = NULL;
17113b3a8eb9SGleb Smirnoff 				break;
17123b3a8eb9SGleb Smirnoff 
17133b3a8eb9SGleb Smirnoff 			case IPPROTO_ESP:	/* RFC 2406 */
17143b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, uint32_t);	/* SPI, Seq# */
17153b3a8eb9SGleb Smirnoff 				/* Anything past Seq# is variable length and
17163b3a8eb9SGleb Smirnoff 				 * data past this ext. header is encrypted. */
17173b3a8eb9SGleb Smirnoff 				ext_hd |= EXT_ESP;
17183b3a8eb9SGleb Smirnoff 				break;
17193b3a8eb9SGleb Smirnoff 
17203b3a8eb9SGleb Smirnoff 			case IPPROTO_NONE:	/* RFC 2460 */
17213b3a8eb9SGleb Smirnoff 				/*
17223b3a8eb9SGleb Smirnoff 				 * Packet ends here, and IPv6 header has
17233b3a8eb9SGleb Smirnoff 				 * already been pulled up. If ip6e_len!=0
17243b3a8eb9SGleb Smirnoff 				 * then octets must be ignored.
17253b3a8eb9SGleb Smirnoff 				 */
17263b3a8eb9SGleb Smirnoff 				ulp = ip; /* non-NULL to get out of loop. */
17273b3a8eb9SGleb Smirnoff 				break;
17283b3a8eb9SGleb Smirnoff 
17293b3a8eb9SGleb Smirnoff 			case IPPROTO_OSPFIGP:
17303b3a8eb9SGleb Smirnoff 				/* XXX OSPF header check? */
17313b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct ip6_ext);
17323b3a8eb9SGleb Smirnoff 				break;
17333b3a8eb9SGleb Smirnoff 
17343b3a8eb9SGleb Smirnoff 			case IPPROTO_PIM:
17353b3a8eb9SGleb Smirnoff 				/* XXX PIM header check? */
17363b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct pim);
17373b3a8eb9SGleb Smirnoff 				break;
17383b3a8eb9SGleb Smirnoff 
1739b0e1660dSPhilip Paeps 			case IPPROTO_GRE:	/* RFC 1701 */
1740b0e1660dSPhilip Paeps 				/* XXX GRE header check? */
1741b0e1660dSPhilip Paeps 				PULLUP_TO(hlen, ulp, struct grehdr);
1742b0e1660dSPhilip Paeps 				break;
1743b0e1660dSPhilip Paeps 
17443b3a8eb9SGleb Smirnoff 			case IPPROTO_CARP:
174548266154SAndrey V. Elsukov 				PULLUP_TO(hlen, ulp, offsetof(
174648266154SAndrey V. Elsukov 				    struct carp_header, carp_counter));
174748266154SAndrey V. Elsukov 				if (CARP_ADVERTISEMENT !=
174848266154SAndrey V. Elsukov 				    ((struct carp_header *)ulp)->carp_type)
17493b3a8eb9SGleb Smirnoff 					return (IP_FW_DENY);
17503b3a8eb9SGleb Smirnoff 				break;
17513b3a8eb9SGleb Smirnoff 
17523b3a8eb9SGleb Smirnoff 			case IPPROTO_IPV6:	/* RFC 2893 */
17533b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct ip6_hdr);
17543b3a8eb9SGleb Smirnoff 				break;
17553b3a8eb9SGleb Smirnoff 
17563b3a8eb9SGleb Smirnoff 			case IPPROTO_IPV4:	/* RFC 2893 */
17573b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct ip);
17583b3a8eb9SGleb Smirnoff 				break;
17593b3a8eb9SGleb Smirnoff 
176066f2f9eeSKristof Provost 			case IPPROTO_PFSYNC:
176166f2f9eeSKristof Provost 				PULLUP_TO(hlen, ulp, struct pfsync_header);
176266f2f9eeSKristof Provost 				break;
176366f2f9eeSKristof Provost 
17643b3a8eb9SGleb Smirnoff 			default:
17653b3a8eb9SGleb Smirnoff 				if (V_fw_verbose)
17663b3a8eb9SGleb Smirnoff 					printf("IPFW2: IPV6 - Unknown "
17673b3a8eb9SGleb Smirnoff 					    "Extension Header(%d), ext_hd=%x\n",
17683b3a8eb9SGleb Smirnoff 					     proto, ext_hd);
17693b3a8eb9SGleb Smirnoff 				if (V_fw_deny_unknown_exthdrs)
17703b3a8eb9SGleb Smirnoff 				    return (IP_FW_DENY);
17713b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct ip6_ext);
17723b3a8eb9SGleb Smirnoff 				break;
17733b3a8eb9SGleb Smirnoff 			} /*switch */
17743b3a8eb9SGleb Smirnoff 		}
1775f355cb3eSGleb Smirnoff 		UPDATE_POINTERS();
17763b3a8eb9SGleb Smirnoff 		ip6 = (struct ip6_hdr *)ip;
17771cdf23bcSAndrey V. Elsukov 		args->f_id.addr_type = 6;
17783b3a8eb9SGleb Smirnoff 		args->f_id.src_ip6 = ip6->ip6_src;
17793b3a8eb9SGleb Smirnoff 		args->f_id.dst_ip6 = ip6->ip6_dst;
17803b3a8eb9SGleb Smirnoff 		args->f_id.flow_id6 = ntohl(ip6->ip6_flow);
17815c70ebfaSAndrey V. Elsukov 		iplen = ntohs(ip6->ip6_plen) + sizeof(*ip6);
17823b3a8eb9SGleb Smirnoff 	} else if (pktlen >= sizeof(struct ip) &&
1783f355cb3eSGleb Smirnoff 	    (eh == NULL || eh->ether_type == htons(ETHERTYPE_IP)) &&
1784f355cb3eSGleb Smirnoff 	    ip->ip_v == 4) {
17853b3a8eb9SGleb Smirnoff 		is_ipv4 = 1;
1786dc0fa4f7SGleb Smirnoff 		args->flags |= IPFW_ARGS_IP4;
17873b3a8eb9SGleb Smirnoff 		hlen = ip->ip_hl << 2;
17883b3a8eb9SGleb Smirnoff 		/*
17891cdf23bcSAndrey V. Elsukov 		 * Collect parameters into local variables for faster
17901cdf23bcSAndrey V. Elsukov 		 * matching.
17913b3a8eb9SGleb Smirnoff 		 */
17923b3a8eb9SGleb Smirnoff 		proto = ip->ip_p;
17933b3a8eb9SGleb Smirnoff 		src_ip = ip->ip_src;
17943b3a8eb9SGleb Smirnoff 		dst_ip = ip->ip_dst;
17953b3a8eb9SGleb Smirnoff 		offset = ntohs(ip->ip_off) & IP_OFFMASK;
17963b3a8eb9SGleb Smirnoff 		iplen = ntohs(ip->ip_len);
17973b3a8eb9SGleb Smirnoff 
17983b3a8eb9SGleb Smirnoff 		if (offset == 0) {
17993b3a8eb9SGleb Smirnoff 			switch (proto) {
18003b3a8eb9SGleb Smirnoff 			case IPPROTO_TCP:
18013b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct tcphdr);
18023b3a8eb9SGleb Smirnoff 				dst_port = TCP(ulp)->th_dport;
18033b3a8eb9SGleb Smirnoff 				src_port = TCP(ulp)->th_sport;
18043b3a8eb9SGleb Smirnoff 				/* save flags for dynamic rules */
18050fc7bdc9SRichard Scheffenegger 				args->f_id._flags = tcp_get_flags(TCP(ulp));
18063b3a8eb9SGleb Smirnoff 				break;
18073b3a8eb9SGleb Smirnoff 
18083b3a8eb9SGleb Smirnoff 			case IPPROTO_SCTP:
1809665c8a2eSMichael Tuexen 				if (pktlen >= hlen + sizeof(struct sctphdr) +
1810665c8a2eSMichael Tuexen 				    sizeof(struct sctp_chunkhdr) +
1811665c8a2eSMichael Tuexen 				    offsetof(struct sctp_init, a_rwnd))
1812665c8a2eSMichael Tuexen 					PULLUP_LEN(hlen, ulp,
1813665c8a2eSMichael Tuexen 					    sizeof(struct sctphdr) +
1814665c8a2eSMichael Tuexen 					    sizeof(struct sctp_chunkhdr) +
1815665c8a2eSMichael Tuexen 					    offsetof(struct sctp_init, a_rwnd));
1816665c8a2eSMichael Tuexen 				else if (pktlen >= hlen + sizeof(struct sctphdr))
1817665c8a2eSMichael Tuexen 					PULLUP_LEN(hlen, ulp, pktlen - hlen);
1818665c8a2eSMichael Tuexen 				else
1819665c8a2eSMichael Tuexen 					PULLUP_LEN(hlen, ulp,
1820665c8a2eSMichael Tuexen 					    sizeof(struct sctphdr));
18213b3a8eb9SGleb Smirnoff 				src_port = SCTP(ulp)->src_port;
18223b3a8eb9SGleb Smirnoff 				dst_port = SCTP(ulp)->dest_port;
18233b3a8eb9SGleb Smirnoff 				break;
18243b3a8eb9SGleb Smirnoff 
18253b3a8eb9SGleb Smirnoff 			case IPPROTO_UDP:
1826d3834420SAndrey V. Elsukov 			case IPPROTO_UDPLITE:
18273b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct udphdr);
18283b3a8eb9SGleb Smirnoff 				dst_port = UDP(ulp)->uh_dport;
18293b3a8eb9SGleb Smirnoff 				src_port = UDP(ulp)->uh_sport;
18303b3a8eb9SGleb Smirnoff 				break;
18313b3a8eb9SGleb Smirnoff 
18323b3a8eb9SGleb Smirnoff 			case IPPROTO_ICMP:
18333b3a8eb9SGleb Smirnoff 				PULLUP_TO(hlen, ulp, struct icmphdr);
18343b3a8eb9SGleb Smirnoff 				//args->f_id.flags = ICMP(ulp)->icmp_type;
18353b3a8eb9SGleb Smirnoff 				break;
18363b3a8eb9SGleb Smirnoff 
18373b3a8eb9SGleb Smirnoff 			default:
18383b3a8eb9SGleb Smirnoff 				break;
18393b3a8eb9SGleb Smirnoff 			}
1840019c8c93SAndrey V. Elsukov 		} else {
1841019c8c93SAndrey V. Elsukov 			if (offset == 1 && proto == IPPROTO_TCP) {
1842019c8c93SAndrey V. Elsukov 				/* RFC 3128 */
1843019c8c93SAndrey V. Elsukov 				goto pullup_failed;
1844019c8c93SAndrey V. Elsukov 			}
18453b3a8eb9SGleb Smirnoff 		}
18463b3a8eb9SGleb Smirnoff 
1847f355cb3eSGleb Smirnoff 		UPDATE_POINTERS();
18481cdf23bcSAndrey V. Elsukov 		args->f_id.addr_type = 4;
18493b3a8eb9SGleb Smirnoff 		args->f_id.src_ip = ntohl(src_ip.s_addr);
18503b3a8eb9SGleb Smirnoff 		args->f_id.dst_ip = ntohl(dst_ip.s_addr);
18511cdf23bcSAndrey V. Elsukov 	} else {
18521cdf23bcSAndrey V. Elsukov 		proto = 0;
18531cdf23bcSAndrey V. Elsukov 		dst_ip.s_addr = src_ip.s_addr = 0;
18541cdf23bcSAndrey V. Elsukov 
18551cdf23bcSAndrey V. Elsukov 		args->f_id.addr_type = 1; /* XXX */
18563b3a8eb9SGleb Smirnoff 	}
18573b3a8eb9SGleb Smirnoff #undef PULLUP_TO
18585c70ebfaSAndrey V. Elsukov 	pktlen = iplen < pktlen ? iplen: pktlen;
18591cdf23bcSAndrey V. Elsukov 
18601cdf23bcSAndrey V. Elsukov 	/* Properly initialize the rest of f_id */
18613b3a8eb9SGleb Smirnoff 	args->f_id.proto = proto;
18623b3a8eb9SGleb Smirnoff 	args->f_id.src_port = src_port = ntohs(src_port);
18633b3a8eb9SGleb Smirnoff 	args->f_id.dst_port = dst_port = ntohs(dst_port);
18643b3a8eb9SGleb Smirnoff 
186593bb4f9eSAndrey V. Elsukov 	IPFW_PF_RLOCK(chain);
18663b3a8eb9SGleb Smirnoff 	if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */
186793bb4f9eSAndrey V. Elsukov 		IPFW_PF_RUNLOCK(chain);
18683b3a8eb9SGleb Smirnoff 		return (IP_FW_PASS);	/* accept */
18693b3a8eb9SGleb Smirnoff 	}
18701cdf23bcSAndrey V. Elsukov 	if (args->flags & IPFW_ARGS_REF) {
18713b3a8eb9SGleb Smirnoff 		/*
18723b3a8eb9SGleb Smirnoff 		 * Packet has already been tagged as a result of a previous
18733b3a8eb9SGleb Smirnoff 		 * match on rule args->rule aka args->rule_id (PIPE, QUEUE,
18743b3a8eb9SGleb Smirnoff 		 * REASS, NETGRAPH, DIVERT/TEE...)
18753b3a8eb9SGleb Smirnoff 		 * Validate the slot and continue from the next one
18763b3a8eb9SGleb Smirnoff 		 * if still present, otherwise do a lookup.
18773b3a8eb9SGleb Smirnoff 		 */
18783b3a8eb9SGleb Smirnoff 		f_pos = (args->rule.chain_id == chain->id) ?
18793b3a8eb9SGleb Smirnoff 		    args->rule.slot :
18803b3a8eb9SGleb Smirnoff 		    ipfw_find_rule(chain, args->rule.rulenum,
18813b3a8eb9SGleb Smirnoff 			args->rule.rule_id);
18823b3a8eb9SGleb Smirnoff 	} else {
18833b3a8eb9SGleb Smirnoff 		f_pos = 0;
18843b3a8eb9SGleb Smirnoff 	}
18853b3a8eb9SGleb Smirnoff 
1886b7795b67SGleb Smirnoff 	if (args->flags & IPFW_ARGS_IN) {
1887b7795b67SGleb Smirnoff 		iif = args->ifp;
1888b7795b67SGleb Smirnoff 		oif = NULL;
1889b7795b67SGleb Smirnoff 	} else {
1890b7795b67SGleb Smirnoff 		MPASS(args->flags & IPFW_ARGS_OUT);
1891fb3bc596SJohn Baldwin 		iif = mem ? NULL : m_rcvif(m);
1892b7795b67SGleb Smirnoff 		oif = args->ifp;
1893b7795b67SGleb Smirnoff 	}
1894b7795b67SGleb Smirnoff 
18953b3a8eb9SGleb Smirnoff 	/*
18963b3a8eb9SGleb Smirnoff 	 * Now scan the rules, and parse microinstructions for each rule.
18973b3a8eb9SGleb Smirnoff 	 * We have two nested loops and an inner switch. Sometimes we
18983b3a8eb9SGleb Smirnoff 	 * need to break out of one or both loops, or re-enter one of
18993b3a8eb9SGleb Smirnoff 	 * the loops with updated variables. Loop variables are:
19003b3a8eb9SGleb Smirnoff 	 *
19013b3a8eb9SGleb Smirnoff 	 *	f_pos (outer loop) points to the current rule.
19023b3a8eb9SGleb Smirnoff 	 *		On output it points to the matching rule.
19033b3a8eb9SGleb Smirnoff 	 *	done (outer loop) is used as a flag to break the loop.
19043b3a8eb9SGleb Smirnoff 	 *	l (inner loop)	residual length of current rule.
19053b3a8eb9SGleb Smirnoff 	 *		cmd points to the current microinstruction.
19063b3a8eb9SGleb Smirnoff 	 *
19073b3a8eb9SGleb Smirnoff 	 * We break the inner loop by setting l=0 and possibly
19083b3a8eb9SGleb Smirnoff 	 * cmdlen=0 if we don't want to advance cmd.
19093b3a8eb9SGleb Smirnoff 	 * We break the outer loop by setting done=1
19103b3a8eb9SGleb Smirnoff 	 * We can restart the inner loop by setting l>0 and f_pos, f, cmd
19113b3a8eb9SGleb Smirnoff 	 * as needed.
19123b3a8eb9SGleb Smirnoff 	 */
19133b3a8eb9SGleb Smirnoff 	for (; f_pos < chain->n_rules; f_pos++) {
19143b3a8eb9SGleb Smirnoff 		ipfw_insn *cmd;
19153b3a8eb9SGleb Smirnoff 		uint32_t tablearg = 0;
19163b3a8eb9SGleb Smirnoff 		int l, cmdlen, skip_or; /* skip rest of OR block */
19173b3a8eb9SGleb Smirnoff 		struct ip_fw *f;
19183b3a8eb9SGleb Smirnoff 
19193b3a8eb9SGleb Smirnoff 		f = chain->map[f_pos];
19203b3a8eb9SGleb Smirnoff 		if (V_set_disable & (1 << f->set) )
19213b3a8eb9SGleb Smirnoff 			continue;
19223b3a8eb9SGleb Smirnoff 
19233b3a8eb9SGleb Smirnoff 		skip_or = 0;
19243b3a8eb9SGleb Smirnoff 		for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
19253b3a8eb9SGleb Smirnoff 		    l -= cmdlen, cmd += cmdlen) {
19263b3a8eb9SGleb Smirnoff 			int match;
19273b3a8eb9SGleb Smirnoff 
19283b3a8eb9SGleb Smirnoff 			/*
19293b3a8eb9SGleb Smirnoff 			 * check_body is a jump target used when we find a
19303b3a8eb9SGleb Smirnoff 			 * CHECK_STATE, and need to jump to the body of
19313b3a8eb9SGleb Smirnoff 			 * the target rule.
19323b3a8eb9SGleb Smirnoff 			 */
19333b3a8eb9SGleb Smirnoff 
19343b3a8eb9SGleb Smirnoff /* check_body: */
19353b3a8eb9SGleb Smirnoff 			cmdlen = F_LEN(cmd);
19363b3a8eb9SGleb Smirnoff 			/*
19373b3a8eb9SGleb Smirnoff 			 * An OR block (insn_1 || .. || insn_n) has the
19383b3a8eb9SGleb Smirnoff 			 * F_OR bit set in all but the last instruction.
19393b3a8eb9SGleb Smirnoff 			 * The first match will set "skip_or", and cause
19403b3a8eb9SGleb Smirnoff 			 * the following instructions to be skipped until
19413b3a8eb9SGleb Smirnoff 			 * past the one with the F_OR bit clear.
19423b3a8eb9SGleb Smirnoff 			 */
19433b3a8eb9SGleb Smirnoff 			if (skip_or) {		/* skip this instruction */
19443b3a8eb9SGleb Smirnoff 				if ((cmd->len & F_OR) == 0)
19453b3a8eb9SGleb Smirnoff 					skip_or = 0;	/* next one is good */
19463b3a8eb9SGleb Smirnoff 				continue;
19473b3a8eb9SGleb Smirnoff 			}
19483b3a8eb9SGleb Smirnoff 			match = 0; /* set to 1 if we succeed */
19493b3a8eb9SGleb Smirnoff 
19503b3a8eb9SGleb Smirnoff 			switch (cmd->opcode) {
19513b3a8eb9SGleb Smirnoff 			/*
19523b3a8eb9SGleb Smirnoff 			 * The first set of opcodes compares the packet's
19533b3a8eb9SGleb Smirnoff 			 * fields with some pattern, setting 'match' if a
19543b3a8eb9SGleb Smirnoff 			 * match is found. At the end of the loop there is
19553b3a8eb9SGleb Smirnoff 			 * logic to deal with F_NOT and F_OR flags associated
19563b3a8eb9SGleb Smirnoff 			 * with the opcode.
19573b3a8eb9SGleb Smirnoff 			 */
19583b3a8eb9SGleb Smirnoff 			case O_NOP:
19593b3a8eb9SGleb Smirnoff 				match = 1;
19603b3a8eb9SGleb Smirnoff 				break;
19613b3a8eb9SGleb Smirnoff 
19623b3a8eb9SGleb Smirnoff 			case O_FORWARD_MAC:
19633b3a8eb9SGleb Smirnoff 				printf("ipfw: opcode %d unimplemented\n",
19643b3a8eb9SGleb Smirnoff 				    cmd->opcode);
19653b3a8eb9SGleb Smirnoff 				break;
19663b3a8eb9SGleb Smirnoff 
19673b3a8eb9SGleb Smirnoff 			case O_GID:
19683b3a8eb9SGleb Smirnoff 			case O_UID:
19693b3a8eb9SGleb Smirnoff 			case O_JAIL:
19703b3a8eb9SGleb Smirnoff 				/*
19713b3a8eb9SGleb Smirnoff 				 * We only check offset == 0 && proto != 0,
19723b3a8eb9SGleb Smirnoff 				 * as this ensures that we have a
19733b3a8eb9SGleb Smirnoff 				 * packet with the ports info.
19743b3a8eb9SGleb Smirnoff 				 */
19753b3a8eb9SGleb Smirnoff 				if (offset != 0)
19763b3a8eb9SGleb Smirnoff 					break;
19773b3a8eb9SGleb Smirnoff 				if (proto == IPPROTO_TCP ||
1978d3834420SAndrey V. Elsukov 				    proto == IPPROTO_UDP ||
1979d3834420SAndrey V. Elsukov 				    proto == IPPROTO_UDPLITE)
19803b3a8eb9SGleb Smirnoff 					match = check_uidgid(
19813b3a8eb9SGleb Smirnoff 						    (ipfw_insn_u32 *)cmd,
19823b3a8eb9SGleb Smirnoff 						    args, &ucred_lookup,
19833b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
19843b3a8eb9SGleb Smirnoff 						    &ucred_cache);
19853b3a8eb9SGleb Smirnoff #else
19863b3a8eb9SGleb Smirnoff 						    (void *)&ucred_cache);
19873b3a8eb9SGleb Smirnoff #endif
19883b3a8eb9SGleb Smirnoff 				break;
19893b3a8eb9SGleb Smirnoff 
19903b3a8eb9SGleb Smirnoff 			case O_RECV:
1991b7795b67SGleb Smirnoff 				match = iface_match(iif, (ipfw_insn_if *)cmd,
1992b7795b67SGleb Smirnoff 				    chain, &tablearg);
19933b3a8eb9SGleb Smirnoff 				break;
19943b3a8eb9SGleb Smirnoff 
19953b3a8eb9SGleb Smirnoff 			case O_XMIT:
19963b3a8eb9SGleb Smirnoff 				match = iface_match(oif, (ipfw_insn_if *)cmd,
19973b3a8eb9SGleb Smirnoff 				    chain, &tablearg);
19983b3a8eb9SGleb Smirnoff 				break;
19993b3a8eb9SGleb Smirnoff 
20003b3a8eb9SGleb Smirnoff 			case O_VIA:
2001b7795b67SGleb Smirnoff 				match = iface_match(args->ifp,
2002b7795b67SGleb Smirnoff 				    (ipfw_insn_if *)cmd, chain, &tablearg);
20033b3a8eb9SGleb Smirnoff 				break;
20043b3a8eb9SGleb Smirnoff 
20053b3a8eb9SGleb Smirnoff 			case O_MACADDR2:
20061cdf23bcSAndrey V. Elsukov 				if (args->flags & IPFW_ARGS_ETHER) {
20073b3a8eb9SGleb Smirnoff 					u_int32_t *want = (u_int32_t *)
20083b3a8eb9SGleb Smirnoff 						((ipfw_insn_mac *)cmd)->addr;
20093b3a8eb9SGleb Smirnoff 					u_int32_t *mask = (u_int32_t *)
20103b3a8eb9SGleb Smirnoff 						((ipfw_insn_mac *)cmd)->mask;
2011f355cb3eSGleb Smirnoff 					u_int32_t *hdr = (u_int32_t *)eh;
20123b3a8eb9SGleb Smirnoff 
20133b3a8eb9SGleb Smirnoff 					match =
20143b3a8eb9SGleb Smirnoff 					    ( want[0] == (hdr[0] & mask[0]) &&
20153b3a8eb9SGleb Smirnoff 					      want[1] == (hdr[1] & mask[1]) &&
20163b3a8eb9SGleb Smirnoff 					      want[2] == (hdr[2] & mask[2]) );
20173b3a8eb9SGleb Smirnoff 				}
20183b3a8eb9SGleb Smirnoff 				break;
20193b3a8eb9SGleb Smirnoff 
20203b3a8eb9SGleb Smirnoff 			case O_MAC_TYPE:
20211cdf23bcSAndrey V. Elsukov 				if (args->flags & IPFW_ARGS_ETHER) {
20223b3a8eb9SGleb Smirnoff 					u_int16_t *p =
20233b3a8eb9SGleb Smirnoff 					    ((ipfw_insn_u16 *)cmd)->ports;
20243b3a8eb9SGleb Smirnoff 					int i;
20253b3a8eb9SGleb Smirnoff 
20263b3a8eb9SGleb Smirnoff 					for (i = cmdlen - 1; !match && i>0;
20273b3a8eb9SGleb Smirnoff 					    i--, p += 2)
2028f355cb3eSGleb Smirnoff 						match =
2029f355cb3eSGleb Smirnoff 						    (ntohs(eh->ether_type) >=
2030f355cb3eSGleb Smirnoff 						    p[0] &&
2031f355cb3eSGleb Smirnoff 						    ntohs(eh->ether_type) <=
2032f355cb3eSGleb Smirnoff 						    p[1]);
20333b3a8eb9SGleb Smirnoff 				}
20343b3a8eb9SGleb Smirnoff 				break;
20353b3a8eb9SGleb Smirnoff 
20363b3a8eb9SGleb Smirnoff 			case O_FRAG:
2037825398f9SGleb Smirnoff 				if (is_ipv4) {
2038825398f9SGleb Smirnoff 					/*
2039825398f9SGleb Smirnoff 					 * Since flags_match() works with
2040825398f9SGleb Smirnoff 					 * uint8_t we pack ip_off into 8 bits.
2041825398f9SGleb Smirnoff 					 * For this match offset is a boolean.
2042825398f9SGleb Smirnoff 					 */
2043825398f9SGleb Smirnoff 					match = flags_match(cmd,
2044825398f9SGleb Smirnoff 					    ((ntohs(ip->ip_off) & ~IP_OFFMASK)
2045825398f9SGleb Smirnoff 					    >> 8) | (offset != 0));
2046825398f9SGleb Smirnoff 				} else {
2047825398f9SGleb Smirnoff 					/*
204874081dc2SIgor Ostapenko 					 * Compatibility: historically bare
2049825398f9SGleb Smirnoff 					 * "frag" would match IPv6 fragments.
2050825398f9SGleb Smirnoff 					 */
2051825398f9SGleb Smirnoff 					match = (cmd->arg1 == 0x1 &&
2052825398f9SGleb Smirnoff 					    (offset != 0));
2053825398f9SGleb Smirnoff 				}
20543b3a8eb9SGleb Smirnoff 				break;
20553b3a8eb9SGleb Smirnoff 
20563b3a8eb9SGleb Smirnoff 			case O_IN:	/* "out" is "not in" */
20573b3a8eb9SGleb Smirnoff 				match = (oif == NULL);
20583b3a8eb9SGleb Smirnoff 				break;
20593b3a8eb9SGleb Smirnoff 
20603b3a8eb9SGleb Smirnoff 			case O_LAYER2:
20611cdf23bcSAndrey V. Elsukov 				match = (args->flags & IPFW_ARGS_ETHER);
20623b3a8eb9SGleb Smirnoff 				break;
20633b3a8eb9SGleb Smirnoff 
20643b3a8eb9SGleb Smirnoff 			case O_DIVERTED:
20651cdf23bcSAndrey V. Elsukov 				if ((args->flags & IPFW_ARGS_REF) == 0)
20661cdf23bcSAndrey V. Elsukov 					break;
20671cdf23bcSAndrey V. Elsukov 				/*
20681cdf23bcSAndrey V. Elsukov 				 * For diverted packets, args->rule.info
20693b3a8eb9SGleb Smirnoff 				 * contains the divert port (in host format)
20703b3a8eb9SGleb Smirnoff 				 * reason and direction.
20713b3a8eb9SGleb Smirnoff 				 */
20721cdf23bcSAndrey V. Elsukov 				match = ((args->rule.info & IPFW_IS_MASK) ==
20731cdf23bcSAndrey V. Elsukov 				    IPFW_IS_DIVERT) && (
20741cdf23bcSAndrey V. Elsukov 				    ((args->rule.info & IPFW_INFO_IN) ?
20751cdf23bcSAndrey V. Elsukov 					1: 2) & cmd->arg1);
20763b3a8eb9SGleb Smirnoff 				break;
20773b3a8eb9SGleb Smirnoff 
20783b3a8eb9SGleb Smirnoff 			case O_PROTO:
20793b3a8eb9SGleb Smirnoff 				/*
20803b3a8eb9SGleb Smirnoff 				 * We do not allow an arg of 0 so the
20813b3a8eb9SGleb Smirnoff 				 * check of "proto" only suffices.
20823b3a8eb9SGleb Smirnoff 				 */
20833b3a8eb9SGleb Smirnoff 				match = (proto == cmd->arg1);
20843b3a8eb9SGleb Smirnoff 				break;
20853b3a8eb9SGleb Smirnoff 
20863b3a8eb9SGleb Smirnoff 			case O_IP_SRC:
20873b3a8eb9SGleb Smirnoff 				match = is_ipv4 &&
20883b3a8eb9SGleb Smirnoff 				    (((ipfw_insn_ip *)cmd)->addr.s_addr ==
20893b3a8eb9SGleb Smirnoff 				    src_ip.s_addr);
20903b3a8eb9SGleb Smirnoff 				break;
20913b3a8eb9SGleb Smirnoff 
20923b3a8eb9SGleb Smirnoff 			case O_IP_DST_LOOKUP:
20934a77657cSAndrey V. Elsukov 				if (IPFW_LOOKUP_TYPE(cmd) != LOOKUP_NONE) {
20944a77657cSAndrey V. Elsukov 				  void *pkey = NULL;
20954a77657cSAndrey V. Elsukov 				  uint32_t key, vidx;
209681cac390SArseny Smalyuk 				  uint16_t keylen = 0; /* zero if can't match the packet */
20974a77657cSAndrey V. Elsukov 				  uint8_t lookup_type;
20983b3a8eb9SGleb Smirnoff 
20994a77657cSAndrey V. Elsukov 				lookup_type = IPFW_LOOKUP_TYPE(cmd);
21004a77657cSAndrey V. Elsukov 
21014a77657cSAndrey V. Elsukov 				switch (lookup_type) {
210281cac390SArseny Smalyuk 				case LOOKUP_DST_IP:
210381cac390SArseny Smalyuk 				case LOOKUP_SRC_IP:
21044a77657cSAndrey V. Elsukov 					if (is_ipv4) {
21054a77657cSAndrey V. Elsukov 						keylen = sizeof(in_addr_t);
21064a77657cSAndrey V. Elsukov 						if (lookup_type == LOOKUP_DST_IP)
21074a77657cSAndrey V. Elsukov 							pkey = &dst_ip;
210881cac390SArseny Smalyuk 						else
21094a77657cSAndrey V. Elsukov 							pkey = &src_ip;
21104a77657cSAndrey V. Elsukov 					} else if (is_ipv6) {
21114a77657cSAndrey V. Elsukov 						keylen = sizeof(struct in6_addr);
21124a77657cSAndrey V. Elsukov 						if (lookup_type == LOOKUP_DST_IP)
21134a77657cSAndrey V. Elsukov 							pkey = &args->f_id.dst_ip6;
21144a77657cSAndrey V. Elsukov 						else
21154a77657cSAndrey V. Elsukov 							pkey = &args->f_id.src_ip6;
21164a77657cSAndrey V. Elsukov 					} else /* only for L3 */
21174a77657cSAndrey V. Elsukov 						break;
21184a77657cSAndrey V. Elsukov 				case LOOKUP_DSCP:
21194a77657cSAndrey V. Elsukov 					if (is_ipv4)
21204a77657cSAndrey V. Elsukov 						key = ip->ip_tos >> 2;
21214a77657cSAndrey V. Elsukov 					else if (is_ipv6)
21224a77657cSAndrey V. Elsukov 						key = IPV6_DSCP(
21234a77657cSAndrey V. Elsukov 						    (struct ip6_hdr *)ip) >> 2;
21244a77657cSAndrey V. Elsukov 					else
21254a77657cSAndrey V. Elsukov 						break; /* only for L3 */
21264a77657cSAndrey V. Elsukov 
21274a77657cSAndrey V. Elsukov 					key &= 0x3f;
21284a77657cSAndrey V. Elsukov 					if (cmdlen == F_INSN_SIZE(ipfw_insn_table))
21294a77657cSAndrey V. Elsukov 						key &= insntod(cmd, table)->value;
21304a77657cSAndrey V. Elsukov 					pkey = &key;
21314a77657cSAndrey V. Elsukov 					keylen = sizeof(key);
213281cac390SArseny Smalyuk 					break;
213381cac390SArseny Smalyuk 				case LOOKUP_DST_PORT:
213481cac390SArseny Smalyuk 				case LOOKUP_SRC_PORT:
21354a77657cSAndrey V. Elsukov 					/* only for L3 */
21364a77657cSAndrey V. Elsukov 					if (is_ipv6 == 0 && is_ipv4 == 0) {
213781cac390SArseny Smalyuk 						break;
21384a77657cSAndrey V. Elsukov 					}
213954e5669dSAndrey V. Elsukov 					/* Skip fragments */
21404a77657cSAndrey V. Elsukov 					if (offset != 0) {
21413b3a8eb9SGleb Smirnoff 						break;
21424a77657cSAndrey V. Elsukov 					}
214354e5669dSAndrey V. Elsukov 					/* Skip proto without ports */
214454e5669dSAndrey V. Elsukov 					if (proto != IPPROTO_TCP &&
214554e5669dSAndrey V. Elsukov 					    proto != IPPROTO_UDP &&
2146d3834420SAndrey V. Elsukov 					    proto != IPPROTO_UDPLITE &&
214754e5669dSAndrey V. Elsukov 					    proto != IPPROTO_SCTP)
214854e5669dSAndrey V. Elsukov 						break;
21494a77657cSAndrey V. Elsukov 					if (lookup_type == LOOKUP_DST_PORT)
21504a77657cSAndrey V. Elsukov 						key = dst_port;
21518ff71b03SLuigi Rizzo 					else
21524a77657cSAndrey V. Elsukov 						key = src_port;
215381cac390SArseny Smalyuk 					pkey = &key;
21544a77657cSAndrey V. Elsukov 					if (cmdlen == F_INSN_SIZE(ipfw_insn_table))
21554a77657cSAndrey V. Elsukov 						key &= insntod(cmd, table)->value;
215681cac390SArseny Smalyuk 					keylen = sizeof(key);
215781cac390SArseny Smalyuk 					break;
215881cac390SArseny Smalyuk 				case LOOKUP_DST_MAC:
215981cac390SArseny Smalyuk 				case LOOKUP_SRC_MAC:
21604a77657cSAndrey V. Elsukov 					/* only for L2 */
216181cac390SArseny Smalyuk 					if ((args->flags & IPFW_ARGS_ETHER) == 0)
216281cac390SArseny Smalyuk 						break;
21634a77657cSAndrey V. Elsukov 
21644a77657cSAndrey V. Elsukov 					pkey = lookup_type == LOOKUP_DST_MAC ?
21654a77657cSAndrey V. Elsukov 					    eh->ether_dhost : eh->ether_shost;
216681cac390SArseny Smalyuk 					keylen = ETHER_ADDR_LEN;
216781cac390SArseny Smalyuk 					break;
21684a77657cSAndrey V. Elsukov #ifndef USERSPACE
21694a77657cSAndrey V. Elsukov 				case LOOKUP_UID:
21704a77657cSAndrey V. Elsukov 				case LOOKUP_JAIL:
21714a77657cSAndrey V. Elsukov 					check_uidgid(insntod(cmd, u32),
21724a77657cSAndrey V. Elsukov 					    args, &ucred_lookup,
21734a77657cSAndrey V. Elsukov #ifdef __FreeBSD__
21744a77657cSAndrey V. Elsukov 					    &ucred_cache);
21754a77657cSAndrey V. Elsukov 					if (lookup_type == LOOKUP_UID)
21764a77657cSAndrey V. Elsukov 						key = ucred_cache->cr_uid;
21774a77657cSAndrey V. Elsukov 					else if (lookup_type == LOOKUP_JAIL)
21784a77657cSAndrey V. Elsukov 						key = ucred_cache->cr_prison->pr_id;
21794a77657cSAndrey V. Elsukov #else /* !__FreeBSD__ */
21804a77657cSAndrey V. Elsukov 					    (void *)&ucred_cache);
21814a77657cSAndrey V. Elsukov 					if (lookup_type == LOOKUP_UID)
21824a77657cSAndrey V. Elsukov 						key = ucred_cache.uid;
21834a77657cSAndrey V. Elsukov 					else if (lookup_type == LOOKUP_JAIL)
21844a77657cSAndrey V. Elsukov 						key = ucred_cache.xid;
21854a77657cSAndrey V. Elsukov #endif /* !__FreeBSD__ */
21864a77657cSAndrey V. Elsukov 					pkey = &key;
21874a77657cSAndrey V. Elsukov 					if (cmdlen == F_INSN_SIZE(ipfw_insn_table))
21884a77657cSAndrey V. Elsukov 						key &= insntod(cmd, table)->value;
21894a77657cSAndrey V. Elsukov 					keylen = sizeof(key);
21904a77657cSAndrey V. Elsukov 					break;
21914a77657cSAndrey V. Elsukov #endif /* !USERSPACE */
2192fc727ad6SBoris Lytochkin 				case LOOKUP_MARK:
2193fc727ad6SBoris Lytochkin 					key = args->rule.pkt_mark;
21944a77657cSAndrey V. Elsukov 					if (cmdlen == F_INSN_SIZE(ipfw_insn_table))
21954a77657cSAndrey V. Elsukov 						key &= insntod(cmd, table)->value;
21964a77657cSAndrey V. Elsukov 					pkey = &key;
21974a77657cSAndrey V. Elsukov 					keylen = sizeof(key);
21984a77657cSAndrey V. Elsukov 					break;
21994a77657cSAndrey V. Elsukov 				case LOOKUP_RULENUM:
22004a77657cSAndrey V. Elsukov 					key = f->rulenum;
22014a77657cSAndrey V. Elsukov 					if (cmdlen == F_INSN_SIZE(ipfw_insn_table))
22024a77657cSAndrey V. Elsukov 						key &= insntod(cmd, table)->value;
2203fc727ad6SBoris Lytochkin 					pkey = &key;
2204fc727ad6SBoris Lytochkin 					keylen = sizeof(key);
2205fc727ad6SBoris Lytochkin 					break;
220681cac390SArseny Smalyuk 				}
22074a77657cSAndrey V. Elsukov 				/* unknown key type */
220881cac390SArseny Smalyuk 				if (keylen == 0)
22093b3a8eb9SGleb Smirnoff 					break;
22103b3a8eb9SGleb Smirnoff 				match = ipfw_lookup_table(chain,
22114a77657cSAndrey V. Elsukov 				    insntod(cmd, kidx)->kidx, keylen,
22124a77657cSAndrey V. Elsukov 				    pkey, &vidx);
22134a77657cSAndrey V. Elsukov 
22144a77657cSAndrey V. Elsukov 				if (match)
221554e5669dSAndrey V. Elsukov 					tablearg = vidx;
22163b3a8eb9SGleb Smirnoff 				break;
221754e5669dSAndrey V. Elsukov 			}
22184a77657cSAndrey V. Elsukov 				/* LOOKUP_NONE */
221954e5669dSAndrey V. Elsukov 				/* FALLTHROUGH */
222054e5669dSAndrey V. Elsukov 			case O_IP_SRC_LOOKUP:
222154e5669dSAndrey V. Elsukov 			{
222254e5669dSAndrey V. Elsukov 				void *pkey;
222354e5669dSAndrey V. Elsukov 				uint32_t vidx;
222454e5669dSAndrey V. Elsukov 				uint16_t keylen;
222554e5669dSAndrey V. Elsukov 
222654e5669dSAndrey V. Elsukov 				if (is_ipv4) {
222754e5669dSAndrey V. Elsukov 					keylen = sizeof(in_addr_t);
222854e5669dSAndrey V. Elsukov 					if (cmd->opcode == O_IP_DST_LOOKUP)
222954e5669dSAndrey V. Elsukov 						pkey = &dst_ip;
223054e5669dSAndrey V. Elsukov 					else
223154e5669dSAndrey V. Elsukov 						pkey = &src_ip;
223254e5669dSAndrey V. Elsukov 				} else if (is_ipv6) {
223354e5669dSAndrey V. Elsukov 					keylen = sizeof(struct in6_addr);
223454e5669dSAndrey V. Elsukov 					if (cmd->opcode == O_IP_DST_LOOKUP)
223554e5669dSAndrey V. Elsukov 						pkey = &args->f_id.dst_ip6;
223654e5669dSAndrey V. Elsukov 					else
223754e5669dSAndrey V. Elsukov 						pkey = &args->f_id.src_ip6;
223854e5669dSAndrey V. Elsukov 				} else
223954e5669dSAndrey V. Elsukov 					break;
22404a77657cSAndrey V. Elsukov 				match = ipfw_lookup_table(chain,
22414a77657cSAndrey V. Elsukov 				    insntod(cmd, kidx)->kidx,
224254e5669dSAndrey V. Elsukov 				    keylen, pkey, &vidx);
224354e5669dSAndrey V. Elsukov 				if (!match)
224454e5669dSAndrey V. Elsukov 					break;
22454a77657cSAndrey V. Elsukov 				if (cmdlen == F_INSN_SIZE(ipfw_insn_table)) {
22464a77657cSAndrey V. Elsukov 					match = tvalue_match(chain,
22474a77657cSAndrey V. Elsukov 					    insntod(cmd, table), vidx);
224854e5669dSAndrey V. Elsukov 					if (!match)
224954e5669dSAndrey V. Elsukov 						break;
225054e5669dSAndrey V. Elsukov 				}
225154e5669dSAndrey V. Elsukov 				tablearg = vidx;
225254e5669dSAndrey V. Elsukov 				break;
225354e5669dSAndrey V. Elsukov 			}
22543b3a8eb9SGleb Smirnoff 
225581cac390SArseny Smalyuk 			case O_MAC_SRC_LOOKUP:
225681cac390SArseny Smalyuk 			case O_MAC_DST_LOOKUP:
225781cac390SArseny Smalyuk 			{
225881cac390SArseny Smalyuk 				void *pkey;
225981cac390SArseny Smalyuk 				uint32_t vidx;
226081cac390SArseny Smalyuk 				uint16_t keylen = ETHER_ADDR_LEN;
226181cac390SArseny Smalyuk 
226281cac390SArseny Smalyuk 				/* Need ether frame */
226381cac390SArseny Smalyuk 				if ((args->flags & IPFW_ARGS_ETHER) == 0)
226481cac390SArseny Smalyuk 					break;
226581cac390SArseny Smalyuk 
226681cac390SArseny Smalyuk 				if (cmd->opcode == O_MAC_DST_LOOKUP)
226781cac390SArseny Smalyuk 					pkey = eh->ether_dhost;
226881cac390SArseny Smalyuk 				else
226981cac390SArseny Smalyuk 					pkey = eh->ether_shost;
227081cac390SArseny Smalyuk 
22714a77657cSAndrey V. Elsukov 				match = ipfw_lookup_table(chain,
22724a77657cSAndrey V. Elsukov 				    insntod(cmd, kidx)->kidx,
227381cac390SArseny Smalyuk 				    keylen, pkey, &vidx);
227481cac390SArseny Smalyuk 				if (!match)
227581cac390SArseny Smalyuk 					break;
22764a77657cSAndrey V. Elsukov 				if (cmdlen == F_INSN_SIZE(ipfw_insn_table)) {
22774a77657cSAndrey V. Elsukov 					match = tvalue_match(chain,
22784a77657cSAndrey V. Elsukov 					    insntod(cmd, table), vidx);
227981cac390SArseny Smalyuk 					if (!match)
228081cac390SArseny Smalyuk 						break;
228181cac390SArseny Smalyuk 				}
228281cac390SArseny Smalyuk 				tablearg = vidx;
228381cac390SArseny Smalyuk 				break;
228481cac390SArseny Smalyuk 			}
228581cac390SArseny Smalyuk 
2286914bffb6SAlexander V. Chernikov 			case O_IP_FLOW_LOOKUP:
2287914bffb6SAlexander V. Chernikov 			{
22884a77657cSAndrey V. Elsukov 				uint32_t vidx = 0;
22894a77657cSAndrey V. Elsukov 
229054e5669dSAndrey V. Elsukov 				match = ipfw_lookup_table(chain,
22914a77657cSAndrey V. Elsukov 				    insntod(cmd, kidx)->kidx, 0,
22924a77657cSAndrey V. Elsukov 				    &args->f_id, &vidx);
2293e43ae8dcSAndrey V. Elsukov 				if (!match)
2294e43ae8dcSAndrey V. Elsukov 					break;
22954a77657cSAndrey V. Elsukov 				if (cmdlen == F_INSN_SIZE(ipfw_insn_table))
22964a77657cSAndrey V. Elsukov 					match = tvalue_match(chain,
22974a77657cSAndrey V. Elsukov 					    insntod(cmd, table), vidx);
2298914bffb6SAlexander V. Chernikov 				if (match)
22994a77657cSAndrey V. Elsukov 					tablearg = vidx;
2300914bffb6SAlexander V. Chernikov 				break;
23014a77657cSAndrey V. Elsukov 			}
23024a77657cSAndrey V. Elsukov 
23033b3a8eb9SGleb Smirnoff 			case O_IP_SRC_MASK:
23043b3a8eb9SGleb Smirnoff 			case O_IP_DST_MASK:
23053b3a8eb9SGleb Smirnoff 				if (is_ipv4) {
23063b3a8eb9SGleb Smirnoff 				    uint32_t a =
23073b3a8eb9SGleb Smirnoff 					(cmd->opcode == O_IP_DST_MASK) ?
23083b3a8eb9SGleb Smirnoff 					    dst_ip.s_addr : src_ip.s_addr;
23093b3a8eb9SGleb Smirnoff 				    uint32_t *p = ((ipfw_insn_u32 *)cmd)->d;
23103b3a8eb9SGleb Smirnoff 				    int i = cmdlen-1;
23113b3a8eb9SGleb Smirnoff 
23123b3a8eb9SGleb Smirnoff 				    for (; !match && i>0; i-= 2, p+= 2)
23133b3a8eb9SGleb Smirnoff 					match = (p[0] == (a & p[1]));
23143b3a8eb9SGleb Smirnoff 				}
23153b3a8eb9SGleb Smirnoff 				break;
23163b3a8eb9SGleb Smirnoff 
23173b3a8eb9SGleb Smirnoff 			case O_IP_SRC_ME:
23183b3a8eb9SGleb Smirnoff 				if (is_ipv4) {
23195df8171dSAndrey V. Elsukov 					match = in_localip(src_ip);
23203b3a8eb9SGleb Smirnoff 					break;
23213b3a8eb9SGleb Smirnoff 				}
23223b3a8eb9SGleb Smirnoff #ifdef INET6
23233b3a8eb9SGleb Smirnoff 				/* FALLTHROUGH */
23243b3a8eb9SGleb Smirnoff 			case O_IP6_SRC_ME:
23251cdf23bcSAndrey V. Elsukov 				match = is_ipv6 &&
23261cdf23bcSAndrey V. Elsukov 				    ipfw_localip6(&args->f_id.src_ip6);
23273b3a8eb9SGleb Smirnoff #endif
23283b3a8eb9SGleb Smirnoff 				break;
23293b3a8eb9SGleb Smirnoff 
23303b3a8eb9SGleb Smirnoff 			case O_IP_DST_SET:
23313b3a8eb9SGleb Smirnoff 			case O_IP_SRC_SET:
23323b3a8eb9SGleb Smirnoff 				if (is_ipv4) {
23333b3a8eb9SGleb Smirnoff 					u_int32_t *d = (u_int32_t *)(cmd+1);
23343b3a8eb9SGleb Smirnoff 					u_int32_t addr =
23353b3a8eb9SGleb Smirnoff 					    cmd->opcode == O_IP_DST_SET ?
23363b3a8eb9SGleb Smirnoff 						args->f_id.dst_ip :
23373b3a8eb9SGleb Smirnoff 						args->f_id.src_ip;
23383b3a8eb9SGleb Smirnoff 
23393b3a8eb9SGleb Smirnoff 					    if (addr < d[0])
23403b3a8eb9SGleb Smirnoff 						    break;
23413b3a8eb9SGleb Smirnoff 					    addr -= d[0]; /* subtract base */
23423b3a8eb9SGleb Smirnoff 					    match = (addr < cmd->arg1) &&
23433b3a8eb9SGleb Smirnoff 						( d[ 1 + (addr>>5)] &
23443b3a8eb9SGleb Smirnoff 						  (1<<(addr & 0x1f)) );
23453b3a8eb9SGleb Smirnoff 				}
23463b3a8eb9SGleb Smirnoff 				break;
23473b3a8eb9SGleb Smirnoff 
23483b3a8eb9SGleb Smirnoff 			case O_IP_DST:
23493b3a8eb9SGleb Smirnoff 				match = is_ipv4 &&
23503b3a8eb9SGleb Smirnoff 				    (((ipfw_insn_ip *)cmd)->addr.s_addr ==
23513b3a8eb9SGleb Smirnoff 				    dst_ip.s_addr);
23523b3a8eb9SGleb Smirnoff 				break;
23533b3a8eb9SGleb Smirnoff 
23543b3a8eb9SGleb Smirnoff 			case O_IP_DST_ME:
23553b3a8eb9SGleb Smirnoff 				if (is_ipv4) {
23565df8171dSAndrey V. Elsukov 					match = in_localip(dst_ip);
23573b3a8eb9SGleb Smirnoff 					break;
23583b3a8eb9SGleb Smirnoff 				}
23593b3a8eb9SGleb Smirnoff #ifdef INET6
23603b3a8eb9SGleb Smirnoff 				/* FALLTHROUGH */
23613b3a8eb9SGleb Smirnoff 			case O_IP6_DST_ME:
23621cdf23bcSAndrey V. Elsukov 				match = is_ipv6 &&
23631cdf23bcSAndrey V. Elsukov 				    ipfw_localip6(&args->f_id.dst_ip6);
23643b3a8eb9SGleb Smirnoff #endif
23653b3a8eb9SGleb Smirnoff 				break;
23663b3a8eb9SGleb Smirnoff 
23673b3a8eb9SGleb Smirnoff 			case O_IP_SRCPORT:
23683b3a8eb9SGleb Smirnoff 			case O_IP_DSTPORT:
23693b3a8eb9SGleb Smirnoff 				/*
23703b3a8eb9SGleb Smirnoff 				 * offset == 0 && proto != 0 is enough
23713b3a8eb9SGleb Smirnoff 				 * to guarantee that we have a
23723b3a8eb9SGleb Smirnoff 				 * packet with port info.
23733b3a8eb9SGleb Smirnoff 				 */
2374d3834420SAndrey V. Elsukov 				if ((proto == IPPROTO_UDP ||
2375d3834420SAndrey V. Elsukov 				    proto == IPPROTO_UDPLITE ||
2376d3834420SAndrey V. Elsukov 				    proto == IPPROTO_TCP ||
237794590638SMichael Tuexen 				    proto == IPPROTO_SCTP) && offset == 0) {
23783b3a8eb9SGleb Smirnoff 					u_int16_t x =
23793b3a8eb9SGleb Smirnoff 					    (cmd->opcode == O_IP_SRCPORT) ?
23803b3a8eb9SGleb Smirnoff 						src_port : dst_port ;
23813b3a8eb9SGleb Smirnoff 					u_int16_t *p =
23823b3a8eb9SGleb Smirnoff 					    ((ipfw_insn_u16 *)cmd)->ports;
23833b3a8eb9SGleb Smirnoff 					int i;
23843b3a8eb9SGleb Smirnoff 
23853b3a8eb9SGleb Smirnoff 					for (i = cmdlen - 1; !match && i>0;
23863b3a8eb9SGleb Smirnoff 					    i--, p += 2)
23873b3a8eb9SGleb Smirnoff 						match = (x>=p[0] && x<=p[1]);
23883b3a8eb9SGleb Smirnoff 				}
23893b3a8eb9SGleb Smirnoff 				break;
23903b3a8eb9SGleb Smirnoff 
23913b3a8eb9SGleb Smirnoff 			case O_ICMPTYPE:
23923b3a8eb9SGleb Smirnoff 				match = (offset == 0 && proto==IPPROTO_ICMP &&
23933b3a8eb9SGleb Smirnoff 				    icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) );
23943b3a8eb9SGleb Smirnoff 				break;
23953b3a8eb9SGleb Smirnoff 
23963b3a8eb9SGleb Smirnoff #ifdef INET6
23973b3a8eb9SGleb Smirnoff 			case O_ICMP6TYPE:
23983b3a8eb9SGleb Smirnoff 				match = is_ipv6 && offset == 0 &&
23993b3a8eb9SGleb Smirnoff 				    proto==IPPROTO_ICMPV6 &&
24003b3a8eb9SGleb Smirnoff 				    icmp6type_match(
24013b3a8eb9SGleb Smirnoff 					ICMP6(ulp)->icmp6_type,
24023b3a8eb9SGleb Smirnoff 					(ipfw_insn_u32 *)cmd);
24033b3a8eb9SGleb Smirnoff 				break;
24043b3a8eb9SGleb Smirnoff #endif /* INET6 */
24053b3a8eb9SGleb Smirnoff 
24063b3a8eb9SGleb Smirnoff 			case O_IPOPT:
24073b3a8eb9SGleb Smirnoff 				match = (is_ipv4 &&
24083b3a8eb9SGleb Smirnoff 				    ipopts_match(ip, cmd) );
24093b3a8eb9SGleb Smirnoff 				break;
24103b3a8eb9SGleb Smirnoff 
24113b3a8eb9SGleb Smirnoff 			case O_IPVER:
24121388cfe1SMark Johnston 				match = ((is_ipv4 || is_ipv6) &&
24133b3a8eb9SGleb Smirnoff 				    cmd->arg1 == ip->ip_v);
24143b3a8eb9SGleb Smirnoff 				break;
24153b3a8eb9SGleb Smirnoff 
24163b3a8eb9SGleb Smirnoff 			case O_IPID:
24173b3a8eb9SGleb Smirnoff 			case O_IPTTL:
241890ecb41fSAndrey V. Elsukov 				if (!is_ipv4)
241990ecb41fSAndrey V. Elsukov 					break;
242090ecb41fSAndrey V. Elsukov 			case O_IPLEN:
242190ecb41fSAndrey V. Elsukov 				{	/* only for IP packets */
24223b3a8eb9SGleb Smirnoff 				    uint16_t x;
24233b3a8eb9SGleb Smirnoff 				    uint16_t *p;
24243b3a8eb9SGleb Smirnoff 				    int i;
24253b3a8eb9SGleb Smirnoff 
24263b3a8eb9SGleb Smirnoff 				    if (cmd->opcode == O_IPLEN)
24273b3a8eb9SGleb Smirnoff 					x = iplen;
24283b3a8eb9SGleb Smirnoff 				    else if (cmd->opcode == O_IPTTL)
24293b3a8eb9SGleb Smirnoff 					x = ip->ip_ttl;
24303b3a8eb9SGleb Smirnoff 				    else /* must be IPID */
24313b3a8eb9SGleb Smirnoff 					x = ntohs(ip->ip_id);
24323b3a8eb9SGleb Smirnoff 				    if (cmdlen == 1) {
24333b3a8eb9SGleb Smirnoff 					match = (cmd->arg1 == x);
24343b3a8eb9SGleb Smirnoff 					break;
24353b3a8eb9SGleb Smirnoff 				    }
24363b3a8eb9SGleb Smirnoff 				    /* otherwise we have ranges */
24373b3a8eb9SGleb Smirnoff 				    p = ((ipfw_insn_u16 *)cmd)->ports;
24383b3a8eb9SGleb Smirnoff 				    i = cmdlen - 1;
24393b3a8eb9SGleb Smirnoff 				    for (; !match && i>0; i--, p += 2)
24403b3a8eb9SGleb Smirnoff 					match = (x >= p[0] && x <= p[1]);
24413b3a8eb9SGleb Smirnoff 				}
24423b3a8eb9SGleb Smirnoff 				break;
24433b3a8eb9SGleb Smirnoff 
24443b3a8eb9SGleb Smirnoff 			case O_IPPRECEDENCE:
24453b3a8eb9SGleb Smirnoff 				match = (is_ipv4 &&
24463b3a8eb9SGleb Smirnoff 				    (cmd->arg1 == (ip->ip_tos & 0xe0)) );
24473b3a8eb9SGleb Smirnoff 				break;
24483b3a8eb9SGleb Smirnoff 
24493b3a8eb9SGleb Smirnoff 			case O_IPTOS:
24503b3a8eb9SGleb Smirnoff 				match = (is_ipv4 &&
24513b3a8eb9SGleb Smirnoff 				    flags_match(cmd, ip->ip_tos));
24523b3a8eb9SGleb Smirnoff 				break;
24533b3a8eb9SGleb Smirnoff 
2454ae01d73cSAlexander V. Chernikov 			case O_DSCP:
2455ae01d73cSAlexander V. Chernikov 			    {
2456ae01d73cSAlexander V. Chernikov 				uint32_t *p;
2457ae01d73cSAlexander V. Chernikov 				uint16_t x;
2458ae01d73cSAlexander V. Chernikov 
2459ae01d73cSAlexander V. Chernikov 				p = ((ipfw_insn_u32 *)cmd)->d;
2460ae01d73cSAlexander V. Chernikov 
2461ae01d73cSAlexander V. Chernikov 				if (is_ipv4)
2462ae01d73cSAlexander V. Chernikov 					x = ip->ip_tos >> 2;
2463ae01d73cSAlexander V. Chernikov 				else if (is_ipv6) {
24644763c0aaSAndrey V. Elsukov 					x = IPV6_DSCP(
24654763c0aaSAndrey V. Elsukov 					    (struct ip6_hdr *)ip) >> 2;
24664763c0aaSAndrey V. Elsukov 					x &= 0x3f;
2467ae01d73cSAlexander V. Chernikov 				} else
2468ae01d73cSAlexander V. Chernikov 					break;
2469ae01d73cSAlexander V. Chernikov 
2470ae01d73cSAlexander V. Chernikov 				/* DSCP bitmask is stored as low_u32 high_u32 */
247123a6c733SAndrey V. Elsukov 				if (x >= 32)
2472ae01d73cSAlexander V. Chernikov 					match = *(p + 1) & (1 << (x - 32));
2473ae01d73cSAlexander V. Chernikov 				else
2474ae01d73cSAlexander V. Chernikov 					match = *p & (1 << x);
2475ae01d73cSAlexander V. Chernikov 			    }
2476ae01d73cSAlexander V. Chernikov 				break;
2477ae01d73cSAlexander V. Chernikov 
24783b3a8eb9SGleb Smirnoff 			case O_TCPDATALEN:
24793b3a8eb9SGleb Smirnoff 				if (proto == IPPROTO_TCP && offset == 0) {
24803b3a8eb9SGleb Smirnoff 				    struct tcphdr *tcp;
24813b3a8eb9SGleb Smirnoff 				    uint16_t x;
24823b3a8eb9SGleb Smirnoff 				    uint16_t *p;
24833b3a8eb9SGleb Smirnoff 				    int i;
24845c70ebfaSAndrey V. Elsukov #ifdef INET6
24855c70ebfaSAndrey V. Elsukov 				    if (is_ipv6) {
24865c70ebfaSAndrey V. Elsukov 					    struct ip6_hdr *ip6;
24873b3a8eb9SGleb Smirnoff 
24885c70ebfaSAndrey V. Elsukov 					    ip6 = (struct ip6_hdr *)ip;
24895c70ebfaSAndrey V. Elsukov 					    if (ip6->ip6_plen == 0) {
24905c70ebfaSAndrey V. Elsukov 						    /*
24915c70ebfaSAndrey V. Elsukov 						     * Jumbo payload is not
24925c70ebfaSAndrey V. Elsukov 						     * supported by this
24935c70ebfaSAndrey V. Elsukov 						     * opcode.
24945c70ebfaSAndrey V. Elsukov 						     */
24955c70ebfaSAndrey V. Elsukov 						    break;
24965c70ebfaSAndrey V. Elsukov 					    }
24975c70ebfaSAndrey V. Elsukov 					    x = iplen - hlen;
24985c70ebfaSAndrey V. Elsukov 				    } else
24995c70ebfaSAndrey V. Elsukov #endif /* INET6 */
25005c70ebfaSAndrey V. Elsukov 					    x = iplen - (ip->ip_hl << 2);
25013b3a8eb9SGleb Smirnoff 				    tcp = TCP(ulp);
25025c70ebfaSAndrey V. Elsukov 				    x -= tcp->th_off << 2;
25033b3a8eb9SGleb Smirnoff 				    if (cmdlen == 1) {
25043b3a8eb9SGleb Smirnoff 					match = (cmd->arg1 == x);
25053b3a8eb9SGleb Smirnoff 					break;
25063b3a8eb9SGleb Smirnoff 				    }
25073b3a8eb9SGleb Smirnoff 				    /* otherwise we have ranges */
25083b3a8eb9SGleb Smirnoff 				    p = ((ipfw_insn_u16 *)cmd)->ports;
25093b3a8eb9SGleb Smirnoff 				    i = cmdlen - 1;
25103b3a8eb9SGleb Smirnoff 				    for (; !match && i>0; i--, p += 2)
25113b3a8eb9SGleb Smirnoff 					match = (x >= p[0] && x <= p[1]);
25123b3a8eb9SGleb Smirnoff 				}
25133b3a8eb9SGleb Smirnoff 				break;
25143b3a8eb9SGleb Smirnoff 
25153b3a8eb9SGleb Smirnoff 			case O_TCPFLAGS:
25160fc7bdc9SRichard Scheffenegger 				/*
25170fc7bdc9SRichard Scheffenegger 				 * Note that this is currently only set up to
25180fc7bdc9SRichard Scheffenegger 				 * match the lower 8 TCP header flag bits, not
25190fc7bdc9SRichard Scheffenegger 				 * the full compliment of all 12 flags.
25200fc7bdc9SRichard Scheffenegger 				 */
25213b3a8eb9SGleb Smirnoff 				match = (proto == IPPROTO_TCP && offset == 0 &&
25220fc7bdc9SRichard Scheffenegger 				    flags_match(cmd, tcp_get_flags(TCP(ulp))));
25233b3a8eb9SGleb Smirnoff 				break;
25243b3a8eb9SGleb Smirnoff 
25253b3a8eb9SGleb Smirnoff 			case O_TCPOPTS:
2526bbd5a842SAlexander V. Chernikov 				if (proto == IPPROTO_TCP && offset == 0 && ulp){
2527d4e6a529SAndrey V. Elsukov 					PULLUP_LEN_LOCKED(hlen, ulp,
252836159814SAlexander V. Chernikov 					    (TCP(ulp)->th_off << 2));
2529bbd5a842SAlexander V. Chernikov 					match = tcpopts_match(TCP(ulp), cmd);
253036159814SAlexander V. Chernikov 				}
25313b3a8eb9SGleb Smirnoff 				break;
25323b3a8eb9SGleb Smirnoff 
25333b3a8eb9SGleb Smirnoff 			case O_TCPSEQ:
25343b3a8eb9SGleb Smirnoff 				match = (proto == IPPROTO_TCP && offset == 0 &&
25353b3a8eb9SGleb Smirnoff 				    ((ipfw_insn_u32 *)cmd)->d[0] ==
25363b3a8eb9SGleb Smirnoff 					TCP(ulp)->th_seq);
25373b3a8eb9SGleb Smirnoff 				break;
25383b3a8eb9SGleb Smirnoff 
25393b3a8eb9SGleb Smirnoff 			case O_TCPACK:
25403b3a8eb9SGleb Smirnoff 				match = (proto == IPPROTO_TCP && offset == 0 &&
25413b3a8eb9SGleb Smirnoff 				    ((ipfw_insn_u32 *)cmd)->d[0] ==
25423b3a8eb9SGleb Smirnoff 					TCP(ulp)->th_ack);
25433b3a8eb9SGleb Smirnoff 				break;
25443b3a8eb9SGleb Smirnoff 
2545978f2d17SAndrey V. Elsukov 			case O_TCPMSS:
2546978f2d17SAndrey V. Elsukov 				if (proto == IPPROTO_TCP &&
2547978f2d17SAndrey V. Elsukov 				    (args->f_id._flags & TH_SYN) != 0 &&
2548978f2d17SAndrey V. Elsukov 				    ulp != NULL) {
2549978f2d17SAndrey V. Elsukov 					uint16_t mss, *p;
2550978f2d17SAndrey V. Elsukov 					int i;
2551978f2d17SAndrey V. Elsukov 
2552d4e6a529SAndrey V. Elsukov 					PULLUP_LEN_LOCKED(hlen, ulp,
2553978f2d17SAndrey V. Elsukov 					    (TCP(ulp)->th_off << 2));
2554978f2d17SAndrey V. Elsukov 					if ((tcpopts_parse(TCP(ulp), &mss) &
2555978f2d17SAndrey V. Elsukov 					    IP_FW_TCPOPT_MSS) == 0)
2556978f2d17SAndrey V. Elsukov 						break;
2557978f2d17SAndrey V. Elsukov 					if (cmdlen == 1) {
2558978f2d17SAndrey V. Elsukov 						match = (cmd->arg1 == mss);
2559978f2d17SAndrey V. Elsukov 						break;
2560978f2d17SAndrey V. Elsukov 					}
2561978f2d17SAndrey V. Elsukov 					/* Otherwise we have ranges. */
2562978f2d17SAndrey V. Elsukov 					p = ((ipfw_insn_u16 *)cmd)->ports;
2563978f2d17SAndrey V. Elsukov 					i = cmdlen - 1;
2564978f2d17SAndrey V. Elsukov 					for (; !match && i > 0; i--, p += 2)
2565978f2d17SAndrey V. Elsukov 						match = (mss >= p[0] &&
2566978f2d17SAndrey V. Elsukov 						    mss <= p[1]);
2567978f2d17SAndrey V. Elsukov 				}
2568978f2d17SAndrey V. Elsukov 				break;
2569978f2d17SAndrey V. Elsukov 
25703b3a8eb9SGleb Smirnoff 			case O_TCPWIN:
25713b3a8eb9SGleb Smirnoff 				if (proto == IPPROTO_TCP && offset == 0) {
25723b3a8eb9SGleb Smirnoff 				    uint16_t x;
25733b3a8eb9SGleb Smirnoff 				    uint16_t *p;
25743b3a8eb9SGleb Smirnoff 				    int i;
25753b3a8eb9SGleb Smirnoff 
25763b3a8eb9SGleb Smirnoff 				    x = ntohs(TCP(ulp)->th_win);
25773b3a8eb9SGleb Smirnoff 				    if (cmdlen == 1) {
25783b3a8eb9SGleb Smirnoff 					match = (cmd->arg1 == x);
25793b3a8eb9SGleb Smirnoff 					break;
25803b3a8eb9SGleb Smirnoff 				    }
25813b3a8eb9SGleb Smirnoff 				    /* Otherwise we have ranges. */
25823b3a8eb9SGleb Smirnoff 				    p = ((ipfw_insn_u16 *)cmd)->ports;
25833b3a8eb9SGleb Smirnoff 				    i = cmdlen - 1;
25843b3a8eb9SGleb Smirnoff 				    for (; !match && i > 0; i--, p += 2)
25853b3a8eb9SGleb Smirnoff 					match = (x >= p[0] && x <= p[1]);
25863b3a8eb9SGleb Smirnoff 				}
25873b3a8eb9SGleb Smirnoff 				break;
25883b3a8eb9SGleb Smirnoff 
25893b3a8eb9SGleb Smirnoff 			case O_ESTAB:
25903b3a8eb9SGleb Smirnoff 				/* reject packets which have SYN only */
25913b3a8eb9SGleb Smirnoff 				/* XXX should i also check for TH_ACK ? */
25923b3a8eb9SGleb Smirnoff 				match = (proto == IPPROTO_TCP && offset == 0 &&
25930fc7bdc9SRichard Scheffenegger 				    (tcp_get_flags(TCP(ulp)) &
25943b3a8eb9SGleb Smirnoff 				     (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
25953b3a8eb9SGleb Smirnoff 				break;
25963b3a8eb9SGleb Smirnoff 
25973b3a8eb9SGleb Smirnoff 			case O_ALTQ: {
25983b3a8eb9SGleb Smirnoff 				struct pf_mtag *at;
25993b3a8eb9SGleb Smirnoff 				struct m_tag *mtag;
26003b3a8eb9SGleb Smirnoff 				ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
26013b3a8eb9SGleb Smirnoff 
26023b3a8eb9SGleb Smirnoff 				/*
26033b3a8eb9SGleb Smirnoff 				 * ALTQ uses mbuf tags from another
26043b3a8eb9SGleb Smirnoff 				 * packet filtering system - pf(4).
26053b3a8eb9SGleb Smirnoff 				 * We allocate a tag in its format
26063b3a8eb9SGleb Smirnoff 				 * and fill it in, pretending to be pf(4).
26073b3a8eb9SGleb Smirnoff 				 */
26083b3a8eb9SGleb Smirnoff 				match = 1;
26093b3a8eb9SGleb Smirnoff 				at = pf_find_mtag(m);
26103b3a8eb9SGleb Smirnoff 				if (at != NULL && at->qid != 0)
26113b3a8eb9SGleb Smirnoff 					break;
26123b3a8eb9SGleb Smirnoff 				mtag = m_tag_get(PACKET_TAG_PF,
26133b3a8eb9SGleb Smirnoff 				    sizeof(struct pf_mtag), M_NOWAIT | M_ZERO);
26143b3a8eb9SGleb Smirnoff 				if (mtag == NULL) {
26153b3a8eb9SGleb Smirnoff 					/*
26163b3a8eb9SGleb Smirnoff 					 * Let the packet fall back to the
26173b3a8eb9SGleb Smirnoff 					 * default ALTQ.
26183b3a8eb9SGleb Smirnoff 					 */
26193b3a8eb9SGleb Smirnoff 					break;
26203b3a8eb9SGleb Smirnoff 				}
26213b3a8eb9SGleb Smirnoff 				m_tag_prepend(m, mtag);
26223b3a8eb9SGleb Smirnoff 				at = (struct pf_mtag *)(mtag + 1);
26233b3a8eb9SGleb Smirnoff 				at->qid = altq->qid;
26243b3a8eb9SGleb Smirnoff 				at->hdr = ip;
26253b3a8eb9SGleb Smirnoff 				break;
26263b3a8eb9SGleb Smirnoff 			}
26273b3a8eb9SGleb Smirnoff 
26283b3a8eb9SGleb Smirnoff 			case O_LOG:
2629f355cb3eSGleb Smirnoff 				ipfw_log(chain, f, hlen, args,
26304a77657cSAndrey V. Elsukov 				    offset | ip6f_mf, tablearg, ip, eh);
26313b3a8eb9SGleb Smirnoff 				match = 1;
26323b3a8eb9SGleb Smirnoff 				break;
26333b3a8eb9SGleb Smirnoff 
26343b3a8eb9SGleb Smirnoff 			case O_PROB:
26353b3a8eb9SGleb Smirnoff 				match = (random()<((ipfw_insn_u32 *)cmd)->d[0]);
26363b3a8eb9SGleb Smirnoff 				break;
26373b3a8eb9SGleb Smirnoff 
26383b3a8eb9SGleb Smirnoff 			case O_VERREVPATH:
26393b3a8eb9SGleb Smirnoff 				/* Outgoing packets automatically pass/match */
2640b7795b67SGleb Smirnoff 				match = (args->flags & IPFW_ARGS_OUT ||
26413b3a8eb9SGleb Smirnoff 				    (
26423b3a8eb9SGleb Smirnoff #ifdef INET6
26433b3a8eb9SGleb Smirnoff 				    is_ipv6 ?
26443b3a8eb9SGleb Smirnoff 					verify_path6(&(args->f_id.src_ip6),
2645b7795b67SGleb Smirnoff 					    iif, args->f_id.fib) :
26463b3a8eb9SGleb Smirnoff #endif
2647b7795b67SGleb Smirnoff 				    verify_path(src_ip, iif, args->f_id.fib)));
26483b3a8eb9SGleb Smirnoff 				break;
26493b3a8eb9SGleb Smirnoff 
26503b3a8eb9SGleb Smirnoff 			case O_VERSRCREACH:
26513b3a8eb9SGleb Smirnoff 				/* Outgoing packets automatically pass/match */
2652986368d8SAndrey V. Elsukov 				match = (hlen > 0 && ((oif != NULL) || (
26533b3a8eb9SGleb Smirnoff #ifdef INET6
26543b3a8eb9SGleb Smirnoff 				    is_ipv6 ?
26553b3a8eb9SGleb Smirnoff 				        verify_path6(&(args->f_id.src_ip6),
26563b3a8eb9SGleb Smirnoff 				            NULL, args->f_id.fib) :
26573b3a8eb9SGleb Smirnoff #endif
2658986368d8SAndrey V. Elsukov 				    verify_path(src_ip, NULL, args->f_id.fib))));
26593b3a8eb9SGleb Smirnoff 				break;
26603b3a8eb9SGleb Smirnoff 
26613b3a8eb9SGleb Smirnoff 			case O_ANTISPOOF:
26623b3a8eb9SGleb Smirnoff 				/* Outgoing packets automatically pass/match */
26633b3a8eb9SGleb Smirnoff 				if (oif == NULL && hlen > 0 &&
26643b3a8eb9SGleb Smirnoff 				    (  (is_ipv4 && in_localaddr(src_ip))
26653b3a8eb9SGleb Smirnoff #ifdef INET6
26663b3a8eb9SGleb Smirnoff 				    || (is_ipv6 &&
26673b3a8eb9SGleb Smirnoff 				        in6_localaddr(&(args->f_id.src_ip6)))
26683b3a8eb9SGleb Smirnoff #endif
26693b3a8eb9SGleb Smirnoff 				    ))
26703b3a8eb9SGleb Smirnoff 					match =
26713b3a8eb9SGleb Smirnoff #ifdef INET6
26723b3a8eb9SGleb Smirnoff 					    is_ipv6 ? verify_path6(
2673b7795b67SGleb Smirnoff 					        &(args->f_id.src_ip6), iif,
26743b3a8eb9SGleb Smirnoff 						args->f_id.fib) :
26753b3a8eb9SGleb Smirnoff #endif
2676b7795b67SGleb Smirnoff 					    verify_path(src_ip, iif,
26773b3a8eb9SGleb Smirnoff 					        args->f_id.fib);
26783b3a8eb9SGleb Smirnoff 				else
26793b3a8eb9SGleb Smirnoff 					match = 1;
26803b3a8eb9SGleb Smirnoff 				break;
26813b3a8eb9SGleb Smirnoff 
26823b3a8eb9SGleb Smirnoff 			case O_IPSEC:
26833b3a8eb9SGleb Smirnoff 				match = (m_tag_find(m,
26843b3a8eb9SGleb Smirnoff 				    PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL);
26853b3a8eb9SGleb Smirnoff 				/* otherwise no match */
26863b3a8eb9SGleb Smirnoff 				break;
26873b3a8eb9SGleb Smirnoff 
26883b3a8eb9SGleb Smirnoff #ifdef INET6
26893b3a8eb9SGleb Smirnoff 			case O_IP6_SRC:
26903b3a8eb9SGleb Smirnoff 				match = is_ipv6 &&
26913b3a8eb9SGleb Smirnoff 				    IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6,
26923b3a8eb9SGleb Smirnoff 				    &((ipfw_insn_ip6 *)cmd)->addr6);
26933b3a8eb9SGleb Smirnoff 				break;
26943b3a8eb9SGleb Smirnoff 
26953b3a8eb9SGleb Smirnoff 			case O_IP6_DST:
26963b3a8eb9SGleb Smirnoff 				match = is_ipv6 &&
26973b3a8eb9SGleb Smirnoff 				IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6,
26983b3a8eb9SGleb Smirnoff 				    &((ipfw_insn_ip6 *)cmd)->addr6);
26993b3a8eb9SGleb Smirnoff 				break;
27003b3a8eb9SGleb Smirnoff 			case O_IP6_SRC_MASK:
27013b3a8eb9SGleb Smirnoff 			case O_IP6_DST_MASK:
27023b3a8eb9SGleb Smirnoff 				if (is_ipv6) {
27033b3a8eb9SGleb Smirnoff 					int i = cmdlen - 1;
27043b3a8eb9SGleb Smirnoff 					struct in6_addr p;
27053b3a8eb9SGleb Smirnoff 					struct in6_addr *d =
27063b3a8eb9SGleb Smirnoff 					    &((ipfw_insn_ip6 *)cmd)->addr6;
27073b3a8eb9SGleb Smirnoff 
27083b3a8eb9SGleb Smirnoff 					for (; !match && i > 0; d += 2,
27093b3a8eb9SGleb Smirnoff 					    i -= F_INSN_SIZE(struct in6_addr)
27103b3a8eb9SGleb Smirnoff 					    * 2) {
27113b3a8eb9SGleb Smirnoff 						p = (cmd->opcode ==
27123b3a8eb9SGleb Smirnoff 						    O_IP6_SRC_MASK) ?
27133b3a8eb9SGleb Smirnoff 						    args->f_id.src_ip6:
27143b3a8eb9SGleb Smirnoff 						    args->f_id.dst_ip6;
27153b3a8eb9SGleb Smirnoff 						APPLY_MASK(&p, &d[1]);
27163b3a8eb9SGleb Smirnoff 						match =
27173b3a8eb9SGleb Smirnoff 						    IN6_ARE_ADDR_EQUAL(&d[0],
27183b3a8eb9SGleb Smirnoff 						    &p);
27193b3a8eb9SGleb Smirnoff 					}
27203b3a8eb9SGleb Smirnoff 				}
27213b3a8eb9SGleb Smirnoff 				break;
27223b3a8eb9SGleb Smirnoff 
27233b3a8eb9SGleb Smirnoff 			case O_FLOW6ID:
27243b3a8eb9SGleb Smirnoff 				match = is_ipv6 &&
27253b3a8eb9SGleb Smirnoff 				    flow6id_match(args->f_id.flow_id6,
27263b3a8eb9SGleb Smirnoff 				    (ipfw_insn_u32 *) cmd);
27273b3a8eb9SGleb Smirnoff 				break;
27283b3a8eb9SGleb Smirnoff 
27293b3a8eb9SGleb Smirnoff 			case O_EXT_HDR:
27303b3a8eb9SGleb Smirnoff 				match = is_ipv6 &&
27313b3a8eb9SGleb Smirnoff 				    (ext_hd & ((ipfw_insn *) cmd)->arg1);
27323b3a8eb9SGleb Smirnoff 				break;
27333b3a8eb9SGleb Smirnoff 
27343b3a8eb9SGleb Smirnoff 			case O_IP6:
27353b3a8eb9SGleb Smirnoff 				match = is_ipv6;
27363b3a8eb9SGleb Smirnoff 				break;
27373b3a8eb9SGleb Smirnoff #endif
27383b3a8eb9SGleb Smirnoff 
27393b3a8eb9SGleb Smirnoff 			case O_IP4:
27403b3a8eb9SGleb Smirnoff 				match = is_ipv4;
27413b3a8eb9SGleb Smirnoff 				break;
27423b3a8eb9SGleb Smirnoff 
27433b3a8eb9SGleb Smirnoff 			case O_TAG: {
27443b3a8eb9SGleb Smirnoff 				struct m_tag *mtag;
27450cba2b28SAlexander V. Chernikov 				uint32_t tag = TARG(cmd->arg1, tag);
27463b3a8eb9SGleb Smirnoff 
27473b3a8eb9SGleb Smirnoff 				/* Packet is already tagged with this tag? */
27483b3a8eb9SGleb Smirnoff 				mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL);
27493b3a8eb9SGleb Smirnoff 
27503b3a8eb9SGleb Smirnoff 				/* We have `untag' action when F_NOT flag is
27513b3a8eb9SGleb Smirnoff 				 * present. And we must remove this mtag from
27523b3a8eb9SGleb Smirnoff 				 * mbuf and reset `match' to zero (`match' will
27533b3a8eb9SGleb Smirnoff 				 * be inversed later).
27543b3a8eb9SGleb Smirnoff 				 * Otherwise we should allocate new mtag and
27553b3a8eb9SGleb Smirnoff 				 * push it into mbuf.
27563b3a8eb9SGleb Smirnoff 				 */
27573b3a8eb9SGleb Smirnoff 				if (cmd->len & F_NOT) { /* `untag' action */
27583b3a8eb9SGleb Smirnoff 					if (mtag != NULL)
27593b3a8eb9SGleb Smirnoff 						m_tag_delete(m, mtag);
27603b3a8eb9SGleb Smirnoff 					match = 0;
27613b3a8eb9SGleb Smirnoff 				} else {
27623b3a8eb9SGleb Smirnoff 					if (mtag == NULL) {
27633b3a8eb9SGleb Smirnoff 						mtag = m_tag_alloc( MTAG_IPFW,
27643b3a8eb9SGleb Smirnoff 						    tag, 0, M_NOWAIT);
27653b3a8eb9SGleb Smirnoff 						if (mtag != NULL)
27663b3a8eb9SGleb Smirnoff 							m_tag_prepend(m, mtag);
27673b3a8eb9SGleb Smirnoff 					}
27683b3a8eb9SGleb Smirnoff 					match = 1;
27693b3a8eb9SGleb Smirnoff 				}
27703b3a8eb9SGleb Smirnoff 				break;
27713b3a8eb9SGleb Smirnoff 			}
27723b3a8eb9SGleb Smirnoff 
27733b3a8eb9SGleb Smirnoff 			case O_FIB: /* try match the specified fib */
27743b3a8eb9SGleb Smirnoff 				if (args->f_id.fib == cmd->arg1)
27753b3a8eb9SGleb Smirnoff 					match = 1;
27763b3a8eb9SGleb Smirnoff 				break;
27773b3a8eb9SGleb Smirnoff 
27783b3a8eb9SGleb Smirnoff 			case O_SOCKARG:	{
2779f783a35cSLuigi Rizzo #ifndef USERSPACE	/* not supported in userspace */
27803b3a8eb9SGleb Smirnoff 				struct inpcb *inp = args->inp;
27813b3a8eb9SGleb Smirnoff 				struct inpcbinfo *pi;
2782a9f7eba9SAndrey V. Elsukov 				bool inp_locked = false;
27833b3a8eb9SGleb Smirnoff 
27843b3a8eb9SGleb Smirnoff 				if (proto == IPPROTO_TCP)
27853b3a8eb9SGleb Smirnoff 					pi = &V_tcbinfo;
27863b3a8eb9SGleb Smirnoff 				else if (proto == IPPROTO_UDP)
27873b3a8eb9SGleb Smirnoff 					pi = &V_udbinfo;
2788d3834420SAndrey V. Elsukov 				else if (proto == IPPROTO_UDPLITE)
2789d3834420SAndrey V. Elsukov 					pi = &V_ulitecbinfo;
27903b3a8eb9SGleb Smirnoff 				else
27913b3a8eb9SGleb Smirnoff 					break;
27923b3a8eb9SGleb Smirnoff 
27933b3a8eb9SGleb Smirnoff 				/*
27943b3a8eb9SGleb Smirnoff 				 * XXXRW: so_user_cookie should almost
27953b3a8eb9SGleb Smirnoff 				 * certainly be inp_user_cookie?
27963b3a8eb9SGleb Smirnoff 				 */
27973b3a8eb9SGleb Smirnoff 
2798a9f7eba9SAndrey V. Elsukov 				/*
2799a9f7eba9SAndrey V. Elsukov 				 * For incoming packet lookup the inpcb
2800a9f7eba9SAndrey V. Elsukov 				 * using the src/dest ip/port tuple.
2801a9f7eba9SAndrey V. Elsukov 				 */
2802a9f7eba9SAndrey V. Elsukov 				if (is_ipv4 && inp == NULL) {
28033b3a8eb9SGleb Smirnoff 					inp = in_pcblookup(pi,
28043b3a8eb9SGleb Smirnoff 					    src_ip, htons(src_port),
28053b3a8eb9SGleb Smirnoff 					    dst_ip, htons(dst_port),
28063b3a8eb9SGleb Smirnoff 					    INPLOOKUP_RLOCKPCB, NULL);
2807a9f7eba9SAndrey V. Elsukov 					inp_locked = true;
28083b3a8eb9SGleb Smirnoff 				}
2809a9f7eba9SAndrey V. Elsukov #ifdef INET6
2810a9f7eba9SAndrey V. Elsukov 				if (is_ipv6 && inp == NULL) {
2811a9f7eba9SAndrey V. Elsukov 					inp = in6_pcblookup(pi,
2812a9f7eba9SAndrey V. Elsukov 					    &args->f_id.src_ip6,
2813a9f7eba9SAndrey V. Elsukov 					    htons(src_port),
2814a9f7eba9SAndrey V. Elsukov 					    &args->f_id.dst_ip6,
2815a9f7eba9SAndrey V. Elsukov 					    htons(dst_port),
2816a9f7eba9SAndrey V. Elsukov 					    INPLOOKUP_RLOCKPCB, NULL);
2817a9f7eba9SAndrey V. Elsukov 					inp_locked = true;
2818a9f7eba9SAndrey V. Elsukov 				}
2819a9f7eba9SAndrey V. Elsukov #endif /* INET6 */
2820a9f7eba9SAndrey V. Elsukov 				if (inp != NULL) {
28213b3a8eb9SGleb Smirnoff 					if (inp->inp_socket) {
28223b3a8eb9SGleb Smirnoff 						tablearg =
28233b3a8eb9SGleb Smirnoff 						    inp->inp_socket->so_user_cookie;
28243b3a8eb9SGleb Smirnoff 						if (tablearg)
28253b3a8eb9SGleb Smirnoff 							match = 1;
28263b3a8eb9SGleb Smirnoff 					}
2827a9f7eba9SAndrey V. Elsukov 					if (inp_locked)
2828a9f7eba9SAndrey V. Elsukov 						INP_RUNLOCK(inp);
28293b3a8eb9SGleb Smirnoff 				}
2830f783a35cSLuigi Rizzo #endif /* !USERSPACE */
28313b3a8eb9SGleb Smirnoff 				break;
28323b3a8eb9SGleb Smirnoff 			}
28333b3a8eb9SGleb Smirnoff 
28343b3a8eb9SGleb Smirnoff 			case O_TAGGED: {
28353b3a8eb9SGleb Smirnoff 				struct m_tag *mtag;
28360cba2b28SAlexander V. Chernikov 				uint32_t tag = TARG(cmd->arg1, tag);
28373b3a8eb9SGleb Smirnoff 
28383b3a8eb9SGleb Smirnoff 				if (cmdlen == 1) {
28393b3a8eb9SGleb Smirnoff 					match = m_tag_locate(m, MTAG_IPFW,
28403b3a8eb9SGleb Smirnoff 					    tag, NULL) != NULL;
28413b3a8eb9SGleb Smirnoff 					break;
28423b3a8eb9SGleb Smirnoff 				}
28433b3a8eb9SGleb Smirnoff 
28443b3a8eb9SGleb Smirnoff 				/* we have ranges */
28453b3a8eb9SGleb Smirnoff 				for (mtag = m_tag_first(m);
28463b3a8eb9SGleb Smirnoff 				    mtag != NULL && !match;
28473b3a8eb9SGleb Smirnoff 				    mtag = m_tag_next(m, mtag)) {
28483b3a8eb9SGleb Smirnoff 					uint16_t *p;
28493b3a8eb9SGleb Smirnoff 					int i;
28503b3a8eb9SGleb Smirnoff 
28513b3a8eb9SGleb Smirnoff 					if (mtag->m_tag_cookie != MTAG_IPFW)
28523b3a8eb9SGleb Smirnoff 						continue;
28533b3a8eb9SGleb Smirnoff 
28543b3a8eb9SGleb Smirnoff 					p = ((ipfw_insn_u16 *)cmd)->ports;
28553b3a8eb9SGleb Smirnoff 					i = cmdlen - 1;
28563b3a8eb9SGleb Smirnoff 					for(; !match && i > 0; i--, p += 2)
28573b3a8eb9SGleb Smirnoff 						match =
28583b3a8eb9SGleb Smirnoff 						    mtag->m_tag_id >= p[0] &&
28593b3a8eb9SGleb Smirnoff 						    mtag->m_tag_id <= p[1];
28603b3a8eb9SGleb Smirnoff 				}
28613b3a8eb9SGleb Smirnoff 				break;
28623b3a8eb9SGleb Smirnoff 			}
28633b3a8eb9SGleb Smirnoff 
2864fc727ad6SBoris Lytochkin 			case O_MARK: {
2865fc727ad6SBoris Lytochkin 				uint32_t mark;
2866fc727ad6SBoris Lytochkin 				if (cmd->arg1 == IP_FW_TARG)
2867fc727ad6SBoris Lytochkin 					mark = TARG_VAL(chain, tablearg, mark);
2868fc727ad6SBoris Lytochkin 				else
28694a77657cSAndrey V. Elsukov 					mark = insntoc(cmd, u32)->d[0];
2870fc727ad6SBoris Lytochkin 				match =
2871fc727ad6SBoris Lytochkin 				    (args->rule.pkt_mark &
28724a77657cSAndrey V. Elsukov 				    insntoc(cmd, u32)->d[1]) ==
28734a77657cSAndrey V. Elsukov 				    (mark & insntoc(cmd, u32)->d[1]);
2874fc727ad6SBoris Lytochkin 				break;
2875fc727ad6SBoris Lytochkin 			}
2876fc727ad6SBoris Lytochkin 
28773b3a8eb9SGleb Smirnoff 			/*
28783b3a8eb9SGleb Smirnoff 			 * The second set of opcodes represents 'actions',
28793b3a8eb9SGleb Smirnoff 			 * i.e. the terminal part of a rule once the packet
28803b3a8eb9SGleb Smirnoff 			 * matches all previous patterns.
28813b3a8eb9SGleb Smirnoff 			 * Typically there is only one action for each rule,
28823b3a8eb9SGleb Smirnoff 			 * and the opcode is stored at the end of the rule
28833b3a8eb9SGleb Smirnoff 			 * (but there are exceptions -- see below).
28843b3a8eb9SGleb Smirnoff 			 *
28853b3a8eb9SGleb Smirnoff 			 * In general, here we set retval and terminate the
28863b3a8eb9SGleb Smirnoff 			 * outer loop (would be a 'break 3' in some language,
28873b3a8eb9SGleb Smirnoff 			 * but we need to set l=0, done=1)
28883b3a8eb9SGleb Smirnoff 			 *
28893b3a8eb9SGleb Smirnoff 			 * Exceptions:
28903b3a8eb9SGleb Smirnoff 			 * O_COUNT and O_SKIPTO actions:
28913b3a8eb9SGleb Smirnoff 			 *   instead of terminating, we jump to the next rule
28923b3a8eb9SGleb Smirnoff 			 *   (setting l=0), or to the SKIPTO target (setting
28933b3a8eb9SGleb Smirnoff 			 *   f/f_len, cmd and l as needed), respectively.
28943b3a8eb9SGleb Smirnoff 			 *
28953b3a8eb9SGleb Smirnoff 			 * O_TAG, O_LOG and O_ALTQ action parameters:
28963b3a8eb9SGleb Smirnoff 			 *   perform some action and set match = 1;
28973b3a8eb9SGleb Smirnoff 			 *
28983b3a8eb9SGleb Smirnoff 			 * O_LIMIT and O_KEEP_STATE: these opcodes are
28993b3a8eb9SGleb Smirnoff 			 *   not real 'actions', and are stored right
2900f7c4fdeeSAndrey V. Elsukov 			 *   before the 'action' part of the rule (one
2901f7c4fdeeSAndrey V. Elsukov 			 *   exception is O_SKIP_ACTION which could be
2902f7c4fdeeSAndrey V. Elsukov 			 *   between these opcodes and 'action' one).
29033b3a8eb9SGleb Smirnoff 			 *   These opcodes try to install an entry in the
29043b3a8eb9SGleb Smirnoff 			 *   state tables; if successful, we continue with
29053b3a8eb9SGleb Smirnoff 			 *   the next opcode (match=1; break;), otherwise
29063b3a8eb9SGleb Smirnoff 			 *   the packet must be dropped (set retval,
29073b3a8eb9SGleb Smirnoff 			 *   break loops with l=0, done=1)
29083b3a8eb9SGleb Smirnoff 			 *
29093b3a8eb9SGleb Smirnoff 			 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
29103b3a8eb9SGleb Smirnoff 			 *   cause a lookup of the state table, and a jump
29113b3a8eb9SGleb Smirnoff 			 *   to the 'action' part of the parent rule
29123b3a8eb9SGleb Smirnoff 			 *   if an entry is found, or
29133b3a8eb9SGleb Smirnoff 			 *   (CHECK_STATE only) a jump to the next rule if
29143b3a8eb9SGleb Smirnoff 			 *   the entry is not found.
29153b3a8eb9SGleb Smirnoff 			 *   The result of the lookup is cached so that
29163b3a8eb9SGleb Smirnoff 			 *   further instances of these opcodes become NOPs.
29173b3a8eb9SGleb Smirnoff 			 *   The jump to the next rule is done by setting
29183b3a8eb9SGleb Smirnoff 			 *   l=0, cmdlen=0.
2919f7c4fdeeSAndrey V. Elsukov 			 *
2920f7c4fdeeSAndrey V. Elsukov 			 * O_SKIP_ACTION: this opcode is not a real 'action'
2921f7c4fdeeSAndrey V. Elsukov 			 *  either, and is stored right before the 'action'
2922f7c4fdeeSAndrey V. Elsukov 			 *  part of the rule, right after the O_KEEP_STATE
2923f7c4fdeeSAndrey V. Elsukov 			 *  opcode. It causes match failure so the real
2924f7c4fdeeSAndrey V. Elsukov 			 *  'action' could be executed only if the rule
2925f7c4fdeeSAndrey V. Elsukov 			 *  is checked via dynamic rule from the state
2926f7c4fdeeSAndrey V. Elsukov 			 *  table, as in such case execution starts
2927f7c4fdeeSAndrey V. Elsukov 			 *  from the true 'action' opcode directly.
2928f7c4fdeeSAndrey V. Elsukov 			 *
29293b3a8eb9SGleb Smirnoff 			 */
29303b3a8eb9SGleb Smirnoff 			case O_LIMIT:
29313b3a8eb9SGleb Smirnoff 			case O_KEEP_STATE:
29321719df1bSAndrey V. Elsukov 				if (ipfw_dyn_install_state(chain, f,
2933b99a6823SAndrey V. Elsukov 				    (ipfw_insn_limit *)cmd, args, ulp,
2934b99a6823SAndrey V. Elsukov 				    pktlen, &dyn_info, tablearg)) {
29353b3a8eb9SGleb Smirnoff 					/* error or limit violation */
29363b3a8eb9SGleb Smirnoff 					retval = IP_FW_DENY;
29373b3a8eb9SGleb Smirnoff 					l = 0;	/* exit inner loop */
29383b3a8eb9SGleb Smirnoff 					done = 1; /* exit outer loop */
29393b3a8eb9SGleb Smirnoff 				}
29403b3a8eb9SGleb Smirnoff 				match = 1;
29413b3a8eb9SGleb Smirnoff 				break;
29423b3a8eb9SGleb Smirnoff 
29433b3a8eb9SGleb Smirnoff 			case O_PROBE_STATE:
29443b3a8eb9SGleb Smirnoff 			case O_CHECK_STATE:
29453b3a8eb9SGleb Smirnoff 				/*
29463b3a8eb9SGleb Smirnoff 				 * dynamic rules are checked at the first
29473b3a8eb9SGleb Smirnoff 				 * keep-state or check-state occurrence,
2948b99a6823SAndrey V. Elsukov 				 * with the result being stored in dyn_info.
29493b3a8eb9SGleb Smirnoff 				 * The compiler introduces a PROBE_STATE
29503b3a8eb9SGleb Smirnoff 				 * instruction for us when we have a
29513b3a8eb9SGleb Smirnoff 				 * KEEP_STATE (because PROBE_STATE needs
29523b3a8eb9SGleb Smirnoff 				 * to be run first).
29533b3a8eb9SGleb Smirnoff 				 */
2954b99a6823SAndrey V. Elsukov 				if (DYN_LOOKUP_NEEDED(&dyn_info, cmd) &&
2955b99a6823SAndrey V. Elsukov 				    (q = ipfw_dyn_lookup_state(args, ulp,
2956b99a6823SAndrey V. Elsukov 				    pktlen, cmd, &dyn_info)) != NULL) {
29573b3a8eb9SGleb Smirnoff 					/*
29581719df1bSAndrey V. Elsukov 					 * Found dynamic entry, jump to the
29591719df1bSAndrey V. Elsukov 					 * 'action' part of the parent rule
29601719df1bSAndrey V. Elsukov 					 * by setting f, cmd, l and clearing
29611719df1bSAndrey V. Elsukov 					 * cmdlen.
29623b3a8eb9SGleb Smirnoff 					 */
29631719df1bSAndrey V. Elsukov 					f = q;
2964b99a6823SAndrey V. Elsukov 					f_pos = dyn_info.f_pos;
29653b3a8eb9SGleb Smirnoff 					cmd = ACTION_PTR(f);
29663b3a8eb9SGleb Smirnoff 					l = f->cmd_len - f->act_ofs;
29673b3a8eb9SGleb Smirnoff 					cmdlen = 0;
296862b1faa3SKarim Fodil-Lemelin 					continue;
29693b3a8eb9SGleb Smirnoff 				}
29703b3a8eb9SGleb Smirnoff 				/*
29713b3a8eb9SGleb Smirnoff 				 * Dynamic entry not found. If CHECK_STATE,
29723b3a8eb9SGleb Smirnoff 				 * skip to next rule, if PROBE_STATE just
29733b3a8eb9SGleb Smirnoff 				 * ignore and continue with next opcode.
29743b3a8eb9SGleb Smirnoff 				 */
29753b3a8eb9SGleb Smirnoff 				if (cmd->opcode == O_CHECK_STATE)
29763b3a8eb9SGleb Smirnoff 					l = 0;	/* exit inner loop */
29773b3a8eb9SGleb Smirnoff 				match = 1;
29783b3a8eb9SGleb Smirnoff 				break;
29793b3a8eb9SGleb Smirnoff 
2980f7c4fdeeSAndrey V. Elsukov 			case O_SKIP_ACTION:
2981f7c4fdeeSAndrey V. Elsukov 				match = 0;	/* skip to the next rule */
2982f7c4fdeeSAndrey V. Elsukov 				l = 0;		/* exit inner loop */
2983f7c4fdeeSAndrey V. Elsukov 				break;
2984f7c4fdeeSAndrey V. Elsukov 
29853b3a8eb9SGleb Smirnoff 			case O_ACCEPT:
29863b3a8eb9SGleb Smirnoff 				retval = 0;	/* accept */
29873b3a8eb9SGleb Smirnoff 				l = 0;		/* exit inner loop */
29883b3a8eb9SGleb Smirnoff 				done = 1;	/* exit outer loop */
29893b3a8eb9SGleb Smirnoff 				break;
29903b3a8eb9SGleb Smirnoff 
29913b3a8eb9SGleb Smirnoff 			case O_PIPE:
29923b3a8eb9SGleb Smirnoff 			case O_QUEUE:
29933b3a8eb9SGleb Smirnoff 				set_match(args, f_pos, chain);
29940cba2b28SAlexander V. Chernikov 				args->rule.info = TARG(cmd->arg1, pipe);
29953b3a8eb9SGleb Smirnoff 				if (cmd->opcode == O_PIPE)
29963b3a8eb9SGleb Smirnoff 					args->rule.info |= IPFW_IS_PIPE;
29973b3a8eb9SGleb Smirnoff 				if (V_fw_one_pass)
29983b3a8eb9SGleb Smirnoff 					args->rule.info |= IPFW_ONEPASS;
29993b3a8eb9SGleb Smirnoff 				retval = IP_FW_DUMMYNET;
30003b3a8eb9SGleb Smirnoff 				l = 0;          /* exit inner loop */
30013b3a8eb9SGleb Smirnoff 				done = 1;       /* exit outer loop */
30023b3a8eb9SGleb Smirnoff 				break;
30033b3a8eb9SGleb Smirnoff 
30043b3a8eb9SGleb Smirnoff 			case O_DIVERT:
30053b3a8eb9SGleb Smirnoff 			case O_TEE:
30061cdf23bcSAndrey V. Elsukov 				if (args->flags & IPFW_ARGS_ETHER)
30071cdf23bcSAndrey V. Elsukov 					break;	/* not on layer 2 */
30083b3a8eb9SGleb Smirnoff 				/* otherwise this is terminal */
30093b3a8eb9SGleb Smirnoff 				l = 0;		/* exit inner loop */
30103b3a8eb9SGleb Smirnoff 				done = 1;	/* exit outer loop */
30113b3a8eb9SGleb Smirnoff 				retval = (cmd->opcode == O_DIVERT) ?
30123b3a8eb9SGleb Smirnoff 					IP_FW_DIVERT : IP_FW_TEE;
30133b3a8eb9SGleb Smirnoff 				set_match(args, f_pos, chain);
30140cba2b28SAlexander V. Chernikov 				args->rule.info = TARG(cmd->arg1, divert);
30153b3a8eb9SGleb Smirnoff 				break;
30163b3a8eb9SGleb Smirnoff 
30173b3a8eb9SGleb Smirnoff 			case O_COUNT:
3018c187c1fbSAlexander V. Chernikov 				IPFW_INC_RULE_COUNTER(f, pktlen);
30193b3a8eb9SGleb Smirnoff 				l = 0;		/* exit inner loop */
30203b3a8eb9SGleb Smirnoff 				break;
30213b3a8eb9SGleb Smirnoff 
30223b3a8eb9SGleb Smirnoff 			case O_SKIPTO:
3023c187c1fbSAlexander V. Chernikov 			    IPFW_INC_RULE_COUNTER(f, pktlen);
30244a77657cSAndrey V. Elsukov 			    f_pos = jump(chain, f,
30254a77657cSAndrey V. Elsukov 				insntod(cmd, u32)->d[0], tablearg, false);
30263b3a8eb9SGleb Smirnoff 			    /*
30273b3a8eb9SGleb Smirnoff 			     * Skip disabled rules, and re-enter
30283b3a8eb9SGleb Smirnoff 			     * the inner loop with the correct
30293b3a8eb9SGleb Smirnoff 			     * f_pos, f, l and cmd.
30303b3a8eb9SGleb Smirnoff 			     * Also clear cmdlen and skip_or
30313b3a8eb9SGleb Smirnoff 			     */
30323b3a8eb9SGleb Smirnoff 			    for (; f_pos < chain->n_rules - 1 &&
30333b3a8eb9SGleb Smirnoff 				    (V_set_disable &
30343b3a8eb9SGleb Smirnoff 				     (1 << chain->map[f_pos]->set));
30353b3a8eb9SGleb Smirnoff 				    f_pos++)
30363b3a8eb9SGleb Smirnoff 				;
30373b3a8eb9SGleb Smirnoff 			    /* Re-enter the inner loop at the skipto rule. */
30383b3a8eb9SGleb Smirnoff 			    f = chain->map[f_pos];
30393b3a8eb9SGleb Smirnoff 			    l = f->cmd_len;
30403b3a8eb9SGleb Smirnoff 			    cmd = f->cmd;
30413b3a8eb9SGleb Smirnoff 			    match = 1;
30423b3a8eb9SGleb Smirnoff 			    cmdlen = 0;
30433b3a8eb9SGleb Smirnoff 			    skip_or = 0;
30443b3a8eb9SGleb Smirnoff 			    continue;
30453b3a8eb9SGleb Smirnoff 			    break;	/* not reached */
30463b3a8eb9SGleb Smirnoff 
30473b3a8eb9SGleb Smirnoff 			case O_CALLRETURN: {
30483b3a8eb9SGleb Smirnoff 				/*
30493b3a8eb9SGleb Smirnoff 				 * Implementation of `subroutine' call/return,
30503b3a8eb9SGleb Smirnoff 				 * in the stack carried in an mbuf tag. This
30513b3a8eb9SGleb Smirnoff 				 * is different from `skipto' in that any call
30523b3a8eb9SGleb Smirnoff 				 * address is possible (`skipto' must prevent
30533b3a8eb9SGleb Smirnoff 				 * backward jumps to avoid endless loops).
30543b3a8eb9SGleb Smirnoff 				 * We have `return' action when F_NOT flag is
30553b3a8eb9SGleb Smirnoff 				 * present. The `m_tag_id' field is used as
30563b3a8eb9SGleb Smirnoff 				 * stack pointer.
30573b3a8eb9SGleb Smirnoff 				 */
30583b3a8eb9SGleb Smirnoff 				struct m_tag *mtag;
30594a77657cSAndrey V. Elsukov 				uint32_t jmpto, *stack;
30603b3a8eb9SGleb Smirnoff 
30613b3a8eb9SGleb Smirnoff #define	IS_CALL		((cmd->len & F_NOT) == 0)
30623b3a8eb9SGleb Smirnoff #define	IS_RETURN	((cmd->len & F_NOT) != 0)
30633b3a8eb9SGleb Smirnoff 				/*
30643b3a8eb9SGleb Smirnoff 				 * Hand-rolled version of m_tag_locate() with
30653b3a8eb9SGleb Smirnoff 				 * wildcard `type'.
30663b3a8eb9SGleb Smirnoff 				 * If not already tagged, allocate new tag.
30673b3a8eb9SGleb Smirnoff 				 */
30683b3a8eb9SGleb Smirnoff 				mtag = m_tag_first(m);
30693b3a8eb9SGleb Smirnoff 				while (mtag != NULL) {
30703b3a8eb9SGleb Smirnoff 					if (mtag->m_tag_cookie ==
30713b3a8eb9SGleb Smirnoff 					    MTAG_IPFW_CALL)
30723b3a8eb9SGleb Smirnoff 						break;
30733b3a8eb9SGleb Smirnoff 					mtag = m_tag_next(m, mtag);
30743b3a8eb9SGleb Smirnoff 				}
30754a77657cSAndrey V. Elsukov 
30764a77657cSAndrey V. Elsukov 				/*
30774a77657cSAndrey V. Elsukov 				 * We keep ruleset id in the first element
30784a77657cSAndrey V. Elsukov 				 * of stack. If it doesn't match chain->id,
30794a77657cSAndrey V. Elsukov 				 * then we can't trust information in the
30804a77657cSAndrey V. Elsukov 				 * stack, since rules were changed.
30814a77657cSAndrey V. Elsukov 				 * We reset stack pointer to be able reuse
30824a77657cSAndrey V. Elsukov 				 * tag if it will be needed.
30834a77657cSAndrey V. Elsukov 				 */
30844a77657cSAndrey V. Elsukov 				if (mtag != NULL) {
30854a77657cSAndrey V. Elsukov 					stack = (uint32_t *)(mtag + 1);
30864a77657cSAndrey V. Elsukov 					if (stack[0] != chain->id) {
30874a77657cSAndrey V. Elsukov 						stack[0] = chain->id;
30884a77657cSAndrey V. Elsukov 						mtag->m_tag_id = 0;
30894a77657cSAndrey V. Elsukov 					}
30903b3a8eb9SGleb Smirnoff 				}
30913b3a8eb9SGleb Smirnoff 
30923b3a8eb9SGleb Smirnoff 				/*
30934a77657cSAndrey V. Elsukov 				 * If there is no mtag or stack is empty,
30944a77657cSAndrey V. Elsukov 				 * `return` continues with next rule.
30953b3a8eb9SGleb Smirnoff 				 */
30963b3a8eb9SGleb Smirnoff 				if (IS_RETURN && (mtag == NULL ||
30973b3a8eb9SGleb Smirnoff 				    mtag->m_tag_id == 0)) {
30983b3a8eb9SGleb Smirnoff 					l = 0;		/* exit inner loop */
30993b3a8eb9SGleb Smirnoff 					break;
31003b3a8eb9SGleb Smirnoff 				}
31014a77657cSAndrey V. Elsukov 
31024a77657cSAndrey V. Elsukov 				if (mtag == NULL) {
31034a77657cSAndrey V. Elsukov 					MPASS(IS_CALL);
31044a77657cSAndrey V. Elsukov 					mtag = m_tag_alloc(MTAG_IPFW_CALL, 0,
31054a77657cSAndrey V. Elsukov 					    IPFW_CALLSTACK_SIZE *
31064a77657cSAndrey V. Elsukov 					    sizeof(uint32_t), M_NOWAIT);
31074a77657cSAndrey V. Elsukov 					if (mtag != NULL) {
31084a77657cSAndrey V. Elsukov 						m_tag_prepend(m, mtag);
31094a77657cSAndrey V. Elsukov 						stack = (uint32_t *)(mtag + 1);
31104a77657cSAndrey V. Elsukov 						stack[0] = chain->id;
31114a77657cSAndrey V. Elsukov 					}
31124a77657cSAndrey V. Elsukov 				}
31134a77657cSAndrey V. Elsukov 
31144a77657cSAndrey V. Elsukov 				if (mtag == NULL) {
31154a77657cSAndrey V. Elsukov 					printf("ipfw: rule %u: failed to "
31164a77657cSAndrey V. Elsukov 					    "allocate call stack. "
31174a77657cSAndrey V. Elsukov 					    "Denying packet.\n",
31184a77657cSAndrey V. Elsukov 					    f->rulenum);
31193b3a8eb9SGleb Smirnoff 					l = 0;		/* exit inner loop */
31204a77657cSAndrey V. Elsukov 					done = 1;	/* exit outer loop */
31214a77657cSAndrey V. Elsukov 					retval = IP_FW_DENY; /* drop packet */
31223b3a8eb9SGleb Smirnoff 					break;
31233b3a8eb9SGleb Smirnoff 				}
31243b3a8eb9SGleb Smirnoff 
31254a77657cSAndrey V. Elsukov 				if (IS_CALL && mtag->m_tag_id >=
31264a77657cSAndrey V. Elsukov 				    IPFW_CALLSTACK_SIZE - 1) {
31274a77657cSAndrey V. Elsukov 					printf("ipfw: rule %u: call stack "
31284a77657cSAndrey V. Elsukov 					    "overflow. Denying packet.\n",
31294a77657cSAndrey V. Elsukov 					    f->rulenum);
31304a77657cSAndrey V. Elsukov 					l = 0;		/* exit inner loop */
31314a77657cSAndrey V. Elsukov 					done = 1;	/* exit outer loop */
31324a77657cSAndrey V. Elsukov 					retval = IP_FW_DENY; /* drop packet */
31334a77657cSAndrey V. Elsukov 					break;
31344a77657cSAndrey V. Elsukov 				}
31353b3a8eb9SGleb Smirnoff 
31364a77657cSAndrey V. Elsukov 				MPASS(stack == (uint32_t *)(mtag + 1));
31374a77657cSAndrey V. Elsukov 				IPFW_INC_RULE_COUNTER(f, pktlen);
31384a77657cSAndrey V. Elsukov 
31393b3a8eb9SGleb Smirnoff 				if (IS_CALL) {
31404a77657cSAndrey V. Elsukov 					stack[++mtag->m_tag_id] = f_pos;
31414a77657cSAndrey V. Elsukov 					f_pos = jump(chain, f,
31424a77657cSAndrey V. Elsukov 					    insntod(cmd, u32)->d[0],
31434a77657cSAndrey V. Elsukov 					    tablearg, true);
31443b3a8eb9SGleb Smirnoff 				} else {	/* `return' action */
31454a77657cSAndrey V. Elsukov 					jmpto = stack[mtag->m_tag_id--];
31464a77657cSAndrey V. Elsukov 					if (cmd->arg1 == RETURN_NEXT_RULE)
31474a77657cSAndrey V. Elsukov 						f_pos = jmpto + 1;
31484a77657cSAndrey V. Elsukov 					else /* RETURN_NEXT_RULENUM */
31494a77657cSAndrey V. Elsukov 						f_pos = ipfw_find_rule(chain,
31504a77657cSAndrey V. Elsukov 						    chain->map[
31514a77657cSAndrey V. Elsukov 						    jmpto]->rulenum + 1, 0);
31523b3a8eb9SGleb Smirnoff 				}
31533b3a8eb9SGleb Smirnoff 
31543b3a8eb9SGleb Smirnoff 				/*
31553b3a8eb9SGleb Smirnoff 				 * Skip disabled rules, and re-enter
31563b3a8eb9SGleb Smirnoff 				 * the inner loop with the correct
31573b3a8eb9SGleb Smirnoff 				 * f_pos, f, l and cmd.
31583b3a8eb9SGleb Smirnoff 				 * Also clear cmdlen and skip_or
31593b3a8eb9SGleb Smirnoff 				 */
31604a77657cSAndrey V. Elsukov 				MPASS(f_pos < chain->n_rules - 1);
31613b3a8eb9SGleb Smirnoff 				for (; f_pos < chain->n_rules - 1 &&
31623b3a8eb9SGleb Smirnoff 				    (V_set_disable &
31633b3a8eb9SGleb Smirnoff 				    (1 << chain->map[f_pos]->set)); f_pos++)
31643b3a8eb9SGleb Smirnoff 					;
31654a77657cSAndrey V. Elsukov 				/*
31664a77657cSAndrey V. Elsukov 				 * Re-enter the inner loop at the dest
31674a77657cSAndrey V. Elsukov 				 * rule.
31684a77657cSAndrey V. Elsukov 				 */
31693b3a8eb9SGleb Smirnoff 				f = chain->map[f_pos];
31703b3a8eb9SGleb Smirnoff 				l = f->cmd_len;
31713b3a8eb9SGleb Smirnoff 				cmd = f->cmd;
31723b3a8eb9SGleb Smirnoff 				cmdlen = 0;
31733b3a8eb9SGleb Smirnoff 				skip_or = 0;
31743b3a8eb9SGleb Smirnoff 				continue;
31753b3a8eb9SGleb Smirnoff 				break;	/* NOTREACHED */
31763b3a8eb9SGleb Smirnoff 			}
31773b3a8eb9SGleb Smirnoff #undef IS_CALL
31783b3a8eb9SGleb Smirnoff #undef IS_RETURN
31793b3a8eb9SGleb Smirnoff 
31803b3a8eb9SGleb Smirnoff 			case O_REJECT:
31813b3a8eb9SGleb Smirnoff 				/*
31823b3a8eb9SGleb Smirnoff 				 * Drop the packet and send a reject notice
31833b3a8eb9SGleb Smirnoff 				 * if the packet is not ICMP (or is an ICMP
31843b3a8eb9SGleb Smirnoff 				 * query), and it is not multicast/broadcast.
31853b3a8eb9SGleb Smirnoff 				 */
31863b3a8eb9SGleb Smirnoff 				if (hlen > 0 && is_ipv4 && offset == 0 &&
31873b3a8eb9SGleb Smirnoff 				    (proto != IPPROTO_ICMP ||
31883b3a8eb9SGleb Smirnoff 				     is_icmp_query(ICMP(ulp))) &&
31893b3a8eb9SGleb Smirnoff 				    !(m->m_flags & (M_BCAST|M_MCAST)) &&
31903b3a8eb9SGleb Smirnoff 				    !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
31918e780285SRichard Scheffenegger 					KASSERT(!need_send_reject,
31928e780285SRichard Scheffenegger 					    ("o_reject - need_send_reject was set previously"));
31938e780285SRichard Scheffenegger 					if ((reject_code = cmd->arg1) == ICMP_UNREACH_NEEDFRAG &&
31948e780285SRichard Scheffenegger 					    cmd->len == F_INSN_SIZE(ipfw_insn_u16)) {
31958e780285SRichard Scheffenegger 						reject_mtu =
31968e780285SRichard Scheffenegger 						    ((ipfw_insn_u16 *)cmd)->ports[0];
31978e780285SRichard Scheffenegger 					} else {
31988e780285SRichard Scheffenegger 						reject_mtu = 0;
31998e780285SRichard Scheffenegger 					}
32008e780285SRichard Scheffenegger 					need_send_reject = true;
32013b3a8eb9SGleb Smirnoff 				}
32023b3a8eb9SGleb Smirnoff 				/* FALLTHROUGH */
32033b3a8eb9SGleb Smirnoff #ifdef INET6
32043b3a8eb9SGleb Smirnoff 			case O_UNREACH6:
32053b3a8eb9SGleb Smirnoff 				if (hlen > 0 && is_ipv6 &&
32063b3a8eb9SGleb Smirnoff 				    ((offset & IP6F_OFF_MASK) == 0) &&
32073b3a8eb9SGleb Smirnoff 				    (proto != IPPROTO_ICMPV6 ||
32083b3a8eb9SGleb Smirnoff 				     (is_icmp6_query(icmp6_type) == 1)) &&
32093b3a8eb9SGleb Smirnoff 				    !(m->m_flags & (M_BCAST|M_MCAST)) &&
321014a6bab1SAndrey V. Elsukov 				    !IN6_IS_ADDR_MULTICAST(
321114a6bab1SAndrey V. Elsukov 					&args->f_id.dst_ip6)) {
32128e780285SRichard Scheffenegger 					KASSERT(!need_send_reject,
32138e780285SRichard Scheffenegger 					    ("o_unreach6 - need_send_reject was set previously"));
32148e780285SRichard Scheffenegger 					reject_code = cmd->arg1;
32158e780285SRichard Scheffenegger 					if (cmd->opcode == O_REJECT) {
32168e780285SRichard Scheffenegger 						reject_code =
32178e780285SRichard Scheffenegger 						    map_icmp_unreach(reject_code);
32188e780285SRichard Scheffenegger 					}
32198e780285SRichard Scheffenegger 					need_send_reject = true;
32203b3a8eb9SGleb Smirnoff 				}
32213b3a8eb9SGleb Smirnoff 				/* FALLTHROUGH */
32223b3a8eb9SGleb Smirnoff #endif
32233b3a8eb9SGleb Smirnoff 			case O_DENY:
32243b3a8eb9SGleb Smirnoff 				retval = IP_FW_DENY;
32253b3a8eb9SGleb Smirnoff 				l = 0;		/* exit inner loop */
32263b3a8eb9SGleb Smirnoff 				done = 1;	/* exit outer loop */
32273b3a8eb9SGleb Smirnoff 				break;
32283b3a8eb9SGleb Smirnoff 
32293b3a8eb9SGleb Smirnoff 			case O_FORWARD_IP:
32301cdf23bcSAndrey V. Elsukov 				if (args->flags & IPFW_ARGS_ETHER)
32311cdf23bcSAndrey V. Elsukov 					break;	/* not valid on layer2 pkts */
3232b99a6823SAndrey V. Elsukov 				if (q != f ||
3233b99a6823SAndrey V. Elsukov 				    dyn_info.direction == MATCH_FORWARD) {
32343b3a8eb9SGleb Smirnoff 				    struct sockaddr_in *sa;
32352530ed9eSAndrey V. Elsukov 
32363b3a8eb9SGleb Smirnoff 				    sa = &(((ipfw_insn_sa *)cmd)->sa);
32373b3a8eb9SGleb Smirnoff 				    if (sa->sin_addr.s_addr == INADDR_ANY) {
32382530ed9eSAndrey V. Elsukov #ifdef INET6
32392530ed9eSAndrey V. Elsukov 					/*
32402530ed9eSAndrey V. Elsukov 					 * We use O_FORWARD_IP opcode for
32412530ed9eSAndrey V. Elsukov 					 * fwd rule with tablearg, but tables
32422530ed9eSAndrey V. Elsukov 					 * now support IPv6 addresses. And
32432530ed9eSAndrey V. Elsukov 					 * when we are inspecting IPv6 packet,
32442530ed9eSAndrey V. Elsukov 					 * we can use nh6 field from
32452530ed9eSAndrey V. Elsukov 					 * table_value as next_hop6 address.
32462530ed9eSAndrey V. Elsukov 					 */
32472530ed9eSAndrey V. Elsukov 					if (is_ipv6) {
32481cdf23bcSAndrey V. Elsukov 						struct ip_fw_nh6 *nh6;
32492530ed9eSAndrey V. Elsukov 
32501cdf23bcSAndrey V. Elsukov 						args->flags |= IPFW_ARGS_NH6;
32511cdf23bcSAndrey V. Elsukov 						nh6 = &args->hopstore6;
32521cdf23bcSAndrey V. Elsukov 						nh6->sin6_addr = TARG_VAL(
32532530ed9eSAndrey V. Elsukov 						    chain, tablearg, nh6);
32541cdf23bcSAndrey V. Elsukov 						nh6->sin6_port = sa->sin_port;
32551cdf23bcSAndrey V. Elsukov 						nh6->sin6_scope_id = TARG_VAL(
32561cdf23bcSAndrey V. Elsukov 						    chain, tablearg, zoneid);
32572530ed9eSAndrey V. Elsukov 					} else
32582530ed9eSAndrey V. Elsukov #endif
32592530ed9eSAndrey V. Elsukov 					{
32601cdf23bcSAndrey V. Elsukov 						args->flags |= IPFW_ARGS_NH4;
3261ff0a1379SAndrey V. Elsukov 						args->hopstore.sin_port =
3262ff0a1379SAndrey V. Elsukov 						    sa->sin_port;
32631cdf23bcSAndrey V. Elsukov 						sa = &args->hopstore;
32642530ed9eSAndrey V. Elsukov 						sa->sin_family = AF_INET;
32652530ed9eSAndrey V. Elsukov 						sa->sin_len = sizeof(*sa);
32662530ed9eSAndrey V. Elsukov 						sa->sin_addr.s_addr = htonl(
32672530ed9eSAndrey V. Elsukov 						    TARG_VAL(chain, tablearg,
32682530ed9eSAndrey V. Elsukov 						    nh4));
32692530ed9eSAndrey V. Elsukov 					}
32703b3a8eb9SGleb Smirnoff 				    } else {
32711cdf23bcSAndrey V. Elsukov 					    args->flags |= IPFW_ARGS_NH4PTR;
32723b3a8eb9SGleb Smirnoff 					    args->next_hop = sa;
32733b3a8eb9SGleb Smirnoff 				    }
32743b3a8eb9SGleb Smirnoff 				}
32753b3a8eb9SGleb Smirnoff 				retval = IP_FW_PASS;
32763b3a8eb9SGleb Smirnoff 				l = 0;          /* exit inner loop */
32773b3a8eb9SGleb Smirnoff 				done = 1;       /* exit outer loop */
32783b3a8eb9SGleb Smirnoff 				break;
32793b3a8eb9SGleb Smirnoff 
32803b3a8eb9SGleb Smirnoff #ifdef INET6
32813b3a8eb9SGleb Smirnoff 			case O_FORWARD_IP6:
32821cdf23bcSAndrey V. Elsukov 				if (args->flags & IPFW_ARGS_ETHER)
32831cdf23bcSAndrey V. Elsukov 					break;	/* not valid on layer2 pkts */
3284b99a6823SAndrey V. Elsukov 				if (q != f ||
3285b99a6823SAndrey V. Elsukov 				    dyn_info.direction == MATCH_FORWARD) {
32863b3a8eb9SGleb Smirnoff 					struct sockaddr_in6 *sin6;
32873b3a8eb9SGleb Smirnoff 
32883b3a8eb9SGleb Smirnoff 					sin6 = &(((ipfw_insn_sa6 *)cmd)->sa);
32891cdf23bcSAndrey V. Elsukov 					args->flags |= IPFW_ARGS_NH6PTR;
32903b3a8eb9SGleb Smirnoff 					args->next_hop6 = sin6;
32913b3a8eb9SGleb Smirnoff 				}
32923b3a8eb9SGleb Smirnoff 				retval = IP_FW_PASS;
32933b3a8eb9SGleb Smirnoff 				l = 0;		/* exit inner loop */
32943b3a8eb9SGleb Smirnoff 				done = 1;	/* exit outer loop */
32953b3a8eb9SGleb Smirnoff 				break;
32963b3a8eb9SGleb Smirnoff #endif
32973b3a8eb9SGleb Smirnoff 
32983b3a8eb9SGleb Smirnoff 			case O_NETGRAPH:
32993b3a8eb9SGleb Smirnoff 			case O_NGTEE:
33003b3a8eb9SGleb Smirnoff 				set_match(args, f_pos, chain);
33010cba2b28SAlexander V. Chernikov 				args->rule.info = TARG(cmd->arg1, netgraph);
33023b3a8eb9SGleb Smirnoff 				if (V_fw_one_pass)
33033b3a8eb9SGleb Smirnoff 					args->rule.info |= IPFW_ONEPASS;
33043b3a8eb9SGleb Smirnoff 				retval = (cmd->opcode == O_NETGRAPH) ?
33053b3a8eb9SGleb Smirnoff 				    IP_FW_NETGRAPH : IP_FW_NGTEE;
33063b3a8eb9SGleb Smirnoff 				l = 0;          /* exit inner loop */
33073b3a8eb9SGleb Smirnoff 				done = 1;       /* exit outer loop */
33083b3a8eb9SGleb Smirnoff 				break;
33093b3a8eb9SGleb Smirnoff 
33103b3a8eb9SGleb Smirnoff 			case O_SETFIB: {
33113b3a8eb9SGleb Smirnoff 				uint32_t fib;
33123b3a8eb9SGleb Smirnoff 
3313c187c1fbSAlexander V. Chernikov 				IPFW_INC_RULE_COUNTER(f, pktlen);
3314b554a278SAlexander V. Chernikov 				fib = TARG(cmd->arg1, fib) & 0x7FFF;
33153b3a8eb9SGleb Smirnoff 				if (fib >= rt_numfibs)
33163b3a8eb9SGleb Smirnoff 					fib = 0;
33173b3a8eb9SGleb Smirnoff 				M_SETFIB(m, fib);
33181cdf23bcSAndrey V. Elsukov 				args->f_id.fib = fib; /* XXX */
33193b3a8eb9SGleb Smirnoff 				l = 0;		/* exit inner loop */
33203b3a8eb9SGleb Smirnoff 				break;
33213b3a8eb9SGleb Smirnoff 		        }
33223b3a8eb9SGleb Smirnoff 
3323ae01d73cSAlexander V. Chernikov 			case O_SETDSCP: {
3324ae01d73cSAlexander V. Chernikov 				uint16_t code;
3325ae01d73cSAlexander V. Chernikov 
33260cba2b28SAlexander V. Chernikov 				code = TARG(cmd->arg1, dscp) & 0x3F;
3327ae01d73cSAlexander V. Chernikov 				l = 0;		/* exit inner loop */
3328ae01d73cSAlexander V. Chernikov 				if (is_ipv4) {
3329af9aa0a8SAndrey V. Elsukov 					uint16_t old;
3330ae01d73cSAlexander V. Chernikov 
3331af9aa0a8SAndrey V. Elsukov 					old = *(uint16_t *)ip;
3332af9aa0a8SAndrey V. Elsukov 					ip->ip_tos = (code << 2) |
3333af9aa0a8SAndrey V. Elsukov 					    (ip->ip_tos & 0x03);
3334af9aa0a8SAndrey V. Elsukov 					ip->ip_sum = cksum_adjust(ip->ip_sum,
3335af9aa0a8SAndrey V. Elsukov 					    old, *(uint16_t *)ip);
3336ae01d73cSAlexander V. Chernikov 				} else if (is_ipv6) {
33374763c0aaSAndrey V. Elsukov 					/* update cached value */
33384763c0aaSAndrey V. Elsukov 					args->f_id.flow_id6 =
33394763c0aaSAndrey V. Elsukov 					    ntohl(*(uint32_t *)ip) & ~0x0FC00000;
33404763c0aaSAndrey V. Elsukov 					args->f_id.flow_id6 |= code << 22;
3341ae01d73cSAlexander V. Chernikov 
33424763c0aaSAndrey V. Elsukov 					*((uint32_t *)ip) =
33434763c0aaSAndrey V. Elsukov 					    htonl(args->f_id.flow_id6);
3344ae01d73cSAlexander V. Chernikov 				} else
3345ae01d73cSAlexander V. Chernikov 					break;
3346ae01d73cSAlexander V. Chernikov 
3347ae01d73cSAlexander V. Chernikov 				IPFW_INC_RULE_COUNTER(f, pktlen);
3348ae01d73cSAlexander V. Chernikov 				break;
3349ae01d73cSAlexander V. Chernikov 			}
3350ae01d73cSAlexander V. Chernikov 
33513b3a8eb9SGleb Smirnoff 			case O_NAT:
3352a19b3f74SAlexander V. Chernikov 				l = 0;          /* exit inner loop */
3353a19b3f74SAlexander V. Chernikov 				done = 1;       /* exit outer loop */
335466f84fabSAndrey V. Elsukov 				/*
335566f84fabSAndrey V. Elsukov 				 * Ensure that we do not invoke NAT handler for
335666f84fabSAndrey V. Elsukov 				 * non IPv4 packets. Libalias expects only IPv4.
335766f84fabSAndrey V. Elsukov 				 */
3358e11f0a0cSAndrey V. Elsukov 				if (!is_ipv4 || !IPFW_NAT_LOADED) {
33593b3a8eb9SGleb Smirnoff 				    retval = IP_FW_DENY;
3360a19b3f74SAlexander V. Chernikov 				    break;
3361a19b3f74SAlexander V. Chernikov 				}
3362a19b3f74SAlexander V. Chernikov 
33633b3a8eb9SGleb Smirnoff 				struct cfg_nat *t;
33643b3a8eb9SGleb Smirnoff 				int nat_id;
33653b3a8eb9SGleb Smirnoff 
33661cdf23bcSAndrey V. Elsukov 				args->rule.info = 0;
33673b3a8eb9SGleb Smirnoff 				set_match(args, f_pos, chain);
33683b3a8eb9SGleb Smirnoff 				/* Check if this is 'global' nat rule */
3369d6eb9b02SAndrey V. Elsukov 				if (cmd->arg1 == IP_FW_NAT44_GLOBAL) {
33703b3a8eb9SGleb Smirnoff 					retval = ipfw_nat_ptr(args, NULL, m);
33713b3a8eb9SGleb Smirnoff 					break;
33723b3a8eb9SGleb Smirnoff 				}
33733b3a8eb9SGleb Smirnoff 				t = ((ipfw_insn_nat *)cmd)->nat;
33743b3a8eb9SGleb Smirnoff 				if (t == NULL) {
33750cba2b28SAlexander V. Chernikov 					nat_id = TARG(cmd->arg1, nat);
33763b3a8eb9SGleb Smirnoff 					t = (*lookup_nat_ptr)(&chain->nat, nat_id);
33773b3a8eb9SGleb Smirnoff 
33783b3a8eb9SGleb Smirnoff 					if (t == NULL) {
33793b3a8eb9SGleb Smirnoff 					    retval = IP_FW_DENY;
33803b3a8eb9SGleb Smirnoff 					    break;
33813b3a8eb9SGleb Smirnoff 					}
33821940fa77SAlexander V. Chernikov 					if (cmd->arg1 != IP_FW_TARG)
33833b3a8eb9SGleb Smirnoff 					    ((ipfw_insn_nat *)cmd)->nat = t;
33843b3a8eb9SGleb Smirnoff 				}
33853b3a8eb9SGleb Smirnoff 				retval = ipfw_nat_ptr(args, t, m);
33863b3a8eb9SGleb Smirnoff 				break;
33873b3a8eb9SGleb Smirnoff 
33883b3a8eb9SGleb Smirnoff 			case O_REASS: {
33893b3a8eb9SGleb Smirnoff 				int ip_off;
33903b3a8eb9SGleb Smirnoff 
33913b3a8eb9SGleb Smirnoff 				l = 0;	/* in any case exit inner loop */
339212c080e6SAndrey V. Elsukov 				if (is_ipv6) /* IPv6 is not supported yet */
339312c080e6SAndrey V. Elsukov 					break;
339412c080e6SAndrey V. Elsukov 				IPFW_INC_RULE_COUNTER(f, pktlen);
33953b3a8eb9SGleb Smirnoff 				ip_off = ntohs(ip->ip_off);
33963b3a8eb9SGleb Smirnoff 
33973b3a8eb9SGleb Smirnoff 				/* if not fragmented, go to next rule */
33983b3a8eb9SGleb Smirnoff 				if ((ip_off & (IP_MF | IP_OFFMASK)) == 0)
33993b3a8eb9SGleb Smirnoff 				    break;
34003b3a8eb9SGleb Smirnoff 
34013b3a8eb9SGleb Smirnoff 				args->m = m = ip_reass(m);
34023b3a8eb9SGleb Smirnoff 
34033b3a8eb9SGleb Smirnoff 				/*
34043b3a8eb9SGleb Smirnoff 				 * do IP header checksum fixup.
34053b3a8eb9SGleb Smirnoff 				 */
34063b3a8eb9SGleb Smirnoff 				if (m == NULL) { /* fragment got swallowed */
34073b3a8eb9SGleb Smirnoff 				    retval = IP_FW_DENY;
34083b3a8eb9SGleb Smirnoff 				} else { /* good, packet complete */
34093b3a8eb9SGleb Smirnoff 				    int hlen;
34103b3a8eb9SGleb Smirnoff 
34113b3a8eb9SGleb Smirnoff 				    ip = mtod(m, struct ip *);
34123b3a8eb9SGleb Smirnoff 				    hlen = ip->ip_hl << 2;
34133b3a8eb9SGleb Smirnoff 				    ip->ip_sum = 0;
34143b3a8eb9SGleb Smirnoff 				    if (hlen == sizeof(struct ip))
34153b3a8eb9SGleb Smirnoff 					ip->ip_sum = in_cksum_hdr(ip);
34163b3a8eb9SGleb Smirnoff 				    else
34173b3a8eb9SGleb Smirnoff 					ip->ip_sum = in_cksum(m, hlen);
34183b3a8eb9SGleb Smirnoff 				    retval = IP_FW_REASS;
34191cdf23bcSAndrey V. Elsukov 				    args->rule.info = 0;
34203b3a8eb9SGleb Smirnoff 				    set_match(args, f_pos, chain);
34213b3a8eb9SGleb Smirnoff 				}
34223b3a8eb9SGleb Smirnoff 				done = 1;	/* exit outer loop */
34233b3a8eb9SGleb Smirnoff 				break;
34243b3a8eb9SGleb Smirnoff 			}
3425fc727ad6SBoris Lytochkin 
3426fc727ad6SBoris Lytochkin 			case O_SETMARK: {
3427fc727ad6SBoris Lytochkin 				l = 0;		/* exit inner loop */
3428fc727ad6SBoris Lytochkin 				args->rule.pkt_mark = (
3429fc727ad6SBoris Lytochkin 				    (cmd->arg1 == IP_FW_TARG) ?
3430fc727ad6SBoris Lytochkin 				    TARG_VAL(chain, tablearg, mark) :
34314a77657cSAndrey V. Elsukov 				    insntoc(cmd, u32)->d[0]);
3432fc727ad6SBoris Lytochkin 
3433fc727ad6SBoris Lytochkin 				IPFW_INC_RULE_COUNTER(f, pktlen);
3434fc727ad6SBoris Lytochkin 				break;
3435fc727ad6SBoris Lytochkin 			}
3436fc727ad6SBoris Lytochkin 
34372acdf79fSAndrey V. Elsukov 			case O_EXTERNAL_ACTION:
34382acdf79fSAndrey V. Elsukov 				l = 0; /* in any case exit inner loop */
34392acdf79fSAndrey V. Elsukov 				retval = ipfw_run_eaction(chain, args,
34402acdf79fSAndrey V. Elsukov 				    cmd, &done);
3441576429f0SAndrey V. Elsukov 				/*
3442576429f0SAndrey V. Elsukov 				 * If both @retval and @done are zero,
3443576429f0SAndrey V. Elsukov 				 * consider this as rule matching and
3444576429f0SAndrey V. Elsukov 				 * update counters.
3445576429f0SAndrey V. Elsukov 				 */
3446788e6286SAndrey V. Elsukov 				if (retval == 0 && done == 0) {
3447576429f0SAndrey V. Elsukov 					IPFW_INC_RULE_COUNTER(f, pktlen);
3448788e6286SAndrey V. Elsukov 					/*
3449788e6286SAndrey V. Elsukov 					 * Reset the result of the last
3450788e6286SAndrey V. Elsukov 					 * dynamic state lookup.
3451788e6286SAndrey V. Elsukov 					 * External action can change
3452788e6286SAndrey V. Elsukov 					 * @args content, and it may be
3453788e6286SAndrey V. Elsukov 					 * used for new state lookup later.
3454788e6286SAndrey V. Elsukov 					 */
3455b99a6823SAndrey V. Elsukov 					DYN_INFO_INIT(&dyn_info);
3456788e6286SAndrey V. Elsukov 				}
34572acdf79fSAndrey V. Elsukov 				break;
34583b3a8eb9SGleb Smirnoff 
34593b3a8eb9SGleb Smirnoff 			default:
34604a77657cSAndrey V. Elsukov 				panic("ipfw: rule %u: unknown opcode %d\n",
34614a77657cSAndrey V. Elsukov 				    f->rulenum, cmd->opcode);
34623b3a8eb9SGleb Smirnoff 			} /* end of switch() on opcodes */
34633b3a8eb9SGleb Smirnoff 			/*
34643b3a8eb9SGleb Smirnoff 			 * if we get here with l=0, then match is irrelevant.
34653b3a8eb9SGleb Smirnoff 			 */
34663b3a8eb9SGleb Smirnoff 
34673b3a8eb9SGleb Smirnoff 			if (cmd->len & F_NOT)
34683b3a8eb9SGleb Smirnoff 				match = !match;
34693b3a8eb9SGleb Smirnoff 
34703b3a8eb9SGleb Smirnoff 			if (match) {
34713b3a8eb9SGleb Smirnoff 				if (cmd->len & F_OR)
34723b3a8eb9SGleb Smirnoff 					skip_or = 1;
34733b3a8eb9SGleb Smirnoff 			} else {
34743b3a8eb9SGleb Smirnoff 				if (!(cmd->len & F_OR)) /* not an OR block, */
34753b3a8eb9SGleb Smirnoff 					break;		/* try next rule    */
34763b3a8eb9SGleb Smirnoff 			}
34773b3a8eb9SGleb Smirnoff 
34783b3a8eb9SGleb Smirnoff 		}	/* end of inner loop, scan opcodes */
34793b3a8eb9SGleb Smirnoff #undef PULLUP_LEN
3480d4e6a529SAndrey V. Elsukov #undef PULLUP_LEN_LOCKED
34813b3a8eb9SGleb Smirnoff 
34823b3a8eb9SGleb Smirnoff 		if (done)
34833b3a8eb9SGleb Smirnoff 			break;
34843b3a8eb9SGleb Smirnoff 
34853b3a8eb9SGleb Smirnoff /* next_rule:; */	/* try next rule		*/
34863b3a8eb9SGleb Smirnoff 
34873b3a8eb9SGleb Smirnoff 	}		/* end of outer for, scan rules */
34883b3a8eb9SGleb Smirnoff 
34893b3a8eb9SGleb Smirnoff 	if (done) {
34903b3a8eb9SGleb Smirnoff 		struct ip_fw *rule = chain->map[f_pos];
34913b3a8eb9SGleb Smirnoff 		/* Update statistics */
3492c187c1fbSAlexander V. Chernikov 		IPFW_INC_RULE_COUNTER(rule, pktlen);
34937ec2f6bcSAndrey V. Elsukov 		IPFW_PROBE(rule__matched, retval,
34947ec2f6bcSAndrey V. Elsukov 		    is_ipv4 ? AF_INET : AF_INET6,
34957ec2f6bcSAndrey V. Elsukov 		    is_ipv4 ? (uintptr_t)&src_ip :
34967ec2f6bcSAndrey V. Elsukov 		        (uintptr_t)&args->f_id.src_ip6,
34977ec2f6bcSAndrey V. Elsukov 		    is_ipv4 ? (uintptr_t)&dst_ip :
34987ec2f6bcSAndrey V. Elsukov 		        (uintptr_t)&args->f_id.dst_ip6,
34997ec2f6bcSAndrey V. Elsukov 		    args, rule);
35003b3a8eb9SGleb Smirnoff 	} else {
35013b3a8eb9SGleb Smirnoff 		retval = IP_FW_DENY;
35023b3a8eb9SGleb Smirnoff 		printf("ipfw: ouch!, skip past end of rules, denying packet\n");
35033b3a8eb9SGleb Smirnoff 	}
350493bb4f9eSAndrey V. Elsukov 	IPFW_PF_RUNLOCK(chain);
35058e780285SRichard Scheffenegger 	if (need_send_reject) {
35068e780285SRichard Scheffenegger #ifdef INET6
35078e780285SRichard Scheffenegger 		if (is_ipv6)
35088e780285SRichard Scheffenegger 			send_reject6(args, reject_code, hlen,
35098e780285SRichard Scheffenegger 				     (struct ip6_hdr *)ip);
35108e780285SRichard Scheffenegger 		else
35118e780285SRichard Scheffenegger #endif
35128e780285SRichard Scheffenegger 			send_reject(args, reject_code, reject_mtu,
35138e780285SRichard Scheffenegger 				    iplen, ip);
35148e780285SRichard Scheffenegger 	}
35153b3a8eb9SGleb Smirnoff #ifdef __FreeBSD__
35163b3a8eb9SGleb Smirnoff 	if (ucred_cache != NULL)
35173b3a8eb9SGleb Smirnoff 		crfree(ucred_cache);
35183b3a8eb9SGleb Smirnoff #endif
35193b3a8eb9SGleb Smirnoff 	return (retval);
35203b3a8eb9SGleb Smirnoff 
35213b3a8eb9SGleb Smirnoff pullup_failed:
35223b3a8eb9SGleb Smirnoff 	if (V_fw_verbose)
35233b3a8eb9SGleb Smirnoff 		printf("ipfw: pullup failed\n");
35243b3a8eb9SGleb Smirnoff 	return (IP_FW_DENY);
35253b3a8eb9SGleb Smirnoff }
35263b3a8eb9SGleb Smirnoff 
35273b3a8eb9SGleb Smirnoff /*
35283b3a8eb9SGleb Smirnoff  * Set maximum number of tables that can be used in given VNET ipfw instance.
35293b3a8eb9SGleb Smirnoff  */
35303b3a8eb9SGleb Smirnoff #ifdef SYSCTL_NODE
35313b3a8eb9SGleb Smirnoff static int
sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS)35323b3a8eb9SGleb Smirnoff sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS)
35333b3a8eb9SGleb Smirnoff {
35343b3a8eb9SGleb Smirnoff 	int error;
35353b3a8eb9SGleb Smirnoff 	unsigned int ntables;
35363b3a8eb9SGleb Smirnoff 
35373b3a8eb9SGleb Smirnoff 	ntables = V_fw_tables_max;
35383b3a8eb9SGleb Smirnoff 
35393b3a8eb9SGleb Smirnoff 	error = sysctl_handle_int(oidp, &ntables, 0, req);
35403b3a8eb9SGleb Smirnoff 	/* Read operation or some error */
35413b3a8eb9SGleb Smirnoff 	if ((error != 0) || (req->newptr == NULL))
35423b3a8eb9SGleb Smirnoff 		return (error);
35433b3a8eb9SGleb Smirnoff 
35443b3a8eb9SGleb Smirnoff 	return (ipfw_resize_tables(&V_layer3_chain, ntables));
35453b3a8eb9SGleb Smirnoff }
3546a73d728dSAlexander V. Chernikov 
3547a73d728dSAlexander V. Chernikov /*
3548a73d728dSAlexander V. Chernikov  * Switches table namespace between global and per-set.
3549a73d728dSAlexander V. Chernikov  */
3550a73d728dSAlexander V. Chernikov static int
sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS)3551a73d728dSAlexander V. Chernikov sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS)
3552a73d728dSAlexander V. Chernikov {
3553a73d728dSAlexander V. Chernikov 	int error;
3554a73d728dSAlexander V. Chernikov 	unsigned int sets;
3555a73d728dSAlexander V. Chernikov 
3556a73d728dSAlexander V. Chernikov 	sets = V_fw_tables_sets;
3557a73d728dSAlexander V. Chernikov 
3558a73d728dSAlexander V. Chernikov 	error = sysctl_handle_int(oidp, &sets, 0, req);
3559a73d728dSAlexander V. Chernikov 	/* Read operation or some error */
3560a73d728dSAlexander V. Chernikov 	if ((error != 0) || (req->newptr == NULL))
3561a73d728dSAlexander V. Chernikov 		return (error);
3562a73d728dSAlexander V. Chernikov 
3563a73d728dSAlexander V. Chernikov 	return (ipfw_switch_tables_namespace(&V_layer3_chain, sets));
3564a73d728dSAlexander V. Chernikov }
35653b3a8eb9SGleb Smirnoff #endif
3566a73d728dSAlexander V. Chernikov 
35673b3a8eb9SGleb Smirnoff /*
35683b3a8eb9SGleb Smirnoff  * Module and VNET glue
35693b3a8eb9SGleb Smirnoff  */
35703b3a8eb9SGleb Smirnoff 
35713b3a8eb9SGleb Smirnoff /*
35723b3a8eb9SGleb Smirnoff  * Stuff that must be initialised only on boot or module load
35733b3a8eb9SGleb Smirnoff  */
35743b3a8eb9SGleb Smirnoff static int
ipfw_init(void)35753b3a8eb9SGleb Smirnoff ipfw_init(void)
35763b3a8eb9SGleb Smirnoff {
35773b3a8eb9SGleb Smirnoff 	int error = 0;
35783b3a8eb9SGleb Smirnoff 
35793b3a8eb9SGleb Smirnoff 	/*
35803b3a8eb9SGleb Smirnoff  	 * Only print out this stuff the first time around,
35813b3a8eb9SGleb Smirnoff 	 * when called from the sysinit code.
35823b3a8eb9SGleb Smirnoff 	 */
35833b3a8eb9SGleb Smirnoff 	printf("ipfw2 "
35843b3a8eb9SGleb Smirnoff #ifdef INET6
35853b3a8eb9SGleb Smirnoff 		"(+ipv6) "
35863b3a8eb9SGleb Smirnoff #endif
35873b3a8eb9SGleb Smirnoff 		"initialized, divert %s, nat %s, "
35883b3a8eb9SGleb Smirnoff 		"default to %s, logging ",
35893b3a8eb9SGleb Smirnoff #ifdef IPDIVERT
35903b3a8eb9SGleb Smirnoff 		"enabled",
35913b3a8eb9SGleb Smirnoff #else
35923b3a8eb9SGleb Smirnoff 		"loadable",
35933b3a8eb9SGleb Smirnoff #endif
35943b3a8eb9SGleb Smirnoff #ifdef IPFIREWALL_NAT
35953b3a8eb9SGleb Smirnoff 		"enabled",
35963b3a8eb9SGleb Smirnoff #else
35973b3a8eb9SGleb Smirnoff 		"loadable",
35983b3a8eb9SGleb Smirnoff #endif
35993b3a8eb9SGleb Smirnoff 		default_to_accept ? "accept" : "deny");
36003b3a8eb9SGleb Smirnoff 
36013b3a8eb9SGleb Smirnoff 	/*
36023b3a8eb9SGleb Smirnoff 	 * Note: V_xxx variables can be accessed here but the vnet specific
36033b3a8eb9SGleb Smirnoff 	 * initializer may not have been called yet for the VIMAGE case.
36043b3a8eb9SGleb Smirnoff 	 * Tuneables will have been processed. We will print out values for
36053b3a8eb9SGleb Smirnoff 	 * the default vnet.
36063b3a8eb9SGleb Smirnoff 	 * XXX This should all be rationalized AFTER 8.0
36073b3a8eb9SGleb Smirnoff 	 */
36083b3a8eb9SGleb Smirnoff 	if (V_fw_verbose == 0)
36093b3a8eb9SGleb Smirnoff 		printf("disabled\n");
36103b3a8eb9SGleb Smirnoff 	else if (V_verbose_limit == 0)
36113b3a8eb9SGleb Smirnoff 		printf("unlimited\n");
36123b3a8eb9SGleb Smirnoff 	else
36133b3a8eb9SGleb Smirnoff 		printf("limited to %d packets/entry by default\n",
36143b3a8eb9SGleb Smirnoff 		    V_verbose_limit);
36153b3a8eb9SGleb Smirnoff 
36163b3a8eb9SGleb Smirnoff 	/* Check user-supplied table count for validness */
36173b3a8eb9SGleb Smirnoff 	if (default_fw_tables > IPFW_TABLES_MAX)
36183b3a8eb9SGleb Smirnoff 	  default_fw_tables = IPFW_TABLES_MAX;
36193b3a8eb9SGleb Smirnoff 
36206b988f3aSAlexander V. Chernikov 	ipfw_init_sopt_handler();
36210d9cbb87SAndrey V. Elsukov 	ipfw_init_obj_rewriter();
362268394ec8SAlexander V. Chernikov 	ipfw_iface_init();
36233b3a8eb9SGleb Smirnoff 	return (error);
36243b3a8eb9SGleb Smirnoff }
36253b3a8eb9SGleb Smirnoff 
36263b3a8eb9SGleb Smirnoff /*
36273b3a8eb9SGleb Smirnoff  * Called for the removal of the last instance only on module unload.
36283b3a8eb9SGleb Smirnoff  */
36293b3a8eb9SGleb Smirnoff static void
ipfw_destroy(void)36303b3a8eb9SGleb Smirnoff ipfw_destroy(void)
36313b3a8eb9SGleb Smirnoff {
36323b3a8eb9SGleb Smirnoff 
363368394ec8SAlexander V. Chernikov 	ipfw_iface_destroy();
36346b988f3aSAlexander V. Chernikov 	ipfw_destroy_sopt_handler();
36350d9cbb87SAndrey V. Elsukov 	ipfw_destroy_obj_rewriter();
36363b3a8eb9SGleb Smirnoff 	printf("IP firewall unloaded\n");
36373b3a8eb9SGleb Smirnoff }
36383b3a8eb9SGleb Smirnoff 
36393b3a8eb9SGleb Smirnoff /*
36403b3a8eb9SGleb Smirnoff  * Stuff that must be initialized for every instance
36413b3a8eb9SGleb Smirnoff  * (including the first of course).
36423b3a8eb9SGleb Smirnoff  */
36433b3a8eb9SGleb Smirnoff static int
vnet_ipfw_init(const void * unused)36443b3a8eb9SGleb Smirnoff vnet_ipfw_init(const void *unused)
36453b3a8eb9SGleb Smirnoff {
36466b988f3aSAlexander V. Chernikov 	int error, first;
36473b3a8eb9SGleb Smirnoff 	struct ip_fw *rule = NULL;
36483b3a8eb9SGleb Smirnoff 	struct ip_fw_chain *chain;
36493b3a8eb9SGleb Smirnoff 
36503b3a8eb9SGleb Smirnoff 	chain = &V_layer3_chain;
36513b3a8eb9SGleb Smirnoff 
36526b988f3aSAlexander V. Chernikov 	first = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
36536b988f3aSAlexander V. Chernikov 
36543b3a8eb9SGleb Smirnoff 	/* First set up some values that are compile time options */
36553b3a8eb9SGleb Smirnoff 	V_autoinc_step = 100;	/* bounded to 1..1000 in add_rule() */
36563b3a8eb9SGleb Smirnoff 	V_fw_deny_unknown_exthdrs = 1;
36573b3a8eb9SGleb Smirnoff #ifdef IPFIREWALL_VERBOSE
36583b3a8eb9SGleb Smirnoff 	V_fw_verbose = 1;
36593b3a8eb9SGleb Smirnoff #endif
36603b3a8eb9SGleb Smirnoff #ifdef IPFIREWALL_VERBOSE_LIMIT
36613b3a8eb9SGleb Smirnoff 	V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
36623b3a8eb9SGleb Smirnoff #endif
36633b3a8eb9SGleb Smirnoff #ifdef IPFIREWALL_NAT
36643b3a8eb9SGleb Smirnoff 	LIST_INIT(&chain->nat);
36653b3a8eb9SGleb Smirnoff #endif
36663b3a8eb9SGleb Smirnoff 
366774b22066SAlexander V. Chernikov 	/* Init shared services hash table */
366874b22066SAlexander V. Chernikov 	ipfw_init_srv(chain);
366974b22066SAlexander V. Chernikov 
36707e767c79SAlexander V. Chernikov 	ipfw_init_counters();
36713b3a8eb9SGleb Smirnoff 	/* Set initial number of tables */
36723b3a8eb9SGleb Smirnoff 	V_fw_tables_max = default_fw_tables;
36736b988f3aSAlexander V. Chernikov 	error = ipfw_init_tables(chain, first);
36743b3a8eb9SGleb Smirnoff 	if (error) {
36753b3a8eb9SGleb Smirnoff 		printf("ipfw2: setting up tables failed\n");
36763b3a8eb9SGleb Smirnoff 		free(chain->map, M_IPFW);
36773b3a8eb9SGleb Smirnoff 		free(rule, M_IPFW);
36783b3a8eb9SGleb Smirnoff 		return (ENOSPC);
36793b3a8eb9SGleb Smirnoff 	}
36803b3a8eb9SGleb Smirnoff 
36817143bb76SAndrey V. Elsukov 	IPFW_LOCK_INIT(chain);
36827143bb76SAndrey V. Elsukov 
36833b3a8eb9SGleb Smirnoff 	/* fill and insert the default rule */
36847143bb76SAndrey V. Elsukov 	rule = ipfw_alloc_rule(chain, sizeof(struct ip_fw));
36857d4b2d52SAndrey V. Elsukov 	rule->flags |= IPFW_RULE_NOOPT;
36863b3a8eb9SGleb Smirnoff 	rule->cmd_len = 1;
36873b3a8eb9SGleb Smirnoff 	rule->cmd[0].len = 1;
36883b3a8eb9SGleb Smirnoff 	rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY;
36897143bb76SAndrey V. Elsukov 	chain->default_rule = rule;
36907143bb76SAndrey V. Elsukov 	ipfw_add_protected_rule(chain, rule, 0);
36913b3a8eb9SGleb Smirnoff 
36922e089d5cSAlexander V. Chernikov 	ipfw_dyn_init(chain);
36932acdf79fSAndrey V. Elsukov 	ipfw_eaction_init(chain, first);
369446d52008SAlexander V. Chernikov 	ipfw_init_skipto_cache(chain);
369556132dccSAndrey V. Elsukov 	ipfw_bpf_init(first);
36963b3a8eb9SGleb Smirnoff 
36973b3a8eb9SGleb Smirnoff 	/* First set up some values that are compile time options */
36983b3a8eb9SGleb Smirnoff 	V_ipfw_vnet_ready = 1;		/* Open for business */
36993b3a8eb9SGleb Smirnoff 
37003b3a8eb9SGleb Smirnoff 	/*
37013b3a8eb9SGleb Smirnoff 	 * Hook the sockopt handler and pfil hooks for ipv4 and ipv6.
37023b3a8eb9SGleb Smirnoff 	 * Even if the latter two fail we still keep the module alive
37033b3a8eb9SGleb Smirnoff 	 * because the sockopt and layer2 paths are still useful.
37043b3a8eb9SGleb Smirnoff 	 * ipfw[6]_hook return 0 on success, ENOENT on failure,
37053b3a8eb9SGleb Smirnoff 	 * so we can ignore the exact return value and just set a flag.
37063b3a8eb9SGleb Smirnoff 	 *
37073b3a8eb9SGleb Smirnoff 	 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so
37083b3a8eb9SGleb Smirnoff 	 * changes in the underlying (per-vnet) variables trigger
37093b3a8eb9SGleb Smirnoff 	 * immediate hook()/unhook() calls.
37103b3a8eb9SGleb Smirnoff 	 * In layer2 we have the same behaviour, except that V_ether_ipfw
37113b3a8eb9SGleb Smirnoff 	 * is checked on each packet because there are no pfil hooks.
37123b3a8eb9SGleb Smirnoff 	 */
371391e721d7SAlexander V. Chernikov 	V_ip_fw_ctl_ptr = ipfw_ctl3;
371497245d40SGleb Smirnoff 	error = ipfw_attach_hooks();
37153b3a8eb9SGleb Smirnoff 	return (error);
37163b3a8eb9SGleb Smirnoff }
37173b3a8eb9SGleb Smirnoff 
37183b3a8eb9SGleb Smirnoff /*
37193b3a8eb9SGleb Smirnoff  * Called for the removal of each instance.
37203b3a8eb9SGleb Smirnoff  */
37213b3a8eb9SGleb Smirnoff static int
vnet_ipfw_uninit(const void * unused)37223b3a8eb9SGleb Smirnoff vnet_ipfw_uninit(const void *unused)
37233b3a8eb9SGleb Smirnoff {
3724030b184fSAlexander V. Chernikov 	struct ip_fw *reap;
37253b3a8eb9SGleb Smirnoff 	struct ip_fw_chain *chain = &V_layer3_chain;
37266b988f3aSAlexander V. Chernikov 	int i, last;
37273b3a8eb9SGleb Smirnoff 
37283b3a8eb9SGleb Smirnoff 	V_ipfw_vnet_ready = 0; /* tell new callers to go away */
37293b3a8eb9SGleb Smirnoff 	/*
37303b3a8eb9SGleb Smirnoff 	 * disconnect from ipv4, ipv6, layer2 and sockopt.
37313b3a8eb9SGleb Smirnoff 	 * Then grab, release and grab again the WLOCK so we make
37323b3a8eb9SGleb Smirnoff 	 * sure the update is propagated and nobody will be in.
37333b3a8eb9SGleb Smirnoff 	 */
373497245d40SGleb Smirnoff 	ipfw_detach_hooks();
37353b3a8eb9SGleb Smirnoff 	V_ip_fw_ctl_ptr = NULL;
37366b988f3aSAlexander V. Chernikov 
37376b988f3aSAlexander V. Chernikov 	last = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
37386b988f3aSAlexander V. Chernikov 
37393b3a8eb9SGleb Smirnoff 	IPFW_UH_WLOCK(chain);
37403b3a8eb9SGleb Smirnoff 	IPFW_UH_WUNLOCK(chain);
37413b3a8eb9SGleb Smirnoff 
37423b3a8eb9SGleb Smirnoff 	ipfw_dyn_uninit(0);	/* run the callout_drain */
3743c8cfbc06SHans Petter Selasky 
3744c8cfbc06SHans Petter Selasky 	IPFW_UH_WLOCK(chain);
37453b3a8eb9SGleb Smirnoff 
37463b3a8eb9SGleb Smirnoff 	reap = NULL;
37473b3a8eb9SGleb Smirnoff 	IPFW_WLOCK(chain);
3748030b184fSAlexander V. Chernikov 	for (i = 0; i < chain->n_rules; i++)
3749030b184fSAlexander V. Chernikov 		ipfw_reap_add(chain, &reap, chain->map[i]);
37503b3a8eb9SGleb Smirnoff 	free(chain->map, M_IPFW);
3751d5eb80cbSAlexander V. Chernikov 	ipfw_destroy_skipto_cache(chain);
37523b3a8eb9SGleb Smirnoff 	IPFW_WUNLOCK(chain);
37533b3a8eb9SGleb Smirnoff 	IPFW_UH_WUNLOCK(chain);
37546b988f3aSAlexander V. Chernikov 	ipfw_destroy_tables(chain, last);
37552acdf79fSAndrey V. Elsukov 	ipfw_eaction_uninit(chain, last);
37563b3a8eb9SGleb Smirnoff 	if (reap != NULL)
37573b3a8eb9SGleb Smirnoff 		ipfw_reap_rules(reap);
375868394ec8SAlexander V. Chernikov 	vnet_ipfw_iface_destroy(chain);
375974b22066SAlexander V. Chernikov 	ipfw_destroy_srv(chain);
37603b3a8eb9SGleb Smirnoff 	IPFW_LOCK_DESTROY(chain);
37613b3a8eb9SGleb Smirnoff 	ipfw_dyn_uninit(1);	/* free the remaining parts */
37627e767c79SAlexander V. Chernikov 	ipfw_destroy_counters();
376356132dccSAndrey V. Elsukov 	ipfw_bpf_uninit(last);
37647e767c79SAlexander V. Chernikov 	return (0);
37653b3a8eb9SGleb Smirnoff }
37663b3a8eb9SGleb Smirnoff 
37673b3a8eb9SGleb Smirnoff /*
37683b3a8eb9SGleb Smirnoff  * Module event handler.
37693b3a8eb9SGleb Smirnoff  * In general we have the choice of handling most of these events by the
37703b3a8eb9SGleb Smirnoff  * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to
37713b3a8eb9SGleb Smirnoff  * use the SYSINIT handlers as they are more capable of expressing the
37723b3a8eb9SGleb Smirnoff  * flow of control during module and vnet operations, so this is just
37733b3a8eb9SGleb Smirnoff  * a skeleton. Note there is no SYSINIT equivalent of the module
37743b3a8eb9SGleb Smirnoff  * SHUTDOWN handler, but we don't have anything to do in that case anyhow.
37753b3a8eb9SGleb Smirnoff  */
37763b3a8eb9SGleb Smirnoff static int
ipfw_modevent(module_t mod,int type,void * unused)37773b3a8eb9SGleb Smirnoff ipfw_modevent(module_t mod, int type, void *unused)
37783b3a8eb9SGleb Smirnoff {
37793b3a8eb9SGleb Smirnoff 	int err = 0;
37803b3a8eb9SGleb Smirnoff 
37813b3a8eb9SGleb Smirnoff 	switch (type) {
37823b3a8eb9SGleb Smirnoff 	case MOD_LOAD:
37833b3a8eb9SGleb Smirnoff 		/* Called once at module load or
37843b3a8eb9SGleb Smirnoff 	 	 * system boot if compiled in. */
37853b3a8eb9SGleb Smirnoff 		break;
37863b3a8eb9SGleb Smirnoff 	case MOD_QUIESCE:
37873b3a8eb9SGleb Smirnoff 		/* Called before unload. May veto unloading. */
37883b3a8eb9SGleb Smirnoff 		break;
37893b3a8eb9SGleb Smirnoff 	case MOD_UNLOAD:
37903b3a8eb9SGleb Smirnoff 		/* Called during unload. */
37913b3a8eb9SGleb Smirnoff 		break;
37923b3a8eb9SGleb Smirnoff 	case MOD_SHUTDOWN:
37933b3a8eb9SGleb Smirnoff 		/* Called during system shutdown. */
37943b3a8eb9SGleb Smirnoff 		break;
37953b3a8eb9SGleb Smirnoff 	default:
37963b3a8eb9SGleb Smirnoff 		err = EOPNOTSUPP;
37973b3a8eb9SGleb Smirnoff 		break;
37983b3a8eb9SGleb Smirnoff 	}
37993b3a8eb9SGleb Smirnoff 	return err;
38003b3a8eb9SGleb Smirnoff }
38013b3a8eb9SGleb Smirnoff 
38023b3a8eb9SGleb Smirnoff static moduledata_t ipfwmod = {
38033b3a8eb9SGleb Smirnoff 	"ipfw",
38043b3a8eb9SGleb Smirnoff 	ipfw_modevent,
38059823d527SKevin Lo 	0
38063b3a8eb9SGleb Smirnoff };
38073b3a8eb9SGleb Smirnoff 
38083b3a8eb9SGleb Smirnoff /* Define startup order. */
380989856f7eSBjoern A. Zeeb #define	IPFW_SI_SUB_FIREWALL	SI_SUB_PROTO_FIREWALL
38103b3a8eb9SGleb Smirnoff #define	IPFW_MODEVENT_ORDER	(SI_ORDER_ANY - 255) /* On boot slot in here. */
38113b3a8eb9SGleb Smirnoff #define	IPFW_MODULE_ORDER	(IPFW_MODEVENT_ORDER + 1) /* A little later. */
38123b3a8eb9SGleb Smirnoff #define	IPFW_VNET_ORDER		(IPFW_MODEVENT_ORDER + 2) /* Later still. */
38133b3a8eb9SGleb Smirnoff 
38143b3a8eb9SGleb Smirnoff DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER);
381560805b89SAlexander V. Chernikov FEATURE(ipfw_ctl3, "ipfw new sockopt calls");
3816f9ab623bSAlexander V. Chernikov MODULE_VERSION(ipfw, 3);
38173b3a8eb9SGleb Smirnoff /* should declare some dependencies here */
38183b3a8eb9SGleb Smirnoff 
38193b3a8eb9SGleb Smirnoff /*
38203b3a8eb9SGleb Smirnoff  * Starting up. Done in order after ipfwmod() has been called.
38213b3a8eb9SGleb Smirnoff  * VNET_SYSINIT is also called for each existing vnet and each new vnet.
38223b3a8eb9SGleb Smirnoff  */
38233b3a8eb9SGleb Smirnoff SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
38243b3a8eb9SGleb Smirnoff 	    ipfw_init, NULL);
38253b3a8eb9SGleb Smirnoff VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
38263b3a8eb9SGleb Smirnoff 	    vnet_ipfw_init, NULL);
38273b3a8eb9SGleb Smirnoff 
38283b3a8eb9SGleb Smirnoff /*
38293b3a8eb9SGleb Smirnoff  * Closing up shop. These are done in REVERSE ORDER, but still
38303b3a8eb9SGleb Smirnoff  * after ipfwmod() has been called. Not called on reboot.
38313b3a8eb9SGleb Smirnoff  * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
38323b3a8eb9SGleb Smirnoff  * or when the module is unloaded.
38333b3a8eb9SGleb Smirnoff  */
38343b3a8eb9SGleb Smirnoff SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
38353b3a8eb9SGleb Smirnoff 	    ipfw_destroy, NULL);
38363b3a8eb9SGleb Smirnoff VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
38373b3a8eb9SGleb Smirnoff 	    vnet_ipfw_uninit, NULL);
38383b3a8eb9SGleb Smirnoff /* end of file */
3839