Project

General

Profile

Download (44.2 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	interfaces.inc
5 0e16b9ca Scott Ullrich
	Copyright (C) 2004-2006 Scott Ullrich
6 ac3f8318 Espen Johansen
	All rights reserved.
7
8
	function interfaces_wireless_configure is
9
	Copyright (C) 2005 Espen Johansen
10 cfc707f7 Scott Ullrich
	All rights reserved.
11
12
	originally part of m0n0wall (http://m0n0.ch/wall)
13 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
	All rights reserved.
15 cfc707f7 Scott Ullrich
16 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
17
	modification, are permitted provided that the following conditions are met:
18 cfc707f7 Scott Ullrich
19 ac3f8318 Espen Johansen
	1. Redistributions of source code must retain the above copyright notices,
20 5b237745 Scott Ullrich
	   this list of conditions and the following disclaimer.
21 cfc707f7 Scott Ullrich
22 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
23 ac3f8318 Espen Johansen
	   notices, this list of conditions and the following disclaimer in the
24 5b237745 Scott Ullrich
	   documentation and/or other materials provided with the distribution.
25 cfc707f7 Scott Ullrich
26 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
	POSSIBILITY OF SUCH DAMAGE.
36
*/
37
38
/* include all configuration functions */
39
require_once("functions.inc");
40
41
function interfaces_loopback_configure() {
42
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
43 cfc707f7 Scott Ullrich
44 5b237745 Scott Ullrich
	return 0;
45
}
46
47
function interfaces_vlan_configure() {
48 669e1adb Bill Marquette
	global $config;
49 cfc707f7 Scott Ullrich
50 5b237745 Scott Ullrich
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
51 cfc707f7 Scott Ullrich
52 5b237745 Scott Ullrich
		/* devices with native VLAN support */
53
		$vlan_native_supp = explode(" ", "bge em gx nge ti txp");
54 cfc707f7 Scott Ullrich
55 5b237745 Scott Ullrich
		/* devices with long frame support */
56
		$vlan_long_supp = explode(" ", "dc fxp sis ste tl tx xl");
57 cfc707f7 Scott Ullrich
58 5b237745 Scott Ullrich
		$i = 0;
59 cfc707f7 Scott Ullrich
60 5b237745 Scott Ullrich
		foreach ($config['vlans']['vlan'] as $vlan) {
61 cfc707f7 Scott Ullrich
62
			$cmd = "/sbin/ifconfig vlan{$i} create vlan " .
63
				escapeshellarg($vlan['tag']) . " vlandev " .
64 5b237745 Scott Ullrich
				escapeshellarg($vlan['if']);
65 cfc707f7 Scott Ullrich
66 5b237745 Scott Ullrich
			/* get driver name */
67
			for ($j = 0; $j < strlen($vlan['if']); $j++) {
68
				if ($vlan['if'][$j] >= '0' && $vlan['if'][$j] <= '9')
69
					break;
70
			}
71
			$drvname = substr($vlan['if'], 0, $j);
72 cfc707f7 Scott Ullrich
73 5b237745 Scott Ullrich
			if (in_array($drvname, $vlan_native_supp))
74
				$cmd .= " link0";
75
			else if (in_array($drvname, $vlan_long_supp))
76
				$cmd .= " mtu 1500";
77 cfc707f7 Scott Ullrich
78 5b237745 Scott Ullrich
			mwexec($cmd);
79 cfc707f7 Scott Ullrich
80 5b237745 Scott Ullrich
			/* make sure the parent interface is up */
81
			mwexec("/sbin/ifconfig " . escapeshellarg($vlan['if']) . " up");
82 cfc707f7 Scott Ullrich
83 5b237745 Scott Ullrich
			$i++;
84
		}
85
	}
86 cfc707f7 Scott Ullrich
87 5b237745 Scott Ullrich
	return 0;
88
}
89
90
function interfaces_lan_configure() {
91 6ee83b6e Scott Ullrich
	global $config, $g;
92
93
	$bridges_total = get_next_available_bridge_interface();
94 cfc707f7 Scott Ullrich
95 5b237745 Scott Ullrich
	$lancfg = $config['interfaces']['lan'];
96 cfc707f7 Scott Ullrich
97 7cc7e84d Scott Ullrich
	/* if user has removed ip address, clear it*/
98
	if($lancfg['ipaddr'] == "")
99
		mwexec("/sbin/ifconfig {$lancfg['if']} delete");
100
101 5b237745 Scott Ullrich
	/* wireless configuration? */
102
	if (is_array($lancfg['wireless']))
103
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
104 cfc707f7 Scott Ullrich
105 5b237745 Scott Ullrich
	/* MAC spoofing? */
106 f36d4bd2 Scott Ullrich
	if ($lancfg['spoofmac']) {
107 cfc707f7 Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
108 5b237745 Scott Ullrich
			" link " . escapeshellarg($lancfg['spoofmac']));
109 f36d4bd2 Scott Ullrich
	} else {
110
		$mac = get_interface_mac_address($lancfg['if']);
111
		if($mac == "ff:ff:ff:ff:ff:ff") {
112
			/*   this is not a valid mac address.  generate a
113
			 *   temporary mac address so the machine can get online.
114
			 */
115 9315ef83 Scott Ullrich
			echo "Generating new MAC address.";
116 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
117
			mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
118
				" link " . escapeshellarg($random_mac));
119
			$lancfg['spoofmac'] = $random_mac;
120
			write_config();
121 da86dd6f Scott Ullrich
			file_notice("MAC Address altered", "The INVALID MAC address (ff:ff:ff:ff:ff:ff) on interface {$lancfg['if']} has been automatically replaced with {$random_mac}", "Interfaces");
122 f36d4bd2 Scott Ullrich
		}
123
	}	
124 a4d9f914 Scott Ullrich
125
	/* bridged? */
126 b1b55ed2 Scott Ullrich
	
127 6065fd77 Scott Ullrich
	if ($lancfg['bridge']) {
128 a4d9f914 Scott Ullrich
		/* use open/netBSD style bridge */
129
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
130
		
131 96812f11 Scott Ullrich
		/* force all bridged interfaces to use same mtu */
132 51535829 Scott Ullrich
		$mtu = get_interface_mtu($config['interfaces'][$lancfg['bridge']]['if']);
133 96812f11 Scott Ullrich
		mwexec("/sbin/ifconfig {$lancfg['if']} mtu {$mtu}");
134
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}");
135 51535829 Scott Ullrich
136
		/* assign items to a bridge */
137 a591b9cb Scott Ullrich
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}");
138 df792110 Scott Ullrich
		
139 e7aa7560 Scott Ullrich
		if(!is_interface_wireless($lancfg['if']) and
140
		   !is_interface_wireless($config['interfaces'][$lancfg['bridge']]['if'])) 
141 3789e4df Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$lancfg['bridge']]['if']} stp {$lancfg['if']}");
142 51535829 Scott Ullrich
143
		/* log commands run for debugging in /tmp/ */
144 6065fd77 Scott Ullrich
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
145 51535829 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig {$lancfg['if']} mtu {$mtu}\n");
146
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}\n");
147 a4d9f914 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
148 b3a66d90 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}\n");
149 01ae6c40 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}\n");
150 a4d9f914 Scott Ullrich
		fclose($fd);
151 f70c887f Scott Ullrich
		
152
		/* bring up interfaces */
153 77712c1e Scott Ullrich
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
154
		usleep(100);
155 f70c887f Scott Ullrich
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} up");
156 77712c1e Scott Ullrich
		usleep(5);
157 f70c887f Scott Ullrich
		mwexec("/sbin/ifconfig {$lancfg['if']} up");
158 77712c1e Scott Ullrich
		usleep(5);
159 43715f8b Scott Ullrich
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
160 c60e4f51 Scott Ullrich
		
161
		$bridges_total++;
162 a4d9f914 Scott Ullrich
	}
163 f36d4bd2 Scott Ullrich
	
164 5b237745 Scott Ullrich
	/* media */
165
	if ($lancfg['media'] || $lancfg['mediaopt']) {
166
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
167
		if ($lancfg['media'])
168
			$cmd .= " media " . escapeshellarg($lancfg['media']);
169
		if ($lancfg['mediaopt'])
170
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
171
		mwexec($cmd);
172
	}
173 cfc707f7 Scott Ullrich
174
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
175 5b237745 Scott Ullrich
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
176 cfc707f7 Scott Ullrich
177 5b237745 Scott Ullrich
	if (!$g['booting']) {
178
		/* make new hosts file */
179
		system_hosts_generate();
180 cfc707f7 Scott Ullrich
181 5b237745 Scott Ullrich
		/* reconfigure static routes (kernel may have deleted them) */
182
		system_routing_configure();
183 cfc707f7 Scott Ullrich
184 e239df5a Scott Ullrich
		/* set the reload filter dity flag */
185 f229e20f Scott Ullrich
		touch("{$g['tmp_path']}/filter_dirty");
186 cfc707f7 Scott Ullrich
187 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
188
		vpn_ipsec_configure();
189 cfc707f7 Scott Ullrich
190 5b237745 Scott Ullrich
		/* reload dhcpd (gateway may have changed) */
191
		services_dhcpd_configure();
192 cfc707f7 Scott Ullrich
193 5b237745 Scott Ullrich
		/* reload dnsmasq */
194
		services_dnsmasq_configure();
195 cfc707f7 Scott Ullrich
196 6a669fb0 Scott Ullrich
		/* reload captive portal */
197
		captiveportal_configure();
198 cfc707f7 Scott Ullrich
199 5b237745 Scott Ullrich
	}
