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"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/condvar.h>
44 #include <sys/counter.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/rmlock.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
60 #include <sys/ucred.h>
61 #include <net/ethernet.h> /* for ETHERTYPE_IP */
63 #include <net/if_var.h>
64 #include <net/route.h>
68 #include <netpfil/pf/pf_mtag.h>
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_icmp.h>
76 #include <netinet/ip_fw.h>
77 #include <netinet/ip_carp.h>
78 #include <netinet/pim.h>
79 #include <netinet/tcp_var.h>
80 #include <netinet/udp.h>
81 #include <netinet/udp_var.h>
82 #include <netinet/sctp.h>
83 #include <netinet/sctp_crc32.h>
84 #include <netinet/sctp_header.h>
86 #include <netinet/ip6.h>
87 #include <netinet/icmp6.h>
88 #include <netinet/in_fib.h>
90 #include <netinet6/in6_fib.h>
91 #include <netinet6/in6_pcb.h>
92 #include <netinet6/scope6_var.h>
93 #include <netinet6/ip6_var.h>
96 #include <net/if_gre.h> /* for struct grehdr */
98 #include <netpfil/ipfw/ip_fw_private.h>
100 #include <machine/in_cksum.h> /* XXX for in_cksum */
103 #include <security/mac/mac_framework.h>
107 * static variables followed by global ones.
108 * All ipfw global variables are here.
111 static VNET_DEFINE(int, fw_deny_unknown_exthdrs);
112 #define V_fw_deny_unknown_exthdrs VNET(fw_deny_unknown_exthdrs)
114 static VNET_DEFINE(int, fw_permit_single_frag6) = 1;
115 #define V_fw_permit_single_frag6 VNET(fw_permit_single_frag6)
117 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
118 static int default_to_accept = 1;
120 static int default_to_accept;
123 VNET_DEFINE(int, autoinc_step);
124 VNET_DEFINE(int, fw_one_pass) = 1;
126 VNET_DEFINE(unsigned int, fw_tables_max);
127 VNET_DEFINE(unsigned int, fw_tables_sets) = 0; /* Don't use set-aware tables */
128 /* Use 128 tables by default */
129 static unsigned int default_fw_tables = IPFW_TABLES_DEFAULT;
131 #ifndef LINEAR_SKIPTO
132 static int jump_fast(struct ip_fw_chain *chain, struct ip_fw *f, int num,
133 int tablearg, int jump_backwards);
134 #define JUMP(ch, f, num, targ, back) jump_fast(ch, f, num, targ, back)
136 static int jump_linear(struct ip_fw_chain *chain, struct ip_fw *f, int num,
137 int tablearg, int jump_backwards);
138 #define JUMP(ch, f, num, targ, back) jump_linear(ch, f, num, targ, back)
142 * Each rule belongs to one of 32 different sets (0..31).
143 * The variable set_disable contains one bit per set.
144 * If the bit is set, all rules in the corresponding set
145 * are disabled. Set RESVD_SET(31) is reserved for the default rule
146 * and rules that are not deleted by the flush command,
147 * and CANNOT be disabled.
148 * Rules in set RESVD_SET can only be deleted individually.
150 VNET_DEFINE(u_int32_t, set_disable);
151 #define V_set_disable VNET(set_disable)
153 VNET_DEFINE(int, fw_verbose);
154 /* counter for ipfw_log(NULL...) */
155 VNET_DEFINE(u_int64_t, norule_counter);
156 VNET_DEFINE(int, verbose_limit);
158 /* layer3_chain contains the list of rules for layer 3 */
159 VNET_DEFINE(struct ip_fw_chain, layer3_chain);
161 /* ipfw_vnet_ready controls when we are open for business */
162 VNET_DEFINE(int, ipfw_vnet_ready) = 0;
164 VNET_DEFINE(int, ipfw_nat_ready) = 0;
166 ipfw_nat_t *ipfw_nat_ptr = NULL;
167 struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
168 ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
169 ipfw_nat_cfg_t *ipfw_nat_del_ptr;
170 ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
171 ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
174 uint32_t dummy_def = IPFW_DEFAULT_RULE;
175 static int sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS);
176 static int sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS);
180 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
181 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, one_pass,
182 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0,
183 "Only do a single pass through ipfw when using dummynet(4)");
184 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step,
185 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(autoinc_step), 0,
186 "Rule number auto-increment step");
187 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose,
188 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0,
189 "Log matches to ipfw rules");
190 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit,
191 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(verbose_limit), 0,
192 "Set upper limit of matches of ipfw rules logged");
193 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD,
195 "The default/max possible rule number.");
196 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_max,
197 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, 0, 0, sysctl_ipfw_table_num, "IU",
198 "Maximum number of concurrently used tables");
199 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_sets,
200 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW,
201 0, 0, sysctl_ipfw_tables_sets, "IU",
202 "Use per-set namespace for tables");
203 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN,
204 &default_to_accept, 0,
205 "Make the default rule accept all packets.");
206 TUNABLE_INT("net.inet.ip.fw.tables_max", (int *)&default_fw_tables);
207 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count,
208 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0,
209 "Number of static rules");
212 SYSCTL_DECL(_net_inet6_ip6);
213 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
214 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs,
215 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
216 &VNET_NAME(fw_deny_unknown_exthdrs), 0,
217 "Deny packets with unknown IPv6 Extension Headers");
218 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, permit_single_frag6,
219 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
220 &VNET_NAME(fw_permit_single_frag6), 0,
221 "Permit single packet IPv6 fragments");
226 #endif /* SYSCTL_NODE */
230 * Some macros used in the various matching options.
231 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
232 * Other macros just cast void * into the appropriate type
234 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
235 #define TCP(p) ((struct tcphdr *)(p))
236 #define SCTP(p) ((struct sctphdr *)(p))
237 #define UDP(p) ((struct udphdr *)(p))
238 #define ICMP(p) ((struct icmphdr *)(p))
239 #define ICMP6(p) ((struct icmp6_hdr *)(p))
242 icmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd)
244 int type = icmp->icmp_type;
246 return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) );
249 #define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
250 (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
253 is_icmp_query(struct icmphdr *icmp)
255 int type = icmp->icmp_type;
257 return (type <= ICMP_MAXTYPE && (TT & (1<<type)) );
262 * The following checks use two arrays of 8 or 16 bits to store the
263 * bits that we want set or clear, respectively. They are in the
264 * low and high half of cmd->arg1 or cmd->d[0].
266 * We scan options and store the bits we find set. We succeed if
268 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
270 * The code is sometimes optimized not to store additional variables.
274 flags_match(ipfw_insn *cmd, u_int8_t bits)
279 if ( ((cmd->arg1 & 0xff) & bits) != 0)
280 return 0; /* some bits we want set were clear */
281 want_clear = (cmd->arg1 >> 8) & 0xff;
282 if ( (want_clear & bits) != want_clear)
283 return 0; /* some bits we want clear were set */
288 ipopts_match(struct ip *ip, ipfw_insn *cmd)
290 int optlen, bits = 0;
291 u_char *cp = (u_char *)(ip + 1);
292 int x = (ip->ip_hl << 2) - sizeof (struct ip);
294 for (; x > 0; x -= optlen, cp += optlen) {
295 int opt = cp[IPOPT_OPTVAL];
297 if (opt == IPOPT_EOL)
299 if (opt == IPOPT_NOP)
302 optlen = cp[IPOPT_OLEN];
303 if (optlen <= 0 || optlen > x)
304 return 0; /* invalid or truncated */
312 bits |= IP_FW_IPOPT_LSRR;
316 bits |= IP_FW_IPOPT_SSRR;
320 bits |= IP_FW_IPOPT_RR;
324 bits |= IP_FW_IPOPT_TS;
328 return (flags_match(cmd, bits));
332 * Parse TCP options. The logic copied from tcp_dooptions().
335 tcpopts_parse(const struct tcphdr *tcp, uint16_t *mss)
337 const u_char *cp = (const u_char *)(tcp + 1);
338 int optlen, bits = 0;
339 int cnt = (tcp->th_off << 2) - sizeof(struct tcphdr);
341 for (; cnt > 0; cnt -= optlen, cp += optlen) {
343 if (opt == TCPOPT_EOL)
345 if (opt == TCPOPT_NOP)
351 if (optlen < 2 || optlen > cnt)
360 if (optlen != TCPOLEN_MAXSEG)
362 bits |= IP_FW_TCPOPT_MSS;
364 *mss = be16dec(cp + 2);
368 if (optlen == TCPOLEN_WINDOW)
369 bits |= IP_FW_TCPOPT_WINDOW;
372 case TCPOPT_SACK_PERMITTED:
373 if (optlen == TCPOLEN_SACK_PERMITTED)
374 bits |= IP_FW_TCPOPT_SACK;
378 if (optlen > 2 && (optlen - 2) % TCPOLEN_SACK == 0)
379 bits |= IP_FW_TCPOPT_SACK;
382 case TCPOPT_TIMESTAMP:
383 if (optlen == TCPOLEN_TIMESTAMP)
384 bits |= IP_FW_TCPOPT_TS;
392 tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd)
395 return (flags_match(cmd, tcpopts_parse(tcp, NULL)));
399 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd, struct ip_fw_chain *chain,
403 if (ifp == NULL) /* no iface with this packet, match fails */
406 /* Check by name or by IP address */
407 if (cmd->name[0] != '\0') { /* match by name */
408 if (cmd->name[0] == '\1') /* use tablearg to match */
409 return ipfw_lookup_table(chain, cmd->p.kidx, 0,
410 &ifp->if_index, tablearg);
413 if (fnmatch(cmd->name, ifp->if_xname, 0) == 0)
416 if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
420 #if !defined(USERSPACE) && defined(__FreeBSD__) /* and OSX too ? */
424 TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
425 if (ia->ifa_addr->sa_family != AF_INET)
427 if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
428 (ia->ifa_addr))->sin_addr.s_addr) {
429 if_addr_runlock(ifp);
430 return(1); /* match */
433 if_addr_runlock(ifp);
434 #endif /* __FreeBSD__ */
436 return(0); /* no match, fail ... */
440 * The verify_path function checks if a route to the src exists and
441 * if it is reachable via ifp (when provided).
443 * The 'verrevpath' option checks that the interface that an IP packet
444 * arrives on is the same interface that traffic destined for the
445 * packet's source address would be routed out of.
446 * The 'versrcreach' option just checks that the source address is
447 * reachable via any route (except default) in the routing table.
448 * These two are a measure to block forged packets. This is also
449 * commonly known as "anti-spoofing" or Unicast Reverse Path
450 * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs
451 * is purposely reminiscent of the Cisco IOS command,
453 * ip verify unicast reverse-path
454 * ip verify unicast source reachable-via any
456 * which implements the same functionality. But note that the syntax
457 * is misleading, and the check may be performed on all IP packets
458 * whether unicast, multicast, or broadcast.
461 verify_path(struct in_addr src, struct ifnet *ifp, u_int fib)
463 #if defined(USERSPACE) || !defined(__FreeBSD__)
466 struct nhop4_basic nh4;
468 if (fib4_lookup_nh_basic(fib, src, NHR_IFAIF, 0, &nh4) != 0)
472 * If ifp is provided, check for equality with rtentry.
473 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
474 * in order to pass packets injected back by if_simloop():
475 * routing entry (via lo0) for our own address
476 * may exist, so we need to handle routing assymetry.
478 if (ifp != NULL && ifp != nh4.nh_ifp)
481 /* if no ifp provided, check if rtentry is not default route */
482 if (ifp == NULL && (nh4.nh_flags & NHF_DEFAULT) != 0)
485 /* or if this is a blackhole/reject route */
486 if (ifp == NULL && (nh4.nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
489 /* found valid route */
491 #endif /* __FreeBSD__ */
495 * Generate an SCTP packet containing an ABORT chunk. The verification tag
496 * is given by vtag. The T-bit is set in the ABORT chunk if and only if
497 * reflected is not 0.
501 ipfw_send_abort(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t vtag,
509 struct sctphdr *sctp;
510 struct sctp_chunkhdr *chunk;
511 u_int16_t hlen, plen, tlen;
513 MGETHDR(m, M_NOWAIT, MT_DATA);
517 M_SETFIB(m, id->fib);
520 mac_netinet_firewall_reply(replyto, m);
522 mac_netinet_firewall_send(m);
524 (void)replyto; /* don't warn about unused arg */
527 switch (id->addr_type) {
529 hlen = sizeof(struct ip);
533 hlen = sizeof(struct ip6_hdr);
541 plen = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
543 m->m_data += max_linkhdr;
544 m->m_flags |= M_SKIP_FIREWALL;
545 m->m_pkthdr.len = m->m_len = tlen;
546 m->m_pkthdr.rcvif = NULL;
547 bzero(m->m_data, tlen);
549 switch (id->addr_type) {
551 ip = mtod(m, struct ip *);
554 ip->ip_hl = sizeof(struct ip) >> 2;
555 ip->ip_tos = IPTOS_LOWDELAY;
556 ip->ip_len = htons(tlen);
557 ip->ip_id = htons(0);
558 ip->ip_off = htons(0);
559 ip->ip_ttl = V_ip_defttl;
560 ip->ip_p = IPPROTO_SCTP;
562 ip->ip_src.s_addr = htonl(id->dst_ip);
563 ip->ip_dst.s_addr = htonl(id->src_ip);
565 sctp = (struct sctphdr *)(ip + 1);
569 ip6 = mtod(m, struct ip6_hdr *);
571 ip6->ip6_vfc = IPV6_VERSION;
572 ip6->ip6_plen = htons(plen);
573 ip6->ip6_nxt = IPPROTO_SCTP;
574 ip6->ip6_hlim = IPV6_DEFHLIM;
575 ip6->ip6_src = id->dst_ip6;
576 ip6->ip6_dst = id->src_ip6;
578 sctp = (struct sctphdr *)(ip6 + 1);
583 sctp->src_port = htons(id->dst_port);
584 sctp->dest_port = htons(id->src_port);
585 sctp->v_tag = htonl(vtag);
586 sctp->checksum = htonl(0);
588 chunk = (struct sctp_chunkhdr *)(sctp + 1);
589 chunk->chunk_type = SCTP_ABORT_ASSOCIATION;
590 chunk->chunk_flags = 0;
591 if (reflected != 0) {
592 chunk->chunk_flags |= SCTP_HAD_NO_TCB;
594 chunk->chunk_length = htons(sizeof(struct sctp_chunkhdr));
596 sctp->checksum = sctp_calculate_cksum(m, hlen);
602 * Generate a TCP packet, containing either a RST or a keepalive.
603 * When flags & TH_RST, we are sending a RST packet, because of a
604 * "reset" action matched the packet.
605 * Otherwise we are sending a keepalive, and flags & TH_
606 * The 'replyto' mbuf is the mbuf being replied to, if any, and is required
607 * so that MAC can label the reply appropriately.
610 ipfw_send_pkt(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t seq,
611 u_int32_t ack, int flags)
613 struct mbuf *m = NULL; /* stupid compiler */
614 struct ip *h = NULL; /* stupid compiler */
616 struct ip6_hdr *h6 = NULL;
618 struct tcphdr *th = NULL;
621 MGETHDR(m, M_NOWAIT, MT_DATA);
625 M_SETFIB(m, id->fib);
628 mac_netinet_firewall_reply(replyto, m);
630 mac_netinet_firewall_send(m);
632 (void)replyto; /* don't warn about unused arg */
635 switch (id->addr_type) {
637 len = sizeof(struct ip) + sizeof(struct tcphdr);
641 len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
649 dir = ((flags & (TH_SYN | TH_RST)) == TH_SYN);
651 m->m_data += max_linkhdr;
652 m->m_flags |= M_SKIP_FIREWALL;
653 m->m_pkthdr.len = m->m_len = len;
654 m->m_pkthdr.rcvif = NULL;
655 bzero(m->m_data, len);
657 switch (id->addr_type) {
659 h = mtod(m, struct ip *);
661 /* prepare for checksum */
662 h->ip_p = IPPROTO_TCP;
663 h->ip_len = htons(sizeof(struct tcphdr));
665 h->ip_src.s_addr = htonl(id->src_ip);
666 h->ip_dst.s_addr = htonl(id->dst_ip);
668 h->ip_src.s_addr = htonl(id->dst_ip);
669 h->ip_dst.s_addr = htonl(id->src_ip);
672 th = (struct tcphdr *)(h + 1);
676 h6 = mtod(m, struct ip6_hdr *);
678 /* prepare for checksum */
679 h6->ip6_nxt = IPPROTO_TCP;
680 h6->ip6_plen = htons(sizeof(struct tcphdr));
682 h6->ip6_src = id->src_ip6;
683 h6->ip6_dst = id->dst_ip6;
685 h6->ip6_src = id->dst_ip6;
686 h6->ip6_dst = id->src_ip6;
689 th = (struct tcphdr *)(h6 + 1);
695 th->th_sport = htons(id->src_port);
696 th->th_dport = htons(id->dst_port);
698 th->th_sport = htons(id->dst_port);
699 th->th_dport = htons(id->src_port);
701 th->th_off = sizeof(struct tcphdr) >> 2;
703 if (flags & TH_RST) {
704 if (flags & TH_ACK) {
705 th->th_seq = htonl(ack);
706 th->th_flags = TH_RST;
710 th->th_ack = htonl(seq);
711 th->th_flags = TH_RST | TH_ACK;
715 * Keepalive - use caller provided sequence numbers
717 th->th_seq = htonl(seq);
718 th->th_ack = htonl(ack);
719 th->th_flags = TH_ACK;
722 switch (id->addr_type) {
724 th->th_sum = in_cksum(m, len);
726 /* finish the ip header */
728 h->ip_hl = sizeof(*h) >> 2;
729 h->ip_tos = IPTOS_LOWDELAY;
730 h->ip_off = htons(0);
731 h->ip_len = htons(len);
732 h->ip_ttl = V_ip_defttl;
737 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(*h6),
738 sizeof(struct tcphdr));
740 /* finish the ip6 header */
741 h6->ip6_vfc |= IPV6_VERSION;
742 h6->ip6_hlim = IPV6_DEFHLIM;
752 * ipv6 specific rules here...
755 icmp6type_match (int type, ipfw_insn_u32 *cmd)
757 return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) );
761 flow6id_match( int curr_flow, ipfw_insn_u32 *cmd )
764 for (i=0; i <= cmd->o.arg1; ++i )
765 if (curr_flow == cmd->d[i] )
770 /* support for IP6_*_ME opcodes */
771 static const struct in6_addr lla_mask = {{{
772 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
773 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
777 ipfw_localip6(struct in6_addr *in6)
779 struct rm_priotracker in6_ifa_tracker;
780 struct in6_ifaddr *ia;
782 if (IN6_IS_ADDR_MULTICAST(in6))
785 if (!IN6_IS_ADDR_LINKLOCAL(in6))
786 return (in6_localip(in6));
788 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
789 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
790 if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr))
792 if (IN6_ARE_MASKED_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
794 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
798 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
803 verify_path6(struct in6_addr *src, struct ifnet *ifp, u_int fib)
805 struct nhop6_basic nh6;
807 if (IN6_IS_SCOPE_LINKLOCAL(src))
810 if (fib6_lookup_nh_basic(fib, src, 0, NHR_IFAIF, 0, &nh6) != 0)
813 /* If ifp is provided, check for equality with route table. */
814 if (ifp != NULL && ifp != nh6.nh_ifp)
817 /* if no ifp provided, check if rtentry is not default route */
818 if (ifp == NULL && (nh6.nh_flags & NHF_DEFAULT) != 0)
821 /* or if this is a blackhole/reject route */
822 if (ifp == NULL && (nh6.nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
825 /* found valid route */
830 is_icmp6_query(int icmp6_type)
832 if ((icmp6_type <= ICMP6_MAXTYPE) &&
833 (icmp6_type == ICMP6_ECHO_REQUEST ||
834 icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
835 icmp6_type == ICMP6_WRUREQUEST ||
836 icmp6_type == ICMP6_FQDN_QUERY ||
837 icmp6_type == ICMP6_NI_QUERY))
844 map_icmp_unreach(int code)
849 case ICMP_UNREACH_NET:
850 case ICMP_UNREACH_HOST:
851 case ICMP_UNREACH_SRCFAIL:
852 case ICMP_UNREACH_NET_UNKNOWN:
853 case ICMP_UNREACH_HOST_UNKNOWN:
854 case ICMP_UNREACH_TOSNET:
855 case ICMP_UNREACH_TOSHOST:
856 return (ICMP6_DST_UNREACH_NOROUTE);
857 case ICMP_UNREACH_PORT:
858 return (ICMP6_DST_UNREACH_NOPORT);
861 * Map the rest of codes into admit prohibited.
862 * XXX: unreach proto should be mapped into ICMPv6
863 * parameter problem, but we use only unreach type.
865 return (ICMP6_DST_UNREACH_ADMIN);
870 send_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6)
875 if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) {
877 tcp = (struct tcphdr *)((char *)ip6 + hlen);
879 if ((tcp->th_flags & TH_RST) == 0) {
881 m0 = ipfw_send_pkt(args->m, &(args->f_id),
882 ntohl(tcp->th_seq), ntohl(tcp->th_ack),
883 tcp->th_flags | TH_RST);
885 ip6_output(m0, NULL, NULL, 0, NULL, NULL,
889 } else if (code == ICMP6_UNREACH_ABORT &&
890 args->f_id.proto == IPPROTO_SCTP) {
892 struct sctphdr *sctp;
896 sctp = (struct sctphdr *)((char *)ip6 + hlen);
898 v_tag = ntohl(sctp->v_tag);
899 /* Investigate the first chunk header if available */
900 if (m->m_len >= hlen + sizeof(struct sctphdr) +
901 sizeof(struct sctp_chunkhdr)) {
902 struct sctp_chunkhdr *chunk;
904 chunk = (struct sctp_chunkhdr *)(sctp + 1);
905 switch (chunk->chunk_type) {
906 case SCTP_INITIATION:
908 * Packets containing an INIT chunk MUST have
915 /* INIT chunk MUST NOT be bundled */
916 if (m->m_pkthdr.len >
917 hlen + sizeof(struct sctphdr) +
918 ntohs(chunk->chunk_length) + 3) {
921 /* Use the initiate tag if available */
922 if ((m->m_len >= hlen + sizeof(struct sctphdr) +
923 sizeof(struct sctp_chunkhdr) +
924 offsetof(struct sctp_init, a_rwnd))) {
925 struct sctp_init *init;
927 init = (struct sctp_init *)(chunk + 1);
928 v_tag = ntohl(init->initiate_tag);
932 case SCTP_ABORT_ASSOCIATION:
934 * If the packet contains an ABORT chunk, don't
936 * XXX: We should search through all chunks,
937 * but don't do to avoid attacks.
946 m0 = ipfw_send_abort(args->m, &(args->f_id), v_tag,
950 ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
952 } else if (code != ICMP6_UNREACH_RST && code != ICMP6_UNREACH_ABORT) {
953 /* Send an ICMPv6 unreach. */
956 * Unlike above, the mbufs need to line up with the ip6 hdr,
957 * as the contents are read. We need to m_adj() the
959 * The mbuf will however be thrown away so we can adjust it.
960 * Remember we did an m_pullup on it already so we
961 * can make some assumptions about contiguousness.
964 m_adj(m, args->L3offset);
966 icmp6_error(m, ICMP6_DST_UNREACH, code, 0);
977 * sends a reject message, consuming the mbuf passed as an argument.
980 send_reject(struct ip_fw_args *args, int code, int iplen, struct ip *ip)
984 /* XXX When ip is not guaranteed to be at mtod() we will
985 * need to account for this */
986 * The mbuf will however be thrown away so we can adjust it.
987 * Remember we did an m_pullup on it already so we
988 * can make some assumptions about contiguousness.
991 m_adj(m, args->L3offset);
993 if (code != ICMP_REJECT_RST && code != ICMP_REJECT_ABORT) {
994 /* Send an ICMP unreach */
995 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
996 } else if (code == ICMP_REJECT_RST && args->f_id.proto == IPPROTO_TCP) {
997 struct tcphdr *const tcp =
998 L3HDR(struct tcphdr, mtod(args->m, struct ip *));
999 if ( (tcp->th_flags & TH_RST) == 0) {
1001 m = ipfw_send_pkt(args->m, &(args->f_id),
1002 ntohl(tcp->th_seq), ntohl(tcp->th_ack),
1003 tcp->th_flags | TH_RST);
1005 ip_output(m, NULL, NULL, 0, NULL, NULL);
1008 } else if (code == ICMP_REJECT_ABORT &&
1009 args->f_id.proto == IPPROTO_SCTP) {
1011 struct sctphdr *sctp;
1012 struct sctp_chunkhdr *chunk;
1013 struct sctp_init *init;
1017 sctp = L3HDR(struct sctphdr, mtod(args->m, struct ip *));
1019 v_tag = ntohl(sctp->v_tag);
1020 if (iplen >= (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1021 sizeof(struct sctp_chunkhdr)) {
1022 /* Look at the first chunk header if available */
1023 chunk = (struct sctp_chunkhdr *)(sctp + 1);
1024 switch (chunk->chunk_type) {
1025 case SCTP_INITIATION:
1027 * Packets containing an INIT chunk MUST have
1034 /* INIT chunk MUST NOT be bundled */
1036 (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1037 ntohs(chunk->chunk_length) + 3) {
1040 /* Use the initiate tag if available */
1041 if ((iplen >= (ip->ip_hl << 2) +
1042 sizeof(struct sctphdr) +
1043 sizeof(struct sctp_chunkhdr) +
1044 offsetof(struct sctp_init, a_rwnd))) {
1045 init = (struct sctp_init *)(chunk + 1);
1046 v_tag = ntohl(init->initiate_tag);
1050 case SCTP_ABORT_ASSOCIATION:
1052 * If the packet contains an ABORT chunk, don't
1054 * XXX: We should search through all chunks,
1055 * but don't do to avoid attacks.
1064 m = ipfw_send_abort(args->m, &(args->f_id), v_tag,
1068 ip_output(m, NULL, NULL, 0, NULL, NULL);
1076 * Support for uid/gid/jail lookup. These tests are expensive
1077 * (because we may need to look into the list of active sockets)
1078 * so we cache the results. ugid_lookupp is 0 if we have not
1079 * yet done a lookup, 1 if we succeeded, and -1 if we tried
1080 * and failed. The function always returns the match value.
1081 * We could actually spare the variable and use *uc, setting
1082 * it to '(void *)check_uidgid if we have no info, NULL if
1083 * we tried and failed, or any other value if successful.
1086 check_uidgid(ipfw_insn_u32 *insn, struct ip_fw_args *args, int *ugid_lookupp,
1089 #if defined(USERSPACE)
1090 return 0; // not supported in userspace
1094 return cred_check(insn, proto, oif,
1095 dst_ip, dst_port, src_ip, src_port,
1096 (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb);
1098 struct in_addr src_ip, dst_ip;
1099 struct inpcbinfo *pi;
1100 struct ipfw_flow_id *id;
1101 struct inpcb *pcb, *inp;
1111 * Check to see if the UDP or TCP stack supplied us with
1112 * the PCB. If so, rather then holding a lock and looking
1113 * up the PCB, we can use the one that was supplied.
1115 if (inp && *ugid_lookupp == 0) {
1116 INP_LOCK_ASSERT(inp);
1117 if (inp->inp_socket != NULL) {
1118 *uc = crhold(inp->inp_cred);
1124 * If we have already been here and the packet has no
1125 * PCB entry associated with it, then we can safely
1126 * assume that this is a no match.
1128 if (*ugid_lookupp == -1)
1130 if (id->proto == IPPROTO_TCP) {
1133 } else if (id->proto == IPPROTO_UDP) {
1134 lookupflags = INPLOOKUP_WILDCARD;
1136 } else if (id->proto == IPPROTO_UDPLITE) {
1137 lookupflags = INPLOOKUP_WILDCARD;
1138 pi = &V_ulitecbinfo;
1141 lookupflags |= INPLOOKUP_RLOCKPCB;
1143 if (*ugid_lookupp == 0) {
1144 if (id->addr_type == 6) {
1147 pcb = in6_pcblookup_mbuf(pi,
1148 &id->src_ip6, htons(id->src_port),
1149 &id->dst_ip6, htons(id->dst_port),
1150 lookupflags, oif, args->m);
1152 pcb = in6_pcblookup_mbuf(pi,
1153 &id->dst_ip6, htons(id->dst_port),
1154 &id->src_ip6, htons(id->src_port),
1155 lookupflags, oif, args->m);
1161 src_ip.s_addr = htonl(id->src_ip);
1162 dst_ip.s_addr = htonl(id->dst_ip);
1164 pcb = in_pcblookup_mbuf(pi,
1165 src_ip, htons(id->src_port),
1166 dst_ip, htons(id->dst_port),
1167 lookupflags, oif, args->m);
1169 pcb = in_pcblookup_mbuf(pi,
1170 dst_ip, htons(id->dst_port),
1171 src_ip, htons(id->src_port),
1172 lookupflags, oif, args->m);
1175 INP_RLOCK_ASSERT(pcb);
1176 *uc = crhold(pcb->inp_cred);
1180 if (*ugid_lookupp == 0) {
1182 * We tried and failed, set the variable to -1
1183 * so we will not try again on this packet.
1189 if (insn->o.opcode == O_UID)
1190 match = ((*uc)->cr_uid == (uid_t)insn->d[0]);
1191 else if (insn->o.opcode == O_GID)
1192 match = groupmember((gid_t)insn->d[0], *uc);
1193 else if (insn->o.opcode == O_JAIL)
1194 match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]);
1196 #endif /* __FreeBSD__ */
1197 #endif /* not supported in userspace */
1201 * Helper function to set args with info on the rule after the matching
1202 * one. slot is precise, whereas we guess rule_id as they are
1203 * assigned sequentially.
1206 set_match(struct ip_fw_args *args, int slot,
1207 struct ip_fw_chain *chain)
1209 args->rule.chain_id = chain->id;
1210 args->rule.slot = slot + 1; /* we use 0 as a marker */
1211 args->rule.rule_id = 1 + chain->map[slot]->id;
1212 args->rule.rulenum = chain->map[slot]->rulenum;
1213 args->flags |= IPFW_ARGS_REF;
1216 #ifndef LINEAR_SKIPTO
1218 * Helper function to enable cached rule lookups using
1219 * cached_id and cached_pos fields in ipfw rule.
1222 jump_fast(struct ip_fw_chain *chain, struct ip_fw *f, int num,
1223 int tablearg, int jump_backwards)
1227 /* If possible use cached f_pos (in f->cached_pos),
1228 * whose version is written in f->cached_id
1229 * (horrible hacks to avoid changing the ABI).
1231 if (num != IP_FW_TARG && f->cached_id == chain->id)
1232 f_pos = f->cached_pos;
1234 int i = IP_FW_ARG_TABLEARG(chain, num, skipto);
1235 /* make sure we do not jump backward */
1236 if (jump_backwards == 0 && i <= f->rulenum)
1238 if (chain->idxmap != NULL)
1239 f_pos = chain->idxmap[i];
1241 f_pos = ipfw_find_rule(chain, i, 0);
1242 /* update the cache */
1243 if (num != IP_FW_TARG) {
1244 f->cached_id = chain->id;
1245 f->cached_pos = f_pos;
1253 * Helper function to enable real fast rule lookups.
1256 jump_linear(struct ip_fw_chain *chain, struct ip_fw *f, int num,
1257 int tablearg, int jump_backwards)
1261 num = IP_FW_ARG_TABLEARG(chain, num, skipto);
1262 /* make sure we do not jump backward */
1263 if (jump_backwards == 0 && num <= f->rulenum)
1264 num = f->rulenum + 1;
1265 f_pos = chain->idxmap[num];
1271 #define TARG(k, f) IP_FW_ARG_TABLEARG(chain, k, f)
1273 * The main check routine for the firewall.
1275 * All arguments are in args so we can modify them and return them
1276 * back to the caller.
1280 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1281 * Starts with the IP header.
1282 * args->eh (in) Mac header if present, NULL for layer3 packet.
1283 * args->L3offset Number of bytes bypassed if we came from L2.
1284 * e.g. often sizeof(eh) ** NOTYET **
1285 * args->oif Outgoing interface, NULL if packet is incoming.
1286 * The incoming interface is in the mbuf. (in)
1287 * args->divert_rule (in/out)
1288 * Skip up to the first rule past this rule number;
1289 * upon return, non-zero port number for divert or tee.
1291 * args->rule Pointer to the last matching rule (in/out)
1292 * args->next_hop Socket we are forwarding to (out).
1293 * args->next_hop6 IPv6 next hop we are forwarding to (out).
1294 * args->f_id Addresses grabbed from the packet (out)
1295 * args->rule.info a cookie depending on rule action
1299 * IP_FW_PASS the packet must be accepted
1300 * IP_FW_DENY the packet must be dropped
1301 * IP_FW_DIVERT divert packet, port in m_tag
1302 * IP_FW_TEE tee packet, port in m_tag
1303 * IP_FW_DUMMYNET to dummynet, pipe in args->cookie
1304 * IP_FW_NETGRAPH into netgraph, cookie args->cookie
1305 * args->rule contains the matching rule,
1306 * args->rule.info has additional information.
1310 ipfw_chk(struct ip_fw_args *args)
1314 * Local variables holding state while processing a packet:
1316 * IMPORTANT NOTE: to speed up the processing of rules, there
1317 * are some assumption on the values of the variables, which
1318 * are documented here. Should you change them, please check
1319 * the implementation of the various instructions to make sure
1320 * that they still work.
1322 * args->eh The MAC header. It is non-null for a layer2
1323 * packet, it is NULL for a layer-3 packet.
1325 * args->L3offset Offset in the packet to the L3 (IP or equiv.) header.
1327 * m | args->m Pointer to the mbuf, as received from the caller.
1328 * It may change if ipfw_chk() does an m_pullup, or if it
1329 * consumes the packet because it calls send_reject().
1330 * XXX This has to change, so that ipfw_chk() never modifies
1331 * or consumes the buffer.
1332 * ip is the beginning of the ip(4 or 6) header.
1333 * Calculated by adding the L3offset to the start of data.
1334 * (Until we start using L3offset, the packet is
1335 * supposed to start with the ip header).
1337 struct mbuf *m = args->m;
1338 struct ip *ip = mtod(m, struct ip *);
1341 * For rules which contain uid/gid or jail constraints, cache
1342 * a copy of the users credentials after the pcb lookup has been
1343 * executed. This will speed up the processing of rules with
1344 * these types of constraints, as well as decrease contention
1345 * on pcb related locks.
1348 struct bsd_ucred ucred_cache;
1350 struct ucred *ucred_cache = NULL;
1352 int ucred_lookup = 0;
1355 * oif | args->oif If NULL, ipfw_chk has been called on the
1356 * inbound path (ether_input, ip_input).
1357 * If non-NULL, ipfw_chk has been called on the outbound path
1358 * (ether_output, ip_output).
1360 struct ifnet *oif = args->oif;
1362 int f_pos = 0; /* index of current rule in the array */
1366 * hlen The length of the IP header.
1368 u_int hlen = 0; /* hlen >0 means we have an IP pkt */
1371 * offset The offset of a fragment. offset != 0 means that
1372 * we have a fragment at this offset of an IPv4 packet.
1373 * offset == 0 means that (if this is an IPv4 packet)
1374 * this is the first or only fragment.
1375 * For IPv6 offset|ip6f_mf == 0 means there is no Fragment Header
1376 * or there is a single packet fragment (fragment header added
1377 * without needed). We will treat a single packet fragment as if
1378 * there was no fragment header (or log/block depending on the
1379 * V_fw_permit_single_frag6 sysctl setting).
1382 u_short ip6f_mf = 0;
1385 * Local copies of addresses. They are only valid if we have
1388 * proto The protocol. Set to 0 for non-ip packets,
1389 * or to the protocol read from the packet otherwise.
1390 * proto != 0 means that we have an IPv4 packet.
1392 * src_port, dst_port port numbers, in HOST format. Only
1393 * valid for TCP and UDP packets.
1395 * src_ip, dst_ip ip addresses, in NETWORK format.
1396 * Only valid for IPv4 packets.
1399 uint16_t src_port, dst_port; /* NOTE: host format */
1400 struct in_addr src_ip, dst_ip; /* NOTE: network format */
1403 uint16_t etype; /* Host order stored ether type */
1405 struct ipfw_dyn_info dyn_info;
1406 struct ip_fw *q = NULL;
1407 struct ip_fw_chain *chain = &V_layer3_chain;
1410 * We store in ulp a pointer to the upper layer protocol header.
1411 * In the ipv4 case this is easy to determine from the header,
1412 * but for ipv6 we might have some additional headers in the middle.
1413 * ulp is NULL if not found.
1415 void *ulp = NULL; /* upper layer protocol pointer. */
1417 /* XXX ipv6 variables */
1419 uint8_t icmp6_type = 0;
1420 uint16_t ext_hd = 0; /* bits vector for extension header filtering */
1421 /* end of ipv6 variables */
1425 int done = 0; /* flag to exit the outer loop */
1427 if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready))
1428 return (IP_FW_PASS); /* accept */
1430 dst_ip.s_addr = 0; /* make sure it is initialized */
1431 src_ip.s_addr = 0; /* make sure it is initialized */
1432 src_port = dst_port = 0;
1433 pktlen = m->m_pkthdr.len;
1435 DYN_INFO_INIT(&dyn_info);
1437 * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous,
1438 * then it sets p to point at the offset "len" in the mbuf. WARNING: the
1439 * pointer might become stale after other pullups (but we never use it
1442 #define PULLUP_TO(_len, p, T) PULLUP_LEN(_len, p, sizeof(T))
1443 #define _PULLUP_LOCKED(_len, p, T, unlock) \
1445 int x = (_len) + T; \
1446 if ((m)->m_len < x) { \
1447 args->m = m = m_pullup(m, x); \
1450 goto pullup_failed; \
1453 p = (mtod(m, char *) + (_len)); \
1456 #define PULLUP_LEN(_len, p, T) _PULLUP_LOCKED(_len, p, T, )
1457 #define PULLUP_LEN_LOCKED(_len, p, T) \
1458 _PULLUP_LOCKED(_len, p, T, IPFW_PF_RUNLOCK(chain)); \
1461 * In case pointers got stale after pullups, update them.
1463 #define UPDATE_POINTERS() \
1465 ip = mtod(m, struct ip *); \
1469 * if we have an ether header,
1471 if (args->flags & IPFW_ARGS_ETHER)
1472 etype = ntohs(args->eh->ether_type);
1476 /* Identify IP packets and fill up variables. */
1477 if (pktlen >= sizeof(struct ip6_hdr) &&
1478 (etype == 0 || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) {
1479 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
1482 hlen = sizeof(struct ip6_hdr);
1483 proto = ip6->ip6_nxt;
1484 /* Search extension headers to find upper layer protocols */
1485 while (ulp == NULL && offset == 0) {
1487 case IPPROTO_ICMPV6:
1488 PULLUP_TO(hlen, ulp, struct icmp6_hdr);
1489 icmp6_type = ICMP6(ulp)->icmp6_type;
1493 PULLUP_TO(hlen, ulp, struct tcphdr);
1494 dst_port = TCP(ulp)->th_dport;
1495 src_port = TCP(ulp)->th_sport;
1496 /* save flags for dynamic rules */
1497 args->f_id._flags = TCP(ulp)->th_flags;
1501 if (pktlen >= hlen + sizeof(struct sctphdr) +
1502 sizeof(struct sctp_chunkhdr) +
1503 offsetof(struct sctp_init, a_rwnd))
1504 PULLUP_LEN(hlen, ulp,
1505 sizeof(struct sctphdr) +
1506 sizeof(struct sctp_chunkhdr) +
1507 offsetof(struct sctp_init, a_rwnd));
1508 else if (pktlen >= hlen + sizeof(struct sctphdr))
1509 PULLUP_LEN(hlen, ulp, pktlen - hlen);
1511 PULLUP_LEN(hlen, ulp,
1512 sizeof(struct sctphdr));
1513 src_port = SCTP(ulp)->src_port;
1514 dst_port = SCTP(ulp)->dest_port;
1518 case IPPROTO_UDPLITE:
1519 PULLUP_TO(hlen, ulp, struct udphdr);
1520 dst_port = UDP(ulp)->uh_dport;
1521 src_port = UDP(ulp)->uh_sport;
1524 case IPPROTO_HOPOPTS: /* RFC 2460 */
1525 PULLUP_TO(hlen, ulp, struct ip6_hbh);
1526 ext_hd |= EXT_HOPOPTS;
1527 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1528 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1532 case IPPROTO_ROUTING: /* RFC 2460 */
1533 PULLUP_TO(hlen, ulp, struct ip6_rthdr);
1534 switch (((struct ip6_rthdr *)ulp)->ip6r_type) {
1536 ext_hd |= EXT_RTHDR0;
1539 ext_hd |= EXT_RTHDR2;
1543 printf("IPFW2: IPV6 - Unknown "
1544 "Routing Header type(%d)\n",
1545 ((struct ip6_rthdr *)
1547 if (V_fw_deny_unknown_exthdrs)
1548 return (IP_FW_DENY);
1551 ext_hd |= EXT_ROUTING;
1552 hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3;
1553 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt;
1557 case IPPROTO_FRAGMENT: /* RFC 2460 */
1558 PULLUP_TO(hlen, ulp, struct ip6_frag);
1559 ext_hd |= EXT_FRAGMENT;
1560 hlen += sizeof (struct ip6_frag);
1561 proto = ((struct ip6_frag *)ulp)->ip6f_nxt;
1562 offset = ((struct ip6_frag *)ulp)->ip6f_offlg &
1564 ip6f_mf = ((struct ip6_frag *)ulp)->ip6f_offlg &
1566 if (V_fw_permit_single_frag6 == 0 &&
1567 offset == 0 && ip6f_mf == 0) {
1569 printf("IPFW2: IPV6 - Invalid "
1570 "Fragment Header\n");
1571 if (V_fw_deny_unknown_exthdrs)
1572 return (IP_FW_DENY);
1576 ntohl(((struct ip6_frag *)ulp)->ip6f_ident);
1580 case IPPROTO_DSTOPTS: /* RFC 2460 */
1581 PULLUP_TO(hlen, ulp, struct ip6_hbh);
1582 ext_hd |= EXT_DSTOPTS;
1583 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1584 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1588 case IPPROTO_AH: /* RFC 2402 */
1589 PULLUP_TO(hlen, ulp, struct ip6_ext);
1591 hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2;
1592 proto = ((struct ip6_ext *)ulp)->ip6e_nxt;
1596 case IPPROTO_ESP: /* RFC 2406 */
1597 PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */
1598 /* Anything past Seq# is variable length and
1599 * data past this ext. header is encrypted. */
1603 case IPPROTO_NONE: /* RFC 2460 */
1605 * Packet ends here, and IPv6 header has
1606 * already been pulled up. If ip6e_len!=0
1607 * then octets must be ignored.
1609 ulp = ip; /* non-NULL to get out of loop. */
1612 case IPPROTO_OSPFIGP:
1613 /* XXX OSPF header check? */
1614 PULLUP_TO(hlen, ulp, struct ip6_ext);
1618 /* XXX PIM header check? */
1619 PULLUP_TO(hlen, ulp, struct pim);
1622 case IPPROTO_GRE: /* RFC 1701 */
1623 /* XXX GRE header check? */
1624 PULLUP_TO(hlen, ulp, struct grehdr);
1628 PULLUP_TO(hlen, ulp, offsetof(
1629 struct carp_header, carp_counter));
1630 if (CARP_ADVERTISEMENT !=
1631 ((struct carp_header *)ulp)->carp_type)
1632 return (IP_FW_DENY);
1635 case IPPROTO_IPV6: /* RFC 2893 */
1636 PULLUP_TO(hlen, ulp, struct ip6_hdr);
1639 case IPPROTO_IPV4: /* RFC 2893 */
1640 PULLUP_TO(hlen, ulp, struct ip);
1645 printf("IPFW2: IPV6 - Unknown "
1646 "Extension Header(%d), ext_hd=%x\n",
1648 if (V_fw_deny_unknown_exthdrs)
1649 return (IP_FW_DENY);
1650 PULLUP_TO(hlen, ulp, struct ip6_ext);
1654 ip = mtod(m, struct ip *);
1655 ip6 = (struct ip6_hdr *)ip;
1656 args->f_id.addr_type = 6;
1657 args->f_id.src_ip6 = ip6->ip6_src;
1658 args->f_id.dst_ip6 = ip6->ip6_dst;
1659 args->f_id.flow_id6 = ntohl(ip6->ip6_flow);
1660 iplen = ntohs(ip6->ip6_plen) + sizeof(*ip6);
1661 } else if (pktlen >= sizeof(struct ip) &&
1662 (etype == 0 || etype == ETHERTYPE_IP) && ip->ip_v == 4) {
1664 hlen = ip->ip_hl << 2;
1666 * Collect parameters into local variables for faster
1670 src_ip = ip->ip_src;
1671 dst_ip = ip->ip_dst;
1672 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1673 iplen = ntohs(ip->ip_len);
1678 PULLUP_TO(hlen, ulp, struct tcphdr);
1679 dst_port = TCP(ulp)->th_dport;
1680 src_port = TCP(ulp)->th_sport;
1681 /* save flags for dynamic rules */
1682 args->f_id._flags = TCP(ulp)->th_flags;
1686 if (pktlen >= hlen + sizeof(struct sctphdr) +
1687 sizeof(struct sctp_chunkhdr) +
1688 offsetof(struct sctp_init, a_rwnd))
1689 PULLUP_LEN(hlen, ulp,
1690 sizeof(struct sctphdr) +
1691 sizeof(struct sctp_chunkhdr) +
1692 offsetof(struct sctp_init, a_rwnd));
1693 else if (pktlen >= hlen + sizeof(struct sctphdr))
1694 PULLUP_LEN(hlen, ulp, pktlen - hlen);
1696 PULLUP_LEN(hlen, ulp,
1697 sizeof(struct sctphdr));
1698 src_port = SCTP(ulp)->src_port;
1699 dst_port = SCTP(ulp)->dest_port;
1703 case IPPROTO_UDPLITE:
1704 PULLUP_TO(hlen, ulp, struct udphdr);
1705 dst_port = UDP(ulp)->uh_dport;
1706 src_port = UDP(ulp)->uh_sport;
1710 PULLUP_TO(hlen, ulp, struct icmphdr);
1711 //args->f_id.flags = ICMP(ulp)->icmp_type;
1719 ip = mtod(m, struct ip *);
1720 args->f_id.addr_type = 4;
1721 args->f_id.src_ip = ntohl(src_ip.s_addr);
1722 args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1725 dst_ip.s_addr = src_ip.s_addr = 0;
1727 args->f_id.addr_type = 1; /* XXX */
1730 pktlen = iplen < pktlen ? iplen: pktlen;
1732 /* Properly initialize the rest of f_id */
1733 args->f_id.proto = proto;
1734 args->f_id.src_port = src_port = ntohs(src_port);
1735 args->f_id.dst_port = dst_port = ntohs(dst_port);
1736 args->f_id.fib = M_GETFIB(m);
1738 IPFW_PF_RLOCK(chain);
1739 if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */
1740 IPFW_PF_RUNLOCK(chain);
1741 return (IP_FW_PASS); /* accept */
1743 if (args->flags & IPFW_ARGS_REF) {
1745 * Packet has already been tagged as a result of a previous
1746 * match on rule args->rule aka args->rule_id (PIPE, QUEUE,
1747 * REASS, NETGRAPH, DIVERT/TEE...)
1748 * Validate the slot and continue from the next one
1749 * if still present, otherwise do a lookup.
1751 f_pos = (args->rule.chain_id == chain->id) ?
1753 ipfw_find_rule(chain, args->rule.rulenum,
1754 args->rule.rule_id);
1760 * Now scan the rules, and parse microinstructions for each rule.
1761 * We have two nested loops and an inner switch. Sometimes we
1762 * need to break out of one or both loops, or re-enter one of
1763 * the loops with updated variables. Loop variables are:
1765 * f_pos (outer loop) points to the current rule.
1766 * On output it points to the matching rule.
1767 * done (outer loop) is used as a flag to break the loop.
1768 * l (inner loop) residual length of current rule.
1769 * cmd points to the current microinstruction.
1771 * We break the inner loop by setting l=0 and possibly
1772 * cmdlen=0 if we don't want to advance cmd.
1773 * We break the outer loop by setting done=1
1774 * We can restart the inner loop by setting l>0 and f_pos, f, cmd
1777 for (; f_pos < chain->n_rules; f_pos++) {
1779 uint32_t tablearg = 0;
1780 int l, cmdlen, skip_or; /* skip rest of OR block */
1783 f = chain->map[f_pos];
1784 if (V_set_disable & (1 << f->set) )
1788 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
1789 l -= cmdlen, cmd += cmdlen) {
1793 * check_body is a jump target used when we find a
1794 * CHECK_STATE, and need to jump to the body of
1799 cmdlen = F_LEN(cmd);
1801 * An OR block (insn_1 || .. || insn_n) has the
1802 * F_OR bit set in all but the last instruction.
1803 * The first match will set "skip_or", and cause
1804 * the following instructions to be skipped until
1805 * past the one with the F_OR bit clear.
1807 if (skip_or) { /* skip this instruction */
1808 if ((cmd->len & F_OR) == 0)
1809 skip_or = 0; /* next one is good */
1812 match = 0; /* set to 1 if we succeed */
1814 switch (cmd->opcode) {
1816 * The first set of opcodes compares the packet's
1817 * fields with some pattern, setting 'match' if a
1818 * match is found. At the end of the loop there is
1819 * logic to deal with F_NOT and F_OR flags associated
1827 printf("ipfw: opcode %d unimplemented\n",
1835 * We only check offset == 0 && proto != 0,
1836 * as this ensures that we have a
1837 * packet with the ports info.
1841 if (proto == IPPROTO_TCP ||
1842 proto == IPPROTO_UDP ||
1843 proto == IPPROTO_UDPLITE)
1844 match = check_uidgid(
1845 (ipfw_insn_u32 *)cmd,
1846 args, &ucred_lookup,
1850 (void *)&ucred_cache);
1855 match = iface_match(m->m_pkthdr.rcvif,
1856 (ipfw_insn_if *)cmd, chain, &tablearg);
1860 match = iface_match(oif, (ipfw_insn_if *)cmd,
1865 match = iface_match(oif ? oif :
1866 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd,
1871 if (args->flags & IPFW_ARGS_ETHER) {
1872 u_int32_t *want = (u_int32_t *)
1873 ((ipfw_insn_mac *)cmd)->addr;
1874 u_int32_t *mask = (u_int32_t *)
1875 ((ipfw_insn_mac *)cmd)->mask;
1876 u_int32_t *hdr = (u_int32_t *)args->eh;
1879 ( want[0] == (hdr[0] & mask[0]) &&
1880 want[1] == (hdr[1] & mask[1]) &&
1881 want[2] == (hdr[2] & mask[2]) );
1886 if (args->flags & IPFW_ARGS_ETHER) {
1888 ((ipfw_insn_u16 *)cmd)->ports;
1891 for (i = cmdlen - 1; !match && i>0;
1893 match = (etype >= p[0] &&
1899 match = (offset != 0);
1902 case O_IN: /* "out" is "not in" */
1903 match = (oif == NULL);
1907 match = (args->flags & IPFW_ARGS_ETHER);
1911 if ((args->flags & IPFW_ARGS_REF) == 0)
1914 * For diverted packets, args->rule.info
1915 * contains the divert port (in host format)
1916 * reason and direction.
1918 match = ((args->rule.info & IPFW_IS_MASK) ==
1919 IPFW_IS_DIVERT) && (
1920 ((args->rule.info & IPFW_INFO_IN) ?
1926 * We do not allow an arg of 0 so the
1927 * check of "proto" only suffices.
1929 match = (proto == cmd->arg1);
1934 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
1938 case O_IP_DST_LOOKUP:
1944 if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) {
1945 /* Determine lookup key type */
1946 vidx = ((ipfw_insn_u32 *)cmd)->d[1];
1947 if (vidx != 4 /* uid */ &&
1948 vidx != 5 /* jail */ &&
1949 is_ipv6 == 0 && is_ipv4 == 0)
1951 /* Determine key length */
1952 if (vidx == 0 /* dst-ip */ ||
1953 vidx == 1 /* src-ip */)
1955 sizeof(struct in6_addr):
1958 keylen = sizeof(key);
1961 if (vidx == 0 /* dst-ip */)
1962 pkey = is_ipv4 ? (void *)&dst_ip:
1963 (void *)&args->f_id.dst_ip6;
1964 else if (vidx == 1 /* src-ip */)
1965 pkey = is_ipv4 ? (void *)&src_ip:
1966 (void *)&args->f_id.src_ip6;
1967 else if (vidx == 6 /* dscp */) {
1969 key = ip->ip_tos >> 2;
1971 key = args->f_id.flow_id6;
1972 key = (key & 0x0f) << 2 |
1973 (key & 0xf000) >> 14;
1976 } else if (vidx == 2 /* dst-port */ ||
1977 vidx == 3 /* src-port */) {
1978 /* Skip fragments */
1981 /* Skip proto without ports */
1982 if (proto != IPPROTO_TCP &&
1983 proto != IPPROTO_UDP &&
1984 proto != IPPROTO_UDPLITE &&
1985 proto != IPPROTO_SCTP)
1987 if (vidx == 2 /* dst-port */)
1993 else if (vidx == 4 /* uid */ ||
1994 vidx == 5 /* jail */) {
1996 (ipfw_insn_u32 *)cmd,
1997 args, &ucred_lookup,
2000 if (vidx == 4 /* uid */)
2001 key = ucred_cache->cr_uid;
2002 else if (vidx == 5 /* jail */)
2003 key = ucred_cache->cr_prison->pr_id;
2004 #else /* !__FreeBSD__ */
2005 (void *)&ucred_cache);
2006 if (vidx == 4 /* uid */)
2007 key = ucred_cache.uid;
2008 else if (vidx == 5 /* jail */)
2009 key = ucred_cache.xid;
2010 #endif /* !__FreeBSD__ */
2012 #endif /* !USERSPACE */
2015 match = ipfw_lookup_table(chain,
2016 cmd->arg1, keylen, pkey, &vidx);
2022 /* cmdlen =< F_INSN_SIZE(ipfw_insn_u32) */
2025 case O_IP_SRC_LOOKUP:
2032 keylen = sizeof(in_addr_t);
2033 if (cmd->opcode == O_IP_DST_LOOKUP)
2037 } else if (is_ipv6) {
2038 keylen = sizeof(struct in6_addr);
2039 if (cmd->opcode == O_IP_DST_LOOKUP)
2040 pkey = &args->f_id.dst_ip6;
2042 pkey = &args->f_id.src_ip6;
2045 match = ipfw_lookup_table(chain, cmd->arg1,
2046 keylen, pkey, &vidx);
2049 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) {
2050 match = ((ipfw_insn_u32 *)cmd)->d[0] ==
2051 TARG_VAL(chain, vidx, tag);
2059 case O_IP_FLOW_LOOKUP:
2062 match = ipfw_lookup_table(chain,
2063 cmd->arg1, 0, &args->f_id, &v);
2064 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
2065 match = ((ipfw_insn_u32 *)cmd)->d[0] ==
2066 TARG_VAL(chain, v, tag);
2075 (cmd->opcode == O_IP_DST_MASK) ?
2076 dst_ip.s_addr : src_ip.s_addr;
2077 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d;
2080 for (; !match && i>0; i-= 2, p+= 2)
2081 match = (p[0] == (a & p[1]));
2087 match = in_localip(src_ip);
2094 ipfw_localip6(&args->f_id.src_ip6);
2101 u_int32_t *d = (u_int32_t *)(cmd+1);
2103 cmd->opcode == O_IP_DST_SET ?
2109 addr -= d[0]; /* subtract base */
2110 match = (addr < cmd->arg1) &&
2111 ( d[ 1 + (addr>>5)] &
2112 (1<<(addr & 0x1f)) );
2118 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
2124 match = in_localip(dst_ip);
2131 ipfw_localip6(&args->f_id.dst_ip6);
2139 * offset == 0 && proto != 0 is enough
2140 * to guarantee that we have a
2141 * packet with port info.
2143 if ((proto == IPPROTO_UDP ||
2144 proto == IPPROTO_UDPLITE ||
2145 proto == IPPROTO_TCP ||
2146 proto==IPPROTO_SCTP) && offset == 0) {
2148 (cmd->opcode == O_IP_SRCPORT) ?
2149 src_port : dst_port ;
2151 ((ipfw_insn_u16 *)cmd)->ports;
2154 for (i = cmdlen - 1; !match && i>0;
2156 match = (x>=p[0] && x<=p[1]);
2161 match = (offset == 0 && proto==IPPROTO_ICMP &&
2162 icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) );
2167 match = is_ipv6 && offset == 0 &&
2168 proto==IPPROTO_ICMPV6 &&
2170 ICMP6(ulp)->icmp6_type,
2171 (ipfw_insn_u32 *)cmd);
2177 ipopts_match(ip, cmd) );
2182 cmd->arg1 == ip->ip_v);
2190 { /* only for IP packets */
2195 if (cmd->opcode == O_IPLEN)
2197 else if (cmd->opcode == O_IPTTL)
2199 else /* must be IPID */
2200 x = ntohs(ip->ip_id);
2202 match = (cmd->arg1 == x);
2205 /* otherwise we have ranges */
2206 p = ((ipfw_insn_u16 *)cmd)->ports;
2208 for (; !match && i>0; i--, p += 2)
2209 match = (x >= p[0] && x <= p[1]);
2213 case O_IPPRECEDENCE:
2215 (cmd->arg1 == (ip->ip_tos & 0xe0)) );
2220 flags_match(cmd, ip->ip_tos));
2228 p = ((ipfw_insn_u32 *)cmd)->d;
2231 x = ip->ip_tos >> 2;
2234 v = &((struct ip6_hdr *)ip)->ip6_vfc;
2235 x = (*v & 0x0F) << 2;
2241 /* DSCP bitmask is stored as low_u32 high_u32 */
2243 match = *(p + 1) & (1 << (x - 32));
2245 match = *p & (1 << x);
2250 if (proto == IPPROTO_TCP && offset == 0) {
2257 struct ip6_hdr *ip6;
2259 ip6 = (struct ip6_hdr *)ip;
2260 if (ip6->ip6_plen == 0) {
2262 * Jumbo payload is not
2271 x = iplen - (ip->ip_hl << 2);
2273 x -= tcp->th_off << 2;
2275 match = (cmd->arg1 == x);
2278 /* otherwise we have ranges */
2279 p = ((ipfw_insn_u16 *)cmd)->ports;
2281 for (; !match && i>0; i--, p += 2)
2282 match = (x >= p[0] && x <= p[1]);
2287 match = (proto == IPPROTO_TCP && offset == 0 &&
2288 flags_match(cmd, TCP(ulp)->th_flags));
2292 if (proto == IPPROTO_TCP && offset == 0 && ulp){
2293 PULLUP_LEN_LOCKED(hlen, ulp,
2294 (TCP(ulp)->th_off << 2));
2295 match = tcpopts_match(TCP(ulp), cmd);
2300 match = (proto == IPPROTO_TCP && offset == 0 &&
2301 ((ipfw_insn_u32 *)cmd)->d[0] ==
2306 match = (proto == IPPROTO_TCP && offset == 0 &&
2307 ((ipfw_insn_u32 *)cmd)->d[0] ==
2312 if (proto == IPPROTO_TCP && offset == 0) {
2317 x = ntohs(TCP(ulp)->th_win);
2319 match = (cmd->arg1 == x);
2322 /* Otherwise we have ranges. */
2323 p = ((ipfw_insn_u16 *)cmd)->ports;
2325 for (; !match && i > 0; i--, p += 2)
2326 match = (x >= p[0] && x <= p[1]);
2331 /* reject packets which have SYN only */
2332 /* XXX should i also check for TH_ACK ? */
2333 match = (proto == IPPROTO_TCP && offset == 0 &&
2334 (TCP(ulp)->th_flags &
2335 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
2341 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
2344 * ALTQ uses mbuf tags from another
2345 * packet filtering system - pf(4).
2346 * We allocate a tag in its format
2347 * and fill it in, pretending to be pf(4).
2350 at = pf_find_mtag(m);
2351 if (at != NULL && at->qid != 0)
2353 mtag = m_tag_get(PACKET_TAG_PF,
2354 sizeof(struct pf_mtag), M_NOWAIT | M_ZERO);
2357 * Let the packet fall back to the
2362 m_tag_prepend(m, mtag);
2363 at = (struct pf_mtag *)(mtag + 1);
2364 at->qid = altq->qid;
2370 ipfw_log(chain, f, hlen, args, m,
2371 oif, offset | ip6f_mf, tablearg, ip);
2376 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]);
2380 /* Outgoing packets automatically pass/match */
2381 match = ((oif != NULL) ||
2382 (m->m_pkthdr.rcvif == NULL) ||
2386 verify_path6(&(args->f_id.src_ip6),
2387 m->m_pkthdr.rcvif, args->f_id.fib) :
2389 verify_path(src_ip, m->m_pkthdr.rcvif,
2394 /* Outgoing packets automatically pass/match */
2395 match = (hlen > 0 && ((oif != NULL) || (
2398 verify_path6(&(args->f_id.src_ip6),
2399 NULL, args->f_id.fib) :
2401 verify_path(src_ip, NULL, args->f_id.fib))));
2405 /* Outgoing packets automatically pass/match */
2406 if (oif == NULL && hlen > 0 &&
2407 ( (is_ipv4 && in_localaddr(src_ip))
2410 in6_localaddr(&(args->f_id.src_ip6)))
2415 is_ipv6 ? verify_path6(
2416 &(args->f_id.src_ip6),
2428 match = (m_tag_find(m,
2429 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL);
2430 /* otherwise no match */
2436 IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6,
2437 &((ipfw_insn_ip6 *)cmd)->addr6);
2442 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6,
2443 &((ipfw_insn_ip6 *)cmd)->addr6);
2445 case O_IP6_SRC_MASK:
2446 case O_IP6_DST_MASK:
2450 struct in6_addr *d =
2451 &((ipfw_insn_ip6 *)cmd)->addr6;
2453 for (; !match && i > 0; d += 2,
2454 i -= F_INSN_SIZE(struct in6_addr)
2460 APPLY_MASK(&p, &d[1]);
2462 IN6_ARE_ADDR_EQUAL(&d[0],
2470 flow6id_match(args->f_id.flow_id6,
2471 (ipfw_insn_u32 *) cmd);
2476 (ext_hd & ((ipfw_insn *) cmd)->arg1);
2490 uint32_t tag = TARG(cmd->arg1, tag);
2492 /* Packet is already tagged with this tag? */
2493 mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL);
2495 /* We have `untag' action when F_NOT flag is
2496 * present. And we must remove this mtag from
2497 * mbuf and reset `match' to zero (`match' will
2498 * be inversed later).
2499 * Otherwise we should allocate new mtag and
2500 * push it into mbuf.
2502 if (cmd->len & F_NOT) { /* `untag' action */
2504 m_tag_delete(m, mtag);
2508 mtag = m_tag_alloc( MTAG_IPFW,
2511 m_tag_prepend(m, mtag);
2518 case O_FIB: /* try match the specified fib */
2519 if (args->f_id.fib == cmd->arg1)
2524 #ifndef USERSPACE /* not supported in userspace */
2525 struct inpcb *inp = args->inp;
2526 struct inpcbinfo *pi;
2528 if (is_ipv6) /* XXX can we remove this ? */
2531 if (proto == IPPROTO_TCP)
2533 else if (proto == IPPROTO_UDP)
2535 else if (proto == IPPROTO_UDPLITE)
2536 pi = &V_ulitecbinfo;
2541 * XXXRW: so_user_cookie should almost
2542 * certainly be inp_user_cookie?
2545 /* For incoming packet, lookup up the
2546 inpcb using the src/dest ip/port tuple */
2548 inp = in_pcblookup(pi,
2549 src_ip, htons(src_port),
2550 dst_ip, htons(dst_port),
2551 INPLOOKUP_RLOCKPCB, NULL);
2554 inp->inp_socket->so_user_cookie;
2560 if (inp->inp_socket) {
2562 inp->inp_socket->so_user_cookie;
2567 #endif /* !USERSPACE */
2573 uint32_t tag = TARG(cmd->arg1, tag);
2576 match = m_tag_locate(m, MTAG_IPFW,
2581 /* we have ranges */
2582 for (mtag = m_tag_first(m);
2583 mtag != NULL && !match;
2584 mtag = m_tag_next(m, mtag)) {
2588 if (mtag->m_tag_cookie != MTAG_IPFW)
2591 p = ((ipfw_insn_u16 *)cmd)->ports;
2593 for(; !match && i > 0; i--, p += 2)
2595 mtag->m_tag_id >= p[0] &&
2596 mtag->m_tag_id <= p[1];
2602 * The second set of opcodes represents 'actions',
2603 * i.e. the terminal part of a rule once the packet
2604 * matches all previous patterns.
2605 * Typically there is only one action for each rule,
2606 * and the opcode is stored at the end of the rule
2607 * (but there are exceptions -- see below).
2609 * In general, here we set retval and terminate the
2610 * outer loop (would be a 'break 3' in some language,
2611 * but we need to set l=0, done=1)
2614 * O_COUNT and O_SKIPTO actions:
2615 * instead of terminating, we jump to the next rule
2616 * (setting l=0), or to the SKIPTO target (setting
2617 * f/f_len, cmd and l as needed), respectively.
2619 * O_TAG, O_LOG and O_ALTQ action parameters:
2620 * perform some action and set match = 1;
2622 * O_LIMIT and O_KEEP_STATE: these opcodes are
2623 * not real 'actions', and are stored right
2624 * before the 'action' part of the rule (one
2625 * exception is O_SKIP_ACTION which could be
2626 * between these opcodes and 'action' one).
2627 * These opcodes try to install an entry in the
2628 * state tables; if successful, we continue with
2629 * the next opcode (match=1; break;), otherwise
2630 * the packet must be dropped (set retval,
2631 * break loops with l=0, done=1)
2633 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2634 * cause a lookup of the state table, and a jump
2635 * to the 'action' part of the parent rule
2636 * if an entry is found, or
2637 * (CHECK_STATE only) a jump to the next rule if
2638 * the entry is not found.
2639 * The result of the lookup is cached so that
2640 * further instances of these opcodes become NOPs.
2641 * The jump to the next rule is done by setting
2644 * O_SKIP_ACTION: this opcode is not a real 'action'
2645 * either, and is stored right before the 'action'
2646 * part of the rule, right after the O_KEEP_STATE
2647 * opcode. It causes match failure so the real
2648 * 'action' could be executed only if the rule
2649 * is checked via dynamic rule from the state
2650 * table, as in such case execution starts
2651 * from the true 'action' opcode directly.
2656 if (ipfw_dyn_install_state(chain, f,
2657 (ipfw_insn_limit *)cmd, args, ulp,
2658 pktlen, &dyn_info, tablearg)) {
2659 /* error or limit violation */
2660 retval = IP_FW_DENY;
2661 l = 0; /* exit inner loop */
2662 done = 1; /* exit outer loop */
2670 * dynamic rules are checked at the first
2671 * keep-state or check-state occurrence,
2672 * with the result being stored in dyn_info.
2673 * The compiler introduces a PROBE_STATE
2674 * instruction for us when we have a
2675 * KEEP_STATE (because PROBE_STATE needs
2678 if (DYN_LOOKUP_NEEDED(&dyn_info, cmd) &&
2679 (q = ipfw_dyn_lookup_state(args, ulp,
2680 pktlen, cmd, &dyn_info)) != NULL) {
2682 * Found dynamic entry, jump to the
2683 * 'action' part of the parent rule
2684 * by setting f, cmd, l and clearing
2688 f_pos = dyn_info.f_pos;
2689 cmd = ACTION_PTR(f);
2690 l = f->cmd_len - f->act_ofs;
2696 * Dynamic entry not found. If CHECK_STATE,
2697 * skip to next rule, if PROBE_STATE just
2698 * ignore and continue with next opcode.
2700 if (cmd->opcode == O_CHECK_STATE)
2701 l = 0; /* exit inner loop */
2706 match = 0; /* skip to the next rule */
2707 l = 0; /* exit inner loop */
2711 retval = 0; /* accept */
2712 l = 0; /* exit inner loop */
2713 done = 1; /* exit outer loop */
2718 set_match(args, f_pos, chain);
2719 args->rule.info = TARG(cmd->arg1, pipe);
2720 if (cmd->opcode == O_PIPE)
2721 args->rule.info |= IPFW_IS_PIPE;
2723 args->rule.info |= IPFW_ONEPASS;
2724 retval = IP_FW_DUMMYNET;
2725 l = 0; /* exit inner loop */
2726 done = 1; /* exit outer loop */
2731 if (args->flags & IPFW_ARGS_ETHER)
2732 break; /* not on layer 2 */
2733 /* otherwise this is terminal */
2734 l = 0; /* exit inner loop */
2735 done = 1; /* exit outer loop */
2736 retval = (cmd->opcode == O_DIVERT) ?
2737 IP_FW_DIVERT : IP_FW_TEE;
2738 set_match(args, f_pos, chain);
2739 args->rule.info = TARG(cmd->arg1, divert);
2743 IPFW_INC_RULE_COUNTER(f, pktlen);
2744 l = 0; /* exit inner loop */
2748 IPFW_INC_RULE_COUNTER(f, pktlen);
2749 f_pos = JUMP(chain, f, cmd->arg1, tablearg, 0);
2751 * Skip disabled rules, and re-enter
2752 * the inner loop with the correct
2753 * f_pos, f, l and cmd.
2754 * Also clear cmdlen and skip_or
2756 for (; f_pos < chain->n_rules - 1 &&
2758 (1 << chain->map[f_pos]->set));
2761 /* Re-enter the inner loop at the skipto rule. */
2762 f = chain->map[f_pos];
2769 break; /* not reached */
2771 case O_CALLRETURN: {
2773 * Implementation of `subroutine' call/return,
2774 * in the stack carried in an mbuf tag. This
2775 * is different from `skipto' in that any call
2776 * address is possible (`skipto' must prevent
2777 * backward jumps to avoid endless loops).
2778 * We have `return' action when F_NOT flag is
2779 * present. The `m_tag_id' field is used as
2783 uint16_t jmpto, *stack;
2785 #define IS_CALL ((cmd->len & F_NOT) == 0)
2786 #define IS_RETURN ((cmd->len & F_NOT) != 0)
2788 * Hand-rolled version of m_tag_locate() with
2790 * If not already tagged, allocate new tag.
2792 mtag = m_tag_first(m);
2793 while (mtag != NULL) {
2794 if (mtag->m_tag_cookie ==
2797 mtag = m_tag_next(m, mtag);
2799 if (mtag == NULL && IS_CALL) {
2800 mtag = m_tag_alloc(MTAG_IPFW_CALL, 0,
2801 IPFW_CALLSTACK_SIZE *
2802 sizeof(uint16_t), M_NOWAIT);
2804 m_tag_prepend(m, mtag);
2808 * On error both `call' and `return' just
2809 * continue with next rule.
2811 if (IS_RETURN && (mtag == NULL ||
2812 mtag->m_tag_id == 0)) {
2813 l = 0; /* exit inner loop */
2816 if (IS_CALL && (mtag == NULL ||
2817 mtag->m_tag_id >= IPFW_CALLSTACK_SIZE)) {
2818 printf("ipfw: call stack error, "
2819 "go to next rule\n");
2820 l = 0; /* exit inner loop */
2824 IPFW_INC_RULE_COUNTER(f, pktlen);
2825 stack = (uint16_t *)(mtag + 1);
2828 * The `call' action may use cached f_pos
2829 * (in f->next_rule), whose version is written
2831 * The `return' action, however, doesn't have
2832 * fixed jump address in cmd->arg1 and can't use
2836 stack[mtag->m_tag_id] = f->rulenum;
2838 f_pos = JUMP(chain, f, cmd->arg1,
2840 } else { /* `return' action */
2842 jmpto = stack[mtag->m_tag_id] + 1;
2843 f_pos = ipfw_find_rule(chain, jmpto, 0);
2847 * Skip disabled rules, and re-enter
2848 * the inner loop with the correct
2849 * f_pos, f, l and cmd.
2850 * Also clear cmdlen and skip_or
2852 for (; f_pos < chain->n_rules - 1 &&
2854 (1 << chain->map[f_pos]->set)); f_pos++)
2856 /* Re-enter the inner loop at the dest rule. */
2857 f = chain->map[f_pos];
2863 break; /* NOTREACHED */
2870 * Drop the packet and send a reject notice
2871 * if the packet is not ICMP (or is an ICMP
2872 * query), and it is not multicast/broadcast.
2874 if (hlen > 0 && is_ipv4 && offset == 0 &&
2875 (proto != IPPROTO_ICMP ||
2876 is_icmp_query(ICMP(ulp))) &&
2877 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2878 !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2879 send_reject(args, cmd->arg1, iplen, ip);
2885 if (hlen > 0 && is_ipv6 &&
2886 ((offset & IP6F_OFF_MASK) == 0) &&
2887 (proto != IPPROTO_ICMPV6 ||
2888 (is_icmp6_query(icmp6_type) == 1)) &&
2889 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2890 !IN6_IS_ADDR_MULTICAST(
2891 &args->f_id.dst_ip6)) {
2893 cmd->opcode == O_REJECT ?
2894 map_icmp_unreach(cmd->arg1):
2896 (struct ip6_hdr *)ip);
2902 retval = IP_FW_DENY;
2903 l = 0; /* exit inner loop */
2904 done = 1; /* exit outer loop */
2908 if (args->flags & IPFW_ARGS_ETHER)
2909 break; /* not valid on layer2 pkts */
2911 dyn_info.direction == MATCH_FORWARD) {
2912 struct sockaddr_in *sa;
2914 sa = &(((ipfw_insn_sa *)cmd)->sa);
2915 if (sa->sin_addr.s_addr == INADDR_ANY) {
2918 * We use O_FORWARD_IP opcode for
2919 * fwd rule with tablearg, but tables
2920 * now support IPv6 addresses. And
2921 * when we are inspecting IPv6 packet,
2922 * we can use nh6 field from
2923 * table_value as next_hop6 address.
2926 struct ip_fw_nh6 *nh6;
2928 args->flags |= IPFW_ARGS_NH6;
2929 nh6 = &args->hopstore6;
2930 nh6->sin6_addr = TARG_VAL(
2931 chain, tablearg, nh6);
2932 nh6->sin6_port = sa->sin_port;
2933 nh6->sin6_scope_id = TARG_VAL(
2934 chain, tablearg, zoneid);
2938 args->flags |= IPFW_ARGS_NH4;
2939 args->hopstore.sin_port =
2941 sa = &args->hopstore;
2942 sa->sin_family = AF_INET;
2943 sa->sin_len = sizeof(*sa);
2944 sa->sin_addr.s_addr = htonl(
2945 TARG_VAL(chain, tablearg,
2949 args->flags |= IPFW_ARGS_NH4PTR;
2950 args->next_hop = sa;
2953 retval = IP_FW_PASS;
2954 l = 0; /* exit inner loop */
2955 done = 1; /* exit outer loop */
2960 if (args->flags & IPFW_ARGS_ETHER)
2961 break; /* not valid on layer2 pkts */
2963 dyn_info.direction == MATCH_FORWARD) {
2964 struct sockaddr_in6 *sin6;
2966 sin6 = &(((ipfw_insn_sa6 *)cmd)->sa);
2967 args->flags |= IPFW_ARGS_NH6PTR;
2968 args->next_hop6 = sin6;
2970 retval = IP_FW_PASS;
2971 l = 0; /* exit inner loop */
2972 done = 1; /* exit outer loop */
2978 set_match(args, f_pos, chain);
2979 args->rule.info = TARG(cmd->arg1, netgraph);
2981 args->rule.info |= IPFW_ONEPASS;
2982 retval = (cmd->opcode == O_NETGRAPH) ?
2983 IP_FW_NETGRAPH : IP_FW_NGTEE;
2984 l = 0; /* exit inner loop */
2985 done = 1; /* exit outer loop */
2991 IPFW_INC_RULE_COUNTER(f, pktlen);
2992 fib = TARG(cmd->arg1, fib) & 0x7FFF;
2993 if (fib >= rt_numfibs)
2996 args->f_id.fib = fib; /* XXX */
2997 l = 0; /* exit inner loop */
3004 code = TARG(cmd->arg1, dscp) & 0x3F;
3005 l = 0; /* exit inner loop */
3009 old = *(uint16_t *)ip;
3010 ip->ip_tos = (code << 2) |
3011 (ip->ip_tos & 0x03);
3012 ip->ip_sum = cksum_adjust(ip->ip_sum,
3013 old, *(uint16_t *)ip);
3014 } else if (is_ipv6) {
3017 v = &((struct ip6_hdr *)ip)->ip6_vfc;
3018 *v = (*v & 0xF0) | (code >> 2);
3020 *v = (*v & 0x3F) | ((code & 0x03) << 6);
3024 IPFW_INC_RULE_COUNTER(f, pktlen);
3029 l = 0; /* exit inner loop */
3030 done = 1; /* exit outer loop */
3032 * Ensure that we do not invoke NAT handler for
3033 * non IPv4 packets. Libalias expects only IPv4.
3035 if (!is_ipv4 || !IPFW_NAT_LOADED) {
3036 retval = IP_FW_DENY;
3043 args->rule.info = 0;
3044 set_match(args, f_pos, chain);
3045 /* Check if this is 'global' nat rule */
3046 if (cmd->arg1 == IP_FW_NAT44_GLOBAL) {
3047 retval = ipfw_nat_ptr(args, NULL, m);
3050 t = ((ipfw_insn_nat *)cmd)->nat;
3052 nat_id = TARG(cmd->arg1, nat);
3053 t = (*lookup_nat_ptr)(&chain->nat, nat_id);
3056 retval = IP_FW_DENY;
3059 if (cmd->arg1 != IP_FW_TARG)
3060 ((ipfw_insn_nat *)cmd)->nat = t;
3062 retval = ipfw_nat_ptr(args, t, m);
3068 l = 0; /* in any case exit inner loop */
3069 if (is_ipv6) /* IPv6 is not supported yet */
3071 IPFW_INC_RULE_COUNTER(f, pktlen);
3072 ip_off = ntohs(ip->ip_off);
3074 /* if not fragmented, go to next rule */
3075 if ((ip_off & (IP_MF | IP_OFFMASK)) == 0)
3078 args->m = m = ip_reass(m);
3081 * do IP header checksum fixup.
3083 if (m == NULL) { /* fragment got swallowed */
3084 retval = IP_FW_DENY;
3085 } else { /* good, packet complete */
3088 ip = mtod(m, struct ip *);
3089 hlen = ip->ip_hl << 2;
3091 if (hlen == sizeof(struct ip))
3092 ip->ip_sum = in_cksum_hdr(ip);
3094 ip->ip_sum = in_cksum(m, hlen);
3095 retval = IP_FW_REASS;
3096 args->rule.info = 0;
3097 set_match(args, f_pos, chain);
3099 done = 1; /* exit outer loop */
3102 case O_EXTERNAL_ACTION:
3103 l = 0; /* in any case exit inner loop */
3104 retval = ipfw_run_eaction(chain, args,
3107 * If both @retval and @done are zero,
3108 * consider this as rule matching and
3111 if (retval == 0 && done == 0) {
3112 IPFW_INC_RULE_COUNTER(f, pktlen);
3114 * Reset the result of the last
3115 * dynamic state lookup.
3116 * External action can change
3117 * @args content, and it may be
3118 * used for new state lookup later.
3120 DYN_INFO_INIT(&dyn_info);
3125 panic("-- unknown opcode %d\n", cmd->opcode);
3126 } /* end of switch() on opcodes */
3128 * if we get here with l=0, then match is irrelevant.
3131 if (cmd->len & F_NOT)
3135 if (cmd->len & F_OR)
3138 if (!(cmd->len & F_OR)) /* not an OR block, */
3139 break; /* try next rule */
3142 } /* end of inner loop, scan opcodes */
3144 #undef PULLUP_LEN_LOCKED
3149 /* next_rule:; */ /* try next rule */
3151 } /* end of outer for, scan rules */
3154 struct ip_fw *rule = chain->map[f_pos];
3155 /* Update statistics */
3156 IPFW_INC_RULE_COUNTER(rule, pktlen);
3158 retval = IP_FW_DENY;
3159 printf("ipfw: ouch!, skip past end of rules, denying packet\n");
3161 IPFW_PF_RUNLOCK(chain);
3163 if (ucred_cache != NULL)
3164 crfree(ucred_cache);
3170 printf("ipfw: pullup failed\n");
3171 return (IP_FW_DENY);
3175 * Set maximum number of tables that can be used in given VNET ipfw instance.
3179 sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS)
3182 unsigned int ntables;
3184 ntables = V_fw_tables_max;
3186 error = sysctl_handle_int(oidp, &ntables, 0, req);
3187 /* Read operation or some error */
3188 if ((error != 0) || (req->newptr == NULL))
3191 return (ipfw_resize_tables(&V_layer3_chain, ntables));
3195 * Switches table namespace between global and per-set.
3198 sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS)
3203 sets = V_fw_tables_sets;
3205 error = sysctl_handle_int(oidp, &sets, 0, req);
3206 /* Read operation or some error */
3207 if ((error != 0) || (req->newptr == NULL))
3210 return (ipfw_switch_tables_namespace(&V_layer3_chain, sets));
3215 * Module and VNET glue
3219 * Stuff that must be initialised only on boot or module load
3227 * Only print out this stuff the first time around,
3228 * when called from the sysinit code.
3234 "initialized, divert %s, nat %s, "
3235 "default to %s, logging ",
3241 #ifdef IPFIREWALL_NAT
3246 default_to_accept ? "accept" : "deny");
3249 * Note: V_xxx variables can be accessed here but the vnet specific
3250 * initializer may not have been called yet for the VIMAGE case.
3251 * Tuneables will have been processed. We will print out values for
3253 * XXX This should all be rationalized AFTER 8.0
3255 if (V_fw_verbose == 0)
3256 printf("disabled\n");
3257 else if (V_verbose_limit == 0)
3258 printf("unlimited\n");
3260 printf("limited to %d packets/entry by default\n",
3263 /* Check user-supplied table count for validness */
3264 if (default_fw_tables > IPFW_TABLES_MAX)
3265 default_fw_tables = IPFW_TABLES_MAX;
3267 ipfw_init_sopt_handler();
3268 ipfw_init_obj_rewriter();
3274 * Called for the removal of the last instance only on module unload.
3280 ipfw_iface_destroy();
3281 ipfw_destroy_sopt_handler();
3282 ipfw_destroy_obj_rewriter();
3283 printf("IP firewall unloaded\n");
3287 * Stuff that must be initialized for every instance
3288 * (including the first of course).
3291 vnet_ipfw_init(const void *unused)
3294 struct ip_fw *rule = NULL;
3295 struct ip_fw_chain *chain;
3297 chain = &V_layer3_chain;
3299 first = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
3301 /* First set up some values that are compile time options */
3302 V_autoinc_step = 100; /* bounded to 1..1000 in add_rule() */
3303 V_fw_deny_unknown_exthdrs = 1;
3304 #ifdef IPFIREWALL_VERBOSE
3307 #ifdef IPFIREWALL_VERBOSE_LIMIT
3308 V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
3310 #ifdef IPFIREWALL_NAT
3311 LIST_INIT(&chain->nat);
3314 /* Init shared services hash table */
3315 ipfw_init_srv(chain);
3317 ipfw_init_counters();
3318 /* Set initial number of tables */
3319 V_fw_tables_max = default_fw_tables;
3320 error = ipfw_init_tables(chain, first);
3322 printf("ipfw2: setting up tables failed\n");
3323 free(chain->map, M_IPFW);
3328 IPFW_LOCK_INIT(chain);
3330 /* fill and insert the default rule */
3331 rule = ipfw_alloc_rule(chain, sizeof(struct ip_fw));
3333 rule->cmd[0].len = 1;
3334 rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY;
3335 chain->default_rule = rule;
3336 ipfw_add_protected_rule(chain, rule, 0);
3338 ipfw_dyn_init(chain);
3339 ipfw_eaction_init(chain, first);
3340 #ifdef LINEAR_SKIPTO
3341 ipfw_init_skipto_cache(chain);
3343 ipfw_bpf_init(first);
3345 /* First set up some values that are compile time options */
3346 V_ipfw_vnet_ready = 1; /* Open for business */
3349 * Hook the sockopt handler and pfil hooks for ipv4 and ipv6.
3350 * Even if the latter two fail we still keep the module alive
3351 * because the sockopt and layer2 paths are still useful.
3352 * ipfw[6]_hook return 0 on success, ENOENT on failure,
3353 * so we can ignore the exact return value and just set a flag.
3355 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so
3356 * changes in the underlying (per-vnet) variables trigger
3357 * immediate hook()/unhook() calls.
3358 * In layer2 we have the same behaviour, except that V_ether_ipfw
3359 * is checked on each packet because there are no pfil hooks.
3361 V_ip_fw_ctl_ptr = ipfw_ctl3;
3362 error = ipfw_attach_hooks(1);
3367 * Called for the removal of each instance.
3370 vnet_ipfw_uninit(const void *unused)
3373 struct ip_fw_chain *chain = &V_layer3_chain;
3376 V_ipfw_vnet_ready = 0; /* tell new callers to go away */
3378 * disconnect from ipv4, ipv6, layer2 and sockopt.
3379 * Then grab, release and grab again the WLOCK so we make
3380 * sure the update is propagated and nobody will be in.
3382 (void)ipfw_attach_hooks(0 /* detach */);
3383 V_ip_fw_ctl_ptr = NULL;
3385 last = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
3387 IPFW_UH_WLOCK(chain);
3388 IPFW_UH_WUNLOCK(chain);
3390 ipfw_dyn_uninit(0); /* run the callout_drain */
3392 IPFW_UH_WLOCK(chain);
3396 for (i = 0; i < chain->n_rules; i++)
3397 ipfw_reap_add(chain, &reap, chain->map[i]);
3398 free(chain->map, M_IPFW);
3399 #ifdef LINEAR_SKIPTO
3400 ipfw_destroy_skipto_cache(chain);
3402 IPFW_WUNLOCK(chain);
3403 IPFW_UH_WUNLOCK(chain);
3404 ipfw_destroy_tables(chain, last);
3405 ipfw_eaction_uninit(chain, last);
3407 ipfw_reap_rules(reap);
3408 vnet_ipfw_iface_destroy(chain);
3409 ipfw_destroy_srv(chain);
3410 IPFW_LOCK_DESTROY(chain);
3411 ipfw_dyn_uninit(1); /* free the remaining parts */
3412 ipfw_destroy_counters();
3413 ipfw_bpf_uninit(last);
3418 * Module event handler.
3419 * In general we have the choice of handling most of these events by the
3420 * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to
3421 * use the SYSINIT handlers as they are more capable of expressing the
3422 * flow of control during module and vnet operations, so this is just
3423 * a skeleton. Note there is no SYSINIT equivalent of the module
3424 * SHUTDOWN handler, but we don't have anything to do in that case anyhow.
3427 ipfw_modevent(module_t mod, int type, void *unused)
3433 /* Called once at module load or
3434 * system boot if compiled in. */
3437 /* Called before unload. May veto unloading. */
3440 /* Called during unload. */
3443 /* Called during system shutdown. */
3452 static moduledata_t ipfwmod = {
3458 /* Define startup order. */
3459 #define IPFW_SI_SUB_FIREWALL SI_SUB_PROTO_FIREWALL
3460 #define IPFW_MODEVENT_ORDER (SI_ORDER_ANY - 255) /* On boot slot in here. */
3461 #define IPFW_MODULE_ORDER (IPFW_MODEVENT_ORDER + 1) /* A little later. */
3462 #define IPFW_VNET_ORDER (IPFW_MODEVENT_ORDER + 2) /* Later still. */
3464 DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER);
3465 FEATURE(ipfw_ctl3, "ipfw new sockopt calls");
3466 MODULE_VERSION(ipfw, 3);
3467 /* should declare some dependencies here */
3470 * Starting up. Done in order after ipfwmod() has been called.
3471 * VNET_SYSINIT is also called for each existing vnet and each new vnet.
3473 SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
3475 VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
3476 vnet_ipfw_init, NULL);
3479 * Closing up shop. These are done in REVERSE ORDER, but still
3480 * after ipfwmod() has been called. Not called on reboot.
3481 * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
3482 * or when the module is unloaded.
3484 SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
3485 ipfw_destroy, NULL);
3486 VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
3487 vnet_ipfw_uninit, NULL);