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