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 tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd)
334 int optlen, bits = 0;
335 u_char *cp = (u_char *)(tcp + 1);
336 int x = (tcp->th_off << 2) - sizeof(struct tcphdr);
338 for (; x > 0; x -= optlen, cp += optlen) {
340 if (opt == TCPOPT_EOL)
342 if (opt == TCPOPT_NOP)
356 bits |= IP_FW_TCPOPT_MSS;
360 bits |= IP_FW_TCPOPT_WINDOW;
363 case TCPOPT_SACK_PERMITTED:
365 bits |= IP_FW_TCPOPT_SACK;
368 case TCPOPT_TIMESTAMP:
369 bits |= IP_FW_TCPOPT_TS;
374 return (flags_match(cmd, bits));
378 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd, struct ip_fw_chain *chain,
382 if (ifp == NULL) /* no iface with this packet, match fails */
385 /* Check by name or by IP address */
386 if (cmd->name[0] != '\0') { /* match by name */
387 if (cmd->name[0] == '\1') /* use tablearg to match */
388 return ipfw_lookup_table(chain, cmd->p.kidx, 0,
389 &ifp->if_index, tablearg);
392 if (fnmatch(cmd->name, ifp->if_xname, 0) == 0)
395 if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
399 #if !defined(USERSPACE) && defined(__FreeBSD__) /* and OSX too ? */
403 TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
404 if (ia->ifa_addr->sa_family != AF_INET)
406 if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
407 (ia->ifa_addr))->sin_addr.s_addr) {
408 if_addr_runlock(ifp);
409 return(1); /* match */
412 if_addr_runlock(ifp);
413 #endif /* __FreeBSD__ */
415 return(0); /* no match, fail ... */
419 * The verify_path function checks if a route to the src exists and
420 * if it is reachable via ifp (when provided).
422 * The 'verrevpath' option checks that the interface that an IP packet
423 * arrives on is the same interface that traffic destined for the
424 * packet's source address would be routed out of.
425 * The 'versrcreach' option just checks that the source address is
426 * reachable via any route (except default) in the routing table.
427 * These two are a measure to block forged packets. This is also
428 * commonly known as "anti-spoofing" or Unicast Reverse Path
429 * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs
430 * is purposely reminiscent of the Cisco IOS command,
432 * ip verify unicast reverse-path
433 * ip verify unicast source reachable-via any
435 * which implements the same functionality. But note that the syntax
436 * is misleading, and the check may be performed on all IP packets
437 * whether unicast, multicast, or broadcast.
440 verify_path(struct in_addr src, struct ifnet *ifp, u_int fib)
442 #if defined(USERSPACE) || !defined(__FreeBSD__)
445 struct nhop4_basic nh4;
447 if (fib4_lookup_nh_basic(fib, src, NHR_IFAIF, 0, &nh4) != 0)
451 * If ifp is provided, check for equality with rtentry.
452 * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp,
453 * in order to pass packets injected back by if_simloop():
454 * routing entry (via lo0) for our own address
455 * may exist, so we need to handle routing assymetry.
457 if (ifp != NULL && ifp != nh4.nh_ifp)
460 /* if no ifp provided, check if rtentry is not default route */
461 if (ifp == NULL && (nh4.nh_flags & NHF_DEFAULT) != 0)
464 /* or if this is a blackhole/reject route */
465 if (ifp == NULL && (nh4.nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
468 /* found valid route */
470 #endif /* __FreeBSD__ */
474 * Generate an SCTP packet containing an ABORT chunk. The verification tag
475 * is given by vtag. The T-bit is set in the ABORT chunk if and only if
476 * reflected is not 0.
480 ipfw_send_abort(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t vtag,
488 struct sctphdr *sctp;
489 struct sctp_chunkhdr *chunk;
490 u_int16_t hlen, plen, tlen;
492 MGETHDR(m, M_NOWAIT, MT_DATA);
496 M_SETFIB(m, id->fib);
499 mac_netinet_firewall_reply(replyto, m);
501 mac_netinet_firewall_send(m);
503 (void)replyto; /* don't warn about unused arg */
506 switch (id->addr_type) {
508 hlen = sizeof(struct ip);
512 hlen = sizeof(struct ip6_hdr);
520 plen = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
522 m->m_data += max_linkhdr;
523 m->m_flags |= M_SKIP_FIREWALL;
524 m->m_pkthdr.len = m->m_len = tlen;
525 m->m_pkthdr.rcvif = NULL;
526 bzero(m->m_data, tlen);
528 switch (id->addr_type) {
530 ip = mtod(m, struct ip *);
533 ip->ip_hl = sizeof(struct ip) >> 2;
534 ip->ip_tos = IPTOS_LOWDELAY;
535 ip->ip_len = htons(tlen);
536 ip->ip_id = htons(0);
537 ip->ip_off = htons(0);
538 ip->ip_ttl = V_ip_defttl;
539 ip->ip_p = IPPROTO_SCTP;
541 ip->ip_src.s_addr = htonl(id->dst_ip);
542 ip->ip_dst.s_addr = htonl(id->src_ip);
544 sctp = (struct sctphdr *)(ip + 1);
548 ip6 = mtod(m, struct ip6_hdr *);
550 ip6->ip6_vfc = IPV6_VERSION;
551 ip6->ip6_plen = htons(plen);
552 ip6->ip6_nxt = IPPROTO_SCTP;
553 ip6->ip6_hlim = IPV6_DEFHLIM;
554 ip6->ip6_src = id->dst_ip6;
555 ip6->ip6_dst = id->src_ip6;
557 sctp = (struct sctphdr *)(ip6 + 1);
562 sctp->src_port = htons(id->dst_port);
563 sctp->dest_port = htons(id->src_port);
564 sctp->v_tag = htonl(vtag);
565 sctp->checksum = htonl(0);
567 chunk = (struct sctp_chunkhdr *)(sctp + 1);
568 chunk->chunk_type = SCTP_ABORT_ASSOCIATION;
569 chunk->chunk_flags = 0;
570 if (reflected != 0) {
571 chunk->chunk_flags |= SCTP_HAD_NO_TCB;
573 chunk->chunk_length = htons(sizeof(struct sctp_chunkhdr));
575 sctp->checksum = sctp_calculate_cksum(m, hlen);
581 * Generate a TCP packet, containing either a RST or a keepalive.
582 * When flags & TH_RST, we are sending a RST packet, because of a
583 * "reset" action matched the packet.
584 * Otherwise we are sending a keepalive, and flags & TH_
585 * The 'replyto' mbuf is the mbuf being replied to, if any, and is required
586 * so that MAC can label the reply appropriately.
589 ipfw_send_pkt(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t seq,
590 u_int32_t ack, int flags)
592 struct mbuf *m = NULL; /* stupid compiler */
593 struct ip *h = NULL; /* stupid compiler */
595 struct ip6_hdr *h6 = NULL;
597 struct tcphdr *th = NULL;
600 MGETHDR(m, M_NOWAIT, MT_DATA);
604 M_SETFIB(m, id->fib);
607 mac_netinet_firewall_reply(replyto, m);
609 mac_netinet_firewall_send(m);
611 (void)replyto; /* don't warn about unused arg */
614 switch (id->addr_type) {
616 len = sizeof(struct ip) + sizeof(struct tcphdr);
620 len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
628 dir = ((flags & (TH_SYN | TH_RST)) == TH_SYN);
630 m->m_data += max_linkhdr;
631 m->m_flags |= M_SKIP_FIREWALL;
632 m->m_pkthdr.len = m->m_len = len;
633 m->m_pkthdr.rcvif = NULL;
634 bzero(m->m_data, len);
636 switch (id->addr_type) {
638 h = mtod(m, struct ip *);
640 /* prepare for checksum */
641 h->ip_p = IPPROTO_TCP;
642 h->ip_len = htons(sizeof(struct tcphdr));
644 h->ip_src.s_addr = htonl(id->src_ip);
645 h->ip_dst.s_addr = htonl(id->dst_ip);
647 h->ip_src.s_addr = htonl(id->dst_ip);
648 h->ip_dst.s_addr = htonl(id->src_ip);
651 th = (struct tcphdr *)(h + 1);
655 h6 = mtod(m, struct ip6_hdr *);
657 /* prepare for checksum */
658 h6->ip6_nxt = IPPROTO_TCP;
659 h6->ip6_plen = htons(sizeof(struct tcphdr));
661 h6->ip6_src = id->src_ip6;
662 h6->ip6_dst = id->dst_ip6;
664 h6->ip6_src = id->dst_ip6;
665 h6->ip6_dst = id->src_ip6;
668 th = (struct tcphdr *)(h6 + 1);
674 th->th_sport = htons(id->src_port);
675 th->th_dport = htons(id->dst_port);
677 th->th_sport = htons(id->dst_port);
678 th->th_dport = htons(id->src_port);
680 th->th_off = sizeof(struct tcphdr) >> 2;
682 if (flags & TH_RST) {
683 if (flags & TH_ACK) {
684 th->th_seq = htonl(ack);
685 th->th_flags = TH_RST;
689 th->th_ack = htonl(seq);
690 th->th_flags = TH_RST | TH_ACK;
694 * Keepalive - use caller provided sequence numbers
696 th->th_seq = htonl(seq);
697 th->th_ack = htonl(ack);
698 th->th_flags = TH_ACK;
701 switch (id->addr_type) {
703 th->th_sum = in_cksum(m, len);
705 /* finish the ip header */
707 h->ip_hl = sizeof(*h) >> 2;
708 h->ip_tos = IPTOS_LOWDELAY;
709 h->ip_off = htons(0);
710 h->ip_len = htons(len);
711 h->ip_ttl = V_ip_defttl;
716 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(*h6),
717 sizeof(struct tcphdr));
719 /* finish the ip6 header */
720 h6->ip6_vfc |= IPV6_VERSION;
721 h6->ip6_hlim = IPV6_DEFHLIM;
731 * ipv6 specific rules here...
734 icmp6type_match (int type, ipfw_insn_u32 *cmd)
736 return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) );
740 flow6id_match( int curr_flow, ipfw_insn_u32 *cmd )
743 for (i=0; i <= cmd->o.arg1; ++i )
744 if (curr_flow == cmd->d[i] )
749 /* support for IP6_*_ME opcodes */
750 static const struct in6_addr lla_mask = {{{
751 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
752 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
756 ipfw_localip6(struct in6_addr *in6)
758 struct rm_priotracker in6_ifa_tracker;
759 struct in6_ifaddr *ia;
761 if (IN6_IS_ADDR_MULTICAST(in6))
764 if (!IN6_IS_ADDR_LINKLOCAL(in6))
765 return (in6_localip(in6));
767 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
768 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
769 if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr))
771 if (IN6_ARE_MASKED_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
773 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
777 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
782 verify_path6(struct in6_addr *src, struct ifnet *ifp, u_int fib)
784 struct nhop6_basic nh6;
786 if (IN6_IS_SCOPE_LINKLOCAL(src))
789 if (fib6_lookup_nh_basic(fib, src, 0, NHR_IFAIF, 0, &nh6) != 0)
792 /* If ifp is provided, check for equality with route table. */
793 if (ifp != NULL && ifp != nh6.nh_ifp)
796 /* if no ifp provided, check if rtentry is not default route */
797 if (ifp == NULL && (nh6.nh_flags & NHF_DEFAULT) != 0)
800 /* or if this is a blackhole/reject route */
801 if (ifp == NULL && (nh6.nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0)
804 /* found valid route */
809 is_icmp6_query(int icmp6_type)
811 if ((icmp6_type <= ICMP6_MAXTYPE) &&
812 (icmp6_type == ICMP6_ECHO_REQUEST ||
813 icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
814 icmp6_type == ICMP6_WRUREQUEST ||
815 icmp6_type == ICMP6_FQDN_QUERY ||
816 icmp6_type == ICMP6_NI_QUERY))
823 map_icmp_unreach(int code)
828 case ICMP_UNREACH_NET:
829 case ICMP_UNREACH_HOST:
830 case ICMP_UNREACH_SRCFAIL:
831 case ICMP_UNREACH_NET_UNKNOWN:
832 case ICMP_UNREACH_HOST_UNKNOWN:
833 case ICMP_UNREACH_TOSNET:
834 case ICMP_UNREACH_TOSHOST:
835 return (ICMP6_DST_UNREACH_NOROUTE);
836 case ICMP_UNREACH_PORT:
837 return (ICMP6_DST_UNREACH_NOPORT);
840 * Map the rest of codes into admit prohibited.
841 * XXX: unreach proto should be mapped into ICMPv6
842 * parameter problem, but we use only unreach type.
844 return (ICMP6_DST_UNREACH_ADMIN);
849 send_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6)
854 if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) {
856 tcp = (struct tcphdr *)((char *)ip6 + hlen);
858 if ((tcp->th_flags & TH_RST) == 0) {
860 m0 = ipfw_send_pkt(args->m, &(args->f_id),
861 ntohl(tcp->th_seq), ntohl(tcp->th_ack),
862 tcp->th_flags | TH_RST);
864 ip6_output(m0, NULL, NULL, 0, NULL, NULL,
868 } else if (code == ICMP6_UNREACH_ABORT &&
869 args->f_id.proto == IPPROTO_SCTP) {
871 struct sctphdr *sctp;
875 sctp = (struct sctphdr *)((char *)ip6 + hlen);
877 v_tag = ntohl(sctp->v_tag);
878 /* Investigate the first chunk header if available */
879 if (m->m_len >= hlen + sizeof(struct sctphdr) +
880 sizeof(struct sctp_chunkhdr)) {
881 struct sctp_chunkhdr *chunk;
883 chunk = (struct sctp_chunkhdr *)(sctp + 1);
884 switch (chunk->chunk_type) {
885 case SCTP_INITIATION:
887 * Packets containing an INIT chunk MUST have
894 /* INIT chunk MUST NOT be bundled */
895 if (m->m_pkthdr.len >
896 hlen + sizeof(struct sctphdr) +
897 ntohs(chunk->chunk_length) + 3) {
900 /* Use the initiate tag if available */
901 if ((m->m_len >= hlen + sizeof(struct sctphdr) +
902 sizeof(struct sctp_chunkhdr) +
903 offsetof(struct sctp_init, a_rwnd))) {
904 struct sctp_init *init;
906 init = (struct sctp_init *)(chunk + 1);
907 v_tag = ntohl(init->initiate_tag);
911 case SCTP_ABORT_ASSOCIATION:
913 * If the packet contains an ABORT chunk, don't
915 * XXX: We should search through all chunks,
916 * but don't do to avoid attacks.
925 m0 = ipfw_send_abort(args->m, &(args->f_id), v_tag,
929 ip6_output(m0, NULL, NULL, 0, NULL, NULL, NULL);
931 } else if (code != ICMP6_UNREACH_RST && code != ICMP6_UNREACH_ABORT) {
932 /* Send an ICMPv6 unreach. */
935 * Unlike above, the mbufs need to line up with the ip6 hdr,
936 * as the contents are read. We need to m_adj() the
938 * The mbuf will however be thrown away so we can adjust it.
939 * Remember we did an m_pullup on it already so we
940 * can make some assumptions about contiguousness.
943 m_adj(m, args->L3offset);
945 icmp6_error(m, ICMP6_DST_UNREACH, code, 0);
956 * sends a reject message, consuming the mbuf passed as an argument.
959 send_reject(struct ip_fw_args *args, int code, int iplen, struct ip *ip)
963 /* XXX When ip is not guaranteed to be at mtod() we will
964 * need to account for this */
965 * The mbuf will however be thrown away so we can adjust it.
966 * Remember we did an m_pullup on it already so we
967 * can make some assumptions about contiguousness.
970 m_adj(m, args->L3offset);
972 if (code != ICMP_REJECT_RST && code != ICMP_REJECT_ABORT) {
973 /* Send an ICMP unreach */
974 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
975 } else if (code == ICMP_REJECT_RST && args->f_id.proto == IPPROTO_TCP) {
976 struct tcphdr *const tcp =
977 L3HDR(struct tcphdr, mtod(args->m, struct ip *));
978 if ( (tcp->th_flags & TH_RST) == 0) {
980 m = ipfw_send_pkt(args->m, &(args->f_id),
981 ntohl(tcp->th_seq), ntohl(tcp->th_ack),
982 tcp->th_flags | TH_RST);
984 ip_output(m, NULL, NULL, 0, NULL, NULL);
987 } else if (code == ICMP_REJECT_ABORT &&
988 args->f_id.proto == IPPROTO_SCTP) {
990 struct sctphdr *sctp;
991 struct sctp_chunkhdr *chunk;
992 struct sctp_init *init;
996 sctp = L3HDR(struct sctphdr, mtod(args->m, struct ip *));
998 v_tag = ntohl(sctp->v_tag);
999 if (iplen >= (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1000 sizeof(struct sctp_chunkhdr)) {
1001 /* Look at the first chunk header if available */
1002 chunk = (struct sctp_chunkhdr *)(sctp + 1);
1003 switch (chunk->chunk_type) {
1004 case SCTP_INITIATION:
1006 * Packets containing an INIT chunk MUST have
1013 /* INIT chunk MUST NOT be bundled */
1015 (ip->ip_hl << 2) + sizeof(struct sctphdr) +
1016 ntohs(chunk->chunk_length) + 3) {
1019 /* Use the initiate tag if available */
1020 if ((iplen >= (ip->ip_hl << 2) +
1021 sizeof(struct sctphdr) +
1022 sizeof(struct sctp_chunkhdr) +
1023 offsetof(struct sctp_init, a_rwnd))) {
1024 init = (struct sctp_init *)(chunk + 1);
1025 v_tag = ntohl(init->initiate_tag);
1029 case SCTP_ABORT_ASSOCIATION:
1031 * If the packet contains an ABORT chunk, don't
1033 * XXX: We should search through all chunks,
1034 * but don't do to avoid attacks.
1043 m = ipfw_send_abort(args->m, &(args->f_id), v_tag,
1047 ip_output(m, NULL, NULL, 0, NULL, NULL);
1055 * Support for uid/gid/jail lookup. These tests are expensive
1056 * (because we may need to look into the list of active sockets)
1057 * so we cache the results. ugid_lookupp is 0 if we have not
1058 * yet done a lookup, 1 if we succeeded, and -1 if we tried
1059 * and failed. The function always returns the match value.
1060 * We could actually spare the variable and use *uc, setting
1061 * it to '(void *)check_uidgid if we have no info, NULL if
1062 * we tried and failed, or any other value if successful.
1065 check_uidgid(ipfw_insn_u32 *insn, struct ip_fw_args *args, int *ugid_lookupp,
1068 #if defined(USERSPACE)
1069 return 0; // not supported in userspace
1073 return cred_check(insn, proto, oif,
1074 dst_ip, dst_port, src_ip, src_port,
1075 (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb);
1077 struct in_addr src_ip, dst_ip;
1078 struct inpcbinfo *pi;
1079 struct ipfw_flow_id *id;
1080 struct inpcb *pcb, *inp;
1090 * Check to see if the UDP or TCP stack supplied us with
1091 * the PCB. If so, rather then holding a lock and looking
1092 * up the PCB, we can use the one that was supplied.
1094 if (inp && *ugid_lookupp == 0) {
1095 INP_LOCK_ASSERT(inp);
1096 if (inp->inp_socket != NULL) {
1097 *uc = crhold(inp->inp_cred);
1103 * If we have already been here and the packet has no
1104 * PCB entry associated with it, then we can safely
1105 * assume that this is a no match.
1107 if (*ugid_lookupp == -1)
1109 if (id->proto == IPPROTO_TCP) {
1112 } else if (id->proto == IPPROTO_UDP) {
1113 lookupflags = INPLOOKUP_WILDCARD;
1115 } else if (id->proto == IPPROTO_UDPLITE) {
1116 lookupflags = INPLOOKUP_WILDCARD;
1117 pi = &V_ulitecbinfo;
1120 lookupflags |= INPLOOKUP_RLOCKPCB;
1122 if (*ugid_lookupp == 0) {
1123 if (id->addr_type == 6) {
1126 pcb = in6_pcblookup_mbuf(pi,
1127 &id->src_ip6, htons(id->src_port),
1128 &id->dst_ip6, htons(id->dst_port),
1129 lookupflags, oif, args->m);
1131 pcb = in6_pcblookup_mbuf(pi,
1132 &id->dst_ip6, htons(id->dst_port),
1133 &id->src_ip6, htons(id->src_port),
1134 lookupflags, oif, args->m);
1140 src_ip.s_addr = htonl(id->src_ip);
1141 dst_ip.s_addr = htonl(id->dst_ip);
1143 pcb = in_pcblookup_mbuf(pi,
1144 src_ip, htons(id->src_port),
1145 dst_ip, htons(id->dst_port),
1146 lookupflags, oif, args->m);
1148 pcb = in_pcblookup_mbuf(pi,
1149 dst_ip, htons(id->dst_port),
1150 src_ip, htons(id->src_port),
1151 lookupflags, oif, args->m);
1154 INP_RLOCK_ASSERT(pcb);
1155 *uc = crhold(pcb->inp_cred);
1159 if (*ugid_lookupp == 0) {
1161 * We tried and failed, set the variable to -1
1162 * so we will not try again on this packet.
1168 if (insn->o.opcode == O_UID)
1169 match = ((*uc)->cr_uid == (uid_t)insn->d[0]);
1170 else if (insn->o.opcode == O_GID)
1171 match = groupmember((gid_t)insn->d[0], *uc);
1172 else if (insn->o.opcode == O_JAIL)
1173 match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]);
1175 #endif /* __FreeBSD__ */
1176 #endif /* not supported in userspace */
1180 * Helper function to set args with info on the rule after the matching
1181 * one. slot is precise, whereas we guess rule_id as they are
1182 * assigned sequentially.
1185 set_match(struct ip_fw_args *args, int slot,
1186 struct ip_fw_chain *chain)
1188 args->rule.chain_id = chain->id;
1189 args->rule.slot = slot + 1; /* we use 0 as a marker */
1190 args->rule.rule_id = 1 + chain->map[slot]->id;
1191 args->rule.rulenum = chain->map[slot]->rulenum;
1194 #ifndef LINEAR_SKIPTO
1196 * Helper function to enable cached rule lookups using
1197 * cached_id and cached_pos fields in ipfw rule.
1200 jump_fast(struct ip_fw_chain *chain, struct ip_fw *f, int num,
1201 int tablearg, int jump_backwards)
1205 /* If possible use cached f_pos (in f->cached_pos),
1206 * whose version is written in f->cached_id
1207 * (horrible hacks to avoid changing the ABI).
1209 if (num != IP_FW_TARG && f->cached_id == chain->id)
1210 f_pos = f->cached_pos;
1212 int i = IP_FW_ARG_TABLEARG(chain, num, skipto);
1213 /* make sure we do not jump backward */
1214 if (jump_backwards == 0 && i <= f->rulenum)
1216 if (chain->idxmap != NULL)
1217 f_pos = chain->idxmap[i];
1219 f_pos = ipfw_find_rule(chain, i, 0);
1220 /* update the cache */
1221 if (num != IP_FW_TARG) {
1222 f->cached_id = chain->id;
1223 f->cached_pos = f_pos;
1231 * Helper function to enable real fast rule lookups.
1234 jump_linear(struct ip_fw_chain *chain, struct ip_fw *f, int num,
1235 int tablearg, int jump_backwards)
1239 num = IP_FW_ARG_TABLEARG(chain, num, skipto);
1240 /* make sure we do not jump backward */
1241 if (jump_backwards == 0 && num <= f->rulenum)
1242 num = f->rulenum + 1;
1243 f_pos = chain->idxmap[num];
1249 #define TARG(k, f) IP_FW_ARG_TABLEARG(chain, k, f)
1251 * The main check routine for the firewall.
1253 * All arguments are in args so we can modify them and return them
1254 * back to the caller.
1258 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1259 * Starts with the IP header.
1260 * args->eh (in) Mac header if present, NULL for layer3 packet.
1261 * args->L3offset Number of bytes bypassed if we came from L2.
1262 * e.g. often sizeof(eh) ** NOTYET **
1263 * args->oif Outgoing interface, NULL if packet is incoming.
1264 * The incoming interface is in the mbuf. (in)
1265 * args->divert_rule (in/out)
1266 * Skip up to the first rule past this rule number;
1267 * upon return, non-zero port number for divert or tee.
1269 * args->rule Pointer to the last matching rule (in/out)
1270 * args->next_hop Socket we are forwarding to (out).
1271 * args->next_hop6 IPv6 next hop we are forwarding to (out).
1272 * args->f_id Addresses grabbed from the packet (out)
1273 * args->rule.info a cookie depending on rule action
1277 * IP_FW_PASS the packet must be accepted
1278 * IP_FW_DENY the packet must be dropped
1279 * IP_FW_DIVERT divert packet, port in m_tag
1280 * IP_FW_TEE tee packet, port in m_tag
1281 * IP_FW_DUMMYNET to dummynet, pipe in args->cookie
1282 * IP_FW_NETGRAPH into netgraph, cookie args->cookie
1283 * args->rule contains the matching rule,
1284 * args->rule.info has additional information.
1288 ipfw_chk(struct ip_fw_args *args)
1292 * Local variables holding state while processing a packet:
1294 * IMPORTANT NOTE: to speed up the processing of rules, there
1295 * are some assumption on the values of the variables, which
1296 * are documented here. Should you change them, please check
1297 * the implementation of the various instructions to make sure
1298 * that they still work.
1300 * args->eh The MAC header. It is non-null for a layer2
1301 * packet, it is NULL for a layer-3 packet.
1303 * args->L3offset Offset in the packet to the L3 (IP or equiv.) header.
1305 * m | args->m Pointer to the mbuf, as received from the caller.
1306 * It may change if ipfw_chk() does an m_pullup, or if it
1307 * consumes the packet because it calls send_reject().
1308 * XXX This has to change, so that ipfw_chk() never modifies
1309 * or consumes the buffer.
1310 * ip is the beginning of the ip(4 or 6) header.
1311 * Calculated by adding the L3offset to the start of data.
1312 * (Until we start using L3offset, the packet is
1313 * supposed to start with the ip header).
1315 struct mbuf *m = args->m;
1316 struct ip *ip = mtod(m, struct ip *);
1319 * For rules which contain uid/gid or jail constraints, cache
1320 * a copy of the users credentials after the pcb lookup has been
1321 * executed. This will speed up the processing of rules with
1322 * these types of constraints, as well as decrease contention
1323 * on pcb related locks.
1326 struct bsd_ucred ucred_cache;
1328 struct ucred *ucred_cache = NULL;
1330 int ucred_lookup = 0;
1333 * oif | args->oif If NULL, ipfw_chk has been called on the
1334 * inbound path (ether_input, ip_input).
1335 * If non-NULL, ipfw_chk has been called on the outbound path
1336 * (ether_output, ip_output).
1338 struct ifnet *oif = args->oif;
1340 int f_pos = 0; /* index of current rule in the array */
1344 * hlen The length of the IP header.
1346 u_int hlen = 0; /* hlen >0 means we have an IP pkt */
1349 * offset The offset of a fragment. offset != 0 means that
1350 * we have a fragment at this offset of an IPv4 packet.
1351 * offset == 0 means that (if this is an IPv4 packet)
1352 * this is the first or only fragment.
1353 * For IPv6 offset|ip6f_mf == 0 means there is no Fragment Header
1354 * or there is a single packet fragment (fragment header added
1355 * without needed). We will treat a single packet fragment as if
1356 * there was no fragment header (or log/block depending on the
1357 * V_fw_permit_single_frag6 sysctl setting).
1360 u_short ip6f_mf = 0;
1363 * Local copies of addresses. They are only valid if we have
1366 * proto The protocol. Set to 0 for non-ip packets,
1367 * or to the protocol read from the packet otherwise.
1368 * proto != 0 means that we have an IPv4 packet.
1370 * src_port, dst_port port numbers, in HOST format. Only
1371 * valid for TCP and UDP packets.
1373 * src_ip, dst_ip ip addresses, in NETWORK format.
1374 * Only valid for IPv4 packets.
1377 uint16_t src_port = 0, dst_port = 0; /* NOTE: host format */
1378 struct in_addr src_ip, dst_ip; /* NOTE: network format */
1381 uint16_t etype = 0; /* Host order stored ether type */
1384 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1385 * MATCH_NONE when checked and not matched (q = NULL),
1386 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
1388 struct ipfw_dyn_info dyn_info;
1389 struct ip_fw *q = NULL;
1390 struct ip_fw_chain *chain = &V_layer3_chain;
1393 * We store in ulp a pointer to the upper layer protocol header.
1394 * In the ipv4 case this is easy to determine from the header,
1395 * but for ipv6 we might have some additional headers in the middle.
1396 * ulp is NULL if not found.
1398 void *ulp = NULL; /* upper layer protocol pointer. */
1400 /* XXX ipv6 variables */
1402 uint8_t icmp6_type = 0;
1403 uint16_t ext_hd = 0; /* bits vector for extension header filtering */
1404 /* end of ipv6 variables */
1408 int done = 0; /* flag to exit the outer loop */
1410 if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready))
1411 return (IP_FW_PASS); /* accept */
1413 dst_ip.s_addr = 0; /* make sure it is initialized */
1414 src_ip.s_addr = 0; /* make sure it is initialized */
1415 pktlen = m->m_pkthdr.len;
1416 args->f_id.fib = M_GETFIB(m); /* note mbuf not altered) */
1417 proto = args->f_id.proto = 0; /* mark f_id invalid */
1418 /* XXX 0 is a valid proto: IP/IPv6 Hop-by-Hop Option */
1420 DYN_INFO_INIT(&dyn_info);
1422 * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous,
1423 * then it sets p to point at the offset "len" in the mbuf. WARNING: the
1424 * pointer might become stale after other pullups (but we never use it
1427 #define PULLUP_TO(_len, p, T) PULLUP_LEN(_len, p, sizeof(T))
1428 #define PULLUP_LEN(_len, p, T) \
1430 int x = (_len) + T; \
1431 if ((m)->m_len < x) { \
1432 args->m = m = m_pullup(m, x); \
1434 goto pullup_failed; \
1436 p = (mtod(m, char *) + (_len)); \
1440 * if we have an ether header,
1443 etype = ntohs(args->eh->ether_type);
1445 /* Identify IP packets and fill up variables. */
1446 if (pktlen >= sizeof(struct ip6_hdr) &&
1447 (args->eh == NULL || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) {
1448 struct ip6_hdr *ip6 = (struct ip6_hdr *)ip;
1450 args->f_id.addr_type = 6;
1451 hlen = sizeof(struct ip6_hdr);
1452 proto = ip6->ip6_nxt;
1454 /* Search extension headers to find upper layer protocols */
1455 while (ulp == NULL && offset == 0) {
1457 case IPPROTO_ICMPV6:
1458 PULLUP_TO(hlen, ulp, struct icmp6_hdr);
1459 icmp6_type = ICMP6(ulp)->icmp6_type;
1463 PULLUP_TO(hlen, ulp, struct tcphdr);
1464 dst_port = TCP(ulp)->th_dport;
1465 src_port = TCP(ulp)->th_sport;
1466 /* save flags for dynamic rules */
1467 args->f_id._flags = TCP(ulp)->th_flags;
1471 if (pktlen >= hlen + sizeof(struct sctphdr) +
1472 sizeof(struct sctp_chunkhdr) +
1473 offsetof(struct sctp_init, a_rwnd))
1474 PULLUP_LEN(hlen, ulp,
1475 sizeof(struct sctphdr) +
1476 sizeof(struct sctp_chunkhdr) +
1477 offsetof(struct sctp_init, a_rwnd));
1478 else if (pktlen >= hlen + sizeof(struct sctphdr))
1479 PULLUP_LEN(hlen, ulp, pktlen - hlen);
1481 PULLUP_LEN(hlen, ulp,
1482 sizeof(struct sctphdr));
1483 src_port = SCTP(ulp)->src_port;
1484 dst_port = SCTP(ulp)->dest_port;
1488 case IPPROTO_UDPLITE:
1489 PULLUP_TO(hlen, ulp, struct udphdr);
1490 dst_port = UDP(ulp)->uh_dport;
1491 src_port = UDP(ulp)->uh_sport;
1494 case IPPROTO_HOPOPTS: /* RFC 2460 */
1495 PULLUP_TO(hlen, ulp, struct ip6_hbh);
1496 ext_hd |= EXT_HOPOPTS;
1497 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1498 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1502 case IPPROTO_ROUTING: /* RFC 2460 */
1503 PULLUP_TO(hlen, ulp, struct ip6_rthdr);
1504 switch (((struct ip6_rthdr *)ulp)->ip6r_type) {
1506 ext_hd |= EXT_RTHDR0;
1509 ext_hd |= EXT_RTHDR2;
1513 printf("IPFW2: IPV6 - Unknown "
1514 "Routing Header type(%d)\n",
1515 ((struct ip6_rthdr *)
1517 if (V_fw_deny_unknown_exthdrs)
1518 return (IP_FW_DENY);
1521 ext_hd |= EXT_ROUTING;
1522 hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3;
1523 proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt;
1527 case IPPROTO_FRAGMENT: /* RFC 2460 */
1528 PULLUP_TO(hlen, ulp, struct ip6_frag);
1529 ext_hd |= EXT_FRAGMENT;
1530 hlen += sizeof (struct ip6_frag);
1531 proto = ((struct ip6_frag *)ulp)->ip6f_nxt;
1532 offset = ((struct ip6_frag *)ulp)->ip6f_offlg &
1534 ip6f_mf = ((struct ip6_frag *)ulp)->ip6f_offlg &
1536 if (V_fw_permit_single_frag6 == 0 &&
1537 offset == 0 && ip6f_mf == 0) {
1539 printf("IPFW2: IPV6 - Invalid "
1540 "Fragment Header\n");
1541 if (V_fw_deny_unknown_exthdrs)
1542 return (IP_FW_DENY);
1546 ntohl(((struct ip6_frag *)ulp)->ip6f_ident);
1550 case IPPROTO_DSTOPTS: /* RFC 2460 */
1551 PULLUP_TO(hlen, ulp, struct ip6_hbh);
1552 ext_hd |= EXT_DSTOPTS;
1553 hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3;
1554 proto = ((struct ip6_hbh *)ulp)->ip6h_nxt;
1558 case IPPROTO_AH: /* RFC 2402 */
1559 PULLUP_TO(hlen, ulp, struct ip6_ext);
1561 hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2;
1562 proto = ((struct ip6_ext *)ulp)->ip6e_nxt;
1566 case IPPROTO_ESP: /* RFC 2406 */
1567 PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */
1568 /* Anything past Seq# is variable length and
1569 * data past this ext. header is encrypted. */
1573 case IPPROTO_NONE: /* RFC 2460 */
1575 * Packet ends here, and IPv6 header has
1576 * already been pulled up. If ip6e_len!=0
1577 * then octets must be ignored.
1579 ulp = ip; /* non-NULL to get out of loop. */
1582 case IPPROTO_OSPFIGP:
1583 /* XXX OSPF header check? */
1584 PULLUP_TO(hlen, ulp, struct ip6_ext);
1588 /* XXX PIM header check? */
1589 PULLUP_TO(hlen, ulp, struct pim);
1592 case IPPROTO_GRE: /* RFC 1701 */
1593 /* XXX GRE header check? */
1594 PULLUP_TO(hlen, ulp, struct grehdr);
1598 PULLUP_TO(hlen, ulp, struct carp_header);
1599 if (((struct carp_header *)ulp)->carp_version !=
1601 return (IP_FW_DENY);
1602 if (((struct carp_header *)ulp)->carp_type !=
1604 return (IP_FW_DENY);
1607 case IPPROTO_IPV6: /* RFC 2893 */
1608 PULLUP_TO(hlen, ulp, struct ip6_hdr);
1611 case IPPROTO_IPV4: /* RFC 2893 */
1612 PULLUP_TO(hlen, ulp, struct ip);
1617 printf("IPFW2: IPV6 - Unknown "
1618 "Extension Header(%d), ext_hd=%x\n",
1620 if (V_fw_deny_unknown_exthdrs)
1621 return (IP_FW_DENY);
1622 PULLUP_TO(hlen, ulp, struct ip6_ext);
1626 ip = mtod(m, struct ip *);
1627 ip6 = (struct ip6_hdr *)ip;
1628 args->f_id.src_ip6 = ip6->ip6_src;
1629 args->f_id.dst_ip6 = ip6->ip6_dst;
1630 args->f_id.src_ip = 0;
1631 args->f_id.dst_ip = 0;
1632 args->f_id.flow_id6 = ntohl(ip6->ip6_flow);
1633 iplen = ntohs(ip6->ip6_plen) + sizeof(*ip6);
1634 } else if (pktlen >= sizeof(struct ip) &&
1635 (args->eh == NULL || etype == ETHERTYPE_IP) && ip->ip_v == 4) {
1637 hlen = ip->ip_hl << 2;
1638 args->f_id.addr_type = 4;
1641 * Collect parameters into local variables for faster matching.
1644 src_ip = ip->ip_src;
1645 dst_ip = ip->ip_dst;
1646 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1647 iplen = ntohs(ip->ip_len);
1652 PULLUP_TO(hlen, ulp, struct tcphdr);
1653 dst_port = TCP(ulp)->th_dport;
1654 src_port = TCP(ulp)->th_sport;
1655 /* save flags for dynamic rules */
1656 args->f_id._flags = TCP(ulp)->th_flags;
1660 if (pktlen >= hlen + sizeof(struct sctphdr) +
1661 sizeof(struct sctp_chunkhdr) +
1662 offsetof(struct sctp_init, a_rwnd))
1663 PULLUP_LEN(hlen, ulp,
1664 sizeof(struct sctphdr) +
1665 sizeof(struct sctp_chunkhdr) +
1666 offsetof(struct sctp_init, a_rwnd));
1667 else if (pktlen >= hlen + sizeof(struct sctphdr))
1668 PULLUP_LEN(hlen, ulp, pktlen - hlen);
1670 PULLUP_LEN(hlen, ulp,
1671 sizeof(struct sctphdr));
1672 src_port = SCTP(ulp)->src_port;
1673 dst_port = SCTP(ulp)->dest_port;
1677 case IPPROTO_UDPLITE:
1678 PULLUP_TO(hlen, ulp, struct udphdr);
1679 dst_port = UDP(ulp)->uh_dport;
1680 src_port = UDP(ulp)->uh_sport;
1684 PULLUP_TO(hlen, ulp, struct icmphdr);
1685 //args->f_id.flags = ICMP(ulp)->icmp_type;
1693 ip = mtod(m, struct ip *);
1694 args->f_id.src_ip = ntohl(src_ip.s_addr);
1695 args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1698 pktlen = iplen < pktlen ? iplen: pktlen;
1699 if (proto) { /* we may have port numbers, store them */
1700 args->f_id.proto = proto;
1701 args->f_id.src_port = src_port = ntohs(src_port);
1702 args->f_id.dst_port = dst_port = ntohs(dst_port);
1705 IPFW_PF_RLOCK(chain);
1706 if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */
1707 IPFW_PF_RUNLOCK(chain);
1708 return (IP_FW_PASS); /* accept */
1710 if (args->rule.slot) {
1712 * Packet has already been tagged as a result of a previous
1713 * match on rule args->rule aka args->rule_id (PIPE, QUEUE,
1714 * REASS, NETGRAPH, DIVERT/TEE...)
1715 * Validate the slot and continue from the next one
1716 * if still present, otherwise do a lookup.
1718 f_pos = (args->rule.chain_id == chain->id) ?
1720 ipfw_find_rule(chain, args->rule.rulenum,
1721 args->rule.rule_id);
1727 * Now scan the rules, and parse microinstructions for each rule.
1728 * We have two nested loops and an inner switch. Sometimes we
1729 * need to break out of one or both loops, or re-enter one of
1730 * the loops with updated variables. Loop variables are:
1732 * f_pos (outer loop) points to the current rule.
1733 * On output it points to the matching rule.
1734 * done (outer loop) is used as a flag to break the loop.
1735 * l (inner loop) residual length of current rule.
1736 * cmd points to the current microinstruction.
1738 * We break the inner loop by setting l=0 and possibly
1739 * cmdlen=0 if we don't want to advance cmd.
1740 * We break the outer loop by setting done=1
1741 * We can restart the inner loop by setting l>0 and f_pos, f, cmd
1744 for (; f_pos < chain->n_rules; f_pos++) {
1746 uint32_t tablearg = 0;
1747 int l, cmdlen, skip_or; /* skip rest of OR block */
1750 f = chain->map[f_pos];
1751 if (V_set_disable & (1 << f->set) )
1755 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
1756 l -= cmdlen, cmd += cmdlen) {
1760 * check_body is a jump target used when we find a
1761 * CHECK_STATE, and need to jump to the body of
1766 cmdlen = F_LEN(cmd);
1768 * An OR block (insn_1 || .. || insn_n) has the
1769 * F_OR bit set in all but the last instruction.
1770 * The first match will set "skip_or", and cause
1771 * the following instructions to be skipped until
1772 * past the one with the F_OR bit clear.
1774 if (skip_or) { /* skip this instruction */
1775 if ((cmd->len & F_OR) == 0)
1776 skip_or = 0; /* next one is good */
1779 match = 0; /* set to 1 if we succeed */
1781 switch (cmd->opcode) {
1783 * The first set of opcodes compares the packet's
1784 * fields with some pattern, setting 'match' if a
1785 * match is found. At the end of the loop there is
1786 * logic to deal with F_NOT and F_OR flags associated
1794 printf("ipfw: opcode %d unimplemented\n",
1802 * We only check offset == 0 && proto != 0,
1803 * as this ensures that we have a
1804 * packet with the ports info.
1808 if (proto == IPPROTO_TCP ||
1809 proto == IPPROTO_UDP ||
1810 proto == IPPROTO_UDPLITE)
1811 match = check_uidgid(
1812 (ipfw_insn_u32 *)cmd,
1813 args, &ucred_lookup,
1817 (void *)&ucred_cache);
1822 match = iface_match(m->m_pkthdr.rcvif,
1823 (ipfw_insn_if *)cmd, chain, &tablearg);
1827 match = iface_match(oif, (ipfw_insn_if *)cmd,
1832 match = iface_match(oif ? oif :
1833 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd,
1838 if (args->eh != NULL) { /* have MAC header */
1839 u_int32_t *want = (u_int32_t *)
1840 ((ipfw_insn_mac *)cmd)->addr;
1841 u_int32_t *mask = (u_int32_t *)
1842 ((ipfw_insn_mac *)cmd)->mask;
1843 u_int32_t *hdr = (u_int32_t *)args->eh;
1846 ( want[0] == (hdr[0] & mask[0]) &&
1847 want[1] == (hdr[1] & mask[1]) &&
1848 want[2] == (hdr[2] & mask[2]) );
1853 if (args->eh != NULL) {
1855 ((ipfw_insn_u16 *)cmd)->ports;
1858 for (i = cmdlen - 1; !match && i>0;
1860 match = (etype >= p[0] &&
1866 match = (offset != 0);
1869 case O_IN: /* "out" is "not in" */
1870 match = (oif == NULL);
1874 match = (args->eh != NULL);
1879 /* For diverted packets, args->rule.info
1880 * contains the divert port (in host format)
1881 * reason and direction.
1883 uint32_t i = args->rule.info;
1884 match = (i&IPFW_IS_MASK) == IPFW_IS_DIVERT &&
1885 cmd->arg1 & ((i & IPFW_INFO_IN) ? 1 : 2);
1891 * We do not allow an arg of 0 so the
1892 * check of "proto" only suffices.
1894 match = (proto == cmd->arg1);
1899 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
1903 case O_IP_DST_LOOKUP:
1909 if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) {
1910 /* Determine lookup key type */
1911 vidx = ((ipfw_insn_u32 *)cmd)->d[1];
1912 if (vidx != 4 /* uid */ &&
1913 vidx != 5 /* jail */ &&
1914 is_ipv6 == 0 && is_ipv4 == 0)
1916 /* Determine key length */
1917 if (vidx == 0 /* dst-ip */ ||
1918 vidx == 1 /* src-ip */)
1920 sizeof(struct in6_addr):
1923 keylen = sizeof(key);
1926 if (vidx == 0 /* dst-ip */)
1927 pkey = is_ipv4 ? (void *)&dst_ip:
1928 (void *)&args->f_id.dst_ip6;
1929 else if (vidx == 1 /* src-ip */)
1930 pkey = is_ipv4 ? (void *)&src_ip:
1931 (void *)&args->f_id.src_ip6;
1932 else if (vidx == 6 /* dscp */) {
1934 key = ip->ip_tos >> 2;
1936 key = args->f_id.flow_id6;
1937 key = (key & 0x0f) << 2 |
1938 (key & 0xf000) >> 14;
1941 } else if (vidx == 2 /* dst-port */ ||
1942 vidx == 3 /* src-port */) {
1943 /* Skip fragments */
1946 /* Skip proto without ports */
1947 if (proto != IPPROTO_TCP &&
1948 proto != IPPROTO_UDP &&
1949 proto != IPPROTO_UDPLITE &&
1950 proto != IPPROTO_SCTP)
1952 if (vidx == 2 /* dst-port */)
1958 else if (vidx == 4 /* uid */ ||
1959 vidx == 5 /* jail */) {
1961 (ipfw_insn_u32 *)cmd,
1962 args, &ucred_lookup,
1965 if (vidx == 4 /* uid */)
1966 key = ucred_cache->cr_uid;
1967 else if (vidx == 5 /* jail */)
1968 key = ucred_cache->cr_prison->pr_id;
1969 #else /* !__FreeBSD__ */
1970 (void *)&ucred_cache);
1971 if (vidx == 4 /* uid */)
1972 key = ucred_cache.uid;
1973 else if (vidx == 5 /* jail */)
1974 key = ucred_cache.xid;
1975 #endif /* !__FreeBSD__ */
1977 #endif /* !USERSPACE */
1980 match = ipfw_lookup_table(chain,
1981 cmd->arg1, keylen, pkey, &vidx);
1987 /* cmdlen =< F_INSN_SIZE(ipfw_insn_u32) */
1990 case O_IP_SRC_LOOKUP:
1997 keylen = sizeof(in_addr_t);
1998 if (cmd->opcode == O_IP_DST_LOOKUP)
2002 } else if (is_ipv6) {
2003 keylen = sizeof(struct in6_addr);
2004 if (cmd->opcode == O_IP_DST_LOOKUP)
2005 pkey = &args->f_id.dst_ip6;
2007 pkey = &args->f_id.src_ip6;
2010 match = ipfw_lookup_table(chain, cmd->arg1,
2011 keylen, pkey, &vidx);
2014 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) {
2015 match = ((ipfw_insn_u32 *)cmd)->d[0] ==
2016 TARG_VAL(chain, vidx, tag);
2024 case O_IP_FLOW_LOOKUP:
2027 match = ipfw_lookup_table(chain,
2028 cmd->arg1, 0, &args->f_id, &v);
2029 if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
2030 match = ((ipfw_insn_u32 *)cmd)->d[0] ==
2031 TARG_VAL(chain, v, tag);
2040 (cmd->opcode == O_IP_DST_MASK) ?
2041 dst_ip.s_addr : src_ip.s_addr;
2042 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d;
2045 for (; !match && i>0; i-= 2, p+= 2)
2046 match = (p[0] == (a & p[1]));
2052 match = in_localip(src_ip);
2058 match= is_ipv6 && ipfw_localip6(&args->f_id.src_ip6);
2065 u_int32_t *d = (u_int32_t *)(cmd+1);
2067 cmd->opcode == O_IP_DST_SET ?
2073 addr -= d[0]; /* subtract base */
2074 match = (addr < cmd->arg1) &&
2075 ( d[ 1 + (addr>>5)] &
2076 (1<<(addr & 0x1f)) );
2082 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
2088 match = in_localip(dst_ip);
2094 match= is_ipv6 && ipfw_localip6(&args->f_id.dst_ip6);
2102 * offset == 0 && proto != 0 is enough
2103 * to guarantee that we have a
2104 * packet with port info.
2106 if ((proto == IPPROTO_UDP ||
2107 proto == IPPROTO_UDPLITE ||
2108 proto == IPPROTO_TCP ||
2109 proto==IPPROTO_SCTP) && offset == 0) {
2111 (cmd->opcode == O_IP_SRCPORT) ?
2112 src_port : dst_port ;
2114 ((ipfw_insn_u16 *)cmd)->ports;
2117 for (i = cmdlen - 1; !match && i>0;
2119 match = (x>=p[0] && x<=p[1]);
2124 match = (offset == 0 && proto==IPPROTO_ICMP &&
2125 icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) );
2130 match = is_ipv6 && offset == 0 &&
2131 proto==IPPROTO_ICMPV6 &&
2133 ICMP6(ulp)->icmp6_type,
2134 (ipfw_insn_u32 *)cmd);
2140 ipopts_match(ip, cmd) );
2145 cmd->arg1 == ip->ip_v);
2151 if (is_ipv4) { /* only for IP packets */
2156 if (cmd->opcode == O_IPLEN)
2158 else if (cmd->opcode == O_IPTTL)
2160 else /* must be IPID */
2161 x = ntohs(ip->ip_id);
2163 match = (cmd->arg1 == x);
2166 /* otherwise we have ranges */
2167 p = ((ipfw_insn_u16 *)cmd)->ports;
2169 for (; !match && i>0; i--, p += 2)
2170 match = (x >= p[0] && x <= p[1]);
2174 case O_IPPRECEDENCE:
2176 (cmd->arg1 == (ip->ip_tos & 0xe0)) );
2181 flags_match(cmd, ip->ip_tos));
2189 p = ((ipfw_insn_u32 *)cmd)->d;
2192 x = ip->ip_tos >> 2;
2195 v = &((struct ip6_hdr *)ip)->ip6_vfc;
2196 x = (*v & 0x0F) << 2;
2202 /* DSCP bitmask is stored as low_u32 high_u32 */
2204 match = *(p + 1) & (1 << (x - 32));
2206 match = *p & (1 << x);
2211 if (proto == IPPROTO_TCP && offset == 0) {
2218 struct ip6_hdr *ip6;
2220 ip6 = (struct ip6_hdr *)ip;
2221 if (ip6->ip6_plen == 0) {
2223 * Jumbo payload is not
2232 x = iplen - (ip->ip_hl << 2);
2234 x -= tcp->th_off << 2;
2236 match = (cmd->arg1 == x);
2239 /* otherwise we have ranges */
2240 p = ((ipfw_insn_u16 *)cmd)->ports;
2242 for (; !match && i>0; i--, p += 2)
2243 match = (x >= p[0] && x <= p[1]);
2248 match = (proto == IPPROTO_TCP && offset == 0 &&
2249 flags_match(cmd, TCP(ulp)->th_flags));
2253 if (proto == IPPROTO_TCP && offset == 0 && ulp){
2254 PULLUP_LEN(hlen, ulp,
2255 (TCP(ulp)->th_off << 2));
2256 match = tcpopts_match(TCP(ulp), cmd);
2261 match = (proto == IPPROTO_TCP && offset == 0 &&
2262 ((ipfw_insn_u32 *)cmd)->d[0] ==
2267 match = (proto == IPPROTO_TCP && offset == 0 &&
2268 ((ipfw_insn_u32 *)cmd)->d[0] ==
2273 if (proto == IPPROTO_TCP && offset == 0) {
2278 x = ntohs(TCP(ulp)->th_win);
2280 match = (cmd->arg1 == x);
2283 /* Otherwise we have ranges. */
2284 p = ((ipfw_insn_u16 *)cmd)->ports;
2286 for (; !match && i > 0; i--, p += 2)
2287 match = (x >= p[0] && x <= p[1]);
2292 /* reject packets which have SYN only */
2293 /* XXX should i also check for TH_ACK ? */
2294 match = (proto == IPPROTO_TCP && offset == 0 &&
2295 (TCP(ulp)->th_flags &
2296 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
2302 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
2305 * ALTQ uses mbuf tags from another
2306 * packet filtering system - pf(4).
2307 * We allocate a tag in its format
2308 * and fill it in, pretending to be pf(4).
2311 at = pf_find_mtag(m);
2312 if (at != NULL && at->qid != 0)
2314 mtag = m_tag_get(PACKET_TAG_PF,
2315 sizeof(struct pf_mtag), M_NOWAIT | M_ZERO);
2318 * Let the packet fall back to the
2323 m_tag_prepend(m, mtag);
2324 at = (struct pf_mtag *)(mtag + 1);
2325 at->qid = altq->qid;
2331 ipfw_log(chain, f, hlen, args, m,
2332 oif, offset | ip6f_mf, tablearg, ip);
2337 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]);
2341 /* Outgoing packets automatically pass/match */
2342 match = ((oif != NULL) ||
2343 (m->m_pkthdr.rcvif == NULL) ||
2347 verify_path6(&(args->f_id.src_ip6),
2348 m->m_pkthdr.rcvif, args->f_id.fib) :
2350 verify_path(src_ip, m->m_pkthdr.rcvif,
2355 /* Outgoing packets automatically pass/match */
2356 match = (hlen > 0 && ((oif != NULL) ||
2359 verify_path6(&(args->f_id.src_ip6),
2360 NULL, args->f_id.fib) :
2362 verify_path(src_ip, NULL, args->f_id.fib)));
2366 /* Outgoing packets automatically pass/match */
2367 if (oif == NULL && hlen > 0 &&
2368 ( (is_ipv4 && in_localaddr(src_ip))
2371 in6_localaddr(&(args->f_id.src_ip6)))
2376 is_ipv6 ? verify_path6(
2377 &(args->f_id.src_ip6),
2389 match = (m_tag_find(m,
2390 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL);
2391 /* otherwise no match */
2397 IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6,
2398 &((ipfw_insn_ip6 *)cmd)->addr6);
2403 IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6,
2404 &((ipfw_insn_ip6 *)cmd)->addr6);
2406 case O_IP6_SRC_MASK:
2407 case O_IP6_DST_MASK:
2411 struct in6_addr *d =
2412 &((ipfw_insn_ip6 *)cmd)->addr6;
2414 for (; !match && i > 0; d += 2,
2415 i -= F_INSN_SIZE(struct in6_addr)
2421 APPLY_MASK(&p, &d[1]);
2423 IN6_ARE_ADDR_EQUAL(&d[0],
2431 flow6id_match(args->f_id.flow_id6,
2432 (ipfw_insn_u32 *) cmd);
2437 (ext_hd & ((ipfw_insn *) cmd)->arg1);
2451 uint32_t tag = TARG(cmd->arg1, tag);
2453 /* Packet is already tagged with this tag? */
2454 mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL);
2456 /* We have `untag' action when F_NOT flag is
2457 * present. And we must remove this mtag from
2458 * mbuf and reset `match' to zero (`match' will
2459 * be inversed later).
2460 * Otherwise we should allocate new mtag and
2461 * push it into mbuf.
2463 if (cmd->len & F_NOT) { /* `untag' action */
2465 m_tag_delete(m, mtag);
2469 mtag = m_tag_alloc( MTAG_IPFW,
2472 m_tag_prepend(m, mtag);
2479 case O_FIB: /* try match the specified fib */
2480 if (args->f_id.fib == cmd->arg1)
2485 #ifndef USERSPACE /* not supported in userspace */
2486 struct inpcb *inp = args->inp;
2487 struct inpcbinfo *pi;
2489 if (is_ipv6) /* XXX can we remove this ? */
2492 if (proto == IPPROTO_TCP)
2494 else if (proto == IPPROTO_UDP)
2496 else if (proto == IPPROTO_UDPLITE)
2497 pi = &V_ulitecbinfo;
2502 * XXXRW: so_user_cookie should almost
2503 * certainly be inp_user_cookie?
2506 /* For incoming packet, lookup up the
2507 inpcb using the src/dest ip/port tuple */
2509 inp = in_pcblookup(pi,
2510 src_ip, htons(src_port),
2511 dst_ip, htons(dst_port),
2512 INPLOOKUP_RLOCKPCB, NULL);
2515 inp->inp_socket->so_user_cookie;
2521 if (inp->inp_socket) {
2523 inp->inp_socket->so_user_cookie;
2528 #endif /* !USERSPACE */
2534 uint32_t tag = TARG(cmd->arg1, tag);
2537 match = m_tag_locate(m, MTAG_IPFW,
2542 /* we have ranges */
2543 for (mtag = m_tag_first(m);
2544 mtag != NULL && !match;
2545 mtag = m_tag_next(m, mtag)) {
2549 if (mtag->m_tag_cookie != MTAG_IPFW)
2552 p = ((ipfw_insn_u16 *)cmd)->ports;
2554 for(; !match && i > 0; i--, p += 2)
2556 mtag->m_tag_id >= p[0] &&
2557 mtag->m_tag_id <= p[1];
2563 * The second set of opcodes represents 'actions',
2564 * i.e. the terminal part of a rule once the packet
2565 * matches all previous patterns.
2566 * Typically there is only one action for each rule,
2567 * and the opcode is stored at the end of the rule
2568 * (but there are exceptions -- see below).
2570 * In general, here we set retval and terminate the
2571 * outer loop (would be a 'break 3' in some language,
2572 * but we need to set l=0, done=1)
2575 * O_COUNT and O_SKIPTO actions:
2576 * instead of terminating, we jump to the next rule
2577 * (setting l=0), or to the SKIPTO target (setting
2578 * f/f_len, cmd and l as needed), respectively.
2580 * O_TAG, O_LOG and O_ALTQ action parameters:
2581 * perform some action and set match = 1;
2583 * O_LIMIT and O_KEEP_STATE: these opcodes are
2584 * not real 'actions', and are stored right
2585 * before the 'action' part of the rule.
2586 * These opcodes try to install an entry in the
2587 * state tables; if successful, we continue with
2588 * the next opcode (match=1; break;), otherwise
2589 * the packet must be dropped (set retval,
2590 * break loops with l=0, done=1)
2592 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2593 * cause a lookup of the state table, and a jump
2594 * to the 'action' part of the parent rule
2595 * if an entry is found, or
2596 * (CHECK_STATE only) a jump to the next rule if
2597 * the entry is not found.
2598 * The result of the lookup is cached so that
2599 * further instances of these opcodes become NOPs.
2600 * The jump to the next rule is done by setting
2605 if (ipfw_dyn_install_state(chain, f,
2606 (ipfw_insn_limit *)cmd, args, ulp,
2607 pktlen, &dyn_info, tablearg)) {
2608 /* error or limit violation */
2609 retval = IP_FW_DENY;
2610 l = 0; /* exit inner loop */
2611 done = 1; /* exit outer loop */
2619 * dynamic rules are checked at the first
2620 * keep-state or check-state occurrence,
2621 * with the result being stored in dyn_info.
2622 * The compiler introduces a PROBE_STATE
2623 * instruction for us when we have a
2624 * KEEP_STATE (because PROBE_STATE needs
2627 if (DYN_LOOKUP_NEEDED(&dyn_info, cmd) &&
2628 (q = ipfw_dyn_lookup_state(args, ulp,
2629 pktlen, cmd, &dyn_info)) != NULL) {
2631 * Found dynamic entry, jump to the
2632 * 'action' part of the parent rule
2633 * by setting f, cmd, l and clearing
2637 f_pos = dyn_info.f_pos;
2638 cmd = ACTION_PTR(f);
2639 l = f->cmd_len - f->act_ofs;
2645 * Dynamic entry not found. If CHECK_STATE,
2646 * skip to next rule, if PROBE_STATE just
2647 * ignore and continue with next opcode.
2649 if (cmd->opcode == O_CHECK_STATE)
2650 l = 0; /* exit inner loop */
2655 retval = 0; /* accept */
2656 l = 0; /* exit inner loop */
2657 done = 1; /* exit outer loop */
2662 set_match(args, f_pos, chain);
2663 args->rule.info = TARG(cmd->arg1, pipe);
2664 if (cmd->opcode == O_PIPE)
2665 args->rule.info |= IPFW_IS_PIPE;
2667 args->rule.info |= IPFW_ONEPASS;
2668 retval = IP_FW_DUMMYNET;
2669 l = 0; /* exit inner loop */
2670 done = 1; /* exit outer loop */
2675 if (args->eh) /* not on layer 2 */
2677 /* otherwise this is terminal */
2678 l = 0; /* exit inner loop */
2679 done = 1; /* exit outer loop */
2680 retval = (cmd->opcode == O_DIVERT) ?
2681 IP_FW_DIVERT : IP_FW_TEE;
2682 set_match(args, f_pos, chain);
2683 args->rule.info = TARG(cmd->arg1, divert);
2687 IPFW_INC_RULE_COUNTER(f, pktlen);
2688 l = 0; /* exit inner loop */
2692 IPFW_INC_RULE_COUNTER(f, pktlen);
2693 f_pos = JUMP(chain, f, cmd->arg1, tablearg, 0);
2695 * Skip disabled rules, and re-enter
2696 * the inner loop with the correct
2697 * f_pos, f, l and cmd.
2698 * Also clear cmdlen and skip_or
2700 for (; f_pos < chain->n_rules - 1 &&
2702 (1 << chain->map[f_pos]->set));
2705 /* Re-enter the inner loop at the skipto rule. */
2706 f = chain->map[f_pos];
2713 break; /* not reached */
2715 case O_CALLRETURN: {
2717 * Implementation of `subroutine' call/return,
2718 * in the stack carried in an mbuf tag. This
2719 * is different from `skipto' in that any call
2720 * address is possible (`skipto' must prevent
2721 * backward jumps to avoid endless loops).
2722 * We have `return' action when F_NOT flag is
2723 * present. The `m_tag_id' field is used as
2727 uint16_t jmpto, *stack;
2729 #define IS_CALL ((cmd->len & F_NOT) == 0)
2730 #define IS_RETURN ((cmd->len & F_NOT) != 0)
2732 * Hand-rolled version of m_tag_locate() with
2734 * If not already tagged, allocate new tag.
2736 mtag = m_tag_first(m);
2737 while (mtag != NULL) {
2738 if (mtag->m_tag_cookie ==
2741 mtag = m_tag_next(m, mtag);
2743 if (mtag == NULL && IS_CALL) {
2744 mtag = m_tag_alloc(MTAG_IPFW_CALL, 0,
2745 IPFW_CALLSTACK_SIZE *
2746 sizeof(uint16_t), M_NOWAIT);
2748 m_tag_prepend(m, mtag);
2752 * On error both `call' and `return' just
2753 * continue with next rule.
2755 if (IS_RETURN && (mtag == NULL ||
2756 mtag->m_tag_id == 0)) {
2757 l = 0; /* exit inner loop */
2760 if (IS_CALL && (mtag == NULL ||
2761 mtag->m_tag_id >= IPFW_CALLSTACK_SIZE)) {
2762 printf("ipfw: call stack error, "
2763 "go to next rule\n");
2764 l = 0; /* exit inner loop */
2768 IPFW_INC_RULE_COUNTER(f, pktlen);
2769 stack = (uint16_t *)(mtag + 1);
2772 * The `call' action may use cached f_pos
2773 * (in f->next_rule), whose version is written
2775 * The `return' action, however, doesn't have
2776 * fixed jump address in cmd->arg1 and can't use
2780 stack[mtag->m_tag_id] = f->rulenum;
2782 f_pos = JUMP(chain, f, cmd->arg1,
2784 } else { /* `return' action */
2786 jmpto = stack[mtag->m_tag_id] + 1;
2787 f_pos = ipfw_find_rule(chain, jmpto, 0);
2791 * Skip disabled rules, and re-enter
2792 * the inner loop with the correct
2793 * f_pos, f, l and cmd.
2794 * Also clear cmdlen and skip_or
2796 for (; f_pos < chain->n_rules - 1 &&
2798 (1 << chain->map[f_pos]->set)); f_pos++)
2800 /* Re-enter the inner loop at the dest rule. */
2801 f = chain->map[f_pos];
2807 break; /* NOTREACHED */
2814 * Drop the packet and send a reject notice
2815 * if the packet is not ICMP (or is an ICMP
2816 * query), and it is not multicast/broadcast.
2818 if (hlen > 0 && is_ipv4 && offset == 0 &&
2819 (proto != IPPROTO_ICMP ||
2820 is_icmp_query(ICMP(ulp))) &&
2821 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2822 !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2823 send_reject(args, cmd->arg1, iplen, ip);
2829 if (hlen > 0 && is_ipv6 &&
2830 ((offset & IP6F_OFF_MASK) == 0) &&
2831 (proto != IPPROTO_ICMPV6 ||
2832 (is_icmp6_query(icmp6_type) == 1)) &&
2833 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2834 !IN6_IS_ADDR_MULTICAST(
2835 &args->f_id.dst_ip6)) {
2837 cmd->opcode == O_REJECT ?
2838 map_icmp_unreach(cmd->arg1):
2840 (struct ip6_hdr *)ip);
2846 retval = IP_FW_DENY;
2847 l = 0; /* exit inner loop */
2848 done = 1; /* exit outer loop */
2852 if (args->eh) /* not valid on layer2 pkts */
2855 dyn_info.direction == MATCH_FORWARD) {
2856 struct sockaddr_in *sa;
2858 sa = &(((ipfw_insn_sa *)cmd)->sa);
2859 if (sa->sin_addr.s_addr == INADDR_ANY) {
2862 * We use O_FORWARD_IP opcode for
2863 * fwd rule with tablearg, but tables
2864 * now support IPv6 addresses. And
2865 * when we are inspecting IPv6 packet,
2866 * we can use nh6 field from
2867 * table_value as next_hop6 address.
2870 struct sockaddr_in6 *sa6;
2872 sa6 = args->next_hop6 =
2874 sa6->sin6_family = AF_INET6;
2875 sa6->sin6_len = sizeof(*sa6);
2876 sa6->sin6_addr = TARG_VAL(
2877 chain, tablearg, nh6);
2878 sa6->sin6_port = sa->sin_port;
2880 * Set sin6_scope_id only for
2881 * link-local unicast addresses.
2883 if (IN6_IS_ADDR_LINKLOCAL(
2885 sa6->sin6_scope_id =
2892 args->hopstore.sin_port =
2894 sa = args->next_hop =
2896 sa->sin_family = AF_INET;
2897 sa->sin_len = sizeof(*sa);
2898 sa->sin_addr.s_addr = htonl(
2899 TARG_VAL(chain, tablearg,
2903 args->next_hop = sa;
2906 retval = IP_FW_PASS;
2907 l = 0; /* exit inner loop */
2908 done = 1; /* exit outer loop */
2913 if (args->eh) /* not valid on layer2 pkts */
2916 dyn_info.direction == MATCH_FORWARD) {
2917 struct sockaddr_in6 *sin6;
2919 sin6 = &(((ipfw_insn_sa6 *)cmd)->sa);
2920 args->next_hop6 = sin6;
2922 retval = IP_FW_PASS;
2923 l = 0; /* exit inner loop */
2924 done = 1; /* exit outer loop */
2930 set_match(args, f_pos, chain);
2931 args->rule.info = TARG(cmd->arg1, netgraph);
2933 args->rule.info |= IPFW_ONEPASS;
2934 retval = (cmd->opcode == O_NETGRAPH) ?
2935 IP_FW_NETGRAPH : IP_FW_NGTEE;
2936 l = 0; /* exit inner loop */
2937 done = 1; /* exit outer loop */
2943 IPFW_INC_RULE_COUNTER(f, pktlen);
2944 fib = TARG(cmd->arg1, fib) & 0x7FFF;
2945 if (fib >= rt_numfibs)
2948 args->f_id.fib = fib;
2949 l = 0; /* exit inner loop */
2956 code = TARG(cmd->arg1, dscp) & 0x3F;
2957 l = 0; /* exit inner loop */
2961 old = *(uint16_t *)ip;
2962 ip->ip_tos = (code << 2) |
2963 (ip->ip_tos & 0x03);
2964 ip->ip_sum = cksum_adjust(ip->ip_sum,
2965 old, *(uint16_t *)ip);
2966 } else if (is_ipv6) {
2969 v = &((struct ip6_hdr *)ip)->ip6_vfc;
2970 *v = (*v & 0xF0) | (code >> 2);
2972 *v = (*v & 0x3F) | ((code & 0x03) << 6);
2976 IPFW_INC_RULE_COUNTER(f, pktlen);
2981 l = 0; /* exit inner loop */
2982 done = 1; /* exit outer loop */
2984 * Ensure that we do not invoke NAT handler for
2985 * non IPv4 packets. Libalias expects only IPv4.
2987 if (!is_ipv4 || !IPFW_NAT_LOADED) {
2988 retval = IP_FW_DENY;
2995 set_match(args, f_pos, chain);
2996 /* Check if this is 'global' nat rule */
2997 if (cmd->arg1 == IP_FW_NAT44_GLOBAL) {
2998 retval = ipfw_nat_ptr(args, NULL, m);
3001 t = ((ipfw_insn_nat *)cmd)->nat;
3003 nat_id = TARG(cmd->arg1, nat);
3004 t = (*lookup_nat_ptr)(&chain->nat, nat_id);
3007 retval = IP_FW_DENY;
3010 if (cmd->arg1 != IP_FW_TARG)
3011 ((ipfw_insn_nat *)cmd)->nat = t;
3013 retval = ipfw_nat_ptr(args, t, m);
3019 l = 0; /* in any case exit inner loop */
3020 if (is_ipv6) /* IPv6 is not supported yet */
3022 IPFW_INC_RULE_COUNTER(f, pktlen);
3023 ip_off = ntohs(ip->ip_off);
3025 /* if not fragmented, go to next rule */
3026 if ((ip_off & (IP_MF | IP_OFFMASK)) == 0)
3029 args->m = m = ip_reass(m);
3032 * do IP header checksum fixup.
3034 if (m == NULL) { /* fragment got swallowed */
3035 retval = IP_FW_DENY;
3036 } else { /* good, packet complete */
3039 ip = mtod(m, struct ip *);
3040 hlen = ip->ip_hl << 2;
3042 if (hlen == sizeof(struct ip))
3043 ip->ip_sum = in_cksum_hdr(ip);
3045 ip->ip_sum = in_cksum(m, hlen);
3046 retval = IP_FW_REASS;
3047 set_match(args, f_pos, chain);
3049 done = 1; /* exit outer loop */
3052 case O_EXTERNAL_ACTION:
3053 l = 0; /* in any case exit inner loop */
3054 retval = ipfw_run_eaction(chain, args,
3057 * If both @retval and @done are zero,
3058 * consider this as rule matching and
3061 if (retval == 0 && done == 0) {
3062 IPFW_INC_RULE_COUNTER(f, pktlen);
3064 * Reset the result of the last
3065 * dynamic state lookup.
3066 * External action can change
3067 * @args content, and it may be
3068 * used for new state lookup later.
3070 DYN_INFO_INIT(&dyn_info);
3075 panic("-- unknown opcode %d\n", cmd->opcode);
3076 } /* end of switch() on opcodes */
3078 * if we get here with l=0, then match is irrelevant.
3081 if (cmd->len & F_NOT)
3085 if (cmd->len & F_OR)
3088 if (!(cmd->len & F_OR)) /* not an OR block, */
3089 break; /* try next rule */
3092 } /* end of inner loop, scan opcodes */
3098 /* next_rule:; */ /* try next rule */
3100 } /* end of outer for, scan rules */
3103 struct ip_fw *rule = chain->map[f_pos];
3104 /* Update statistics */
3105 IPFW_INC_RULE_COUNTER(rule, pktlen);
3107 retval = IP_FW_DENY;
3108 printf("ipfw: ouch!, skip past end of rules, denying packet\n");
3110 IPFW_PF_RUNLOCK(chain);
3112 if (ucred_cache != NULL)
3113 crfree(ucred_cache);
3119 printf("ipfw: pullup failed\n");
3120 return (IP_FW_DENY);
3124 * Set maximum number of tables that can be used in given VNET ipfw instance.
3128 sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS)
3131 unsigned int ntables;
3133 ntables = V_fw_tables_max;
3135 error = sysctl_handle_int(oidp, &ntables, 0, req);
3136 /* Read operation or some error */
3137 if ((error != 0) || (req->newptr == NULL))
3140 return (ipfw_resize_tables(&V_layer3_chain, ntables));
3144 * Switches table namespace between global and per-set.
3147 sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS)
3152 sets = V_fw_tables_sets;
3154 error = sysctl_handle_int(oidp, &sets, 0, req);
3155 /* Read operation or some error */
3156 if ((error != 0) || (req->newptr == NULL))
3159 return (ipfw_switch_tables_namespace(&V_layer3_chain, sets));
3164 * Module and VNET glue
3168 * Stuff that must be initialised only on boot or module load
3176 * Only print out this stuff the first time around,
3177 * when called from the sysinit code.
3183 "initialized, divert %s, nat %s, "
3184 "default to %s, logging ",
3190 #ifdef IPFIREWALL_NAT
3195 default_to_accept ? "accept" : "deny");
3198 * Note: V_xxx variables can be accessed here but the vnet specific
3199 * initializer may not have been called yet for the VIMAGE case.
3200 * Tuneables will have been processed. We will print out values for
3202 * XXX This should all be rationalized AFTER 8.0
3204 if (V_fw_verbose == 0)
3205 printf("disabled\n");
3206 else if (V_verbose_limit == 0)
3207 printf("unlimited\n");
3209 printf("limited to %d packets/entry by default\n",
3212 /* Check user-supplied table count for validness */
3213 if (default_fw_tables > IPFW_TABLES_MAX)
3214 default_fw_tables = IPFW_TABLES_MAX;
3216 ipfw_init_sopt_handler();
3217 ipfw_init_obj_rewriter();
3223 * Called for the removal of the last instance only on module unload.
3229 ipfw_iface_destroy();
3230 ipfw_destroy_sopt_handler();
3231 ipfw_destroy_obj_rewriter();
3232 printf("IP firewall unloaded\n");
3236 * Stuff that must be initialized for every instance
3237 * (including the first of course).
3240 vnet_ipfw_init(const void *unused)
3243 struct ip_fw *rule = NULL;
3244 struct ip_fw_chain *chain;
3246 chain = &V_layer3_chain;
3248 first = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
3250 /* First set up some values that are compile time options */
3251 V_autoinc_step = 100; /* bounded to 1..1000 in add_rule() */
3252 V_fw_deny_unknown_exthdrs = 1;
3253 #ifdef IPFIREWALL_VERBOSE
3256 #ifdef IPFIREWALL_VERBOSE_LIMIT
3257 V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
3259 #ifdef IPFIREWALL_NAT
3260 LIST_INIT(&chain->nat);
3263 /* Init shared services hash table */
3264 ipfw_init_srv(chain);
3266 ipfw_init_counters();
3267 /* Set initial number of tables */
3268 V_fw_tables_max = default_fw_tables;
3269 error = ipfw_init_tables(chain, first);
3271 printf("ipfw2: setting up tables failed\n");
3272 free(chain->map, M_IPFW);
3277 IPFW_LOCK_INIT(chain);
3279 /* fill and insert the default rule */
3280 rule = ipfw_alloc_rule(chain, sizeof(struct ip_fw));
3282 rule->cmd[0].len = 1;
3283 rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY;
3284 chain->default_rule = rule;
3285 ipfw_add_protected_rule(chain, rule, 0);
3287 ipfw_dyn_init(chain);
3288 ipfw_eaction_init(chain, first);
3289 #ifdef LINEAR_SKIPTO
3290 ipfw_init_skipto_cache(chain);
3292 ipfw_bpf_init(first);
3294 /* First set up some values that are compile time options */
3295 V_ipfw_vnet_ready = 1; /* Open for business */
3298 * Hook the sockopt handler and pfil hooks for ipv4 and ipv6.
3299 * Even if the latter two fail we still keep the module alive
3300 * because the sockopt and layer2 paths are still useful.
3301 * ipfw[6]_hook return 0 on success, ENOENT on failure,
3302 * so we can ignore the exact return value and just set a flag.
3304 * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so
3305 * changes in the underlying (per-vnet) variables trigger
3306 * immediate hook()/unhook() calls.
3307 * In layer2 we have the same behaviour, except that V_ether_ipfw
3308 * is checked on each packet because there are no pfil hooks.
3310 V_ip_fw_ctl_ptr = ipfw_ctl3;
3311 error = ipfw_attach_hooks(1);
3316 * Called for the removal of each instance.
3319 vnet_ipfw_uninit(const void *unused)
3322 struct ip_fw_chain *chain = &V_layer3_chain;
3325 V_ipfw_vnet_ready = 0; /* tell new callers to go away */
3327 * disconnect from ipv4, ipv6, layer2 and sockopt.
3328 * Then grab, release and grab again the WLOCK so we make
3329 * sure the update is propagated and nobody will be in.
3331 (void)ipfw_attach_hooks(0 /* detach */);
3332 V_ip_fw_ctl_ptr = NULL;
3334 last = IS_DEFAULT_VNET(curvnet) ? 1 : 0;
3336 IPFW_UH_WLOCK(chain);
3337 IPFW_UH_WUNLOCK(chain);
3339 ipfw_dyn_uninit(0); /* run the callout_drain */
3341 IPFW_UH_WLOCK(chain);
3345 for (i = 0; i < chain->n_rules; i++)
3346 ipfw_reap_add(chain, &reap, chain->map[i]);
3347 free(chain->map, M_IPFW);
3348 #ifdef LINEAR_SKIPTO
3349 ipfw_destroy_skipto_cache(chain);
3351 IPFW_WUNLOCK(chain);
3352 IPFW_UH_WUNLOCK(chain);
3353 ipfw_destroy_tables(chain, last);
3354 ipfw_eaction_uninit(chain, last);
3356 ipfw_reap_rules(reap);
3357 vnet_ipfw_iface_destroy(chain);
3358 ipfw_destroy_srv(chain);
3359 IPFW_LOCK_DESTROY(chain);
3360 ipfw_dyn_uninit(1); /* free the remaining parts */
3361 ipfw_destroy_counters();
3362 ipfw_bpf_uninit(last);
3367 * Module event handler.
3368 * In general we have the choice of handling most of these events by the
3369 * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to
3370 * use the SYSINIT handlers as they are more capable of expressing the
3371 * flow of control during module and vnet operations, so this is just
3372 * a skeleton. Note there is no SYSINIT equivalent of the module
3373 * SHUTDOWN handler, but we don't have anything to do in that case anyhow.
3376 ipfw_modevent(module_t mod, int type, void *unused)
3382 /* Called once at module load or
3383 * system boot if compiled in. */
3386 /* Called before unload. May veto unloading. */
3389 /* Called during unload. */
3392 /* Called during system shutdown. */
3401 static moduledata_t ipfwmod = {
3407 /* Define startup order. */
3408 #define IPFW_SI_SUB_FIREWALL SI_SUB_PROTO_FIREWALL
3409 #define IPFW_MODEVENT_ORDER (SI_ORDER_ANY - 255) /* On boot slot in here. */
3410 #define IPFW_MODULE_ORDER (IPFW_MODEVENT_ORDER + 1) /* A little later. */
3411 #define IPFW_VNET_ORDER (IPFW_MODEVENT_ORDER + 2) /* Later still. */
3413 DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER);
3414 FEATURE(ipfw_ctl3, "ipfw new sockopt calls");
3415 MODULE_VERSION(ipfw, 3);
3416 /* should declare some dependencies here */
3419 * Starting up. Done in order after ipfwmod() has been called.
3420 * VNET_SYSINIT is also called for each existing vnet and each new vnet.
3422 SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
3424 VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
3425 vnet_ipfw_init, NULL);
3428 * Closing up shop. These are done in REVERSE ORDER, but still
3429 * after ipfwmod() has been called. Not called on reboot.
3430 * VNET_SYSUNINIT is also called for each exiting vnet as it exits.
3431 * or when the module is unloaded.
3433 SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER,
3434 ipfw_destroy, NULL);
3435 VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER,
3436 vnet_ipfw_uninit, NULL);