Project

General

Profile

Download (42.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	interfaces.inc
5
	Copyright (C) 2004-2005 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 webgui */
197
		system_webgui_start();
198

    
199
		/* reload captive portal */
200
		captiveportal_configure();
201
	}
202

    
203
	return 0;
204
}
205

    
206
function interfaces_optional_configure() {
207
	global $config, $g;
208
	global $bridgeconfig;
209

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

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

    
218
		/* reload IPsec tunnels */
219
		vpn_ipsec_configure();
220

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

    
224
		/* restart dnsmasq */
225
		services_dnsmasq_configure();
226

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

    
231
	return 0;
232
}
233

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

    
238
	$bridges_total = get_next_available_bridge_interface();
239

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

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

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

    
254
		/* MAC spoofing? */
255
		if ($optcfg['spoofmac']) {
256
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
257
				" link " . escapeshellarg($optcfg['spoofmac']));
258
		} else {
259
			$mac = get_interface_mac_address($optcfg['if']);
260
			if($mac == "ff:ff:ff:ff:ff:ff") {
261
				/*   this is not a valid mac address.  generate a
262
				 *   temporary mac address so the machine can get online.
263
				 */
264
				echo "Generating new MAC address.";
265
				$random_mac = generate_random_mac_address();
266
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
267
					" link " . escapeshellarg($random_mac));
268
				$optcfg['spoofmac'] = $random_mac;
269
				write_config();
270
				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");
271
			}
272
		}
273

    
274
		/* media */
275
		if ($optcfg['media'] || $optcfg['mediaopt']) {
276
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
277
			if ($optcfg['media'])
278
				$cmd .= " media " . escapeshellarg($optcfg['media']);
279
			if ($optcfg['mediaopt'])
280
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
281
			mwexec($cmd);
282
		}
283

    
284
		/* OpenVPN configuration? */
285
 		if (isset($optcfg['ovpn'])) {
286
 			if (strstr($optcfg['if'], "tap"))
287
 				ovpn_link_tap();
288
 		}
289

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

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

    
304
			if(!is_interface_wireless($optcfg['if']) and
305
			   !is_interface_wireless($config['interfaces'][$optcfg['bridge']]['if'])) 
306
				mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$optcfg['bridge']]['if']} stp {$optcfg['if']}");
307

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

    
343
function interfaces_carp_configure() {
344
	global $g, $config, $debugging;
345
	if(isset($config['system']['developerspew'])) {
346
		$mt = microtime();
347
		echo "interfaces_carp_configure() being called $mt\n";
348
	}
349
	$carp_instances_counter = 0;
350
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
351
	/* destroy previous interfaces */
352
	for($x=0; $x<$total_carp_interfaces_defined; $x++) 
353
		mwexec("/sbin/ifconfig carp{$x} delete");	
354
	if ($g['booting']) {
355
		echo "Configuring CARP interfaces...";
356
		mute_kernel_msgs();
357
	}
358
	/* suck in configuration items */
359
	if($config['installedpackages']['carpsettings']['config']) {
360
		foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
361
			$pfsyncenabled = $carp['pfsyncenabled'];
362
			$balanacing = $carp['balancing'];
363
			$pfsyncinterface = $carp['pfsyncinterface'];
364
		}
365
	} else {
366
		unset($pfsyncinterface);
367
		unset($balanacing);
368
		unset($pfsyncenabled);
369
	}
370
	if($balanacing) {
371
		mwexec("/sbin/sysctl net.inet.carp.arpbalance=1");
372
		mwexec("/sbin/sysctl net.inet.carp.preempt=0");
373
	} else {
374
		mwexec("/sbin/sysctl net.inet.carp.preempt=1");
375
	}
376
	$carp_sync_int = convert_friendly_interface_to_real_interface_name($pfsyncinterface);
377
	if($g['booting']) {
378
		/*    install rules to alllow pfsync to sync up during boot
379
		 *    carp interfaces will remain down until the bootup sequence finishes
380
		 */
381
		exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
382
		exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
383
		exec("echo pass out proto { tcp, udp } from any to any port 53 keep state >> /tmp/rules.boot");
384
		exec("/sbin/pfctl -f /tmp/rules.boot");
385
	}			
386
	/* setup pfsync interface */
387
	if($carp_sync_int) 	
388
		mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up");
389
	else 
390
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
391
	$fd = fopen("/tmp/carp.sh", "w");
