1 |
cb7d18d5
|
Renato Botelho
|
#!/usr/local/bin/php-cgi -f
|
2 |
46e27ea7
|
Renato Botelho
|
<?php
|
3 |
|
|
/*
|
4 |
ea27f316
|
Renato Botelho
|
* rc.kill_states
|
5 |
|
|
*
|
6 |
ac24dc24
|
Renato Botelho
|
* part of pfSense (https://www.pfsense.org)
|
7 |
880ed461
|
jim-p
|
* Copyright (c) 2004-2020 Rubicon Communications, LLC (Netgate)
|
8 |
ac24dc24
|
Renato Botelho
|
* All rights reserved.
|
9 |
ea27f316
|
Renato Botelho
|
*
|
10 |
b12ea3fb
|
Renato Botelho
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
11 |
|
|
* you may not use this file except in compliance with the License.
|
12 |
|
|
* You may obtain a copy of the License at
|
13 |
ea27f316
|
Renato Botelho
|
*
|
14 |
b12ea3fb
|
Renato Botelho
|
* http://www.apache.org/licenses/LICENSE-2.0
|
15 |
ea27f316
|
Renato Botelho
|
*
|
16 |
b12ea3fb
|
Renato Botelho
|
* Unless required by applicable law or agreed to in writing, software
|
17 |
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
18 |
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
19 |
|
|
* See the License for the specific language governing permissions and
|
20 |
|
|
* limitations under the License.
|
21 |
ea27f316
|
Renato Botelho
|
*/
|
22 |
46e27ea7
|
Renato Botelho
|
|
23 |
|
|
/* parse the configuration and include all functions used below */
|
24 |
|
|
require_once("globals.inc");
|
25 |
|
|
require_once("config.inc");
|
26 |
|
|
require_once("interfaces.inc");
|
27 |
|
|
require_once("util.inc");
|
28 |
|
|
|
29 |
|
|
// Do not process while booting
|
30 |
086cf944
|
Phil Davis
|
if (platform_booting()) {
|
31 |
285ef132
|
Ermal LUÇI
|
return;
|
32 |
086cf944
|
Phil Davis
|
}
|
33 |
46e27ea7
|
Renato Botelho
|
|
34 |
|
|
/* Interface address to cleanup states */
|
35 |
|
|
$interface = str_replace("\n", "", $argv[1]);
|
36 |
|
|
|
37 |
|
|
/* IP address to cleanup states */
|
38 |
|
|
$local_ip = str_replace("\n", "", $argv[2]);
|
39 |
|
|
|
40 |
|
|
if (empty($interface) || !does_interface_exist($interface)) {
|
41 |
|
|
log_error("rc.kill_states: Invalid interface '{$interface}'");
|
42 |
8ad1ee63
|
Ermal LUÇI
|
return;
|
43 |
46e27ea7
|
Renato Botelho
|
}
|
44 |
|
|
|
45 |
|
|
if (!empty($local_ip)) {
|
46 |
|
|
list($local_ip, $subnet_bits) = explode("/", $local_ip);
|
47 |
|
|
|
48 |
e173dd74
|
Phil Davis
|
if (empty($subnet_bits)) {
|
49 |
46e27ea7
|
Renato Botelho
|
$subnet_bits = "32";
|
50 |
e173dd74
|
Phil Davis
|
}
|
51 |
46e27ea7
|
Renato Botelho
|
|
52 |
|
|
if (!is_ipaddr($local_ip)) {
|
53 |
|
|
log_error("rc.kill_states: Invalid IP address '{$local_ip}'");
|
54 |
8ad1ee63
|
Ermal LUÇI
|
return;
|
55 |
46e27ea7
|
Renato Botelho
|
}
|
56 |
|
|
}
|
57 |
|
|
|
58 |
3756fd86
|
Chris Buechler
|
if (isset($config['system']['gw_down_kill_states'])) {
|
59 |
46e27ea7
|
Renato Botelho
|
if (!empty($local_ip)) {
|
60 |
|
|
log_error("rc.kill_states: Removing states for IP {$local_ip}/{$subnet_bits}");
|
61 |
0edf0420
|
jim-p
|
$filter = array(
|
62 |
|
|
array('interface' => $interface),
|
63 |
|
|
array('filter' => $local_ip)
|
64 |
|
|
);
|
65 |
|
|
$states = pfSense_get_pf_states($filter);
|
66 |
d13b7363
|
Renato Botelho
|
$cleared_states = array();
|
67 |
0edf0420
|
jim-p
|
foreach ($states as $state) {
|
68 |
|
|
/* Locate and kill states for sources that NAT out through $local_ip */
|
69 |
|
|
list($src, $srcport) = explode(":", $state['src']);
|
70 |
|
|
list($dst, $dstport) = explode(":", $state['dst']);
|
71 |
|
|
list($osrc, $osrcport) = explode(":", $state['src-orig']);
|
72 |
|
|
/* If the local IP address isn't the source, or if this isn't
|
73 |
|
|
* a NAT state, or if we've already cleared this, skip it. */
|
74 |
|
|
if (($src != $local_ip) ||
|
75 |
|
|
empty($state['src-orig']) ||
|
76 |
|
|
in_array("{$osrc},{$dst}", $cleared_states)) {
|
77 |
d13b7363
|
Renato Botelho
|
continue;
|
78 |
e173dd74
|
Phil Davis
|
}
|
79 |
d13b7363
|
Renato Botelho
|
|
80 |
0edf0420
|
jim-p
|
$cleared_states[] = "{$osrc},{$dst}";
|
81 |
|
|
pfSense_kill_states($osrc, $dst);
|
82 |
d13b7363
|
Renato Botelho
|
}
|
83 |
0174c480
|
Ermal LUÇI
|
pfSense_kill_states("0.0.0.0/0", "{$local_ip}/{$subnet_bits}");
|
84 |
|
|
pfSense_kill_states("{$local_ip}/{$subnet_bits}");
|
85 |
|
|
pfSense_kill_srcstates("{$local_ip}/{$subnet_bits}");
|
86 |
46e27ea7
|
Renato Botelho
|
}
|
87 |
|
|
log_error("rc.kill_states: Removing states for interface {$interface}");
|
88 |
|
|
mwexec("/sbin/pfctl -i {$interface} -Fs", true);
|
89 |
|
|
}
|