1
|
#!/usr/local/bin/php-cgi -f
|
2
|
<?php
|
3
|
/*
|
4
|
rc.kill_states
|
5
|
Copyright (C) 2013 Renato Botelho (garga@pfsense.org)
|
6
|
part of pfSense (https://www.pfsense.org)
|
7
|
|
8
|
Redistribution and use in source and binary forms, with or without
|
9
|
modification, are permitted provided that the following conditions are met:
|
10
|
|
11
|
1. Redistributions of source code must retain the above copyright notice,
|
12
|
this list of conditions and the following disclaimer.
|
13
|
|
14
|
2. Redistributions in binary form must reproduce the above copyright
|
15
|
notice, this list of conditions and the following disclaimer in the
|
16
|
documentation and/or other materials provided with the distribution.
|
17
|
|
18
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
19
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
20
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
21
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
22
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
23
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
25
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
26
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27
|
POSSIBILITY OF SUCH DAMAGE.
|
28
|
*/
|
29
|
|
30
|
/* parse the configuration and include all functions used below */
|
31
|
require_once("globals.inc");
|
32
|
require_once("config.inc");
|
33
|
require_once("interfaces.inc");
|
34
|
require_once("util.inc");
|
35
|
|
36
|
// Do not process while booting
|
37
|
if (platform_booting()) {
|
38
|
return;
|
39
|
}
|
40
|
|
41
|
/* Interface address to cleanup states */
|
42
|
$interface = str_replace("\n", "", $argv[1]);
|
43
|
|
44
|
/* IP address to cleanup states */
|
45
|
$local_ip = str_replace("\n", "", $argv[2]);
|
46
|
|
47
|
if (empty($interface) || !does_interface_exist($interface)) {
|
48
|
log_error("rc.kill_states: Invalid interface '{$interface}'");
|
49
|
return;
|
50
|
}
|
51
|
|
52
|
if (!empty($local_ip)) {
|
53
|
list($local_ip, $subnet_bits) = explode("/", $local_ip);
|
54
|
|
55
|
if (empty($subnet_bits)) {
|
56
|
$subnet_bits = "32";
|
57
|
}
|
58
|
|
59
|
if (!is_ipaddr($local_ip)) {
|
60
|
log_error("rc.kill_states: Invalid IP address '{$local_ip}'");
|
61
|
return;
|
62
|
}
|
63
|
}
|
64
|
|
65
|
if (!isset($config['system']['kill_states'])) {
|
66
|
if (!empty($local_ip)) {
|
67
|
log_error("rc.kill_states: Removing states for IP {$local_ip}/{$subnet_bits}");
|
68
|
$nat_states = exec_command("/sbin/pfctl -i {$interface} -ss | " .
|
69
|
"/usr/bin/egrep '\-> +{$local_ip}:[0-9]+ +\->'");
|
70
|
|
71
|
$cleared_states = array();
|
72
|
foreach (explode("\n", $nat_states) as $nat_state) {
|
73
|
if (preg_match_all('/([\d\.]+):[\d]+[\s->]+/i', $nat_state, $matches, PREG_SET_ORDER) != 3) {
|
74
|
continue;
|
75
|
}
|
76
|
|
77
|
$src = $matches[0][1];
|
78
|
$dst = $matches[2][1];
|
79
|
|
80
|
if (empty($src) || empty($dst) || in_array("{$src},{$dst}", $cleared_states)) {
|
81
|
continue;
|
82
|
}
|
83
|
|
84
|
$cleared_states[] = "{$src},{$dst}";
|
85
|
pfSense_kill_states($src, $dst);
|
86
|
}
|
87
|
|
88
|
pfSense_kill_states("0.0.0.0/0", "{$local_ip}/{$subnet_bits}");
|
89
|
pfSense_kill_states("{$local_ip}/{$subnet_bits}");
|
90
|
pfSense_kill_srcstates("{$local_ip}/{$subnet_bits}");
|
91
|
}
|
92
|
log_error("rc.kill_states: Removing states for interface {$interface}");
|
93
|
mwexec("/sbin/pfctl -i {$interface} -Fs", true);
|
94
|
}
|