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-2022 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("ipsec.inc");
|
30
|
require_once("interfaces.inc");
|
31
|
|
32
|
if (isset($_GET['interface'])) {
|
33
|
$argument = $_GET['interface'];
|
34
|
} else {
|
35
|
$argument = str_replace("\n", "", $argv[1]);
|
36
|
}
|
37
|
$argument = ltrim($argument, '$');
|
38
|
if (!strstr($argument, "@")) {
|
39
|
log_error("CARP master event triggered from wrong source {$argument}");
|
40
|
exit;
|
41
|
}
|
42
|
|
43
|
list($vhid, $iface) = explode("@", $argument);
|
44
|
|
45
|
$friendly = convert_real_interface_to_friendly_interface_name($iface);
|
46
|
$friendly_descr = convert_friendly_interface_to_friendly_descr($friendly);
|
47
|
$vips = link_interface_to_vips($friendly, '', $vhid);
|
48
|
if (!is_array($vips)) {
|
49
|
log_error("CARP master event triggered from wrong source {$argument} - no associated VIPs");
|
50
|
exit;
|
51
|
}
|
52
|
foreach ($vips as $vip) {
|
53
|
$notificationmsg = sprintf('HA cluster member "(%1$s@%2$s): (%3$s)" has resumed CARP state "BACKUP" for vhid %4$s',
|
54
|
$vip['subnet'], $iface, $friendly_descr, $vhid);
|
55
|
|
56
|
notify_all_remote($notificationmsg);
|
57
|
log_error($notificationmsg);
|
58
|
}
|
59
|
restart_ppp_interfaces_using_interfaces($vips);
|
60
|
|
61
|
/* Stop OpenVPN clients running on this VIP, since multiple active OpenVPN clients on a CARP cluster can be problematic. */
|
62
|
global $config;
|
63
|
$a_groups = return_gateway_groups_array(true);
|
64
|
if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-client'])) {
|
65
|
foreach ($config['openvpn']['openvpn-client'] as $settings) {
|
66
|
if (substr($settings['interface'], 0, 4) == '_vip') {
|
67
|
$openvpn_vip = $settings['interface'];
|
68
|
} else if (is_array($a_groups[$settings['interface']])) {
|
69
|
// interface is a gateway group, check CARP VIP
|
70
|
if (substr($a_groups[$settings['interface']][0]['vip'], 0, 4) == '_vip') {
|
71
|
$openvpn_vip = $a_groups[$settings['interface']][0]['vip'];
|
72
|
}
|
73
|
} else {
|
74
|
// this OpenVPN instance not on a CARP IP
|
75
|
continue;
|
76
|
}
|
77
|
foreach ($vips as $vip) {
|
78
|
if ($openvpn_vip == "_vip{$vip['uniqid']}") {
|
79
|
log_error("Stopping OpenVPN client instance on {$friendly_descr} because of transition to CARP backup.");
|
80
|
openvpn_restart('client', $settings);
|
81
|
}
|
82
|
}
|
83
|
}
|
84
|
}
|
85
|
|
86
|
if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-server'])) {
|
87
|
foreach ($config['openvpn']['openvpn-server'] as $settings) {
|
88
|
if (substr($settings['interface'], 0, 4) == '_vip') {
|
89
|
$openvpn_vip = $settings['interface'];
|
90
|
} else if (is_array($a_groups[$settings['interface']])) {
|
91
|
// interface is a gateway group, check CARP VIP
|
92
|
if (substr($a_groups[$settings['interface']][0]['vip'], 0, 4) == '_vip') {
|
93
|
$openvpn_vip = $a_groups[$settings['interface']][0]['vip'];
|
94
|
}
|
95
|
} else {
|
96
|
// this OpenVPN instance not on a CARP IP
|
97
|
continue;
|
98
|
}
|
99
|
foreach ($vips as $vip) {
|
100
|
if ($openvpn_vip == "_vip{$vip['uniqid']}") {
|
101
|
log_error("Stopping OpenVPN server instance on {$friendly_descr} because of transition to CARP backup.");
|
102
|
openvpn_restart('server', $settings);
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
if (is_array($config['ipsec']) && is_array($config['ipsec']['phase1'])) {
|
108
|
foreach ($config['ipsec']['phase1'] as $ph1ent) {
|
109
|
if ((substr($ph1ent['interface'], 0, 4) == '_vip') && (in_array($ph1ent['interface'], $vips))) {
|
110
|
log_error("Reconfiguring IPsec because of transition to CARP backup.");
|
111
|
ipsec_configure();
|
112
|
break;
|
113
|
}
|
114
|
}
|
115
|
}
|
116
|
|
117
|
/* Reconfigure radvd when necessary */
|
118
|
if (isset($config['dhcpdv6']) && is_array($config['dhcpdv6'])) {
|
119
|
$rafound = false;
|
120
|
foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
|
121
|
foreach ($vips as $vip) {
|
122
|
if ($dhcpv6ifconf['rainterface'] == "_vip{$vip['uniqid']}") {
|
123
|
log_error("Stopping radvd instance on {$friendly_descr} because of transition to CARP backup.");
|
124
|
$rafound = true;
|
125
|
}
|
126
|
}
|
127
|
}
|
128
|
if ($rafound) {
|
129
|
services_radvd_configure();
|
130
|
}
|
131
|
}
|
132
|
|
133
|
/* Reconfigure DHCP Relay when necessary */
|
134
|
if (isset($config['dhcrelay']) && is_array($config['dhcrelay']) && isset($config['dhcrelay']['enable']) &&
|
135
|
isset($config['dhcrelay']['carpstatusvip']) && ($config['dhcrelay']['carpstatusvip'] == "_vip{$vip['uniqid']}")) {
|
136
|
log_error("Stopping DHCP Relay service because of transition to CARP backup.");
|
137
|
services_dhcrelay_configure();
|
138
|
}
|
139
|
|
140
|
/* Reconfigure DHCPv6 Relay when necessary */
|
141
|
if (isset($config['dhcrelay6']) && is_array($config['dhcrelay6']) && isset($config['dhcrelay6']['enable']) &&
|
142
|
isset($config['dhcrelay6']['carpstatusvip']) && ($config['dhcrelay6']['carpstatusvip'] == "_vip{$vip['uniqid']}")) {
|
143
|
log_error("Stopping DHCPv6 Relay service because of transition to CARP backup.");
|
144
|
services_dhcrelay6_configure();
|
145
|
}
|
146
|
|
147
|
$pluginparams = array();
|
148
|
$pluginparams['type'] = 'carp';
|
149
|
$pluginparams['event'] = 'rc.carpbackup';
|
150
|
$pluginparams['interface'] = $argument;
|
151
|
pkg_call_plugins('plugin_carp', $pluginparams);
|
152
|
|
153
|
?>
|