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