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