]>
Commit | Line | Data |
---|---|---|
6178953b | 1 | #!/usr/bin/perl -w |
2a81ab0d AM |
2 | ############################################################################### |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
2293e1de | 5 | # Copyright (C) 2007-2020 IPFire Team <info@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 | 22 | use strict; |
5653e551 | 23 | use experimental 'smartmatch'; |
2a81ab0d | 24 | |
97ab0569 MT |
25 | require '/var/ipfire/general-functions.pl'; |
26 | require "${General::swroot}/lang.pl"; | |
27 | require "/usr/lib/firewall/firewall-lib.pl"; | |
5730a5bc | 28 | require "${General::swroot}/location-functions.pl"; |
a7bf1d96 | 29 | require "${General::swroot}/ipblocklist-functions.pl"; |
2a81ab0d | 30 | |
68d1eb10 MT |
31 | # Set to one to enable debugging mode. |
32 | my $DEBUG = 0; | |
33 | ||
1f9e7b53 | 34 | my $IPTABLES = "iptables --wait"; |
3d886880 | 35 | my $IPSET = "ipset"; |
1f9e7b53 | 36 | |
6178953b | 37 | # iptables chains |
8f4f4634 MT |
38 | my $CHAIN_INPUT = "INPUTFW"; |
39 | my $CHAIN_FORWARD = "FORWARDFW"; | |
d98aa95a | 40 | my $CHAIN_OUTPUT = "OUTGOINGFW"; |
8f4f4634 | 41 | my $CHAIN = $CHAIN_FORWARD; |
6178953b MT |
42 | my $CHAIN_NAT_SOURCE = "NAT_SOURCE"; |
43 | my $CHAIN_NAT_DESTINATION = "NAT_DESTINATION"; | |
6e87f0aa | 44 | my $CHAIN_MANGLE_NAT_DESTINATION_FIX = "NAT_DESTINATION"; |
8f4f4634 | 45 | my @VALID_CHAINS = ($CHAIN_INPUT, $CHAIN_FORWARD, $CHAIN_OUTPUT); |
c2a1af75 | 46 | my @ANY_ADDRESSES = ("0.0.0.0/0.0.0.0", "0.0.0.0/0", "0/0"); |
8f4f4634 MT |
47 | |
48 | my @PROTOCOLS = ("tcp", "udp", "icmp", "igmp", "ah", "esp", "gre", "ipv6", "ipip"); | |
49 | my @PROTOCOLS_WITH_PORTS = ("tcp", "udp"); | |
50 | ||
51 | my @VALID_TARGETS = ("ACCEPT", "DROP", "REJECT"); | |
6178953b | 52 | |
c69c8200 MT |
53 | my @PRIVATE_NETWORKS = ( |
54 | "10.0.0.0/8", | |
55 | "172.16.0.0/12", | |
56 | "192.168.0.0/16", | |
57 | "100.64.0.0/10", | |
58 | ); | |
59 | ||
ce31144c MT |
60 | # MARK masks |
61 | my $NAT_MASK = 0x0f000000; | |
62 | ||
7b529f54 SS |
63 | # Country code, which is used to mark hostile networks. |
64 | my $HOSTILE_CCODE = "XD"; | |
65 | ||
2a81ab0d | 66 | my %fwdfwsettings=(); |
aa5f4b65 | 67 | my %fwoptions = (); |
2a81ab0d | 68 | my %defaultNetworks=(); |
8f4f4634 | 69 | my %configfwdfw=();; |
2a81ab0d | 70 | my %customgrp=(); |
2a81ab0d | 71 | my %configinputfw=(); |
5d7faa45 | 72 | my %configoutgoingfw=(); |
a6edca5a | 73 | my %confignatfw=(); |
5730a5bc SS |
74 | my %locationsettings = ( |
75 | "LOCATIONBLOCK_ENABLED" => "off" | |
f5ad4246 | 76 | ); |
a7bf1d96 SS |
77 | my %blocklistsettings= ( |
78 | "ENABLE" => "off", | |
79 | ); | |
80 | ||
2801213d SS |
81 | my %ipset_loaded_sets = (); |
82 | my @ipset_used_sets = (); | |
f5ad4246 | 83 | |
6d8eb5de AM |
84 | my $configfwdfw = "${General::swroot}/firewall/config"; |
85 | my $configinput = "${General::swroot}/firewall/input"; | |
86 | my $configoutgoing = "${General::swroot}/firewall/outgoing"; | |
5730a5bc | 87 | my $locationfile = "${General::swroot}/firewall/locationblock"; |
2a81ab0d | 88 | my $configgrp = "${General::swroot}/fwhosts/customgroups"; |
210ee67b | 89 | my $netsettings = "${General::swroot}/ethernet/settings"; |
a7bf1d96 | 90 | my $blocklistfile = "${General::swroot}/ipblocklist/settings"; |
86a921ee | 91 | |
6d8eb5de | 92 | &General::readhash("${General::swroot}/firewall/settings", \%fwdfwsettings); |
aa5f4b65 | 93 | &General::readhash("${General::swroot}/optionsfw/settings", \%fwoptions); |
210ee67b | 94 | &General::readhash("$netsettings", \%defaultNetworks); |
2a81ab0d AM |
95 | &General::readhasharray($configfwdfw, \%configfwdfw); |
96 | &General::readhasharray($configinput, \%configinputfw); | |
5d7faa45 | 97 | &General::readhasharray($configoutgoing, \%configoutgoingfw); |
2a81ab0d | 98 | &General::readhasharray($configgrp, \%customgrp); |
2a81ab0d | 99 | |
5730a5bc SS |
100 | # Check if the location settings file exists |
101 | if (-e "$locationfile") { | |
f5ad4246 | 102 | # Read settings file |
5730a5bc | 103 | &General::readhash("$locationfile", \%locationsettings); |
f5ad4246 SS |
104 | } |
105 | ||
a7bf1d96 SS |
106 | # Check if the ipblocklist settings file exits. |
107 | if (-e "$blocklistfile") { | |
108 | # Read-in settings file. | |
109 | &General::readhash("$blocklistfile", \%blocklistsettings); | |
110 | } | |
111 | ||
5730a5bc SS |
112 | # Get all available locations. |
113 | my @locations = &Location::Functions::get_locations(); | |
f5ad4246 | 114 | |
a7bf1d96 SS |
115 | # Get all supported blocklists. |
116 | my @blocklists = &IPblocklist::get_blocklists(); | |
117 | ||
7b529f54 SS |
118 | # Name or the RED interface. |
119 | my $RED_DEV = &General::get_red_interface(); | |
120 | ||
3bb4bb3f MT |
121 | my @log_limit_options = &make_log_limit_options(); |
122 | ||
aa5f4b65 MT |
123 | my $POLICY_INPUT_ALLOWED = 0; |
124 | my $POLICY_FORWARD_ALLOWED = ($fwdfwsettings{"POLICY"} eq "MODE2"); | |
125 | my $POLICY_OUTPUT_ALLOWED = ($fwdfwsettings{"POLICY1"} eq "MODE2"); | |
126 | ||
127 | my $POLICY_INPUT_ACTION = $fwoptions{"FWPOLICY2"}; | |
128 | my $POLICY_FORWARD_ACTION = $fwoptions{"FWPOLICY"}; | |
129 | my $POLICY_OUTPUT_ACTION = $fwoptions{"FWPOLICY1"}; | |
130 | ||
0564b0c7 SS |
131 | #workaround to suppress a warning when a variable is used only once |
132 | my @dummy = ( $Location::Functions::ipset_db_directory ); | |
133 | undef (@dummy); | |
134 | ||
8531b94a MT |
135 | # MAIN |
136 | &main(); | |
137 | ||
138 | sub main { | |
2801213d | 139 | # Get currently used ipset sets. |
8b97a537 | 140 | @ipset_used_sets = &ipset_get_sets(); |
2801213d | 141 | |
8531b94a MT |
142 | # Flush all chains. |
143 | &flush(); | |
144 | ||
2d0c7a9f AM |
145 | # Prepare firewall rules. |
146 | if (! -z "${General::swroot}/firewall/input"){ | |
147 | &buildrules(\%configinputfw); | |
148 | } | |
149 | if (! -z "${General::swroot}/firewall/outgoing"){ | |
150 | &buildrules(\%configoutgoingfw); | |
151 | } | |
152 | if (! -z "${General::swroot}/firewall/config"){ | |
153 | &buildrules(\%configfwdfw); | |
154 | } | |
8531b94a | 155 | |
5730a5bc SS |
156 | # Load Location block rules. |
157 | &locationblock(); | |
211694e5 | 158 | |
7b529f54 SS |
159 | # Load rules to block hostile networks. |
160 | &drop_hostile_networks(); | |
161 | ||
a7bf1d96 SS |
162 | # Handle ipblocklist. |
163 | &ipblocklist(); | |
164 | ||
8531b94a MT |
165 | # Reload firewall policy. |
166 | run("/usr/sbin/firewall-policy"); | |
2d0c7a9f | 167 | |
2801213d SS |
168 | # Cleanup not longer needed ipset sets. |
169 | &ipset_cleanup(); | |
170 | ||
2d0c7a9f AM |
171 | #Reload firewall.local if present |
172 | if ( -f '/etc/sysconfig/firewall.local'){ | |
173 | run("/etc/sysconfig/firewall.local reload"); | |
174 | } | |
2a81ab0d | 175 | } |
97ab0569 | 176 | |
68d1eb10 MT |
177 | sub run { |
178 | # Executes or prints the given shell command. | |
179 | my $command = shift; | |
180 | ||
181 | if ($DEBUG) { | |
182 | print "$command\n"; | |
183 | } else { | |
184 | system "$command"; | |
6e87f0aa MT |
185 | |
186 | if ($?) { | |
187 | print_error("ERROR: $command"); | |
188 | } | |
68d1eb10 MT |
189 | } |
190 | } | |
191 | ||
6178953b MT |
192 | sub print_error { |
193 | my $message = shift; | |
194 | ||
195 | print STDERR "$message\n"; | |
196 | } | |
197 | ||
8f4f4634 MT |
198 | sub print_rule { |
199 | my $hash = shift; | |
200 | ||
201 | print "\nRULE:"; | |
202 | ||
203 | my $i = 0; | |
204 | foreach (@$hash) { | |
205 | printf(" %2d: %s", $i++, $_); | |
206 | } | |
207 | print "\n"; | |
208 | } | |
209 | ||
791c2b45 MT |
210 | sub count_elements { |
211 | my $hash = shift; | |
212 | ||
213 | return scalar @$hash; | |
214 | } | |
215 | ||
97ab0569 | 216 | sub flush { |
d98aa95a MT |
217 | run("$IPTABLES -F $CHAIN_INPUT"); |
218 | run("$IPTABLES -F $CHAIN_FORWARD"); | |
219 | run("$IPTABLES -F $CHAIN_OUTPUT"); | |
220 | run("$IPTABLES -t nat -F $CHAIN_NAT_SOURCE"); | |
221 | run("$IPTABLES -t nat -F $CHAIN_NAT_DESTINATION"); | |
6e87f0aa | 222 | run("$IPTABLES -t mangle -F $CHAIN_MANGLE_NAT_DESTINATION_FIX"); |
86a921ee | 223 | } |
97ab0569 | 224 | |
97ab0569 | 225 | sub buildrules { |
8f4f4634 MT |
226 | my $hash = shift; |
227 | ||
aa5f4b65 MT |
228 | # Search for targets that need to be specially handled when adding |
229 | # forwarding rules. Additional rules will automatically get inserted | |
230 | # into the INPUT/OUTPUT chains for these targets. | |
231 | my @special_input_targets = (); | |
232 | if (!$POLICY_FORWARD_ALLOWED) { | |
233 | push(@special_input_targets, "ACCEPT"); | |
234 | } | |
235 | ||
236 | if ($POLICY_INPUT_ACTION eq "DROP") { | |
ae93dd3d | 237 | push(@special_input_targets, ("ACCEPT", "REJECT")); |
aa5f4b65 | 238 | } elsif ($POLICY_INPUT_ACTION eq "REJECT") { |
ae93dd3d | 239 | push(@special_input_targets, ("ACCEPT", "DROP")); |
aa5f4b65 MT |
240 | } |
241 | ||
242 | my @special_output_targets = (); | |
243 | if ($POLICY_OUTPUT_ALLOWED) { | |
244 | push(@special_output_targets, ("DROP", "REJECT")); | |
245 | } else { | |
246 | push(@special_output_targets, "ACCEPT"); | |
247 | ||
248 | if ($POLICY_OUTPUT_ACTION eq "DROP") { | |
ae93dd3d | 249 | push(@special_output_targets, ("ACCEPT", "REJECT")); |
aa5f4b65 | 250 | } elsif ($POLICY_OUTPUT_ACTION eq "REJECT") { |
ae93dd3d | 251 | push(@special_output_targets, ("ACCEPT", "DROP")); |
aa5f4b65 MT |
252 | } |
253 | } | |
254 | ||
8f4f4634 MT |
255 | foreach my $key (sort {$a <=> $b} keys %$hash) { |
256 | # Skip disabled rules. | |
257 | next unless ($$hash{$key}[2] eq 'ON'); | |
258 | ||
791c2b45 MT |
259 | # Count number of elements in this line |
260 | my $elements = &count_elements($$hash{$key}); | |
261 | ||
8f4f4634 MT |
262 | if ($DEBUG) { |
263 | print_rule($$hash{$key}); | |
264 | } | |
265 | ||
266 | # Check if the target is valid. | |
267 | my $target = $$hash{$key}[0]; | |
268 | if (!$target ~~ @VALID_TARGETS) { | |
269 | print_error("Invalid target '$target' for rule $key"); | |
270 | next; | |
271 | } | |
272 | ||
273 | # Check if the chain is valid. | |
274 | my $chain = $$hash{$key}[1]; | |
275 | if (!$chain ~~ @VALID_CHAINS) { | |
276 | print_error("Invalid chain '$chain' in rule $key"); | |
277 | next; | |
278 | } | |
279 | ||
280 | # Collect all sources. | |
4e54e3c6 | 281 | my @sources = &fwlib::get_addresses($hash, $key, "src"); |
8f4f4634 MT |
282 | |
283 | # Collect all destinations. | |
4e54e3c6 | 284 | my @destinations = &fwlib::get_addresses($hash, $key, "tgt"); |
6178953b | 285 | |
c0ce9206 MT |
286 | # True if the destination is the firewall itself. |
287 | my $destination_is_firewall = ($$hash{$key}[5] eq "ipfire"); | |
288 | ||
6178953b | 289 | # Check if logging should be enabled. |
8f4f4634 | 290 | my $LOG = ($$hash{$key}[17] eq 'ON'); |
6178953b | 291 | |
8f4f4634 MT |
292 | # Check if NAT is enabled and initialize variables, that we use for that. |
293 | my $NAT = ($$hash{$key}[28] eq 'ON'); | |
6178953b | 294 | my $NAT_MODE; |
8f4f4634 MT |
295 | if ($NAT) { |
296 | $NAT_MODE = uc($$hash{$key}[31]); | |
297 | } | |
6178953b | 298 | |
8f4f4634 MT |
299 | # Set up time constraints. |
300 | my @time_options = (); | |
301 | if ($$hash{$key}[18] eq 'ON') { | |
302 | push(@time_options, ("-m", "time")); | |
6178953b | 303 | |
8f4f4634 MT |
304 | # Select all days of the week this match is active. |
305 | my @weekdays = (); | |
306 | if ($$hash{$key}[19] ne '') { | |
307 | push (@weekdays, "Mon"); | |
308 | } | |
309 | if ($$hash{$key}[20] ne '') { | |
310 | push (@weekdays, "Tue"); | |
311 | } | |
312 | if ($$hash{$key}[21] ne '') { | |
313 | push (@weekdays, "Wed"); | |
314 | } | |
315 | if ($$hash{$key}[22] ne '') { | |
316 | push (@weekdays, "Thu"); | |
317 | } | |
318 | if ($$hash{$key}[23] ne '') { | |
319 | push (@weekdays, "Fri"); | |
320 | } | |
321 | if ($$hash{$key}[24] ne '') { | |
322 | push (@weekdays, "Sat"); | |
323 | } | |
324 | if ($$hash{$key}[25] ne '') { | |
325 | push (@weekdays, "Sun"); | |
326 | } | |
327 | if (@weekdays) { | |
328 | push(@time_options, ("--weekdays", join(",", @weekdays))); | |
329 | } | |
6178953b | 330 | |
8f4f4634 MT |
331 | # Convert start time. |
332 | my $time_start = &format_time($$hash{$key}[26]); | |
333 | if ($time_start) { | |
334 | push(@time_options, ("--timestart", $time_start)); | |
a6edca5a | 335 | } |
6178953b | 336 | |
8f4f4634 MT |
337 | # Convert end time. |
338 | my $time_stop = &format_time($$hash{$key}[27]); | |
339 | if ($time_stop) { | |
340 | push(@time_options, ("--timestop", $time_stop)); | |
341 | } | |
a6edca5a | 342 | } |
6178953b | 343 | |
d2793ea8 AM |
344 | # Concurrent connection limit |
345 | my @ratelimit_options = (); | |
791c2b45 | 346 | |
d840d02a | 347 | if (($elements ge 34) && ($$hash{$key}[32] eq 'ON')) { |
d2793ea8 AM |
348 | my $conn_limit = $$hash{$key}[33]; |
349 | ||
350 | if ($conn_limit ge 1) { | |
351 | push(@ratelimit_options, ("-m", "connlimit")); | |
352 | ||
353 | # Use the the entire source IP address | |
354 | push(@ratelimit_options, "--connlimit-saddr"); | |
355 | push(@ratelimit_options, ("--connlimit-mask", "32")); | |
356 | ||
357 | # Apply the limit | |
358 | push(@ratelimit_options, ("--connlimit-upto", $conn_limit)); | |
359 | } | |
360 | } | |
361 | ||
362 | # Ratelimit | |
d840d02a | 363 | if (($elements ge 37) && ($$hash{$key}[34] eq 'ON')) { |
d2793ea8 AM |
364 | my $rate_limit = "$$hash{$key}[35]/$$hash{$key}[36]"; |
365 | ||
d840d02a MT |
366 | if ($rate_limit) { |
367 | push(@ratelimit_options, ("-m", "limit")); | |
368 | push(@ratelimit_options, ("--limit", $rate_limit)); | |
369 | } | |
d2793ea8 AM |
370 | } |
371 | ||
8f4f4634 MT |
372 | # Check which protocols are used in this rule and so that we can |
373 | # later group rules by protocols. | |
374 | my @protocols = &get_protocols($hash, $key); | |
375 | if (!@protocols) { | |
376 | print_error("Invalid protocol configuration for rule $key"); | |
377 | next; | |
378 | } | |
379 | ||
380 | foreach my $protocol (@protocols) { | |
381 | # Check if the given protocol is supported. | |
382 | if (($protocol ne "all") && (!$protocol ~~ @PROTOCOLS)) { | |
383 | print_error("Protocol $protocol is not supported (rule $key)"); | |
384 | next; | |
2a81ab0d | 385 | } |
8f4f4634 MT |
386 | |
387 | # Prepare protocol options (like ICMP types, ports, etc...). | |
d7a14d01 | 388 | my @protocol_options = &get_protocol_options($hash, $key, $protocol, 0); |
8f4f4634 MT |
389 | |
390 | # Check if this protocol knows ports. | |
391 | my $protocol_has_ports = ($protocol ~~ @PROTOCOLS_WITH_PORTS); | |
392 | ||
02574191 MT |
393 | foreach my $src (@sources) { |
394 | # Skip invalid source. | |
4e9a2b57 | 395 | next unless (defined $src); |
02574191 | 396 | next unless ($src); |
8f4f4634 | 397 | |
02574191 | 398 | # Sanitize source. |
48f07c19 | 399 | my $source = @$src[0]; |
02574191 MT |
400 | if ($source ~~ @ANY_ADDRESSES) { |
401 | $source = ""; | |
402 | } | |
403 | ||
48f07c19 AM |
404 | my $source_intf = @$src[1]; |
405 | ||
02574191 MT |
406 | foreach my $dst (@destinations) { |
407 | # Skip invalid rules. | |
4e9a2b57 | 408 | next unless (defined $dst); |
02574191 | 409 | next if (!$dst || ($dst eq "none")); |
c2a1af75 MT |
410 | |
411 | # Sanitize destination. | |
48f07c19 | 412 | my $destination = @$dst[0]; |
c2a1af75 MT |
413 | if ($destination ~~ @ANY_ADDRESSES) { |
414 | $destination = ""; | |
415 | } | |
416 | ||
48f07c19 AM |
417 | my $destination_intf = @$dst[1]; |
418 | ||
8f4f4634 MT |
419 | # Array with iptables arguments. |
420 | my @options = (); | |
421 | ||
422 | # Append protocol. | |
423 | if ($protocol ne "all") { | |
8f4f4634 | 424 | push(@options, @protocol_options); |
2a81ab0d | 425 | } |
8f4f4634 | 426 | |
6e87f0aa MT |
427 | # Prepare source options. |
428 | my @source_options = (); | |
8f4f4634 | 429 | if ($source =~ /mac/) { |
6e87f0aa | 430 | push(@source_options, $source); |
07106467 | 431 | } elsif ($source =~ /-m set/) { |
abf148ce SS |
432 | # Split given arguments into single chunks to |
433 | # obtain the set name. | |
434 | my ($a, $b, $c, $loc_src, $e) = split(/ /, $source); | |
07106467 | 435 | |
bae9b5dc SS |
436 | # Call function to load the networks list for this country. |
437 | &ipset_restore($loc_src); | |
07106467 | 438 | |
b9ca2fa6 AM |
439 | push(@source_options, $source); |
440 | } elsif($source) { | |
6e87f0aa | 441 | push(@source_options, ("-s", $source)); |
2a81ab0d | 442 | } |
14f7cb87 | 443 | |
6e87f0aa | 444 | # Prepare destination options. |
c2a1af75 | 445 | my @destination_options = (); |
07106467 | 446 | if ($destination =~ /-m set/) { |
abf148ce SS |
447 | # Split given arguments into single chunks to |
448 | # obtain the set name. | |
449 | my ($a, $b, $c, $loc_dst, $e) = split(/ /, $destination); | |
07106467 | 450 | |
bae9b5dc SS |
451 | # Call function to load the networks list for this country. |
452 | &ipset_restore($loc_dst); | |
07106467 | 453 | |
b9ca2fa6 AM |
454 | push(@destination_options, $destination); |
455 | } elsif ($destination) { | |
c2a1af75 MT |
456 | push(@destination_options, ("-d", $destination)); |
457 | } | |
14f7cb87 | 458 | |
249839b0 MT |
459 | # Add source and destination interface to the filter rules. |
460 | # These are supposed to help filtering forged packets that originate | |
461 | # from BLUE with an IP address from GREEN for instance. | |
462 | my @source_intf_options = (); | |
463 | if ($source_intf) { | |
464 | push(@source_intf_options, ("-i", $source_intf)); | |
465 | } | |
466 | ||
467 | my @destination_intf_options = (); | |
468 | if ($destination_intf) { | |
469 | push(@destination_intf_options, ("-o", $destination_intf)); | |
470 | } | |
471 | ||
8f4f4634 MT |
472 | # Add time constraint options. |
473 | push(@options, @time_options); | |
14f7cb87 | 474 | |
d2793ea8 AM |
475 | # Add ratelimiting option |
476 | push(@options, @ratelimit_options); | |
477 | ||
aa5f4b65 | 478 | my $firewall_is_in_source_subnet = 1; |
e9b5ba41 | 479 | if ($source) { |
da7a2208 | 480 | $firewall_is_in_source_subnet = &firewall_is_in_subnet($source); |
e9b5ba41 MT |
481 | } |
482 | ||
aa5f4b65 MT |
483 | my $firewall_is_in_destination_subnet = 1; |
484 | if ($destination) { | |
485 | $firewall_is_in_destination_subnet = &firewall_is_in_subnet($destination); | |
486 | } | |
487 | ||
8f4f4634 MT |
488 | # Process NAT rules. |
489 | if ($NAT) { | |
4e54e3c6 | 490 | my $nat_address = &fwlib::get_nat_address($$hash{$key}[29], $source); |
b05ec50a | 491 | |
8f4f4634 MT |
492 | # Skip NAT rules if the NAT address is unknown |
493 | # (i.e. no internet connection has been established, yet). | |
494 | next unless ($nat_address); | |
b05ec50a | 495 | |
8f4f4634 MT |
496 | # Destination NAT |
497 | if ($NAT_MODE eq "DNAT") { | |
d7a14d01 MT |
498 | my @nat_options = (); |
499 | if ($protocol ne "all") { | |
500 | my @nat_protocol_options = &get_protocol_options($hash, $key, $protocol, 1); | |
501 | push(@nat_options, @nat_protocol_options); | |
502 | } | |
21b37391 SS |
503 | |
504 | # Add time options. | |
ff7cb6d6 MT |
505 | push(@nat_options, @time_options); |
506 | ||
21b37391 SS |
507 | # Determine if a REDIRECT rule should be created. |
508 | my $use_redirect = ($destination_is_firewall && !$destination && $protocol_has_ports); | |
509 | ||
ff7cb6d6 | 510 | # Make port-forwardings useable from the internal networks. |
21b37391 SS |
511 | if (!$use_redirect) { |
512 | my @internal_addresses = &fwlib::get_internal_firewall_ip_addresses(1); | |
513 | unless ($nat_address ~~ @internal_addresses) { | |
514 | &add_dnat_mangle_rules($nat_address, $source_intf, @nat_options); | |
515 | } | |
ff7cb6d6 MT |
516 | } |
517 | ||
21b37391 | 518 | # Add source options. |
6e87f0aa | 519 | push(@nat_options, @source_options); |
21b37391 SS |
520 | |
521 | # Add NAT address. | |
522 | if (!$use_redirect) { | |
523 | push(@nat_options, ("-d", $nat_address)); | |
524 | } | |
6e87f0aa | 525 | |
c0ce9206 | 526 | my $dnat_port; |
8f4f4634 | 527 | if ($protocol_has_ports) { |
c0ce9206 MT |
528 | $dnat_port = &get_dnat_target_port($hash, $key); |
529 | } | |
530 | ||
531 | my @nat_action_options = (); | |
b05ec50a | 532 | |
c0ce9206 | 533 | # Use iptables REDIRECT |
c0ce9206 | 534 | if ($use_redirect) { |
21b37391 SS |
535 | push(@nat_action_options, ("-j", "REDIRECT")); |
536 | ||
537 | # Redirect to specified port if one has given. | |
538 | if ($dnat_port) { | |
539 | push(@nat_action_options, ("--to-ports", $dnat_port)); | |
540 | } | |
c0ce9206 MT |
541 | |
542 | # Use iptables DNAT | |
543 | } else { | |
f98bb538 MT |
544 | if ($destination_is_firewall && !$destination) { |
545 | $destination = &fwlib::get_external_address(); | |
546 | } | |
547 | next unless ($destination); | |
548 | ||
c0ce9206 MT |
549 | my ($dnat_address, $dnat_mask) = split("/", $destination); |
550 | @destination_options = ("-d", $dnat_address); | |
551 | ||
552 | if ($protocol_has_ports) { | |
553 | my $dnat_port = &get_dnat_target_port($hash, $key); | |
554 | ||
555 | if ($dnat_port) { | |
556 | $dnat_address .= ":$dnat_port"; | |
557 | } | |
86a921ee | 558 | } |
c0ce9206 MT |
559 | |
560 | push(@nat_action_options, ("-j", "DNAT", "--to-destination", $dnat_address)); | |
2a81ab0d | 561 | } |
8f4f4634 MT |
562 | |
563 | if ($LOG) { | |
3bb4bb3f | 564 | run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @log_limit_options -j LOG --log-prefix 'DNAT '"); |
8f4f4634 | 565 | } |
c0ce9206 | 566 | run("$IPTABLES -t nat -A $CHAIN_NAT_DESTINATION @nat_options @nat_action_options"); |
8f4f4634 MT |
567 | |
568 | # Source NAT | |
569 | } elsif ($NAT_MODE eq "SNAT") { | |
c4b7692a | 570 | my @snat_options = ( "-m", "policy", "--dir", "out", "--pol", "none" ); |
6e87f0aa MT |
571 | my @nat_options = @options; |
572 | ||
c4b7692a SS |
573 | # Get addresses for the configured firewall interfaces. |
574 | my @local_addresses = &fwlib::get_internal_firewall_ip_addresses(1); | |
575 | ||
576 | # Check if the nat_address is one of the local addresses. | |
577 | foreach my $local_address (@local_addresses) { | |
578 | if ($nat_address eq $local_address) { | |
579 | # Clear SNAT options. | |
580 | @snat_options = (); | |
581 | ||
582 | # Finish loop. | |
583 | last; | |
584 | } | |
585 | } | |
586 | ||
249839b0 | 587 | push(@nat_options, @destination_intf_options); |
6e87f0aa MT |
588 | push(@nat_options, @source_options); |
589 | push(@nat_options, @destination_options); | |
590 | ||
8f4f4634 | 591 | if ($LOG) { |
c4b7692a | 592 | run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @snat_options @log_limit_options -j LOG --log-prefix 'SNAT '"); |
8f4f4634 | 593 | } |
c4b7692a | 594 | run("$IPTABLES -t nat -A $CHAIN_NAT_SOURCE @nat_options @snat_options -j SNAT --to-source $nat_address"); |
2a81ab0d AM |
595 | } |
596 | } | |
8f4f4634 | 597 | |
6e87f0aa MT |
598 | push(@options, @source_options); |
599 | push(@options, @destination_options); | |
600 | ||
8f4f4634 | 601 | # Insert firewall rule. |
c22369a9 | 602 | if ($LOG) { |
249839b0 | 603 | run("$IPTABLES -A $chain @options @source_intf_options @destination_intf_options @log_limit_options -j LOG --log-prefix '$chain '"); |
8f4f4634 | 604 | } |
249839b0 | 605 | run("$IPTABLES -A $chain @options @source_intf_options @destination_intf_options -j $target"); |
aa5f4b65 MT |
606 | |
607 | # Handle forwarding rules and add corresponding rules for firewall access. | |
608 | if ($chain eq $CHAIN_FORWARD) { | |
609 | # If the firewall is part of the destination subnet and access to the destination network | |
610 | # is granted/forbidden for any network that the firewall itself is part of, we grant/forbid access | |
611 | # for the firewall, too. | |
612 | if ($firewall_is_in_destination_subnet && ($target ~~ @special_input_targets)) { | |
c22369a9 | 613 | if ($LOG) { |
249839b0 | 614 | run("$IPTABLES -A $CHAIN_INPUT @options @source_intf_options @log_limit_options -j LOG --log-prefix '$CHAIN_INPUT '"); |
aa5f4b65 | 615 | } |
249839b0 | 616 | run("$IPTABLES -A $CHAIN_INPUT @options @source_intf_options -j $target"); |
aa5f4b65 MT |
617 | } |
618 | ||
619 | # Likewise. | |
620 | if ($firewall_is_in_source_subnet && ($target ~~ @special_output_targets)) { | |
c22369a9 | 621 | if ($LOG) { |
249839b0 | 622 | run("$IPTABLES -A $CHAIN_OUTPUT @options @destination_intf_options @log_limit_options -j LOG --log-prefix '$CHAIN_OUTPUT '"); |
aa5f4b65 | 623 | } |
249839b0 | 624 | run("$IPTABLES -A $CHAIN_OUTPUT @options @destination_intf_options -j $target"); |
aa5f4b65 MT |
625 | } |
626 | } | |
2a81ab0d AM |
627 | } |
628 | } | |
629 | } | |
2a81ab0d AM |
630 | } |
631 | } | |
97ab0569 | 632 | |
b05ec50a MT |
633 | # Formats the given timestamp into the iptables format which is "hh:mm" UTC. |
634 | sub format_time { | |
635 | my $val = shift; | |
636 | ||
637 | # Convert the given time into minutes. | |
638 | my $minutes = &time_convert_to_minutes($val); | |
639 | ||
640 | # Move the timestamp into UTC. | |
641 | $minutes += &time_utc_offset(); | |
642 | ||
643 | # Make sure $minutes is between 00:00 and 23:59. | |
644 | if ($minutes < 0) { | |
645 | $minutes += 1440; | |
646 | } | |
647 | ||
648 | if ($minutes > 1440) { | |
649 | $minutes -= 1440; | |
650 | } | |
651 | ||
652 | # Format as hh:mm. | |
653 | return sprintf("%02d:%02d", $minutes / 60, $minutes % 60); | |
472136c9 | 654 | } |
97ab0569 | 655 | |
b05ec50a MT |
656 | # Calculates the offsets in minutes from the local timezone to UTC. |
657 | sub time_utc_offset { | |
658 | my @localtime = localtime(time); | |
659 | my @gmtime = gmtime(time); | |
660 | ||
661 | return ($gmtime[2] * 60 + $gmtime[1] % 60) - ($localtime[2] * 60 + $localtime[1] % 60); | |
472136c9 | 662 | } |
97ab0569 | 663 | |
b05ec50a MT |
664 | # Takes a timestamp like "14:00" and converts it into minutes since midnight. |
665 | sub time_convert_to_minutes { | |
666 | my ($hrs, $min) = split(":", shift); | |
667 | ||
668 | return ($hrs * 60) + $min; | |
472136c9 | 669 | } |
97ab0569 | 670 | |
5730a5bc | 671 | sub locationblock { |
2801213d SS |
672 | # Flush LOCATIONBLOCK chain. |
673 | run("$IPTABLES -F LOCATIONBLOCK"); | |
211694e5 | 674 | |
5730a5bc SS |
675 | # If location blocking is not enabled, we are finished here. |
676 | if ($locationsettings{'LOCATIONBLOCK_ENABLED'} ne "on") { | |
211694e5 SS |
677 | # Exit submodule. Process remaining script. |
678 | return; | |
679 | } | |
680 | ||
2293e1de PM |
681 | # Only check the RED interface, which is ppp0 in case of RED_TYPE being |
682 | # set to "PPPOE", and red0 in case of RED_TYPE not being empty otherwise. | |
683 | if ($defaultNetworks{'RED_TYPE'} eq "PPPOE") { | |
684 | run("$IPTABLES -A LOCATIONBLOCK ! -i ppp0 -j RETURN"); | |
685 | } elsif ($defaultNetworks{'RED_DEV'} ne "") { | |
c69c8200 MT |
686 | run("$IPTABLES -A LOCATIONBLOCK ! -i $defaultNetworks{'RED_DEV'} -j RETURN"); |
687 | } | |
688 | ||
689 | # Do not check any private address space | |
690 | foreach my $network (@PRIVATE_NETWORKS) { | |
691 | run("$IPTABLES -A LOCATIONBLOCK -s $network -j RETURN"); | |
692 | } | |
693 | ||
5730a5bc SS |
694 | # Loop through all supported locations and |
695 | # create iptables rules, if blocking for this country | |
211694e5 SS |
696 | # is enabled. |
697 | foreach my $location (@locations) { | |
5730a5bc | 698 | if(exists $locationsettings{$location} && $locationsettings{$location} eq "on") { |
bae9b5dc SS |
699 | # Call function to load the networks list for this country. |
700 | &ipset_restore($location); | |
0df1d268 SS |
701 | |
702 | # Call iptables and create rule to use the loaded ipset list. | |
2801213d | 703 | run("$IPTABLES -A LOCATIONBLOCK -m set --match-set $location src -j DROP"); |
211694e5 SS |
704 | } |
705 | } | |
706 | } | |
707 | ||
7b529f54 SS |
708 | sub drop_hostile_networks () { |
709 | # Flush the HOSTILE firewall chain. | |
710 | run("$IPTABLES -F HOSTILE"); | |
711 | ||
712 | # If dropping hostile networks is not enabled, we are finished here. | |
713 | if ($fwoptions{'DROPHOSTILE'} ne "on") { | |
714 | # Exit function. | |
715 | return; | |
716 | } | |
717 | ||
74659290 SS |
718 | # Exit if there is no red interface. |
719 | return unless($RED_DEV); | |
720 | ||
7b529f54 SS |
721 | # Call function to load the network list of hostile networks. |
722 | &ipset_restore($HOSTILE_CCODE); | |
723 | ||
e77d960b MT |
724 | # Check traffic in incoming/outgoing direction and drop if it matches |
725 | run("$IPTABLES -A HOSTILE -i $RED_DEV -m set --match-set $HOSTILE_CCODE src -j HOSTILE_DROP"); | |
726 | run("$IPTABLES -A HOSTILE -o $RED_DEV -m set --match-set $HOSTILE_CCODE dst -j HOSTILE_DROP"); | |
7b529f54 SS |
727 | } |
728 | ||
a7bf1d96 SS |
729 | sub ipblocklist () { |
730 | # Flush the ipblocklist chains. | |
731 | run("$IPTABLES -F BLOCKLISTIN"); | |
732 | run("$IPTABLES -F BLOCKLISTOUT"); | |
733 | ||
a7bf1d96 SS |
734 | # Loop through the array of blocklists. |
735 | foreach my $blocklist (@blocklists) { | |
aba4e1cd SS |
736 | # Check if the blocklist feature and the current processed blocklist is enabled. |
737 | if(($blocklistsettings{'ENABLE'} eq "on") && ($blocklistsettings{$blocklist}) && ($blocklistsettings{$blocklist} eq "on")) { | |
738 | # Call function to load the blocklist. | |
739 | &ipset_restore($blocklist); | |
740 | ||
741 | # Call function to check if the corresponding iptables drop chain already has been created. | |
742 | if(&firewall_chain_exists("${blocklist}_DROP")) { | |
743 | # Create iptables chain. | |
744 | run("$IPTABLES -N ${blocklist}_DROP"); | |
adbd2bd6 SS |
745 | } else { |
746 | # Flush the chain. | |
747 | run("$IPTABLES -F ${blocklist}_DROP"); | |
748 | } | |
aba4e1cd | 749 | |
adbd2bd6 SS |
750 | # Check if logging is enabled. |
751 | if($blocklistsettings{'LOGGING'} eq "on") { | |
752 | # Create logging rule. | |
753 | run("$IPTABLES -A ${blocklist}_DROP -j LOG -m limit --limit 10/second --log-prefix \"BLKLST_$blocklist\" "); | |
aba4e1cd | 754 | } |
a7bf1d96 | 755 | |
adbd2bd6 SS |
756 | # Create Drop rule. |
757 | run("$IPTABLES -A ${blocklist}_DROP -j DROP"); | |
758 | ||
aba4e1cd SS |
759 | # Add the rules to check against the set |
760 | run("$IPTABLES -A BLOCKLISTIN -p ALL -i $RED_DEV -m set --match-set $blocklist src -j ${blocklist}_DROP"); | |
761 | run("$IPTABLES -A BLOCKLISTOUT -p ALL -o $RED_DEV -m set --match-set $blocklist dst -j ${blocklist}_DROP"); | |
a7bf1d96 | 762 | |
aba4e1cd SS |
763 | # IP blocklist or the blocklist is disabled. |
764 | } else { | |
765 | # Check if the blocklist related iptables drop chain exits. | |
766 | unless(&firewall_chain_exists("${blocklist}_DROP")) { | |
767 | # Flush the chain. | |
768 | run("$IPTABLES -F ${blocklist}_DROP"); | |
a7bf1d96 | 769 | |
aba4e1cd SS |
770 | # Drop the chain. |
771 | run("$IPTABLES -X ${blocklist}_DROP"); | |
772 | } | |
773 | } | |
a7bf1d96 SS |
774 | } |
775 | } | |
776 | ||
8f4f4634 MT |
777 | sub get_protocols { |
778 | my $hash = shift; | |
779 | my $key = shift; | |
780 | ||
781 | my $uses_source_ports = ($$hash{$key}[7] eq "ON"); | |
782 | my $uses_services = ($$hash{$key}[11] eq "ON"); | |
783 | ||
784 | my @protocols = (); | |
785 | ||
786 | # Rules which don't have source ports or services (like ICMP, ESP, ...). | |
787 | if (!$uses_source_ports && !$uses_services) { | |
788 | push(@protocols, $$hash{$key}[8]); | |
789 | ||
790 | # Rules which either use ports or services. | |
791 | } elsif ($uses_source_ports || $uses_services) { | |
792 | # Check if service group or service | |
793 | if ($$hash{$key}[14] eq 'cust_srv') { | |
794 | push(@protocols, &fwlib::get_srv_prot($$hash{$key}[15])); | |
795 | ||
796 | } elsif($$hash{$key}[14] eq 'cust_srvgrp'){ | |
797 | my $protos = &fwlib::get_srvgrp_prot($$hash{$key}[15]); | |
798 | push(@protocols, split(",", $protos)); | |
799 | ||
800 | } else { | |
801 | # Fetch the protocol for this rule. | |
802 | my $protocol = lc($$hash{$key}[8]); | |
803 | ||
804 | # Fetch source and destination ports for this rule. | |
805 | my $source_ports = $$hash{$key}[10]; | |
806 | my $destination_ports = $$hash{$key}[15]; | |
807 | ||
808 | # Check if ports are set for protocols which do not support ports. | |
809 | if (!($protocol ~~ @PROTOCOLS_WITH_PORTS) && ($source_ports || $destination_ports)) { | |
810 | print_error("$protocol does not support ports"); | |
811 | return (); | |
812 | } | |
813 | ||
814 | push(@protocols, $protocol); | |
2a81ab0d AM |
815 | } |
816 | } | |
8f4f4634 MT |
817 | |
818 | # Remove all empty elements | |
819 | @protocols = map { $_ ? $_ : () } @protocols; | |
820 | ||
821 | # If no protocol has been defined, we assume "all". | |
822 | if (!@protocols) { | |
823 | push(@protocols, "all"); | |
98cee89f | 824 | } |
8f4f4634 MT |
825 | |
826 | # Make all protocol names lowercase. | |
827 | @protocols = map { lc } @protocols; | |
828 | ||
829 | return @protocols; | |
2a81ab0d | 830 | } |
97ab0569 | 831 | |
8f4f4634 MT |
832 | sub get_protocol_options { |
833 | my $hash = shift; | |
834 | my $key = shift; | |
835 | my $protocol = shift; | |
d7a14d01 | 836 | my $nat_options_wanted = shift; |
8f4f4634 MT |
837 | my @options = (); |
838 | ||
d7a14d01 MT |
839 | # Nothing to do if no protocol is specified. |
840 | if ($protocol eq "all") { | |
841 | return @options; | |
842 | } else { | |
843 | push(@options, ("-p", $protocol)); | |
844 | } | |
845 | ||
fcc68a42 MT |
846 | if ($protocol ~~ @PROTOCOLS_WITH_PORTS) { |
847 | # Process source ports. | |
848 | my $use_src_ports = ($$hash{$key}[7] eq "ON"); | |
849 | my $src_ports = $$hash{$key}[10]; | |
8f4f4634 | 850 | |
fcc68a42 MT |
851 | if ($use_src_ports && $src_ports) { |
852 | push(@options, &format_ports($src_ports, "src")); | |
853 | } | |
8f4f4634 | 854 | |
fcc68a42 MT |
855 | # Process destination ports. |
856 | my $use_dst_ports = ($$hash{$key}[11] eq "ON"); | |
857 | my $use_dnat = (($$hash{$key}[28] eq "ON") && ($$hash{$key}[31] eq "dnat")); | |
8f4f4634 | 858 | |
fcc68a42 MT |
859 | if ($use_dst_ports) { |
860 | my $dst_ports_mode = $$hash{$key}[14]; | |
861 | my $dst_ports = $$hash{$key}[15]; | |
8f4f4634 | 862 | |
fcc68a42 MT |
863 | if (($dst_ports_mode eq "TGT_PORT") && $dst_ports) { |
864 | if ($nat_options_wanted && $use_dnat && $$hash{$key}[30]) { | |
865 | $dst_ports = $$hash{$key}[30]; | |
866 | } | |
8f4f4634 | 867 | push(@options, &format_ports($dst_ports, "dst")); |
8f4f4634 | 868 | |
fcc68a42 MT |
869 | } elsif ($dst_ports_mode eq "cust_srv") { |
870 | if ($protocol eq "ICMP") { | |
871 | push(@options, ("--icmp-type", &fwlib::get_srv_port($dst_ports, 3, "ICMP"))); | |
872 | } else { | |
873 | $dst_ports = &fwlib::get_srv_port($dst_ports, 1, uc($protocol)); | |
874 | push(@options, &format_ports($dst_ports, "dst")); | |
875 | } | |
876 | ||
877 | } elsif ($dst_ports_mode eq "cust_srvgrp") { | |
878 | push(@options, &fwlib::get_srvgrp_port($dst_ports, uc($protocol))); | |
879 | } | |
2a81ab0d AM |
880 | } |
881 | } | |
8f4f4634 MT |
882 | |
883 | # Check if a single ICMP type is selected. | |
fcc68a42 | 884 | if ($protocol eq "icmp") { |
8f4f4634 MT |
885 | my $icmp_type = $$hash{$key}[9]; |
886 | ||
887 | if (($icmp_type ne "All ICMP-Types") && $icmp_type) { | |
888 | push(@options, ("--icmp-type", $icmp_type)); | |
a4c7bf6b AM |
889 | } |
890 | } | |
8f4f4634 MT |
891 | |
892 | return @options; | |
893 | } | |
894 | ||
895 | sub format_ports { | |
896 | my $ports = shift; | |
897 | my $type = shift; | |
898 | ||
899 | my $arg; | |
900 | if ($type eq "src") { | |
901 | $arg = "--sport"; | |
902 | } elsif ($type eq "dst") { | |
903 | $arg = "--dport"; | |
904 | } | |
905 | ||
906 | my @options = (); | |
907 | ||
908 | if ($ports =~ /\|/) { | |
909 | $ports =~ s/\|/,/g; | |
910 | push(@options, ("-m", "multiport")); | |
911 | } | |
912 | ||
1c3044d7 MT |
913 | if ($ports) { |
914 | push(@options, ($arg, $ports)); | |
915 | } | |
8f4f4634 MT |
916 | |
917 | return @options; | |
918 | } | |
919 | ||
920 | sub get_dnat_target_port { | |
921 | my $hash = shift; | |
922 | my $key = shift; | |
923 | ||
924 | if ($$hash{$key}[14] eq "TGT_PORT") { | |
1c3044d7 MT |
925 | my $port = $$hash{$key}[15]; |
926 | my $external_port = $$hash{$key}[30]; | |
927 | ||
928 | if ($external_port && ($port ne $external_port)) { | |
929 | return $$hash{$key}[15]; | |
930 | } | |
8f4f4634 | 931 | } |
2a81ab0d | 932 | } |
6e87f0aa MT |
933 | |
934 | sub add_dnat_mangle_rules { | |
935 | my $nat_address = shift; | |
48f07c19 | 936 | my $interface = shift; |
6e87f0aa MT |
937 | my @options = @_; |
938 | ||
ce31144c | 939 | my $mark = 0x01000000; |
6e87f0aa | 940 | foreach my $zone ("GREEN", "BLUE", "ORANGE") { |
6e87f0aa MT |
941 | # Skip rule if not all required information exists. |
942 | next unless (exists $defaultNetworks{$zone . "_NETADDRESS"}); | |
943 | next unless (exists $defaultNetworks{$zone . "_NETMASK"}); | |
944 | ||
48f07c19 AM |
945 | next if ($interface && $interface ne $defaultNetworks{$zone . "_DEV"}); |
946 | ||
6e87f0aa MT |
947 | my @mangle_options = @options; |
948 | ||
949 | my $netaddress = $defaultNetworks{$zone . "_NETADDRESS"}; | |
950 | $netaddress .= "/" . $defaultNetworks{$zone . "_NETMASK"}; | |
951 | ||
952 | push(@mangle_options, ("-s", $netaddress, "-d", $nat_address)); | |
ce31144c | 953 | push(@mangle_options, ("-j", "MARK", "--set-xmark", "$mark/$NAT_MASK")); |
6e87f0aa MT |
954 | |
955 | run("$IPTABLES -t mangle -A $CHAIN_MANGLE_NAT_DESTINATION_FIX @mangle_options"); | |
ce31144c MT |
956 | |
957 | $mark <<= 1; | |
6e87f0aa MT |
958 | } |
959 | } | |
3bb4bb3f MT |
960 | |
961 | sub make_log_limit_options { | |
962 | my @options = ("-m", "limit"); | |
963 | ||
964 | # Maybe we should get this from the configuration. | |
965 | my $limit = 10; | |
966 | ||
a85a7a60 | 967 | # We limit log messages to $limit messages per second. |
968 | push(@options, ("--limit", "$limit/second")); | |
3bb4bb3f MT |
969 | |
970 | # And we allow bursts of 2x $limit. | |
971 | push(@options, ("--limit-burst", $limit * 2)); | |
972 | ||
973 | return @options; | |
974 | } | |
e9b5ba41 | 975 | |
da7a2208 MT |
976 | sub firewall_is_in_subnet { |
977 | my $subnet = shift; | |
5cf8c8c1 | 978 | |
e9b5ba41 MT |
979 | # ORANGE is missing here, because nothing may ever access |
980 | # the firewall from this network. | |
4e54e3c6 | 981 | my $address = &fwlib::get_internal_firewall_ip_address($subnet, 0); |
e9b5ba41 | 982 | |
da7a2208 MT |
983 | if ($address) { |
984 | return 1; | |
e9b5ba41 | 985 | } |
da7a2208 MT |
986 | |
987 | return 0; | |
e9b5ba41 | 988 | } |
6babb404 | 989 | |
960608c8 SS |
990 | sub firewall_chain_exists ($) { |
991 | my ($chain) = @_; | |
992 | ||
993 | my $ret = &General::system("iptables", "--wait", "-n", "-L", "$chain"); | |
994 | ||
995 | return $ret; | |
996 | } | |
997 | ||
2801213d | 998 | sub ipset_get_sets () { |
8b97a537 SS |
999 | my @sets; |
1000 | ||
2801213d SS |
1001 | # Get all currently used ipset lists and store them in an array. |
1002 | my @output = `$IPSET -n list`; | |
1003 | ||
1004 | # Loop through the temporary array. | |
1005 | foreach my $set (@output) { | |
1006 | # Remove any newlines. | |
1007 | chomp($set); | |
1008 | ||
1009 | # Add the set the array of used sets. | |
8b97a537 | 1010 | push(@sets, $set); |
2801213d SS |
1011 | } |
1012 | ||
1013 | # Display used sets in debug mode. | |
1014 | if($DEBUG) { | |
1015 | print "Used ipset sets:\n"; | |
8b97a537 | 1016 | print "@sets\n\n"; |
2801213d | 1017 | } |
8b97a537 SS |
1018 | |
1019 | # Return the array of sets. | |
1020 | return @sets; | |
2801213d SS |
1021 | } |
1022 | ||
6babb404 | 1023 | sub ipset_restore ($) { |
2801213d | 1024 | my ($set) = @_; |
6babb404 | 1025 | |
58418009 SS |
1026 | # Empty variable to store the db file, which should be |
1027 | # restored by ipset. | |
1028 | my $db_file; | |
bae9b5dc | 1029 | |
2801213d SS |
1030 | # Check if the set already has been loaded. |
1031 | if($ipset_loaded_sets{$set}) { | |
bae9b5dc SS |
1032 | # It already has been loaded - so there is nothing to do. |
1033 | return; | |
1034 | } | |
50e43059 | 1035 | |
58418009 SS |
1036 | # Check if the given set name is a country code. |
1037 | if($set ~~ @locations) { | |
870c223e SS |
1038 | # Libloc adds the IP type (v4 or v6) as part of the set and file name. |
1039 | my $loc_set = "$set" . "v4"; | |
58418009 | 1040 | |
870c223e SS |
1041 | # The bare filename equals the set name. |
1042 | my $filename = $loc_set; | |
1043 | ||
1044 | # Libloc uses "ipset" as file extension. | |
1045 | my $file_extension = "ipset"; | |
1046 | ||
1047 | # Generate full path and filename for the ipset db file. | |
1048 | my $db_file = "$Location::Functions::ipset_db_directory/$filename.$file_extension"; | |
1049 | ||
1050 | # Call function to restore/load the set. | |
1051 | &ipset_call_restore($db_file); | |
1052 | ||
1053 | # Check if the set is already loaded (has been used before). | |
1054 | if ($set ~~ @ipset_used_sets) { | |
1055 | # The sets contains the IP type (v4 or v6) as part of the name. | |
1056 | # The firewall rules matches against sets without that extension. So we safely | |
1057 | # can swap or rename the sets to use the new ones. | |
1058 | run("$IPSET swap $loc_set $set"); | |
1059 | } else { | |
1060 | # If the set is not loaded, we have to rename it to proper use it. | |
1061 | run("$IPSET rename $loc_set $set"); | |
1062 | } | |
a7bf1d96 SS |
1063 | |
1064 | # Check if the given set name is a blocklist. | |
1065 | } elsif ($set ~~ @blocklists) { | |
404b5137 SS |
1066 | # IPblocklist sets contains v4 as setname extension. |
1067 | my $set_name = "$set" . "v4"; | |
1068 | ||
a7bf1d96 SS |
1069 | # Get the database file for the given blocklist. |
1070 | my $db_file = &IPblocklist::get_ipset_db_file($set); | |
1071 | ||
1072 | # Call function to restore/load the set. | |
1073 | &ipset_call_restore($db_file); | |
404b5137 SS |
1074 | |
1075 | # Check if the set is already loaded (has been used before). | |
1076 | if ($set ~~ @ipset_used_sets) { | |
1077 | # Swap the sets. | |
1078 | run("$IPSET swap $set_name $set"); | |
1079 | } else { | |
1080 | # Rename the set to proper use it. | |
1081 | run("$IPSET rename $set_name $set"); | |
1082 | } | |
58418009 SS |
1083 | } |
1084 | ||
870c223e SS |
1085 | # Store the restored set to the hash to prevent from loading it again. |
1086 | $ipset_loaded_sets{$set} = "1"; | |
1087 | } | |
bae9b5dc | 1088 | |
870c223e SS |
1089 | sub ipset_call_restore ($) { |
1090 | my ($file) = @_; | |
1091 | ||
1092 | # Check if the requested file exists. | |
1093 | if (-f $file) { | |
1094 | # Run ipset and restore the given set. | |
1095 | run("$IPSET restore -f $file"); | |
2801213d SS |
1096 | } |
1097 | } | |
1098 | ||
1099 | sub ipset_cleanup () { | |
8b97a537 SS |
1100 | # Reload the array of used sets. |
1101 | @ipset_used_sets = &ipset_get_sets(); | |
1102 | ||
2801213d SS |
1103 | # Loop through the array of used sets. |
1104 | foreach my $set (@ipset_used_sets) { | |
1105 | # Check if this set is still in use. | |
1106 | # | |
1107 | # In this case an entry in the loaded sets hash exists. | |
1108 | unless($ipset_loaded_sets{$set}) { | |
1109 | # Entry does not exist, so this set is not longer | |
1110 | # used and can be destroyed. | |
1111 | run("$IPSET destroy $set"); | |
1112 | } | |
50e43059 | 1113 | } |
6babb404 | 1114 | } |