Project

General

Profile

Download (44.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	interfaces.inc
5
	Copyright (C) 2004-2006 Scott Ullrich
6
	All rights reserved.
7

    
8
	function interfaces_wireless_configure is
9
	Copyright (C) 2005 Espen Johansen
10
	All rights reserved.
11

    
12
	originally part of m0n0wall (http://m0n0.ch/wall)
13
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
	All rights reserved.
15

    
16
	Redistribution and use in source and binary forms, with or without
17
	modification, are permitted provided that the following conditions are met:
18

    
19
	1. Redistributions of source code must retain the above copyright notices,
20
	   this list of conditions and the following disclaimer.
21

    
22
	2. Redistributions in binary form must reproduce the above copyright
23
	   notices, this list of conditions and the following disclaimer in the
24
	   documentation and/or other materials provided with the distribution.
25

    
26
	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

    
44
	return 0;
45
}
46

    
47
function interfaces_vlan_configure() {
48
	global $config;
49

    
50
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
51

    
52
		/* devices with native VLAN support */
53
		$vlan_native_supp = explode(" ", "bge em gx nge ti txp");
54

    
55
		/* devices with long frame support */
56
		$vlan_long_supp = explode(" ", "dc fxp sis ste tl tx xl");
57

    
58
		$i = 0;
59

    
60
		foreach ($config['vlans']['vlan'] as $vlan) {
61

    
62
			$cmd = "/sbin/ifconfig vlan{$i} create vlan " .
63
				escapeshellarg($vlan['tag']) . " vlandev " .
64
				escapeshellarg($vlan['if']);
65

    
66
			/* 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

    
73
			if (in_array($drvname, $vlan_native_supp))
74
				$cmd .= " link0";
75
			else if (in_array($drvname, $vlan_long_supp))
76
				$cmd .= " mtu 1500";
77

    
78
			mwexec($cmd);
79

    
80
			/* make sure the parent interface is up */
81
			mwexec("/sbin/ifconfig " . escapeshellarg($vlan['if']) . " up");
82

    
83
			$i++;
84
		}
85
	}
86

    
87
	return 0;
88
}
89

    
90
function interfaces_lan_configure() {
91
	global $config, $g;
92

    
93
	$bridges_total = get_next_available_bridge_interface();
94

    
95
	$lancfg = $config['interfaces']['lan'];
96

    
97
	/* if user has removed ip address, clear it*/
98
	if($lancfg['ipaddr'] == "")
99
		mwexec("/sbin/ifconfig {$lancfg['if']} delete");
100

    
101
	/* wireless configuration? */
102
	if (is_array($lancfg['wireless']))
103
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
104

    
105
	/* MAC spoofing? */
106
	if ($lancfg['spoofmac']) {
107
		mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
108
			" link " . escapeshellarg($lancfg['spoofmac']));
109
	} 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
			echo "Generating new MAC address.";
116
			$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
			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
		}
123
	}	
124

    
125
	/* bridged? */
126
	
127
	if ($lancfg['bridge']) {
128
		/* use open/netBSD style bridge */
129
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
130
		
131
		/* force all bridged interfaces to use same mtu */
132
		$mtu = get_interface_mtu($config['interfaces'][$lancfg['bridge']]['if']);
133
		mwexec("/sbin/ifconfig {$lancfg['if']} mtu {$mtu}");
134
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}");
135

    
136
		/* assign items to a bridge */
137
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}");
138
		
139
		if(!is_interface_wireless($lancfg['if']) and
140
		   !is_interface_wireless($config['interfaces'][$lancfg['bridge']]['if'])) 
141
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$lancfg['bridge']]['if']} stp {$lancfg['if']}");
142

    
143
		/* log commands run for debugging in /tmp/ */
144
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
145
		fwrite($fd, "/sbin/ifconfig {$lancfg['if']} mtu {$mtu}\n");
146
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}\n");
147
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
148
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}\n");
149
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}\n");
150
		fclose($fd);
151
		
152
		/* bring up interfaces */
153
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
154
		usleep(100);
155
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} up");
156
		usleep(5);
157
		mwexec("/sbin/ifconfig {$lancfg['if']} up");
