2 * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
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.86 2008/09/16 11:28:31 sephe Exp $
33 * Implement IP packet firewall (new version)
39 #error IPFIREWALL requires INET.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <sys/thread2.h>
53 #include <sys/ucred.h>
54 #include <sys/in_cksum.h>
58 #include <net/route.h>
59 #include <net/netmsg2.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/ip_icmp.h>
70 #include <net/dummynet/ip_dummynet.h>
71 #include <netinet/tcp.h>
72 #include <netinet/tcp_timer.h>
73 #include <netinet/tcp_var.h>
74 #include <netinet/tcpip.h>
75 #include <netinet/udp.h>
76 #include <netinet/udp_var.h>
77 #include <netinet/ip_divert.h>
79 #include <netinet/if_ether.h> /* XXX for ETHERTYPE_IP */
82 * Description about per-CPU rule duplication:
84 * Module loading/unloading and all ioctl operations are serialized
85 * by netisr0, so we don't have any ordering or locking problems.
87 * Following graph shows how operation on per-CPU rule list is
88 * performed [2 CPU case]:
92 * netisr0 <------------------------------------+
103 * forwardmsg---------->ifnet1 |
108 * replymsg--------------+
113 * Rules which will not create states (dyn rules) [2 CPU case]
116 * layer3_chain layer3_chain
119 * +-------+ sibling +-------+ sibling
120 * | rule1 |--------->| rule1 |--------->NULL
121 * +-------+ +-------+
125 * +-------+ sibling +-------+ sibling
126 * | rule2 |--------->| rule2 |--------->NULL
127 * +-------+ +-------+
130 * 1) Ease statistics calculation during IP_FW_GET. We only need to
131 * iterate layer3_chain on CPU0; the current rule's duplication on
132 * the other CPUs could safely be read-only accessed by using
134 * 2) Accelerate rule insertion and deletion, e.g. rule insertion:
135 * a) In netisr0 (on CPU0) rule3 is determined to be inserted between
136 * rule1 and rule2. To make this decision we need to iterate the
137 * layer3_chain on CPU0. The netmsg, which is used to insert the
138 * rule, will contain rule1 on CPU0 as prev_rule and rule2 on CPU0
140 * b) After the insertion on CPU0 is done, we will move on to CPU1.
141 * But instead of relocating the rule3's position on CPU1 by
142 * iterating the layer3_chain on CPU1, we set the netmsg's prev_rule
143 * to rule1->sibling and next_rule to rule2->sibling before the
144 * netmsg is forwarded to CPU1 from CPU0
148 * Rules which will create states (dyn rules) [2 CPU case]
149 * (unnecessary parts are omitted; they are same as in the previous figure)
153 * +-------+ +-------+
154 * | rule1 | | rule1 |
155 * +-------+ +-------+
162 * | +--------------------+ |
164 * | | (read-only shared) | |
166 * | | back pointer array | |
167 * | | (indexed by cpuid) | |
169 * +----|---------[0] | |
170 * | [1]--------|----+
172 * +--------------------+
175 * ........|............|............
179 * : +---------+ +---------+ :
180 * : | state1a | | state1b | .... :
181 * : +---------+ +---------+ :
185 * : (protected by dyn_lock) :
186 * ..................................
188 * [state1a and state1b are states created by rule1]
191 * This structure is introduced so that shared (locked) state table could
192 * work with per-CPU (duplicated) static rules. It mainly bridges states
193 * and static rules and serves as static rule's place holder (a read-only
194 * shared part of duplicated rules) from states point of view.
196 * IPFW_RULE_F_STATE (only for rules which create states):
197 * o During rule installation, this flag is turned on after rule's
198 * duplications reach all CPUs, to avoid at least following race:
199 * 1) rule1 is duplicated on CPU0 and is not duplicated on CPU1 yet
200 * 2) rule1 creates state1
201 * 3) state1 is located on CPU1 by check-state
202 * But rule1 is not duplicated on CPU1 yet
203 * o During rule deletion, this flag is turned off before deleting states
204 * created by the rule and before deleting the rule itself, so no
205 * more states will be created by the to-be-deleted rule even when its
206 * duplication on certain CPUs are not eliminated yet.
209 #define IPFW_AUTOINC_STEP_MIN 1
210 #define IPFW_AUTOINC_STEP_MAX 1000
211 #define IPFW_AUTOINC_STEP_DEF 100
213 #define IPFW_DEFAULT_RULE 65535 /* rulenum for the default rule */
214 #define IPFW_DEFAULT_SET 31 /* set number for the default rule */
218 const struct ipfw_ioc_rule *ioc_rule;
219 struct ip_fw *next_rule;
220 struct ip_fw *prev_rule;
221 struct ip_fw *sibling;
222 struct ip_fw_stub *stub;
227 struct ip_fw *start_rule;
228 struct ip_fw *prev_rule;
236 struct ip_fw *start_rule;
241 struct ipfw_context {
242 struct ip_fw *ipfw_layer3_chain; /* list of rules for layer3 */
243 struct ip_fw *ipfw_default_rule; /* default rule */
244 uint64_t ipfw_norule_counter; /* counter for ipfw_log(NULL) */
247 * ipfw_set_disable contains one bit per set value (0..31).
248 * If the bit is set, all rules with the corresponding set
249 * are disabled. Set IPDW_DEFAULT_SET is reserved for the
250 * default rule and CANNOT be disabled.
252 uint32_t ipfw_set_disable;
253 uint32_t ipfw_gen; /* generation of rule list */
256 static struct ipfw_context *ipfw_ctx[MAXCPU];
260 * Module can not be unloaded, if there are references to
261 * certains rules of ipfw(4), e.g. dummynet(4)
263 static int ipfw_refcnt;
266 MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");
269 * Following two global variables are accessed and
270 * updated only on CPU0
272 static uint32_t static_count; /* # of static rules */
273 static uint32_t static_ioc_len; /* bytes of static rules */
276 * If 1, then ipfw static rules are being flushed,
277 * ipfw_chk() will skip to the default rule.
279 static int ipfw_flushing;
281 static int fw_verbose;
282 static int verbose_limit;
284 static int fw_debug = 1;
285 static int autoinc_step = IPFW_AUTOINC_STEP_DEF;
287 static int ipfw_sysctl_enable(SYSCTL_HANDLER_ARGS);
288 static int ipfw_sysctl_autoinc_step(SYSCTL_HANDLER_ARGS);
289 static int ipfw_sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS);
290 static int ipfw_sysctl_dyn_fin(SYSCTL_HANDLER_ARGS);
291 static int ipfw_sysctl_dyn_rst(SYSCTL_HANDLER_ARGS);
293 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
294 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, enable, CTLTYPE_INT | CTLFLAG_RW,
295 &fw_enable, 0, ipfw_sysctl_enable, "I", "Enable ipfw");
296 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, autoinc_step, CTLTYPE_INT | CTLFLAG_RW,
297 &autoinc_step, 0, ipfw_sysctl_autoinc_step, "I",
298 "Rule number autincrement step");
299 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO,one_pass,CTLFLAG_RW,
301 "Only do a single pass through ipfw when using dummynet(4)");
302 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, debug, CTLFLAG_RW,
303 &fw_debug, 0, "Enable printing of debug ip_fw statements");
304 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose, CTLFLAG_RW,
305 &fw_verbose, 0, "Log matches to ipfw rules");
306 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, CTLFLAG_RW,
307 &verbose_limit, 0, "Set upper limit of matches of ipfw rules logged");
310 * Description of dynamic rules.
312 * Dynamic rules are stored in lists accessed through a hash table
313 * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
314 * be modified through the sysctl variable dyn_buckets which is
315 * updated when the table becomes empty.
317 * XXX currently there is only one list, ipfw_dyn.
319 * When a packet is received, its address fields are first masked
320 * with the mask defined for the rule, then hashed, then matched
321 * against the entries in the corresponding list.
322 * Dynamic rules can be used for different purposes:
324 * + enforcing limits on the number of sessions;
325 * + in-kernel NAT (not implemented yet)
327 * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
328 * measured in seconds and depending on the flags.
330 * The total number of dynamic rules is stored in dyn_count.
331 * The max number of dynamic rules is dyn_max. When we reach
332 * the maximum number of rules we do not create anymore. This is
333 * done to avoid consuming too much memory, but also too much
334 * time when searching on each packet (ideally, we should try instead
335 * to put a limit on the length of the list on each bucket...).
337 * Each dynamic rule holds a pointer to the parent ipfw rule so
338 * we know what action to perform. Dynamic rules are removed when
339 * the parent rule is deleted. XXX we should make them survive.
341 * There are some limitations with dynamic rules -- we do not
342 * obey the 'randomized match', and we do not do multiple
343 * passes through the firewall. XXX check the latter!!!
345 * NOTE about the SHARED LOCKMGR LOCK during dynamic rule looking up:
346 * Only TCP state transition will change dynamic rule's state and ack
347 * sequences, while all packets of one TCP connection only goes through
348 * one TCP thread, so it is safe to use shared lockmgr lock during dynamic
349 * rule looking up. The keep alive callout uses exclusive lockmgr lock
350 * when it tries to find suitable dynamic rules to send keep alive, so
351 * it will not see half updated state and ack sequences. Though the expire
352 * field updating looks racy for other protocols, the resolution (second)
353 * of expire field makes this kind of race harmless.
354 * XXX statistics' updating is _not_ MPsafe!!!
355 * XXX once UDP output path is fixed, we could use lockless dynamic rule
358 static ipfw_dyn_rule **ipfw_dyn_v = NULL;
359 static uint32_t dyn_buckets = 256; /* must be power of 2 */
360 static uint32_t curr_dyn_buckets = 256; /* must be power of 2 */
361 static uint32_t dyn_buckets_gen; /* generation of dyn buckets array */
362 static struct lock dyn_lock; /* dynamic rules' hash table lock */
363 static struct callout ipfw_timeout_h;
366 * Timeouts for various events in handing dynamic rules.
368 static uint32_t dyn_ack_lifetime = 300;
369 static uint32_t dyn_syn_lifetime = 20;
370 static uint32_t dyn_fin_lifetime = 1;
371 static uint32_t dyn_rst_lifetime = 1;
372 static uint32_t dyn_udp_lifetime = 10;
373 static uint32_t dyn_short_lifetime = 5;
376 * Keepalives are sent if dyn_keepalive is set. They are sent every
377 * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
378 * seconds of lifetime of a rule.
379 * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
380 * than dyn_keepalive_period.
383 static uint32_t dyn_keepalive_interval = 20;
384 static uint32_t dyn_keepalive_period = 5;
385 static uint32_t dyn_keepalive = 1; /* do send keepalives */
387 static uint32_t dyn_count; /* # of dynamic rules */
388 static uint32_t dyn_max = 4096; /* max # of dynamic rules */
390 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_buckets, CTLTYPE_INT | CTLFLAG_RW,
391 &dyn_buckets, 0, ipfw_sysctl_dyn_buckets, "I", "Number of dyn. buckets");
392 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets, CTLFLAG_RD,
393 &curr_dyn_buckets, 0, "Current Number of dyn. buckets");
394 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_count, CTLFLAG_RD,
395 &dyn_count, 0, "Number of dyn. rules");
396 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_max, CTLFLAG_RW,
397 &dyn_max, 0, "Max number of dyn. rules");
398 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count, CTLFLAG_RD,
399 &static_count, 0, "Number of static rules");
400 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime, CTLFLAG_RW,
401 &dyn_ack_lifetime, 0, "Lifetime of dyn. rules for acks");
402 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime, CTLFLAG_RW,
403 &dyn_syn_lifetime, 0, "Lifetime of dyn. rules for syn");
404 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime,
405 CTLTYPE_INT | CTLFLAG_RW, &dyn_fin_lifetime, 0, ipfw_sysctl_dyn_fin, "I",
406 "Lifetime of dyn. rules for fin");
407 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime,
408 CTLTYPE_INT | CTLFLAG_RW, &dyn_rst_lifetime, 0, ipfw_sysctl_dyn_rst, "I",
409 "Lifetime of dyn. rules for rst");
410 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime, CTLFLAG_RW,
411 &dyn_udp_lifetime, 0, "Lifetime of dyn. rules for UDP");
412 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime, CTLFLAG_RW,
413 &dyn_short_lifetime, 0, "Lifetime of dyn. rules for other situations");
414 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_keepalive, CTLFLAG_RW,
415 &dyn_keepalive, 0, "Enable keepalives for dyn. rules");
417 static ip_fw_chk_t ipfw_chk;
420 ipfw_free_rule(struct ip_fw *rule)
422 KASSERT(rule->cpuid == mycpuid, ("rule freed on cpu%d\n", mycpuid));
423 KASSERT(rule->refcnt > 0, ("invalid refcnt %u\n", rule->refcnt));
425 if (rule->refcnt == 0) {
433 ipfw_unref_rule(void *priv)
435 ipfw_free_rule(priv);
437 atomic_subtract_int(&ipfw_refcnt, 1);
442 ipfw_ref_rule(struct ip_fw *rule)
444 KASSERT(rule->cpuid == mycpuid, ("rule used on cpu%d\n", mycpuid));
446 atomic_add_int(&ipfw_refcnt, 1);
452 * This macro maps an ip pointer into a layer3 header pointer of type T
454 #define L3HDR(T, ip) ((T *)((uint32_t *)(ip) + (ip)->ip_hl))
457 icmptype_match(struct ip *ip, ipfw_insn_u32 *cmd)
459 int type = L3HDR(struct icmp,ip)->icmp_type;
461 return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1 << type)));
464 #define TT ((1 << ICMP_ECHO) | \
465 (1 << ICMP_ROUTERSOLICIT) | \
466 (1 << ICMP_TSTAMP) | \
471 is_icmp_query(struct ip *ip)
473 int type = L3HDR(struct icmp, ip)->icmp_type;
475 return (type <= ICMP_MAXTYPE && (TT & (1 << type)));
481 * The following checks use two arrays of 8 or 16 bits to store the
482 * bits that we want set or clear, respectively. They are in the
483 * low and high half of cmd->arg1 or cmd->d[0].
485 * We scan options and store the bits we find set. We succeed if
487 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
489 * The code is sometimes optimized not to store additional variables.
493 flags_match(ipfw_insn *cmd, uint8_t bits)
498 if (((cmd->arg1 & 0xff) & bits) != 0)
499 return 0; /* some bits we want set were clear */
501 want_clear = (cmd->arg1 >> 8) & 0xff;
502 if ((want_clear & bits) != want_clear)
503 return 0; /* some bits we want clear were set */
508 ipopts_match(struct ip *ip, ipfw_insn *cmd)
510 int optlen, bits = 0;
511 u_char *cp = (u_char *)(ip + 1);
512 int x = (ip->ip_hl << 2) - sizeof(struct ip);
514 for (; x > 0; x -= optlen, cp += optlen) {
515 int opt = cp[IPOPT_OPTVAL];
517 if (opt == IPOPT_EOL)
520 if (opt == IPOPT_NOP) {
523 optlen = cp[IPOPT_OLEN];
524 if (optlen <= 0 || optlen > x)
525 return 0; /* invalid or truncated */
530 bits |= IP_FW_IPOPT_LSRR;
534 bits |= IP_FW_IPOPT_SSRR;
538 bits |= IP_FW_IPOPT_RR;
542 bits |= IP_FW_IPOPT_TS;
549 return (flags_match(cmd, bits));
553 tcpopts_match(struct ip *ip, ipfw_insn *cmd)
555 int optlen, bits = 0;
556 struct tcphdr *tcp = L3HDR(struct tcphdr,ip);
557 u_char *cp = (u_char *)(tcp + 1);
558 int x = (tcp->th_off << 2) - sizeof(struct tcphdr);
560 for (; x > 0; x -= optlen, cp += optlen) {
563 if (opt == TCPOPT_EOL)
566 if (opt == TCPOPT_NOP) {
576 bits |= IP_FW_TCPOPT_MSS;
580 bits |= IP_FW_TCPOPT_WINDOW;
583 case TCPOPT_SACK_PERMITTED:
585 bits |= IP_FW_TCPOPT_SACK;
588 case TCPOPT_TIMESTAMP:
589 bits |= IP_FW_TCPOPT_TS;
595 bits |= IP_FW_TCPOPT_CC;
602 return (flags_match(cmd, bits));
606 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd)
608 if (ifp == NULL) /* no iface with this packet, match fails */
611 /* Check by name or by IP address */
612 if (cmd->name[0] != '\0') { /* match by name */
615 if (kfnmatch(cmd->name, ifp->if_xname, 0) == 0)
618 if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
622 struct ifaddr_container *ifac;
624 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
625 struct ifaddr *ia = ifac->ifa;
627 if (ia->ifa_addr == NULL)
629 if (ia->ifa_addr->sa_family != AF_INET)
631 if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
632 (ia->ifa_addr))->sin_addr.s_addr)
633 return(1); /* match */
636 return(0); /* no match, fail ... */
639 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
642 * We enter here when we have a rule with O_LOG.
643 * XXX this function alone takes about 2Kbytes of code!
646 ipfw_log(struct ip_fw *f, u_int hlen, struct ether_header *eh,
647 struct mbuf *m, struct ifnet *oif)
650 int limit_reached = 0;
651 char action2[40], proto[48], fragment[28];
656 if (f == NULL) { /* bogus pkt */
657 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
659 if (verbose_limit != 0 &&
660 ctx->ipfw_norule_counter >= verbose_limit)
662 ctx->ipfw_norule_counter++;
663 if (ctx->ipfw_norule_counter == verbose_limit)
664 limit_reached = verbose_limit;
666 } else { /* O_LOG is the first action, find the real one */
667 ipfw_insn *cmd = ACTION_PTR(f);
668 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
670 if (l->max_log != 0 && l->log_left == 0)
673 if (l->log_left == 0)
674 limit_reached = l->max_log;
675 cmd += F_LEN(cmd); /* point to first action */
676 if (cmd->opcode == O_PROB)
680 switch (cmd->opcode) {
686 if (cmd->arg1==ICMP_REJECT_RST) {
688 } else if (cmd->arg1==ICMP_UNREACH_HOST) {
691 ksnprintf(SNPARGS(action2, 0), "Unreach %d",
705 ksnprintf(SNPARGS(action2, 0), "Divert %d", cmd->arg1);
709 ksnprintf(SNPARGS(action2, 0), "Tee %d", cmd->arg1);
713 ksnprintf(SNPARGS(action2, 0), "SkipTo %d", cmd->arg1);
717 ksnprintf(SNPARGS(action2, 0), "Pipe %d", cmd->arg1);
721 ksnprintf(SNPARGS(action2, 0), "Queue %d", cmd->arg1);
726 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
729 len = ksnprintf(SNPARGS(action2, 0),
731 inet_ntoa(sa->sa.sin_addr));
732 if (sa->sa.sin_port) {
733 ksnprintf(SNPARGS(action2, len), ":%d",
745 if (hlen == 0) { /* non-ip */
746 ksnprintf(SNPARGS(proto, 0), "MAC");
748 struct ip *ip = mtod(m, struct ip *);
749 /* these three are all aliases to the same thing */
750 struct icmp *const icmp = L3HDR(struct icmp, ip);
751 struct tcphdr *const tcp = (struct tcphdr *)icmp;
752 struct udphdr *const udp = (struct udphdr *)icmp;
754 int ip_off, offset, ip_len;
757 if (eh != NULL) { /* layer 2 packets are as on the wire */
758 ip_off = ntohs(ip->ip_off);
759 ip_len = ntohs(ip->ip_len);
764 offset = ip_off & IP_OFFMASK;
767 len = ksnprintf(SNPARGS(proto, 0), "TCP %s",
768 inet_ntoa(ip->ip_src));
770 ksnprintf(SNPARGS(proto, len), ":%d %s:%d",
771 ntohs(tcp->th_sport),
772 inet_ntoa(ip->ip_dst),
773 ntohs(tcp->th_dport));
775 ksnprintf(SNPARGS(proto, len), " %s",
776 inet_ntoa(ip->ip_dst));
781 len = ksnprintf(SNPARGS(proto, 0), "UDP %s",
782 inet_ntoa(ip->ip_src));
784 ksnprintf(SNPARGS(proto, len), ":%d %s:%d",
785 ntohs(udp->uh_sport),
786 inet_ntoa(ip->ip_dst),
787 ntohs(udp->uh_dport));
789 ksnprintf(SNPARGS(proto, len), " %s",
790 inet_ntoa(ip->ip_dst));
796 len = ksnprintf(SNPARGS(proto, 0),
801 len = ksnprintf(SNPARGS(proto, 0), "ICMP ");
803 len += ksnprintf(SNPARGS(proto, len), "%s",
804 inet_ntoa(ip->ip_src));
805 ksnprintf(SNPARGS(proto, len), " %s",
806 inet_ntoa(ip->ip_dst));
810 len = ksnprintf(SNPARGS(proto, 0), "P:%d %s", ip->ip_p,
811 inet_ntoa(ip->ip_src));
812 ksnprintf(SNPARGS(proto, len), " %s",
813 inet_ntoa(ip->ip_dst));
817 if (ip_off & (IP_MF | IP_OFFMASK)) {
818 ksnprintf(SNPARGS(fragment, 0), " (frag %d:%d@%d%s)",
819 ntohs(ip->ip_id), ip_len - (ip->ip_hl << 2),
820 offset << 3, (ip_off & IP_MF) ? "+" : "");
824 if (oif || m->m_pkthdr.rcvif) {
825 log(LOG_SECURITY | LOG_INFO,
826 "ipfw: %d %s %s %s via %s%s\n",
828 action, proto, oif ? "out" : "in",
829 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
832 log(LOG_SECURITY | LOG_INFO,
833 "ipfw: %d %s %s [no if info]%s\n",
835 action, proto, fragment);
839 log(LOG_SECURITY | LOG_NOTICE,
840 "ipfw: limit %d reached on entry %d\n",
841 limit_reached, f ? f->rulenum : -1);
848 * IMPORTANT: the hash function for dynamic rules must be commutative
849 * in source and destination (ip,port), because rules are bidirectional
850 * and we want to find both in the same bucket.
853 hash_packet(struct ipfw_flow_id *id)
857 i = (id->dst_ip) ^ (id->src_ip) ^ (id->dst_port) ^ (id->src_port);
858 i &= (curr_dyn_buckets - 1);
863 * unlink a dynamic rule from a chain. prev is a pointer to
864 * the previous one, q is a pointer to the rule to delete,
865 * head is a pointer to the head of the queue.
866 * Modifies q and potentially also head.
868 #define UNLINK_DYN_RULE(prev, head, q) \
870 ipfw_dyn_rule *old_q = q; \
872 /* remove a refcount to the parent */ \
873 if (q->dyn_type == O_LIMIT) \
874 q->parent->count--; \
875 DEB(kprintf("-- unlink entry 0x%08x %d -> 0x%08x %d, %d left\n", \
876 (q->id.src_ip), (q->id.src_port), \
877 (q->id.dst_ip), (q->id.dst_port), dyn_count-1 ); ) \
879 prev->next = q = q->next; \
881 head = q = q->next; \
882 KASSERT(dyn_count > 0, ("invalid dyn count %u\n", dyn_count)); \
884 kfree(old_q, M_IPFW); \
887 #define TIME_LEQ(a, b) ((int)((a) - (b)) <= 0)
890 * Remove dynamic rules pointing to "rule", or all of them if rule == NULL.
892 * If keep_me == NULL, rules are deleted even if not expired,
893 * otherwise only expired rules are removed.
895 * The value of the second parameter is also used to point to identify
896 * a rule we absolutely do not want to remove (e.g. because we are
897 * holding a reference to it -- this is the case with O_LIMIT_PARENT
898 * rules). The pointer is only used for comparison, so any non-null
902 remove_dyn_rule_locked(struct ip_fw *rule, ipfw_dyn_rule *keep_me)
904 static uint32_t last_remove = 0; /* XXX */
906 #define FORCE (keep_me == NULL)
908 ipfw_dyn_rule *prev, *q;
909 int i, pass = 0, max_pass = 0, unlinked = 0;
911 if (ipfw_dyn_v == NULL || dyn_count == 0)
913 /* do not expire more than once per second, it is useless */
914 if (!FORCE && last_remove == time_second)
916 last_remove = time_second;
919 * because O_LIMIT refer to parent rules, during the first pass only
920 * remove child and mark any pending LIMIT_PARENT, and remove
921 * them in a second pass.
924 for (i = 0; i < curr_dyn_buckets; i++) {
925 for (prev = NULL, q = ipfw_dyn_v[i]; q;) {
927 * Logic can become complex here, so we split tests.
931 if (rule != NULL && rule->stub != q->stub)
932 goto next; /* not the one we are looking for */
933 if (q->dyn_type == O_LIMIT_PARENT) {
935 * handle parent in the second pass,
936 * record we need one.
941 if (FORCE && q->count != 0) {
942 /* XXX should not happen! */
943 kprintf("OUCH! cannot remove rule, "
944 "count %d\n", q->count);
947 if (!FORCE && !TIME_LEQ(q->expire, time_second))
951 UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q);
958 if (pass++ < max_pass)
968 * lookup a dynamic rule.
970 static ipfw_dyn_rule *
971 lookup_dyn_rule(struct ipfw_flow_id *pkt, int *match_direction,
975 * stateful ipfw extensions.
976 * Lookup into dynamic session queue
978 #define MATCH_REVERSE 0
979 #define MATCH_FORWARD 1
981 #define MATCH_UNKNOWN 3
982 int i, dir = MATCH_NONE;
983 ipfw_dyn_rule *prev, *q=NULL;
985 if (ipfw_dyn_v == NULL)
986 goto done; /* not found */
988 i = hash_packet(pkt);
989 for (prev = NULL, q = ipfw_dyn_v[i]; q != NULL;) {
990 if (q->dyn_type == O_LIMIT_PARENT)
993 if (TIME_LEQ(q->expire, time_second)) {
995 * Entry expired; skip.
996 * Let ipfw_tick() take care of it
1001 if (pkt->proto == q->id.proto) {
1002 if (pkt->src_ip == q->id.src_ip &&
1003 pkt->dst_ip == q->id.dst_ip &&
1004 pkt->src_port == q->id.src_port &&
1005 pkt->dst_port == q->id.dst_port) {
1006 dir = MATCH_FORWARD;
1009 if (pkt->src_ip == q->id.dst_ip &&
1010 pkt->dst_ip == q->id.src_ip &&
1011 pkt->src_port == q->id.dst_port &&
1012 pkt->dst_port == q->id.src_port) {
1013 dir = MATCH_REVERSE;
1022 goto done; /* q = NULL, not found */
1024 if (pkt->proto == IPPROTO_TCP) { /* update state according to flags */
1025 u_char flags = pkt->flags & (TH_FIN|TH_SYN|TH_RST);
1027 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
1028 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
1030 q->state |= (dir == MATCH_FORWARD ) ? flags : (flags << 8);
1032 case TH_SYN: /* opening */
1033 q->expire = time_second + dyn_syn_lifetime;
1036 case BOTH_SYN: /* move to established */
1037 case BOTH_SYN | TH_FIN : /* one side tries to close */
1038 case BOTH_SYN | (TH_FIN << 8) :
1040 uint32_t ack = ntohl(tcp->th_ack);
1042 #define _SEQ_GE(a, b) ((int)(a) - (int)(b) >= 0)
1044 if (dir == MATCH_FORWARD) {
1045 if (q->ack_fwd == 0 ||
1046 _SEQ_GE(ack, q->ack_fwd))
1048 else /* ignore out-of-sequence */
1051 if (q->ack_rev == 0 ||
1052 _SEQ_GE(ack, q->ack_rev))
1054 else /* ignore out-of-sequence */
1059 q->expire = time_second + dyn_ack_lifetime;
1062 case BOTH_SYN | BOTH_FIN: /* both sides closed */
1063 KKASSERT(dyn_fin_lifetime < dyn_keepalive_period);
1064 q->expire = time_second + dyn_fin_lifetime;
1070 * reset or some invalid combination, but can also
1071 * occur if we use keep-state the wrong way.
1073 if ((q->state & ((TH_RST << 8) | TH_RST)) == 0)
1074 kprintf("invalid state: 0x%x\n", q->state);
1076 KKASSERT(dyn_rst_lifetime < dyn_keepalive_period);
1077 q->expire = time_second + dyn_rst_lifetime;
1080 } else if (pkt->proto == IPPROTO_UDP) {
1081 q->expire = time_second + dyn_udp_lifetime;
1083 /* other protocols */
1084 q->expire = time_second + dyn_short_lifetime;
1087 if (match_direction)
1088 *match_direction = dir;
1092 static struct ip_fw *
1093 lookup_rule(struct ipfw_flow_id *pkt, int *match_direction, struct tcphdr *tcp,
1094 uint16_t len, int *deny)
1096 struct ip_fw *rule = NULL;
1098 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
1102 gen = ctx->ipfw_gen;
1104 lockmgr(&dyn_lock, LK_SHARED);
1106 if (ctx->ipfw_gen != gen) {
1108 * Static rules had been change when we were waiting
1109 * for the dynamic hash table lock; deny this packet,
1110 * since it is _not_ known whether it is safe to keep
1111 * iterating the static rules.
1117 q = lookup_dyn_rule(pkt, match_direction, tcp);
1121 rule = q->stub->rule[mycpuid];
1122 KKASSERT(rule->stub == q->stub && rule->cpuid == mycpuid);
1129 lockmgr(&dyn_lock, LK_RELEASE);
1134 realloc_dynamic_table(void)
1136 ipfw_dyn_rule **old_dyn_v;
1137 uint32_t old_curr_dyn_buckets;
1139 KASSERT(dyn_buckets <= 65536 && (dyn_buckets & (dyn_buckets - 1)) == 0,
1140 ("invalid dyn_buckets %d\n", dyn_buckets));
1142 /* Save the current buckets array for later error recovery */
1143 old_dyn_v = ipfw_dyn_v;
1144 old_curr_dyn_buckets = curr_dyn_buckets;
1146 curr_dyn_buckets = dyn_buckets;
1148 ipfw_dyn_v = kmalloc(curr_dyn_buckets * sizeof(ipfw_dyn_rule *),
1149 M_IPFW, M_NOWAIT | M_ZERO);
1150 if (ipfw_dyn_v != NULL || curr_dyn_buckets <= 2)
1153 curr_dyn_buckets /= 2;
1154 if (curr_dyn_buckets <= old_curr_dyn_buckets &&
1155 old_dyn_v != NULL) {
1157 * Don't try allocating smaller buckets array, reuse
1158 * the old one, which alreay contains enough buckets
1164 if (ipfw_dyn_v != NULL) {
1165 if (old_dyn_v != NULL)
1166 kfree(old_dyn_v, M_IPFW);
1168 /* Allocation failed, restore old buckets array */
1169 ipfw_dyn_v = old_dyn_v;
1170 curr_dyn_buckets = old_curr_dyn_buckets;
1173 if (ipfw_dyn_v != NULL)
1178 * Install state of type 'type' for a dynamic session.
1179 * The hash table contains two type of rules:
1180 * - regular rules (O_KEEP_STATE)
1181 * - rules for sessions with limited number of sess per user
1182 * (O_LIMIT). When they are created, the parent is
1183 * increased by 1, and decreased on delete. In this case,
1184 * the third parameter is the parent rule and not the chain.
1185 * - "parent" rules for the above (O_LIMIT_PARENT).
1187 static ipfw_dyn_rule *
1188 add_dyn_rule(struct ipfw_flow_id *id, uint8_t dyn_type, struct ip_fw *rule)
1193 if (ipfw_dyn_v == NULL ||
1194 (dyn_count == 0 && dyn_buckets != curr_dyn_buckets)) {
1195 realloc_dynamic_table();
1196 if (ipfw_dyn_v == NULL)
1197 return NULL; /* failed ! */
1199 i = hash_packet(id);
1201 r = kmalloc(sizeof(*r), M_IPFW, M_NOWAIT | M_ZERO);
1203 kprintf ("sorry cannot allocate state\n");
1207 /* increase refcount on parent, and set pointer */
1208 if (dyn_type == O_LIMIT) {
1209 ipfw_dyn_rule *parent = (ipfw_dyn_rule *)rule;
1211 if (parent->dyn_type != O_LIMIT_PARENT)
1212 panic("invalid parent");
1215 rule = parent->stub->rule[mycpuid];
1216 KKASSERT(rule->stub == parent->stub);
1218 KKASSERT(rule->cpuid == mycpuid && rule->stub != NULL);
1221 r->expire = time_second + dyn_syn_lifetime;
1222 r->stub = rule->stub;
1223 r->dyn_type = dyn_type;
1224 r->pcnt = r->bcnt = 0;
1228 r->next = ipfw_dyn_v[i];
1232 DEB(kprintf("-- add dyn entry ty %d 0x%08x %d -> 0x%08x %d, total %d\n",
1234 (r->id.src_ip), (r->id.src_port),
1235 (r->id.dst_ip), (r->id.dst_port),
1241 * lookup dynamic parent rule using pkt and rule as search keys.
1242 * If the lookup fails, then install one.
1244 static ipfw_dyn_rule *
1245 lookup_dyn_parent(struct ipfw_flow_id *pkt, struct ip_fw *rule)
1251 i = hash_packet(pkt);
1252 for (q = ipfw_dyn_v[i]; q != NULL; q = q->next) {
1253 if (q->dyn_type == O_LIMIT_PARENT &&
1254 rule->stub == q->stub &&
1255 pkt->proto == q->id.proto &&
1256 pkt->src_ip == q->id.src_ip &&
1257 pkt->dst_ip == q->id.dst_ip &&
1258 pkt->src_port == q->id.src_port &&
1259 pkt->dst_port == q->id.dst_port) {
1260 q->expire = time_second + dyn_short_lifetime;
1261 DEB(kprintf("lookup_dyn_parent found 0x%p\n",q);)
1266 return add_dyn_rule(pkt, O_LIMIT_PARENT, rule);
1270 * Install dynamic state for rule type cmd->o.opcode
1272 * Returns 1 (failure) if state is not installed because of errors or because
1273 * session limitations are enforced.
1276 install_state_locked(struct ip_fw *rule, ipfw_insn_limit *cmd,
1277 struct ip_fw_args *args)
1279 static int last_log; /* XXX */
1283 DEB(kprintf("-- install state type %d 0x%08x %u -> 0x%08x %u\n",
1285 (args->f_id.src_ip), (args->f_id.src_port),
1286 (args->f_id.dst_ip), (args->f_id.dst_port) );)
1288 q = lookup_dyn_rule(&args->f_id, NULL, NULL);
1289 if (q != NULL) { /* should never occur */
1290 if (last_log != time_second) {
1291 last_log = time_second;
1292 kprintf(" install_state: entry already present, done\n");
1297 if (dyn_count >= dyn_max) {
1299 * Run out of slots, try to remove any expired rule.
1301 remove_dyn_rule_locked(NULL, (ipfw_dyn_rule *)1);
1302 if (dyn_count >= dyn_max) {
1303 if (last_log != time_second) {
1304 last_log = time_second;
1305 kprintf("install_state: "
1306 "Too many dynamic rules\n");
1308 return 1; /* cannot install, notify caller */
1312 switch (cmd->o.opcode) {
1313 case O_KEEP_STATE: /* bidir rule */
1314 if (add_dyn_rule(&args->f_id, O_KEEP_STATE, rule) == NULL)
1318 case O_LIMIT: /* limit number of sessions */
1320 uint16_t limit_mask = cmd->limit_mask;
1321 struct ipfw_flow_id id;
1322 ipfw_dyn_rule *parent;
1324 DEB(kprintf("installing dyn-limit rule %d\n",
1327 id.dst_ip = id.src_ip = 0;
1328 id.dst_port = id.src_port = 0;
1329 id.proto = args->f_id.proto;
1331 if (limit_mask & DYN_SRC_ADDR)
1332 id.src_ip = args->f_id.src_ip;
1333 if (limit_mask & DYN_DST_ADDR)
1334 id.dst_ip = args->f_id.dst_ip;
1335 if (limit_mask & DYN_SRC_PORT)
1336 id.src_port = args->f_id.src_port;
1337 if (limit_mask & DYN_DST_PORT)
1338 id.dst_port = args->f_id.dst_port;
1340 parent = lookup_dyn_parent(&id, rule);
1341 if (parent == NULL) {
1342 kprintf("add parent failed\n");
1346 if (parent->count >= cmd->conn_limit) {
1348 * See if we can remove some expired rule.
1350 remove_dyn_rule_locked(rule, parent);
1351 if (parent->count >= cmd->conn_limit) {
1353 last_log != time_second) {
1354 last_log = time_second;
1355 log(LOG_SECURITY | LOG_DEBUG,
1357 "too many entries\n");
1362 if (add_dyn_rule(&args->f_id, O_LIMIT,
1363 (struct ip_fw *)parent) == NULL)
1368 kprintf("unknown dynamic rule type %u\n", cmd->o.opcode);
1371 lookup_dyn_rule(&args->f_id, NULL, NULL); /* XXX just set lifetime */
1376 install_state(struct ip_fw *rule, ipfw_insn_limit *cmd,
1377 struct ip_fw_args *args, int *deny)
1379 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
1384 gen = ctx->ipfw_gen;
1386 lockmgr(&dyn_lock, LK_EXCLUSIVE);
1387 if (ctx->ipfw_gen != gen) {
1388 /* See the comment in lookup_rule() */
1391 ret = install_state_locked(rule, cmd, args);
1393 lockmgr(&dyn_lock, LK_RELEASE);
1399 * Transmit a TCP packet, containing either a RST or a keepalive.
1400 * When flags & TH_RST, we are sending a RST packet, because of a
1401 * "reset" action matched the packet.
1402 * Otherwise we are sending a keepalive, and flags & TH_
1405 send_pkt(struct ipfw_flow_id *id, uint32_t seq, uint32_t ack, int flags)
1410 struct route sro; /* fake route */
1412 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1415 m->m_pkthdr.rcvif = NULL;
1416 m->m_pkthdr.len = m->m_len = sizeof(struct ip) + sizeof(struct tcphdr);
1417 m->m_data += max_linkhdr;
1419 ip = mtod(m, struct ip *);
1420 bzero(ip, m->m_len);
1421 tcp = (struct tcphdr *)(ip + 1); /* no IP options */
1422 ip->ip_p = IPPROTO_TCP;
1426 * Assume we are sending a RST (or a keepalive in the reverse
1427 * direction), swap src and destination addresses and ports.
1429 ip->ip_src.s_addr = htonl(id->dst_ip);
1430 ip->ip_dst.s_addr = htonl(id->src_ip);
1431 tcp->th_sport = htons(id->dst_port);
1432 tcp->th_dport = htons(id->src_port);
1433 if (flags & TH_RST) { /* we are sending a RST */
1434 if (flags & TH_ACK) {
1435 tcp->th_seq = htonl(ack);
1436 tcp->th_ack = htonl(0);
1437 tcp->th_flags = TH_RST;
1441 tcp->th_seq = htonl(0);
1442 tcp->th_ack = htonl(seq);
1443 tcp->th_flags = TH_RST | TH_ACK;
1447 * We are sending a keepalive. flags & TH_SYN determines
1448 * the direction, forward if set, reverse if clear.
1449 * NOTE: seq and ack are always assumed to be correct
1450 * as set by the caller. This may be confusing...
1452 if (flags & TH_SYN) {
1454 * we have to rewrite the correct addresses!
1456 ip->ip_dst.s_addr = htonl(id->dst_ip);
1457 ip->ip_src.s_addr = htonl(id->src_ip);
1458 tcp->th_dport = htons(id->dst_port);
1459 tcp->th_sport = htons(id->src_port);
1461 tcp->th_seq = htonl(seq);
1462 tcp->th_ack = htonl(ack);
1463 tcp->th_flags = TH_ACK;
1467 * set ip_len to the payload size so we can compute
1468 * the tcp checksum on the pseudoheader
1469 * XXX check this, could save a couple of words ?
1471 ip->ip_len = htons(sizeof(struct tcphdr));
1472 tcp->th_sum = in_cksum(m, m->m_pkthdr.len);
1475 * now fill fields left out earlier
1477 ip->ip_ttl = ip_defttl;
1478 ip->ip_len = m->m_pkthdr.len;
1480 bzero(&sro, sizeof(sro));
1481 ip_rtaddr(ip->ip_dst, &sro);
1483 m->m_pkthdr.fw_flags |= IPFW_MBUF_GENERATED;
1484 ip_output(m, NULL, &sro, 0, NULL, NULL);
1490 * sends a reject message, consuming the mbuf passed as an argument.
1493 send_reject(struct ip_fw_args *args, int code, int offset, int ip_len)
1495 if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */
1496 /* We need the IP header in host order for icmp_error(). */
1497 if (args->eh != NULL) {
1498 struct ip *ip = mtod(args->m, struct ip *);
1500 ip->ip_len = ntohs(ip->ip_len);
1501 ip->ip_off = ntohs(ip->ip_off);
1503 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
1504 } else if (offset == 0 && args->f_id.proto == IPPROTO_TCP) {
1505 struct tcphdr *const tcp =
1506 L3HDR(struct tcphdr, mtod(args->m, struct ip *));
1508 if ((tcp->th_flags & TH_RST) == 0) {
1509 send_pkt(&args->f_id, ntohl(tcp->th_seq),
1510 ntohl(tcp->th_ack), tcp->th_flags | TH_RST);
1521 * Given an ip_fw *, lookup_next_rule will return a pointer
1522 * to the next rule, which can be either the jump
1523 * target (for skipto instructions) or the next one in the list (in
1524 * all other cases including a missing jump target).
1525 * The result is also written in the "next_rule" field of the rule.
1526 * Backward jumps are not allowed, so start looking from the next
1529 * This never returns NULL -- in case we do not have an exact match,
1530 * the next rule is returned. When the ruleset is changed,
1531 * pointers are flushed so we are always correct.
1534 static struct ip_fw *
1535 lookup_next_rule(struct ip_fw *me)
1537 struct ip_fw *rule = NULL;
1540 /* look for action, in case it is a skipto */
1541 cmd = ACTION_PTR(me);
1542 if (cmd->opcode == O_LOG)
1544 if (cmd->opcode == O_SKIPTO) {
1545 for (rule = me->next; rule; rule = rule->next) {
1546 if (rule->rulenum >= cmd->arg1)
1550 if (rule == NULL) /* failure or not a skipto */
1552 me->next_rule = rule;
1557 * The main check routine for the firewall.
1559 * All arguments are in args so we can modify them and return them
1560 * back to the caller.
1564 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1565 * Starts with the IP header.
1566 * args->eh (in) Mac header if present, or NULL for layer3 packet.
1567 * args->oif Outgoing interface, or NULL if packet is incoming.
1568 * The incoming interface is in the mbuf. (in)
1570 * args->rule Pointer to the last matching rule (in/out)
1571 * args->f_id Addresses grabbed from the packet (out)
1575 * IP_FW_PORT_DENY_FLAG the packet must be dropped.
1576 * 0 The packet is to be accepted and routed normally OR
1577 * the packet was denied/rejected and has been dropped;
1578 * in the latter case, *m is equal to NULL upon return.
1579 * port Divert the packet to port, with these caveats:
1581 * - If IP_FW_PORT_TEE_FLAG is set, tee the packet instead
1582 * of diverting it (ie, 'ipfw tee').
1584 * - If IP_FW_PORT_DYNT_FLAG is set, interpret the lower
1585 * 16 bits as a dummynet pipe number instead of diverting
1589 ipfw_chk(struct ip_fw_args *args)
1592 * Local variables hold state during the processing of a packet.
1594 * IMPORTANT NOTE: to speed up the processing of rules, there
1595 * are some assumption on the values of the variables, which
1596 * are documented here. Should you change them, please check
1597 * the implementation of the various instructions to make sure
1598 * that they still work.
1600 * args->eh The MAC header. It is non-null for a layer2
1601 * packet, it is NULL for a layer-3 packet.
1603 * m | args->m Pointer to the mbuf, as received from the caller.
1604 * It may change if ipfw_chk() does an m_pullup, or if it
1605 * consumes the packet because it calls send_reject().
1606 * XXX This has to change, so that ipfw_chk() never modifies
1607 * or consumes the buffer.
1608 * ip is simply an alias of the value of m, and it is kept
1609 * in sync with it (the packet is supposed to start with
1612 struct mbuf *m = args->m;
1613 struct ip *ip = mtod(m, struct ip *);
1616 * oif | args->oif If NULL, ipfw_chk has been called on the
1617 * inbound path (ether_input, ip_input).
1618 * If non-NULL, ipfw_chk has been called on the outbound path
1619 * (ether_output, ip_output).
1621 struct ifnet *oif = args->oif;
1623 struct ip_fw *f = NULL; /* matching rule */
1624 int retval = IP_FW_PASS;
1626 struct divert_info *divinfo;
1629 * hlen The length of the IPv4 header.
1630 * hlen >0 means we have an IPv4 packet.
1632 u_int hlen = 0; /* hlen >0 means we have an IP pkt */
1635 * offset The offset of a fragment. offset != 0 means that
1636 * we have a fragment at this offset of an IPv4 packet.
1637 * offset == 0 means that (if this is an IPv4 packet)
1638 * this is the first or only fragment.
1643 * Local copies of addresses. They are only valid if we have
1646 * proto The protocol. Set to 0 for non-ip packets,
1647 * or to the protocol read from the packet otherwise.
1648 * proto != 0 means that we have an IPv4 packet.
1650 * src_port, dst_port port numbers, in HOST format. Only
1651 * valid for TCP and UDP packets.
1653 * src_ip, dst_ip ip addresses, in NETWORK format.
1654 * Only valid for IPv4 packets.
1657 uint16_t src_port = 0, dst_port = 0; /* NOTE: host format */
1658 struct in_addr src_ip, dst_ip; /* NOTE: network format */
1659 uint16_t ip_len = 0;
1662 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1663 * MATCH_NONE when checked and not matched (dyn_f = NULL),
1664 * MATCH_FORWARD or MATCH_REVERSE otherwise (dyn_f != NULL)
1666 int dyn_dir = MATCH_UNKNOWN;
1667 struct ip_fw *dyn_f = NULL;
1668 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
1670 if (m->m_pkthdr.fw_flags & IPFW_MBUF_GENERATED)
1671 return IP_FW_PASS; /* accept */
1673 if (args->eh == NULL || /* layer 3 packet */
1674 (m->m_pkthdr.len >= sizeof(struct ip) &&
1675 ntohs(args->eh->ether_type) == ETHERTYPE_IP))
1676 hlen = ip->ip_hl << 2;
1679 * Collect parameters into local variables for faster matching.
1681 if (hlen == 0) { /* do not grab addresses for non-ip pkts */
1682 proto = args->f_id.proto = 0; /* mark f_id invalid */
1683 goto after_ip_checks;
1686 proto = args->f_id.proto = ip->ip_p;
1687 src_ip = ip->ip_src;
1688 dst_ip = ip->ip_dst;
1689 if (args->eh != NULL) { /* layer 2 packets are as on the wire */
1690 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1691 ip_len = ntohs(ip->ip_len);
1693 offset = ip->ip_off & IP_OFFMASK;
1694 ip_len = ip->ip_len;
1697 #define PULLUP_TO(len) \
1699 if (m->m_len < (len)) { \
1700 args->m = m = m_pullup(m, (len));\
1702 goto pullup_failed; \
1703 ip = mtod(m, struct ip *); \
1713 PULLUP_TO(hlen + sizeof(struct tcphdr));
1714 tcp = L3HDR(struct tcphdr, ip);
1715 dst_port = tcp->th_dport;
1716 src_port = tcp->th_sport;
1717 args->f_id.flags = tcp->th_flags;
1725 PULLUP_TO(hlen + sizeof(struct udphdr));
1726 udp = L3HDR(struct udphdr, ip);
1727 dst_port = udp->uh_dport;
1728 src_port = udp->uh_sport;
1733 PULLUP_TO(hlen + 4); /* type, code and checksum. */
1734 args->f_id.flags = L3HDR(struct icmp, ip)->icmp_type;
1744 args->f_id.src_ip = ntohl(src_ip.s_addr);
1745 args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1746 args->f_id.src_port = src_port = ntohs(src_port);
1747 args->f_id.dst_port = dst_port = ntohs(dst_port);
1752 * Packet has already been tagged. Look for the next rule
1753 * to restart processing.
1755 * If fw_one_pass != 0 then just accept it.
1756 * XXX should not happen here, but optimized out in
1762 /* This rule is being/has been flushed */
1766 KASSERT(args->rule->cpuid == mycpuid,
1767 ("rule used on cpu%d\n", mycpuid));
1769 /* This rule was deleted */
1770 if (args->rule->rule_flags & IPFW_RULE_F_INVALID)
1773 f = args->rule->next_rule;
1775 f = lookup_next_rule(args->rule);
1778 * Find the starting rule. It can be either the first
1779 * one, or the one after divert_rule if asked so.
1783 mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL);
1785 divinfo = m_tag_data(mtag);
1786 skipto = divinfo->skipto;
1791 f = ctx->ipfw_layer3_chain;
1792 if (args->eh == NULL && skipto != 0) {
1793 /* No skipto during rule flushing */
1797 if (skipto >= IPFW_DEFAULT_RULE)
1798 return IP_FW_DENY; /* invalid */
1800 while (f && f->rulenum <= skipto)
1802 if (f == NULL) /* drop packet */
1804 } else if (ipfw_flushing) {
1805 /* Rules are being flushed; skip to default rule */
1806 f = ctx->ipfw_default_rule;
1809 if ((mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
1810 m_tag_delete(m, mtag);
1813 * Now scan the rules, and parse microinstructions for each rule.
1815 for (; f; f = f->next) {
1818 int skip_or; /* skip rest of OR block */
1821 if (ctx->ipfw_set_disable & (1 << f->set))
1825 for (l = f->cmd_len, cmd = f->cmd; l > 0;
1826 l -= cmdlen, cmd += cmdlen) {
1830 * check_body is a jump target used when we find a
1831 * CHECK_STATE, and need to jump to the body of
1836 cmdlen = F_LEN(cmd);
1838 * An OR block (insn_1 || .. || insn_n) has the
1839 * F_OR bit set in all but the last instruction.
1840 * The first match will set "skip_or", and cause
1841 * the following instructions to be skipped until
1842 * past the one with the F_OR bit clear.
1844 if (skip_or) { /* skip this instruction */
1845 if ((cmd->len & F_OR) == 0)
1846 skip_or = 0; /* next one is good */
1849 match = 0; /* set to 1 if we succeed */
1851 switch (cmd->opcode) {
1853 * The first set of opcodes compares the packet's
1854 * fields with some pattern, setting 'match' if a
1855 * match is found. At the end of the loop there is
1856 * logic to deal with F_NOT and F_OR flags associated
1864 kprintf("ipfw: opcode %d unimplemented\n",
1871 * We only check offset == 0 && proto != 0,
1872 * as this ensures that we have an IPv4
1873 * packet with the ports info.
1878 struct inpcbinfo *pi;
1882 if (proto == IPPROTO_TCP) {
1884 pi = &tcbinfo[mycpu->gd_cpuid];
1885 } else if (proto == IPPROTO_UDP) {
1892 in_pcblookup_hash(pi,
1893 dst_ip, htons(dst_port),
1894 src_ip, htons(src_port),
1896 in_pcblookup_hash(pi,
1897 src_ip, htons(src_port),
1898 dst_ip, htons(dst_port),
1901 if (pcb == NULL || pcb->inp_socket == NULL)
1904 if (cmd->opcode == O_UID) {
1905 #define socheckuid(a,b) ((a)->so_cred->cr_uid != (b))
1907 !socheckuid(pcb->inp_socket,
1908 (uid_t)((ipfw_insn_u32 *)cmd)->d[0]);
1911 match = groupmember(
1912 (uid_t)((ipfw_insn_u32 *)cmd)->d[0],
1913 pcb->inp_socket->so_cred);
1919 match = iface_match(m->m_pkthdr.rcvif,
1920 (ipfw_insn_if *)cmd);
1924 match = iface_match(oif, (ipfw_insn_if *)cmd);
1928 match = iface_match(oif ? oif :
1929 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd);
1933 if (args->eh != NULL) { /* have MAC header */
1934 uint32_t *want = (uint32_t *)
1935 ((ipfw_insn_mac *)cmd)->addr;
1936 uint32_t *mask = (uint32_t *)
1937 ((ipfw_insn_mac *)cmd)->mask;
1938 uint32_t *hdr = (uint32_t *)args->eh;
1941 (want[0] == (hdr[0] & mask[0]) &&
1942 want[1] == (hdr[1] & mask[1]) &&
1943 want[2] == (hdr[2] & mask[2]));
1948 if (args->eh != NULL) {
1950 ntohs(args->eh->ether_type);
1952 ((ipfw_insn_u16 *)cmd)->ports;
1955 /* Special vlan handling */
1956 if (m->m_flags & M_VLANTAG)
1959 for (i = cmdlen - 1; !match && i > 0;
1962 (t >= p[0] && t <= p[1]);
1968 match = (hlen > 0 && offset != 0);
1971 case O_IN: /* "out" is "not in" */
1972 match = (oif == NULL);
1976 match = (args->eh != NULL);
1981 * We do not allow an arg of 0 so the
1982 * check of "proto" only suffices.
1984 match = (proto == cmd->arg1);
1988 match = (hlen > 0 &&
1989 ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1994 match = (hlen > 0 &&
1995 ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1997 ((ipfw_insn_ip *)cmd)->mask.s_addr));
2004 tif = INADDR_TO_IFP(&src_ip);
2005 match = (tif != NULL);
2012 uint32_t *d = (uint32_t *)(cmd + 1);
2014 cmd->opcode == O_IP_DST_SET ?
2020 addr -= d[0]; /* subtract base */
2022 (addr < cmd->arg1) &&
2023 (d[1 + (addr >> 5)] &
2024 (1 << (addr & 0x1f)));
2029 match = (hlen > 0 &&
2030 ((ipfw_insn_ip *)cmd)->addr.s_addr ==
2035 match = (hlen > 0) &&
2036 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
2038 ((ipfw_insn_ip *)cmd)->mask.s_addr));
2045 tif = INADDR_TO_IFP(&dst_ip);
2046 match = (tif != NULL);
2053 * offset == 0 && proto != 0 is enough
2054 * to guarantee that we have an IPv4
2055 * packet with port info.
2057 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP)
2060 (cmd->opcode == O_IP_SRCPORT) ?
2061 src_port : dst_port ;
2063 ((ipfw_insn_u16 *)cmd)->ports;
2066 for (i = cmdlen - 1; !match && i > 0;
2069 (x >= p[0] && x <= p[1]);
2075 match = (offset == 0 && proto==IPPROTO_ICMP &&
2076 icmptype_match(ip, (ipfw_insn_u32 *)cmd));
2080 match = (hlen > 0 && ipopts_match(ip, cmd));
2084 match = (hlen > 0 && cmd->arg1 == ip->ip_v);
2088 match = (hlen > 0 && cmd->arg1 == ip->ip_ttl);
2092 match = (hlen > 0 &&
2093 cmd->arg1 == ntohs(ip->ip_id));
2097 match = (hlen > 0 && cmd->arg1 == ip_len);
2100 case O_IPPRECEDENCE:
2101 match = (hlen > 0 &&
2102 (cmd->arg1 == (ip->ip_tos & 0xe0)));
2106 match = (hlen > 0 &&
2107 flags_match(cmd, ip->ip_tos));
2111 match = (proto == IPPROTO_TCP && offset == 0 &&
2113 L3HDR(struct tcphdr,ip)->th_flags));
2117 match = (proto == IPPROTO_TCP && offset == 0 &&
2118 tcpopts_match(ip, cmd));
2122 match = (proto == IPPROTO_TCP && offset == 0 &&
2123 ((ipfw_insn_u32 *)cmd)->d[0] ==
2124 L3HDR(struct tcphdr,ip)->th_seq);
2128 match = (proto == IPPROTO_TCP && offset == 0 &&
2129 ((ipfw_insn_u32 *)cmd)->d[0] ==
2130 L3HDR(struct tcphdr,ip)->th_ack);
2134 match = (proto == IPPROTO_TCP && offset == 0 &&
2136 L3HDR(struct tcphdr,ip)->th_win);
2140 /* reject packets which have SYN only */
2141 /* XXX should i also check for TH_ACK ? */
2142 match = (proto == IPPROTO_TCP && offset == 0 &&
2143 (L3HDR(struct tcphdr,ip)->th_flags &
2144 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
2149 ipfw_log(f, hlen, args->eh, m, oif);
2154 match = (krandom() <
2155 ((ipfw_insn_u32 *)cmd)->d[0]);
2159 * The second set of opcodes represents 'actions',
2160 * i.e. the terminal part of a rule once the packet
2161 * matches all previous patterns.
2162 * Typically there is only one action for each rule,
2163 * and the opcode is stored at the end of the rule
2164 * (but there are exceptions -- see below).
2166 * In general, here we set retval and terminate the
2167 * outer loop (would be a 'break 3' in some language,
2168 * but we need to do a 'goto done').
2171 * O_COUNT and O_SKIPTO actions:
2172 * instead of terminating, we jump to the next rule
2173 * ('goto next_rule', equivalent to a 'break 2'),
2174 * or to the SKIPTO target ('goto again' after
2175 * having set f, cmd and l), respectively.
2177 * O_LIMIT and O_KEEP_STATE: these opcodes are
2178 * not real 'actions', and are stored right
2179 * before the 'action' part of the rule.
2180 * These opcodes try to install an entry in the
2181 * state tables; if successful, we continue with
2182 * the next opcode (match=1; break;), otherwise
2183 * the packet must be dropped ('goto done' after
2184 * setting retval). If static rules are changed
2185 * during the state installation, the packet will
2186 * be dropped and rule's stats will not beupdated
2187 * ('return IP_FW_DENY').
2189 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2190 * cause a lookup of the state table, and a jump
2191 * to the 'action' part of the parent rule
2192 * ('goto check_body') if an entry is found, or
2193 * (CHECK_STATE only) a jump to the next rule if
2194 * the entry is not found ('goto next_rule').
2195 * The result of the lookup is cached to make
2196 * further instances of these opcodes are
2197 * effectively NOPs. If static rules are changed
2198 * during the state looking up, the packet will
2199 * be dropped and rule's stats will not be updated
2200 * ('return IP_FW_DENY').
2204 if (!(f->rule_flags & IPFW_RULE_F_STATE)) {
2205 kprintf("%s rule (%d) is not ready "
2207 cmd->opcode == O_LIMIT ?
2208 "limit" : "keep state",
2209 f->rulenum, f->cpuid);
2212 if (install_state(f,
2213 (ipfw_insn_limit *)cmd, args, &deny)) {
2217 retval = IP_FW_DENY;
2218 goto done; /* error/limit violation */
2228 * dynamic rules are checked at the first
2229 * keep-state or check-state occurrence,
2230 * with the result being stored in dyn_dir.
2231 * The compiler introduces a PROBE_STATE
2232 * instruction for us when we have a
2233 * KEEP_STATE (because PROBE_STATE needs
2236 if (dyn_dir == MATCH_UNKNOWN) {
2237 dyn_f = lookup_rule(&args->f_id,
2239 proto == IPPROTO_TCP ?
2240 L3HDR(struct tcphdr, ip) : NULL,
2244 if (dyn_f != NULL) {
2246 * Found a rule from a dynamic
2247 * entry; jump to the 'action'
2251 cmd = ACTION_PTR(f);
2252 l = f->cmd_len - f->act_ofs;
2257 * Dynamic entry not found. If CHECK_STATE,
2258 * skip to next rule, if PROBE_STATE just
2259 * ignore and continue with next opcode.
2261 if (cmd->opcode == O_CHECK_STATE)
2263 else if (!(f->rule_flags & IPFW_RULE_F_STATE))
2264 goto next_rule; /* not ready yet */
2269 retval = IP_FW_PASS; /* accept */
2274 args->rule = f; /* report matching rule */
2275 args->cookie = cmd->arg1;
2276 retval = IP_FW_DUMMYNET;
2281 if (args->eh) /* not on layer 2 */
2284 mtag = m_tag_get(PACKET_TAG_IPFW_DIVERT,
2285 sizeof(*divinfo), MB_DONTWAIT);
2287 retval = IP_FW_DENY;
2290 divinfo = m_tag_data(mtag);
2292 divinfo->skipto = f->rulenum;
2293 divinfo->port = cmd->arg1;
2294 divinfo->tee = (cmd->opcode == O_TEE);
2295 m_tag_prepend(m, mtag);
2297 args->cookie = cmd->arg1;
2298 retval = (cmd->opcode == O_DIVERT) ?
2299 IP_FW_DIVERT : IP_FW_TEE;
2304 f->pcnt++; /* update stats */
2306 f->timestamp = time_second;
2307 if (cmd->opcode == O_COUNT)
2310 if (f->next_rule == NULL)
2311 lookup_next_rule(f);
2317 * Drop the packet and send a reject notice
2318 * if the packet is not ICMP (or is an ICMP
2319 * query), and it is not multicast/broadcast.
2322 (proto != IPPROTO_ICMP ||
2323 is_icmp_query(ip)) &&
2324 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2325 !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2327 * Update statistics before the possible
2328 * blocking 'send_reject'
2332 f->timestamp = time_second;
2334 send_reject(args, cmd->arg1,
2339 * Return directly here, rule stats
2340 * have been updated above.
2346 retval = IP_FW_DENY;
2350 if (args->eh) /* not valid on layer2 pkts */
2352 if (!dyn_f || dyn_dir == MATCH_FORWARD) {
2353 struct sockaddr_in *sin;
2355 mtag = m_tag_get(PACKET_TAG_IPFORWARD,
2356 sizeof(*sin), MB_DONTWAIT);
2358 retval = IP_FW_DENY;
2361 sin = m_tag_data(mtag);
2363 /* Structure copy */
2364 *sin = ((ipfw_insn_sa *)cmd)->sa;
2366 m_tag_prepend(m, mtag);
2367 m->m_pkthdr.fw_flags |=
2368 IPFORWARD_MBUF_TAGGED;
2370 retval = IP_FW_PASS;
2374 panic("-- unknown opcode %d\n", cmd->opcode);
2375 } /* end of switch() on opcodes */
2377 if (cmd->len & F_NOT)
2381 if (cmd->len & F_OR)
2384 if (!(cmd->len & F_OR)) /* not an OR block, */
2385 break; /* try next rule */
2388 } /* end of inner for, scan opcodes */
2390 next_rule:; /* try next rule */
2392 } /* end of outer for, scan rules */
2393 kprintf("+++ ipfw: ouch!, skip past end of rules, denying packet\n");
2397 /* Update statistics */
2400 f->timestamp = time_second;
2405 kprintf("pullup failed\n");
2410 ipfw_dummynet_io(struct mbuf *m, int pipe_nr, int dir, struct ip_fw_args *fwa)
2415 const struct ipfw_flow_id *id;
2416 struct dn_flow_id *fid;
2420 mtag = m_tag_get(PACKET_TAG_DUMMYNET, sizeof(*pkt), MB_DONTWAIT);
2425 m_tag_prepend(m, mtag);
2427 pkt = m_tag_data(mtag);
2428 bzero(pkt, sizeof(*pkt));
2430 cmd = fwa->rule->cmd + fwa->rule->act_ofs;
2431 if (cmd->opcode == O_LOG)
2433 KASSERT(cmd->opcode == O_PIPE || cmd->opcode == O_QUEUE,
2434 ("Rule is not PIPE or QUEUE, opcode %d\n", cmd->opcode));
2437 pkt->dn_flags = (dir & DN_FLAGS_DIR_MASK);
2438 pkt->ifp = fwa->oif;
2439 pkt->cpuid = mycpu->gd_cpuid;
2440 pkt->pipe_nr = pipe_nr;
2444 fid->fid_dst_ip = id->dst_ip;
2445 fid->fid_src_ip = id->src_ip;
2446 fid->fid_dst_port = id->dst_port;
2447 fid->fid_src_port = id->src_port;
2448 fid->fid_proto = id->proto;
2449 fid->fid_flags = id->flags;
2451 ipfw_ref_rule(fwa->rule);
2452 pkt->dn_priv = fwa->rule;
2453 pkt->dn_unref_priv = ipfw_unref_rule;
2455 if (cmd->opcode == O_PIPE)
2456 pkt->dn_flags |= DN_FLAGS_IS_PIPE;
2458 m->m_pkthdr.fw_flags |= DUMMYNET_MBUF_TAGGED;
2462 * When a rule is added/deleted, clear the next_rule pointers in all rules.
2463 * These will be reconstructed on the fly as packets are matched.
2464 * Must be called at splimp().
2467 ipfw_flush_rule_ptrs(struct ipfw_context *ctx)
2471 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next)
2472 rule->next_rule = NULL;
2475 static __inline void
2476 ipfw_inc_static_count(struct ip_fw *rule)
2478 KKASSERT(mycpuid == 0);
2481 static_ioc_len += IOC_RULESIZE(rule);
2484 static __inline void
2485 ipfw_dec_static_count(struct ip_fw *rule)
2487 int l = IOC_RULESIZE(rule);
2489 KKASSERT(mycpuid == 0);
2491 KASSERT(static_count > 0, ("invalid static count %u\n", static_count));
2494 KASSERT(static_ioc_len >= l,
2495 ("invalid static len %u\n", static_ioc_len));
2496 static_ioc_len -= l;
2500 ipfw_link_sibling(struct netmsg_ipfw *fwmsg, struct ip_fw *rule)
2502 if (fwmsg->sibling != NULL) {
2503 KKASSERT(mycpuid > 0 && fwmsg->sibling->cpuid == mycpuid - 1);
2504 fwmsg->sibling->sibling = rule;
2506 fwmsg->sibling = rule;
2509 static struct ip_fw *
2510 ipfw_create_rule(const struct ipfw_ioc_rule *ioc_rule, struct ip_fw_stub *stub)
2514 rule = kmalloc(RULESIZE(ioc_rule), M_IPFW, M_WAITOK | M_ZERO);
2516 rule->act_ofs = ioc_rule->act_ofs;
2517 rule->cmd_len = ioc_rule->cmd_len;
2518 rule->rulenum = ioc_rule->rulenum;
2519 rule->set = ioc_rule->set;
2520 rule->usr_flags = ioc_rule->usr_flags;
2522 bcopy(ioc_rule->cmd, rule->cmd, rule->cmd_len * 4 /* XXX */);
2525 rule->cpuid = mycpuid;
2529 stub->rule[mycpuid] = rule;
2535 ipfw_add_rule_dispatch(struct netmsg *nmsg)
2537 struct netmsg_ipfw *fwmsg = (struct netmsg_ipfw *)nmsg;
2538 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2541 rule = ipfw_create_rule(fwmsg->ioc_rule, fwmsg->stub);
2544 * Bump generation after ipfw_create_rule(),
2545 * since this function is blocking
2550 * Insert rule into the pre-determined position
2552 if (fwmsg->prev_rule != NULL) {
2553 struct ip_fw *prev, *next;
2555 prev = fwmsg->prev_rule;
2556 KKASSERT(prev->cpuid == mycpuid);
2558 next = fwmsg->next_rule;
2559 KKASSERT(next->cpuid == mycpuid);
2565 * Move to the position on the next CPU
2566 * before the msg is forwarded.
2568 fwmsg->prev_rule = prev->sibling;
2569 fwmsg->next_rule = next->sibling;
2571 KKASSERT(fwmsg->next_rule == NULL);
2572 rule->next = ctx->ipfw_layer3_chain;
2573 ctx->ipfw_layer3_chain = rule;
2576 /* Link rule CPU sibling */
2577 ipfw_link_sibling(fwmsg, rule);
2579 ipfw_flush_rule_ptrs(ctx);
2582 /* Statistics only need to be updated once */
2583 ipfw_inc_static_count(rule);
2585 /* Return the rule on CPU0 */
2586 nmsg->nm_lmsg.u.ms_resultp = rule;
2589 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2593 ipfw_enable_state_dispatch(struct netmsg *nmsg)
2595 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
2596 struct ip_fw *rule = lmsg->u.ms_resultp;
2597 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2601 KKASSERT(rule->cpuid == mycpuid);
2602 KKASSERT(rule->stub != NULL && rule->stub->rule[mycpuid] == rule);
2603 KKASSERT(!(rule->rule_flags & IPFW_RULE_F_STATE));
2604 rule->rule_flags |= IPFW_RULE_F_STATE;
2605 lmsg->u.ms_resultp = rule->sibling;
2607 ifnet_forwardmsg(lmsg, mycpuid + 1);
2611 * Add a new rule to the list. Copy the rule into a malloc'ed area,
2612 * then possibly create a rule number and add the rule to the list.
2613 * Update the rule_number in the input struct so the caller knows
2617 ipfw_add_rule(struct ipfw_ioc_rule *ioc_rule, uint32_t rule_flags)
2619 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2620 struct netmsg_ipfw fwmsg;
2621 struct netmsg *nmsg;
2622 struct ip_fw *f, *prev, *rule;
2623 struct ip_fw_stub *stub;
2625 IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
2630 * If rulenum is 0, find highest numbered rule before the
2631 * default rule, and add rule number incremental step.
2633 if (ioc_rule->rulenum == 0) {
2634 int step = autoinc_step;
2636 KKASSERT(step >= IPFW_AUTOINC_STEP_MIN &&
2637 step <= IPFW_AUTOINC_STEP_MAX);
2640 * Locate the highest numbered rule before default
2642 for (f = ctx->ipfw_layer3_chain; f; f = f->next) {
2643 if (f->rulenum == IPFW_DEFAULT_RULE)
2645 ioc_rule->rulenum = f->rulenum;
2647 if (ioc_rule->rulenum < IPFW_DEFAULT_RULE - step)
2648 ioc_rule->rulenum += step;
2650 KASSERT(ioc_rule->rulenum != IPFW_DEFAULT_RULE &&
2651 ioc_rule->rulenum != 0,
2652 ("invalid rule num %d\n", ioc_rule->rulenum));
2655 * Now find the right place for the new rule in the sorted list.
2657 for (prev = NULL, f = ctx->ipfw_layer3_chain; f;
2658 prev = f, f = f->next) {
2659 if (f->rulenum > ioc_rule->rulenum) {
2660 /* Found the location */
2664 KASSERT(f != NULL, ("no default rule?!\n"));
2666 if (rule_flags & IPFW_RULE_F_STATE) {
2670 * If the new rule will create states, then allocate
2671 * a rule stub, which will be referenced by states
2674 size = sizeof(*stub) + ((ncpus - 1) * sizeof(struct ip_fw *));
2675 stub = kmalloc(size, M_IPFW, M_WAITOK | M_ZERO);
2681 * Duplicate the rule onto each CPU.
2682 * The rule duplicated on CPU0 will be returned.
2684 bzero(&fwmsg, sizeof(fwmsg));
2686 netmsg_init(nmsg, &curthread->td_msgport, 0, ipfw_add_rule_dispatch);
2687 fwmsg.ioc_rule = ioc_rule;
2688 fwmsg.prev_rule = prev;
2689 fwmsg.next_rule = prev == NULL ? NULL : f;
2692 ifnet_domsg(&nmsg->nm_lmsg, 0);
2693 KKASSERT(fwmsg.prev_rule == NULL && fwmsg.next_rule == NULL);
2695 rule = nmsg->nm_lmsg.u.ms_resultp;
2696 KKASSERT(rule != NULL && rule->cpuid == mycpuid);
2698 if (rule_flags & IPFW_RULE_F_STATE) {
2700 * Turn on state flag, _after_ everything on all
2701 * CPUs have been setup.
2703 bzero(nmsg, sizeof(*nmsg));
2704 netmsg_init(nmsg, &curthread->td_msgport, 0,
2705 ipfw_enable_state_dispatch);
2706 nmsg->nm_lmsg.u.ms_resultp = rule;
2708 ifnet_domsg(&nmsg->nm_lmsg, 0);
2709 KKASSERT(nmsg->nm_lmsg.u.ms_resultp == NULL);
2714 DEB(kprintf("++ installed rule %d, static count now %d\n",
2715 rule->rulenum, static_count);)
2719 * Free storage associated with a static rule (including derived
2721 * The caller is in charge of clearing rule pointers to avoid
2722 * dangling pointers.
2723 * @return a pointer to the next entry.
2724 * Arguments are not checked, so they better be correct.
2725 * Must be called at splimp().
2727 static struct ip_fw *
2728 ipfw_delete_rule(struct ipfw_context *ctx,
2729 struct ip_fw *prev, struct ip_fw *rule)
2732 struct ip_fw_stub *stub;
2736 /* STATE flag should have been cleared before we reach here */
2737 KKASSERT((rule->rule_flags & IPFW_RULE_F_STATE) == 0);
2742 ctx->ipfw_layer3_chain = n;
2746 /* Mark the rule as invalid */
2747 rule->rule_flags |= IPFW_RULE_F_INVALID;
2748 rule->next_rule = NULL;
2749 rule->sibling = NULL;
2752 /* Don't reset cpuid here; keep various assertion working */
2756 /* Statistics only need to be updated once */
2758 ipfw_dec_static_count(rule);
2760 /* Free 'stub' on the last CPU */
2761 if (stub != NULL && mycpuid == ncpus - 1)
2762 kfree(stub, M_IPFW);
2764 /* Try to free this rule */
2765 ipfw_free_rule(rule);
2767 /* Return the next rule */
2772 ipfw_flush_dispatch(struct netmsg *nmsg)
2774 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
2775 int kill_default = lmsg->u.ms_result;
2776 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2779 ipfw_flush_rule_ptrs(ctx); /* more efficient to do outside the loop */
2781 while ((rule = ctx->ipfw_layer3_chain) != NULL &&
2782 (kill_default || rule->rulenum != IPFW_DEFAULT_RULE))
2783 ipfw_delete_rule(ctx, NULL, rule);
2785 ifnet_forwardmsg(lmsg, mycpuid + 1);
2789 ipfw_disable_rule_state_dispatch(struct netmsg *nmsg)
2791 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
2792 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2797 rule = dmsg->start_rule;
2799 KKASSERT(rule->cpuid == mycpuid);
2802 * Move to the position on the next CPU
2803 * before the msg is forwarded.
2805 dmsg->start_rule = rule->sibling;
2807 KKASSERT(dmsg->rulenum == 0);
2808 rule = ctx->ipfw_layer3_chain;
2811 while (rule != NULL) {
2812 if (dmsg->rulenum && rule->rulenum != dmsg->rulenum)
2814 rule->rule_flags &= ~IPFW_RULE_F_STATE;
2818 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2822 * Deletes all rules from a chain (including the default rule
2823 * if the second argument is set).
2824 * Must be called at splimp().
2827 ipfw_flush(int kill_default)
2829 struct netmsg_del dmsg;
2831 struct lwkt_msg *lmsg;
2833 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2835 IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
2838 * If 'kill_default' then caller has done the necessary
2839 * msgport syncing; unnecessary to do it again.
2841 if (!kill_default) {
2843 * Let ipfw_chk() know the rules are going to
2844 * be flushed, so it could jump directly to
2848 netmsg_service_sync();
2852 * Clear STATE flag on rules, so no more states (dyn rules)
2855 bzero(&dmsg, sizeof(dmsg));
2856 netmsg_init(&dmsg.nmsg, &curthread->td_msgport, 0,
2857 ipfw_disable_rule_state_dispatch);
2858 ifnet_domsg(&dmsg.nmsg.nm_lmsg, 0);
2861 * This actually nukes all states (dyn rules)
2863 lockmgr(&dyn_lock, LK_EXCLUSIVE);
2864 for (rule = ctx->ipfw_layer3_chain; rule != NULL; rule = rule->next) {
2866 * Can't check IPFW_RULE_F_STATE here,
2867 * since it has been cleared previously.
2868 * Check 'stub' instead.
2870 if (rule->stub != NULL) {
2872 remove_dyn_rule_locked(rule, NULL);
2875 lockmgr(&dyn_lock, LK_RELEASE);
2878 * Press the 'flush' button
2880 bzero(&nmsg, sizeof(nmsg));
2881 netmsg_init(&nmsg, &curthread->td_msgport, 0, ipfw_flush_dispatch);
2882 lmsg = &nmsg.nm_lmsg;
2883 lmsg->u.ms_result = kill_default;
2884 ifnet_domsg(lmsg, 0);
2886 KASSERT(dyn_count == 0, ("%u dyn rule remains\n", dyn_count));
2889 if (ipfw_dyn_v != NULL) {
2891 * Free dynamic rules(state) hash table
2893 kfree(ipfw_dyn_v, M_IPFW);
2897 KASSERT(static_count == 0,
2898 ("%u static rules remains\n", static_count));
2899 KASSERT(static_ioc_len == 0,
2900 ("%u bytes of static rules remains\n", static_ioc_len));
2902 KASSERT(static_count == 1,
2903 ("%u static rules remains\n", static_count));
2904 KASSERT(static_ioc_len == IOC_RULESIZE(ctx->ipfw_default_rule),
2905 ("%u bytes of static rules remains, should be %u\n",
2906 static_ioc_len, IOC_RULESIZE(ctx->ipfw_default_rule)));
2914 ipfw_alt_delete_rule_dispatch(struct netmsg *nmsg)
2916 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
2917 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2918 struct ip_fw *rule, *prev;
2920 rule = dmsg->start_rule;
2921 KKASSERT(rule->cpuid == mycpuid);
2922 dmsg->start_rule = rule->sibling;
2924 prev = dmsg->prev_rule;
2926 KKASSERT(prev->cpuid == mycpuid);
2929 * Move to the position on the next CPU
2930 * before the msg is forwarded.
2932 dmsg->prev_rule = prev->sibling;
2936 * flush pointers outside the loop, then delete all matching
2937 * rules. 'prev' remains the same throughout the cycle.
2939 ipfw_flush_rule_ptrs(ctx);
2940 while (rule && rule->rulenum == dmsg->rulenum)
2941 rule = ipfw_delete_rule(ctx, prev, rule);
2943 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
2947 ipfw_alt_delete_rule(uint16_t rulenum)
2949 struct ip_fw *prev, *rule, *f;
2950 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2951 struct netmsg_del dmsg;
2952 struct netmsg *nmsg;
2956 * Locate first rule to delete
2958 for (prev = NULL, rule = ctx->ipfw_layer3_chain;
2959 rule && rule->rulenum < rulenum;
2960 prev = rule, rule = rule->next)
2962 if (rule->rulenum != rulenum)
2966 * Check whether any rules with the given number will
2970 for (f = rule; f && f->rulenum == rulenum; f = f->next) {
2971 if (f->rule_flags & IPFW_RULE_F_STATE) {
2979 * Clear the STATE flag, so no more states will be
2980 * created based the rules numbered 'rulenum'.
2982 bzero(&dmsg, sizeof(dmsg));
2984 netmsg_init(nmsg, &curthread->td_msgport, 0,
2985 ipfw_disable_rule_state_dispatch);
2986 dmsg.start_rule = rule;
2987 dmsg.rulenum = rulenum;
2989 ifnet_domsg(&nmsg->nm_lmsg, 0);
2990 KKASSERT(dmsg.start_rule == NULL);
2993 * Nuke all related states
2995 lockmgr(&dyn_lock, LK_EXCLUSIVE);
2996 for (f = rule; f && f->rulenum == rulenum; f = f->next) {
2998 * Can't check IPFW_RULE_F_STATE here,
2999 * since it has been cleared previously.
3000 * Check 'stub' instead.
3002 if (f->stub != NULL) {
3004 remove_dyn_rule_locked(f, NULL);
3007 lockmgr(&dyn_lock, LK_RELEASE);
3011 * Get rid of the rule duplications on all CPUs
3013 bzero(&dmsg, sizeof(dmsg));
3015 netmsg_init(nmsg, &curthread->td_msgport, 0,
3016 ipfw_alt_delete_rule_dispatch);
3017 dmsg.prev_rule = prev;
3018 dmsg.start_rule = rule;
3019 dmsg.rulenum = rulenum;
3021 ifnet_domsg(&nmsg->nm_lmsg, 0);
3022 KKASSERT(dmsg.prev_rule == NULL && dmsg.start_rule == NULL);
3027 ipfw_alt_delete_ruleset_dispatch(struct netmsg *nmsg)
3029 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3030 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3031 struct ip_fw *prev, *rule;
3036 ipfw_flush_rule_ptrs(ctx);
3039 rule = ctx->ipfw_layer3_chain;
3040 while (rule != NULL) {
3041 if (rule->set == dmsg->from_set) {
3042 rule = ipfw_delete_rule(ctx, prev, rule);
3051 KASSERT(del, ("no match set?!\n"));
3053 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3057 ipfw_disable_ruleset_state_dispatch(struct netmsg *nmsg)
3059 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3060 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3068 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3069 if (rule->set == dmsg->from_set) {
3073 rule->rule_flags &= ~IPFW_RULE_F_STATE;
3076 KASSERT(cleared, ("no match set?!\n"));
3078 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3082 ipfw_alt_delete_ruleset(uint8_t set)
3084 struct netmsg_del dmsg;
3085 struct netmsg *nmsg;
3088 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3091 * Check whether the 'set' exists. If it exists,
3092 * then check whether any rules within the set will
3093 * try to create states.
3097 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3098 if (rule->set == set) {
3100 if (rule->rule_flags & IPFW_RULE_F_STATE) {
3107 return 0; /* XXX EINVAL? */
3111 * Clear the STATE flag, so no more states will be
3112 * created based the rules in this set.
3114 bzero(&dmsg, sizeof(dmsg));
3116 netmsg_init(nmsg, &curthread->td_msgport, 0,
3117 ipfw_disable_ruleset_state_dispatch);
3118 dmsg.from_set = set;
3120 ifnet_domsg(&nmsg->nm_lmsg, 0);
3123 * Nuke all related states
3125 lockmgr(&dyn_lock, LK_EXCLUSIVE);
3126 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3127 if (rule->set != set)
3131 * Can't check IPFW_RULE_F_STATE here,
3132 * since it has been cleared previously.
3133 * Check 'stub' instead.
3135 if (rule->stub != NULL) {
3137 remove_dyn_rule_locked(rule, NULL);
3140 lockmgr(&dyn_lock, LK_RELEASE);
3146 bzero(&dmsg, sizeof(dmsg));
3148 netmsg_init(nmsg, &curthread->td_msgport, 0,
3149 ipfw_alt_delete_ruleset_dispatch);
3150 dmsg.from_set = set;
3152 ifnet_domsg(&nmsg->nm_lmsg, 0);
3157 ipfw_alt_move_rule_dispatch(struct netmsg *nmsg)
3159 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3162 rule = dmsg->start_rule;
3163 KKASSERT(rule->cpuid == mycpuid);
3166 * Move to the position on the next CPU
3167 * before the msg is forwarded.
3169 dmsg->start_rule = rule->sibling;
3171 while (rule && rule->rulenum <= dmsg->rulenum) {
3172 if (rule->rulenum == dmsg->rulenum)
3173 rule->set = dmsg->to_set;
3176 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3180 ipfw_alt_move_rule(uint16_t rulenum, uint8_t set)
3182 struct netmsg_del dmsg;
3183 struct netmsg *nmsg;
3185 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3188 * Locate first rule to move
3190 for (rule = ctx->ipfw_layer3_chain; rule && rule->rulenum <= rulenum;
3191 rule = rule->next) {
3192 if (rule->rulenum == rulenum && rule->set != set)
3195 if (rule == NULL || rule->rulenum > rulenum)
3196 return 0; /* XXX error? */
3198 bzero(&dmsg, sizeof(dmsg));
3200 netmsg_init(nmsg, &curthread->td_msgport, 0,
3201 ipfw_alt_move_rule_dispatch);
3202 dmsg.start_rule = rule;
3203 dmsg.rulenum = rulenum;
3206 ifnet_domsg(&nmsg->nm_lmsg, 0);
3207 KKASSERT(dmsg.start_rule == NULL);
3212 ipfw_alt_move_ruleset_dispatch(struct netmsg *nmsg)
3214 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3215 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3218 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3219 if (rule->set == dmsg->from_set)
3220 rule->set = dmsg->to_set;
3222 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3226 ipfw_alt_move_ruleset(uint8_t from_set, uint8_t to_set)
3228 struct netmsg_del dmsg;
3229 struct netmsg *nmsg;
3231 bzero(&dmsg, sizeof(dmsg));
3233 netmsg_init(nmsg, &curthread->td_msgport, 0,
3234 ipfw_alt_move_ruleset_dispatch);
3235 dmsg.from_set = from_set;
3236 dmsg.to_set = to_set;
3238 ifnet_domsg(&nmsg->nm_lmsg, 0);
3243 ipfw_alt_swap_ruleset_dispatch(struct netmsg *nmsg)
3245 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3246 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3249 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3250 if (rule->set == dmsg->from_set)
3251 rule->set = dmsg->to_set;
3252 else if (rule->set == dmsg->to_set)
3253 rule->set = dmsg->from_set;
3255 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3259 ipfw_alt_swap_ruleset(uint8_t set1, uint8_t set2)
3261 struct netmsg_del dmsg;
3262 struct netmsg *nmsg;
3264 bzero(&dmsg, sizeof(dmsg));
3266 netmsg_init(nmsg, &curthread->td_msgport, 0,
3267 ipfw_alt_swap_ruleset_dispatch);
3268 dmsg.from_set = set1;
3271 ifnet_domsg(&nmsg->nm_lmsg, 0);
3276 * Remove all rules with given number, and also do set manipulation.
3278 * The argument is an uint32_t. The low 16 bit are the rule or set number,
3279 * the next 8 bits are the new set, the top 8 bits are the command:
3281 * 0 delete rules with given number
3282 * 1 delete rules with given set number
3283 * 2 move rules with given number to new set
3284 * 3 move rules with given set number to new set
3285 * 4 swap sets with given numbers
3288 ipfw_ctl_alter(uint32_t arg)
3291 uint8_t cmd, new_set;
3294 rulenum = arg & 0xffff;
3295 cmd = (arg >> 24) & 0xff;
3296 new_set = (arg >> 16) & 0xff;
3300 if (new_set >= IPFW_DEFAULT_SET)
3302 if (cmd == 0 || cmd == 2) {
3303 if (rulenum == IPFW_DEFAULT_RULE)
3306 if (rulenum >= IPFW_DEFAULT_SET)
3311 case 0: /* delete rules with given number */
3312 error = ipfw_alt_delete_rule(rulenum);
3315 case 1: /* delete all rules with given set number */
3316 error = ipfw_alt_delete_ruleset(rulenum);
3319 case 2: /* move rules with given number to new set */
3320 error = ipfw_alt_move_rule(rulenum, new_set);
3323 case 3: /* move rules with given set number to new set */
3324 error = ipfw_alt_move_ruleset(rulenum, new_set);
3327 case 4: /* swap two sets */
3328 error = ipfw_alt_swap_ruleset(rulenum, new_set);
3335 * Clear counters for a specific rule.
3338 clear_counters(struct ip_fw *rule, int log_only)
3340 ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
3342 if (log_only == 0) {
3343 rule->bcnt = rule->pcnt = 0;
3344 rule->timestamp = 0;
3346 if (l->o.opcode == O_LOG)
3347 l->log_left = l->max_log;
3351 ipfw_zero_entry_dispatch(struct netmsg *nmsg)
3353 struct netmsg_zent *zmsg = (struct netmsg_zent *)nmsg;
3354 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3357 if (zmsg->rulenum == 0) {
3358 KKASSERT(zmsg->start_rule == NULL);
3360 ctx->ipfw_norule_counter = 0;
3361 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next)
3362 clear_counters(rule, zmsg->log_only);
3364 struct ip_fw *start = zmsg->start_rule;
3366 KKASSERT(start->cpuid == mycpuid);
3367 KKASSERT(start->rulenum == zmsg->rulenum);
3370 * We can have multiple rules with the same number, so we
3371 * need to clear them all.
3373 for (rule = start; rule && rule->rulenum == zmsg->rulenum;
3375 clear_counters(rule, zmsg->log_only);
3378 * Move to the position on the next CPU
3379 * before the msg is forwarded.
3381 zmsg->start_rule = start->sibling;
3383 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
3387 * Reset some or all counters on firewall rules.
3388 * @arg frwl is null to clear all entries, or contains a specific
3390 * @arg log_only is 1 if we only want to reset logs, zero otherwise.
3393 ipfw_ctl_zero_entry(int rulenum, int log_only)
3395 struct netmsg_zent zmsg;
3396 struct netmsg *nmsg;
3398 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3400 bzero(&zmsg, sizeof(zmsg));
3402 netmsg_init(nmsg, &curthread->td_msgport, 0, ipfw_zero_entry_dispatch);
3403 zmsg.log_only = log_only;
3406 msg = log_only ? "ipfw: All logging counts reset.\n"
3407 : "ipfw: Accounting cleared.\n";
3412 * Locate the first rule with 'rulenum'
3414 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3415 if (rule->rulenum == rulenum)
3418 if (rule == NULL) /* we did not find any matching rules */
3420 zmsg.start_rule = rule;
3421 zmsg.rulenum = rulenum;
3423 msg = log_only ? "ipfw: Entry %d logging count reset.\n"
3424 : "ipfw: Entry %d cleared.\n";
3426 ifnet_domsg(&nmsg->nm_lmsg, 0);
3427 KKASSERT(zmsg.start_rule == NULL);
3430 log(LOG_SECURITY | LOG_NOTICE, msg, rulenum);
3435 * Check validity of the structure before insert.
3436 * Fortunately rules are simple, so this mostly need to check rule sizes.
3439 ipfw_check_ioc_rule(struct ipfw_ioc_rule *rule, int size, uint32_t *rule_flags)
3442 int have_action = 0;
3447 /* Check for valid size */
3448 if (size < sizeof(*rule)) {
3449 kprintf("ipfw: rule too short\n");
3452 l = IOC_RULESIZE(rule);
3454 kprintf("ipfw: size mismatch (have %d want %d)\n", size, l);
3458 /* Check rule number */
3459 if (rule->rulenum == IPFW_DEFAULT_RULE) {
3460 kprintf("ipfw: invalid rule number\n");
3465 * Now go for the individual checks. Very simple ones, basically only
3466 * instruction sizes.
3468 for (l = rule->cmd_len, cmd = rule->cmd; l > 0;
3469 l -= cmdlen, cmd += cmdlen) {
3470 cmdlen = F_LEN(cmd);
3472 kprintf("ipfw: opcode %d size truncated\n",
3477 DEB(kprintf("ipfw: opcode %d\n", cmd->opcode);)
3479 if (cmd->opcode == O_KEEP_STATE || cmd->opcode == O_LIMIT) {
3480 /* This rule will create states */
3481 *rule_flags |= IPFW_RULE_F_STATE;
3484 switch (cmd->opcode) {
3498 case O_IPPRECEDENCE:
3505 if (cmdlen != F_INSN_SIZE(ipfw_insn))
3517 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32))
3522 if (cmdlen != F_INSN_SIZE(ipfw_insn_limit))
3527 if (cmdlen != F_INSN_SIZE(ipfw_insn_log))
3530 ((ipfw_insn_log *)cmd)->log_left =
3531 ((ipfw_insn_log *)cmd)->max_log;
3537 if (cmdlen != F_INSN_SIZE(ipfw_insn_ip))
3539 if (((ipfw_insn_ip *)cmd)->mask.s_addr == 0) {
3540 kprintf("ipfw: opcode %d, useless rule\n",
3548 if (cmd->arg1 == 0 || cmd->arg1 > 256) {
3549 kprintf("ipfw: invalid set size %d\n",
3553 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
3559 if (cmdlen != F_INSN_SIZE(ipfw_insn_mac))
3565 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */
3566 if (cmdlen < 2 || cmdlen > 31)
3573 if (cmdlen != F_INSN_SIZE(ipfw_insn_if))
3579 if (cmdlen != F_INSN_SIZE(ipfw_insn_pipe))
3584 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa)) {
3589 fwd_addr = ((ipfw_insn_sa *)cmd)->
3591 if (IN_MULTICAST(ntohl(fwd_addr))) {
3592 kprintf("ipfw: try forwarding to "
3593 "multicast address\n");
3599 case O_FORWARD_MAC: /* XXX not implemented yet */
3608 if (cmdlen != F_INSN_SIZE(ipfw_insn))
3612 kprintf("ipfw: opcode %d, multiple actions"
3619 kprintf("ipfw: opcode %d, action must be"
3626 kprintf("ipfw: opcode %d, unknown opcode\n",
3631 if (have_action == 0) {
3632 kprintf("ipfw: missing action\n");
3638 kprintf("ipfw: opcode %d size %d wrong\n",
3639 cmd->opcode, cmdlen);
3644 ipfw_ctl_add_rule(struct sockopt *sopt)
3646 struct ipfw_ioc_rule *ioc_rule;
3648 uint32_t rule_flags;
3651 size = sopt->sopt_valsize;
3652 if (size > (sizeof(uint32_t) * IPFW_RULE_SIZE_MAX) ||
3653 size < sizeof(*ioc_rule)) {
3656 if (size != (sizeof(uint32_t) * IPFW_RULE_SIZE_MAX)) {
3657 sopt->sopt_val = krealloc(sopt->sopt_val, sizeof(uint32_t) *
3658 IPFW_RULE_SIZE_MAX, M_TEMP, M_WAITOK);
3660 ioc_rule = sopt->sopt_val;
3662 error = ipfw_check_ioc_rule(ioc_rule, size, &rule_flags);
3666 ipfw_add_rule(ioc_rule, rule_flags);
3668 if (sopt->sopt_dir == SOPT_GET)
3669 sopt->sopt_valsize = IOC_RULESIZE(ioc_rule);
3674 ipfw_copy_rule(const struct ip_fw *rule, struct ipfw_ioc_rule *ioc_rule)
3676 const struct ip_fw *sibling;
3681 KKASSERT(rule->cpuid == 0);
3683 ioc_rule->act_ofs = rule->act_ofs;
3684 ioc_rule->cmd_len = rule->cmd_len;
3685 ioc_rule->rulenum = rule->rulenum;
3686 ioc_rule->set = rule->set;
3687 ioc_rule->usr_flags = rule->usr_flags;
3689 ioc_rule->set_disable = ipfw_ctx[mycpuid]->ipfw_set_disable;
3690 ioc_rule->static_count = static_count;
3691 ioc_rule->static_len = static_ioc_len;
3694 * Visit (read-only) all of the rule's duplications to get
3695 * the necessary statistics
3702 ioc_rule->timestamp = 0;
3703 for (sibling = rule; sibling != NULL; sibling = sibling->sibling) {
3704 ioc_rule->pcnt += sibling->pcnt;
3705 ioc_rule->bcnt += sibling->bcnt;
3706 if (sibling->timestamp > ioc_rule->timestamp)
3707 ioc_rule->timestamp = sibling->timestamp;
3712 KASSERT(i == ncpus, ("static rule is not duplicated on every cpu\n"));
3714 bcopy(rule->cmd, ioc_rule->cmd, ioc_rule->cmd_len * 4 /* XXX */);
3716 return ((uint8_t *)ioc_rule + IOC_RULESIZE(ioc_rule));
3720 ipfw_copy_state(const ipfw_dyn_rule *dyn_rule,
3721 struct ipfw_ioc_state *ioc_state)
3723 const struct ipfw_flow_id *id;
3724 struct ipfw_ioc_flowid *ioc_id;
3726 ioc_state->expire = TIME_LEQ(dyn_rule->expire, time_second) ?
3727 0 : dyn_rule->expire - time_second;
3728 ioc_state->pcnt = dyn_rule->pcnt;
3729 ioc_state->bcnt = dyn_rule->bcnt;
3731 ioc_state->dyn_type = dyn_rule->dyn_type;
3732 ioc_state->count = dyn_rule->count;
3734 ioc_state->rulenum = dyn_rule->stub->rule[mycpuid]->rulenum;
3737 ioc_id = &ioc_state->id;
3739 ioc_id->type = ETHERTYPE_IP;
3740 ioc_id->u.ip.dst_ip = id->dst_ip;
3741 ioc_id->u.ip.src_ip = id->src_ip;
3742 ioc_id->u.ip.dst_port = id->dst_port;
3743 ioc_id->u.ip.src_port = id->src_port;
3744 ioc_id->u.ip.proto = id->proto;
3748 ipfw_ctl_get_rules(struct sockopt *sopt)
3750 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3754 uint32_t dcount = 0;
3757 * pass up a copy of the current rules. Static rules
3758 * come first (the last of which has number IPFW_DEFAULT_RULE),
3759 * followed by a possibly empty list of dynamic rule.
3763 size = static_ioc_len; /* size of static rules */
3764 if (ipfw_dyn_v) { /* add size of dyn.rules */
3766 size += dcount * sizeof(struct ipfw_ioc_state);
3769 if (sopt->sopt_valsize < size) {
3770 /* short length, no need to return incomplete rules */
3771 /* XXX: if superuser, no need to zero buffer */
3772 bzero(sopt->sopt_val, sopt->sopt_valsize);
3775 bp = sopt->sopt_val;
3777 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next)
3778 bp = ipfw_copy_rule(rule, bp);
3780 if (ipfw_dyn_v && dcount != 0) {
3781 struct ipfw_ioc_state *ioc_state = bp;
3782 uint32_t dcount2 = 0;
3784 size_t old_size = size;
3788 lockmgr(&dyn_lock, LK_SHARED);
3790 /* Check 'ipfw_dyn_v' again with lock held */
3791 if (ipfw_dyn_v == NULL)
3794 for (i = 0; i < curr_dyn_buckets; i++) {
3798 * The # of dynamic rules may have grown after the
3799 * snapshot of 'dyn_count' was taken, so we will have
3800 * to check 'dcount' (snapshot of dyn_count) here to
3801 * make sure that we don't overflow the pre-allocated
3804 for (p = ipfw_dyn_v[i]; p != NULL && dcount != 0;
3805 p = p->next, ioc_state++, dcount--, dcount2++)
3806 ipfw_copy_state(p, ioc_state);
3809 lockmgr(&dyn_lock, LK_RELEASE);
3812 * The # of dynamic rules may be shrinked after the
3813 * snapshot of 'dyn_count' was taken. To give user a
3814 * correct dynamic rule count, we use the 'dcount2'
3815 * calculated above (with shared lockmgr lock held).
3817 size = static_ioc_len +
3818 (dcount2 * sizeof(struct ipfw_ioc_state));
3819 KKASSERT(size <= old_size);
3824 sopt->sopt_valsize = size;
3829 ipfw_set_disable_dispatch(struct netmsg *nmsg)
3831 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
3832 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3835 ctx->ipfw_set_disable = lmsg->u.ms_result32;
3837 ifnet_forwardmsg(lmsg, mycpuid + 1);
3841 ipfw_ctl_set_disable(uint32_t disable, uint32_t enable)
3844 struct lwkt_msg *lmsg;
3845 uint32_t set_disable;
3847 /* IPFW_DEFAULT_SET is always enabled */
3848 enable |= (1 << IPFW_DEFAULT_SET);
3849 set_disable = (ipfw_ctx[mycpuid]->ipfw_set_disable | disable) & ~enable;
3851 bzero(&nmsg, sizeof(nmsg));
3852 netmsg_init(&nmsg, &curthread->td_msgport, 0, ipfw_set_disable_dispatch);
3853 lmsg = &nmsg.nm_lmsg;
3854 lmsg->u.ms_result32 = set_disable;
3856 ifnet_domsg(lmsg, 0);
3860 * {set|get}sockopt parser.
3863 ipfw_ctl(struct sockopt *sopt)
3871 switch (sopt->sopt_name) {
3873 error = ipfw_ctl_get_rules(sopt);
3878 * Normally we cannot release the lock on each iteration.
3879 * We could do it here only because we start from the head all
3880 * the times so there is no risk of missing some entries.
3881 * On the other hand, the risk is that we end up with
3882 * a very inconsistent ruleset, so better keep the lock
3883 * around the whole cycle.
3885 * XXX this code can be improved by resetting the head of
3886 * the list to point to the default rule, and then freeing
3887 * the old list without the need for a lock.
3891 ipfw_flush(0 /* keep default rule */);
3896 error = ipfw_ctl_add_rule(sopt);
3901 * IP_FW_DEL is used for deleting single rules or sets,
3902 * and (ab)used to atomically manipulate sets.
3903 * Argument size is used to distinguish between the two:
3905 * delete single rule or set of rules,
3906 * or reassign rules (or sets) to a different set.
3907 * 2 * sizeof(uint32_t)
3908 * atomic disable/enable sets.
3909 * first uint32_t contains sets to be disabled,
3910 * second uint32_t contains sets to be enabled.
3912 masks = sopt->sopt_val;
3913 size = sopt->sopt_valsize;
3914 if (size == sizeof(*masks)) {
3916 * Delete or reassign static rule
3918 error = ipfw_ctl_alter(masks[0]);
3919 } else if (size == (2 * sizeof(*masks))) {
3921 * Set enable/disable
3923 ipfw_ctl_set_disable(masks[0], masks[1]);
3930 case IP_FW_RESETLOG: /* argument is an int, the rule number */
3933 if (sopt->sopt_val != 0) {
3934 error = soopt_to_kbuf(sopt, &rulenum,
3935 sizeof(int), sizeof(int));
3939 error = ipfw_ctl_zero_entry(rulenum,
3940 sopt->sopt_name == IP_FW_RESETLOG);
3944 kprintf("ipfw_ctl invalid option %d\n", sopt->sopt_name);
3951 * This procedure is only used to handle keepalives. It is invoked
3952 * every dyn_keepalive_period
3955 ipfw_tick(void *dummy __unused)
3961 if (ipfw_dyn_v == NULL || dyn_count == 0)
3964 keep_alive = time_second;
3966 lockmgr(&dyn_lock, LK_EXCLUSIVE);
3968 if (ipfw_dyn_v == NULL || dyn_count == 0) {
3969 lockmgr(&dyn_lock, LK_RELEASE);
3972 gen = dyn_buckets_gen;
3974 for (i = 0; i < curr_dyn_buckets; i++) {
3975 ipfw_dyn_rule *q, *prev;
3977 for (prev = NULL, q = ipfw_dyn_v[i]; q != NULL;) {
3978 uint32_t ack_rev, ack_fwd;
3979 struct ipfw_flow_id id;
3981 if (q->dyn_type == O_LIMIT_PARENT)
3984 if (TIME_LEQ(q->expire, time_second)) {
3986 UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q);
3991 * Keep alive processing
3996 if (q->id.proto != IPPROTO_TCP)
3998 if ((q->state & BOTH_SYN) != BOTH_SYN)
4000 if (TIME_LEQ(time_second + dyn_keepalive_interval,
4002 goto next; /* too early */
4003 if (q->keep_alive == keep_alive)
4004 goto next; /* alreay done */
4007 * Save necessary information, so that they could
4008 * survive after possible blocking in send_pkt()
4011 ack_rev = q->ack_rev;
4012 ack_fwd = q->ack_fwd;
4014 /* Sending has been started */
4015 q->keep_alive = keep_alive;
4017 /* Release lock to avoid possible dead lock */
4018 lockmgr(&dyn_lock, LK_RELEASE);
4019 send_pkt(&id, ack_rev - 1, ack_fwd, TH_SYN);
4020 send_pkt(&id, ack_fwd - 1, ack_rev, 0);
4021 lockmgr(&dyn_lock, LK_EXCLUSIVE);
4023 if (gen != dyn_buckets_gen) {
4025 * Dyn bucket array has been changed during
4026 * the above two sending; reiterate.
4035 lockmgr(&dyn_lock, LK_RELEASE);
4037 callout_reset(&ipfw_timeout_h, dyn_keepalive_period * hz,
4042 ipfw_check_in(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir)
4044 struct ip_fw_args args;
4045 struct mbuf *m = *m0;
4047 int tee = 0, error = 0, i;
4049 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
4050 /* Extract info from dummynet tag */
4051 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
4052 KKASSERT(mtag != NULL);
4053 args.rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
4054 KKASSERT(args.rule != NULL);
4056 m_tag_delete(m, mtag);
4057 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
4065 i = ipfw_chk(&args);
4083 case IP_FW_DUMMYNET:
4084 /* Send packet to the appropriate pipe */
4085 ipfw_dummynet_io(m, args.cookie, DN_TO_IP_IN, &args);
4093 if (ip_divert_p != NULL) {
4094 m = ip_divert_p(m, tee, 1);
4098 /* not sure this is the right error msg */
4104 panic("unknown ipfw return value: %d\n", i);
4112 ipfw_check_out(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir)
4114 struct ip_fw_args args;
4115 struct mbuf *m = *m0;
4117 int tee = 0, error = 0, off;
4119 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
4120 /* Extract info from dummynet tag */
4121 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
4122 KKASSERT(mtag != NULL);
4123 args.rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
4124 KKASSERT(args.rule != NULL);
4126 m_tag_delete(m, mtag);
4127 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
4135 off = ipfw_chk(&args);
4153 case IP_FW_DUMMYNET:
4154 ipfw_dummynet_io(m, args.cookie, DN_TO_IP_OUT, &args);
4162 if (ip_divert_p != NULL) {
4163 m = ip_divert_p(m, tee, 0);
4167 /* not sure this is the right error msg */
4173 panic("unknown ipfw return value: %d\n", off);
4183 struct pfil_head *pfh;
4185 IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
4187 pfh = pfil_head_get(PFIL_TYPE_AF, AF_INET);
4191 pfil_add_hook(ipfw_check_in, NULL, PFIL_IN, pfh);
4192 pfil_add_hook(ipfw_check_out, NULL, PFIL_OUT, pfh);
4198 struct pfil_head *pfh;
4200 IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
4202 pfh = pfil_head_get(PFIL_TYPE_AF, AF_INET);
4206 pfil_remove_hook(ipfw_check_in, NULL, PFIL_IN, pfh);
4207 pfil_remove_hook(ipfw_check_out, NULL, PFIL_OUT, pfh);
4211 ipfw_sysctl_enable_dispatch(struct netmsg *nmsg)
4213 struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
4214 int enable = lmsg->u.ms_result;
4216 if (fw_enable == enable)
4225 lwkt_replymsg(lmsg, 0);
4229 ipfw_sysctl_enable(SYSCTL_HANDLER_ARGS)
4232 struct lwkt_msg *lmsg;
4236 error = sysctl_handle_int(oidp, &enable, 0, req);
4237 if (error || req->newptr == NULL)
4240 netmsg_init(&nmsg, &curthread->td_msgport, 0,
4241 ipfw_sysctl_enable_dispatch);
4242 lmsg = &nmsg.nm_lmsg;
4243 lmsg->u.ms_result = enable;
4245 return lwkt_domsg(IPFW_CFGPORT, lmsg, 0);
4249 ipfw_sysctl_autoinc_step(SYSCTL_HANDLER_ARGS)
4251 return sysctl_int_range(oidp, arg1, arg2, req,
4252 IPFW_AUTOINC_STEP_MIN, IPFW_AUTOINC_STEP_MAX);
4256 ipfw_sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS)
4260 lockmgr(&dyn_lock, LK_EXCLUSIVE);
4262 value = dyn_buckets;
4263 error = sysctl_handle_int(oidp, &value, 0, req);
4264 if (error || !req->newptr)
4268 * Make sure we have a power of 2 and
4269 * do not allow more than 64k entries.
4272 if (value <= 1 || value > 65536)
4274 if ((value & (value - 1)) != 0)
4278 dyn_buckets = value;
4280 lockmgr(&dyn_lock, LK_RELEASE);
4285 ipfw_sysctl_dyn_fin(SYSCTL_HANDLER_ARGS)
4287 return sysctl_int_range(oidp, arg1, arg2, req,
4288 1, dyn_keepalive_period - 1);
4292 ipfw_sysctl_dyn_rst(SYSCTL_HANDLER_ARGS)
4294 return sysctl_int_range(oidp, arg1, arg2, req,
4295 1, dyn_keepalive_period - 1);
4299 ipfw_ctx_init_dispatch(struct netmsg *nmsg)
4301 struct netmsg_ipfw *fwmsg = (struct netmsg_ipfw *)nmsg;
4302 struct ipfw_context *ctx;
4303 struct ip_fw *def_rule;
4305 ctx = kmalloc(sizeof(*ctx), M_IPFW, M_WAITOK | M_ZERO);
4306 ipfw_ctx[mycpuid] = ctx;
4308 def_rule = kmalloc(sizeof(*def_rule), M_IPFW, M_WAITOK | M_ZERO);
4310 def_rule->act_ofs = 0;
4311 def_rule->rulenum = IPFW_DEFAULT_RULE;
4312 def_rule->cmd_len = 1;
4313 def_rule->set = IPFW_DEFAULT_SET;
4315 def_rule->cmd[0].len = 1;
4316 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
4317 def_rule->cmd[0].opcode = O_ACCEPT;
4319 def_rule->cmd[0].opcode = O_DENY;
4322 def_rule->refcnt = 1;
4323 def_rule->cpuid = mycpuid;
4325 /* Install the default rule */
4326 ctx->ipfw_default_rule = def_rule;
4327 ctx->ipfw_layer3_chain = def_rule;
4329 /* Link rule CPU sibling */
4330 ipfw_link_sibling(fwmsg, def_rule);
4332 /* Statistics only need to be updated once */
4334 ipfw_inc_static_count(def_rule);
4336 ifnet_forwardmsg(&nmsg->nm_lmsg, mycpuid + 1);
4340 ipfw_init_dispatch(struct netmsg *nmsg)
4342 struct netmsg_ipfw fwmsg;
4348 kprintf("IP firewall already loaded\n");
4353 bzero(&fwmsg, sizeof(fwmsg));
4354 netmsg_init(&fwmsg.nmsg, &curthread->td_msgport, 0,
4355 ipfw_ctx_init_dispatch);
4356 ifnet_domsg(&fwmsg.nmsg.nm_lmsg, 0);
4358 ip_fw_chk_ptr = ipfw_chk;
4359 ip_fw_ctl_ptr = ipfw_ctl;
4360 ip_fw_dn_io_ptr = ipfw_dummynet_io;
4362 kprintf("ipfw2 initialized, default to %s, logging ",
4363 ipfw_ctx[mycpuid]->ipfw_default_rule->cmd[0].opcode ==
4364 O_ACCEPT ? "accept" : "deny");
4366 #ifdef IPFIREWALL_VERBOSE
4369 #ifdef IPFIREWALL_VERBOSE_LIMIT
4370 verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
4372 if (fw_verbose == 0) {
4373 kprintf("disabled\n");
4374 } else if (verbose_limit == 0) {
4375 kprintf("unlimited\n");
4377 kprintf("limited to %d packets/entry by default\n",
4381 callout_init(&ipfw_timeout_h);
4382 lockinit(&dyn_lock, "ipfw_dyn", 0, 0);
4385 callout_reset(&ipfw_timeout_h, hz, ipfw_tick, NULL);
4391 lwkt_replymsg(&nmsg->nm_lmsg, error);
4399 netmsg_init(&smsg, &curthread->td_msgport, 0, ipfw_init_dispatch);
4400 return lwkt_domsg(IPFW_CFGPORT, &smsg.nm_lmsg, 0);
4406 ipfw_fini_dispatch(struct netmsg *nmsg)
4412 if (ipfw_refcnt != 0) {
4419 callout_stop(&ipfw_timeout_h);
4422 netmsg_service_sync();
4424 ip_fw_chk_ptr = NULL;
4425 ip_fw_ctl_ptr = NULL;
4426 ip_fw_dn_io_ptr = NULL;
4427 ipfw_flush(1 /* kill default rule */);
4429 /* Free pre-cpu context */
4430 for (cpu = 0; cpu < ncpus; ++cpu)
4431 kfree(ipfw_ctx[cpu], M_IPFW);
4433 kprintf("IP firewall unloaded\n");
4436 lwkt_replymsg(&nmsg->nm_lmsg, error);
4444 netmsg_init(&smsg, &curthread->td_msgport, 0, ipfw_fini_dispatch);
4445 return lwkt_domsg(IPFW_CFGPORT, &smsg.nm_lmsg, 0);
4448 #endif /* KLD_MODULE */
4451 ipfw_modevent(module_t mod, int type, void *unused)
4462 kprintf("ipfw statically compiled, cannot unload\n");
4474 static moduledata_t ipfwmod = {
4479 DECLARE_MODULE(ipfw, ipfwmod, SI_SUB_PROTO_END, SI_ORDER_ANY);
4480 MODULE_VERSION(ipfw, 1);