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