200 cfc707f7 Scott Ullrich
201 5b237745 Scott Ullrich
	return 0;
202
}
203
204
function interfaces_optional_configure() {
205
	global $config, $g;
206
	global $bridgeconfig;
207 cfc707f7 Scott Ullrich
208 5b237745 Scott Ullrich
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
209
		interfaces_optional_configure_if($i);
210
	}
211 cfc707f7 Scott Ullrich
212 5b237745 Scott Ullrich
	if (!$g['booting']) {
213
		/* reconfigure static routes (kernel may have deleted them) */
214
		system_routing_configure();
215 cfc707f7 Scott Ullrich
216 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
217
		vpn_ipsec_configure();
218 cfc707f7 Scott Ullrich
219 5b237745 Scott Ullrich
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
220
		services_dhcpd_configure();
221 cfc707f7 Scott Ullrich
222 5b237745 Scott Ullrich
		/* restart dnsmasq */
223
		services_dnsmasq_configure();
224 4d18de6a Scott Ullrich
225 c597d50f Scott Ullrich
		/* reload captive portal */
226
		captiveportal_configure();
227
228 4d18de6a Scott Ullrich
		/* set the reload filter dity flag */
229
		touch("{$g['tmp_path']}/filter_dirty");				
230 5b237745 Scott Ullrich
	}
231 cfc707f7 Scott Ullrich
232 5b237745 Scott Ullrich
	return 0;
233
}
234
235
function interfaces_optional_configure_if($opti) {
236
	global $config, $g;
237 6008210b Scott Ullrich
	global $bridgeconfig, $debugging;
238 6ee83b6e Scott Ullrich
239
	$bridges_total = get_next_available_bridge_interface();
240 cfc707f7 Scott Ullrich
241 5b237745 Scott Ullrich
	$optcfg = $config['interfaces']['opt' . $opti];
242 cfc707f7 Scott Ullrich
243 5b237745 Scott Ullrich
	if ($g['booting']) {
244
		$optdescr = "";
245
		if ($optcfg['descr'])
246
			$optdescr = " ({$optcfg['descr']})";
247 5c6d0f65 Colin Smith
		print "\tOPT{$opti}{$optdescr}... ";
248 5b237745 Scott Ullrich
	}
249 cfc707f7 Scott Ullrich
250 5b237745 Scott Ullrich
	if (isset($optcfg['enable'])) {
251
		/* wireless configuration? */
252
		if (is_array($optcfg['wireless']))
253
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
254 cfc707f7 Scott Ullrich
255 5b237745 Scott Ullrich
		/* MAC spoofing? */
256 f36d4bd2 Scott Ullrich
		if ($optcfg['spoofmac']) {
257 cfc707f7 Scott Ullrich
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
258 5b237745 Scott Ullrich
				" link " . escapeshellarg($optcfg['spoofmac']));
259 0ed77c51 Scott Ullrich
		} else {
260
			$mac = get_interface_mac_address($optcfg['if']);
261
			if($mac == "ff:ff:ff:ff:ff:ff") {
262
				/*   this is not a valid mac address.  generate a
263
				 *   temporary mac address so the machine can get online.
264
				 */
265 9315ef83 Scott Ullrich
				echo "Generating new MAC address.";
266 0ed77c51 Scott Ullrich
				$random_mac = generate_random_mac_address();
267
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
268
					" link " . escapeshellarg($random_mac));
269
				$optcfg['spoofmac'] = $random_mac;
270
				write_config();
271
				file_notice("MAC Address altered", "The INVALID MAC address (ff:ff:ff:ff:ff:ff) on interface {$optcfg['if']} has been automatically replaced with {$random_mac}", "Interfaces");
272
			}
273 f36d4bd2 Scott Ullrich
		}
274 cfc707f7 Scott Ullrich
275 5b237745 Scott Ullrich
		/* media */
276
		if ($optcfg['media'] || $optcfg['mediaopt']) {
277
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
278
			if ($optcfg['media'])
279
				$cmd .= " media " . escapeshellarg($optcfg['media']);
280
			if ($optcfg['mediaopt'])
281
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
282
			mwexec($cmd);
283
		}
284 cfc707f7 Scott Ullrich
285 5b237745 Scott Ullrich
		/* bridged? */
286
		if ($optcfg['bridge']) {
287 5a66117a Scott Ullrich
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
288 38226b19 Scott Ullrich
                        /* use open/netBSD style bridge */
289 5a66117a Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
290 96812f11 Scott Ullrich
			
291
			/* force all bridged interfaces to use same mtu */
292 51535829 Scott Ullrich
			$mtu = get_interface_mtu($config['interfaces'][$optcfg['bridge']]['if']);
293 96812f11 Scott Ullrich
			mwexec("/sbin/ifconfig {$optcfg['if']} mtu {$mtu}");
294
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}");			
295 51535829 Scott Ullrich
296
			/* assign items to a bridge */
297 a591b9cb Scott Ullrich
                        mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']}");
298 df792110 Scott Ullrich
299 e7aa7560 Scott Ullrich
			if(!is_interface_wireless($optcfg['if']) and
300
			   !is_interface_wireless($config['interfaces'][$optcfg['bridge']]['if'])) 
301 3789e4df Scott Ullrich
				mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$optcfg['bridge']]['if']} stp {$optcfg['if']}");
302 51535829 Scott Ullrich
303
			/* log commands run for debugging in /tmp/ */
304 bc1746b5 Scott Ullrich
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
305 51535829 Scott Ullrich
			fwrite($fd, "/sbin/ifconfig {$optcfg['if']} mtu {$mtu}\n");
306
			fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}\n");
307 bc1746b5 Scott Ullrich
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
308
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up\n");
309 01ae6c40 Scott Ullrich
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} stp {$config['interfaces'][$optcfg['bridge']]['if']}\n");
310 bc1746b5 Scott Ullrich
			fclose($fd);
311 f70c887f Scott Ullrich
			
312
			/* bring up interfaces */
313 fafdfe28 Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} down");
314 77712c1e Scott Ullrich
			usleep(100);
315
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} up");
316
			usleep(5);
317 82a7eb4a Scott Ullrich
			mwexec("/sbin/ifconfig {$optcfg['if']} up");
318 77712c1e Scott Ullrich
			usleep(5);
319 6651e3ec Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} up");
320 f70c887f Scott Ullrich
			
321 c60e4f51 Scott Ullrich
			$bridges_total++;
322
			
323 5b237745 Scott Ullrich
		} else {
324 0311dbd5 Scott Ullrich
			/* if user has selected DHCP type then act accordingly */
325 d3ce564b Scott Ullrich
			if($optcfg['ipaddr'] == "dhcp") {
326 1223f922 Scott Ullrich
				interfaces_opt_dhcp_configure("opt{$opti}");
327 0311dbd5 Scott Ullrich
			} else {			
328
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
329 77317f2b Colin Smith
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
330 0311dbd5 Scott Ullrich
			}
331 5b237745 Scott Ullrich
		}
332
	} else {
333 5a66117a Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
334 5b237745 Scott Ullrich
	}
335
	return 0;
336
}
337
338 9f6b1429 Scott Ullrich
function interfaces_carp_configure() {
339 6008210b Scott Ullrich
	global $g, $config, $debugging;
340 2b9747b9 Scott Ullrich
	$balanacing = "";
341
	$pfsyncinterface = "";
342
	$pfsyncenabled = "";
343 b932ef16 Scott Ullrich
	if(isset($config['system']['developerspew'])) {
344
		$mt = microtime();
345
		echo "interfaces_carp_configure() being called $mt\n";
346
	}
347 6008210b Scott Ullrich
	$carp_instances_counter = 0;
348
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
349 467c2c89 Scott Ullrich
	/* destroy previous interfaces */
350
	for($x=0; $x<$total_carp_interfaces_defined; $x++) 
351
		mwexec("/sbin/ifconfig carp{$x} delete");	
352 b932ef16 Scott Ullrich
	if ($g['booting']) {
353 7d0f4544 Scott Ullrich
		echo "Configuring CARP interfaces...";
354
		mute_kernel_msgs();
355 a5250ebc Scott Ullrich
	}
356 b932ef16 Scott Ullrich
	/* suck in configuration items */
357
	if($config['installedpackages']['carpsettings']['config']) {
358
		foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
359
			$pfsyncenabled = $carp['pfsyncenabled'];
360
			$balanacing = $carp['balancing'];
361
			$pfsyncinterface = $carp['pfsyncinterface'];
362 9f6b1429 Scott Ullrich
		}
363 b932ef16 Scott Ullrich
	} else {
364
		unset($pfsyncinterface);
365
		unset($balanacing);
366
		unset($pfsyncenabled);
367 6008210b Scott Ullrich
	}
