2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017-2018 Yandex LLC
5 * Copyright (c) 2017-2018 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
37 #error IPFIREWALL requires INET.
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/kernel.h>
47 #include <sys/queue.h>
48 #include <sys/rmlock.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 #include <sys/syslog.h>
53 #include <net/ethernet.h>
55 #include <net/if_var.h>
59 #include <netinet/in.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_fw.h>
63 #include <netinet/tcp_var.h>
64 #include <netinet/udp.h>
66 #include <netinet/ip6.h> /* IN6_ARE_ADDR_EQUAL */
68 #include <netinet6/in6_var.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/scope6_var.h>
73 #include <netpfil/ipfw/ip_fw_private.h>
75 #include <machine/in_cksum.h> /* XXX for in_cksum */
78 #include <security/mac/mac_framework.h>
82 * Description of dynamic states.
84 * Dynamic states are stored in lists accessed through a hash tables
85 * whose size is curr_dyn_buckets. This value can be modified through
86 * the sysctl variable dyn_buckets.
88 * Currently there are four tables: dyn_ipv4, dyn_ipv6, dyn_ipv4_parent,
89 * and dyn_ipv6_parent.
91 * When a packet is received, its address fields hashed, then matched
92 * against the entries in the corresponding list by addr_type.
93 * Dynamic states can be used for different purposes:
95 * + enforcing limits on the number of sessions;
96 * + in-kernel NAT (not implemented yet)
98 * The lifetime of dynamic states is regulated by dyn_*_lifetime,
99 * measured in seconds and depending on the flags.
101 * The total number of dynamic states is equal to UMA zone items count.
102 * The max number of dynamic states is dyn_max. When we reach
103 * the maximum number of rules we do not create anymore. This is
104 * done to avoid consuming too much memory, but also too much
105 * time when searching on each packet (ideally, we should try instead
106 * to put a limit on the length of the list on each bucket...).
108 * Each state holds a pointer to the parent ipfw rule so we know what
109 * action to perform. Dynamic rules are removed when the parent rule is
112 * There are some limitations with dynamic rules -- we do not
113 * obey the 'randomized match', and we do not do multiple
114 * passes through the firewall. XXX check the latter!!!
117 /* By default use jenkins hash function */
118 #define IPFIREWALL_JENKINSHASH
120 #define DYN_COUNTER_INC(d, dir, pktlen) do { \
121 (d)->pcnt_ ## dir++; \
122 (d)->bcnt_ ## dir += pktlen; \
125 #define DYN_REFERENCED 0x01
127 * DYN_REFERENCED flag is used to show that state keeps reference to named
128 * object, and this reference should be released when state becomes expired.
132 void *parent; /* pointer to parent rule */
133 uint32_t chain_id; /* cached ruleset id */
134 uint32_t f_pos; /* cached rule index */
136 uint32_t hashval; /* hash value used for hash resize */
137 uint16_t fibnum; /* fib used to send keepalives */
139 uint8_t flags; /* internal flags */
140 uint8_t set; /* parent rule set number */
141 uint16_t rulenum; /* parent rule number */
142 uint32_t ruleid; /* parent rule id */
144 uint32_t state; /* TCP session state and flags */
145 uint32_t ack_fwd; /* most recent ACKs in forward */
146 uint32_t ack_rev; /* and reverse direction (used */
147 /* to generate keepalives) */
148 uint32_t sync; /* synchronization time */
149 uint32_t expire; /* expire time */
151 uint64_t pcnt_fwd; /* bytes counter in forward */
152 uint64_t bcnt_fwd; /* packets counter in forward */
153 uint64_t pcnt_rev; /* bytes counter in reverse */
154 uint64_t bcnt_rev; /* packets counter in reverse */
157 #define DPARENT_COUNT_DEC(p) do { \
158 MPASS(p->count > 0); \
159 ck_pr_dec_32(&(p)->count); \
161 #define DPARENT_COUNT_INC(p) ck_pr_inc_32(&(p)->count)
162 #define DPARENT_COUNT(p) ck_pr_load_32(&(p)->count)
164 void *parent; /* pointer to parent rule */
165 uint32_t count; /* number of linked states */
167 uint8_t set; /* parent rule set number */
168 uint16_t rulenum; /* parent rule number */
169 uint32_t ruleid; /* parent rule id */
170 uint32_t hashval; /* hash value used for hash resize */
171 uint32_t expire; /* expire time */
174 struct dyn_ipv4_state {
175 uint8_t type; /* State type */
176 uint8_t proto; /* UL Protocol */
177 uint16_t kidx; /* named object index */
178 uint16_t sport, dport; /* ULP source and destination ports */
179 in_addr_t src, dst; /* IPv4 source and destination */
182 struct dyn_data *data;
183 struct dyn_parent *limit;
185 CK_SLIST_ENTRY(dyn_ipv4_state) entry;
186 SLIST_ENTRY(dyn_ipv4_state) expired;
188 CK_SLIST_HEAD(dyn_ipv4ck_slist, dyn_ipv4_state);
189 VNET_DEFINE_STATIC(struct dyn_ipv4ck_slist *, dyn_ipv4);
190 VNET_DEFINE_STATIC(struct dyn_ipv4ck_slist *, dyn_ipv4_parent);
192 SLIST_HEAD(dyn_ipv4_slist, dyn_ipv4_state);
193 VNET_DEFINE_STATIC(struct dyn_ipv4_slist, dyn_expired_ipv4);
194 #define V_dyn_ipv4 VNET(dyn_ipv4)
195 #define V_dyn_ipv4_parent VNET(dyn_ipv4_parent)
196 #define V_dyn_expired_ipv4 VNET(dyn_expired_ipv4)
199 struct dyn_ipv6_state {
200 uint8_t type; /* State type */
201 uint8_t proto; /* UL Protocol */
202 uint16_t kidx; /* named object index */
203 uint16_t sport, dport; /* ULP source and destination ports */
204 struct in6_addr src, dst; /* IPv6 source and destination */
205 uint32_t zoneid; /* IPv6 scope zone id */
207 struct dyn_data *data;
208 struct dyn_parent *limit;
210 CK_SLIST_ENTRY(dyn_ipv6_state) entry;
211 SLIST_ENTRY(dyn_ipv6_state) expired;
213 CK_SLIST_HEAD(dyn_ipv6ck_slist, dyn_ipv6_state);
214 VNET_DEFINE_STATIC(struct dyn_ipv6ck_slist *, dyn_ipv6);
215 VNET_DEFINE_STATIC(struct dyn_ipv6ck_slist *, dyn_ipv6_parent);
217 SLIST_HEAD(dyn_ipv6_slist, dyn_ipv6_state);
218 VNET_DEFINE_STATIC(struct dyn_ipv6_slist, dyn_expired_ipv6);
219 #define V_dyn_ipv6 VNET(dyn_ipv6)
220 #define V_dyn_ipv6_parent VNET(dyn_ipv6_parent)
221 #define V_dyn_expired_ipv6 VNET(dyn_expired_ipv6)
225 * Per-CPU pointer indicates that specified state is currently in use
226 * and must not be reclaimed by expiration callout.
228 static void **dyn_hp_cache;
229 DPCPU_DEFINE_STATIC(void *, dyn_hp);
230 #define DYNSTATE_GET(cpu) ck_pr_load_ptr(DPCPU_ID_PTR((cpu), dyn_hp))
231 #define DYNSTATE_PROTECT(v) ck_pr_store_ptr(DPCPU_PTR(dyn_hp), (v))
232 #define DYNSTATE_RELEASE() DYNSTATE_PROTECT(NULL)
233 #define DYNSTATE_CRITICAL_ENTER() critical_enter()
234 #define DYNSTATE_CRITICAL_EXIT() do { \
235 DYNSTATE_RELEASE(); \
240 * We keep two version numbers, one is updated when new entry added to
241 * the list. Second is updated when an entry deleted from the list.
242 * Versions are updated under bucket lock.
244 * Bucket "add" version number is used to know, that in the time between
245 * state lookup (i.e. ipfw_dyn_lookup_state()) and the followed state
246 * creation (i.e. ipfw_dyn_install_state()) another concurrent thread did
247 * not install some state in this bucket. Using this info we can avoid
248 * additional state lookup, because we are sure that we will not install
251 * Also doing the tracking of bucket "del" version during lookup we can
252 * be sure, that state entry was not unlinked and freed in time between
253 * we read the state pointer and protect it with hazard pointer.
255 * An entry unlinked from CK list keeps unchanged until it is freed.
256 * Unlinked entries are linked into expired lists using "expired" field.
260 * dyn_expire_lock is used to protect access to dyn_expired_xxx lists.
261 * dyn_bucket_lock is used to get write access to lists in specific bucket.
262 * Currently one dyn_bucket_lock is used for all ipv4, ipv4_parent, ipv6,
263 * and ipv6_parent lists.
265 VNET_DEFINE_STATIC(struct mtx, dyn_expire_lock);
266 VNET_DEFINE_STATIC(struct mtx *, dyn_bucket_lock);
267 #define V_dyn_expire_lock VNET(dyn_expire_lock)
268 #define V_dyn_bucket_lock VNET(dyn_bucket_lock)
271 * Bucket's add/delete generation versions.
273 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv4_add);
274 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv4_del);
275 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv4_parent_add);
276 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv4_parent_del);
277 #define V_dyn_ipv4_add VNET(dyn_ipv4_add)
278 #define V_dyn_ipv4_del VNET(dyn_ipv4_del)
279 #define V_dyn_ipv4_parent_add VNET(dyn_ipv4_parent_add)
280 #define V_dyn_ipv4_parent_del VNET(dyn_ipv4_parent_del)
283 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv6_add);
284 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv6_del);
285 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv6_parent_add);
286 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv6_parent_del);
287 #define V_dyn_ipv6_add VNET(dyn_ipv6_add)
288 #define V_dyn_ipv6_del VNET(dyn_ipv6_del)
289 #define V_dyn_ipv6_parent_add VNET(dyn_ipv6_parent_add)
290 #define V_dyn_ipv6_parent_del VNET(dyn_ipv6_parent_del)
293 #define DYN_BUCKET(h, b) ((h) & (b - 1))
294 #define DYN_BUCKET_VERSION(b, v) ck_pr_load_32(&V_dyn_ ## v[(b)])
295 #define DYN_BUCKET_VERSION_BUMP(b, v) ck_pr_inc_32(&V_dyn_ ## v[(b)])
297 #define DYN_BUCKET_LOCK_INIT(lock, b) \
298 mtx_init(&lock[(b)], "IPFW dynamic bucket", NULL, MTX_DEF)
299 #define DYN_BUCKET_LOCK_DESTROY(lock, b) mtx_destroy(&lock[(b)])
300 #define DYN_BUCKET_LOCK(b) mtx_lock(&V_dyn_bucket_lock[(b)])
301 #define DYN_BUCKET_UNLOCK(b) mtx_unlock(&V_dyn_bucket_lock[(b)])
302 #define DYN_BUCKET_ASSERT(b) mtx_assert(&V_dyn_bucket_lock[(b)], MA_OWNED)
304 #define DYN_EXPIRED_LOCK_INIT() \
305 mtx_init(&V_dyn_expire_lock, "IPFW expired states list", NULL, MTX_DEF)
306 #define DYN_EXPIRED_LOCK_DESTROY() mtx_destroy(&V_dyn_expire_lock)
307 #define DYN_EXPIRED_LOCK() mtx_lock(&V_dyn_expire_lock)
308 #define DYN_EXPIRED_UNLOCK() mtx_unlock(&V_dyn_expire_lock)
310 VNET_DEFINE_STATIC(uint32_t, dyn_buckets_max);
311 VNET_DEFINE_STATIC(uint32_t, curr_dyn_buckets);
312 VNET_DEFINE_STATIC(struct callout, dyn_timeout);
313 #define V_dyn_buckets_max VNET(dyn_buckets_max)
314 #define V_curr_dyn_buckets VNET(curr_dyn_buckets)
315 #define V_dyn_timeout VNET(dyn_timeout)
317 /* Maximum length of states chain in a bucket */
318 VNET_DEFINE_STATIC(uint32_t, curr_max_length);
319 #define V_curr_max_length VNET(curr_max_length)
321 VNET_DEFINE_STATIC(uint32_t, dyn_keep_states);
322 #define V_dyn_keep_states VNET(dyn_keep_states)
324 VNET_DEFINE_STATIC(uma_zone_t, dyn_data_zone);
325 VNET_DEFINE_STATIC(uma_zone_t, dyn_parent_zone);
326 VNET_DEFINE_STATIC(uma_zone_t, dyn_ipv4_zone);
328 VNET_DEFINE_STATIC(uma_zone_t, dyn_ipv6_zone);
329 #define V_dyn_ipv6_zone VNET(dyn_ipv6_zone)
331 #define V_dyn_data_zone VNET(dyn_data_zone)
332 #define V_dyn_parent_zone VNET(dyn_parent_zone)
333 #define V_dyn_ipv4_zone VNET(dyn_ipv4_zone)
336 * Timeouts for various events in handing dynamic rules.
338 VNET_DEFINE_STATIC(uint32_t, dyn_ack_lifetime);
339 VNET_DEFINE_STATIC(uint32_t, dyn_syn_lifetime);
340 VNET_DEFINE_STATIC(uint32_t, dyn_fin_lifetime);
341 VNET_DEFINE_STATIC(uint32_t, dyn_rst_lifetime);
342 VNET_DEFINE_STATIC(uint32_t, dyn_udp_lifetime);
343 VNET_DEFINE_STATIC(uint32_t, dyn_short_lifetime);
345 #define V_dyn_ack_lifetime VNET(dyn_ack_lifetime)
346 #define V_dyn_syn_lifetime VNET(dyn_syn_lifetime)
347 #define V_dyn_fin_lifetime VNET(dyn_fin_lifetime)
348 #define V_dyn_rst_lifetime VNET(dyn_rst_lifetime)
349 #define V_dyn_udp_lifetime VNET(dyn_udp_lifetime)
350 #define V_dyn_short_lifetime VNET(dyn_short_lifetime)
353 * Keepalives are sent if dyn_keepalive is set. They are sent every
354 * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
355 * seconds of lifetime of a rule.
356 * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
357 * than dyn_keepalive_period.
359 VNET_DEFINE_STATIC(uint32_t, dyn_keepalive_interval);
360 VNET_DEFINE_STATIC(uint32_t, dyn_keepalive_period);
361 VNET_DEFINE_STATIC(uint32_t, dyn_keepalive);
362 VNET_DEFINE_STATIC(time_t, dyn_keepalive_last);
364 #define V_dyn_keepalive_interval VNET(dyn_keepalive_interval)
365 #define V_dyn_keepalive_period VNET(dyn_keepalive_period)
366 #define V_dyn_keepalive VNET(dyn_keepalive)
367 #define V_dyn_keepalive_last VNET(dyn_keepalive_last)
369 VNET_DEFINE_STATIC(uint32_t, dyn_max); /* max # of dynamic states */
370 VNET_DEFINE_STATIC(uint32_t, dyn_count); /* number of states */
371 VNET_DEFINE_STATIC(uint32_t, dyn_parent_max); /* max # of parent states */
372 VNET_DEFINE_STATIC(uint32_t, dyn_parent_count); /* number of parent states */
374 #define V_dyn_max VNET(dyn_max)
375 #define V_dyn_count VNET(dyn_count)
376 #define V_dyn_parent_max VNET(dyn_parent_max)
377 #define V_dyn_parent_count VNET(dyn_parent_count)
379 #define DYN_COUNT_DEC(name) do { \
380 MPASS((V_ ## name) > 0); \
381 ck_pr_dec_32(&(V_ ## name)); \
383 #define DYN_COUNT_INC(name) ck_pr_inc_32(&(V_ ## name))
384 #define DYN_COUNT(name) ck_pr_load_32(&(V_ ## name))
386 static time_t last_log; /* Log ratelimiting */
389 * Get/set maximum number of dynamic states in given VNET instance.
392 sysctl_dyn_max(SYSCTL_HANDLER_ARGS)
398 error = sysctl_handle_32(oidp, &nstates, 0, req);
399 /* Read operation or some error */
400 if ((error != 0) || (req->newptr == NULL))
404 uma_zone_set_max(V_dyn_data_zone, V_dyn_max);
409 sysctl_dyn_parent_max(SYSCTL_HANDLER_ARGS)
414 nstates = V_dyn_parent_max;
415 error = sysctl_handle_32(oidp, &nstates, 0, req);
416 /* Read operation or some error */
417 if ((error != 0) || (req->newptr == NULL))
420 V_dyn_parent_max = nstates;
421 uma_zone_set_max(V_dyn_parent_zone, V_dyn_parent_max);
426 sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS)
431 nbuckets = V_dyn_buckets_max;
432 error = sysctl_handle_32(oidp, &nbuckets, 0, req);
433 /* Read operation or some error */
434 if ((error != 0) || (req->newptr == NULL))
438 V_dyn_buckets_max = 1 << fls(nbuckets - 1);
444 SYSCTL_DECL(_net_inet_ip_fw);
446 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_count,
447 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(dyn_count), 0,
448 "Current number of dynamic states.");
449 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_parent_count,
450 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(dyn_parent_count), 0,
451 "Current number of parent states. ");
452 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets,
453 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(curr_dyn_buckets), 0,
454 "Current number of buckets for states hash table.");
455 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, curr_max_length,
456 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(curr_max_length), 0,
457 "Current maximum length of states chains in hash buckets.");
458 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_buckets,
459 CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RW, 0, 0, sysctl_dyn_buckets,
460 "IU", "Max number of buckets for dynamic states hash table.");
461 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_max,
462 CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RW, 0, 0, sysctl_dyn_max,
463 "IU", "Max number of dynamic states.");
464 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_parent_max,
465 CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RW, 0, 0, sysctl_dyn_parent_max,
466 "IU", "Max number of parent dynamic states.");
467 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime,
468 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_ack_lifetime), 0,
469 "Lifetime of dynamic states for TCP ACK.");
470 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime,
471 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_syn_lifetime), 0,
472 "Lifetime of dynamic states for TCP SYN.");
473 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime,
474 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_fin_lifetime), 0,
475 "Lifetime of dynamic states for TCP FIN.");
476 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime,
477 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_rst_lifetime), 0,
478 "Lifetime of dynamic states for TCP RST.");
479 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime,
480 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_udp_lifetime), 0,
481 "Lifetime of dynamic states for UDP.");
482 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime,
483 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_short_lifetime), 0,
484 "Lifetime of dynamic states for other situations.");
485 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_keepalive,
486 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_keepalive), 0,
487 "Enable keepalives for dynamic states.");
488 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_keep_states,
489 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_keep_states), 0,
490 "Do not flush dynamic states on rule deletion");
493 #ifdef IPFIREWALL_DYNDEBUG
494 #define DYN_DEBUG(fmt, ...) do { \
495 printf("%s: " fmt "\n", __func__, __VA_ARGS__); \
498 #define DYN_DEBUG(fmt, ...)
499 #endif /* !IPFIREWALL_DYNDEBUG */
502 /* Functions to work with IPv6 states */
503 static struct dyn_ipv6_state *dyn_lookup_ipv6_state(
504 const struct ipfw_flow_id *, uint32_t, const void *,
505 struct ipfw_dyn_info *, int);
506 static int dyn_lookup_ipv6_state_locked(const struct ipfw_flow_id *,
507 uint32_t, const void *, int, uint32_t, uint16_t);
508 static struct dyn_ipv6_state *dyn_alloc_ipv6_state(
509 const struct ipfw_flow_id *, uint32_t, uint16_t, uint8_t);
510 static int dyn_add_ipv6_state(void *, uint32_t, uint16_t, uint8_t,
511 const struct ipfw_flow_id *, uint32_t, const void *, int, uint32_t,
512 struct ipfw_dyn_info *, uint16_t, uint16_t, uint8_t);
513 static void dyn_export_ipv6_state(const struct dyn_ipv6_state *,
516 static uint32_t dyn_getscopeid(const struct ip_fw_args *);
517 static void dyn_make_keepalive_ipv6(struct mbuf *, const struct in6_addr *,
518 const struct in6_addr *, uint32_t, uint32_t, uint32_t, uint16_t,
520 static void dyn_enqueue_keepalive_ipv6(struct mbufq *,
521 const struct dyn_ipv6_state *);
522 static void dyn_send_keepalive_ipv6(struct ip_fw_chain *);
524 static struct dyn_ipv6_state *dyn_lookup_ipv6_parent(
525 const struct ipfw_flow_id *, uint32_t, const void *, uint32_t, uint16_t,
527 static struct dyn_ipv6_state *dyn_lookup_ipv6_parent_locked(
528 const struct ipfw_flow_id *, uint32_t, const void *, uint32_t, uint16_t,
530 static struct dyn_ipv6_state *dyn_add_ipv6_parent(void *, uint32_t, uint16_t,
531 uint8_t, const struct ipfw_flow_id *, uint32_t, uint32_t, uint32_t,
535 /* Functions to work with limit states */
536 static void *dyn_get_parent_state(const struct ipfw_flow_id *, uint32_t,
537 struct ip_fw *, uint32_t, uint32_t, uint16_t);
538 static struct dyn_ipv4_state *dyn_lookup_ipv4_parent(
539 const struct ipfw_flow_id *, const void *, uint32_t, uint16_t, uint32_t);
540 static struct dyn_ipv4_state *dyn_lookup_ipv4_parent_locked(
541 const struct ipfw_flow_id *, const void *, uint32_t, uint16_t, uint32_t);
542 static struct dyn_parent *dyn_alloc_parent(void *, uint32_t, uint16_t,
544 static struct dyn_ipv4_state *dyn_add_ipv4_parent(void *, uint32_t, uint16_t,
545 uint8_t, const struct ipfw_flow_id *, uint32_t, uint32_t, uint16_t);
547 static void dyn_tick(void *);
548 static void dyn_expire_states(struct ip_fw_chain *, ipfw_range_tlv *);
549 static void dyn_free_states(struct ip_fw_chain *);
550 static void dyn_export_parent(const struct dyn_parent *, uint16_t,
552 static void dyn_export_data(const struct dyn_data *, uint16_t, uint8_t,
554 static uint32_t dyn_update_tcp_state(struct dyn_data *,
555 const struct ipfw_flow_id *, const struct tcphdr *, int);
556 static void dyn_update_proto_state(struct dyn_data *,
557 const struct ipfw_flow_id *, const void *, int, int);
559 /* Functions to work with IPv4 states */
560 struct dyn_ipv4_state *dyn_lookup_ipv4_state(const struct ipfw_flow_id *,
561 const void *, struct ipfw_dyn_info *, int);
562 static int dyn_lookup_ipv4_state_locked(const struct ipfw_flow_id *,
563 const void *, int, uint32_t, uint16_t);
564 static struct dyn_ipv4_state *dyn_alloc_ipv4_state(
565 const struct ipfw_flow_id *, uint16_t, uint8_t);
566 static int dyn_add_ipv4_state(void *, uint32_t, uint16_t, uint8_t,
567 const struct ipfw_flow_id *, const void *, int, uint32_t,
568 struct ipfw_dyn_info *, uint16_t, uint16_t, uint8_t);
569 static void dyn_export_ipv4_state(const struct dyn_ipv4_state *,
573 * Named states support.
575 static char *default_state_name = "default";
576 struct dyn_state_obj {
577 struct named_object no;
581 #define DYN_STATE_OBJ(ch, cmd) \
582 ((struct dyn_state_obj *)SRV_OBJECT(ch, (cmd)->arg1))
584 * Classifier callback.
585 * Return 0 if opcode contains object that should be referenced
589 dyn_classify(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
592 DYN_DEBUG("opcode %d, arg1 %d", cmd->opcode, cmd->arg1);
593 /* Don't rewrite "check-state any" */
594 if (cmd->arg1 == 0 &&
595 cmd->opcode == O_CHECK_STATE)
604 dyn_update(ipfw_insn *cmd, uint16_t idx)
608 DYN_DEBUG("opcode %d, arg1 %d", cmd->opcode, cmd->arg1);
612 dyn_findbyname(struct ip_fw_chain *ch, struct tid_info *ti,
613 struct named_object **pno)
618 DYN_DEBUG("uidx %d", ti->uidx);
620 if (ti->tlvs == NULL)
622 /* Search ntlv in the buffer provided by user */
623 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
624 IPFW_TLV_STATE_NAME);
629 name = default_state_name;
631 * Search named object with corresponding name.
632 * Since states objects are global - ignore the set value
633 * and use zero instead.
635 *pno = ipfw_objhash_lookup_name_type(CHAIN_TO_SRV(ch), 0,
636 IPFW_TLV_STATE_NAME, name);
638 * We always return success here.
639 * The caller will check *pno and mark object as unresolved,
640 * then it will automatically create "default" object.
645 static struct named_object *
646 dyn_findbykidx(struct ip_fw_chain *ch, uint16_t idx)
649 DYN_DEBUG("kidx %d", idx);
650 return (ipfw_objhash_lookup_kidx(CHAIN_TO_SRV(ch), idx));
654 dyn_create(struct ip_fw_chain *ch, struct tid_info *ti,
657 struct namedobj_instance *ni;
658 struct dyn_state_obj *obj;
659 struct named_object *no;
663 DYN_DEBUG("uidx %d", ti->uidx);
665 if (ti->tlvs == NULL)
667 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
668 IPFW_TLV_STATE_NAME);
673 name = default_state_name;
675 ni = CHAIN_TO_SRV(ch);
676 obj = malloc(sizeof(*obj), M_IPFW, M_WAITOK | M_ZERO);
677 obj->no.name = obj->name;
678 obj->no.etlv = IPFW_TLV_STATE_NAME;
679 strlcpy(obj->name, name, sizeof(obj->name));
682 no = ipfw_objhash_lookup_name_type(ni, 0,
683 IPFW_TLV_STATE_NAME, name);
686 * Object is already created.
687 * Just return its kidx and bump refcount.
693 DYN_DEBUG("\tfound kidx %d", *pkidx);
696 if (ipfw_objhash_alloc_idx(ni, &obj->no.kidx) != 0) {
697 DYN_DEBUG("\talloc_idx failed for %s", name);
702 ipfw_objhash_add(ni, &obj->no);
703 SRV_OBJECT(ch, obj->no.kidx) = obj;
705 *pkidx = obj->no.kidx;
707 DYN_DEBUG("\tcreated kidx %d", *pkidx);
712 dyn_destroy(struct ip_fw_chain *ch, struct named_object *no)
714 struct dyn_state_obj *obj;
716 IPFW_UH_WLOCK_ASSERT(ch);
718 KASSERT(no->etlv == IPFW_TLV_STATE_NAME,
719 ("%s: wrong object type %u", __func__, no->etlv));
720 KASSERT(no->refcnt == 1,
721 ("Destroying object '%s' (type %u, idx %u) with refcnt %u",
722 no->name, no->etlv, no->kidx, no->refcnt));
723 DYN_DEBUG("kidx %d", no->kidx);
724 obj = SRV_OBJECT(ch, no->kidx);
725 SRV_OBJECT(ch, no->kidx) = NULL;
726 ipfw_objhash_del(CHAIN_TO_SRV(ch), no);
727 ipfw_objhash_free_idx(CHAIN_TO_SRV(ch), no->kidx);
732 static struct opcode_obj_rewrite dyn_opcodes[] = {
734 O_KEEP_STATE, IPFW_TLV_STATE_NAME,
735 dyn_classify, dyn_update,
736 dyn_findbyname, dyn_findbykidx,
737 dyn_create, dyn_destroy
740 O_CHECK_STATE, IPFW_TLV_STATE_NAME,
741 dyn_classify, dyn_update,
742 dyn_findbyname, dyn_findbykidx,
743 dyn_create, dyn_destroy
746 O_PROBE_STATE, IPFW_TLV_STATE_NAME,
747 dyn_classify, dyn_update,
748 dyn_findbyname, dyn_findbykidx,
749 dyn_create, dyn_destroy
752 O_LIMIT, IPFW_TLV_STATE_NAME,
753 dyn_classify, dyn_update,
754 dyn_findbyname, dyn_findbykidx,
755 dyn_create, dyn_destroy
760 * IMPORTANT: the hash function for dynamic rules must be commutative
761 * in source and destination (ip,port), because rules are bidirectional
762 * and we want to find both in the same bucket.
764 #ifndef IPFIREWALL_JENKINSHASH
765 static __inline uint32_t
766 hash_packet(const struct ipfw_flow_id *id)
771 if (IS_IP6_FLOW_ID(id))
772 i = ntohl((id->dst_ip6.__u6_addr.__u6_addr32[2]) ^
773 (id->dst_ip6.__u6_addr.__u6_addr32[3]) ^
774 (id->src_ip6.__u6_addr.__u6_addr32[2]) ^
775 (id->src_ip6.__u6_addr.__u6_addr32[3]));
778 i = (id->dst_ip) ^ (id->src_ip);
779 i ^= (id->dst_port) ^ (id->src_port);
783 static __inline uint32_t
784 hash_parent(const struct ipfw_flow_id *id, const void *rule)
787 return (hash_packet(id) ^ ((uintptr_t)rule));
790 #else /* IPFIREWALL_JENKINSHASH */
792 VNET_DEFINE_STATIC(uint32_t, dyn_hashseed);
793 #define V_dyn_hashseed VNET(dyn_hashseed)
796 addrcmp4(const struct ipfw_flow_id *id)
799 if (id->src_ip < id->dst_ip)
801 if (id->src_ip > id->dst_ip)
803 if (id->src_port <= id->dst_port)
810 addrcmp6(const struct ipfw_flow_id *id)
814 ret = memcmp(&id->src_ip6, &id->dst_ip6, sizeof(struct in6_addr));
819 if (id->src_port <= id->dst_port)
824 static __inline uint32_t
825 hash_packet6(const struct ipfw_flow_id *id)
828 struct in6_addr addr[2];
832 if (addrcmp6(id) == 0) {
833 t6.addr[0] = id->src_ip6;
834 t6.addr[1] = id->dst_ip6;
835 t6.port[0] = id->src_port;
836 t6.port[1] = id->dst_port;
838 t6.addr[0] = id->dst_ip6;
839 t6.addr[1] = id->src_ip6;
840 t6.port[0] = id->dst_port;
841 t6.port[1] = id->src_port;
843 return (jenkins_hash32((const uint32_t *)&t6,
844 sizeof(t6) / sizeof(uint32_t), V_dyn_hashseed));
848 static __inline uint32_t
849 hash_packet(const struct ipfw_flow_id *id)
856 if (IS_IP4_FLOW_ID(id)) {
857 /* All fields are in host byte order */
858 if (addrcmp4(id) == 0) {
859 t4.addr[0] = id->src_ip;
860 t4.addr[1] = id->dst_ip;
861 t4.port[0] = id->src_port;
862 t4.port[1] = id->dst_port;
864 t4.addr[0] = id->dst_ip;
865 t4.addr[1] = id->src_ip;
866 t4.port[0] = id->dst_port;
867 t4.port[1] = id->src_port;
869 return (jenkins_hash32((const uint32_t *)&t4,
870 sizeof(t4) / sizeof(uint32_t), V_dyn_hashseed));
873 if (IS_IP6_FLOW_ID(id))
874 return (hash_packet6(id));
879 static __inline uint32_t
880 hash_parent(const struct ipfw_flow_id *id, const void *rule)
883 return (jenkins_hash32((const uint32_t *)&rule,
884 sizeof(rule) / sizeof(uint32_t), hash_packet(id)));
886 #endif /* IPFIREWALL_JENKINSHASH */
889 * Print customizable flow id description via log(9) facility.
892 print_dyn_rule_flags(const struct ipfw_flow_id *id, int dyn_type,
893 int log_flags, char *prefix, char *postfix)
897 char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
899 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
903 if (IS_IP6_FLOW_ID(id)) {
904 ip6_sprintf(src, &id->src_ip6);
905 ip6_sprintf(dst, &id->dst_ip6);
909 da.s_addr = htonl(id->src_ip);
910 inet_ntop(AF_INET, &da, src, sizeof(src));
911 da.s_addr = htonl(id->dst_ip);
912 inet_ntop(AF_INET, &da, dst, sizeof(dst));
914 log(log_flags, "ipfw: %s type %d %s %d -> %s %d, %d %s\n",
915 prefix, dyn_type, src, id->src_port, dst,
916 id->dst_port, V_dyn_count, postfix);
919 #define print_dyn_rule(id, dtype, prefix, postfix) \
920 print_dyn_rule_flags(id, dtype, LOG_DEBUG, prefix, postfix)
922 #define TIME_LEQ(a,b) ((int)((a)-(b)) <= 0)
923 #define TIME_LE(a,b) ((int)((a)-(b)) < 0)
924 #define _SEQ_GE(a,b) ((int)((a)-(b)) >= 0)
925 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
926 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
927 #define TCP_FLAGS (TH_FLAGS | (TH_FLAGS << 8))
928 #define ACK_FWD 0x00010000 /* fwd ack seen */
929 #define ACK_REV 0x00020000 /* rev ack seen */
930 #define ACK_BOTH (ACK_FWD | ACK_REV)
933 dyn_update_tcp_state(struct dyn_data *data, const struct ipfw_flow_id *pkt,
934 const struct tcphdr *tcp, int dir)
936 uint32_t ack, expire;
940 expire = data->expire;
941 old = state = data->state;
942 th_flags = pkt->_flags & (TH_FIN | TH_SYN | TH_RST);
943 state |= (dir == MATCH_FORWARD) ? th_flags: (th_flags << 8);
944 switch (state & TCP_FLAGS) {
945 case TH_SYN: /* opening */
946 expire = time_uptime + V_dyn_syn_lifetime;
949 case BOTH_SYN: /* move to established */
950 case BOTH_SYN | TH_FIN: /* one side tries to close */
951 case BOTH_SYN | (TH_FIN << 8):
954 ack = ntohl(tcp->th_ack);
955 if (dir == MATCH_FORWARD) {
956 if (data->ack_fwd == 0 ||
957 _SEQ_GE(ack, data->ack_fwd)) {
959 if (data->ack_fwd != ack)
960 ck_pr_store_32(&data->ack_fwd, ack);
963 if (data->ack_rev == 0 ||
964 _SEQ_GE(ack, data->ack_rev)) {
966 if (data->ack_rev != ack)
967 ck_pr_store_32(&data->ack_rev, ack);
970 if ((state & ACK_BOTH) == ACK_BOTH) {
972 * Set expire time to V_dyn_ack_lifetime only if
973 * we got ACKs for both directions.
974 * We use XOR here to avoid possible state
975 * overwriting in concurrent thread.
977 expire = time_uptime + V_dyn_ack_lifetime;
978 ck_pr_xor_32(&data->state, ACK_BOTH);
979 } else if ((data->state & ACK_BOTH) != (state & ACK_BOTH))
980 ck_pr_or_32(&data->state, state & ACK_BOTH);
983 case BOTH_SYN | BOTH_FIN: /* both sides closed */
984 if (V_dyn_fin_lifetime >= V_dyn_keepalive_period)
985 V_dyn_fin_lifetime = V_dyn_keepalive_period - 1;
986 expire = time_uptime + V_dyn_fin_lifetime;
990 if (V_dyn_keepalive != 0 &&
991 V_dyn_rst_lifetime >= V_dyn_keepalive_period)
992 V_dyn_rst_lifetime = V_dyn_keepalive_period - 1;
993 expire = time_uptime + V_dyn_rst_lifetime;
995 /* Save TCP state if it was changed */
996 if ((state & TCP_FLAGS) != (old & TCP_FLAGS))
997 ck_pr_or_32(&data->state, state & TCP_FLAGS);
1002 * Update ULP specific state.
1003 * For TCP we keep sequence numbers and flags. For other protocols
1004 * currently we update only expire time. Packets and bytes counters
1005 * are also updated here.
1008 dyn_update_proto_state(struct dyn_data *data, const struct ipfw_flow_id *pkt,
1009 const void *ulp, int pktlen, int dir)
1013 /* NOTE: we are in critical section here. */
1014 switch (pkt->proto) {
1016 case IPPROTO_UDPLITE:
1017 expire = time_uptime + V_dyn_udp_lifetime;
1020 expire = dyn_update_tcp_state(data, pkt, ulp, dir);
1023 expire = time_uptime + V_dyn_short_lifetime;
1026 * Expiration timer has the per-second granularity, no need to update
1027 * it every time when state is matched.
1029 if (data->expire != expire)
1030 ck_pr_store_32(&data->expire, expire);
1032 if (dir == MATCH_FORWARD)
1033 DYN_COUNTER_INC(data, fwd, pktlen);
1035 DYN_COUNTER_INC(data, rev, pktlen);
1039 * Lookup IPv4 state.
1040 * Must be called in critical section.
1042 struct dyn_ipv4_state *
1043 dyn_lookup_ipv4_state(const struct ipfw_flow_id *pkt, const void *ulp,
1044 struct ipfw_dyn_info *info, int pktlen)
1046 struct dyn_ipv4_state *s;
1047 uint32_t version, bucket;
1049 bucket = DYN_BUCKET(info->hashval, V_curr_dyn_buckets);
1050 info->version = DYN_BUCKET_VERSION(bucket, ipv4_add);
1052 version = DYN_BUCKET_VERSION(bucket, ipv4_del);
1053 CK_SLIST_FOREACH(s, &V_dyn_ipv4[bucket], entry) {
1054 DYNSTATE_PROTECT(s);
1055 if (version != DYN_BUCKET_VERSION(bucket, ipv4_del))
1057 if (s->proto != pkt->proto)
1059 if (info->kidx != 0 && s->kidx != info->kidx)
1061 if (s->sport == pkt->src_port && s->dport == pkt->dst_port &&
1062 s->src == pkt->src_ip && s->dst == pkt->dst_ip) {
1063 info->direction = MATCH_FORWARD;
1066 if (s->sport == pkt->dst_port && s->dport == pkt->src_port &&
1067 s->src == pkt->dst_ip && s->dst == pkt->src_ip) {
1068 info->direction = MATCH_REVERSE;
1074 dyn_update_proto_state(s->data, pkt, ulp, pktlen,
1080 * Lookup IPv4 state.
1081 * Simplifed version is used to check that matching state doesn't exist.
1084 dyn_lookup_ipv4_state_locked(const struct ipfw_flow_id *pkt,
1085 const void *ulp, int pktlen, uint32_t bucket, uint16_t kidx)
1087 struct dyn_ipv4_state *s;
1091 DYN_BUCKET_ASSERT(bucket);
1092 CK_SLIST_FOREACH(s, &V_dyn_ipv4[bucket], entry) {
1093 if (s->proto != pkt->proto ||
1096 if (s->sport == pkt->src_port &&
1097 s->dport == pkt->dst_port &&
1098 s->src == pkt->src_ip && s->dst == pkt->dst_ip) {
1099 dir = MATCH_FORWARD;
1102 if (s->sport == pkt->dst_port && s->dport == pkt->src_port &&
1103 s->src == pkt->dst_ip && s->dst == pkt->src_ip) {
1104 dir = MATCH_REVERSE;
1109 dyn_update_proto_state(s->data, pkt, ulp, pktlen, dir);
1113 struct dyn_ipv4_state *
1114 dyn_lookup_ipv4_parent(const struct ipfw_flow_id *pkt, const void *rule,
1115 uint32_t ruleid, uint16_t rulenum, uint32_t hashval)
1117 struct dyn_ipv4_state *s;
1118 uint32_t version, bucket;
1120 bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1122 version = DYN_BUCKET_VERSION(bucket, ipv4_parent_del);
1123 CK_SLIST_FOREACH(s, &V_dyn_ipv4_parent[bucket], entry) {
1124 DYNSTATE_PROTECT(s);
1125 if (version != DYN_BUCKET_VERSION(bucket, ipv4_parent_del))
1128 * NOTE: we do not need to check kidx, because parent rule
1129 * can not create states with different kidx.
1130 * And parent rule always created for forward direction.
1132 if (s->limit->parent == rule &&
1133 s->limit->ruleid == ruleid &&
1134 s->limit->rulenum == rulenum &&
1135 s->proto == pkt->proto &&
1136 s->sport == pkt->src_port &&
1137 s->dport == pkt->dst_port &&
1138 s->src == pkt->src_ip && s->dst == pkt->dst_ip) {
1139 if (s->limit->expire != time_uptime +
1140 V_dyn_short_lifetime)
1141 ck_pr_store_32(&s->limit->expire,
1142 time_uptime + V_dyn_short_lifetime);
1149 static struct dyn_ipv4_state *
1150 dyn_lookup_ipv4_parent_locked(const struct ipfw_flow_id *pkt,
1151 const void *rule, uint32_t ruleid, uint16_t rulenum, uint32_t bucket)
1153 struct dyn_ipv4_state *s;
1155 DYN_BUCKET_ASSERT(bucket);
1156 CK_SLIST_FOREACH(s, &V_dyn_ipv4_parent[bucket], entry) {
1157 if (s->limit->parent == rule &&
1158 s->limit->ruleid == ruleid &&
1159 s->limit->rulenum == rulenum &&
1160 s->proto == pkt->proto &&
1161 s->sport == pkt->src_port &&
1162 s->dport == pkt->dst_port &&
1163 s->src == pkt->src_ip && s->dst == pkt->dst_ip)
1172 dyn_getscopeid(const struct ip_fw_args *args)
1176 * If source or destination address is an scopeid address, we need
1177 * determine the scope zone id to resolve address scope ambiguity.
1179 if (IN6_IS_ADDR_LINKLOCAL(&args->f_id.src_ip6) ||
1180 IN6_IS_ADDR_LINKLOCAL(&args->f_id.dst_ip6)) {
1181 MPASS(args->oif != NULL ||
1182 args->m->m_pkthdr.rcvif != NULL);
1183 return (in6_getscopezone(args->oif != NULL ? args->oif:
1184 args->m->m_pkthdr.rcvif, IPV6_ADDR_SCOPE_LINKLOCAL));
1190 * Lookup IPv6 state.
1191 * Must be called in critical section.
1193 static struct dyn_ipv6_state *
1194 dyn_lookup_ipv6_state(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1195 const void *ulp, struct ipfw_dyn_info *info, int pktlen)
1197 struct dyn_ipv6_state *s;
1198 uint32_t version, bucket;
1200 bucket = DYN_BUCKET(info->hashval, V_curr_dyn_buckets);
1201 info->version = DYN_BUCKET_VERSION(bucket, ipv6_add);
1203 version = DYN_BUCKET_VERSION(bucket, ipv6_del);
1204 CK_SLIST_FOREACH(s, &V_dyn_ipv6[bucket], entry) {
1205 DYNSTATE_PROTECT(s);
1206 if (version != DYN_BUCKET_VERSION(bucket, ipv6_del))
1208 if (s->proto != pkt->proto || s->zoneid != zoneid)
1210 if (info->kidx != 0 && s->kidx != info->kidx)
1212 if (s->sport == pkt->src_port && s->dport == pkt->dst_port &&
1213 IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) &&
1214 IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) {
1215 info->direction = MATCH_FORWARD;
1218 if (s->sport == pkt->dst_port && s->dport == pkt->src_port &&
1219 IN6_ARE_ADDR_EQUAL(&s->src, &pkt->dst_ip6) &&
1220 IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->src_ip6)) {
1221 info->direction = MATCH_REVERSE;
1226 dyn_update_proto_state(s->data, pkt, ulp, pktlen,
1232 * Lookup IPv6 state.
1233 * Simplifed version is used to check that matching state doesn't exist.
1236 dyn_lookup_ipv6_state_locked(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1237 const void *ulp, int pktlen, uint32_t bucket, uint16_t kidx)
1239 struct dyn_ipv6_state *s;
1243 DYN_BUCKET_ASSERT(bucket);
1244 CK_SLIST_FOREACH(s, &V_dyn_ipv6[bucket], entry) {
1245 if (s->proto != pkt->proto || s->kidx != kidx ||
1246 s->zoneid != zoneid)
1248 if (s->sport == pkt->src_port && s->dport == pkt->dst_port &&
1249 IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) &&
1250 IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) {
1251 dir = MATCH_FORWARD;
1254 if (s->sport == pkt->dst_port && s->dport == pkt->src_port &&
1255 IN6_ARE_ADDR_EQUAL(&s->src, &pkt->dst_ip6) &&
1256 IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->src_ip6)) {
1257 dir = MATCH_REVERSE;
1262 dyn_update_proto_state(s->data, pkt, ulp, pktlen, dir);
1266 static struct dyn_ipv6_state *
1267 dyn_lookup_ipv6_parent(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1268 const void *rule, uint32_t ruleid, uint16_t rulenum, uint32_t hashval)
1270 struct dyn_ipv6_state *s;
1271 uint32_t version, bucket;
1273 bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1275 version = DYN_BUCKET_VERSION(bucket, ipv6_parent_del);
1276 CK_SLIST_FOREACH(s, &V_dyn_ipv6_parent[bucket], entry) {
1277 DYNSTATE_PROTECT(s);
1278 if (version != DYN_BUCKET_VERSION(bucket, ipv6_parent_del))
1281 * NOTE: we do not need to check kidx, because parent rule
1282 * can not create states with different kidx.
1283 * Also parent rule always created for forward direction.
1285 if (s->limit->parent == rule &&
1286 s->limit->ruleid == ruleid &&
1287 s->limit->rulenum == rulenum &&
1288 s->proto == pkt->proto &&
1289 s->sport == pkt->src_port &&
1290 s->dport == pkt->dst_port && s->zoneid == zoneid &&
1291 IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) &&
1292 IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) {
1293 if (s->limit->expire != time_uptime +
1294 V_dyn_short_lifetime)
1295 ck_pr_store_32(&s->limit->expire,
1296 time_uptime + V_dyn_short_lifetime);
1303 static struct dyn_ipv6_state *
1304 dyn_lookup_ipv6_parent_locked(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1305 const void *rule, uint32_t ruleid, uint16_t rulenum, uint32_t bucket)
1307 struct dyn_ipv6_state *s;
1309 DYN_BUCKET_ASSERT(bucket);
1310 CK_SLIST_FOREACH(s, &V_dyn_ipv6_parent[bucket], entry) {
1311 if (s->limit->parent == rule &&
1312 s->limit->ruleid == ruleid &&
1313 s->limit->rulenum == rulenum &&
1314 s->proto == pkt->proto &&
1315 s->sport == pkt->src_port &&
1316 s->dport == pkt->dst_port && s->zoneid == zoneid &&
1317 IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) &&
1318 IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6))
1327 * Lookup dynamic state.
1328 * pkt - filled by ipfw_chk() ipfw_flow_id;
1329 * ulp - determined by ipfw_chk() upper level protocol header;
1330 * dyn_info - info about matched state to return back;
1331 * Returns pointer to state's parent rule and dyn_info. If there is
1332 * no state, NULL is returned.
1333 * On match ipfw_dyn_lookup() updates state's counters.
1336 ipfw_dyn_lookup_state(const struct ip_fw_args *args, const void *ulp,
1337 int pktlen, const ipfw_insn *cmd, struct ipfw_dyn_info *info)
1339 struct dyn_data *data;
1342 IPFW_RLOCK_ASSERT(&V_layer3_chain);
1346 info->kidx = cmd->arg1;
1347 info->direction = MATCH_NONE;
1348 info->hashval = hash_packet(&args->f_id);
1350 DYNSTATE_CRITICAL_ENTER();
1351 if (IS_IP4_FLOW_ID(&args->f_id)) {
1352 struct dyn_ipv4_state *s;
1354 s = dyn_lookup_ipv4_state(&args->f_id, ulp, info, pktlen);
1357 * Dynamic states are created using the same 5-tuple,
1358 * so it is assumed, that parent rule for O_LIMIT
1359 * state has the same address family.
1362 if (s->type == O_LIMIT) {
1364 rule = s->limit->parent;
1366 rule = data->parent;
1370 else if (IS_IP6_FLOW_ID(&args->f_id)) {
1371 struct dyn_ipv6_state *s;
1373 s = dyn_lookup_ipv6_state(&args->f_id, dyn_getscopeid(args),
1377 if (s->type == O_LIMIT) {
1379 rule = s->limit->parent;
1381 rule = data->parent;
1387 * If cached chain id is the same, we can avoid rule index
1388 * lookup. Otherwise do lookup and update chain_id and f_pos.
1389 * It is safe even if there is concurrent thread that want
1390 * update the same state, because chain->id can be changed
1391 * only under IPFW_WLOCK().
1393 if (data->chain_id != V_layer3_chain.id) {
1394 data->f_pos = ipfw_find_rule(&V_layer3_chain,
1395 data->rulenum, data->ruleid);
1397 * Check that found state has not orphaned.
1398 * When chain->id being changed the parent
1399 * rule can be deleted. If found rule doesn't
1400 * match the parent pointer, consider this
1401 * result as MATCH_NONE and return NULL.
1403 * This will lead to creation of new similar state
1404 * that will be added into head of this bucket.
1405 * And the state that we currently have matched
1406 * should be deleted by dyn_expire_states().
1408 * In case when dyn_keep_states is enabled, return
1409 * pointer to deleted rule and f_pos value
1410 * corresponding to penultimate rule.
1411 * When we have enabled V_dyn_keep_states, states
1412 * that become orphaned will get the DYN_REFERENCED
1413 * flag and rule will keep around. So we can return
1414 * it. But since it is not in the rules map, we need
1415 * return such f_pos value, so after the state
1416 * handling if the search will continue, the next rule
1417 * will be the last one - the default rule.
1419 if (V_layer3_chain.map[data->f_pos] == rule) {
1420 data->chain_id = V_layer3_chain.id;
1421 info->f_pos = data->f_pos;
1422 } else if (V_dyn_keep_states != 0) {
1424 * The original rule pointer is still usable.
1425 * So, we return it, but f_pos need to be
1426 * changed to point to the penultimate rule.
1428 MPASS(V_layer3_chain.n_rules > 1);
1429 data->chain_id = V_layer3_chain.id;
1430 data->f_pos = V_layer3_chain.n_rules - 2;
1431 info->f_pos = data->f_pos;
1434 info->direction = MATCH_NONE;
1435 DYN_DEBUG("rule %p [%u, %u] is considered "
1436 "invalid in data %p", rule, data->ruleid,
1437 data->rulenum, data);
1438 /* info->f_pos doesn't matter here. */
1441 info->f_pos = data->f_pos;
1443 DYNSTATE_CRITICAL_EXIT();
1446 * Return MATCH_NONE if parent rule is in disabled set.
1447 * This will lead to creation of new similar state that
1448 * will be added into head of this bucket.
1450 * XXXAE: we need to be able update state's set when parent
1451 * rule set is changed.
1453 if (rule != NULL && (V_set_disable & (1 << rule->set))) {
1455 info->direction = MATCH_NONE;
1461 static struct dyn_parent *
1462 dyn_alloc_parent(void *parent, uint32_t ruleid, uint16_t rulenum,
1463 uint8_t set, uint32_t hashval)
1465 struct dyn_parent *limit;
1467 limit = uma_zalloc(V_dyn_parent_zone, M_NOWAIT | M_ZERO);
1468 if (limit == NULL) {
1469 if (last_log != time_uptime) {
1470 last_log = time_uptime;
1472 "ipfw: Cannot allocate parent dynamic state, "
1473 "consider increasing "
1474 "net.inet.ip.fw.dyn_parent_max\n");
1479 limit->parent = parent;
1480 limit->ruleid = ruleid;
1481 limit->rulenum = rulenum;
1483 limit->hashval = hashval;
1484 limit->expire = time_uptime + V_dyn_short_lifetime;
1488 static struct dyn_data *
1489 dyn_alloc_dyndata(void *parent, uint32_t ruleid, uint16_t rulenum,
1490 uint8_t set, const struct ipfw_flow_id *pkt, const void *ulp, int pktlen,
1491 uint32_t hashval, uint16_t fibnum)
1493 struct dyn_data *data;
1495 data = uma_zalloc(V_dyn_data_zone, M_NOWAIT | M_ZERO);
1497 if (last_log != time_uptime) {
1498 last_log = time_uptime;
1500 "ipfw: Cannot allocate dynamic state, "
1501 "consider increasing net.inet.ip.fw.dyn_max\n");
1506 data->parent = parent;
1507 data->ruleid = ruleid;
1508 data->rulenum = rulenum;
1510 data->fibnum = fibnum;
1511 data->hashval = hashval;
1512 data->expire = time_uptime + V_dyn_syn_lifetime;
1513 dyn_update_proto_state(data, pkt, ulp, pktlen, MATCH_FORWARD);
1517 static struct dyn_ipv4_state *
1518 dyn_alloc_ipv4_state(const struct ipfw_flow_id *pkt, uint16_t kidx,
1521 struct dyn_ipv4_state *s;
1523 s = uma_zalloc(V_dyn_ipv4_zone, M_NOWAIT | M_ZERO);
1529 s->proto = pkt->proto;
1530 s->sport = pkt->src_port;
1531 s->dport = pkt->dst_port;
1532 s->src = pkt->src_ip;
1533 s->dst = pkt->dst_ip;
1538 * Add IPv4 parent state.
1539 * Returns pointer to parent state. When it is not NULL we are in
1540 * critical section and pointer protected by hazard pointer.
1541 * When some error occurs, it returns NULL and exit from critical section
1544 static struct dyn_ipv4_state *
1545 dyn_add_ipv4_parent(void *rule, uint32_t ruleid, uint16_t rulenum,
1546 uint8_t set, const struct ipfw_flow_id *pkt, uint32_t hashval,
1547 uint32_t version, uint16_t kidx)
1549 struct dyn_ipv4_state *s;
1550 struct dyn_parent *limit;
1553 bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1554 DYN_BUCKET_LOCK(bucket);
1555 if (version != DYN_BUCKET_VERSION(bucket, ipv4_parent_add)) {
1557 * Bucket version has been changed since last lookup,
1558 * do lookup again to be sure that state does not exist.
1560 s = dyn_lookup_ipv4_parent_locked(pkt, rule, ruleid,
1564 * Simultaneous thread has already created this
1565 * state. Just return it.
1567 DYNSTATE_CRITICAL_ENTER();
1568 DYNSTATE_PROTECT(s);
1569 DYN_BUCKET_UNLOCK(bucket);
1574 limit = dyn_alloc_parent(rule, ruleid, rulenum, set, hashval);
1575 if (limit == NULL) {
1576 DYN_BUCKET_UNLOCK(bucket);
1580 s = dyn_alloc_ipv4_state(pkt, kidx, O_LIMIT_PARENT);
1582 DYN_BUCKET_UNLOCK(bucket);
1583 uma_zfree(V_dyn_parent_zone, limit);
1588 CK_SLIST_INSERT_HEAD(&V_dyn_ipv4_parent[bucket], s, entry);
1589 DYN_COUNT_INC(dyn_parent_count);
1590 DYN_BUCKET_VERSION_BUMP(bucket, ipv4_parent_add);
1591 DYNSTATE_CRITICAL_ENTER();
1592 DYNSTATE_PROTECT(s);
1593 DYN_BUCKET_UNLOCK(bucket);
1598 dyn_add_ipv4_state(void *parent, uint32_t ruleid, uint16_t rulenum,
1599 uint8_t set, const struct ipfw_flow_id *pkt, const void *ulp, int pktlen,
1600 uint32_t hashval, struct ipfw_dyn_info *info, uint16_t fibnum,
1601 uint16_t kidx, uint8_t type)
1603 struct dyn_ipv4_state *s;
1607 bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1608 DYN_BUCKET_LOCK(bucket);
1609 if (info->direction == MATCH_UNKNOWN ||
1610 info->kidx != kidx ||
1611 info->hashval != hashval ||
1612 info->version != DYN_BUCKET_VERSION(bucket, ipv4_add)) {
1614 * Bucket version has been changed since last lookup,
1615 * do lookup again to be sure that state does not exist.
1617 if (dyn_lookup_ipv4_state_locked(pkt, ulp, pktlen,
1618 bucket, kidx) != 0) {
1619 DYN_BUCKET_UNLOCK(bucket);
1624 data = dyn_alloc_dyndata(parent, ruleid, rulenum, set, pkt, ulp,
1625 pktlen, hashval, fibnum);
1627 DYN_BUCKET_UNLOCK(bucket);
1631 s = dyn_alloc_ipv4_state(pkt, kidx, type);
1633 DYN_BUCKET_UNLOCK(bucket);
1634 uma_zfree(V_dyn_data_zone, data);
1639 CK_SLIST_INSERT_HEAD(&V_dyn_ipv4[bucket], s, entry);
1640 DYN_COUNT_INC(dyn_count);
1641 DYN_BUCKET_VERSION_BUMP(bucket, ipv4_add);
1642 DYN_BUCKET_UNLOCK(bucket);
1647 static struct dyn_ipv6_state *
1648 dyn_alloc_ipv6_state(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1649 uint16_t kidx, uint8_t type)
1651 struct dyn_ipv6_state *s;
1653 s = uma_zalloc(V_dyn_ipv6_zone, M_NOWAIT | M_ZERO);
1660 s->proto = pkt->proto;
1661 s->sport = pkt->src_port;
1662 s->dport = pkt->dst_port;
1663 s->src = pkt->src_ip6;
1664 s->dst = pkt->dst_ip6;
1669 * Add IPv6 parent state.
1670 * Returns pointer to parent state. When it is not NULL we are in
1671 * critical section and pointer protected by hazard pointer.
1672 * When some error occurs, it return NULL and exit from critical section
1675 static struct dyn_ipv6_state *
1676 dyn_add_ipv6_parent(void *rule, uint32_t ruleid, uint16_t rulenum,
1677 uint8_t set, const struct ipfw_flow_id *pkt, uint32_t zoneid,
1678 uint32_t hashval, uint32_t version, uint16_t kidx)
1680 struct dyn_ipv6_state *s;
1681 struct dyn_parent *limit;
1684 bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1685 DYN_BUCKET_LOCK(bucket);
1686 if (version != DYN_BUCKET_VERSION(bucket, ipv6_parent_add)) {
1688 * Bucket version has been changed since last lookup,
1689 * do lookup again to be sure that state does not exist.
1691 s = dyn_lookup_ipv6_parent_locked(pkt, zoneid, rule, ruleid,
1695 * Simultaneous thread has already created this
1696 * state. Just return it.
1698 DYNSTATE_CRITICAL_ENTER();
1699 DYNSTATE_PROTECT(s);
1700 DYN_BUCKET_UNLOCK(bucket);
1705 limit = dyn_alloc_parent(rule, ruleid, rulenum, set, hashval);
1706 if (limit == NULL) {
1707 DYN_BUCKET_UNLOCK(bucket);
1711 s = dyn_alloc_ipv6_state(pkt, zoneid, kidx, O_LIMIT_PARENT);
1713 DYN_BUCKET_UNLOCK(bucket);
1714 uma_zfree(V_dyn_parent_zone, limit);
1719 CK_SLIST_INSERT_HEAD(&V_dyn_ipv6_parent[bucket], s, entry);
1720 DYN_COUNT_INC(dyn_parent_count);
1721 DYN_BUCKET_VERSION_BUMP(bucket, ipv6_parent_add);
1722 DYNSTATE_CRITICAL_ENTER();
1723 DYNSTATE_PROTECT(s);
1724 DYN_BUCKET_UNLOCK(bucket);
1729 dyn_add_ipv6_state(void *parent, uint32_t ruleid, uint16_t rulenum,
1730 uint8_t set, const struct ipfw_flow_id *pkt, uint32_t zoneid,
1731 const void *ulp, int pktlen, uint32_t hashval, struct ipfw_dyn_info *info,
1732 uint16_t fibnum, uint16_t kidx, uint8_t type)
1734 struct dyn_ipv6_state *s;
1735 struct dyn_data *data;
1738 bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1739 DYN_BUCKET_LOCK(bucket);
1740 if (info->direction == MATCH_UNKNOWN ||
1741 info->kidx != kidx ||
1742 info->hashval != hashval ||
1743 info->version != DYN_BUCKET_VERSION(bucket, ipv6_add)) {
1745 * Bucket version has been changed since last lookup,
1746 * do lookup again to be sure that state does not exist.
1748 if (dyn_lookup_ipv6_state_locked(pkt, zoneid, ulp, pktlen,
1749 bucket, kidx) != 0) {
1750 DYN_BUCKET_UNLOCK(bucket);
1755 data = dyn_alloc_dyndata(parent, ruleid, rulenum, set, pkt, ulp,
1756 pktlen, hashval, fibnum);
1758 DYN_BUCKET_UNLOCK(bucket);
1762 s = dyn_alloc_ipv6_state(pkt, zoneid, kidx, type);
1764 DYN_BUCKET_UNLOCK(bucket);
1765 uma_zfree(V_dyn_data_zone, data);
1770 CK_SLIST_INSERT_HEAD(&V_dyn_ipv6[bucket], s, entry);
1771 DYN_COUNT_INC(dyn_count);
1772 DYN_BUCKET_VERSION_BUMP(bucket, ipv6_add);
1773 DYN_BUCKET_UNLOCK(bucket);
1779 dyn_get_parent_state(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1780 struct ip_fw *rule, uint32_t hashval, uint32_t limit, uint16_t kidx)
1783 struct dyn_parent *p;
1785 uint32_t bucket, version;
1789 bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1790 DYNSTATE_CRITICAL_ENTER();
1791 if (IS_IP4_FLOW_ID(pkt)) {
1792 struct dyn_ipv4_state *s;
1794 version = DYN_BUCKET_VERSION(bucket, ipv4_parent_add);
1795 s = dyn_lookup_ipv4_parent(pkt, rule, rule->id,
1796 rule->rulenum, bucket);
1799 * Exit from critical section because dyn_add_parent()
1800 * will acquire bucket lock.
1802 DYNSTATE_CRITICAL_EXIT();
1804 s = dyn_add_ipv4_parent(rule, rule->id,
1805 rule->rulenum, rule->set, pkt, hashval,
1809 /* Now we are in critical section again. */
1815 else if (IS_IP6_FLOW_ID(pkt)) {
1816 struct dyn_ipv6_state *s;
1818 version = DYN_BUCKET_VERSION(bucket, ipv6_parent_add);
1819 s = dyn_lookup_ipv6_parent(pkt, zoneid, rule, rule->id,
1820 rule->rulenum, bucket);
1823 * Exit from critical section because dyn_add_parent()
1824 * can acquire bucket mutex.
1826 DYNSTATE_CRITICAL_EXIT();
1828 s = dyn_add_ipv6_parent(rule, rule->id,
1829 rule->rulenum, rule->set, pkt, zoneid, hashval,
1833 /* Now we are in critical section again. */
1840 DYNSTATE_CRITICAL_EXIT();
1844 /* Check the limit */
1845 if (DPARENT_COUNT(p) >= limit) {
1846 DYNSTATE_CRITICAL_EXIT();
1847 if (V_fw_verbose && last_log != time_uptime) {
1848 last_log = time_uptime;
1849 snprintf(sbuf, sizeof(sbuf), "%u drop session",
1851 print_dyn_rule_flags(pkt, O_LIMIT,
1852 LOG_SECURITY | LOG_DEBUG, sbuf,
1853 "too many entries");
1858 /* Take new session into account. */
1859 DPARENT_COUNT_INC(p);
1861 * We must exit from critical section because the following code
1862 * can acquire bucket mutex.
1863 * We rely on the the 'count' field. The state will not expire
1864 * until it has some child states, i.e. 'count' field is not zero.
1865 * Return state pointer, it will be used by child states as parent.
1867 DYNSTATE_CRITICAL_EXIT();
1872 dyn_install_state(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1873 uint16_t fibnum, const void *ulp, int pktlen, void *rule,
1874 uint32_t ruleid, uint16_t rulenum, uint8_t set,
1875 struct ipfw_dyn_info *info, uint32_t limit, uint16_t limit_mask,
1876 uint16_t kidx, uint8_t type)
1878 struct ipfw_flow_id id;
1879 uint32_t hashval, parent_hashval;
1882 MPASS(type == O_LIMIT || type == O_KEEP_STATE);
1884 if (type == O_LIMIT) {
1885 /* Create masked flow id and calculate bucket */
1886 id.addr_type = pkt->addr_type;
1887 id.proto = pkt->proto;
1888 id.fib = fibnum; /* unused */
1889 id.src_port = (limit_mask & DYN_SRC_PORT) ?
1891 id.dst_port = (limit_mask & DYN_DST_PORT) ?
1893 if (IS_IP4_FLOW_ID(pkt)) {
1894 id.src_ip = (limit_mask & DYN_SRC_ADDR) ?
1896 id.dst_ip = (limit_mask & DYN_DST_ADDR) ?
1900 else if (IS_IP6_FLOW_ID(pkt)) {
1901 if (limit_mask & DYN_SRC_ADDR)
1902 id.src_ip6 = pkt->src_ip6;
1904 memset(&id.src_ip6, 0, sizeof(id.src_ip6));
1905 if (limit_mask & DYN_DST_ADDR)
1906 id.dst_ip6 = pkt->dst_ip6;
1908 memset(&id.dst_ip6, 0, sizeof(id.dst_ip6));
1912 return (EAFNOSUPPORT);
1914 parent_hashval = hash_parent(&id, rule);
1915 rule = dyn_get_parent_state(&id, zoneid, rule, parent_hashval,
1919 if (V_fw_verbose && last_log != time_uptime) {
1920 last_log = time_uptime;
1921 snprintf(sbuf, sizeof(sbuf),
1922 "%u drop session", rule->rulenum);
1923 print_dyn_rule_flags(pkt, O_LIMIT,
1924 LOG_SECURITY | LOG_DEBUG, sbuf,
1925 "too many entries");
1931 * Limit is not reached, create new state.
1932 * Now rule points to parent state.
1936 hashval = hash_packet(pkt);
1937 if (IS_IP4_FLOW_ID(pkt))
1938 ret = dyn_add_ipv4_state(rule, ruleid, rulenum, set, pkt,
1939 ulp, pktlen, hashval, info, fibnum, kidx, type);
1941 else if (IS_IP6_FLOW_ID(pkt))
1942 ret = dyn_add_ipv6_state(rule, ruleid, rulenum, set, pkt,
1943 zoneid, ulp, pktlen, hashval, info, fibnum, kidx, type);
1948 if (type == O_LIMIT) {
1951 * We failed to create child state for O_LIMIT
1952 * opcode. Since we already counted it in the parent,
1953 * we must revert counter back. The 'rule' points to
1954 * parent state, use it to get dyn_parent.
1956 * XXXAE: it should be safe to use 'rule' pointer
1957 * without extra lookup, parent state is referenced
1958 * and should not be freed.
1960 if (IS_IP4_FLOW_ID(&id))
1962 ((struct dyn_ipv4_state *)rule)->limit);
1964 else if (IS_IP6_FLOW_ID(&id))
1966 ((struct dyn_ipv6_state *)rule)->limit);
1971 * EEXIST means that simultaneous thread has created this
1972 * state. Consider this as success.
1974 * XXXAE: should we invalidate 'info' content here?
1982 * Install dynamic state.
1983 * chain - ipfw's instance;
1984 * rule - the parent rule that installs the state;
1985 * cmd - opcode that installs the state;
1986 * args - ipfw arguments;
1987 * ulp - upper level protocol header;
1988 * pktlen - packet length;
1989 * info - dynamic state lookup info;
1990 * tablearg - tablearg id.
1992 * Returns non-zero value (failure) if state is not installed because
1993 * of errors or because session limitations are enforced.
1996 ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule,
1997 const ipfw_insn_limit *cmd, const struct ip_fw_args *args,
1998 const void *ulp, int pktlen, struct ipfw_dyn_info *info,
2002 uint16_t limit_mask;
2004 if (cmd->o.opcode == O_LIMIT) {
2005 limit = IP_FW_ARG_TABLEARG(chain, cmd->conn_limit, limit);
2006 limit_mask = cmd->limit_mask;
2011 return (dyn_install_state(&args->f_id,
2013 IS_IP6_FLOW_ID(&args->f_id) ? dyn_getscopeid(args):
2015 0, M_GETFIB(args->m), ulp, pktlen, rule, rule->id, rule->rulenum,
2016 rule->set, info, limit, limit_mask, cmd->o.arg1, cmd->o.opcode));
2020 * Free safe to remove state entries from expired lists.
2023 dyn_free_states(struct ip_fw_chain *chain)
2025 struct dyn_ipv4_state *s4, *s4n;
2027 struct dyn_ipv6_state *s6, *s6n;
2029 int cached_count, i;
2032 * We keep pointers to objects that are in use on each CPU
2033 * in the per-cpu dyn_hp pointer. When object is going to be
2034 * removed, first of it is unlinked from the corresponding
2035 * list. This leads to changing of dyn_bucket_xxx_delver version.
2036 * Unlinked objects is placed into corresponding dyn_expired_xxx
2037 * list. Reader that is going to dereference object pointer checks
2038 * dyn_bucket_xxx_delver version before and after storing pointer
2039 * into dyn_hp. If version is the same, the object is protected
2040 * from freeing and it is safe to dereference. Othervise reader
2041 * tries to iterate list again from the beginning, but this object
2042 * now unlinked and thus will not be accessible.
2044 * Copy dyn_hp pointers for each CPU into dyn_hp_cache array.
2045 * It does not matter that some pointer can be changed in
2046 * time while we are copying. We need to check, that objects
2047 * removed in the previous pass are not in use. And if dyn_hp
2048 * pointer does not contain it in the time when we are copying,
2049 * it will not appear there, because it is already unlinked.
2050 * And for new pointers we will not free objects that will be
2051 * unlinked in this pass.
2055 dyn_hp_cache[cached_count] = DYNSTATE_GET(i);
2056 if (dyn_hp_cache[cached_count] != NULL)
2061 * Free expired states that are safe to free.
2062 * Check each entry from previous pass in the dyn_expired_xxx
2063 * list, if pointer to the object is in the dyn_hp_cache array,
2064 * keep it until next pass. Otherwise it is safe to free the
2067 * XXXAE: optimize this to use SLIST_REMOVE_AFTER.
2069 #define DYN_FREE_STATES(s, next, name) do { \
2070 s = SLIST_FIRST(&V_dyn_expired_ ## name); \
2071 while (s != NULL) { \
2072 next = SLIST_NEXT(s, expired); \
2073 for (i = 0; i < cached_count; i++) \
2074 if (dyn_hp_cache[i] == s) \
2076 if (i == cached_count) { \
2077 if (s->type == O_LIMIT_PARENT && \
2078 s->limit->count != 0) { \
2082 SLIST_REMOVE(&V_dyn_expired_ ## name, \
2083 s, dyn_ ## name ## _state, expired); \
2084 if (s->type == O_LIMIT_PARENT) \
2085 uma_zfree(V_dyn_parent_zone, s->limit); \
2087 uma_zfree(V_dyn_data_zone, s->data); \
2088 uma_zfree(V_dyn_ ## name ## _zone, s); \
2095 * Protect access to expired lists with DYN_EXPIRED_LOCK.
2096 * Userland can invoke ipfw_expire_dyn_states() to delete
2097 * specific states, this will lead to modification of expired
2100 * XXXAE: do we need DYN_EXPIRED_LOCK? We can just use
2101 * IPFW_UH_WLOCK to protect access to these lists.
2104 DYN_FREE_STATES(s4, s4n, ipv4);
2106 DYN_FREE_STATES(s6, s6n, ipv6);
2108 DYN_EXPIRED_UNLOCK();
2109 #undef DYN_FREE_STATES
2114 * 0 when state is not matched by specified range;
2115 * 1 when state is matched by specified range;
2116 * 2 when state is matched by specified range and requested deletion of
2120 dyn_match_range(uint16_t rulenum, uint8_t set, const ipfw_range_tlv *rt)
2124 /* flush all states */
2125 if (rt->flags & IPFW_RCFLAG_ALL) {
2126 if (rt->flags & IPFW_RCFLAG_DYNAMIC)
2127 return (2); /* forced */
2130 if ((rt->flags & IPFW_RCFLAG_SET) != 0 && set != rt->set)
2132 if ((rt->flags & IPFW_RCFLAG_RANGE) != 0 &&
2133 (rulenum < rt->start_rule || rulenum > rt->end_rule))
2135 if (rt->flags & IPFW_RCFLAG_DYNAMIC)
2141 dyn_acquire_rule(struct ip_fw_chain *ch, struct dyn_data *data,
2142 struct ip_fw *rule, uint16_t kidx)
2144 struct dyn_state_obj *obj;
2147 * Do not acquire reference twice.
2148 * This can happen when rule deletion executed for
2149 * the same range, but different ruleset id.
2151 if (data->flags & DYN_REFERENCED)
2154 IPFW_UH_WLOCK_ASSERT(ch);
2157 data->flags |= DYN_REFERENCED;
2158 /* Reference the named object */
2159 obj = SRV_OBJECT(ch, kidx);
2161 MPASS(obj->no.etlv == IPFW_TLV_STATE_NAME);
2163 /* Reference the parent rule */
2168 dyn_release_rule(struct ip_fw_chain *ch, struct dyn_data *data,
2169 struct ip_fw *rule, uint16_t kidx)
2171 struct dyn_state_obj *obj;
2173 IPFW_UH_WLOCK_ASSERT(ch);
2176 obj = SRV_OBJECT(ch, kidx);
2177 if (obj->no.refcnt == 1)
2178 dyn_destroy(ch, &obj->no);
2182 if (--rule->refcnt == 1)
2183 ipfw_free_rule(rule);
2187 * We do not keep O_LIMIT_PARENT states when V_dyn_keep_states is enabled.
2188 * O_LIMIT state is created when new connection is going to be established
2189 * and there is no matching state. So, since the old parent rule was deleted
2190 * we can't create new states with old parent, and thus we can not account
2191 * new connections with already established connections, and can not do
2195 dyn_match_ipv4_state(struct ip_fw_chain *ch, struct dyn_ipv4_state *s,
2196 const ipfw_range_tlv *rt)
2201 if (s->type == O_LIMIT_PARENT)
2202 return (dyn_match_range(s->limit->rulenum,
2203 s->limit->set, rt));
2205 ret = dyn_match_range(s->data->rulenum, s->data->set, rt);
2206 if (ret == 0 || V_dyn_keep_states == 0 || ret > 1)
2209 rule = s->data->parent;
2210 if (s->type == O_LIMIT)
2211 rule = ((struct dyn_ipv4_state *)rule)->limit->parent;
2212 dyn_acquire_rule(ch, s->data, rule, s->kidx);
2218 dyn_match_ipv6_state(struct ip_fw_chain *ch, struct dyn_ipv6_state *s,
2219 const ipfw_range_tlv *rt)
2224 if (s->type == O_LIMIT_PARENT)
2225 return (dyn_match_range(s->limit->rulenum,
2226 s->limit->set, rt));
2228 ret = dyn_match_range(s->data->rulenum, s->data->set, rt);
2229 if (ret == 0 || V_dyn_keep_states == 0 || ret > 1)
2232 rule = s->data->parent;
2233 if (s->type == O_LIMIT)
2234 rule = ((struct dyn_ipv6_state *)rule)->limit->parent;
2235 dyn_acquire_rule(ch, s->data, rule, s->kidx);
2241 * Unlink expired entries from states lists.
2242 * @rt can be used to specify the range of states for deletion.
2245 dyn_expire_states(struct ip_fw_chain *ch, ipfw_range_tlv *rt)
2247 struct dyn_ipv4_slist expired_ipv4;
2249 struct dyn_ipv6_slist expired_ipv6;
2250 struct dyn_ipv6_state *s6, *s6n, *s6p;
2252 struct dyn_ipv4_state *s4, *s4n, *s4p;
2254 int bucket, removed, length, max_length;
2256 IPFW_UH_WLOCK_ASSERT(ch);
2259 * Unlink expired states from each bucket.
2260 * With acquired bucket lock iterate entries of each lists:
2261 * ipv4, ipv4_parent, ipv6, and ipv6_parent. Check expired time
2262 * and unlink entry from the list, link entry into temporary
2263 * expired_xxx lists then bump "del" bucket version.
2265 * When an entry is removed, corresponding states counter is
2266 * decremented. If entry has O_LIMIT type, parent's reference
2267 * counter is decremented.
2269 * NOTE: this function can be called from userspace context
2270 * when user deletes rules. In this case all matched states
2271 * will be forcedly unlinked. O_LIMIT_PARENT states will be kept
2272 * in the expired lists until reference counter become zero.
2274 #define DYN_UNLINK_STATES(s, prev, next, exp, af, name, extra) do { \
2278 s = CK_SLIST_FIRST(&V_dyn_ ## name [bucket]); \
2279 while (s != NULL) { \
2280 next = CK_SLIST_NEXT(s, entry); \
2281 if ((TIME_LEQ((s)->exp, time_uptime) && extra) || \
2283 dyn_match_ ## af ## _state(ch, s, rt))) { \
2285 CK_SLIST_REMOVE_AFTER(prev, entry); \
2287 CK_SLIST_REMOVE_HEAD( \
2288 &V_dyn_ ## name [bucket], entry); \
2290 SLIST_INSERT_HEAD(&expired_ ## af, s, expired); \
2291 if (s->type == O_LIMIT_PARENT) \
2292 DYN_COUNT_DEC(dyn_parent_count); \
2294 DYN_COUNT_DEC(dyn_count); \
2295 if (s->data->flags & DYN_REFERENCED) { \
2296 rule = s->data->parent; \
2297 if (s->type == O_LIMIT) \
2298 rule = ((__typeof(s)) \
2299 rule)->limit->parent;\
2300 dyn_release_rule(ch, s->data, \
2303 if (s->type == O_LIMIT) { \
2304 s = s->data->parent; \
2305 DPARENT_COUNT_DEC(s->limit); \
2315 DYN_BUCKET_VERSION_BUMP(bucket, name ## _del); \
2316 if (length > max_length) \
2317 max_length = length; \
2320 SLIST_INIT(&expired_ipv4);
2322 SLIST_INIT(&expired_ipv6);
2325 for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2326 DYN_BUCKET_LOCK(bucket);
2327 DYN_UNLINK_STATES(s4, s4p, s4n, data->expire, ipv4, ipv4, 1);
2328 DYN_UNLINK_STATES(s4, s4p, s4n, limit->expire, ipv4,
2329 ipv4_parent, (s4->limit->count == 0));
2331 DYN_UNLINK_STATES(s6, s6p, s6n, data->expire, ipv6, ipv6, 1);
2332 DYN_UNLINK_STATES(s6, s6p, s6n, limit->expire, ipv6,
2333 ipv6_parent, (s6->limit->count == 0));
2335 DYN_BUCKET_UNLOCK(bucket);
2337 /* Update curr_max_length for statistics. */
2338 V_curr_max_length = max_length;
2340 * Concatenate temporary lists with global expired lists.
2343 SLIST_CONCAT(&V_dyn_expired_ipv4, &expired_ipv4,
2344 dyn_ipv4_state, expired);
2346 SLIST_CONCAT(&V_dyn_expired_ipv6, &expired_ipv6,
2347 dyn_ipv6_state, expired);
2349 DYN_EXPIRED_UNLOCK();
2350 #undef DYN_UNLINK_STATES
2351 #undef DYN_UNREF_STATES
2354 static struct mbuf *
2355 dyn_mgethdr(int len, uint16_t fibnum)
2359 m = m_gethdr(M_NOWAIT, MT_DATA);
2363 mac_netinet_firewall_send(m);
2365 M_SETFIB(m, fibnum);
2366 m->m_data += max_linkhdr;
2367 m->m_flags |= M_SKIP_FIREWALL;
2368 m->m_len = m->m_pkthdr.len = len;
2369 bzero(m->m_data, len);
2374 dyn_make_keepalive_ipv4(struct mbuf *m, in_addr_t src, in_addr_t dst,
2375 uint32_t seq, uint32_t ack, uint16_t sport, uint16_t dport)
2380 ip = mtod(m, struct ip *);
2382 ip->ip_hl = sizeof(*ip) >> 2;
2383 ip->ip_tos = IPTOS_LOWDELAY;
2384 ip->ip_len = htons(m->m_len);
2385 ip->ip_off |= htons(IP_DF);
2386 ip->ip_ttl = V_ip_defttl;
2387 ip->ip_p = IPPROTO_TCP;
2388 ip->ip_src.s_addr = htonl(src);
2389 ip->ip_dst.s_addr = htonl(dst);
2391 tcp = mtodo(m, sizeof(struct ip));
2392 tcp->th_sport = htons(sport);
2393 tcp->th_dport = htons(dport);
2394 tcp->th_off = sizeof(struct tcphdr) >> 2;
2395 tcp->th_seq = htonl(seq);
2396 tcp->th_ack = htonl(ack);
2397 tcp->th_flags = TH_ACK;
2398 tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
2399 htons(sizeof(struct tcphdr) + IPPROTO_TCP));
2401 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2402 m->m_pkthdr.csum_flags = CSUM_TCP;
2406 dyn_enqueue_keepalive_ipv4(struct mbufq *q, const struct dyn_ipv4_state *s)
2410 if ((s->data->state & ACK_FWD) == 0 && s->data->ack_fwd > 0) {
2411 m = dyn_mgethdr(sizeof(struct ip) + sizeof(struct tcphdr),
2414 dyn_make_keepalive_ipv4(m, s->dst, s->src,
2415 s->data->ack_fwd - 1, s->data->ack_rev,
2416 s->dport, s->sport);
2417 if (mbufq_enqueue(q, m)) {
2419 log(LOG_DEBUG, "ipfw: limit for IPv4 "
2420 "keepalive queue is reached.\n");
2426 if ((s->data->state & ACK_REV) == 0 && s->data->ack_rev > 0) {
2427 m = dyn_mgethdr(sizeof(struct ip) + sizeof(struct tcphdr),
2430 dyn_make_keepalive_ipv4(m, s->src, s->dst,
2431 s->data->ack_rev - 1, s->data->ack_fwd,
2432 s->sport, s->dport);
2433 if (mbufq_enqueue(q, m)) {
2435 log(LOG_DEBUG, "ipfw: limit for IPv4 "
2436 "keepalive queue is reached.\n");
2444 * Prepare and send keep-alive packets.
2447 dyn_send_keepalive_ipv4(struct ip_fw_chain *chain)
2451 struct dyn_ipv4_state *s;
2454 mbufq_init(&q, INT_MAX);
2455 IPFW_UH_RLOCK(chain);
2457 * It is safe to not use hazard pointer and just do lockless
2458 * access to the lists, because states entries can not be deleted
2459 * while we hold IPFW_UH_RLOCK.
2461 for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2462 CK_SLIST_FOREACH(s, &V_dyn_ipv4[bucket], entry) {
2464 * Only established TCP connections that will
2465 * become expired withing dyn_keepalive_interval.
2467 if (s->proto != IPPROTO_TCP ||
2468 (s->data->state & BOTH_SYN) != BOTH_SYN ||
2469 TIME_LEQ(time_uptime + V_dyn_keepalive_interval,
2472 dyn_enqueue_keepalive_ipv4(&q, s);
2475 IPFW_UH_RUNLOCK(chain);
2476 while ((m = mbufq_dequeue(&q)) != NULL)
2477 ip_output(m, NULL, NULL, 0, NULL, NULL);
2482 dyn_make_keepalive_ipv6(struct mbuf *m, const struct in6_addr *src,
2483 const struct in6_addr *dst, uint32_t zoneid, uint32_t seq, uint32_t ack,
2484 uint16_t sport, uint16_t dport)
2487 struct ip6_hdr *ip6;
2489 ip6 = mtod(m, struct ip6_hdr *);
2490 ip6->ip6_vfc |= IPV6_VERSION;
2491 ip6->ip6_plen = htons(sizeof(struct tcphdr));
2492 ip6->ip6_nxt = IPPROTO_TCP;
2493 ip6->ip6_hlim = IPV6_DEFHLIM;
2494 ip6->ip6_src = *src;
2495 if (IN6_IS_ADDR_LINKLOCAL(src))
2496 ip6->ip6_src.s6_addr16[1] = htons(zoneid & 0xffff);
2497 ip6->ip6_dst = *dst;
2498 if (IN6_IS_ADDR_LINKLOCAL(dst))
2499 ip6->ip6_dst.s6_addr16[1] = htons(zoneid & 0xffff);
2501 tcp = mtodo(m, sizeof(struct ip6_hdr));
2502 tcp->th_sport = htons(sport);
2503 tcp->th_dport = htons(dport);
2504 tcp->th_off = sizeof(struct tcphdr) >> 2;
2505 tcp->th_seq = htonl(seq);
2506 tcp->th_ack = htonl(ack);
2507 tcp->th_flags = TH_ACK;
2508 tcp->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr),
2511 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2512 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
2516 dyn_enqueue_keepalive_ipv6(struct mbufq *q, const struct dyn_ipv6_state *s)
2520 if ((s->data->state & ACK_FWD) == 0 && s->data->ack_fwd > 0) {
2521 m = dyn_mgethdr(sizeof(struct ip6_hdr) +
2522 sizeof(struct tcphdr), s->data->fibnum);
2524 dyn_make_keepalive_ipv6(m, &s->dst, &s->src,
2525 s->zoneid, s->data->ack_fwd - 1, s->data->ack_rev,
2526 s->dport, s->sport);
2527 if (mbufq_enqueue(q, m)) {
2529 log(LOG_DEBUG, "ipfw: limit for IPv6 "
2530 "keepalive queue is reached.\n");
2536 if ((s->data->state & ACK_REV) == 0 && s->data->ack_rev > 0) {
2537 m = dyn_mgethdr(sizeof(struct ip6_hdr) +
2538 sizeof(struct tcphdr), s->data->fibnum);
2540 dyn_make_keepalive_ipv6(m, &s->src, &s->dst,
2541 s->zoneid, s->data->ack_rev - 1, s->data->ack_fwd,
2542 s->sport, s->dport);
2543 if (mbufq_enqueue(q, m)) {
2545 log(LOG_DEBUG, "ipfw: limit for IPv6 "
2546 "keepalive queue is reached.\n");
2554 dyn_send_keepalive_ipv6(struct ip_fw_chain *chain)
2558 struct dyn_ipv6_state *s;
2561 mbufq_init(&q, INT_MAX);
2562 IPFW_UH_RLOCK(chain);
2564 * It is safe to not use hazard pointer and just do lockless
2565 * access to the lists, because states entries can not be deleted
2566 * while we hold IPFW_UH_RLOCK.
2568 for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2569 CK_SLIST_FOREACH(s, &V_dyn_ipv6[bucket], entry) {
2571 * Only established TCP connections that will
2572 * become expired withing dyn_keepalive_interval.
2574 if (s->proto != IPPROTO_TCP ||
2575 (s->data->state & BOTH_SYN) != BOTH_SYN ||
2576 TIME_LEQ(time_uptime + V_dyn_keepalive_interval,
2579 dyn_enqueue_keepalive_ipv6(&q, s);
2582 IPFW_UH_RUNLOCK(chain);
2583 while ((m = mbufq_dequeue(&q)) != NULL)
2584 ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
2589 dyn_grow_hashtable(struct ip_fw_chain *chain, uint32_t new)
2592 struct dyn_ipv6ck_slist *ipv6, *ipv6_parent;
2593 uint32_t *ipv6_add, *ipv6_del, *ipv6_parent_add, *ipv6_parent_del;
2594 struct dyn_ipv6_state *s6;
2596 struct dyn_ipv4ck_slist *ipv4, *ipv4_parent;
2597 uint32_t *ipv4_add, *ipv4_del, *ipv4_parent_add, *ipv4_parent_del;
2598 struct dyn_ipv4_state *s4;
2599 struct mtx *bucket_lock;
2603 MPASS(powerof2(new));
2604 DYN_DEBUG("grow hash size %u -> %u", V_curr_dyn_buckets, new);
2606 * Allocate and initialize new lists.
2607 * XXXAE: on memory pressure this can disable callout timer.
2609 bucket_lock = malloc(new * sizeof(struct mtx), M_IPFW,
2611 ipv4 = malloc(new * sizeof(struct dyn_ipv4ck_slist), M_IPFW,
2613 ipv4_parent = malloc(new * sizeof(struct dyn_ipv4ck_slist), M_IPFW,
2615 ipv4_add = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO);
2616 ipv4_del = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO);
2617 ipv4_parent_add = malloc(new * sizeof(uint32_t), M_IPFW,
2619 ipv4_parent_del = malloc(new * sizeof(uint32_t), M_IPFW,
2622 ipv6 = malloc(new * sizeof(struct dyn_ipv6ck_slist), M_IPFW,
2624 ipv6_parent = malloc(new * sizeof(struct dyn_ipv6ck_slist), M_IPFW,
2626 ipv6_add = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO);
2627 ipv6_del = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO);
2628 ipv6_parent_add = malloc(new * sizeof(uint32_t), M_IPFW,
2630 ipv6_parent_del = malloc(new * sizeof(uint32_t), M_IPFW,
2633 for (bucket = 0; bucket < new; bucket++) {
2634 DYN_BUCKET_LOCK_INIT(bucket_lock, bucket);
2635 CK_SLIST_INIT(&ipv4[bucket]);
2636 CK_SLIST_INIT(&ipv4_parent[bucket]);
2638 CK_SLIST_INIT(&ipv6[bucket]);
2639 CK_SLIST_INIT(&ipv6_parent[bucket]);
2643 #define DYN_RELINK_STATES(s, hval, i, head, ohead) do { \
2644 while ((s = CK_SLIST_FIRST(&V_dyn_ ## ohead[i])) != NULL) { \
2645 CK_SLIST_REMOVE_HEAD(&V_dyn_ ## ohead[i], entry); \
2646 CK_SLIST_INSERT_HEAD(&head[DYN_BUCKET(s->hval, new)], \
2651 * Prevent rules changing from userland.
2653 IPFW_UH_WLOCK(chain);
2655 * Hold traffic processing until we finish resize to
2656 * prevent access to states lists.
2659 /* Re-link all dynamic states */
2660 for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2661 DYN_RELINK_STATES(s4, data->hashval, bucket, ipv4, ipv4);
2662 DYN_RELINK_STATES(s4, limit->hashval, bucket, ipv4_parent,
2665 DYN_RELINK_STATES(s6, data->hashval, bucket, ipv6, ipv6);
2666 DYN_RELINK_STATES(s6, limit->hashval, bucket, ipv6_parent,
2671 #define DYN_SWAP_PTR(old, new, tmp) do { \
2677 DYN_SWAP_PTR(V_dyn_bucket_lock, bucket_lock, tmp);
2678 DYN_SWAP_PTR(V_dyn_ipv4, ipv4, tmp);
2679 DYN_SWAP_PTR(V_dyn_ipv4_parent, ipv4_parent, tmp);
2680 DYN_SWAP_PTR(V_dyn_ipv4_add, ipv4_add, tmp);
2681 DYN_SWAP_PTR(V_dyn_ipv4_parent_add, ipv4_parent_add, tmp);
2682 DYN_SWAP_PTR(V_dyn_ipv4_del, ipv4_del, tmp);
2683 DYN_SWAP_PTR(V_dyn_ipv4_parent_del, ipv4_parent_del, tmp);
2686 DYN_SWAP_PTR(V_dyn_ipv6, ipv6, tmp);
2687 DYN_SWAP_PTR(V_dyn_ipv6_parent, ipv6_parent, tmp);
2688 DYN_SWAP_PTR(V_dyn_ipv6_add, ipv6_add, tmp);
2689 DYN_SWAP_PTR(V_dyn_ipv6_parent_add, ipv6_parent_add, tmp);
2690 DYN_SWAP_PTR(V_dyn_ipv6_del, ipv6_del, tmp);
2691 DYN_SWAP_PTR(V_dyn_ipv6_parent_del, ipv6_parent_del, tmp);
2693 bucket = V_curr_dyn_buckets;
2694 V_curr_dyn_buckets = new;
2696 IPFW_WUNLOCK(chain);
2697 IPFW_UH_WUNLOCK(chain);
2699 /* Release old resources */
2700 while (bucket-- != 0)
2701 DYN_BUCKET_LOCK_DESTROY(bucket_lock, bucket);
2702 free(bucket_lock, M_IPFW);
2704 free(ipv4_parent, M_IPFW);
2705 free(ipv4_add, M_IPFW);
2706 free(ipv4_parent_add, M_IPFW);
2707 free(ipv4_del, M_IPFW);
2708 free(ipv4_parent_del, M_IPFW);
2711 free(ipv6_parent, M_IPFW);
2712 free(ipv6_add, M_IPFW);
2713 free(ipv6_parent_add, M_IPFW);
2714 free(ipv6_del, M_IPFW);
2715 free(ipv6_parent_del, M_IPFW);
2720 * This function is used to perform various maintenance
2721 * on dynamic hash lists. Currently it is called every second.
2724 dyn_tick(void *vnetx)
2728 CURVNET_SET((struct vnet *)vnetx);
2730 * First free states unlinked in previous passes.
2732 dyn_free_states(&V_layer3_chain);
2734 * Now unlink others expired states.
2735 * We use IPFW_UH_WLOCK to avoid concurrent call of
2736 * dyn_expire_states(). It is the only function that does
2737 * deletion of state entries from states lists.
2739 IPFW_UH_WLOCK(&V_layer3_chain);
2740 dyn_expire_states(&V_layer3_chain, NULL);
2741 IPFW_UH_WUNLOCK(&V_layer3_chain);
2743 * Send keepalives if they are enabled and the time has come.
2745 if (V_dyn_keepalive != 0 &&
2746 V_dyn_keepalive_last + V_dyn_keepalive_period <= time_uptime) {
2747 V_dyn_keepalive_last = time_uptime;
2748 dyn_send_keepalive_ipv4(&V_layer3_chain);
2750 dyn_send_keepalive_ipv6(&V_layer3_chain);
2754 * Check if we need to resize the hash:
2755 * if current number of states exceeds number of buckets in hash,
2756 * and dyn_buckets_max permits to grow the number of buckets, then
2757 * do it. Grow hash size to the minimum power of 2 which is bigger
2758 * than current states count.
2760 if (V_curr_dyn_buckets < V_dyn_buckets_max &&
2761 (V_curr_dyn_buckets < V_dyn_count / 2 || (
2762 V_curr_dyn_buckets < V_dyn_count && V_curr_max_length > 8))) {
2763 buckets = 1 << fls(V_dyn_count);
2764 if (buckets > V_dyn_buckets_max)
2765 buckets = V_dyn_buckets_max;
2766 dyn_grow_hashtable(&V_layer3_chain, buckets);
2769 callout_reset_on(&V_dyn_timeout, hz, dyn_tick, vnetx, 0);
2774 ipfw_expire_dyn_states(struct ip_fw_chain *chain, ipfw_range_tlv *rt)
2777 * Do not perform any checks if we currently have no dynamic states
2779 if (V_dyn_count == 0)
2782 IPFW_UH_WLOCK_ASSERT(chain);
2783 dyn_expire_states(chain, rt);
2787 * Pass through all states and reset eaction for orphaned rules.
2790 ipfw_dyn_reset_eaction(struct ip_fw_chain *ch, uint16_t eaction_id,
2791 uint16_t default_id, uint16_t instance_id)
2794 struct dyn_ipv6_state *s6;
2796 struct dyn_ipv4_state *s4;
2800 #define DYN_RESET_EACTION(s, h, b) \
2801 CK_SLIST_FOREACH(s, &V_dyn_ ## h[b], entry) { \
2802 if ((s->data->flags & DYN_REFERENCED) == 0) \
2804 rule = s->data->parent; \
2805 if (s->type == O_LIMIT) \
2806 rule = ((__typeof(s))rule)->limit->parent; \
2807 ipfw_reset_eaction(ch, rule, eaction_id, \
2808 default_id, instance_id); \
2811 IPFW_UH_WLOCK_ASSERT(ch);
2812 if (V_dyn_count == 0)
2814 for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2815 DYN_RESET_EACTION(s4, ipv4, bucket);
2817 DYN_RESET_EACTION(s6, ipv6, bucket);
2823 * Returns size of dynamic states in legacy format
2829 return ((V_dyn_count + V_dyn_parent_count) * sizeof(ipfw_dyn_rule));
2833 * Returns number of dynamic states.
2834 * Marks every named object index used by dynamic states with bit in @bmask.
2835 * Returns number of named objects accounted in bmask via @nocnt.
2836 * Used by dump format v1 (current).
2839 ipfw_dyn_get_count(uint32_t *bmask, int *nocnt)
2842 struct dyn_ipv6_state *s6;
2844 struct dyn_ipv4_state *s4;
2847 #define DYN_COUNT_OBJECTS(s, h, b) \
2848 CK_SLIST_FOREACH(s, &V_dyn_ ## h[b], entry) { \
2849 MPASS(s->kidx != 0); \
2850 if (ipfw_mark_object_kidx(bmask, IPFW_TLV_STATE_NAME, \
2855 IPFW_UH_RLOCK_ASSERT(&V_layer3_chain);
2857 /* No need to pass through all the buckets. */
2859 if (V_dyn_count + V_dyn_parent_count == 0)
2862 for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2863 DYN_COUNT_OBJECTS(s4, ipv4, bucket);
2865 DYN_COUNT_OBJECTS(s6, ipv6, bucket);
2869 return (V_dyn_count + V_dyn_parent_count);
2873 * Check if rule contains at least one dynamic opcode.
2875 * Returns 1 if such opcode is found, 0 otherwise.
2878 ipfw_is_dyn_rule(struct ip_fw *rule)
2886 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
2887 cmdlen = F_LEN(cmd);
2889 switch (cmd->opcode) {
2902 dyn_export_parent(const struct dyn_parent *p, uint16_t kidx,
2906 dst->dyn_type = O_LIMIT_PARENT;
2908 dst->count = (uint16_t)DPARENT_COUNT(p);
2909 dst->expire = TIME_LEQ(p->expire, time_uptime) ? 0:
2910 p->expire - time_uptime;
2912 /* 'rule' is used to pass up the rule number and set */
2913 memcpy(&dst->rule, &p->rulenum, sizeof(p->rulenum));
2914 /* store set number into high word of dst->rule pointer. */
2915 memcpy((char *)&dst->rule + sizeof(p->rulenum), &p->set,
2925 dst->bucket = p->hashval;
2927 * The legacy userland code will interpret a NULL here as a marker
2928 * for the last dynamic rule.
2930 dst->next = (ipfw_dyn_rule *)1;
2934 dyn_export_data(const struct dyn_data *data, uint16_t kidx, uint8_t type,
2938 dst->dyn_type = type;
2940 dst->pcnt = data->pcnt_fwd + data->pcnt_rev;
2941 dst->bcnt = data->bcnt_fwd + data->bcnt_rev;
2942 dst->expire = TIME_LEQ(data->expire, time_uptime) ? 0:
2943 data->expire - time_uptime;
2945 /* 'rule' is used to pass up the rule number and set */
2946 memcpy(&dst->rule, &data->rulenum, sizeof(data->rulenum));
2947 /* store set number into high word of dst->rule pointer. */
2948 memcpy((char *)&dst->rule + sizeof(data->rulenum), &data->set,
2951 dst->state = data->state;
2952 if (data->flags & DYN_REFERENCED)
2953 dst->state |= IPFW_DYN_ORPHANED;
2957 dst->ack_fwd = data->ack_fwd;
2958 dst->ack_rev = data->ack_rev;
2960 dst->bucket = data->hashval;
2962 * The legacy userland code will interpret a NULL here as a marker
2963 * for the last dynamic rule.
2965 dst->next = (ipfw_dyn_rule *)1;
2969 dyn_export_ipv4_state(const struct dyn_ipv4_state *s, ipfw_dyn_rule *dst)
2973 case O_LIMIT_PARENT:
2974 dyn_export_parent(s->limit, s->kidx, dst);
2977 dyn_export_data(s->data, s->kidx, s->type, dst);
2980 dst->id.dst_ip = s->dst;
2981 dst->id.src_ip = s->src;
2982 dst->id.dst_port = s->dport;
2983 dst->id.src_port = s->sport;
2984 dst->id.fib = s->data->fibnum;
2985 dst->id.proto = s->proto;
2987 dst->id.addr_type = 4;
2989 memset(&dst->id.dst_ip6, 0, sizeof(dst->id.dst_ip6));
2990 memset(&dst->id.src_ip6, 0, sizeof(dst->id.src_ip6));
2991 dst->id.flow_id6 = dst->id.extra = 0;
2996 dyn_export_ipv6_state(const struct dyn_ipv6_state *s, ipfw_dyn_rule *dst)
3000 case O_LIMIT_PARENT:
3001 dyn_export_parent(s->limit, s->kidx, dst);
3004 dyn_export_data(s->data, s->kidx, s->type, dst);
3007 dst->id.src_ip6 = s->src;
3008 dst->id.dst_ip6 = s->dst;
3009 dst->id.dst_port = s->dport;
3010 dst->id.src_port = s->sport;
3011 dst->id.fib = s->data->fibnum;
3012 dst->id.proto = s->proto;
3014 dst->id.addr_type = 6;
3016 dst->id.dst_ip = dst->id.src_ip = 0;
3017 dst->id.flow_id6 = dst->id.extra = 0;
3022 * Fills the buffer given by @sd with dynamic states.
3023 * Used by dump format v1 (current).
3025 * Returns 0 on success.
3028 ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd)
3031 struct dyn_ipv6_state *s6;
3033 struct dyn_ipv4_state *s4;
3034 ipfw_obj_dyntlv *dst, *last;
3035 ipfw_obj_ctlv *ctlv;
3038 if (V_dyn_count == 0)
3042 * IPFW_UH_RLOCK garantees that another userland request
3043 * and callout thread will not delete entries from states
3046 IPFW_UH_RLOCK_ASSERT(chain);
3048 ctlv = (ipfw_obj_ctlv *)ipfw_get_sopt_space(sd, sizeof(*ctlv));
3051 ctlv->head.type = IPFW_TLV_DYNSTATE_LIST;
3052 ctlv->objsize = sizeof(ipfw_obj_dyntlv);
3055 #define DYN_EXPORT_STATES(s, af, h, b) \
3056 CK_SLIST_FOREACH(s, &V_dyn_ ## h[b], entry) { \
3057 dst = (ipfw_obj_dyntlv *)ipfw_get_sopt_space(sd, \
3058 sizeof(ipfw_obj_dyntlv)); \
3061 dyn_export_ ## af ## _state(s, &dst->state); \
3062 dst->head.length = sizeof(ipfw_obj_dyntlv); \
3063 dst->head.type = IPFW_TLV_DYN_ENT; \
3067 for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
3068 DYN_EXPORT_STATES(s4, ipv4, ipv4_parent, bucket);
3069 DYN_EXPORT_STATES(s4, ipv4, ipv4, bucket);
3071 DYN_EXPORT_STATES(s6, ipv6, ipv6_parent, bucket);
3072 DYN_EXPORT_STATES(s6, ipv6, ipv6, bucket);
3076 /* mark last dynamic rule */
3078 last->head.flags = IPFW_DF_LAST; /* XXX: unused */
3080 #undef DYN_EXPORT_STATES
3084 * Fill given buffer with dynamic states (legacy format).
3085 * IPFW_UH_RLOCK has to be held while calling.
3088 ipfw_get_dynamic(struct ip_fw_chain *chain, char **pbp, const char *ep)
3091 struct dyn_ipv6_state *s6;
3093 struct dyn_ipv4_state *s4;
3094 ipfw_dyn_rule *p, *last = NULL;
3098 if (V_dyn_count == 0)
3102 IPFW_UH_RLOCK_ASSERT(chain);
3104 #define DYN_EXPORT_STATES(s, af, head, b) \
3105 CK_SLIST_FOREACH(s, &V_dyn_ ## head[b], entry) { \
3106 if (bp + sizeof(*p) > ep) \
3108 p = (ipfw_dyn_rule *)bp; \
3109 dyn_export_ ## af ## _state(s, p); \
3114 for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
3115 DYN_EXPORT_STATES(s4, ipv4, ipv4_parent, bucket);
3116 DYN_EXPORT_STATES(s4, ipv4, ipv4, bucket);
3118 DYN_EXPORT_STATES(s6, ipv6, ipv6_parent, bucket);
3119 DYN_EXPORT_STATES(s6, ipv6, ipv6, bucket);
3123 if (last != NULL) /* mark last dynamic rule */
3126 #undef DYN_EXPORT_STATES
3130 ipfw_dyn_init(struct ip_fw_chain *chain)
3133 #ifdef IPFIREWALL_JENKINSHASH
3134 V_dyn_hashseed = arc4random();
3136 V_dyn_max = 16384; /* max # of states */
3137 V_dyn_parent_max = 4096; /* max # of parent states */
3138 V_dyn_buckets_max = 8192; /* must be power of 2 */
3140 V_dyn_ack_lifetime = 300;
3141 V_dyn_syn_lifetime = 20;
3142 V_dyn_fin_lifetime = 1;
3143 V_dyn_rst_lifetime = 1;
3144 V_dyn_udp_lifetime = 10;
3145 V_dyn_short_lifetime = 5;
3147 V_dyn_keepalive_interval = 20;
3148 V_dyn_keepalive_period = 5;
3149 V_dyn_keepalive = 1; /* send keepalives */
3150 V_dyn_keepalive_last = time_uptime;
3152 V_dyn_data_zone = uma_zcreate("IPFW dynamic states data",
3153 sizeof(struct dyn_data), NULL, NULL, NULL, NULL,
3155 uma_zone_set_max(V_dyn_data_zone, V_dyn_max);
3157 V_dyn_parent_zone = uma_zcreate("IPFW parent dynamic states",
3158 sizeof(struct dyn_parent), NULL, NULL, NULL, NULL,
3160 uma_zone_set_max(V_dyn_parent_zone, V_dyn_parent_max);
3162 SLIST_INIT(&V_dyn_expired_ipv4);
3164 V_dyn_ipv4_parent = NULL;
3165 V_dyn_ipv4_zone = uma_zcreate("IPFW IPv4 dynamic states",
3166 sizeof(struct dyn_ipv4_state), NULL, NULL, NULL, NULL,
3170 SLIST_INIT(&V_dyn_expired_ipv6);
3172 V_dyn_ipv6_parent = NULL;
3173 V_dyn_ipv6_zone = uma_zcreate("IPFW IPv6 dynamic states",
3174 sizeof(struct dyn_ipv6_state), NULL, NULL, NULL, NULL,
3178 /* Initialize buckets. */
3179 V_curr_dyn_buckets = 0;
3180 V_dyn_bucket_lock = NULL;
3181 dyn_grow_hashtable(chain, 256);
3183 if (IS_DEFAULT_VNET(curvnet))
3184 dyn_hp_cache = malloc(mp_ncpus * sizeof(void *), M_IPFW,
3187 DYN_EXPIRED_LOCK_INIT();
3188 callout_init(&V_dyn_timeout, 1);
3189 callout_reset(&V_dyn_timeout, hz, dyn_tick, curvnet);
3190 IPFW_ADD_OBJ_REWRITER(IS_DEFAULT_VNET(curvnet), dyn_opcodes);
3194 ipfw_dyn_uninit(int pass)
3197 struct dyn_ipv6_state *s6;
3199 struct dyn_ipv4_state *s4;
3203 callout_drain(&V_dyn_timeout);
3206 IPFW_DEL_OBJ_REWRITER(IS_DEFAULT_VNET(curvnet), dyn_opcodes);
3207 DYN_EXPIRED_LOCK_DESTROY();
3209 #define DYN_FREE_STATES_FORCED(CK, s, af, name, en) do { \
3210 while ((s = CK ## SLIST_FIRST(&V_dyn_ ## name)) != NULL) { \
3211 CK ## SLIST_REMOVE_HEAD(&V_dyn_ ## name, en); \
3212 if (s->type == O_LIMIT_PARENT) \
3213 uma_zfree(V_dyn_parent_zone, s->limit); \
3215 uma_zfree(V_dyn_data_zone, s->data); \
3216 uma_zfree(V_dyn_ ## af ## _zone, s); \
3219 for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
3220 DYN_BUCKET_LOCK_DESTROY(V_dyn_bucket_lock, bucket);
3222 DYN_FREE_STATES_FORCED(CK_, s4, ipv4, ipv4[bucket], entry);
3223 DYN_FREE_STATES_FORCED(CK_, s4, ipv4, ipv4_parent[bucket],
3226 DYN_FREE_STATES_FORCED(CK_, s6, ipv6, ipv6[bucket], entry);
3227 DYN_FREE_STATES_FORCED(CK_, s6, ipv6, ipv6_parent[bucket],
3231 DYN_FREE_STATES_FORCED(, s4, ipv4, expired_ipv4, expired);
3233 DYN_FREE_STATES_FORCED(, s6, ipv6, expired_ipv6, expired);
3235 #undef DYN_FREE_STATES_FORCED
3237 uma_zdestroy(V_dyn_ipv4_zone);
3238 uma_zdestroy(V_dyn_data_zone);
3239 uma_zdestroy(V_dyn_parent_zone);
3241 uma_zdestroy(V_dyn_ipv6_zone);
3242 free(V_dyn_ipv6, M_IPFW);
3243 free(V_dyn_ipv6_parent, M_IPFW);
3244 free(V_dyn_ipv6_add, M_IPFW);
3245 free(V_dyn_ipv6_parent_add, M_IPFW);
3246 free(V_dyn_ipv6_del, M_IPFW);
3247 free(V_dyn_ipv6_parent_del, M_IPFW);
3249 free(V_dyn_bucket_lock, M_IPFW);
3250 free(V_dyn_ipv4, M_IPFW);
3251 free(V_dyn_ipv4_parent, M_IPFW);
3252 free(V_dyn_ipv4_add, M_IPFW);
3253 free(V_dyn_ipv4_parent_add, M_IPFW);
3254 free(V_dyn_ipv4_del, M_IPFW);
3255 free(V_dyn_ipv4_parent_del, M_IPFW);
3256 if (IS_DEFAULT_VNET(curvnet))
3257 free(dyn_hp_cache, M_IPFW);