392
	if($config['virtualip']['vip']) { 
393
		$viparr = &$config['virtualip']['vip'];
394
		mwexec("/sbin/sysctl net.inet.carp.allow=1");
395
	} else {
396
		$viparr = array();
397
		mwexec("/sbin/sysctl net.inet.carp.allow=0");
398
	}
399
	foreach ($viparr as $vip) {
400
		if ($vip['mode'] == "carp") {
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
	/* set values for /path/program */
477
	$hostapd = "/usr/sbin/hostapd";
478
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
479
	$ifconfig = "/sbin/ifconfig ";
480
	$killall = "/usr/bin/killall ";
481

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

    
484
	/* Set a/b/g standard */
485
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
486

    
487
	/* Set 802.11g protection mode */
488
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
489

    
490
	/* set wireless channel value */
491
	if(isset($wlcfg['channel']))
492
		$channel = "channel " . escapeshellarg($wlcfg['channel']);
493

    
494
	/* set Distance value */
495
	if($wlcfg['distance']) 
496
		$distance = escapeshellarg($wlcfg['distance']);
497

    
498
	/* Set ssid */
499
	if($wlcfg['ssid'])  
500
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
501
		
502
	/* Set wireless hostap mode */
503
	if ($wlcfg['mode'] == "hostap")
504
		$hostapmode = "mediaopt hostap";
505
	else
506
		$hostapmode = "-mediaopt hostap";
507

    
508
	/* Set wireless adhoc mode */
509
	if ($wlcfg['mode'] == "adhoc")
510
		$adhocmode = "mediaopt adhoc";
511
	else
512
		$adhocmode = "-mediaopt adhoc";
513

    
514
	/* Not neccesary to set BSS mode as this is default if adhoc and/or hostap is NOT set */
515

    
516
	/* handle hide ssid option */
517
	if(isset($wlcfg['hidessid']['enable']))
518
		$hidessid = "hidessid";
519
	else
520
		$hidessid = "-hidessid";
521

    
522
	/* handle pureg (802.11g) only option */
523
	if(isset($wlcfg['pureg']['enable']))
524
		$pureg = "mode 11g pureg";
525
	else
526
		$pureg = "-pureg";
527

    
528
	/* enable apbridge option */
529
	if(isset($wlcfg['apbridge']['enable']))
530
		$apbridge = "apbridge";
531
	else
532
		$apbridge = "-apbridge";
533

    
534
	/* handle turbo option */
535
	if(isset($wlcfg['turbo']['enable']))
536
		$turbo = "mediaopt turbo";
537
	else
538
		$turbo = "-mediaopt turbo";
539

    
540
	/* handle txpower setting */
541
	if($wlcfg['txpower'] <> "")
542
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
543
	
544
	/* handle wme option */
545
	if(isset($wlcfg['wme']['enable']))
546
		$wme = "wme";
547
	else
548
		$wme = "-wme";
549
	
550
	/* set up wep if enabled */
551
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
552
                $wepset .= "authmode shared wepmode on ";
553
                $i = 1;
554
                foreach ($wlcfg['wep']['key'] as $wepkey) {
555
                        $wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
556
                        if (isset($wepkey['txkey'])) {
557
                                $wepset .= "weptxkey {$i} ";
558
                        }
559
                        $i++;
560
                }
561
        } else {
562
                $wepset = "authmode open wepmode off";
563
	}
564

    
565
	/* generate wpa_supplicant/hostap config if wpa is enabled */
566

    
567
	switch ($wlcfg['mode']) {
568
		case 'bss':
569
			if (isset($wlcfg['wpa']['enable'])) {
570

    
571
				$wpa .= <<<EOD
572
ctrl_interface={$g['varrun_path']}/wpa_supplicant
573
ctrl_interface_group=0
574
ap_scan=1
575
#fast_reauth=1
576
network={
577
ssid="{$wlcfg['ssid']}"
578
scan_ssid=1
579
priority=5
580
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
581
psk="{$wlcfg['wpa']['passphrase']}"
582
pairwise={$wlcfg['wpa']['wpa_pairwise']}
583
group={$wlcfg['wpa']['wpa_pairwise']}
584
}
585
EOD;
586

    
587
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
588
				fwrite($fd, "{$wpa}");
589
				fclose($fd);
590

    
591
				mwexec("{$killall} wpa_supplicant");
592
			}
593
		break;
594

    
595
		case 'hostap':
596
			if (isset($wlcfg['wpa']['enable'])) {
597
				$wpa .= <<<EOD
598
interface={$if}
599
driver=bsd
600
logger_syslog=-1
601
logger_syslog_level=0
602
logger_stdout=-1
603
logger_stdout_level=0
604
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
605
ctrl_interface={$g['varrun_path']}/hostapd
606
ctrl_interface_group=wheel
607
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
608
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
609
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
610
ssid={$wlcfg['ssid']}
611
debug={$wlcfg['wpa']['debug_mode']}
612
auth_algs={$wlcfg['wpa']['auth_algs']}
613
wpa={$wlcfg['wpa']['wpa_mode']}
614
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
615
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
616
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
617
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
618
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
619
wpa_passphrase={$wlcfg['wpa']['passphrase']}
620
ieee8021x={$wlcfg['wpa']['ieee8021x']}
621
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
622
#rsn_preauth=1
623
#rsn_preauth_interfaces=eth0
624
EOD;
625

    
626
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
627
				fwrite($fd, "{$wpa}");
628
				fclose($fd);
629

    
630
				mwexec("{$killall} hostapd");
631
			}
632
		break;
633

    
634
		case 'adhoc':
635
			mwexec("{$killall} hostapd");
636
			mwexec("{$killall} wpa_supplicant");
637
		break;
638
	}	
639

    
640
	/* all variables are set, lets start up everything */
641
	
642
	/* set ack timers according to users preference (if he/she has any) */
643

    
644
	if($distance) {
645
		mwexec("/sbin/athctrl.sh -i {$if} -d {$distance}");
646
	}
647

    
648
	mwexec("$ifconfig $if" . " -mediaopt hostap,turbo");	// Fix bug with turbomode and reboot (hopefully)        
649
	mwexec("$ifconfig $if" . " down"); 
650
	mwexec("$ifconfig $if" . " " . $standard);
651
	mwexec("$ifconfig $if" . " " . $protmode);
652
	mwexec("$ifconfig $if" . " " . $channel);
653
	mwexec("$ifconfig $if" . " " . $ssid);
654
	mwexec("$ifconfig $if" . " " . $adhocmode);
655
	/* fix turbo mode and reboot */
656
	mwexec("$ifconfig $if" . " up"); 
657
	mwexec("$ifconfig $if" . " " . $hostapmode);
658
	mwexec("$ifconfig $if" . " " . $turbo);
659
	mwexec("$ifconfig $if" . " down"); 
660
	/* fix turbo mode and reboot */
661
	mwexec("$ifconfig $if" . " " . $hidessid);
662
	mwexec("$ifconfig $if" . " " . $pureg);
663
	mwexec("$ifconfig $if" . " " . $apbridge);
664
	mwexec("$ifconfig $if" . " " . $wme);
665
	mwexec("$ifconfig $if" . " " . $wepset);
666
	mwexec("$ifconfig $if" . " " . $txpower);
667
	mwexec("$ifconfig $if" . " up");
668

    
669
	if (isset($wlcfg['wpa']['enable'])) {
670
		if ($wlcfg['mode'] == bss) 
671
			mwexec("{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf");
672
		if ($wlcfg['mode'] == hostap) 
673
			mwexec("{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf");
674
	}
675

    
676

    
677
	if(isset($wlcfg['useolsr']))
678
		setup_wireless_olsr($if);
679

    
680
	return 0;
681

    
682
}
683

    
684
function find_dhclient_process($interface) {
685
	if(filter_translate_type_to_real_interface($interface) <> "")
686
        	$realinterface = filter_translate_type_to_real_interface($interface);
687
	$pid = `/usr/bin/pgrep -f "dhclient: {$realinterface}(\$| .*)"`;
688
	return $pid;
689
}
690

    
691
function interfaces_wan_configure() {
692
	global $config, $g, $bridges_total;
693

    
694
	$wancfg = $config['interfaces']['wan'];
695

    
696
	if(!$g['booting']) {
697
		mute_kernel_msgs();
698

    
699
		/* find dhclient process for wan and kill it */
700
		killbypid(find_dhclient_process("wan"));
701

    
702
		/* remove wanup file if it exists */
703
		unlink_if_exists("{$g['tmp_path']}/wanup");
704

    
705
		/* kill PPPoE client (mpd) */
706
		killbypid("{$g['varrun_path']}/mpd.pid");
707

    
708
		/* wait for processes to die */
709
		sleep(3);
710

    
711
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
712
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
713
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
714
		unlink_if_exists("{$g['vardb_path']}/wanip");
715
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
716
	}
717

    
718
	/* remove all addresses first */
719
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
720
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
721

    
722
	/* wireless configuration? */
723
	if (is_array($wancfg['wireless']))
724
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
725

    
726
	if ($wancfg['spoofmac']) {
727
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
728
			" link " . escapeshellarg($wancfg['spoofmac']));
729
	}  else {
730
		$mac = get_interface_mac_address($wancfg['if']);
731
		if($mac == "ff:ff:ff:ff:ff:ff") {
732
			/*   this is not a valid mac address.  generate a
733
			 *   temporary mac address so the machine can get online.
734
			 */
735
			echo "Generating new MAC address.";
736
			$random_mac = generate_random_mac_address();
737
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
738
				" link " . escapeshellarg($random_mac));
739
			$wancfg['spoofmac'] = $random_mac;
740
			write_config();
741
			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");
742
		}
