Project

General

Profile

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

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

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

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

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

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

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

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

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

    
44
	return 0;
45
}
46

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

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

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

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

    
58
		$i = 0;
59

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

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

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

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

    
78
			mwexec($cmd);
79

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

    
83
			$i++;
84
		}
85
	}
86

    
87
	return 0;
88
}
89

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

    
93
	$bridges_total = get_next_available_bridge_interface();
94

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

    
97
	/* 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
		/* use open/netBSD style bridge */
125
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
126
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up");
127
		mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}");
128
		
129
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
130
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
131
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up\n");
132
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}\n");
133
		fclose($fd);
134

    
135
	}
136
	
137
	/* media */
138
	if ($lancfg['media'] || $lancfg['mediaopt']) {
139
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
140
		if ($lancfg['media'])
141
			$cmd .= " media " . escapeshellarg($lancfg['media']);
142
		if ($lancfg['mediaopt'])
143
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
144
		mwexec($cmd);
145
	}
146

    
147
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
148
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
149

    
150
	if (!$g['booting']) {
151
		/* make new hosts file */
152
		system_hosts_generate();
153

    
154
		/* reconfigure static routes (kernel may have deleted them) */
155
		system_routing_configure();
156

    
157
		/* set the reload filter dity flag */
158
		touch("{$g['tmp_path']}/filter_dirty");
159

    
160
		/* reload IPsec tunnels */
161
		vpn_ipsec_configure();
162

    
163
		/* reload dhcpd (gateway may have changed) */
164
		services_dhcpd_configure();
165

    
166
		/* reload dnsmasq */
167
		services_dnsmasq_configure();
168

    
169
		/* reload webgui */
170
		system_webgui_start();
171

    
172
		/* reload captive portal */
173
		captiveportal_configure();
174
	}
175

    
176
	return 0;
177
}
178

    
179
function interfaces_optional_configure() {
180
	global $config, $g;
181
	global $bridgeconfig;
182

    
183
	/* Reset bridge configuration.	Interfaces will add to it. */
184
	$bridgeconfig = "";
185

    
186
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
187
		interfaces_optional_configure_if($i);
188
	}
189

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

    
194
		/* reload IPsec tunnels */
195
		vpn_ipsec_configure();
196

    
197
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
198
		services_dhcpd_configure();
199

    
200
		/* restart dnsmasq */
201
		services_dnsmasq_configure();
202

    
203
		/* set the reload filter dity flag */
204
		touch("{$g['tmp_path']}/filter_dirty");				
205
	}
206

    
207
	return 0;
208
}
209

    
210
function interfaces_optional_configure_if($opti) {
211
	global $config, $g;
212
	global $bridgeconfig, $debugging;
213

    
214
	$bridges_total = get_next_available_bridge_interface();
215

    
216
	$optcfg = $config['interfaces']['opt' . $opti];
217

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

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

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

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

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

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

    
293
function interfaces_carp_configure() {
294
	global $g, $config, $debugging;
295
	$carp_instances_counter = 0;
296
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
297
	if(isset($config['system']['developerspew'])) {
298
		$mt = microtime();
299
		echo "interfaces_carp_configure() being called $mt\n";
300
	}
301
	if ($g['booting'] and !$debugging) {
302
		echo "Configuring CARP interfaces...";
303
		mute_kernel_msgs();
304
	}
305
	/* if neither items are arrays then redirect pfsync to loopback */
306
	if (!is_array($config['virtualip']['vip']) or
307
	    !is_array($config['installedpackages']['carpsettings']['config'])) {
308
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
309
		return;
310
	}
311
	foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
312
		if($carp['pfsyncenabled'] != "") {
313
			mwexec("/sbin/sysctl net.inet.carp.allow=1");
314
			if($debugging) 
315
				echo "Enabling preempt\n";
316
			if($carp['premption'] != "")
317
				mwexec("/sbin/sysctl net.inet.carp.preempt=1");
318
			if($carp['balancing'] != "")
319
				mwexec("/sbin/sysctl net.inet.carp.arpbalance=1");
320
			if($debugging) 
321
				echo "Get friendly interface name {$carp['pfsyncinterface']}.\n";
322
			$carp_sync_int = convert_friendly_interface_to_real_interface_name($carp['pfsyncinterface']);
323
			if($debugging) 
324
				echo "Friendly name {$carp_sync_int}.\n";
325
			$carp_sync_int = convert_friendly_interface_to_real_interface_name($carp['pfsyncinterface']);
326
			if($g['booting']) {
327
				/*    install rules to alllow pfsync to sync up during boot
328
				 *    carp interfaces will remain down until the bootup sequence finishes
329
				 */
330
				if($debugging) 
331
					echo "Adding firewall rules..\n";
332
				exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
333
				exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
334
				exec("echo pass out proto { tcp, udp } from any to any port 53 keep state >> /tmp/rules.boot");
335
				exec("/sbin/pfctl -f /tmp/rules.boot");
336
				if($debugging) {
337
					echo "Showing loaded rule set:\n";
338
					system("/sbin/pfctl -vvsr");
339
				}
340
			}			
341
			/* do not setup pfsync twice */
342
			if($total_carp_interfaces_defined == 0) {
343
				//if($debugging)
344
				//	echo "Bringing up pfsync0.\n";
345
				//mwexec("/sbin/ifconfig pfsync0 create");
346
				if($debugging)
347
					echo "Assigning syncdev to {$carp_sync_int}.\n";						
348
				mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up");
349
			}
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
			/* XXX: billm - carpdev not in our build?
365
			    $carpdev = "";
366
			    if(isset($vip['interface']) && ($vip['interface'] != "AUTO" && $vip['interface'] != "")) {
367
			   	$ci = filter_opt_interface_to_real($vip['interface']);
368
			   	$carpdev = " carpdev {$ci} ";
369
			    }
370
			*/
371
			if($debugging)
372
				echo "Configuring carp{$carp_instances_counter}.\n";
373
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password);
374
			if($g['booting']) 
375
				mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
376
			$carp_instances_counter++;
377
		}
378
	}
