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