743
	}
744

    
745
	/* media */
746
	if ($wancfg['media'] || $wancfg['mediaopt']) {
747
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
748
		if ($wancfg['media'])
749
			$cmd .= " media " . escapeshellarg($wancfg['media']);
750
		if ($wancfg['mediaopt'])
751
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
752
		mwexec($cmd);
753
	}
754

    
755
	switch ($wancfg['ipaddr']) {
756

    
757
		case 'dhcp':
758
			interfaces_wan_dhcp_configure();
759
			break;
760

    
761
		case 'pppoe':
762
			interfaces_wan_pppoe_configure();
763
			break;
764

    
765
		case 'pptp':
766
			interfaces_wan_pptp_configure();
767
			break;
768

    
769
		case 'bigpond':
770
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
771
			interfaces_wan_dhcp_configure();
772
			break;
773

    
774
		default:
775
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
776
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
777
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
778
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
779
			} else {
780
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
781
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
782
			}
783
			/* install default route */
784
			mwexec("/sbin/route delete default");
785
			mwexec("/sbin/route add default " . escapeshellarg($config['interfaces']['wan']['gateway']));
786

    
787
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
788
			filter_configure();
789
	}
790

    
791
	if ($wancfg['bridge']) {
792
		/* use open/netBSD style bridge */
793
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
794
		
795
		/* force all bridged interfaces to use same mtu */
796
		$mtu = get_interface_mtu($config['interfaces'][$wancfg['bridge']]['if']);
797
		mwexec("/sbin/ifconfig {$wancfg['if']} mtu {$mtu}");
798
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}");
799
		
