xref: /freebsd/sys/netpfil/ipfw/ip_fw2.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * The FreeBSD IP packet firewall, main file
33  */
34 
35 #include "opt_ipfw.h"
36 #include "opt_ipdivert.h"
37 #include "opt_inet.h"
38 #ifndef INET
39 #error "IPFIREWALL requires INET"
40 #endif /* INET */
41 #include "opt_inet6.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/condvar.h>
46 #include <sys/counter.h>
47 #include <sys/eventhandler.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/jail.h>
53 #include <sys/module.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/rwlock.h>
57 #include <sys/rmlock.h>
58 #include <sys/sdt.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/syslog.h>
63 #include <sys/ucred.h>
64 #include <net/ethernet.h> /* for ETHERTYPE_IP */
65 #include <net/if.h>
66 #include <net/if_var.h>
67 #include <net/if_private.h>
68 #include <net/route.h>
69 #include <net/route/nhop.h>
70 #include <net/pfil.h>
71 #include <net/vnet.h>
72 
73 #include <netpfil/pf/pf_mtag.h>
74 
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/ip.h>
79 #include <netinet/ip_var.h>
80 #include <netinet/ip_icmp.h>
81 #include <netinet/ip_fw.h>
82 #include <netinet/ip_carp.h>
83 #include <netinet/pim.h>
84 #include <netinet/tcp_var.h>
85 #include <netinet/udp.h>
86 #include <netinet/udp_var.h>
87 #include <netinet/sctp.h>
88 #include <netinet/sctp_crc32.h>
89 #include <netinet/sctp_header.h>
90 
91 #include <netinet/ip6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet/in_fib.h>
94 #ifdef INET6
95 #include <netinet6/in6_fib.h>
96 #include <netinet6/in6_pcb.h>
97 #include <netinet6/scope6_var.h>
98 #include <netinet6/ip6_var.h>
99 #endif
100 
101 #include <net/if_gre.h> /* for struct grehdr */
102 
103 #include <netpfil/ipfw/ip_fw_private.h>
104 
105 #include <machine/in_cksum.h>	/* XXX for in_cksum */
106 
107 #ifdef MAC
108 #include <security/mac/mac_framework.h>
109 #endif
110 
111 #define	IPFW_PROBE(probe, arg0, arg1, arg2, arg3, arg4, arg5)		\
112     SDT_PROBE6(ipfw, , , probe, arg0, arg1, arg2, arg3, arg4, arg5)
113 
114 SDT_PROVIDER_DEFINE(ipfw);
115 SDT_PROBE_DEFINE6(ipfw, , , rule__matched,
116     "int",			/* retval */
117     "int",			/* af */
118     "void *",			/* src addr */
119     "void *",			/* dst addr */
120     "struct ip_fw_args *",	/* args */
121     "struct ip_fw *"		/* rule */);
122 
123 /*
124  * static variables followed by global ones.
125  * All ipfw global variables are here.
126  */
127 
128 VNET_DEFINE_STATIC(int, fw_deny_unknown_exthdrs);
129 #define	V_fw_deny_unknown_exthdrs	VNET(fw_deny_unknown_exthdrs)
130 
131 VNET_DEFINE_STATIC(int, fw_permit_single_frag6) = 1;
132 #define	V_fw_permit_single_frag6	VNET(fw_permit_single_frag6)
133 
134 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
135 static int default_to_accept = 1;
136 #else
137 static int default_to_accept;
138 #endif
139 
140 VNET_DEFINE(int, autoinc_step);
141 VNET_DEFINE(int, fw_one_pass) = 1;
142 
143 VNET_DEFINE(unsigned int, fw_tables_max);
144 VNET_DEFINE(unsigned int, fw_tables_sets) = 0;	/* Don't use set-aware tables */
145 /* Use 128 tables by default */
146 static unsigned int default_fw_tables = IPFW_TABLES_DEFAULT;
147 
148 static int jump_lookup_pos(struct ip_fw_chain *chain, struct ip_fw *f, int num,
149     int tablearg, int jump_backwards);
150 #ifndef LINEAR_SKIPTO
151 static int jump_cached(struct ip_fw_chain *chain, struct ip_fw *f, int num,
152     int tablearg, int jump_backwards);
153 #define	JUMP(ch, f, num, targ, back)	jump_cached(ch, f, num, targ, back)
154 #else
155 #define	JUMP(ch, f, num, targ, back)	jump_lookup_pos(ch, f, num, targ, back)
156 #endif
157 
158 /*
159  * Each rule belongs to one of 32 different sets (0..31).
160  * The variable set_disable contains one bit per set.
161  * If the bit is set, all rules in the corresponding set
162  * are disabled. Set RESVD_SET(31) is reserved for the default rule
163  * and rules that are not deleted by the flush command,
164  * and CANNOT be disabled.
165  * Rules in set RESVD_SET can only be deleted individually.
166  */
167 VNET_DEFINE(u_int32_t, set_disable);
168 #define	V_set_disable			VNET(set_disable)
169 
170 VNET_DEFINE(int, fw_verbose);
171 /* counter for ipfw_log(NULL...) */
172 VNET_DEFINE(u_int64_t, norule_counter);
173 VNET_DEFINE(int, verbose_limit);
174 
175 /* layer3_chain contains the list of rules for layer 3 */
176 VNET_DEFINE(struct ip_fw_chain, layer3_chain);
177 
178 /* ipfw_vnet_ready controls when we are open for business */
179 VNET_DEFINE(int, ipfw_vnet_ready) = 0;
180 
181 VNET_DEFINE(int, ipfw_nat_ready) = 0;
182 
183 ipfw_nat_t *ipfw_nat_ptr = NULL;
184 struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
185 ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
186 ipfw_nat_cfg_t *ipfw_nat_del_ptr;
187 ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
188 ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
189 
190 #ifdef SYSCTL_NODE
191 uint32_t dummy_def = IPFW_DEFAULT_RULE;
192 static int sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS);
193 static int sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS);
194 
195 SYSBEGIN(f3)
196 
197 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
198     "Firewall");
199 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, one_pass,
200     CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0,
201     "Only do a single pass through ipfw when using dummynet(4)");
202 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step,
203     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(autoinc_step), 0,
204     "Rule number auto-increment step");
205 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose,
206     CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0,
207     "Log matches to ipfw rules");
208 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit,
209     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(verbose_limit), 0,
210     "Set upper limit of matches of ipfw rules logged");
211 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD,
212     &dummy_def, 0,
213     "The default/max possible rule number.");
214 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_max,
215     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
216     0, 0, sysctl_ipfw_table_num, "IU",
217     "Maximum number of concurrently used tables");
218 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_sets,
219     CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE,
220     0, 0, sysctl_ipfw_tables_sets, "IU",
221     "Use per-set namespace for tables");
222 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN,
223     &default_to_accept, 0,
224     "Make the default rule accept all packets.");
225 TUNABLE_INT("net.inet.ip.fw.tables_max", (int *)&default_fw_tables);
226 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count,
227     CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0,
228     "Number of static rules");
229 
230 #ifdef INET6
231 SYSCTL_DECL(_net_inet6_ip6);
232 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
233     "Firewall");
234 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs,
235     CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
236     &VNET_NAME(fw_deny_unknown_exthdrs), 0,
237     "Deny packets with unknown IPv6 Extension Headers");
238 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, permit_single_frag6,
239     CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
240     &VNET_NAME(fw_permit_single_frag6), 0,
241     "Permit single packet IPv6 fragments");
242 #endif /* INET6 */
243 
244 SYSEND
245 
246 #endif /* SYSCTL_NODE */
247 
248 /*
249  * Some macros used in the various matching options.
250  * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
251  * Other macros just cast void * into the appropriate type
252  */
253 #define	L3HDR(T, ip)	((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
254 #define	TCP(p)		((struct tcphdr *)(p))
255 #define	SCTP(p)		((struct sctphdr *)(p))
256 #define	UDP(p)		((struct udphdr *)(p))
257 #define	ICMP(p)		((struct icmphdr *)(p))
258 #define	ICMP6(p)	((struct icmp6_hdr *)(p))
259 
260 static __inline int
261 icmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd)
262 {
263 	int type = icmp->icmp_type;
264 
265 	return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) );
266 }
267 
268 #define TT	( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
269     (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
270 
271 static int
272 is_icmp_query(struct icmphdr *icmp)
273 {
274 	int type = icmp->icmp_type;
275 
276 	return (type <= ICMP_MAXTYPE && (TT & (1<<type)) );
277 }
278 #undef TT
279 
280 /*
281  * The following checks use two arrays of 8 or 16 bits to store the
282  * bits that we want set or clear, respectively. They are in the
283  * low and high half of cmd->arg1 or cmd->d[0].
284  *
285  * We scan options and store the bits we find set. We succeed if
286  *
287  *	(want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
288  *
289  * The code is sometimes optimized not to store additional variables.
290  */
291 
292 static int
293 flags_match(ipfw_insn *cmd, u_int8_t bits)
294 {
295 	u_char want_clear;
296 	bits = ~bits;
297 
298 	if ( ((cmd->arg1 & 0xff) & bits) != 0)
299 		return 0; /* some bits we want set were clear */
300 	want_clear = (cmd->arg1 >> 8) & 0xff;
301 	if ( (want_clear & bits) != want_clear)
302 		return 0; /* some bits we want clear were set */
303 	return 1;
304 }
305 
306 static int
307 ipopts_match(struct ip *ip, ipfw_insn *cmd)
308 {
309 	int optlen, bits = 0;
310 	u_char *cp = (u_char *)(ip + 1);
311 	int x = (ip->ip_hl << 2) - sizeof (struct ip);
312 
313 	for (; x > 0; x -= optlen, cp += optlen) {
314 		int opt = cp[IPOPT_OPTVAL];
315 
316 		if (opt == IPOPT_EOL)
317 			break;
318 		if (opt == IPOPT_NOP)
319 			optlen = 1;
320 		else {
321 			optlen = cp[IPOPT_OLEN];
322 			if (optlen <= 0 || optlen > x)
323 				return 0; /* invalid or truncated */
324 		}
325 		switch (opt) {
326 		default:
327 			break;
328 
329 		case IPOPT_LSRR:
330 			bits |= IP_FW_IPOPT_LSRR;
331 			break;
332 
333 		case IPOPT_SSRR:
334 			bits |= IP_FW_IPOPT_SSRR;
335 			break;
336 
337 		case IPOPT_RR:
338 			bits |= IP_FW_IPOPT_RR;
339 			break;
340 
341 		case IPOPT_TS:
342 			bits |= IP_FW_IPOPT_TS;
343 			break;
344 		}
345 	}
346 	return (flags_match(cmd, bits));
347 }
348 
349 /*
350  * Parse TCP options. The logic copied from tcp_dooptions().
351  */
352 static int
353 tcpopts_parse(const struct tcphdr *tcp, uint16_t *mss)
354 {
355 	const u_char *cp = (const u_char *)(tcp + 1);
356 	int optlen, bits = 0;
357 	int cnt = (tcp->th_off << 2) - sizeof(struct tcphdr);
358 
359 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
360 		int opt = cp[0];
361 		if (opt == TCPOPT_EOL)
362 			break;
363 		if (opt == TCPOPT_NOP)
364 			optlen = 1;
365 		else {
366 			if (cnt < 2)
367 				break;
368 			optlen = cp[1];
369 			if (optlen < 2 || optlen > cnt)
370 				break;
371 		}
372 
373 		switch (opt) {
374 		default:
375 			break;
376 
377 		case TCPOPT_MAXSEG:
378 			if (optlen != TCPOLEN_MAXSEG)
379 				break;
380 			bits |= IP_FW_TCPOPT_MSS;
381 			if (mss != NULL)
382 				*mss = be16dec(cp + 2);
383 			break;
384 
385 		case TCPOPT_WINDOW:
386 			if (optlen == TCPOLEN_WINDOW)
387 				bits |= IP_FW_TCPOPT_WINDOW;
388 			break;
389 
390 		case TCPOPT_SACK_PERMITTED:
391 			if (optlen == TCPOLEN_SACK_PERMITTED)
392 				bits |= IP_FW_TCPOPT_SACK;
393 			break;
394 
395 		case TCPOPT_SACK:
396 			if (optlen > 2 && (optlen - 2) % TCPOLEN_SACK == 0)
397 				bits |= IP_FW_TCPOPT_SACK;
398 			break;
399 
400 		case TCPOPT_TIMESTAMP:
401 			if (optlen == TCPOLEN_TIMESTAMP)
402 				bits |= IP_FW_TCPOPT_TS;
403 			break;
404 		}
405 	}
406 	return (bits);
407 }
408 
409 static int
410 tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd)
411 {
412 
413 	return (flags_match(cmd, tcpopts_parse(tcp, NULL)));
414 }
415 
416 static int
417 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd, struct ip_fw_chain *chain,
418     uint32_t *tablearg)
419 {
420 
421 	if (ifp == NULL)	/* no iface with this packet, match fails */
422 		return (0);
423 
424 	/* Check by name or by IP address */
425 	if (cmd->name[0] != '\0') { /* match by name */
426 		if (cmd->name[0] == '\1') /* use tablearg to match */
427 			return ipfw_lookup_table(chain, cmd->p.kidx, 0,
428 			    &ifp->if_index, tablearg);
429 		/* Check name */
430 		if (cmd->p.glob) {
431 			if (fnmatch(cmd->name, ifp->if_xname, 0) == 0)
432 				return(1);
433 		} else {
434 			if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
435 				return(1);
436 		}
437 	} else {
438 #if !defined(USERSPACE) && defined(__FreeBSD__)	/* and OSX too ? */
439 		struct ifaddr *ia;
440 
441 		NET_EPOCH_ASSERT();
442 
443 		CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
444 			if (ia->ifa_addr->sa_family != AF_INET)
445 				continue;
446 			if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
447 			    (ia->ifa_addr))->sin_addr.s_addr)
448 				return (1);	/* match */
449 		}
450 #endif /* __FreeBSD__ */
451 	}
452 	return(0);	/* no match, fail ... */
453 }
454 
455 /*
456  * The verify_path function checks if a route to the src exists and
457  * if it is reachable via ifp (when provided).
458  *
459  * The 'verrevpath' option checks that the interface that an IP packet
460  * arrives on is the same interface that traffic destined for the
461  * packet's source address would be routed out of.
462  * The 'versrcreach' option just checks that the source address is
463  * reachable via any route (except default) in the routing table.
464  * These two are a measure to block forged packets. This is also
465  * commonly known as "anti-spoofing" or Unicast Reverse Path
466  * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs
467  * is purposely reminiscent of the Cisco IOS command,
468  *
469  *   ip verify unicast reverse-path
470  *   ip verify unicast source reachable-via any
471  *
472  * which implements the same functionality. But note that the syntax
473  * is misleading, and the check may be performed on all IP packets
474  * whether unicast, multicast, or broadcast.
475  */
476 static int
477 verify_path(struct in_addr src, struct ifnet *ifp, u_int fib)
478 {
479 #if defined(USERSPACE) || !defined(__FreeBSD__)
480 	return 0;
481 #else
482 	struct nhop_object *nh;
483 
484 	nh = fib4_lookup(fib, src, 0, NHR_NONE, 0);
485 	if (nh == NULL)
486 		return (0);
487 
488 	/*
489 	 * If ifp is provided, check for equality with rtentry.
490 	 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
491 	 * in order to pass packets injected back by if_simloop():
492 	 * routing entry (via lo0) for our own address
493 	 * may exist, so we need to handle routing assymetry.
494 	 */
495 	if (ifp != NULL && ifp != nh->nh_aifp)
496 		return (0);
497 
498 	/* if no ifp provided, check if rtentry is not default route */
499 	if (ifp == NULL && (nh->nh_flags & NHF_DEFAULT) != 0)
500 		return (0);
501 
502 	/* or if this is a blackhole/reject route */
503 	if (ifp == NULL && (nh->nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
504 		return (0);
505 
506 	/* found valid route */
507 	return 1;
508 #endif /* __FreeBSD__ */
509 }
510 
511 /*
512  * Generate an SCTP packet containing an ABORT chunk. The verification tag
513  * is given by vtag. The T-bit is set in the ABORT chunk if and only if
514  * reflected is not 0.
515  */
516 
517 static struct mbuf *
518 ipfw_send_abort(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t vtag,
519     int reflected)
520 {
521 	struct mbuf *m;
522 	struct ip *ip;
523 #ifdef INET6
524 	struct ip6_hdr *ip6;
525 #endif
526 	struct sctphdr *sctp;
527 	struct sctp_chunkhdr *chunk;
528 	u_int16_t hlen, plen, tlen;
529 
530 	MGETHDR(m, M_NOWAIT, MT_DATA);
531 	if (m == NULL)
532 		return (NULL);
533 
534 	M_SETFIB(m, id->fib);
535 #ifdef MAC
536 	if (replyto != NULL)
537 		mac_netinet_firewall_reply(replyto, m);
538 	else
539 		mac_netinet_firewall_send(m);
540 #else
541 	(void)replyto;		/* don't warn about unused arg */
542 #endif
543 
544 	switch (id->addr_type) {
545 	case 4:
546 		hlen = sizeof(struct ip);
547 		break;
548 #ifdef INET6
549 	case 6:
550 		hlen = sizeof(struct ip6_hdr);
551 		break;
552 #endif
553 	default:
554 		/* XXX: log me?!? */
555 		FREE_PKT(m);
556 		return (NULL);
557 	}
558 	plen = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
559 	tlen = hlen + plen;
560 	m->m_data += max_linkhdr;
561 	m->m_flags |= M_SKIP_FIREWALL;
562 	m->m_pkthdr.len = m->m_len = tlen;
563 	m->m_pkthdr.rcvif = NULL;
564 	bzero(m->m_data, tlen);
565 
566 	switch (id->addr_type) {
567 	case 4:
568 		ip = mtod(m, struct ip *);
569 
570 		ip->ip_v = 4;
571 		ip->ip_hl = sizeof(struct ip) >> 2;
572 		ip->ip_tos = IPTOS_LOWDELAY;
573 		ip->ip_len = htons(tlen);
574 		ip->ip_id = htons(0);
575 		ip->ip_off = htons(0);
576 		ip->ip_ttl = V_ip_defttl;
577 		ip->ip_p = IPPROTO_SCTP;
578 		ip->ip_sum = 0;
579 		ip->ip_src.s_addr = htonl(id->dst_ip);
580 		ip->ip_dst.s_addr = htonl(id->src_ip);
581 
582 		sctp = (struct sctphdr *)(ip + 1);
583 		break;
584 #ifdef INET6
585 	case 6:
586 		ip6 = mtod(m, struct ip6_hdr *);
587 
588 		ip6->ip6_vfc = IPV6_VERSION;
589 		ip6->ip6_plen = htons(plen);
590 		ip6->ip6_nxt = IPPROTO_SCTP;
591 		ip6->ip6_hlim = IPV6_DEFHLIM;
592 		ip6->ip6_src = id->dst_ip6;
593 		ip6->ip6_dst = id->src_ip6;
594 
595 		sctp = (struct sctphdr *)(ip6 + 1);
596 		break;
597 #endif
598 	}
599 
600 	sctp->src_port = htons(id->dst_port);
601 	sctp->dest_port = htons(id->src_port);
602 	sctp->v_tag = htonl(vtag);
603 	sctp->checksum = htonl(0);
604 
605 	chunk = (struct sctp_chunkhdr *)(sctp + 1);
606 	chunk->chunk_type = SCTP_ABORT_ASSOCIATION;
607 	chunk->chunk_flags = 0;
608 	if (reflected != 0) {
609 		chunk->chunk_flags |= SCTP_HAD_NO_TCB;
610 	}
611 	chunk->chunk_length = htons(sizeof(struct sctp_chunkhdr));
612 
613 	sctp->checksum = sctp_calculate_cksum(m, hlen);
614 
615 	return (m);
616 }
617 
618 /*
619  * Generate a TCP packet, containing either a RST or a keepalive.
620  * When flags & TH_RST, we are sending a RST packet, because of a
621  * "reset" action matched the packet.
622  * Otherwise we are sending a keepalive, and flags & TH_
623  * The 'replyto' mbuf is the mbuf being replied to, if any, and is required
624  * so that MAC can label the reply appropriately.
625  */
626 struct mbuf *
627 ipfw_send_pkt(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t seq,
628     u_int32_t ack, int flags)
629 {
630 	struct mbuf *m = NULL;		/* stupid compiler */
631 	struct ip *h = NULL;		/* stupid compiler */
632 #ifdef INET6
633 	struct ip6_hdr *h6 = NULL;
634 #endif
635 	struct tcphdr *th = NULL;
636 	int len, dir;
637 
638 	MGETHDR(m, M_NOWAIT, MT_DATA);
639 	if (m == NULL)
640 		return (NULL);
641 
642 	M_SETFIB(m, id->fib);
643 #ifdef MAC
644 	if (replyto != NULL)
645 		mac_netinet_firewall_reply(replyto, m);
646 	else
647 		mac_netinet_firewall_send(m);
648 #else
649 	(void)replyto;		/* don't warn about unused arg */
650 #endif
651 
652 	switch (id->addr_type) {
653 	case 4:
654 		len = sizeof(struct ip) + sizeof(struct tcphdr);
655 		break;
656 #ifdef INET6
657 	case 6:
658 		len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
659 		break;
660 #endif
661 	default:
662 		/* XXX: log me?!? */
663 		FREE_PKT(m);
664 		return (NULL);
665 	}
666 	dir = ((flags & (TH_SYN | TH_RST)) == TH_SYN);
667 
668 	m->m_data += max_linkhdr;
669 	m->m_flags |= M_SKIP_FIREWALL;
670 	m->m_pkthdr.len = m->m_len = len;
671 	m->m_pkthdr.rcvif = NULL;
672 	bzero(m->m_data, len);
673 
674 	switch (id->addr_type) {
675 	case 4:
676 		h = mtod(m, struct ip *);
677 
678 		/* prepare for checksum */
679 		h->ip_p = IPPROTO_TCP;
680 		h->ip_len = htons(sizeof(struct tcphdr));
681 		if (dir) {
682 			h->ip_src.s_addr = htonl(id->src_ip);
683 			h->ip_dst.s_addr = htonl(id->dst_ip);
684 		} else {
685 			h->ip_src.s_addr = htonl(id->dst_ip);
686 			h->ip_dst.s_addr = htonl(id->src_ip);
687 		}
688 
689 		th = (struct tcphdr *)(h + 1);
690 		break;
691 #ifdef INET6
692 	case 6:
693 		h6 = mtod(m, struct ip6_hdr *);
694 
695 		/* prepare for checksum */
696 		h6->ip6_nxt = IPPROTO_TCP;
697 		h6->ip6_plen = htons(sizeof(struct tcphdr));
698 		if (dir) {
699 			h6->ip6_src = id->src_ip6;
700 			h6->ip6_dst = id->dst_ip6;
701 		} else {
702 			h6->ip6_src = id->dst_ip6;
703 			h6->ip6_dst = id->src_ip6;
704 		}
705 
706 		th = (struct tcphdr *)(h6 + 1);
707 		break;
708 #endif
709 	}
710 
711 	if (dir) {
712 		th->th_sport = htons(id->src_port);
713 		th->th_dport = htons(id->dst_port);
714 	} else {
715 		th->th_sport = htons(id->dst_port);
716 		th->th_dport = htons(id->src_port);
717 	}
718 	th->th_off = sizeof(struct tcphdr) >> 2;
719 
720 	if (flags & TH_RST) {
721 		if (flags & TH_ACK) {
722 			th->th_seq = htonl(ack);
723 			th->th_flags = TH_RST;
724 		} else {
725 			if (flags & TH_SYN)
726 				seq++;
727 			th->th_ack = htonl(seq);
728 			th->th_flags = TH_RST | TH_ACK;
729 		}
730 	} else {
731 		/*
732 		 * Keepalive - use caller provided sequence numbers
733 		 */
734 		th->th_seq = htonl(seq);
735 		th->th_ack = htonl(ack);
736 		th->th_flags = TH_ACK;
737 	}
738 
739 	switch (id->addr_type) {
740 	case 4:
741 		th->th_sum = in_cksum(m, len);
742 
743 		/* finish the ip header */
744 		h->ip_v = 4;
745 		h->ip_hl = sizeof(*h) >> 2;
746 		h->ip_tos = IPTOS_LOWDELAY;
747 		h->ip_off = htons(0);
748 		h->ip_len = htons(len);
749 		h->ip_ttl = V_ip_defttl;
750 		h->ip_sum = 0;
751 		break;
752 #ifdef INET6
753 	case 6:
754 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(*h6),
755 		    sizeof(struct tcphdr));
756 
757 		/* finish the ip6 header */
758 		h6->ip6_vfc |= IPV6_VERSION;
759 		h6->ip6_hlim = IPV6_DEFHLIM;
760 		break;
761 #endif
762 	}
763 
764 	return (m);
765 }
766 
767 #ifdef INET6
768 /*
769  * ipv6 specific rules here...
770  */
771 static __inline int
772 icmp6type_match(int type, ipfw_insn_u32 *cmd)
773 {
774 	return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) );
775 }
776 
777 static int
778 flow6id_match(int curr_flow, ipfw_insn_u32 *cmd)
779 {
780 	int i;
781 	for (i=0; i <= cmd->o.arg1; ++i)
782 		if (curr_flow == cmd->d[i])
783 			return 1;
784 	return 0;
785 }
786 
787 /* support for IP6_*_ME opcodes */
788 static const struct in6_addr lla_mask = {{{
789 	0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
790 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
791 }}};
792 
793 static int
794 ipfw_localip6(struct in6_addr *in6)
795 {
796 	struct rm_priotracker in6_ifa_tracker;
797 	struct in6_ifaddr *ia;
798 
799 	if (IN6_IS_ADDR_MULTICAST(in6))
800 		return (0);
801 
802 	if (!IN6_IS_ADDR_LINKLOCAL(in6))
803 		return (in6_localip(in6));
804 
805 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
806 	CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
807 		if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr))
808 			continue;
809 		if (IN6_ARE_MASKED_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
810 		    in6, &lla_mask)) {
811 			IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
812 			return (1);
813 		}
814 	}
815 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
816 	return (0);
817 }
818 
819 static int
820 verify_path6(struct in6_addr *src, struct ifnet *ifp, u_int fib)
821 {
822 	struct nhop_object *nh;
823 
824 	if (IN6_IS_SCOPE_LINKLOCAL(src))
825 		return (1);
826 
827 	nh = fib6_lookup(fib, src, 0, NHR_NONE, 0);
828 	if (nh == NULL)
829 		return (0);
830 
831 	/* If ifp is provided, check for equality with route table. */
832 	if (ifp != NULL && ifp != nh->nh_aifp)
833 		return (0);
834 
835 	/* if no ifp provided, check if rtentry is not default route */
836 	if (ifp == NULL && (nh->nh_flags & NHF_DEFAULT) != 0)
837 		return (0);
838 
839 	/* or if this is a blackhole/reject route */
840 	if (ifp == NULL && (nh->nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
841 		return (0);
842 
843 	/* found valid route */
844 	return 1;
845 }
846 
847 static int
848 is_icmp6_query(int icmp6_type)
849 {
850 	if ((icmp6_type <= ICMP6_MAXTYPE) &&
851 	    (icmp6_type == ICMP6_ECHO_REQUEST ||
852 	    icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
853 	    icmp6_type == ICMP6_WRUREQUEST ||
854 	    icmp6_type == ICMP6_FQDN_QUERY ||
855 	    icmp6_type == ICMP6_NI_QUERY))
856 		return (1);
857 
858 	return (0);
859 }
860 
861 static int
862 map_icmp_unreach(int code)
863 {
864 
865 	/* RFC 7915 p4.2 */
866 	switch (code) {
867 	case ICMP_UNREACH_NET:
868 	case ICMP_UNREACH_HOST:
869 	case ICMP_UNREACH_SRCFAIL:
870 	case ICMP_UNREACH_NET_UNKNOWN:
871 	case ICMP_UNREACH_HOST_UNKNOWN:
872 	case ICMP_UNREACH_TOSNET:
873 	case ICMP_UNREACH_TOSHOST:
874 		return (ICMP6_DST_UNREACH_NOROUTE);
875 	case ICMP_UNREACH_PORT:
876 		return (ICMP6_DST_UNREACH_NOPORT);
877 	default:
878 		/*
879 		 * Map the rest of codes into admit prohibited.
880 		 * XXX: unreach proto should be mapped into ICMPv6
881 		 * parameter problem, but we use only unreach type.
882 		 */
883 		return (ICMP6_DST_UNREACH_ADMIN);
884 	}
885 }
886 
887 static void
888 send_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6)
889 {
890 	struct mbuf *m;
891 
892 	m = args->m;
893 	if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) {
894 		struct tcphdr *tcp;
895 		tcp = (struct tcphdr *)((char *)ip6 + hlen);
896 
897 		if ((tcp->th_flags & TH_RST) == 0) {
898 			struct mbuf *m0;
899 			m0 = ipfw_send_pkt(args->m, &(args->f_id),
900 			    ntohl(tcp->th_seq), ntohl(tcp->th_ack),
901 			    tcp->th_flags | TH_RST);
902 			if (m0 != NULL)
903 				ip6_output(m0, NULL, NULL, 0, NULL, NULL,
904 				    NULL);
905 		}
906 		FREE_PKT(m);
907 	} else if (code == ICMP6_UNREACH_ABORT &&
908 	    args->f_id.proto == IPPROTO_SCTP) {
909 		struct mbuf *m0;
910 		struct sctphdr *sctp;
911 		u_int32_t v_tag;
912 		int reflected;
913 
914 		sctp = (struct sctphdr *)((char *)ip6 + hlen);
915 		reflected = 1;
916 		v_tag = ntohl(sctp->v_tag);
917 		/* Investigate the first chunk header if available */
918 		if (m->m_len >= hlen + sizeof(struct sctphdr) +
919 		    sizeof(struct sctp_chunkhdr)) {
920 			struct sctp_chunkhdr *chunk;
921 
922 			chunk = (struct sctp_chunkhdr *)(sctp + 1);
923 			switch (chunk->chunk_type) {
924 			case SCTP_INITIATION:
925 				/*
926 				 * Packets containing an INIT chunk MUST have
927 				 * a zero v-tag.
928 				 */
929 				if (v_tag != 0) {
930 					v_tag = 0;
931 					break;
932 				}
933 				/* INIT chunk MUST NOT be bundled */
934 				if (m->m_pkthdr.len >
935 				    hlen + sizeof(struct sctphdr) +
936 				    ntohs(chunk->chunk_length) + 3) {
937 					break;
938 				}
939 				/* Use the initiate tag if available */
940 				if ((m->m_len >= hlen + sizeof(struct sctphdr) +
941 				    sizeof(struct sctp_chunkhdr) +
942 				    offsetof(struct sctp_init, a_rwnd))) {
943 					struct sctp_init *init;
944 
945 					init = (struct sctp_init *)(chunk + 1);
946 					v_tag = ntohl(init->initiate_tag);
947 					reflected = 0;
948 				}
949 				break;
950 			case SCTP_ABORT_ASSOCIATION:
951 				/*
952 				 * If the packet contains an ABORT chunk, don't
953 				 * reply.
954 				 * XXX: We should search through all chunks,
955 				 * but do not do that to avoid attacks.
956 				 */
957 				v_tag = 0;
958 				break;
959 			}
960 		}
961 		if (v_tag == 0) {
962 			m0 = NULL;
963 		} else {
964 			m0 = ipfw_send_abort(args->m, &(args->f_id), v_tag,
965 			    reflected);
966 		}
967 		if (m0 != NULL)
968 			ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
969 		FREE_PKT(m);
970 	} else if (code != ICMP6_UNREACH_RST && code != ICMP6_UNREACH_ABORT) {
971 		/* Send an ICMPv6 unreach. */
972 #if 0
973 		/*
974 		 * Unlike above, the mbufs need to line up with the ip6 hdr,
975 		 * as the contents are read. We need to m_adj() the
976 		 * needed amount.
977 		 * The mbuf will however be thrown away so we can adjust it.
978 		 * Remember we did an m_pullup on it already so we
979 		 * can make some assumptions about contiguousness.
980 		 */
981 		if (args->L3offset)
982 			m_adj(m, args->L3offset);
983 #endif
984 		icmp6_error(m, ICMP6_DST_UNREACH, code, 0);
985 	} else
986 		FREE_PKT(m);
987 
988 	args->m = NULL;
989 }
990 
991 #endif /* INET6 */
992 
993 /*
994  * sends a reject message, consuming the mbuf passed as an argument.
995  */
996 static void
997 send_reject(struct ip_fw_args *args, const ipfw_insn *cmd, int iplen,
998     struct ip *ip)
999 {
1000 	int code, mtu;
1001 
1002 	code = cmd->arg1;
1003 	if (code == ICMP_UNREACH_NEEDFRAG &&
1004 	    cmd->len == F_INSN_SIZE(ipfw_insn_u16))
1005 		mtu = ((const ipfw_insn_u16 *)cmd)->ports[0];
1006 	else
1007 		mtu = 0;
1008 
1009 #if 0
1010 	/* XXX When ip is not guaranteed to be at mtod() we will
1011 	 * need to account for this */
1012 	 * The mbuf will however be thrown away so we can adjust it.
1013 	 * Remember we did an m_pullup on it already so we
1014 	 * can make some assumptions about contiguousness.
1015 	 */
1016 	if (args->L3offset)
1017 		m_adj(m, args->L3offset);
1018 #endif
1019 	if (code != ICMP_REJECT_RST && code != ICMP_REJECT_ABORT) {
1020 		/* Send an ICMP unreach */
1021 		icmp_error(args->m, ICMP_UNREACH, code, 0L, mtu);
1022 	} else if (code == ICMP_REJECT_RST && args->f_id.proto == IPPROTO_TCP) {
1023 		struct tcphdr *const tcp =
1024 		    L3HDR(struct tcphdr, mtod(args->m, struct ip *));
1025 		if ( (tcp->th_flags & TH_RST) == 0) {
1026 			struct mbuf *m;
1027 			m = ipfw_send_pkt(args->m, &(args->f_id),
1028 				ntohl(tcp->th_seq), ntohl(tcp->th_ack),
1029 				tcp->th_flags | TH_RST);
1030 			if (m != NULL)
1031 				ip_output(m, NULL, NULL, 0, NULL, NULL);
1032 		}
1033 		FREE_PKT(args->m);
1034 	} else if (code == ICMP_REJECT_ABORT &&
1035 	    args->f_id.proto == IPPROTO_SCTP) {
1036 		struct mbuf *m;
1037 		struct sctphdr *sctp;
1038 		struct sctp_chunkhdr *chunk;
1039 		struct sctp_init *init;
1040 		u_int32_t v_tag;
1041 		int reflected;
1042 
1043 		sctp = L3HDR(struct sctphdr, mtod(args->m, struct ip *));
1044 		reflected = 1;
1045 		v_tag = ntohl(sctp->v_tag);
1046 		if (iplen >= (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1047 		    sizeof(struct sctp_chunkhdr)) {
1048 			/* Look at the first chunk header if available */
1049 			chunk = (struct sctp_chunkhdr *)(sctp + 1);
1050 			switch (chunk->chunk_type) {
1051 			case SCTP_INITIATION:
1052 				/*
1053 				 * Packets containing an INIT chunk MUST have
1054 				 * a zero v-tag.
1055 				 */
1056 				if (v_tag != 0) {
1057 					v_tag = 0;
1058 					break;
1059 				}
1060 				/* INIT chunk MUST NOT be bundled */
1061 				if (iplen >
1062 				    (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1063 				    ntohs(chunk->chunk_length) + 3) {
1064 					break;
1065 				}
1066 				/* Use the initiate tag if available */
1067 				if ((iplen >= (ip->ip_hl << 2) +
1068 				    sizeof(struct sctphdr) +
1069 				    sizeof(struct sctp_chunkhdr) +
1070 				    offsetof(struct sctp_init, a_rwnd))) {
1071 					init = (struct sctp_init *)(chunk + 1);
1072 					v_tag = ntohl(init->initiate_tag);
1073 					reflected = 0;
1074 				}
1075 				break;
1076 			case SCTP_ABORT_ASSOCIATION:
1077 				/*
1078 				 * If the packet contains an ABORT chunk, don't
1079 				 * reply.
1080 				 * XXX: We should search through all chunks,
1081 				 * but do not do that to avoid attacks.
1082 				 */
1083 				v_tag = 0;
1084 				break;
1085 			}
1086 		}
1087 		if (v_tag == 0) {
1088 			m = NULL;
1089 		} else {
1090 			m = ipfw_send_abort(args->m, &(args->f_id), v_tag,
1091 			    reflected);
1092 		}
1093 		if (m != NULL)
1094 			ip_output(m, NULL, NULL, 0, NULL, NULL);
1095 		FREE_PKT(args->m);
1096 	} else
1097 		FREE_PKT(args->m);
1098 	args->m = NULL;
1099 }
1100 
1101 /*
1102  * Support for uid/gid/jail lookup. These tests are expensive
1103  * (because we may need to look into the list of active sockets)
1104  * so we cache the results. ugid_lookupp is 0 if we have not
1105  * yet done a lookup, 1 if we succeeded, and -1 if we tried
1106  * and failed. The function always returns the match value.
1107  * We could actually spare the variable and use *uc, setting
1108  * it to '(void *)check_uidgid if we have no info, NULL if
1109  * we tried and failed, or any other value if successful.
1110  */
1111 static int
1112 check_uidgid(ipfw_insn_u32 *insn, struct ip_fw_args *args, int *ugid_lookupp,
1113     struct ucred **uc)
1114 {
1115 #if defined(USERSPACE)
1116 	return 0;	// not supported in userspace
1117 #else
1118 #ifndef __FreeBSD__
1119 	/* XXX */
1120 	return cred_check(insn, proto, oif,
1121 	    dst_ip, dst_port, src_ip, src_port,
1122 	    (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb);
1123 #else  /* FreeBSD */
1124 	struct in_addr src_ip, dst_ip;
1125 	struct inpcbinfo *pi;
1126 	struct ipfw_flow_id *id;
1127 	struct inpcb *pcb, *inp;
1128 	int lookupflags;
1129 	int match;
1130 
1131 	id = &args->f_id;
1132 	inp = args->inp;
1133 
1134 	/*
1135 	 * Check to see if the UDP or TCP stack supplied us with
1136 	 * the PCB. If so, rather then holding a lock and looking
1137 	 * up the PCB, we can use the one that was supplied.
1138 	 */
1139 	if (inp && *ugid_lookupp == 0) {
1140 		INP_LOCK_ASSERT(inp);
1141 		if (inp->inp_socket != NULL) {
1142 			*uc = crhold(inp->inp_cred);
1143 			*ugid_lookupp = 1;
1144 		} else
1145 			*ugid_lookupp = -1;
1146 	}
1147 	/*
1148 	 * If we have already been here and the packet has no
1149 	 * PCB entry associated with it, then we can safely
1150 	 * assume that this is a no match.
1151 	 */
1152 	if (*ugid_lookupp == -1)
1153 		return (0);
1154 	if (id->proto == IPPROTO_TCP) {
1155 		lookupflags = 0;
1156 		pi = &V_tcbinfo;
1157 	} else if (id->proto == IPPROTO_UDP) {
1158 		lookupflags = INPLOOKUP_WILDCARD;
1159 		pi = &V_udbinfo;
1160 	} else if (id->proto == IPPROTO_UDPLITE) {
1161 		lookupflags = INPLOOKUP_WILDCARD;
1162 		pi = &V_ulitecbinfo;
1163 	} else
1164 		return 0;
1165 	lookupflags |= INPLOOKUP_RLOCKPCB;
1166 	match = 0;
1167 	if (*ugid_lookupp == 0) {
1168 		if (id->addr_type == 6) {
1169 #ifdef INET6
1170 			if (args->flags & IPFW_ARGS_IN)
1171 				pcb = in6_pcblookup_mbuf(pi,
1172 				    &id->src_ip6, htons(id->src_port),
1173 				    &id->dst_ip6, htons(id->dst_port),
1174 				    lookupflags, NULL, args->m);
1175 			else
1176 				pcb = in6_pcblookup_mbuf(pi,
1177 				    &id->dst_ip6, htons(id->dst_port),
1178 				    &id->src_ip6, htons(id->src_port),
1179 				    lookupflags, args->ifp, args->m);
1180 #else
1181 			*ugid_lookupp = -1;
1182 			return (0);
1183 #endif
1184 		} else {
1185 			src_ip.s_addr = htonl(id->src_ip);
1186 			dst_ip.s_addr = htonl(id->dst_ip);
1187 			if (args->flags & IPFW_ARGS_IN)
1188 				pcb = in_pcblookup_mbuf(pi,
1189 				    src_ip, htons(id->src_port),
1190 				    dst_ip, htons(id->dst_port),
1191 				    lookupflags, NULL, args->m);
1192 			else
1193 				pcb = in_pcblookup_mbuf(pi,
1194 				    dst_ip, htons(id->dst_port),
1195 				    src_ip, htons(id->src_port),
1196 				    lookupflags, args->ifp, args->m);
1197 		}
1198 		if (pcb != NULL) {
1199 			INP_RLOCK_ASSERT(pcb);
1200 			*uc = crhold(pcb->inp_cred);
1201 			*ugid_lookupp = 1;
1202 			INP_RUNLOCK(pcb);
1203 		}
1204 		if (*ugid_lookupp == 0) {
1205 			/*
1206 			 * We tried and failed, set the variable to -1
1207 			 * so we will not try again on this packet.
1208 			 */
1209 			*ugid_lookupp = -1;
1210 			return (0);
1211 		}
1212 	}
1213 	if (insn->o.opcode == O_UID)
1214 		match = ((*uc)->cr_uid == (uid_t)insn->d[0]);
1215 	else if (insn->o.opcode == O_GID)
1216 		match = groupmember((gid_t)insn->d[0], *uc);
1217 	else if (insn->o.opcode == O_JAIL)
1218 		match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]);
1219 	return (match);
1220 #endif /* __FreeBSD__ */
1221 #endif /* not supported in userspace */
1222 }
1223 
1224 /*
1225  * Helper function to set args with info on the rule after the matching
1226  * one. slot is precise, whereas we guess rule_id as they are
1227  * assigned sequentially.
1228  */
1229 static inline void
1230 set_match(struct ip_fw_args *args, int slot,
1231 	struct ip_fw_chain *chain)
1232 {
1233 	args->rule.chain_id = chain->id;
1234 	args->rule.slot = slot + 1; /* we use 0 as a marker */
1235 	args->rule.rule_id = 1 + chain->map[slot]->id;
1236 	args->rule.rulenum = chain->map[slot]->rulenum;
1237 	args->flags |= IPFW_ARGS_REF;
1238 }
1239 
1240 static int
1241 jump_lookup_pos(struct ip_fw_chain *chain, struct ip_fw *f, int num,
1242     int tablearg, int jump_backwards)
1243 {
1244 	int f_pos, i;
1245 
1246 	i = IP_FW_ARG_TABLEARG(chain, num, skipto);
1247 	/* make sure we do not jump backward */
1248 	if (jump_backwards == 0 && i <= f->rulenum)
1249 		i = f->rulenum + 1;
1250 
1251 #ifndef LINEAR_SKIPTO
1252 	if (chain->idxmap != NULL)
1253 		f_pos = chain->idxmap[i];
1254 	else
1255 		f_pos = ipfw_find_rule(chain, i, 0);
1256 #else
1257 	f_pos = chain->idxmap[i];
1258 #endif /* LINEAR_SKIPTO */
1259 
1260 	return (f_pos);
1261 }
1262 
1263 
1264 #ifndef LINEAR_SKIPTO
1265 /*
1266  * Helper function to enable cached rule lookups using
1267  * cache.id and cache.pos fields in ipfw rule.
1268  */
1269 static int
1270 jump_cached(struct ip_fw_chain *chain, struct ip_fw *f, int num,
1271     int tablearg, int jump_backwards)
1272 {
1273 	int f_pos;
1274 
1275 	/* Can't use cache with IP_FW_TARG */
1276 	if (num == IP_FW_TARG)
1277 		return jump_lookup_pos(chain, f, num, tablearg, jump_backwards);
1278 
1279 	/*
1280 	 * If possible use cached f_pos (in f->cache.pos),
1281 	 * whose version is written in f->cache.id (horrible hacks
1282 	 * to avoid changing the ABI).
1283 	 *
1284 	 * Multiple threads can execute the same rule simultaneously,
1285 	 * we need to ensure that cache.pos is updated before cache.id.
1286 	 */
1287 
1288 #ifdef __LP64__
1289 	struct ip_fw_jump_cache cache;
1290 
1291 	cache.raw_value = f->cache.raw_value;
1292 	if (cache.id == chain->id)
1293 		return (cache.pos);
1294 
1295 	f_pos = jump_lookup_pos(chain, f, num, tablearg, jump_backwards);
1296 
1297 	cache.pos = f_pos;
1298 	cache.id = chain->id;
1299 	f->cache.raw_value = cache.raw_value;
1300 #else
1301 	if (f->cache.id == chain->id) {
1302 		/* Load pos after id */
1303 		atomic_thread_fence_acq();
1304 		return (f->cache.pos);
1305 	}
1306 
1307 	f_pos = jump_lookup_pos(chain, f, num, tablearg, jump_backwards);
1308 
1309 	f->cache.pos = f_pos;
1310 	/* Store id after pos */
1311 	atomic_thread_fence_rel();
1312 	f->cache.id = chain->id;
1313 #endif /* !__LP64__ */
1314 	return (f_pos);
1315 }
1316 #endif /* !LINEAR_SKIPTO */
1317 
1318 #define	TARG(k, f)	IP_FW_ARG_TABLEARG(chain, k, f)
1319 /*
1320  * The main check routine for the firewall.
1321  *
1322  * All arguments are in args so we can modify them and return them
1323  * back to the caller.
1324  *
1325  * Parameters:
1326  *
1327  *	args->m	(in/out) The packet; we set to NULL when/if we nuke it.
1328  *		Starts with the IP header.
1329  *	args->L3offset	Number of bytes bypassed if we came from L2.
1330  *			e.g. often sizeof(eh)  ** NOTYET **
1331  *	args->ifp	Incoming or outgoing interface.
1332  *	args->divert_rule (in/out)
1333  *		Skip up to the first rule past this rule number;
1334  *		upon return, non-zero port number for divert or tee.
1335  *
1336  *	args->rule	Pointer to the last matching rule (in/out)
1337  *	args->next_hop	Socket we are forwarding to (out).
1338  *	args->next_hop6	IPv6 next hop we are forwarding to (out).
1339  *	args->f_id	Addresses grabbed from the packet (out)
1340  * 	args->rule.info	a cookie depending on rule action
1341  *
1342  * Return value:
1343  *
1344  *	IP_FW_PASS	the packet must be accepted
1345  *	IP_FW_DENY	the packet must be dropped
1346  *	IP_FW_DIVERT	divert packet, port in m_tag
1347  *	IP_FW_TEE	tee packet, port in m_tag
1348  *	IP_FW_DUMMYNET	to dummynet, pipe in args->cookie
1349  *	IP_FW_NETGRAPH	into netgraph, cookie args->cookie
1350  *		args->rule contains the matching rule,
1351  *		args->rule.info has additional information.
1352  *
1353  */
1354 int
1355 ipfw_chk(struct ip_fw_args *args)
1356 {
1357 
1358 	/*
1359 	 * Local variables holding state while processing a packet:
1360 	 *
1361 	 * IMPORTANT NOTE: to speed up the processing of rules, there
1362 	 * are some assumption on the values of the variables, which
1363 	 * are documented here. Should you change them, please check
1364 	 * the implementation of the various instructions to make sure
1365 	 * that they still work.
1366 	 *
1367 	 * m | args->m	Pointer to the mbuf, as received from the caller.
1368 	 *	It may change if ipfw_chk() does an m_pullup, or if it
1369 	 *	consumes the packet because it calls send_reject().
1370 	 *	XXX This has to change, so that ipfw_chk() never modifies
1371 	 *	or consumes the buffer.
1372 	 *	OR
1373 	 * args->mem	Pointer to contigous memory chunk.
1374 	 * ip	Is the beginning of the ip(4 or 6) header.
1375 	 * eh	Ethernet header in case if input is Layer2.
1376 	 */
1377 	struct mbuf *m;
1378 	struct ip *ip;
1379 	struct ether_header *eh;
1380 
1381 	/*
1382 	 * For rules which contain uid/gid or jail constraints, cache
1383 	 * a copy of the users credentials after the pcb lookup has been
1384 	 * executed. This will speed up the processing of rules with
1385 	 * these types of constraints, as well as decrease contention
1386 	 * on pcb related locks.
1387 	 */
1388 #ifndef __FreeBSD__
1389 	struct bsd_ucred ucred_cache;
1390 #else
1391 	struct ucred *ucred_cache = NULL;
1392 #endif
1393 	int ucred_lookup = 0;
1394 	int f_pos = 0;		/* index of current rule in the array */
1395 	int retval = 0;
1396 	struct ifnet *oif, *iif;
1397 
1398 	/*
1399 	 * hlen	The length of the IP header.
1400 	 */
1401 	u_int hlen = 0;		/* hlen >0 means we have an IP pkt */
1402 
1403 	/*
1404 	 * offset	The offset of a fragment. offset != 0 means that
1405 	 *	we have a fragment at this offset of an IPv4 packet.
1406 	 *	offset == 0 means that (if this is an IPv4 packet)
1407 	 *	this is the first or only fragment.
1408 	 *	For IPv6 offset|ip6f_mf == 0 means there is no Fragment Header
1409 	 *	or there is a single packet fragment (fragment header added
1410 	 *	without needed).  We will treat a single packet fragment as if
1411 	 *	there was no fragment header (or log/block depending on the
1412 	 *	V_fw_permit_single_frag6 sysctl setting).
1413 	 */
1414 	u_short offset = 0;
1415 	u_short ip6f_mf = 0;
1416 
1417 	/*
1418 	 * Local copies of addresses. They are only valid if we have
1419 	 * an IP packet.
1420 	 *
1421 	 * proto	The protocol. Set to 0 for non-ip packets,
1422 	 *	or to the protocol read from the packet otherwise.
1423 	 *	proto != 0 means that we have an IPv4 packet.
1424 	 *
1425 	 * src_port, dst_port	port numbers, in HOST format. Only
1426 	 *	valid for TCP and UDP packets.
1427 	 *
1428 	 * src_ip, dst_ip	ip addresses, in NETWORK format.
1429 	 *	Only valid for IPv4 packets.
1430 	 */
1431 	uint8_t proto;
1432 	uint16_t src_port, dst_port;		/* NOTE: host format	*/
1433 	struct in_addr src_ip, dst_ip;		/* NOTE: network format	*/
1434 	int iplen = 0;
1435 	int pktlen;
1436 
1437 	struct ipfw_dyn_info dyn_info;
1438 	struct ip_fw *q = NULL;
1439 	struct ip_fw_chain *chain = &V_layer3_chain;
1440 
1441 	/*
1442 	 * We store in ulp a pointer to the upper layer protocol header.
1443 	 * In the ipv4 case this is easy to determine from the header,
1444 	 * but for ipv6 we might have some additional headers in the middle.
1445 	 * ulp is NULL if not found.
1446 	 */
1447 	void *ulp = NULL;		/* upper layer protocol pointer. */
1448 
1449 	/* XXX ipv6 variables */
1450 	int is_ipv6 = 0;
1451 #ifdef INET6
1452 	uint8_t	icmp6_type = 0;
1453 #endif
1454 	uint16_t ext_hd = 0;	/* bits vector for extension header filtering */
1455 	/* end of ipv6 variables */
1456 
1457 	int is_ipv4 = 0;
1458 
1459 	int done = 0;		/* flag to exit the outer loop */
1460 	IPFW_RLOCK_TRACKER;
1461 	bool mem;
1462 
1463 	if ((mem = (args->flags & IPFW_ARGS_LENMASK))) {
1464 		if (args->flags & IPFW_ARGS_ETHER) {
1465 			eh = (struct ether_header *)args->mem;
1466 			if (eh->ether_type == htons(ETHERTYPE_VLAN))
1467 				ip = (struct ip *)
1468 				    ((struct ether_vlan_header *)eh + 1);
1469 			else
1470 				ip = (struct ip *)(eh + 1);
1471 		} else {
1472 			eh = NULL;
1473 			ip = (struct ip *)args->mem;
1474 		}
1475 		pktlen = IPFW_ARGS_LENGTH(args->flags);
1476 		args->f_id.fib = args->ifp->if_fib;	/* best guess */
1477 	} else {
1478 		m = args->m;
1479 		if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready))
1480 			return (IP_FW_PASS);	/* accept */
1481 		if (args->flags & IPFW_ARGS_ETHER) {
1482 	                /* We need some amount of data to be contiguous. */
1483 			if (m->m_len < min(m->m_pkthdr.len, max_protohdr) &&
1484 			    (args->m = m = m_pullup(m, min(m->m_pkthdr.len,
1485 			    max_protohdr))) == NULL)
1486 				goto pullup_failed;
1487 			eh = mtod(m, struct ether_header *);
1488 			ip = (struct ip *)(eh + 1);
1489 		} else {
1490 			eh = NULL;
1491 			ip = mtod(m, struct ip *);
1492 		}
1493 		pktlen = m->m_pkthdr.len;
1494 		args->f_id.fib = M_GETFIB(m); /* mbuf not altered */
1495 	}
1496 
1497 	dst_ip.s_addr = 0;		/* make sure it is initialized */
1498 	src_ip.s_addr = 0;		/* make sure it is initialized */
1499 	src_port = dst_port = 0;
1500 
1501 	DYN_INFO_INIT(&dyn_info);
1502 /*
1503  * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous,
1504  * then it sets p to point at the offset "len" in the mbuf. WARNING: the
1505  * pointer might become stale after other pullups (but we never use it
1506  * this way).
1507  */
1508 #define	PULLUP_TO(_len, p, T)	PULLUP_LEN(_len, p, sizeof(T))
1509 #define	EHLEN	(eh != NULL ? ((char *)ip - (char *)eh) : 0)
1510 #define	_PULLUP_LOCKED(_len, p, T, unlock)			\
1511 do {								\
1512 	int x = (_len) + T + EHLEN;				\
1513 	if (mem) {						\
1514 		if (__predict_false(pktlen < x)) {		\
1515 			unlock;					\
1516 			goto pullup_failed;			\
1517 		}						\
1518 		p = (char *)args->mem + (_len) + EHLEN;		\
1519 	} else {						\
1520 		if (__predict_false((m)->m_len < x)) {		\
1521 			args->m = m = m_pullup(m, x);		\
1522 			if (m == NULL) {			\
1523 				unlock;				\
1524 				goto pullup_failed;		\
1525 			}					\
1526 		}						\
1527 		p = mtod(m, char *) + (_len) + EHLEN;		\
1528 	}							\
1529 } while (0)
1530 
1531 #define	PULLUP_LEN(_len, p, T)	_PULLUP_LOCKED(_len, p, T, )
1532 #define	PULLUP_LEN_LOCKED(_len, p, T)	\
1533     _PULLUP_LOCKED(_len, p, T, IPFW_PF_RUNLOCK(chain));	\
1534     UPDATE_POINTERS()
1535 /*
1536  * In case pointers got stale after pullups, update them.
1537  */
1538 #define	UPDATE_POINTERS()					\
1539 do {								\
1540 	if (!mem) {						\
1541 		if (eh != NULL) {				\
1542 			eh = mtod(m, struct ether_header *);	\
1543 			ip = (struct ip *)(eh + 1);		\
1544 		} else						\
1545 			ip = mtod(m, struct ip *);		\
1546 		args->m = m;					\
1547 	}							\
1548 } while (0)
1549 
1550 	/* Identify IP packets and fill up variables. */
1551 	if (pktlen >= sizeof(struct ip6_hdr) &&
1552 	    (eh == NULL || eh->ether_type == htons(ETHERTYPE_IPV6)) &&
1553 	    ip->ip_v == 6) {
1554 		struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
1555 
1556 		is_ipv6 = 1;
1557 		args->flags |= IPFW_ARGS_IP6;
1558 		hlen = sizeof(struct ip6_hdr);
1559 		proto = ip6->ip6_nxt;
1560 		/* Search extension headers to find upper layer protocols */
1561 		while (ulp == NULL && offset == 0) {
1562 			switch (proto) {
1563 			case IPPROTO_ICMPV6:
1564 				PULLUP_TO(hlen, ulp, struct icmp6_hdr);
1565 #ifdef INET6
1566 				icmp6_type = ICMP6(ulp)->icmp6_type;
1567 #endif
1568 				break;
1569 
1570 			case IPPROTO_TCP:
1571 				PULLUP_TO(hlen, ulp, struct tcphdr);
1572 				dst_port = TCP(ulp)->th_dport;
1573 				src_port = TCP(ulp)->th_sport;
1574 				/* save flags for dynamic rules */
1575 				args->f_id._flags = TCP(ulp)->th_flags;
1576 				break;
1577 
1578 			case IPPROTO_SCTP:
1579 				if (pktlen >= hlen + sizeof(struct sctphdr) +
1580 				    sizeof(struct sctp_chunkhdr) +
1581 				    offsetof(struct sctp_init, a_rwnd))
1582 					PULLUP_LEN(hlen, ulp,
1583 					    sizeof(struct sctphdr) +
1584 					    sizeof(struct sctp_chunkhdr) +
1585 					    offsetof(struct sctp_init, a_rwnd));
1586 				else if (pktlen >= hlen + sizeof(struct sctphdr))
1587 					PULLUP_LEN(hlen, ulp, pktlen - hlen);
1588 				else
1589 					PULLUP_LEN(hlen, ulp,
1590 					    sizeof(struct sctphdr));
1591 				src_port = SCTP(ulp)->src_port;
1592 				dst_port = SCTP(ulp)->dest_port;
1593 				break;
1594 
1595 			case IPPROTO_UDP:
1596 			case IPPROTO_UDPLITE:
1597 				PULLUP_TO(hlen, ulp, struct udphdr);
1598 				dst_port = UDP(ulp)->uh_dport;
1599 				src_port = UDP(ulp)->uh_sport;
1600 				break;
1601 
1602 			case IPPROTO_HOPOPTS:	/* RFC 2460 */
1603 				PULLUP_TO(hlen, ulp, struct ip6_hbh);
1604 				ext_hd |= EXT_HOPOPTS;
1605 				hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1606 				proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1607 				ulp = NULL;
1608 				break;
1609 
1610 			case IPPROTO_ROUTING:	/* RFC 2460 */
1611 				PULLUP_TO(hlen, ulp, struct ip6_rthdr);
1612 				switch (((struct ip6_rthdr *)ulp)->ip6r_type) {
1613 				case 0:
1614 					ext_hd |= EXT_RTHDR0;
1615 					break;
1616 				case 2:
1617 					ext_hd |= EXT_RTHDR2;
1618 					break;
1619 				default:
1620 					if (V_fw_verbose)
1621 						printf("IPFW2: IPV6 - Unknown "
1622 						    "Routing Header type(%d)\n",
1623 						    ((struct ip6_rthdr *)
1624 						    ulp)->ip6r_type);
1625 					if (V_fw_deny_unknown_exthdrs)
1626 					    return (IP_FW_DENY);
1627 					break;
1628 				}
1629 				ext_hd |= EXT_ROUTING;
1630 				hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3;
1631 				proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt;
1632 				ulp = NULL;
1633 				break;
1634 
1635 			case IPPROTO_FRAGMENT:	/* RFC 2460 */
1636 				PULLUP_TO(hlen, ulp, struct ip6_frag);
1637 				ext_hd |= EXT_FRAGMENT;
1638 				hlen += sizeof (struct ip6_frag);
1639 				proto = ((struct ip6_frag *)ulp)->ip6f_nxt;
1640 				offset = ((struct ip6_frag *)ulp)->ip6f_offlg &
1641 					IP6F_OFF_MASK;
1642 				ip6f_mf = ((struct ip6_frag *)ulp)->ip6f_offlg &
1643 					IP6F_MORE_FRAG;
1644 				if (V_fw_permit_single_frag6 == 0 &&
1645 				    offset == 0 && ip6f_mf == 0) {
1646 					if (V_fw_verbose)
1647 						printf("IPFW2: IPV6 - Invalid "
1648 						    "Fragment Header\n");
1649 					if (V_fw_deny_unknown_exthdrs)
1650 					    return (IP_FW_DENY);
1651 					break;
1652 				}
1653 				args->f_id.extra =
1654 				    ntohl(((struct ip6_frag *)ulp)->ip6f_ident);
1655 				ulp = NULL;
1656 				break;
1657 
1658 			case IPPROTO_DSTOPTS:	/* RFC 2460 */
1659 				PULLUP_TO(hlen, ulp, struct ip6_hbh);
1660 				ext_hd |= EXT_DSTOPTS;
1661 				hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1662 				proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1663 				ulp = NULL;
1664 				break;
1665 
1666 			case IPPROTO_AH:	/* RFC 2402 */
1667 				PULLUP_TO(hlen, ulp, struct ip6_ext);
1668 				ext_hd |= EXT_AH;
1669 				hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2;
1670 				proto = ((struct ip6_ext *)ulp)->ip6e_nxt;
1671 				ulp = NULL;
1672 				break;
1673 
1674 			case IPPROTO_ESP:	/* RFC 2406 */
1675 				PULLUP_TO(hlen, ulp, uint32_t);	/* SPI, Seq# */
1676 				/* Anything past Seq# is variable length and
1677 				 * data past this ext. header is encrypted. */
1678 				ext_hd |= EXT_ESP;
1679 				break;
1680 
1681 			case IPPROTO_NONE:	/* RFC 2460 */
1682 				/*
1683 				 * Packet ends here, and IPv6 header has
1684 				 * already been pulled up. If ip6e_len!=0
1685 				 * then octets must be ignored.
1686 				 */
1687 				ulp = ip; /* non-NULL to get out of loop. */
1688 				break;
1689 
1690 			case IPPROTO_OSPFIGP:
1691 				/* XXX OSPF header check? */
1692 				PULLUP_TO(hlen, ulp, struct ip6_ext);
1693 				break;
1694 
1695 			case IPPROTO_PIM:
1696 				/* XXX PIM header check? */
1697 				PULLUP_TO(hlen, ulp, struct pim);
1698 				break;
1699 
1700 			case IPPROTO_GRE:	/* RFC 1701 */
1701 				/* XXX GRE header check? */
1702 				PULLUP_TO(hlen, ulp, struct grehdr);
1703 				break;
1704 
1705 			case IPPROTO_CARP:
1706 				PULLUP_TO(hlen, ulp, offsetof(
1707 				    struct carp_header, carp_counter));
1708 				if (CARP_ADVERTISEMENT !=
1709 				    ((struct carp_header *)ulp)->carp_type)
1710 					return (IP_FW_DENY);
1711 				break;
1712 
1713 			case IPPROTO_IPV6:	/* RFC 2893 */
1714 				PULLUP_TO(hlen, ulp, struct ip6_hdr);
1715 				break;
1716 
1717 			case IPPROTO_IPV4:	/* RFC 2893 */
1718 				PULLUP_TO(hlen, ulp, struct ip);
1719 				break;
1720 
1721 			default:
1722 				if (V_fw_verbose)
1723 					printf("IPFW2: IPV6 - Unknown "
1724 					    "Extension Header(%d), ext_hd=%x\n",
1725 					     proto, ext_hd);
1726 				if (V_fw_deny_unknown_exthdrs)
1727 				    return (IP_FW_DENY);
1728 				PULLUP_TO(hlen, ulp, struct ip6_ext);
1729 				break;
1730 			} /*switch */
1731 		}
1732 		UPDATE_POINTERS();
1733 		ip6 = (struct ip6_hdr *)ip;
1734 		args->f_id.addr_type = 6;
1735 		args->f_id.src_ip6 = ip6->ip6_src;
1736 		args->f_id.dst_ip6 = ip6->ip6_dst;
1737 		args->f_id.flow_id6 = ntohl(ip6->ip6_flow);
1738 		iplen = ntohs(ip6->ip6_plen) + sizeof(*ip6);
1739 	} else if (pktlen >= sizeof(struct ip) &&
1740 	    (eh == NULL || eh->ether_type == htons(ETHERTYPE_IP)) &&
1741 	    ip->ip_v == 4) {
1742 		is_ipv4 = 1;
1743 		args->flags |= IPFW_ARGS_IP4;
1744 		hlen = ip->ip_hl << 2;
1745 		/*
1746 		 * Collect parameters into local variables for faster
1747 		 * matching.
1748 		 */
1749 		proto = ip->ip_p;
1750 		src_ip = ip->ip_src;
1751 		dst_ip = ip->ip_dst;
1752 		offset = ntohs(ip->ip_off) & IP_OFFMASK;
1753 		iplen = ntohs(ip->ip_len);
1754 
1755 		if (offset == 0) {
1756 			switch (proto) {
1757 			case IPPROTO_TCP:
1758 				PULLUP_TO(hlen, ulp, struct tcphdr);
1759 				dst_port = TCP(ulp)->th_dport;
1760 				src_port = TCP(ulp)->th_sport;
1761 				/* save flags for dynamic rules */
1762 				args->f_id._flags = TCP(ulp)->th_flags;
1763 				break;
1764 
1765 			case IPPROTO_SCTP:
1766 				if (pktlen >= hlen + sizeof(struct sctphdr) +
1767 				    sizeof(struct sctp_chunkhdr) +
1768 				    offsetof(struct sctp_init, a_rwnd))
1769 					PULLUP_LEN(hlen, ulp,
1770 					    sizeof(struct sctphdr) +
1771 					    sizeof(struct sctp_chunkhdr) +
1772 					    offsetof(struct sctp_init, a_rwnd));
1773 				else if (pktlen >= hlen + sizeof(struct sctphdr))
1774 					PULLUP_LEN(hlen, ulp, pktlen - hlen);
1775 				else
1776 					PULLUP_LEN(hlen, ulp,
1777 					    sizeof(struct sctphdr));
1778 				src_port = SCTP(ulp)->src_port;
1779 				dst_port = SCTP(ulp)->dest_port;
1780 				break;
1781 
1782 			case IPPROTO_UDP:
1783 			case IPPROTO_UDPLITE:
1784 				PULLUP_TO(hlen, ulp, struct udphdr);
1785 				dst_port = UDP(ulp)->uh_dport;
1786 				src_port = UDP(ulp)->uh_sport;
1787 				break;
1788 
1789 			case IPPROTO_ICMP:
1790 				PULLUP_TO(hlen, ulp, struct icmphdr);
1791 				//args->f_id.flags = ICMP(ulp)->icmp_type;
1792 				break;
1793 
1794 			default:
1795 				break;
1796 			}
1797 		} else {
1798 			if (offset == 1 && proto == IPPROTO_TCP) {
1799 				/* RFC 3128 */
1800 				goto pullup_failed;
1801 			}
1802 		}
1803 
1804 		UPDATE_POINTERS();
1805 		args->f_id.addr_type = 4;
1806 		args->f_id.src_ip = ntohl(src_ip.s_addr);
1807 		args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1808 	} else {
1809 		proto = 0;
1810 		dst_ip.s_addr = src_ip.s_addr = 0;
1811 
1812 		args->f_id.addr_type = 1; /* XXX */
1813 	}
1814 #undef PULLUP_TO
1815 	pktlen = iplen < pktlen ? iplen: pktlen;
1816 
1817 	/* Properly initialize the rest of f_id */
1818 	args->f_id.proto = proto;
1819 	args->f_id.src_port = src_port = ntohs(src_port);
1820 	args->f_id.dst_port = dst_port = ntohs(dst_port);
1821 
1822 	IPFW_PF_RLOCK(chain);
1823 	if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */
1824 		IPFW_PF_RUNLOCK(chain);
1825 		return (IP_FW_PASS);	/* accept */
1826 	}
1827 	if (args->flags & IPFW_ARGS_REF) {
1828 		/*
1829 		 * Packet has already been tagged as a result of a previous
1830 		 * match on rule args->rule aka args->rule_id (PIPE, QUEUE,
1831 		 * REASS, NETGRAPH, DIVERT/TEE...)
1832 		 * Validate the slot and continue from the next one
1833 		 * if still present, otherwise do a lookup.
1834 		 */
1835 		f_pos = (args->rule.chain_id == chain->id) ?
1836 		    args->rule.slot :
1837 		    ipfw_find_rule(chain, args->rule.rulenum,
1838 			args->rule.rule_id);
1839 	} else {
1840 		f_pos = 0;
1841 	}
1842 
1843 	if (args->flags & IPFW_ARGS_IN) {
1844 		iif = args->ifp;
1845 		oif = NULL;
1846 	} else {
1847 		MPASS(args->flags & IPFW_ARGS_OUT);
1848 		iif = mem ? NULL : m_rcvif(m);
1849 		oif = args->ifp;
1850 	}
1851 
1852 	/*
1853 	 * Now scan the rules, and parse microinstructions for each rule.
1854 	 * We have two nested loops and an inner switch. Sometimes we
1855 	 * need to break out of one or both loops, or re-enter one of
1856 	 * the loops with updated variables. Loop variables are:
1857 	 *
1858 	 *	f_pos (outer loop) points to the current rule.
1859 	 *		On output it points to the matching rule.
1860 	 *	done (outer loop) is used as a flag to break the loop.
1861 	 *	l (inner loop)	residual length of current rule.
1862 	 *		cmd points to the current microinstruction.
1863 	 *
1864 	 * We break the inner loop by setting l=0 and possibly
1865 	 * cmdlen=0 if we don't want to advance cmd.
1866 	 * We break the outer loop by setting done=1
1867 	 * We can restart the inner loop by setting l>0 and f_pos, f, cmd
1868 	 * as needed.
1869 	 */
1870 	for (; f_pos < chain->n_rules; f_pos++) {
1871 		ipfw_insn *cmd;
1872 		uint32_t tablearg = 0;
1873 		int l, cmdlen, skip_or; /* skip rest of OR block */
1874 		struct ip_fw *f;
1875 
1876 		f = chain->map[f_pos];
1877 		if (V_set_disable & (1 << f->set) )
1878 			continue;
1879 
1880 		skip_or = 0;
1881 		for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
1882 		    l -= cmdlen, cmd += cmdlen) {
1883 			int match;
1884 
1885 			/*
1886 			 * check_body is a jump target used when we find a
1887 			 * CHECK_STATE, and need to jump to the body of
1888 			 * the target rule.
1889 			 */
1890 
1891 /* check_body: */
1892 			cmdlen = F_LEN(cmd);
1893 			/*
1894 			 * An OR block (insn_1 || .. || insn_n) has the
1895 			 * F_OR bit set in all but the last instruction.
1896 			 * The first match will set "skip_or", and cause
1897 			 * the following instructions to be skipped until
1898 			 * past the one with the F_OR bit clear.
1899 			 */
1900 			if (skip_or) {		/* skip this instruction */
1901 				if ((cmd->len & F_OR) == 0)
1902 					skip_or = 0;	/* next one is good */
1903 				continue;
1904 			}
1905 			match = 0; /* set to 1 if we succeed */
1906 
1907 			switch (cmd->opcode) {
1908 			/*
1909 			 * The first set of opcodes compares the packet's
1910 			 * fields with some pattern, setting 'match' if a
1911 			 * match is found. At the end of the loop there is
1912 			 * logic to deal with F_NOT and F_OR flags associated
1913 			 * with the opcode.
1914 			 */
1915 			case O_NOP:
1916 				match = 1;
1917 				break;
1918 
1919 			case O_FORWARD_MAC:
1920 				printf("ipfw: opcode %d unimplemented\n",
1921 				    cmd->opcode);
1922 				break;
1923 
1924 			case O_GID:
1925 			case O_UID:
1926 			case O_JAIL:
1927 				/*
1928 				 * We only check offset == 0 && proto != 0,
1929 				 * as this ensures that we have a
1930 				 * packet with the ports info.
1931 				 */
1932 				if (offset != 0)
1933 					break;
1934 				if (proto == IPPROTO_TCP ||
1935 				    proto == IPPROTO_UDP ||
1936 				    proto == IPPROTO_UDPLITE)
1937 					match = check_uidgid(
1938 						    (ipfw_insn_u32 *)cmd,
1939 						    args, &ucred_lookup,
1940 #ifdef __FreeBSD__
1941 						    &ucred_cache);
1942 #else
1943 						    (void *)&ucred_cache);
1944 #endif
1945 				break;
1946 
1947 			case O_RECV:
1948 				match = iface_match(iif, (ipfw_insn_if *)cmd,
1949 				    chain, &tablearg);
1950 				break;
1951 
1952 			case O_XMIT:
1953 				match = iface_match(oif, (ipfw_insn_if *)cmd,
1954 				    chain, &tablearg);
1955 				break;
1956 
1957 			case O_VIA:
1958 				match = iface_match(args->ifp,
1959 				    (ipfw_insn_if *)cmd, chain, &tablearg);
1960 				break;
1961 
1962 			case O_MACADDR2:
1963 				if (args->flags & IPFW_ARGS_ETHER) {
1964 					u_int32_t *want = (u_int32_t *)
1965 						((ipfw_insn_mac *)cmd)->addr;
1966 					u_int32_t *mask = (u_int32_t *)
1967 						((ipfw_insn_mac *)cmd)->mask;
1968 					u_int32_t *hdr = (u_int32_t *)eh;
1969 
1970 					match =
1971 					    ( want[0] == (hdr[0] & mask[0]) &&
1972 					      want[1] == (hdr[1] & mask[1]) &&
1973 					      want[2] == (hdr[2] & mask[2]) );
1974 				}
1975 				break;
1976 
1977 			case O_MAC_TYPE:
1978 				if (args->flags & IPFW_ARGS_ETHER) {
1979 					u_int16_t *p =
1980 					    ((ipfw_insn_u16 *)cmd)->ports;
1981 					int i;
1982 
1983 					for (i = cmdlen - 1; !match && i>0;
1984 					    i--, p += 2)
1985 						match =
1986 						    (ntohs(eh->ether_type) >=
1987 						    p[0] &&
1988 						    ntohs(eh->ether_type) <=
1989 						    p[1]);
1990 				}
1991 				break;
1992 
1993 			case O_FRAG:
1994 				if (is_ipv4) {
1995 					/*
1996 					 * Since flags_match() works with
1997 					 * uint8_t we pack ip_off into 8 bits.
1998 					 * For this match offset is a boolean.
1999 					 */
2000 					match = flags_match(cmd,
2001 					    ((ntohs(ip->ip_off) & ~IP_OFFMASK)
2002 					    >> 8) | (offset != 0));
2003 				} else {
2004 					/*
2005 					 * Compatibility: historically bare
2006 					 * "frag" would match IPv6 fragments.
2007 					 */
2008 					match = (cmd->arg1 == 0x1 &&
2009 					    (offset != 0));
2010 				}
2011 				break;
2012 
2013 			case O_IN:	/* "out" is "not in" */
2014 				match = (oif == NULL);
2015 				break;
2016 
2017 			case O_LAYER2:
2018 				match = (args->flags & IPFW_ARGS_ETHER);
2019 				break;
2020 
2021 			case O_DIVERTED:
2022 				if ((args->flags & IPFW_ARGS_REF) == 0)
2023 					break;
2024 				/*
2025 				 * For diverted packets, args->rule.info
2026 				 * contains the divert port (in host format)
2027 				 * reason and direction.
2028 				 */
2029 				match = ((args->rule.info & IPFW_IS_MASK) ==
2030 				    IPFW_IS_DIVERT) && (
2031 				    ((args->rule.info & IPFW_INFO_IN) ?
2032 					1: 2) & cmd->arg1);
2033 				break;
2034 
2035 			case O_PROTO:
2036 				/*
2037 				 * We do not allow an arg of 0 so the
2038 				 * check of "proto" only suffices.
2039 				 */
2040 				match = (proto == cmd->arg1);
2041 				break;
2042 
2043 			case O_IP_SRC:
2044 				match = is_ipv4 &&
2045 				    (((ipfw_insn_ip *)cmd)->addr.s_addr ==
2046 				    src_ip.s_addr);
2047 				break;
2048 
2049 			case O_IP_DST_LOOKUP:
2050 			{
2051 				if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) {
2052 					void *pkey;
2053 					uint32_t vidx, key;
2054 					uint16_t keylen = 0; /* zero if can't match the packet */
2055 
2056 					/* Determine lookup key type */
2057 					vidx = ((ipfw_insn_u32 *)cmd)->d[1];
2058 					switch (vidx) {
2059 					case LOOKUP_DST_IP:
2060 					case LOOKUP_SRC_IP:
2061 						/* Need IP frame */
2062 						if (is_ipv6 == 0 && is_ipv4 == 0)
2063 							break;
2064 						if (vidx == LOOKUP_DST_IP)
2065 							pkey = is_ipv6 ?
2066 								(void *)&args->f_id.dst_ip6:
2067 								(void *)&dst_ip;
2068 						else
2069 							pkey = is_ipv6 ?
2070 								(void *)&args->f_id.src_ip6:
2071 								(void *)&src_ip;
2072 						keylen = is_ipv6 ?
2073 							sizeof(struct in6_addr):
2074 							sizeof(in_addr_t);
2075 						break;
2076 					case LOOKUP_DST_PORT:
2077 					case LOOKUP_SRC_PORT:
2078 						/* Need IP frame */
2079 						if (is_ipv6 == 0 && is_ipv4 == 0)
2080 							break;
2081 						/* Skip fragments */
2082 						if (offset != 0)
2083 							break;
2084 						/* Skip proto without ports */
2085 						if (proto != IPPROTO_TCP &&
2086 							proto != IPPROTO_UDP &&
2087 							proto != IPPROTO_UDPLITE &&
2088 							proto != IPPROTO_SCTP)
2089 							break;
2090 						key = vidx == LOOKUP_DST_PORT ?
2091 							dst_port:
2092 							src_port;
2093 						pkey = &key;
2094 						keylen = sizeof(key);
2095 						break;
2096 					case LOOKUP_UID:
2097 					case LOOKUP_JAIL:
2098 						check_uidgid(
2099 						    (ipfw_insn_u32 *)cmd,
2100 						    args, &ucred_lookup,
2101 						    &ucred_cache);
2102 						key = vidx == LOOKUP_UID ?
2103 							ucred_cache->cr_uid:
2104 							ucred_cache->cr_prison->pr_id;
2105 						pkey = &key;
2106 						keylen = sizeof(key);
2107 						break;
2108 					case LOOKUP_DSCP:
2109 						/* Need IP frame */
2110 						if (is_ipv6 == 0 && is_ipv4 == 0)
2111 							break;
2112 						if (is_ipv6)
2113 							key = IPV6_DSCP(
2114 							    (struct ip6_hdr *)ip) >> 2;
2115 						else
2116 							key = ip->ip_tos >> 2;
2117 						pkey = &key;
2118 						keylen = sizeof(key);
2119 						break;
2120 					case LOOKUP_DST_MAC:
2121 					case LOOKUP_SRC_MAC:
2122 						/* Need ether frame */
2123 						if ((args->flags & IPFW_ARGS_ETHER) == 0)
2124 							break;
2125 						pkey = vidx == LOOKUP_DST_MAC ?
2126 							eh->ether_dhost:
2127 							eh->ether_shost;
2128 						keylen = ETHER_ADDR_LEN;
2129 						break;
2130 					case LOOKUP_MARK:
2131 						key = args->rule.pkt_mark;
2132 						pkey = &key;
2133 						keylen = sizeof(key);
2134 						break;
2135 					}
2136 					if (keylen == 0)
2137 						break;
2138 					match = ipfw_lookup_table(chain,
2139 					    cmd->arg1, keylen, pkey, &vidx);
2140 					if (!match)
2141 						break;
2142 					tablearg = vidx;
2143 					break;
2144 				}
2145 				/* cmdlen =< F_INSN_SIZE(ipfw_insn_u32) */
2146 				/* FALLTHROUGH */
2147 			}
2148 			case O_IP_SRC_LOOKUP:
2149 			{
2150 				void *pkey;
2151 				uint32_t vidx;
2152 				uint16_t keylen;
2153 
2154 				if (is_ipv4) {
2155 					keylen = sizeof(in_addr_t);
2156 					if (cmd->opcode == O_IP_DST_LOOKUP)
2157 						pkey = &dst_ip;
2158 					else
2159 						pkey = &src_ip;
2160 				} else if (is_ipv6) {
2161 					keylen = sizeof(struct in6_addr);
2162 					if (cmd->opcode == O_IP_DST_LOOKUP)
2163 						pkey = &args->f_id.dst_ip6;
2164 					else
2165 						pkey = &args->f_id.src_ip6;
2166 				} else
2167 					break;
2168 				match = ipfw_lookup_table(chain, cmd->arg1,
2169 				    keylen, pkey, &vidx);
2170 				if (!match)
2171 					break;
2172 				if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) {
2173 					match = ((ipfw_insn_u32 *)cmd)->d[0] ==
2174 					    TARG_VAL(chain, vidx, tag);
2175 					if (!match)
2176 						break;
2177 				}
2178 				tablearg = vidx;
2179 				break;
2180 			}
2181 
2182 			case O_MAC_SRC_LOOKUP:
2183 			case O_MAC_DST_LOOKUP:
2184 			{
2185 				void *pkey;
2186 				uint32_t vidx;
2187 				uint16_t keylen = ETHER_ADDR_LEN;
2188 
2189 				/* Need ether frame */
2190 				if ((args->flags & IPFW_ARGS_ETHER) == 0)
2191 					break;
2192 
2193 				if (cmd->opcode == O_MAC_DST_LOOKUP)
2194 					pkey = eh->ether_dhost;
2195 				else
2196 					pkey = eh->ether_shost;
2197 
2198 				match = ipfw_lookup_table(chain, cmd->arg1,
2199 				    keylen, pkey, &vidx);
2200 				if (!match)
2201 					break;
2202 				if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) {
2203 					match = ((ipfw_insn_u32 *)cmd)->d[0] ==
2204 					    TARG_VAL(chain, vidx, tag);
2205 					if (!match)
2206 						break;
2207 				}
2208 				tablearg = vidx;
2209 				break;
2210 			}
2211 
2212 			case O_IP_FLOW_LOOKUP:
2213 				{
2214 					uint32_t v = 0;
2215 					match = ipfw_lookup_table(chain,
2216 					    cmd->arg1, 0, &args->f_id, &v);
2217 					if (!match)
2218 						break;
2219 					if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
2220 						match = ((ipfw_insn_u32 *)cmd)->d[0] ==
2221 						    TARG_VAL(chain, v, tag);
2222 					if (match)
2223 						tablearg = v;
2224 				}
2225 				break;
2226 			case O_IP_SRC_MASK:
2227 			case O_IP_DST_MASK:
2228 				if (is_ipv4) {
2229 				    uint32_t a =
2230 					(cmd->opcode == O_IP_DST_MASK) ?
2231 					    dst_ip.s_addr : src_ip.s_addr;
2232 				    uint32_t *p = ((ipfw_insn_u32 *)cmd)->d;
2233 				    int i = cmdlen-1;
2234 
2235 				    for (; !match && i>0; i-= 2, p+= 2)
2236 					match = (p[0] == (a & p[1]));
2237 				}
2238 				break;
2239 
2240 			case O_IP_SRC_ME:
2241 				if (is_ipv4) {
2242 					match = in_localip(src_ip);
2243 					break;
2244 				}
2245 #ifdef INET6
2246 				/* FALLTHROUGH */
2247 			case O_IP6_SRC_ME:
2248 				match = is_ipv6 &&
2249 				    ipfw_localip6(&args->f_id.src_ip6);
2250 #endif
2251 				break;
2252 
2253 			case O_IP_DST_SET:
2254 			case O_IP_SRC_SET:
2255 				if (is_ipv4) {
2256 					u_int32_t *d = (u_int32_t *)(cmd+1);
2257 					u_int32_t addr =
2258 					    cmd->opcode == O_IP_DST_SET ?
2259 						args->f_id.dst_ip :
2260 						args->f_id.src_ip;
2261 
2262 					    if (addr < d[0])
2263 						    break;
2264 					    addr -= d[0]; /* subtract base */
2265 					    match = (addr < cmd->arg1) &&
2266 						( d[ 1 + (addr>>5)] &
2267 						  (1<<(addr & 0x1f)) );
2268 				}
2269 				break;
2270 
2271 			case O_IP_DST:
2272 				match = is_ipv4 &&
2273 				    (((ipfw_insn_ip *)cmd)->addr.s_addr ==
2274 				    dst_ip.s_addr);
2275 				break;
2276 
2277 			case O_IP_DST_ME:
2278 				if (is_ipv4) {
2279 					match = in_localip(dst_ip);
2280 					break;
2281 				}
2282 #ifdef INET6
2283 				/* FALLTHROUGH */
2284 			case O_IP6_DST_ME:
2285 				match = is_ipv6 &&
2286 				    ipfw_localip6(&args->f_id.dst_ip6);
2287 #endif
2288 				break;
2289 
2290 			case O_IP_SRCPORT:
2291 			case O_IP_DSTPORT:
2292 				/*
2293 				 * offset == 0 && proto != 0 is enough
2294 				 * to guarantee that we have a
2295 				 * packet with port info.
2296 				 */
2297 				if ((proto == IPPROTO_UDP ||
2298 				    proto == IPPROTO_UDPLITE ||
2299 				    proto == IPPROTO_TCP ||
2300 				    proto == IPPROTO_SCTP) && offset == 0) {
2301 					u_int16_t x =
2302 					    (cmd->opcode == O_IP_SRCPORT) ?
2303 						src_port : dst_port ;
2304 					u_int16_t *p =
2305 					    ((ipfw_insn_u16 *)cmd)->ports;
2306 					int i;
2307 
2308 					for (i = cmdlen - 1; !match && i>0;
2309 					    i--, p += 2)
2310 						match = (x>=p[0] && x<=p[1]);
2311 				}
2312 				break;
2313 
2314 			case O_ICMPTYPE:
2315 				match = (offset == 0 && proto==IPPROTO_ICMP &&
2316 				    icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) );
2317 				break;
2318 
2319 #ifdef INET6
2320 			case O_ICMP6TYPE:
2321 				match = is_ipv6 && offset == 0 &&
2322 				    proto==IPPROTO_ICMPV6 &&
2323 				    icmp6type_match(
2324 					ICMP6(ulp)->icmp6_type,
2325 					(ipfw_insn_u32 *)cmd);
2326 				break;
2327 #endif /* INET6 */
2328 
2329 			case O_IPOPT:
2330 				match = (is_ipv4 &&
2331 				    ipopts_match(ip, cmd) );
2332 				break;
2333 
2334 			case O_IPVER:
2335 				match = ((is_ipv4 || is_ipv6) &&
2336 				    cmd->arg1 == ip->ip_v);
2337 				break;
2338 
2339 			case O_IPID:
2340 			case O_IPTTL:
2341 				if (!is_ipv4)
2342 					break;
2343 			case O_IPLEN:
2344 				{	/* only for IP packets */
2345 				    uint16_t x;
2346 				    uint16_t *p;
2347 				    int i;
2348 
2349 				    if (cmd->opcode == O_IPLEN)
2350 					x = iplen;
2351 				    else if (cmd->opcode == O_IPTTL)
2352 					x = ip->ip_ttl;
2353 				    else /* must be IPID */
2354 					x = ntohs(ip->ip_id);
2355 				    if (cmdlen == 1) {
2356 					match = (cmd->arg1 == x);
2357 					break;
2358 				    }
2359 				    /* otherwise we have ranges */
2360 				    p = ((ipfw_insn_u16 *)cmd)->ports;
2361 				    i = cmdlen - 1;
2362 				    for (; !match && i>0; i--, p += 2)
2363 					match = (x >= p[0] && x <= p[1]);
2364 				}
2365 				break;
2366 
2367 			case O_IPPRECEDENCE:
2368 				match = (is_ipv4 &&
2369 				    (cmd->arg1 == (ip->ip_tos & 0xe0)) );
2370 				break;
2371 
2372 			case O_IPTOS:
2373 				match = (is_ipv4 &&
2374 				    flags_match(cmd, ip->ip_tos));
2375 				break;
2376 
2377 			case O_DSCP:
2378 			    {
2379 				uint32_t *p;
2380 				uint16_t x;
2381 
2382 				p = ((ipfw_insn_u32 *)cmd)->d;
2383 
2384 				if (is_ipv4)
2385 					x = ip->ip_tos >> 2;
2386 				else if (is_ipv6) {
2387 					x = IPV6_DSCP(
2388 					    (struct ip6_hdr *)ip) >> 2;
2389 					x &= 0x3f;
2390 				} else
2391 					break;
2392 
2393 				/* DSCP bitmask is stored as low_u32 high_u32 */
2394 				if (x >= 32)
2395 					match = *(p + 1) & (1 << (x - 32));
2396 				else
2397 					match = *p & (1 << x);
2398 			    }
2399 				break;
2400 
2401 			case O_TCPDATALEN:
2402 				if (proto == IPPROTO_TCP && offset == 0) {
2403 				    struct tcphdr *tcp;
2404 				    uint16_t x;
2405 				    uint16_t *p;
2406 				    int i;
2407 #ifdef INET6
2408 				    if (is_ipv6) {
2409 					    struct ip6_hdr *ip6;
2410 
2411 					    ip6 = (struct ip6_hdr *)ip;
2412 					    if (ip6->ip6_plen == 0) {
2413 						    /*
2414 						     * Jumbo payload is not
2415 						     * supported by this
2416 						     * opcode.
2417 						     */
2418 						    break;
2419 					    }
2420 					    x = iplen - hlen;
2421 				    } else
2422 #endif /* INET6 */
2423 					    x = iplen - (ip->ip_hl << 2);
2424 				    tcp = TCP(ulp);
2425 				    x -= tcp->th_off << 2;
2426 				    if (cmdlen == 1) {
2427 					match = (cmd->arg1 == x);
2428 					break;
2429 				    }
2430 				    /* otherwise we have ranges */
2431 				    p = ((ipfw_insn_u16 *)cmd)->ports;
2432 				    i = cmdlen - 1;
2433 				    for (; !match && i>0; i--, p += 2)
2434 					match = (x >= p[0] && x <= p[1]);
2435 				}
2436 				break;
2437 
2438 			case O_TCPFLAGS:
2439 				match = (proto == IPPROTO_TCP && offset == 0 &&
2440 				    flags_match(cmd, TCP(ulp)->th_flags));
2441 				break;
2442 
2443 			case O_TCPOPTS:
2444 				if (proto == IPPROTO_TCP && offset == 0 && ulp){
2445 					PULLUP_LEN_LOCKED(hlen, ulp,
2446 					    (TCP(ulp)->th_off << 2));
2447 					match = tcpopts_match(TCP(ulp), cmd);
2448 				}
2449 				break;
2450 
2451 			case O_TCPSEQ:
2452 				match = (proto == IPPROTO_TCP && offset == 0 &&
2453 				    ((ipfw_insn_u32 *)cmd)->d[0] ==
2454 					TCP(ulp)->th_seq);
2455 				break;
2456 
2457 			case O_TCPACK:
2458 				match = (proto == IPPROTO_TCP && offset == 0 &&
2459 				    ((ipfw_insn_u32 *)cmd)->d[0] ==
2460 					TCP(ulp)->th_ack);
2461 				break;
2462 
2463 			case O_TCPMSS:
2464 				if (proto == IPPROTO_TCP &&
2465 				    (args->f_id._flags & TH_SYN) != 0 &&
2466 				    ulp != NULL) {
2467 					uint16_t mss, *p;
2468 					int i;
2469 
2470 					PULLUP_LEN_LOCKED(hlen, ulp,
2471 					    (TCP(ulp)->th_off << 2));
2472 					if ((tcpopts_parse(TCP(ulp), &mss) &
2473 					    IP_FW_TCPOPT_MSS) == 0)
2474 						break;
2475 					if (cmdlen == 1) {
2476 						match = (cmd->arg1 == mss);
2477 						break;
2478 					}
2479 					/* Otherwise we have ranges. */
2480 					p = ((ipfw_insn_u16 *)cmd)->ports;
2481 					i = cmdlen - 1;
2482 					for (; !match && i > 0; i--, p += 2)
2483 						match = (mss >= p[0] &&
2484 						    mss <= p[1]);
2485 				}
2486 				break;
2487 
2488 			case O_TCPWIN:
2489 				if (proto == IPPROTO_TCP && offset == 0) {
2490 				    uint16_t x;
2491 				    uint16_t *p;
2492 				    int i;
2493 
2494 				    x = ntohs(TCP(ulp)->th_win);
2495 				    if (cmdlen == 1) {
2496 					match = (cmd->arg1 == x);
2497 					break;
2498 				    }
2499 				    /* Otherwise we have ranges. */
2500 				    p = ((ipfw_insn_u16 *)cmd)->ports;
2501 				    i = cmdlen - 1;
2502 				    for (; !match && i > 0; i--, p += 2)
2503 					match = (x >= p[0] && x <= p[1]);
2504 				}
2505 				break;
2506 
2507 			case O_ESTAB:
2508 				/* reject packets which have SYN only */
2509 				/* XXX should i also check for TH_ACK ? */
2510 				match = (proto == IPPROTO_TCP && offset == 0 &&
2511 				    (TCP(ulp)->th_flags &
2512 				     (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
2513 				break;
2514 
2515 			case O_ALTQ: {
2516 				struct pf_mtag *at;
2517 				struct m_tag *mtag;
2518 				ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
2519 
2520 				/*
2521 				 * ALTQ uses mbuf tags from another
2522 				 * packet filtering system - pf(4).
2523 				 * We allocate a tag in its format
2524 				 * and fill it in, pretending to be pf(4).
2525 				 */
2526 				match = 1;
2527 				at = pf_find_mtag(m);
2528 				if (at != NULL && at->qid != 0)
2529 					break;
2530 				mtag = m_tag_get(PACKET_TAG_PF,
2531 				    sizeof(struct pf_mtag), M_NOWAIT | M_ZERO);
2532 				if (mtag == NULL) {
2533 					/*
2534 					 * Let the packet fall back to the
2535 					 * default ALTQ.
2536 					 */
2537 					break;
2538 				}
2539 				m_tag_prepend(m, mtag);
2540 				at = (struct pf_mtag *)(mtag + 1);
2541 				at->qid = altq->qid;
2542 				at->hdr = ip;
2543 				break;
2544 			}
2545 
2546 			case O_LOG:
2547 				ipfw_log(chain, f, hlen, args,
2548 				    offset | ip6f_mf, tablearg, ip);
2549 				match = 1;
2550 				break;
2551 
2552 			case O_PROB:
2553 				match = (random()<((ipfw_insn_u32 *)cmd)->d[0]);
2554 				break;
2555 
2556 			case O_VERREVPATH:
2557 				/* Outgoing packets automatically pass/match */
2558 				match = (args->flags & IPFW_ARGS_OUT ||
2559 				    (
2560 #ifdef INET6
2561 				    is_ipv6 ?
2562 					verify_path6(&(args->f_id.src_ip6),
2563 					    iif, args->f_id.fib) :
2564 #endif
2565 				    verify_path(src_ip, iif, args->f_id.fib)));
2566 				break;
2567 
2568 			case O_VERSRCREACH:
2569 				/* Outgoing packets automatically pass/match */
2570 				match = (hlen > 0 && ((oif != NULL) || (
2571 #ifdef INET6
2572 				    is_ipv6 ?
2573 				        verify_path6(&(args->f_id.src_ip6),
2574 				            NULL, args->f_id.fib) :
2575 #endif
2576 				    verify_path(src_ip, NULL, args->f_id.fib))));
2577 				break;
2578 
2579 			case O_ANTISPOOF:
2580 				/* Outgoing packets automatically pass/match */
2581 				if (oif == NULL && hlen > 0 &&
2582 				    (  (is_ipv4 && in_localaddr(src_ip))
2583 #ifdef INET6
2584 				    || (is_ipv6 &&
2585 				        in6_localaddr(&(args->f_id.src_ip6)))
2586 #endif
2587 				    ))
2588 					match =
2589 #ifdef INET6
2590 					    is_ipv6 ? verify_path6(
2591 					        &(args->f_id.src_ip6), iif,
2592 						args->f_id.fib) :
2593 #endif
2594 					    verify_path(src_ip, iif,
2595 					        args->f_id.fib);
2596 				else
2597 					match = 1;
2598 				break;
2599 
2600 			case O_IPSEC:
2601 				match = (m_tag_find(m,
2602 				    PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL);
2603 				/* otherwise no match */
2604 				break;
2605 
2606 #ifdef INET6
2607 			case O_IP6_SRC:
2608 				match = is_ipv6 &&
2609 				    IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6,
2610 				    &((ipfw_insn_ip6 *)cmd)->addr6);
2611 				break;
2612 
2613 			case O_IP6_DST:
2614 				match = is_ipv6 &&
2615 				IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6,
2616 				    &((ipfw_insn_ip6 *)cmd)->addr6);
2617 				break;
2618 			case O_IP6_SRC_MASK:
2619 			case O_IP6_DST_MASK:
2620 				if (is_ipv6) {
2621 					int i = cmdlen - 1;
2622 					struct in6_addr p;
2623 					struct in6_addr *d =
2624 					    &((ipfw_insn_ip6 *)cmd)->addr6;
2625 
2626 					for (; !match && i > 0; d += 2,
2627 					    i -= F_INSN_SIZE(struct in6_addr)
2628 					    * 2) {
2629 						p = (cmd->opcode ==
2630 						    O_IP6_SRC_MASK) ?
2631 						    args->f_id.src_ip6:
2632 						    args->f_id.dst_ip6;
2633 						APPLY_MASK(&p, &d[1]);
2634 						match =
2635 						    IN6_ARE_ADDR_EQUAL(&d[0],
2636 						    &p);
2637 					}
2638 				}
2639 				break;
2640 
2641 			case O_FLOW6ID:
2642 				match = is_ipv6 &&
2643 				    flow6id_match(args->f_id.flow_id6,
2644 				    (ipfw_insn_u32 *) cmd);
2645 				break;
2646 
2647 			case O_EXT_HDR:
2648 				match = is_ipv6 &&
2649 				    (ext_hd & ((ipfw_insn *) cmd)->arg1);
2650 				break;
2651 
2652 			case O_IP6:
2653 				match = is_ipv6;
2654 				break;
2655 #endif
2656 
2657 			case O_IP4:
2658 				match = is_ipv4;
2659 				break;
2660 
2661 			case O_TAG: {
2662 				struct m_tag *mtag;
2663 				uint32_t tag = TARG(cmd->arg1, tag);
2664 
2665 				/* Packet is already tagged with this tag? */
2666 				mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL);
2667 
2668 				/* We have `untag' action when F_NOT flag is
2669 				 * present. And we must remove this mtag from
2670 				 * mbuf and reset `match' to zero (`match' will
2671 				 * be inversed later).
2672 				 * Otherwise we should allocate new mtag and
2673 				 * push it into mbuf.
2674 				 */
2675 				if (cmd->len & F_NOT) { /* `untag' action */
2676 					if (mtag != NULL)
2677 						m_tag_delete(m, mtag);
2678 					match = 0;
2679 				} else {
2680 					if (mtag == NULL) {
2681 						mtag = m_tag_alloc( MTAG_IPFW,
2682 						    tag, 0, M_NOWAIT);
2683 						if (mtag != NULL)
2684 							m_tag_prepend(m, mtag);
2685 					}
2686 					match = 1;
2687 				}
2688 				break;
2689 			}
2690 
2691 			case O_FIB: /* try match the specified fib */
2692 				if (args->f_id.fib == cmd->arg1)
2693 					match = 1;
2694 				break;
2695 
2696 			case O_SOCKARG:	{
2697 #ifndef USERSPACE	/* not supported in userspace */
2698 				struct inpcb *inp = args->inp;
2699 				struct inpcbinfo *pi;
2700 				bool inp_locked = false;
2701 
2702 				if (proto == IPPROTO_TCP)
2703 					pi = &V_tcbinfo;
2704 				else if (proto == IPPROTO_UDP)
2705 					pi = &V_udbinfo;
2706 				else if (proto == IPPROTO_UDPLITE)
2707 					pi = &V_ulitecbinfo;
2708 				else
2709 					break;
2710 
2711 				/*
2712 				 * XXXRW: so_user_cookie should almost
2713 				 * certainly be inp_user_cookie?
2714 				 */
2715 
2716 				/*
2717 				 * For incoming packet lookup the inpcb
2718 				 * using the src/dest ip/port tuple.
2719 				 */
2720 				if (is_ipv4 && inp == NULL) {
2721 					inp = in_pcblookup(pi,
2722 					    src_ip, htons(src_port),
2723 					    dst_ip, htons(dst_port),
2724 					    INPLOOKUP_RLOCKPCB, NULL);
2725 					inp_locked = true;
2726 				}
2727 #ifdef INET6
2728 				if (is_ipv6 && inp == NULL) {
2729 					inp = in6_pcblookup(pi,
2730 					    &args->f_id.src_ip6,
2731 					    htons(src_port),
2732 					    &args->f_id.dst_ip6,
2733 					    htons(dst_port),
2734 					    INPLOOKUP_RLOCKPCB, NULL);
2735 					inp_locked = true;
2736 				}
2737 #endif /* INET6 */
2738 				if (inp != NULL) {
2739 					if (inp->inp_socket) {
2740 						tablearg =
2741 						    inp->inp_socket->so_user_cookie;
2742 						if (tablearg)
2743 							match = 1;
2744 					}
2745 					if (inp_locked)
2746 						INP_RUNLOCK(inp);
2747 				}
2748 #endif /* !USERSPACE */
2749 				break;
2750 			}
2751 
2752 			case O_TAGGED: {
2753 				struct m_tag *mtag;
2754 				uint32_t tag = TARG(cmd->arg1, tag);
2755 
2756 				if (cmdlen == 1) {
2757 					match = m_tag_locate(m, MTAG_IPFW,
2758 					    tag, NULL) != NULL;
2759 					break;
2760 				}
2761 
2762 				/* we have ranges */
2763 				for (mtag = m_tag_first(m);
2764 				    mtag != NULL && !match;
2765 				    mtag = m_tag_next(m, mtag)) {
2766 					uint16_t *p;
2767 					int i;
2768 
2769 					if (mtag->m_tag_cookie != MTAG_IPFW)
2770 						continue;
2771 
2772 					p = ((ipfw_insn_u16 *)cmd)->ports;
2773 					i = cmdlen - 1;
2774 					for(; !match && i > 0; i--, p += 2)
2775 						match =
2776 						    mtag->m_tag_id >= p[0] &&
2777 						    mtag->m_tag_id <= p[1];
2778 				}
2779 				break;
2780 			}
2781 
2782 			case O_MARK: {
2783 				uint32_t mark;
2784 				if (cmd->arg1 == IP_FW_TARG)
2785 					mark = TARG_VAL(chain, tablearg, mark);
2786 				else
2787 					mark = ((ipfw_insn_u32 *)cmd)->d[0];
2788 				match =
2789 				    (args->rule.pkt_mark &
2790 				    ((ipfw_insn_u32 *)cmd)->d[1]) ==
2791 				    (mark & ((ipfw_insn_u32 *)cmd)->d[1]);
2792 				break;
2793 			}
2794 
2795 			/*
2796 			 * The second set of opcodes represents 'actions',
2797 			 * i.e. the terminal part of a rule once the packet
2798 			 * matches all previous patterns.
2799 			 * Typically there is only one action for each rule,
2800 			 * and the opcode is stored at the end of the rule
2801 			 * (but there are exceptions -- see below).
2802 			 *
2803 			 * In general, here we set retval and terminate the
2804 			 * outer loop (would be a 'break 3' in some language,
2805 			 * but we need to set l=0, done=1)
2806 			 *
2807 			 * Exceptions:
2808 			 * O_COUNT and O_SKIPTO actions:
2809 			 *   instead of terminating, we jump to the next rule
2810 			 *   (setting l=0), or to the SKIPTO target (setting
2811 			 *   f/f_len, cmd and l as needed), respectively.
2812 			 *
2813 			 * O_TAG, O_LOG and O_ALTQ action parameters:
2814 			 *   perform some action and set match = 1;
2815 			 *
2816 			 * O_LIMIT and O_KEEP_STATE: these opcodes are
2817 			 *   not real 'actions', and are stored right
2818 			 *   before the 'action' part of the rule (one
2819 			 *   exception is O_SKIP_ACTION which could be
2820 			 *   between these opcodes and 'action' one).
2821 			 *   These opcodes try to install an entry in the
2822 			 *   state tables; if successful, we continue with
2823 			 *   the next opcode (match=1; break;), otherwise
2824 			 *   the packet must be dropped (set retval,
2825 			 *   break loops with l=0, done=1)
2826 			 *
2827 			 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2828 			 *   cause a lookup of the state table, and a jump
2829 			 *   to the 'action' part of the parent rule
2830 			 *   if an entry is found, or
2831 			 *   (CHECK_STATE only) a jump to the next rule if
2832 			 *   the entry is not found.
2833 			 *   The result of the lookup is cached so that
2834 			 *   further instances of these opcodes become NOPs.
2835 			 *   The jump to the next rule is done by setting
2836 			 *   l=0, cmdlen=0.
2837 			 *
2838 			 * O_SKIP_ACTION: this opcode is not a real 'action'
2839 			 *  either, and is stored right before the 'action'
2840 			 *  part of the rule, right after the O_KEEP_STATE
2841 			 *  opcode. It causes match failure so the real
2842 			 *  'action' could be executed only if the rule
2843 			 *  is checked via dynamic rule from the state
2844 			 *  table, as in such case execution starts
2845 			 *  from the true 'action' opcode directly.
2846 			 *
2847 			 */
2848 			case O_LIMIT:
2849 			case O_KEEP_STATE:
2850 				if (ipfw_dyn_install_state(chain, f,
2851 				    (ipfw_insn_limit *)cmd, args, ulp,
2852 				    pktlen, &dyn_info, tablearg)) {
2853 					/* error or limit violation */
2854 					retval = IP_FW_DENY;
2855 					l = 0;	/* exit inner loop */
2856 					done = 1; /* exit outer loop */
2857 				}
2858 				match = 1;
2859 				break;
2860 
2861 			case O_PROBE_STATE:
2862 			case O_CHECK_STATE:
2863 				/*
2864 				 * dynamic rules are checked at the first
2865 				 * keep-state or check-state occurrence,
2866 				 * with the result being stored in dyn_info.
2867 				 * The compiler introduces a PROBE_STATE
2868 				 * instruction for us when we have a
2869 				 * KEEP_STATE (because PROBE_STATE needs
2870 				 * to be run first).
2871 				 */
2872 				if (DYN_LOOKUP_NEEDED(&dyn_info, cmd) &&
2873 				    (q = ipfw_dyn_lookup_state(args, ulp,
2874 				    pktlen, cmd, &dyn_info)) != NULL) {
2875 					/*
2876 					 * Found dynamic entry, jump to the
2877 					 * 'action' part of the parent rule
2878 					 * by setting f, cmd, l and clearing
2879 					 * cmdlen.
2880 					 */
2881 					f = q;
2882 					f_pos = dyn_info.f_pos;
2883 					cmd = ACTION_PTR(f);
2884 					l = f->cmd_len - f->act_ofs;
2885 					cmdlen = 0;
2886 					match = 1;
2887 					break;
2888 				}
2889 				/*
2890 				 * Dynamic entry not found. If CHECK_STATE,
2891 				 * skip to next rule, if PROBE_STATE just
2892 				 * ignore and continue with next opcode.
2893 				 */
2894 				if (cmd->opcode == O_CHECK_STATE)
2895 					l = 0;	/* exit inner loop */
2896 				match = 1;
2897 				break;
2898 
2899 			case O_SKIP_ACTION:
2900 				match = 0;	/* skip to the next rule */
2901 				l = 0;		/* exit inner loop */
2902 				break;
2903 
2904 			case O_ACCEPT:
2905 				retval = 0;	/* accept */
2906 				l = 0;		/* exit inner loop */
2907 				done = 1;	/* exit outer loop */
2908 				break;
2909 
2910 			case O_PIPE:
2911 			case O_QUEUE:
2912 				set_match(args, f_pos, chain);
2913 				args->rule.info = TARG(cmd->arg1, pipe);
2914 				if (cmd->opcode == O_PIPE)
2915 					args->rule.info |= IPFW_IS_PIPE;
2916 				if (V_fw_one_pass)
2917 					args->rule.info |= IPFW_ONEPASS;
2918 				retval = IP_FW_DUMMYNET;
2919 				l = 0;          /* exit inner loop */
2920 				done = 1;       /* exit outer loop */
2921 				break;
2922 
2923 			case O_DIVERT:
2924 			case O_TEE:
2925 				if (args->flags & IPFW_ARGS_ETHER)
2926 					break;	/* not on layer 2 */
2927 				/* otherwise this is terminal */
2928 				l = 0;		/* exit inner loop */
2929 				done = 1;	/* exit outer loop */
2930 				retval = (cmd->opcode == O_DIVERT) ?
2931 					IP_FW_DIVERT : IP_FW_TEE;
2932 				set_match(args, f_pos, chain);
2933 				args->rule.info = TARG(cmd->arg1, divert);
2934 				break;
2935 
2936 			case O_COUNT:
2937 				IPFW_INC_RULE_COUNTER(f, pktlen);
2938 				l = 0;		/* exit inner loop */
2939 				break;
2940 
2941 			case O_SKIPTO:
2942 			    IPFW_INC_RULE_COUNTER(f, pktlen);
2943 			    f_pos = JUMP(chain, f, cmd->arg1, tablearg, 0);
2944 			    /*
2945 			     * Skip disabled rules, and re-enter
2946 			     * the inner loop with the correct
2947 			     * f_pos, f, l and cmd.
2948 			     * Also clear cmdlen and skip_or
2949 			     */
2950 			    for (; f_pos < chain->n_rules - 1 &&
2951 				    (V_set_disable &
2952 				     (1 << chain->map[f_pos]->set));
2953 				    f_pos++)
2954 				;
2955 			    /* Re-enter the inner loop at the skipto rule. */
2956 			    f = chain->map[f_pos];
2957 			    l = f->cmd_len;
2958 			    cmd = f->cmd;
2959 			    match = 1;
2960 			    cmdlen = 0;
2961 			    skip_or = 0;
2962 			    continue;
2963 			    break;	/* not reached */
2964 
2965 			case O_CALLRETURN: {
2966 				/*
2967 				 * Implementation of `subroutine' call/return,
2968 				 * in the stack carried in an mbuf tag. This
2969 				 * is different from `skipto' in that any call
2970 				 * address is possible (`skipto' must prevent
2971 				 * backward jumps to avoid endless loops).
2972 				 * We have `return' action when F_NOT flag is
2973 				 * present. The `m_tag_id' field is used as
2974 				 * stack pointer.
2975 				 */
2976 				struct m_tag *mtag;
2977 				uint16_t jmpto, *stack;
2978 
2979 #define	IS_CALL		((cmd->len & F_NOT) == 0)
2980 #define	IS_RETURN	((cmd->len & F_NOT) != 0)
2981 				/*
2982 				 * Hand-rolled version of m_tag_locate() with
2983 				 * wildcard `type'.
2984 				 * If not already tagged, allocate new tag.
2985 				 */
2986 				mtag = m_tag_first(m);
2987 				while (mtag != NULL) {
2988 					if (mtag->m_tag_cookie ==
2989 					    MTAG_IPFW_CALL)
2990 						break;
2991 					mtag = m_tag_next(m, mtag);
2992 				}
2993 				if (mtag == NULL && IS_CALL) {
2994 					mtag = m_tag_alloc(MTAG_IPFW_CALL, 0,
2995 					    IPFW_CALLSTACK_SIZE *
2996 					    sizeof(uint16_t), M_NOWAIT);
2997 					if (mtag != NULL)
2998 						m_tag_prepend(m, mtag);
2999 				}
3000 
3001 				/*
3002 				 * On error both `call' and `return' just
3003 				 * continue with next rule.
3004 				 */
3005 				if (IS_RETURN && (mtag == NULL ||
3006 				    mtag->m_tag_id == 0)) {
3007 					l = 0;		/* exit inner loop */
3008 					break;
3009 				}
3010 				if (IS_CALL && (mtag == NULL ||
3011 				    mtag->m_tag_id >= IPFW_CALLSTACK_SIZE)) {
3012 					printf("ipfw: call stack error, "
3013 					    "go to next rule\n");
3014 					l = 0;		/* exit inner loop */
3015 					break;
3016 				}
3017 
3018 				IPFW_INC_RULE_COUNTER(f, pktlen);
3019 				stack = (uint16_t *)(mtag + 1);
3020 
3021 				/*
3022 				 * The `call' action may use cached f_pos
3023 				 * (in f->next_rule), whose version is written
3024 				 * in f->next_rule.
3025 				 * The `return' action, however, doesn't have
3026 				 * fixed jump address in cmd->arg1 and can't use
3027 				 * cache.
3028 				 */
3029 				if (IS_CALL) {
3030 					stack[mtag->m_tag_id] = f->rulenum;
3031 					mtag->m_tag_id++;
3032 			    		f_pos = JUMP(chain, f, cmd->arg1,
3033 					    tablearg, 1);
3034 				} else {	/* `return' action */
3035 					mtag->m_tag_id--;
3036 					jmpto = stack[mtag->m_tag_id] + 1;
3037 					f_pos = ipfw_find_rule(chain, jmpto, 0);
3038 				}
3039 
3040 				/*
3041 				 * Skip disabled rules, and re-enter
3042 				 * the inner loop with the correct
3043 				 * f_pos, f, l and cmd.
3044 				 * Also clear cmdlen and skip_or
3045 				 */
3046 				for (; f_pos < chain->n_rules - 1 &&
3047 				    (V_set_disable &
3048 				    (1 << chain->map[f_pos]->set)); f_pos++)
3049 					;
3050 				/* Re-enter the inner loop at the dest rule. */
3051 				f = chain->map[f_pos];
3052 				l = f->cmd_len;
3053 				cmd = f->cmd;
3054 				cmdlen = 0;
3055 				skip_or = 0;
3056 				continue;
3057 				break;	/* NOTREACHED */
3058 			}
3059 #undef IS_CALL
3060 #undef IS_RETURN
3061 
3062 			case O_REJECT:
3063 				/*
3064 				 * Drop the packet and send a reject notice
3065 				 * if the packet is not ICMP (or is an ICMP
3066 				 * query), and it is not multicast/broadcast.
3067 				 */
3068 				if (hlen > 0 && is_ipv4 && offset == 0 &&
3069 				    (proto != IPPROTO_ICMP ||
3070 				     is_icmp_query(ICMP(ulp))) &&
3071 				    !(m->m_flags & (M_BCAST|M_MCAST)) &&
3072 				    !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
3073 					send_reject(args, cmd, iplen, ip);
3074 					m = args->m;
3075 				}
3076 				/* FALLTHROUGH */
3077 #ifdef INET6
3078 			case O_UNREACH6:
3079 				if (hlen > 0 && is_ipv6 &&
3080 				    ((offset & IP6F_OFF_MASK) == 0) &&
3081 				    (proto != IPPROTO_ICMPV6 ||
3082 				     (is_icmp6_query(icmp6_type) == 1)) &&
3083 				    !(m->m_flags & (M_BCAST|M_MCAST)) &&
3084 				    !IN6_IS_ADDR_MULTICAST(
3085 					&args->f_id.dst_ip6)) {
3086 					send_reject6(args,
3087 					    cmd->opcode == O_REJECT ?
3088 					    map_icmp_unreach(cmd->arg1):
3089 					    cmd->arg1, hlen,
3090 					    (struct ip6_hdr *)ip);
3091 					m = args->m;
3092 				}
3093 				/* FALLTHROUGH */
3094 #endif
3095 			case O_DENY:
3096 				retval = IP_FW_DENY;
3097 				l = 0;		/* exit inner loop */
3098 				done = 1;	/* exit outer loop */
3099 				break;
3100 
3101 			case O_FORWARD_IP:
3102 				if (args->flags & IPFW_ARGS_ETHER)
3103 					break;	/* not valid on layer2 pkts */
3104 				if (q != f ||
3105 				    dyn_info.direction == MATCH_FORWARD) {
3106 				    struct sockaddr_in *sa;
3107 
3108 				    sa = &(((ipfw_insn_sa *)cmd)->sa);
3109 				    if (sa->sin_addr.s_addr == INADDR_ANY) {
3110 #ifdef INET6
3111 					/*
3112 					 * We use O_FORWARD_IP opcode for
3113 					 * fwd rule with tablearg, but tables
3114 					 * now support IPv6 addresses. And
3115 					 * when we are inspecting IPv6 packet,
3116 					 * we can use nh6 field from
3117 					 * table_value as next_hop6 address.
3118 					 */
3119 					if (is_ipv6) {
3120 						struct ip_fw_nh6 *nh6;
3121 
3122 						args->flags |= IPFW_ARGS_NH6;
3123 						nh6 = &args->hopstore6;
3124 						nh6->sin6_addr = TARG_VAL(
3125 						    chain, tablearg, nh6);
3126 						nh6->sin6_port = sa->sin_port;
3127 						nh6->sin6_scope_id = TARG_VAL(
3128 						    chain, tablearg, zoneid);
3129 					} else
3130 #endif
3131 					{
3132 						args->flags |= IPFW_ARGS_NH4;
3133 						args->hopstore.sin_port =
3134 						    sa->sin_port;
3135 						sa = &args->hopstore;
3136 						sa->sin_family = AF_INET;
3137 						sa->sin_len = sizeof(*sa);
3138 						sa->sin_addr.s_addr = htonl(
3139 						    TARG_VAL(chain, tablearg,
3140 						    nh4));
3141 					}
3142 				    } else {
3143 					    args->flags |= IPFW_ARGS_NH4PTR;
3144 					    args->next_hop = sa;
3145 				    }
3146 				}
3147 				retval = IP_FW_PASS;
3148 				l = 0;          /* exit inner loop */
3149 				done = 1;       /* exit outer loop */
3150 				break;
3151 
3152 #ifdef INET6
3153 			case O_FORWARD_IP6:
3154 				if (args->flags & IPFW_ARGS_ETHER)
3155 					break;	/* not valid on layer2 pkts */
3156 				if (q != f ||
3157 				    dyn_info.direction == MATCH_FORWARD) {
3158 					struct sockaddr_in6 *sin6;
3159 
3160 					sin6 = &(((ipfw_insn_sa6 *)cmd)->sa);
3161 					args->flags |= IPFW_ARGS_NH6PTR;
3162 					args->next_hop6 = sin6;
3163 				}
3164 				retval = IP_FW_PASS;
3165 				l = 0;		/* exit inner loop */
3166 				done = 1;	/* exit outer loop */
3167 				break;
3168 #endif
3169 
3170 			case O_NETGRAPH:
3171 			case O_NGTEE:
3172 				set_match(args, f_pos, chain);
3173 				args->rule.info = TARG(cmd->arg1, netgraph);
3174 				if (V_fw_one_pass)
3175 					args->rule.info |= IPFW_ONEPASS;
3176 				retval = (cmd->opcode == O_NETGRAPH) ?
3177 				    IP_FW_NETGRAPH : IP_FW_NGTEE;
3178 				l = 0;          /* exit inner loop */
3179 				done = 1;       /* exit outer loop */
3180 				break;
3181 
3182 			case O_SETFIB: {
3183 				uint32_t fib;
3184 
3185 				IPFW_INC_RULE_COUNTER(f, pktlen);
3186 				fib = TARG(cmd->arg1, fib) & 0x7FFF;
3187 				if (fib >= rt_numfibs)
3188 					fib = 0;
3189 				M_SETFIB(m, fib);
3190 				args->f_id.fib = fib; /* XXX */
3191 				l = 0;		/* exit inner loop */
3192 				break;
3193 		        }
3194 
3195 			case O_SETDSCP: {
3196 				uint16_t code;
3197 
3198 				code = TARG(cmd->arg1, dscp) & 0x3F;
3199 				l = 0;		/* exit inner loop */
3200 				if (is_ipv4) {
3201 					uint16_t old;
3202 
3203 					old = *(uint16_t *)ip;
3204 					ip->ip_tos = (code << 2) |
3205 					    (ip->ip_tos & 0x03);
3206 					ip->ip_sum = cksum_adjust(ip->ip_sum,
3207 					    old, *(uint16_t *)ip);
3208 				} else if (is_ipv6) {
3209 					/* update cached value */
3210 					args->f_id.flow_id6 =
3211 					    ntohl(*(uint32_t *)ip) & ~0x0FC00000;
3212 					args->f_id.flow_id6 |= code << 22;
3213 
3214 					*((uint32_t *)ip) =
3215 					    htonl(args->f_id.flow_id6);
3216 				} else
3217 					break;
3218 
3219 				IPFW_INC_RULE_COUNTER(f, pktlen);
3220 				break;
3221 			}
3222 
3223 			case O_NAT:
3224 				l = 0;          /* exit inner loop */
3225 				done = 1;       /* exit outer loop */
3226 				/*
3227 				 * Ensure that we do not invoke NAT handler for
3228 				 * non IPv4 packets. Libalias expects only IPv4.
3229 				 */
3230 				if (!is_ipv4 || !IPFW_NAT_LOADED) {
3231 				    retval = IP_FW_DENY;
3232 				    break;
3233 				}
3234 
3235 				struct cfg_nat *t;
3236 				int nat_id;
3237 
3238 				args->rule.info = 0;
3239 				set_match(args, f_pos, chain);
3240 				/* Check if this is 'global' nat rule */
3241 				if (cmd->arg1 == IP_FW_NAT44_GLOBAL) {
3242 					retval = ipfw_nat_ptr(args, NULL, m);
3243 					break;
3244 				}
3245 				t = ((ipfw_insn_nat *)cmd)->nat;
3246 				if (t == NULL) {
3247 					nat_id = TARG(cmd->arg1, nat);
3248 					t = (*lookup_nat_ptr)(&chain->nat, nat_id);
3249 
3250 					if (t == NULL) {
3251 					    retval = IP_FW_DENY;
3252 					    break;
3253 					}
3254 					if (cmd->arg1 != IP_FW_TARG)
3255 					    ((ipfw_insn_nat *)cmd)->nat = t;
3256 				}
3257 				retval = ipfw_nat_ptr(args, t, m);
3258 				break;
3259 
3260 			case O_REASS: {
3261 				int ip_off;
3262 
3263 				l = 0;	/* in any case exit inner loop */
3264 				if (is_ipv6) /* IPv6 is not supported yet */
3265 					break;
3266 				IPFW_INC_RULE_COUNTER(f, pktlen);
3267 				ip_off = ntohs(ip->ip_off);
3268 
3269 				/* if not fragmented, go to next rule */
3270 				if ((ip_off & (IP_MF | IP_OFFMASK)) == 0)
3271 				    break;
3272 
3273 				args->m = m = ip_reass(m);
3274 
3275 				/*
3276 				 * do IP header checksum fixup.
3277 				 */
3278 				if (m == NULL) { /* fragment got swallowed */
3279 				    retval = IP_FW_DENY;
3280 				} else { /* good, packet complete */
3281 				    int hlen;
3282 
3283 				    ip = mtod(m, struct ip *);
3284 				    hlen = ip->ip_hl << 2;
3285 				    ip->ip_sum = 0;
3286 				    if (hlen == sizeof(struct ip))
3287 					ip->ip_sum = in_cksum_hdr(ip);
3288 				    else
3289 					ip->ip_sum = in_cksum(m, hlen);
3290 				    retval = IP_FW_REASS;
3291 				    args->rule.info = 0;
3292 				    set_match(args, f_pos, chain);
3293 				}
3294 				done = 1;	/* exit outer loop */
3295 				break;
3296 			}
3297 
3298 			case O_SETMARK: {
3299 				l = 0;		/* exit inner loop */
3300 				args->rule.pkt_mark = (
3301 				    (cmd->arg1 == IP_FW_TARG) ?
3302 				    TARG_VAL(chain, tablearg, mark) :
3303 				    ((ipfw_insn_u32 *)cmd)->d[0]);
3304 
3305 				IPFW_INC_RULE_COUNTER(f, pktlen);
3306 				break;
3307 			}
3308 
3309 			case O_EXTERNAL_ACTION:
3310 				l = 0; /* in any case exit inner loop */
3311 				retval = ipfw_run_eaction(chain, args,
3312 				    cmd, &done);
3313 				/*
3314 				 * If both @retval and @done are zero,
3315 				 * consider this as rule matching and
3316 				 * update counters.
3317 				 */
3318 				if (retval == 0 && done == 0) {
3319 					IPFW_INC_RULE_COUNTER(f, pktlen);
3320 					/*
3321 					 * Reset the result of the last
3322 					 * dynamic state lookup.
3323 					 * External action can change
3324 					 * @args content, and it may be
3325 					 * used for new state lookup later.
3326 					 */
3327 					DYN_INFO_INIT(&dyn_info);
3328 				}
3329 				break;
3330 
3331 			default:
3332 				panic("-- unknown opcode %d\n", cmd->opcode);
3333 			} /* end of switch() on opcodes */
3334 			/*
3335 			 * if we get here with l=0, then match is irrelevant.
3336 			 */
3337 
3338 			if (cmd->len & F_NOT)
3339 				match = !match;
3340 
3341 			if (match) {
3342 				if (cmd->len & F_OR)
3343 					skip_or = 1;
3344 			} else {
3345 				if (!(cmd->len & F_OR)) /* not an OR block, */
3346 					break;		/* try next rule    */
3347 			}
3348 
3349 		}	/* end of inner loop, scan opcodes */
3350 #undef PULLUP_LEN
3351 #undef PULLUP_LEN_LOCKED
3352 
3353 		if (done)
3354 			break;
3355 
3356 /* next_rule:; */	/* try next rule		*/
3357 
3358 	}		/* end of outer for, scan rules */
3359 
3360 	if (done) {
3361 		struct ip_fw *rule = chain->map[f_pos];
3362 		/* Update statistics */
3363 		IPFW_INC_RULE_COUNTER(rule, pktlen);
3364 		IPFW_PROBE(rule__matched, retval,
3365 		    is_ipv4 ? AF_INET : AF_INET6,
3366 		    is_ipv4 ? (uintptr_t)&src_ip :
3367 		        (uintptr_t)&args->f_id.src_ip6,
3368 		    is_ipv4 ? (uintptr_t)&dst_ip :
3369 		        (uintptr_t)&args->f_id.dst_ip6,
3370 		    args, rule);
3371 	} else {
3372 		retval = IP_FW_DENY;
3373 		printf("ipfw: ouch!, skip past end of rules, denying packet\n");
3374 	}
3375 	IPFW_PF_RUNLOCK(chain);
3376 #ifdef __FreeBSD__
3377 	if (ucred_cache != NULL)
3378 		crfree(ucred_cache);
3379 #endif
3380 	return (retval);
3381 
3382 pullup_failed:
3383 	if (V_fw_verbose)
3384 		printf("ipfw: pullup failed\n");
3385 	return (IP_FW_DENY);
3386 }
3387 
3388 /*
3389  * Set maximum number of tables that can be used in given VNET ipfw instance.
3390  */
3391 #ifdef SYSCTL_NODE
3392 static int
3393 sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS)
3394 {
3395 	int error;
3396 	unsigned int ntables;
3397 
3398 	ntables = V_fw_tables_max;
3399 
3400 	error = sysctl_handle_int(oidp, &ntables, 0, req);
3401 	/* Read operation or some error */
3402 	if ((error != 0) || (req->newptr == NULL))
3403 		return (error);
3404 
3405 	return (ipfw_resize_tables(&V_layer3_chain, ntables));
3406 }
3407 
3408 /*
3409  * Switches table namespace between global and per-set.
3410  */
3411 static int
3412 sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS)
3413 {
3414 	int error;
3415 	unsigned int sets;
3416 
3417 	sets = V_fw_tables_sets;
3418 
3419 	error = sysctl_handle_int(oidp, &sets, 0, req);
3420 	/* Read operation or some error */
3421 	if ((error != 0) || (req->newptr == NULL))
3422 		return (error);
3423 
3424 	return (ipfw_switch_tables_namespace(&V_layer3_chain, sets));
3425 }
3426 #endif
3427 
3428 /*
3429  * Module and VNET glue
3430  */
3431 
3432 /*
3433  * Stuff that must be initialised only on boot or module load
3434  */
3435 static int
3436 ipfw_init(void)
3437 {
3438 	int error = 0;
3439 
3440 	/*
3441  	 * Only print out this stuff the first time around,
3442 	 * when called from the sysinit code.
3443 	 */
3444 	printf("ipfw2 "
3445 #ifdef INET6
3446 		"(+ipv6) "
3447 #endif
3448 		"initialized, divert %s, nat %s, "
3449 		"default to %s, logging ",
3450 #ifdef IPDIVERT
3451 		"enabled",
3452 #else
3453 		"loadable",
3454 #endif
3455 #ifdef IPFIREWALL_NAT
3456 		"enabled",
3457 #else
3458 		"loadable",
3459 #endif
3460 		default_to_accept ? "accept" : "deny");
3461 
3462 	/*
3463 	 * Note: V_xxx variables can be accessed here but the vnet specific
3464 	 * initializer may not have been called yet for the VIMAGE case.
3465 	 * Tuneables will have been processed. We will print out values for
3466 	 * the default vnet.
3467 	 * XXX This should all be rationalized AFTER 8.0
3468 	 */
3469 	if (V_fw_verbose == 0)
3470 		printf("disabled\n");
3471 	else if (V_verbose_limit == 0)
3472 		printf("unlimited\n");
3473 	else
3474 		printf("limited to %d packets/entry by default\n",
3475 		    V_verbose_limit);
3476 
3477 	/* Check user-supplied table count for validness */
3478 	if (default_fw_tables > IPFW_TABLES_MAX)
3479 	  default_fw_tables = IPFW_TABLES_MAX;
3480 
3481 	ipfw_init_sopt_handler();
3482 	ipfw_init_obj_rewriter();
3483 	ipfw_iface_init();
3484 	return (error);
3485 }
3486 
3487 /*
3488  * Called for the removal of the last instance only on module unload.
3489  */
3490 static void
3491 ipfw_destroy(void)
3492 {
3493 
3494 	ipfw_iface_destroy();
3495 	ipfw_destroy_sopt_handler();
3496 	ipfw_destroy_obj_rewriter();
3497 	printf("IP firewall unloaded\n");
3498 }
3499 
3500 /*
3501  * Stuff that must be initialized for every instance
3502  * (including the first of course).
3503  */
3504 static int
3505 vnet_ipfw_init(const void *unused)
3506 {
3507 	int error, first;
3508 	struct ip_fw *rule = NULL;
3509 	struct ip_fw_chain *chain;
3510 
3511 	chain = &V_layer3_chain;
3512 
3513 	first = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
3514 
3515 	/* First set up some values that are compile time options */
3516 	V_autoinc_step = 100;	/* bounded to 1..1000 in add_rule() */
3517 	V_fw_deny_unknown_exthdrs = 1;
3518 #ifdef IPFIREWALL_VERBOSE
3519 	V_fw_verbose = 1;
3520 #endif
3521 #ifdef IPFIREWALL_VERBOSE_LIMIT
3522 	V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
3523 #endif
3524 #ifdef IPFIREWALL_NAT
3525 	LIST_INIT(&chain->nat);
3526 #endif
3527 
3528 	/* Init shared services hash table */
3529 	ipfw_init_srv(chain);
3530 
3531 	ipfw_init_counters();
3532 	/* Set initial number of tables */
3533 	V_fw_tables_max = default_fw_tables;
3534 	error = ipfw_init_tables(chain, first);
3535 	if (error) {
3536 		printf("ipfw2: setting up tables failed\n");
3537 		free(chain->map, M_IPFW);
3538 		free(rule, M_IPFW);
3539 		return (ENOSPC);
3540 	}
3541 
3542 	IPFW_LOCK_INIT(chain);
3543 
3544 	/* fill and insert the default rule */
3545 	rule = ipfw_alloc_rule(chain, sizeof(struct ip_fw));
3546 	rule->flags |= IPFW_RULE_NOOPT;
3547 	rule->cmd_len = 1;
3548 	rule->cmd[0].len = 1;
3549 	rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY;
3550 	chain->default_rule = rule;
3551 	ipfw_add_protected_rule(chain, rule, 0);
3552 
3553 	ipfw_dyn_init(chain);
3554 	ipfw_eaction_init(chain, first);
3555 #ifdef LINEAR_SKIPTO
3556 	ipfw_init_skipto_cache(chain);
3557 #endif
3558 	ipfw_bpf_init(first);
3559 
3560 	/* First set up some values that are compile time options */
3561 	V_ipfw_vnet_ready = 1;		/* Open for business */
3562 
3563 	/*
3564 	 * Hook the sockopt handler and pfil hooks for ipv4 and ipv6.
3565 	 * Even if the latter two fail we still keep the module alive
3566 	 * because the sockopt and layer2 paths are still useful.
3567 	 * ipfw[6]_hook return 0 on success, ENOENT on failure,
3568 	 * so we can ignore the exact return value and just set a flag.
3569 	 *
3570 	 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so
3571 	 * changes in the underlying (per-vnet) variables trigger
3572 	 * immediate hook()/unhook() calls.
3573 	 * In layer2 we have the same behaviour, except that V_ether_ipfw
3574 	 * is checked on each packet because there are no pfil hooks.
3575 	 */
3576 	V_ip_fw_ctl_ptr = ipfw_ctl3;
3577 	error = ipfw_attach_hooks();
3578 	return (error);
3579 }
3580 
3581 /*
3582  * Called for the removal of each instance.
3583  */
3584 static int
3585 vnet_ipfw_uninit(const void *unused)
3586 {
3587 	struct ip_fw *reap;
3588 	struct ip_fw_chain *chain = &V_layer3_chain;
3589 	int i, last;
3590 
3591 	V_ipfw_vnet_ready = 0; /* tell new callers to go away */
3592 	/*
3593 	 * disconnect from ipv4, ipv6, layer2 and sockopt.
3594 	 * Then grab, release and grab again the WLOCK so we make
3595 	 * sure the update is propagated and nobody will be in.
3596 	 */
3597 	ipfw_detach_hooks();
3598 	V_ip_fw_ctl_ptr = NULL;
3599 
3600 	last = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
3601 
3602 	IPFW_UH_WLOCK(chain);
3603 	IPFW_UH_WUNLOCK(chain);
3604 
3605 	ipfw_dyn_uninit(0);	/* run the callout_drain */
3606 
3607 	IPFW_UH_WLOCK(chain);
3608 
3609 	reap = NULL;
3610 	IPFW_WLOCK(chain);
3611 	for (i = 0; i < chain->n_rules; i++)
3612 		ipfw_reap_add(chain, &reap, chain->map[i]);
3613 	free(chain->map, M_IPFW);
3614 #ifdef LINEAR_SKIPTO
3615 	ipfw_destroy_skipto_cache(chain);
3616 #endif
3617 	IPFW_WUNLOCK(chain);
3618 	IPFW_UH_WUNLOCK(chain);
3619 	ipfw_destroy_tables(chain, last);
3620 	ipfw_eaction_uninit(chain, last);
3621 	if (reap != NULL)
3622 		ipfw_reap_rules(reap);
3623 	vnet_ipfw_iface_destroy(chain);
3624 	ipfw_destroy_srv(chain);
3625 	IPFW_LOCK_DESTROY(chain);
3626 	ipfw_dyn_uninit(1);	/* free the remaining parts */
3627 	ipfw_destroy_counters();
3628 	ipfw_bpf_uninit(last);
3629 	return (0);
3630 }
3631 
3632 /*
3633  * Module event handler.
3634  * In general we have the choice of handling most of these events by the
3635  * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to
3636  * use the SYSINIT handlers as they are more capable of expressing the
3637  * flow of control during module and vnet operations, so this is just
3638  * a skeleton. Note there is no SYSINIT equivalent of the module
3639  * SHUTDOWN handler, but we don't have anything to do in that case anyhow.
3640  */
3641 static int
3642 ipfw_modevent(module_t mod, int type, void *unused)
3643 {
3644 	int err = 0;
3645 
3646 	switch (type) {
3647 	case MOD_LOAD:
3648 		/* Called once at module load or
3649 	 	 * system boot if compiled in. */
3650 		break;
3651 	case MOD_QUIESCE:
3652 		/* Called before unload. May veto unloading. */
3653 		break;
3654 	case MOD_UNLOAD:
3655 		/* Called during unload. */
3656 		break;
3657 	case MOD_SHUTDOWN:
3658 		/* Called during system shutdown. */
3659 		break;
3660 	default:
3661 		err = EOPNOTSUPP;
3662 		break;
3663 	}
3664 	return err;
3665 }
3666 
3667 static moduledata_t ipfwmod = {
3668 	"ipfw",
3669 	ipfw_modevent,
3670 	0
3671 };
3672 
3673 /* Define startup order. */
3674 #define	IPFW_SI_SUB_FIREWALL	SI_SUB_PROTO_FIREWALL
3675 #define	IPFW_MODEVENT_ORDER	(SI_ORDER_ANY - 255) /* On boot slot in here. */
3676 #define	IPFW_MODULE_ORDER	(IPFW_MODEVENT_ORDER + 1) /* A little later. */
3677 #define	IPFW_VNET_ORDER		(IPFW_MODEVENT_ORDER + 2) /* Later still. */
3678 
3679 DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER);
3680 FEATURE(ipfw_ctl3, "ipfw new sockopt calls");
3681 MODULE_VERSION(ipfw, 3);
3682 /* should declare some dependencies here */
3683 
3684 /*
3685  * Starting up. Done in order after ipfwmod() has been called.
3686  * VNET_SYSINIT is also called for each existing vnet and each new vnet.
3687  */
3688 SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
3689 	    ipfw_init, NULL);
3690 VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
3691 	    vnet_ipfw_init, NULL);
3692 
3693 /*
3694  * Closing up shop. These are done in REVERSE ORDER, but still
3695  * after ipfwmod() has been called. Not called on reboot.
3696  * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
3697  * or when the module is unloaded.
3698  */
3699 SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
3700 	    ipfw_destroy, NULL);
3701 VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
3702 	    vnet_ipfw_uninit, NULL);
3703 /* end of file */
3704