158
		usleep(5);
159
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
160
		
161
		$bridges_total++;
162
	}
163
	
164
	/* 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

    
174
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
175
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
176

    
177
	if (!$g['booting']) {
178
		/* make new hosts file */
179
		system_hosts_generate();
180

    
181
		/* reconfigure static routes (kernel may have deleted them) */
182
		system_routing_configure();
183

    
184
		/* set the reload filter dity flag */
185
		touch("{$g['tmp_path']}/filter_dirty");
186

    
187
		/* reload IPsec tunnels */
188
		vpn_ipsec_configure();
189

    
190
		/* reload dhcpd (gateway may have changed) */
191
		services_dhcpd_configure();
192

    
193
		/* reload dnsmasq */
194
		services_dnsmasq_configure();
195

    
196
		/* reload captive portal */
197
		captiveportal_configure();
198

    
199
	}
200

    
201
	return 0;
202
}
203

    
204
function interfaces_optional_configure() {
205
	global $config, $g;
206
	global $bridgeconfig;
207

    
208
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
209
		interfaces_optional_configure_if($i);
210
	}
211

    
212
	if (!$g['booting']) {
213
		/* reconfigure static routes (kernel may have deleted them) */
214
		system_routing_configure();
215

    
216
		/* reload IPsec tunnels */
217
		vpn_ipsec_configure();
218

    
219
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
220
		services_dhcpd_configure();
221

    
222
		/* restart dnsmasq */
223
		services_dnsmasq_configure();
224

    
225
		/* reload captive portal */
226
		captiveportal_configure();
227

    
228
		/* set the reload filter dity flag */
229
		touch("{$g['tmp_path']}/filter_dirty");				
230
	}
231

    
232
	return 0;
233
}
234

    
235
function interfaces_optional_configure_if($opti) {
236
	global $config, $g;
237
	global $bridgeconfig, $debugging;
238

    
239
	$bridges_total = get_next_available_bridge_interface();
240

    
241
	$optcfg = $config['interfaces']['opt' . $opti];
242

    
243
	if ($g['booting']) {
244
		$optdescr = "";
245
		if ($optcfg['descr'])
246
			$optdescr = " ({$optcfg['descr']})";
247
		print "\tOPT{$opti}{$optdescr}... ";
248
	}
249

    
250
	if (isset($optcfg['enable'])) {
251
		/* wireless configuration? */
252
		if (is_array($optcfg['wireless']))
253
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
254

    
255
		/* MAC spoofing? */
256
		if ($optcfg['spoofmac']) {
257
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
258
				" link " . escapeshellarg($optcfg['spoofmac']));
259
		} 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
				echo "Generating new MAC address.";
266
				$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
		}
274

    
275
		/* 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

    
285
		/* bridged? */
286
		if ($optcfg['bridge']) {
287
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
288
                        /* use open/netBSD style bridge */
289
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
290
			
291
			/* force all bridged interfaces to use same mtu */
292
			$mtu = get_interface_mtu($config['interfaces'][$optcfg['bridge']]['if']);
293
			mwexec("/sbin/ifconfig {$optcfg['if']} mtu {$mtu}");
294
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}");			
295

    
296
			/* assign items to a bridge */
297
                        mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']}");
298

    
299
			if(!is_interface_wireless($optcfg['if']) and
300
			   !is_interface_wireless($config['interfaces'][$optcfg['bridge']]['if'])) 
301
				mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$optcfg['bridge']]['if']} stp {$optcfg['if']}");
302

    
303
			/* log commands run for debugging in /tmp/ */
304
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
305
			fwrite($fd, "/sbin/ifconfig {$optcfg['if']} mtu {$mtu}\n");
306
			fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}\n");
307
			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
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} stp {$config['interfaces'][$optcfg['bridge']]['if']}\n");
310
			fclose($fd);
311
			
312
			/* bring up interfaces */
313
			mwexec("/sbin/ifconfig bridge{$bridges_total} down");
314
			usleep(100);
315
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} up");
316
			usleep(5);
317
			mwexec("/sbin/ifconfig {$optcfg['if']} up");
