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