368 b932ef16 Scott Ullrich
	if($balanacing) {
369
		mwexec("/sbin/sysctl net.inet.carp.arpbalance=1");
370
		mwexec("/sbin/sysctl net.inet.carp.preempt=0");
371
	} else {
372
		mwexec("/sbin/sysctl net.inet.carp.preempt=1");
373
	}
374
	$carp_sync_int = convert_friendly_interface_to_real_interface_name($pfsyncinterface);
375
	if($g['booting']) {
376
		/*    install rules to alllow pfsync to sync up during boot
377
		 *    carp interfaces will remain down until the bootup sequence finishes
378
		 */
379
		exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
380
		exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
381
		exec("echo pass out proto { tcp, udp } from any to any port 53 keep state >> /tmp/rules.boot");
382
		exec("/sbin/pfctl -f /tmp/rules.boot");
383
	}			
384
	/* setup pfsync interface */
385 c5d5ffaf Scott Ullrich
	if($carp_sync_int and $pfsyncenabled) 
386 b932ef16 Scott Ullrich
		mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up");
387
	else 
388
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
389 f6189feb Scott Ullrich
	$fd = fopen("/tmp/carp.sh", "w");
390 b932ef16 Scott Ullrich
	if($config['virtualip']['vip']) { 
391
		$viparr = &$config['virtualip']['vip'];
392
		mwexec("/sbin/sysctl net.inet.carp.allow=1");
393
	} else {
394
		$viparr = array();
395
		mwexec("/sbin/sysctl net.inet.carp.allow=0");
396
	}
397 6008210b Scott Ullrich
	foreach ($viparr as $vip) {
398
		if ($vip['mode'] == "carp") {
399 8a573737 Scott Ullrich
			$vip_password = $vip['password'];
400
			$vip_password = str_replace(" ", "", $vip_password);			
401 b932ef16 Scott Ullrich
			/* create the carp interface and setup */
402 6008210b Scott Ullrich
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
403
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
404
			if($vip['password'] != "") 
405 8a573737 Scott Ullrich
				$password = " pass \"" . $vip_password . "\"";
406 6008210b Scott Ullrich
			if($debugging)
407
				echo "Configuring carp{$carp_instances_counter}.\n";
408 f6189feb Scott Ullrich
			fwrite($fd, "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password . "\n");
409 9669a414 Scott Ullrich
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password);
410 94b636a6 Scott Ullrich
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
411 be606e9a Scott Ullrich
			fwrite($fd, "/sbin/ifconfig carp" . $carp_instances_counter . " up\n");
412 b932ef16 Scott Ullrich
			usleep(10);
413 6008210b Scott Ullrich
			$carp_instances_counter++;
414
		}
415 75f2c22c Scott Ullrich
	}
416 f6189feb Scott Ullrich
	fclose($fd);
417 b932ef16 Scott Ullrich
	mwexec("/bin/sh /tmp/carp.sh");
418 3c18fbc9 Scott Ullrich
	if ($g['booting']) {
419 deebaae1 Scott Ullrich
		unmute_kernel_msgs();
420 74dbce1f Scott Ullrich
		echo "done.\n";
421 669e1adb Bill Marquette
	}
422 9f6b1429 Scott Ullrich
}
423
424 04c5bd17 Scott Ullrich
function interfaces_carp_bring_up_final() {
425 6008210b Scott Ullrich
	global $config, $g, $debugging;
426
	if(isset($config['system']['developerspew'])) {
427
		$mt = microtime();
428
		echo "interfaces_carp_bring_up_final() being called $mt\n";
429
	}
430 cb77ff27 Scott Ullrich
	if(!$config['installedpackages']['carpsettings']['config'])
431
		return;
432 04c5bd17 Scott Ullrich
	$viparr = &$config['virtualip']['vip'];
433 6008210b Scott Ullrich
	/* could not locate an array, return */
434
	if(!is_array($viparr)) 
435 5b0272bf Scott Ullrich
		return;
436 6008210b Scott Ullrich
	$carp_instances_counter = 0;
437 25530ad8 Scott Ullrich
	$counter = 0;
438 0c6ee817 Scott Ullrich
	if($g['booting'])
439
		echo "Waiting for final CARP interface bringup...";
440 512fa4d7 Scott Ullrich
	$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
441 44837e6b Scott Ullrich
	if($g['booting']) {
442
		while($supress > 0) {
443
			sleep(2);
444
			$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
445
			if($counter > 15)
446 9123cef8 Scott Ullrich
				$supress = 0;
447 44837e6b Scott Ullrich
			$counter++;
448
			echo ".";
449
		}
450 2d1e01b0 Scott Ullrich
		for($x=0; $x<23; $x++) {
451
			sleep(2);
452
			echo ".";
453
		}
454
		echo " done.\n";
455 44837e6b Scott Ullrich
	}
456 04c5bd17 Scott Ullrich
	foreach ($viparr as $vip) {
457 6008210b Scott Ullrich
		if($debugging)
458
			echo "Upping interface carp{$carp_instances_counter}.\n";
459
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
460
		if($vip['password'] != "") 
461
			$password = " pass " . $vip['password'];
462
		if($debugging)
463
			echo "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password . "\n";
464
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
465 5f2ad497 Scott Ullrich
		sleep(1);
466
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
467 04c5bd17 Scott Ullrich
		$carp_instances_counter++;
468
	}
469 0c6ee817 Scott Ullrich
	if($g['booting'])
470
		echo " done.\n";	
471 04c5bd17 Scott Ullrich
}
472
473 5b237745 Scott Ullrich
function interfaces_wireless_configure($if, $wlcfg) {
474 ac3f8318 Espen Johansen
	global $config, $g;
475 15e67907 Scott Ullrich
	
476 4742e927 Scott Ullrich
	/*    open up a shell script that will be used to output the commands.
477
	 *    since wireless is changing a lot, these series of commands are fragile
478
     *    and will sometimes need to be verified by a operator by executing the command
479
     *    and returning the output of the command to the developers for inspection.  please
480
     *    do not change this routine from a shell script to individul exec commands.  -sullrich
481
	 */
482 490b8b2a Scott Ullrich
	
483 8a958125 Scott Ullrich
	conf_mount_rw();
484 490b8b2a Scott Ullrich
	
485
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
486
	
487 4742e927 Scott Ullrich
	$fd_set = fopen("/tmp/{$if}_setup.sh","w");
488
	fwrite($fd_set, "#!/bin/sh\n");
489 8a958125 Scott Ullrich
	fwrite($fd_set, "# pfSense wireless configuration script.\n\n");
490 4742e927 Scott Ullrich
	
491
	fwrite($fd_set, "# enable shell debugging\n");
492
	fwrite($fd_set, "set -x\n");
493
	
494 2ac908dd Espen Johansen
	/* set values for /path/program */
495
	$hostapd = "/usr/sbin/hostapd";
496
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
497 4742e927 Scott Ullrich
	$ifconfig = "/sbin/ifconfig";
498
	$killall = "/usr/bin/killall";
499 2ac908dd Espen Johansen
500 a59abc65 Scott Ullrich
	/* Set all wireless ifconfig variables (splitt up to get rid of needed checking) */
501 5508cf57 Scott Ullrich
502 ac3f8318 Espen Johansen
	/* Set a/b/g standard */
503 f4c9d138 Scott Ullrich
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
504 5508cf57 Scott Ullrich
505 0856c4ac Scott Ullrich
	/* Set 802.11g protection mode */
506
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
507
508 ac3f8318 Espen Johansen
	/* set wireless channel value */
509 ea9d29fa Scott Ullrich
	if(isset($wlcfg['channel']))
510 f4c9d138 Scott Ullrich
		$channel = "channel " . escapeshellarg($wlcfg['channel']);
511 2ac908dd Espen Johansen
512 f134033e Scott Ullrich
	/* set Distance value */
513 a59abc65 Scott Ullrich
	if($wlcfg['distance']) 
514 f134033e Scott Ullrich
		$distance = escapeshellarg($wlcfg['distance']);
515
516 ac3f8318 Espen Johansen
	/* Set ssid */
517 191a8175 Scott Ullrich
	if($wlcfg['ssid'])  
518
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
519 a59abc65 Scott Ullrich
		
520 ac3f8318 Espen Johansen
	/* Set wireless hostap mode */
521 a59abc65 Scott Ullrich
	if ($wlcfg['mode'] == "hostap")
522 ac3f8318 Espen Johansen
		$hostapmode = "mediaopt hostap";
523
	else
524
		$hostapmode = "-mediaopt hostap";
525
526
	/* Set wireless adhoc mode */
527 a59abc65 Scott Ullrich
	if ($wlcfg['mode'] == "adhoc")
528 ac3f8318 Espen Johansen
		$adhocmode = "mediaopt adhoc";
529
	else
530
		$adhocmode = "-mediaopt adhoc";
531
532
	/* Not neccesary to set BSS mode as this is default if adhoc and/or hostap is NOT set */
533
534
	/* handle hide ssid option */
535
	if(isset($wlcfg['hidessid']['enable']))
536
		$hidessid = "hidessid";
537
	else
538
		$hidessid = "-hidessid";
539
540
	/* handle pureg (802.11g) only option */
541
	if(isset($wlcfg['pureg']['enable']))
542
		$pureg = "mode 11g pureg";
543
	else
544
		$pureg = "-pureg";
545
546
	/* enable apbridge option */
547
	if(isset($wlcfg['apbridge']['enable']))
548
		$apbridge = "apbridge";
549
	else
550
		$apbridge = "-apbridge";
551
552
	/* handle turbo option */
553
	if(isset($wlcfg['turbo']['enable']))
554
		$turbo = "mediaopt turbo";
555
	else
556
		$turbo = "-mediaopt turbo";
557
558
	/* handle txpower setting */
559
	if($wlcfg['txpower'] <> "")
560 f4c9d138 Scott Ullrich
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
561 ac3f8318 Espen Johansen
	
562
	/* handle wme option */
563
	if(isset($wlcfg['wme']['enable']))
564
		$wme = "wme";
565
	else
566
		$wme = "-wme";
567
	
568
	/* set up wep if enabled */
569
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
570
                $wepset .= "authmode shared wepmode on ";
571
                $i = 1;
572
                foreach ($wlcfg['wep']['key'] as $wepkey) {
573
                        $wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
574
                        if (isset($wepkey['txkey'])) {
575
                                $wepset .= "weptxkey {$i} ";
576
                        }
577
                        $i++;
578
                }
579
        } else {
580
                $wepset = "authmode open wepmode off";
581
	}
