Project

General

Profile

Download (8.34 KB) Statistics
| Branch: | Tag: | Revision:
1 cb7d18d5 Renato Botelho
#!/usr/local/bin/php-cgi -f
2 5b237745 Scott Ullrich
<?php
3
/*
4 ac24dc24 Renato Botelho
 * rc.newwanip
5
 *
6
 * part of pfSense (https://www.pfsense.org)
7 38809d47 Renato Botelho do Couto
 * Copyright (c) 2006-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9 0284d79e jim-p
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
10 ac24dc24 Renato Botelho
 * All rights reserved.
11
 *
12
 * Originally part of m0n0wall (http://m0n0.ch/wall)
13 c5d81585 Renato Botelho
 * Copyright (c) 2003-2005 Manuel Kasper <mk@neon1.net>.
14 ac24dc24 Renato Botelho
 * All rights reserved.
15
 *
16 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
20 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
21 ac24dc24 Renato Botelho
 *
22 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
28 5b237745 Scott Ullrich
29 0363c100 Scott Ullrich
/* 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 5f2d078e Scott Ullrich
require_once("filter.inc");
34
require_once("shaper.inc");
35 f973148e Ermal
require_once("ipsec.inc");
36
require_once("vpn.inc");
37 c7f60193 Ermal
require_once("openvpn.inc");
38 470fddb1 Renato Botelho
require_once("Net/IPv6.php");
39 76f5d95c Scott Ullrich
require_once("rrd.inc");
40 0363c100 Scott Ullrich
41 9ccecb65 Ermal
function restart_packages() {
42 b82e4696 Renato Botelho
	global $oldip, $curwanip, $g;
43 9ccecb65 Ermal
44
	/* restart packages */
45 038f6e96 Chris Buechler
	log_error("{$g['product_name']} package system has detected an IP change or dynamic WAN reconnection - $oldip ->  $curwanip - Restarting packages.");
46 0042f5d9 Ermal
	send_event("service reload packages");
47 9ccecb65 Ermal
}
48 0363c100 Scott Ullrich
49 9ccecb65 Ermal
/* Interface IP address has changed */
50 e173dd74 Phil Davis
if (isset($_GET['interface'])) {
51 c71b14fd Ermal
	$argument = $_GET['interface'];
52 e173dd74 Phil Davis
} else {
53 c71b14fd Ermal
	$argument = str_replace("\n", "", $argv[1]);
54 e173dd74 Phil Davis
}
55 0363c100 Scott Ullrich
56 6960993d Augustin-FL
openlog("", LOG_PID, LOG_LOCAL0);
57 002d286c Chris Buechler
log_error("rc.newwanip: Info: starting on {$argument}.");
58 2aa80139 Scott Ullrich
59 91571af5 Ermal
if (empty($argument)) {
60 7488a9e0 Renato Botelho
	$interface = "wan";
61
	$interface_real = get_real_interface();
62 67ee1ec5 Ermal Luçi
} else {
63 7488a9e0 Renato Botelho
	$interface = convert_real_interface_to_friendly_interface_name($argument);
64
	$interface_real = $argument;
65 de8f0075 Renato Botelho
}
66
67 91571af5 Ermal
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
68
69 63f72828 jim-p
/* 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 e173dd74 Phil Davis
if (empty($argument)) {
76 de8f0075 Renato Botelho
	$curwanip = get_interface_ip();
77 e173dd74 Phil Davis
} else {
78 7488a9e0 Renato Botelho
	$curwanip = find_interface_ip($interface_real, true);
79 086cf944 Phil Davis
	if ($curwanip == "") {
80 7488a9e0 Renato Botelho
		$curwanip = get_interface_ip($interface);
81 e173dd74 Phil Davis
	}
82 b1f7e75e Scott Ullrich
}
83
84 80d3cf1c Chris Buechler
log_error("rc.newwanip: on (IP address: {$curwanip}) (interface: {$interface_descr}[{$interface}]) (real interface: {$interface_real}).");
85 a8840317 Scott Ullrich
86 91571af5 Ermal
/*
87 31dbd433 Ermal
 * NOTE: Take care of openvpn, no-ip or similar interfaces if you generate the event to reconfigure an interface.
88 91571af5 Ermal
 *      i.e. OpenVPN might be in tap mode and not have an ip.
89
 */
