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