Project

General

Profile

Download (52.9 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
require_once("globals.inc");
41

    
42
function interfaces_loopback_configure() {
43
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
44

    
45
	return 0;
46
}
47

    
48
function interfaces_vlan_configure() {
49
	global $config, $g;
50

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

    
53
		/* devices with native VLAN support */
54
		$vlan_native_supp = $g['vlan_native_supp'];
55

    
56
		/* devices with long frame support */
57
		$vlan_long_frame = $g['vlan_long_frame'];
58

    
59
		/* sweep through and axe old interfaces */
60
		$vlan_count = get_number_of_vlan_interfaces();
61
		for($x=0; $x<$vlan_count; $x++)
62
			exec("/sbin/ifconfig vlan{$x} down destroy");
63

    
64
		$i = 0;
65

    
66
		foreach ($config['vlans']['vlan'] as $vlan) {
67

    
68
			$cmd = "/sbin/ifconfig vlan{$i} create vlan " .
69
				escapeshellarg($vlan['tag']) . " vlandev " .
70
				escapeshellarg($vlan['if']);
71

    
72
			/* get driver name */
73
			for ($j = 0; $j < strlen($vlan['if']); $j++) {
74
				if ($vlan['if'][$j] >= '0' && $vlan['if'][$j] <= '9')
75
					break;
76
			}
77
			$drvname = substr($vlan['if'], 0, $j);
78

    
79
			if (in_array($drvname, $vlan_native_supp))
80
				$cmd .= " link0";
81
			else if (in_array($drvname, $vlan_long_frame))
82
				$cmd .= " mtu 1500";
83

    
84
			mwexec($cmd);
85

    
86
			/* invalidate interface cache */
87
			get_interface_arr(true);
88

    
89
			/*   all vlans need to spoof their parent mac address, too.  see
90
			 *   ticket #1514: http://cvstrac.pfsense.com/tktview?tn=1514,33 
91
			 */
92
			foreach($config['interfaces'] as $interfaces) {
93
				if($interfaces['if'] == $vlan['if']) {
94
					if($interfaces['spoofmac']) {
95
						mwexec("/sbin/ifconfig " . escapeshellarg($interfaces['if']) .
96
							" link " . escapeshellarg($interfaces['spoofmac']));
97
					}
98
				}
99
			}
100

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

    
104
			$i++;
105
		}
106
	}
107

    
108
	return 0;
109
}
110

    
111
function interfaces_lan_configure() {
112
	global $config, $g;
113

    
114
	$bridges_total = get_next_available_bridge_interface();
115

    
116
	$lancfg = $config['interfaces']['lan'];
117

    
118
	/* if user has removed ip address, clear it*/
119
	if($lancfg['ipaddr'] == "")
120
		mwexec("/sbin/ifconfig {$lancfg['if']} delete");
121

    
122
	/* wireless configuration? */
123
	if (is_array($lancfg['wireless']))
124
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
125

    
126
	/* MAC spoofing? */
127
	if ($lancfg['spoofmac']) {
128
		mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
129
			" link " . escapeshellarg($lancfg['spoofmac']));
130
	} else {
131
		$mac = get_interface_mac_address($lancfg['if']);
132
		if($mac == "ff:ff:ff:ff:ff:ff") {
133
			/*   this is not a valid mac address.  generate a
134
			 *   temporary mac address so the machine can get online.
135
			 */
136
			echo "Generating new MAC address.";
137
			$random_mac = generate_random_mac_address();
138
			mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
139
				" link " . escapeshellarg($random_mac));
140
			$lancfg['spoofmac'] = $random_mac;
141
			write_config();
142
			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");
143
		}
144
	}
145

    
146
	/* bridged? */
147

    
148
	if ($lancfg['bridge']) {
149
		/* use open/netBSD style bridge */
150
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
151

    
152
		/* force all bridged interfaces to use same mtu */
153
		$mtu = get_interface_mtu($config['interfaces'][$lancfg['bridge']]['if']);
154
		mwexec("/sbin/ifconfig {$lancfg['if']} mtu {$mtu}");
155
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}");
156

    
157
		/* assign items to a bridge */
158
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}");
159

    
160
		if(!is_interface_wireless($lancfg['if']) and
161
		   !is_interface_wireless($config['interfaces'][$lancfg['bridge']]['if']))
162
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$lancfg['bridge']]['if']} stp {$lancfg['if']}");
163

    
164
		/* log commands run for debugging in /tmp/ */
165
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
166
		fwrite($fd, "/sbin/ifconfig {$lancfg['if']} mtu {$mtu}\n");
167
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}\n");
168
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
169
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}\n");
170
		if(!is_interface_wireless($lancfg['if']) and
171
		   !is_interface_wireless($config['interfaces'][$lancfg['bridge']]['if']))		
172
				fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}\n");
173
		fclose($fd);
174

    
175
		/* bring up interfaces */
176
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
177
		usleep(100);
178
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} up");
179
		usleep(5);
180
		mwexec("/sbin/ifconfig {$lancfg['if']} up");
181
		usleep(5);
182
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
183

    
184
		$bridges_total++;
185
		/* update cache */
186
		if ($bridges_total != find_number_of_created_bridges())
187
			find_number_of_created_bridges(true);
188
	}
189

    
190
	/* media */
191
	if ($lancfg['media'] || $lancfg['mediaopt']) {
192
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
193
		if ($lancfg['media'])
194
			$cmd .= " media " . escapeshellarg($lancfg['media']);
195
		if ($lancfg['mediaopt'])
196
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
197
		mwexec($cmd);
198
	}
199

    
200
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
201
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
202

    
203
	if (!$g['booting']) {
204
		/* make new hosts file */
205
		system_hosts_generate();
206

    
207
		/* reconfigure static routes (kernel may have deleted them) */
208
		system_routing_configure();
209

    
210
		/* set the reload filter dity flag */
211
		touch("{$g['tmp_path']}/filter_dirty");
212

    
213
		/* reload IPsec tunnels */
214
		vpn_ipsec_configure();
215

    
216
		/* reload dhcpd (gateway may have changed) */
217
		services_dhcpd_configure();
218

    
219
		/* reload dnsmasq */
220
		services_dnsmasq_configure();
221

    
222
		/* reload captive portal */
223
		captiveportal_configure();
224

    
225
	}
226

    
227
	return 0;
228
}
229

    
230
function interfaces_optional_configure() {
231
	global $config, $g;
232
	global $bridgeconfig;
233

    
234
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
235
		interfaces_optional_configure_if($i);
236
	}
237

    
238
	if (!$g['booting']) {
239
		/* reconfigure static routes (kernel may have deleted them) */
240
		system_routing_configure();
241

    
242
		/* reload IPsec tunnels */
243
		vpn_ipsec_configure();
244

    
245
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
246
		services_dhcpd_configure();
247

    
248
		/* restart dnsmasq */
249
		services_dnsmasq_configure();
250

    
251
		/* reload captive portal */
252
		captiveportal_configure();
253

    
254
		/* set the reload filter dity flag */
255
		touch("{$g['tmp_path']}/filter_dirty");
256
	}
257

    
258
	return 0;
