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