318
			usleep(5);
319
			mwexec("/sbin/ifconfig bridge{$bridges_total} up");
320
			
321
			$bridges_total++;
322
			
323
		} else {
324
			/* if user has selected DHCP type then act accordingly */
325
			if($optcfg['ipaddr'] == "dhcp") {
326
				interfaces_opt_dhcp_configure("opt{$opti}");
327
			} else {			
328
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
329
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
330
			}
331
		}
332
	} else {
333
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
334
	}
335
	return 0;
336
}
337

    
338
function interfaces_carp_configure() {
339
	global $g, $config, $debugging;
340
	$balanacing = "";
341
	$pfsyncinterface = "";
342
	$pfsyncenabled = "";
343
	if(isset($config['system']['developerspew'])) {
344
		$mt = microtime();
345
		echo "interfaces_carp_configure() being called $mt\n";
346
	}
347
	$carp_instances_counter = 0;
348
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
349
	/* destroy previous interfaces */
350
	for($x=0; $x<$total_carp_interfaces_defined; $x++) 
351
		mwexec("/sbin/ifconfig carp{$x} delete");	
352
	if ($g['booting']) {
353
		echo "Configuring CARP interfaces...";
354
		mute_kernel_msgs();
355
	}
356
	/* 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
		}
363
	} else {
364
		unset($pfsyncinterface);
365
		unset($balanacing);
366
		unset($pfsyncenabled);
367
	}
368
	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
	if($carp_sync_int and $pfsyncenabled) 
386
		mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up");
387
	else 
388
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
389
	$fd = fopen("/tmp/carp.sh", "w");
390
	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
	foreach ($viparr as $vip) {
398
		if ($vip['mode'] == "carp") {
399
			$vip_password = $vip['password'];
400
			$vip_password = str_replace(" ", "", $vip_password);			
401
			/* create the carp interface and setup */
402
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
403
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
404
			if($vip['password'] != "") 
405
				$password = " pass \"" . $vip_password . "\"";
406
			if($debugging)
407
				echo "Configuring carp{$carp_instances_counter}.\n";
408
			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
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password);
410
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
411
			fwrite($fd, "/sbin/ifconfig carp" . $carp_instances_counter . " up\n");
412
			usleep(10);
413
			$carp_instances_counter++;
414
		}
415
	}
416
	fclose($fd);
417
	mwexec("/bin/sh /tmp/carp.sh");
418
	if ($g['booting']) {
419
		unmute_kernel_msgs();
420
		echo "done.\n";
421
	}
422
}
423

    
424
function interfaces_carp_bring_up_final() {
425
	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
	if(!$config['installedpackages']['carpsettings']['config'])
431
		return;
432
	$viparr = &$config['virtualip']['vip'];
433
	/* could not locate an array, return */
434
	if(!is_array($viparr)) 
435
		return;
436
	$carp_instances_counter = 0;
437
	$counter = 0;
438
	if($g['booting'])
439
		echo "Waiting for final CARP interface bringup...";
440
	$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
441
	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
				$supress = 0;
447
			$counter++;
448
			echo ".";
449
		}
450
		for($x=0; $x<23; $x++) {
451
			sleep(2);
452
			echo ".";
453
		}
454
		echo " done.\n";
455
	}
456
	foreach ($viparr as $vip) {
457
		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
		sleep(1);
466
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
467
		$carp_instances_counter++;
468
	}
469
	if($g['booting'])
470
		echo " done.\n";	
