Let ipfw_chk() return IP_FW_{PASS,DENY,DUMMYNET,TEE,DIVERT} and the caller
[dragonfly.git] / sys / net / ipfw / ip_fw2.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  * $FreeBSD: src/sys/netinet/ip_fw2.c,v 1.6.2.12 2003/04/08 10:42:32 maxim Exp $
26  * $DragonFly: src/sys/net/ipfw/ip_fw2.c,v 1.79 2008/09/07 10:03:45 sephe Exp $
27  */
28
29 #define        DEB(x)
30 #define        DDB(x) x
31
32 /*
33  * Implement IP packet firewall (new version)
34  */
35
36 #ifndef KLD_MODULE
37 #include "opt_ipfw.h"
38 #include "opt_ipdn.h"
39 #include "opt_ipdivert.h"
40 #include "opt_inet.h"
41 #ifndef INET
42 #error IPFIREWALL requires INET.
43 #endif /* INET */
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 #include <sys/thread2.h>
57 #include <sys/ucred.h>
58 #include <sys/in_cksum.h>
59 #include <sys/lock.h>
60
61 #include <net/if.h>
62 #include <net/route.h>
63 #include <net/netmsg2.h>
64
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/ip_icmp.h>
72 #include "ip_fw.h"
73 #include <net/dummynet/ip_dummynet.h>
74 #include <netinet/tcp.h>
75 #include <netinet/tcp_timer.h>
76 #include <netinet/tcp_var.h>
77 #include <netinet/tcpip.h>
78 #include <netinet/udp.h>
79 #include <netinet/udp_var.h>
80 #include <netinet/ip_divert.h>
81
82 #include <netinet/if_ether.h> /* XXX for ETHERTYPE_IP */
83
84 /*
85  * Description about per-CPU rule duplication:
86  *
87  * Module loading/unloading and all ioctl operations are serialized
88  * by netisr0, so we don't have any ordering or locking problems.
89  *
90  * Following graph shows how operation on per-CPU rule list is
91  * performed [2 CPU case]:
92  *
93  *   CPU0                 CPU1
94  *
95  * netisr0 <------------------------------------+
96  *  domsg                                       |
97  *    |                                         |
98  *    | netmsg                                  |
99  *    |                                         |
100  *    V                                         |
101  *  ifnet0                                      |
102  *    :                                         | netmsg
103  *    :(delete/add...)                          |
104  *    :                                         |
105  *    :         netmsg                          |
106  *  forwardmsg---------->ifnet1                 |
107  *                          :                   |
108  *                          :(delete/add...)    |
109  *                          :                   |
110  *                          :                   |
111  *                        replymsg--------------+
112  *
113  *
114  *
115  *
116  * Rules which will not create states (dyn rules) [2 CPU case]
117  *
118  *    CPU0               CPU1
119  * layer3_chain       layer3_chain
120  *     |                  |
121  *     V                  V
122  * +-------+ sibling  +-------+ sibling
123  * | rule1 |--------->| rule1 |--------->NULL
124  * +-------+          +-------+
125  *     |                  |
126  *     |next              |next
127  *     V                  V
128  * +-------+ sibling  +-------+ sibling
129  * | rule2 |--------->| rule2 |--------->NULL
130  * +-------+          +-------+
131  *
132  * ip_fw.sibling:
133  * 1) Ease statistics calculation during IP_FW_GET.  We only need to
134  *    iterate layer3_chain on CPU0; the current rule's duplication on
135  *    the other CPUs could safely be read-only accessed by using
136  *    ip_fw.sibling
137  * 2) Accelerate rule insertion and deletion, e.g. rule insertion:
138  *    a) In netisr0 (on CPU0) rule3 is determined to be inserted between
139  *       rule1 and rule2.  To make this decision we need to iterate the
140  *       layer3_chain on CPU0.  The netmsg, which is used to insert the
141  *       rule, will contain rule1 on CPU0 as prev_rule and rule2 on CPU0
142  *       as next_rule
143  *    b) After the insertion on CPU0 is done, we will move on to CPU1.
144  *       But instead of relocating the rule3's position on CPU1 by
145  *       iterating the layer3_chain on CPU1, we set the netmsg's prev_rule
146  *       to rule1->sibling and next_rule to rule2->sibling before the
147  *       netmsg is forwarded to CPU1 from CPU0
148  *       
149  *    
150  *
151  * Rules which will create states (dyn rules) [2 CPU case]
152  * (unnecessary parts are omitted; they are same as in the previous figure)
153  *
154  *   CPU0                       CPU1
155  * 
156  * +-------+                  +-------+
157  * | rule1 |                  | rule1 |
158  * +-------+                  +-------+
159  *   ^   |                      |   ^
160  *   |   |stub              stub|   |
161  *   |   |                      |   |
162  *   |   +----+            +----+   |
163  *   |        |            |        |
164  *   |        V            V        |
165  *   |    +--------------------+    |
166  *   |    |     rule_stub      |    |
167  *   |    | (read-only shared) |    |
168  *   |    |                    |    |
169  *   |    | back pointer array |    |
170  *   |    | (indexed by cpuid) |    |
171  *   |    |                    |    |
172  *   +----|---------[0]        |    |
173  *        |         [1]--------|----+
174  *        |                    |
175  *        +--------------------+
176  *          ^            ^
177  *          |            |
178  *  ........|............|............
179  *  :       |            |           :
180  *  :       |stub        |stub       :
181  *  :       |            |           :
182  *  :  +---------+  +---------+      :
183  *  :  | state1a |  | state1b | .... :
184  *  :  +---------+  +---------+      :
185  *  :                                :
186  *  :           states table         :
187  *  :            (shared)            :
188  *  :      (protected by dyn_lock)   :
189  *  ..................................
190  * 
191  * [state1a and state1b are states created by rule1]
192  *
193  * ip_fw_stub:
194  * This structure is introduced so that shared (locked) state table could
195  * work with per-CPU (duplicated) static rules.  It mainly bridges states
196  * and static rules and serves as static rule's place holder (a read-only
197  * shared part of duplicated rules) from states point of view.
198  *
199  * IPFW_RULE_F_STATE (only for rules which create states):
200  * o  During rule installation, this flag is turned on after rule's
201  *    duplications reach all CPUs, to avoid at least following race:
202  *    1) rule1 is duplicated on CPU0 and is not duplicated on CPU1 yet
203  *    2) rule1 creates state1
204  *    3) state1 is located on CPU1 by check-state
205  *    But rule1 is not duplicated on CPU1 yet
206  * o  During rule deletion, this flag is turned off before deleting states
207  *    created by the rule and before deleting the rule itself, so no
208  *    more states will be created by the to-be-deleted rule even when its
209  *    duplication on certain CPUs are not eliminated yet.
210  */
211
212 #define IPFW_AUTOINC_STEP_MIN   1
213 #define IPFW_AUTOINC_STEP_MAX   1000
214 #define IPFW_AUTOINC_STEP_DEF   100
215
216 #define IPFW_DEFAULT_RULE       65535   /* rulenum for the default rule */
217 #define IPFW_DEFAULT_SET        31      /* set number for the default rule */
218
219 struct netmsg_ipfw {
220         struct netmsg   nmsg;
221         const struct ipfw_ioc_rule *ioc_rule;
222         struct ip_fw    *next_rule;
223         struct ip_fw    *prev_rule;
224         struct ip_fw    *sibling;
225         struct ip_fw_stub *stub;
226 };
227
228 struct netmsg_del {
229         struct netmsg   nmsg;
230         struct ip_fw    *start_rule;
231         struct ip_fw    *prev_rule;
232         uint16_t        rulenum;
233         uint8_t         from_set;
234         uint8_t         to_set;
235 };
236
237 struct netmsg_zent {
238         struct netmsg   nmsg;
239         struct ip_fw    *start_rule;
240         uint16_t        rulenum;
241         uint16_t        log_only;
242 };
243
244 struct ipfw_context {
245         struct ip_fw    *ipfw_layer3_chain;     /* list of rules for layer3 */
246         struct ip_fw    *ipfw_default_rule;     /* default rule */
247         uint64_t        ipfw_norule_counter;    /* counter for ipfw_log(NULL) */
248
249         /*
250          * ipfw_set_disable contains one bit per set value (0..31).
251          * If the bit is set, all rules with the corresponding set
252          * are disabled.  Set IPDW_DEFAULT_SET is reserved for the
253          * default rule and CANNOT be disabled.
254          */
255         uint32_t        ipfw_set_disable;
256         uint32_t        ipfw_gen;               /* generation of rule list */
257 };
258
259 static struct ipfw_context      *ipfw_ctx[MAXCPU];
260
261 #ifdef KLD_MODULE
262 /*
263  * Module can not be unloaded, if there are references to
264  * certains rules of ipfw(4), e.g. dummynet(4)
265  */
266 static int ipfw_refcnt;
267 #endif
268
269 MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");
270
271 /*
272  * Following two global variables are accessed and
273  * updated only on CPU0
274  */
275 static uint32_t static_count;   /* # of static rules */
276 static uint32_t static_ioc_len; /* bytes of static rules */
277
278 /*
279  * If 1, then ipfw static rules are being flushed,
280  * ipfw_chk() will skip to the default rule.
281  */
282 static int ipfw_flushing;
283
284 static int fw_verbose;
285 static int verbose_limit;
286
287 static int fw_debug = 1;
288 static int autoinc_step = IPFW_AUTOINC_STEP_DEF;
289
290 static int      ipfw_sysctl_autoinc_step(SYSCTL_HANDLER_ARGS);
291 static int      ipfw_sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS);
292 static int      ipfw_sysctl_dyn_fin(SYSCTL_HANDLER_ARGS);
293 static int      ipfw_sysctl_dyn_rst(SYSCTL_HANDLER_ARGS);
294
295 #ifdef SYSCTL_NODE
296 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
297 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, enable, CTLFLAG_RW,
298     &fw_enable, 0, "Enable ipfw");
299 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, autoinc_step, CTLTYPE_INT | CTLFLAG_RW,
300     &autoinc_step, 0, ipfw_sysctl_autoinc_step, "I",
301     "Rule number autincrement step");
302 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO,one_pass,CTLFLAG_RW,
303     &fw_one_pass, 0,
304     "Only do a single pass through ipfw when using dummynet(4)");
305 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, debug, CTLFLAG_RW,
306     &fw_debug, 0, "Enable printing of debug ip_fw statements");
307 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose, CTLFLAG_RW,
308     &fw_verbose, 0, "Log matches to ipfw rules");
309 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, CTLFLAG_RW,
310     &verbose_limit, 0, "Set upper limit of matches of ipfw rules logged");
311
312 /*
313  * Description of dynamic rules.
314  *
315  * Dynamic rules are stored in lists accessed through a hash table
316  * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
317  * be modified through the sysctl variable dyn_buckets which is
318  * updated when the table becomes empty.
319  *
320  * XXX currently there is only one list, ipfw_dyn.
321  *
322  * When a packet is received, its address fields are first masked
323  * with the mask defined for the rule, then hashed, then matched
324  * against the entries in the corresponding list.
325  * Dynamic rules can be used for different purposes:
326  *  + stateful rules;
327  *  + enforcing limits on the number of sessions;
328  *  + in-kernel NAT (not implemented yet)
329  *
330  * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
331  * measured in seconds and depending on the flags.
332  *
333  * The total number of dynamic rules is stored in dyn_count.
334  * The max number of dynamic rules is dyn_max. When we reach
335  * the maximum number of rules we do not create anymore. This is
336  * done to avoid consuming too much memory, but also too much
337  * time when searching on each packet (ideally, we should try instead
338  * to put a limit on the length of the list on each bucket...).
339  *
340  * Each dynamic rule holds a pointer to the parent ipfw rule so
341  * we know what action to perform. Dynamic rules are removed when
342  * the parent rule is deleted. XXX we should make them survive.
343  *
344  * There are some limitations with dynamic rules -- we do not
345  * obey the 'randomized match', and we do not do multiple
346  * passes through the firewall. XXX check the latter!!!
347  *
348  * NOTE about the SHARED LOCKMGR LOCK during dynamic rule looking up:
349  * Only TCP state transition will change dynamic rule's state and ack
350  * sequences, while all packets of one TCP connection only goes through
351  * one TCP thread, so it is safe to use shared lockmgr lock during dynamic
352  * rule looking up.  The keep alive callout uses exclusive lockmgr lock
353  * when it tries to find suitable dynamic rules to send keep alive, so
354  * it will not see half updated state and ack sequences.  Though the expire
355  * field updating looks racy for other protocols, the resolution (second)
356  * of expire field makes this kind of race harmless.
357  * XXX statistics' updating is _not_ MPsafe!!!
358  * XXX once UDP output path is fixed, we could use lockless dynamic rule
359  *     hash table
360  */
361 static ipfw_dyn_rule **ipfw_dyn_v = NULL;
362 static uint32_t dyn_buckets = 256; /* must be power of 2 */
363 static uint32_t curr_dyn_buckets = 256; /* must be power of 2 */
364 static uint32_t dyn_buckets_gen; /* generation of dyn buckets array */
365 static struct lock dyn_lock; /* dynamic rules' hash table lock */
366 static struct callout ipfw_timeout_h;
367
368 /*
369  * Timeouts for various events in handing dynamic rules.
370  */
371 static uint32_t dyn_ack_lifetime = 300;
372 static uint32_t dyn_syn_lifetime = 20;
373 static uint32_t dyn_fin_lifetime = 1;
374 static uint32_t dyn_rst_lifetime = 1;
375 static uint32_t dyn_udp_lifetime = 10;
376 static uint32_t dyn_short_lifetime = 5;
377
378 /*
379  * Keepalives are sent if dyn_keepalive is set. They are sent every
380  * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
381  * seconds of lifetime of a rule.
382  * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
383  * than dyn_keepalive_period.
384  */
385
386 static uint32_t dyn_keepalive_interval = 20;
387 static uint32_t dyn_keepalive_period = 5;
388 static uint32_t dyn_keepalive = 1;      /* do send keepalives */
389
390 static uint32_t dyn_count;              /* # of dynamic rules */
391 static uint32_t dyn_max = 4096;         /* max # of dynamic rules */
392
393 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_buckets, CTLTYPE_INT | CTLFLAG_RW,
394     &dyn_buckets, 0, ipfw_sysctl_dyn_buckets, "I", "Number of dyn. buckets");
395 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets, CTLFLAG_RD,
396     &curr_dyn_buckets, 0, "Current Number of dyn. buckets");
397 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_count, CTLFLAG_RD,
398     &dyn_count, 0, "Number of dyn. rules");
399 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_max, CTLFLAG_RW,
400     &dyn_max, 0, "Max number of dyn. rules");
401 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count, CTLFLAG_RD,
402     &static_count, 0, "Number of static rules");
403 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime, CTLFLAG_RW,
404     &dyn_ack_lifetime, 0, "Lifetime of dyn. rules for acks");
405 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime, CTLFLAG_RW,
406     &dyn_syn_lifetime, 0, "Lifetime of dyn. rules for syn");
407 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime,
408     CTLTYPE_INT | CTLFLAG_RW, &dyn_fin_lifetime, 0, ipfw_sysctl_dyn_fin, "I",
409     "Lifetime of dyn. rules for fin");
410 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime,
411     CTLTYPE_INT | CTLFLAG_RW, &dyn_rst_lifetime, 0, ipfw_sysctl_dyn_rst, "I",
412     "Lifetime of dyn. rules for rst");
413 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime, CTLFLAG_RW,
414     &dyn_udp_lifetime, 0, "Lifetime of dyn. rules for UDP");
415 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime, CTLFLAG_RW,
416     &dyn_short_lifetime, 0, "Lifetime of dyn. rules for other situations");
417 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_keepalive, CTLFLAG_RW,
418     &dyn_keepalive, 0, "Enable keepalives for dyn. rules");
419
420 #endif /* SYSCTL_NODE */
421
422 static ip_fw_chk_t      ipfw_chk;
423
424 static __inline int
425 ipfw_free_rule(struct ip_fw *rule)
426 {
427         KASSERT(rule->cpuid == mycpuid, ("rule freed on cpu%d\n", mycpuid));
428         KASSERT(rule->refcnt > 0, ("invalid refcnt %u\n", rule->refcnt));
429         rule->refcnt--;
430         if (rule->refcnt == 0) {
431                 kfree(rule, M_IPFW);
432                 return 1;
433         }
434         return 0;
435 }
436
437 static void
438 ipfw_unref_rule(void *priv)
439 {
440         ipfw_free_rule(priv);
441 #ifdef KLD_MODULE
442         atomic_subtract_int(&ipfw_refcnt, 1);
443 #endif
444 }
445
446 static __inline void
447 ipfw_ref_rule(struct ip_fw *rule)
448 {
449         KASSERT(rule->cpuid == mycpuid, ("rule used on cpu%d\n", mycpuid));
450 #ifdef KLD_MODULE
451         atomic_add_int(&ipfw_refcnt, 1);
452 #endif
453         rule->refcnt++;
454 }
455
456 /*
457  * This macro maps an ip pointer into a layer3 header pointer of type T
458  */
459 #define L3HDR(T, ip) ((T *)((uint32_t *)(ip) + (ip)->ip_hl))
460
461 static __inline int
462 icmptype_match(struct ip *ip, ipfw_insn_u32 *cmd)
463 {
464         int type = L3HDR(struct icmp,ip)->icmp_type;
465
466         return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1 << type)));
467 }
468
469 #define TT      ((1 << ICMP_ECHO) | \
470                  (1 << ICMP_ROUTERSOLICIT) | \
471                  (1 << ICMP_TSTAMP) | \
472                  (1 << ICMP_IREQ) | \
473                  (1 << ICMP_MASKREQ))
474
475 static int
476 is_icmp_query(struct ip *ip)
477 {
478         int type = L3HDR(struct icmp, ip)->icmp_type;
479
480         return (type <= ICMP_MAXTYPE && (TT & (1 << type)));
481 }
482
483 #undef TT
484
485 /*
486  * The following checks use two arrays of 8 or 16 bits to store the
487  * bits that we want set or clear, respectively. They are in the
488  * low and high half of cmd->arg1 or cmd->d[0].
489  *
490  * We scan options and store the bits we find set. We succeed if
491  *
492  *      (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
493  *
494  * The code is sometimes optimized not to store additional variables.
495  */
496
497 static int
498 flags_match(ipfw_insn *cmd, uint8_t bits)
499 {
500         u_char want_clear;
501         bits = ~bits;
502
503         if (((cmd->arg1 & 0xff) & bits) != 0)
504                 return 0; /* some bits we want set were clear */
505
506         want_clear = (cmd->arg1 >> 8) & 0xff;
507         if ((want_clear & bits) != want_clear)
508                 return 0; /* some bits we want clear were set */
509         return 1;
510 }
511
512 static int
513 ipopts_match(struct ip *ip, ipfw_insn *cmd)
514 {
515         int optlen, bits = 0;
516         u_char *cp = (u_char *)(ip + 1);
517         int x = (ip->ip_hl << 2) - sizeof(struct ip);
518
519         for (; x > 0; x -= optlen, cp += optlen) {
520                 int opt = cp[IPOPT_OPTVAL];
521
522                 if (opt == IPOPT_EOL)
523                         break;
524
525                 if (opt == IPOPT_NOP) {
526                         optlen = 1;
527                 } else {
528                         optlen = cp[IPOPT_OLEN];
529                         if (optlen <= 0 || optlen > x)
530                                 return 0; /* invalid or truncated */
531                 }
532
533                 switch (opt) {
534                 case IPOPT_LSRR:
535                         bits |= IP_FW_IPOPT_LSRR;
536                         break;
537
538                 case IPOPT_SSRR:
539                         bits |= IP_FW_IPOPT_SSRR;
540                         break;
541
542                 case IPOPT_RR:
543                         bits |= IP_FW_IPOPT_RR;
544                         break;
545
546                 case IPOPT_TS:
547                         bits |= IP_FW_IPOPT_TS;
548                         break;
549
550                 default:
551                         break;
552                 }
553         }
554         return (flags_match(cmd, bits));
555 }
556
557 static int
558 tcpopts_match(struct ip *ip, ipfw_insn *cmd)
559 {
560         int optlen, bits = 0;
561         struct tcphdr *tcp = L3HDR(struct tcphdr,ip);
562         u_char *cp = (u_char *)(tcp + 1);
563         int x = (tcp->th_off << 2) - sizeof(struct tcphdr);
564
565         for (; x > 0; x -= optlen, cp += optlen) {
566                 int opt = cp[0];
567
568                 if (opt == TCPOPT_EOL)
569                         break;
570
571                 if (opt == TCPOPT_NOP) {
572                         optlen = 1;
573                 } else {
574                         optlen = cp[1];
575                         if (optlen <= 0)
576                                 break;
577                 }
578
579                 switch (opt) {
580                 case TCPOPT_MAXSEG:
581                         bits |= IP_FW_TCPOPT_MSS;
582                         break;
583
584                 case TCPOPT_WINDOW:
585                         bits |= IP_FW_TCPOPT_WINDOW;
586                         break;
587
588                 case TCPOPT_SACK_PERMITTED:
589                 case TCPOPT_SACK:
590                         bits |= IP_FW_TCPOPT_SACK;
591                         break;
592
593                 case TCPOPT_TIMESTAMP:
594                         bits |= IP_FW_TCPOPT_TS;
595                         break;
596
597                 case TCPOPT_CC:
598                 case TCPOPT_CCNEW:
599                 case TCPOPT_CCECHO:
600                         bits |= IP_FW_TCPOPT_CC;
601                         break;
602
603                 default:
604                         break;
605                 }
606         }
607         return (flags_match(cmd, bits));
608 }
609
610 static int
611 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd)
612 {
613         if (ifp == NULL)        /* no iface with this packet, match fails */
614                 return 0;
615
616         /* Check by name or by IP address */
617         if (cmd->name[0] != '\0') { /* match by name */
618                 /* Check name */
619                 if (cmd->p.glob) {
620                         if (kfnmatch(cmd->name, ifp->if_xname, 0) == 0)
621                                 return(1);
622                 } else {
623                         if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
624                                 return(1);
625                 }
626         } else {
627                 struct ifaddr_container *ifac;
628
629                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
630                         struct ifaddr *ia = ifac->ifa;
631
632                         if (ia->ifa_addr == NULL)
633                                 continue;
634                         if (ia->ifa_addr->sa_family != AF_INET)
635                                 continue;
636                         if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
637                             (ia->ifa_addr))->sin_addr.s_addr)
638                                 return(1);      /* match */
639                 }
640         }
641         return(0);      /* no match, fail ... */
642 }
643
644 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
645
646 /*
647  * We enter here when we have a rule with O_LOG.
648  * XXX this function alone takes about 2Kbytes of code!
649  */
650 static void
651 ipfw_log(struct ip_fw *f, u_int hlen, struct ether_header *eh,
652          struct mbuf *m, struct ifnet *oif)
653 {
654         char *action;
655         int limit_reached = 0;
656         char action2[40], proto[48], fragment[28];
657
658         fragment[0] = '\0';
659         proto[0] = '\0';
660
661         if (f == NULL) {        /* bogus pkt */
662                 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
663
664                 if (verbose_limit != 0 &&
665                     ctx->ipfw_norule_counter >= verbose_limit)
666                         return;
667                 ctx->ipfw_norule_counter++;
668                 if (ctx->ipfw_norule_counter == verbose_limit)
669                         limit_reached = verbose_limit;
670                 action = "Refuse";
671         } else {        /* O_LOG is the first action, find the real one */
672                 ipfw_insn *cmd = ACTION_PTR(f);
673                 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
674
675                 if (l->max_log != 0 && l->log_left == 0)
676                         return;
677                 l->log_left--;
678                 if (l->log_left == 0)
679                         limit_reached = l->max_log;
680                 cmd += F_LEN(cmd);      /* point to first action */
681                 if (cmd->opcode == O_PROB)
682                         cmd += F_LEN(cmd);
683
684                 action = action2;
685                 switch (cmd->opcode) {
686                 case O_DENY:
687                         action = "Deny";
688                         break;
689
690                 case O_REJECT:
691                         if (cmd->arg1==ICMP_REJECT_RST) {
692                                 action = "Reset";
693                         } else if (cmd->arg1==ICMP_UNREACH_HOST) {
694                                 action = "Reject";
695                         } else {
696                                 ksnprintf(SNPARGS(action2, 0), "Unreach %d",
697                                           cmd->arg1);
698                         }
699                         break;
700
701                 case O_ACCEPT:
702                         action = "Accept";
703                         break;
704
705                 case O_COUNT:
706                         action = "Count";
707                         break;
708
709                 case O_DIVERT:
710                         ksnprintf(SNPARGS(action2, 0), "Divert %d", cmd->arg1);
711                         break;
712
713                 case O_TEE:
714                         ksnprintf(SNPARGS(action2, 0), "Tee %d", cmd->arg1);
715                         break;
716
717                 case O_SKIPTO:
718                         ksnprintf(SNPARGS(action2, 0), "SkipTo %d", cmd->arg1);
719                         break;
720
721                 case O_PIPE:
722                         ksnprintf(SNPARGS(action2, 0), "Pipe %d", cmd->arg1);
723                         break;
724
725                 case O_QUEUE:
726                         ksnprintf(SNPARGS(action2, 0), "Queue %d", cmd->arg1);
727                         break;
728
729                 case O_FORWARD_IP:
730                         {
731                                 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
732                                 int len;
733
734                                 len = ksnprintf(SNPARGS(action2, 0),
735                                                 "Forward to %s",
736                                                 inet_ntoa(sa->sa.sin_addr));
737                                 if (sa->sa.sin_port) {
738                                         ksnprintf(SNPARGS(action2, len), ":%d",
739                                                   sa->sa.sin_port);
740                                 }
741                         }
742                         break;
743
744                 default:
745                         action = "UNKNOWN";
746                         break;
747                 }
748         }
749
750         if (hlen == 0) {        /* non-ip */
751                 ksnprintf(SNPARGS(proto, 0), "MAC");
752         } else {
753                 struct ip *ip = mtod(m, struct ip *);
754                 /* these three are all aliases to the same thing */
755                 struct icmp *const icmp = L3HDR(struct icmp, ip);
756                 struct tcphdr *const tcp = (struct tcphdr *)icmp;
757                 struct udphdr *const udp = (struct udphdr *)icmp;
758
759                 int ip_off, offset, ip_len;
760                 int len;
761
762                 if (eh != NULL) { /* layer 2 packets are as on the wire */
763                         ip_off = ntohs(ip->ip_off);
764                         ip_len = ntohs(ip->ip_len);
765                 } else {
766                         ip_off = ip->ip_off;
767                         ip_len = ip->ip_len;
768                 }
769                 offset = ip_off & IP_OFFMASK;
770                 switch (ip->ip_p) {
771                 case IPPROTO_TCP:
772                         len = ksnprintf(SNPARGS(proto, 0), "TCP %s",
773                                         inet_ntoa(ip->ip_src));
774                         if (offset == 0) {
775                                 ksnprintf(SNPARGS(proto, len), ":%d %s:%d",
776                                           ntohs(tcp->th_sport),
777                                           inet_ntoa(ip->ip_dst),
778                                           ntohs(tcp->th_dport));
779                         } else {
780                                 ksnprintf(SNPARGS(proto, len), " %s",
781                                           inet_ntoa(ip->ip_dst));
782                         }
783                         break;
784
785                 case IPPROTO_UDP:
786                         len = ksnprintf(SNPARGS(proto, 0), "UDP %s",
787                                         inet_ntoa(ip->ip_src));
788                         if (offset == 0) {
789                                 ksnprintf(SNPARGS(proto, len), ":%d %s:%d",
790                                           ntohs(udp->uh_sport),
791                                           inet_ntoa(ip->ip_dst),
792                                           ntohs(udp->uh_dport));
793                         } else {
794                                 ksnprintf(SNPARGS(proto, len), " %s",
795                                           inet_ntoa(ip->ip_dst));
796                         }
797                         break;
798
799                 case IPPROTO_ICMP:
800                         if (offset == 0) {
801                                 len = ksnprintf(SNPARGS(proto, 0),
802                                                 "ICMP:%u.%u ",
803                                                 icmp->icmp_type,
804                                                 icmp->icmp_code);
805                         } else {
806                                 len = ksnprintf(SNPARGS(proto, 0), "ICMP ");
807                         }
808                         len += ksnprintf(SNPARGS(proto, len), "%s",
809                                          inet_ntoa(ip->ip_src));
810                         ksnprintf(SNPARGS(proto, len), " %s",
811                                   inet_ntoa(ip->ip_dst));
812                         break;
813
814                 default:
815                         len = ksnprintf(SNPARGS(proto, 0), "P:%d %s", ip->ip_p,
816                                         inet_ntoa(ip->ip_src));
817                         ksnprintf(SNPARGS(proto, len), " %s",
818                                   inet_ntoa(ip->ip_dst));
819                         break;
820                 }
821
822                 if (ip_off & (IP_MF | IP_OFFMASK)) {
823                         ksnprintf(SNPARGS(fragment, 0), " (frag %d:%d@%d%s)",
824                                   ntohs(ip->ip_id), ip_len - (ip->ip_hl << 2),
825                                   offset << 3, (ip_off & IP_MF) ? "+" : "");
826                 }
827         }
828
829         if (oif || m->m_pkthdr.rcvif) {
830                 log(LOG_SECURITY | LOG_INFO,
831                     "ipfw: %d %s %s %s via %s%s\n",
832                     f ? f->rulenum : -1,
833                     action, proto, oif ? "out" : "in",
834                     oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
835                     fragment);
836         } else {
837                 log(LOG_SECURITY | LOG_INFO,
838                     "ipfw: %d %s %s [no if info]%s\n",
839                     f ? f->rulenum : -1,
840                     action, proto, fragment);
841         }
842
843         if (limit_reached) {
844                 log(LOG_SECURITY | LOG_NOTICE,
845                     "ipfw: limit %d reached on entry %d\n",
846                     limit_reached, f ? f->rulenum : -1);
847         }
848 }
849
850 #undef SNPARGS
851
852 /*
853  * IMPORTANT: the hash function for dynamic rules must be commutative
854  * in source and destination (ip,port), because rules are bidirectional
855  * and we want to find both in the same bucket.
856  */
857 static __inline int
858 hash_packet(struct ipfw_flow_id *id)
859 {
860         uint32_t i;
861
862         i = (id->dst_ip) ^ (id->src_ip) ^ (id->dst_port) ^ (id->src_port);
863         i &= (curr_dyn_buckets - 1);
864         return i;
865 }
866
867 /**
868  * unlink a dynamic rule from a chain. prev is a pointer to
869  * the previous one, q is a pointer to the rule to delete,
870  * head is a pointer to the head of the queue.
871  * Modifies q and potentially also head.
872  */
873 #define UNLINK_DYN_RULE(prev, head, q)                                  \
874 do {                                                                    \
875         ipfw_dyn_rule *old_q = q;                                       \
876                                                                         \
877         /* remove a refcount to the parent */                           \
878         if (q->dyn_type == O_LIMIT)                                     \
879                 q->parent->count--;                                     \
880         DEB(kprintf("-- unlink entry 0x%08x %d -> 0x%08x %d, %d left\n", \
881                 (q->id.src_ip), (q->id.src_port),                       \
882                 (q->id.dst_ip), (q->id.dst_port), dyn_count-1 ); )      \
883         if (prev != NULL)                                               \
884                 prev->next = q = q->next;                               \
885         else                                                            \
886                 head = q = q->next;                                     \
887         KASSERT(dyn_count > 0, ("invalid dyn count %u\n", dyn_count));  \
888         dyn_count--;                                                    \
889         kfree(old_q, M_IPFW);                                           \
890 } while (0)
891
892 #define TIME_LEQ(a, b)  ((int)((a) - (b)) <= 0)
893
894 /**
895  * Remove dynamic rules pointing to "rule", or all of them if rule == NULL.
896  *
897  * If keep_me == NULL, rules are deleted even if not expired,
898  * otherwise only expired rules are removed.
899  *
900  * The value of the second parameter is also used to point to identify
901  * a rule we absolutely do not want to remove (e.g. because we are
902  * holding a reference to it -- this is the case with O_LIMIT_PARENT
903  * rules). The pointer is only used for comparison, so any non-null
904  * value will do.
905  */
906 static void
907 remove_dyn_rule_locked(struct ip_fw *rule, ipfw_dyn_rule *keep_me)
908 {
909         static uint32_t last_remove = 0; /* XXX */
910
911 #define FORCE   (keep_me == NULL)
912
913         ipfw_dyn_rule *prev, *q;
914         int i, pass = 0, max_pass = 0, unlinked = 0;
915
916         if (ipfw_dyn_v == NULL || dyn_count == 0)
917                 return;
918         /* do not expire more than once per second, it is useless */
919         if (!FORCE && last_remove == time_second)
920                 return;
921         last_remove = time_second;
922
923         /*
924          * because O_LIMIT refer to parent rules, during the first pass only
925          * remove child and mark any pending LIMIT_PARENT, and remove
926          * them in a second pass.
927          */
928 next_pass:
929         for (i = 0; i < curr_dyn_buckets; i++) {
930                 for (prev = NULL, q = ipfw_dyn_v[i]; q;) {
931                         /*
932                          * Logic can become complex here, so we split tests.
933                          */
934                         if (q == keep_me)
935                                 goto next;
936                         if (rule != NULL && rule->stub != q->stub)
937                                 goto next; /* not the one we are looking for */
938                         if (q->dyn_type == O_LIMIT_PARENT) {
939                                 /*
940                                  * handle parent in the second pass,
941                                  * record we need one.
942                                  */
943                                 max_pass = 1;
944                                 if (pass == 0)
945                                         goto next;
946                                 if (FORCE && q->count != 0) {
947                                         /* XXX should not happen! */
948                                         kprintf("OUCH! cannot remove rule, "
949                                                 "count %d\n", q->count);
950                                 }
951                         } else {
952                                 if (!FORCE && !TIME_LEQ(q->expire, time_second))
953                                         goto next;
954                         }
955                         unlinked = 1;
956                         UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q);
957                         continue;
958 next:
959                         prev = q;
960                         q = q->next;
961                 }
962         }
963         if (pass++ < max_pass)
964                 goto next_pass;
965
966         if (unlinked)
967                 ++dyn_buckets_gen;
968
969 #undef FORCE
970 }
971
972 /**
973  * lookup a dynamic rule.
974  */
975 static ipfw_dyn_rule *
976 lookup_dyn_rule(struct ipfw_flow_id *pkt, int *match_direction,
977                 struct tcphdr *tcp)
978 {
979         /*
980          * stateful ipfw extensions.
981          * Lookup into dynamic session queue
982          */
983 #define MATCH_REVERSE   0
984 #define MATCH_FORWARD   1
985 #define MATCH_NONE      2
986 #define MATCH_UNKNOWN   3
987         int i, dir = MATCH_NONE;
988         ipfw_dyn_rule *prev, *q=NULL;
989
990         if (ipfw_dyn_v == NULL)
991                 goto done;      /* not found */
992
993         i = hash_packet(pkt);
994         for (prev = NULL, q = ipfw_dyn_v[i]; q != NULL;) {
995                 if (q->dyn_type == O_LIMIT_PARENT)
996                         goto next;
997
998                 if (TIME_LEQ(q->expire, time_second)) {
999                         /*
1000                          * Entry expired; skip.
1001                          * Let ipfw_tick() take care of it
1002                          */
1003                         goto next;
1004                 }
1005
1006                 if (pkt->proto == q->id.proto) {
1007                         if (pkt->src_ip == q->id.src_ip &&
1008                             pkt->dst_ip == q->id.dst_ip &&
1009                             pkt->src_port == q->id.src_port &&
1010                             pkt->dst_port == q->id.dst_port) {
1011                                 dir = MATCH_FORWARD;
1012                                 break;
1013                         }
1014                         if (pkt->src_ip == q->id.dst_ip &&
1015                             pkt->dst_ip == q->id.src_ip &&
1016                             pkt->src_port == q->id.dst_port &&
1017                             pkt->dst_port == q->id.src_port) {
1018                                 dir = MATCH_REVERSE;
1019                                 break;
1020                         }
1021                 }
1022 next:
1023                 prev = q;
1024                 q = q->next;
1025         }
1026         if (q == NULL)
1027                 goto done; /* q = NULL, not found */
1028
1029         if (pkt->proto == IPPROTO_TCP) { /* update state according to flags */
1030                 u_char flags = pkt->flags & (TH_FIN|TH_SYN|TH_RST);
1031
1032 #define BOTH_SYN        (TH_SYN | (TH_SYN << 8))
1033 #define BOTH_FIN        (TH_FIN | (TH_FIN << 8))
1034
1035                 q->state |= (dir == MATCH_FORWARD ) ? flags : (flags << 8);
1036                 switch (q->state) {
1037                 case TH_SYN:                            /* opening */
1038                         q->expire = time_second + dyn_syn_lifetime;
1039                         break;
1040
1041                 case BOTH_SYN:                  /* move to established */
1042                 case BOTH_SYN | TH_FIN :        /* one side tries to close */
1043                 case BOTH_SYN | (TH_FIN << 8) :
1044                         if (tcp) {
1045                                 uint32_t ack = ntohl(tcp->th_ack);
1046
1047 #define _SEQ_GE(a, b)   ((int)(a) - (int)(b) >= 0)
1048
1049                                 if (dir == MATCH_FORWARD) {
1050                                         if (q->ack_fwd == 0 ||
1051                                             _SEQ_GE(ack, q->ack_fwd))
1052                                                 q->ack_fwd = ack;
1053                                         else /* ignore out-of-sequence */
1054                                                 break;
1055                                 } else {
1056                                         if (q->ack_rev == 0 ||
1057                                             _SEQ_GE(ack, q->ack_rev))
1058                                                 q->ack_rev = ack;
1059                                         else /* ignore out-of-sequence */
1060                                                 break;
1061                                 }
1062 #undef _SEQ_GE
1063                         }
1064                         q->expire = time_second + dyn_ack_lifetime;
1065                         break;
1066
1067                 case BOTH_SYN | BOTH_FIN:       /* both sides closed */
1068                         KKASSERT(dyn_fin_lifetime < dyn_keepalive_period);
1069                         q->expire = time_second + dyn_fin_lifetime;
1070                         break;
1071
1072                 default:
1073 #if 0
1074                         /*
1075                          * reset or some invalid combination, but can also
1076                          * occur if we use keep-state the wrong way.
1077                          */
1078                         if ((q->state & ((TH_RST << 8) | TH_RST)) == 0)
1079                                 kprintf("invalid state: 0x%x\n", q->state);
1080 #endif
1081                         KKASSERT(dyn_rst_lifetime < dyn_keepalive_period);
1082                         q->expire = time_second + dyn_rst_lifetime;
1083                         break;
1084                 }
1085         } else if (pkt->proto == IPPROTO_UDP) {
1086                 q->expire = time_second + dyn_udp_lifetime;
1087         } else {
1088                 /* other protocols */
1089                 q->expire = time_second + dyn_short_lifetime;
1090         }
1091 done:
1092         if (match_direction)
1093                 *match_direction = dir;
1094         return q;
1095 }
1096
1097 static struct ip_fw *
1098 lookup_rule(struct ipfw_flow_id *pkt, int *match_direction, struct tcphdr *tcp,
1099             uint16_t len, int *deny)
1100 {
1101         struct ip_fw *rule = NULL;
1102         ipfw_dyn_rule *q;
1103         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
1104         uint32_t gen;
1105
1106         *deny = 0;
1107         gen = ctx->ipfw_gen;
1108
1109         lockmgr(&dyn_lock, LK_SHARED);
1110
1111         if (ctx->ipfw_gen != gen) {
1112                 /*
1113                  * Static rules had been change when we were waiting
1114                  * for the dynamic hash table lock; deny this packet,
1115                  * since it is _not_ known whether it is safe to keep
1116                  * iterating the static rules.
1117                  */
1118                 *deny = 1;
1119                 goto back;
1120         }
1121
1122         q = lookup_dyn_rule(pkt, match_direction, tcp);
1123         if (q == NULL) {
1124                 rule = NULL;
1125         } else {
1126                 rule = q->stub->rule[mycpuid];
1127                 KKASSERT(rule->stub == q->stub && rule->cpuid == mycpuid);
1128
1129                 /* XXX */
1130                 q->pcnt++;
1131                 q->bcnt += len;
1132         }
1133 back:
1134         lockmgr(&dyn_lock, LK_RELEASE);
1135         return rule;
1136 }
1137
1138 static void
1139 realloc_dynamic_table(void)
1140 {
1141         ipfw_dyn_rule **old_dyn_v;
1142         uint32_t old_curr_dyn_buckets;
1143
1144         KASSERT(dyn_buckets <= 65536 && (dyn_buckets & (dyn_buckets - 1)) == 0,
1145                 ("invalid dyn_buckets %d\n", dyn_buckets));
1146
1147         /* Save the current buckets array for later error recovery */
1148         old_dyn_v = ipfw_dyn_v;
1149         old_curr_dyn_buckets = curr_dyn_buckets;
1150
1151         curr_dyn_buckets = dyn_buckets;
1152         for (;;) {
1153                 ipfw_dyn_v = kmalloc(curr_dyn_buckets * sizeof(ipfw_dyn_rule *),
1154                                      M_IPFW, M_NOWAIT | M_ZERO);
1155                 if (ipfw_dyn_v != NULL || curr_dyn_buckets <= 2)
1156                         break;
1157
1158                 curr_dyn_buckets /= 2;
1159                 if (curr_dyn_buckets <= old_curr_dyn_buckets &&
1160                     old_dyn_v != NULL) {
1161                         /*
1162                          * Don't try allocating smaller buckets array, reuse
1163                          * the old one, which alreay contains enough buckets
1164                          */
1165                         break;
1166                 }
1167         }
1168
1169         if (ipfw_dyn_v != NULL) {
1170                 if (old_dyn_v != NULL)
1171                         kfree(old_dyn_v, M_IPFW);
1172         } else {
1173                 /* Allocation failed, restore old buckets array */
1174                 ipfw_dyn_v = old_dyn_v;
1175                 curr_dyn_buckets = old_curr_dyn_buckets;
1176         }
1177
1178         if (ipfw_dyn_v != NULL)
1179                 ++dyn_buckets_gen;
1180 }
1181
1182 /**
1183  * Install state of type 'type' for a dynamic session.
1184  * The hash table contains two type of rules:
1185  * - regular rules (O_KEEP_STATE)
1186  * - rules for sessions with limited number of sess per user
1187  *   (O_LIMIT). When they are created, the parent is
1188  *   increased by 1, and decreased on delete. In this case,
1189  *   the third parameter is the parent rule and not the chain.
1190  * - "parent" rules for the above (O_LIMIT_PARENT).
1191  */
1192 static ipfw_dyn_rule *
1193 add_dyn_rule(struct ipfw_flow_id *id, uint8_t dyn_type, struct ip_fw *rule)
1194 {
1195         ipfw_dyn_rule *r;
1196         int i;
1197
1198         if (ipfw_dyn_v == NULL ||
1199             (dyn_count == 0 && dyn_buckets != curr_dyn_buckets)) {
1200                 realloc_dynamic_table();
1201                 if (ipfw_dyn_v == NULL)
1202                         return NULL; /* failed ! */
1203         }
1204         i = hash_packet(id);
1205
1206         r = kmalloc(sizeof(*r), M_IPFW, M_NOWAIT | M_ZERO);
1207         if (r == NULL) {
1208                 kprintf ("sorry cannot allocate state\n");
1209                 return NULL;
1210         }
1211
1212         /* increase refcount on parent, and set pointer */
1213         if (dyn_type == O_LIMIT) {
1214                 ipfw_dyn_rule *parent = (ipfw_dyn_rule *)rule;
1215
1216                 if (parent->dyn_type != O_LIMIT_PARENT)
1217                         panic("invalid parent");
1218                 parent->count++;
1219                 r->parent = parent;
1220                 rule = parent->stub->rule[mycpuid];
1221                 KKASSERT(rule->stub == parent->stub);
1222         }
1223         KKASSERT(rule->cpuid == mycpuid && rule->stub != NULL);
1224
1225         r->id = *id;
1226         r->expire = time_second + dyn_syn_lifetime;
1227         r->stub = rule->stub;
1228         r->dyn_type = dyn_type;
1229         r->pcnt = r->bcnt = 0;
1230         r->count = 0;
1231
1232         r->bucket = i;
1233         r->next = ipfw_dyn_v[i];
1234         ipfw_dyn_v[i] = r;
1235         dyn_count++;
1236         dyn_buckets_gen++;
1237         DEB(kprintf("-- add dyn entry ty %d 0x%08x %d -> 0x%08x %d, total %d\n",
1238            dyn_type,
1239            (r->id.src_ip), (r->id.src_port),
1240            (r->id.dst_ip), (r->id.dst_port),
1241            dyn_count );)
1242         return r;
1243 }
1244
1245 /**
1246  * lookup dynamic parent rule using pkt and rule as search keys.
1247  * If the lookup fails, then install one.
1248  */
1249 static ipfw_dyn_rule *
1250 lookup_dyn_parent(struct ipfw_flow_id *pkt, struct ip_fw *rule)
1251 {
1252         ipfw_dyn_rule *q;
1253         int i;
1254
1255         if (ipfw_dyn_v) {
1256                 i = hash_packet(pkt);
1257                 for (q = ipfw_dyn_v[i]; q != NULL; q = q->next) {
1258                         if (q->dyn_type == O_LIMIT_PARENT &&
1259                             rule->stub == q->stub &&
1260                             pkt->proto == q->id.proto &&
1261                             pkt->src_ip == q->id.src_ip &&
1262                             pkt->dst_ip == q->id.dst_ip &&
1263                             pkt->src_port == q->id.src_port &&
1264                             pkt->dst_port == q->id.dst_port) {
1265                                 q->expire = time_second + dyn_short_lifetime;
1266                                 DEB(kprintf("lookup_dyn_parent found 0x%p\n",q);)
1267                                 return q;
1268                         }
1269                 }
1270         }
1271         return add_dyn_rule(pkt, O_LIMIT_PARENT, rule);
1272 }
1273
1274 /**
1275  * Install dynamic state for rule type cmd->o.opcode
1276  *
1277  * Returns 1 (failure) if state is not installed because of errors or because
1278  * session limitations are enforced.
1279  */
1280 static int
1281 install_state_locked(struct ip_fw *rule, ipfw_insn_limit *cmd,
1282                      struct ip_fw_args *args)
1283 {
1284         static int last_log; /* XXX */
1285
1286         ipfw_dyn_rule *q;
1287
1288         DEB(kprintf("-- install state type %d 0x%08x %u -> 0x%08x %u\n",
1289             cmd->o.opcode,
1290             (args->f_id.src_ip), (args->f_id.src_port),
1291             (args->f_id.dst_ip), (args->f_id.dst_port) );)
1292
1293         q = lookup_dyn_rule(&args->f_id, NULL, NULL);
1294         if (q != NULL) { /* should never occur */
1295                 if (last_log != time_second) {
1296                         last_log = time_second;
1297                         kprintf(" install_state: entry already present, done\n");
1298                 }
1299                 return 0;
1300         }
1301
1302         if (dyn_count >= dyn_max) {
1303                 /*
1304                  * Run out of slots, try to remove any expired rule.
1305                  */
1306                 remove_dyn_rule_locked(NULL, (ipfw_dyn_rule *)1);
1307                 if (dyn_count >= dyn_max) {
1308                         if (last_log != time_second) {
1309                                 last_log = time_second;
1310                                 kprintf("install_state: "
1311                                         "Too many dynamic rules\n");
1312                         }
1313                         return 1; /* cannot install, notify caller */
1314                 }
1315         }
1316
1317         switch (cmd->o.opcode) {
1318         case O_KEEP_STATE: /* bidir rule */
1319                 if (add_dyn_rule(&args->f_id, O_KEEP_STATE, rule) == NULL)
1320                         return 1;
1321                 break;
1322
1323         case O_LIMIT: /* limit number of sessions */
1324                 {
1325                         uint16_t limit_mask = cmd->limit_mask;
1326                         struct ipfw_flow_id id;
1327                         ipfw_dyn_rule *parent;
1328
1329                         DEB(kprintf("installing dyn-limit rule %d\n",
1330                             cmd->conn_limit);)
1331
1332                         id.dst_ip = id.src_ip = 0;
1333                         id.dst_port = id.src_port = 0;
1334                         id.proto = args->f_id.proto;
1335
1336                         if (limit_mask & DYN_SRC_ADDR)
1337                                 id.src_ip = args->f_id.src_ip;
1338                         if (limit_mask & DYN_DST_ADDR)
1339                                 id.dst_ip = args->f_id.dst_ip;
1340                         if (limit_mask & DYN_SRC_PORT)
1341                                 id.src_port = args->f_id.src_port;
1342                         if (limit_mask & DYN_DST_PORT)
1343                                 id.dst_port = args->f_id.dst_port;
1344
1345                         parent = lookup_dyn_parent(&id, rule);
1346                         if (parent == NULL) {
1347                                 kprintf("add parent failed\n");
1348                                 return 1;
1349                         }
1350
1351                         if (parent->count >= cmd->conn_limit) {
1352                                 /*
1353                                  * See if we can remove some expired rule.
1354                                  */
1355                                 remove_dyn_rule_locked(rule, parent);
1356                                 if (parent->count >= cmd->conn_limit) {
1357                                         if (fw_verbose &&
1358                                             last_log != time_second) {
1359                                                 last_log = time_second;
1360                                                 log(LOG_SECURITY | LOG_DEBUG,
1361                                                     "drop session, "
1362                                                     "too many entries\n");
1363                                         }
1364                                         return 1;
1365                                 }
1366                         }
1367                         if (add_dyn_rule(&args->f_id, O_LIMIT,
1368                                          (struct ip_fw *)parent) == NULL)
1369                                 return 1;
1370                 }
1371                 break;
1372         default:
1373                 kprintf("unknown dynamic rule type %u\n", cmd->o.opcode);
1374                 return 1;
1375         }
1376         lookup_dyn_rule(&args->f_id, NULL, NULL); /* XXX just set lifetime */
1377         return 0;
1378 }
1379
1380 static int
1381 install_state(struct ip_fw *rule, ipfw_insn_limit *cmd,
1382               struct ip_fw_args *args, int *deny)
1383 {
1384         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
1385         uint32_t gen;
1386         int ret = 0;
1387
1388         *deny = 0;
1389         gen = ctx->ipfw_gen;
1390
1391         lockmgr(&dyn_lock, LK_EXCLUSIVE);
1392         if (ctx->ipfw_gen != gen) {
1393                 /* See the comment in lookup_rule() */
1394                 *deny = 1;
1395         } else {
1396                 ret = install_state_locked(rule, cmd, args);
1397         }
1398         lockmgr(&dyn_lock, LK_RELEASE);
1399
1400         return ret;
1401 }
1402
1403 /*
1404  * Transmit a TCP packet, containing either a RST or a keepalive.
1405  * When flags & TH_RST, we are sending a RST packet, because of a
1406  * "reset" action matched the packet.
1407  * Otherwise we are sending a keepalive, and flags & TH_
1408  */
1409 static void
1410 send_pkt(struct ipfw_flow_id *id, uint32_t seq, uint32_t ack, int flags)
1411 {
1412         struct mbuf *m;
1413         struct ip *ip;
1414         struct tcphdr *tcp;
1415         struct route sro;       /* fake route */
1416
1417         MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1418         if (m == NULL)
1419                 return;
1420         m->m_pkthdr.rcvif = NULL;
1421         m->m_pkthdr.len = m->m_len = sizeof(struct ip) + sizeof(struct tcphdr);
1422         m->m_data += max_linkhdr;
1423
1424         ip = mtod(m, struct ip *);
1425         bzero(ip, m->m_len);
1426         tcp = (struct tcphdr *)(ip + 1); /* no IP options */
1427         ip->ip_p = IPPROTO_TCP;
1428         tcp->th_off = 5;
1429
1430         /*
1431          * Assume we are sending a RST (or a keepalive in the reverse
1432          * direction), swap src and destination addresses and ports.
1433          */
1434         ip->ip_src.s_addr = htonl(id->dst_ip);
1435         ip->ip_dst.s_addr = htonl(id->src_ip);
1436         tcp->th_sport = htons(id->dst_port);
1437         tcp->th_dport = htons(id->src_port);
1438         if (flags & TH_RST) {   /* we are sending a RST */
1439                 if (flags & TH_ACK) {
1440                         tcp->th_seq = htonl(ack);
1441                         tcp->th_ack = htonl(0);
1442                         tcp->th_flags = TH_RST;
1443                 } else {
1444                         if (flags & TH_SYN)
1445                                 seq++;
1446                         tcp->th_seq = htonl(0);
1447                         tcp->th_ack = htonl(seq);
1448                         tcp->th_flags = TH_RST | TH_ACK;
1449                 }
1450         } else {
1451                 /*
1452                  * We are sending a keepalive. flags & TH_SYN determines
1453                  * the direction, forward if set, reverse if clear.
1454                  * NOTE: seq and ack are always assumed to be correct
1455                  * as set by the caller. This may be confusing...
1456                  */
1457                 if (flags & TH_SYN) {
1458                         /*
1459                          * we have to rewrite the correct addresses!
1460                          */
1461                         ip->ip_dst.s_addr = htonl(id->dst_ip);
1462                         ip->ip_src.s_addr = htonl(id->src_ip);
1463                         tcp->th_dport = htons(id->dst_port);
1464                         tcp->th_sport = htons(id->src_port);
1465                 }
1466                 tcp->th_seq = htonl(seq);
1467                 tcp->th_ack = htonl(ack);
1468                 tcp->th_flags = TH_ACK;
1469         }
1470
1471         /*
1472          * set ip_len to the payload size so we can compute
1473          * the tcp checksum on the pseudoheader
1474          * XXX check this, could save a couple of words ?
1475          */
1476         ip->ip_len = htons(sizeof(struct tcphdr));
1477         tcp->th_sum = in_cksum(m, m->m_pkthdr.len);
1478
1479         /*
1480          * now fill fields left out earlier
1481          */
1482         ip->ip_ttl = ip_defttl;
1483         ip->ip_len = m->m_pkthdr.len;
1484
1485         bzero(&sro, sizeof(sro));
1486         ip_rtaddr(ip->ip_dst, &sro);
1487
1488         m->m_pkthdr.fw_flags |= IPFW_MBUF_GENERATED;
1489         ip_output(m, NULL, &sro, 0, NULL, NULL);
1490         if (sro.ro_rt)
1491                 RTFREE(sro.ro_rt);
1492 }
1493
1494 /*
1495  * sends a reject message, consuming the mbuf passed as an argument.
1496  */
1497 static void
1498 send_reject(struct ip_fw_args *args, int code, int offset, int ip_len)
1499 {
1500         if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */
1501                 /* We need the IP header in host order for icmp_error(). */
1502                 if (args->eh != NULL) {
1503                         struct ip *ip = mtod(args->m, struct ip *);
1504
1505                         ip->ip_len = ntohs(ip->ip_len);
1506                         ip->ip_off = ntohs(ip->ip_off);
1507                 }
1508                 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
1509         } else if (offset == 0 && args->f_id.proto == IPPROTO_TCP) {
1510                 struct tcphdr *const tcp =
1511                     L3HDR(struct tcphdr, mtod(args->m, struct ip *));
1512
1513                 if ((tcp->th_flags & TH_RST) == 0) {
1514                         send_pkt(&args->f_id, ntohl(tcp->th_seq),
1515                                  ntohl(tcp->th_ack), tcp->th_flags | TH_RST);
1516                 }
1517                 m_freem(args->m);
1518         } else {
1519                 m_freem(args->m);
1520         }
1521         args->m = NULL;
1522 }
1523
1524 /**
1525  *
1526  * Given an ip_fw *, lookup_next_rule will return a pointer
1527  * to the next rule, which can be either the jump
1528  * target (for skipto instructions) or the next one in the list (in
1529  * all other cases including a missing jump target).
1530  * The result is also written in the "next_rule" field of the rule.
1531  * Backward jumps are not allowed, so start looking from the next
1532  * rule...
1533  *
1534  * This never returns NULL -- in case we do not have an exact match,
1535  * the next rule is returned. When the ruleset is changed,
1536  * pointers are flushed so we are always correct.
1537  */
1538
1539 static struct ip_fw *
1540 lookup_next_rule(struct ip_fw *me)
1541 {
1542         struct ip_fw *rule = NULL;
1543         ipfw_insn *cmd;
1544
1545         /* look for action, in case it is a skipto */
1546         cmd = ACTION_PTR(me);
1547         if (cmd->opcode == O_LOG)
1548                 cmd += F_LEN(cmd);
1549         if (cmd->opcode == O_SKIPTO) {
1550                 for (rule = me->next; rule; rule = rule->next) {
1551                         if (rule->rulenum >= cmd->arg1)
1552                                 break;
1553                 }
1554         }
1555         if (rule == NULL)                       /* failure or not a skipto */
1556                 rule = me->next;
1557         me->next_rule = rule;
1558         return rule;
1559 }
1560
1561 /*
1562  * The main check routine for the firewall.
1563  *
1564  * All arguments are in args so we can modify them and return them
1565  * back to the caller.
1566  *
1567  * Parameters:
1568  *
1569  *      args->m (in/out) The packet; we set to NULL when/if we nuke it.
1570  *              Starts with the IP header.
1571  *      args->eh (in)   Mac header if present, or NULL for layer3 packet.
1572  *      args->oif       Outgoing interface, or NULL if packet is incoming.
1573  *              The incoming interface is in the mbuf. (in)
1574  *
1575  *      args->rule      Pointer to the last matching rule (in/out)
1576  *      args->f_id      Addresses grabbed from the packet (out)
1577  *
1578  * Return value:
1579  *
1580  *      IP_FW_PORT_DENY_FLAG    the packet must be dropped.
1581  *      0       The packet is to be accepted and routed normally OR
1582  *              the packet was denied/rejected and has been dropped;
1583  *              in the latter case, *m is equal to NULL upon return.
1584  *      port    Divert the packet to port, with these caveats:
1585  *
1586  *              - If IP_FW_PORT_TEE_FLAG is set, tee the packet instead
1587  *                of diverting it (ie, 'ipfw tee').
1588  *
1589  *              - If IP_FW_PORT_DYNT_FLAG is set, interpret the lower
1590  *                16 bits as a dummynet pipe number instead of diverting
1591  */
1592
1593 static int
1594 ipfw_chk(struct ip_fw_args *args)
1595 {
1596         /*
1597          * Local variables hold state during the processing of a packet.
1598          *
1599          * IMPORTANT NOTE: to speed up the processing of rules, there
1600          * are some assumption on the values of the variables, which
1601          * are documented here. Should you change them, please check
1602          * the implementation of the various instructions to make sure
1603          * that they still work.
1604          *
1605          * args->eh     The MAC header. It is non-null for a layer2
1606          *      packet, it is NULL for a layer-3 packet.
1607          *
1608          * m | args->m  Pointer to the mbuf, as received from the caller.
1609          *      It may change if ipfw_chk() does an m_pullup, or if it
1610          *      consumes the packet because it calls send_reject().
1611          *      XXX This has to change, so that ipfw_chk() never modifies
1612          *      or consumes the buffer.
1613          * ip   is simply an alias of the value of m, and it is kept
1614          *      in sync with it (the packet is  supposed to start with
1615          *      the ip header).
1616          */
1617         struct mbuf *m = args->m;
1618         struct ip *ip = mtod(m, struct ip *);
1619
1620         /*
1621          * oif | args->oif      If NULL, ipfw_chk has been called on the
1622          *      inbound path (ether_input, ip_input).
1623          *      If non-NULL, ipfw_chk has been called on the outbound path
1624          *      (ether_output, ip_output).
1625          */
1626         struct ifnet *oif = args->oif;
1627
1628         struct ip_fw *f = NULL;         /* matching rule */
1629         int retval = IP_FW_PASS;
1630         struct m_tag *mtag;
1631         struct divert_info *divinfo;
1632
1633         /*
1634          * hlen The length of the IPv4 header.
1635          *      hlen >0 means we have an IPv4 packet.
1636          */
1637         u_int hlen = 0;         /* hlen >0 means we have an IP pkt */
1638
1639         /*
1640          * offset       The offset of a fragment. offset != 0 means that
1641          *      we have a fragment at this offset of an IPv4 packet.
1642          *      offset == 0 means that (if this is an IPv4 packet)
1643          *      this is the first or only fragment.
1644          */
1645         u_short offset = 0;
1646
1647         /*
1648          * Local copies of addresses. They are only valid if we have
1649          * an IP packet.
1650          *
1651          * proto        The protocol. Set to 0 for non-ip packets,
1652          *      or to the protocol read from the packet otherwise.
1653          *      proto != 0 means that we have an IPv4 packet.
1654          *
1655          * src_port, dst_port   port numbers, in HOST format. Only
1656          *      valid for TCP and UDP packets.
1657          *
1658          * src_ip, dst_ip       ip addresses, in NETWORK format.
1659          *      Only valid for IPv4 packets.
1660          */
1661         uint8_t proto;
1662         uint16_t src_port = 0, dst_port = 0;    /* NOTE: host format    */
1663         struct in_addr src_ip, dst_ip;          /* NOTE: network format */
1664         uint16_t ip_len = 0;
1665
1666         /*
1667          * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1668          *      MATCH_NONE when checked and not matched (dyn_f = NULL),
1669          *      MATCH_FORWARD or MATCH_REVERSE otherwise (dyn_f != NULL)
1670          */
1671         int dyn_dir = MATCH_UNKNOWN;
1672         struct ip_fw *dyn_f = NULL;
1673         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
1674
1675         if (m->m_pkthdr.fw_flags & IPFW_MBUF_GENERATED)
1676                 return IP_FW_PASS;      /* accept */
1677
1678         if (args->eh == NULL ||         /* layer 3 packet */
1679             (m->m_pkthdr.len >= sizeof(struct ip) &&
1680              ntohs(args->eh->ether_type) == ETHERTYPE_IP))
1681                 hlen = ip->ip_hl << 2;
1682
1683         /*
1684          * Collect parameters into local variables for faster matching.
1685          */
1686         if (hlen == 0) {        /* do not grab addresses for non-ip pkts */
1687                 proto = args->f_id.proto = 0;   /* mark f_id invalid */
1688                 goto after_ip_checks;
1689         }
1690
1691         proto = args->f_id.proto = ip->ip_p;
1692         src_ip = ip->ip_src;
1693         dst_ip = ip->ip_dst;
1694         if (args->eh != NULL) { /* layer 2 packets are as on the wire */
1695                 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1696                 ip_len = ntohs(ip->ip_len);
1697         } else {
1698                 offset = ip->ip_off & IP_OFFMASK;
1699                 ip_len = ip->ip_len;
1700         }
1701
1702 #define PULLUP_TO(len)                          \
1703 do {                                            \
1704         if (m->m_len < (len)) {                 \
1705                 args->m = m = m_pullup(m, (len));\
1706                 if (m == NULL)                  \
1707                         goto pullup_failed;     \
1708                 ip = mtod(m, struct ip *);      \
1709         }                                       \
1710 } while (0)
1711
1712         if (offset == 0) {
1713                 switch (proto) {
1714                 case IPPROTO_TCP:
1715                         {
1716                                 struct tcphdr *tcp;
1717
1718                                 PULLUP_TO(hlen + sizeof(struct tcphdr));
1719                                 tcp = L3HDR(struct tcphdr, ip);
1720                                 dst_port = tcp->th_dport;
1721                                 src_port = tcp->th_sport;
1722                                 args->f_id.flags = tcp->th_flags;
1723                         }
1724                         break;
1725
1726                 case IPPROTO_UDP:
1727                         {
1728                                 struct udphdr *udp;
1729
1730                                 PULLUP_TO(hlen + sizeof(struct udphdr));
1731                                 udp = L3HDR(struct udphdr, ip);
1732                                 dst_port = udp->uh_dport;
1733                                 src_port = udp->uh_sport;
1734                         }
1735                         break;
1736
1737                 case IPPROTO_ICMP:
1738                         PULLUP_TO(hlen + 4);    /* type, code and checksum. */
1739                         args->f_id.flags = L3HDR(struct icmp, ip)->icmp_type;
1740                         break;
1741
1742                 default:
1743                         break;
1744                 }
1745         }
1746
1747 #undef PULLUP_TO
1748
1749         args->f_id.src_ip = ntohl(src_ip.s_addr);
1750         args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1751         args->f_id.src_port = src_port = ntohs(src_port);
1752         args->f_id.dst_port = dst_port = ntohs(dst_port);
1753
1754 after_ip_checks:
1755         if (args->rule) {
1756                 /*
1757                  * Packet has already been tagged. Look for the next rule
1758                  * to restart processing.
1759                  *
1760                  * If fw_one_pass != 0 then just accept it.
1761                  * XXX should not happen here, but optimized out in
1762                  * the caller.
1763                  */
1764                 if (fw_one_pass)
1765                         return IP_FW_PASS;
1766
1767                 /* This rule is being/has been flushed */
1768                 if (ipfw_flushing)
1769                         return IP_FW_DENY;
1770
1771                 KASSERT(args->rule->cpuid == mycpuid,
1772                         ("rule used on cpu%d\n", mycpuid));
1773
1774                 /* This rule was deleted */
1775                 if (args->rule->rule_flags & IPFW_RULE_F_INVALID)
1776                         return IP_FW_DENY;
1777
1778                 f = args->rule->next_rule;
1779                 if (f == NULL)
1780                         f = lookup_next_rule(args->rule);
1781         } else {
1782                 /*
1783                  * Find the starting rule. It can be either the first
1784                  * one, or the one after divert_rule if asked so.
1785                  */
1786                 int skipto;
1787
1788                 mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL);
1789                 if (mtag != NULL) {
1790                         divinfo = m_tag_data(mtag);
1791                         skipto = divinfo->skipto;
1792                 } else {
1793                         skipto = 0;
1794                 }
1795
1796                 f = ctx->ipfw_layer3_chain;
1797                 if (args->eh == NULL && skipto != 0) {
1798                         /* No skipto during rule flushing */
1799                         if (ipfw_flushing)
1800                                 return IP_FW_DENY;
1801
1802                         if (skipto >= IPFW_DEFAULT_RULE)
1803                                 return IP_FW_DENY; /* invalid */
1804
1805                         while (f && f->rulenum <= skipto)
1806                                 f = f->next;
1807                         if (f == NULL)  /* drop packet */
1808                                 return IP_FW_DENY;
1809                 } else if (ipfw_flushing) {
1810                         /* Rules are being flushed; skip to default rule */
1811                         f = ctx->ipfw_default_rule;
1812                 }
1813         }
1814         if ((mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
1815                 m_tag_delete(m, mtag);
1816
1817         /*
1818          * Now scan the rules, and parse microinstructions for each rule.
1819          */
1820         for (; f; f = f->next) {
1821                 int l, cmdlen;
1822                 ipfw_insn *cmd;
1823                 int skip_or; /* skip rest of OR block */
1824
1825 again:
1826                 if (ctx->ipfw_set_disable & (1 << f->set))
1827                         continue;
1828
1829                 skip_or = 0;
1830                 for (l = f->cmd_len, cmd = f->cmd; l > 0;
1831                      l -= cmdlen, cmd += cmdlen) {
1832                         int match, deny;
1833
1834                         /*
1835                          * check_body is a jump target used when we find a
1836                          * CHECK_STATE, and need to jump to the body of
1837                          * the target rule.
1838                          */
1839
1840 check_body:
1841                         cmdlen = F_LEN(cmd);
1842                         /*
1843                          * An OR block (insn_1 || .. || insn_n) has the
1844                          * F_OR bit set in all but the last instruction.
1845                          * The first match will set "skip_or", and cause
1846                          * the following instructions to be skipped until
1847                          * past the one with the F_OR bit clear.
1848                          */
1849                         if (skip_or) {          /* skip this instruction */
1850                                 if ((cmd->len & F_OR) == 0)
1851                                         skip_or = 0;    /* next one is good */
1852                                 continue;
1853                         }
1854                         match = 0; /* set to 1 if we succeed */
1855
1856                         switch (cmd->opcode) {
1857                         /*
1858                          * The first set of opcodes compares the packet's
1859                          * fields with some pattern, setting 'match' if a
1860                          * match is found. At the end of the loop there is
1861                          * logic to deal with F_NOT and F_OR flags associated
1862                          * with the opcode.
1863                          */
1864                         case O_NOP:
1865                                 match = 1;
1866                                 break;
1867
1868                         case O_FORWARD_MAC:
1869                                 kprintf("ipfw: opcode %d unimplemented\n",
1870                                         cmd->opcode);
1871                                 break;
1872
1873                         case O_GID:
1874                         case O_UID:
1875                                 /*
1876                                  * We only check offset == 0 && proto != 0,
1877                                  * as this ensures that we have an IPv4
1878                                  * packet with the ports info.
1879                                  */
1880                                 if (offset!=0)
1881                                         break;
1882                             {
1883                                 struct inpcbinfo *pi;
1884                                 int wildcard;
1885                                 struct inpcb *pcb;
1886
1887                                 if (proto == IPPROTO_TCP) {
1888                                         wildcard = 0;
1889                                         pi = &tcbinfo[mycpu->gd_cpuid];
1890                                 } else if (proto == IPPROTO_UDP) {
1891                                         wildcard = 1;
1892                                         pi = &udbinfo;
1893                                 } else
1894                                         break;
1895
1896                                 pcb =  (oif) ?
1897                                         in_pcblookup_hash(pi,
1898                                             dst_ip, htons(dst_port),
1899                                             src_ip, htons(src_port),
1900                                             wildcard, oif) :
1901                                         in_pcblookup_hash(pi,
1902                                             src_ip, htons(src_port),
1903                                             dst_ip, htons(dst_port),
1904                                             wildcard, NULL);
1905
1906                                 if (pcb == NULL || pcb->inp_socket == NULL)
1907                                         break;
1908
1909                                 if (cmd->opcode == O_UID) {
1910 #define socheckuid(a,b) ((a)->so_cred->cr_uid != (b))
1911                                         match =
1912                                           !socheckuid(pcb->inp_socket,
1913                                            (uid_t)((ipfw_insn_u32 *)cmd)->d[0]);
1914 #undef socheckuid
1915                                 } else  {
1916                                         match = groupmember(
1917                                             (uid_t)((ipfw_insn_u32 *)cmd)->d[0],
1918                                             pcb->inp_socket->so_cred);
1919                                 }
1920                             }
1921                                 break;
1922
1923                         case O_RECV:
1924                                 match = iface_match(m->m_pkthdr.rcvif,
1925                                     (ipfw_insn_if *)cmd);
1926                                 break;
1927
1928                         case O_XMIT:
1929                                 match = iface_match(oif, (ipfw_insn_if *)cmd);
1930                                 break;
1931
1932                         case O_VIA:
1933                                 match = iface_match(oif ? oif :
1934                                     m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd);
1935                                 break;
1936
1937                         case O_MACADDR2:
1938                                 if (args->eh != NULL) { /* have MAC header */
1939                                         uint32_t *want = (uint32_t *)
1940                                                 ((ipfw_insn_mac *)cmd)->addr;
1941                                         uint32_t *mask = (uint32_t *)
1942                                                 ((ipfw_insn_mac *)cmd)->mask;
1943                                         uint32_t *hdr = (uint32_t *)args->eh;
1944
1945                                         match =
1946                                         (want[0] == (hdr[0] & mask[0]) &&
1947                                          want[1] == (hdr[1] & mask[1]) &&
1948                                          want[2] == (hdr[2] & mask[2]));
1949                                 }
1950                                 break;
1951
1952                         case O_MAC_TYPE:
1953                                 if (args->eh != NULL) {
1954                                         uint16_t t =
1955                                             ntohs(args->eh->ether_type);
1956                                         uint16_t *p =
1957                                             ((ipfw_insn_u16 *)cmd)->ports;
1958                                         int i;
1959
1960                                         /* Special vlan handling */
1961                                         if (m->m_flags & M_VLANTAG)
1962                                                 t = ETHERTYPE_VLAN;
1963
1964                                         for (i = cmdlen - 1; !match && i > 0;
1965                                              i--, p += 2) {
1966                                                 match =
1967                                                 (t >= p[0] && t <= p[1]);
1968                                         }
1969                                 }
1970                                 break;
1971
1972                         case O_FRAG:
1973                                 match = (hlen > 0 && offset != 0);
1974                                 break;
1975
1976                         case O_IN:      /* "out" is "not in" */
1977                                 match = (oif == NULL);
1978                                 break;
1979
1980                         case O_LAYER2:
1981                                 match = (args->eh != NULL);
1982                                 break;
1983
1984                         case O_PROTO:
1985                                 /*
1986                                  * We do not allow an arg of 0 so the
1987                                  * check of "proto" only suffices.
1988                                  */
1989                                 match = (proto == cmd->arg1);
1990                                 break;
1991
1992                         case O_IP_SRC:
1993                                 match = (hlen > 0 &&
1994                                     ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1995                                     src_ip.s_addr);
1996                                 break;
1997
1998                         case O_IP_SRC_MASK:
1999                                 match = (hlen > 0 &&
2000                                     ((ipfw_insn_ip *)cmd)->addr.s_addr ==
2001                                      (src_ip.s_addr &
2002                                      ((ipfw_insn_ip *)cmd)->mask.s_addr));
2003                                 break;
2004
2005                         case O_IP_SRC_ME:
2006                                 if (hlen > 0) {
2007                                         struct ifnet *tif;
2008
2009                                         tif = INADDR_TO_IFP(&src_ip);
2010                                         match = (tif != NULL);
2011                                 }
2012                                 break;
2013
2014                         case O_IP_DST_SET:
2015                         case O_IP_SRC_SET:
2016                                 if (hlen > 0) {
2017                                         uint32_t *d = (uint32_t *)(cmd + 1);
2018                                         uint32_t addr =
2019                                             cmd->opcode == O_IP_DST_SET ?
2020                                                 args->f_id.dst_ip :
2021                                                 args->f_id.src_ip;
2022
2023                                         if (addr < d[0])
2024                                                 break;
2025                                         addr -= d[0]; /* subtract base */
2026                                         match =
2027                                         (addr < cmd->arg1) &&
2028                                          (d[1 + (addr >> 5)] &
2029                                           (1 << (addr & 0x1f)));
2030                                 }
2031                                 break;
2032
2033                         case O_IP_DST:
2034                                 match = (hlen > 0 &&
2035                                     ((ipfw_insn_ip *)cmd)->addr.s_addr ==
2036                                     dst_ip.s_addr);
2037                                 break;
2038
2039                         case O_IP_DST_MASK:
2040                                 match = (hlen > 0) &&
2041                                     (((ipfw_insn_ip *)cmd)->addr.s_addr ==
2042                                      (dst_ip.s_addr &
2043                                      ((ipfw_insn_ip *)cmd)->mask.s_addr));
2044                                 break;
2045
2046                         case O_IP_DST_ME:
2047                                 if (hlen > 0) {
2048                                         struct ifnet *tif;
2049
2050                                         tif = INADDR_TO_IFP(&dst_ip);
2051                                         match = (tif != NULL);
2052                                 }
2053                                 break;
2054
2055                         case O_IP_SRCPORT:
2056                         case O_IP_DSTPORT:
2057                                 /*
2058                                  * offset == 0 && proto != 0 is enough
2059                                  * to guarantee that we have an IPv4
2060                                  * packet with port info.
2061                                  */
2062                                 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP)
2063                                     && offset == 0) {
2064                                         uint16_t x =
2065                                             (cmd->opcode == O_IP_SRCPORT) ?
2066                                                 src_port : dst_port ;
2067                                         uint16_t *p =
2068                                             ((ipfw_insn_u16 *)cmd)->ports;
2069                                         int i;
2070
2071                                         for (i = cmdlen - 1; !match && i > 0;
2072                                              i--, p += 2) {
2073                                                 match =
2074                                                 (x >= p[0] && x <= p[1]);
2075                                         }
2076                                 }
2077                                 break;
2078
2079                         case O_ICMPTYPE:
2080                                 match = (offset == 0 && proto==IPPROTO_ICMP &&
2081                                     icmptype_match(ip, (ipfw_insn_u32 *)cmd));
2082                                 break;
2083
2084                         case O_IPOPT:
2085                                 match = (hlen > 0 && ipopts_match(ip, cmd));
2086                                 break;
2087
2088                         case O_IPVER:
2089                                 match = (hlen > 0 && cmd->arg1 == ip->ip_v);
2090                                 break;
2091
2092                         case O_IPTTL:
2093                                 match = (hlen > 0 && cmd->arg1 == ip->ip_ttl);
2094                                 break;
2095
2096                         case O_IPID:
2097                                 match = (hlen > 0 &&
2098                                     cmd->arg1 == ntohs(ip->ip_id));
2099                                 break;
2100
2101                         case O_IPLEN:
2102                                 match = (hlen > 0 && cmd->arg1 == ip_len);
2103                                 break;
2104
2105                         case O_IPPRECEDENCE:
2106                                 match = (hlen > 0 &&
2107                                     (cmd->arg1 == (ip->ip_tos & 0xe0)));
2108                                 break;
2109
2110                         case O_IPTOS:
2111                                 match = (hlen > 0 &&
2112                                     flags_match(cmd, ip->ip_tos));
2113                                 break;
2114
2115                         case O_TCPFLAGS:
2116                                 match = (proto == IPPROTO_TCP && offset == 0 &&
2117                                     flags_match(cmd,
2118                                         L3HDR(struct tcphdr,ip)->th_flags));
2119                                 break;
2120
2121                         case O_TCPOPTS:
2122                                 match = (proto == IPPROTO_TCP && offset == 0 &&
2123                                     tcpopts_match(ip, cmd));
2124                                 break;
2125
2126                         case O_TCPSEQ:
2127                                 match = (proto == IPPROTO_TCP && offset == 0 &&
2128                                     ((ipfw_insn_u32 *)cmd)->d[0] ==
2129                                         L3HDR(struct tcphdr,ip)->th_seq);
2130                                 break;
2131
2132                         case O_TCPACK:
2133                                 match = (proto == IPPROTO_TCP && offset == 0 &&
2134                                     ((ipfw_insn_u32 *)cmd)->d[0] ==
2135                                         L3HDR(struct tcphdr,ip)->th_ack);
2136                                 break;
2137
2138                         case O_TCPWIN:
2139                                 match = (proto == IPPROTO_TCP && offset == 0 &&
2140                                     cmd->arg1 ==
2141                                         L3HDR(struct tcphdr,ip)->th_win);
2142                                 break;
2143
2144                         case O_ESTAB:
2145                                 /* reject packets which have SYN only */
2146                                 /* XXX should i also check for TH_ACK ? */
2147                                 match = (proto == IPPROTO_TCP && offset == 0 &&
2148                                     (L3HDR(struct tcphdr,ip)->th_flags &
2149                                      (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
2150                                 break;
2151
2152                         case O_LOG:
2153                                 if (fw_verbose)
2154                                         ipfw_log(f, hlen, args->eh, m, oif);
2155                                 match = 1;
2156                                 break;
2157
2158                         case O_PROB:
2159                                 match = (krandom() <
2160                                         ((ipfw_insn_u32 *)cmd)->d[0]);
2161                                 break;
2162
2163                         /*
2164                          * The second set of opcodes represents 'actions',
2165                          * i.e. the terminal part of a rule once the packet
2166                          * matches all previous patterns.
2167                          * Typically there is only one action for each rule,
2168                          * and the opcode is stored at the end of the rule
2169                          * (but there are exceptions -- see below).
2170                          *
2171                          * In general, here we set retval and terminate the
2172                          * outer loop (would be a 'break 3' in some language,
2173                          * but we need to do a 'goto done').
2174                          *
2175                          * Exceptions:
2176                          * O_COUNT and O_SKIPTO actions:
2177                          *   instead of terminating, we jump to the next rule
2178                          *   ('goto next_rule', equivalent to a 'break 2'),
2179                          *   or to the SKIPTO target ('goto again' after
2180                          *   having set f, cmd and l), respectively.
2181                          *
2182                          * O_LIMIT and O_KEEP_STATE: these opcodes are
2183                          *   not real 'actions', and are stored right
2184                          *   before the 'action' part of the rule.
2185                          *   These opcodes try to install an entry in the
2186                          *   state tables; if successful, we continue with
2187                          *   the next opcode (match=1; break;), otherwise
2188                          *   the packet must be dropped ('goto done' after
2189                          *   setting retval).  If static rules are changed
2190                          *   during the state installation, the packet will
2191                          *   be dropped and rule's stats will not beupdated
2192                          *   ('return IP_FW_DENY').
2193                          *
2194                          * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2195                          *   cause a lookup of the state table, and a jump
2196                          *   to the 'action' part of the parent rule
2197                          *   ('goto check_body') if an entry is found, or
2198                          *   (CHECK_STATE only) a jump to the next rule if
2199                          *   the entry is not found ('goto next_rule').
2200                          *   The result of the lookup is cached to make
2201                          *   further instances of these opcodes are
2202                          *   effectively NOPs.  If static rules are changed
2203                          *   during the state looking up, the packet will
2204                          *   be dropped and rule's stats will not be updated
2205                          *   ('return IP_FW_DENY').
2206                          */
2207                         case O_LIMIT:
2208                         case O_KEEP_STATE:
2209                                 if (!(f->rule_flags & IPFW_RULE_F_STATE)) {
2210                                         kprintf("%s rule (%d) is not ready "
2211                                                 "on cpu%d\n",
2212                                                 cmd->opcode == O_LIMIT ?
2213                                                 "limit" : "keep state",
2214                                                 f->rulenum, f->cpuid);
2215                                         goto next_rule;
2216                                 }
2217                                 if (install_state(f,
2218                                     (ipfw_insn_limit *)cmd, args, &deny)) {
2219                                         if (deny)
2220                                                 return IP_FW_DENY;
2221
2222                                         retval = IP_FW_DENY;
2223                                         goto done; /* error/limit violation */
2224                                 }
2225                                 if (deny)
2226                                         return IP_FW_DENY;
2227                                 match = 1;
2228                                 break;
2229
2230                         case O_PROBE_STATE:
2231                         case O_CHECK_STATE:
2232                                 /*
2233                                  * dynamic rules are checked at the first
2234                                  * keep-state or check-state occurrence,
2235                                  * with the result being stored in dyn_dir.
2236                                  * The compiler introduces a PROBE_STATE
2237                                  * instruction for us when we have a
2238                                  * KEEP_STATE (because PROBE_STATE needs
2239                                  * to be run first).
2240                                  */
2241                                 if (dyn_dir == MATCH_UNKNOWN) {
2242                                         dyn_f = lookup_rule(&args->f_id,
2243                                                 &dyn_dir,
2244                                                 proto == IPPROTO_TCP ?
2245                                                 L3HDR(struct tcphdr, ip) : NULL,
2246                                                 ip_len, &deny);
2247                                         if (deny)
2248                                                 return IP_FW_DENY;
2249                                         if (dyn_f != NULL) {
2250                                                 /*
2251                                                  * Found a rule from a dynamic
2252                                                  * entry; jump to the 'action'
2253                                                  * part of the rule.
2254                                                  */
2255                                                 f = dyn_f;
2256                                                 cmd = ACTION_PTR(f);
2257                                                 l = f->cmd_len - f->act_ofs;
2258                                                 goto check_body;
2259                                         }
2260                                 }
2261                                 /*
2262                                  * Dynamic entry not found. If CHECK_STATE,
2263                                  * skip to next rule, if PROBE_STATE just
2264                                  * ignore and continue with next opcode.
2265                                  */
2266                                 if (cmd->opcode == O_CHECK_STATE)
2267                                         goto next_rule;
2268                                 else if (!(f->rule_flags & IPFW_RULE_F_STATE))
2269                                         goto next_rule; /* not ready yet */
2270                                 match = 1;
2271                                 break;
2272
2273                         case O_ACCEPT:
2274                                 retval = IP_FW_PASS;    /* accept */
2275                                 goto done;
2276
2277                         case O_PIPE:
2278                         case O_QUEUE:
2279                                 args->rule = f; /* report matching rule */
2280                                 args->cookie = cmd->arg1;
2281                                 retval = IP_FW_DUMMYNET;
2282                                 goto done;
2283
2284                         case O_DIVERT:
2285                         case O_TEE:
2286                                 if (args->eh) /* not on layer 2 */
2287                                         break;
2288
2289                                 mtag = m_tag_get(PACKET_TAG_IPFW_DIVERT,
2290                                                  sizeof(*divinfo), MB_DONTWAIT);
2291                                 if (mtag == NULL) {
2292                                         retval = IP_FW_DENY;
2293                                         goto done;
2294                                 }
2295                                 divinfo = m_tag_data(mtag);
2296
2297                                 divinfo->skipto = f->rulenum;
2298                                 divinfo->port = cmd->arg1;
2299                                 divinfo->tee = (cmd->opcode == O_TEE);
2300                                 m_tag_prepend(m, mtag);
2301
2302                                 args->cookie = cmd->arg1;
2303                                 retval = (cmd->opcode == O_DIVERT) ?
2304                                          IP_FW_DIVERT : IP_FW_TEE;
2305                                 goto done;
2306
2307                         case O_COUNT:
2308                         case O_SKIPTO:
2309                                 f->pcnt++;      /* update stats */
2310                                 f->bcnt += ip_len;
2311                                 f->timestamp = time_second;
2312                                 if (cmd->opcode == O_COUNT)
2313                                         goto next_rule;
2314                                 /* handle skipto */
2315                                 if (f->next_rule == NULL)
2316                                         lookup_next_rule(f);
2317                                 f = f->next_rule;
2318                                 goto again;
2319
2320                         case O_REJECT:
2321                                 /*
2322                                  * Drop the packet and send a reject notice
2323                                  * if the packet is not ICMP (or is an ICMP
2324                                  * query), and it is not multicast/broadcast.
2325                                  */
2326                                 if (hlen > 0 &&
2327                                     (proto != IPPROTO_ICMP ||
2328                                      is_icmp_query(ip)) &&
2329                                     !(m->m_flags & (M_BCAST|M_MCAST)) &&
2330                                     !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2331                                         /*
2332                                          * Update statistics before the possible
2333                                          * blocking 'send_reject'
2334                                          */
2335                                         f->pcnt++;
2336                                         f->bcnt += ip_len;
2337                                         f->timestamp = time_second;
2338
2339                                         send_reject(args, cmd->arg1,
2340                                             offset,ip_len);
2341                                         m = args->m;
2342
2343                                         /*
2344                                          * Return directly here, rule stats
2345                                          * have been updated above.
2346                                          */
2347                                         return IP_FW_DENY;
2348                                 }
2349                                 /* FALLTHROUGH */
2350                         case O_DENY:
2351                                 retval = IP_FW_DENY;
2352                                 goto done;
2353
2354                         case O_FORWARD_IP:
2355                                 if (args->eh)   /* not valid on layer2 pkts */
2356                                         break;
2357                                 if (!dyn_f || dyn_dir == MATCH_FORWARD) {
2358                                         struct sockaddr_in *sin;
2359
2360                                         mtag = m_tag_get(PACKET_TAG_IPFORWARD,
2361                                                sizeof(*sin), MB_DONTWAIT);
2362                                         if (mtag == NULL) {
2363                                                 retval = IP_FW_DENY;
2364                                                 goto done;
2365                                         }
2366                                         sin = m_tag_data(mtag);
2367
2368                                         /* Structure copy */
2369                                         *sin = ((ipfw_insn_sa *)cmd)->sa;
2370
2371                                         m_tag_prepend(m, mtag);
2372                                         m->m_pkthdr.fw_flags |=
2373                                                 IPFORWARD_MBUF_TAGGED;
2374                                 }
2375                                 retval = IP_FW_PASS;
2376                                 goto done;
2377
2378                         default:
2379                                 panic("-- unknown opcode %d\n", cmd->opcode);
2380                         } /* end of switch() on opcodes */
2381
2382                         if (cmd->len & F_NOT)
2383                                 match = !match;
2384
2385                         if (match) {
2386                                 if (cmd->len & F_OR)
2387                                         skip_or = 1;
2388                         } else {
2389                                 if (!(cmd->len & F_OR)) /* not an OR block, */
2390                                         break;          /* try next rule    */
2391                         }
2392
2393                 }       /* end of inner for, scan opcodes */
2394
2395 next_rule:;             /* try next rule                */
2396
2397         }               /* end of outer for, scan rules */
2398         kprintf("+++ ipfw: ouch!, skip past end of rules, denying packet\n");
2399         return IP_FW_DENY;
2400
2401 done:
2402         /* Update statistics */
2403         f->pcnt++;
2404         f->bcnt += ip_len;
2405         f->timestamp = time_second;
2406         return retval;
2407
2408 pullup_failed:
2409         if (fw_verbose)
2410                 kprintf("pullup failed\n");
2411         return IP_FW_DENY;
2412 }
2413
2414 static void
2415 ipfw_dummynet_io(struct mbuf *m, int pipe_nr, int dir, struct ip_fw_args *fwa)
2416 {
2417         struct m_tag *mtag;
2418         struct dn_pkt *pkt;
2419         ipfw_insn *cmd;
2420         const struct ipfw_flow_id *id;
2421         struct dn_flow_id *fid;
2422
2423         M_ASSERTPKTHDR(m);
2424
2425         mtag = m_tag_get(PACKET_TAG_DUMMYNET, sizeof(*pkt), MB_DONTWAIT);
2426         if (mtag == NULL) {
2427                 m_freem(m);
2428                 return;
2429         }
2430         m_tag_prepend(m, mtag);
2431
2432         pkt = m_tag_data(mtag);
2433         bzero(pkt, sizeof(*pkt));
2434
2435         cmd = fwa->rule->cmd + fwa->rule->act_ofs;
2436         if (cmd->opcode == O_LOG)
2437                 cmd += F_LEN(cmd);
2438         KASSERT(cmd->opcode == O_PIPE || cmd->opcode == O_QUEUE,
2439                 ("Rule is not PIPE or QUEUE, opcode %d\n", cmd->opcode));
2440
2441         pkt->dn_m = m;
2442         pkt->dn_flags = (dir & DN_FLAGS_DIR_MASK);
2443         pkt->ifp = fwa->oif;
2444         pkt->cpuid = mycpu->gd_cpuid;
2445         pkt->pipe_nr = pipe_nr;
2446
2447         id = &fwa->f_id;
2448         fid = &pkt->id;
2449         fid->fid_dst_ip = id->dst_ip;
2450         fid->fid_src_ip = id->src_ip;
2451         fid->fid_dst_port = id->dst_port;
2452         fid->fid_src_port = id->src_port;
2453         fid->fid_proto = id->proto;
2454         fid->fid_flags = id->flags;
2455
2456         ipfw_ref_rule(fwa->rule);
2457         pkt->dn_priv = fwa->rule;
2458         pkt->dn_unref_priv = ipfw_unref_rule;
2459
2460         if (cmd->opcode == O_PIPE)
2461                 pkt->dn_flags |= DN_FLAGS_IS_PIPE;
2462
2463         if (dir == DN_TO_IP_OUT) {
2464                 /*
2465                  * We need to copy *ro because for ICMP pkts (and maybe
2466                  * others) the caller passed a pointer into the stack;
2467                  * dst might also be a pointer into *ro so it needs to
2468                  * be updated.
2469                  */
2470                 pkt->ro = *(fwa->ro);
2471                 if (fwa->ro->ro_rt)
2472                         fwa->ro->ro_rt->rt_refcnt++;
2473                 if (fwa->dst == (struct sockaddr_in *)&fwa->ro->ro_dst) {
2474                         /* 'dst' points into 'ro' */
2475                         fwa->dst = (struct sockaddr_in *)&(pkt->ro.ro_dst);
2476                 }
2477                 pkt->dn_dst = fwa->dst;
2478                 pkt->flags = fwa->flags;
2479         }
2480
2481         m->m_pkthdr.fw_flags |= DUMMYNET_MBUF_TAGGED;
2482         ip_dn_queue(m);
2483 }
2484
2485 /*
2486  * When a rule is added/deleted, clear the next_rule pointers in all rules.
2487  * These will be reconstructed on the fly as packets are matched.
2488  * Must be called at splimp().
2489  */
2490 static void
2491 ipfw_flush_rule_ptrs(struct ipfw_context *ctx)
2492 {
2493         struct ip_fw *rule;
2494
2495         for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next)
2496                 rule->next_rule = NULL;
2497 }
2498
2499 static __inline void
2500 ipfw_inc_static_count(struct ip_fw *rule)
2501 {
2502         KKASSERT(mycpuid == 0);
2503
2504         static_count++;
2505         static_ioc_len += IOC_RULESIZE(rule);
2506 }
2507
2508 static __inline void
2509 ipfw_dec_static_count(struct ip_fw *rule)
2510 {
2511         int l = IOC_RULESIZE(rule);
2512
2513         KKASSERT(mycpuid == 0);
2514
2515         KASSERT(static_count > 0, ("invalid static count %u\n", static_count));
2516         static_count--;
2517
2518         KASSERT(static_ioc_len >= l,
2519                 ("invalid static len %u\n", static_ioc_len));
2520         static_ioc_len -= l;
2521 }
2522
2523 static void
2524 ipfw_link_sibling(struct netmsg_ipfw *fwmsg, struct ip_fw *rule)
2525 {
2526         if (fwmsg->sibling != NULL) {
2527                 KKASSERT(mycpuid > 0 && fwmsg->sibling->cpuid == mycpuid - 1);
2528                 fwmsg->sibling->sibling = rule;
2529         }
2530         fwmsg->sibling = rule;
2531 }
2532
2533 static struct ip_fw *
2534 ipfw_create_rule(const struct ipfw_ioc_rule *ioc_rule, struct ip_fw_stub *stub)
2535 {
2536         struct ip_fw *rule;
2537
2538         rule = kmalloc(RULESIZE(ioc_rule), M_IPFW, M_WAITOK | M_ZERO);
2539
2540         rule->act_ofs = ioc_rule->act_ofs;
2541         rule->cmd_len = ioc_rule->cmd_len;
2542         rule->rulenum = ioc_rule->rulenum;
2543         rule->set = ioc_rule->set;
2544         rule->usr_flags = ioc_rule->usr_flags;
2545
2546         bcopy(ioc_rule->cmd, rule->cmd, rule->cmd_len * 4 /* XXX */);
2547
2548         rule->refcnt = 1;
2549         rule->cpuid = mycpuid;
2550
2551         rule->stub = stub;
2552         if (stub != NULL)
2553                 stub->rule[mycpuid] = rule;
2554
2555         return rule;
2556 }
2557
2558 static void
2559 ipfw_add_rule_dispatch(struct netmsg *nmsg)
2560 {
2561         struct netmsg_ipfw *fwmsg = (struct netmsg_ipfw *)nmsg;
2562         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2563         struct ip_fw *rule;
2564
2565         rule = ipfw_create_rule(fwmsg->ioc_rule, fwmsg->stub);
2566
2567         /*
2568          * Bump generation after ipfw_create_rule(),
2569          * since this function is blocking
2570          */
2571         ctx->ipfw_gen++;
2572
2573         /*
2574          * Insert rule into the pre-determined position
2575          */
2576         if (fwmsg->prev_rule != NULL) {
2577                 struct ip_fw *prev, *next;
2578
2579                 prev = fwmsg->prev_rule;
2580                 KKASSERT(prev->cpuid == mycpuid);
2581
2582                 next = fwmsg->next_rule;
2583                 KKASSERT(next->cpuid == mycpuid);
2584
2585                 rule->next = next;
2586                 prev->next = rule;
2587
2588                 /*
2589                  * Move to the position on the next CPU
2590                  * before the msg is forwarded.
2591                  */
2592                 fwmsg->prev_rule = prev->sibling;
2593                 fwmsg->next_rule = next->sibling;
2594         } else {
2595                 KKASSERT(fwmsg->next_rule == NULL);
2596                 rule->next = ctx->ipfw_layer3_chain;
2597                 ctx->ipfw_layer3_chain = rule;
2598         }
2599
2600         /* Link rule CPU sibling */
2601         ipfw_link_sibling(fwmsg, rule);
2602
2603         ipfw_flush_rule_ptrs(ctx);
2604
2605         if (mycpuid == 0) {
2606                 /* Statistics only need to be updated once */
2607                 ipfw_inc_static_count(rule);
2608
2609                 /* Return the rule on CPU0 */
2610                 nmsg->nm_lmsg.u.ms_resultp = rule;
2611         }
2612
2613         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2614 }
2615
2616 static void
2617 ipfw_enable_state_dispatch(struct netmsg *nmsg)
2618 {
2619         struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
2620         struct ip_fw *rule = lmsg->u.ms_resultp;
2621
2622         KKASSERT(rule->cpuid == mycpuid);
2623         KKASSERT(rule->stub != NULL && rule->stub->rule[mycpuid] == rule);
2624         KKASSERT(!(rule->rule_flags & IPFW_RULE_F_STATE));
2625         rule->rule_flags |= IPFW_RULE_F_STATE;
2626         lmsg->u.ms_resultp = rule->sibling;
2627
2628         ifnet_forwardmsg(lmsg, mycpuid + 1);
2629 }
2630
2631 /*
2632  * Add a new rule to the list.  Copy the rule into a malloc'ed area,
2633  * then possibly create a rule number and add the rule to the list.
2634  * Update the rule_number in the input struct so the caller knows
2635  * it as well.
2636  */
2637 static void
2638 ipfw_add_rule(struct ipfw_ioc_rule *ioc_rule, uint32_t rule_flags)
2639 {
2640         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2641         struct netmsg_ipfw fwmsg;
2642         struct netmsg *nmsg;
2643         struct ip_fw *f, *prev, *rule;
2644         struct ip_fw_stub *stub;
2645
2646         IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
2647
2648         crit_enter();
2649
2650         /*
2651          * If rulenum is 0, find highest numbered rule before the
2652          * default rule, and add rule number incremental step.
2653          */
2654         if (ioc_rule->rulenum == 0) {
2655                 int step = autoinc_step;
2656
2657                 KKASSERT(step >= IPFW_AUTOINC_STEP_MIN &&
2658                          step <= IPFW_AUTOINC_STEP_MAX);
2659
2660                 /*
2661                  * Locate the highest numbered rule before default
2662                  */
2663                 for (f = ctx->ipfw_layer3_chain; f; f = f->next) {
2664                         if (f->rulenum == IPFW_DEFAULT_RULE)
2665                                 break;
2666                         ioc_rule->rulenum = f->rulenum;
2667                 }
2668                 if (ioc_rule->rulenum < IPFW_DEFAULT_RULE - step)
2669                         ioc_rule->rulenum += step;
2670         }
2671         KASSERT(ioc_rule->rulenum != IPFW_DEFAULT_RULE &&
2672                 ioc_rule->rulenum != 0,
2673                 ("invalid rule num %d\n", ioc_rule->rulenum));
2674
2675         /*
2676          * Now find the right place for the new rule in the sorted list.
2677          */
2678         for (prev = NULL, f = ctx->ipfw_layer3_chain; f;
2679              prev = f, f = f->next) {
2680                 if (f->rulenum > ioc_rule->rulenum) {
2681                         /* Found the location */
2682                         break;
2683                 }
2684         }
2685         KASSERT(f != NULL, ("no default rule?!\n"));
2686
2687         if (rule_flags & IPFW_RULE_F_STATE) {
2688                 int size;
2689
2690                 /*
2691                  * If the new rule will create states, then allocate
2692                  * a rule stub, which will be referenced by states
2693                  * (dyn rules)
2694                  */
2695                 size = sizeof(*stub) + ((ncpus - 1) * sizeof(struct ip_fw *));
2696                 stub = kmalloc(size, M_IPFW, M_WAITOK | M_ZERO);
2697         } else {
2698                 stub = NULL;
2699         }
2700
2701         /*
2702          * Duplicate the rule onto each CPU.
2703          * The rule duplicated on CPU0 will be returned.
2704          */
2705         bzero(&fwmsg, sizeof(fwmsg));
2706         nmsg = &fwmsg.nmsg;
2707         netmsg_init(nmsg, &curthread->td_msgport, 0, ipfw_add_rule_dispatch);
2708         fwmsg.ioc_rule = ioc_rule;
2709         fwmsg.prev_rule = prev;
2710         fwmsg.next_rule = prev == NULL ? NULL : f;
2711         fwmsg.stub = stub;
2712
2713         ifnet_domsg(&nmsg->nm_lmsg, 0);
2714         KKASSERT(fwmsg.prev_rule == NULL && fwmsg.next_rule == NULL);
2715
2716         rule = nmsg->nm_lmsg.u.ms_resultp;
2717         KKASSERT(rule != NULL && rule->cpuid == mycpuid);
2718
2719         if (rule_flags & IPFW_RULE_F_STATE) {
2720                 /*
2721                  * Turn on state flag, _after_ everything on all
2722                  * CPUs have been setup.
2723                  */
2724                 bzero(nmsg, sizeof(*nmsg));
2725                 netmsg_init(nmsg, &curthread->td_msgport, 0,
2726                             ipfw_enable_state_dispatch);
2727                 nmsg->nm_lmsg.u.ms_resultp = rule;
2728
2729                 ifnet_domsg(&nmsg->nm_lmsg, 0);
2730                 KKASSERT(nmsg->nm_lmsg.u.ms_resultp == NULL);
2731         }
2732
2733         crit_exit();
2734
2735         DEB(kprintf("++ installed rule %d, static count now %d\n",
2736                 rule->rulenum, static_count);)
2737 }
2738
2739 /**
2740  * Free storage associated with a static rule (including derived
2741  * dynamic rules).
2742  * The caller is in charge of clearing rule pointers to avoid
2743  * dangling pointers.
2744  * @return a pointer to the next entry.
2745  * Arguments are not checked, so they better be correct.
2746  * Must be called at splimp().
2747  */
2748 static struct ip_fw *
2749 ipfw_delete_rule(struct ipfw_context *ctx,
2750                  struct ip_fw *prev, struct ip_fw *rule)
2751 {
2752         struct ip_fw *n;
2753         struct ip_fw_stub *stub;
2754
2755         ctx->ipfw_gen++;
2756
2757         /* STATE flag should have been cleared before we reach here */
2758         KKASSERT((rule->rule_flags & IPFW_RULE_F_STATE) == 0);
2759
2760         stub = rule->stub;
2761         n = rule->next;
2762         if (prev == NULL)
2763                 ctx->ipfw_layer3_chain = n;
2764         else
2765                 prev->next = n;
2766
2767         /* Mark the rule as invalid */
2768         rule->rule_flags |= IPFW_RULE_F_INVALID;
2769         rule->next_rule = NULL;
2770         rule->sibling = NULL;
2771         rule->stub = NULL;
2772 #ifdef foo
2773         /* Don't reset cpuid here; keep various assertion working */
2774         rule->cpuid = -1;
2775 #endif
2776
2777         /* Statistics only need to be updated once */
2778         if (mycpuid == 0)
2779                 ipfw_dec_static_count(rule);
2780
2781         /* Free 'stub' on the last CPU */
2782         if (stub != NULL && mycpuid == ncpus - 1)
2783                 kfree(stub, M_IPFW);
2784
2785         /* Try to free this rule */
2786         ipfw_free_rule(rule);
2787
2788         /* Return the next rule */
2789         return n;
2790 }
2791
2792 static void
2793 ipfw_flush_dispatch(struct netmsg *nmsg)
2794 {
2795         struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
2796         int kill_default = lmsg->u.ms_result;
2797         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2798         struct ip_fw *rule;
2799
2800         ipfw_flush_rule_ptrs(ctx); /* more efficient to do outside the loop */
2801
2802         while ((rule = ctx->ipfw_layer3_chain) != NULL &&
2803                (kill_default || rule->rulenum != IPFW_DEFAULT_RULE))
2804                 ipfw_delete_rule(ctx, NULL, rule);
2805
2806         ifnet_forwardmsg(lmsg, mycpuid + 1);
2807 }
2808
2809 static void
2810 ipfw_disable_rule_state_dispatch(struct netmsg *nmsg)
2811 {
2812         struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
2813         struct ip_fw *rule;
2814
2815         rule = dmsg->start_rule;
2816         if (rule != NULL) {
2817                 KKASSERT(rule->cpuid == mycpuid);
2818
2819                 /*
2820                  * Move to the position on the next CPU
2821                  * before the msg is forwarded.
2822                  */
2823                 dmsg->start_rule = rule->sibling;
2824         } else {
2825                 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2826
2827                 KKASSERT(dmsg->rulenum == 0);
2828                 rule = ctx->ipfw_layer3_chain;
2829         }
2830
2831         while (rule != NULL) {
2832                 if (dmsg->rulenum && rule->rulenum != dmsg->rulenum)
2833                         break;
2834                 rule->rule_flags &= ~IPFW_RULE_F_STATE;
2835                 rule = rule->next;
2836         }
2837
2838         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2839 }
2840
2841 /*
2842  * Deletes all rules from a chain (including the default rule
2843  * if the second argument is set).
2844  * Must be called at splimp().
2845  */
2846 static void
2847 ipfw_flush(int kill_default)
2848 {
2849         struct netmsg_del dmsg;
2850         struct netmsg nmsg;
2851         struct lwkt_msg *lmsg;
2852         struct ip_fw *rule;
2853         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2854
2855         IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
2856
2857         /*
2858          * If 'kill_default' then caller has done the necessary
2859          * msgport syncing; unnecessary to do it again.
2860          */
2861         if (!kill_default) {
2862                 /*
2863                  * Let ipfw_chk() know the rules are going to
2864                  * be flushed, so it could jump directly to
2865                  * the default rule.
2866                  */
2867                 ipfw_flushing = 1;
2868                 netmsg_service_sync();
2869         }
2870
2871         /*
2872          * Clear STATE flag on rules, so no more states (dyn rules)
2873          * will be created.
2874          */
2875         bzero(&dmsg, sizeof(dmsg));
2876         netmsg_init(&dmsg.nmsg, &curthread->td_msgport, 0,
2877                     ipfw_disable_rule_state_dispatch);
2878         ifnet_domsg(&dmsg.nmsg.nm_lmsg, 0);
2879
2880         /*
2881          * This actually nukes all states (dyn rules)
2882          */
2883         lockmgr(&dyn_lock, LK_EXCLUSIVE);
2884         for (rule = ctx->ipfw_layer3_chain; rule != NULL; rule = rule->next) {
2885                 /*
2886                  * Can't check IPFW_RULE_F_STATE here,
2887                  * since it has been cleared previously.
2888                  * Check 'stub' instead.
2889                  */
2890                 if (rule->stub != NULL) {
2891                         /* Force removal */
2892                         remove_dyn_rule_locked(rule, NULL);
2893                 }
2894         }
2895         lockmgr(&dyn_lock, LK_RELEASE);
2896
2897         /*
2898          * Press the 'flush' button
2899          */
2900         bzero(&nmsg, sizeof(nmsg));
2901         netmsg_init(&nmsg, &curthread->td_msgport, 0, ipfw_flush_dispatch);
2902         lmsg = &nmsg.nm_lmsg;
2903         lmsg->u.ms_result = kill_default;
2904         ifnet_domsg(lmsg, 0);
2905
2906         KASSERT(dyn_count == 0, ("%u dyn rule remains\n", dyn_count));
2907
2908         if (kill_default) {
2909                 if (ipfw_dyn_v != NULL) {
2910                         /*
2911                          * Free dynamic rules(state) hash table
2912                          */
2913                         kfree(ipfw_dyn_v, M_IPFW);
2914                         ipfw_dyn_v = NULL;
2915                 }
2916
2917                 KASSERT(static_count == 0,
2918                         ("%u static rules remains\n", static_count));
2919                 KASSERT(static_ioc_len == 0,
2920                         ("%u bytes of static rules remains\n", static_ioc_len));
2921         } else {
2922                 KASSERT(static_count == 1,
2923                         ("%u static rules remains\n", static_count));
2924                 KASSERT(static_ioc_len == IOC_RULESIZE(ctx->ipfw_default_rule),
2925                         ("%u bytes of static rules remains, should be %u\n",
2926                          static_ioc_len, IOC_RULESIZE(ctx->ipfw_default_rule)));
2927         }
2928
2929         /* Flush is done */
2930         ipfw_flushing = 0;
2931 }
2932
2933 static void
2934 ipfw_alt_delete_rule_dispatch(struct netmsg *nmsg)
2935 {
2936         struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
2937         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2938         struct ip_fw *rule, *prev;
2939
2940         rule = dmsg->start_rule;
2941         KKASSERT(rule->cpuid == mycpuid);
2942         dmsg->start_rule = rule->sibling;
2943
2944         prev = dmsg->prev_rule;
2945         if (prev != NULL) {
2946                 KKASSERT(prev->cpuid == mycpuid);
2947
2948                 /*
2949                  * Move to the position on the next CPU
2950                  * before the msg is forwarded.
2951                  */
2952                 dmsg->prev_rule = prev->sibling;
2953         }
2954
2955         /*
2956          * flush pointers outside the loop, then delete all matching
2957          * rules.  'prev' remains the same throughout the cycle.
2958          */
2959         ipfw_flush_rule_ptrs(ctx);
2960         while (rule && rule->rulenum == dmsg->rulenum)
2961                 rule = ipfw_delete_rule(ctx, prev, rule);
2962
2963         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2964 }
2965
2966 static int
2967 ipfw_alt_delete_rule(uint16_t rulenum)
2968 {
2969         struct ip_fw *prev, *rule, *f;
2970         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2971         struct netmsg_del dmsg;
2972         struct netmsg *nmsg;
2973         int state;
2974
2975         /*
2976          * Locate first rule to delete
2977          */
2978         for (prev = NULL, rule = ctx->ipfw_layer3_chain;
2979              rule && rule->rulenum < rulenum;
2980              prev = rule, rule = rule->next)
2981                 ; /* EMPTY */
2982         if (rule->rulenum != rulenum)
2983                 return EINVAL;
2984
2985         /*
2986          * Check whether any rules with the given number will
2987          * create states.
2988          */
2989         state = 0;
2990         for (f = rule; f && f->rulenum == rulenum; f = f->next) {
2991                 if (f->rule_flags & IPFW_RULE_F_STATE) {
2992                         state = 1;
2993                         break;
2994                 }
2995         }
2996
2997         if (state) {
2998                 /*
2999                  * Clear the STATE flag, so no more states will be
3000                  * created based the rules numbered 'rulenum'.
3001                  */
3002                 bzero(&dmsg, sizeof(dmsg));
3003                 nmsg = &dmsg.nmsg;
3004                 netmsg_init(nmsg, &curthread->td_msgport, 0,
3005                             ipfw_disable_rule_state_dispatch);
3006                 dmsg.start_rule = rule;
3007                 dmsg.rulenum = rulenum;
3008
3009                 ifnet_domsg(&nmsg->nm_lmsg, 0);
3010                 KKASSERT(dmsg.start_rule == NULL);
3011
3012                 /*
3013                  * Nuke all related states
3014                  */
3015                 lockmgr(&dyn_lock, LK_EXCLUSIVE);
3016                 for (f = rule; f && f->rulenum == rulenum; f = f->next) {
3017                         /*
3018                          * Can't check IPFW_RULE_F_STATE here,
3019                          * since it has been cleared previously.
3020                          * Check 'stub' instead.
3021                          */
3022                         if (f->stub != NULL) {
3023                                 /* Force removal */
3024                                 remove_dyn_rule_locked(f, NULL);
3025                         }
3026                 }
3027                 lockmgr(&dyn_lock, LK_RELEASE);
3028         }
3029
3030         /*
3031          * Get rid of the rule duplications on all CPUs
3032          */
3033         bzero(&dmsg, sizeof(dmsg));
3034         nmsg = &dmsg.nmsg;
3035         netmsg_init(nmsg, &curthread->td_msgport, 0,
3036                     ipfw_alt_delete_rule_dispatch);
3037         dmsg.prev_rule = prev;
3038         dmsg.start_rule = rule;
3039         dmsg.rulenum = rulenum;
3040
3041         ifnet_domsg(&nmsg->nm_lmsg, 0);
3042         KKASSERT(dmsg.prev_rule == NULL && dmsg.start_rule == NULL);
3043         return 0;
3044 }
3045
3046 static void
3047 ipfw_alt_delete_ruleset_dispatch(struct netmsg *nmsg)
3048 {
3049         struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3050         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3051         struct ip_fw *prev, *rule;
3052 #ifdef INVARIANTS
3053         int del = 0;
3054 #endif
3055
3056         ipfw_flush_rule_ptrs(ctx);
3057
3058         prev = NULL;
3059         rule = ctx->ipfw_layer3_chain;
3060         while (rule != NULL) {
3061                 if (rule->set == dmsg->from_set) {
3062                         rule = ipfw_delete_rule(ctx, prev, rule);
3063 #ifdef INVARIANTS
3064                         del = 1;
3065 #endif
3066                 } else {
3067                         prev = rule;
3068                         rule = rule->next;
3069                 }
3070         }
3071         KASSERT(del, ("no match set?!\n"));
3072
3073         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3074 }
3075
3076 static void
3077 ipfw_disable_ruleset_state_dispatch(struct netmsg *nmsg)
3078 {
3079         struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3080         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3081         struct ip_fw *rule;
3082 #ifdef INVARIANTS
3083         int cleared = 0;
3084 #endif
3085
3086         for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3087                 if (rule->set == dmsg->from_set) {
3088 #ifdef INVARIANTS
3089                         cleared = 1;
3090 #endif
3091                         rule->rule_flags &= ~IPFW_RULE_F_STATE;
3092                 }
3093         }
3094         KASSERT(cleared, ("no match set?!\n"));
3095
3096         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3097 }
3098
3099 static int
3100 ipfw_alt_delete_ruleset(uint8_t set)
3101 {
3102         struct netmsg_del dmsg;
3103         struct netmsg *nmsg;
3104         int state, del;
3105         struct ip_fw *rule;
3106         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3107
3108         /*
3109          * Check whether the 'set' exists.  If it exists,
3110          * then check whether any rules within the set will
3111          * try to create states.
3112          */
3113         state = 0;
3114         del = 0;
3115         for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3116                 if (rule->set == set) {
3117                         del = 1;
3118                         if (rule->rule_flags & IPFW_RULE_F_STATE) {
3119                                 state = 1;
3120                                 break;
3121                         }
3122                 }
3123         }
3124         if (!del)
3125                 return 0; /* XXX EINVAL? */
3126
3127         if (state) {
3128                 /*
3129                  * Clear the STATE flag, so no more states will be
3130                  * created based the rules in this set.
3131                  */
3132                 bzero(&dmsg, sizeof(dmsg));
3133                 nmsg = &dmsg.nmsg;
3134                 netmsg_init(nmsg, &curthread->td_msgport, 0,
3135                             ipfw_disable_ruleset_state_dispatch);
3136                 dmsg.from_set = set;
3137
3138                 ifnet_domsg(&nmsg->nm_lmsg, 0);
3139
3140                 /*
3141                  * Nuke all related states
3142                  */
3143                 lockmgr(&dyn_lock, LK_EXCLUSIVE);
3144                 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3145                         if (rule->set != set)
3146                                 continue;
3147
3148                         /*
3149                          * Can't check IPFW_RULE_F_STATE here,
3150                          * since it has been cleared previously.
3151                          * Check 'stub' instead.
3152                          */
3153                         if (rule->stub != NULL) {
3154                                 /* Force removal */
3155                                 remove_dyn_rule_locked(rule, NULL);
3156                         }
3157                 }
3158                 lockmgr(&dyn_lock, LK_RELEASE);
3159         }
3160
3161         /*
3162          * Delete this set
3163          */
3164         bzero(&dmsg, sizeof(dmsg));
3165         nmsg = &dmsg.nmsg;
3166         netmsg_init(nmsg, &curthread->td_msgport, 0,
3167                     ipfw_alt_delete_ruleset_dispatch);
3168         dmsg.from_set = set;
3169
3170         ifnet_domsg(&nmsg->nm_lmsg, 0);
3171         return 0;
3172 }
3173
3174 static void
3175 ipfw_alt_move_rule_dispatch(struct netmsg *nmsg)
3176 {
3177         struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3178         struct ip_fw *rule;
3179
3180         rule = dmsg->start_rule;
3181         KKASSERT(rule->cpuid == mycpuid);
3182
3183         /*
3184          * Move to the position on the next CPU
3185          * before the msg is forwarded.
3186          */
3187         dmsg->start_rule = rule->sibling;
3188
3189         while (rule && rule->rulenum <= dmsg->rulenum) {
3190                 if (rule->rulenum == dmsg->rulenum)
3191                         rule->set = dmsg->to_set;
3192                 rule = rule->next;
3193         }
3194         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3195 }
3196
3197 static int
3198 ipfw_alt_move_rule(uint16_t rulenum, uint8_t set)
3199 {
3200         struct netmsg_del dmsg;
3201         struct netmsg *nmsg;
3202         struct ip_fw *rule;
3203         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3204
3205         /*
3206          * Locate first rule to move
3207          */
3208         for (rule = ctx->ipfw_layer3_chain; rule && rule->rulenum <= rulenum;
3209              rule = rule->next) {
3210                 if (rule->rulenum == rulenum && rule->set != set)
3211                         break;
3212         }
3213         if (rule == NULL || rule->rulenum > rulenum)
3214                 return 0; /* XXX error? */
3215
3216         bzero(&dmsg, sizeof(dmsg));
3217         nmsg = &dmsg.nmsg;
3218         netmsg_init(nmsg, &curthread->td_msgport, 0,
3219                     ipfw_alt_move_rule_dispatch);
3220         dmsg.start_rule = rule;
3221         dmsg.rulenum = rulenum;
3222         dmsg.to_set = set;
3223
3224         ifnet_domsg(&nmsg->nm_lmsg, 0);
3225         KKASSERT(dmsg.start_rule == NULL);
3226         return 0;
3227 }
3228
3229 static void
3230 ipfw_alt_move_ruleset_dispatch(struct netmsg *nmsg)
3231 {
3232         struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3233         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3234         struct ip_fw *rule;
3235
3236         for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3237                 if (rule->set == dmsg->from_set)
3238                         rule->set = dmsg->to_set;
3239         }
3240         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3241 }
3242
3243 static int
3244 ipfw_alt_move_ruleset(uint8_t from_set, uint8_t to_set)
3245 {
3246         struct netmsg_del dmsg;
3247         struct netmsg *nmsg;
3248
3249         bzero(&dmsg, sizeof(dmsg));
3250         nmsg = &dmsg.nmsg;
3251         netmsg_init(nmsg, &curthread->td_msgport, 0,
3252                     ipfw_alt_move_ruleset_dispatch);
3253         dmsg.from_set = from_set;
3254         dmsg.to_set = to_set;
3255
3256         ifnet_domsg(&nmsg->nm_lmsg, 0);
3257         return 0;
3258 }
3259
3260 static void
3261 ipfw_alt_swap_ruleset_dispatch(struct netmsg *nmsg)
3262 {
3263         struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3264         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3265         struct ip_fw *rule;
3266
3267         for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3268                 if (rule->set == dmsg->from_set)
3269                         rule->set = dmsg->to_set;
3270                 else if (rule->set == dmsg->to_set)
3271                         rule->set = dmsg->from_set;
3272         }
3273         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3274 }
3275
3276 static int
3277 ipfw_alt_swap_ruleset(uint8_t set1, uint8_t set2)
3278 {
3279         struct netmsg_del dmsg;
3280         struct netmsg *nmsg;
3281
3282         bzero(&dmsg, sizeof(dmsg));
3283         nmsg = &dmsg.nmsg;
3284         netmsg_init(nmsg, &curthread->td_msgport, 0,
3285                     ipfw_alt_swap_ruleset_dispatch);
3286         dmsg.from_set = set1;
3287         dmsg.to_set = set2;
3288
3289         ifnet_domsg(&nmsg->nm_lmsg, 0);
3290         return 0;
3291 }
3292
3293 /**
3294  * Remove all rules with given number, and also do set manipulation.
3295  *
3296  * The argument is an uint32_t. The low 16 bit are the rule or set number,
3297  * the next 8 bits are the new set, the top 8 bits are the command:
3298  *
3299  *      0       delete rules with given number
3300  *      1       delete rules with given set number
3301  *      2       move rules with given number to new set
3302  *      3       move rules with given set number to new set
3303  *      4       swap sets with given numbers
3304  */
3305 static int
3306 ipfw_ctl_alter(uint32_t arg)
3307 {
3308         uint16_t rulenum;
3309         uint8_t cmd, new_set;
3310         int error = 0;
3311
3312         rulenum = arg & 0xffff;
3313         cmd = (arg >> 24) & 0xff;
3314         new_set = (arg >> 16) & 0xff;
3315
3316         if (cmd > 4)
3317                 return EINVAL;
3318         if (new_set >= IPFW_DEFAULT_SET)
3319                 return EINVAL;
3320         if (cmd == 0 || cmd == 2) {
3321                 if (rulenum == IPFW_DEFAULT_RULE)
3322                         return EINVAL;
3323         } else {
3324                 if (rulenum >= IPFW_DEFAULT_SET)
3325                         return EINVAL;
3326         }
3327
3328         switch (cmd) {
3329         case 0: /* delete rules with given number */
3330                 error = ipfw_alt_delete_rule(rulenum);
3331                 break;
3332
3333         case 1: /* delete all rules with given set number */
3334                 error = ipfw_alt_delete_ruleset(rulenum);
3335                 break;
3336
3337         case 2: /* move rules with given number to new set */
3338                 error = ipfw_alt_move_rule(rulenum, new_set);
3339                 break;
3340
3341         case 3: /* move rules with given set number to new set */
3342                 error = ipfw_alt_move_ruleset(rulenum, new_set);
3343                 break;
3344
3345         case 4: /* swap two sets */
3346                 error = ipfw_alt_swap_ruleset(rulenum, new_set);
3347                 break;
3348         }
3349         return error;
3350 }
3351
3352 /*
3353  * Clear counters for a specific rule.
3354  */
3355 static void
3356 clear_counters(struct ip_fw *rule, int log_only)
3357 {
3358         ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
3359
3360         if (log_only == 0) {
3361                 rule->bcnt = rule->pcnt = 0;
3362                 rule->timestamp = 0;
3363         }
3364         if (l->o.opcode == O_LOG)
3365                 l->log_left = l->max_log;
3366 }
3367
3368 static void
3369 ipfw_zero_entry_dispatch(struct netmsg *nmsg)
3370 {
3371         struct netmsg_zent *zmsg = (struct netmsg_zent *)nmsg;
3372         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3373         struct ip_fw *rule;
3374
3375         if (zmsg->rulenum == 0) {
3376                 KKASSERT(zmsg->start_rule == NULL);
3377
3378                 ctx->ipfw_norule_counter = 0;
3379                 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next)
3380                         clear_counters(rule, zmsg->log_only);
3381         } else {
3382                 struct ip_fw *start = zmsg->start_rule;
3383
3384                 KKASSERT(start->cpuid == mycpuid);
3385                 KKASSERT(start->rulenum == zmsg->rulenum);
3386
3387                 /*
3388                  * We can have multiple rules with the same number, so we
3389                  * need to clear them all.
3390                  */
3391                 for (rule = start; rule && rule->rulenum == zmsg->rulenum;
3392                      rule = rule->next)
3393                         clear_counters(rule, zmsg->log_only);
3394
3395                 /*
3396                  * Move to the position on the next CPU
3397                  * before the msg is forwarded.
3398                  */
3399                 zmsg->start_rule = start->sibling;
3400         }
3401         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3402 }
3403
3404 /**
3405  * Reset some or all counters on firewall rules.
3406  * @arg frwl is null to clear all entries, or contains a specific
3407  * rule number.
3408  * @arg log_only is 1 if we only want to reset logs, zero otherwise.
3409  */
3410 static int
3411 ipfw_ctl_zero_entry(int rulenum, int log_only)
3412 {
3413         struct netmsg_zent zmsg;
3414         struct netmsg *nmsg;
3415         const char *msg;
3416         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3417
3418         bzero(&zmsg, sizeof(zmsg));
3419         nmsg = &zmsg.nmsg;
3420         netmsg_init(nmsg, &curthread->td_msgport, 0, ipfw_zero_entry_dispatch);
3421         zmsg.log_only = log_only;
3422
3423         if (rulenum == 0) {
3424                 msg = log_only ? "ipfw: All logging counts reset.\n"
3425                                : "ipfw: Accounting cleared.\n";
3426         } else {
3427                 struct ip_fw *rule;
3428
3429                 /*
3430                  * Locate the first rule with 'rulenum'
3431                  */
3432                 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3433                         if (rule->rulenum == rulenum)
3434                                 break;
3435                 }
3436                 if (rule == NULL) /* we did not find any matching rules */
3437                         return (EINVAL);
3438                 zmsg.start_rule = rule;
3439                 zmsg.rulenum = rulenum;
3440
3441                 msg = log_only ? "ipfw: Entry %d logging count reset.\n"
3442                                : "ipfw: Entry %d cleared.\n";
3443         }
3444         ifnet_domsg(&nmsg->nm_lmsg, 0);
3445         KKASSERT(zmsg.start_rule == NULL);
3446
3447         if (fw_verbose)
3448                 log(LOG_SECURITY | LOG_NOTICE, msg, rulenum);
3449         return (0);
3450 }
3451
3452 /*
3453  * Check validity of the structure before insert.
3454  * Fortunately rules are simple, so this mostly need to check rule sizes.
3455  */
3456 static int
3457 ipfw_check_ioc_rule(struct ipfw_ioc_rule *rule, int size, uint32_t *rule_flags)
3458 {
3459         int l, cmdlen = 0;
3460         int have_action = 0;
3461         ipfw_insn *cmd;
3462
3463         *rule_flags = 0;
3464
3465         /* Check for valid size */
3466         if (size < sizeof(*rule)) {
3467                 kprintf("ipfw: rule too short\n");
3468                 return EINVAL;
3469         }
3470         l = IOC_RULESIZE(rule);
3471         if (l != size) {
3472                 kprintf("ipfw: size mismatch (have %d want %d)\n", size, l);
3473                 return EINVAL;
3474         }
3475
3476         /* Check rule number */
3477         if (rule->rulenum == IPFW_DEFAULT_RULE) {
3478                 kprintf("ipfw: invalid rule number\n");
3479                 return EINVAL;
3480         }
3481
3482         /*
3483          * Now go for the individual checks. Very simple ones, basically only
3484          * instruction sizes.
3485          */
3486         for (l = rule->cmd_len, cmd = rule->cmd; l > 0;
3487              l -= cmdlen, cmd += cmdlen) {
3488                 cmdlen = F_LEN(cmd);
3489                 if (cmdlen > l) {
3490                         kprintf("ipfw: opcode %d size truncated\n",
3491                                 cmd->opcode);
3492                         return EINVAL;
3493                 }
3494
3495                 DEB(kprintf("ipfw: opcode %d\n", cmd->opcode);)
3496
3497                 if (cmd->opcode == O_KEEP_STATE || cmd->opcode == O_LIMIT) {
3498                         /* This rule will create states */
3499                         *rule_flags |= IPFW_RULE_F_STATE;
3500                 }
3501
3502                 switch (cmd->opcode) {
3503                 case O_NOP:
3504                 case O_PROBE_STATE:
3505                 case O_KEEP_STATE:
3506                 case O_PROTO:
3507                 case O_IP_SRC_ME:
3508                 case O_IP_DST_ME:
3509                 case O_LAYER2:
3510                 case O_IN:
3511                 case O_FRAG:
3512                 case O_IPOPT:
3513                 case O_IPLEN:
3514                 case O_IPID:
3515                 case O_IPTOS:
3516                 case O_IPPRECEDENCE:
3517                 case O_IPTTL:
3518                 case O_IPVER:
3519                 case O_TCPWIN:
3520                 case O_TCPFLAGS:
3521                 case O_TCPOPTS:
3522                 case O_ESTAB:
3523                         if (cmdlen != F_INSN_SIZE(ipfw_insn))
3524                                 goto bad_size;
3525                         break;
3526
3527                 case O_UID:
3528                 case O_GID:
3529                 case O_IP_SRC:
3530                 case O_IP_DST:
3531                 case O_TCPSEQ:
3532                 case O_TCPACK:
3533                 case O_PROB:
3534                 case O_ICMPTYPE:
3535                         if (cmdlen != F_INSN_SIZE(ipfw_insn_u32))
3536                                 goto bad_size;
3537                         break;
3538
3539                 case O_LIMIT:
3540                         if (cmdlen != F_INSN_SIZE(ipfw_insn_limit))
3541                                 goto bad_size;
3542                         break;
3543
3544                 case O_LOG:
3545                         if (cmdlen != F_INSN_SIZE(ipfw_insn_log))
3546                                 goto bad_size;
3547
3548                         ((ipfw_insn_log *)cmd)->log_left =
3549                             ((ipfw_insn_log *)cmd)->max_log;
3550
3551                         break;
3552
3553                 case O_IP_SRC_MASK:
3554                 case O_IP_DST_MASK:
3555                         if (cmdlen != F_INSN_SIZE(ipfw_insn_ip))
3556                                 goto bad_size;
3557                         if (((ipfw_insn_ip *)cmd)->mask.s_addr == 0) {
3558                                 kprintf("ipfw: opcode %d, useless rule\n",
3559                                         cmd->opcode);
3560                                 return EINVAL;
3561                         }
3562                         break;
3563
3564                 case O_IP_SRC_SET:
3565                 case O_IP_DST_SET:
3566                         if (cmd->arg1 == 0 || cmd->arg1 > 256) {
3567                                 kprintf("ipfw: invalid set size %d\n",
3568                                         cmd->arg1);
3569                                 return EINVAL;
3570                         }
3571                         if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
3572                             (cmd->arg1+31)/32 )
3573                                 goto bad_size;
3574                         break;
3575
3576                 case O_MACADDR2:
3577                         if (cmdlen != F_INSN_SIZE(ipfw_insn_mac))
3578                                 goto bad_size;
3579                         break;
3580
3581                 case O_MAC_TYPE:
3582                 case O_IP_SRCPORT:
3583                 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */
3584                         if (cmdlen < 2 || cmdlen > 31)
3585                                 goto bad_size;
3586                         break;
3587
3588                 case O_RECV:
3589                 case O_XMIT:
3590                 case O_VIA:
3591                         if (cmdlen != F_INSN_SIZE(ipfw_insn_if))
3592                                 goto bad_size;
3593                         break;
3594
3595                 case O_PIPE:
3596                 case O_QUEUE:
3597                         if (cmdlen != F_INSN_SIZE(ipfw_insn_pipe))
3598                                 goto bad_size;
3599                         goto check_action;
3600
3601                 case O_FORWARD_IP:
3602                         if (cmdlen != F_INSN_SIZE(ipfw_insn_sa)) {
3603                                 goto bad_size;
3604                         } else {
3605                                 in_addr_t fwd_addr;
3606
3607                                 fwd_addr = ((ipfw_insn_sa *)cmd)->
3608                                            sa.sin_addr.s_addr;
3609                                 if (IN_MULTICAST(ntohl(fwd_addr))) {
3610                                         kprintf("ipfw: try forwarding to "
3611                                                 "multicast address\n");
3612                                         return EINVAL;
3613                                 }
3614                         }
3615                         goto check_action;
3616
3617                 case O_FORWARD_MAC: /* XXX not implemented yet */
3618                 case O_CHECK_STATE:
3619                 case O_COUNT:
3620                 case O_ACCEPT:
3621                 case O_DENY:
3622                 case O_REJECT:
3623                 case O_SKIPTO:
3624                 case O_DIVERT:
3625                 case O_TEE:
3626                         if (cmdlen != F_INSN_SIZE(ipfw_insn))
3627                                 goto bad_size;
3628 check_action:
3629                         if (have_action) {
3630                                 kprintf("ipfw: opcode %d, multiple actions"
3631                                         " not allowed\n",
3632                                         cmd->opcode);
3633                                 return EINVAL;
3634                         }
3635                         have_action = 1;
3636                         if (l != cmdlen) {
3637                                 kprintf("ipfw: opcode %d, action must be"
3638                                         " last opcode\n",
3639                                         cmd->opcode);
3640                                 return EINVAL;
3641                         }
3642                         break;
3643                 default:
3644                         kprintf("ipfw: opcode %d, unknown opcode\n",
3645                                 cmd->opcode);
3646                         return EINVAL;
3647                 }
3648         }
3649         if (have_action == 0) {
3650                 kprintf("ipfw: missing action\n");
3651                 return EINVAL;
3652         }
3653         return 0;
3654
3655 bad_size:
3656         kprintf("ipfw: opcode %d size %d wrong\n",
3657                 cmd->opcode, cmdlen);
3658         return EINVAL;
3659 }
3660
3661 static int
3662 ipfw_ctl_add_rule(struct sockopt *sopt)
3663 {
3664         struct ipfw_ioc_rule *ioc_rule;
3665         size_t size;
3666         uint32_t rule_flags;
3667         int error;
3668         
3669         size = sopt->sopt_valsize;
3670         if (size > (sizeof(uint32_t) * IPFW_RULE_SIZE_MAX) ||
3671             size < sizeof(*ioc_rule)) {
3672                 return EINVAL;
3673         }
3674         if (size != (sizeof(uint32_t) * IPFW_RULE_SIZE_MAX)) {
3675                 sopt->sopt_val = krealloc(sopt->sopt_val, sizeof(uint32_t) *
3676                                           IPFW_RULE_SIZE_MAX, M_TEMP, M_WAITOK);
3677         }
3678         ioc_rule = sopt->sopt_val;
3679
3680         error = ipfw_check_ioc_rule(ioc_rule, size, &rule_flags);
3681         if (error)
3682                 return error;
3683
3684         ipfw_add_rule(ioc_rule, rule_flags);
3685
3686         if (sopt->sopt_dir == SOPT_GET)
3687                 sopt->sopt_valsize = IOC_RULESIZE(ioc_rule);
3688         return 0;
3689 }
3690
3691 static void *
3692 ipfw_copy_rule(const struct ip_fw *rule, struct ipfw_ioc_rule *ioc_rule)
3693 {
3694         const struct ip_fw *sibling;
3695 #ifdef INVARIANTS
3696         int i;
3697 #endif
3698
3699         KKASSERT(rule->cpuid == 0);
3700
3701         ioc_rule->act_ofs = rule->act_ofs;
3702         ioc_rule->cmd_len = rule->cmd_len;
3703         ioc_rule->rulenum = rule->rulenum;
3704         ioc_rule->set = rule->set;
3705         ioc_rule->usr_flags = rule->usr_flags;
3706
3707         ioc_rule->set_disable = ipfw_ctx[mycpuid]->ipfw_set_disable;
3708         ioc_rule->static_count = static_count;
3709         ioc_rule->static_len = static_ioc_len;
3710
3711         /*
3712          * Visit (read-only) all of the rule's duplications to get
3713          * the necessary statistics
3714          */
3715 #ifdef INVARIANTS
3716         i = 0;
3717 #endif
3718         ioc_rule->pcnt = 0;
3719         ioc_rule->bcnt = 0;
3720         ioc_rule->timestamp = 0;
3721         for (sibling = rule; sibling != NULL; sibling = sibling->sibling) {
3722                 ioc_rule->pcnt += sibling->pcnt;
3723                 ioc_rule->bcnt += sibling->bcnt;
3724                 if (sibling->timestamp > ioc_rule->timestamp)
3725                         ioc_rule->timestamp = sibling->timestamp;
3726 #ifdef INVARIANTS
3727                 ++i;
3728 #endif
3729         }
3730         KASSERT(i == ncpus, ("static rule is not duplicated on every cpu\n"));
3731
3732         bcopy(rule->cmd, ioc_rule->cmd, ioc_rule->cmd_len * 4 /* XXX */);
3733
3734         return ((uint8_t *)ioc_rule + IOC_RULESIZE(ioc_rule));
3735 }
3736
3737 static void
3738 ipfw_copy_state(const ipfw_dyn_rule *dyn_rule,
3739                 struct ipfw_ioc_state *ioc_state)
3740 {
3741         const struct ipfw_flow_id *id;
3742         struct ipfw_ioc_flowid *ioc_id;
3743
3744         ioc_state->expire = TIME_LEQ(dyn_rule->expire, time_second) ?
3745                             0 : dyn_rule->expire - time_second;
3746         ioc_state->pcnt = dyn_rule->pcnt;
3747         ioc_state->bcnt = dyn_rule->bcnt;
3748
3749         ioc_state->dyn_type = dyn_rule->dyn_type;
3750         ioc_state->count = dyn_rule->count;
3751
3752         ioc_state->rulenum = dyn_rule->stub->rule[mycpuid]->rulenum;
3753
3754         id = &dyn_rule->id;
3755         ioc_id = &ioc_state->id;
3756
3757         ioc_id->type = ETHERTYPE_IP;
3758         ioc_id->u.ip.dst_ip = id->dst_ip;
3759         ioc_id->u.ip.src_ip = id->src_ip;
3760         ioc_id->u.ip.dst_port = id->dst_port;
3761         ioc_id->u.ip.src_port = id->src_port;
3762         ioc_id->u.ip.proto = id->proto;
3763 }
3764
3765 static int
3766 ipfw_ctl_get_rules(struct sockopt *sopt)
3767 {
3768         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3769         struct ip_fw *rule;
3770         void *bp;
3771         size_t size;
3772         uint32_t dcount = 0;
3773
3774         /*
3775          * pass up a copy of the current rules. Static rules
3776          * come first (the last of which has number IPFW_DEFAULT_RULE),
3777          * followed by a possibly empty list of dynamic rule.
3778          */
3779         crit_enter();
3780
3781         size = static_ioc_len;  /* size of static rules */
3782         if (ipfw_dyn_v) {       /* add size of dyn.rules */
3783                 dcount = dyn_count;
3784                 size += dcount * sizeof(struct ipfw_ioc_state);
3785         }
3786
3787         if (sopt->sopt_valsize < size) {
3788                 /* short length, no need to return incomplete rules */
3789                 /* XXX: if superuser, no need to zero buffer */
3790                 bzero(sopt->sopt_val, sopt->sopt_valsize); 
3791                 return 0;
3792         }
3793         bp = sopt->sopt_val;
3794
3795         for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next)
3796                 bp = ipfw_copy_rule(rule, bp);
3797
3798         if (ipfw_dyn_v && dcount != 0) {
3799                 struct ipfw_ioc_state *ioc_state = bp;
3800                 uint32_t dcount2 = 0;
3801 #ifdef INVARIANTS
3802                 size_t old_size = size;
3803 #endif
3804                 int i;
3805
3806                 lockmgr(&dyn_lock, LK_SHARED);
3807
3808                 /* Check 'ipfw_dyn_v' again with lock held */
3809                 if (ipfw_dyn_v == NULL)
3810                         goto skip;
3811
3812                 for (i = 0; i < curr_dyn_buckets; i++) {
3813                         ipfw_dyn_rule *p;
3814
3815                         /*
3816                          * The # of dynamic rules may have grown after the
3817                          * snapshot of 'dyn_count' was taken, so we will have
3818                          * to check 'dcount' (snapshot of dyn_count) here to
3819                          * make sure that we don't overflow the pre-allocated
3820                          * buffer.
3821                          */
3822                         for (p = ipfw_dyn_v[i]; p != NULL && dcount != 0;
3823                              p = p->next, ioc_state++, dcount--, dcount2++)
3824                                 ipfw_copy_state(p, ioc_state);
3825                 }
3826 skip:
3827                 lockmgr(&dyn_lock, LK_RELEASE);
3828
3829                 /*
3830                  * The # of dynamic rules may be shrinked after the
3831                  * snapshot of 'dyn_count' was taken.  To give user a
3832                  * correct dynamic rule count, we use the 'dcount2'
3833                  * calculated above (with shared lockmgr lock held).
3834                  */
3835                 size = static_ioc_len +
3836                        (dcount2 * sizeof(struct ipfw_ioc_state));
3837                 KKASSERT(size <= old_size);
3838         }
3839
3840         crit_exit();
3841
3842         sopt->sopt_valsize = size;
3843         return 0;
3844 }
3845
3846 static void
3847 ipfw_set_disable_dispatch(struct netmsg *nmsg)
3848 {
3849         struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
3850         struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3851
3852         ctx->ipfw_gen++;
3853         ctx->ipfw_set_disable = lmsg->u.ms_result32;
3854
3855         ifnet_forwardmsg(lmsg, mycpuid + 1);
3856 }
3857
3858 static void
3859 ipfw_ctl_set_disable(uint32_t disable, uint32_t enable)
3860 {
3861         struct netmsg nmsg;
3862         struct lwkt_msg *lmsg;
3863         uint32_t set_disable;
3864
3865         /* IPFW_DEFAULT_SET is always enabled */
3866         enable |= (1 << IPFW_DEFAULT_SET);
3867         set_disable = (ipfw_ctx[mycpuid]->ipfw_set_disable | disable) & ~enable;
3868
3869         bzero(&nmsg, sizeof(nmsg));
3870         netmsg_init(&nmsg, &curthread->td_msgport, 0, ipfw_set_disable_dispatch);
3871         lmsg = &nmsg.nm_lmsg;
3872         lmsg->u.ms_result32 = set_disable;
3873
3874         ifnet_domsg(lmsg, 0);
3875 }
3876
3877 /**
3878  * {set|get}sockopt parser.
3879  */
3880 static int
3881 ipfw_ctl(struct sockopt *sopt)
3882 {
3883         int error, rulenum;
3884         uint32_t *masks;
3885         size_t size;
3886
3887         error = 0;
3888
3889         switch (sopt->sopt_name) {
3890         case IP_FW_GET:
3891                 error = ipfw_ctl_get_rules(sopt);
3892                 break;
3893
3894         case IP_FW_FLUSH:
3895                 /*
3896                  * Normally we cannot release the lock on each iteration.
3897                  * We could do it here only because we start from the head all
3898                  * the times so there is no risk of missing some entries.
3899                  * On the other hand, the risk is that we end up with
3900                  * a very inconsistent ruleset, so better keep the lock
3901                  * around the whole cycle.
3902                  *
3903                  * XXX this code can be improved by resetting the head of
3904                  * the list to point to the default rule, and then freeing
3905                  * the old list without the need for a lock.
3906                  */
3907
3908                 crit_enter();
3909                 ipfw_flush(0 /* keep default rule */);
3910                 crit_exit();
3911                 break;
3912
3913         case IP_FW_ADD:
3914                 error = ipfw_ctl_add_rule(sopt);
3915                 break;
3916
3917         case IP_FW_DEL:
3918                 /*
3919                  * IP_FW_DEL is used for deleting single rules or sets,
3920                  * and (ab)used to atomically manipulate sets.
3921                  * Argument size is used to distinguish between the two:
3922                  *    sizeof(uint32_t)
3923                  *      delete single rule or set of rules,
3924                  *      or reassign rules (or sets) to a different set.
3925                  *    2 * sizeof(uint32_t)
3926                  *      atomic disable/enable sets.
3927                  *      first uint32_t contains sets to be disabled,
3928                  *      second uint32_t contains sets to be enabled.
3929                  */
3930                 masks = sopt->sopt_val;
3931                 size = sopt->sopt_valsize;
3932                 if (size == sizeof(*masks)) {
3933                         /*
3934                          * Delete or reassign static rule
3935                          */
3936                         error = ipfw_ctl_alter(masks[0]);
3937                 } else if (size == (2 * sizeof(*masks))) {
3938                         /*
3939                          * Set enable/disable
3940                          */
3941                         ipfw_ctl_set_disable(masks[0], masks[1]);
3942                 } else {
3943                         error = EINVAL;
3944                 }
3945                 break;
3946
3947         case IP_FW_ZERO:
3948         case IP_FW_RESETLOG: /* argument is an int, the rule number */
3949                 rulenum = 0;
3950
3951                 if (sopt->sopt_val != 0) {
3952                     error = soopt_to_kbuf(sopt, &rulenum,
3953                             sizeof(int), sizeof(int));
3954                     if (error)
3955                         break;
3956                 }
3957                 error = ipfw_ctl_zero_entry(rulenum,
3958                         sopt->sopt_name == IP_FW_RESETLOG);
3959                 break;
3960
3961         default:
3962                 kprintf("ipfw_ctl invalid option %d\n", sopt->sopt_name);
3963                 error = EINVAL;
3964         }
3965         return error;
3966 }
3967
3968 /*
3969  * This procedure is only used to handle keepalives. It is invoked
3970  * every dyn_keepalive_period
3971  */
3972 static void
3973 ipfw_tick(void *dummy __unused)
3974 {
3975         time_t keep_alive;
3976         uint32_t gen;
3977         int i;
3978
3979         if (ipfw_dyn_v == NULL || dyn_count == 0)
3980                 goto done;
3981
3982         keep_alive = time_second;
3983
3984         lockmgr(&dyn_lock, LK_EXCLUSIVE);
3985 again:
3986         if (ipfw_dyn_v == NULL || dyn_count == 0) {
3987                 lockmgr(&dyn_lock, LK_RELEASE);
3988                 goto done;
3989         }
3990         gen = dyn_buckets_gen;
3991
3992         for (i = 0; i < curr_dyn_buckets; i++) {
3993                 ipfw_dyn_rule *q, *prev;
3994
3995                 for (prev = NULL, q = ipfw_dyn_v[i]; q != NULL;) {
3996                         uint32_t ack_rev, ack_fwd;
3997                         struct ipfw_flow_id id;
3998
3999                         if (q->dyn_type == O_LIMIT_PARENT)
4000                                 goto next;
4001
4002                         if (TIME_LEQ(q->expire, time_second)) {
4003                                 /* State expired */
4004                                 UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q);
4005                                 continue;
4006                         }
4007
4008                         /*
4009                          * Keep alive processing
4010                          */
4011
4012                         if (!dyn_keepalive)
4013                                 goto next;
4014                         if (q->id.proto != IPPROTO_TCP)
4015                                 goto next;
4016                         if ((q->state & BOTH_SYN) != BOTH_SYN)
4017                                 goto next;
4018                         if (TIME_LEQ(time_second + dyn_keepalive_interval,
4019                             q->expire))
4020                                 goto next;      /* too early */
4021                         if (q->keep_alive == keep_alive)
4022                                 goto next;      /* alreay done */
4023
4024                         /*
4025                          * Save necessary information, so that they could
4026                          * survive after possible blocking in send_pkt()
4027                          */
4028                         id = q->id;
4029                         ack_rev = q->ack_rev;
4030                         ack_fwd = q->ack_fwd;
4031
4032                         /* Sending has been started */
4033                         q->keep_alive = keep_alive;
4034
4035                         /* Release lock to avoid possible dead lock */
4036                         lockmgr(&dyn_lock, LK_RELEASE);
4037                         send_pkt(&id, ack_rev - 1, ack_fwd, TH_SYN);
4038                         send_pkt(&id, ack_fwd - 1, ack_rev, 0);
4039                         lockmgr(&dyn_lock, LK_EXCLUSIVE);
4040
4041                         if (gen != dyn_buckets_gen) {
4042                                 /*
4043                                  * Dyn bucket array has been changed during
4044                                  * the above two sending; reiterate.
4045                                  */
4046                                 goto again;
4047                         }
4048 next:
4049                         prev = q;
4050                         q = q->next;
4051                 }
4052         }
4053         lockmgr(&dyn_lock, LK_RELEASE);
4054 done:
4055         callout_reset(&ipfw_timeout_h, dyn_keepalive_period * hz,
4056                       ipfw_tick, NULL);
4057 }
4058
4059 static int
4060 ipfw_sysctl_autoinc_step(SYSCTL_HANDLER_ARGS)
4061 {
4062         return sysctl_int_range(oidp, arg1, arg2, req,
4063                IPFW_AUTOINC_STEP_MIN, IPFW_AUTOINC_STEP_MAX);
4064 }
4065
4066 static int
4067 ipfw_sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS)
4068 {
4069         int error, value;
4070
4071         lockmgr(&dyn_lock, LK_EXCLUSIVE);
4072
4073         value = dyn_buckets;
4074         error = sysctl_handle_int(oidp, &value, 0, req);
4075         if (error || !req->newptr)
4076                 goto back;
4077
4078         /*
4079          * Make sure we have a power of 2 and
4080          * do not allow more than 64k entries.
4081          */
4082         error = EINVAL;
4083         if (value <= 1 || value > 65536)
4084                 goto back;
4085         if ((value & (value - 1)) != 0)
4086                 goto back;
4087
4088         error = 0;
4089         dyn_buckets = value;
4090 back:
4091         lockmgr(&dyn_lock, LK_RELEASE);
4092         return error;
4093 }
4094
4095 static int
4096 ipfw_sysctl_dyn_fin(SYSCTL_HANDLER_ARGS)
4097 {
4098         return sysctl_int_range(oidp, arg1, arg2, req,
4099                                 1, dyn_keepalive_period - 1);
4100 }
4101
4102 static int
4103 ipfw_sysctl_dyn_rst(SYSCTL_HANDLER_ARGS)
4104 {
4105         return sysctl_int_range(oidp, arg1, arg2, req,
4106                                 1, dyn_keepalive_period - 1);
4107 }
4108
4109 static void
4110 ipfw_ctx_init_dispatch(struct netmsg *nmsg)
4111 {
4112         struct netmsg_ipfw *fwmsg = (struct netmsg_ipfw *)nmsg;
4113         struct ipfw_context *ctx;
4114         struct ip_fw *def_rule;
4115
4116         ctx = kmalloc(sizeof(*ctx), M_IPFW, M_WAITOK | M_ZERO);
4117         ipfw_ctx[mycpuid] = ctx;
4118
4119         def_rule = kmalloc(sizeof(*def_rule), M_IPFW, M_WAITOK | M_ZERO);
4120
4121         def_rule->act_ofs = 0;
4122         def_rule->rulenum = IPFW_DEFAULT_RULE;
4123         def_rule->cmd_len = 1;
4124         def_rule->set = IPFW_DEFAULT_SET;
4125
4126         def_rule->cmd[0].len = 1;
4127 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
4128         def_rule->cmd[0].opcode = O_ACCEPT;
4129 #else
4130         def_rule->cmd[0].opcode = O_DENY;
4131 #endif
4132
4133         def_rule->refcnt = 1;
4134         def_rule->cpuid = mycpuid;
4135
4136         /* Install the default rule */
4137         ctx->ipfw_default_rule = def_rule;
4138         ctx->ipfw_layer3_chain = def_rule;
4139
4140         /* Link rule CPU sibling */
4141         ipfw_link_sibling(fwmsg, def_rule);
4142
4143         /* Statistics only need to be updated once */
4144         if (mycpuid == 0)
4145                 ipfw_inc_static_count(def_rule);
4146
4147         ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
4148 }
4149
4150 static void
4151 ipfw_init_dispatch(struct netmsg *nmsg)
4152 {
4153         struct netmsg_ipfw fwmsg;
4154         int error = 0;
4155
4156         crit_enter();
4157
4158         if (IPFW_LOADED) {
4159                 kprintf("IP firewall already loaded\n");
4160                 error = EEXIST;
4161                 goto reply;
4162         }
4163
4164         bzero(&fwmsg, sizeof(fwmsg));
4165         netmsg_init(&fwmsg.nmsg, &curthread->td_msgport, 0,
4166                     ipfw_ctx_init_dispatch);
4167         ifnet_domsg(&fwmsg.nmsg.nm_lmsg, 0);
4168
4169         ip_fw_chk_ptr = ipfw_chk;
4170         ip_fw_ctl_ptr = ipfw_ctl;
4171         ip_fw_dn_io_ptr = ipfw_dummynet_io;
4172
4173         kprintf("ipfw2 initialized, divert %s, "
4174                 "rule-based forwarding enabled, default to %s, logging ",
4175 #ifdef IPDIVERT
4176                 "enabled",
4177 #else
4178                 "disabled",
4179 #endif
4180                 ipfw_ctx[mycpuid]->ipfw_default_rule->cmd[0].opcode ==
4181                 O_ACCEPT ? "accept" : "deny");
4182
4183 #ifdef IPFIREWALL_VERBOSE
4184         fw_verbose = 1;
4185 #endif
4186 #ifdef IPFIREWALL_VERBOSE_LIMIT
4187         verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
4188 #endif
4189         if (fw_verbose == 0) {
4190                 kprintf("disabled\n");
4191         } else if (verbose_limit == 0) {
4192                 kprintf("unlimited\n");
4193         } else {
4194                 kprintf("limited to %d packets/entry by default\n",
4195                         verbose_limit);
4196         }
4197
4198         callout_init(&ipfw_timeout_h);
4199         lockinit(&dyn_lock, "ipfw_dyn", 0, 0);
4200
4201         ip_fw_loaded = 1;
4202         callout_reset(&ipfw_timeout_h, hz, ipfw_tick, NULL);
4203 reply:
4204         crit_exit();
4205         lwkt_replymsg(&nmsg->nm_lmsg, error);
4206 }
4207
4208 static int
4209 ipfw_init(void)
4210 {
4211         struct netmsg smsg;
4212
4213         netmsg_init(&smsg, &curthread->td_msgport, 0, ipfw_init_dispatch);
4214         return lwkt_domsg(IPFW_CFGPORT, &smsg.nm_lmsg, 0);
4215 }
4216
4217 #ifdef KLD_MODULE
4218
4219 static void
4220 ipfw_fini_dispatch(struct netmsg *nmsg)
4221 {
4222         int error = 0, cpu;
4223
4224         crit_enter();
4225
4226         if (ipfw_refcnt != 0) {
4227                 error = EBUSY;
4228                 goto reply;
4229         }
4230
4231         callout_stop(&ipfw_timeout_h);
4232
4233         ip_fw_loaded = 0;
4234         netmsg_service_sync();
4235
4236         ip_fw_chk_ptr = NULL;
4237         ip_fw_ctl_ptr = NULL;
4238         ip_fw_dn_io_ptr = NULL;
4239         ipfw_flush(1 /* kill default rule */);
4240
4241         /* Free pre-cpu context */
4242         for (cpu = 0; cpu < ncpus; ++cpu)
4243                 kfree(ipfw_ctx[cpu], M_IPFW);
4244
4245         kprintf("IP firewall unloaded\n");
4246 reply:
4247         crit_exit();
4248         lwkt_replymsg(&nmsg->nm_lmsg, error);
4249 }
4250
4251 static int
4252 ipfw_fini(void)
4253 {
4254         struct netmsg smsg;
4255
4256         netmsg_init(&smsg, &curthread->td_msgport, 0, ipfw_fini_dispatch);
4257         return lwkt_domsg(IPFW_CFGPORT, &smsg.nm_lmsg, 0);
4258 }
4259
4260 #endif  /* KLD_MODULE */
4261
4262 static int
4263 ipfw_modevent(module_t mod, int type, void *unused)
4264 {
4265         int err = 0;
4266
4267         switch (type) {
4268         case MOD_LOAD:
4269                 err = ipfw_init();
4270                 break;
4271
4272         case MOD_UNLOAD:
4273 #ifndef KLD_MODULE
4274                 kprintf("ipfw statically compiled, cannot unload\n");
4275                 err = EBUSY;
4276 #else
4277                 err = ipfw_fini();
4278 #endif
4279                 break;
4280         default:
4281                 break;
4282         }
4283         return err;
4284 }
4285
4286 static moduledata_t ipfwmod = {
4287         "ipfw",
4288         ipfw_modevent,
4289         0
4290 };
4291 DECLARE_MODULE(ipfw, ipfwmod, SI_SUB_PROTO_END, SI_ORDER_ANY);
4292 MODULE_VERSION(ipfw, 1);