Project

General

Profile

Download (7.84 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -f
2
<?php
3
/*
4
	rc.newwanip
5
	Copyright (C) 2006 Scott Ullrich (sullrich@gmail.com)
6
	part of pfSense (https://www.pfsense.org)
7

    
8
	Originally part of m0n0wall (http://m0n0.ch)
9
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11

    
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14

    
15
	1. Redistributions of source code must retain the above copyright notice,
16
	this list of conditions and the following disclaimer.
17

    
18
	2. Redistributions in binary form must reproduce the above copyright
19
	notice, this list of conditions and the following disclaimer in the
20
	documentation and/or other materials provided with the distribution.
21

    
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33

    
34
/* parse the configuration and include all functions used below */
35
require_once("globals.inc");
36
require_once("config.inc");
37
require_once("functions.inc");
38
require_once("filter.inc");
39
require_once("shaper.inc");
40
require_once("ipsec.inc");
41
require_once("vpn.inc");
42
require_once("openvpn.inc");
43
require_once("IPv6.inc");
44
require_once("rrd.inc");
45

    
46
function restart_packages() {
47
	global $oldip, $curwanip, $g;
48

    
49
	/* restart packages */
50
	system_ntp_configure(false);
51
	mwexec_bg("/usr/local/sbin/ntpdate_sync_once.sh", true);
52
	log_error("{$g['product_name']} package system has detected an IP change or dynamic WAN reconnection - $oldip ->  $curwanip - Restarting packages.");
53
	send_event("service reload packages");
54
}
55

    
56
/* Interface IP address has changed */
57
if (isset($_GET['interface'])) {
58
	$argument = $_GET['interface'];
59
} else {
60
	$argument = str_replace("\n", "", $argv[1]);
61
}
62

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

    
65
if (empty($argument)) {
66
	$interface = "wan";
67
	$interface_real = get_real_interface();
68
} else {
69
	$interface = convert_real_interface_to_friendly_interface_name($argument);
70
	$interface_real = $argument;
71
}
72

    
73
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
74

    
75
/* If the interface is configured and not enabled, bail. We do not need to change settings for disabled interfaces. #3313 */
76
if (is_array($config['interfaces'][$interface]) && !isset($config['interfaces'][$interface]['enable'])) {
77
	log_error("Interface is disabled, nothing to do.");
78
	return;
79
}
80

    
81
if (empty($argument)) {
82
	$curwanip = get_interface_ip();
83
} else {
84
	$curwanip = find_interface_ip($interface_real, true);
85
	if ($curwanip == "") {
86
		$curwanip = get_interface_ip($interface);
87
	}
88
}
89

    
90
if (!platform_booting()) {
91
	log_error("rc.newwanip: on (IP address: {$curwanip}) (interface: {$interface_descr}[{$interface}]) (real interface: {$interface_real}).");
92
}
93

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

    
108
/* XXX: This really possible? */
109
if (empty($interface)) {
110
	if (platform_booting()) {
111
		return;
112
	}
113
	filter_configure();
114
	restart_packages();
115
	return;
116
}
117

    
118
$oldip = "0.0.0.0";
119
if (file_exists("{$g['vardb_path']}/{$interface}_cacheip")) {
120
	$oldip = file_get_contents("{$g['vardb_path']}/{$interface}_cacheip");
121
}
122

    
123
/* regenerate resolv.conf if DNS overrides are allowed */
124
if (!platform_booting()) {
125
	system_resolvconf_generate(true);
126
}
127

    
128
/* write the current interface IP to file */
129
if (is_ipaddr($curwanip)) {
130
	@file_put_contents("{$g['vardb_path']}/{$interface}_ip", $curwanip);
131
}
132

    
133
link_interface_to_vips($interface, "update");
134

    
135
unset($gre);
136
$gre = link_interface_to_gre($interface);
137
if (!empty($gre)) {
138
	array_walk($gre, 'interface_gre_configure');
139
}
140

    
141
unset($gif);
142
$gif = link_interface_to_gif($interface);
143
if (!empty($gif)) {
144
	array_walk($gif, 'interface_gif_configure');
145
}
146

    
147
$grouptmp = link_interface_to_group($interface);
148
if (!empty($grouptmp)) {
149
	array_walk($grouptmp, 'interface_group_add_member');
150
}
151

    
152
unset($bridgetmp);
153
$bridgetmp = link_interface_to_bridge($interface);
154
if (!empty($bridgetmp)) {
155
	interface_bridge_add_member($bridgetmp, $interface_real);
156
}
157

    
158
// Do not process while booting
159
if (platform_booting()) {
160
	return;
161
}
162

    
163
/* make new hosts file */
164
system_hosts_generate();
165

    
166
/* check tunnelled IPv6 interface tracking */
167
switch ($config['interfaces'][$interface]['ipaddrv6']) {
168
	case "6to4":
169
		interface_6to4_configure($interface, $config['interfaces'][$interface]);
170
		break;
171
	case "6rd":
172
		interface_6rd_configure($interface, $config['interfaces'][$interface]);
173
		break;
174
	case "dhcp6":
175
		if (isset($config['interfaces'][$interface]['dhcp6usev4iface'])) {
176
			interface_dhcpv6_configure($interface, $config['interfaces'][$interface]);
177
		}
178
		break;
179
}
180

    
181
/* Check Gif tunnels */
182
if (!empty($gif)) {
183
	foreach ($gif as $giftun) {
184
		$confif = convert_real_interface_to_friendly_interface_name($giftun['gifif']);
185
		if (!empty($confif)) {
186
			interface_configure($confif);
187
			system_routing_configure($confif);
188
		}
189
	}
190
}
191
if (!empty($gre)) {
192
	foreach ($gre as $gretun) {
193
		$confif = convert_real_interface_to_friendly_interface_name($gretun['greif']);
194
		if (!empty($confif)) {
195
			interface_configure($confif);
196
			system_routing_configure($confif);
197
		}
198
	}
199
}
200

    
201
/*
202
 * We need to force sync VPNs on such even when the IP is the same for dynamic interfaces.
203
 * Even with the same IP the VPN software is unhappy with the IP disappearing, and we
204
 * could be failing back in which case we need to switch IPs back anyhow.
205
 */
206
if (!is_ipaddr($oldip) || $curwanip != $oldip || !is_ipaddrv4($config['interfaces'][$interface]['ipaddr'])) {
207
	/* IP changed, kill states accordingly */
208
	if ($curwanip != $oldip) {
209
		log_error("IP has changed, killing states on former IP $oldip.");
210
		pfSense_kill_states($oldip);
211
		if (isset($config['system']['ip_change_kill_states'])) {
212
			/* hidden config option to wipe all states if needed */
213
			log_error("Killing all states post-IP change.");
214
			filter_flush_state_table();
215
		}
216
	}
217

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

    
226
	/* reconfigure static routes (kernel may have deleted them) */
227
	system_routing_configure($interface);
228

    
229
	/* reconfigure our gateway monitor */
230
	setup_gateways_monitor();
231

    
232
	/* reload unbound */
233
	services_unbound_configure();
234

    
235
	if (is_ipaddr($curwanip)) {
236
		@file_put_contents("{$g['vardb_path']}/{$interface}_cacheip", $curwanip);
237
	}
238

    
239
	/* perform RFC 2136 DNS update */
240
	services_dnsupdate_process($interface);
241

    
242
	/* signal dyndns update */
243
	services_dyndns_configure($interface);
244

    
245
	/* reconfigure IPsec tunnels */
246
	vpn_ipsec_force_reload($interface);
247

    
248
	/* start OpenVPN server & clients */
249
	if (substr($interface_real, 0, 4) != "ovpn") {
250
		openvpn_resync_all($interface);
251
	}
252

    
253
	/* reload graphing functions */
254
	enable_rrd_graphing();
255

    
256
	/* reload igmpproxy */
257
	services_igmpproxy_configure();
258

    
259
	/* restart snmp */
260
	services_snmpd_configure();
261

    
262
	restart_packages();
263
} else {
264
	/* signal filter reload */
265
	filter_configure();
266
}
267

    
268
?>
(76-76/105)