259
}
260

    
261
function interfaces_optional_configure_if($opti) {
262
	global $config, $g;
263
	global $bridgeconfig, $debugging;
264

    
265
	$bridges_total = get_next_available_bridge_interface();
266

    
267
	$optcfg = $config['interfaces']['opt' . $opti];
268

    
269
	if ($g['booting']) {
270
		$optdescr = "";
271
		if ($optcfg['descr'])
272
			$optdescr = " ({$optcfg['descr']})";
273
		print "\tOPT{$opti}{$optdescr}... ";
274
	}
275
	
276
	if(file_exists("/tmp/{$optcfg['if']}_router"))
277
		unlink("/tmp/{$optcfg['if']}_router");
278

    
279
	if (isset($optcfg['enable'])) {
280
		if($optcfg['gateway'])
281
			system("echo " . $optcfg['gateway'] . " > /tmp/" . $optcfg['if'] . "_router");
282

    
283
		/* wireless configuration? */
284
		if (is_array($optcfg['wireless']))
285
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
286

    
287
		/* PPP configuration */
288
		if (isset($optcfg['pointtopoint']))
289
			interfaces_ppp_configure_if($optcfg);
290

    
291
		/* MAC spoofing? */
292
		if ($optcfg['spoofmac']) {
293
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
294
				" link " . escapeshellarg($optcfg['spoofmac']));
295
		} else {
296
			$mac = get_interface_mac_address($optcfg['if']);
297
			if($mac == "ff:ff:ff:ff:ff:ff") {
298
				/*   this is not a valid mac address.  generate a
299
				 *   temporary mac address so the machine can get online.
300
				 */
301
				echo "Generating new MAC address.";
302
				$random_mac = generate_random_mac_address();
303
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
304
					" link " . escapeshellarg($random_mac));
305
				$optcfg['spoofmac'] = $random_mac;
306
				write_config();
307
				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");
308
			}
309
		}
310

    
311
		/* media */
312
		if ($optcfg['media'] || $optcfg['mediaopt']) {
313
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
314
			if ($optcfg['media'])
315
				$cmd .= " media " . escapeshellarg($optcfg['media']);
316
			if ($optcfg['mediaopt'])
317
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
318
			mwexec($cmd);
319
		}
320

    
321
		/* bridged? */
322
		if ($optcfg['bridge']) {
323
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
324
                        /* use open/netBSD style bridge */
325
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
326

    
327
			/* invalidate interface cache */
328
			get_interface_arr(true);
329

    
330
			/* force all bridged interfaces to use same mtu */
331
			$mtu = get_interface_mtu($config['interfaces'][$optcfg['bridge']]['if']);
332
			mwexec("/sbin/ifconfig {$optcfg['if']} mtu {$mtu}");
333
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}");
334

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

    
338
			if(!is_interface_wireless($optcfg['if']) and
339
			   !is_interface_wireless($config['interfaces'][$optcfg['bridge']]['if']))
340
				mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$optcfg['bridge']]['if']} stp {$optcfg['if']}");
341

    
342
			/* log commands run for debugging in /tmp/ */
343
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
344
			fwrite($fd, "/sbin/ifconfig {$optcfg['if']} mtu {$mtu}\n");
345
			fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}\n");
346
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
347
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up\n");
348
			if(!is_interface_wireless($optcfg['if']) and
349
			   !is_interface_wireless($config['interfaces'][$optcfg['bridge']]['if']))
350
					fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} stp {$config['interfaces'][$optcfg['bridge']]['if']}\n");
351
			fclose($fd);
352

    
353
			/* bring up interfaces */
354
			mwexec("/sbin/ifconfig bridge{$bridges_total} down");
355
			usleep(100);
356
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} up");
357
			usleep(5);
358
			mwexec("/sbin/ifconfig {$optcfg['if']} up");
359
			usleep(5);
360
			mwexec("/sbin/ifconfig bridge{$bridges_total} up");
361

    
362
			$bridges_total++;
363
			/* update cache */
364
			if ($bridges_total != find_number_of_created_bridges())
365
				find_number_of_created_bridges(true);
366
		} else {
367
			/* if user has selected DHCP type then act accordingly */
368
			if($optcfg['ipaddr'] == "dhcp") {
369
				interfaces_opt_dhcp_configure("opt{$opti}");
370
			} else {
371
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
372
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
373
			}
374
		}
375
	} else {
376
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
377
	}
378
	return 0;
379
}
380

    
381
function interfaces_ppp_configure_if($ifcfg) {
382
	global $config;
383
	
384
	if(file_exists("/var/run/ppp0.pid")) {
385
		$pid = file_get_contents("/var/run/ppp0.pid");
386
		mwexec('kill $pid');
387
	}
388
	
389
	mwexec("/sbin/ifconfig ppp0 down destroy");
390

    
391
	$peerfile = "lcp-echo-failure 0\n";
392
	$peerfile .= "lcp-echo-interval 0\n";
393
	$peerfile .= "connect /etc/ppp/peers/ppp0-connect-chat\n";
394
	//$peerfile .= "disconnect /etc/ppp/peers/ppp0-disconnect-chat\n";
395
	$peerfile .= "/dev/{$ifcfg['serialport']}\n";
396
	$peerfile .= "crtscts\n";
397
	$peerfile .= "local\n";
398
	$peerfile .= ":{$ifcfg['gateway']}\n";
399
	$peerfile .= "noipdefault\n";
400
	$peerfile .= "ipcp-accept-local\n";
401
	$peerfile .= "novj\n";
402
	$peerfile .= "nobsdcomp\n";
403
	$peerfile .= "novjccomp\n";
404
	$peerfile .= "nopcomp\n";
405
	$peerfile .= "noaccomp\n";
406
	$peerfile .= "noauth\n";
407
	$peerfile .= "persist\n";
408
	$peerfile .= "debug\n";
409
	// KD - test
410
	//$peerfile .= "defaultroute\n";
411
	//$peerfile .= "nodetach\n";
412
	// KD - so I know where to look!
413
	$peerfile .= "# created by /etc/inc/interfaces.inc\n";
414
	file_put_contents("/etc/ppp/peers/ppp0", $peerfile);
415

    
416
	// Added single quotes to some strings below:
417
	// the \rAT is *always* going to need it
418
	// and the phone number on a GSM connection ends in a # char
419
	// Kevin Dawson, 22 Jan 2008
420
	// Refer Andrew Curtis
421
			
422
	$chatfile = "#!/bin/sh\n";
423
	$chatfile .= "exec chat \\\n";
424
	$chatfile .= "TIMEOUT 5 \\\n";
425
	$chatfile .= "ECHO ON \\\n";
426
	$chatfile .= "ABORT '\\nBUSY\\r' \\\n";
427
	$chatfile .= "ABORT '\\nERROR\\r' \\\n";
428
	$chatfile .= "ABORT '\\nNO ANSWER\\r' \\\n";
429
	$chatfile .= "ABORT '\\nNO CARRIER\\r' \\\n";
430
	$chatfile .= "ABORT '\\nNO DIALTONE\\r' \\\n";
431
	$chatfile .= "ABORT '\\nRINGING\\r\\n\\r\\nRINGING\\r' \\\n";
432
	// KD
433
	$chatfile .= "'' '\\rAT' \\\n";
434
	$chatfile .= "TIMEOUT 12 \\\n";
435
	$chatfile .= "OK ATH \\\n";
436
	$chatfile .= "OK ATE1 \\\n";
437
	$chatfile .= "OK 'AT+CGDCONT=1,\"IP\",\"{$ifcfg['ap']}\"' \\\n";
438
	// KD
439
	$chatfile .= "OK 'ATD{$ifcfg['phone']}' \\\n";
440
	$chatfile .= "TIMEOUT 22 \\\n";
441
	$chatfile .= "CONNECT \"\" \\\n";
442
	$chatfile .= "SAY \"\\nConnected.\"\n";
443
	file_put_contents("/etc/ppp/peers/ppp0-connect-chat", $chatfile);
444
	chmod("/etc/ppp/peers/ppp0-connect-chat", 0755);
445
	mwexec("/sbin/ifconfig ppp0 create");
446
	return 0;
447
}
448

    
449
function interfaces_carp_configure() {
450
	global $g, $config, $debugging;
451
	$balanacing = "";
452
	$pfsyncinterface = "";
453
	$pfsyncenabled = "";
454
	if(isset($config['system']['developerspew'])) {
455
		$mt = microtime();
456
		echo "interfaces_carp_configure() being called $mt\n";
457
	}
458
	$carp_instances_counter = 0;
459
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
460
	/* destroy previous interfaces */
461
	for($x=0; $x<$total_carp_interfaces_defined; $x++)
462
		mwexec("/sbin/ifconfig carp{$x} delete");
463
	if ($g['booting']) {
464
		echo "Configuring CARP interfaces...";
465
		mute_kernel_msgs();
466
	}
467
	/* suck in configuration items */
468
	if($config['installedpackages']['carpsettings']) 
469
		if($config['installedpackages']['carpsettings']['config']) {
470
		foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
471
			$pfsyncenabled = $carp['pfsyncenabled'];
472
			$balanacing = $carp['balancing'];
473
			$pfsyncinterface = $carp['pfsyncinterface'];
474
			$pfsyncpeerip = $carp['pfsyncpeerip'];
475
		}
476
	} else {
477
		unset($pfsyncinterface);
478
		unset($balanacing);
479
		unset($pfsyncenabled);
480
	}
