Project

General

Profile

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

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

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

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

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

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

    
26
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
	POSSIBILITY OF SUCH DAMAGE.
36
*/
37

    
38
/* include all configuration functions */
39
require_once("functions.inc");
40
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
	/* CARP interfaces can be attached to VLAN's, too. */
109
	interfaces_carp_bring_up_final();
110

    
111
	return 0;
112
}
113

    
114
function interfaces_lan_configure() {
115
	global $config, $g;
116

    
117
	$bridges_total = get_next_available_bridge_interface();
118

    
119
	$lancfg = $config['interfaces']['lan'];
120

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

    
125
	/* wireless configuration? */
126
	if (is_array($lancfg['wireless']))
127
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
128

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

    
149
	/* bridged? */
150

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

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

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

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

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

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

    
187
		$bridges_total++;
188
		/* update cache */
189
		if ($bridges_total != find_number_of_created_bridges())
190
			find_number_of_created_bridges(true);
191
	}
192

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

    
203
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
204
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
205

    
206
	if (!$g['booting']) {
207
		/* make new hosts file */
208
		system_hosts_generate();
209

    
210
		/* reconfigure static routes (kernel may have deleted them) */
211
		system_routing_configure();
212

    
213
		/* set the reload filter dity flag */
214
		touch("{$g['tmp_path']}/filter_dirty");
215

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

    
219
		/* reload dhcpd (gateway may have changed) */
220
		services_dhcpd_configure();
221

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

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

    
228
	}
229

    
230
	return 0;
231
}
232

    
233
function interfaces_optional_configure() {
234
	global $config, $g;
235
	global $bridgeconfig;
236

    
237
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
238
		interfaces_optional_configure_if($i);
239
	}
240

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

    
245
		/* reload IPsec tunnels */
246
		vpn_ipsec_configure();
247

    
248
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
249
		services_dhcpd_configure();
250

    
251
		/* restart dnsmasq */
252
		services_dnsmasq_configure();
253

    
254
		/* reload captive portal */
255
		captiveportal_configure();
256

    
257
		/* set the reload filter dity flag */
258
		touch("{$g['tmp_path']}/filter_dirty");
259
	}
260

    
261
	return 0;
262
}
263

    
264
function interfaces_optional_configure_if($opti) {
265
	global $config, $g;
266
	global $bridgeconfig, $debugging;
267

    
268
	$bridges_total = get_next_available_bridge_interface();
269

    
270
	$optcfg = $config['interfaces']['opt' . $opti];
271

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

    
282
	if (isset($optcfg['enable'])) {
283
		if($optcfg['gateway'])
284
			system("echo " . $optcfg['gateway'] . " > /tmp/" . $optcfg['if'] . "_router");
285

    
286
		/* wireless configuration? */
287
		if (is_array($optcfg['wireless']))
288
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
289

    
290
		/* PPP configuration */
291
		if (isset($optcfg['pointtopoint']))
292
			interfaces_ppp_configure_if($optcfg);
293

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

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

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

    
330
			/* invalidate interface cache */
331
			get_interface_arr(true);
332

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

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

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

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

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

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

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

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

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

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

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

    
545
			/* invalidate interface cache */
546
			get_interface_arr(true);
547

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

    
568
	/* update cache */
569
	if ($carp_instances_counter != find_number_of_created_carp_interfaces())
570
		find_number_of_created_carp_interfaces(true);
571
}
572

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

    
614
		if($debugging)
615
			echo "Upping interface carp{$carp_instances_counter}.\n";
616
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
617
		if($vip['password'] != "")
618
			$password = " pass " . $vip['password'];
619
		if($debugging)
620
			echo "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password . "\n";
621
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
622
		sleep(1);
623
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
624
		$carp_instances_counter++;
625
	}
626
	if($g['booting'])
627
		echo " done.\n";