90 31dbd433 Ermal
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 45f2708c Scott Ullrich
}
99
100 91571af5 Ermal
/* XXX: This really possible? */
101 9ccecb65 Ermal
if (empty($interface)) {
102 e173dd74 Phil Davis
	if (platform_booting()) {
103 30a61a89 Ermal LUÇI
		return;
104 e173dd74 Phil Davis
	}
105 ef945b86 Chris Buechler
	log_error("rc.newwanip called with empty interface.");
106 9ccecb65 Ermal
	filter_configure();
107
	restart_packages();
108 3e5933f2 Ermal
	return;
109 9ccecb65 Ermal
}
110
111 40655051 Ermal
$oldip = "0.0.0.0";
112 e173dd74 Phil Davis
if (file_exists("{$g['vardb_path']}/{$interface}_cacheip")) {
113 40655051 Ermal
	$oldip = file_get_contents("{$g['vardb_path']}/{$interface}_cacheip");
114 e173dd74 Phil Davis
}
115 40655051 Ermal
116 80d3cf1c Chris Buechler
/* regenerate resolv.conf */
117
system_resolvconf_generate(true);
118 0363c100 Scott Ullrich
119 aea7da2f Phil Davis
/* write the current interface IP to file */
120 e173dd74 Phil Davis
if (is_ipaddr($curwanip)) {
121 91571af5 Ermal
	@file_put_contents("{$g['vardb_path']}/{$interface}_ip", $curwanip);
122 e173dd74 Phil Davis
}
123 0363c100 Scott Ullrich
124 b5264f22 Ermal
link_interface_to_vips($interface, "update");
125
126 48484aac Ermal
unset($gre);
127
$gre = link_interface_to_gre($interface);
128 e173dd74 Phil Davis
if (!empty($gre)) {
129 48484aac Ermal
	array_walk($gre, 'interface_gre_configure');
130 e173dd74 Phil Davis
}
131
132 48484aac Ermal
unset($gif);
133
$gif = link_interface_to_gif($interface);
134 e173dd74 Phil Davis
if (!empty($gif)) {
135 48484aac Ermal
	array_walk($gif, 'interface_gif_configure');
136 e173dd74 Phil Davis
}
137 48484aac Ermal
138 ed62880b Ermal
$grouptmp = link_interface_to_group($interface);
139 e173dd74 Phil Davis
if (!empty($grouptmp)) {
140 ed62880b Ermal
	array_walk($grouptmp, 'interface_group_add_member');
141 e173dd74 Phil Davis
}
142 ed62880b Ermal
143 9ce0dd12 Ermal
unset($bridgetmp);
144
$bridgetmp = link_interface_to_bridge($interface);
145 e173dd74 Phil Davis
if (!empty($bridgetmp)) {
146 9af087de Chris Buechler
	interface_bridge_add_member($bridgetmp, $interface_real);
147 e173dd74 Phil Davis
}
148 b5264f22 Ermal
149
/* make new hosts file */
150 c9065c1e Ermal
system_hosts_generate();
151 8614f335 Ermal
152 e173dd74 Phil Davis
/* check tunnelled IPv6 interface tracking */
153
switch ($config['interfaces'][$interface]['ipaddrv6']) {
154 b5191708 smos
	case "6to4":
155 7a04cd20 Ermal
		interface_6to4_configure($interface, $config['interfaces'][$interface]);
156 b5191708 smos
		break;
157
	case "6rd":
158 7a04cd20 Ermal
		interface_6rd_configure($interface, $config['interfaces'][$interface]);
159 b5191708 smos
		break;
160 0b18ef05 Ermal
	case "dhcp6":
161 0c9b98c0 Chris Buechler
		// 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 0b18ef05 Ermal
			interface_dhcpv6_configure($interface, $config['interfaces'][$interface]);
165 e173dd74 Phil Davis
		}
166 0b18ef05 Ermal
		break;
