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