628
}
629

    
630
function interfaces_ipalias_configure() {
631
	global $g, $config, $debugging;
632
	if(isset($config['system']['developerspew'])) {
633
		$mt = microtime();
634
		echo "interfaces_ipalias_configure() being called $mt\n";
635
	}
636
	$viparr = &$config['virtualip']['vip'];
637
	if(is_array($viparr)) {
638
		foreach ($viparr as $vip) {
639
			if ($vip['mode'] == "ipalias") {
640
				$if = $vip['interface'];
641
				mwexec("/sbin/ifconfig " . escapeshellarg($config['interfaces'][$if]['if']) . " " . $vip['subnet'] . "/" . escapeshellarg($vip['subnet_bits']) . " alias"); 
642
			}
643
		}
644
	}
645
}
646

    
647
function interfaces_wireless_configure($if, $wlcfg) {
648
	global $config, $g;
649

    
650
	/*    open up a shell script that will be used to output the commands.
651
	 *    since wireless is changing a lot, these series of commands are fragile
652
     *    and will sometimes need to be verified by a operator by executing the command
653
     *    and returning the output of the command to the developers for inspection.  please
654
     *    do not change this routine from a shell script to individul exec commands.  -sullrich
655
	 */
656

    
657
	conf_mount_rw();
658

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

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

    
665
	fwrite($fd_set, "# enable shell debugging\n");
666
	fwrite($fd_set, "set -x\n");
667

    
668
	/* set values for /path/program */
669
	$hostapd = "/usr/sbin/hostapd";
670
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
671
	$ifconfig = "/sbin/ifconfig";
672
	$killall = "/usr/bin/killall";
673

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

    
676
	/* Set a/b/g standard */
677
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
678

    
679
	/* Set 802.11g protection mode */
680
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
681

    
682
	/* set wireless channel value */
683
	if(isset($wlcfg['channel']))
684
		$channel = "channel " . escapeshellarg($wlcfg['channel']);
685

    
686
	/* set Distance value */
687
	if($wlcfg['distance'])
688
		$distance = escapeshellarg($wlcfg['distance']);
689

    
690
	/* Set ssid */
691
	if($wlcfg['ssid'])
692
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
693

    
694
	/* Set wireless hostap mode */
695
	if ($wlcfg['mode'] == "hostap")
696
		$hostapmode = "mediaopt hostap";
697
	else
698
		$hostapmode = "-mediaopt hostap";
699

    
700
	/* Set wireless adhoc mode */
701
	if ($wlcfg['mode'] == "adhoc")
702
		$adhocmode = "mediaopt adhoc";
703
	else
704
		$adhocmode = "-mediaopt adhoc";
705

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

    
708
	/* handle hide ssid option */
709
	if(isset($wlcfg['hidessid']['enable']))
710
		$hidessid = "hidessid";
711
	else
712
		$hidessid = "-hidessid";
713

    
714
	/* handle pureg (802.11g) only option */
715
	if(isset($wlcfg['pureg']['enable']))
716
		$pureg = "mode 11g pureg";
717
	else
718
		$pureg = "-pureg";
719

    
720
	/* enable apbridge option */
721
	if(isset($wlcfg['apbridge']['enable']))
722
		$apbridge = "apbridge";
723
	else
724
		$apbridge = "-apbridge";
725

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

    
732
	/* handle txpower setting */
733
	if($wlcfg['txpower'] <> "")
734
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
735

    
736
	/* handle wme option */
737
	if(isset($wlcfg['wme']['enable']))
738
		$wme = "wme";
739
	else
740
		$wme = "-wme";
741

    
742
	/* set up wep if enabled */
743
    if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
744
		if($wlcfg['wpa']['auth_algs'] == "1")
745
			$wepset .= "authmode open wepmode on ";
746
		else if($wlcfg['wpa']['auth_algs'] == "2")
747
			$wepset .= "authmode shared wepmode on ";
748
		else if($wlcfg['wpa']['auth_algs'] == "3")
749
			$wepset .= "authmode mixed wepmode on ";
750
		$i = 1;
751
		foreach ($wlcfg['wep']['key'] as $wepkey) {
752
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
753
			if (isset($wepkey['txkey']))
754
				$wepset .= "weptxkey {$i} ";
755
			$i++;
756
		}
757
    } else {
758
    	$wepset .= "authmode open wepmode off ";
759
	}
760

    
761
	/* generate wpa_supplicant/hostap config if wpa is enabled */
762

    
763
	switch ($wlcfg['mode']) {
764
		case 'bss':
765
			if (isset($wlcfg['wpa']['enable'])) {
766

    
767
				$wpa .= <<<EOD
768
ctrl_interface={$g['varrun_path']}/wpa_supplicant
769
ctrl_interface_group=0
770
ap_scan=1
771
#fast_reauth=1
772
network={
773
ssid="{$wlcfg['ssid']}"
774
scan_ssid=1
775
priority=5
776
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
777
psk="{$wlcfg['wpa']['passphrase']}"
778
pairwise={$wlcfg['wpa']['wpa_pairwise']}
779
group={$wlcfg['wpa']['wpa_pairwise']}
780
}
781
EOD;
782

    
783
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
784
				fwrite($fd, "{$wpa}");
785
				fclose($fd);
786

    
787
				fwrite($fd_set, kill_wpasupplicant($if));
788
			}
789
		break;
790

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

    
822
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
823
				fwrite($fd, "{$wpa}");
824
				fclose($fd);
825

    
826
				fwrite($fd_set, kill_hostapd($if));
827
			}
828
		break;
829

    
830
		case 'adhoc':
831
			fwrite($fd_set, kill_hostapd($if));
832
			fwrite($fd_set, kill_wpasupplicant($if));
833
		break;
834
	}
835

    
836
	/*
837
	 *    all variables are set, lets start up everything
838
     */
839

    
840
	/* set ack timers according to users preference (if he/she has any) */
841
	if($distance) {
842
		fwrite($fd_set, "# Enable ATH distance settings\n");
843
		fwrite($fd_set, "/sbin/athctrl.sh -i {$if} -d {$distance}\n");
844
	}
845

    
846
	$standard_no_turbo = str_replace(" Turbo", "", $standard);
847

    
848
	$settings = <<<EOD
849

    
850
{$ifconfig} {$if} down
851
{$ifconfig} {$if} {$hostapmode}
852
{$ifconfig} {$if} {$standard_no_turbo}
853
{$ifconfig} {$if} {$channel}
854
{$ifconfig} {$if} {$turbo}
855
{$ifconfig} {$if} {$ssid}
856
{$ifconfig} {$if} {$hidessid}
857
{$ifconfig} {$if} {$adhocmode}
858
{$ifconfig} {$if} {$protmode}
859
{$ifconfig} {$if} {$pureg}
860
{$ifconfig} {$if} {$apbridge}
861
{$ifconfig} {$if} {$wme}
862
{$ifconfig} {$if} {$wepset}
863
{$ifconfig} {$if} {$txpower}
864
{$ifconfig} {$if} up
865

    
866
EOD;
867

    
868
	/* write out above <<EOD stuff */