481
	if($balanacing) {
482
		mwexec("/sbin/sysctl net.inet.carp.arpbalance=1");
483
		mwexec("/sbin/sysctl net.inet.carp.preempt=0");
484
	} else {
485
		mwexec("/sbin/sysctl net.inet.carp.preempt=1");
486
	}
487
	$carp_sync_int = convert_friendly_interface_to_real_interface_name($pfsyncinterface);
488
	if($g['booting']) {
489
		/*    install rules to alllow pfsync to sync up during boot
490
		 *    carp interfaces will remain down until the bootup sequence finishes
491
		 */
492
		exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
493
		exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
494
		exec("echo pass out proto { tcp, udp } from any to any port 53 keep state >> /tmp/rules.boot");
495
		exec("/sbin/pfctl -f /tmp/rules.boot");
496
	}
497
	/* setup pfsync interface */
498
	if($carp_sync_int and $pfsyncenabled) {
499
		if($pfsyncpeerip) {
500
			mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} syncpeer {$pfsyncpeerip} up");
501
		} else {
502
			mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up");
503
		}
504
	} else {
505
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
506
	}
507
	$fd = fopen("/tmp/carp.sh", "w");
508
	if($config['virtualip']['vip']) {
509
		$viparr = &$config['virtualip']['vip'];
510
		mwexec("/sbin/sysctl net.inet.carp.allow=1");
511
	} else {
512
		$viparr = array();
513
		mwexec("/sbin/sysctl net.inet.carp.allow=0");
514
	}
515
	foreach ($viparr as $vip) {
516
		if ($vip['mode'] == "carp") {
517
			$vip_password = $vip['password'];
518
			$vip_password = str_replace(" ", "", $vip_password);
519

    
520
			/* ensure CARP IP really exists prior to loading up */
521
			$found = false;
522
			$iflist = array("lan", "wan");
523
			for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++)
524
				$iflist['opt' . $i] = 'opt' . $i;
525
			foreach($iflist as $if) {
526
				/* ignore down or "disabled" interfaces */
527
				if($if <> "lan" and $if <> "wan")
528
					if (!isset($config['interfaces'][$if]['enable'])) 
529
						continue;								
530
				$ww_subnet_ip = $config['interfaces'][$if]['ipaddr'];
531
				$ww_subnet_bits = $config['interfaces'][$if]['subnet'];
532
				if (ip_in_subnet($vip['subnet'], gen_subnet($ww_subnet_ip, $ww_subnet_bits) . "/" . $ww_subnet_bits))
533
					$found = true;
534
			}
535
			if($found == false) {
536
				file_notice("CARP", "Sorry but we could not find a matching real interface subnet for the virtual IP address {$vip['subnet']}.", "Firewall: Virtual IP", "");
537
				continue;
538
			}			
539
			/* create the carp interface and setup */
540
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
541

    
542
			/* invalidate interface cache */
543
			get_interface_arr(true);
544

    
545
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
546
			if($vip['password'] != "")
547
				$password = " pass \"" . $vip_password . "\"";
548
			if($debugging)
549
				echo "Configuring carp{$carp_instances_counter}.\n";
550
			fwrite($fd, "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password . "\n");
551
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password);
552
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
553
			fwrite($fd, "/sbin/ifconfig carp" . $carp_instances_counter . " up\n");
554
			usleep(10);
555
			$carp_instances_counter++;
556
		}
557
	}
558
	fclose($fd);
559
	mwexec("/bin/sh /tmp/carp.sh");
560
	if ($g['booting']) {
561
		unmute_kernel_msgs();
562
		echo "done.\n";
563
	}
564

    
565
	/* update cache */
566
	if ($carp_instances_counter != find_number_of_created_carp_interfaces())
567
		find_number_of_created_carp_interfaces(true);