582
583
	/* generate wpa_supplicant/hostap config if wpa is enabled */
584
585
	switch ($wlcfg['mode']) {
586 b67d192d Scott Ullrich
		case 'bss':
587 ac3f8318 Espen Johansen
			if (isset($wlcfg['wpa']['enable'])) {
588
589
				$wpa .= <<<EOD
590 454756b9 Scott Ullrich
ctrl_interface={$g['varrun_path']}/wpa_supplicant
591 50ad3b7c Scott Ullrich
ctrl_interface_group=0
592
ap_scan=1
593 2ac908dd Espen Johansen
#fast_reauth=1
594 249558a2 Scott Ullrich
network={
595 454756b9 Scott Ullrich
ssid="{$wlcfg['ssid']}"
596
scan_ssid=1
597 2ac908dd Espen Johansen
priority=5
598
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
599 454756b9 Scott Ullrich
psk="{$wlcfg['wpa']['passphrase']}"
600 2ac908dd Espen Johansen
pairwise={$wlcfg['wpa']['wpa_pairwise']}
601
group={$wlcfg['wpa']['wpa_pairwise']}
602 50ad3b7c Scott Ullrich
}
603
EOD;
604
605 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
606 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
607
				fclose($fd);
608 8d27a5fe Espen Johansen
609 4742e927 Scott Ullrich
				fwrite($fd_set, "{$killall} wpa_supplicant\n");
610 ac3f8318 Espen Johansen
			}
611
		break;
612 459d6351 Scott Ullrich
613 ac3f8318 Espen Johansen
		case 'hostap':
614
			if (isset($wlcfg['wpa']['enable'])) {
615
				$wpa .= <<<EOD
616 459d6351 Scott Ullrich
interface={$if}
617
driver=bsd
618
logger_syslog=-1
619
logger_syslog_level=0
620
logger_stdout=-1
621
logger_stdout_level=0
622 2ac908dd Espen Johansen
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
623
ctrl_interface={$g['varrun_path']}/hostapd
624 459d6351 Scott Ullrich
ctrl_interface_group=wheel
625 2ac908dd Espen Johansen
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
626
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
627 b67d192d Scott Ullrich
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
628 459d6351 Scott Ullrich
ssid={$wlcfg['ssid']}
629 2ac908dd Espen Johansen
debug={$wlcfg['wpa']['debug_mode']}
630
auth_algs={$wlcfg['wpa']['auth_algs']}
631
wpa={$wlcfg['wpa']['wpa_mode']}
632
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
633
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
634 ac3f8318 Espen Johansen
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
635
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
636
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
637 2ac908dd Espen Johansen
wpa_passphrase={$wlcfg['wpa']['passphrase']}
638
ieee8021x={$wlcfg['wpa']['ieee8021x']}
639 53dfd34e Espen Johansen
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
640
#rsn_preauth=1
641
#rsn_preauth_interfaces=eth0
642 459d6351 Scott Ullrich
EOD;
643 2ac908dd Espen Johansen
644 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
645 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
646
				fclose($fd);
647 2ac908dd Espen Johansen
648 4742e927 Scott Ullrich
				fwrite($fd_set, "{$killall} hostapd\n");
649 ac3f8318 Espen Johansen
			}
650
		break;
651 5508cf57 Scott Ullrich
652 ac3f8318 Espen Johansen
		case 'adhoc':
653 4742e927 Scott Ullrich
			fwrite($fd_set, "{$killall} hostapd\n");
654
			fwrite($fd_set, "{$killall} wpa_supplicant\n");
655 ac3f8318 Espen Johansen
		break;
656
	}	
657
658 4742e927 Scott Ullrich
	/*
659
	 *    all variables are set, lets start up everything
660
     */
661 da1dab20 Scott Ullrich
	
662 78922914 Scott Ullrich
	/* set ack timers according to users preference (if he/she has any) */
663
	if($distance) {
664 4742e927 Scott Ullrich
		fwrite($fd_set, "# Enable ATH distance settings\n");
665
		fwrite($fd_set, "/sbin/athctrl.sh -i {$if} -d {$distance}\n");
666 78922914 Scott Ullrich
	}
667 4d857dcf Scott Ullrich
	
668
	$standard_no_turbo = str_replace(" Turbo", "", $standard);
669
	
670 4742e927 Scott Ullrich
	$settings = <<<EOD
671
672
{$ifconfig} {$if} {$hostapmode}
673 490b8b2a Scott Ullrich
{$ifconfig} {$if} {$standard_no_turbo}
674 8a958125 Scott Ullrich
{$ifconfig} {$if} {$channel}
675 4742e927 Scott Ullrich
{$ifconfig} {$if} {$turbo}
676 8a958125 Scott Ullrich
{$ifconfig} {$if} {$ssid}
677 4742e927 Scott Ullrich
{$ifconfig} {$if} {$hidessid}
678 8a958125 Scott Ullrich
{$ifconfig} {$if} {$adhocmode}
679
{$ifconfig} {$if} {$protmode}
680 4742e927 Scott Ullrich
{$ifconfig} {$if} {$pureg}
681
{$ifconfig} {$if} {$apbridge}
682
{$ifconfig} {$if} {$wme}
683
{$ifconfig} {$if} {$wepset}
684
{$ifconfig} {$if} {$txpower}
685
{$ifconfig} {$if} up
686
687
EOD;
688
	
689
	/* write out above <<EOD stuff */
690
	fwrite($fd_set, $settings);
691 da1dab20 Scott Ullrich
692 ac3f8318 Espen Johansen
	if (isset($wlcfg['wpa']['enable'])) {
693 4742e927 Scott Ullrich
		if ($wlcfg['mode'] == "bss") 
694
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
695
		if ($wlcfg['mode'] == "hostap") 
696
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
697 ac3f8318 Espen Johansen
	}
698 191a8175 Scott Ullrich
699 4742e927 Scott Ullrich
	fclose($fd_set);
700
701 8a958125 Scott Ullrich
	conf_mount_ro();
702
703 4742e927 Scott Ullrich
	/* execute commands now in shell */
704
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
705 490b8b2a Scott Ullrich
	sleep(1);
706
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
707 191a8175 Scott Ullrich
708 ac3f8318 Espen Johansen
	return 0;