800
		/* assign items to a bridge */
801
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}");
802

    
803
		if(!is_interface_wireless($wancfg['if']) and
804
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if'])) 
805
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$wancfg['bridge']]['if']} stp {$wancfg['if']}");
806
		
807
		/* log commands run for debugging in /tmp/ */
808
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$wancfg['if']}", "w");
809
		fwrite($fd, "/sbin/ifconfig {$wancfg['if']} mtu {$mtu}\n");
810
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}\n");
811
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
812
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}\n");
813
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$wancfg['if']} stp {$config['interfaces'][$wancfg['bridge']]['if']}\n");
814
		fclose($fd);
815
		
816
		/* bring up interfaces */
817
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
818
		usleep(100);
819
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} up");
820
		usleep(5);
821
		mwexec("/sbin/ifconfig {$wancfg['if']} up");
822
		usleep(5);
823
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
824
		
825
		$bridges_total++;
826
	}
827

    
828
	if (!$g['booting']) {
829
		/* reconfigure static routes (kernel may have deleted them) */
830
		system_routing_configure();
831

    
832
		/* set the reload filter dity flag */
833
		touch("{$g['tmp_path']}/filter_dirty");
834

    
835
		/* reload ipsec tunnels */
836
		vpn_ipsec_configure();
837

    
838
		/* restart ez-ipupdate */
839
		services_dyndns_configure();
840

    
841
		/* force DNS update */
842
		services_dnsupdate_process();
843

    
844
		/* restart dnsmasq */
845
		services_dnsmasq_configure();
846
	}
847

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

    
850
	unmute_kernel_msgs();
851

    
852
	return 0;
