2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
35 * Dynamic rule support for ipfw
41 #error IPFIREWALL requires INET.
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
49 #include <sys/kernel.h>
52 #include <sys/rmlock.h>
53 #include <sys/socket.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 #include <net/ethernet.h> /* for ETHERTYPE_IP */
58 #include <net/if_var.h>
62 #include <netinet/in.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_var.h> /* ip_defttl */
65 #include <netinet/ip_fw.h>
66 #include <netinet/tcp_var.h>
67 #include <netinet/udp.h>
69 #include <netinet/ip6.h> /* IN6_ARE_ADDR_EQUAL */
71 #include <netinet6/in6_var.h>
72 #include <netinet6/ip6_var.h>
75 #include <netpfil/ipfw/ip_fw_private.h>
77 #include <machine/in_cksum.h> /* XXX for in_cksum */
80 #include <security/mac/mac_framework.h>
84 * Description of dynamic rules.
86 * Dynamic rules are stored in lists accessed through a hash table
87 * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
88 * be modified through the sysctl variable dyn_buckets which is
89 * updated when the table becomes empty.
91 * XXX currently there is only one list, ipfw_dyn.
93 * When a packet is received, its address fields are first masked
94 * with the mask defined for the rule, then hashed, then matched
95 * against the entries in the corresponding list.
96 * Dynamic rules can be used for different purposes:
98 * + enforcing limits on the number of sessions;
99 * + in-kernel NAT (not implemented yet)
101 * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
102 * measured in seconds and depending on the flags.
104 * The total number of dynamic rules is equal to UMA zone items count.
105 * The max number of dynamic rules is dyn_max. When we reach
106 * the maximum number of rules we do not create anymore. This is
107 * done to avoid consuming too much memory, but also too much
108 * time when searching on each packet (ideally, we should try instead
109 * to put a limit on the length of the list on each bucket...).
111 * Each dynamic rule holds a pointer to the parent ipfw rule so
112 * we know what action to perform. Dynamic rules are removed when
113 * the parent rule is deleted. This can be changed by dyn_keep_states
116 * There are some limitations with dynamic rules -- we do not
117 * obey the 'randomized match', and we do not do multiple
118 * passes through the firewall. XXX check the latter!!!
121 struct ipfw_dyn_bucket {
122 struct mtx mtx; /* Bucket protecting lock */
123 ipfw_dyn_rule *head; /* Pointer to first rule */
127 * Static variables followed by global ones
129 static VNET_DEFINE(struct ipfw_dyn_bucket *, ipfw_dyn_v);
130 static VNET_DEFINE(u_int32_t, dyn_buckets_max);
131 static VNET_DEFINE(u_int32_t, curr_dyn_buckets);
132 static VNET_DEFINE(struct callout, ipfw_timeout);
133 #define V_ipfw_dyn_v VNET(ipfw_dyn_v)
134 #define V_dyn_buckets_max VNET(dyn_buckets_max)
135 #define V_curr_dyn_buckets VNET(curr_dyn_buckets)
136 #define V_ipfw_timeout VNET(ipfw_timeout)
138 static VNET_DEFINE(uma_zone_t, ipfw_dyn_rule_zone);
139 #define V_ipfw_dyn_rule_zone VNET(ipfw_dyn_rule_zone)
141 #define IPFW_BUCK_LOCK_INIT(b) \
142 mtx_init(&(b)->mtx, "IPFW dynamic bucket", NULL, MTX_DEF)
143 #define IPFW_BUCK_LOCK_DESTROY(b) \
144 mtx_destroy(&(b)->mtx)
145 #define IPFW_BUCK_LOCK(i) mtx_lock(&V_ipfw_dyn_v[(i)].mtx)
146 #define IPFW_BUCK_UNLOCK(i) mtx_unlock(&V_ipfw_dyn_v[(i)].mtx)
147 #define IPFW_BUCK_ASSERT(i) mtx_assert(&V_ipfw_dyn_v[(i)].mtx, MA_OWNED)
150 static VNET_DEFINE(int, dyn_keep_states);
151 #define V_dyn_keep_states VNET(dyn_keep_states)
154 * Timeouts for various events in handing dynamic rules.
156 static VNET_DEFINE(u_int32_t, dyn_ack_lifetime);
157 static VNET_DEFINE(u_int32_t, dyn_syn_lifetime);
158 static VNET_DEFINE(u_int32_t, dyn_fin_lifetime);
159 static VNET_DEFINE(u_int32_t, dyn_rst_lifetime);
160 static VNET_DEFINE(u_int32_t, dyn_udp_lifetime);
161 static VNET_DEFINE(u_int32_t, dyn_short_lifetime);
163 #define V_dyn_ack_lifetime VNET(dyn_ack_lifetime)
164 #define V_dyn_syn_lifetime VNET(dyn_syn_lifetime)
165 #define V_dyn_fin_lifetime VNET(dyn_fin_lifetime)
166 #define V_dyn_rst_lifetime VNET(dyn_rst_lifetime)
167 #define V_dyn_udp_lifetime VNET(dyn_udp_lifetime)
168 #define V_dyn_short_lifetime VNET(dyn_short_lifetime)
171 * Keepalives are sent if dyn_keepalive is set. They are sent every
172 * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
173 * seconds of lifetime of a rule.
174 * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
175 * than dyn_keepalive_period.
178 static VNET_DEFINE(u_int32_t, dyn_keepalive_interval);
179 static VNET_DEFINE(u_int32_t, dyn_keepalive_period);
180 static VNET_DEFINE(u_int32_t, dyn_keepalive);
181 static VNET_DEFINE(time_t, dyn_keepalive_last);
183 #define V_dyn_keepalive_interval VNET(dyn_keepalive_interval)
184 #define V_dyn_keepalive_period VNET(dyn_keepalive_period)
185 #define V_dyn_keepalive VNET(dyn_keepalive)
186 #define V_dyn_keepalive_last VNET(dyn_keepalive_last)
188 static VNET_DEFINE(u_int32_t, dyn_max); /* max # of dynamic rules */
190 #define DYN_COUNT uma_zone_get_cur(V_ipfw_dyn_rule_zone)
191 #define V_dyn_max VNET(dyn_max)
193 /* for userspace, we emulate the uma_zone_counter with ipfw_dyn_count */
194 static int ipfw_dyn_count; /* number of objects */
196 #ifdef USERSPACE /* emulation of UMA object counters for userspace */
197 #define uma_zone_get_cur(x) ipfw_dyn_count
198 #endif /* USERSPACE */
200 static int last_log; /* Log ratelimiting */
202 static void ipfw_dyn_tick(void *vnetx);
203 static void check_dyn_rules(struct ip_fw_chain *, ipfw_range_tlv *, int, int);
206 static int sysctl_ipfw_dyn_count(SYSCTL_HANDLER_ARGS);
207 static int sysctl_ipfw_dyn_max(SYSCTL_HANDLER_ARGS);
211 SYSCTL_DECL(_net_inet_ip_fw);
212 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, dyn_buckets,
213 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_buckets_max), 0,
214 "Max number of dyn. buckets");
215 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets,
216 CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(curr_dyn_buckets), 0,
217 "Current Number of dyn. buckets");
218 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_count,
219 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RD, 0, 0, sysctl_ipfw_dyn_count, "IU",
220 "Number of dyn. rules");
221 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_max,
222 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, 0, 0, sysctl_ipfw_dyn_max, "IU",
223 "Max number of dyn. rules");
224 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime,
225 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_ack_lifetime), 0,
226 "Lifetime of dyn. rules for acks");
227 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime,
228 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_syn_lifetime), 0,
229 "Lifetime of dyn. rules for syn");
230 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime,
231 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_fin_lifetime), 0,
232 "Lifetime of dyn. rules for fin");
233 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime,
234 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_rst_lifetime), 0,
235 "Lifetime of dyn. rules for rst");
236 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime,
237 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_udp_lifetime), 0,
238 "Lifetime of dyn. rules for UDP");
239 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime,
240 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_short_lifetime), 0,
241 "Lifetime of dyn. rules for other situations");
242 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, dyn_keepalive,
243 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_keepalive), 0,
244 "Enable keepalives for dyn. rules");
245 SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, dyn_keep_states,
246 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(dyn_keep_states), 0,
247 "Do not flush dynamic states on rule deletion");
251 #endif /* SYSCTL_NODE */
256 hash_packet6(const struct ipfw_flow_id *id)
259 i = (id->dst_ip6.__u6_addr.__u6_addr32[2]) ^
260 (id->dst_ip6.__u6_addr.__u6_addr32[3]) ^
261 (id->src_ip6.__u6_addr.__u6_addr32[2]) ^
262 (id->src_ip6.__u6_addr.__u6_addr32[3]);
268 * IMPORTANT: the hash function for dynamic rules must be commutative
269 * in source and destination (ip,port), because rules are bidirectional
270 * and we want to find both in the same bucket.
273 hash_packet(const struct ipfw_flow_id *id, int buckets)
278 if (IS_IP6_FLOW_ID(id))
279 i = hash_packet6(id);
282 i = (id->dst_ip) ^ (id->src_ip);
283 i ^= (id->dst_port) ^ (id->src_port);
284 return (i & (buckets - 1));
288 #define DYN_DEBUG(fmt, ...) do { \
289 printf("%s: " fmt "\n", __func__, __VA_ARGS__); \
292 #define DYN_DEBUG(fmt, ...)
295 static char *default_state_name = "default";
296 struct dyn_state_obj {
297 struct named_object no;
301 #define DYN_STATE_OBJ(ch, cmd) \
302 ((struct dyn_state_obj *)SRV_OBJECT(ch, (cmd)->arg1))
304 * Classifier callback.
305 * Return 0 if opcode contains object that should be referenced
309 dyn_classify(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
312 DYN_DEBUG("opcode %d, arg1 %d", cmd->opcode, cmd->arg1);
313 /* Don't rewrite "check-state any" */
314 if (cmd->arg1 == 0 &&
315 cmd->opcode == O_CHECK_STATE)
324 dyn_update(ipfw_insn *cmd, uint16_t idx)
328 DYN_DEBUG("opcode %d, arg1 %d", cmd->opcode, cmd->arg1);
332 dyn_findbyname(struct ip_fw_chain *ch, struct tid_info *ti,
333 struct named_object **pno)
338 DYN_DEBUG("uidx %d", ti->uidx);
340 if (ti->tlvs == NULL)
342 /* Search ntlv in the buffer provided by user */
343 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
344 IPFW_TLV_STATE_NAME);
349 name = default_state_name;
351 * Search named object with corresponding name.
352 * Since states objects are global - ignore the set value
353 * and use zero instead.
355 *pno = ipfw_objhash_lookup_name_type(CHAIN_TO_SRV(ch), 0,
356 IPFW_TLV_STATE_NAME, name);
358 * We always return success here.
359 * The caller will check *pno and mark object as unresolved,
360 * then it will automatically create "default" object.
365 static struct named_object *
366 dyn_findbykidx(struct ip_fw_chain *ch, uint16_t idx)
369 DYN_DEBUG("kidx %d", idx);
370 return (ipfw_objhash_lookup_kidx(CHAIN_TO_SRV(ch), idx));
374 dyn_create(struct ip_fw_chain *ch, struct tid_info *ti,
377 struct namedobj_instance *ni;
378 struct dyn_state_obj *obj;
379 struct named_object *no;
383 DYN_DEBUG("uidx %d", ti->uidx);
385 if (ti->tlvs == NULL)
387 ntlv = ipfw_find_name_tlv_type(ti->tlvs, ti->tlen, ti->uidx,
388 IPFW_TLV_STATE_NAME);
393 name = default_state_name;
395 ni = CHAIN_TO_SRV(ch);
396 obj = malloc(sizeof(*obj), M_IPFW, M_WAITOK | M_ZERO);
397 obj->no.name = obj->name;
398 obj->no.etlv = IPFW_TLV_STATE_NAME;
399 strlcpy(obj->name, name, sizeof(obj->name));
402 no = ipfw_objhash_lookup_name_type(ni, 0,
403 IPFW_TLV_STATE_NAME, name);
406 * Object is already created.
407 * Just return its kidx and bump refcount.
413 DYN_DEBUG("\tfound kidx %d", *pkidx);
416 if (ipfw_objhash_alloc_idx(ni, &obj->no.kidx) != 0) {
417 DYN_DEBUG("\talloc_idx failed for %s", name);
422 ipfw_objhash_add(ni, &obj->no);
423 SRV_OBJECT(ch, obj->no.kidx) = obj;
425 *pkidx = obj->no.kidx;
427 DYN_DEBUG("\tcreated kidx %d", *pkidx);
432 dyn_destroy(struct ip_fw_chain *ch, struct named_object *no)
434 struct dyn_state_obj *obj;
436 IPFW_UH_WLOCK_ASSERT(ch);
438 KASSERT(no->refcnt == 1,
439 ("Destroying object '%s' (type %u, idx %u) with refcnt %u",
440 no->name, no->etlv, no->kidx, no->refcnt));
442 DYN_DEBUG("kidx %d", no->kidx);
443 obj = SRV_OBJECT(ch, no->kidx);
444 SRV_OBJECT(ch, no->kidx) = NULL;
445 ipfw_objhash_del(CHAIN_TO_SRV(ch), no);
446 ipfw_objhash_free_idx(CHAIN_TO_SRV(ch), no->kidx);
451 static struct opcode_obj_rewrite dyn_opcodes[] = {
453 O_KEEP_STATE, IPFW_TLV_STATE_NAME,
454 dyn_classify, dyn_update,
455 dyn_findbyname, dyn_findbykidx,
456 dyn_create, dyn_destroy
459 O_CHECK_STATE, IPFW_TLV_STATE_NAME,
460 dyn_classify, dyn_update,
461 dyn_findbyname, dyn_findbykidx,
462 dyn_create, dyn_destroy
465 O_PROBE_STATE, IPFW_TLV_STATE_NAME,
466 dyn_classify, dyn_update,
467 dyn_findbyname, dyn_findbykidx,
468 dyn_create, dyn_destroy
471 O_LIMIT, IPFW_TLV_STATE_NAME,
472 dyn_classify, dyn_update,
473 dyn_findbyname, dyn_findbykidx,
474 dyn_create, dyn_destroy
478 * Print customizable flow id description via log(9) facility.
481 print_dyn_rule_flags(const struct ipfw_flow_id *id, int dyn_type,
482 int log_flags, char *prefix, char *postfix)
486 char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
488 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
492 if (IS_IP6_FLOW_ID(id)) {
493 ip6_sprintf(src, &id->src_ip6);
494 ip6_sprintf(dst, &id->dst_ip6);
498 da.s_addr = htonl(id->src_ip);
499 inet_ntop(AF_INET, &da, src, sizeof(src));
500 da.s_addr = htonl(id->dst_ip);
501 inet_ntop(AF_INET, &da, dst, sizeof(dst));
503 log(log_flags, "ipfw: %s type %d %s %d -> %s %d, %d %s\n",
504 prefix, dyn_type, src, id->src_port, dst,
505 id->dst_port, DYN_COUNT, postfix);
508 #define print_dyn_rule(id, dtype, prefix, postfix) \
509 print_dyn_rule_flags(id, dtype, LOG_DEBUG, prefix, postfix)
511 #define TIME_LEQ(a,b) ((int)((a)-(b)) <= 0)
512 #define TIME_LE(a,b) ((int)((a)-(b)) < 0)
515 dyn_update_proto_state(ipfw_dyn_rule *q, const struct ipfw_flow_id *id,
516 const void *ulp, int dir)
518 const struct tcphdr *tcp;
522 if (id->proto == IPPROTO_TCP) {
523 tcp = (const struct tcphdr *)ulp;
524 flags = id->_flags & (TH_FIN | TH_SYN | TH_RST);
525 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
526 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
527 #define TCP_FLAGS (TH_FLAGS | (TH_FLAGS << 8))
528 #define ACK_FWD 0x10000 /* fwd ack seen */
529 #define ACK_REV 0x20000 /* rev ack seen */
531 q->state |= (dir == MATCH_FORWARD) ? flags : (flags << 8);
532 switch (q->state & TCP_FLAGS) {
533 case TH_SYN: /* opening */
534 q->expire = time_uptime + V_dyn_syn_lifetime;
537 case BOTH_SYN: /* move to established */
538 case BOTH_SYN | TH_FIN: /* one side tries to close */
539 case BOTH_SYN | (TH_FIN << 8):
540 #define _SEQ_GE(a,b) ((int)(a) - (int)(b) >= 0)
544 ack = ntohl(tcp->th_ack);
545 if (dir == MATCH_FORWARD) {
546 if (q->ack_fwd == 0 ||
547 _SEQ_GE(ack, q->ack_fwd)) {
552 if (q->ack_rev == 0 ||
553 _SEQ_GE(ack, q->ack_rev)) {
558 if ((q->state & (ACK_FWD | ACK_REV)) ==
559 (ACK_FWD | ACK_REV)) {
560 q->expire = time_uptime + V_dyn_ack_lifetime;
561 q->state &= ~(ACK_FWD | ACK_REV);
565 case BOTH_SYN | BOTH_FIN: /* both sides closed */
566 if (V_dyn_fin_lifetime >= V_dyn_keepalive_period)
568 V_dyn_keepalive_period - 1;
569 q->expire = time_uptime + V_dyn_fin_lifetime;
575 * reset or some invalid combination, but can also
576 * occur if we use keep-state the wrong way.
578 if ( (q->state & ((TH_RST << 8)|TH_RST)) == 0)
579 printf("invalid state: 0x%x\n", q->state);
581 if (V_dyn_rst_lifetime >= V_dyn_keepalive_period)
583 V_dyn_keepalive_period - 1;
584 q->expire = time_uptime + V_dyn_rst_lifetime;
587 } else if (id->proto == IPPROTO_UDP) {
588 q->expire = time_uptime + V_dyn_udp_lifetime;
590 /* other protocols */
591 q->expire = time_uptime + V_dyn_short_lifetime;
596 * Lookup a dynamic rule, locked version.
598 static ipfw_dyn_rule *
599 lookup_dyn_rule_locked(const struct ipfw_flow_id *pkt, const void *ulp,
600 int i, int *match_direction, uint16_t kidx)
603 * Stateful ipfw extensions.
604 * Lookup into dynamic session queue.
606 ipfw_dyn_rule *prev, *q = NULL;
612 for (prev = NULL, q = V_ipfw_dyn_v[i].head; q; prev = q, q = q->next) {
613 if (q->dyn_type == O_LIMIT_PARENT)
616 if (pkt->addr_type != q->id.addr_type)
619 if (pkt->proto != q->id.proto)
622 if (kidx != 0 && kidx != q->kidx)
625 if (IS_IP6_FLOW_ID(pkt)) {
626 if (IN6_ARE_ADDR_EQUAL(&pkt->src_ip6, &q->id.src_ip6) &&
627 IN6_ARE_ADDR_EQUAL(&pkt->dst_ip6, &q->id.dst_ip6) &&
628 pkt->src_port == q->id.src_port &&
629 pkt->dst_port == q->id.dst_port) {
633 if (IN6_ARE_ADDR_EQUAL(&pkt->src_ip6, &q->id.dst_ip6) &&
634 IN6_ARE_ADDR_EQUAL(&pkt->dst_ip6, &q->id.src_ip6) &&
635 pkt->src_port == q->id.dst_port &&
636 pkt->dst_port == q->id.src_port) {
641 if (pkt->src_ip == q->id.src_ip &&
642 pkt->dst_ip == q->id.dst_ip &&
643 pkt->src_port == q->id.src_port &&
644 pkt->dst_port == q->id.dst_port) {
648 if (pkt->src_ip == q->id.dst_ip &&
649 pkt->dst_ip == q->id.src_ip &&
650 pkt->src_port == q->id.dst_port &&
651 pkt->dst_port == q->id.src_port) {
658 goto done; /* q = NULL, not found */
660 if (prev != NULL) { /* found and not in front */
661 prev->next = q->next;
662 q->next = V_ipfw_dyn_v[i].head;
663 V_ipfw_dyn_v[i].head = q;
666 /* update state according to flags */
667 dyn_update_proto_state(q, pkt, ulp, dir);
669 if (match_direction != NULL)
670 *match_direction = dir;
675 ipfw_dyn_lookup_state(const struct ipfw_flow_id *pkt, const void *ulp,
676 int pktlen, int *match_direction, uint16_t kidx)
682 i = hash_packet(pkt, V_curr_dyn_buckets);
685 q = lookup_dyn_rule_locked(pkt, ulp, i, match_direction, kidx);
690 IPFW_INC_DYN_COUNTER(q, pktlen);
697 resize_dynamic_table(struct ip_fw_chain *chain, int nbuckets)
699 int i, k, nbuckets_old;
701 struct ipfw_dyn_bucket *dyn_v, *dyn_v_old;
703 /* Check if given number is power of 2 and less than 64k */
704 if ((nbuckets > 65536) || (!powerof2(nbuckets)))
707 CTR3(KTR_NET, "%s: resize dynamic hash: %d -> %d", __func__,
708 V_curr_dyn_buckets, nbuckets);
710 /* Allocate and initialize new hash */
711 dyn_v = malloc(nbuckets * sizeof(*dyn_v), M_IPFW,
714 for (i = 0 ; i < nbuckets; i++)
715 IPFW_BUCK_LOCK_INIT(&dyn_v[i]);
718 * Call upper half lock, as get_map() do to ease
719 * read-only access to dynamic rules hash from sysctl
721 IPFW_UH_WLOCK(chain);
724 * Acquire chain write lock to permit hash access
725 * for main traffic path without additional locks
729 /* Save old values */
730 nbuckets_old = V_curr_dyn_buckets;
731 dyn_v_old = V_ipfw_dyn_v;
733 /* Skip relinking if array is not set up */
734 if (V_ipfw_dyn_v == NULL)
735 V_curr_dyn_buckets = 0;
737 /* Re-link all dynamic states */
738 for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
739 while (V_ipfw_dyn_v[i].head != NULL) {
740 /* Remove from current chain */
741 q = V_ipfw_dyn_v[i].head;
742 V_ipfw_dyn_v[i].head = q->next;
744 /* Get new hash value */
745 k = hash_packet(&q->id, nbuckets);
747 /* Add to the new head */
748 q->next = dyn_v[k].head;
753 /* Update current pointers/buckets values */
754 V_curr_dyn_buckets = nbuckets;
755 V_ipfw_dyn_v = dyn_v;
759 IPFW_UH_WUNLOCK(chain);
761 /* Start periodic callout on initial creation */
762 if (dyn_v_old == NULL) {
763 callout_reset_on(&V_ipfw_timeout, hz, ipfw_dyn_tick, curvnet, 0);
767 /* Destroy all mutexes */
768 for (i = 0 ; i < nbuckets_old ; i++)
769 IPFW_BUCK_LOCK_DESTROY(&dyn_v_old[i]);
772 free(dyn_v_old, M_IPFW);
778 * Install state of type 'type' for a dynamic session.
779 * The hash table contains two type of rules:
780 * - regular rules (O_KEEP_STATE)
781 * - rules for sessions with limited number of sess per user
782 * (O_LIMIT). When they are created, the parent is
783 * increased by 1, and decreased on delete. In this case,
784 * the third parameter is the parent rule and not the chain.
785 * - "parent" rules for the above (O_LIMIT_PARENT).
787 static ipfw_dyn_rule *
788 add_dyn_rule(const struct ipfw_flow_id *id, int i, uint8_t dyn_type,
789 struct ip_fw *rule, uint16_t kidx)
795 r = uma_zalloc(V_ipfw_dyn_rule_zone, M_NOWAIT | M_ZERO);
797 if (last_log != time_uptime) {
798 last_log = time_uptime;
800 "ipfw: Cannot allocate dynamic state, "
801 "consider increasing net.inet.ip.fw.dyn_max\n");
808 * refcount on parent is already incremented, so
809 * it is safe to use parent unlocked.
811 if (dyn_type == O_LIMIT) {
812 ipfw_dyn_rule *parent = (ipfw_dyn_rule *)rule;
813 if ( parent->dyn_type != O_LIMIT_PARENT)
814 panic("invalid parent");
820 r->expire = time_uptime + V_dyn_syn_lifetime;
822 r->dyn_type = dyn_type;
823 IPFW_ZERO_DYN_COUNTER(r);
827 r->next = V_ipfw_dyn_v[i].head;
828 V_ipfw_dyn_v[i].head = r;
829 DEB(print_dyn_rule(id, dyn_type, "add dyn entry", "total");)
834 * lookup dynamic parent rule using pkt and rule as search keys.
835 * If the lookup fails, then install one.
837 static ipfw_dyn_rule *
838 lookup_dyn_parent(const struct ipfw_flow_id *pkt, int *pindex,
839 struct ip_fw *rule, uint16_t kidx)
844 is_v6 = IS_IP6_FLOW_ID(pkt);
845 i = hash_packet( pkt, V_curr_dyn_buckets );
848 for (q = V_ipfw_dyn_v[i].head ; q != NULL ; q=q->next)
849 if (q->dyn_type == O_LIMIT_PARENT &&
852 pkt->proto == q->id.proto &&
853 pkt->src_port == q->id.src_port &&
854 pkt->dst_port == q->id.dst_port &&
857 IN6_ARE_ADDR_EQUAL(&(pkt->src_ip6),
859 IN6_ARE_ADDR_EQUAL(&(pkt->dst_ip6),
860 &(q->id.dst_ip6))) ||
862 pkt->src_ip == q->id.src_ip &&
863 pkt->dst_ip == q->id.dst_ip)
866 q->expire = time_uptime + V_dyn_short_lifetime;
867 DEB(print_dyn_rule(pkt, q->dyn_type,
868 "lookup_dyn_parent found", "");)
872 /* Add virtual limiting rule */
873 return add_dyn_rule(pkt, i, O_LIMIT_PARENT, rule, kidx);
877 * Install dynamic state for rule type cmd->o.opcode
879 * Returns 1 (failure) if state is not installed because of errors or because
880 * session limitations are enforced.
883 ipfw_dyn_install_state(struct ip_fw_chain *chain, struct ip_fw *rule,
884 ipfw_insn_limit *cmd, struct ip_fw_args *args, uint32_t tablearg)
889 DEB(print_dyn_rule(&args->f_id, cmd->o.opcode, "install_state",
890 (cmd->o.arg1 == 0 ? "": DYN_STATE_OBJ(chain, &cmd->o)->name));)
892 i = hash_packet(&args->f_id, V_curr_dyn_buckets);
896 q = lookup_dyn_rule_locked(&args->f_id, NULL, i, NULL, cmd->o.arg1);
897 if (q != NULL) { /* should never occur */
899 if (last_log != time_uptime) {
900 last_log = time_uptime;
901 printf("ipfw: %s: entry already present, done\n",
909 * State limiting is done via uma(9) zone limiting.
910 * Save pointer to newly-installed rule and reject
911 * packet if add_dyn_rule() returned NULL.
912 * Note q is currently set to NULL.
915 switch (cmd->o.opcode) {
916 case O_KEEP_STATE: /* bidir rule */
917 q = add_dyn_rule(&args->f_id, i, O_KEEP_STATE, rule,
921 case O_LIMIT: { /* limit number of sessions */
922 struct ipfw_flow_id id;
923 ipfw_dyn_rule *parent;
925 uint16_t limit_mask = cmd->limit_mask;
928 conn_limit = IP_FW_ARG_TABLEARG(chain, cmd->conn_limit, limit);
931 if (cmd->conn_limit == IP_FW_TARG)
932 printf("ipfw: %s: O_LIMIT rule, conn_limit: %u "
933 "(tablearg)\n", __func__, conn_limit);
935 printf("ipfw: %s: O_LIMIT rule, conn_limit: %u\n",
936 __func__, conn_limit);
939 id.dst_ip = id.src_ip = id.dst_port = id.src_port = 0;
940 id.proto = args->f_id.proto;
941 id.addr_type = args->f_id.addr_type;
942 id.fib = M_GETFIB(args->m);
944 if (IS_IP6_FLOW_ID (&(args->f_id))) {
945 bzero(&id.src_ip6, sizeof(id.src_ip6));
946 bzero(&id.dst_ip6, sizeof(id.dst_ip6));
948 if (limit_mask & DYN_SRC_ADDR)
949 id.src_ip6 = args->f_id.src_ip6;
950 if (limit_mask & DYN_DST_ADDR)
951 id.dst_ip6 = args->f_id.dst_ip6;
953 if (limit_mask & DYN_SRC_ADDR)
954 id.src_ip = args->f_id.src_ip;
955 if (limit_mask & DYN_DST_ADDR)
956 id.dst_ip = args->f_id.dst_ip;
958 if (limit_mask & DYN_SRC_PORT)
959 id.src_port = args->f_id.src_port;
960 if (limit_mask & DYN_DST_PORT)
961 id.dst_port = args->f_id.dst_port;
964 * We have to release lock for previous bucket to
965 * avoid possible deadlock
969 parent = lookup_dyn_parent(&id, &pindex, rule, cmd->o.arg1);
970 if (parent == NULL) {
971 printf("ipfw: %s: add parent failed\n", __func__);
972 IPFW_BUCK_UNLOCK(pindex);
976 if (parent->count >= conn_limit) {
977 if (V_fw_verbose && last_log != time_uptime) {
979 last_log = time_uptime;
980 snprintf(sbuf, sizeof(sbuf),
982 parent->rule->rulenum);
983 print_dyn_rule_flags(&args->f_id,
985 LOG_SECURITY | LOG_DEBUG,
986 sbuf, "too many entries");
988 IPFW_BUCK_UNLOCK(pindex);
991 /* Increment counter on parent */
993 IPFW_BUCK_UNLOCK(pindex);
996 q = add_dyn_rule(&args->f_id, i, O_LIMIT,
997 (struct ip_fw *)parent, cmd->o.arg1);
999 /* Decrement index and notify caller */
1000 IPFW_BUCK_UNLOCK(i);
1001 IPFW_BUCK_LOCK(pindex);
1003 IPFW_BUCK_UNLOCK(pindex);
1009 printf("ipfw: %s: unknown dynamic rule type %u\n",
1010 __func__, cmd->o.opcode);
1014 IPFW_BUCK_UNLOCK(i);
1015 return (1); /* Notify caller about failure */
1018 dyn_update_proto_state(q, &args->f_id, NULL, MATCH_FORWARD);
1019 IPFW_BUCK_UNLOCK(i);
1024 * Queue keepalive packets for given dynamic rule
1026 static struct mbuf **
1027 ipfw_dyn_send_ka(struct mbuf **mtailp, ipfw_dyn_rule *q)
1029 struct mbuf *m_rev, *m_fwd;
1031 m_rev = (q->state & ACK_REV) ? NULL :
1032 ipfw_send_pkt(NULL, &(q->id), q->ack_rev - 1, q->ack_fwd, TH_SYN);
1033 m_fwd = (q->state & ACK_FWD) ? NULL :
1034 ipfw_send_pkt(NULL, &(q->id), q->ack_fwd - 1, q->ack_rev, 0);
1036 if (m_rev != NULL) {
1038 mtailp = &(*mtailp)->m_nextpkt;
1040 if (m_fwd != NULL) {
1042 mtailp = &(*mtailp)->m_nextpkt;
1049 * This procedure is used to perform various maintenance
1050 * on dynamic hash list. Currently it is called every second.
1053 ipfw_dyn_tick(void * vnetx)
1055 struct ip_fw_chain *chain;
1058 struct vnet *vp = vnetx;
1063 chain = &V_layer3_chain;
1065 /* Run keepalive checks every keepalive_period iff ka is enabled */
1066 if ((V_dyn_keepalive_last + V_dyn_keepalive_period <= time_uptime) &&
1067 (V_dyn_keepalive != 0)) {
1068 V_dyn_keepalive_last = time_uptime;
1072 check_dyn_rules(chain, NULL, check_ka, 1);
1074 callout_reset_on(&V_ipfw_timeout, hz, ipfw_dyn_tick, vnetx, 0);
1081 * Walk through all dynamic states doing generic maintenance:
1082 * 1) free expired states
1083 * 2) free all states based on deleted rule / set
1084 * 3) send keepalives for states if needed
1086 * @chain - pointer to current ipfw rules chain
1087 * @rule - delete all states originated by given rule if != NULL
1088 * @set - delete all states originated by any rule in set @set if != RESVD_SET
1089 * @check_ka - perform checking/sending keepalives
1090 * @timer - indicate call from timer routine.
1092 * Timer routine must call this function unlocked to permit
1093 * sending keepalives/resizing table.
1095 * Others has to call function with IPFW_UH_WLOCK held.
1096 * Additionally, function assume that dynamic rule/set is
1097 * ALREADY deleted so no new states can be generated by
1100 * Write lock is needed to ensure that unused parent rules
1101 * are not freed by other instance (see stage 2, 3)
1104 check_dyn_rules(struct ip_fw_chain *chain, ipfw_range_tlv *rt,
1105 int check_ka, int timer)
1107 struct mbuf *m0, *m, *mnext, **mtailp;
1109 int i, dyn_count, new_buckets = 0, max_buckets;
1110 int expired = 0, expired_limits = 0, parents = 0, total = 0;
1111 ipfw_dyn_rule *q, *q_prev, *q_next;
1112 ipfw_dyn_rule *exp_head, **exptailp;
1113 ipfw_dyn_rule *exp_lhead, **expltailp;
1115 KASSERT(V_ipfw_dyn_v != NULL, ("%s: dynamic table not allocated",
1118 /* Avoid possible LOR */
1119 KASSERT(!check_ka || timer, ("%s: keepalive check with lock held",
1123 * Do not perform any checks if we currently have no dynamic states
1128 /* Expired states */
1130 exptailp = &exp_head;
1132 /* Expired limit states */
1134 expltailp = &exp_lhead;
1137 * We make a chain of packets to go out here -- not deferring
1138 * until after we drop the IPFW dynamic rule lock would result
1139 * in a lock order reversal with the normal packet input -> ipfw
1145 /* Protect from hash resizing */
1147 IPFW_UH_WLOCK(chain);
1149 IPFW_UH_WLOCK_ASSERT(chain);
1151 #define NEXT_RULE() { q_prev = q; q = q->next ; continue; }
1153 /* Stage 1: perform requested deletion */
1154 for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
1156 for (q = V_ipfw_dyn_v[i].head, q_prev = q; q ; ) {
1157 /* account every rule */
1160 /* Skip parent rules at all */
1161 if (q->dyn_type == O_LIMIT_PARENT) {
1167 * Remove rules which are:
1169 * 2) matches deletion range
1171 if ((TIME_LEQ(q->expire, time_uptime)) ||
1172 (rt != NULL && ipfw_match_range(q->rule, rt))) {
1173 if (TIME_LE(time_uptime, q->expire) &&
1174 q->dyn_type == O_KEEP_STATE &&
1175 V_dyn_keep_states != 0) {
1177 * Do not delete state if
1178 * it is not expired and
1179 * dyn_keep_states is ON.
1180 * However we need to re-link it
1181 * to any other stable rule
1183 q->rule = chain->default_rule;
1187 /* Unlink q from current list */
1189 if (q == V_ipfw_dyn_v[i].head)
1190 V_ipfw_dyn_v[i].head = q_next;
1192 q_prev->next = q_next;
1196 /* queue q to expire list */
1197 if (q->dyn_type != O_LIMIT) {
1199 exptailp = &(*exptailp)->next;
1200 DEB(print_dyn_rule(&q->id, q->dyn_type,
1201 "unlink entry", "left");
1204 /* Separate list for limit rules */
1206 expltailp = &(*expltailp)->next;
1208 DEB(print_dyn_rule(&q->id, q->dyn_type,
1209 "unlink limit entry", "left");
1219 * Check if we need to send keepalive:
1220 * we need to ensure if is time to do KA,
1221 * this is established TCP session, and
1222 * expire time is within keepalive interval
1224 if ((check_ka != 0) && (q->id.proto == IPPROTO_TCP) &&
1225 ((q->state & BOTH_SYN) == BOTH_SYN) &&
1226 (TIME_LEQ(q->expire, time_uptime +
1227 V_dyn_keepalive_interval)))
1228 mtailp = ipfw_dyn_send_ka(mtailp, q);
1232 IPFW_BUCK_UNLOCK(i);
1235 /* Stage 2: decrement counters from O_LIMIT parents */
1236 if (expired_limits != 0) {
1238 * XXX: Note that deleting set with more than one
1239 * heavily-used LIMIT rules can result in overwhelming
1240 * locking due to lack of per-hash value sorting
1242 * We should probably think about:
1243 * 1) pre-allocating hash of size, say,
1244 * MAX(16, V_curr_dyn_buckets / 1024)
1245 * 2) checking if expired_limits is large enough
1246 * 3) If yes, init hash (or its part), re-link
1247 * current list and start decrementing procedure in
1248 * each bucket separately
1252 * Small optimization: do not unlock bucket until
1253 * we see the next item resides in different bucket
1255 if (exp_lhead != NULL) {
1256 i = exp_lhead->parent->bucket;
1259 for (q = exp_lhead; q != NULL; q = q->next) {
1260 if (i != q->parent->bucket) {
1261 IPFW_BUCK_UNLOCK(i);
1262 i = q->parent->bucket;
1266 /* Decrease parent refcount */
1269 if (exp_lhead != NULL)
1270 IPFW_BUCK_UNLOCK(i);
1274 * We protectet ourselves from unused parent deletion
1275 * (from the timer function) by holding UH write lock.
1278 /* Stage 3: remove unused parent rules */
1279 if ((parents != 0) && (expired != 0)) {
1280 for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
1282 for (q = V_ipfw_dyn_v[i].head, q_prev = q ; q ; ) {
1283 if (q->dyn_type != O_LIMIT_PARENT)
1289 /* Parent rule without consumers */
1291 /* Unlink q from current list */
1293 if (q == V_ipfw_dyn_v[i].head)
1294 V_ipfw_dyn_v[i].head = q_next;
1296 q_prev->next = q_next;
1300 /* Add to expired list */
1302 exptailp = &(*exptailp)->next;
1304 DEB(print_dyn_rule(&q->id, q->dyn_type,
1305 "unlink parent entry", "left");
1312 IPFW_BUCK_UNLOCK(i);
1320 * Check if we need to resize hash:
1321 * if current number of states exceeds number of buckes in hash,
1322 * grow hash size to the minimum power of 2 which is bigger than
1323 * current states count. Limit hash size by 64k.
1325 max_buckets = (V_dyn_buckets_max > 65536) ?
1326 65536 : V_dyn_buckets_max;
1328 dyn_count = DYN_COUNT;
1330 if ((dyn_count > V_curr_dyn_buckets * 2) &&
1331 (dyn_count < max_buckets)) {
1332 new_buckets = V_curr_dyn_buckets;
1333 while (new_buckets < dyn_count) {
1336 if (new_buckets >= max_buckets)
1341 IPFW_UH_WUNLOCK(chain);
1344 /* Finally delete old states ad limits if any */
1345 for (q = exp_head; q != NULL; q = q_next) {
1347 uma_zfree(V_ipfw_dyn_rule_zone, q);
1351 for (q = exp_lhead; q != NULL; q = q_next) {
1353 uma_zfree(V_ipfw_dyn_rule_zone, q);
1358 * The rest code MUST be called from timer routine only
1359 * without holding any locks
1364 /* Send keepalive packets if any */
1365 for (m = m0; m != NULL; m = mnext) {
1366 mnext = m->m_nextpkt;
1367 m->m_nextpkt = NULL;
1368 h = mtod(m, struct ip *);
1370 ip_output(m, NULL, NULL, 0, NULL, NULL);
1373 ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
1377 /* Run table resize without holding any locks */
1378 if (new_buckets != 0)
1379 resize_dynamic_table(chain, new_buckets);
1383 * Deletes all dynamic rules originated by given rule or all rules in
1384 * given set. Specify RESVD_SET to indicate set should not be used.
1385 * @chain - pointer to current ipfw rules chain
1386 * @rr - delete all states originated by rules in matched range.
1388 * Function has to be called with IPFW_UH_WLOCK held.
1389 * Additionally, function assume that dynamic rule/set is
1390 * ALREADY deleted so no new states can be generated by
1394 ipfw_expire_dyn_rules(struct ip_fw_chain *chain, ipfw_range_tlv *rt)
1397 check_dyn_rules(chain, rt, 0, 0);
1401 * Check if rule contains at least one dynamic opcode.
1403 * Returns 1 if such opcode is found, 0 otherwise.
1406 ipfw_is_dyn_rule(struct ip_fw *rule)
1414 for ( ; l > 0 ; l -= cmdlen, cmd += cmdlen) {
1415 cmdlen = F_LEN(cmd);
1417 switch (cmd->opcode) {
1430 ipfw_dyn_init(struct ip_fw_chain *chain)
1433 V_ipfw_dyn_v = NULL;
1434 V_dyn_buckets_max = 256; /* must be power of 2 */
1435 V_curr_dyn_buckets = 256; /* must be power of 2 */
1437 V_dyn_ack_lifetime = 300;
1438 V_dyn_syn_lifetime = 20;
1439 V_dyn_fin_lifetime = 1;
1440 V_dyn_rst_lifetime = 1;
1441 V_dyn_udp_lifetime = 10;
1442 V_dyn_short_lifetime = 5;
1444 V_dyn_keepalive_interval = 20;
1445 V_dyn_keepalive_period = 5;
1446 V_dyn_keepalive = 1; /* do send keepalives */
1447 V_dyn_keepalive_last = time_uptime;
1449 V_dyn_max = 16384; /* max # of dynamic rules */
1451 V_ipfw_dyn_rule_zone = uma_zcreate("IPFW dynamic rule",
1452 sizeof(ipfw_dyn_rule), NULL, NULL, NULL, NULL,
1455 /* Enforce limit on dynamic rules */
1456 uma_zone_set_max(V_ipfw_dyn_rule_zone, V_dyn_max);
1458 callout_init(&V_ipfw_timeout, 1);
1461 * This can potentially be done on first dynamic rule
1462 * being added to chain.
1464 resize_dynamic_table(chain, V_curr_dyn_buckets);
1465 IPFW_ADD_OBJ_REWRITER(IS_DEFAULT_VNET(curvnet), dyn_opcodes);
1469 ipfw_dyn_uninit(int pass)
1474 callout_drain(&V_ipfw_timeout);
1477 IPFW_DEL_OBJ_REWRITER(IS_DEFAULT_VNET(curvnet), dyn_opcodes);
1479 if (V_ipfw_dyn_v != NULL) {
1481 * Skip deleting all dynamic states -
1482 * uma_zdestroy() does this more efficiently;
1485 /* Destroy all mutexes */
1486 for (i = 0 ; i < V_curr_dyn_buckets ; i++)
1487 IPFW_BUCK_LOCK_DESTROY(&V_ipfw_dyn_v[i]);
1488 free(V_ipfw_dyn_v, M_IPFW);
1489 V_ipfw_dyn_v = NULL;
1492 uma_zdestroy(V_ipfw_dyn_rule_zone);
1497 * Get/set maximum number of dynamic states in given VNET instance.
1500 sysctl_ipfw_dyn_max(SYSCTL_HANDLER_ARGS)
1503 unsigned int nstates;
1505 nstates = V_dyn_max;
1507 error = sysctl_handle_int(oidp, &nstates, 0, req);
1508 /* Read operation or some error */
1509 if ((error != 0) || (req->newptr == NULL))
1512 V_dyn_max = nstates;
1513 uma_zone_set_max(V_ipfw_dyn_rule_zone, V_dyn_max);
1519 * Get current number of dynamic states in given VNET instance.
1522 sysctl_ipfw_dyn_count(SYSCTL_HANDLER_ARGS)
1525 unsigned int nstates;
1527 nstates = DYN_COUNT;
1529 error = sysctl_handle_int(oidp, &nstates, 0, req);
1536 * Returns size of dynamic states in legacy format
1542 return (V_ipfw_dyn_v == NULL) ? 0 :
1543 (DYN_COUNT * sizeof(ipfw_dyn_rule));
1547 * Returns number of dynamic states.
1548 * Used by dump format v1 (current).
1551 ipfw_dyn_get_count(void)
1554 return (V_ipfw_dyn_v == NULL) ? 0 : DYN_COUNT;
1558 export_dyn_rule(ipfw_dyn_rule *src, ipfw_dyn_rule *dst)
1562 rulenum = (uint16_t)src->rule->rulenum;
1563 memcpy(dst, src, sizeof(*src));
1564 memcpy(&dst->rule, &rulenum, sizeof(rulenum));
1566 * store set number into high word of
1567 * dst->rule pointer.
1569 memcpy((char *)&dst->rule + sizeof(rulenum), &src->rule->set,
1570 sizeof(src->rule->set));
1572 * store a non-null value in "next".
1573 * The userland code will interpret a
1574 * NULL here as a marker
1575 * for the last dynamic rule.
1577 memcpy(&dst->next, &dst, sizeof(dst));
1578 dst->expire = TIME_LEQ(dst->expire, time_uptime) ? 0:
1579 dst->expire - time_uptime;
1583 * Fills int buffer given by @sd with dynamic states.
1584 * Used by dump format v1 (current).
1586 * Returns 0 on success.
1589 ipfw_dump_states(struct ip_fw_chain *chain, struct sockopt_data *sd)
1592 ipfw_obj_dyntlv *dst, *last;
1593 ipfw_obj_ctlv *ctlv;
1597 if (V_ipfw_dyn_v == NULL)
1600 IPFW_UH_RLOCK_ASSERT(chain);
1602 ctlv = (ipfw_obj_ctlv *)ipfw_get_sopt_space(sd, sizeof(*ctlv));
1605 sz = sizeof(ipfw_obj_dyntlv);
1606 ctlv->head.type = IPFW_TLV_DYNSTATE_LIST;
1610 for (i = 0 ; i < V_curr_dyn_buckets; i++) {
1612 for (p = V_ipfw_dyn_v[i].head ; p != NULL; p = p->next) {
1613 dst = (ipfw_obj_dyntlv *)ipfw_get_sopt_space(sd, sz);
1615 IPFW_BUCK_UNLOCK(i);
1619 export_dyn_rule(p, &dst->state);
1620 dst->head.length = sz;
1621 dst->head.type = IPFW_TLV_DYN_ENT;
1624 IPFW_BUCK_UNLOCK(i);
1627 if (last != NULL) /* mark last dynamic rule */
1628 last->head.flags = IPFW_DF_LAST;
1634 * Fill given buffer with dynamic states (legacy format).
1635 * IPFW_UH_RLOCK has to be held while calling.
1638 ipfw_get_dynamic(struct ip_fw_chain *chain, char **pbp, const char *ep)
1640 ipfw_dyn_rule *p, *last = NULL;
1644 if (V_ipfw_dyn_v == NULL)
1648 IPFW_UH_RLOCK_ASSERT(chain);
1650 for (i = 0 ; i < V_curr_dyn_buckets; i++) {
1652 for (p = V_ipfw_dyn_v[i].head ; p != NULL; p = p->next) {
1653 if (bp + sizeof *p <= ep) {
1654 ipfw_dyn_rule *dst =
1655 (ipfw_dyn_rule *)bp;
1657 export_dyn_rule(p, dst);
1659 bp += sizeof(ipfw_dyn_rule);
1662 IPFW_BUCK_UNLOCK(i);
1665 if (last != NULL) /* mark last dynamic rule */
1666 bzero(&last->next, sizeof(last));