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-2022 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
|
$interface_real = $argument;
|
64
|
}
|
65
|
|
66
|
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
|
67
|
|
68
|
/* 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
|
if (empty($argument)) {
|
75
|
$curwanip = get_interface_ip();
|
76
|
} else {
|
77
|
$curwanip = find_interface_ip($interface_real, true);
|
78
|
if ($curwanip == "") {
|
79
|
$curwanip = get_interface_ip($interface);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
log_error("rc.newwanip: on (IP address: {$curwanip}) (interface: {$interface_descr}[{$interface}]) (real interface: {$interface_real}).");
|
84
|
|
85
|
/*
|
86
|
* NOTE: Take care of openvpn, no-ip or similar interfaces if you generate the event to reconfigure an interface.
|
87
|
* i.e. OpenVPN might be in tap mode and not have an ip.
|
88
|
*/
|
89
|
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
|
}
|
98
|
|
99
|
/* XXX: This really possible? */
|
100
|
if (empty($interface)) {
|
101
|
if (platform_booting()) {
|
102
|
return;
|
103
|
}
|
104
|
log_error("rc.newwanip called with empty interface.");
|
105
|
filter_configure();
|
106
|
restart_packages();
|
107
|
return;
|
108
|
}
|
109
|
|
110
|
$oldip = "0.0.0.0";
|
111
|
if (file_exists("{$g['vardb_path']}/{$interface}_cacheip")) {
|
112
|
$oldip = file_get_contents("{$g['vardb_path']}/{$interface}_cacheip");
|
113
|
}
|
114
|
|
115
|
/* regenerate resolv.conf */
|
116
|
system_resolvconf_generate(true);
|
117
|
|
118
|
/* write the current interface IP to file */
|
119
|
if (is_ipaddr($curwanip)) {
|
120
|
@file_put_contents("{$g['vardb_path']}/{$interface}_ip", $curwanip);
|
121
|
}
|
122
|
|
123
|
link_interface_to_vips($interface, "update");
|
124
|
|
125
|
$gre = link_interface_to_tunnelif($interface, 'gre', 'inet');
|
126
|
array_walk($gre, 'interface_gre_configure');
|
127
|
|
128
|
$gif = link_interface_to_tunnelif($interface, 'gif', 'inet');
|
129
|
array_walk($gif, 'interface_gif_configure');
|
130
|
|
131
|
/* reconfigure L2TP/PPTP tunnels, see https://redmine.pfsense.org/issues/12072 */
|
132
|
$ppp = link_interface_to_ppp_tunnelif($interface);
|
133
|
|
134
|
$grouptmp = link_interface_to_group($interface);
|
135
|
if (!empty($grouptmp)) {
|
136
|
array_walk($grouptmp, 'interface_group_add_member');
|
137
|
}
|
138
|
|
139
|
unset($bridgetmp);
|
140
|
$bridgetmp = link_interface_to_bridge($interface);
|
141
|
if (!empty($bridgetmp)) {
|
142
|
interface_bridge_add_member($bridgetmp, $interface_real);
|
143
|
}
|
144
|
|
145
|
/* make new hosts file */
|
146
|
system_hosts_generate();
|
147
|
|
148
|
/* check tunnelled IPv6 interface tracking */
|
149
|
switch ($config['interfaces'][$interface]['ipaddrv6']) {
|
150
|
case "6to4":
|
151
|
interface_6to4_configure($interface, $config['interfaces'][$interface]);
|
152
|
break;
|
153
|
case "6rd":
|
154
|
interface_6rd_configure($interface, $config['interfaces'][$interface]);
|
155
|
break;
|
156
|
case "slaac":
|
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 (((($config['interfaces'][$interface]['ipaddrv6'] == 'dhcp6') &&
|
161
|
isset($config['interfaces'][$interface]['dhcp6usev4iface'])) ||
|
162
|
(($config['interfaces'][$interface]['ipaddrv6'] == 'slaac') &&
|
163
|
isset($config['interfaces'][$interface]['slaacusev4iface']))) &&
|
164
|
!interface_isppp_type($interface)) {
|
165
|
interface_dhcpv6_configure($interface, $config['interfaces'][$interface]);
|
166
|
}
|
167
|
break;
|
168
|
default:
|
169
|
break;
|
170
|
}
|
171
|
|
172
|
/* Check Gif tunnels */
|
173
|
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
|
}
|
179
|
}
|
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
|
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
|
|
195
|
if (platform_booting() && !in_array(substr($interface_real, 0, 3), array("ppp", "ppt", "l2t"))) {
|
196
|
// unlike dhcp interfaces which wait until they get an ip, a ppp connection lets the boot continue while
|
197
|
// trying to acquire a ip address so to avoid a race condition where it would be possible that the default
|
198
|
// 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
|
// 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
|
/*
|
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
|
if (!is_ipaddr($oldip) || ($curwanip != $oldip) || file_exists("{$g['tmp_path']}/{$interface}_upstart4") ||
|
215
|
(!is_ipaddrv4($config['interfaces'][$interface]['ipaddr']) && ($config['interfaces'][$interface]['ipaddr'] != 'dhcp'))) {
|
216
|
/*
|
217
|
* Some services (e.g. dyndns, see ticket #4066) depend on
|
218
|
* filter_configure() to be called before, otherwise pass out
|
219
|
* route-to rules have the old ip set in 'from' and connections
|
220
|
* do not go through the correct link
|
221
|
*/
|
222
|
filter_configure_sync();
|
223
|
|
224
|
/* reconfigure our gateway monitor, dpinger results need to be
|
225
|
* available when configuring the default gateway */
|
226
|
setup_gateways_monitor();
|
227
|
|
228
|
/* reconfigure static routes (kernel may have deleted them) */
|
229
|
system_routing_configure($interface);
|
230
|
|
231
|
/* 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
|
pfSense_kill_states(utf8_encode($oldip));
|
236
|
filter_flush_state_table();
|
237
|
} else {
|
238
|
log_error("IP Address has changed, killing states on former IP Address $oldip.");
|
239
|
pfSense_kill_states(utf8_encode($oldip));
|
240
|
}
|
241
|
}
|
242
|
|
243
|
/* reload unbound */
|
244
|
services_unbound_configure(true, $interface);
|
245
|
|
246
|
if (is_ipaddr($curwanip)) {
|
247
|
@file_put_contents("{$g['vardb_path']}/{$interface}_cacheip", $curwanip);
|
248
|
}
|
249
|
|
250
|
/* perform RFC 2136 DNS update */
|
251
|
services_dnsupdate_process($interface);
|
252
|
|
253
|
/* signal dyndns update */
|
254
|
services_dyndns_configure($interface);
|
255
|
|
256
|
/* reconfigure IPsec tunnels */
|
257
|
ipsec_force_reload($interface, 'inet');
|
258
|
|
259
|
/* start OpenVPN server & clients */
|
260
|
if (substr($interface_real, 0, 4) != "ovpn") {
|
261
|
openvpn_resync_all($interface, 'inet');
|
262
|
}
|
263
|
|
264
|
/* reload graphing functions */
|
265
|
enable_rrd_graphing();
|
266
|
|
267
|
/* reload igmpproxy */
|
268
|
services_igmpproxy_configure($interface);
|
269
|
|
270
|
/* restart snmp */
|
271
|
services_snmpd_configure($interface);
|
272
|
|
273
|
/* restart L2TP VPN */
|
274
|
vpn_l2tp_configure($interface);
|
275
|
|
276
|
restart_packages();
|
277
|
|
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
|
}
|
283
|
|
284
|
/* 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
|
?>
|