853
}
854

    
855
function interfaces_opt_dhcp_configure($interface) {
856
	global $config, $g;
857

    
858
	$optcfg = $config['interfaces'][$interface];
859
	$optif = $optcfg['if'];
860

    
861
	/* generate dhclient_wan.conf */
862
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
863
	if (!$fd) {
864
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
865
		return 1;
866
	}
867

    
868
	if ($optcfg['dhcphostname']) {
869
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
870
	} else {
871
		$dhclientconf_hostname = "";
872
	}
873

    
874
 	$dhclientconf = "";
875

    
876
	$dhclientconf .= <<<EOD
877
interface "{$optif}" {
878
	send host-name "{$optcfg['dhcphostname']}";
879
	script "/sbin/dhclient-script";
880
	{$dhclientconf_hostname}
881
}
882

    
883
EOD;
884

    
885
	fwrite($fd, $dhclientconf);
886
	fclose($fd);
887

    
888
        /* bring interface up before starting dhclient */
889
        mwexec("/sbin/ifconfig {$optif} up");
890

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

    
894
	return 0;
895
}
896

    
897
function interfaces_dhcp_configure($interface) {
898
	global $config, $g;
899

    
900
	if(filter_translate_type_to_real_interface($interface) <> "")
901
        	$realinterface = filter_translate_type_to_real_interface($interface);
902

    
903
	$optcfg = $config['interfaces'][$interface];
904

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

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

    
918
 	$dhclientconf = "";
919

    
920
	$dhclientconf .= <<<EOD
921
interface "{$realinterface}" {
922
	script "/sbin/dhclient-script";
923
	{$dhclientconf_hostname}
924
}
925

    
926
EOD;
927

    
928
	fwrite($fd, $dhclientconf);
929
	fclose($fd);
930
	
931
	$optif = $optcfg['if'];
932
	
933
        /* bring wan interface up before starting dhclient */
934
        mwexec("/sbin/ifconfig {$optif} up");
935

    
936
        /* fire up dhclient */
937
        mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif} >/tmp/{$optif}_output >/tmp/{$optif}_error_output");
938

    
939
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
940
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
941
	fclose($fout);
942

    
943
	return 0;
944
}
945

    
946
function interfaces_wan_dhcp_configure() {
947
	global $config, $g;
948

    
949
	$wancfg = $config['interfaces']['wan'];
950

    
951
	/* generate dhclient_wan.conf */
952
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
953
	if (!$fd) {
954
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
955
		return 1;
956
	}
957
	
958
	if ($wancfg['dhcphostname']) {
959
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
960
	} else {
961
		$dhclientconf_hostname = "";
962
	}
963

    
964
 	$dhclientconf = "";
965

    
966
	$dhclientconf .= <<<EOD
967
interface "{$wancfg['if']}" {
968
	script "/sbin/dhclient-script";
969
	{$dhclientconf_hostname}
970
}
971

    
972
EOD;
973

    
974
	fwrite($fd, $dhclientconf);
975
	fclose($fd);
976
	
977
	$wanif = $wancfg['if'];
978
	
979
        /* bring wan interface up before starting dhclient */
980
        mwexec("/sbin/ifconfig {$wanif} up");
981

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

    
985
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
986
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
987
	fclose($fout);
988

    
989
	return 0;
990
}
991

    
992
function interfaces_wan_dhcp_down() {
993
	global $config;
994
	$wancfg = $config['interfaces']['wan'];
995
	$wanif = $wancfg['if'];
996
	mwexec("/sbin/ifconfig {$wanif} delete");
997
	sleep(1);
998
}
999

    
1000
function interfaces_dhcp_down($interface) {
1001
	global $config;
1002
	if(filter_translate_type_to_real_interface($interface) <> "")
1003
		$realinterface = filter_translate_type_to_real_interface($interface);
1004
	mwexec("/sbin/ifconfig {$realinterface} down");
1005
	sleep(1);
1006
	$pid = find_dhclient_process($interface);
1007
	if($pid)
1008
		mwexec("kill {$pid}");
1009
}
1010

    
1011
function interfaces_dhcp_up($interface) {
1012
	interfaces_dhcp_configure($interface);
1013
	sleep(1);
1014
}
1015

    
1016
function interfaces_wan_dhcp_up() {
1017
	interfaces_wan_dhcp_configure();
1018
	sleep(1);
1019
}
1020

    
1021
function interfaces_wan_pppoe_configure() {
1022
	global $config, $g;
1023

    
1024
	$wancfg = $config['interfaces']['wan'];
1025
	$pppoecfg = $config['pppoe'];
1026

    
1027
	/* generate mpd.conf */
1028
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1029
	if (!$fd) {
1030
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
1031
		return 1;
1032
	}
1033

    
1034
	$idle = 0;
1035

    
1036
	if (isset($pppoecfg['ondemand'])) {
1037
		$ondemand = "enable";
1038
		if ($pppoecfg['timeout'])
1039
			$idle = $pppoecfg['timeout'];
1040
	} else {
1041
		$ondemand = "disable";
1042
	}
1043

    
1044
	$mpdconf = <<<EOD
1045
pppoe:
1046
	new -i ng0 pppoe pppoe
1047
	set iface route default
1048
	set iface {$ondemand} on-demand
1049
	set iface idle {$idle}
1050
	set iface up-script /usr/local/sbin/ppp-linkup
1051

    
1052
EOD;
1053

    
1054
	/*    Check for ppp-linkdown Script in /usr/local/sbin
1055
	 *    Create reference in mpd.conf
1056
	 */
1057
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1058
		$mpdconf .= <<<EOD
1059
	set iface down-script /usr/local/sbin/ppp-linkdown
1060

    
1061
EOD;
1062
	}