471
}
472

    
473
function interfaces_wireless_configure($if, $wlcfg) {
474
	global $config, $g;
475
	
476
	/*    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
	
483
	conf_mount_rw();
484
	
485
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
486
	
487
	$fd_set = fopen("/tmp/{$if}_setup.sh","w");
488
	fwrite($fd_set, "#!/bin/sh\n");
489
	fwrite($fd_set, "# pfSense wireless configuration script.\n\n");
490
	
491
	fwrite($fd_set, "# enable shell debugging\n");
492
	fwrite($fd_set, "set -x\n");
493
	
494
	/* set values for /path/program */
495
	$hostapd = "/usr/sbin/hostapd";
496
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
497
	$ifconfig = "/sbin/ifconfig";
498
	$killall = "/usr/bin/killall";
499

    
500
	/* Set all wireless ifconfig variables (splitt up to get rid of needed checking) */
501

    
502
	/* Set a/b/g standard */
503
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
504

    
505
	/* Set 802.11g protection mode */
506
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
507

    
508
	/* set wireless channel value */
509
	if(isset($wlcfg['channel']))
510
		$channel = "channel " . escapeshellarg($wlcfg['channel']);
511

    
512
	/* set Distance value */
513
	if($wlcfg['distance']) 
514
		$distance = escapeshellarg($wlcfg['distance']);
515

    
516
	/* Set ssid */
517
	if($wlcfg['ssid'])  
518
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
519
		
520
	/* Set wireless hostap mode */
521
	if ($wlcfg['mode'] == "hostap")
522
		$hostapmode = "mediaopt hostap";
523
	else
524
		$hostapmode = "-mediaopt hostap";
525

    
526
	/* Set wireless adhoc mode */
527
	if ($wlcfg['mode'] == "adhoc")
528
		$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
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
561
	
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
		case 'bss':
587
			if (isset($wlcfg['wpa']['enable'])) {
588

    
589
				$wpa .= <<<EOD
590
ctrl_interface={$g['varrun_path']}/wpa_supplicant
591
ctrl_interface_group=0
592
ap_scan=1
593
#fast_reauth=1
594
network={
595
ssid="{$wlcfg['ssid']}"
596
scan_ssid=1
597
priority=5
598
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
599
psk="{$wlcfg['wpa']['passphrase']}"
600
pairwise={$wlcfg['wpa']['wpa_pairwise']}
601
group={$wlcfg['wpa']['wpa_pairwise']}
602
}
603
EOD;
604

    
605
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
606
				fwrite($fd, "{$wpa}");
607
				fclose($fd);
608

    
609
				fwrite($fd_set, "{$killall} wpa_supplicant\n");
610
			}
611
		break;
612

    
613
		case 'hostap':
614
			if (isset($wlcfg['wpa']['enable'])) {
615
				$wpa .= <<<EOD
616
interface={$if}
617
driver=bsd
618
logger_syslog=-1
619
logger_syslog_level=0
620
logger_stdout=-1
621
logger_stdout_level=0
622
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
623
ctrl_interface={$g['varrun_path']}/hostapd
624
ctrl_interface_group=wheel
625
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
626
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
627
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
628
ssid={$wlcfg['ssid']}
629
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
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
wpa_passphrase={$wlcfg['wpa']['passphrase']}
638
ieee8021x={$wlcfg['wpa']['ieee8021x']}
639
#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
EOD;
643

    
644
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
645
				fwrite($fd, "{$wpa}");
646
				fclose($fd);
647

    
648
				fwrite($fd_set, "{$killall} hostapd\n");
649
			}
650
		break;
651

    
652
		case 'adhoc':
653
			fwrite($fd_set, "{$killall} hostapd\n");
654
			fwrite($fd_set, "{$killall} wpa_supplicant\n");
655
		break;
656
	}	
657

    
658
	/*
659
	 *    all variables are set, lets start up everything
660
     */
661
	
662
	/* set ack timers according to users preference (if he/she has any) */
663
	if($distance) {
664
		fwrite($fd_set, "# Enable ATH distance settings\n");
665
		fwrite($fd_set, "/sbin/athctrl.sh -i {$if} -d {$distance}\n");
666
	}
667
	
668
	$standard_no_turbo = str_replace(" Turbo", "", $standard);
669
	
670
	$settings = <<<EOD
