xref: /freebsd/sys/netpfil/ipfw/ip_fw_dynamic.c (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
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
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_ipfw.h"
36 #ifndef INET
37 #error IPFIREWALL requires INET.
38 #endif /* INET */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/hash.h>
43 #include <sys/mbuf.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/pcpu.h>
47 #include <sys/queue.h>
48 #include <sys/rmlock.h>
49 #include <sys/smp.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 #include <sys/syslog.h>
53 #include <net/ethernet.h>
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/vnet.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/ip_fw.h>
62 #include <netinet/tcp_var.h>
63 #include <netinet/udp.h>
64 
65 #include <netinet/ip6.h>	/* IN6_ARE_ADDR_EQUAL */
66 #ifdef INET6
67 #include <netinet6/in6_var.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/scope6_var.h>
70 #endif
71 
72 #include <netpfil/ipfw/ip_fw_private.h>
73 
74 #include <machine/in_cksum.h>	/* XXX for in_cksum */
75 
76 #ifdef MAC
77 #include <security/mac/mac_framework.h>
78 #endif
79 
80 /*
81  * Description of dynamic states.
82  *
83  * Dynamic states are stored in lists accessed through a hash tables
84  * whose size is curr_dyn_buckets. This value can be modified through
85  * the sysctl variable dyn_buckets.
86  *
87  * Currently there are four tables: dyn_ipv4, dyn_ipv6, dyn_ipv4_parent,
88  * and dyn_ipv6_parent.
89  *
90  * When a packet is received, its address fields hashed, then matched
91  * against the entries in the corresponding list by addr_type.
92  * Dynamic states can be used for different purposes:
93  *  + stateful rules;
94  *  + enforcing limits on the number of sessions;
95  *  + in-kernel NAT (not implemented yet)
96  *
97  * The lifetime of dynamic states is regulated by dyn_*_lifetime,
98  * measured in seconds and depending on the flags.
99  *
100  * The total number of dynamic states is equal to UMA zone items count.
101  * The max number of dynamic states is dyn_max. When we reach
102  * the maximum number of rules we do not create anymore. This is
103  * done to avoid consuming too much memory, but also too much
104  * time when searching on each packet (ideally, we should try instead
105  * to put a limit on the length of the list on each bucket...).
106  *
107  * Each state holds a pointer to the parent ipfw rule so we know what
108  * action to perform. Dynamic rules are removed when the parent rule is
109  * deleted.
110  *
111  * There are some limitations with dynamic rules -- we do not
112  * obey the 'randomized match', and we do not do multiple
113  * passes through the firewall. XXX check the latter!!!
114  */
115 
116 /* By default use jenkins hash function */
117 #define	IPFIREWALL_JENKINSHASH
118 
119 #define	DYN_COUNTER_INC(d, dir, pktlen)	do {	\
120 	(d)->pcnt_ ## dir++;			\
121 	(d)->bcnt_ ## dir += pktlen;		\
122 	} while (0)
123 
124 #define	DYN_REFERENCED		0x01
125 /*
126  * DYN_REFERENCED flag is used to show that state keeps reference to named
127  * object, and this reference should be released when state becomes expired.
128  */
129 
130 struct dyn_data {
131 	void		*parent;	/* pointer to parent rule */
132 	uint32_t	chain_id;	/* cached ruleset id */
133 	uint32_t	f_pos;		/* cached rule index */
134 
135 	uint32_t	hashval;	/* hash value used for hash resize */
136 	uint16_t	fibnum;		/* fib used to send keepalives */
137 	uint8_t		_pad[3];
138 	uint8_t		flags;		/* internal flags */
139 	uint16_t	rulenum;	/* parent rule number */
140 	uint32_t	ruleid;		/* parent rule id */
141 
142 	uint32_t	state;		/* TCP session state and flags */
143 	uint32_t	ack_fwd;	/* most recent ACKs in forward */
144 	uint32_t	ack_rev;	/* and reverse direction (used */
145 					/* to generate keepalives) */
146 	uint32_t	sync;		/* synchronization time */
147 	uint32_t	expire;		/* expire time */
148 
149 	uint64_t	pcnt_fwd;	/* bytes counter in forward */
150 	uint64_t	bcnt_fwd;	/* packets counter in forward */
151 	uint64_t	pcnt_rev;	/* bytes counter in reverse */
152 	uint64_t	bcnt_rev;	/* packets counter in reverse */
153 };
154 
155 #define	DPARENT_COUNT_DEC(p)	do {			\
156 	MPASS(p->count > 0);				\
157 	ck_pr_dec_32(&(p)->count);			\
158 } while (0)
159 #define	DPARENT_COUNT_INC(p)	ck_pr_inc_32(&(p)->count)
160 #define	DPARENT_COUNT(p)	ck_pr_load_32(&(p)->count)
161 struct dyn_parent {
162 	void		*parent;	/* pointer to parent rule */
163 	uint32_t	count;		/* number of linked states */
164 	uint8_t		_pad[2];
165 	uint16_t	rulenum;	/* parent rule number */
166 	uint32_t	ruleid;		/* parent rule id */
167 	uint32_t	hashval;	/* hash value used for hash resize */
168 	uint32_t	expire;		/* expire time */
169 };
170 
171 struct dyn_ipv4_state {
172 	uint8_t		type;		/* State type */
173 	uint8_t		proto;		/* UL Protocol */
174 	uint16_t	kidx;		/* named object index */
175 	uint16_t	sport, dport;	/* ULP source and destination ports */
176 	in_addr_t	src, dst;	/* IPv4 source and destination */
177 
178 	union {
179 		struct dyn_data	*data;
180 		struct dyn_parent *limit;
181 	};
182 	CK_SLIST_ENTRY(dyn_ipv4_state)	entry;
183 	SLIST_ENTRY(dyn_ipv4_state)	expired;
184 };
185 CK_SLIST_HEAD(dyn_ipv4ck_slist, dyn_ipv4_state);
186 VNET_DEFINE_STATIC(struct dyn_ipv4ck_slist *, dyn_ipv4);
187 VNET_DEFINE_STATIC(struct dyn_ipv4ck_slist *, dyn_ipv4_parent);
188 
189 SLIST_HEAD(dyn_ipv4_slist, dyn_ipv4_state);
190 VNET_DEFINE_STATIC(struct dyn_ipv4_slist, dyn_expired_ipv4);
191 #define	V_dyn_ipv4			VNET(dyn_ipv4)
192 #define	V_dyn_ipv4_parent		VNET(dyn_ipv4_parent)
193 #define	V_dyn_expired_ipv4		VNET(dyn_expired_ipv4)
194 
195 #ifdef INET6
196 struct dyn_ipv6_state {
197 	uint8_t		type;		/* State type */
198 	uint8_t		proto;		/* UL Protocol */
199 	uint16_t	kidx;		/* named object index */
200 	uint16_t	sport, dport;	/* ULP source and destination ports */
201 	struct in6_addr	src, dst;	/* IPv6 source and destination */
202 	uint32_t	zoneid;		/* IPv6 scope zone id */
203 	union {
204 		struct dyn_data	*data;
205 		struct dyn_parent *limit;
206 	};
207 	CK_SLIST_ENTRY(dyn_ipv6_state)	entry;
208 	SLIST_ENTRY(dyn_ipv6_state)	expired;
209 };
210 CK_SLIST_HEAD(dyn_ipv6ck_slist, dyn_ipv6_state);
211 VNET_DEFINE_STATIC(struct dyn_ipv6ck_slist *, dyn_ipv6);
212 VNET_DEFINE_STATIC(struct dyn_ipv6ck_slist *, dyn_ipv6_parent);
213 
214 SLIST_HEAD(dyn_ipv6_slist, dyn_ipv6_state);
215 VNET_DEFINE_STATIC(struct dyn_ipv6_slist, dyn_expired_ipv6);
216 #define	V_dyn_ipv6			VNET(dyn_ipv6)
217 #define	V_dyn_ipv6_parent		VNET(dyn_ipv6_parent)
218 #define	V_dyn_expired_ipv6		VNET(dyn_expired_ipv6)
219 #endif /* INET6 */
220 
221 /*
222  * Per-CPU pointer indicates that specified state is currently in use
223  * and must not be reclaimed by expiration callout.
224  */
225 static void **dyn_hp_cache;
226 DPCPU_DEFINE_STATIC(void *, dyn_hp);
227 #define	DYNSTATE_GET(cpu)	ck_pr_load_ptr(DPCPU_ID_PTR((cpu), dyn_hp))
228 #define	DYNSTATE_PROTECT(v)	ck_pr_store_ptr(DPCPU_PTR(dyn_hp), (v))
229 #define	DYNSTATE_RELEASE()	DYNSTATE_PROTECT(NULL)
230 #define	DYNSTATE_CRITICAL_ENTER()	critical_enter()
231 #define	DYNSTATE_CRITICAL_EXIT()	do {	\
232 	DYNSTATE_RELEASE();			\
233 	critical_exit();			\
234 } while (0);
235 
236 /*
237  * We keep two version numbers, one is updated when new entry added to
238  * the list. Second is updated when an entry deleted from the list.
239  * Versions are updated under bucket lock.
240  *
241  * Bucket "add" version number is used to know, that in the time between
242  * state lookup (i.e. ipfw_dyn_lookup_state()) and the followed state
243  * creation (i.e. ipfw_dyn_install_state()) another concurrent thread did
244  * not install some state in this bucket. Using this info we can avoid
245  * additional state lookup, because we are sure that we will not install
246  * the state twice.
247  *
248  * Also doing the tracking of bucket "del" version during lookup we can
249  * be sure, that state entry was not unlinked and freed in time between
250  * we read the state pointer and protect it with hazard pointer.
251  *
252  * An entry unlinked from CK list keeps unchanged until it is freed.
253  * Unlinked entries are linked into expired lists using "expired" field.
254  */
255 
256 /*
257  * dyn_expire_lock is used to protect access to dyn_expired_xxx lists.
258  * dyn_bucket_lock is used to get write access to lists in specific bucket.
259  * Currently one dyn_bucket_lock is used for all ipv4, ipv4_parent, ipv6,
260  * and ipv6_parent lists.
261  */
262 VNET_DEFINE_STATIC(struct mtx, dyn_expire_lock);
263 VNET_DEFINE_STATIC(struct mtx *, dyn_bucket_lock);
264 #define	V_dyn_expire_lock		VNET(dyn_expire_lock)
265 #define	V_dyn_bucket_lock		VNET(dyn_bucket_lock)
266 
267 /*
268  * Bucket's add/delete generation versions.
269  */
270 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv4_add);
271 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv4_del);
272 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv4_parent_add);
273 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv4_parent_del);
274 #define	V_dyn_ipv4_add			VNET(dyn_ipv4_add)
275 #define	V_dyn_ipv4_del			VNET(dyn_ipv4_del)
276 #define	V_dyn_ipv4_parent_add		VNET(dyn_ipv4_parent_add)
277 #define	V_dyn_ipv4_parent_del		VNET(dyn_ipv4_parent_del)
278 
279 #ifdef INET6
280 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv6_add);
281 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv6_del);
282 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv6_parent_add);
283 VNET_DEFINE_STATIC(uint32_t *, dyn_ipv6_parent_del);
284 #define	V_dyn_ipv6_add			VNET(dyn_ipv6_add)
285 #define	V_dyn_ipv6_del			VNET(dyn_ipv6_del)
286 #define	V_dyn_ipv6_parent_add		VNET(dyn_ipv6_parent_add)
287 #define	V_dyn_ipv6_parent_del		VNET(dyn_ipv6_parent_del)
288 #endif /* INET6 */
289 
290 #define	DYN_BUCKET(h, b)		((h) & (b - 1))
291 #define	DYN_BUCKET_VERSION(b, v)	ck_pr_load_32(&V_dyn_ ## v[(b)])
292 #define	DYN_BUCKET_VERSION_BUMP(b, v)	ck_pr_inc_32(&V_dyn_ ## v[(b)])
293 
294 #define	DYN_BUCKET_LOCK_INIT(lock, b)		\
295     mtx_init(&lock[(b)], "IPFW dynamic bucket", NULL, MTX_DEF)
296 #define	DYN_BUCKET_LOCK_DESTROY(lock, b)	mtx_destroy(&lock[(b)])
297 #define	DYN_BUCKET_LOCK(b)	mtx_lock(&V_dyn_bucket_lock[(b)])
298 #define	DYN_BUCKET_UNLOCK(b)	mtx_unlock(&V_dyn_bucket_lock[(b)])
299 #define	DYN_BUCKET_ASSERT(b)	mtx_assert(&V_dyn_bucket_lock[(b)], MA_OWNED)
300 
301 #define	DYN_EXPIRED_LOCK_INIT()		\
302     mtx_init(&V_dyn_expire_lock, "IPFW expired states list", NULL, MTX_DEF)
303 #define	DYN_EXPIRED_LOCK_DESTROY()	mtx_destroy(&V_dyn_expire_lock)
304 #define	DYN_EXPIRED_LOCK()		mtx_lock(&V_dyn_expire_lock)
305 #define	DYN_EXPIRED_UNLOCK()		mtx_unlock(&V_dyn_expire_lock)
306 
307 VNET_DEFINE_STATIC(uint32_t, dyn_buckets_max);
308 VNET_DEFINE_STATIC(uint32_t, curr_dyn_buckets);
309 VNET_DEFINE_STATIC(struct callout, dyn_timeout);
310 #define	V_dyn_buckets_max		VNET(dyn_buckets_max)
311 #define	V_curr_dyn_buckets		VNET(curr_dyn_buckets)
312 #define	V_dyn_timeout			VNET(dyn_timeout)
313 
314 /* Maximum length of states chain in a bucket */
315 VNET_DEFINE_STATIC(uint32_t, curr_max_length);
316 #define	V_curr_max_length		VNET(curr_max_length)
317 
318 VNET_DEFINE_STATIC(uint32_t, dyn_keep_states);
319 #define	V_dyn_keep_states		VNET(dyn_keep_states)
320 
321 VNET_DEFINE_STATIC(uma_zone_t, dyn_data_zone);
322 VNET_DEFINE_STATIC(uma_zone_t, dyn_parent_zone);
323 VNET_DEFINE_STATIC(uma_zone_t, dyn_ipv4_zone);
324 #ifdef INET6
325 VNET_DEFINE_STATIC(uma_zone_t, dyn_ipv6_zone);
326 #define	V_dyn_ipv6_zone			VNET(dyn_ipv6_zone)
327 #endif /* INET6 */
328 #define	V_dyn_data_zone			VNET(dyn_data_zone)
329 #define	V_dyn_parent_zone		VNET(dyn_parent_zone)
330 #define	V_dyn_ipv4_zone			VNET(dyn_ipv4_zone)
331 
332 /*
333  * Timeouts for various events in handing dynamic rules.
334  */
335 VNET_DEFINE_STATIC(uint32_t, dyn_ack_lifetime);
336 VNET_DEFINE_STATIC(uint32_t, dyn_syn_lifetime);
337 VNET_DEFINE_STATIC(uint32_t, dyn_fin_lifetime);
338 VNET_DEFINE_STATIC(uint32_t, dyn_rst_lifetime);
339 VNET_DEFINE_STATIC(uint32_t, dyn_udp_lifetime);
340 VNET_DEFINE_STATIC(uint32_t, dyn_short_lifetime);
341 
342 #define	V_dyn_ack_lifetime		VNET(dyn_ack_lifetime)
343 #define	V_dyn_syn_lifetime		VNET(dyn_syn_lifetime)
344 #define	V_dyn_fin_lifetime		VNET(dyn_fin_lifetime)
345 #define	V_dyn_rst_lifetime		VNET(dyn_rst_lifetime)
346 #define	V_dyn_udp_lifetime		VNET(dyn_udp_lifetime)
347 #define	V_dyn_short_lifetime		VNET(dyn_short_lifetime)
348 
349 /*
350  * Keepalives are sent if dyn_keepalive is set. They are sent every
351  * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
352  * seconds of lifetime of a rule.
353  * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
354  * than dyn_keepalive_period.
355  */
356 VNET_DEFINE_STATIC(uint32_t, dyn_keepalive_interval);
357 VNET_DEFINE_STATIC(uint32_t, dyn_keepalive_period);
358 VNET_DEFINE_STATIC(uint32_t, dyn_keepalive);
359 VNET_DEFINE_STATIC(time_t, dyn_keepalive_last);
360 
361 #define	V_dyn_keepalive_interval	VNET(dyn_keepalive_interval)
362 #define	V_dyn_keepalive_period		VNET(dyn_keepalive_period)
363 #define	V_dyn_keepalive			VNET(dyn_keepalive)
364 #define	V_dyn_keepalive_last		VNET(dyn_keepalive_last)
365 
366 VNET_DEFINE_STATIC(uint32_t, dyn_max);		/* max # of dynamic states */
367 VNET_DEFINE_STATIC(uint32_t, dyn_count);	/* number of states */
368 VNET_DEFINE_STATIC(uint32_t, dyn_parent_max);	/* max # of parent states */
369 VNET_DEFINE_STATIC(uint32_t, dyn_parent_count);	/* number of parent states */
370 
371 #define	V_dyn_max			VNET(dyn_max)
372 #define	V_dyn_count			VNET(dyn_count)
373 #define	V_dyn_parent_max		VNET(dyn_parent_max)
374 #define	V_dyn_parent_count		VNET(dyn_parent_count)
375 
376 #define	DYN_COUNT_DEC(name)	do {			\
377 	MPASS((V_ ## name) > 0);			\
378 	ck_pr_dec_32(&(V_ ## name));			\
379 } while (0)
380 #define	DYN_COUNT_INC(name)	ck_pr_inc_32(&(V_ ## name))
381 #define	DYN_COUNT(name)		ck_pr_load_32(&(V_ ## name))
382 
383 static time_t last_log;	/* Log ratelimiting */
384 
385 /*
386  * Get/set maximum number of dynamic states in given VNET instance.
387  */
388 static int
389 sysctl_dyn_max(SYSCTL_HANDLER_ARGS)
390 {
391 	uint32_t nstates;
392 	int error;
393 
394 	nstates = V_dyn_max;
395 	error = sysctl_handle_32(oidp, &nstates, 0, req);
396 	/* Read operation or some error */
397 	if ((error != 0) || (req->newptr == NULL))
398 		return (error);
399 
400 	V_dyn_max = nstates;
401 	uma_zone_set_max(V_dyn_data_zone, V_dyn_max);
402 	return (0);
403 }
404 
405 static int
406 sysctl_dyn_parent_max(SYSCTL_HANDLER_ARGS)
407 {
408 	uint32_t nstates;
409 	int error;
410 
411 	nstates = V_dyn_parent_max;
412 	error = sysctl_handle_32(oidp, &nstates, 0, req);
413 	/* Read operation or some error */
414 	if ((error != 0) || (req->newptr == NULL))
415 		return (error);
416 
417 	V_dyn_parent_max = nstates;
418 	uma_zone_set_max(V_dyn_parent_zone, V_dyn_parent_max);
419 	return (0);
420 }
421 
422 static int
423 sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS)
424 {
425 	uint32_t nbuckets;
426 	int error;
427 
428 	nbuckets = V_dyn_buckets_max;
429 	error = sysctl_handle_32(oidp, &nbuckets, 0, req);
430 	/* Read operation or some error */
431 	if ((error != 0) || (req->newptr == NULL))
432 		return (error);
433 
434 	if (nbuckets > 256)
435 		V_dyn_buckets_max = 1 << fls(nbuckets - 1);
436 	else
437 		return (EINVAL);
438 	return (0);
439 }
440 
441 SYSCTL_DECL(_net_inet_ip_fw);
442 
443 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_count,
444     CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(dyn_count), 0,
445     "Current number of dynamic states.");
446 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, dyn_parent_count,
447     CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(dyn_parent_count), 0,
448     "Current number of parent states. ");
449 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets,
450     CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(curr_dyn_buckets), 0,
451     "Current number of buckets for states hash table.");
452 SYSCTL_U32(_net_inet_ip_fw, OID_AUTO, curr_max_length,
453     CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(curr_max_length), 0,
454     "Current maximum length of states chains in hash buckets.");
455 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_buckets,
456     CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
457     0, 0, sysctl_dyn_buckets, "IU",
458     "Max number of buckets for dynamic states hash table.");
459 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_max,
460     CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
461     0, 0, sysctl_dyn_max, "IU",
462     "Max number of dynamic states.");
463 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_parent_max,
464     CTLFLAG_VNET | CTLTYPE_U32 | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
465     0, 0, sysctl_dyn_parent_max, "IU",
466     "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");
491 
492 
493 #ifdef IPFIREWALL_DYNDEBUG
494 #define	DYN_DEBUG(fmt, ...)	do {			\
495 	printf("%s: " fmt "\n", __func__, __VA_ARGS__);	\
496 } while (0)
497 #else
498 #define	DYN_DEBUG(fmt, ...)
499 #endif /* !IPFIREWALL_DYNDEBUG */
500 
501 #ifdef INET6
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,
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 *,
514     ipfw_dyn_rule *);
515 
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,
519     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 *);
523 
524 static struct dyn_ipv6_state *dyn_lookup_ipv6_parent(
525     const struct ipfw_flow_id *, uint32_t, const void *, uint32_t, uint16_t,
526     uint32_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,
529     uint32_t);
530 static struct dyn_ipv6_state *dyn_add_ipv6_parent(void *, uint32_t, uint16_t,
531     const struct ipfw_flow_id *, uint32_t, uint32_t, uint32_t, uint16_t);
532 #endif /* INET6 */
533 
534 /* Functions to work with limit states */
535 static void *dyn_get_parent_state(const struct ipfw_flow_id *, uint32_t,
536     struct ip_fw *, uint32_t, uint32_t, uint16_t);
537 static struct dyn_ipv4_state *dyn_lookup_ipv4_parent(
538     const struct ipfw_flow_id *, const void *, uint32_t, uint16_t, uint32_t);
539 static struct dyn_ipv4_state *dyn_lookup_ipv4_parent_locked(
540     const struct ipfw_flow_id *, const void *, uint32_t, uint16_t, uint32_t);
541 static struct dyn_parent *dyn_alloc_parent(void *, uint32_t, uint16_t,
542     uint32_t);
543 static struct dyn_ipv4_state *dyn_add_ipv4_parent(void *, uint32_t, uint16_t,
544     const struct ipfw_flow_id *, uint32_t, uint32_t, uint16_t);
545 
546 static void dyn_tick(void *);
547 static void dyn_expire_states(struct ip_fw_chain *, ipfw_range_tlv *);
548 static void dyn_free_states(struct ip_fw_chain *);
549 static void dyn_export_parent(const struct dyn_parent *, uint16_t, uint8_t,
550     ipfw_dyn_rule *);
551 static void dyn_export_data(const struct dyn_data *, uint16_t, uint8_t,
552     uint8_t, ipfw_dyn_rule *);
553 static uint32_t dyn_update_tcp_state(struct dyn_data *,
554     const struct ipfw_flow_id *, const struct tcphdr *, int);
555 static void dyn_update_proto_state(struct dyn_data *,
556     const struct ipfw_flow_id *, const void *, int, int);
557 
558 /* Functions to work with IPv4 states */
559 struct dyn_ipv4_state *dyn_lookup_ipv4_state(const struct ipfw_flow_id *,
560     const void *, struct ipfw_dyn_info *, int);
561 static int dyn_lookup_ipv4_state_locked(const struct ipfw_flow_id *,
562     const void *, int, uint32_t, uint16_t);
563 static struct dyn_ipv4_state *dyn_alloc_ipv4_state(
564     const struct ipfw_flow_id *, uint16_t, uint8_t);
565 static int dyn_add_ipv4_state(void *, uint32_t, uint16_t,
566     const struct ipfw_flow_id *, const void *, int, uint32_t,
567     struct ipfw_dyn_info *, uint16_t, uint16_t, uint8_t);
568 static void dyn_export_ipv4_state(const struct dyn_ipv4_state *,
569     ipfw_dyn_rule *);
570 
571 /*
572  * Named states support.
573  */
574 static char *default_state_name = "default";
575 struct dyn_state_obj {
576 	struct named_object	no;
577 	char			name[64];
578 };
579 
580 #define	DYN_STATE_OBJ(ch, cmd)	\
581     ((struct dyn_state_obj *)SRV_OBJECT(ch, (cmd)->arg1))
582 /*
583  * Classifier callback.
584  * Return 0 if opcode contains object that should be referenced
585  * or rewritten.
586  */
587 static int
588 dyn_classify(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
589 {
590 
591 	DYN_DEBUG("opcode %d, arg1 %d", cmd->opcode, cmd->arg1);
592 	/* Don't rewrite "check-state any" */
593 	if (cmd->arg1 == 0 &&
594 	    cmd->opcode == O_CHECK_STATE)
595 		return (1);
596 
597 	*puidx = cmd->arg1;
598 	*ptype = 0;
599 	return (0);
600 }
601 
602 static void
603 dyn_update(ipfw_insn *cmd, uint16_t idx)
604 {
605 
606 	cmd->arg1 = idx;
607 	DYN_DEBUG("opcode %d, arg1 %d", cmd->opcode, cmd->arg1);
608 }
609 
610 static int
611 dyn_findbyname(struct ip_fw_chain *ch, struct tid_info *ti,
612     struct named_object **pno)
613 {
614 	ipfw_obj_ntlv *ntlv;
615 	const char *name;
616 
617 	DYN_DEBUG("uidx %d", ti->uidx);
618 	if (ti->uidx != 0) {
619 		if (ti->tlvs == NULL)
620 			return (EINVAL);
621 		/* Search ntlv in the buffer provided by user */
622 		ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
623 		    IPFW_TLV_STATE_NAME);
624 		if (ntlv == NULL)
625 			return (EINVAL);
626 		name = ntlv->name;
627 	} else
628 		name = default_state_name;
629 	/*
630 	 * Search named object with corresponding name.
631 	 * Since states objects are global - ignore the set value
632 	 * and use zero instead.
633 	 */
634 	*pno = ipfw_objhash_lookup_name_type(CHAIN_TO_SRV(ch), 0,
635 	    IPFW_TLV_STATE_NAME, name);
636 	/*
637 	 * We always return success here.
638 	 * The caller will check *pno and mark object as unresolved,
639 	 * then it will automatically create "default" object.
640 	 */
641 	return (0);
642 }
643 
644 static struct named_object *
645 dyn_findbykidx(struct ip_fw_chain *ch, uint16_t idx)
646 {
647 
648 	DYN_DEBUG("kidx %d", idx);
649 	return (ipfw_objhash_lookup_kidx(CHAIN_TO_SRV(ch), idx));
650 }
651 
652 static int
653 dyn_create(struct ip_fw_chain *ch, struct tid_info *ti,
654     uint16_t *pkidx)
655 {
656 	struct namedobj_instance *ni;
657 	struct dyn_state_obj *obj;
658 	struct named_object *no;
659 	ipfw_obj_ntlv *ntlv;
660 	char *name;
661 
662 	DYN_DEBUG("uidx %d", ti->uidx);
663 	if (ti->uidx != 0) {
664 		if (ti->tlvs == NULL)
665 			return (EINVAL);
666 		ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
667 		    IPFW_TLV_STATE_NAME);
668 		if (ntlv == NULL)
669 			return (EINVAL);
670 		name = ntlv->name;
671 	} else
672 		name = default_state_name;
673 
674 	ni = CHAIN_TO_SRV(ch);
675 	obj = malloc(sizeof(*obj), M_IPFW, M_WAITOK | M_ZERO);
676 	obj->no.name = obj->name;
677 	obj->no.etlv = IPFW_TLV_STATE_NAME;
678 	strlcpy(obj->name, name, sizeof(obj->name));
679 
680 	IPFW_UH_WLOCK(ch);
681 	no = ipfw_objhash_lookup_name_type(ni, 0,
682 	    IPFW_TLV_STATE_NAME, name);
683 	if (no != NULL) {
684 		/*
685 		 * Object is already created.
686 		 * Just return its kidx and bump refcount.
687 		 */
688 		*pkidx = no->kidx;
689 		no->refcnt++;
690 		IPFW_UH_WUNLOCK(ch);
691 		free(obj, M_IPFW);
692 		DYN_DEBUG("\tfound kidx %d", *pkidx);
693 		return (0);
694 	}
695 	if (ipfw_objhash_alloc_idx(ni, &obj->no.kidx) != 0) {
696 		DYN_DEBUG("\talloc_idx failed for %s", name);
697 		IPFW_UH_WUNLOCK(ch);
698 		free(obj, M_IPFW);
699 		return (ENOSPC);
700 	}
701 	ipfw_objhash_add(ni, &obj->no);
702 	SRV_OBJECT(ch, obj->no.kidx) = obj;
703 	obj->no.refcnt++;
704 	*pkidx = obj->no.kidx;
705 	IPFW_UH_WUNLOCK(ch);
706 	DYN_DEBUG("\tcreated kidx %d", *pkidx);
707 	return (0);
708 }
709 
710 static void
711 dyn_destroy(struct ip_fw_chain *ch, struct named_object *no)
712 {
713 	struct dyn_state_obj *obj;
714 
715 	IPFW_UH_WLOCK_ASSERT(ch);
716 
717 	KASSERT(no->etlv == IPFW_TLV_STATE_NAME,
718 	    ("%s: wrong object type %u", __func__, no->etlv));
719 	KASSERT(no->refcnt == 1,
720 	    ("Destroying object '%s' (type %u, idx %u) with refcnt %u",
721 	    no->name, no->etlv, no->kidx, no->refcnt));
722 	DYN_DEBUG("kidx %d", no->kidx);
723 	obj = SRV_OBJECT(ch, no->kidx);
724 	SRV_OBJECT(ch, no->kidx) = NULL;
725 	ipfw_objhash_del(CHAIN_TO_SRV(ch), no);
726 	ipfw_objhash_free_idx(CHAIN_TO_SRV(ch), no->kidx);
727 
728 	free(obj, M_IPFW);
729 }
730 
731 static struct opcode_obj_rewrite dyn_opcodes[] = {
732 	{
733 		O_KEEP_STATE, IPFW_TLV_STATE_NAME,
734 		dyn_classify, dyn_update,
735 		dyn_findbyname, dyn_findbykidx,
736 		dyn_create, dyn_destroy
737 	},
738 	{
739 		O_CHECK_STATE, IPFW_TLV_STATE_NAME,
740 		dyn_classify, dyn_update,
741 		dyn_findbyname, dyn_findbykidx,
742 		dyn_create, dyn_destroy
743 	},
744 	{
745 		O_PROBE_STATE, IPFW_TLV_STATE_NAME,
746 		dyn_classify, dyn_update,
747 		dyn_findbyname, dyn_findbykidx,
748 		dyn_create, dyn_destroy
749 	},
750 	{
751 		O_LIMIT, IPFW_TLV_STATE_NAME,
752 		dyn_classify, dyn_update,
753 		dyn_findbyname, dyn_findbykidx,
754 		dyn_create, dyn_destroy
755 	},
756 };
757 
758 /*
759  * IMPORTANT: the hash function for dynamic rules must be commutative
760  * in source and destination (ip,port), because rules are bidirectional
761  * and we want to find both in the same bucket.
762  */
763 #ifndef IPFIREWALL_JENKINSHASH
764 static __inline uint32_t
765 hash_packet(const struct ipfw_flow_id *id)
766 {
767 	uint32_t i;
768 
769 #ifdef INET6
770 	if (IS_IP6_FLOW_ID(id))
771 		i = ntohl((id->dst_ip6.__u6_addr.__u6_addr32[2]) ^
772 		    (id->dst_ip6.__u6_addr.__u6_addr32[3]) ^
773 		    (id->src_ip6.__u6_addr.__u6_addr32[2]) ^
774 		    (id->src_ip6.__u6_addr.__u6_addr32[3]));
775 	else
776 #endif /* INET6 */
777 	i = (id->dst_ip) ^ (id->src_ip);
778 	i ^= (id->dst_port) ^ (id->src_port);
779 	return (i);
780 }
781 
782 static __inline uint32_t
783 hash_parent(const struct ipfw_flow_id *id, const void *rule)
784 {
785 
786 	return (hash_packet(id) ^ ((uintptr_t)rule));
787 }
788 
789 #else /* IPFIREWALL_JENKINSHASH */
790 
791 VNET_DEFINE_STATIC(uint32_t, dyn_hashseed);
792 #define	V_dyn_hashseed		VNET(dyn_hashseed)
793 
794 static __inline int
795 addrcmp4(const struct ipfw_flow_id *id)
796 {
797 
798 	if (id->src_ip < id->dst_ip)
799 		return (0);
800 	if (id->src_ip > id->dst_ip)
801 		return (1);
802 	if (id->src_port <= id->dst_port)
803 		return (0);
804 	return (1);
805 }
806 
807 #ifdef INET6
808 static __inline int
809 addrcmp6(const struct ipfw_flow_id *id)
810 {
811 	int ret;
812 
813 	ret = memcmp(&id->src_ip6, &id->dst_ip6, sizeof(struct in6_addr));
814 	if (ret < 0)
815 		return (0);
816 	if (ret > 0)
817 		return (1);
818 	if (id->src_port <= id->dst_port)
819 		return (0);
820 	return (1);
821 }
822 
823 static __inline uint32_t
824 hash_packet6(const struct ipfw_flow_id *id)
825 {
826 	struct tuple6 {
827 		struct in6_addr	addr[2];
828 		uint16_t	port[2];
829 	} t6;
830 
831 	if (addrcmp6(id) == 0) {
832 		t6.addr[0] = id->src_ip6;
833 		t6.addr[1] = id->dst_ip6;
834 		t6.port[0] = id->src_port;
835 		t6.port[1] = id->dst_port;
836 	} else {
837 		t6.addr[0] = id->dst_ip6;
838 		t6.addr[1] = id->src_ip6;
839 		t6.port[0] = id->dst_port;
840 		t6.port[1] = id->src_port;
841 	}
842 	return (jenkins_hash32((const uint32_t *)&t6,
843 	    sizeof(t6) / sizeof(uint32_t), V_dyn_hashseed));
844 }
845 #endif
846 
847 static __inline uint32_t
848 hash_packet(const struct ipfw_flow_id *id)
849 {
850 	struct tuple4 {
851 		in_addr_t	addr[2];
852 		uint16_t	port[2];
853 	} t4;
854 
855 	if (IS_IP4_FLOW_ID(id)) {
856 		/* All fields are in host byte order */
857 		if (addrcmp4(id) == 0) {
858 			t4.addr[0] = id->src_ip;
859 			t4.addr[1] = id->dst_ip;
860 			t4.port[0] = id->src_port;
861 			t4.port[1] = id->dst_port;
862 		} else {
863 			t4.addr[0] = id->dst_ip;
864 			t4.addr[1] = id->src_ip;
865 			t4.port[0] = id->dst_port;
866 			t4.port[1] = id->src_port;
867 		}
868 		return (jenkins_hash32((const uint32_t *)&t4,
869 		    sizeof(t4) / sizeof(uint32_t), V_dyn_hashseed));
870 	} else
871 #ifdef INET6
872 	if (IS_IP6_FLOW_ID(id))
873 		return (hash_packet6(id));
874 #endif
875 	return (0);
876 }
877 
878 static __inline uint32_t
879 hash_parent(const struct ipfw_flow_id *id, const void *rule)
880 {
881 
882 	return (jenkins_hash32((const uint32_t *)&rule,
883 	    sizeof(rule) / sizeof(uint32_t), hash_packet(id)));
884 }
885 #endif /* IPFIREWALL_JENKINSHASH */
886 
887 /*
888  * Print customizable flow id description via log(9) facility.
889  */
890 static void
891 print_dyn_rule_flags(const struct ipfw_flow_id *id, int dyn_type,
892     int log_flags, char *prefix, char *postfix)
893 {
894 	struct in_addr da;
895 #ifdef INET6
896 	char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
897 #else
898 	char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
899 #endif
900 
901 #ifdef INET6
902 	if (IS_IP6_FLOW_ID(id)) {
903 		ip6_sprintf(src, &id->src_ip6);
904 		ip6_sprintf(dst, &id->dst_ip6);
905 	} else
906 #endif
907 	{
908 		da.s_addr = htonl(id->src_ip);
909 		inet_ntop(AF_INET, &da, src, sizeof(src));
910 		da.s_addr = htonl(id->dst_ip);
911 		inet_ntop(AF_INET, &da, dst, sizeof(dst));
912 	}
913 	log(log_flags, "ipfw: %s type %d %s %d -> %s %d, %d %s\n",
914 	    prefix, dyn_type, src, id->src_port, dst,
915 	    id->dst_port, V_dyn_count, postfix);
916 }
917 
918 #define	print_dyn_rule(id, dtype, prefix, postfix)	\
919 	print_dyn_rule_flags(id, dtype, LOG_DEBUG, prefix, postfix)
920 
921 #define	TIME_LEQ(a,b)	((int)((a)-(b)) <= 0)
922 #define	TIME_LE(a,b)	((int)((a)-(b)) < 0)
923 #define	_SEQ_GE(a,b)	((int)((a)-(b)) >= 0)
924 #define	BOTH_SYN	(TH_SYN | (TH_SYN << 8))
925 #define	BOTH_FIN	(TH_FIN | (TH_FIN << 8))
926 #define	TCP_FLAGS	(TH_FLAGS | (TH_FLAGS << 8))
927 #define	ACK_FWD		0x00010000	/* fwd ack seen */
928 #define	ACK_REV		0x00020000	/* rev ack seen */
929 #define	ACK_BOTH	(ACK_FWD | ACK_REV)
930 
931 static uint32_t
932 dyn_update_tcp_state(struct dyn_data *data, const struct ipfw_flow_id *pkt,
933     const struct tcphdr *tcp, int dir)
934 {
935 	uint32_t ack, expire;
936 	uint32_t state, old;
937 	uint8_t th_flags;
938 
939 	expire = data->expire;
940 	old = state = data->state;
941 	th_flags = pkt->_flags & (TH_FIN | TH_SYN | TH_RST);
942 	state |= (dir == MATCH_FORWARD) ? th_flags: (th_flags << 8);
943 	switch (state & TCP_FLAGS) {
944 	case TH_SYN:			/* opening */
945 		expire = time_uptime + V_dyn_syn_lifetime;
946 		break;
947 
948 	case BOTH_SYN:			/* move to established */
949 	case BOTH_SYN | TH_FIN:		/* one side tries to close */
950 	case BOTH_SYN | (TH_FIN << 8):
951 		if (tcp == NULL)
952 			break;
953 		ack = ntohl(tcp->th_ack);
954 		if (dir == MATCH_FORWARD) {
955 			if (data->ack_fwd == 0 ||
956 			    _SEQ_GE(ack, data->ack_fwd)) {
957 				state |= ACK_FWD;
958 				if (data->ack_fwd != ack)
959 					ck_pr_store_32(&data->ack_fwd, ack);
960 			}
961 		} else {
962 			if (data->ack_rev == 0 ||
963 			    _SEQ_GE(ack, data->ack_rev)) {
964 				state |= ACK_REV;
965 				if (data->ack_rev != ack)
966 					ck_pr_store_32(&data->ack_rev, ack);
967 			}
968 		}
969 		if ((state & ACK_BOTH) == ACK_BOTH) {
970 			/*
971 			 * Set expire time to V_dyn_ack_lifetime only if
972 			 * we got ACKs for both directions.
973 			 * We use XOR here to avoid possible state
974 			 * overwriting in concurrent thread.
975 			 */
976 			expire = time_uptime + V_dyn_ack_lifetime;
977 			ck_pr_xor_32(&data->state, ACK_BOTH);
978 		} else if ((data->state & ACK_BOTH) != (state & ACK_BOTH))
979 			ck_pr_or_32(&data->state, state & ACK_BOTH);
980 		break;
981 
982 	case BOTH_SYN | BOTH_FIN:	/* both sides closed */
983 		if (V_dyn_fin_lifetime >= V_dyn_keepalive_period)
984 			V_dyn_fin_lifetime = V_dyn_keepalive_period - 1;
985 		expire = time_uptime + V_dyn_fin_lifetime;
986 		break;
987 
988 	default:
989 		if (V_dyn_keepalive != 0 &&
990 		    V_dyn_rst_lifetime >= V_dyn_keepalive_period)
991 			V_dyn_rst_lifetime = V_dyn_keepalive_period - 1;
992 		expire = time_uptime + V_dyn_rst_lifetime;
993 	}
994 	/* Save TCP state if it was changed */
995 	if ((state & TCP_FLAGS) != (old & TCP_FLAGS))
996 		ck_pr_or_32(&data->state, state & TCP_FLAGS);
997 	return (expire);
998 }
999 
1000 /*
1001  * Update ULP specific state.
1002  * For TCP we keep sequence numbers and flags. For other protocols
1003  * currently we update only expire time. Packets and bytes counters
1004  * are also updated here.
1005  */
1006 static void
1007 dyn_update_proto_state(struct dyn_data *data, const struct ipfw_flow_id *pkt,
1008     const void *ulp, int pktlen, int dir)
1009 {
1010 	uint32_t expire;
1011 
1012 	/* NOTE: we are in critical section here. */
1013 	switch (pkt->proto) {
1014 	case IPPROTO_UDP:
1015 	case IPPROTO_UDPLITE:
1016 		expire = time_uptime + V_dyn_udp_lifetime;
1017 		break;
1018 	case IPPROTO_TCP:
1019 		expire = dyn_update_tcp_state(data, pkt, ulp, dir);
1020 		break;
1021 	default:
1022 		expire = time_uptime + V_dyn_short_lifetime;
1023 	}
1024 	/*
1025 	 * Expiration timer has the per-second granularity, no need to update
1026 	 * it every time when state is matched.
1027 	 */
1028 	if (data->expire != expire)
1029 		ck_pr_store_32(&data->expire, expire);
1030 
1031 	if (dir == MATCH_FORWARD)
1032 		DYN_COUNTER_INC(data, fwd, pktlen);
1033 	else
1034 		DYN_COUNTER_INC(data, rev, pktlen);
1035 }
1036 
1037 /*
1038  * Lookup IPv4 state.
1039  * Must be called in critical section.
1040  */
1041 struct dyn_ipv4_state *
1042 dyn_lookup_ipv4_state(const struct ipfw_flow_id *pkt, const void *ulp,
1043     struct ipfw_dyn_info *info, int pktlen)
1044 {
1045 	struct dyn_ipv4_state *s;
1046 	uint32_t version, bucket;
1047 
1048 	bucket = DYN_BUCKET(info->hashval, V_curr_dyn_buckets);
1049 	info->version = DYN_BUCKET_VERSION(bucket, ipv4_add);
1050 restart:
1051 	version = DYN_BUCKET_VERSION(bucket, ipv4_del);
1052 	CK_SLIST_FOREACH(s, &V_dyn_ipv4[bucket], entry) {
1053 		DYNSTATE_PROTECT(s);
1054 		if (version != DYN_BUCKET_VERSION(bucket, ipv4_del))
1055 			goto restart;
1056 		if (s->proto != pkt->proto)
1057 			continue;
1058 		if (info->kidx != 0 && s->kidx != info->kidx)
1059 			continue;
1060 		if (s->sport == pkt->src_port && s->dport == pkt->dst_port &&
1061 		    s->src == pkt->src_ip && s->dst == pkt->dst_ip) {
1062 			info->direction = MATCH_FORWARD;
1063 			break;
1064 		}
1065 		if (s->sport == pkt->dst_port && s->dport == pkt->src_port &&
1066 		    s->src == pkt->dst_ip && s->dst == pkt->src_ip) {
1067 			info->direction = MATCH_REVERSE;
1068 			break;
1069 		}
1070 	}
1071 
1072 	if (s != NULL)
1073 		dyn_update_proto_state(s->data, pkt, ulp, pktlen,
1074 		    info->direction);
1075 	return (s);
1076 }
1077 
1078 /*
1079  * Lookup IPv4 state.
1080  * Simplifed version is used to check that matching state doesn't exist.
1081  */
1082 static int
1083 dyn_lookup_ipv4_state_locked(const struct ipfw_flow_id *pkt,
1084     const void *ulp, int pktlen, uint32_t bucket, uint16_t kidx)
1085 {
1086 	struct dyn_ipv4_state *s;
1087 	int dir;
1088 
1089 	dir = MATCH_NONE;
1090 	DYN_BUCKET_ASSERT(bucket);
1091 	CK_SLIST_FOREACH(s, &V_dyn_ipv4[bucket], entry) {
1092 		if (s->proto != pkt->proto ||
1093 		    s->kidx != kidx)
1094 			continue;
1095 		if (s->sport == pkt->src_port &&
1096 		    s->dport == pkt->dst_port &&
1097 		    s->src == pkt->src_ip && s->dst == pkt->dst_ip) {
1098 			dir = MATCH_FORWARD;
1099 			break;
1100 		}
1101 		if (s->sport == pkt->dst_port && s->dport == pkt->src_port &&
1102 		    s->src == pkt->dst_ip && s->dst == pkt->src_ip) {
1103 			dir = MATCH_REVERSE;
1104 			break;
1105 		}
1106 	}
1107 	if (s != NULL)
1108 		dyn_update_proto_state(s->data, pkt, ulp, pktlen, dir);
1109 	return (s != NULL);
1110 }
1111 
1112 struct dyn_ipv4_state *
1113 dyn_lookup_ipv4_parent(const struct ipfw_flow_id *pkt, const void *rule,
1114     uint32_t ruleid, uint16_t rulenum, uint32_t hashval)
1115 {
1116 	struct dyn_ipv4_state *s;
1117 	uint32_t version, bucket;
1118 
1119 	bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1120 restart:
1121 	version = DYN_BUCKET_VERSION(bucket, ipv4_parent_del);
1122 	CK_SLIST_FOREACH(s, &V_dyn_ipv4_parent[bucket], entry) {
1123 		DYNSTATE_PROTECT(s);
1124 		if (version != DYN_BUCKET_VERSION(bucket, ipv4_parent_del))
1125 			goto restart;
1126 		/*
1127 		 * NOTE: we do not need to check kidx, because parent rule
1128 		 * can not create states with different kidx.
1129 		 * And parent rule always created for forward direction.
1130 		 */
1131 		if (s->limit->parent == rule &&
1132 		    s->limit->ruleid == ruleid &&
1133 		    s->limit->rulenum == rulenum &&
1134 		    s->proto == pkt->proto &&
1135 		    s->sport == pkt->src_port &&
1136 		    s->dport == pkt->dst_port &&
1137 		    s->src == pkt->src_ip && s->dst == pkt->dst_ip) {
1138 			if (s->limit->expire != time_uptime +
1139 			    V_dyn_short_lifetime)
1140 				ck_pr_store_32(&s->limit->expire,
1141 				    time_uptime + V_dyn_short_lifetime);
1142 			break;
1143 		}
1144 	}
1145 	return (s);
1146 }
1147 
1148 static struct dyn_ipv4_state *
1149 dyn_lookup_ipv4_parent_locked(const struct ipfw_flow_id *pkt,
1150     const void *rule, uint32_t ruleid, uint16_t rulenum, uint32_t bucket)
1151 {
1152 	struct dyn_ipv4_state *s;
1153 
1154 	DYN_BUCKET_ASSERT(bucket);
1155 	CK_SLIST_FOREACH(s, &V_dyn_ipv4_parent[bucket], entry) {
1156 		if (s->limit->parent == rule &&
1157 		    s->limit->ruleid == ruleid &&
1158 		    s->limit->rulenum == rulenum &&
1159 		    s->proto == pkt->proto &&
1160 		    s->sport == pkt->src_port &&
1161 		    s->dport == pkt->dst_port &&
1162 		    s->src == pkt->src_ip && s->dst == pkt->dst_ip)
1163 			break;
1164 	}
1165 	return (s);
1166 }
1167 
1168 
1169 #ifdef INET6
1170 static uint32_t
1171 dyn_getscopeid(const struct ip_fw_args *args)
1172 {
1173 
1174 	/*
1175 	 * If source or destination address is an scopeid address, we need
1176 	 * determine the scope zone id to resolve address scope ambiguity.
1177 	 */
1178 	if (IN6_IS_ADDR_LINKLOCAL(&args->f_id.src_ip6) ||
1179 	    IN6_IS_ADDR_LINKLOCAL(&args->f_id.dst_ip6))
1180 		return (in6_getscopezone(args->ifp, IPV6_ADDR_SCOPE_LINKLOCAL));
1181 
1182 	return (0);
1183 }
1184 
1185 /*
1186  * Lookup IPv6 state.
1187  * Must be called in critical section.
1188  */
1189 static struct dyn_ipv6_state *
1190 dyn_lookup_ipv6_state(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1191     const void *ulp, struct ipfw_dyn_info *info, int pktlen)
1192 {
1193 	struct dyn_ipv6_state *s;
1194 	uint32_t version, bucket;
1195 
1196 	bucket = DYN_BUCKET(info->hashval, V_curr_dyn_buckets);
1197 	info->version = DYN_BUCKET_VERSION(bucket, ipv6_add);
1198 restart:
1199 	version = DYN_BUCKET_VERSION(bucket, ipv6_del);
1200 	CK_SLIST_FOREACH(s, &V_dyn_ipv6[bucket], entry) {
1201 		DYNSTATE_PROTECT(s);
1202 		if (version != DYN_BUCKET_VERSION(bucket, ipv6_del))
1203 			goto restart;
1204 		if (s->proto != pkt->proto || s->zoneid != zoneid)
1205 			continue;
1206 		if (info->kidx != 0 && s->kidx != info->kidx)
1207 			continue;
1208 		if (s->sport == pkt->src_port && s->dport == pkt->dst_port &&
1209 		    IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) &&
1210 		    IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) {
1211 			info->direction = MATCH_FORWARD;
1212 			break;
1213 		}
1214 		if (s->sport == pkt->dst_port && s->dport == pkt->src_port &&
1215 		    IN6_ARE_ADDR_EQUAL(&s->src, &pkt->dst_ip6) &&
1216 		    IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->src_ip6)) {
1217 			info->direction = MATCH_REVERSE;
1218 			break;
1219 		}
1220 	}
1221 	if (s != NULL)
1222 		dyn_update_proto_state(s->data, pkt, ulp, pktlen,
1223 		    info->direction);
1224 	return (s);
1225 }
1226 
1227 /*
1228  * Lookup IPv6 state.
1229  * Simplifed version is used to check that matching state doesn't exist.
1230  */
1231 static int
1232 dyn_lookup_ipv6_state_locked(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1233     const void *ulp, int pktlen, uint32_t bucket, uint16_t kidx)
1234 {
1235 	struct dyn_ipv6_state *s;
1236 	int dir;
1237 
1238 	dir = MATCH_NONE;
1239 	DYN_BUCKET_ASSERT(bucket);
1240 	CK_SLIST_FOREACH(s, &V_dyn_ipv6[bucket], entry) {
1241 		if (s->proto != pkt->proto || s->kidx != kidx ||
1242 		    s->zoneid != zoneid)
1243 			continue;
1244 		if (s->sport == pkt->src_port && s->dport == pkt->dst_port &&
1245 		    IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) &&
1246 		    IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) {
1247 			dir = MATCH_FORWARD;
1248 			break;
1249 		}
1250 		if (s->sport == pkt->dst_port && s->dport == pkt->src_port &&
1251 		    IN6_ARE_ADDR_EQUAL(&s->src, &pkt->dst_ip6) &&
1252 		    IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->src_ip6)) {
1253 			dir = MATCH_REVERSE;
1254 			break;
1255 		}
1256 	}
1257 	if (s != NULL)
1258 		dyn_update_proto_state(s->data, pkt, ulp, pktlen, dir);
1259 	return (s != NULL);
1260 }
1261 
1262 static struct dyn_ipv6_state *
1263 dyn_lookup_ipv6_parent(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1264     const void *rule, uint32_t ruleid, uint16_t rulenum, uint32_t hashval)
1265 {
1266 	struct dyn_ipv6_state *s;
1267 	uint32_t version, bucket;
1268 
1269 	bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1270 restart:
1271 	version = DYN_BUCKET_VERSION(bucket, ipv6_parent_del);
1272 	CK_SLIST_FOREACH(s, &V_dyn_ipv6_parent[bucket], entry) {
1273 		DYNSTATE_PROTECT(s);
1274 		if (version != DYN_BUCKET_VERSION(bucket, ipv6_parent_del))
1275 			goto restart;
1276 		/*
1277 		 * NOTE: we do not need to check kidx, because parent rule
1278 		 * can not create states with different kidx.
1279 		 * Also parent rule always created for forward direction.
1280 		 */
1281 		if (s->limit->parent == rule &&
1282 		    s->limit->ruleid == ruleid &&
1283 		    s->limit->rulenum == rulenum &&
1284 		    s->proto == pkt->proto &&
1285 		    s->sport == pkt->src_port &&
1286 		    s->dport == pkt->dst_port && s->zoneid == zoneid &&
1287 		    IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) &&
1288 		    IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6)) {
1289 			if (s->limit->expire != time_uptime +
1290 			    V_dyn_short_lifetime)
1291 				ck_pr_store_32(&s->limit->expire,
1292 				    time_uptime + V_dyn_short_lifetime);
1293 			break;
1294 		}
1295 	}
1296 	return (s);
1297 }
1298 
1299 static struct dyn_ipv6_state *
1300 dyn_lookup_ipv6_parent_locked(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1301     const void *rule, uint32_t ruleid, uint16_t rulenum, uint32_t bucket)
1302 {
1303 	struct dyn_ipv6_state *s;
1304 
1305 	DYN_BUCKET_ASSERT(bucket);
1306 	CK_SLIST_FOREACH(s, &V_dyn_ipv6_parent[bucket], entry) {
1307 		if (s->limit->parent == rule &&
1308 		    s->limit->ruleid == ruleid &&
1309 		    s->limit->rulenum == rulenum &&
1310 		    s->proto == pkt->proto &&
1311 		    s->sport == pkt->src_port &&
1312 		    s->dport == pkt->dst_port && s->zoneid == zoneid &&
1313 		    IN6_ARE_ADDR_EQUAL(&s->src, &pkt->src_ip6) &&
1314 		    IN6_ARE_ADDR_EQUAL(&s->dst, &pkt->dst_ip6))
1315 			break;
1316 	}
1317 	return (s);
1318 }
1319 
1320 #endif /* INET6 */
1321 
1322 /*
1323  * Lookup dynamic state.
1324  *  pkt - filled by ipfw_chk() ipfw_flow_id;
1325  *  ulp - determined by ipfw_chk() upper level protocol header;
1326  *  dyn_info - info about matched state to return back;
1327  * Returns pointer to state's parent rule and dyn_info. If there is
1328  * no state, NULL is returned.
1329  * On match ipfw_dyn_lookup() updates state's counters.
1330  */
1331 struct ip_fw *
1332 ipfw_dyn_lookup_state(const struct ip_fw_args *args, const void *ulp,
1333     int pktlen, const ipfw_insn *cmd, struct ipfw_dyn_info *info)
1334 {
1335 	struct dyn_data *data;
1336 	struct ip_fw *rule;
1337 
1338 	IPFW_RLOCK_ASSERT(&V_layer3_chain);
1339 
1340 	data = NULL;
1341 	rule = NULL;
1342 	info->kidx = cmd->arg1;
1343 	info->direction = MATCH_NONE;
1344 	info->hashval = hash_packet(&args->f_id);
1345 
1346 	DYNSTATE_CRITICAL_ENTER();
1347 	if (IS_IP4_FLOW_ID(&args->f_id)) {
1348 		struct dyn_ipv4_state *s;
1349 
1350 		s = dyn_lookup_ipv4_state(&args->f_id, ulp, info, pktlen);
1351 		if (s != NULL) {
1352 			/*
1353 			 * Dynamic states are created using the same 5-tuple,
1354 			 * so it is assumed, that parent rule for O_LIMIT
1355 			 * state has the same address family.
1356 			 */
1357 			data = s->data;
1358 			if (s->type == O_LIMIT) {
1359 				s = data->parent;
1360 				rule = s->limit->parent;
1361 			} else
1362 				rule = data->parent;
1363 		}
1364 	}
1365 #ifdef INET6
1366 	else if (IS_IP6_FLOW_ID(&args->f_id)) {
1367 		struct dyn_ipv6_state *s;
1368 
1369 		s = dyn_lookup_ipv6_state(&args->f_id, dyn_getscopeid(args),
1370 		    ulp, info, pktlen);
1371 		if (s != NULL) {
1372 			data = s->data;
1373 			if (s->type == O_LIMIT) {
1374 				s = data->parent;
1375 				rule = s->limit->parent;
1376 			} else
1377 				rule = data->parent;
1378 		}
1379 	}
1380 #endif
1381 	if (data != NULL) {
1382 		/*
1383 		 * If cached chain id is the same, we can avoid rule index
1384 		 * lookup. Otherwise do lookup and update chain_id and f_pos.
1385 		 * It is safe even if there is concurrent thread that want
1386 		 * update the same state, because chain->id can be changed
1387 		 * only under IPFW_WLOCK().
1388 		 */
1389 		if (data->chain_id != V_layer3_chain.id) {
1390 			data->f_pos = ipfw_find_rule(&V_layer3_chain,
1391 			    data->rulenum, data->ruleid);
1392 			/*
1393 			 * Check that found state has not orphaned.
1394 			 * When chain->id being changed the parent
1395 			 * rule can be deleted. If found rule doesn't
1396 			 * match the parent pointer, consider this
1397 			 * result as MATCH_NONE and return NULL.
1398 			 *
1399 			 * This will lead to creation of new similar state
1400 			 * that will be added into head of this bucket.
1401 			 * And the state that we currently have matched
1402 			 * should be deleted by dyn_expire_states().
1403 			 *
1404 			 * In case when dyn_keep_states is enabled, return
1405 			 * pointer to deleted rule and f_pos value
1406 			 * corresponding to penultimate rule.
1407 			 * When we have enabled V_dyn_keep_states, states
1408 			 * that become orphaned will get the DYN_REFERENCED
1409 			 * flag and rule will keep around. So we can return
1410 			 * it. But since it is not in the rules map, we need
1411 			 * return such f_pos value, so after the state
1412 			 * handling if the search will continue, the next rule
1413 			 * will be the last one - the default rule.
1414 			 */
1415 			if (V_layer3_chain.map[data->f_pos] == rule) {
1416 				data->chain_id = V_layer3_chain.id;
1417 				info->f_pos = data->f_pos;
1418 			} else if (V_dyn_keep_states != 0) {
1419 				/*
1420 				 * The original rule pointer is still usable.
1421 				 * So, we return it, but f_pos need to be
1422 				 * changed to point to the penultimate rule.
1423 				 */
1424 				MPASS(V_layer3_chain.n_rules > 1);
1425 				data->chain_id = V_layer3_chain.id;
1426 				data->f_pos = V_layer3_chain.n_rules - 2;
1427 				info->f_pos = data->f_pos;
1428 			} else {
1429 				rule = NULL;
1430 				info->direction = MATCH_NONE;
1431 				DYN_DEBUG("rule %p  [%u, %u] is considered "
1432 				    "invalid in data %p", rule, data->ruleid,
1433 				    data->rulenum, data);
1434 				/* info->f_pos doesn't matter here. */
1435 			}
1436 		} else
1437 			info->f_pos = data->f_pos;
1438 	}
1439 	DYNSTATE_CRITICAL_EXIT();
1440 #if 0
1441 	/*
1442 	 * Return MATCH_NONE if parent rule is in disabled set.
1443 	 * This will lead to creation of new similar state that
1444 	 * will be added into head of this bucket.
1445 	 *
1446 	 * XXXAE: we need to be able update state's set when parent
1447 	 *	  rule set is changed.
1448 	 */
1449 	if (rule != NULL && (V_set_disable & (1 << rule->set))) {
1450 		rule = NULL;
1451 		info->direction = MATCH_NONE;
1452 	}
1453 #endif
1454 	return (rule);
1455 }
1456 
1457 static struct dyn_parent *
1458 dyn_alloc_parent(void *parent, uint32_t ruleid, uint16_t rulenum,
1459     uint32_t hashval)
1460 {
1461 	struct dyn_parent *limit;
1462 
1463 	limit = uma_zalloc(V_dyn_parent_zone, M_NOWAIT | M_ZERO);
1464 	if (limit == NULL) {
1465 		if (last_log != time_uptime) {
1466 			last_log = time_uptime;
1467 			log(LOG_DEBUG,
1468 			    "ipfw: Cannot allocate parent dynamic state, "
1469 			    "consider increasing "
1470 			    "net.inet.ip.fw.dyn_parent_max\n");
1471 		}
1472 		return (NULL);
1473 	}
1474 
1475 	limit->parent = parent;
1476 	limit->ruleid = ruleid;
1477 	limit->rulenum = rulenum;
1478 	limit->hashval = hashval;
1479 	limit->expire = time_uptime + V_dyn_short_lifetime;
1480 	return (limit);
1481 }
1482 
1483 static struct dyn_data *
1484 dyn_alloc_dyndata(void *parent, uint32_t ruleid, uint16_t rulenum,
1485     const struct ipfw_flow_id *pkt, const void *ulp, int pktlen,
1486     uint32_t hashval, uint16_t fibnum)
1487 {
1488 	struct dyn_data *data;
1489 
1490 	data = uma_zalloc(V_dyn_data_zone, M_NOWAIT | M_ZERO);
1491 	if (data == NULL) {
1492 		if (last_log != time_uptime) {
1493 			last_log = time_uptime;
1494 			log(LOG_DEBUG,
1495 			    "ipfw: Cannot allocate dynamic state, "
1496 			    "consider increasing net.inet.ip.fw.dyn_max\n");
1497 		}
1498 		return (NULL);
1499 	}
1500 
1501 	data->parent = parent;
1502 	data->ruleid = ruleid;
1503 	data->rulenum = rulenum;
1504 	data->fibnum = fibnum;
1505 	data->hashval = hashval;
1506 	data->expire = time_uptime + V_dyn_syn_lifetime;
1507 	dyn_update_proto_state(data, pkt, ulp, pktlen, MATCH_FORWARD);
1508 	return (data);
1509 }
1510 
1511 static struct dyn_ipv4_state *
1512 dyn_alloc_ipv4_state(const struct ipfw_flow_id *pkt, uint16_t kidx,
1513     uint8_t type)
1514 {
1515 	struct dyn_ipv4_state *s;
1516 
1517 	s = uma_zalloc(V_dyn_ipv4_zone, M_NOWAIT | M_ZERO);
1518 	if (s == NULL)
1519 		return (NULL);
1520 
1521 	s->type = type;
1522 	s->kidx = kidx;
1523 	s->proto = pkt->proto;
1524 	s->sport = pkt->src_port;
1525 	s->dport = pkt->dst_port;
1526 	s->src = pkt->src_ip;
1527 	s->dst = pkt->dst_ip;
1528 	return (s);
1529 }
1530 
1531 /*
1532  * Add IPv4 parent state.
1533  * Returns pointer to parent state. When it is not NULL we are in
1534  * critical section and pointer protected by hazard pointer.
1535  * When some error occurs, it returns NULL and exit from critical section
1536  * is not needed.
1537  */
1538 static struct dyn_ipv4_state *
1539 dyn_add_ipv4_parent(void *rule, uint32_t ruleid, uint16_t rulenum,
1540     const struct ipfw_flow_id *pkt, uint32_t hashval, uint32_t version,
1541     uint16_t kidx)
1542 {
1543 	struct dyn_ipv4_state *s;
1544 	struct dyn_parent *limit;
1545 	uint32_t bucket;
1546 
1547 	bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1548 	DYN_BUCKET_LOCK(bucket);
1549 	if (version != DYN_BUCKET_VERSION(bucket, ipv4_parent_add)) {
1550 		/*
1551 		 * Bucket version has been changed since last lookup,
1552 		 * do lookup again to be sure that state does not exist.
1553 		 */
1554 		s = dyn_lookup_ipv4_parent_locked(pkt, rule, ruleid,
1555 		    rulenum, bucket);
1556 		if (s != NULL) {
1557 			/*
1558 			 * Simultaneous thread has already created this
1559 			 * state. Just return it.
1560 			 */
1561 			DYNSTATE_CRITICAL_ENTER();
1562 			DYNSTATE_PROTECT(s);
1563 			DYN_BUCKET_UNLOCK(bucket);
1564 			return (s);
1565 		}
1566 	}
1567 
1568 	limit = dyn_alloc_parent(rule, ruleid, rulenum, hashval);
1569 	if (limit == NULL) {
1570 		DYN_BUCKET_UNLOCK(bucket);
1571 		return (NULL);
1572 	}
1573 
1574 	s = dyn_alloc_ipv4_state(pkt, kidx, O_LIMIT_PARENT);
1575 	if (s == NULL) {
1576 		DYN_BUCKET_UNLOCK(bucket);
1577 		uma_zfree(V_dyn_parent_zone, limit);
1578 		return (NULL);
1579 	}
1580 
1581 	s->limit = limit;
1582 	CK_SLIST_INSERT_HEAD(&V_dyn_ipv4_parent[bucket], s, entry);
1583 	DYN_COUNT_INC(dyn_parent_count);
1584 	DYN_BUCKET_VERSION_BUMP(bucket, ipv4_parent_add);
1585 	DYNSTATE_CRITICAL_ENTER();
1586 	DYNSTATE_PROTECT(s);
1587 	DYN_BUCKET_UNLOCK(bucket);
1588 	return (s);
1589 }
1590 
1591 static int
1592 dyn_add_ipv4_state(void *parent, uint32_t ruleid, uint16_t rulenum,
1593     const struct ipfw_flow_id *pkt, const void *ulp, int pktlen,
1594     uint32_t hashval, struct ipfw_dyn_info *info, uint16_t fibnum,
1595     uint16_t kidx, uint8_t type)
1596 {
1597 	struct dyn_ipv4_state *s;
1598 	void *data;
1599 	uint32_t bucket;
1600 
1601 	bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1602 	DYN_BUCKET_LOCK(bucket);
1603 	if (info->direction == MATCH_UNKNOWN ||
1604 	    info->kidx != kidx ||
1605 	    info->hashval != hashval ||
1606 	    info->version != DYN_BUCKET_VERSION(bucket, ipv4_add)) {
1607 		/*
1608 		 * Bucket version has been changed since last lookup,
1609 		 * do lookup again to be sure that state does not exist.
1610 		 */
1611 		if (dyn_lookup_ipv4_state_locked(pkt, ulp, pktlen,
1612 		    bucket, kidx) != 0) {
1613 			DYN_BUCKET_UNLOCK(bucket);
1614 			return (EEXIST);
1615 		}
1616 	}
1617 
1618 	data = dyn_alloc_dyndata(parent, ruleid, rulenum, pkt, ulp,
1619 	    pktlen, hashval, fibnum);
1620 	if (data == NULL) {
1621 		DYN_BUCKET_UNLOCK(bucket);
1622 		return (ENOMEM);
1623 	}
1624 
1625 	s = dyn_alloc_ipv4_state(pkt, kidx, type);
1626 	if (s == NULL) {
1627 		DYN_BUCKET_UNLOCK(bucket);
1628 		uma_zfree(V_dyn_data_zone, data);
1629 		return (ENOMEM);
1630 	}
1631 
1632 	s->data = data;
1633 	CK_SLIST_INSERT_HEAD(&V_dyn_ipv4[bucket], s, entry);
1634 	DYN_COUNT_INC(dyn_count);
1635 	DYN_BUCKET_VERSION_BUMP(bucket, ipv4_add);
1636 	DYN_BUCKET_UNLOCK(bucket);
1637 	return (0);
1638 }
1639 
1640 #ifdef INET6
1641 static struct dyn_ipv6_state *
1642 dyn_alloc_ipv6_state(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1643     uint16_t kidx, uint8_t type)
1644 {
1645 	struct dyn_ipv6_state *s;
1646 
1647 	s = uma_zalloc(V_dyn_ipv6_zone, M_NOWAIT | M_ZERO);
1648 	if (s == NULL)
1649 		return (NULL);
1650 
1651 	s->type = type;
1652 	s->kidx = kidx;
1653 	s->zoneid = zoneid;
1654 	s->proto = pkt->proto;
1655 	s->sport = pkt->src_port;
1656 	s->dport = pkt->dst_port;
1657 	s->src = pkt->src_ip6;
1658 	s->dst = pkt->dst_ip6;
1659 	return (s);
1660 }
1661 
1662 /*
1663  * Add IPv6 parent state.
1664  * Returns pointer to parent state. When it is not NULL we are in
1665  * critical section and pointer protected by hazard pointer.
1666  * When some error occurs, it return NULL and exit from critical section
1667  * is not needed.
1668  */
1669 static struct dyn_ipv6_state *
1670 dyn_add_ipv6_parent(void *rule, uint32_t ruleid, uint16_t rulenum,
1671     const struct ipfw_flow_id *pkt, uint32_t zoneid, uint32_t hashval,
1672     uint32_t version, uint16_t kidx)
1673 {
1674 	struct dyn_ipv6_state *s;
1675 	struct dyn_parent *limit;
1676 	uint32_t bucket;
1677 
1678 	bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1679 	DYN_BUCKET_LOCK(bucket);
1680 	if (version != DYN_BUCKET_VERSION(bucket, ipv6_parent_add)) {
1681 		/*
1682 		 * Bucket version has been changed since last lookup,
1683 		 * do lookup again to be sure that state does not exist.
1684 		 */
1685 		s = dyn_lookup_ipv6_parent_locked(pkt, zoneid, rule, ruleid,
1686 		    rulenum, bucket);
1687 		if (s != NULL) {
1688 			/*
1689 			 * Simultaneous thread has already created this
1690 			 * state. Just return it.
1691 			 */
1692 			DYNSTATE_CRITICAL_ENTER();
1693 			DYNSTATE_PROTECT(s);
1694 			DYN_BUCKET_UNLOCK(bucket);
1695 			return (s);
1696 		}
1697 	}
1698 
1699 	limit = dyn_alloc_parent(rule, ruleid, rulenum, hashval);
1700 	if (limit == NULL) {
1701 		DYN_BUCKET_UNLOCK(bucket);
1702 		return (NULL);
1703 	}
1704 
1705 	s = dyn_alloc_ipv6_state(pkt, zoneid, kidx, O_LIMIT_PARENT);
1706 	if (s == NULL) {
1707 		DYN_BUCKET_UNLOCK(bucket);
1708 		uma_zfree(V_dyn_parent_zone, limit);
1709 		return (NULL);
1710 	}
1711 
1712 	s->limit = limit;
1713 	CK_SLIST_INSERT_HEAD(&V_dyn_ipv6_parent[bucket], s, entry);
1714 	DYN_COUNT_INC(dyn_parent_count);
1715 	DYN_BUCKET_VERSION_BUMP(bucket, ipv6_parent_add);
1716 	DYNSTATE_CRITICAL_ENTER();
1717 	DYNSTATE_PROTECT(s);
1718 	DYN_BUCKET_UNLOCK(bucket);
1719 	return (s);
1720 }
1721 
1722 static int
1723 dyn_add_ipv6_state(void *parent, uint32_t ruleid, uint16_t rulenum,
1724     const struct ipfw_flow_id *pkt, uint32_t zoneid, const void *ulp,
1725     int pktlen, uint32_t hashval, struct ipfw_dyn_info *info,
1726     uint16_t fibnum, uint16_t kidx, uint8_t type)
1727 {
1728 	struct dyn_ipv6_state *s;
1729 	struct dyn_data *data;
1730 	uint32_t bucket;
1731 
1732 	bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1733 	DYN_BUCKET_LOCK(bucket);
1734 	if (info->direction == MATCH_UNKNOWN ||
1735 	    info->kidx != kidx ||
1736 	    info->hashval != hashval ||
1737 	    info->version != DYN_BUCKET_VERSION(bucket, ipv6_add)) {
1738 		/*
1739 		 * Bucket version has been changed since last lookup,
1740 		 * do lookup again to be sure that state does not exist.
1741 		 */
1742 		if (dyn_lookup_ipv6_state_locked(pkt, zoneid, ulp, pktlen,
1743 		    bucket, kidx) != 0) {
1744 			DYN_BUCKET_UNLOCK(bucket);
1745 			return (EEXIST);
1746 		}
1747 	}
1748 
1749 	data = dyn_alloc_dyndata(parent, ruleid, rulenum, pkt, ulp,
1750 	    pktlen, hashval, fibnum);
1751 	if (data == NULL) {
1752 		DYN_BUCKET_UNLOCK(bucket);
1753 		return (ENOMEM);
1754 	}
1755 
1756 	s = dyn_alloc_ipv6_state(pkt, zoneid, kidx, type);
1757 	if (s == NULL) {
1758 		DYN_BUCKET_UNLOCK(bucket);
1759 		uma_zfree(V_dyn_data_zone, data);
1760 		return (ENOMEM);
1761 	}
1762 
1763 	s->data = data;
1764 	CK_SLIST_INSERT_HEAD(&V_dyn_ipv6[bucket], s, entry);
1765 	DYN_COUNT_INC(dyn_count);
1766 	DYN_BUCKET_VERSION_BUMP(bucket, ipv6_add);
1767 	DYN_BUCKET_UNLOCK(bucket);
1768 	return (0);
1769 }
1770 #endif /* INET6 */
1771 
1772 static void *
1773 dyn_get_parent_state(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1774     struct ip_fw *rule, uint32_t hashval, uint32_t limit, uint16_t kidx)
1775 {
1776 	char sbuf[24];
1777 	struct dyn_parent *p;
1778 	void *ret;
1779 	uint32_t bucket, version;
1780 
1781 	p = NULL;
1782 	ret = NULL;
1783 	bucket = DYN_BUCKET(hashval, V_curr_dyn_buckets);
1784 	DYNSTATE_CRITICAL_ENTER();
1785 	if (IS_IP4_FLOW_ID(pkt)) {
1786 		struct dyn_ipv4_state *s;
1787 
1788 		version = DYN_BUCKET_VERSION(bucket, ipv4_parent_add);
1789 		s = dyn_lookup_ipv4_parent(pkt, rule, rule->id,
1790 		    rule->rulenum, bucket);
1791 		if (s == NULL) {
1792 			/*
1793 			 * Exit from critical section because dyn_add_parent()
1794 			 * will acquire bucket lock.
1795 			 */
1796 			DYNSTATE_CRITICAL_EXIT();
1797 
1798 			s = dyn_add_ipv4_parent(rule, rule->id,
1799 			    rule->rulenum, pkt, hashval, version, kidx);
1800 			if (s == NULL)
1801 				return (NULL);
1802 			/* Now we are in critical section again. */
1803 		}
1804 		ret = s;
1805 		p = s->limit;
1806 	}
1807 #ifdef INET6
1808 	else if (IS_IP6_FLOW_ID(pkt)) {
1809 		struct dyn_ipv6_state *s;
1810 
1811 		version = DYN_BUCKET_VERSION(bucket, ipv6_parent_add);
1812 		s = dyn_lookup_ipv6_parent(pkt, zoneid, rule, rule->id,
1813 		    rule->rulenum, bucket);
1814 		if (s == NULL) {
1815 			/*
1816 			 * Exit from critical section because dyn_add_parent()
1817 			 * can acquire bucket mutex.
1818 			 */
1819 			DYNSTATE_CRITICAL_EXIT();
1820 
1821 			s = dyn_add_ipv6_parent(rule, rule->id,
1822 			    rule->rulenum, pkt, zoneid, hashval, version,
1823 			    kidx);
1824 			if (s == NULL)
1825 				return (NULL);
1826 			/* Now we are in critical section again. */
1827 		}
1828 		ret = s;
1829 		p = s->limit;
1830 	}
1831 #endif
1832 	else {
1833 		DYNSTATE_CRITICAL_EXIT();
1834 		return (NULL);
1835 	}
1836 
1837 	/* Check the limit */
1838 	if (DPARENT_COUNT(p) >= limit) {
1839 		DYNSTATE_CRITICAL_EXIT();
1840 		if (V_fw_verbose && last_log != time_uptime) {
1841 			last_log = time_uptime;
1842 			snprintf(sbuf, sizeof(sbuf), "%u drop session",
1843 			    rule->rulenum);
1844 			print_dyn_rule_flags(pkt, O_LIMIT,
1845 			    LOG_SECURITY | LOG_DEBUG, sbuf,
1846 			    "too many entries");
1847 		}
1848 		return (NULL);
1849 	}
1850 
1851 	/* Take new session into account. */
1852 	DPARENT_COUNT_INC(p);
1853 	/*
1854 	 * We must exit from critical section because the following code
1855 	 * can acquire bucket mutex.
1856 	 * We rely on the the 'count' field. The state will not expire
1857 	 * until it has some child states, i.e. 'count' field is not zero.
1858 	 * Return state pointer, it will be used by child states as parent.
1859 	 */
1860 	DYNSTATE_CRITICAL_EXIT();
1861 	return (ret);
1862 }
1863 
1864 static int
1865 dyn_install_state(const struct ipfw_flow_id *pkt, uint32_t zoneid,
1866     uint16_t fibnum, const void *ulp, int pktlen, struct ip_fw *rule,
1867     struct ipfw_dyn_info *info, uint32_t limit, uint16_t limit_mask,
1868     uint16_t kidx, uint8_t type)
1869 {
1870 	struct ipfw_flow_id id;
1871 	uint32_t hashval, parent_hashval, ruleid, rulenum;
1872 	int ret;
1873 
1874 	MPASS(type == O_LIMIT || type == O_KEEP_STATE);
1875 
1876 	ruleid = rule->id;
1877 	rulenum = rule->rulenum;
1878 	if (type == O_LIMIT) {
1879 		/* Create masked flow id and calculate bucket */
1880 		id.addr_type = pkt->addr_type;
1881 		id.proto = pkt->proto;
1882 		id.fib = fibnum; /* unused */
1883 		id.src_port = (limit_mask & DYN_SRC_PORT) ?
1884 		    pkt->src_port: 0;
1885 		id.dst_port = (limit_mask & DYN_DST_PORT) ?
1886 		    pkt->dst_port: 0;
1887 		if (IS_IP4_FLOW_ID(pkt)) {
1888 			id.src_ip = (limit_mask & DYN_SRC_ADDR) ?
1889 			    pkt->src_ip: 0;
1890 			id.dst_ip = (limit_mask & DYN_DST_ADDR) ?
1891 			    pkt->dst_ip: 0;
1892 		}
1893 #ifdef INET6
1894 		else if (IS_IP6_FLOW_ID(pkt)) {
1895 			if (limit_mask & DYN_SRC_ADDR)
1896 				id.src_ip6 = pkt->src_ip6;
1897 			else
1898 				memset(&id.src_ip6, 0, sizeof(id.src_ip6));
1899 			if (limit_mask & DYN_DST_ADDR)
1900 				id.dst_ip6 = pkt->dst_ip6;
1901 			else
1902 				memset(&id.dst_ip6, 0, sizeof(id.dst_ip6));
1903 		}
1904 #endif
1905 		else
1906 			return (EAFNOSUPPORT);
1907 
1908 		parent_hashval = hash_parent(&id, rule);
1909 		rule = dyn_get_parent_state(&id, zoneid, rule, parent_hashval,
1910 		    limit, kidx);
1911 		if (rule == NULL) {
1912 #if 0
1913 			if (V_fw_verbose && last_log != time_uptime) {
1914 				last_log = time_uptime;
1915 				snprintf(sbuf, sizeof(sbuf),
1916 				    "%u drop session", rule->rulenum);
1917 			print_dyn_rule_flags(pkt, O_LIMIT,
1918 			    LOG_SECURITY | LOG_DEBUG, sbuf,
1919 			    "too many entries");
1920 			}
1921 #endif
1922 			return (EACCES);
1923 		}
1924 		/*
1925 		 * Limit is not reached, create new state.
1926 		 * Now rule points to parent state.
1927 		 */
1928 	}
1929 
1930 	hashval = hash_packet(pkt);
1931 	if (IS_IP4_FLOW_ID(pkt))
1932 		ret = dyn_add_ipv4_state(rule, ruleid, rulenum, pkt,
1933 		    ulp, pktlen, hashval, info, fibnum, kidx, type);
1934 #ifdef INET6
1935 	else if (IS_IP6_FLOW_ID(pkt))
1936 		ret = dyn_add_ipv6_state(rule, ruleid, rulenum, pkt,
1937 		    zoneid, ulp, pktlen, hashval, info, fibnum, kidx, type);
1938 #endif /* INET6 */
1939 	else
1940 		ret = EAFNOSUPPORT;
1941 
1942 	if (type == O_LIMIT) {
1943 		if (ret != 0) {
1944 			/*
1945 			 * We failed to create child state for O_LIMIT
1946 			 * opcode. Since we already counted it in the parent,
1947 			 * we must revert counter back. The 'rule' points to
1948 			 * parent state, use it to get dyn_parent.
1949 			 *
1950 			 * XXXAE: it should be safe to use 'rule' pointer
1951 			 * without extra lookup, parent state is referenced
1952 			 * and should not be freed.
1953 			 */
1954 			if (IS_IP4_FLOW_ID(&id))
1955 				DPARENT_COUNT_DEC(
1956 				    ((struct dyn_ipv4_state *)rule)->limit);
1957 #ifdef INET6
1958 			else if (IS_IP6_FLOW_ID(&id))
1959 				DPARENT_COUNT_DEC(
1960 				    ((struct dyn_ipv6_state *)rule)->limit);
1961 #endif
1962 		}
1963 	}
1964 	/*
1965 	 * EEXIST means that simultaneous thread has created this
1966 	 * state. Consider this as success.
1967 	 *
1968 	 * XXXAE: should we invalidate 'info' content here?
1969 	 */
1970 	if (ret == EEXIST)
1971 		return (0);
1972 	return (ret);
1973 }
1974 
1975 /*
1976  * Install dynamic state.
1977  *  chain - ipfw's instance;
1978  *  rule - the parent rule that installs the state;
1979  *  cmd - opcode that installs the state;
1980  *  args - ipfw arguments;
1981  *  ulp - upper level protocol header;
1982  *  pktlen - packet length;
1983  *  info - dynamic state lookup info;
1984  *  tablearg - tablearg id.
1985  *
1986  * Returns non-zero value (failure) if state is not installed because
1987  * of errors or because session limitations are enforced.
1988  */
1989 int
1990 ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule,
1991     const ipfw_insn_limit *cmd, const struct ip_fw_args *args,
1992     const void *ulp, int pktlen, struct ipfw_dyn_info *info,
1993     uint32_t tablearg)
1994 {
1995 	uint32_t limit;
1996 	uint16_t limit_mask;
1997 
1998 	if (cmd->o.opcode == O_LIMIT) {
1999 		limit = IP_FW_ARG_TABLEARG(chain, cmd->conn_limit, limit);
2000 		limit_mask = cmd->limit_mask;
2001 	} else {
2002 		limit = 0;
2003 		limit_mask = 0;
2004 	}
2005 	return (dyn_install_state(&args->f_id,
2006 #ifdef INET6
2007 	    IS_IP6_FLOW_ID(&args->f_id) ? dyn_getscopeid(args):
2008 #endif
2009 	    0, M_GETFIB(args->m), ulp, pktlen, rule, info, limit,
2010 	    limit_mask, cmd->o.arg1, cmd->o.opcode));
2011 }
2012 
2013 /*
2014  * Free safe to remove state entries from expired lists.
2015  */
2016 static void
2017 dyn_free_states(struct ip_fw_chain *chain)
2018 {
2019 	struct dyn_ipv4_state *s4, *s4n;
2020 #ifdef INET6
2021 	struct dyn_ipv6_state *s6, *s6n;
2022 #endif
2023 	int cached_count, i;
2024 
2025 	/*
2026 	 * We keep pointers to objects that are in use on each CPU
2027 	 * in the per-cpu dyn_hp pointer. When object is going to be
2028 	 * removed, first of it is unlinked from the corresponding
2029 	 * list. This leads to changing of dyn_bucket_xxx_delver version.
2030 	 * Unlinked objects is placed into corresponding dyn_expired_xxx
2031 	 * list. Reader that is going to dereference object pointer checks
2032 	 * dyn_bucket_xxx_delver version before and after storing pointer
2033 	 * into dyn_hp. If version is the same, the object is protected
2034 	 * from freeing and it is safe to dereference. Othervise reader
2035 	 * tries to iterate list again from the beginning, but this object
2036 	 * now unlinked and thus will not be accessible.
2037 	 *
2038 	 * Copy dyn_hp pointers for each CPU into dyn_hp_cache array.
2039 	 * It does not matter that some pointer can be changed in
2040 	 * time while we are copying. We need to check, that objects
2041 	 * removed in the previous pass are not in use. And if dyn_hp
2042 	 * pointer does not contain it in the time when we are copying,
2043 	 * it will not appear there, because it is already unlinked.
2044 	 * And for new pointers we will not free objects that will be
2045 	 * unlinked in this pass.
2046 	 */
2047 	cached_count = 0;
2048 	CPU_FOREACH(i) {
2049 		dyn_hp_cache[cached_count] = DYNSTATE_GET(i);
2050 		if (dyn_hp_cache[cached_count] != NULL)
2051 			cached_count++;
2052 	}
2053 
2054 	/*
2055 	 * Free expired states that are safe to free.
2056 	 * Check each entry from previous pass in the dyn_expired_xxx
2057 	 * list, if pointer to the object is in the dyn_hp_cache array,
2058 	 * keep it until next pass. Otherwise it is safe to free the
2059 	 * object.
2060 	 *
2061 	 * XXXAE: optimize this to use SLIST_REMOVE_AFTER.
2062 	 */
2063 #define	DYN_FREE_STATES(s, next, name)		do {			\
2064 	s = SLIST_FIRST(&V_dyn_expired_ ## name);			\
2065 	while (s != NULL) {						\
2066 		next = SLIST_NEXT(s, expired);				\
2067 		for (i = 0; i < cached_count; i++)			\
2068 			if (dyn_hp_cache[i] == s)			\
2069 				break;					\
2070 		if (i == cached_count) {				\
2071 			if (s->type == O_LIMIT_PARENT &&		\
2072 			    s->limit->count != 0) {			\
2073 				s = next;				\
2074 				continue;				\
2075 			}						\
2076 			SLIST_REMOVE(&V_dyn_expired_ ## name,		\
2077 			    s, dyn_ ## name ## _state, expired);	\
2078 			if (s->type == O_LIMIT_PARENT)			\
2079 				uma_zfree(V_dyn_parent_zone, s->limit);	\
2080 			else						\
2081 				uma_zfree(V_dyn_data_zone, s->data);	\
2082 			uma_zfree(V_dyn_ ## name ## _zone, s);		\
2083 		}							\
2084 		s = next;						\
2085 	}								\
2086 } while (0)
2087 
2088 	/*
2089 	 * Protect access to expired lists with DYN_EXPIRED_LOCK.
2090 	 * Userland can invoke ipfw_expire_dyn_states() to delete
2091 	 * specific states, this will lead to modification of expired
2092 	 * lists.
2093 	 *
2094 	 * XXXAE: do we need DYN_EXPIRED_LOCK? We can just use
2095 	 *	  IPFW_UH_WLOCK to protect access to these lists.
2096 	 */
2097 	DYN_EXPIRED_LOCK();
2098 	DYN_FREE_STATES(s4, s4n, ipv4);
2099 #ifdef INET6
2100 	DYN_FREE_STATES(s6, s6n, ipv6);
2101 #endif
2102 	DYN_EXPIRED_UNLOCK();
2103 #undef DYN_FREE_STATES
2104 }
2105 
2106 /*
2107  * Returns:
2108  * 0 when state is not matched by specified range;
2109  * 1 when state is matched by specified range;
2110  * 2 when state is matched by specified range and requested deletion of
2111  *   dynamic states.
2112  */
2113 static int
2114 dyn_match_range(uint16_t rulenum, uint8_t set, const ipfw_range_tlv *rt)
2115 {
2116 
2117 	MPASS(rt != NULL);
2118 	/* flush all states */
2119 	if (rt->flags & IPFW_RCFLAG_ALL) {
2120 		if (rt->flags & IPFW_RCFLAG_DYNAMIC)
2121 			return (2); /* forced */
2122 		return (1);
2123 	}
2124 	if ((rt->flags & IPFW_RCFLAG_SET) != 0 && set != rt->set)
2125 		return (0);
2126 	if ((rt->flags & IPFW_RCFLAG_RANGE) != 0 &&
2127 	    (rulenum < rt->start_rule || rulenum > rt->end_rule))
2128 		return (0);
2129 	if (rt->flags & IPFW_RCFLAG_DYNAMIC)
2130 		return (2);
2131 	return (1);
2132 }
2133 
2134 static void
2135 dyn_acquire_rule(struct ip_fw_chain *ch, struct dyn_data *data,
2136     struct ip_fw *rule, uint16_t kidx)
2137 {
2138 	struct dyn_state_obj *obj;
2139 
2140 	/*
2141 	 * Do not acquire reference twice.
2142 	 * This can happen when rule deletion executed for
2143 	 * the same range, but different ruleset id.
2144 	 */
2145 	if (data->flags & DYN_REFERENCED)
2146 		return;
2147 
2148 	IPFW_UH_WLOCK_ASSERT(ch);
2149 	MPASS(kidx != 0);
2150 
2151 	data->flags |= DYN_REFERENCED;
2152 	/* Reference the named object */
2153 	obj = SRV_OBJECT(ch, kidx);
2154 	obj->no.refcnt++;
2155 	MPASS(obj->no.etlv == IPFW_TLV_STATE_NAME);
2156 
2157 	/* Reference the parent rule */
2158 	rule->refcnt++;
2159 }
2160 
2161 static void
2162 dyn_release_rule(struct ip_fw_chain *ch, struct dyn_data *data,
2163     struct ip_fw *rule, uint16_t kidx)
2164 {
2165 	struct dyn_state_obj *obj;
2166 
2167 	IPFW_UH_WLOCK_ASSERT(ch);
2168 	MPASS(kidx != 0);
2169 
2170 	obj = SRV_OBJECT(ch, kidx);
2171 	if (obj->no.refcnt == 1)
2172 		dyn_destroy(ch, &obj->no);
2173 	else
2174 		obj->no.refcnt--;
2175 
2176 	if (--rule->refcnt == 1)
2177 		ipfw_free_rule(rule);
2178 }
2179 
2180 /*
2181  * We do not keep O_LIMIT_PARENT states when V_dyn_keep_states is enabled.
2182  * O_LIMIT state is created when new connection is going to be established
2183  * and there is no matching state. So, since the old parent rule was deleted
2184  * we can't create new states with old parent, and thus we can not account
2185  * new connections with already established connections, and can not do
2186  * proper limiting.
2187  */
2188 static int
2189 dyn_match_ipv4_state(struct ip_fw_chain *ch, struct dyn_ipv4_state *s,
2190     const ipfw_range_tlv *rt)
2191 {
2192 	struct ip_fw *rule;
2193 	int ret;
2194 
2195 	if (s->type == O_LIMIT_PARENT) {
2196 		rule = s->limit->parent;
2197 		return (dyn_match_range(s->limit->rulenum, rule->set, rt));
2198 	}
2199 
2200 	rule = s->data->parent;
2201 	if (s->type == O_LIMIT)
2202 		rule = ((struct dyn_ipv4_state *)rule)->limit->parent;
2203 
2204 	ret = dyn_match_range(s->data->rulenum, rule->set, rt);
2205 	if (ret == 0 || V_dyn_keep_states == 0 || ret > 1)
2206 		return (ret);
2207 
2208 	dyn_acquire_rule(ch, s->data, rule, s->kidx);
2209 	return (0);
2210 }
2211 
2212 #ifdef INET6
2213 static int
2214 dyn_match_ipv6_state(struct ip_fw_chain *ch, struct dyn_ipv6_state *s,
2215     const ipfw_range_tlv *rt)
2216 {
2217 	struct ip_fw *rule;
2218 	int ret;
2219 
2220 	if (s->type == O_LIMIT_PARENT) {
2221 		rule = s->limit->parent;
2222 		return (dyn_match_range(s->limit->rulenum, rule->set, rt));
2223 	}
2224 
2225 	rule = s->data->parent;
2226 	if (s->type == O_LIMIT)
2227 		rule = ((struct dyn_ipv6_state *)rule)->limit->parent;
2228 
2229 	ret = dyn_match_range(s->data->rulenum, rule->set, rt);
2230 	if (ret == 0 || V_dyn_keep_states == 0 || ret > 1)
2231 		return (ret);
2232 
2233 	dyn_acquire_rule(ch, s->data, rule, s->kidx);
2234 	return (0);
2235 }
2236 #endif
2237 
2238 /*
2239  * Unlink expired entries from states lists.
2240  * @rt can be used to specify the range of states for deletion.
2241  */
2242 static void
2243 dyn_expire_states(struct ip_fw_chain *ch, ipfw_range_tlv *rt)
2244 {
2245 	struct dyn_ipv4_slist expired_ipv4;
2246 #ifdef INET6
2247 	struct dyn_ipv6_slist expired_ipv6;
2248 	struct dyn_ipv6_state *s6, *s6n, *s6p;
2249 #endif
2250 	struct dyn_ipv4_state *s4, *s4n, *s4p;
2251 	void *rule;
2252 	int bucket, removed, length, max_length;
2253 
2254 	IPFW_UH_WLOCK_ASSERT(ch);
2255 
2256 	/*
2257 	 * Unlink expired states from each bucket.
2258 	 * With acquired bucket lock iterate entries of each lists:
2259 	 * ipv4, ipv4_parent, ipv6, and ipv6_parent. Check expired time
2260 	 * and unlink entry from the list, link entry into temporary
2261 	 * expired_xxx lists then bump "del" bucket version.
2262 	 *
2263 	 * When an entry is removed, corresponding states counter is
2264 	 * decremented. If entry has O_LIMIT type, parent's reference
2265 	 * counter is decremented.
2266 	 *
2267 	 * NOTE: this function can be called from userspace context
2268 	 * when user deletes rules. In this case all matched states
2269 	 * will be forcedly unlinked. O_LIMIT_PARENT states will be kept
2270 	 * in the expired lists until reference counter become zero.
2271 	 */
2272 #define	DYN_UNLINK_STATES(s, prev, next, exp, af, name, extra)	do {	\
2273 	length = 0;							\
2274 	removed = 0;							\
2275 	prev = NULL;							\
2276 	s = CK_SLIST_FIRST(&V_dyn_ ## name [bucket]);			\
2277 	while (s != NULL) {						\
2278 		next = CK_SLIST_NEXT(s, entry);				\
2279 		if ((TIME_LEQ((s)->exp, time_uptime) && extra) ||	\
2280 		    (rt != NULL &&					\
2281 		     dyn_match_ ## af ## _state(ch, s, rt))) {		\
2282 			if (prev != NULL)				\
2283 				CK_SLIST_REMOVE_AFTER(prev, entry);	\
2284 			else						\
2285 				CK_SLIST_REMOVE_HEAD(			\
2286 				    &V_dyn_ ## name [bucket], entry);	\
2287 			removed++;					\
2288 			SLIST_INSERT_HEAD(&expired_ ## af, s, expired);	\
2289 			if (s->type == O_LIMIT_PARENT)			\
2290 				DYN_COUNT_DEC(dyn_parent_count);	\
2291 			else {						\
2292 				DYN_COUNT_DEC(dyn_count);		\
2293 				if (s->data->flags & DYN_REFERENCED) {	\
2294 					rule = s->data->parent;		\
2295 					if (s->type == O_LIMIT)		\
2296 						rule = ((__typeof(s))	\
2297 						    rule)->limit->parent;\
2298 					dyn_release_rule(ch, s->data,	\
2299 					    rule, s->kidx);		\
2300 				}					\
2301 				if (s->type == O_LIMIT)	{		\
2302 					s = s->data->parent;		\
2303 					DPARENT_COUNT_DEC(s->limit);	\
2304 				}					\
2305 			}						\
2306 		} else {						\
2307 			prev = s;					\
2308 			length++;					\
2309 		}							\
2310 		s = next;						\
2311 	}								\
2312 	if (removed != 0)						\
2313 		DYN_BUCKET_VERSION_BUMP(bucket, name ## _del);		\
2314 	if (length > max_length)				\
2315 		max_length = length;				\
2316 } while (0)
2317 
2318 	SLIST_INIT(&expired_ipv4);
2319 #ifdef INET6
2320 	SLIST_INIT(&expired_ipv6);
2321 #endif
2322 	max_length = 0;
2323 	for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2324 		DYN_BUCKET_LOCK(bucket);
2325 		DYN_UNLINK_STATES(s4, s4p, s4n, data->expire, ipv4, ipv4, 1);
2326 		DYN_UNLINK_STATES(s4, s4p, s4n, limit->expire, ipv4,
2327 		    ipv4_parent, (s4->limit->count == 0));
2328 #ifdef INET6
2329 		DYN_UNLINK_STATES(s6, s6p, s6n, data->expire, ipv6, ipv6, 1);
2330 		DYN_UNLINK_STATES(s6, s6p, s6n, limit->expire, ipv6,
2331 		    ipv6_parent, (s6->limit->count == 0));
2332 #endif
2333 		DYN_BUCKET_UNLOCK(bucket);
2334 	}
2335 	/* Update curr_max_length for statistics. */
2336 	V_curr_max_length = max_length;
2337 	/*
2338 	 * Concatenate temporary lists with global expired lists.
2339 	 */
2340 	DYN_EXPIRED_LOCK();
2341 	SLIST_CONCAT(&V_dyn_expired_ipv4, &expired_ipv4,
2342 	    dyn_ipv4_state, expired);
2343 #ifdef INET6
2344 	SLIST_CONCAT(&V_dyn_expired_ipv6, &expired_ipv6,
2345 	    dyn_ipv6_state, expired);
2346 #endif
2347 	DYN_EXPIRED_UNLOCK();
2348 #undef DYN_UNLINK_STATES
2349 #undef DYN_UNREF_STATES
2350 }
2351 
2352 static struct mbuf *
2353 dyn_mgethdr(int len, uint16_t fibnum)
2354 {
2355 	struct mbuf *m;
2356 
2357 	m = m_gethdr(M_NOWAIT, MT_DATA);
2358 	if (m == NULL)
2359 		return (NULL);
2360 #ifdef MAC
2361 	mac_netinet_firewall_send(m);
2362 #endif
2363 	M_SETFIB(m, fibnum);
2364 	m->m_data += max_linkhdr;
2365 	m->m_flags |= M_SKIP_FIREWALL;
2366 	m->m_len = m->m_pkthdr.len = len;
2367 	bzero(m->m_data, len);
2368 	return (m);
2369 }
2370 
2371 static void
2372 dyn_make_keepalive_ipv4(struct mbuf *m, in_addr_t src, in_addr_t dst,
2373     uint32_t seq, uint32_t ack, uint16_t sport, uint16_t dport)
2374 {
2375 	struct tcphdr *tcp;
2376 	struct ip *ip;
2377 
2378 	ip = mtod(m, struct ip *);
2379 	ip->ip_v = 4;
2380 	ip->ip_hl = sizeof(*ip) >> 2;
2381 	ip->ip_tos = IPTOS_LOWDELAY;
2382 	ip->ip_len = htons(m->m_len);
2383 	ip->ip_off |= htons(IP_DF);
2384 	ip->ip_ttl = V_ip_defttl;
2385 	ip->ip_p = IPPROTO_TCP;
2386 	ip->ip_src.s_addr = htonl(src);
2387 	ip->ip_dst.s_addr = htonl(dst);
2388 
2389 	tcp = mtodo(m, sizeof(struct ip));
2390 	tcp->th_sport = htons(sport);
2391 	tcp->th_dport = htons(dport);
2392 	tcp->th_off = sizeof(struct tcphdr) >> 2;
2393 	tcp->th_seq = htonl(seq);
2394 	tcp->th_ack = htonl(ack);
2395 	tcp->th_flags = TH_ACK;
2396 	tcp->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
2397 	    htons(sizeof(struct tcphdr) + IPPROTO_TCP));
2398 
2399 	m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2400 	m->m_pkthdr.csum_flags = CSUM_TCP;
2401 }
2402 
2403 static void
2404 dyn_enqueue_keepalive_ipv4(struct mbufq *q, const struct dyn_ipv4_state *s)
2405 {
2406 	struct mbuf *m;
2407 
2408 	if ((s->data->state & ACK_FWD) == 0 && s->data->ack_fwd > 0) {
2409 		m = dyn_mgethdr(sizeof(struct ip) + sizeof(struct tcphdr),
2410 		    s->data->fibnum);
2411 		if (m != NULL) {
2412 			dyn_make_keepalive_ipv4(m, s->dst, s->src,
2413 			    s->data->ack_fwd - 1, s->data->ack_rev,
2414 			    s->dport, s->sport);
2415 			if (mbufq_enqueue(q, m)) {
2416 				m_freem(m);
2417 				log(LOG_DEBUG, "ipfw: limit for IPv4 "
2418 				    "keepalive queue is reached.\n");
2419 				return;
2420 			}
2421 		}
2422 	}
2423 
2424 	if ((s->data->state & ACK_REV) == 0 && s->data->ack_rev > 0) {
2425 		m = dyn_mgethdr(sizeof(struct ip) + sizeof(struct tcphdr),
2426 		    s->data->fibnum);
2427 		if (m != NULL) {
2428 			dyn_make_keepalive_ipv4(m, s->src, s->dst,
2429 			    s->data->ack_rev - 1, s->data->ack_fwd,
2430 			    s->sport, s->dport);
2431 			if (mbufq_enqueue(q, m)) {
2432 				m_freem(m);
2433 				log(LOG_DEBUG, "ipfw: limit for IPv4 "
2434 				    "keepalive queue is reached.\n");
2435 				return;
2436 			}
2437 		}
2438 	}
2439 }
2440 
2441 /*
2442  * Prepare and send keep-alive packets.
2443  */
2444 static void
2445 dyn_send_keepalive_ipv4(struct ip_fw_chain *chain)
2446 {
2447 	struct mbufq q;
2448 	struct mbuf *m;
2449 	struct dyn_ipv4_state *s;
2450 	uint32_t bucket;
2451 
2452 	mbufq_init(&q, INT_MAX);
2453 	IPFW_UH_RLOCK(chain);
2454 	/*
2455 	 * It is safe to not use hazard pointer and just do lockless
2456 	 * access to the lists, because states entries can not be deleted
2457 	 * while we hold IPFW_UH_RLOCK.
2458 	 */
2459 	for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2460 		CK_SLIST_FOREACH(s, &V_dyn_ipv4[bucket], entry) {
2461 			/*
2462 			 * Only established TCP connections that will
2463 			 * become expired withing dyn_keepalive_interval.
2464 			 */
2465 			if (s->proto != IPPROTO_TCP ||
2466 			    (s->data->state & BOTH_SYN) != BOTH_SYN ||
2467 			    TIME_LEQ(time_uptime + V_dyn_keepalive_interval,
2468 				s->data->expire))
2469 				continue;
2470 			dyn_enqueue_keepalive_ipv4(&q, s);
2471 		}
2472 	}
2473 	IPFW_UH_RUNLOCK(chain);
2474 	while ((m = mbufq_dequeue(&q)) != NULL)
2475 		ip_output(m, NULL, NULL, 0, NULL, NULL);
2476 }
2477 
2478 #ifdef INET6
2479 static void
2480 dyn_make_keepalive_ipv6(struct mbuf *m, const struct in6_addr *src,
2481     const struct in6_addr *dst, uint32_t zoneid, uint32_t seq, uint32_t ack,
2482     uint16_t sport, uint16_t dport)
2483 {
2484 	struct tcphdr *tcp;
2485 	struct ip6_hdr *ip6;
2486 
2487 	ip6 = mtod(m, struct ip6_hdr *);
2488 	ip6->ip6_vfc |= IPV6_VERSION;
2489 	ip6->ip6_plen = htons(sizeof(struct tcphdr));
2490 	ip6->ip6_nxt = IPPROTO_TCP;
2491 	ip6->ip6_hlim = IPV6_DEFHLIM;
2492 	ip6->ip6_src = *src;
2493 	if (IN6_IS_ADDR_LINKLOCAL(src))
2494 		ip6->ip6_src.s6_addr16[1] = htons(zoneid & 0xffff);
2495 	ip6->ip6_dst = *dst;
2496 	if (IN6_IS_ADDR_LINKLOCAL(dst))
2497 		ip6->ip6_dst.s6_addr16[1] = htons(zoneid & 0xffff);
2498 
2499 	tcp = mtodo(m, sizeof(struct ip6_hdr));
2500 	tcp->th_sport = htons(sport);
2501 	tcp->th_dport = htons(dport);
2502 	tcp->th_off = sizeof(struct tcphdr) >> 2;
2503 	tcp->th_seq = htonl(seq);
2504 	tcp->th_ack = htonl(ack);
2505 	tcp->th_flags = TH_ACK;
2506 	tcp->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr),
2507 	    IPPROTO_TCP, 0);
2508 
2509 	m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
2510 	m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
2511 }
2512 
2513 static void
2514 dyn_enqueue_keepalive_ipv6(struct mbufq *q, const struct dyn_ipv6_state *s)
2515 {
2516 	struct mbuf *m;
2517 
2518 	if ((s->data->state & ACK_FWD) == 0 && s->data->ack_fwd > 0) {
2519 		m = dyn_mgethdr(sizeof(struct ip6_hdr) +
2520 		    sizeof(struct tcphdr), s->data->fibnum);
2521 		if (m != NULL) {
2522 			dyn_make_keepalive_ipv6(m, &s->dst, &s->src,
2523 			    s->zoneid, s->data->ack_fwd - 1, s->data->ack_rev,
2524 			    s->dport, s->sport);
2525 			if (mbufq_enqueue(q, m)) {
2526 				m_freem(m);
2527 				log(LOG_DEBUG, "ipfw: limit for IPv6 "
2528 				    "keepalive queue is reached.\n");
2529 				return;
2530 			}
2531 		}
2532 	}
2533 
2534 	if ((s->data->state & ACK_REV) == 0 && s->data->ack_rev > 0) {
2535 		m = dyn_mgethdr(sizeof(struct ip6_hdr) +
2536 		    sizeof(struct tcphdr), s->data->fibnum);
2537 		if (m != NULL) {
2538 			dyn_make_keepalive_ipv6(m, &s->src, &s->dst,
2539 			    s->zoneid, s->data->ack_rev - 1, s->data->ack_fwd,
2540 			    s->sport, s->dport);
2541 			if (mbufq_enqueue(q, m)) {
2542 				m_freem(m);
2543 				log(LOG_DEBUG, "ipfw: limit for IPv6 "
2544 				    "keepalive queue is reached.\n");
2545 				return;
2546 			}
2547 		}
2548 	}
2549 }
2550 
2551 static void
2552 dyn_send_keepalive_ipv6(struct ip_fw_chain *chain)
2553 {
2554 	struct mbufq q;
2555 	struct mbuf *m;
2556 	struct dyn_ipv6_state *s;
2557 	uint32_t bucket;
2558 
2559 	mbufq_init(&q, INT_MAX);
2560 	IPFW_UH_RLOCK(chain);
2561 	/*
2562 	 * It is safe to not use hazard pointer and just do lockless
2563 	 * access to the lists, because states entries can not be deleted
2564 	 * while we hold IPFW_UH_RLOCK.
2565 	 */
2566 	for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2567 		CK_SLIST_FOREACH(s, &V_dyn_ipv6[bucket], entry) {
2568 			/*
2569 			 * Only established TCP connections that will
2570 			 * become expired withing dyn_keepalive_interval.
2571 			 */
2572 			if (s->proto != IPPROTO_TCP ||
2573 			    (s->data->state & BOTH_SYN) != BOTH_SYN ||
2574 			    TIME_LEQ(time_uptime + V_dyn_keepalive_interval,
2575 				s->data->expire))
2576 				continue;
2577 			dyn_enqueue_keepalive_ipv6(&q, s);
2578 		}
2579 	}
2580 	IPFW_UH_RUNLOCK(chain);
2581 	while ((m = mbufq_dequeue(&q)) != NULL)
2582 		ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
2583 }
2584 #endif /* INET6 */
2585 
2586 static void
2587 dyn_grow_hashtable(struct ip_fw_chain *chain, uint32_t new)
2588 {
2589 #ifdef INET6
2590 	struct dyn_ipv6ck_slist *ipv6, *ipv6_parent;
2591 	uint32_t *ipv6_add, *ipv6_del, *ipv6_parent_add, *ipv6_parent_del;
2592 	struct dyn_ipv6_state *s6;
2593 #endif
2594 	struct dyn_ipv4ck_slist *ipv4, *ipv4_parent;
2595 	uint32_t *ipv4_add, *ipv4_del, *ipv4_parent_add, *ipv4_parent_del;
2596 	struct dyn_ipv4_state *s4;
2597 	struct mtx *bucket_lock;
2598 	void *tmp;
2599 	uint32_t bucket;
2600 
2601 	MPASS(powerof2(new));
2602 	DYN_DEBUG("grow hash size %u -> %u", V_curr_dyn_buckets, new);
2603 	/*
2604 	 * Allocate and initialize new lists.
2605 	 * XXXAE: on memory pressure this can disable callout timer.
2606 	 */
2607 	bucket_lock = malloc(new * sizeof(struct mtx), M_IPFW,
2608 	    M_WAITOK | M_ZERO);
2609 	ipv4 = malloc(new * sizeof(struct dyn_ipv4ck_slist), M_IPFW,
2610 	    M_WAITOK | M_ZERO);
2611 	ipv4_parent = malloc(new * sizeof(struct dyn_ipv4ck_slist), M_IPFW,
2612 	    M_WAITOK | M_ZERO);
2613 	ipv4_add = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO);
2614 	ipv4_del = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO);
2615 	ipv4_parent_add = malloc(new * sizeof(uint32_t), M_IPFW,
2616 	    M_WAITOK | M_ZERO);
2617 	ipv4_parent_del = malloc(new * sizeof(uint32_t), M_IPFW,
2618 	    M_WAITOK | M_ZERO);
2619 #ifdef INET6
2620 	ipv6 = malloc(new * sizeof(struct dyn_ipv6ck_slist), M_IPFW,
2621 	    M_WAITOK | M_ZERO);
2622 	ipv6_parent = malloc(new * sizeof(struct dyn_ipv6ck_slist), M_IPFW,
2623 	    M_WAITOK | M_ZERO);
2624 	ipv6_add = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO);
2625 	ipv6_del = malloc(new * sizeof(uint32_t), M_IPFW, M_WAITOK | M_ZERO);
2626 	ipv6_parent_add = malloc(new * sizeof(uint32_t), M_IPFW,
2627 	    M_WAITOK | M_ZERO);
2628 	ipv6_parent_del = malloc(new * sizeof(uint32_t), M_IPFW,
2629 	    M_WAITOK | M_ZERO);
2630 #endif
2631 	for (bucket = 0; bucket < new; bucket++) {
2632 		DYN_BUCKET_LOCK_INIT(bucket_lock, bucket);
2633 		CK_SLIST_INIT(&ipv4[bucket]);
2634 		CK_SLIST_INIT(&ipv4_parent[bucket]);
2635 #ifdef INET6
2636 		CK_SLIST_INIT(&ipv6[bucket]);
2637 		CK_SLIST_INIT(&ipv6_parent[bucket]);
2638 #endif
2639 	}
2640 
2641 #define DYN_RELINK_STATES(s, hval, i, head, ohead)	do {		\
2642 	while ((s = CK_SLIST_FIRST(&V_dyn_ ## ohead[i])) != NULL) {	\
2643 		CK_SLIST_REMOVE_HEAD(&V_dyn_ ## ohead[i], entry);	\
2644 		CK_SLIST_INSERT_HEAD(&head[DYN_BUCKET(s->hval, new)],	\
2645 		    s, entry);						\
2646 	}								\
2647 } while (0)
2648 	/*
2649 	 * Prevent rules changing from userland.
2650 	 */
2651 	IPFW_UH_WLOCK(chain);
2652 	/*
2653 	 * Hold traffic processing until we finish resize to
2654 	 * prevent access to states lists.
2655 	 */
2656 	IPFW_WLOCK(chain);
2657 	/* Re-link all dynamic states */
2658 	for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2659 		DYN_RELINK_STATES(s4, data->hashval, bucket, ipv4, ipv4);
2660 		DYN_RELINK_STATES(s4, limit->hashval, bucket, ipv4_parent,
2661 		    ipv4_parent);
2662 #ifdef INET6
2663 		DYN_RELINK_STATES(s6, data->hashval, bucket, ipv6, ipv6);
2664 		DYN_RELINK_STATES(s6, limit->hashval, bucket, ipv6_parent,
2665 		    ipv6_parent);
2666 #endif
2667 	}
2668 
2669 #define	DYN_SWAP_PTR(old, new, tmp)	do {		\
2670 	tmp = old;					\
2671 	old = new;					\
2672 	new = tmp;					\
2673 } while (0)
2674 	/* Swap pointers */
2675 	DYN_SWAP_PTR(V_dyn_bucket_lock, bucket_lock, tmp);
2676 	DYN_SWAP_PTR(V_dyn_ipv4, ipv4, tmp);
2677 	DYN_SWAP_PTR(V_dyn_ipv4_parent, ipv4_parent, tmp);
2678 	DYN_SWAP_PTR(V_dyn_ipv4_add, ipv4_add, tmp);
2679 	DYN_SWAP_PTR(V_dyn_ipv4_parent_add, ipv4_parent_add, tmp);
2680 	DYN_SWAP_PTR(V_dyn_ipv4_del, ipv4_del, tmp);
2681 	DYN_SWAP_PTR(V_dyn_ipv4_parent_del, ipv4_parent_del, tmp);
2682 
2683 #ifdef INET6
2684 	DYN_SWAP_PTR(V_dyn_ipv6, ipv6, tmp);
2685 	DYN_SWAP_PTR(V_dyn_ipv6_parent, ipv6_parent, tmp);
2686 	DYN_SWAP_PTR(V_dyn_ipv6_add, ipv6_add, tmp);
2687 	DYN_SWAP_PTR(V_dyn_ipv6_parent_add, ipv6_parent_add, tmp);
2688 	DYN_SWAP_PTR(V_dyn_ipv6_del, ipv6_del, tmp);
2689 	DYN_SWAP_PTR(V_dyn_ipv6_parent_del, ipv6_parent_del, tmp);
2690 #endif
2691 	bucket = V_curr_dyn_buckets;
2692 	V_curr_dyn_buckets = new;
2693 
2694 	IPFW_WUNLOCK(chain);
2695 	IPFW_UH_WUNLOCK(chain);
2696 
2697 	/* Release old resources */
2698 	while (bucket-- != 0)
2699 		DYN_BUCKET_LOCK_DESTROY(bucket_lock, bucket);
2700 	free(bucket_lock, M_IPFW);
2701 	free(ipv4, M_IPFW);
2702 	free(ipv4_parent, M_IPFW);
2703 	free(ipv4_add, M_IPFW);
2704 	free(ipv4_parent_add, M_IPFW);
2705 	free(ipv4_del, M_IPFW);
2706 	free(ipv4_parent_del, M_IPFW);
2707 #ifdef INET6
2708 	free(ipv6, M_IPFW);
2709 	free(ipv6_parent, M_IPFW);
2710 	free(ipv6_add, M_IPFW);
2711 	free(ipv6_parent_add, M_IPFW);
2712 	free(ipv6_del, M_IPFW);
2713 	free(ipv6_parent_del, M_IPFW);
2714 #endif
2715 }
2716 
2717 /*
2718  * This function is used to perform various maintenance
2719  * on dynamic hash lists. Currently it is called every second.
2720  */
2721 static void
2722 dyn_tick(void *vnetx)
2723 {
2724 	struct epoch_tracker et;
2725 	uint32_t buckets;
2726 
2727 	CURVNET_SET((struct vnet *)vnetx);
2728 	/*
2729 	 * First free states unlinked in previous passes.
2730 	 */
2731 	dyn_free_states(&V_layer3_chain);
2732 	/*
2733 	 * Now unlink others expired states.
2734 	 * We use IPFW_UH_WLOCK to avoid concurrent call of
2735 	 * dyn_expire_states(). It is the only function that does
2736 	 * deletion of state entries from states lists.
2737 	 */
2738 	IPFW_UH_WLOCK(&V_layer3_chain);
2739 	dyn_expire_states(&V_layer3_chain, NULL);
2740 	IPFW_UH_WUNLOCK(&V_layer3_chain);
2741 	/*
2742 	 * Send keepalives if they are enabled and the time has come.
2743 	 */
2744 	if (V_dyn_keepalive != 0 &&
2745 	    V_dyn_keepalive_last + V_dyn_keepalive_period <= time_uptime) {
2746 		V_dyn_keepalive_last = time_uptime;
2747 		NET_EPOCH_ENTER(et);
2748 		dyn_send_keepalive_ipv4(&V_layer3_chain);
2749 #ifdef INET6
2750 		dyn_send_keepalive_ipv6(&V_layer3_chain);
2751 #endif
2752 		NET_EPOCH_EXIT(et);
2753 	}
2754 	/*
2755 	 * Check if we need to resize the hash:
2756 	 * if current number of states exceeds number of buckets in hash,
2757 	 * and dyn_buckets_max permits to grow the number of buckets, then
2758 	 * do it. Grow hash size to the minimum power of 2 which is bigger
2759 	 * than current states count.
2760 	 */
2761 	if (V_curr_dyn_buckets < V_dyn_buckets_max &&
2762 	    (V_curr_dyn_buckets < V_dyn_count / 2 || (
2763 	    V_curr_dyn_buckets < V_dyn_count && V_curr_max_length > 8))) {
2764 		buckets = 1 << fls(V_dyn_count);
2765 		if (buckets > V_dyn_buckets_max)
2766 			buckets = V_dyn_buckets_max;
2767 		dyn_grow_hashtable(&V_layer3_chain, buckets);
2768 	}
2769 
2770 	callout_reset_on(&V_dyn_timeout, hz, dyn_tick, vnetx, 0);
2771 	CURVNET_RESTORE();
2772 }
2773 
2774 void
2775 ipfw_expire_dyn_states(struct ip_fw_chain *chain, ipfw_range_tlv *rt)
2776 {
2777 	/*
2778 	 * Do not perform any checks if we currently have no dynamic states
2779 	 */
2780 	if (V_dyn_count == 0)
2781 		return;
2782 
2783 	IPFW_UH_WLOCK_ASSERT(chain);
2784 	dyn_expire_states(chain, rt);
2785 }
2786 
2787 /*
2788  * Pass through all states and reset eaction for orphaned rules.
2789  */
2790 void
2791 ipfw_dyn_reset_eaction(struct ip_fw_chain *ch, uint16_t eaction_id,
2792     uint16_t default_id, uint16_t instance_id)
2793 {
2794 #ifdef INET6
2795 	struct dyn_ipv6_state *s6;
2796 #endif
2797 	struct dyn_ipv4_state *s4;
2798 	struct ip_fw *rule;
2799 	uint32_t bucket;
2800 
2801 #define	DYN_RESET_EACTION(s, h, b)					\
2802 	CK_SLIST_FOREACH(s, &V_dyn_ ## h[b], entry) {			\
2803 		if ((s->data->flags & DYN_REFERENCED) == 0)		\
2804 			continue;					\
2805 		rule = s->data->parent;					\
2806 		if (s->type == O_LIMIT)					\
2807 			rule = ((__typeof(s))rule)->limit->parent;	\
2808 		ipfw_reset_eaction(ch, rule, eaction_id,		\
2809 		    default_id, instance_id);				\
2810 	}
2811 
2812 	IPFW_UH_WLOCK_ASSERT(ch);
2813 	if (V_dyn_count == 0)
2814 		return;
2815 	for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2816 		DYN_RESET_EACTION(s4, ipv4, bucket);
2817 #ifdef INET6
2818 		DYN_RESET_EACTION(s6, ipv6, bucket);
2819 #endif
2820 	}
2821 }
2822 
2823 /*
2824  * Returns size of dynamic states in legacy format
2825  */
2826 int
2827 ipfw_dyn_len(void)
2828 {
2829 
2830 	return ((V_dyn_count + V_dyn_parent_count) * sizeof(ipfw_dyn_rule));
2831 }
2832 
2833 /*
2834  * Returns number of dynamic states.
2835  * Marks every named object index used by dynamic states with bit in @bmask.
2836  * Returns number of named objects accounted in bmask via @nocnt.
2837  * Used by dump format v1 (current).
2838  */
2839 uint32_t
2840 ipfw_dyn_get_count(uint32_t *bmask, int *nocnt)
2841 {
2842 #ifdef INET6
2843 	struct dyn_ipv6_state *s6;
2844 #endif
2845 	struct dyn_ipv4_state *s4;
2846 	uint32_t bucket;
2847 
2848 #define	DYN_COUNT_OBJECTS(s, h, b)					\
2849 	CK_SLIST_FOREACH(s, &V_dyn_ ## h[b], entry) {			\
2850 		MPASS(s->kidx != 0);					\
2851 		if (ipfw_mark_object_kidx(bmask, IPFW_TLV_STATE_NAME,	\
2852 		    s->kidx) != 0)					\
2853 			(*nocnt)++;					\
2854 	}
2855 
2856 	IPFW_UH_RLOCK_ASSERT(&V_layer3_chain);
2857 
2858 	/* No need to pass through all the buckets. */
2859 	*nocnt = 0;
2860 	if (V_dyn_count + V_dyn_parent_count == 0)
2861 		return (0);
2862 
2863 	for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
2864 		DYN_COUNT_OBJECTS(s4, ipv4, bucket);
2865 #ifdef INET6
2866 		DYN_COUNT_OBJECTS(s6, ipv6, bucket);
2867 #endif
2868 	}
2869 
2870 	return (V_dyn_count + V_dyn_parent_count);
2871 }
2872 
2873 /*
2874  * Check if rule contains at least one dynamic opcode.
2875  *
2876  * Returns 1 if such opcode is found, 0 otherwise.
2877  */
2878 int
2879 ipfw_is_dyn_rule(struct ip_fw *rule)
2880 {
2881 	int cmdlen, l;
2882 	ipfw_insn *cmd;
2883 
2884 	l = rule->cmd_len;
2885 	cmd = rule->cmd;
2886 	cmdlen = 0;
2887 	for ( ;	l > 0 ; l -= cmdlen, cmd += cmdlen) {
2888 		cmdlen = F_LEN(cmd);
2889 
2890 		switch (cmd->opcode) {
2891 		case O_LIMIT:
2892 		case O_KEEP_STATE:
2893 		case O_PROBE_STATE:
2894 		case O_CHECK_STATE:
2895 			return (1);
2896 		}
2897 	}
2898 
2899 	return (0);
2900 }
2901 
2902 static void
2903 dyn_export_parent(const struct dyn_parent *p, uint16_t kidx, uint8_t set,
2904     ipfw_dyn_rule *dst)
2905 {
2906 
2907 	dst->dyn_type = O_LIMIT_PARENT;
2908 	dst->kidx = kidx;
2909 	dst->count = (uint16_t)DPARENT_COUNT(p);
2910 	dst->expire = TIME_LEQ(p->expire, time_uptime) ?  0:
2911 	    p->expire - time_uptime;
2912 
2913 	/* 'rule' is used to pass up the rule number and set */
2914 	memcpy(&dst->rule, &p->rulenum, sizeof(p->rulenum));
2915 
2916 	/* store set number into high word of dst->rule pointer. */
2917 	memcpy((char *)&dst->rule + sizeof(p->rulenum), &set, sizeof(set));
2918 
2919 	/* unused fields */
2920 	dst->pcnt = 0;
2921 	dst->bcnt = 0;
2922 	dst->parent = NULL;
2923 	dst->state = 0;
2924 	dst->ack_fwd = 0;
2925 	dst->ack_rev = 0;
2926 	dst->bucket = p->hashval;
2927 	/*
2928 	 * The legacy userland code will interpret a NULL here as a marker
2929 	 * for the last dynamic rule.
2930 	 */
2931 	dst->next = (ipfw_dyn_rule *)1;
2932 }
2933 
2934 static void
2935 dyn_export_data(const struct dyn_data *data, uint16_t kidx, uint8_t type,
2936     uint8_t set, ipfw_dyn_rule *dst)
2937 {
2938 
2939 	dst->dyn_type = type;
2940 	dst->kidx = kidx;
2941 	dst->pcnt = data->pcnt_fwd + data->pcnt_rev;
2942 	dst->bcnt = data->bcnt_fwd + data->bcnt_rev;
2943 	dst->expire = TIME_LEQ(data->expire, time_uptime) ?  0:
2944 	    data->expire - time_uptime;
2945 
2946 	/* 'rule' is used to pass up the rule number and set */
2947 	memcpy(&dst->rule, &data->rulenum, sizeof(data->rulenum));
2948 
2949 	/* store set number into high word of dst->rule pointer. */
2950 	memcpy((char *)&dst->rule + sizeof(data->rulenum), &set, sizeof(set));
2951 
2952 	dst->state = data->state;
2953 	if (data->flags & DYN_REFERENCED)
2954 		dst->state |= IPFW_DYN_ORPHANED;
2955 
2956 	/* unused fields */
2957 	dst->parent = NULL;
2958 	dst->ack_fwd = data->ack_fwd;
2959 	dst->ack_rev = data->ack_rev;
2960 	dst->count = 0;
2961 	dst->bucket = data->hashval;
2962 	/*
2963 	 * The legacy userland code will interpret a NULL here as a marker
2964 	 * for the last dynamic rule.
2965 	 */
2966 	dst->next = (ipfw_dyn_rule *)1;
2967 }
2968 
2969 static void
2970 dyn_export_ipv4_state(const struct dyn_ipv4_state *s, ipfw_dyn_rule *dst)
2971 {
2972 	struct ip_fw *rule;
2973 
2974 	switch (s->type) {
2975 	case O_LIMIT_PARENT:
2976 		rule = s->limit->parent;
2977 		dyn_export_parent(s->limit, s->kidx, rule->set, dst);
2978 		break;
2979 	default:
2980 		rule = s->data->parent;
2981 		if (s->type == O_LIMIT)
2982 			rule = ((struct dyn_ipv4_state *)rule)->limit->parent;
2983 		dyn_export_data(s->data, s->kidx, s->type, rule->set, dst);
2984 	}
2985 
2986 	dst->id.dst_ip = s->dst;
2987 	dst->id.src_ip = s->src;
2988 	dst->id.dst_port = s->dport;
2989 	dst->id.src_port = s->sport;
2990 	dst->id.fib = s->data->fibnum;
2991 	dst->id.proto = s->proto;
2992 	dst->id._flags = 0;
2993 	dst->id.addr_type = 4;
2994 
2995 	memset(&dst->id.dst_ip6, 0, sizeof(dst->id.dst_ip6));
2996 	memset(&dst->id.src_ip6, 0, sizeof(dst->id.src_ip6));
2997 	dst->id.flow_id6 = dst->id.extra = 0;
2998 }
2999 
3000 #ifdef INET6
3001 static void
3002 dyn_export_ipv6_state(const struct dyn_ipv6_state *s, ipfw_dyn_rule *dst)
3003 {
3004 	struct ip_fw *rule;
3005 
3006 	switch (s->type) {
3007 	case O_LIMIT_PARENT:
3008 		rule = s->limit->parent;
3009 		dyn_export_parent(s->limit, s->kidx, rule->set, dst);
3010 		break;
3011 	default:
3012 		rule = s->data->parent;
3013 		if (s->type == O_LIMIT)
3014 			rule = ((struct dyn_ipv6_state *)rule)->limit->parent;
3015 		dyn_export_data(s->data, s->kidx, s->type, rule->set, dst);
3016 	}
3017 
3018 	dst->id.src_ip6 = s->src;
3019 	dst->id.dst_ip6 = s->dst;
3020 	dst->id.dst_port = s->dport;
3021 	dst->id.src_port = s->sport;
3022 	dst->id.fib = s->data->fibnum;
3023 	dst->id.proto = s->proto;
3024 	dst->id._flags = 0;
3025 	dst->id.addr_type = 6;
3026 
3027 	dst->id.dst_ip = dst->id.src_ip = 0;
3028 	dst->id.flow_id6 = dst->id.extra = 0;
3029 }
3030 #endif /* INET6 */
3031 
3032 /*
3033  * Fills the buffer given by @sd with dynamic states.
3034  * Used by dump format v1 (current).
3035  *
3036  * Returns 0 on success.
3037  */
3038 int
3039 ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd)
3040 {
3041 #ifdef INET6
3042 	struct dyn_ipv6_state *s6;
3043 #endif
3044 	struct dyn_ipv4_state *s4;
3045 	ipfw_obj_dyntlv *dst, *last;
3046 	ipfw_obj_ctlv *ctlv;
3047 	uint32_t bucket;
3048 
3049 	if (V_dyn_count == 0)
3050 		return (0);
3051 
3052 	/*
3053 	 * IPFW_UH_RLOCK garantees that another userland request
3054 	 * and callout thread will not delete entries from states
3055 	 * lists.
3056 	 */
3057 	IPFW_UH_RLOCK_ASSERT(chain);
3058 
3059 	ctlv = (ipfw_obj_ctlv *)ipfw_get_sopt_space(sd, sizeof(*ctlv));
3060 	if (ctlv == NULL)
3061 		return (ENOMEM);
3062 	ctlv->head.type = IPFW_TLV_DYNSTATE_LIST;
3063 	ctlv->objsize = sizeof(ipfw_obj_dyntlv);
3064 	last = NULL;
3065 
3066 #define	DYN_EXPORT_STATES(s, af, h, b)				\
3067 	CK_SLIST_FOREACH(s, &V_dyn_ ## h[b], entry) {			\
3068 		dst = (ipfw_obj_dyntlv *)ipfw_get_sopt_space(sd,	\
3069 		    sizeof(ipfw_obj_dyntlv));				\
3070 		if (dst == NULL)					\
3071 			return (ENOMEM);				\
3072 		dyn_export_ ## af ## _state(s, &dst->state);		\
3073 		dst->head.length = sizeof(ipfw_obj_dyntlv);		\
3074 		dst->head.type = IPFW_TLV_DYN_ENT;			\
3075 		last = dst;						\
3076 	}
3077 
3078 	for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
3079 		DYN_EXPORT_STATES(s4, ipv4, ipv4_parent, bucket);
3080 		DYN_EXPORT_STATES(s4, ipv4, ipv4, bucket);
3081 #ifdef INET6
3082 		DYN_EXPORT_STATES(s6, ipv6, ipv6_parent, bucket);
3083 		DYN_EXPORT_STATES(s6, ipv6, ipv6, bucket);
3084 #endif /* INET6 */
3085 	}
3086 
3087 	/* mark last dynamic rule */
3088 	if (last != NULL)
3089 		last->head.flags = IPFW_DF_LAST; /* XXX: unused */
3090 	return (0);
3091 #undef DYN_EXPORT_STATES
3092 }
3093 
3094 /*
3095  * Fill given buffer with dynamic states (legacy format).
3096  * IPFW_UH_RLOCK has to be held while calling.
3097  */
3098 void
3099 ipfw_get_dynamic(struct ip_fw_chain *chain, char **pbp, const char *ep)
3100 {
3101 #ifdef INET6
3102 	struct dyn_ipv6_state *s6;
3103 #endif
3104 	struct dyn_ipv4_state *s4;
3105 	ipfw_dyn_rule *p, *last = NULL;
3106 	char *bp;
3107 	uint32_t bucket;
3108 
3109 	if (V_dyn_count == 0)
3110 		return;
3111 	bp = *pbp;
3112 
3113 	IPFW_UH_RLOCK_ASSERT(chain);
3114 
3115 #define	DYN_EXPORT_STATES(s, af, head, b)				\
3116 	CK_SLIST_FOREACH(s, &V_dyn_ ## head[b], entry) {		\
3117 		if (bp + sizeof(*p) > ep)				\
3118 			break;						\
3119 		p = (ipfw_dyn_rule *)bp;				\
3120 		dyn_export_ ## af ## _state(s, p);			\
3121 		last = p;						\
3122 		bp += sizeof(*p);					\
3123 	}
3124 
3125 	for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
3126 		DYN_EXPORT_STATES(s4, ipv4, ipv4_parent, bucket);
3127 		DYN_EXPORT_STATES(s4, ipv4, ipv4, bucket);
3128 #ifdef INET6
3129 		DYN_EXPORT_STATES(s6, ipv6, ipv6_parent, bucket);
3130 		DYN_EXPORT_STATES(s6, ipv6, ipv6, bucket);
3131 #endif /* INET6 */
3132 	}
3133 
3134 	if (last != NULL) /* mark last dynamic rule */
3135 		last->next = NULL;
3136 	*pbp = bp;
3137 #undef DYN_EXPORT_STATES
3138 }
3139 
3140 void
3141 ipfw_dyn_init(struct ip_fw_chain *chain)
3142 {
3143 
3144 #ifdef IPFIREWALL_JENKINSHASH
3145 	V_dyn_hashseed = arc4random();
3146 #endif
3147 	V_dyn_max = 16384;		/* max # of states */
3148 	V_dyn_parent_max = 4096;	/* max # of parent states */
3149 	V_dyn_buckets_max = 8192;	/* must be power of 2 */
3150 
3151 	V_dyn_ack_lifetime = 300;
3152 	V_dyn_syn_lifetime = 20;
3153 	V_dyn_fin_lifetime = 1;
3154 	V_dyn_rst_lifetime = 1;
3155 	V_dyn_udp_lifetime = 10;
3156 	V_dyn_short_lifetime = 5;
3157 
3158 	V_dyn_keepalive_interval = 20;
3159 	V_dyn_keepalive_period = 5;
3160 	V_dyn_keepalive = 1;		/* send keepalives */
3161 	V_dyn_keepalive_last = time_uptime;
3162 
3163 	V_dyn_data_zone = uma_zcreate("IPFW dynamic states data",
3164 	    sizeof(struct dyn_data), NULL, NULL, NULL, NULL,
3165 	    UMA_ALIGN_PTR, 0);
3166 	uma_zone_set_max(V_dyn_data_zone, V_dyn_max);
3167 
3168 	V_dyn_parent_zone = uma_zcreate("IPFW parent dynamic states",
3169 	    sizeof(struct dyn_parent), NULL, NULL, NULL, NULL,
3170 	    UMA_ALIGN_PTR, 0);
3171 	uma_zone_set_max(V_dyn_parent_zone, V_dyn_parent_max);
3172 
3173 	SLIST_INIT(&V_dyn_expired_ipv4);
3174 	V_dyn_ipv4 = NULL;
3175 	V_dyn_ipv4_parent = NULL;
3176 	V_dyn_ipv4_zone = uma_zcreate("IPFW IPv4 dynamic states",
3177 	    sizeof(struct dyn_ipv4_state), NULL, NULL, NULL, NULL,
3178 	    UMA_ALIGN_PTR, 0);
3179 
3180 #ifdef INET6
3181 	SLIST_INIT(&V_dyn_expired_ipv6);
3182 	V_dyn_ipv6 = NULL;
3183 	V_dyn_ipv6_parent = NULL;
3184 	V_dyn_ipv6_zone = uma_zcreate("IPFW IPv6 dynamic states",
3185 	    sizeof(struct dyn_ipv6_state), NULL, NULL, NULL, NULL,
3186 	    UMA_ALIGN_PTR, 0);
3187 #endif
3188 
3189 	/* Initialize buckets. */
3190 	V_curr_dyn_buckets = 0;
3191 	V_dyn_bucket_lock = NULL;
3192 	dyn_grow_hashtable(chain, 256);
3193 
3194 	if (IS_DEFAULT_VNET(curvnet))
3195 		dyn_hp_cache = malloc(mp_ncpus * sizeof(void *), M_IPFW,
3196 		    M_WAITOK | M_ZERO);
3197 
3198 	DYN_EXPIRED_LOCK_INIT();
3199 	callout_init(&V_dyn_timeout, 1);
3200 	callout_reset(&V_dyn_timeout, hz, dyn_tick, curvnet);
3201 	IPFW_ADD_OBJ_REWRITER(IS_DEFAULT_VNET(curvnet), dyn_opcodes);
3202 }
3203 
3204 void
3205 ipfw_dyn_uninit(int pass)
3206 {
3207 #ifdef INET6
3208 	struct dyn_ipv6_state *s6;
3209 #endif
3210 	struct dyn_ipv4_state *s4;
3211 	int bucket;
3212 
3213 	if (pass == 0) {
3214 		callout_drain(&V_dyn_timeout);
3215 		return;
3216 	}
3217 	IPFW_DEL_OBJ_REWRITER(IS_DEFAULT_VNET(curvnet), dyn_opcodes);
3218 	DYN_EXPIRED_LOCK_DESTROY();
3219 
3220 #define	DYN_FREE_STATES_FORCED(CK, s, af, name, en)	do {		\
3221 	while ((s = CK ## SLIST_FIRST(&V_dyn_ ## name)) != NULL) {	\
3222 		CK ## SLIST_REMOVE_HEAD(&V_dyn_ ## name, en);	\
3223 		if (s->type == O_LIMIT_PARENT)				\
3224 			uma_zfree(V_dyn_parent_zone, s->limit);		\
3225 		else							\
3226 			uma_zfree(V_dyn_data_zone, s->data);		\
3227 		uma_zfree(V_dyn_ ## af ## _zone, s);			\
3228 	}								\
3229 } while (0)
3230 	for (bucket = 0; bucket < V_curr_dyn_buckets; bucket++) {
3231 		DYN_BUCKET_LOCK_DESTROY(V_dyn_bucket_lock, bucket);
3232 
3233 		DYN_FREE_STATES_FORCED(CK_, s4, ipv4, ipv4[bucket], entry);
3234 		DYN_FREE_STATES_FORCED(CK_, s4, ipv4, ipv4_parent[bucket],
3235 		    entry);
3236 #ifdef INET6
3237 		DYN_FREE_STATES_FORCED(CK_, s6, ipv6, ipv6[bucket], entry);
3238 		DYN_FREE_STATES_FORCED(CK_, s6, ipv6, ipv6_parent[bucket],
3239 		    entry);
3240 #endif /* INET6 */
3241 	}
3242 	DYN_FREE_STATES_FORCED(, s4, ipv4, expired_ipv4, expired);
3243 #ifdef INET6
3244 	DYN_FREE_STATES_FORCED(, s6, ipv6, expired_ipv6, expired);
3245 #endif
3246 #undef DYN_FREE_STATES_FORCED
3247 
3248 	uma_zdestroy(V_dyn_ipv4_zone);
3249 	uma_zdestroy(V_dyn_data_zone);
3250 	uma_zdestroy(V_dyn_parent_zone);
3251 #ifdef INET6
3252 	uma_zdestroy(V_dyn_ipv6_zone);
3253 	free(V_dyn_ipv6, M_IPFW);
3254 	free(V_dyn_ipv6_parent, M_IPFW);
3255 	free(V_dyn_ipv6_add, M_IPFW);
3256 	free(V_dyn_ipv6_parent_add, M_IPFW);
3257 	free(V_dyn_ipv6_del, M_IPFW);
3258 	free(V_dyn_ipv6_parent_del, M_IPFW);
3259 #endif
3260 	free(V_dyn_bucket_lock, M_IPFW);
3261 	free(V_dyn_ipv4, M_IPFW);
3262 	free(V_dyn_ipv4_parent, M_IPFW);
3263 	free(V_dyn_ipv4_add, M_IPFW);
3264 	free(V_dyn_ipv4_parent_add, M_IPFW);
3265 	free(V_dyn_ipv4_del, M_IPFW);
3266 	free(V_dyn_ipv4_parent_del, M_IPFW);
3267 	if (IS_DEFAULT_VNET(curvnet))
3268 		free(dyn_hp_cache, M_IPFW);
3269 }
3270 
3271 
3272