]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/netpfil/ipfw/ip_fw_dynamic.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / netpfil / ipfw / ip_fw_dynamic.c
1 /*-
2  * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #define        DEB(x)
30 #define        DDB(x) x
31
32 /*
33  * Dynamic rule support for ipfw
34  */
35
36 #include "opt_ipfw.h"
37 #include "opt_inet.h"
38 #ifndef INET
39 #error IPFIREWALL requires INET.
40 #endif /* INET */
41 #include "opt_inet6.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/kernel.h>
48 #include <sys/lock.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <net/ethernet.h> /* for ETHERTYPE_IP */
53 #include <net/if.h>
54 #include <net/vnet.h>
55
56 #include <netinet/in.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_var.h>     /* ip_defttl */
59 #include <netinet/ip_fw.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/udp.h>
62
63 #include <netinet/ip6.h>        /* IN6_ARE_ADDR_EQUAL */
64 #ifdef INET6
65 #include <netinet6/in6_var.h>
66 #include <netinet6/ip6_var.h>
67 #endif
68
69 #include <netpfil/ipfw/ip_fw_private.h>
70
71 #include <machine/in_cksum.h>   /* XXX for in_cksum */
72
73 #ifdef MAC
74 #include <security/mac/mac_framework.h>
75 #endif
76
77 /*
78  * Description of dynamic rules.
79  *
80  * Dynamic rules are stored in lists accessed through a hash table
81  * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
82  * be modified through the sysctl variable dyn_buckets which is
83  * updated when the table becomes empty.
84  *
85  * XXX currently there is only one list, ipfw_dyn.
86  *
87  * When a packet is received, its address fields are first masked
88  * with the mask defined for the rule, then hashed, then matched
89  * against the entries in the corresponding list.
90  * Dynamic rules can be used for different purposes:
91  *  + stateful rules;
92  *  + enforcing limits on the number of sessions;
93  *  + in-kernel NAT (not implemented yet)
94  *
95  * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
96  * measured in seconds and depending on the flags.
97  *
98  * The total number of dynamic rules is equal to UMA zone items count.
99  * The max number of dynamic rules is dyn_max. When we reach
100  * the maximum number of rules we do not create anymore. This is
101  * done to avoid consuming too much memory, but also too much
102  * time when searching on each packet (ideally, we should try instead
103  * to put a limit on the length of the list on each bucket...).
104  *
105  * Each dynamic rule holds a pointer to the parent ipfw rule so
106  * we know what action to perform. Dynamic rules are removed when
107  * the parent rule is deleted. XXX we should make them survive.
108  *
109  * There are some limitations with dynamic rules -- we do not
110  * obey the 'randomized match', and we do not do multiple
111  * passes through the firewall. XXX check the latter!!!
112  */
113
114 struct ipfw_dyn_bucket {
115         struct mtx      mtx;            /* Bucket protecting lock */
116         ipfw_dyn_rule   *head;          /* Pointer to first rule */
117 };
118
119 /*
120  * Static variables followed by global ones
121  */
122 static VNET_DEFINE(struct ipfw_dyn_bucket *, ipfw_dyn_v);
123 static VNET_DEFINE(u_int32_t, dyn_buckets_max);
124 static VNET_DEFINE(u_int32_t, curr_dyn_buckets);
125 static VNET_DEFINE(struct callout, ipfw_timeout);
126 #define V_ipfw_dyn_v                    VNET(ipfw_dyn_v)
127 #define V_dyn_buckets_max               VNET(dyn_buckets_max)
128 #define V_curr_dyn_buckets              VNET(curr_dyn_buckets)
129 #define V_ipfw_timeout                  VNET(ipfw_timeout)
130
131 static VNET_DEFINE(uma_zone_t, ipfw_dyn_rule_zone);
132 #define V_ipfw_dyn_rule_zone            VNET(ipfw_dyn_rule_zone)
133
134 #define IPFW_BUCK_LOCK_INIT(b)  \
135         mtx_init(&(b)->mtx, "IPFW dynamic bucket", NULL, MTX_DEF)
136 #define IPFW_BUCK_LOCK_DESTROY(b)       \
137         mtx_destroy(&(b)->mtx)
138 #define IPFW_BUCK_LOCK(i)       mtx_lock(&V_ipfw_dyn_v[(i)].mtx)
139 #define IPFW_BUCK_UNLOCK(i)     mtx_unlock(&V_ipfw_dyn_v[(i)].mtx)
140 #define IPFW_BUCK_ASSERT(i)     mtx_assert(&V_ipfw_dyn_v[(i)].mtx, MA_OWNED)
141
142 /*
143  * Timeouts for various events in handing dynamic rules.
144  */
145 static VNET_DEFINE(u_int32_t, dyn_ack_lifetime);
146 static VNET_DEFINE(u_int32_t, dyn_syn_lifetime);
147 static VNET_DEFINE(u_int32_t, dyn_fin_lifetime);
148 static VNET_DEFINE(u_int32_t, dyn_rst_lifetime);
149 static VNET_DEFINE(u_int32_t, dyn_udp_lifetime);
150 static VNET_DEFINE(u_int32_t, dyn_short_lifetime);
151
152 #define V_dyn_ack_lifetime              VNET(dyn_ack_lifetime)
153 #define V_dyn_syn_lifetime              VNET(dyn_syn_lifetime)
154 #define V_dyn_fin_lifetime              VNET(dyn_fin_lifetime)
155 #define V_dyn_rst_lifetime              VNET(dyn_rst_lifetime)
156 #define V_dyn_udp_lifetime              VNET(dyn_udp_lifetime)
157 #define V_dyn_short_lifetime            VNET(dyn_short_lifetime)
158
159 /*
160  * Keepalives are sent if dyn_keepalive is set. They are sent every
161  * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
162  * seconds of lifetime of a rule.
163  * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
164  * than dyn_keepalive_period.
165  */
166
167 static VNET_DEFINE(u_int32_t, dyn_keepalive_interval);
168 static VNET_DEFINE(u_int32_t, dyn_keepalive_period);
169 static VNET_DEFINE(u_int32_t, dyn_keepalive);
170 static VNET_DEFINE(time_t, dyn_keepalive_last);
171
172 #define V_dyn_keepalive_interval        VNET(dyn_keepalive_interval)
173 #define V_dyn_keepalive_period          VNET(dyn_keepalive_period)
174 #define V_dyn_keepalive                 VNET(dyn_keepalive)
175 #define V_dyn_keepalive_last            VNET(dyn_keepalive_last)
176
177 static VNET_DEFINE(u_int32_t, dyn_max);         /* max # of dynamic rules */
178
179 #define DYN_COUNT                       uma_zone_get_cur(V_ipfw_dyn_rule_zone)
180 #define V_dyn_max                       VNET(dyn_max)
181
182 static int last_log;    /* Log ratelimiting */
183
184 static void ipfw_dyn_tick(void *vnetx);
185 static void check_dyn_rules(struct ip_fw_chain *, struct ip_fw *,
186     int, int, int);
187 #ifdef SYSCTL_NODE
188
189 static int sysctl_ipfw_dyn_count(SYSCTL_HANDLER_ARGS);
190 static int sysctl_ipfw_dyn_max(SYSCTL_HANDLER_ARGS);
191
192 SYSBEGIN(f2)
193
194 SYSCTL_DECL(_net_inet_ip_fw);
195 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_buckets,
196     CTLFLAG_RW, &VNET_NAME(dyn_buckets_max), 0,
197     "Max number of dyn. buckets");
198 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets,
199     CTLFLAG_RD, &VNET_NAME(curr_dyn_buckets), 0,
200     "Current Number of dyn. buckets");
201 SYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, dyn_count,
202     CTLTYPE_UINT|CTLFLAG_RD, 0, 0, sysctl_ipfw_dyn_count, "IU",
203     "Number of dyn. rules");
204 SYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, dyn_max,
205     CTLTYPE_UINT|CTLFLAG_RW, 0, 0, sysctl_ipfw_dyn_max, "IU",
206     "Max number of dyn. rules");
207 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime,
208     CTLFLAG_RW, &VNET_NAME(dyn_ack_lifetime), 0,
209     "Lifetime of dyn. rules for acks");
210 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime,
211     CTLFLAG_RW, &VNET_NAME(dyn_syn_lifetime), 0,
212     "Lifetime of dyn. rules for syn");
213 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime,
214     CTLFLAG_RW, &VNET_NAME(dyn_fin_lifetime), 0,
215     "Lifetime of dyn. rules for fin");
216 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime,
217     CTLFLAG_RW, &VNET_NAME(dyn_rst_lifetime), 0,
218     "Lifetime of dyn. rules for rst");
219 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime,
220     CTLFLAG_RW, &VNET_NAME(dyn_udp_lifetime), 0,
221     "Lifetime of dyn. rules for UDP");
222 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime,
223     CTLFLAG_RW, &VNET_NAME(dyn_short_lifetime), 0,
224     "Lifetime of dyn. rules for other situations");
225 SYSCTL_VNET_UINT(_net_inet_ip_fw, OID_AUTO, dyn_keepalive,
226     CTLFLAG_RW, &VNET_NAME(dyn_keepalive), 0,
227     "Enable keepalives for dyn. rules");
228
229 SYSEND
230
231 #endif /* SYSCTL_NODE */
232
233
234 static __inline int
235 hash_packet6(struct ipfw_flow_id *id)
236 {
237         u_int32_t i;
238         i = (id->dst_ip6.__u6_addr.__u6_addr32[2]) ^
239             (id->dst_ip6.__u6_addr.__u6_addr32[3]) ^
240             (id->src_ip6.__u6_addr.__u6_addr32[2]) ^
241             (id->src_ip6.__u6_addr.__u6_addr32[3]) ^
242             (id->dst_port) ^ (id->src_port);
243         return i;
244 }
245
246 /*
247  * IMPORTANT: the hash function for dynamic rules must be commutative
248  * in source and destination (ip,port), because rules are bidirectional
249  * and we want to find both in the same bucket.
250  */
251 static __inline int
252 hash_packet(struct ipfw_flow_id *id, int buckets)
253 {
254         u_int32_t i;
255
256 #ifdef INET6
257         if (IS_IP6_FLOW_ID(id)) 
258                 i = hash_packet6(id);
259         else
260 #endif /* INET6 */
261         i = (id->dst_ip) ^ (id->src_ip) ^ (id->dst_port) ^ (id->src_port);
262         i &= (buckets - 1);
263         return i;
264 }
265
266 /**
267  * Print customizable flow id description via log(9) facility.
268  */
269 static void
270 print_dyn_rule_flags(struct ipfw_flow_id *id, int dyn_type, int log_flags,
271     char *prefix, char *postfix)
272 {
273         struct in_addr da;
274 #ifdef INET6
275         char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
276 #else
277         char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
278 #endif
279
280 #ifdef INET6
281         if (IS_IP6_FLOW_ID(id)) {
282                 ip6_sprintf(src, &id->src_ip6);
283                 ip6_sprintf(dst, &id->dst_ip6);
284         } else
285 #endif
286         {
287                 da.s_addr = htonl(id->src_ip);
288                 inet_ntop(AF_INET, &da, src, sizeof(src));
289                 da.s_addr = htonl(id->dst_ip);
290                 inet_ntop(AF_INET, &da, dst, sizeof(dst));
291         }
292         log(log_flags, "ipfw: %s type %d %s %d -> %s %d, %d %s\n",
293             prefix, dyn_type, src, id->src_port, dst,
294             id->dst_port, DYN_COUNT, postfix);
295 }
296
297 #define print_dyn_rule(id, dtype, prefix, postfix)      \
298         print_dyn_rule_flags(id, dtype, LOG_DEBUG, prefix, postfix)
299
300 #define TIME_LEQ(a,b)       ((int)((a)-(b)) <= 0)
301
302 /*
303  * Lookup a dynamic rule, locked version.
304  */
305 static ipfw_dyn_rule *
306 lookup_dyn_rule_locked(struct ipfw_flow_id *pkt, int i, int *match_direction,
307     struct tcphdr *tcp)
308 {
309         /*
310          * Stateful ipfw extensions.
311          * Lookup into dynamic session queue.
312          */
313 #define MATCH_REVERSE   0
314 #define MATCH_FORWARD   1
315 #define MATCH_NONE      2
316 #define MATCH_UNKNOWN   3
317         int dir = MATCH_NONE;
318         ipfw_dyn_rule *prev, *q = NULL;
319
320         IPFW_BUCK_ASSERT(i);
321
322         for (prev = NULL, q = V_ipfw_dyn_v[i].head; q; prev = q, q = q->next) {
323                 if (q->dyn_type == O_LIMIT_PARENT && q->count)
324                         continue;
325
326                 if (pkt->proto != q->id.proto || q->dyn_type == O_LIMIT_PARENT)
327                         continue;
328
329                 if (IS_IP6_FLOW_ID(pkt)) {
330                         if (IN6_ARE_ADDR_EQUAL(&pkt->src_ip6, &q->id.src_ip6) &&
331                             IN6_ARE_ADDR_EQUAL(&pkt->dst_ip6, &q->id.dst_ip6) &&
332                             pkt->src_port == q->id.src_port &&
333                             pkt->dst_port == q->id.dst_port) {
334                                 dir = MATCH_FORWARD;
335                                 break;
336                         }
337                         if (IN6_ARE_ADDR_EQUAL(&pkt->src_ip6, &q->id.dst_ip6) &&
338                             IN6_ARE_ADDR_EQUAL(&pkt->dst_ip6, &q->id.src_ip6) &&
339                             pkt->src_port == q->id.dst_port &&
340                             pkt->dst_port == q->id.src_port) {
341                                 dir = MATCH_REVERSE;
342                                 break;
343                         }
344                 } else {
345                         if (pkt->src_ip == q->id.src_ip &&
346                             pkt->dst_ip == q->id.dst_ip &&
347                             pkt->src_port == q->id.src_port &&
348                             pkt->dst_port == q->id.dst_port) {
349                                 dir = MATCH_FORWARD;
350                                 break;
351                         }
352                         if (pkt->src_ip == q->id.dst_ip &&
353                             pkt->dst_ip == q->id.src_ip &&
354                             pkt->src_port == q->id.dst_port &&
355                             pkt->dst_port == q->id.src_port) {
356                                 dir = MATCH_REVERSE;
357                                 break;
358                         }
359                 }
360         }
361         if (q == NULL)
362                 goto done;      /* q = NULL, not found */
363
364         if (prev != NULL) {     /* found and not in front */
365                 prev->next = q->next;
366                 q->next = V_ipfw_dyn_v[i].head;
367                 V_ipfw_dyn_v[i].head = q;
368         }
369         if (pkt->proto == IPPROTO_TCP) { /* update state according to flags */
370                 uint32_t ack;
371                 u_char flags = pkt->_flags & (TH_FIN | TH_SYN | TH_RST);
372
373 #define BOTH_SYN        (TH_SYN | (TH_SYN << 8))
374 #define BOTH_FIN        (TH_FIN | (TH_FIN << 8))
375 #define TCP_FLAGS       (TH_FLAGS | (TH_FLAGS << 8))
376 #define ACK_FWD         0x10000                 /* fwd ack seen */
377 #define ACK_REV         0x20000                 /* rev ack seen */
378
379                 q->state |= (dir == MATCH_FORWARD) ? flags : (flags << 8);
380                 switch (q->state & TCP_FLAGS) {
381                 case TH_SYN:                    /* opening */
382                         q->expire = time_uptime + V_dyn_syn_lifetime;
383                         break;
384
385                 case BOTH_SYN:                  /* move to established */
386                 case BOTH_SYN | TH_FIN:         /* one side tries to close */
387                 case BOTH_SYN | (TH_FIN << 8):
388 #define _SEQ_GE(a,b) ((int)(a) - (int)(b) >= 0)
389                         if (tcp == NULL)
390                                 break;
391
392                         ack = ntohl(tcp->th_ack);
393                         if (dir == MATCH_FORWARD) {
394                                 if (q->ack_fwd == 0 ||
395                                     _SEQ_GE(ack, q->ack_fwd)) {
396                                         q->ack_fwd = ack;
397                                         q->state |= ACK_FWD;
398                                 }
399                         } else {
400                                 if (q->ack_rev == 0 ||
401                                     _SEQ_GE(ack, q->ack_rev)) {
402                                         q->ack_rev = ack;
403                                         q->state |= ACK_REV;
404                                 }
405                         }
406                         if ((q->state & (ACK_FWD | ACK_REV)) ==
407                             (ACK_FWD | ACK_REV)) {
408                                 q->expire = time_uptime + V_dyn_ack_lifetime;
409                                 q->state &= ~(ACK_FWD | ACK_REV);
410                         }
411                         break;
412
413                 case BOTH_SYN | BOTH_FIN:       /* both sides closed */
414                         if (V_dyn_fin_lifetime >= V_dyn_keepalive_period)
415                                 V_dyn_fin_lifetime = V_dyn_keepalive_period - 1;
416                         q->expire = time_uptime + V_dyn_fin_lifetime;
417                         break;
418
419                 default:
420 #if 0
421                         /*
422                          * reset or some invalid combination, but can also
423                          * occur if we use keep-state the wrong way.
424                          */
425                         if ( (q->state & ((TH_RST << 8)|TH_RST)) == 0)
426                                 printf("invalid state: 0x%x\n", q->state);
427 #endif
428                         if (V_dyn_rst_lifetime >= V_dyn_keepalive_period)
429                                 V_dyn_rst_lifetime = V_dyn_keepalive_period - 1;
430                         q->expire = time_uptime + V_dyn_rst_lifetime;
431                         break;
432                 }
433         } else if (pkt->proto == IPPROTO_UDP) {
434                 q->expire = time_uptime + V_dyn_udp_lifetime;
435         } else {
436                 /* other protocols */
437                 q->expire = time_uptime + V_dyn_short_lifetime;
438         }
439 done:
440         if (match_direction != NULL)
441                 *match_direction = dir;
442         return (q);
443 }
444
445 ipfw_dyn_rule *
446 ipfw_lookup_dyn_rule(struct ipfw_flow_id *pkt, int *match_direction,
447     struct tcphdr *tcp)
448 {
449         ipfw_dyn_rule *q;
450         int i;
451
452         i = hash_packet(pkt, V_curr_dyn_buckets);
453
454         IPFW_BUCK_LOCK(i);
455         q = lookup_dyn_rule_locked(pkt, i, match_direction, tcp);
456         if (q == NULL)
457                 IPFW_BUCK_UNLOCK(i);
458         /* NB: return table locked when q is not NULL */
459         return q;
460 }
461
462 /*
463  * Unlock bucket mtx
464  * @p - pointer to dynamic rule
465  */
466 void
467 ipfw_dyn_unlock(ipfw_dyn_rule *q)
468 {
469
470         IPFW_BUCK_UNLOCK(q->bucket);
471 }
472
473 static int
474 resize_dynamic_table(struct ip_fw_chain *chain, int nbuckets)
475 {
476         int i, k, nbuckets_old;
477         ipfw_dyn_rule *q;
478         struct ipfw_dyn_bucket *dyn_v, *dyn_v_old;
479
480         /* Check if given number is power of 2 and less than 64k */
481         if ((nbuckets > 65536) || (!powerof2(nbuckets)))
482                 return 1;
483
484         CTR3(KTR_NET, "%s: resize dynamic hash: %d -> %d", __func__,
485             V_curr_dyn_buckets, nbuckets);
486
487         /* Allocate and initialize new hash */
488         dyn_v = malloc(nbuckets * sizeof(ipfw_dyn_rule), M_IPFW,
489             M_WAITOK | M_ZERO);
490
491         for (i = 0 ; i < nbuckets; i++)
492                 IPFW_BUCK_LOCK_INIT(&dyn_v[i]);
493
494         /*
495          * Call upper half lock, as get_map() do to ease
496          * read-only access to dynamic rules hash from sysctl
497          */
498         IPFW_UH_WLOCK(chain);
499
500         /*
501          * Acquire chain write lock to permit hash access
502          * for main traffic path without additional locks
503          */
504         IPFW_WLOCK(chain);
505
506         /* Save old values */
507         nbuckets_old = V_curr_dyn_buckets;
508         dyn_v_old = V_ipfw_dyn_v;
509
510         /* Skip relinking if array is not set up */
511         if (V_ipfw_dyn_v == NULL)
512                 V_curr_dyn_buckets = 0;
513
514         /* Re-link all dynamic states */
515         for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
516                 while (V_ipfw_dyn_v[i].head != NULL) {
517                         /* Remove from current chain */
518                         q = V_ipfw_dyn_v[i].head;
519                         V_ipfw_dyn_v[i].head = q->next;
520
521                         /* Get new hash value */
522                         k = hash_packet(&q->id, nbuckets);
523                         q->bucket = k;
524                         /* Add to the new head */
525                         q->next = dyn_v[k].head;
526                         dyn_v[k].head = q;
527              }
528         }
529
530         /* Update current pointers/buckets values */
531         V_curr_dyn_buckets = nbuckets;
532         V_ipfw_dyn_v = dyn_v;
533
534         IPFW_WUNLOCK(chain);
535
536         IPFW_UH_WUNLOCK(chain);
537
538         /* Start periodic callout on initial creation */
539         if (dyn_v_old == NULL) {
540                 callout_reset_on(&V_ipfw_timeout, hz, ipfw_dyn_tick, curvnet, 0);
541                 return (0);
542         }
543
544         /* Destroy all mutexes */
545         for (i = 0 ; i < nbuckets_old ; i++)
546                 IPFW_BUCK_LOCK_DESTROY(&dyn_v_old[i]);
547
548         /* Free old hash */
549         free(dyn_v_old, M_IPFW);
550
551         return 0;
552 }
553
554 /**
555  * Install state of type 'type' for a dynamic session.
556  * The hash table contains two type of rules:
557  * - regular rules (O_KEEP_STATE)
558  * - rules for sessions with limited number of sess per user
559  *   (O_LIMIT). When they are created, the parent is
560  *   increased by 1, and decreased on delete. In this case,
561  *   the third parameter is the parent rule and not the chain.
562  * - "parent" rules for the above (O_LIMIT_PARENT).
563  */
564 static ipfw_dyn_rule *
565 add_dyn_rule(struct ipfw_flow_id *id, int i, u_int8_t dyn_type, struct ip_fw *rule)
566 {
567         ipfw_dyn_rule *r;
568
569         IPFW_BUCK_ASSERT(i);
570
571         r = uma_zalloc(V_ipfw_dyn_rule_zone, M_NOWAIT | M_ZERO);
572         if (r == NULL) {
573                 if (last_log != time_uptime) {
574                         last_log = time_uptime;
575                         log(LOG_DEBUG, "ipfw: %s: Cannot allocate rule\n",
576                             __func__);
577                 }
578                 return NULL;
579         }
580
581         /*
582          * refcount on parent is already incremented, so
583          * it is safe to use parent unlocked.
584          */
585         if (dyn_type == O_LIMIT) {
586                 ipfw_dyn_rule *parent = (ipfw_dyn_rule *)rule;
587                 if ( parent->dyn_type != O_LIMIT_PARENT)
588                         panic("invalid parent");
589                 r->parent = parent;
590                 rule = parent->rule;
591         }
592
593         r->id = *id;
594         r->expire = time_uptime + V_dyn_syn_lifetime;
595         r->rule = rule;
596         r->dyn_type = dyn_type;
597         IPFW_ZERO_DYN_COUNTER(r);
598         r->count = 0;
599
600         r->bucket = i;
601         r->next = V_ipfw_dyn_v[i].head;
602         V_ipfw_dyn_v[i].head = r;
603         DEB(print_dyn_rule(id, dyn_type, "add dyn entry", "total");)
604         return r;
605 }
606
607 /**
608  * lookup dynamic parent rule using pkt and rule as search keys.
609  * If the lookup fails, then install one.
610  */
611 static ipfw_dyn_rule *
612 lookup_dyn_parent(struct ipfw_flow_id *pkt, int *pindex, struct ip_fw *rule)
613 {
614         ipfw_dyn_rule *q;
615         int i, is_v6;
616
617         is_v6 = IS_IP6_FLOW_ID(pkt);
618         i = hash_packet( pkt, V_curr_dyn_buckets );
619         *pindex = i;
620         IPFW_BUCK_LOCK(i);
621         for (q = V_ipfw_dyn_v[i].head ; q != NULL ; q=q->next)
622                 if (q->dyn_type == O_LIMIT_PARENT &&
623                     rule== q->rule &&
624                     pkt->proto == q->id.proto &&
625                     pkt->src_port == q->id.src_port &&
626                     pkt->dst_port == q->id.dst_port &&
627                     (
628                         (is_v6 &&
629                          IN6_ARE_ADDR_EQUAL(&(pkt->src_ip6),
630                                 &(q->id.src_ip6)) &&
631                          IN6_ARE_ADDR_EQUAL(&(pkt->dst_ip6),
632                                 &(q->id.dst_ip6))) ||
633                         (!is_v6 &&
634                          pkt->src_ip == q->id.src_ip &&
635                          pkt->dst_ip == q->id.dst_ip)
636                     )
637                 ) {
638                         q->expire = time_uptime + V_dyn_short_lifetime;
639                         DEB(print_dyn_rule(pkt, q->dyn_type,
640                             "lookup_dyn_parent found", "");)
641                         return q;
642                 }
643
644         /* Add virtual limiting rule */
645         return add_dyn_rule(pkt, i, O_LIMIT_PARENT, rule);
646 }
647
648 /**
649  * Install dynamic state for rule type cmd->o.opcode
650  *
651  * Returns 1 (failure) if state is not installed because of errors or because
652  * session limitations are enforced.
653  */
654 int
655 ipfw_install_state(struct ip_fw *rule, ipfw_insn_limit *cmd,
656     struct ip_fw_args *args, uint32_t tablearg)
657 {
658         ipfw_dyn_rule *q;
659         int i;
660
661         DEB(print_dyn_rule(&args->f_id, cmd->o.opcode, "install_state", "");)
662         
663         i = hash_packet(&args->f_id, V_curr_dyn_buckets);
664
665         IPFW_BUCK_LOCK(i);
666
667         q = lookup_dyn_rule_locked(&args->f_id, i, NULL, NULL);
668
669         if (q != NULL) {        /* should never occur */
670                 DEB(
671                 if (last_log != time_uptime) {
672                         last_log = time_uptime;
673                         printf("ipfw: %s: entry already present, done\n",
674                             __func__);
675                 })
676                 IPFW_BUCK_UNLOCK(i);
677                 return (0);
678         }
679
680         /*
681          * State limiting is done via uma(9) zone limiting.
682          * Save pointer to newly-installed rule and reject
683          * packet if add_dyn_rule() returned NULL.
684          * Note q is currently set to NULL.
685          */
686
687         switch (cmd->o.opcode) {
688         case O_KEEP_STATE:      /* bidir rule */
689                 q = add_dyn_rule(&args->f_id, i, O_KEEP_STATE, rule);
690                 break;
691
692         case O_LIMIT: {         /* limit number of sessions */
693                 struct ipfw_flow_id id;
694                 ipfw_dyn_rule *parent;
695                 uint32_t conn_limit;
696                 uint16_t limit_mask = cmd->limit_mask;
697                 int pindex;
698
699                 conn_limit = IP_FW_ARG_TABLEARG(cmd->conn_limit);
700                   
701                 DEB(
702                 if (cmd->conn_limit == IP_FW_TABLEARG)
703                         printf("ipfw: %s: O_LIMIT rule, conn_limit: %u "
704                             "(tablearg)\n", __func__, conn_limit);
705                 else
706                         printf("ipfw: %s: O_LIMIT rule, conn_limit: %u\n",
707                             __func__, conn_limit);
708                 )
709
710                 id.dst_ip = id.src_ip = id.dst_port = id.src_port = 0;
711                 id.proto = args->f_id.proto;
712                 id.addr_type = args->f_id.addr_type;
713                 id.fib = M_GETFIB(args->m);
714
715                 if (IS_IP6_FLOW_ID (&(args->f_id))) {
716                         if (limit_mask & DYN_SRC_ADDR)
717                                 id.src_ip6 = args->f_id.src_ip6;
718                         if (limit_mask & DYN_DST_ADDR)
719                                 id.dst_ip6 = args->f_id.dst_ip6;
720                 } else {
721                         if (limit_mask & DYN_SRC_ADDR)
722                                 id.src_ip = args->f_id.src_ip;
723                         if (limit_mask & DYN_DST_ADDR)
724                                 id.dst_ip = args->f_id.dst_ip;
725                 }
726                 if (limit_mask & DYN_SRC_PORT)
727                         id.src_port = args->f_id.src_port;
728                 if (limit_mask & DYN_DST_PORT)
729                         id.dst_port = args->f_id.dst_port;
730
731                 /*
732                  * We have to release lock for previous bucket to
733                  * avoid possible deadlock
734                  */
735                 IPFW_BUCK_UNLOCK(i);
736
737                 if ((parent = lookup_dyn_parent(&id, &pindex, rule)) == NULL) {
738                         printf("ipfw: %s: add parent failed\n", __func__);
739                         IPFW_BUCK_UNLOCK(pindex);
740                         return (1);
741                 }
742
743                 if (parent->count >= conn_limit) {
744                         if (V_fw_verbose && last_log != time_uptime) {
745                                 last_log = time_uptime;
746                                 char sbuf[24];
747                                 last_log = time_uptime;
748                                 snprintf(sbuf, sizeof(sbuf),
749                                     "%d drop session",
750                                     parent->rule->rulenum);
751                                 print_dyn_rule_flags(&args->f_id,
752                                     cmd->o.opcode,
753                                     LOG_SECURITY | LOG_DEBUG,
754                                     sbuf, "too many entries");
755                         }
756                         IPFW_BUCK_UNLOCK(pindex);
757                         return (1);
758                 }
759                 /* Increment counter on parent */
760                 parent->count++;
761                 IPFW_BUCK_UNLOCK(pindex);
762
763                 IPFW_BUCK_LOCK(i);
764                 q = add_dyn_rule(&args->f_id, i, O_LIMIT, (struct ip_fw *)parent);
765                 if (q == NULL) {
766                         /* Decrement index and notify caller */
767                         IPFW_BUCK_UNLOCK(i);
768                         IPFW_BUCK_LOCK(pindex);
769                         parent->count--;
770                         IPFW_BUCK_UNLOCK(pindex);
771                         return (1);
772                 }
773                 break;
774         }
775         default:
776                 printf("ipfw: %s: unknown dynamic rule type %u\n",
777                     __func__, cmd->o.opcode);
778         }
779
780         if (q == NULL) {
781                 IPFW_BUCK_UNLOCK(i);
782                 return (1);     /* Notify caller about failure */
783         }
784
785         /* XXX just set lifetime */
786         lookup_dyn_rule_locked(&args->f_id, i, NULL, NULL);
787
788         IPFW_BUCK_UNLOCK(i);
789         return (0);
790 }
791
792 /*
793  * Generate a TCP packet, containing either a RST or a keepalive.
794  * When flags & TH_RST, we are sending a RST packet, because of a
795  * "reset" action matched the packet.
796  * Otherwise we are sending a keepalive, and flags & TH_
797  * The 'replyto' mbuf is the mbuf being replied to, if any, and is required
798  * so that MAC can label the reply appropriately.
799  */
800 struct mbuf *
801 ipfw_send_pkt(struct mbuf *replyto, struct ipfw_flow_id *id, u_int32_t seq,
802     u_int32_t ack, int flags)
803 {
804         struct mbuf *m = NULL;          /* stupid compiler */
805         int len, dir;
806         struct ip *h = NULL;            /* stupid compiler */
807 #ifdef INET6
808         struct ip6_hdr *h6 = NULL;
809 #endif
810         struct tcphdr *th = NULL;
811
812         MGETHDR(m, M_NOWAIT, MT_DATA);
813         if (m == NULL)
814                 return (NULL);
815
816         M_SETFIB(m, id->fib);
817 #ifdef MAC
818         if (replyto != NULL)
819                 mac_netinet_firewall_reply(replyto, m);
820         else
821                 mac_netinet_firewall_send(m);
822 #else
823         (void)replyto;          /* don't warn about unused arg */
824 #endif
825
826         switch (id->addr_type) {
827         case 4:
828                 len = sizeof(struct ip) + sizeof(struct tcphdr);
829                 break;
830 #ifdef INET6
831         case 6:
832                 len = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
833                 break;
834 #endif
835         default:
836                 /* XXX: log me?!? */
837                 FREE_PKT(m);
838                 return (NULL);
839         }
840         dir = ((flags & (TH_SYN | TH_RST)) == TH_SYN);
841
842         m->m_data += max_linkhdr;
843         m->m_flags |= M_SKIP_FIREWALL;
844         m->m_pkthdr.len = m->m_len = len;
845         m->m_pkthdr.rcvif = NULL;
846         bzero(m->m_data, len);
847
848         switch (id->addr_type) {
849         case 4:
850                 h = mtod(m, struct ip *);
851
852                 /* prepare for checksum */
853                 h->ip_p = IPPROTO_TCP;
854                 h->ip_len = htons(sizeof(struct tcphdr));
855                 if (dir) {
856                         h->ip_src.s_addr = htonl(id->src_ip);
857                         h->ip_dst.s_addr = htonl(id->dst_ip);
858                 } else {
859                         h->ip_src.s_addr = htonl(id->dst_ip);
860                         h->ip_dst.s_addr = htonl(id->src_ip);
861                 }
862
863                 th = (struct tcphdr *)(h + 1);
864                 break;
865 #ifdef INET6
866         case 6:
867                 h6 = mtod(m, struct ip6_hdr *);
868
869                 /* prepare for checksum */
870                 h6->ip6_nxt = IPPROTO_TCP;
871                 h6->ip6_plen = htons(sizeof(struct tcphdr));
872                 if (dir) {
873                         h6->ip6_src = id->src_ip6;
874                         h6->ip6_dst = id->dst_ip6;
875                 } else {
876                         h6->ip6_src = id->dst_ip6;
877                         h6->ip6_dst = id->src_ip6;
878                 }
879
880                 th = (struct tcphdr *)(h6 + 1);
881                 break;
882 #endif
883         }
884
885         if (dir) {
886                 th->th_sport = htons(id->src_port);
887                 th->th_dport = htons(id->dst_port);
888         } else {
889                 th->th_sport = htons(id->dst_port);
890                 th->th_dport = htons(id->src_port);
891         }
892         th->th_off = sizeof(struct tcphdr) >> 2;
893
894         if (flags & TH_RST) {
895                 if (flags & TH_ACK) {
896                         th->th_seq = htonl(ack);
897                         th->th_flags = TH_RST;
898                 } else {
899                         if (flags & TH_SYN)
900                                 seq++;
901                         th->th_ack = htonl(seq);
902                         th->th_flags = TH_RST | TH_ACK;
903                 }
904         } else {
905                 /*
906                  * Keepalive - use caller provided sequence numbers
907                  */
908                 th->th_seq = htonl(seq);
909                 th->th_ack = htonl(ack);
910                 th->th_flags = TH_ACK;
911         }
912
913         switch (id->addr_type) {
914         case 4:
915                 th->th_sum = in_cksum(m, len);
916
917                 /* finish the ip header */
918                 h->ip_v = 4;
919                 h->ip_hl = sizeof(*h) >> 2;
920                 h->ip_tos = IPTOS_LOWDELAY;
921                 h->ip_off = htons(0);
922                 h->ip_len = htons(len);
923                 h->ip_ttl = V_ip_defttl;
924                 h->ip_sum = 0;
925                 break;
926 #ifdef INET6
927         case 6:
928                 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(*h6),
929                     sizeof(struct tcphdr));
930
931                 /* finish the ip6 header */
932                 h6->ip6_vfc |= IPV6_VERSION;
933                 h6->ip6_hlim = IPV6_DEFHLIM;
934                 break;
935 #endif
936         }
937
938         return (m);
939 }
940
941 /*
942  * Queue keepalive packets for given dynamic rule
943  */
944 static struct mbuf **
945 ipfw_dyn_send_ka(struct mbuf **mtailp, ipfw_dyn_rule *q)
946 {
947         struct mbuf *m_rev, *m_fwd;
948
949         m_rev = (q->state & ACK_REV) ? NULL :
950             ipfw_send_pkt(NULL, &(q->id), q->ack_rev - 1, q->ack_fwd, TH_SYN);
951         m_fwd = (q->state & ACK_FWD) ? NULL :
952             ipfw_send_pkt(NULL, &(q->id), q->ack_fwd - 1, q->ack_rev, 0);
953
954         if (m_rev != NULL) {
955                 *mtailp = m_rev;
956                 mtailp = &(*mtailp)->m_nextpkt;
957         }
958         if (m_fwd != NULL) {
959                 *mtailp = m_fwd;
960                 mtailp = &(*mtailp)->m_nextpkt;
961         }
962
963         return (mtailp);
964 }
965
966 /*
967  * This procedure is used to perform various maintance
968  * on dynamic hash list. Currently it is called every second.
969  */
970 static void
971 ipfw_dyn_tick(void * vnetx) 
972 {
973         struct ip_fw_chain *chain;
974         int check_ka = 0;
975 #ifdef VIMAGE
976         struct vnet *vp = vnetx;
977 #endif
978
979         CURVNET_SET(vp);
980
981         chain = &V_layer3_chain;
982
983         /* Run keepalive checks every keepalive_period iff ka is enabled */
984         if ((V_dyn_keepalive_last + V_dyn_keepalive_period <= time_uptime) &&
985             (V_dyn_keepalive != 0)) {
986                 V_dyn_keepalive_last = time_uptime;
987                 check_ka = 1;
988         }
989
990         check_dyn_rules(chain, NULL, RESVD_SET, check_ka, 1);
991
992         callout_reset_on(&V_ipfw_timeout, hz, ipfw_dyn_tick, vnetx, 0);
993
994         CURVNET_RESTORE();
995 }
996
997
998 /*
999  * Walk thru all dynamic states doing generic maintance:
1000  * 1) free expired states
1001  * 2) free all states based on deleted rule / set
1002  * 3) send keepalives for states if needed
1003  *
1004  * @chain - pointer to current ipfw rules chain
1005  * @rule - delete all states originated by given rule if != NULL
1006  * @set - delete all states originated by any rule in set @set if != RESVD_SET
1007  * @check_ka - perform checking/sending keepalives
1008  * @timer - indicate call from timer routine.
1009  *
1010  * Timer routine must call this function unlocked to permit
1011  * sending keepalives/resizing table.
1012  *
1013  * Others has to call function with IPFW_UH_WLOCK held.
1014  * Additionally, function assume that dynamic rule/set is
1015  * ALREADY deleted so no new states can be generated by
1016  * 'deleted' rules.
1017  *
1018  * Write lock is needed to ensure that unused parent rules
1019  * are not freed by other instance (see stage 2, 3)
1020  */
1021 static void
1022 check_dyn_rules(struct ip_fw_chain *chain, struct ip_fw *rule,
1023     int set, int check_ka, int timer)
1024 {
1025         struct mbuf *m0, *m, *mnext, **mtailp;
1026         struct ip *h;
1027         int i, dyn_count, new_buckets = 0, max_buckets;
1028         int expired = 0, expired_limits = 0, parents = 0, total = 0;
1029         ipfw_dyn_rule *q, *q_prev, *q_next;
1030         ipfw_dyn_rule *exp_head, **exptailp;
1031         ipfw_dyn_rule *exp_lhead, **expltailp;
1032
1033         KASSERT(V_ipfw_dyn_v != NULL, ("%s: dynamic table not allocated",
1034             __func__));
1035
1036         /* Avoid possible LOR */
1037         KASSERT(!check_ka || timer, ("%s: keepalive check with lock held",
1038             __func__));
1039
1040         /*
1041          * Do not perform any checks if we currently have no dynamic states
1042          */
1043         if (DYN_COUNT == 0)
1044                 return;
1045
1046         /* Expired states */
1047         exp_head = NULL;
1048         exptailp = &exp_head;
1049
1050         /* Expired limit states */
1051         exp_lhead = NULL;
1052         expltailp = &exp_lhead;
1053
1054         /*
1055          * We make a chain of packets to go out here -- not deferring
1056          * until after we drop the IPFW dynamic rule lock would result
1057          * in a lock order reversal with the normal packet input -> ipfw
1058          * call stack.
1059          */
1060         m0 = NULL;
1061         mtailp = &m0;
1062
1063         /* Protect from hash resizing */
1064         if (timer != 0)
1065                 IPFW_UH_WLOCK(chain);
1066         else
1067                 IPFW_UH_WLOCK_ASSERT(chain);
1068
1069 #define NEXT_RULE()     { q_prev = q; q = q->next ; continue; }
1070
1071         /* Stage 1: perform requested deletion */
1072         for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
1073                 IPFW_BUCK_LOCK(i);
1074                 for (q = V_ipfw_dyn_v[i].head, q_prev = q; q ; ) {
1075                         /* account every rule */
1076                         total++;
1077
1078                         /* Skip parent rules at all */
1079                         if (q->dyn_type == O_LIMIT_PARENT) {
1080                                 parents++;
1081                                 NEXT_RULE();
1082                         }
1083
1084                         /*
1085                          * Remove rules which are:
1086                          * 1) expired
1087                          * 2) created by given rule
1088                          * 3) created by any rule in given set
1089                          */
1090                         if ((TIME_LEQ(q->expire, time_uptime)) ||
1091                             ((rule != NULL) && (q->rule == rule)) ||
1092                             ((set != RESVD_SET) && (q->rule->set == set))) {
1093                                 /* Unlink q from current list */
1094                                 q_next = q->next;
1095                                 if (q == V_ipfw_dyn_v[i].head)
1096                                         V_ipfw_dyn_v[i].head = q_next;
1097                                 else
1098                                         q_prev->next = q_next;
1099
1100                                 q->next = NULL;
1101
1102                                 /* queue q to expire list */
1103                                 if (q->dyn_type != O_LIMIT) {
1104                                         *exptailp = q;
1105                                         exptailp = &(*exptailp)->next;
1106                                         DEB(print_dyn_rule(&q->id, q->dyn_type,
1107                                             "unlink entry", "left");
1108                                         )
1109                                 } else {
1110                                         /* Separate list for limit rules */
1111                                         *expltailp = q;
1112                                         expltailp = &(*expltailp)->next;
1113                                         expired_limits++;
1114                                         DEB(print_dyn_rule(&q->id, q->dyn_type,
1115                                             "unlink limit entry", "left");
1116                                         )
1117                                 }
1118
1119                                 q = q_next;
1120                                 expired++;
1121                                 continue;
1122                         }
1123
1124                         /*
1125                          * Check if we need to send keepalive:
1126                          * we need to ensure if is time to do KA,
1127                          * this is established TCP session, and
1128                          * expire time is within keepalive interval
1129                          */
1130                         if ((check_ka != 0) && (q->id.proto == IPPROTO_TCP) &&
1131                             ((q->state & BOTH_SYN) == BOTH_SYN) &&
1132                             (TIME_LEQ(q->expire, time_uptime +
1133                               V_dyn_keepalive_interval)))
1134                                 mtailp = ipfw_dyn_send_ka(mtailp, q);
1135
1136                         NEXT_RULE();
1137                 }
1138                 IPFW_BUCK_UNLOCK(i);
1139         }
1140
1141         /* Stage 2: decrement counters from O_LIMIT parents */
1142         if (expired_limits != 0) {
1143                 /*
1144                  * XXX: Note that deleting set with more than one
1145                  * heavily-used LIMIT rules can result in overwhelming
1146                  * locking due to lack of per-hash value sorting
1147                  *
1148                  * We should probably think about:
1149                  * 1) pre-allocating hash of size, say,
1150                  * MAX(16, V_curr_dyn_buckets / 1024)
1151                  * 2) checking if expired_limits is large enough
1152                  * 3) If yes, init hash (or its part), re-link
1153                  * current list and start decrementing procedure in
1154                  * each bucket separately
1155                  */
1156
1157                 /*
1158                  * Small optimization: do not unlock bucket until
1159                  * we see the next item resides in different bucket
1160                  */
1161                 if (exp_lhead != NULL) {
1162                         i = exp_lhead->parent->bucket;
1163                         IPFW_BUCK_LOCK(i);
1164                 }
1165                 for (q = exp_lhead; q != NULL; q = q->next) {
1166                         if (i != q->parent->bucket) {
1167                                 IPFW_BUCK_UNLOCK(i);
1168                                 i = q->parent->bucket;
1169                                 IPFW_BUCK_LOCK(i);
1170                         }
1171
1172                         /* Decrease parent refcount */
1173                         q->parent->count--;
1174                 }
1175                 if (exp_lhead != NULL)
1176                         IPFW_BUCK_UNLOCK(i);
1177         }
1178
1179         /*
1180          * We protectet ourselves from unused parent deletion
1181          * (from the timer function) by holding UH write lock.
1182          */
1183
1184         /* Stage 3: remove unused parent rules */
1185         if ((parents != 0) && (expired != 0)) {
1186                 for (i = 0 ; i < V_curr_dyn_buckets ; i++) {
1187                         IPFW_BUCK_LOCK(i);
1188                         for (q = V_ipfw_dyn_v[i].head, q_prev = q ; q ; ) {
1189                                 if (q->dyn_type != O_LIMIT_PARENT)
1190                                         NEXT_RULE();
1191
1192                                 if (q->count != 0)
1193                                         NEXT_RULE();
1194
1195                                 /* Parent rule without consumers */
1196
1197                                 /* Unlink q from current list */
1198                                 q_next = q->next;
1199                                 if (q == V_ipfw_dyn_v[i].head)
1200                                         V_ipfw_dyn_v[i].head = q_next;
1201                                 else
1202                                         q_prev->next = q_next;
1203
1204                                 q->next = NULL;
1205
1206                                 /* Add to expired list */
1207                                 *exptailp = q;
1208                                 exptailp = &(*exptailp)->next;
1209
1210                                 DEB(print_dyn_rule(&q->id, q->dyn_type,
1211                                     "unlink parent entry", "left");
1212                                 )
1213
1214                                 expired++;
1215
1216                                 q = q_next;
1217                         }
1218                         IPFW_BUCK_UNLOCK(i);
1219                 }
1220         }
1221
1222 #undef NEXT_RULE
1223
1224         if (timer != 0) {
1225                 /*
1226                  * Check if we need to resize hash:
1227                  * if current number of states exceeds number of buckes in hash,
1228                  * grow hash size to the minimum power of 2 which is bigger than
1229                  * current states count. Limit hash size by 64k.
1230                  */
1231                 max_buckets = (V_dyn_buckets_max > 65536) ?
1232                     65536 : V_dyn_buckets_max;
1233         
1234                 dyn_count = DYN_COUNT;
1235         
1236                 if ((dyn_count > V_curr_dyn_buckets * 2) &&
1237                     (dyn_count < max_buckets)) {
1238                         new_buckets = V_curr_dyn_buckets;
1239                         while (new_buckets < dyn_count) {
1240                                 new_buckets *= 2;
1241         
1242                                 if (new_buckets >= max_buckets)
1243                                         break;
1244                         }
1245                 }
1246
1247                 IPFW_UH_WUNLOCK(chain);
1248         }
1249
1250         /* Finally delete old states ad limits if any */
1251         for (q = exp_head; q != NULL; q = q_next) {
1252                 q_next = q->next;
1253                 uma_zfree(V_ipfw_dyn_rule_zone, q);
1254         }
1255
1256         for (q = exp_lhead; q != NULL; q = q_next) {
1257                 q_next = q->next;
1258                 uma_zfree(V_ipfw_dyn_rule_zone, q);
1259         }
1260
1261         /*
1262          * The rest code MUST be called from timer routine only
1263          * without holding any locks
1264          */
1265         if (timer == 0)
1266                 return;
1267
1268         /* Send keepalive packets if any */
1269         for (m = m0; m != NULL; m = mnext) {
1270                 mnext = m->m_nextpkt;
1271                 m->m_nextpkt = NULL;
1272                 h = mtod(m, struct ip *);
1273                 if (h->ip_v == 4)
1274                         ip_output(m, NULL, NULL, 0, NULL, NULL);
1275 #ifdef INET6
1276                 else
1277                         ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
1278 #endif
1279         }
1280
1281         /* Run table resize without holding any locks */
1282         if (new_buckets != 0)
1283                 resize_dynamic_table(chain, new_buckets);
1284 }
1285
1286 /*
1287  * Deletes all dynamic rules originated by given rule or all rules in
1288  * given set. Specify RESVD_SET to indicate set should not be used.
1289  * @chain - pointer to current ipfw rules chain
1290  * @rule - delete all states originated by given rule if != NULL
1291  * @set - delete all states originated by any rule in set @set if != RESVD_SET
1292  *
1293  * Function has to be called with IPFW_UH_WLOCK held.
1294  * Additionally, function assume that dynamic rule/set is
1295  * ALREADY deleted so no new states can be generated by
1296  * 'deleted' rules.
1297  */
1298 void
1299 ipfw_expire_dyn_rules(struct ip_fw_chain *chain, struct ip_fw *rule, int set)
1300 {
1301
1302         check_dyn_rules(chain, rule, set, 0, 0);
1303 }
1304
1305 void
1306 ipfw_dyn_init(struct ip_fw_chain *chain)
1307 {
1308
1309         V_ipfw_dyn_v = NULL;
1310         V_dyn_buckets_max = 256; /* must be power of 2 */
1311         V_curr_dyn_buckets = 256; /* must be power of 2 */
1312  
1313         V_dyn_ack_lifetime = 300;
1314         V_dyn_syn_lifetime = 20;
1315         V_dyn_fin_lifetime = 1;
1316         V_dyn_rst_lifetime = 1;
1317         V_dyn_udp_lifetime = 10;
1318         V_dyn_short_lifetime = 5;
1319
1320         V_dyn_keepalive_interval = 20;
1321         V_dyn_keepalive_period = 5;
1322         V_dyn_keepalive = 1;    /* do send keepalives */
1323         V_dyn_keepalive_last = time_uptime;
1324         
1325         V_dyn_max = 4096;       /* max # of dynamic rules */
1326
1327         V_ipfw_dyn_rule_zone = uma_zcreate("IPFW dynamic rule",
1328             sizeof(ipfw_dyn_rule), NULL, NULL, NULL, NULL,
1329             UMA_ALIGN_PTR, 0);
1330
1331         /* Enforce limit on dynamic rules */
1332         uma_zone_set_max(V_ipfw_dyn_rule_zone, V_dyn_max);
1333
1334         callout_init(&V_ipfw_timeout, CALLOUT_MPSAFE);
1335
1336         /*
1337          * This can potentially be done on first dynamic rule
1338          * being added to chain.
1339          */
1340         resize_dynamic_table(chain, V_curr_dyn_buckets);
1341 }
1342
1343 void
1344 ipfw_dyn_uninit(int pass)
1345 {
1346         int i;
1347
1348         if (pass == 0) {
1349                 callout_drain(&V_ipfw_timeout);
1350                 return;
1351         }
1352
1353         if (V_ipfw_dyn_v != NULL) {
1354                 /*
1355                  * Skip deleting all dynamic states -
1356                  * uma_zdestroy() does this more efficiently;
1357                  */
1358
1359                 /* Destroy all mutexes */
1360                 for (i = 0 ; i < V_curr_dyn_buckets ; i++)
1361                         IPFW_BUCK_LOCK_DESTROY(&V_ipfw_dyn_v[i]);
1362                 free(V_ipfw_dyn_v, M_IPFW);
1363                 V_ipfw_dyn_v = NULL;
1364         }
1365
1366         uma_zdestroy(V_ipfw_dyn_rule_zone);
1367 }
1368
1369 #ifdef SYSCTL_NODE
1370 /*
1371  * Get/set maximum number of dynamic states in given VNET instance.
1372  */
1373 static int
1374 sysctl_ipfw_dyn_max(SYSCTL_HANDLER_ARGS)
1375 {
1376         int error;
1377         unsigned int nstates;
1378
1379         nstates = V_dyn_max;
1380
1381         error = sysctl_handle_int(oidp, &nstates, 0, req);
1382         /* Read operation or some error */
1383         if ((error != 0) || (req->newptr == NULL))
1384                 return (error);
1385
1386         V_dyn_max = nstates;
1387         uma_zone_set_max(V_ipfw_dyn_rule_zone, V_dyn_max);
1388
1389         return (0);
1390 }
1391
1392 /*
1393  * Get current number of dynamic states in given VNET instance.
1394  */
1395 static int
1396 sysctl_ipfw_dyn_count(SYSCTL_HANDLER_ARGS)
1397 {
1398         int error;
1399         unsigned int nstates;
1400
1401         nstates = DYN_COUNT;
1402
1403         error = sysctl_handle_int(oidp, &nstates, 0, req);
1404
1405         return (error);
1406 }
1407 #endif
1408
1409 /*
1410  * Returns number of dynamic rules.
1411  */
1412 int
1413 ipfw_dyn_len(void)
1414 {
1415
1416         return (V_ipfw_dyn_v == NULL) ? 0 :
1417                 (DYN_COUNT * sizeof(ipfw_dyn_rule));
1418 }
1419
1420 /*
1421  * Fill given buffer with dynamic states.
1422  * IPFW_UH_RLOCK has to be held while calling.
1423  */
1424 void
1425 ipfw_get_dynamic(struct ip_fw_chain *chain, char **pbp, const char *ep)
1426 {
1427         ipfw_dyn_rule *p, *last = NULL;
1428         char *bp;
1429         int i;
1430
1431         if (V_ipfw_dyn_v == NULL)
1432                 return;
1433         bp = *pbp;
1434
1435         IPFW_UH_RLOCK_ASSERT(chain);
1436
1437         for (i = 0 ; i < V_curr_dyn_buckets; i++) {
1438                 IPFW_BUCK_LOCK(i);
1439                 for (p = V_ipfw_dyn_v[i].head ; p != NULL; p = p->next) {
1440                         if (bp + sizeof *p <= ep) {
1441                                 ipfw_dyn_rule *dst =
1442                                         (ipfw_dyn_rule *)bp;
1443                                 bcopy(p, dst, sizeof *p);
1444                                 bcopy(&(p->rule->rulenum), &(dst->rule),
1445                                     sizeof(p->rule->rulenum));
1446                                 /*
1447                                  * store set number into high word of
1448                                  * dst->rule pointer.
1449                                  */
1450                                 bcopy(&(p->rule->set),
1451                                     (char *)&dst->rule +
1452                                     sizeof(p->rule->rulenum),
1453                                     sizeof(p->rule->set));
1454                                 /*
1455                                  * store a non-null value in "next".
1456                                  * The userland code will interpret a
1457                                  * NULL here as a marker
1458                                  * for the last dynamic rule.
1459                                  */
1460                                 bcopy(&dst, &dst->next, sizeof(dst));
1461                                 last = dst;
1462                                 dst->expire =
1463                                     TIME_LEQ(dst->expire, time_uptime) ?
1464                                         0 : dst->expire - time_uptime ;
1465                                 bp += sizeof(ipfw_dyn_rule);
1466                         }
1467                 }
1468                 IPFW_BUCK_UNLOCK(i);
1469         }
1470
1471         if (last != NULL) /* mark last dynamic rule */
1472                 bzero(&last->next, sizeof(last));
1473         *pbp = bp;
1474 }
1475 /* end of file */