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