709 cfc707f7 Scott Ullrich
710 5b237745 Scott Ullrich
}
711
712 0311dbd5 Scott Ullrich
function find_dhclient_process($interface) {
713 84cec030 Scott Ullrich
	if(filter_translate_type_to_real_interface($interface) <> "")
714
        	$realinterface = filter_translate_type_to_real_interface($interface);
715 027f9e05 Scott Ullrich
	$pid = `/usr/bin/pgrep -f "dhclient: {$realinterface}(\$| .*)"`;
716 0311dbd5 Scott Ullrich
	return $pid;
717
}
718
719 5b237745 Scott Ullrich
function interfaces_wan_configure() {
720 0831bc86 Scott Ullrich
	global $config, $g, $bridges_total;
721 cfc707f7 Scott Ullrich
722 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
723 cfc707f7 Scott Ullrich
724 5c6d0f65 Colin Smith
	if(!$g['booting']) {
725 c1627786 Scott Ullrich
		mute_kernel_msgs();
726 0311dbd5 Scott Ullrich
727
		/* find dhclient process for wan and kill it */
728
		killbypid(find_dhclient_process("wan"));
729 cfc707f7 Scott Ullrich
730 d7a6517a Scott Ullrich
		/* remove wanup file if it exists */
731
		unlink_if_exists("{$g['tmp_path']}/wanup");
732
733 5b237745 Scott Ullrich
		/* kill PPPoE client (mpd) */
734
		killbypid("{$g['varrun_path']}/mpd.pid");
735 cfc707f7 Scott Ullrich
736 5b237745 Scott Ullrich
		/* wait for processes to die */
737 d7a6517a Scott Ullrich
		sleep(3);
738 cfc707f7 Scott Ullrich
739 0311dbd5 Scott Ullrich
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
740 a23d7248 Scott Ullrich
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
741
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
742
		unlink_if_exists("{$g['vardb_path']}/wanip");
743
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
744 5b237745 Scott Ullrich
	}
745 cfc707f7 Scott Ullrich
746 5b237745 Scott Ullrich
	/* remove all addresses first */
747
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
748
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
749 cfc707f7 Scott Ullrich
750 5b237745 Scott Ullrich
	/* wireless configuration? */
751
	if (is_array($wancfg['wireless']))
752
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
753 cfc707f7 Scott Ullrich
754 f36d4bd2 Scott Ullrich
	if ($wancfg['spoofmac']) {
755 cfc707f7 Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
756 5b237745 Scott Ullrich
			" link " . escapeshellarg($wancfg['spoofmac']));
757 f36d4bd2 Scott Ullrich
	}  else {
758
		$mac = get_interface_mac_address($wancfg['if']);
759
		if($mac == "ff:ff:ff:ff:ff:ff") {
760
			/*   this is not a valid mac address.  generate a
761
			 *   temporary mac address so the machine can get online.
762
			 */
763 9315ef83 Scott Ullrich
			echo "Generating new MAC address.";
764 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
765
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
766
				" link " . escapeshellarg($random_mac));
767
			$wancfg['spoofmac'] = $random_mac;
768
			write_config();
769 da86dd6f Scott Ullrich
			file_notice("MAC Address altered", "The INVALID MAC address (ff:ff:ff:ff:ff:ff) on interface {$wancfg['if']} has been automatically replaced with {$random_mac}", "Interfaces");
770 f36d4bd2 Scott Ullrich
		}
771
	}
772 cfc707f7 Scott Ullrich
773 5b237745 Scott Ullrich
	/* media */
774
	if ($wancfg['media'] || $wancfg['mediaopt']) {
775
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
776
		if ($wancfg['media'])
777
			$cmd .= " media " . escapeshellarg($wancfg['media']);
778
		if ($wancfg['mediaopt'])
779
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
780
		mwexec($cmd);
781
	}
782 cfc707f7 Scott Ullrich
783 5b237745 Scott Ullrich
	switch ($wancfg['ipaddr']) {
784 cfc707f7 Scott Ullrich
785 5b237745 Scott Ullrich
		case 'dhcp':
786
			interfaces_wan_dhcp_configure();
787
			break;
788 cfc707f7 Scott Ullrich
789 5b237745 Scott Ullrich
		case 'pppoe':
790
			interfaces_wan_pppoe_configure();
791
			break;
792 cfc707f7 Scott Ullrich
793 5b237745 Scott Ullrich
		case 'pptp':
794
			interfaces_wan_pptp_configure();
795
			break;
796 cfc707f7 Scott Ullrich
797 5b237745 Scott Ullrich
		case 'bigpond':
798
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
799
			interfaces_wan_dhcp_configure();
800
			break;
801 cfc707f7 Scott Ullrich
802 5b237745 Scott Ullrich
		default:
803 a23d7248 Scott Ullrich
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
804
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
805
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
806
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
807
			} else {
808
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
809
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
810
			}
811 5b237745 Scott Ullrich
			/* install default route */
812
			mwexec("/sbin/route delete default");
813 09b897ae Scott Ullrich
			
814
			$dont_add_route = false;
815
			/* if OLSRD is enabled, allow WAN to house DHCP. */
816
			if($config['installedpackages']['olsrd']) {
817
				foreach($config['installedpackages']['olsrd']['config'] as $olsrd) {
818
						if($olsrd['enabledyngw'] == "on") {
819
							$dont_add_route = true;
820
						}
821
				}
822
			}
823
			
824
			if($dont_add_route == false) 
825
				mwexec("/sbin/route add default " . escapeshellarg($config['interfaces']['wan']['gateway']));
826 cfc707f7 Scott Ullrich
827 3c9daf99 Scott Ullrich
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
828
			filter_configure();
829 5b237745 Scott Ullrich
	}
830 cfc707f7 Scott Ullrich
831 77712c1e Scott Ullrich
	if ($wancfg['bridge']) {
832
		/* use open/netBSD style bridge */
833
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
834
		
835
		/* force all bridged interfaces to use same mtu */
836
		$mtu = get_interface_mtu($config['interfaces'][$wancfg['bridge']]['if']);
837
		mwexec("/sbin/ifconfig {$wancfg['if']} mtu {$mtu}");
838
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}");
839
		
840
		/* assign items to a bridge */
841 a591b9cb Scott Ullrich
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}");
842 df792110 Scott Ullrich
843 e7aa7560 Scott Ullrich
		if(!is_interface_wireless($wancfg['if']) and
844
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if'])) 
845 3789e4df Scott Ullrich
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$wancfg['bridge']]['if']} stp {$wancfg['if']}");
846 77712c1e Scott Ullrich
		
847
		/* log commands run for debugging in /tmp/ */
848
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$wancfg['if']}", "w");
849
		fwrite($fd, "/sbin/ifconfig {$wancfg['if']} mtu {$mtu}\n");
850
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}\n");
851
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
852 b3a66d90 Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}\n");
853 77712c1e Scott Ullrich
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$wancfg['if']} stp {$config['interfaces'][$wancfg['bridge']]['if']}\n");
854
		fclose($fd);
855
		
856
		/* bring up interfaces */
857
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
858
		usleep(100);
859
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} up");
860
		usleep(5);
861
		mwexec("/sbin/ifconfig {$wancfg['if']} up");
862
		usleep(5);
863
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
864 c60e4f51 Scott Ullrich
		
865
		$bridges_total++;
866 77712c1e Scott Ullrich
	}
867
868 5b237745 Scott Ullrich
	if (!$g['booting']) {
869
		/* reconfigure static routes (kernel may have deleted them) */
870
		system_routing_configure();
871 cfc707f7 Scott Ullrich
872 e239df5a Scott Ullrich
		/* set the reload filter dity flag */
873 f229e20f Scott Ullrich
		touch("{$g['tmp_path']}/filter_dirty");
874 cfc707f7 Scott Ullrich
875 5b237745 Scott Ullrich
		/* reload ipsec tunnels */
876
		vpn_ipsec_configure();
877 cfc707f7 Scott Ullrich
878 5b237745 Scott Ullrich
		/* restart ez-ipupdate */
879
		services_dyndns_configure();
880 cfc707f7 Scott Ullrich
881 a23d7248 Scott Ullrich
		/* force DNS update */
882
		services_dnsupdate_process();
883
884 5b237745 Scott Ullrich
		/* restart dnsmasq */
885
		services_dnsmasq_configure();
886 c597d50f Scott Ullrich
		
887
		/* reload captive portal */
888
		captiveportal_configure();
889 5b237745 Scott Ullrich
	}
890 cfc707f7 Scott Ullrich
891 5e99d81a Scott Ullrich
	mwexec("/sbin/ifconfig {$wancfg['if']} up");
892
893 c1627786 Scott Ullrich
	unmute_kernel_msgs();
894
895 5b237745 Scott Ullrich
	return 0;
