Project

General

Profile

Download (9.38 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 8f2f85c3 Luiz Otavio O Souza
 * Copyright (c) 2014-2022 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 573ec19d Renato Botelho do Couto
	log_error("{$g['product_label']} 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 002d286c Chris Buechler
log_error("rc.newwanip: Info: starting on {$argument}.");
57 2aa80139 Scott Ullrich
58 91571af5 Ermal
if (empty($argument)) {
59 7488a9e0 Renato Botelho
	$interface = "wan";
60
	$interface_real = get_real_interface();
61 67ee1ec5 Ermal Luçi
} else {
62 7488a9e0 Renato Botelho
	$interface = convert_real_interface_to_friendly_interface_name($argument);
63
	$interface_real = $argument;
64 de8f0075 Renato Botelho
}
65
66 91571af5 Ermal
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
67
68 63f72828 jim-p
/* If the interface is configured and not enabled, bail. We do not need to change settings for disabled interfaces. #3313 */
69
if (is_array($config['interfaces'][$interface]) && !isset($config['interfaces'][$interface]['enable'])) {
70
	log_error("Interface is disabled, nothing to do.");
71
	return;
72
}
73
74 e173dd74 Phil Davis
if (empty($argument)) {
75 de8f0075 Renato Botelho
	$curwanip = get_interface_ip();
76 e173dd74 Phil Davis
} else {
77 7488a9e0 Renato Botelho
	$curwanip = find_interface_ip($interface_real, true);
78 086cf944 Phil Davis
	if ($curwanip == "") {
79 7488a9e0 Renato Botelho
		$curwanip = get_interface_ip($interface);
80 e173dd74 Phil Davis
	}
81 b1f7e75e Scott Ullrich
}
82
83 80d3cf1c Chris Buechler
log_error("rc.newwanip: on (IP address: {$curwanip}) (interface: {$interface_descr}[{$interface}]) (real interface: {$interface_real}).");
84 a8840317 Scott Ullrich
85 91571af5 Ermal
/*
86 31dbd433 Ermal
 * NOTE: Take care of openvpn, no-ip or similar interfaces if you generate the event to reconfigure an interface.
87 91571af5 Ermal
 *      i.e. OpenVPN might be in tap mode and not have an ip.
88
 */
89 31dbd433 Ermal
if ($curwanip == "0.0.0.0" || !is_ipaddr($curwanip)) {
90
	if (substr($interface_real, 0, 4) != "ovpn") {
91
		if (!empty($config['interfaces'][$interface]['ipaddr'])) {
92
			log_error("rc.newwanip: Failed to update {$interface} IP, restarting...");
93
			send_event("interface reconfigure {$interface}");
94
			return;
95
		}
96
	}
97 45f2708c Scott Ullrich
}
98
99 91571af5 Ermal
/* XXX: This really possible? */
100 9ccecb65 Ermal
if (empty($interface)) {
101 e173dd74 Phil Davis
	if (platform_booting()) {
102 30a61a89 Ermal LUÇI
		return;
103 e173dd74 Phil Davis
	}
104 ef945b86 Chris Buechler
	log_error("rc.newwanip called with empty interface.");
105 9ccecb65 Ermal
	filter_configure();
106
	restart_packages();
107 3e5933f2 Ermal
	return;
108 9ccecb65 Ermal
}
109
110 40655051 Ermal
$oldip = "0.0.0.0";
111 e173dd74 Phil Davis
if (file_exists("{$g['vardb_path']}/{$interface}_cacheip")) {
112 40655051 Ermal
	$oldip = file_get_contents("{$g['vardb_path']}/{$interface}_cacheip");
113 e173dd74 Phil Davis
}
114 40655051 Ermal
115 80d3cf1c Chris Buechler
/* regenerate resolv.conf */
116
system_resolvconf_generate(true);
117 0363c100 Scott Ullrich
118 aea7da2f Phil Davis
/* write the current interface IP to file */
119 e173dd74 Phil Davis
if (is_ipaddr($curwanip)) {
120 91571af5 Ermal
	@file_put_contents("{$g['vardb_path']}/{$interface}_ip", $curwanip);
121 e173dd74 Phil Davis
}
122 0363c100 Scott Ullrich
123 b5264f22 Ermal
link_interface_to_vips($interface, "update");
124
125 474b0fed Viktor G
$gre = link_interface_to_tunnelif($interface, 'gre', 'inet');
126 a17e9816 Renato Botelho do Couto
array_walk($gre, 'interface_gre_configure');
127 e173dd74 Phil Davis
128 474b0fed Viktor G
$gif = link_interface_to_tunnelif($interface, 'gif', 'inet');
129 a17e9816 Renato Botelho do Couto
array_walk($gif, 'interface_gif_configure');
130 48484aac Ermal
131 ce04d03f Viktor G
/* reconfigure L2TP/PPTP tunnels, see https://redmine.pfsense.org/issues/12072 */ 
132
$ppp = link_interface_to_ppp_tunnelif($interface);
133
134 ed62880b Ermal
$grouptmp = link_interface_to_group($interface);
135 e173dd74 Phil Davis
if (!empty($grouptmp)) {
136 ed62880b Ermal
	array_walk($grouptmp, 'interface_group_add_member');
137 e173dd74 Phil Davis
}
138 ed62880b Ermal
139 9ce0dd12 Ermal
unset($bridgetmp);
140
$bridgetmp = link_interface_to_bridge($interface);
141 e173dd74 Phil Davis
if (!empty($bridgetmp)) {
142 9af087de Chris Buechler
	interface_bridge_add_member($bridgetmp, $interface_real);
143 e173dd74 Phil Davis
}
144 b5264f22 Ermal
145
/* make new hosts file */
146 c9065c1e Ermal
system_hosts_generate();
147 8614f335 Ermal
148 e173dd74 Phil Davis
/* check tunnelled IPv6 interface tracking */
149
switch ($config['interfaces'][$interface]['ipaddrv6']) {
150 b5191708 smos
	case "6to4":
151 7a04cd20 Ermal
		interface_6to4_configure($interface, $config['interfaces'][$interface]);
152 b5191708 smos
		break;
153
	case "6rd":
154 7a04cd20 Ermal
		interface_6rd_configure($interface, $config['interfaces'][$interface]);
155 b5191708 smos
		break;
156 d4cde1bd Viktor G
	case "slaac":
157 0b18ef05 Ermal
	case "dhcp6":
158 0c9b98c0 Chris Buechler
		// 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 dd3d48af Viktor G
		if (((($config['interfaces'][$interface]['ipaddrv6'] == 'dhcp6') &&
161 d4cde1bd Viktor G
		    isset($config['interfaces'][$interface]['dhcp6usev4iface'])) ||
162
		    (($config['interfaces'][$interface]['ipaddrv6'] == 'slaac') &&
163 dd3d48af Viktor G
		    isset($config['interfaces'][$interface]['slaacusev4iface']))) &&
164 d4cde1bd Viktor G
		    !interface_isppp_type($interface)) {
165 0b18ef05 Ermal
			interface_dhcpv6_configure($interface, $config['interfaces'][$interface]);
166 e173dd74 Phil Davis
		}
167 0b18ef05 Ermal
		break;
168 d4cde1bd Viktor G
	default:
169
		break;
170 b5191708 smos
}
171 b746dc61 Ermal
172 90c386ba jim-p
/* Check Gif tunnels */
173 a17e9816 Renato Botelho do Couto
foreach ($gif as $giftun) {
174
	$confif = convert_real_interface_to_friendly_interface_name($giftun['gifif']);
175
	if (!empty($confif)) {
176
		interface_configure($confif);
177
		system_routing_configure($confif);
178 eb8ad408 Ermal LUÇI
	}
179
}
180 a17e9816 Renato Botelho do Couto
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 cf3096df Viktor G
	}
186
}
187 ce04d03f Viktor G
foreach ($ppp as $ppptun) {
188
	$confif = convert_real_interface_to_friendly_interface_name($ppptun['if']);
189
	if (!empty($confif)) {
190
		interface_configure($confif);
191
		system_routing_configure($confif);
192
	}
193
}
194 90c386ba jim-p
195 561077c7 PiBa-NL
if (platform_booting() && !in_array(substr($interface_real, 0, 3), array("ppp", "ppt", "l2t"))) {
196 f54ca2e1 PiBa-NL
	// unlike dhcp interfaces which wait until they get an ip, a ppp connection lets the boot continue while 
197 f3f98e97 Phil Davis
	// trying to acquire a ip address so to avoid a race condition where it would be possible that the default
198 f54ca2e1 PiBa-NL
	// route would not be set, this script must continue to use the new assigned ip even while booting
199
	// https://redmine.pfsense.org/issues/8561
200
	
201 c4b5c8be Chris Buechler
	// avoid race conditions in many of the below functions that occur during boot
202
	// setting up gateways monitor doesn't seem to have issues here, and fixes the
203
	// most commonly encountered bugs from earlier versions when everything below
204
	// was skipped during boot
205
	setup_gateways_monitor();
206
	exit;
207
}
208
209 ef59836c Ermal
/*
210
 * We need to force sync VPNs on such even when the IP is the same for dynamic interfaces.
211
 * Even with the same IP the VPN software is unhappy with the IP disappearing, and we
212
 * could be failing back in which case we need to switch IPs back anyhow.
213
 */
