Project

General

Profile

Download (8.34 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-2020 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_name']} 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
openlog("", LOG_PID, LOG_LOCAL0);
57
log_error("rc.newwanip: Info: starting on {$argument}.");
58

    
59
if (empty($argument)) {
60
	$interface = "wan";
61
	$interface_real = get_real_interface();
62
} else {
63
	$interface = convert_real_interface_to_friendly_interface_name($argument);
64
	$interface_real = $argument;
65
}
66

    
67
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
68

    
69
/* If the interface is configured and not enabled, bail. We do not need to change settings for disabled interfaces. #3313 */
70
if (is_array($config['interfaces'][$interface]) && !isset($config['interfaces'][$interface]['enable'])) {
71
	log_error("Interface is disabled, nothing to do.");
72
	return;
73
}
74

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

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

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

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

    
111
$oldip = "0.0.0.0";
112
if (file_exists("{$g['vardb_path']}/{$interface}_cacheip")) {
113
	$oldip = file_get_contents("{$g['vardb_path']}/{$interface}_cacheip");
114
}
115

    
116
/* regenerate resolv.conf */
117
system_resolvconf_generate(true);
118

    
119
/* write the current interface IP to file */
120
if (is_ipaddr($curwanip)) {
121
	@file_put_contents("{$g['vardb_path']}/{$interface}_ip", $curwanip);
122
}
123

    
124
link_interface_to_vips($interface, "update");
125

    
126
unset($gre);
127
$gre = link_interface_to_gre($interface);
128
if (!empty($gre)) {
129
	array_walk($gre, 'interface_gre_configure');
130
}
131

    
132
unset($gif);
133
$gif = link_interface_to_gif($interface);
134
if (!empty($gif)) {
135
	array_walk($gif, 'interface_gif_configure');
136
}
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['interfaces'][$interface]['ipaddrv6']) {
154
	case "6to4":
155
		interface_6to4_configure($interface, $config['interfaces'][$interface]);
156
		break;
157
	case "6rd":
158
		interface_6rd_configure($interface, $config['interfaces'][$interface]);
159
		break;
160
	case "dhcp6":
161
		// N.B. PPP connections using PPP as the IPv6 parent interface are excluded because the ppp-ipv6 script calls
162
		// interface_dhcpv6_configure() for these connections after IPv6CP is up
163
		if (isset($config['interfaces'][$interface]['dhcp6usev4iface']) && !interface_isppp_type($interface)) {
164
			interface_dhcpv6_configure($interface, $config['interfaces'][$interface]);
165
		}
166
		break;
167
}
168

    
169
/* Check Gif tunnels */
170
if (!empty($gif)) {
171
	foreach ($gif as $giftun) {
172
		$confif = convert_real_interface_to_friendly_interface_name($giftun['gifif']);
173
		if (!empty($confif)) {
174
			interface_configure($confif);
175
			system_routing_configure($confif);
176
		}
177
	}
178
}
179
if (!empty($gre)) {
180
	foreach ($gre as $gretun) {
181
		$confif = convert_real_interface_to_friendly_interface_name($gretun['greif']);
182
		if (!empty($confif)) {
183
			interface_configure($confif);
184
			system_routing_configure($confif);
185
		}
186
	}
187
}
188

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

    
203
/*
204
 * We need to force sync VPNs on such even when the IP is the same for dynamic interfaces.
205
 * Even with the same IP the VPN software is unhappy with the IP disappearing, and we
206
 * could be failing back in which case we need to switch IPs back anyhow.
207
 */
208
if (!is_ipaddr($oldip) || $curwanip != $oldip || !is_ipaddrv4($config['interfaces'][$interface]['ipaddr'])) {
209
	/*
210
	 * Some services (e.g. dyndns, see ticket #4066) depend on
211
	 * filter_configure() to be called before, otherwise pass out
212
	 * route-to rules have the old ip set in 'from' and connections
213
	 * do not go through the correct link
214
	 */
215
	filter_configure_sync();
216

    
217
	/* reconfigure our gateway monitor, dpinger results need to be 
218
	 * available when configuring the default gateway */
219
	setup_gateways_monitor();
220

    
221
	/* reconfigure static routes (kernel may have deleted them) */
222
	system_routing_configure($interface);
223

    
224
	/* If the IP address changed, kill old states after rules and routing have been updated */
225
	if ($curwanip != $oldip) {
226
		if (isset($config['system']['ip_change_kill_states'])) {
227
			log_error("IP Address has changed, killing all states (ip_change_kill_states is set).");
228
			pfSense_kill_states($oldip);
229
			filter_flush_state_table();
230
		} else {
231
			log_error("IP Address has changed, killing states on former IP Address $oldip.");
232
			pfSense_kill_states($oldip);
233
		}
234
	}
235

    
236
	/* reload unbound */
237
	services_unbound_configure();
238

    
239
	if (is_ipaddr($curwanip)) {
240
		@file_put_contents("{$g['vardb_path']}/{$interface}_cacheip", $curwanip);
241
	}
242

    
243
	/* perform RFC 2136 DNS update */
244
	services_dnsupdate_process($interface);
245

    
246
	/* signal dyndns update */
247
	services_dyndns_configure($interface);
248

    
249
	/* reconfigure IPsec tunnels */
250
	ipsec_force_reload($interface);
251

    
252
	/* start OpenVPN server & clients */
253
	if (substr($interface_real, 0, 4) != "ovpn") {
254
		openvpn_resync_all($interface);
255
	}
256

    
257
	/* reload graphing functions */
258
	enable_rrd_graphing();
259

    
260
	/* reload igmpproxy */
261
	services_igmpproxy_configure();
262

    
263
	/* restart snmp */
264
	services_snmpd_configure();
265

    
266
	restart_packages();
267
} else {
268
	/* signal filter reload */
269
	filter_configure();
270
}
271

    
272
?>
(54-54/82)