896
}
897
898 0311dbd5 Scott Ullrich
function interfaces_opt_dhcp_configure($interface) {
899
	global $config, $g;
900
901 1223f922 Scott Ullrich
	$optcfg = $config['interfaces'][$interface];
902 45870464 Scott Ullrich
	$optif = $optcfg['if'];
903 0311dbd5 Scott Ullrich
904
	/* generate dhclient_wan.conf */
905 aab78cf6 Scott Ullrich
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
906 0311dbd5 Scott Ullrich
	if (!$fd) {
907 aab78cf6 Scott Ullrich
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
908 0311dbd5 Scott Ullrich
		return 1;
909
	}
910
911 6d76590c Scott Ullrich
	if ($optcfg['dhcphostname']) {
912
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
913
	} else {
914
		$dhclientconf_hostname = "";
915
	}
916
917 0311dbd5 Scott Ullrich
 	$dhclientconf = "";
918
919 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
920
interface "{$optif}" {
921 0311dbd5 Scott Ullrich
	send host-name "{$optcfg['dhcphostname']}";
922 6d302eba Scott Ullrich
	script "/sbin/dhclient-script";
923 6d76590c Scott Ullrich
	{$dhclientconf_hostname}
924 0311dbd5 Scott Ullrich
}
925
926
EOD;
927
928
	fwrite($fd, $dhclientconf);
929
	fclose($fd);
930 45870464 Scott Ullrich
931 0f1b5370 Scott Ullrich
        /* bring interface up before starting dhclient */
932 0311dbd5 Scott Ullrich
        mwexec("/sbin/ifconfig {$optif} up");
933
934
        /* fire up dhclient */
935 c3fe3ad8 Scott Ullrich
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
936 0311dbd5 Scott Ullrich
937
	return 0;
938
}
939
940 8c3e8725 Scott Ullrich
function interfaces_dhcp_configure($interface) {
941
	global $config, $g;
942
943 84cec030 Scott Ullrich
	if(filter_translate_type_to_real_interface($interface) <> "")
944
        	$realinterface = filter_translate_type_to_real_interface($interface);
945 6d76590c Scott Ullrich
946 8c3e8725 Scott Ullrich
	$optcfg = $config['interfaces'][$interface];
947
948
	/* generate dhclient_$interface.conf */
949
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
950
	if (!$fd) {
951
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
952
		return 1;
953
	}
954
955 6d76590c Scott Ullrich
	if ($optcfg['dhcphostname']) {
956
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
957
	} else {
958
		$dhclientconf_hostname = "";
959
	}
960
961 8c3e8725 Scott Ullrich
 	$dhclientconf = "";
962
963 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
964 84cec030 Scott Ullrich
interface "{$realinterface}" {
965 8c3e8725 Scott Ullrich
	script "/sbin/dhclient-script";
966 6d76590c Scott Ullrich
	{$dhclientconf_hostname}
967 8c3e8725 Scott Ullrich
}
968
969
EOD;
970
971
	fwrite($fd, $dhclientconf);
972
	fclose($fd);
973
	
974
	$optif = $optcfg['if'];
975
	
976
        /* bring wan interface up before starting dhclient */
977
        mwexec("/sbin/ifconfig {$optif} up");
978
979
        /* fire up dhclient */
980 c3fe3ad8 Scott Ullrich
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif} >/tmp/{$optif}_output >/tmp/{$optif}_error_output");
981 8c3e8725 Scott Ullrich
982
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
983 c3fe3ad8 Scott Ullrich
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
984 8c3e8725 Scott Ullrich
	fclose($fout);
985
986
	return 0;
987
}
988
989 5b237745 Scott Ullrich
function interfaces_wan_dhcp_configure() {
990
	global $config, $g;
991 cfc707f7 Scott Ullrich
992 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
993
994 0311dbd5 Scott Ullrich
	/* generate dhclient_wan.conf */
995
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
996 5b237745 Scott Ullrich
	if (!$fd) {
997 0311dbd5 Scott Ullrich
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
998 5b237745 Scott Ullrich
		return 1;
999
	}
1000 6d76590c Scott Ullrich
	
1001
	if ($wancfg['dhcphostname']) {
1002
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
1003
	} else {
1004
		$dhclientconf_hostname = "";
1005
	}
1006 cfc707f7 Scott Ullrich
1007 5b237745 Scott Ullrich
 	$dhclientconf = "";
1008 cfc707f7 Scott Ullrich
1009 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
1010 d19cc554 Scott Ullrich
interface "{$wancfg['if']}" {
1011 6d302eba Scott Ullrich
	script "/sbin/dhclient-script";
1012 6d76590c Scott Ullrich
	{$dhclientconf_hostname}
1013 5b237745 Scott Ullrich
}
1014
1015
EOD;
1016
1017
	fwrite($fd, $dhclientconf);
1018
	fclose($fd);
1019 209309ac Scott Ullrich
	
1020 0521b82d Scott Ullrich
	$wanif = $wancfg['if'];
1021
	
1022 eacc8c14 Scott Ullrich
        /* bring wan interface up before starting dhclient */
1023 0521b82d Scott Ullrich
        mwexec("/sbin/ifconfig {$wanif} up");
1024 eacc8c14 Scott Ullrich
1025 0521b82d Scott Ullrich
        /* fire up dhclient */
1026 c3fe3ad8 Scott Ullrich
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
1027 cfc707f7 Scott Ullrich
1028 fdca0ea8 Scott Ullrich
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1029 c3fe3ad8 Scott Ullrich
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
1030 0119d2f7 Scott Ullrich
	fclose($fout);
1031
1032 5b237745 Scott Ullrich
	return 0;
1033
}
1034
1035 a23d7248 Scott Ullrich
function interfaces_wan_dhcp_down() {
1036 0aba287e Scott Ullrich
	global $config;
1037
	$wancfg = $config['interfaces']['wan'];
1038
	$wanif = $wancfg['if'];
1039
	mwexec("/sbin/ifconfig {$wanif} delete");
1040 8551d2ef Scott Ullrich
	sleep(1);
1041 a23d7248 Scott Ullrich
}
1042
1043 468cee8d Scott Ullrich
function interfaces_dhcp_down($interface) {
1044
	global $config;
1045 84cec030 Scott Ullrich
	if(filter_translate_type_to_real_interface($interface) <> "")
1046
		$realinterface = filter_translate_type_to_real_interface($interface);
1047
	mwexec("/sbin/ifconfig {$realinterface} down");
1048 0f1b5370 Scott Ullrich
	sleep(1);
1049 84cec030 Scott Ullrich
	$pid = find_dhclient_process($interface);
1050
	if($pid)
1051
		mwexec("kill {$pid}");
1052 468cee8d Scott Ullrich
}
1053
1054 8c3e8725 Scott Ullrich
function interfaces_dhcp_up($interface) {
1055
	interfaces_dhcp_configure($interface);
1056
	sleep(1);
1057
}
1058
1059 a23d7248 Scott Ullrich
function interfaces_wan_dhcp_up() {
1060
	interfaces_wan_dhcp_configure();
1061 8551d2ef Scott Ullrich
	sleep(1);
1062 a23d7248 Scott Ullrich
}
1063
1064 5b237745 Scott Ullrich
function interfaces_wan_pppoe_configure() {
1065
	global $config, $g;
1066 cfc707f7 Scott Ullrich
1067 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
1068
	$pppoecfg = $config['pppoe'];
1069 cfc707f7 Scott Ullrich
1070 5b237745 Scott Ullrich
	/* generate mpd.conf */
1071
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1072
	if (!$fd) {
1073
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
1074
		return 1;
1075
	}
1076 cfc707f7 Scott Ullrich
1077 5b237745 Scott Ullrich
	$idle = 0;
1078 cfc707f7 Scott Ullrich
1079 5b237745 Scott Ullrich
	if (isset($pppoecfg['ondemand'])) {
1080
		$ondemand = "enable";
1081
		if ($pppoecfg['timeout'])
1082
			$idle = $pppoecfg['timeout'];
1083
	} else {
1084
		$ondemand = "disable";
1085
	}
1086 cfc707f7 Scott Ullrich
1087 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1088
pppoe:
1089
	new -i ng0 pppoe pppoe
1090
	set iface route default
1091
	set iface {$ondemand} on-demand
1092
	set iface idle {$idle}
1093
	set iface up-script /usr/local/sbin/ppp-linkup
1094 cc936773 Scott Ullrich
1095 5b237745 Scott Ullrich
EOD;
1096 389741e5 Scott Ullrich
1097
	/*    Check for ppp-linkdown Script in /usr/local/sbin
1098
	 *    Create reference in mpd.conf
1099
	 */
1100
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1101
		$mpdconf .= <<<EOD
1102
	set iface down-script /usr/local/sbin/ppp-linkdown
1103
1104
EOD;
1105
	}
1106
1107 5b237745 Scott Ullrich
	if (isset($pppoecfg['ondemand'])) {
1108 41404ef1 Scott Ullrich
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
1109
			$mpdconf .= <<<EOD
1110
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
1111 5b237745 Scott Ullrich
1112
EOD;
1113 41404ef1 Scott Ullrich
		} else {
1114
			$mpdconf .= <<<EOD
1115
	set iface addrs 192.0.2.112 192.0.2.113
1116
1117
EOD;
1118
		}
1119 5b237745 Scott Ullrich
	}
1120 cfc707f7 Scott Ullrich
1121 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
1122
	set bundle disable multilink
1123
	set bundle authname "{$pppoecfg['username']}"
1124
	set bundle password "{$pppoecfg['password']}"
1125
	set link keep-alive 10 60
1126
	set link max-redial 0
1127
	set link no acfcomp protocomp
1128
	set link disable pap chap
1129
	set link accept chap
1130
	set link mtu 1492
1131
	set ipcp yes vjcomp
