Project

General

Profile

Download (4.3 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
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 "MASTER" 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
/* Start OpenVPN clients running on this VIP, since they should be in the stopped state while the VIP is CARP Backup. */
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("Starting OpenVPN client instance on {$friendly_descr} because of transition to CARP master.");
78
				openvpn_restart('client', $settings);
79
			}
80
		}
81
	}
82
}
83
if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-server'])) {
84
	foreach ($config['openvpn']['openvpn-server'] as $settings) {
85
		if (substr($settings['interface'], 0, 4) == '_vip') {
86
			$openvpn_vip = $settings['interface'];
87
		} else if (is_array($a_groups[$settings['interface']])) {
88
			// interface is a gateway group, check CARP VIP
89
			if (substr($a_groups[$settings['interface']][0]['vip'], 0, 4) == '_vip') {
90
				$openvpn_vip = $a_groups[$settings['interface']][0]['vip'];
91
			}
92
		} else {
93
			// this OpenVPN instance not on a CARP IP
94
			continue;
95
		}
96
		foreach ($vips as $vip) {
97
			if ($openvpn_vip == "_vip{$vip['uniqid']}") {
98
				log_error("Starting OpenVPN server instance on {$friendly_descr} because of transition to CARP master.");
99
				openvpn_restart('server', $settings);
100
			}
101
		}
102
	}
103
}
104

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

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

    
127
?>
(25-25/80)