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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
33 * Dynamic rule support for ipfw
39 #error IPFIREWALL requires INET.
41 #include "opt_inet6.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
47 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 #include <sys/syslog.h>
53 #include <net/ethernet.h> /* for ETHERTYPE_IP */
55 #include <net/if_var.h>
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h> /* ip_defttl */
61 #include <netinet/ip_fw.h>
62 #include <netinet/tcp_var.h>
63 #include <netinet/udp.h>
65 #include <netinet/ip6.h> /* IN6_ARE_ADDR_EQUAL */
67 #include <netinet6/in6_var.h>
68 #include <netinet6/ip6_var.h>
71 #include <netpfil/ipfw/ip_fw_private.h>
73 #include <machine/in_cksum.h> /* XXX for in_cksum */
76 #include <security/mac/mac_framework.h>
80 * Description of dynamic rules.
82 * Dynamic rules are stored in lists accessed through a hash table
83 * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
84 * be modified through the sysctl variable dyn_buckets which is
85 * updated when the table becomes empty.
87 * XXX currently there is only one list, ipfw_dyn.
89 * When a packet is received, its address fields are first masked
90 * with the mask defined for the rule, then hashed, then matched
91 * against the entries in the corresponding list.
92 * Dynamic rules can be used for different purposes:
94 * + enforcing limits on the number of sessions;
95 * + in-kernel NAT (not implemented yet)
97 * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
98 * measured in seconds and depending on the flags.
100 * The total number of dynamic rules is equal to UMA zone items count.
101 * The max number of dynamic rules is dyn_max. When we reach
102 * the maximum number of rules we do not create anymore. This is
103 * done to avoid consuming too much memory, but also too much
104 * time when searching on each packet (ideally, we should try instead
105 * to put a limit on the length of the list on each bucket...).
107 * Each dynamic rule holds a pointer to the parent ipfw rule so
108 * we know what action to perform. Dynamic rules are removed when
109 * the parent rule is deleted. This can be changed by dyn_keep_states
112 * There are some limitations with dynamic rules -- we do not
113 * obey the 'randomized match', and we do not do multiple
114 * passes through the firewall. XXX check the latter!!!
117 struct ipfw_dyn_bucket {
118 struct mtx mtx; /* Bucket protecting lock */
119 ipfw_dyn_rule *head; /* Pointer to first rule */
123 * Static variables followed by global ones
125 static VNET_DEFINE(struct ipfw_dyn_bucket *, ipfw_dyn_v);
126 static VNET_DEFINE(u_int32_t, dyn_buckets_max);
127 static VNET_DEFINE(u_int32_t, curr_dyn_buckets);
128 static VNET_DEFINE(struct callout, ipfw_timeout);
129 #define V_ipfw_dyn_v VNET(ipfw_dyn_v)
130 #define V_dyn_buckets_max VNET(dyn_buckets_max)
131 #define V_curr_dyn_buckets VNET(curr_dyn_buckets)
132 #define V_ipfw_timeout VNET(ipfw_timeout)
134 static VNET_DEFINE(uma_zone_t, ipfw_dyn_rule_zone);
135 #define V_ipfw_dyn_rule_zone VNET(ipfw_dyn_rule_zone)
137 #define IPFW_BUCK_LOCK_INIT(b) \
138 mtx_init(&(b)->mtx, "IPFW dynamic bucket", NULL, MTX_DEF)
139 #define IPFW_BUCK_LOCK_DESTROY(b) \
140 mtx_destroy(&(b)->mtx)
141 #define IPFW_BUCK_LOCK(i) mtx_lock(&V_ipfw_dyn_v[(i)].mtx)
142 #define IPFW_BUCK_UNLOCK(i) mtx_unlock(&V_ipfw_dyn_v[(i)].mtx)
143 #define IPFW_BUCK_ASSERT(i) mtx_assert(&V_ipfw_dyn_v[(i)].mtx, MA_OWNED)
146 static VNET_DEFINE(int, dyn_keep_states);
147 #define V_dyn_keep_states VNET(dyn_keep_states)
150 * Timeouts for various events in handing dynamic rules.
152 static VNET_DEFINE(u_int32_t, dyn_ack_lifetime);
153 static VNET_DEFINE(u_int32_t, dyn_syn_lifetime);
154 static VNET_DEFINE(u_int32_t, dyn_fin_lifetime);
155 static VNET_DEFINE(u_int32_t, dyn_rst_lifetime);
156 static VNET_DEFINE(u_int32_t, dyn_udp_lifetime);
157 static VNET_DEFINE(u_int32_t, dyn_short_lifetime);
159 #define V_dyn_ack_lifetime VNET(dyn_ack_lifetime)
160 #define V_dyn_syn_lifetime VNET(dyn_syn_lifetime)
161 #define V_dyn_fin_lifetime VNET(dyn_fin_lifetime)
162 #define V_dyn_rst_lifetime VNET(dyn_rst_lifetime)
163 #define V_dyn_udp_lifetime VNET(dyn_udp_lifetime)
164 #define V_dyn_short_lifetime VNET(dyn_short_lifetime)
167 * Keepalives are sent if dyn_keepalive is set. They are sent every
168 * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
169 * seconds of lifetime of a rule.
170 * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
171 * than dyn_keepalive_period.
174 static VNET_DEFINE(u_int32_t, dyn_keepalive_interval);
175 static VNET_DEFINE(u_int32_t, dyn_keepalive_period);
176 static VNET_DEFINE(u_int32_t, dyn_keepalive);
177 static VNET_DEFINE(time_t, dyn_keepalive_last);
179 #define V_dyn_keepalive_interval VNET(dyn_keepalive_interval)
180 #define V_dyn_keepalive_period VNET(dyn_keepalive_period)
181 #define V_dyn_keepalive VNET(dyn_keepalive)
182 #define V_dyn_keepalive_last VNET(dyn_keepalive_last)
184 static VNET_DEFINE(u_int32_t, dyn_max); /* max # of dynamic rules */
186 #define DYN_COUNT uma_zone_get_cur(V_ipfw_dyn_rule_zone)
187 #define V_dyn_max VNET(dyn_max)
189 /* for userspace, we emulate the uma_zone_counter with ipfw_dyn_count */
190 static int ipfw_dyn_count; /* number of objects */
192 #ifdef USERSPACE /* emulation of UMA object counters for userspace */
193 #define uma_zone_get_cur(x) ipfw_dyn_count
194 #endif /* USERSPACE */
196 static int last_log; /* Log ratelimiting */
198 static void ipfw_dyn_tick(void *vnetx);
199 static void check_dyn_rules(struct ip_fw_chain *, struct ip_fw *,
203 static int sysctl_ipfw_dyn_count(SYSCTL_HANDLER_ARGS);
204 static int sysctl_ipfw_dyn_max(SYSCTL_HANDLER_ARGS);
208 SYSCTL_DECL(_net_inet_ip_fw);
209 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_buckets,
210 CTLFLAG_RW, &VNET_NAME(dyn_buckets_max), 0,
211 "Max number of dyn. buckets");
212 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets,
213 CTLFLAG_RD, &VNET_NAME(curr_dyn_buckets), 0,
214 "Current Number of dyn. buckets");
215 SYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, dyn_count,
216 CTLTYPE_UINT|CTLFLAG_RD, 0, 0, sysctl_ipfw_dyn_count, "IU",
217 "Number of dyn. rules");
218 SYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, dyn_max,
219 CTLTYPE_UINT|CTLFLAG_RW, 0, 0, sysctl_ipfw_dyn_max, "IU",
220 "Max number of dyn. rules");
221 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime,
222 CTLFLAG_RW, &VNET_NAME(dyn_ack_lifetime), 0,
223 "Lifetime of dyn. rules for acks");
224 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime,
225 CTLFLAG_RW, &VNET_NAME(dyn_syn_lifetime), 0,
226 "Lifetime of dyn. rules for syn");
227 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime,
228 CTLFLAG_RW, &VNET_NAME(dyn_fin_lifetime), 0,
229 "Lifetime of dyn. rules for fin");
230 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime,
231 CTLFLAG_RW, &VNET_NAME(dyn_rst_lifetime), 0,
232 "Lifetime of dyn. rules for rst");
233 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime,
234 CTLFLAG_RW, &VNET_NAME(dyn_udp_lifetime), 0,
235 "Lifetime of dyn. rules for UDP");
236 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime,
237 CTLFLAG_RW, &VNET_NAME(dyn_short_lifetime), 0,
238 "Lifetime of dyn. rules for other situations");
239 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_keepalive,
240 CTLFLAG_RW, &VNET_NAME(dyn_keepalive), 0,
241 "Enable keepalives for dyn. rules");
242 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_keep_states,
243 CTLFLAG_RW, &VNET_NAME(dyn_keep_states), 0,
244 "Do not flush dynamic states on rule deletion");
248 #endif /* SYSCTL_NODE */
252 hash_packet6(struct ipfw_flow_id *id)
255 i = (id->dst_ip6.__u6_addr.__u6_addr32[2]) ^
256 (id->dst_ip6.__u6_addr.__u6_addr32[3]) ^
257 (id->src_ip6.__u6_addr.__u6_addr32[2]) ^
258 (id->src_ip6.__u6_addr.__u6_addr32[3]) ^
259 (id->dst_port) ^ (id->src_port);
264 * IMPORTANT: the hash function for dynamic rules must be commutative
265 * in source and destination (ip,port), because rules are bidirectional
266 * and we want to find both in the same bucket.
269 hash_packet(struct ipfw_flow_id *id, int buckets)
274 if (IS_IP6_FLOW_ID(id))
275 i = hash_packet6(id);
278 i = (id->dst_ip) ^ (id->src_ip) ^ (id->dst_port) ^ (id->src_port);
284 * Print customizable flow id description via log(9) facility.
287 print_dyn_rule_flags(struct ipfw_flow_id *id, int dyn_type, int log_flags,
288 char *prefix, char *postfix)
292 char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
294 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
298 if (IS_IP6_FLOW_ID(id)) {
299 ip6_sprintf(src, &id->src_ip6);
300 ip6_sprintf(dst, &id->dst_ip6);
304 da.s_addr = htonl(id->src_ip);
305 inet_ntop(AF_INET, &da, src, sizeof(src));
306 da.s_addr = htonl(id->dst_ip);
307 inet_ntop(AF_INET, &da, dst, sizeof(dst));
309 log(log_flags, "ipfw: %s type %d %s %d -> %s %d, %d %s\n",
310 prefix, dyn_type, src, id->src_port, dst,
311 id->dst_port, DYN_COUNT, postfix);
314 #define print_dyn_rule(id, dtype, prefix, postfix) \
315 print_dyn_rule_flags(id, dtype, LOG_DEBUG, prefix, postfix)
317 #define TIME_LEQ(a,b) ((int)((a)-(b)) <= 0)
318 #define TIME_LE(a,b) ((int)((a)-(b)) < 0)
321 * Lookup a dynamic rule, locked version.
323 static ipfw_dyn_rule *
324 lookup_dyn_rule_locked(struct ipfw_flow_id *pkt, int i, int *match_direction,
328 * Stateful ipfw extensions.
329 * Lookup into dynamic session queue.
331 #define MATCH_REVERSE 0
332 #define MATCH_FORWARD 1
334 #define MATCH_UNKNOWN 3
335 int dir = MATCH_NONE;
336 ipfw_dyn_rule *prev, *q = NULL;
340 for (prev = NULL, q = V_ipfw_dyn_v[i].head; q; prev = q, q = q->next) {
341 if (q->dyn_type == O_LIMIT_PARENT && q->count)
344 if (pkt->proto != q->id.proto || q->dyn_type == O_LIMIT_PARENT)
347 if (IS_IP6_FLOW_ID(pkt)) {
348 if (IN6_ARE_ADDR_EQUAL(&pkt->src_ip6, &q->id.src_ip6) &&
349 IN6_ARE_ADDR_EQUAL(&pkt->dst_ip6, &q->id.dst_ip6) &&
350 pkt->src_port == q->id.src_port &&
351 pkt->dst_port == q->id.dst_port) {
355 if (IN6_ARE_ADDR_EQUAL(&pkt->src_ip6, &q->id.dst_ip6) &&
356 IN6_ARE_ADDR_EQUAL(&pkt->dst_ip6, &q->id.src_ip6) &&
357 pkt->src_port == q->id.dst_port &&
358 pkt->dst_port == q->id.src_port) {
363 if (pkt->src_ip == q->id.src_ip &&
364 pkt->dst_ip == q->id.dst_ip &&
365 pkt->src_port == q->id.src_port &&
366 pkt->dst_port == q->id.dst_port) {
370 if (pkt->src_ip == q->id.dst_ip &&
371 pkt->dst_ip == q->id.src_ip &&
372 pkt->src_port == q->id.dst_port &&
373 pkt->dst_port == q->id.src_port) {
380 goto done; /* q = NULL, not found */
382 if (prev != NULL) { /* found and not in front */
383 prev->next = q->next;
384 q->next = V_ipfw_dyn_v[i].head;
385 V_ipfw_dyn_v[i].head = q;
387 if (pkt->proto == IPPROTO_TCP) { /* update state according to flags */
389 u_char flags = pkt->_flags & (TH_FIN | TH_SYN | TH_RST);
391 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
392 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
393 #define TCP_FLAGS (TH_FLAGS | (TH_FLAGS << 8))
394 #define ACK_FWD 0x10000 /* fwd ack seen */
395 #define ACK_REV 0x20000 /* rev ack seen */
397 q->state |= (dir == MATCH_FORWARD) ? flags : (flags << 8);
398 switch (q->state & TCP_FLAGS) {
399 case TH_SYN: /* opening */
400 q->expire = time_uptime + V_dyn_syn_lifetime;
403 case BOTH_SYN: /* move to established */
404 case BOTH_SYN | TH_FIN: /* one side tries to close */
405 case BOTH_SYN | (TH_FIN << 8):
406 #define _SEQ_GE(a,b) ((int)(a) - (int)(b) >= 0)
410 ack = ntohl(tcp->th_ack);
411 if (dir == MATCH_FORWARD) {
412 if (q->ack_fwd == 0 ||
413 _SEQ_GE(ack, q->ack_fwd)) {
418 if (q->ack_rev == 0 ||
419 _SEQ_GE(ack, q->ack_rev)) {
424 if ((q->state & (ACK_FWD | ACK_REV)) ==
425 (ACK_FWD | ACK_REV)) {
426 q->expire = time_uptime + V_dyn_ack_lifetime;
427 q->state &= ~(ACK_FWD | ACK_REV);
431 case BOTH_SYN | BOTH_FIN: /* both sides closed */
432 if (V_dyn_fin_lifetime >= V_dyn_keepalive_period)
433 V_dyn_fin_lifetime = V_dyn_keepalive_period - 1;
434 q->expire = time_uptime + V_dyn_fin_lifetime;
440 * reset or some invalid combination, but can also
441 * occur if we use keep-state the wrong way.
443 if ( (q->state & ((TH_RST << 8)|TH_RST)) == 0)
444 printf("invalid state: 0x%x\n", q->state);
446 if (V_dyn_rst_lifetime >= V_dyn_keepalive_period)
447 V_dyn_rst_lifetime = V_dyn_keepalive_period - 1;
448 q->expire = time_uptime + V_dyn_rst_lifetime;
451 } else if (pkt->proto == IPPROTO_UDP) {
452 q->expire = time_uptime + V_dyn_udp_lifetime;
454 /* other protocols */
455 q->expire = time_uptime + V_dyn_short_lifetime;
458 if (match_direction != NULL)
459 *match_direction = dir;
464 ipfw_lookup_dyn_rule(struct ipfw_flow_id *pkt, int *match_direction,
470 i = hash_packet(pkt, V_curr_dyn_buckets);
473 q = lookup_dyn_rule_locked(pkt, i, match_direction, tcp);
476 /* NB: return table locked when q is not NULL */
482 * @p - pointer to dynamic rule
485 ipfw_dyn_unlock(ipfw_dyn_rule *q)
488 IPFW_BUCK_UNLOCK(q->bucket);
492 resize_dynamic_table(struct ip_fw_chain *chain, int nbuckets)
494 int i, k, nbuckets_old;
496 struct ipfw_dyn_bucket *dyn_v, *dyn_v_old;
498 /* Check if given number is power of 2 and less than 64k */
499 if ((nbuckets > 65536) || (!powerof2(nbuckets)))
502 CTR3(KTR_NET, "%s: resize dynamic hash: %d -> %d", __func__,
503 V_curr_dyn_buckets, nbuckets);
505 /* Allocate and initialize new hash */
506 dyn_v = malloc(nbuckets * sizeof(ipfw_dyn_rule), M_IPFW,
509 for (i = 0 ; i < nbuckets; i++)
510 IPFW_BUCK_LOCK_INIT(&dyn_v[i]);
513 * Call upper half lock, as get_map() do to ease
514 * read-only access to dynamic rules hash from sysctl
516 IPFW_UH_WLOCK(chain);
519 * Acquire chain write lock to permit hash access
520 * for main traffic path without additional locks
524 /* Save old values */
525 nbuckets_old = V_curr_dyn_buckets;
526 dyn_v_old = V_ipfw_dyn_v;
528 /* Skip relinking if array is not set up */
529 if (V_ipfw_dyn_v == NULL)
530 V_curr_dyn_buckets = 0;
532 /* Re-link all dynamic states */
533 for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
534 while (V_ipfw_dyn_v[i].head != NULL) {
535 /* Remove from current chain */
536 q = V_ipfw_dyn_v[i].head;
537 V_ipfw_dyn_v[i].head = q->next;
539 /* Get new hash value */
540 k = hash_packet(&q->id, nbuckets);
542 /* Add to the new head */
543 q->next = dyn_v[k].head;
548 /* Update current pointers/buckets values */
549 V_curr_dyn_buckets = nbuckets;
550 V_ipfw_dyn_v = dyn_v;
554 IPFW_UH_WUNLOCK(chain);
556 /* Start periodic callout on initial creation */
557 if (dyn_v_old == NULL) {
558 callout_reset_on(&V_ipfw_timeout, hz, ipfw_dyn_tick, curvnet, 0);
562 /* Destroy all mutexes */
563 for (i = 0 ; i < nbuckets_old ; i++)
564 IPFW_BUCK_LOCK_DESTROY(&dyn_v_old[i]);
567 free(dyn_v_old, M_IPFW);
573 * Install state of type 'type' for a dynamic session.
574 * The hash table contains two type of rules:
575 * - regular rules (O_KEEP_STATE)
576 * - rules for sessions with limited number of sess per user
577 * (O_LIMIT). When they are created, the parent is
578 * increased by 1, and decreased on delete. In this case,
579 * the third parameter is the parent rule and not the chain.
580 * - "parent" rules for the above (O_LIMIT_PARENT).
582 static ipfw_dyn_rule *
583 add_dyn_rule(struct ipfw_flow_id *id, int i, u_int8_t dyn_type, struct ip_fw *rule)
589 r = uma_zalloc(V_ipfw_dyn_rule_zone, M_NOWAIT | M_ZERO);
591 if (last_log != time_uptime) {
592 last_log = time_uptime;
593 log(LOG_DEBUG, "ipfw: %s: Cannot allocate rule\n",
601 * refcount on parent is already incremented, so
602 * it is safe to use parent unlocked.
604 if (dyn_type == O_LIMIT) {
605 ipfw_dyn_rule *parent = (ipfw_dyn_rule *)rule;
606 if ( parent->dyn_type != O_LIMIT_PARENT)
607 panic("invalid parent");
613 r->expire = time_uptime + V_dyn_syn_lifetime;
615 r->dyn_type = dyn_type;
616 IPFW_ZERO_DYN_COUNTER(r);
620 r->next = V_ipfw_dyn_v[i].head;
621 V_ipfw_dyn_v[i].head = r;
622 DEB(print_dyn_rule(id, dyn_type, "add dyn entry", "total");)
627 * lookup dynamic parent rule using pkt and rule as search keys.
628 * If the lookup fails, then install one.
630 static ipfw_dyn_rule *
631 lookup_dyn_parent(struct ipfw_flow_id *pkt, int *pindex, struct ip_fw *rule)
636 is_v6 = IS_IP6_FLOW_ID(pkt);
637 i = hash_packet( pkt, V_curr_dyn_buckets );
640 for (q = V_ipfw_dyn_v[i].head ; q != NULL ; q=q->next)
641 if (q->dyn_type == O_LIMIT_PARENT &&
643 pkt->proto == q->id.proto &&
644 pkt->src_port == q->id.src_port &&
645 pkt->dst_port == q->id.dst_port &&
648 IN6_ARE_ADDR_EQUAL(&(pkt->src_ip6),
650 IN6_ARE_ADDR_EQUAL(&(pkt->dst_ip6),
651 &(q->id.dst_ip6))) ||
653 pkt->src_ip == q->id.src_ip &&
654 pkt->dst_ip == q->id.dst_ip)
657 q->expire = time_uptime + V_dyn_short_lifetime;
658 DEB(print_dyn_rule(pkt, q->dyn_type,
659 "lookup_dyn_parent found", "");)
663 /* Add virtual limiting rule */
664 return add_dyn_rule(pkt, i, O_LIMIT_PARENT, rule);
668 * Install dynamic state for rule type cmd->o.opcode
670 * Returns 1 (failure) if state is not installed because of errors or because
671 * session limitations are enforced.
674 ipfw_install_state(struct ip_fw *rule, ipfw_insn_limit *cmd,
675 struct ip_fw_args *args, uint32_t tablearg)
680 DEB(print_dyn_rule(&args->f_id, cmd->o.opcode, "install_state", "");)
682 i = hash_packet(&args->f_id, V_curr_dyn_buckets);
686 q = lookup_dyn_rule_locked(&args->f_id, i, NULL, NULL);
688 if (q != NULL) { /* should never occur */
690 if (last_log != time_uptime) {
691 last_log = time_uptime;
692 printf("ipfw: %s: entry already present, done\n",
700 * State limiting is done via uma(9) zone limiting.
701 * Save pointer to newly-installed rule and reject
702 * packet if add_dyn_rule() returned NULL.
703 * Note q is currently set to NULL.
706 switch (cmd->o.opcode) {
707 case O_KEEP_STATE: /* bidir rule */
708 q = add_dyn_rule(&args->f_id, i, O_KEEP_STATE, rule);
711 case O_LIMIT: { /* limit number of sessions */
712 struct ipfw_flow_id id;
713 ipfw_dyn_rule *parent;
715 uint16_t limit_mask = cmd->limit_mask;
718 conn_limit = IP_FW_ARG_TABLEARG(cmd->conn_limit);
721 if (cmd->conn_limit == IP_FW_TABLEARG)
722 printf("ipfw: %s: O_LIMIT rule, conn_limit: %u "
723 "(tablearg)\n", __func__, conn_limit);
725 printf("ipfw: %s: O_LIMIT rule, conn_limit: %u\n",
726 __func__, conn_limit);
729 id.dst_ip = id.src_ip = id.dst_port = id.src_port = 0;
730 id.proto = args->f_id.proto;
731 id.addr_type = args->f_id.addr_type;
732 id.fib = M_GETFIB(args->m);
734 if (IS_IP6_FLOW_ID (&(args->f_id))) {
735 if (limit_mask & DYN_SRC_ADDR)
736 id.src_ip6 = args->f_id.src_ip6;
737 if (limit_mask & DYN_DST_ADDR)
738 id.dst_ip6 = args->f_id.dst_ip6;
740 if (limit_mask & DYN_SRC_ADDR)
741 id.src_ip = args->f_id.src_ip;
742 if (limit_mask & DYN_DST_ADDR)
743 id.dst_ip = args->f_id.dst_ip;
745 if (limit_mask & DYN_SRC_PORT)
746 id.src_port = args->f_id.src_port;
747 if (limit_mask & DYN_DST_PORT)
748 id.dst_port = args->f_id.dst_port;
751 * We have to release lock for previous bucket to
752 * avoid possible deadlock
756 if ((parent = lookup_dyn_parent(&id, &pindex, rule)) == NULL) {
757 printf("ipfw: %s: add parent failed\n", __func__);
758 IPFW_BUCK_UNLOCK(pindex);
762 if (parent->count >= conn_limit) {
763 if (V_fw_verbose && last_log != time_uptime) {
764 last_log = time_uptime;
766 last_log = time_uptime;
767 snprintf(sbuf, sizeof(sbuf),
769 parent->rule->rulenum);
770 print_dyn_rule_flags(&args->f_id,
772 LOG_SECURITY | LOG_DEBUG,
773 sbuf, "too many entries");
775 IPFW_BUCK_UNLOCK(pindex);
778 /* Increment counter on parent */
780 IPFW_BUCK_UNLOCK(pindex);
783 q = add_dyn_rule(&args->f_id, i, O_LIMIT, (struct ip_fw *)parent);
785 /* Decrement index and notify caller */
787 IPFW_BUCK_LOCK(pindex);
789 IPFW_BUCK_UNLOCK(pindex);
795 printf("ipfw: %s: unknown dynamic rule type %u\n",
796 __func__, cmd->o.opcode);
801 return (1); /* Notify caller about failure */
804 /* XXX just set lifetime */
805 lookup_dyn_rule_locked(&args->f_id, i, NULL, NULL);
812 * Generate a TCP packet, containing either a RST or a keepalive.
813 * When flags & TH_RST, we are sending a RST packet, because of a
814 * "reset" action matched the packet.
815 * Otherwise we are sending a keepalive, and flags & TH_
816 * The 'replyto' mbuf is the mbuf being replied to, if any, and is required
817 * so that MAC can label the reply appropriately.
820 ipfw_send_pkt(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t seq,
821 u_int32_t ack, int flags)
823 struct mbuf *m = NULL; /* stupid compiler */
825 struct ip *h = NULL; /* stupid compiler */
827 struct ip6_hdr *h6 = NULL;
829 struct tcphdr *th = NULL;
831 MGETHDR(m, M_NOWAIT, MT_DATA);
835 M_SETFIB(m, id->fib);
838 mac_netinet_firewall_reply(replyto, m);
840 mac_netinet_firewall_send(m);
842 (void)replyto; /* don't warn about unused arg */
845 switch (id->addr_type) {
847 len = sizeof(struct ip) + sizeof(struct tcphdr);
851 len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
859 dir = ((flags & (TH_SYN | TH_RST)) == TH_SYN);
861 m->m_data += max_linkhdr;
862 m->m_flags |= M_SKIP_FIREWALL;
863 m->m_pkthdr.len = m->m_len = len;
864 m->m_pkthdr.rcvif = NULL;
865 bzero(m->m_data, len);
867 switch (id->addr_type) {
869 h = mtod(m, struct ip *);
871 /* prepare for checksum */
872 h->ip_p = IPPROTO_TCP;
873 h->ip_len = htons(sizeof(struct tcphdr));
875 h->ip_src.s_addr = htonl(id->src_ip);
876 h->ip_dst.s_addr = htonl(id->dst_ip);
878 h->ip_src.s_addr = htonl(id->dst_ip);
879 h->ip_dst.s_addr = htonl(id->src_ip);
882 th = (struct tcphdr *)(h + 1);
886 h6 = mtod(m, struct ip6_hdr *);
888 /* prepare for checksum */
889 h6->ip6_nxt = IPPROTO_TCP;
890 h6->ip6_plen = htons(sizeof(struct tcphdr));
892 h6->ip6_src = id->src_ip6;
893 h6->ip6_dst = id->dst_ip6;
895 h6->ip6_src = id->dst_ip6;
896 h6->ip6_dst = id->src_ip6;
899 th = (struct tcphdr *)(h6 + 1);
905 th->th_sport = htons(id->src_port);
906 th->th_dport = htons(id->dst_port);
908 th->th_sport = htons(id->dst_port);
909 th->th_dport = htons(id->src_port);
911 th->th_off = sizeof(struct tcphdr) >> 2;
913 if (flags & TH_RST) {
914 if (flags & TH_ACK) {
915 th->th_seq = htonl(ack);
916 th->th_flags = TH_RST;
920 th->th_ack = htonl(seq);
921 th->th_flags = TH_RST | TH_ACK;
925 * Keepalive - use caller provided sequence numbers
927 th->th_seq = htonl(seq);
928 th->th_ack = htonl(ack);
929 th->th_flags = TH_ACK;
932 switch (id->addr_type) {
934 th->th_sum = in_cksum(m, len);
936 /* finish the ip header */
938 h->ip_hl = sizeof(*h) >> 2;
939 h->ip_tos = IPTOS_LOWDELAY;
940 h->ip_off = htons(0);
941 h->ip_len = htons(len);
942 h->ip_ttl = V_ip_defttl;
947 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(*h6),
948 sizeof(struct tcphdr));
950 /* finish the ip6 header */
951 h6->ip6_vfc |= IPV6_VERSION;
952 h6->ip6_hlim = IPV6_DEFHLIM;
961 * Queue keepalive packets for given dynamic rule
963 static struct mbuf **
964 ipfw_dyn_send_ka(struct mbuf **mtailp, ipfw_dyn_rule *q)
966 struct mbuf *m_rev, *m_fwd;
968 m_rev = (q->state & ACK_REV) ? NULL :
969 ipfw_send_pkt(NULL, &(q->id), q->ack_rev - 1, q->ack_fwd, TH_SYN);
970 m_fwd = (q->state & ACK_FWD) ? NULL :
971 ipfw_send_pkt(NULL, &(q->id), q->ack_fwd - 1, q->ack_rev, 0);
975 mtailp = &(*mtailp)->m_nextpkt;
979 mtailp = &(*mtailp)->m_nextpkt;
986 * This procedure is used to perform various maintance
987 * on dynamic hash list. Currently it is called every second.
990 ipfw_dyn_tick(void * vnetx)
992 struct ip_fw_chain *chain;
995 struct vnet *vp = vnetx;
1000 chain = &V_layer3_chain;
1002 /* Run keepalive checks every keepalive_period iff ka is enabled */
1003 if ((V_dyn_keepalive_last + V_dyn_keepalive_period <= time_uptime) &&
1004 (V_dyn_keepalive != 0)) {
1005 V_dyn_keepalive_last = time_uptime;
1009 check_dyn_rules(chain, NULL, RESVD_SET, check_ka, 1);
1011 callout_reset_on(&V_ipfw_timeout, hz, ipfw_dyn_tick, vnetx, 0);
1018 * Walk thru all dynamic states doing generic maintance:
1019 * 1) free expired states
1020 * 2) free all states based on deleted rule / set
1021 * 3) send keepalives for states if needed
1023 * @chain - pointer to current ipfw rules chain
1024 * @rule - delete all states originated by given rule if != NULL
1025 * @set - delete all states originated by any rule in set @set if != RESVD_SET
1026 * @check_ka - perform checking/sending keepalives
1027 * @timer - indicate call from timer routine.
1029 * Timer routine must call this function unlocked to permit
1030 * sending keepalives/resizing table.
1032 * Others has to call function with IPFW_UH_WLOCK held.
1033 * Additionally, function assume that dynamic rule/set is
1034 * ALREADY deleted so no new states can be generated by
1037 * Write lock is needed to ensure that unused parent rules
1038 * are not freed by other instance (see stage 2, 3)
1041 check_dyn_rules(struct ip_fw_chain *chain, struct ip_fw *rule,
1042 int set, int check_ka, int timer)
1044 struct mbuf *m0, *m, *mnext, **mtailp;
1046 int i, dyn_count, new_buckets = 0, max_buckets;
1047 int expired = 0, expired_limits = 0, parents = 0, total = 0;
1048 ipfw_dyn_rule *q, *q_prev, *q_next;
1049 ipfw_dyn_rule *exp_head, **exptailp;
1050 ipfw_dyn_rule *exp_lhead, **expltailp;
1052 KASSERT(V_ipfw_dyn_v != NULL, ("%s: dynamic table not allocated",
1055 /* Avoid possible LOR */
1056 KASSERT(!check_ka || timer, ("%s: keepalive check with lock held",
1060 * Do not perform any checks if we currently have no dynamic states
1065 /* Expired states */
1067 exptailp = &exp_head;
1069 /* Expired limit states */
1071 expltailp = &exp_lhead;
1074 * We make a chain of packets to go out here -- not deferring
1075 * until after we drop the IPFW dynamic rule lock would result
1076 * in a lock order reversal with the normal packet input -> ipfw
1082 /* Protect from hash resizing */
1084 IPFW_UH_WLOCK(chain);
1086 IPFW_UH_WLOCK_ASSERT(chain);
1088 #define NEXT_RULE() { q_prev = q; q = q->next ; continue; }
1090 /* Stage 1: perform requested deletion */
1091 for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
1093 for (q = V_ipfw_dyn_v[i].head, q_prev = q; q ; ) {
1094 /* account every rule */
1097 /* Skip parent rules at all */
1098 if (q->dyn_type == O_LIMIT_PARENT) {
1104 * Remove rules which are:
1106 * 2) created by given rule
1107 * 3) created by any rule in given set
1109 if ((TIME_LEQ(q->expire, time_uptime)) ||
1110 ((rule != NULL) && (q->rule == rule)) ||
1111 ((set != RESVD_SET) && (q->rule->set == set))) {
1112 if (TIME_LE(time_uptime, q->expire) &&
1113 q->dyn_type == O_KEEP_STATE &&
1114 V_dyn_keep_states != 0) {
1116 * Do not delete state if
1117 * it is not expired and
1118 * dyn_keep_states is ON.
1119 * However we need to re-link it
1120 * to any other stable rule
1122 q->rule = chain->default_rule;
1126 /* Unlink q from current list */
1128 if (q == V_ipfw_dyn_v[i].head)
1129 V_ipfw_dyn_v[i].head = q_next;
1131 q_prev->next = q_next;
1135 /* queue q to expire list */
1136 if (q->dyn_type != O_LIMIT) {
1138 exptailp = &(*exptailp)->next;
1139 DEB(print_dyn_rule(&q->id, q->dyn_type,
1140 "unlink entry", "left");
1143 /* Separate list for limit rules */
1145 expltailp = &(*expltailp)->next;
1147 DEB(print_dyn_rule(&q->id, q->dyn_type,
1148 "unlink limit entry", "left");
1158 * Check if we need to send keepalive:
1159 * we need to ensure if is time to do KA,
1160 * this is established TCP session, and
1161 * expire time is within keepalive interval
1163 if ((check_ka != 0) && (q->id.proto == IPPROTO_TCP) &&
1164 ((q->state & BOTH_SYN) == BOTH_SYN) &&
1165 (TIME_LEQ(q->expire, time_uptime +
1166 V_dyn_keepalive_interval)))
1167 mtailp = ipfw_dyn_send_ka(mtailp, q);
1171 IPFW_BUCK_UNLOCK(i);
1174 /* Stage 2: decrement counters from O_LIMIT parents */
1175 if (expired_limits != 0) {
1177 * XXX: Note that deleting set with more than one
1178 * heavily-used LIMIT rules can result in overwhelming
1179 * locking due to lack of per-hash value sorting
1181 * We should probably think about:
1182 * 1) pre-allocating hash of size, say,
1183 * MAX(16, V_curr_dyn_buckets / 1024)
1184 * 2) checking if expired_limits is large enough
1185 * 3) If yes, init hash (or its part), re-link
1186 * current list and start decrementing procedure in
1187 * each bucket separately
1191 * Small optimization: do not unlock bucket until
1192 * we see the next item resides in different bucket
1194 if (exp_lhead != NULL) {
1195 i = exp_lhead->parent->bucket;
1198 for (q = exp_lhead; q != NULL; q = q->next) {
1199 if (i != q->parent->bucket) {
1200 IPFW_BUCK_UNLOCK(i);
1201 i = q->parent->bucket;
1205 /* Decrease parent refcount */
1208 if (exp_lhead != NULL)
1209 IPFW_BUCK_UNLOCK(i);
1213 * We protectet ourselves from unused parent deletion
1214 * (from the timer function) by holding UH write lock.
1217 /* Stage 3: remove unused parent rules */
1218 if ((parents != 0) && (expired != 0)) {
1219 for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
1221 for (q = V_ipfw_dyn_v[i].head, q_prev = q ; q ; ) {
1222 if (q->dyn_type != O_LIMIT_PARENT)
1228 /* Parent rule without consumers */
1230 /* Unlink q from current list */
1232 if (q == V_ipfw_dyn_v[i].head)
1233 V_ipfw_dyn_v[i].head = q_next;
1235 q_prev->next = q_next;
1239 /* Add to expired list */
1241 exptailp = &(*exptailp)->next;
1243 DEB(print_dyn_rule(&q->id, q->dyn_type,
1244 "unlink parent entry", "left");
1251 IPFW_BUCK_UNLOCK(i);
1259 * Check if we need to resize hash:
1260 * if current number of states exceeds number of buckes in hash,
1261 * grow hash size to the minimum power of 2 which is bigger than
1262 * current states count. Limit hash size by 64k.
1264 max_buckets = (V_dyn_buckets_max > 65536) ?
1265 65536 : V_dyn_buckets_max;
1267 dyn_count = DYN_COUNT;
1269 if ((dyn_count > V_curr_dyn_buckets * 2) &&
1270 (dyn_count < max_buckets)) {
1271 new_buckets = V_curr_dyn_buckets;
1272 while (new_buckets < dyn_count) {
1275 if (new_buckets >= max_buckets)
1280 IPFW_UH_WUNLOCK(chain);
1283 /* Finally delete old states ad limits if any */
1284 for (q = exp_head; q != NULL; q = q_next) {
1286 uma_zfree(V_ipfw_dyn_rule_zone, q);
1290 for (q = exp_lhead; q != NULL; q = q_next) {
1292 uma_zfree(V_ipfw_dyn_rule_zone, q);
1297 * The rest code MUST be called from timer routine only
1298 * without holding any locks
1303 /* Send keepalive packets if any */
1304 for (m = m0; m != NULL; m = mnext) {
1305 mnext = m->m_nextpkt;
1306 m->m_nextpkt = NULL;
1307 h = mtod(m, struct ip *);
1309 ip_output(m, NULL, NULL, 0, NULL, NULL);
1312 ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
1316 /* Run table resize without holding any locks */
1317 if (new_buckets != 0)
1318 resize_dynamic_table(chain, new_buckets);
1322 * Deletes all dynamic rules originated by given rule or all rules in
1323 * given set. Specify RESVD_SET to indicate set should not be used.
1324 * @chain - pointer to current ipfw rules chain
1325 * @rule - delete all states originated by given rule if != NULL
1326 * @set - delete all states originated by any rule in set @set if != RESVD_SET
1328 * Function has to be called with IPFW_UH_WLOCK held.
1329 * Additionally, function assume that dynamic rule/set is
1330 * ALREADY deleted so no new states can be generated by
1334 ipfw_expire_dyn_rules(struct ip_fw_chain *chain, struct ip_fw *rule, int set)
1337 check_dyn_rules(chain, rule, set, 0, 0);
1341 ipfw_dyn_init(struct ip_fw_chain *chain)
1344 V_ipfw_dyn_v = NULL;
1345 V_dyn_buckets_max = 256; /* must be power of 2 */
1346 V_curr_dyn_buckets = 256; /* must be power of 2 */
1348 V_dyn_ack_lifetime = 300;
1349 V_dyn_syn_lifetime = 20;
1350 V_dyn_fin_lifetime = 1;
1351 V_dyn_rst_lifetime = 1;
1352 V_dyn_udp_lifetime = 10;
1353 V_dyn_short_lifetime = 5;
1355 V_dyn_keepalive_interval = 20;
1356 V_dyn_keepalive_period = 5;
1357 V_dyn_keepalive = 1; /* do send keepalives */
1358 V_dyn_keepalive_last = time_uptime;
1360 V_dyn_max = 4096; /* max # of dynamic rules */
1362 V_ipfw_dyn_rule_zone = uma_zcreate("IPFW dynamic rule",
1363 sizeof(ipfw_dyn_rule), NULL, NULL, NULL, NULL,
1366 /* Enforce limit on dynamic rules */
1367 uma_zone_set_max(V_ipfw_dyn_rule_zone, V_dyn_max);
1369 callout_init(&V_ipfw_timeout, CALLOUT_MPSAFE);
1372 * This can potentially be done on first dynamic rule
1373 * being added to chain.
1375 resize_dynamic_table(chain, V_curr_dyn_buckets);
1379 ipfw_dyn_uninit(int pass)
1384 callout_drain(&V_ipfw_timeout);
1388 if (V_ipfw_dyn_v != NULL) {
1390 * Skip deleting all dynamic states -
1391 * uma_zdestroy() does this more efficiently;
1394 /* Destroy all mutexes */
1395 for (i = 0 ; i < V_curr_dyn_buckets ; i++)
1396 IPFW_BUCK_LOCK_DESTROY(&V_ipfw_dyn_v[i]);
1397 free(V_ipfw_dyn_v, M_IPFW);
1398 V_ipfw_dyn_v = NULL;
1401 uma_zdestroy(V_ipfw_dyn_rule_zone);
1406 * Get/set maximum number of dynamic states in given VNET instance.
1409 sysctl_ipfw_dyn_max(SYSCTL_HANDLER_ARGS)
1412 unsigned int nstates;
1414 nstates = V_dyn_max;
1416 error = sysctl_handle_int(oidp, &nstates, 0, req);
1417 /* Read operation or some error */
1418 if ((error != 0) || (req->newptr == NULL))
1421 V_dyn_max = nstates;
1422 uma_zone_set_max(V_ipfw_dyn_rule_zone, V_dyn_max);
1428 * Get current number of dynamic states in given VNET instance.
1431 sysctl_ipfw_dyn_count(SYSCTL_HANDLER_ARGS)
1434 unsigned int nstates;
1436 nstates = DYN_COUNT;
1438 error = sysctl_handle_int(oidp, &nstates, 0, req);
1445 * Returns number of dynamic rules.
1451 return (V_ipfw_dyn_v == NULL) ? 0 :
1452 (DYN_COUNT * sizeof(ipfw_dyn_rule));
1456 * Fill given buffer with dynamic states.
1457 * IPFW_UH_RLOCK has to be held while calling.
1460 ipfw_get_dynamic(struct ip_fw_chain *chain, char **pbp, const char *ep)
1462 ipfw_dyn_rule *p, *last = NULL;
1466 if (V_ipfw_dyn_v == NULL)
1470 IPFW_UH_RLOCK_ASSERT(chain);
1472 for (i = 0 ; i < V_curr_dyn_buckets; i++) {
1474 for (p = V_ipfw_dyn_v[i].head ; p != NULL; p = p->next) {
1475 if (bp + sizeof *p <= ep) {
1476 ipfw_dyn_rule *dst =
1477 (ipfw_dyn_rule *)bp;
1478 bcopy(p, dst, sizeof *p);
1479 bcopy(&(p->rule->rulenum), &(dst->rule),
1480 sizeof(p->rule->rulenum));
1482 * store set number into high word of
1483 * dst->rule pointer.
1485 bcopy(&(p->rule->set),
1486 (char *)&dst->rule +
1487 sizeof(p->rule->rulenum),
1488 sizeof(p->rule->set));
1490 * store a non-null value in "next".
1491 * The userland code will interpret a
1492 * NULL here as a marker
1493 * for the last dynamic rule.
1495 bcopy(&dst, &dst->next, sizeof(dst));
1498 TIME_LEQ(dst->expire, time_uptime) ?
1499 0 : dst->expire - time_uptime ;
1500 bp += sizeof(ipfw_dyn_rule);
1503 IPFW_BUCK_UNLOCK(i);
1506 if (last != NULL) /* mark last dynamic rule */
1507 bzero(&last->next, sizeof(last));