1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | * SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <sys/cdefs.h> |
---|
29 | __FBSDID("$FreeBSD$"); |
---|
30 | |
---|
31 | /* |
---|
32 | * The FreeBSD IP packet firewall, main file |
---|
33 | */ |
---|
34 | |
---|
35 | #include <rtems/bsd/local/opt_ipfw.h> |
---|
36 | #include <rtems/bsd/local/opt_ipdivert.h> |
---|
37 | #include <rtems/bsd/local/opt_inet.h> |
---|
38 | #ifndef INET |
---|
39 | #error "IPFIREWALL requires INET" |
---|
40 | #endif /* INET */ |
---|
41 | #include <rtems/bsd/local/opt_inet6.h> |
---|
42 | #include <rtems/bsd/local/opt_ipsec.h> |
---|
43 | |
---|
44 | #include <sys/param.h> |
---|
45 | #include <sys/systm.h> |
---|
46 | #include <sys/condvar.h> |
---|
47 | #include <sys/counter.h> |
---|
48 | #include <sys/eventhandler.h> |
---|
49 | #include <sys/malloc.h> |
---|
50 | #include <sys/mbuf.h> |
---|
51 | #include <sys/kernel.h> |
---|
52 | #include <sys/lock.h> |
---|
53 | #include <sys/jail.h> |
---|
54 | #include <sys/module.h> |
---|
55 | #include <sys/priv.h> |
---|
56 | #include <sys/proc.h> |
---|
57 | #include <sys/rwlock.h> |
---|
58 | #include <sys/rmlock.h> |
---|
59 | #include <sys/socket.h> |
---|
60 | #include <sys/socketvar.h> |
---|
61 | #include <sys/sysctl.h> |
---|
62 | #include <sys/syslog.h> |
---|
63 | #include <sys/ucred.h> |
---|
64 | #include <net/ethernet.h> /* for ETHERTYPE_IP */ |
---|
65 | #include <net/if.h> |
---|
66 | #include <net/if_var.h> |
---|
67 | #include <net/route.h> |
---|
68 | #include <net/pfil.h> |
---|
69 | #include <net/vnet.h> |
---|
70 | |
---|
71 | #include <netpfil/pf/pf_mtag.h> |
---|
72 | |
---|
73 | #include <netinet/in.h> |
---|
74 | #include <netinet/in_var.h> |
---|
75 | #include <netinet/in_pcb.h> |
---|
76 | #include <netinet/ip.h> |
---|
77 | #include <netinet/ip_var.h> |
---|
78 | #include <netinet/ip_icmp.h> |
---|
79 | #include <netinet/ip_fw.h> |
---|
80 | #include <netinet/ip_carp.h> |
---|
81 | #include <netinet/pim.h> |
---|
82 | #include <netinet/tcp_var.h> |
---|
83 | #include <netinet/udp.h> |
---|
84 | #include <netinet/udp_var.h> |
---|
85 | #include <netinet/sctp.h> |
---|
86 | |
---|
87 | #include <netinet/ip6.h> |
---|
88 | #include <netinet/icmp6.h> |
---|
89 | #include <netinet/in_fib.h> |
---|
90 | #ifdef INET6 |
---|
91 | #include <netinet6/in6_fib.h> |
---|
92 | #include <netinet6/in6_pcb.h> |
---|
93 | #include <netinet6/scope6_var.h> |
---|
94 | #include <netinet6/ip6_var.h> |
---|
95 | #endif |
---|
96 | |
---|
97 | #include <netpfil/ipfw/ip_fw_private.h> |
---|
98 | |
---|
99 | #include <machine/in_cksum.h> /* XXX for in_cksum */ |
---|
100 | |
---|
101 | #ifdef MAC |
---|
102 | #include <security/mac/mac_framework.h> |
---|
103 | #endif |
---|
104 | |
---|
105 | /* |
---|
106 | * static variables followed by global ones. |
---|
107 | * All ipfw global variables are here. |
---|
108 | */ |
---|
109 | |
---|
110 | static VNET_DEFINE(int, fw_deny_unknown_exthdrs); |
---|
111 | #define V_fw_deny_unknown_exthdrs VNET(fw_deny_unknown_exthdrs) |
---|
112 | |
---|
113 | static VNET_DEFINE(int, fw_permit_single_frag6) = 1; |
---|
114 | #define V_fw_permit_single_frag6 VNET(fw_permit_single_frag6) |
---|
115 | |
---|
116 | #ifdef IPFIREWALL_DEFAULT_TO_ACCEPT |
---|
117 | static int default_to_accept = 1; |
---|
118 | #else |
---|
119 | static int default_to_accept; |
---|
120 | #endif |
---|
121 | |
---|
122 | VNET_DEFINE(int, autoinc_step); |
---|
123 | VNET_DEFINE(int, fw_one_pass) = 1; |
---|
124 | |
---|
125 | VNET_DEFINE(unsigned int, fw_tables_max); |
---|
126 | VNET_DEFINE(unsigned int, fw_tables_sets) = 0; /* Don't use set-aware tables */ |
---|
127 | /* Use 128 tables by default */ |
---|
128 | static unsigned int default_fw_tables = IPFW_TABLES_DEFAULT; |
---|
129 | |
---|
130 | #ifndef LINEAR_SKIPTO |
---|
131 | static int jump_fast(struct ip_fw_chain *chain, struct ip_fw *f, int num, |
---|
132 | int tablearg, int jump_backwards); |
---|
133 | #define JUMP(ch, f, num, targ, back) jump_fast(ch, f, num, targ, back) |
---|
134 | #else |
---|
135 | static int jump_linear(struct ip_fw_chain *chain, struct ip_fw *f, int num, |
---|
136 | int tablearg, int jump_backwards); |
---|
137 | #define JUMP(ch, f, num, targ, back) jump_linear(ch, f, num, targ, back) |
---|
138 | #endif |
---|
139 | |
---|
140 | /* |
---|
141 | * Each rule belongs to one of 32 different sets (0..31). |
---|
142 | * The variable set_disable contains one bit per set. |
---|
143 | * If the bit is set, all rules in the corresponding set |
---|
144 | * are disabled. Set RESVD_SET(31) is reserved for the default rule |
---|
145 | * and rules that are not deleted by the flush command, |
---|
146 | * and CANNOT be disabled. |
---|
147 | * Rules in set RESVD_SET can only be deleted individually. |
---|
148 | */ |
---|
149 | VNET_DEFINE(u_int32_t, set_disable); |
---|
150 | #define V_set_disable VNET(set_disable) |
---|
151 | |
---|
152 | VNET_DEFINE(int, fw_verbose); |
---|
153 | /* counter for ipfw_log(NULL...) */ |
---|
154 | VNET_DEFINE(u_int64_t, norule_counter); |
---|
155 | VNET_DEFINE(int, verbose_limit); |
---|
156 | |
---|
157 | /* layer3_chain contains the list of rules for layer 3 */ |
---|
158 | VNET_DEFINE(struct ip_fw_chain, layer3_chain); |
---|
159 | |
---|
160 | /* ipfw_vnet_ready controls when we are open for business */ |
---|
161 | VNET_DEFINE(int, ipfw_vnet_ready) = 0; |
---|
162 | |
---|
163 | VNET_DEFINE(int, ipfw_nat_ready) = 0; |
---|
164 | |
---|
165 | ipfw_nat_t *ipfw_nat_ptr = NULL; |
---|
166 | struct cfg_nat *(*lookup_nat_ptr)(struct nat_list *, int); |
---|
167 | ipfw_nat_cfg_t *ipfw_nat_cfg_ptr; |
---|
168 | ipfw_nat_cfg_t *ipfw_nat_del_ptr; |
---|
169 | ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr; |
---|
170 | ipfw_nat_cfg_t *ipfw_nat_get_log_ptr; |
---|
171 | |
---|
172 | #ifdef SYSCTL_NODE |
---|
173 | uint32_t dummy_def = IPFW_DEFAULT_RULE; |
---|
174 | static int sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS); |
---|
175 | static int sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS); |
---|
176 | |
---|
177 | SYSBEGIN(f3) |
---|
178 | |
---|
179 | SYSCTL_NODE(_net_inet_ip, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall"); |
---|
180 | SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, one_pass, |
---|
181 | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_one_pass), 0, |
---|
182 | "Only do a single pass through ipfw when using dummynet(4)"); |
---|
183 | SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, autoinc_step, |
---|
184 | CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(autoinc_step), 0, |
---|
185 | "Rule number auto-increment step"); |
---|
186 | SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose, |
---|
187 | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_verbose), 0, |
---|
188 | "Log matches to ipfw rules"); |
---|
189 | SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, verbose_limit, |
---|
190 | CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(verbose_limit), 0, |
---|
191 | "Set upper limit of matches of ipfw rules logged"); |
---|
192 | SYSCTL_UINT(_net_inet_ip_fw, OID_AUTO, default_rule, CTLFLAG_RD, |
---|
193 | &dummy_def, 0, |
---|
194 | "The default/max possible rule number."); |
---|
195 | SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_max, |
---|
196 | CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, 0, 0, sysctl_ipfw_table_num, "IU", |
---|
197 | "Maximum number of concurrently used tables"); |
---|
198 | SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, tables_sets, |
---|
199 | CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW, |
---|
200 | 0, 0, sysctl_ipfw_tables_sets, "IU", |
---|
201 | "Use per-set namespace for tables"); |
---|
202 | SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, default_to_accept, CTLFLAG_RDTUN, |
---|
203 | &default_to_accept, 0, |
---|
204 | "Make the default rule accept all packets."); |
---|
205 | TUNABLE_INT("net.inet.ip.fw.tables_max", (int *)&default_fw_tables); |
---|
206 | SYSCTL_INT(_net_inet_ip_fw, OID_AUTO, static_count, |
---|
207 | CTLFLAG_VNET | CTLFLAG_RD, &VNET_NAME(layer3_chain.n_rules), 0, |
---|
208 | "Number of static rules"); |
---|
209 | |
---|
210 | #ifdef INET6 |
---|
211 | SYSCTL_DECL(_net_inet6_ip6); |
---|
212 | SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall"); |
---|
213 | SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, deny_unknown_exthdrs, |
---|
214 | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, |
---|
215 | &VNET_NAME(fw_deny_unknown_exthdrs), 0, |
---|
216 | "Deny packets with unknown IPv6 Extension Headers"); |
---|
217 | SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, permit_single_frag6, |
---|
218 | CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE, |
---|
219 | &VNET_NAME(fw_permit_single_frag6), 0, |
---|
220 | "Permit single packet IPv6 fragments"); |
---|
221 | #endif /* INET6 */ |
---|
222 | |
---|
223 | SYSEND |
---|
224 | |
---|
225 | #endif /* SYSCTL_NODE */ |
---|
226 | |
---|
227 | |
---|
228 | /* |
---|
229 | * Some macros used in the various matching options. |
---|
230 | * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T |
---|
231 | * Other macros just cast void * into the appropriate type |
---|
232 | */ |
---|
233 | #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl)) |
---|
234 | #define TCP(p) ((struct tcphdr *)(p)) |
---|
235 | #define SCTP(p) ((struct sctphdr *)(p)) |
---|
236 | #define UDP(p) ((struct udphdr *)(p)) |
---|
237 | #define ICMP(p) ((struct icmphdr *)(p)) |
---|
238 | #define ICMP6(p) ((struct icmp6_hdr *)(p)) |
---|
239 | |
---|
240 | static __inline int |
---|
241 | icmptype_match(struct icmphdr *icmp, ipfw_insn_u32 *cmd) |
---|
242 | { |
---|
243 | int type = icmp->icmp_type; |
---|
244 | |
---|
245 | return (type <= ICMP_MAXTYPE && (cmd->d[0] & (1<<type)) ); |
---|
246 | } |
---|
247 | |
---|
248 | #define TT ( (1 << ICMP_ECHO) | (1 << ICMP_ROUTERSOLICIT) | \ |
---|
249 | (1 << ICMP_TSTAMP) | (1 << ICMP_IREQ) | (1 << ICMP_MASKREQ) ) |
---|
250 | |
---|
251 | static int |
---|
252 | is_icmp_query(struct icmphdr *icmp) |
---|
253 | { |
---|
254 | int type = icmp->icmp_type; |
---|
255 | |
---|
256 | return (type <= ICMP_MAXTYPE && (TT & (1<<type)) ); |
---|
257 | } |
---|
258 | #undef TT |
---|
259 | |
---|
260 | /* |
---|
261 | * The following checks use two arrays of 8 or 16 bits to store the |
---|
262 | * bits that we want set or clear, respectively. They are in the |
---|
263 | * low and high half of cmd->arg1 or cmd->d[0]. |
---|
264 | * |
---|
265 | * We scan options and store the bits we find set. We succeed if |
---|
266 | * |
---|
267 | * (want_set & ~bits) == 0 && (want_clear & ~bits) == want_clear |
---|
268 | * |
---|
269 | * The code is sometimes optimized not to store additional variables. |
---|
270 | */ |
---|
271 | |
---|
272 | static int |
---|
273 | flags_match(ipfw_insn *cmd, u_int8_t bits) |
---|
274 | { |
---|
275 | u_char want_clear; |
---|
276 | bits = ~bits; |
---|
277 | |
---|
278 | if ( ((cmd->arg1 & 0xff) & bits) != 0) |
---|
279 | return 0; /* some bits we want set were clear */ |
---|
280 | want_clear = (cmd->arg1 >> 8) & 0xff; |
---|
281 | if ( (want_clear & bits) != want_clear) |
---|
282 | return 0; /* some bits we want clear were set */ |
---|
283 | return 1; |
---|
284 | } |
---|
285 | |
---|
286 | static int |
---|
287 | ipopts_match(struct ip *ip, ipfw_insn *cmd) |
---|
288 | { |
---|
289 | int optlen, bits = 0; |
---|
290 | u_char *cp = (u_char *)(ip + 1); |
---|
291 | int x = (ip->ip_hl << 2) - sizeof (struct ip); |
---|
292 | |
---|
293 | for (; x > 0; x -= optlen, cp += optlen) { |
---|
294 | int opt = cp[IPOPT_OPTVAL]; |
---|
295 | |
---|
296 | if (opt == IPOPT_EOL) |
---|
297 | break; |
---|
298 | if (opt == IPOPT_NOP) |
---|
299 | optlen = 1; |
---|
300 | else { |
---|
301 | optlen = cp[IPOPT_OLEN]; |
---|
302 | if (optlen <= 0 || optlen > x) |
---|
303 | return 0; /* invalid or truncated */ |
---|
304 | } |
---|
305 | switch (opt) { |
---|
306 | |
---|
307 | default: |
---|
308 | break; |
---|
309 | |
---|
310 | case IPOPT_LSRR: |
---|
311 | bits |= IP_FW_IPOPT_LSRR; |
---|
312 | break; |
---|
313 | |
---|
314 | case IPOPT_SSRR: |
---|
315 | bits |= IP_FW_IPOPT_SSRR; |
---|
316 | break; |
---|
317 | |
---|
318 | case IPOPT_RR: |
---|
319 | bits |= IP_FW_IPOPT_RR; |
---|
320 | break; |
---|
321 | |
---|
322 | case IPOPT_TS: |
---|
323 | bits |= IP_FW_IPOPT_TS; |
---|
324 | break; |
---|
325 | } |
---|
326 | } |
---|
327 | return (flags_match(cmd, bits)); |
---|
328 | } |
---|
329 | |
---|
330 | static int |
---|
331 | tcpopts_match(struct tcphdr *tcp, ipfw_insn *cmd) |
---|
332 | { |
---|
333 | int optlen, bits = 0; |
---|
334 | u_char *cp = (u_char *)(tcp + 1); |
---|
335 | int x = (tcp->th_off << 2) - sizeof(struct tcphdr); |
---|
336 | |
---|
337 | for (; x > 0; x -= optlen, cp += optlen) { |
---|
338 | int opt = cp[0]; |
---|
339 | if (opt == TCPOPT_EOL) |
---|
340 | break; |
---|
341 | if (opt == TCPOPT_NOP) |
---|
342 | optlen = 1; |
---|
343 | else { |
---|
344 | optlen = cp[1]; |
---|
345 | if (optlen <= 0) |
---|
346 | break; |
---|
347 | } |
---|
348 | |
---|
349 | switch (opt) { |
---|
350 | |
---|
351 | default: |
---|
352 | break; |
---|
353 | |
---|
354 | case TCPOPT_MAXSEG: |
---|
355 | bits |= IP_FW_TCPOPT_MSS; |
---|
356 | break; |
---|
357 | |
---|
358 | case TCPOPT_WINDOW: |
---|
359 | bits |= IP_FW_TCPOPT_WINDOW; |
---|
360 | break; |
---|
361 | |
---|
362 | case TCPOPT_SACK_PERMITTED: |
---|
363 | case TCPOPT_SACK: |
---|
364 | bits |= IP_FW_TCPOPT_SACK; |
---|
365 | break; |
---|
366 | |
---|
367 | case TCPOPT_TIMESTAMP: |
---|
368 | bits |= IP_FW_TCPOPT_TS; |
---|
369 | break; |
---|
370 | |
---|
371 | } |
---|
372 | } |
---|
373 | return (flags_match(cmd, bits)); |
---|
374 | } |
---|
375 | |
---|
376 | static int |
---|
377 | iface_match(struct ifnet *ifp, ipfw_insn_if *cmd, struct ip_fw_chain *chain, |
---|
378 | uint32_t *tablearg) |
---|
379 | { |
---|
380 | |
---|
381 | if (ifp == NULL) /* no iface with this packet, match fails */ |
---|
382 | return (0); |
---|
383 | |
---|
384 | /* Check by name or by IP address */ |
---|
385 | if (cmd->name[0] != '\0') { /* match by name */ |
---|
386 | if (cmd->name[0] == '\1') /* use tablearg to match */ |
---|
387 | return ipfw_lookup_table(chain, cmd->p.kidx, 0, |
---|
388 | &ifp->if_index, tablearg); |
---|
389 | /* Check name */ |
---|
390 | if (cmd->p.glob) { |
---|
391 | if (fnmatch(cmd->name, ifp->if_xname, 0) == 0) |
---|
392 | return(1); |
---|
393 | } else { |
---|
394 | if (strncmp(ifp->if_xname, cmd->name, IFNAMSIZ) == 0) |
---|
395 | return(1); |
---|
396 | } |
---|
397 | } else { |
---|
398 | #if !defined(USERSPACE) && defined(__FreeBSD__) /* and OSX too ? */ |
---|
399 | struct ifaddr *ia; |
---|
400 | |
---|
401 | if_addr_rlock(ifp); |
---|
402 | TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { |
---|
403 | if (ia->ifa_addr->sa_family != AF_INET) |
---|
404 | continue; |
---|
405 | if (cmd->p.ip.s_addr == ((struct sockaddr_in *) |
---|
406 | (ia->ifa_addr))->sin_addr.s_addr) { |
---|
407 | if_addr_runlock(ifp); |
---|
408 | return(1); /* match */ |
---|
409 | } |
---|
410 | } |
---|
411 | if_addr_runlock(ifp); |
---|
412 | #endif /* __FreeBSD__ */ |
---|
413 | } |
---|
414 | return(0); /* no match, fail ... */ |
---|
415 | } |
---|
416 | |
---|
417 | /* |
---|
418 | * The verify_path function checks if a route to the src exists and |
---|
419 | * if it is reachable via ifp (when provided). |
---|
420 | * |
---|
421 | * The 'verrevpath' option checks that the interface that an IP packet |
---|
422 | * arrives on is the same interface that traffic destined for the |
---|
423 | * packet's source address would be routed out of. |
---|
424 | * The 'versrcreach' option just checks that the source address is |
---|
425 | * reachable via any route (except default) in the routing table. |
---|
426 | * These two are a measure to block forged packets. This is also |
---|
427 | * commonly known as "anti-spoofing" or Unicast Reverse Path |
---|
428 | * Forwarding (Unicast RFP) in Cisco-ese. The name of the knobs |
---|
429 | * is purposely reminiscent of the Cisco IOS command, |
---|
430 | * |
---|
431 | * ip verify unicast reverse-path |
---|
432 | * ip verify unicast source reachable-via any |
---|
433 | * |
---|
434 | * which implements the same functionality. But note that the syntax |
---|
435 | * is misleading, and the check may be performed on all IP packets |
---|
436 | * whether unicast, multicast, or broadcast. |
---|
437 | */ |
---|
438 | static int |
---|
439 | verify_path(struct in_addr src, struct ifnet *ifp, u_int fib) |
---|
440 | { |
---|
441 | #if defined(USERSPACE) || !defined(__FreeBSD__) |
---|
442 | return 0; |
---|
443 | #else |
---|
444 | struct nhop4_basic nh4; |
---|
445 | |
---|
446 | if (fib4_lookup_nh_basic(fib, src, NHR_IFAIF, 0, &nh4) != 0) |
---|
447 | return (0); |
---|
448 | |
---|
449 | /* |
---|
450 | * If ifp is provided, check for equality with rtentry. |
---|
451 | * We should use rt->rt_ifa->ifa_ifp, instead of rt->rt_ifp, |
---|
452 | * in order to pass packets injected back by if_simloop(): |
---|
453 | * routing entry (via lo0) for our own address |
---|
454 | * may exist, so we need to handle routing assymetry. |
---|
455 | */ |
---|
456 | if (ifp != NULL && ifp != nh4.nh_ifp) |
---|
457 | return (0); |
---|
458 | |
---|
459 | /* if no ifp provided, check if rtentry is not default route */ |
---|
460 | if (ifp == NULL && (nh4.nh_flags & NHF_DEFAULT) != 0) |
---|
461 | return (0); |
---|
462 | |
---|
463 | /* or if this is a blackhole/reject route */ |
---|
464 | if (ifp == NULL && (nh4.nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0) |
---|
465 | return (0); |
---|
466 | |
---|
467 | /* found valid route */ |
---|
468 | return 1; |
---|
469 | #endif /* __FreeBSD__ */ |
---|
470 | } |
---|
471 | |
---|
472 | #ifdef INET6 |
---|
473 | /* |
---|
474 | * ipv6 specific rules here... |
---|
475 | */ |
---|
476 | static __inline int |
---|
477 | icmp6type_match (int type, ipfw_insn_u32 *cmd) |
---|
478 | { |
---|
479 | return (type <= ICMP6_MAXTYPE && (cmd->d[type/32] & (1<<(type%32)) ) ); |
---|
480 | } |
---|
481 | |
---|
482 | static int |
---|
483 | flow6id_match( int curr_flow, ipfw_insn_u32 *cmd ) |
---|
484 | { |
---|
485 | int i; |
---|
486 | for (i=0; i <= cmd->o.arg1; ++i ) |
---|
487 | if (curr_flow == cmd->d[i] ) |
---|
488 | return 1; |
---|
489 | return 0; |
---|
490 | } |
---|
491 | |
---|
492 | /* support for IP6_*_ME opcodes */ |
---|
493 | static const struct in6_addr lla_mask = {{{ |
---|
494 | 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, |
---|
495 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff |
---|
496 | }}}; |
---|
497 | |
---|
498 | static int |
---|
499 | ipfw_localip6(struct in6_addr *in6) |
---|
500 | { |
---|
501 | struct rm_priotracker in6_ifa_tracker; |
---|
502 | struct in6_ifaddr *ia; |
---|
503 | |
---|
504 | if (IN6_IS_ADDR_MULTICAST(in6)) |
---|
505 | return (0); |
---|
506 | |
---|
507 | if (!IN6_IS_ADDR_LINKLOCAL(in6)) |
---|
508 | return (in6_localip(in6)); |
---|
509 | |
---|
510 | IN6_IFADDR_RLOCK(&in6_ifa_tracker); |
---|
511 | TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { |
---|
512 | if (!IN6_IS_ADDR_LINKLOCAL(&ia->ia_addr.sin6_addr)) |
---|
513 | continue; |
---|
514 | if (IN6_ARE_MASKED_ADDR_EQUAL(&ia->ia_addr.sin6_addr, |
---|
515 | in6, &lla_mask)) { |
---|
516 | IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); |
---|
517 | return (1); |
---|
518 | } |
---|
519 | } |
---|
520 | IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); |
---|
521 | return (0); |
---|
522 | } |
---|
523 | |
---|
524 | static int |
---|
525 | verify_path6(struct in6_addr *src, struct ifnet *ifp, u_int fib) |
---|
526 | { |
---|
527 | struct nhop6_basic nh6; |
---|
528 | |
---|
529 | if (IN6_IS_SCOPE_LINKLOCAL(src)) |
---|
530 | return (1); |
---|
531 | |
---|
532 | if (fib6_lookup_nh_basic(fib, src, 0, NHR_IFAIF, 0, &nh6) != 0) |
---|
533 | return (0); |
---|
534 | |
---|
535 | /* If ifp is provided, check for equality with route table. */ |
---|
536 | if (ifp != NULL && ifp != nh6.nh_ifp) |
---|
537 | return (0); |
---|
538 | |
---|
539 | /* if no ifp provided, check if rtentry is not default route */ |
---|
540 | if (ifp == NULL && (nh6.nh_flags & NHF_DEFAULT) != 0) |
---|
541 | return (0); |
---|
542 | |
---|
543 | /* or if this is a blackhole/reject route */ |
---|
544 | if (ifp == NULL && (nh6.nh_flags & (NHF_REJECT|NHF_BLACKHOLE)) != 0) |
---|
545 | return (0); |
---|
546 | |
---|
547 | /* found valid route */ |
---|
548 | return 1; |
---|
549 | } |
---|
550 | |
---|
551 | static int |
---|
552 | is_icmp6_query(int icmp6_type) |
---|
553 | { |
---|
554 | if ((icmp6_type <= ICMP6_MAXTYPE) && |
---|
555 | (icmp6_type == ICMP6_ECHO_REQUEST || |
---|
556 | icmp6_type == ICMP6_MEMBERSHIP_QUERY || |
---|
557 | icmp6_type == ICMP6_WRUREQUEST || |
---|
558 | icmp6_type == ICMP6_FQDN_QUERY || |
---|
559 | icmp6_type == ICMP6_NI_QUERY)) |
---|
560 | return (1); |
---|
561 | |
---|
562 | return (0); |
---|
563 | } |
---|
564 | |
---|
565 | static void |
---|
566 | send_reject6(struct ip_fw_args *args, int code, u_int hlen, struct ip6_hdr *ip6) |
---|
567 | { |
---|
568 | struct mbuf *m; |
---|
569 | |
---|
570 | m = args->m; |
---|
571 | if (code == ICMP6_UNREACH_RST && args->f_id.proto == IPPROTO_TCP) { |
---|
572 | struct tcphdr *tcp; |
---|
573 | tcp = (struct tcphdr *)((char *)ip6 + hlen); |
---|
574 | |
---|
575 | if ((tcp->th_flags & TH_RST) == 0) { |
---|
576 | struct mbuf *m0; |
---|
577 | m0 = ipfw_send_pkt(args->m, &(args->f_id), |
---|
578 | ntohl(tcp->th_seq), ntohl(tcp->th_ack), |
---|
579 | tcp->th_flags | TH_RST); |
---|
580 | if (m0 != NULL) |
---|
581 | ip6_output(m0, NULL, NULL, 0, NULL, NULL, |
---|
582 | NULL); |
---|
583 | } |
---|
584 | FREE_PKT(m); |
---|
585 | } else if (code != ICMP6_UNREACH_RST) { /* Send an ICMPv6 unreach. */ |
---|
586 | #if 0 |
---|
587 | /* |
---|
588 | * Unlike above, the mbufs need to line up with the ip6 hdr, |
---|
589 | * as the contents are read. We need to m_adj() the |
---|
590 | * needed amount. |
---|
591 | * The mbuf will however be thrown away so we can adjust it. |
---|
592 | * Remember we did an m_pullup on it already so we |
---|
593 | * can make some assumptions about contiguousness. |
---|
594 | */ |
---|
595 | if (args->L3offset) |
---|
596 | m_adj(m, args->L3offset); |
---|
597 | #endif |
---|
598 | icmp6_error(m, ICMP6_DST_UNREACH, code, 0); |
---|
599 | } else |
---|
600 | FREE_PKT(m); |
---|
601 | |
---|
602 | args->m = NULL; |
---|
603 | } |
---|
604 | |
---|
605 | #endif /* INET6 */ |
---|
606 | |
---|
607 | |
---|
608 | /* |
---|
609 | * sends a reject message, consuming the mbuf passed as an argument. |
---|
610 | */ |
---|
611 | static void |
---|
612 | send_reject(struct ip_fw_args *args, int code, int iplen, struct ip *ip) |
---|
613 | { |
---|
614 | |
---|
615 | #if 0 |
---|
616 | /* XXX When ip is not guaranteed to be at mtod() we will |
---|
617 | * need to account for this */ |
---|
618 | * The mbuf will however be thrown away so we can adjust it. |
---|
619 | * Remember we did an m_pullup on it already so we |
---|
620 | * can make some assumptions about contiguousness. |
---|
621 | */ |
---|
622 | if (args->L3offset) |
---|
623 | m_adj(m, args->L3offset); |
---|
624 | #endif |
---|
625 | if (code != ICMP_REJECT_RST) { /* Send an ICMP unreach */ |
---|
626 | icmp_error(args->m, ICMP_UNREACH, code, 0L, 0); |
---|
627 | } else if (args->f_id.proto == IPPROTO_TCP) { |
---|
628 | struct tcphdr *const tcp = |
---|
629 | L3HDR(struct tcphdr, mtod(args->m, struct ip *)); |
---|
630 | if ( (tcp->th_flags & TH_RST) == 0) { |
---|
631 | struct mbuf *m; |
---|
632 | m = ipfw_send_pkt(args->m, &(args->f_id), |
---|
633 | ntohl(tcp->th_seq), ntohl(tcp->th_ack), |
---|
634 | tcp->th_flags | TH_RST); |
---|
635 | if (m != NULL) |
---|
636 | ip_output(m, NULL, NULL, 0, NULL, NULL); |
---|
637 | } |
---|
638 | FREE_PKT(args->m); |
---|
639 | } else |
---|
640 | FREE_PKT(args->m); |
---|
641 | args->m = NULL; |
---|
642 | } |
---|
643 | |
---|
644 | /* |
---|
645 | * Support for uid/gid/jail lookup. These tests are expensive |
---|
646 | * (because we may need to look into the list of active sockets) |
---|
647 | * so we cache the results. ugid_lookupp is 0 if we have not |
---|
648 | * yet done a lookup, 1 if we succeeded, and -1 if we tried |
---|
649 | * and failed. The function always returns the match value. |
---|
650 | * We could actually spare the variable and use *uc, setting |
---|
651 | * it to '(void *)check_uidgid if we have no info, NULL if |
---|
652 | * we tried and failed, or any other value if successful. |
---|
653 | */ |
---|
654 | static int |
---|
655 | check_uidgid(ipfw_insn_u32 *insn, struct ip_fw_args *args, int *ugid_lookupp, |
---|
656 | struct ucred **uc) |
---|
657 | { |
---|
658 | #if defined(USERSPACE) |
---|
659 | return 0; // not supported in userspace |
---|
660 | #else |
---|
661 | #ifndef __FreeBSD__ |
---|
662 | /* XXX */ |
---|
663 | return cred_check(insn, proto, oif, |
---|
664 | dst_ip, dst_port, src_ip, src_port, |
---|
665 | (struct bsd_ucred *)uc, ugid_lookupp, ((struct mbuf *)inp)->m_skb); |
---|
666 | #else /* FreeBSD */ |
---|
667 | struct in_addr src_ip, dst_ip; |
---|
668 | struct inpcbinfo *pi; |
---|
669 | struct ipfw_flow_id *id; |
---|
670 | struct inpcb *pcb, *inp; |
---|
671 | struct ifnet *oif; |
---|
672 | int lookupflags; |
---|
673 | int match; |
---|
674 | |
---|
675 | id = &args->f_id; |
---|
676 | inp = args->inp; |
---|
677 | oif = args->oif; |
---|
678 | |
---|
679 | /* |
---|
680 | * Check to see if the UDP or TCP stack supplied us with |
---|
681 | * the PCB. If so, rather then holding a lock and looking |
---|
682 | * up the PCB, we can use the one that was supplied. |
---|
683 | */ |
---|
684 | if (inp && *ugid_lookupp == 0) { |
---|
685 | INP_LOCK_ASSERT(inp); |
---|
686 | if (inp->inp_socket != NULL) { |
---|
687 | *uc = crhold(inp->inp_cred); |
---|
688 | *ugid_lookupp = 1; |
---|
689 | } else |
---|
690 | *ugid_lookupp = -1; |
---|
691 | } |
---|
692 | /* |
---|
693 | * If we have already been here and the packet has no |
---|
694 | * PCB entry associated with it, then we can safely |
---|
695 | * assume that this is a no match. |
---|
696 | */ |
---|
697 | if (*ugid_lookupp == -1) |
---|
698 | return (0); |
---|
699 | if (id->proto == IPPROTO_TCP) { |
---|
700 | lookupflags = 0; |
---|
701 | pi = &V_tcbinfo; |
---|
702 | } else if (id->proto == IPPROTO_UDP) { |
---|
703 | lookupflags = INPLOOKUP_WILDCARD; |
---|
704 | pi = &V_udbinfo; |
---|
705 | } else |
---|
706 | return 0; |
---|
707 | lookupflags |= INPLOOKUP_RLOCKPCB; |
---|
708 | match = 0; |
---|
709 | if (*ugid_lookupp == 0) { |
---|
710 | if (id->addr_type == 6) { |
---|
711 | #ifdef INET6 |
---|
712 | if (oif == NULL) |
---|
713 | pcb = in6_pcblookup_mbuf(pi, |
---|
714 | &id->src_ip6, htons(id->src_port), |
---|
715 | &id->dst_ip6, htons(id->dst_port), |
---|
716 | lookupflags, oif, args->m); |
---|
717 | else |
---|
718 | pcb = in6_pcblookup_mbuf(pi, |
---|
719 | &id->dst_ip6, htons(id->dst_port), |
---|
720 | &id->src_ip6, htons(id->src_port), |
---|
721 | lookupflags, oif, args->m); |
---|
722 | #else |
---|
723 | *ugid_lookupp = -1; |
---|
724 | return (0); |
---|
725 | #endif |
---|
726 | } else { |
---|
727 | src_ip.s_addr = htonl(id->src_ip); |
---|
728 | dst_ip.s_addr = htonl(id->dst_ip); |
---|
729 | if (oif == NULL) |
---|
730 | pcb = in_pcblookup_mbuf(pi, |
---|
731 | src_ip, htons(id->src_port), |
---|
732 | dst_ip, htons(id->dst_port), |
---|
733 | lookupflags, oif, args->m); |
---|
734 | else |
---|
735 | pcb = in_pcblookup_mbuf(pi, |
---|
736 | dst_ip, htons(id->dst_port), |
---|
737 | src_ip, htons(id->src_port), |
---|
738 | lookupflags, oif, args->m); |
---|
739 | } |
---|
740 | if (pcb != NULL) { |
---|
741 | INP_RLOCK_ASSERT(pcb); |
---|
742 | *uc = crhold(pcb->inp_cred); |
---|
743 | *ugid_lookupp = 1; |
---|
744 | INP_RUNLOCK(pcb); |
---|
745 | } |
---|
746 | if (*ugid_lookupp == 0) { |
---|
747 | /* |
---|
748 | * We tried and failed, set the variable to -1 |
---|
749 | * so we will not try again on this packet. |
---|
750 | */ |
---|
751 | *ugid_lookupp = -1; |
---|
752 | return (0); |
---|
753 | } |
---|
754 | } |
---|
755 | if (insn->o.opcode == O_UID) |
---|
756 | #ifndef __rtems__ |
---|
757 | match = ((*uc)->cr_uid == (uid_t)insn->d[0]); |
---|
758 | #else /* __rtems__ */ |
---|
759 | match = (BSD_DEFAULT_UID == (uid_t)insn->d[0]); |
---|
760 | #endif /* __rtems__ */ |
---|
761 | else if (insn->o.opcode == O_GID) |
---|
762 | match = groupmember((gid_t)insn->d[0], *uc); |
---|
763 | else if (insn->o.opcode == O_JAIL) |
---|
764 | #ifndef __rtems__ |
---|
765 | match = ((*uc)->cr_prison->pr_id == (int)insn->d[0]); |
---|
766 | #else /* __rtems__ */ |
---|
767 | match = (BSD_DEFAULT_PRISON->pr_id == (int)insn->d[0]); |
---|
768 | #endif /* __rtems__ */ |
---|
769 | return (match); |
---|
770 | #endif /* __FreeBSD__ */ |
---|
771 | #endif /* not supported in userspace */ |
---|
772 | } |
---|
773 | |
---|
774 | /* |
---|
775 | * Helper function to set args with info on the rule after the matching |
---|
776 | * one. slot is precise, whereas we guess rule_id as they are |
---|
777 | * assigned sequentially. |
---|
778 | */ |
---|
779 | static inline void |
---|
780 | set_match(struct ip_fw_args *args, int slot, |
---|
781 | struct ip_fw_chain *chain) |
---|
782 | { |
---|
783 | args->rule.chain_id = chain->id; |
---|
784 | args->rule.slot = slot + 1; /* we use 0 as a marker */ |
---|
785 | args->rule.rule_id = 1 + chain->map[slot]->id; |
---|
786 | args->rule.rulenum = chain->map[slot]->rulenum; |
---|
787 | } |
---|
788 | |
---|
789 | #ifndef LINEAR_SKIPTO |
---|
790 | /* |
---|
791 | * Helper function to enable cached rule lookups using |
---|
792 | * cached_id and cached_pos fields in ipfw rule. |
---|
793 | */ |
---|
794 | static int |
---|
795 | jump_fast(struct ip_fw_chain *chain, struct ip_fw *f, int num, |
---|
796 | int tablearg, int jump_backwards) |
---|
797 | { |
---|
798 | int f_pos; |
---|
799 | |
---|
800 | /* If possible use cached f_pos (in f->cached_pos), |
---|
801 | * whose version is written in f->cached_id |
---|
802 | * (horrible hacks to avoid changing the ABI). |
---|
803 | */ |
---|
804 | if (num != IP_FW_TARG && f->cached_id == chain->id) |
---|
805 | f_pos = f->cached_pos; |
---|
806 | else { |
---|
807 | int i = IP_FW_ARG_TABLEARG(chain, num, skipto); |
---|
808 | /* make sure we do not jump backward */ |
---|
809 | if (jump_backwards == 0 && i <= f->rulenum) |
---|
810 | i = f->rulenum + 1; |
---|
811 | if (chain->idxmap != NULL) |
---|
812 | f_pos = chain->idxmap[i]; |
---|
813 | else |
---|
814 | f_pos = ipfw_find_rule(chain, i, 0); |
---|
815 | /* update the cache */ |
---|
816 | if (num != IP_FW_TARG) { |
---|
817 | f->cached_id = chain->id; |
---|
818 | f->cached_pos = f_pos; |
---|
819 | } |
---|
820 | } |
---|
821 | |
---|
822 | return (f_pos); |
---|
823 | } |
---|
824 | #else |
---|
825 | /* |
---|
826 | * Helper function to enable real fast rule lookups. |
---|
827 | */ |
---|
828 | static int |
---|
829 | jump_linear(struct ip_fw_chain *chain, struct ip_fw *f, int num, |
---|
830 | int tablearg, int jump_backwards) |
---|
831 | { |
---|
832 | int f_pos; |
---|
833 | |
---|
834 | num = IP_FW_ARG_TABLEARG(chain, num, skipto); |
---|
835 | /* make sure we do not jump backward */ |
---|
836 | if (jump_backwards == 0 && num <= f->rulenum) |
---|
837 | num = f->rulenum + 1; |
---|
838 | f_pos = chain->idxmap[num]; |
---|
839 | |
---|
840 | return (f_pos); |
---|
841 | } |
---|
842 | #endif |
---|
843 | |
---|
844 | #define TARG(k, f) IP_FW_ARG_TABLEARG(chain, k, f) |
---|
845 | /* |
---|
846 | * The main check routine for the firewall. |
---|
847 | * |
---|
848 | * All arguments are in args so we can modify them and return them |
---|
849 | * back to the caller. |
---|
850 | * |
---|
851 | * Parameters: |
---|
852 | * |
---|
853 | * args->m (in/out) The packet; we set to NULL when/if we nuke it. |
---|
854 | * Starts with the IP header. |
---|
855 | * args->eh (in) Mac header if present, NULL for layer3 packet. |
---|
856 | * args->L3offset Number of bytes bypassed if we came from L2. |
---|
857 | * e.g. often sizeof(eh) ** NOTYET ** |
---|
858 | * args->oif Outgoing interface, NULL if packet is incoming. |
---|
859 | * The incoming interface is in the mbuf. (in) |
---|
860 | * args->divert_rule (in/out) |
---|
861 | * Skip up to the first rule past this rule number; |
---|
862 | * upon return, non-zero port number for divert or tee. |
---|
863 | * |
---|
864 | * args->rule Pointer to the last matching rule (in/out) |
---|
865 | * args->next_hop Socket we are forwarding to (out). |
---|
866 | * args->next_hop6 IPv6 next hop we are forwarding to (out). |
---|
867 | * args->f_id Addresses grabbed from the packet (out) |
---|
868 | * args->rule.info a cookie depending on rule action |
---|
869 | * |
---|
870 | * Return value: |
---|
871 | * |
---|
872 | * IP_FW_PASS the packet must be accepted |
---|
873 | * IP_FW_DENY the packet must be dropped |
---|
874 | * IP_FW_DIVERT divert packet, port in m_tag |
---|
875 | * IP_FW_TEE tee packet, port in m_tag |
---|
876 | * IP_FW_DUMMYNET to dummynet, pipe in args->cookie |
---|
877 | * IP_FW_NETGRAPH into netgraph, cookie args->cookie |
---|
878 | * args->rule contains the matching rule, |
---|
879 | * args->rule.info has additional information. |
---|
880 | * |
---|
881 | */ |
---|
882 | int |
---|
883 | ipfw_chk(struct ip_fw_args *args) |
---|
884 | { |
---|
885 | |
---|
886 | /* |
---|
887 | * Local variables holding state while processing a packet: |
---|
888 | * |
---|
889 | * IMPORTANT NOTE: to speed up the processing of rules, there |
---|
890 | * are some assumption on the values of the variables, which |
---|
891 | * are documented here. Should you change them, please check |
---|
892 | * the implementation of the various instructions to make sure |
---|
893 | * that they still work. |
---|
894 | * |
---|
895 | * args->eh The MAC header. It is non-null for a layer2 |
---|
896 | * packet, it is NULL for a layer-3 packet. |
---|
897 | * **notyet** |
---|
898 | * args->L3offset Offset in the packet to the L3 (IP or equiv.) header. |
---|
899 | * |
---|
900 | * m | args->m Pointer to the mbuf, as received from the caller. |
---|
901 | * It may change if ipfw_chk() does an m_pullup, or if it |
---|
902 | * consumes the packet because it calls send_reject(). |
---|
903 | * XXX This has to change, so that ipfw_chk() never modifies |
---|
904 | * or consumes the buffer. |
---|
905 | * ip is the beginning of the ip(4 or 6) header. |
---|
906 | * Calculated by adding the L3offset to the start of data. |
---|
907 | * (Until we start using L3offset, the packet is |
---|
908 | * supposed to start with the ip header). |
---|
909 | */ |
---|
910 | struct mbuf *m = args->m; |
---|
911 | struct ip *ip = mtod(m, struct ip *); |
---|
912 | |
---|
913 | /* |
---|
914 | * For rules which contain uid/gid or jail constraints, cache |
---|
915 | * a copy of the users credentials after the pcb lookup has been |
---|
916 | * executed. This will speed up the processing of rules with |
---|
917 | * these types of constraints, as well as decrease contention |
---|
918 | * on pcb related locks. |
---|
919 | */ |
---|
920 | #ifndef __FreeBSD__ |
---|
921 | struct bsd_ucred ucred_cache; |
---|
922 | #else |
---|
923 | struct ucred *ucred_cache = NULL; |
---|
924 | #endif |
---|
925 | int ucred_lookup = 0; |
---|
926 | |
---|
927 | /* |
---|
928 | * oif | args->oif If NULL, ipfw_chk has been called on the |
---|
929 | * inbound path (ether_input, ip_input). |
---|
930 | * If non-NULL, ipfw_chk has been called on the outbound path |
---|
931 | * (ether_output, ip_output). |
---|
932 | */ |
---|
933 | struct ifnet *oif = args->oif; |
---|
934 | |
---|
935 | int f_pos = 0; /* index of current rule in the array */ |
---|
936 | int retval = 0; |
---|
937 | |
---|
938 | /* |
---|
939 | * hlen The length of the IP header. |
---|
940 | */ |
---|
941 | u_int hlen = 0; /* hlen >0 means we have an IP pkt */ |
---|
942 | |
---|
943 | /* |
---|
944 | * offset The offset of a fragment. offset != 0 means that |
---|
945 | * we have a fragment at this offset of an IPv4 packet. |
---|
946 | * offset == 0 means that (if this is an IPv4 packet) |
---|
947 | * this is the first or only fragment. |
---|
948 | * For IPv6 offset|ip6f_mf == 0 means there is no Fragment Header |
---|
949 | * or there is a single packet fragment (fragment header added |
---|
950 | * without needed). We will treat a single packet fragment as if |
---|
951 | * there was no fragment header (or log/block depending on the |
---|
952 | * V_fw_permit_single_frag6 sysctl setting). |
---|
953 | */ |
---|
954 | u_short offset = 0; |
---|
955 | u_short ip6f_mf = 0; |
---|
956 | |
---|
957 | /* |
---|
958 | * Local copies of addresses. They are only valid if we have |
---|
959 | * an IP packet. |
---|
960 | * |
---|
961 | * proto The protocol. Set to 0 for non-ip packets, |
---|
962 | * or to the protocol read from the packet otherwise. |
---|
963 | * proto != 0 means that we have an IPv4 packet. |
---|
964 | * |
---|
965 | * src_port, dst_port port numbers, in HOST format. Only |
---|
966 | * valid for TCP and UDP packets. |
---|
967 | * |
---|
968 | * src_ip, dst_ip ip addresses, in NETWORK format. |
---|
969 | * Only valid for IPv4 packets. |
---|
970 | */ |
---|
971 | uint8_t proto; |
---|
972 | uint16_t src_port = 0, dst_port = 0; /* NOTE: host format */ |
---|
973 | struct in_addr src_ip, dst_ip; /* NOTE: network format */ |
---|
974 | uint16_t iplen=0; |
---|
975 | int pktlen; |
---|
976 | uint16_t etype = 0; /* Host order stored ether type */ |
---|
977 | |
---|
978 | /* |
---|
979 | * dyn_dir = MATCH_UNKNOWN when rules unchecked, |
---|
980 | * MATCH_NONE when checked and not matched (q = NULL), |
---|
981 | * MATCH_FORWARD or MATCH_REVERSE otherwise (q != NULL) |
---|
982 | */ |
---|
983 | int dyn_dir = MATCH_UNKNOWN; |
---|
984 | uint16_t dyn_name = 0; |
---|
985 | ipfw_dyn_rule *q = NULL; |
---|
986 | struct ip_fw_chain *chain = &V_layer3_chain; |
---|
987 | |
---|
988 | /* |
---|
989 | * We store in ulp a pointer to the upper layer protocol header. |
---|
990 | * In the ipv4 case this is easy to determine from the header, |
---|
991 | * but for ipv6 we might have some additional headers in the middle. |
---|
992 | * ulp is NULL if not found. |
---|
993 | */ |
---|
994 | void *ulp = NULL; /* upper layer protocol pointer. */ |
---|
995 | |
---|
996 | /* XXX ipv6 variables */ |
---|
997 | int is_ipv6 = 0; |
---|
998 | uint8_t icmp6_type = 0; |
---|
999 | uint16_t ext_hd = 0; /* bits vector for extension header filtering */ |
---|
1000 | /* end of ipv6 variables */ |
---|
1001 | |
---|
1002 | int is_ipv4 = 0; |
---|
1003 | |
---|
1004 | int done = 0; /* flag to exit the outer loop */ |
---|
1005 | |
---|
1006 | if (m->m_flags & M_SKIP_FIREWALL || (! V_ipfw_vnet_ready)) |
---|
1007 | return (IP_FW_PASS); /* accept */ |
---|
1008 | |
---|
1009 | dst_ip.s_addr = 0; /* make sure it is initialized */ |
---|
1010 | src_ip.s_addr = 0; /* make sure it is initialized */ |
---|
1011 | pktlen = m->m_pkthdr.len; |
---|
1012 | args->f_id.fib = M_GETFIB(m); /* note mbuf not altered) */ |
---|
1013 | proto = args->f_id.proto = 0; /* mark f_id invalid */ |
---|
1014 | /* XXX 0 is a valid proto: IP/IPv6 Hop-by-Hop Option */ |
---|
1015 | |
---|
1016 | /* |
---|
1017 | * PULLUP_TO(len, p, T) makes sure that len + sizeof(T) is contiguous, |
---|
1018 | * then it sets p to point at the offset "len" in the mbuf. WARNING: the |
---|
1019 | * pointer might become stale after other pullups (but we never use it |
---|
1020 | * this way). |
---|
1021 | */ |
---|
1022 | #define PULLUP_TO(_len, p, T) PULLUP_LEN(_len, p, sizeof(T)) |
---|
1023 | #define PULLUP_LEN(_len, p, T) \ |
---|
1024 | do { \ |
---|
1025 | int x = (_len) + T; \ |
---|
1026 | if ((m)->m_len < x) { \ |
---|
1027 | args->m = m = m_pullup(m, x); \ |
---|
1028 | if (m == NULL) \ |
---|
1029 | goto pullup_failed; \ |
---|
1030 | } \ |
---|
1031 | p = (mtod(m, char *) + (_len)); \ |
---|
1032 | } while (0) |
---|
1033 | |
---|
1034 | /* |
---|
1035 | * if we have an ether header, |
---|
1036 | */ |
---|
1037 | if (args->eh) |
---|
1038 | etype = ntohs(args->eh->ether_type); |
---|
1039 | |
---|
1040 | /* Identify IP packets and fill up variables. */ |
---|
1041 | if (pktlen >= sizeof(struct ip6_hdr) && |
---|
1042 | (args->eh == NULL || etype == ETHERTYPE_IPV6) && ip->ip_v == 6) { |
---|
1043 | struct ip6_hdr *ip6 = (struct ip6_hdr *)ip; |
---|
1044 | is_ipv6 = 1; |
---|
1045 | args->f_id.addr_type = 6; |
---|
1046 | hlen = sizeof(struct ip6_hdr); |
---|
1047 | proto = ip6->ip6_nxt; |
---|
1048 | |
---|
1049 | /* Search extension headers to find upper layer protocols */ |
---|
1050 | while (ulp == NULL && offset == 0) { |
---|
1051 | switch (proto) { |
---|
1052 | case IPPROTO_ICMPV6: |
---|
1053 | PULLUP_TO(hlen, ulp, struct icmp6_hdr); |
---|
1054 | icmp6_type = ICMP6(ulp)->icmp6_type; |
---|
1055 | break; |
---|
1056 | |
---|
1057 | case IPPROTO_TCP: |
---|
1058 | PULLUP_TO(hlen, ulp, struct tcphdr); |
---|
1059 | dst_port = TCP(ulp)->th_dport; |
---|
1060 | src_port = TCP(ulp)->th_sport; |
---|
1061 | /* save flags for dynamic rules */ |
---|
1062 | args->f_id._flags = TCP(ulp)->th_flags; |
---|
1063 | break; |
---|
1064 | |
---|
1065 | case IPPROTO_SCTP: |
---|
1066 | PULLUP_TO(hlen, ulp, struct sctphdr); |
---|
1067 | src_port = SCTP(ulp)->src_port; |
---|
1068 | dst_port = SCTP(ulp)->dest_port; |
---|
1069 | break; |
---|
1070 | |
---|
1071 | case IPPROTO_UDP: |
---|
1072 | PULLUP_TO(hlen, ulp, struct udphdr); |
---|
1073 | dst_port = UDP(ulp)->uh_dport; |
---|
1074 | src_port = UDP(ulp)->uh_sport; |
---|
1075 | break; |
---|
1076 | |
---|
1077 | case IPPROTO_HOPOPTS: /* RFC 2460 */ |
---|
1078 | PULLUP_TO(hlen, ulp, struct ip6_hbh); |
---|
1079 | ext_hd |= EXT_HOPOPTS; |
---|
1080 | hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; |
---|
1081 | proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; |
---|
1082 | ulp = NULL; |
---|
1083 | break; |
---|
1084 | |
---|
1085 | case IPPROTO_ROUTING: /* RFC 2460 */ |
---|
1086 | PULLUP_TO(hlen, ulp, struct ip6_rthdr); |
---|
1087 | switch (((struct ip6_rthdr *)ulp)->ip6r_type) { |
---|
1088 | case 0: |
---|
1089 | ext_hd |= EXT_RTHDR0; |
---|
1090 | break; |
---|
1091 | case 2: |
---|
1092 | ext_hd |= EXT_RTHDR2; |
---|
1093 | break; |
---|
1094 | default: |
---|
1095 | if (V_fw_verbose) |
---|
1096 | printf("IPFW2: IPV6 - Unknown " |
---|
1097 | "Routing Header type(%d)\n", |
---|
1098 | ((struct ip6_rthdr *) |
---|
1099 | ulp)->ip6r_type); |
---|
1100 | if (V_fw_deny_unknown_exthdrs) |
---|
1101 | return (IP_FW_DENY); |
---|
1102 | break; |
---|
1103 | } |
---|
1104 | ext_hd |= EXT_ROUTING; |
---|
1105 | hlen += (((struct ip6_rthdr *)ulp)->ip6r_len + 1) << 3; |
---|
1106 | proto = ((struct ip6_rthdr *)ulp)->ip6r_nxt; |
---|
1107 | ulp = NULL; |
---|
1108 | break; |
---|
1109 | |
---|
1110 | case IPPROTO_FRAGMENT: /* RFC 2460 */ |
---|
1111 | PULLUP_TO(hlen, ulp, struct ip6_frag); |
---|
1112 | ext_hd |= EXT_FRAGMENT; |
---|
1113 | hlen += sizeof (struct ip6_frag); |
---|
1114 | proto = ((struct ip6_frag *)ulp)->ip6f_nxt; |
---|
1115 | offset = ((struct ip6_frag *)ulp)->ip6f_offlg & |
---|
1116 | IP6F_OFF_MASK; |
---|
1117 | ip6f_mf = ((struct ip6_frag *)ulp)->ip6f_offlg & |
---|
1118 | IP6F_MORE_FRAG; |
---|
1119 | if (V_fw_permit_single_frag6 == 0 && |
---|
1120 | offset == 0 && ip6f_mf == 0) { |
---|
1121 | if (V_fw_verbose) |
---|
1122 | printf("IPFW2: IPV6 - Invalid " |
---|
1123 | "Fragment Header\n"); |
---|
1124 | if (V_fw_deny_unknown_exthdrs) |
---|
1125 | return (IP_FW_DENY); |
---|
1126 | break; |
---|
1127 | } |
---|
1128 | args->f_id.extra = |
---|
1129 | ntohl(((struct ip6_frag *)ulp)->ip6f_ident); |
---|
1130 | ulp = NULL; |
---|
1131 | break; |
---|
1132 | |
---|
1133 | case IPPROTO_DSTOPTS: /* RFC 2460 */ |
---|
1134 | PULLUP_TO(hlen, ulp, struct ip6_hbh); |
---|
1135 | ext_hd |= EXT_DSTOPTS; |
---|
1136 | hlen += (((struct ip6_hbh *)ulp)->ip6h_len + 1) << 3; |
---|
1137 | proto = ((struct ip6_hbh *)ulp)->ip6h_nxt; |
---|
1138 | ulp = NULL; |
---|
1139 | break; |
---|
1140 | |
---|
1141 | case IPPROTO_AH: /* RFC 2402 */ |
---|
1142 | PULLUP_TO(hlen, ulp, struct ip6_ext); |
---|
1143 | ext_hd |= EXT_AH; |
---|
1144 | hlen += (((struct ip6_ext *)ulp)->ip6e_len + 2) << 2; |
---|
1145 | proto = ((struct ip6_ext *)ulp)->ip6e_nxt; |
---|
1146 | ulp = NULL; |
---|
1147 | break; |
---|
1148 | |
---|
1149 | case IPPROTO_ESP: /* RFC 2406 */ |
---|
1150 | PULLUP_TO(hlen, ulp, uint32_t); /* SPI, Seq# */ |
---|
1151 | /* Anything past Seq# is variable length and |
---|
1152 | * data past this ext. header is encrypted. */ |
---|
1153 | ext_hd |= EXT_ESP; |
---|
1154 | break; |
---|
1155 | |
---|
1156 | case IPPROTO_NONE: /* RFC 2460 */ |
---|
1157 | /* |
---|
1158 | * Packet ends here, and IPv6 header has |
---|
1159 | * already been pulled up. If ip6e_len!=0 |
---|
1160 | * then octets must be ignored. |
---|
1161 | */ |
---|
1162 | ulp = ip; /* non-NULL to get out of loop. */ |
---|
1163 | break; |
---|
1164 | |
---|
1165 | case IPPROTO_OSPFIGP: |
---|
1166 | /* XXX OSPF header check? */ |
---|
1167 | PULLUP_TO(hlen, ulp, struct ip6_ext); |
---|
1168 | break; |
---|
1169 | |
---|
1170 | case IPPROTO_PIM: |
---|
1171 | /* XXX PIM header check? */ |
---|
1172 | PULLUP_TO(hlen, ulp, struct pim); |
---|
1173 | break; |
---|
1174 | |
---|
1175 | case IPPROTO_CARP: |
---|
1176 | PULLUP_TO(hlen, ulp, struct carp_header); |
---|
1177 | if (((struct carp_header *)ulp)->carp_version != |
---|
1178 | CARP_VERSION) |
---|
1179 | return (IP_FW_DENY); |
---|
1180 | if (((struct carp_header *)ulp)->carp_type != |
---|
1181 | CARP_ADVERTISEMENT) |
---|
1182 | return (IP_FW_DENY); |
---|
1183 | break; |
---|
1184 | |
---|
1185 | case IPPROTO_IPV6: /* RFC 2893 */ |
---|
1186 | PULLUP_TO(hlen, ulp, struct ip6_hdr); |
---|
1187 | break; |
---|
1188 | |
---|
1189 | case IPPROTO_IPV4: /* RFC 2893 */ |
---|
1190 | PULLUP_TO(hlen, ulp, struct ip); |
---|
1191 | break; |
---|
1192 | |
---|
1193 | default: |
---|
1194 | if (V_fw_verbose) |
---|
1195 | printf("IPFW2: IPV6 - Unknown " |
---|
1196 | "Extension Header(%d), ext_hd=%x\n", |
---|
1197 | proto, ext_hd); |
---|
1198 | if (V_fw_deny_unknown_exthdrs) |
---|
1199 | return (IP_FW_DENY); |
---|
1200 | PULLUP_TO(hlen, ulp, struct ip6_ext); |
---|
1201 | break; |
---|
1202 | } /*switch */ |
---|
1203 | } |
---|
1204 | ip = mtod(m, struct ip *); |
---|
1205 | ip6 = (struct ip6_hdr *)ip; |
---|
1206 | args->f_id.src_ip6 = ip6->ip6_src; |
---|
1207 | args->f_id.dst_ip6 = ip6->ip6_dst; |
---|
1208 | args->f_id.src_ip = 0; |
---|
1209 | args->f_id.dst_ip = 0; |
---|
1210 | args->f_id.flow_id6 = ntohl(ip6->ip6_flow); |
---|
1211 | } else if (pktlen >= sizeof(struct ip) && |
---|
1212 | (args->eh == NULL || etype == ETHERTYPE_IP) && ip->ip_v == 4) { |
---|
1213 | is_ipv4 = 1; |
---|
1214 | hlen = ip->ip_hl << 2; |
---|
1215 | args->f_id.addr_type = 4; |
---|
1216 | |
---|
1217 | /* |
---|
1218 | * Collect parameters into local variables for faster matching. |
---|
1219 | */ |
---|
1220 | proto = ip->ip_p; |
---|
1221 | src_ip = ip->ip_src; |
---|
1222 | dst_ip = ip->ip_dst; |
---|
1223 | offset = ntohs(ip->ip_off) & IP_OFFMASK; |
---|
1224 | iplen = ntohs(ip->ip_len); |
---|
1225 | pktlen = iplen < pktlen ? iplen : pktlen; |
---|
1226 | |
---|
1227 | if (offset == 0) { |
---|
1228 | switch (proto) { |
---|
1229 | case IPPROTO_TCP: |
---|
1230 | PULLUP_TO(hlen, ulp, struct tcphdr); |
---|
1231 | dst_port = TCP(ulp)->th_dport; |
---|
1232 | src_port = TCP(ulp)->th_sport; |
---|
1233 | /* save flags for dynamic rules */ |
---|
1234 | args->f_id._flags = TCP(ulp)->th_flags; |
---|
1235 | break; |
---|
1236 | |
---|
1237 | case IPPROTO_SCTP: |
---|
1238 | PULLUP_TO(hlen, ulp, struct sctphdr); |
---|
1239 | src_port = SCTP(ulp)->src_port; |
---|
1240 | dst_port = SCTP(ulp)->dest_port; |
---|
1241 | break; |
---|
1242 | |
---|
1243 | case IPPROTO_UDP: |
---|
1244 | PULLUP_TO(hlen, ulp, struct udphdr); |
---|
1245 | dst_port = UDP(ulp)->uh_dport; |
---|
1246 | src_port = UDP(ulp)->uh_sport; |
---|
1247 | break; |
---|
1248 | |
---|
1249 | case IPPROTO_ICMP: |
---|
1250 | PULLUP_TO(hlen, ulp, struct icmphdr); |
---|
1251 | //args->f_id.flags = ICMP(ulp)->icmp_type; |
---|
1252 | break; |
---|
1253 | |
---|
1254 | default: |
---|
1255 | break; |
---|
1256 | } |
---|
1257 | } |
---|
1258 | |
---|
1259 | ip = mtod(m, struct ip *); |
---|
1260 | args->f_id.src_ip = ntohl(src_ip.s_addr); |
---|
1261 | args->f_id.dst_ip = ntohl(dst_ip.s_addr); |
---|
1262 | } |
---|
1263 | #undef PULLUP_TO |
---|
1264 | if (proto) { /* we may have port numbers, store them */ |
---|
1265 | args->f_id.proto = proto; |
---|
1266 | args->f_id.src_port = src_port = ntohs(src_port); |
---|
1267 | args->f_id.dst_port = dst_port = ntohs(dst_port); |
---|
1268 | } |
---|
1269 | |
---|
1270 | IPFW_PF_RLOCK(chain); |
---|
1271 | if (! V_ipfw_vnet_ready) { /* shutting down, leave NOW. */ |
---|
1272 | IPFW_PF_RUNLOCK(chain); |
---|
1273 | return (IP_FW_PASS); /* accept */ |
---|
1274 | } |
---|
1275 | if (args->rule.slot) { |
---|
1276 | /* |
---|
1277 | * Packet has already been tagged as a result of a previous |
---|
1278 | * match on rule args->rule aka args->rule_id (PIPE, QUEUE, |
---|
1279 | * REASS, NETGRAPH, DIVERT/TEE...) |
---|
1280 | * Validate the slot and continue from the next one |
---|
1281 | * if still present, otherwise do a lookup. |
---|
1282 | */ |
---|
1283 | f_pos = (args->rule.chain_id == chain->id) ? |
---|
1284 | args->rule.slot : |
---|
1285 | ipfw_find_rule(chain, args->rule.rulenum, |
---|
1286 | args->rule.rule_id); |
---|
1287 | } else { |
---|
1288 | f_pos = 0; |
---|
1289 | } |
---|
1290 | |
---|
1291 | /* |
---|
1292 | * Now scan the rules, and parse microinstructions for each rule. |
---|
1293 | * We have two nested loops and an inner switch. Sometimes we |
---|
1294 | * need to break out of one or both loops, or re-enter one of |
---|
1295 | * the loops with updated variables. Loop variables are: |
---|
1296 | * |
---|
1297 | * f_pos (outer loop) points to the current rule. |
---|
1298 | * On output it points to the matching rule. |
---|
1299 | * done (outer loop) is used as a flag to break the loop. |
---|
1300 | * l (inner loop) residual length of current rule. |
---|
1301 | * cmd points to the current microinstruction. |
---|
1302 | * |
---|
1303 | * We break the inner loop by setting l=0 and possibly |
---|
1304 | * cmdlen=0 if we don't want to advance cmd. |
---|
1305 | * We break the outer loop by setting done=1 |
---|
1306 | * We can restart the inner loop by setting l>0 and f_pos, f, cmd |
---|
1307 | * as needed. |
---|
1308 | */ |
---|
1309 | for (; f_pos < chain->n_rules; f_pos++) { |
---|
1310 | ipfw_insn *cmd; |
---|
1311 | uint32_t tablearg = 0; |
---|
1312 | int l, cmdlen, skip_or; /* skip rest of OR block */ |
---|
1313 | struct ip_fw *f; |
---|
1314 | |
---|
1315 | f = chain->map[f_pos]; |
---|
1316 | if (V_set_disable & (1 << f->set) ) |
---|
1317 | continue; |
---|
1318 | |
---|
1319 | skip_or = 0; |
---|
1320 | for (l = f->cmd_len, cmd = f->cmd ; l > 0 ; |
---|
1321 | l -= cmdlen, cmd += cmdlen) { |
---|
1322 | int match; |
---|
1323 | |
---|
1324 | /* |
---|
1325 | * check_body is a jump target used when we find a |
---|
1326 | * CHECK_STATE, and need to jump to the body of |
---|
1327 | * the target rule. |
---|
1328 | */ |
---|
1329 | |
---|
1330 | /* check_body: */ |
---|
1331 | cmdlen = F_LEN(cmd); |
---|
1332 | /* |
---|
1333 | * An OR block (insn_1 || .. || insn_n) has the |
---|
1334 | * F_OR bit set in all but the last instruction. |
---|
1335 | * The first match will set "skip_or", and cause |
---|
1336 | * the following instructions to be skipped until |
---|
1337 | * past the one with the F_OR bit clear. |
---|
1338 | */ |
---|
1339 | if (skip_or) { /* skip this instruction */ |
---|
1340 | if ((cmd->len & F_OR) == 0) |
---|
1341 | skip_or = 0; /* next one is good */ |
---|
1342 | continue; |
---|
1343 | } |
---|
1344 | match = 0; /* set to 1 if we succeed */ |
---|
1345 | |
---|
1346 | switch (cmd->opcode) { |
---|
1347 | /* |
---|
1348 | * The first set of opcodes compares the packet's |
---|
1349 | * fields with some pattern, setting 'match' if a |
---|
1350 | * match is found. At the end of the loop there is |
---|
1351 | * logic to deal with F_NOT and F_OR flags associated |
---|
1352 | * with the opcode. |
---|
1353 | */ |
---|
1354 | case O_NOP: |
---|
1355 | match = 1; |
---|
1356 | break; |
---|
1357 | |
---|
1358 | case O_FORWARD_MAC: |
---|
1359 | printf("ipfw: opcode %d unimplemented\n", |
---|
1360 | cmd->opcode); |
---|
1361 | break; |
---|
1362 | |
---|
1363 | case O_GID: |
---|
1364 | case O_UID: |
---|
1365 | case O_JAIL: |
---|
1366 | /* |
---|
1367 | * We only check offset == 0 && proto != 0, |
---|
1368 | * as this ensures that we have a |
---|
1369 | * packet with the ports info. |
---|
1370 | */ |
---|
1371 | if (offset != 0) |
---|
1372 | break; |
---|
1373 | if (proto == IPPROTO_TCP || |
---|
1374 | proto == IPPROTO_UDP) |
---|
1375 | match = check_uidgid( |
---|
1376 | (ipfw_insn_u32 *)cmd, |
---|
1377 | args, &ucred_lookup, |
---|
1378 | #ifdef __FreeBSD__ |
---|
1379 | &ucred_cache); |
---|
1380 | #else |
---|
1381 | (void *)&ucred_cache); |
---|
1382 | #endif |
---|
1383 | break; |
---|
1384 | |
---|
1385 | case O_RECV: |
---|
1386 | match = iface_match(m->m_pkthdr.rcvif, |
---|
1387 | (ipfw_insn_if *)cmd, chain, &tablearg); |
---|
1388 | break; |
---|
1389 | |
---|
1390 | case O_XMIT: |
---|
1391 | match = iface_match(oif, (ipfw_insn_if *)cmd, |
---|
1392 | chain, &tablearg); |
---|
1393 | break; |
---|
1394 | |
---|
1395 | case O_VIA: |
---|
1396 | match = iface_match(oif ? oif : |
---|
1397 | m->m_pkthdr.rcvif, (ipfw_insn_if *)cmd, |
---|
1398 | chain, &tablearg); |
---|
1399 | break; |
---|
1400 | |
---|
1401 | case O_MACADDR2: |
---|
1402 | if (args->eh != NULL) { /* have MAC header */ |
---|
1403 | u_int32_t *want = (u_int32_t *) |
---|
1404 | ((ipfw_insn_mac *)cmd)->addr; |
---|
1405 | u_int32_t *mask = (u_int32_t *) |
---|
1406 | ((ipfw_insn_mac *)cmd)->mask; |
---|
1407 | u_int32_t *hdr = (u_int32_t *)args->eh; |
---|
1408 | |
---|
1409 | match = |
---|
1410 | ( want[0] == (hdr[0] & mask[0]) && |
---|
1411 | want[1] == (hdr[1] & mask[1]) && |
---|
1412 | want[2] == (hdr[2] & mask[2]) ); |
---|
1413 | } |
---|
1414 | break; |
---|
1415 | |
---|
1416 | case O_MAC_TYPE: |
---|
1417 | if (args->eh != NULL) { |
---|
1418 | u_int16_t *p = |
---|
1419 | ((ipfw_insn_u16 *)cmd)->ports; |
---|
1420 | int i; |
---|
1421 | |
---|
1422 | for (i = cmdlen - 1; !match && i>0; |
---|
1423 | i--, p += 2) |
---|
1424 | match = (etype >= p[0] && |
---|
1425 | etype <= p[1]); |
---|
1426 | } |
---|
1427 | break; |
---|
1428 | |
---|
1429 | case O_FRAG: |
---|
1430 | match = (offset != 0); |
---|
1431 | break; |
---|
1432 | |
---|
1433 | case O_IN: /* "out" is "not in" */ |
---|
1434 | match = (oif == NULL); |
---|
1435 | break; |
---|
1436 | |
---|
1437 | case O_LAYER2: |
---|
1438 | match = (args->eh != NULL); |
---|
1439 | break; |
---|
1440 | |
---|
1441 | case O_DIVERTED: |
---|
1442 | { |
---|
1443 | /* For diverted packets, args->rule.info |
---|
1444 | * contains the divert port (in host format) |
---|
1445 | * reason and direction. |
---|
1446 | */ |
---|
1447 | uint32_t i = args->rule.info; |
---|
1448 | match = (i&IPFW_IS_MASK) == IPFW_IS_DIVERT && |
---|
1449 | cmd->arg1 & ((i & IPFW_INFO_IN) ? 1 : 2); |
---|
1450 | } |
---|
1451 | break; |
---|
1452 | |
---|
1453 | case O_PROTO: |
---|
1454 | /* |
---|
1455 | * We do not allow an arg of 0 so the |
---|
1456 | * check of "proto" only suffices. |
---|
1457 | */ |
---|
1458 | match = (proto == cmd->arg1); |
---|
1459 | break; |
---|
1460 | |
---|
1461 | case O_IP_SRC: |
---|
1462 | match = is_ipv4 && |
---|
1463 | (((ipfw_insn_ip *)cmd)->addr.s_addr == |
---|
1464 | src_ip.s_addr); |
---|
1465 | break; |
---|
1466 | |
---|
1467 | case O_IP_DST_LOOKUP: |
---|
1468 | { |
---|
1469 | void *pkey; |
---|
1470 | uint32_t vidx, key; |
---|
1471 | uint16_t keylen; |
---|
1472 | |
---|
1473 | if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) { |
---|
1474 | /* Determine lookup key type */ |
---|
1475 | vidx = ((ipfw_insn_u32 *)cmd)->d[1]; |
---|
1476 | if (vidx != 4 /* uid */ && |
---|
1477 | vidx != 5 /* jail */ && |
---|
1478 | is_ipv6 == 0 && is_ipv4 == 0) |
---|
1479 | break; |
---|
1480 | /* Determine key length */ |
---|
1481 | if (vidx == 0 /* dst-ip */ || |
---|
1482 | vidx == 1 /* src-ip */) |
---|
1483 | keylen = is_ipv6 ? |
---|
1484 | sizeof(struct in6_addr): |
---|
1485 | sizeof(in_addr_t); |
---|
1486 | else { |
---|
1487 | keylen = sizeof(key); |
---|
1488 | pkey = &key; |
---|
1489 | } |
---|
1490 | if (vidx == 0 /* dst-ip */) |
---|
1491 | pkey = is_ipv4 ? (void *)&dst_ip: |
---|
1492 | (void *)&args->f_id.dst_ip6; |
---|
1493 | else if (vidx == 1 /* src-ip */) |
---|
1494 | pkey = is_ipv4 ? (void *)&src_ip: |
---|
1495 | (void *)&args->f_id.src_ip6; |
---|
1496 | else if (vidx == 6 /* dscp */) { |
---|
1497 | if (is_ipv4) |
---|
1498 | key = ip->ip_tos >> 2; |
---|
1499 | else { |
---|
1500 | key = args->f_id.flow_id6; |
---|
1501 | key = (key & 0x0f) << 2 | |
---|
1502 | (key & 0xf000) >> 14; |
---|
1503 | } |
---|
1504 | key &= 0x3f; |
---|
1505 | } else if (vidx == 2 /* dst-port */ || |
---|
1506 | vidx == 3 /* src-port */) { |
---|
1507 | /* Skip fragments */ |
---|
1508 | if (offset != 0) |
---|
1509 | break; |
---|
1510 | /* Skip proto without ports */ |
---|
1511 | if (proto != IPPROTO_TCP && |
---|
1512 | proto != IPPROTO_UDP && |
---|
1513 | proto != IPPROTO_SCTP) |
---|
1514 | break; |
---|
1515 | if (vidx == 2 /* dst-port */) |
---|
1516 | key = dst_port; |
---|
1517 | else |
---|
1518 | key = src_port; |
---|
1519 | } |
---|
1520 | #ifndef USERSPACE |
---|
1521 | else if (vidx == 4 /* uid */ || |
---|
1522 | vidx == 5 /* jail */) { |
---|
1523 | check_uidgid( |
---|
1524 | (ipfw_insn_u32 *)cmd, |
---|
1525 | args, &ucred_lookup, |
---|
1526 | #ifdef __FreeBSD__ |
---|
1527 | &ucred_cache); |
---|
1528 | if (vidx == 4 /* uid */) |
---|
1529 | #ifndef __rtems__ |
---|
1530 | key = ucred_cache->cr_uid; |
---|
1531 | #else /* __rtems__ */ |
---|
1532 | key = BSD_DEFAULT_UID; |
---|
1533 | #endif /* __rtems__ */ |
---|
1534 | else if (vidx == 5 /* jail */) |
---|
1535 | #ifndef __rtems__ |
---|
1536 | key = ucred_cache->cr_prison->pr_id; |
---|
1537 | #else /* __rtems__ */ |
---|
1538 | key = BSD_DEFAULT_PRISON->pr_id; |
---|
1539 | #endif /* __rtems__ */ |
---|
1540 | #else /* !__FreeBSD__ */ |
---|
1541 | (void *)&ucred_cache); |
---|
1542 | if (vidx == 4 /* uid */) |
---|
1543 | key = ucred_cache.uid; |
---|
1544 | else if (vidx == 5 /* jail */) |
---|
1545 | key = ucred_cache.xid; |
---|
1546 | #endif /* !__FreeBSD__ */ |
---|
1547 | } |
---|
1548 | #endif /* !USERSPACE */ |
---|
1549 | else |
---|
1550 | break; |
---|
1551 | match = ipfw_lookup_table(chain, |
---|
1552 | cmd->arg1, keylen, pkey, &vidx); |
---|
1553 | if (!match) |
---|
1554 | break; |
---|
1555 | tablearg = vidx; |
---|
1556 | break; |
---|
1557 | } |
---|
1558 | /* cmdlen =< F_INSN_SIZE(ipfw_insn_u32) */ |
---|
1559 | /* FALLTHROUGH */ |
---|
1560 | } |
---|
1561 | case O_IP_SRC_LOOKUP: |
---|
1562 | { |
---|
1563 | void *pkey; |
---|
1564 | uint32_t vidx; |
---|
1565 | uint16_t keylen; |
---|
1566 | |
---|
1567 | if (is_ipv4) { |
---|
1568 | keylen = sizeof(in_addr_t); |
---|
1569 | if (cmd->opcode == O_IP_DST_LOOKUP) |
---|
1570 | pkey = &dst_ip; |
---|
1571 | else |
---|
1572 | pkey = &src_ip; |
---|
1573 | } else if (is_ipv6) { |
---|
1574 | keylen = sizeof(struct in6_addr); |
---|
1575 | if (cmd->opcode == O_IP_DST_LOOKUP) |
---|
1576 | pkey = &args->f_id.dst_ip6; |
---|
1577 | else |
---|
1578 | pkey = &args->f_id.src_ip6; |
---|
1579 | } else |
---|
1580 | break; |
---|
1581 | match = ipfw_lookup_table(chain, cmd->arg1, |
---|
1582 | keylen, pkey, &vidx); |
---|
1583 | if (!match) |
---|
1584 | break; |
---|
1585 | if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) { |
---|
1586 | match = ((ipfw_insn_u32 *)cmd)->d[0] == |
---|
1587 | TARG_VAL(chain, vidx, tag); |
---|
1588 | if (!match) |
---|
1589 | break; |
---|
1590 | } |
---|
1591 | tablearg = vidx; |
---|
1592 | break; |
---|
1593 | } |
---|
1594 | |
---|
1595 | case O_IP_FLOW_LOOKUP: |
---|
1596 | { |
---|
1597 | uint32_t v = 0; |
---|
1598 | match = ipfw_lookup_table(chain, |
---|
1599 | cmd->arg1, 0, &args->f_id, &v); |
---|
1600 | if (cmdlen == F_INSN_SIZE(ipfw_insn_u32)) |
---|
1601 | match = ((ipfw_insn_u32 *)cmd)->d[0] == |
---|
1602 | TARG_VAL(chain, v, tag); |
---|
1603 | if (match) |
---|
1604 | tablearg = v; |
---|
1605 | } |
---|
1606 | break; |
---|
1607 | case O_IP_SRC_MASK: |
---|
1608 | case O_IP_DST_MASK: |
---|
1609 | if (is_ipv4) { |
---|
1610 | uint32_t a = |
---|
1611 | (cmd->opcode == O_IP_DST_MASK) ? |
---|
1612 | dst_ip.s_addr : src_ip.s_addr; |
---|
1613 | uint32_t *p = ((ipfw_insn_u32 *)cmd)->d; |
---|
1614 | int i = cmdlen-1; |
---|
1615 | |
---|
1616 | for (; !match && i>0; i-= 2, p+= 2) |
---|
1617 | match = (p[0] == (a & p[1])); |
---|
1618 | } |
---|
1619 | break; |
---|
1620 | |
---|
1621 | case O_IP_SRC_ME: |
---|
1622 | if (is_ipv4) { |
---|
1623 | struct ifnet *tif; |
---|
1624 | |
---|
1625 | INADDR_TO_IFP(src_ip, tif); |
---|
1626 | match = (tif != NULL); |
---|
1627 | break; |
---|
1628 | } |
---|
1629 | #ifdef INET6 |
---|
1630 | /* FALLTHROUGH */ |
---|
1631 | case O_IP6_SRC_ME: |
---|
1632 | match= is_ipv6 && ipfw_localip6(&args->f_id.src_ip6); |
---|
1633 | #endif |
---|
1634 | break; |
---|
1635 | |
---|
1636 | case O_IP_DST_SET: |
---|
1637 | case O_IP_SRC_SET: |
---|
1638 | if (is_ipv4) { |
---|
1639 | u_int32_t *d = (u_int32_t *)(cmd+1); |
---|
1640 | u_int32_t addr = |
---|
1641 | cmd->opcode == O_IP_DST_SET ? |
---|
1642 | args->f_id.dst_ip : |
---|
1643 | args->f_id.src_ip; |
---|
1644 | |
---|
1645 | if (addr < d[0]) |
---|
1646 | break; |
---|
1647 | addr -= d[0]; /* subtract base */ |
---|
1648 | match = (addr < cmd->arg1) && |
---|
1649 | ( d[ 1 + (addr>>5)] & |
---|
1650 | (1<<(addr & 0x1f)) ); |
---|
1651 | } |
---|
1652 | break; |
---|
1653 | |
---|
1654 | case O_IP_DST: |
---|
1655 | match = is_ipv4 && |
---|
1656 | (((ipfw_insn_ip *)cmd)->addr.s_addr == |
---|
1657 | dst_ip.s_addr); |
---|
1658 | break; |
---|
1659 | |
---|
1660 | case O_IP_DST_ME: |
---|
1661 | if (is_ipv4) { |
---|
1662 | struct ifnet *tif; |
---|
1663 | |
---|
1664 | INADDR_TO_IFP(dst_ip, tif); |
---|
1665 | match = (tif != NULL); |
---|
1666 | break; |
---|
1667 | } |
---|
1668 | #ifdef INET6 |
---|
1669 | /* FALLTHROUGH */ |
---|
1670 | case O_IP6_DST_ME: |
---|
1671 | match= is_ipv6 && ipfw_localip6(&args->f_id.dst_ip6); |
---|
1672 | #endif |
---|
1673 | break; |
---|
1674 | |
---|
1675 | |
---|
1676 | case O_IP_SRCPORT: |
---|
1677 | case O_IP_DSTPORT: |
---|
1678 | /* |
---|
1679 | * offset == 0 && proto != 0 is enough |
---|
1680 | * to guarantee that we have a |
---|
1681 | * packet with port info. |
---|
1682 | */ |
---|
1683 | if ((proto==IPPROTO_UDP || proto==IPPROTO_TCP) |
---|
1684 | && offset == 0) { |
---|
1685 | u_int16_t x = |
---|
1686 | (cmd->opcode == O_IP_SRCPORT) ? |
---|
1687 | src_port : dst_port ; |
---|
1688 | u_int16_t *p = |
---|
1689 | ((ipfw_insn_u16 *)cmd)->ports; |
---|
1690 | int i; |
---|
1691 | |
---|
1692 | for (i = cmdlen - 1; !match && i>0; |
---|
1693 | i--, p += 2) |
---|
1694 | match = (x>=p[0] && x<=p[1]); |
---|
1695 | } |
---|
1696 | break; |
---|
1697 | |
---|
1698 | case O_ICMPTYPE: |
---|
1699 | match = (offset == 0 && proto==IPPROTO_ICMP && |
---|
1700 | icmptype_match(ICMP(ulp), (ipfw_insn_u32 *)cmd) ); |
---|
1701 | break; |
---|
1702 | |
---|
1703 | #ifdef INET6 |
---|
1704 | case O_ICMP6TYPE: |
---|
1705 | match = is_ipv6 && offset == 0 && |
---|
1706 | proto==IPPROTO_ICMPV6 && |
---|
1707 | icmp6type_match( |
---|
1708 | ICMP6(ulp)->icmp6_type, |
---|
1709 | (ipfw_insn_u32 *)cmd); |
---|
1710 | break; |
---|
1711 | #endif /* INET6 */ |
---|
1712 | |
---|
1713 | case O_IPOPT: |
---|
1714 | match = (is_ipv4 && |
---|
1715 | ipopts_match(ip, cmd) ); |
---|
1716 | break; |
---|
1717 | |
---|
1718 | case O_IPVER: |
---|
1719 | match = (is_ipv4 && |
---|
1720 | cmd->arg1 == ip->ip_v); |
---|
1721 | break; |
---|
1722 | |
---|
1723 | case O_IPID: |
---|
1724 | case O_IPLEN: |
---|
1725 | case O_IPTTL: |
---|
1726 | if (is_ipv4) { /* only for IP packets */ |
---|
1727 | uint16_t x; |
---|
1728 | uint16_t *p; |
---|
1729 | int i; |
---|
1730 | |
---|
1731 | if (cmd->opcode == O_IPLEN) |
---|
1732 | x = iplen; |
---|
1733 | else if (cmd->opcode == O_IPTTL) |
---|
1734 | x = ip->ip_ttl; |
---|
1735 | else /* must be IPID */ |
---|
1736 | x = ntohs(ip->ip_id); |
---|
1737 | if (cmdlen == 1) { |
---|
1738 | match = (cmd->arg1 == x); |
---|
1739 | break; |
---|
1740 | } |
---|
1741 | /* otherwise we have ranges */ |
---|
1742 | p = ((ipfw_insn_u16 *)cmd)->ports; |
---|
1743 | i = cmdlen - 1; |
---|
1744 | for (; !match && i>0; i--, p += 2) |
---|
1745 | match = (x >= p[0] && x <= p[1]); |
---|
1746 | } |
---|
1747 | break; |
---|
1748 | |
---|
1749 | case O_IPPRECEDENCE: |
---|
1750 | match = (is_ipv4 && |
---|
1751 | (cmd->arg1 == (ip->ip_tos & 0xe0)) ); |
---|
1752 | break; |
---|
1753 | |
---|
1754 | case O_IPTOS: |
---|
1755 | match = (is_ipv4 && |
---|
1756 | flags_match(cmd, ip->ip_tos)); |
---|
1757 | break; |
---|
1758 | |
---|
1759 | case O_DSCP: |
---|
1760 | { |
---|
1761 | uint32_t *p; |
---|
1762 | uint16_t x; |
---|
1763 | |
---|
1764 | p = ((ipfw_insn_u32 *)cmd)->d; |
---|
1765 | |
---|
1766 | if (is_ipv4) |
---|
1767 | x = ip->ip_tos >> 2; |
---|
1768 | else if (is_ipv6) { |
---|
1769 | uint8_t *v; |
---|
1770 | v = &((struct ip6_hdr *)ip)->ip6_vfc; |
---|
1771 | x = (*v & 0x0F) << 2; |
---|
1772 | v++; |
---|
1773 | x |= *v >> 6; |
---|
1774 | } else |
---|
1775 | break; |
---|
1776 | |
---|
1777 | /* DSCP bitmask is stored as low_u32 high_u32 */ |
---|
1778 | if (x >= 32) |
---|
1779 | match = *(p + 1) & (1 << (x - 32)); |
---|
1780 | else |
---|
1781 | match = *p & (1 << x); |
---|
1782 | } |
---|
1783 | break; |
---|
1784 | |
---|
1785 | case O_TCPDATALEN: |
---|
1786 | if (proto == IPPROTO_TCP && offset == 0) { |
---|
1787 | struct tcphdr *tcp; |
---|
1788 | uint16_t x; |
---|
1789 | uint16_t *p; |
---|
1790 | int i; |
---|
1791 | |
---|
1792 | tcp = TCP(ulp); |
---|
1793 | x = iplen - |
---|
1794 | ((ip->ip_hl + tcp->th_off) << 2); |
---|
1795 | if (cmdlen == 1) { |
---|
1796 | match = (cmd->arg1 == x); |
---|
1797 | break; |
---|
1798 | } |
---|
1799 | /* otherwise we have ranges */ |
---|
1800 | p = ((ipfw_insn_u16 *)cmd)->ports; |
---|
1801 | i = cmdlen - 1; |
---|
1802 | for (; !match && i>0; i--, p += 2) |
---|
1803 | match = (x >= p[0] && x <= p[1]); |
---|
1804 | } |
---|
1805 | break; |
---|
1806 | |
---|
1807 | case O_TCPFLAGS: |
---|
1808 | match = (proto == IPPROTO_TCP && offset == 0 && |
---|
1809 | flags_match(cmd, TCP(ulp)->th_flags)); |
---|
1810 | break; |
---|
1811 | |
---|
1812 | case O_TCPOPTS: |
---|
1813 | if (proto == IPPROTO_TCP && offset == 0 && ulp){ |
---|
1814 | PULLUP_LEN(hlen, ulp, |
---|
1815 | (TCP(ulp)->th_off << 2)); |
---|
1816 | match = tcpopts_match(TCP(ulp), cmd); |
---|
1817 | } |
---|
1818 | break; |
---|
1819 | |
---|
1820 | case O_TCPSEQ: |
---|
1821 | match = (proto == IPPROTO_TCP && offset == 0 && |
---|
1822 | ((ipfw_insn_u32 *)cmd)->d[0] == |
---|
1823 | TCP(ulp)->th_seq); |
---|
1824 | break; |
---|
1825 | |
---|
1826 | case O_TCPACK: |
---|
1827 | match = (proto == IPPROTO_TCP && offset == 0 && |
---|
1828 | ((ipfw_insn_u32 *)cmd)->d[0] == |
---|
1829 | TCP(ulp)->th_ack); |
---|
1830 | break; |
---|
1831 | |
---|
1832 | case O_TCPWIN: |
---|
1833 | if (proto == IPPROTO_TCP && offset == 0) { |
---|
1834 | uint16_t x; |
---|
1835 | uint16_t *p; |
---|
1836 | int i; |
---|
1837 | |
---|
1838 | x = ntohs(TCP(ulp)->th_win); |
---|
1839 | if (cmdlen == 1) { |
---|
1840 | match = (cmd->arg1 == x); |
---|
1841 | break; |
---|
1842 | } |
---|
1843 | /* Otherwise we have ranges. */ |
---|
1844 | p = ((ipfw_insn_u16 *)cmd)->ports; |
---|
1845 | i = cmdlen - 1; |
---|
1846 | for (; !match && i > 0; i--, p += 2) |
---|
1847 | match = (x >= p[0] && x <= p[1]); |
---|
1848 | } |
---|
1849 | break; |
---|
1850 | |
---|
1851 | case O_ESTAB: |
---|
1852 | /* reject packets which have SYN only */ |
---|
1853 | /* XXX should i also check for TH_ACK ? */ |
---|
1854 | match = (proto == IPPROTO_TCP && offset == 0 && |
---|
1855 | (TCP(ulp)->th_flags & |
---|
1856 | (TH_RST | TH_ACK | TH_SYN)) != TH_SYN); |
---|
1857 | break; |
---|
1858 | |
---|
1859 | case O_ALTQ: { |
---|
1860 | struct pf_mtag *at; |
---|
1861 | struct m_tag *mtag; |
---|
1862 | ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd; |
---|
1863 | |
---|
1864 | /* |
---|
1865 | * ALTQ uses mbuf tags from another |
---|
1866 | * packet filtering system - pf(4). |
---|
1867 | * We allocate a tag in its format |
---|
1868 | * and fill it in, pretending to be pf(4). |
---|
1869 | */ |
---|
1870 | match = 1; |
---|
1871 | at = pf_find_mtag(m); |
---|
1872 | if (at != NULL && at->qid != 0) |
---|
1873 | break; |
---|
1874 | mtag = m_tag_get(PACKET_TAG_PF, |
---|
1875 | sizeof(struct pf_mtag), M_NOWAIT | M_ZERO); |
---|
1876 | if (mtag == NULL) { |
---|
1877 | /* |
---|
1878 | * Let the packet fall back to the |
---|
1879 | * default ALTQ. |
---|
1880 | */ |
---|
1881 | break; |
---|
1882 | } |
---|
1883 | m_tag_prepend(m, mtag); |
---|
1884 | at = (struct pf_mtag *)(mtag + 1); |
---|
1885 | at->qid = altq->qid; |
---|
1886 | at->hdr = ip; |
---|
1887 | break; |
---|
1888 | } |
---|
1889 | |
---|
1890 | case O_LOG: |
---|
1891 | ipfw_log(chain, f, hlen, args, m, |
---|
1892 | oif, offset | ip6f_mf, tablearg, ip); |
---|
1893 | match = 1; |
---|
1894 | break; |
---|
1895 | |
---|
1896 | case O_PROB: |
---|
1897 | match = (random()<((ipfw_insn_u32 *)cmd)->d[0]); |
---|
1898 | break; |
---|
1899 | |
---|
1900 | case O_VERREVPATH: |
---|
1901 | /* Outgoing packets automatically pass/match */ |
---|
1902 | match = ((oif != NULL) || |
---|
1903 | (m->m_pkthdr.rcvif == NULL) || |
---|
1904 | ( |
---|
1905 | #ifdef INET6 |
---|
1906 | is_ipv6 ? |
---|
1907 | verify_path6(&(args->f_id.src_ip6), |
---|
1908 | m->m_pkthdr.rcvif, args->f_id.fib) : |
---|
1909 | #endif |
---|
1910 | verify_path(src_ip, m->m_pkthdr.rcvif, |
---|
1911 | args->f_id.fib))); |
---|
1912 | break; |
---|
1913 | |
---|
1914 | case O_VERSRCREACH: |
---|
1915 | /* Outgoing packets automatically pass/match */ |
---|
1916 | match = (hlen > 0 && ((oif != NULL) || |
---|
1917 | #ifdef INET6 |
---|
1918 | is_ipv6 ? |
---|
1919 | verify_path6(&(args->f_id.src_ip6), |
---|
1920 | NULL, args->f_id.fib) : |
---|
1921 | #endif |
---|
1922 | verify_path(src_ip, NULL, args->f_id.fib))); |
---|
1923 | break; |
---|
1924 | |
---|
1925 | case O_ANTISPOOF: |
---|
1926 | /* Outgoing packets automatically pass/match */ |
---|
1927 | if (oif == NULL && hlen > 0 && |
---|
1928 | ( (is_ipv4 && in_localaddr(src_ip)) |
---|
1929 | #ifdef INET6 |
---|
1930 | || (is_ipv6 && |
---|
1931 | in6_localaddr(&(args->f_id.src_ip6))) |
---|
1932 | #endif |
---|
1933 | )) |
---|
1934 | match = |
---|
1935 | #ifdef INET6 |
---|
1936 | is_ipv6 ? verify_path6( |
---|
1937 | &(args->f_id.src_ip6), |
---|
1938 | m->m_pkthdr.rcvif, |
---|
1939 | args->f_id.fib) : |
---|
1940 | #endif |
---|
1941 | verify_path(src_ip, |
---|
1942 | m->m_pkthdr.rcvif, |
---|
1943 | args->f_id.fib); |
---|
1944 | else |
---|
1945 | match = 1; |
---|
1946 | break; |
---|
1947 | |
---|
1948 | case O_IPSEC: |
---|
1949 | #ifdef IPSEC |
---|
1950 | match = (m_tag_find(m, |
---|
1951 | PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL); |
---|
1952 | #endif |
---|
1953 | /* otherwise no match */ |
---|
1954 | break; |
---|
1955 | |
---|
1956 | #ifdef INET6 |
---|
1957 | case O_IP6_SRC: |
---|
1958 | match = is_ipv6 && |
---|
1959 | IN6_ARE_ADDR_EQUAL(&args->f_id.src_ip6, |
---|
1960 | &((ipfw_insn_ip6 *)cmd)->addr6); |
---|
1961 | break; |
---|
1962 | |
---|
1963 | case O_IP6_DST: |
---|
1964 | match = is_ipv6 && |
---|
1965 | IN6_ARE_ADDR_EQUAL(&args->f_id.dst_ip6, |
---|
1966 | &((ipfw_insn_ip6 *)cmd)->addr6); |
---|
1967 | break; |
---|
1968 | case O_IP6_SRC_MASK: |
---|
1969 | case O_IP6_DST_MASK: |
---|
1970 | if (is_ipv6) { |
---|
1971 | int i = cmdlen - 1; |
---|
1972 | struct in6_addr p; |
---|
1973 | struct in6_addr *d = |
---|
1974 | &((ipfw_insn_ip6 *)cmd)->addr6; |
---|
1975 | |
---|
1976 | for (; !match && i > 0; d += 2, |
---|
1977 | i -= F_INSN_SIZE(struct in6_addr) |
---|
1978 | * 2) { |
---|
1979 | p = (cmd->opcode == |
---|
1980 | O_IP6_SRC_MASK) ? |
---|
1981 | args->f_id.src_ip6: |
---|
1982 | args->f_id.dst_ip6; |
---|
1983 | APPLY_MASK(&p, &d[1]); |
---|
1984 | match = |
---|
1985 | IN6_ARE_ADDR_EQUAL(&d[0], |
---|
1986 | &p); |
---|
1987 | } |
---|
1988 | } |
---|
1989 | break; |
---|
1990 | |
---|
1991 | case O_FLOW6ID: |
---|
1992 | match = is_ipv6 && |
---|
1993 | flow6id_match(args->f_id.flow_id6, |
---|
1994 | (ipfw_insn_u32 *) cmd); |
---|
1995 | break; |
---|
1996 | |
---|
1997 | case O_EXT_HDR: |
---|
1998 | match = is_ipv6 && |
---|
1999 | (ext_hd & ((ipfw_insn *) cmd)->arg1); |
---|
2000 | break; |
---|
2001 | |
---|
2002 | case O_IP6: |
---|
2003 | match = is_ipv6; |
---|
2004 | break; |
---|
2005 | #endif |
---|
2006 | |
---|
2007 | case O_IP4: |
---|
2008 | match = is_ipv4; |
---|
2009 | break; |
---|
2010 | |
---|
2011 | case O_TAG: { |
---|
2012 | struct m_tag *mtag; |
---|
2013 | uint32_t tag = TARG(cmd->arg1, tag); |
---|
2014 | |
---|
2015 | /* Packet is already tagged with this tag? */ |
---|
2016 | mtag = m_tag_locate(m, MTAG_IPFW, tag, NULL); |
---|
2017 | |
---|
2018 | /* We have `untag' action when F_NOT flag is |
---|
2019 | * present. And we must remove this mtag from |
---|
2020 | * mbuf and reset `match' to zero (`match' will |
---|
2021 | * be inversed later). |
---|
2022 | * Otherwise we should allocate new mtag and |
---|
2023 | * push it into mbuf. |
---|
2024 | */ |
---|
2025 | if (cmd->len & F_NOT) { /* `untag' action */ |
---|
2026 | if (mtag != NULL) |
---|
2027 | m_tag_delete(m, mtag); |
---|
2028 | match = 0; |
---|
2029 | } else { |
---|
2030 | if (mtag == NULL) { |
---|
2031 | mtag = m_tag_alloc( MTAG_IPFW, |
---|
2032 | tag, 0, M_NOWAIT); |
---|
2033 | if (mtag != NULL) |
---|
2034 | m_tag_prepend(m, mtag); |
---|
2035 | } |
---|
2036 | match = 1; |
---|
2037 | } |
---|
2038 | break; |
---|
2039 | } |
---|
2040 | |
---|
2041 | case O_FIB: /* try match the specified fib */ |
---|
2042 | if (args->f_id.fib == cmd->arg1) |
---|
2043 | match = 1; |
---|
2044 | break; |
---|
2045 | |
---|
2046 | case O_SOCKARG: { |
---|
2047 | #ifndef USERSPACE /* not supported in userspace */ |
---|
2048 | struct inpcb *inp = args->inp; |
---|
2049 | struct inpcbinfo *pi; |
---|
2050 | |
---|
2051 | if (is_ipv6) /* XXX can we remove this ? */ |
---|
2052 | break; |
---|
2053 | |
---|
2054 | if (proto == IPPROTO_TCP) |
---|
2055 | pi = &V_tcbinfo; |
---|
2056 | else if (proto == IPPROTO_UDP) |
---|
2057 | pi = &V_udbinfo; |
---|
2058 | else |
---|
2059 | break; |
---|
2060 | |
---|
2061 | /* |
---|
2062 | * XXXRW: so_user_cookie should almost |
---|
2063 | * certainly be inp_user_cookie? |
---|
2064 | */ |
---|
2065 | |
---|
2066 | /* For incoming packet, lookup up the |
---|
2067 | inpcb using the src/dest ip/port tuple */ |
---|
2068 | if (inp == NULL) { |
---|
2069 | inp = in_pcblookup(pi, |
---|
2070 | src_ip, htons(src_port), |
---|
2071 | dst_ip, htons(dst_port), |
---|
2072 | INPLOOKUP_RLOCKPCB, NULL); |
---|
2073 | if (inp != NULL) { |
---|
2074 | tablearg = |
---|
2075 | inp->inp_socket->so_user_cookie; |
---|
2076 | if (tablearg) |
---|
2077 | match = 1; |
---|
2078 | INP_RUNLOCK(inp); |
---|
2079 | } |
---|
2080 | } else { |
---|
2081 | if (inp->inp_socket) { |
---|
2082 | tablearg = |
---|
2083 | inp->inp_socket->so_user_cookie; |
---|
2084 | if (tablearg) |
---|
2085 | match = 1; |
---|
2086 | } |
---|
2087 | } |
---|
2088 | #endif /* !USERSPACE */ |
---|
2089 | break; |
---|
2090 | } |
---|
2091 | |
---|
2092 | case O_TAGGED: { |
---|
2093 | struct m_tag *mtag; |
---|
2094 | uint32_t tag = TARG(cmd->arg1, tag); |
---|
2095 | |
---|
2096 | if (cmdlen == 1) { |
---|
2097 | match = m_tag_locate(m, MTAG_IPFW, |
---|
2098 | tag, NULL) != NULL; |
---|
2099 | break; |
---|
2100 | } |
---|
2101 | |
---|
2102 | /* we have ranges */ |
---|
2103 | for (mtag = m_tag_first(m); |
---|
2104 | mtag != NULL && !match; |
---|
2105 | mtag = m_tag_next(m, mtag)) { |
---|
2106 | uint16_t *p; |
---|
2107 | int i; |
---|
2108 | |
---|
2109 | if (mtag->m_tag_cookie != MTAG_IPFW) |
---|
2110 | continue; |
---|
2111 | |
---|
2112 | p = ((ipfw_insn_u16 *)cmd)->ports; |
---|
2113 | i = cmdlen - 1; |
---|
2114 | for(; !match && i > 0; i--, p += 2) |
---|
2115 | match = |
---|
2116 | mtag->m_tag_id >= p[0] && |
---|
2117 | mtag->m_tag_id <= p[1]; |
---|
2118 | } |
---|
2119 | break; |
---|
2120 | } |
---|
2121 | |
---|
2122 | /* |
---|
2123 | * The second set of opcodes represents 'actions', |
---|
2124 | * i.e. the terminal part of a rule once the packet |
---|
2125 | * matches all previous patterns. |
---|
2126 | * Typically there is only one action for each rule, |
---|
2127 | * and the opcode is stored at the end of the rule |
---|
2128 | * (but there are exceptions -- see below). |
---|
2129 | * |
---|
2130 | * In general, here we set retval and terminate the |
---|
2131 | * outer loop (would be a 'break 3' in some language, |
---|
2132 | * but we need to set l=0, done=1) |
---|
2133 | * |
---|
2134 | * Exceptions: |
---|
2135 | * O_COUNT and O_SKIPTO actions: |
---|
2136 | * instead of terminating, we jump to the next rule |
---|
2137 | * (setting l=0), or to the SKIPTO target (setting |
---|
2138 | * f/f_len, cmd and l as needed), respectively. |
---|
2139 | * |
---|
2140 | * O_TAG, O_LOG and O_ALTQ action parameters: |
---|
2141 | * perform some action and set match = 1; |
---|
2142 | * |
---|
2143 | * O_LIMIT and O_KEEP_STATE: these opcodes are |
---|
2144 | * not real 'actions', and are stored right |
---|
2145 | * before the 'action' part of the rule. |
---|
2146 | * These opcodes try to install an entry in the |
---|
2147 | * state tables; if successful, we continue with |
---|
2148 | * the next opcode (match=1; break;), otherwise |
---|
2149 | * the packet must be dropped (set retval, |
---|
2150 | * break loops with l=0, done=1) |
---|
2151 | * |
---|
2152 | * O_PROBE_STATE and O_CHECK_STATE: these opcodes |
---|
2153 | * cause a lookup of the state table, and a jump |
---|
2154 | * to the 'action' part of the parent rule |
---|
2155 | * if an entry is found, or |
---|
2156 | * (CHECK_STATE only) a jump to the next rule if |
---|
2157 | * the entry is not found. |
---|
2158 | * The result of the lookup is cached so that |
---|
2159 | * further instances of these opcodes become NOPs. |
---|
2160 | * The jump to the next rule is done by setting |
---|
2161 | * l=0, cmdlen=0. |
---|
2162 | */ |
---|
2163 | case O_LIMIT: |
---|
2164 | case O_KEEP_STATE: |
---|
2165 | if (ipfw_install_state(chain, f, |
---|
2166 | (ipfw_insn_limit *)cmd, args, tablearg)) { |
---|
2167 | /* error or limit violation */ |
---|
2168 | retval = IP_FW_DENY; |
---|
2169 | l = 0; /* exit inner loop */ |
---|
2170 | done = 1; /* exit outer loop */ |
---|
2171 | } |
---|
2172 | match = 1; |
---|
2173 | break; |
---|
2174 | |
---|
2175 | case O_PROBE_STATE: |
---|
2176 | case O_CHECK_STATE: |
---|
2177 | /* |
---|
2178 | * dynamic rules are checked at the first |
---|
2179 | * keep-state or check-state occurrence, |
---|
2180 | * with the result being stored in dyn_dir |
---|
2181 | * and dyn_name. |
---|
2182 | * The compiler introduces a PROBE_STATE |
---|
2183 | * instruction for us when we have a |
---|
2184 | * KEEP_STATE (because PROBE_STATE needs |
---|
2185 | * to be run first). |
---|
2186 | * |
---|
2187 | * (dyn_dir == MATCH_UNKNOWN) means this is |
---|
2188 | * first lookup for such f_id. Do lookup. |
---|
2189 | * |
---|
2190 | * (dyn_dir != MATCH_UNKNOWN && |
---|
2191 | * dyn_name != 0 && dyn_name != cmd->arg1) |
---|
2192 | * means previous lookup didn't find dynamic |
---|
2193 | * rule for specific state name and current |
---|
2194 | * lookup will search rule with another state |
---|
2195 | * name. Redo lookup. |
---|
2196 | * |
---|
2197 | * (dyn_dir != MATCH_UNKNOWN && dyn_name == 0) |
---|
2198 | * means previous lookup was for `any' name |
---|
2199 | * and it didn't find rule. No need to do |
---|
2200 | * lookup again. |
---|
2201 | */ |
---|
2202 | if ((dyn_dir == MATCH_UNKNOWN || |
---|
2203 | (dyn_name != 0 && |
---|
2204 | dyn_name != cmd->arg1)) && |
---|
2205 | (q = ipfw_lookup_dyn_rule(&args->f_id, |
---|
2206 | &dyn_dir, proto == IPPROTO_TCP ? |
---|
2207 | TCP(ulp): NULL, |
---|
2208 | (dyn_name = cmd->arg1))) != NULL) { |
---|
2209 | /* |
---|
2210 | * Found dynamic entry, update stats |
---|
2211 | * and jump to the 'action' part of |
---|
2212 | * the parent rule by setting |
---|
2213 | * f, cmd, l and clearing cmdlen. |
---|
2214 | */ |
---|
2215 | IPFW_INC_DYN_COUNTER(q, pktlen); |
---|
2216 | /* XXX we would like to have f_pos |
---|
2217 | * readily accessible in the dynamic |
---|
2218 | * rule, instead of having to |
---|
2219 | * lookup q->rule. |
---|
2220 | */ |
---|
2221 | f = q->rule; |
---|
2222 | f_pos = ipfw_find_rule(chain, |
---|
2223 | f->rulenum, f->id); |
---|
2224 | cmd = ACTION_PTR(f); |
---|
2225 | l = f->cmd_len - f->act_ofs; |
---|
2226 | ipfw_dyn_unlock(q); |
---|
2227 | cmdlen = 0; |
---|
2228 | match = 1; |
---|
2229 | break; |
---|
2230 | } |
---|
2231 | /* |
---|
2232 | * Dynamic entry not found. If CHECK_STATE, |
---|
2233 | * skip to next rule, if PROBE_STATE just |
---|
2234 | * ignore and continue with next opcode. |
---|
2235 | */ |
---|
2236 | if (cmd->opcode == O_CHECK_STATE) |
---|
2237 | l = 0; /* exit inner loop */ |
---|
2238 | match = 1; |
---|
2239 | break; |
---|
2240 | |
---|
2241 | case O_ACCEPT: |
---|
2242 | retval = 0; /* accept */ |
---|
2243 | l = 0; /* exit inner loop */ |
---|
2244 | done = 1; /* exit outer loop */ |
---|
2245 | break; |
---|
2246 | |
---|
2247 | case O_PIPE: |
---|
2248 | case O_QUEUE: |
---|
2249 | set_match(args, f_pos, chain); |
---|
2250 | args->rule.info = TARG(cmd->arg1, pipe); |
---|
2251 | if (cmd->opcode == O_PIPE) |
---|
2252 | args->rule.info |= IPFW_IS_PIPE; |
---|
2253 | if (V_fw_one_pass) |
---|
2254 | args->rule.info |= IPFW_ONEPASS; |
---|
2255 | retval = IP_FW_DUMMYNET; |
---|
2256 | l = 0; /* exit inner loop */ |
---|
2257 | done = 1; /* exit outer loop */ |
---|
2258 | break; |
---|
2259 | |
---|
2260 | case O_DIVERT: |
---|
2261 | case O_TEE: |
---|
2262 | if (args->eh) /* not on layer 2 */ |
---|
2263 | break; |
---|
2264 | /* otherwise this is terminal */ |
---|
2265 | l = 0; /* exit inner loop */ |
---|
2266 | done = 1; /* exit outer loop */ |
---|
2267 | retval = (cmd->opcode == O_DIVERT) ? |
---|
2268 | IP_FW_DIVERT : IP_FW_TEE; |
---|
2269 | set_match(args, f_pos, chain); |
---|
2270 | args->rule.info = TARG(cmd->arg1, divert); |
---|
2271 | break; |
---|
2272 | |
---|
2273 | case O_COUNT: |
---|
2274 | IPFW_INC_RULE_COUNTER(f, pktlen); |
---|
2275 | l = 0; /* exit inner loop */ |
---|
2276 | break; |
---|
2277 | |
---|
2278 | case O_SKIPTO: |
---|
2279 | IPFW_INC_RULE_COUNTER(f, pktlen); |
---|
2280 | f_pos = JUMP(chain, f, cmd->arg1, tablearg, 0); |
---|
2281 | /* |
---|
2282 | * Skip disabled rules, and re-enter |
---|
2283 | * the inner loop with the correct |
---|
2284 | * f_pos, f, l and cmd. |
---|
2285 | * Also clear cmdlen and skip_or |
---|
2286 | */ |
---|
2287 | for (; f_pos < chain->n_rules - 1 && |
---|
2288 | (V_set_disable & |
---|
2289 | (1 << chain->map[f_pos]->set)); |
---|
2290 | f_pos++) |
---|
2291 | ; |
---|
2292 | /* Re-enter the inner loop at the skipto rule. */ |
---|
2293 | f = chain->map[f_pos]; |
---|
2294 | l = f->cmd_len; |
---|
2295 | cmd = f->cmd; |
---|
2296 | match = 1; |
---|
2297 | cmdlen = 0; |
---|
2298 | skip_or = 0; |
---|
2299 | continue; |
---|
2300 | break; /* not reached */ |
---|
2301 | |
---|
2302 | case O_CALLRETURN: { |
---|
2303 | /* |
---|
2304 | * Implementation of `subroutine' call/return, |
---|
2305 | * in the stack carried in an mbuf tag. This |
---|
2306 | * is different from `skipto' in that any call |
---|
2307 | * address is possible (`skipto' must prevent |
---|
2308 | * backward jumps to avoid endless loops). |
---|
2309 | * We have `return' action when F_NOT flag is |
---|
2310 | * present. The `m_tag_id' field is used as |
---|
2311 | * stack pointer. |
---|
2312 | */ |
---|
2313 | struct m_tag *mtag; |
---|
2314 | uint16_t jmpto, *stack; |
---|
2315 | |
---|
2316 | #define IS_CALL ((cmd->len & F_NOT) == 0) |
---|
2317 | #define IS_RETURN ((cmd->len & F_NOT) != 0) |
---|
2318 | /* |
---|
2319 | * Hand-rolled version of m_tag_locate() with |
---|
2320 | * wildcard `type'. |
---|
2321 | * If not already tagged, allocate new tag. |
---|
2322 | */ |
---|
2323 | mtag = m_tag_first(m); |
---|
2324 | while (mtag != NULL) { |
---|
2325 | if (mtag->m_tag_cookie == |
---|
2326 | MTAG_IPFW_CALL) |
---|
2327 | break; |
---|
2328 | mtag = m_tag_next(m, mtag); |
---|
2329 | } |
---|
2330 | if (mtag == NULL && IS_CALL) { |
---|
2331 | mtag = m_tag_alloc(MTAG_IPFW_CALL, 0, |
---|
2332 | IPFW_CALLSTACK_SIZE * |
---|
2333 | sizeof(uint16_t), M_NOWAIT); |
---|
2334 | if (mtag != NULL) |
---|
2335 | m_tag_prepend(m, mtag); |
---|
2336 | } |
---|
2337 | |
---|
2338 | /* |
---|
2339 | * On error both `call' and `return' just |
---|
2340 | * continue with next rule. |
---|
2341 | */ |
---|
2342 | if (IS_RETURN && (mtag == NULL || |
---|
2343 | mtag->m_tag_id == 0)) { |
---|
2344 | l = 0; /* exit inner loop */ |
---|
2345 | break; |
---|
2346 | } |
---|
2347 | if (IS_CALL && (mtag == NULL || |
---|
2348 | mtag->m_tag_id >= IPFW_CALLSTACK_SIZE)) { |
---|
2349 | printf("ipfw: call stack error, " |
---|
2350 | "go to next rule\n"); |
---|
2351 | l = 0; /* exit inner loop */ |
---|
2352 | break; |
---|
2353 | } |
---|
2354 | |
---|
2355 | IPFW_INC_RULE_COUNTER(f, pktlen); |
---|
2356 | stack = (uint16_t *)(mtag + 1); |
---|
2357 | |
---|
2358 | /* |
---|
2359 | * The `call' action may use cached f_pos |
---|
2360 | * (in f->next_rule), whose version is written |
---|
2361 | * in f->next_rule. |
---|
2362 | * The `return' action, however, doesn't have |
---|
2363 | * fixed jump address in cmd->arg1 and can't use |
---|
2364 | * cache. |
---|
2365 | */ |
---|
2366 | if (IS_CALL) { |
---|
2367 | stack[mtag->m_tag_id] = f->rulenum; |
---|
2368 | mtag->m_tag_id++; |
---|
2369 | f_pos = JUMP(chain, f, cmd->arg1, |
---|
2370 | tablearg, 1); |
---|
2371 | } else { /* `return' action */ |
---|
2372 | mtag->m_tag_id--; |
---|
2373 | jmpto = stack[mtag->m_tag_id] + 1; |
---|
2374 | f_pos = ipfw_find_rule(chain, jmpto, 0); |
---|
2375 | } |
---|
2376 | |
---|
2377 | /* |
---|
2378 | * Skip disabled rules, and re-enter |
---|
2379 | * the inner loop with the correct |
---|
2380 | * f_pos, f, l and cmd. |
---|
2381 | * Also clear cmdlen and skip_or |
---|
2382 | */ |
---|
2383 | for (; f_pos < chain->n_rules - 1 && |
---|
2384 | (V_set_disable & |
---|
2385 | (1 << chain->map[f_pos]->set)); f_pos++) |
---|
2386 | ; |
---|
2387 | /* Re-enter the inner loop at the dest rule. */ |
---|
2388 | f = chain->map[f_pos]; |
---|
2389 | l = f->cmd_len; |
---|
2390 | cmd = f->cmd; |
---|
2391 | cmdlen = 0; |
---|
2392 | skip_or = 0; |
---|
2393 | continue; |
---|
2394 | break; /* NOTREACHED */ |
---|
2395 | } |
---|
2396 | #undef IS_CALL |
---|
2397 | #undef IS_RETURN |
---|
2398 | |
---|
2399 | case O_REJECT: |
---|
2400 | /* |
---|
2401 | * Drop the packet and send a reject notice |
---|
2402 | * if the packet is not ICMP (or is an ICMP |
---|
2403 | * query), and it is not multicast/broadcast. |
---|
2404 | */ |
---|
2405 | if (hlen > 0 && is_ipv4 && offset == 0 && |
---|
2406 | (proto != IPPROTO_ICMP || |
---|
2407 | is_icmp_query(ICMP(ulp))) && |
---|
2408 | !(m->m_flags & (M_BCAST|M_MCAST)) && |
---|
2409 | !IN_MULTICAST(ntohl(dst_ip.s_addr))) { |
---|
2410 | send_reject(args, cmd->arg1, iplen, ip); |
---|
2411 | m = args->m; |
---|
2412 | } |
---|
2413 | /* FALLTHROUGH */ |
---|
2414 | #ifdef INET6 |
---|
2415 | case O_UNREACH6: |
---|
2416 | if (hlen > 0 && is_ipv6 && |
---|
2417 | ((offset & IP6F_OFF_MASK) == 0) && |
---|
2418 | (proto != IPPROTO_ICMPV6 || |
---|
2419 | (is_icmp6_query(icmp6_type) == 1)) && |
---|
2420 | !(m->m_flags & (M_BCAST|M_MCAST)) && |
---|
2421 | !IN6_IS_ADDR_MULTICAST(&args->f_id.dst_ip6)) { |
---|
2422 | send_reject6( |
---|
2423 | args, cmd->arg1, hlen, |
---|
2424 | (struct ip6_hdr *)ip); |
---|
2425 | m = args->m; |
---|
2426 | } |
---|
2427 | /* FALLTHROUGH */ |
---|
2428 | #endif |
---|
2429 | case O_DENY: |
---|
2430 | retval = IP_FW_DENY; |
---|
2431 | l = 0; /* exit inner loop */ |
---|
2432 | done = 1; /* exit outer loop */ |
---|
2433 | break; |
---|
2434 | |
---|
2435 | case O_FORWARD_IP: |
---|
2436 | if (args->eh) /* not valid on layer2 pkts */ |
---|
2437 | break; |
---|
2438 | if (q == NULL || q->rule != f || |
---|
2439 | dyn_dir == MATCH_FORWARD) { |
---|
2440 | struct sockaddr_in *sa; |
---|
2441 | |
---|
2442 | sa = &(((ipfw_insn_sa *)cmd)->sa); |
---|
2443 | if (sa->sin_addr.s_addr == INADDR_ANY) { |
---|
2444 | #ifdef INET6 |
---|
2445 | /* |
---|
2446 | * We use O_FORWARD_IP opcode for |
---|
2447 | * fwd rule with tablearg, but tables |
---|
2448 | * now support IPv6 addresses. And |
---|
2449 | * when we are inspecting IPv6 packet, |
---|
2450 | * we can use nh6 field from |
---|
2451 | * table_value as next_hop6 address. |
---|
2452 | */ |
---|
2453 | if (is_ipv6) { |
---|
2454 | struct sockaddr_in6 *sa6; |
---|
2455 | |
---|
2456 | sa6 = args->next_hop6 = |
---|
2457 | &args->hopstore6; |
---|
2458 | sa6->sin6_family = AF_INET6; |
---|
2459 | sa6->sin6_len = sizeof(*sa6); |
---|
2460 | sa6->sin6_addr = TARG_VAL( |
---|
2461 | chain, tablearg, nh6); |
---|
2462 | /* |
---|
2463 | * Set sin6_scope_id only for |
---|
2464 | * link-local unicast addresses. |
---|
2465 | */ |
---|
2466 | if (IN6_IS_ADDR_LINKLOCAL( |
---|
2467 | &sa6->sin6_addr)) |
---|
2468 | sa6->sin6_scope_id = |
---|
2469 | TARG_VAL(chain, |
---|
2470 | tablearg, |
---|
2471 | zoneid); |
---|
2472 | } else |
---|
2473 | #endif |
---|
2474 | { |
---|
2475 | sa = args->next_hop = |
---|
2476 | &args->hopstore; |
---|
2477 | sa->sin_family = AF_INET; |
---|
2478 | sa->sin_len = sizeof(*sa); |
---|
2479 | sa->sin_addr.s_addr = htonl( |
---|
2480 | TARG_VAL(chain, tablearg, |
---|
2481 | nh4)); |
---|
2482 | } |
---|
2483 | } else { |
---|
2484 | args->next_hop = sa; |
---|
2485 | } |
---|
2486 | } |
---|
2487 | retval = IP_FW_PASS; |
---|
2488 | l = 0; /* exit inner loop */ |
---|
2489 | done = 1; /* exit outer loop */ |
---|
2490 | break; |
---|
2491 | |
---|
2492 | #ifdef INET6 |
---|
2493 | case O_FORWARD_IP6: |
---|
2494 | if (args->eh) /* not valid on layer2 pkts */ |
---|
2495 | break; |
---|
2496 | if (q == NULL || q->rule != f || |
---|
2497 | dyn_dir == MATCH_FORWARD) { |
---|
2498 | struct sockaddr_in6 *sin6; |
---|
2499 | |
---|
2500 | sin6 = &(((ipfw_insn_sa6 *)cmd)->sa); |
---|
2501 | args->next_hop6 = sin6; |
---|
2502 | } |
---|
2503 | retval = IP_FW_PASS; |
---|
2504 | l = 0; /* exit inner loop */ |
---|
2505 | done = 1; /* exit outer loop */ |
---|
2506 | break; |
---|
2507 | #endif |
---|
2508 | |
---|
2509 | case O_NETGRAPH: |
---|
2510 | case O_NGTEE: |
---|
2511 | set_match(args, f_pos, chain); |
---|
2512 | args->rule.info = TARG(cmd->arg1, netgraph); |
---|
2513 | if (V_fw_one_pass) |
---|
2514 | args->rule.info |= IPFW_ONEPASS; |
---|
2515 | retval = (cmd->opcode == O_NETGRAPH) ? |
---|
2516 | IP_FW_NETGRAPH : IP_FW_NGTEE; |
---|
2517 | l = 0; /* exit inner loop */ |
---|
2518 | done = 1; /* exit outer loop */ |
---|
2519 | break; |
---|
2520 | |
---|
2521 | case O_SETFIB: { |
---|
2522 | uint32_t fib; |
---|
2523 | |
---|
2524 | IPFW_INC_RULE_COUNTER(f, pktlen); |
---|
2525 | fib = TARG(cmd->arg1, fib) & 0x7FFF; |
---|
2526 | if (fib >= rt_numfibs) |
---|
2527 | fib = 0; |
---|
2528 | M_SETFIB(m, fib); |
---|
2529 | args->f_id.fib = fib; |
---|
2530 | l = 0; /* exit inner loop */ |
---|
2531 | break; |
---|
2532 | } |
---|
2533 | |
---|
2534 | case O_SETDSCP: { |
---|
2535 | uint16_t code; |
---|
2536 | |
---|
2537 | code = TARG(cmd->arg1, dscp) & 0x3F; |
---|
2538 | l = 0; /* exit inner loop */ |
---|
2539 | if (is_ipv4) { |
---|
2540 | uint16_t old; |
---|
2541 | |
---|
2542 | old = *(uint16_t *)ip; |
---|
2543 | ip->ip_tos = (code << 2) | |
---|
2544 | (ip->ip_tos & 0x03); |
---|
2545 | ip->ip_sum = cksum_adjust(ip->ip_sum, |
---|
2546 | old, *(uint16_t *)ip); |
---|
2547 | } else if (is_ipv6) { |
---|
2548 | uint8_t *v; |
---|
2549 | |
---|
2550 | v = &((struct ip6_hdr *)ip)->ip6_vfc; |
---|
2551 | *v = (*v & 0xF0) | (code >> 2); |
---|
2552 | v++; |
---|
2553 | *v = (*v & 0x3F) | ((code & 0x03) << 6); |
---|
2554 | } else |
---|
2555 | break; |
---|
2556 | |
---|
2557 | IPFW_INC_RULE_COUNTER(f, pktlen); |
---|
2558 | break; |
---|
2559 | } |
---|
2560 | |
---|
2561 | case O_NAT: |
---|
2562 | l = 0; /* exit inner loop */ |
---|
2563 | done = 1; /* exit outer loop */ |
---|
2564 | if (!IPFW_NAT_LOADED) { |
---|
2565 | retval = IP_FW_DENY; |
---|
2566 | break; |
---|
2567 | } |
---|
2568 | |
---|
2569 | struct cfg_nat *t; |
---|
2570 | int nat_id; |
---|
2571 | |
---|
2572 | set_match(args, f_pos, chain); |
---|
2573 | /* Check if this is 'global' nat rule */ |
---|
2574 | if (cmd->arg1 == IP_FW_NAT44_GLOBAL) { |
---|
2575 | retval = ipfw_nat_ptr(args, NULL, m); |
---|
2576 | break; |
---|
2577 | } |
---|
2578 | t = ((ipfw_insn_nat *)cmd)->nat; |
---|
2579 | if (t == NULL) { |
---|
2580 | nat_id = TARG(cmd->arg1, nat); |
---|
2581 | t = (*lookup_nat_ptr)(&chain->nat, nat_id); |
---|
2582 | |
---|
2583 | if (t == NULL) { |
---|
2584 | retval = IP_FW_DENY; |
---|
2585 | break; |
---|
2586 | } |
---|
2587 | if (cmd->arg1 != IP_FW_TARG) |
---|
2588 | ((ipfw_insn_nat *)cmd)->nat = t; |
---|
2589 | } |
---|
2590 | retval = ipfw_nat_ptr(args, t, m); |
---|
2591 | break; |
---|
2592 | |
---|
2593 | case O_REASS: { |
---|
2594 | int ip_off; |
---|
2595 | |
---|
2596 | IPFW_INC_RULE_COUNTER(f, pktlen); |
---|
2597 | l = 0; /* in any case exit inner loop */ |
---|
2598 | ip_off = ntohs(ip->ip_off); |
---|
2599 | |
---|
2600 | /* if not fragmented, go to next rule */ |
---|
2601 | if ((ip_off & (IP_MF | IP_OFFMASK)) == 0) |
---|
2602 | break; |
---|
2603 | |
---|
2604 | args->m = m = ip_reass(m); |
---|
2605 | |
---|
2606 | /* |
---|
2607 | * do IP header checksum fixup. |
---|
2608 | */ |
---|
2609 | if (m == NULL) { /* fragment got swallowed */ |
---|
2610 | retval = IP_FW_DENY; |
---|
2611 | } else { /* good, packet complete */ |
---|
2612 | int hlen; |
---|
2613 | |
---|
2614 | ip = mtod(m, struct ip *); |
---|
2615 | hlen = ip->ip_hl << 2; |
---|
2616 | ip->ip_sum = 0; |
---|
2617 | if (hlen == sizeof(struct ip)) |
---|
2618 | ip->ip_sum = in_cksum_hdr(ip); |
---|
2619 | else |
---|
2620 | ip->ip_sum = in_cksum(m, hlen); |
---|
2621 | retval = IP_FW_REASS; |
---|
2622 | set_match(args, f_pos, chain); |
---|
2623 | } |
---|
2624 | done = 1; /* exit outer loop */ |
---|
2625 | break; |
---|
2626 | } |
---|
2627 | case O_EXTERNAL_ACTION: |
---|
2628 | l = 0; /* in any case exit inner loop */ |
---|
2629 | retval = ipfw_run_eaction(chain, args, |
---|
2630 | cmd, &done); |
---|
2631 | /* |
---|
2632 | * If both @retval and @done are zero, |
---|
2633 | * consider this as rule matching and |
---|
2634 | * update counters. |
---|
2635 | */ |
---|
2636 | if (retval == 0 && done == 0) { |
---|
2637 | IPFW_INC_RULE_COUNTER(f, pktlen); |
---|
2638 | /* |
---|
2639 | * Reset the result of the last |
---|
2640 | * dynamic state lookup. |
---|
2641 | * External action can change |
---|
2642 | * @args content, and it may be |
---|
2643 | * used for new state lookup later. |
---|
2644 | */ |
---|
2645 | dyn_dir = MATCH_UNKNOWN; |
---|
2646 | } |
---|
2647 | break; |
---|
2648 | |
---|
2649 | default: |
---|
2650 | panic("-- unknown opcode %d\n", cmd->opcode); |
---|
2651 | } /* end of switch() on opcodes */ |
---|
2652 | /* |
---|
2653 | * if we get here with l=0, then match is irrelevant. |
---|
2654 | */ |
---|
2655 | |
---|
2656 | if (cmd->len & F_NOT) |
---|
2657 | match = !match; |
---|
2658 | |
---|
2659 | if (match) { |
---|
2660 | if (cmd->len & F_OR) |
---|
2661 | skip_or = 1; |
---|
2662 | } else { |
---|
2663 | if (!(cmd->len & F_OR)) /* not an OR block, */ |
---|
2664 | break; /* try next rule */ |
---|
2665 | } |
---|
2666 | |
---|
2667 | } /* end of inner loop, scan opcodes */ |
---|
2668 | #undef PULLUP_LEN |
---|
2669 | |
---|
2670 | if (done) |
---|
2671 | break; |
---|
2672 | |
---|
2673 | /* next_rule:; */ /* try next rule */ |
---|
2674 | |
---|
2675 | } /* end of outer for, scan rules */ |
---|
2676 | |
---|
2677 | if (done) { |
---|
2678 | struct ip_fw *rule = chain->map[f_pos]; |
---|
2679 | /* Update statistics */ |
---|
2680 | IPFW_INC_RULE_COUNTER(rule, pktlen); |
---|
2681 | } else { |
---|
2682 | retval = IP_FW_DENY; |
---|
2683 | printf("ipfw: ouch!, skip past end of rules, denying packet\n"); |
---|
2684 | } |
---|
2685 | IPFW_PF_RUNLOCK(chain); |
---|
2686 | #ifdef __FreeBSD__ |
---|
2687 | if (ucred_cache != NULL) |
---|
2688 | crfree(ucred_cache); |
---|
2689 | #endif |
---|
2690 | return (retval); |
---|
2691 | |
---|
2692 | pullup_failed: |
---|
2693 | if (V_fw_verbose) |
---|
2694 | printf("ipfw: pullup failed\n"); |
---|
2695 | return (IP_FW_DENY); |
---|
2696 | } |
---|
2697 | |
---|
2698 | /* |
---|
2699 | * Set maximum number of tables that can be used in given VNET ipfw instance. |
---|
2700 | */ |
---|
2701 | #ifdef SYSCTL_NODE |
---|
2702 | static int |
---|
2703 | sysctl_ipfw_table_num(SYSCTL_HANDLER_ARGS) |
---|
2704 | { |
---|
2705 | int error; |
---|
2706 | unsigned int ntables; |
---|
2707 | |
---|
2708 | ntables = V_fw_tables_max; |
---|
2709 | |
---|
2710 | error = sysctl_handle_int(oidp, &ntables, 0, req); |
---|
2711 | /* Read operation or some error */ |
---|
2712 | if ((error != 0) || (req->newptr == NULL)) |
---|
2713 | return (error); |
---|
2714 | |
---|
2715 | return (ipfw_resize_tables(&V_layer3_chain, ntables)); |
---|
2716 | } |
---|
2717 | |
---|
2718 | /* |
---|
2719 | * Switches table namespace between global and per-set. |
---|
2720 | */ |
---|
2721 | static int |
---|
2722 | sysctl_ipfw_tables_sets(SYSCTL_HANDLER_ARGS) |
---|
2723 | { |
---|
2724 | int error; |
---|
2725 | unsigned int sets; |
---|
2726 | |
---|
2727 | sets = V_fw_tables_sets; |
---|
2728 | |
---|
2729 | error = sysctl_handle_int(oidp, &sets, 0, req); |
---|
2730 | /* Read operation or some error */ |
---|
2731 | if ((error != 0) || (req->newptr == NULL)) |
---|
2732 | return (error); |
---|
2733 | |
---|
2734 | return (ipfw_switch_tables_namespace(&V_layer3_chain, sets)); |
---|
2735 | } |
---|
2736 | #endif |
---|
2737 | |
---|
2738 | /* |
---|
2739 | * Module and VNET glue |
---|
2740 | */ |
---|
2741 | |
---|
2742 | /* |
---|
2743 | * Stuff that must be initialised only on boot or module load |
---|
2744 | */ |
---|
2745 | static int |
---|
2746 | ipfw_init(void) |
---|
2747 | { |
---|
2748 | int error = 0; |
---|
2749 | |
---|
2750 | /* |
---|
2751 | * Only print out this stuff the first time around, |
---|
2752 | * when called from the sysinit code. |
---|
2753 | */ |
---|
2754 | printf("ipfw2 " |
---|
2755 | #ifdef INET6 |
---|
2756 | "(+ipv6) " |
---|
2757 | #endif |
---|
2758 | "initialized, divert %s, nat %s, " |
---|
2759 | "default to %s, logging ", |
---|
2760 | #ifdef IPDIVERT |
---|
2761 | "enabled", |
---|
2762 | #else |
---|
2763 | "loadable", |
---|
2764 | #endif |
---|
2765 | #ifdef IPFIREWALL_NAT |
---|
2766 | "enabled", |
---|
2767 | #else |
---|
2768 | "loadable", |
---|
2769 | #endif |
---|
2770 | default_to_accept ? "accept" : "deny"); |
---|
2771 | |
---|
2772 | /* |
---|
2773 | * Note: V_xxx variables can be accessed here but the vnet specific |
---|
2774 | * initializer may not have been called yet for the VIMAGE case. |
---|
2775 | * Tuneables will have been processed. We will print out values for |
---|
2776 | * the default vnet. |
---|
2777 | * XXX This should all be rationalized AFTER 8.0 |
---|
2778 | */ |
---|
2779 | if (V_fw_verbose == 0) |
---|
2780 | printf("disabled\n"); |
---|
2781 | else if (V_verbose_limit == 0) |
---|
2782 | printf("unlimited\n"); |
---|
2783 | else |
---|
2784 | printf("limited to %d packets/entry by default\n", |
---|
2785 | V_verbose_limit); |
---|
2786 | |
---|
2787 | /* Check user-supplied table count for validness */ |
---|
2788 | if (default_fw_tables > IPFW_TABLES_MAX) |
---|
2789 | default_fw_tables = IPFW_TABLES_MAX; |
---|
2790 | |
---|
2791 | ipfw_init_sopt_handler(); |
---|
2792 | ipfw_init_obj_rewriter(); |
---|
2793 | ipfw_iface_init(); |
---|
2794 | return (error); |
---|
2795 | } |
---|
2796 | |
---|
2797 | #ifndef __rtems__ |
---|
2798 | /* |
---|
2799 | * Called for the removal of the last instance only on module unload. |
---|
2800 | */ |
---|
2801 | static void |
---|
2802 | ipfw_destroy(void) |
---|
2803 | { |
---|
2804 | |
---|
2805 | ipfw_iface_destroy(); |
---|
2806 | ipfw_destroy_sopt_handler(); |
---|
2807 | ipfw_destroy_obj_rewriter(); |
---|
2808 | printf("IP firewall unloaded\n"); |
---|
2809 | } |
---|
2810 | #endif /* __rtems__ */ |
---|
2811 | |
---|
2812 | /* |
---|
2813 | * Stuff that must be initialized for every instance |
---|
2814 | * (including the first of course). |
---|
2815 | */ |
---|
2816 | static int |
---|
2817 | vnet_ipfw_init(const void *unused) |
---|
2818 | { |
---|
2819 | int error, first; |
---|
2820 | struct ip_fw *rule = NULL; |
---|
2821 | struct ip_fw_chain *chain; |
---|
2822 | |
---|
2823 | chain = &V_layer3_chain; |
---|
2824 | |
---|
2825 | first = IS_DEFAULT_VNET(curvnet) ? 1 : 0; |
---|
2826 | |
---|
2827 | /* First set up some values that are compile time options */ |
---|
2828 | V_autoinc_step = 100; /* bounded to 1..1000 in add_rule() */ |
---|
2829 | V_fw_deny_unknown_exthdrs = 1; |
---|
2830 | #ifdef IPFIREWALL_VERBOSE |
---|
2831 | V_fw_verbose = 1; |
---|
2832 | #endif |
---|
2833 | #ifdef IPFIREWALL_VERBOSE_LIMIT |
---|
2834 | V_verbose_limit = IPFIREWALL_VERBOSE_LIMIT; |
---|
2835 | #endif |
---|
2836 | #ifdef IPFIREWALL_NAT |
---|
2837 | LIST_INIT(&chain->nat); |
---|
2838 | #endif |
---|
2839 | |
---|
2840 | /* Init shared services hash table */ |
---|
2841 | ipfw_init_srv(chain); |
---|
2842 | |
---|
2843 | ipfw_init_counters(); |
---|
2844 | /* insert the default rule and create the initial map */ |
---|
2845 | chain->n_rules = 1; |
---|
2846 | chain->map = malloc(sizeof(struct ip_fw *), M_IPFW, M_WAITOK | M_ZERO); |
---|
2847 | rule = ipfw_alloc_rule(chain, sizeof(struct ip_fw)); |
---|
2848 | |
---|
2849 | /* Set initial number of tables */ |
---|
2850 | V_fw_tables_max = default_fw_tables; |
---|
2851 | error = ipfw_init_tables(chain, first); |
---|
2852 | if (error) { |
---|
2853 | printf("ipfw2: setting up tables failed\n"); |
---|
2854 | free(chain->map, M_IPFW); |
---|
2855 | free(rule, M_IPFW); |
---|
2856 | return (ENOSPC); |
---|
2857 | } |
---|
2858 | |
---|
2859 | /* fill and insert the default rule */ |
---|
2860 | rule->act_ofs = 0; |
---|
2861 | rule->rulenum = IPFW_DEFAULT_RULE; |
---|
2862 | rule->cmd_len = 1; |
---|
2863 | rule->set = RESVD_SET; |
---|
2864 | rule->cmd[0].len = 1; |
---|
2865 | rule->cmd[0].opcode = default_to_accept ? O_ACCEPT : O_DENY; |
---|
2866 | chain->default_rule = chain->map[0] = rule; |
---|
2867 | chain->id = rule->id = 1; |
---|
2868 | /* Pre-calculate rules length for legacy dump format */ |
---|
2869 | chain->static_len = sizeof(struct ip_fw_rule0); |
---|
2870 | |
---|
2871 | IPFW_LOCK_INIT(chain); |
---|
2872 | ipfw_dyn_init(chain); |
---|
2873 | ipfw_eaction_init(chain, first); |
---|
2874 | #ifdef LINEAR_SKIPTO |
---|
2875 | ipfw_init_skipto_cache(chain); |
---|
2876 | #endif |
---|
2877 | ipfw_bpf_init(first); |
---|
2878 | |
---|
2879 | /* First set up some values that are compile time options */ |
---|
2880 | V_ipfw_vnet_ready = 1; /* Open for business */ |
---|
2881 | |
---|
2882 | /* |
---|
2883 | * Hook the sockopt handler and pfil hooks for ipv4 and ipv6. |
---|
2884 | * Even if the latter two fail we still keep the module alive |
---|
2885 | * because the sockopt and layer2 paths are still useful. |
---|
2886 | * ipfw[6]_hook return 0 on success, ENOENT on failure, |
---|
2887 | * so we can ignore the exact return value and just set a flag. |
---|
2888 | * |
---|
2889 | * Note that V_fw[6]_enable are manipulated by a SYSCTL_PROC so |
---|
2890 | * changes in the underlying (per-vnet) variables trigger |
---|
2891 | * immediate hook()/unhook() calls. |
---|
2892 | * In layer2 we have the same behaviour, except that V_ether_ipfw |
---|
2893 | * is checked on each packet because there are no pfil hooks. |
---|
2894 | */ |
---|
2895 | V_ip_fw_ctl_ptr = ipfw_ctl3; |
---|
2896 | error = ipfw_attach_hooks(1); |
---|
2897 | return (error); |
---|
2898 | } |
---|
2899 | |
---|
2900 | #ifndef __rtems__ |
---|
2901 | /* |
---|
2902 | * Called for the removal of each instance. |
---|
2903 | */ |
---|
2904 | static int |
---|
2905 | vnet_ipfw_uninit(const void *unused) |
---|
2906 | { |
---|
2907 | struct ip_fw *reap; |
---|
2908 | struct ip_fw_chain *chain = &V_layer3_chain; |
---|
2909 | int i, last; |
---|
2910 | |
---|
2911 | V_ipfw_vnet_ready = 0; /* tell new callers to go away */ |
---|
2912 | /* |
---|
2913 | * disconnect from ipv4, ipv6, layer2 and sockopt. |
---|
2914 | * Then grab, release and grab again the WLOCK so we make |
---|
2915 | * sure the update is propagated and nobody will be in. |
---|
2916 | */ |
---|
2917 | (void)ipfw_attach_hooks(0 /* detach */); |
---|
2918 | V_ip_fw_ctl_ptr = NULL; |
---|
2919 | |
---|
2920 | last = IS_DEFAULT_VNET(curvnet) ? 1 : 0; |
---|
2921 | |
---|
2922 | IPFW_UH_WLOCK(chain); |
---|
2923 | IPFW_UH_WUNLOCK(chain); |
---|
2924 | |
---|
2925 | ipfw_dyn_uninit(0); /* run the callout_drain */ |
---|
2926 | |
---|
2927 | IPFW_UH_WLOCK(chain); |
---|
2928 | |
---|
2929 | reap = NULL; |
---|
2930 | IPFW_WLOCK(chain); |
---|
2931 | for (i = 0; i < chain->n_rules; i++) |
---|
2932 | ipfw_reap_add(chain, &reap, chain->map[i]); |
---|
2933 | free(chain->map, M_IPFW); |
---|
2934 | #ifdef LINEAR_SKIPTO |
---|
2935 | ipfw_destroy_skipto_cache(chain); |
---|
2936 | #endif |
---|
2937 | IPFW_WUNLOCK(chain); |
---|
2938 | IPFW_UH_WUNLOCK(chain); |
---|
2939 | ipfw_destroy_tables(chain, last); |
---|
2940 | ipfw_eaction_uninit(chain, last); |
---|
2941 | if (reap != NULL) |
---|
2942 | ipfw_reap_rules(reap); |
---|
2943 | vnet_ipfw_iface_destroy(chain); |
---|
2944 | ipfw_destroy_srv(chain); |
---|
2945 | IPFW_LOCK_DESTROY(chain); |
---|
2946 | ipfw_dyn_uninit(1); /* free the remaining parts */ |
---|
2947 | ipfw_destroy_counters(); |
---|
2948 | ipfw_bpf_uninit(last); |
---|
2949 | return (0); |
---|
2950 | } |
---|
2951 | #endif /* __rtems__ */ |
---|
2952 | |
---|
2953 | /* |
---|
2954 | * Module event handler. |
---|
2955 | * In general we have the choice of handling most of these events by the |
---|
2956 | * event handler or by the (VNET_)SYS(UN)INIT handlers. I have chosen to |
---|
2957 | * use the SYSINIT handlers as they are more capable of expressing the |
---|
2958 | * flow of control during module and vnet operations, so this is just |
---|
2959 | * a skeleton. Note there is no SYSINIT equivalent of the module |
---|
2960 | * SHUTDOWN handler, but we don't have anything to do in that case anyhow. |
---|
2961 | */ |
---|
2962 | static int |
---|
2963 | ipfw_modevent(module_t mod, int type, void *unused) |
---|
2964 | { |
---|
2965 | int err = 0; |
---|
2966 | |
---|
2967 | switch (type) { |
---|
2968 | case MOD_LOAD: |
---|
2969 | /* Called once at module load or |
---|
2970 | * system boot if compiled in. */ |
---|
2971 | break; |
---|
2972 | case MOD_QUIESCE: |
---|
2973 | /* Called before unload. May veto unloading. */ |
---|
2974 | break; |
---|
2975 | case MOD_UNLOAD: |
---|
2976 | /* Called during unload. */ |
---|
2977 | break; |
---|
2978 | case MOD_SHUTDOWN: |
---|
2979 | /* Called during system shutdown. */ |
---|
2980 | break; |
---|
2981 | default: |
---|
2982 | err = EOPNOTSUPP; |
---|
2983 | break; |
---|
2984 | } |
---|
2985 | return err; |
---|
2986 | } |
---|
2987 | |
---|
2988 | static moduledata_t ipfwmod = { |
---|
2989 | "ipfw", |
---|
2990 | ipfw_modevent, |
---|
2991 | 0 |
---|
2992 | }; |
---|
2993 | |
---|
2994 | /* Define startup order. */ |
---|
2995 | #define IPFW_SI_SUB_FIREWALL SI_SUB_PROTO_FIREWALL |
---|
2996 | #define IPFW_MODEVENT_ORDER (SI_ORDER_ANY - 255) /* On boot slot in here. */ |
---|
2997 | #define IPFW_MODULE_ORDER (IPFW_MODEVENT_ORDER + 1) /* A little later. */ |
---|
2998 | #define IPFW_VNET_ORDER (IPFW_MODEVENT_ORDER + 2) /* Later still. */ |
---|
2999 | |
---|
3000 | DECLARE_MODULE(ipfw, ipfwmod, IPFW_SI_SUB_FIREWALL, IPFW_MODEVENT_ORDER); |
---|
3001 | FEATURE(ipfw_ctl3, "ipfw new sockopt calls"); |
---|
3002 | MODULE_VERSION(ipfw, 3); |
---|
3003 | /* should declare some dependencies here */ |
---|
3004 | |
---|
3005 | /* |
---|
3006 | * Starting up. Done in order after ipfwmod() has been called. |
---|
3007 | * VNET_SYSINIT is also called for each existing vnet and each new vnet. |
---|
3008 | */ |
---|
3009 | SYSINIT(ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, |
---|
3010 | ipfw_init, NULL); |
---|
3011 | VNET_SYSINIT(vnet_ipfw_init, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, |
---|
3012 | vnet_ipfw_init, NULL); |
---|
3013 | |
---|
3014 | /* |
---|
3015 | * Closing up shop. These are done in REVERSE ORDER, but still |
---|
3016 | * after ipfwmod() has been called. Not called on reboot. |
---|
3017 | * VNET_SYSUNINIT is also called for each exiting vnet as it exits. |
---|
3018 | * or when the module is unloaded. |
---|
3019 | */ |
---|
3020 | SYSUNINIT(ipfw_destroy, IPFW_SI_SUB_FIREWALL, IPFW_MODULE_ORDER, |
---|
3021 | ipfw_destroy, NULL); |
---|
3022 | VNET_SYSUNINIT(vnet_ipfw_uninit, IPFW_SI_SUB_FIREWALL, IPFW_VNET_ORDER, |
---|
3023 | vnet_ipfw_uninit, NULL); |
---|
3024 | /* end of file */ |
---|