869
	fwrite($fd_set, $settings);
870

    
871
	if (isset($wlcfg['wpa']['enable'])) {
872
		if ($wlcfg['mode'] == "bss")
873
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
874
		if ($wlcfg['mode'] == "hostap")
875
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
876
	}
877

    
878
	fclose($fd_set);
879

    
880
	conf_mount_ro();
881

    
882
	/* execute commands now in shell */
883
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
884
	sleep(2);
885
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
886

    
887
	return 0;
888

    
889
}
890

    
891
function kill_hostapd($interface) {
892
	return "/bin/ps awwuxx | grep hostapd | grep $interface | awk '{ print \$2 }' | xargs kill\n";
893
}
894

    
895
function kill_wpasupplicant($interface) {
896
	return "/bin/ps awwuxx | grep wpa_supplicant | grep $interface | awk '{ print \$2 }' | xargs kill\n";
897
}
898

    
899
function find_dhclient_process($interface) {
900
	if(filter_translate_type_to_real_interface($interface) <> "")
901
        	$realinterface = filter_translate_type_to_real_interface($interface);
902
	if($realinterface)
903
		$pid = `ps awwwux | grep dhclient | grep -v grep | grep {$realinterface} | awk '{ print \$2 }'`;
904
	return $pid;
905
}
906

    
907
function interfaces_wan_configure() {
908
	global $config, $g, $bridges_total;
909

    
910
	$wancfg = $config['interfaces']['wan'];
911

    
912
	if(file_exists("/tmp/{$config['interfaces']['wan']['if']}_router")) 
913
		unlink("/tmp/{$config['interfaces']['wan']['if']}_router");
914

    
915
	if(!$g['booting']) {
916
		mute_kernel_msgs();
917

    
918
		/* find dhclient process for wan and kill it */
919
		killbypid(find_dhclient_process("wan"));
920

    
921
		/* remove wanup file if it exists */
922
		unlink_if_exists("{$g['tmp_path']}/wanup");
923

    
924
		/* kill PPPoE client (mpd) */
925
		killbypid("{$g['varrun_path']}/mpd.pid");
926

    
927
		/* wait for processes to die */
928
		sleep(3);
929

    
930
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
931
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
932
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
933
		unlink_if_exists("{$g['vardb_path']}/wanip");
934
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
935
	}
936

    
937
	/* remove all addresses first */
938
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
939
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
940

    
941
	/* wireless configuration? */
942
	if (is_array($wancfg['wireless']))
943
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
944

    
945
	if ($wancfg['spoofmac']) {
946
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
947
			" link " . escapeshellarg($wancfg['spoofmac']));
948
	}  else {
949
		$mac = get_interface_mac_address($wancfg['if']);
950
		if($mac == "ff:ff:ff:ff:ff:ff") {
951
			/*   this is not a valid mac address.  generate a
952
			 *   temporary mac address so the machine can get online.
953
			 */
954
			echo "Generating new MAC address.";
955
			$random_mac = generate_random_mac_address();
956
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
957
				" link " . escapeshellarg($random_mac));
958
			$wancfg['spoofmac'] = $random_mac;
959
			write_config();
960
			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");
961
		}
962
	}
963

    
964
	/* media */
965
	if ($wancfg['media'] || $wancfg['mediaopt']) {
966
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
967
		if ($wancfg['media'])
968
			$cmd .= " media " . escapeshellarg($wancfg['media']);
969
		if ($wancfg['mediaopt'])
970
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
971
		mwexec($cmd);
972
	}
973

    
974
	switch ($wancfg['ipaddr']) {
975

    
976
		case 'dhcp':
977
			interfaces_wan_dhcp_configure();
978
			break;
979

    
980
		case 'pppoe':
981
			interfaces_wan_pppoe_configure();
982
			break;
983

    
984
		case 'pptp':
985
			interfaces_wan_pptp_configure();
986
			break;
987

    
988
		case 'bigpond':
989
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
990
			interfaces_wan_dhcp_configure();
991
			break;
992

    
993
		default:
994
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
995
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
996
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
997
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
998
			} else {
999
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1000
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
1001
			}
1002

    
1003
			if($config['interfaces']['wan']['gateway'])
1004
				system("echo " . $config['interfaces']['wan']['gateway'] . " > /tmp/" . $config['interfaces']['wan']['if'] . "_router");
1005

    
1006
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
1007
			filter_configure();
1008
	}
1009

    
1010
	if ($wancfg['bridge']) {
1011
		/* use open/netBSD style bridge */
1012
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
1013

    
1014
		/* invalidate interface cache */
1015
		get_interface_arr(true);
1016

    
1017
		/* force all bridged interfaces to use same mtu */
1018
		$mtu = get_interface_mtu($config['interfaces'][$wancfg['bridge']]['if']);
1019
		mwexec("/sbin/ifconfig {$wancfg['if']} mtu {$mtu}");
1020
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}");
1021

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

    
1025
		if(!is_interface_wireless($wancfg['if']) and
1026
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if']))
1027
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$wancfg['bridge']]['if']} stp {$wancfg['if']}");
1028

    
1029
		/* log commands run for debugging in /tmp/ */
