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 |
81299b5c
|
Renato Botelho
|
* Copyright (c) 2004-2016 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 |
d13b7363
|
Renato Botelho
|
$nat_states = exec_command("/sbin/pfctl -i {$interface} -ss | " .
|
62 |
|
|
"/usr/bin/egrep '\-> +{$local_ip}:[0-9]+ +\->'");
|
63 |
|
|
|
64 |
|
|
$cleared_states = array();
|
65 |
e173dd74
|
Phil Davis
|
foreach (explode("\n", $nat_states) as $nat_state) {
|
66 |
|
|
if (preg_match_all('/([\d\.]+):[\d]+[\s->]+/i', $nat_state, $matches, PREG_SET_ORDER) != 3) {
|
67 |
d13b7363
|
Renato Botelho
|
continue;
|
68 |
e173dd74
|
Phil Davis
|
}
|
69 |
d13b7363
|
Renato Botelho
|
|
70 |
|
|
$src = $matches[0][1];
|
71 |
|
|
$dst = $matches[2][1];
|
72 |
|
|
|
73 |
e173dd74
|
Phil Davis
|
if (empty($src) || empty($dst) || in_array("{$src},{$dst}", $cleared_states)) {
|
74 |
d13b7363
|
Renato Botelho
|
continue;
|
75 |
e173dd74
|
Phil Davis
|
}
|
76 |
d13b7363
|
Renato Botelho
|
|
77 |
|
|
$cleared_states[] = "{$src},{$dst}";
|
78 |
0174c480
|
Ermal LUÇI
|
pfSense_kill_states($src, $dst);
|
79 |
d13b7363
|
Renato Botelho
|
}
|
80 |
|
|
|
81 |
0174c480
|
Ermal LUÇI
|
pfSense_kill_states("0.0.0.0/0", "{$local_ip}/{$subnet_bits}");
|
82 |
|
|
pfSense_kill_states("{$local_ip}/{$subnet_bits}");
|
83 |
|
|
pfSense_kill_srcstates("{$local_ip}/{$subnet_bits}");
|
84 |
46e27ea7
|
Renato Botelho
|
}
|
85 |
|
|
log_error("rc.kill_states: Removing states for interface {$interface}");
|
86 |
|
|
mwexec("/sbin/pfctl -i {$interface} -Fs", true);
|
87 |
|
|
}
|