Project

General

Profile

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

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

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

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

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

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

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

    
38
/* include all configuration functions */
39
require_once("functions.inc");
40

    
41
if(!is_numeric($bridges_total)) $bridges_total=0;
42

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

    
46
	return 0;
47
}
48

    
49
function interfaces_vlan_configure() {
50
	global $config;
51

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

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

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

    
60
		$i = 0;
61

    
62
		foreach ($config['vlans']['vlan'] as $vlan) {
63

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

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

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

    
80
			mwexec($cmd);
81

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

    
85
			$i++;
86
		}
87
	}
88

    
89
	return 0;
90
}
91

    
92
function interfaces_lan_configure() {
93
	global $config, $g, $bridges_total;
94

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

    
97
	/* wireless configuration? */
98
	if (is_array($lancfg['wireless']))
99
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
100

    
101
	/* MAC spoofing? */
102
	if ($lancfg['spoofmac']) {
103
		mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
104
			" link " . escapeshellarg($lancfg['spoofmac']));
105
	} else {
106
		$mac = get_interface_mac_address($lancfg['if']);
107
		if($mac == "ff:ff:ff:ff:ff:ff") {
108
			/*   this is not a valid mac address.  generate a
109
			 *   temporary mac address so the machine can get online.
110
			 */
111
			echo "Generating new MAC address.";
112
			$random_mac = generate_random_mac_address();
113
			mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
114
				" link " . escapeshellarg($random_mac));
115
			$lancfg['spoofmac'] = $random_mac;
116
			write_config();
117
			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");
118
		}
119
	}	
120

    
121
	/* bridged? */
122
	
123
	if ($lancfg['bridge']) {
124
		// mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " delete up");
125
		/* use open/netBSD style bridge */
126
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
127
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up");
128
		mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} add {$config['interfaces'][$lancfg['bridge']]['if']}");
129
		
130
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
131
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
132
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up\n");
133
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} add {$config['interfaces'][$lancfg['bridge']]['if']}\n");
134
		fclose($fd);
135
		
136
		/* lets keep track of the amount of bridges initialized */
137
		$bridges_total++;
138
	}
139
	
140
	/* media */
141
	if ($lancfg['media'] || $lancfg['mediaopt']) {
142
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
143
		if ($lancfg['media'])
144
			$cmd .= " media " . escapeshellarg($lancfg['media']);
145
		if ($lancfg['mediaopt'])
146
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
147
		mwexec($cmd);
148
	}
149

    
150
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
151
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
152

    
153
	if (!$g['booting']) {
154
		/* make new hosts file */
155
		system_hosts_generate();
156

    
157
		/* reconfigure static routes (kernel may have deleted them) */
158
		system_routing_configure();
159

    
160
		/* set the reload filter dity flag */
161
		touch("{$g['tmp_path']}/filter_dirty");
162

    
163
		/* reload IPsec tunnels */
164
		vpn_ipsec_configure();
165

    
166
		/* reload dhcpd (gateway may have changed) */
167
		services_dhcpd_configure();
168

    
169
		/* reload dnsmasq */
170
		services_dnsmasq_configure();
171

    
172
		/* reload webgui */
173
		system_webgui_start();
174

    
175
		/* reload captive portal */
176
		captiveportal_configure();
177
	}
178

    
179
	return 0;
180
}
181

    
182
function interfaces_optional_configure() {
183
	global $config, $g;
184
	global $bridgeconfig;
185

    
186
	/* Reset bridge configuration.	Interfaces will add to it. */
187
	$bridgeconfig = "";
188

    
189
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
190
		interfaces_optional_configure_if($i);
191
	}
192

    
193
	if (!$g['booting']) {
194
		/* reconfigure static routes (kernel may have deleted them) */
195
		system_routing_configure();
196

    
197
		/* reload IPsec tunnels */
198
		vpn_ipsec_configure();
199

    
200
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
201
		services_dhcpd_configure();
202

    
203
		/* restart dnsmasq */
204
		services_dnsmasq_configure();
205

    
206
		/* set the reload filter dity flag */
207
		touch("{$g['tmp_path']}/filter_dirty");				
208
	}
209

    
210
	return 0;