379
	if ($g['booting'] and !$debugging) {
380
		unmute_kernel_msgs();
381
		echo "done.\n";
382
	}
383
}
384

    
385
function interfaces_carp_bring_up_final() {
386
	global $config, $g, $debugging;
387
	if(isset($config['system']['developerspew'])) {
388
		$mt = microtime();
389
		echo "interfaces_carp_bring_up_final() being called $mt\n";
390
	}
391
	$viparr = &$config['virtualip']['vip'];
392
	/* could not locate an array, return */
393
	if(!is_array($viparr)) 
394
		return;
395
	$carp_instances_counter = 0;
396
	foreach ($viparr as $vip) {
397
		if($debugging)
398
			echo "Upping interface carp{$carp_instances_counter}.\n";
399
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
400
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
401
		if($vip['password'] != "") 
402
			$password = " pass " . $vip['password'];
403
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
404
		if($debugging)
405
			echo "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password . "\n";
406
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
407
		$carp_instances_counter++;
408
	}
409
}
410

    
411
function interfaces_wireless_configure($if, $wlcfg) {
412
	global $config, $g;
413
	
414
	/* set values for /path/program */
415
	$hostapd = "/usr/sbin/hostapd";
416
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
417
	$ifconfig = "/sbin/ifconfig ";
418
	$killall = "/usr/bin/killall ";
419

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

    
422
	/* Set a/b/g standard */
423
	$standard = ("mode " . escapeshellarg($wlcfg['standard']));
424

    
425
	/* set wireless channel value */
426
	$channel = escapeshellarg($wlcfg['channel']);
427
	
428
	if($channel == "") {
429
		$channel = "";
430
	} else { 
431
		$channel = ("channel " . escapeshellarg($wlcfg['channel']));
432
	}
433

    
434
	/* Set ssid */
435
	$ssid = ("ssid " . escapeshellarg($wlcfg['ssid']));
436

    
437
	/* Set stationname */
438
	if (!$wlcfg['stationname'])
439
		$stationname = "pfsense";
440
	else
441
		$stationname = ("stationname " . escapeshellarg($wlcfg['stationname']));
442

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

    
449
	/* Set wireless adhoc mode */
450
	if ($wlcfg['mode'] == adhoc)
451
		$adhocmode = "mediaopt adhoc";
452
	else
453
		$adhocmode = "-mediaopt adhoc";
454

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

    
457
	/* handle hide ssid option */
458
	if(isset($wlcfg['hidessid']['enable']))
459
		$hidessid = "hidessid";
460
	else
461
		$hidessid = "-hidessid";
462

    
463
	/* handle pureg (802.11g) only option */
464
	if(isset($wlcfg['pureg']['enable']))
465
		$pureg = "mode 11g pureg";
466
	else
467
		$pureg = "-pureg";
468

    
469
	/* enable apbridge option */
470
	if(isset($wlcfg['apbridge']['enable']))
471
		$apbridge = "apbridge";
472
	else
473
		$apbridge = "-apbridge";
474

    
475
	/* handle turbo option */
476
	if(isset($wlcfg['turbo']['enable']))
477
		$turbo = "mediaopt turbo";
478
	else
479
		$turbo = "-mediaopt turbo";
480

    
481
	/* handle txpower setting */
482
	if($wlcfg['txpower'] <> "")
483
		$txpower = ("txpower " . escapeshellarg($wlcfg['txpower']));
484
	
485
	/* handle wme option */
486
	if(isset($wlcfg['wme']['enable']))
487
		$wme = "wme";
488
	else
489
		$wme = "-wme";
490
	
491
	/* set up wep if enabled */
492
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
493
                $wepset .= "authmode shared wepmode on ";
494

    
495
                $i = 1;
496
                foreach ($wlcfg['wep']['key'] as $wepkey) {
497
                        $wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
498
                        if (isset($wepkey['txkey'])) {
499
                                $wepset .= "weptxkey {$i} ";
500
                        }
501
                        $i++;
502
                }
503
        } else {
504
                $wepset = "authmode open wepmode off";
505
	}