671

    
672
{$ifconfig} {$if} {$hostapmode}
673
{$ifconfig} {$if} {$standard_no_turbo}
674
{$ifconfig} {$if} {$channel}
675
{$ifconfig} {$if} {$turbo}
676
{$ifconfig} {$if} {$ssid}
677
{$ifconfig} {$if} {$hidessid}
678
{$ifconfig} {$if} {$adhocmode}
679
{$ifconfig} {$if} {$protmode}
680
{$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

    
692
	if (isset($wlcfg['wpa']['enable'])) {
693
		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
	}
698

    
699
	fclose($fd_set);
700

    
701
	conf_mount_ro();
702

    
703
	/* execute commands now in shell */
704
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
705
	sleep(1);
706
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
707

    
708
	return 0;
709

    
710
}
711

    
712
function find_dhclient_process($interface) {
713
	if(filter_translate_type_to_real_interface($interface) <> "")
714
        	$realinterface = filter_translate_type_to_real_interface($interface);
715
	$pid = `/usr/bin/pgrep -f "dhclient: {$realinterface}(\$| .*)"`;
716
	return $pid;
717
}
718

    
719
function interfaces_wan_configure() {
720
	global $config, $g, $bridges_total;
721

    
722
	$wancfg = $config['interfaces']['wan'];
723

    
724
	if(!$g['booting']) {
725
		mute_kernel_msgs();
726

    
727
		/* find dhclient process for wan and kill it */
728
		killbypid(find_dhclient_process("wan"));
729

    
730
		/* remove wanup file if it exists */
731
		unlink_if_exists("{$g['tmp_path']}/wanup");
732

    
733
		/* kill PPPoE client (mpd) */
734
		killbypid("{$g['varrun_path']}/mpd.pid");
735

    
736
		/* wait for processes to die */
737
		sleep(3);
738

    
739
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
740
		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
	}
745

    
746
	/* remove all addresses first */
747
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
748
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
749

    
750
	/* wireless configuration? */
751
	if (is_array($wancfg['wireless']))
752
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
753

    
754
	if ($wancfg['spoofmac']) {
755
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
756
			" link " . escapeshellarg($wancfg['spoofmac']));
757
	}  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
			echo "Generating new MAC address.";
764
			$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
			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
		}
771
	}
772

    
773
	/* 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

    
783
	switch ($wancfg['ipaddr']) {
784

    
785
		case 'dhcp':
786
			interfaces_wan_dhcp_configure();
787
			break;
788

    
789
		case 'pppoe':
790
			interfaces_wan_pppoe_configure();
791
			break;
792

    
793
		case 'pptp':
794
			interfaces_wan_pptp_configure();
795
			break;
796

    
797
		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

    
802
		default:
803
			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
			/* install default route */
812
			mwexec("/sbin/route delete default");
813
			
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

    
827
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
828
			filter_configure();
829
	}
830

    
831
	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
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}");
842

    
843
		if(!is_interface_wireless($wancfg['if']) and
844
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if'])) 
845
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$wancfg['bridge']]['if']} stp {$wancfg['if']}");
846
		
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
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}\n");
853
		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
		
865
		$bridges_total++;
866
	}
867

    
868
	if (!$g['booting']) {
869
		/* reconfigure static routes (kernel may have deleted them) */
870
		system_routing_configure();
871

    
872
		/* set the reload filter dity flag */
873
		touch("{$g['tmp_path']}/filter_dirty");
874

    
875
		/* reload ipsec tunnels */
876
		vpn_ipsec_configure();
877

    
878
		/* restart ez-ipupdate */
879
		services_dyndns_configure();
880

    
881
		/* force DNS update */
882
		services_dnsupdate_process();
883

    
884
		/* restart dnsmasq */
885
		services_dnsmasq_configure();
886
		
887
		/* reload captive portal */
888
		captiveportal_configure();
889
	}
890

    
891
	mwexec("/sbin/ifconfig {$wancfg['if']} up");
892

    
893
	unmute_kernel_msgs();
894

    
895
	return 0;