211
}
212

    
213
function interfaces_optional_configure_if($opti) {
214
	global $config, $g;
215
	global $bridgeconfig;
216
	global $bridges_total;
217

    
218
	$optcfg = $config['interfaces']['opt' . $opti];
219

    
220
	if ($g['booting']) {
221
		$optdescr = "";
222
		if ($optcfg['descr'])
223
			$optdescr = " ({$optcfg['descr']})";
224
		print "\tOPT{$opti}{$optdescr}... ";
225
	}
226

    
227
	if (isset($optcfg['enable'])) {
228
		/* wireless configuration? */
229
		if (is_array($optcfg['wireless']))
230
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
231

    
232
		/* MAC spoofing? */
233
		if ($optcfg['spoofmac']) {
234
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
235
				" link " . escapeshellarg($optcfg['spoofmac']));
236
		} else {
237
			$mac = get_interface_mac_address($optcfg['if']);
238
			if($mac == "ff:ff:ff:ff:ff:ff") {
239
				/*   this is not a valid mac address.  generate a
240
				 *   temporary mac address so the machine can get online.
241
				 */
242
				echo "Generating new MAC address.";
243
				$random_mac = generate_random_mac_address();
244
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
245
					" link " . escapeshellarg($random_mac));
246
				$optcfg['spoofmac'] = $random_mac;
247
				write_config();
248
				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");
249
			}
250
		}
251

    
252
		/* media */
253
		if ($optcfg['media'] || $optcfg['mediaopt']) {
254
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
255
			if ($optcfg['media'])
256
				$cmd .= " media " . escapeshellarg($optcfg['media']);
257
			if ($optcfg['mediaopt'])
258
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
259
			mwexec($cmd);
260
		}
261

    
262
		/* OpenVPN configuration? */
263
 		if (isset($optcfg['ovpn'])) {
264
 			if (strstr($optcfg['if'], "tap"))
265
 				ovpn_link_tap();
266
 		}
267

    
268
		/* bridged? */
269
		if ($optcfg['bridge']) {
270
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
271
                        /* use open/netBSD style bridge */
272
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
273
                        mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up");
274
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} add {$config['interfaces'][$optcfg['bridge']]['if']}");
275
			
276
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
277
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
278
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up\n");
279
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} add {$config['interfaces'][$optcfg['bridge']]['if']}\n");
280
			fclose($fd);
281
			
282
			/* lets keep track of the amount of bridges initialized */
283
			$bridges_total++;
284
		} else {
285
			/* if user has selected DHCP type then act accordingly */
286
			if($optcfg['ipaddr'] == "dhcp") {
287
				interfaces_opt_dhcp_configure("opt{$opti}");
288
			} else {			
289
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
290
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
291
			}
292
		}
293
	} else {
294
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
295
	}
296

    
297
	return 0;
298
}
299

    
300
function interfaces_carp_configure() {
301
	global $g, $config;
302
	if ($g['booting'] and !$debugging) {
303
		echo "Configuring CARP interfaces...";
304
		mute_kernel_msgs();
305
	}
306
	unlink_if_exists("/usr/local/etc/rc.d/carp.sh");
307
	unlink_if_exists("/usr/local/pkg/pf/carp.sh");
308
	unlink_if_exists("/usr/local/pkg/pf/carp_rules.sh");
309
	$carp_instances_counter = 0;
310
	$pfsync_instances_counter = 0;
311
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
312
	if (is_array($config['virtualip']['vip'])) {
313
		if(is_array($config['installedpackages']['carpsettings']['config'])) {
314
			foreach($config['installedpackages']['carpsettings']['config'] as $carp)
315
				if($carp['pfsyncenabled'] != "") {
316
					if($debugging) 
317
						echo "Enabling preempt\n";
318
					if($carp['premption'] != "")
319
						mwexec("/sbin/sysctl net.inet.carp.preempt=1");
320
					if($carp['balancing'] != "")
321
						mwexec("/sbin/sysctl net.inet.arpbalance=1");
322
					if($debugging) 
323
						echo "Get friendly interface name.\n";
324
					$carp_sync_int = convert_friendly_interface_to_real_interface_name($carp['pfsyncinterface']);
325
					/* do not setup pfsync twice */
326
					if($total_carp_interfaces_defined == 0) {
327
						if($debugging)
328
							echo "Bringing up pfsync0.\n";
329
						mwexec("/sbin/ifconfig pfsync0 create");
330
						if($debugging)
331
							echo "Assigning syncdev to {$carp_sync_int}.\n";						
332
						mwexec("/sbin/ifconfig pfsync0 syncdev " . $carp_sync_int);
333
						if($debugging)
334
							echo "Bringing up syncif pfsync0.\n";
335

    
336
						mwexec("/sbin/ifconfig pfsync0 up");
337
					}
338
					if($g['booting']) {
339
						/* install rules to alllow pfsync to sync up during boot
340
						* carp interfaces will remain down until the bootup sequence finishes
341
						*/
342
						if($debugging) 
343
							echo "Adding firewall rules..\n";
344
						exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
345
						exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
346
						exec("echo pass out proto { tcp, udp } from any to any port 53 keep state >> /tmp/rules.boot");
347
						exec("/sbin/pfctl -f /tmp/rules.boot");
348
					}
349
					$pfsync_instances_counter++;
350
				}
351
		}
352
		$viparr = &$config['virtualip']['vip'];
353
		foreach ($viparr as $vip) {
354
			if ($vip['mode'] == "carp") {
355
				/*
356
				*  create the carp interface
357
				*/
358
				if($debugging)
359
					echo "Creating carp{$carp_instances_counter}.\n";
360
				mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
361
				$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
362
				if($vip['password'] != "") {
363
					$password = " pass " . $vip['password'];
364
				}
365
				/* XXX: billm - carpdev not in our build?
366
				$carpdev = "";
367
				if(isset($vip['interface']) && ($vip['interface'] != "AUTO" && $vip['interface'] != "")) {
368
					$ci = filter_opt_interface_to_real($vip['interface']);
369
					$carpdev = " carpdev {$ci} ";
370
				}
371
				*/
372
				if($debugging)
373
					echo "Configuring carp{$carp_instances_counter}.\n";
374
				mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
375
				if($g['booting']) 
376
					mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " down");
377
				$carp_instances_counter++;
378
			}
379
		}
380
	} else {
381
		/* Hush little pfsync, don't say a word.
382
		   GeekGod's gonna buy you a mocking bird. */
383
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
384
	}