506

    
507
	/* generate wpa_supplicant/hostap config if wpa is enabled */
508

    
509
	switch ($wlcfg['mode']) {
510
		case 'BSS':
511
			if (isset($wlcfg['wpa']['enable'])) {
512

    
513
				$wpa .= <<<EOD
514
ctrl_interface={$g['varrun_path']}/hostapd
515
ctrl_interface_group=0
516
ap_scan=1
517
#fast_reauth=1
518
network={
519
ssid={$wlcfg['ssid']}
520
scan_ssid=2
521
priority=5
522
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
523
psk={$wlcfg['wpa']['passphrase']}
524
pairwise={$wlcfg['wpa']['wpa_pairwise']}
525
group={$wlcfg['wpa']['wpa_pairwise']}
526
}
527
EOD;
528

    
529
				$fd = fopen("{$g['tmp_path']}/wpa_supplicant_{$if}.conf", "w");
530
				fwrite($fd, "{$wpa}");
531
				fclose($fd);
532

    
533
				if(is_process_running("wpa_supplicant"))
534
					mwexec("$killall wpa_supplicant");
535
			}
536
		break;
537

    
538
		case 'hostap':
539
			if (isset($wlcfg['wpa']['enable'])) {
540
				$wpa .= <<<EOD
541
interface={$if}
542
driver=bsd
543
logger_syslog=-1
544
logger_syslog_level=0
545
logger_stdout=-1
546
logger_stdout_level=0
547
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
548
ctrl_interface={$g['varrun_path']}/hostapd
549
ctrl_interface_group=wheel
550
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
551
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
552
ssid={$wlcfg['ssid']}
553
debug={$wlcfg['wpa']['debug_mode']}
554
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
555
auth_algs={$wlcfg['wpa']['auth_algs']}
556
wpa={$wlcfg['wpa']['wpa_mode']}
557
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
558
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
559
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
560
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
561
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
562
wpa_passphrase={$wlcfg['wpa']['passphrase']}
563
ieee8021x={$wlcfg['wpa']['ieee8021x']}
564
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
565
#rsn_preauth=1
566
#rsn_preauth_interfaces=eth0
567
EOD;
568

    
569
				$fd = fopen("{$g['tmp_path']}/hostapd_{$if}.conf", "w");
570
				fwrite($fd, "{$wpa}");
571
				fclose($fd);
572

    
573
				if(is_process_running("hostapd"))
574
					mwexec("$killall  hostapd");
575
			}
576
		break;
577

    
578
		case 'adhoc':
579
			if(is_process_running("hostapd"))
580
				mwexec("$killall hostapd");
581

    
582
			if(is_process_running("wpa_supplicant"))
583
				mwexec("$killall wpa_supplicant");
584
		break;
585
	}	
586

    
587
	/* start up everything */
588
	        
589
	mwexec("$ifconfig {$if}" . " down"); 
590
	mwexec("$ifconfig $if" . " " . $standard);
591
	mwexec("$ifconfig $if" . " " . $channel);
