diff --git a/sys/modules/ipfw/Makefile b/sys/modules/ipfw/Makefile index 1b0e79a4c3f..b2a5b73db38 100644 --- a/sys/modules/ipfw/Makefile +++ b/sys/modules/ipfw/Makefile @@ -4,12 +4,14 @@ KMOD= ipfw SRCS= ip_fw2.c ip_fw_pfil.c ip_fw_bpf.c -SRCS+= ip_fw_dynamic.c ip_fw_log.c ip_fw_eaction.c +SRCS+= ip_fw_log.c ip_fw_eaction.c SRCS+= ip_fw_sockopt.c ip_fw_table.c ip_fw_table_algo.c ip_fw_iface.c SRCS+= ip_fw_table_value.c SRCS+= opt_inet.h opt_inet6.h opt_ipdivert.h opt_ipfw.h opt_ipsec.h -CFLAGS+= -DIPFIREWALL +SRCS+= ip_fw_dynamic2.c + +CFLAGS+= -DIPFIREWALL -I${SRCTOP}/sys/contrib/ck/include # #If you want it verbose #CFLAGS+= -DIPFIREWALL_VERBOSE diff --git a/sys/netpfil/ipfw/ip_fw2.c b/sys/netpfil/ipfw/ip_fw2.c index a32261dac8a..3c2a6d2a5ac 100644 --- a/sys/netpfil/ipfw/ip_fw2.c +++ b/sys/netpfil/ipfw/ip_fw2.c @@ -972,9 +972,8 @@ ipfw_chk(struct ip_fw_args *args) * MATCH_NONE when checked and not matched (q = NULL), * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL) */ - int dyn_dir = MATCH_UNKNOWN; - uint16_t dyn_name = 0; - ipfw_dyn_rule *q = NULL; + struct ipfw_dyn_info dyn_info; + struct ip_fw *q = NULL; struct ip_fw_chain *chain = &V_layer3_chain; /* @@ -1005,6 +1004,7 @@ ipfw_chk(struct ip_fw_args *args) proto = args->f_id.proto = 0; /* mark f_id invalid */ /* XXX 0 is a valid proto: IP/IPv6 Hop-by-Hop Option */ + DYN_INFO_INIT(&dyn_info); /* * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous, * then it sets p to point at the offset "len" in the mbuf. WARNING: the @@ -2151,8 +2151,9 @@ do { \ */ case O_LIMIT: case O_KEEP_STATE: - if (ipfw_install_state(chain, f, - (ipfw_insn_limit *)cmd, args, tablearg)) { + if (ipfw_dyn_install_state(chain, f, + (ipfw_insn_limit *)cmd, args, ulp, + pktlen, &dyn_info, tablearg)) { /* error or limit violation */ retval = IP_FW_DENY; l = 0; /* exit inner loop */ @@ -2166,53 +2167,25 @@ do { \ /* * dynamic rules are checked at the first * keep-state or check-state occurrence, - * with the result being stored in dyn_dir - * and dyn_name. + * with the result being stored in dyn_info. * The compiler introduces a PROBE_STATE * instruction for us when we have a * KEEP_STATE (because PROBE_STATE needs * to be run first). - * - * (dyn_dir == MATCH_UNKNOWN) means this is - * first lookup for such f_id. Do lookup. - * - * (dyn_dir != MATCH_UNKNOWN && - * dyn_name != 0 && dyn_name != cmd->arg1) - * means previous lookup didn't find dynamic - * rule for specific state name and current - * lookup will search rule with another state - * name. Redo lookup. - * - * (dyn_dir != MATCH_UNKNOWN && dyn_name == 0) - * means previous lookup was for `any' name - * and it didn't find rule. No need to do - * lookup again. */ - if ((dyn_dir == MATCH_UNKNOWN || - (dyn_name != 0 && - dyn_name != cmd->arg1)) && - (q = ipfw_lookup_dyn_rule(&args->f_id, - &dyn_dir, proto == IPPROTO_TCP ? - TCP(ulp): NULL, - (dyn_name = cmd->arg1))) != NULL) { + if (DYN_LOOKUP_NEEDED(&dyn_info, cmd) && + (q = ipfw_dyn_lookup_state(args, ulp, + pktlen, cmd, &dyn_info)) != NULL) { /* - * Found dynamic entry, update stats - * and jump to the 'action' part of - * the parent rule by setting - * f, cmd, l and clearing cmdlen. + * Found dynamic entry, jump to the + * 'action' part of the parent rule + * by setting f, cmd, l and clearing + * cmdlen. */ - IPFW_INC_DYN_COUNTER(q, pktlen); - /* XXX we would like to have f_pos - * readily accessible in the dynamic - * rule, instead of having to - * lookup q->rule. - */ - f = q->rule; - f_pos = ipfw_find_rule(chain, - f->rulenum, f->id); + f = q; + f_pos = dyn_info.f_pos; cmd = ACTION_PTR(f); l = f->cmd_len - f->act_ofs; - ipfw_dyn_unlock(q); cmdlen = 0; match = 1; break; @@ -2424,8 +2397,8 @@ do { \ case O_FORWARD_IP: if (args->eh) /* not valid on layer2 pkts */ break; - if (q == NULL || q->rule != f || - dyn_dir == MATCH_FORWARD) { + if (q != f || + dyn_info.direction == MATCH_FORWARD) { struct sockaddr_in *sa; sa = &(((ipfw_insn_sa *)cmd)->sa); @@ -2482,8 +2455,8 @@ do { \ case O_FORWARD_IP6: if (args->eh) /* not valid on layer2 pkts */ break; - if (q == NULL || q->rule != f || - dyn_dir == MATCH_FORWARD) { + if (q != f || + dyn_info.direction == MATCH_FORWARD) { struct sockaddr_in6 *sin6; sin6 = &(((ipfw_insn_sa6 *)cmd)->sa); @@ -2631,7 +2604,7 @@ do { \ * @args content, and it may be * used for new state lookup later. */ - dyn_dir = MATCH_UNKNOWN; + DYN_INFO_INIT(&dyn_info); } break; diff --git a/sys/netpfil/ipfw/ip_fw_dynamic2.c b/sys/netpfil/ipfw/ip_fw_dynamic2.c new file mode 100644 index 00000000000..2cec328ab60 --- /dev/null +++ b/sys/netpfil/ipfw/ip_fw_dynamic2.c @@ -0,0 +1,2988 @@ +/*- + * Copyright (c) 2017 Yandex LLC + * Copyright (c) 2017 Andrey V. Elsukov + * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include "opt_inet.h" +#include "opt_inet6.h" +#include "opt_ipfw.h" +#ifndef INET +#error IPFIREWALL requires INET. +#endif /* INET */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include /* IN6_ARE_ADDR_EQUAL */ +#ifdef INET6 +#include +#include +#include +#endif + +#include + +#include /* XXX for in_cksum */ + +#ifdef MAC +#include +#endif +#include + +/* + * Description of dynamic rules. + * + * Dynamic rules are stored in lists accessed through a hash tables + * whose size is curr_dyn_buckets. This value can be modified through + * the sysctl variable dyn_buckets which is updated when the table + * becomes empty. + * + * Currently there are four tables: dyn_ipv4, dyn_ipv6, dyn_ipv4_parent, + * and dyn_ipv6_parent. + * + * When a packet is received, its address fields hashed, then matched + * against the entries in the corresponding list by addr_type. + * Dynamic rules can be used for different purposes: + * + stateful rules; + * + enforcing limits on the number of sessions; + * + in-kernel NAT (not implemented yet) + * + * The lifetime of dynamic rules is regulated by dyn_*_lifetime, + * measured in seconds and depending on the flags. + * + * The total number of dynamic rules is equal to UMA zone items count. + * The max number of dynamic rules is dyn_max. When we reach + * the maximum number of rules we do not create anymore. This is + * done to avoid consuming too much memory, but also too much + * time when searching on each packet (ideally, we should try instead + * to put a limit on the length of the list on each bucket...). + * + * Each dynamic rule holds a pointer to the parent ipfw rule so + * we know what action to perform. Dynamic rules are removed when + * the parent rule is deleted. + * XXX: This can be changed by dyn_keep_states sysctl. + * + * There are some limitations with dynamic rules -- we do not + * obey the 'randomized match', and we do not do multiple + * passes through the firewall. XXX check the latter!!! + */ + +#define DYN_COUNTER_INC(d, dir, pktlen) do { \ + (d)->pcnt_ ## dir++; \ + (d)->bcnt_ ## dir += pktlen; \ + } while (0) +#define DYN_SETTIME(time, value) ck_pr_store_32(&(time), (value)) + +struct dyn_data { + void *parent; /* pointer to parent rule */ + + uint32_t hashval; /* hash value used for hash resize */ + uint16_t fibnum; /* fib used to send keepalives */ + uint16_t _pad; + + uint32_t chain_id; /* cached ruleset id */ + uint32_t f_pos; /* cached rule index */ + + uint32_t state; /* TCP session state */ + uint32_t ack_fwd; /* most recent ACKs in forward */ + uint32_t ack_rev; /* and reverse direction (used */ + /* to generate keepalives) */ + uint32_t sync; /* synchronization time */ + uint32_t expire; /* expire time */ + + uint64_t pcnt_fwd; /* bytes counter in forward */ + uint64_t bcnt_fwd; /* packets counter in forward */ + uint64_t pcnt_rev; /* bytes counter in reverse */ + uint64_t bcnt_rev; /* packets counter in reverse */ +}; + +#define DPARENT_COUNT_DEC(p) do { \ + MPASS(p->count > 0); \ + ck_pr_dec_32(&(p)->count); \ +} while (0) +#define DPARENT_COUNT_INC(p) ck_pr_inc_32(&(p)->count) +#define DPARENT_COUNT(p) ck_pr_load_32(&(p)->count) +struct dyn_parent { + void *parent; /* pointer to parent rule */ + uint32_t count; /* number of linked states */ + uint32_t hashval; /* hash value used for hash resize */ + uint32_t expire; /* expire time */ +}; + +#define IS_IP4_FLOW_ID(f) ((f)->addr_type == 4) +struct dyn_ipv4_state { + uint8_t type; /* State type */ + uint8_t proto; /* UL Protocol */ + uint16_t kidx; /* named object index */ + uint16_t sport, dport; /* ULP source and destination ports */ + in_addr_t src, dst; /* IPv4 source and destination */ + + union { + struct dyn_data *data; + struct dyn_parent *limit; + }; + CK_SLIST_ENTRY(dyn_ipv4_state) entry; + SLIST_ENTRY(dyn_ipv4_state) expired; +}; +CK_SLIST_HEAD(dyn_ipv4ck_slist, dyn_ipv4_state); +static VNET_DEFINE(struct dyn_ipv4ck_slist *, dyn_ipv4); +static VNET_DEFINE(struct dyn_ipv4ck_slist *, dyn_ipv4_parent); + +SLIST_HEAD(dyn_ipv4_slist, dyn_ipv4_state); +static VNET_DEFINE(struct dyn_ipv4_slist, dyn_expired_ipv4); +#define V_dyn_ipv4 VNET(dyn_ipv4) +#define V_dyn_ipv4_parent VNET(dyn_ipv4_parent) +#define V_dyn_expired_ipv4 VNET(dyn_expired_ipv4) + +#ifdef INET6 +struct dyn_ipv6_state { + uint8_t type; /* State type */ + uint8_t proto; /* UL Protocol */ + uint16_t kidx; /* named object index */ + uint16_t sport, dport; /* ULP source and destination ports */ + struct in6_addr src, dst; /* IPv6 source and destination */ + uint32_t zoneid; /* IPv6 scope zone id */ + union { + struct dyn_data *data; + struct dyn_parent *limit; + }; + CK_SLIST_ENTRY(dyn_ipv6_state) entry; + SLIST_ENTRY(dyn_ipv6_state) expired; +}; +CK_SLIST_HEAD(dyn_ipv6ck_slist, dyn_ipv6_state); +static VNET_DEFINE(struct dyn_ipv6ck_slist *, dyn_ipv6); +static VNET_DEFINE(struct dyn_ipv6ck_slist *, dyn_ipv6_parent); + +SLIST_HEAD(dyn_ipv6_slist, dyn_ipv6_state); +static VNET_DEFINE(struct dyn_ipv6_slist, dyn_expired_ipv6); +#define V_dyn_ipv6 VNET(dyn_ipv6) +#define V_dyn_ipv6_parent VNET(dyn_ipv6_parent) +#define V_dyn_expired_ipv6 VNET(dyn_expired_ipv6) +#endif /* INET6 */ + +/* + * Per-CPU pointer indicates that specified state is currently in use + * and must not be reclaimed by expiration callout. + */ +static void **dyn_hp_cache; +static DPCPU_DEFINE(void *, dyn_hp); +#define DYNSTATE_GET(cpu) ck_pr_load_ptr(DPCPU_ID_PTR((cpu), dyn_hp)) +#define DYNSTATE_PROTECT(v) ck_pr_store_ptr(DPCPU_PTR(dyn_hp), (v)) +#define DYNSTATE_RELEASE() DYNSTATE_PROTECT(NULL) +#define DYNSTATE_CRITICAL_ENTER() critical_enter() +#define DYNSTATE_CRITICAL_EXIT() do { \ + DYNSTATE_RELEASE(); \ + critical_exit(); \ +} while (0); + +/* + * We keep two version numbers, one is updated when new entry added to + * the list. Second is updated when an entry deleted from the list. + * Versions are updated under bucket lock. + * + * Bucket "add" version number is used to know, that in the time between + * state lookup (i.e. ipfw_dyn_lookup_state()) and the followed state + * creation (i.e. ipfw_dyn_install_state()) another concurrent thread did + * not install some state in this bucket. Using this info we can avoid + * additional state lookup, because we are sure that we will not install + * the state twice. + * + * Also doing the tracking of bucket "del" version during lookup we can + * be sure, that state entry was not unlinked and freed in time between + * we read the state pointer and protect it with hazard pointer. + * + * An entry unlinked from CK list keeps unchanged until it is freed. + * Unlinked entries are linked into expired lists using "expired" field. + */ + +/* + * dyn_expire_lock is used to protect access to dyn_expired_xxx lists. + * dyn_bucket_lock is used to get write access to lists in specific bucket. + * Currently one dyn_bucket_lock is used for all ipv4, ipv4_parent, ipv6, + * and ipv6_parent lists. + */ +static VNET_DEFINE(struct mtx, dyn_expire_lock); +static VNET_DEFINE(struct mtx *, dyn_bucket_lock); +#define V_dyn_expire_lock VNET(dyn_expire_lock) +#define V_dyn_bucket_lock VNET(dyn_bucket_lock) + +/* + * Bucket's add/delete generation versions. + */ +static VNET_DEFINE(uint32_t *, dyn_ipv4_add); +static VNET_DEFINE(uint32_t *, dyn_ipv4_del); +static VNET_DEFINE(uint32_t *, dyn_ipv4_parent_add); +static VNET_DEFINE(uint32_t *, dyn_ipv4_parent_del); +#define V_dyn_ipv4_add VNET(dyn_ipv4_add) +#define V_dyn_ipv4_del VNET(dyn_ipv4_del) +#define V_dyn_ipv4_parent_add VNET(dyn_ipv4_parent_add) +#define V_dyn_ipv4_parent_del VNET(dyn_ipv4_parent_del) + +#ifdef INET6 +static VNET_DEFINE(uint32_t *, dyn_ipv6_add); +static VNET_DEFINE(uint32_t *, dyn_ipv6_del); +static VNET_DEFINE(uint32_t *, dyn_ipv6_parent_add); +static VNET_DEFINE(uint32_t *, dyn_ipv6_parent_del); +#define V_dyn_ipv6_add VNET(dyn_ipv6_add) +#define V_dyn_ipv6_del VNET(dyn_ipv6_del) +#define V_dyn_ipv6_parent_add VNET(dyn_ipv6_parent_add) +#define V_dyn_ipv6_parent_del VNET(dyn_ipv6_parent_del) +#endif /* INET6 */ + +#define DYN_BUCKET(h, b) ((h) & (b - 1)) +#define DYN_BUCKET_VERSION(b, v) ck_pr_load_32(&V_dyn_ ## v[(b)]) +#define DYN_BUCKET_VERSION_BUMP(b, v) ck_pr_inc_32(&V_dyn_ ## v[(b)]) + +#define DYN_BUCKET_LOCK_INIT(lock, b) \ + mtx_init(&lock[(b)], "IPFW dynamic bucket", NULL, MTX_DEF) +#define DYN_BUCKET_LOCK_DESTROY(lock, b) mtx_destroy(&lock[(b)]) +#define DYN_BUCKET_LOCK(b) mtx_lock(&V_dyn_bucket_lock[(b)]) +#define DYN_BUCKET_UNLOCK(b) mtx_unlock(&V_dyn_bucket_lock[(b)]) +#define DYN_BUCKET_ASSERT(b) mtx_assert(&V_dyn_bucket_lock[(b)], MA_OWNED) + +#define DYN_EXPIRED_LOCK_INIT() \ + mtx_init(&V_dyn_expire_lock, "IPFW expired states list", NULL, MTX_DEF) +#define DYN_EXPIRED_LOCK_DESTROY() mtx_destroy(&V_dyn_expire_lock) +#define DYN_EXPIRED_LOCK() mtx_lock(&V_dyn_expire_lock) +#define DYN_EXPIRED_UNLOCK() mtx_unlock(&V_dyn_expire_lock) + +static VNET_DEFINE(uint32_t, dyn_buckets_max); +static VNET_DEFINE(uint32_t, curr_dyn_buckets); +static VNET_DEFINE(struct callout, dyn_timeout); +#define V_dyn_buckets_max VNET(dyn_buckets_max) +#define V_curr_dyn_buckets VNET(curr_dyn_buckets) +#define V_dyn_timeout VNET(dyn_timeout) + +/* Maximum length of states chain in a bucket */ +static VNET_DEFINE(uint32_t, curr_max_length); +#define V_curr_max_length VNET(curr_max_length) + +static VNET_DEFINE(uma_zone_t, dyn_data_zone); +static VNET_DEFINE(uma_zone_t, dyn_parent_zone); +static VNET_DEFINE(uma_zone_t, dyn_ipv4_zone); +static VNET_DEFINE(uma_zone_t, dyn_ipv6_zone); +#define V_dyn_data_zone VNET(dyn_data_zone) +#define V_dyn_parent_zone VNET(dyn_parent_zone) +#define V_dyn_ipv4_zone VNET(dyn_ipv4_zone) +#define V_dyn_ipv6_zone VNET(dyn_ipv6_zone) + +/* + * Timeouts for various events in handing dynamic rules. + */ +static VNET_DEFINE(uint32_t, dyn_ack_lifetime); +static VNET_DEFINE(uint32_t, dyn_syn_lifetime); +static VNET_DEFINE(uint32_t, dyn_fin_lifetime); +static VNET_DEFINE(uint32_t, dyn_rst_lifetime); +static VNET_DEFINE(uint32_t, dyn_udp_lifetime); +static VNET_DEFINE(uint32_t, dyn_short_lifetime); +static VNET_DEFINE(uint32_t, dyn_sync_lifetime); + +#define V_dyn_ack_lifetime VNET(dyn_ack_lifetime) +#define V_dyn_syn_lifetime VNET(dyn_syn_lifetime) +#define V_dyn_fin_lifetime VNET(dyn_fin_lifetime) +#define V_dyn_rst_lifetime VNET(dyn_rst_lifetime) +#define V_dyn_udp_lifetime VNET(dyn_udp_lifetime) +#define V_dyn_short_lifetime VNET(dyn_short_lifetime) +#define V_dyn_sync_lifetime VNET(dyn_sync_lifetime) + +/* + * Keepalives are sent if dyn_keepalive is set. They are sent every + * dyn_keepalive_period seconds, in the last dyn_keepalive_interval + * seconds of lifetime of a rule. + * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower + * than dyn_keepalive_period. + */ + +#define DYN_KEEPALIVE_MAXQ 512 +static VNET_DEFINE(uint32_t, dyn_keepalive_interval); +static VNET_DEFINE(uint32_t, dyn_keepalive_period); +static VNET_DEFINE(uint32_t, dyn_keepalive); +static VNET_DEFINE(time_t, dyn_keepalive_last); + +#define V_dyn_keepalive_interval VNET(dyn_keepalive_interval) +#define V_dyn_keepalive_period VNET(dyn_keepalive_period) +#define V_dyn_keepalive VNET(dyn_keepalive) +#define V_dyn_keepalive_last VNET(dyn_keepalive_last) + +static VNET_DEFINE(uint32_t, dyn_max); /* max # of dynamic states */ +static VNET_DEFINE(uint32_t, dyn_count); /* number of states */ +static VNET_DEFINE(uint32_t, dyn_parent_max); /* max # of parent states */ +static VNET_DEFINE(uint32_t, dyn_parent_count); /* number of parent states */ +#define V_dyn_max VNET(dyn_max) +#define V_dyn_count VNET(dyn_count) +#define V_dyn_parent_max VNET(dyn_parent_max) +#define V_dyn_parent_count VNET(dyn_parent_count) + +#define DYN_COUNT_DEC(name) do { \ + MPASS((V_ ## name) > 0); \ + ck_pr_dec_32(&(V_ ## name)); \ +} while (0) +#define DYN_COUNT_INC(name) ck_pr_inc_32(&(V_ ## name)) +#define DYN_COUNT(name) ck_pr_load_32(&(V_ ## name)) + +static time_t last_log; /* Log ratelimiting */ + +/* + * Get/set maximum number of dynamic states in given VNET instance. + */ +static int +sysctl_dyn_max(SYSCTL_HANDLER_ARGS) +{ + uint32_t nstates; + int error; + + nstates = V_dyn_max; + error = sysctl_handle_32(oidp, &nstates, 0, req); + /* Read operation or some error */ + if ((error != 0) || (req->newptr == NULL)) + return (error); + + V_dyn_max = nstates; + uma_zone_set_max(V_dyn_data_zone, V_dyn_max); + return (0); +} + +static int +sysctl_dyn_parent_max(SYSCTL_HANDLER_ARGS) +{ + uint32_t nstates; + int error; + + nstates = V_dyn_parent_max; + error = sysctl_handle_32(oidp, &nstates, 0, req); + /* Read operation or some error */ + if ((error != 0) || (req->newptr == NULL)) + return (error); + + V_dyn_parent_max = nstates; + uma_zone_set_max(V_dyn_parent_zone, V_dyn_parent_max); + return (0); +} + +static int +sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS) +{ + uint32_t nbuckets; + int error; + + nbuckets = V_dyn_buckets_max; + error = sysctl_handle_32(oidp, &nbuckets, 0, req); + /* Read operation or some error */ + if ((error != 0) || (req->newptr == NULL)) + return (error); + + if (nbuckets > 256) + V_dyn_parent_max = 1 << fls(nbuckets - 1); + else + return (EINVAL); + return (0); +} + +SYSCTL_DECL(_net_inet_ip_fw); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_count, + CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(dyn_count), 0, + "Current number of dynamic states."); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_parent_count, + CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(dyn_parent_count), 0, + "Current number of parent states. "); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets, + CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(curr_dyn_buckets), 0, + "Current number of buckets for states hash table."); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, curr_max_length, + CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(curr_max_length), 0, + "Current maximum length of states chains in hash buckets."); +SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_buckets, + CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RW, 0, 0, sysctl_dyn_buckets, + "IU", "Max number of buckets for dynamic states hash table."); +SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_max, + CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RW, 0, 0, sysctl_dyn_max, + "IU", "Max number of dynamic states."); +SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_parent_max, + CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RW, 0, 0, sysctl_dyn_parent_max, + "IU", "Max number of parent dynamic states."); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime, + CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_ack_lifetime), 0, + "Lifetime of dynamic states for TCP ACK."); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime, + CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_syn_lifetime), 0, + "Lifetime of dynamic states for TCP SYN."); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime, + CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_fin_lifetime), 0, + "Lifetime of dynamic states for TCP FIN."); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime, + CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_rst_lifetime), 0, + "Lifetime of dynamic states for TCP RST."); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime, + CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_udp_lifetime), 0, + "Lifetime of dynamic states for UDP."); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime, + CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_short_lifetime), 0, + "Lifetime of dynamic states for other situations."); +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_keepalive, + CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_keepalive), 0, + "Enable keepalives for dynamic states."); +#if 0 +SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_keep_states, + CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_keep_states), 0, + "Do not flush dynamic states on rule deletion"); +#endif + +#ifdef IPFIREWALL_DYNDEBUG +#define DYN_DEBUG_CODE(code) code +#define DYN_DEBUG(fmt, ...) do { \ + printf("%s: " fmt "\n", __func__, __VA_ARGS__); \ +} while (0) +#define DYN_CNTR_TOTAL 0 +#define DYN_CNTR_RESTART 1 +#define DYN_CNTR_SKIPPED 2 +#define DYN_CNTR_CREATED 3 +#define DYN_CNTR_MAX 8 + +#define DYN_CNTR(name) dyn_debug_cntr[DYN_CNTR_ ## name] +#define DYN_CNTR_ADD(n, v) counter_u64_add(DYN_CNTR(n), (v)) +#define DYN_CNTR_INC(n) DYN_CNTR_ADD(n, 1) + +static counter_u64_t dyn_debug_cntr[DYN_CNTR_MAX]; + +SYSCTL_NODE(_net_inet_ip_fw, OID_AUTO, stats, CTLFLAG_RW, + 0, "Dynamic states debug statistics"); +SYSCTL_COUNTER_U64(_net_inet_ip_fw_stats, OID_AUTO, lookups, + CTLFLAG_RD, &DYN_CNTR(TOTAL), "Number of performed state lookups."); +SYSCTL_COUNTER_U64(_net_inet_ip_fw_stats, OID_AUTO, lookups_restarts, + CTLFLAG_RD, &DYN_CNTR(RESTART), "Number of performed lookup restarts."); +SYSCTL_COUNTER_U64(_net_inet_ip_fw_stats, OID_AUTO, lookups_skipped, + CTLFLAG_RD, &DYN_CNTR(SKIPPED), + "Number of lookups skipped by dyn_add_xxx()."); +SYSCTL_COUNTER_U64(_net_inet_ip_fw_stats, OID_AUTO, states_created, + CTLFLAG_RD, &DYN_CNTR(CREATED), + "Number of times the dyn_add_xxx() invoked."); + +static void +dyn_debug_init(void *dummy __unused) +{ + + COUNTER_ARRAY_ALLOC(dyn_debug_cntr, DYN_CNTR_MAX, M_WAITOK); +} +SYSINIT(ipfw_dyn_debug_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY, + dyn_debug_init, NULL); + +static void +dyn_debug_uninit(void *dummy __unused) +{ + + COUNTER_ARRAY_FREE(dyn_debug_cntr, DYN_CNTR_MAX); +} +SYSUNINIT(ipfw_dyn_debug_uninit, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY, + dyn_debug_uninit, NULL); +#else +#define DYN_DEBUG(fmt, ...) +#define DYN_DEBUG_CODE(code) +#define DYN_CNTR(name) +#define DYN_CNTR_ADD(n, v) +#define DYN_CNTR_INC(n) +#endif /* !IPFIREWALL_DYNDEBUG */ + +#ifdef INET6 +/* Functions to work with IPv6 states */ +static struct dyn_ipv6_state *dyn_lookup_ipv6_state( + const struct ipfw_flow_id *, uint32_t, const void *, + struct ipfw_dyn_info *, int); +static int dyn_lookup_ipv6_state_locked(const struct ipfw_flow_id *, + uint32_t, uint32_t, uint16_t); +static struct dyn_ipv6_state *dyn_alloc_ipv6_state( + const struct ipfw_flow_id *, uint32_t, uint16_t, uint8_t); +static int dyn_add_ipv6_state(void *, const struct ipfw_flow_id *, + uint32_t, const void *, int, uint32_t, struct ipfw_dyn_info *, + uint16_t, uint16_t, uint8_t); +static void dyn_export_ipv6_state(const struct dyn_ipv6_state *, + ipfw_dyn_rule *); + +static uint32_t dyn_getscopeid(const struct ip_fw_args *); +static void dyn_make_keepalive_ipv6(struct mbuf *, const struct in6_addr *, + const struct in6_addr *, uint32_t, uint32_t, uint32_t, uint16_t, + uint16_t); +static void dyn_enqueue_keepalive_ipv6(struct mbufq *, + const struct dyn_ipv6_state *); +static void dyn_send_keepalive_ipv6(struct ip_fw_chain *); + +struct dyn_ipv6_state *dyn_lookup_ipv6_parent(const struct ipfw_flow_id *, + uint32_t, const void *, uint32_t); +struct dyn_ipv6_state *dyn_lookup_ipv6_parent_locked( + const struct ipfw_flow_id *, uint32_t, const void *, uint32_t); +static struct dyn_ipv6_state *dyn_add_ipv6_parent(void *, + const struct ipfw_flow_id *, uint32_t, uint32_t, uint32_t, uint16_t); +#endif /* INET6 */ + +/* Functions to work with limit states */ +static int dyn_get_parent_state(const struct ipfw_flow_id *, uint32_t, + struct ip_fw *, uint32_t, const ipfw_insn_limit *, uint32_t); +struct dyn_ipv4_state *dyn_lookup_ipv4_parent(const struct ipfw_flow_id *, + const void *, uint32_t); +struct dyn_ipv4_state *dyn_lookup_ipv4_parent_locked( + const struct ipfw_flow_id *, const void *, uint32_t); +static struct dyn_parent *dyn_alloc_parent(void *, uint32_t); +static struct dyn_ipv4_state *dyn_add_ipv4_parent(void *, + const struct ipfw_flow_id *, uint32_t, uint32_t, uint16_t); + +static void dyn_tick(void *); +static void dyn_expire_states(struct ip_fw_chain *, ipfw_range_tlv *); +static void dyn_free_states(struct ip_fw_chain *); +static void dyn_export_parent(const struct dyn_parent *, uint16_t, + ipfw_dyn_rule *); +static void dyn_export_data(const struct dyn_data *, uint16_t, uint8_t, + ipfw_dyn_rule *); +static void dyn_update_proto_state(struct dyn_data *, + const struct ipfw_flow_id *, const void *, int, int); +static void dyn_update_proto_state_locked(struct dyn_data *, + const struct ipfw_flow_id *, const void *, int, int); + +/* Functions to work with IPv4 states */ +struct dyn_ipv4_state *dyn_lookup_ipv4_state(const struct ipfw_flow_id *, + const void *, struct ipfw_dyn_info *, int); +static int dyn_lookup_ipv4_state_locked(const struct ipfw_flow_id *, + uint32_t, uint16_t); +static struct dyn_ipv4_state *dyn_alloc_ipv4_state( + const struct ipfw_flow_id *, uint16_t, uint8_t); +static int dyn_add_ipv4_state(void *, const struct ipfw_flow_id *, + const void *, int, uint32_t, struct ipfw_dyn_info *, uint16_t, + uint16_t, uint8_t); +static void dyn_export_ipv4_state(const struct dyn_ipv4_state *, + ipfw_dyn_rule *); + +/* + * Named states support. + */ +static char *default_state_name = "default"; +struct dyn_state_obj { + struct named_object no; + char name[64]; +}; + +#define DYN_STATE_OBJ(ch, cmd) \ + ((struct dyn_state_obj *)SRV_OBJECT(ch, (cmd)->arg1)) +/* + * Classifier callback. + * Return 0 if opcode contains object that should be referenced + * or rewritten. + */ +static int +dyn_classify(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype) +{ + + DYN_DEBUG("opcode %d, arg1 %d", cmd->opcode, cmd->arg1); + /* Don't rewrite "check-state any" */ + if (cmd->arg1 == 0 && + cmd->opcode == O_CHECK_STATE) + return (1); + + *puidx = cmd->arg1; + *ptype = 0; + return (0); +} + +static void +dyn_update(ipfw_insn *cmd, uint16_t idx) +{ + + cmd->arg1 = idx; + DYN_DEBUG("opcode %d, arg1 %d", cmd->opcode, cmd->arg1); +} + +static int +dyn_findbyname(struct ip_fw_chain *ch, struct tid_info *ti, + struct named_object **pno) +{ + ipfw_obj_ntlv *ntlv; + const char *name; + + DYN_DEBUG("uidx %d", ti->uidx); + if (ti->uidx != 0) { + if (ti->tlvs == NULL) + return (EINVAL); + /* Search ntlv in the buffer provided by user */ + ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx, + IPFW_TLV_STATE_NAME); + if (ntlv == NULL) + return (EINVAL); + name = ntlv->name; + } else + name = default_state_name; + /* + * Search named object with corresponding name. + * Since states objects are global - ignore the set value + * and use zero instead. + */ + *pno = ipfw_objhash_lookup_name_type(CHAIN_TO_SRV(ch), 0, + IPFW_TLV_STATE_NAME, name); + /* + * We always return success here. + * The caller will check *pno and mark object as unresolved, + * then it will automatically create "default" object. + */ + return (0); +} + +static struct named_object * +dyn_findbykidx(struct ip_fw_chain *ch, uint16_t idx) +{ + + DYN_DEBUG("kidx %d", idx); + return (ipfw_objhash_lookup_kidx(CHAIN_TO_SRV(ch), idx)); +} + +static int +dyn_create(struct ip_fw_chain *ch, struct tid_info *ti, + uint16_t *pkidx) +{ + struct namedobj_instance *ni; + struct dyn_state_obj *obj; + struct named_object *no; + ipfw_obj_ntlv *ntlv; + char *name; + + DYN_DEBUG("uidx %d", ti->uidx); + if (ti->uidx != 0) { + if (ti->tlvs == NULL) + return (EINVAL); + ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx, + IPFW_TLV_STATE_NAME); + if (ntlv == NULL) + return (EINVAL); + name = ntlv->name; + } else + name = default_state_name; + + ni = CHAIN_TO_SRV(ch); + obj = malloc(sizeof(*obj), M_IPFW, M_WAITOK | M_ZERO); + obj->no.name = obj->name; + obj->no.etlv = IPFW_TLV_STATE_NAME; + strlcpy(obj->name, name, sizeof(obj->name)); + + IPFW_UH_WLOCK(ch); + no = ipfw_objhash_lookup_name_type(ni, 0, + IPFW_TLV_STATE_NAME, name); + if (no != NULL) { + /* + * Object is already created. + * Just return its kidx and bump refcount. + */ + *pkidx = no->kidx; + no->refcnt++; + IPFW_UH_WUNLOCK(ch); + free(obj, M_IPFW); + DYN_DEBUG("\tfound kidx %d", *pkidx); + return (0); + } + if (ipfw_objhash_alloc_idx(ni, &obj->no.kidx) != 0) { + DYN_DEBUG("\talloc_idx failed for %s", name); + IPFW_UH_WUNLOCK(ch); + free(obj, M_IPFW); + return (ENOSPC); + } + ipfw_objhash_add(ni, &obj->no); + SRV_OBJECT(ch, obj->no.kidx) = obj; + obj->no.refcnt++; + *pkidx = obj->no.kidx; + IPFW_UH_WUNLOCK(ch); + DYN_DEBUG("\tcreated kidx %d", *pkidx); + return (0); +} + +static void +dyn_destroy(struct ip_fw_chain *ch, struct named_object *no) +{ + struct dyn_state_obj *obj; + + IPFW_UH_WLOCK_ASSERT(ch); + + KASSERT(no->refcnt == 1, + ("Destroying object '%s' (type %u, idx %u) with refcnt %u", + no->name, no->etlv, no->kidx, no->refcnt)); + + DYN_DEBUG("kidx %d", no->kidx); + obj = SRV_OBJECT(ch, no->kidx); + SRV_OBJECT(ch, no->kidx) = NULL; + ipfw_objhash_del(CHAIN_TO_SRV(ch), no); + ipfw_objhash_free_idx(CHAIN_TO_SRV(ch), no->kidx); + + free(obj, M_IPFW); +} + +static struct opcode_obj_rewrite dyn_opcodes[] = { + { + O_KEEP_STATE, IPFW_TLV_STATE_NAME, + dyn_classify, dyn_update, + dyn_findbyname, dyn_findbykidx, + dyn_create, dyn_destroy + }, + { + O_CHECK_STATE, IPFW_TLV_STATE_NAME, + dyn_classify, dyn_update, + dyn_findbyname, dyn_findbykidx, + dyn_create, dyn_destroy + }, + { + O_PROBE_STATE, IPFW_TLV_STATE_NAME, + dyn_classify, dyn_update, + dyn_findbyname, dyn_findbykidx, + dyn_create, dyn_destroy + }, + { + O_LIMIT, IPFW_TLV_STATE_NAME, + dyn_classify, dyn_update, + dyn_findbyname, dyn_findbykidx, + dyn_create, dyn_destroy + }, +}; + +/* + * IMPORTANT: the hash function for dynamic rules must be commutative + * in source and destination (ip,port), because rules are bidirectional + * and we want to find both in the same bucket. + */ +static __inline uint32_t +hash_packet(const struct ipfw_flow_id *id) +{ + uint32_t i; + +#ifdef INET6 + if (IS_IP6_FLOW_ID(id)) + i = ntohl((id->dst_ip6.__u6_addr.__u6_addr32[2]) ^ + (id->dst_ip6.__u6_addr.__u6_addr32[3]) ^ + (id->src_ip6.__u6_addr.__u6_addr32[2]) ^ + (id->src_ip6.__u6_addr.__u6_addr32[3])); + else +#endif /* INET6 */ + i = (id->dst_ip) ^ (id->src_ip); + i ^= (id->dst_port) ^ (id->src_port); + return (i); +} + +static __inline uint32_t +hash_parent(const struct ipfw_flow_id *id, const void *rule) +{ + + return (hash_packet(id) ^ ((uintptr_t)rule)); +} + +/* + * Print customizable flow id description via log(9) facility. + */ +static void +print_dyn_rule_flags(const struct ipfw_flow_id *id, int dyn_type, + int log_flags, char *prefix, char *postfix) +{ + struct in_addr da; +#ifdef INET6 + char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN]; +#else + char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN]; +#endif + +#ifdef INET6 + if (IS_IP6_FLOW_ID(id)) { + ip6_sprintf(src, &id->src_ip6); + ip6_sprintf(dst, &id->dst_ip6); + } else +#endif + { + da.s_addr = htonl(id->src_ip); + inet_ntop(AF_INET, &da, src, sizeof(src)); + da.s_addr = htonl(id->dst_ip); + inet_ntop(AF_INET, &da, dst, sizeof(dst)); + } + log(log_flags, "ipfw: %s type %d %s %d -> %s %d, %d %s\n", + prefix, dyn_type, src, id->src_port, dst, + id->dst_port, V_dyn_count, postfix); +} + +#define print_dyn_rule(id, dtype, prefix, postfix) \ + print_dyn_rule_flags(id, dtype, LOG_DEBUG, prefix, postfix) + +#define TIME_LEQ(a,b) ((int)((a)-(b)) <= 0) +#define TIME_LE(a,b) ((int)((a)-(b)) < 0) +#define _SEQ_GE(a,b) ((int)(a) - (int)(b) >= 0) +#define BOTH_SYN (TH_SYN | (TH_SYN << 8)) +#define BOTH_FIN (TH_FIN | (TH_FIN << 8)) +#define TCP_FLAGS (TH_FLAGS | (TH_FLAGS << 8)) +#define ACK_FWD 0x10000 /* fwd ack seen */ +#define ACK_REV 0x20000 /* rev ack seen */ + +/* + * Update ULP specific state. + * For TCP we keep sequence numbers and flags. For other protocols + * currently we update only expire time. Packets and bytes counters + * are also updated here. + */ +static void +dyn_update_proto_state_locked(struct dyn_data *data, + const struct ipfw_flow_id *pkt, const void *ulp, int pktlen, int dir) +{ + + if (pkt->proto != IPPROTO_TCP) + data->expire = time_uptime + ((pkt->proto == IPPROTO_UDP) ? + V_dyn_udp_lifetime : V_dyn_short_lifetime); + else { /* IPPROTO_TCP */ + const struct tcphdr *tcp; + uint32_t ack; + uint8_t flags; + + MPASS(ulp != NULL); + tcp = (const struct tcphdr *)ulp; + flags = pkt->_flags & (TH_FIN | TH_SYN | TH_RST); + data->state |= (dir == MATCH_FORWARD) ? flags : (flags << 8); + switch (data->state & TCP_FLAGS) { + case TH_SYN: /* opening */ + data->expire = time_uptime + V_dyn_syn_lifetime; + break; + + case BOTH_SYN: /* move to established */ + case BOTH_SYN | TH_FIN: /* one side tries to close */ + case BOTH_SYN | (TH_FIN << 8): + ack = ntohl(tcp->th_ack); + if (dir == MATCH_FORWARD) { + if (data->ack_fwd == 0 || + _SEQ_GE(ack, data->ack_fwd)) { + data->ack_fwd = ack; + data->state |= ACK_FWD; + } + } else { + if (data->ack_rev == 0 || + _SEQ_GE(ack, data->ack_rev)) { + data->ack_rev = ack; + data->state |= ACK_REV; + } + } + if ((data->state & (ACK_FWD | ACK_REV)) == + (ACK_FWD | ACK_REV)) { + data->expire = time_uptime + + V_dyn_ack_lifetime; + data->state &= ~(ACK_FWD | ACK_REV); + } + break; + + case BOTH_SYN | BOTH_FIN: /* both sides closed */ + if (V_dyn_fin_lifetime >= V_dyn_keepalive_period) + V_dyn_fin_lifetime = + V_dyn_keepalive_period - 1; + data->expire = time_uptime + V_dyn_fin_lifetime; + break; + + default: +#if 0 + /* + * reset or some invalid combination, but can also + * occur if we use keep-state the wrong way. + */ + if ((data->state & ((TH_RST << 8) | TH_RST)) == 0) + printf("invalid state: 0x%x\n", state); +#endif + if (V_dyn_rst_lifetime >= V_dyn_keepalive_period) + V_dyn_rst_lifetime = + V_dyn_keepalive_period - 1; + data->expire = time_uptime + V_dyn_rst_lifetime; + break; + } + } + if (dir == MATCH_FORWARD) + DYN_COUNTER_INC(data, fwd, pktlen); + else + DYN_COUNTER_INC(data, rev, pktlen); +} + +static void +dyn_update_proto_state(struct dyn_data *data, const struct ipfw_flow_id *id, + const void *ulp, int pktlen, int dir) +{ + + /* XXXAE: we need some lightweise way to synchronize writers */ + /* NOTE: we are in critical section here. */ + dyn_update_proto_state_locked(data, id, ulp, pktlen, dir); +} + +/* + * Lookup IPv4 state. + * Must be called in critical section. + */ +struct dyn_ipv4_state * +dyn_lookup_ipv4_state(const struct ipfw_flow_id *pkt, const void *ulp, + struct ipfw_dyn_info *info, int pktlen) +{ + struct dyn_ipv4_state *s; + uint32_t version, bucket; + + bucket = DYN_BUCKET(info->hashval, V_curr_dyn_buckets); + info->version = DYN_BUCKET_VERSION(bucket, ipv4_add); + DYN_CNTR_INC(TOTAL); +restart: + version = DYN_BUCKET_VERSION(bucket, ipv4_del); + CK_SLIST_FOREACH(s, &V_dyn_ipv4[bucket], entry) { + DYNSTATE_PROTECT(s); + if (version != DYN_BUCKET_VERSION(bucket, ipv4_del)) { + DYN_CNTR_INC(RESTART); + goto restart; + } + + if (s->proto != pkt->proto || + s->kidx != info->kidx) + continue; + if (s->sport == pkt->src_port && s->dport == pkt->dst_port && + s->src == pkt->src_ip && s->dst == pkt->dst_ip) { + info->direction = MATCH_FORWARD; + break; + } + if (s->sport == pkt->dst_port && s->dport == pkt->src_port && + s->src == pkt->dst_ip && s->dst == pkt->src_ip) { + info->direction = MATCH_REVERSE; + break; + } + } + + if (s != NULL) + dyn_update_proto_state(s->data, pkt, ulp, pktlen, + info->direction); + return (s); +} + +/* + * Lookup IPv4 state. + * Simplifed version is used to check that matching state doesn't exist. + */ +static int +dyn_lookup_ipv4_state_locked(const struct ipfw_flow_id *pkt, uint32_t bucket, + uint16_t kidx) +{ + struct dyn_ipv4_state *s; + + DYN_BUCKET_ASSERT(bucket); + CK_SLIST_FOREACH(s, &V_dyn_ipv4[bucket], entry) { + if (s->proto != pkt->proto || + s->kidx != kidx) + continue; + if (s->sport == pkt->src_port && s->dport == pkt->dst_port && + s->src == pkt->src_ip && s->dst == pkt->dst_ip) + break; + if (s->sport == pkt->dst_port && s->dport == pkt->src_port && + s->src == pkt->dst_ip && s->dst == pkt->src_ip) + break; + } + return (s != NULL); +} + +struct dyn_ipv4_state * +dyn_lookup_ipv4_parent(const struct ipfw_flow_id *pkt, const void *rule, + uint32_t hashval) +{ + struct dyn_ipv4_state *s; + uint32_t version, bucket; + + bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets); + DYN_CNTR_INC(TOTAL); +restart: + version = DYN_BUCKET_VERSION(bucket, ipv4_parent_del); + CK_SLIST_FOREACH(s, &V_dyn_ipv4_parent[bucket], entry) { + DYNSTATE_PROTECT(s); + if (version != DYN_BUCKET_VERSION(bucket, ipv4_parent_del)) { + DYN_CNTR_INC(RESTART); + goto restart; + } + /* + * NOTE: we do not need to check kidx, because parent rule + * can not create states with different kidx. + * And parent rule always created for forward direction. + */ + if (s->limit->parent == rule && + s->proto == pkt->proto && + s->sport == pkt->src_port && + s->dport == pkt->dst_port && + s->src == pkt->src_ip && s->dst == pkt->dst_ip) { + DYN_SETTIME(s->limit->expire, time_uptime + + V_dyn_short_lifetime); + break; + } + } + return (s); +} + +struct dyn_ipv4_state * +dyn_lookup_ipv4_parent_locked(const struct ipfw_flow_id *pkt, + const void *rule, uint32_t bucket) +{ + struct dyn_ipv4_state *s; + + DYN_BUCKET_ASSERT(bucket); + CK_SLIST_FOREACH(s, &V_dyn_ipv4_parent[bucket], entry) { + if (s->limit->parent == rule && + s->proto == pkt->proto && + s->sport == pkt->src_port && + s->dport == pkt->dst_port && + s->src == pkt->src_ip && s->dst == pkt->dst_ip) + break; + } + return (s); +} + + +#ifdef INET6 +static uint32_t +dyn_getscopeid(const struct ip_fw_args *args) +{ + + /* + * If source or destination address is an scopeid address, we need + * determine the scope zone id to resolve address scope ambiguity. + */ + if (IN6_IS_ADDR_LINKLOCAL(&args->f_id.src_ip6) || + IN6_IS_ADDR_LINKLOCAL(&args->f_id.dst_ip6)) { + MPASS(args->oif != NULL || + args->m->m_pkthdr.rcvif != NULL); + return (in6_getscopezone(args->oif != NULL ? args->oif: + args->m->m_pkthdr.rcvif, IPV6_ADDR_SCOPE_LINKLOCAL)); + } + return (0); +} + +/* + * Lookup IPv6 state. + * Must be called in critical section. + */ +static struct dyn_ipv6_state * +dyn_lookup_ipv6_state(const struct ipfw_flow_id *pkt, uint32_t zoneid, + const void *ulp, struct ipfw_dyn_info *info, int pktlen) +{ + struct dyn_ipv6_state *s; + uint32_t version, bucket; + + bucket = DYN_BUCKET(info->hashval, V_curr_dyn_buckets); + info->version = DYN_BUCKET_VERSION(bucket, ipv6_add); + DYN_CNTR_INC(TOTAL); +restart: + version = DYN_BUCKET_VERSION(bucket, ipv6_del); + CK_SLIST_FOREACH(s, &V_dyn_ipv6[bucket], entry) { + DYNSTATE_PROTECT(s); + if (version != DYN_BUCKET_VERSION(bucket, ipv6_del)) { + DYN_CNTR_INC(RESTART); + goto restart; + } + + if (s->proto != pkt->proto || s->kidx != info->kidx || + s->zoneid != zoneid) + continue; + if (s->sport == pkt->src_port && s->dport == pkt->dst_port && + IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) && + IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) { + info->direction = MATCH_FORWARD; + break; + } + if (s->sport == pkt->dst_port && s->dport == pkt->src_port && + IN6_ARE_ADDR_EQUAL(&s->src, &pkt->dst_ip6) && + IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->src_ip6)) { + info->direction = MATCH_REVERSE; + break; + } + } + if (s != NULL) + dyn_update_proto_state(s->data, pkt, ulp, pktlen, + info->direction); + return (s); +} + +/* + * Lookup IPv6 state. + * Simplifed version is used to check that matching state doesn't exist. + */ +static int +dyn_lookup_ipv6_state_locked(const struct ipfw_flow_id *pkt, uint32_t zoneid, + uint32_t bucket, uint16_t kidx) +{ + struct dyn_ipv6_state *s; + + DYN_BUCKET_ASSERT(bucket); + CK_SLIST_FOREACH(s, &V_dyn_ipv6[bucket], entry) { + if (s->proto != pkt->proto || s->kidx != kidx || + s->zoneid != zoneid) + continue; + if (s->sport == pkt->src_port && s->dport == pkt->dst_port && + IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) && + IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) + break; + if (s->sport == pkt->dst_port && s->dport == pkt->src_port && + IN6_ARE_ADDR_EQUAL(&s->src, &pkt->dst_ip6) && + IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->src_ip6)) + break; + } + return (s != NULL); +} + +struct dyn_ipv6_state * +dyn_lookup_ipv6_parent(const struct ipfw_flow_id *pkt, uint32_t zoneid, + const void *rule, uint32_t hashval) +{ + struct dyn_ipv6_state *s; + uint32_t version, bucket; + + bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets); + DYN_CNTR_INC(TOTAL); +restart: + version = DYN_BUCKET_VERSION(bucket, ipv6_parent_del); + CK_SLIST_FOREACH(s, &V_dyn_ipv6_parent[bucket], entry) { + DYNSTATE_PROTECT(s); + if (version != DYN_BUCKET_VERSION(bucket, ipv6_parent_del)) { + DYN_CNTR_INC(RESTART); + goto restart; + } + /* + * NOTE: we do not need to check kidx, because parent rule + * can not create states with different kidx. + * Also parent rule always created for forward direction. + */ + if (s->limit->parent == rule && + s->proto == pkt->proto && + s->sport == pkt->src_port && + s->dport == pkt->dst_port && s->zoneid == zoneid && + IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) && + IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) { + DYN_SETTIME(s->limit->expire, time_uptime + + V_dyn_short_lifetime); + break; + } + } + return (s); +} + +struct dyn_ipv6_state * +dyn_lookup_ipv6_parent_locked(const struct ipfw_flow_id *pkt, uint32_t zoneid, + const void *rule, uint32_t bucket) +{ + struct dyn_ipv6_state *s; + + DYN_BUCKET_ASSERT(bucket); + CK_SLIST_FOREACH(s, &V_dyn_ipv6_parent[bucket], entry) { + if (s->limit->parent == rule && + s->proto == pkt->proto && + s->sport == pkt->src_port && + s->dport == pkt->dst_port && s->zoneid == zoneid && + IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) && + IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) + break; + } + return (s); +} + +#endif /* INET6 */ + +/* + * Lookup dynamic state. + * pkt - filled by ipfw_chk() ipfw_flow_id; + * ulp - determined by ipfw_chk() upper level protocol header; + * match_direction - direction of matched state to return back; + * kidx - index of named object to match. + * Returns pointer to state's parent rule and direction. If there is + * no state, NULL is returned. + * On match ipfw_dyn_lookup() updates state's counters. + */ +struct ip_fw * +ipfw_dyn_lookup_state(const struct ip_fw_args *args, const void *ulp, + int pktlen, const ipfw_insn *cmd, struct ipfw_dyn_info *info) +{ + struct dyn_data *data; + struct ip_fw *rule; + + IPFW_RLOCK_ASSERT(&V_layer3_chain); + + data = NULL; + rule = NULL; + info->kidx = cmd->arg1; + info->direction = MATCH_NONE; + info->hashval = hash_packet(&args->f_id); + + DYNSTATE_CRITICAL_ENTER(); + if (IS_IP4_FLOW_ID(&args->f_id)) { + struct dyn_ipv4_state *s; + + s = dyn_lookup_ipv4_state(&args->f_id, ulp, info, pktlen); + if (s != NULL) { + /* + * Dynamic states are created using the same 5-tuple, + * so it is assumed, that parent rule for O_LIMIT + * state has the same address family. + */ + data = s->data; + if (s->type == O_LIMIT) { + s = s->data->parent; + rule = s->limit->parent; + } else + rule = s->data->parent; + } + } +#ifdef INET6 + else if (IS_IP6_FLOW_ID(&args->f_id)) { + struct dyn_ipv6_state *s; + + s = dyn_lookup_ipv6_state(&args->f_id, dyn_getscopeid(args), + ulp, info, pktlen); + if (s != NULL) { + data = s->data; + if (s->type == O_LIMIT) { + s = s->data->parent; + rule = s->limit->parent; + } else + rule = s->data->parent; + } + } +#endif + if (data != NULL) { + /* + * If cached chain id is the same, we can avoid rule index + * lookup. Otherwise do lookup and update chain_id and f_pos. + * It is safe even if there is concurrent thread that want + * update the same state, because chain->id can be changed + * only under IPFW_WLOCK(). + */ + if (data->chain_id != V_layer3_chain.id) { + data->f_pos = ipfw_find_rule(&V_layer3_chain, + rule->rulenum, rule->id); + data->chain_id = V_layer3_chain.id; + } + info->f_pos = data->f_pos; + } + DYNSTATE_CRITICAL_EXIT(); + return (rule); +} + +static struct dyn_parent * +dyn_alloc_parent(void *parent, uint32_t hashval) +{ + struct dyn_parent *limit; + + limit = uma_zalloc(V_dyn_parent_zone, M_NOWAIT | M_ZERO); + if (limit == NULL) { + if (last_log != time_uptime) { + last_log = time_uptime; + log(LOG_DEBUG, + "ipfw: Cannot allocate parent dynamic state, " + "consider increasing " + "net.inet.ip.fw.dyn_parent_max\n"); + } + return (NULL); + } + + limit->parent = parent; + limit->hashval = hashval; + limit->expire = time_uptime + V_dyn_short_lifetime; + return (limit); +} + +static struct dyn_data * +dyn_alloc_dyndata(void *parent, const struct ipfw_flow_id *pkt, + const void *ulp, int pktlen, uint32_t hashval, uint16_t fibnum) +{ + struct dyn_data *data; + + data = uma_zalloc(V_dyn_data_zone, M_NOWAIT | M_ZERO); + if (data == NULL) { + if (last_log != time_uptime) { + last_log = time_uptime; + log(LOG_DEBUG, + "ipfw: Cannot allocate dynamic state, " + "consider increasing net.inet.ip.fw.dyn_max\n"); + } + return (NULL); + } + + data->parent = parent; + data->fibnum = fibnum; + data->hashval = hashval; + data->expire = time_uptime + V_dyn_syn_lifetime; + dyn_update_proto_state_locked(data, pkt, ulp, pktlen, MATCH_FORWARD); + return (data); +} + +static struct dyn_ipv4_state * +dyn_alloc_ipv4_state(const struct ipfw_flow_id *pkt, uint16_t kidx, + uint8_t type) +{ + struct dyn_ipv4_state *s; + + s = uma_zalloc(V_dyn_ipv4_zone, M_NOWAIT | M_ZERO); + if (s == NULL) + return (NULL); + + s->type = type; + s->kidx = kidx; + s->proto = pkt->proto; + s->sport = pkt->src_port; + s->dport = pkt->dst_port; + s->src = pkt->src_ip; + s->dst = pkt->dst_ip; + return (s); +} + +/* + * Add IPv4 parent state. + * Returns pointer to parent state. When it is not NULL we are in + * critical section and pointer protected by hazard pointer. + * When some error occurs, it returns NULL and exit from critical section + * is not needed. + */ +static struct dyn_ipv4_state * +dyn_add_ipv4_parent(void *rule, const struct ipfw_flow_id *pkt, + uint32_t hashval, uint32_t version, uint16_t kidx) +{ + struct dyn_ipv4_state *s; + struct dyn_parent *limit; + uint32_t bucket; + + bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets); + DYN_CNTR_INC(CREATED); + DYN_BUCKET_LOCK(bucket); + if (version != DYN_BUCKET_VERSION(bucket, ipv4_parent_add)) { + /* + * Bucket version has been changed since last lookup, + * do lookup again to be sure that state does not exist. + */ + s = dyn_lookup_ipv4_parent_locked(pkt, rule, bucket); + if (s != NULL) { + /* + * Simultaneous thread has already created this + * state. Just return it. + */ + DYNSTATE_CRITICAL_ENTER(); + DYNSTATE_PROTECT(s); + DYN_BUCKET_UNLOCK(bucket); + return (s); + } + } else + DYN_CNTR_INC(SKIPPED); + + limit = dyn_alloc_parent(rule, hashval); + if (limit == NULL) { + DYN_BUCKET_UNLOCK(bucket); + return (NULL); + } + + s = dyn_alloc_ipv4_state(pkt, kidx, O_LIMIT_PARENT); + if (s == NULL) { + DYN_BUCKET_UNLOCK(bucket); + uma_zfree(V_dyn_parent_zone, limit); + return (NULL); + } + + s->limit = limit; + CK_SLIST_INSERT_HEAD(&V_dyn_ipv4_parent[bucket], s, entry); + DYN_COUNT_INC(dyn_parent_count); + DYN_BUCKET_VERSION_BUMP(bucket, ipv4_parent_add); + DYNSTATE_CRITICAL_ENTER(); + DYNSTATE_PROTECT(s); + DYN_BUCKET_UNLOCK(bucket); + return (s); +} + +static int +dyn_add_ipv4_state(void *parent, const struct ipfw_flow_id *pkt, + const void *ulp, int pktlen, uint32_t hashval, struct ipfw_dyn_info *info, + uint16_t fibnum, uint16_t kidx, uint8_t type) +{ + struct dyn_ipv4_state *s; + void *data; + uint32_t bucket; + + bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets); + DYN_BUCKET_LOCK(bucket); + if (info->direction == MATCH_UNKNOWN || + info->kidx != kidx || + info->hashval != hashval || + info->version != DYN_BUCKET_VERSION(bucket, ipv4_add)) { + /* + * Bucket version has been changed since last lookup, + * do lookup again to be sure that state does not exist. + */ + if (dyn_lookup_ipv4_state_locked(pkt, bucket, kidx) != 0) { + DYN_BUCKET_UNLOCK(bucket); + return (EEXIST); + } + } else + DYN_CNTR_INC(SKIPPED); + + data = dyn_alloc_dyndata(parent, pkt, ulp, pktlen, hashval, fibnum); + if (data == NULL) { + DYN_BUCKET_UNLOCK(bucket); + return (ENOMEM); + } + + s = dyn_alloc_ipv4_state(pkt, kidx, type); + if (s == NULL) { + DYN_BUCKET_UNLOCK(bucket); + uma_zfree(V_dyn_data_zone, data); + return (ENOMEM); + } + + s->data = data; + CK_SLIST_INSERT_HEAD(&V_dyn_ipv4[bucket], s, entry); + DYN_COUNT_INC(dyn_count); + DYN_BUCKET_VERSION_BUMP(bucket, ipv4_add); + DYN_BUCKET_UNLOCK(bucket); + return (0); +} + +#ifdef INET6 +static struct dyn_ipv6_state * +dyn_alloc_ipv6_state(const struct ipfw_flow_id *pkt, uint32_t zoneid, + uint16_t kidx, uint8_t type) +{ + struct dyn_ipv6_state *s; + + s = uma_zalloc(V_dyn_ipv6_zone, M_NOWAIT | M_ZERO); + if (s == NULL) + return (NULL); + + s->type = type; + s->kidx = kidx; + s->zoneid = zoneid; + s->proto = pkt->proto; + s->sport = pkt->src_port; + s->dport = pkt->dst_port; + s->src = pkt->src_ip6; + s->dst = pkt->dst_ip6; + return (s); +} + +/* + * Add IPv6 parent state. + * Returns pointer to parent state. When it is not NULL we are in + * critical section and pointer protected by hazard pointer. + * When some error occurs, it return NULL and exit from critical section + * is not needed. + */ +static struct dyn_ipv6_state * +dyn_add_ipv6_parent(void *rule, const struct ipfw_flow_id *pkt, + uint32_t zoneid, uint32_t hashval, uint32_t version, uint16_t kidx) +{ + struct dyn_ipv6_state *s; + struct dyn_parent *limit; + uint32_t bucket; + + bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets); + DYN_CNTR_INC(CREATED); + DYN_BUCKET_LOCK(bucket); + if (version != DYN_BUCKET_VERSION(bucket, ipv6_parent_add)) { + /* + * Bucket version has been changed since last lookup, + * do lookup again to be sure that state does not exist. + */ + s = dyn_lookup_ipv6_parent_locked(pkt, zoneid, rule, bucket); + if (s != NULL) { + /* + * Simultaneous thread has already created this + * state. Just return it. + */ + DYNSTATE_CRITICAL_ENTER(); + DYNSTATE_PROTECT(s); + DYN_BUCKET_UNLOCK(bucket); + return (s); + } + } else + DYN_CNTR_INC(SKIPPED); + + limit = dyn_alloc_parent(rule, hashval); + if (limit == NULL) { + DYN_BUCKET_UNLOCK(bucket); + return (NULL); + } + + s = dyn_alloc_ipv6_state(pkt, zoneid, kidx, O_LIMIT_PARENT); + if (s == NULL) { + DYN_BUCKET_UNLOCK(bucket); + uma_zfree(V_dyn_parent_zone, limit); + return (NULL); + } + + s->limit = limit; + CK_SLIST_INSERT_HEAD(&V_dyn_ipv6_parent[bucket], s, entry); + DYN_COUNT_INC(dyn_parent_count); + DYN_BUCKET_VERSION_BUMP(bucket, ipv6_parent_add); + DYNSTATE_CRITICAL_ENTER(); + DYNSTATE_PROTECT(s); + DYN_BUCKET_UNLOCK(bucket); + return (s); +} + +static int +dyn_add_ipv6_state(void *parent, const struct ipfw_flow_id *pkt, + uint32_t zoneid, const void *ulp, int pktlen, uint32_t hashval, + struct ipfw_dyn_info *info, uint16_t fibnum, uint16_t kidx, uint8_t type) +{ + struct dyn_ipv6_state *s; + struct dyn_data *data; + uint32_t bucket; + + bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets); + DYN_BUCKET_LOCK(bucket); + if (info->direction == MATCH_UNKNOWN || + info->kidx != kidx || + info->hashval != hashval || + info->version != DYN_BUCKET_VERSION(bucket, ipv6_add)) { + /* + * Bucket version has been changed since last lookup, + * do lookup again to be sure that state does not exist. + */ + if (dyn_lookup_ipv6_state_locked(pkt, zoneid, bucket, + kidx) != 0) { + DYN_BUCKET_UNLOCK(bucket); + return (EEXIST); + } + } else + DYN_CNTR_INC(SKIPPED); + + data = dyn_alloc_dyndata(parent, pkt, ulp, pktlen, hashval, fibnum); + if (data == NULL) { + DYN_BUCKET_UNLOCK(bucket); + return (ENOMEM); + } + + s = dyn_alloc_ipv6_state(pkt, zoneid, kidx, type); + if (s == NULL) { + DYN_BUCKET_UNLOCK(bucket); + uma_zfree(V_dyn_data_zone, data); + return (ENOMEM); + } + + s->data = data; + CK_SLIST_INSERT_HEAD(&V_dyn_ipv6[bucket], s, entry); + DYN_COUNT_INC(dyn_count); + DYN_BUCKET_VERSION_BUMP(bucket, ipv6_add); + DYN_BUCKET_UNLOCK(bucket); + return (0); +} +#endif /* INET6 */ + +static int +dyn_get_parent_state(const struct ipfw_flow_id *pkt, uint32_t zoneid, + struct ip_fw *rule, uint32_t hashval, const ipfw_insn_limit *cmd, + uint32_t limit) +{ + char sbuf[24]; + struct dyn_parent *p; + uint32_t bucket, version; + + p = NULL; + bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets); + DYNSTATE_CRITICAL_ENTER(); + if (IS_IP4_FLOW_ID(pkt)) { + struct dyn_ipv4_state *s; + + version = DYN_BUCKET_VERSION(bucket, ipv4_parent_add); + s = dyn_lookup_ipv4_parent(pkt, rule, bucket); + if (s == NULL) { + /* + * Exit from critical section because dyn_add_parent() + * will acquire bucket lock. + */ + DYNSTATE_CRITICAL_EXIT(); + + s = dyn_add_ipv4_parent(rule, pkt, hashval, version, + cmd->o.arg1); + if (s == NULL) + return (ENOMEM); + /* Now we are in critical section again. */ + } + p = s->limit; + } +#ifdef INET6 + else if (IS_IP6_FLOW_ID(pkt)) { + struct dyn_ipv6_state *s; + + version = DYN_BUCKET_VERSION(bucket, ipv6_parent_add); + s = dyn_lookup_ipv6_parent(pkt, zoneid, rule, bucket); + if (s == NULL) { + /* + * Exit from critical section because dyn_add_parent() + * can acquire bucket mutex. + */ + DYNSTATE_CRITICAL_EXIT(); + + s = dyn_add_ipv6_parent(rule, pkt, zoneid, hashval, + version, cmd->o.arg1); + if (s == NULL) + return (ENOMEM); + /* Now we are in critical section again. */ + } + p = s->limit; + } +#endif + else { + DYNSTATE_CRITICAL_EXIT(); + return (EAFNOSUPPORT); + } + + MPASS(p != NULL); + /* Check the limit */ + if (DPARENT_COUNT(p) >= limit) { + DYNSTATE_CRITICAL_EXIT(); + if (V_fw_verbose && last_log != time_uptime) { + last_log = time_uptime; + snprintf(sbuf, sizeof(sbuf), "%u drop session", + rule->rulenum); + print_dyn_rule_flags(pkt, cmd->o.opcode, + LOG_SECURITY | LOG_DEBUG, sbuf, + "too many entries"); + } + return (EACCES); + } + + /* + * Take new session into account. + * The state will not expired until it has some child states. + */ + DPARENT_COUNT_INC(p); + DYNSTATE_CRITICAL_EXIT(); + return (0); +} + +/* + * Install dynamic state. + * chain - ipfw's instance; + * rule - the parent rule that installs the state; + * cmd - opcode that installs the state; + * args - ipfw arguments (usually we need only flow id and mbuf); + * tablearg - tablearg id. + * + * Returns non-zero value (failure) if state is not installed because + * of errors or because session limitations are enforced. + */ +int +ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule, + const ipfw_insn_limit *cmd, const struct ip_fw_args *args, + const void *ulp, int pktlen, struct ipfw_dyn_info *info, + uint32_t tablearg) +{ + struct ipfw_flow_id id; + uint32_t hashval, parent_hashval, zoneid; + int ret; + + MPASS(cmd->o.opcode == O_LIMIT || cmd->o.opcode == O_KEEP_STATE); + + if (cmd->o.opcode == O_LIMIT) { + /* Create masked flow id and calculate bucket */ + zoneid = 0; + id.addr_type = args->f_id.addr_type; + id.proto = args->f_id.proto; + id.fib = M_GETFIB(args->m); /* unused */ + id.src_port = (cmd->limit_mask & DYN_SRC_PORT) ? + args->f_id.src_port: 0; + id.dst_port = (cmd->limit_mask & DYN_DST_PORT) ? + args->f_id.dst_port: 0; + if (IS_IP4_FLOW_ID(&args->f_id)) { + id.src_ip = (cmd->limit_mask & DYN_SRC_ADDR) ? + args->f_id.src_ip: 0; + id.dst_ip = (cmd->limit_mask & DYN_DST_ADDR) ? + args->f_id.dst_ip: 0; + } +#ifdef INET6 + else if (IS_IP6_FLOW_ID(&args->f_id)) { + zoneid = dyn_getscopeid(args); + if (cmd->limit_mask & DYN_SRC_ADDR) + id.src_ip6 = args->f_id.src_ip6; + else + memset(&id.src_ip6, 0, sizeof(id.src_ip6)); + if (cmd->limit_mask & DYN_DST_ADDR) + id.dst_ip6 = args->f_id.dst_ip6; + else + memset(&id.dst_ip6, 0, sizeof(id.dst_ip6)); + } +#endif + else + return (EAFNOSUPPORT); + + parent_hashval = hash_parent(&id, rule); + ret = dyn_get_parent_state(&id, zoneid, rule, parent_hashval, + cmd, IP_FW_ARG_TABLEARG(chain, cmd->conn_limit, limit)); + if (ret != 0) + return (ret); + /* Limit is not reached, create new state. */ + } + + hashval = hash_packet(&args->f_id); + DYN_CNTR_INC(CREATED); + if (IS_IP4_FLOW_ID(&args->f_id)) + ret = dyn_add_ipv4_state(rule, &args->f_id, + ulp, pktlen, hashval, info, M_GETFIB(args->m), + cmd->o.arg1, cmd->o.opcode); +#ifdef INET6 + else if (IS_IP6_FLOW_ID(&args->f_id)) + ret = dyn_add_ipv6_state(rule, &args->f_id, + dyn_getscopeid(args), ulp, pktlen, hashval, info, + M_GETFIB(args->m), cmd->o.arg1, cmd->o.opcode); +#endif /* INET6 */ + else + ret = EAFNOSUPPORT; + + if (ret != 0 && cmd->o.opcode == O_LIMIT) { + /* + * We failed to create child state for O_LIMIT opcode. + * We already counted it in the parent and must revert + * counter back. + */ + DYNSTATE_CRITICAL_ENTER(); + if (IS_IP4_FLOW_ID(&id)) { + struct dyn_ipv4_state *s; + + s = dyn_lookup_ipv4_parent(&id, rule, parent_hashval); + if (s != NULL) + DPARENT_COUNT_DEC(s->limit); + } +#ifdef INET6 + else if (IS_IP6_FLOW_ID(&id)) { + struct dyn_ipv6_state *s; + + s = dyn_lookup_ipv6_parent(&id, zoneid, rule, + parent_hashval); + if (s != NULL) + DPARENT_COUNT_DEC(s->limit); + } +#endif /* INET6 */ + DYNSTATE_CRITICAL_EXIT(); + } + /* + * EEXIST means that simultaneous thread has created this + * state. Consider this as success. + * + * XXXAE: should we invalidate info content here? + */ + if (ret == EEXIST) + return (0); + return (ret); +} + +/* + * Free safe to remove state entries from expired lists. + */ +static void +dyn_free_states(struct ip_fw_chain *chain) +{ + struct dyn_ipv4_state *s4, *s4n; +#ifdef INET6 + struct dyn_ipv6_state *s6, *s6n; +#endif + int cached_count, i; + + /* + * We keep pointers to objects that are in use on each CPU + * in the per-cpu dyn_hp pointer. When object is going to be + * removed, first of it is unlinked from the corresponding + * list. This leads to changing of dyn_bucket_xxx_delver version. + * Unlinked objects is placed into corresponding dyn_expired_xxx + * list. Reader that is going to dereference object pointer checks + * dyn_bucket_xxx_delver version before and after storing pointer + * into dyn_hp. If version is the same, the object is protectet + * from freeing and it is safe to dereference. Othervise reader + * tries to iterate list again from the begining, but this object + * now unlinked and thus will not be accessible. + * + * Copy dyn_hp pointers for each CPU into dyn_hp_cache array. + * It does not matter that some pointer can be changed in + * time while we are copying. We need to check, that objects + * removed in the previous pass are not in use. And if dyn_hp + * pointer does not contain it in the time when we are copying, + * it will not appear there, because it is already unlinked. + * And for new pointers we will not free objects that will be + * unlinked in this pass. + */ + cached_count = 0; + CPU_FOREACH(i) { + dyn_hp_cache[cached_count] = DYNSTATE_GET(i); + if (dyn_hp_cache[cached_count] != NULL) + cached_count++; + } + + /* + * Free expired states that are safe to free. + * Check each entry from previous pass in the dyn_expired_xxx + * list, if pointer to the object is in the dyn_hp_cache array, + * keep it until next pass. Otherwise it is safe to free the + * object. + * + * XXXAE: optimize this to use SLIST_REMOVE_AFTER. + */ +#define DYN_FREE_STATES(s, next, name) do { \ + s = SLIST_FIRST(&V_dyn_expired_ ## name); \ + while (s != NULL) { \ + next = SLIST_NEXT(s, expired); \ + for (i = 0; i < cached_count; i++) \ + if (dyn_hp_cache[i] == s) \ + break; \ + if (i == cached_count) { \ + SLIST_REMOVE(&V_dyn_expired_ ## name, \ + s, dyn_ ## name ## _state, expired); \ + if (s->type == O_LIMIT_PARENT) \ + uma_zfree(V_dyn_parent_zone, s->limit); \ + else \ + uma_zfree(V_dyn_data_zone, s->data); \ + uma_zfree(V_dyn_ ## name ## _zone, s); \ + } \ + s = next; \ + } \ +} while (0) + + /* + * Protect access to expired lists with DYN_EXPIRED_LOCK. + * Userland can invoke ipfw_expire_dyn_states() to delete + * specific states, this will lead to modification of expired + * lists. + * + * XXXAE: do we need DYN_EXPIRED_LOCK? We can just use + * IPFW_UH_WLOCK to protect access to these lists. + */ + DYN_EXPIRED_LOCK(); + DYN_FREE_STATES(s4, s4n, ipv4); +#ifdef INET6 + DYN_FREE_STATES(s6, s6n, ipv6); +#endif + DYN_EXPIRED_UNLOCK(); +#undef DYN_FREE_STATES +} + +/* + * Unlink expired entries from states lists. + * @rt can be used to specify the range of states for deletion. + */ +static void +dyn_expire_states(struct ip_fw_chain *chain, ipfw_range_tlv *rt) +{ + struct dyn_ipv4_slist expired_ipv4; +#ifdef INET6 + struct dyn_ipv6_slist expired_ipv6; + struct dyn_ipv6_state *s6, *s6n, *s6p; +#endif + struct dyn_ipv4_state *s4, *s4n, *s4p; + int bucket, removed, length, max_length; + + /* + * Unlink expired states from each bucket. + * With acquired bucket lock iterate entries of each lists: + * ipv4, ipv4_parent, ipv6, and ipv6_parent. Check expired time + * and unlink entry from the list, link entry into temporary + * expired_xxx lists then bump "del" bucket version. + * + * When all buckets will be inspected, use temporary lists + * to find O_LIMIT states and decrement parent's refcount. + */ +#define DYN_UNLINK_STATES(s, prev, next, exp, af, name, extra) do { \ + length = 0; \ + removed = 0; \ + prev = NULL; \ + s = CK_SLIST_FIRST(&V_dyn_ ## name [bucket]); \ + while (s != NULL) { \ + next = CK_SLIST_NEXT(s, entry); \ + if (TIME_LEQ((s)->exp, time_uptime) && extra) { \ + if (prev != NULL) \ + CK_SLIST_REMOVE_AFTER(prev, entry); \ + else \ + CK_SLIST_REMOVE_HEAD( \ + &V_dyn_ ## name [bucket], entry); \ + removed++; \ + SLIST_INSERT_HEAD(&expired_ ## af, s, expired); \ + } else \ + prev = s; \ + s = next; \ + length++; \ + } \ + if (removed != 0) \ + DYN_BUCKET_VERSION_BUMP(bucket, name ## _del); \ + if (length > max_length) \ + max_length = length; \ +} while (0) + + SLIST_INIT(&expired_ipv4); +#ifdef INET6 + SLIST_INIT(&expired_ipv6); +#endif + max_length = 0; + for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) { + DYN_BUCKET_LOCK(bucket); + DYN_UNLINK_STATES(s4, s4p, s4n, limit->expire, ipv4, + ipv4_parent, (s4->limit->count == 0)); + DYN_UNLINK_STATES(s4, s4p, s4n, data->expire, ipv4, ipv4, + (rt == NULL || ipfw_match_range(s4->data->parent, rt))); +#ifdef INET6 + DYN_UNLINK_STATES(s6, s6p, s6n, limit->expire, ipv6, + ipv6_parent, (s6->limit->count == 0)); + DYN_UNLINK_STATES(s6, s6p, s6n, data->expire, ipv6, ipv6, + (rt == NULL || ipfw_match_range(s6->data->parent, rt))); +#endif + DYN_BUCKET_UNLOCK(bucket); + } + /* Update curr_max_length for statistics. */ + V_curr_max_length = max_length; + + /* + * Decrement parent state counter if some O_LIMIT states were + * removed. + */ +#define DYN_UNREF_STATES(s, tmp, af) \ + SLIST_FOREACH(s, &expired_ ## af, expired) { \ + if (s->type == O_LIMIT_PARENT) \ + DYN_COUNT_DEC(dyn_parent_count); \ + else \ + DYN_COUNT_DEC(dyn_count); \ + if (s->type != O_LIMIT) \ + continue; \ + tmp = s->data->parent; \ + DPARENT_COUNT_DEC(tmp->limit); \ + } + + DYN_UNREF_STATES(s4, s4n, ipv4); +#ifdef INET6 + DYN_UNREF_STATES(s6, s6n, ipv6); +#endif + /* + * Concatenate temporary lists with global expired lists. + */ + DYN_EXPIRED_LOCK(); + SLIST_CONCAT(&V_dyn_expired_ipv4, &expired_ipv4, + dyn_ipv4_state, expired); +#ifdef INET6 + SLIST_CONCAT(&V_dyn_expired_ipv6, &expired_ipv6, + dyn_ipv6_state, expired); +#endif + DYN_EXPIRED_UNLOCK(); +#undef DYN_UNLINK_STATES +#undef DYN_UNREF_STATES +} + +/* + * Generate a TCP packet, containing either a RST or a keepalive. + * When flags & TH_RST, we are sending a RST packet, because of a + * "reset" action matched the packet. + * Otherwise we are sending a keepalive, and flags & TH_ + * The 'replyto' mbuf is the mbuf being replied to, if any, and is required + * so that MAC can label the reply appropriately. + */ +struct mbuf * +ipfw_send_pkt(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t seq, + u_int32_t ack, int flags) +{ + struct mbuf *m = NULL; /* stupid compiler */ + struct ip *h = NULL; /* stupid compiler */ +#ifdef INET6 + struct ip6_hdr *h6 = NULL; +#endif + struct tcphdr *th = NULL; + int len, dir; + + MGETHDR(m, M_NOWAIT, MT_DATA); + if (m == NULL) + return (NULL); + + M_SETFIB(m, id->fib); +#ifdef MAC + if (replyto != NULL) + mac_netinet_firewall_reply(replyto, m); + else + mac_netinet_firewall_send(m); +#else + (void)replyto; /* don't warn about unused arg */ +#endif + + switch (id->addr_type) { + case 4: + len = sizeof(struct ip) + sizeof(struct tcphdr); + break; +#ifdef INET6 + case 6: + len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); + break; +#endif + default: + /* XXX: log me?!? */ + FREE_PKT(m); + return (NULL); + } + dir = ((flags & (TH_SYN | TH_RST)) == TH_SYN); + + m->m_data += max_linkhdr; + m->m_flags |= M_SKIP_FIREWALL; + m->m_pkthdr.len = m->m_len = len; + m->m_pkthdr.rcvif = NULL; + bzero(m->m_data, len); + + switch (id->addr_type) { + case 4: + h = mtod(m, struct ip *); + + /* prepare for checksum */ + h->ip_p = IPPROTO_TCP; + h->ip_len = htons(sizeof(struct tcphdr)); + if (dir) { + h->ip_src.s_addr = htonl(id->src_ip); + h->ip_dst.s_addr = htonl(id->dst_ip); + } else { + h->ip_src.s_addr = htonl(id->dst_ip); + h->ip_dst.s_addr = htonl(id->src_ip); + } + + th = (struct tcphdr *)(h + 1); + break; +#ifdef INET6 + case 6: + h6 = mtod(m, struct ip6_hdr *); + + /* prepare for checksum */ + h6->ip6_nxt = IPPROTO_TCP; + h6->ip6_plen = htons(sizeof(struct tcphdr)); + if (dir) { + h6->ip6_src = id->src_ip6; + h6->ip6_dst = id->dst_ip6; + } else { + h6->ip6_src = id->dst_ip6; + h6->ip6_dst = id->src_ip6; + } + + th = (struct tcphdr *)(h6 + 1); + break; +#endif + } + + if (dir) { + th->th_sport = htons(id->src_port); + th->th_dport = htons(id->dst_port); + } else { + th->th_sport = htons(id->dst_port); + th->th_dport = htons(id->src_port); + } + th->th_off = sizeof(struct tcphdr) >> 2; + + if (flags & TH_RST) { + if (flags & TH_ACK) { + th->th_seq = htonl(ack); + th->th_flags = TH_RST; + } else { + if (flags & TH_SYN) + seq++; + th->th_ack = htonl(seq); + th->th_flags = TH_RST | TH_ACK; + } + } else { + /* + * Keepalive - use caller provided sequence numbers + */ + th->th_seq = htonl(seq); + th->th_ack = htonl(ack); + th->th_flags = TH_ACK; + } + + switch (id->addr_type) { + case 4: + th->th_sum = in_cksum(m, len); + + /* finish the ip header */ + h->ip_v = 4; + h->ip_hl = sizeof(*h) >> 2; + h->ip_tos = IPTOS_LOWDELAY; + h->ip_off = htons(0); + h->ip_len = htons(len); + h->ip_ttl = V_ip_defttl; + h->ip_sum = 0; + break; +#ifdef INET6 + case 6: + th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(*h6), + sizeof(struct tcphdr)); + + /* finish the ip6 header */ + h6->ip6_vfc |= IPV6_VERSION; + h6->ip6_hlim = IPV6_DEFHLIM; + break; +#endif + } + + return (m); +} + +static struct mbuf * +dyn_mgethdr(int len, uint16_t fibnum) +{ + struct mbuf *m; + + m = m_gethdr(M_NOWAIT, MT_DATA); + if (m == NULL) + return (NULL); +#ifdef MAC + mac_netinet_firewall_send(m); +#endif + M_SETFIB(m, fibnum); + m->m_data += max_linkhdr; + m->m_flags |= M_SKIP_FIREWALL; + m->m_len = m->m_pkthdr.len = len; + bzero(m->m_data, len); + return (m); +} + +static void +dyn_make_keepalive_ipv4(struct mbuf *m, in_addr_t src, in_addr_t dst, + uint32_t seq, uint32_t ack, uint16_t sport, uint16_t dport) +{ + struct tcphdr *tcp; + struct ip *ip; + + ip = mtod(m, struct ip *); + ip->ip_v = 4; + ip->ip_hl = sizeof(*ip) >> 2; + ip->ip_tos = IPTOS_LOWDELAY; + ip->ip_len = htons(m->m_len); + ip->ip_ttl = V_ip_defttl; + ip->ip_p = IPPROTO_TCP; + ip->ip_src.s_addr = htonl(src); + ip->ip_dst.s_addr = htonl(dst); + + tcp = mtodo(m, sizeof(struct ip)); + tcp->th_sport = htons(sport); + tcp->th_dport = htons(dport); + tcp->th_off = sizeof(struct tcphdr) >> 2; + tcp->th_seq = htonl(seq); + tcp->th_ack = htonl(ack); + tcp->th_flags = TH_ACK; + tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, + htons(sizeof(struct tcphdr) + IPPROTO_TCP)); +} + +static void +dyn_enqueue_keepalive_ipv4(struct mbufq *q, const struct dyn_ipv4_state *s) +{ + struct mbuf *m; + + if ((s->data->state & ACK_FWD) == 0 && s->data->ack_fwd > 0) { + m = dyn_mgethdr(sizeof(struct ip) + sizeof(struct tcphdr), + s->data->fibnum); + if (m != NULL) { + dyn_make_keepalive_ipv4(m, s->dst, s->src, + s->data->ack_fwd - 1, s->data->ack_rev, + s->dport, s->sport); + if (mbufq_enqueue(q, m)) { + m_freem(m); + log(LOG_DEBUG, "ipfw: limit for IPv4 keepalive" + " queue is reached.\n"); + return; + } + } + } + + if ((s->data->state & ACK_REV) == 0 && s->data->ack_rev > 0) { + m = dyn_mgethdr(sizeof(struct ip) + sizeof(struct tcphdr), + s->data->fibnum); + if (m != NULL) { + dyn_make_keepalive_ipv4(m, s->src, s->dst, + s->data->ack_rev - 1, s->data->ack_fwd, + s->sport, s->dport); + if (mbufq_enqueue(q, m)) { + m_freem(m); + log(LOG_DEBUG, "ipfw: limit for IPv4 keepalive" + " queue is reached.\n"); + return; + } + } + } +} + +/* + * Prepare and send keep-alive packets. + */ +static void +dyn_send_keepalive_ipv4(struct ip_fw_chain *chain) +{ + struct mbufq q; + struct mbuf *m; + struct dyn_ipv4_state *s; + uint32_t bucket; + + mbufq_init(&q, DYN_KEEPALIVE_MAXQ); + IPFW_UH_RLOCK(chain); + /* + * It is safe to not use hazard pointer and just do lockless + * access to the lists, because states entries can not be deleted + * while we hold IPFW_UH_RLOCK. + */ + for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) { + CK_SLIST_FOREACH(s, &V_dyn_ipv4[bucket], entry) { + /* + * Only established TCP connections that will + * become expired withing dyn_keepalive_interval. + */ + if (s->proto != IPPROTO_TCP || + (s->data->state & BOTH_SYN) != BOTH_SYN || + TIME_LEQ(time_uptime + V_dyn_keepalive_interval, + s->data->expire)) + continue; + dyn_enqueue_keepalive_ipv4(&q, s); + } + } + IPFW_UH_RUNLOCK(chain); + while ((m = mbufq_dequeue(&q)) != NULL) + ip_output(m, NULL, NULL, 0, NULL, NULL); +} + +#ifdef INET6 +static void +dyn_make_keepalive_ipv6(struct mbuf *m, const struct in6_addr *src, + const struct in6_addr *dst, uint32_t zoneid, uint32_t seq, uint32_t ack, + uint16_t sport, uint16_t dport) +{ + struct tcphdr *tcp; + struct ip6_hdr *ip6; + + ip6 = mtod(m, struct ip6_hdr *); + ip6->ip6_vfc |= IPV6_VERSION; + ip6->ip6_plen = htons(sizeof(struct tcphdr)); + ip6->ip6_nxt = IPPROTO_TCP; + ip6->ip6_hlim = IPV6_DEFHLIM; + ip6->ip6_src = *src; + if (IN6_IS_ADDR_LINKLOCAL(src)) + ip6->ip6_src.s6_addr16[1] = htons(zoneid & 0xffff); + ip6->ip6_dst = *dst; + if (IN6_IS_ADDR_LINKLOCAL(dst)) + ip6->ip6_dst.s6_addr16[1] = htons(zoneid & 0xffff); + + tcp = mtodo(m, sizeof(struct ip6_hdr)); + tcp->th_sport = htons(sport); + tcp->th_dport = htons(dport); + tcp->th_off = sizeof(struct tcphdr) >> 2; + tcp->th_seq = htonl(seq); + tcp->th_ack = htonl(ack); + tcp->th_flags = TH_ACK; + tcp->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr), + IPPROTO_TCP, 0); +} + +static void +dyn_enqueue_keepalive_ipv6(struct mbufq *q, const struct dyn_ipv6_state *s) +{ + struct mbuf *m; + + if ((s->data->state & ACK_FWD) == 0 && s->data->ack_fwd > 0) { + m = dyn_mgethdr(sizeof(struct ip6_hdr) + + sizeof(struct tcphdr), s->data->fibnum); + if (m != NULL) { + dyn_make_keepalive_ipv6(m, &s->dst, &s->src, + s->zoneid, s->data->ack_fwd - 1, s->data->ack_rev, + s->dport, s->sport); + if (mbufq_enqueue(q, m)) { + m_freem(m); + log(LOG_DEBUG, "ipfw: limit for IPv6 keepalive" + " queue is reached.\n"); + return; + } + } + } + + if ((s->data->state & ACK_REV) == 0 && s->data->ack_rev > 0) { + m = dyn_mgethdr(sizeof(struct ip6_hdr) + + sizeof(struct tcphdr), s->data->fibnum); + if (m != NULL) { + dyn_make_keepalive_ipv6(m, &s->src, &s->dst, + s->zoneid, s->data->ack_rev - 1, s->data->ack_fwd, + s->sport, s->dport); + if (mbufq_enqueue(q, m)) { + m_freem(m); + log(LOG_DEBUG, "ipfw: limit for IPv6 keepalive" + " queue is reached.\n"); + return; + } + } + } +} + +static void +dyn_send_keepalive_ipv6(struct ip_fw_chain *chain) +{ + struct mbufq q; + struct mbuf *m; + struct dyn_ipv6_state *s; + uint32_t bucket; + + mbufq_init(&q, DYN_KEEPALIVE_MAXQ); + IPFW_UH_RLOCK(chain); + /* + * It is safe to not use hazard pointer and just do lockless + * access to the lists, because states entries can not be deleted + * while we hold IPFW_UH_RLOCK. + */ + for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) { + CK_SLIST_FOREACH(s, &V_dyn_ipv6[bucket], entry) { + /* + * Only established TCP connections that will + * become expired withing dyn_keepalive_interval. + */ + if (s->proto != IPPROTO_TCP || + (s->data->state & BOTH_SYN) != BOTH_SYN || + TIME_LEQ(time_uptime + V_dyn_keepalive_interval, + s->data->expire)) + continue; + dyn_enqueue_keepalive_ipv6(&q, s); + } + } + IPFW_UH_RUNLOCK(chain); + while ((m = mbufq_dequeue(&q)) != NULL) + ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); +} +#endif /* INET6 */ + +static void +dyn_grow_hashtable(struct ip_fw_chain *chain, uint32_t new) +{ +#ifdef INET6 + struct dyn_ipv6ck_slist *ipv6, *ipv6_parent; + uint32_t *ipv6_add, *ipv6_del, *ipv6_parent_add, *ipv6_parent_del; + struct dyn_ipv6_state *s6; +#endif + struct dyn_ipv4ck_slist *ipv4, *ipv4_parent; + uint32_t *ipv4_add, *ipv4_del, *ipv4_parent_add, *ipv4_parent_del; + struct dyn_ipv4_state *s4; + struct mtx *bucket_lock; + void *tmp; + uint32_t bucket; + + MPASS(powerof2(new)); + DYN_DEBUG("grow hash size %u -> %u", V_curr_dyn_buckets, new); + /* + * Allocate and initialize new lists. + * XXXAE: on memory pressure this can disable callout timer. + */ + bucket_lock = malloc(new * sizeof(struct mtx), M_IPFW, + M_WAITOK | M_ZERO); + ipv4 = malloc(new * sizeof(struct dyn_ipv4ck_slist), M_IPFW, + M_WAITOK | M_ZERO); + ipv4_parent = malloc(new * sizeof(struct dyn_ipv4ck_slist), M_IPFW, + M_WAITOK | M_ZERO); + ipv4_add = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO); + ipv4_del = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO); + ipv4_parent_add = malloc(new * sizeof(uint32_t), M_IPFW, + M_WAITOK | M_ZERO); + ipv4_parent_del = malloc(new * sizeof(uint32_t), M_IPFW, + M_WAITOK | M_ZERO); +#ifdef INET6 + ipv6 = malloc(new * sizeof(struct dyn_ipv6ck_slist), M_IPFW, + M_WAITOK | M_ZERO); + ipv6_parent = malloc(new * sizeof(struct dyn_ipv6ck_slist), M_IPFW, + M_WAITOK | M_ZERO); + ipv6_add = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO); + ipv6_del = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO); + ipv6_parent_add = malloc(new * sizeof(uint32_t), M_IPFW, + M_WAITOK | M_ZERO); + ipv6_parent_del = malloc(new * sizeof(uint32_t), M_IPFW, + M_WAITOK | M_ZERO); +#endif + for (bucket = 0; bucket < new; bucket++) { + DYN_BUCKET_LOCK_INIT(bucket_lock, bucket); +#if 0 + CK_SLIST_INIT(&ipv4[bucket]); + CK_SLIST_INIT(&ipv4_parent[bucket]); +#ifdef INET6 + CK_SLIST_INIT(&ipv6[bucket]); + CK_SLIST_INIT(&ipv6_parent[bucket]); +#endif +#endif + } + +#define DYN_RELINK_STATES(s, hval, i, head, ohead) do { \ + while ((s = CK_SLIST_FIRST(&V_dyn_ ## ohead[i])) != NULL) { \ + CK_SLIST_REMOVE_HEAD(&V_dyn_ ## ohead[i], entry); \ + CK_SLIST_INSERT_HEAD(&head[DYN_BUCKET(s->hval, new)], \ + s, entry); \ + } \ +} while (0) + /* + * Prevent rules changing from userland. + */ + IPFW_UH_WLOCK(chain); + /* + * Hold traffic processing until we finish resize to + * prevent access to states lists. + */ + IPFW_WLOCK(chain); + /* Re-link all dynamic states */ + for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) { + DYN_RELINK_STATES(s4, data->hashval, bucket, ipv4, ipv4); + DYN_RELINK_STATES(s4, limit->hashval, bucket, ipv4_parent, + ipv4_parent); +#ifdef INET6 + DYN_RELINK_STATES(s6, data->hashval, bucket, ipv6, ipv6); + DYN_RELINK_STATES(s6, limit->hashval, bucket, ipv6_parent, + ipv6_parent); +#endif + } + +#define DYN_SWAP_PTR(old, new, tmp) do { \ + tmp = old; \ + old = new; \ + new = tmp; \ +} while (0) + /* Swap pointers */ + DYN_SWAP_PTR(V_dyn_bucket_lock, bucket_lock, tmp); + DYN_SWAP_PTR(V_dyn_ipv4, ipv4, tmp); + DYN_SWAP_PTR(V_dyn_ipv4_parent, ipv4_parent, tmp); + DYN_SWAP_PTR(V_dyn_ipv4_add, ipv4_add, tmp); + DYN_SWAP_PTR(V_dyn_ipv4_parent_add, ipv4_parent_add, tmp); + DYN_SWAP_PTR(V_dyn_ipv4_del, ipv4_del, tmp); + DYN_SWAP_PTR(V_dyn_ipv4_parent_del, ipv4_parent_del, tmp); + +#ifdef INET6 + DYN_SWAP_PTR(V_dyn_ipv6, ipv6, tmp); + DYN_SWAP_PTR(V_dyn_ipv6_parent, ipv6_parent, tmp); + DYN_SWAP_PTR(V_dyn_ipv6_add, ipv6_add, tmp); + DYN_SWAP_PTR(V_dyn_ipv6_parent_add, ipv6_parent_add, tmp); + DYN_SWAP_PTR(V_dyn_ipv6_del, ipv6_del, tmp); + DYN_SWAP_PTR(V_dyn_ipv6_parent_del, ipv6_parent_del, tmp); +#endif + bucket = V_curr_dyn_buckets; + V_curr_dyn_buckets = new; + + IPFW_WUNLOCK(chain); + IPFW_UH_WUNLOCK(chain); + + /* Release old resources */ + while (bucket-- != 0) + DYN_BUCKET_LOCK_DESTROY(bucket_lock, bucket); + free(bucket_lock, M_IPFW); + free(ipv4, M_IPFW); + free(ipv4_parent, M_IPFW); + free(ipv4_add, M_IPFW); + free(ipv4_parent_add, M_IPFW); + free(ipv4_del, M_IPFW); + free(ipv4_parent_del, M_IPFW); +#ifdef INET6 + free(ipv6, M_IPFW); + free(ipv6_parent, M_IPFW); + free(ipv6_add, M_IPFW); + free(ipv6_parent_add, M_IPFW); + free(ipv6_del, M_IPFW); + free(ipv6_parent_del, M_IPFW); +#endif +} +/* + * This function is used to perform various maintenance + * on dynamic hash lists. Currently it is called every second. + */ +static void +dyn_tick(void *vnetx) +{ + uint32_t buckets; + + CURVNET_SET((struct vnet *)vnetx); + /* + * First free states unlinked in previous passes. + */ + dyn_free_states(&V_layer3_chain); + /* + * Now unlink others expired states. + * We use IPFW_UH_WLOCK to avoid concurrent call of + * dyn_expire_states(). It is the only function that does + * deletion of state entries from states lists. + */ + IPFW_UH_WLOCK(&V_layer3_chain); + dyn_expire_states(&V_layer3_chain, NULL); + IPFW_UH_WUNLOCK(&V_layer3_chain); + /* + * Send keepalives if they are enabled and the time has come. + */ + if (V_dyn_keepalive != 0 && + V_dyn_keepalive_last + V_dyn_keepalive_period <= time_uptime) { + V_dyn_keepalive_last = time_uptime; + dyn_send_keepalive_ipv4(&V_layer3_chain); +#ifdef INET6 + dyn_send_keepalive_ipv6(&V_layer3_chain); +#endif + } + /* + * Check if we need to resize the hash: + * if current number of states exceeds number of buckets in hash, + * and dyn_buckets_max permits to grow the number of buckets, then + * do it. Grow hash size to the minimum power of 2 which is bigger + * than current states count. + */ + if (V_curr_dyn_buckets < V_dyn_buckets_max && + V_curr_dyn_buckets < V_dyn_count / 2) { + buckets = 1 << fls(V_dyn_count); + if (buckets > V_dyn_buckets_max) + buckets = V_dyn_buckets_max; + dyn_grow_hashtable(&V_layer3_chain, buckets); + } + + callout_reset_on(&V_dyn_timeout, hz, dyn_tick, vnetx, 0); + CURVNET_RESTORE(); +} + +void +ipfw_expire_dyn_states(struct ip_fw_chain *chain, ipfw_range_tlv *rt) +{ + /* + * Do not perform any checks if we currently have no dynamic states + */ + if (V_dyn_count == 0) + return; + + IPFW_UH_WLOCK_ASSERT(chain); + dyn_expire_states(chain, rt); +} + +/* + * Returns size of dynamic states in legacy format + */ +int +ipfw_dyn_len(void) +{ + + return ((uma_zone_get_cur(V_dyn_parent_zone) + + uma_zone_get_cur(V_dyn_data_zone)) * sizeof(ipfw_dyn_rule)); +} + +/* + * Returns number of dynamic states. + * Used by dump format v1 (current). + */ +uint32_t +ipfw_dyn_get_count(void) +{ + + return (V_dyn_count); +} + +/* + * Check if rule contains at least one dynamic opcode. + * + * Returns 1 if such opcode is found, 0 otherwise. + */ +int +ipfw_is_dyn_rule(struct ip_fw *rule) +{ + int cmdlen, l; + ipfw_insn *cmd; + + l = rule->cmd_len; + cmd = rule->cmd; + cmdlen = 0; + for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) { + cmdlen = F_LEN(cmd); + + switch (cmd->opcode) { + case O_LIMIT: + case O_KEEP_STATE: + case O_PROBE_STATE: + case O_CHECK_STATE: + return (1); + } + } + + return (0); +} + +static void +dyn_export_parent(const struct dyn_parent *p, uint16_t kidx, + ipfw_dyn_rule *dst) +{ + struct ip_fw *rule; + uint16_t rulenum; + + dst->dyn_type = O_LIMIT_PARENT; + dst->kidx = kidx; + dst->count = DPARENT_COUNT(p); + dst->expire = TIME_LEQ(p->expire, time_uptime) ? 0: + p->expire - time_uptime; + + rule = p->parent; + /* 'rule' is used to pass up the rule number and set */ + rulenum = (uint16_t)rule->rulenum; + memcpy(&dst->rule, &rulenum, sizeof(rulenum)); + /* store set number into high word of dst->rule pointer. */ + memcpy((char *)&dst->rule + sizeof(rulenum), &rule->set, + sizeof(rule->set)); + + /* unused fields */ + dst->pcnt = 0; + dst->bcnt = 0; + dst->parent = NULL; + dst->state = 0; + dst->ack_fwd = 0; + dst->ack_rev = 0; + dst->bucket = p->hashval; + /* + * The legacy userland code will interpret a NULL here as a marker + * for the last dynamic rule. + */ + dst->next = (ipfw_dyn_rule *)1; +} + +static void +dyn_export_data(const struct dyn_data *data, uint16_t kidx, uint8_t type, + ipfw_dyn_rule *dst) +{ + struct dyn_parent *p; + struct ip_fw *rule; + uint16_t rulenum; + + dst->dyn_type = type; + dst->kidx = kidx; + dst->pcnt = data->pcnt_fwd + data->pcnt_rev; + dst->bcnt = data->bcnt_fwd + data->pcnt_rev; + dst->expire = TIME_LEQ(data->expire, time_uptime) ? 0: + data->expire - time_uptime; + + if (type == O_LIMIT) { + p = data->parent; + rule = p->parent; + } else + rule = data->parent; + + /* 'rule' is used to pass up the rule number and set */ + rulenum = (uint16_t)rule->rulenum; + memcpy(&dst->rule, &rulenum, sizeof(rulenum)); + /* store set number into high word of dst->rule pointer. */ + memcpy((char *)&dst->rule + sizeof(rulenum), &rule->set, + sizeof(rule->set)); + + /* unused fields */ + dst->parent = NULL; + dst->state = data->state; + dst->ack_fwd = data->ack_fwd; + dst->ack_rev = data->ack_rev; + dst->count = 0; + dst->bucket = data->hashval; + /* + * The legacy userland code will interpret a NULL here as a marker + * for the last dynamic rule. + */ + dst->next = (ipfw_dyn_rule *)1; +} + +static void +dyn_export_ipv4_state(const struct dyn_ipv4_state *s, ipfw_dyn_rule *dst) +{ + + if (s->type == O_LIMIT_PARENT) + dyn_export_parent(s->limit, s->kidx, dst); + else + dyn_export_data(s->data, s->kidx, s->type, dst); + + dst->id.dst_ip = s->src; + dst->id.src_ip = s->dst; + dst->id.dst_port = s->dport; + dst->id.src_port = s->sport; + dst->id.fib = s->data->fibnum; + dst->id.proto = s->proto; + dst->id._flags = 0; + dst->id.addr_type = 4; + + memset(&dst->id.dst_ip6, 0, sizeof(dst->id.dst_ip6)); + memset(&dst->id.src_ip6, 0, sizeof(dst->id.src_ip6)); + dst->id.flow_id6 = dst->id.extra = 0; +} + +#ifdef INET6 +static void +dyn_export_ipv6_state(const struct dyn_ipv6_state *s, ipfw_dyn_rule *dst) +{ + + if (s->type == O_LIMIT_PARENT) + dyn_export_parent(s->limit, s->kidx, dst); + else + dyn_export_data(s->data, s->kidx, s->type, dst); + + dst->id.src_ip6 = s->src; + dst->id.dst_ip6 = s->dst; + dst->id.dst_port = s->dport; + dst->id.src_port = s->sport; + dst->id.fib = s->data->fibnum; + dst->id.proto = s->proto; + dst->id._flags = 0; + dst->id.addr_type = 6; + + dst->id.dst_ip = dst->id.src_ip = 0; + dst->id.flow_id6 = dst->id.extra = 0; +} +#endif /* INET6 */ + +/* + * Fills the buffer given by @sd with dynamic states. + * Used by dump format v1 (current). + * + * Returns 0 on success. + */ +int +ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd) +{ +#ifdef INET6 + struct dyn_ipv6_state *s6; +#endif + struct dyn_ipv4_state *s4; + ipfw_obj_dyntlv *dst, *last; + ipfw_obj_ctlv *ctlv; + uint32_t bucket; + + if (V_dyn_count == 0) + return (0); + + /* + * IPFW_UH_RLOCK garantees that another userland request + * and callout thread will not delete entries from states + * lists. + */ + IPFW_UH_RLOCK_ASSERT(chain); + + ctlv = (ipfw_obj_ctlv *)ipfw_get_sopt_space(sd, sizeof(*ctlv)); + if (ctlv == NULL) + return (ENOMEM); + ctlv->head.type = IPFW_TLV_DYNSTATE_LIST; + ctlv->objsize = sizeof(ipfw_obj_dyntlv); + last = NULL; + +#define DYN_EXPORT_STATES(s, af, h, b) \ + CK_SLIST_FOREACH(s, &V_dyn_ ## h[b], entry) { \ + dst = (ipfw_obj_dyntlv *)ipfw_get_sopt_space(sd, \ + sizeof(ipfw_obj_dyntlv)); \ + if (dst == NULL) \ + return (ENOMEM); \ + dyn_export_ ## af ## _state(s, &dst->state); \ + dst->head.length = sizeof(ipfw_obj_dyntlv); \ + dst->head.type = IPFW_TLV_DYN_ENT; \ + last = dst; \ + } + + for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) { + DYN_EXPORT_STATES(s4, ipv4, ipv4_parent, bucket); + DYN_EXPORT_STATES(s4, ipv4, ipv4, bucket); +#ifdef INET6 + DYN_EXPORT_STATES(s6, ipv6, ipv6_parent, bucket); + DYN_EXPORT_STATES(s6, ipv6, ipv6, bucket); +#endif /* INET6 */ + } + + /* mark last dynamic rule */ + if (last != NULL) + last->head.flags = IPFW_DF_LAST; /* XXX: unused */ + return (0); +#undef DYN_EXPORT_STATES +} + +/* + * Fill given buffer with dynamic states (legacy format). + * IPFW_UH_RLOCK has to be held while calling. + */ +void +ipfw_get_dynamic(struct ip_fw_chain *chain, char **pbp, const char *ep) +{ +#ifdef INET6 + struct dyn_ipv6_state *s6; +#endif + struct dyn_ipv4_state *s4; + ipfw_dyn_rule *p, *last = NULL; + char *bp; + uint32_t bucket; + + if (V_dyn_count == 0) + return; + bp = *pbp; + + IPFW_UH_RLOCK_ASSERT(chain); + +#define DYN_EXPORT_STATES(s, af, head, b) \ + CK_SLIST_FOREACH(s, &V_dyn_ ## head[b], entry) { \ + if (bp + sizeof(*p) > ep) \ + break; \ + p = (ipfw_dyn_rule *)bp; \ + dyn_export_ ## af ## _state(s, p); \ + last = p; \ + bp += sizeof(*p); \ + } + + for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) { + DYN_EXPORT_STATES(s4, ipv4, ipv4_parent, bucket); + DYN_EXPORT_STATES(s4, ipv4, ipv4, bucket); +#ifdef INET6 + DYN_EXPORT_STATES(s6, ipv6, ipv6_parent, bucket); + DYN_EXPORT_STATES(s6, ipv6, ipv6, bucket); +#endif /* INET6 */ + } + + if (last != NULL) /* mark last dynamic rule */ + last->next = NULL; + *pbp = bp; +#undef DYN_EXPORT_STATES +} + +void +ipfw_dyn_init(struct ip_fw_chain *chain) +{ + + V_dyn_max = 16384; /* max # of states */ + V_dyn_parent_max = 4096; /* max # of parent states */ + V_dyn_buckets_max = 8192; /* must be power of 2 */ + + V_dyn_ack_lifetime = 300; + V_dyn_syn_lifetime = 20; + V_dyn_fin_lifetime = 1; + V_dyn_rst_lifetime = 1; + V_dyn_udp_lifetime = 10; + V_dyn_short_lifetime = 5; + V_dyn_sync_lifetime = 5; + + V_dyn_keepalive_interval = 20; + V_dyn_keepalive_period = 5; + V_dyn_keepalive = 1; /* send keepalives */ + V_dyn_keepalive_last = time_uptime; + + V_dyn_data_zone = uma_zcreate("IPFW dynamic states data", + sizeof(struct dyn_data), NULL, NULL, NULL, NULL, + UMA_ALIGN_PTR, 0); + uma_zone_set_max(V_dyn_data_zone, V_dyn_max); + + V_dyn_parent_zone = uma_zcreate("IPFW parent dynamic states", + sizeof(struct dyn_parent), NULL, NULL, NULL, NULL, + UMA_ALIGN_PTR, 0); + uma_zone_set_max(V_dyn_parent_zone, V_dyn_parent_max); + + SLIST_INIT(&dyn_expired_ipv4); + V_dyn_ipv4 = NULL; + V_dyn_ipv4_parent = NULL; + V_dyn_ipv4_zone = uma_zcreate("IPFW IPv4 dynamic states", + sizeof(struct dyn_ipv4_state), NULL, NULL, NULL, NULL, + UMA_ALIGN_PTR, 0); + +#ifdef INET6 + SLIST_INIT(&dyn_expired_ipv6); + V_dyn_ipv6 = NULL; + V_dyn_ipv6_parent = NULL; + V_dyn_ipv6_zone = uma_zcreate("IPFW IPv6 dynamic states", + sizeof(struct dyn_ipv6_state), NULL, NULL, NULL, NULL, + UMA_ALIGN_PTR, 0); +#endif + + /* Initialize buckets. */ + V_curr_dyn_buckets = 0; + V_dyn_bucket_lock = NULL; + dyn_grow_hashtable(chain, 256); + + if (IS_DEFAULT_VNET(curvnet)) + dyn_hp_cache = malloc(mp_ncpus * sizeof(void *), M_IPFW, + M_WAITOK | M_ZERO); + + DYN_EXPIRED_LOCK_INIT(); + callout_init(&V_dyn_timeout, 1); + callout_reset(&V_dyn_timeout, hz, dyn_tick, curvnet); + IPFW_ADD_OBJ_REWRITER(IS_DEFAULT_VNET(curvnet), dyn_opcodes); +} + +void +ipfw_dyn_uninit(int pass) +{ +#ifdef INET6 + struct dyn_ipv6_state *s6; +#endif + struct dyn_ipv4_state *s4; + int bucket; + + if (pass == 0) { + callout_drain(&V_dyn_timeout); + return; + } + IPFW_DEL_OBJ_REWRITER(IS_DEFAULT_VNET(curvnet), dyn_opcodes); + DYN_EXPIRED_LOCK_DESTROY(); + +#define DYN_FREE_STATES_FORCED(CK, s, af, name, en) do { \ + while ((s = CK ## SLIST_FIRST(&V_dyn_ ## name)) != NULL) { \ + CK ## SLIST_REMOVE_HEAD(&V_dyn_ ## name, en); \ + if (s->type == O_LIMIT_PARENT) \ + uma_zfree(V_dyn_parent_zone, s->limit); \ + else \ + uma_zfree(V_dyn_data_zone, s->data); \ + uma_zfree(V_dyn_ ## af ## _zone, s); \ + } \ +} while (0) + for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) { + DYN_BUCKET_LOCK_DESTROY(V_dyn_bucket_lock, bucket); + + DYN_FREE_STATES_FORCED(CK_, s4, ipv4, ipv4[bucket], entry); + DYN_FREE_STATES_FORCED(CK_, s4, ipv4, ipv4_parent[bucket], + entry); +#ifdef INET6 + DYN_FREE_STATES_FORCED(CK_, s6, ipv6, ipv6[bucket], entry); + DYN_FREE_STATES_FORCED(CK_, s6, ipv6, ipv6_parent[bucket], + entry); +#endif /* INET6 */ + } + DYN_FREE_STATES_FORCED(, s4, ipv4, expired_ipv4, expired); +#ifdef INET6 + DYN_FREE_STATES_FORCED(, s6, ipv6, expired_ipv6, expired); +#endif +#undef DYN_FREE_STATES_FORCED + + uma_zdestroy(V_dyn_ipv4_zone); + uma_zdestroy(V_dyn_data_zone); + uma_zdestroy(V_dyn_parent_zone); +#ifdef INET6 + uma_zdestroy(V_dyn_ipv6_zone); + free(V_dyn_ipv6, M_IPFW); + free(V_dyn_ipv6_parent, M_IPFW); + free(V_dyn_ipv6_add, M_IPFW); + free(V_dyn_ipv6_parent_add, M_IPFW); + free(V_dyn_ipv6_del, M_IPFW); + free(V_dyn_ipv6_parent_del, M_IPFW); +#endif + free(V_dyn_bucket_lock, M_IPFW); + free(V_dyn_ipv4, M_IPFW); + free(V_dyn_ipv4_parent, M_IPFW); + free(V_dyn_ipv4_add, M_IPFW); + free(V_dyn_ipv4_parent_add, M_IPFW); + free(V_dyn_ipv4_del, M_IPFW); + free(V_dyn_ipv4_parent_del, M_IPFW); + if (IS_DEFAULT_VNET(curvnet)) + free(dyn_hp_cache, M_IPFW); +} + + diff --git a/sys/netpfil/ipfw/ip_fw_private.h b/sys/netpfil/ipfw/ip_fw_private.h index b6471a0235d..9b0a175c2b9 100644 --- a/sys/netpfil/ipfw/ip_fw_private.h +++ b/sys/netpfil/ipfw/ip_fw_private.h @@ -182,24 +182,47 @@ enum { /* result for matching dynamic rules */ struct ip_fw_chain; struct sockopt_data; int ipfw_is_dyn_rule(struct ip_fw *rule); -void ipfw_expire_dyn_rules(struct ip_fw_chain *, ipfw_range_tlv *); -void ipfw_dyn_unlock(ipfw_dyn_rule *q); +void ipfw_expire_dyn_states(struct ip_fw_chain *, ipfw_range_tlv *); -struct tcphdr; struct mbuf *ipfw_send_pkt(struct mbuf *, struct ipfw_flow_id *, u_int32_t, u_int32_t, int); -int ipfw_install_state(struct ip_fw_chain *chain, struct ip_fw *rule, - ipfw_insn_limit *cmd, struct ip_fw_args *args, uint32_t tablearg); -ipfw_dyn_rule *ipfw_lookup_dyn_rule(struct ipfw_flow_id *pkt, - int *match_direction, struct tcphdr *tcp, uint16_t kidx); -void ipfw_remove_dyn_children(struct ip_fw *rule); +/* + * Macro to determine that we need to do or redo dynamic state lookup. + * direction == MATCH_UNKNOWN means that this is first lookup, then we need + * to do lookup. + * Otherwise check the state name, if previous lookup was for "any" name, + * this means there is no state with specific name. Thus no need to do + * lookup. If previous name was not "any", redo lookup for specific name. + */ +#define DYN_LOOKUP_NEEDED(p, cmd) \ + ((p)->direction == MATCH_UNKNOWN || \ + ((p)->kidx != 0 && (p)->kidx != (cmd)->arg1)) +#define DYN_INFO_INIT(p) do { \ + (p)->direction = MATCH_UNKNOWN; \ + (p)->kidx = 0; \ +} while (0) +struct ipfw_dyn_info { + uint16_t direction; /* match direction */ + uint16_t kidx; /* state name kidx */ + uint32_t hashval; /* hash value */ + uint32_t version; /* bucket version */ + uint32_t f_pos; +}; +int ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule, + const ipfw_insn_limit *cmd, const struct ip_fw_args *args, + const void *ulp, int pktlen, struct ipfw_dyn_info *info, + uint32_t tablearg); +struct ip_fw *ipfw_dyn_lookup_state(const struct ip_fw_args *args, + const void *ulp, int pktlen, const ipfw_insn *cmd, + struct ipfw_dyn_info *info); + void ipfw_get_dynamic(struct ip_fw_chain *chain, char **bp, const char *ep); int ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd); void ipfw_dyn_init(struct ip_fw_chain *); /* per-vnet initialization */ void ipfw_dyn_uninit(int); /* per-vnet deinitialization */ int ipfw_dyn_len(void); -int ipfw_dyn_get_count(void); +uint32_t ipfw_dyn_get_count(void); /* common variables */ VNET_DECLARE(int, fw_one_pass); diff --git a/sys/netpfil/ipfw/ip_fw_sockopt.c b/sys/netpfil/ipfw/ip_fw_sockopt.c index e6d74875a4d..678d9e5ad75 100644 --- a/sys/netpfil/ipfw/ip_fw_sockopt.c +++ b/sys/netpfil/ipfw/ip_fw_sockopt.c @@ -1037,7 +1037,7 @@ delete_range(struct ip_fw_chain *chain, ipfw_range_tlv *rt, int *ndel) map = swap_map(chain, map, chain->n_rules - n); /* 6. Remove all dynamic states originated by deleted rules */ if (ndyn > 0) - ipfw_expire_dyn_rules(chain, rt); + ipfw_expire_dyn_states(chain, rt); /* 7. now remove the rules deleted from the old map */ for (i = start; i < end; i++) { rule = map[i];