2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * The FreeBSD IP packet firewall, main file
36 #include "opt_ipdivert.h"
39 #error "IPFIREWALL requires INET"
41 #include "opt_inet6.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/condvar.h>
46 #include <sys/counter.h>
47 #include <sys/eventhandler.h>
48 #include <sys/malloc.h>
50 #include <sys/kernel.h>
53 #include <sys/module.h>
56 #include <sys/rwlock.h>
57 #include <sys/rmlock.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/sysctl.h>
61 #include <sys/syslog.h>
62 #include <sys/ucred.h>
63 #include <net/ethernet.h> /* for ETHERTYPE_IP */
65 #include <net/if_var.h>
66 #include <net/route.h>
70 #include <netpfil/pf/pf_mtag.h>
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #include <netinet/in_pcb.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/ip_icmp.h>
78 #include <netinet/ip_fw.h>
79 #include <netinet/ip_carp.h>
80 #include <netinet/pim.h>
81 #include <netinet/tcp_var.h>
82 #include <netinet/udp.h>
83 #include <netinet/udp_var.h>
84 #include <netinet/sctp.h>
85 #include <netinet/sctp_crc32.h>
86 #include <netinet/sctp_header.h>
88 #include <netinet/ip6.h>
89 #include <netinet/icmp6.h>
90 #include <netinet/in_fib.h>
92 #include <netinet6/in6_fib.h>
93 #include <netinet6/in6_pcb.h>
94 #include <netinet6/scope6_var.h>
95 #include <netinet6/ip6_var.h>
98 #include <net/if_gre.h> /* for struct grehdr */
100 #include <netpfil/ipfw/ip_fw_private.h>
102 #include <machine/in_cksum.h> /* XXX for in_cksum */
105 #include <security/mac/mac_framework.h>
109 * static variables followed by global ones.
110 * All ipfw global variables are here.
113 VNET_DEFINE_STATIC(int, fw_deny_unknown_exthdrs);
114 #define V_fw_deny_unknown_exthdrs VNET(fw_deny_unknown_exthdrs)
116 VNET_DEFINE_STATIC(int, fw_permit_single_frag6) = 1;
117 #define V_fw_permit_single_frag6 VNET(fw_permit_single_frag6)
119 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
120 static int default_to_accept = 1;
122 static int default_to_accept;
125 VNET_DEFINE(int, autoinc_step);
126 VNET_DEFINE(int, fw_one_pass) = 1;
128 VNET_DEFINE(unsigned int, fw_tables_max);
129 VNET_DEFINE(unsigned int, fw_tables_sets) = 0; /* Don't use set-aware tables */
130 /* Use 128 tables by default */
131 static unsigned int default_fw_tables = IPFW_TABLES_DEFAULT;
133 #ifndef LINEAR_SKIPTO
134 static int jump_fast(struct ip_fw_chain *chain, struct ip_fw *f, int num,
135 int tablearg, int jump_backwards);
136 #define JUMP(ch, f, num, targ, back) jump_fast(ch, f, num, targ, back)
138 static int jump_linear(struct ip_fw_chain *chain, struct ip_fw *f, int num,
139 int tablearg, int jump_backwards);
140 #define JUMP(ch, f, num, targ, back) jump_linear(ch, f, num, targ, back)
144 * Each rule belongs to one of 32 different sets (0..31).
145 * The variable set_disable contains one bit per set.
146 * If the bit is set, all rules in the corresponding set
147 * are disabled. Set RESVD_SET(31) is reserved for the default rule
148 * and rules that are not deleted by the flush command,
149 * and CANNOT be disabled.
150 * Rules in set RESVD_SET can only be deleted individually.
152 VNET_DEFINE(u_int32_t, set_disable);
153 #define V_set_disable VNET(set_disable)
155 VNET_DEFINE(int, fw_verbose);
156 /* counter for ipfw_log(NULL...) */
157 VNET_DEFINE(u_int64_t, norule_counter);
158 VNET_DEFINE(int, verbose_limit);
160 /* layer3_chain contains the list of rules for layer 3 */
161 VNET_DEFINE(struct ip_fw_chain, layer3_chain);
163 /* ipfw_vnet_ready controls when we are open for business */
164 VNET_DEFINE(int, ipfw_vnet_ready) = 0;
166 VNET_DEFINE(int, ipfw_nat_ready) = 0;
168 ipfw_nat_t *ipfw_nat_ptr = NULL;
169 struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int);
170 ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
171 ipfw_nat_cfg_t *ipfw_nat_del_ptr;
172 ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
173 ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
176 uint32_t dummy_def = IPFW_DEFAULT_RULE;
177 static int sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS);
178 static int sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS);
182 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
183 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, one_pass,
184 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0,
185 "Only do a single pass through ipfw when using dummynet(4)");
186 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step,
187 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(autoinc_step), 0,
188 "Rule number auto-increment step");
189 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose,
190 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0,
191 "Log matches to ipfw rules");
192 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit,
193 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(verbose_limit), 0,
194 "Set upper limit of matches of ipfw rules logged");
195 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD,
197 "The default/max possible rule number.");
198 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_max,
199 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, 0, 0, sysctl_ipfw_table_num, "IU",
200 "Maximum number of concurrently used tables");
201 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_sets,
202 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW,
203 0, 0, sysctl_ipfw_tables_sets, "IU",
204 "Use per-set namespace for tables");
205 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN,
206 &default_to_accept, 0,
207 "Make the default rule accept all packets.");
208 TUNABLE_INT("net.inet.ip.fw.tables_max", (int *)&default_fw_tables);
209 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count,
210 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0,
211 "Number of static rules");
214 SYSCTL_DECL(_net_inet6_ip6);
215 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
216 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs,
217 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
218 &VNET_NAME(fw_deny_unknown_exthdrs), 0,
219 "Deny packets with unknown IPv6 Extension Headers");
220 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, permit_single_frag6,
221 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
222 &VNET_NAME(fw_permit_single_frag6), 0,
223 "Permit single packet IPv6 fragments");
228 #endif /* SYSCTL_NODE */
232 * Some macros used in the various matching options.
233 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
234 * Other macros just cast void * into the appropriate type
236 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
237 #define TCP(p) ((struct tcphdr *)(p))
238 #define SCTP(p) ((struct sctphdr *)(p))
239 #define UDP(p) ((struct udphdr *)(p))
240 #define ICMP(p) ((struct icmphdr *)(p))
241 #define ICMP6(p) ((struct icmp6_hdr *)(p))
244 icmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd)
246 int type = icmp->icmp_type;
248 return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) );
251 #define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
252 (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
255 is_icmp_query(struct icmphdr *icmp)
257 int type = icmp->icmp_type;
259 return (type <= ICMP_MAXTYPE && (TT & (1<<type)) );
264 * The following checks use two arrays of 8 or 16 bits to store the
265 * bits that we want set or clear, respectively. They are in the
266 * low and high half of cmd->arg1 or cmd->d[0].
268 * We scan options and store the bits we find set. We succeed if
270 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
272 * The code is sometimes optimized not to store additional variables.
276 flags_match(ipfw_insn *cmd, u_int8_t bits)
281 if ( ((cmd->arg1 & 0xff) & bits) != 0)
282 return 0; /* some bits we want set were clear */
283 want_clear = (cmd->arg1 >> 8) & 0xff;
284 if ( (want_clear & bits) != want_clear)
285 return 0; /* some bits we want clear were set */
290 ipopts_match(struct ip *ip, ipfw_insn *cmd)
292 int optlen, bits = 0;
293 u_char *cp = (u_char *)(ip + 1);
294 int x = (ip->ip_hl << 2) - sizeof (struct ip);
296 for (; x > 0; x -= optlen, cp += optlen) {
297 int opt = cp[IPOPT_OPTVAL];
299 if (opt == IPOPT_EOL)
301 if (opt == IPOPT_NOP)
304 optlen = cp[IPOPT_OLEN];
305 if (optlen <= 0 || optlen > x)
306 return 0; /* invalid or truncated */
314 bits |= IP_FW_IPOPT_LSRR;
318 bits |= IP_FW_IPOPT_SSRR;
322 bits |= IP_FW_IPOPT_RR;
326 bits |= IP_FW_IPOPT_TS;
330 return (flags_match(cmd, bits));
334 tcpopts_parse(struct tcphdr *tcp, uint16_t *mss)
336 u_char *cp = (u_char *)(tcp + 1);
337 int optlen, bits = 0;
338 int x = (tcp->th_off << 2) - sizeof(struct tcphdr);
340 for (; x > 0; x -= optlen, cp += optlen) {
342 if (opt == TCPOPT_EOL)
344 if (opt == TCPOPT_NOP)
357 bits |= IP_FW_TCPOPT_MSS;
359 *mss = be16dec(cp + 2);
363 bits |= IP_FW_TCPOPT_WINDOW;
366 case TCPOPT_SACK_PERMITTED:
368 bits |= IP_FW_TCPOPT_SACK;
371 case TCPOPT_TIMESTAMP:
372 bits |= IP_FW_TCPOPT_TS;
380 tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd)
383 return (flags_match(cmd, tcpopts_parse(tcp, NULL)));
387 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd, struct ip_fw_chain *chain,
391 if (ifp == NULL) /* no iface with this packet, match fails */
394 /* Check by name or by IP address */
395 if (cmd->name[0] != '\0') { /* match by name */
396 if (cmd->name[0] == '\1') /* use tablearg to match */
397 return ipfw_lookup_table(chain, cmd->p.kidx, 0,
398 &ifp->if_index, tablearg);
401 if (fnmatch(cmd->name, ifp->if_xname, 0) == 0)
404 if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
408 #if !defined(USERSPACE) && defined(__FreeBSD__) /* and OSX too ? */
412 CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
413 if (ia->ifa_addr->sa_family != AF_INET)
415 if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
416 (ia->ifa_addr))->sin_addr.s_addr) {
417 if_addr_runlock(ifp);
418 return(1); /* match */
421 if_addr_runlock(ifp);
422 #endif /* __FreeBSD__ */
424 return(0); /* no match, fail ... */
428 * The verify_path function checks if a route to the src exists and
429 * if it is reachable via ifp (when provided).
431 * The 'verrevpath' option checks that the interface that an IP packet
432 * arrives on is the same interface that traffic destined for the
433 * packet's source address would be routed out of.
434 * The 'versrcreach' option just checks that the source address is
435 * reachable via any route (except default) in the routing table.
436 * These two are a measure to block forged packets. This is also
437 * commonly known as "anti-spoofing" or Unicast Reverse Path
438 * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs
439 * is purposely reminiscent of the Cisco IOS command,
441 * ip verify unicast reverse-path
442 * ip verify unicast source reachable-via any
444 * which implements the same functionality. But note that the syntax
445 * is misleading, and the check may be performed on all IP packets
446 * whether unicast, multicast, or broadcast.
449 verify_path(struct in_addr src, struct ifnet *ifp, u_int fib)
451 #if defined(USERSPACE) || !defined(__FreeBSD__)
454 struct nhop4_basic nh4;
456 if (fib4_lookup_nh_basic(fib, src, NHR_IFAIF, 0, &nh4) != 0)
460 * If ifp is provided, check for equality with rtentry.
461 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
462 * in order to pass packets injected back by if_simloop():
463 * routing entry (via lo0) for our own address
464 * may exist, so we need to handle routing assymetry.
466 if (ifp != NULL && ifp != nh4.nh_ifp)
469 /* if no ifp provided, check if rtentry is not default route */
470 if (ifp == NULL && (nh4.nh_flags & NHF_DEFAULT) != 0)
473 /* or if this is a blackhole/reject route */
474 if (ifp == NULL && (nh4.nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
477 /* found valid route */
479 #endif /* __FreeBSD__ */
483 * Generate an SCTP packet containing an ABORT chunk. The verification tag
484 * is given by vtag. The T-bit is set in the ABORT chunk if and only if
485 * reflected is not 0.
489 ipfw_send_abort(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t vtag,
497 struct sctphdr *sctp;
498 struct sctp_chunkhdr *chunk;
499 u_int16_t hlen, plen, tlen;
501 MGETHDR(m, M_NOWAIT, MT_DATA);
505 M_SETFIB(m, id->fib);
508 mac_netinet_firewall_reply(replyto, m);
510 mac_netinet_firewall_send(m);
512 (void)replyto; /* don't warn about unused arg */
515 switch (id->addr_type) {
517 hlen = sizeof(struct ip);
521 hlen = sizeof(struct ip6_hdr);
529 plen = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
531 m->m_data += max_linkhdr;
532 m->m_flags |= M_SKIP_FIREWALL;
533 m->m_pkthdr.len = m->m_len = tlen;
534 m->m_pkthdr.rcvif = NULL;
535 bzero(m->m_data, tlen);
537 switch (id->addr_type) {
539 ip = mtod(m, struct ip *);
542 ip->ip_hl = sizeof(struct ip) >> 2;
543 ip->ip_tos = IPTOS_LOWDELAY;
544 ip->ip_len = htons(tlen);
545 ip->ip_id = htons(0);
546 ip->ip_off = htons(0);
547 ip->ip_ttl = V_ip_defttl;
548 ip->ip_p = IPPROTO_SCTP;
550 ip->ip_src.s_addr = htonl(id->dst_ip);
551 ip->ip_dst.s_addr = htonl(id->src_ip);
553 sctp = (struct sctphdr *)(ip + 1);
557 ip6 = mtod(m, struct ip6_hdr *);
559 ip6->ip6_vfc = IPV6_VERSION;
560 ip6->ip6_plen = htons(plen);
561 ip6->ip6_nxt = IPPROTO_SCTP;
562 ip6->ip6_hlim = IPV6_DEFHLIM;
563 ip6->ip6_src = id->dst_ip6;
564 ip6->ip6_dst = id->src_ip6;
566 sctp = (struct sctphdr *)(ip6 + 1);
571 sctp->src_port = htons(id->dst_port);
572 sctp->dest_port = htons(id->src_port);
573 sctp->v_tag = htonl(vtag);
574 sctp->checksum = htonl(0);
576 chunk = (struct sctp_chunkhdr *)(sctp + 1);
577 chunk->chunk_type = SCTP_ABORT_ASSOCIATION;
578 chunk->chunk_flags = 0;
579 if (reflected != 0) {
580 chunk->chunk_flags |= SCTP_HAD_NO_TCB;
582 chunk->chunk_length = htons(sizeof(struct sctp_chunkhdr));
584 sctp->checksum = sctp_calculate_cksum(m, hlen);
590 * Generate a TCP packet, containing either a RST or a keepalive.
591 * When flags & TH_RST, we are sending a RST packet, because of a
592 * "reset" action matched the packet.
593 * Otherwise we are sending a keepalive, and flags & TH_
594 * The 'replyto' mbuf is the mbuf being replied to, if any, and is required
595 * so that MAC can label the reply appropriately.
598 ipfw_send_pkt(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t seq,
599 u_int32_t ack, int flags)
601 struct mbuf *m = NULL; /* stupid compiler */
602 struct ip *h = NULL; /* stupid compiler */
604 struct ip6_hdr *h6 = NULL;
606 struct tcphdr *th = NULL;
609 MGETHDR(m, M_NOWAIT, MT_DATA);
613 M_SETFIB(m, id->fib);
616 mac_netinet_firewall_reply(replyto, m);
618 mac_netinet_firewall_send(m);
620 (void)replyto; /* don't warn about unused arg */
623 switch (id->addr_type) {
625 len = sizeof(struct ip) + sizeof(struct tcphdr);
629 len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
637 dir = ((flags & (TH_SYN | TH_RST)) == TH_SYN);
639 m->m_data += max_linkhdr;
640 m->m_flags |= M_SKIP_FIREWALL;
641 m->m_pkthdr.len = m->m_len = len;
642 m->m_pkthdr.rcvif = NULL;
643 bzero(m->m_data, len);
645 switch (id->addr_type) {
647 h = mtod(m, struct ip *);
649 /* prepare for checksum */
650 h->ip_p = IPPROTO_TCP;
651 h->ip_len = htons(sizeof(struct tcphdr));
653 h->ip_src.s_addr = htonl(id->src_ip);
654 h->ip_dst.s_addr = htonl(id->dst_ip);
656 h->ip_src.s_addr = htonl(id->dst_ip);
657 h->ip_dst.s_addr = htonl(id->src_ip);
660 th = (struct tcphdr *)(h + 1);
664 h6 = mtod(m, struct ip6_hdr *);
666 /* prepare for checksum */
667 h6->ip6_nxt = IPPROTO_TCP;
668 h6->ip6_plen = htons(sizeof(struct tcphdr));
670 h6->ip6_src = id->src_ip6;
671 h6->ip6_dst = id->dst_ip6;
673 h6->ip6_src = id->dst_ip6;
674 h6->ip6_dst = id->src_ip6;
677 th = (struct tcphdr *)(h6 + 1);
683 th->th_sport = htons(id->src_port);
684 th->th_dport = htons(id->dst_port);
686 th->th_sport = htons(id->dst_port);
687 th->th_dport = htons(id->src_port);
689 th->th_off = sizeof(struct tcphdr) >> 2;
691 if (flags & TH_RST) {
692 if (flags & TH_ACK) {
693 th->th_seq = htonl(ack);
694 th->th_flags = TH_RST;
698 th->th_ack = htonl(seq);
699 th->th_flags = TH_RST | TH_ACK;
703 * Keepalive - use caller provided sequence numbers
705 th->th_seq = htonl(seq);
706 th->th_ack = htonl(ack);
707 th->th_flags = TH_ACK;
710 switch (id->addr_type) {
712 th->th_sum = in_cksum(m, len);
714 /* finish the ip header */
716 h->ip_hl = sizeof(*h) >> 2;
717 h->ip_tos = IPTOS_LOWDELAY;
718 h->ip_off = htons(0);
719 h->ip_len = htons(len);
720 h->ip_ttl = V_ip_defttl;
725 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(*h6),
726 sizeof(struct tcphdr));
728 /* finish the ip6 header */
729 h6->ip6_vfc |= IPV6_VERSION;
730 h6->ip6_hlim = IPV6_DEFHLIM;
740 * ipv6 specific rules here...
743 icmp6type_match (int type, ipfw_insn_u32 *cmd)
745 return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) );
749 flow6id_match( int curr_flow, ipfw_insn_u32 *cmd )
752 for (i=0; i <= cmd->o.arg1; ++i )
753 if (curr_flow == cmd->d[i] )
758 /* support for IP6_*_ME opcodes */
759 static const struct in6_addr lla_mask = {{{
760 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
761 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
765 ipfw_localip6(struct in6_addr *in6)
767 struct rm_priotracker in6_ifa_tracker;
768 struct in6_ifaddr *ia;
770 if (IN6_IS_ADDR_MULTICAST(in6))
773 if (!IN6_IS_ADDR_LINKLOCAL(in6))
774 return (in6_localip(in6));
776 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
777 CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
778 if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr))
780 if (IN6_ARE_MASKED_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
782 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
786 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
791 verify_path6(struct in6_addr *src, struct ifnet *ifp, u_int fib)
793 struct nhop6_basic nh6;
795 if (IN6_IS_SCOPE_LINKLOCAL(src))
798 if (fib6_lookup_nh_basic(fib, src, 0, NHR_IFAIF, 0, &nh6) != 0)
801 /* If ifp is provided, check for equality with route table. */
802 if (ifp != NULL && ifp != nh6.nh_ifp)
805 /* if no ifp provided, check if rtentry is not default route */
806 if (ifp == NULL && (nh6.nh_flags & NHF_DEFAULT) != 0)
809 /* or if this is a blackhole/reject route */
810 if (ifp == NULL && (nh6.nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
813 /* found valid route */
818 is_icmp6_query(int icmp6_type)
820 if ((icmp6_type <= ICMP6_MAXTYPE) &&
821 (icmp6_type == ICMP6_ECHO_REQUEST ||
822 icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
823 icmp6_type == ICMP6_WRUREQUEST ||
824 icmp6_type == ICMP6_FQDN_QUERY ||
825 icmp6_type == ICMP6_NI_QUERY))
832 map_icmp_unreach(int code)
837 case ICMP_UNREACH_NET:
838 case ICMP_UNREACH_HOST:
839 case ICMP_UNREACH_SRCFAIL:
840 case ICMP_UNREACH_NET_UNKNOWN:
841 case ICMP_UNREACH_HOST_UNKNOWN:
842 case ICMP_UNREACH_TOSNET:
843 case ICMP_UNREACH_TOSHOST:
844 return (ICMP6_DST_UNREACH_NOROUTE);
845 case ICMP_UNREACH_PORT:
846 return (ICMP6_DST_UNREACH_NOPORT);
849 * Map the rest of codes into admit prohibited.
850 * XXX: unreach proto should be mapped into ICMPv6
851 * parameter problem, but we use only unreach type.
853 return (ICMP6_DST_UNREACH_ADMIN);
858 send_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6)
863 if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) {
865 tcp = (struct tcphdr *)((char *)ip6 + hlen);
867 if ((tcp->th_flags & TH_RST) == 0) {
869 m0 = ipfw_send_pkt(args->m, &(args->f_id),
870 ntohl(tcp->th_seq), ntohl(tcp->th_ack),
871 tcp->th_flags | TH_RST);
873 ip6_output(m0, NULL, NULL, 0, NULL, NULL,
877 } else if (code == ICMP6_UNREACH_ABORT &&
878 args->f_id.proto == IPPROTO_SCTP) {
880 struct sctphdr *sctp;
884 sctp = (struct sctphdr *)((char *)ip6 + hlen);
886 v_tag = ntohl(sctp->v_tag);
887 /* Investigate the first chunk header if available */
888 if (m->m_len >= hlen + sizeof(struct sctphdr) +
889 sizeof(struct sctp_chunkhdr)) {
890 struct sctp_chunkhdr *chunk;
892 chunk = (struct sctp_chunkhdr *)(sctp + 1);
893 switch (chunk->chunk_type) {
894 case SCTP_INITIATION:
896 * Packets containing an INIT chunk MUST have
903 /* INIT chunk MUST NOT be bundled */
904 if (m->m_pkthdr.len >
905 hlen + sizeof(struct sctphdr) +
906 ntohs(chunk->chunk_length) + 3) {
909 /* Use the initiate tag if available */
910 if ((m->m_len >= hlen + sizeof(struct sctphdr) +
911 sizeof(struct sctp_chunkhdr) +
912 offsetof(struct sctp_init, a_rwnd))) {
913 struct sctp_init *init;
915 init = (struct sctp_init *)(chunk + 1);
916 v_tag = ntohl(init->initiate_tag);
920 case SCTP_ABORT_ASSOCIATION:
922 * If the packet contains an ABORT chunk, don't
924 * XXX: We should search through all chunks,
925 * but don't do to avoid attacks.
934 m0 = ipfw_send_abort(args->m, &(args->f_id), v_tag,
938 ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
940 } else if (code != ICMP6_UNREACH_RST && code != ICMP6_UNREACH_ABORT) {
941 /* Send an ICMPv6 unreach. */
944 * Unlike above, the mbufs need to line up with the ip6 hdr,
945 * as the contents are read. We need to m_adj() the
947 * The mbuf will however be thrown away so we can adjust it.
948 * Remember we did an m_pullup on it already so we
949 * can make some assumptions about contiguousness.
952 m_adj(m, args->L3offset);
954 icmp6_error(m, ICMP6_DST_UNREACH, code, 0);
965 * sends a reject message, consuming the mbuf passed as an argument.
968 send_reject(struct ip_fw_args *args, int code, int iplen, struct ip *ip)
972 /* XXX When ip is not guaranteed to be at mtod() we will
973 * need to account for this */
974 * The mbuf will however be thrown away so we can adjust it.
975 * Remember we did an m_pullup on it already so we
976 * can make some assumptions about contiguousness.
979 m_adj(m, args->L3offset);
981 if (code != ICMP_REJECT_RST && code != ICMP_REJECT_ABORT) {
982 /* Send an ICMP unreach */
983 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
984 } else if (code == ICMP_REJECT_RST && args->f_id.proto == IPPROTO_TCP) {
985 struct tcphdr *const tcp =
986 L3HDR(struct tcphdr, mtod(args->m, struct ip *));
987 if ( (tcp->th_flags & TH_RST) == 0) {
989 m = ipfw_send_pkt(args->m, &(args->f_id),
990 ntohl(tcp->th_seq), ntohl(tcp->th_ack),
991 tcp->th_flags | TH_RST);
993 ip_output(m, NULL, NULL, 0, NULL, NULL);
996 } else if (code == ICMP_REJECT_ABORT &&
997 args->f_id.proto == IPPROTO_SCTP) {
999 struct sctphdr *sctp;
1000 struct sctp_chunkhdr *chunk;
1001 struct sctp_init *init;
1005 sctp = L3HDR(struct sctphdr, mtod(args->m, struct ip *));
1007 v_tag = ntohl(sctp->v_tag);
1008 if (iplen >= (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1009 sizeof(struct sctp_chunkhdr)) {
1010 /* Look at the first chunk header if available */
1011 chunk = (struct sctp_chunkhdr *)(sctp + 1);
1012 switch (chunk->chunk_type) {
1013 case SCTP_INITIATION:
1015 * Packets containing an INIT chunk MUST have
1022 /* INIT chunk MUST NOT be bundled */
1024 (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1025 ntohs(chunk->chunk_length) + 3) {
1028 /* Use the initiate tag if available */
1029 if ((iplen >= (ip->ip_hl << 2) +
1030 sizeof(struct sctphdr) +
1031 sizeof(struct sctp_chunkhdr) +
1032 offsetof(struct sctp_init, a_rwnd))) {
1033 init = (struct sctp_init *)(chunk + 1);
1034 v_tag = ntohl(init->initiate_tag);
1038 case SCTP_ABORT_ASSOCIATION:
1040 * If the packet contains an ABORT chunk, don't
1042 * XXX: We should search through all chunks,
1043 * but don't do to avoid attacks.
1052 m = ipfw_send_abort(args->m, &(args->f_id), v_tag,
1056 ip_output(m, NULL, NULL, 0, NULL, NULL);
1064 * Support for uid/gid/jail lookup. These tests are expensive
1065 * (because we may need to look into the list of active sockets)
1066 * so we cache the results. ugid_lookupp is 0 if we have not
1067 * yet done a lookup, 1 if we succeeded, and -1 if we tried
1068 * and failed. The function always returns the match value.
1069 * We could actually spare the variable and use *uc, setting
1070 * it to '(void *)check_uidgid if we have no info, NULL if
1071 * we tried and failed, or any other value if successful.
1074 check_uidgid(ipfw_insn_u32 *insn, struct ip_fw_args *args, int *ugid_lookupp,
1077 #if defined(USERSPACE)
1078 return 0; // not supported in userspace
1082 return cred_check(insn, proto, oif,
1083 dst_ip, dst_port, src_ip, src_port,
1084 (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb);
1086 struct in_addr src_ip, dst_ip;
1087 struct inpcbinfo *pi;
1088 struct ipfw_flow_id *id;
1089 struct inpcb *pcb, *inp;
1097 * Check to see if the UDP or TCP stack supplied us with
1098 * the PCB. If so, rather then holding a lock and looking
1099 * up the PCB, we can use the one that was supplied.
1101 if (inp && *ugid_lookupp == 0) {
1102 INP_LOCK_ASSERT(inp);
1103 if (inp->inp_socket != NULL) {
1104 *uc = crhold(inp->inp_cred);
1110 * If we have already been here and the packet has no
1111 * PCB entry associated with it, then we can safely
1112 * assume that this is a no match.
1114 if (*ugid_lookupp == -1)
1116 if (id->proto == IPPROTO_TCP) {
1119 } else if (id->proto == IPPROTO_UDP) {
1120 lookupflags = INPLOOKUP_WILDCARD;
1122 } else if (id->proto == IPPROTO_UDPLITE) {
1123 lookupflags = INPLOOKUP_WILDCARD;
1124 pi = &V_ulitecbinfo;
1127 lookupflags |= INPLOOKUP_RLOCKPCB;
1129 if (*ugid_lookupp == 0) {
1130 if (id->addr_type == 6) {
1132 if (args->flags & IPFW_ARGS_IN)
1133 pcb = in6_pcblookup_mbuf(pi,
1134 &id->src_ip6, htons(id->src_port),
1135 &id->dst_ip6, htons(id->dst_port),
1136 lookupflags, NULL, args->m);
1138 pcb = in6_pcblookup_mbuf(pi,
1139 &id->dst_ip6, htons(id->dst_port),
1140 &id->src_ip6, htons(id->src_port),
1141 lookupflags, args->ifp, args->m);
1147 src_ip.s_addr = htonl(id->src_ip);
1148 dst_ip.s_addr = htonl(id->dst_ip);
1149 if (args->flags & IPFW_ARGS_IN)
1150 pcb = in_pcblookup_mbuf(pi,
1151 src_ip, htons(id->src_port),
1152 dst_ip, htons(id->dst_port),
1153 lookupflags, NULL, args->m);
1155 pcb = in_pcblookup_mbuf(pi,
1156 dst_ip, htons(id->dst_port),
1157 src_ip, htons(id->src_port),
1158 lookupflags, args->ifp, args->m);
1161 INP_RLOCK_ASSERT(pcb);
1162 *uc = crhold(pcb->inp_cred);
1166 if (*ugid_lookupp == 0) {
1168 * We tried and failed, set the variable to -1
1169 * so we will not try again on this packet.
1175 if (insn->o.opcode == O_UID)
1176 match = ((*uc)->cr_uid == (uid_t)insn->d[0]);
1177 else if (insn->o.opcode == O_GID)
1178 match = groupmember((gid_t)insn->d[0], *uc);
1179 else if (insn->o.opcode == O_JAIL)
1180 match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]);
1182 #endif /* __FreeBSD__ */
1183 #endif /* not supported in userspace */
1187 * Helper function to set args with info on the rule after the matching
1188 * one. slot is precise, whereas we guess rule_id as they are
1189 * assigned sequentially.
1192 set_match(struct ip_fw_args *args, int slot,
1193 struct ip_fw_chain *chain)
1195 args->rule.chain_id = chain->id;
1196 args->rule.slot = slot + 1; /* we use 0 as a marker */
1197 args->rule.rule_id = 1 + chain->map[slot]->id;
1198 args->rule.rulenum = chain->map[slot]->rulenum;
1199 args->flags |= IPFW_ARGS_REF;
1202 #ifndef LINEAR_SKIPTO
1204 * Helper function to enable cached rule lookups using
1205 * cached_id and cached_pos fields in ipfw rule.
1208 jump_fast(struct ip_fw_chain *chain, struct ip_fw *f, int num,
1209 int tablearg, int jump_backwards)
1213 /* If possible use cached f_pos (in f->cached_pos),
1214 * whose version is written in f->cached_id
1215 * (horrible hacks to avoid changing the ABI).
1217 if (num != IP_FW_TARG && f->cached_id == chain->id)
1218 f_pos = f->cached_pos;
1220 int i = IP_FW_ARG_TABLEARG(chain, num, skipto);
1221 /* make sure we do not jump backward */
1222 if (jump_backwards == 0 && i <= f->rulenum)
1224 if (chain->idxmap != NULL)
1225 f_pos = chain->idxmap[i];
1227 f_pos = ipfw_find_rule(chain, i, 0);
1228 /* update the cache */
1229 if (num != IP_FW_TARG) {
1230 f->cached_id = chain->id;
1231 f->cached_pos = f_pos;
1239 * Helper function to enable real fast rule lookups.
1242 jump_linear(struct ip_fw_chain *chain, struct ip_fw *f, int num,
1243 int tablearg, int jump_backwards)
1247 num = IP_FW_ARG_TABLEARG(chain, num, skipto);
1248 /* make sure we do not jump backward */
1249 if (jump_backwards == 0 && num <= f->rulenum)
1250 num = f->rulenum + 1;
1251 f_pos = chain->idxmap[num];
1257 #define TARG(k, f) IP_FW_ARG_TABLEARG(chain, k, f)
1259 * The main check routine for the firewall.
1261 * All arguments are in args so we can modify them and return them
1262 * back to the caller.
1266 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1267 * Starts with the IP header.
1268 * args->L3offset Number of bytes bypassed if we came from L2.
1269 * e.g. often sizeof(eh) ** NOTYET **
1270 * args->ifp Incoming or outgoing interface.
1271 * args->divert_rule (in/out)
1272 * Skip up to the first rule past this rule number;
1273 * upon return, non-zero port number for divert or tee.
1275 * args->rule Pointer to the last matching rule (in/out)
1276 * args->next_hop Socket we are forwarding to (out).
1277 * args->next_hop6 IPv6 next hop we are forwarding to (out).
1278 * args->f_id Addresses grabbed from the packet (out)
1279 * args->rule.info a cookie depending on rule action
1283 * IP_FW_PASS the packet must be accepted
1284 * IP_FW_DENY the packet must be dropped
1285 * IP_FW_DIVERT divert packet, port in m_tag
1286 * IP_FW_TEE tee packet, port in m_tag
1287 * IP_FW_DUMMYNET to dummynet, pipe in args->cookie
1288 * IP_FW_NETGRAPH into netgraph, cookie args->cookie
1289 * args->rule contains the matching rule,
1290 * args->rule.info has additional information.
1294 ipfw_chk(struct ip_fw_args *args)
1298 * Local variables holding state while processing a packet:
1300 * IMPORTANT NOTE: to speed up the processing of rules, there
1301 * are some assumption on the values of the variables, which
1302 * are documented here. Should you change them, please check
1303 * the implementation of the various instructions to make sure
1304 * that they still work.
1306 * m | args->m Pointer to the mbuf, as received from the caller.
1307 * It may change if ipfw_chk() does an m_pullup, or if it
1308 * consumes the packet because it calls send_reject().
1309 * XXX This has to change, so that ipfw_chk() never modifies
1310 * or consumes the buffer.
1312 * args->mem Pointer to contigous memory chunk.
1313 * ip Is the beginning of the ip(4 or 6) header.
1314 * eh Ethernet header in case if input is Layer2.
1318 struct ether_header *eh;
1321 * For rules which contain uid/gid or jail constraints, cache
1322 * a copy of the users credentials after the pcb lookup has been
1323 * executed. This will speed up the processing of rules with
1324 * these types of constraints, as well as decrease contention
1325 * on pcb related locks.
1328 struct bsd_ucred ucred_cache;
1330 struct ucred *ucred_cache = NULL;
1332 int ucred_lookup = 0;
1333 int f_pos = 0; /* index of current rule in the array */
1335 struct ifnet *oif, *iif;
1338 * hlen The length of the IP header.
1340 u_int hlen = 0; /* hlen >0 means we have an IP pkt */
1343 * offset The offset of a fragment. offset != 0 means that
1344 * we have a fragment at this offset of an IPv4 packet.
1345 * offset == 0 means that (if this is an IPv4 packet)
1346 * this is the first or only fragment.
1347 * For IPv6 offset|ip6f_mf == 0 means there is no Fragment Header
1348 * or there is a single packet fragment (fragment header added
1349 * without needed). We will treat a single packet fragment as if
1350 * there was no fragment header (or log/block depending on the
1351 * V_fw_permit_single_frag6 sysctl setting).
1354 u_short ip6f_mf = 0;
1357 * Local copies of addresses. They are only valid if we have
1360 * proto The protocol. Set to 0 for non-ip packets,
1361 * or to the protocol read from the packet otherwise.
1362 * proto != 0 means that we have an IPv4 packet.
1364 * src_port, dst_port port numbers, in HOST format. Only
1365 * valid for TCP and UDP packets.
1367 * src_ip, dst_ip ip addresses, in NETWORK format.
1368 * Only valid for IPv4 packets.
1371 uint16_t src_port, dst_port; /* NOTE: host format */
1372 struct in_addr src_ip, dst_ip; /* NOTE: network format */
1376 struct ipfw_dyn_info dyn_info;
1377 struct ip_fw *q = NULL;
1378 struct ip_fw_chain *chain = &V_layer3_chain;
1381 * We store in ulp a pointer to the upper layer protocol header.
1382 * In the ipv4 case this is easy to determine from the header,
1383 * but for ipv6 we might have some additional headers in the middle.
1384 * ulp is NULL if not found.
1386 void *ulp = NULL; /* upper layer protocol pointer. */
1388 /* XXX ipv6 variables */
1390 uint8_t icmp6_type = 0;
1391 uint16_t ext_hd = 0; /* bits vector for extension header filtering */
1392 /* end of ipv6 variables */
1396 int done = 0; /* flag to exit the outer loop */
1400 if ((mem = (args->flags & IPFW_ARGS_LENMASK))) {
1401 if (args->flags & IPFW_ARGS_ETHER) {
1402 eh = (struct ether_header *)args->mem;
1403 if (eh->ether_type == htons(ETHERTYPE_VLAN))
1405 ((struct ether_vlan_header *)eh + 1);
1407 ip = (struct ip *)(eh + 1);
1410 ip = (struct ip *)args->mem;
1412 pktlen = IPFW_ARGS_LENGTH(args->flags);
1413 args->f_id.fib = args->ifp->if_fib; /* best guess */
1416 if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready))
1417 return (IP_FW_PASS); /* accept */
1418 if (args->flags & IPFW_ARGS_ETHER) {
1419 /* We need some amount of data to be contiguous. */
1420 if (m->m_len < min(m->m_pkthdr.len, max_protohdr) &&
1421 (args->m = m = m_pullup(m, min(m->m_pkthdr.len,
1422 max_protohdr))) == NULL)
1424 eh = mtod(m, struct ether_header *);
1425 ip = (struct ip *)(eh + 1);
1428 ip = mtod(m, struct ip *);
1430 pktlen = m->m_pkthdr.len;
1431 args->f_id.fib = M_GETFIB(m); /* mbuf not altered */
1434 dst_ip.s_addr = 0; /* make sure it is initialized */
1435 src_ip.s_addr = 0; /* make sure it is initialized */
1436 src_port = dst_port = 0;
1438 DYN_INFO_INIT(&dyn_info);
1440 * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous,
1441 * then it sets p to point at the offset "len" in the mbuf. WARNING: the
1442 * pointer might become stale after other pullups (but we never use it
1445 #define PULLUP_TO(_len, p, T) PULLUP_LEN(_len, p, sizeof(T))
1446 #define EHLEN (eh != NULL ? ((char *)ip - (char *)eh) : 0)
1447 #define PULLUP_LEN(_len, p, T) \
1449 int x = (_len) + T + EHLEN; \
1451 MPASS(pktlen >= x); \
1452 p = (char *)args->mem + (_len) + EHLEN; \
1454 if (__predict_false((m)->m_len < x)) { \
1455 args->m = m = m_pullup(m, x); \
1457 goto pullup_failed; \
1459 p = mtod(m, char *) + (_len) + EHLEN; \
1463 * In case pointers got stale after pullups, update them.
1465 #define UPDATE_POINTERS() \
1469 eh = mtod(m, struct ether_header *); \
1470 ip = (struct ip *)(eh + 1); \
1472 ip = mtod(m, struct ip *); \
1477 /* Identify IP packets and fill up variables. */
1478 if (pktlen >= sizeof(struct ip6_hdr) &&
1479 (eh == NULL || eh->ether_type == htons(ETHERTYPE_IPV6)) &&
1481 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
1484 args->flags |= IPFW_ARGS_IP6;
1485 hlen = sizeof(struct ip6_hdr);
1486 proto = ip6->ip6_nxt;
1487 /* Search extension headers to find upper layer protocols */
1488 while (ulp == NULL && offset == 0) {
1490 case IPPROTO_ICMPV6:
1491 PULLUP_TO(hlen, ulp, struct icmp6_hdr);
1492 icmp6_type = ICMP6(ulp)->icmp6_type;
1496 PULLUP_TO(hlen, ulp, struct tcphdr);
1497 dst_port = TCP(ulp)->th_dport;
1498 src_port = TCP(ulp)->th_sport;
1499 /* save flags for dynamic rules */
1500 args->f_id._flags = TCP(ulp)->th_flags;
1504 if (pktlen >= hlen + sizeof(struct sctphdr) +
1505 sizeof(struct sctp_chunkhdr) +
1506 offsetof(struct sctp_init, a_rwnd))
1507 PULLUP_LEN(hlen, ulp,
1508 sizeof(struct sctphdr) +
1509 sizeof(struct sctp_chunkhdr) +
1510 offsetof(struct sctp_init, a_rwnd));
1511 else if (pktlen >= hlen + sizeof(struct sctphdr))
1512 PULLUP_LEN(hlen, ulp, pktlen - hlen);
1514 PULLUP_LEN(hlen, ulp,
1515 sizeof(struct sctphdr));
1516 src_port = SCTP(ulp)->src_port;
1517 dst_port = SCTP(ulp)->dest_port;
1521 case IPPROTO_UDPLITE:
1522 PULLUP_TO(hlen, ulp, struct udphdr);
1523 dst_port = UDP(ulp)->uh_dport;
1524 src_port = UDP(ulp)->uh_sport;
1527 case IPPROTO_HOPOPTS: /* RFC 2460 */
1528 PULLUP_TO(hlen, ulp, struct ip6_hbh);
1529 ext_hd |= EXT_HOPOPTS;
1530 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1531 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1535 case IPPROTO_ROUTING: /* RFC 2460 */
1536 PULLUP_TO(hlen, ulp, struct ip6_rthdr);
1537 switch (((struct ip6_rthdr *)ulp)->ip6r_type) {
1539 ext_hd |= EXT_RTHDR0;
1542 ext_hd |= EXT_RTHDR2;
1546 printf("IPFW2: IPV6 - Unknown "
1547 "Routing Header type(%d)\n",
1548 ((struct ip6_rthdr *)
1550 if (V_fw_deny_unknown_exthdrs)
1551 return (IP_FW_DENY);
1554 ext_hd |= EXT_ROUTING;
1555 hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3;
1556 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt;
1560 case IPPROTO_FRAGMENT: /* RFC 2460 */
1561 PULLUP_TO(hlen, ulp, struct ip6_frag);
1562 ext_hd |= EXT_FRAGMENT;
1563 hlen += sizeof (struct ip6_frag);
1564 proto = ((struct ip6_frag *)ulp)->ip6f_nxt;
1565 offset = ((struct ip6_frag *)ulp)->ip6f_offlg &
1567 ip6f_mf = ((struct ip6_frag *)ulp)->ip6f_offlg &
1569 if (V_fw_permit_single_frag6 == 0 &&
1570 offset == 0 && ip6f_mf == 0) {
1572 printf("IPFW2: IPV6 - Invalid "
1573 "Fragment Header\n");
1574 if (V_fw_deny_unknown_exthdrs)
1575 return (IP_FW_DENY);
1579 ntohl(((struct ip6_frag *)ulp)->ip6f_ident);
1583 case IPPROTO_DSTOPTS: /* RFC 2460 */
1584 PULLUP_TO(hlen, ulp, struct ip6_hbh);
1585 ext_hd |= EXT_DSTOPTS;
1586 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1587 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1591 case IPPROTO_AH: /* RFC 2402 */
1592 PULLUP_TO(hlen, ulp, struct ip6_ext);
1594 hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2;
1595 proto = ((struct ip6_ext *)ulp)->ip6e_nxt;
1599 case IPPROTO_ESP: /* RFC 2406 */
1600 PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */
1601 /* Anything past Seq# is variable length and
1602 * data past this ext. header is encrypted. */
1606 case IPPROTO_NONE: /* RFC 2460 */
1608 * Packet ends here, and IPv6 header has
1609 * already been pulled up. If ip6e_len!=0
1610 * then octets must be ignored.
1612 ulp = ip; /* non-NULL to get out of loop. */
1615 case IPPROTO_OSPFIGP:
1616 /* XXX OSPF header check? */
1617 PULLUP_TO(hlen, ulp, struct ip6_ext);
1621 /* XXX PIM header check? */
1622 PULLUP_TO(hlen, ulp, struct pim);
1625 case IPPROTO_GRE: /* RFC 1701 */
1626 /* XXX GRE header check? */
1627 PULLUP_TO(hlen, ulp, struct grehdr);
1631 PULLUP_TO(hlen, ulp, offsetof(
1632 struct carp_header, carp_counter));
1633 if (CARP_ADVERTISEMENT !=
1634 ((struct carp_header *)ulp)->carp_type)
1635 return (IP_FW_DENY);
1638 case IPPROTO_IPV6: /* RFC 2893 */
1639 PULLUP_TO(hlen, ulp, struct ip6_hdr);
1642 case IPPROTO_IPV4: /* RFC 2893 */
1643 PULLUP_TO(hlen, ulp, struct ip);
1648 printf("IPFW2: IPV6 - Unknown "
1649 "Extension Header(%d), ext_hd=%x\n",
1651 if (V_fw_deny_unknown_exthdrs)
1652 return (IP_FW_DENY);
1653 PULLUP_TO(hlen, ulp, struct ip6_ext);
1658 ip6 = (struct ip6_hdr *)ip;
1659 args->f_id.addr_type = 6;
1660 args->f_id.src_ip6 = ip6->ip6_src;
1661 args->f_id.dst_ip6 = ip6->ip6_dst;
1662 args->f_id.flow_id6 = ntohl(ip6->ip6_flow);
1663 iplen = ntohs(ip6->ip6_plen) + sizeof(*ip6);
1664 } else if (pktlen >= sizeof(struct ip) &&
1665 (eh == NULL || eh->ether_type == htons(ETHERTYPE_IP)) &&
1668 args->flags |= IPFW_ARGS_IP4;
1669 hlen = ip->ip_hl << 2;
1671 * Collect parameters into local variables for faster
1675 src_ip = ip->ip_src;
1676 dst_ip = ip->ip_dst;
1677 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1678 iplen = ntohs(ip->ip_len);
1683 PULLUP_TO(hlen, ulp, struct tcphdr);
1684 dst_port = TCP(ulp)->th_dport;
1685 src_port = TCP(ulp)->th_sport;
1686 /* save flags for dynamic rules */
1687 args->f_id._flags = TCP(ulp)->th_flags;
1691 if (pktlen >= hlen + sizeof(struct sctphdr) +
1692 sizeof(struct sctp_chunkhdr) +
1693 offsetof(struct sctp_init, a_rwnd))
1694 PULLUP_LEN(hlen, ulp,
1695 sizeof(struct sctphdr) +
1696 sizeof(struct sctp_chunkhdr) +
1697 offsetof(struct sctp_init, a_rwnd));
1698 else if (pktlen >= hlen + sizeof(struct sctphdr))
1699 PULLUP_LEN(hlen, ulp, pktlen - hlen);
1701 PULLUP_LEN(hlen, ulp,
1702 sizeof(struct sctphdr));
1703 src_port = SCTP(ulp)->src_port;
1704 dst_port = SCTP(ulp)->dest_port;
1708 case IPPROTO_UDPLITE:
1709 PULLUP_TO(hlen, ulp, struct udphdr);
1710 dst_port = UDP(ulp)->uh_dport;
1711 src_port = UDP(ulp)->uh_sport;
1715 PULLUP_TO(hlen, ulp, struct icmphdr);
1716 //args->f_id.flags = ICMP(ulp)->icmp_type;
1723 if (offset == 1 && proto == IPPROTO_TCP) {
1730 args->f_id.addr_type = 4;
1731 args->f_id.src_ip = ntohl(src_ip.s_addr);
1732 args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1735 dst_ip.s_addr = src_ip.s_addr = 0;
1737 args->f_id.addr_type = 1; /* XXX */
1740 pktlen = iplen < pktlen ? iplen: pktlen;
1742 /* Properly initialize the rest of f_id */
1743 args->f_id.proto = proto;
1744 args->f_id.src_port = src_port = ntohs(src_port);
1745 args->f_id.dst_port = dst_port = ntohs(dst_port);
1747 IPFW_PF_RLOCK(chain);
1748 if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */
1749 IPFW_PF_RUNLOCK(chain);
1750 return (IP_FW_PASS); /* accept */
1752 if (args->flags & IPFW_ARGS_REF) {
1754 * Packet has already been tagged as a result of a previous
1755 * match on rule args->rule aka args->rule_id (PIPE, QUEUE,
1756 * REASS, NETGRAPH, DIVERT/TEE...)
1757 * Validate the slot and continue from the next one
1758 * if still present, otherwise do a lookup.
1760 f_pos = (args->rule.chain_id == chain->id) ?
1762 ipfw_find_rule(chain, args->rule.rulenum,
1763 args->rule.rule_id);
1768 if (args->flags & IPFW_ARGS_IN) {
1772 MPASS(args->flags & IPFW_ARGS_OUT);
1773 iif = mem ? NULL : m_rcvif(m);
1778 * Now scan the rules, and parse microinstructions for each rule.
1779 * We have two nested loops and an inner switch. Sometimes we
1780 * need to break out of one or both loops, or re-enter one of
1781 * the loops with updated variables. Loop variables are:
1783 * f_pos (outer loop) points to the current rule.
1784 * On output it points to the matching rule.
1785 * done (outer loop) is used as a flag to break the loop.
1786 * l (inner loop) residual length of current rule.
1787 * cmd points to the current microinstruction.
1789 * We break the inner loop by setting l=0 and possibly
1790 * cmdlen=0 if we don't want to advance cmd.
1791 * We break the outer loop by setting done=1
1792 * We can restart the inner loop by setting l>0 and f_pos, f, cmd
1795 for (; f_pos < chain->n_rules; f_pos++) {
1797 uint32_t tablearg = 0;
1798 int l, cmdlen, skip_or; /* skip rest of OR block */
1801 f = chain->map[f_pos];
1802 if (V_set_disable & (1 << f->set) )
1806 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
1807 l -= cmdlen, cmd += cmdlen) {
1811 * check_body is a jump target used when we find a
1812 * CHECK_STATE, and need to jump to the body of
1817 cmdlen = F_LEN(cmd);
1819 * An OR block (insn_1 || .. || insn_n) has the
1820 * F_OR bit set in all but the last instruction.
1821 * The first match will set "skip_or", and cause
1822 * the following instructions to be skipped until
1823 * past the one with the F_OR bit clear.
1825 if (skip_or) { /* skip this instruction */
1826 if ((cmd->len & F_OR) == 0)
1827 skip_or = 0; /* next one is good */
1830 match = 0; /* set to 1 if we succeed */
1832 switch (cmd->opcode) {
1834 * The first set of opcodes compares the packet's
1835 * fields with some pattern, setting 'match' if a
1836 * match is found. At the end of the loop there is
1837 * logic to deal with F_NOT and F_OR flags associated
1845 printf("ipfw: opcode %d unimplemented\n",
1853 * We only check offset == 0 && proto != 0,
1854 * as this ensures that we have a
1855 * packet with the ports info.
1859 if (proto == IPPROTO_TCP ||
1860 proto == IPPROTO_UDP ||
1861 proto == IPPROTO_UDPLITE)
1862 match = check_uidgid(
1863 (ipfw_insn_u32 *)cmd,
1864 args, &ucred_lookup,
1868 (void *)&ucred_cache);
1873 match = iface_match(iif, (ipfw_insn_if *)cmd,
1878 match = iface_match(oif, (ipfw_insn_if *)cmd,
1883 match = iface_match(args->ifp,
1884 (ipfw_insn_if *)cmd, chain, &tablearg);
1888 if (args->flags & IPFW_ARGS_ETHER) {
1889 u_int32_t *want = (u_int32_t *)
1890 ((ipfw_insn_mac *)cmd)->addr;
1891 u_int32_t *mask = (u_int32_t *)
1892 ((ipfw_insn_mac *)cmd)->mask;
1893 u_int32_t *hdr = (u_int32_t *)eh;
1896 ( want[0] == (hdr[0] & mask[0]) &&
1897 want[1] == (hdr[1] & mask[1]) &&
1898 want[2] == (hdr[2] & mask[2]) );
1903 if (args->flags & IPFW_ARGS_ETHER) {
1905 ((ipfw_insn_u16 *)cmd)->ports;
1908 for (i = cmdlen - 1; !match && i>0;
1911 (ntohs(eh->ether_type) >=
1913 ntohs(eh->ether_type) <=
1919 match = (offset != 0);
1922 case O_IN: /* "out" is "not in" */
1923 match = (oif == NULL);
1927 match = (args->flags & IPFW_ARGS_ETHER);
1931 if ((args->flags & IPFW_ARGS_REF) == 0)
1934 * For diverted packets, args->rule.info
1935 * contains the divert port (in host format)
1936 * reason and direction.
1938 match = ((args->rule.info & IPFW_IS_MASK) ==
1939 IPFW_IS_DIVERT) && (
1940 ((args->rule.info & IPFW_INFO_IN) ?
1946 * We do not allow an arg of 0 so the
1947 * check of "proto" only suffices.
1949 match = (proto == cmd->arg1);
1954 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
1958 case O_IP_DST_LOOKUP:
1964 if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) {
1965 /* Determine lookup key type */
1966 vidx = ((ipfw_insn_u32 *)cmd)->d[1];
1967 if (vidx != 4 /* uid */ &&
1968 vidx != 5 /* jail */ &&
1969 is_ipv6 == 0 && is_ipv4 == 0)
1971 /* Determine key length */
1972 if (vidx == 0 /* dst-ip */ ||
1973 vidx == 1 /* src-ip */)
1975 sizeof(struct in6_addr):
1978 keylen = sizeof(key);
1981 if (vidx == 0 /* dst-ip */)
1982 pkey = is_ipv4 ? (void *)&dst_ip:
1983 (void *)&args->f_id.dst_ip6;
1984 else if (vidx == 1 /* src-ip */)
1985 pkey = is_ipv4 ? (void *)&src_ip:
1986 (void *)&args->f_id.src_ip6;
1987 else if (vidx == 6 /* dscp */) {
1989 key = ip->ip_tos >> 2;
1991 key = args->f_id.flow_id6;
1992 key = (key & 0x0f) << 2 |
1993 (key & 0xf000) >> 14;
1996 } else if (vidx == 2 /* dst-port */ ||
1997 vidx == 3 /* src-port */) {
1998 /* Skip fragments */
2001 /* Skip proto without ports */
2002 if (proto != IPPROTO_TCP &&
2003 proto != IPPROTO_UDP &&
2004 proto != IPPROTO_UDPLITE &&
2005 proto != IPPROTO_SCTP)
2007 if (vidx == 2 /* dst-port */)
2013 else if (vidx == 4 /* uid */ ||
2014 vidx == 5 /* jail */) {
2016 (ipfw_insn_u32 *)cmd,
2017 args, &ucred_lookup,
2020 if (vidx == 4 /* uid */)
2021 key = ucred_cache->cr_uid;
2022 else if (vidx == 5 /* jail */)
2023 key = ucred_cache->cr_prison->pr_id;
2024 #else /* !__FreeBSD__ */
2025 (void *)&ucred_cache);
2026 if (vidx == 4 /* uid */)
2027 key = ucred_cache.uid;
2028 else if (vidx == 5 /* jail */)
2029 key = ucred_cache.xid;
2030 #endif /* !__FreeBSD__ */
2032 #endif /* !USERSPACE */
2035 match = ipfw_lookup_table(chain,
2036 cmd->arg1, keylen, pkey, &vidx);
2042 /* cmdlen =< F_INSN_SIZE(ipfw_insn_u32) */
2045 case O_IP_SRC_LOOKUP:
2052 keylen = sizeof(in_addr_t);
2053 if (cmd->opcode == O_IP_DST_LOOKUP)
2057 } else if (is_ipv6) {
2058 keylen = sizeof(struct in6_addr);
2059 if (cmd->opcode == O_IP_DST_LOOKUP)
2060 pkey = &args->f_id.dst_ip6;
2062 pkey = &args->f_id.src_ip6;
2065 match = ipfw_lookup_table(chain, cmd->arg1,
2066 keylen, pkey, &vidx);
2069 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) {
2070 match = ((ipfw_insn_u32 *)cmd)->d[0] ==
2071 TARG_VAL(chain, vidx, tag);
2079 case O_IP_FLOW_LOOKUP:
2082 match = ipfw_lookup_table(chain,
2083 cmd->arg1, 0, &args->f_id, &v);
2084 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
2085 match = ((ipfw_insn_u32 *)cmd)->d[0] ==
2086 TARG_VAL(chain, v, tag);
2095 (cmd->opcode == O_IP_DST_MASK) ?
2096 dst_ip.s_addr : src_ip.s_addr;
2097 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d;
2100 for (; !match && i>0; i-= 2, p+= 2)
2101 match = (p[0] == (a & p[1]));
2107 match = in_localip(src_ip);
2114 ipfw_localip6(&args->f_id.src_ip6);
2121 u_int32_t *d = (u_int32_t *)(cmd+1);
2123 cmd->opcode == O_IP_DST_SET ?
2129 addr -= d[0]; /* subtract base */
2130 match = (addr < cmd->arg1) &&
2131 ( d[ 1 + (addr>>5)] &
2132 (1<<(addr & 0x1f)) );
2138 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
2144 match = in_localip(dst_ip);
2151 ipfw_localip6(&args->f_id.dst_ip6);
2159 * offset == 0 && proto != 0 is enough
2160 * to guarantee that we have a
2161 * packet with port info.
2163 if ((proto == IPPROTO_UDP ||
2164 proto == IPPROTO_UDPLITE ||
2165 proto == IPPROTO_TCP ||
2166 proto == IPPROTO_SCTP) && offset == 0) {
2168 (cmd->opcode == O_IP_SRCPORT) ?
2169 src_port : dst_port ;
2171 ((ipfw_insn_u16 *)cmd)->ports;
2174 for (i = cmdlen - 1; !match && i>0;
2176 match = (x>=p[0] && x<=p[1]);
2181 match = (offset == 0 && proto==IPPROTO_ICMP &&
2182 icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) );
2187 match = is_ipv6 && offset == 0 &&
2188 proto==IPPROTO_ICMPV6 &&
2190 ICMP6(ulp)->icmp6_type,
2191 (ipfw_insn_u32 *)cmd);
2197 ipopts_match(ip, cmd) );
2202 cmd->arg1 == ip->ip_v);
2210 { /* only for IP packets */
2215 if (cmd->opcode == O_IPLEN)
2217 else if (cmd->opcode == O_IPTTL)
2219 else /* must be IPID */
2220 x = ntohs(ip->ip_id);
2222 match = (cmd->arg1 == x);
2225 /* otherwise we have ranges */
2226 p = ((ipfw_insn_u16 *)cmd)->ports;
2228 for (; !match && i>0; i--, p += 2)
2229 match = (x >= p[0] && x <= p[1]);
2233 case O_IPPRECEDENCE:
2235 (cmd->arg1 == (ip->ip_tos & 0xe0)) );
2240 flags_match(cmd, ip->ip_tos));
2248 p = ((ipfw_insn_u32 *)cmd)->d;
2251 x = ip->ip_tos >> 2;
2254 v = &((struct ip6_hdr *)ip)->ip6_vfc;
2255 x = (*v & 0x0F) << 2;
2261 /* DSCP bitmask is stored as low_u32 high_u32 */
2263 match = *(p + 1) & (1 << (x - 32));
2265 match = *p & (1 << x);
2270 if (proto == IPPROTO_TCP && offset == 0) {
2277 struct ip6_hdr *ip6;
2279 ip6 = (struct ip6_hdr *)ip;
2280 if (ip6->ip6_plen == 0) {
2282 * Jumbo payload is not
2291 x = iplen - (ip->ip_hl << 2);
2293 x -= tcp->th_off << 2;
2295 match = (cmd->arg1 == x);
2298 /* otherwise we have ranges */
2299 p = ((ipfw_insn_u16 *)cmd)->ports;
2301 for (; !match && i>0; i--, p += 2)
2302 match = (x >= p[0] && x <= p[1]);
2307 match = (proto == IPPROTO_TCP && offset == 0 &&
2308 flags_match(cmd, TCP(ulp)->th_flags));
2312 if (proto == IPPROTO_TCP && offset == 0 && ulp){
2313 PULLUP_LEN(hlen, ulp,
2314 (TCP(ulp)->th_off << 2));
2315 match = tcpopts_match(TCP(ulp), cmd);
2320 match = (proto == IPPROTO_TCP && offset == 0 &&
2321 ((ipfw_insn_u32 *)cmd)->d[0] ==
2326 match = (proto == IPPROTO_TCP && offset == 0 &&
2327 ((ipfw_insn_u32 *)cmd)->d[0] ==
2332 if (proto == IPPROTO_TCP &&
2333 (args->f_id._flags & TH_SYN) != 0 &&
2338 PULLUP_LEN(hlen, ulp,
2339 (TCP(ulp)->th_off << 2));
2340 if ((tcpopts_parse(TCP(ulp), &mss) &
2341 IP_FW_TCPOPT_MSS) == 0)
2344 match = (cmd->arg1 == mss);
2347 /* Otherwise we have ranges. */
2348 p = ((ipfw_insn_u16 *)cmd)->ports;
2350 for (; !match && i > 0; i--, p += 2)
2351 match = (mss >= p[0] &&
2357 if (proto == IPPROTO_TCP && offset == 0) {
2362 x = ntohs(TCP(ulp)->th_win);
2364 match = (cmd->arg1 == x);
2367 /* Otherwise we have ranges. */
2368 p = ((ipfw_insn_u16 *)cmd)->ports;
2370 for (; !match && i > 0; i--, p += 2)
2371 match = (x >= p[0] && x <= p[1]);
2376 /* reject packets which have SYN only */
2377 /* XXX should i also check for TH_ACK ? */
2378 match = (proto == IPPROTO_TCP && offset == 0 &&
2379 (TCP(ulp)->th_flags &
2380 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
2386 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
2389 * ALTQ uses mbuf tags from another
2390 * packet filtering system - pf(4).
2391 * We allocate a tag in its format
2392 * and fill it in, pretending to be pf(4).
2395 at = pf_find_mtag(m);
2396 if (at != NULL && at->qid != 0)
2398 mtag = m_tag_get(PACKET_TAG_PF,
2399 sizeof(struct pf_mtag), M_NOWAIT | M_ZERO);
2402 * Let the packet fall back to the
2407 m_tag_prepend(m, mtag);
2408 at = (struct pf_mtag *)(mtag + 1);
2409 at->qid = altq->qid;
2415 ipfw_log(chain, f, hlen, args,
2416 offset | ip6f_mf, tablearg, ip);
2421 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]);
2425 /* Outgoing packets automatically pass/match */
2426 match = (args->flags & IPFW_ARGS_OUT ||
2430 verify_path6(&(args->f_id.src_ip6),
2431 iif, args->f_id.fib) :
2433 verify_path(src_ip, iif, args->f_id.fib)));
2437 /* Outgoing packets automatically pass/match */
2438 match = (hlen > 0 && ((oif != NULL) || (
2441 verify_path6(&(args->f_id.src_ip6),
2442 NULL, args->f_id.fib) :
2444 verify_path(src_ip, NULL, args->f_id.fib))));
2448 /* Outgoing packets automatically pass/match */
2449 if (oif == NULL && hlen > 0 &&
2450 ( (is_ipv4 && in_localaddr(src_ip))
2453 in6_localaddr(&(args->f_id.src_ip6)))
2458 is_ipv6 ? verify_path6(
2459 &(args->f_id.src_ip6), iif,
2462 verify_path(src_ip, iif,
2469 match = (m_tag_find(m,
2470 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL);
2471 /* otherwise no match */
2477 IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6,
2478 &((ipfw_insn_ip6 *)cmd)->addr6);
2483 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6,
2484 &((ipfw_insn_ip6 *)cmd)->addr6);
2486 case O_IP6_SRC_MASK:
2487 case O_IP6_DST_MASK:
2491 struct in6_addr *d =
2492 &((ipfw_insn_ip6 *)cmd)->addr6;
2494 for (; !match && i > 0; d += 2,
2495 i -= F_INSN_SIZE(struct in6_addr)
2501 APPLY_MASK(&p, &d[1]);
2503 IN6_ARE_ADDR_EQUAL(&d[0],
2511 flow6id_match(args->f_id.flow_id6,
2512 (ipfw_insn_u32 *) cmd);
2517 (ext_hd & ((ipfw_insn *) cmd)->arg1);
2531 uint32_t tag = TARG(cmd->arg1, tag);
2533 /* Packet is already tagged with this tag? */
2534 mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL);
2536 /* We have `untag' action when F_NOT flag is
2537 * present. And we must remove this mtag from
2538 * mbuf and reset `match' to zero (`match' will
2539 * be inversed later).
2540 * Otherwise we should allocate new mtag and
2541 * push it into mbuf.
2543 if (cmd->len & F_NOT) { /* `untag' action */
2545 m_tag_delete(m, mtag);
2549 mtag = m_tag_alloc( MTAG_IPFW,
2552 m_tag_prepend(m, mtag);
2559 case O_FIB: /* try match the specified fib */
2560 if (args->f_id.fib == cmd->arg1)
2565 #ifndef USERSPACE /* not supported in userspace */
2566 struct inpcb *inp = args->inp;
2567 struct inpcbinfo *pi;
2569 if (is_ipv6) /* XXX can we remove this ? */
2572 if (proto == IPPROTO_TCP)
2574 else if (proto == IPPROTO_UDP)
2576 else if (proto == IPPROTO_UDPLITE)
2577 pi = &V_ulitecbinfo;
2582 * XXXRW: so_user_cookie should almost
2583 * certainly be inp_user_cookie?
2586 /* For incoming packet, lookup up the
2587 inpcb using the src/dest ip/port tuple */
2589 inp = in_pcblookup(pi,
2590 src_ip, htons(src_port),
2591 dst_ip, htons(dst_port),
2592 INPLOOKUP_RLOCKPCB, NULL);
2595 inp->inp_socket->so_user_cookie;
2601 if (inp->inp_socket) {
2603 inp->inp_socket->so_user_cookie;
2608 #endif /* !USERSPACE */
2614 uint32_t tag = TARG(cmd->arg1, tag);
2617 match = m_tag_locate(m, MTAG_IPFW,
2622 /* we have ranges */
2623 for (mtag = m_tag_first(m);
2624 mtag != NULL && !match;
2625 mtag = m_tag_next(m, mtag)) {
2629 if (mtag->m_tag_cookie != MTAG_IPFW)
2632 p = ((ipfw_insn_u16 *)cmd)->ports;
2634 for(; !match && i > 0; i--, p += 2)
2636 mtag->m_tag_id >= p[0] &&
2637 mtag->m_tag_id <= p[1];
2643 * The second set of opcodes represents 'actions',
2644 * i.e. the terminal part of a rule once the packet
2645 * matches all previous patterns.
2646 * Typically there is only one action for each rule,
2647 * and the opcode is stored at the end of the rule
2648 * (but there are exceptions -- see below).
2650 * In general, here we set retval and terminate the
2651 * outer loop (would be a 'break 3' in some language,
2652 * but we need to set l=0, done=1)
2655 * O_COUNT and O_SKIPTO actions:
2656 * instead of terminating, we jump to the next rule
2657 * (setting l=0), or to the SKIPTO target (setting
2658 * f/f_len, cmd and l as needed), respectively.
2660 * O_TAG, O_LOG and O_ALTQ action parameters:
2661 * perform some action and set match = 1;
2663 * O_LIMIT and O_KEEP_STATE: these opcodes are
2664 * not real 'actions', and are stored right
2665 * before the 'action' part of the rule (one
2666 * exception is O_SKIP_ACTION which could be
2667 * between these opcodes and 'action' one).
2668 * These opcodes try to install an entry in the
2669 * state tables; if successful, we continue with
2670 * the next opcode (match=1; break;), otherwise
2671 * the packet must be dropped (set retval,
2672 * break loops with l=0, done=1)
2674 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2675 * cause a lookup of the state table, and a jump
2676 * to the 'action' part of the parent rule
2677 * if an entry is found, or
2678 * (CHECK_STATE only) a jump to the next rule if
2679 * the entry is not found.
2680 * The result of the lookup is cached so that
2681 * further instances of these opcodes become NOPs.
2682 * The jump to the next rule is done by setting
2685 * O_SKIP_ACTION: this opcode is not a real 'action'
2686 * either, and is stored right before the 'action'
2687 * part of the rule, right after the O_KEEP_STATE
2688 * opcode. It causes match failure so the real
2689 * 'action' could be executed only if the rule
2690 * is checked via dynamic rule from the state
2691 * table, as in such case execution starts
2692 * from the true 'action' opcode directly.
2697 if (ipfw_dyn_install_state(chain, f,
2698 (ipfw_insn_limit *)cmd, args, ulp,
2699 pktlen, &dyn_info, tablearg)) {
2700 /* error or limit violation */
2701 retval = IP_FW_DENY;
2702 l = 0; /* exit inner loop */
2703 done = 1; /* exit outer loop */
2711 * dynamic rules are checked at the first
2712 * keep-state or check-state occurrence,
2713 * with the result being stored in dyn_info.
2714 * The compiler introduces a PROBE_STATE
2715 * instruction for us when we have a
2716 * KEEP_STATE (because PROBE_STATE needs
2719 if (DYN_LOOKUP_NEEDED(&dyn_info, cmd) &&
2720 (q = ipfw_dyn_lookup_state(args, ulp,
2721 pktlen, cmd, &dyn_info)) != NULL) {
2723 * Found dynamic entry, jump to the
2724 * 'action' part of the parent rule
2725 * by setting f, cmd, l and clearing
2729 f_pos = dyn_info.f_pos;
2730 cmd = ACTION_PTR(f);
2731 l = f->cmd_len - f->act_ofs;
2737 * Dynamic entry not found. If CHECK_STATE,
2738 * skip to next rule, if PROBE_STATE just
2739 * ignore and continue with next opcode.
2741 if (cmd->opcode == O_CHECK_STATE)
2742 l = 0; /* exit inner loop */
2747 match = 0; /* skip to the next rule */
2748 l = 0; /* exit inner loop */
2752 retval = 0; /* accept */
2753 l = 0; /* exit inner loop */
2754 done = 1; /* exit outer loop */
2759 set_match(args, f_pos, chain);
2760 args->rule.info = TARG(cmd->arg1, pipe);
2761 if (cmd->opcode == O_PIPE)
2762 args->rule.info |= IPFW_IS_PIPE;
2764 args->rule.info |= IPFW_ONEPASS;
2765 retval = IP_FW_DUMMYNET;
2766 l = 0; /* exit inner loop */
2767 done = 1; /* exit outer loop */
2772 if (args->flags & IPFW_ARGS_ETHER)
2773 break; /* not on layer 2 */
2774 /* otherwise this is terminal */
2775 l = 0; /* exit inner loop */
2776 done = 1; /* exit outer loop */
2777 retval = (cmd->opcode == O_DIVERT) ?
2778 IP_FW_DIVERT : IP_FW_TEE;
2779 set_match(args, f_pos, chain);
2780 args->rule.info = TARG(cmd->arg1, divert);
2784 IPFW_INC_RULE_COUNTER(f, pktlen);
2785 l = 0; /* exit inner loop */
2789 IPFW_INC_RULE_COUNTER(f, pktlen);
2790 f_pos = JUMP(chain, f, cmd->arg1, tablearg, 0);
2792 * Skip disabled rules, and re-enter
2793 * the inner loop with the correct
2794 * f_pos, f, l and cmd.
2795 * Also clear cmdlen and skip_or
2797 for (; f_pos < chain->n_rules - 1 &&
2799 (1 << chain->map[f_pos]->set));
2802 /* Re-enter the inner loop at the skipto rule. */
2803 f = chain->map[f_pos];
2810 break; /* not reached */
2812 case O_CALLRETURN: {
2814 * Implementation of `subroutine' call/return,
2815 * in the stack carried in an mbuf tag. This
2816 * is different from `skipto' in that any call
2817 * address is possible (`skipto' must prevent
2818 * backward jumps to avoid endless loops).
2819 * We have `return' action when F_NOT flag is
2820 * present. The `m_tag_id' field is used as
2824 uint16_t jmpto, *stack;
2826 #define IS_CALL ((cmd->len & F_NOT) == 0)
2827 #define IS_RETURN ((cmd->len & F_NOT) != 0)
2829 * Hand-rolled version of m_tag_locate() with
2831 * If not already tagged, allocate new tag.
2833 mtag = m_tag_first(m);
2834 while (mtag != NULL) {
2835 if (mtag->m_tag_cookie ==
2838 mtag = m_tag_next(m, mtag);
2840 if (mtag == NULL && IS_CALL) {
2841 mtag = m_tag_alloc(MTAG_IPFW_CALL, 0,
2842 IPFW_CALLSTACK_SIZE *
2843 sizeof(uint16_t), M_NOWAIT);
2845 m_tag_prepend(m, mtag);
2849 * On error both `call' and `return' just
2850 * continue with next rule.
2852 if (IS_RETURN && (mtag == NULL ||
2853 mtag->m_tag_id == 0)) {
2854 l = 0; /* exit inner loop */
2857 if (IS_CALL && (mtag == NULL ||
2858 mtag->m_tag_id >= IPFW_CALLSTACK_SIZE)) {
2859 printf("ipfw: call stack error, "
2860 "go to next rule\n");
2861 l = 0; /* exit inner loop */
2865 IPFW_INC_RULE_COUNTER(f, pktlen);
2866 stack = (uint16_t *)(mtag + 1);
2869 * The `call' action may use cached f_pos
2870 * (in f->next_rule), whose version is written
2872 * The `return' action, however, doesn't have
2873 * fixed jump address in cmd->arg1 and can't use
2877 stack[mtag->m_tag_id] = f->rulenum;
2879 f_pos = JUMP(chain, f, cmd->arg1,
2881 } else { /* `return' action */
2883 jmpto = stack[mtag->m_tag_id] + 1;
2884 f_pos = ipfw_find_rule(chain, jmpto, 0);
2888 * Skip disabled rules, and re-enter
2889 * the inner loop with the correct
2890 * f_pos, f, l and cmd.
2891 * Also clear cmdlen and skip_or
2893 for (; f_pos < chain->n_rules - 1 &&
2895 (1 << chain->map[f_pos]->set)); f_pos++)
2897 /* Re-enter the inner loop at the dest rule. */
2898 f = chain->map[f_pos];
2904 break; /* NOTREACHED */
2911 * Drop the packet and send a reject notice
2912 * if the packet is not ICMP (or is an ICMP
2913 * query), and it is not multicast/broadcast.
2915 if (hlen > 0 && is_ipv4 && offset == 0 &&
2916 (proto != IPPROTO_ICMP ||
2917 is_icmp_query(ICMP(ulp))) &&
2918 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2919 !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2920 send_reject(args, cmd->arg1, iplen, ip);
2926 if (hlen > 0 && is_ipv6 &&
2927 ((offset & IP6F_OFF_MASK) == 0) &&
2928 (proto != IPPROTO_ICMPV6 ||
2929 (is_icmp6_query(icmp6_type) == 1)) &&
2930 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2931 !IN6_IS_ADDR_MULTICAST(
2932 &args->f_id.dst_ip6)) {
2934 cmd->opcode == O_REJECT ?
2935 map_icmp_unreach(cmd->arg1):
2937 (struct ip6_hdr *)ip);
2943 retval = IP_FW_DENY;
2944 l = 0; /* exit inner loop */
2945 done = 1; /* exit outer loop */
2949 if (args->flags & IPFW_ARGS_ETHER)
2950 break; /* not valid on layer2 pkts */
2952 dyn_info.direction == MATCH_FORWARD) {
2953 struct sockaddr_in *sa;
2955 sa = &(((ipfw_insn_sa *)cmd)->sa);
2956 if (sa->sin_addr.s_addr == INADDR_ANY) {
2959 * We use O_FORWARD_IP opcode for
2960 * fwd rule with tablearg, but tables
2961 * now support IPv6 addresses. And
2962 * when we are inspecting IPv6 packet,
2963 * we can use nh6 field from
2964 * table_value as next_hop6 address.
2967 struct ip_fw_nh6 *nh6;
2969 args->flags |= IPFW_ARGS_NH6;
2970 nh6 = &args->hopstore6;
2971 nh6->sin6_addr = TARG_VAL(
2972 chain, tablearg, nh6);
2973 nh6->sin6_port = sa->sin_port;
2974 nh6->sin6_scope_id = TARG_VAL(
2975 chain, tablearg, zoneid);
2979 args->flags |= IPFW_ARGS_NH4;
2980 args->hopstore.sin_port =
2982 sa = &args->hopstore;
2983 sa->sin_family = AF_INET;
2984 sa->sin_len = sizeof(*sa);
2985 sa->sin_addr.s_addr = htonl(
2986 TARG_VAL(chain, tablearg,
2990 args->flags |= IPFW_ARGS_NH4PTR;
2991 args->next_hop = sa;
2994 retval = IP_FW_PASS;
2995 l = 0; /* exit inner loop */
2996 done = 1; /* exit outer loop */
3001 if (args->flags & IPFW_ARGS_ETHER)
3002 break; /* not valid on layer2 pkts */
3004 dyn_info.direction == MATCH_FORWARD) {
3005 struct sockaddr_in6 *sin6;
3007 sin6 = &(((ipfw_insn_sa6 *)cmd)->sa);
3008 args->flags |= IPFW_ARGS_NH6PTR;
3009 args->next_hop6 = sin6;
3011 retval = IP_FW_PASS;
3012 l = 0; /* exit inner loop */
3013 done = 1; /* exit outer loop */
3019 set_match(args, f_pos, chain);
3020 args->rule.info = TARG(cmd->arg1, netgraph);
3022 args->rule.info |= IPFW_ONEPASS;
3023 retval = (cmd->opcode == O_NETGRAPH) ?
3024 IP_FW_NETGRAPH : IP_FW_NGTEE;
3025 l = 0; /* exit inner loop */
3026 done = 1; /* exit outer loop */
3032 IPFW_INC_RULE_COUNTER(f, pktlen);
3033 fib = TARG(cmd->arg1, fib) & 0x7FFF;
3034 if (fib >= rt_numfibs)
3037 args->f_id.fib = fib; /* XXX */
3038 l = 0; /* exit inner loop */
3045 code = TARG(cmd->arg1, dscp) & 0x3F;
3046 l = 0; /* exit inner loop */
3050 old = *(uint16_t *)ip;
3051 ip->ip_tos = (code << 2) |
3052 (ip->ip_tos & 0x03);
3053 ip->ip_sum = cksum_adjust(ip->ip_sum,
3054 old, *(uint16_t *)ip);
3055 } else if (is_ipv6) {
3058 v = &((struct ip6_hdr *)ip)->ip6_vfc;
3059 *v = (*v & 0xF0) | (code >> 2);
3061 *v = (*v & 0x3F) | ((code & 0x03) << 6);
3065 IPFW_INC_RULE_COUNTER(f, pktlen);
3070 l = 0; /* exit inner loop */
3071 done = 1; /* exit outer loop */
3073 * Ensure that we do not invoke NAT handler for
3074 * non IPv4 packets. Libalias expects only IPv4.
3076 if (!is_ipv4 || !IPFW_NAT_LOADED) {
3077 retval = IP_FW_DENY;
3084 args->rule.info = 0;
3085 set_match(args, f_pos, chain);
3086 /* Check if this is 'global' nat rule */
3087 if (cmd->arg1 == IP_FW_NAT44_GLOBAL) {
3088 retval = ipfw_nat_ptr(args, NULL, m);
3091 t = ((ipfw_insn_nat *)cmd)->nat;
3093 nat_id = TARG(cmd->arg1, nat);
3094 t = (*lookup_nat_ptr)(&chain->nat, nat_id);
3097 retval = IP_FW_DENY;
3100 if (cmd->arg1 != IP_FW_TARG)
3101 ((ipfw_insn_nat *)cmd)->nat = t;
3103 retval = ipfw_nat_ptr(args, t, m);
3109 l = 0; /* in any case exit inner loop */
3110 if (is_ipv6) /* IPv6 is not supported yet */
3112 IPFW_INC_RULE_COUNTER(f, pktlen);
3113 ip_off = ntohs(ip->ip_off);
3115 /* if not fragmented, go to next rule */
3116 if ((ip_off & (IP_MF | IP_OFFMASK)) == 0)
3119 args->m = m = ip_reass(m);
3122 * do IP header checksum fixup.
3124 if (m == NULL) { /* fragment got swallowed */
3125 retval = IP_FW_DENY;
3126 } else { /* good, packet complete */
3129 ip = mtod(m, struct ip *);
3130 hlen = ip->ip_hl << 2;
3132 if (hlen == sizeof(struct ip))
3133 ip->ip_sum = in_cksum_hdr(ip);
3135 ip->ip_sum = in_cksum(m, hlen);
3136 retval = IP_FW_REASS;
3137 args->rule.info = 0;
3138 set_match(args, f_pos, chain);
3140 done = 1; /* exit outer loop */
3143 case O_EXTERNAL_ACTION:
3144 l = 0; /* in any case exit inner loop */
3145 retval = ipfw_run_eaction(chain, args,
3148 * If both @retval and @done are zero,
3149 * consider this as rule matching and
3152 if (retval == 0 && done == 0) {
3153 IPFW_INC_RULE_COUNTER(f, pktlen);
3155 * Reset the result of the last
3156 * dynamic state lookup.
3157 * External action can change
3158 * @args content, and it may be
3159 * used for new state lookup later.
3161 DYN_INFO_INIT(&dyn_info);
3166 panic("-- unknown opcode %d\n", cmd->opcode);
3167 } /* end of switch() on opcodes */
3169 * if we get here with l=0, then match is irrelevant.
3172 if (cmd->len & F_NOT)
3176 if (cmd->len & F_OR)
3179 if (!(cmd->len & F_OR)) /* not an OR block, */
3180 break; /* try next rule */
3183 } /* end of inner loop, scan opcodes */
3189 /* next_rule:; */ /* try next rule */
3191 } /* end of outer for, scan rules */
3194 struct ip_fw *rule = chain->map[f_pos];
3195 /* Update statistics */
3196 IPFW_INC_RULE_COUNTER(rule, pktlen);
3198 retval = IP_FW_DENY;
3199 printf("ipfw: ouch!, skip past end of rules, denying packet\n");
3201 IPFW_PF_RUNLOCK(chain);
3203 if (ucred_cache != NULL)
3204 crfree(ucred_cache);
3210 printf("ipfw: pullup failed\n");
3211 return (IP_FW_DENY);
3215 * Set maximum number of tables that can be used in given VNET ipfw instance.
3219 sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS)
3222 unsigned int ntables;
3224 ntables = V_fw_tables_max;
3226 error = sysctl_handle_int(oidp, &ntables, 0, req);
3227 /* Read operation or some error */
3228 if ((error != 0) || (req->newptr == NULL))
3231 return (ipfw_resize_tables(&V_layer3_chain, ntables));
3235 * Switches table namespace between global and per-set.
3238 sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS)
3243 sets = V_fw_tables_sets;
3245 error = sysctl_handle_int(oidp, &sets, 0, req);
3246 /* Read operation or some error */
3247 if ((error != 0) || (req->newptr == NULL))
3250 return (ipfw_switch_tables_namespace(&V_layer3_chain, sets));
3255 * Module and VNET glue
3259 * Stuff that must be initialised only on boot or module load
3267 * Only print out this stuff the first time around,
3268 * when called from the sysinit code.
3274 "initialized, divert %s, nat %s, "
3275 "default to %s, logging ",
3281 #ifdef IPFIREWALL_NAT
3286 default_to_accept ? "accept" : "deny");
3289 * Note: V_xxx variables can be accessed here but the vnet specific
3290 * initializer may not have been called yet for the VIMAGE case.
3291 * Tuneables will have been processed. We will print out values for
3293 * XXX This should all be rationalized AFTER 8.0
3295 if (V_fw_verbose == 0)
3296 printf("disabled\n");
3297 else if (V_verbose_limit == 0)
3298 printf("unlimited\n");
3300 printf("limited to %d packets/entry by default\n",
3303 /* Check user-supplied table count for validness */
3304 if (default_fw_tables > IPFW_TABLES_MAX)
3305 default_fw_tables = IPFW_TABLES_MAX;
3307 ipfw_init_sopt_handler();
3308 ipfw_init_obj_rewriter();
3314 * Called for the removal of the last instance only on module unload.
3320 ipfw_iface_destroy();
3321 ipfw_destroy_sopt_handler();
3322 ipfw_destroy_obj_rewriter();
3323 printf("IP firewall unloaded\n");
3327 * Stuff that must be initialized for every instance
3328 * (including the first of course).
3331 vnet_ipfw_init(const void *unused)
3334 struct ip_fw *rule = NULL;
3335 struct ip_fw_chain *chain;
3337 chain = &V_layer3_chain;
3339 first = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
3341 /* First set up some values that are compile time options */
3342 V_autoinc_step = 100; /* bounded to 1..1000 in add_rule() */
3343 V_fw_deny_unknown_exthdrs = 1;
3344 #ifdef IPFIREWALL_VERBOSE
3347 #ifdef IPFIREWALL_VERBOSE_LIMIT
3348 V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
3350 #ifdef IPFIREWALL_NAT
3351 LIST_INIT(&chain->nat);
3354 /* Init shared services hash table */
3355 ipfw_init_srv(chain);
3357 ipfw_init_counters();
3358 /* Set initial number of tables */
3359 V_fw_tables_max = default_fw_tables;
3360 error = ipfw_init_tables(chain, first);
3362 printf("ipfw2: setting up tables failed\n");
3363 free(chain->map, M_IPFW);
3368 IPFW_LOCK_INIT(chain);
3370 /* fill and insert the default rule */
3371 rule = ipfw_alloc_rule(chain, sizeof(struct ip_fw));
3372 rule->flags |= IPFW_RULE_NOOPT;
3374 rule->cmd[0].len = 1;
3375 rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY;
3376 chain->default_rule = rule;
3377 ipfw_add_protected_rule(chain, rule, 0);
3379 ipfw_dyn_init(chain);
3380 ipfw_eaction_init(chain, first);
3381 #ifdef LINEAR_SKIPTO
3382 ipfw_init_skipto_cache(chain);
3384 ipfw_bpf_init(first);
3386 /* First set up some values that are compile time options */
3387 V_ipfw_vnet_ready = 1; /* Open for business */
3390 * Hook the sockopt handler and pfil hooks for ipv4 and ipv6.
3391 * Even if the latter two fail we still keep the module alive
3392 * because the sockopt and layer2 paths are still useful.
3393 * ipfw[6]_hook return 0 on success, ENOENT on failure,
3394 * so we can ignore the exact return value and just set a flag.
3396 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so
3397 * changes in the underlying (per-vnet) variables trigger
3398 * immediate hook()/unhook() calls.
3399 * In layer2 we have the same behaviour, except that V_ether_ipfw
3400 * is checked on each packet because there are no pfil hooks.
3402 V_ip_fw_ctl_ptr = ipfw_ctl3;
3403 error = ipfw_attach_hooks();
3408 * Called for the removal of each instance.
3411 vnet_ipfw_uninit(const void *unused)
3414 struct ip_fw_chain *chain = &V_layer3_chain;
3417 V_ipfw_vnet_ready = 0; /* tell new callers to go away */
3419 * disconnect from ipv4, ipv6, layer2 and sockopt.
3420 * Then grab, release and grab again the WLOCK so we make
3421 * sure the update is propagated and nobody will be in.
3423 ipfw_detach_hooks();
3424 V_ip_fw_ctl_ptr = NULL;
3426 last = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
3428 IPFW_UH_WLOCK(chain);
3429 IPFW_UH_WUNLOCK(chain);
3431 ipfw_dyn_uninit(0); /* run the callout_drain */
3433 IPFW_UH_WLOCK(chain);
3437 for (i = 0; i < chain->n_rules; i++)
3438 ipfw_reap_add(chain, &reap, chain->map[i]);
3439 free(chain->map, M_IPFW);
3440 #ifdef LINEAR_SKIPTO
3441 ipfw_destroy_skipto_cache(chain);
3443 IPFW_WUNLOCK(chain);
3444 IPFW_UH_WUNLOCK(chain);
3445 ipfw_destroy_tables(chain, last);
3446 ipfw_eaction_uninit(chain, last);
3448 ipfw_reap_rules(reap);
3449 vnet_ipfw_iface_destroy(chain);
3450 ipfw_destroy_srv(chain);
3451 IPFW_LOCK_DESTROY(chain);
3452 ipfw_dyn_uninit(1); /* free the remaining parts */
3453 ipfw_destroy_counters();
3454 ipfw_bpf_uninit(last);
3459 * Module event handler.
3460 * In general we have the choice of handling most of these events by the
3461 * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to
3462 * use the SYSINIT handlers as they are more capable of expressing the
3463 * flow of control during module and vnet operations, so this is just
3464 * a skeleton. Note there is no SYSINIT equivalent of the module
3465 * SHUTDOWN handler, but we don't have anything to do in that case anyhow.
3468 ipfw_modevent(module_t mod, int type, void *unused)
3474 /* Called once at module load or
3475 * system boot if compiled in. */
3478 /* Called before unload. May veto unloading. */
3481 /* Called during unload. */
3484 /* Called during system shutdown. */
3493 static moduledata_t ipfwmod = {
3499 /* Define startup order. */
3500 #define IPFW_SI_SUB_FIREWALL SI_SUB_PROTO_FIREWALL
3501 #define IPFW_MODEVENT_ORDER (SI_ORDER_ANY - 255) /* On boot slot in here. */
3502 #define IPFW_MODULE_ORDER (IPFW_MODEVENT_ORDER + 1) /* A little later. */
3503 #define IPFW_VNET_ORDER (IPFW_MODEVENT_ORDER + 2) /* Later still. */
3505 DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER);
3506 FEATURE(ipfw_ctl3, "ipfw new sockopt calls");
3507 MODULE_VERSION(ipfw, 3);
3508 /* should declare some dependencies here */
3511 * Starting up. Done in order after ipfwmod() has been called.
3512 * VNET_SYSINIT is also called for each existing vnet and each new vnet.
3514 SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
3516 VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
3517 vnet_ipfw_init, NULL);
3520 * Closing up shop. These are done in REVERSE ORDER, but still
3521 * after ipfwmod() has been called. Not called on reboot.
3522 * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
3523 * or when the module is unloaded.
3525 SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
3526 ipfw_destroy, NULL);
3527 VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
3528 vnet_ipfw_uninit, NULL);