]> git.ipfire.org Git - ipfire-2.x.git/blob - config/firewall/rules.pl
firewall/rules.pl: Make geoipsettings hash and locations array
[ipfire-2.x.git] / config / firewall / rules.pl
1 #!/usr/bin/perl -w
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2019 IPFire Team <info@ipfire.org> #
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 ###############################################################################
21
22 use strict;
23 use experimental 'smartmatch';
24
25 require '/var/ipfire/general-functions.pl';
26 require "${General::swroot}/lang.pl";
27 require "/usr/lib/firewall/firewall-lib.pl";
28
29 # Set to one to enable debugging mode.
30 my $DEBUG = 0;
31
32 my $IPTABLES = "iptables --wait";
33
34 # iptables chains
35 my $CHAIN_INPUT = "INPUTFW";
36 my $CHAIN_FORWARD = "FORWARDFW";
37 my $CHAIN_OUTPUT = "OUTGOINGFW";
38 my $CHAIN = $CHAIN_FORWARD;
39 my $CHAIN_NAT_SOURCE = "NAT_SOURCE";
40 my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION";
41 my $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION";
42 my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT);
43 my @ANY_ADDRESSES = ("0.0.0.0/0.0.0.0", "0.0.0.0/0", "0/0");
44
45 my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
46 my @PROTOCOLS_WITH_PORTS = ("tcp", "udp");
47
48 my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT");
49
50 my %fwdfwsettings=();
51 my %fwoptions = ();
52 my %defaultNetworks=();
53 my %configfwdfw=();;
54 my %customgrp=();
55 my %configinputfw=();
56 my %configoutgoingfw=();
57 my %confignatfw=();
58 my %geoipsettings = (
59 "GEOIPBLOCK_ENABLED" => "off"
60 );
61
62 my @p2ps=();
63
64 my $configfwdfw = "${General::swroot}/firewall/config";
65 my $configinput = "${General::swroot}/firewall/input";
66 my $configoutgoing = "${General::swroot}/firewall/outgoing";
67 my $p2pfile = "${General::swroot}/firewall/p2protocols";
68 my $geoipfile = "${General::swroot}/firewall/geoipblock";
69 my $configgrp = "${General::swroot}/fwhosts/customgroups";
70 my $netsettings = "${General::swroot}/ethernet/settings";
71
72 &General::readhash("${General::swroot}/firewall/settings", \%fwdfwsettings);
73 &General::readhash("${General::swroot}/optionsfw/settings", \%fwoptions);
74 &General::readhash("$netsettings", \%defaultNetworks);
75 &General::readhasharray($configfwdfw, \%configfwdfw);
76 &General::readhasharray($configinput, \%configinputfw);
77 &General::readhasharray($configoutgoing, \%configoutgoingfw);
78 &General::readhasharray($configgrp, \%customgrp);
79
80 # Check if the geoip settings file exists
81 if (-e "$geoipfile") {
82 # Read settings file
83 &General::readhash("$geoipfile", \%geoipsettings);
84 }
85
86 # Get all GeoIP locations.
87 my @locations = &fwlib::get_geoip_locations();
88
89 my @log_limit_options = &make_log_limit_options();
90
91 my $POLICY_INPUT_ALLOWED = 0;
92 my $POLICY_FORWARD_ALLOWED = ($fwdfwsettings{"POLICY"} eq "MODE2");
93 my $POLICY_OUTPUT_ALLOWED = ($fwdfwsettings{"POLICY1"} eq "MODE2");
94
95 my $POLICY_INPUT_ACTION = $fwoptions{"FWPOLICY2"};
96 my $POLICY_FORWARD_ACTION = $fwoptions{"FWPOLICY"};
97 my $POLICY_OUTPUT_ACTION = $fwoptions{"FWPOLICY1"};
98
99 # MAIN
100 &main();
101
102 sub main {
103 # Flush all chains.
104 &flush();
105
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 }
116
117 # Load P2P block rules.
118 &p2pblock();
119
120 # Load GeoIP block rules.
121 &geoipblock();
122
123 # Reload firewall policy.
124 run("/usr/sbin/firewall-policy");
125
126 #Reload firewall.local if present
127 if ( -f '/etc/sysconfig/firewall.local'){
128 run("/etc/sysconfig/firewall.local reload");
129 }
130 }
131
132 sub 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";
140
141 if ($?) {
142 print_error("ERROR: $command");
143 }
144 }
145 }
146
147 sub print_error {
148 my $message = shift;
149
150 print STDERR "$message\n";
151 }
152
153 sub 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
165 sub count_elements {
166 my $hash = shift;
167
168 return scalar @$hash;
169 }
170
171 sub flush {
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");
177 run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
178 }
179
180 sub buildrules {
181 my $hash = shift;
182
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") {
192 push(@special_input_targets, ("ACCEPT", "REJECT"));
193 } elsif ($POLICY_INPUT_ACTION eq "REJECT") {
194 push(@special_input_targets, ("ACCEPT", "DROP"));
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") {
204 push(@special_output_targets, ("ACCEPT", "REJECT"));
205 } elsif ($POLICY_OUTPUT_ACTION eq "REJECT") {
206 push(@special_output_targets, ("ACCEPT", "DROP"));
207 }
208 }
209
210 foreach my $key (sort {$a <=> $b} keys %$hash) {
211 # Skip disabled rules.
212 next unless ($$hash{$key}[2] eq 'ON');
213
214 # Count number of elements in this line
215 my $elements = &count_elements($$hash{$key});
216
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.
236 my @sources = &fwlib::get_addresses($hash, $key, "src");
237
238 # Collect all destinations.
239 my @destinations = &fwlib::get_addresses($hash, $key, "tgt");
240
241 # True if the destination is the firewall itself.
242 my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire");
243
244 # Check if logging should be enabled.
245 my $LOG = ($$hash{$key}[17] eq 'ON');
246
247 # Check if NAT is enabled and initialize variables, that we use for that.
248 my $NAT = ($$hash{$key}[28] eq 'ON');
249 my $NAT_MODE;
250 if ($NAT) {
251 $NAT_MODE = uc($$hash{$key}[31]);
252 }
253
254 # Set up time constraints.
255 my @time_options = ();
256 if ($$hash{$key}[18] eq 'ON') {
257 push(@time_options, ("-m", "time"));
258
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 }
285
286 # Convert start time.
287 my $time_start = &format_time($$hash{$key}[26]);
288 if ($time_start) {
289 push(@time_options, ("--timestart", $time_start));
290 }
291
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 }
297 }
298
299 # Concurrent connection limit
300 my @ratelimit_options = ();
301
302 if (($elements ge 34) && ($$hash{$key}[32] eq 'ON')) {
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
318 if (($elements ge 37) && ($$hash{$key}[34] eq 'ON')) {
319 my $rate_limit = "$$hash{$key}[35]/$$hash{$key}[36]";
320
321 if ($rate_limit) {
322 push(@ratelimit_options, ("-m", "limit"));
323 push(@ratelimit_options, ("--limit", $rate_limit));
324 }
325 }
326
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;
340 }
341
342 # Prepare protocol options (like ICMP types, ports, etc...).
343 my @protocol_options = &get_protocol_options($hash, $key, $protocol, 0);
344
345 # Check if this protocol knows ports.
346 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
347
348 foreach my $src (@sources) {
349 # Skip invalid source.
350 next unless (defined $src);
351 next unless ($src);
352
353 # Sanitize source.
354 my $source = @$src[0];
355 if ($source ~~ @ANY_ADDRESSES) {
356 $source = "";
357 }
358
359 my $source_intf = @$src[1];
360
361 foreach my $dst (@destinations) {
362 # Skip invalid rules.
363 next unless (defined $dst);
364 next if (!$dst || ($dst eq "none"));
365
366 # Sanitize destination.
367 my $destination = @$dst[0];
368 if ($destination ~~ @ANY_ADDRESSES) {
369 $destination = "";
370 }
371
372 my $destination_intf = @$dst[1];
373
374 # Array with iptables arguments.
375 my @options = ();
376
377 # Append protocol.
378 if ($protocol ne "all") {
379 push(@options, @protocol_options);
380 }
381
382 # Prepare source options.
383 my @source_options = ();
384 if ($source =~ /mac/) {
385 push(@source_options, $source);
386 } elsif ($source =~ /-m geoip/) {
387 push(@source_options, $source);
388 } elsif($source) {
389 push(@source_options, ("-s", $source));
390 }
391
392 # Prepare destination options.
393 my @destination_options = ();
394 if ($destination =~ /-m geoip/) {
395 push(@destination_options, $destination);
396 } elsif ($destination) {
397 push(@destination_options, ("-d", $destination));
398 }
399
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
413 # Add time constraint options.
414 push(@options, @time_options);
415
416 # Add ratelimiting option
417 push(@options, @ratelimit_options);
418
419 my $firewall_is_in_source_subnet = 1;
420 if ($source) {
421 $firewall_is_in_source_subnet = &firewall_is_in_subnet($source);
422 }
423
424 my $firewall_is_in_destination_subnet = 1;
425 if ($destination) {
426 $firewall_is_in_destination_subnet = &firewall_is_in_subnet($destination);
427 }
428
429 # Process NAT rules.
430 if ($NAT) {
431 my $nat_address = &fwlib::get_nat_address($$hash{$key}[29], $source);
432
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);
436
437 # Destination NAT
438 if ($NAT_MODE eq "DNAT") {
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 }
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) {
449 &add_dnat_mangle_rules($nat_address, $source_intf, @nat_options);
450 }
451
452 push(@nat_options, @source_options);
453 push(@nat_options, ("-d", $nat_address));
454
455 my $dnat_port;
456 if ($protocol_has_ports) {
457 $dnat_port = &get_dnat_target_port($hash, $key);
458 }
459
460 my @nat_action_options = ();
461
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 {
469 if ($destination_is_firewall && !$destination) {
470 $destination = &fwlib::get_external_address();
471 }
472 next unless ($destination);
473
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 }
483 }
484
485 push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address));
486 }
487
488 if ($LOG) {
489 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '");
490 }
491 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options");
492
493 # Source NAT
494 } elsif ($NAT_MODE eq "SNAT") {
495 my @nat_options = @options;
496
497 push(@nat_options, @destination_intf_options);
498 push(@nat_options, @source_options);
499 push(@nat_options, @destination_options);
500
501 if ($LOG) {
502 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '");
503 }
504 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
505 }
506 }
507
508 push(@options, @source_options);
509 push(@options, @destination_options);
510
511 # Insert firewall rule.
512 if ($LOG && !$NAT) {
513 run("$IPTABLES -A $chain @options @source_intf_options @destination_intf_options @log_limit_options -j LOG --log-prefix '$chain '");
514 }
515 run("$IPTABLES -A $chain @options @source_intf_options @destination_intf_options -j $target");
516
517 # Handle forwarding rules and add corresponding rules for firewall access.
518 if ($chain eq $CHAIN_FORWARD) {
519 # If the firewall is part of the destination subnet and access to the destination network
520 # is granted/forbidden for any network that the firewall itself is part of, we grant/forbid access
521 # for the firewall, too.
522 if ($firewall_is_in_destination_subnet && ($target ~~ @special_input_targets)) {
523 if ($LOG && !$NAT) {
524 run("$IPTABLES -A $CHAIN_INPUT @options @source_intf_options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '");
525 }
526 run("$IPTABLES -A $CHAIN_INPUT @options @source_intf_options -j $target");
527 }
528
529 # Likewise.
530 if ($firewall_is_in_source_subnet && ($target ~~ @special_output_targets)) {
531 if ($LOG && !$NAT) {
532 run("$IPTABLES -A $CHAIN_OUTPUT @options @destination_intf_options @log_limit_options -j LOG --log-prefix '$CHAIN_OUTPUT '");
533 }
534 run("$IPTABLES -A $CHAIN_OUTPUT @options @destination_intf_options -j $target");
535 }
536 }
537 }
538 }
539 }
540 }
541 }
542
543 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
544 sub format_time {
545 my $val = shift;
546
547 # Convert the given time into minutes.
548 my $minutes = &time_convert_to_minutes($val);
549
550 # Move the timestamp into UTC.
551 $minutes += &time_utc_offset();
552
553 # Make sure $minutes is between 00:00 and 23:59.
554 if ($minutes < 0) {
555 $minutes += 1440;
556 }
557
558 if ($minutes > 1440) {
559 $minutes -= 1440;
560 }
561
562 # Format as hh:mm.
563 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
564 }
565
566 # Calculates the offsets in minutes from the local timezone to UTC.
567 sub time_utc_offset {
568 my @localtime = localtime(time);
569 my @gmtime = gmtime(time);
570
571 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
572 }
573
574 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
575 sub time_convert_to_minutes {
576 my ($hrs, $min) = split(":", shift);
577
578 return ($hrs * 60) + $min;
579 }
580
581 sub p2pblock {
582 open(FILE, "<$p2pfile") or die "Unable to read $p2pfile";
583 my @protocols = ();
584 foreach my $p2pentry (<FILE>) {
585 my @p2pline = split(/\;/, $p2pentry);
586 next unless ($p2pline[2] eq "off");
587
588 push(@protocols, "--$p2pline[1]");
589 }
590 close(FILE);
591
592 run("$IPTABLES -F P2PBLOCK");
593 if (@protocols) {
594 run("$IPTABLES -A P2PBLOCK -m ipp2p @protocols -j DROP");
595 }
596 }
597
598 sub geoipblock {
599 # Flush iptables chain.
600 run("$IPTABLES -F GEOIPBLOCK");
601
602 # If geoip blocking is not enabled, we are finished here.
603 if ($geoipsettings{'GEOIPBLOCK_ENABLED'} ne "on") {
604 # Exit submodule. Process remaining script.
605 return;
606 }
607
608 # Loop through all supported geoip locations and
609 # create iptables rules, if blocking this country
610 # is enabled.
611 foreach my $location (@locations) {
612 if(exists $geoipsettings{$location} && $geoipsettings{$location} eq "on") {
613 run("$IPTABLES -A GEOIPBLOCK -m geoip --src-cc $location -j DROP");
614 }
615 }
616 }
617
618 sub get_protocols {
619 my $hash = shift;
620 my $key = shift;
621
622 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
623 my $uses_services = ($$hash{$key}[11] eq "ON");
624
625 my @protocols = ();
626
627 # Rules which don't have source ports or services (like ICMP, ESP, ...).
628 if (!$uses_source_ports && !$uses_services) {
629 push(@protocols, $$hash{$key}[8]);
630
631 # Rules which either use ports or services.
632 } elsif ($uses_source_ports || $uses_services) {
633 # Check if service group or service
634 if ($$hash{$key}[14] eq 'cust_srv') {
635 push(@protocols, &fwlib::get_srv_prot($$hash{$key}[15]));
636
637 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
638 my $protos = &fwlib::get_srvgrp_prot($$hash{$key}[15]);
639 push(@protocols, split(",", $protos));
640
641 } else {
642 # Fetch the protocol for this rule.
643 my $protocol = lc($$hash{$key}[8]);
644
645 # Fetch source and destination ports for this rule.
646 my $source_ports = $$hash{$key}[10];
647 my $destination_ports = $$hash{$key}[15];
648
649 # Check if ports are set for protocols which do not support ports.
650 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
651 print_error("$protocol does not support ports");
652 return ();
653 }
654
655 push(@protocols, $protocol);
656 }
657 }
658
659 # Remove all empty elements
660 @protocols = map { $_ ? $_ : () } @protocols;
661
662 # If no protocol has been defined, we assume "all".
663 if (!@protocols) {
664 push(@protocols, "all");
665 }
666
667 # Make all protocol names lowercase.
668 @protocols = map { lc } @protocols;
669
670 return @protocols;
671 }
672
673 sub get_protocol_options {
674 my $hash = shift;
675 my $key = shift;
676 my $protocol = shift;
677 my $nat_options_wanted = shift;
678 my @options = ();
679
680 # Nothing to do if no protocol is specified.
681 if ($protocol eq "all") {
682 return @options;
683 } else {
684 push(@options, ("-p", $protocol));
685 }
686
687 if ($protocol ~~ @PROTOCOLS_WITH_PORTS) {
688 # Process source ports.
689 my $use_src_ports = ($$hash{$key}[7] eq "ON");
690 my $src_ports = $$hash{$key}[10];
691
692 if ($use_src_ports && $src_ports) {
693 push(@options, &format_ports($src_ports, "src"));
694 }
695
696 # Process destination ports.
697 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
698 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
699
700 if ($use_dst_ports) {
701 my $dst_ports_mode = $$hash{$key}[14];
702 my $dst_ports = $$hash{$key}[15];
703
704 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
705 if ($nat_options_wanted && $use_dnat && $$hash{$key}[30]) {
706 $dst_ports = $$hash{$key}[30];
707 }
708 push(@options, &format_ports($dst_ports, "dst"));
709
710 } elsif ($dst_ports_mode eq "cust_srv") {
711 if ($protocol eq "ICMP") {
712 push(@options, ("--icmp-type", &fwlib::get_srv_port($dst_ports, 3, "ICMP")));
713 } else {
714 $dst_ports = &fwlib::get_srv_port($dst_ports, 1, uc($protocol));
715 push(@options, &format_ports($dst_ports, "dst"));
716 }
717
718 } elsif ($dst_ports_mode eq "cust_srvgrp") {
719 push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol)));
720 }
721 }
722 }
723
724 # Check if a single ICMP type is selected.
725 if ($protocol eq "icmp") {
726 my $icmp_type = $$hash{$key}[9];
727
728 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
729 push(@options, ("--icmp-type", $icmp_type));
730 }
731 }
732
733 return @options;
734 }
735
736 sub format_ports {
737 my $ports = shift;
738 my $type = shift;
739
740 my $arg;
741 if ($type eq "src") {
742 $arg = "--sport";
743 } elsif ($type eq "dst") {
744 $arg = "--dport";
745 }
746
747 my @options = ();
748
749 if ($ports =~ /\|/) {
750 $ports =~ s/\|/,/g;
751 push(@options, ("-m", "multiport"));
752 }
753
754 if ($ports) {
755 push(@options, ($arg, $ports));
756 }
757
758 return @options;
759 }
760
761 sub get_dnat_target_port {
762 my $hash = shift;
763 my $key = shift;
764
765 if ($$hash{$key}[14] eq "TGT_PORT") {
766 my $port = $$hash{$key}[15];
767 my $external_port = $$hash{$key}[30];
768
769 if ($external_port && ($port ne $external_port)) {
770 return $$hash{$key}[15];
771 }
772 }
773 }
774
775 sub add_dnat_mangle_rules {
776 my $nat_address = shift;
777 my $interface = shift;
778 my @options = @_;
779
780 my $mark = 0;
781 foreach my $zone ("GREEN", "BLUE", "ORANGE") {
782 $mark++;
783
784 # Skip rule if not all required information exists.
785 next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
786 next unless (exists $defaultNetworks{$zone . "_NETMASK"});
787
788 next if ($interface && $interface ne $defaultNetworks{$zone . "_DEV"});
789
790 my @mangle_options = @options;
791
792 my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
793 $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
794
795 push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
796 push(@mangle_options, ("-j", "MARK", "--set-mark", $mark));
797
798 run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");
799 }
800 }
801
802 sub make_log_limit_options {
803 my @options = ("-m", "limit");
804
805 # Maybe we should get this from the configuration.
806 my $limit = 10;
807
808 # We limit log messages to $limit messages per second.
809 push(@options, ("--limit", "$limit/second"));
810
811 # And we allow bursts of 2x $limit.
812 push(@options, ("--limit-burst", $limit * 2));
813
814 return @options;
815 }
816
817 sub firewall_is_in_subnet {
818 my $subnet = shift;
819
820 # ORANGE is missing here, because nothing may ever access
821 # the firewall from this network.
822 my $address = &fwlib::get_internal_firewall_ip_address($subnet, 0);
823
824 if ($address) {
825 return 1;
826 }
827
828 return 0;
829 }