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