896
}
897

    
898
function interfaces_opt_dhcp_configure($interface) {
899
	global $config, $g;
900

    
901
	$optcfg = $config['interfaces'][$interface];
902
	$optif = $optcfg['if'];
903

    
904
	/* generate dhclient_wan.conf */
905
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
906
	if (!$fd) {
907
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
908
		return 1;
909
	}
910

    
911
	if ($optcfg['dhcphostname']) {
912
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
913
	} else {
914
		$dhclientconf_hostname = "";
915
	}
916

    
917
 	$dhclientconf = "";
918

    
919
	$dhclientconf .= <<<EOD
920
interface "{$optif}" {
921
	send host-name "{$optcfg['dhcphostname']}";
922
	script "/sbin/dhclient-script";
923
	{$dhclientconf_hostname}
924
}
925

    
926
EOD;
927

    
928
	fwrite($fd, $dhclientconf);
929
	fclose($fd);
930

    
931
        /* bring interface up before starting dhclient */
932
        mwexec("/sbin/ifconfig {$optif} up");
933

    
934
        /* fire up dhclient */
935
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
936

    
937
	return 0;
938
}
939

    
940
function interfaces_dhcp_configure($interface) {
941
	global $config, $g;
942

    
943
	if(filter_translate_type_to_real_interface($interface) <> "")
944
        	$realinterface = filter_translate_type_to_real_interface($interface);
945

    
946
	$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
	if ($optcfg['dhcphostname']) {
956
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
957
	} else {
958
		$dhclientconf_hostname = "";
959
	}
960

    
961
 	$dhclientconf = "";
962

    
963
	$dhclientconf .= <<<EOD
964
interface "{$realinterface}" {
965
	script "/sbin/dhclient-script";
966
	{$dhclientconf_hostname}
967
}
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
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif} >/tmp/{$optif}_output >/tmp/{$optif}_error_output");
981

    
982
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
983
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
984
	fclose($fout);
985

    
986
	return 0;
