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