1063

    
1064
	if (isset($pppoecfg['ondemand'])) {
1065
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
1066
			$mpdconf .= <<<EOD
1067
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
1068

    
1069
EOD;
1070
		} else {
1071
			$mpdconf .= <<<EOD
1072
	set iface addrs 192.0.2.112 192.0.2.113
1073

    
1074
EOD;
1075
		}
1076
	}
1077

    
1078
	$mpdconf .= <<<EOD
1079
	set bundle disable multilink
1080
	set bundle authname "{$pppoecfg['username']}"
1081
	set bundle password "{$pppoecfg['password']}"
1082
	set link keep-alive 10 60
1083
	set link max-redial 0
1084
	set link no acfcomp protocomp
1085
	set link disable pap chap
1086
	set link accept chap
1087
	set link mtu 1492
1088
	set ipcp yes vjcomp
1089
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1090

    
1091
EOD;
1092

    
1093
	if (isset($config['system']['dnsallowoverride'])) {
1094
		$mpdconf .= <<<EOD
1095
	set ipcp enable req-pri-dns
1096

    
1097
EOD;
1098
	}
1099

    
1100
	$mpdconf .= <<<EOD
1101
	open iface
1102

    
1103
EOD;
1104

    
1105
	fwrite($fd, $mpdconf);
1106
	fclose($fd);
1107

    
1108
	/* generate mpd.links */
1109
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1110
	if (!$fd) {
1111
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
1112
		return 1;
1113
	}
1114

    
1115
	$mpdconf = <<<EOD
1116
pppoe:
1117
	set link type pppoe
1118
	set pppoe iface {$wancfg['if']}
1119
	set pppoe service "{$pppoecfg['provider']}"
1120
	set pppoe enable originate
1121
	set pppoe disable incoming
1122

    
1123
EOD;
1124

    
1125
	fwrite($fd, $mpdconf);
1126
	fclose($fd);
1127
	
1128
	if(file_exists("{$g['varrun_path']}/mpd.pid") and $g['booting']) {
1129
		/* if we are booting and mpd has already been started then don't start again. */
1130
	} else {
1131
		/* if mpd is active, lets take it down */
1132
		if(file_exists("{$g['varrun_path']}/mpd.pid")) {
1133
			killbypid("{$g['varrun_path']}/mpd.pid");
1134
			sleep(3);
1135
		}
1136
		/* fire up mpd */	
1137
		mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pppoe");
1138
	}
1139

    
1140
        /* sleep until wan is up - or 30 seconds, whichever comes first */
1141
	for ($count = 0; $count < 30; $count++) {
1142
		if(file_exists("{$g['tmp_path']}/wanup")) {
1143
			break;
1144
		}
1145
		sleep(1);
1146
	}
1147

    
1148
	unlink_if_exists("{$g['tmp_path']}/wanup");
1149

    
1150
	return 0;
1151
}
1152

    
1153
function interfaces_wan_pppoe_down() {
1154
	global $g;
1155
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1156
	sleep(1);
1157
}
1158

    
1159
function interfaces_wan_pppoe_up() {
1160
	global $g;
1161
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1162
	sleep(1);
1163
}
1164

    
1165
function interfaces_wan_pptp_configure() {
1166
	global $config, $g;
1167

    
1168
	$wancfg = $config['interfaces']['wan'];
1169
	$pptpcfg = $config['pptp'];
1170

    
1171
	/* generate mpd.conf */
1172
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1173
	if (!$fd) {
1174
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1175
		return 1;
1176
	}
1177

    
1178
	$idle = 0;
1179

    
1180
	if (isset($pptpcfg['ondemand'])) {
1181
		$ondemand = "enable";
1182
		if ($pptpcfg['timeout'])
1183
			$idle = $pptpcfg['timeout'];
1184
	} else {
1185
		$ondemand = "disable";
1186
	}
1187

    
1188
	$mpdconf = <<<EOD
1189
pptp:
1190
	new -i ng0 pptp pptp
1191
	set iface route default
1192
	set iface {$ondemand} on-demand
1193
	set iface idle {$idle}
1194
	set iface up-script /usr/local/sbin/ppp-linkup
1195

    
1196
EOD;
1197

    
1198
	/*   Check for ppp-linkdown Script in /usr/local/sbin
1199
	 *   Create reference in mpd.conf
1200
	 */
1201
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1202
		$mpdconf .= <<<EOD
1203
	set iface down-script /usr/local/sbin/ppp-linkdown
1204

    
1205
EOD;
1206
	}