214 ec73bb89 Viktor G
if (!is_ipaddr($oldip) || ($curwanip != $oldip) || file_exists("{$g['tmp_path']}/{$interface}_upstart4") ||
215 27f89b06 Viktor G
    (!is_ipaddrv4($config['interfaces'][$interface]['ipaddr']) && ($config['interfaces'][$interface]['ipaddr'] != 'dhcp'))) {
216 6d744cc8 Renato Botelho
	/*
217 e173dd74 Phil Davis
	 * Some services (e.g. dyndns, see ticket #4066) depend on
218 6d744cc8 Renato Botelho
	 * filter_configure() to be called before, otherwise pass out
219 4e322e2c Phil Davis
	 * route-to rules have the old ip set in 'from' and connections
220
	 * do not go through the correct link
221 6d744cc8 Renato Botelho
	 */
222 56c6993c Renato Botelho
	filter_configure_sync();
223 6d744cc8 Renato Botelho
224 742cc9ae PiBa-NL
	/* reconfigure our gateway monitor, dpinger results need to be 
225
	 * available when configuring the default gateway */
226
	setup_gateways_monitor();
227
228 ef59836c Ermal
	/* reconfigure static routes (kernel may have deleted them) */
229
	system_routing_configure($interface);
230 69b54cbe smos
231 86e6e0bc jim-p
	/* If the IP address changed, kill old states after rules and routing have been updated */
232
	if ($curwanip != $oldip) {
233
		if (isset($config['system']['ip_change_kill_states'])) {
234
			log_error("IP Address has changed, killing all states (ip_change_kill_states is set).");
235 ef094bef Renato Botelho do Couto
			pfSense_kill_states(utf8_encode($oldip));
236 86e6e0bc jim-p
			filter_flush_state_table();
237
		} else {
238
			log_error("IP Address has changed, killing states on former IP Address $oldip.");
239 ef094bef Renato Botelho do Couto
			pfSense_kill_states(utf8_encode($oldip));
240 86e6e0bc jim-p
		}
241
	}
242
243 36dbc3ae Chris Buechler
	/* reload unbound */
244 6ac625e8 Viktor G
	services_unbound_configure(true, $interface);
245 da70dc36 jim-p
246 e173dd74 Phil Davis
	if (is_ipaddr($curwanip)) {
247 91571af5 Ermal
		@file_put_contents("{$g['vardb_path']}/{$interface}_cacheip", $curwanip);
248 e173dd74 Phil Davis
	}
249 ef59836c Ermal
250
	/* perform RFC 2136 DNS update */
251
	services_dnsupdate_process($interface);
252 2c6b0d67 Ermal
253 ef59836c Ermal
	/* signal dyndns update */
254
	services_dyndns_configure($interface);
255 9c4c5e80 jim-p
256 611b65a8 jim-p
	/* reconfigure IPsec tunnels */
257 7c97240f Viktor G
	ipsec_force_reload($interface, 'inet');
258 611b65a8 jim-p
259
	/* start OpenVPN server & clients */
260 e173dd74 Phil Davis
	if (substr($interface_real, 0, 4) != "ovpn") {
261 810f1026 Viktor G
		openvpn_resync_all($interface, 'inet');
262 e173dd74 Phil Davis
	}
263 611b65a8 jim-p
264 ef59836c Ermal
	/* reload graphing functions */
265
	enable_rrd_graphing();
266 17649c87 Ermal
267 ef59836c Ermal
	/* reload igmpproxy */
268 1098cb94 Viktor G
	services_igmpproxy_configure($interface);
269 0363c100 Scott Ullrich
270 ef59836c Ermal
	/* restart snmp */
271 dc6a9ddc Viktor G
	services_snmpd_configure($interface);
272 ebbae443 jim-p
273 2d82d2e3 Viktor G
	/* restart L2TP VPN */
274
	vpn_l2tp_configure($interface);
275
276 ef59836c Ermal
	restart_packages();
277 ec73bb89 Viktor G
278
	unlink_if_exists("{$g['tmp_path']}/{$interface}_upstart4");
279
	if (empty($config['interfaces'][$interface]['ipaddrv6'])) {
280
		unlink_if_exists("{$g['tmp_path']}/{$interface}_upstart6");
281
	}
282 ef59836c Ermal
}
283 77901966 Renato Botelho
284 ad20a68b jim-p
/* Unconditional filter reload to ensure the correct rules and gateways are
285
 * active after this script has processed all changes.
286
 * See https://redmine.pfsense.org/issues/13228 */
287
filter_configure();
288 2c6b0d67 Ermal
?>