Project

General

Profile

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

    
29
/* parse the configuration and include all functions used below */
30
require_once("globals.inc");
31
require_once("config.inc");
32
require_once("functions.inc");
33
require_once("filter.inc");
34
require_once("shaper.inc");
35
require_once("ipsec.inc");
36
require_once("vpn.inc");
37
require_once("openvpn.inc");
38
require_once("Net/IPv6.php");
39
require_once("services.inc");
40
require_once("rrd.inc");
41

    
42
function restart_packages() {
43
	global $oldipv6, $curwanipv6, $g;
44

    
45
	/* restart packages */
46
	log_error("{$g['product_label']} package system has detected an IP change or dynamic WAN reconnection - $oldipv6 -> $curwanipv6 - Restarting packages.");
47
	send_event("service reload packages");
48
}
49

    
50
/* dhcp6c reasons: INFOREQ, REQUEST, RENEW, REBIND, RELEASE, EXIT */
51
if (isset($_GET['reason'])) {
52
	$reason = $_GET['reason'];
53
} else {
54
	$reason = trim($argv[4]);
55
}
56

    
57
/* Interface IP address has changed */
58
if (isset($_GET['interface'])) {
59
	$argument = $_GET['interface'];
60
} else {
61
	$argument = trim($argv[1], " \n\t");
62
}
63

    
64
if (!empty($reason) && ($reason != 'RENEW')) {
65
	log_error("rc.newwanipv6: Info: starting on {$argument} due to {$reason}.");
66
} else {
67
	log_error("rc.newwanipv6: Info: starting on {$argument}.");
68
}
69

    
70
if (empty($argument)) {
71
	$interface = "wan";
72
	$interface_real = get_real_interface($interface, "inet6");
73
	$curwanipv6 = get_interface_ipv6($interface, true);
74
} else {
75
	$interface_real = $argument;
76
	$interface = convert_real_interface_to_friendly_interface_name($interface_real);
77
	if (empty($interface)) {
78
		log_error("Interface is unassigned, nothing to do.");
79
		return;
80
	}
81
	$curwanipv6 = get_interface_ipv6($interface, true);
82
}
83

    
84
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
85

    
86
if (empty($interface)) {
87
	log_error("rc.newwanipv6 called with empty interface");
88
	filter_configure();
89
	return;
90
}
91

    
92
/*
93
 * NOTE: Take care of openvpn and similar if you generate the event to reconfigure an interface.
94
 *	i.e. OpenVPN might be in tap mode and not have an ip.
95
 */
96
if ((empty($curwanipv6) || !is_ipaddrv6($curwanipv6)) && substr($interface_real, 0, 4) != "ovpn") {
97
	log_error("rc.newwanipv6: No IPv6 address found for interface {$interface_descr} [{$interface}].");
98
	return;
99
}
100

    
101
if (isset($_GET['dmips'])) {
102
	$new_domain_name_servers = $_GET['dmips'];
103
} else {
104
	$new_domain_name_servers = getenv("new_domain_name_servers");
105
}
106

    
107
if (!empty($new_domain_name_servers)) {
108
	$new_domain_name_servers_received = true;
109
	$name_servers = explode(" ", $new_domain_name_servers);
110
	$valid_ns = array();
111
	foreach ($name_servers as $ns) {
112
		if (is_ipaddrv6(trim($ns))) {
113
			$valid_ns[] = trim($ns);
114
		}
115
	}
116

    
117
	$saved_name_servers = file_get_contents("{$g['varetc_path']}/nameserver_v6{$interface}");
118
	if ($saved_name_servers !== false) {
119
		$saved_name_servers = array_filter(explode(PHP_EOL, $saved_name_servers));
120
		if ($saved_name_servers === $valid_ns) {
121
			$new_domain_name_servers_received = false;
122
		}
123
	}
124

    
125
	if ($new_domain_name_servers_received && count($valid_ns) > 0) {
126
		file_put_contents("{$g['varetc_path']}/nameserver_v6{$interface}", implode("\n", $valid_ns));
127
	}
128
}
129
if (isset($_GET['dmnames'])) {
130
	$new_domain_name = $_GET['dmnames'];
131
} else {
132
	$new_domain_name = getenv("new_domain_name");
133
}
134

    
135
if (!empty($new_domain_name)) {
136
	$new_searchdomains_received = true;
137
	$saved_searchdomains = file_get_contents("{$g['varetc_path']}/searchdomain_v6{$interface}");
138
	if (($saved_searchdomains !== false) && ($saved_searchdomains === $new_domain_name)) {
139
		$new_searchdomains_received = false;
140
	}
141

    
142
	if ($new_searchdomains_received) {
143
		file_put_contents("{$g['varetc_path']}/searchdomain_v6{$interface}", $new_domain_name);
144
	}
145
}
146

    
147
/* write current WAN IPv6 to file */
148
if (is_ipaddrv6($curwanipv6)) {
149
	@file_put_contents(g_get('vardb_path') . "/{$interface_real}_ipv6", $curwanipv6);
150
}
151

    
152
$oldipv6 = '';
153
if (file_exists(g_get('vardb_path') . "/{$interface_real}_cacheipv6")) {
154
	$oldipv6 = file_get_contents(g_get('vardb_path') . "/{$interface_real}_cacheipv6");
155
}
156

    
157
/**
158
 * @var bool Used for only taking action when the address is replaced.
159
 */
