Project

General

Profile

Download (4.3 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-2019 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
if (!strstr($argument, "@")) {
37
	log_error("CARP master event triggered from wrong source {$argument}");
38
	exit;
39
}
40

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

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

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

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

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

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

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

    
128
?>
(24-24/80)