568
}
569

    
570
function interfaces_carp_bring_up_final() {
571
	global $config, $g, $debugging;
572
	if(isset($config['system']['developerspew'])) {
573
		$mt = microtime();
574
		echo "interfaces_carp_bring_up_final() being called $mt\n";
575
	}
576
	if(!$config['virtualip']['vip'])
577
		return;
578
	$viparr = &$config['virtualip']['vip'];
579
	/* could not locate an array, return */
580
	if(!is_array($viparr))
581
		return;
582
	$carp_instances_counter = 0;
583
	$counter = 0;
584
	if($g['booting'])
585
		echo "Waiting for final CARP interface bringup...";
586
	$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
587
	if($g['booting']) {
588
		while($supress > 0) {
589
			sleep(2);
590
			$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
591
			if($counter > 15)
592
				$supress = 0;
593
			$counter++;
594
			echo ".";
595
		}
596
		echo " done.\n";
597
	}
598
	foreach ($viparr as $vip) {
599
		/* bail if this isn't a carp VIP */
600
		if ($vip['mode'] != "carp")
601
			continue;
602

    
603
		if($debugging)
604
			echo "Upping interface carp{$carp_instances_counter}.\n";
605
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
606
		if($vip['password'] != "")
607
			$password = " pass " . $vip['password'];
608
		if($debugging)
609
			echo "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password . "\n";
610
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
611
		sleep(1);
612
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
613
		$carp_instances_counter++;
614
	}
615
	if($g['booting'])
616
		echo " done.\n";
617
}
618

    
619
function interfaces_ipalias_configure() {
620
	global $g, $config, $debugging;
621
	if(isset($config['system']['developerspew'])) {
622
		$mt = microtime();
623
		echo "interfaces_ipalias_configure() being called $mt\n";
624
	}
625
	$viparr = &$config['virtualip']['vip'];
626
	if(is_array($viparr)) {
627
		foreach ($viparr as $vip) {
628
			if ($vip['mode'] == "ipalias") {
629
				$if = $vip['interface'];
630
				mwexec("/sbin/ifconfig " . escapeshellarg($config['interfaces'][$if]['if']) . " " . $vip['subnet'] . "/" . escapeshellarg($vip['subnet_bits']) . " alias"); 
631
			}
632
		}
633
	}
634
}
635

    
636
function interfaces_wireless_configure($if, $wlcfg) {
637
	global $config, $g;
638

    
639
	/*    open up a shell script that will be used to output the commands.
640
	 *    since wireless is changing a lot, these series of commands are fragile
641
     *    and will sometimes need to be verified by a operator by executing the command
642
     *    and returning the output of the command to the developers for inspection.  please
643
     *    do not change this routine from a shell script to individul exec commands.  -sullrich
644
	 */
645

    
646
	conf_mount_rw();
647

    
648
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
649

    
650
	$fd_set = fopen("/tmp/{$if}_setup.sh","w");
651
	fwrite($fd_set, "#!/bin/sh\n");
652
	fwrite($fd_set, "# {$g['product_name']} wireless configuration script.\n\n");
653

    
654
	fwrite($fd_set, "# enable shell debugging\n");
655
	fwrite($fd_set, "set -x\n");
656

    
657
	/* set values for /path/program */
658
	$hostapd = "/usr/sbin/hostapd";
659
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
660
	$ifconfig = "/sbin/ifconfig";
661
	$killall = "/usr/bin/killall";
662

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

    
665
	/* Set a/b/g standard */
666
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
667

    
668
	/* Set 802.11g protection mode */
669
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
670

    
671
	/* set wireless channel value */
672
	if(isset($wlcfg['channel']))
673
		$channel = "channel " . escapeshellarg($wlcfg['channel']);
674

    
675
	/* set Distance value */
676
	if($wlcfg['distance'])
677
		$distance = escapeshellarg($wlcfg['distance']);
678

    
679
	/* Set ssid */
680
	if($wlcfg['ssid'])
681
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
682

    
683
	/* Set wireless hostap mode */
684
	if ($wlcfg['mode'] == "hostap")
685
		$hostapmode = "mediaopt hostap";
686
	else
687
		$hostapmode = "-mediaopt hostap";
688

    
689
	/* Set wireless adhoc mode */
690
	if ($wlcfg['mode'] == "adhoc")
691
		$adhocmode = "mediaopt adhoc";
692
	else
693
		$adhocmode = "-mediaopt adhoc";
694

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

    
697
	/* handle hide ssid option */
698
	if(isset($wlcfg['hidessid']['enable']))
699
		$hidessid = "hidessid";
700
	else
701
		$hidessid = "-hidessid";
702

    
703
	/* handle pureg (802.11g) only option */
704
	if(isset($wlcfg['pureg']['enable']))
705
		$pureg = "mode 11g pureg";
706
	else
707
		$pureg = "-pureg";
708

    
709
	/* enable apbridge option */
710
	if(isset($wlcfg['apbridge']['enable']))
711
		$apbridge = "apbridge";
712
	else
713
		$apbridge = "-apbridge";
714

    
715
	/* handle turbo option */
716
	if(isset($wlcfg['turbo']['enable']))
717
		$turbo = "mediaopt turbo";
718
	else
719
		$turbo = "-mediaopt turbo";
720

    
721
	/* handle txpower setting */
722
	if($wlcfg['txpower'] <> "")
723
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
724

    
725
	/* handle wme option */
726
	if(isset($wlcfg['wme']['enable']))
727
		$wme = "wme";
728
	else
729
		$wme = "-wme";
730

    
731
	/* set up wep if enabled */
732
    if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
733
		if($wlcfg['wpa']['auth_algs'] == "1")
734
			$wepset .= "authmode open wepmode on ";
735
		else if($wlcfg['wpa']['auth_algs'] == "2")
736
			$wepset .= "authmode shared wepmode on ";
737
		else if($wlcfg['wpa']['auth_algs'] == "3")
738
			$wepset .= "authmode mixed wepmode on ";
739
		$i = 1;
740
		foreach ($wlcfg['wep']['key'] as $wepkey) {
741
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
742
			if (isset($wepkey['txkey']))
743
				$wepset .= "weptxkey {$i} ";
744
			$i++;
745
		}
746
    } else {
747
    	$wepset .= "authmode open wepmode off ";
748
	}
749

    
750
	/* generate wpa_supplicant/hostap config if wpa is enabled */
751

    
752
	switch ($wlcfg['mode']) {
753
		case 'bss':
754
			if (isset($wlcfg['wpa']['enable'])) {
755

    
756
				$wpa .= <<<EOD
757
ctrl_interface={$g['varrun_path']}/wpa_supplicant
758
ctrl_interface_group=0
759
ap_scan=1
760
#fast_reauth=1
761
network={
762
ssid="{$wlcfg['ssid']}"
763
scan_ssid=1
764
priority=5
765
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
766
psk="{$wlcfg['wpa']['passphrase']}"
767
pairwise={$wlcfg['wpa']['wpa_pairwise']}
768
group={$wlcfg['wpa']['wpa_pairwise']}
769
}
770
EOD;
771

    
772
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
773
				fwrite($fd, "{$wpa}");
774
				fclose($fd);
775

    
776
				fwrite($fd_set, kill_wpasupplicant($if));
777
			}
778
		break;
779

    
780
		case 'hostap':
781
			if (isset($wlcfg['wpa']['enable'])) {
782
				$wpa .= <<<EOD
783
interface={$if}
784
driver=bsd
785
logger_syslog=-1
786
logger_syslog_level=0
787
logger_stdout=-1
788
logger_stdout_level=0
789
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
790
ctrl_interface={$g['varrun_path']}/hostapd
791
ctrl_interface_group=wheel
792
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
793
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
794
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
795
ssid={$wlcfg['ssid']}
796
debug={$wlcfg['wpa']['debug_mode']}
797
auth_algs={$wlcfg['wpa']['auth_algs']}
798
wpa={$wlcfg['wpa']['wpa_mode']}
799
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
800
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
801
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
802
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
803
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
804
wpa_passphrase={$wlcfg['wpa']['passphrase']}
805
ieee8021x={$wlcfg['wpa']['ieee8021x']}
806
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
807
#rsn_preauth=1
808
#rsn_preauth_interfaces=eth0
809
EOD;
810

    
811
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
812
				fwrite($fd, "{$wpa}");
813
				fclose($fd);
814

    
815
				fwrite($fd_set, kill_hostapd($if));
816
			}
817
		break;
818

    
819
		case 'adhoc':
820
			fwrite($fd_set, kill_hostapd($if));
821
			fwrite($fd_set, kill_wpasupplicant($if));
822
		break;
823
	}
824

    
825
	/*
826
	 *    all variables are set, lets start up everything
827
     */
828

    
829
	/* set ack timers according to users preference (if he/she has any) */
830
	if($distance) {
831
		fwrite($fd_set, "# Enable ATH distance settings\n");
832
		fwrite($fd_set, "/sbin/athctrl.sh -i {$if} -d {$distance}\n");
833
	}
834

    
835
	$standard_no_turbo = str_replace(" Turbo", "", $standard);
836

    
837
	$settings = <<<EOD
838

    
839
{$ifconfig} {$if} down
840
{$ifconfig} {$if} {$hostapmode}
841
{$ifconfig} {$if} {$standard_no_turbo}
842
{$ifconfig} {$if} {$channel}
843
{$ifconfig} {$if} {$turbo}
844
{$ifconfig} {$if} {$ssid}
845
{$ifconfig} {$if} {$hidessid}
846
{$ifconfig} {$if} {$adhocmode}
847
{$ifconfig} {$if} {$protmode}
848
{$ifconfig} {$if} {$pureg}
849
{$ifconfig} {$if} {$apbridge}
850
{$ifconfig} {$if} {$wme}
851
{$ifconfig} {$if} {$wepset}
852
{$ifconfig} {$if} {$txpower}
853
{$ifconfig} {$if} up
854

    
855
EOD;
856

    
857
	/* write out above <<EOD stuff */
858
	fwrite($fd_set, $settings);
859

    
860
	if (isset($wlcfg['wpa']['enable'])) {
861
		if ($wlcfg['mode'] == "bss")
862
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
863
		if ($wlcfg['mode'] == "hostap")
864
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
865
	}
866

    
867
	fclose($fd_set);
868

    
869
	conf_mount_ro();
870

    
871
	/* execute commands now in shell */
872
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
873
	sleep(2);
874
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
875

    
876
	return 0;