1132
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1133 a23d7248 Scott Ullrich
1134
EOD;
1135
1136
	if (isset($config['system']['dnsallowoverride'])) {
1137
		$mpdconf .= <<<EOD
1138 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
1139 a23d7248 Scott Ullrich
1140
EOD;
1141
	}
1142 a0ff9696 Scott Ullrich
1143 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
1144 5b237745 Scott Ullrich
	open iface
1145
1146
EOD;
1147
1148
	fwrite($fd, $mpdconf);
1149
	fclose($fd);
1150 cfc707f7 Scott Ullrich
1151 5b237745 Scott Ullrich
	/* generate mpd.links */
1152
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1153
	if (!$fd) {
1154
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
1155
		return 1;
1156
	}
1157 cfc707f7 Scott Ullrich
1158 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1159
pppoe:
1160
	set link type pppoe
1161
	set pppoe iface {$wancfg['if']}
1162
	set pppoe service "{$pppoecfg['provider']}"
1163
	set pppoe enable originate
1164
	set pppoe disable incoming
1165
1166
EOD;
1167
1168
	fwrite($fd, $mpdconf);
1169
	fclose($fd);
1170 d7a6517a Scott Ullrich
	
1171
	if(file_exists("{$g['varrun_path']}/mpd.pid") and $g['booting']) {
1172
		/* if we are booting and mpd has already been started then don't start again. */
1173
	} else {
1174
		/* if mpd is active, lets take it down */
1175
		if(file_exists("{$g['varrun_path']}/mpd.pid")) {
1176
			killbypid("{$g['varrun_path']}/mpd.pid");
1177
			sleep(3);
1178
		}
1179
		/* fire up mpd */	
1180
		mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pppoe");
1181 ec11a1ad Scott Ullrich
	}
1182
1183 a205d904 Scott Ullrich
        /* sleep until wan is up - or 30 seconds, whichever comes first */
1184
	for ($count = 0; $count < 30; $count++) {
1185
		if(file_exists("{$g['tmp_path']}/wanup")) {
1186
			break;
1187
		}
1188
		sleep(1);
1189
	}
1190 d7a6517a Scott Ullrich
1191 a205d904 Scott Ullrich
	unlink_if_exists("{$g['tmp_path']}/wanup");
1192 e1c8cdf5 Scott Ullrich
1193 5b237745 Scott Ullrich
	return 0;
1194
}
1195
1196 a23d7248 Scott Ullrich
function interfaces_wan_pppoe_down() {
1197
	global $g;
1198
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1199 8551d2ef Scott Ullrich
	sleep(1);
1200 a23d7248 Scott Ullrich
}
1201
1202
function interfaces_wan_pppoe_up() {
1203
	global $g;
1204
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1205 8551d2ef Scott Ullrich
	sleep(1);
1206 a23d7248 Scott Ullrich
}
1207
1208 5b237745 Scott Ullrich
function interfaces_wan_pptp_configure() {
1209
	global $config, $g;
1210 cfc707f7 Scott Ullrich
1211 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
1212
	$pptpcfg = $config['pptp'];
1213 cfc707f7 Scott Ullrich
1214 5b237745 Scott Ullrich
	/* generate mpd.conf */
1215
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1216
	if (!$fd) {
1217
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1218
		return 1;
1219
	}
1220 cfc707f7 Scott Ullrich
1221 5b237745 Scott Ullrich
	$idle = 0;
1222 cfc707f7 Scott Ullrich
1223 5b237745 Scott Ullrich
	if (isset($pptpcfg['ondemand'])) {
1224
		$ondemand = "enable";
1225
		if ($pptpcfg['timeout'])
1226
			$idle = $pptpcfg['timeout'];
1227
	} else {
1228
		$ondemand = "disable";
1229
	}
1230 cfc707f7 Scott Ullrich
1231 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1232
pptp:
1233
	new -i ng0 pptp pptp
1234
	set iface route default
1235
	set iface {$ondemand} on-demand
1236
	set iface idle {$idle}
1237
	set iface up-script /usr/local/sbin/ppp-linkup
1238
1239
EOD;
1240 cfc707f7 Scott Ullrich
1241 389741e5 Scott Ullrich
	/*   Check for ppp-linkdown Script in /usr/local/sbin
1242
	 *   Create reference in mpd.conf
1243
	 */
1244
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1245
		$mpdconf .= <<<EOD
1246
	set iface down-script /usr/local/sbin/ppp-linkdown
1247
1248
EOD;
1249
	}
1250
1251 5b237745 Scott Ullrich
	if (isset($pptpcfg['ondemand'])) {
1252
		$mpdconf .= <<<EOD
1253 a23d7248 Scott Ullrich
	set iface addrs 10.0.0.1 10.0.0.2
1254 5b237745 Scott Ullrich
1255
EOD;
1256
	}
1257 cfc707f7 Scott Ullrich
1258 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
1259
	set bundle disable multilink
1260
	set bundle authname "{$pptpcfg['username']}"
1261
	set bundle password "{$pptpcfg['password']}"
1262
	set link keep-alive 10 60
1263
	set link max-redial 0
1264
	set link no acfcomp protocomp
1265
	set link disable pap chap
1266
	set link accept chap
1267
	set ipcp no vjcomp
1268
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1269 a23d7248 Scott Ullrich
1270
EOD;
1271
	if (isset($config['system']['dnsallowoverride'])) {
1272
		$mpdconf .= <<<EOD
1273 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
1274 a23d7248 Scott Ullrich
1275
EOD;
1276
	}
1277 a0ff9696 Scott Ullrich
1278 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
1279 5b237745 Scott Ullrich
	open
1280
1281
EOD;
1282
1283
	fwrite($fd, $mpdconf);
1284
	fclose($fd);
1285 cfc707f7 Scott Ullrich
1286 5b237745 Scott Ullrich
	/* generate mpd.links */
1287
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1288
	if (!$fd) {
1289
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1290
		return 1;
1291
	}
1292 cfc707f7 Scott Ullrich
1293 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1294
pptp:
1295
	set link type pptp
1296
	set pptp enable originate outcall
1297
	set pptp disable windowing
1298
	set pptp self {$pptpcfg['local']}
1299
	set pptp peer {$pptpcfg['remote']}
1300
1301
EOD;
1302
1303
	fwrite($fd, $mpdconf);
1304
	fclose($fd);
1305 cfc707f7 Scott Ullrich
1306 5b237745 Scott Ullrich
	/* configure interface */
1307 cfc707f7 Scott Ullrich
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1308 5b237745 Scott Ullrich
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1309 cfc707f7 Scott Ullrich
1310 5b237745 Scott Ullrich
	/* fire up mpd */
1311
	mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pptp");
1312 cfc707f7 Scott Ullrich
1313 5b237745 Scott Ullrich
	return 0;
1314
}
1315
1316 a23d7248 Scott Ullrich
function interfaces_wan_pptp_down() {
1317
	global $g;
1318
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1319 8551d2ef Scott Ullrich
	sleep(1);
1320 a23d7248 Scott Ullrich
}
1321
1322
function interfaces_wan_pptp_up() {
1323
	global $g;
1324
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1325 8551d2ef Scott Ullrich
	sleep(1);
1326 a23d7248 Scott Ullrich
}
1327
1328 5b237745 Scott Ullrich
function interfaces_wan_bigpond_configure($curwanip) {
1329
	global $config, $g;
1330 cfc707f7 Scott Ullrich
1331 5b237745 Scott Ullrich
	$bpcfg = $config['bigpond'];
1332 cfc707f7 Scott Ullrich
1333 5b237745 Scott Ullrich
	if (!$curwanip) {
1334
		/* IP address not configured yet, exit */
1335
		return 0;
1336
	}
1337 cfc707f7 Scott Ullrich
1338 5b237745 Scott Ullrich
	/* kill bpalogin */
1339
	killbyname("bpalogin");
1340 cfc707f7 Scott Ullrich
1341 5b237745 Scott Ullrich
	/* wait a moment */
1342
	sleep(1);
1343 cfc707f7 Scott Ullrich
1344 5b237745 Scott Ullrich
	/* get the default domain */
1345
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1346
	if ($nfd) {
1347
		$defaultdomain = trim(fgets($nfd));
1348
		fclose($nfd);
1349
	}
1350 cfc707f7 Scott Ullrich
1351 5b237745 Scott Ullrich
	/* generate bpalogin.conf */
1352
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1353
	if (!$fd) {
1354
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1355
		return 1;
1356
	}
1357 cfc707f7 Scott Ullrich
1358 5b237745 Scott Ullrich
	if (!$bpcfg['authserver'])
1359
		$bpcfg['authserver'] = "dce-server";
1360
	if (!$bpcfg['authdomain'])
1361
		$bpcfg['authdomain'] = $defaultdomain;
1362 cfc707f7 Scott Ullrich
1363 5b237745 Scott Ullrich
	$bpconf = <<<EOD
1364
username {$bpcfg['username']}
1365
password {$bpcfg['password']}
1366
authserver {$bpcfg['authserver']}
1367
authdomain {$bpcfg['authdomain']}
1368
localport 5050
1369
1370
EOD;
1371
1372
	if ($bpcfg['minheartbeatinterval'])
1373
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1374
1375
	fwrite($fd, $bpconf);
1376
	fclose($fd);
1377 cfc707f7 Scott Ullrich
1378 5b237745 Scott Ullrich
	/* fire up bpalogin */
1379
	mwexec("/usr/local/sbin/bpalogin -c {$g['varetc_path']}/bpalogin.conf");
1380 cfc707f7 Scott Ullrich
1381 5b237745 Scott Ullrich
	return 0;
1382
}
1383
1384
function get_real_wan_interface() {
1385
	global $config, $g;
1386 cfc707f7 Scott Ullrich
1387 5b237745 Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
1388 cfc707f7 Scott Ullrich
1389 5b237745 Scott Ullrich
	$wanif = $wancfg['if'];
1390
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1391
		$wanif = $g['pppoe_interface'];
1392
	}
