Project

General

Profile

Download (2.53 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * rc.kill_states
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * 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
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * 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
 */
22

    
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
if (platform_booting()) {
31
	return;
32
}
33

    
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
	return;
43
}
44

    
45
if (!empty($local_ip)) {
46
	list($local_ip, $subnet_bits) = explode("/", $local_ip);
47

    
48
	if (empty($subnet_bits)) {
49
		$subnet_bits = "32";
50
	}
51

    
52
	if (!is_ipaddr($local_ip)) {
53
		log_error("rc.kill_states: Invalid IP address '{$local_ip}'");
54
		return;
55
	}
56
}
57

    
58
if (isset($config['system']['gw_down_kill_states'])) {
59
	if (!empty($local_ip)) {
60
		log_error("rc.kill_states: Removing states for IP {$local_ip}/{$subnet_bits}");
61
		$nat_states = exec_command("/sbin/pfctl -i {$interface} -ss | " .
62
			"/usr/bin/egrep '\-> +{$local_ip}:[0-9]+ +\->'");
63

    
64
		$cleared_states = array();
65
		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
				continue;
68
			}
69

    
70
			$src = $matches[0][1];
71
			$dst = $matches[2][1];
72

    
73
			if (empty($src) || empty($dst) || in_array("{$src},{$dst}", $cleared_states)) {
74
				continue;
75
			}
76

    
77
			$cleared_states[] = "{$src},{$dst}";
78
			pfSense_kill_states($src, $dst);
79
		}
80

    
81
		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
	}
85
	log_error("rc.kill_states: Removing states for interface {$interface}");
86
	mwexec("/sbin/pfctl -i {$interface} -Fs", true);
87
}
(50-50/78)