1030
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$wancfg['if']}", "w");
1031
		fwrite($fd, "/sbin/ifconfig {$wancfg['if']} mtu {$mtu}\n");
1032
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}\n");
1033
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
1034
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}\n");
1035
		if(!is_interface_wireless($wancfg['if']) and
1036
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if']))
1037
				fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$wancfg['if']} stp {$config['interfaces'][$wancfg['bridge']]['if']}\n");
1038
		fclose($fd);
1039

    
1040
		/* bring up interfaces */
1041
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
1042
		usleep(100);
1043
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} up");
1044
		usleep(5);
1045
		mwexec("/sbin/ifconfig {$wancfg['if']} up");
1046
		usleep(5);
1047
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
1048

    
1049
		$bridges_total++;
1050
		/* update cache */
1051
		if ($bridges_total != find_number_of_created_bridges())
1052
			find_number_of_created_bridges(true);
1053
	}
1054

    
1055
	if (!$g['booting']) {
1056
		/* reconfigure static routes (kernel may have deleted them) */
1057
		system_routing_configure();
1058

    
1059
		/* set the reload filter dity flag */
1060
		touch("{$g['tmp_path']}/filter_dirty");
1061

    
1062
		/* reload ipsec tunnels */
1063
		vpn_ipsec_configure();
1064

    
1065
		/* restart ez-ipupdate */
1066
		services_dyndns_configure();
1067

    
1068
		/* force DNS update */
1069
		services_dnsupdate_process();
1070

    
1071
		/* restart dnsmasq */
1072
		services_dnsmasq_configure();
1073

    
1074
		/* reload captive portal */
1075
		captiveportal_configure();
1076
	}
1077

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

    
1080
	unmute_kernel_msgs();
1081

    
1082
	return 0;
1083
}
1084

    
1085
function interfaces_opt_dhcp_configure($interface) {
1086
	global $config, $g;
1087

    
1088
	$optcfg = $config['interfaces'][$interface];
1089
	$optif = $optcfg['if'];
1090

    
1091
	/* generate dhclient_wan.conf */
1092
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
1093
	if (!$fd) {
1094
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
1095
		return 1;
1096
	}
1097

    
1098
	if ($optcfg['dhcphostname']) {
1099
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
1100
		$dhclientconf_hostname .= "\tsend host-name \"{$optcfg['dhcphostname']}\";\n";
1101
	} else {
1102
		$dhclientconf_hostname = "";
1103
	}
1104

    
1105
 	$dhclientconf = "";
1106

    
1107
	$dhclientconf .= <<<EOD
1108
timeout 60;
1109
retry 1;
1110
select-timeout 0;
1111
initial-interval 1;
1112
interface "{$optif}" {
1113
	script "/sbin/dhclient-script";
1114
	{$dhclientconf_hostname}
1115
}
1116

    
1117
EOD;
1118

    
1119
if(is_ipaddr($optcfg['alias-address'])) {
1120
	$subnetmask = gen_subnet_mask($optcfg['alias-subnet']);
1121
	$dhclientconf .= <<<EOD
1122
alias {
1123
	interface  "{$optif}";
1124
	fixed-address {$optcfg['alias-address']};
1125
	option subnet-mask {$subnetmask};
1126
}
1127

    
1128
EOD;
1129
}
1130
	fwrite($fd, $dhclientconf);
1131
	fclose($fd);
1132

    
1133
        /* bring interface up before starting dhclient */
1134
        mwexec("/sbin/ifconfig {$optif} up");
1135

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

    
1139
	return 0;
1140
}
1141

    
1142
function interfaces_dhcp_configure($interface) {
1143
	global $config, $g;
1144

    
1145
	if(filter_translate_type_to_real_interface($interface) <> "")
1146
        	$realinterface = filter_translate_type_to_real_interface($interface);
1147

    
1148
	$optcfg = $config['interfaces'][$interface];
1149

    
1150
	/* generate dhclient_$interface.conf */
1151
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
1152
	if (!$fd) {
1153
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
1154
		return 1;
1155
	}
1156

    
1157
	if ($optcfg['dhcphostname']) {
1158
		$dhclientconf_hostname =  "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
1159
		$dhclientconf_hostname .= "\tsend host-name \"{$optcfg['dhcphostname']}\";\n";
1160
	} else {
1161
		$dhclientconf_hostname = "";
1162
	}
1163

    
1164
 	$dhclientconf = "";
1165

    
1166
	$dhclientconf .= <<<EOD
1167
timeout 60;
1168
retry 1;
1169
select-timeout 0;
1170
initial-interval 1;
1171
interface "{$realinterface}" {
1172
	{$dhclientconf_hostname}
1173
	script "/sbin/dhclient-script";
1174
}
1175

    
1176
EOD;
1177

    
1178
if(is_ipaddr($optcfg['alias-address'])) {
1179
	$subnetmask = gen_subnet_mask($optcfg['alias-subnet']);
1180
	$dhclientconf .= <<<EOD
1181
alias {
1182
	interface  "{$optif}";
1183
	fixed-address {$optcfg['alias-address']};
1184
	option subnet-mask {$subnetmask};
1185
}
1186

    
1187
EOD;
1188
}
1189

    
1190
	fwrite($fd, $dhclientconf);
1191
	fclose($fd);
1192

    
1193
	$optif = $optcfg['if'];
1194

    
1195
        /* bring wan interface up before starting dhclient */
1196
        mwexec("/sbin/ifconfig {$optif} up");
1197

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

    
1201
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
1202
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
1203
	fclose($fout);
1204

    
1205
	return 0;
1206
}
1207

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

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

    
1213
	/* generate dhclient_wan.conf */