385
	if ($g['booting'] and !$debugging) {
386
		unmute_kernel_msgs();
387
		echo "done.\n";
388
	}
389
}
390

    
391
function interfaces_carp_bring_up_final() {
392
	global $config, $g;
393
	$carp_instances_counter = 0;
394
	$viparr = &$config['virtualip']['vip'];
395
	if(!is_array($viparr))
396
		return;
397
	foreach ($viparr as $vip) {
398
		echo "Upping interface carp{$carp_instances_counter}.\n";
399
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
400
		$carp_instances_counter++;
401
	}
402
}
403

    
404
function interfaces_wireless_configure($if, $wlcfg) {
405
	global $config, $g;
406
	
407
	/* set values for /path/program */
408
	$hostapd = "/usr/sbin/hostapd";
409
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
410
	$ifconfig = "/sbin/ifconfig ";
411
	$killall = "/usr/bin/killall ";
412

    
413
	/* Sett all wireless ifconfig variables (splitt up to get rid of needed checking) */
414

    
415
	/* Set a/b/g standard */
416
	$standard = ("mode " . escapeshellarg($wlcfg['standard']));
417

    
418
	/* set wireless channel value */
419
	$channel = escapeshellarg($wlcfg['channel']);
420
	
421
	if($channel == "") {
422
		$channel = "";
423
	} else { 
424
		$channel = ("channel " . escapeshellarg($wlcfg['channel']));
425
	}
426

    
427
	/* Set ssid */
428
	$ssid = ("ssid " . escapeshellarg($wlcfg['ssid']));
429

    
430
	/* Set stationname */
431
	if (!$wlcfg['stationname'])
432
		$stationname = "pfsense";
433
	else
434
		$stationname = ("stationname " . escapeshellarg($wlcfg['stationname']));
435

    
436
	/* Set wireless hostap mode */
437
	if ($wlcfg['mode'] == hostap)
438
		$hostapmode = "mediaopt hostap";
439
	else
440
		$hostapmode = "-mediaopt hostap";
441

    
442
	/* Set wireless adhoc mode */
443
	if ($wlcfg['mode'] == adhoc)
444
		$adhocmode = "mediaopt adhoc";
445
	else
446
		$adhocmode = "-mediaopt adhoc";
447

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

    
450
	/* handle hide ssid option */
451
	if(isset($wlcfg['hidessid']['enable']))
452
		$hidessid = "hidessid";
453
	else
454
		$hidessid = "-hidessid";
455

    
456
	/* handle pureg (802.11g) only option */
457
	if(isset($wlcfg['pureg']['enable']))
458
		$pureg = "mode 11g pureg";
459
	else
460
		$pureg = "-pureg";
461

    
462
	/* enable apbridge option */
463
	if(isset($wlcfg['apbridge']['enable']))
464
		$apbridge = "apbridge";
465
	else
466
		$apbridge = "-apbridge";
467

    
468
	/* handle turbo option */
469
	if(isset($wlcfg['turbo']['enable']))
470
		$turbo = "mediaopt turbo";
471
	else
472
		$turbo = "-mediaopt turbo";
473

    
474
	/* handle txpower setting */
475
	if($wlcfg['txpower'] <> "")
476
		$txpower = ("txpower " . escapeshellarg($wlcfg['txpower']));
477
	
478
	/* handle wme option */
479
	if(isset($wlcfg['wme']['enable']))
480
		$wme = "wme";
481
	else
482
		$wme = "-wme";
483
	
484
	/* set up wep if enabled */
485
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
486
                $wepset .= "authmode shared wepmode on ";
487

    
488
                $i = 1;
489
                foreach ($wlcfg['wep']['key'] as $wepkey) {
490
                        $wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
491
                        if (isset($wepkey['txkey'])) {
492
                                $wepset .= "weptxkey {$i} ";
493
                        }
494
                        $i++;
495
                }
496
        } else {
497
                $wepset = "authmode open wepmode off";
498
	}