592
	mwexec("$ifconfig $if" . " " . $ssid);
593
	mwexec("$ifconfig $if" . " " . $stationname);
594
	mwexec("$ifconfig $if" . " " . $hostapmode);
595
	mwexec("$ifconfig $if" . " " . $adhocmode);
596
	mwexec("$ifconfig $if" . " " . $hidessid);
597
	mwexec("$ifconfig $if" . " " . $pureg);
598
	mwexec("$ifconfig $if" . " " . $apbridge);
599
	mwexec("$ifconfig $if" . " " . $turbo);
600
	mwexec("$ifconfig $if" . " " . $wme);
601
	mwexec("$ifconfig $if" . " " . $wepset);
602
	mwexec("$ifconfig $if" . " up"); 
603

    
604
	if (isset($wlcfg['wpa']['enable'])) {
605
		if ($wlcfg['mode'] == BSS) 
606
			mwexec("$wpa_supplicant -i {$if} -c {$g['etc_path']}/wpa_supplicant_{$if}.conf");
607
		if ($wlcfg['mode'] == hostap) 
608
			mwexec("$hostapd -B {$g['tmp_path']}/hostapd_{$if}.conf");
609
	}
610

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

    
617
	/* Write wep crap out */
618
//	$fd = fopen("{$g['tmp_path']}/ifconfig_wep", "w");
619
//	fwrite($fd, "sbin/ifconfig {$wepset}");
620
//	fclose($fd);
621
	
622
	if(isset($wlcfg['useolsr']))
623
		setup_wireless_olsr(escapeshellarg($if));
624

    
625
	return 0;
626

    
627
}
628

    
629
function find_dhclient_process($interface) {
630
	if(filter_translate_type_to_real_interface($interface) <> "")
631
        	$realinterface = filter_translate_type_to_real_interface($interface);
632
	$pid = `ps ax | grep "[d]hclient" | grep {$realinterface} | awk -F" " '{print $1}'`;
633
	return $pid;
634
}
635

    
636
function interfaces_wan_configure() {
637
	global $config, $g;
638

    
639
	$wancfg = $config['interfaces']['wan'];
640

    
641
	if(!$g['booting']) {
642
		mute_kernel_msgs();
643

    
644
		/* find dhclient process for wan and kill it */
645
		killbypid(find_dhclient_process("wan"));
646

    
647
		/* kill PPPoE client (mpd) */
648
		killbypid("{$g['varrun_path']}/mpd.pid");
649

    
650
		/* wait for processes to die */
651
		sleep(1);
652

    
653
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
654
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
655
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
656
		unlink_if_exists("{$g['vardb_path']}/wanip");
657
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
658
	}
659

    
660
	/* remove all addresses first */
661
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
662
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
663

    
664
	/* wireless configuration? */
665
	if (is_array($wancfg['wireless']))
666
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
667

    
668
	if ($wancfg['spoofmac']) {
669
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
670
			" link " . escapeshellarg($wancfg['spoofmac']));
671
	}  else {
672
		$mac = get_interface_mac_address($wancfg['if']);
673
		if($mac == "ff:ff:ff:ff:ff:ff") {
674
			/*   this is not a valid mac address.  generate a
675
			 *   temporary mac address so the machine can get online.
676
			 */
677
			echo "Generating new MAC address.";
678
			$random_mac = generate_random_mac_address();
679
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
680
				" link " . escapeshellarg($random_mac));
681
			$wancfg['spoofmac'] = $random_mac;
682
			write_config();
683
			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");
684
		}
685
	}
686

    
687
	/* media */
688
	if ($wancfg['media'] || $wancfg['mediaopt']) {
689
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
690
		if ($wancfg['media'])
691
			$cmd .= " media " . escapeshellarg($wancfg['media']);
692
		if ($wancfg['mediaopt'])
693
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
694
		mwexec($cmd);
695
	}
696

    
697
	switch ($wancfg['ipaddr']) {
698

    
699
		case 'dhcp':
700
			interfaces_wan_dhcp_configure();
701
			break;
702

    
703
		case 'pppoe':
704
			interfaces_wan_pppoe_configure();
705
			break;
706

    
707
		case 'pptp':
708
			interfaces_wan_pptp_configure();
709
			break;
710

    
711
		case 'bigpond':
712
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
713
			interfaces_wan_dhcp_configure();
714
			break;
715

    
716
		default:
717
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
718
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
719
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
720
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
721
			} else {
722
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
723
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
724
			}
