Project

General

Profile

Download (38.8 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
		
127
		/* force all bridged interfaces to use same mtu */
128
		$mtu = get_interface_mtu($config['interfaces'][$lancfg['bridge']]['if']);
129
		mwexec("/sbin/ifconfig {$lancfg['if']} mtu {$mtu}");
130
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}");
131

    
132
		/* assign items to a bridge */
133
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up");
134
		mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}");
135

    
136
		/* log commands run for debugging in /tmp/ */
137
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
138
		fwrite($fd, "/sbin/ifconfig {$lancfg['if']} mtu {$mtu}\n");
139
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}\n");
140
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
141
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']} up\n");
142
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}\n");
143
		fclose($fd);
144
	}
145
	
146
	/* media */
147
	if ($lancfg['media'] || $lancfg['mediaopt']) {
148
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
149
		if ($lancfg['media'])
150
			$cmd .= " media " . escapeshellarg($lancfg['media']);
151
		if ($lancfg['mediaopt'])
152
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
153
		mwexec($cmd);
154
	}
155

    
156
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
157
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
158

    
159
	if (!$g['booting']) {
160
		/* make new hosts file */
161
		system_hosts_generate();
162

    
163
		/* reconfigure static routes (kernel may have deleted them) */
164
		system_routing_configure();
165

    
166
		/* set the reload filter dity flag */
167
		touch("{$g['tmp_path']}/filter_dirty");
168

    
169
		/* reload IPsec tunnels */
170
		vpn_ipsec_configure();
171

    
172
		/* reload dhcpd (gateway may have changed) */
173
		services_dhcpd_configure();
174

    
175
		/* reload dnsmasq */
176
		services_dnsmasq_configure();
177

    
178
		/* reload webgui */
179
		system_webgui_start();
180

    
181
		/* reload captive portal */
182
		captiveportal_configure();
183
	}
184

    
185
	return 0;
186
}
187

    
188
function interfaces_optional_configure() {
189
	global $config, $g;
190
	global $bridgeconfig;
191

    
192
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
193
		interfaces_optional_configure_if($i);
194
	}
195

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

    
200
		/* reload IPsec tunnels */
201
		vpn_ipsec_configure();
202

    
203
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
204
		services_dhcpd_configure();
205

    
206
		/* restart dnsmasq */
207
		services_dnsmasq_configure();
208

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

    
213
	return 0;
214
}
215

    
216
function interfaces_optional_configure_if($opti) {
217
	global $config, $g;
218
	global $bridgeconfig, $debugging;
219

    
220
	$bridges_total = get_next_available_bridge_interface();
221

    
222
	$optcfg = $config['interfaces']['opt' . $opti];
223

    
224
	if ($g['booting']) {
225
		$optdescr = "";
226
		if ($optcfg['descr'])
227
			$optdescr = " ({$optcfg['descr']})";
228
		print "\tOPT{$opti}{$optdescr}... ";
229
	}
230

    
231
	if (isset($optcfg['enable'])) {
232
		/* wireless configuration? */
233
		if (is_array($optcfg['wireless']))
234
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
235

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

    
256
		/* media */
257
		if ($optcfg['media'] || $optcfg['mediaopt']) {
258
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
259
			if ($optcfg['media'])
260
				$cmd .= " media " . escapeshellarg($optcfg['media']);
261
			if ($optcfg['mediaopt'])
262
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
263
			mwexec($cmd);
264
		}
265

    
266
		/* OpenVPN configuration? */
267
 		if (isset($optcfg['ovpn'])) {
268
 			if (strstr($optcfg['if'], "tap"))
269
 				ovpn_link_tap();
270
 		}
271

    
272
		/* bridged? */
273
		if ($optcfg['bridge']) {
274
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
275
                        /* use open/netBSD style bridge */
276
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
277
			
278
			/* force all bridged interfaces to use same mtu */
279
			$mtu = get_interface_mtu($config['interfaces'][$optcfg['bridge']]['if']);
280
			mwexec("/sbin/ifconfig {$optcfg['if']} mtu {$mtu}");
281
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}");			
282

    
283
			/* assign items to a bridge */
284
                        mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up");
285
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} stp {$config['interfaces'][$optcfg['bridge']]['if']}");
286

    
287
			/* log commands run for debugging in /tmp/ */
288
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
289
			fwrite($fd, "/sbin/ifconfig {$optcfg['if']} mtu {$mtu}\n");
290
			fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}\n");
291
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
292
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up\n");
293
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} stp {$config['interfaces'][$optcfg['bridge']]['if']}\n");
294
			fclose($fd);
