]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blame - config/firewall/rules.pl
firewall: Fix rule generation for protocols without ports.
[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 41my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT);
c2a1af75 42my @ANY_ADDRESSES = ("0.0.0.0/0.0.0.0", "0.0.0.0/0", "0/0");
8f4f4634
MT
43
44my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
45my @PROTOCOLS_WITH_PORTS = ("tcp", "udp");
46
47my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT");
6178953b 48
2a81ab0d
AM
49my %fwdfwsettings=();
50my %defaultNetworks=();
8f4f4634 51my %configfwdfw=();;
2a81ab0d 52my %customgrp=();
2a81ab0d 53my %configinputfw=();
5d7faa45 54my %configoutgoingfw=();
a6edca5a 55my %confignatfw=();
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 70&General::readhasharray($configgrp, \%customgrp);
2a81ab0d 71
3bb4bb3f
MT
72my @log_limit_options = &make_log_limit_options();
73
8531b94a
MT
74# MAIN
75&main();
76
77sub main {
78 # Flush all chains.
79 &flush();
80
81 # Reload firewall rules.
82 &preparerules();
83
84 # Load P2P block rules.
85 &p2pblock();
86
87 # Reload firewall policy.
88 run("/usr/sbin/firewall-policy");
2a81ab0d 89}
97ab0569 90
68d1eb10
MT
91sub run {
92 # Executes or prints the given shell command.
93 my $command = shift;
94
95 if ($DEBUG) {
96 print "$command\n";
97 } else {
98 system "$command";
6e87f0aa
MT
99
100 if ($?) {
101 print_error("ERROR: $command");
102 }
68d1eb10
MT
103 }
104}
105
6178953b
MT
106sub print_error {
107 my $message = shift;
108
109 print STDERR "$message\n";
110}
111
8f4f4634
MT
112sub print_rule {
113 my $hash = shift;
114
115 print "\nRULE:";
116
117 my $i = 0;
118 foreach (@$hash) {
119 printf(" %2d: %s", $i++, $_);
120 }
121 print "\n";
122}
123
97ab0569 124sub flush {
d98aa95a
MT
125 run("$IPTABLES -F $CHAIN_INPUT");
126 run("$IPTABLES -F $CHAIN_FORWARD");
127 run("$IPTABLES -F $CHAIN_OUTPUT");
128 run("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE");
129 run("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION");
6e87f0aa 130 run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
86a921ee 131}
97ab0569
MT
132
133sub preparerules {
6d8eb5de 134 if (! -z "${General::swroot}/firewall/config"){
2a81ab0d
AM
135 &buildrules(\%configfwdfw);
136 }
6d8eb5de 137 if (! -z "${General::swroot}/firewall/input"){
2a81ab0d
AM
138 &buildrules(\%configinputfw);
139 }
6d8eb5de 140 if (! -z "${General::swroot}/firewall/outgoing"){
5d7faa45
AM
141 &buildrules(\%configoutgoingfw);
142 }
2a81ab0d 143}
97ab0569
MT
144
145sub buildrules {
8f4f4634
MT
146 my $hash = shift;
147
148 foreach my $key (sort {$a <=> $b} keys %$hash) {
149 # Skip disabled rules.
150 next unless ($$hash{$key}[2] eq 'ON');
151
152 if ($DEBUG) {
153 print_rule($$hash{$key});
154 }
155
156 # Check if the target is valid.
157 my $target = $$hash{$key}[0];
158 if (!$target ~~ @VALID_TARGETS) {
159 print_error("Invalid target '$target' for rule $key");
160 next;
161 }
162
163 # Check if the chain is valid.
164 my $chain = $$hash{$key}[1];
165 if (!$chain ~~ @VALID_CHAINS) {
166 print_error("Invalid chain '$chain' in rule $key");
167 next;
168 }
169
170 # Collect all sources.
4e54e3c6 171 my @sources = &fwlib::get_addresses($hash, $key, "src");
8f4f4634
MT
172
173 # Collect all destinations.
4e54e3c6 174 my @destinations = &fwlib::get_addresses($hash, $key, "tgt");
6178953b 175
c0ce9206
MT
176 # True if the destination is the firewall itself.
177 my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire");
178
6178953b 179 # Check if logging should be enabled.
8f4f4634 180 my $LOG = ($$hash{$key}[17] eq 'ON');
6178953b 181
8f4f4634
MT
182 # Check if NAT is enabled and initialize variables, that we use for that.
183 my $NAT = ($$hash{$key}[28] eq 'ON');
6178953b 184 my $NAT_MODE;
8f4f4634
MT
185 if ($NAT) {
186 $NAT_MODE = uc($$hash{$key}[31]);
187 }
6178953b 188
8f4f4634
MT
189 # Set up time constraints.
190 my @time_options = ();
191 if ($$hash{$key}[18] eq 'ON') {
192 push(@time_options, ("-m", "time"));
6178953b 193
8f4f4634
MT
194 # Select all days of the week this match is active.
195 my @weekdays = ();
196 if ($$hash{$key}[19] ne '') {
197 push (@weekdays, "Mon");
198 }
199 if ($$hash{$key}[20] ne '') {
200 push (@weekdays, "Tue");
201 }
202 if ($$hash{$key}[21] ne '') {
203 push (@weekdays, "Wed");
204 }
205 if ($$hash{$key}[22] ne '') {
206 push (@weekdays, "Thu");
207 }
208 if ($$hash{$key}[23] ne '') {
209 push (@weekdays, "Fri");
210 }
211 if ($$hash{$key}[24] ne '') {
212 push (@weekdays, "Sat");
213 }
214 if ($$hash{$key}[25] ne '') {
215 push (@weekdays, "Sun");
216 }
217 if (@weekdays) {
218 push(@time_options, ("--weekdays", join(",", @weekdays)));
219 }
6178953b 220
8f4f4634
MT
221 # Convert start time.
222 my $time_start = &format_time($$hash{$key}[26]);
223 if ($time_start) {
224 push(@time_options, ("--timestart", $time_start));
a6edca5a 225 }
6178953b 226
8f4f4634
MT
227 # Convert end time.
228 my $time_stop = &format_time($$hash{$key}[27]);
229 if ($time_stop) {
230 push(@time_options, ("--timestop", $time_stop));
231 }
a6edca5a 232 }
6178953b 233
8f4f4634
MT
234 # Check which protocols are used in this rule and so that we can
235 # later group rules by protocols.
236 my @protocols = &get_protocols($hash, $key);
237 if (!@protocols) {
238 print_error("Invalid protocol configuration for rule $key");
239 next;
240 }
241
242 foreach my $protocol (@protocols) {
243 # Check if the given protocol is supported.
244 if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) {
245 print_error("Protocol $protocol is not supported (rule $key)");
246 next;
2a81ab0d 247 }
8f4f4634
MT
248
249 # Prepare protocol options (like ICMP types, ports, etc...).
d7a14d01 250 my @protocol_options = &get_protocol_options($hash, $key, $protocol, 0);
8f4f4634
MT
251
252 # Check if this protocol knows ports.
253 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
254
02574191
MT
255 foreach my $src (@sources) {
256 # Skip invalid source.
257 next unless ($src);
8f4f4634 258
02574191
MT
259 # Sanitize source.
260 my $source = $src;
261 if ($source ~~ @ANY_ADDRESSES) {
262 $source = "";
263 }
264
265 foreach my $dst (@destinations) {
266 # Skip invalid rules.
267 next if (!$dst || ($dst eq "none"));
c2a1af75
MT
268
269 # Sanitize destination.
02574191 270 my $destination = $dst;
c2a1af75
MT
271 if ($destination ~~ @ANY_ADDRESSES) {
272 $destination = "";
273 }
274
8f4f4634
MT
275 # Array with iptables arguments.
276 my @options = ();
277
278 # Append protocol.
279 if ($protocol ne "all") {
8f4f4634 280 push(@options, @protocol_options);
2a81ab0d 281 }
8f4f4634 282
6e87f0aa
MT
283 # Prepare source options.
284 my @source_options = ();
8f4f4634 285 if ($source =~ /mac/) {
6e87f0aa 286 push(@source_options, $source);
c2a1af75 287 } elsif ($source) {
6e87f0aa 288 push(@source_options, ("-s", $source));
2a81ab0d 289 }
14f7cb87 290
6e87f0aa 291 # Prepare destination options.
c2a1af75
MT
292 my @destination_options = ();
293 if ($destination) {
294 push(@destination_options, ("-d", $destination));
295 }
14f7cb87 296
8f4f4634
MT
297 # Add time constraint options.
298 push(@options, @time_options);
14f7cb87 299
e9b5ba41
MT
300 my $firewall_is_in_source_subnet = 0;
301 if ($source) {
da7a2208 302 $firewall_is_in_source_subnet = &firewall_is_in_subnet($source);
e9b5ba41
MT
303 }
304
8f4f4634
MT
305 # Process NAT rules.
306 if ($NAT) {
4e54e3c6 307 my $nat_address = &fwlib::get_nat_address($$hash{$key}[29], $source);
b05ec50a 308
8f4f4634
MT
309 # Skip NAT rules if the NAT address is unknown
310 # (i.e. no internet connection has been established, yet).
311 next unless ($nat_address);
b05ec50a 312
8f4f4634
MT
313 # Destination NAT
314 if ($NAT_MODE eq "DNAT") {
6e87f0aa 315 # Make port-forwardings useable from the internal networks.
4e54e3c6 316 my @internal_addresses = &fwlib::get_internal_firewall_ip_addresses(1);
5cf8c8c1
MT
317 unless ($nat_address ~~ @internal_addresses) {
318 &add_dnat_mangle_rules($nat_address, @options);
319 }
b05ec50a 320
d7a14d01
MT
321 my @nat_options = ();
322 if ($protocol ne "all") {
323 my @nat_protocol_options = &get_protocol_options($hash, $key, $protocol, 1);
324 push(@nat_options, @nat_protocol_options);
325 }
6e87f0aa 326 push(@nat_options, @source_options);
8f4f4634 327 push(@nat_options, ("-d", $nat_address));
51cf3f8b 328 push(@nat_options, @time_options);
6e87f0aa 329
c0ce9206 330 my $dnat_port;
8f4f4634 331 if ($protocol_has_ports) {
c0ce9206
MT
332 $dnat_port = &get_dnat_target_port($hash, $key);
333 }
334
335 my @nat_action_options = ();
b05ec50a 336
c0ce9206
MT
337 # Use iptables REDIRECT
338 my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports && $dnat_port);
339 if ($use_redirect) {
340 push(@nat_action_options, ("-j", "REDIRECT", "--to-ports", $dnat_port));
341
342 # Use iptables DNAT
343 } else {
f98bb538
MT
344 if ($destination_is_firewall && !$destination) {
345 $destination = &fwlib::get_external_address();
346 }
347 next unless ($destination);
348
c0ce9206
MT
349 my ($dnat_address, $dnat_mask) = split("/", $destination);
350 @destination_options = ("-d", $dnat_address);
351
352 if ($protocol_has_ports) {
353 my $dnat_port = &get_dnat_target_port($hash, $key);
354
355 if ($dnat_port) {
356 $dnat_address .= ":$dnat_port";
357 }
86a921ee 358 }
c0ce9206
MT
359
360 push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address));
2a81ab0d 361 }
8f4f4634
MT
362
363 if ($LOG) {
3bb4bb3f 364 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '");
8f4f4634 365 }
c0ce9206 366 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options");
8f4f4634
MT
367
368 # Source NAT
369 } elsif ($NAT_MODE eq "SNAT") {
6e87f0aa
MT
370 my @nat_options = @options;
371
372 push(@nat_options, @source_options);
373 push(@nat_options, @destination_options);
374
8f4f4634 375 if ($LOG) {
3bb4bb3f 376 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '");
8f4f4634 377 }
6e87f0aa 378 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
2a81ab0d
AM
379 }
380 }
8f4f4634 381
6e87f0aa 382 push(@options, @source_options);
e9b5ba41
MT
383
384 if ($firewall_is_in_source_subnet && ($fwdfwsettings{"POLICY"} eq "MODE1") && ($chain eq $CHAIN_FORWARD)) {
385 if ($LOG && !$NAT) {
386 run("$IPTABLES -A $CHAIN_INPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '");
387 }
388 run("$IPTABLES -A $CHAIN_INPUT @options -j $target");
389 }
390
6e87f0aa
MT
391 push(@options, @destination_options);
392
8f4f4634
MT
393 # Insert firewall rule.
394 if ($LOG && !$NAT) {
0bda23f5 395 run("$IPTABLES -A $chain @options @log_limit_options -j LOG --log-prefix '$chain '");
8f4f4634
MT
396 }
397 run("$IPTABLES -A $chain @options -j $target");
2a81ab0d
AM
398 }
399 }
400 }
2a81ab0d
AM
401 }
402}
97ab0569 403
b05ec50a
MT
404# Formats the given timestamp into the iptables format which is "hh:mm" UTC.
405sub format_time {
406 my $val = shift;
407
408 # Convert the given time into minutes.
409 my $minutes = &time_convert_to_minutes($val);
410
411 # Move the timestamp into UTC.
412 $minutes += &time_utc_offset();
413
414 # Make sure $minutes is between 00:00 and 23:59.
415 if ($minutes < 0) {
416 $minutes += 1440;
417 }
418
419 if ($minutes > 1440) {
420 $minutes -= 1440;
421 }
422
423 # Format as hh:mm.
424 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
472136c9 425}
97ab0569 426
b05ec50a
MT
427# Calculates the offsets in minutes from the local timezone to UTC.
428sub time_utc_offset {
429 my @localtime = localtime(time);
430 my @gmtime = gmtime(time);
431
432 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
472136c9 433}
97ab0569 434
b05ec50a
MT
435# Takes a timestamp like "14:00" and converts it into minutes since midnight.
436sub time_convert_to_minutes {
437 my ($hrs, $min) = split(":", shift);
438
439 return ($hrs * 60) + $min;
472136c9 440}
97ab0569
MT
441
442sub p2pblock {
6178953b 443 my $P2PSTRING = "";
36196d0d
AM
444 my $DO;
445 open( FILE, "< $p2pfile" ) or die "Unable to read $p2pfile";
446 @p2ps = <FILE>;
447 close FILE;
448 my $CMD = "-m ipp2p";
449 foreach my $p2pentry (sort @p2ps) {
450 my @p2pline = split( /\;/, $p2pentry );
8d1beadc
AM
451 if ( $fwdfwsettings{'POLICY'} eq 'MODE1' ) {
452 $DO = "ACCEPT";
5238a871 453 if ("$p2pline[2]" eq "on") {
36196d0d
AM
454 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
455 }
8d1beadc 456 }else {
36196d0d 457 $DO = "RETURN";
5238a871 458 if ("$p2pline[2]" eq "off") {
36196d0d
AM
459 $P2PSTRING = "$P2PSTRING --$p2pline[1]";
460 }
461 }
462 }
68d1eb10
MT
463
464 if($P2PSTRING) {
1f9e7b53 465 run("$IPTABLES -A FORWARDFW $CMD $P2PSTRING -j $DO");
36196d0d
AM
466 }
467}
97ab0569 468
8f4f4634
MT
469sub get_protocols {
470 my $hash = shift;
471 my $key = shift;
472
473 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
474 my $uses_services = ($$hash{$key}[11] eq "ON");
475
476 my @protocols = ();
477
478 # Rules which don't have source ports or services (like ICMP, ESP, ...).
479 if (!$uses_source_ports && !$uses_services) {
480 push(@protocols, $$hash{$key}[8]);
481
482 # Rules which either use ports or services.
483 } elsif ($uses_source_ports || $uses_services) {
484 # Check if service group or service
485 if ($$hash{$key}[14] eq 'cust_srv') {
486 push(@protocols, &fwlib::get_srv_prot($$hash{$key}[15]));
487
488 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
489 my $protos = &fwlib::get_srvgrp_prot($$hash{$key}[15]);
490 push(@protocols, split(",", $protos));
491
492 } else {
493 # Fetch the protocol for this rule.
494 my $protocol = lc($$hash{$key}[8]);
495
496 # Fetch source and destination ports for this rule.
497 my $source_ports = $$hash{$key}[10];
498 my $destination_ports = $$hash{$key}[15];
499
500 # Check if ports are set for protocols which do not support ports.
501 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
502 print_error("$protocol does not support ports");
503 return ();
504 }
505
506 push(@protocols, $protocol);
2a81ab0d
AM
507 }
508 }
8f4f4634
MT
509
510 # Remove all empty elements
511 @protocols = map { $_ ? $_ : () } @protocols;
512
513 # If no protocol has been defined, we assume "all".
514 if (!@protocols) {
515 push(@protocols, "all");
98cee89f 516 }
8f4f4634
MT
517
518 # Make all protocol names lowercase.
519 @protocols = map { lc } @protocols;
520
521 return @protocols;
2a81ab0d 522}
97ab0569 523
8f4f4634
MT
524sub get_protocol_options {
525 my $hash = shift;
526 my $key = shift;
527 my $protocol = shift;
d7a14d01 528 my $nat_options_wanted = shift;
8f4f4634
MT
529 my @options = ();
530
d7a14d01
MT
531 # Nothing to do if no protocol is specified.
532 if ($protocol eq "all") {
533 return @options;
534 } else {
535 push(@options, ("-p", $protocol));
536 }
537
fcc68a42
MT
538 if ($protocol ~~ @PROTOCOLS_WITH_PORTS) {
539 # Process source ports.
540 my $use_src_ports = ($$hash{$key}[7] eq "ON");
541 my $src_ports = $$hash{$key}[10];
8f4f4634 542
fcc68a42
MT
543 if ($use_src_ports && $src_ports) {
544 push(@options, &format_ports($src_ports, "src"));
545 }
8f4f4634 546
fcc68a42
MT
547 # Process destination ports.
548 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
549 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
8f4f4634 550
fcc68a42
MT
551 if ($use_dst_ports) {
552 my $dst_ports_mode = $$hash{$key}[14];
553 my $dst_ports = $$hash{$key}[15];
8f4f4634 554
fcc68a42
MT
555 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
556 if ($nat_options_wanted && $use_dnat && $$hash{$key}[30]) {
557 $dst_ports = $$hash{$key}[30];
558 }
8f4f4634 559 push(@options, &format_ports($dst_ports, "dst"));
8f4f4634 560
fcc68a42
MT
561 } elsif ($dst_ports_mode eq "cust_srv") {
562 if ($protocol eq "ICMP") {
563 push(@options, ("--icmp-type", &fwlib::get_srv_port($dst_ports, 3, "ICMP")));
564 } else {
565 $dst_ports = &fwlib::get_srv_port($dst_ports, 1, uc($protocol));
566 push(@options, &format_ports($dst_ports, "dst"));
567 }
568
569 } elsif ($dst_ports_mode eq "cust_srvgrp") {
570 push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol)));
571 }
2a81ab0d
AM
572 }
573 }
8f4f4634
MT
574
575 # Check if a single ICMP type is selected.
fcc68a42 576 if ($protocol eq "icmp") {
8f4f4634
MT
577 my $icmp_type = $$hash{$key}[9];
578
579 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
580 push(@options, ("--icmp-type", $icmp_type));
a4c7bf6b
AM
581 }
582 }
8f4f4634
MT
583
584 return @options;
585}
586
587sub format_ports {
588 my $ports = shift;
589 my $type = shift;
590
591 my $arg;
592 if ($type eq "src") {
593 $arg = "--sport";
594 } elsif ($type eq "dst") {
595 $arg = "--dport";
596 }
597
598 my @options = ();
599
600 if ($ports =~ /\|/) {
601 $ports =~ s/\|/,/g;
602 push(@options, ("-m", "multiport"));
603 }
604
1c3044d7
MT
605 if ($ports) {
606 push(@options, ($arg, $ports));
607 }
8f4f4634
MT
608
609 return @options;
610}
611
612sub get_dnat_target_port {
613 my $hash = shift;
614 my $key = shift;
615
616 if ($$hash{$key}[14] eq "TGT_PORT") {
1c3044d7
MT
617 my $port = $$hash{$key}[15];
618 my $external_port = $$hash{$key}[30];
619
620 if ($external_port && ($port ne $external_port)) {
621 return $$hash{$key}[15];
622 }
8f4f4634 623 }
2a81ab0d 624}
6e87f0aa
MT
625
626sub add_dnat_mangle_rules {
627 my $nat_address = shift;
628 my @options = @_;
629
630 my $mark = 0;
631 foreach my $zone ("GREEN", "BLUE", "ORANGE") {
632 $mark++;
633
634 # Skip rule if not all required information exists.
635 next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
636 next unless (exists $defaultNetworks{$zone . "_NETMASK"});
637
638 my @mangle_options = @options;
639
640 my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
641 $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
642
643 push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
644 push(@mangle_options, ("-j", "MARK", "--set-mark", $mark));
645
646 run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");
647 }
648}
3bb4bb3f
MT
649
650sub make_log_limit_options {
651 my @options = ("-m", "limit");
652
653 # Maybe we should get this from the configuration.
654 my $limit = 10;
655
656 # We limit log messages to $limit messages per minute.
657 push(@options, ("--limit", "$limit/min"));
658
659 # And we allow bursts of 2x $limit.
660 push(@options, ("--limit-burst", $limit * 2));
661
662 return @options;
663}
e9b5ba41 664
da7a2208
MT
665sub firewall_is_in_subnet {
666 my $subnet = shift;
5cf8c8c1 667
e9b5ba41
MT
668 # ORANGE is missing here, because nothing may ever access
669 # the firewall from this network.
4e54e3c6 670 my $address = &fwlib::get_internal_firewall_ip_address($subnet, 0);
e9b5ba41 671
da7a2208
MT
672 if ($address) {
673 return 1;
e9b5ba41 674 }
da7a2208
MT
675
676 return 0;
e9b5ba41 677}
b0d9fad3 678