725
			/* install default route */
726
			mwexec("/sbin/route delete default");
727
			mwexec("/sbin/route add default " . escapeshellarg($config['interfaces']['wan']['gateway']));
728

    
729
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
730
			filter_configure();
731
	}
732

    
733
	if (!$g['booting']) {
734
		/* reconfigure static routes (kernel may have deleted them) */
735
		system_routing_configure();
736

    
737
		/* set the reload filter dity flag */
738
		touch("{$g['tmp_path']}/filter_dirty");
739

    
740
		/* reload ipsec tunnels */
741
		vpn_ipsec_configure();
742

    
743
		/* restart ez-ipupdate */
744
		services_dyndns_configure();
745

    
746
		/* force DNS update */
747
		services_dnsupdate_process();
748

    
749
		/* restart dnsmasq */
750
		services_dnsmasq_configure();
751
	}
752

    
753
	unmute_kernel_msgs();
754

    
755
	return 0;
756
}
757

    
758
function interfaces_opt_dhcp_configure($interface) {
759
	global $config, $g;
760

    
761
	$optcfg = $config['interfaces'][$interface];
762
	$optif = $optcfg['if'];
763

    
764
	/* generate dhclient_wan.conf */
765
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
766
	if (!$fd) {
767
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
768
		return 1;
769
	}
770

    
771
	if ($optcfg['dhcphostname']) {
772
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
773
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
774
	} else {
775
		$dhclientconf_hostname = "";
776
	}
777

    
778
 	$dhclientconf = "";
779

    
780
	$dhclientconf .= <<<EOD
781
interface "{$optif}" {
782
	send host-name "{$optcfg['dhcphostname']}";
783
	script "/sbin/dhclient-script";
784
	{$dhclientconf_hostname}
785
}
786

    
787
EOD;
788

    
789
	fwrite($fd, $dhclientconf);
790
	fclose($fd);
791

    
792
        /* bring interface up before starting dhclient */
793
        mwexec("/sbin/ifconfig {$optif} up");
794

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

    
798
	return 0;
799
}
800

    
801
function interfaces_dhcp_configure($interface) {
802
	global $config, $g;
803

    
804
	if(filter_translate_type_to_real_interface($interface) <> "")
805
        	$realinterface = filter_translate_type_to_real_interface($interface);
806

    
807
	$optcfg = $config['interfaces'][$interface];
808

    
809
	/* generate dhclient_$interface.conf */
810
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
811
	if (!$fd) {
812
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
813
		return 1;
814
	}
815

    
816
	if ($optcfg['dhcphostname']) {
817
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
818
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
819
	} else {
820
		$dhclientconf_hostname = "";
821
	}
822

    
823
 	$dhclientconf = "";
824

    
825
	$dhclientconf .= <<<EOD
826
interface "{$realinterface}" {
827
	script "/sbin/dhclient-script";
828
	{$dhclientconf_hostname}
829
}
830

    
831
EOD;
832

    
833
	fwrite($fd, $dhclientconf);
834
	fclose($fd);
835
	
836
	$optif = $optcfg['if'];
837
	
838
        /* bring wan interface up before starting dhclient */
839
        mwexec("/sbin/ifconfig {$optif} up");
840

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

    
844
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
845
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
846
	fclose($fout);
847

    
848
	return 0;
849
}
850

    
851
function interfaces_wan_dhcp_configure() {
852
	global $config, $g;
853

    
854
	$wancfg = $config['interfaces']['wan'];
855

    
856
	/* generate dhclient_wan.conf */
857
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
858
	if (!$fd) {
859
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
860
		return 1;
861
	}
862
	
863
	if ($wancfg['dhcphostname']) {
864
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
865
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
866
	} else {
867
		$dhclientconf_hostname = "";
868
	}
869

    
870
 	$dhclientconf = "";
871

    
872
	$dhclientconf .= <<<EOD
873
interface "{$wancfg['if']}" {
874
	script "/sbin/dhclient-script";
875
	{$dhclientconf_hostname}
876
}
877

    
878
EOD;
879

    
880
	fwrite($fd, $dhclientconf);
881
	fclose($fd);
882
	
883
	$wanif = $wancfg['if'];
884
	
885
        /* bring wan interface up before starting dhclient */
886
        mwexec("/sbin/ifconfig {$wanif} up");
887

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

    
891
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
892
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
893
	fclose($fout);
894

    
895
	return 0;
896
}
897

    
898
function interfaces_wan_dhcp_down() {
899
	global $config;
900
	$wancfg = $config['interfaces']['wan'];
901
	$wanif = $wancfg['if'];
902
	mwexec("/sbin/ifconfig {$wanif} delete");
903
	sleep(1);
904
}
905

    
906
function interfaces_dhcp_down($interface) {
907
	global $config;
908
	if(filter_translate_type_to_real_interface($interface) <> "")
909
		$realinterface = filter_translate_type_to_real_interface($interface);
910
	mwexec("/sbin/ifconfig {$realinterface} down");
911
	sleep(1);
912
	$pid = find_dhclient_process($interface);
913
	if($pid)
914
		mwexec("kill {$pid}");
915
}
916

    
917
function interfaces_dhcp_up($interface) {
918
	interfaces_dhcp_configure($interface);
919
	sleep(1);
920
}
921

    
922
function interfaces_wan_dhcp_up() {
923
	interfaces_wan_dhcp_configure();
924
	sleep(1);
925
}
926

    
927
function interfaces_wan_pppoe_configure() {
928
	global $config, $g;
929

    
930
	$wancfg = $config['interfaces']['wan'];
931
	$pppoecfg = $config['pppoe'];
932

    
933
	/* generate mpd.conf */
934
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
935
	if (!$fd) {
936
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
937
		return 1;
938
	}
939

    
940
	$idle = 0;
941

    
942
	if (isset($pppoecfg['ondemand'])) {
943
		$ondemand = "enable";
944
		if ($pppoecfg['timeout'])
945
			$idle = $pppoecfg['timeout'];
946
	} else {
947
		$ondemand = "disable";
948
	}
949

    
950
	$mpdconf = <<<EOD
951
pppoe:
952
	new -i ng0 pppoe pppoe
953
	set iface route default
954
	set iface {$ondemand} on-demand
955
	set iface idle {$idle}
956
	set iface up-script /usr/local/sbin/ppp-linkup
957
EOD;
958
	
959
	if (isset($pppoecfg['ondemand'])) {
960
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
961
			$mpdconf .= <<<EOD
962
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
963

    
964
EOD;
965
		} else {
966
			$mpdconf .= <<<EOD
967
	set iface addrs 192.0.2.112 192.0.2.113
968

    
969
EOD;
970
		}
971
	}
