Project

General

Profile

Download (4.33 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * rc.carpbackup
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7
 * Copyright (c) 2004-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
require_once("functions.inc");
26
require_once("config.inc");
27
require_once("notices.inc");
28
require_once("openvpn.inc");
29
require_once("interfaces.inc");
30

    
31
if (isset($_GET['interface'])) {
32
	$argument = $_GET['interface'];
33
} else {
34
	$argument = str_replace("\n", "", $argv[1]);
35
}
36
$argument = ltrim($argument, '$');
37
if (!strstr($argument, "@")) {
38
	log_error("CARP master event triggered from wrong source {$argument}");
39
	exit;
40
}
41

    
42
list($vhid, $iface) = explode("@", $argument);
43

    
44
$friendly = convert_real_interface_to_friendly_interface_name($iface);
45
$friendly_descr = convert_friendly_interface_to_friendly_descr($friendly);
46
$vips = link_interface_to_vips($friendly, '', $vhid);
47
if (!is_array($vips)) {
48
	log_error("CARP master event triggered from wrong source {$argument} - no associated VIPs");
49
	exit;
50
}
51
foreach ($vips as $vip) {
52
	$notificationmsg = sprintf('HA cluster member "(%1$s@%2$s): (%3$s)" has resumed CARP state "BACKUP" for vhid %4$s',
53
		$vip['subnet'], $iface, $friendly_descr, $vhid);
54

    
55
	notify_via_smtp($notificationmsg);
56
	log_error($notificationmsg);
57
}
58
restart_ppp_interfaces_using_interfaces($vips);
59

    
60
/* Stop OpenVPN clients running on this VIP, since multiple active OpenVPN clients on a CARP cluster can be problematic. */
61
global $config;
62
$a_groups = return_gateway_groups_array(true);
63
if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-client'])) {
64
	foreach ($config['openvpn']['openvpn-client'] as $settings) {
65
		if (substr($settings['interface'], 0, 4) == '_vip') {
66
			$openvpn_vip = $settings['interface'];
67
		} else if (is_array($a_groups[$settings['interface']])) {
68
			// interface is a gateway group, check CARP VIP
69
			if (substr($a_groups[$settings['interface']][0]['vip'], 0, 4) == '_vip') {
70
				$openvpn_vip = $a_groups[$settings['interface']][0]['vip'];
71
			}
72
		} else {
73
			// this OpenVPN instance not on a CARP IP
74
			continue;
75
		}
76
		foreach ($vips as $vip) {
77
			if ($openvpn_vip == "_vip{$vip['uniqid']}") {
78
				log_error("Stopping OpenVPN client instance on {$friendly_descr} because of transition to CARP backup.");
79
				openvpn_restart('client', $settings);
80
			}
81
		}
82
	}
83
}
84

    
85
if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-server'])) {
86
	foreach ($config['openvpn']['openvpn-server'] as $settings) {
87
		if (substr($settings['interface'], 0, 4) == '_vip') {
88
			$openvpn_vip = $settings['interface'];
89
		} else if (is_array($a_groups[$settings['interface']])) {
90
			// interface is a gateway group, check CARP VIP
91
			if (substr($a_groups[$settings['interface']][0]['vip'], 0, 4) == '_vip') {
92
				$openvpn_vip = $a_groups[$settings['interface']][0]['vip'];
93
			}
94
		} else {
95
			// this OpenVPN instance not on a CARP IP
96
			continue;
97
		}
98
		foreach ($vips as $vip) {
99
			if ($openvpn_vip == "_vip{$vip['uniqid']}") {
100
				log_error("Stopping OpenVPN server instance on {$friendly_descr} because of transition to CARP backup.");
101
				openvpn_restart('server', $settings);
102
			}
103
		}
104
	}
105
}
106

    
107
/* Reconfigure radvd when necessary */
108
if (isset($config['dhcpdv6']) && is_array($config['dhcpdv6'])) {
109
	$rafound = false;
110
	foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
111
		foreach ($vips as $vip) {
112
			if ($dhcpv6ifconf['rainterface'] == "_vip{$vip['uniqid']}") {
113
				log_error("Stopping radvd instance on {$friendly_descr} because of transition to CARP master.");
114
				$rafound = true;
115
			}
116
		}
117
	}
118
	if ($rafound) {
119
		services_radvd_configure();
120
	}
121
}
122

    
123
$pluginparams = array();
124
$pluginparams['type'] = 'carp';
125
$pluginparams['event'] = 'rc.carpbackup';
126
$pluginparams['interface'] = $argument;
127
pkg_call_plugins('plugin_carp', $pluginparams);
128

    
129
?>
(24-24/82)