]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/firewall/rules.pl
Add support for generating GeoIP-based firewall rules.
[people/pmueller/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) 2013 Alexander Marx <amarx@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
24 require '/var/ipfire/general-functions.pl';
25 require "${General::swroot}/lang.pl";
26 require "/usr/lib/firewall/firewall-lib.pl";
27
28 # Set to one to enable debugging mode.
29 my $DEBUG = 0;
30
31 my $IPTABLES = "iptables --wait";
32
33 # iptables chains
34 my $CHAIN_INPUT = "INPUTFW";
35 my $CHAIN_FORWARD = "FORWARDFW";
36 my $CHAIN_OUTPUT = "OUTGOINGFW";
37 my $CHAIN = $CHAIN_FORWARD;
38 my $CHAIN_NAT_SOURCE = "NAT_SOURCE";
39 my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION";
40 my $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION";
41 my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT);
42 my @ANY_ADDRESSES = ("0.0.0.0/0.0.0.0", "0.0.0.0/0", "0/0");
43
44 my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
45 my @PROTOCOLS_WITH_PORTS = ("tcp", "udp");
46
47 my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT");
48
49 my %fwdfwsettings=();
50 my %fwoptions = ();
51 my %defaultNetworks=();
52 my %configfwdfw=();;
53 my %customgrp=();
54 my %configinputfw=();
55 my %configoutgoingfw=();
56 my %confignatfw=();
57 my @p2ps=();
58
59 my $configfwdfw = "${General::swroot}/firewall/config";
60 my $configinput = "${General::swroot}/firewall/input";
61 my $configoutgoing = "${General::swroot}/firewall/outgoing";
62 my $p2pfile = "${General::swroot}/firewall/p2protocols";
63 my $geoipfile = "${General::swroot}/firewall/geoipblock";
64 my $configgrp = "${General::swroot}/fwhosts/customgroups";
65 my $netsettings = "${General::swroot}/ethernet/settings";
66
67 &General::readhash("${General::swroot}/firewall/settings", \%fwdfwsettings);
68 &General::readhash("${General::swroot}/optionsfw/settings", \%fwoptions);
69 &General::readhash("$netsettings", \%defaultNetworks);
70 &General::readhasharray($configfwdfw, \%configfwdfw);
71 &General::readhasharray($configinput, \%configinputfw);
72 &General::readhasharray($configoutgoing, \%configoutgoingfw);
73 &General::readhasharray($configgrp, \%customgrp);
74
75 my @log_limit_options = &make_log_limit_options();
76
77 my $POLICY_INPUT_ALLOWED = 0;
78 my $POLICY_FORWARD_ALLOWED = ($fwdfwsettings{"POLICY"} eq "MODE2");
79 my $POLICY_OUTPUT_ALLOWED = ($fwdfwsettings{"POLICY1"} eq "MODE2");
80
81 my $POLICY_INPUT_ACTION = $fwoptions{"FWPOLICY2"};
82 my $POLICY_FORWARD_ACTION = $fwoptions{"FWPOLICY"};
83 my $POLICY_OUTPUT_ACTION = $fwoptions{"FWPOLICY1"};
84
85 # MAIN
86 &main();
87
88 sub main {
89 # Flush all chains.
90 &flush();
91
92 # Reload firewall rules.
93 &preparerules();
94
95 # Load P2P block rules.
96 &p2pblock();
97
98 # Load GeoIP block rules.
99 &geoipblock();
100
101 # Reload firewall policy.
102 run("/usr/sbin/firewall-policy");
103 }
104
105 sub 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";
113
114 if ($?) {
115 print_error("ERROR: $command");
116 }
117 }
118 }
119
120 sub print_error {
121 my $message = shift;
122
123 print STDERR "$message\n";
124 }
125
126 sub 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
138 sub count_elements {
139 my $hash = shift;
140
141 return scalar @$hash;
142 }
143
144 sub flush {
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");
150 run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
151 }
152
153 sub preparerules {
154 if (! -z "${General::swroot}/firewall/input"){
155 &buildrules(\%configinputfw);
156 }
157 if (! -z "${General::swroot}/firewall/outgoing"){
158 &buildrules(\%configoutgoingfw);
159 }
160 if (! -z "${General::swroot}/firewall/config"){
161 &buildrules(\%configfwdfw);
162 }
163 }
164
165 sub buildrules {
166 my $hash = shift;
167
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
195 foreach my $key (sort {$a <=> $b} keys %$hash) {
196 # Skip disabled rules.
197 next unless ($$hash{$key}[2] eq 'ON');
198
199 # Count number of elements in this line
200 my $elements = &count_elements($$hash{$key});
201
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.
221 my @sources = &fwlib::get_addresses($hash, $key, "src");
222
223 # Collect all destinations.
224 my @destinations = &fwlib::get_addresses($hash, $key, "tgt");
225
226 # True if the destination is the firewall itself.
227 my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire");
228
229 # Check if logging should be enabled.
230 my $LOG = ($$hash{$key}[17] eq 'ON');
231
232 # Check if NAT is enabled and initialize variables, that we use for that.
233 my $NAT = ($$hash{$key}[28] eq 'ON');
234 my $NAT_MODE;
235 if ($NAT) {
236 $NAT_MODE = uc($$hash{$key}[31]);
237 }
238
239 # Set up time constraints.
240 my @time_options = ();
241 if ($$hash{$key}[18] eq 'ON') {
242 push(@time_options, ("-m", "time"));
243
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 }
270
271 # Convert start time.
272 my $time_start = &format_time($$hash{$key}[26]);
273 if ($time_start) {
274 push(@time_options, ("--timestart", $time_start));
275 }
276
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 }
282 }
283
284 # Concurrent connection limit
285 my @ratelimit_options = ();
286
287 if (($elements ge 34) && ($$hash{$key}[32] eq 'ON')) {
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
303 if (($elements ge 37) && ($$hash{$key}[34] eq 'ON')) {
304 my $rate_limit = "$$hash{$key}[35]/$$hash{$key}[36]";
305
306 if ($rate_limit) {
307 push(@ratelimit_options, ("-m", "limit"));
308 push(@ratelimit_options, ("--limit", $rate_limit));
309 }
310 }
311
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;
325 }
326
327 # Prepare protocol options (like ICMP types, ports, etc...).
328 my @protocol_options = &get_protocol_options($hash, $key, $protocol, 0);
329
330 # Check if this protocol knows ports.
331 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
332
333 foreach my $src (@sources) {
334 # Skip invalid source.
335 next unless (defined $src);
336 next unless ($src);
337
338 # Sanitize source.
339 my $source = @$src[0];
340 if ($source ~~ @ANY_ADDRESSES) {
341 $source = "";
342 }
343
344 my $source_intf = @$src[1];
345
346 foreach my $dst (@destinations) {
347 # Skip invalid rules.
348 next unless (defined $dst);
349 next if (!$dst || ($dst eq "none"));
350
351 # Sanitize destination.
352 my $destination = @$dst[0];
353 if ($destination ~~ @ANY_ADDRESSES) {
354 $destination = "";
355 }
356
357 my $destination_intf = @$dst[1];
358
359 # Array with iptables arguments.
360 my @options = ();
361
362 # Append protocol.
363 if ($protocol ne "all") {
364 push(@options, @protocol_options);
365 }
366
367 # Prepare source options.
368 my @source_options = ();
369 if ($source =~ /mac/) {
370 push(@source_options, $source);
371 } elsif ($source =~ /-m geoip/) {
372 push(@source_options, $source);
373 } elsif($source) {
374 push(@source_options, ("-s", $source));
375 }
376
377 # Prepare destination options.
378 my @destination_options = ();
379 if ($destination =~ /-m geoip/) {
380 push(@destination_options, $destination);
381 } elsif ($destination) {
382 push(@destination_options, ("-d", $destination));
383 }
384
385 # Add time constraint options.
386 push(@options, @time_options);
387
388 # Add ratelimiting option
389 push(@options, @ratelimit_options);
390
391 my $firewall_is_in_source_subnet = 1;
392 if ($source) {
393 $firewall_is_in_source_subnet = &firewall_is_in_subnet($source);
394 }
395
396 my $firewall_is_in_destination_subnet = 1;
397 if ($destination) {
398 $firewall_is_in_destination_subnet = &firewall_is_in_subnet($destination);
399 }
400
401 # Process NAT rules.
402 if ($NAT) {
403 my $nat_address = &fwlib::get_nat_address($$hash{$key}[29], $source);
404
405 # Skip NAT rules if the NAT address is unknown
406 # (i.e. no internet connection has been established, yet).
407 next unless ($nat_address);
408
409 # Destination NAT
410 if ($NAT_MODE eq "DNAT") {
411 my @nat_options = ();
412 if ($protocol ne "all") {
413 my @nat_protocol_options = &get_protocol_options($hash, $key, $protocol, 1);
414 push(@nat_options, @nat_protocol_options);
415 }
416 push(@nat_options, @time_options);
417
418 # Make port-forwardings useable from the internal networks.
419 my @internal_addresses = &fwlib::get_internal_firewall_ip_addresses(1);
420 unless ($nat_address ~~ @internal_addresses) {
421 &add_dnat_mangle_rules($nat_address, $source_intf, @nat_options);
422 }
423
424 push(@nat_options, @source_options);
425 push(@nat_options, ("-d", $nat_address));
426
427 my $dnat_port;
428 if ($protocol_has_ports) {
429 $dnat_port = &get_dnat_target_port($hash, $key);
430 }
431
432 my @nat_action_options = ();
433
434 # Use iptables REDIRECT
435 my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports && $dnat_port);
436 if ($use_redirect) {
437 push(@nat_action_options, ("-j", "REDIRECT", "--to-ports", $dnat_port));
438
439 # Use iptables DNAT
440 } else {
441 if ($destination_is_firewall && !$destination) {
442 $destination = &fwlib::get_external_address();
443 }
444 next unless ($destination);
445
446 my ($dnat_address, $dnat_mask) = split("/", $destination);
447 @destination_options = ("-d", $dnat_address);
448
449 if ($protocol_has_ports) {
450 my $dnat_port = &get_dnat_target_port($hash, $key);
451
452 if ($dnat_port) {
453 $dnat_address .= ":$dnat_port";
454 }
455 }
456
457 push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address));
458 }
459
460 if ($LOG) {
461 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '");
462 }
463 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options");
464
465 # Source NAT
466 } elsif ($NAT_MODE eq "SNAT") {
467 my @nat_options = @options;
468
469 push(@nat_options, @source_options);
470 push(@nat_options, @destination_options);
471
472 if ($LOG) {
473 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '");
474 }
475 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
476 }
477 }
478
479 # Add source and destination interface to the filter rules.
480 # These are supposed to help filtering forged packets that originate
481 # from BLUE with an IP address from GREEN for instance.
482 if ($source_intf) {
483 push(@source_options, ("-i", $source_intf));
484 }
485
486 if ($destination_intf) {
487 push(@destination_options, ("-o", $destination_intf));
488 }
489
490 push(@options, @source_options);
491 push(@options, @destination_options);
492
493 # Insert firewall rule.
494 if ($LOG && !$NAT) {
495 run("$IPTABLES -A $chain @options @log_limit_options -j LOG --log-prefix '$chain '");
496 }
497 run("$IPTABLES -A $chain @options -j $target");
498
499 # Handle forwarding rules and add corresponding rules for firewall access.
500 if ($chain eq $CHAIN_FORWARD) {
501 # If the firewall is part of the destination subnet and access to the destination network
502 # is granted/forbidden for any network that the firewall itself is part of, we grant/forbid access
503 # for the firewall, too.
504 if ($firewall_is_in_destination_subnet && ($target ~~ @special_input_targets)) {
505 if ($LOG && !$NAT) {
506 run("$IPTABLES -A $CHAIN_INPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '");
507 }
508 run("$IPTABLES -A $CHAIN_INPUT @options -j $target");
509 }
510
511 # Likewise.
512 if ($firewall_is_in_source_subnet && ($target ~~ @special_output_targets)) {
513 if ($LOG && !$NAT) {
514 run("$IPTABLES -A $CHAIN_OUTPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_OUTPUT '");
515 }
516 run("$IPTABLES -A $CHAIN_OUTPUT @options -j $target");
517 }
518 }
519 }
520 }
521 }
522 }
523 #Reload firewall.local if present
524 if ( -f '/etc/sysconfig/firewall.local'){
525 run("/etc/sysconfig/firewall.local reload");
526 }
527 }
528
529 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
530 sub format_time {
531 my $val = shift;
532
533 # Convert the given time into minutes.
534 my $minutes = &time_convert_to_minutes($val);
535
536 # Move the timestamp into UTC.
537 $minutes += &time_utc_offset();
538
539 # Make sure $minutes is between 00:00 and 23:59.
540 if ($minutes < 0) {
541 $minutes += 1440;
542 }
543
544 if ($minutes > 1440) {
545 $minutes -= 1440;
546 }
547
548 # Format as hh:mm.
549 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
550 }
551
552 # Calculates the offsets in minutes from the local timezone to UTC.
553 sub time_utc_offset {
554 my @localtime = localtime(time);
555 my @gmtime = gmtime(time);
556
557 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
558 }
559
560 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
561 sub time_convert_to_minutes {
562 my ($hrs, $min) = split(":", shift);
563
564 return ($hrs * 60) + $min;
565 }
566
567 sub p2pblock {
568 open(FILE, "<$p2pfile") or die "Unable to read $p2pfile";
569 my @protocols = ();
570 foreach my $p2pentry (<FILE>) {
571 my @p2pline = split(/\;/, $p2pentry);
572 next unless ($p2pline[2] eq "off");
573
574 push(@protocols, "--$p2pline[1]");
575 }
576 close(FILE);
577
578 run("$IPTABLES -F P2PBLOCK");
579 if (@protocols) {
580 run("$IPTABLES -A P2PBLOCK -m ipp2p @protocols -j DROP");
581 }
582 }
583
584 sub geoipblock {
585 my %geoipsettings = ();
586
587 # Check if the geoip settings file exists
588 if (-e "$geoipfile") {
589 # Read settings file
590 &General::readhash("$geoipfile", \%geoipsettings);
591 } else {
592 # Exit submodule, go on processing the remaining script
593 return;
594 }
595
596 # If geoip blocking is not enabled, we are finished here.
597 if ($geoipsettings{'GEOIPBLOCK_ENABLED'} ne "on") {
598 # Exit submodule. Process remaining script.
599 return;
600 }
601
602 # Get supported locations.
603 my @locations = &fwlib::get_geoip_locations();
604
605 # Create iptables chain.
606 run("$IPTABLES -F GEOIPBLOCK");
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($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 minute.
809 push(@options, ("--limit", "$limit/min"));
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 }