295
		} else {
296
			/* if user has selected DHCP type then act accordingly */
297
			if($optcfg['ipaddr'] == "dhcp") {
298
				interfaces_opt_dhcp_configure("opt{$opti}");
299
			} else {			
300
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
301
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
302
			}
303
		}
304
	} else {
305
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
306
	}
307
	return 0;
308
}
309

    
310
function interfaces_carp_configure() {
311
	global $g, $config, $debugging;
312
	$carp_instances_counter = 0;
313
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
314
	if(isset($config['system']['developerspew'])) {
315
		$mt = microtime();
316
		echo "interfaces_carp_configure() being called $mt\n";
317
	}
318
	if ($g['booting'] and !$debugging) {
319
		echo "Configuring CARP interfaces...";
320
		mute_kernel_msgs();
321
	}
322
	/* if neither items are arrays then redirect pfsync to loopback */
323
	if (!is_array($config['virtualip']['vip']) or
324
	    !is_array($config['installedpackages']['carpsettings']['config'])) {
325
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
326
		return;
327
	}
328
	foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
329
		if($carp['pfsyncenabled'] != "") {
330
			mwexec("/sbin/sysctl net.inet.carp.allow=1");
331
			if($debugging) 
332
				echo "Enabling preempt\n";
333
			if($carp['premption'] != "")
334
				mwexec("/sbin/sysctl net.inet.carp.preempt=1");
335
			if($carp['balancing'] != "")
336
				mwexec("/sbin/sysctl net.inet.carp.arpbalance=1");
337
			if($debugging) 
338
				echo "Get friendly interface name {$carp['pfsyncinterface']}.\n";
339
			$carp_sync_int = convert_friendly_interface_to_real_interface_name($carp['pfsyncinterface']);
340
			if($debugging) 
341
				echo "Friendly name {$carp_sync_int}.\n";
342
			$carp_sync_int = convert_friendly_interface_to_real_interface_name($carp['pfsyncinterface']);
343
			if($g['booting']) {
344
				/*    install rules to alllow pfsync to sync up during boot
345
				 *    carp interfaces will remain down until the bootup sequence finishes
346
				 */
347
				if($debugging) 
348
					echo "Adding firewall rules..\n";
349
				exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
350
				exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
351
				exec("echo pass out proto { tcp, udp } from any to any port 53 keep state >> /tmp/rules.boot");
352
				exec("/sbin/pfctl -f /tmp/rules.boot");
353
				if($debugging) {
354
					echo "Showing loaded rule set:\n";
355
					system("/sbin/pfctl -vvsr");
356
				}
357
			}			
358
			/* do not setup pfsync twice */
359
			if($total_carp_interfaces_defined == 0) {
360
				//if($debugging)
361
				//	echo "Bringing up pfsync0.\n";
362
				//mwexec("/sbin/ifconfig pfsync0 create");
363
				if($debugging)
364
					echo "Assigning syncdev to {$carp_sync_int}.\n";						
365
				mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up");
366
			}
367
		}
368
	}
369
	$viparr = &$config['virtualip']['vip'];
370
	foreach ($viparr as $vip) {
371
		if ($vip['mode'] == "carp") {
372
			/*
373
			 *   create the carp interface
374
			 */
375
			if($debugging)
376
				echo "Creating carp{$carp_instances_counter}.\n";
377
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
378
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
379
			if($vip['password'] != "") 
380
				$password = " pass " . $vip['password'];
381
			/* XXX: billm - carpdev not in our build?
382
			    $carpdev = "";
383
			    if(isset($vip['interface']) && ($vip['interface'] != "AUTO" && $vip['interface'] != "")) {
384
			   	$ci = filter_opt_interface_to_real($vip['interface']);
385
			   	$carpdev = " carpdev {$ci} ";
386
			    }
387
			*/
388
			if($debugging)
389
				echo "Configuring carp{$carp_instances_counter}.\n";
390
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password);
391
			if($g['booting']) 
392
				mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
393
			$carp_instances_counter++;
394
		}
395
	}
396
	if ($g['booting']) {
397
		unmute_kernel_msgs();
398
		echo "done.\n";
399
	}