877

    
878
}
879

    
880
function kill_hostapd($interface) {
881
	return "/bin/ps awwuxx | grep hostapd | grep $interface | awk '{ print \$2 }' | xargs kill\n";
882
}
883

    
884
function kill_wpasupplicant($interface) {
885
	return "/bin/ps awwuxx | grep wpa_supplicant | grep $interface | awk '{ print \$2 }' | xargs kill\n";
886
}
887

    
888
function find_dhclient_process($interface) {
889
	if(filter_translate_type_to_real_interface($interface) <> "")
890
        	$realinterface = filter_translate_type_to_real_interface($interface);
891
	if($realinterface)
892
		$pid = `ps awwwux | grep dhclient | grep -v grep | grep {$realinterface} | awk '{ print \$2 }'`;
893
	return $pid;
894
}
895

    
896
function interfaces_wan_configure() {
897
	global $config, $g, $bridges_total;
898

    
899
	$wancfg = $config['interfaces']['wan'];
900

    
901
	if(file_exists("/tmp/{$config['interfaces']['wan']['if']}_router")) 
902
		unlink("/tmp/{$config['interfaces']['wan']['if']}_router");
903

    
904
	if(!$g['booting']) {
905
		mute_kernel_msgs();
906

    
907
		/* find dhclient process for wan and kill it */
908
		killbypid(find_dhclient_process("wan"));
909

    
910
		/* remove wanup file if it exists */
911
		unlink_if_exists("{$g['tmp_path']}/wanup");
912

    
913
		/* kill PPPoE client (mpd) */
914
		killbypid("{$g['varrun_path']}/mpd.pid");
915

    
916
		/* wait for processes to die */
917
		sleep(3);
918

    
919
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
920
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
921
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
922
		unlink_if_exists("{$g['vardb_path']}/wanip");
923
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
924
	}
925

    
926
	/* remove all addresses first */
927
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
928
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
929

    
930
	/* wireless configuration? */
931
	if (is_array($wancfg['wireless']))
932
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
933

    
934
	if ($wancfg['spoofmac']) {
935
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
936
			" link " . escapeshellarg($wancfg['spoofmac']));
937
	}  else {
938
		$mac = get_interface_mac_address($wancfg['if']);
939
		if($mac == "ff:ff:ff:ff:ff:ff") {
940
			/*   this is not a valid mac address.  generate a
941
			 *   temporary mac address so the machine can get online.
942
			 */
943
			echo "Generating new MAC address.";
944
			$random_mac = generate_random_mac_address();
945
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
946
				" link " . escapeshellarg($random_mac));
947
			$wancfg['spoofmac'] = $random_mac;
948
			write_config();
949
			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");
950
		}
951
	}
952

    
953
	/* media */
954
	if ($wancfg['media'] || $wancfg['mediaopt']) {
955
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
956
		if ($wancfg['media'])
957
			$cmd .= " media " . escapeshellarg($wancfg['media']);
958
		if ($wancfg['mediaopt'])
959
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
960
		mwexec($cmd);
961
	}
962

    
963
	switch ($wancfg['ipaddr']) {
964

    
965
		case 'dhcp':
966
			interfaces_wan_dhcp_configure();
967
			break;
968

    
969
		case 'pppoe':
970
			interfaces_wan_pppoe_configure();
971
			break;
972

    
973
		case 'pptp':
974
			interfaces_wan_pptp_configure();
975
			break;
976

    
977
		case 'bigpond':
978
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
979
			interfaces_wan_dhcp_configure();
980
			break;
981

    
982
		default:
983
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
984
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
985
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
986
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
987
			} else {
988
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
989
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
990
			}
991

    
992
			if($config['interfaces']['wan']['gateway'])
993
				system("echo " . $config['interfaces']['wan']['gateway'] . " > /tmp/" . $config['interfaces']['wan']['if'] . "_router");
994

    
995
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
996
			filter_configure();
997
	}
998

    
999
	if ($wancfg['bridge']) {
1000
		/* use open/netBSD style bridge */
1001
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
1002

    
1003
		/* invalidate interface cache */
1004
		get_interface_arr(true);
1005

    
1006
		/* force all bridged interfaces to use same mtu */
1007
		$mtu = get_interface_mtu($config['interfaces'][$wancfg['bridge']]['if']);
1008
		mwexec("/sbin/ifconfig {$wancfg['if']} mtu {$mtu}");
1009
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}");
1010

    
1011
		/* assign items to a bridge */
1012
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}");
1013

    
1014
		if(!is_interface_wireless($wancfg['if']) and
1015
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if']))
1016
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$wancfg['bridge']]['if']} stp {$wancfg['if']}");
1017

    
1018
		/* log commands run for debugging in /tmp/ */
1019
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$wancfg['if']}", "w");
1020
		fwrite($fd, "/sbin/ifconfig {$wancfg['if']} mtu {$mtu}\n");
1021
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}\n");
1022
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
1023
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}\n");
1024
		if(!is_interface_wireless($wancfg['if']) and
1025
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if']))
1026
				fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$wancfg['if']} stp {$config['interfaces'][$wancfg['bridge']]['if']}\n");
1027
		fclose($fd);
1028

    
1029
		/* bring up interfaces */
1030
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
1031
		usleep(100);
1032
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} up");
1033
		usleep(5);
1034
		mwexec("/sbin/ifconfig {$wancfg['if']} up");
1035
		usleep(5);
1036
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
1037

    
1038
		$bridges_total++;
1039
		/* update cache */
1040
		if ($bridges_total != find_number_of_created_bridges())
1041
			find_number_of_created_bridges(true);
1042
	}
1043

    
1044
	if (!$g['booting']) {
1045
		/* reconfigure static routes (kernel may have deleted them) */
1046
		system_routing_configure();
1047

    
1048
		/* set the reload filter dity flag */
1049
		touch("{$g['tmp_path']}/filter_dirty");
1050

    
1051
		/* reload ipsec tunnels */
1052
		vpn_ipsec_configure();
1053

    
1054
		/* restart ez-ipupdate */
1055
		services_dyndns_configure();
1056

    
1057
		/* force DNS update */
1058
		services_dnsupdate_process();
1059

    
1060
		/* restart dnsmasq */
1061
		services_dnsmasq_configure();
1062

    
1063
		/* reload captive portal */
1064
		captiveportal_configure();
1065
	}
1066

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

    
1069
	unmute_kernel_msgs();
1070

    
1071
	return 0;