1393 cfc707f7 Scott Ullrich
1394 5b237745 Scott Ullrich
	return $wanif;
1395
}
1396
1397 1675c73f Scott Ullrich
function get_current_wan_address($interface = "wan") {
1398 5b237745 Scott Ullrich
	global $config, $g;
1399 cfc707f7 Scott Ullrich
1400 1675c73f Scott Ullrich
	$wancfg = $config['interfaces'][$interface];
1401 cfc707f7 Scott Ullrich
1402 a8a7325e Scott Ullrich
	$interface = filter_translate_type_to_real_interface($interface);
1403 767a716e Scott Ullrich
	$ifinfo = "";
1404 bc76c771 Scott Ullrich
	if(in_array($wancfg['ipaddr'], array('dhcp'))) {
1405
		/* get interface info with netstat */
1406
		exec("/usr/bin/netstat -nWI " . escapeshellarg($interface) . " -f inet", $ifinfo);
1407
1408
		if (isset($ifinfo[1])) {
1409
			$aif = preg_split("/\s+/", $ifinfo[1]);
1410
			$curwanip = chop($aif[3]);
1411
1412
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1413
				return $curwanip;
1414
		}
1415
1416
		return null;		
1417
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1418 5b237745 Scott Ullrich
		/* dynamic WAN IP address, find out which one */
1419
		$wanif = get_real_wan_interface();
1420 cfc707f7 Scott Ullrich
1421 5b237745 Scott Ullrich
		/* get interface info with netstat */
1422
		exec("/usr/bin/netstat -nWI " . escapeshellarg($wanif) . " -f inet", $ifinfo);
1423 cfc707f7 Scott Ullrich
1424 5b237745 Scott Ullrich
		if (isset($ifinfo[1])) {
1425
			$aif = preg_split("/\s+/", $ifinfo[1]);
1426
			$curwanip = chop($aif[3]);
1427 cfc707f7 Scott Ullrich
1428 5b237745 Scott Ullrich
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1429
				return $curwanip;
1430
		}
1431 cfc707f7 Scott Ullrich
1432 5b237745 Scott Ullrich
		return null;
1433
	} else {
1434
		/* static WAN IP address */
1435
		return $wancfg['ipaddr'];
1436
	}
1437
}
1438
1439 a57b119e Bill Marquette
/****f* interfaces/is_altq_capable
1440
 * NAME
1441
 *   is_altq_capable - Test if interface is capable of using ALTQ
1442
 * INPUTS
1443
 *   $int            - string containing interface name
1444
 * RESULT
1445
 *   boolean         - true or false
1446
 ******/
1447
1448
function is_altq_capable($int) {
1449
        /* Per:
1450
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1451
         * Only the following drivers have ALTQ support
1452
         */
1453
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1454 8e8cbefd Scott Ullrich
		"em", "fxp", "hme", "lnc", "re", "rl", "ndis", "sf", "sis", "sk",
1455 a262450f Scott Ullrich
		"tun", "vr", "wi", "xl", "vlan", "ste");
1456 a57b119e Bill Marquette
1457
        $int_family = preg_split("/[0-9]+/", $int);
1458
1459
        if (in_array($int_family[0], $capable))
1460
                return true;
1461
        else
1462
                return false;
1463
}
1464
1465 015bb1cc Scott Ullrich
function get_number_of_bridged_interfaces() {
1466 ce33da32 Scott Ullrich
	$bridges_total = 0;
1467
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1468
	foreach($bridges as $bridge) {
1469 767a716e Scott Ullrich
		$match_array = "";
1470 ce33da32 Scott Ullrich
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1471 7370613f Scott Ullrich
		if($match_array[1][0] <> "") {
1472
			if($match_array[1][0] > $bridges_total)
1473
				$bridges_total = $match_array[1][0];
1474
		}
1475 ce33da32 Scott Ullrich
	}
1476
	return "{$bridges_total}";
1477 015bb1cc Scott Ullrich
}
1478
1479
function get_next_available_bridge_interface() {
1480 38a3e74e Scott Ullrich
	$bridges_total = get_number_of_bridged_interfaces();
1481 0d429e43 Scott Ullrich
	$interfaces = `/sbin/ifconfig -l`;
1482 26eb3795 Scott Ullrich
	$x=0;
1483 f78d2412 Scott Ullrich
	for($x=0; $x<$bridges_total; $x++) {
1484 0d429e43 Scott Ullrich
		if(!stristr($interfaces, "bridge{$x}")) {
1485
			return "{$x}";
1486 dba4049f Scott Ullrich
		}
1487 015bb1cc Scott Ullrich
	}
1488 0d429e43 Scott Ullrich
	return "{$x}";
1489 015bb1cc Scott Ullrich
}
1490
1491 ba0e11c7 Scott Ullrich
function destroy_bridge($bridge_num) {
1492 e2f56f0d Scott Ullrich
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1493 f9118d49 Scott Ullrich
	sleep(1);
1494 ba0e11c7 Scott Ullrich
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1495 0d429e43 Scott Ullrich
	sleep(1);
1496 ba0e11c7 Scott Ullrich
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1497 f9118d49 Scott Ullrich
	sleep(1);
1498 ba0e11c7 Scott Ullrich
	return;
1499
}
1500
1501
function discover_bridge($interface1, $interface2) {
1502 7370613f Scott Ullrich
	if(!$interface1) return;
1503
	if(!$interface2) return;
1504 ba0e11c7 Scott Ullrich
	$total_bridges = get_number_of_bridged_interfaces();
1505 4f9a78d9 Scott Ullrich
	$interfaces = `/sbin/ifconfig -l`;
1506 26eb3795 Scott Ullrich
	$x=0;
1507 ba0e11c7 Scott Ullrich
	for($x=0; $x<$total_bridges; $x++) {
1508 4f9a78d9 Scott Ullrich
		$bridge_text = "NA";
1509 7370613f Scott Ullrich
		if(!stristr($interfaces, "bridge{$x}")) 
1510
			continue;
1511
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1512 a3484b5c Scott Ullrich
		if(stristr($bridge_text, $interface1) == true and
1513
		   stristr($bridge_text, $interface2) == true) {
1514 0d429e43 Scott Ullrich
			return "{$x}";			
1515 ba0e11c7 Scott Ullrich
		}
1516
	}
1517 0d429e43 Scott Ullrich
	return "-1";
1518 ba0e11c7 Scott Ullrich
}
1519 a57b119e Bill Marquette
1520 d8c67d69 Scott Ullrich
function get_wireless_modes($interface)
1521
{
1522
	/* return wireless modes and channels */
1523
	if(is_interface_wireless($interface)) {
1524
		$wi = 1;
1525
		$ifconfig = "/sbin/ifconfig";
1526
		$awk = "/usr/bin/awk";
1527
		$chan_list = "$ifconfig $interface list chan";
1528 4066776d Scott Ullrich
		$stack_list = "$awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
1529 d8c67d69 Scott Ullrich
		$format_list = "$awk '{print \$5 \" \" \$6 \",\" \$1}'";
1530
1531 4b0e71db Scott Ullrich
		$interface_channels = "";
1532 d8c67d69 Scott Ullrich
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
1533
		$interface_channel_count = count($interface_channels);
1534
1535
		$c = 0;
1536
		while ($c < $interface_channel_count)
1537
		{
1538
			$channel_line = explode(",", $interface_channels["$c"]);
1539
			$wireless_mode = trim($channel_line[0]);
1540
			$wireless_channel = trim($channel_line[1]);
1541 4066776d Scott Ullrich
			if(trim($wireless_mode) != "") {
1542
				/* if we only have 11g also set 11b channels */
1543
				if($wireless_mode == "11g") {
1544
					$wireless_modes["11b"] = array();
1545
				}
1546
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
1547
			}
1548 d8c67d69 Scott Ullrich
			$c++;
1549
		}
1550
	}
1551 4066776d Scott Ullrich
	return($wireless_modes);
1552 d8c67d69 Scott Ullrich
}
1553
1554 c597d50f Scott Ullrich
?>