400
}
401

    
402
function interfaces_carp_bring_up_final() {
403
	global $config, $g, $debugging;
404
	if(isset($config['system']['developerspew'])) {
405
		$mt = microtime();
406
		echo "interfaces_carp_bring_up_final() being called $mt\n";
407
	}
408
	$viparr = &$config['virtualip']['vip'];
409
	/* could not locate an array, return */
410
	if(!is_array($viparr)) 
411
		return;
412
	$carp_instances_counter = 0;
413
	foreach ($viparr as $vip) {
414
		if($debugging)
415
			echo "Upping interface carp{$carp_instances_counter}.\n";
416
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
417
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
418
		if($vip['password'] != "") 
419
			$password = " pass " . $vip['password'];
420
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
421
		if($debugging)
422
			echo "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password . "\n";
423
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
424
		$carp_instances_counter++;
425
	}
426
}
427

    
428
function interfaces_wireless_configure($if, $wlcfg) {
429
	global $config, $g;
430
	
431
	/* set values for /path/program */
432
	$hostapd = "/usr/sbin/hostapd";
433
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
434
	$ifconfig = "/sbin/ifconfig ";
435
	$killall = "/usr/bin/killall ";
436

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

    
439
	/* Set a/b/g standard */
440
	$standard = ("mode " . escapeshellarg($wlcfg['standard']));
441

    
442
	/* set wireless channel value */
443
	$channel = escapeshellarg($wlcfg['channel']);
444
	
445
	if($channel == "") {
446
		$channel = "";
447
	} else { 
448
		$channel = ("channel " . escapeshellarg($wlcfg['channel']));
449
	}
450

    
451
	/* Set ssid */
452
	$ssid = ("ssid " . escapeshellarg($wlcfg['ssid']));
453

    
454
	/* Set stationname */
455
	if (!$wlcfg['stationname'])
456
		$stationname = "pfsense";
457
	else
458
		$stationname = ("stationname " . escapeshellarg($wlcfg['stationname']));
459

    
460
	/* Set wireless hostap mode */
461
	if ($wlcfg['mode'] == hostap)
462
		$hostapmode = "mediaopt hostap";
463
	else
464
		$hostapmode = "-mediaopt hostap";
465

    
466
	/* Set wireless adhoc mode */
467
	if ($wlcfg['mode'] == adhoc)
468
		$adhocmode = "mediaopt adhoc";
469
	else
470
		$adhocmode = "-mediaopt adhoc";
471

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

    
474
	/* handle hide ssid option */
475
	if(isset($wlcfg['hidessid']['enable']))
476
		$hidessid = "hidessid";
477
	else
478
		$hidessid = "-hidessid";
479

    
480
	/* handle pureg (802.11g) only option */
481
	if(isset($wlcfg['pureg']['enable']))
482
		$pureg = "mode 11g pureg";
483
	else
484
		$pureg = "-pureg";
485

    
486
	/* enable apbridge option */
487
	if(isset($wlcfg['apbridge']['enable']))
488
		$apbridge = "apbridge";
489
	else
490
		$apbridge = "-apbridge";
491

    
492
	/* handle turbo option */
493
	if(isset($wlcfg['turbo']['enable']))
494
		$turbo = "mediaopt turbo";
495
	else
496
		$turbo = "-mediaopt turbo";
497

    
498
	/* handle txpower setting */
499
	if($wlcfg['txpower'] <> "")
500
		$txpower = ("txpower " . escapeshellarg($wlcfg['txpower']));
501
	
502
	/* handle wme option */
503
	if(isset($wlcfg['wme']['enable']))
504
		$wme = "wme";
505
	else
506
		$wme = "-wme";
507
	
508
	/* set up wep if enabled */
509
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
510
                $wepset .= "authmode shared wepmode on ";
511

    
512
                $i = 1;
513
                foreach ($wlcfg['wep']['key'] as $wepkey) {
514
                        $wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
515
                        if (isset($wepkey['txkey'])) {
516
                                $wepset .= "weptxkey {$i} ";
517
                        }
518
                        $i++;
519
                }
520
        } else {
521
                $wepset = "authmode open wepmode off";
522
	}
523

    
524
	/* generate wpa_supplicant/hostap config if wpa is enabled */
