2 * Copyright (c) 2002 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
25 * $FreeBSD: src/sys/netinet/ip_fw2.c,v 1.6.2.12 2003/04/08 10:42:32 maxim Exp $
26 * $DragonFly: src/sys/net/ipfw/ip_fw2.c,v 1.41 2008/03/07 11:34:20 sephe Exp $
33 * Implement IP packet firewall (new version)
39 #include "opt_ipdivert.h"
42 #error IPFIREWALL requires INET.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
50 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 #include <sys/thread2.h>
57 #include <sys/ucred.h>
58 #include <sys/in_cksum.h>
60 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/ip_icmp.h>
69 #include <net/dummynet/ip_dummynet.h>
70 #include <netinet/tcp.h>
71 #include <netinet/tcp_timer.h>
72 #include <netinet/tcp_var.h>
73 #include <netinet/tcpip.h>
74 #include <netinet/udp.h>
75 #include <netinet/udp_var.h>
77 #include <netinet/if_ether.h> /* XXX for ETHERTYPE_IP */
80 * set_disable contains one bit per set value (0..31).
81 * If the bit is set, all rules with the corresponding set
82 * are disabled. Set 31 is reserved for the default rule
83 * and CANNOT be disabled.
85 static uint32_t set_disable
;
87 static int fw_verbose
;
88 static int verbose_limit
;
91 static int ipfw_refcnt
;
94 static struct callout ipfw_timeout_h
;
95 #define IPFW_DEFAULT_RULE 65535
98 * list of rules for layer 3
100 static struct ip_fw
*layer3_chain
;
102 MALLOC_DEFINE(M_IPFW
, "IpFw/IpAcct", "IpFw/IpAcct chain's");
104 static int fw_debug
= 1;
105 static int autoinc_step
= 100; /* bounded to 1..1000 in ipfw_add_rule() */
108 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, fw
, CTLFLAG_RW
, 0, "Firewall");
109 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, enable
, CTLFLAG_RW
,
110 &fw_enable
, 0, "Enable ipfw");
111 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, autoinc_step
, CTLFLAG_RW
,
112 &autoinc_step
, 0, "Rule number autincrement step");
113 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
,one_pass
,CTLFLAG_RW
,
115 "Only do a single pass through ipfw when using dummynet(4)");
116 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, debug
, CTLFLAG_RW
,
117 &fw_debug
, 0, "Enable printing of debug ip_fw statements");
118 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, verbose
, CTLFLAG_RW
,
119 &fw_verbose
, 0, "Log matches to ipfw rules");
120 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, verbose_limit
, CTLFLAG_RW
,
121 &verbose_limit
, 0, "Set upper limit of matches of ipfw rules logged");
124 * Description of dynamic rules.
126 * Dynamic rules are stored in lists accessed through a hash table
127 * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
128 * be modified through the sysctl variable dyn_buckets which is
129 * updated when the table becomes empty.
131 * XXX currently there is only one list, ipfw_dyn.
133 * When a packet is received, its address fields are first masked
134 * with the mask defined for the rule, then hashed, then matched
135 * against the entries in the corresponding list.
136 * Dynamic rules can be used for different purposes:
138 * + enforcing limits on the number of sessions;
139 * + in-kernel NAT (not implemented yet)
141 * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
142 * measured in seconds and depending on the flags.
144 * The total number of dynamic rules is stored in dyn_count.
145 * The max number of dynamic rules is dyn_max. When we reach
146 * the maximum number of rules we do not create anymore. This is
147 * done to avoid consuming too much memory, but also too much
148 * time when searching on each packet (ideally, we should try instead
149 * to put a limit on the length of the list on each bucket...).
151 * Each dynamic rule holds a pointer to the parent ipfw rule so
152 * we know what action to perform. Dynamic rules are removed when
153 * the parent rule is deleted. XXX we should make them survive.
155 * There are some limitations with dynamic rules -- we do not
156 * obey the 'randomized match', and we do not do multiple
157 * passes through the firewall. XXX check the latter!!!
159 static ipfw_dyn_rule
**ipfw_dyn_v
= NULL
;
160 static uint32_t dyn_buckets
= 256; /* must be power of 2 */
161 static uint32_t curr_dyn_buckets
= 256; /* must be power of 2 */
164 * Timeouts for various events in handing dynamic rules.
166 static uint32_t dyn_ack_lifetime
= 300;
167 static uint32_t dyn_syn_lifetime
= 20;
168 static uint32_t dyn_fin_lifetime
= 1;
169 static uint32_t dyn_rst_lifetime
= 1;
170 static uint32_t dyn_udp_lifetime
= 10;
171 static uint32_t dyn_short_lifetime
= 5;
174 * Keepalives are sent if dyn_keepalive is set. They are sent every
175 * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
176 * seconds of lifetime of a rule.
177 * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
178 * than dyn_keepalive_period.
181 static uint32_t dyn_keepalive_interval
= 20;
182 static uint32_t dyn_keepalive_period
= 5;
183 static uint32_t dyn_keepalive
= 1; /* do send keepalives */
185 static uint32_t static_count
; /* # of static rules */
186 static uint32_t static_ioc_len
; /* bytes of static rules */
187 static uint32_t dyn_count
; /* # of dynamic rules */
188 static uint32_t dyn_max
= 4096; /* max # of dynamic rules */
190 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_buckets
, CTLFLAG_RW
,
191 &dyn_buckets
, 0, "Number of dyn. buckets");
192 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, curr_dyn_buckets
, CTLFLAG_RD
,
193 &curr_dyn_buckets
, 0, "Current Number of dyn. buckets");
194 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_count
, CTLFLAG_RD
,
195 &dyn_count
, 0, "Number of dyn. rules");
196 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_max
, CTLFLAG_RW
,
197 &dyn_max
, 0, "Max number of dyn. rules");
198 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, static_count
, CTLFLAG_RD
,
199 &static_count
, 0, "Number of static rules");
200 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_ack_lifetime
, CTLFLAG_RW
,
201 &dyn_ack_lifetime
, 0, "Lifetime of dyn. rules for acks");
202 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_syn_lifetime
, CTLFLAG_RW
,
203 &dyn_syn_lifetime
, 0, "Lifetime of dyn. rules for syn");
204 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_fin_lifetime
, CTLFLAG_RW
,
205 &dyn_fin_lifetime
, 0, "Lifetime of dyn. rules for fin");
206 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_rst_lifetime
, CTLFLAG_RW
,
207 &dyn_rst_lifetime
, 0, "Lifetime of dyn. rules for rst");
208 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_udp_lifetime
, CTLFLAG_RW
,
209 &dyn_udp_lifetime
, 0, "Lifetime of dyn. rules for UDP");
210 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_short_lifetime
, CTLFLAG_RW
,
211 &dyn_short_lifetime
, 0, "Lifetime of dyn. rules for other situations");
212 SYSCTL_INT(_net_inet_ip_fw
, OID_AUTO
, dyn_keepalive
, CTLFLAG_RW
,
213 &dyn_keepalive
, 0, "Enable keepalives for dyn. rules");
215 #endif /* SYSCTL_NODE */
218 * dummynet needs a reference to the default rule, because rules can be
219 * deleted while packets hold a reference to them. When this happens,
220 * dummynet changes the reference to the default rule (it could well be a
221 * NULL pointer, but this way we do not need to check for the special
222 * case, plus here he have info on the default behaviour).
224 struct ip_fw
*ip_fw_default_rule
;
226 static ip_fw_chk_t ipfw_chk
;
229 ipfw_free_rule(struct ip_fw
*rule
)
231 KASSERT(rule
->refcnt
> 0, ("invalid refcnt %u\n", rule
->refcnt
));
232 atomic_subtract_int(&rule
->refcnt
, 1);
233 if (atomic_cmpset_int(&rule
->refcnt
, 0, 1)) {
241 ipfw_unref_rule(void *priv
)
243 ipfw_free_rule(priv
);
245 atomic_subtract_int(&ipfw_refcnt
, 1);
250 ipfw_ref_rule(struct ip_fw
*rule
)
253 atomic_add_int(&ipfw_refcnt
, 1);
255 atomic_add_int(&rule
->refcnt
, 1);
259 * This macro maps an ip pointer into a layer3 header pointer of type T
261 #define L3HDR(T, ip) ((T *)((uint32_t *)(ip) + (ip)->ip_hl))
264 icmptype_match(struct ip
*ip
, ipfw_insn_u32
*cmd
)
266 int type
= L3HDR(struct icmp
,ip
)->icmp_type
;
268 return (type
<= ICMP_MAXTYPE
&& (cmd
->d
[0] & (1<<type
)) );
271 #define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \
272 (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) )
275 is_icmp_query(struct ip
*ip
)
277 int type
= L3HDR(struct icmp
, ip
)->icmp_type
;
278 return (type
<= ICMP_MAXTYPE
&& (TT
& (1<<type
)) );
283 * The following checks use two arrays of 8 or 16 bits to store the
284 * bits that we want set or clear, respectively. They are in the
285 * low and high half of cmd->arg1 or cmd->d[0].
287 * We scan options and store the bits we find set. We succeed if
289 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
291 * The code is sometimes optimized not to store additional variables.
295 flags_match(ipfw_insn
*cmd
, uint8_t bits
)
300 if ( ((cmd
->arg1
& 0xff) & bits
) != 0)
301 return 0; /* some bits we want set were clear */
302 want_clear
= (cmd
->arg1
>> 8) & 0xff;
303 if ( (want_clear
& bits
) != want_clear
)
304 return 0; /* some bits we want clear were set */
309 ipopts_match(struct ip
*ip
, ipfw_insn
*cmd
)
311 int optlen
, bits
= 0;
312 u_char
*cp
= (u_char
*)(ip
+ 1);
313 int x
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
315 for (; x
> 0; x
-= optlen
, cp
+= optlen
) {
316 int opt
= cp
[IPOPT_OPTVAL
];
318 if (opt
== IPOPT_EOL
)
320 if (opt
== IPOPT_NOP
)
323 optlen
= cp
[IPOPT_OLEN
];
324 if (optlen
<= 0 || optlen
> x
)
325 return 0; /* invalid or truncated */
333 bits
|= IP_FW_IPOPT_LSRR
;
337 bits
|= IP_FW_IPOPT_SSRR
;
341 bits
|= IP_FW_IPOPT_RR
;
345 bits
|= IP_FW_IPOPT_TS
;
349 return (flags_match(cmd
, bits
));
353 tcpopts_match(struct ip
*ip
, ipfw_insn
*cmd
)
355 int optlen
, bits
= 0;
356 struct tcphdr
*tcp
= L3HDR(struct tcphdr
,ip
);
357 u_char
*cp
= (u_char
*)(tcp
+ 1);
358 int x
= (tcp
->th_off
<< 2) - sizeof(struct tcphdr
);
360 for (; x
> 0; x
-= optlen
, cp
+= optlen
) {
362 if (opt
== TCPOPT_EOL
)
364 if (opt
== TCPOPT_NOP
)
378 bits
|= IP_FW_TCPOPT_MSS
;
382 bits
|= IP_FW_TCPOPT_WINDOW
;
385 case TCPOPT_SACK_PERMITTED
:
387 bits
|= IP_FW_TCPOPT_SACK
;
390 case TCPOPT_TIMESTAMP
:
391 bits
|= IP_FW_TCPOPT_TS
;
397 bits
|= IP_FW_TCPOPT_CC
;
401 return (flags_match(cmd
, bits
));
405 iface_match(struct ifnet
*ifp
, ipfw_insn_if
*cmd
)
407 if (ifp
== NULL
) /* no iface with this packet, match fails */
409 /* Check by name or by IP address */
410 if (cmd
->name
[0] != '\0') { /* match by name */
413 if (kfnmatch(cmd
->name
, ifp
->if_xname
, 0) == 0)
416 if (strncmp(ifp
->if_xname
, cmd
->name
, IFNAMSIZ
) == 0)
420 struct ifaddr_container
*ifac
;
422 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
423 struct ifaddr
*ia
= ifac
->ifa
;
425 if (ia
->ifa_addr
== NULL
)
427 if (ia
->ifa_addr
->sa_family
!= AF_INET
)
429 if (cmd
->p
.ip
.s_addr
== ((struct sockaddr_in
*)
430 (ia
->ifa_addr
))->sin_addr
.s_addr
)
431 return(1); /* match */
434 return(0); /* no match, fail ... */
437 static uint64_t norule_counter
; /* counter for ipfw_log(NULL...) */
439 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
440 #define SNP(buf) buf, sizeof(buf)
443 * We enter here when we have a rule with O_LOG.
444 * XXX this function alone takes about 2Kbytes of code!
447 ipfw_log(struct ip_fw
*f
, u_int hlen
, struct ether_header
*eh
,
448 struct mbuf
*m
, struct ifnet
*oif
)
451 int limit_reached
= 0;
452 char action2
[40], proto
[48], fragment
[28];
457 if (f
== NULL
) { /* bogus pkt */
458 if (verbose_limit
!= 0 && norule_counter
>= verbose_limit
)
461 if (norule_counter
== verbose_limit
)
462 limit_reached
= verbose_limit
;
464 } else { /* O_LOG is the first action, find the real one */
465 ipfw_insn
*cmd
= ACTION_PTR(f
);
466 ipfw_insn_log
*l
= (ipfw_insn_log
*)cmd
;
468 if (l
->max_log
!= 0 && l
->log_left
== 0)
471 if (l
->log_left
== 0)
472 limit_reached
= l
->max_log
;
473 cmd
+= F_LEN(cmd
); /* point to first action */
474 if (cmd
->opcode
== O_PROB
)
478 switch (cmd
->opcode
) {
484 if (cmd
->arg1
==ICMP_REJECT_RST
)
486 else if (cmd
->arg1
==ICMP_UNREACH_HOST
)
489 ksnprintf(SNPARGS(action2
, 0), "Unreach %d",
500 ksnprintf(SNPARGS(action2
, 0), "Divert %d",
504 ksnprintf(SNPARGS(action2
, 0), "Tee %d",
508 ksnprintf(SNPARGS(action2
, 0), "SkipTo %d",
512 ksnprintf(SNPARGS(action2
, 0), "Pipe %d",
516 ksnprintf(SNPARGS(action2
, 0), "Queue %d",
520 ipfw_insn_sa
*sa
= (ipfw_insn_sa
*)cmd
;
523 len
= ksnprintf(SNPARGS(action2
, 0), "Forward to %s",
524 inet_ntoa(sa
->sa
.sin_addr
));
526 ksnprintf(SNPARGS(action2
, len
), ":%d",
536 if (hlen
== 0) { /* non-ip */
537 ksnprintf(SNPARGS(proto
, 0), "MAC");
539 struct ip
*ip
= mtod(m
, struct ip
*);
540 /* these three are all aliases to the same thing */
541 struct icmp
*const icmp
= L3HDR(struct icmp
, ip
);
542 struct tcphdr
*const tcp
= (struct tcphdr
*)icmp
;
543 struct udphdr
*const udp
= (struct udphdr
*)icmp
;
545 int ip_off
, offset
, ip_len
;
549 if (eh
!= NULL
) { /* layer 2 packets are as on the wire */
550 ip_off
= ntohs(ip
->ip_off
);
551 ip_len
= ntohs(ip
->ip_len
);
556 offset
= ip_off
& IP_OFFMASK
;
559 len
= ksnprintf(SNPARGS(proto
, 0), "TCP %s",
560 inet_ntoa(ip
->ip_src
));
562 ksnprintf(SNPARGS(proto
, len
), ":%d %s:%d",
563 ntohs(tcp
->th_sport
),
564 inet_ntoa(ip
->ip_dst
),
565 ntohs(tcp
->th_dport
));
567 ksnprintf(SNPARGS(proto
, len
), " %s",
568 inet_ntoa(ip
->ip_dst
));
572 len
= ksnprintf(SNPARGS(proto
, 0), "UDP %s",
573 inet_ntoa(ip
->ip_src
));
575 ksnprintf(SNPARGS(proto
, len
), ":%d %s:%d",
576 ntohs(udp
->uh_sport
),
577 inet_ntoa(ip
->ip_dst
),
578 ntohs(udp
->uh_dport
));
580 ksnprintf(SNPARGS(proto
, len
), " %s",
581 inet_ntoa(ip
->ip_dst
));
586 len
= ksnprintf(SNPARGS(proto
, 0),
588 icmp
->icmp_type
, icmp
->icmp_code
);
590 len
= ksnprintf(SNPARGS(proto
, 0), "ICMP ");
591 len
+= ksnprintf(SNPARGS(proto
, len
), "%s",
592 inet_ntoa(ip
->ip_src
));
593 ksnprintf(SNPARGS(proto
, len
), " %s",
594 inet_ntoa(ip
->ip_dst
));
598 len
= ksnprintf(SNPARGS(proto
, 0), "P:%d %s", ip
->ip_p
,
599 inet_ntoa(ip
->ip_src
));
600 ksnprintf(SNPARGS(proto
, len
), " %s",
601 inet_ntoa(ip
->ip_dst
));
605 if (ip_off
& (IP_MF
| IP_OFFMASK
))
606 ksnprintf(SNPARGS(fragment
, 0), " (frag %d:%d@%d%s)",
607 ntohs(ip
->ip_id
), ip_len
- (ip
->ip_hl
<< 2),
609 (ip_off
& IP_MF
) ? "+" : "");
611 if (oif
|| m
->m_pkthdr
.rcvif
)
612 log(LOG_SECURITY
| LOG_INFO
,
613 "ipfw: %d %s %s %s via %s%s\n",
615 action
, proto
, oif
? "out" : "in",
616 oif
? oif
->if_xname
: m
->m_pkthdr
.rcvif
->if_xname
,
619 log(LOG_SECURITY
| LOG_INFO
,
620 "ipfw: %d %s %s [no if info]%s\n",
622 action
, proto
, fragment
);
624 log(LOG_SECURITY
| LOG_NOTICE
,
625 "ipfw: limit %d reached on entry %d\n",
626 limit_reached
, f
? f
->rulenum
: -1);
630 * IMPORTANT: the hash function for dynamic rules must be commutative
631 * in source and destination (ip,port), because rules are bidirectional
632 * and we want to find both in the same bucket.
635 hash_packet(struct ipfw_flow_id
*id
)
639 i
= (id
->dst_ip
) ^ (id
->src_ip
) ^ (id
->dst_port
) ^ (id
->src_port
);
640 i
&= (curr_dyn_buckets
- 1);
645 * unlink a dynamic rule from a chain. prev is a pointer to
646 * the previous one, q is a pointer to the rule to delete,
647 * head is a pointer to the head of the queue.
648 * Modifies q and potentially also head.
650 #define UNLINK_DYN_RULE(prev, head, q) { \
651 ipfw_dyn_rule *old_q = q; \
653 /* remove a refcount to the parent */ \
654 if (q->dyn_type == O_LIMIT) \
655 q->parent->count--; \
656 DEB(kprintf("-- unlink entry 0x%08x %d -> 0x%08x %d, %d left\n", \
657 (q->id.src_ip), (q->id.src_port), \
658 (q->id.dst_ip), (q->id.dst_port), dyn_count-1 ); ) \
660 prev->next = q = q->next; \
662 head = q = q->next; \
663 KASSERT(dyn_count > 0, ("invalid dyn count %u\n", dyn_count)); \
665 kfree(old_q, M_IPFW); }
667 #define TIME_LEQ(a,b) ((int)((a)-(b)) <= 0)
670 * Remove dynamic rules pointing to "rule", or all of them if rule == NULL.
672 * If keep_me == NULL, rules are deleted even if not expired,
673 * otherwise only expired rules are removed.
675 * The value of the second parameter is also used to point to identify
676 * a rule we absolutely do not want to remove (e.g. because we are
677 * holding a reference to it -- this is the case with O_LIMIT_PARENT
678 * rules). The pointer is only used for comparison, so any non-null
682 remove_dyn_rule(struct ip_fw
*rule
, ipfw_dyn_rule
*keep_me
)
684 static uint32_t last_remove
= 0;
686 #define FORCE (keep_me == NULL)
688 ipfw_dyn_rule
*prev
, *q
;
689 int i
, pass
= 0, max_pass
= 0;
691 if (ipfw_dyn_v
== NULL
|| dyn_count
== 0)
693 /* do not expire more than once per second, it is useless */
694 if (!FORCE
&& last_remove
== time_second
)
696 last_remove
= time_second
;
699 * because O_LIMIT refer to parent rules, during the first pass only
700 * remove child and mark any pending LIMIT_PARENT, and remove
701 * them in a second pass.
704 for (i
= 0 ; i
< curr_dyn_buckets
; i
++) {
705 for (prev
=NULL
, q
= ipfw_dyn_v
[i
] ; q
; ) {
707 * Logic can become complex here, so we split tests.
711 if (rule
!= NULL
&& rule
!= q
->rule
)
712 goto next
; /* not the one we are looking for */
713 if (q
->dyn_type
== O_LIMIT_PARENT
) {
715 * handle parent in the second pass,
716 * record we need one.
721 if (FORCE
&& q
->count
!= 0 ) {
722 /* XXX should not happen! */
723 kprintf( "OUCH! cannot remove rule,"
724 " count %d\n", q
->count
);
728 !TIME_LEQ( q
->expire
, time_second
))
731 UNLINK_DYN_RULE(prev
, ipfw_dyn_v
[i
], q
);
738 if (pass
++ < max_pass
)
744 * lookup a dynamic rule.
746 static ipfw_dyn_rule
*
747 lookup_dyn_rule(struct ipfw_flow_id
*pkt
, int *match_direction
,
751 * stateful ipfw extensions.
752 * Lookup into dynamic session queue
754 #define MATCH_REVERSE 0
755 #define MATCH_FORWARD 1
757 #define MATCH_UNKNOWN 3
758 int i
, dir
= MATCH_NONE
;
759 ipfw_dyn_rule
*prev
, *q
=NULL
;
761 if (ipfw_dyn_v
== NULL
)
762 goto done
; /* not found */
763 i
= hash_packet( pkt
);
764 for (prev
=NULL
, q
= ipfw_dyn_v
[i
] ; q
!= NULL
; ) {
765 if (q
->dyn_type
== O_LIMIT_PARENT
)
767 if (TIME_LEQ( q
->expire
, time_second
)) { /* expire entry */
768 UNLINK_DYN_RULE(prev
, ipfw_dyn_v
[i
], q
);
771 if ( pkt
->proto
== q
->id
.proto
) {
772 if (pkt
->src_ip
== q
->id
.src_ip
&&
773 pkt
->dst_ip
== q
->id
.dst_ip
&&
774 pkt
->src_port
== q
->id
.src_port
&&
775 pkt
->dst_port
== q
->id
.dst_port
) {
779 if (pkt
->src_ip
== q
->id
.dst_ip
&&
780 pkt
->dst_ip
== q
->id
.src_ip
&&
781 pkt
->src_port
== q
->id
.dst_port
&&
782 pkt
->dst_port
== q
->id
.src_port
) {
792 goto done
; /* q = NULL, not found */
794 if ( prev
!= NULL
) { /* found and not in front */
795 prev
->next
= q
->next
;
796 q
->next
= ipfw_dyn_v
[i
];
799 if (pkt
->proto
== IPPROTO_TCP
) { /* update state according to flags */
800 u_char flags
= pkt
->flags
& (TH_FIN
|TH_SYN
|TH_RST
);
802 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
803 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
804 q
->state
|= (dir
== MATCH_FORWARD
) ? flags
: (flags
<< 8);
806 case TH_SYN
: /* opening */
807 q
->expire
= time_second
+ dyn_syn_lifetime
;
810 case BOTH_SYN
: /* move to established */
811 case BOTH_SYN
| TH_FIN
: /* one side tries to close */
812 case BOTH_SYN
| (TH_FIN
<< 8) :
814 #define _SEQ_GE(a,b) ((int)(a) - (int)(b) >= 0)
815 uint32_t ack
= ntohl(tcp
->th_ack
);
816 if (dir
== MATCH_FORWARD
) {
817 if (q
->ack_fwd
== 0 || _SEQ_GE(ack
, q
->ack_fwd
))
819 else { /* ignore out-of-sequence */
823 if (q
->ack_rev
== 0 || _SEQ_GE(ack
, q
->ack_rev
))
825 else { /* ignore out-of-sequence */
830 q
->expire
= time_second
+ dyn_ack_lifetime
;
833 case BOTH_SYN
| BOTH_FIN
: /* both sides closed */
834 if (dyn_fin_lifetime
>= dyn_keepalive_period
)
835 dyn_fin_lifetime
= dyn_keepalive_period
- 1;
836 q
->expire
= time_second
+ dyn_fin_lifetime
;
842 * reset or some invalid combination, but can also
843 * occur if we use keep-state the wrong way.
845 if ( (q
->state
& ((TH_RST
<< 8)|TH_RST
)) == 0)
846 kprintf("invalid state: 0x%x\n", q
->state
);
848 if (dyn_rst_lifetime
>= dyn_keepalive_period
)
849 dyn_rst_lifetime
= dyn_keepalive_period
- 1;
850 q
->expire
= time_second
+ dyn_rst_lifetime
;
853 } else if (pkt
->proto
== IPPROTO_UDP
) {
854 q
->expire
= time_second
+ dyn_udp_lifetime
;
856 /* other protocols */
857 q
->expire
= time_second
+ dyn_short_lifetime
;
861 *match_direction
= dir
;
866 realloc_dynamic_table(void)
869 * Try reallocation, make sure we have a power of 2 and do
870 * not allow more than 64k entries. In case of overflow,
874 if (dyn_buckets
> 65536)
876 if ((dyn_buckets
& (dyn_buckets
-1)) != 0) { /* not a power of 2 */
877 dyn_buckets
= curr_dyn_buckets
; /* reset */
880 curr_dyn_buckets
= dyn_buckets
;
881 if (ipfw_dyn_v
!= NULL
)
882 kfree(ipfw_dyn_v
, M_IPFW
);
884 ipfw_dyn_v
= kmalloc(curr_dyn_buckets
* sizeof(ipfw_dyn_rule
*),
885 M_IPFW
, M_INTWAIT
| M_NULLOK
| M_ZERO
);
886 if (ipfw_dyn_v
!= NULL
|| curr_dyn_buckets
<= 2)
888 curr_dyn_buckets
/= 2;
893 * Install state of type 'type' for a dynamic session.
894 * The hash table contains two type of rules:
895 * - regular rules (O_KEEP_STATE)
896 * - rules for sessions with limited number of sess per user
897 * (O_LIMIT). When they are created, the parent is
898 * increased by 1, and decreased on delete. In this case,
899 * the third parameter is the parent rule and not the chain.
900 * - "parent" rules for the above (O_LIMIT_PARENT).
902 static ipfw_dyn_rule
*
903 add_dyn_rule(struct ipfw_flow_id
*id
, uint8_t dyn_type
, struct ip_fw
*rule
)
908 if (ipfw_dyn_v
== NULL
||
909 (dyn_count
== 0 && dyn_buckets
!= curr_dyn_buckets
)) {
910 realloc_dynamic_table();
911 if (ipfw_dyn_v
== NULL
)
912 return NULL
; /* failed ! */
916 r
= kmalloc(sizeof *r
, M_IPFW
, M_INTWAIT
| M_NULLOK
| M_ZERO
);
918 kprintf ("sorry cannot allocate state\n");
922 /* increase refcount on parent, and set pointer */
923 if (dyn_type
== O_LIMIT
) {
924 ipfw_dyn_rule
*parent
= (ipfw_dyn_rule
*)rule
;
925 if ( parent
->dyn_type
!= O_LIMIT_PARENT
)
926 panic("invalid parent");
933 r
->expire
= time_second
+ dyn_syn_lifetime
;
935 r
->dyn_type
= dyn_type
;
936 r
->pcnt
= r
->bcnt
= 0;
940 r
->next
= ipfw_dyn_v
[i
];
943 DEB(kprintf("-- add dyn entry ty %d 0x%08x %d -> 0x%08x %d, total %d\n",
945 (r
->id
.src_ip
), (r
->id
.src_port
),
946 (r
->id
.dst_ip
), (r
->id
.dst_port
),
952 * lookup dynamic parent rule using pkt and rule as search keys.
953 * If the lookup fails, then install one.
955 static ipfw_dyn_rule
*
956 lookup_dyn_parent(struct ipfw_flow_id
*pkt
, struct ip_fw
*rule
)
962 i
= hash_packet( pkt
);
963 for (q
= ipfw_dyn_v
[i
] ; q
!= NULL
; q
=q
->next
)
964 if (q
->dyn_type
== O_LIMIT_PARENT
&&
966 pkt
->proto
== q
->id
.proto
&&
967 pkt
->src_ip
== q
->id
.src_ip
&&
968 pkt
->dst_ip
== q
->id
.dst_ip
&&
969 pkt
->src_port
== q
->id
.src_port
&&
970 pkt
->dst_port
== q
->id
.dst_port
) {
971 q
->expire
= time_second
+ dyn_short_lifetime
;
972 DEB(kprintf("lookup_dyn_parent found 0x%p\n",q
);)
976 return add_dyn_rule(pkt
, O_LIMIT_PARENT
, rule
);
980 * Install dynamic state for rule type cmd->o.opcode
982 * Returns 1 (failure) if state is not installed because of errors or because
983 * session limitations are enforced.
986 install_state(struct ip_fw
*rule
, ipfw_insn_limit
*cmd
,
987 struct ip_fw_args
*args
)
993 DEB(kprintf("-- install state type %d 0x%08x %u -> 0x%08x %u\n",
995 (args
->f_id
.src_ip
), (args
->f_id
.src_port
),
996 (args
->f_id
.dst_ip
), (args
->f_id
.dst_port
) );)
998 q
= lookup_dyn_rule(&args
->f_id
, NULL
, NULL
);
1000 if (q
!= NULL
) { /* should never occur */
1001 if (last_log
!= time_second
) {
1002 last_log
= time_second
;
1003 kprintf(" install_state: entry already present, done\n");
1008 if (dyn_count
>= dyn_max
)
1010 * Run out of slots, try to remove any expired rule.
1012 remove_dyn_rule(NULL
, (ipfw_dyn_rule
*)1);
1014 if (dyn_count
>= dyn_max
) {
1015 if (last_log
!= time_second
) {
1016 last_log
= time_second
;
1017 kprintf("install_state: Too many dynamic rules\n");
1019 return 1; /* cannot install, notify caller */
1022 switch (cmd
->o
.opcode
) {
1023 case O_KEEP_STATE
: /* bidir rule */
1024 add_dyn_rule(&args
->f_id
, O_KEEP_STATE
, rule
);
1027 case O_LIMIT
: /* limit number of sessions */
1029 uint16_t limit_mask
= cmd
->limit_mask
;
1030 struct ipfw_flow_id id
;
1031 ipfw_dyn_rule
*parent
;
1033 DEB(kprintf("installing dyn-limit rule %d\n", cmd
->conn_limit
);)
1035 id
.dst_ip
= id
.src_ip
= 0;
1036 id
.dst_port
= id
.src_port
= 0;
1037 id
.proto
= args
->f_id
.proto
;
1039 if (limit_mask
& DYN_SRC_ADDR
)
1040 id
.src_ip
= args
->f_id
.src_ip
;
1041 if (limit_mask
& DYN_DST_ADDR
)
1042 id
.dst_ip
= args
->f_id
.dst_ip
;
1043 if (limit_mask
& DYN_SRC_PORT
)
1044 id
.src_port
= args
->f_id
.src_port
;
1045 if (limit_mask
& DYN_DST_PORT
)
1046 id
.dst_port
= args
->f_id
.dst_port
;
1047 parent
= lookup_dyn_parent(&id
, rule
);
1048 if (parent
== NULL
) {
1049 kprintf("add parent failed\n");
1052 if (parent
->count
>= cmd
->conn_limit
) {
1054 * See if we can remove some expired rule.
1056 remove_dyn_rule(rule
, parent
);
1057 if (parent
->count
>= cmd
->conn_limit
) {
1058 if (fw_verbose
&& last_log
!= time_second
) {
1059 last_log
= time_second
;
1060 log(LOG_SECURITY
| LOG_DEBUG
,
1061 "drop session, too many entries\n");
1066 add_dyn_rule(&args
->f_id
, O_LIMIT
, (struct ip_fw
*)parent
);
1070 kprintf("unknown dynamic rule type %u\n", cmd
->o
.opcode
);
1073 lookup_dyn_rule(&args
->f_id
, NULL
, NULL
); /* XXX just set lifetime */
1078 * Transmit a TCP packet, containing either a RST or a keepalive.
1079 * When flags & TH_RST, we are sending a RST packet, because of a
1080 * "reset" action matched the packet.
1081 * Otherwise we are sending a keepalive, and flags & TH_
1084 send_pkt(struct ipfw_flow_id
*id
, uint32_t seq
, uint32_t ack
, int flags
)
1089 struct route sro
; /* fake route */
1091 MGETHDR(m
, MB_DONTWAIT
, MT_HEADER
);
1094 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
1095 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct ip
) + sizeof(struct tcphdr
);
1096 m
->m_data
+= max_linkhdr
;
1098 ip
= mtod(m
, struct ip
*);
1099 bzero(ip
, m
->m_len
);
1100 tcp
= (struct tcphdr
*)(ip
+ 1); /* no IP options */
1101 ip
->ip_p
= IPPROTO_TCP
;
1104 * Assume we are sending a RST (or a keepalive in the reverse
1105 * direction), swap src and destination addresses and ports.
1107 ip
->ip_src
.s_addr
= htonl(id
->dst_ip
);
1108 ip
->ip_dst
.s_addr
= htonl(id
->src_ip
);
1109 tcp
->th_sport
= htons(id
->dst_port
);
1110 tcp
->th_dport
= htons(id
->src_port
);
1111 if (flags
& TH_RST
) { /* we are sending a RST */
1112 if (flags
& TH_ACK
) {
1113 tcp
->th_seq
= htonl(ack
);
1114 tcp
->th_ack
= htonl(0);
1115 tcp
->th_flags
= TH_RST
;
1119 tcp
->th_seq
= htonl(0);
1120 tcp
->th_ack
= htonl(seq
);
1121 tcp
->th_flags
= TH_RST
| TH_ACK
;
1125 * We are sending a keepalive. flags & TH_SYN determines
1126 * the direction, forward if set, reverse if clear.
1127 * NOTE: seq and ack are always assumed to be correct
1128 * as set by the caller. This may be confusing...
1130 if (flags
& TH_SYN
) {
1132 * we have to rewrite the correct addresses!
1134 ip
->ip_dst
.s_addr
= htonl(id
->dst_ip
);
1135 ip
->ip_src
.s_addr
= htonl(id
->src_ip
);
1136 tcp
->th_dport
= htons(id
->dst_port
);
1137 tcp
->th_sport
= htons(id
->src_port
);
1139 tcp
->th_seq
= htonl(seq
);
1140 tcp
->th_ack
= htonl(ack
);
1141 tcp
->th_flags
= TH_ACK
;
1144 * set ip_len to the payload size so we can compute
1145 * the tcp checksum on the pseudoheader
1146 * XXX check this, could save a couple of words ?
1148 ip
->ip_len
= htons(sizeof(struct tcphdr
));
1149 tcp
->th_sum
= in_cksum(m
, m
->m_pkthdr
.len
);
1151 * now fill fields left out earlier
1153 ip
->ip_ttl
= ip_defttl
;
1154 ip
->ip_len
= m
->m_pkthdr
.len
;
1155 bzero (&sro
, sizeof (sro
));
1156 ip_rtaddr(ip
->ip_dst
, &sro
);
1157 m
->m_pkthdr
.fw_flags
|= IPFW_MBUF_GENERATED
;
1158 ip_output(m
, NULL
, &sro
, 0, NULL
, NULL
);
1164 * sends a reject message, consuming the mbuf passed as an argument.
1167 send_reject(struct ip_fw_args
*args
, int code
, int offset
, int ip_len
)
1170 if (code
!= ICMP_REJECT_RST
) { /* Send an ICMP unreach */
1171 /* We need the IP header in host order for icmp_error(). */
1172 if (args
->eh
!= NULL
) {
1173 struct ip
*ip
= mtod(args
->m
, struct ip
*);
1174 ip
->ip_len
= ntohs(ip
->ip_len
);
1175 ip
->ip_off
= ntohs(ip
->ip_off
);
1177 icmp_error(args
->m
, ICMP_UNREACH
, code
, 0L, 0);
1178 } else if (offset
== 0 && args
->f_id
.proto
== IPPROTO_TCP
) {
1179 struct tcphdr
*const tcp
=
1180 L3HDR(struct tcphdr
, mtod(args
->m
, struct ip
*));
1181 if ( (tcp
->th_flags
& TH_RST
) == 0)
1182 send_pkt(&(args
->f_id
), ntohl(tcp
->th_seq
),
1184 tcp
->th_flags
| TH_RST
);
1193 * Given an ip_fw *, lookup_next_rule will return a pointer
1194 * to the next rule, which can be either the jump
1195 * target (for skipto instructions) or the next one in the list (in
1196 * all other cases including a missing jump target).
1197 * The result is also written in the "next_rule" field of the rule.
1198 * Backward jumps are not allowed, so start looking from the next
1201 * This never returns NULL -- in case we do not have an exact match,
1202 * the next rule is returned. When the ruleset is changed,
1203 * pointers are flushed so we are always correct.
1206 static struct ip_fw
*
1207 lookup_next_rule(struct ip_fw
*me
)
1209 struct ip_fw
*rule
= NULL
;
1212 /* look for action, in case it is a skipto */
1213 cmd
= ACTION_PTR(me
);
1214 if (cmd
->opcode
== O_LOG
)
1216 if ( cmd
->opcode
== O_SKIPTO
)
1217 for (rule
= me
->next
; rule
; rule
= rule
->next
)
1218 if (rule
->rulenum
>= cmd
->arg1
)
1220 if (rule
== NULL
) /* failure or not a skipto */
1222 me
->next_rule
= rule
;
1227 * The main check routine for the firewall.
1229 * All arguments are in args so we can modify them and return them
1230 * back to the caller.
1234 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1235 * Starts with the IP header.
1236 * args->eh (in) Mac header if present, or NULL for layer3 packet.
1237 * args->oif Outgoing interface, or NULL if packet is incoming.
1238 * The incoming interface is in the mbuf. (in)
1240 * args->rule Pointer to the last matching rule (in/out)
1241 * args->next_hop Socket we are forwarding to (out).
1242 * args->f_id Addresses grabbed from the packet (out)
1246 * IP_FW_PORT_DENY_FLAG the packet must be dropped.
1247 * 0 The packet is to be accepted and routed normally OR
1248 * the packet was denied/rejected and has been dropped;
1249 * in the latter case, *m is equal to NULL upon return.
1250 * port Divert the packet to port, with these caveats:
1252 * - If IP_FW_PORT_TEE_FLAG is set, tee the packet instead
1253 * of diverting it (ie, 'ipfw tee').
1255 * - If IP_FW_PORT_DYNT_FLAG is set, interpret the lower
1256 * 16 bits as a dummynet pipe number instead of diverting
1260 ipfw_chk(struct ip_fw_args
*args
)
1263 * Local variables hold state during the processing of a packet.
1265 * IMPORTANT NOTE: to speed up the processing of rules, there
1266 * are some assumption on the values of the variables, which
1267 * are documented here. Should you change them, please check
1268 * the implementation of the various instructions to make sure
1269 * that they still work.
1271 * args->eh The MAC header. It is non-null for a layer2
1272 * packet, it is NULL for a layer-3 packet.
1274 * m | args->m Pointer to the mbuf, as received from the caller.
1275 * It may change if ipfw_chk() does an m_pullup, or if it
1276 * consumes the packet because it calls send_reject().
1277 * XXX This has to change, so that ipfw_chk() never modifies
1278 * or consumes the buffer.
1279 * ip is simply an alias of the value of m, and it is kept
1280 * in sync with it (the packet is supposed to start with
1283 struct mbuf
*m
= args
->m
;
1284 struct ip
*ip
= mtod(m
, struct ip
*);
1287 * oif | args->oif If NULL, ipfw_chk has been called on the
1288 * inbound path (ether_input, ip_input).
1289 * If non-NULL, ipfw_chk has been called on the outbound path
1290 * (ether_output, ip_output).
1292 struct ifnet
*oif
= args
->oif
;
1294 struct ip_fw
*f
= NULL
; /* matching rule */
1299 * hlen The length of the IPv4 header.
1300 * hlen >0 means we have an IPv4 packet.
1302 u_int hlen
= 0; /* hlen >0 means we have an IP pkt */
1305 * offset The offset of a fragment. offset != 0 means that
1306 * we have a fragment at this offset of an IPv4 packet.
1307 * offset == 0 means that (if this is an IPv4 packet)
1308 * this is the first or only fragment.
1313 * Local copies of addresses. They are only valid if we have
1316 * proto The protocol. Set to 0 for non-ip packets,
1317 * or to the protocol read from the packet otherwise.
1318 * proto != 0 means that we have an IPv4 packet.
1320 * src_port, dst_port port numbers, in HOST format. Only
1321 * valid for TCP and UDP packets.
1323 * src_ip, dst_ip ip addresses, in NETWORK format.
1324 * Only valid for IPv4 packets.
1327 uint16_t src_port
= 0, dst_port
= 0; /* NOTE: host format */
1328 struct in_addr src_ip
, dst_ip
; /* NOTE: network format */
1330 int dyn_dir
= MATCH_UNKNOWN
;
1331 ipfw_dyn_rule
*q
= NULL
;
1333 if (m
->m_pkthdr
.fw_flags
& IPFW_MBUF_GENERATED
)
1334 return 0; /* accept */
1336 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1337 * MATCH_NONE when checked and not matched (q = NULL),
1338 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
1341 if (args
->eh
== NULL
|| /* layer 3 packet */
1342 ( m
->m_pkthdr
.len
>= sizeof(struct ip
) &&
1343 ntohs(args
->eh
->ether_type
) == ETHERTYPE_IP
))
1344 hlen
= ip
->ip_hl
<< 2;
1347 * Collect parameters into local variables for faster matching.
1349 if (hlen
== 0) { /* do not grab addresses for non-ip pkts */
1350 proto
= args
->f_id
.proto
= 0; /* mark f_id invalid */
1351 goto after_ip_checks
;
1354 proto
= args
->f_id
.proto
= ip
->ip_p
;
1355 src_ip
= ip
->ip_src
;
1356 dst_ip
= ip
->ip_dst
;
1357 if (args
->eh
!= NULL
) { /* layer 2 packets are as on the wire */
1358 offset
= ntohs(ip
->ip_off
) & IP_OFFMASK
;
1359 ip_len
= ntohs(ip
->ip_len
);
1361 offset
= ip
->ip_off
& IP_OFFMASK
;
1362 ip_len
= ip
->ip_len
;
1365 #define PULLUP_TO(len) \
1367 if ((m)->m_len < (len)) { \
1368 args->m = m = m_pullup(m, (len)); \
1370 goto pullup_failed; \
1371 ip = mtod(m, struct ip *); \
1381 PULLUP_TO(hlen
+ sizeof(struct tcphdr
));
1382 tcp
= L3HDR(struct tcphdr
, ip
);
1383 dst_port
= tcp
->th_dport
;
1384 src_port
= tcp
->th_sport
;
1385 args
->f_id
.flags
= tcp
->th_flags
;
1393 PULLUP_TO(hlen
+ sizeof(struct udphdr
));
1394 udp
= L3HDR(struct udphdr
, ip
);
1395 dst_port
= udp
->uh_dport
;
1396 src_port
= udp
->uh_sport
;
1401 PULLUP_TO(hlen
+ 4); /* type, code and checksum. */
1402 args
->f_id
.flags
= L3HDR(struct icmp
, ip
)->icmp_type
;
1411 args
->f_id
.src_ip
= ntohl(src_ip
.s_addr
);
1412 args
->f_id
.dst_ip
= ntohl(dst_ip
.s_addr
);
1413 args
->f_id
.src_port
= src_port
= ntohs(src_port
);
1414 args
->f_id
.dst_port
= dst_port
= ntohs(dst_port
);
1419 * Packet has already been tagged. Look for the next rule
1420 * to restart processing.
1422 * If fw_one_pass != 0 then just accept it.
1423 * XXX should not happen here, but optimized out in
1429 /* This rule was deleted */
1430 if (args
->rule
->rule_flags
& IPFW_RULE_F_INVALID
)
1431 return IP_FW_PORT_DENY_FLAG
;
1433 f
= args
->rule
->next_rule
;
1435 f
= lookup_next_rule(args
->rule
);
1438 * Find the starting rule. It can be either the first
1439 * one, or the one after divert_rule if asked so.
1443 mtag
= m_tag_find(m
, PACKET_TAG_IPFW_DIVERT
, NULL
);
1445 skipto
= *(uint16_t *)m_tag_data(mtag
);
1450 if (args
->eh
== NULL
&& skipto
!= 0) {
1451 if (skipto
>= IPFW_DEFAULT_RULE
)
1452 return(IP_FW_PORT_DENY_FLAG
); /* invalid */
1453 while (f
&& f
->rulenum
<= skipto
)
1455 if (f
== NULL
) /* drop packet */
1456 return(IP_FW_PORT_DENY_FLAG
);
1459 if ((mtag
= m_tag_find(m
, PACKET_TAG_IPFW_DIVERT
, NULL
)) != NULL
)
1460 m_tag_delete(m
, mtag
);
1463 * Now scan the rules, and parse microinstructions for each rule.
1465 for (; f
; f
= f
->next
) {
1468 int skip_or
; /* skip rest of OR block */
1471 if (set_disable
& (1 << f
->set
) )
1475 for (l
= f
->cmd_len
, cmd
= f
->cmd
; l
> 0 ;
1476 l
-= cmdlen
, cmd
+= cmdlen
) {
1480 * check_body is a jump target used when we find a
1481 * CHECK_STATE, and need to jump to the body of
1486 cmdlen
= F_LEN(cmd
);
1488 * An OR block (insn_1 || .. || insn_n) has the
1489 * F_OR bit set in all but the last instruction.
1490 * The first match will set "skip_or", and cause
1491 * the following instructions to be skipped until
1492 * past the one with the F_OR bit clear.
1494 if (skip_or
) { /* skip this instruction */
1495 if ((cmd
->len
& F_OR
) == 0)
1496 skip_or
= 0; /* next one is good */
1499 match
= 0; /* set to 1 if we succeed */
1501 switch (cmd
->opcode
) {
1503 * The first set of opcodes compares the packet's
1504 * fields with some pattern, setting 'match' if a
1505 * match is found. At the end of the loop there is
1506 * logic to deal with F_NOT and F_OR flags associated
1514 kprintf("ipfw: opcode %d unimplemented\n",
1521 * We only check offset == 0 && proto != 0,
1522 * as this ensures that we have an IPv4
1523 * packet with the ports info.
1528 struct inpcbinfo
*pi
;
1532 if (proto
== IPPROTO_TCP
) {
1534 pi
= &tcbinfo
[mycpu
->gd_cpuid
];
1535 } else if (proto
== IPPROTO_UDP
) {
1542 in_pcblookup_hash(pi
,
1543 dst_ip
, htons(dst_port
),
1544 src_ip
, htons(src_port
),
1546 in_pcblookup_hash(pi
,
1547 src_ip
, htons(src_port
),
1548 dst_ip
, htons(dst_port
),
1551 if (pcb
== NULL
|| pcb
->inp_socket
== NULL
)
1553 #if defined(__DragonFly__) || (defined(__FreeBSD__) && __FreeBSD_version < 500034)
1554 #define socheckuid(a,b) ((a)->so_cred->cr_uid != (b))
1556 if (cmd
->opcode
== O_UID
) {
1558 !socheckuid(pcb
->inp_socket
,
1559 (uid_t
)((ipfw_insn_u32
*)cmd
)->d
[0]);
1561 match
= groupmember(
1562 (uid_t
)((ipfw_insn_u32
*)cmd
)->d
[0],
1563 pcb
->inp_socket
->so_cred
);
1569 match
= iface_match(m
->m_pkthdr
.rcvif
,
1570 (ipfw_insn_if
*)cmd
);
1574 match
= iface_match(oif
, (ipfw_insn_if
*)cmd
);
1578 match
= iface_match(oif
? oif
:
1579 m
->m_pkthdr
.rcvif
, (ipfw_insn_if
*)cmd
);
1583 if (args
->eh
!= NULL
) { /* have MAC header */
1584 uint32_t *want
= (uint32_t *)
1585 ((ipfw_insn_mac
*)cmd
)->addr
;
1586 uint32_t *mask
= (uint32_t *)
1587 ((ipfw_insn_mac
*)cmd
)->mask
;
1588 uint32_t *hdr
= (uint32_t *)args
->eh
;
1591 ( want
[0] == (hdr
[0] & mask
[0]) &&
1592 want
[1] == (hdr
[1] & mask
[1]) &&
1593 want
[2] == (hdr
[2] & mask
[2]) );
1598 if (args
->eh
!= NULL
) {
1600 ntohs(args
->eh
->ether_type
);
1602 ((ipfw_insn_u16
*)cmd
)->ports
;
1605 for (i
= cmdlen
- 1; !match
&& i
>0;
1607 match
= (t
>=p
[0] && t
<=p
[1]);
1612 match
= (hlen
> 0 && offset
!= 0);
1615 case O_IN
: /* "out" is "not in" */
1616 match
= (oif
== NULL
);
1620 match
= (args
->eh
!= NULL
);
1625 * We do not allow an arg of 0 so the
1626 * check of "proto" only suffices.
1628 match
= (proto
== cmd
->arg1
);
1632 match
= (hlen
> 0 &&
1633 ((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1638 match
= (hlen
> 0 &&
1639 ((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1641 ((ipfw_insn_ip
*)cmd
)->mask
.s_addr
));
1648 INADDR_TO_IFP(src_ip
, tif
);
1649 match
= (tif
!= NULL
);
1656 uint32_t *d
= (uint32_t *)(cmd
+1);
1658 cmd
->opcode
== O_IP_DST_SET
?
1664 addr
-= d
[0]; /* subtract base */
1665 match
= (addr
< cmd
->arg1
) &&
1666 ( d
[ 1 + (addr
>>5)] &
1667 (1<<(addr
& 0x1f)) );
1672 match
= (hlen
> 0 &&
1673 ((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1678 match
= (hlen
> 0) &&
1679 (((ipfw_insn_ip
*)cmd
)->addr
.s_addr
==
1681 ((ipfw_insn_ip
*)cmd
)->mask
.s_addr
));
1688 INADDR_TO_IFP(dst_ip
, tif
);
1689 match
= (tif
!= NULL
);
1696 * offset == 0 && proto != 0 is enough
1697 * to guarantee that we have an IPv4
1698 * packet with port info.
1700 if ((proto
==IPPROTO_UDP
|| proto
==IPPROTO_TCP
)
1703 (cmd
->opcode
== O_IP_SRCPORT
) ?
1704 src_port
: dst_port
;
1706 ((ipfw_insn_u16
*)cmd
)->ports
;
1709 for (i
= cmdlen
- 1; !match
&& i
>0;
1711 match
= (x
>=p
[0] && x
<=p
[1]);
1716 match
= (offset
== 0 && proto
==IPPROTO_ICMP
&&
1717 icmptype_match(ip
, (ipfw_insn_u32
*)cmd
) );
1721 match
= (hlen
> 0 && ipopts_match(ip
, cmd
) );
1725 match
= (hlen
> 0 && cmd
->arg1
== ip
->ip_v
);
1729 match
= (hlen
> 0 && cmd
->arg1
== ip
->ip_ttl
);
1733 match
= (hlen
> 0 &&
1734 cmd
->arg1
== ntohs(ip
->ip_id
));
1738 match
= (hlen
> 0 && cmd
->arg1
== ip_len
);
1741 case O_IPPRECEDENCE
:
1742 match
= (hlen
> 0 &&
1743 (cmd
->arg1
== (ip
->ip_tos
& 0xe0)) );
1747 match
= (hlen
> 0 &&
1748 flags_match(cmd
, ip
->ip_tos
));
1752 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1754 L3HDR(struct tcphdr
,ip
)->th_flags
));
1758 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1759 tcpopts_match(ip
, cmd
));
1763 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1764 ((ipfw_insn_u32
*)cmd
)->d
[0] ==
1765 L3HDR(struct tcphdr
,ip
)->th_seq
);
1769 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1770 ((ipfw_insn_u32
*)cmd
)->d
[0] ==
1771 L3HDR(struct tcphdr
,ip
)->th_ack
);
1775 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1777 L3HDR(struct tcphdr
,ip
)->th_win
);
1781 /* reject packets which have SYN only */
1782 /* XXX should i also check for TH_ACK ? */
1783 match
= (proto
== IPPROTO_TCP
&& offset
== 0 &&
1784 (L3HDR(struct tcphdr
,ip
)->th_flags
&
1785 (TH_RST
| TH_ACK
| TH_SYN
)) != TH_SYN
);
1790 ipfw_log(f
, hlen
, args
->eh
, m
, oif
);
1795 match
= (krandom() <
1796 ((ipfw_insn_u32
*)cmd
)->d
[0]);
1800 * The second set of opcodes represents 'actions',
1801 * i.e. the terminal part of a rule once the packet
1802 * matches all previous patterns.
1803 * Typically there is only one action for each rule,
1804 * and the opcode is stored at the end of the rule
1805 * (but there are exceptions -- see below).
1807 * In general, here we set retval and terminate the
1808 * outer loop (would be a 'break 3' in some language,
1809 * but we need to do a 'goto done').
1812 * O_COUNT and O_SKIPTO actions:
1813 * instead of terminating, we jump to the next rule
1814 * ('goto next_rule', equivalent to a 'break 2'),
1815 * or to the SKIPTO target ('goto again' after
1816 * having set f, cmd and l), respectively.
1818 * O_LIMIT and O_KEEP_STATE: these opcodes are
1819 * not real 'actions', and are stored right
1820 * before the 'action' part of the rule.
1821 * These opcodes try to install an entry in the
1822 * state tables; if successful, we continue with
1823 * the next opcode (match=1; break;), otherwise
1824 * the packet * must be dropped
1825 * ('goto done' after setting retval);
1827 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
1828 * cause a lookup of the state table, and a jump
1829 * to the 'action' part of the parent rule
1830 * ('goto check_body') if an entry is found, or
1831 * (CHECK_STATE only) a jump to the next rule if
1832 * the entry is not found ('goto next_rule').
1833 * The result of the lookup is cached to make
1834 * further instances of these opcodes are
1839 if (install_state(f
,
1840 (ipfw_insn_limit
*)cmd
, args
)) {
1841 retval
= IP_FW_PORT_DENY_FLAG
;
1842 goto done
; /* error/limit violation */
1850 * dynamic rules are checked at the first
1851 * keep-state or check-state occurrence,
1852 * with the result being stored in dyn_dir.
1853 * The compiler introduces a PROBE_STATE
1854 * instruction for us when we have a
1855 * KEEP_STATE (because PROBE_STATE needs
1858 if (dyn_dir
== MATCH_UNKNOWN
&&
1859 (q
= lookup_dyn_rule(&args
->f_id
,
1860 &dyn_dir
, proto
== IPPROTO_TCP
?
1861 L3HDR(struct tcphdr
, ip
) : NULL
))
1864 * Found dynamic entry, update stats
1865 * and jump to the 'action' part of
1871 cmd
= ACTION_PTR(f
);
1872 l
= f
->cmd_len
- f
->act_ofs
;
1876 * Dynamic entry not found. If CHECK_STATE,
1877 * skip to next rule, if PROBE_STATE just
1878 * ignore and continue with next opcode.
1880 if (cmd
->opcode
== O_CHECK_STATE
)
1886 retval
= 0; /* accept */
1891 args
->rule
= f
; /* report matching rule */
1892 retval
= cmd
->arg1
| IP_FW_PORT_DYNT_FLAG
;
1897 if (args
->eh
) /* not on layer 2 */
1900 mtag
= m_tag_get(PACKET_TAG_IPFW_DIVERT
,
1901 sizeof(uint16_t), MB_DONTWAIT
);
1903 retval
= IP_FW_PORT_DENY_FLAG
;
1906 *(uint16_t *)m_tag_data(mtag
) = f
->rulenum
;
1907 m_tag_prepend(m
, mtag
);
1908 retval
= (cmd
->opcode
== O_DIVERT
) ?
1910 cmd
->arg1
| IP_FW_PORT_TEE_FLAG
;
1915 f
->pcnt
++; /* update stats */
1917 f
->timestamp
= time_second
;
1918 if (cmd
->opcode
== O_COUNT
)
1921 if (f
->next_rule
== NULL
)
1922 lookup_next_rule(f
);
1928 * Drop the packet and send a reject notice
1929 * if the packet is not ICMP (or is an ICMP
1930 * query), and it is not multicast/broadcast.
1933 (proto
!= IPPROTO_ICMP
||
1934 is_icmp_query(ip
)) &&
1935 !(m
->m_flags
& (M_BCAST
|M_MCAST
)) &&
1936 !IN_MULTICAST(ntohl(dst_ip
.s_addr
))) {
1937 send_reject(args
, cmd
->arg1
,
1943 retval
= IP_FW_PORT_DENY_FLAG
;
1947 if (args
->eh
) /* not valid on layer2 pkts */
1949 if (!q
|| dyn_dir
== MATCH_FORWARD
)
1951 &((ipfw_insn_sa
*)cmd
)->sa
;
1956 panic("-- unknown opcode %d\n", cmd
->opcode
);
1957 } /* end of switch() on opcodes */
1959 if (cmd
->len
& F_NOT
)
1963 if (cmd
->len
& F_OR
)
1966 if (!(cmd
->len
& F_OR
)) /* not an OR block, */
1967 break; /* try next rule */
1970 } /* end of inner for, scan opcodes */
1972 next_rule
:; /* try next rule */
1974 } /* end of outer for, scan rules */
1975 kprintf("+++ ipfw: ouch!, skip past end of rules, denying packet\n");
1976 return(IP_FW_PORT_DENY_FLAG
);
1979 /* Update statistics */
1982 f
->timestamp
= time_second
;
1987 kprintf("pullup failed\n");
1988 return(IP_FW_PORT_DENY_FLAG
);
1992 ipfw_dummynet_io(struct mbuf
*m
, int pipe_nr
, int dir
, struct ip_fw_args
*fwa
)
1997 const struct ipfw_flow_id
*id
;
1998 struct dn_flow_id
*fid
;
2002 mtag
= m_tag_get(PACKET_TAG_DUMMYNET
, sizeof(*pkt
), MB_DONTWAIT
);
2007 m_tag_prepend(m
, mtag
);
2009 pkt
= m_tag_data(mtag
);
2010 bzero(pkt
, sizeof(*pkt
));
2012 cmd
= fwa
->rule
->cmd
+ fwa
->rule
->act_ofs
;
2013 if (cmd
->opcode
== O_LOG
)
2015 KASSERT(cmd
->opcode
== O_PIPE
|| cmd
->opcode
== O_QUEUE
,
2016 ("Rule is not PIPE or QUEUE, opcode %d\n", cmd
->opcode
));
2019 pkt
->dn_flags
= (dir
& DN_FLAGS_DIR_MASK
);
2020 pkt
->ifp
= fwa
->oif
;
2021 pkt
->cpuid
= mycpu
->gd_cpuid
;
2022 pkt
->pipe_nr
= pipe_nr
;
2026 fid
->fid_dst_ip
= id
->dst_ip
;
2027 fid
->fid_src_ip
= id
->src_ip
;
2028 fid
->fid_dst_port
= id
->dst_port
;
2029 fid
->fid_src_port
= id
->src_port
;
2030 fid
->fid_proto
= id
->proto
;
2031 fid
->fid_flags
= id
->flags
;
2033 ipfw_ref_rule(fwa
->rule
);
2034 pkt
->dn_priv
= fwa
->rule
;
2035 pkt
->dn_unref_priv
= ipfw_unref_rule
;
2037 if (cmd
->opcode
== O_PIPE
)
2038 pkt
->dn_flags
|= DN_FLAGS_IS_PIPE
;
2040 if (dir
== DN_TO_IP_OUT
) {
2042 * We need to copy *ro because for ICMP pkts (and maybe
2043 * others) the caller passed a pointer into the stack;
2044 * dst might also be a pointer into *ro so it needs to
2047 pkt
->ro
= *(fwa
->ro
);
2049 fwa
->ro
->ro_rt
->rt_refcnt
++;
2050 if (fwa
->dst
== (struct sockaddr_in
*)&fwa
->ro
->ro_dst
) {
2051 /* 'dst' points into 'ro' */
2052 fwa
->dst
= (struct sockaddr_in
*)&(pkt
->ro
.ro_dst
);
2054 pkt
->dn_dst
= fwa
->dst
;
2055 pkt
->flags
= fwa
->flags
;
2058 m
->m_pkthdr
.fw_flags
|= DUMMYNET_MBUF_TAGGED
;
2063 * When a rule is added/deleted, clear the next_rule pointers in all rules.
2064 * These will be reconstructed on the fly as packets are matched.
2065 * Must be called at splimp().
2068 flush_rule_ptrs(void)
2072 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2073 rule
->next_rule
= NULL
;
2076 static __inline
void
2077 ipfw_inc_static_count(struct ip_fw
*rule
)
2080 static_ioc_len
+= IOC_RULESIZE(rule
);
2083 static __inline
void
2084 ipfw_dec_static_count(struct ip_fw
*rule
)
2086 int l
= IOC_RULESIZE(rule
);
2088 KASSERT(static_count
> 0, ("invalid static count %u\n", static_count
));
2091 KASSERT(static_ioc_len
>= l
,
2092 ("invalid static len %u\n", static_ioc_len
));
2093 static_ioc_len
-= l
;
2096 static struct ip_fw
*
2097 ipfw_create_rule(const struct ipfw_ioc_rule
*ioc_rule
)
2101 rule
= kmalloc(RULESIZE(ioc_rule
), M_IPFW
, M_WAITOK
| M_ZERO
);
2103 rule
->act_ofs
= ioc_rule
->act_ofs
;
2104 rule
->cmd_len
= ioc_rule
->cmd_len
;
2105 rule
->rulenum
= ioc_rule
->rulenum
;
2106 rule
->set
= ioc_rule
->set
;
2107 rule
->usr_flags
= ioc_rule
->usr_flags
;
2109 bcopy(ioc_rule
->cmd
, rule
->cmd
, rule
->cmd_len
* 4 /* XXX */);
2117 * Add a new rule to the list. Copy the rule into a malloc'ed area, then
2118 * possibly create a rule number and add the rule to the list.
2119 * Update the rule_number in the input struct so the caller knows it as well.
2122 ipfw_add_rule(struct ip_fw
**head
, struct ipfw_ioc_rule
*ioc_rule
)
2124 struct ip_fw
*rule
, *f
, *prev
;
2126 KKASSERT(*head
!= NULL
);
2128 rule
= ipfw_create_rule(ioc_rule
);
2133 * If rulenum is 0, find highest numbered rule before the
2134 * default rule, and add autoinc_step
2136 if (autoinc_step
< 1)
2138 else if (autoinc_step
> 1000)
2139 autoinc_step
= 1000;
2140 if (rule
->rulenum
== 0) {
2142 * locate the highest numbered rule before default
2144 for (f
= *head
; f
; f
= f
->next
) {
2145 if (f
->rulenum
== IPFW_DEFAULT_RULE
)
2147 rule
->rulenum
= f
->rulenum
;
2149 if (rule
->rulenum
< IPFW_DEFAULT_RULE
- autoinc_step
)
2150 rule
->rulenum
+= autoinc_step
;
2151 ioc_rule
->rulenum
= rule
->rulenum
;
2155 * Now insert the new rule in the right place in the sorted list.
2157 for (prev
= NULL
, f
= *head
; f
; prev
= f
, f
= f
->next
) {
2158 if (f
->rulenum
> rule
->rulenum
) { /* found the location */
2162 } else { /* head insert */
2171 ipfw_inc_static_count(rule
);
2175 DEB(kprintf("++ installed rule %d, static count now %d\n",
2176 rule
->rulenum
, static_count
);)
2181 * Free storage associated with a static rule (including derived
2183 * The caller is in charge of clearing rule pointers to avoid
2184 * dangling pointers.
2185 * @return a pointer to the next entry.
2186 * Arguments are not checked, so they better be correct.
2187 * Must be called at splimp().
2189 static struct ip_fw
*
2190 delete_rule(struct ip_fw
**head
, struct ip_fw
*prev
, struct ip_fw
*rule
)
2195 remove_dyn_rule(rule
, NULL
/* force removal */);
2200 ipfw_dec_static_count(rule
);
2202 /* Mark the rule as invalid */
2203 rule
->rule_flags
|= IPFW_RULE_F_INVALID
;
2204 rule
->next_rule
= NULL
;
2206 /* Try to free this rule */
2207 ipfw_free_rule(rule
);
2213 * Deletes all rules from a chain (including the default rule
2214 * if the second argument is set).
2215 * Must be called at splimp().
2218 free_chain(struct ip_fw
**chain
, int kill_default
)
2222 flush_rule_ptrs(); /* more efficient to do outside the loop */
2224 while ( (rule
= *chain
) != NULL
&&
2225 (kill_default
|| rule
->rulenum
!= IPFW_DEFAULT_RULE
) )
2226 delete_rule(chain
, NULL
, rule
);
2228 KASSERT(dyn_count
== 0, ("%u dyn rule remains\n", dyn_count
));
2231 ip_fw_default_rule
= NULL
; /* Reset default rule */
2233 if (ipfw_dyn_v
!= NULL
) {
2235 * Free dynamic rules(state) hash table
2237 kfree(ipfw_dyn_v
, M_IPFW
);
2241 KASSERT(static_count
== 0,
2242 ("%u static rules remains\n", static_count
));
2243 KASSERT(static_ioc_len
== 0,
2244 ("%u bytes of static rules remains\n", static_ioc_len
));
2246 KASSERT(static_count
== 1,
2247 ("%u static rules remains\n", static_count
));
2248 KASSERT(static_ioc_len
== IOC_RULESIZE(ip_fw_default_rule
),
2249 ("%u bytes of static rules remains, should be %u\n",
2250 static_ioc_len
, IOC_RULESIZE(ip_fw_default_rule
)));
2255 * Remove all rules with given number, and also do set manipulation.
2257 * The argument is an uint32_t. The low 16 bit are the rule or set number,
2258 * the next 8 bits are the new set, the top 8 bits are the command:
2260 * 0 delete rules with given number
2261 * 1 delete rules with given set number
2262 * 2 move rules with given number to new set
2263 * 3 move rules with given set number to new set
2264 * 4 swap sets with given numbers
2267 del_entry(struct ip_fw
**chain
, uint32_t arg
)
2269 struct ip_fw
*prev
, *rule
;
2271 uint8_t cmd
, new_set
;
2273 rulenum
= arg
& 0xffff;
2274 cmd
= (arg
>> 24) & 0xff;
2275 new_set
= (arg
>> 16) & 0xff;
2281 if (cmd
== 0 || cmd
== 2) {
2282 if (rulenum
== IPFW_DEFAULT_RULE
)
2290 case 0: /* delete rules with given number */
2292 * locate first rule to delete
2294 for (prev
= NULL
, rule
= *chain
;
2295 rule
&& rule
->rulenum
< rulenum
;
2296 prev
= rule
, rule
= rule
->next
)
2298 if (rule
->rulenum
!= rulenum
)
2301 crit_enter(); /* no access to rules while removing */
2303 * flush pointers outside the loop, then delete all matching
2304 * rules. prev remains the same throughout the cycle.
2307 while (rule
&& rule
->rulenum
== rulenum
)
2308 rule
= delete_rule(chain
, prev
, rule
);
2312 case 1: /* delete all rules with given set number */
2315 for (prev
= NULL
, rule
= *chain
; rule
; )
2316 if (rule
->set
== rulenum
)
2317 rule
= delete_rule(chain
, prev
, rule
);
2325 case 2: /* move rules with given number to new set */
2327 for (rule
= *chain
; rule
; rule
= rule
->next
)
2328 if (rule
->rulenum
== rulenum
)
2329 rule
->set
= new_set
;
2333 case 3: /* move rules with given set number to new set */
2335 for (rule
= *chain
; rule
; rule
= rule
->next
)
2336 if (rule
->set
== rulenum
)
2337 rule
->set
= new_set
;
2341 case 4: /* swap two sets */
2343 for (rule
= *chain
; rule
; rule
= rule
->next
)
2344 if (rule
->set
== rulenum
)
2345 rule
->set
= new_set
;
2346 else if (rule
->set
== new_set
)
2347 rule
->set
= rulenum
;
2355 * Clear counters for a specific rule.
2358 clear_counters(struct ip_fw
*rule
, int log_only
)
2360 ipfw_insn_log
*l
= (ipfw_insn_log
*)ACTION_PTR(rule
);
2362 if (log_only
== 0) {
2363 rule
->bcnt
= rule
->pcnt
= 0;
2364 rule
->timestamp
= 0;
2366 if (l
->o
.opcode
== O_LOG
)
2367 l
->log_left
= l
->max_log
;
2371 * Reset some or all counters on firewall rules.
2372 * @arg frwl is null to clear all entries, or contains a specific
2374 * @arg log_only is 1 if we only want to reset logs, zero otherwise.
2377 zero_entry(int rulenum
, int log_only
)
2385 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2386 clear_counters(rule
, log_only
);
2388 msg
= log_only
? "ipfw: All logging counts reset.\n" :
2389 "ipfw: Accounting cleared.\n";
2393 * We can have multiple rules with the same number, so we
2394 * need to clear them all.
2396 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2397 if (rule
->rulenum
== rulenum
) {
2399 while (rule
&& rule
->rulenum
== rulenum
) {
2400 clear_counters(rule
, log_only
);
2407 if (!cleared
) /* we did not find any matching rules */
2409 msg
= log_only
? "ipfw: Entry %d logging count reset.\n" :
2410 "ipfw: Entry %d cleared.\n";
2413 log(LOG_SECURITY
| LOG_NOTICE
, msg
, rulenum
);
2418 * Check validity of the structure before insert.
2419 * Fortunately rules are simple, so this mostly need to check rule sizes.
2422 ipfw_ctl_check_rule(struct ipfw_ioc_rule
*rule
, int size
)
2425 int have_action
= 0;
2428 /* Check for valid size */
2429 if (size
< sizeof(*rule
)) {
2430 kprintf("ipfw: rule too short\n");
2433 l
= IOC_RULESIZE(rule
);
2435 kprintf("ipfw: size mismatch (have %d want %d)\n", size
, l
);
2440 * Now go for the individual checks. Very simple ones, basically only
2441 * instruction sizes.
2443 for (l
= rule
->cmd_len
, cmd
= rule
->cmd
; l
> 0;
2444 l
-= cmdlen
, cmd
+= cmdlen
) {
2445 cmdlen
= F_LEN(cmd
);
2447 kprintf("ipfw: opcode %d size truncated\n",
2451 DEB(kprintf("ipfw: opcode %d\n", cmd
->opcode
);)
2452 switch (cmd
->opcode
) {
2466 case O_IPPRECEDENCE
:
2473 if (cmdlen
!= F_INSN_SIZE(ipfw_insn
))
2485 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_u32
))
2490 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_limit
))
2495 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_log
))
2498 ((ipfw_insn_log
*)cmd
)->log_left
=
2499 ((ipfw_insn_log
*)cmd
)->max_log
;
2505 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_ip
))
2507 if (((ipfw_insn_ip
*)cmd
)->mask
.s_addr
== 0) {
2508 kprintf("ipfw: opcode %d, useless rule\n",
2516 if (cmd
->arg1
== 0 || cmd
->arg1
> 256) {
2517 kprintf("ipfw: invalid set size %d\n",
2521 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_u32
) +
2527 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_mac
))
2533 case O_IP_DSTPORT
: /* XXX artificial limit, 30 port pairs */
2534 if (cmdlen
< 2 || cmdlen
> 31)
2541 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_if
))
2547 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_pipe
))
2552 if (cmdlen
!= F_INSN_SIZE(ipfw_insn_sa
))
2556 case O_FORWARD_MAC
: /* XXX not implemented yet */
2565 if (cmdlen
!= F_INSN_SIZE(ipfw_insn
))
2569 kprintf("ipfw: opcode %d, multiple actions"
2576 kprintf("ipfw: opcode %d, action must be"
2583 kprintf("ipfw: opcode %d, unknown opcode\n",
2588 if (have_action
== 0) {
2589 kprintf("ipfw: missing action\n");
2595 kprintf("ipfw: opcode %d size %d wrong\n",
2596 cmd
->opcode
, cmdlen
);
2601 ipfw_ctl_add_rule(struct sockopt
*sopt
)
2603 struct ipfw_ioc_rule
*ioc_rule
;
2604 uint32_t rule_buf
[IPFW_RULE_SIZE_MAX
];
2608 ioc_rule
= (struct ipfw_ioc_rule
*)rule_buf
;
2609 error
= sooptcopyin(sopt
, ioc_rule
, sizeof(rule_buf
),
2614 size
= sopt
->sopt_valsize
;
2615 error
= ipfw_ctl_check_rule(ioc_rule
, size
);
2619 error
= ipfw_add_rule(&layer3_chain
, ioc_rule
);
2623 if (sopt
->sopt_dir
== SOPT_GET
)
2624 error
= sooptcopyout(sopt
, ioc_rule
, IOC_RULESIZE(ioc_rule
));
2629 ipfw_copy_rule(const struct ip_fw
*rule
, struct ipfw_ioc_rule
*ioc_rule
)
2631 ioc_rule
->act_ofs
= rule
->act_ofs
;
2632 ioc_rule
->cmd_len
= rule
->cmd_len
;
2633 ioc_rule
->rulenum
= rule
->rulenum
;
2634 ioc_rule
->set
= rule
->set
;
2635 ioc_rule
->usr_flags
= rule
->usr_flags
;
2637 ioc_rule
->set_disable
= set_disable
;
2638 ioc_rule
->static_count
= static_count
;
2639 ioc_rule
->static_len
= static_ioc_len
;
2641 ioc_rule
->pcnt
= rule
->pcnt
;
2642 ioc_rule
->bcnt
= rule
->bcnt
;
2643 ioc_rule
->timestamp
= rule
->timestamp
;
2645 bcopy(rule
->cmd
, ioc_rule
->cmd
, ioc_rule
->cmd_len
* 4 /* XXX */);
2647 return ((uint8_t *)ioc_rule
+ IOC_RULESIZE(ioc_rule
));
2651 ipfw_copy_state(const ipfw_dyn_rule
*dyn_rule
,
2652 struct ipfw_ioc_state
*ioc_state
)
2654 const struct ipfw_flow_id
*id
;
2655 struct ipfw_ioc_flowid
*ioc_id
;
2657 ioc_state
->expire
= TIME_LEQ(dyn_rule
->expire
, time_second
) ?
2658 0 : dyn_rule
->expire
- time_second
;
2659 ioc_state
->pcnt
= dyn_rule
->pcnt
;
2660 ioc_state
->bcnt
= dyn_rule
->bcnt
;
2662 ioc_state
->dyn_type
= dyn_rule
->dyn_type
;
2663 ioc_state
->count
= dyn_rule
->count
;
2665 ioc_state
->rulenum
= dyn_rule
->rule
->rulenum
;
2668 ioc_id
= &ioc_state
->id
;
2670 ioc_id
->type
= ETHERTYPE_IP
;
2671 ioc_id
->u
.ip
.dst_ip
= id
->dst_ip
;
2672 ioc_id
->u
.ip
.src_ip
= id
->src_ip
;
2673 ioc_id
->u
.ip
.dst_port
= id
->dst_port
;
2674 ioc_id
->u
.ip
.src_port
= id
->src_port
;
2675 ioc_id
->u
.ip
.proto
= id
->proto
;
2679 ipfw_ctl_get_rules(struct sockopt
*sopt
)
2687 * pass up a copy of the current rules. Static rules
2688 * come first (the last of which has number IPFW_DEFAULT_RULE),
2689 * followed by a possibly empty list of dynamic rule.
2693 size
= static_ioc_len
; /* size of static rules */
2694 if (ipfw_dyn_v
) /* add size of dyn.rules */
2695 size
+= (dyn_count
* sizeof(struct ipfw_ioc_state
));
2698 * XXX todo: if the user passes a short length just to know
2699 * how much room is needed, do not bother filling up the
2700 * buffer, just jump to the sooptcopyout.
2702 bp
= buf
= kmalloc(size
, M_TEMP
, M_WAITOK
| M_ZERO
);
2704 for (rule
= layer3_chain
; rule
; rule
= rule
->next
)
2705 bp
= ipfw_copy_rule(rule
, bp
);
2708 struct ipfw_ioc_state
*ioc_state
;
2712 for (i
= 0; i
< curr_dyn_buckets
; i
++) {
2715 for (p
= ipfw_dyn_v
[i
]; p
!= NULL
;
2716 p
= p
->next
, ioc_state
++)
2717 ipfw_copy_state(p
, ioc_state
);
2723 error
= sooptcopyout(sopt
, buf
, size
);
2729 * {set|get}sockopt parser.
2732 ipfw_ctl(struct sockopt
*sopt
)
2739 * Disallow modifications in really-really secure mode, but still allow
2740 * the logging counters to be reset.
2742 if (sopt
->sopt_name
== IP_FW_ADD
||
2743 (sopt
->sopt_dir
== SOPT_SET
&& sopt
->sopt_name
!= IP_FW_RESETLOG
)) {
2744 if (securelevel
>= 3)
2750 switch (sopt
->sopt_name
) {
2752 error
= ipfw_ctl_get_rules(sopt
);
2757 * Normally we cannot release the lock on each iteration.
2758 * We could do it here only because we start from the head all
2759 * the times so there is no risk of missing some entries.
2760 * On the other hand, the risk is that we end up with
2761 * a very inconsistent ruleset, so better keep the lock
2762 * around the whole cycle.
2764 * XXX this code can be improved by resetting the head of
2765 * the list to point to the default rule, and then freeing
2766 * the old list without the need for a lock.
2770 free_chain(&layer3_chain
, 0 /* keep default rule */);
2775 error
= ipfw_ctl_add_rule(sopt
);
2780 * IP_FW_DEL is used for deleting single rules or sets,
2781 * and (ab)used to atomically manipulate sets. Argument size
2782 * is used to distinguish between the two:
2784 * delete single rule or set of rules,
2785 * or reassign rules (or sets) to a different set.
2786 * 2*sizeof(uint32_t)
2787 * atomic disable/enable sets.
2788 * first uint32_t contains sets to be disabled,
2789 * second uint32_t contains sets to be enabled.
2791 error
= sooptcopyin(sopt
, masks
,
2792 sizeof(masks
), sizeof(masks
[0]));
2796 size
= sopt
->sopt_valsize
;
2797 if (size
== sizeof(masks
[0])) {
2799 * Delete or reassign static rule
2801 error
= del_entry(&layer3_chain
, masks
[0]);
2802 } else if (size
== sizeof(masks
)) {
2804 * Set enable/disable
2809 (set_disable
| masks
[0]) & ~masks
[1] &
2810 ~(1 << 31); /* set 31 always enabled */
2819 case IP_FW_RESETLOG
: /* argument is an int, the rule number */
2822 if (sopt
->sopt_val
!= 0) {
2823 error
= sooptcopyin(sopt
, &rulenum
,
2824 sizeof(int), sizeof(int));
2828 error
= zero_entry(rulenum
, sopt
->sopt_name
== IP_FW_RESETLOG
);
2832 kprintf("ipfw_ctl invalid option %d\n", sopt
->sopt_name
);
2840 * This procedure is only used to handle keepalives. It is invoked
2841 * every dyn_keepalive_period
2844 ipfw_tick(void * __unused unused
)
2849 if (dyn_keepalive
== 0 || ipfw_dyn_v
== NULL
|| dyn_count
== 0)
2853 for (i
= 0 ; i
< curr_dyn_buckets
; i
++) {
2854 for (q
= ipfw_dyn_v
[i
] ; q
; q
= q
->next
) {
2855 if (q
->dyn_type
== O_LIMIT_PARENT
)
2857 if (q
->id
.proto
!= IPPROTO_TCP
)
2859 if ( (q
->state
& BOTH_SYN
) != BOTH_SYN
)
2861 if (TIME_LEQ( time_second
+dyn_keepalive_interval
,
2863 continue; /* too early */
2864 if (TIME_LEQ(q
->expire
, time_second
))
2865 continue; /* too late, rule expired */
2867 send_pkt(&(q
->id
), q
->ack_rev
- 1, q
->ack_fwd
, TH_SYN
);
2868 send_pkt(&(q
->id
), q
->ack_fwd
- 1, q
->ack_rev
, 0);
2873 callout_reset(&ipfw_timeout_h
, dyn_keepalive_period
* hz
,
2878 ipfw_init_default_rule(struct ip_fw
**head
)
2880 struct ip_fw
*def_rule
;
2882 KKASSERT(*head
== NULL
);
2884 def_rule
= kmalloc(sizeof(*def_rule
), M_IPFW
, M_WAITOK
| M_ZERO
);
2886 def_rule
->act_ofs
= 0;
2887 def_rule
->rulenum
= IPFW_DEFAULT_RULE
;
2888 def_rule
->cmd_len
= 1;
2891 def_rule
->cmd
[0].len
= 1;
2892 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
2893 def_rule
->cmd
[0].opcode
= O_ACCEPT
;
2895 def_rule
->cmd
[0].opcode
= O_DENY
;
2898 def_rule
->refcnt
= 1;
2901 ipfw_inc_static_count(def_rule
);
2903 /* Install the default rule */
2904 ip_fw_default_rule
= def_rule
;
2910 ip_fw_chk_ptr
= ipfw_chk
;
2911 ip_fw_ctl_ptr
= ipfw_ctl
;
2912 ip_fw_dn_io_ptr
= ipfw_dummynet_io
;
2914 layer3_chain
= NULL
;
2915 ipfw_init_default_rule(&layer3_chain
);
2917 kprintf("ipfw2 initialized, divert %s, "
2918 "rule-based forwarding enabled, default to %s, logging ",
2924 ip_fw_default_rule
->cmd
[0].opcode
== O_ACCEPT
?
2927 #ifdef IPFIREWALL_VERBOSE
2930 #ifdef IPFIREWALL_VERBOSE_LIMIT
2931 verbose_limit
= IPFIREWALL_VERBOSE_LIMIT
;
2933 if (fw_verbose
== 0)
2934 kprintf("disabled\n");
2935 else if (verbose_limit
== 0)
2936 kprintf("unlimited\n");
2938 kprintf("limited to %d packets/entry by default\n",
2940 callout_init(&ipfw_timeout_h
);
2941 callout_reset(&ipfw_timeout_h
, hz
, ipfw_tick
, NULL
);
2945 ipfw_modevent(module_t mod
, int type
, void *unused
)
2954 kprintf("IP firewall already loaded\n");
2964 kprintf("ipfw statically compiled, cannot unload\n");
2967 if (ipfw_refcnt
!= 0) {
2973 callout_stop(&ipfw_timeout_h
);
2974 ip_fw_chk_ptr
= NULL
;
2975 ip_fw_ctl_ptr
= NULL
;
2976 ip_fw_dn_io_ptr
= NULL
;
2977 free_chain(&layer3_chain
, 1 /* kill default rule */);
2979 kprintf("IP firewall unloaded\n");
2988 static moduledata_t ipfwmod
= {
2993 DECLARE_MODULE(ipfw
, ipfwmod
, SI_SUB_PROTO_END
, SI_ORDER_ANY
);
2994 MODULE_VERSION(ipfw
, 1);