499

    
500
	/* generate wpa_supplicant/hostap config if wpa is enabled */
501

    
502
	switch ($wlcfg['mode']) {
503
		case 'BSS':
504
			if (isset($wlcfg['wpa']['enable'])) {
505

    
506
				$wpa .= <<<EOD
507
ctrl_interface={$g['varrun_path']}/hostapd
508
ctrl_interface_group=0
509
ap_scan=1
510
#fast_reauth=1
511
network={
512
ssid={$wlcfg['ssid']}
513
scan_ssid=2
514
priority=5
515
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
516
psk={$wlcfg['wpa']['passphrase']}
517
pairwise={$wlcfg['wpa']['wpa_pairwise']}
518
group={$wlcfg['wpa']['wpa_pairwise']}
519
}
520
EOD;
521

    
522
				$fd = fopen("{$g['tmp_path']}/wpa_supplicant_{$if}.conf", "w");
523
				fwrite($fd, "{$wpa}");
524
				fclose($fd);
525

    
526
				if(is_process_running("wpa_supplicant"))
527
					mwexec("$killall" . " wpa_supplicant");
528
			}
529
		break;
530

    
531
		case 'hostap':
532
			if (isset($wlcfg['wpa']['enable'])) {
533
				$wpa .= <<<EOD
534
interface={$if}
535
driver=bsd
536
logger_syslog=-1
537
logger_syslog_level=0
538
logger_stdout=-1
539
logger_stdout_level=0
540
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
541
ctrl_interface={$g['varrun_path']}/hostapd
542
ctrl_interface_group=wheel
543
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
544
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
545
ssid={$wlcfg['ssid']}
546
debug={$wlcfg['wpa']['debug_mode']}
547
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
548
auth_algs={$wlcfg['wpa']['auth_algs']}
549
wpa={$wlcfg['wpa']['wpa_mode']}
550
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
551
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
552
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
553
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
554
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
555
wpa_passphrase={$wlcfg['wpa']['passphrase']}
556
ieee8021x={$wlcfg['wpa']['ieee8021x']}
557
EOD;
558

    
559
				$fd = fopen("{$g['tmp_path']}/hostapd_{$if}.conf", "w");
560
				fwrite($fd, "{$wpa}");
561
				fclose($fd);
562

    
563
				if(is_process_running("hostapd"))
564
					mwexec("$killall" . " hostapd");
565
			}
566
		break;
567

    
568
		case 'adhoc':
569
			if(is_process_running("hostapd"))
570
				mwexec("$killall hostapd");
571

    
572
			if(is_process_running("wpa_supplicant"))
573
				mwexec("$killall wpa_supplicant");
574
		break;
575
	}	
576

    
577
	/* start up everything */
578
	        
579
	mwexec("$ifconfig {$if}" . " down"); 
580
	mwexec("$ifconfig $if" . " " . $standard);
581
	mwexec("$ifconfig $if" . " " . $channel);
582
	mwexec("$ifconfig $if" . " " . $ssid);
583
	mwexec("$ifconfig $if" . " " . $stationname);
584
	mwexec("$ifconfig $if" . " " . $hostapmode);
585
	mwexec("$ifconfig $if" . " " . $adhocmode);
586
	mwexec("$ifconfig $if" . " " . $hidessid);
587
	mwexec("$ifconfig $if" . " " . $pureg);
588
	mwexec("$ifconfig $if" . " " . $apbridge);
589
	mwexec("$ifconfig $if" . " " . $turbo);
590
	mwexec("$ifconfig $if" . " " . $wme);
591
	mwexec("$ifconfig $if" . " " . $wepset);
592
	mwexec("$ifconfig $if" . " up"); 
593

    
594
	if (isset($wlcfg['wpa']['enable'])) {
595
		if ($wlcfg['mode'] == BSS) 
596
			mwexec("$wpa_supplicant -i {$if} -c {$g['etc_path']}/wpa_supplicant_{$if}.conf");
597
		if ($wlcfg['mode'] == hostap) 
598
			mwexec("$hostapd -B {$g['tmp_path']}/hostapd_{$if}.conf");
599
	}
600

    
601
	/* Write ifconfig settings to tmp file so we can see if user set something weird */ 
602
//	$ifcargs = ("$standard $channel $ssid $stationname $hostapmode $adhocmode $hidessid $pureg $apbridge $turbo $wme $wepset");
603
//	$fd = fopen("{$g['tmp_path']}/ifconfig_wireless", "w");
604
//	fwrite($fd, "/sbin/ifconfig {$ifcargs}");
605
//	fclose($fd);
606

    
607
	/* Write wep crap out */
