Project

General

Profile

Download (9.72 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
 * rc.newwanip
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-2024 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("rrd.inc");
40

    
41
function restart_packages() {
42
	global $oldip, $curwanip, $g;
43

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

    
49
/* Interface IP address has changed */
50
if (isset($_GET['interface'])) {
51
	$argument = $_GET['interface'];
52
} else {
53
	$argument = str_replace("\n", "", $argv[1]);
54
}
55

    
56
log_error("rc.newwanip: Info: starting on {$argument}.");
57

    
58
if (empty($argument)) {
59
	$interface = "wan";
60
	$interface_real = get_real_interface();
61
} else {
62
	$interface = convert_real_interface_to_friendly_interface_name($argument);
63
	if (empty($interface)) {
64
		log_error("Interface is unassigned, nothing to do.");
65
		return;
66
	}
67
	$interface_real = $argument;
68
}
69

    
70
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
71

    
72
/* If the interface is configured and not enabled, bail. We do not need to change settings for disabled interfaces. #3313 */
73
if (is_array(config_get_path("interfaces/{$interface}")) && !config_path_enabled("interfaces/{$interface}")) {
74
	log_error("Interface is disabled, nothing to do.");
75
	return;
76
}
77

    
78
if (empty($argument)) {
79
	$curwanip = get_interface_ip();
80
} else {
81
	$curwanip = find_interface_ip($interface_real, true);
82
	if ($curwanip == "") {
83
		$curwanip = get_interface_ip($interface);
84
	}
85
}
86

    
87
log_error("rc.newwanip: on (IP address: {$curwanip}) (interface: {$interface_descr}[{$interface}]) (real interface: {$interface_real}).");
88

    
89
/*
90
 * NOTE: Take care of openvpn, no-ip or similar interfaces if you generate the event to reconfigure an interface.
91
 *      i.e. OpenVPN might be in tap mode and not have an ip.
92
 */
93
if ($curwanip == "0.0.0.0" || !is_ipaddr($curwanip)) {
94
	if (substr($interface_real, 0, 4) != "ovpn") {
95
		if (!empty(config_get_path("interfaces/{$interface}/ipaddr"))) {
96
			log_error("rc.newwanip: Failed to update {$interface} IP, restarting...");
97
			send_event("interface reconfigure {$interface}");
98
			return;
99
		}
100
	}
101
}
102

    
103
/* XXX: This really possible? */
104
if (empty($interface)) {
105
	if (is_platform_booting()) {
106
		return;
107
	}
108
	log_error("rc.newwanip called with empty interface.");
109
	filter_configure();
110
	restart_packages();
111
	return;
112
}
113

    
114
$oldip = "0.0.0.0";
115
if (file_exists(g_get('vardb_path') . "/{$interface_real}_cacheip")) {
116
	$oldip = file_get_contents(g_get('vardb_path') . "/{$interface_real}_cacheip");
117
}
118

    
119
/* regenerate resolv.conf */
120
system_resolvconf_generate(true);
121

    
122
/* write the current interface IP to file */
123
if (is_ipaddr($curwanip)) {
124
	@file_put_contents(g_get('vardb_path') . "/{$interface_real}_ip", $curwanip);
125
}
126

    
127
link_interface_to_vips($interface, "update");
128

    
129
$gre = link_interface_to_tunnelif($interface, 'gre', 'inet');
130
array_walk($gre, 'interface_gre_configure');
131

    
132
$gif = link_interface_to_tunnelif($interface, 'gif', 'inet');
133
array_walk($gif, 'interface_gif_configure');
134

    
135
/* reconfigure L2TP/PPTP tunnels, see https://redmine.pfsense.org/issues/12072 */ 
136
$ppp = link_interface_to_ppp_tunnelif($interface);
137

    
138
$grouptmp = link_interface_to_group($interface);
139
if (!empty($grouptmp)) {
140
	array_walk($grouptmp, 'interface_group_add_member');
141
}
142

    
143
unset($bridgetmp);
144
$bridgetmp = link_interface_to_bridge($interface);
145
if (!empty($bridgetmp)) {
146
	interface_bridge_add_member($bridgetmp, $interface_real);
147
}
148

    
149
/* make new hosts file */
150
system_hosts_generate();
151

    
152
/* check tunnelled IPv6 interface tracking */
153
switch (config_get_path("interfaces/{$interface}/ipaddrv6")) {
154
	case "6to4":
155
		interface_6to4_configure($interface, config_get_path("interfaces/{$interface}"));
156
		break;
157
	case "6rd":
158
		interface_6rd_configure($interface, config_get_path("interfaces/{$interface}"));
159
		break;
160
	case "slaac":
161
	case "dhcp6":
162
		// N.B. PPP connections using PPP as the IPv6 parent interface are excluded because the ppp-ipv6 script calls
163
		// interface_dhcpv6_configure() for these connections after IPv6CP is up
164
		if ((((config_get_path("interfaces/{$interface}/ipaddrv6") == 'dhcp6') &&
165
		    config_path_enabled("interfaces/{$interface}", 'dhcp6usev4iface')) ||
166
		    ((config_get_path("interfaces/{$interface}/ipaddrv6") == 'slaac') &&
167
		    config_path_enabled("interfaces/{$interface}", 'slaacusev4iface'))) &&
168
		    !interface_isppp_type($interface)) {
169
			interface_dhcpv6_configure($interface, config_get_path("interfaces/{$interface}"));
170
		}
171
		break;
172
	default:
173
		break;
174
}
175

    
176
/* Check Gif tunnels */
177
foreach ($gif as $giftun) {
178
	$confif = convert_real_interface_to_friendly_interface_name($giftun['gifif']);
179
	if (!empty($confif)) {
180
		interface_configure($confif);
181
		system_routing_configure($confif);
182
	}
183
}
184
foreach ($gre as $gretun) {
185
	$confif = convert_real_interface_to_friendly_interface_name($gretun['greif']);
186
	if (!empty($confif)) {
187
		interface_configure($confif);
188
		system_routing_configure($confif);
189
	}
190
}
191
foreach ($ppp as $ppptun) {
192
	$confif = convert_real_interface_to_friendly_interface_name($ppptun['if']);
193
	if (!empty($confif)) {
194
		interface_configure($confif);
195
		system_routing_configure($confif);
196
	}
197
}
198

    
199
if (is_platform_booting() && !in_array(substr($interface_real, 0, 3), array("ppp", "ppt", "l2t"))) {
200
	// unlike dhcp interfaces which wait until they get an ip, a ppp connection lets the boot continue while 
201
	// trying to acquire a ip address so to avoid a race condition where it would be possible that the default
202
	// route would not be set, this script must continue to use the new assigned ip even while booting
203
	// https://redmine.pfsense.org/issues/8561
204
	
205
	// avoid race conditions in many of the below functions that occur during boot
206
	// setting up gateways monitor doesn't seem to have issues here, and fixes the
207
	// most commonly encountered bugs from earlier versions when everything below
208
	// was skipped during boot
209
	filter_configure_sync();
210
	setup_gateways_monitor();
211

    
212
	// Make sure the default gateway is set up after DHCP succeeds. See:
213
	// https://redmine.pfsense.org/issues/15791
214
	system_routing_configure($interface);
215
	exit;
216
}
217

    
218
/*
219
 * We need to force sync VPNs on such even when the IP is the same for dynamic interfaces.
220
 * Even with the same IP the VPN software is unhappy with the IP disappearing, and we
221
 * could be failing back in which case we need to switch IPs back anyhow.
222
 */
223
if (!is_ipaddr($oldip) || ($curwanip != $oldip) || file_exists("{$g['tmp_path']}/{$interface}_upstart4") ||
224
    (!is_ipaddrv4(config_get_path("interfaces/{$interface}/ipaddr")) && (config_get_path("interfaces/{$interface}/ipaddr") != 'dhcp'))) {
225
	/*
226
	 * Some services (e.g. dyndns, see ticket #4066) depend on
227
	 * filter_configure() to be called before, otherwise pass out
228
	 * route-to rules have the old ip set in 'from' and connections
229
	 * do not go through the correct link
230
	 */
231
	filter_configure_sync();
232

    
233
	/* reconfigure our gateway monitor, dpinger results need to be 
234
	 * available when configuring the default gateway */
235
	setup_gateways_monitor();
236

    
237
	/* reconfigure static routes (kernel may have deleted them) */
238
	system_routing_configure($interface);
239

    
240
	/* If the IP address changed, kill old states after rules and routing have been updated */
241
	if ($curwanip != $oldip) {
242
		if (config_path_enabled('system', 'ip_change_kill_states')) {
243
			log_error("IP Address has changed, killing all states (ip_change_kill_states is set).");
244
			filter_flush_state_table();
245
		} else {
246
			log_error("IP Address has changed, killing states on former IP Address $oldip.");
247
			pfSense_kill_states($oldip);
248
		}
249
	}
250

    
251
	/* reload unbound */
252
	services_unbound_configure(true, $interface);
253

    
254
	if (is_ipaddr($curwanip)) {
255
		@file_put_contents(g_get('vardb_path') . "/{$interface_real}_cacheip", $curwanip);
256
	}
257

    
258
	/* perform RFC 2136 DNS update */
259
	services_dnsupdate_process($interface);
260

    
261
	/* signal dyndns update */
262
	services_dyndns_configure($interface);
263

    
264
	/* reconfigure IPsec tunnels */
265
	ipsec_force_reload($interface, 'inet');
266

    
267
	/* start OpenVPN server & clients */
268
	if (substr($interface_real, 0, 4) != "ovpn") {
269
		openvpn_resync_all($interface, 'inet');
270
	}
271

    
272
	/* reload graphing functions */
273
	enable_rrd_graphing();
274

    
275
	/* reload igmpproxy */
276
	services_igmpproxy_configure($interface);
277

    
278
	/* restart snmp */
279
	services_snmpd_configure($interface);
280

    
281
	/* restart L2TP VPN */
282
	vpn_l2tp_configure($interface);
283

    
284
	restart_packages();
285

    
286
	unlink_if_exists("{$g['tmp_path']}/{$interface}_upstart4");
287
	if (empty(config_get_path("interfaces/{$interface}/ipaddrv6"))) {
288
		unlink_if_exists("{$g['tmp_path']}/{$interface}_upstart6");
289
	}
290
}
291

    
292
/* Unconditional filter reload to ensure the correct rules and gateways are
293
 * active after this script has processed all changes.
294
 * See https://redmine.pfsense.org/issues/13228 */
295
filter_configure();
296
?>
(57-57/85)