1072
}
1073

    
1074
function interfaces_opt_dhcp_configure($interface) {
1075
	global $config, $g;
1076

    
1077
	$optcfg = $config['interfaces'][$interface];
1078
	$optif = $optcfg['if'];
1079

    
1080
	/* generate dhclient_wan.conf */
1081
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
1082
	if (!$fd) {
1083
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
1084
		return 1;
1085
	}
1086

    
1087
	if ($optcfg['dhcphostname']) {
1088
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
1089
		$dhclientconf_hostname .= "\tsend host-name \"{$optcfg['dhcphostname']}\";\n";
1090
	} else {
1091
		$dhclientconf_hostname = "";
1092
	}
1093

    
1094
 	$dhclientconf = "";
1095

    
1096
	$dhclientconf .= <<<EOD
1097
timeout 60;
1098
retry 1;
1099
select-timeout 0;
1100
initial-interval 1;
1101
interface "{$optif}" {
1102
	script "/sbin/dhclient-script";
1103
	{$dhclientconf_hostname}
1104
}
1105

    
1106
EOD;
1107

    
1108
if(is_ipaddr($optcfg['alias-address'])) {
1109
	$subnetmask = gen_subnet_mask($optcfg['alias-subnet']);
1110
	$dhclientconf .= <<<EOD
1111
alias {
1112
	interface  "{$optif}";
1113
	fixed-address {$optcfg['alias-address']};
1114
	option subnet-mask {$subnetmask};
1115
}
1116

    
1117
EOD;
1118
}
1119
	fwrite($fd, $dhclientconf);
1120
	fclose($fd);
1121

    
1122
        /* bring interface up before starting dhclient */
1123
        mwexec("/sbin/ifconfig {$optif} up");
1124

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

    
1128
	return 0;
1129
}
1130

    
1131
function interfaces_dhcp_configure($interface) {
1132
	global $config, $g;
1133

    
1134
	if(filter_translate_type_to_real_interface($interface) <> "")
1135
        	$realinterface = filter_translate_type_to_real_interface($interface);
1136

    
1137
	$optcfg = $config['interfaces'][$interface];
1138

    
1139
	/* generate dhclient_$interface.conf */
1140
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
1141
	if (!$fd) {
1142
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
1143
		return 1;
1144
	}
1145

    
1146
	if ($optcfg['dhcphostname']) {
1147
		$dhclientconf_hostname =  "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
1148
		$dhclientconf_hostname .= "\tsend host-name \"{$optcfg['dhcphostname']}\";\n";
1149
	} else {
1150
		$dhclientconf_hostname = "";
1151
	}
1152

    
1153
 	$dhclientconf = "";
1154

    
1155
	$dhclientconf .= <<<EOD
1156
timeout 60;
1157
retry 1;
1158
select-timeout 0;
1159
initial-interval 1;
1160
interface "{$realinterface}" {
1161
	{$dhclientconf_hostname}
1162
	script "/sbin/dhclient-script";
1163
}
1164

    
1165
EOD;
1166

    
1167
if(is_ipaddr($optcfg['alias-address'])) {
1168
	$subnetmask = gen_subnet_mask($optcfg['alias-subnet']);
1169
	$dhclientconf .= <<<EOD
1170
alias {
1171
	interface  "{$optif}";
1172
	fixed-address {$optcfg['alias-address']};
1173
	option subnet-mask {$subnetmask};
1174
}
1175

    
1176
EOD;
1177
}
1178

    
1179
	fwrite($fd, $dhclientconf);
1180
	fclose($fd);
1181

    
1182
	$optif = $optcfg['if'];
1183

    
1184
        /* bring wan interface up before starting dhclient */
1185
        mwexec("/sbin/ifconfig {$optif} up");
1186

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

    
1190
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
1191
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
1192
	fclose($fout);
1193

    
1194
	return 0;
1195
}
1196

    
1197
function interfaces_wan_dhcp_configure() {
1198
	global $config, $g;
1199

    
1200
	$wancfg = $config['interfaces']['wan'];
1201

    
1202
	/* generate dhclient_wan.conf */
1203
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
1204
	if (!$fd) {
1205
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
1206
		return 1;
1207
	}
1208

    
1209
	if ($wancfg['dhcphostname']) {
1210
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
1211
		$dhclientconf_hostname .= "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
1212
	} else {
1213
		$dhclientconf_hostname = "";
1214
	}
1215

    
1216
 	$dhclientconf = "";
1217

    
1218
	$dhclientconf .= <<<EOD
1219
interface "{$wancfg['if']}" {
1220
timeout 60;
1221
retry 1;
1222
select-timeout 0;
1223
initial-interval 1;
1224
	{$dhclientconf_hostname}
1225
	script "/sbin/dhclient-script";
1226
}
1227

    
1228
EOD;
1229

    
1230
if(is_ipaddr($wancfg['alias-address'])) {
1231
	$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
1232
	$dhclientconf .= <<<EOD
1233
alias {
1234
	interface  "{$wancfg['if']}";
1235
	fixed-address {$wancfg['alias-address']};
1236
	option subnet-mask {$subnetmask};
1237
}
1238

    
1239
EOD;
1240
}
1241
	fwrite($fd, $dhclientconf);
1242
	fclose($fd);
1243

    
1244
	$wanif = $wancfg['if'];
1245

    
1246
        /* bring wan interface up before starting dhclient */
1247
        mwexec("/sbin/ifconfig {$wanif} up");
1248

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

    
1252
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1253
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
1254
	fclose($fout);
1255

    
1256
	return 0;
1257
}
1258

    
1259
function interfaces_wan_dhcp_down() {
1260
	global $config;
1261
	$wancfg = $config['interfaces']['wan'];
1262
	$wanif = $wancfg['if'];
1263
	mwexec("/sbin/ifconfig {$wanif} delete");
1264
	sleep(1);
1265
}
1266

    
1267
function interfaces_dhcp_down($interface) {
1268
	global $config;
1269
	if(filter_translate_type_to_real_interface($interface) <> "")
1270
		$realinterface = filter_translate_type_to_real_interface($interface);
1271
	mwexec("/sbin/ifconfig {$realinterface} down");
1272
	sleep(1);
1273
	$pid = find_dhclient_process($interface);
1274
	if($pid)
1275
		mwexec("kill {$pid}");
1276
}
1277

    
1278
function interfaces_dhcp_up($interface) {
1279
	interfaces_dhcp_configure($interface);
1280
	sleep(1);
1281
}
1282

    
1283
function interfaces_wan_dhcp_up() {
1284
	interfaces_wan_dhcp_configure();
1285
	sleep(1);
1286
}
1287

    
1288
function interfaces_wan_pppoe_configure() {
1289
	global $config, $g;
1290

    
1291
	$wancfg = $config['interfaces']['wan'];
1292
	$pppoecfg = $config['pppoe'];
1293

    
1294
	/* generate mpd.conf */
1295
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1296
	if (!$fd) {
1297
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
1298
		return 1;
1299
	}
1300

    
1301
	$idle = 0;
1302

    
1303
	if (isset($pppoecfg['ondemand'])) {
1304
		$ondemand = "enable";
1305
		if ($pppoecfg['timeout'])
1306
			$idle = $pppoecfg['timeout'];
1307
	} else {
1308
		$ondemand = "disable";
1309
	}
1310

    
1311
	$mpdconf = <<<EOD
1312
startup:
1313
pppoeclient:
1314
	new -i pppoe0 pppoeclient pppoeclient
1315
	set iface route default
1316
	set iface {$ondemand} on-demand
1317
	set iface idle {$idle}
1318
	set iface up-script /usr/local/sbin/ppp-linkup
1319

    
1320
EOD;
1321

    
1322
	/*    Check for ppp-linkdown Script in /usr/local/sbin
1323
	 *    Create reference in mpd.conf
1324
	 */
1325
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1326
		$mpdconf .= <<<EOD
1327
	set iface down-script /usr/local/sbin/ppp-linkdown
1328

    
1329
EOD;
1330
	}
1331

    
1332
	if (isset($pppoecfg['ondemand'])) {
1333
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
1334
			$mpdconf .= <<<EOD
1335
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
1336

    
1337
EOD;
1338
		} else {
1339
			$mpdconf .= <<<EOD
1340
	set iface addrs 192.0.2.112 192.0.2.113
1341

    
1342
EOD;
1343
		}
1344
	}
1345

    
1346
	$mpdconf .= <<<EOD
1347
	set bundle disable multilink
1348
	set auth authname "{$pppoecfg['username']}"
1349
	set auth password "{$pppoecfg['password']}"
1350
	set link keep-alive 10 60
1351
	set link max-redial 0
1352
	set link no acfcomp protocomp
1353
	set link disable pap chap
1354
	set link accept chap
1355
	set link mtu 1492
1356
	set ipcp yes vjcomp
1357
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1358

    
1359

    
1360

    
1361
EOD;
1362

    
1363
	if (isset($config['system']['dnsallowoverride'])) {
1364
		$mpdconf .= <<<EOD
1365
	set ipcp enable req-pri-dns
1366

    
1367
EOD;
1368
	}