525

    
526
	switch ($wlcfg['mode']) {
527
		case 'BSS':
528
			if (isset($wlcfg['wpa']['enable'])) {
529

    
530
				$wpa .= <<<EOD
531
ctrl_interface={$g['varrun_path']}/hostapd
532
ctrl_interface_group=0
533
ap_scan=1
534
#fast_reauth=1
535
network={
536
ssid={$wlcfg['ssid']}
537
scan_ssid=2
538
priority=5
539
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
540
psk={$wlcfg['wpa']['passphrase']}
541
pairwise={$wlcfg['wpa']['wpa_pairwise']}
542
group={$wlcfg['wpa']['wpa_pairwise']}
543
}
544
EOD;
545

    
546
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
547
				fwrite($fd, "{$wpa}");
548
				fclose($fd);
549

    
550
				if(is_process_running("wpa_supplicant"))
551
					mwexec("$killall wpa_supplicant");
552
			}
553
		break;
554

    
555
		case 'hostap':
556
			if (isset($wlcfg['wpa']['enable'])) {
557
				$wpa .= <<<EOD
558
interface={$if}
559
driver=bsd
560
logger_syslog=-1
561
logger_syslog_level=0
562
logger_stdout=-1
563
logger_stdout_level=0
564
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
565
ctrl_interface={$g['varrun_path']}/hostapd
566
ctrl_interface_group=wheel
567
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
568
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
569
ssid={$wlcfg['ssid']}
570
debug={$wlcfg['wpa']['debug_mode']}
571
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
572
auth_algs={$wlcfg['wpa']['auth_algs']}
573
wpa={$wlcfg['wpa']['wpa_mode']}
574
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
575
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
576
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
577
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
578
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
579
wpa_passphrase={$wlcfg['wpa']['passphrase']}
580
ieee8021x={$wlcfg['wpa']['ieee8021x']}
581
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
582
#rsn_preauth=1
583
#rsn_preauth_interfaces=eth0
584
EOD;
585

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

    
590
				if(is_process_running("hostapd"))
591
					mwexec("$killall  hostapd");
592
			}
593
		break;
594

    
595
		case 'adhoc':
596
			if(is_process_running("hostapd"))
597
				mwexec("$killall hostapd");
598

    
599
			if(is_process_running("wpa_supplicant"))
600
				mwexec("$killall wpa_supplicant");
601
		break;
602
	}	
603

    
604
	/* start up everything */
605
	        
606
	mwexec("$ifconfig {$if}" . " down"); 
607
	mwexec("$ifconfig $if" . " " . $standard);
608
	mwexec("$ifconfig $if" . " " . $channel);
609
	mwexec("$ifconfig $if" . " " . $ssid);
610
	mwexec("$ifconfig $if" . " " . $stationname);
611
	mwexec("$ifconfig $if" . " " . $hostapmode);
612
	mwexec("$ifconfig $if" . " " . $adhocmode);
613
	mwexec("$ifconfig $if" . " " . $hidessid);
614
	mwexec("$ifconfig $if" . " " . $pureg);
615
	mwexec("$ifconfig $if" . " " . $apbridge);
616
	mwexec("$ifconfig $if" . " " . $turbo);
617
	mwexec("$ifconfig $if" . " " . $wme);
618
	mwexec("$ifconfig $if" . " " . $wepset);
619
	mwexec("$ifconfig $if" . " up"); 
620

    
621
	if (isset($wlcfg['wpa']['enable'])) {
622
		if ($wlcfg['mode'] == BSS) 
623
			mwexec("$wpa_supplicant -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf");
624
		if ($wlcfg['mode'] == hostap) 
625
			mwexec("$hostapd -B {$g['varetc_path']}/hostapd_{$if}.conf");
626
	}
627

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

    
634
	/* Write wep crap out */
635
//	$fd = fopen("{$g['tmp_path']}/ifconfig_wep", "w");
636
//	fwrite($fd, "sbin/ifconfig {$wepset}");
637
//	fclose($fd);
638
	
639
	if(isset($wlcfg['useolsr']))
640
		setup_wireless_olsr(escapeshellarg($if));
641

    
642
	return 0;
643

    
644
}
645

    
646
function find_dhclient_process($interface) {
647
	if(filter_translate_type_to_real_interface($interface) <> "")
648
        	$realinterface = filter_translate_type_to_real_interface($interface);
649
	$pid = `ps ax | grep "[d]hclient" | grep {$realinterface} | awk -F" " '{print $1}'`;
650
	return $pid;
651
}
652

    
653
function interfaces_wan_configure() {
654
	global $config, $g;
655

    
656
	$wancfg = $config['interfaces']['wan'];
657

    
658
	if(!$g['booting']) {
659
		mute_kernel_msgs();
660

    
661
		/* find dhclient process for wan and kill it */
662
		killbypid(find_dhclient_process("wan"));
663

    
664
		/* kill PPPoE client (mpd) */
665
		killbypid("{$g['varrun_path']}/mpd.pid");
666

    
667
		/* wait for processes to die */
668
		sleep(1);
669

    
670
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
671
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
672
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
673
		unlink_if_exists("{$g['vardb_path']}/wanip");
674
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
675
	}
676

    
677
	/* remove all addresses first */
678
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
679
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
680

    
681
	/* wireless configuration? */
682
	if (is_array($wancfg['wireless']))
683
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
684

    
685
	if ($wancfg['spoofmac']) {
686
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
687
			" link " . escapeshellarg($wancfg['spoofmac']));
688
	}  else {
689
		$mac = get_interface_mac_address($wancfg['if']);
690
		if($mac == "ff:ff:ff:ff:ff:ff") {
691
			/*   this is not a valid mac address.  generate a
692
			 *   temporary mac address so the machine can get online.
693
			 */
694
			echo "Generating new MAC address.";
695
			$random_mac = generate_random_mac_address();
696
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
697
				" link " . escapeshellarg($random_mac));
698
			$wancfg['spoofmac'] = $random_mac;
699
			write_config();
700
			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");
701
		}