972

    
973
	$mpdconf .= <<<EOD
974
	set bundle disable multilink
975
	set bundle authname "{$pppoecfg['username']}"
976
	set bundle password "{$pppoecfg['password']}"
977
	set link keep-alive 10 60
978
	set link max-redial 0
979
	set link no acfcomp protocomp
980
	set link disable pap chap
981
	set link accept chap
982
	set link mtu 1492
983
	set ipcp yes vjcomp
984
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
985

    
986
EOD;
987

    
988
	if (isset($config['system']['dnsallowoverride'])) {
989
		$mpdconf .= <<<EOD
990
	set ipcp enable req-pri-dns
991

    
992
EOD;
993
	}
994

    
995
	$mpdconf .= <<<EOD
996
	open iface
997

    
998
EOD;
999

    
1000
	fwrite($fd, $mpdconf);
1001
	fclose($fd);
1002

    
1003
	/* generate mpd.links */
1004
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1005
	if (!$fd) {
1006
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
1007
		return 1;
1008
	}
1009

    
1010
	$mpdconf = <<<EOD
1011
pppoe:
1012
	set link type pppoe
1013
	set pppoe iface {$wancfg['if']}
1014
	set pppoe service "{$pppoecfg['provider']}"
1015
	set pppoe enable originate
1016
	set pppoe disable incoming
1017

    
1018
EOD;
1019

    
1020
	fwrite($fd, $mpdconf);
1021
	fclose($fd);
1022

    
1023
	/* if mpd is active, lets take it down */
1024
	if(file_exists("{$g['varrun_path']}/mpd.pid")) {
1025
		killbypid(file_get_contents("{$g['varrun_path']}/mpd.pid"));
1026
		sleep(1);
1027
	}
1028

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

    
1032
        /* sleep until wan is up */
1033
        while(!file_exists("{$g['tmp_path']}/wanup")) {
1034
                sleep(1);
1035
        }
1036
        unlink_if_exists("{$g['tmp_path']}/wanup");
