1
|
#!/usr/local/bin/php-cgi -f
|
2
|
<?php
|
3
|
/*
|
4
|
* rc.kill_states
|
5
|
*
|
6
|
* Copyright (c) 2004-2015 Electric Sheep Fencing, LLC. All rights reserved.
|
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
|
16
|
* the documentation and/or other materials provided with the
|
17
|
* distribution.
|
18
|
*
|
19
|
* 3. All advertising materials mentioning features or use of this software
|
20
|
* must display the following acknowledgment:
|
21
|
* "This product includes software developed by the pfSense Project
|
22
|
* for use in the pfSense® software distribution. (http://www.pfsense.org/).
|
23
|
*
|
24
|
* 4. The names "pfSense" and "pfSense Project" must not be used to
|
25
|
* endorse or promote products derived from this software without
|
26
|
* prior written permission. For written permission, please contact
|
27
|
* coreteam@pfsense.org.
|
28
|
*
|
29
|
* 5. Products derived from this software may not be called "pfSense"
|
30
|
* nor may "pfSense" appear in their names without prior written
|
31
|
* permission of the Electric Sheep Fencing, LLC.
|
32
|
*
|
33
|
* 6. Redistributions of any form whatsoever must retain the following
|
34
|
* acknowledgment:
|
35
|
*
|
36
|
* "This product includes software developed by the pfSense Project
|
37
|
* for use in the pfSense software distribution (http://www.pfsense.org/).
|
38
|
*
|
39
|
* THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
|
40
|
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
41
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
42
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
|
43
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
44
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
45
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
46
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
47
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
48
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
49
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
50
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
51
|
*/
|
52
|
|
53
|
/* parse the configuration and include all functions used below */
|
54
|
require_once("globals.inc");
|
55
|
require_once("config.inc");
|
56
|
require_once("interfaces.inc");
|
57
|
require_once("util.inc");
|
58
|
|
59
|
// Do not process while booting
|
60
|
if (platform_booting()) {
|
61
|
return;
|
62
|
}
|
63
|
|
64
|
/* Interface address to cleanup states */
|
65
|
$interface = str_replace("\n", "", $argv[1]);
|
66
|
|
67
|
/* IP address to cleanup states */
|
68
|
$local_ip = str_replace("\n", "", $argv[2]);
|
69
|
|
70
|
if (empty($interface) || !does_interface_exist($interface)) {
|
71
|
log_error("rc.kill_states: Invalid interface '{$interface}'");
|
72
|
return;
|
73
|
}
|
74
|
|
75
|
if (!empty($local_ip)) {
|
76
|
list($local_ip, $subnet_bits) = explode("/", $local_ip);
|
77
|
|
78
|
if (empty($subnet_bits)) {
|
79
|
$subnet_bits = "32";
|
80
|
}
|
81
|
|
82
|
if (!is_ipaddr($local_ip)) {
|
83
|
log_error("rc.kill_states: Invalid IP address '{$local_ip}'");
|
84
|
return;
|
85
|
}
|
86
|
}
|
87
|
|
88
|
if (isset($config['system']['gw_down_kill_states'])) {
|
89
|
if (!empty($local_ip)) {
|
90
|
log_error("rc.kill_states: Removing states for IP {$local_ip}/{$subnet_bits}");
|
91
|
$nat_states = exec_command("/sbin/pfctl -i {$interface} -ss | " .
|
92
|
"/usr/bin/egrep '\-> +{$local_ip}:[0-9]+ +\->'");
|
93
|
|
94
|
$cleared_states = array();
|
95
|
foreach (explode("\n", $nat_states) as $nat_state) {
|
96
|
if (preg_match_all('/([\d\.]+):[\d]+[\s->]+/i', $nat_state, $matches, PREG_SET_ORDER) != 3) {
|
97
|
continue;
|
98
|
}
|
99
|
|
100
|
$src = $matches[0][1];
|
101
|
$dst = $matches[2][1];
|
102
|
|
103
|
if (empty($src) || empty($dst) || in_array("{$src},{$dst}", $cleared_states)) {
|
104
|
continue;
|
105
|
}
|
106
|
|
107
|
$cleared_states[] = "{$src},{$dst}";
|
108
|
pfSense_kill_states($src, $dst);
|
109
|
}
|
110
|
|
111
|
pfSense_kill_states("0.0.0.0/0", "{$local_ip}/{$subnet_bits}");
|
112
|
pfSense_kill_states("{$local_ip}/{$subnet_bits}");
|
113
|
pfSense_kill_srcstates("{$local_ip}/{$subnet_bits}");
|
114
|
}
|
115
|
log_error("rc.kill_states: Removing states for interface {$interface}");
|
116
|
mwexec("/sbin/pfctl -i {$interface} -Fs", true);
|
117
|
}
|