1214
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
1215
	if (!$fd) {
1216
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
1217
		return 1;
1218
	}
1219

    
1220
	if ($wancfg['dhcphostname']) {
1221
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
1222
		$dhclientconf_hostname .= "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
1223
	} else {
1224
		$dhclientconf_hostname = "";
1225
	}
1226

    
1227
 	$dhclientconf = "";
1228

    
1229
	$dhclientconf .= <<<EOD
1230
interface "{$wancfg['if']}" {
1231
timeout 60;
1232
retry 1;
1233
select-timeout 0;
1234
initial-interval 1;
1235
	{$dhclientconf_hostname}
1236
	script "/sbin/dhclient-script";
1237
}
1238

    
1239
EOD;
1240

    
1241
if(is_ipaddr($wancfg['alias-address'])) {
1242
	$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
1243
	$dhclientconf .= <<<EOD
1244
alias {
1245
	interface  "{$wancfg['if']}";
1246
	fixed-address {$wancfg['alias-address']};
1247
	option subnet-mask {$subnetmask};
1248
}
1249

    
1250
EOD;
1251
}
1252
	fwrite($fd, $dhclientconf);
1253
	fclose($fd);
1254

    
1255
	$wanif = $wancfg['if'];
1256

    
1257
        /* bring wan interface up before starting dhclient */
1258
        mwexec("/sbin/ifconfig {$wanif} up");
1259

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

    
1263
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1264
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
1265
	fclose($fout);
1266

    
1267
	return 0;
1268
}
1269

    
1270
function interfaces_wan_dhcp_down() {
1271
	global $config;
1272
	$wancfg = $config['interfaces']['wan'];
1273
	$wanif = $wancfg['if'];
1274
	mwexec("/sbin/ifconfig {$wanif} delete");
1275
	sleep(1);
1276
}
1277

    
1278
function interfaces_dhcp_down($interface) {
1279
	global $config;
1280
	if(filter_translate_type_to_real_interface($interface) <> "")
1281
		$realinterface = filter_translate_type_to_real_interface($interface);
1282
	mwexec("/sbin/ifconfig {$realinterface} down");
1283
	sleep(1);
1284
	$pid = find_dhclient_process($interface);
1285
	if($pid)
1286
		mwexec("kill {$pid}");
1287
}
1288

    
1289
function interfaces_dhcp_up($interface) {
1290
	interfaces_dhcp_configure($interface);
1291
	sleep(1);
1292
}
1293

    
1294
function interfaces_wan_dhcp_up() {
1295
	interfaces_wan_dhcp_configure();
1296
	sleep(1);
1297
}
1298

    
1299
function interfaces_wan_pppoe_configure() {
1300
	global $config, $g;
1301

    
1302
	$wancfg = $config['interfaces']['wan'];
1303
	$pppoecfg = $config['pppoe'];
1304

    
1305
	/* generate mpd.conf */
1306
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1307
	if (!$fd) {
1308
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
1309
		return 1;
1310
	}
1311

    
1312
	$idle = 0;
1313

    
1314
	if (isset($pppoecfg['ondemand'])) {
1315
		$ondemand = "enable";
1316
		if ($pppoecfg['timeout'])
1317
			$idle = $pppoecfg['timeout'];
1318
	} else {
1319
		$ondemand = "disable";
1320
	}
1321

    
1322
	$mpdconf = <<<EOD
1323
startup:
1324
pppoeclient:
1325
	new -i pppoe0 pppoeclient pppoeclient
1326
	set iface route default
1327
	set iface {$ondemand} on-demand
1328
	set iface idle {$idle}
1329
	set iface up-script /usr/local/sbin/ppp-linkup
1330

    
1331
EOD;
1332

    
1333
	/*    Check for ppp-linkdown Script in /usr/local/sbin
1334
	 *    Create reference in mpd.conf
1335
	 */
1336
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1337
		$mpdconf .= <<<EOD
1338
	set iface down-script /usr/local/sbin/ppp-linkdown
1339

    
1340
EOD;
1341
	}
1342

    
1343
	if (isset($pppoecfg['ondemand'])) {
1344
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
1345
			$mpdconf .= <<<EOD
1346
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
1347

    
1348
EOD;
1349
		} else {
1350
			$mpdconf .= <<<EOD
1351
	set iface addrs 192.0.2.112 192.0.2.113
1352

    
1353
EOD;
1354
		}
1355
	}
1356

    
1357
	$mpdconf .= <<<EOD
1358
	set bundle disable multilink
1359
	set auth authname "{$pppoecfg['username']}"
1360
	set auth password "{$pppoecfg['password']}"
1361
	set link keep-alive 10 60
1362
	set link max-redial 0
1363
	set link no acfcomp protocomp
