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