1037

    
1038

    
1039
	return 0;
1040
}
1041

    
1042
function interfaces_wan_pppoe_down() {
1043
	global $g;
1044
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1045
	sleep(1);
1046
}
1047

    
1048
function interfaces_wan_pppoe_up() {
1049
	global $g;
1050
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1051
	sleep(1);
1052
}
1053

    
1054
function interfaces_wan_pptp_configure() {
1055
	global $config, $g;
1056

    
1057
	$wancfg = $config['interfaces']['wan'];
1058
	$pptpcfg = $config['pptp'];
1059

    
1060
	/* generate mpd.conf */
1061
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1062
	if (!$fd) {
1063
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1064
		return 1;
1065
	}
1066

    
1067
	$idle = 0;
1068

    
1069
	if (isset($pptpcfg['ondemand'])) {
1070
		$ondemand = "enable";
1071
		if ($pptpcfg['timeout'])
1072
			$idle = $pptpcfg['timeout'];
1073
	} else {
1074
		$ondemand = "disable";
1075
	}
1076

    
1077
	$mpdconf = <<<EOD
1078
pptp:
1079
	new -i ng0 pptp pptp
1080
	set iface route default
1081
	set iface {$ondemand} on-demand
1082
	set iface idle {$idle}
1083
	set iface up-script /usr/local/sbin/ppp-linkup
1084

    
1085
EOD;
1086

    
1087
	if (isset($pptpcfg['ondemand'])) {
1088
		$mpdconf .= <<<EOD
1089
	set iface addrs 10.0.0.1 10.0.0.2
1090

    
1091
EOD;
1092
	}
1093

    
1094
	$mpdconf .= <<<EOD
1095
	set bundle disable multilink
1096
	set bundle authname "{$pptpcfg['username']}"
1097
	set bundle password "{$pptpcfg['password']}"
1098
	set link keep-alive 10 60
1099
	set link max-redial 0
1100
	set link no acfcomp protocomp
1101
	set link disable pap chap
1102
	set link accept chap
1103
	set ipcp no vjcomp
1104
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1105

    
1106
EOD;
1107

    
1108
	if (isset($config['system']['dnsallowoverride'])) {
1109
		$mpdconf .= <<<EOD
1110
	set ipcp enable req-pri-dns
1111

    
1112
EOD;
1113
	}
1114

    
1115
	$mpdconf .= <<<EOD
1116
	open
1117

    
1118
EOD;
1119

    
1120
	fwrite($fd, $mpdconf);
1121
	fclose($fd);
1122

    
1123
	/* generate mpd.links */
1124
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1125
	if (!$fd) {
1126
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1127
		return 1;
1128
	}
1129

    
1130
	$mpdconf = <<<EOD
1131
pptp:
1132
	set link type pptp
1133
	set pptp enable originate outcall
1134
	set pptp disable windowing
1135
	set pptp self {$pptpcfg['local']}
1136
	set pptp peer {$pptpcfg['remote']}
1137

    
1138
EOD;
1139

    
1140
	fwrite($fd, $mpdconf);
1141
	fclose($fd);
1142

    
1143
	/* configure interface */
1144
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1145
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1146

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

    
1150
	return 0;
1151
}
1152

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

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

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

    
1168
	$bpcfg = $config['bigpond'];
1169

    
1170
	if (!$curwanip) {
1171
		/* IP address not configured yet, exit */
1172
		return 0;
1173
	}
1174

    
1175
	/* kill bpalogin */
1176
	killbyname("bpalogin");
1177

    
1178
	/* wait a moment */
1179
	sleep(1);
1180

    
1181
	/* get the default domain */
1182
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1183
	if ($nfd) {
1184
		$defaultdomain = trim(fgets($nfd));
1185
		fclose($nfd);
1186
	}
1187

    
1188
	/* generate bpalogin.conf */
1189
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1190
	if (!$fd) {
1191
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1192
		return 1;
1193
	}
1194

    
1195
	if (!$bpcfg['authserver'])
1196
		$bpcfg['authserver'] = "dce-server";
1197
	if (!$bpcfg['authdomain'])
1198
		$bpcfg['authdomain'] = $defaultdomain;
1199

    
1200
	$bpconf = <<<EOD
1201
username {$bpcfg['username']}
1202
password {$bpcfg['password']}
1203
authserver {$bpcfg['authserver']}
1204
authdomain {$bpcfg['authdomain']}
1205
localport 5050
1206

    
1207
EOD;
1208

    
1209
	if ($bpcfg['minheartbeatinterval'])
