Project

General

Profile

Download (7.68 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-2018 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Originally part of m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2003-2005 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25
 */
26

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

    
39
function restart_packages() {
40
	global $oldip, $curwanip, $g;
41

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

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

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

    
56
if (empty($argument)) {
57
	$interface = "wan";
58
	$interface_real = get_real_interface();
59
} else {
60
	$interface = convert_real_interface_to_friendly_interface_name($argument);
61
	$interface_real = $argument;
62
}
63

    
64
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
65

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

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

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

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

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

    
108
$oldip = "0.0.0.0";
109
if (file_exists("{$g['vardb_path']}/{$interface}_cacheip")) {
110
	$oldip = file_get_contents("{$g['vardb_path']}/{$interface}_cacheip");
111
}
112

    
113
/* regenerate resolv.conf */
114
system_resolvconf_generate(true);
115

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

    
121
link_interface_to_vips($interface, "update");
122

    
123
unset($gre);
124
$gre = link_interface_to_gre($interface);
125
if (!empty($gre)) {
126
	array_walk($gre, 'interface_gre_configure');
127
}
128

    
129
unset($gif);
130
$gif = link_interface_to_gif($interface);
131
if (!empty($gif)) {
132
	array_walk($gif, 'interface_gif_configure');
133
}
134

    
135
$grouptmp = link_interface_to_group($interface);
136
if (!empty($grouptmp)) {
137
	array_walk($grouptmp, 'interface_group_add_member');
138
}
139

    
140
unset($bridgetmp);
141
$bridgetmp = link_interface_to_bridge($interface);
142
if (!empty($bridgetmp)) {
143
	interface_bridge_add_member($bridgetmp, $interface_real);
144
}
145

    
146
/* make new hosts file */
147
system_hosts_generate();
148

    
149
/* check tunnelled IPv6 interface tracking */
150
switch ($config['interfaces'][$interface]['ipaddrv6']) {
151
	case "6to4":
152
		interface_6to4_configure($interface, $config['interfaces'][$interface]);
153
		break;
154
	case "6rd":
155
		interface_6rd_configure($interface, $config['interfaces'][$interface]);
156
		break;
157
	case "dhcp6":
158
		// N.B. PPP connections using PPP as the IPv6 parent interface are excluded because the ppp-ipv6 script calls
159
		// interface_dhcpv6_configure() for these connections after IPv6CP is up
160
		if (isset($config['interfaces'][$interface]['dhcp6usev4iface']) && !interface_isppp_type($interface)) {
161
			interface_dhcpv6_configure($interface, $config['interfaces'][$interface]);
162
		}
163
		break;
164
}
165

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

    
186
if (platform_booting()) {
187
	// avoid race conditions in many of the below functions that occur during boot
188
	// setting up gateways monitor doesn't seem to have issues here, and fixes the
189
	// most commonly encountered bugs from earlier versions when everything below
190
	// was skipped during boot
191
	setup_gateways_monitor();
192
	exit;
193
}
194

    
195
/*
196
 * We need to force sync VPNs on such even when the IP is the same for dynamic interfaces.
197
 * Even with the same IP the VPN software is unhappy with the IP disappearing, and we
198
 * could be failing back in which case we need to switch IPs back anyhow.
199
 */
200
if (!is_ipaddr($oldip) || $curwanip != $oldip || !is_ipaddrv4($config['interfaces'][$interface]['ipaddr'])) {
201
	/* IP changed, kill states accordingly */
202
	if ($curwanip != $oldip) {
203
		if (isset($config['system']['ip_change_kill_states'])) {
204
			log_error("IP Address has changed, killing all states (ip_change_kill_states is set).");
205
			pfSense_kill_states($oldip);
206
			filter_flush_state_table();
207
		} else {
208
			log_error("IP Address has changed, killing states on former IP Address $oldip.");
209
			pfSense_kill_states($oldip);
210
		}
211
	}
212

    
213
	/*
214
	 * Some services (e.g. dyndns, see ticket #4066) depend on
215
	 * filter_configure() to be called before, otherwise pass out
216
	 * route-to rules have the old ip set in 'from' and connections
217
	 * do not go through the correct link
218
	 */
219
	filter_configure_sync();
220

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

    
224
	/* reconfigure our gateway monitor */
225
	setup_gateways_monitor();
226

    
227
	/* reload unbound */
228
	services_unbound_configure();
229

    
230
	if (is_ipaddr($curwanip)) {
231
		@file_put_contents("{$g['vardb_path']}/{$interface}_cacheip", $curwanip);
232
	}
233

    
234
	/* perform RFC 2136 DNS update */
235
	services_dnsupdate_process($interface);
236

    
237
	/* signal dyndns update */
238
	services_dyndns_configure($interface);
239

    
240
	/* reconfigure IPsec tunnels */
241
	vpn_ipsec_force_reload($interface);
242

    
243
	/* start OpenVPN server & clients */
244
	if (substr($interface_real, 0, 4) != "ovpn") {
245
		openvpn_resync_all($interface);
246
	}
247

    
248
	/* reload graphing functions */
249
	enable_rrd_graphing();
250

    
251
	/* reload igmpproxy */
252
	services_igmpproxy_configure();
253

    
254
	/* restart snmp */
255
	services_snmpd_configure();
256

    
257
	restart_packages();
258
} else {
259
	/* signal filter reload */
260
	filter_configure();
261
}
262

    
263
?>
(54-54/79)