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