Project

General

Profile

Download (7.76 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -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
log_error("rc.newwanip: Info: starting on {$argument}.");
63

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

    
72
$interface_descr = convert_friendly_interface_to_friendly_descr($interface);
73

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

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

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

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

    
118
/* regenerate resolv.conf if DNS overrides are allowed */
119
if (!platform_booting())
120
	system_resolvconf_generate(true);
121

    
122
/* write the current interface IP to file */
123
if (is_ipaddr($curwanip))
124
	@file_put_contents("{$g['vardb_path']}/{$interface}_ip", $curwanip);
125

    
126
link_interface_to_vips($interface, "update");
127

    
128
unset($gre);
129
$gre = link_interface_to_gre($interface);
130
if (!empty($gre))
131
	array_walk($gre, 'interface_gre_configure');
132
unset($gif);
133
$gif = link_interface_to_gif($interface);
134
if (!empty($gif))
135
	array_walk($gif, 'interface_gif_configure');
136

    
137
$grouptmp = link_interface_to_group($interface);
138
if (!empty($grouptmp))
139
	array_walk($grouptmp, 'interface_group_add_member');
140

    
141
unset($bridgetmp);
142
$bridgetmp = link_interface_to_bridge($interface);
143
if (!empty($bridgetmp))
144
	interface_bridge_add_member($bridgetmp, $interface_real);
145

    
146
// Do not process while booting
147
if (platform_booting())
148
	return;
149

    
150
/* make new hosts file */
151
system_hosts_generate();
152

    
153
/* check tunneled IPv6 interface tracking */
154
switch($config['interfaces'][$interface]['ipaddrv6']) {
155
	case "6to4":
156
		interface_6to4_configure($interface, $config['interfaces'][$interface]);
157
		break;
158
	case "6rd":
159
		interface_6rd_configure($interface, $config['interfaces'][$interface]);
160
		break;
161
	case "dhcp6":
162
		if (isset($config['interfaces'][$interface]['dhcp6usev4iface']))
163
			interface_dhcpv6_configure($interface, $config['interfaces'][$interface]);
164
		break;
165
}
166

    
167
/* Check Gif tunnels */
168
if (!empty($gif)) {
169
	foreach($gif as $giftun) {
170
		$confif = convert_real_interface_to_friendly_interface_name($giftun['gifif']);
171
		if (!empty($confif)) {
172
			interface_configure($confif);
173
			system_routing_configure($confif);
174
		}
175
	}
176
}
177
if (!empty($gre)) {
178
	foreach($gre as $gretun) {
179
		$confif = convert_real_interface_to_friendly_interface_name($gretun['greif']);
180
		if (!empty($confif)) {
181
			interface_configure($confif);
182
			system_routing_configure($confif);
183
		}
184
	}
185
}
186

    
187
/*
188
 * We need to force sync VPNs on such even when the IP is the same for dynamic interfaces.
189
 * Even with the same IP the VPN software is unhappy with the IP disappearing, and we
190
 * could be failing back in which case we need to switch IPs back anyhow.
191
 */
192
if (!is_ipaddr($oldip) || $curwanip != $oldip || !is_ipaddrv4($config['interfaces'][$interface]['ipaddr'])) {
193
	/* IP changed, kill states accordingly */
194
	if ($curwanip != $oldip) {
195
		log_error("IP has changed, killing states on former IP $oldip.");
196
		pfSense_kill_states($oldip);
197
		if (isset($config['system']['ip_change_kill_states'])) {
198
			/* hidden config option to wipe all states if needed */
199
			log_error("Killing all states post-IP change.");
200
			filter_flush_state_table();
201
		}
202
	}
203
	
204
	/*
205
	 * Some services (e.g. dyndns, see ticket #4066) depends of
206
	 * filter_configure() to be called before, otherwise pass out
207
	 * route-to rules have the old ip set in 'from' and connection
208
	 * do not go through correct link
209
	 */
210
	filter_configure_sync();
211

    
212
	/* reconfigure static routes (kernel may have deleted them) */
213
	system_routing_configure($interface);
214

    
215
	/* reconfigure our gateway monitor */
216
	setup_gateways_monitor();
217
	
218
	/* reload unbound */
219
	services_unbound_configure();
220

    
221
	if (is_ipaddr($curwanip))
222
		@file_put_contents("{$g['vardb_path']}/{$interface}_cacheip", $curwanip);
223

    
224
	/* perform RFC 2136 DNS update */
225
	services_dnsupdate_process($interface);
226

    
227
	/* signal dyndns update */
228
	services_dyndns_configure($interface);
229

    
230
	/* reconfigure IPsec tunnels */
231
	vpn_ipsec_force_reload($interface);
232

    
233
	/* start OpenVPN server & clients */
234
	if (substr($interface_real, 0, 4) != "ovpn")
235
		openvpn_resync_all($interface);
236

    
237
	/* reload graphing functions */
238
	enable_rrd_graphing();
239

    
240
	/* reload igmpproxy */
241
	services_igmpproxy_configure();
242

    
243
	/* restart snmp */
244
	services_snmpd_configure();
245

    
246
	restart_packages();
247
} else {
248
	/* signal filter reload */
249
	filter_configure();
250
}
251

    
252
?>
(74-74/103)