1207

    
1208
	if (isset($pptpcfg['ondemand'])) {
1209
		$mpdconf .= <<<EOD
1210
	set iface addrs 10.0.0.1 10.0.0.2
1211

    
1212
EOD;
1213
	}
1214

    
1215
	$mpdconf .= <<<EOD
1216
	set bundle disable multilink
1217
	set bundle authname "{$pptpcfg['username']}"
1218
	set bundle password "{$pptpcfg['password']}"
1219
	set link keep-alive 10 60
1220
	set link max-redial 0
1221
	set link no acfcomp protocomp
1222
	set link disable pap chap
1223
	set link accept chap
1224
	set ipcp no vjcomp
1225
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1226

    
1227
EOD;
1228

    
1229
	if (isset($config['system']['dnsallowoverride'])) {
1230
		$mpdconf .= <<<EOD
1231
	set ipcp enable req-pri-dns
1232

    
1233
EOD;
1234
	}
1235

    
1236
	$mpdconf .= <<<EOD
1237
	open
1238

    
1239
EOD;
1240

    
1241
	fwrite($fd, $mpdconf);
1242
	fclose($fd);
1243

    
1244
	/* generate mpd.links */
1245
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1246
	if (!$fd) {
1247
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1248
		return 1;
1249
	}
1250

    
1251
	$mpdconf = <<<EOD
1252
pptp:
1253
	set link type pptp
1254
	set pptp enable originate outcall
1255
	set pptp disable windowing
1256
	set pptp self {$pptpcfg['local']}
1257
	set pptp peer {$pptpcfg['remote']}
1258

    
1259
EOD;
1260

    
1261
	fwrite($fd, $mpdconf);
1262
	fclose($fd);
1263

    
1264
	/* configure interface */
1265
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1266
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1267

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

    
1271
	return 0;
1272
}
1273

    
1274
function interfaces_wan_pptp_down() {
1275
	global $g;
1276
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1277
	sleep(1);
1278
}
1279

    
1280
function interfaces_wan_pptp_up() {
1281
	global $g;
1282
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1283
	sleep(1);
1284
}
1285

    
1286
function interfaces_wan_bigpond_configure($curwanip) {
1287
	global $config, $g;
1288

    
1289
	$bpcfg = $config['bigpond'];
1290

    
1291
	if (!$curwanip) {
1292
		/* IP address not configured yet, exit */
1293
		return 0;
1294
	}
1295

    
1296
	/* kill bpalogin */
1297
	killbyname("bpalogin");
1298

    
1299
	/* wait a moment */
1300
	sleep(1);
1301

    
1302
	/* get the default domain */
1303
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1304
	if ($nfd) {
1305
		$defaultdomain = trim(fgets($nfd));
1306
		fclose($nfd);
1307
	}
1308

    
1309
	/* generate bpalogin.conf */
1310
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1311
	if (!$fd) {
1312
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1313
		return 1;
1314
	}
1315

    
1316
	if (!$bpcfg['authserver'])
1317
		$bpcfg['authserver'] = "dce-server";
1318
	if (!$bpcfg['authdomain'])
1319
		$bpcfg['authdomain'] = $defaultdomain;
1320

    
1321
	$bpconf = <<<EOD
1322
username {$bpcfg['username']}
1323
password {$bpcfg['password']}
1324
authserver {$bpcfg['authserver']}
1325
authdomain {$bpcfg['authdomain']}
1326
localport 5050
1327

    
1328
EOD;
1329

    
1330
	if ($bpcfg['minheartbeatinterval'])
1331
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1332

    
1333
	fwrite($fd, $bpconf);
1334
	fclose($fd);
1335

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

    
1339
	return 0;
1340
}
1341

    
1342
function get_real_wan_interface() {
1343
	global $config, $g;
1344

    
1345
	$wancfg = $config['interfaces']['wan'];
1346

    
1347
	$wanif = $wancfg['if'];
1348
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1349
		$wanif = $g['pppoe_interface'];
1350
	}
