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