608
//	$fd = fopen("{$g['tmp_path']}/ifconfig_wep", "w");
609
//	fwrite($fd, "sbin/ifconfig {$wepset}");
610
//	fclose($fd);
611
	
612
	if(isset($wlcfg['useolsr']))
613
		setup_wireless_olsr(escapeshellarg($if));
614

    
615
	return 0;
616

    
617
}
618

    
619
function find_dhclient_process($interface) {
620
	if(filter_translate_type_to_real_interface($interface) <> "")
621
        	$realinterface = filter_translate_type_to_real_interface($interface);
622
	$pid = `ps ax | grep "[d]hclient" | grep {$realinterface} | awk -F" " '{print $1}'`;
623
	return $pid;
624
}
625

    
626
function interfaces_wan_configure() {
627
	global $config, $g;
628

    
629
	$wancfg = $config['interfaces']['wan'];
630

    
631
	if(!$g['booting']) {
632
		mute_kernel_msgs();
633

    
634
		/* find dhclient process for wan and kill it */
635
		killbypid(find_dhclient_process("wan"));
636

    
637
		/* kill PPPoE client (mpd) */
638
		killbypid("{$g['varrun_path']}/mpd.pid");
639

    
640
		/* wait for processes to die */
641
		sleep(1);
642

    
643
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
644
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
645
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
646
		unlink_if_exists("{$g['vardb_path']}/wanip");
647
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
648
	}
649

    
650
	/* remove all addresses first */
651
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
652
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
653

    
654
	/* wireless configuration? */
655
	if (is_array($wancfg['wireless']))
656
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
657

    
658
	if ($wancfg['spoofmac']) {
659
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
660
			" link " . escapeshellarg($wancfg['spoofmac']));
661
	}  else {
662
		$mac = get_interface_mac_address($wancfg['if']);
663
		if($mac == "ff:ff:ff:ff:ff:ff") {
664
			/*   this is not a valid mac address.  generate a
665
			 *   temporary mac address so the machine can get online.
666
			 */
667
			echo "Generating new MAC address.";
668
			$random_mac = generate_random_mac_address();
669
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
670
				" link " . escapeshellarg($random_mac));
671
			$wancfg['spoofmac'] = $random_mac;
672
			write_config();
673
			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");
674
		}
675
	}
676

    
677
	/* media */
678
	if ($wancfg['media'] || $wancfg['mediaopt']) {
679
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
680
		if ($wancfg['media'])
681
			$cmd .= " media " . escapeshellarg($wancfg['media']);
682
		if ($wancfg['mediaopt'])
683
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
684
		mwexec($cmd);
685
	}
686

    
687
	switch ($wancfg['ipaddr']) {
688

    
689
		case 'dhcp':
690
			interfaces_wan_dhcp_configure();
691
			break;
692

    
693
		case 'pppoe':
694
			interfaces_wan_pppoe_configure();
695
			break;
696

    
697
		case 'pptp':
698
			interfaces_wan_pptp_configure();
699
			break;
700

    
701
		case 'bigpond':
702
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
703
			interfaces_wan_dhcp_configure();
704
			break;
705

    
706
		default:
707
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
708
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
709
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
710
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
711
			} else {
712
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
713
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
714
			}
715
			/* install default route */
716
			mwexec("/sbin/route delete default");
717
			mwexec("/sbin/route add default " . escapeshellarg($config['system']['gateway']));
718

    
719
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
720
			filter_configure();
721
	}
722

    
723
	if (!$g['booting']) {
724
		/* reconfigure static routes (kernel may have deleted them) */
725
		system_routing_configure();
726

    
727
		/* set the reload filter dity flag */
728
		touch("{$g['tmp_path']}/filter_dirty");
729

    
730
		/* reload ipsec tunnels */
731
		vpn_ipsec_configure();
732

    
733
		/* restart ez-ipupdate */
734
		services_dyndns_configure();
735

    
736
		/* force DNS update */
737
		services_dnsupdate_process();
738

    
739
		/* restart dnsmasq */
740
		services_dnsmasq_configure();
741
	}
742

    
743
	unmute_kernel_msgs();
744

    
745
	return 0;
