]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_fw2.c
xnu-792.6.56.tar.gz
[apple/xnu.git] / bsd / netinet / ip_fw2.c
1 /*
2 * Copyright (c) 2002 Luigi Rizzo, Universita` di Pisa
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 *
25 * $FreeBSD: src/sys/netinet/ip_fw2.c,v 1.6.2.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 */
107 static u_int32_t set_disable;
108
109 int fw_verbose;
110 static 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 */
119 static struct ip_fw *layer3_chain;
120
121 MALLOC_DEFINE(M_IPFW, "IpFw/IpAcct", "IpFw/IpAcct chain's");
122
123 static int fw_debug = 1;
124 static int autoinc_step = 100; /* bounded to 1..1000 in add_rule() */
125
126 #ifdef SYSCTL_NODE
127 SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
128 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, enable,
129 CTLFLAG_RW,
130 &fw_enable, 0, "Enable ipfw");
131 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step, CTLFLAG_RW,
132 &autoinc_step, 0, "Rule number autincrement step");
133 SYSCTL_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)");
137 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, debug,
138 CTLFLAG_RW,
139 &fw_debug, 0, "Enable printing of debug ip_fw statements");
140 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose,
141 CTLFLAG_RW,
142 &fw_verbose, 0, "Log matches to ipfw rules");
143 SYSCTL_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 */
182 static ipfw_dyn_rule **ipfw_dyn_v = NULL;
183 static u_int32_t dyn_buckets = 256; /* must be power of 2 */
184 static u_int32_t curr_dyn_buckets = 256; /* must be power of 2 */
185
186 /*
187 * Timeouts for various events in handing dynamic rules.
188 */
189 static u_int32_t dyn_ack_lifetime = 300;
190 static u_int32_t dyn_syn_lifetime = 20;
191 static u_int32_t dyn_fin_lifetime = 1;
192 static u_int32_t dyn_rst_lifetime = 1;
193 static u_int32_t dyn_udp_lifetime = 10;
194 static 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
204 static u_int32_t dyn_keepalive_interval = 20;
205 static u_int32_t dyn_keepalive_period = 5;
206 static u_int32_t dyn_keepalive = 1; /* do send keepalives */
207
208 static u_int32_t static_count; /* # of static rules */
209 static u_int32_t static_len; /* size in bytes of static rules */
210 static u_int32_t dyn_count; /* # of dynamic rules */
211 static u_int32_t dyn_max = 4096; /* max # of dynamic rules */
212
213 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_buckets, CTLFLAG_RW,
214 &dyn_buckets, 0, "Number of dyn. buckets");
215 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, curr_dyn_buckets, CTLFLAG_RD,
216 &curr_dyn_buckets, 0, "Current Number of dyn. buckets");
217 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_count, CTLFLAG_RD,
218 &dyn_count, 0, "Number of dyn. rules");
219 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_max, CTLFLAG_RW,
220 &dyn_max, 0, "Max number of dyn. rules");
221 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count, CTLFLAG_RD,
222 &static_count, 0, "Number of static rules");
223 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_ack_lifetime, CTLFLAG_RW,
224 &dyn_ack_lifetime, 0, "Lifetime of dyn. rules for acks");
225 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_syn_lifetime, CTLFLAG_RW,
226 &dyn_syn_lifetime, 0, "Lifetime of dyn. rules for syn");
227 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_fin_lifetime, CTLFLAG_RW,
228 &dyn_fin_lifetime, 0, "Lifetime of dyn. rules for fin");
229 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_rst_lifetime, CTLFLAG_RW,
230 &dyn_rst_lifetime, 0, "Lifetime of dyn. rules for rst");
231 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_udp_lifetime, CTLFLAG_RW,
232 &dyn_udp_lifetime, 0, "Lifetime of dyn. rules for UDP");
233 SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, dyn_short_lifetime, CTLFLAG_RW,
234 &dyn_short_lifetime, 0, "Lifetime of dyn. rules for other situations");
235 SYSCTL_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
241 static ip_fw_chk_t ipfw_chk;
242
243 /* firewall lock */
244 lck_grp_t *ipfw_mutex_grp;
245 lck_grp_attr_t *ipfw_mutex_grp_attr;
246 lck_attr_t *ipfw_mutex_attr;
247 lck_mtx_t *ipfw_mutex;
248
249 extern void ipfwsyslog( int level, char *format,...);
250
251 #if DUMMYNET
252 ip_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:"
259 static 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
267 void 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
311 static __inline int
312 icmptype_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
322 static int
323 is_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
342 static int
343 flags_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
356 static int
357 ipopts_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
400 static int
401 tcpopts_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
452 static int
453 iface_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 */
499 static int
500 verify_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
527 static 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 */
536 static void
537 ipfw_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 */
733 static __inline int
734 hash_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 */
779 static void
780 remove_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 */
804 next_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 }
836 next:
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 */
849 static ipfw_dyn_rule *
850 lookup_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 }
908 next:
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 }
978 done:
979 if (match_direction)
980 *match_direction = dir;
981 return q;
982 }
983
984 static void
985 realloc_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 */
1021 static ipfw_dyn_rule *
1022 add_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 */
1079 static ipfw_dyn_rule *
1080 lookup_dyn_parent(struct ipfw_flow_id *pkt, struct ip_fw *rule)
1081 {
1082 ipfw_dyn_rule *q;
1083 int i;
1084 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 */
1112 static int
1113 install_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 */
1213 static void
1214 send_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 */
1296 static void
1297 send_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 }
1307 icmp_error(args->m, ICMP_UNREACH, code, 0L, 0);
1308 } else if (offset == 0 && args->f_id.proto == IPPROTO_TCP) {
1309 struct tcphdr *const tcp =
1310 L3HDR(struct tcphdr, mtod(args->m, struct ip *));
1311 if ( (tcp->th_flags & TH_RST) == 0) {
1312 send_pkt(&(args->f_id), ntohl(tcp->th_seq),
1313 ntohl(tcp->th_ack),
1314 tcp->th_flags | TH_RST);
1315 }
1316 m_freem(args->m);
1317 } else
1318 m_freem(args->m);
1319 args->m = NULL;
1320 }
1321
1322 /**
1323 *
1324 * Given an ip_fw *, lookup_next_rule will return a pointer
1325 * to the next rule, which can be either the jump
1326 * target (for skipto instructions) or the next one in the list (in
1327 * all other cases including a missing jump target).
1328 * The result is also written in the "next_rule" field of the rule.
1329 * Backward jumps are not allowed, so start looking from the next
1330 * rule...
1331 *
1332 * This never returns NULL -- in case we do not have an exact match,
1333 * the next rule is returned. When the ruleset is changed,
1334 * pointers are flushed so we are always correct.
1335 */
1336
1337 static struct ip_fw *
1338 lookup_next_rule(struct ip_fw *me)
1339 {
1340 struct ip_fw *rule = NULL;
1341 ipfw_insn *cmd;
1342
1343 /* look for action, in case it is a skipto */
1344 cmd = ACTION_PTR(me);
1345 if (cmd->opcode == O_LOG)
1346 cmd += F_LEN(cmd);
1347 if ( cmd->opcode == O_SKIPTO )
1348 for (rule = me->next; rule ; rule = rule->next)
1349 if (rule->rulenum >= cmd->arg1)
1350 break;
1351 if (rule == NULL) /* failure or not a skipto */
1352 rule = me->next;
1353 me->next_rule = rule;
1354 return rule;
1355 }
1356
1357 /*
1358 * The main check routine for the firewall.
1359 *
1360 * All arguments are in args so we can modify them and return them
1361 * back to the caller.
1362 *
1363 * Parameters:
1364 *
1365 * args->m (in/out) The packet; we set to NULL when/if we nuke it.
1366 * Starts with the IP header.
1367 * args->eh (in) Mac header if present, or NULL for layer3 packet.
1368 * args->oif Outgoing interface, or NULL if packet is incoming.
1369 * The incoming interface is in the mbuf. (in)
1370 * args->divert_rule (in/out)
1371 * Skip up to the first rule past this rule number;
1372 * upon return, non-zero port number for divert or tee.
1373 *
1374 * args->rule Pointer to the last matching rule (in/out)
1375 * args->next_hop Socket we are forwarding to (out).
1376 * args->f_id Addresses grabbed from the packet (out)
1377 *
1378 * Return value:
1379 *
1380 * IP_FW_PORT_DENY_FLAG the packet must be dropped.
1381 * 0 The packet is to be accepted and routed normally OR
1382 * the packet was denied/rejected and has been dropped;
1383 * in the latter case, *m is equal to NULL upon return.
1384 * port Divert the packet to port, with these caveats:
1385 *
1386 * - If IP_FW_PORT_TEE_FLAG is set, tee the packet instead
1387 * of diverting it (ie, 'ipfw tee').
1388 *
1389 * - If IP_FW_PORT_DYNT_FLAG is set, interpret the lower
1390 * 16 bits as a dummynet pipe number instead of diverting
1391 */
1392
1393 static int
1394 ipfw_chk(struct ip_fw_args *args)
1395 {
1396 /*
1397 * Local variables hold state during the processing of a packet.
1398 *
1399 * IMPORTANT NOTE: to speed up the processing of rules, there
1400 * are some assumption on the values of the variables, which
1401 * are documented here. Should you change them, please check
1402 * the implementation of the various instructions to make sure
1403 * that they still work.
1404 *
1405 * args->eh The MAC header. It is non-null for a layer2
1406 * packet, it is NULL for a layer-3 packet.
1407 *
1408 * m | args->m Pointer to the mbuf, as received from the caller.
1409 * It may change if ipfw_chk() does an m_pullup, or if it
1410 * consumes the packet because it calls send_reject().
1411 * XXX This has to change, so that ipfw_chk() never modifies
1412 * or consumes the buffer.
1413 * ip is simply an alias of the value of m, and it is kept
1414 * in sync with it (the packet is supposed to start with
1415 * the ip header).
1416 */
1417 struct mbuf *m = args->m;
1418 struct ip *ip = mtod(m, struct ip *);
1419
1420 /*
1421 * oif | args->oif If NULL, ipfw_chk has been called on the
1422 * inbound path (ether_input, bdg_forward, ip_input).
1423 * If non-NULL, ipfw_chk has been called on the outbound path
1424 * (ether_output, ip_output).
1425 */
1426 struct ifnet *oif = args->oif;
1427
1428 struct ip_fw *f = NULL; /* matching rule */
1429 int retval = 0;
1430
1431 /*
1432 * hlen The length of the IPv4 header.
1433 * hlen >0 means we have an IPv4 packet.
1434 */
1435 u_int hlen = 0; /* hlen >0 means we have an IP pkt */
1436
1437 /*
1438 * offset The offset of a fragment. offset != 0 means that
1439 * we have a fragment at this offset of an IPv4 packet.
1440 * offset == 0 means that (if this is an IPv4 packet)
1441 * this is the first or only fragment.
1442 */
1443 u_short offset = 0;
1444
1445 /*
1446 * Local copies of addresses. They are only valid if we have
1447 * an IP packet.
1448 *
1449 * proto The protocol. Set to 0 for non-ip packets,
1450 * or to the protocol read from the packet otherwise.
1451 * proto != 0 means that we have an IPv4 packet.
1452 *
1453 * src_port, dst_port port numbers, in HOST format. Only
1454 * valid for TCP and UDP packets.
1455 *
1456 * src_ip, dst_ip ip addresses, in NETWORK format.
1457 * Only valid for IPv4 packets.
1458 */
1459 u_int8_t proto;
1460 u_int16_t src_port = 0, dst_port = 0; /* NOTE: host format */
1461 struct in_addr src_ip, dst_ip; /* NOTE: network format */
1462 u_int16_t ip_len=0;
1463 int pktlen;
1464 int dyn_dir = MATCH_UNKNOWN;
1465 ipfw_dyn_rule *q = NULL;
1466 struct timeval timenow;
1467
1468 if (m->m_flags & M_SKIP_FIREWALL) {
1469 return 0; /* accept */
1470 }
1471
1472 lck_mtx_lock(ipfw_mutex);
1473
1474 getmicrotime(&timenow);
1475 /*
1476 * dyn_dir = MATCH_UNKNOWN when rules unchecked,
1477 * MATCH_NONE when checked and not matched (q = NULL),
1478 * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL)
1479 */
1480
1481 pktlen = m->m_pkthdr.len;
1482 if (args->eh == NULL || /* layer 3 packet */
1483 ( m->m_pkthdr.len >= sizeof(struct ip) &&
1484 ntohs(args->eh->ether_type) == ETHERTYPE_IP))
1485 hlen = ip->ip_hl << 2;
1486
1487 /*
1488 * Collect parameters into local variables for faster matching.
1489 */
1490 if (hlen == 0) { /* do not grab addresses for non-ip pkts */
1491 proto = args->f_id.proto = 0; /* mark f_id invalid */
1492 goto after_ip_checks;
1493 }
1494
1495 proto = args->f_id.proto = ip->ip_p;
1496 src_ip = ip->ip_src;
1497 dst_ip = ip->ip_dst;
1498 if (args->eh != NULL) { /* layer 2 packets are as on the wire */
1499 offset = ntohs(ip->ip_off) & IP_OFFMASK;
1500 ip_len = ntohs(ip->ip_len);
1501 } else {
1502 offset = ip->ip_off & IP_OFFMASK;
1503 ip_len = ip->ip_len;
1504 }
1505 pktlen = ip_len < pktlen ? ip_len : pktlen;
1506
1507 #define PULLUP_TO(len) \
1508 do { \
1509 if ((m)->m_len < (len)) { \
1510 args->m = m = m_pullup(m, (len)); \
1511 if (m == 0) \
1512 goto pullup_failed; \
1513 ip = mtod(m, struct ip *); \
1514 } \
1515 } while (0)
1516
1517 if (offset == 0) {
1518 switch (proto) {
1519 case IPPROTO_TCP:
1520 {
1521 struct tcphdr *tcp;
1522
1523 PULLUP_TO(hlen + sizeof(struct tcphdr));
1524 tcp = L3HDR(struct tcphdr, ip);
1525 dst_port = tcp->th_dport;
1526 src_port = tcp->th_sport;
1527 args->f_id.flags = tcp->th_flags;
1528 }
1529 break;
1530
1531 case IPPROTO_UDP:
1532 {
1533 struct udphdr *udp;
1534
1535 PULLUP_TO(hlen + sizeof(struct udphdr));
1536 udp = L3HDR(struct udphdr, ip);
1537 dst_port = udp->uh_dport;
1538 src_port = udp->uh_sport;
1539 }
1540 break;
1541
1542 case IPPROTO_ICMP:
1543 PULLUP_TO(hlen + 4); /* type, code and checksum. */
1544 args->f_id.flags = L3HDR(struct icmp, ip)->icmp_type;
1545 break;
1546
1547 default:
1548 break;
1549 }
1550 #undef PULLUP_TO
1551 }
1552
1553 args->f_id.src_ip = ntohl(src_ip.s_addr);
1554 args->f_id.dst_ip = ntohl(dst_ip.s_addr);
1555 args->f_id.src_port = src_port = ntohs(src_port);
1556 args->f_id.dst_port = dst_port = ntohs(dst_port);
1557
1558 after_ip_checks:
1559 if (args->rule) {
1560 /*
1561 * Packet has already been tagged. Look for the next rule
1562 * to restart processing.
1563 *
1564 * If fw_one_pass != 0 then just accept it.
1565 * XXX should not happen here, but optimized out in
1566 * the caller.
1567 */
1568 if (fw_one_pass) {
1569 lck_mtx_unlock(ipfw_mutex);
1570 return 0;
1571 }
1572
1573 f = args->rule->next_rule;
1574 if (f == NULL)
1575 f = lookup_next_rule(args->rule);
1576 } else {
1577 /*
1578 * Find the starting rule. It can be either the first
1579 * one, or the one after divert_rule if asked so.
1580 */
1581 int skipto = args->divert_rule;
1582
1583 f = layer3_chain;
1584 if (args->eh == NULL && skipto != 0) {
1585 if (skipto >= IPFW_DEFAULT_RULE) {
1586 lck_mtx_unlock(ipfw_mutex);
1587 return(IP_FW_PORT_DENY_FLAG); /* invalid */
1588 }
1589 while (f && f->rulenum <= skipto)
1590 f = f->next;
1591 if (f == NULL) { /* drop packet */
1592 lck_mtx_unlock(ipfw_mutex);
1593 return(IP_FW_PORT_DENY_FLAG);
1594 }
1595 }
1596 }
1597 args->divert_rule = 0; /* reset to avoid confusion later */
1598
1599 /*
1600 * Now scan the rules, and parse microinstructions for each rule.
1601 */
1602 for (; f; f = f->next) {
1603 int l, cmdlen;
1604 ipfw_insn *cmd;
1605 int skip_or; /* skip rest of OR block */
1606
1607 again:
1608 if (f->reserved_1 == IPFW_RULE_INACTIVE) {
1609 continue;
1610 }
1611
1612 if (set_disable & (1 << f->set) )
1613 continue;
1614
1615 skip_or = 0;
1616 for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
1617 l -= cmdlen, cmd += cmdlen) {
1618 int match;
1619
1620 /*
1621 * check_body is a jump target used when we find a
1622 * CHECK_STATE, and need to jump to the body of
1623 * the target rule.
1624 */
1625
1626 check_body:
1627 cmdlen = F_LEN(cmd);
1628 /*
1629 * An OR block (insn_1 || .. || insn_n) has the
1630 * F_OR bit set in all but the last instruction.
1631 * The first match will set "skip_or", and cause
1632 * the following instructions to be skipped until
1633 * past the one with the F_OR bit clear.
1634 */
1635 if (skip_or) { /* skip this instruction */
1636 if ((cmd->len & F_OR) == 0)
1637 skip_or = 0; /* next one is good */
1638 continue;
1639 }
1640 match = 0; /* set to 1 if we succeed */
1641
1642 switch (cmd->opcode) {
1643 /*
1644 * The first set of opcodes compares the packet's
1645 * fields with some pattern, setting 'match' if a
1646 * match is found. At the end of the loop there is
1647 * logic to deal with F_NOT and F_OR flags associated
1648 * with the opcode.
1649 */
1650 case O_NOP:
1651 match = 1;
1652 break;
1653
1654 case O_FORWARD_MAC:
1655 printf("ipfw: opcode %d unimplemented\n",
1656 cmd->opcode);
1657 break;
1658
1659 #ifndef __APPLE__
1660 case O_GID:
1661 #endif
1662 case O_UID:
1663 /*
1664 * We only check offset == 0 && proto != 0,
1665 * as this ensures that we have an IPv4
1666 * packet with the ports info.
1667 */
1668 if (offset!=0)
1669 break;
1670
1671 {
1672 struct inpcbinfo *pi;
1673 int wildcard;
1674 struct inpcb *pcb;
1675
1676 if (proto == IPPROTO_TCP) {
1677 wildcard = 0;
1678 pi = &tcbinfo;
1679 } else if (proto == IPPROTO_UDP) {
1680 wildcard = 1;
1681 pi = &udbinfo;
1682 } else
1683 break;
1684
1685 pcb = (oif) ?
1686 in_pcblookup_hash(pi,
1687 dst_ip, htons(dst_port),
1688 src_ip, htons(src_port),
1689 wildcard, oif) :
1690 in_pcblookup_hash(pi,
1691 src_ip, htons(src_port),
1692 dst_ip, htons(dst_port),
1693 wildcard, NULL);
1694
1695 if (pcb == NULL || pcb->inp_socket == NULL)
1696 break;
1697 #if __FreeBSD_version < 500034
1698 #define socheckuid(a,b) (kauth_cred_getuid((a)->so_cred) != (b))
1699 #endif
1700 if (cmd->opcode == O_UID) {
1701 match =
1702 #ifdef __APPLE__
1703 (pcb->inp_socket->so_uid == (uid_t)((ipfw_insn_u32 *)cmd)->d[0]);
1704 #else
1705 !socheckuid(pcb->inp_socket,
1706 (uid_t)((ipfw_insn_u32 *)cmd)->d[0]);
1707 #endif
1708 }
1709 #ifndef __APPLE__
1710 else {
1711 match = 0;
1712 kauth_cred_ismember_gid(pcb->inp_socket->so_cred,
1713 (gid_t)((ipfw_insn_u32 *)cmd)->d[0], &match);
1714 }
1715 #endif
1716 }
1717
1718 break;
1719
1720 case O_RECV:
1721 match = iface_match(m->m_pkthdr.rcvif,
1722 (ipfw_insn_if *)cmd);
1723 break;
1724
1725 case O_XMIT:
1726 match = iface_match(oif, (ipfw_insn_if *)cmd);
1727 break;
1728
1729 case O_VIA:
1730 match = iface_match(oif ? oif :
1731 m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd);
1732 break;
1733
1734 case O_MACADDR2:
1735 if (args->eh != NULL) { /* have MAC header */
1736 u_int32_t *want = (u_int32_t *)
1737 ((ipfw_insn_mac *)cmd)->addr;
1738 u_int32_t *mask = (u_int32_t *)
1739 ((ipfw_insn_mac *)cmd)->mask;
1740 u_int32_t *hdr = (u_int32_t *)args->eh;
1741
1742 match =
1743 ( want[0] == (hdr[0] & mask[0]) &&
1744 want[1] == (hdr[1] & mask[1]) &&
1745 want[2] == (hdr[2] & mask[2]) );
1746 }
1747 break;
1748
1749 case O_MAC_TYPE:
1750 if (args->eh != NULL) {
1751 u_int16_t t =
1752 ntohs(args->eh->ether_type);
1753 u_int16_t *p =
1754 ((ipfw_insn_u16 *)cmd)->ports;
1755 int i;
1756
1757 for (i = cmdlen - 1; !match && i>0;
1758 i--, p += 2)
1759 match = (t>=p[0] && t<=p[1]);
1760 }
1761 break;
1762
1763 case O_FRAG:
1764 match = (hlen > 0 && offset != 0);
1765 break;
1766
1767 case O_IN: /* "out" is "not in" */
1768 match = (oif == NULL);
1769 break;
1770
1771 case O_LAYER2:
1772 match = (args->eh != NULL);
1773 break;
1774
1775 case O_PROTO:
1776 /*
1777 * We do not allow an arg of 0 so the
1778 * check of "proto" only suffices.
1779 */
1780 match = (proto == cmd->arg1);
1781 break;
1782
1783 case O_IP_SRC:
1784 match = (hlen > 0 &&
1785 ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1786 src_ip.s_addr);
1787 break;
1788
1789 case O_IP_SRC_MASK:
1790 case O_IP_DST_MASK:
1791 if (hlen > 0) {
1792 uint32_t a =
1793 (cmd->opcode == O_IP_DST_MASK) ?
1794 dst_ip.s_addr : src_ip.s_addr;
1795 uint32_t *p = ((ipfw_insn_u32 *)cmd)->d;
1796 int i = cmdlen-1;
1797
1798 for (; !match && i>0; i-= 2, p+= 2)
1799 match = (p[0] == (a & p[1]));
1800 }
1801 break;
1802
1803 case O_IP_SRC_ME:
1804 if (hlen > 0) {
1805 struct ifnet *tif;
1806
1807 INADDR_TO_IFP(src_ip, tif);
1808 match = (tif != NULL);
1809 }
1810 break;
1811
1812 case O_IP_DST_SET:
1813 case O_IP_SRC_SET:
1814 if (hlen > 0) {
1815 u_int32_t *d = (u_int32_t *)(cmd+1);
1816 u_int32_t addr =
1817 cmd->opcode == O_IP_DST_SET ?
1818 args->f_id.dst_ip :
1819 args->f_id.src_ip;
1820
1821 if (addr < d[0])
1822 break;
1823 addr -= d[0]; /* subtract base */
1824 match = (addr < cmd->arg1) &&
1825 ( d[ 1 + (addr>>5)] &
1826 (1<<(addr & 0x1f)) );
1827 }
1828 break;
1829
1830 case O_IP_DST:
1831 match = (hlen > 0 &&
1832 ((ipfw_insn_ip *)cmd)->addr.s_addr ==
1833 dst_ip.s_addr);
1834 break;
1835
1836 case O_IP_DST_ME:
1837 if (hlen > 0) {
1838 struct ifnet *tif;
1839
1840 INADDR_TO_IFP(dst_ip, tif);
1841 match = (tif != NULL);
1842 }
1843 break;
1844
1845 case O_IP_SRCPORT:
1846 case O_IP_DSTPORT:
1847 /*
1848 * offset == 0 && proto != 0 is enough
1849 * to guarantee that we have an IPv4
1850 * packet with port info.
1851 */
1852 if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP)
1853 && offset == 0) {
1854 u_int16_t x =
1855 (cmd->opcode == O_IP_SRCPORT) ?
1856 src_port : dst_port ;
1857 u_int16_t *p =
1858 ((ipfw_insn_u16 *)cmd)->ports;
1859 int i;
1860
1861 for (i = cmdlen - 1; !match && i>0;
1862 i--, p += 2)
1863 match = (x>=p[0] && x<=p[1]);
1864 }
1865 break;
1866
1867 case O_ICMPTYPE:
1868 match = (offset == 0 && proto==IPPROTO_ICMP &&
1869 icmptype_match(ip, (ipfw_insn_u32 *)cmd) );
1870 break;
1871
1872 case O_IPOPT:
1873 match = (hlen > 0 && ipopts_match(ip, cmd) );
1874 break;
1875
1876 case O_IPVER:
1877 match = (hlen > 0 && cmd->arg1 == ip->ip_v);
1878 break;
1879
1880 case O_IPID:
1881 case O_IPLEN:
1882 case O_IPTTL:
1883 if (hlen > 0) { /* only for IP packets */
1884 uint16_t x;
1885 uint16_t *p;
1886 int i;
1887
1888 if (cmd->opcode == O_IPLEN)
1889 x = ip_len;
1890 else if (cmd->opcode == O_IPTTL)
1891 x = ip->ip_ttl;
1892 else /* must be IPID */
1893 x = ntohs(ip->ip_id);
1894 if (cmdlen == 1) {
1895 match = (cmd->arg1 == x);
1896 break;
1897 }
1898 /* otherwise we have ranges */
1899 p = ((ipfw_insn_u16 *)cmd)->ports;
1900 i = cmdlen - 1;
1901 for (; !match && i>0; i--, p += 2)
1902 match = (x >= p[0] && x <= p[1]);
1903 }
1904 break;
1905
1906 case O_IPPRECEDENCE:
1907 match = (hlen > 0 &&
1908 (cmd->arg1 == (ip->ip_tos & 0xe0)) );
1909 break;
1910
1911 case O_IPTOS:
1912 match = (hlen > 0 &&
1913 flags_match(cmd, ip->ip_tos));
1914 break;
1915
1916 case O_TCPFLAGS:
1917 match = (proto == IPPROTO_TCP && offset == 0 &&
1918 flags_match(cmd,
1919 L3HDR(struct tcphdr,ip)->th_flags));
1920 break;
1921
1922 case O_TCPOPTS:
1923 match = (proto == IPPROTO_TCP && offset == 0 &&
1924 tcpopts_match(ip, cmd));
1925 break;
1926
1927 case O_TCPSEQ:
1928 match = (proto == IPPROTO_TCP && offset == 0 &&
1929 ((ipfw_insn_u32 *)cmd)->d[0] ==
1930 L3HDR(struct tcphdr,ip)->th_seq);
1931 break;
1932
1933 case O_TCPACK:
1934 match = (proto == IPPROTO_TCP && offset == 0 &&
1935 ((ipfw_insn_u32 *)cmd)->d[0] ==
1936 L3HDR(struct tcphdr,ip)->th_ack);
1937 break;
1938
1939 case O_TCPWIN:
1940 match = (proto == IPPROTO_TCP && offset == 0 &&
1941 cmd->arg1 ==
1942 L3HDR(struct tcphdr,ip)->th_win);
1943 break;
1944
1945 case O_ESTAB:
1946 /* reject packets which have SYN only */
1947 /* XXX should i also check for TH_ACK ? */
1948 match = (proto == IPPROTO_TCP && offset == 0 &&
1949 (L3HDR(struct tcphdr,ip)->th_flags &
1950 (TH_RST | TH_ACK | TH_SYN)) != TH_SYN);
1951 break;
1952
1953 case O_LOG:
1954 if (fw_verbose)
1955 ipfw_log(f, hlen, args->eh, m, oif);
1956 match = 1;
1957 break;
1958
1959 case O_PROB:
1960 match = (random()<((ipfw_insn_u32 *)cmd)->d[0]);
1961 break;
1962
1963 case O_VERREVPATH:
1964 /* Outgoing packets automatically pass/match */
1965 match = ((oif != NULL) ||
1966 (m->m_pkthdr.rcvif == NULL) ||
1967 verify_rev_path(src_ip, m->m_pkthdr.rcvif));
1968 break;
1969
1970 case O_IPSEC:
1971 #ifdef FAST_IPSEC
1972 match = (m_tag_find(m,
1973 PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL);
1974 #endif
1975 #ifdef IPSEC
1976 match = (ipsec_gethist(m, NULL) != NULL);
1977 #endif
1978 /* otherwise no match */
1979 break;
1980
1981 /*
1982 * The second set of opcodes represents 'actions',
1983 * i.e. the terminal part of a rule once the packet
1984 * matches all previous patterns.
1985 * Typically there is only one action for each rule,
1986 * and the opcode is stored at the end of the rule
1987 * (but there are exceptions -- see below).
1988 *
1989 * In general, here we set retval and terminate the
1990 * outer loop (would be a 'break 3' in some language,
1991 * but we need to do a 'goto done').
1992 *
1993 * Exceptions:
1994 * O_COUNT and O_SKIPTO actions:
1995 * instead of terminating, we jump to the next rule
1996 * ('goto next_rule', equivalent to a 'break 2'),
1997 * or to the SKIPTO target ('goto again' after
1998 * having set f, cmd and l), respectively.
1999 *
2000 * O_LIMIT and O_KEEP_STATE: these opcodes are
2001 * not real 'actions', and are stored right
2002 * before the 'action' part of the rule.
2003 * These opcodes try to install an entry in the
2004 * state tables; if successful, we continue with
2005 * the next opcode (match=1; break;), otherwise
2006 * the packet * must be dropped
2007 * ('goto done' after setting retval);
2008 *
2009 * O_PROBE_STATE and O_CHECK_STATE: these opcodes
2010 * cause a lookup of the state table, and a jump
2011 * to the 'action' part of the parent rule
2012 * ('goto check_body') if an entry is found, or
2013 * (CHECK_STATE only) a jump to the next rule if
2014 * the entry is not found ('goto next_rule').
2015 * The result of the lookup is cached to make
2016 * further instances of these opcodes are
2017 * effectively NOPs.
2018 */
2019 case O_LIMIT:
2020 case O_KEEP_STATE:
2021 if (install_state(f,
2022 (ipfw_insn_limit *)cmd, args)) {
2023 retval = IP_FW_PORT_DENY_FLAG;
2024 goto done; /* error/limit violation */
2025 }
2026 match = 1;
2027 break;
2028
2029 case O_PROBE_STATE:
2030 case O_CHECK_STATE:
2031 /*
2032 * dynamic rules are checked at the first
2033 * keep-state or check-state occurrence,
2034 * with the result being stored in dyn_dir.
2035 * The compiler introduces a PROBE_STATE
2036 * instruction for us when we have a
2037 * KEEP_STATE (because PROBE_STATE needs
2038 * to be run first).
2039 */
2040 if (dyn_dir == MATCH_UNKNOWN &&
2041 (q = lookup_dyn_rule(&args->f_id,
2042 &dyn_dir, proto == IPPROTO_TCP ?
2043 L3HDR(struct tcphdr, ip) : NULL))
2044 != NULL) {
2045 /*
2046 * Found dynamic entry, update stats
2047 * and jump to the 'action' part of
2048 * the parent rule.
2049 */
2050 q->pcnt++;
2051 q->bcnt += pktlen;
2052 f = q->rule;
2053 cmd = ACTION_PTR(f);
2054 l = f->cmd_len - f->act_ofs;
2055 goto check_body;
2056 }
2057 /*
2058 * Dynamic entry not found. If CHECK_STATE,
2059 * skip to next rule, if PROBE_STATE just
2060 * ignore and continue with next opcode.
2061 */
2062 if (cmd->opcode == O_CHECK_STATE)
2063 goto next_rule;
2064 match = 1;
2065 break;
2066
2067 case O_ACCEPT:
2068 retval = 0; /* accept */
2069 goto done;
2070
2071 case O_PIPE:
2072 case O_QUEUE:
2073 args->rule = f; /* report matching rule */
2074 retval = cmd->arg1 | IP_FW_PORT_DYNT_FLAG;
2075 goto done;
2076
2077 case O_DIVERT:
2078 case O_TEE:
2079 if (args->eh) /* not on layer 2 */
2080 break;
2081 args->divert_rule = f->rulenum;
2082 retval = (cmd->opcode == O_DIVERT) ?
2083 cmd->arg1 :
2084 cmd->arg1 | IP_FW_PORT_TEE_FLAG;
2085 goto done;
2086
2087 case O_COUNT:
2088 case O_SKIPTO:
2089 f->pcnt++; /* update stats */
2090 f->bcnt += pktlen;
2091 f->timestamp = timenow.tv_sec;
2092 if (cmd->opcode == O_COUNT)
2093 goto next_rule;
2094 /* handle skipto */
2095 if (f->next_rule == NULL)
2096 lookup_next_rule(f);
2097 f = f->next_rule;
2098 goto again;
2099
2100 case O_REJECT:
2101 /*
2102 * Drop the packet and send a reject notice
2103 * if the packet is not ICMP (or is an ICMP
2104 * query), and it is not multicast/broadcast.
2105 */
2106 if (hlen > 0 &&
2107 (proto != IPPROTO_ICMP ||
2108 is_icmp_query(ip)) &&
2109 !(m->m_flags & (M_BCAST|M_MCAST)) &&
2110 !IN_MULTICAST(dst_ip.s_addr)) {
2111 send_reject(args, cmd->arg1,
2112 offset,ip_len);
2113 m = args->m;
2114 }
2115 /* FALLTHROUGH */
2116 case O_DENY:
2117 retval = IP_FW_PORT_DENY_FLAG;
2118 goto done;
2119
2120 case O_FORWARD_IP:
2121 if (args->eh) /* not valid on layer2 pkts */
2122 break;
2123 if (!q || dyn_dir == MATCH_FORWARD)
2124 args->next_hop =
2125 &((ipfw_insn_sa *)cmd)->sa;
2126 retval = 0;
2127 goto done;
2128
2129 default:
2130 panic("-- unknown opcode %d\n", cmd->opcode);
2131 } /* end of switch() on opcodes */
2132
2133 if (cmd->len & F_NOT)
2134 match = !match;
2135
2136 if (match) {
2137 if (cmd->len & F_OR)
2138 skip_or = 1;
2139 } else {
2140 if (!(cmd->len & F_OR)) /* not an OR block, */
2141 break; /* try next rule */
2142 }
2143
2144 } /* end of inner for, scan opcodes */
2145
2146 next_rule:; /* try next rule */
2147
2148 } /* end of outer for, scan rules */
2149 printf("ipfw: ouch!, skip past end of rules, denying packet\n");
2150 lck_mtx_unlock(ipfw_mutex);
2151 return(IP_FW_PORT_DENY_FLAG);
2152
2153 done:
2154 /* Update statistics */
2155 f->pcnt++;
2156 f->bcnt += pktlen;
2157 f->timestamp = timenow.tv_sec;
2158 lck_mtx_unlock(ipfw_mutex);
2159 return retval;
2160
2161 pullup_failed:
2162 if (fw_verbose)
2163 printf("ipfw: pullup failed\n");
2164 lck_mtx_unlock(ipfw_mutex);
2165 return(IP_FW_PORT_DENY_FLAG);
2166 }
2167
2168 /*
2169 * When a rule is added/deleted, clear the next_rule pointers in all rules.
2170 * These will be reconstructed on the fly as packets are matched.
2171 * Must be called at splimp().
2172 */
2173 static void
2174 flush_rule_ptrs(void)
2175 {
2176 struct ip_fw *rule;
2177
2178 for (rule = layer3_chain; rule; rule = rule->next)
2179 rule->next_rule = NULL;
2180 }
2181
2182 /*
2183 * When pipes/queues are deleted, clear the "pipe_ptr" pointer to a given
2184 * pipe/queue, or to all of them (match == NULL).
2185 * Must be called at splimp().
2186 */
2187 void
2188 flush_pipe_ptrs(struct dn_flow_set *match)
2189 {
2190 struct ip_fw *rule;
2191
2192 for (rule = layer3_chain; rule; rule = rule->next) {
2193 ipfw_insn_pipe *cmd = (ipfw_insn_pipe *)ACTION_PTR(rule);
2194
2195 if (cmd->o.opcode != O_PIPE && cmd->o.opcode != O_QUEUE)
2196 continue;
2197 /*
2198 * XXX Use bcmp/bzero to handle pipe_ptr to overcome
2199 * possible alignment problems on 64-bit architectures.
2200 * This code is seldom used so we do not worry too
2201 * much about efficiency.
2202 */
2203 if (match == NULL ||
2204 !bcmp(&cmd->pipe_ptr, &match, sizeof(match)) )
2205 bzero(&cmd->pipe_ptr, sizeof(cmd->pipe_ptr));
2206 }
2207 }
2208
2209 /*
2210 * Add a new rule to the list. Copy the rule into a malloc'ed area, then
2211 * possibly create a rule number and add the rule to the list.
2212 * Update the rule_number in the input struct so the caller knows it as well.
2213 */
2214 static int
2215 add_rule(struct ip_fw **head, struct ip_fw *input_rule)
2216 {
2217 struct ip_fw *rule, *f, *prev;
2218 int s;
2219 int l = RULESIZE(input_rule);
2220
2221 if (*head == NULL && input_rule->rulenum != IPFW_DEFAULT_RULE)
2222 return (EINVAL);
2223
2224 rule = _MALLOC(l, M_IPFW, M_WAIT);
2225 if (rule == NULL) {
2226 printf("ipfw2: add_rule MALLOC failed\n");
2227 return (ENOSPC);
2228 }
2229
2230 bzero(rule, l);
2231 bcopy(input_rule, rule, l);
2232
2233 rule->next = NULL;
2234 rule->next_rule = NULL;
2235
2236 rule->pcnt = 0;
2237 rule->bcnt = 0;
2238 rule->timestamp = 0;
2239
2240 if (*head == NULL) { /* default rule */
2241 *head = rule;
2242 goto done;
2243 }
2244
2245 /*
2246 * If rulenum is 0, find highest numbered rule before the
2247 * default rule, and add autoinc_step
2248 */
2249 if (autoinc_step < 1)
2250 autoinc_step = 1;
2251 else if (autoinc_step > 1000)
2252 autoinc_step = 1000;
2253 if (rule->rulenum == 0) {
2254 /*
2255 * locate the highest numbered rule before default
2256 */
2257 for (f = *head; f; f = f->next) {
2258 if (f->rulenum == IPFW_DEFAULT_RULE)
2259 break;
2260 rule->rulenum = f->rulenum;
2261 }
2262 if (rule->rulenum < IPFW_DEFAULT_RULE - autoinc_step)
2263 rule->rulenum += autoinc_step;
2264 input_rule->rulenum = rule->rulenum;
2265 }
2266
2267 /*
2268 * Now insert the new rule in the right place in the sorted list.
2269 */
2270 for (prev = NULL, f = *head; f; prev = f, f = f->next) {
2271 if (f->rulenum > rule->rulenum) { /* found the location */
2272 if (prev) {
2273 rule->next = f;
2274 prev->next = rule;
2275 } else { /* head insert */
2276 rule->next = *head;
2277 *head = rule;
2278 }
2279 break;
2280 }
2281 }
2282 flush_rule_ptrs();
2283 done:
2284 static_count++;
2285 static_len += l;
2286 DEB(printf("ipfw: installed rule %d, static count now %d\n",
2287 rule->rulenum, static_count);)
2288 return (0);
2289 }
2290
2291 /**
2292 * Free storage associated with a static rule (including derived
2293 * dynamic rules).
2294 * The caller is in charge of clearing rule pointers to avoid
2295 * dangling pointers.
2296 * @return a pointer to the next entry.
2297 * Arguments are not checked, so they better be correct.
2298 * Must be called at splimp().
2299 */
2300 static struct ip_fw *
2301 delete_rule(struct ip_fw **head, struct ip_fw *prev, struct ip_fw *rule)
2302 {
2303 struct ip_fw *n;
2304 int l = RULESIZE(rule);
2305
2306 n = rule->next;
2307 remove_dyn_rule(rule, NULL /* force removal */);
2308 if (prev == NULL)
2309 *head = n;
2310 else
2311 prev->next = n;
2312 static_count--;
2313 static_len -= l;
2314
2315 #if DUMMYNET
2316 if (DUMMYNET_LOADED)
2317 ip_dn_ruledel_ptr(rule);
2318 #endif /* DUMMYNET */
2319 _FREE(rule, M_IPFW);
2320 return n;
2321 }
2322
2323 #if DEBUG_INACTIVE_RULES
2324 static void
2325 print_chain(struct ip_fw **chain)
2326 {
2327 struct ip_fw *rule = *chain;
2328
2329 for (; rule; rule = rule->next) {
2330 ipfw_insn *cmd = ACTION_PTR(rule);
2331
2332 printf("ipfw: rule->rulenum = %d\n", rule->rulenum);
2333
2334 if (rule->reserved_1 == IPFW_RULE_INACTIVE) {
2335 printf("ipfw: rule->reserved = IPFW_RULE_INACTIVE\n");
2336 }
2337
2338 switch (cmd->opcode) {
2339 case O_DENY:
2340 printf("ipfw: ACTION: Deny\n");
2341 break;
2342
2343 case O_REJECT:
2344 if (cmd->arg1==ICMP_REJECT_RST)
2345 printf("ipfw: ACTION: Reset\n");
2346 else if (cmd->arg1==ICMP_UNREACH_HOST)
2347 printf("ipfw: ACTION: Reject\n");
2348 break;
2349
2350 case O_ACCEPT:
2351 printf("ipfw: ACTION: Accept\n");
2352 break;
2353 case O_COUNT:
2354 printf("ipfw: ACTION: Count\n");
2355 break;
2356 case O_DIVERT:
2357 printf("ipfw: ACTION: Divert\n");
2358 break;
2359 case O_TEE:
2360 printf("ipfw: ACTION: Tee\n");
2361 break;
2362 case O_SKIPTO:
2363 printf("ipfw: ACTION: SkipTo\n");
2364 break;
2365 case O_PIPE:
2366 printf("ipfw: ACTION: Pipe\n");
2367 break;
2368 case O_QUEUE:
2369 printf("ipfw: ACTION: Queue\n");
2370 break;
2371 case O_FORWARD_IP:
2372 printf("ipfw: ACTION: Forward\n");
2373 break;
2374 default:
2375 printf("ipfw: invalid action! %d\n", cmd->opcode);
2376 }
2377 }
2378 }
2379 #endif /* DEBUG_INACTIVE_RULES */
2380
2381 static void
2382 flush_inactive(void *param)
2383 {
2384 struct ip_fw *inactive_rule = (struct ip_fw *)param;
2385 struct ip_fw *rule, *prev;
2386
2387 lck_mtx_lock(ipfw_mutex);
2388
2389 for (rule = layer3_chain, prev = NULL; rule; ) {
2390 if (rule == inactive_rule && rule->reserved_1 == IPFW_RULE_INACTIVE) {
2391 struct ip_fw *n = rule;
2392
2393 if (prev == NULL) {
2394 layer3_chain = rule->next;
2395 }
2396 else {
2397 prev->next = rule->next;
2398 }
2399 rule = rule->next;
2400 _FREE(n, M_IPFW);
2401 }
2402 else {
2403 prev = rule;
2404 rule = rule->next;
2405 }
2406 }
2407
2408 #if DEBUG_INACTIVE_RULES
2409 print_chain(&layer3_chain);
2410 #endif
2411 lck_mtx_unlock(ipfw_mutex);
2412 }
2413
2414 static void
2415 mark_inactive(struct ip_fw **prev, struct ip_fw **rule)
2416 {
2417 int l = RULESIZE(*rule);
2418
2419 if ((*rule)->reserved_1 != IPFW_RULE_INACTIVE) {
2420 (*rule)->reserved_1 = IPFW_RULE_INACTIVE;
2421 static_count--;
2422 static_len -= l;
2423
2424 timeout(flush_inactive, *rule, 30*hz); /* 30 sec. */
2425 }
2426
2427 *prev = *rule;
2428 *rule = (*rule)->next;
2429 }
2430
2431 /*
2432 * Deletes all rules from a chain (except rules in set RESVD_SET
2433 * unless kill_default = 1).
2434 * Must be called at splimp().
2435 */
2436 static void
2437 free_chain(struct ip_fw **chain, int kill_default)
2438 {
2439 struct ip_fw *prev, *rule;
2440
2441 flush_rule_ptrs(); /* more efficient to do outside the loop */
2442 for (prev = NULL, rule = *chain; rule ; )
2443 if (kill_default || rule->set != RESVD_SET) {
2444 ipfw_insn *cmd = ACTION_PTR(rule);
2445
2446 /* skip over forwarding rules so struct isn't
2447 * deleted while pointer is still in use elsewhere
2448 */
2449 if (cmd->opcode == O_FORWARD_IP) {
2450 mark_inactive(&prev, &rule);
2451 }
2452 else {
2453 rule = delete_rule(chain, prev, rule);
2454 }
2455 }
2456 else {
2457 prev = rule;
2458 rule = rule->next;
2459 }
2460 }
2461
2462 /**
2463 * Remove all rules with given number, and also do set manipulation.
2464 * Assumes chain != NULL && *chain != NULL.
2465 *
2466 * The argument is an u_int32_t. The low 16 bit are the rule or set number,
2467 * the next 8 bits are the new set, the top 8 bits are the command:
2468 *
2469 * 0 delete rules with given number
2470 * 1 delete rules with given set number
2471 * 2 move rules with given number to new set
2472 * 3 move rules with given set number to new set
2473 * 4 swap sets with given numbers
2474 */
2475 static int
2476 del_entry(struct ip_fw **chain, u_int32_t arg)
2477 {
2478 struct ip_fw *prev = NULL, *rule = *chain;
2479 int s;
2480 u_int16_t rulenum; /* rule or old_set */
2481 u_int8_t cmd, new_set;
2482
2483 rulenum = arg & 0xffff;
2484 cmd = (arg >> 24) & 0xff;
2485 new_set = (arg >> 16) & 0xff;
2486
2487 if (cmd > 4)
2488 return EINVAL;
2489 if (new_set > RESVD_SET)
2490 return EINVAL;
2491 if (cmd == 0 || cmd == 2) {
2492 if (rulenum >= IPFW_DEFAULT_RULE)
2493 return EINVAL;
2494 } else {
2495 if (rulenum > RESVD_SET) /* old_set */
2496 return EINVAL;
2497 }
2498
2499 switch (cmd) {
2500 case 0: /* delete rules with given number */
2501 /*
2502 * locate first rule to delete
2503 */
2504 for (; rule->rulenum < rulenum; prev = rule, rule = rule->next)
2505 ;
2506 if (rule->rulenum != rulenum)
2507 return EINVAL;
2508
2509 /*
2510 * flush pointers outside the loop, then delete all matching
2511 * rules. prev remains the same throughout the cycle.
2512 */
2513 flush_rule_ptrs();
2514 while (rule->rulenum == rulenum) {
2515 ipfw_insn *cmd = ACTION_PTR(rule);
2516
2517 /* keep forwarding rules around so struct isn't
2518 * deleted while pointer is still in use elsewhere
2519 */
2520 if (cmd->opcode == O_FORWARD_IP) {
2521 mark_inactive(&prev, &rule);
2522 }
2523 else {
2524 rule = delete_rule(chain, prev, rule);
2525 }
2526 }
2527 break;
2528
2529 case 1: /* delete all rules with given set number */
2530 flush_rule_ptrs();
2531 while (rule->rulenum < IPFW_DEFAULT_RULE) {
2532 if (rule->set == rulenum) {
2533 ipfw_insn *cmd = ACTION_PTR(rule);
2534
2535 /* keep forwarding rules around so struct isn't
2536 * deleted while pointer is still in use elsewhere
2537 */
2538 if (cmd->opcode == O_FORWARD_IP) {
2539 mark_inactive(&prev, &rule);
2540 }
2541 else {
2542 rule = delete_rule(chain, prev, rule);
2543 }
2544 }
2545 else {
2546 prev = rule;
2547 rule = rule->next;
2548 }
2549 }
2550 break;
2551
2552 case 2: /* move rules with given number to new set */
2553 for (; rule->rulenum < IPFW_DEFAULT_RULE; rule = rule->next)
2554 if (rule->rulenum == rulenum)
2555 rule->set = new_set;
2556 break;
2557
2558 case 3: /* move rules with given set number to new set */
2559 for (; rule->rulenum < IPFW_DEFAULT_RULE; rule = rule->next)
2560 if (rule->set == rulenum)
2561 rule->set = new_set;
2562 break;
2563
2564 case 4: /* swap two sets */
2565 for (; rule->rulenum < IPFW_DEFAULT_RULE; rule = rule->next)
2566 if (rule->set == rulenum)
2567 rule->set = new_set;
2568 else if (rule->set == new_set)
2569 rule->set = rulenum;
2570 break;
2571 }
2572 return 0;
2573 }
2574
2575 /*
2576 * Clear counters for a specific rule.
2577 */
2578 static void
2579 clear_counters(struct ip_fw *rule, int log_only)
2580 {
2581 ipfw_insn_log *l = (ipfw_insn_log *)ACTION_PTR(rule);
2582
2583 if (log_only == 0) {
2584 rule->bcnt = rule->pcnt = 0;
2585 rule->timestamp = 0;
2586 }
2587 if (l->o.opcode == O_LOG)
2588 l->log_left = l->max_log;
2589 }
2590
2591 /**
2592 * Reset some or all counters on firewall rules.
2593 * @arg frwl is null to clear all entries, or contains a specific
2594 * rule number.
2595 * @arg log_only is 1 if we only want to reset logs, zero otherwise.
2596 */
2597 static int
2598 zero_entry(int rulenum, int log_only)
2599 {
2600 struct ip_fw *rule;
2601 int s;
2602 char *msg;
2603
2604 if (rulenum == 0) {
2605 norule_counter = 0;
2606 for (rule = layer3_chain; rule; rule = rule->next)
2607 clear_counters(rule, log_only);
2608 msg = log_only ? "ipfw: All logging counts reset.\n" :
2609 "ipfw: Accounting cleared.\n";
2610 } else {
2611 int cleared = 0;
2612 /*
2613 * We can have multiple rules with the same number, so we
2614 * need to clear them all.
2615 */
2616 for (rule = layer3_chain; rule; rule = rule->next)
2617 if (rule->rulenum == rulenum) {
2618 while (rule && rule->rulenum == rulenum) {
2619 clear_counters(rule, log_only);
2620 rule = rule->next;
2621 }
2622 cleared = 1;
2623 break;
2624 }
2625 if (!cleared) /* we did not find any matching rules */
2626 return (EINVAL);
2627 msg = log_only ? "ipfw: Entry %d logging count reset.\n" :
2628 "ipfw: Entry %d cleared.\n";
2629 }
2630 if (fw_verbose)
2631 {
2632 dolog((LOG_AUTHPRIV | LOG_NOTICE, msg, rulenum));
2633 }
2634 return (0);
2635 }
2636
2637 /*
2638 * Check validity of the structure before insert.
2639 * Fortunately rules are simple, so this mostly need to check rule sizes.
2640 */
2641 static int
2642 check_ipfw_struct(struct ip_fw *rule, int size)
2643 {
2644 int l, cmdlen = 0;
2645 int have_action=0;
2646 ipfw_insn *cmd;
2647
2648 if (size < sizeof(*rule)) {
2649 printf("ipfw: rule too short\n");
2650 return (EINVAL);
2651 }
2652 /* first, check for valid size */
2653 l = RULESIZE(rule);
2654 if (l != size) {
2655 printf("ipfw: size mismatch (have %d want %d)\n", size, l);
2656 return (EINVAL);
2657 }
2658 /*
2659 * Now go for the individual checks. Very simple ones, basically only
2660 * instruction sizes.
2661 */
2662 for (l = rule->cmd_len, cmd = rule->cmd ;
2663 l > 0 ; l -= cmdlen, cmd += cmdlen) {
2664 cmdlen = F_LEN(cmd);
2665 if (cmdlen > l) {
2666 printf("ipfw: opcode %d size truncated\n",
2667 cmd->opcode);
2668 return EINVAL;
2669 }
2670 DEB(printf("ipfw: opcode %d\n", cmd->opcode);)
2671 switch (cmd->opcode) {
2672 case O_PROBE_STATE:
2673 case O_KEEP_STATE:
2674 case O_PROTO:
2675 case O_IP_SRC_ME:
2676 case O_IP_DST_ME:
2677 case O_LAYER2:
2678 case O_IN:
2679 case O_FRAG:
2680 case O_IPOPT:
2681 case O_IPTOS:
2682 case O_IPPRECEDENCE:
2683 case O_IPVER:
2684 case O_TCPWIN:
2685 case O_TCPFLAGS:
2686 case O_TCPOPTS:
2687 case O_ESTAB:
2688 case O_VERREVPATH:
2689 case O_IPSEC:
2690 if (cmdlen != F_INSN_SIZE(ipfw_insn))
2691 goto bad_size;
2692 break;
2693 case O_UID:
2694 #ifndef __APPLE__
2695 case O_GID:
2696 #endif /* __APPLE__ */
2697 case O_IP_SRC:
2698 case O_IP_DST:
2699 case O_TCPSEQ:
2700 case O_TCPACK:
2701 case O_PROB:
2702 case O_ICMPTYPE:
2703 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32))
2704 goto bad_size;
2705 break;
2706
2707 case O_LIMIT:
2708 if (cmdlen != F_INSN_SIZE(ipfw_insn_limit))
2709 goto bad_size;
2710 break;
2711
2712 case O_LOG:
2713 if (cmdlen != F_INSN_SIZE(ipfw_insn_log))
2714 goto bad_size;
2715
2716 /* enforce logging limit */
2717 if (fw_verbose &&
2718 ((ipfw_insn_log *)cmd)->max_log == 0 && verbose_limit != 0) {
2719 ((ipfw_insn_log *)cmd)->max_log = verbose_limit;
2720 }
2721
2722 ((ipfw_insn_log *)cmd)->log_left =
2723 ((ipfw_insn_log *)cmd)->max_log;
2724
2725 break;
2726
2727 case O_IP_SRC_MASK:
2728 case O_IP_DST_MASK:
2729 /* only odd command lengths */
2730 if ( !(cmdlen & 1) || cmdlen > 31)
2731 goto bad_size;
2732 break;
2733
2734 case O_IP_SRC_SET:
2735 case O_IP_DST_SET:
2736 if (cmd->arg1 == 0 || cmd->arg1 > 256) {
2737 printf("ipfw: invalid set size %d\n",
2738 cmd->arg1);
2739 return EINVAL;
2740 }
2741 if (cmdlen != F_INSN_SIZE(ipfw_insn_u32) +
2742 (cmd->arg1+31)/32 )
2743 goto bad_size;
2744 break;
2745
2746 case O_MACADDR2:
2747 if (cmdlen != F_INSN_SIZE(ipfw_insn_mac))
2748 goto bad_size;
2749 break;
2750
2751 case O_NOP:
2752 case O_IPID:
2753 case O_IPTTL:
2754 case O_IPLEN:
2755 if (cmdlen < 1 || cmdlen > 31)
2756 goto bad_size;
2757 break;
2758
2759 case O_MAC_TYPE:
2760 case O_IP_SRCPORT:
2761 case O_IP_DSTPORT: /* XXX artificial limit, 30 port pairs */
2762 if (cmdlen < 2 || cmdlen > 31)
2763 goto bad_size;
2764 break;
2765
2766 case O_RECV:
2767 case O_XMIT:
2768 case O_VIA:
2769 if (cmdlen != F_INSN_SIZE(ipfw_insn_if))
2770 goto bad_size;
2771 break;
2772
2773 case O_PIPE:
2774 case O_QUEUE:
2775 if (cmdlen != F_INSN_SIZE(ipfw_insn_pipe))
2776 goto bad_size;
2777 goto check_action;
2778
2779 case O_FORWARD_IP:
2780 if (cmdlen != F_INSN_SIZE(ipfw_insn_sa))
2781 goto bad_size;
2782 goto check_action;
2783
2784 case O_FORWARD_MAC: /* XXX not implemented yet */
2785 case O_CHECK_STATE:
2786 case O_COUNT:
2787 case O_ACCEPT:
2788 case O_DENY:
2789 case O_REJECT:
2790 case O_SKIPTO:
2791 case O_DIVERT:
2792 case O_TEE:
2793 if (cmdlen != F_INSN_SIZE(ipfw_insn))
2794 goto bad_size;
2795 check_action:
2796 if (have_action) {
2797 printf("ipfw: opcode %d, multiple actions"
2798 " not allowed\n",
2799 cmd->opcode);
2800 return EINVAL;
2801 }
2802 have_action = 1;
2803 if (l != cmdlen) {
2804 printf("ipfw: opcode %d, action must be"
2805 " last opcode\n",
2806 cmd->opcode);
2807 return EINVAL;
2808 }
2809 break;
2810 default:
2811 printf("ipfw: opcode %d, unknown opcode\n",
2812 cmd->opcode);
2813 return EINVAL;
2814 }
2815 }
2816 if (have_action == 0) {
2817 printf("ipfw: missing action\n");
2818 return EINVAL;
2819 }
2820 return 0;
2821
2822 bad_size:
2823 printf("ipfw: opcode %d size %d wrong\n",
2824 cmd->opcode, cmdlen);
2825 return EINVAL;
2826 }
2827
2828
2829 /**
2830 * {set|get}sockopt parser.
2831 */
2832 static int
2833 ipfw_ctl(struct sockopt *sopt)
2834 {
2835 #define RULE_MAXSIZE (256*sizeof(u_int32_t))
2836 u_int32_t api_version;
2837 int command;
2838 int error, s;
2839 size_t size;
2840 struct ip_fw *bp , *buf, *rule;
2841
2842 /* copy of orig sopt to send to ipfw_get_command_and_version() */
2843 struct sockopt tmp_sopt = *sopt;
2844 struct timeval timenow;
2845
2846 getmicrotime(&timenow);
2847
2848 /*
2849 * Disallow modifications in really-really secure mode, but still allow
2850 * the logging counters to be reset.
2851 */
2852 if (sopt->sopt_name == IP_FW_ADD ||
2853 (sopt->sopt_dir == SOPT_SET && sopt->sopt_name != IP_FW_RESETLOG)) {
2854 #if __FreeBSD_version >= 500034
2855 error = securelevel_ge(sopt->sopt_td->td_ucred, 3);
2856 if (error)
2857 return (error);
2858 #else /* FreeBSD 4.x */
2859 if (securelevel >= 3)
2860 return (EPERM);
2861 #endif
2862 }
2863
2864 /* first get the command and version, then do conversion as necessary */
2865 error = ipfw_get_command_and_version(&tmp_sopt, &command, &api_version);
2866
2867 if (error) {
2868 /* error getting the version */
2869 return error;
2870 }
2871
2872 switch (command) {
2873 case IP_FW_GET:
2874 /*
2875 * pass up a copy of the current rules. Static rules
2876 * come first (the last of which has number IPFW_DEFAULT_RULE),
2877 * followed by a possibly empty list of dynamic rule.
2878 * The last dynamic rule has NULL in the "next" field.
2879 */
2880 lck_mtx_lock(ipfw_mutex);
2881 size = static_len; /* size of static rules */
2882 if (ipfw_dyn_v) /* add size of dyn.rules */
2883 size += (dyn_count * sizeof(ipfw_dyn_rule));
2884
2885 /*
2886 * XXX todo: if the user passes a short length just to know
2887 * how much room is needed, do not bother filling up the
2888 * buffer, just jump to the sooptcopyout.
2889 */
2890 buf = _MALLOC(size, M_TEMP, M_WAITOK);
2891 if (buf == 0) {
2892 lck_mtx_unlock(ipfw_mutex);
2893 error = ENOBUFS;
2894 break;
2895 }
2896
2897 bzero(buf, size);
2898
2899 bp = buf;
2900 for (rule = layer3_chain; rule ; rule = rule->next) {
2901 int i = RULESIZE(rule);
2902
2903 if (rule->reserved_1 == IPFW_RULE_INACTIVE) {
2904 continue;
2905 }
2906 bcopy(rule, bp, i);
2907 bcopy(&set_disable, &(bp->next_rule),
2908 sizeof(set_disable));
2909 bp = (struct ip_fw *)((char *)bp + i);
2910 }
2911 if (ipfw_dyn_v) {
2912 int i;
2913 ipfw_dyn_rule *p, *dst, *last = NULL;
2914
2915 dst = (ipfw_dyn_rule *)bp;
2916 for (i = 0 ; i < curr_dyn_buckets ; i++ )
2917 for ( p = ipfw_dyn_v[i] ; p != NULL ;
2918 p = p->next, dst++ ) {
2919 bcopy(p, dst, sizeof *p);
2920 bcopy(&(p->rule->rulenum), &(dst->rule),
2921 sizeof(p->rule->rulenum));
2922 /*
2923 * store a non-null value in "next".
2924 * The userland code will interpret a
2925 * NULL here as a marker
2926 * for the last dynamic rule.
2927 */
2928 bcopy(&dst, &dst->next, sizeof(dst));
2929 last = dst ;
2930 dst->expire =
2931 TIME_LEQ(dst->expire, timenow.tv_sec) ?
2932 0 : dst->expire - timenow.tv_sec ;
2933 }
2934 if (last != NULL) /* mark last dynamic rule */
2935 bzero(&last->next, sizeof(last));
2936 }
2937 lck_mtx_unlock(ipfw_mutex);
2938
2939 /* convert back if necessary and copyout */
2940 if (api_version == IP_FW_VERSION_0) {
2941 int i, len = 0;
2942 struct ip_old_fw *buf2, *rule_vers0;
2943
2944 buf2 = _MALLOC(static_count * sizeof(struct ip_old_fw), M_TEMP, M_WAITOK);
2945 if (buf2 == 0) {
2946 error = ENOBUFS;
2947 }
2948
2949 if (!error) {
2950 bp = buf;
2951 rule_vers0 = buf2;
2952
2953 for (i = 0; i < static_count; i++) {
2954 /* static rules have different sizes */
2955 int j = RULESIZE(bp);
2956 ipfw_convert_from_latest(bp, rule_vers0, api_version);
2957 bp = (struct ip_fw *)((char *)bp + j);
2958 len += sizeof(*rule_vers0);
2959 rule_vers0++;
2960 }
2961 error = sooptcopyout(sopt, buf2, len);
2962 _FREE(buf2, M_TEMP);
2963 }
2964 } else if (api_version == IP_FW_VERSION_1) {
2965 int i, len = 0, buf_size;
2966 struct ip_fw_compat *buf2, *rule_vers1;
2967 struct ipfw_dyn_rule_compat *dyn_rule_vers1, *dyn_last = NULL;
2968 ipfw_dyn_rule *p;
2969
2970 buf_size = static_count * sizeof(struct ip_fw_compat) +
2971 dyn_count * sizeof(struct ipfw_dyn_rule_compat);
2972
2973 buf2 = _MALLOC(buf_size, M_TEMP, M_WAITOK);
2974 if (buf2 == 0) {
2975 error = ENOBUFS;
2976 }
2977
2978 if (!error) {
2979 bp = buf;
2980 rule_vers1 = buf2;
2981
2982 /* first do static rules */
2983 for (i = 0; i < static_count; i++) {
2984 /* static rules have different sizes */
2985 int j = RULESIZE(bp);
2986 ipfw_convert_from_latest(bp, rule_vers1, api_version);
2987 bp = (struct ip_fw *)((char *)bp + j);
2988 len += sizeof(*rule_vers1);
2989 rule_vers1++;
2990 }
2991
2992 /* now do dynamic rules */
2993 dyn_rule_vers1 = (struct ipfw_dyn_rule_compat *)rule_vers1;
2994 if (ipfw_dyn_v) {
2995 for (i = 0; i < curr_dyn_buckets; i++) {
2996 for ( p = ipfw_dyn_v[i] ; p != NULL ; p = p->next) {
2997 (int) dyn_rule_vers1->chain = p->rule->rulenum;
2998 dyn_rule_vers1->id = p->id;
2999 dyn_rule_vers1->mask = p->id;
3000 dyn_rule_vers1->type = p->dyn_type;
3001 dyn_rule_vers1->expire = p->expire;
3002 dyn_rule_vers1->pcnt = p->pcnt;
3003 dyn_rule_vers1->bcnt = p->bcnt;
3004 dyn_rule_vers1->bucket = p->bucket;
3005 dyn_rule_vers1->state = p->state;
3006
3007 dyn_rule_vers1->next = dyn_rule_vers1;
3008 dyn_last = dyn_rule_vers1;
3009
3010 len += sizeof(*dyn_rule_vers1);
3011 dyn_rule_vers1++;
3012 }
3013 }
3014
3015 if (dyn_last != NULL) {
3016 dyn_last->next = NULL;
3017 }
3018 }
3019
3020 error = sooptcopyout(sopt, buf2, len);
3021 _FREE(buf2, M_TEMP);
3022 }
3023 } else {
3024 error = sooptcopyout(sopt, buf, size);
3025 }
3026
3027 _FREE(buf, M_TEMP);
3028 break;
3029
3030 case IP_FW_FLUSH:
3031 /*
3032 * Normally we cannot release the lock on each iteration.
3033 * We could do it here only because we start from the head all
3034 * the times so there is no risk of missing some entries.
3035 * On the other hand, the risk is that we end up with
3036 * a very inconsistent ruleset, so better keep the lock
3037 * around the whole cycle.
3038 *
3039 * XXX this code can be improved by resetting the head of
3040 * the list to point to the default rule, and then freeing
3041 * the old list without the need for a lock.
3042 */
3043
3044 lck_mtx_lock(ipfw_mutex);
3045 free_chain(&layer3_chain, 0 /* keep default rule */);
3046 #if DEBUG_INACTIVE_RULES
3047 print_chain(&layer3_chain);
3048 #endif
3049 lck_mtx_unlock(ipfw_mutex);
3050 break;
3051
3052 case IP_FW_ADD:
3053 rule = _MALLOC(RULE_MAXSIZE, M_TEMP, M_WAITOK);
3054 if (rule == 0) {
3055 error = ENOBUFS;
3056 break;
3057 }
3058
3059 bzero(rule, RULE_MAXSIZE);
3060
3061 if (api_version != IP_FW_CURRENT_API_VERSION) {
3062 error = ipfw_convert_to_latest(sopt, rule, api_version);
3063 }
3064 else {
3065 error = sooptcopyin(sopt, rule, RULE_MAXSIZE,
3066 sizeof(struct ip_fw) );
3067 }
3068
3069 if (!error) {
3070 if ((api_version == IP_FW_VERSION_0) || (api_version == IP_FW_VERSION_1)) {
3071 /* the rule has already been checked so just
3072 * adjust sopt_valsize to match what would be expected.
3073 */
3074 sopt->sopt_valsize = RULESIZE(rule);
3075 }
3076 error = check_ipfw_struct(rule, sopt->sopt_valsize);
3077 if (!error) {
3078 lck_mtx_lock(ipfw_mutex);
3079 error = add_rule(&layer3_chain, rule);
3080 lck_mtx_unlock(ipfw_mutex);
3081
3082 size = RULESIZE(rule);
3083 if (!error && sopt->sopt_dir == SOPT_GET) {
3084 /* convert back if necessary and copyout */
3085 if (api_version == IP_FW_VERSION_0) {
3086 struct ip_old_fw rule_vers0;
3087
3088 ipfw_convert_from_latest(rule, &rule_vers0, api_version);
3089 sopt->sopt_valsize = sizeof(struct ip_old_fw);
3090
3091 error = sooptcopyout(sopt, &rule_vers0, sizeof(struct ip_old_fw));
3092 } else if (api_version == IP_FW_VERSION_1) {
3093 struct ip_fw_compat rule_vers1;
3094
3095 ipfw_convert_from_latest(rule, &rule_vers1, api_version);
3096 sopt->sopt_valsize = sizeof(struct ip_fw_compat);
3097
3098 error = sooptcopyout(sopt, &rule_vers1, sizeof(struct ip_fw_compat));
3099 } else {
3100 error = sooptcopyout(sopt, rule, size);
3101 }
3102 }
3103 }
3104 }
3105
3106 _FREE(rule, M_TEMP);
3107 break;
3108
3109 case IP_FW_DEL:
3110 {
3111 /*
3112 * IP_FW_DEL is used for deleting single rules or sets,
3113 * and (ab)used to atomically manipulate sets.
3114 * rule->rulenum != 0 indicates single rule delete
3115 * rule->set_masks used to manipulate sets
3116 * rule->set_masks[0] contains info on sets to be
3117 * disabled, swapped, or moved
3118 * rule->set_masks[1] contains sets to be enabled.
3119 */
3120
3121 /* there is only a simple rule passed in
3122 * (no cmds), so use a temp struct to copy
3123 */
3124 struct ip_fw temp_rule;
3125 u_int32_t arg;
3126 u_int8_t cmd;
3127
3128 bzero(&temp_rule, sizeof(struct ip_fw));
3129 if (api_version != IP_FW_CURRENT_API_VERSION) {
3130 error = ipfw_convert_to_latest(sopt, &temp_rule, api_version);
3131 }
3132 else {
3133 error = sooptcopyin(sopt, &temp_rule, sizeof(struct ip_fw),
3134 sizeof(struct ip_fw) );
3135 }
3136
3137 if (!error) {
3138 /* set_masks is used to distinguish between deleting
3139 * single rules or atomically manipulating sets
3140 */
3141 lck_mtx_lock(ipfw_mutex);
3142
3143 arg = temp_rule.set_masks[0];
3144 cmd = (arg >> 24) & 0xff;
3145
3146 if (temp_rule.rulenum) {
3147 /* single rule */
3148 error = del_entry(&layer3_chain, temp_rule.rulenum);
3149 #if DEBUG_INACTIVE_RULES
3150 print_chain(&layer3_chain);
3151 #endif
3152 }
3153 else if (cmd) {
3154 /* set reassignment - see comment above del_entry() for details */
3155 error = del_entry(&layer3_chain, temp_rule.set_masks[0]);
3156 #if DEBUG_INACTIVE_RULES
3157 print_chain(&layer3_chain);
3158 #endif
3159 }
3160 else if (temp_rule.set_masks[0] != 0 ||
3161 temp_rule.set_masks[1] != 0) {
3162 /* set enable/disable */
3163 set_disable =
3164 (set_disable | temp_rule.set_masks[0]) & ~temp_rule.set_masks[1] &
3165 ~(1<<RESVD_SET); /* set RESVD_SET always enabled */
3166 }
3167
3168 lck_mtx_unlock(ipfw_mutex);
3169 }
3170 break;
3171 }
3172 case IP_FW_ZERO:
3173 case IP_FW_RESETLOG: /* using rule->rulenum */
3174 {
3175 /* there is only a simple rule passed in
3176 * (no cmds), so use a temp struct to copy
3177 */
3178 struct ip_fw temp_rule = { 0 };
3179
3180 if (api_version != IP_FW_CURRENT_API_VERSION) {
3181 error = ipfw_convert_to_latest(sopt, &temp_rule, api_version);
3182 }
3183 else {
3184 if (sopt->sopt_val != 0) {
3185 error = sooptcopyin(sopt, &temp_rule, sizeof(struct ip_fw),
3186 sizeof(struct ip_fw) );
3187 }
3188 }
3189
3190 if (!error) {
3191 lck_mtx_lock(ipfw_mutex);
3192 error = zero_entry(temp_rule.rulenum, sopt->sopt_name == IP_FW_RESETLOG);
3193 lck_mtx_unlock(ipfw_mutex);
3194 }
3195 break;
3196 }
3197 default:
3198 printf("ipfw: ipfw_ctl invalid option %d\n", sopt->sopt_name);
3199 error = EINVAL;
3200 }
3201
3202 return (error);
3203 }
3204
3205 /**
3206 * dummynet needs a reference to the default rule, because rules can be
3207 * deleted while packets hold a reference to them. When this happens,
3208 * dummynet changes the reference to the default rule (it could well be a
3209 * NULL pointer, but this way we do not need to check for the special
3210 * case, plus here he have info on the default behaviour).
3211 */
3212 struct ip_fw *ip_fw_default_rule;
3213
3214 /*
3215 * This procedure is only used to handle keepalives. It is invoked
3216 * every dyn_keepalive_period
3217 */
3218 static void
3219 ipfw_tick(void * __unused unused)
3220 {
3221 int i;
3222 int s;
3223 ipfw_dyn_rule *q;
3224 struct timeval timenow;
3225
3226
3227 if (dyn_keepalive == 0 || ipfw_dyn_v == NULL || dyn_count == 0)
3228 goto done;
3229
3230 getmicrotime(&timenow);
3231
3232 lck_mtx_lock(ipfw_mutex);
3233 for (i = 0 ; i < curr_dyn_buckets ; i++) {
3234 for (q = ipfw_dyn_v[i] ; q ; q = q->next ) {
3235 if (q->dyn_type == O_LIMIT_PARENT)
3236 continue;
3237 if (q->id.proto != IPPROTO_TCP)
3238 continue;
3239 if ( (q->state & BOTH_SYN) != BOTH_SYN)
3240 continue;
3241 if (TIME_LEQ( timenow.tv_sec+dyn_keepalive_interval,
3242 q->expire))
3243 continue; /* too early */
3244 if (TIME_LEQ(q->expire, timenow.tv_sec))
3245 continue; /* too late, rule expired */
3246
3247 send_pkt(&(q->id), q->ack_rev - 1, q->ack_fwd, TH_SYN);
3248 send_pkt(&(q->id), q->ack_fwd - 1, q->ack_rev, 0);
3249 }
3250 }
3251 lck_mtx_unlock(ipfw_mutex);
3252 done:
3253 timeout(ipfw_tick, NULL, dyn_keepalive_period*hz);
3254 }
3255
3256 void
3257 ipfw_init(void)
3258 {
3259 struct ip_fw default_rule;
3260
3261 /* setup locks */
3262 ipfw_mutex_grp_attr = lck_grp_attr_alloc_init();
3263 ipfw_mutex_grp = lck_grp_alloc_init("ipfw", ipfw_mutex_grp_attr);
3264 ipfw_mutex_attr = lck_attr_alloc_init();
3265 lck_attr_setdefault(ipfw_mutex_attr);
3266
3267 if ((ipfw_mutex = lck_mtx_alloc_init(ipfw_mutex_grp, ipfw_mutex_attr)) == NULL) {
3268 printf("ipfw_init: can't alloc ipfw_mutex\n");
3269 return;
3270 }
3271
3272 layer3_chain = NULL;
3273
3274 bzero(&default_rule, sizeof default_rule);
3275
3276 default_rule.act_ofs = 0;
3277 default_rule.rulenum = IPFW_DEFAULT_RULE;
3278 default_rule.cmd_len = 1;
3279 default_rule.set = RESVD_SET;
3280
3281 default_rule.cmd[0].len = 1;
3282 default_rule.cmd[0].opcode =
3283 #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT
3284 1 ? O_ACCEPT :
3285 #endif
3286 O_DENY;
3287
3288 if (add_rule(&layer3_chain, &default_rule)) {
3289 printf("ipfw2: add_rule failed adding default rule\n");
3290 printf("ipfw2 failed initialization!!\n");
3291 fw_enable = 0;
3292 }
3293 else {
3294 ip_fw_default_rule = layer3_chain;
3295 #if 0
3296 /* Radar 3920649, don't print unncessary messages to the log */
3297 printf("ipfw2 initialized, divert %s, "
3298 "rule-based forwarding enabled, default to %s, logging ",
3299 #ifdef IPDIVERT
3300 "enabled",
3301 #else
3302 "disabled",
3303 #endif
3304 default_rule.cmd[0].opcode == O_ACCEPT ? "accept" : "deny");
3305 #endif
3306
3307 #ifdef IPFIREWALL_VERBOSE
3308 fw_verbose = 1;
3309 #endif
3310 #ifdef IPFIREWALL_VERBOSE_LIMIT
3311 verbose_limit = IPFIREWALL_VERBOSE_LIMIT;
3312 #endif
3313 if (fw_verbose == 0)
3314 printf("disabled\n");
3315 else if (verbose_limit == 0)
3316 printf("unlimited\n");
3317 else
3318 printf("limited to %d packets/entry by default\n",
3319 verbose_limit);
3320 }
3321
3322 ip_fw_chk_ptr = ipfw_chk;
3323 ip_fw_ctl_ptr = ipfw_ctl;
3324
3325 ipfwstringlen = strlen( ipfwstring );
3326
3327 timeout(ipfw_tick, NULL, hz);
3328 }
3329
3330 #endif /* IPFW2 */