2ae9fb72aa764190b40eecbb0bc531471f86d2fa
[dragonfly.git] / sys / net / ipfw / ip_fw2.c
1 /*
2  * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/sys/netinet/ip_fw2.c,v 1.6.2.12 2003/04/08 10:42:32 maxim Exp $
26  * $DragonFly: src/sys/net/ipfw/ip_fw2.c,v 1.67 2008/08/09 06:09:18 sephe Exp $
27  */
28
29 #define        DEB(x)
30 #define        DDB(x) x
31
32 /*
33  * Implement IP packet firewall (new version)
34  */
35
36 #ifndef KLD_MODULE
37 #include "opt_ipfw.h"
38 #include "opt_ipdn.h"
39 #include "opt_ipdivert.h"
40 #include "opt_inet.h"
41 #ifndef INET
42 #error IPFIREWALL requires INET.
43 #endif /* INET */
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/kernel.h>
51 #include <sys/proc.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 #include <sys/thread2.h>
57 #include <sys/ucred.h>
58 #include <sys/in_cksum.h>
59 #include <sys/lock.h>
60
61 #include <net/if.h>
62 #include <net/route.h>
63 #include <net/netmsg2.h>
64
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/ip_icmp.h>
72 #include "ip_fw.h"
73 #include <net/dummynet/ip_dummynet.h>
74 #include <netinet/tcp.h>
75 #include <netinet/tcp_timer.h>
76 #include <netinet/tcp_var.h>
77 #include <netinet/tcpip.h>
78 #include <netinet/udp.h>
79 #include <netinet/udp_var.h>
80
81 #include <netinet/if_ether.h> /* XXX for ETHERTYPE_IP */
82
83 #define IPFW_AUTOINC_STEP_MIN   1
84 #define IPFW_AUTOINC_STEP_MAX   1000
85 #define IPFW_AUTOINC_STEP_DEF   100
86
87 /*
88  * set_disable contains one bit per set value (0..31).
89  * If the bit is set, all rules with the corresponding set
90  * are disabled. Set 31 is reserved for the default rule
91  * and CANNOT be disabled.
92  */
93 static uint32_t set_disable;
94
95 static int fw_verbose;
96 static int verbose_limit;
97
98 #ifdef KLD_MODULE
99 static int ipfw_refcnt;
100 #endif
101
102 static struct callout ipfw_timeout_h;
103 #define IPFW_DEFAULT_RULE       65535
104
105 /*
106  * list of rules for layer 3
107  */
108 static struct ip_fw *layer3_chain;
109
110 MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");
111
112 static int fw_debug = 1;
113 static int autoinc_step = IPFW_AUTOINC_STEP_DEF;
114 static uint32_t static_count;   /* # of static rules */
115 static uint32_t static_ioc_len; /* bytes of static rules */
116 static uint32_t static_gen;     /* generation of static rules */
117
118 static int      ipfw_sysctl_autoinc_step(SYSCTL_HANDLER_ARGS);
119 static int      ipfw_sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS);
120 static int      ipfw_sysctl_dyn_fin(SYSCTL_HANDLER_ARGS);
121 static int      ipfw_sysctl_dyn_rst(SYSCTL_HANDLER_ARGS);
122
123 #ifdef SYSCTL_NODE
124 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
125 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, enable, CTLFLAG_RW,
126     &fw_enable, 0, "Enable ipfw");
127 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, autoinc_step, CTLTYPE_INT | CTLFLAG_RW,
128     &autoinc_step, 0, ipfw_sysctl_autoinc_step, "I",
129     "Rule number autincrement step");
130 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO,one_pass,CTLFLAG_RW,
131     &fw_one_pass, 0,
132     "Only do a single pass through ipfw when using dummynet(4)");
133 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, debug, CTLFLAG_RW,
134     &fw_debug, 0, "Enable printing of debug ip_fw statements");
135 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose, CTLFLAG_RW,
136     &fw_verbose, 0, "Log matches to ipfw rules");
137 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, CTLFLAG_RW,
138     &verbose_limit, 0, "Set upper limit of matches of ipfw rules logged");
139
140 /*
141  * Description of dynamic rules.
142  *
143  * Dynamic rules are stored in lists accessed through a hash table
144  * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
145  * be modified through the sysctl variable dyn_buckets which is
146  * updated when the table becomes empty.
147  *
148  * XXX currently there is only one list, ipfw_dyn.
149  *
150  * When a packet is received, its address fields are first masked
151  * with the mask defined for the rule, then hashed, then matched
152  * against the entries in the corresponding list.
153  * Dynamic rules can be used for different purposes:
154  *  + stateful rules;
155  *  + enforcing limits on the number of sessions;
156  *  + in-kernel NAT (not implemented yet)
157  *
158  * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
159  * measured in seconds and depending on the flags.
160  *
161  * The total number of dynamic rules is stored in dyn_count.
162  * The max number of dynamic rules is dyn_max. When we reach
163  * the maximum number of rules we do not create anymore. This is
164  * done to avoid consuming too much memory, but also too much
165  * time when searching on each packet (ideally, we should try instead
166  * to put a limit on the length of the list on each bucket...).
167  *
168  * Each dynamic rule holds a pointer to the parent ipfw rule so
169  * we know what action to perform. Dynamic rules are removed when
170  * the parent rule is deleted. XXX we should make them survive.
171  *
172  * There are some limitations with dynamic rules -- we do not
173  * obey the 'randomized match', and we do not do multiple
174  * passes through the firewall. XXX check the latter!!!
175  *
176  * NOTE about the SHARED LOCKMGR LOCK during dynamic rule looking up:
177  * Only TCP state transition will change dynamic rule's state and ack
178  * sequences, while all packets of one TCP connection only goes through
179  * one TCP thread, so it is safe to use shared lockmgr lock during dynamic
180  * rule looking up.  The keep alive callout uses exclusive lockmgr lock
181  * when it tries to find suitable dynamic rules to send keep alive, so
182  * it will not see half updated state and ack sequences.  Though the expire
183  * field updating looks racy for other protocols, the resolution (second)
184  * of expire field makes this kind of race harmless.
185  * XXX statistics' updating is _not_ MPsafe!!!
186  * XXX once UDP output path is fixed, we could use lockless dynamic rule
187  *     hash table
188  */
189 static ipfw_dyn_rule **ipfw_dyn_v = NULL;
190 static uint32_t dyn_buckets = 256; /* must be power of 2 */
191 static uint32_t curr_dyn_buckets = 256; /* must be power of 2 */
192 static uint32_t dyn_buckets_gen; /* generation of dyn buckets array */
193 static struct lock dyn_lock; /* dynamic rules' hash table lock */
194
195 /*
196  * Timeouts for various events in handing dynamic rules.
197  */
198 static uint32_t dyn_ack_lifetime = 300;
199 static uint32_t dyn_syn_lifetime = 20;
200 static uint32_t dyn_fin_lifetime = 1;
201 static uint32_t dyn_rst_lifetime = 1;
202 static uint32_t dyn_udp_lifetime = 10;
203 static uint32_t dyn_short_lifetime = 5;
204
205 /*
206  * Keepalives are sent if dyn_keepalive is set. They are sent every
207  * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
208  * seconds of lifetime of a rule.
209  * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
210  * than dyn_keepalive_period.
211  */
212
213 static uint32_t dyn_keepalive_interval = 20;
214 static uint32_t dyn_keepalive_period = 5;
215 static uint32_t dyn_keepalive = 1;      /* do send keepalives */
216
217 static uint32_t dyn_count;              /* # of dynamic rules */
218 static uint32_t dyn_max = 4096;         /* max # of dynamic rules */
219
220 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_buckets, CTLTYPE_INT | CTLFLAG_RW,
221     &dyn_buckets, 0, ipfw_sysctl_dyn_buckets, "I", "Number of dyn. buckets");
222 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets, CTLFLAG_RD,
223     &curr_dyn_buckets, 0, "Current Number of dyn. buckets");
224 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_count, CTLFLAG_RD,
225     &dyn_count, 0, "Number of dyn. rules");
226 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_max, CTLFLAG_RW,
227     &dyn_max, 0, "Max number of dyn. rules");
228 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count, CTLFLAG_RD,
229     &static_count, 0, "Number of static rules");
230 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime, CTLFLAG_RW,
231     &dyn_ack_lifetime, 0, "Lifetime of dyn. rules for acks");
232 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime, CTLFLAG_RW,
233     &dyn_syn_lifetime, 0, "Lifetime of dyn. rules for syn");
234 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime,
235     CTLTYPE_INT | CTLFLAG_RW, &dyn_fin_lifetime, 0, ipfw_sysctl_dyn_fin, "I",
236     "Lifetime of dyn. rules for fin");
237 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime,
238     CTLTYPE_INT | CTLFLAG_RW, &dyn_rst_lifetime, 0, ipfw_sysctl_dyn_rst, "I",
239     "Lifetime of dyn. rules for rst");
240 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime, CTLFLAG_RW,
241     &dyn_udp_lifetime, 0, "Lifetime of dyn. rules for UDP");
242 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime, CTLFLAG_RW,
243     &dyn_short_lifetime, 0, "Lifetime of dyn. rules for other situations");
244 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_keepalive, CTLFLAG_RW,
245     &dyn_keepalive, 0, "Enable keepalives for dyn. rules");
246
247 #endif /* SYSCTL_NODE */
248
249 static struct ip_fw *ip_fw_default_rule;
250
251 static ip_fw_chk_t      ipfw_chk;
252
253 static __inline int
254 ipfw_free_rule(struct ip_fw *rule)
255 {
256         KASSERT(rule->refcnt > 0, ("invalid refcnt %u\n", rule->refcnt));
257         atomic_subtract_int(&rule->refcnt, 1);
258         if (atomic_cmpset_int(&rule->refcnt, 0, 1)) {
259                 kfree(rule, M_IPFW);
260                 return 1;
261         }
262         return 0;
263 }
264
265 static void
266 ipfw_unref_rule(void *priv)
267 {
268         ipfw_free_rule(priv);
269 #ifdef KLD_MODULE
270         atomic_subtract_int(&ipfw_refcnt, 1);
271 #endif
272 }
273
274 static __inline void
275 ipfw_ref_rule(struct ip_fw *rule)
276 {
277 #ifdef KLD_MODULE
278         atomic_add_int(&ipfw_refcnt, 1);
279 #endif
280         atomic_add_int(&rule->refcnt, 1);
281 }
282
283 /*
284  * This macro maps an ip pointer into a layer3 header pointer of type T
285  */
286 #define L3HDR(T, ip) ((T *)((uint32_t *)(ip) + (ip)->ip_hl))
287
288 static __inline int
289 icmptype_match(struct ip *ip, ipfw_insn_u32 *cmd)
290 {
291         int type = L3HDR(struct icmp,ip)->icmp_type;
292
293         return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1 << type)));
294 }
295
296 #define TT      ((1 << ICMP_ECHO) | \
297                  (1 << ICMP_ROUTERSOLICIT) | \
298                  (1 << ICMP_TSTAMP) | \
299                  (1 << ICMP_IREQ) | \
300                  (1 << ICMP_MASKREQ))
301
302 static int
303 is_icmp_query(struct ip *ip)
304 {
305         int type = L3HDR(struct icmp, ip)->icmp_type;
306
307         return (type <= ICMP_MAXTYPE && (TT & (1 << type)));
308 }
309
310 #undef TT
311
312 /*
313  * The following checks use two arrays of 8 or 16 bits to store the
314  * bits that we want set or clear, respectively. They are in the
315  * low and high half of cmd->arg1 or cmd->d[0].
316  *
317  * We scan options and store the bits we find set. We succeed if
318  *
319  *      (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
320  *
321  * The code is sometimes optimized not to store additional variables.
322  */
323
324 static int
325 flags_match(ipfw_insn *cmd, uint8_t bits)
326 {
327         u_char want_clear;
328         bits = ~bits;
329
330         if (((cmd->arg1 & 0xff) & bits) != 0)
331                 return 0; /* some bits we want set were clear */
332
333         want_clear = (cmd->arg1 >> 8) & 0xff;
334         if ((want_clear & bits) != want_clear)
335                 return 0; /* some bits we want clear were set */
336         return 1;
337 }
338
339 static int
340 ipopts_match(struct ip *ip, ipfw_insn *cmd)
341 {
342         int optlen, bits = 0;
343         u_char *cp = (u_char *)(ip + 1);
344         int x = (ip->ip_hl << 2) - sizeof(struct ip);
345
346         for (; x > 0; x -= optlen, cp += optlen) {
347                 int opt = cp[IPOPT_OPTVAL];
348
349                 if (opt == IPOPT_EOL)
350                         break;
351
352                 if (opt == IPOPT_NOP) {
353                         optlen = 1;
354                 } else {
355                         optlen = cp[IPOPT_OLEN];
356                         if (optlen <= 0 || optlen > x)
357                                 return 0; /* invalid or truncated */
358                 }
359
360                 switch (opt) {
361                 case IPOPT_LSRR:
362                         bits |= IP_FW_IPOPT_LSRR;
363                         break;
364
365                 case IPOPT_SSRR:
366                         bits |= IP_FW_IPOPT_SSRR;
367                         break;
368
369                 case IPOPT_RR:
370                         bits |= IP_FW_IPOPT_RR;
371                         break;
372
373                 case IPOPT_TS:
374                         bits |= IP_FW_IPOPT_TS;
375                         break;
376
377                 default:
378                         break;
379                 }
380         }
381         return (flags_match(cmd, bits));
382 }
383
384 static int
385 tcpopts_match(struct ip *ip, ipfw_insn *cmd)
386 {
387         int optlen, bits = 0;
388         struct tcphdr *tcp = L3HDR(struct tcphdr,ip);
389         u_char *cp = (u_char *)(tcp + 1);
390         int x = (tcp->th_off << 2) - sizeof(struct tcphdr);
391
392         for (; x > 0; x -= optlen, cp += optlen) {
393                 int opt = cp[0];
394
395                 if (opt == TCPOPT_EOL)
396                         break;
397
398                 if (opt == TCPOPT_NOP) {
399                         optlen = 1;
400                 } else {
401                         optlen = cp[1];
402                         if (optlen <= 0)
403                                 break;
404                 }
405
406                 switch (opt) {
407                 case TCPOPT_MAXSEG:
408                         bits |= IP_FW_TCPOPT_MSS;
409                         break;
410
411                 case TCPOPT_WINDOW:
412                         bits |= IP_FW_TCPOPT_WINDOW;
413                         break;
414
415                 case TCPOPT_SACK_PERMITTED:
416                 case TCPOPT_SACK:
417                         bits |= IP_FW_TCPOPT_SACK;
418                         break;
419
420                 case TCPOPT_TIMESTAMP:
421                         bits |= IP_FW_TCPOPT_TS;
422                         break;
423
424                 case TCPOPT_CC:
425                 case TCPOPT_CCNEW:
426                 case TCPOPT_CCECHO:
427                         bits |= IP_FW_TCPOPT_CC;
428                         break;
429
430                 default:
431                         break;
432                 }
433         }
434         return (flags_match(cmd, bits));
435 }
436
437 static int
438 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd)
439 {
440         if (ifp == NULL)        /* no iface with this packet, match fails */
441                 return 0;
442
443         /* Check by name or by IP address */
444         if (cmd->name[0] != '\0') { /* match by name */
445                 /* Check name */
446                 if (cmd->p.glob) {
447                         if (kfnmatch(cmd->name, ifp->if_xname, 0) == 0)
448                                 return(1);
449                 } else {
450                         if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
451                                 return(1);
452                 }
453         } else {
454                 struct ifaddr_container *ifac;
455
456                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
457                         struct ifaddr *ia = ifac->ifa;
458
459                         if (ia->ifa_addr == NULL)
460                                 continue;
461                         if (ia->ifa_addr->sa_family != AF_INET)
462                                 continue;
463                         if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
464                             (ia->ifa_addr))->sin_addr.s_addr)
465                                 return(1);      /* match */
466                 }
467         }
468         return(0);      /* no match, fail ... */
469 }
470
471 static uint64_t norule_counter; /* counter for ipfw_log(NULL...) */
472
473 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
474
475 /*
476  * We enter here when we have a rule with O_LOG.
477  * XXX this function alone takes about 2Kbytes of code!
478  */
479 static void
480 ipfw_log(struct ip_fw *f, u_int hlen, struct ether_header *eh,
481          struct mbuf *m, struct ifnet *oif)
482 {
483         char *action;
484         int limit_reached = 0;
485         char action2[40], proto[48], fragment[28];
486
487         fragment[0] = '\0';
488         proto[0] = '\0';
489
490         if (f == NULL) {        /* bogus pkt */
491                 if (verbose_limit != 0 && norule_counter >= verbose_limit)
492                         return;
493                 norule_counter++;
494                 if (norule_counter == verbose_limit)
495                         limit_reached = verbose_limit;
496                 action = "Refuse";
497         } else {        /* O_LOG is the first action, find the real one */
498                 ipfw_insn *cmd = ACTION_PTR(f);
499                 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
500
501                 if (l->max_log != 0 && l->log_left == 0)
502                         return;
503                 l->log_left--;
504                 if (l->log_left == 0)
505                         limit_reached = l->max_log;
506                 cmd += F_LEN(cmd);      /* point to first action */
507                 if (cmd->opcode == O_PROB)
508                         cmd += F_LEN(cmd);
509
510                 action = action2;
511                 switch (cmd->opcode) {
512                 case O_DENY:
513                         action = "Deny";
514                         break;
515
516                 case O_REJECT:
517                         if (cmd->arg1==ICMP_REJECT_RST) {
518                                 action = "Reset";
519                         } else if (cmd->arg1==ICMP_UNREACH_HOST) {
520                                 action = "Reject";
521                         } else {
522                                 ksnprintf(SNPARGS(action2, 0), "Unreach %d",
523                                           cmd->arg1);
524                         }
525                         break;
526
527                 case O_ACCEPT:
528                         action = "Accept";
529                         break;
530
531                 case O_COUNT:
532                         action = "Count";
533                         break;
534
535                 case O_DIVERT:
536                         ksnprintf(SNPARGS(action2, 0), "Divert %d", cmd->arg1);
537                         break;
538
539                 case O_TEE:
540                         ksnprintf(SNPARGS(action2, 0), "Tee %d", cmd->arg1);
541                         break;
542
543                 case O_SKIPTO:
544                         ksnprintf(SNPARGS(action2, 0), "SkipTo %d", cmd->arg1);
545                         break;
546
547                 case O_PIPE:
548                         ksnprintf(SNPARGS(action2, 0), "Pipe %d", cmd->arg1);
549                         break;
550
551                 case O_QUEUE:
552                         ksnprintf(SNPARGS(action2, 0), "Queue %d", cmd->arg1);
553                         break;
554
555                 case O_FORWARD_IP:
556                         {
557                                 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
558                                 int len;
559
560                                 len = ksnprintf(SNPARGS(action2, 0),
561                                                 "Forward to %s",
562                                                 inet_ntoa(sa->sa.sin_addr));
563                                 if (sa->sa.sin_port) {
564                                         ksnprintf(SNPARGS(action2, len), ":%d",
565                                                   sa->sa.sin_port);
566                                 }
567                         }
568                         break;
569
570                 default:
571                         action = "UNKNOWN";
572                         break;
573                 }
574         }
575
576         if (hlen == 0) {        /* non-ip */
577                 ksnprintf(SNPARGS(proto, 0), "MAC");
578         } else {
579                 struct ip *ip = mtod(m, struct ip *);
580                 /* these three are all aliases to the same thing */
581                 struct icmp *const icmp = L3HDR(struct icmp, ip);
582                 struct tcphdr *const tcp = (struct tcphdr *)icmp;
583                 struct udphdr *const udp = (struct udphdr *)icmp;
584
585                 int ip_off, offset, ip_len;
586                 int len;
587
588                 if (eh != NULL) { /* layer 2 packets are as on the wire */
589                         ip_off = ntohs(ip->ip_off);
590                         ip_len = ntohs(ip->ip_len);
591                 } else {
592                         ip_off = ip->ip_off;
593                         ip_len = ip->ip_len;
594                 }
595                 offset = ip_off & IP_OFFMASK;
596                 switch (ip->ip_p) {
597                 case IPPROTO_TCP:
598                         len = ksnprintf(SNPARGS(proto, 0), "TCP %s",
599                                         inet_ntoa(ip->ip_src));
600                         if (offset == 0) {
601                                 ksnprintf(SNPARGS(proto, len), ":%d %s:%d",
602                                           ntohs(tcp->th_sport),
603                                           inet_ntoa(ip->ip_dst),
604                                           ntohs(tcp->th_dport));
605                         } else {
606                                 ksnprintf(SNPARGS(proto, len), " %s",
607                                           inet_ntoa(ip->ip_dst));
608                         }
609                         break;
610
611                 case IPPROTO_UDP:
612                         len = ksnprintf(SNPARGS(proto, 0), "UDP %s",
613                                         inet_ntoa(ip->ip_src));
614                         if (offset == 0) {
615                                 ksnprintf(SNPARGS(proto, len), ":%d %s:%d",
616                                           ntohs(udp->uh_sport),
617                                           inet_ntoa(ip->ip_dst),
618                                           ntohs(udp->uh_dport));
619                         } else {
620                                 ksnprintf(SNPARGS(proto, len), " %s",
621                                           inet_ntoa(ip->ip_dst));
622                         }
623                         break;
624
625                 case IPPROTO_ICMP:
626                         if (offset == 0) {
627                                 len = ksnprintf(SNPARGS(proto, 0),
628                                                 "ICMP:%u.%u ",
629                                                 icmp->icmp_type,
630                                                 icmp->icmp_code);
631                         } else {
632                                 len = ksnprintf(SNPARGS(proto, 0), "ICMP ");
633                         }
634                         len += ksnprintf(SNPARGS(proto, len), "%s",
635                                          inet_ntoa(ip->ip_src));
636                         ksnprintf(SNPARGS(proto, len), " %s",
637                                   inet_ntoa(ip->ip_dst));
638                         break;
639
640                 default:
641                         len = ksnprintf(SNPARGS(proto, 0), "P:%d %s", ip->ip_p,
642                                         inet_ntoa(ip->ip_src));
643                         ksnprintf(SNPARGS(proto, len), " %s",
644                                   inet_ntoa(ip->ip_dst));
645                         break;
646                 }
647
648                 if (ip_off & (IP_MF | IP_OFFMASK)) {
649                         ksnprintf(SNPARGS(fragment, 0), " (frag %d:%d@%d%s)",
650                                   ntohs(ip->ip_id), ip_len - (ip->ip_hl << 2),
651                                   offset << 3, (ip_off & IP_MF) ? "+" : "");
652                 }
653         }
654
655         if (oif || m->m_pkthdr.rcvif) {
656                 log(LOG_SECURITY | LOG_INFO,
657                     "ipfw: %d %s %s %s via %s%s\n",
658                     f ? f->rulenum : -1,
659                     action, proto, oif ? "out" : "in",
660                     oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
661                     fragment);
662         } else {
663                 log(LOG_SECURITY | LOG_INFO,
664                     "ipfw: %d %s %s [no if info]%s\n",
665                     f ? f->rulenum : -1,
666                     action, proto, fragment);
667         }
668
669         if (limit_reached) {
670                 log(LOG_SECURITY | LOG_NOTICE,
671                     "ipfw: limit %d reached on entry %d\n",
672                     limit_reached, f ? f->rulenum : -1);
673         }
674 }
675
676 #undef SNPARGS
677
678 /*
679  * IMPORTANT: the hash function for dynamic rules must be commutative
680  * in source and destination (ip,port), because rules are bidirectional
681  * and we want to find both in the same bucket.
682  */
683 static __inline int
684 hash_packet(struct ipfw_flow_id *id)
685 {
686         uint32_t i;
687
688         i = (id->dst_ip) ^ (id->src_ip) ^ (id->dst_port) ^ (id->src_port);
689         i &= (curr_dyn_buckets - 1);
690         return i;
691 }
692
693 /**
694  * unlink a dynamic rule from a chain. prev is a pointer to
695  * the previous one, q is a pointer to the rule to delete,
696  * head is a pointer to the head of the queue.
697  * Modifies q and potentially also head.
698  */
699 #define UNLINK_DYN_RULE(prev, head, q)                                  \
700 do {                                                                    \
701         ipfw_dyn_rule *old_q = q;                                       \
702                                                                         \
703         /* remove a refcount to the parent */                           \
704         if (q->dyn_type == O_LIMIT)                                     \
705                 q->parent->count--;                                     \
706         DEB(kprintf("-- unlink entry 0x%08x %d -> 0x%08x %d, %d left\n", \
707                 (q->id.src_ip), (q->id.src_port),                       \
708                 (q->id.dst_ip), (q->id.dst_port), dyn_count-1 ); )      \
709         if (prev != NULL)                                               \
710                 prev->next = q = q->next;                               \
711         else                                                            \
712                 head = q = q->next;                                     \
713         KASSERT(dyn_count > 0, ("invalid dyn count %u\n", dyn_count));  \
714         dyn_count--;                                                    \
715         kfree(old_q, M_IPFW);                                           \
716 } while (0)
717
718 #define TIME_LEQ(a, b)  ((int)((a) - (b)) <= 0)
719
720 /**
721  * Remove dynamic rules pointing to "rule", or all of them if rule == NULL.
722  *
723  * If keep_me == NULL, rules are deleted even if not expired,
724  * otherwise only expired rules are removed.
725  *
726  * The value of the second parameter is also used to point to identify
727  * a rule we absolutely do not want to remove (e.g. because we are
728  * holding a reference to it -- this is the case with O_LIMIT_PARENT
729  * rules). The pointer is only used for comparison, so any non-null
730  * value will do.
731  */
732 static void
733 remove_dyn_rule_locked(struct ip_fw *rule, ipfw_dyn_rule *keep_me)
734 {
735         static uint32_t last_remove = 0; /* XXX */
736
737 #define FORCE   (keep_me == NULL)
738
739         ipfw_dyn_rule *prev, *q;
740         int i, pass = 0, max_pass = 0, unlinked = 0;
741
742         if (ipfw_dyn_v == NULL || dyn_count == 0)
743                 return;
744         /* do not expire more than once per second, it is useless */
745         if (!FORCE && last_remove == time_second)
746                 return;
747         last_remove = time_second;
748
749         /*
750          * because O_LIMIT refer to parent rules, during the first pass only
751          * remove child and mark any pending LIMIT_PARENT, and remove
752          * them in a second pass.
753          */
754 next_pass:
755         for (i = 0; i < curr_dyn_buckets; i++) {
756                 for (prev = NULL, q = ipfw_dyn_v[i]; q;) {
757                         /*
758                          * Logic can become complex here, so we split tests.
759                          */
760                         if (q == keep_me)
761                                 goto next;
762                         if (rule != NULL && rule != q->rule)
763                                 goto next; /* not the one we are looking for */
764                         if (q->dyn_type == O_LIMIT_PARENT) {
765                                 /*
766                                  * handle parent in the second pass,
767                                  * record we need one.
768                                  */
769                                 max_pass = 1;
770                                 if (pass == 0)
771                                         goto next;
772                                 if (FORCE && q->count != 0) {
773                                         /* XXX should not happen! */
774                                         kprintf("OUCH! cannot remove rule, "
775                                                 "count %d\n", q->count);
776                                 }
777                         } else {
778                                 if (!FORCE && !TIME_LEQ(q->expire, time_second))
779                                         goto next;
780                         }
781                         unlinked = 1;
782                         UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q);
783                         continue;
784 next:
785                         prev = q;
786                         q = q->next;
787                 }
788         }
789         if (pass++ < max_pass)
790                 goto next_pass;
791
792         if (unlinked)
793                 ++dyn_buckets_gen;
794
795 #undef FORCE
796 }
797
798 static void
799 remove_dyn_rule(struct ip_fw *rule, ipfw_dyn_rule *keep_me)
800 {
801         lockmgr(&dyn_lock, LK_EXCLUSIVE);
802         remove_dyn_rule_locked(rule, keep_me);
803         lockmgr(&dyn_lock, LK_RELEASE);
804 }
805
806 /**
807  * lookup a dynamic rule.
808  */
809 static ipfw_dyn_rule *
810 lookup_dyn_rule(struct ipfw_flow_id *pkt, int *match_direction,
811                 struct tcphdr *tcp)
812 {
813         /*
814          * stateful ipfw extensions.
815          * Lookup into dynamic session queue
816          */
817 #define MATCH_REVERSE   0
818 #define MATCH_FORWARD   1
819 #define MATCH_NONE      2
820 #define MATCH_UNKNOWN   3
821         int i, dir = MATCH_NONE;
822         ipfw_dyn_rule *prev, *q=NULL;
823
824         if (ipfw_dyn_v == NULL)
825                 goto done;      /* not found */
826
827         i = hash_packet(pkt);
828         for (prev = NULL, q = ipfw_dyn_v[i]; q != NULL;) {
829                 if (q->dyn_type == O_LIMIT_PARENT)
830                         goto next;
831
832                 if (TIME_LEQ(q->expire, time_second)) {
833                         /*
834                          * Entry expired; skip.
835                          * Let ipfw_tick() take care of it
836                          */
837                         goto next;
838                 }
839
840                 if (pkt->proto == q->id.proto) {
841                         if (pkt->src_ip == q->id.src_ip &&
842                             pkt->dst_ip == q->id.dst_ip &&
843                             pkt->src_port == q->id.src_port &&
844                             pkt->dst_port == q->id.dst_port) {
845                                 dir = MATCH_FORWARD;
846                                 break;
847                         }
848                         if (pkt->src_ip == q->id.dst_ip &&
849                             pkt->dst_ip == q->id.src_ip &&
850                             pkt->src_port == q->id.dst_port &&
851                             pkt->dst_port == q->id.src_port) {
852                                 dir = MATCH_REVERSE;
853                                 break;
854                         }
855                 }
856 next:
857                 prev = q;
858                 q = q->next;
859         }
860         if (q == NULL)
861                 goto done; /* q = NULL, not found */
862
863         if (pkt->proto == IPPROTO_TCP) { /* update state according to flags */
864                 u_char flags = pkt->flags & (TH_FIN|TH_SYN|TH_RST);
865
866 #define BOTH_SYN        (TH_SYN | (TH_SYN << 8))
867 #define BOTH_FIN        (TH_FIN | (TH_FIN << 8))
868
869                 q->state |= (dir == MATCH_FORWARD ) ? flags : (flags << 8);
870                 switch (q->state) {
871                 case TH_SYN:                            /* opening */
872                         q->expire = time_second + dyn_syn_lifetime;
873                         break;
874
875                 case BOTH_SYN:                  /* move to established */
876                 case BOTH_SYN | TH_FIN :        /* one side tries to close */
877                 case BOTH_SYN | (TH_FIN << 8) :
878                         if (tcp) {
879                                 uint32_t ack = ntohl(tcp->th_ack);
880
881 #define _SEQ_GE(a, b)   ((int)(a) - (int)(b) >= 0)
882
883                                 if (dir == MATCH_FORWARD) {
884                                         if (q->ack_fwd == 0 ||
885                                             _SEQ_GE(ack, q->ack_fwd))
886                                                 q->ack_fwd = ack;
887                                         else /* ignore out-of-sequence */
888                                                 break;
889                                 } else {
890                                         if (q->ack_rev == 0 ||
891                                             _SEQ_GE(ack, q->ack_rev))
892                                                 q->ack_rev = ack;
893                                         else /* ignore out-of-sequence */
894                                                 break;
895                                 }
896 #undef _SEQ_GE
897                         }
898                         q->expire = time_second + dyn_ack_lifetime;
899                         break;
900
901                 case BOTH_SYN | BOTH_FIN:       /* both sides closed */
902                         KKASSERT(dyn_fin_lifetime < dyn_keepalive_period);
903                         q->expire = time_second + dyn_fin_lifetime;
904                         break;
905
906                 default:
907 #if 0
908                         /*
909                          * reset or some invalid combination, but can also
910                          * occur if we use keep-state the wrong way.
911                          */
912                         if ((q->state & ((TH_RST << 8) | TH_RST)) == 0)
913                                 kprintf("invalid state: 0x%x\n", q->state);
914 #endif
915                         KKASSERT(dyn_rst_lifetime < dyn_keepalive_period);
916                         q->expire = time_second + dyn_rst_lifetime;
917                         break;
918                 }
919         } else if (pkt->proto == IPPROTO_UDP) {
920                 q->expire = time_second + dyn_udp_lifetime;
921         } else {
922                 /* other protocols */
923                 q->expire = time_second + dyn_short_lifetime;
924         }
925 done:
926         if (match_direction)
927                 *match_direction = dir;
928         return q;
929 }
930
931 static struct ip_fw *
932 lookup_rule(struct ipfw_flow_id *pkt, int *match_direction, struct tcphdr *tcp,
933             uint16_t len, int *deny)
934 {
935         struct ip_fw *rule = NULL;
936         ipfw_dyn_rule *q;
937         uint32_t gen;
938
939         *deny = 0;
940         gen = static_gen;
941
942         lockmgr(&dyn_lock, LK_SHARED);
943
944         if (static_gen != gen) {
945                 /*
946                  * Static rules had been change when we were waiting
947                  * for the dynamic hash table lock; deny this packet,
948                  * since it is _not_ known whether it is safe to keep
949                  * iterating the static rules.
950                  */
951                 *deny = 1;
952                 goto back;
953         }
954
955         q = lookup_dyn_rule(pkt, match_direction, tcp);
956         if (q == NULL) {
957                 rule = NULL;
958         } else {
959                 rule = q->rule;
960
961                 /* XXX */
962                 q->pcnt++;
963                 q->bcnt += len;
964         }
965 back:
966         lockmgr(&dyn_lock, LK_RELEASE);
967         return rule;
968 }
969
970 static void
971 realloc_dynamic_table(void)
972 {
973         ipfw_dyn_rule **old_dyn_v;
974         uint32_t old_curr_dyn_buckets;
975
976         KASSERT(dyn_buckets <= 65536 && (dyn_buckets & (dyn_buckets - 1)) == 0,
977                 ("invalid dyn_buckets %d\n", dyn_buckets));
978
979         /* Save the current buckets array for later error recovery */
980         old_dyn_v = ipfw_dyn_v;
981         old_curr_dyn_buckets = curr_dyn_buckets;
982
983         curr_dyn_buckets = dyn_buckets;
984         for (;;) {
985                 ipfw_dyn_v = kmalloc(curr_dyn_buckets * sizeof(ipfw_dyn_rule *),
986                                      M_IPFW, M_NOWAIT | M_ZERO);
987                 if (ipfw_dyn_v != NULL || curr_dyn_buckets <= 2)
988                         break;
989
990                 curr_dyn_buckets /= 2;
991                 if (curr_dyn_buckets <= old_curr_dyn_buckets &&
992                     old_dyn_v != NULL) {
993                         /*
994                          * Don't try allocating smaller buckets array, reuse
995                          * the old one, which alreay contains enough buckets
996                          */
997                         break;
998                 }
999         }
1000
1001         if (ipfw_dyn_v != NULL) {
1002                 if (old_dyn_v != NULL)
1003                         kfree(old_dyn_v, M_IPFW);
1004         } else {
1005                 /* Allocation failed, restore old buckets array */
1006                 ipfw_dyn_v = old_dyn_v;
1007                 curr_dyn_buckets = old_curr_dyn_buckets;
1008         }
1009
1010         if (ipfw_dyn_v != NULL)
1011                 ++dyn_buckets_gen;
1012 }
1013
1014 /**
1015  * Install state of type 'type' for a dynamic session.
1016  * The hash table contains two type of rules:
1017  * - regular rules (O_KEEP_STATE)
1018  * - rules for sessions with limited number of sess per user
1019  *   (O_LIMIT). When they are created, the parent is
1020  *   increased by 1, and decreased on delete. In this case,
1021  *   the third parameter is the parent rule and not the chain.
1022  * - "parent" rules for the above (O_LIMIT_PARENT).
1023  */
1024 static ipfw_dyn_rule *
1025 add_dyn_rule(struct ipfw_flow_id *id, uint8_t dyn_type, struct ip_fw *rule)
1026 {
1027         ipfw_dyn_rule *r;
1028         int i;
1029
1030         if (ipfw_dyn_v == NULL ||
1031             (dyn_count == 0 && dyn_buckets != curr_dyn_buckets)) {
1032                 realloc_dynamic_table();
1033                 if (ipfw_dyn_v == NULL)
1034                         return NULL; /* failed ! */
1035         }
1036         i = hash_packet(id);
1037
1038         r = kmalloc(sizeof(*r), M_IPFW, M_NOWAIT | M_ZERO);
1039         if (r == NULL) {
1040                 kprintf ("sorry cannot allocate state\n");
1041                 return NULL;
1042         }
1043
1044         /* increase refcount on parent, and set pointer */
1045         if (dyn_type == O_LIMIT) {
1046                 ipfw_dyn_rule *parent = (ipfw_dyn_rule *)rule;
1047
1048                 if (parent->dyn_type != O_LIMIT_PARENT)
1049                         panic("invalid parent");
1050                 parent->count++;
1051                 r->parent = parent;
1052                 rule = parent->rule;
1053         }
1054
1055         r->id = *id;
1056         r->expire = time_second + dyn_syn_lifetime;
1057         r->rule = rule;
1058         r->dyn_type = dyn_type;
1059         r->pcnt = r->bcnt = 0;
1060         r->count = 0;
1061
1062         r->bucket = i;
1063         r->next = ipfw_dyn_v[i];
1064         ipfw_dyn_v[i] = r;
1065         dyn_count++;
1066         dyn_buckets_gen++;
1067         DEB(kprintf("-- add dyn entry ty %d 0x%08x %d -> 0x%08x %d, total %d\n",
1068            dyn_type,
1069            (r->id.src_ip), (r->id.src_port),
1070            (r->id.dst_ip), (r->id.dst_port),
1071            dyn_count );)
1072         return r;
1073 }
1074
1075 /**
1076  * lookup dynamic parent rule using pkt and rule as search keys.
1077  * If the lookup fails, then install one.
1078  */
1079 static ipfw_dyn_rule *
1080 lookup_dyn_parent(struct ipfw_flow_id *pkt, struct ip_fw *rule)
1081 {
1082         ipfw_dyn_rule *q;
1083         int i;
1084
1085         if (ipfw_dyn_v) {
1086                 i = hash_packet(pkt);
1087                 for (q = ipfw_dyn_v[i]; q != NULL; q = q->next) {
1088                         if (q->dyn_type == O_LIMIT_PARENT &&
1089                             rule == q->rule &&
1090                             pkt->proto == q->id.proto &&
1091                             pkt->src_ip == q->id.src_ip &&
1092                             pkt->dst_ip == q->id.dst_ip &&
1093                             pkt->src_port == q->id.src_port &&
1094                             pkt->dst_port == q->id.dst_port) {
1095                                 q->expire = time_second + dyn_short_lifetime;
1096                                 DEB(kprintf("lookup_dyn_parent found 0x%p\n",q);)
1097                                 return q;
1098                         }
1099                 }
1100         }
1101         return add_dyn_rule(pkt, O_LIMIT_PARENT, rule);
1102 }
1103
1104 /**
1105  * Install dynamic state for rule type cmd->o.opcode
1106  *
1107  * Returns 1 (failure) if state is not installed because of errors or because
1108  * session limitations are enforced.
1109  */
1110 static int
1111 install_state_locked(struct ip_fw *rule, ipfw_insn_limit *cmd,
1112                      struct ip_fw_args *args)
1113 {
1114         static int last_log; /* XXX */
1115
1116         ipfw_dyn_rule *q;
1117
1118         DEB(kprintf("-- install state type %d 0x%08x %u -> 0x%08x %u\n",
1119             cmd->o.opcode,
1120             (args->f_id.src_ip), (args->f_id.src_port),
1121             (args->f_id.dst_ip), (args->f_id.dst_port) );)
1122
1123         q = lookup_dyn_rule(&args->f_id, NULL, NULL);
1124         if (q != NULL) { /* should never occur */
1125                 if (last_log != time_second) {
1126                         last_log = time_second;
1127                         kprintf(" install_state: entry already present, done\n");
1128                 }
1129                 return 0;
1130         }
1131
1132         if (dyn_count >= dyn_max) {
1133                 /*
1134                  * Run out of slots, try to remove any expired rule.
1135                  */
1136                 remove_dyn_rule_locked(NULL, (ipfw_dyn_rule *)1);
1137                 if (dyn_count >= dyn_max) {
1138                         if (last_log != time_second) {
1139                                 last_log = time_second;
1140                                 kprintf("install_state: "
1141                                         "Too many dynamic rules\n");
1142                         }
1143                         return 1; /* cannot install, notify caller */
1144                 }
1145         }
1146
1147         switch (cmd->o.opcode) {
1148         case O_KEEP_STATE: /* bidir rule */
1149                 if (add_dyn_rule(&args->f_id, O_KEEP_STATE, rule) == NULL)
1150                         return 1;
1151                 break;
1152
1153         case O_LIMIT: /* limit number of sessions */
1154                 {
1155                         uint16_t limit_mask = cmd->limit_mask;
1156                         struct ipfw_flow_id id;
1157                         ipfw_dyn_rule *parent;
1158
1159                         DEB(kprintf("installing dyn-limit rule %d\n",
1160                             cmd->conn_limit);)
1161
1162                         id.dst_ip = id.src_ip = 0;
1163                         id.dst_port = id.src_port = 0;
1164                         id.proto = args->f_id.proto;
1165
1166                         if (limit_mask & DYN_SRC_ADDR)
1167                                 id.src_ip = args->f_id.src_ip;
1168                         if (limit_mask & DYN_DST_ADDR)
1169                                 id.dst_ip = args->f_id.dst_ip;
1170                         if (limit_mask & DYN_SRC_PORT)
1171                                 id.src_port = args->f_id.src_port;
1172                         if (limit_mask & DYN_DST_PORT)
1173                                 id.dst_port = args->f_id.dst_port;
1174
1175                         parent = lookup_dyn_parent(&id, rule);
1176                         if (parent == NULL) {
1177                                 kprintf("add parent failed\n");
1178                                 return 1;
1179                         }
1180
1181                         if (parent->count >= cmd->conn_limit) {
1182                                 /*
1183                                  * See if we can remove some expired rule.
1184                                  */
1185                                 remove_dyn_rule_locked(rule, parent);
1186                                 if (parent->count >= cmd->conn_limit) {
1187                                         if (fw_verbose &&
1188                                             last_log != time_second) {
1189                                                 last_log = time_second;
1190                                                 log(LOG_SECURITY | LOG_DEBUG,
1191                                                     "drop session, "
1192                                                     "too many entries\n");
1193                                         }
1194                                         return 1;
1195                                 }
1196                         }
1197                         if (add_dyn_rule(&args->f_id, O_LIMIT,
1198                                          (struct ip_fw *)parent) == NULL)
1199                                 return 1;
1200                 }
1201                 break;
1202         default:
1203                 kprintf("unknown dynamic rule type %u\n", cmd->o.opcode);
1204                 return 1;
1205         }
1206         lookup_dyn_rule(&args->f_id, NULL, NULL); /* XXX just set lifetime */
1207         return 0;
1208 }
1209
1210 static int
1211 install_state(struct ip_fw *rule, ipfw_insn_limit *cmd,
1212               struct ip_fw_args *args, int *deny)
1213 {
1214         uint32_t gen;
1215         int ret = 0;
1216
1217         *deny = 0;
1218         gen = static_gen;
1219
1220         lockmgr(&dyn_lock, LK_EXCLUSIVE);
1221         if (static_gen != gen) {
1222                 /* See the comment in lookup_rule() */
1223                 *deny = 1;
1224         } else {
1225                 ret = install_state_locked(rule, cmd, args);
1226         }
1227         lockmgr(&dyn_lock, LK_RELEASE);
1228
1229         return ret;
1230 }
1231
1232 /*
1233  * Transmit a TCP packet, containing either a RST or a keepalive.
1234  * When flags & TH_RST, we are sending a RST packet, because of a
1235  * "reset" action matched the packet.
1236  * Otherwise we are sending a keepalive, and flags & TH_
1237  */
1238 static void
1239 send_pkt(struct ipfw_flow_id *id, uint32_t seq, uint32_t ack, int flags)
1240 {
1241         struct mbuf *m;
1242         struct ip *ip;
1243         struct tcphdr *tcp;
1244         struct route sro;       /* fake route */
1245
1246         MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1247         if (m == NULL)
1248                 return;
1249         m->m_pkthdr.rcvif = NULL;
1250         m->m_pkthdr.len = m->m_len = sizeof(struct ip) + sizeof(struct tcphdr);
1251         m->m_data += max_linkhdr;
1252
1253         ip = mtod(m, struct ip *);
1254         bzero(ip, m->m_len);
1255         tcp = (struct tcphdr *)(ip + 1); /* no IP options */
1256         ip->ip_p = IPPROTO_TCP;
1257         tcp->th_off = 5;
1258
1259         /*
1260          * Assume we are sending a RST (or a keepalive in the reverse
1261          * direction), swap src and destination addresses and ports.
1262          */
1263         ip->ip_src.s_addr = htonl(id->dst_ip);
1264         ip->ip_dst.s_addr = htonl(id->src_ip);
1265         tcp->th_sport = htons(id->dst_port);
1266         tcp->th_dport = htons(id->src_port);
1267         if (flags & TH_RST) {   /* we are sending a RST */
1268                 if (flags & TH_ACK) {
1269                         tcp->th_seq = htonl(ack);
1270                         tcp->th_ack = htonl(0);
1271                         tcp->th_flags = TH_RST;
1272                 } else {
1273                         if (flags & TH_SYN)
1274                                 seq++;
1275                         tcp->th_seq = htonl(0);
1276                         tcp->th_ack = htonl(seq);
1277                         tcp->th_flags = TH_RST | TH_ACK;
1278                 }
1279         } else {
1280                 /*
1281                  * We are sending a keepalive. flags & TH_SYN determines
1282                  * the direction, forward if set, reverse if clear.
1283                  * NOTE: seq and ack are always assumed to be correct
1284                  * as set by the caller. This may be confusing...
1285                  */
1286                 if (flags & TH_SYN) {
1287                         /*
1288                          * we have to rewrite the correct addresses!
1289                          */
1290                         ip->ip_dst.s_addr = htonl(id->dst_ip);
1291                         ip->ip_src.s_addr = htonl(id->src_ip);
1292                         tcp->th_dport = htons(id->dst_port);
1293                         tcp->th_sport = htons(id->src_port);
1294                 }
1295                 tcp->th_seq = htonl(seq);
1296                 tcp->th_ack = htonl(ack);
1297                 tcp->th_flags = TH_ACK;
1298         }
1299
1300         /*
1301          * set ip_len to the payload size so we can compute
1302          * the tcp checksum on the pseudoheader
1303          * XXX check this, could save a couple of words ?
1304          */
1305         ip->ip_len = htons(sizeof(struct tcphdr));
1306         tcp->th_sum = in_cksum(m, m->m_pkthdr.len);
1307
1308         /*
1309          * now fill fields left out earlier
1310          */
1311         ip->ip_ttl = ip_defttl;
1312         ip->ip_len = m->m_pkthdr.len;
1313
1314         bzero(&sro, sizeof(sro));
1315         ip_rtaddr(ip->ip_dst, &sro);
1316
1317         m->m_pkthdr.fw_flags |= IPFW_MBUF_GENERATED;
1318         ip_output(m, NULL, &sro, 0, NULL, NULL);
1319         if (sro.ro_rt)
1320                 RTFREE(sro.ro_rt);
1321 }
1322
1323 /*
1324  * sends a reject message, consuming the mbuf passed as an argument.
1325  */
1326 static void
1327 send_reject(struct ip_fw_args *args, int code, int offset, int ip_len)
1328 {
1329         if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */
1330                 /* We need the IP header in host order for icmp_error(). */
1331                 if (args->eh != NULL) {
1332                         struct ip *ip = mtod(args->m, struct ip *);
1333
1334                         ip->ip_len = ntohs(ip->ip_len);
1335                         ip->ip_off = ntohs(ip->ip_off);
1336                 }
1337                 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
1338         } else if (offset == 0 && args->f_id.proto == IPPROTO_TCP) {
1339                 struct tcphdr *const tcp =
1340                     L3HDR(struct tcphdr, mtod(args->m, struct ip *));
1341
1342                 if ((tcp->th_flags & TH_RST) == 0) {
1343                         send_pkt(&args->f_id, ntohl(tcp->th_seq),
1344                                  ntohl(tcp->th_ack), tcp->th_flags | TH_RST);
1345                 }
1346                 m_freem(args->m);
1347         } else {
1348                 m_freem(args->m);
1349         }
1350         args->m = NULL;
1351 }
1352
1353 /**
1354  *
1355  * Given an ip_fw *, lookup_next_rule will return a pointer
1356  * to the next rule, which can be either the jump
1357  * target (for skipto instructions) or the next one in the list (in
1358  * all other cases including a missing jump target).
1359  * The result is also written in the "next_rule" field of the rule.
1360  * Backward jumps are not allowed, so start looking from the next
1361  * rule...
1362  *
1363  * This never returns NULL -- in case we do not have an exact match,
1364  * the next rule is returned. When the ruleset is changed,
1365  * pointers are flushed so we are always correct.
1366  */
1367
1368 static struct ip_fw *
1369 lookup_next_rule(struct ip_fw *me)
1370 {
1371         struct ip_fw *rule = NULL;
1372         ipfw_insn *cmd;
1373
1374         /* look for action, in case it is a skipto */
1375         cmd = ACTION_PTR(me);
1376         if (cmd->opcode == O_LOG)
1377                 cmd += F_LEN(cmd);
1378         if (cmd->opcode == O_SKIPTO) {
1379                 for (rule = me->next; rule; rule = rule->next) {
1380                         if (rule->rulenum >= cmd->arg1)
1381                                 break;
1382                 }
1383         }
1384         if (rule == NULL)                       /* failure or not a skipto */
1385                 rule = me->next;
1386         me->next_rule = rule;
1387         return rule;
1388 }
1389
1390 /*
1391  * The main check routine for the firewall.
1392  *
1393  * All arguments are in args so we can modify them and return them
1394  * back to the caller.
1395  *
1396  * Parameters:
1397  *
1398  *      args->m (in/out) The packet; we set to NULL when/if we nuke it.
1399  *              Starts with the IP header.
1400  *      args->eh (in)   Mac header if present, or NULL for layer3 packet.
1401  *      args->oif       Outgoing interface, or NULL if packet is incoming.
1402  *              The incoming interface is in the mbuf. (in)
1403  *
1404  *      args->rule      Pointer to the last matching rule (in/out)
1405  *      args->next_hop  Socket we are forwarding to (out).
1406  *      args->f_id      Addresses grabbed from the packet (out)
1407  *
1408  * Return value:
1409  *
1410  *      IP_FW_PORT_DENY_FLAG    the packet must be dropped.
1411  *      0       The packet is to be accepted and routed normally OR
1412  *              the packet was denied/rejected and has been dropped;
1413  *              in the latter case, *m is equal to NULL upon return.
1414  *      port    Divert the packet to port, with these caveats:
1415  *
1416  *              - If IP_FW_PORT_TEE_FLAG is set, tee the packet instead
1417  *                of diverting it (ie, 'ipfw tee').
1418  *
1419  *              - If IP_FW_PORT_DYNT_FLAG is set, interpret the lower
1420  *                16 bits as a dummynet pipe number instead of diverting
1421  */
1422
1423 static int
1424 ipfw_chk(struct ip_fw_args *args)
1425 {
1426         /*
1427          * Local variables hold state during the processing of a packet.
1428          *
1429          * IMPORTANT NOTE: to speed up the processing of rules, there
1430          * are some assumption on the values of the variables, which
1431          * are documented here. Should you change them, please check
1432          * the implementation of the various instructions to make sure
1433          * that they still work.
1434          *
1435          * args->eh     The MAC header. It is non-null for a layer2
1436          *      packet, it is NULL for a layer-3 packet.
1437          *
1438          * m | args->m  Pointer to the mbuf, as received from the caller.
1439          *      It may change if ipfw_chk() does an m_pullup, or if it
1440          *      consumes the packet because it calls send_reject().
1441          *      XXX This has to change, so that ipfw_chk() never modifies
1442          *      or consumes the buffer.
1443          * ip   is simply an alias of the value of m, and it is kept
1444          *      in sync with it (the packet is  supposed to start with
1445          *      the ip header).
1446          */
1447         struct mbuf *m = args->m;
1448         struct ip *ip = mtod(m, struct ip *);
1449
1450         /*
1451          * oif | args->oif      If NULL, ipfw_chk has been called on the
1452          *      inbound path (ether_input, ip_input).
1453          *      If non-NULL, ipfw_chk has been called on the outbound path
1454          *      (ether_output, ip_output).
1455          */
1456         struct ifnet *oif = args->oif;
1457
1458         struct ip_fw *f = NULL;         /* matching rule */
1459         int retval = 0;
1460         struct m_tag *mtag;
1461
1462         /*
1463          * hlen The length of the IPv4 header.
1464          *      hlen >0 means we have an IPv4 packet.
1465          */
1466         u_int hlen = 0;         /* hlen >0 means we have an IP pkt */
1467
1468         /*
1469          * offset       The offset of a fragment. offset != 0 means that
1470          *      we have a fragment at this offset of an IPv4 packet.
1471          *      offset == 0 means that (if this is an IPv4 packet)
1472          *      this is the first or only fragment.
1473          */
1474         u_short offset = 0;
1475
1476         /*
1477          * Local copies of addresses. They are only valid if we have
1478          * an IP packet.
1479          *
1480          * proto        The protocol. Set to 0 for non-ip packets,
1481          *      or to the protocol read from the packet otherwise.
1482          *      proto != 0 means that we have an IPv4 packet.
1483          *
1484          * src_port, dst_port   port numbers, in HOST format. Only
1485          *      valid for TCP and UDP packets.
1486          *
1487          * src_ip, dst_ip       ip addresses, in NETWORK format.
1488          *      Only valid for IPv4 packets.
1489          */
1490         uint8_t proto;
1491         uint16_t src_port = 0, dst_port = 0;    /* NOTE: host format    */
1492         struct in_addr src_ip, dst_ip;          /* NOTE: network format */
1493         uint16_t ip_len = 0;
1494         int dyn_dir = MATCH_UNKNOWN;
1495         ipfw_dyn_rule *q = NULL;
1496
1497         if (m->m_pkthdr.fw_flags & IPFW_MBUF_GENERATED)
1498                 return 0;       /* accept */
1499         /*
1500          * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1501          *      MATCH_NONE when checked and not matched (q = NULL),
1502          *      MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
1503          */
1504
1505         if (args->eh == NULL ||         /* layer 3 packet */
1506             (m->m_pkthdr.len >= sizeof(struct ip) &&
1507              ntohs(args->eh->ether_type) == ETHERTYPE_IP))
1508                 hlen = ip->ip_hl << 2;
1509
1510         /*
1511          * Collect parameters into local variables for faster matching.
1512          */
1513         if (hlen == 0) {        /* do not grab addresses for non-ip pkts */
1514                 proto = args->f_id.proto = 0;   /* mark f_id invalid */
1515                 goto after_ip_checks;
1516         }
1517
1518         proto = args->f_id.proto = ip->ip_p;
1519         src_ip = ip->ip_src;
1520         dst_ip = ip->ip_dst;
1521         if (args->eh != NULL) { /* layer 2 packets are as on the wire */
1522                 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1523                 ip_len = ntohs(ip->ip_len);
1524         } else {
1525                 offset = ip->ip_off & IP_OFFMASK;
1526                 ip_len = ip->ip_len;
1527         }
1528
1529 #define PULLUP_TO(len)                          \
1530 do {                                            \
1531         if (m->m_len < (len)) {                 \
1532                 args->m = m = m_pullup(m, (len));\
1533                 if (m == NULL)                  \
1534                         goto pullup_failed;     \
1535                 ip = mtod(m, struct ip *);      \
1536         }                                       \
1537 } while (0)
1538
1539         if (offset == 0) {
1540                 switch (proto) {
1541                 case IPPROTO_TCP:
1542                         {
1543                                 struct tcphdr *tcp;
1544
1545                                 PULLUP_TO(hlen + sizeof(struct tcphdr));
1546                                 tcp = L3HDR(struct tcphdr, ip);
1547                                 dst_port = tcp->th_dport;
1548                                 src_port = tcp->th_sport;
1549                                 args->f_id.flags = tcp->th_flags;
1550                         }
1551                         break;
1552
1553                 case IPPROTO_UDP:
1554                         {
1555                                 struct udphdr *udp;
1556
1557                                 PULLUP_TO(hlen + sizeof(struct udphdr));
1558                                 udp = L3HDR(struct udphdr, ip);
1559                                 dst_port = udp->uh_dport;
1560                                 src_port = udp->uh_sport;
1561                         }
1562                         break;
1563
1564                 case IPPROTO_ICMP:
1565                         PULLUP_TO(hlen + 4);    /* type, code and checksum. */
1566                         args->f_id.flags = L3HDR(struct icmp, ip)->icmp_type;
1567                         break;
1568
1569                 default:
1570                         break;
1571                 }
1572         }
1573
1574 #undef PULLUP_TO
1575
1576         args->f_id.src_ip = ntohl(src_ip.s_addr);
1577         args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1578         args->f_id.src_port = src_port = ntohs(src_port);
1579         args->f_id.dst_port = dst_port = ntohs(dst_port);
1580
1581 after_ip_checks:
1582         if (args->rule) {
1583                 /*
1584                  * Packet has already been tagged. Look for the next rule
1585                  * to restart processing.
1586                  *
1587                  * If fw_one_pass != 0 then just accept it.
1588                  * XXX should not happen here, but optimized out in
1589                  * the caller.
1590                  */
1591                 if (fw_one_pass)
1592                         return 0;
1593
1594                 /* This rule was deleted */
1595                 if (args->rule->rule_flags & IPFW_RULE_F_INVALID)
1596                         return IP_FW_PORT_DENY_FLAG;
1597
1598                 f = args->rule->next_rule;
1599                 if (f == NULL)
1600                         f = lookup_next_rule(args->rule);
1601         } else {
1602                 /*
1603                  * Find the starting rule. It can be either the first
1604                  * one, or the one after divert_rule if asked so.
1605                  */
1606                 int skipto;
1607
1608                 mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL);
1609                 if (mtag != NULL)
1610                         skipto = *(uint16_t *)m_tag_data(mtag);
1611                 else
1612                         skipto = 0;
1613
1614                 f = layer3_chain;
1615                 if (args->eh == NULL && skipto != 0) {
1616                         if (skipto >= IPFW_DEFAULT_RULE)
1617                                 return(IP_FW_PORT_DENY_FLAG); /* invalid */
1618                         while (f && f->rulenum <= skipto)
1619                                 f = f->next;
1620                         if (f == NULL)  /* drop packet */
1621                                 return(IP_FW_PORT_DENY_FLAG);
1622                 }
1623         }
1624         if ((mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
1625                 m_tag_delete(m, mtag);
1626
1627         /*
1628          * Now scan the rules, and parse microinstructions for each rule.
1629          */
1630         for (; f; f = f->next) {
1631                 int l, cmdlen;
1632                 ipfw_insn *cmd;
1633                 int skip_or; /* skip rest of OR block */
1634
1635 again:
1636                 if (set_disable & (1 << f->set))
1637                         continue;
1638
1639                 skip_or = 0;
1640                 for (l = f->cmd_len, cmd = f->cmd; l > 0;
1641                      l -= cmdlen, cmd += cmdlen) {
1642                         int match, deny;
1643
1644                         /*
1645                          * check_body is a jump target used when we find a
1646                          * CHECK_STATE, and need to jump to the body of
1647                          * the target rule.
1648                          */
1649
1650 check_body:
1651                         cmdlen = F_LEN(cmd);
1652                         /*
1653                          * An OR block (insn_1 || .. || insn_n) has the
1654                          * F_OR bit set in all but the last instruction.
1655                          * The first match will set "skip_or", and cause
1656                          * the following instructions to be skipped until
1657                          * past the one with the F_OR bit clear.
1658                          */
1659                         if (skip_or) {          /* skip this instruction */
1660                                 if ((cmd->len & F_OR) == 0)
1661                                         skip_or = 0;    /* next one is good */
1662                                 continue;
1663                         }
1664                         match = 0; /* set to 1 if we succeed */
1665
1666                         switch (cmd->opcode) {
1667                         /*
1668                          * The first set of opcodes compares the packet's
1669                          * fields with some pattern, setting 'match' if a
1670                          * match is found. At the end of the loop there is
1671                          * logic to deal with F_NOT and F_OR flags associated
1672                          * with the opcode.
1673                          */
1674                         case O_NOP:
1675                                 match = 1;
1676                                 break;
1677
1678                         case O_FORWARD_MAC:
1679                                 kprintf("ipfw: opcode %d unimplemented\n",
1680                                         cmd->opcode);
1681                                 break;
1682
1683                         case O_GID:
1684                         case O_UID:
1685                                 /*
1686                                  * We only check offset == 0 && proto != 0,
1687                                  * as this ensures that we have an IPv4
1688                                  * packet with the ports info.
1689                                  */
1690                                 if (offset!=0)
1691                                         break;
1692                             {
1693                                 struct inpcbinfo *pi;
1694                                 int wildcard;
1695                                 struct inpcb *pcb;
1696
1697                                 if (proto == IPPROTO_TCP) {
1698                                         wildcard = 0;
1699                                         pi = &tcbinfo[mycpu->gd_cpuid];
1700                                 } else if (proto == IPPROTO_UDP) {
1701                                         wildcard = 1;
1702                                         pi = &udbinfo;
1703                                 } else
1704                                         break;
1705
1706                                 pcb =  (oif) ?
1707                                         in_pcblookup_hash(pi,
1708                                             dst_ip, htons(dst_port),
1709                                             src_ip, htons(src_port),
1710                                             wildcard, oif) :
1711                                         in_pcblookup_hash(pi,
1712                                             src_ip, htons(src_port),
1713                                             dst_ip, htons(dst_port),
1714                                             wildcard, NULL);
1715
1716                                 if (pcb == NULL || pcb->inp_socket == NULL)
1717                                         break;
1718
1719                                 if (cmd->opcode == O_UID) {
1720 #define socheckuid(a,b) ((a)->so_cred->cr_uid != (b))
1721                                         match =
1722                                           !socheckuid(pcb->inp_socket,
1723                                            (uid_t)((ipfw_insn_u32 *)cmd)->d[0]);
1724 #undef socheckuid
1725                                 } else  {
1726                                         match = groupmember(
1727                                             (uid_t)((ipfw_insn_u32 *)cmd)->d[0],
1728                                             pcb->inp_socket->so_cred);
1729                                 }
1730                             }
1731                                 break;
1732
1733                         case O_RECV:
1734                                 match = iface_match(m->m_pkthdr.rcvif,
1735                                     (ipfw_insn_if *)cmd);
1736                                 break;
1737
1738                         case O_XMIT:
1739                                 match = iface_match(oif, (ipfw_insn_if *)cmd);
1740                                 break;
1741
1742                         case O_VIA:
1743                                 match = iface_match(oif ? oif :
1744                                     m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd);
1745                                 break;
1746
1747                         case O_MACADDR2:
1748                                 if (args->eh != NULL) { /* have MAC header */
1749                                         uint32_t *want = (uint32_t *)
1750                                                 ((ipfw_insn_mac *)cmd)->addr;
1751                                         uint32_t *mask = (uint32_t *)
1752                                                 ((ipfw_insn_mac *)cmd)->mask;
1753                                         uint32_t *hdr = (uint32_t *)args->eh;
1754
1755                                         match =
1756                                         (want[0] == (hdr[0] & mask[0]) &&
1757                                          want[1] == (hdr[1] & mask[1]) &&
1758                                          want[2] == (hdr[2] & mask[2]));
1759                                 }
1760                                 break;
1761
1762                         case O_MAC_TYPE:
1763                                 if (args->eh != NULL) {
1764                                         uint16_t t =
1765                                             ntohs(args->eh->ether_type);
1766                                         uint16_t *p =
1767                                             ((ipfw_insn_u16 *)cmd)->ports;
1768                                         int i;
1769
1770                                         /* Special vlan handling */
1771                                         if (m->m_flags & M_VLANTAG)
1772                                                 t = ETHERTYPE_VLAN;
1773
1774                                         for (i = cmdlen - 1; !match && i > 0;
1775                                              i--, p += 2) {
1776                                                 match =
1777                                                 (t >= p[0] && t <= p[1]);
1778                                         }
1779                                 }
1780                                 break;
1781
1782                         case O_FRAG:
1783                                 match = (hlen > 0 && offset != 0);
1784                                 break;
1785
1786                         case O_IN:      /* "out" is "not in" */
1787                                 match = (oif == NULL);
1788                                 break;
1789
1790                         case O_LAYER2:
1791                                 match = (args->eh != NULL);
1792                                 break;
1793
1794                         case O_PROTO:
1795                                 /*
1796                                  * We do not allow an arg of 0 so the
1797                                  * check of "proto" only suffices.
1798                                  */
1799                                 match = (proto == cmd->arg1);
1800                                 break;
1801
1802                         case O_IP_SRC:
1803                                 match = (hlen > 0 &&
1804                                     ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1805                                     src_ip.s_addr);
1806                                 break;
1807
1808                         case O_IP_SRC_MASK:
1809                                 match = (hlen > 0 &&
1810                                     ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1811                                      (src_ip.s_addr &
1812                                      ((ipfw_insn_ip *)cmd)->mask.s_addr));
1813                                 break;
1814
1815                         case O_IP_SRC_ME:
1816                                 if (hlen > 0) {
1817                                         struct ifnet *tif;
1818
1819                                         tif = INADDR_TO_IFP(&src_ip);
1820                                         match = (tif != NULL);
1821                                 }
1822                                 break;
1823
1824                         case O_IP_DST_SET:
1825                         case O_IP_SRC_SET:
1826                                 if (hlen > 0) {
1827                                         uint32_t *d = (uint32_t *)(cmd + 1);
1828                                         uint32_t addr =
1829                                             cmd->opcode == O_IP_DST_SET ?
1830                                                 args->f_id.dst_ip :
1831                                                 args->f_id.src_ip;
1832
1833                                         if (addr < d[0])
1834                                                 break;
1835                                         addr -= d[0]; /* subtract base */
1836                                         match =
1837                                         (addr < cmd->arg1) &&
1838                                          (d[1 + (addr >> 5)] &
1839                                           (1 << (addr & 0x1f)));
1840                                 }
1841                                 break;
1842
1843                         case O_IP_DST:
1844                                 match = (hlen > 0 &&
1845                                     ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1846                                     dst_ip.s_addr);
1847                                 break;
1848
1849                         case O_IP_DST_MASK:
1850                                 match = (hlen > 0) &&
1851                                     (((ipfw_insn_ip *)cmd)->addr.s_addr ==
1852                                      (dst_ip.s_addr &
1853                                      ((ipfw_insn_ip *)cmd)->mask.s_addr));
1854                                 break;
1855
1856                         case O_IP_DST_ME:
1857                                 if (hlen > 0) {
1858                                         struct ifnet *tif;
1859
1860                                         tif = INADDR_TO_IFP(&dst_ip);
1861                                         match = (tif != NULL);
1862                                 }
1863                                 break;
1864
1865                         case O_IP_SRCPORT:
1866                         case O_IP_DSTPORT:
1867                                 /*
1868                                  * offset == 0 && proto != 0 is enough
1869                                  * to guarantee that we have an IPv4
1870                                  * packet with port info.
1871                                  */
1872                                 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP)
1873                                     && offset == 0) {
1874                                         uint16_t x =
1875                                             (cmd->opcode == O_IP_SRCPORT) ?
1876                                                 src_port : dst_port ;
1877                                         uint16_t *p =
1878                                             ((ipfw_insn_u16 *)cmd)->ports;
1879                                         int i;
1880
1881                                         for (i = cmdlen - 1; !match && i > 0;
1882                                              i--, p += 2) {
1883                                                 match =
1884                                                 (x >= p[0] && x <= p[1]);
1885                                         }
1886                                 }
1887                                 break;
1888
1889                         case O_ICMPTYPE:
1890                                 match = (offset == 0 && proto==IPPROTO_ICMP &&
1891                                     icmptype_match(ip, (ipfw_insn_u32 *)cmd));
1892                                 break;
1893
1894                         case O_IPOPT:
1895                                 match = (hlen > 0 && ipopts_match(ip, cmd));
1896                                 break;
1897
1898                         case O_IPVER:
1899                                 match = (hlen > 0 && cmd->arg1 == ip->ip_v);
1900                                 break;
1901
1902                         case O_IPTTL:
1903                                 match = (hlen > 0 && cmd->arg1 == ip->ip_ttl);
1904                                 break;
1905
1906                         case O_IPID:
1907                                 match = (hlen > 0 &&
1908                                     cmd->arg1 == ntohs(ip->ip_id));
1909                                 break;
1910
1911                         case O_IPLEN:
1912                                 match = (hlen > 0 && cmd->arg1 == ip_len);
1913                                 break;
1914
1915                         case O_IPPRECEDENCE:
1916                                 match = (hlen > 0 &&
1917                                     (cmd->arg1 == (ip->ip_tos & 0xe0)));
1918                                 break;
1919
1920                         case O_IPTOS:
1921                                 match = (hlen > 0 &&
1922                                     flags_match(cmd, ip->ip_tos));
1923                                 break;
1924
1925                         case O_TCPFLAGS:
1926                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1927                                     flags_match(cmd,
1928                                         L3HDR(struct tcphdr,ip)->th_flags));
1929                                 break;
1930
1931                         case O_TCPOPTS:
1932                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1933                                     tcpopts_match(ip, cmd));
1934                                 break;
1935
1936                         case O_TCPSEQ:
1937                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1938                                     ((ipfw_insn_u32 *)cmd)->d[0] ==
1939                                         L3HDR(struct tcphdr,ip)->th_seq);
1940                                 break;
1941
1942                         case O_TCPACK:
1943                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1944                                     ((ipfw_insn_u32 *)cmd)->d[0] ==
1945                                         L3HDR(struct tcphdr,ip)->th_ack);
1946                                 break;
1947
1948                         case O_TCPWIN:
1949                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1950                                     cmd->arg1 ==
1951                                         L3HDR(struct tcphdr,ip)->th_win);
1952                                 break;
1953
1954                         case O_ESTAB:
1955                                 /* reject packets which have SYN only */
1956                                 /* XXX should i also check for TH_ACK ? */
1957                                 match = (proto == IPPROTO_TCP && offset == 0 &&
1958                                     (L3HDR(struct tcphdr,ip)->th_flags &
1959                                      (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
1960                                 break;
1961
1962                         case O_LOG:
1963                                 if (fw_verbose)
1964                                         ipfw_log(f, hlen, args->eh, m, oif);
1965                                 match = 1;
1966                                 break;
1967
1968                         case O_PROB:
1969                                 match = (krandom() <
1970                                         ((ipfw_insn_u32 *)cmd)->d[0]);
1971                                 break;
1972
1973                         /*
1974                          * The second set of opcodes represents 'actions',
1975                          * i.e. the terminal part of a rule once the packet
1976                          * matches all previous patterns.
1977                          * Typically there is only one action for each rule,
1978                          * and the opcode is stored at the end of the rule
1979                          * (but there are exceptions -- see below).
1980                          *
1981                          * In general, here we set retval and terminate the
1982                          * outer loop (would be a 'break 3' in some language,
1983                          * but we need to do a 'goto done').
1984                          *
1985                          * Exceptions:
1986                          * O_COUNT and O_SKIPTO actions:
1987                          *   instead of terminating, we jump to the next rule
1988                          *   ('goto next_rule', equivalent to a 'break 2'),
1989                          *   or to the SKIPTO target ('goto again' after
1990                          *   having set f, cmd and l), respectively.
1991                          *
1992                          * O_LIMIT and O_KEEP_STATE: these opcodes are
1993                          *   not real 'actions', and are stored right
1994                          *   before the 'action' part of the rule.
1995                          *   These opcodes try to install an entry in the
1996                          *   state tables; if successful, we continue with
1997                          *   the next opcode (match=1; break;), otherwise
1998                          *   the packet *   must be dropped
1999                          *   ('goto done' after setting retval);
2000                          *
2001                          * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2002                          *   cause a lookup of the state table, and a jump
2003                          *   to the 'action' part of the parent rule
2004                          *   ('goto check_body') if an entry is found, or
2005                          *   (CHECK_STATE only) a jump to the next rule if
2006                          *   the entry is not found ('goto next_rule').
2007                          *   The result of the lookup is cached to make
2008                          *   further instances of these opcodes are
2009                          *   effectively NOPs.
2010                          */
2011                         case O_LIMIT:
2012                         case O_KEEP_STATE:
2013                                 if (install_state(f,
2014                                     (ipfw_insn_limit *)cmd, args, &deny)) {
2015                                         if (deny)
2016                                                 return IP_FW_PORT_DENY_FLAG;
2017
2018                                         retval = IP_FW_PORT_DENY_FLAG;
2019                                         goto done; /* error/limit violation */
2020                                 }
2021                                 if (deny)
2022                                         return IP_FW_PORT_DENY_FLAG;
2023                                 match = 1;
2024                                 break;
2025
2026                         case O_PROBE_STATE:
2027                         case O_CHECK_STATE:
2028                                 /*
2029                                  * dynamic rules are checked at the first
2030                                  * keep-state or check-state occurrence,
2031                                  * with the result being stored in dyn_dir.
2032                                  * The compiler introduces a PROBE_STATE
2033                                  * instruction for us when we have a
2034                                  * KEEP_STATE (because PROBE_STATE needs
2035                                  * to be run first).
2036                                  */
2037                                 if (dyn_dir == MATCH_UNKNOWN) {
2038                                         struct ip_fw *dyn_f;
2039
2040                                         dyn_f = lookup_rule(&args->f_id,
2041                                                 &dyn_dir,
2042                                                 proto == IPPROTO_TCP ?
2043                                                 L3HDR(struct tcphdr, ip) : NULL,
2044                                                 ip_len, &deny);
2045                                         if (deny)
2046                                                 return IP_FW_PORT_DENY_FLAG;
2047                                         if (dyn_f != NULL) {
2048                                                 /*
2049                                                  * Found a rule from a dynamic
2050                                                  * entry; jump to the 'action'
2051                                                  * part of the rule.
2052                                                  */
2053                                                 f = dyn_f;
2054                                                 cmd = ACTION_PTR(f);
2055                                                 l = f->cmd_len - f->act_ofs;
2056                                                 goto check_body;
2057                                         }
2058                                 }
2059                                 /*
2060                                  * Dynamic entry not found. If CHECK_STATE,
2061                                  * skip to next rule, if PROBE_STATE just
2062                                  * ignore and continue with next opcode.
2063                                  */
2064                                 if (cmd->opcode == O_CHECK_STATE)
2065                                         goto next_rule;
2066                                 match = 1;
2067                                 break;
2068
2069                         case O_ACCEPT:
2070                                 retval = 0;     /* accept */
2071                                 goto done;
2072
2073                         case O_PIPE:
2074                         case O_QUEUE:
2075                                 args->rule = f; /* report matching rule */
2076                                 retval = cmd->arg1 | IP_FW_PORT_DYNT_FLAG;
2077                                 goto done;
2078
2079                         case O_DIVERT:
2080                         case O_TEE:
2081                                 if (args->eh) /* not on layer 2 */
2082                                         break;
2083
2084                                 mtag = m_tag_get(PACKET_TAG_IPFW_DIVERT,
2085                                                  sizeof(uint16_t), MB_DONTWAIT);
2086                                 if (mtag == NULL) {
2087                                         retval = IP_FW_PORT_DENY_FLAG;
2088                                         goto done;
2089                                 }
2090                                 *(uint16_t *)m_tag_data(mtag) = f->rulenum;
2091                                 m_tag_prepend(m, mtag);
2092                                 retval = (cmd->opcode == O_DIVERT) ?
2093                                     cmd->arg1 :
2094                                     cmd->arg1 | IP_FW_PORT_TEE_FLAG;
2095                                 goto done;
2096
2097                         case O_COUNT:
2098                         case O_SKIPTO:
2099                                 f->pcnt++;      /* update stats */
2100                                 f->bcnt += ip_len;
2101                                 f->timestamp = time_second;
2102                                 if (cmd->opcode == O_COUNT)
2103                                         goto next_rule;
2104                                 /* handle skipto */
2105                                 if (f->next_rule == NULL)
2106                                         lookup_next_rule(f);
2107                                 f = f->next_rule;
2108                                 goto again;
2109
2110                         case O_REJECT:
2111                                 /*
2112                                  * Drop the packet and send a reject notice
2113                                  * if the packet is not ICMP (or is an ICMP
2114                                  * query), and it is not multicast/broadcast.
2115                                  */
2116                                 if (hlen > 0 &&
2117                                     (proto != IPPROTO_ICMP ||
2118                                      is_icmp_query(ip)) &&
2119                                     !(m->m_flags & (M_BCAST|M_MCAST)) &&
2120                                     !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2121                                         send_reject(args, cmd->arg1,
2122                                             offset,ip_len);
2123                                         m = args->m;
2124                                 }
2125                                 /* FALLTHROUGH */
2126                         case O_DENY:
2127                                 retval = IP_FW_PORT_DENY_FLAG;
2128                                 goto done;
2129
2130                         case O_FORWARD_IP:
2131                                 if (args->eh)   /* not valid on layer2 pkts */
2132                                         break;
2133                                 if (!q || dyn_dir == MATCH_FORWARD) {
2134                                         args->next_hop =
2135                                             &((ipfw_insn_sa *)cmd)->sa;
2136                                 }
2137                                 retval = 0;
2138                                 goto done;
2139
2140                         default:
2141                                 panic("-- unknown opcode %d\n", cmd->opcode);
2142                         } /* end of switch() on opcodes */
2143
2144                         if (cmd->len & F_NOT)
2145                                 match = !match;
2146
2147                         if (match) {
2148                                 if (cmd->len & F_OR)
2149                                         skip_or = 1;
2150                         } else {
2151                                 if (!(cmd->len & F_OR)) /* not an OR block, */
2152                                         break;          /* try next rule    */
2153                         }
2154
2155                 }       /* end of inner for, scan opcodes */
2156
2157 next_rule:;             /* try next rule                */
2158
2159         }               /* end of outer for, scan rules */
2160         kprintf("+++ ipfw: ouch!, skip past end of rules, denying packet\n");
2161         return(IP_FW_PORT_DENY_FLAG);
2162
2163 done:
2164         /* Update statistics */
2165         f->pcnt++;
2166         f->bcnt += ip_len;
2167         f->timestamp = time_second;
2168         return retval;
2169
2170 pullup_failed:
2171         if (fw_verbose)
2172                 kprintf("pullup failed\n");
2173         return(IP_FW_PORT_DENY_FLAG);
2174 }
2175
2176 static void
2177 ipfw_dummynet_io(struct mbuf *m, int pipe_nr, int dir, struct ip_fw_args *fwa)
2178 {
2179         struct m_tag *mtag;
2180         struct dn_pkt *pkt;
2181         ipfw_insn *cmd;
2182         const struct ipfw_flow_id *id;
2183         struct dn_flow_id *fid;
2184
2185         M_ASSERTPKTHDR(m);
2186
2187         mtag = m_tag_get(PACKET_TAG_DUMMYNET, sizeof(*pkt), MB_DONTWAIT);
2188         if (mtag == NULL) {
2189                 m_freem(m);
2190                 return;
2191         }
2192         m_tag_prepend(m, mtag);
2193
2194         pkt = m_tag_data(mtag);
2195         bzero(pkt, sizeof(*pkt));
2196
2197         cmd = fwa->rule->cmd + fwa->rule->act_ofs;
2198         if (cmd->opcode == O_LOG)
2199                 cmd += F_LEN(cmd);
2200         KASSERT(cmd->opcode == O_PIPE || cmd->opcode == O_QUEUE,
2201                 ("Rule is not PIPE or QUEUE, opcode %d\n", cmd->opcode));
2202
2203         pkt->dn_m = m;
2204         pkt->dn_flags = (dir & DN_FLAGS_DIR_MASK);
2205         pkt->ifp = fwa->oif;
2206         pkt->cpuid = mycpu->gd_cpuid;
2207         pkt->pipe_nr = pipe_nr;
2208
2209         id = &fwa->f_id;
2210         fid = &pkt->id;
2211         fid->fid_dst_ip = id->dst_ip;
2212         fid->fid_src_ip = id->src_ip;
2213         fid->fid_dst_port = id->dst_port;
2214         fid->fid_src_port = id->src_port;
2215         fid->fid_proto = id->proto;
2216         fid->fid_flags = id->flags;
2217
2218         ipfw_ref_rule(fwa->rule);
2219         pkt->dn_priv = fwa->rule;
2220         pkt->dn_unref_priv = ipfw_unref_rule;
2221
2222         if (cmd->opcode == O_PIPE)
2223                 pkt->dn_flags |= DN_FLAGS_IS_PIPE;
2224
2225         if (dir == DN_TO_IP_OUT) {
2226                 /*
2227                  * We need to copy *ro because for ICMP pkts (and maybe
2228                  * others) the caller passed a pointer into the stack;
2229                  * dst might also be a pointer into *ro so it needs to
2230                  * be updated.
2231                  */
2232                 pkt->ro = *(fwa->ro);
2233                 if (fwa->ro->ro_rt)
2234                         fwa->ro->ro_rt->rt_refcnt++;
2235                 if (fwa->dst == (struct sockaddr_in *)&fwa->ro->ro_dst) {
2236                         /* 'dst' points into 'ro' */
2237                         fwa->dst = (struct sockaddr_in *)&(pkt->ro.ro_dst);
2238                 }
2239                 pkt->dn_dst = fwa->dst;
2240                 pkt->flags = fwa->flags;
2241         }
2242
2243         m->m_pkthdr.fw_flags |= DUMMYNET_MBUF_TAGGED;
2244         ip_dn_queue(m);
2245 }
2246
2247 /*
2248  * When a rule is added/deleted, clear the next_rule pointers in all rules.
2249  * These will be reconstructed on the fly as packets are matched.
2250  * Must be called at splimp().
2251  */
2252 static void
2253 flush_rule_ptrs(void)
2254 {
2255         struct ip_fw *rule;
2256
2257         for (rule = layer3_chain; rule; rule = rule->next)
2258                 rule->next_rule = NULL;
2259 }
2260
2261 static __inline void
2262 ipfw_inc_static_count(struct ip_fw *rule)
2263 {
2264         IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
2265
2266         static_count++;
2267         static_ioc_len += IOC_RULESIZE(rule);
2268 }
2269
2270 static __inline void
2271 ipfw_dec_static_count(struct ip_fw *rule)
2272 {
2273         int l = IOC_RULESIZE(rule);
2274
2275         IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
2276
2277         KASSERT(static_count > 0, ("invalid static count %u\n", static_count));
2278         static_count--;
2279
2280         KASSERT(static_ioc_len >= l,
2281                 ("invalid static len %u\n", static_ioc_len));
2282         static_ioc_len -= l;
2283 }
2284
2285 static struct ip_fw *
2286 ipfw_create_rule(const struct ipfw_ioc_rule *ioc_rule)
2287 {
2288         struct ip_fw *rule;
2289
2290         rule = kmalloc(RULESIZE(ioc_rule), M_IPFW, M_WAITOK | M_ZERO);
2291
2292         rule->act_ofs = ioc_rule->act_ofs;
2293         rule->cmd_len = ioc_rule->cmd_len;
2294         rule->rulenum = ioc_rule->rulenum;
2295         rule->set = ioc_rule->set;
2296         rule->usr_flags = ioc_rule->usr_flags;
2297
2298         bcopy(ioc_rule->cmd, rule->cmd, rule->cmd_len * 4 /* XXX */);
2299
2300         rule->refcnt = 1;
2301
2302         return rule;
2303 }
2304
2305 /*
2306  * Add a new rule to the list.  Copy the rule into a malloc'ed area,
2307  * then possibly create a rule number and add the rule to the list.
2308  * Update the rule_number in the input struct so the caller knows
2309  * it as well.
2310  */
2311 static void
2312 ipfw_add_rule(struct ip_fw **head, struct ipfw_ioc_rule *ioc_rule)
2313 {
2314         struct ip_fw *rule, *f, *prev;
2315
2316         KKASSERT(*head != NULL);
2317         IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
2318
2319         ++static_gen;
2320
2321         rule = ipfw_create_rule(ioc_rule);
2322
2323         crit_enter();
2324
2325         /*
2326          * If rulenum is 0, find highest numbered rule before the
2327          * default rule, and add rule number incremental step
2328          */
2329         if (rule->rulenum == 0) {
2330                 int step = autoinc_step;
2331
2332                 KKASSERT(step >= IPFW_AUTOINC_STEP_MIN &&
2333                          step <= IPFW_AUTOINC_STEP_MAX);
2334
2335                 /*
2336                  * Locate the highest numbered rule before default
2337                  */
2338                 for (f = *head; f; f = f->next) {
2339                         if (f->rulenum == IPFW_DEFAULT_RULE)
2340                                 break;
2341                         rule->rulenum = f->rulenum;
2342                 }
2343                 if (rule->rulenum < IPFW_DEFAULT_RULE - step)
2344                         rule->rulenum += step;
2345
2346                 /* Update the input structure */
2347                 ioc_rule->rulenum = rule->rulenum;
2348         }
2349
2350         /*
2351          * Now insert the new rule in the right place in the sorted list.
2352          */
2353         for (prev = NULL, f = *head; f; prev = f, f = f->next) {
2354                 if (f->rulenum > rule->rulenum) {
2355                         /* Found the location */
2356                         if (prev) {
2357                                 rule->next = f;
2358                                 prev->next = rule;
2359                         } else {
2360                                 rule->next = *head;
2361                                 *head = rule;
2362                         }
2363                         break;
2364                 }
2365         }
2366
2367         flush_rule_ptrs();
2368         ipfw_inc_static_count(rule);
2369
2370         crit_exit();
2371
2372         DEB(kprintf("++ installed rule %d, static count now %d\n",
2373                 rule->rulenum, static_count);)
2374 }
2375
2376 /**
2377  * Free storage associated with a static rule (including derived
2378  * dynamic rules).
2379  * The caller is in charge of clearing rule pointers to avoid
2380  * dangling pointers.
2381  * @return a pointer to the next entry.
2382  * Arguments are not checked, so they better be correct.
2383  * Must be called at splimp().
2384  */
2385 static struct ip_fw *
2386 delete_rule(struct ip_fw **head, struct ip_fw *prev, struct ip_fw *rule)
2387 {
2388         struct ip_fw *n;
2389
2390         ++static_gen;
2391
2392         n = rule->next;
2393         remove_dyn_rule(rule, NULL /* force removal */);
2394         if (prev == NULL)
2395                 *head = n;
2396         else
2397                 prev->next = n;
2398         ipfw_dec_static_count(rule);
2399
2400         /* Mark the rule as invalid */
2401         rule->rule_flags |= IPFW_RULE_F_INVALID;
2402         rule->next_rule = NULL;
2403
2404         /* Try to free this rule */
2405         ipfw_free_rule(rule);
2406
2407         return n;
2408 }
2409
2410 /*
2411  * Deletes all rules from a chain (including the default rule
2412  * if the second argument is set).
2413  * Must be called at splimp().
2414  */
2415 static void
2416 free_chain(struct ip_fw **chain, int kill_default)
2417 {
2418         struct ip_fw *rule;
2419
2420         flush_rule_ptrs(); /* more efficient to do outside the loop */
2421
2422         while ((rule = *chain) != NULL &&
2423                (kill_default || rule->rulenum != IPFW_DEFAULT_RULE))
2424                 delete_rule(chain, NULL, rule);
2425
2426         KASSERT(dyn_count == 0, ("%u dyn rule remains\n", dyn_count));
2427
2428         if (kill_default) {
2429                 ip_fw_default_rule = NULL;      /* Reset default rule */
2430
2431                 if (ipfw_dyn_v != NULL) {
2432                         /*
2433                          * Free dynamic rules(state) hash table
2434                          */
2435                         kfree(ipfw_dyn_v, M_IPFW);
2436                         ipfw_dyn_v = NULL;
2437                 }
2438
2439                 KASSERT(static_count == 0,
2440                         ("%u static rules remains\n", static_count));
2441                 KASSERT(static_ioc_len == 0,
2442                         ("%u bytes of static rules remains\n", static_ioc_len));
2443         } else {
2444                 KASSERT(static_count == 1,
2445                         ("%u static rules remains\n", static_count));
2446                 KASSERT(static_ioc_len == IOC_RULESIZE(ip_fw_default_rule),
2447                         ("%u bytes of static rules remains, should be %u\n",
2448                          static_ioc_len, IOC_RULESIZE(ip_fw_default_rule)));
2449         }
2450 }
2451
2452 /**
2453  * Remove all rules with given number, and also do set manipulation.
2454  *
2455  * The argument is an uint32_t. The low 16 bit are the rule or set number,
2456  * the next 8 bits are the new set, the top 8 bits are the command:
2457  *
2458  *      0       delete rules with given number
2459  *      1       delete rules with given set number
2460  *      2       move rules with given number to new set
2461  *      3       move rules with given set number to new set
2462  *      4       swap sets with given numbers
2463  */
2464 static int
2465 del_entry(struct ip_fw **chain, uint32_t arg)
2466 {
2467         struct ip_fw *prev, *rule;
2468         uint16_t rulenum;
2469         uint8_t cmd, new_set;
2470
2471         rulenum = arg & 0xffff;
2472         cmd = (arg >> 24) & 0xff;
2473         new_set = (arg >> 16) & 0xff;
2474
2475         if (cmd > 4)
2476                 return EINVAL;
2477         if (new_set > 30)
2478                 return EINVAL;
2479         if (cmd == 0 || cmd == 2) {
2480                 if (rulenum == IPFW_DEFAULT_RULE)
2481                         return EINVAL;
2482         } else {
2483                 if (rulenum > 30)
2484                         return EINVAL;
2485         }
2486
2487         switch (cmd) {
2488         case 0: /* delete rules with given number */
2489                 /*
2490                  * locate first rule to delete
2491                  */
2492                 for (prev = NULL, rule = *chain;
2493                      rule && rule->rulenum < rulenum;
2494                      prev = rule, rule = rule->next)
2495                         ;
2496                 if (rule->rulenum != rulenum)
2497                         return EINVAL;
2498
2499                 crit_enter(); /* no access to rules while removing */
2500                 /*
2501                  * flush pointers outside the loop, then delete all matching
2502                  * rules. prev remains the same throughout the cycle.
2503                  */
2504                 flush_rule_ptrs();
2505                 while (rule && rule->rulenum == rulenum)
2506                         rule = delete_rule(chain, prev, rule);
2507                 crit_exit();
2508                 break;
2509
2510         case 1: /* delete all rules with given set number */
2511                 crit_enter();
2512                 flush_rule_ptrs();
2513                 for (prev = NULL, rule = *chain; rule;) {
2514                         if (rule->set == rulenum) {
2515                                 rule = delete_rule(chain, prev, rule);
2516                         } else {
2517                                 prev = rule;
2518                                 rule = rule->next;
2519                         }
2520                 }
2521                 crit_exit();
2522                 break;
2523
2524         case 2: /* move rules with given number to new set */
2525                 crit_enter();
2526                 for (rule = *chain; rule; rule = rule->next) {
2527                         if (rule->rulenum == rulenum)
2528                                 rule->set = new_set;
2529                 }
2530                 crit_exit();
2531                 break;
2532
2533         case 3: /* move rules with given set number to new set */
2534                 crit_enter();
2535                 for (rule = *chain; rule; rule = rule->next) {
2536                         if (rule->set == rulenum)
2537                                 rule->set = new_set;
2538                 }
2539                 crit_exit();
2540                 break;
2541
2542         case 4: /* swap two sets */
2543                 crit_enter();
2544                 for (rule = *chain; rule; rule = rule->next) {
2545                         if (rule->set == rulenum)
2546                                 rule->set = new_set;
2547                         else if (rule->set == new_set)
2548                                 rule->set = rulenum;
2549                 }
2550                 crit_exit();
2551                 break;
2552         }
2553         return 0;
2554 }
2555
2556 /*
2557  * Clear counters for a specific rule.
2558  */
2559 static void
2560 clear_counters(struct ip_fw *rule, int log_only)
2561 {
2562         ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
2563
2564         if (log_only == 0) {
2565                 rule->bcnt = rule->pcnt = 0;
2566                 rule->timestamp = 0;
2567         }
2568         if (l->o.opcode == O_LOG)
2569                 l->log_left = l->max_log;
2570 }
2571
2572 /**
2573  * Reset some or all counters on firewall rules.
2574  * @arg frwl is null to clear all entries, or contains a specific
2575  * rule number.
2576  * @arg log_only is 1 if we only want to reset logs, zero otherwise.
2577  */
2578 static int
2579 zero_entry(int rulenum, int log_only)
2580 {
2581         struct ip_fw *rule;
2582         char *msg;
2583
2584         if (rulenum == 0) {
2585                 crit_enter();
2586                 norule_counter = 0;
2587                 for (rule = layer3_chain; rule; rule = rule->next)
2588                         clear_counters(rule, log_only);
2589                 crit_exit();
2590                 msg = log_only ? "ipfw: All logging counts reset.\n"
2591                                : "ipfw: Accounting cleared.\n";
2592         } else {
2593                 int cleared = 0;
2594
2595                 /*
2596                  * We can have multiple rules with the same number, so we
2597                  * need to clear them all.
2598                  */
2599                 for (rule = layer3_chain; rule; rule = rule->next) {
2600                         if (rule->rulenum == rulenum) {
2601                                 crit_enter();
2602                                 while (rule && rule->rulenum == rulenum) {
2603                                         clear_counters(rule, log_only);
2604                                         rule = rule->next;
2605                                 }
2606                                 crit_exit();
2607                                 cleared = 1;
2608                                 break;
2609                         }
2610                 }
2611                 if (!cleared)   /* we did not find any matching rules */
2612                         return (EINVAL);
2613                 msg = log_only ? "ipfw: Entry %d logging count reset.\n"
2614                                : "ipfw: Entry %d cleared.\n";
2615         }
2616         if (fw_verbose)
2617                 log(LOG_SECURITY | LOG_NOTICE, msg, rulenum);
2618         return (0);
2619 }
2620
2621 /*
2622  * Check validity of the structure before insert.
2623  * Fortunately rules are simple, so this mostly need to check rule sizes.
2624  */
2625 static int
2626 ipfw_ctl_check_rule(struct ipfw_ioc_rule *rule, int size)
2627 {
2628         int l, cmdlen = 0;
2629         int have_action = 0;
2630         ipfw_insn *cmd;
2631
2632         /* Check for valid size */
2633         if (size < sizeof(*rule)) {
2634                 kprintf("ipfw: rule too short\n");
2635                 return EINVAL;
2636         }
2637         l = IOC_RULESIZE(rule);
2638         if (l != size) {
2639                 kprintf("ipfw: size mismatch (have %d want %d)\n", size, l);
2640                 return EINVAL;
2641         }
2642
2643         /*
2644          * Now go for the individual checks. Very simple ones, basically only
2645          * instruction sizes.
2646          */
2647         for (l = rule->cmd_len, cmd = rule->cmd; l > 0;
2648              l -= cmdlen, cmd += cmdlen) {
2649                 cmdlen = F_LEN(cmd);
2650                 if (cmdlen > l) {
2651                         kprintf("ipfw: opcode %d size truncated\n",
2652                                 cmd->opcode);
2653                         return EINVAL;
2654                 }
2655                 DEB(kprintf("ipfw: opcode %d\n", cmd->opcode);)
2656                 switch (cmd->opcode) {
2657                 case O_NOP:
2658                 case O_PROBE_STATE:
2659                 case O_KEEP_STATE:
2660                 case O_PROTO:
2661                 case O_IP_SRC_ME:
2662                 case O_IP_DST_ME:
2663                 case O_LAYER2:
2664                 case O_IN:
2665                 case O_FRAG:
2666                 case O_IPOPT:
2667                 case O_IPLEN:
2668                 case O_IPID:
2669                 case O_IPTOS:
2670                 case O_IPPRECEDENCE:
2671                 case O_IPTTL:
2672                 case O_IPVER:
2673                 case O_TCPWIN:
2674                 case O_TCPFLAGS:
2675                 case O_TCPOPTS:
2676                 case O_ESTAB:
2677                         if (cmdlen != F_INSN_SIZE(ipfw_insn))
2678                                 goto bad_size;
2679                         break;
2680
2681                 case O_UID:
2682                 case O_GID:
2683                 case O_IP_SRC:
2684                 case O_IP_DST:
2685                 case O_TCPSEQ:
2686                 case O_TCPACK:
2687                 case O_PROB:
2688                 case O_ICMPTYPE:
2689                         if (cmdlen != F_INSN_SIZE(ipfw_insn_u32))
2690                                 goto bad_size;
2691                         break;
2692
2693                 case O_LIMIT:
2694                         if (cmdlen != F_INSN_SIZE(ipfw_insn_limit))
2695                                 goto bad_size;
2696                         break;
2697
2698                 case O_LOG:
2699                         if (cmdlen != F_INSN_SIZE(ipfw_insn_log))
2700                                 goto bad_size;
2701
2702                         ((ipfw_insn_log *)cmd)->log_left =
2703                             ((ipfw_insn_log *)cmd)->max_log;
2704
2705                         break;
2706
2707                 case O_IP_SRC_MASK:
2708                 case O_IP_DST_MASK:
2709                         if (cmdlen != F_INSN_SIZE(ipfw_insn_ip))
2710                                 goto bad_size;
2711                         if (((ipfw_insn_ip *)cmd)->mask.s_addr == 0) {
2712                                 kprintf("ipfw: opcode %d, useless rule\n",
2713                                         cmd->opcode);
2714                                 return EINVAL;
2715                         }
2716                         break;
2717
2718                 case O_IP_SRC_SET:
2719                 case O_IP_DST_SET:
2720                         if (cmd->arg1 == 0 || cmd->arg1 > 256) {
2721                                 kprintf("ipfw: invalid set size %d\n",
2722                                         cmd->arg1);
2723                                 return EINVAL;
2724                         }
2725                         if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
2726                             (cmd->arg1+31)/32 )
2727                                 goto bad_size;
2728                         break;
2729
2730                 case O_MACADDR2:
2731                         if (cmdlen != F_INSN_SIZE(ipfw_insn_mac))
2732                                 goto bad_size;
2733                         break;
2734
2735                 case O_MAC_TYPE:
2736                 case O_IP_SRCPORT:
2737                 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */
2738                         if (cmdlen < 2 || cmdlen > 31)
2739                                 goto bad_size;
2740                         break;
2741
2742                 case O_RECV:
2743                 case O_XMIT:
2744                 case O_VIA:
2745                         if (cmdlen != F_INSN_SIZE(ipfw_insn_if))
2746                                 goto bad_size;
2747                         break;
2748
2749                 case O_PIPE:
2750                 case O_QUEUE:
2751                         if (cmdlen != F_INSN_SIZE(ipfw_insn_pipe))
2752                                 goto bad_size;
2753                         goto check_action;
2754
2755                 case O_FORWARD_IP:
2756                         if (cmdlen != F_INSN_SIZE(ipfw_insn_sa))
2757                                 goto bad_size;
2758                         goto check_action;
2759
2760                 case O_FORWARD_MAC: /* XXX not implemented yet */
2761                 case O_CHECK_STATE:
2762                 case O_COUNT:
2763                 case O_ACCEPT:
2764                 case O_DENY:
2765                 case O_REJECT:
2766                 case O_SKIPTO:
2767                 case O_DIVERT:
2768                 case O_TEE:
2769                         if (cmdlen != F_INSN_SIZE(ipfw_insn))
2770                                 goto bad_size;
2771 check_action:
2772                         if (have_action) {
2773                                 kprintf("ipfw: opcode %d, multiple actions"
2774                                         " not allowed\n",
2775                                         cmd->opcode);
2776                                 return EINVAL;
2777                         }
2778                         have_action = 1;
2779                         if (l != cmdlen) {
2780                                 kprintf("ipfw: opcode %d, action must be"
2781                                         " last opcode\n",
2782                                         cmd->opcode);
2783                                 return EINVAL;
2784                         }
2785                         break;
2786                 default:
2787                         kprintf("ipfw: opcode %d, unknown opcode\n",
2788                                 cmd->opcode);
2789                         return EINVAL;
2790                 }
2791         }
2792         if (have_action == 0) {
2793                 kprintf("ipfw: missing action\n");
2794                 return EINVAL;
2795         }
2796         return 0;
2797
2798 bad_size:
2799         kprintf("ipfw: opcode %d size %d wrong\n",
2800                 cmd->opcode, cmdlen);
2801         return EINVAL;
2802 }
2803
2804 static int
2805 ipfw_ctl_add_rule(struct sockopt *sopt)
2806 {
2807         struct ipfw_ioc_rule *ioc_rule;
2808         size_t size;
2809         int error;
2810         
2811         size = sopt->sopt_valsize;
2812         if (size > (sizeof(uint32_t) * IPFW_RULE_SIZE_MAX) ||
2813             size < sizeof(*ioc_rule)) {
2814                 return EINVAL;
2815         }
2816         if (size != (sizeof(uint32_t) * IPFW_RULE_SIZE_MAX)) {
2817                 sopt->sopt_val = krealloc(sopt->sopt_val, sizeof(uint32_t) *
2818                                           IPFW_RULE_SIZE_MAX, M_TEMP, M_WAITOK);
2819         }
2820         ioc_rule = sopt->sopt_val;
2821
2822         error = ipfw_ctl_check_rule(ioc_rule, size);
2823         if (error)
2824                 return error;
2825
2826         ipfw_add_rule(&layer3_chain, ioc_rule);
2827
2828         if (sopt->sopt_dir == SOPT_GET)
2829                 sopt->sopt_valsize = IOC_RULESIZE(ioc_rule);
2830         return 0;
2831 }
2832
2833 static void *
2834 ipfw_copy_rule(const struct ip_fw *rule, struct ipfw_ioc_rule *ioc_rule)
2835 {
2836         ioc_rule->act_ofs = rule->act_ofs;
2837         ioc_rule->cmd_len = rule->cmd_len;
2838         ioc_rule->rulenum = rule->rulenum;
2839         ioc_rule->set = rule->set;
2840         ioc_rule->usr_flags = rule->usr_flags;
2841
2842         ioc_rule->set_disable = set_disable;
2843         ioc_rule->static_count = static_count;
2844         ioc_rule->static_len = static_ioc_len;
2845
2846         ioc_rule->pcnt = rule->pcnt;
2847         ioc_rule->bcnt = rule->bcnt;
2848         ioc_rule->timestamp = rule->timestamp;
2849
2850         bcopy(rule->cmd, ioc_rule->cmd, ioc_rule->cmd_len * 4 /* XXX */);
2851
2852         return ((uint8_t *)ioc_rule + IOC_RULESIZE(ioc_rule));
2853 }
2854
2855 static void
2856 ipfw_copy_state(const ipfw_dyn_rule *dyn_rule,
2857                 struct ipfw_ioc_state *ioc_state)
2858 {
2859         const struct ipfw_flow_id *id;
2860         struct ipfw_ioc_flowid *ioc_id;
2861
2862         ioc_state->expire = TIME_LEQ(dyn_rule->expire, time_second) ?
2863                             0 : dyn_rule->expire - time_second;
2864         ioc_state->pcnt = dyn_rule->pcnt;
2865         ioc_state->bcnt = dyn_rule->bcnt;
2866
2867         ioc_state->dyn_type = dyn_rule->dyn_type;
2868         ioc_state->count = dyn_rule->count;
2869
2870         ioc_state->rulenum = dyn_rule->rule->rulenum;
2871
2872         id = &dyn_rule->id;
2873         ioc_id = &ioc_state->id;
2874
2875         ioc_id->type = ETHERTYPE_IP;
2876         ioc_id->u.ip.dst_ip = id->dst_ip;
2877         ioc_id->u.ip.src_ip = id->src_ip;
2878         ioc_id->u.ip.dst_port = id->dst_port;
2879         ioc_id->u.ip.src_port = id->src_port;
2880         ioc_id->u.ip.proto = id->proto;
2881 }
2882
2883 static int
2884 ipfw_ctl_get_rules(struct sockopt *sopt)
2885 {
2886         struct ip_fw *rule;
2887         void *bp;
2888         size_t size;
2889         uint32_t dcount = 0;
2890
2891         /*
2892          * pass up a copy of the current rules. Static rules
2893          * come first (the last of which has number IPFW_DEFAULT_RULE),
2894          * followed by a possibly empty list of dynamic rule.
2895          */
2896         crit_enter();
2897
2898         size = static_ioc_len;  /* size of static rules */
2899         if (ipfw_dyn_v) {       /* add size of dyn.rules */
2900                 dcount = dyn_count;
2901                 size += dcount * sizeof(struct ipfw_ioc_state);
2902         }
2903
2904         if (sopt->sopt_valsize < size) {
2905                 /* short length, no need to return incomplete rules */
2906                 /* XXX: if superuser, no need to zero buffer */
2907                 bzero(sopt->sopt_val, sopt->sopt_valsize); 
2908                 return 0;
2909         }
2910         bp = sopt->sopt_val;
2911
2912         for (rule = layer3_chain; rule; rule = rule->next)
2913                 bp = ipfw_copy_rule(rule, bp);
2914
2915         if (ipfw_dyn_v && dcount != 0) {
2916                 struct ipfw_ioc_state *ioc_state = bp;
2917                 uint32_t dcount2 = 0;
2918 #ifdef INVARIANTS
2919                 size_t old_size = size;
2920 #endif
2921                 int i;
2922
2923                 lockmgr(&dyn_lock, LK_SHARED);
2924                 for (i = 0; i < curr_dyn_buckets; i++) {
2925                         ipfw_dyn_rule *p;
2926
2927                         /*
2928                          * The # of dynamic rules may have grown after the
2929                          * snapshot of 'dyn_count' was taken, so we will have
2930                          * to check 'dcount' (snapshot of dyn_count) here to
2931                          * make sure that we don't overflow the pre-allocated
2932                          * buffer.
2933                          */
2934                         for (p = ipfw_dyn_v[i]; p != NULL && dcount != 0;
2935                              p = p->next, ioc_state++, dcount--, dcount2++)
2936                                 ipfw_copy_state(p, ioc_state);
2937                 }
2938                 lockmgr(&dyn_lock, LK_RELEASE);
2939
2940                 /*
2941                  * The # of dynamic rules may be shrinked after the
2942                  * snapshot of 'dyn_count' was taken.  To give user a
2943                  * correct dynamic rule count, we use the 'dcount2'
2944                  * calculated above (with shared lockmgr lock held).
2945                  */
2946                 size = static_ioc_len +
2947                        (dcount2 * sizeof(struct ipfw_ioc_state));
2948                 KKASSERT(size <= old_size);
2949         }
2950
2951         crit_exit();
2952
2953         sopt->sopt_valsize = size;
2954         return 0;
2955 }
2956
2957 /**
2958  * {set|get}sockopt parser.
2959  */
2960 static int
2961 ipfw_ctl(struct sockopt *sopt)
2962 {
2963         int error, rulenum;
2964         uint32_t *masks;
2965         size_t size;
2966
2967         error = 0;
2968
2969         switch (sopt->sopt_name) {
2970         case IP_FW_GET:
2971                 error = ipfw_ctl_get_rules(sopt);
2972                 break;
2973
2974         case IP_FW_FLUSH:
2975                 /*
2976                  * Normally we cannot release the lock on each iteration.
2977                  * We could do it here only because we start from the head all
2978                  * the times so there is no risk of missing some entries.
2979                  * On the other hand, the risk is that we end up with
2980                  * a very inconsistent ruleset, so better keep the lock
2981                  * around the whole cycle.
2982                  *
2983                  * XXX this code can be improved by resetting the head of
2984                  * the list to point to the default rule, and then freeing
2985                  * the old list without the need for a lock.
2986                  */
2987
2988                 crit_enter();
2989                 free_chain(&layer3_chain, 0 /* keep default rule */);
2990                 crit_exit();
2991                 break;
2992
2993         case IP_FW_ADD:
2994                 error = ipfw_ctl_add_rule(sopt);
2995                 break;
2996
2997         case IP_FW_DEL:
2998                 /*
2999                  * IP_FW_DEL is used for deleting single rules or sets,
3000                  * and (ab)used to atomically manipulate sets. Argument size
3001                  * is used to distinguish between the two:
3002                  *    sizeof(uint32_t)
3003                  *      delete single rule or set of rules,
3004                  *      or reassign rules (or sets) to a different set.
3005                  *    2*sizeof(uint32_t)
3006                  *      atomic disable/enable sets.
3007                  *      first uint32_t contains sets to be disabled,
3008                  *      second uint32_t contains sets to be enabled.
3009                  */
3010                 masks = sopt->sopt_val;
3011                 size = sopt->sopt_valsize;
3012                 if (size == sizeof(*masks)) {
3013                         /*
3014                          * Delete or reassign static rule
3015                          */
3016                         error = del_entry(&layer3_chain, masks[0]);
3017                 } else if (size == (2 * sizeof(*masks))) {
3018                         /*
3019                          * Set enable/disable
3020                          */
3021                         crit_enter();
3022
3023                         set_disable =
3024                             (set_disable | masks[0]) & ~masks[1] &
3025                             ~(1 << 31); /* set 31 always enabled */
3026
3027                         crit_exit();
3028                 } else {
3029                         error = EINVAL;
3030                 }
3031                 break;
3032
3033         case IP_FW_ZERO:
3034         case IP_FW_RESETLOG: /* argument is an int, the rule number */
3035                 rulenum=0;
3036
3037                 if (sopt->sopt_val != 0) {
3038                     error = soopt_to_kbuf(sopt, &rulenum,
3039                             sizeof(int), sizeof(int));
3040                     if (error)
3041                         break;
3042                 }
3043                 error = zero_entry(rulenum, sopt->sopt_name == IP_FW_RESETLOG);
3044                 break;
3045
3046         default:
3047                 kprintf("ipfw_ctl invalid option %d\n", sopt->sopt_name);
3048                 error = EINVAL;
3049         }
3050         return error;
3051 }
3052
3053 /*
3054  * This procedure is only used to handle keepalives. It is invoked
3055  * every dyn_keepalive_period
3056  */
3057 static void
3058 ipfw_tick(void *dummy __unused)
3059 {
3060         time_t keep_alive;
3061         uint32_t gen;
3062         int i;
3063
3064         if (ipfw_dyn_v == NULL || dyn_count == 0)
3065                 goto done;
3066
3067         keep_alive = time_second;
3068
3069         lockmgr(&dyn_lock, LK_EXCLUSIVE);
3070 again:
3071         gen = dyn_buckets_gen;
3072         for (i = 0; i < curr_dyn_buckets; i++) {
3073                 ipfw_dyn_rule *q, *prev;
3074
3075                 for (prev = NULL, q = ipfw_dyn_v[i]; q != NULL;) {
3076                         uint32_t ack_rev, ack_fwd;
3077                         struct ipfw_flow_id id;
3078
3079                         if (q->dyn_type == O_LIMIT_PARENT)
3080                                 goto next;
3081
3082                         if (TIME_LEQ(q->expire, time_second)) {
3083                                 /* State expired */
3084                                 UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q);
3085                                 continue;
3086                         }
3087
3088                         /*
3089                          * Keep alive processing
3090                          */
3091
3092                         if (!dyn_keepalive)
3093                                 goto next;
3094                         if (q->id.proto != IPPROTO_TCP)
3095                                 goto next;
3096                         if ((q->state & BOTH_SYN) != BOTH_SYN)
3097                                 goto next;
3098                         if (TIME_LEQ(time_second + dyn_keepalive_interval,
3099                             q->expire))
3100                                 goto next;      /* too early */
3101                         if (q->keep_alive == keep_alive)
3102                                 goto next;      /* alreay done */
3103
3104                         /*
3105                          * Save necessary information, so that they could
3106                          * survive after possible blocking in send_pkt()
3107                          */
3108                         id = q->id;
3109                         ack_rev = q->ack_rev;
3110                         ack_fwd = q->ack_fwd;
3111
3112                         /* Sending has been started */
3113                         q->keep_alive = keep_alive;
3114
3115                         /* Release lock to avoid possible dead lock */
3116                         lockmgr(&dyn_lock, LK_RELEASE);
3117                         send_pkt(&id, ack_rev - 1, ack_fwd, TH_SYN);
3118                         send_pkt(&id, ack_fwd - 1, ack_rev, 0);
3119                         lockmgr(&dyn_lock, LK_EXCLUSIVE);
3120
3121                         if (gen != dyn_buckets_gen) {
3122                                 /*
3123                                  * Dyn bucket array has been changed during
3124                                  * the above two sending; reiterate.
3125                                  */
3126                                 goto again;
3127                         }
3128 next:
3129                         prev = q;
3130                         q = q->next;
3131                 }
3132         }
3133         lockmgr(&dyn_lock, LK_RELEASE);
3134 done:
3135         callout_reset(&ipfw_timeout_h, dyn_keepalive_period * hz,
3136                       ipfw_tick, NULL);
3137 }
3138
3139 static void
3140 ipfw_init_default_rule(struct ip_fw **head)
3141 {
3142         struct ip_fw *def_rule;
3143
3144         KKASSERT(*head == NULL);
3145
3146         def_rule = kmalloc(sizeof(*def_rule), M_IPFW, M_WAITOK | M_ZERO);
3147
3148         def_rule->act_ofs = 0;
3149         def_rule->rulenum = IPFW_DEFAULT_RULE;
3150         def_rule->cmd_len = 1;
3151         def_rule->set = 31;
3152
3153         def_rule->cmd[0].len = 1;
3154 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
3155         def_rule->cmd[0].opcode = O_ACCEPT;
3156 #else
3157         def_rule->cmd[0].opcode = O_DENY;
3158 #endif
3159
3160         def_rule->refcnt = 1;
3161
3162         *head = def_rule;
3163         ipfw_inc_static_count(def_rule);
3164
3165         /* Install the default rule */
3166         ip_fw_default_rule = def_rule;
3167 }
3168
3169 static int
3170 ipfw_sysctl_autoinc_step(SYSCTL_HANDLER_ARGS)
3171 {
3172         return sysctl_int_range(oidp, arg1, arg2, req,
3173                IPFW_AUTOINC_STEP_MIN, IPFW_AUTOINC_STEP_MAX);
3174 }
3175
3176 static int
3177 ipfw_sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS)
3178 {
3179         int error, value;
3180
3181         lockmgr(&dyn_lock, LK_EXCLUSIVE);
3182
3183         value = dyn_buckets;
3184         error = sysctl_handle_int(oidp, &value, 0, req);
3185         if (error || !req->newptr)
3186                 goto back;
3187
3188         /*
3189          * Make sure we have a power of 2 and
3190          * do not allow more than 64k entries.
3191          */
3192         error = EINVAL;
3193         if (value < 0 || value > 65536)
3194                 goto back;
3195         if ((value & (value - 1)) != 0)
3196                 goto back;
3197
3198         error = 0;
3199         dyn_buckets = value;
3200 back:
3201         lockmgr(&dyn_lock, LK_RELEASE);
3202         return error;
3203 }
3204
3205 static int
3206 ipfw_sysctl_dyn_fin(SYSCTL_HANDLER_ARGS)
3207 {
3208         return sysctl_int_range(oidp, arg1, arg2, req,
3209                                 1, dyn_keepalive_period - 1);
3210 }
3211
3212 static int
3213 ipfw_sysctl_dyn_rst(SYSCTL_HANDLER_ARGS)
3214 {
3215         return sysctl_int_range(oidp, arg1, arg2, req,
3216                                 1, dyn_keepalive_period - 1);
3217 }
3218
3219 static void
3220 ipfw_init_dispatch(struct netmsg *nmsg)
3221 {
3222         int error = 0;
3223
3224         crit_enter();
3225
3226         if (IPFW_LOADED) {
3227                 kprintf("IP firewall already loaded\n");
3228                 error = EEXIST;
3229                 goto reply;
3230         }
3231
3232         ip_fw_chk_ptr = ipfw_chk;
3233         ip_fw_ctl_ptr = ipfw_ctl;
3234         ip_fw_dn_io_ptr = ipfw_dummynet_io;
3235
3236         layer3_chain = NULL;
3237         ipfw_init_default_rule(&layer3_chain);
3238
3239         kprintf("ipfw2 initialized, divert %s, "
3240                 "rule-based forwarding enabled, default to %s, logging ",
3241 #ifdef IPDIVERT
3242                 "enabled",
3243 #else
3244                 "disabled",
3245 #endif
3246                 ip_fw_default_rule->cmd[0].opcode == O_ACCEPT ?
3247                 "accept" : "deny");
3248
3249 #ifdef IPFIREWALL_VERBOSE
3250         fw_verbose = 1;
3251 #endif
3252 #ifdef IPFIREWALL_VERBOSE_LIMIT
3253         verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
3254 #endif
3255         if (fw_verbose == 0) {
3256                 kprintf("disabled\n");
3257         } else if (verbose_limit == 0) {
3258                 kprintf("unlimited\n");
3259         } else {
3260                 kprintf("limited to %d packets/entry by default\n",
3261                         verbose_limit);
3262         }
3263
3264         callout_init(&ipfw_timeout_h);
3265         lockinit(&dyn_lock, "ipfw_dyn", 0, 0);
3266
3267         ip_fw_loaded = 1;
3268         callout_reset(&ipfw_timeout_h, hz, ipfw_tick, NULL);
3269 reply:
3270         crit_exit();
3271         lwkt_replymsg(&nmsg->nm_lmsg, error);
3272 }
3273
3274 static int
3275 ipfw_init(void)
3276 {
3277         struct netmsg smsg;
3278
3279         netmsg_init(&smsg, &curthread->td_msgport, 0, ipfw_init_dispatch);
3280         return lwkt_domsg(IPFW_CFGPORT, &smsg.nm_lmsg, 0);
3281 }
3282
3283 #ifdef KLD_MODULE
3284
3285 static void
3286 ipfw_fini_dispatch(struct netmsg *nmsg)
3287 {
3288         int error = 0;
3289
3290         crit_enter();
3291
3292         if (ipfw_refcnt != 0) {
3293                 error = EBUSY;
3294                 goto reply;
3295         }
3296
3297         callout_stop(&ipfw_timeout_h);
3298
3299         ip_fw_loaded = 0;
3300         netmsg_service_sync();
3301
3302         ip_fw_chk_ptr = NULL;
3303         ip_fw_ctl_ptr = NULL;
3304         ip_fw_dn_io_ptr = NULL;
3305         free_chain(&layer3_chain, 1 /* kill default rule */);
3306
3307         kprintf("IP firewall unloaded\n");
3308 reply:
3309         crit_exit();
3310         lwkt_replymsg(&nmsg->nm_lmsg, error);
3311 }
3312
3313 static int
3314 ipfw_fini(void)
3315 {
3316         struct netmsg smsg;
3317
3318         netmsg_init(&smsg, &curthread->td_msgport, 0, ipfw_fini_dispatch);
3319         return lwkt_domsg(IPFW_CFGPORT, &smsg.nm_lmsg, 0);
3320 }
3321
3322 #endif  /* KLD_MODULE */
3323
3324 static int
3325 ipfw_modevent(module_t mod, int type, void *unused)
3326 {
3327         int err = 0;
3328
3329         switch (type) {
3330         case MOD_LOAD:
3331                 err = ipfw_init();
3332                 break;
3333
3334         case MOD_UNLOAD:
3335 #ifndef KLD_MODULE
3336                 kprintf("ipfw statically compiled, cannot unload\n");
3337                 err = EBUSY;
3338 #else
3339                 err = ipfw_fini();
3340 #endif
3341                 break;
3342         default:
3343                 break;
3344         }
3345         return err;
3346 }
3347
3348 static moduledata_t ipfwmod = {
3349         "ipfw",
3350         ipfw_modevent,
3351         0
3352 };
3353 DECLARE_MODULE(ipfw, ipfwmod, SI_SUB_PROTO_END, SI_ORDER_ANY);
3354 MODULE_VERSION(ipfw, 1);