160
$address_replaced = true;
161
if (($curwanipv6 == $oldipv6) && (substr($interface_real, 0, 4) != "ovpn")) {
162
	/**
163
	 * Reasons other than RENEW may replace the current address with the
164
	 * same one causing the address to temporarily disappear - this is
165
	 * also considered a change.
166
	 */
167
	switch ($reason) {
168
		case 'RENEW':
169
			$address_replaced = false;
170
			break;
171
		default:
172
			break;
173
	}
174
}
175

    
176
/**
177
 * @var bool Used for only action when the DNS information changes.
178
 */
179
$dns_changed = true;
180
if (($new_domain_name_servers_received === false) && ($new_searchdomains_received === false)) {
181
	$dns_changed = false;
182
}
183

    
184
// Only continue on RENEW if there has been a change.
185
if (($reason == 'RENEW') && !$address_replaced && !$dns_changed) {
186
	return;
187
}
188

    
189
if ($reason == 'RENEW') {
190
	log_error("rc.newwanipv6: Info: received {$reason} on {$argument} (previous IP address: {$oldipv6}).");
191
}
192
log_error("rc.newwanipv6: on (IP address: {$curwanipv6}) (interface: {$interface}) (real interface: {$interface_real}).");
193

    
194
if (($reason != 'RENEW') || $address_replaced) {
195
	$grouptmp = link_interface_to_group($interface);
196
	if (!empty($grouptmp)) {
197
		array_walk($grouptmp, 'interface_group_add_member');
198
	}
199

    
200
	link_interface_to_track6($interface, "update");
201
}
202

    
203
if (($reason != 'RENEW') || $dns_changed) {
204
	/* regenerate resolv.conf if DNS overrides are allowed */
205
	system_resolvconf_generate(true);
206
}
207

    
208
/* reconfigure our gateway monitor, dpinger results need to be 
209
 * available when configuring the default gateway */
210
setup_gateways_monitor();
211

    
212
/* reconfigure static routes (kernel may have deleted them) */
213
system_routing_configure($interface);
214

    
215
if (is_platform_booting()) {
216
	// avoid race conditions in many of the below functions that occur during boot
217
	touch("/tmp/{$interface_real}_dhcp6_complete");
218
	exit;
219
}
220

    
221
/* signal filter reload */
222
filter_configure();
223

    
224
$srvupdate = true;
225
if (empty($oldipv6) || is_ipaddrv6($oldipv6)) {
226
	if (($curwanipv6 == $oldipv6) && !file_exists("{$g['tmp_path']}/{$interface}_upstart6")) {
227
		// Still need to sync VPNs on PPPoE and such, as even with the same IP the VPN software is unhappy with the IP disappearing.
228
		if (!in_array(config_get_path("interfaces/{$interface}/ipaddr"), array('pppoe', 'pptp', 'ppp'))) {
229
			return;
230
		} else {
231
			$srvupdate = false;
232
		}
233
	} elseif (($reason != 'RENEW') && does_interface_exist($interface_real) && !empty($oldipv6)) {
234
		/**
235
		 * Don't call this on RENEW since dhcp6c already removes the old
236
		 * address and adds the new one.
237
		 */
238
		mwexec("/sbin/ifconfig {$interface_real} inet6 {$oldipv6} delete");
239
	}
240

    
241
	file_put_contents(g_get('vardb_path') . "/{$interface_real}_cacheipv6", $curwanipv6);
242
}
243

    
244
if ($srvupdate) {
245
	/* reload unbound */
246
	services_unbound_configure(true, $interface);
247

    
248
	/* perform RFC 2136 DNS update */
249
	services_dnsupdate_process($interface);
250

    
251
	/* signal dyndns update */
252
	services_dyndns_configure($interface);
253
}
254

    
255
/* reconfigure IPsec tunnels */
256
ipsec_force_reload($interface, 'inet6');
257

    
258
/* start OpenVPN server & clients */
259
if (substr($interface_real, 0, 4) != "ovpn") {
260
	openvpn_resync_all($interface, 'inet6');
261
}
262

    
263
/* reconfigure GRE/GIF tunnels */
264
$gre = link_interface_to_tunnelif($interface, 'gre', 'inet6');
265
array_walk($gre, 'interface_gre_configure');
266

    
267
$gif = link_interface_to_tunnelif($interface, 'gif', 'inet6');
268
array_walk($gif, 'interface_gif_configure');
269

    
270
foreach ($gif as $giftun) {
271
	$confif = convert_real_interface_to_friendly_interface_name($giftun['gifif']);
272
	if (!empty($confif)) {
273
		interface_configure($confif);
274
		system_routing_configure($confif);
275
	}
276
}
277
foreach ($gre as $gretun) {
278
	$confif = convert_real_interface_to_friendly_interface_name($gretun['greif']);
279
	if (!empty($confif)) {
280
		interface_configure($confif);
281
		system_routing_configure($confif);
282
	}
283
}
284

    
285
if ($srvupdate) {
286
	/* reload graphing functions */
287
	enable_rrd_graphing();
288

    
289
	restart_packages();
290
}
291

    
292
unlink_if_exists("{$g['tmp_path']}/{$interface}_upstart6");
293
if (empty(config_get_path("interfaces/{$interface}/ipaddr"))) {
294
	unlink_if_exists("{$g['tmp_path']}/{$interface}_upstart4");
295
}
296

    
297
/* Unconditional filter reload to ensure the correct rules and gateways are
298
 * active after this script has processed all changes.
299
 * See https://redmine.pfsense.org/issues/13228 */
300
filter_configure();
301
?>
(57-57/84)