746
}
747

    
748
function interfaces_opt_dhcp_configure($interface) {
749
	global $config, $g;
750

    
751
	$optcfg = $config['interfaces'][$interface];
752
	$optif = $optcfg['if'];
753

    
754
	/* generate dhclient_wan.conf */
755
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
756
	if (!$fd) {
757
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
758
		return 1;
759
	}
760

    
761
	if ($optcfg['dhcphostname']) {
762
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
763
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
764
	} else {
765
		$dhclientconf_hostname = "";
766
	}
767

    
768
 	$dhclientconf = "";
769

    
770
	$dhclientconf .= <<<EOD
771
interface "{$optif}" {
772
	send host-name "{$optcfg['dhcphostname']}";
773
	script "/sbin/dhclient-script";
774
	{$dhclientconf_hostname}
775
}
776

    
777
EOD;
778

    
779
	fwrite($fd, $dhclientconf);
780
	fclose($fd);
781

    
782
        /* bring interface up before starting dhclient */
783
        mwexec("/sbin/ifconfig {$optif} up");
784

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

    
788
	return 0;
789
}
790

    
791
function interfaces_dhcp_configure($interface) {
792
	global $config, $g;
793

    
794
	if(filter_translate_type_to_real_interface($interface) <> "")
795
        	$realinterface = filter_translate_type_to_real_interface($interface);
796

    
797
	$optcfg = $config['interfaces'][$interface];
798

    
799
	/* generate dhclient_$interface.conf */
800
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
801
	if (!$fd) {
802
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
803
		return 1;
804
	}
805

    
806
	if ($optcfg['dhcphostname']) {
807
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
808
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
809
	} else {
810
		$dhclientconf_hostname = "";
811
	}
812

    
813
 	$dhclientconf = "";
814

    
815
	$dhclientconf .= <<<EOD
816
interface "{$realinterface}" {
817
	script "/sbin/dhclient-script";
818
	{$dhclientconf_hostname}
819
}
820

    
821
EOD;
822

    
823
	fwrite($fd, $dhclientconf);
824
	fclose($fd);
825
	
826
	$optif = $optcfg['if'];
827
	
828
        /* bring wan interface up before starting dhclient */
829
        mwexec("/sbin/ifconfig {$optif} up");
830

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

    
834
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
835
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
836
	fclose($fout);
837

    
838
	return 0;
839
}
840

    
841
function interfaces_wan_dhcp_configure() {
842
	global $config, $g;
843

    
844
	$wancfg = $config['interfaces']['wan'];
845

    
846
	/* generate dhclient_wan.conf */
847
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
848
	if (!$fd) {
849
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
850
		return 1;
851
	}
852
	
853
	if ($wancfg['dhcphostname']) {
854
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
855
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
856
	} else {
857
		$dhclientconf_hostname = "";
858
	}
859

    
860
 	$dhclientconf = "";
861

    
862
	$dhclientconf .= <<<EOD
863
interface "{$wancfg['if']}" {
864
	script "/sbin/dhclient-script";
865
	{$dhclientconf_hostname}
866
}
867

    
868
EOD;
869

    
870
	fwrite($fd, $dhclientconf);
871
	fclose($fd);
872
	
873
	$wanif = $wancfg['if'];
874
	
875
        /* bring wan interface up before starting dhclient */
876
        mwexec("/sbin/ifconfig {$wanif} up");
877

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

    
881
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
882
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
883
	fclose($fout);
884

    
885
	return 0;
886
}
887

    
888
function interfaces_wan_dhcp_down() {
889
	global $config;
890
	$wancfg = $config['interfaces']['wan'];
891
	$wanif = $wancfg['if'];
892
	mwexec("/sbin/ifconfig {$wanif} delete");
893
	sleep(1);
894
}
895

    
896
function interfaces_dhcp_down($interface) {
897
	global $config;
898
	if(filter_translate_type_to_real_interface($interface) <> "")
899
		$realinterface = filter_translate_type_to_real_interface($interface);
900
	mwexec("/sbin/ifconfig {$realinterface} down");
901
	sleep(1);
902
	$pid = find_dhclient_process($interface);
903
	if($pid)
904
		mwexec("kill {$pid}");
905
}
906

    
907
function interfaces_dhcp_up($interface) {
908
	interfaces_dhcp_configure($interface);
909
	sleep(1);
910
}
911

    
912
function interfaces_wan_dhcp_up() {
913
	interfaces_wan_dhcp_configure();
914
	sleep(1);
915
}
916

    
917
function interfaces_wan_pppoe_configure() {
918
	global $config, $g;
919

    
920
	$wancfg = $config['interfaces']['wan'];
921
	$pppoecfg = $config['pppoe'];
922

    
923
	/* generate mpd.conf */
924
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
925
	if (!$fd) {
926
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
927
		return 1;
928
	}
929

    
930
	$idle = 0;
931

    
932
	if (isset($pppoecfg['ondemand'])) {
933
		$ondemand = "enable";
934
		if ($pppoecfg['timeout'])
935
			$idle = $pppoecfg['timeout'];
936
	} else {
937
		$ondemand = "disable";
938
	}
939

    
940
	$mpdconf = <<<EOD
941
pppoe:
942
	new -i ng0 pppoe pppoe
943
	set iface route default
944
	set iface {$ondemand} on-demand
945
	set iface idle {$idle}
946
	set iface up-script /usr/local/sbin/ppp-linkup
947

    
948
EOD;
949

    
950
	if (isset($pppoecfg['ondemand'])) {
951
		$mpdconf .= <<<EOD
952
	set iface addrs 10.0.0.1 10.0.0.2
953

    
954
EOD;
955
	}
956

    
957
	$mpdconf .= <<<EOD
958
	set bundle disable multilink
959
	set bundle authname "{$pppoecfg['username']}"
960
	set bundle password "{$pppoecfg['password']}"
961
	set link keep-alive 10 60
962
	set link max-redial 0
963
	set link no acfcomp protocomp
964
	set link disable pap chap
965
	set link accept chap
966
	set link mtu 1492
967
	set ipcp yes vjcomp
968
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
969

    
970
EOD;
971

    
972
	if (isset($config['system']['dnsallowoverride'])) {
973
		$mpdconf .= <<<EOD
974
	set ipcp enable req-pri-dns
975

    
976
EOD;
977
	}
978

    
979
	$mpdconf .= <<<EOD
980
	open iface
981

    
982
EOD;
983

    
984
	fwrite($fd, $mpdconf);
985
	fclose($fd);
986

    
987
	/* generate mpd.links */
988
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
989
	if (!$fd) {
990
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
991
		return 1;
992
	}
993

    
994
	$mpdconf = <<<EOD
995
pppoe:
996
	set link type pppoe
997
	set pppoe iface {$wancfg['if']}
998
	set pppoe service "{$pppoecfg['provider']}"
999
	set pppoe enable originate
1000
	set pppoe disable incoming
1001

    
1002
EOD;
1003

    
1004
	fwrite($fd, $mpdconf);
1005
	fclose($fd);
1006

    
1007
	/* if mpd is active, lets take it down */
1008
	if(file_exists("{$g['varrun_path']}/mpd.pid")) {
1009
		killbypid(file_get_contents("{$g['varrun_path']}/mpd.pid"));
1010
		sleep(1);
1011
	}
1012

    
1013
	/* fire up mpd */
1014
	mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pppoe");
1015

    
1016
	return 0;
1017
}
1018

    
1019
function interfaces_wan_pppoe_down() {
1020
	global $g;
1021
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1022
	sleep(1);
1023
}
1024

    
1025
function interfaces_wan_pppoe_up() {
1026
	global $g;
1027
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1028
	sleep(1);
1029
}
1030

    
1031
function interfaces_wan_pptp_configure() {
1032
	global $config, $g;
1033

    
1034
	$wancfg = $config['interfaces']['wan'];
1035
	$pptpcfg = $config['pptp'];
1036

    
1037
	/* generate mpd.conf */
1038
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1039
	if (!$fd) {
1040
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1041
		return 1;
1042
	}
1043

    
1044
	$idle = 0;
1045

    
1046
	if (isset($pptpcfg['ondemand'])) {
1047
		$ondemand = "enable";
1048
		if ($pptpcfg['timeout'])
1049
			$idle = $pptpcfg['timeout'];
1050
	} else {
1051
		$ondemand = "disable";
1052
	}
1053

    
1054
	$mpdconf = <<<EOD
1055
pptp:
1056
	new -i ng0 pptp pptp
1057
	set iface route default
1058
	set iface {$ondemand} on-demand
1059
	set iface idle {$idle}
1060
	set iface up-script /usr/local/sbin/ppp-linkup
1061

    
1062
EOD;
1063

    
1064
	if (isset($pptpcfg['ondemand'])) {
1065
		$mpdconf .= <<<EOD
1066
	set iface addrs 10.0.0.1 10.0.0.2
1067

    
1068
EOD;
1069
	}
1070

    
1071
	$mpdconf .= <<<EOD
1072
	set bundle disable multilink
1073
	set bundle authname "{$pptpcfg['username']}"
1074
	set bundle password "{$pptpcfg['password']}"
1075
	set link keep-alive 10 60
1076
	set link max-redial 0
1077
	set link no acfcomp protocomp
1078
	set link disable pap chap
1079
	set link accept chap
1080
	set ipcp no vjcomp
1081
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1082

    
1083
EOD;
1084

    
1085
	if (isset($config['system']['dnsallowoverride'])) {
1086
		$mpdconf .= <<<EOD
1087
	set ipcp enable req-pri-dns
1088

    
1089
EOD;
1090
	}
1091

    
1092
	$mpdconf .= <<<EOD
1093
	open
1094

    
1095
EOD;
1096

    
1097
	fwrite($fd, $mpdconf);
1098
	fclose($fd);
1099

    
1100
	/* generate mpd.links */
1101
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1102
	if (!$fd) {
1103
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1104
		return 1;
1105
	}
1106

    
1107
	$mpdconf = <<<EOD
1108
pptp:
1109
	set link type pptp
1110
	set pptp enable originate outcall
1111
	set pptp disable windowing
1112
	set pptp self {$pptpcfg['local']}
1113
	set pptp peer {$pptpcfg['remote']}
1114

    
1115
EOD;
1116

    
1117
	fwrite($fd, $mpdconf);
1118
	fclose($fd);
1119

    
1120
	/* configure interface */
1121
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1122
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1123

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

    
1127
	return 0;
1128
}
1129

    
1130
function interfaces_wan_pptp_down() {
1131
	global $g;
1132
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1133
	sleep(1);
1134
}
1135

    
1136
function interfaces_wan_pptp_up() {
1137
	global $g;
1138
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1139
	sleep(1);
1140
}
1141

    
1142
function interfaces_wan_bigpond_configure($curwanip) {
1143
	global $config, $g;
1144

    
1145
	$bpcfg = $config['bigpond'];
1146

    
1147
	if (!$curwanip) {
1148
		/* IP address not configured yet, exit */
1149
		return 0;
1150
	}
1151

    
1152
	/* kill bpalogin */
1153
	killbyname("bpalogin");
1154

    
1155
	/* wait a moment */
1156
	sleep(1);
1157

    
1158
	/* get the default domain */
1159
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1160
	if ($nfd) {
1161
		$defaultdomain = trim(fgets($nfd));
1162
		fclose($nfd);
1163
	}
1164

    
1165
	/* generate bpalogin.conf */
1166
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1167
	if (!$fd) {
1168
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1169
		return 1;
1170
	}
1171

    
1172
	if (!$bpcfg['authserver'])
1173
		$bpcfg['authserver'] = "dce-server";
1174
	if (!$bpcfg['authdomain'])
1175
		$bpcfg['authdomain'] = $defaultdomain;
1176

    
1177
	$bpconf = <<<EOD
1178
username {$bpcfg['username']}
1179
password {$bpcfg['password']}
1180
authserver {$bpcfg['authserver']}
1181
authdomain {$bpcfg['authdomain']}
1182
localport 5050
1183

    
1184
EOD;
1185

    
1186
	if ($bpcfg['minheartbeatinterval'])
1187
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1188

    
1189
	fwrite($fd, $bpconf);
1190
	fclose($fd);
1191

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

    
1195
	return 0;
1196
}
1197

    
1198
function get_real_wan_interface() {
1199
	global $config, $g;
1200

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

    
1203
	$wanif = $wancfg['if'];
1204
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1205
		$wanif = $g['pppoe_interface'];
1206
	}
