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