Project

General

Profile

Download (2.76 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-2020 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
		$filter = array(
62
			array('interface' => $interface),
63
			array('filter' => $local_ip)
64
		);
65
		$states = pfSense_get_pf_states($filter);
66
		$cleared_states = array();
67
		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
				continue;
78
			}
79

    
80
			$cleared_states[] = "{$osrc},{$dst}";
81
			pfSense_kill_states($osrc, $dst);
82
		}
83
		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
	}
87
	log_error("rc.kill_states: Removing states for interface {$interface}");
88
	mwexec("/sbin/pfctl -i {$interface} -Fs", true);
89
}
(52-52/83)