Project

General

Profile

Download (7.76 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
	log_error("{$g['product_name']} package system has detected an IP change or dynamic WAN reconnection - $oldip ->  $curwanip - Restarting packages.");
51
	send_event("service reload packages");
52
}
53

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

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

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

    
71
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
72

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

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

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

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

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

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

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

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

    
131
link_interface_to_vips($interface, "update");
132

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

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

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

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

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

    
161
/* make new hosts file */
162
system_hosts_generate();
163

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

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

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

    
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 static routes (kernel may have deleted them) */
225
	system_routing_configure($interface);
226

    
227
	/* reconfigure our gateway monitor */
228
	setup_gateways_monitor();
229

    
230
	/* reload unbound */
231
	services_unbound_configure();
232

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

    
237
	/* perform RFC 2136 DNS update */
238
	services_dnsupdate_process($interface);
239

    
240
	/* signal dyndns update */
241
	services_dyndns_configure($interface);
242

    
243
	/* reconfigure IPsec tunnels */
244
	vpn_ipsec_force_reload($interface);
245

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

    
251
	/* reload graphing functions */
252
	enable_rrd_graphing();
253

    
254
	/* reload igmpproxy */
255
	services_igmpproxy_configure();
256

    
257
	/* restart snmp */
258
	services_snmpd_configure();
259

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

    
266
?>
(73-73/102)