]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/firewall/rules.pl
firewall: Allow accessing port forwardings from internal networks.
[people/teissler/ipfire-2.x.git] / config / firewall / rules.pl
CommitLineData
6178953b 1#!/usr/bin/perl -w
2a81ab0d
AM
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5bee9a9d 5# Copyright (C) 2013 Alexander Marx <amarx@ipfire.org> #
2a81ab0d
AM
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###############################################################################
2a81ab0d 21
2a81ab0d 22use strict;
2a81ab0d 23
97ab0569
MT
24require '/var/ipfire/general-functions.pl';
25require "${General::swroot}/lang.pl";
26require "/usr/lib/firewall/firewall-lib.pl";
2a81ab0d 27
68d1eb10
MT
28# Set to one to enable debugging mode.
29my $DEBUG = 0;
30
1f9e7b53
MT
31my $IPTABLES = "iptables --wait";
32
6178953b 33# iptables chains
8f4f4634
MT
34my $CHAIN_INPUT = "INPUTFW";
35my $CHAIN_FORWARD = "FORWARDFW";
36my $CHAIN_OUTPUT = "OUTPUTFW";
37my $CHAIN = $CHAIN_FORWARD;
6178953b
MT
38my $CHAIN_NAT_SOURCE = "NAT_SOURCE";
39my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION";
6e87f0aa 40my $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION";
8f4f4634
MT
41my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT);
42
43my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
44my @PROTOCOLS_WITH_PORTS = ("tcp", "udp");
45
46my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT");
6178953b 47
2a81ab0d
AM
48my %fwdfwsettings=();
49my %defaultNetworks=();
8f4f4634 50my %configfwdfw=();;
2a81ab0d 51my %customgrp=();
2a81ab0d 52my %configinputfw=();
5d7faa45 53my %configoutgoingfw=();
a6edca5a 54my %confignatfw=();
2a81ab0d 55my %aliases=();
36196d0d 56my @p2ps=();
2a81ab0d 57
6d8eb5de
AM
58my $configfwdfw = "${General::swroot}/firewall/config";
59my $configinput = "${General::swroot}/firewall/input";
60my $configoutgoing = "${General::swroot}/firewall/outgoing";
61my $p2pfile = "${General::swroot}/firewall/p2protocols";
2a81ab0d 62my $configgrp = "${General::swroot}/fwhosts/customgroups";
210ee67b 63my $netsettings = "${General::swroot}/ethernet/settings";
86a921ee 64
6d8eb5de 65&General::readhash("${General::swroot}/firewall/settings", \%fwdfwsettings);
210ee67b 66&General::readhash("$netsettings", \%defaultNetworks);
2a81ab0d
AM
67&General::readhasharray($configfwdfw, \%configfwdfw);
68&General::readhasharray($configinput, \%configinputfw);
5d7faa45 69&General::readhasharray($configoutgoing, \%configoutgoingfw);
2a81ab0d
AM
70&General::readhasharray($configgrp, \%customgrp);
71&General::get_aliases(\%aliases);
72
8531b94a
MT
73# MAIN
74&main();
75
76sub 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");
2a81ab0d 88}
97ab0569 89
68d1eb10
MT
90sub 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";
6e87f0aa
MT
98
99 if ($?) {
100 print_error("ERROR: $command");
101 }
68d1eb10
MT
102 }
103}
104
6178953b
MT
105sub print_error {
106 my $message = shift;
107
108 print STDERR "$message\n";
109}
110
8f4f4634
MT
111sub 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
97ab0569 123sub flush {
1f9e7b53
MT
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");
6e87f0aa 129 run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
86a921ee 130}
97ab0569
MT
131
132sub preparerules {
6d8eb5de 133 if (! -z "${General::swroot}/firewall/config"){
2a81ab0d
AM
134 &buildrules(\%configfwdfw);
135 }
6d8eb5de 136 if (! -z "${General::swroot}/firewall/input"){
2a81ab0d
AM
137 &buildrules(\%configinputfw);
138 }
6d8eb5de 139 if (! -z "${General::swroot}/firewall/outgoing"){
5d7faa45
AM
140 &buildrules(\%configoutgoingfw);
141 }
2a81ab0d 142}
97ab0569
MT
143
144sub buildrules {
8f4f4634
MT
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");
6178953b 174
b05ec50a 175 my $time_constraints = "";
6178953b
MT
176
177 # Check if logging should be enabled.
8f4f4634 178 my $LOG = ($$hash{$key}[17] eq 'ON');
6178953b 179
8f4f4634
MT
180 # Check if NAT is enabled and initialize variables, that we use for that.
181 my $NAT = ($$hash{$key}[28] eq 'ON');
6178953b 182 my $NAT_MODE;
8f4f4634
MT
183 if ($NAT) {
184 $NAT_MODE = uc($$hash{$key}[31]);
185 }
6178953b 186
8f4f4634
MT
187 # Set up time constraints.
188 my @time_options = ();
189 if ($$hash{$key}[18] eq 'ON') {
190 push(@time_options, ("-m", "time"));
6178953b 191
8f4f4634
MT
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 }
6178953b 218
8f4f4634
MT
219 # Convert start time.
220 my $time_start = &format_time($$hash{$key}[26]);
221 if ($time_start) {
222 push(@time_options, ("--timestart", $time_start));
a6edca5a 223 }
6178953b 224
8f4f4634
MT
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 }
a6edca5a 230 }
6178953b 231
8f4f4634
MT
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;
2a81ab0d 245 }
8f4f4634
MT
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);
2a81ab0d 265 }
8f4f4634 266
6e87f0aa
MT
267 # Prepare source options.
268 my @source_options = ();
8f4f4634 269 if ($source =~ /mac/) {
6e87f0aa 270 push(@source_options, $source);
8f4f4634 271 } else {
6e87f0aa 272 push(@source_options, ("-s", $source));
2a81ab0d 273 }
14f7cb87 274
6e87f0aa
MT
275 # Prepare destination options.
276 my @destination_options = ("-d", $destination);
14f7cb87 277
8f4f4634
MT
278 # Add time constraint options.
279 push(@options, @time_options);
14f7cb87 280
8f4f4634
MT
281 # Process NAT rules.
282 if ($NAT) {
283 my $nat_address = &get_nat_address($$hash{$key}[29]);
b05ec50a 284
8f4f4634
MT
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);
b05ec50a 288
8f4f4634
MT
289 # Destination NAT
290 if ($NAT_MODE eq "DNAT") {
6e87f0aa
MT
291 # Make port-forwardings useable from the internal networks.
292 &add_dnat_mangle_rules($nat_address, @options);
b05ec50a 293
8f4f4634 294 my @nat_options = @options;
6e87f0aa 295 push(@nat_options, @source_options);
8f4f4634 296 push(@nat_options, ("-d", $nat_address));
6e87f0aa
MT
297
298 my ($dnat_address, $dnat_mask) = split("/", $destination);
299 @destination_options = ("-d", $dnat_address);
b05ec50a 300
8f4f4634
MT
301 if ($protocol_has_ports) {
302 my $dnat_port = &get_dnat_target_port($hash, $key);
b05ec50a 303
8f4f4634
MT
304 if ($dnat_port) {
305 $dnat_address .= ":$dnat_port";
306
307 # Replace --dport with the translated one.
6e87f0aa 308 my @new_nat_options = ();
8f4f4634 309 my $skip_count = 0;
6e87f0aa 310 foreach my $option (@nat_options) {
8f4f4634
MT
311 next if ($skip_count-- > 0);
312
313 if ($option eq "--dport") {
6e87f0aa 314 push(@new_nat_options, ("--dport", $dnat_port));
8f4f4634
MT
315 $skip_count = 1;
316 next;
98cee89f 317 }
8f4f4634 318
6e87f0aa 319 push(@new_nat_options, $option);
c12392c0 320 }
6e87f0aa 321 @nat_options = @new_nat_options;
86a921ee 322 }
2a81ab0d 323 }
8f4f4634
MT
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") {
6e87f0aa
MT
332 my @nat_options = @options;
333
334 push(@nat_options, @source_options);
335 push(@nat_options, @destination_options);
336
8f4f4634 337 if ($LOG) {
6e87f0aa 338 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j LOG --log-prefix 'SNAT'");
8f4f4634 339 }
6e87f0aa 340 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
2a81ab0d
AM
341 }
342 }
8f4f4634 343
6e87f0aa
MT
344 push(@options, @source_options);
345 push(@options, @destination_options);
346
8f4f4634
MT
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");
2a81ab0d
AM
352 }
353 }
354 }
2a81ab0d
AM
355 }
356}
97ab0569 357
8f4f4634
MT
358sub 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
366sub 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
374sub get_alias {
375 my $id = shift;
376
377 foreach my $alias (sort keys %aliases) {
378 if ($id eq $alias) {
379 return $aliases{$alias};
a6edca5a
AM
380 }
381 }
8f4f4634
MT
382}
383
384sub 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");
a6edca5a 399}
97ab0569 400
b05ec50a
MT
401# Formats the given timestamp into the iptables format which is "hh:mm" UTC.
402sub 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);
472136c9 422}
97ab0569 423
b05ec50a
MT
424# Calculates the offsets in minutes from the local timezone to UTC.
425sub 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);
472136c9 430}
97ab0569 431
b05ec50a
MT
432# Takes a timestamp like "14:00" and converts it into minutes since midnight.
433sub time_convert_to_minutes {
434 my ($hrs, $min) = split(":", shift);
435
436 return ($hrs * 60) + $min;
472136c9 437}
97ab0569
MT
438
439sub p2pblock {
6178953b 440 my $P2PSTRING = "";
36196d0d
AM
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 );
8d1beadc
AM
448 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
449 $DO = "ACCEPT";
5238a871 450 if ("$p2pline[2]" eq "on") {
36196d0d
AM
451 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
452 }
8d1beadc 453 }else {
36196d0d 454 $DO = "RETURN";
5238a871 455 if ("$p2pline[2]" eq "off") {
36196d0d
AM
456 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
457 }
458 }
459 }
68d1eb10
MT
460
461 if($P2PSTRING) {
1f9e7b53 462 run("$IPTABLES -A FORWARDFW $CMD $P2PSTRING -j $DO");
36196d0d
AM
463 }
464}
97ab0569 465
8f4f4634
MT
466sub 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 }
2a81ab0d 501 }
8f4f4634
MT
502
503 return @addresses;
504}
505
506sub 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);
b5269091 530 }
8f4f4634
MT
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);
a0fb1099 537 }
8f4f4634
MT
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);
a0fb1099 544 }
8f4f4634
MT
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);
a0fb1099 551 }
8f4f4634
MT
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);
a0fb1099 558 }
8f4f4634
MT
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"});
a0fb1099
AM
604 }
605 }
8f4f4634
MT
606
607 # If nothing was selected, we assume "any".
608 } else {
609 push(@ret, "0/0");
2a81ab0d 610 }
8f4f4634
MT
611
612 return @ret;
2a81ab0d 613}
97ab0569 614
8f4f4634
MT
615sub 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);
2a81ab0d
AM
653 }
654 }
8f4f4634
MT
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");
98cee89f 662 }
8f4f4634
MT
663
664 # Make all protocol names lowercase.
665 @protocols = map { lc } @protocols;
666
667 return @protocols;
2a81ab0d 668}
97ab0569 669
8f4f4634
MT
670sub 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];
2a81ab0d 693 }
8f4f4634
MT
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"));
2a81ab0d 704 }
8f4f4634
MT
705
706 } elsif ($dst_ports_mode eq "cust_srvgrp") {
707 push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol)));
2a81ab0d
AM
708 }
709 }
8f4f4634
MT
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));
a4c7bf6b
AM
717 }
718 }
8f4f4634
MT
719
720 return @options;
721}
722
723sub 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
746sub 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 }
2a81ab0d 753}
6e87f0aa
MT
754
755sub 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}