1364
	set link disable pap chap
1365
	set link accept chap
1366
	set link mtu 1492
1367
	set ipcp yes vjcomp
1368
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1369

    
1370

    
1371

    
1372
EOD;
1373

    
1374
	if (isset($config['system']['dnsallowoverride'])) {
1375
		$mpdconf .= <<<EOD
1376
	set ipcp enable req-pri-dns
1377

    
1378
EOD;
1379
	}
1380

    
1381
	if (!isset($config['pppoe']['dnsnosec'])) {
1382
			$mpdconf .= <<<EOD
1383
	set ipcp enable req-sec-dns
1384

    
1385
EOD;
1386
	}
1387
	
1388
	$mpdconf .= <<<EOD
1389
	open
1390

    
1391
EOD;
1392

    
1393
	fwrite($fd, $mpdconf);
1394
	fclose($fd);
1395

    
1396
	/* generate mpd.links */
1397
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1398
	if (!$fd) {
1399
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
1400
		return 1;
1401
	}
1402

    
1403
	$mpdconf = <<<EOD
1404
pppoeclient:
1405
	set link type pppoe
1406
	set pppoe iface {$wancfg['if']}
1407
	set pppoe service "{$pppoecfg['provider']}"
1408
	set pppoe enable originate
1409
	set pppoe disable incoming
1410

    
1411
EOD;
1412

    
1413
	fwrite($fd, $mpdconf);
1414
	fclose($fd);
1415

    
1416
	if(file_exists("{$g['varrun_path']}/mpdpppoe.pid") and $g['booting']) {
1417
		/* if we are booting and mpd has already been started then don't start again. */
1418
	} else {
1419
		/* if mpd is active, lets take it down */
1420
		if(file_exists("{$g['varrun_path']}/mpdpppoe.pid")) {
1421
			killbypid("{$g['varrun_path']}/mpdpppoe.pid");
1422
			sleep(3);
1423
		}
1424
		/* fire up mpd */
1425
		mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpdpppoe.pid pppoeclient");
1426
	}
1427

    
1428
        /* sleep until wan is up - or 30 seconds, whichever comes first */
1429
	for ($count = 0; $count < 30; $count++) {
1430
		if(file_exists("{$g['tmp_path']}/wanup")) {
1431
			break;
1432
		}
1433
		sleep(1);
1434
	}
1435

    
1436
	unlink_if_exists("{$g['tmp_path']}/wanup");
1437

    
1438
	return 0;
1439
}
1440

    
1441
function interfaces_wan_pppoe_restart() {
1442
	interfaces_wan_pppoe_down();
1443
	sleep(1);
1444
	interfaces_wan_pppoe_up();
1445
}
1446

    
1447
function interfaces_wan_pppoe_down() {
1448
	global $g;
1449
	sigkillbypid("{$g['varrun_path']}/mpdpppoe.pid", "SIGUSR2");
1450
	sleep(1);
1451
}
1452

    
1453
function interfaces_wan_pppoe_up() {
1454
	global $g;
1455
	sigkillbypid("{$g['varrun_path']}/mpdpppoe.pid", "SIGUSR1");
1456
	sleep(1);
1457
}
1458

    
1459
function interfaces_wan_pptp_configure() {
1460
	global $config, $g;
1461

    
1462
	$wancfg = $config['interfaces']['wan'];
1463
	$pptpcfg = $config['pptp'];
1464

    
1465
	/* generate mpd.conf */
1466
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1467
	if (!$fd) {
1468
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1469
		return 1;
1470
	}
1471

    
1472
	$idle = 0;
1473

    
1474
	if (isset($pptpcfg['ondemand'])) {
1475
		$ondemand = "enable";
1476
		if ($pptpcfg['timeout'])
1477
			$idle = $pptpcfg['timeout'];
1478
	} else {
1479
		$ondemand = "disable";
1480
	}
1481

    
1482
	$mpdconf = <<<EOD
1483
pptp:
1484
	new -i pptp0 pptp pptp
1485
	set iface route default
1486
	set iface {$ondemand} on-demand
1487
	set iface idle {$idle}
1488
	set iface up-script /usr/local/sbin/ppp-linkup
1489

    
1490
EOD;
1491

    
1492
	/*   Check for ppp-linkdown Script in /usr/local/sbin
1493
	 *   Create reference in mpd.conf
1494
	 */
1495
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1496
		$mpdconf .= <<<EOD
1497
	set iface down-script /usr/local/sbin/ppp-linkdown
1498

    
1499
EOD;
1500
	}
1501

    
1502
	if (isset($pptpcfg['ondemand'])) {
1503
		$mpdconf .= <<<EOD
1504
	set iface addrs 10.0.0.1 10.0.0.2
1505

    
1506
EOD;
1507
	}
1508

    
1509
	$mpdconf .= <<<EOD
1510
	set bundle disable multilink
1511
	set bundle authname "{$pptpcfg['username']}"
1512
	set bundle password "{$pptpcfg['password']}"
1513
	set link keep-alive 10 60
1514
	set link max-redial 0
1515
	set link no acfcomp protocomp
1516
	set link disable pap chap
1517
	set link accept chap
1518
	set ipcp no vjcomp