1210
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1211

    
1212
	fwrite($fd, $bpconf);
1213
	fclose($fd);
1214

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

    
1218
	return 0;
1219
}
1220

    
1221
function get_real_wan_interface() {
1222
	global $config, $g;
1223

    
1224
	$wancfg = $config['interfaces']['wan'];
1225

    
1226
	$wanif = $wancfg['if'];
1227
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1228
		$wanif = $g['pppoe_interface'];
1229
	}
1230

    
1231
	return $wanif;
1232
}
1233

    
1234
function get_current_wan_address($interface = "wan") {
1235
	global $config, $g;
1236

    
1237
	$wancfg = $config['interfaces'][$interface];
1238

    
1239
	$interface = filter_translate_type_to_real_interface($interface);
1240

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

    
1245
		if (isset($ifinfo[1])) {
1246
			$aif = preg_split("/\s+/", $ifinfo[1]);
1247
			$curwanip = chop($aif[3]);
1248

    
1249
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1250
				return $curwanip;
1251
		}
1252

    
1253
		return null;		
1254
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1255
		/* dynamic WAN IP address, find out which one */
1256
		$wanif = get_real_wan_interface();
1257

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

    
1261
		if (isset($ifinfo[1])) {
1262
			$aif = preg_split("/\s+/", $ifinfo[1]);
1263
			$curwanip = chop($aif[3]);
1264

    
1265
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1266
				return $curwanip;
1267
		}
1268

    
1269
		return null;
1270
	} else {
1271
		/* static WAN IP address */
1272
		return $wancfg['ipaddr'];
1273
	}
1274
}
1275

    
1276
/****f* interfaces/is_altq_capable
1277
 * NAME
1278
 *   is_altq_capable - Test if interface is capable of using ALTQ
1279
 * INPUTS
1280
 *   $int            - string containing interface name
1281
 * RESULT
1282
 *   boolean         - true or false
1283
 ******/
1284

    
1285
function is_altq_capable($int) {
1286
        /* Per:
1287
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1288
         * Only the following drivers have ALTQ support
1289
         */
1290
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1291
		"em", "fxp", "hme", "lnc", "ndis", "rl", "sf", "sis", "sk",
1292
		"tun", "vr", "wi", "xl");
1293

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

    
1296
        if (in_array($int_family[0], $capable))
1297
                return true;
1298
        else
1299
                return false;
1300
}
1301

    
1302
function get_number_of_bridged_interfaces() {
1303
	$bridges_total = 0;
1304
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1305
	foreach($bridges as $bridge) {
1306
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1307
		if($match_array[1][0] <> "") {
1308
			if($match_array[1][0] > $bridges_total)
1309
				$bridges_total = $match_array[1][0];
1310
		}
1311
	}
1312
	return "{$bridges_total}";
1313
}
1314

    
1315
function get_next_available_bridge_interface() {
1316
	$bridges_total = get_number_of_bridged_interfaces();
1317
	$interfaces = `/sbin/ifconfig -l`;
1318
	$x=0;
1319
	for($x=0; $x<$bridges_total; $x++) {
1320
		if(!stristr($interfaces, "bridge{$x}")) {
1321
			return "{$x}";
1322
		}
1323
	}
1324
	return "{$x}";
1325
}
1326

    
1327
function destroy_bridge($bridge_num) {
1328
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1329
	sleep(1);
1330
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1331
	sleep(1);
1332
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1333
	sleep(1);
1334
	return;
1335
}
1336

    
1337
function discover_bridge($interface1, $interface2) {
1338
	if(!$interface1) return;
1339
	if(!$interface2) return;
1340
	$total_bridges = get_number_of_bridged_interfaces();
1341
	$interfaces = `/sbin/ifconfig -l`;
1342
	$x=0;
1343
	for($x=0; $x<$total_bridges; $x++) {
1344
		$bridge_text = "NA";
1345
		if(!stristr($interfaces, "bridge{$x}")) 
1346
			continue;
1347
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1348
		if(stristr($bridge_text, $interface1) == true and
1349
		   stristr($bridge_text, $interface2) == true) {
1350
			return "{$x}";			
1351
		}
1352
	}
1353
	return "-1";
1354
}
1355

    
1356
?>
(8-8/24)