]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/firewall/rules.pl
Merge branch 'next' of ssh://git.ipfire.org/pub/git/ipfire-2.x into asterisk-update
[people/pmueller/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) 2013 Alexander Marx <amarx@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
24 require '/var/ipfire/general-functions.pl';
25 require "${General::swroot}/lang.pl";
26 require "/usr/lib/firewall/firewall-lib.pl";
27
28 # Set to one to enable debugging mode.
29 my $DEBUG = 0;
30
31 my $IPTABLES = "iptables --wait";
32
33 # iptables chains
34 my $CHAIN_INPUT = "INPUTFW";
35 my $CHAIN_FORWARD = "FORWARDFW";
36 my $CHAIN_OUTPUT = "OUTGOINGFW";
37 my $CHAIN = $CHAIN_FORWARD;
38 my $CHAIN_NAT_SOURCE = "NAT_SOURCE";
39 my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION";
40 my $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION";
41 my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT);
42 my @ANY_ADDRESSES = ("0.0.0.0/0.0.0.0", "0.0.0.0/0", "0/0");
43
44 my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip");
45 my @PROTOCOLS_WITH_PORTS = ("tcp", "udp");
46
47 my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT");
48
49 my %fwdfwsettings=();
50 my %fwoptions = ();
51 my %defaultNetworks=();
52 my %configfwdfw=();;
53 my %customgrp=();
54 my %configinputfw=();
55 my %configoutgoingfw=();
56 my %confignatfw=();
57 my @p2ps=();
58
59 my $configfwdfw = "${General::swroot}/firewall/config";
60 my $configinput = "${General::swroot}/firewall/input";
61 my $configoutgoing = "${General::swroot}/firewall/outgoing";
62 my $p2pfile = "${General::swroot}/firewall/p2protocols";
63 my $configgrp = "${General::swroot}/fwhosts/customgroups";
64 my $netsettings = "${General::swroot}/ethernet/settings";
65
66 &General::readhash("${General::swroot}/firewall/settings", \%fwdfwsettings);
67 &General::readhash("${General::swroot}/optionsfw/settings", \%fwoptions);
68 &General::readhash("$netsettings", \%defaultNetworks);
69 &General::readhasharray($configfwdfw, \%configfwdfw);
70 &General::readhasharray($configinput, \%configinputfw);
71 &General::readhasharray($configoutgoing, \%configoutgoingfw);
72 &General::readhasharray($configgrp, \%customgrp);
73
74 my @log_limit_options = &make_log_limit_options();
75
76 my $POLICY_INPUT_ALLOWED = 0;
77 my $POLICY_FORWARD_ALLOWED = ($fwdfwsettings{"POLICY"} eq "MODE2");
78 my $POLICY_OUTPUT_ALLOWED = ($fwdfwsettings{"POLICY1"} eq "MODE2");
79
80 my $POLICY_INPUT_ACTION = $fwoptions{"FWPOLICY2"};
81 my $POLICY_FORWARD_ACTION = $fwoptions{"FWPOLICY"};
82 my $POLICY_OUTPUT_ACTION = $fwoptions{"FWPOLICY1"};
83
84 # MAIN
85 &main();
86
87 sub 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");
99 }
100
101 sub 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";
109
110 if ($?) {
111 print_error("ERROR: $command");
112 }
113 }
114 }
115
116 sub print_error {
117 my $message = shift;
118
119 print STDERR "$message\n";
120 }
121
122 sub 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
134 sub count_elements {
135 my $hash = shift;
136
137 return scalar @$hash;
138 }
139
140 sub flush {
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");
146 run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX");
147 }
148
149 sub preparerules {
150 if (! -z "${General::swroot}/firewall/input"){
151 &buildrules(\%configinputfw);
152 }
153 if (! -z "${General::swroot}/firewall/outgoing"){
154 &buildrules(\%configoutgoingfw);
155 }
156 if (! -z "${General::swroot}/firewall/config"){
157 &buildrules(\%configfwdfw);
158 }
159 }
160
161 sub buildrules {
162 my $hash = shift;
163
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
191 foreach my $key (sort {$a <=> $b} keys %$hash) {
192 # Skip disabled rules.
193 next unless ($$hash{$key}[2] eq 'ON');
194
195 # Count number of elements in this line
196 my $elements = &count_elements($$hash{$key});
197
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.
217 my @sources = &fwlib::get_addresses($hash, $key, "src");
218
219 # Collect all destinations.
220 my @destinations = &fwlib::get_addresses($hash, $key, "tgt");
221
222 # True if the destination is the firewall itself.
223 my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire");
224
225 # Check if logging should be enabled.
226 my $LOG = ($$hash{$key}[17] eq 'ON');
227
228 # Check if NAT is enabled and initialize variables, that we use for that.
229 my $NAT = ($$hash{$key}[28] eq 'ON');
230 my $NAT_MODE;
231 if ($NAT) {
232 $NAT_MODE = uc($$hash{$key}[31]);
233 }
234
235 # Set up time constraints.
236 my @time_options = ();
237 if ($$hash{$key}[18] eq 'ON') {
238 push(@time_options, ("-m", "time"));
239
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 }
266
267 # Convert start time.
268 my $time_start = &format_time($$hash{$key}[26]);
269 if ($time_start) {
270 push(@time_options, ("--timestart", $time_start));
271 }
272
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 }
278 }
279
280 # Concurrent connection limit
281 my @ratelimit_options = ();
282
283 if (($elements gt 34) && ($$hash{$key}[32] eq 'ON')) {
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
299 if (($elements gt 37) && ($$hash{$key}[34] eq 'ON')) {
300 my $rate_limit = "$$hash{$key}[35]/$$hash{$key}[36]";
301
302 if ($rate_limit) {
303 push(@ratelimit_options, ("-m", "limit"));
304 push(@ratelimit_options, ("--limit", $rate_limit));
305 }
306 }
307
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;
321 }
322
323 # Prepare protocol options (like ICMP types, ports, etc...).
324 my @protocol_options = &get_protocol_options($hash, $key, $protocol, 0);
325
326 # Check if this protocol knows ports.
327 my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS);
328
329 foreach my $src (@sources) {
330 # Skip invalid source.
331 next unless (defined $src);
332 next unless ($src);
333
334 # Sanitize source.
335 my $source = @$src[0];
336 if ($source ~~ @ANY_ADDRESSES) {
337 $source = "";
338 }
339
340 my $source_intf = @$src[1];
341
342 foreach my $dst (@destinations) {
343 # Skip invalid rules.
344 next unless (defined $dst);
345 next if (!$dst || ($dst eq "none"));
346
347 # Sanitize destination.
348 my $destination = @$dst[0];
349 if ($destination ~~ @ANY_ADDRESSES) {
350 $destination = "";
351 }
352
353 my $destination_intf = @$dst[1];
354
355 # Array with iptables arguments.
356 my @options = ();
357
358 # Append protocol.
359 if ($protocol ne "all") {
360 push(@options, @protocol_options);
361 }
362
363 # Prepare source options.
364 my @source_options = ();
365 if ($source =~ /mac/) {
366 push(@source_options, $source);
367 } elsif ($source) {
368 push(@source_options, ("-s", $source));
369 }
370
371 if ($source_intf) {
372 push(@source_options, ("-i", $source_intf));
373 }
374
375 # Prepare destination options.
376 my @destination_options = ();
377 if ($destination) {
378 push(@destination_options, ("-d", $destination));
379 }
380
381 if ($destination_intf) {
382 push(@destination_options, ("-o", $destination_intf));
383 }
384
385 # Add time constraint options.
386 push(@options, @time_options);
387
388 # Add ratelimiting option
389 push(@options, @ratelimit_options);
390
391 my $firewall_is_in_source_subnet = 1;
392 if ($source) {
393 $firewall_is_in_source_subnet = &firewall_is_in_subnet($source);
394 }
395
396 my $firewall_is_in_destination_subnet = 1;
397 if ($destination) {
398 $firewall_is_in_destination_subnet = &firewall_is_in_subnet($destination);
399 }
400
401 # Process NAT rules.
402 if ($NAT) {
403 my $nat_address = &fwlib::get_nat_address($$hash{$key}[29], $source);
404
405 # Skip NAT rules if the NAT address is unknown
406 # (i.e. no internet connection has been established, yet).
407 next unless ($nat_address);
408
409 # Destination NAT
410 if ($NAT_MODE eq "DNAT") {
411 my @nat_options = ();
412 if ($protocol ne "all") {
413 my @nat_protocol_options = &get_protocol_options($hash, $key, $protocol, 1);
414 push(@nat_options, @nat_protocol_options);
415 }
416 push(@nat_options, @time_options);
417
418 # Make port-forwardings useable from the internal networks.
419 my @internal_addresses = &fwlib::get_internal_firewall_ip_addresses(1);
420 unless ($nat_address ~~ @internal_addresses) {
421 &add_dnat_mangle_rules($nat_address, $source_intf, @nat_options);
422 }
423
424 push(@nat_options, @source_options);
425 push(@nat_options, ("-d", $nat_address));
426
427 my $dnat_port;
428 if ($protocol_has_ports) {
429 $dnat_port = &get_dnat_target_port($hash, $key);
430 }
431
432 my @nat_action_options = ();
433
434 # Use iptables REDIRECT
435 my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports && $dnat_port);
436 if ($use_redirect) {
437 push(@nat_action_options, ("-j", "REDIRECT", "--to-ports", $dnat_port));
438
439 # Use iptables DNAT
440 } else {
441 if ($destination_is_firewall && !$destination) {
442 $destination = &fwlib::get_external_address();
443 }
444 next unless ($destination);
445
446 my ($dnat_address, $dnat_mask) = split("/", $destination);
447 @destination_options = ("-d", $dnat_address);
448
449 if ($protocol_has_ports) {
450 my $dnat_port = &get_dnat_target_port($hash, $key);
451
452 if ($dnat_port) {
453 $dnat_address .= ":$dnat_port";
454 }
455 }
456
457 push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address));
458 }
459
460 if ($LOG) {
461 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '");
462 }
463 run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options");
464
465 # Source NAT
466 } elsif ($NAT_MODE eq "SNAT") {
467 my @nat_options = @options;
468
469 push(@nat_options, @source_options);
470 push(@nat_options, @destination_options);
471
472 if ($LOG) {
473 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @log_limit_options -j LOG --log-prefix 'SNAT '");
474 }
475 run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options -j SNAT --to-source $nat_address");
476 }
477 }
478
479 push(@options, @source_options);
480 push(@options, @destination_options);
481
482 # Insert firewall rule.
483 if ($LOG && !$NAT) {
484 run("$IPTABLES -A $chain @options @log_limit_options -j LOG --log-prefix '$chain '");
485 }
486 run("$IPTABLES -A $chain @options -j $target");
487
488 # Handle forwarding rules and add corresponding rules for firewall access.
489 if ($chain eq $CHAIN_FORWARD) {
490 # If the firewall is part of the destination subnet and access to the destination network
491 # is granted/forbidden for any network that the firewall itself is part of, we grant/forbid access
492 # for the firewall, too.
493 if ($firewall_is_in_destination_subnet && ($target ~~ @special_input_targets)) {
494 if ($LOG && !$NAT) {
495 run("$IPTABLES -A $CHAIN_INPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '");
496 }
497 run("$IPTABLES -A $CHAIN_INPUT @options -j $target");
498 }
499
500 # Likewise.
501 if ($firewall_is_in_source_subnet && ($target ~~ @special_output_targets)) {
502 if ($LOG && !$NAT) {
503 run("$IPTABLES -A $CHAIN_OUTPUT @options @log_limit_options -j LOG --log-prefix '$CHAIN_OUTPUT '");
504 }
505 run("$IPTABLES -A $CHAIN_OUTPUT @options -j $target");
506 }
507 }
508 }
509 }
510 }
511 }
512 #Reload firewall.local if present
513 if ( -f '/etc/sysconfig/firewall.local'){
514 run("/etc/sysconfig/firewall.local reload");
515 }
516 }
517
518 # Formats the given timestamp into the iptables format which is "hh:mm" UTC.
519 sub format_time {
520 my $val = shift;
521
522 # Convert the given time into minutes.
523 my $minutes = &time_convert_to_minutes($val);
524
525 # Move the timestamp into UTC.
526 $minutes += &time_utc_offset();
527
528 # Make sure $minutes is between 00:00 and 23:59.
529 if ($minutes < 0) {
530 $minutes += 1440;
531 }
532
533 if ($minutes > 1440) {
534 $minutes -= 1440;
535 }
536
537 # Format as hh:mm.
538 return sprintf("%02d:%02d", $minutes / 60, $minutes % 60);
539 }
540
541 # Calculates the offsets in minutes from the local timezone to UTC.
542 sub time_utc_offset {
543 my @localtime = localtime(time);
544 my @gmtime = gmtime(time);
545
546 return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60);
547 }
548
549 # Takes a timestamp like "14:00" and converts it into minutes since midnight.
550 sub time_convert_to_minutes {
551 my ($hrs, $min) = split(":", shift);
552
553 return ($hrs * 60) + $min;
554 }
555
556 sub p2pblock {
557 open(FILE, "<$p2pfile") or die "Unable to read $p2pfile";
558 my @protocols = ();
559 foreach my $p2pentry (<FILE>) {
560 my @p2pline = split(/\;/, $p2pentry);
561 next unless ($p2pline[2] eq "off");
562
563 push(@protocols, "--$p2pline[1]");
564 }
565 close(FILE);
566
567 run("$IPTABLES -F P2PBLOCK");
568 if (@protocols) {
569 run("$IPTABLES -A P2PBLOCK -m ipp2p @protocols -j DROP");
570 }
571 }
572
573 sub get_protocols {
574 my $hash = shift;
575 my $key = shift;
576
577 my $uses_source_ports = ($$hash{$key}[7] eq "ON");
578 my $uses_services = ($$hash{$key}[11] eq "ON");
579
580 my @protocols = ();
581
582 # Rules which don't have source ports or services (like ICMP, ESP, ...).
583 if (!$uses_source_ports && !$uses_services) {
584 push(@protocols, $$hash{$key}[8]);
585
586 # Rules which either use ports or services.
587 } elsif ($uses_source_ports || $uses_services) {
588 # Check if service group or service
589 if ($$hash{$key}[14] eq 'cust_srv') {
590 push(@protocols, &fwlib::get_srv_prot($$hash{$key}[15]));
591
592 } elsif($$hash{$key}[14] eq 'cust_srvgrp'){
593 my $protos = &fwlib::get_srvgrp_prot($$hash{$key}[15]);
594 push(@protocols, split(",", $protos));
595
596 } else {
597 # Fetch the protocol for this rule.
598 my $protocol = lc($$hash{$key}[8]);
599
600 # Fetch source and destination ports for this rule.
601 my $source_ports = $$hash{$key}[10];
602 my $destination_ports = $$hash{$key}[15];
603
604 # Check if ports are set for protocols which do not support ports.
605 if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) {
606 print_error("$protocol does not support ports");
607 return ();
608 }
609
610 push(@protocols, $protocol);
611 }
612 }
613
614 # Remove all empty elements
615 @protocols = map { $_ ? $_ : () } @protocols;
616
617 # If no protocol has been defined, we assume "all".
618 if (!@protocols) {
619 push(@protocols, "all");
620 }
621
622 # Make all protocol names lowercase.
623 @protocols = map { lc } @protocols;
624
625 return @protocols;
626 }
627
628 sub get_protocol_options {
629 my $hash = shift;
630 my $key = shift;
631 my $protocol = shift;
632 my $nat_options_wanted = shift;
633 my @options = ();
634
635 # Nothing to do if no protocol is specified.
636 if ($protocol eq "all") {
637 return @options;
638 } else {
639 push(@options, ("-p", $protocol));
640 }
641
642 if ($protocol ~~ @PROTOCOLS_WITH_PORTS) {
643 # Process source ports.
644 my $use_src_ports = ($$hash{$key}[7] eq "ON");
645 my $src_ports = $$hash{$key}[10];
646
647 if ($use_src_ports && $src_ports) {
648 push(@options, &format_ports($src_ports, "src"));
649 }
650
651 # Process destination ports.
652 my $use_dst_ports = ($$hash{$key}[11] eq "ON");
653 my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat"));
654
655 if ($use_dst_ports) {
656 my $dst_ports_mode = $$hash{$key}[14];
657 my $dst_ports = $$hash{$key}[15];
658
659 if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) {
660 if ($nat_options_wanted && $use_dnat && $$hash{$key}[30]) {
661 $dst_ports = $$hash{$key}[30];
662 }
663 push(@options, &format_ports($dst_ports, "dst"));
664
665 } elsif ($dst_ports_mode eq "cust_srv") {
666 if ($protocol eq "ICMP") {
667 push(@options, ("--icmp-type", &fwlib::get_srv_port($dst_ports, 3, "ICMP")));
668 } else {
669 $dst_ports = &fwlib::get_srv_port($dst_ports, 1, uc($protocol));
670 push(@options, &format_ports($dst_ports, "dst"));
671 }
672
673 } elsif ($dst_ports_mode eq "cust_srvgrp") {
674 push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol)));
675 }
676 }
677 }
678
679 # Check if a single ICMP type is selected.
680 if ($protocol eq "icmp") {
681 my $icmp_type = $$hash{$key}[9];
682
683 if (($icmp_type ne "All ICMP-Types") && $icmp_type) {
684 push(@options, ("--icmp-type", $icmp_type));
685 }
686 }
687
688 return @options;
689 }
690
691 sub format_ports {
692 my $ports = shift;
693 my $type = shift;
694
695 my $arg;
696 if ($type eq "src") {
697 $arg = "--sport";
698 } elsif ($type eq "dst") {
699 $arg = "--dport";
700 }
701
702 my @options = ();
703
704 if ($ports =~ /\|/) {
705 $ports =~ s/\|/,/g;
706 push(@options, ("-m", "multiport"));
707 }
708
709 if ($ports) {
710 push(@options, ($arg, $ports));
711 }
712
713 return @options;
714 }
715
716 sub get_dnat_target_port {
717 my $hash = shift;
718 my $key = shift;
719
720 if ($$hash{$key}[14] eq "TGT_PORT") {
721 my $port = $$hash{$key}[15];
722 my $external_port = $$hash{$key}[30];
723
724 if ($external_port && ($port ne $external_port)) {
725 return $$hash{$key}[15];
726 }
727 }
728 }
729
730 sub add_dnat_mangle_rules {
731 my $nat_address = shift;
732 my $interface = shift;
733 my @options = @_;
734
735 my $mark = 0;
736 foreach my $zone ("GREEN", "BLUE", "ORANGE") {
737 $mark++;
738
739 # Skip rule if not all required information exists.
740 next unless (exists $defaultNetworks{$zone . "_NETADDRESS"});
741 next unless (exists $defaultNetworks{$zone . "_NETMASK"});
742
743 next if ($interface && $interface ne $defaultNetworks{$zone . "_DEV"});
744
745 my @mangle_options = @options;
746
747 my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"};
748 $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"};
749
750 push(@mangle_options, ("-s", $netaddress, "-d", $nat_address));
751 push(@mangle_options, ("-j", "MARK", "--set-mark", $mark));
752
753 run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options");
754 }
755 }
756
757 sub make_log_limit_options {
758 my @options = ("-m", "limit");
759
760 # Maybe we should get this from the configuration.
761 my $limit = 10;
762
763 # We limit log messages to $limit messages per minute.
764 push(@options, ("--limit", "$limit/min"));
765
766 # And we allow bursts of 2x $limit.
767 push(@options, ("--limit-burst", $limit * 2));
768
769 return @options;
770 }
771
772 sub firewall_is_in_subnet {
773 my $subnet = shift;
774
775 # ORANGE is missing here, because nothing may ever access
776 # the firewall from this network.
777 my $address = &fwlib::get_internal_firewall_ip_address($subnet, 0);
778
779 if ($address) {
780 return 1;
781 }
782
783 return 0;
784 }