]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/firewall/rules.pl
Merge remote-tracking branch 'mfischer/slang' into next
[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 # 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 }
102
103 # Load P2P block rules.
104 &p2pblock();
105
106 # Load GeoIP block rules.
107 &geoipblock();
108
109 # Reload firewall policy.
110 run("/usr/sbin/firewall-policy");
111
112 #Reload firewall.local if present
113 if ( -f '/etc/sysconfig/firewall.local'){
114 run("/etc/sysconfig/firewall.local reload");
115 }
116 }
117
118 sub 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";
126
127 if ($?) {
128 print_error("ERROR: $command");
129 }
130 }
131 }
132
133 sub print_error {
134 my $message = shift;
135
136 print STDERR "$message\n";
137 }
138
139 sub 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
151 sub count_elements {
152 my $hash = shift;
153
154 return scalar @$hash;
155 }
156
157 sub flush {
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");
163 run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
164 }
165
166 sub buildrules {
167 my $hash = shift;
168
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
196 foreach my $key (sort {$a <=> $b} keys %$hash) {
197 # Skip disabled rules.
198 next unless ($$hash{$key}[2] eq 'ON');
199
200 # Count number of elements in this line
201 my $elements = &count_elements($$hash{$key});
202
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.
222 my @sources = &fwlib::get_addresses($hash, $key, "src");
223
224 # Collect all destinations.
225 my @destinations = &fwlib::get_addresses($hash, $key, "tgt");
226
227 # True if the destination is the firewall itself.
228 my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire");
229
230 # Check if logging should be enabled.
231 my $LOG = ($$hash{$key}[17] eq 'ON');
232
233 # Check if NAT is enabled and initialize variables, that we use for that.
234 my $NAT = ($$hash{$key}[28] eq 'ON');
235 my $NAT_MODE;
236 if ($NAT) {
237 $NAT_MODE = uc($$hash{$key}[31]);
238 }
239
240 # Set up time constraints.
241 my @time_options = ();
242 if ($$hash{$key}[18] eq 'ON') {
243 push(@time_options, ("-m", "time"));
244
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 }
271
272 # Convert start time.
273 my $time_start = &format_time($$hash{$key}[26]);
274 if ($time_start) {
275 push(@time_options, ("--timestart", $time_start));
276 }
277
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 }
283 }
284
285 # Concurrent connection limit
286 my @ratelimit_options = ();
287
288 if (($elements ge 34) && ($$hash{$key}[32] eq 'ON')) {
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
304 if (($elements ge 37) && ($$hash{$key}[34] eq 'ON')) {
305 my $rate_limit = "$$hash{$key}[35]/$$hash{$key}[36]";
306
307 if ($rate_limit) {
308 push(@ratelimit_options, ("-m", "limit"));
309 push(@ratelimit_options, ("--limit", $rate_limit));
310 }
311 }
312
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;
326 }
327
328 # Prepare protocol options (like ICMP types, ports, etc...).
329 my @protocol_options = &get_protocol_options($hash, $key, $protocol, 0);
330
331 # Check if this protocol knows ports.
332 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
333
334 foreach my $src (@sources) {
335 # Skip invalid source.
336 next unless (defined $src);
337 next unless ($src);
338
339 # Sanitize source.
340 my $source = @$src[0];
341 if ($source ~~ @ANY_ADDRESSES) {
342 $source = "";
343 }
344
345 my $source_intf = @$src[1];
346
347 foreach my $dst (@destinations) {
348 # Skip invalid rules.
349 next unless (defined $dst);
350 next if (!$dst || ($dst eq "none"));
351
352 # Sanitize destination.
353 my $destination = @$dst[0];
354 if ($destination ~~ @ANY_ADDRESSES) {
355 $destination = "";
356 }
357
358 my $destination_intf = @$dst[1];
359
360 # Array with iptables arguments.
361 my @options = ();
362
363 # Append protocol.
364 if ($protocol ne "all") {
365 push(@options, @protocol_options);
366 }
367
368 # Prepare source options.
369 my @source_options = ();
370 if ($source =~ /mac/) {
371 push(@source_options, $source);
372 } elsif ($source =~ /-m geoip/) {
373 push(@source_options, $source);
374 } elsif($source) {
375 push(@source_options, ("-s", $source));
376 }
377
378 # Prepare destination options.
379 my @destination_options = ();
380 if ($destination =~ /-m geoip/) {
381 push(@destination_options, $destination);
382 } elsif ($destination) {
383 push(@destination_options, ("-d", $destination));
384 }
385
386 # Add time constraint options.
387 push(@options, @time_options);
388
389 # Add ratelimiting option
390 push(@options, @ratelimit_options);
391
392 my $firewall_is_in_source_subnet = 1;
393 if ($source) {
394 $firewall_is_in_source_subnet = &firewall_is_in_subnet($source);
395 }
396
397 my $firewall_is_in_destination_subnet = 1;
398 if ($destination) {
399 $firewall_is_in_destination_subnet = &firewall_is_in_subnet($destination);
400 }
401
402 # Process NAT rules.
403 if ($NAT) {
404 my $nat_address = &fwlib::get_nat_address($$hash{$key}[29], $source);
405
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);
409
410 # Destination NAT
411 if ($NAT_MODE eq "DNAT") {
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 }
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) {
422 &add_dnat_mangle_rules($nat_address, $source_intf, @nat_options);
423 }
424
425 push(@nat_options, @source_options);
426 push(@nat_options, ("-d", $nat_address));
427
428 my $dnat_port;
429 if ($protocol_has_ports) {
430 $dnat_port = &get_dnat_target_port($hash, $key);
431 }
432
433 my @nat_action_options = ();
434
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 {
442 if ($destination_is_firewall && !$destination) {
443 $destination = &fwlib::get_external_address();
444 }
445 next unless ($destination);
446
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 }
456 }
457
458 push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address));
459 }
460
461 if ($LOG) {
462 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '");
463 }
464 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options");
465
466 # Source NAT
467 } elsif ($NAT_MODE eq "SNAT") {
468 my @nat_options = @options;
469
470 push(@nat_options, @source_options);
471 push(@nat_options, @destination_options);
472
473 if ($LOG) {
474 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '");
475 }
476 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
477 }
478 }
479
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
491 push(@options, @source_options);
492 push(@options, @destination_options);
493
494 # Insert firewall rule.
495 if ($LOG && !$NAT) {
496 run("$IPTABLES -A $chain @options @log_limit_options -j LOG --log-prefix '$chain '");
497 }
498 run("$IPTABLES -A $chain @options -j $target");
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 }
520 }
521 }
522 }
523 }
524 }
525
526 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
527 sub 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);
547 }
548
549 # Calculates the offsets in minutes from the local timezone to UTC.
550 sub 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);
555 }
556
557 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
558 sub time_convert_to_minutes {
559 my ($hrs, $min) = split(":", shift);
560
561 return ($hrs * 60) + $min;
562 }
563
564 sub p2pblock {
565 open(FILE, "<$p2pfile") or die "Unable to read $p2pfile";
566 my @protocols = ();
567 foreach my $p2pentry (<FILE>) {
568 my @p2pline = split(/\;/, $p2pentry);
569 next unless ($p2pline[2] eq "off");
570
571 push(@protocols, "--$p2pline[1]");
572 }
573 close(FILE);
574
575 run("$IPTABLES -F P2PBLOCK");
576 if (@protocols) {
577 run("$IPTABLES -A P2PBLOCK -m ipp2p @protocols -j DROP");
578 }
579 }
580
581 sub geoipblock {
582 my %geoipsettings = ();
583 $geoipsettings{'GEOIPBLOCK_ENABLED'} = "off";
584
585 # Flush iptables chain.
586 run("$IPTABLES -F GEOIPBLOCK");
587
588 # Check if the geoip settings file exists
589 if (-e "$geoipfile") {
590 # Read settings file
591 &General::readhash("$geoipfile", \%geoipsettings);
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
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
613 sub 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);
651 }
652 }
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");
660 }
661
662 # Make all protocol names lowercase.
663 @protocols = map { lc } @protocols;
664
665 return @protocols;
666 }
667
668 sub get_protocol_options {
669 my $hash = shift;
670 my $key = shift;
671 my $protocol = shift;
672 my $nat_options_wanted = shift;
673 my @options = ();
674
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
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];
686
687 if ($use_src_ports && $src_ports) {
688 push(@options, &format_ports($src_ports, "src"));
689 }
690
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"));
694
695 if ($use_dst_ports) {
696 my $dst_ports_mode = $$hash{$key}[14];
697 my $dst_ports = $$hash{$key}[15];
698
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 }
703 push(@options, &format_ports($dst_ports, "dst"));
704
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 }
716 }
717 }
718
719 # Check if a single ICMP type is selected.
720 if ($protocol eq "icmp") {
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));
725 }
726 }
727
728 return @options;
729 }
730
731 sub 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
749 if ($ports) {
750 push(@options, ($arg, $ports));
751 }
752
753 return @options;
754 }
755
756 sub get_dnat_target_port {
757 my $hash = shift;
758 my $key = shift;
759
760 if ($$hash{$key}[14] eq "TGT_PORT") {
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 }
767 }
768 }
769
770 sub add_dnat_mangle_rules {
771 my $nat_address = shift;
772 my $interface = shift;
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
783 next if ($interface && $interface ne $defaultNetworks{$zone . "_DEV"});
784
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 }
796
797 sub 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 }
811
812 sub firewall_is_in_subnet {
813 my $subnet = shift;
814
815 # ORANGE is missing here, because nothing may ever access
816 # the firewall from this network.
817 my $address = &fwlib::get_internal_firewall_ip_address($subnet, 0);
818
819 if ($address) {
820 return 1;
821 }
822
823 return 0;
824 }