987
}
988

    
989
function interfaces_wan_dhcp_configure() {
990
	global $config, $g;
991

    
992
	$wancfg = $config['interfaces']['wan'];
993

    
994
	/* generate dhclient_wan.conf */
995
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
996
	if (!$fd) {
997
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
998
		return 1;
999
	}
1000
	
1001
	if ($wancfg['dhcphostname']) {
1002
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
1003
	} else {
1004
		$dhclientconf_hostname = "";
1005
	}
1006

    
1007
 	$dhclientconf = "";
1008

    
1009
	$dhclientconf .= <<<EOD
1010
interface "{$wancfg['if']}" {
1011
	script "/sbin/dhclient-script";
1012
	{$dhclientconf_hostname}
1013
}
1014

    
1015
EOD;
1016

    
1017
	fwrite($fd, $dhclientconf);
1018
	fclose($fd);
1019
	
1020
	$wanif = $wancfg['if'];
1021
	
1022
        /* bring wan interface up before starting dhclient */
1023
        mwexec("/sbin/ifconfig {$wanif} up");
1024

    
1025
        /* fire up dhclient */
1026
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
1027

    
1028
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1029
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
1030
	fclose($fout);
1031

    
1032
	return 0;
1033
}
1034

    
1035
function interfaces_wan_dhcp_down() {
1036
	global $config;
1037
	$wancfg = $config['interfaces']['wan'];
1038
	$wanif = $wancfg['if'];
1039
	mwexec("/sbin/ifconfig {$wanif} delete");
1040
	sleep(1);
1041
}
1042

    
1043
function interfaces_dhcp_down($interface) {
1044
	global $config;
1045
	if(filter_translate_type_to_real_interface($interface) <> "")
1046
		$realinterface = filter_translate_type_to_real_interface($interface);
1047
	mwexec("/sbin/ifconfig {$realinterface} down");
1048
	sleep(1);
1049
	$pid = find_dhclient_process($interface);
1050
	if($pid)
1051
		mwexec("kill {$pid}");
1052
}
1053

    
1054
function interfaces_dhcp_up($interface) {
1055
	interfaces_dhcp_configure($interface);
1056
	sleep(1);
1057
}
1058

    
1059
function interfaces_wan_dhcp_up() {
1060
	interfaces_wan_dhcp_configure();
1061
	sleep(1);
1062
}
1063

    
1064
function interfaces_wan_pppoe_configure() {
1065
	global $config, $g;
1066

    
1067
	$wancfg = $config['interfaces']['wan'];
1068
	$pppoecfg = $config['pppoe'];
1069

    
1070
	/* 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

    
1077
	$idle = 0;
1078

    
1079
	if (isset($pppoecfg['ondemand'])) {
1080
		$ondemand = "enable";
1081
		if ($pppoecfg['timeout'])
1082
			$idle = $pppoecfg['timeout'];
1083
	} else {
1084
		$ondemand = "disable";
1085
	}
1086

    
1087
	$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

    
1095
EOD;
1096

    
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
	if (isset($pppoecfg['ondemand'])) {
1108
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
1109
			$mpdconf .= <<<EOD
1110
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
1111

    
1112
EOD;
1113
		} else {
1114
			$mpdconf .= <<<EOD
1115
	set iface addrs 192.0.2.112 192.0.2.113
1116

    
1117
EOD;
1118
		}
1119
	}
1120

    
1121
	$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

    
1134
EOD;
1135

    
1136
	if (isset($config['system']['dnsallowoverride'])) {
1137
		$mpdconf .= <<<EOD
1138
	set ipcp enable req-pri-dns
1139

    
1140
EOD;
1141
	}
1142

    
1143
	$mpdconf .= <<<EOD
1144
	open iface
1145

    
1146
EOD;
1147

    
1148
	fwrite($fd, $mpdconf);
1149
	fclose($fd);
1150

    
1151
	/* 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

    
1158
	$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
	
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
	}
1182

    
1183
        /* 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

    
1191
	unlink_if_exists("{$g['tmp_path']}/wanup");
1192

    
1193
	return 0;
1194
}
1195

    
1196
function interfaces_wan_pppoe_down() {
1197
	global $g;
1198
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1199
	sleep(1);
1200
}
1201

    
1202
function interfaces_wan_pppoe_up() {
1203
	global $g;
1204
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1205
	sleep(1);
1206
}
1207

    
1208
function interfaces_wan_pptp_configure() {
1209
	global $config, $g;
1210

    
1211
	$wancfg = $config['interfaces']['wan'];
1212
	$pptpcfg = $config['pptp'];
1213

    
1214
	/* 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

    
1221
	$idle = 0;
1222

    
1223
	if (isset($pptpcfg['ondemand'])) {
1224
		$ondemand = "enable";
1225
		if ($pptpcfg['timeout'])
1226
			$idle = $pptpcfg['timeout'];
1227
	} else {
1228
		$ondemand = "disable";
1229
	}
1230

    
1231
	$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

    
1241
	/*   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
	if (isset($pptpcfg['ondemand'])) {
1252
		$mpdconf .= <<<EOD
1253
	set iface addrs 10.0.0.1 10.0.0.2
1254

    
1255
EOD;
1256
	}
1257

    
1258
	$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

    
1270
EOD;
1271
	if (isset($config['system']['dnsallowoverride'])) {
1272
		$mpdconf .= <<<EOD
1273
	set ipcp enable req-pri-dns
1274

    
1275
EOD;
1276
	}
1277

    
1278
	$mpdconf .= <<<EOD
1279
	open
1280

    
1281
EOD;
1282

    
1283
	fwrite($fd, $mpdconf);
1284
	fclose($fd);
1285

    
1286
	/* 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

    
1293
	$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

    
1306
	/* configure interface */
1307
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1308
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1309

    
1310
	/* fire up mpd */
1311
	mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pptp");
1312

    
1313
	return 0;
1314
}
1315

    
1316
function interfaces_wan_pptp_down() {
1317
	global $g;
1318
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1319
	sleep(1);
1320
}
1321

    
1322
function interfaces_wan_pptp_up() {
1323
	global $g;
1324
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1325
	sleep(1);
1326
}
1327

    
1328
function interfaces_wan_bigpond_configure($curwanip) {
1329
	global $config, $g;
1330

    
1331
	$bpcfg = $config['bigpond'];
1332

    
1333
	if (!$curwanip) {
1334
		/* IP address not configured yet, exit */
1335
		return 0;
1336
	}
1337

    
1338
	/* kill bpalogin */
1339
	killbyname("bpalogin");
1340

    
1341
	/* wait a moment */
1342
	sleep(1);
1343

    
1344
	/* 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

    
1351
	/* 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

    
1358
	if (!$bpcfg['authserver'])
1359
		$bpcfg['authserver'] = "dce-server";
1360
	if (!$bpcfg['authdomain'])
1361
		$bpcfg['authdomain'] = $defaultdomain;
1362

    
1363
	$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

    
1378
	/* fire up bpalogin */
1379
	mwexec("/usr/local/sbin/bpalogin -c {$g['varetc_path']}/bpalogin.conf");
1380

    
1381
	return 0;
1382
}
1383

    
1384
function get_real_wan_interface() {
1385
	global $config, $g;
1386

    
1387
	$wancfg = $config['interfaces']['wan'];
1388

    
1389
	$wanif = $wancfg['if'];
1390
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1391
		$wanif = $g['pppoe_interface'];
1392
	}