1519
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1520

    
1521
EOD;
1522
	if (isset($config['system']['dnsallowoverride'])) {
1523
		$mpdconf .= <<<EOD
1524
	set ipcp enable req-pri-dns
1525

    
1526
EOD;
1527
	}
1528

    
1529
	$mpdconf .= <<<EOD
1530
	open
1531

    
1532
EOD;
1533

    
1534
	fwrite($fd, $mpdconf);
1535
	fclose($fd);
1536

    
1537
	/* generate mpd.links */
1538
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1539
	if (!$fd) {
1540
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1541
		return 1;
1542
	}
1543

    
1544
	$mpdconf = <<<EOD
1545
pptp:
1546
	set link type pptp
1547
	set pptp enable originate outcall
1548
	set pptp disable windowing
1549
	set pptp self {$pptpcfg['local']}
1550
	set pptp peer {$pptpcfg['remote']}
1551

    
1552
EOD;
1553

    
1554
	fwrite($fd, $mpdconf);
1555
	fclose($fd);
1556

    
1557
	/* configure interface */
1558
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1559
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1560

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

    
1564
	return 0;
1565
}
1566

    
1567
function interfaces_wan_pptp_restart() {
1568
	interfaces_wan_pptp_down();
1569
	sleep(1);
1570
	interfaces_wan_pptp_up();
1571
}
1572

    
1573
function interfaces_wan_pptp_down() {
1574
	global $g;
1575
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1576
	sleep(1);
1577
}
1578

    
1579
function interfaces_wan_pptp_up() {
1580
	global $g;
1581
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1582
	sleep(1);
1583
}
1584

    
1585
function interfaces_wan_bigpond_configure($curwanip) {
1586
	global $config, $g;
1587

    
1588
	$bpcfg = $config['bigpond'];
1589

    
1590
	if (!$curwanip) {
1591
		/* IP address not configured yet, exit */
1592
		return 0;
1593
	}
1594

    
1595
	/* kill bpalogin */
1596
	killbyname("bpalogin");
1597

    
1598
	/* wait a moment */
1599
	sleep(1);
1600

    
1601
	/* get the default domain */
1602
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1603
	if ($nfd) {
1604
		$defaultdomain = trim(fgets($nfd));
1605
		fclose($nfd);
1606
	}
1607

    
1608
	/* generate bpalogin.conf */
1609
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1610
	if (!$fd) {
1611
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1612
		return 1;
1613
	}
1614

    
1615
	if (!$bpcfg['authserver'])
1616
		$bpcfg['authserver'] = "dce-server";
1617
	if (!$bpcfg['authdomain'])
1618
		$bpcfg['authdomain'] = $defaultdomain;
1619

    
1620
	$bpconf = <<<EOD
1621
username {$bpcfg['username']}
1622
password {$bpcfg['password']}
1623
authserver {$bpcfg['authserver']}
1624
authdomain {$bpcfg['authdomain']}
1625
localport 5050
1626

    
1627
EOD;
1628

    
1629
	if ($bpcfg['minheartbeatinterval'])
1630
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1631

    
1632
	fwrite($fd, $bpconf);
1633
	fclose($fd);
1634

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

    
1638
	return 0;
1639
}
1640

    
1641
function get_real_wan_interface() {
1642
	global $config, $g;
1643

    
1644
	$wancfg = $config['interfaces']['wan'];
1645

    
1646
	$wanif = $wancfg['if'];
1647
	if ($wancfg['ipaddr'] == "pppoe") 
1648
		$wanif = "pppoe0";
1649
	if ($wancfg['ipaddr'] == "pptp") 
1650
		$wanif = "pptp0";
1651

    
1652
	return $wanif;
1653
}
1654

    
1655
function get_current_wan_address($interface = "wan") {
1656
	global $config, $g;
1657

    
1658
	$wancfg = $config['interfaces'][$interface];
1659

    
1660
	$interface = filter_translate_type_to_real_interface($interface);
1661
	$ifinfo = "";
1662
	if(in_array($wancfg['ipaddr'], array('dhcp'))) {
1663
		/* get interface info with netstat */
1664
		exec("/usr/bin/netstat -nWI " . escapeshellarg($interface) . " -f inet", $ifinfo);
1665

    
1666
		if (isset($ifinfo[1])) {
1667
			$aif = preg_split("/\s+/", $ifinfo[1]);
1668
			$curwanip = chop($aif[3]);
1669

    
1670
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1671
				return $curwanip;
1672
		}
1673

    
1674
		return null;
1675
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1676
		/* dynamic WAN IP address, find out which one */
1677
		$wanif = get_real_wan_interface();
1678

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

    
1682
		if (isset($ifinfo[1])) {
1683
			$aif = preg_split("/\s+/", $ifinfo[1]);
1684
			$curwanip = chop($aif[3]);
1685

    
1686
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1687
				return $curwanip;
1688
		}
1689

    
1690
		return null;
1691
	} else {
1692
		/* static WAN IP address */
1693
		return $wancfg['ipaddr'];
1694
	}
1695
}
1696

    
1697
/****f* interfaces/is_altq_capable
1698
 * NAME
1699
 *   is_altq_capable - Test if interface is capable of using ALTQ
1700
 * INPUTS
1701
 *   $int            - string containing interface name
1702
 * RESULT
1703
 *   boolean         - true or false
1704
 ******/