167 b5191708 smos
}
168 b746dc61 Ermal
169 90c386ba jim-p
/* Check Gif tunnels */
170 eb8ad408 Ermal LUÇI
if (!empty($gif)) {
171 e173dd74 Phil Davis
	foreach ($gif as $giftun) {
172 b9767e86 Renato Botelho
		$confif = convert_real_interface_to_friendly_interface_name($giftun['gifif']);
173 eb8ad408 Ermal LUÇI
		if (!empty($confif)) {
174
			interface_configure($confif);
175
			system_routing_configure($confif);
176
		}
177
	}
178
}
179
if (!empty($gre)) {
180 086cf944 Phil Davis
	foreach ($gre as $gretun) {
181 b9767e86 Renato Botelho
		$confif = convert_real_interface_to_friendly_interface_name($gretun['greif']);
182 eb8ad408 Ermal LUÇI
		if (!empty($confif)) {
183
			interface_configure($confif);
184
			system_routing_configure($confif);
185 90c386ba jim-p
		}
186
	}
187
}
188
189 561077c7 PiBa-NL
if (platform_booting() && !in_array(substr($interface_real, 0, 3), array("ppp", "ppt", "l2t"))) {
190 f54ca2e1 PiBa-NL
	// unlike dhcp interfaces which wait until they get an ip, a ppp connection lets the boot continue while 
191 f3f98e97 Phil Davis
	// trying to acquire a ip address so to avoid a race condition where it would be possible that the default
192 f54ca2e1 PiBa-NL
	// 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 c4b5c8be Chris Buechler
	// 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 ef59836c Ermal
/*
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 6d744cc8 Renato Botelho
	/*
210 e173dd74 Phil Davis
	 * Some services (e.g. dyndns, see ticket #4066) depend on
211 6d744cc8 Renato Botelho
	 * filter_configure() to be called before, otherwise pass out
212 4e322e2c Phil Davis
	 * route-to rules have the old ip set in 'from' and connections
213
	 * do not go through the correct link
214 6d744cc8 Renato Botelho
	 */
215 56c6993c Renato Botelho
	filter_configure_sync();
216 6d744cc8 Renato Botelho
217 742cc9ae PiBa-NL
	/* reconfigure our gateway monitor, dpinger results need to be 
218
	 * available when configuring the default gateway */
219
	setup_gateways_monitor();
220
221 ef59836c Ermal
	/* reconfigure static routes (kernel may have deleted them) */
222
	system_routing_configure($interface);
223 69b54cbe smos
224 86e6e0bc jim-p
	/* 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 36dbc3ae Chris Buechler
	/* reload unbound */
237
	services_unbound_configure();
238 da70dc36 jim-p
239 e173dd74 Phil Davis
	if (is_ipaddr($curwanip)) {
240 91571af5 Ermal
		@file_put_contents("{$g['vardb_path']}/{$interface}_cacheip", $curwanip);
241 e173dd74 Phil Davis
	}
242 ef59836c Ermal
243
	/* perform RFC 2136 DNS update */
244
	services_dnsupdate_process($interface);
245 2c6b0d67 Ermal
246 ef59836c Ermal
	/* signal dyndns update */
247
	services_dyndns_configure($interface);
248 9c4c5e80 jim-p
249 611b65a8 jim-p
	/* reconfigure IPsec tunnels */
250 c6220dcf jim-p
	ipsec_force_reload($interface);
251 611b65a8 jim-p
252
	/* start OpenVPN server & clients */
253 e173dd74 Phil Davis
	if (substr($interface_real, 0, 4) != "ovpn") {
254 158f0e7d jim-p
		openvpn_resync_all($interface);
255 e173dd74 Phil Davis
	}
256 611b65a8 jim-p
257 ef59836c Ermal
	/* reload graphing functions */
258
	enable_rrd_graphing();
259 17649c87 Ermal
260 ef59836c Ermal
	/* reload igmpproxy */
261
	services_igmpproxy_configure();
262 0363c100 Scott Ullrich
263 ef59836c Ermal
	/* restart snmp */
264
	services_snmpd_configure();
265 ebbae443 jim-p
266 ef59836c Ermal
	restart_packages();
267 6d744cc8 Renato Botelho
} else {
268
	/* signal filter reload */
269
	filter_configure();
270 ef59836c Ermal
}
271 77901966 Renato Botelho
272 2c6b0d67 Ermal
?>