1207

    
1208
	return $wanif;
1209
}
1210

    
1211
function get_current_wan_address($interface = "wan") {
1212
	global $config, $g;
1213

    
1214
	$wancfg = $config['interfaces'][$interface];
1215

    
1216
	$interface = filter_translate_type_to_real_interface($interface);
1217

    
1218
	if(in_array($wancfg['ipaddr'], array('dhcp'))) {
1219
		/* get interface info with netstat */
1220
		exec("/usr/bin/netstat -nWI " . escapeshellarg($interface) . " -f inet", $ifinfo);
1221

    
1222
		if (isset($ifinfo[1])) {
1223
			$aif = preg_split("/\s+/", $ifinfo[1]);
1224
			$curwanip = chop($aif[3]);
1225

    
1226
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1227
				return $curwanip;
1228
		}
1229

    
1230
		return null;		
1231
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1232
		/* dynamic WAN IP address, find out which one */
1233
		$wanif = get_real_wan_interface();
1234

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

    
1238
		if (isset($ifinfo[1])) {
1239
			$aif = preg_split("/\s+/", $ifinfo[1]);
1240
			$curwanip = chop($aif[3]);
1241

    
1242
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1243
				return $curwanip;
1244
		}
1245

    
1246
		return null;
1247
	} else {
1248
		/* static WAN IP address */
1249
		return $wancfg['ipaddr'];
1250
	}
1251
}
1252

    
1253
/****f* interfaces/is_altq_capable
1254
 * NAME
1255
 *   is_altq_capable - Test if interface is capable of using ALTQ
1256
 * INPUTS
1257
 *   $int            - string containing interface name
1258
 * RESULT
1259
 *   boolean         - true or false
1260
 ******/
1261

    
1262
function is_altq_capable($int) {
1263
        /* Per:
1264
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1265
         * Only the following drivers have ALTQ support
1266
         */
1267
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1268
		"em", "fxp", "hme", "lnc", "ndis", "rl", "sf", "sis", "sk",
1269
		"tun", "vr", "wi", "xl");
1270

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

    
1273
        if (in_array($int_family[0], $capable))
1274
                return true;
1275
        else
1276
                return false;
1277
}
1278

    
1279

    
1280
?>
(8-8/24)