1393

    
1394
	return $wanif;
1395
}
1396

    
1397
function get_current_wan_address($interface = "wan") {
1398
	global $config, $g;
1399

    
1400
	$wancfg = $config['interfaces'][$interface];
1401

    
1402
	$interface = filter_translate_type_to_real_interface($interface);
1403
	$ifinfo = "";
1404
	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
		/* dynamic WAN IP address, find out which one */
1419
		$wanif = get_real_wan_interface();
1420

    
1421
		/* get interface info with netstat */
1422
		exec("/usr/bin/netstat -nWI " . escapeshellarg($wanif) . " -f inet", $ifinfo);
1423

    
1424
		if (isset($ifinfo[1])) {
1425
			$aif = preg_split("/\s+/", $ifinfo[1]);
1426
			$curwanip = chop($aif[3]);
1427

    
1428
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1429
				return $curwanip;
1430
		}
1431

    
1432
		return null;
1433
	} else {
1434
		/* static WAN IP address */
1435
		return $wancfg['ipaddr'];
1436
	}
1437
}
1438

    
1439
/****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
		"em", "fxp", "hme", "lnc", "re", "rl", "ndis", "sf", "sis", "sk",
1455
		"tun", "vr", "wi", "xl", "vlan", "ste");
1456

    
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
function get_number_of_bridged_interfaces() {
1466
	$bridges_total = 0;
1467
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1468
	foreach($bridges as $bridge) {
1469
		$match_array = "";
1470
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1471
		if($match_array[1][0] <> "") {
1472
			if($match_array[1][0] > $bridges_total)
1473
				$bridges_total = $match_array[1][0];
1474
		}
1475
	}
1476
	return "{$bridges_total}";
1477
}
1478

    
1479
function get_next_available_bridge_interface() {
1480
	$bridges_total = get_number_of_bridged_interfaces();
1481
	$interfaces = `/sbin/ifconfig -l`;
1482
	$x=0;
1483
	for($x=0; $x<$bridges_total; $x++) {
1484
		if(!stristr($interfaces, "bridge{$x}")) {
1485
			return "{$x}";
1486
		}
1487
	}
1488
	return "{$x}";
1489
}
1490

    
1491
function destroy_bridge($bridge_num) {
1492
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1493
	sleep(1);
1494
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1495
	sleep(1);
1496
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1497
	sleep(1);
1498
	return;
1499
}
1500

    
1501
function discover_bridge($interface1, $interface2) {
1502
	if(!$interface1) return;
1503
	if(!$interface2) return;
1504
	$total_bridges = get_number_of_bridged_interfaces();
1505
	$interfaces = `/sbin/ifconfig -l`;
1506
	$x=0;
1507
	for($x=0; $x<$total_bridges; $x++) {
1508
		$bridge_text = "NA";
1509
		if(!stristr($interfaces, "bridge{$x}")) 
1510
			continue;
1511
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1512
		if(stristr($bridge_text, $interface1) == true and
1513
		   stristr($bridge_text, $interface2) == true) {
1514
			return "{$x}";			
1515
		}
1516
	}
1517
	return "-1";
1518
}
1519

    
1520
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
		$stack_list = "$awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
1529
		$format_list = "$awk '{print \$5 \" \" \$6 \",\" \$1}'";
1530

    
1531
		$interface_channels = "";
1532
		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
			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
			$c++;
1549
		}
1550
	}
1551
	return($wireless_modes);
1552
}
1553

    
1554
?>
(10-10/27)