1351

    
1352
	return $wanif;
1353
}
1354

    
1355
function get_current_wan_address($interface = "wan") {
1356
	global $config, $g;
1357

    
1358
	$wancfg = $config['interfaces'][$interface];
1359

    
1360
	$interface = filter_translate_type_to_real_interface($interface);
1361
	$ifinfo = "";
1362
	if(in_array($wancfg['ipaddr'], array('dhcp'))) {
1363
		/* get interface info with netstat */
1364
		exec("/usr/bin/netstat -nWI " . escapeshellarg($interface) . " -f inet", $ifinfo);
1365

    
1366
		if (isset($ifinfo[1])) {
1367
			$aif = preg_split("/\s+/", $ifinfo[1]);
1368
			$curwanip = chop($aif[3]);
1369

    
1370
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1371
				return $curwanip;
1372
		}
1373

    
1374
		return null;		
1375
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1376
		/* dynamic WAN IP address, find out which one */
1377
		$wanif = get_real_wan_interface();
1378

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

    
1382
		if (isset($ifinfo[1])) {
1383
			$aif = preg_split("/\s+/", $ifinfo[1]);
1384
			$curwanip = chop($aif[3]);
1385

    
1386
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1387
				return $curwanip;
1388
		}
1389

    
1390
		return null;
1391
	} else {
1392
		/* static WAN IP address */
1393
		return $wancfg['ipaddr'];
1394
	}
1395
}
1396

    
1397
/****f* interfaces/is_altq_capable
1398
 * NAME
1399
 *   is_altq_capable - Test if interface is capable of using ALTQ
1400
 * INPUTS
1401
 *   $int            - string containing interface name
1402
 * RESULT
1403
 *   boolean         - true or false
1404
 ******/
1405

    
1406
function is_altq_capable($int) {
1407
        /* Per:
1408
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1409
         * Only the following drivers have ALTQ support
1410
         */
1411
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1412
		"em", "fxp", "hme", "lnc", "ndis", "rl", "sf", "sis", "sk",
1413
		"tun", "vr", "wi", "xl", "vlan", "ste");
1414

    
1415
        $int_family = preg_split("/[0-9]+/", $int);
1416

    
1417
        if (in_array($int_family[0], $capable))
1418
                return true;
1419
        else
1420
                return false;
1421
}
1422

    
1423
function get_number_of_bridged_interfaces() {
1424
	$bridges_total = 0;
1425
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1426
	foreach($bridges as $bridge) {
1427
		$match_array = "";
1428
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1429
		if($match_array[1][0] <> "") {
1430
			if($match_array[1][0] > $bridges_total)
1431
				$bridges_total = $match_array[1][0];
1432
		}
1433
	}
1434
	return "{$bridges_total}";
1435
}
1436

    
1437
function get_next_available_bridge_interface() {
1438
	$bridges_total = get_number_of_bridged_interfaces();
1439
	$interfaces = `/sbin/ifconfig -l`;
1440
	$x=0;
1441
	for($x=0; $x<$bridges_total; $x++) {
1442
		if(!stristr($interfaces, "bridge{$x}")) {
1443
			return "{$x}";
1444
		}
1445
	}
1446
	return "{$x}";
1447
}
1448

    
1449
function destroy_bridge($bridge_num) {
1450
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1451
	sleep(1);
1452
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1453
	sleep(1);
1454
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1455
	sleep(1);
1456
	return;
1457
}
1458

    
1459
function discover_bridge($interface1, $interface2) {
1460
	if(!$interface1) return;
1461
	if(!$interface2) return;
1462
	$total_bridges = get_number_of_bridged_interfaces();
1463
	$interfaces = `/sbin/ifconfig -l`;
1464
	$x=0;
1465
	for($x=0; $x<$total_bridges; $x++) {
1466
		$bridge_text = "NA";
1467
		if(!stristr($interfaces, "bridge{$x}")) 
1468
			continue;
1469
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1470
		if(stristr($bridge_text, $interface1) == true and
1471
		   stristr($bridge_text, $interface2) == true) {
1472
			return "{$x}";			
1473
		}
1474
	}
1475
	return "-1";
1476
}
1477

    
1478
?>
(10-10/27)