702
	}
703

    
704
	/* media */
705
	if ($wancfg['media'] || $wancfg['mediaopt']) {
706
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
707
		if ($wancfg['media'])
708
			$cmd .= " media " . escapeshellarg($wancfg['media']);
709
		if ($wancfg['mediaopt'])
710
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
711
		mwexec($cmd);
712
	}
713

    
714
	switch ($wancfg['ipaddr']) {
715

    
716
		case 'dhcp':
717
			interfaces_wan_dhcp_configure();
718
			break;
719

    
720
		case 'pppoe':
721
			interfaces_wan_pppoe_configure();
722
			break;
723

    
724
		case 'pptp':
725
			interfaces_wan_pptp_configure();
726
			break;
727

    
728
		case 'bigpond':
729
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
730
			interfaces_wan_dhcp_configure();
731
			break;
732

    
733
		default:
734
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
735
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
736
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
737
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
738
			} else {
739
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
740
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
741
			}
742
			/* install default route */
743
			mwexec("/sbin/route delete default");
744
			mwexec("/sbin/route add default " . escapeshellarg($config['interfaces']['wan']['gateway']));
745

    
746
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
747
			filter_configure();
748
	}
749

    
750
	if (!$g['booting']) {
751
		/* reconfigure static routes (kernel may have deleted them) */
752
		system_routing_configure();
753

    
754
		/* set the reload filter dity flag */
755
		touch("{$g['tmp_path']}/filter_dirty");
756

    
757
		/* reload ipsec tunnels */
758
		vpn_ipsec_configure();
759

    
760
		/* restart ez-ipupdate */
761
		services_dyndns_configure();
762

    
763
		/* force DNS update */
764
		services_dnsupdate_process();
765

    
766
		/* restart dnsmasq */
767
		services_dnsmasq_configure();
768
	}
769

    
770
	unmute_kernel_msgs();
771

    
772
	return 0;
