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 $
29 * Implement IP packet firewall (new version)
35 #error IPFIREWALL requires INET.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/kernel.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
48 #include <sys/ucred.h>
49 #include <sys/in_cksum.h>
53 #include <net/route.h>
55 #include <net/dummynet/ip_dummynet.h>
57 #include <sys/thread2.h>
58 #include <sys/mplock2.h>
59 #include <net/netmsg2.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/ip_icmp.h>
68 #include <netinet/tcp.h>
69 #include <netinet/tcp_timer.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/tcpip.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
74 #include <netinet/ip_divert.h>
75 #include <netinet/if_ether.h> /* XXX for ETHERTYPE_IP */
77 #include <net/ipfw/ip_fw2.h>
79 #ifdef IPFIREWALL_DEBUG
80 #define DPRINTF(fmt, ...) \
83 kprintf(fmt, __VA_ARGS__); \
86 #define DPRINTF(fmt, ...) ((void)0)
90 * Description about per-CPU rule duplication:
92 * Module loading/unloading and all ioctl operations are serialized
93 * by netisr0, so we don't have any ordering or locking problems.
95 * Following graph shows how operation on per-CPU rule list is
96 * performed [2 CPU case]:
100 * netisr0 <------------------------------------+
106 * forwardmsg---------->netisr1 |
111 * replymsg--------------+
115 * Rules which will not create states (dyn rules) [2 CPU case]
119 * layer3_chain layer3_chain
122 * +-------+ sibling +-------+ sibling
123 * | rule1 |--------->| rule1 |--------->NULL
124 * +-------+ +-------+
128 * +-------+ sibling +-------+ sibling
129 * | rule2 |--------->| rule2 |--------->NULL
130 * +-------+ +-------+
133 * 1) Ease statistics calculation during IP_FW_GET. We only need to
134 * iterate layer3_chain in netisr0; the current rule's duplication
135 * to the other CPUs could safely be read-only accessed through
137 * 2) Accelerate rule insertion and deletion, e.g. rule insertion:
138 * a) In netisr0 rule3 is determined to be inserted between rule1
139 * and rule2. To make this decision we need to iterate the
140 * layer3_chain in netisr0. The netmsg, which is used to insert
141 * the rule, will contain rule1 in netisr0 as prev_rule and rule2
142 * in netisr0 as next_rule.
143 * b) After the insertion in netisr0 is done, we will move on to
144 * netisr1. But instead of relocating the rule3's position in
145 * netisr1 by iterating the layer3_chain in netisr1, we set the
146 * netmsg's prev_rule to rule1->sibling and next_rule to
147 * rule2->sibling before the netmsg is forwarded to netisr1 from
152 * Rules which will create states (dyn rules) [2 CPU case]
153 * (unnecessary parts are omitted; they are same as in the previous figure)
157 * +-------+ +-------+
158 * | rule1 | | rule1 |
159 * +-------+ +-------+
166 * | +--------------------+ |
168 * | | (read-only shared) | |
170 * | | back pointer array | |
171 * | | (indexed by cpuid) | |
173 * +----|---------[0] | |
174 * | [1]--------|----+
176 * +--------------------+
179 * ........|............|............
183 * : +---------+ +---------+ :
184 * : | state1a | | state1b | .... :
185 * : +---------+ +---------+ :
189 * : (protected by dyn_lock) :
190 * ..................................
192 * [state1a and state1b are states created by rule1]
195 * This structure is introduced so that shared (locked) state table could
196 * work with per-CPU (duplicated) static rules. It mainly bridges states
197 * and static rules and serves as static rule's place holder (a read-only
198 * shared part of duplicated rules) from states point of view.
200 * IPFW_RULE_F_STATE (only for rules which create states):
201 * o During rule installation, this flag is turned on after rule's
202 * duplications reach all CPUs, to avoid at least following race:
203 * 1) rule1 is duplicated on CPU0 and is not duplicated on CPU1 yet
204 * 2) rule1 creates state1
205 * 3) state1 is located on CPU1 by check-state
206 * But rule1 is not duplicated on CPU1 yet
207 * o During rule deletion, this flag is turned off before deleting states
208 * created by the rule and before deleting the rule itself, so no
209 * more states will be created by the to-be-deleted rule even when its
210 * duplication on certain CPUs are not eliminated yet.
213 #define IPFW_AUTOINC_STEP_MIN 1
214 #define IPFW_AUTOINC_STEP_MAX 1000
215 #define IPFW_AUTOINC_STEP_DEF 100
217 #define IPFW_DEFAULT_RULE 65535 /* rulenum for the default rule */
218 #define IPFW_DEFAULT_SET 31 /* set number for the default rule */
221 struct netmsg_base base;
222 const struct ipfw_ioc_rule *ioc_rule;
223 struct ip_fw *next_rule;
224 struct ip_fw *prev_rule;
225 struct ip_fw *sibling;
226 struct ip_fw_stub *stub;
230 struct netmsg_base base;
231 struct ip_fw *start_rule;
232 struct ip_fw *prev_rule;
239 struct netmsg_base base;
240 struct ip_fw *start_rule;
245 struct ipfw_context {
246 struct ip_fw *ipfw_layer3_chain; /* list of rules for layer3 */
247 struct ip_fw *ipfw_default_rule; /* default rule */
248 uint64_t ipfw_norule_counter; /* counter for ipfw_log(NULL) */
251 * ipfw_set_disable contains one bit per set value (0..31).
252 * If the bit is set, all rules with the corresponding set
253 * are disabled. Set IPDW_DEFAULT_SET is reserved for the
254 * default rule and CANNOT be disabled.
256 uint32_t ipfw_set_disable;
259 static struct ipfw_context *ipfw_ctx[MAXCPU];
263 * Module can not be unloaded, if there are references to
264 * certains rules of ipfw(4), e.g. dummynet(4)
266 static int ipfw_refcnt;
269 MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");
272 * Following two global variables are accessed and updated only
275 static uint32_t static_count; /* # of static rules */
276 static uint32_t static_ioc_len; /* bytes of static rules */
279 * If 1, then ipfw static rules are being flushed,
280 * ipfw_chk() will skip to the default rule.
282 static int ipfw_flushing;
284 static int fw_verbose;
285 static int verbose_limit;
288 static int autoinc_step = IPFW_AUTOINC_STEP_DEF;
290 static int ipfw_sysctl_enable(SYSCTL_HANDLER_ARGS);
291 static int ipfw_sysctl_autoinc_step(SYSCTL_HANDLER_ARGS);
292 static int ipfw_sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS);
293 static int ipfw_sysctl_dyn_fin(SYSCTL_HANDLER_ARGS);
294 static int ipfw_sysctl_dyn_rst(SYSCTL_HANDLER_ARGS);
296 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
297 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, enable, CTLTYPE_INT | CTLFLAG_RW,
298 &fw_enable, 0, ipfw_sysctl_enable, "I", "Enable ipfw");
299 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, autoinc_step, CTLTYPE_INT | CTLFLAG_RW,
300 &autoinc_step, 0, ipfw_sysctl_autoinc_step, "I",
301 "Rule number autincrement step");
302 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO,one_pass,CTLFLAG_RW,
304 "Only do a single pass through ipfw when using dummynet(4)");
305 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, debug, CTLFLAG_RW,
306 &fw_debug, 0, "Enable printing of debug ip_fw statements");
307 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose, CTLFLAG_RW,
308 &fw_verbose, 0, "Log matches to ipfw rules");
309 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, CTLFLAG_RW,
310 &verbose_limit, 0, "Set upper limit of matches of ipfw rules logged");
313 * Description of dynamic rules.
315 * Dynamic rules are stored in lists accessed through a hash table
316 * (ipfw_dyn_v) whose size is curr_dyn_buckets. This value can
317 * be modified through the sysctl variable dyn_buckets which is
318 * updated when the table becomes empty.
320 * XXX currently there is only one list, ipfw_dyn.
322 * When a packet is received, its address fields are first masked
323 * with the mask defined for the rule, then hashed, then matched
324 * against the entries in the corresponding list.
325 * Dynamic rules can be used for different purposes:
327 * + enforcing limits on the number of sessions;
328 * + in-kernel NAT (not implemented yet)
330 * The lifetime of dynamic rules is regulated by dyn_*_lifetime,
331 * measured in seconds and depending on the flags.
333 * The total number of dynamic rules is stored in dyn_count.
334 * The max number of dynamic rules is dyn_max. When we reach
335 * the maximum number of rules we do not create anymore. This is
336 * done to avoid consuming too much memory, but also too much
337 * time when searching on each packet (ideally, we should try instead
338 * to put a limit on the length of the list on each bucket...).
340 * Each dynamic rule holds a pointer to the parent ipfw rule so
341 * we know what action to perform. Dynamic rules are removed when
342 * the parent rule is deleted. XXX we should make them survive.
344 * There are some limitations with dynamic rules -- we do not
345 * obey the 'randomized match', and we do not do multiple
346 * passes through the firewall. XXX check the latter!!!
348 * NOTE about the SHARED LOCKMGR LOCK during dynamic rule looking up:
349 * Only TCP state transition will change dynamic rule's state and ack
350 * sequences, while all packets of one TCP connection only goes through
351 * one TCP thread, so it is safe to use shared lockmgr lock during dynamic
352 * rule looking up. The keep alive callout uses exclusive lockmgr lock
353 * when it tries to find suitable dynamic rules to send keep alive, so
354 * it will not see half updated state and ack sequences. Though the expire
355 * field updating looks racy for other protocols, the resolution (second)
356 * of expire field makes this kind of race harmless.
357 * XXX statistics' updating is _not_ MPsafe!!!
358 * XXX once UDP output path is fixed, we could use lockless dynamic rule
361 static ipfw_dyn_rule **ipfw_dyn_v = NULL;
362 static uint32_t dyn_buckets = 256; /* must be power of 2 */
363 static uint32_t curr_dyn_buckets = 256; /* must be power of 2 */
364 static uint32_t dyn_buckets_gen; /* generation of dyn buckets array */
365 static struct lock dyn_lock; /* dynamic rules' hash table lock */
367 static struct netmsg_base ipfw_timeout_netmsg; /* schedule ipfw timeout */
368 static struct callout ipfw_timeout_h;
371 * Timeouts for various events in handing dynamic rules.
375 * 2 == 1~2 second(s).
377 * We use 2 seconds for FIN lifetime, so that the states will not be
378 * ripped prematurely.
380 static uint32_t dyn_ack_lifetime = 300;
381 static uint32_t dyn_syn_lifetime = 20;
382 static uint32_t dyn_fin_lifetime = 2;
383 static uint32_t dyn_rst_lifetime = 1;
384 static uint32_t dyn_udp_lifetime = 10;
385 static uint32_t dyn_short_lifetime = 5;
388 * Keepalives are sent if dyn_keepalive is set. They are sent every
389 * dyn_keepalive_period seconds, in the last dyn_keepalive_interval
390 * seconds of lifetime of a rule.
391 * dyn_rst_lifetime and dyn_fin_lifetime should be strictly lower
392 * than dyn_keepalive_period.
395 static uint32_t dyn_keepalive_interval = 20;
396 static uint32_t dyn_keepalive_period = 5;
397 static uint32_t dyn_keepalive = 1; /* do send keepalives */
399 static uint32_t dyn_count; /* # of dynamic rules */
400 static uint32_t dyn_max = 4096; /* max # of dynamic rules */
402 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_buckets, CTLTYPE_INT | CTLFLAG_RW,
403 &dyn_buckets, 0, ipfw_sysctl_dyn_buckets, "I", "Number of dyn. buckets");
404 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets, CTLFLAG_RD,
405 &curr_dyn_buckets, 0, "Current Number of dyn. buckets");
406 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_count, CTLFLAG_RD,
407 &dyn_count, 0, "Number of dyn. rules");
408 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_max, CTLFLAG_RW,
409 &dyn_max, 0, "Max number of dyn. rules");
410 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count, CTLFLAG_RD,
411 &static_count, 0, "Number of static rules");
412 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime, CTLFLAG_RW,
413 &dyn_ack_lifetime, 0, "Lifetime of dyn. rules for acks");
414 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime, CTLFLAG_RW,
415 &dyn_syn_lifetime, 0, "Lifetime of dyn. rules for syn");
416 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime,
417 CTLTYPE_INT | CTLFLAG_RW, &dyn_fin_lifetime, 0, ipfw_sysctl_dyn_fin, "I",
418 "Lifetime of dyn. rules for fin");
419 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime,
420 CTLTYPE_INT | CTLFLAG_RW, &dyn_rst_lifetime, 0, ipfw_sysctl_dyn_rst, "I",
421 "Lifetime of dyn. rules for rst");
422 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime, CTLFLAG_RW,
423 &dyn_udp_lifetime, 0, "Lifetime of dyn. rules for UDP");
424 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime, CTLFLAG_RW,
425 &dyn_short_lifetime, 0, "Lifetime of dyn. rules for other situations");
426 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_keepalive, CTLFLAG_RW,
427 &dyn_keepalive, 0, "Enable keepalives for dyn. rules");
429 static ip_fw_chk_t ipfw_chk;
430 static void ipfw_tick(void *);
433 ipfw_free_rule(struct ip_fw *rule)
435 KASSERT(rule->cpuid == mycpuid, ("rule freed on cpu%d", mycpuid));
436 KASSERT(rule->refcnt > 0, ("invalid refcnt %u", rule->refcnt));
438 if (rule->refcnt == 0) {
446 ipfw_unref_rule(void *priv)
448 ipfw_free_rule(priv);
450 atomic_subtract_int(&ipfw_refcnt, 1);
455 ipfw_ref_rule(struct ip_fw *rule)
457 KASSERT(rule->cpuid == mycpuid, ("rule used on cpu%d", mycpuid));
459 atomic_add_int(&ipfw_refcnt, 1);
465 * This macro maps an ip pointer into a layer3 header pointer of type T
467 #define L3HDR(T, ip) ((T *)((uint32_t *)(ip) + (ip)->ip_hl))
470 icmptype_match(struct ip *ip, ipfw_insn_u32 *cmd)
472 int type = L3HDR(struct icmp,ip)->icmp_type;
474 return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1 << type)));
477 #define TT ((1 << ICMP_ECHO) | \
478 (1 << ICMP_ROUTERSOLICIT) | \
479 (1 << ICMP_TSTAMP) | \
484 is_icmp_query(struct ip *ip)
486 int type = L3HDR(struct icmp, ip)->icmp_type;
488 return (type <= ICMP_MAXTYPE && (TT & (1 << type)));
494 * The following checks use two arrays of 8 or 16 bits to store the
495 * bits that we want set or clear, respectively. They are in the
496 * low and high half of cmd->arg1 or cmd->d[0].
498 * We scan options and store the bits we find set. We succeed if
500 * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear
502 * The code is sometimes optimized not to store additional variables.
505 flags_match(ipfw_insn *cmd, uint8_t bits)
510 if (((cmd->arg1 & 0xff) & bits) != 0)
511 return 0; /* some bits we want set were clear */
513 want_clear = (cmd->arg1 >> 8) & 0xff;
514 if ((want_clear & bits) != want_clear)
515 return 0; /* some bits we want clear were set */
520 ipopts_match(struct ip *ip, ipfw_insn *cmd)
522 int optlen, bits = 0;
523 u_char *cp = (u_char *)(ip + 1);
524 int x = (ip->ip_hl << 2) - sizeof(struct ip);
526 for (; x > 0; x -= optlen, cp += optlen) {
527 int opt = cp[IPOPT_OPTVAL];
529 if (opt == IPOPT_EOL)
532 if (opt == IPOPT_NOP) {
535 optlen = cp[IPOPT_OLEN];
536 if (optlen <= 0 || optlen > x)
537 return 0; /* invalid or truncated */
542 bits |= IP_FW_IPOPT_LSRR;
546 bits |= IP_FW_IPOPT_SSRR;
550 bits |= IP_FW_IPOPT_RR;
554 bits |= IP_FW_IPOPT_TS;
561 return (flags_match(cmd, bits));
565 tcpopts_match(struct ip *ip, ipfw_insn *cmd)
567 int optlen, bits = 0;
568 struct tcphdr *tcp = L3HDR(struct tcphdr,ip);
569 u_char *cp = (u_char *)(tcp + 1);
570 int x = (tcp->th_off << 2) - sizeof(struct tcphdr);
572 for (; x > 0; x -= optlen, cp += optlen) {
575 if (opt == TCPOPT_EOL)
578 if (opt == TCPOPT_NOP) {
588 bits |= IP_FW_TCPOPT_MSS;
592 bits |= IP_FW_TCPOPT_WINDOW;
595 case TCPOPT_SACK_PERMITTED:
597 bits |= IP_FW_TCPOPT_SACK;
600 case TCPOPT_TIMESTAMP:
601 bits |= IP_FW_TCPOPT_TS;
607 bits |= IP_FW_TCPOPT_CC;
614 return (flags_match(cmd, bits));
618 iface_match(struct ifnet *ifp, ipfw_insn_if *cmd)
620 if (ifp == NULL) /* no iface with this packet, match fails */
623 /* Check by name or by IP address */
624 if (cmd->name[0] != '\0') { /* match by name */
627 if (kfnmatch(cmd->name, ifp->if_xname, 0) == 0)
630 if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0)
634 struct ifaddr_container *ifac;
636 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
637 struct ifaddr *ia = ifac->ifa;
639 if (ia->ifa_addr == NULL)
641 if (ia->ifa_addr->sa_family != AF_INET)
643 if (cmd->p.ip.s_addr == ((struct sockaddr_in *)
644 (ia->ifa_addr))->sin_addr.s_addr)
645 return(1); /* match */
648 return(0); /* no match, fail ... */
651 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
654 * We enter here when we have a rule with O_LOG.
655 * XXX this function alone takes about 2Kbytes of code!
658 ipfw_log(struct ip_fw *f, u_int hlen, struct ether_header *eh,
659 struct mbuf *m, struct ifnet *oif)
662 int limit_reached = 0;
663 char action2[40], proto[48], fragment[28], abuf[INET_ADDRSTRLEN];
668 if (f == NULL) { /* bogus pkt */
669 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
671 if (verbose_limit != 0 &&
672 ctx->ipfw_norule_counter >= verbose_limit)
674 ctx->ipfw_norule_counter++;
675 if (ctx->ipfw_norule_counter == verbose_limit)
676 limit_reached = verbose_limit;
678 } else { /* O_LOG is the first action, find the real one */
679 ipfw_insn *cmd = ACTION_PTR(f);
680 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
682 if (l->max_log != 0 && l->log_left == 0)
685 if (l->log_left == 0)
686 limit_reached = l->max_log;
687 cmd += F_LEN(cmd); /* point to first action */
688 if (cmd->opcode == O_PROB)
692 switch (cmd->opcode) {
698 if (cmd->arg1==ICMP_REJECT_RST) {
700 } else if (cmd->arg1==ICMP_UNREACH_HOST) {
703 ksnprintf(SNPARGS(action2, 0), "Unreach %d",
717 ksnprintf(SNPARGS(action2, 0), "Divert %d", cmd->arg1);
721 ksnprintf(SNPARGS(action2, 0), "Tee %d", cmd->arg1);
725 ksnprintf(SNPARGS(action2, 0), "SkipTo %d", cmd->arg1);
729 ksnprintf(SNPARGS(action2, 0), "Pipe %d", cmd->arg1);
733 ksnprintf(SNPARGS(action2, 0), "Queue %d", cmd->arg1);
738 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
741 len = ksnprintf(SNPARGS(action2, 0),
743 kinet_ntoa(sa->sa.sin_addr, abuf));
744 if (sa->sa.sin_port) {
745 ksnprintf(SNPARGS(action2, len), ":%d",
757 if (hlen == 0) { /* non-ip */
758 ksnprintf(SNPARGS(proto, 0), "MAC");
760 struct ip *ip = mtod(m, struct ip *);
761 /* these three are all aliases to the same thing */
762 struct icmp *const icmp = L3HDR(struct icmp, ip);
763 struct tcphdr *const tcp = (struct tcphdr *)icmp;
764 struct udphdr *const udp = (struct udphdr *)icmp;
766 int ip_off, offset, ip_len;
769 if (eh != NULL) { /* layer 2 packets are as on the wire */
770 ip_off = ntohs(ip->ip_off);
771 ip_len = ntohs(ip->ip_len);
776 offset = ip_off & IP_OFFMASK;
779 len = ksnprintf(SNPARGS(proto, 0), "TCP %s",
780 kinet_ntoa(ip->ip_src, abuf));
782 ksnprintf(SNPARGS(proto, len), ":%d %s:%d",
783 ntohs(tcp->th_sport),
784 kinet_ntoa(ip->ip_dst, abuf),
785 ntohs(tcp->th_dport));
787 ksnprintf(SNPARGS(proto, len), " %s",
788 kinet_ntoa(ip->ip_dst, abuf));
793 len = ksnprintf(SNPARGS(proto, 0), "UDP %s",
794 kinet_ntoa(ip->ip_src, abuf));
796 ksnprintf(SNPARGS(proto, len), ":%d %s:%d",
797 ntohs(udp->uh_sport),
798 kinet_ntoa(ip->ip_dst, abuf),
799 ntohs(udp->uh_dport));
801 ksnprintf(SNPARGS(proto, len), " %s",
802 kinet_ntoa(ip->ip_dst, abuf));
808 len = ksnprintf(SNPARGS(proto, 0),
813 len = ksnprintf(SNPARGS(proto, 0), "ICMP ");
815 len += ksnprintf(SNPARGS(proto, len), "%s",
816 kinet_ntoa(ip->ip_src, abuf));
817 ksnprintf(SNPARGS(proto, len), " %s",
818 kinet_ntoa(ip->ip_dst, abuf));
822 len = ksnprintf(SNPARGS(proto, 0), "P:%d %s", ip->ip_p,
823 kinet_ntoa(ip->ip_src, abuf));
824 ksnprintf(SNPARGS(proto, len), " %s",
825 kinet_ntoa(ip->ip_dst, abuf));
829 if (ip_off & (IP_MF | IP_OFFMASK)) {
830 ksnprintf(SNPARGS(fragment, 0), " (frag %d:%d@%d%s)",
831 ntohs(ip->ip_id), ip_len - (ip->ip_hl << 2),
832 offset << 3, (ip_off & IP_MF) ? "+" : "");
836 if (oif || m->m_pkthdr.rcvif) {
837 log(LOG_SECURITY | LOG_INFO,
838 "ipfw: %d %s %s %s via %s%s\n",
840 action, proto, oif ? "out" : "in",
841 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
844 log(LOG_SECURITY | LOG_INFO,
845 "ipfw: %d %s %s [no if info]%s\n",
847 action, proto, fragment);
851 log(LOG_SECURITY | LOG_NOTICE,
852 "ipfw: limit %d reached on entry %d\n",
853 limit_reached, f ? f->rulenum : -1);
860 * IMPORTANT: the hash function for dynamic rules must be commutative
861 * in source and destination (ip,port), because rules are bidirectional
862 * and we want to find both in the same bucket.
865 hash_packet(struct ipfw_flow_id *id)
869 i = (id->dst_ip) ^ (id->src_ip) ^ (id->dst_port) ^ (id->src_port);
870 i &= (curr_dyn_buckets - 1);
875 * Unlink a dynamic rule from a chain. prev is a pointer to
876 * the previous one, q is a pointer to the rule to delete,
877 * head is a pointer to the head of the queue.
878 * Modifies q and potentially also head.
880 #define UNLINK_DYN_RULE(prev, head, q) \
882 ipfw_dyn_rule *old_q = q; \
884 /* remove a refcount to the parent */ \
885 if (q->dyn_type == O_LIMIT) \
886 q->parent->count--; \
887 DPRINTF("-- unlink entry 0x%08x %d -> 0x%08x %d, %d left\n", \
888 q->id.src_ip, q->id.src_port, \
889 q->id.dst_ip, q->id.dst_port, dyn_count - 1); \
891 prev->next = q = q->next; \
893 head = q = q->next; \
894 KASSERT(dyn_count > 0, ("invalid dyn count %u", dyn_count)); \
896 kfree(old_q, M_IPFW); \
899 #define TIME_LEQ(a, b) ((int)((a) - (b)) <= 0)
902 * Remove dynamic rules pointing to "rule", or all of them if rule == NULL.
904 * If keep_me == NULL, rules are deleted even if not expired,
905 * otherwise only expired rules are removed.
907 * The value of the second parameter is also used to point to identify
908 * a rule we absolutely do not want to remove (e.g. because we are
909 * holding a reference to it -- this is the case with O_LIMIT_PARENT
910 * rules). The pointer is only used for comparison, so any non-null
914 remove_dyn_rule_locked(struct ip_fw *rule, ipfw_dyn_rule *keep_me)
916 static time_t last_remove = 0; /* XXX */
918 #define FORCE (keep_me == NULL)
920 ipfw_dyn_rule *prev, *q;
921 int i, pass = 0, max_pass = 0, unlinked = 0;
923 if (ipfw_dyn_v == NULL || dyn_count == 0)
925 /* do not expire more than once per second, it is useless */
926 if (!FORCE && last_remove == time_uptime)
928 last_remove = time_uptime;
931 * because O_LIMIT refer to parent rules, during the first pass only
932 * remove child and mark any pending LIMIT_PARENT, and remove
933 * them in a second pass.
936 for (i = 0; i < curr_dyn_buckets; i++) {
937 for (prev = NULL, q = ipfw_dyn_v[i]; q;) {
939 * Logic can become complex here, so we split tests.
943 if (rule != NULL && rule->stub != q->stub)
944 goto next; /* not the one we are looking for */
945 if (q->dyn_type == O_LIMIT_PARENT) {
947 * handle parent in the second pass,
948 * record we need one.
953 if (FORCE && q->count != 0) {
954 /* XXX should not happen! */
955 kprintf("OUCH! cannot remove rule, "
956 "count %d\n", q->count);
959 if (!FORCE && !TIME_LEQ(q->expire, time_second))
963 UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q);
970 if (pass++ < max_pass)
980 * Lookup a dynamic rule.
982 static ipfw_dyn_rule *
983 lookup_dyn_rule(struct ipfw_flow_id *pkt, int *match_direction,
987 * stateful ipfw extensions.
988 * Lookup into dynamic session queue
990 #define MATCH_REVERSE 0
991 #define MATCH_FORWARD 1
993 #define MATCH_UNKNOWN 3
994 int i, dir = MATCH_NONE;
995 ipfw_dyn_rule *q=NULL;
997 if (ipfw_dyn_v == NULL)
998 goto done; /* not found */
1000 i = hash_packet(pkt);
1001 for (q = ipfw_dyn_v[i]; q != NULL;) {
1002 if (q->dyn_type == O_LIMIT_PARENT)
1005 if (TIME_LEQ(q->expire, time_second)) {
1007 * Entry expired; skip.
1008 * Let ipfw_tick() take care of it
1013 if (pkt->proto == q->id.proto) {
1014 if (pkt->src_ip == q->id.src_ip &&
1015 pkt->dst_ip == q->id.dst_ip &&
1016 pkt->src_port == q->id.src_port &&
1017 pkt->dst_port == q->id.dst_port) {
1018 dir = MATCH_FORWARD;
1021 if (pkt->src_ip == q->id.dst_ip &&
1022 pkt->dst_ip == q->id.src_ip &&
1023 pkt->src_port == q->id.dst_port &&
1024 pkt->dst_port == q->id.src_port) {
1025 dir = MATCH_REVERSE;
1033 goto done; /* q = NULL, not found */
1035 if (pkt->proto == IPPROTO_TCP) { /* update state according to flags */
1036 u_char flags = pkt->flags & (TH_FIN|TH_SYN|TH_RST);
1038 #define BOTH_SYN (TH_SYN | (TH_SYN << 8))
1039 #define BOTH_FIN (TH_FIN | (TH_FIN << 8))
1041 q->state |= (dir == MATCH_FORWARD ) ? flags : (flags << 8);
1043 case TH_SYN: /* opening */
1044 q->expire = time_second + dyn_syn_lifetime;
1047 case BOTH_SYN: /* move to established */
1048 case BOTH_SYN | TH_FIN : /* one side tries to close */
1049 case BOTH_SYN | (TH_FIN << 8) :
1051 uint32_t ack = ntohl(tcp->th_ack);
1053 #define _SEQ_GE(a, b) ((int)(a) - (int)(b) >= 0)
1055 if (dir == MATCH_FORWARD) {
1056 if (q->ack_fwd == 0 ||
1057 _SEQ_GE(ack, q->ack_fwd))
1059 else /* ignore out-of-sequence */
1062 if (q->ack_rev == 0 ||
1063 _SEQ_GE(ack, q->ack_rev))
1065 else /* ignore out-of-sequence */
1070 q->expire = time_second + dyn_ack_lifetime;
1073 case BOTH_SYN | BOTH_FIN: /* both sides closed */
1074 KKASSERT(dyn_fin_lifetime < dyn_keepalive_period);
1075 q->expire = time_second + dyn_fin_lifetime;
1081 * reset or some invalid combination, but can also
1082 * occur if we use keep-state the wrong way.
1084 if ((q->state & ((TH_RST << 8) | TH_RST)) == 0)
1085 kprintf("invalid state: 0x%x\n", q->state);
1087 KKASSERT(dyn_rst_lifetime < dyn_keepalive_period);
1088 q->expire = time_second + dyn_rst_lifetime;
1091 } else if (pkt->proto == IPPROTO_UDP) {
1092 q->expire = time_second + dyn_udp_lifetime;
1094 /* other protocols */
1095 q->expire = time_second + dyn_short_lifetime;
1098 if (match_direction)
1099 *match_direction = dir;
1103 static struct ip_fw *
1104 lookup_rule(struct ipfw_flow_id *pkt, int *match_direction, struct tcphdr *tcp,
1107 struct ip_fw *rule = NULL;
1110 lockmgr(&dyn_lock, LK_SHARED);
1111 q = lookup_dyn_rule(pkt, match_direction, tcp);
1115 rule = q->stub->rule[mycpuid];
1116 KKASSERT(rule->stub == q->stub && rule->cpuid == mycpuid);
1122 lockmgr(&dyn_lock, LK_RELEASE);
1127 realloc_dynamic_table(void)
1129 ipfw_dyn_rule **old_dyn_v;
1131 KASSERT(dyn_buckets <= 65536 && (dyn_buckets & (dyn_buckets - 1)) == 0,
1132 ("invalid dyn_buckets %d", dyn_buckets));
1134 /* Save the current buckets array for later error recovery */
1135 old_dyn_v = ipfw_dyn_v;
1137 curr_dyn_buckets = dyn_buckets;
1138 ipfw_dyn_v = kmalloc(curr_dyn_buckets * sizeof(ipfw_dyn_rule *),
1139 M_IPFW, M_WAITOK | M_ZERO);
1141 if (old_dyn_v != NULL)
1142 kfree(old_dyn_v, M_IPFW);
1147 * Install state of type 'type' for a dynamic session.
1148 * The hash table contains two type of rules:
1149 * - regular rules (O_KEEP_STATE)
1150 * - rules for sessions with limited number of sess per user
1151 * (O_LIMIT). When they are created, the parent is
1152 * increased by 1, and decreased on delete. In this case,
1153 * the third parameter is the parent rule and not the chain.
1154 * - "parent" rules for the above (O_LIMIT_PARENT).
1156 static ipfw_dyn_rule *
1157 add_dyn_rule(struct ipfw_flow_id *id, uint8_t dyn_type, struct ip_fw *rule)
1162 if (ipfw_dyn_v == NULL ||
1163 (dyn_count == 0 && dyn_buckets != curr_dyn_buckets)) {
1164 realloc_dynamic_table();
1165 KKASSERT(ipfw_dyn_v != NULL);
1167 i = hash_packet(id);
1169 r = kmalloc(sizeof(*r), M_IPFW, M_INTWAIT | M_NULLOK | M_ZERO);
1173 /* increase refcount on parent, and set pointer */
1174 if (dyn_type == O_LIMIT) {
1175 ipfw_dyn_rule *parent = (ipfw_dyn_rule *)rule;
1177 if (parent->dyn_type != O_LIMIT_PARENT)
1178 panic("invalid parent");
1181 rule = parent->stub->rule[mycpuid];
1182 KKASSERT(rule->stub == parent->stub);
1184 KKASSERT(rule->cpuid == mycpuid && rule->stub != NULL);
1187 r->expire = time_second + dyn_syn_lifetime;
1188 r->stub = rule->stub;
1189 r->dyn_type = dyn_type;
1190 r->pcnt = r->bcnt = 0;
1194 r->next = ipfw_dyn_v[i];
1198 DPRINTF("-- add dyn entry ty %d 0x%08x %d -> 0x%08x %d, total %d\n",
1200 r->id.src_ip, r->id.src_port,
1201 r->id.dst_ip, r->id.dst_port, dyn_count);
1206 * Lookup dynamic parent rule using pkt and rule as search keys.
1207 * If the lookup fails, then install one.
1209 static ipfw_dyn_rule *
1210 lookup_dyn_parent(struct ipfw_flow_id *pkt, struct ip_fw *rule)
1216 i = hash_packet(pkt);
1217 for (q = ipfw_dyn_v[i]; q != NULL; q = q->next) {
1218 if (q->dyn_type == O_LIMIT_PARENT &&
1219 rule->stub == q->stub &&
1220 pkt->proto == q->id.proto &&
1221 pkt->src_ip == q->id.src_ip &&
1222 pkt->dst_ip == q->id.dst_ip &&
1223 pkt->src_port == q->id.src_port &&
1224 pkt->dst_port == q->id.dst_port) {
1225 q->expire = time_second + dyn_short_lifetime;
1226 DPRINTF("lookup_dyn_parent found 0x%p\n", q);
1231 return add_dyn_rule(pkt, O_LIMIT_PARENT, rule);
1235 * Install dynamic state for rule type cmd->o.opcode
1237 * Returns 1 (failure) if state is not installed because of errors or because
1238 * session limitations are enforced.
1241 install_state_locked(struct ip_fw *rule, ipfw_insn_limit *cmd,
1242 struct ip_fw_args *args)
1244 static int last_log; /* XXX */
1248 DPRINTF("-- install state type %d 0x%08x %u -> 0x%08x %u\n",
1250 args->f_id.src_ip, args->f_id.src_port,
1251 args->f_id.dst_ip, args->f_id.dst_port);
1253 q = lookup_dyn_rule(&args->f_id, NULL, NULL);
1254 if (q != NULL) { /* should never occur */
1255 if (last_log != time_second) {
1256 last_log = time_second;
1257 kprintf(" install_state: entry already present, done\n");
1262 if (dyn_count >= dyn_max) {
1264 * Run out of slots, try to remove any expired rule.
1266 remove_dyn_rule_locked(NULL, (ipfw_dyn_rule *)1);
1267 if (dyn_count >= dyn_max) {
1268 if (last_log != time_second) {
1269 last_log = time_second;
1270 kprintf("install_state: "
1271 "Too many dynamic rules\n");
1273 return 1; /* cannot install, notify caller */
1277 switch (cmd->o.opcode) {
1278 case O_KEEP_STATE: /* bidir rule */
1279 if (add_dyn_rule(&args->f_id, O_KEEP_STATE, rule) == NULL)
1283 case O_LIMIT: /* limit number of sessions */
1285 uint16_t limit_mask = cmd->limit_mask;
1286 struct ipfw_flow_id id;
1287 ipfw_dyn_rule *parent;
1289 DPRINTF("installing dyn-limit rule %d\n",
1292 id.dst_ip = id.src_ip = 0;
1293 id.dst_port = id.src_port = 0;
1294 id.proto = args->f_id.proto;
1296 if (limit_mask & DYN_SRC_ADDR)
1297 id.src_ip = args->f_id.src_ip;
1298 if (limit_mask & DYN_DST_ADDR)
1299 id.dst_ip = args->f_id.dst_ip;
1300 if (limit_mask & DYN_SRC_PORT)
1301 id.src_port = args->f_id.src_port;
1302 if (limit_mask & DYN_DST_PORT)
1303 id.dst_port = args->f_id.dst_port;
1305 parent = lookup_dyn_parent(&id, rule);
1306 if (parent == NULL) {
1307 kprintf("add parent failed\n");
1311 if (parent->count >= cmd->conn_limit) {
1313 * See if we can remove some expired rule.
1315 remove_dyn_rule_locked(rule, parent);
1316 if (parent->count >= cmd->conn_limit) {
1318 last_log != time_second) {
1319 last_log = time_second;
1320 log(LOG_SECURITY | LOG_DEBUG,
1322 "too many entries\n");
1327 if (add_dyn_rule(&args->f_id, O_LIMIT,
1328 (struct ip_fw *)parent) == NULL)
1333 kprintf("unknown dynamic rule type %u\n", cmd->o.opcode);
1336 lookup_dyn_rule(&args->f_id, NULL, NULL); /* XXX just set lifetime */
1341 install_state(struct ip_fw *rule, ipfw_insn_limit *cmd, struct ip_fw_args *args)
1345 lockmgr(&dyn_lock, LK_EXCLUSIVE);
1346 ret = install_state_locked(rule, cmd, args);
1347 lockmgr(&dyn_lock, LK_RELEASE);
1353 * Transmit a TCP packet, containing either a RST or a keepalive.
1354 * When flags & TH_RST, we are sending a RST packet, because of a
1355 * "reset" action matched the packet.
1356 * Otherwise we are sending a keepalive, and flags & TH_
1359 send_pkt(struct ipfw_flow_id *id, uint32_t seq, uint32_t ack, int flags)
1364 struct route sro; /* fake route */
1366 MGETHDR(m, M_NOWAIT, MT_HEADER);
1369 m->m_pkthdr.rcvif = NULL;
1370 m->m_pkthdr.len = m->m_len = sizeof(struct ip) + sizeof(struct tcphdr);
1371 m->m_data += max_linkhdr;
1373 ip = mtod(m, struct ip *);
1374 bzero(ip, m->m_len);
1375 tcp = (struct tcphdr *)(ip + 1); /* no IP options */
1376 ip->ip_p = IPPROTO_TCP;
1380 * Assume we are sending a RST (or a keepalive in the reverse
1381 * direction), swap src and destination addresses and ports.
1383 ip->ip_src.s_addr = htonl(id->dst_ip);
1384 ip->ip_dst.s_addr = htonl(id->src_ip);
1385 tcp->th_sport = htons(id->dst_port);
1386 tcp->th_dport = htons(id->src_port);
1387 if (flags & TH_RST) { /* we are sending a RST */
1388 if (flags & TH_ACK) {
1389 tcp->th_seq = htonl(ack);
1390 tcp->th_ack = htonl(0);
1391 tcp->th_flags = TH_RST;
1395 tcp->th_seq = htonl(0);
1396 tcp->th_ack = htonl(seq);
1397 tcp->th_flags = TH_RST | TH_ACK;
1401 * We are sending a keepalive. flags & TH_SYN determines
1402 * the direction, forward if set, reverse if clear.
1403 * NOTE: seq and ack are always assumed to be correct
1404 * as set by the caller. This may be confusing...
1406 if (flags & TH_SYN) {
1408 * we have to rewrite the correct addresses!
1410 ip->ip_dst.s_addr = htonl(id->dst_ip);
1411 ip->ip_src.s_addr = htonl(id->src_ip);
1412 tcp->th_dport = htons(id->dst_port);
1413 tcp->th_sport = htons(id->src_port);
1415 tcp->th_seq = htonl(seq);
1416 tcp->th_ack = htonl(ack);
1417 tcp->th_flags = TH_ACK;
1421 * set ip_len to the payload size so we can compute
1422 * the tcp checksum on the pseudoheader
1423 * XXX check this, could save a couple of words ?
1425 ip->ip_len = htons(sizeof(struct tcphdr));
1426 tcp->th_sum = in_cksum(m, m->m_pkthdr.len);
1429 * now fill fields left out earlier
1431 ip->ip_ttl = ip_defttl;
1432 ip->ip_len = m->m_pkthdr.len;
1434 bzero(&sro, sizeof(sro));
1435 ip_rtaddr(ip->ip_dst, &sro);
1437 m->m_pkthdr.fw_flags |= IPFW_MBUF_GENERATED;
1438 ip_output(m, NULL, &sro, 0, NULL, NULL);
1444 * Send a reject message, consuming the mbuf passed as an argument.
1447 send_reject(struct ip_fw_args *args, int code, int offset, int ip_len)
1449 if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */
1450 /* We need the IP header in host order for icmp_error(). */
1451 if (args->eh != NULL) {
1452 struct ip *ip = mtod(args->m, struct ip *);
1454 ip->ip_len = ntohs(ip->ip_len);
1455 ip->ip_off = ntohs(ip->ip_off);
1457 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
1458 } else if (offset == 0 && args->f_id.proto == IPPROTO_TCP) {
1459 struct tcphdr *const tcp =
1460 L3HDR(struct tcphdr, mtod(args->m, struct ip *));
1462 if ((tcp->th_flags & TH_RST) == 0) {
1463 send_pkt(&args->f_id, ntohl(tcp->th_seq),
1464 ntohl(tcp->th_ack), tcp->th_flags | TH_RST);
1474 * Given an ip_fw *, lookup_next_rule will return a pointer
1475 * to the next rule, which can be either the jump
1476 * target (for skipto instructions) or the next one in the list (in
1477 * all other cases including a missing jump target).
1478 * The result is also written in the "next_rule" field of the rule.
1479 * Backward jumps are not allowed, so start looking from the next
1482 * This never returns NULL -- in case we do not have an exact match,
1483 * the next rule is returned. When the ruleset is changed,
1484 * pointers are flushed so we are always correct.
1486 static struct ip_fw *
1487 lookup_next_rule(struct ip_fw *me)
1489 struct ip_fw *rule = NULL;
1492 /* look for action, in case it is a skipto */
1493 cmd = ACTION_PTR(me);
1494 if (cmd->opcode == O_LOG)
1496 if (cmd->opcode == O_SKIPTO) {
1497 for (rule = me->next; rule; rule = rule->next) {
1498 if (rule->rulenum >= cmd->arg1)
1502 if (rule == NULL) /* failure or not a skipto */
1504 me->next_rule = rule;
1509 ipfw_match_uid(const struct ipfw_flow_id *fid, struct ifnet *oif,
1510 enum ipfw_opcodes opcode, uid_t uid)
1512 struct in_addr src_ip, dst_ip;
1513 struct inpcbinfo *pi;
1517 if (fid->proto == IPPROTO_TCP) {
1519 pi = &tcbinfo[mycpuid];
1520 } else if (fid->proto == IPPROTO_UDP) {
1522 pi = &udbinfo[mycpuid];
1528 * Values in 'fid' are in host byte order
1530 dst_ip.s_addr = htonl(fid->dst_ip);
1531 src_ip.s_addr = htonl(fid->src_ip);
1533 pcb = in_pcblookup_hash(pi,
1534 dst_ip, htons(fid->dst_port),
1535 src_ip, htons(fid->src_port),
1538 pcb = in_pcblookup_hash(pi,
1539 src_ip, htons(fid->src_port),
1540 dst_ip, htons(fid->dst_port),
1543 if (pcb == NULL || pcb->inp_socket == NULL)
1546 if (opcode == O_UID) {
1547 #define socheckuid(a,b) ((a)->so_cred->cr_uid != (b))
1548 return !socheckuid(pcb->inp_socket, uid);
1551 return groupmember(uid, pcb->inp_socket->so_cred);
1556 * The main check routine for the firewall.
1558 * All arguments are in args so we can modify them and return them
1559 * back to the caller.
1563 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1564 * Starts with the IP header.
1565 * args->eh (in) Mac header if present, or NULL for layer3 packet.
1566 * args->oif Outgoing interface, or NULL if packet is incoming.
1567 * The incoming interface is in the mbuf. (in)
1569 * args->rule Pointer to the last matching rule (in/out)
1570 * args->f_id Addresses grabbed from the packet (out)
1574 * If the packet was denied/rejected and has been dropped, *m is equal
1575 * to NULL upon return.
1577 * IP_FW_DENY the packet must be dropped.
1578 * IP_FW_PASS The packet is to be accepted and routed normally.
1579 * IP_FW_DIVERT Divert the packet to port (args->cookie)
1580 * IP_FW_TEE Tee the packet to port (args->cookie)
1581 * IP_FW_DUMMYNET Send the packet to pipe/queue (args->cookie)
1584 ipfw_chk(struct ip_fw_args *args)
1587 * Local variables hold state during the processing of a packet.
1589 * IMPORTANT NOTE: to speed up the processing of rules, there
1590 * are some assumption on the values of the variables, which
1591 * are documented here. Should you change them, please check
1592 * the implementation of the various instructions to make sure
1593 * that they still work.
1595 * args->eh The MAC header. It is non-null for a layer2
1596 * packet, it is NULL for a layer-3 packet.
1598 * m | args->m Pointer to the mbuf, as received from the caller.
1599 * It may change if ipfw_chk() does an m_pullup, or if it
1600 * consumes the packet because it calls send_reject().
1601 * XXX This has to change, so that ipfw_chk() never modifies
1602 * or consumes the buffer.
1603 * ip is simply an alias of the value of m, and it is kept
1604 * in sync with it (the packet is supposed to start with
1607 struct mbuf *m = args->m;
1608 struct ip *ip = mtod(m, struct ip *);
1611 * oif | args->oif If NULL, ipfw_chk has been called on the
1612 * inbound path (ether_input, ip_input).
1613 * If non-NULL, ipfw_chk has been called on the outbound path
1614 * (ether_output, ip_output).
1616 struct ifnet *oif = args->oif;
1618 struct ip_fw *f = NULL; /* matching rule */
1619 int retval = IP_FW_PASS;
1621 struct divert_info *divinfo;
1624 * hlen The length of the IPv4 header.
1625 * hlen >0 means we have an IPv4 packet.
1627 u_int hlen = 0; /* hlen >0 means we have an IP pkt */
1630 * offset The offset of a fragment. offset != 0 means that
1631 * we have a fragment at this offset of an IPv4 packet.
1632 * offset == 0 means that (if this is an IPv4 packet)
1633 * this is the first or only fragment.
1638 * Local copies of addresses. They are only valid if we have
1641 * proto The protocol. Set to 0 for non-ip packets,
1642 * or to the protocol read from the packet otherwise.
1643 * proto != 0 means that we have an IPv4 packet.
1645 * src_port, dst_port port numbers, in HOST format. Only
1646 * valid for TCP and UDP packets.
1648 * src_ip, dst_ip ip addresses, in NETWORK format.
1649 * Only valid for IPv4 packets.
1652 uint16_t src_port = 0, dst_port = 0; /* NOTE: host format */
1653 struct in_addr src_ip, dst_ip; /* NOTE: network format */
1654 uint16_t ip_len = 0;
1657 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1658 * MATCH_NONE when checked and not matched (dyn_f = NULL),
1659 * MATCH_FORWARD or MATCH_REVERSE otherwise (dyn_f != NULL)
1661 int dyn_dir = MATCH_UNKNOWN;
1662 struct ip_fw *dyn_f = NULL;
1663 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
1665 if (m->m_pkthdr.fw_flags & IPFW_MBUF_GENERATED)
1666 return IP_FW_PASS; /* accept */
1668 if (args->eh == NULL || /* layer 3 packet */
1669 (m->m_pkthdr.len >= sizeof(struct ip) &&
1670 ntohs(args->eh->ether_type) == ETHERTYPE_IP))
1671 hlen = ip->ip_hl << 2;
1674 * Collect parameters into local variables for faster matching.
1676 if (hlen == 0) { /* do not grab addresses for non-ip pkts */
1677 proto = args->f_id.proto = 0; /* mark f_id invalid */
1678 goto after_ip_checks;
1681 proto = args->f_id.proto = ip->ip_p;
1682 src_ip = ip->ip_src;
1683 dst_ip = ip->ip_dst;
1684 if (args->eh != NULL) { /* layer 2 packets are as on the wire */
1685 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1686 ip_len = ntohs(ip->ip_len);
1688 offset = ip->ip_off & IP_OFFMASK;
1689 ip_len = ip->ip_len;
1692 #define PULLUP_TO(len) \
1694 if (m->m_len < (len)) { \
1695 args->m = m = m_pullup(m, (len));\
1697 goto pullup_failed; \
1698 ip = mtod(m, struct ip *); \
1708 PULLUP_TO(hlen + sizeof(struct tcphdr));
1709 tcp = L3HDR(struct tcphdr, ip);
1710 dst_port = tcp->th_dport;
1711 src_port = tcp->th_sport;
1712 args->f_id.flags = tcp->th_flags;
1720 PULLUP_TO(hlen + sizeof(struct udphdr));
1721 udp = L3HDR(struct udphdr, ip);
1722 dst_port = udp->uh_dport;
1723 src_port = udp->uh_sport;
1728 PULLUP_TO(hlen + 4); /* type, code and checksum. */
1729 args->f_id.flags = L3HDR(struct icmp, ip)->icmp_type;
1739 args->f_id.src_ip = ntohl(src_ip.s_addr);
1740 args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1741 args->f_id.src_port = src_port = ntohs(src_port);
1742 args->f_id.dst_port = dst_port = ntohs(dst_port);
1747 * Packet has already been tagged. Look for the next rule
1748 * to restart processing.
1750 * If fw_one_pass != 0 then just accept it.
1751 * XXX should not happen here, but optimized out in
1757 /* This rule is being/has been flushed */
1761 KASSERT(args->rule->cpuid == mycpuid,
1762 ("rule used on cpu%d", mycpuid));
1764 /* This rule was deleted */
1765 if (args->rule->rule_flags & IPFW_RULE_F_INVALID)
1768 f = args->rule->next_rule;
1770 f = lookup_next_rule(args->rule);
1773 * Find the starting rule. It can be either the first
1774 * one, or the one after divert_rule if asked so.
1778 mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL);
1780 divinfo = m_tag_data(mtag);
1781 skipto = divinfo->skipto;
1786 f = ctx->ipfw_layer3_chain;
1787 if (args->eh == NULL && skipto != 0) {
1788 /* No skipto during rule flushing */
1792 if (skipto >= IPFW_DEFAULT_RULE)
1793 return IP_FW_DENY; /* invalid */
1795 while (f && f->rulenum <= skipto)
1797 if (f == NULL) /* drop packet */
1799 } else if (ipfw_flushing) {
1800 /* Rules are being flushed; skip to default rule */
1801 f = ctx->ipfw_default_rule;
1804 if ((mtag = m_tag_find(m, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
1805 m_tag_delete(m, mtag);
1808 * Now scan the rules, and parse microinstructions for each rule.
1810 for (; f; f = f->next) {
1813 int skip_or; /* skip rest of OR block */
1816 if (ctx->ipfw_set_disable & (1 << f->set))
1820 for (l = f->cmd_len, cmd = f->cmd; l > 0;
1821 l -= cmdlen, cmd += cmdlen) {
1825 * check_body is a jump target used when we find a
1826 * CHECK_STATE, and need to jump to the body of
1831 cmdlen = F_LEN(cmd);
1833 * An OR block (insn_1 || .. || insn_n) has the
1834 * F_OR bit set in all but the last instruction.
1835 * The first match will set "skip_or", and cause
1836 * the following instructions to be skipped until
1837 * past the one with the F_OR bit clear.
1839 if (skip_or) { /* skip this instruction */
1840 if ((cmd->len & F_OR) == 0)
1841 skip_or = 0; /* next one is good */
1844 match = 0; /* set to 1 if we succeed */
1846 switch (cmd->opcode) {
1848 * The first set of opcodes compares the packet's
1849 * fields with some pattern, setting 'match' if a
1850 * match is found. At the end of the loop there is
1851 * logic to deal with F_NOT and F_OR flags associated
1859 kprintf("ipfw: opcode %d unimplemented\n",
1866 * We only check offset == 0 && proto != 0,
1867 * as this ensures that we have an IPv4
1868 * packet with the ports info.
1873 match = ipfw_match_uid(&args->f_id, oif,
1875 (uid_t)((ipfw_insn_u32 *)cmd)->d[0]);
1879 match = iface_match(m->m_pkthdr.rcvif,
1880 (ipfw_insn_if *)cmd);
1884 match = iface_match(oif, (ipfw_insn_if *)cmd);
1888 match = iface_match(oif ? oif :
1889 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd);
1893 if (args->eh != NULL) { /* have MAC header */
1894 uint32_t *want = (uint32_t *)
1895 ((ipfw_insn_mac *)cmd)->addr;
1896 uint32_t *mask = (uint32_t *)
1897 ((ipfw_insn_mac *)cmd)->mask;
1898 uint32_t *hdr = (uint32_t *)args->eh;
1901 (want[0] == (hdr[0] & mask[0]) &&
1902 want[1] == (hdr[1] & mask[1]) &&
1903 want[2] == (hdr[2] & mask[2]));
1908 if (args->eh != NULL) {
1910 ntohs(args->eh->ether_type);
1912 ((ipfw_insn_u16 *)cmd)->ports;
1915 /* Special vlan handling */
1916 if (m->m_flags & M_VLANTAG)
1919 for (i = cmdlen - 1; !match && i > 0;
1922 (t >= p[0] && t <= p[1]);
1928 match = (hlen > 0 && offset != 0);
1931 case O_IN: /* "out" is "not in" */
1932 match = (oif == NULL);
1936 match = (args->eh != NULL);
1941 * We do not allow an arg of 0 so the
1942 * check of "proto" only suffices.
1944 match = (proto == cmd->arg1);
1948 match = (hlen > 0 &&
1949 ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1954 match = (hlen > 0 &&
1955 ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1957 ((ipfw_insn_ip *)cmd)->mask.s_addr));
1964 tif = INADDR_TO_IFP(&src_ip);
1965 match = (tif != NULL);
1972 uint32_t *d = (uint32_t *)(cmd + 1);
1974 cmd->opcode == O_IP_DST_SET ?
1980 addr -= d[0]; /* subtract base */
1982 (addr < cmd->arg1) &&
1983 (d[1 + (addr >> 5)] &
1984 (1 << (addr & 0x1f)));
1989 match = (hlen > 0 &&
1990 ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1995 match = (hlen > 0) &&
1996 (((ipfw_insn_ip *)cmd)->addr.s_addr ==
1998 ((ipfw_insn_ip *)cmd)->mask.s_addr));
2005 tif = INADDR_TO_IFP(&dst_ip);
2006 match = (tif != NULL);
2013 * offset == 0 && proto != 0 is enough
2014 * to guarantee that we have an IPv4
2015 * packet with port info.
2017 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP)
2020 (cmd->opcode == O_IP_SRCPORT) ?
2021 src_port : dst_port ;
2023 ((ipfw_insn_u16 *)cmd)->ports;
2026 for (i = cmdlen - 1; !match && i > 0;
2029 (x >= p[0] && x <= p[1]);
2035 match = (offset == 0 && proto==IPPROTO_ICMP &&
2036 icmptype_match(ip, (ipfw_insn_u32 *)cmd));
2040 match = (hlen > 0 && ipopts_match(ip, cmd));
2044 match = (hlen > 0 && cmd->arg1 == ip->ip_v);
2048 match = (hlen > 0 && cmd->arg1 == ip->ip_ttl);
2052 match = (hlen > 0 &&
2053 cmd->arg1 == ntohs(ip->ip_id));
2057 match = (hlen > 0 && cmd->arg1 == ip_len);
2060 case O_IPPRECEDENCE:
2061 match = (hlen > 0 &&
2062 (cmd->arg1 == (ip->ip_tos & 0xe0)));
2066 match = (hlen > 0 &&
2067 flags_match(cmd, ip->ip_tos));
2071 match = (proto == IPPROTO_TCP && offset == 0 &&
2073 L3HDR(struct tcphdr,ip)->th_flags));
2077 match = (proto == IPPROTO_TCP && offset == 0 &&
2078 tcpopts_match(ip, cmd));
2082 match = (proto == IPPROTO_TCP && offset == 0 &&
2083 ((ipfw_insn_u32 *)cmd)->d[0] ==
2084 L3HDR(struct tcphdr,ip)->th_seq);
2088 match = (proto == IPPROTO_TCP && offset == 0 &&
2089 ((ipfw_insn_u32 *)cmd)->d[0] ==
2090 L3HDR(struct tcphdr,ip)->th_ack);
2094 match = (proto == IPPROTO_TCP && offset == 0 &&
2096 L3HDR(struct tcphdr,ip)->th_win);
2100 /* reject packets which have SYN only */
2101 /* XXX should i also check for TH_ACK ? */
2102 match = (proto == IPPROTO_TCP && offset == 0 &&
2103 (L3HDR(struct tcphdr,ip)->th_flags &
2104 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
2109 ipfw_log(f, hlen, args->eh, m, oif);
2114 match = (krandom() <
2115 ((ipfw_insn_u32 *)cmd)->d[0]);
2119 * The second set of opcodes represents 'actions',
2120 * i.e. the terminal part of a rule once the packet
2121 * matches all previous patterns.
2122 * Typically there is only one action for each rule,
2123 * and the opcode is stored at the end of the rule
2124 * (but there are exceptions -- see below).
2126 * In general, here we set retval and terminate the
2127 * outer loop (would be a 'break 3' in some language,
2128 * but we need to do a 'goto done').
2131 * O_COUNT and O_SKIPTO actions:
2132 * instead of terminating, we jump to the next rule
2133 * ('goto next_rule', equivalent to a 'break 2'),
2134 * or to the SKIPTO target ('goto again' after
2135 * having set f, cmd and l), respectively.
2137 * O_LIMIT and O_KEEP_STATE: these opcodes are
2138 * not real 'actions', and are stored right
2139 * before the 'action' part of the rule.
2140 * These opcodes try to install an entry in the
2141 * state tables; if successful, we continue with
2142 * the next opcode (match=1; break;), otherwise
2143 * the packet must be dropped ('goto done' after
2144 * setting retval). If static rules are changed
2145 * during the state installation, the packet will
2146 * be dropped and rule's stats will not beupdated
2147 * ('return IP_FW_DENY').
2149 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2150 * cause a lookup of the state table, and a jump
2151 * to the 'action' part of the parent rule
2152 * ('goto check_body') if an entry is found, or
2153 * (CHECK_STATE only) a jump to the next rule if
2154 * the entry is not found ('goto next_rule').
2155 * The result of the lookup is cached to make
2156 * further instances of these opcodes are
2157 * effectively NOPs. If static rules are changed
2158 * during the state looking up, the packet will
2159 * be dropped and rule's stats will not be updated
2160 * ('return IP_FW_DENY').
2164 if (!(f->rule_flags & IPFW_RULE_F_STATE)) {
2165 kprintf("%s rule (%d) is not ready "
2167 cmd->opcode == O_LIMIT ?
2168 "limit" : "keep state",
2169 f->rulenum, f->cpuid);
2172 if (install_state(f,
2173 (ipfw_insn_limit *)cmd, args)) {
2174 retval = IP_FW_DENY;
2175 goto done; /* error/limit violation */
2183 * dynamic rules are checked at the first
2184 * keep-state or check-state occurrence,
2185 * with the result being stored in dyn_dir.
2186 * The compiler introduces a PROBE_STATE
2187 * instruction for us when we have a
2188 * KEEP_STATE (because PROBE_STATE needs
2191 if (dyn_dir == MATCH_UNKNOWN) {
2192 dyn_f = lookup_rule(&args->f_id,
2194 proto == IPPROTO_TCP ?
2195 L3HDR(struct tcphdr, ip) : NULL,
2197 if (dyn_f != NULL) {
2199 * Found a rule from a dynamic
2200 * entry; jump to the 'action'
2204 cmd = ACTION_PTR(f);
2205 l = f->cmd_len - f->act_ofs;
2210 * Dynamic entry not found. If CHECK_STATE,
2211 * skip to next rule, if PROBE_STATE just
2212 * ignore and continue with next opcode.
2214 if (cmd->opcode == O_CHECK_STATE)
2216 else if (!(f->rule_flags & IPFW_RULE_F_STATE))
2217 goto next_rule; /* not ready yet */
2222 retval = IP_FW_PASS; /* accept */
2227 args->rule = f; /* report matching rule */
2228 args->cookie = cmd->arg1;
2229 retval = IP_FW_DUMMYNET;
2234 if (args->eh) /* not on layer 2 */
2237 mtag = m_tag_get(PACKET_TAG_IPFW_DIVERT,
2238 sizeof(*divinfo), M_NOWAIT);
2240 retval = IP_FW_DENY;
2243 divinfo = m_tag_data(mtag);
2245 divinfo->skipto = f->rulenum;
2246 divinfo->port = cmd->arg1;
2247 divinfo->tee = (cmd->opcode == O_TEE);
2248 m_tag_prepend(m, mtag);
2250 args->cookie = cmd->arg1;
2251 retval = (cmd->opcode == O_DIVERT) ?
2252 IP_FW_DIVERT : IP_FW_TEE;
2257 f->pcnt++; /* update stats */
2259 f->timestamp = time_second;
2260 if (cmd->opcode == O_COUNT)
2263 if (f->next_rule == NULL)
2264 lookup_next_rule(f);
2270 * Drop the packet and send a reject notice
2271 * if the packet is not ICMP (or is an ICMP
2272 * query), and it is not multicast/broadcast.
2275 (proto != IPPROTO_ICMP ||
2276 is_icmp_query(ip)) &&
2277 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2278 !IN_MULTICAST(ntohl(dst_ip.s_addr))) {
2280 * Update statistics before the possible
2281 * blocking 'send_reject'
2285 f->timestamp = time_second;
2287 send_reject(args, cmd->arg1,
2292 * Return directly here, rule stats
2293 * have been updated above.
2299 retval = IP_FW_DENY;
2303 if (args->eh) /* not valid on layer2 pkts */
2305 if (!dyn_f || dyn_dir == MATCH_FORWARD) {
2306 struct sockaddr_in *sin;
2308 mtag = m_tag_get(PACKET_TAG_IPFORWARD,
2309 sizeof(*sin), M_NOWAIT);
2311 retval = IP_FW_DENY;
2314 sin = m_tag_data(mtag);
2316 /* Structure copy */
2317 *sin = ((ipfw_insn_sa *)cmd)->sa;
2319 m_tag_prepend(m, mtag);
2320 m->m_pkthdr.fw_flags |=
2321 IPFORWARD_MBUF_TAGGED;
2322 m->m_pkthdr.fw_flags &=
2323 ~BRIDGE_MBUF_TAGGED;
2325 retval = IP_FW_PASS;
2329 panic("-- unknown opcode %d", cmd->opcode);
2330 } /* end of switch() on opcodes */
2332 if (cmd->len & F_NOT)
2336 if (cmd->len & F_OR)
2339 if (!(cmd->len & F_OR)) /* not an OR block, */
2340 break; /* try next rule */
2343 } /* end of inner for, scan opcodes */
2345 next_rule:; /* try next rule */
2347 } /* end of outer for, scan rules */
2348 kprintf("+++ ipfw: ouch!, skip past end of rules, denying packet\n");
2352 /* Update statistics */
2355 f->timestamp = time_second;
2360 kprintf("pullup failed\n");
2365 ipfw_dummynet_io(struct mbuf *m, int pipe_nr, int dir, struct ip_fw_args *fwa)
2370 const struct ipfw_flow_id *id;
2371 struct dn_flow_id *fid;
2375 mtag = m_tag_get(PACKET_TAG_DUMMYNET, sizeof(*pkt), M_NOWAIT);
2380 m_tag_prepend(m, mtag);
2382 pkt = m_tag_data(mtag);
2383 bzero(pkt, sizeof(*pkt));
2385 cmd = fwa->rule->cmd + fwa->rule->act_ofs;
2386 if (cmd->opcode == O_LOG)
2388 KASSERT(cmd->opcode == O_PIPE || cmd->opcode == O_QUEUE,
2389 ("Rule is not PIPE or QUEUE, opcode %d", cmd->opcode));
2392 pkt->dn_flags = (dir & DN_FLAGS_DIR_MASK);
2393 pkt->ifp = fwa->oif;
2394 pkt->pipe_nr = pipe_nr;
2396 pkt->cpuid = mycpuid;
2397 pkt->msgport = netisr_curport();
2401 fid->fid_dst_ip = id->dst_ip;
2402 fid->fid_src_ip = id->src_ip;
2403 fid->fid_dst_port = id->dst_port;
2404 fid->fid_src_port = id->src_port;
2405 fid->fid_proto = id->proto;
2406 fid->fid_flags = id->flags;
2408 ipfw_ref_rule(fwa->rule);
2409 pkt->dn_priv = fwa->rule;
2410 pkt->dn_unref_priv = ipfw_unref_rule;
2412 if (cmd->opcode == O_PIPE)
2413 pkt->dn_flags |= DN_FLAGS_IS_PIPE;
2415 m->m_pkthdr.fw_flags |= DUMMYNET_MBUF_TAGGED;
2419 * When a rule is added/deleted, clear the next_rule pointers in all rules.
2420 * These will be reconstructed on the fly as packets are matched.
2423 ipfw_flush_rule_ptrs(struct ipfw_context *ctx)
2427 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next)
2428 rule->next_rule = NULL;
2431 static __inline void
2432 ipfw_inc_static_count(struct ip_fw *rule)
2434 /* Static rule's counts are updated only on CPU0 */
2435 KKASSERT(mycpuid == 0);
2438 static_ioc_len += IOC_RULESIZE(rule);
2441 static __inline void
2442 ipfw_dec_static_count(struct ip_fw *rule)
2444 int l = IOC_RULESIZE(rule);
2446 /* Static rule's counts are updated only on CPU0 */
2447 KKASSERT(mycpuid == 0);
2449 KASSERT(static_count > 0, ("invalid static count %u", static_count));
2452 KASSERT(static_ioc_len >= l,
2453 ("invalid static len %u", static_ioc_len));
2454 static_ioc_len -= l;
2458 ipfw_link_sibling(struct netmsg_ipfw *fwmsg, struct ip_fw *rule)
2460 if (fwmsg->sibling != NULL) {
2461 KKASSERT(mycpuid > 0 && fwmsg->sibling->cpuid == mycpuid - 1);
2462 fwmsg->sibling->sibling = rule;
2464 fwmsg->sibling = rule;
2467 static struct ip_fw *
2468 ipfw_create_rule(const struct ipfw_ioc_rule *ioc_rule, struct ip_fw_stub *stub)
2472 rule = kmalloc(RULESIZE(ioc_rule), M_IPFW, M_WAITOK | M_ZERO);
2474 rule->act_ofs = ioc_rule->act_ofs;
2475 rule->cmd_len = ioc_rule->cmd_len;
2476 rule->rulenum = ioc_rule->rulenum;
2477 rule->set = ioc_rule->set;
2478 rule->usr_flags = ioc_rule->usr_flags;
2480 bcopy(ioc_rule->cmd, rule->cmd, rule->cmd_len * 4 /* XXX */);
2483 rule->cpuid = mycpuid;
2487 stub->rule[mycpuid] = rule;
2493 ipfw_add_rule_dispatch(netmsg_t nmsg)
2495 struct netmsg_ipfw *fwmsg = (struct netmsg_ipfw *)nmsg;
2496 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2499 rule = ipfw_create_rule(fwmsg->ioc_rule, fwmsg->stub);
2502 * Insert rule into the pre-determined position
2504 if (fwmsg->prev_rule != NULL) {
2505 struct ip_fw *prev, *next;
2507 prev = fwmsg->prev_rule;
2508 KKASSERT(prev->cpuid == mycpuid);
2510 next = fwmsg->next_rule;
2511 KKASSERT(next->cpuid == mycpuid);
2517 * Move to the position on the next CPU
2518 * before the msg is forwarded.
2520 fwmsg->prev_rule = prev->sibling;
2521 fwmsg->next_rule = next->sibling;
2523 KKASSERT(fwmsg->next_rule == NULL);
2524 rule->next = ctx->ipfw_layer3_chain;
2525 ctx->ipfw_layer3_chain = rule;
2528 /* Link rule CPU sibling */
2529 ipfw_link_sibling(fwmsg, rule);
2531 ipfw_flush_rule_ptrs(ctx);
2534 /* Statistics only need to be updated once */
2535 ipfw_inc_static_count(rule);
2537 /* Return the rule on CPU0 */
2538 nmsg->lmsg.u.ms_resultp = rule;
2541 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
2545 ipfw_enable_state_dispatch(netmsg_t nmsg)
2547 struct lwkt_msg *lmsg = &nmsg->lmsg;
2548 struct ip_fw *rule = lmsg->u.ms_resultp;
2550 KKASSERT(rule->cpuid == mycpuid);
2551 KKASSERT(rule->stub != NULL && rule->stub->rule[mycpuid] == rule);
2552 KKASSERT(!(rule->rule_flags & IPFW_RULE_F_STATE));
2553 rule->rule_flags |= IPFW_RULE_F_STATE;
2554 lmsg->u.ms_resultp = rule->sibling;
2556 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
2560 * Add a new rule to the list. Copy the rule into a malloc'ed area,
2561 * then possibly create a rule number and add the rule to the list.
2562 * Update the rule_number in the input struct so the caller knows
2566 ipfw_add_rule(struct ipfw_ioc_rule *ioc_rule, uint32_t rule_flags)
2568 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2569 struct netmsg_ipfw fwmsg;
2570 struct netmsg_base *nmsg;
2571 struct ip_fw *f, *prev, *rule;
2572 struct ip_fw_stub *stub;
2574 IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
2577 * If rulenum is 0, find highest numbered rule before the
2578 * default rule, and add rule number incremental step.
2580 if (ioc_rule->rulenum == 0) {
2581 int step = autoinc_step;
2583 KKASSERT(step >= IPFW_AUTOINC_STEP_MIN &&
2584 step <= IPFW_AUTOINC_STEP_MAX);
2587 * Locate the highest numbered rule before default
2589 for (f = ctx->ipfw_layer3_chain; f; f = f->next) {
2590 if (f->rulenum == IPFW_DEFAULT_RULE)
2592 ioc_rule->rulenum = f->rulenum;
2594 if (ioc_rule->rulenum < IPFW_DEFAULT_RULE - step)
2595 ioc_rule->rulenum += step;
2597 KASSERT(ioc_rule->rulenum != IPFW_DEFAULT_RULE &&
2598 ioc_rule->rulenum != 0,
2599 ("invalid rule num %d", ioc_rule->rulenum));
2602 * Now find the right place for the new rule in the sorted list.
2604 for (prev = NULL, f = ctx->ipfw_layer3_chain; f;
2605 prev = f, f = f->next) {
2606 if (f->rulenum > ioc_rule->rulenum) {
2607 /* Found the location */
2611 KASSERT(f != NULL, ("no default rule?!"));
2613 if (rule_flags & IPFW_RULE_F_STATE) {
2617 * If the new rule will create states, then allocate
2618 * a rule stub, which will be referenced by states
2621 size = sizeof(*stub) + ((ncpus - 1) * sizeof(struct ip_fw *));
2622 stub = kmalloc(size, M_IPFW, M_WAITOK | M_ZERO);
2628 * Duplicate the rule onto each CPU.
2629 * The rule duplicated on CPU0 will be returned.
2631 bzero(&fwmsg, sizeof(fwmsg));
2633 netmsg_init(nmsg, NULL, &curthread->td_msgport,
2634 0, ipfw_add_rule_dispatch);
2635 fwmsg.ioc_rule = ioc_rule;
2636 fwmsg.prev_rule = prev;
2637 fwmsg.next_rule = prev == NULL ? NULL : f;
2640 netisr_domsg(nmsg, 0);
2641 KKASSERT(fwmsg.prev_rule == NULL && fwmsg.next_rule == NULL);
2643 rule = nmsg->lmsg.u.ms_resultp;
2644 KKASSERT(rule != NULL && rule->cpuid == mycpuid);
2646 if (rule_flags & IPFW_RULE_F_STATE) {
2648 * Turn on state flag, _after_ everything on all
2649 * CPUs have been setup.
2651 bzero(nmsg, sizeof(*nmsg));
2652 netmsg_init(nmsg, NULL, &curthread->td_msgport,
2653 0, ipfw_enable_state_dispatch);
2654 nmsg->lmsg.u.ms_resultp = rule;
2656 netisr_domsg(nmsg, 0);
2657 KKASSERT(nmsg->lmsg.u.ms_resultp == NULL);
2660 DPRINTF("++ installed rule %d, static count now %d\n",
2661 rule->rulenum, static_count);
2665 * Free storage associated with a static rule (including derived
2667 * The caller is in charge of clearing rule pointers to avoid
2668 * dangling pointers.
2669 * @return a pointer to the next entry.
2670 * Arguments are not checked, so they better be correct.
2672 static struct ip_fw *
2673 ipfw_delete_rule(struct ipfw_context *ctx,
2674 struct ip_fw *prev, struct ip_fw *rule)
2677 struct ip_fw_stub *stub;
2679 /* STATE flag should have been cleared before we reach here */
2680 KKASSERT((rule->rule_flags & IPFW_RULE_F_STATE) == 0);
2685 ctx->ipfw_layer3_chain = n;
2689 /* Mark the rule as invalid */
2690 rule->rule_flags |= IPFW_RULE_F_INVALID;
2691 rule->next_rule = NULL;
2692 rule->sibling = NULL;
2695 /* Don't reset cpuid here; keep various assertion working */
2699 /* Statistics only need to be updated once */
2701 ipfw_dec_static_count(rule);
2703 /* Free 'stub' on the last CPU */
2704 if (stub != NULL && mycpuid == ncpus - 1)
2705 kfree(stub, M_IPFW);
2707 /* Try to free this rule */
2708 ipfw_free_rule(rule);
2710 /* Return the next rule */
2715 ipfw_flush_dispatch(netmsg_t nmsg)
2717 struct lwkt_msg *lmsg = &nmsg->lmsg;
2718 int kill_default = lmsg->u.ms_result;
2719 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2722 ipfw_flush_rule_ptrs(ctx); /* more efficient to do outside the loop */
2724 while ((rule = ctx->ipfw_layer3_chain) != NULL &&
2725 (kill_default || rule->rulenum != IPFW_DEFAULT_RULE))
2726 ipfw_delete_rule(ctx, NULL, rule);
2728 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
2732 ipfw_disable_rule_state_dispatch(netmsg_t nmsg)
2734 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
2735 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2738 rule = dmsg->start_rule;
2740 KKASSERT(rule->cpuid == mycpuid);
2743 * Move to the position on the next CPU
2744 * before the msg is forwarded.
2746 dmsg->start_rule = rule->sibling;
2748 KKASSERT(dmsg->rulenum == 0);
2749 rule = ctx->ipfw_layer3_chain;
2752 while (rule != NULL) {
2753 if (dmsg->rulenum && rule->rulenum != dmsg->rulenum)
2755 rule->rule_flags &= ~IPFW_RULE_F_STATE;
2759 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
2763 * Deletes all rules from a chain (including the default rule
2764 * if the second argument is set).
2767 ipfw_flush(int kill_default)
2769 struct netmsg_del dmsg;
2770 struct netmsg_base nmsg;
2771 struct lwkt_msg *lmsg;
2773 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2775 IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
2778 * If 'kill_default' then caller has done the necessary
2779 * msgport syncing; unnecessary to do it again.
2781 if (!kill_default) {
2783 * Let ipfw_chk() know the rules are going to
2784 * be flushed, so it could jump directly to
2788 netmsg_service_sync();
2792 * Clear STATE flag on rules, so no more states (dyn rules)
2795 bzero(&dmsg, sizeof(dmsg));
2796 netmsg_init(&dmsg.base, NULL, &curthread->td_msgport,
2797 0, ipfw_disable_rule_state_dispatch);
2798 netisr_domsg(&dmsg.base, 0);
2801 * This actually nukes all states (dyn rules)
2803 lockmgr(&dyn_lock, LK_EXCLUSIVE);
2804 for (rule = ctx->ipfw_layer3_chain; rule != NULL; rule = rule->next) {
2806 * Can't check IPFW_RULE_F_STATE here,
2807 * since it has been cleared previously.
2808 * Check 'stub' instead.
2810 if (rule->stub != NULL) {
2812 remove_dyn_rule_locked(rule, NULL);
2815 lockmgr(&dyn_lock, LK_RELEASE);
2818 * Press the 'flush' button
2820 bzero(&nmsg, sizeof(nmsg));
2821 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
2822 0, ipfw_flush_dispatch);
2824 lmsg->u.ms_result = kill_default;
2825 netisr_domsg(&nmsg, 0);
2827 KASSERT(dyn_count == 0, ("%u dyn rule remains", dyn_count));
2830 if (ipfw_dyn_v != NULL) {
2832 * Free dynamic rules(state) hash table
2834 kfree(ipfw_dyn_v, M_IPFW);
2838 KASSERT(static_count == 0,
2839 ("%u static rules remain", static_count));
2840 KASSERT(static_ioc_len == 0,
2841 ("%u bytes of static rules remain", static_ioc_len));
2843 KASSERT(static_count == 1,
2844 ("%u static rules remain", static_count));
2845 KASSERT(static_ioc_len == IOC_RULESIZE(ctx->ipfw_default_rule),
2846 ("%u bytes of static rules remain, should be %lu",
2848 (u_long)IOC_RULESIZE(ctx->ipfw_default_rule)));
2856 ipfw_alt_delete_rule_dispatch(netmsg_t nmsg)
2858 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
2859 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2860 struct ip_fw *rule, *prev;
2862 rule = dmsg->start_rule;
2863 KKASSERT(rule->cpuid == mycpuid);
2864 dmsg->start_rule = rule->sibling;
2866 prev = dmsg->prev_rule;
2868 KKASSERT(prev->cpuid == mycpuid);
2871 * Move to the position on the next CPU
2872 * before the msg is forwarded.
2874 dmsg->prev_rule = prev->sibling;
2878 * flush pointers outside the loop, then delete all matching
2879 * rules. 'prev' remains the same throughout the cycle.
2881 ipfw_flush_rule_ptrs(ctx);
2882 while (rule && rule->rulenum == dmsg->rulenum)
2883 rule = ipfw_delete_rule(ctx, prev, rule);
2885 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
2889 ipfw_alt_delete_rule(uint16_t rulenum)
2891 struct ip_fw *prev, *rule, *f;
2892 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2893 struct netmsg_del dmsg;
2894 struct netmsg_base *nmsg;
2898 * Locate first rule to delete
2900 for (prev = NULL, rule = ctx->ipfw_layer3_chain;
2901 rule && rule->rulenum < rulenum;
2902 prev = rule, rule = rule->next)
2904 if (rule->rulenum != rulenum)
2908 * Check whether any rules with the given number will
2912 for (f = rule; f && f->rulenum == rulenum; f = f->next) {
2913 if (f->rule_flags & IPFW_RULE_F_STATE) {
2921 * Clear the STATE flag, so no more states will be
2922 * created based the rules numbered 'rulenum'.
2924 bzero(&dmsg, sizeof(dmsg));
2926 netmsg_init(nmsg, NULL, &curthread->td_msgport,
2927 0, ipfw_disable_rule_state_dispatch);
2928 dmsg.start_rule = rule;
2929 dmsg.rulenum = rulenum;
2931 netisr_domsg(nmsg, 0);
2932 KKASSERT(dmsg.start_rule == NULL);
2935 * Nuke all related states
2937 lockmgr(&dyn_lock, LK_EXCLUSIVE);
2938 for (f = rule; f && f->rulenum == rulenum; f = f->next) {
2940 * Can't check IPFW_RULE_F_STATE here,
2941 * since it has been cleared previously.
2942 * Check 'stub' instead.
2944 if (f->stub != NULL) {
2946 remove_dyn_rule_locked(f, NULL);
2949 lockmgr(&dyn_lock, LK_RELEASE);
2953 * Get rid of the rule duplications on all CPUs
2955 bzero(&dmsg, sizeof(dmsg));
2957 netmsg_init(nmsg, NULL, &curthread->td_msgport,
2958 0, ipfw_alt_delete_rule_dispatch);
2959 dmsg.prev_rule = prev;
2960 dmsg.start_rule = rule;
2961 dmsg.rulenum = rulenum;
2963 netisr_domsg(nmsg, 0);
2964 KKASSERT(dmsg.prev_rule == NULL && dmsg.start_rule == NULL);
2969 ipfw_alt_delete_ruleset_dispatch(netmsg_t nmsg)
2971 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
2972 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
2973 struct ip_fw *prev, *rule;
2978 ipfw_flush_rule_ptrs(ctx);
2981 rule = ctx->ipfw_layer3_chain;
2982 while (rule != NULL) {
2983 if (rule->set == dmsg->from_set) {
2984 rule = ipfw_delete_rule(ctx, prev, rule);
2993 KASSERT(del, ("no match set?!"));
2995 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
2999 ipfw_disable_ruleset_state_dispatch(netmsg_t nmsg)
3001 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3002 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3008 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3009 if (rule->set == dmsg->from_set) {
3013 rule->rule_flags &= ~IPFW_RULE_F_STATE;
3016 KASSERT(cleared, ("no match set?!"));
3018 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3022 ipfw_alt_delete_ruleset(uint8_t set)
3024 struct netmsg_del dmsg;
3025 struct netmsg_base *nmsg;
3028 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3031 * Check whether the 'set' exists. If it exists,
3032 * then check whether any rules within the set will
3033 * try to create states.
3037 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3038 if (rule->set == set) {
3040 if (rule->rule_flags & IPFW_RULE_F_STATE) {
3047 return 0; /* XXX EINVAL? */
3051 * Clear the STATE flag, so no more states will be
3052 * created based the rules in this set.
3054 bzero(&dmsg, sizeof(dmsg));
3056 netmsg_init(nmsg, NULL, &curthread->td_msgport,
3057 0, ipfw_disable_ruleset_state_dispatch);
3058 dmsg.from_set = set;
3060 netisr_domsg(nmsg, 0);
3063 * Nuke all related states
3065 lockmgr(&dyn_lock, LK_EXCLUSIVE);
3066 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3067 if (rule->set != set)
3071 * Can't check IPFW_RULE_F_STATE here,
3072 * since it has been cleared previously.
3073 * Check 'stub' instead.
3075 if (rule->stub != NULL) {
3077 remove_dyn_rule_locked(rule, NULL);
3080 lockmgr(&dyn_lock, LK_RELEASE);
3086 bzero(&dmsg, sizeof(dmsg));
3088 netmsg_init(nmsg, NULL, &curthread->td_msgport,
3089 0, ipfw_alt_delete_ruleset_dispatch);
3090 dmsg.from_set = set;
3092 netisr_domsg(nmsg, 0);
3097 ipfw_alt_move_rule_dispatch(netmsg_t nmsg)
3099 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3102 rule = dmsg->start_rule;
3103 KKASSERT(rule->cpuid == mycpuid);
3106 * Move to the position on the next CPU
3107 * before the msg is forwarded.
3109 dmsg->start_rule = rule->sibling;
3111 while (rule && rule->rulenum <= dmsg->rulenum) {
3112 if (rule->rulenum == dmsg->rulenum)
3113 rule->set = dmsg->to_set;
3116 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3120 ipfw_alt_move_rule(uint16_t rulenum, uint8_t set)
3122 struct netmsg_del dmsg;
3123 struct netmsg_base *nmsg;
3125 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3128 * Locate first rule to move
3130 for (rule = ctx->ipfw_layer3_chain; rule && rule->rulenum <= rulenum;
3131 rule = rule->next) {
3132 if (rule->rulenum == rulenum && rule->set != set)
3135 if (rule == NULL || rule->rulenum > rulenum)
3136 return 0; /* XXX error? */
3138 bzero(&dmsg, sizeof(dmsg));
3140 netmsg_init(nmsg, NULL, &curthread->td_msgport,
3141 0, ipfw_alt_move_rule_dispatch);
3142 dmsg.start_rule = rule;
3143 dmsg.rulenum = rulenum;
3146 netisr_domsg(nmsg, 0);
3147 KKASSERT(dmsg.start_rule == NULL);
3152 ipfw_alt_move_ruleset_dispatch(netmsg_t nmsg)
3154 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3155 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3158 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3159 if (rule->set == dmsg->from_set)
3160 rule->set = dmsg->to_set;
3162 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3166 ipfw_alt_move_ruleset(uint8_t from_set, uint8_t to_set)
3168 struct netmsg_del dmsg;
3169 struct netmsg_base *nmsg;
3171 bzero(&dmsg, sizeof(dmsg));
3173 netmsg_init(nmsg, NULL, &curthread->td_msgport,
3174 0, ipfw_alt_move_ruleset_dispatch);
3175 dmsg.from_set = from_set;
3176 dmsg.to_set = to_set;
3178 netisr_domsg(nmsg, 0);
3183 ipfw_alt_swap_ruleset_dispatch(netmsg_t nmsg)
3185 struct netmsg_del *dmsg = (struct netmsg_del *)nmsg;
3186 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3189 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3190 if (rule->set == dmsg->from_set)
3191 rule->set = dmsg->to_set;
3192 else if (rule->set == dmsg->to_set)
3193 rule->set = dmsg->from_set;
3195 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3199 ipfw_alt_swap_ruleset(uint8_t set1, uint8_t set2)
3201 struct netmsg_del dmsg;
3202 struct netmsg_base *nmsg;
3204 bzero(&dmsg, sizeof(dmsg));
3206 netmsg_init(nmsg, NULL, &curthread->td_msgport,
3207 0, ipfw_alt_swap_ruleset_dispatch);
3208 dmsg.from_set = set1;
3211 netisr_domsg(nmsg, 0);
3216 * Remove all rules with given number, and also do set manipulation.
3218 * The argument is an uint32_t. The low 16 bit are the rule or set number,
3219 * the next 8 bits are the new set, the top 8 bits are the command:
3221 * 0 delete rules with given number
3222 * 1 delete rules with given set number
3223 * 2 move rules with given number to new set
3224 * 3 move rules with given set number to new set
3225 * 4 swap sets with given numbers
3228 ipfw_ctl_alter(uint32_t arg)
3231 uint8_t cmd, new_set;
3234 rulenum = arg & 0xffff;
3235 cmd = (arg >> 24) & 0xff;
3236 new_set = (arg >> 16) & 0xff;
3240 if (new_set >= IPFW_DEFAULT_SET)
3242 if (cmd == 0 || cmd == 2) {
3243 if (rulenum == IPFW_DEFAULT_RULE)
3246 if (rulenum >= IPFW_DEFAULT_SET)
3251 case 0: /* delete rules with given number */
3252 error = ipfw_alt_delete_rule(rulenum);
3255 case 1: /* delete all rules with given set number */
3256 error = ipfw_alt_delete_ruleset(rulenum);
3259 case 2: /* move rules with given number to new set */
3260 error = ipfw_alt_move_rule(rulenum, new_set);
3263 case 3: /* move rules with given set number to new set */
3264 error = ipfw_alt_move_ruleset(rulenum, new_set);
3267 case 4: /* swap two sets */
3268 error = ipfw_alt_swap_ruleset(rulenum, new_set);
3275 * Clear counters for a specific rule.
3278 clear_counters(struct ip_fw *rule, int log_only)
3280 ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
3282 if (log_only == 0) {
3283 rule->bcnt = rule->pcnt = 0;
3284 rule->timestamp = 0;
3286 if (l->o.opcode == O_LOG)
3287 l->log_left = l->max_log;
3291 ipfw_zero_entry_dispatch(netmsg_t nmsg)
3293 struct netmsg_zent *zmsg = (struct netmsg_zent *)nmsg;
3294 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3297 if (zmsg->rulenum == 0) {
3298 KKASSERT(zmsg->start_rule == NULL);
3300 ctx->ipfw_norule_counter = 0;
3301 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next)
3302 clear_counters(rule, zmsg->log_only);
3304 struct ip_fw *start = zmsg->start_rule;
3306 KKASSERT(start->cpuid == mycpuid);
3307 KKASSERT(start->rulenum == zmsg->rulenum);
3310 * We can have multiple rules with the same number, so we
3311 * need to clear them all.
3313 for (rule = start; rule && rule->rulenum == zmsg->rulenum;
3315 clear_counters(rule, zmsg->log_only);
3318 * Move to the position on the next CPU
3319 * before the msg is forwarded.
3321 zmsg->start_rule = start->sibling;
3323 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3327 * Reset some or all counters on firewall rules.
3328 * @arg frwl is null to clear all entries, or contains a specific
3330 * @arg log_only is 1 if we only want to reset logs, zero otherwise.
3333 ipfw_ctl_zero_entry(int rulenum, int log_only)
3335 struct netmsg_zent zmsg;
3336 struct netmsg_base *nmsg;
3338 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3340 bzero(&zmsg, sizeof(zmsg));
3342 netmsg_init(nmsg, NULL, &curthread->td_msgport,
3343 0, ipfw_zero_entry_dispatch);
3344 zmsg.log_only = log_only;
3347 msg = log_only ? "ipfw: All logging counts reset.\n"
3348 : "ipfw: Accounting cleared.\n";
3353 * Locate the first rule with 'rulenum'
3355 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next) {
3356 if (rule->rulenum == rulenum)
3359 if (rule == NULL) /* we did not find any matching rules */
3361 zmsg.start_rule = rule;
3362 zmsg.rulenum = rulenum;
3364 msg = log_only ? "ipfw: Entry %d logging count reset.\n"
3365 : "ipfw: Entry %d cleared.\n";
3367 netisr_domsg(nmsg, 0);
3368 KKASSERT(zmsg.start_rule == NULL);
3371 log(LOG_SECURITY | LOG_NOTICE, msg, rulenum);
3376 * Check validity of the structure before insert.
3377 * Fortunately rules are simple, so this mostly need to check rule sizes.
3380 ipfw_check_ioc_rule(struct ipfw_ioc_rule *rule, int size, uint32_t *rule_flags)
3383 int have_action = 0;
3388 /* Check for valid size */
3389 if (size < sizeof(*rule)) {
3390 kprintf("ipfw: rule too short\n");
3393 l = IOC_RULESIZE(rule);
3395 kprintf("ipfw: size mismatch (have %d want %d)\n", size, l);
3399 /* Check rule number */
3400 if (rule->rulenum == IPFW_DEFAULT_RULE) {
3401 kprintf("ipfw: invalid rule number\n");
3406 * Now go for the individual checks. Very simple ones, basically only
3407 * instruction sizes.
3409 for (l = rule->cmd_len, cmd = rule->cmd; l > 0;
3410 l -= cmdlen, cmd += cmdlen) {
3411 cmdlen = F_LEN(cmd);
3413 kprintf("ipfw: opcode %d size truncated\n",
3418 DPRINTF("ipfw: opcode %d\n", cmd->opcode);
3420 if (cmd->opcode == O_KEEP_STATE || cmd->opcode == O_LIMIT) {
3421 /* This rule will create states */
3422 *rule_flags |= IPFW_RULE_F_STATE;
3425 switch (cmd->opcode) {
3439 case O_IPPRECEDENCE:
3446 if (cmdlen != F_INSN_SIZE(ipfw_insn))
3458 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32))
3463 if (cmdlen != F_INSN_SIZE(ipfw_insn_limit))
3468 if (cmdlen != F_INSN_SIZE(ipfw_insn_log))
3471 ((ipfw_insn_log *)cmd)->log_left =
3472 ((ipfw_insn_log *)cmd)->max_log;
3478 if (cmdlen != F_INSN_SIZE(ipfw_insn_ip))
3480 if (((ipfw_insn_ip *)cmd)->mask.s_addr == 0) {
3481 kprintf("ipfw: opcode %d, useless rule\n",
3489 if (cmd->arg1 == 0 || cmd->arg1 > 256) {
3490 kprintf("ipfw: invalid set size %d\n",
3494 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
3500 if (cmdlen != F_INSN_SIZE(ipfw_insn_mac))
3506 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */
3507 if (cmdlen < 2 || cmdlen > 31)
3514 if (cmdlen != F_INSN_SIZE(ipfw_insn_if))
3520 if (cmdlen != F_INSN_SIZE(ipfw_insn_pipe))
3525 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa)) {
3530 fwd_addr = ((ipfw_insn_sa *)cmd)->
3532 if (IN_MULTICAST(ntohl(fwd_addr))) {
3533 kprintf("ipfw: try forwarding to "
3534 "multicast address\n");
3540 case O_FORWARD_MAC: /* XXX not implemented yet */
3549 if (cmdlen != F_INSN_SIZE(ipfw_insn))
3553 kprintf("ipfw: opcode %d, multiple actions"
3560 kprintf("ipfw: opcode %d, action must be"
3567 kprintf("ipfw: opcode %d, unknown opcode\n",
3572 if (have_action == 0) {
3573 kprintf("ipfw: missing action\n");
3579 kprintf("ipfw: opcode %d size %d wrong\n",
3580 cmd->opcode, cmdlen);
3585 ipfw_ctl_add_rule(struct sockopt *sopt)
3587 struct ipfw_ioc_rule *ioc_rule;
3589 uint32_t rule_flags;
3592 size = sopt->sopt_valsize;
3593 if (size > (sizeof(uint32_t) * IPFW_RULE_SIZE_MAX) ||
3594 size < sizeof(*ioc_rule)) {
3597 if (size != (sizeof(uint32_t) * IPFW_RULE_SIZE_MAX)) {
3598 sopt->sopt_val = krealloc(sopt->sopt_val, sizeof(uint32_t) *
3599 IPFW_RULE_SIZE_MAX, M_TEMP, M_WAITOK);
3601 ioc_rule = sopt->sopt_val;
3603 error = ipfw_check_ioc_rule(ioc_rule, size, &rule_flags);
3607 ipfw_add_rule(ioc_rule, rule_flags);
3609 if (sopt->sopt_dir == SOPT_GET)
3610 sopt->sopt_valsize = IOC_RULESIZE(ioc_rule);
3615 ipfw_copy_rule(const struct ip_fw *rule, struct ipfw_ioc_rule *ioc_rule)
3617 const struct ip_fw *sibling;
3622 KKASSERT(rule->cpuid == IPFW_CFGCPUID);
3624 ioc_rule->act_ofs = rule->act_ofs;
3625 ioc_rule->cmd_len = rule->cmd_len;
3626 ioc_rule->rulenum = rule->rulenum;
3627 ioc_rule->set = rule->set;
3628 ioc_rule->usr_flags = rule->usr_flags;
3630 ioc_rule->set_disable = ipfw_ctx[mycpuid]->ipfw_set_disable;
3631 ioc_rule->static_count = static_count;
3632 ioc_rule->static_len = static_ioc_len;
3635 * Visit (read-only) all of the rule's duplications to get
3636 * the necessary statistics
3643 ioc_rule->timestamp = 0;
3644 for (sibling = rule; sibling != NULL; sibling = sibling->sibling) {
3645 ioc_rule->pcnt += sibling->pcnt;
3646 ioc_rule->bcnt += sibling->bcnt;
3647 if (sibling->timestamp > ioc_rule->timestamp)
3648 ioc_rule->timestamp = sibling->timestamp;
3653 KASSERT(i == ncpus, ("static rule is not duplicated on every cpu"));
3655 bcopy(rule->cmd, ioc_rule->cmd, ioc_rule->cmd_len * 4 /* XXX */);
3657 return ((uint8_t *)ioc_rule + IOC_RULESIZE(ioc_rule));
3661 ipfw_copy_state(const ipfw_dyn_rule *dyn_rule,
3662 struct ipfw_ioc_state *ioc_state)
3664 const struct ipfw_flow_id *id;
3665 struct ipfw_ioc_flowid *ioc_id;
3667 ioc_state->expire = TIME_LEQ(dyn_rule->expire, time_second) ?
3668 0 : dyn_rule->expire - time_second;
3669 ioc_state->pcnt = dyn_rule->pcnt;
3670 ioc_state->bcnt = dyn_rule->bcnt;
3672 ioc_state->dyn_type = dyn_rule->dyn_type;
3673 ioc_state->count = dyn_rule->count;
3675 ioc_state->rulenum = dyn_rule->stub->rule[mycpuid]->rulenum;
3678 ioc_id = &ioc_state->id;
3680 ioc_id->type = ETHERTYPE_IP;
3681 ioc_id->u.ip.dst_ip = id->dst_ip;
3682 ioc_id->u.ip.src_ip = id->src_ip;
3683 ioc_id->u.ip.dst_port = id->dst_port;
3684 ioc_id->u.ip.src_port = id->src_port;
3685 ioc_id->u.ip.proto = id->proto;
3689 ipfw_ctl_get_rules(struct sockopt *sopt)
3691 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3695 uint32_t dcount = 0;
3698 * pass up a copy of the current rules. Static rules
3699 * come first (the last of which has number IPFW_DEFAULT_RULE),
3700 * followed by a possibly empty list of dynamic rule.
3703 size = static_ioc_len; /* size of static rules */
3704 if (ipfw_dyn_v) { /* add size of dyn.rules */
3706 size += dcount * sizeof(struct ipfw_ioc_state);
3709 if (sopt->sopt_valsize < size) {
3710 /* short length, no need to return incomplete rules */
3711 /* XXX: if superuser, no need to zero buffer */
3712 bzero(sopt->sopt_val, sopt->sopt_valsize);
3715 bp = sopt->sopt_val;
3717 for (rule = ctx->ipfw_layer3_chain; rule; rule = rule->next)
3718 bp = ipfw_copy_rule(rule, bp);
3720 if (ipfw_dyn_v && dcount != 0) {
3721 struct ipfw_ioc_state *ioc_state = bp;
3722 uint32_t dcount2 = 0;
3724 size_t old_size = size;
3728 lockmgr(&dyn_lock, LK_SHARED);
3730 /* Check 'ipfw_dyn_v' again with lock held */
3731 if (ipfw_dyn_v == NULL)
3734 for (i = 0; i < curr_dyn_buckets; i++) {
3738 * The # of dynamic rules may have grown after the
3739 * snapshot of 'dyn_count' was taken, so we will have
3740 * to check 'dcount' (snapshot of dyn_count) here to
3741 * make sure that we don't overflow the pre-allocated
3744 for (p = ipfw_dyn_v[i]; p != NULL && dcount != 0;
3745 p = p->next, ioc_state++, dcount--, dcount2++)
3746 ipfw_copy_state(p, ioc_state);
3749 lockmgr(&dyn_lock, LK_RELEASE);
3752 * The # of dynamic rules may be shrinked after the
3753 * snapshot of 'dyn_count' was taken. To give user a
3754 * correct dynamic rule count, we use the 'dcount2'
3755 * calculated above (with shared lockmgr lock held).
3757 size = static_ioc_len +
3758 (dcount2 * sizeof(struct ipfw_ioc_state));
3759 KKASSERT(size <= old_size);
3762 sopt->sopt_valsize = size;
3767 ipfw_set_disable_dispatch(netmsg_t nmsg)
3769 struct lwkt_msg *lmsg = &nmsg->lmsg;
3770 struct ipfw_context *ctx = ipfw_ctx[mycpuid];
3772 ctx->ipfw_set_disable = lmsg->u.ms_result32;
3774 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3778 ipfw_ctl_set_disable(uint32_t disable, uint32_t enable)
3780 struct netmsg_base nmsg;
3781 struct lwkt_msg *lmsg;
3782 uint32_t set_disable;
3784 /* IPFW_DEFAULT_SET is always enabled */
3785 enable |= (1 << IPFW_DEFAULT_SET);
3786 set_disable = (ipfw_ctx[mycpuid]->ipfw_set_disable | disable) & ~enable;
3788 bzero(&nmsg, sizeof(nmsg));
3789 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
3790 0, ipfw_set_disable_dispatch);
3792 lmsg->u.ms_result32 = set_disable;
3794 netisr_domsg(&nmsg, 0);
3798 * {set|get}sockopt parser.
3801 ipfw_ctl(struct sockopt *sopt)
3809 switch (sopt->sopt_name) {
3811 error = ipfw_ctl_get_rules(sopt);
3815 ipfw_flush(0 /* keep default rule */);
3819 error = ipfw_ctl_add_rule(sopt);
3824 * IP_FW_DEL is used for deleting single rules or sets,
3825 * and (ab)used to atomically manipulate sets.
3826 * Argument size is used to distinguish between the two:
3828 * delete single rule or set of rules,
3829 * or reassign rules (or sets) to a different set.
3830 * 2 * sizeof(uint32_t)
3831 * atomic disable/enable sets.
3832 * first uint32_t contains sets to be disabled,
3833 * second uint32_t contains sets to be enabled.
3835 masks = sopt->sopt_val;
3836 size = sopt->sopt_valsize;
3837 if (size == sizeof(*masks)) {
3839 * Delete or reassign static rule
3841 error = ipfw_ctl_alter(masks[0]);
3842 } else if (size == (2 * sizeof(*masks))) {
3844 * Set enable/disable
3846 ipfw_ctl_set_disable(masks[0], masks[1]);
3853 case IP_FW_RESETLOG: /* argument is an int, the rule number */
3856 if (sopt->sopt_val != 0) {
3857 error = soopt_to_kbuf(sopt, &rulenum,
3858 sizeof(int), sizeof(int));
3862 error = ipfw_ctl_zero_entry(rulenum,
3863 sopt->sopt_name == IP_FW_RESETLOG);
3867 kprintf("ipfw_ctl invalid option %d\n", sopt->sopt_name);
3874 * This procedure is only used to handle keepalives. It is invoked
3875 * every dyn_keepalive_period
3878 ipfw_tick_dispatch(netmsg_t nmsg)
3884 IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
3885 KKASSERT(IPFW_LOADED);
3889 lwkt_replymsg(&nmsg->lmsg, 0);
3892 if (ipfw_dyn_v == NULL || dyn_count == 0)
3895 keep_alive = time_second;
3897 lockmgr(&dyn_lock, LK_EXCLUSIVE);
3899 if (ipfw_dyn_v == NULL || dyn_count == 0) {
3900 lockmgr(&dyn_lock, LK_RELEASE);
3903 gen = dyn_buckets_gen;
3905 for (i = 0; i < curr_dyn_buckets; i++) {
3906 ipfw_dyn_rule *q, *prev;
3908 for (prev = NULL, q = ipfw_dyn_v[i]; q != NULL;) {
3909 uint32_t ack_rev, ack_fwd;
3910 struct ipfw_flow_id id;
3912 if (q->dyn_type == O_LIMIT_PARENT)
3915 if (TIME_LEQ(q->expire, time_second)) {
3917 UNLINK_DYN_RULE(prev, ipfw_dyn_v[i], q);
3922 * Keep alive processing
3927 if (q->id.proto != IPPROTO_TCP)
3929 if ((q->state & BOTH_SYN) != BOTH_SYN)
3931 if (TIME_LEQ(time_second + dyn_keepalive_interval,
3933 goto next; /* too early */
3934 if (q->keep_alive == keep_alive)
3935 goto next; /* alreay done */
3938 * Save necessary information, so that they could
3939 * survive after possible blocking in send_pkt()
3942 ack_rev = q->ack_rev;
3943 ack_fwd = q->ack_fwd;
3945 /* Sending has been started */
3946 q->keep_alive = keep_alive;
3948 /* Release lock to avoid possible dead lock */
3949 lockmgr(&dyn_lock, LK_RELEASE);
3950 send_pkt(&id, ack_rev - 1, ack_fwd, TH_SYN);
3951 send_pkt(&id, ack_fwd - 1, ack_rev, 0);
3952 lockmgr(&dyn_lock, LK_EXCLUSIVE);
3954 if (gen != dyn_buckets_gen) {
3956 * Dyn bucket array has been changed during
3957 * the above two sending; reiterate.
3966 lockmgr(&dyn_lock, LK_RELEASE);
3968 callout_reset(&ipfw_timeout_h, dyn_keepalive_period * hz,
3973 * This procedure is only used to handle keepalives. It is invoked
3974 * every dyn_keepalive_period
3977 ipfw_tick(void *dummy __unused)
3979 struct lwkt_msg *lmsg = &ipfw_timeout_netmsg.lmsg;
3981 KKASSERT(mycpuid == IPFW_CFGCPUID);
3985 KKASSERT(lmsg->ms_flags & MSGF_DONE);
3987 lwkt_sendmsg_oncpu(IPFW_CFGPORT, lmsg);
3988 /* ipfw_timeout_netmsg's handler reset this callout */
3995 ipfw_check_in(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir)
3997 struct ip_fw_args args;
3998 struct mbuf *m = *m0;
4000 int tee = 0, error = 0, ret;
4002 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
4003 /* Extract info from dummynet tag */
4004 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
4005 KKASSERT(mtag != NULL);
4006 args.rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
4007 KKASSERT(args.rule != NULL);
4009 m_tag_delete(m, mtag);
4010 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
4018 ret = ipfw_chk(&args);
4036 case IP_FW_DUMMYNET:
4037 /* Send packet to the appropriate pipe */
4038 ipfw_dummynet_io(m, args.cookie, DN_TO_IP_IN, &args);
4047 * Must clear bridge tag when changing
4049 m->m_pkthdr.fw_flags &= ~BRIDGE_MBUF_TAGGED;
4050 if (ip_divert_p != NULL) {
4051 m = ip_divert_p(m, tee, 1);
4055 /* not sure this is the right error msg */
4061 panic("unknown ipfw return value: %d", ret);
4069 ipfw_check_out(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir)
4071 struct ip_fw_args args;
4072 struct mbuf *m = *m0;
4074 int tee = 0, error = 0, ret;
4076 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
4077 /* Extract info from dummynet tag */
4078 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
4079 KKASSERT(mtag != NULL);
4080 args.rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
4081 KKASSERT(args.rule != NULL);
4083 m_tag_delete(m, mtag);
4084 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
4092 ret = ipfw_chk(&args);
4110 case IP_FW_DUMMYNET:
4111 ipfw_dummynet_io(m, args.cookie, DN_TO_IP_OUT, &args);
4119 if (ip_divert_p != NULL) {
4120 m = ip_divert_p(m, tee, 0);
4124 /* not sure this is the right error msg */
4130 panic("unknown ipfw return value: %d", ret);
4140 struct pfil_head *pfh;
4142 IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
4144 pfh = pfil_head_get(PFIL_TYPE_AF, AF_INET);
4148 pfil_add_hook(ipfw_check_in, NULL, PFIL_IN, pfh);
4149 pfil_add_hook(ipfw_check_out, NULL, PFIL_OUT, pfh);
4155 struct pfil_head *pfh;
4157 IPFW_ASSERT_CFGPORT(&curthread->td_msgport);
4159 pfh = pfil_head_get(PFIL_TYPE_AF, AF_INET);
4163 pfil_remove_hook(ipfw_check_in, NULL, PFIL_IN, pfh);
4164 pfil_remove_hook(ipfw_check_out, NULL, PFIL_OUT, pfh);
4168 ipfw_sysctl_enable_dispatch(netmsg_t nmsg)
4170 struct lwkt_msg *lmsg = &nmsg->lmsg;
4171 int enable = lmsg->u.ms_result;
4173 if (fw_enable == enable)
4182 lwkt_replymsg(lmsg, 0);
4186 ipfw_sysctl_enable(SYSCTL_HANDLER_ARGS)
4188 struct netmsg_base nmsg;
4189 struct lwkt_msg *lmsg;
4193 error = sysctl_handle_int(oidp, &enable, 0, req);
4194 if (error || req->newptr == NULL)
4197 netmsg_init(&nmsg, NULL, &curthread->td_msgport,
4198 0, ipfw_sysctl_enable_dispatch);
4200 lmsg->u.ms_result = enable;
4202 return lwkt_domsg(IPFW_CFGPORT, lmsg, 0);
4206 ipfw_sysctl_autoinc_step(SYSCTL_HANDLER_ARGS)
4208 return sysctl_int_range(oidp, arg1, arg2, req,
4209 IPFW_AUTOINC_STEP_MIN, IPFW_AUTOINC_STEP_MAX);
4213 ipfw_sysctl_dyn_buckets(SYSCTL_HANDLER_ARGS)
4217 lockmgr(&dyn_lock, LK_EXCLUSIVE);
4219 value = dyn_buckets;
4220 error = sysctl_handle_int(oidp, &value, 0, req);
4221 if (error || !req->newptr)
4225 * Make sure we have a power of 2 and
4226 * do not allow more than 64k entries.
4229 if (value <= 1 || value > 65536)
4231 if ((value & (value - 1)) != 0)
4235 dyn_buckets = value;
4237 lockmgr(&dyn_lock, LK_RELEASE);
4242 ipfw_sysctl_dyn_fin(SYSCTL_HANDLER_ARGS)
4244 return sysctl_int_range(oidp, arg1, arg2, req,
4245 1, dyn_keepalive_period - 1);
4249 ipfw_sysctl_dyn_rst(SYSCTL_HANDLER_ARGS)
4251 return sysctl_int_range(oidp, arg1, arg2, req,
4252 1, dyn_keepalive_period - 1);
4256 ipfw_ctx_init_dispatch(netmsg_t nmsg)
4258 struct netmsg_ipfw *fwmsg = (struct netmsg_ipfw *)nmsg;
4259 struct ipfw_context *ctx;
4260 struct ip_fw *def_rule;
4262 ctx = kmalloc(sizeof(*ctx), M_IPFW, M_WAITOK | M_ZERO);
4263 ipfw_ctx[mycpuid] = ctx;
4265 def_rule = kmalloc(sizeof(*def_rule), M_IPFW, M_WAITOK | M_ZERO);
4267 def_rule->act_ofs = 0;
4268 def_rule->rulenum = IPFW_DEFAULT_RULE;
4269 def_rule->cmd_len = 1;
4270 def_rule->set = IPFW_DEFAULT_SET;
4272 def_rule->cmd[0].len = 1;
4273 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
4274 def_rule->cmd[0].opcode = O_ACCEPT;
4276 if (filters_default_to_accept)
4277 def_rule->cmd[0].opcode = O_ACCEPT;
4279 def_rule->cmd[0].opcode = O_DENY;
4282 def_rule->refcnt = 1;
4283 def_rule->cpuid = mycpuid;
4285 /* Install the default rule */
4286 ctx->ipfw_default_rule = def_rule;
4287 ctx->ipfw_layer3_chain = def_rule;
4289 /* Link rule CPU sibling */
4290 ipfw_link_sibling(fwmsg, def_rule);
4292 /* Statistics only need to be updated once */
4294 ipfw_inc_static_count(def_rule);
4296 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
4300 ipfw_init_dispatch(netmsg_t nmsg)
4302 struct netmsg_ipfw fwmsg;
4306 kprintf("IP firewall already loaded\n");
4311 bzero(&fwmsg, sizeof(fwmsg));
4312 netmsg_init(&fwmsg.base, NULL, &curthread->td_msgport,
4313 0, ipfw_ctx_init_dispatch);
4314 netisr_domsg(&fwmsg.base, 0);
4316 ip_fw_chk_ptr = ipfw_chk;
4317 ip_fw_ctl_ptr = ipfw_ctl;
4318 ip_fw_dn_io_ptr = ipfw_dummynet_io;
4320 kprintf("ipfw2 initialized, default to %s, logging ",
4321 ipfw_ctx[mycpuid]->ipfw_default_rule->cmd[0].opcode ==
4322 O_ACCEPT ? "accept" : "deny");
4324 #ifdef IPFIREWALL_VERBOSE
4327 #ifdef IPFIREWALL_VERBOSE_LIMIT
4328 verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
4330 if (fw_verbose == 0) {
4331 kprintf("disabled\n");
4332 } else if (verbose_limit == 0) {
4333 kprintf("unlimited\n");
4335 kprintf("limited to %d packets/entry by default\n",
4339 callout_init_mp(&ipfw_timeout_h);
4340 netmsg_init(&ipfw_timeout_netmsg, NULL, &netisr_adone_rport,
4341 MSGF_DROPABLE | MSGF_PRIORITY,
4342 ipfw_tick_dispatch);
4343 lockinit(&dyn_lock, "ipfw_dyn", 0, 0);
4346 callout_reset(&ipfw_timeout_h, hz, ipfw_tick, NULL);
4351 lwkt_replymsg(&nmsg->lmsg, error);
4357 struct netmsg_base smsg;
4359 netmsg_init(&smsg, NULL, &curthread->td_msgport,
4360 0, ipfw_init_dispatch);
4361 return lwkt_domsg(IPFW_CFGPORT, &smsg.lmsg, 0);
4367 ipfw_fini_dispatch(netmsg_t nmsg)
4371 if (ipfw_refcnt != 0) {
4379 callout_stop(&ipfw_timeout_h);
4381 netmsg_service_sync();
4384 lwkt_dropmsg(&ipfw_timeout_netmsg.lmsg);
4387 ip_fw_chk_ptr = NULL;
4388 ip_fw_ctl_ptr = NULL;
4389 ip_fw_dn_io_ptr = NULL;
4390 ipfw_flush(1 /* kill default rule */);
4392 /* Free pre-cpu context */
4393 for (cpu = 0; cpu < ncpus; ++cpu)
4394 kfree(ipfw_ctx[cpu], M_IPFW);
4396 kprintf("IP firewall unloaded\n");
4398 lwkt_replymsg(&nmsg->lmsg, error);
4404 struct netmsg_base smsg;
4406 netmsg_init(&smsg, NULL, &curthread->td_msgport,
4407 0, ipfw_fini_dispatch);
4408 return lwkt_domsg(IPFW_CFGPORT, &smsg.lmsg, 0);
4411 #endif /* KLD_MODULE */
4414 ipfw_modevent(module_t mod, int type, void *unused)
4425 kprintf("ipfw statically compiled, cannot unload\n");
4437 static moduledata_t ipfwmod = {
4442 DECLARE_MODULE(ipfw, ipfwmod, SI_SUB_PROTO_END, SI_ORDER_ANY);
4443 MODULE_VERSION(ipfw, 1);