]>
Commit | Line | Data |
---|---|---|
6178953b | 1 | #!/usr/bin/perl -w |
2a81ab0d AM |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5bee9a9d | 5 | # Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> # |
2a81ab0d AM |
6 | # # |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
2a81ab0d | 21 | |
2a81ab0d | 22 | use strict; |
2a81ab0d | 23 | |
97ab0569 MT |
24 | require '/var/ipfire/general-functions.pl'; |
25 | require "${General::swroot}/lang.pl"; | |
26 | require "/usr/lib/firewall/firewall-lib.pl"; | |
2a81ab0d | 27 | |
68d1eb10 MT |
28 | # Set to one to enable debugging mode. |
29 | my $DEBUG = 0; | |
30 | ||
1f9e7b53 MT |
31 | my $IPTABLES = "iptables --wait"; |
32 | ||
6178953b | 33 | # iptables chains |
8f4f4634 MT |
34 | my $CHAIN_INPUT = "INPUTFW"; |
35 | my $CHAIN_FORWARD = "FORWARDFW"; | |
d98aa95a | 36 | my $CHAIN_OUTPUT = "OUTGOINGFW"; |
8f4f4634 | 37 | my $CHAIN = $CHAIN_FORWARD; |
6178953b MT |
38 | my $CHAIN_NAT_SOURCE = "NAT_SOURCE"; |
39 | my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION"; | |
6e87f0aa | 40 | my $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION"; |
8f4f4634 | 41 | my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT); |
c2a1af75 | 42 | my @ANY_ADDRESSES = ("0.0.0.0/0.0.0.0", "0.0.0.0/0", "0/0"); |
8f4f4634 MT |
43 | |
44 | my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip"); | |
45 | my @PROTOCOLS_WITH_PORTS = ("tcp", "udp"); | |
46 | ||
47 | my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT"); | |
6178953b | 48 | |
2a81ab0d | 49 | my %fwdfwsettings=(); |
aa5f4b65 | 50 | my %fwoptions = (); |
2a81ab0d | 51 | my %defaultNetworks=(); |
8f4f4634 | 52 | my %configfwdfw=();; |
2a81ab0d | 53 | my %customgrp=(); |
2a81ab0d | 54 | my %configinputfw=(); |
5d7faa45 | 55 | my %configoutgoingfw=(); |
a6edca5a | 56 | my %confignatfw=(); |
36196d0d | 57 | my @p2ps=(); |
2a81ab0d | 58 | |
6d8eb5de AM |
59 | my $configfwdfw = "${General::swroot}/firewall/config"; |
60 | my $configinput = "${General::swroot}/firewall/input"; | |
61 | my $configoutgoing = "${General::swroot}/firewall/outgoing"; | |
62 | my $p2pfile = "${General::swroot}/firewall/p2protocols"; | |
2a81ab0d | 63 | my $configgrp = "${General::swroot}/fwhosts/customgroups"; |
210ee67b | 64 | my $netsettings = "${General::swroot}/ethernet/settings"; |
86a921ee | 65 | |
6d8eb5de | 66 | &General::readhash("${General::swroot}/firewall/settings", \%fwdfwsettings); |
aa5f4b65 | 67 | &General::readhash("${General::swroot}/optionsfw/settings", \%fwoptions); |
210ee67b | 68 | &General::readhash("$netsettings", \%defaultNetworks); |
2a81ab0d AM |
69 | &General::readhasharray($configfwdfw, \%configfwdfw); |
70 | &General::readhasharray($configinput, \%configinputfw); | |
5d7faa45 | 71 | &General::readhasharray($configoutgoing, \%configoutgoingfw); |
2a81ab0d | 72 | &General::readhasharray($configgrp, \%customgrp); |
2a81ab0d | 73 | |
3bb4bb3f MT |
74 | my @log_limit_options = &make_log_limit_options(); |
75 | ||
aa5f4b65 MT |
76 | my $POLICY_INPUT_ALLOWED = 0; |
77 | my $POLICY_FORWARD_ALLOWED = ($fwdfwsettings{"POLICY"} eq "MODE2"); | |
78 | my $POLICY_OUTPUT_ALLOWED = ($fwdfwsettings{"POLICY1"} eq "MODE2"); | |
79 | ||
80 | my $POLICY_INPUT_ACTION = $fwoptions{"FWPOLICY2"}; | |
81 | my $POLICY_FORWARD_ACTION = $fwoptions{"FWPOLICY"}; | |
82 | my $POLICY_OUTPUT_ACTION = $fwoptions{"FWPOLICY1"}; | |
83 | ||
8531b94a MT |
84 | # MAIN |
85 | &main(); | |
86 | ||
87 | sub main { | |
88 | # Flush all chains. | |
89 | &flush(); | |
90 | ||
2d0c7a9f AM |
91 | # Prepare firewall rules. |
92 | if (! -z "${General::swroot}/firewall/input"){ | |
93 | &buildrules(\%configinputfw); | |
94 | } | |
95 | if (! -z "${General::swroot}/firewall/outgoing"){ | |
96 | &buildrules(\%configoutgoingfw); | |
97 | } | |
98 | if (! -z "${General::swroot}/firewall/config"){ | |
99 | &buildrules(\%configfwdfw); | |
100 | } | |
8531b94a MT |
101 | |
102 | # Load P2P block rules. | |
103 | &p2pblock(); | |
104 | ||
105 | # Reload firewall policy. | |
106 | run("/usr/sbin/firewall-policy"); | |
2d0c7a9f AM |
107 | |
108 | #Reload firewall.local if present | |
109 | if ( -f '/etc/sysconfig/firewall.local'){ | |
110 | run("/etc/sysconfig/firewall.local reload"); | |
111 | } | |
2a81ab0d | 112 | } |
97ab0569 | 113 | |
68d1eb10 MT |
114 | sub run { |
115 | # Executes or prints the given shell command. | |
116 | my $command = shift; | |
117 | ||
118 | if ($DEBUG) { | |
119 | print "$command\n"; | |
120 | } else { | |
121 | system "$command"; | |
6e87f0aa MT |
122 | |
123 | if ($?) { | |
124 | print_error("ERROR: $command"); | |
125 | } | |
68d1eb10 MT |
126 | } |
127 | } | |
128 | ||
6178953b MT |
129 | sub print_error { |
130 | my $message = shift; | |
131 | ||
132 | print STDERR "$message\n"; | |
133 | } | |
134 | ||
8f4f4634 MT |
135 | sub print_rule { |
136 | my $hash = shift; | |
137 | ||
138 | print "\nRULE:"; | |
139 | ||
140 | my $i = 0; | |
141 | foreach (@$hash) { | |
142 | printf(" %2d: %s", $i++, $_); | |
143 | } | |
144 | print "\n"; | |
145 | } | |
146 | ||
791c2b45 MT |
147 | sub count_elements { |
148 | my $hash = shift; | |
149 | ||
150 | return scalar @$hash; | |
151 | } | |
152 | ||
97ab0569 | 153 | sub flush { |
d98aa95a MT |
154 | run("$IPTABLES -F $CHAIN_INPUT"); |
155 | run("$IPTABLES -F $CHAIN_FORWARD"); | |
156 | run("$IPTABLES -F $CHAIN_OUTPUT"); | |
157 | run("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE"); | |
158 | run("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION"); | |
6e87f0aa | 159 | run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX"); |
86a921ee | 160 | } |
97ab0569 | 161 | |
97ab0569 | 162 | sub buildrules { |
8f4f4634 MT |
163 | my $hash = shift; |
164 | ||
aa5f4b65 MT |
165 | # Search for targets that need to be specially handled when adding |
166 | # forwarding rules. Additional rules will automatically get inserted | |
167 | # into the INPUT/OUTPUT chains for these targets. | |
168 | my @special_input_targets = (); | |
169 | if (!$POLICY_FORWARD_ALLOWED) { | |
170 | push(@special_input_targets, "ACCEPT"); | |
171 | } | |
172 | ||
173 | if ($POLICY_INPUT_ACTION eq "DROP") { | |
174 | push(@special_input_targets, "REJECT"); | |
175 | } elsif ($POLICY_INPUT_ACTION eq "REJECT") { | |
176 | push(@special_input_targets, "DROP"); | |
177 | } | |
178 | ||
179 | my @special_output_targets = (); | |
180 | if ($POLICY_OUTPUT_ALLOWED) { | |
181 | push(@special_output_targets, ("DROP", "REJECT")); | |
182 | } else { | |
183 | push(@special_output_targets, "ACCEPT"); | |
184 | ||
185 | if ($POLICY_OUTPUT_ACTION eq "DROP") { | |
186 | push(@special_output_targets, "REJECT"); | |
187 | } elsif ($POLICY_OUTPUT_ACTION eq "REJECT") { | |
188 | push(@special_output_targets, "DROP"); | |
189 | } | |
190 | } | |
191 | ||
8f4f4634 MT |
192 | foreach my $key (sort {$a <=> $b} keys %$hash) { |
193 | # Skip disabled rules. | |
194 | next unless ($$hash{$key}[2] eq 'ON'); | |
195 | ||
791c2b45 MT |
196 | # Count number of elements in this line |
197 | my $elements = &count_elements($$hash{$key}); | |
198 | ||
8f4f4634 MT |
199 | if ($DEBUG) { |
200 | print_rule($$hash{$key}); | |
201 | } | |
202 | ||
203 | # Check if the target is valid. | |
204 | my $target = $$hash{$key}[0]; | |
205 | if (!$target ~~ @VALID_TARGETS) { | |
206 | print_error("Invalid target '$target' for rule $key"); | |
207 | next; | |
208 | } | |
209 | ||
210 | # Check if the chain is valid. | |
211 | my $chain = $$hash{$key}[1]; | |
212 | if (!$chain ~~ @VALID_CHAINS) { | |
213 | print_error("Invalid chain '$chain' in rule $key"); | |
214 | next; | |
215 | } | |
216 | ||
217 | # Collect all sources. | |
4e54e3c6 | 218 | my @sources = &fwlib::get_addresses($hash, $key, "src"); |
8f4f4634 MT |
219 | |
220 | # Collect all destinations. | |
4e54e3c6 | 221 | my @destinations = &fwlib::get_addresses($hash, $key, "tgt"); |
6178953b | 222 | |
c0ce9206 MT |
223 | # True if the destination is the firewall itself. |
224 | my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire"); | |
225 | ||
6178953b | 226 | # Check if logging should be enabled. |
8f4f4634 | 227 | my $LOG = ($$hash{$key}[17] eq 'ON'); |
6178953b | 228 | |
8f4f4634 MT |
229 | # Check if NAT is enabled and initialize variables, that we use for that. |
230 | my $NAT = ($$hash{$key}[28] eq 'ON'); | |
6178953b | 231 | my $NAT_MODE; |
8f4f4634 MT |
232 | if ($NAT) { |
233 | $NAT_MODE = uc($$hash{$key}[31]); | |
234 | } | |
6178953b | 235 | |
8f4f4634 MT |
236 | # Set up time constraints. |
237 | my @time_options = (); | |
238 | if ($$hash{$key}[18] eq 'ON') { | |
239 | push(@time_options, ("-m", "time")); | |
6178953b | 240 | |
8f4f4634 MT |
241 | # Select all days of the week this match is active. |
242 | my @weekdays = (); | |
243 | if ($$hash{$key}[19] ne '') { | |
244 | push (@weekdays, "Mon"); | |
245 | } | |
246 | if ($$hash{$key}[20] ne '') { | |
247 | push (@weekdays, "Tue"); | |
248 | } | |
249 | if ($$hash{$key}[21] ne '') { | |
250 | push (@weekdays, "Wed"); | |
251 | } | |
252 | if ($$hash{$key}[22] ne '') { | |
253 | push (@weekdays, "Thu"); | |
254 | } | |
255 | if ($$hash{$key}[23] ne '') { | |
256 | push (@weekdays, "Fri"); | |
257 | } | |
258 | if ($$hash{$key}[24] ne '') { | |
259 | push (@weekdays, "Sat"); | |
260 | } | |
261 | if ($$hash{$key}[25] ne '') { | |
262 | push (@weekdays, "Sun"); | |
263 | } | |
264 | if (@weekdays) { | |
265 | push(@time_options, ("--weekdays", join(",", @weekdays))); | |
266 | } | |
6178953b | 267 | |
8f4f4634 MT |
268 | # Convert start time. |
269 | my $time_start = &format_time($$hash{$key}[26]); | |
270 | if ($time_start) { | |
271 | push(@time_options, ("--timestart", $time_start)); | |
a6edca5a | 272 | } |
6178953b | 273 | |
8f4f4634 MT |
274 | # Convert end time. |
275 | my $time_stop = &format_time($$hash{$key}[27]); | |
276 | if ($time_stop) { | |
277 | push(@time_options, ("--timestop", $time_stop)); | |
278 | } | |
a6edca5a | 279 | } |
6178953b | 280 | |
d2793ea8 AM |
281 | # Concurrent connection limit |
282 | my @ratelimit_options = (); | |
791c2b45 | 283 | |
d840d02a | 284 | if (($elements ge 34) && ($$hash{$key}[32] eq 'ON')) { |
d2793ea8 AM |
285 | my $conn_limit = $$hash{$key}[33]; |
286 | ||
287 | if ($conn_limit ge 1) { | |
288 | push(@ratelimit_options, ("-m", "connlimit")); | |
289 | ||
290 | # Use the the entire source IP address | |
291 | push(@ratelimit_options, "--connlimit-saddr"); | |
292 | push(@ratelimit_options, ("--connlimit-mask", "32")); | |
293 | ||
294 | # Apply the limit | |
295 | push(@ratelimit_options, ("--connlimit-upto", $conn_limit)); | |
296 | } | |
297 | } | |
298 | ||
299 | # Ratelimit | |
d840d02a | 300 | if (($elements ge 37) && ($$hash{$key}[34] eq 'ON')) { |
d2793ea8 AM |
301 | my $rate_limit = "$$hash{$key}[35]/$$hash{$key}[36]"; |
302 | ||
d840d02a MT |
303 | if ($rate_limit) { |
304 | push(@ratelimit_options, ("-m", "limit")); | |
305 | push(@ratelimit_options, ("--limit", $rate_limit)); | |
306 | } | |
d2793ea8 AM |
307 | } |
308 | ||
8f4f4634 MT |
309 | # Check which protocols are used in this rule and so that we can |
310 | # later group rules by protocols. | |
311 | my @protocols = &get_protocols($hash, $key); | |
312 | if (!@protocols) { | |
313 | print_error("Invalid protocol configuration for rule $key"); | |
314 | next; | |
315 | } | |
316 | ||
317 | foreach my $protocol (@protocols) { | |
318 | # Check if the given protocol is supported. | |
319 | if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) { | |
320 | print_error("Protocol $protocol is not supported (rule $key)"); | |
321 | next; | |
2a81ab0d | 322 | } |
8f4f4634 MT |
323 | |
324 | # Prepare protocol options (like ICMP types, ports, etc...). | |
d7a14d01 | 325 | my @protocol_options = &get_protocol_options($hash, $key, $protocol, 0); |
8f4f4634 MT |
326 | |
327 | # Check if this protocol knows ports. | |
328 | my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS); | |
329 | ||
02574191 MT |
330 | foreach my $src (@sources) { |
331 | # Skip invalid source. | |
4e9a2b57 | 332 | next unless (defined $src); |
02574191 | 333 | next unless ($src); |
8f4f4634 | 334 | |
02574191 | 335 | # Sanitize source. |
48f07c19 | 336 | my $source = @$src[0]; |
02574191 MT |
337 | if ($source ~~ @ANY_ADDRESSES) { |
338 | $source = ""; | |
339 | } | |
340 | ||
48f07c19 AM |
341 | my $source_intf = @$src[1]; |
342 | ||
02574191 MT |
343 | foreach my $dst (@destinations) { |
344 | # Skip invalid rules. | |
4e9a2b57 | 345 | next unless (defined $dst); |
02574191 | 346 | next if (!$dst || ($dst eq "none")); |
c2a1af75 MT |
347 | |
348 | # Sanitize destination. | |
48f07c19 | 349 | my $destination = @$dst[0]; |
c2a1af75 MT |
350 | if ($destination ~~ @ANY_ADDRESSES) { |
351 | $destination = ""; | |
352 | } | |
353 | ||
48f07c19 AM |
354 | my $destination_intf = @$dst[1]; |
355 | ||
8f4f4634 MT |
356 | # Array with iptables arguments. |
357 | my @options = (); | |
358 | ||
359 | # Append protocol. | |
360 | if ($protocol ne "all") { | |
8f4f4634 | 361 | push(@options, @protocol_options); |
2a81ab0d | 362 | } |
8f4f4634 | 363 | |
6e87f0aa MT |
364 | # Prepare source options. |
365 | my @source_options = (); | |
8f4f4634 | 366 | if ($source =~ /mac/) { |
6e87f0aa | 367 | push(@source_options, $source); |
c2a1af75 | 368 | } elsif ($source) { |
6e87f0aa | 369 | push(@source_options, ("-s", $source)); |
2a81ab0d | 370 | } |
14f7cb87 | 371 | |
6e87f0aa | 372 | # Prepare destination options. |
c2a1af75 MT |
373 | my @destination_options = (); |
374 | if ($destination) { | |
375 | push(@destination_options, ("-d", $destination)); | |
376 | } | |
14f7cb87 | 377 | |
8f4f4634 MT |
378 | # Add time constraint options. |
379 | push(@options, @time_options); | |
14f7cb87 | 380 | |
d2793ea8 AM |
381 | # Add ratelimiting option |
382 | push(@options, @ratelimit_options); | |
383 | ||
aa5f4b65 | 384 | my $firewall_is_in_source_subnet = 1; |
e9b5ba41 | 385 | if ($source) { |
da7a2208 | 386 | $firewall_is_in_source_subnet = &firewall_is_in_subnet($source); |
e9b5ba41 MT |
387 | } |
388 | ||
aa5f4b65 MT |
389 | my $firewall_is_in_destination_subnet = 1; |
390 | if ($destination) { | |
391 | $firewall_is_in_destination_subnet = &firewall_is_in_subnet($destination); | |
392 | } | |
393 | ||
8f4f4634 MT |
394 | # Process NAT rules. |
395 | if ($NAT) { | |
4e54e3c6 | 396 | my $nat_address = &fwlib::get_nat_address($$hash{$key}[29], $source); |
b05ec50a | 397 | |
8f4f4634 MT |
398 | # Skip NAT rules if the NAT address is unknown |
399 | # (i.e. no internet connection has been established, yet). | |
400 | next unless ($nat_address); | |
b05ec50a | 401 | |
8f4f4634 MT |
402 | # Destination NAT |
403 | if ($NAT_MODE eq "DNAT") { | |
d7a14d01 MT |
404 | my @nat_options = (); |
405 | if ($protocol ne "all") { | |
406 | my @nat_protocol_options = &get_protocol_options($hash, $key, $protocol, 1); | |
407 | push(@nat_options, @nat_protocol_options); | |
408 | } | |
ff7cb6d6 MT |
409 | push(@nat_options, @time_options); |
410 | ||
411 | # Make port-forwardings useable from the internal networks. | |
412 | my @internal_addresses = &fwlib::get_internal_firewall_ip_addresses(1); | |
413 | unless ($nat_address ~~ @internal_addresses) { | |
48f07c19 | 414 | &add_dnat_mangle_rules($nat_address, $source_intf, @nat_options); |
ff7cb6d6 MT |
415 | } |
416 | ||
6e87f0aa | 417 | push(@nat_options, @source_options); |
8f4f4634 | 418 | push(@nat_options, ("-d", $nat_address)); |
6e87f0aa | 419 | |
c0ce9206 | 420 | my $dnat_port; |
8f4f4634 | 421 | if ($protocol_has_ports) { |
c0ce9206 MT |
422 | $dnat_port = &get_dnat_target_port($hash, $key); |
423 | } | |
424 | ||
425 | my @nat_action_options = (); | |
b05ec50a | 426 | |
c0ce9206 MT |
427 | # Use iptables REDIRECT |
428 | my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports && $dnat_port); | |
429 | if ($use_redirect) { | |
430 | push(@nat_action_options, ("-j", "REDIRECT", "--to-ports", $dnat_port)); | |
431 | ||
432 | # Use iptables DNAT | |
433 | } else { | |
f98bb538 MT |
434 | if ($destination_is_firewall && !$destination) { |
435 | $destination = &fwlib::get_external_address(); | |
436 | } | |
437 | next unless ($destination); | |
438 | ||
c0ce9206 MT |
439 | my ($dnat_address, $dnat_mask) = split("/", $destination); |
440 | @destination_options = ("-d", $dnat_address); | |
441 | ||
442 | if ($protocol_has_ports) { | |
443 | my $dnat_port = &get_dnat_target_port($hash, $key); | |
444 | ||
445 | if ($dnat_port) { | |
446 | $dnat_address .= ":$dnat_port"; | |
447 | } | |
86a921ee | 448 | } |
c0ce9206 MT |
449 | |
450 | push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address)); | |
2a81ab0d | 451 | } |
8f4f4634 MT |
452 | |
453 | if ($LOG) { | |
3bb4bb3f | 454 | run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '"); |
8f4f4634 | 455 | } |
c0ce9206 | 456 | run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options"); |
8f4f4634 MT |
457 | |
458 | # Source NAT | |
459 | } elsif ($NAT_MODE eq "SNAT") { | |
6e87f0aa MT |
460 | my @nat_options = @options; |
461 | ||
462 | push(@nat_options, @source_options); | |
463 | push(@nat_options, @destination_options); | |
464 | ||
8f4f4634 | 465 | if ($LOG) { |
3bb4bb3f | 466 | run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '"); |
8f4f4634 | 467 | } |
6e87f0aa | 468 | run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address"); |
2a81ab0d AM |
469 | } |
470 | } | |
8f4f4634 | 471 | |
1b34f6cd MT |
472 | # Add source and destination interface to the filter rules. |
473 | # These are supposed to help filtering forged packets that originate | |
474 | # from BLUE with an IP address from GREEN for instance. | |
475 | if ($source_intf) { | |
476 | push(@source_options, ("-i", $source_intf)); | |
477 | } | |
478 | ||
479 | if ($destination_intf) { | |
480 | push(@destination_options, ("-o", $destination_intf)); | |
481 | } | |
482 | ||
6e87f0aa MT |
483 | push(@options, @source_options); |
484 | push(@options, @destination_options); | |
485 | ||
8f4f4634 MT |
486 | # Insert firewall rule. |
487 | if ($LOG && !$NAT) { | |
0bda23f5 | 488 | run("$IPTABLES -A $chain @options @log_limit_options -j LOG --log-prefix '$chain '"); |
8f4f4634 MT |
489 | } |
490 | run("$IPTABLES -A $chain @options -j $target"); | |
aa5f4b65 MT |
491 | |
492 | # Handle forwarding rules and add corresponding rules for firewall access. | |
493 | if ($chain eq $CHAIN_FORWARD) { | |
494 | # If the firewall is part of the destination subnet and access to the destination network | |
495 | # is granted/forbidden for any network that the firewall itself is part of, we grant/forbid access | |
496 | # for the firewall, too. | |
497 | if ($firewall_is_in_destination_subnet && ($target ~~ @special_input_targets)) { | |
498 | if ($LOG && !$NAT) { | |
499 | run("$IPTABLES -A $CHAIN_INPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '"); | |
500 | } | |
501 | run("$IPTABLES -A $CHAIN_INPUT @options -j $target"); | |
502 | } | |
503 | ||
504 | # Likewise. | |
505 | if ($firewall_is_in_source_subnet && ($target ~~ @special_output_targets)) { | |
506 | if ($LOG && !$NAT) { | |
507 | run("$IPTABLES -A $CHAIN_OUTPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_OUTPUT '"); | |
508 | } | |
509 | run("$IPTABLES -A $CHAIN_OUTPUT @options -j $target"); | |
510 | } | |
511 | } | |
2a81ab0d AM |
512 | } |
513 | } | |
514 | } | |
2a81ab0d AM |
515 | } |
516 | } | |
97ab0569 | 517 | |
b05ec50a MT |
518 | # Formats the given timestamp into the iptables format which is "hh:mm" UTC. |
519 | sub format_time { | |
520 | my $val = shift; | |
521 | ||
522 | # Convert the given time into minutes. | |
523 | my $minutes = &time_convert_to_minutes($val); | |
524 | ||
525 | # Move the timestamp into UTC. | |
526 | $minutes += &time_utc_offset(); | |
527 | ||
528 | # Make sure $minutes is between 00:00 and 23:59. | |
529 | if ($minutes < 0) { | |
530 | $minutes += 1440; | |
531 | } | |
532 | ||
533 | if ($minutes > 1440) { | |
534 | $minutes -= 1440; | |
535 | } | |
536 | ||
537 | # Format as hh:mm. | |
538 | return sprintf("%02d:%02d", $minutes / 60, $minutes % 60); | |
472136c9 | 539 | } |
97ab0569 | 540 | |
b05ec50a MT |
541 | # Calculates the offsets in minutes from the local timezone to UTC. |
542 | sub time_utc_offset { | |
543 | my @localtime = localtime(time); | |
544 | my @gmtime = gmtime(time); | |
545 | ||
546 | return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60); | |
472136c9 | 547 | } |
97ab0569 | 548 | |
b05ec50a MT |
549 | # Takes a timestamp like "14:00" and converts it into minutes since midnight. |
550 | sub time_convert_to_minutes { | |
551 | my ($hrs, $min) = split(":", shift); | |
552 | ||
553 | return ($hrs * 60) + $min; | |
472136c9 | 554 | } |
97ab0569 MT |
555 | |
556 | sub p2pblock { | |
766c2f60 MT |
557 | open(FILE, "<$p2pfile") or die "Unable to read $p2pfile"; |
558 | my @protocols = (); | |
559 | foreach my $p2pentry (<FILE>) { | |
560 | my @p2pline = split(/\;/, $p2pentry); | |
2a5b19c5 | 561 | next unless ($p2pline[2] eq "off"); |
766c2f60 MT |
562 | |
563 | push(@protocols, "--$p2pline[1]"); | |
36196d0d | 564 | } |
766c2f60 | 565 | close(FILE); |
68d1eb10 | 566 | |
24d36c80 | 567 | run("$IPTABLES -F P2PBLOCK"); |
766c2f60 | 568 | if (@protocols) { |
2a5b19c5 | 569 | run("$IPTABLES -A P2PBLOCK -m ipp2p @protocols -j DROP"); |
36196d0d AM |
570 | } |
571 | } | |
97ab0569 | 572 | |
8f4f4634 MT |
573 | sub get_protocols { |
574 | my $hash = shift; | |
575 | my $key = shift; | |
576 | ||
577 | my $uses_source_ports = ($$hash{$key}[7] eq "ON"); | |
578 | my $uses_services = ($$hash{$key}[11] eq "ON"); | |
579 | ||
580 | my @protocols = (); | |
581 | ||
582 | # Rules which don't have source ports or services (like ICMP, ESP, ...). | |
583 | if (!$uses_source_ports && !$uses_services) { | |
584 | push(@protocols, $$hash{$key}[8]); | |
585 | ||
586 | # Rules which either use ports or services. | |
587 | } elsif ($uses_source_ports || $uses_services) { | |
588 | # Check if service group or service | |
589 | if ($$hash{$key}[14] eq 'cust_srv') { | |
590 | push(@protocols, &fwlib::get_srv_prot($$hash{$key}[15])); | |
591 | ||
592 | } elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
593 | my $protos = &fwlib::get_srvgrp_prot($$hash{$key}[15]); | |
594 | push(@protocols, split(",", $protos)); | |
595 | ||
596 | } else { | |
597 | # Fetch the protocol for this rule. | |
598 | my $protocol = lc($$hash{$key}[8]); | |
599 | ||
600 | # Fetch source and destination ports for this rule. | |
601 | my $source_ports = $$hash{$key}[10]; | |
602 | my $destination_ports = $$hash{$key}[15]; | |
603 | ||
604 | # Check if ports are set for protocols which do not support ports. | |
605 | if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) { | |
606 | print_error("$protocol does not support ports"); | |
607 | return (); | |
608 | } | |
609 | ||
610 | push(@protocols, $protocol); | |
2a81ab0d AM |
611 | } |
612 | } | |
8f4f4634 MT |
613 | |
614 | # Remove all empty elements | |
615 | @protocols = map { $_ ? $_ : () } @protocols; | |
616 | ||
617 | # If no protocol has been defined, we assume "all". | |
618 | if (!@protocols) { | |
619 | push(@protocols, "all"); | |
98cee89f | 620 | } |
8f4f4634 MT |
621 | |
622 | # Make all protocol names lowercase. | |
623 | @protocols = map { lc } @protocols; | |
624 | ||
625 | return @protocols; | |
2a81ab0d | 626 | } |
97ab0569 | 627 | |
8f4f4634 MT |
628 | sub get_protocol_options { |
629 | my $hash = shift; | |
630 | my $key = shift; | |
631 | my $protocol = shift; | |
d7a14d01 | 632 | my $nat_options_wanted = shift; |
8f4f4634 MT |
633 | my @options = (); |
634 | ||
d7a14d01 MT |
635 | # Nothing to do if no protocol is specified. |
636 | if ($protocol eq "all") { | |
637 | return @options; | |
638 | } else { | |
639 | push(@options, ("-p", $protocol)); | |
640 | } | |
641 | ||
fcc68a42 MT |
642 | if ($protocol ~~ @PROTOCOLS_WITH_PORTS) { |
643 | # Process source ports. | |
644 | my $use_src_ports = ($$hash{$key}[7] eq "ON"); | |
645 | my $src_ports = $$hash{$key}[10]; | |
8f4f4634 | 646 | |
fcc68a42 MT |
647 | if ($use_src_ports && $src_ports) { |
648 | push(@options, &format_ports($src_ports, "src")); | |
649 | } | |
8f4f4634 | 650 | |
fcc68a42 MT |
651 | # Process destination ports. |
652 | my $use_dst_ports = ($$hash{$key}[11] eq "ON"); | |
653 | my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat")); | |
8f4f4634 | 654 | |
fcc68a42 MT |
655 | if ($use_dst_ports) { |
656 | my $dst_ports_mode = $$hash{$key}[14]; | |
657 | my $dst_ports = $$hash{$key}[15]; | |
8f4f4634 | 658 | |
fcc68a42 MT |
659 | if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) { |
660 | if ($nat_options_wanted && $use_dnat && $$hash{$key}[30]) { | |
661 | $dst_ports = $$hash{$key}[30]; | |
662 | } | |
8f4f4634 | 663 | push(@options, &format_ports($dst_ports, "dst")); |
8f4f4634 | 664 | |
fcc68a42 MT |
665 | } elsif ($dst_ports_mode eq "cust_srv") { |
666 | if ($protocol eq "ICMP") { | |
667 | push(@options, ("--icmp-type", &fwlib::get_srv_port($dst_ports, 3, "ICMP"))); | |
668 | } else { | |
669 | $dst_ports = &fwlib::get_srv_port($dst_ports, 1, uc($protocol)); | |
670 | push(@options, &format_ports($dst_ports, "dst")); | |
671 | } | |
672 | ||
673 | } elsif ($dst_ports_mode eq "cust_srvgrp") { | |
674 | push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol))); | |
675 | } | |
2a81ab0d AM |
676 | } |
677 | } | |
8f4f4634 MT |
678 | |
679 | # Check if a single ICMP type is selected. | |
fcc68a42 | 680 | if ($protocol eq "icmp") { |
8f4f4634 MT |
681 | my $icmp_type = $$hash{$key}[9]; |
682 | ||
683 | if (($icmp_type ne "All ICMP-Types") && $icmp_type) { | |
684 | push(@options, ("--icmp-type", $icmp_type)); | |
a4c7bf6b AM |
685 | } |
686 | } | |
8f4f4634 MT |
687 | |
688 | return @options; | |
689 | } | |
690 | ||
691 | sub format_ports { | |
692 | my $ports = shift; | |
693 | my $type = shift; | |
694 | ||
695 | my $arg; | |
696 | if ($type eq "src") { | |
697 | $arg = "--sport"; | |
698 | } elsif ($type eq "dst") { | |
699 | $arg = "--dport"; | |
700 | } | |
701 | ||
702 | my @options = (); | |
703 | ||
704 | if ($ports =~ /\|/) { | |
705 | $ports =~ s/\|/,/g; | |
706 | push(@options, ("-m", "multiport")); | |
707 | } | |
708 | ||
1c3044d7 MT |
709 | if ($ports) { |
710 | push(@options, ($arg, $ports)); | |
711 | } | |
8f4f4634 MT |
712 | |
713 | return @options; | |
714 | } | |
715 | ||
716 | sub get_dnat_target_port { | |
717 | my $hash = shift; | |
718 | my $key = shift; | |
719 | ||
720 | if ($$hash{$key}[14] eq "TGT_PORT") { | |
1c3044d7 MT |
721 | my $port = $$hash{$key}[15]; |
722 | my $external_port = $$hash{$key}[30]; | |
723 | ||
724 | if ($external_port && ($port ne $external_port)) { | |
725 | return $$hash{$key}[15]; | |
726 | } | |
8f4f4634 | 727 | } |
2a81ab0d | 728 | } |
6e87f0aa MT |
729 | |
730 | sub add_dnat_mangle_rules { | |
731 | my $nat_address = shift; | |
48f07c19 | 732 | my $interface = shift; |
6e87f0aa MT |
733 | my @options = @_; |
734 | ||
735 | my $mark = 0; | |
736 | foreach my $zone ("GREEN", "BLUE", "ORANGE") { | |
737 | $mark++; | |
738 | ||
739 | # Skip rule if not all required information exists. | |
740 | next unless (exists $defaultNetworks{$zone . "_NETADDRESS"}); | |
741 | next unless (exists $defaultNetworks{$zone . "_NETMASK"}); | |
742 | ||
48f07c19 AM |
743 | next if ($interface && $interface ne $defaultNetworks{$zone . "_DEV"}); |
744 | ||
6e87f0aa MT |
745 | my @mangle_options = @options; |
746 | ||
747 | my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"}; | |
748 | $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"}; | |
749 | ||
750 | push(@mangle_options, ("-s", $netaddress, "-d", $nat_address)); | |
751 | push(@mangle_options, ("-j", "MARK", "--set-mark", $mark)); | |
752 | ||
753 | run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options"); | |
754 | } | |
755 | } | |
3bb4bb3f MT |
756 | |
757 | sub make_log_limit_options { | |
758 | my @options = ("-m", "limit"); | |
759 | ||
760 | # Maybe we should get this from the configuration. | |
761 | my $limit = 10; | |
762 | ||
763 | # We limit log messages to $limit messages per minute. | |
764 | push(@options, ("--limit", "$limit/min")); | |
765 | ||
766 | # And we allow bursts of 2x $limit. | |
767 | push(@options, ("--limit-burst", $limit * 2)); | |
768 | ||
769 | return @options; | |
770 | } | |
e9b5ba41 | 771 | |
da7a2208 MT |
772 | sub firewall_is_in_subnet { |
773 | my $subnet = shift; | |
5cf8c8c1 | 774 | |
e9b5ba41 MT |
775 | # ORANGE is missing here, because nothing may ever access |
776 | # the firewall from this network. | |
4e54e3c6 | 777 | my $address = &fwlib::get_internal_firewall_ip_address($subnet, 0); |
e9b5ba41 | 778 | |
da7a2208 MT |
779 | if ($address) { |
780 | return 1; | |
e9b5ba41 | 781 | } |
da7a2208 MT |
782 | |
783 | return 0; | |
e9b5ba41 | 784 | } |