773
}
774

    
775
function interfaces_opt_dhcp_configure($interface) {
776
	global $config, $g;
777

    
778
	$optcfg = $config['interfaces'][$interface];
779
	$optif = $optcfg['if'];
780

    
781
	/* generate dhclient_wan.conf */
782
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
783
	if (!$fd) {
784
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
785
		return 1;
786
	}
787

    
788
	if ($optcfg['dhcphostname']) {
789
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
790
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
791
	} else {
792
		$dhclientconf_hostname = "";
793
	}
794

    
795
 	$dhclientconf = "";
796

    
797
	$dhclientconf .= <<<EOD
798
interface "{$optif}" {
799
	send host-name "{$optcfg['dhcphostname']}";
800
	script "/sbin/dhclient-script";
801
	{$dhclientconf_hostname}
802
}
803

    
804
EOD;
805

    
806
	fwrite($fd, $dhclientconf);
807
	fclose($fd);
808

    
809
        /* bring interface up before starting dhclient */
810
        mwexec("/sbin/ifconfig {$optif} up");
811

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

    
815
	return 0;
816
}
817

    
818
function interfaces_dhcp_configure($interface) {
819
	global $config, $g;
820

    
821
	if(filter_translate_type_to_real_interface($interface) <> "")
822
        	$realinterface = filter_translate_type_to_real_interface($interface);
823

    
824
	$optcfg = $config['interfaces'][$interface];
825

    
826
	/* generate dhclient_$interface.conf */
827
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
828
	if (!$fd) {
829
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
830
		return 1;
831
	}
832

    
833
	if ($optcfg['dhcphostname']) {
834
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
835
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
836
	} else {
837
		$dhclientconf_hostname = "";
838
	}
839

    
840
 	$dhclientconf = "";
841

    
842
	$dhclientconf .= <<<EOD
843
interface "{$realinterface}" {
844
	script "/sbin/dhclient-script";
845
	{$dhclientconf_hostname}
846
}
847

    
848
EOD;
849

    
850
	fwrite($fd, $dhclientconf);
851
	fclose($fd);
852
	
853
	$optif = $optcfg['if'];
854
	
855
        /* bring wan interface up before starting dhclient */
856
        mwexec("/sbin/ifconfig {$optif} up");
857

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

    
861
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
862
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
863
	fclose($fout);
864

    
865
	return 0;
866
}
867

    
868
function interfaces_wan_dhcp_configure() {
869
	global $config, $g;
870

    
871
	$wancfg = $config['interfaces']['wan'];
872

    
873
	/* generate dhclient_wan.conf */
874
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
875
	if (!$fd) {
876
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
877
		return 1;
878
	}
879
	
880
	if ($wancfg['dhcphostname']) {
881
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
882
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
883
	} else {
884
		$dhclientconf_hostname = "";
885
	}
886

    
887
 	$dhclientconf = "";
888

    
889
	$dhclientconf .= <<<EOD
890
interface "{$wancfg['if']}" {
891
	script "/sbin/dhclient-script";
892
	{$dhclientconf_hostname}
893
}
894

    
895
EOD;
896

    
897
	fwrite($fd, $dhclientconf);
898
	fclose($fd);
899
	
900
	$wanif = $wancfg['if'];
901
	
902
        /* bring wan interface up before starting dhclient */
903
        mwexec("/sbin/ifconfig {$wanif} up");
904

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

    
908
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
909
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
910
	fclose($fout);
911

    
912
	return 0;
913
}
914

    
915
function interfaces_wan_dhcp_down() {
916
	global $config;
917
	$wancfg = $config['interfaces']['wan'];
918
	$wanif = $wancfg['if'];
919
	mwexec("/sbin/ifconfig {$wanif} delete");
920
	sleep(1);
921
}
922

    
923
function interfaces_dhcp_down($interface) {
924
	global $config;
925
	if(filter_translate_type_to_real_interface($interface) <> "")
926
		$realinterface = filter_translate_type_to_real_interface($interface);
927
	mwexec("/sbin/ifconfig {$realinterface} down");
928
	sleep(1);
929
	$pid = find_dhclient_process($interface);
930
	if($pid)
931
		mwexec("kill {$pid}");
932
}
933

    
934
function interfaces_dhcp_up($interface) {
935
	interfaces_dhcp_configure($interface);
936
	sleep(1);
937
}
938

    
939
function interfaces_wan_dhcp_up() {
940
	interfaces_wan_dhcp_configure();
941
	sleep(1);
942
}
943

    
944
function interfaces_wan_pppoe_configure() {
945
	global $config, $g;
946

    
947
	$wancfg = $config['interfaces']['wan'];
948
	$pppoecfg = $config['pppoe'];
949

    
950
	/* generate mpd.conf */
951
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
952
	if (!$fd) {
953
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
954
		return 1;
955
	}
956

    
957
	$idle = 0;
958

    
959
	if (isset($pppoecfg['ondemand'])) {
960
		$ondemand = "enable";
961
		if ($pppoecfg['timeout'])
962
			$idle = $pppoecfg['timeout'];
963
	} else {
964
		$ondemand = "disable";
965
	}
966

    
967
	$mpdconf = <<<EOD
968
pppoe:
969
	new -i ng0 pppoe pppoe
970
	set iface route default
971
	set iface {$ondemand} on-demand
972
	set iface idle {$idle}
973
	set iface up-script /usr/local/sbin/ppp-linkup
974
EOD;
975
	
976
	if (isset($pppoecfg['ondemand'])) {
977
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
978
			$mpdconf .= <<<EOD
979
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
980

    
981
EOD;
982
		} else {
983
			$mpdconf .= <<<EOD
984
	set iface addrs 192.0.2.112 192.0.2.113
985

    
986
EOD;
987
		}
988
	}
989

    
990
	$mpdconf .= <<<EOD
991
	set bundle disable multilink
992
	set bundle authname "{$pppoecfg['username']}"
993
	set bundle password "{$pppoecfg['password']}"
994
	set link keep-alive 10 60
995
	set link max-redial 0
996
	set link no acfcomp protocomp
997
	set link disable pap chap
998
	set link accept chap
999
	set link mtu 1492
1000
	set ipcp yes vjcomp
1001
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1002

    
1003
EOD;
1004

    
1005
	if (isset($config['system']['dnsallowoverride'])) {
1006
		$mpdconf .= <<<EOD
1007
	set ipcp enable req-pri-dns
1008

    
1009
EOD;
1010
	}
1011

    
1012
	$mpdconf .= <<<EOD
1013
	open iface
1014

    
1015
EOD;
1016

    
1017
	fwrite($fd, $mpdconf);
1018
	fclose($fd);
1019

    
1020
	/* generate mpd.links */
1021
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1022
	if (!$fd) {
1023
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
1024
		return 1;
1025
	}
1026

    
1027
	$mpdconf = <<<EOD
1028
pppoe:
1029
	set link type pppoe
1030
	set pppoe iface {$wancfg['if']}
1031
	set pppoe service "{$pppoecfg['provider']}"
1032
	set pppoe enable originate
1033
	set pppoe disable incoming
1034

    
1035
EOD;
1036

    
1037
	fwrite($fd, $mpdconf);
1038
	fclose($fd);
