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