]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/firewall/rules.pl
2ce31c9a256d6ec3a0fc63da87cc23221a10cf37
[people/teissler/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 = "OUTPUTFW";
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
43 my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
44 my @PROTOCOLS_WITH_PORTS = ("tcp", "udp");
45
46 my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT");
47
48 my %fwdfwsettings=();
49 my %defaultNetworks=();
50 my %configfwdfw=();;
51 my %customgrp=();
52 my %configinputfw=();
53 my %configoutgoingfw=();
54 my %confignatfw=();
55 my %aliases=();
56 my @p2ps=();
57
58 my $configfwdfw = "${General::swroot}/firewall/config";
59 my $configinput = "${General::swroot}/firewall/input";
60 my $configoutgoing = "${General::swroot}/firewall/outgoing";
61 my $p2pfile = "${General::swroot}/firewall/p2protocols";
62 my $configgrp = "${General::swroot}/fwhosts/customgroups";
63 my $netsettings = "${General::swroot}/ethernet/settings";
64
65 &General::readhash("${General::swroot}/firewall/settings", \%fwdfwsettings);
66 &General::readhash("$netsettings", \%defaultNetworks);
67 &General::readhasharray($configfwdfw, \%configfwdfw);
68 &General::readhasharray($configinput, \%configinputfw);
69 &General::readhasharray($configoutgoing, \%configoutgoingfw);
70 &General::readhasharray($configgrp, \%customgrp);
71 &General::get_aliases(\%aliases);
72
73 # MAIN
74 &main();
75
76 sub main {
77 # Flush all chains.
78 &flush();
79
80 # Reload firewall rules.
81 &preparerules();
82
83 # Load P2P block rules.
84 &p2pblock();
85
86 # Reload firewall policy.
87 run("/usr/sbin/firewall-policy");
88 }
89
90 sub run {
91 # Executes or prints the given shell command.
92 my $command = shift;
93
94 if ($DEBUG) {
95 print "$command\n";
96 } else {
97 system "$command";
98
99 if ($?) {
100 print_error("ERROR: $command");
101 }
102 }
103 }
104
105 sub print_error {
106 my $message = shift;
107
108 print STDERR "$message\n";
109 }
110
111 sub print_rule {
112 my $hash = shift;
113
114 print "\nRULE:";
115
116 my $i = 0;
117 foreach (@$hash) {
118 printf(" %2d: %s", $i++, $_);
119 }
120 print "\n";
121 }
122
123 sub flush {
124 run("$IPTABLES -F FORWARDFW");
125 run("$IPTABLES -F INPUTFW");
126 run("$IPTABLES -F OUTGOINGFW");
127 run("$IPTABLES -t nat -F NAT_DESTINATION");
128 run("$IPTABLES -t nat -F NAT_SOURCE");
129 run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
130 }
131
132 sub preparerules {
133 if (! -z "${General::swroot}/firewall/config"){
134 &buildrules(\%configfwdfw);
135 }
136 if (! -z "${General::swroot}/firewall/input"){
137 &buildrules(\%configinputfw);
138 }
139 if (! -z "${General::swroot}/firewall/outgoing"){
140 &buildrules(\%configoutgoingfw);
141 }
142 }
143
144 sub buildrules {
145 my $hash = shift;
146
147 foreach my $key (sort {$a <=> $b} keys %$hash) {
148 # Skip disabled rules.
149 next unless ($$hash{$key}[2] eq 'ON');
150
151 if ($DEBUG) {
152 print_rule($$hash{$key});
153 }
154
155 # Check if the target is valid.
156 my $target = $$hash{$key}[0];
157 if (!$target ~~ @VALID_TARGETS) {
158 print_error("Invalid target '$target' for rule $key");
159 next;
160 }
161
162 # Check if the chain is valid.
163 my $chain = $$hash{$key}[1];
164 if (!$chain ~~ @VALID_CHAINS) {
165 print_error("Invalid chain '$chain' in rule $key");
166 next;
167 }
168
169 # Collect all sources.
170 my @sources = &get_addresses($hash, $key, "src");
171
172 # Collect all destinations.
173 my @destinations = &get_addresses($hash, $key, "tgt");
174
175 my $time_constraints = "";
176
177 # Check if logging should be enabled.
178 my $LOG = ($$hash{$key}[17] eq 'ON');
179
180 # Check if NAT is enabled and initialize variables, that we use for that.
181 my $NAT = ($$hash{$key}[28] eq 'ON');
182 my $NAT_MODE;
183 if ($NAT) {
184 $NAT_MODE = uc($$hash{$key}[31]);
185 }
186
187 # Set up time constraints.
188 my @time_options = ();
189 if ($$hash{$key}[18] eq 'ON') {
190 push(@time_options, ("-m", "time"));
191
192 # Select all days of the week this match is active.
193 my @weekdays = ();
194 if ($$hash{$key}[19] ne '') {
195 push (@weekdays, "Mon");
196 }
197 if ($$hash{$key}[20] ne '') {
198 push (@weekdays, "Tue");
199 }
200 if ($$hash{$key}[21] ne '') {
201 push (@weekdays, "Wed");
202 }
203 if ($$hash{$key}[22] ne '') {
204 push (@weekdays, "Thu");
205 }
206 if ($$hash{$key}[23] ne '') {
207 push (@weekdays, "Fri");
208 }
209 if ($$hash{$key}[24] ne '') {
210 push (@weekdays, "Sat");
211 }
212 if ($$hash{$key}[25] ne '') {
213 push (@weekdays, "Sun");
214 }
215 if (@weekdays) {
216 push(@time_options, ("--weekdays", join(",", @weekdays)));
217 }
218
219 # Convert start time.
220 my $time_start = &format_time($$hash{$key}[26]);
221 if ($time_start) {
222 push(@time_options, ("--timestart", $time_start));
223 }
224
225 # Convert end time.
226 my $time_stop = &format_time($$hash{$key}[27]);
227 if ($time_stop) {
228 push(@time_options, ("--timestop", $time_stop));
229 }
230 }
231
232 # Check which protocols are used in this rule and so that we can
233 # later group rules by protocols.
234 my @protocols = &get_protocols($hash, $key);
235 if (!@protocols) {
236 print_error("Invalid protocol configuration for rule $key");
237 next;
238 }
239
240 foreach my $protocol (@protocols) {
241 # Check if the given protocol is supported.
242 if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) {
243 print_error("Protocol $protocol is not supported (rule $key)");
244 next;
245 }
246
247 # Prepare protocol options (like ICMP types, ports, etc...).
248 my @protocol_options = &get_protocol_options($hash, $key, $protocol);
249
250 # Check if this protocol knows ports.
251 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
252
253 foreach my $source (@sources) {
254 foreach my $destination (@destinations) {
255 # Skip invalid rules.
256 next if (!$source || !$destination || ($destination eq "none"));
257
258 # Array with iptables arguments.
259 my @options = ();
260
261 # Append protocol.
262 if ($protocol ne "all") {
263 push(@options, ("-p", $protocol));
264 push(@options, @protocol_options);
265 }
266
267 # Prepare source options.
268 my @source_options = ();
269 if ($source =~ /mac/) {
270 push(@source_options, $source);
271 } else {
272 push(@source_options, ("-s", $source));
273 }
274
275 # Prepare destination options.
276 my @destination_options = ("-d", $destination);
277
278 # Add time constraint options.
279 push(@options, @time_options);
280
281 # Process NAT rules.
282 if ($NAT) {
283 my $nat_address = &get_nat_address($$hash{$key}[29]);
284
285 # Skip NAT rules if the NAT address is unknown
286 # (i.e. no internet connection has been established, yet).
287 next unless ($nat_address);
288
289 # Destination NAT
290 if ($NAT_MODE eq "DNAT") {
291 # Make port-forwardings useable from the internal networks.
292 &add_dnat_mangle_rules($nat_address, @options);
293
294 my @nat_options = @options;
295 push(@nat_options, @source_options);
296 push(@nat_options, ("-d", $nat_address));
297
298 my ($dnat_address, $dnat_mask) = split("/", $destination);
299 @destination_options = ("-d", $dnat_address);
300
301 if ($protocol_has_ports) {
302 my $dnat_port = &get_dnat_target_port($hash, $key);
303
304 if ($dnat_port) {
305 $dnat_address .= ":$dnat_port";
306
307 # Replace --dport with the translated one.
308 my @new_nat_options = ();
309 my $skip_count = 0;
310 foreach my $option (@nat_options) {
311 next if ($skip_count-- > 0);
312
313 if ($option eq "--dport") {
314 push(@new_nat_options, ("--dport", $dnat_port));
315 $skip_count = 1;
316 next;
317 }
318
319 push(@new_nat_options, $option);
320 }
321 @nat_options = @new_nat_options;
322 }
323 }
324
325 if ($LOG) {
326 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j LOG --log-prefix 'DNAT'");
327 }
328 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j DNAT --to-destination $dnat_address");
329
330 # Source NAT
331 } elsif ($NAT_MODE eq "SNAT") {
332 my @nat_options = @options;
333
334 push(@nat_options, @source_options);
335 push(@nat_options, @destination_options);
336
337 if ($LOG) {
338 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j LOG --log-prefix 'SNAT'");
339 }
340 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
341 }
342 }
343
344 push(@options, @source_options);
345 push(@options, @destination_options);
346
347 # Insert firewall rule.
348 if ($LOG && !$NAT) {
349 run("$IPTABLES -A $chain @options -j LOG");
350 }
351 run("$IPTABLES -A $chain @options -j $target");
352 }
353 }
354 }
355 }
356 }
357
358 sub get_external_interface() {
359 open(IFACE, "/var/ipfire/red/iface") or return "";
360 my $iface = <IFACE>;
361 close(IFACE);
362
363 return $iface;
364 }
365
366 sub get_external_address() {
367 open(ADDR, "/var/ipfire/red/local-ipaddress") or return "";
368 my $address = <ADDR>;
369 close(ADDR);
370
371 return $address;
372 }
373
374 sub get_alias {
375 my $id = shift;
376
377 foreach my $alias (sort keys %aliases) {
378 if ($id eq $alias) {
379 return $aliases{$alias};
380 }
381 }
382 }
383
384 sub get_nat_address {
385 my $zone = shift;
386
387 # Any static address of any zone.
388 if ($zone eq "RED" || $zone eq "GREEN" || $zone eq "ORANGE" || $zone eq "BLUE") {
389 return $defaultNetworks{$zone . "_ADDRESS"};
390
391 } elsif ($zone eq "Default IP") {
392 return &get_external_address();
393
394 } else {
395 return &get_alias($zone);
396 }
397
398 print_error("Could not find NAT address");
399 }
400
401 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
402 sub format_time {
403 my $val = shift;
404
405 # Convert the given time into minutes.
406 my $minutes = &time_convert_to_minutes($val);
407
408 # Move the timestamp into UTC.
409 $minutes += &time_utc_offset();
410
411 # Make sure $minutes is between 00:00 and 23:59.
412 if ($minutes < 0) {
413 $minutes += 1440;
414 }
415
416 if ($minutes > 1440) {
417 $minutes -= 1440;
418 }
419
420 # Format as hh:mm.
421 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
422 }
423
424 # Calculates the offsets in minutes from the local timezone to UTC.
425 sub time_utc_offset {
426 my @localtime = localtime(time);
427 my @gmtime = gmtime(time);
428
429 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
430 }
431
432 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
433 sub time_convert_to_minutes {
434 my ($hrs, $min) = split(":", shift);
435
436 return ($hrs * 60) + $min;
437 }
438
439 sub p2pblock {
440 my $P2PSTRING = "";
441 my $DO;
442 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
443 @p2ps = <FILE>;
444 close FILE;
445 my $CMD = "-m ipp2p";
446 foreach my $p2pentry (sort @p2ps) {
447 my @p2pline = split( /\;/, $p2pentry );
448 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
449 $DO = "ACCEPT";
450 if ("$p2pline[2]" eq "on") {
451 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
452 }
453 }else {
454 $DO = "RETURN";
455 if ("$p2pline[2]" eq "off") {
456 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
457 }
458 }
459 }
460
461 if($P2PSTRING) {
462 run("$IPTABLES -A FORWARDFW $CMD $P2PSTRING -j $DO");
463 }
464 }
465
466 sub get_addresses {
467 my $hash = shift;
468 my $key = shift;
469 my $type = shift;
470
471 my @addresses = ();
472 my $addr_type;
473 my $value;
474 my $group_name;
475
476 if ($type eq "src") {
477 $addr_type = $$hash{$key}[3];
478 $value = $$hash{$key}[4];
479
480 } elsif ($type eq "tgt") {
481 $addr_type = $$hash{$key}[5];
482 $value = $$hash{$key}[6];
483 }
484
485 if ($addr_type ~~ ["cust_grp_src", "cust_grp_tgt"]) {
486 foreach my $grp (sort {$a <=> $b} keys %customgrp) {
487 if ($customgrp{$grp}[0] eq $value) {
488 my @address = &get_address($customgrp{$grp}[3], $customgrp{$grp}[2], $type);
489
490 if (@address) {
491 push(@addresses, @address);
492 }
493 }
494 }
495 } else {
496 my @address = &get_address($addr_type, $value, $type);
497
498 if (@address) {
499 push(@addresses, @address);
500 }
501 }
502
503 return @addresses;
504 }
505
506 sub get_address {
507 my $key = shift;
508 my $value = shift;
509 my $type = shift;
510
511 my @ret = ();
512
513 # If the user manually typed an address, we just check if it is a MAC
514 # address. Otherwise, we assume that it is an IP address.
515 if ($key ~~ ["src_addr", "tgt_addr"]) {
516 if (&General::validmac($value)) {
517 push(@ret, "-m mac --mac-source $value");
518 } else {
519 push(@ret, $value);
520 }
521
522 # If a default network interface (GREEN, BLUE, etc.) is selected, we
523 # try to get the corresponding address of the network.
524 } elsif ($key ~~ ["std_net_src", "std_net_tgt", "Standard Network"]) {
525 my $external_interface = &get_external_interface();
526
527 my $network_address = &fwlib::get_std_net_ip($value, $external_interface);
528 if ($network_address) {
529 push(@ret, $network_address);
530 }
531
532 # Custom networks.
533 } elsif ($key ~~ ["cust_net_src", "cust_net_tgt", "Custom Network"]) {
534 my $network_address = &fwlib::get_net_ip($value);
535 if ($network_address) {
536 push(@ret, $network_address);
537 }
538
539 # Custom hosts.
540 } elsif ($key ~~ ["cust_host_src", "cust_host_tgt", "Custom Host"]) {
541 my $host_address = &fwlib::get_host_ip($value, $type);
542 if ($host_address) {
543 push(@ret, $host_address);
544 }
545
546 # OpenVPN networks.
547 } elsif ($key ~~ ["ovpn_net_src", "ovpn_net_tgt", "OpenVPN static network"]) {
548 my $network_address = &fwlib::get_ovpn_net_ip($value, 1);
549 if ($network_address) {
550 push(@ret, $network_address);
551 }
552
553 # OpenVPN hosts.
554 } elsif ($key ~~ ["ovpn_host_src", "ovpn_host_tgt", "OpenVPN static host"]) {
555 my $host_address = &fwlib::get_ovpn_host_ip($value, 33);
556 if ($host_address) {
557 push(@ret, $host_address);
558 }
559
560 # OpenVPN N2N.
561 } elsif ($key ~~ ["ovpn_n2n_src", "ovpn_n2n_tgt", "OpenVPN N-2-N"]) {
562 my $network_address = &fwlib::get_ovpn_n2n_ip($value, 11);
563 if ($network_address) {
564 push(@ret, $network_address);
565 }
566
567 # IPsec networks.
568 } elsif ($key ~~ ["ipsec_net_src", "ipsec_net_tgt", "IpSec Network"]) {
569 my $network_address = &fwlib::get_ipsec_net_ip($value, 11);
570 if ($network_address) {
571 push(@ret, $network_address);
572 }
573
574 # The firewall's own IP addresses.
575 } elsif ($key ~~ ["ipfire", "ipfire_src"]) {
576 # ALL
577 if ($value eq "ALL") {
578 push(@ret, "0/0");
579
580 # GREEN
581 } elsif ($value eq "GREEN") {
582 push(@ret, $defaultNetworks{"GREEN_ADDRESS"});
583
584 # BLUE
585 } elsif ($value eq "BLUE") {
586 push(@ret, $defaultNetworks{"BLUE_ADDRESS"});
587
588 # ORANGE
589 } elsif ($value eq "ORANGE") {
590 push(@ret, $defaultNetworks{"ORANGE_ADDRESS"});
591
592 # RED
593 } elsif ($value ~~ ["RED", "RED1"]) {
594 my $address = &get_external_address();
595 if ($address) {
596 push(@ret, $address);
597 }
598
599 # Aliases
600 } else {
601 my %alias = &get_alias($value);
602 if (%alias) {
603 push(@ret, $alias{"IPT"});
604 }
605 }
606
607 # If nothing was selected, we assume "any".
608 } else {
609 push(@ret, "0/0");
610 }
611
612 return @ret;
613 }
614
615 sub get_protocols {
616 my $hash = shift;
617 my $key = shift;
618
619 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
620 my $uses_services = ($$hash{$key}[11] eq "ON");
621
622 my @protocols = ();
623
624 # Rules which don't have source ports or services (like ICMP, ESP, ...).
625 if (!$uses_source_ports && !$uses_services) {
626 push(@protocols, $$hash{$key}[8]);
627
628 # Rules which either use ports or services.
629 } elsif ($uses_source_ports || $uses_services) {
630 # Check if service group or service
631 if ($$hash{$key}[14] eq 'cust_srv') {
632 push(@protocols, &fwlib::get_srv_prot($$hash{$key}[15]));
633
634 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
635 my $protos = &fwlib::get_srvgrp_prot($$hash{$key}[15]);
636 push(@protocols, split(",", $protos));
637
638 } else {
639 # Fetch the protocol for this rule.
640 my $protocol = lc($$hash{$key}[8]);
641
642 # Fetch source and destination ports for this rule.
643 my $source_ports = $$hash{$key}[10];
644 my $destination_ports = $$hash{$key}[15];
645
646 # Check if ports are set for protocols which do not support ports.
647 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
648 print_error("$protocol does not support ports");
649 return ();
650 }
651
652 push(@protocols, $protocol);
653 }
654 }
655
656 # Remove all empty elements
657 @protocols = map { $_ ? $_ : () } @protocols;
658
659 # If no protocol has been defined, we assume "all".
660 if (!@protocols) {
661 push(@protocols, "all");
662 }
663
664 # Make all protocol names lowercase.
665 @protocols = map { lc } @protocols;
666
667 return @protocols;
668 }
669
670 sub get_protocol_options {
671 my $hash = shift;
672 my $key = shift;
673 my $protocol = shift;
674 my @options = ();
675
676 # Process source ports.
677 my $use_src_ports = ($$hash{$key}[7] eq "ON");
678 my $src_ports = $$hash{$key}[10];
679
680 if ($use_src_ports && $src_ports) {
681 push(@options, &format_ports($src_ports, "src"));
682 }
683
684 # Process destination ports.
685 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
686 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
687
688 if ($use_dst_ports) {
689 my $dst_ports_mode = $$hash{$key}[14];
690 my $dst_ports = $$hash{$key}[15];
691 if ($use_dnat && $$hash{$key}[30]) {
692 $dst_ports = $$hash{$key}[30];
693 }
694
695 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
696 push(@options, &format_ports($dst_ports, "dst"));
697
698 } elsif ($dst_ports_mode eq "cust_srv") {
699 if ($protocol eq "ICMP") {
700 push(@options, ("--icmp-type", &fwlib::get_srv_port($dst_ports, 3, "ICMP")));
701 } else {
702 $dst_ports = &fwlib::get_srv_port($dst_ports, 1, uc($protocol));
703 push(@options, &format_ports($dst_ports, "dst"));
704 }
705
706 } elsif ($dst_ports_mode eq "cust_srvgrp") {
707 push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol)));
708 }
709 }
710
711 # Check if a single ICMP type is selected.
712 if (!$use_src_ports && !$use_dst_ports && $protocol eq "icmp") {
713 my $icmp_type = $$hash{$key}[9];
714
715 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
716 push(@options, ("--icmp-type", $icmp_type));
717 }
718 }
719
720 return @options;
721 }
722
723 sub format_ports {
724 my $ports = shift;
725 my $type = shift;
726
727 my $arg;
728 if ($type eq "src") {
729 $arg = "--sport";
730 } elsif ($type eq "dst") {
731 $arg = "--dport";
732 }
733
734 my @options = ();
735
736 if ($ports =~ /\|/) {
737 $ports =~ s/\|/,/g;
738 push(@options, ("-m", "multiport"));
739 }
740
741 push(@options, ($arg, $ports));
742
743 return @options;
744 }
745
746 sub get_dnat_target_port {
747 my $hash = shift;
748 my $key = shift;
749
750 if ($$hash{$key}[14] eq "TGT_PORT") {
751 return $$hash{$key}[15];
752 }
753 }
754
755 sub add_dnat_mangle_rules {
756 my $nat_address = shift;
757 my @options = @_;
758
759 my $mark = 0;
760 foreach my $zone ("GREEN", "BLUE", "ORANGE") {
761 $mark++;
762
763 # Skip rule if not all required information exists.
764 next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
765 next unless (exists $defaultNetworks{$zone . "_NETMASK"});
766
767 my @mangle_options = @options;
768
769 my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
770 $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
771
772 push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
773 push(@mangle_options, ("-j", "MARK", "--set-mark", $mark));
774
775 run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");
776 }
777 }