1039

    
1040
	/* if mpd is active, lets take it down */
1041
	if(file_exists("{$g['varrun_path']}/mpd.pid")) {
1042
		killbypid(file_get_contents("{$g['varrun_path']}/mpd.pid"));
1043
		sleep(1);
1044
	}
1045

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

    
1049
        /* sleep until wan is up */
1050
        while(!file_exists("{$g['tmp_path']}/wanup")) {
1051
                sleep(1);
1052
        }
1053
        unlink_if_exists("{$g['tmp_path']}/wanup");
1054

    
1055

    
1056
	return 0;
1057
}
1058

    
1059
function interfaces_wan_pppoe_down() {
1060
	global $g;
1061
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1062
	sleep(1);
1063
}
1064

    
1065
function interfaces_wan_pppoe_up() {
1066
	global $g;
1067
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1068
	sleep(1);
1069
}
1070

    
1071
function interfaces_wan_pptp_configure() {
1072
	global $config, $g;
1073

    
1074
	$wancfg = $config['interfaces']['wan'];
1075
	$pptpcfg = $config['pptp'];
1076

    
1077
	/* generate mpd.conf */
1078
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1079
	if (!$fd) {
1080
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1081
		return 1;
1082
	}
1083

    
1084
	$idle = 0;
1085

    
1086
	if (isset($pptpcfg['ondemand'])) {
1087
		$ondemand = "enable";
1088
		if ($pptpcfg['timeout'])
1089
			$idle = $pptpcfg['timeout'];
1090
	} else {
1091
		$ondemand = "disable";
1092
	}
1093

    
1094
	$mpdconf = <<<EOD
1095
pptp:
1096
	new -i ng0 pptp pptp
1097
	set iface route default
1098
	set iface {$ondemand} on-demand
1099
	set iface idle {$idle}
1100
	set iface up-script /usr/local/sbin/ppp-linkup
1101

    
1102
EOD;
1103

    
1104
	if (isset($pptpcfg['ondemand'])) {
1105
		$mpdconf .= <<<EOD
1106
	set iface addrs 10.0.0.1 10.0.0.2
1107

    
1108
EOD;
1109
	}
1110

    
1111
	$mpdconf .= <<<EOD
1112
	set bundle disable multilink
1113
	set bundle authname "{$pptpcfg['username']}"
1114
	set bundle password "{$pptpcfg['password']}"
1115
	set link keep-alive 10 60
1116
	set link max-redial 0
1117
	set link no acfcomp protocomp
1118
	set link disable pap chap
1119
	set link accept chap
1120
	set ipcp no vjcomp
1121
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1122

    
1123
EOD;
1124

    
1125
	if (isset($config['system']['dnsallowoverride'])) {
1126
		$mpdconf .= <<<EOD
1127
	set ipcp enable req-pri-dns
1128

    
1129
EOD;
1130
	}
1131

    
1132
	$mpdconf .= <<<EOD
1133
	open
1134

    
1135
EOD;
1136

    
1137
	fwrite($fd, $mpdconf);
1138
	fclose($fd);
1139

    
1140
	/* generate mpd.links */
1141
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1142
	if (!$fd) {
1143
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1144
		return 1;
1145
	}
1146

    
1147
	$mpdconf = <<<EOD
1148
pptp:
1149
	set link type pptp
1150
	set pptp enable originate outcall
1151
	set pptp disable windowing
1152
	set pptp self {$pptpcfg['local']}
1153
	set pptp peer {$pptpcfg['remote']}
1154

    
1155
EOD;
1156

    
1157
	fwrite($fd, $mpdconf);
1158
	fclose($fd);
1159

    
1160
	/* configure interface */
1161
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1162
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1163

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

    
1167
	return 0;
1168
}
1169

    
1170
function interfaces_wan_pptp_down() {
1171
	global $g;
1172
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1173
	sleep(1);
1174
}
1175

    
1176
function interfaces_wan_pptp_up() {
1177
	global $g;
1178
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1179
	sleep(1);
1180
}
1181

    
1182
function interfaces_wan_bigpond_configure($curwanip) {
1183
	global $config, $g;
1184

    
1185
	$bpcfg = $config['bigpond'];
1186

    
1187
	if (!$curwanip) {
1188
		/* IP address not configured yet, exit */
1189
		return 0;
1190
	}
1191

    
1192
	/* kill bpalogin */
1193
	killbyname("bpalogin");
1194

    
1195
	/* wait a moment */
1196
	sleep(1);
1197

    
1198
	/* get the default domain */
1199
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1200
	if ($nfd) {
1201
		$defaultdomain = trim(fgets($nfd));
1202
		fclose($nfd);
1203
	}
1204

    
1205
	/* generate bpalogin.conf */
1206
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1207
	if (!$fd) {
1208
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1209
		return 1;
1210
	}
1211

    
1212
	if (!$bpcfg['authserver'])
1213
		$bpcfg['authserver'] = "dce-server";
1214
	if (!$bpcfg['authdomain'])
1215
		$bpcfg['authdomain'] = $defaultdomain;
1216

    
1217
	$bpconf = <<<EOD
1218
username {$bpcfg['username']}
1219
password {$bpcfg['password']}
1220
authserver {$bpcfg['authserver']}
1221
authdomain {$bpcfg['authdomain']}
1222
localport 5050
1223

    
1224
EOD;
1225

    
1226
	if ($bpcfg['minheartbeatinterval'])
1227
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1228

    
1229
	fwrite($fd, $bpconf);
1230
	fclose($fd);
1231

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

    
1235
	return 0;
1236
}
1237

    
1238
function get_real_wan_interface() {
1239
	global $config, $g;
1240

    
1241
	$wancfg = $config['interfaces']['wan'];
1242

    
1243
	$wanif = $wancfg['if'];
1244
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1245
		$wanif = $g['pppoe_interface'];
1246
	}
