2 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * The FreeBSD IP packet firewall, main file
34 #include "opt_ipdivert.h"
37 #error IPFIREWALL requires INET.
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
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>
48 #include <sys/kernel.h>
51 #include <sys/module.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 */
62 #include <net/route.h>
63 #include <net/pf_mtag.h>
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>
81 #include <netinet/ip6.h>
82 #include <netinet/icmp6.h>
84 #include <netinet6/in6_pcb.h>
85 #include <netinet6/scope6_var.h>
86 #include <netinet6/ip6_var.h>
89 #include <machine/in_cksum.h> /* XXX for in_cksum */
92 #include <security/mac/mac_framework.h>
96 * static variables followed by global ones.
97 * All ipfw global variables are here.
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)
104 static VNET_DEFINE(int, fw_deny_unknown_exthdrs);
105 #define V_fw_deny_unknown_exthdrs VNET(fw_deny_unknown_exthdrs)
107 static VNET_DEFINE(int, fw_permit_single_frag6) = 1;
108 #define V_fw_permit_single_frag6 VNET(fw_permit_single_frag6)
110 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
111 static int default_to_accept = 1;
113 static int default_to_accept;
116 VNET_DEFINE(int, autoinc_step);
117 VNET_DEFINE(int, fw_one_pass) = 1;
120 * Each rule belongs to one of 32 different sets (0..31).
121 * The variable set_disable contains one bit per set.
122 * If the bit is set, all rules in the corresponding set
123 * are disabled. Set RESVD_SET(31) is reserved for the default rule
124 * and rules that are not deleted by the flush command,
125 * and CANNOT be disabled.
126 * Rules in set RESVD_SET can only be deleted individually.
128 VNET_DEFINE(u_int32_t, set_disable);
129 #define V_set_disable VNET(set_disable)
131 VNET_DEFINE(int, fw_verbose);
132 /* counter for ipfw_log(NULL...) */
133 VNET_DEFINE(u_int64_t, norule_counter);
134 VNET_DEFINE(int, verbose_limit);
136 /* layer3_chain contains the list of rules for layer 3 */
137 VNET_DEFINE(struct ip_fw_chain, layer3_chain);
139 ipfw_nat_t *ipfw_nat_ptr = NULL;
140 struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
141 ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
142 ipfw_nat_cfg_t *ipfw_nat_del_ptr;
143 ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
144 ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
147 uint32_t dummy_def = IPFW_DEFAULT_RULE;
148 uint32_t dummy_tables_max = IPFW_TABLES_MAX;
152 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
153 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, one_pass,
154 CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0,
155 "Only do a single pass through ipfw when using dummynet(4)");
156 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step,
157 CTLFLAG_RW, &VNET_NAME(autoinc_step), 0,
158 "Rule number auto-increment step");
159 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose,
160 CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0,
161 "Log matches to ipfw rules");
162 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit,
163 CTLFLAG_RW, &VNET_NAME(verbose_limit), 0,
164 "Set upper limit of matches of ipfw rules logged");
165 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD,
167 "The default/max possible rule number.");
168 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, tables_max, CTLFLAG_RD,
169 &dummy_tables_max, 0,
170 "The maximum number of tables.");
171 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN,
172 &default_to_accept, 0,
173 "Make the default rule accept all packets.");
174 TUNABLE_INT("net.inet.ip.fw.default_to_accept", &default_to_accept);
175 SYSCTL_VNET_INT(_net_inet_ip_fw, OID_AUTO, static_count,
176 CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0,
177 "Number of static rules");
180 SYSCTL_DECL(_net_inet6_ip6);
181 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
182 SYSCTL_VNET_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs,
183 CTLFLAG_RW | CTLFLAG_SECURE, &VNET_NAME(fw_deny_unknown_exthdrs), 0,
184 "Deny packets with unknown IPv6 Extension Headers");
185 SYSCTL_VNET_INT(_net_inet6_ip6_fw, OID_AUTO, permit_single_frag6,
186 CTLFLAG_RW | CTLFLAG_SECURE, &VNET_NAME(fw_permit_single_frag6), 0,
187 "Permit single packet IPv6 fragments");
192 #endif /* SYSCTL_NODE */
196 * Some macros used in the various matching options.
197 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
198 * Other macros just cast void * into the appropriate type
200 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
201 #define TCP(p) ((struct tcphdr *)(p))
202 #define SCTP(p) ((struct sctphdr *)(p))
203 #define UDP(p) ((struct udphdr *)(p))
204 #define ICMP(p) ((struct icmphdr *)(p))
205 #define ICMP6(p) ((struct icmp6_hdr *)(p))
208 icmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd)
210 int type = icmp->icmp_type;
212 return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) );
215 #define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
216 (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
219 is_icmp_query(struct icmphdr *icmp)
221 int type = icmp->icmp_type;
223 return (type <= ICMP_MAXTYPE && (TT & (1<<type)) );
228 * The following checks use two arrays of 8 or 16 bits to store the
229 * bits that we want set or clear, respectively. They are in the
230 * low and high half of cmd->arg1 or cmd->d[0].
232 * We scan options and store the bits we find set. We succeed if
234 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
236 * The code is sometimes optimized not to store additional variables.
240 flags_match(ipfw_insn *cmd, u_int8_t bits)
245 if ( ((cmd->arg1 & 0xff) & bits) != 0)
246 return 0; /* some bits we want set were clear */
247 want_clear = (cmd->arg1 >> 8) & 0xff;
248 if ( (want_clear & bits) != want_clear)
249 return 0; /* some bits we want clear were set */
254 ipopts_match(struct ip *ip, ipfw_insn *cmd)
256 int optlen, bits = 0;
257 u_char *cp = (u_char *)(ip + 1);
258 int x = (ip->ip_hl << 2) - sizeof (struct ip);
260 for (; x > 0; x -= optlen, cp += optlen) {
261 int opt = cp[IPOPT_OPTVAL];
263 if (opt == IPOPT_EOL)
265 if (opt == IPOPT_NOP)
268 optlen = cp[IPOPT_OLEN];
269 if (optlen <= 0 || optlen > x)
270 return 0; /* invalid or truncated */
278 bits |= IP_FW_IPOPT_LSRR;
282 bits |= IP_FW_IPOPT_SSRR;
286 bits |= IP_FW_IPOPT_RR;
290 bits |= IP_FW_IPOPT_TS;
294 return (flags_match(cmd, bits));
298 tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd)
300 int optlen, bits = 0;
301 u_char *cp = (u_char *)(tcp + 1);
302 int x = (tcp->th_off << 2) - sizeof(struct tcphdr);
304 for (; x > 0; x -= optlen, cp += optlen) {
306 if (opt == TCPOPT_EOL)
308 if (opt == TCPOPT_NOP)
322 bits |= IP_FW_TCPOPT_MSS;
326 bits |= IP_FW_TCPOPT_WINDOW;
329 case TCPOPT_SACK_PERMITTED:
331 bits |= IP_FW_TCPOPT_SACK;
334 case TCPOPT_TIMESTAMP:
335 bits |= IP_FW_TCPOPT_TS;
340 return (flags_match(cmd, bits));
344 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd)
346 if (ifp == NULL) /* no iface with this packet, match fails */
348 /* Check by name or by IP address */
349 if (cmd->name[0] != '\0') { /* match by name */
352 if (fnmatch(cmd->name, ifp->if_xname, 0) == 0)
355 if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
359 #ifdef __FreeBSD__ /* and OSX too ? */
363 TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
364 if (ia->ifa_addr->sa_family != AF_INET)
366 if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
367 (ia->ifa_addr))->sin_addr.s_addr) {
368 if_addr_runlock(ifp);
369 return(1); /* match */
372 if_addr_runlock(ifp);
373 #endif /* __FreeBSD__ */
375 return(0); /* no match, fail ... */
379 * The verify_path function checks if a route to the src exists and
380 * if it is reachable via ifp (when provided).
382 * The 'verrevpath' option checks that the interface that an IP packet
383 * arrives on is the same interface that traffic destined for the
384 * packet's source address would be routed out of.
385 * The 'versrcreach' option just checks that the source address is
386 * reachable via any route (except default) in the routing table.
387 * These two are a measure to block forged packets. This is also
388 * commonly known as "anti-spoofing" or Unicast Reverse Path
389 * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs
390 * is purposely reminiscent of the Cisco IOS command,
392 * ip verify unicast reverse-path
393 * ip verify unicast source reachable-via any
395 * which implements the same functionality. But note that the syntax
396 * is misleading, and the check may be performed on all IP packets
397 * whether unicast, multicast, or broadcast.
400 verify_path(struct in_addr src, struct ifnet *ifp, u_int fib)
406 struct sockaddr_in *dst;
408 bzero(&ro, sizeof(ro));
410 dst = (struct sockaddr_in *)&(ro.ro_dst);
411 dst->sin_family = AF_INET;
412 dst->sin_len = sizeof(*dst);
414 in_rtalloc_ign(&ro, 0, fib);
416 if (ro.ro_rt == NULL)
420 * If ifp is provided, check for equality with rtentry.
421 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
422 * in order to pass packets injected back by if_simloop():
423 * if useloopback == 1 routing entry (via lo0) for our own address
424 * may exist, so we need to handle routing assymetry.
426 if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) {
431 /* if no ifp provided, check if rtentry is not default route */
433 satosin(rt_key(ro.ro_rt))->sin_addr.s_addr == INADDR_ANY) {
438 /* or if this is a blackhole/reject route */
439 if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
444 /* found valid route */
447 #endif /* __FreeBSD__ */
452 * ipv6 specific rules here...
455 icmp6type_match (int type, ipfw_insn_u32 *cmd)
457 return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) );
461 flow6id_match( int curr_flow, ipfw_insn_u32 *cmd )
464 for (i=0; i <= cmd->o.arg1; ++i )
465 if (curr_flow == cmd->d[i] )
470 /* support for IP6_*_ME opcodes */
472 search_ip6_addr_net (struct in6_addr * ip6_addr)
476 struct in6_ifaddr *fdm;
477 struct in6_addr copia;
479 TAILQ_FOREACH(mdc, &V_ifnet, if_link) {
481 TAILQ_FOREACH(mdc2, &mdc->if_addrhead, ifa_link) {
482 if (mdc2->ifa_addr->sa_family == AF_INET6) {
483 fdm = (struct in6_ifaddr *)mdc2;
484 copia = fdm->ia_addr.sin6_addr;
485 /* need for leaving scope_id in the sock_addr */
486 in6_clearscope(&copia);
487 if (IN6_ARE_ADDR_EQUAL(ip6_addr, &copia)) {
488 if_addr_runlock(mdc);
493 if_addr_runlock(mdc);
499 verify_path6(struct in6_addr *src, struct ifnet *ifp)
502 struct sockaddr_in6 *dst;
504 bzero(&ro, sizeof(ro));
506 dst = (struct sockaddr_in6 * )&(ro.ro_dst);
507 dst->sin6_family = AF_INET6;
508 dst->sin6_len = sizeof(*dst);
509 dst->sin6_addr = *src;
510 /* XXX MRT 0 for ipv6 at this time */
511 rtalloc_ign((struct route *)&ro, 0);
513 if (ro.ro_rt == NULL)
517 * if ifp is provided, check for equality with rtentry
518 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
519 * to support the case of sending packets to an address of our own.
520 * (where the former interface is the first argument of if_simloop()
521 * (=ifp), the latter is lo0)
523 if (ifp != NULL && ro.ro_rt->rt_ifa->ifa_ifp != ifp) {
528 /* if no ifp provided, check if rtentry is not default route */
530 IN6_IS_ADDR_UNSPECIFIED(&satosin6(rt_key(ro.ro_rt))->sin6_addr)) {
535 /* or if this is a blackhole/reject route */
536 if (ifp == NULL && ro.ro_rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
541 /* found valid route */
548 is_icmp6_query(int icmp6_type)
550 if ((icmp6_type <= ICMP6_MAXTYPE) &&
551 (icmp6_type == ICMP6_ECHO_REQUEST ||
552 icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
553 icmp6_type == ICMP6_WRUREQUEST ||
554 icmp6_type == ICMP6_FQDN_QUERY ||
555 icmp6_type == ICMP6_NI_QUERY))
562 send_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6)
567 if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) {
569 tcp = (struct tcphdr *)((char *)ip6 + hlen);
571 if ((tcp->th_flags & TH_RST) == 0) {
573 m0 = ipfw_send_pkt(args->m, &(args->f_id),
574 ntohl(tcp->th_seq), ntohl(tcp->th_ack),
575 tcp->th_flags | TH_RST);
577 ip6_output(m0, NULL, NULL, 0, NULL, NULL,
581 } else if (code != ICMP6_UNREACH_RST) { /* Send an ICMPv6 unreach. */
584 * Unlike above, the mbufs need to line up with the ip6 hdr,
585 * as the contents are read. We need to m_adj() the
587 * The mbuf will however be thrown away so we can adjust it.
588 * Remember we did an m_pullup on it already so we
589 * can make some assumptions about contiguousness.
592 m_adj(m, args->L3offset);
594 icmp6_error(m, ICMP6_DST_UNREACH, code, 0);
605 * sends a reject message, consuming the mbuf passed as an argument.
608 send_reject(struct ip_fw_args *args, int code, int iplen, struct ip *ip)
612 /* XXX When ip is not guaranteed to be at mtod() we will
613 * need to account for this */
614 * The mbuf will however be thrown away so we can adjust it.
615 * Remember we did an m_pullup on it already so we
616 * can make some assumptions about contiguousness.
619 m_adj(m, args->L3offset);
621 if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */
622 /* We need the IP header in host order for icmp_error(). */
624 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
625 } else if (args->f_id.proto == IPPROTO_TCP) {
626 struct tcphdr *const tcp =
627 L3HDR(struct tcphdr, mtod(args->m, struct ip *));
628 if ( (tcp->th_flags & TH_RST) == 0) {
630 m = ipfw_send_pkt(args->m, &(args->f_id),
631 ntohl(tcp->th_seq), ntohl(tcp->th_ack),
632 tcp->th_flags | TH_RST);
634 ip_output(m, NULL, NULL, 0, NULL, NULL);
643 * Support for uid/gid/jail lookup. These tests are expensive
644 * (because we may need to look into the list of active sockets)
645 * so we cache the results. ugid_lookupp is 0 if we have not
646 * yet done a lookup, 1 if we succeeded, and -1 if we tried
647 * and failed. The function always returns the match value.
648 * We could actually spare the variable and use *uc, setting
649 * it to '(void *)check_uidgid if we have no info, NULL if
650 * we tried and failed, or any other value if successful.
653 check_uidgid(ipfw_insn_u32 *insn, struct ip_fw_args *args, int *ugid_lookupp,
658 return cred_check(insn, proto, oif,
659 dst_ip, dst_port, src_ip, src_port,
660 (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb);
662 struct in_addr src_ip, dst_ip;
663 struct inpcbinfo *pi;
664 struct ipfw_flow_id *id;
665 struct inpcb *pcb, *inp;
675 * Check to see if the UDP or TCP stack supplied us with
676 * the PCB. If so, rather then holding a lock and looking
677 * up the PCB, we can use the one that was supplied.
679 if (inp && *ugid_lookupp == 0) {
680 INP_LOCK_ASSERT(inp);
681 if (inp->inp_socket != NULL) {
682 *uc = crhold(inp->inp_cred);
688 * If we have already been here and the packet has no
689 * PCB entry associated with it, then we can safely
690 * assume that this is a no match.
692 if (*ugid_lookupp == -1)
694 if (id->proto == IPPROTO_TCP) {
697 } else if (id->proto == IPPROTO_UDP) {
698 lookupflags = INPLOOKUP_WILDCARD;
702 lookupflags |= INPLOOKUP_RLOCKPCB;
704 if (*ugid_lookupp == 0) {
705 if (id->addr_type == 6) {
708 pcb = in6_pcblookup_mbuf(pi,
709 &id->src_ip6, htons(id->src_port),
710 &id->dst_ip6, htons(id->dst_port),
711 lookupflags, oif, args->m);
713 pcb = in6_pcblookup_mbuf(pi,
714 &id->dst_ip6, htons(id->dst_port),
715 &id->src_ip6, htons(id->src_port),
716 lookupflags, oif, args->m);
722 src_ip.s_addr = htonl(id->src_ip);
723 dst_ip.s_addr = htonl(id->dst_ip);
725 pcb = in_pcblookup_mbuf(pi,
726 src_ip, htons(id->src_port),
727 dst_ip, htons(id->dst_port),
728 lookupflags, oif, args->m);
730 pcb = in_pcblookup_mbuf(pi,
731 dst_ip, htons(id->dst_port),
732 src_ip, htons(id->src_port),
733 lookupflags, oif, args->m);
736 INP_RLOCK_ASSERT(pcb);
737 *uc = crhold(pcb->inp_cred);
741 if (*ugid_lookupp == 0) {
743 * We tried and failed, set the variable to -1
744 * so we will not try again on this packet.
750 if (insn->o.opcode == O_UID)
751 match = ((*uc)->cr_uid == (uid_t)insn->d[0]);
752 else if (insn->o.opcode == O_GID)
753 match = groupmember((gid_t)insn->d[0], *uc);
754 else if (insn->o.opcode == O_JAIL)
755 match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]);
757 #endif /* __FreeBSD__ */
761 * Helper function to set args with info on the rule after the matching
762 * one. slot is precise, whereas we guess rule_id as they are
763 * assigned sequentially.
766 set_match(struct ip_fw_args *args, int slot,
767 struct ip_fw_chain *chain)
769 args->rule.chain_id = chain->id;
770 args->rule.slot = slot + 1; /* we use 0 as a marker */
771 args->rule.rule_id = 1 + chain->map[slot]->id;
772 args->rule.rulenum = chain->map[slot]->rulenum;
776 * The main check routine for the firewall.
778 * All arguments are in args so we can modify them and return them
779 * back to the caller.
783 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
784 * Starts with the IP header.
785 * args->eh (in) Mac header if present, NULL for layer3 packet.
786 * args->L3offset Number of bytes bypassed if we came from L2.
787 * e.g. often sizeof(eh) ** NOTYET **
788 * args->oif Outgoing interface, NULL if packet is incoming.
789 * The incoming interface is in the mbuf. (in)
790 * args->divert_rule (in/out)
791 * Skip up to the first rule past this rule number;
792 * upon return, non-zero port number for divert or tee.
794 * args->rule Pointer to the last matching rule (in/out)
795 * args->next_hop Socket we are forwarding to (out).
796 * args->next_hop6 IPv6 next hop we are forwarding to (out).
797 * args->f_id Addresses grabbed from the packet (out)
798 * args->rule.info a cookie depending on rule action
802 * IP_FW_PASS the packet must be accepted
803 * IP_FW_DENY the packet must be dropped
804 * IP_FW_DIVERT divert packet, port in m_tag
805 * IP_FW_TEE tee packet, port in m_tag
806 * IP_FW_DUMMYNET to dummynet, pipe in args->cookie
807 * IP_FW_NETGRAPH into netgraph, cookie args->cookie
808 * args->rule contains the matching rule,
809 * args->rule.info has additional information.
813 ipfw_chk(struct ip_fw_args *args)
817 * Local variables holding state while processing a packet:
819 * IMPORTANT NOTE: to speed up the processing of rules, there
820 * are some assumption on the values of the variables, which
821 * are documented here. Should you change them, please check
822 * the implementation of the various instructions to make sure
823 * that they still work.
825 * args->eh The MAC header. It is non-null for a layer2
826 * packet, it is NULL for a layer-3 packet.
828 * args->L3offset Offset in the packet to the L3 (IP or equiv.) header.
830 * m | args->m Pointer to the mbuf, as received from the caller.
831 * It may change if ipfw_chk() does an m_pullup, or if it
832 * consumes the packet because it calls send_reject().
833 * XXX This has to change, so that ipfw_chk() never modifies
834 * or consumes the buffer.
835 * ip is the beginning of the ip(4 or 6) header.
836 * Calculated by adding the L3offset to the start of data.
837 * (Until we start using L3offset, the packet is
838 * supposed to start with the ip header).
840 struct mbuf *m = args->m;
841 struct ip *ip = mtod(m, struct ip *);
844 * For rules which contain uid/gid or jail constraints, cache
845 * a copy of the users credentials after the pcb lookup has been
846 * executed. This will speed up the processing of rules with
847 * these types of constraints, as well as decrease contention
848 * on pcb related locks.
851 struct bsd_ucred ucred_cache;
853 struct ucred *ucred_cache = NULL;
855 int ucred_lookup = 0;
858 * oif | args->oif If NULL, ipfw_chk has been called on the
859 * inbound path (ether_input, ip_input).
860 * If non-NULL, ipfw_chk has been called on the outbound path
861 * (ether_output, ip_output).
863 struct ifnet *oif = args->oif;
865 int f_pos = 0; /* index of current rule in the array */
869 * hlen The length of the IP header.
871 u_int hlen = 0; /* hlen >0 means we have an IP pkt */
874 * offset The offset of a fragment. offset != 0 means that
875 * we have a fragment at this offset of an IPv4 packet.
876 * offset == 0 means that (if this is an IPv4 packet)
877 * this is the first or only fragment.
878 * For IPv6 offset|ip6f_mf == 0 means there is no Fragment Header
879 * or there is a single packet fragement (fragement header added
880 * without needed). We will treat a single packet fragment as if
881 * there was no fragment header (or log/block depending on the
882 * V_fw_permit_single_frag6 sysctl setting).
888 * Local copies of addresses. They are only valid if we have
891 * proto The protocol. Set to 0 for non-ip packets,
892 * or to the protocol read from the packet otherwise.
893 * proto != 0 means that we have an IPv4 packet.
895 * src_port, dst_port port numbers, in HOST format. Only
896 * valid for TCP and UDP packets.
898 * src_ip, dst_ip ip addresses, in NETWORK format.
899 * Only valid for IPv4 packets.
902 uint16_t src_port = 0, dst_port = 0; /* NOTE: host format */
903 struct in_addr src_ip, dst_ip; /* NOTE: network format */
906 uint16_t etype = 0; /* Host order stored ether type */
909 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
910 * MATCH_NONE when checked and not matched (q = NULL),
911 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
913 int dyn_dir = MATCH_UNKNOWN;
914 ipfw_dyn_rule *q = NULL;
915 struct ip_fw_chain *chain = &V_layer3_chain;
918 * We store in ulp a pointer to the upper layer protocol header.
919 * In the ipv4 case this is easy to determine from the header,
920 * but for ipv6 we might have some additional headers in the middle.
921 * ulp is NULL if not found.
923 void *ulp = NULL; /* upper layer protocol pointer. */
925 /* XXX ipv6 variables */
927 uint8_t icmp6_type = 0;
928 uint16_t ext_hd = 0; /* bits vector for extension header filtering */
929 /* end of ipv6 variables */
933 int done = 0; /* flag to exit the outer loop */
935 if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready))
936 return (IP_FW_PASS); /* accept */
938 dst_ip.s_addr = 0; /* make sure it is initialized */
939 src_ip.s_addr = 0; /* make sure it is initialized */
940 pktlen = m->m_pkthdr.len;
941 args->f_id.fib = M_GETFIB(m); /* note mbuf not altered) */
942 proto = args->f_id.proto = 0; /* mark f_id invalid */
943 /* XXX 0 is a valid proto: IP/IPv6 Hop-by-Hop Option */
946 * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous,
947 * then it sets p to point at the offset "len" in the mbuf. WARNING: the
948 * pointer might become stale after other pullups (but we never use it
951 #define PULLUP_TO(_len, p, T) PULLUP_LEN(_len, p, sizeof(T))
952 #define PULLUP_LEN(_len, p, T) \
954 int x = (_len) + T; \
955 if ((m)->m_len < x) { \
956 args->m = m = m_pullup(m, x); \
958 goto pullup_failed; \
960 p = (mtod(m, char *) + (_len)); \
964 * if we have an ether header,
967 etype = ntohs(args->eh->ether_type);
969 /* Identify IP packets and fill up variables. */
970 if (pktlen >= sizeof(struct ip6_hdr) &&
971 (args->eh == NULL || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) {
972 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
974 args->f_id.addr_type = 6;
975 hlen = sizeof(struct ip6_hdr);
976 proto = ip6->ip6_nxt;
978 /* Search extension headers to find upper layer protocols */
979 while (ulp == NULL && offset == 0) {
982 PULLUP_TO(hlen, ulp, struct icmp6_hdr);
983 icmp6_type = ICMP6(ulp)->icmp6_type;
987 PULLUP_TO(hlen, ulp, struct tcphdr);
988 dst_port = TCP(ulp)->th_dport;
989 src_port = TCP(ulp)->th_sport;
990 /* save flags for dynamic rules */
991 args->f_id._flags = TCP(ulp)->th_flags;
995 PULLUP_TO(hlen, ulp, struct sctphdr);
996 src_port = SCTP(ulp)->src_port;
997 dst_port = SCTP(ulp)->dest_port;
1001 PULLUP_TO(hlen, ulp, struct udphdr);
1002 dst_port = UDP(ulp)->uh_dport;
1003 src_port = UDP(ulp)->uh_sport;
1006 case IPPROTO_HOPOPTS: /* RFC 2460 */
1007 PULLUP_TO(hlen, ulp, struct ip6_hbh);
1008 ext_hd |= EXT_HOPOPTS;
1009 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1010 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1014 case IPPROTO_ROUTING: /* RFC 2460 */
1015 PULLUP_TO(hlen, ulp, struct ip6_rthdr);
1016 switch (((struct ip6_rthdr *)ulp)->ip6r_type) {
1018 ext_hd |= EXT_RTHDR0;
1021 ext_hd |= EXT_RTHDR2;
1025 printf("IPFW2: IPV6 - Unknown "
1026 "Routing Header type(%d)\n",
1027 ((struct ip6_rthdr *)
1029 if (V_fw_deny_unknown_exthdrs)
1030 return (IP_FW_DENY);
1033 ext_hd |= EXT_ROUTING;
1034 hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3;
1035 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt;
1039 case IPPROTO_FRAGMENT: /* RFC 2460 */
1040 PULLUP_TO(hlen, ulp, struct ip6_frag);
1041 ext_hd |= EXT_FRAGMENT;
1042 hlen += sizeof (struct ip6_frag);
1043 proto = ((struct ip6_frag *)ulp)->ip6f_nxt;
1044 offset = ((struct ip6_frag *)ulp)->ip6f_offlg &
1046 ip6f_mf = ((struct ip6_frag *)ulp)->ip6f_offlg &
1048 if (V_fw_permit_single_frag6 == 0 &&
1049 offset == 0 && ip6f_mf == 0) {
1051 printf("IPFW2: IPV6 - Invalid "
1052 "Fragment Header\n");
1053 if (V_fw_deny_unknown_exthdrs)
1054 return (IP_FW_DENY);
1058 ntohl(((struct ip6_frag *)ulp)->ip6f_ident);
1062 case IPPROTO_DSTOPTS: /* RFC 2460 */
1063 PULLUP_TO(hlen, ulp, struct ip6_hbh);
1064 ext_hd |= EXT_DSTOPTS;
1065 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1066 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1070 case IPPROTO_AH: /* RFC 2402 */
1071 PULLUP_TO(hlen, ulp, struct ip6_ext);
1073 hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2;
1074 proto = ((struct ip6_ext *)ulp)->ip6e_nxt;
1078 case IPPROTO_ESP: /* RFC 2406 */
1079 PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */
1080 /* Anything past Seq# is variable length and
1081 * data past this ext. header is encrypted. */
1085 case IPPROTO_NONE: /* RFC 2460 */
1087 * Packet ends here, and IPv6 header has
1088 * already been pulled up. If ip6e_len!=0
1089 * then octets must be ignored.
1091 ulp = ip; /* non-NULL to get out of loop. */
1094 case IPPROTO_OSPFIGP:
1095 /* XXX OSPF header check? */
1096 PULLUP_TO(hlen, ulp, struct ip6_ext);
1100 /* XXX PIM header check? */
1101 PULLUP_TO(hlen, ulp, struct pim);
1105 PULLUP_TO(hlen, ulp, struct carp_header);
1106 if (((struct carp_header *)ulp)->carp_version !=
1108 return (IP_FW_DENY);
1109 if (((struct carp_header *)ulp)->carp_type !=
1111 return (IP_FW_DENY);
1114 case IPPROTO_IPV6: /* RFC 2893 */
1115 PULLUP_TO(hlen, ulp, struct ip6_hdr);
1118 case IPPROTO_IPV4: /* RFC 2893 */
1119 PULLUP_TO(hlen, ulp, struct ip);
1124 printf("IPFW2: IPV6 - Unknown "
1125 "Extension Header(%d), ext_hd=%x\n",
1127 if (V_fw_deny_unknown_exthdrs)
1128 return (IP_FW_DENY);
1129 PULLUP_TO(hlen, ulp, struct ip6_ext);
1133 ip = mtod(m, struct ip *);
1134 ip6 = (struct ip6_hdr *)ip;
1135 args->f_id.src_ip6 = ip6->ip6_src;
1136 args->f_id.dst_ip6 = ip6->ip6_dst;
1137 args->f_id.src_ip = 0;
1138 args->f_id.dst_ip = 0;
1139 args->f_id.flow_id6 = ntohl(ip6->ip6_flow);
1140 } else if (pktlen >= sizeof(struct ip) &&
1141 (args->eh == NULL || etype == ETHERTYPE_IP) && ip->ip_v == 4) {
1143 hlen = ip->ip_hl << 2;
1144 args->f_id.addr_type = 4;
1147 * Collect parameters into local variables for faster matching.
1150 src_ip = ip->ip_src;
1151 dst_ip = ip->ip_dst;
1152 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1153 iplen = ntohs(ip->ip_len);
1154 pktlen = iplen < pktlen ? iplen : pktlen;
1159 PULLUP_TO(hlen, ulp, struct tcphdr);
1160 dst_port = TCP(ulp)->th_dport;
1161 src_port = TCP(ulp)->th_sport;
1162 /* save flags for dynamic rules */
1163 args->f_id._flags = TCP(ulp)->th_flags;
1167 PULLUP_TO(hlen, ulp, struct sctphdr);
1168 src_port = SCTP(ulp)->src_port;
1169 dst_port = SCTP(ulp)->dest_port;
1173 PULLUP_TO(hlen, ulp, struct udphdr);
1174 dst_port = UDP(ulp)->uh_dport;
1175 src_port = UDP(ulp)->uh_sport;
1179 PULLUP_TO(hlen, ulp, struct icmphdr);
1180 //args->f_id.flags = ICMP(ulp)->icmp_type;
1188 ip = mtod(m, struct ip *);
1189 args->f_id.src_ip = ntohl(src_ip.s_addr);
1190 args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1193 if (proto) { /* we may have port numbers, store them */
1194 args->f_id.proto = proto;
1195 args->f_id.src_port = src_port = ntohs(src_port);
1196 args->f_id.dst_port = dst_port = ntohs(dst_port);
1200 if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */
1201 IPFW_RUNLOCK(chain);
1202 return (IP_FW_PASS); /* accept */
1204 if (args->rule.slot) {
1206 * Packet has already been tagged as a result of a previous
1207 * match on rule args->rule aka args->rule_id (PIPE, QUEUE,
1208 * REASS, NETGRAPH, DIVERT/TEE...)
1209 * Validate the slot and continue from the next one
1210 * if still present, otherwise do a lookup.
1212 f_pos = (args->rule.chain_id == chain->id) ?
1214 ipfw_find_rule(chain, args->rule.rulenum,
1215 args->rule.rule_id);
1221 * Now scan the rules, and parse microinstructions for each rule.
1222 * We have two nested loops and an inner switch. Sometimes we
1223 * need to break out of one or both loops, or re-enter one of
1224 * the loops with updated variables. Loop variables are:
1226 * f_pos (outer loop) points to the current rule.
1227 * On output it points to the matching rule.
1228 * done (outer loop) is used as a flag to break the loop.
1229 * l (inner loop) residual length of current rule.
1230 * cmd points to the current microinstruction.
1232 * We break the inner loop by setting l=0 and possibly
1233 * cmdlen=0 if we don't want to advance cmd.
1234 * We break the outer loop by setting done=1
1235 * We can restart the inner loop by setting l>0 and f_pos, f, cmd
1238 for (; f_pos < chain->n_rules; f_pos++) {
1240 uint32_t tablearg = 0;
1241 int l, cmdlen, skip_or; /* skip rest of OR block */
1244 f = chain->map[f_pos];
1245 if (V_set_disable & (1 << f->set) )
1249 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
1250 l -= cmdlen, cmd += cmdlen) {
1254 * check_body is a jump target used when we find a
1255 * CHECK_STATE, and need to jump to the body of
1260 cmdlen = F_LEN(cmd);
1262 * An OR block (insn_1 || .. || insn_n) has the
1263 * F_OR bit set in all but the last instruction.
1264 * The first match will set "skip_or", and cause
1265 * the following instructions to be skipped until
1266 * past the one with the F_OR bit clear.
1268 if (skip_or) { /* skip this instruction */
1269 if ((cmd->len & F_OR) == 0)
1270 skip_or = 0; /* next one is good */
1273 match = 0; /* set to 1 if we succeed */
1275 switch (cmd->opcode) {
1277 * The first set of opcodes compares the packet's
1278 * fields with some pattern, setting 'match' if a
1279 * match is found. At the end of the loop there is
1280 * logic to deal with F_NOT and F_OR flags associated
1288 printf("ipfw: opcode %d unimplemented\n",
1296 * We only check offset == 0 && proto != 0,
1297 * as this ensures that we have a
1298 * packet with the ports info.
1302 if (proto == IPPROTO_TCP ||
1303 proto == IPPROTO_UDP)
1304 match = check_uidgid(
1305 (ipfw_insn_u32 *)cmd,
1306 args, &ucred_lookup,
1310 (void *)&ucred_cache);
1315 match = iface_match(m->m_pkthdr.rcvif,
1316 (ipfw_insn_if *)cmd);
1320 match = iface_match(oif, (ipfw_insn_if *)cmd);
1324 match = iface_match(oif ? oif :
1325 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd);
1329 if (args->eh != NULL) { /* have MAC header */
1330 u_int32_t *want = (u_int32_t *)
1331 ((ipfw_insn_mac *)cmd)->addr;
1332 u_int32_t *mask = (u_int32_t *)
1333 ((ipfw_insn_mac *)cmd)->mask;
1334 u_int32_t *hdr = (u_int32_t *)args->eh;
1337 ( want[0] == (hdr[0] & mask[0]) &&
1338 want[1] == (hdr[1] & mask[1]) &&
1339 want[2] == (hdr[2] & mask[2]) );
1344 if (args->eh != NULL) {
1346 ((ipfw_insn_u16 *)cmd)->ports;
1349 for (i = cmdlen - 1; !match && i>0;
1351 match = (etype >= p[0] &&
1357 match = (offset != 0);
1360 case O_IN: /* "out" is "not in" */
1361 match = (oif == NULL);
1365 match = (args->eh != NULL);
1370 /* For diverted packets, args->rule.info
1371 * contains the divert port (in host format)
1372 * reason and direction.
1374 uint32_t i = args->rule.info;
1375 match = (i&IPFW_IS_MASK) == IPFW_IS_DIVERT &&
1376 cmd->arg1 & ((i & IPFW_INFO_IN) ? 1 : 2);
1382 * We do not allow an arg of 0 so the
1383 * check of "proto" only suffices.
1385 match = (proto == cmd->arg1);
1390 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
1394 case O_IP_SRC_LOOKUP:
1395 case O_IP_DST_LOOKUP:
1398 (cmd->opcode == O_IP_DST_LOOKUP) ?
1399 dst_ip.s_addr : src_ip.s_addr;
1402 if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) {
1403 /* generic lookup. The key must be
1404 * in 32bit big-endian format.
1406 v = ((ipfw_insn_u32 *)cmd)->d[1];
1408 key = dst_ip.s_addr;
1410 key = src_ip.s_addr;
1411 else if (v == 6) /* dscp */
1412 key = (ip->ip_tos >> 2) & 0x3f;
1413 else if (offset != 0)
1415 else if (proto != IPPROTO_TCP &&
1416 proto != IPPROTO_UDP)
1419 key = htonl(dst_port);
1421 key = htonl(src_port);
1422 else if (v == 4 || v == 5) {
1424 (ipfw_insn_u32 *)cmd,
1425 args, &ucred_lookup,
1428 if (v == 4 /* O_UID */)
1429 key = ucred_cache->cr_uid;
1430 else if (v == 5 /* O_JAIL */)
1431 key = ucred_cache->cr_prison->pr_id;
1432 #else /* !__FreeBSD__ */
1433 (void *)&ucred_cache);
1434 if (v ==4 /* O_UID */)
1435 key = ucred_cache.uid;
1436 else if (v == 5 /* O_JAIL */)
1437 key = ucred_cache.xid;
1438 #endif /* !__FreeBSD__ */
1443 match = ipfw_lookup_table(chain,
1444 cmd->arg1, key, &v);
1447 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
1449 ((ipfw_insn_u32 *)cmd)->d[0] == v;
1459 (cmd->opcode == O_IP_DST_MASK) ?
1460 dst_ip.s_addr : src_ip.s_addr;
1461 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d;
1464 for (; !match && i>0; i-= 2, p+= 2)
1465 match = (p[0] == (a & p[1]));
1473 INADDR_TO_IFP(src_ip, tif);
1474 match = (tif != NULL);
1480 match= is_ipv6 && search_ip6_addr_net(&args->f_id.src_ip6);
1487 u_int32_t *d = (u_int32_t *)(cmd+1);
1489 cmd->opcode == O_IP_DST_SET ?
1495 addr -= d[0]; /* subtract base */
1496 match = (addr < cmd->arg1) &&
1497 ( d[ 1 + (addr>>5)] &
1498 (1<<(addr & 0x1f)) );
1504 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
1512 INADDR_TO_IFP(dst_ip, tif);
1513 match = (tif != NULL);
1519 match= is_ipv6 && search_ip6_addr_net(&args->f_id.dst_ip6);
1527 * offset == 0 && proto != 0 is enough
1528 * to guarantee that we have a
1529 * packet with port info.
1531 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP)
1534 (cmd->opcode == O_IP_SRCPORT) ?
1535 src_port : dst_port ;
1537 ((ipfw_insn_u16 *)cmd)->ports;
1540 for (i = cmdlen - 1; !match && i>0;
1542 match = (x>=p[0] && x<=p[1]);
1547 match = (offset == 0 && proto==IPPROTO_ICMP &&
1548 icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) );
1553 match = is_ipv6 && offset == 0 &&
1554 proto==IPPROTO_ICMPV6 &&
1556 ICMP6(ulp)->icmp6_type,
1557 (ipfw_insn_u32 *)cmd);
1563 ipopts_match(ip, cmd) );
1568 cmd->arg1 == ip->ip_v);
1574 if (is_ipv4) { /* only for IP packets */
1579 if (cmd->opcode == O_IPLEN)
1581 else if (cmd->opcode == O_IPTTL)
1583 else /* must be IPID */
1584 x = ntohs(ip->ip_id);
1586 match = (cmd->arg1 == x);
1589 /* otherwise we have ranges */
1590 p = ((ipfw_insn_u16 *)cmd)->ports;
1592 for (; !match && i>0; i--, p += 2)
1593 match = (x >= p[0] && x <= p[1]);
1597 case O_IPPRECEDENCE:
1599 (cmd->arg1 == (ip->ip_tos & 0xe0)) );
1604 flags_match(cmd, ip->ip_tos));
1608 if (proto == IPPROTO_TCP && offset == 0) {
1616 ((ip->ip_hl + tcp->th_off) << 2);
1618 match = (cmd->arg1 == x);
1621 /* otherwise we have ranges */
1622 p = ((ipfw_insn_u16 *)cmd)->ports;
1624 for (; !match && i>0; i--, p += 2)
1625 match = (x >= p[0] && x <= p[1]);
1630 match = (proto == IPPROTO_TCP && offset == 0 &&
1631 flags_match(cmd, TCP(ulp)->th_flags));
1635 PULLUP_LEN(hlen, ulp, (TCP(ulp)->th_off << 2));
1636 match = (proto == IPPROTO_TCP && offset == 0 &&
1637 tcpopts_match(TCP(ulp), cmd));
1641 match = (proto == IPPROTO_TCP && offset == 0 &&
1642 ((ipfw_insn_u32 *)cmd)->d[0] ==
1647 match = (proto == IPPROTO_TCP && offset == 0 &&
1648 ((ipfw_insn_u32 *)cmd)->d[0] ==
1653 match = (proto == IPPROTO_TCP && offset == 0 &&
1654 cmd->arg1 == TCP(ulp)->th_win);
1658 /* reject packets which have SYN only */
1659 /* XXX should i also check for TH_ACK ? */
1660 match = (proto == IPPROTO_TCP && offset == 0 &&
1661 (TCP(ulp)->th_flags &
1662 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
1667 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
1670 at = pf_find_mtag(m);
1671 if (at != NULL && at->qid != 0)
1673 at = pf_get_mtag(m);
1676 * Let the packet fall back to the
1681 at->qid = altq->qid;
1687 ipfw_log(f, hlen, args, m,
1688 oif, offset | ip6f_mf, tablearg, ip);
1693 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]);
1697 /* Outgoing packets automatically pass/match */
1698 match = ((oif != NULL) ||
1699 (m->m_pkthdr.rcvif == NULL) ||
1703 verify_path6(&(args->f_id.src_ip6),
1704 m->m_pkthdr.rcvif) :
1706 verify_path(src_ip, m->m_pkthdr.rcvif,
1711 /* Outgoing packets automatically pass/match */
1712 match = (hlen > 0 && ((oif != NULL) ||
1715 verify_path6(&(args->f_id.src_ip6),
1718 verify_path(src_ip, NULL, args->f_id.fib)));
1722 /* Outgoing packets automatically pass/match */
1723 if (oif == NULL && hlen > 0 &&
1724 ( (is_ipv4 && in_localaddr(src_ip))
1727 in6_localaddr(&(args->f_id.src_ip6)))
1732 is_ipv6 ? verify_path6(
1733 &(args->f_id.src_ip6),
1734 m->m_pkthdr.rcvif) :
1745 match = (m_tag_find(m,
1746 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL);
1748 /* otherwise no match */
1754 IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6,
1755 &((ipfw_insn_ip6 *)cmd)->addr6);
1760 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6,
1761 &((ipfw_insn_ip6 *)cmd)->addr6);
1763 case O_IP6_SRC_MASK:
1764 case O_IP6_DST_MASK:
1768 struct in6_addr *d =
1769 &((ipfw_insn_ip6 *)cmd)->addr6;
1771 for (; !match && i > 0; d += 2,
1772 i -= F_INSN_SIZE(struct in6_addr)
1778 APPLY_MASK(&p, &d[1]);
1780 IN6_ARE_ADDR_EQUAL(&d[0],
1788 flow6id_match(args->f_id.flow_id6,
1789 (ipfw_insn_u32 *) cmd);
1794 (ext_hd & ((ipfw_insn *) cmd)->arg1);
1808 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ?
1809 tablearg : cmd->arg1;
1811 /* Packet is already tagged with this tag? */
1812 mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL);
1814 /* We have `untag' action when F_NOT flag is
1815 * present. And we must remove this mtag from
1816 * mbuf and reset `match' to zero (`match' will
1817 * be inversed later).
1818 * Otherwise we should allocate new mtag and
1819 * push it into mbuf.
1821 if (cmd->len & F_NOT) { /* `untag' action */
1823 m_tag_delete(m, mtag);
1827 mtag = m_tag_alloc( MTAG_IPFW,
1830 m_tag_prepend(m, mtag);
1837 case O_FIB: /* try match the specified fib */
1838 if (args->f_id.fib == cmd->arg1)
1843 struct inpcb *inp = args->inp;
1844 struct inpcbinfo *pi;
1846 if (is_ipv6) /* XXX can we remove this ? */
1849 if (proto == IPPROTO_TCP)
1851 else if (proto == IPPROTO_UDP)
1857 * XXXRW: so_user_cookie should almost
1858 * certainly be inp_user_cookie?
1861 /* For incomming packet, lookup up the
1862 inpcb using the src/dest ip/port tuple */
1864 inp = in_pcblookup(pi,
1865 src_ip, htons(src_port),
1866 dst_ip, htons(dst_port),
1867 INPLOOKUP_RLOCKPCB, NULL);
1870 inp->inp_socket->so_user_cookie;
1876 if (inp->inp_socket) {
1878 inp->inp_socket->so_user_cookie;
1888 uint32_t tag = (cmd->arg1 == IP_FW_TABLEARG) ?
1889 tablearg : cmd->arg1;
1892 match = m_tag_locate(m, MTAG_IPFW,
1897 /* we have ranges */
1898 for (mtag = m_tag_first(m);
1899 mtag != NULL && !match;
1900 mtag = m_tag_next(m, mtag)) {
1904 if (mtag->m_tag_cookie != MTAG_IPFW)
1907 p = ((ipfw_insn_u16 *)cmd)->ports;
1909 for(; !match && i > 0; i--, p += 2)
1911 mtag->m_tag_id >= p[0] &&
1912 mtag->m_tag_id <= p[1];
1918 * The second set of opcodes represents 'actions',
1919 * i.e. the terminal part of a rule once the packet
1920 * matches all previous patterns.
1921 * Typically there is only one action for each rule,
1922 * and the opcode is stored at the end of the rule
1923 * (but there are exceptions -- see below).
1925 * In general, here we set retval and terminate the
1926 * outer loop (would be a 'break 3' in some language,
1927 * but we need to set l=0, done=1)
1930 * O_COUNT and O_SKIPTO actions:
1931 * instead of terminating, we jump to the next rule
1932 * (setting l=0), or to the SKIPTO target (setting
1933 * f/f_len, cmd and l as needed), respectively.
1935 * O_TAG, O_LOG and O_ALTQ action parameters:
1936 * perform some action and set match = 1;
1938 * O_LIMIT and O_KEEP_STATE: these opcodes are
1939 * not real 'actions', and are stored right
1940 * before the 'action' part of the rule.
1941 * These opcodes try to install an entry in the
1942 * state tables; if successful, we continue with
1943 * the next opcode (match=1; break;), otherwise
1944 * the packet must be dropped (set retval,
1945 * break loops with l=0, done=1)
1947 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
1948 * cause a lookup of the state table, and a jump
1949 * to the 'action' part of the parent rule
1950 * if an entry is found, or
1951 * (CHECK_STATE only) a jump to the next rule if
1952 * the entry is not found.
1953 * The result of the lookup is cached so that
1954 * further instances of these opcodes become NOPs.
1955 * The jump to the next rule is done by setting
1960 if (ipfw_install_state(f,
1961 (ipfw_insn_limit *)cmd, args, tablearg)) {
1962 /* error or limit violation */
1963 retval = IP_FW_DENY;
1964 l = 0; /* exit inner loop */
1965 done = 1; /* exit outer loop */
1973 * dynamic rules are checked at the first
1974 * keep-state or check-state occurrence,
1975 * with the result being stored in dyn_dir.
1976 * The compiler introduces a PROBE_STATE
1977 * instruction for us when we have a
1978 * KEEP_STATE (because PROBE_STATE needs
1981 if (dyn_dir == MATCH_UNKNOWN &&
1982 (q = ipfw_lookup_dyn_rule(&args->f_id,
1983 &dyn_dir, proto == IPPROTO_TCP ?
1987 * Found dynamic entry, update stats
1988 * and jump to the 'action' part of
1989 * the parent rule by setting
1990 * f, cmd, l and clearing cmdlen.
1994 /* XXX we would like to have f_pos
1995 * readily accessible in the dynamic
1996 * rule, instead of having to
2000 f_pos = ipfw_find_rule(chain,
2002 cmd = ACTION_PTR(f);
2003 l = f->cmd_len - f->act_ofs;
2010 * Dynamic entry not found. If CHECK_STATE,
2011 * skip to next rule, if PROBE_STATE just
2012 * ignore and continue with next opcode.
2014 if (cmd->opcode == O_CHECK_STATE)
2015 l = 0; /* exit inner loop */
2020 retval = 0; /* accept */
2021 l = 0; /* exit inner loop */
2022 done = 1; /* exit outer loop */
2027 set_match(args, f_pos, chain);
2028 args->rule.info = (cmd->arg1 == IP_FW_TABLEARG) ?
2029 tablearg : cmd->arg1;
2030 if (cmd->opcode == O_PIPE)
2031 args->rule.info |= IPFW_IS_PIPE;
2033 args->rule.info |= IPFW_ONEPASS;
2034 retval = IP_FW_DUMMYNET;
2035 l = 0; /* exit inner loop */
2036 done = 1; /* exit outer loop */
2041 if (args->eh) /* not on layer 2 */
2043 /* otherwise this is terminal */
2044 l = 0; /* exit inner loop */
2045 done = 1; /* exit outer loop */
2046 retval = (cmd->opcode == O_DIVERT) ?
2047 IP_FW_DIVERT : IP_FW_TEE;
2048 set_match(args, f_pos, chain);
2049 args->rule.info = (cmd->arg1 == IP_FW_TABLEARG) ?
2050 tablearg : cmd->arg1;
2054 f->pcnt++; /* update stats */
2056 f->timestamp = time_uptime;
2057 l = 0; /* exit inner loop */
2061 f->pcnt++; /* update stats */
2063 f->timestamp = time_uptime;
2064 /* If possible use cached f_pos (in f->next_rule),
2065 * whose version is written in f->next_rule
2066 * (horrible hacks to avoid changing the ABI).
2068 if (cmd->arg1 != IP_FW_TABLEARG &&
2069 (uintptr_t)f->x_next == chain->id) {
2070 f_pos = (uintptr_t)f->next_rule;
2072 int i = (cmd->arg1 == IP_FW_TABLEARG) ?
2073 tablearg : cmd->arg1;
2074 /* make sure we do not jump backward */
2075 if (i <= f->rulenum)
2077 f_pos = ipfw_find_rule(chain, i, 0);
2078 /* update the cache */
2079 if (cmd->arg1 != IP_FW_TABLEARG) {
2081 (void *)(uintptr_t)f_pos;
2083 (void *)(uintptr_t)chain->id;
2087 * Skip disabled rules, and re-enter
2088 * the inner loop with the correct
2089 * f_pos, f, l and cmd.
2090 * Also clear cmdlen and skip_or
2092 for (; f_pos < chain->n_rules - 1 &&
2094 (1 << chain->map[f_pos]->set));
2097 /* Re-enter the inner loop at the skipto rule. */
2098 f = chain->map[f_pos];
2105 break; /* not reached */
2107 case O_CALLRETURN: {
2109 * Implementation of `subroutine' call/return,
2110 * in the stack carried in an mbuf tag. This
2111 * is different from `skipto' in that any call
2112 * address is possible (`skipto' must prevent
2113 * backward jumps to avoid endless loops).
2114 * We have `return' action when F_NOT flag is
2115 * present. The `m_tag_id' field is used as
2119 uint16_t jmpto, *stack;
2121 #define IS_CALL ((cmd->len & F_NOT) == 0)
2122 #define IS_RETURN ((cmd->len & F_NOT) != 0)
2124 * Hand-rolled version of m_tag_locate() with
2126 * If not already tagged, allocate new tag.
2128 mtag = m_tag_first(m);
2129 while (mtag != NULL) {
2130 if (mtag->m_tag_cookie ==
2133 mtag = m_tag_next(m, mtag);
2135 if (mtag == NULL && IS_CALL) {
2136 mtag = m_tag_alloc(MTAG_IPFW_CALL, 0,
2137 IPFW_CALLSTACK_SIZE *
2138 sizeof(uint16_t), M_NOWAIT);
2140 m_tag_prepend(m, mtag);
2144 * On error both `call' and `return' just
2145 * continue with next rule.
2147 if (IS_RETURN && (mtag == NULL ||
2148 mtag->m_tag_id == 0)) {
2149 l = 0; /* exit inner loop */
2152 if (IS_CALL && (mtag == NULL ||
2153 mtag->m_tag_id >= IPFW_CALLSTACK_SIZE)) {
2154 printf("ipfw: call stack error, "
2155 "go to next rule\n");
2156 l = 0; /* exit inner loop */
2160 f->pcnt++; /* update stats */
2162 f->timestamp = time_uptime;
2163 stack = (uint16_t *)(mtag + 1);
2166 * The `call' action may use cached f_pos
2167 * (in f->next_rule), whose version is written
2169 * The `return' action, however, doesn't have
2170 * fixed jump address in cmd->arg1 and can't use
2174 stack[mtag->m_tag_id] = f->rulenum;
2176 if (cmd->arg1 != IP_FW_TABLEARG &&
2177 (uintptr_t)f->x_next == chain->id) {
2178 f_pos = (uintptr_t)f->next_rule;
2180 jmpto = (cmd->arg1 ==
2181 IP_FW_TABLEARG) ? tablearg:
2183 f_pos = ipfw_find_rule(chain,
2185 /* update the cache */
2196 } else { /* `return' action */
2198 jmpto = stack[mtag->m_tag_id] + 1;
2199 f_pos = ipfw_find_rule(chain, jmpto, 0);
2203 * Skip disabled rules, and re-enter
2204 * the inner loop with the correct
2205 * f_pos, f, l and cmd.
2206 * Also clear cmdlen and skip_or
2208 for (; f_pos < chain->n_rules - 1 &&
2210 (1 << chain->map[f_pos]->set)); f_pos++)
2212 /* Re-enter the inner loop at the dest rule. */
2213 f = chain->map[f_pos];
2219 break; /* NOTREACHED */
2226 * Drop the packet and send a reject notice
2227 * if the packet is not ICMP (or is an ICMP
2228 * query), and it is not multicast/broadcast.
2230 if (hlen > 0 && is_ipv4 && offset == 0 &&
2231 (proto != IPPROTO_ICMP ||
2232 is_icmp_query(ICMP(ulp))) &&
2233 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2234 !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2235 send_reject(args, cmd->arg1, iplen, ip);
2241 if (hlen > 0 && is_ipv6 &&
2242 ((offset & IP6F_OFF_MASK) == 0) &&
2243 (proto != IPPROTO_ICMPV6 ||
2244 (is_icmp6_query(icmp6_type) == 1)) &&
2245 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2246 !IN6_IS_ADDR_MULTICAST(&args->f_id.dst_ip6)) {
2248 args, cmd->arg1, hlen,
2249 (struct ip6_hdr *)ip);
2255 retval = IP_FW_DENY;
2256 l = 0; /* exit inner loop */
2257 done = 1; /* exit outer loop */
2261 if (args->eh) /* not valid on layer2 pkts */
2263 if (q == NULL || q->rule != f ||
2264 dyn_dir == MATCH_FORWARD) {
2265 struct sockaddr_in *sa;
2266 sa = &(((ipfw_insn_sa *)cmd)->sa);
2267 if (sa->sin_addr.s_addr == INADDR_ANY) {
2268 bcopy(sa, &args->hopstore,
2270 args->hopstore.sin_addr.s_addr =
2272 args->next_hop = &args->hopstore;
2274 args->next_hop = sa;
2277 retval = IP_FW_PASS;
2278 l = 0; /* exit inner loop */
2279 done = 1; /* exit outer loop */
2284 if (args->eh) /* not valid on layer2 pkts */
2286 if (q == NULL || q->rule != f ||
2287 dyn_dir == MATCH_FORWARD) {
2288 struct sockaddr_in6 *sin6;
2290 sin6 = &(((ipfw_insn_sa6 *)cmd)->sa);
2291 args->next_hop6 = sin6;
2293 retval = IP_FW_PASS;
2294 l = 0; /* exit inner loop */
2295 done = 1; /* exit outer loop */
2301 set_match(args, f_pos, chain);
2302 args->rule.info = (cmd->arg1 == IP_FW_TABLEARG) ?
2303 tablearg : cmd->arg1;
2305 args->rule.info |= IPFW_ONEPASS;
2306 retval = (cmd->opcode == O_NETGRAPH) ?
2307 IP_FW_NETGRAPH : IP_FW_NGTEE;
2308 l = 0; /* exit inner loop */
2309 done = 1; /* exit outer loop */
2315 f->pcnt++; /* update stats */
2317 f->timestamp = time_uptime;
2318 fib = (cmd->arg1 == IP_FW_TABLEARG) ? tablearg:
2320 if (fib >= rt_numfibs)
2323 args->f_id.fib = fib;
2324 l = 0; /* exit inner loop */
2329 if (!IPFW_NAT_LOADED) {
2330 retval = IP_FW_DENY;
2335 set_match(args, f_pos, chain);
2336 /* Check if this is 'global' nat rule */
2337 if (cmd->arg1 == 0) {
2338 retval = ipfw_nat_ptr(args, NULL, m);
2343 t = ((ipfw_insn_nat *)cmd)->nat;
2345 nat_id = (cmd->arg1 == IP_FW_TABLEARG) ?
2346 tablearg : cmd->arg1;
2347 t = (*lookup_nat_ptr)(&chain->nat, nat_id);
2350 retval = IP_FW_DENY;
2351 l = 0; /* exit inner loop */
2352 done = 1; /* exit outer loop */
2355 if (cmd->arg1 != IP_FW_TABLEARG)
2356 ((ipfw_insn_nat *)cmd)->nat = t;
2358 retval = ipfw_nat_ptr(args, t, m);
2360 l = 0; /* exit inner loop */
2361 done = 1; /* exit outer loop */
2369 l = 0; /* in any case exit inner loop */
2370 ip_off = ntohs(ip->ip_off);
2372 /* if not fragmented, go to next rule */
2373 if ((ip_off & (IP_MF | IP_OFFMASK)) == 0)
2376 * ip_reass() expects len & off in host
2381 args->m = m = ip_reass(m);
2384 * do IP header checksum fixup.
2386 if (m == NULL) { /* fragment got swallowed */
2387 retval = IP_FW_DENY;
2388 } else { /* good, packet complete */
2391 ip = mtod(m, struct ip *);
2392 hlen = ip->ip_hl << 2;
2395 if (hlen == sizeof(struct ip))
2396 ip->ip_sum = in_cksum_hdr(ip);
2398 ip->ip_sum = in_cksum(m, hlen);
2399 retval = IP_FW_REASS;
2400 set_match(args, f_pos, chain);
2402 done = 1; /* exit outer loop */
2407 panic("-- unknown opcode %d\n", cmd->opcode);
2408 } /* end of switch() on opcodes */
2410 * if we get here with l=0, then match is irrelevant.
2413 if (cmd->len & F_NOT)
2417 if (cmd->len & F_OR)
2420 if (!(cmd->len & F_OR)) /* not an OR block, */
2421 break; /* try next rule */
2424 } /* end of inner loop, scan opcodes */
2430 /* next_rule:; */ /* try next rule */
2432 } /* end of outer for, scan rules */
2435 struct ip_fw *rule = chain->map[f_pos];
2436 /* Update statistics */
2438 rule->bcnt += pktlen;
2439 rule->timestamp = time_uptime;
2441 retval = IP_FW_DENY;
2442 printf("ipfw: ouch!, skip past end of rules, denying packet\n");
2444 IPFW_RUNLOCK(chain);
2446 if (ucred_cache != NULL)
2447 crfree(ucred_cache);
2453 printf("ipfw: pullup failed\n");
2454 return (IP_FW_DENY);
2458 * Module and VNET glue
2462 * Stuff that must be initialised only on boot or module load
2471 * Only print out this stuff the first time around,
2472 * when called from the sysinit code.
2478 "initialized, divert %s, nat %s, "
2479 "rule-based forwarding "
2480 #ifdef IPFIREWALL_FORWARD
2485 "default to %s, logging ",
2491 #ifdef IPFIREWALL_NAT
2496 default_to_accept ? "accept" : "deny");
2499 * Note: V_xxx variables can be accessed here but the vnet specific
2500 * initializer may not have been called yet for the VIMAGE case.
2501 * Tuneables will have been processed. We will print out values for
2503 * XXX This should all be rationalized AFTER 8.0
2505 if (V_fw_verbose == 0)
2506 printf("disabled\n");
2507 else if (V_verbose_limit == 0)
2508 printf("unlimited\n");
2510 printf("limited to %d packets/entry by default\n",
2513 ipfw_log_bpf(1); /* init */
2518 * Called for the removal of the last instance only on module unload.
2524 ipfw_log_bpf(0); /* uninit */
2526 printf("IP firewall unloaded\n");
2530 * Stuff that must be initialized for every instance
2531 * (including the first of course).
2534 vnet_ipfw_init(const void *unused)
2537 struct ip_fw *rule = NULL;
2538 struct ip_fw_chain *chain;
2540 chain = &V_layer3_chain;
2542 /* First set up some values that are compile time options */
2543 V_autoinc_step = 100; /* bounded to 1..1000 in add_rule() */
2544 V_fw_deny_unknown_exthdrs = 1;
2545 #ifdef IPFIREWALL_VERBOSE
2548 #ifdef IPFIREWALL_VERBOSE_LIMIT
2549 V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
2551 #ifdef IPFIREWALL_NAT
2552 LIST_INIT(&chain->nat);
2555 /* insert the default rule and create the initial map */
2557 chain->static_len = sizeof(struct ip_fw);
2558 chain->map = malloc(sizeof(struct ip_fw *), M_IPFW, M_NOWAIT | M_ZERO);
2560 rule = malloc(chain->static_len, M_IPFW, M_NOWAIT | M_ZERO);
2563 free(chain->map, M_IPFW);
2564 printf("ipfw2: ENOSPC initializing default rule "
2565 "(support disabled)\n");
2568 error = ipfw_init_tables(chain);
2570 panic("init_tables"); /* XXX Marko fix this ! */
2573 /* fill and insert the default rule */
2575 rule->rulenum = IPFW_DEFAULT_RULE;
2577 rule->set = RESVD_SET;
2578 rule->cmd[0].len = 1;
2579 rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY;
2580 chain->rules = chain->default_rule = chain->map[0] = rule;
2581 chain->id = rule->id = 1;
2583 IPFW_LOCK_INIT(chain);
2586 /* First set up some values that are compile time options */
2587 V_ipfw_vnet_ready = 1; /* Open for business */
2590 * Hook the sockopt handler, and the layer2 (V_ip_fw_chk_ptr)
2591 * and pfil hooks for ipv4 and ipv6. Even if the latter two fail
2592 * we still keep the module alive because the sockopt and
2593 * layer2 paths are still useful.
2594 * ipfw[6]_hook return 0 on success, ENOENT on failure,
2595 * so we can ignore the exact return value and just set a flag.
2597 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so
2598 * changes in the underlying (per-vnet) variables trigger
2599 * immediate hook()/unhook() calls.
2600 * In layer2 we have the same behaviour, except that V_ether_ipfw
2601 * is checked on each packet because there are no pfil hooks.
2603 V_ip_fw_ctl_ptr = ipfw_ctl;
2604 V_ip_fw_chk_ptr = ipfw_chk;
2605 error = ipfw_attach_hooks(1);
2610 * Called for the removal of each instance.
2613 vnet_ipfw_uninit(const void *unused)
2615 struct ip_fw *reap, *rule;
2616 struct ip_fw_chain *chain = &V_layer3_chain;
2619 V_ipfw_vnet_ready = 0; /* tell new callers to go away */
2621 * disconnect from ipv4, ipv6, layer2 and sockopt.
2622 * Then grab, release and grab again the WLOCK so we make
2623 * sure the update is propagated and nobody will be in.
2625 (void)ipfw_attach_hooks(0 /* detach */);
2626 V_ip_fw_chk_ptr = NULL;
2627 V_ip_fw_ctl_ptr = NULL;
2628 IPFW_UH_WLOCK(chain);
2629 IPFW_UH_WUNLOCK(chain);
2630 IPFW_UH_WLOCK(chain);
2633 IPFW_WUNLOCK(chain);
2636 ipfw_dyn_uninit(0); /* run the callout_drain */
2637 ipfw_destroy_tables(chain);
2639 for (i = 0; i < chain->n_rules; i++) {
2640 rule = chain->map[i];
2641 rule->x_next = reap;
2645 free(chain->map, M_IPFW);
2646 IPFW_WUNLOCK(chain);
2647 IPFW_UH_WUNLOCK(chain);
2649 ipfw_reap_rules(reap);
2650 IPFW_LOCK_DESTROY(chain);
2651 ipfw_dyn_uninit(1); /* free the remaining parts */
2656 * Module event handler.
2657 * In general we have the choice of handling most of these events by the
2658 * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to
2659 * use the SYSINIT handlers as they are more capable of expressing the
2660 * flow of control during module and vnet operations, so this is just
2661 * a skeleton. Note there is no SYSINIT equivalent of the module
2662 * SHUTDOWN handler, but we don't have anything to do in that case anyhow.
2665 ipfw_modevent(module_t mod, int type, void *unused)
2671 /* Called once at module load or
2672 * system boot if compiled in. */
2675 /* Called before unload. May veto unloading. */
2678 /* Called during unload. */
2681 /* Called during system shutdown. */
2690 static moduledata_t ipfwmod = {
2696 /* Define startup order. */
2697 #define IPFW_SI_SUB_FIREWALL SI_SUB_PROTO_IFATTACHDOMAIN
2698 #define IPFW_MODEVENT_ORDER (SI_ORDER_ANY - 255) /* On boot slot in here. */
2699 #define IPFW_MODULE_ORDER (IPFW_MODEVENT_ORDER + 1) /* A little later. */
2700 #define IPFW_VNET_ORDER (IPFW_MODEVENT_ORDER + 2) /* Later still. */
2702 DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER);
2703 MODULE_VERSION(ipfw, 2);
2704 /* should declare some dependencies here */
2707 * Starting up. Done in order after ipfwmod() has been called.
2708 * VNET_SYSINIT is also called for each existing vnet and each new vnet.
2710 SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
2712 VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
2713 vnet_ipfw_init, NULL);
2716 * Closing up shop. These are done in REVERSE ORDER, but still
2717 * after ipfwmod() has been called. Not called on reboot.
2718 * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
2719 * or when the module is unloaded.
2721 SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
2722 ipfw_destroy, NULL);
2723 VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
2724 vnet_ipfw_uninit, NULL);