Project

General

Profile

Download (2.9 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * rc.ipsec
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2021-2023 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("config.inc");
25
require_once("gwlb.inc");
26
require_once("ipsec.inc");
27
require_once("util.inc");
28
require_once("globals.inc");
29

    
30
/* make sure to wait until the boot scripts have finished */
31
if (platform_booting()) {
32
	return;
33
}
34

    
35
$argument = (isset($_GET['interface'])) ? $_GET['interface'] : $argv[1];
36
$argument = trim($argument, "'\" \n");
37

    
38
$ipsec_reload = false;
39
if (empty($argument)) {
40
	$ipsec_reload = true;
41
} else {
42
	$arg_array = explode(",", $argument);
43
	foreach ($arg_array as $arg_element) {
44
		$gwgroups = array();
45
		if ($arg_element == "all") {
46
			$ipsec_reload = true;
47
			break;
48
		} else {
49
			// e.g. $arg_element = "WANGW", $interface = "wan"
50
			$interface = lookup_gateway_interface_by_name($arg_element);
51
			if (empty($interface)) {
52
				$interface = $arg_element;
53
			} else {
54
				// e.g. $arg_element = "WANGW", $gwgroups = array of gateway groups that use "wan"
55
				$gwgroups = gateway_is_gwgroup_member($arg_element);
56
			}
57
			init_config_arr(array('ipsec', 'phase1'));
58
			foreach ($config['ipsec']['phase1'] as $phase1) {
59
				if (isset($phase1['disabled']) || isset($phase1['mobile']) ||
60
				    ($phase1['startaction'] == 'none')) {
61
					continue;
62
				}
63
				if (substr($phase1['interface'], 0, 4) == "_vip") {
64
					$phase1iface = get_configured_vip_interface($phase1['interface']);
65
					if (substr($phase1iface, 0, 4) == "_vip") {
66
						// vips are nested if its a ipalias with a carp parent
67
						$phase1iface = get_configured_vip_interface($phase1iface);
68
					}
69
				} else {
70
					$phase1iface = $phase1['interface'];
71
				}
72
				if (($phase1iface == $interface) ||
73
				    (!empty($gwgroups) && in_array($phase1iface, $gwgroups))) {
74
					$ipsec_reload = true;
75
					break 2;
76
				}
77
			}
78
		}
79
	}
80
}
81

    
82
if ($ipsec_reload) {
83
	$ipseclck_pending = try_lock('ipsecdns_pending', 0);
84
	if (!$ipseclck_pending) {
85
		/* if a ipsec_configure() is still pending no need to stack up another one */
86
		return;
87
	}
88
	$ipseclck = lock('ipsecdns', LOCK_EX);
89
	sleep(12);
90
	unlock($ipseclck_pending);
91
	sleep(3);
92
	log_error("IPSEC: One or more IPsec tunnel gateways have changed. Refreshing.");
93
	ipsec_configure();
94
	unlock($ipseclck);
95
	ipsec_reload_package_hook();
96
}
(52-52/85)