Project

General

Profile

Download (4.33 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * rc.carpmaster
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
$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 "MASTER" 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
/* Start OpenVPN clients running on this VIP, since they should be in the stopped state while the VIP is CARP Backup. */
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("Starting OpenVPN client instance on {$friendly_descr} because of transition to CARP master.");
79
				openvpn_restart('client', $settings);
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("Starting OpenVPN server instance on {$friendly_descr} because of transition to CARP master.");
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("Starting 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.carpmaster';
125
$pluginparams['interface'] = $argument;
126
pkg_call_plugins('plugin_carp', $pluginparams);
127

    
128
?>
(25-25/81)