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