1369

    
1370
	if (!isset($config['pppoe']['dnsnosec'])) {
1371
			$mpdconf .= <<<EOD
1372
	set ipcp enable req-sec-dns
1373

    
1374
EOD;
1375
	}
1376
	
1377
	$mpdconf .= <<<EOD
1378
	open
1379

    
1380
EOD;
1381

    
1382
	fwrite($fd, $mpdconf);
1383
	fclose($fd);
1384

    
1385
	/* generate mpd.links */
1386
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1387
	if (!$fd) {
1388
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
1389
		return 1;
1390
	}
1391

    
1392
	$mpdconf = <<<EOD
1393
pppoeclient:
1394
	set link type pppoe
1395
	set pppoe iface {$wancfg['if']}
1396
	set pppoe service "{$pppoecfg['provider']}"
1397
	set pppoe enable originate
1398
	set pppoe disable incoming
1399

    
1400
EOD;
1401

    
1402
	fwrite($fd, $mpdconf);
1403
	fclose($fd);
1404

    
1405
	if(file_exists("{$g['varrun_path']}/mpdpppoe.pid") and $g['booting']) {
1406
		/* if we are booting and mpd has already been started then don't start again. */
1407
	} else {
1408
		/* if mpd is active, lets take it down */
1409
		if(file_exists("{$g['varrun_path']}/mpdpppoe.pid")) {
1410
			killbypid("{$g['varrun_path']}/mpdpppoe.pid");
1411
			sleep(3);
1412
		}
1413
		/* fire up mpd */
1414
		mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpdpppoe.pid pppoeclient");
1415
	}
1416

    
1417
        /* sleep until wan is up - or 30 seconds, whichever comes first */
1418
	for ($count = 0; $count < 30; $count++) {
1419
		if(file_exists("{$g['tmp_path']}/wanup")) {
1420
			break;
1421
		}
1422
		sleep(1);
1423
	}
1424

    
1425
	unlink_if_exists("{$g['tmp_path']}/wanup");
1426

    
1427
	return 0;
1428
}
1429

    
1430
function interfaces_wan_pppoe_restart() {
1431
	interfaces_wan_pppoe_down();
1432
	sleep(1);
1433
	interfaces_wan_pppoe_up();
1434
}
1435

    
1436
function interfaces_wan_pppoe_down() {
1437
	global $g;
1438
	sigkillbypid("{$g['varrun_path']}/mpdpppoe.pid", "SIGUSR2");
1439
	sleep(1);
1440
}
1441

    
1442
function interfaces_wan_pppoe_up() {
1443
	global $g;
1444
	sigkillbypid("{$g['varrun_path']}/mpdpppoe.pid", "SIGUSR1");
1445
	sleep(1);
1446
}
1447

    
1448
function interfaces_wan_pptp_configure() {
1449
	global $config, $g;
1450

    
1451
	$wancfg = $config['interfaces']['wan'];
1452
	$pptpcfg = $config['pptp'];
1453

    
1454
	/* generate mpd.conf */
1455
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1456
	if (!$fd) {
1457
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1458
		return 1;
1459
	}
1460

    
1461
	$idle = 0;
1462

    
1463
	if (isset($pptpcfg['ondemand'])) {
1464
		$ondemand = "enable";
1465
		if ($pptpcfg['timeout'])
1466
			$idle = $pptpcfg['timeout'];
1467
	} else {
1468
		$ondemand = "disable";
1469
	}
1470

    
1471
	$mpdconf = <<<EOD
1472
pptp:
1473
	new -i pptp0 pptp pptp
1474
	set iface route default
1475
	set iface {$ondemand} on-demand
1476
	set iface idle {$idle}
1477
	set iface up-script /usr/local/sbin/ppp-linkup
1478

    
1479
EOD;
1480

    
1481
	/*   Check for ppp-linkdown Script in /usr/local/sbin
1482
	 *   Create reference in mpd.conf
1483
	 */
1484
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1485
		$mpdconf .= <<<EOD
1486
	set iface down-script /usr/local/sbin/ppp-linkdown
1487

    
1488
EOD;
1489
	}
1490

    
1491
	if (isset($pptpcfg['ondemand'])) {
1492
		$mpdconf .= <<<EOD
1493
	set iface addrs 10.0.0.1 10.0.0.2
1494

    
1495
EOD;
1496
	}
1497

    
1498
	$mpdconf .= <<<EOD
1499
	set bundle disable multilink
1500
	set bundle authname "{$pptpcfg['username']}"
1501
	set bundle password "{$pptpcfg['password']}"
1502
	set link keep-alive 10 60
1503
	set link max-redial 0
1504
	set link no acfcomp protocomp
1505
	set link disable pap chap
1506
	set link accept chap
1507
	set ipcp no vjcomp
1508
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1509

    
1510
EOD;
1511
	if (isset($config['system']['dnsallowoverride'])) {
1512
		$mpdconf .= <<<EOD
1513
	set ipcp enable req-pri-dns
1514

    
1515
EOD;
1516
	}
1517

    
1518
	$mpdconf .= <<<EOD
1519
	open
1520

    
1521
EOD;
1522

    
1523
	fwrite($fd, $mpdconf);
1524
	fclose($fd);
1525

    
1526
	/* generate mpd.links */
1527
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1528
	if (!$fd) {
1529
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1530
		return 1;
1531
	}
1532

    
1533
	$mpdconf = <<<EOD
1534
pptp:
1535
	set link type pptp
1536
	set pptp enable originate outcall
1537
	set pptp disable windowing
1538
	set pptp self {$pptpcfg['local']}
1539
	set pptp peer {$pptpcfg['remote']}
1540

    
1541
EOD;
1542

    
1543
	fwrite($fd, $mpdconf);
1544
	fclose($fd);
1545

    
1546
	/* configure interface */
1547
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1548
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1549

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

    
1553
	return 0;
