initial version, corresponding to ipfw3-2012
[ipfw-google.git] / sys / netinet / ipfw / ip_fw2.c
1 /*-
2  * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw2.c 200601 2009-12-16 10:48:40Z luigi $");
28
29 /*
30  * The FreeBSD IP packet firewall, main file
31  */
32
33 #include "opt_ipfw.h"
34 #include "opt_ipdivert.h"
35 #include "opt_inet.h"
36 #ifndef INET
37 #error "IPFIREWALL requires INET"
38 #endif /* INET */
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/condvar.h>
45 #include <sys/eventhandler.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/jail.h>
51 #include <sys/module.h>
52 #include <sys/priv.h>
53 #include <sys/proc.h>
54 #include <sys/rwlock.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/ucred.h>
60 #include <net/ethernet.h> /* for ETHERTYPE_IP */
61 #include <net/if.h>
62 #include <net/route.h>
63 #include <net/pf_mtag.h>
64 #include <net/vnet.h>
65
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/ip_icmp.h>
72 #include <netinet/ip_fw.h>
73 #include <netinet/ipfw/ip_fw_private.h>
74 #include <netinet/ip_carp.h>
75 #include <netinet/pim.h>
76 #include <netinet/tcp_var.h>
77 #include <netinet/udp.h>
78 #include <netinet/udp_var.h>
79 #include <netinet/sctp.h>
80
81 #include <netinet/ip6.h>
82 #include <netinet/icmp6.h>
83 #ifdef INET6
84 #include <netinet6/in6_pcb.h>
85 #include <netinet6/scope6_var.h>
86 #include <netinet6/ip6_var.h>
87 #endif
88
89 #include <machine/in_cksum.h>   /* XXX for in_cksum */
90
91 #ifdef MAC
92 #include <security/mac/mac_framework.h>
93 #endif
94
95 /*
96  * static variables followed by global ones.
97  * All ipfw global variables are here.
98  */
99
100 /* ipfw_vnet_ready controls when we are open for business */
101 static VNET_DEFINE(int, ipfw_vnet_ready) = 0;
102 #define V_ipfw_vnet_ready       VNET(ipfw_vnet_ready)
103
104 static VNET_DEFINE(int, fw_deny_unknown_exthdrs);
105 #define V_fw_deny_unknown_exthdrs       VNET(fw_deny_unknown_exthdrs)
106
107 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
108 static int default_to_accept = 1;
109 #else
110 static int default_to_accept;
111 #endif
112
113 VNET_DEFINE(int, autoinc_step);
114
115 /*
116  * Each rule belongs to one of 32 different sets (0..31).
117  * The variable set_disable contains one bit per set.
118  * If the bit is set, all rules in the corresponding set
119  * are disabled. Set RESVD_SET(31) is reserved for the default rule
120  * and rules that are not deleted by the flush command,
121  * and CANNOT be disabled.
122  * Rules in set RESVD_SET can only be deleted individually.
123  */
124 VNET_DEFINE(u_int32_t, set_disable);
125 #define V_set_disable                   VNET(set_disable)
126
127 VNET_DEFINE(int, fw_verbose);
128 /* counter for ipfw_log(NULL...) */
129 VNET_DEFINE(u_int64_t, norule_counter);
130 VNET_DEFINE(int, verbose_limit);
131
132 /* layer3_chain contains the list of rules for layer 3 */
133 VNET_DEFINE(struct ip_fw_chain, layer3_chain);
134
135 ipfw_nat_t *ipfw_nat_ptr = NULL;
136 struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
137 ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
138 ipfw_nat_cfg_t *ipfw_nat_del_ptr;
139 ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
140 ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
141
142 #ifdef SYSCTL_NODE
143 uint32_t dummy_def = IPFW_DEFAULT_RULE;
144 uint32_t dummy_tables_max = IPFW_TABLES_MAX;
145
146 SYSBEGIN(f3)
147
148 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
149 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, one_pass,
150     CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0,
151     "Only do a single pass through ipfw when using dummynet(4)");
152 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step,
153     CTLFLAG_RW, &VNET_NAME(autoinc_step), 0,
154     "Rule number auto-increment step");
155 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose,
156     CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0,
157     "Log matches to ipfw rules");
158 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit,
159     CTLFLAG_RW, &VNET_NAME(verbose_limit), 0,
160     "Set upper limit of matches of ipfw rules logged");
161 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD,
162     &dummy_def, 0,
163     "The default/max possible rule number.");
164 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, tables_max, CTLFLAG_RD,
165     &dummy_tables_max, 0,
166     "The maximum number of tables.");
167 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN,
168     &default_to_accept, 0,
169     "Make the default rule accept all packets.");
170 TUNABLE_INT("net.inet.ip.fw.default_to_accept", &default_to_accept);
171 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, static_count,
172     CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0,
173     "Number of static rules");
174
175 #ifdef INET6
176 SYSCTL_DECL(_net_inet6_ip6);
177 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
178 SYSCTL_VNET_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs,
179     CTLFLAG_RW | CTLFLAG_SECURE, &VNET_NAME(fw_deny_unknown_exthdrs), 0,
180     "Deny packets with unknown IPv6 Extension Headers");
181 #endif /* INET6 */
182
183 SYSEND
184
185 #endif /* SYSCTL_NODE */
186
187
188 /*
189  * Some macros used in the various matching options.
190  * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
191  * Other macros just cast void * into the appropriate type
192  */
193 #define L3HDR(T, ip)    ((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
194 #define TCP(p)          ((struct tcphdr *)(p))
195 #define SCTP(p)         ((struct sctphdr *)(p))
196 #define UDP(p)          ((struct udphdr *)(p))
197 #define ICMP(p)         ((struct icmphdr *)(p))
198 #define ICMP6(p)        ((struct icmp6_hdr *)(p))
199
200 static __inline int
201 icmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd)
202 {
203         int type = icmp->icmp_type;
204
205         return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) );
206 }
207
208 #define TT      ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
209     (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
210
211 static int
212 is_icmp_query(struct icmphdr *icmp)
213 {
214         int type = icmp->icmp_type;
215
216         return (type <= ICMP_MAXTYPE && (TT & (1<<type)) );
217 }
218 #undef TT
219
220 /*
221  * The following checks use two arrays of 8 or 16 bits to store the
222  * bits that we want set or clear, respectively. They are in the
223  * low and high half of cmd->arg1 or cmd->d[0].
224  *
225  * We scan options and store the bits we find set. We succeed if
226  *
227  *      (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
228  *
229  * The code is sometimes optimized not to store additional variables.
230  */
231
232 static int
233 flags_match(ipfw_insn *cmd, u_int8_t bits)
234 {
235         u_char want_clear;
236         bits = ~bits;
237
238         if ( ((cmd->arg1 & 0xff) & bits) != 0)
239                 return 0; /* some bits we want set were clear */
240         want_clear = (cmd->arg1 >> 8) & 0xff;
241         if ( (want_clear & bits) != want_clear)
242                 return 0; /* some bits we want clear were set */
243         return 1;
244 }
245
246 static int
247 ipopts_match(struct ip *ip, ipfw_insn *cmd)
248 {
249         int optlen, bits = 0;
250         u_char *cp = (u_char *)(ip + 1);
251         int x = (ip->ip_hl << 2) - sizeof (struct ip);
252
253         for (; x > 0; x -= optlen, cp += optlen) {
254                 int opt = cp[IPOPT_OPTVAL];
255
256                 if (opt == IPOPT_EOL)
257                         break;
258                 if (opt == IPOPT_NOP)
259                         optlen = 1;
260                 else {
261                         optlen = cp[IPOPT_OLEN];
262                         if (optlen <= 0 || optlen > x)
263                                 return 0; /* invalid or truncated */
264                 }
265                 switch (opt) {
266
267                 default:
268                         break;
269
270                 case IPOPT_LSRR:
271                         bits |= IP_FW_IPOPT_LSRR;
272                         break;
273
274                 case IPOPT_SSRR:
275                         bits |= IP_FW_IPOPT_SSRR;
276                         break;
277
278                 case IPOPT_RR:
279                         bits |= IP_FW_IPOPT_RR;
280                         break;
281
282                 case IPOPT_TS:
283                         bits |= IP_FW_IPOPT_TS;
284                         break;
285                 }
286         }
287         return (flags_match(cmd, bits));
288 }
289
290 static int
291 tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd)
292 {
293         int optlen, bits = 0;
294         u_char *cp = (u_char *)(tcp + 1);
295         int x = (tcp->th_off << 2) - sizeof(struct tcphdr);
296
297         for (; x > 0; x -= optlen, cp += optlen) {
298                 int opt = cp[0];
299                 if (opt == TCPOPT_EOL)
300                         break;
301                 if (opt == TCPOPT_NOP)
302                         optlen = 1;
303                 else {
304                         optlen = cp[1];
305                         if (optlen <= 0)
306                                 break;
307                 }
308
309                 switch (opt) {
310
311                 default:
312                         break;
313
314                 case TCPOPT_MAXSEG:
315                         bits |= IP_FW_TCPOPT_MSS;
316                         break;
317
318                 case TCPOPT_WINDOW:
319                         bits |= IP_FW_TCPOPT_WINDOW;
320                         break;
321
322                 case TCPOPT_SACK_PERMITTED:
323                 case TCPOPT_SACK:
324                         bits |= IP_FW_TCPOPT_SACK;
325                         break;
326
327                 case TCPOPT_TIMESTAMP:
328                         bits |= IP_FW_TCPOPT_TS;
329                         break;
330
331                 }
332         }
333         return (flags_match(cmd, bits));
334 }
335
336 static int
337 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd)
338 {
339         if (ifp == NULL)        /* no iface with this packet, match fails */
340                 return 0;
341         /* Check by name or by IP address */
342         if (cmd->name[0] != '\0') { /* match by name */
343                 /* Check name */
344                 if (cmd->p.glob) {
345                         if (fnmatch(cmd->name, ifp->if_xname, 0) == 0)
346                                 return(1);
347                 } else {
348                         if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
349                                 return(1);
350                 }
351         } else {
352 #ifdef __FreeBSD__      /* and OSX too ? */
353                 struct ifaddr *ia;
354
355                 if_addr_rlock(ifp);
356                 TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
357                         if (ia->ifa_addr->sa_family != AF_INET)
358                                 continue;
359                         if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
360                             (ia->ifa_addr))->sin_addr.s_addr) {
361                                 if_addr_runlock(ifp);
362                                 return(1);      /* match */
363                         }
364                 }
365                 if_addr_runlock(ifp);
366 #endif /* __FreeBSD__ */
367         }
368         return(0);      /* no match, fail ... */
369 }
370
371 /*
372  * The verify_path function checks if a route to the src exists and
373  * if it is reachable via ifp (when provided).
374  * 
375  * The 'verrevpath' option checks that the interface that an IP packet
376  * arrives on is the same interface that traffic destined for the
377  * packet's source address would be routed out of.
378  * The 'versrcreach' option just checks that the source address is
379  * reachable via any route (except default) in the routing table.
380  * These two are a measure to block forged packets. This is also
381  * commonly known as "anti-spoofing" or Unicast Reverse Path
382  * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs
383  * is purposely reminiscent of the Cisco IOS command,
384  *
385  *   ip verify unicast reverse-path
386  *   ip verify unicast source reachable-via any
387  *
388  * which implements the same functionality. But note that the syntax
389  * is misleading, and the check may be performed on all IP packets
390  * whether unicast, multicast, or broadcast.
391  */
392 static int
393 verify_path(struct in_addr src, struct ifnet *ifp, u_int fib)
394 {
395 #ifndef __FreeBSD__
396         return 0;
397 #else
398         struct route ro;
399         struct sockaddr_in *dst;
400
401         bzero(&ro, sizeof(ro));
402
403         dst = (struct sockaddr_in *)&(ro.ro_dst);
404         dst->sin_family = AF_INET;
405         dst->sin_len = sizeof(*dst);
406         dst->sin_addr = src;
407         in_rtalloc_ign(&ro, 0, fib);
408
409         if (ro.ro_rt == NULL)
410                 return 0;
411
412         /*
413          * If ifp is provided, check for equality with rtentry.
414          * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
415          * in order to pass packets injected back by if_simloop():
416          * if useloopback == 1 routing entry (via lo0) for our own address
417          * may exist, so we need to handle routing assymetry.
418          */
419         if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) {
420                 RTFREE(ro.ro_rt);
421                 return 0;
422         }
423
424         /* if no ifp provided, check if rtentry is not default route */
425         if (ifp == NULL &&
426              satosin(rt_key(ro.ro_rt))->sin_addr.s_addr == INADDR_ANY) {
427                 RTFREE(ro.ro_rt);
428                 return 0;
429         }
430
431         /* or if this is a blackhole/reject route */
432         if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
433                 RTFREE(ro.ro_rt);
434                 return 0;
435         }
436
437         /* found valid route */
438         RTFREE(ro.ro_rt);
439         return 1;
440 #endif /* __FreeBSD__ */
441 }
442
443 #ifdef INET6
444 /*
445  * ipv6 specific rules here...
446  */
447 static __inline int
448 icmp6type_match (int type, ipfw_insn_u32 *cmd)
449 {
450         return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) );
451 }
452
453 static int
454 flow6id_match( int curr_flow, ipfw_insn_u32 *cmd )
455 {
456         int i;
457         for (i=0; i <= cmd->o.arg1; ++i )
458                 if (curr_flow == cmd->d[i] )
459                         return 1;
460         return 0;
461 }
462
463 /* support for IP6_*_ME opcodes */
464 static int
465 search_ip6_addr_net (struct in6_addr * ip6_addr)
466 {
467         struct ifnet *mdc;
468         struct ifaddr *mdc2;
469         struct in6_ifaddr *fdm;
470         struct in6_addr copia;
471
472         TAILQ_FOREACH(mdc, &V_ifnet, if_link) {
473                 if_addr_rlock(mdc);
474                 TAILQ_FOREACH(mdc2, &mdc->if_addrhead, ifa_link) {
475                         if (mdc2->ifa_addr->sa_family == AF_INET6) {
476                                 fdm = (struct in6_ifaddr *)mdc2;
477                                 copia = fdm->ia_addr.sin6_addr;
478                                 /* need for leaving scope_id in the sock_addr */
479                                 in6_clearscope(&copia);
480                                 if (IN6_ARE_ADDR_EQUAL(ip6_addr, &copia)) {
481                                         if_addr_runlock(mdc);
482                                         return 1;
483                                 }
484                         }
485                 }
486                 if_addr_runlock(mdc);
487         }
488         return 0;
489 }
490
491 static int
492 verify_path6(struct in6_addr *src, struct ifnet *ifp)
493 {
494         struct route_in6 ro;
495         struct sockaddr_in6 *dst;
496
497         bzero(&ro, sizeof(ro));
498
499         dst = (struct sockaddr_in6 * )&(ro.ro_dst);
500         dst->sin6_family = AF_INET6;
501         dst->sin6_len = sizeof(*dst);
502         dst->sin6_addr = *src;
503         /* XXX MRT 0 for ipv6 at this time */
504         rtalloc_ign((struct route *)&ro, 0);
505
506         if (ro.ro_rt == NULL)
507                 return 0;
508
509         /* 
510          * if ifp is provided, check for equality with rtentry
511          * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
512          * to support the case of sending packets to an address of our own.
513          * (where the former interface is the first argument of if_simloop()
514          *  (=ifp), the latter is lo0)
515          */
516         if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) {
517                 RTFREE(ro.ro_rt);
518                 return 0;
519         }
520
521         /* if no ifp provided, check if rtentry is not default route */
522         if (ifp == NULL &&
523             IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(ro.ro_rt))->sin6_addr)) {
524                 RTFREE(ro.ro_rt);
525                 return 0;
526         }
527
528         /* or if this is a blackhole/reject route */
529         if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
530                 RTFREE(ro.ro_rt);
531                 return 0;
532         }
533
534         /* found valid route */
535         RTFREE(ro.ro_rt);
536         return 1;
537
538 }
539
540 static int
541 is_icmp6_query(int icmp6_type)
542 {
543         if ((icmp6_type <= ICMP6_MAXTYPE) &&
544             (icmp6_type == ICMP6_ECHO_REQUEST ||
545             icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
546             icmp6_type == ICMP6_WRUREQUEST ||
547             icmp6_type == ICMP6_FQDN_QUERY ||
548             icmp6_type == ICMP6_NI_QUERY))
549                 return (1);
550
551         return (0);
552 }
553
554 static void
555 send_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6)
556 {
557         struct mbuf *m;
558
559         m = args->m;
560         if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) {
561                 struct tcphdr *tcp;
562                 tcp = (struct tcphdr *)((char *)ip6 + hlen);
563
564                 if ((tcp->th_flags & TH_RST) == 0) {
565                         struct mbuf *m0;
566                         m0 = ipfw_send_pkt(args->m, &(args->f_id),
567                             ntohl(tcp->th_seq), ntohl(tcp->th_ack),
568                             tcp->th_flags | TH_RST);
569                         if (m0 != NULL)
570                                 ip6_output(m0, NULL, NULL, 0, NULL, NULL,
571                                     NULL);
572                 }
573                 FREE_PKT(m);
574         } else if (code != ICMP6_UNREACH_RST) { /* Send an ICMPv6 unreach. */
575 #if 0
576                 /*
577                  * Unlike above, the mbufs need to line up with the ip6 hdr,
578                  * as the contents are read. We need to m_adj() the
579                  * needed amount.
580                  * The mbuf will however be thrown away so we can adjust it.
581                  * Remember we did an m_pullup on it already so we
582                  * can make some assumptions about contiguousness.
583                  */
584                 if (args->L3offset)
585                         m_adj(m, args->L3offset);
586 #endif
587                 icmp6_error(m, ICMP6_DST_UNREACH, code, 0);
588         } else
589                 FREE_PKT(m);
590
591         args->m = NULL;
592 }
593
594 #endif /* INET6 */
595
596
597 /*
598  * sends a reject message, consuming the mbuf passed as an argument.
599  */
600 static void
601 send_reject(struct ip_fw_args *args, int code, int iplen, struct ip *ip)
602 {
603
604 #if 0
605         /* XXX When ip is not guaranteed to be at mtod() we will
606          * need to account for this */
607          * The mbuf will however be thrown away so we can adjust it.
608          * Remember we did an m_pullup on it already so we
609          * can make some assumptions about contiguousness.
610          */
611         if (args->L3offset)
612                 m_adj(m, args->L3offset);
613 #endif
614         if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */
615                 /* We need the IP header in host order for icmp_error(). */
616                 SET_HOST_IPLEN(ip);
617                 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
618         } else if (args->f_id.proto == IPPROTO_TCP) {
619                 struct tcphdr *const tcp =
620                     L3HDR(struct tcphdr, mtod(args->m, struct ip *));
621                 if ( (tcp->th_flags & TH_RST) == 0) {
622                         struct mbuf *m;
623                         m = ipfw_send_pkt(args->m, &(args->f_id),
624                                 ntohl(tcp->th_seq), ntohl(tcp->th_ack),
625                                 tcp->th_flags | TH_RST);
626                         if (m != NULL)
627                                 ip_output(m, NULL, NULL, 0, NULL, NULL);
628                 }
629                 FREE_PKT(args->m);
630         } else
631                 FREE_PKT(args->m);
632         args->m = NULL;
633 }
634
635 /*
636  * Support for uid/gid/jail lookup. These tests are expensive
637  * (because we may need to look into the list of active sockets)
638  * so we cache the results. ugid_lookupp is 0 if we have not
639  * yet done a lookup, 1 if we succeeded, and -1 if we tried
640  * and failed. The function always returns the match value.
641  * We could actually spare the variable and use *uc, setting
642  * it to '(void *)check_uidgid if we have no info, NULL if
643  * we tried and failed, or any other value if successful.
644  */
645 static int
646 check_uidgid(ipfw_insn_u32 *insn, int proto, struct ifnet *oif,
647     struct in_addr dst_ip, u_int16_t dst_port, struct in_addr src_ip,
648     u_int16_t src_port, int *ugid_lookupp,
649     struct ucred **uc, struct inpcb *inp)
650 {
651 #ifndef __FreeBSD__
652         return cred_check(insn, proto, oif,
653             dst_ip, dst_port, src_ip, src_port,
654             (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb);
655 #else  /* FreeBSD */
656         struct inpcbinfo *pi;
657         int wildcard;
658         struct inpcb *pcb;
659         int match;
660
661         /*
662          * Check to see if the UDP or TCP stack supplied us with
663          * the PCB. If so, rather then holding a lock and looking
664          * up the PCB, we can use the one that was supplied.
665          */
666         if (inp && *ugid_lookupp == 0) {
667                 INP_LOCK_ASSERT(inp);
668                 if (inp->inp_socket != NULL) {
669                         *uc = crhold(inp->inp_cred);
670                         *ugid_lookupp = 1;
671                 } else
672                         *ugid_lookupp = -1;
673         }
674         /*
675          * If we have already been here and the packet has no
676          * PCB entry associated with it, then we can safely
677          * assume that this is a no match.
678          */
679         if (*ugid_lookupp == -1)
680                 return (0);
681         if (proto == IPPROTO_TCP) {
682                 wildcard = 0;
683                 pi = &V_tcbinfo;
684         } else if (proto == IPPROTO_UDP) {
685                 wildcard = INPLOOKUP_WILDCARD;
686                 pi = &V_udbinfo;
687         } else
688                 return 0;
689         match = 0;
690         if (*ugid_lookupp == 0) {
691                 INP_INFO_RLOCK(pi);
692                 pcb =  (oif) ?
693                         in_pcblookup_hash(pi,
694                                 dst_ip, htons(dst_port),
695                                 src_ip, htons(src_port),
696                                 wildcard, oif) :
697                         in_pcblookup_hash(pi,
698                                 src_ip, htons(src_port),
699                                 dst_ip, htons(dst_port),
700                                 wildcard, NULL);
701                 if (pcb != NULL) {
702                         *uc = crhold(pcb->inp_cred);
703                         *ugid_lookupp = 1;
704                 }
705                 INP_INFO_RUNLOCK(pi);
706                 if (*ugid_lookupp == 0) {
707                         /*
708                          * We tried and failed, set the variable to -1
709                          * so we will not try again on this packet.
710                          */
711                         *ugid_lookupp = -1;
712                         return (0);
713                 }
714         } 
715         if (insn->o.opcode == O_UID)
716                 match = ((*uc)->cr_uid == (uid_t)insn->d[0]);
717         else if (insn->o.opcode == O_GID)
718                 match = groupmember((gid_t)insn->d[0], *uc);
719         else if (insn->o.opcode == O_JAIL)
720                 match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]);
721         return match;
722 #endif /* __FreeBSD__ */
723 }
724
725 /*
726  * Helper function to set args with info on the rule after the matching
727  * one. slot is precise, whereas we guess rule_id as they are
728  * assigned sequentially.
729  */
730 static inline void
731 set_match(struct ip_fw_args *args, int slot,
732         struct ip_fw_chain *chain)
733 {
734         args->rule.chain_id = chain->id;
735         args->rule.slot = slot + 1; /* we use 0 as a marker */
736         args->rule.rule_id = 1 + chain->map[slot]->id;
737         args->rule.rulenum = chain->map[slot]->rulenum;
738 }
739
740 /*
741  * The main check routine for the firewall.
742  *
743  * All arguments are in args so we can modify them and return them
744  * back to the caller.
745  *
746  * Parameters:
747  *
748  *      args->m (in/out) The packet; we set to NULL when/if we nuke it.
749  *              Starts with the IP header.
750  *      args->eh (in)   Mac header if present, NULL for layer3 packet.
751  *      args->L3offset  Number of bytes bypassed if we came from L2.
752  *                      e.g. often sizeof(eh)  ** NOTYET **
753  *      args->oif       Outgoing interface, NULL if packet is incoming.
754  *              The incoming interface is in the mbuf. (in)
755  *      args->divert_rule (in/out)
756  *              Skip up to the first rule past this rule number;
757  *              upon return, non-zero port number for divert or tee.
758  *
759  *      args->rule      Pointer to the last matching rule (in/out)
760  *      args->next_hop  Socket we are forwarding to (out).
761  *      args->f_id      Addresses grabbed from the packet (out)
762  *      args->rule.info a cookie depending on rule action
763  *
764  * Return value:
765  *
766  *      IP_FW_PASS      the packet must be accepted
767  *      IP_FW_DENY      the packet must be dropped
768  *      IP_FW_DIVERT    divert packet, port in m_tag
769  *      IP_FW_TEE       tee packet, port in m_tag
770  *      IP_FW_DUMMYNET  to dummynet, pipe in args->cookie
771  *      IP_FW_NETGRAPH  into netgraph, cookie args->cookie
772  *              args->rule contains the matching rule,
773  *              args->rule.info has additional information.
774  *
775  */
776 int
777 ipfw_chk(struct ip_fw_args *args)
778 {
779
780         /*
781          * Local variables holding state while processing a packet:
782          *
783          * IMPORTANT NOTE: to speed up the processing of rules, there
784          * are some assumption on the values of the variables, which
785          * are documented here. Should you change them, please check
786          * the implementation of the various instructions to make sure
787          * that they still work.
788          *
789          * args->eh     The MAC header. It is non-null for a layer2
790          *      packet, it is NULL for a layer-3 packet.
791          * **notyet**
792          * args->L3offset Offset in the packet to the L3 (IP or equiv.) header.
793          *
794          * m | args->m  Pointer to the mbuf, as received from the caller.
795          *      It may change if ipfw_chk() does an m_pullup, or if it
796          *      consumes the packet because it calls send_reject().
797          *      XXX This has to change, so that ipfw_chk() never modifies
798          *      or consumes the buffer.
799          * ip   is the beginning of the ip(4 or 6) header.
800          *      Calculated by adding the L3offset to the start of data.
801          *      (Until we start using L3offset, the packet is
802          *      supposed to start with the ip header).
803          */
804         struct mbuf *m = args->m;
805         struct ip *ip = mtod(m, struct ip *);
806
807         /*
808          * For rules which contain uid/gid or jail constraints, cache
809          * a copy of the users credentials after the pcb lookup has been
810          * executed. This will speed up the processing of rules with
811          * these types of constraints, as well as decrease contention
812          * on pcb related locks.
813          */
814 #ifndef __FreeBSD__
815         struct bsd_ucred ucred_cache;
816 #else
817         struct ucred *ucred_cache = NULL;
818 #endif
819         int ucred_lookup = 0;
820
821         /*
822          * oif | args->oif      If NULL, ipfw_chk has been called on the
823          *      inbound path (ether_input, ip_input).
824          *      If non-NULL, ipfw_chk has been called on the outbound path
825          *      (ether_output, ip_output).
826          */
827         struct ifnet *oif = args->oif;
828
829         int f_pos = 0;          /* index of current rule in the array */
830         int retval = 0;
831
832         /*
833          * hlen The length of the IP header.
834          */
835         u_int hlen = 0;         /* hlen >0 means we have an IP pkt */
836
837         /*
838          * offset       The offset of a fragment. offset != 0 means that
839          *      we have a fragment at this offset of an IPv4 packet.
840          *      offset == 0 means that (if this is an IPv4 packet)
841          *      this is the first or only fragment.
842          *      For IPv6 offset == 0 means there is no Fragment Header. 
843          *      If offset != 0 for IPv6 always use correct mask to
844          *      get the correct offset because we add IP6F_MORE_FRAG
845          *      to be able to dectect the first fragment which would
846          *      otherwise have offset = 0.
847          */
848         u_short offset = 0;
849
850         /*
851          * Local copies of addresses. They are only valid if we have
852          * an IP packet.
853          *
854          * proto        The protocol. Set to 0 for non-ip packets,
855          *      or to the protocol read from the packet otherwise.
856          *      proto != 0 means that we have an IPv4 packet.
857          *
858          * src_port, dst_port   port numbers, in HOST format. Only
859          *      valid for TCP and UDP packets.
860          *
861          * src_ip, dst_ip       ip addresses, in NETWORK format.
862          *      Only valid for IPv4 packets.
863          */
864         uint8_t proto;
865         uint16_t src_port = 0, dst_port = 0;    /* NOTE: host format    */
866         struct in_addr src_ip, dst_ip;          /* NOTE: network format */
867         uint16_t iplen=0;
868         int pktlen;
869         uint16_t        etype = 0;      /* Host order stored ether type */
870
871         /*
872          * dyn_dir = MATCH_UNKNOWN when rules unchecked,
873          *      MATCH_NONE when checked and not matched (q = NULL),
874          *      MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
875          */
876         int dyn_dir = MATCH_UNKNOWN;
877         ipfw_dyn_rule *q = NULL;
878         struct ip_fw_chain *chain = &V_layer3_chain;
879
880         /*
881          * We store in ulp a pointer to the upper layer protocol header.
882          * In the ipv4 case this is easy to determine from the header,
883          * but for ipv6 we might have some additional headers in the middle.
884          * ulp is NULL if not found.
885          */
886         void *ulp = NULL;               /* upper layer protocol pointer. */
887
888         /* XXX ipv6 variables */
889         int is_ipv6 = 0;
890         uint8_t icmp6_type = 0;
891         uint16_t ext_hd = 0;    /* bits vector for extension header filtering */
892         /* end of ipv6 variables */
893
894         int is_ipv4 = 0;
895
896         int done = 0;           /* flag to exit the outer loop */
897
898         if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready))
899                 return (IP_FW_PASS);    /* accept */
900
901         dst_ip.s_addr = 0;              /* make sure it is initialized */
902         src_ip.s_addr = 0;              /* make sure it is initialized */
903         pktlen = m->m_pkthdr.len;
904         args->f_id.fib = M_GETFIB(m); /* note mbuf not altered) */
905         proto = args->f_id.proto = 0;   /* mark f_id invalid */
906                 /* XXX 0 is a valid proto: IP/IPv6 Hop-by-Hop Option */
907
908 /*
909  * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous,
910  * then it sets p to point at the offset "len" in the mbuf. WARNING: the
911  * pointer might become stale after other pullups (but we never use it
912  * this way).
913  */
914 #define PULLUP_TO(_len, p, T)                                   \
915 do {                                                            \
916         int x = (_len) + sizeof(T);                             \
917         if ((m)->m_len < x) {                                   \
918                 args->m = m = m_pullup(m, x);                   \
919                 if (m == NULL)                                  \
920                         goto pullup_failed;                     \
921         }                                                       \
922         p = (mtod(m, char *) + (_len));                         \
923 } while (0)
924
925         /*
926          * if we have an ether header,
927          */
928         if (args->eh)
929                 etype = ntohs(args->eh->ether_type);
930
931         /* Identify IP packets and fill up variables. */
932         if (pktlen >= sizeof(struct ip6_hdr) &&
933             (args->eh == NULL || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) {
934                 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
935                 is_ipv6 = 1;
936                 args->f_id.addr_type = 6;
937                 hlen = sizeof(struct ip6_hdr);
938                 proto = ip6->ip6_nxt;
939
940                 /* Search extension headers to find upper layer protocols */
941                 while (ulp == NULL) {
942                         switch (proto) {
943                         case IPPROTO_ICMPV6:
944                                 PULLUP_TO(hlen, ulp, struct icmp6_hdr);
945                                 icmp6_type = ICMP6(ulp)->icmp6_type;
946                                 break;
947
948                         case IPPROTO_TCP:
949                                 PULLUP_TO(hlen, ulp, struct tcphdr);
950                                 dst_port = TCP(ulp)->th_dport;
951                                 src_port = TCP(ulp)->th_sport;
952                                 /* save flags for dynamic rules */
953                                 args->f_id._flags = TCP(ulp)->th_flags;
954                                 break;
955
956                         case IPPROTO_SCTP:
957                                 PULLUP_TO(hlen, ulp, struct sctphdr);
958                                 src_port = SCTP(ulp)->src_port;
959                                 dst_port = SCTP(ulp)->dest_port;
960                                 break;
961
962                         case IPPROTO_UDP:
963                                 PULLUP_TO(hlen, ulp, struct udphdr);
964                                 dst_port = UDP(ulp)->uh_dport;
965                                 src_port = UDP(ulp)->uh_sport;
966                                 break;
967
968                         case IPPROTO_HOPOPTS:   /* RFC 2460 */
969                                 PULLUP_TO(hlen, ulp, struct ip6_hbh);
970                                 ext_hd |= EXT_HOPOPTS;
971                                 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
972                                 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
973                                 ulp = NULL;
974                                 break;
975
976                         case IPPROTO_ROUTING:   /* RFC 2460 */
977                                 PULLUP_TO(hlen, ulp, struct ip6_rthdr);
978                                 switch (((struct ip6_rthdr *)ulp)->ip6r_type) {
979                                 case 0:
980                                         ext_hd |= EXT_RTHDR0;
981                                         break;
982                                 case 2:
983                                         ext_hd |= EXT_RTHDR2;
984                                         break;
985                                 default:
986                                         printf("IPFW2: IPV6 - Unknown Routing "
987                                             "Header type(%d)\n",
988                                             ((struct ip6_rthdr *)ulp)->ip6r_type);
989                                         if (V_fw_deny_unknown_exthdrs)
990                                             return (IP_FW_DENY);
991                                         break;
992                                 }
993                                 ext_hd |= EXT_ROUTING;
994                                 hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3;
995                                 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt;
996                                 ulp = NULL;
997                                 break;
998
999                         case IPPROTO_FRAGMENT:  /* RFC 2460 */
1000                                 PULLUP_TO(hlen, ulp, struct ip6_frag);
1001                                 ext_hd |= EXT_FRAGMENT;
1002                                 hlen += sizeof (struct ip6_frag);
1003                                 proto = ((struct ip6_frag *)ulp)->ip6f_nxt;
1004                                 offset = ((struct ip6_frag *)ulp)->ip6f_offlg &
1005                                         IP6F_OFF_MASK;
1006                                 /* Add IP6F_MORE_FRAG for offset of first
1007                                  * fragment to be != 0. */
1008                                 offset |= ((struct ip6_frag *)ulp)->ip6f_offlg &
1009                                         IP6F_MORE_FRAG;
1010                                 if (offset == 0) {
1011                                         printf("IPFW2: IPV6 - Invalid Fragment "
1012                                             "Header\n");
1013                                         if (V_fw_deny_unknown_exthdrs)
1014                                             return (IP_FW_DENY);
1015                                         break;
1016                                 }
1017                                 args->f_id.extra =
1018                                     ntohl(((struct ip6_frag *)ulp)->ip6f_ident);
1019                                 ulp = NULL;
1020                                 break;
1021
1022                         case IPPROTO_DSTOPTS:   /* RFC 2460 */
1023                                 PULLUP_TO(hlen, ulp, struct ip6_hbh);
1024                                 ext_hd |= EXT_DSTOPTS;
1025                                 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1026                                 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1027                                 ulp = NULL;
1028                                 break;
1029
1030                         case IPPROTO_AH:        /* RFC 2402 */
1031                                 PULLUP_TO(hlen, ulp, struct ip6_ext);
1032                                 ext_hd |= EXT_AH;
1033                                 hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2;
1034                                 proto = ((struct ip6_ext *)ulp)->ip6e_nxt;
1035                                 ulp = NULL;
1036                                 break;
1037
1038                         case IPPROTO_ESP:       /* RFC 2406 */
1039                                 PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */
1040                                 /* Anything past Seq# is variable length and
1041                                  * data past this ext. header is encrypted. */
1042                                 ext_hd |= EXT_ESP;
1043                                 break;
1044
1045                         case IPPROTO_NONE:      /* RFC 2460 */
1046                                 /*
1047                                  * Packet ends here, and IPv6 header has
1048                                  * already been pulled up. If ip6e_len!=0
1049                                  * then octets must be ignored.
1050                                  */
1051                                 ulp = ip; /* non-NULL to get out of loop. */
1052                                 break;
1053
1054                         case IPPROTO_OSPFIGP:
1055                                 /* XXX OSPF header check? */
1056                                 PULLUP_TO(hlen, ulp, struct ip6_ext);
1057                                 break;
1058
1059                         case IPPROTO_PIM:
1060                                 /* XXX PIM header check? */
1061                                 PULLUP_TO(hlen, ulp, struct pim);
1062                                 break;
1063
1064                         case IPPROTO_CARP:
1065                                 PULLUP_TO(hlen, ulp, struct carp_header);
1066                                 if (((struct carp_header *)ulp)->carp_version !=
1067                                     CARP_VERSION) 
1068                                         return (IP_FW_DENY);
1069                                 if (((struct carp_header *)ulp)->carp_type !=
1070                                     CARP_ADVERTISEMENT) 
1071                                         return (IP_FW_DENY);
1072                                 break;
1073
1074                         case IPPROTO_IPV6:      /* RFC 2893 */
1075                                 PULLUP_TO(hlen, ulp, struct ip6_hdr);
1076                                 break;
1077
1078                         case IPPROTO_IPV4:      /* RFC 2893 */
1079                                 PULLUP_TO(hlen, ulp, struct ip);
1080                                 break;
1081
1082                         default:
1083                                 printf("IPFW2: IPV6 - Unknown Extension "
1084                                     "Header(%d), ext_hd=%x\n", proto, ext_hd);
1085                                 if (V_fw_deny_unknown_exthdrs)
1086                                     return (IP_FW_DENY);
1087                                 PULLUP_TO(hlen, ulp, struct ip6_ext);
1088                                 break;
1089                         } /*switch */
1090                 }
1091                 ip = mtod(m, struct ip *);
1092                 ip6 = (struct ip6_hdr *)ip;
1093                 args->f_id.src_ip6 = ip6->ip6_src;
1094                 args->f_id.dst_ip6 = ip6->ip6_dst;
1095                 args->f_id.src_ip = 0;
1096                 args->f_id.dst_ip = 0;
1097                 args->f_id.flow_id6 = ntohl(ip6->ip6_flow);
1098         } else if (pktlen >= sizeof(struct ip) &&
1099             (args->eh == NULL || etype == ETHERTYPE_IP) && ip->ip_v == 4) {
1100                 is_ipv4 = 1;
1101                 hlen = ip->ip_hl << 2;
1102                 args->f_id.addr_type = 4;
1103
1104                 /*
1105                  * Collect parameters into local variables for faster matching.
1106                  */
1107                 proto = ip->ip_p;
1108                 src_ip = ip->ip_src;
1109                 dst_ip = ip->ip_dst;
1110                 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1111                 iplen = ntohs(ip->ip_len);
1112                 pktlen = iplen < pktlen ? iplen : pktlen;
1113
1114                 if (offset == 0) {
1115                         switch (proto) {
1116                         case IPPROTO_TCP:
1117                                 PULLUP_TO(hlen, ulp, struct tcphdr);
1118                                 dst_port = TCP(ulp)->th_dport;
1119                                 src_port = TCP(ulp)->th_sport;
1120                                 /* save flags for dynamic rules */
1121                                 args->f_id._flags = TCP(ulp)->th_flags;
1122                                 break;
1123
1124                         case IPPROTO_UDP:
1125                                 PULLUP_TO(hlen, ulp, struct udphdr);
1126                                 dst_port = UDP(ulp)->uh_dport;
1127                                 src_port = UDP(ulp)->uh_sport;
1128                                 break;
1129
1130                         case IPPROTO_ICMP:
1131                                 PULLUP_TO(hlen, ulp, struct icmphdr);
1132                                 //args->f_id.flags = ICMP(ulp)->icmp_type;
1133                                 break;
1134
1135                         default:
1136                                 break;
1137                         }
1138                 }
1139
1140                 ip = mtod(m, struct ip *);
1141                 args->f_id.src_ip = ntohl(src_ip.s_addr);
1142                 args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1143         }
1144 #undef PULLUP_TO
1145         if (proto) { /* we may have port numbers, store them */
1146                 args->f_id.proto = proto;
1147                 args->f_id.src_port = src_port = ntohs(src_port);
1148                 args->f_id.dst_port = dst_port = ntohs(dst_port);
1149         }
1150
1151         IPFW_RLOCK(chain);
1152         if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */
1153                 IPFW_RUNLOCK(chain);
1154                 return (IP_FW_PASS);    /* accept */
1155         }
1156         if (args->rule.slot) {
1157                 /*
1158                  * Packet has already been tagged as a result of a previous
1159                  * match on rule args->rule aka args->rule_id (PIPE, QUEUE,
1160                  * REASS, NETGRAPH, DIVERT/TEE...)
1161                  * Validate the slot and continue from the next one
1162                  * if still present, otherwise do a lookup.
1163                  */
1164                 f_pos = (args->rule.chain_id == chain->id) ?
1165                     args->rule.slot :
1166                     ipfw_find_rule(chain, args->rule.rulenum,
1167                         args->rule.rule_id);
1168         } else {
1169                 f_pos = 0;
1170         }
1171
1172         /*
1173          * Now scan the rules, and parse microinstructions for each rule.
1174          * We have two nested loops and an inner switch. Sometimes we
1175          * need to break out of one or both loops, or re-enter one of
1176          * the loops with updated variables. Loop variables are:
1177          *
1178          *      f_pos (outer loop) points to the current rule.
1179          *              On output it points to the matching rule.
1180          *      done (outer loop) is used as a flag to break the loop.
1181          *      l (inner loop)  residual length of current rule.
1182          *              cmd points to the current microinstruction.
1183          *
1184          * We break the inner loop by setting l=0 and possibly
1185          * cmdlen=0 if we don't want to advance cmd.
1186          * We break the outer loop by setting done=1
1187          * We can restart the inner loop by setting l>0 and f_pos, f, cmd
1188          * as needed.
1189          */
1190         for (; f_pos < chain->n_rules; f_pos++) {
1191                 ipfw_insn *cmd;
1192                 uint32_t tablearg = 0;
1193                 int l, cmdlen, skip_or; /* skip rest of OR block */
1194                 struct ip_fw *f;
1195
1196                 f = chain->map[f_pos];
1197                 if (V_set_disable & (1 << f->set) )
1198                         continue;
1199
1200                 skip_or = 0;
1201                 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
1202                     l -= cmdlen, cmd += cmdlen) {
1203                         int match;
1204
1205                         /*
1206                          * check_body is a jump target used when we find a
1207                          * CHECK_STATE, and need to jump to the body of
1208                          * the target rule.
1209                          */
1210
1211 /* check_body: */
1212                         cmdlen = F_LEN(cmd);
1213                         /*
1214                          * An OR block (insn_1 || .. || insn_n) has the
1215                          * F_OR bit set in all but the last instruction.
1216                          * The first match will set "skip_or", and cause
1217                          * the following instructions to be skipped until
1218                          * past the one with the F_OR bit clear.
1219                          */
1220                         if (skip_or) {          /* skip this instruction */
1221                                 if ((cmd->len & F_OR) == 0)
1222                                         skip_or = 0;    /* next one is good */
1223                                 continue;
1224                         }
1225                         match = 0; /* set to 1 if we succeed */
1226
1227                         switch (cmd->opcode) {
1228                         /*
1229                          * The first set of opcodes compares the packet's
1230                          * fields with some pattern, setting 'match' if a
1231                          * match is found. At the end of the loop there is
1232                          * logic to deal with F_NOT and F_OR flags associated
1233                          * with the opcode.
1234                          */
1235                         case O_NOP:
1236                                 match = 1;
1237                                 break;
1238
1239                         case O_FORWARD_MAC:
1240                                 printf("ipfw: opcode %d unimplemented\n",
1241                                     cmd->opcode);
1242                                 break;
1243
1244                         case O_GID:
1245                         case O_UID:
1246                         case O_JAIL:
1247                                 /*
1248                                  * We only check offset == 0 && proto != 0,
1249                                  * as this ensures that we have a
1250                                  * packet with the ports info.
1251                                  */
1252                                 if (offset!=0)
1253                                         break;
1254                                 if (is_ipv6) /* XXX to be fixed later */
1255                                         break;
1256                                 if (proto == IPPROTO_TCP ||
1257                                     proto == IPPROTO_UDP)
1258                                         match = check_uidgid(
1259                                                     (ipfw_insn_u32 *)cmd,
1260                                                     proto, oif,
1261                                                     dst_ip, dst_port,
1262                                                     src_ip, src_port, &ucred_lookup,
1263 #ifdef __FreeBSD__
1264                                                     &ucred_cache, args->inp);
1265 #else
1266                                                     (void *)&ucred_cache,
1267                                                     (struct inpcb *)args->m);
1268 #endif
1269                                 break;
1270
1271                         case O_RECV:
1272                                 match = iface_match(m->m_pkthdr.rcvif,
1273                                     (ipfw_insn_if *)cmd);
1274                                 break;
1275
1276                         case O_XMIT:
1277                                 match = iface_match(oif, (ipfw_insn_if *)cmd);
1278                                 break;
1279
1280                         case O_VIA:
1281                                 match = iface_match(oif ? oif :
1282                                     m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd);
1283                                 break;
1284
1285                         case O_MACADDR2:
1286                                 if (args->eh != NULL) { /* have MAC header */
1287                                         u_int32_t *want = (u_int32_t *)
1288                                                 ((ipfw_insn_mac *)cmd)->addr;
1289                                         u_int32_t *mask = (u_int32_t *)
1290                                                 ((ipfw_insn_mac *)cmd)->mask;
1291                                         u_int32_t *hdr = (u_int32_t *)args->eh;
1292
1293                                         match =
1294                                             ( want[0] == (hdr[0] & mask[0]) &&
1295                                               want[1] == (hdr[1] & mask[1]) &&
1296                                               want[2] == (hdr[2] & mask[2]) );
1297                                 }
1298                                 break;
1299
1300                         case O_MAC_TYPE:
1301                                 if (args->eh != NULL) {
1302                                         u_int16_t *p =
1303                                             ((ipfw_insn_u16 *)cmd)->ports;
1304                                         int i;
1305
1306                                         for (i = cmdlen - 1; !match && i>0;
1307                                             i--, p += 2)
1308                                                 match = (etype >= p[0] &&
1309                                                     etype <= p[1]);
1310                                 }
1311                                 break;
1312
1313                         case O_FRAG:
1314                                 match = (offset != 0);
1315                                 break;
1316
1317                         case O_IN:      /* "out" is "not in" */
1318                                 match = (oif == NULL);
1319                                 break;
1320
1321                         case O_LAYER2:
1322                                 match = (args->eh != NULL);
1323                                 break;
1324
1325                         case O_DIVERTED:
1326                             {
1327                                 /* For diverted packets, args->rule.info
1328                                  * contains the divert port (in host format)
1329                                  * reason and direction.
1330                                  */
1331                                 uint32_t i = args->rule.info;
1332                                 match = (i&IPFW_IS_MASK) == IPFW_IS_DIVERT &&
1333                                     cmd->arg1 & ((i & IPFW_INFO_IN) ? 1 : 2);
1334                             }
1335                                 break;
1336
1337                         case O_PROTO:
1338                                 /*
1339                                  * We do not allow an arg of 0 so the
1340                                  * check of "proto" only suffices.
1341                                  */
1342                                 match = (proto == cmd->arg1);
1343                                 break;
1344
1345                         case O_IP_SRC:
1346                                 match = is_ipv4 &&
1347                                     (((ipfw_insn_ip *)cmd)->addr.s_addr ==
1348                                     src_ip.s_addr);
1349                                 break;
1350
1351                         case O_IP_SRC_LOOKUP:
1352                         case O_IP_DST_LOOKUP:
1353                                 if (is_ipv4) {
1354                                     uint32_t key =
1355                                         (cmd->opcode == O_IP_DST_LOOKUP) ?
1356                                             dst_ip.s_addr : src_ip.s_addr;
1357                                     uint32_t v = 0;
1358
1359                                     if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) {
1360                                         /* generic lookup. The key must be
1361                                          * in 32bit big-endian format.
1362                                          */
1363                                         v = ((ipfw_insn_u32 *)cmd)->d[1];
1364                                         if (v == 0)
1365                                             key = dst_ip.s_addr;
1366                                         else if (v == 1)
1367                                             key = src_ip.s_addr;
1368                                         else if (v == 6) /* dscp */
1369                                             key = (ip->ip_tos >> 2) & 0x3f;
1370                                         else if (offset != 0)
1371                                             break;
1372                                         else if (proto != IPPROTO_TCP &&
1373                                                 proto != IPPROTO_UDP)
1374                                             break;
1375                                         else if (v == 2)
1376                                             key = htonl(dst_port);
1377                                         else if (v == 3)
1378                                             key = htonl(src_port);
1379                                         else if (v == 4 || v == 5) {
1380                                             check_uidgid(
1381                                                 (ipfw_insn_u32 *)cmd,
1382                                                 proto, oif,
1383                                                 dst_ip, dst_port,
1384                                                 src_ip, src_port, &ucred_lookup,
1385 #ifdef __FreeBSD__
1386                                                 &ucred_cache, args->inp);
1387                                             if (v == 4 /* O_UID */)
1388                                                 key = ucred_cache->cr_uid;
1389                                             else if (v == 5 /* O_JAIL */)
1390                                                 key = ucred_cache->cr_prison->pr_id;
1391 #else /* !__FreeBSD__ */
1392                                                 (void *)&ucred_cache,
1393                                                 (struct inpcb *)args->m);
1394                                             if (v ==4 /* O_UID */)
1395                                                 key = ucred_cache.uid;
1396                                             else if (v == 5 /* O_JAIL */)
1397                                                 key = ucred_cache.xid;
1398 #endif /* !__FreeBSD__ */
1399                                             key = htonl(key);
1400                                         } else
1401                                             break;
1402                                     }
1403                                     match = ipfw_lookup_table(chain,
1404                                         cmd->arg1, key, &v);
1405                                     if (!match)
1406                                         break;
1407                                     if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
1408                                         match =
1409                                             ((ipfw_insn_u32 *)cmd)->d[0] == v;
1410                                     else
1411                                         tablearg = v;
1412                                 }
1413                                 break;
1414
1415                         case O_IP_SRC_MASK:
1416                         case O_IP_DST_MASK:
1417                                 if (is_ipv4) {
1418                                     uint32_t a =
1419                                         (cmd->opcode == O_IP_DST_MASK) ?
1420                                             dst_ip.s_addr : src_ip.s_addr;
1421                                     uint32_t *p = ((ipfw_insn_u32 *)cmd)->d;
1422                                     int i = cmdlen-1;
1423
1424                                     for (; !match && i>0; i-= 2, p+= 2)
1425                                         match = (p[0] == (a & p[1]));
1426                                 }
1427                                 break;
1428
1429                         case O_IP_SRC_ME:
1430                                 if (is_ipv4) {
1431                                         struct ifnet *tif;
1432
1433                                         INADDR_TO_IFP(src_ip, tif);
1434                                         match = (tif != NULL);
1435                                         break;
1436                                 }
1437 #ifdef INET6
1438                                 /* FALLTHROUGH */
1439                         case O_IP6_SRC_ME:
1440                                 match= is_ipv6 && search_ip6_addr_net(&args->f_id.src_ip6);
1441 #endif
1442                                 break;
1443
1444                         case O_IP_DST_SET:
1445                         case O_IP_SRC_SET:
1446                                 if (is_ipv4) {
1447                                         u_int32_t *d = (u_int32_t *)(cmd+1);
1448                                         u_int32_t addr =
1449                                             cmd->opcode == O_IP_DST_SET ?
1450                                                 args->f_id.dst_ip :
1451                                                 args->f_id.src_ip;
1452
1453                                             if (addr < d[0])
1454                                                     break;
1455                                             addr -= d[0]; /* subtract base */
1456                                             match = (addr < cmd->arg1) &&
1457                                                 ( d[ 1 + (addr>>5)] &
1458                                                   (1<<(addr & 0x1f)) );
1459                                 }
1460                                 break;
1461
1462                         case O_IP_DST:
1463                                 match = is_ipv4 &&
1464                                     (((ipfw_insn_ip *)cmd)->addr.s_addr ==
1465                                     dst_ip.s_addr);
1466                                 break;
1467
1468                         case O_IP_DST_ME:
1469                                 if (is_ipv4) {
1470                                         struct ifnet *tif;
1471
1472                                         INADDR_TO_IFP(dst_ip, tif);
1473                                         match = (tif != NULL);
1474                                         break;
1475                                 }
1476 #ifdef INET6
1477                                 /* FALLTHROUGH */
1478                         case O_IP6_DST_ME:
1479                                 match= is_ipv6 && search_ip6_addr_net(&args->f_id.dst_ip6);
1480 #endif
1481                                 break;
1482
1483
1484                         case O_IP_SRCPORT:
1485                         case O_IP_DSTPORT:
1486                                 /*
1487                                  * offset == 0 && proto != 0 is enough
1488                                  * to guarantee that we have a
1489                                  * packet with port info.
1490                                  */
1491                                 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP)
1492                                     && offset == 0) {
1493                                         u_int16_t x =
1494                                             (cmd->opcode == O_IP_SRCPORT) ?
1495                                                 src_port : dst_port ;
1496                                         u_int16_t *p =
1497                                             ((ipfw_insn_u16 *)cmd)->ports;
1498                                         int i;
1499
1500                                         for (i = cmdlen - 1; !match && i>0;
1501                                             i--, p += 2)
1502                                                 match = (x>=p[0] && x<=p[1]);
1503                                 }
1504                                 break;
1505
1506                         case O_ICMPTYPE:
1507                                 match = (offset == 0 && proto==IPPROTO_ICMP &&
1508                                     icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) );
1509                                 break;
1510
1511 #ifdef INET6
1512                         case O_ICMP6TYPE:
1513                                 match = is_ipv6 && offset == 0 &&
1514                                     proto==IPPROTO_ICMPV6 &&
1515                                     icmp6type_match(
1516                                         ICMP6(ulp)->icmp6_type,
1517                                         (ipfw_insn_u32 *)cmd);
1518                                 break;
1519 #endif /* INET6 */
1520
1521                         case O_IPOPT:
1522                                 match = (is_ipv4 &&
1523                                     ipopts_match(ip, cmd) );
1524                                 break;
1525
1526                         case O_IPVER:
1527                                 match = (is_ipv4 &&
1528                                     cmd->arg1 == ip->ip_v);
1529                                 break;
1530
1531                         case O_IPID:
1532                         case O_IPLEN:
1533                         case O_IPTTL:
1534                                 if (is_ipv4) {  /* only for IP packets */
1535                                     uint16_t x;
1536                                     uint16_t *p;
1537                                     int i;
1538
1539                                     if (cmd->opcode == O_IPLEN)
1540                                         x = iplen;
1541                                     else if (cmd->opcode == O_IPTTL)
1542                                         x = ip->ip_ttl;
1543                                     else /* must be IPID */
1544                                         x = ntohs(ip->ip_id);
1545                                     if (cmdlen == 1) {
1546                                         match = (cmd->arg1 == x);
1547                                         break;
1548                                     }
1549                                     /* otherwise we have ranges */
1550                                     p = ((ipfw_insn_u16 *)cmd)->ports;
1551                                     i = cmdlen - 1;
1552                                     for (; !match && i>0; i--, p += 2)
1553                                         match = (x >= p[0] && x <= p[1]);
1554                                 }
1555                                 break;
1556
1557                         case O_IPPRECEDENCE:
1558                                 match = (is_ipv4 &&
1559                                     (cmd->arg1 == (ip->ip_tos & 0xe0)) );
1560                                 break;
1561
1562                         case O_IPTOS:
1563                                 match = (is_ipv4 &&
1564                                     flags_match(cmd, ip->ip_tos));
1565                                 break;
1566
1567                         case O_TCPDATALEN:
1568                                 if (proto == IPPROTO_TCP && offset == 0) {
1569                                     struct tcphdr *tcp;
1570                                     uint16_t x;
1571                                     uint16_t *p;
1572                                     int i;
1573
1574                                     tcp = TCP(ulp);
1575                                     x = iplen -
1576                                         ((ip->ip_hl + tcp->th_off) << 2);
1577                                     if (cmdlen == 1) {
1578                                         match = (cmd->arg1 == x);
1579                                         break;
1580                                     }
1581                                     /* otherwise we have ranges */
1582                                     p = ((ipfw_insn_u16 *)cmd)->ports;
1583                                     i = cmdlen - 1;
1584                                     for (; !match && i>0; i--, p += 2)
1585                                         match = (x >= p[0] && x <= p[1]);
1586                                 }
1587                                 break;
1588
1589                         case O_TCPFLAGS:
1590                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1591                                     flags_match(cmd, TCP(ulp)->th_flags));
1592                                 break;
1593
1594                         case O_TCPOPTS:
1595                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1596                                     tcpopts_match(TCP(ulp), cmd));
1597                                 break;
1598
1599                         case O_TCPSEQ:
1600                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1601                                     ((ipfw_insn_u32 *)cmd)->d[0] ==
1602                                         TCP(ulp)->th_seq);
1603                                 break;
1604
1605                         case O_TCPACK:
1606                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1607                                     ((ipfw_insn_u32 *)cmd)->d[0] ==
1608                                         TCP(ulp)->th_ack);
1609                                 break;
1610
1611                         case O_TCPWIN:
1612                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1613                                     cmd->arg1 == TCP(ulp)->th_win);
1614                                 break;
1615
1616                         case O_ESTAB:
1617                                 /* reject packets which have SYN only */
1618                                 /* XXX should i also check for TH_ACK ? */
1619                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1620                                     (TCP(ulp)->th_flags &
1621                                      (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
1622                                 break;
1623
1624                         case O_ALTQ: {
1625                                 struct pf_mtag *at;
1626                                 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
1627
1628                                 match = 1;
1629                                 at = pf_find_mtag(m);
1630                                 if (at != NULL && at->qid != 0)
1631                                         break;
1632                                 at = pf_get_mtag(m);
1633                                 if (at == NULL) {
1634                                         /*
1635                                          * Let the packet fall back to the
1636                                          * default ALTQ.
1637                                          */
1638                                         break;
1639                                 }
1640                                 at->qid = altq->qid;
1641                                 if (is_ipv4)
1642                                         at->af = AF_INET;
1643                                 else
1644                                         at->af = AF_LINK;
1645                                 at->hdr = ip;
1646                                 break;
1647                         }
1648
1649                         case O_LOG:
1650                                 ipfw_log(f, hlen, args, m,
1651                                             oif, offset, tablearg, ip);
1652                                 match = 1;
1653                                 break;
1654
1655                         case O_PROB:
1656                                 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]);
1657                                 break;
1658
1659                         case O_VERREVPATH:
1660                                 /* Outgoing packets automatically pass/match */
1661                                 match = ((oif != NULL) ||
1662                                     (m->m_pkthdr.rcvif == NULL) ||
1663                                     (
1664 #ifdef INET6
1665                                     is_ipv6 ?
1666                                         verify_path6(&(args->f_id.src_ip6),
1667                                             m->m_pkthdr.rcvif) :
1668 #endif
1669                                     verify_path(src_ip, m->m_pkthdr.rcvif,
1670                                         args->f_id.fib)));
1671                                 break;
1672
1673                         case O_VERSRCREACH:
1674                                 /* Outgoing packets automatically pass/match */
1675                                 match = (hlen > 0 && ((oif != NULL) ||
1676 #ifdef INET6
1677                                     is_ipv6 ?
1678                                         verify_path6(&(args->f_id.src_ip6),
1679                                             NULL) :
1680 #endif
1681                                     verify_path(src_ip, NULL, args->f_id.fib)));
1682                                 break;
1683
1684                         case O_ANTISPOOF:
1685                                 /* Outgoing packets automatically pass/match */
1686                                 if (oif == NULL && hlen > 0 &&
1687                                     (  (is_ipv4 && in_localaddr(src_ip))
1688 #ifdef INET6
1689                                     || (is_ipv6 &&
1690                                         in6_localaddr(&(args->f_id.src_ip6)))
1691 #endif
1692                                     ))
1693                                         match =
1694 #ifdef INET6
1695                                             is_ipv6 ? verify_path6(
1696                                                 &(args->f_id.src_ip6),
1697                                                 m->m_pkthdr.rcvif) :
1698 #endif
1699                                             verify_path(src_ip,
1700                                                 m->m_pkthdr.rcvif,
1701                                                 args->f_id.fib);
1702                                 else
1703                                         match = 1;
1704                                 break;
1705
1706                         case O_IPSEC:
1707 #ifdef IPSEC
1708                                 match = (m_tag_find(m,
1709                                     PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL);
1710 #endif
1711                                 /* otherwise no match */
1712                                 break;
1713
1714 #ifdef INET6
1715                         case O_IP6_SRC:
1716                                 match = is_ipv6 &&
1717                                     IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6,
1718                                     &((ipfw_insn_ip6 *)cmd)->addr6);
1719                                 break;
1720
1721                         case O_IP6_DST:
1722                                 match = is_ipv6 &&
1723                                 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6,
1724                                     &((ipfw_insn_ip6 *)cmd)->addr6);
1725                                 break;
1726                         case O_IP6_SRC_MASK:
1727                         case O_IP6_DST_MASK:
1728                                 if (is_ipv6) {
1729                                         int i = cmdlen - 1;
1730                                         struct in6_addr p;
1731                                         struct in6_addr *d =
1732                                             &((ipfw_insn_ip6 *)cmd)->addr6;
1733
1734                                         for (; !match && i > 0; d += 2,
1735                                             i -= F_INSN_SIZE(struct in6_addr)
1736                                             * 2) {
1737                                                 p = (cmd->opcode ==
1738                                                     O_IP6_SRC_MASK) ?
1739                                                     args->f_id.src_ip6:
1740                                                     args->f_id.dst_ip6;
1741                                                 APPLY_MASK(&p, &d[1]);
1742                                                 match =
1743                                                     IN6_ARE_ADDR_EQUAL(&d[0],
1744                                                     &p);
1745                                         }
1746                                 }
1747                                 break;
1748
1749                         case O_FLOW6ID:
1750                                 match = is_ipv6 &&
1751                                     flow6id_match(args->f_id.flow_id6,
1752                                     (ipfw_insn_u32 *) cmd);
1753                                 break;
1754
1755                         case O_EXT_HDR:
1756                                 match = is_ipv6 &&
1757                                     (ext_hd & ((ipfw_insn *) cmd)->arg1);
1758                                 break;
1759
1760                         case O_IP6:
1761                                 match = is_ipv6;
1762                                 break;
1763 #endif
1764
1765                         case O_IP4:
1766                                 match = is_ipv4;
1767                                 break;
1768
1769                         case O_TAG: {
1770                                 struct m_tag *mtag;
1771                                 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ?
1772                                     tablearg : cmd->arg1;
1773
1774                                 /* Packet is already tagged with this tag? */
1775                                 mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL);
1776
1777                                 /* We have `untag' action when F_NOT flag is
1778                                  * present. And we must remove this mtag from
1779                                  * mbuf and reset `match' to zero (`match' will
1780                                  * be inversed later).
1781                                  * Otherwise we should allocate new mtag and
1782                                  * push it into mbuf.
1783                                  */
1784                                 if (cmd->len & F_NOT) { /* `untag' action */
1785                                         if (mtag != NULL)
1786                                                 m_tag_delete(m, mtag);
1787                                         match = 0;
1788                                 } else if (mtag == NULL) {
1789                                         if ((mtag = m_tag_alloc(MTAG_IPFW,
1790                                             tag, 0, M_NOWAIT)) != NULL)
1791                                                 m_tag_prepend(m, mtag);
1792                                         match = 1;
1793                                 }
1794                                 break;
1795                         }
1796
1797                         case O_FIB: /* try match the specified fib */
1798                                 if (args->f_id.fib == cmd->arg1)
1799                                         match = 1;
1800                                 break;
1801
1802                         case O_TAGGED: {
1803                                 struct m_tag *mtag;
1804                                 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ?
1805                                     tablearg : cmd->arg1;
1806
1807                                 if (cmdlen == 1) {
1808                                         match = m_tag_locate(m, MTAG_IPFW,
1809                                             tag, NULL) != NULL;
1810                                         break;
1811                                 }
1812
1813                                 /* we have ranges */
1814                                 for (mtag = m_tag_first(m);
1815                                     mtag != NULL && !match;
1816                                     mtag = m_tag_next(m, mtag)) {
1817                                         uint16_t *p;
1818                                         int i;
1819
1820                                         if (mtag->m_tag_cookie != MTAG_IPFW)
1821                                                 continue;
1822
1823                                         p = ((ipfw_insn_u16 *)cmd)->ports;
1824                                         i = cmdlen - 1;
1825                                         for(; !match && i > 0; i--, p += 2)
1826                                                 match =
1827                                                     mtag->m_tag_id >= p[0] &&
1828                                                     mtag->m_tag_id <= p[1];
1829                                 }
1830                                 break;
1831                         }
1832                                 
1833                         /*
1834                          * The second set of opcodes represents 'actions',
1835                          * i.e. the terminal part of a rule once the packet
1836                          * matches all previous patterns.
1837                          * Typically there is only one action for each rule,
1838                          * and the opcode is stored at the end of the rule
1839                          * (but there are exceptions -- see below).
1840                          *
1841                          * In general, here we set retval and terminate the
1842                          * outer loop (would be a 'break 3' in some language,
1843                          * but we need to set l=0, done=1)
1844                          *
1845                          * Exceptions:
1846                          * O_COUNT and O_SKIPTO actions:
1847                          *   instead of terminating, we jump to the next rule
1848                          *   (setting l=0), or to the SKIPTO target (setting
1849                          *   f/f_len, cmd and l as needed), respectively.
1850                          *
1851                          * O_TAG, O_LOG and O_ALTQ action parameters:
1852                          *   perform some action and set match = 1;
1853                          *
1854                          * O_LIMIT and O_KEEP_STATE: these opcodes are
1855                          *   not real 'actions', and are stored right
1856                          *   before the 'action' part of the rule.
1857                          *   These opcodes try to install an entry in the
1858                          *   state tables; if successful, we continue with
1859                          *   the next opcode (match=1; break;), otherwise
1860                          *   the packet must be dropped (set retval,
1861                          *   break loops with l=0, done=1)
1862                          *
1863                          * O_PROBE_STATE and O_CHECK_STATE: these opcodes
1864                          *   cause a lookup of the state table, and a jump
1865                          *   to the 'action' part of the parent rule
1866                          *   if an entry is found, or
1867                          *   (CHECK_STATE only) a jump to the next rule if
1868                          *   the entry is not found.
1869                          *   The result of the lookup is cached so that
1870                          *   further instances of these opcodes become NOPs.
1871                          *   The jump to the next rule is done by setting
1872                          *   l=0, cmdlen=0.
1873                          */
1874                         case O_LIMIT:
1875                         case O_KEEP_STATE:
1876                                 if (ipfw_install_state(f,
1877                                     (ipfw_insn_limit *)cmd, args, tablearg)) {
1878                                         /* error or limit violation */
1879                                         retval = IP_FW_DENY;
1880                                         l = 0;  /* exit inner loop */
1881                                         done = 1; /* exit outer loop */
1882                                 }
1883                                 match = 1;
1884                                 break;
1885
1886                         case O_PROBE_STATE:
1887                         case O_CHECK_STATE:
1888                                 /*
1889                                  * dynamic rules are checked at the first
1890                                  * keep-state or check-state occurrence,
1891                                  * with the result being stored in dyn_dir.
1892                                  * The compiler introduces a PROBE_STATE
1893                                  * instruction for us when we have a
1894                                  * KEEP_STATE (because PROBE_STATE needs
1895                                  * to be run first).
1896                                  */
1897                                 if (dyn_dir == MATCH_UNKNOWN &&
1898                                     (q = ipfw_lookup_dyn_rule(&args->f_id,
1899                                      &dyn_dir, proto == IPPROTO_TCP ?
1900                                         TCP(ulp) : NULL))
1901                                         != NULL) {
1902                                         /*
1903                                          * Found dynamic entry, update stats
1904                                          * and jump to the 'action' part of
1905                                          * the parent rule by setting
1906                                          * f, cmd, l and clearing cmdlen.
1907                                          */
1908                                         q->pcnt++;
1909                                         q->bcnt += pktlen;
1910                                         /* XXX we would like to have f_pos
1911                                          * readily accessible in the dynamic
1912                                          * rule, instead of having to
1913                                          * lookup q->rule.
1914                                          */
1915                                         f = q->rule;
1916                                         f_pos = ipfw_find_rule(chain,
1917                                                 f->rulenum, f->id);
1918                                         cmd = ACTION_PTR(f);
1919                                         l = f->cmd_len - f->act_ofs;
1920                                         ipfw_dyn_unlock();
1921                                         cmdlen = 0;
1922                                         match = 1;
1923                                         break;
1924                                 }
1925                                 /*
1926                                  * Dynamic entry not found. If CHECK_STATE,
1927                                  * skip to next rule, if PROBE_STATE just
1928                                  * ignore and continue with next opcode.
1929                                  */
1930                                 if (cmd->opcode == O_CHECK_STATE)
1931                                         l = 0;  /* exit inner loop */
1932                                 match = 1;
1933                                 break;
1934
1935                         case O_ACCEPT:
1936                                 retval = 0;     /* accept */
1937                                 l = 0;          /* exit inner loop */
1938                                 done = 1;       /* exit outer loop */
1939                                 break;
1940
1941                         case O_PIPE:
1942                         case O_QUEUE:
1943                                 set_match(args, f_pos, chain);
1944                                 args->rule.info = (cmd->arg1 == IP_FW_TABLEARG) ?
1945                                         tablearg : cmd->arg1;
1946                                 if (cmd->opcode == O_PIPE)
1947                                         args->rule.info |= IPFW_IS_PIPE;
1948                                 if (V_fw_one_pass)
1949                                         args->rule.info |= IPFW_ONEPASS;
1950                                 retval = IP_FW_DUMMYNET;
1951                                 l = 0;          /* exit inner loop */
1952                                 done = 1;       /* exit outer loop */
1953                                 break;
1954
1955                         case O_DIVERT:
1956                         case O_TEE:
1957                                 if (args->eh) /* not on layer 2 */
1958                                     break;
1959                                 /* otherwise this is terminal */
1960                                 l = 0;          /* exit inner loop */
1961                                 done = 1;       /* exit outer loop */
1962                                 retval = (cmd->opcode == O_DIVERT) ?
1963                                         IP_FW_DIVERT : IP_FW_TEE;
1964                                 set_match(args, f_pos, chain);
1965                                 args->rule.info = (cmd->arg1 == IP_FW_TABLEARG) ?
1966                                     tablearg : cmd->arg1;
1967                                 break;
1968
1969                         case O_COUNT:
1970                                 f->pcnt++;      /* update stats */
1971                                 f->bcnt += pktlen;
1972                                 f->timestamp = time_uptime;
1973                                 l = 0;          /* exit inner loop */
1974                                 break;
1975
1976                         case O_SKIPTO:
1977                             f->pcnt++;  /* update stats */
1978                             f->bcnt += pktlen;
1979                             f->timestamp = time_uptime;
1980                             /* If possible use cached f_pos (in f->next_rule),
1981                              * whose version is written in f->next_rule
1982                              * (horrible hacks to avoid changing the ABI).
1983                              */
1984                             if (cmd->arg1 != IP_FW_TABLEARG &&
1985                                     (uintptr_t)f->x_next == chain->id) {
1986                                 f_pos = (uintptr_t)f->next_rule;
1987                             } else {
1988                                 int i = (cmd->arg1 == IP_FW_TABLEARG) ?
1989                                         tablearg : cmd->arg1;
1990                                 /* make sure we do not jump backward */
1991                                 if (i <= f->rulenum)
1992                                     i = f->rulenum + 1;
1993                                 f_pos = ipfw_find_rule(chain, i, 0);
1994                                 /* update the cache */
1995                                 if (cmd->arg1 != IP_FW_TABLEARG) {
1996                                     f->next_rule =
1997                                         (void *)(uintptr_t)f_pos;
1998                                     f->x_next =
1999                                         (void *)(uintptr_t)chain->id;
2000                                 }
2001                             }
2002                             /*
2003                              * Skip disabled rules, and re-enter
2004                              * the inner loop with the correct
2005                              * f_pos, f, l and cmd.
2006                              * Also clear cmdlen and skip_or
2007                              */
2008                             for (; f_pos < chain->n_rules - 1 &&
2009                                     (V_set_disable &
2010                                      (1 << chain->map[f_pos]->set));
2011                                     f_pos++)
2012                                 ;
2013                             /* Re-enter the inner loop at the skipto rule. */
2014                             f = chain->map[f_pos];
2015                             l = f->cmd_len;
2016                             cmd = f->cmd;
2017                             match = 1;
2018                             cmdlen = 0;
2019                             skip_or = 0;
2020                             continue;
2021                             break;      /* not reached */
2022
2023                         case O_REJECT:
2024                                 /*
2025                                  * Drop the packet and send a reject notice
2026                                  * if the packet is not ICMP (or is an ICMP
2027                                  * query), and it is not multicast/broadcast.
2028                                  */
2029                                 if (hlen > 0 && is_ipv4 && offset == 0 &&
2030                                     (proto != IPPROTO_ICMP ||
2031                                      is_icmp_query(ICMP(ulp))) &&
2032                                     !(m->m_flags & (M_BCAST|M_MCAST)) &&
2033                                     !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2034                                         send_reject(args, cmd->arg1, iplen, ip);
2035                                         m = args->m;
2036                                 }
2037                                 /* FALLTHROUGH */
2038 #ifdef INET6
2039                         case O_UNREACH6:
2040                                 if (hlen > 0 && is_ipv6 &&
2041                                     ((offset & IP6F_OFF_MASK) == 0) &&
2042                                     (proto != IPPROTO_ICMPV6 ||
2043                                      (is_icmp6_query(icmp6_type) == 1)) &&
2044                                     !(m->m_flags & (M_BCAST|M_MCAST)) &&
2045                                     !IN6_IS_ADDR_MULTICAST(&args->f_id.dst_ip6)) {
2046                                         send_reject6(
2047                                             args, cmd->arg1, hlen,
2048                                             (struct ip6_hdr *)ip);
2049                                         m = args->m;
2050                                 }
2051                                 /* FALLTHROUGH */
2052 #endif
2053                         case O_DENY:
2054                                 retval = IP_FW_DENY;
2055                                 l = 0;          /* exit inner loop */
2056                                 done = 1;       /* exit outer loop */
2057                                 break;
2058
2059                         case O_FORWARD_IP:
2060                                 if (args->eh)   /* not valid on layer2 pkts */
2061                                         break;
2062                                 if (!q || dyn_dir == MATCH_FORWARD) {
2063                                     struct sockaddr_in *sa;
2064                                     sa = &(((ipfw_insn_sa *)cmd)->sa);
2065                                     if (sa->sin_addr.s_addr == INADDR_ANY) {
2066                                         bcopy(sa, &args->hopstore,
2067                                                         sizeof(*sa));
2068                                         args->hopstore.sin_addr.s_addr =
2069                                                     htonl(tablearg);
2070                                         args->next_hop = &args->hopstore;
2071                                     } else {
2072                                         args->next_hop = sa;
2073                                     }
2074                                 }
2075                                 retval = IP_FW_PASS;
2076                                 l = 0;          /* exit inner loop */
2077                                 done = 1;       /* exit outer loop */
2078                                 break;
2079
2080                         case O_NETGRAPH:
2081                         case O_NGTEE:
2082                                 set_match(args, f_pos, chain);
2083                                 args->rule.info = (cmd->arg1 == IP_FW_TABLEARG) ?
2084                                         tablearg : cmd->arg1;
2085                                 if (V_fw_one_pass)
2086                                         args->rule.info |= IPFW_ONEPASS;
2087                                 retval = (cmd->opcode == O_NETGRAPH) ?
2088                                     IP_FW_NETGRAPH : IP_FW_NGTEE;
2089                                 l = 0;          /* exit inner loop */
2090                                 done = 1;       /* exit outer loop */
2091                                 break;
2092
2093                         case O_SETFIB:
2094                                 f->pcnt++;      /* update stats */
2095                                 f->bcnt += pktlen;
2096                                 f->timestamp = time_uptime;
2097                                 M_SETFIB(m, cmd->arg1);
2098                                 args->f_id.fib = cmd->arg1;
2099                                 l = 0;          /* exit inner loop */
2100                                 break;
2101
2102                         case O_NAT:
2103                                 if (!IPFW_NAT_LOADED) {
2104                                     retval = IP_FW_DENY;
2105                                 } else {
2106                                     struct cfg_nat *t;
2107                                     int nat_id;
2108
2109                                     set_match(args, f_pos, chain);
2110                                     t = ((ipfw_insn_nat *)cmd)->nat;
2111                                     if (t == NULL) {
2112                                         nat_id = (cmd->arg1 == IP_FW_TABLEARG) ?
2113                                                 tablearg : cmd->arg1;
2114                                         t = (*lookup_nat_ptr)(&chain->nat, nat_id);
2115
2116                                         if (t == NULL) {
2117                                             retval = IP_FW_DENY;
2118                                             l = 0;      /* exit inner loop */
2119                                             done = 1;   /* exit outer loop */
2120                                             break;
2121                                         }
2122                                         if (cmd->arg1 != IP_FW_TABLEARG)
2123                                             ((ipfw_insn_nat *)cmd)->nat = t;
2124                                     }
2125                                     retval = ipfw_nat_ptr(args, t, m);
2126                                 }
2127                                 l = 0;          /* exit inner loop */
2128                                 done = 1;       /* exit outer loop */
2129                                 break;
2130
2131                         case O_REASS: {
2132                                 int ip_off;
2133
2134                                 f->pcnt++;
2135                                 f->bcnt += pktlen;
2136                                 l = 0;  /* in any case exit inner loop */
2137                                 ip_off = ntohs(ip->ip_off);
2138
2139                                 /* if not fragmented, go to next rule */
2140                                 if ((ip_off & (IP_MF | IP_OFFMASK)) == 0)
2141                                     break;
2142                                 /* 
2143                                  * ip_reass() expects len & off in host
2144                                  * byte order.
2145                                  */
2146                                 SET_HOST_IPLEN(ip);
2147
2148                                 args->m = m = ip_reass(m);
2149
2150                                 /*
2151                                  * do IP header checksum fixup.
2152                                  */
2153                                 if (m == NULL) { /* fragment got swallowed */
2154                                     retval = IP_FW_DENY;
2155                                 } else { /* good, packet complete */
2156                                     int hlen;
2157
2158                                     ip = mtod(m, struct ip *);
2159                                     hlen = ip->ip_hl << 2;
2160                                     SET_NET_IPLEN(ip);
2161                                     ip->ip_sum = 0;
2162                                     if (hlen == sizeof(struct ip))
2163                                         ip->ip_sum = in_cksum_hdr(ip);
2164                                     else
2165                                         ip->ip_sum = in_cksum(m, hlen);
2166                                     retval = IP_FW_REASS;
2167                                     set_match(args, f_pos, chain);
2168                                 }
2169                                 done = 1;       /* exit outer loop */
2170                                 break;
2171                         }
2172
2173                         default:
2174                                 panic("-- unknown opcode %d\n", cmd->opcode);
2175                         } /* end of switch() on opcodes */
2176                         /*
2177                          * if we get here with l=0, then match is irrelevant.
2178                          */
2179
2180                         if (cmd->len & F_NOT)
2181                                 match = !match;
2182
2183                         if (match) {
2184                                 if (cmd->len & F_OR)
2185                                         skip_or = 1;
2186                         } else {
2187                                 if (!(cmd->len & F_OR)) /* not an OR block, */
2188                                         break;          /* try next rule    */
2189                         }
2190
2191                 }       /* end of inner loop, scan opcodes */
2192
2193                 if (done)
2194                         break;
2195
2196 /* next_rule:; */       /* try next rule                */
2197
2198         }               /* end of outer for, scan rules */
2199
2200         if (done) {
2201                 struct ip_fw *rule = chain->map[f_pos];
2202                 /* Update statistics */
2203                 rule->pcnt++;
2204                 rule->bcnt += pktlen;
2205                 rule->timestamp = time_uptime;
2206         } else {
2207                 retval = IP_FW_DENY;
2208                 printf("ipfw: ouch!, skip past end of rules, denying packet\n");
2209         }
2210         IPFW_RUNLOCK(chain);
2211 #ifdef __FreeBSD__
2212         if (ucred_cache != NULL)
2213                 crfree(ucred_cache);
2214 #endif
2215         return (retval);
2216
2217 pullup_failed:
2218         if (V_fw_verbose)
2219                 printf("ipfw: pullup failed\n");
2220         return (IP_FW_DENY);
2221 }
2222
2223 /*
2224  * Module and VNET glue
2225  */
2226
2227 /*
2228  * Stuff that must be initialised only on boot or module load
2229  */
2230 static int
2231 ipfw_init(void)
2232 {
2233         int error = 0;
2234
2235         ipfw_dyn_attach();
2236         /*
2237          * Only print out this stuff the first time around,
2238          * when called from the sysinit code.
2239          */
2240         printf("ipfw2 "
2241 #ifdef INET6
2242                 "(+ipv6) "
2243 #endif
2244                 "initialized, divert %s, nat %s, "
2245                 "rule-based forwarding "
2246 #ifdef IPFIREWALL_FORWARD
2247                 "enabled, "
2248 #else
2249                 "disabled, "
2250 #endif
2251                 "default to %s, logging ",
2252 #ifdef IPDIVERT
2253                 "enabled",
2254 #else
2255                 "loadable",
2256 #endif
2257 #ifdef IPFIREWALL_NAT
2258                 "enabled",
2259 #else
2260                 "loadable",
2261 #endif
2262                 default_to_accept ? "accept" : "deny");
2263
2264         /*
2265          * Note: V_xxx variables can be accessed here but the vnet specific
2266          * initializer may not have been called yet for the VIMAGE case.
2267          * Tuneables will have been processed. We will print out values for
2268          * the default vnet. 
2269          * XXX This should all be rationalized AFTER 8.0
2270          */
2271         if (V_fw_verbose == 0)
2272                 printf("disabled\n");
2273         else if (V_verbose_limit == 0)
2274                 printf("unlimited\n");
2275         else
2276                 printf("limited to %d packets/entry by default\n",
2277                     V_verbose_limit);
2278
2279         ipfw_log_bpf(1); /* init */
2280         return (error);
2281 }
2282
2283 /*
2284  * Called for the removal of the last instance only on module unload.
2285  */
2286 static void
2287 ipfw_destroy(void)
2288 {
2289
2290         ipfw_log_bpf(0); /* uninit */
2291         ipfw_dyn_detach();
2292         printf("IP firewall unloaded\n");
2293 }
2294
2295 /*
2296  * Stuff that must be initialized for every instance
2297  * (including the first of course).
2298  */
2299 static int
2300 vnet_ipfw_init(const void *unused)
2301 {
2302         int error;
2303         struct ip_fw *rule = NULL;
2304         struct ip_fw_chain *chain;
2305
2306         chain = &V_layer3_chain;
2307
2308         /* First set up some values that are compile time options */
2309         V_autoinc_step = 100;   /* bounded to 1..1000 in add_rule() */
2310         V_fw_deny_unknown_exthdrs = 1;
2311 #ifdef IPFIREWALL_VERBOSE
2312         V_fw_verbose = 1;
2313 #endif
2314 #ifdef IPFIREWALL_VERBOSE_LIMIT
2315         V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
2316 #endif
2317 #ifdef IPFIREWALL_NAT
2318         LIST_INIT(&chain->nat);
2319 #endif
2320
2321         /* insert the default rule and create the initial map */
2322         chain->n_rules = 1;
2323         chain->static_len = sizeof(struct ip_fw);
2324         chain->map = malloc(sizeof(struct ip_fw *), M_IPFW, M_NOWAIT | M_ZERO);
2325         if (chain->map)
2326                 rule = malloc(chain->static_len, M_IPFW, M_NOWAIT | M_ZERO);
2327         if (rule == NULL) {
2328                 if (chain->map)
2329                         free(chain->map, M_IPFW);
2330                 printf("ipfw2: ENOSPC initializing default rule "
2331                         "(support disabled)\n");
2332                 return (ENOSPC);
2333         }
2334         error = ipfw_init_tables(chain);
2335         if (error) {
2336                 panic("init_tables"); /* XXX Marko fix this ! */
2337         }
2338
2339         /* fill and insert the default rule */
2340         rule->act_ofs = 0;
2341         rule->rulenum = IPFW_DEFAULT_RULE;
2342         rule->cmd_len = 1;
2343         rule->set = RESVD_SET;
2344         rule->cmd[0].len = 1;
2345         rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY;
2346         chain->rules = chain->default_rule = chain->map[0] = rule;
2347         chain->id = rule->id = 1;
2348
2349         IPFW_LOCK_INIT(chain);
2350         ipfw_dyn_init();
2351
2352         /* First set up some values that are compile time options */
2353         V_ipfw_vnet_ready = 1;          /* Open for business */
2354
2355         /*
2356          * Hook the sockopt handler, and the layer2 (V_ip_fw_chk_ptr)
2357          * and pfil hooks for ipv4 and ipv6. Even if the latter two fail
2358          * we still keep the module alive because the sockopt and
2359          * layer2 paths are still useful.
2360          * ipfw[6]_hook return 0 on success, ENOENT on failure,
2361          * so we can ignore the exact return value and just set a flag.
2362          *
2363          * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so
2364          * changes in the underlying (per-vnet) variables trigger
2365          * immediate hook()/unhook() calls.
2366          * In layer2 we have the same behaviour, except that V_ether_ipfw
2367          * is checked on each packet because there are no pfil hooks.
2368          */
2369         V_ip_fw_ctl_ptr = ipfw_ctl;
2370         V_ip_fw_chk_ptr = ipfw_chk;
2371         error = ipfw_attach_hooks(1);
2372         return (error);
2373 }
2374
2375 /*
2376  * Called for the removal of each instance.
2377  */
2378 static int
2379 vnet_ipfw_uninit(const void *unused)
2380 {
2381         struct ip_fw *reap, *rule;
2382         struct ip_fw_chain *chain = &V_layer3_chain;
2383         int i;
2384
2385         V_ipfw_vnet_ready = 0; /* tell new callers to go away */
2386         /*
2387          * disconnect from ipv4, ipv6, layer2 and sockopt.
2388          * Then grab, release and grab again the WLOCK so we make
2389          * sure the update is propagated and nobody will be in.
2390          */
2391         (void)ipfw_attach_hooks(0 /* detach */);
2392         V_ip_fw_chk_ptr = NULL;
2393         V_ip_fw_ctl_ptr = NULL;
2394         IPFW_UH_WLOCK(chain);
2395         IPFW_UH_WUNLOCK(chain);
2396         IPFW_UH_WLOCK(chain);
2397
2398         IPFW_WLOCK(chain);
2399         IPFW_WUNLOCK(chain);
2400         IPFW_WLOCK(chain);
2401
2402         ipfw_dyn_uninit(0);     /* run the callout_drain */
2403         ipfw_destroy_tables(chain);
2404         reap = NULL;
2405         for (i = 0; i < chain->n_rules; i++) {
2406                 rule = chain->map[i];
2407                 rule->x_next = reap;
2408                 reap = rule;
2409         }
2410         if (chain->map)
2411                 free(chain->map, M_IPFW);
2412         IPFW_WUNLOCK(chain);
2413         IPFW_UH_WUNLOCK(chain);
2414         if (reap != NULL)
2415                 ipfw_reap_rules(reap);
2416         IPFW_LOCK_DESTROY(chain);
2417         ipfw_dyn_uninit(1);     /* free the remaining parts */
2418         return 0;
2419 }
2420
2421 /*
2422  * Module event handler.
2423  * In general we have the choice of handling most of these events by the
2424  * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to
2425  * use the SYSINIT handlers as they are more capable of expressing the
2426  * flow of control during module and vnet operations, so this is just
2427  * a skeleton. Note there is no SYSINIT equivalent of the module
2428  * SHUTDOWN handler, but we don't have anything to do in that case anyhow.
2429  */
2430 static int
2431 ipfw_modevent(module_t mod, int type, void *unused)
2432 {
2433         int err = 0;
2434
2435         switch (type) {
2436         case MOD_LOAD:
2437                 /* Called once at module load or
2438                  * system boot if compiled in. */
2439                 break;
2440         case MOD_QUIESCE:
2441                 /* Called before unload. May veto unloading. */
2442                 break;
2443         case MOD_UNLOAD:
2444                 /* Called during unload. */
2445                 break;
2446         case MOD_SHUTDOWN:
2447                 /* Called during system shutdown. */
2448                 break;
2449         default:
2450                 err = EOPNOTSUPP;
2451                 break;
2452         }
2453         return err;
2454 }
2455
2456 static moduledata_t ipfwmod = {
2457         "ipfw",
2458         ipfw_modevent,
2459         0
2460 };
2461
2462 /* Define startup order. */
2463 #define IPFW_SI_SUB_FIREWALL    SI_SUB_PROTO_IFATTACHDOMAIN
2464 #define IPFW_MODEVENT_ORDER     (SI_ORDER_ANY - 255) /* On boot slot in here. */
2465 #define IPFW_MODULE_ORDER       (IPFW_MODEVENT_ORDER + 1) /* A little later. */
2466 #define IPFW_VNET_ORDER         (IPFW_MODEVENT_ORDER + 2) /* Later still. */
2467
2468 DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER);
2469 MODULE_VERSION(ipfw, 2);
2470 /* should declare some dependencies here */
2471
2472 /*
2473  * Starting up. Done in order after ipfwmod() has been called.
2474  * VNET_SYSINIT is also called for each existing vnet and each new vnet.
2475  */
2476 SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
2477             ipfw_init, NULL);
2478 VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
2479             vnet_ipfw_init, NULL);
2480  
2481 /*
2482  * Closing up shop. These are done in REVERSE ORDER, but still
2483  * after ipfwmod() has been called. Not called on reboot.
2484  * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
2485  * or when the module is unloaded.
2486  */
2487 SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
2488             ipfw_destroy, NULL);
2489 VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
2490             vnet_ipfw_uninit, NULL);
2491 /* end of file */