1705

    
1706
function is_altq_capable($int) {
1707
        /* Per:
1708
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1709
         * Only the following drivers have ALTQ support
1710
         */
1711
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1712
		"em", "fxp", "hme", "lnc", "le", "nve", "re", "rl", "ndis", "sf", "sis", "sk",
1713
		"tun", "vr", "wi", "xl", "vlan", "ste");
1714

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

    
1717
        if (in_array($int_family[0], $capable))
1718
                return true;
1719
        else
1720
                return false;
1721
}
1722

    
1723
function get_number_of_bridged_interfaces() {
1724
	$bridges_total = 0;
1725
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1726
	foreach($bridges as $bridge) {
1727
		$match_array = "";
1728
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1729
		if($match_array[1][0] <> "") {
1730
			if($match_array[1][0] > $bridges_total)
1731
				$bridges_total = $match_array[1][0];
1732
		}
1733
	}
1734
	return "{$bridges_total}";
1735
}
1736

    
1737
function get_number_of_vlan_interfaces() {
1738
        $vlans_total = 0;
1739
        $vlans = split("\n", `/sbin/ifconfig -a | /usr/bin/grep vlan | grep flags`);
1740
        foreach($vlans as $bridge) {
1741
                $match_array = "";
1742
                preg_match_all("/vlan(.*):/",$bridge,$match_array);
1743
                if($match_array[1][0] <> "") {
1744
                        if($match_array[1][0] > $vlans_total)
1745
                                $vlans_total = $match_array[1][0];
1746
                }
1747
        }
1748
        return "{$vlans_total}";
1749
}
1750

    
1751
function get_number_of_ppp_interfaces() {
1752
        $ppps_total = 0;
1753
        $ppps = split("\n", `/sbin/ifconfig -a | /usr/bin/grep ppp | grep flags`);
1754
        foreach($ppps as $bridge) {
1755
                $match_array = "";
1756
                preg_match_all("/ppp(.*):/",$bridge,$match_array);
1757
                if($match_array[1][0] <> "") {
1758
                        if($match_array[1][0] > $ppps_total)
1759
                                $ppps_total = $match_array[1][0];
1760
                }
1761
        }
1762
        return "{$ppps_total}";
1763
}
1764

    
1765
function get_next_available_bridge_interface() {
1766
	$bridges_total = get_number_of_bridged_interfaces();
1767
	$interfaces = `/sbin/ifconfig -l`;
1768
	$x=0;
1769
	for($x=0; $x<$bridges_total; $x++) {
1770
		if(!stristr($interfaces, "bridge{$x}")) {
1771
			return "{$x}";
1772
		}
1773
	}
1774
	return "{$x}";
1775
}
1776

    
1777
function destroy_bridge($bridge_num) {
1778
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1779
	sleep(1);
1780
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1781
	sleep(1);
1782
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1783
	sleep(1);
1784
	return;
1785
}
1786

    
1787
function discover_bridge($interface1, $interface2) {
1788
	if(!$interface1) return;
1789
	if(!$interface2) return;
1790
	$total_bridges = get_number_of_bridged_interfaces();
1791
	$total_bridges++;
1792
	$interfaces = `/sbin/ifconfig -l`;
1793
	$x=0;
1794
	for($x=0; $x<$total_bridges; $x++) {
1795
		$bridge_text = "NA";
1796
		if(!stristr($interfaces, "bridge{$x}"))
1797
			continue;
1798
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1799
		if(stristr($bridge_text, $interface1))
1800
			if(stristr($bridge_text, $interface2))
1801
				return $x;
1802
	}
1803
	return "-1";
1804
}
1805

    
1806
function get_wireless_modes($interface)
1807
{
1808
	/* return wireless modes and channels */
1809
	if(is_interface_wireless($interface)) {
1810
		$wi = 1;
1811
		$ifconfig = "/sbin/ifconfig";
1812
		$awk = "/usr/bin/awk";
1813
		$chan_list = "$ifconfig $interface list chan";
1814
		$stack_list = "$awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
1815
		$format_list = "$awk '{print \$5 \" \" \$6 \",\" \$1}'";
1816

    
1817
		$interface_channels = "";
1818
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
1819
		$interface_channel_count = count($interface_channels);
1820

    
1821
		$c = 0;
1822
		while ($c < $interface_channel_count)
1823
		{
1824
			$channel_line = explode(",", $interface_channels["$c"]);
1825
			$wireless_mode = trim($channel_line[0]);
1826
			$wireless_channel = trim($channel_line[1]);
1827
			if(trim($wireless_mode) != "") {
1828
				/* if we only have 11g also set 11b channels */
1829
				if($wireless_mode == "11g") {
1830
					$wireless_modes["11b"] = array();
1831
				}
1832
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
1833
			}
1834
			$c++;
1835
		}
1836
	}
1837
	return($wireless_modes);
1838
}
1839

    
1840
function get_interface_mac($interface) {
1841

    
1842
        /* build interface list with netstat */
1843
        $linkinfo = "";
1844
        exec("/usr/bin/netstat -I $interface -nW -f link", $linkinfo);
1845
        array_shift($linkinfo);
1846
        $alink = preg_split("/\s+/", $linkinfo[0]);
1847
        $mac = chop($alink[3]);
1848
        return $mac;
1849
}
1850

    
1851
?>
(11-11/29)