1247

    
1248
	return $wanif;
1249
}
1250

    
1251
function get_current_wan_address($interface = "wan") {
1252
	global $config, $g;
1253

    
1254
	$wancfg = $config['interfaces'][$interface];
1255

    
1256
	$interface = filter_translate_type_to_real_interface($interface);
1257

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

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

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

    
1270
		return null;		
1271
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1272
		/* dynamic WAN IP address, find out which one */
1273
		$wanif = get_real_wan_interface();
1274

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

    
1278
		if (isset($ifinfo[1])) {
1279
			$aif = preg_split("/\s+/", $ifinfo[1]);
1280
			$curwanip = chop($aif[3]);
1281

    
1282
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1283
				return $curwanip;
1284
		}
1285

    
1286
		return null;
1287
	} else {
1288
		/* static WAN IP address */
1289
		return $wancfg['ipaddr'];
1290
	}
1291
}
1292

    
1293
/****f* interfaces/is_altq_capable
1294
 * NAME
1295
 *   is_altq_capable - Test if interface is capable of using ALTQ
1296
 * INPUTS
1297
 *   $int            - string containing interface name
1298
 * RESULT
1299
 *   boolean         - true or false
1300
 ******/
1301

    
1302
function is_altq_capable($int) {
1303
        /* Per:
1304
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1305
         * Only the following drivers have ALTQ support
1306
         */
1307
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1308
		"em", "fxp", "hme", "lnc", "ndis", "rl", "sf", "sis", "sk",
1309
		"tun", "vr", "wi", "xl");
1310

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

    
1313
        if (in_array($int_family[0], $capable))
1314
                return true;
1315
        else
1316
                return false;
1317
}
1318

    
1319
function get_number_of_bridged_interfaces() {
1320
	$bridges_total = 0;
1321
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1322
	foreach($bridges as $bridge) {
1323
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1324
		if($match_array[1][0] <> "") {
1325
			if($match_array[1][0] > $bridges_total)
1326
				$bridges_total = $match_array[1][0];
1327
		}
1328
	}
1329
	return "{$bridges_total}";
1330
}
1331

    
1332
function get_next_available_bridge_interface() {
1333
	$bridges_total = get_number_of_bridged_interfaces();
1334
	$interfaces = `/sbin/ifconfig -l`;
1335
	$x=0;
1336
	for($x=0; $x<$bridges_total; $x++) {
1337
		if(!stristr($interfaces, "bridge{$x}")) {
1338
			return "{$x}";
1339
		}
1340
	}
1341
	return "{$x}";
1342
}
1343

    
1344
function destroy_bridge($bridge_num) {
1345
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1346
	sleep(1);
1347
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1348
	sleep(1);
1349
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1350
	sleep(1);
1351
	return;
1352
}
1353

    
1354
function discover_bridge($interface1, $interface2) {
1355
	if(!$interface1) return;
1356
	if(!$interface2) return;
1357
	$total_bridges = get_number_of_bridged_interfaces();
1358
	$interfaces = `/sbin/ifconfig -l`;
1359
	$x=0;
1360
	for($x=0; $x<$total_bridges; $x++) {
1361
		$bridge_text = "NA";
1362
		if(!stristr($interfaces, "bridge{$x}")) 
1363
			continue;
1364
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1365
		if(stristr($bridge_text, $interface1) == true and
1366
		   stristr($bridge_text, $interface2) == true) {
1367
			return "{$x}";			
1368
		}
1369
	}
1370
	return "-1";
1371
}
1372

    
1373
?>
(8-8/24)