]> git.ipfire.org Git - ipfire-2.x.git/blob - config/firewall/rules.pl
Merge remote-tracking branch 'amarx/INDEX' into next
[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
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 my @log_limit_options = &make_log_limit_options();
74
75 # MAIN
76 &main();
77
78 sub main {
79 # Flush all chains.
80 &flush();
81
82 # Reload firewall rules.
83 &preparerules();
84
85 # Load P2P block rules.
86 &p2pblock();
87
88 # Reload firewall policy.
89 run("/usr/sbin/firewall-policy");
90 }
91
92 sub run {
93 # Executes or prints the given shell command.
94 my $command = shift;
95
96 if ($DEBUG) {
97 print "$command\n";
98 } else {
99 system "$command";
100
101 if ($?) {
102 print_error("ERROR: $command");
103 }
104 }
105 }
106
107 sub print_error {
108 my $message = shift;
109
110 print STDERR "$message\n";
111 }
112
113 sub print_rule {
114 my $hash = shift;
115
116 print "\nRULE:";
117
118 my $i = 0;
119 foreach (@$hash) {
120 printf(" %2d: %s", $i++, $_);
121 }
122 print "\n";
123 }
124
125 sub flush {
126 run("$IPTABLES -F $CHAIN_INPUT");
127 run("$IPTABLES -F $CHAIN_FORWARD");
128 run("$IPTABLES -F $CHAIN_OUTPUT");
129 run("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE");
130 run("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION");
131 run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
132 }
133
134 sub preparerules {
135 if (! -z "${General::swroot}/firewall/config"){
136 &buildrules(\%configfwdfw);
137 }
138 if (! -z "${General::swroot}/firewall/input"){
139 &buildrules(\%configinputfw);
140 }
141 if (! -z "${General::swroot}/firewall/outgoing"){
142 &buildrules(\%configoutgoingfw);
143 }
144 }
145
146 sub buildrules {
147 my $hash = shift;
148
149 foreach my $key (sort {$a <=> $b} keys %$hash) {
150 # Skip disabled rules.
151 next unless ($$hash{$key}[2] eq 'ON');
152
153 if ($DEBUG) {
154 print_rule($$hash{$key});
155 }
156
157 # Check if the target is valid.
158 my $target = $$hash{$key}[0];
159 if (!$target ~~ @VALID_TARGETS) {
160 print_error("Invalid target '$target' for rule $key");
161 next;
162 }
163
164 # Check if the chain is valid.
165 my $chain = $$hash{$key}[1];
166 if (!$chain ~~ @VALID_CHAINS) {
167 print_error("Invalid chain '$chain' in rule $key");
168 next;
169 }
170
171 # Collect all sources.
172 my @sources = &get_addresses($hash, $key, "src");
173
174 # Collect all destinations.
175 my @destinations = &get_addresses($hash, $key, "tgt");
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 my $firewall_is_in_source_subnet = 0;
282 if ($source) {
283 $firewall_is_in_source_subnet = &firewall_is_in_subnet($source);
284 }
285
286 # Process NAT rules.
287 if ($NAT) {
288 my $nat_address = &get_nat_address($$hash{$key}[29]);
289
290 # Skip NAT rules if the NAT address is unknown
291 # (i.e. no internet connection has been established, yet).
292 next unless ($nat_address);
293
294 # Destination NAT
295 if ($NAT_MODE eq "DNAT") {
296 # Make port-forwardings useable from the internal networks.
297 &add_dnat_mangle_rules($nat_address, @options);
298
299 my @nat_options = @options;
300 push(@nat_options, @source_options);
301 push(@nat_options, ("-d", $nat_address));
302
303 my ($dnat_address, $dnat_mask) = split("/", $destination);
304 @destination_options = ("-d", $dnat_address);
305
306 if ($protocol_has_ports) {
307 my $dnat_port = &get_dnat_target_port($hash, $key);
308
309 if ($dnat_port) {
310 $dnat_address .= ":$dnat_port";
311 }
312 }
313
314 if ($LOG) {
315 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '");
316 }
317 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options -j DNAT --to-destination $dnat_address");
318
319 # Source NAT
320 } elsif ($NAT_MODE eq "SNAT") {
321 my @nat_options = @options;
322
323 push(@nat_options, @source_options);
324 push(@nat_options, @destination_options);
325
326 if ($LOG) {
327 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '");
328 }
329 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
330 }
331 }
332
333 push(@options, @source_options);
334
335 if ($firewall_is_in_source_subnet && ($fwdfwsettings{"POLICY"} eq "MODE1") && ($chain eq $CHAIN_FORWARD)) {
336 if ($LOG && !$NAT) {
337 run("$IPTABLES -A $CHAIN_INPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '");
338 }
339 run("$IPTABLES -A $CHAIN_INPUT @options -j $target");
340 }
341
342 push(@options, @destination_options);
343
344 # Insert firewall rule.
345 if ($LOG && !$NAT) {
346 run("$IPTABLES -A $chain @options @log_limit_options -j LOG --log-prefix '$chain '");
347 }
348 run("$IPTABLES -A $chain @options -j $target");
349 }
350 }
351 }
352 }
353 }
354
355 sub get_external_interface() {
356 open(IFACE, "/var/ipfire/red/iface") or return "";
357 my $iface = <IFACE>;
358 close(IFACE);
359
360 return $iface;
361 }
362
363 sub get_external_address() {
364 open(ADDR, "/var/ipfire/red/local-ipaddress") or return "";
365 my $address = <ADDR>;
366 close(ADDR);
367
368 return $address;
369 }
370
371 sub get_alias {
372 my $id = shift;
373
374 foreach my $alias (sort keys %aliases) {
375 if ($id eq $alias) {
376 return $aliases{$alias};
377 }
378 }
379 }
380
381 sub get_nat_address {
382 my $zone = shift;
383
384 # Any static address of any zone.
385 if ($zone eq "RED" || $zone eq "GREEN" || $zone eq "ORANGE" || $zone eq "BLUE") {
386 return $defaultNetworks{$zone . "_ADDRESS"};
387
388 } elsif ($zone eq "Default IP") {
389 return &get_external_address();
390
391 } else {
392 return &get_alias($zone);
393 }
394
395 print_error("Could not find NAT address");
396 }
397
398 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
399 sub format_time {
400 my $val = shift;
401
402 # Convert the given time into minutes.
403 my $minutes = &time_convert_to_minutes($val);
404
405 # Move the timestamp into UTC.
406 $minutes += &time_utc_offset();
407
408 # Make sure $minutes is between 00:00 and 23:59.
409 if ($minutes < 0) {
410 $minutes += 1440;
411 }
412
413 if ($minutes > 1440) {
414 $minutes -= 1440;
415 }
416
417 # Format as hh:mm.
418 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
419 }
420
421 # Calculates the offsets in minutes from the local timezone to UTC.
422 sub time_utc_offset {
423 my @localtime = localtime(time);
424 my @gmtime = gmtime(time);
425
426 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
427 }
428
429 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
430 sub time_convert_to_minutes {
431 my ($hrs, $min) = split(":", shift);
432
433 return ($hrs * 60) + $min;
434 }
435
436 sub p2pblock {
437 my $P2PSTRING = "";
438 my $DO;
439 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
440 @p2ps = <FILE>;
441 close FILE;
442 my $CMD = "-m ipp2p";
443 foreach my $p2pentry (sort @p2ps) {
444 my @p2pline = split( /\;/, $p2pentry );
445 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
446 $DO = "ACCEPT";
447 if ("$p2pline[2]" eq "on") {
448 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
449 }
450 }else {
451 $DO = "RETURN";
452 if ("$p2pline[2]" eq "off") {
453 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
454 }
455 }
456 }
457
458 if($P2PSTRING) {
459 run("$IPTABLES -A FORWARDFW $CMD $P2PSTRING -j $DO");
460 }
461 }
462
463 sub get_addresses {
464 my $hash = shift;
465 my $key = shift;
466 my $type = shift;
467
468 my @addresses = ();
469 my $addr_type;
470 my $value;
471 my $group_name;
472
473 if ($type eq "src") {
474 $addr_type = $$hash{$key}[3];
475 $value = $$hash{$key}[4];
476
477 } elsif ($type eq "tgt") {
478 $addr_type = $$hash{$key}[5];
479 $value = $$hash{$key}[6];
480 }
481
482 if ($addr_type ~~ ["cust_grp_src", "cust_grp_tgt"]) {
483 foreach my $grp (sort {$a <=> $b} keys %customgrp) {
484 if ($customgrp{$grp}[0] eq $value) {
485 my @address = &get_address($customgrp{$grp}[3], $customgrp{$grp}[2], $type);
486
487 if (@address) {
488 push(@addresses, @address);
489 }
490 }
491 }
492 } else {
493 my @address = &get_address($addr_type, $value, $type);
494
495 if (@address) {
496 push(@addresses, @address);
497 }
498 }
499
500 return @addresses;
501 }
502
503 sub get_address {
504 my $key = shift;
505 my $value = shift;
506 my $type = shift;
507
508 my @ret = ();
509
510 # If the user manually typed an address, we just check if it is a MAC
511 # address. Otherwise, we assume that it is an IP address.
512 if ($key ~~ ["src_addr", "tgt_addr"]) {
513 if (&General::validmac($value)) {
514 push(@ret, "-m mac --mac-source $value");
515 } else {
516 push(@ret, $value);
517 }
518
519 # If a default network interface (GREEN, BLUE, etc.) is selected, we
520 # try to get the corresponding address of the network.
521 } elsif ($key ~~ ["std_net_src", "std_net_tgt", "Standard Network"]) {
522 my $external_interface = &get_external_interface();
523
524 my $network_address = &fwlib::get_std_net_ip($value, $external_interface);
525 if ($network_address) {
526 push(@ret, $network_address);
527 }
528
529 # Custom networks.
530 } elsif ($key ~~ ["cust_net_src", "cust_net_tgt", "Custom Network"]) {
531 my $network_address = &fwlib::get_net_ip($value);
532 if ($network_address) {
533 push(@ret, $network_address);
534 }
535
536 # Custom hosts.
537 } elsif ($key ~~ ["cust_host_src", "cust_host_tgt", "Custom Host"]) {
538 my $host_address = &fwlib::get_host_ip($value, $type);
539 if ($host_address) {
540 push(@ret, $host_address);
541 }
542
543 # OpenVPN networks.
544 } elsif ($key ~~ ["ovpn_net_src", "ovpn_net_tgt", "OpenVPN static network"]) {
545 my $network_address = &fwlib::get_ovpn_net_ip($value, 1);
546 if ($network_address) {
547 push(@ret, $network_address);
548 }
549
550 # OpenVPN hosts.
551 } elsif ($key ~~ ["ovpn_host_src", "ovpn_host_tgt", "OpenVPN static host"]) {
552 my $host_address = &fwlib::get_ovpn_host_ip($value, 33);
553 if ($host_address) {
554 push(@ret, $host_address);
555 }
556
557 # OpenVPN N2N.
558 } elsif ($key ~~ ["ovpn_n2n_src", "ovpn_n2n_tgt", "OpenVPN N-2-N"]) {
559 my $network_address = &fwlib::get_ovpn_n2n_ip($value, 11);
560 if ($network_address) {
561 push(@ret, $network_address);
562 }
563
564 # IPsec networks.
565 } elsif ($key ~~ ["ipsec_net_src", "ipsec_net_tgt", "IpSec Network"]) {
566 my $network_address = &fwlib::get_ipsec_net_ip($value, 11);
567 if ($network_address) {
568 push(@ret, $network_address);
569 }
570
571 # The firewall's own IP addresses.
572 } elsif ($key ~~ ["ipfire", "ipfire_src"]) {
573 # ALL
574 if ($value eq "ALL") {
575 push(@ret, "0/0");
576
577 # GREEN
578 } elsif ($value eq "GREEN") {
579 push(@ret, $defaultNetworks{"GREEN_ADDRESS"});
580
581 # BLUE
582 } elsif ($value eq "BLUE") {
583 push(@ret, $defaultNetworks{"BLUE_ADDRESS"});
584
585 # ORANGE
586 } elsif ($value eq "ORANGE") {
587 push(@ret, $defaultNetworks{"ORANGE_ADDRESS"});
588
589 # RED
590 } elsif ($value ~~ ["RED", "RED1"]) {
591 my $address = &get_external_address();
592 if ($address) {
593 push(@ret, $address);
594 }
595
596 # Aliases
597 } else {
598 my %alias = &get_alias($value);
599 if (%alias) {
600 push(@ret, $alias{"IPT"});
601 }
602 }
603
604 # If nothing was selected, we assume "any".
605 } else {
606 push(@ret, "0/0");
607 }
608
609 return @ret;
610 }
611
612 sub get_protocols {
613 my $hash = shift;
614 my $key = shift;
615
616 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
617 my $uses_services = ($$hash{$key}[11] eq "ON");
618
619 my @protocols = ();
620
621 # Rules which don't have source ports or services (like ICMP, ESP, ...).
622 if (!$uses_source_ports && !$uses_services) {
623 push(@protocols, $$hash{$key}[8]);
624
625 # Rules which either use ports or services.
626 } elsif ($uses_source_ports || $uses_services) {
627 # Check if service group or service
628 if ($$hash{$key}[14] eq 'cust_srv') {
629 push(@protocols, &fwlib::get_srv_prot($$hash{$key}[15]));
630
631 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
632 my $protos = &fwlib::get_srvgrp_prot($$hash{$key}[15]);
633 push(@protocols, split(",", $protos));
634
635 } else {
636 # Fetch the protocol for this rule.
637 my $protocol = lc($$hash{$key}[8]);
638
639 # Fetch source and destination ports for this rule.
640 my $source_ports = $$hash{$key}[10];
641 my $destination_ports = $$hash{$key}[15];
642
643 # Check if ports are set for protocols which do not support ports.
644 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
645 print_error("$protocol does not support ports");
646 return ();
647 }
648
649 push(@protocols, $protocol);
650 }
651 }
652
653 # Remove all empty elements
654 @protocols = map { $_ ? $_ : () } @protocols;
655
656 # If no protocol has been defined, we assume "all".
657 if (!@protocols) {
658 push(@protocols, "all");
659 }
660
661 # Make all protocol names lowercase.
662 @protocols = map { lc } @protocols;
663
664 return @protocols;
665 }
666
667 sub get_protocol_options {
668 my $hash = shift;
669 my $key = shift;
670 my $protocol = shift;
671 my @options = ();
672
673 # Process source ports.
674 my $use_src_ports = ($$hash{$key}[7] eq "ON");
675 my $src_ports = $$hash{$key}[10];
676
677 if ($use_src_ports && $src_ports) {
678 push(@options, &format_ports($src_ports, "src"));
679 }
680
681 # Process destination ports.
682 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
683 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
684
685 if ($use_dst_ports) {
686 my $dst_ports_mode = $$hash{$key}[14];
687 my $dst_ports = $$hash{$key}[15];
688
689 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
690 if ($use_dnat && $$hash{$key}[30]) {
691 $dst_ports = $$hash{$key}[30];
692 }
693 push(@options, &format_ports($dst_ports, "dst"));
694
695 } elsif ($dst_ports_mode eq "cust_srv") {
696 if ($protocol eq "ICMP") {
697 push(@options, ("--icmp-type", &fwlib::get_srv_port($dst_ports, 3, "ICMP")));
698 } else {
699 $dst_ports = &fwlib::get_srv_port($dst_ports, 1, uc($protocol));
700 push(@options, &format_ports($dst_ports, "dst"));
701 }
702
703 } elsif ($dst_ports_mode eq "cust_srvgrp") {
704 push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol)));
705 }
706 }
707
708 # Check if a single ICMP type is selected.
709 if (!$use_src_ports && !$use_dst_ports && $protocol eq "icmp") {
710 my $icmp_type = $$hash{$key}[9];
711
712 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
713 push(@options, ("--icmp-type", $icmp_type));
714 }
715 }
716
717 return @options;
718 }
719
720 sub format_ports {
721 my $ports = shift;
722 my $type = shift;
723
724 my $arg;
725 if ($type eq "src") {
726 $arg = "--sport";
727 } elsif ($type eq "dst") {
728 $arg = "--dport";
729 }
730
731 my @options = ();
732
733 if ($ports =~ /\|/) {
734 $ports =~ s/\|/,/g;
735 push(@options, ("-m", "multiport"));
736 }
737
738 if ($ports) {
739 push(@options, ($arg, $ports));
740 }
741
742 return @options;
743 }
744
745 sub get_dnat_target_port {
746 my $hash = shift;
747 my $key = shift;
748
749 if ($$hash{$key}[14] eq "TGT_PORT") {
750 my $port = $$hash{$key}[15];
751 my $external_port = $$hash{$key}[30];
752
753 if ($external_port && ($port ne $external_port)) {
754 return $$hash{$key}[15];
755 }
756 }
757 }
758
759 sub add_dnat_mangle_rules {
760 my $nat_address = shift;
761 my @options = @_;
762
763 my $mark = 0;
764 foreach my $zone ("GREEN", "BLUE", "ORANGE") {
765 $mark++;
766
767 # Skip rule if not all required information exists.
768 next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
769 next unless (exists $defaultNetworks{$zone . "_NETMASK"});
770
771 my @mangle_options = @options;
772
773 my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
774 $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
775
776 push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
777 push(@mangle_options, ("-j", "MARK", "--set-mark", $mark));
778
779 run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");
780 }
781 }
782
783 sub make_log_limit_options {
784 my @options = ("-m", "limit");
785
786 # Maybe we should get this from the configuration.
787 my $limit = 10;
788
789 # We limit log messages to $limit messages per minute.
790 push(@options, ("--limit", "$limit/min"));
791
792 # And we allow bursts of 2x $limit.
793 push(@options, ("--limit-burst", $limit * 2));
794
795 return @options;
796 }
797
798 sub firewall_is_in_subnet {
799 my $subnet = shift;
800
801 my ($net_address, $net_mask) = split("/", $subnet);
802 if (!$net_mask) {
803 return 0;
804 }
805
806 # ORANGE is missing here, because nothing may ever access
807 # the firewall from this network.
808 foreach my $zone ("GREEN", "BLUE") {
809 next unless (exists $defaultNetworks{$zone . "_ADDRESS"});
810
811 my $zone_address = $defaultNetworks{$zone . "_ADDRESS"};
812
813 if (&General::IpInSubnet($zone_address, $net_address, $net_mask)) {
814 return 1;
815 }
816 }
817
818 return 0;
819 }