1554
}
1555

    
1556
function interfaces_wan_pptp_restart() {
1557
	interfaces_wan_pptp_down();
1558
	sleep(1);
1559
	interfaces_wan_pptp_up();
1560
}
1561

    
1562
function interfaces_wan_pptp_down() {
1563
	global $g;
1564
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1565
	sleep(1);
1566
}
1567

    
1568
function interfaces_wan_pptp_up() {
1569
	global $g;
1570
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1571
	sleep(1);
1572
}
1573

    
1574
function interfaces_wan_bigpond_configure($curwanip) {
1575
	global $config, $g;
1576

    
1577
	$bpcfg = $config['bigpond'];
1578

    
1579
	if (!$curwanip) {
1580
		/* IP address not configured yet, exit */
1581
		return 0;
1582
	}
1583

    
1584
	/* kill bpalogin */
1585
	killbyname("bpalogin");
1586

    
1587
	/* wait a moment */
1588
	sleep(1);
1589

    
1590
	/* get the default domain */
1591
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1592
	if ($nfd) {
1593
		$defaultdomain = trim(fgets($nfd));
1594
		fclose($nfd);
1595
	}
1596

    
1597
	/* generate bpalogin.conf */
1598
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1599
	if (!$fd) {
1600
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1601
		return 1;
1602
	}
1603

    
1604
	if (!$bpcfg['authserver'])
1605
		$bpcfg['authserver'] = "dce-server";
1606
	if (!$bpcfg['authdomain'])
1607
		$bpcfg['authdomain'] = $defaultdomain;
1608

    
1609
	$bpconf = <<<EOD
1610
username {$bpcfg['username']}
1611
password {$bpcfg['password']}
1612
authserver {$bpcfg['authserver']}
1613
authdomain {$bpcfg['authdomain']}
1614
localport 5050
1615

    
1616
EOD;
1617

    
1618
	if ($bpcfg['minheartbeatinterval'])
1619
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1620

    
1621
	fwrite($fd, $bpconf);
1622
	fclose($fd);
1623

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

    
1627
	return 0;
1628
}
1629

    
1630
function get_real_wan_interface() {
1631
	global $config, $g;
1632

    
1633
	$wancfg = $config['interfaces']['wan'];
1634

    
1635
	$wanif = $wancfg['if'];
1636
	if ($wancfg['ipaddr'] == "pppoe") 
1637
		$wanif = "pppoe0";
1638
	if ($wancfg['ipaddr'] == "pptp") 
1639
		$wanif = "pptp0";
1640

    
1641
	return $wanif;
1642
}
1643

    
1644
function get_current_wan_address($interface = "wan") {
1645
	global $config, $g;
1646

    
1647
	$wancfg = $config['interfaces'][$interface];
1648

    
1649
	$interface = filter_translate_type_to_real_interface($interface);
1650
	$ifinfo = "";
1651
	if(in_array($wancfg['ipaddr'], array('dhcp'))) {
1652
		/* get interface info with netstat */
1653
		exec("/usr/bin/netstat -nWI " . escapeshellarg($interface) . " -f inet", $ifinfo);
1654

    
1655
		if (isset($ifinfo[1])) {
1656
			$aif = preg_split("/\s+/", $ifinfo[1]);
1657
			$curwanip = chop($aif[3]);
1658

    
1659
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1660
				return $curwanip;
1661
		}
1662

    
1663
		return null;
1664
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1665
		/* dynamic WAN IP address, find out which one */
1666
		$wanif = get_real_wan_interface();
1667

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

    
1671
		if (isset($ifinfo[1])) {
1672
			$aif = preg_split("/\s+/", $ifinfo[1]);
1673
			$curwanip = chop($aif[3]);
1674

    
1675
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1676
				return $curwanip;
1677
		}
1678

    
1679
		return null;
1680
	} else {
1681
		/* static WAN IP address */
1682
		return $wancfg['ipaddr'];
1683
	}
1684
}
1685

    
1686
/****f* interfaces/is_altq_capable
1687
 * NAME
1688
 *   is_altq_capable - Test if interface is capable of using ALTQ
1689
 * INPUTS
1690
 *   $int            - string containing interface name
1691
 * RESULT
1692
 *   boolean         - true or false
1693
 ******/
1694

    
1695
function is_altq_capable($int) {
1696
        /* Per:
1697
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1698
         * Only the following drivers have ALTQ support
1699
         */
1700
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1701
		"em", "fxp", "hme", "lnc", "le", "nve", "re", "rl", "ndis", "sf", "sis", "sk",
1702
		"tun", "vr", "wi", "xl", "vlan", "ste");
1703

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

    
1706
        if (in_array($int_family[0], $capable))
1707
                return true;
1708
        else
1709
                return false;
1710
}
1711

    
1712
function get_number_of_bridged_interfaces() {
1713
	$bridges_total = 0;
1714
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1715
	foreach($bridges as $bridge) {
1716
		$match_array = "";
1717
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1718
		if($match_array[1][0] <> "") {
1719
			if($match_array[1][0] > $bridges_total)
1720
				$bridges_total = $match_array[1][0];
1721
		}
1722
	}
1723
	return "{$bridges_total}";
1724
}
1725

    
1726
function get_number_of_vlan_interfaces() {
1727
        $vlans_total = 0;
1728
        $vlans = split("\n", `/sbin/ifconfig -a | /usr/bin/grep vlan | grep flags`);
1729
        foreach($vlans as $bridge) {
1730
                $match_array = "";
1731
                preg_match_all("/vlan(.*):/",$bridge,$match_array);
1732
                if($match_array[1][0] <> "") {
1733
                        if($match_array[1][0] > $vlans_total)
1734
                                $vlans_total = $match_array[1][0];
1735
                }
1736
        }
1737
        return "{$vlans_total}";
1738
}
1739

    
1740
function get_number_of_ppp_interfaces() {
1741
        $ppps_total = 0;
1742
        $ppps = split("\n", `/sbin/ifconfig -a | /usr/bin/grep ppp | grep flags`);
1743
        foreach($ppps as $bridge) {
1744
                $match_array = "";
1745
                preg_match_all("/ppp(.*):/",$bridge,$match_array);
1746
                if($match_array[1][0] <> "") {
1747
                        if($match_array[1][0] > $ppps_total)
1748
                                $ppps_total = $match_array[1][0];
1749
                }
1750
        }
1751
        return "{$ppps_total}";
1752
}
1753

    
1754
function get_next_available_bridge_interface() {
1755
	$bridges_total = get_number_of_bridged_interfaces();
1756
	$interfaces = `/sbin/ifconfig -l`;
1757
	$x=0;
1758
	for($x=0; $x<$bridges_total; $x++) {
1759
		if(!stristr($interfaces, "bridge{$x}")) {
1760
			return "{$x}";
1761
		}
1762
	}
1763
	return "{$x}";
1764
}
1765

    
1766
function destroy_bridge($bridge_num) {
1767
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1768
	sleep(1);
1769
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1770
	sleep(1);
1771
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1772
	sleep(1);
1773
	return;
1774
}
1775

    
1776
function discover_bridge($interface1, $interface2) {
1777
	if(!$interface1) return;
1778
	if(!$interface2) return;
1779
	$total_bridges = get_number_of_bridged_interfaces();
1780
	$total_bridges++;
1781
	$interfaces = `/sbin/ifconfig -l`;
1782
	$x=0;
1783
	for($x=0; $x<$total_bridges; $x++) {
1784
		$bridge_text = "NA";
1785
		if(!stristr($interfaces, "bridge{$x}"))
1786
			continue;
1787
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1788
		if(stristr($bridge_text, $interface1))
1789
			if(stristr($bridge_text, $interface2))
1790
				return $x;
1791
	}
1792
	return "-1";
1793
}
1794

    
1795
function get_wireless_modes($interface)
1796
{
1797
	/* return wireless modes and channels */
1798
	if(is_interface_wireless($interface)) {
1799
		$wi = 1;
1800
		$ifconfig = "/sbin/ifconfig";
1801
		$awk = "/usr/bin/awk";
1802
		$chan_list = "$ifconfig $interface list chan";
1803
		$stack_list = "$awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
1804
		$format_list = "$awk '{print \$5 \" \" \$6 \",\" \$1}'";
1805

    
1806
		$interface_channels = "";
1807
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
1808
		$interface_channel_count = count($interface_channels);
1809

    
1810
		$c = 0;
1811
		while ($c < $interface_channel_count)
1812
		{
1813
			$channel_line = explode(",", $interface_channels["$c"]);
1814
			$wireless_mode = trim($channel_line[0]);
1815
			$wireless_channel = trim($channel_line[1]);
1816
			if(trim($wireless_mode) != "") {
1817
				/* if we only have 11g also set 11b channels */
1818
				if($wireless_mode == "11g") {
1819
					$wireless_modes["11b"] = array();
1820
				}
1821
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
1822
			}
1823
			$c++;
1824
		}
1825
	}
1826
	return($wireless_modes);
1827
}
1828

    
1829
function get_interface_mac($interface) {
1830

    
1831
        /* build interface list with netstat */
1832
        $linkinfo = "";
1833
        exec("/usr/bin/netstat -I $interface -nW -f link", $linkinfo);
1834
        array_shift($linkinfo);
1835
        $alink = preg_split("/\s+/", $linkinfo[0]);
1836
        $mac = chop($alink[3]);
1837
        return $mac;
1838
}
1839

    
1840
?>
(11-11/29)