Project

General

Profile

Download (43.3 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
	/* if user has removed ip address, clear it*/
98
	if($lancfg['ipaddr'] == "")
99
		mwexec("/sbin/ifconfig {$lancfg['if']} delete");
100

    
101
	/* wireless configuration? */
102
	if (is_array($lancfg['wireless']))
103
		interfaces_wireless_configure($lancfg['if'], $lancfg['wireless']);
104

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

    
125
	/* bridged? */
126
	
127
	if ($lancfg['bridge']) {
128
		/* use open/netBSD style bridge */
129
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
130
		
131
		/* force all bridged interfaces to use same mtu */
132
		$mtu = get_interface_mtu($config['interfaces'][$lancfg['bridge']]['if']);
133
		mwexec("/sbin/ifconfig {$lancfg['if']} mtu {$mtu}");
134
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}");
135

    
136
		/* assign items to a bridge */
137
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}");
138
		
139
		if(!is_interface_wireless($lancfg['if']) and
140
		   !is_interface_wireless($config['interfaces'][$lancfg['bridge']]['if'])) 
141
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$lancfg['bridge']]['if']} stp {$lancfg['if']}");
142

    
143
		/* log commands run for debugging in /tmp/ */
144
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$lancfg['if']}", "w");
145
		fwrite($fd, "/sbin/ifconfig {$lancfg['if']} mtu {$mtu}\n");
146
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} mtu {$mtu}\n");
147
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
148
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$lancfg['if']} addm {$config['interfaces'][$lancfg['bridge']]['if']}\n");
149
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}\n");
150
		fclose($fd);
151
		
152
		/* bring up interfaces */
153
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
154
		usleep(100);
155
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} up");
156
		usleep(5);
157
		mwexec("/sbin/ifconfig {$lancfg['if']} up");
158
		usleep(5);
159
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
160
		
161
		$bridges_total++;
162
	}
163
	
164
	/* media */
165
	if ($lancfg['media'] || $lancfg['mediaopt']) {
166
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
167
		if ($lancfg['media'])
168
			$cmd .= " media " . escapeshellarg($lancfg['media']);
169
		if ($lancfg['mediaopt'])
170
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
171
		mwexec($cmd);
172
	}
173

    
174
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
175
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
176

    
177
	if (!$g['booting']) {
178
		/* make new hosts file */
179
		system_hosts_generate();
180

    
181
		/* reconfigure static routes (kernel may have deleted them) */
182
		system_routing_configure();
183

    
184
		/* set the reload filter dity flag */
185
		touch("{$g['tmp_path']}/filter_dirty");
186

    
187
		/* reload IPsec tunnels */
188
		vpn_ipsec_configure();
189

    
190
		/* reload dhcpd (gateway may have changed) */
191
		services_dhcpd_configure();
192

    
193
		/* reload dnsmasq */
194
		services_dnsmasq_configure();
195

    
196
		/* reload webgui */
197
		system_webgui_start();
198

    
199
		/* reload captive portal */
200
		captiveportal_configure();
201
	}
202

    
203
	return 0;
204
}
205

    
206
function interfaces_optional_configure() {
207
	global $config, $g;
208
	global $bridgeconfig;
209

    
210
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
211
		interfaces_optional_configure_if($i);
212
	}
213

    
214
	if (!$g['booting']) {
215
		/* reconfigure static routes (kernel may have deleted them) */
216
		system_routing_configure();
217

    
218
		/* reload IPsec tunnels */
219
		vpn_ipsec_configure();
220

    
221
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
222
		services_dhcpd_configure();
223

    
224
		/* restart dnsmasq */
225
		services_dnsmasq_configure();
226

    
227
		/* set the reload filter dity flag */
228
		touch("{$g['tmp_path']}/filter_dirty");				
229
	}
230

    
231
	return 0;
232
}
233

    
234
function interfaces_optional_configure_if($opti) {
235
	global $config, $g;
236
	global $bridgeconfig, $debugging;
237

    
238
	$bridges_total = get_next_available_bridge_interface();
239

    
240
	$optcfg = $config['interfaces']['opt' . $opti];
241

    
242
	if ($g['booting']) {
243
		$optdescr = "";
244
		if ($optcfg['descr'])
245
			$optdescr = " ({$optcfg['descr']})";
246
		print "\tOPT{$opti}{$optdescr}... ";
247
	}
248

    
249
	if (isset($optcfg['enable'])) {
250
		/* wireless configuration? */
251
		if (is_array($optcfg['wireless']))
252
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
253

    
254
		/* MAC spoofing? */
255
		if ($optcfg['spoofmac']) {
256
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
257
				" link " . escapeshellarg($optcfg['spoofmac']));
258
		} else {
259
			$mac = get_interface_mac_address($optcfg['if']);
260
			if($mac == "ff:ff:ff:ff:ff:ff") {
261
				/*   this is not a valid mac address.  generate a
262
				 *   temporary mac address so the machine can get online.
263
				 */
264
				echo "Generating new MAC address.";
265
				$random_mac = generate_random_mac_address();
266
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) .
267
					" link " . escapeshellarg($random_mac));
268
				$optcfg['spoofmac'] = $random_mac;
269
				write_config();
270
				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");
271
			}
272
		}
273

    
274
		/* media */
275
		if ($optcfg['media'] || $optcfg['mediaopt']) {
276
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
277
			if ($optcfg['media'])
278
				$cmd .= " media " . escapeshellarg($optcfg['media']);
279
			if ($optcfg['mediaopt'])
280
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
281
			mwexec($cmd);
282
		}
283

    
284
		/* OpenVPN configuration? */
285
 		if (isset($optcfg['ovpn'])) {
286
 			if (strstr($optcfg['if'], "tap"))
287
 				ovpn_link_tap();
288
 		}
289

    
290
		/* bridged? */
291
		if ($optcfg['bridge']) {
292
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
293
                        /* use open/netBSD style bridge */
294
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
295
			
296
			/* force all bridged interfaces to use same mtu */
297
			$mtu = get_interface_mtu($config['interfaces'][$optcfg['bridge']]['if']);
298
			mwexec("/sbin/ifconfig {$optcfg['if']} mtu {$mtu}");
299
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}");			
300

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

    
304
			if(!is_interface_wireless($optcfg['if']) and
305
			   !is_interface_wireless($config['interfaces'][$optcfg['bridge']]['if'])) 
306
				mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$optcfg['bridge']]['if']} stp {$optcfg['if']}");
307

    
308
			/* log commands run for debugging in /tmp/ */
309
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
310
			fwrite($fd, "/sbin/ifconfig {$optcfg['if']} mtu {$mtu}\n");
311
			fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}\n");
312
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
313
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up\n");
314
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} stp {$config['interfaces'][$optcfg['bridge']]['if']}\n");
315
			fclose($fd);
316
			
317
			/* bring up interfaces */
318
			mwexec("/sbin/ifconfig bridge{$bridges_total} down");
319
			usleep(100);
320
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} up");
321
			usleep(5);
322
			mwexec("/sbin/ifconfig {$optcfg['if']} up");
323
			usleep(5);
324
			mwexec("/sbin/ifconfig bridge{$bridges_total} up");
325
			
326
			$bridges_total++;
327
			
328
		} else {
329
			/* if user has selected DHCP type then act accordingly */
330
			if($optcfg['ipaddr'] == "dhcp") {
331
				interfaces_opt_dhcp_configure("opt{$opti}");
332
			} else {			
333
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
334
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
335
			}
336
		}
337
	} else {
338
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
339
	}
340
	return 0;
341
}
342

    
343
function interfaces_carp_configure() {
344
	global $g, $config, $debugging;
345
	$carp_instances_counter = 0;
346
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
347
	/* destroy previous interfaces */
348
	for($x=0; $x<$total_carp_interfaces_defined; $x++) 
349
		mwexec("/sbin/ifconfig carp{$x} delete");	
350
	if(isset($config['system']['developerspew'])) {
351
		$mt = microtime();
352
		echo "interfaces_carp_configure() being called $mt\n";
353
	}
354
	/* if neither items are arrays then redirect pfsync to loopback */
355
	if (!is_array($config['virtualip']['vip']) or
356
	    !is_array($config['installedpackages']['carpsettings']['config'])) {
357
		mwexec("/sbin/ifconfig pfsync0 syncdev lo0 up");
358
		if($g['booting']) {
359
			unmute_kernel_msgs();
360
			echo "done.\n";
361
		}
362
		return;
363
	}
364
	if(!$config['installedpackages']['carpsettings']['config'])
365
		return;	
366
	if ($g['booting'] and !$debugging) {
367
		echo "Configuring CARP interfaces...";
368
		mute_kernel_msgs();
369
	}	
370
	foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
371
		if($carp['pfsyncenabled'] != "") {
372
			mwexec("/sbin/sysctl net.inet.carp.allow=1");
373
			if($debugging) 
374
				echo "Enabling preempt\n";
375
			if($carp['premption'] != "")
376
				mwexec("/sbin/sysctl net.inet.carp.preempt=1");
377
			if($carp['balancing'] != "")
378
				mwexec("/sbin/sysctl net.inet.carp.arpbalance=1");
379
			if($debugging) 
380
				echo "Get friendly interface name {$carp['pfsyncinterface']}.\n";
381
			$carp_sync_int = convert_friendly_interface_to_real_interface_name($carp['pfsyncinterface']);
382
			if($debugging) 
383
				echo "Friendly name {$carp_sync_int}.\n";
384
			$carp_sync_int = convert_friendly_interface_to_real_interface_name($carp['pfsyncinterface']);
385
			if($g['booting']) {
386
				/*    install rules to alllow pfsync to sync up during boot
387
				 *    carp interfaces will remain down until the bootup sequence finishes
388
				 */
389
				if($debugging) 
390
					echo "Adding firewall rules..\n";
391
				exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
392
				exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
393
				exec("echo pass out proto { tcp, udp } from any to any port 53 keep state >> /tmp/rules.boot");
394
				exec("/sbin/pfctl -f /tmp/rules.boot");
395
				if($debugging) {
396
					echo "Showing loaded rule set:\n";
397
					system("/sbin/pfctl -vvsr");
398
				}
399
			}			
400
			/* do not setup pfsync twice */
401
			if($total_carp_interfaces_defined == 0) {
402
				//if($debugging)
403
				//	echo "Bringing up pfsync0.\n";
404
				//mwexec("/sbin/ifconfig pfsync0 create");
405
				if($debugging)
406
					echo "Assigning syncdev to {$carp_sync_int}.\n";						
407
				mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up");
408
			}
409
		}
410
	}
411
	$viparr = &$config['virtualip']['vip'];
412
	$fd = fopen("/tmp/carp.sh", "w");
413
	foreach ($viparr as $vip) {
414
		if ($vip['mode'] == "carp") {
415
			/*
416
			 *   create the carp interface
417
			 */
418
			if($debugging)
419
				echo "Creating carp{$carp_instances_counter}.\n";
420
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
421
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
422
			if($vip['password'] != "") 
423
				$password = " pass " . $vip['password'];
424
			/* XXX: billm - carpdev not in our build?
425
			    $carpdev = "";
426
			    if(isset($vip['interface']) && ($vip['interface'] != "AUTO" && $vip['interface'] != "")) {
427
			   	$ci = filter_opt_interface_to_real($vip['interface']);
428
			   	$carpdev = " carpdev {$ci} ";
429
			    }
430
			*/
431
			if($debugging)
432
				echo "Configuring carp{$carp_instances_counter}.\n";
433
			fwrite($fd, "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password . "\n");
434
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew 200 " . $password);
435
			usleep(10);
436
			mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
437
			fwrite($fd, "/sbin/ifconfig carp" . $carp_instances_counter . " up");
438
			$carp_instances_counter++;
439
		}
440
	}
441
	mwexec("/bin/sh /tmp/carp.sh");
442
	fclose($fd);
443
	if ($g['booting']) {
444
		unmute_kernel_msgs();
445
		echo "done.\n";
446
	}
447
}
448

    
449
function interfaces_carp_bring_up_final() {
450
	global $config, $g, $debugging;
451
	if(isset($config['system']['developerspew'])) {
452
		$mt = microtime();
453
		echo "interfaces_carp_bring_up_final() being called $mt\n";
454
	}
455
	if(!$config['installedpackages']['carpsettings']['config'])
456
		return;
457
	$viparr = &$config['virtualip']['vip'];
458
	/* could not locate an array, return */
459
	if(!is_array($viparr)) 
460
		return;
461
	$carp_instances_counter = 0;
462
	$counter = 0;
463
	if($g['booting'])
464
		echo "Waiting for final CARP interface bringup...";
465
	$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
466
	if($g['booting']) {
467
		while($supress > 0) {
468
			sleep(2);
469
			$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
470
			if($counter > 15)
471
				$supress = 0;
472
			$counter++;
473
			echo ".";
474
		}
475
		for($x=0; $x<23; $x++) {
476
			sleep(2);
477
			echo ".";
478
		}
479
		echo " done.\n";
480
	}
481
	foreach ($viparr as $vip) {
482
		if($debugging)
483
			echo "Upping interface carp{$carp_instances_counter}.\n";
484
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
485
		if($vip['password'] != "") 
486
			$password = " pass " . $vip['password'];
487
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
488
		if($debugging)
489
			echo "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password . "\n";
490
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
491
		$carp_instances_counter++;
492
	}
493
	if($g['booting'])
494
		echo " done.\n";	
495
}
496

    
497
function interfaces_wireless_configure($if, $wlcfg) {
498
	global $config, $g;
499
	
500
	/* set values for /path/program */
501
	$hostapd = "/usr/sbin/hostapd";
502
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
503
	$ifconfig = "/sbin/ifconfig ";
504
	$killall = "/usr/bin/killall ";
505

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

    
508
	/* Set a/b/g standard */
509
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
510

    
511
	/* Set 802.11g protection mode */
512
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
513

    
514
	/* set wireless channel value */
515
	if(isset($wlcfg['channel']))
516
		$channel = "channel " . escapeshellarg($wlcfg['channel']);
517

    
518
	/* set Distance value */
519
	if($wlcfg['distance']) 
520
		$distance = escapeshellarg($wlcfg['distance']);
521

    
522
	/* Set ssid */
523
	if($wlcfg['ssid'])  
524
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
525

    
526
	/* Set stationname */
527
	if ($wlcfg['stationname'])
528
		$stationname = "stationname " . escapeshellarg($wlcfg['stationname']);
529
	else
530
		$stationname = "stationname pfsense";
531
		
532
	/* Set wireless hostap mode */
533
	if ($wlcfg['mode'] == "hostap")
534
		$hostapmode = "mediaopt hostap";
535
	else
536
		$hostapmode = "-mediaopt hostap";
537

    
538
	/* Set wireless adhoc mode */
539
	if ($wlcfg['mode'] == "adhoc")
540
		$adhocmode = "mediaopt adhoc";
541
	else
542
		$adhocmode = "-mediaopt adhoc";
543

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

    
546
	/* handle hide ssid option */
547
	if(isset($wlcfg['hidessid']['enable']))
548
		$hidessid = "hidessid";
549
	else
550
		$hidessid = "-hidessid";
551

    
552
	/* handle pureg (802.11g) only option */
553
	if(isset($wlcfg['pureg']['enable']))
554
		$pureg = "mode 11g pureg";
555
	else
556
		$pureg = "-pureg";
557

    
558
	/* enable apbridge option */
559
	if(isset($wlcfg['apbridge']['enable']))
560
		$apbridge = "apbridge";
561
	else
562
		$apbridge = "-apbridge";
563

    
564
	/* handle turbo option */
565
	if(isset($wlcfg['turbo']['enable']))
566
		$turbo = "mediaopt turbo";
567
	else
568
		$turbo = "-mediaopt turbo";
569

    
570
	/* handle txpower setting */
571
	if($wlcfg['txpower'] <> "")
572
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
573
	
574
	/* handle wme option */
575
	if(isset($wlcfg['wme']['enable']))
576
		$wme = "wme";
577
	else
578
		$wme = "-wme";
579
	
580
	/* set up wep if enabled */
581
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
582
                $wepset .= "authmode shared wepmode on ";
583
                $i = 1;
584
                foreach ($wlcfg['wep']['key'] as $wepkey) {
585
                        $wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
586
                        if (isset($wepkey['txkey'])) {
587
                                $wepset .= "weptxkey {$i} ";
588
                        }
589
                        $i++;
590
                }
591
        } else {
592
                $wepset = "authmode open wepmode off";
593
	}
594

    
595
	/* generate wpa_supplicant/hostap config if wpa is enabled */
596

    
597
	switch ($wlcfg['mode']) {
598
		case 'bss':
599
			if (isset($wlcfg['wpa']['enable'])) {
600

    
601
				$wpa .= <<<EOD
602
ctrl_interface={$g['varrun_path']}/wpa_supplicant
603
ctrl_interface_group=0
604
ap_scan=1
605
#fast_reauth=1
606
network={
607
ssid="{$wlcfg['ssid']}"
608
scan_ssid=1
609
priority=5
610
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
611
psk="{$wlcfg['wpa']['passphrase']}"
612
pairwise={$wlcfg['wpa']['wpa_pairwise']}
613
group={$wlcfg['wpa']['wpa_pairwise']}
614
}
615
EOD;
616

    
617
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
618
				fwrite($fd, "{$wpa}");
619
				fclose($fd);
620

    
621
				mwexec("{$killall} wpa_supplicant");
622
			}
623
		break;
624

    
625
		case 'hostap':
626
			if (isset($wlcfg['wpa']['enable'])) {
627
				$wpa .= <<<EOD
628
interface={$if}
629
driver=bsd
630
logger_syslog=-1
631
logger_syslog_level=0
632
logger_stdout=-1
633
logger_stdout_level=0
634
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
635
ctrl_interface={$g['varrun_path']}/hostapd
636
ctrl_interface_group=wheel
637
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
638
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
639
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
640
ssid={$wlcfg['ssid']}
641
debug={$wlcfg['wpa']['debug_mode']}
642
auth_algs={$wlcfg['wpa']['auth_algs']}
643
wpa={$wlcfg['wpa']['wpa_mode']}
644
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
645
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
646
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
647
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
648
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
649
wpa_passphrase={$wlcfg['wpa']['passphrase']}
650
ieee8021x={$wlcfg['wpa']['ieee8021x']}
651
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
652
#rsn_preauth=1
653
#rsn_preauth_interfaces=eth0
654
EOD;
655

    
656
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
657
				fwrite($fd, "{$wpa}");
658
				fclose($fd);
659

    
660
				mwexec("{$killall} hostapd");
661
			}
662
		break;
663

    
664
		case 'adhoc':
665
			mwexec("{$killall} hostapd");
666
			mwexec("{$killall} wpa_supplicant");
667
		break;
668
	}	
669

    
670
	/* all variables are set, lets start up everything */
671
	
672
	mwexec("$ifconfig $if" . " -mediaopt hostap,turbo");	// Fix bug with turbomode and reboot (hopefully)        
673
	mwexec("$ifconfig $if" . " down"); 
674
	mwexec("$ifconfig $if" . " " . $standard);
675
	mwexec("$ifconfig $if" . " " . $protmode);
676
	mwexec("$ifconfig $if" . " " . $channel);
677
	mwexec("$ifconfig $if" . " " . $ssid);
678
	mwexec("$ifconfig $if" . " " . $stationname);
679
	mwexec("$ifconfig $if" . " " . $adhocmode);
680
	/* fix turbo mode and reboot */
681
	mwexec("$ifconfig $if" . " up"); 
682
	mwexec("$ifconfig $if" . " " . $hostapmode);
683
	mwexec("$ifconfig $if" . " " . $turbo);
684
	mwexec("$ifconfig $if" . " down"); 
685
	/* fix turbo mode and reboot */
686
	mwexec("$ifconfig $if" . " " . $hidessid);
687
	mwexec("$ifconfig $if" . " " . $pureg);
688
	mwexec("$ifconfig $if" . " " . $apbridge);
689
	mwexec("$ifconfig $if" . " " . $wme);
690
	mwexec("$ifconfig $if" . " " . $wepset);
691
	mwexec("$ifconfig $if" . " up");
692

    
693
	if (isset($wlcfg['wpa']['enable'])) {
694
		if ($wlcfg['mode'] == bss) 
695
			mwexec("{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf");
696
		if ($wlcfg['mode'] == hostap) 
697
			mwexec("{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf");
698
	}
699

    
700
	/* set ack timers according to users preference (if he/she has any) */
701
	if($distance) {
702
		mwexec("/sbin/athctrl.sh -i {$if} -d {$distance}");
703
		$fd = fopen("/tmp/athctrl.txt", "w");
704
		fwrite($fd, "/sbin/athctrl.sh -i {$if} -d {$distance}");
705
		fclose($fd);		
706
	}
707

    
708
	if(isset($wlcfg['useolsr']))
709
		setup_wireless_olsr($if);
710

    
711
	return 0;
712

    
713
}
714

    
715
function find_dhclient_process($interface) {
716
	if(filter_translate_type_to_real_interface($interface) <> "")
717
        	$realinterface = filter_translate_type_to_real_interface($interface);
718
	$pid = `/usr/bin/pgrep -f "dhclient: {$realinterface}(\$| .*)"`;
719
	return $pid;
720
}
721

    
722
function interfaces_wan_configure() {
723
	global $config, $g;
724

    
725
	$wancfg = $config['interfaces']['wan'];
726

    
727
	if(!$g['booting']) {
728
		mute_kernel_msgs();
729

    
730
		/* find dhclient process for wan and kill it */
731
		killbypid(find_dhclient_process("wan"));
732

    
733
		/* remove wanup file if it exists */
734
		unlink_if_exists("{$g['tmp_path']}/wanup");
735

    
736
		/* kill PPPoE client (mpd) */
737
		killbypid("{$g['varrun_path']}/mpd.pid");
738

    
739
		/* wait for processes to die */
740
		sleep(3);
741

    
742
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
743
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
744
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
745
		unlink_if_exists("{$g['vardb_path']}/wanip");
746
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
747
	}
748

    
749
	/* remove all addresses first */
750
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
751
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
752

    
753
	/* wireless configuration? */
754
	if (is_array($wancfg['wireless']))
755
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
756

    
757
	if ($wancfg['spoofmac']) {
758
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
759
			" link " . escapeshellarg($wancfg['spoofmac']));
760
	}  else {
761
		$mac = get_interface_mac_address($wancfg['if']);
762
		if($mac == "ff:ff:ff:ff:ff:ff") {
763
			/*   this is not a valid mac address.  generate a
764
			 *   temporary mac address so the machine can get online.
765
			 */
766
			echo "Generating new MAC address.";
767
			$random_mac = generate_random_mac_address();
768
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
769
				" link " . escapeshellarg($random_mac));
770
			$wancfg['spoofmac'] = $random_mac;
771
			write_config();
772
			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");
773
		}
774
	}
775

    
776
	/* media */
777
	if ($wancfg['media'] || $wancfg['mediaopt']) {
778
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
779
		if ($wancfg['media'])
780
			$cmd .= " media " . escapeshellarg($wancfg['media']);
781
		if ($wancfg['mediaopt'])
782
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
783
		mwexec($cmd);
784
	}
785

    
786
	switch ($wancfg['ipaddr']) {
787

    
788
		case 'dhcp':
789
			interfaces_wan_dhcp_configure();
790
			break;
791

    
792
		case 'pppoe':
793
			interfaces_wan_pppoe_configure();
794
			break;
795

    
796
		case 'pptp':
797
			interfaces_wan_pptp_configure();
798
			break;
799

    
800
		case 'bigpond':
801
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
802
			interfaces_wan_dhcp_configure();
803
			break;
804

    
805
		default:
806
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
807
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
808
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
809
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
810
			} else {
811
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
812
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
813
			}
814
			/* install default route */
815
			mwexec("/sbin/route delete default");
816
			mwexec("/sbin/route add default " . escapeshellarg($config['interfaces']['wan']['gateway']));
817

    
818
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
819
			filter_configure();
820
	}
821

    
822
	if ($wancfg['bridge']) {
823
		/* use open/netBSD style bridge */
824
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
825
		
826
		/* force all bridged interfaces to use same mtu */
827
		$mtu = get_interface_mtu($config['interfaces'][$wancfg['bridge']]['if']);
828
		mwexec("/sbin/ifconfig {$wancfg['if']} mtu {$mtu}");
829
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}");
830
		
831
		/* assign items to a bridge */
832
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}");
833

    
834
		if(!is_interface_wireless($wancfg['if']) and
835
		   !is_interface_wireless($config['interfaces'][$wancfg['bridge']]['if'])) 
836
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$config['interfaces'][$wancfg['bridge']]['if']} stp {$wancfg['if']}");
837
		
838
		/* log commands run for debugging in /tmp/ */
839
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$wancfg['if']}", "w");
840
		fwrite($fd, "/sbin/ifconfig {$wancfg['if']} mtu {$mtu}\n");
841
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}\n");
842
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
843
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}\n");
844
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$wancfg['if']} stp {$config['interfaces'][$wancfg['bridge']]['if']}\n");
845
		fclose($fd);
846
		
847
		/* bring up interfaces */
848
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
849
		usleep(100);
850
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} up");
851
		usleep(5);
852
		mwexec("/sbin/ifconfig {$wancfg['if']} up");
853
		usleep(5);
854
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
855
		
856
		$bridges_total++;
857
	}
858

    
859
	if (!$g['booting']) {
860
		/* reconfigure static routes (kernel may have deleted them) */
861
		system_routing_configure();
862

    
863
		/* set the reload filter dity flag */
864
		touch("{$g['tmp_path']}/filter_dirty");
865

    
866
		/* reload ipsec tunnels */
867
		vpn_ipsec_configure();
868

    
869
		/* restart ez-ipupdate */
870
		services_dyndns_configure();
871

    
872
		/* force DNS update */
873
		services_dnsupdate_process();
874

    
875
		/* restart dnsmasq */
876
		services_dnsmasq_configure();
877
	}
878

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

    
881
	unmute_kernel_msgs();
882

    
883
	return 0;
884
}
885

    
886
function interfaces_opt_dhcp_configure($interface) {
887
	global $config, $g;
888

    
889
	$optcfg = $config['interfaces'][$interface];
890
	$optif = $optcfg['if'];
891

    
892
	/* generate dhclient_wan.conf */
893
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
894
	if (!$fd) {
895
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
896
		return 1;
897
	}
898

    
899
	if ($optcfg['dhcphostname']) {
900
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
901
	} else {
902
		$dhclientconf_hostname = "";
903
	}
904

    
905
 	$dhclientconf = "";
906

    
907
	$dhclientconf .= <<<EOD
908
interface "{$optif}" {
909
	send host-name "{$optcfg['dhcphostname']}";
910
	script "/sbin/dhclient-script";
911
	{$dhclientconf_hostname}
912
}
913

    
914
EOD;
915

    
916
	fwrite($fd, $dhclientconf);
917
	fclose($fd);
918

    
919
        /* bring interface up before starting dhclient */
920
        mwexec("/sbin/ifconfig {$optif} up");
921

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

    
925
	return 0;
926
}
927

    
928
function interfaces_dhcp_configure($interface) {
929
	global $config, $g;
930

    
931
	if(filter_translate_type_to_real_interface($interface) <> "")
932
        	$realinterface = filter_translate_type_to_real_interface($interface);
933

    
934
	$optcfg = $config['interfaces'][$interface];
935

    
936
	/* generate dhclient_$interface.conf */
937
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
938
	if (!$fd) {
939
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
940
		return 1;
941
	}
942

    
943
	if ($optcfg['dhcphostname']) {
944
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
945
	} else {
946
		$dhclientconf_hostname = "";
947
	}
948

    
949
 	$dhclientconf = "";
950

    
951
	$dhclientconf .= <<<EOD
952
interface "{$realinterface}" {
953
	script "/sbin/dhclient-script";
954
	{$dhclientconf_hostname}
955
}
956

    
957
EOD;
958

    
959
	fwrite($fd, $dhclientconf);
960
	fclose($fd);
961
	
962
	$optif = $optcfg['if'];
963
	
964
        /* bring wan interface up before starting dhclient */
965
        mwexec("/sbin/ifconfig {$optif} up");
966

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

    
970
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
971
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
972
	fclose($fout);
973

    
974
	return 0;
975
}
976

    
977
function interfaces_wan_dhcp_configure() {
978
	global $config, $g;
979

    
980
	$wancfg = $config['interfaces']['wan'];
981

    
982
	/* generate dhclient_wan.conf */
983
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
984
	if (!$fd) {
985
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
986
		return 1;
987
	}
988
	
989
	if ($wancfg['dhcphostname']) {
990
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
991
	} else {
992
		$dhclientconf_hostname = "";
993
	}
994

    
995
 	$dhclientconf = "";
996

    
997
	$dhclientconf .= <<<EOD
998
interface "{$wancfg['if']}" {
999
	script "/sbin/dhclient-script";
1000
	{$dhclientconf_hostname}
1001
}
1002

    
1003
EOD;
1004

    
1005
	fwrite($fd, $dhclientconf);
1006
	fclose($fd);
1007
	
1008
	$wanif = $wancfg['if'];
1009
	
1010
        /* bring wan interface up before starting dhclient */
1011
        mwexec("/sbin/ifconfig {$wanif} up");
1012

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

    
1016
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1017
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
1018
	fclose($fout);
1019

    
1020
	return 0;
1021
}
1022

    
1023
function interfaces_wan_dhcp_down() {
1024
	global $config;
1025
	$wancfg = $config['interfaces']['wan'];
1026
	$wanif = $wancfg['if'];
1027
	mwexec("/sbin/ifconfig {$wanif} delete");
1028
	sleep(1);
1029
}
1030

    
1031
function interfaces_dhcp_down($interface) {
1032
	global $config;
1033
	if(filter_translate_type_to_real_interface($interface) <> "")
1034
		$realinterface = filter_translate_type_to_real_interface($interface);
1035
	mwexec("/sbin/ifconfig {$realinterface} down");
1036
	sleep(1);
1037
	$pid = find_dhclient_process($interface);
1038
	if($pid)
1039
		mwexec("kill {$pid}");
1040
}
1041

    
1042
function interfaces_dhcp_up($interface) {
1043
	interfaces_dhcp_configure($interface);
1044
	sleep(1);
1045
}
1046

    
1047
function interfaces_wan_dhcp_up() {
1048
	interfaces_wan_dhcp_configure();
1049
	sleep(1);
1050
}
1051

    
1052
function interfaces_wan_pppoe_configure() {
1053
	global $config, $g;
1054

    
1055
	$wancfg = $config['interfaces']['wan'];
1056
	$pppoecfg = $config['pppoe'];
1057

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

    
1065
	$idle = 0;
1066

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

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

    
1083
EOD;
1084

    
1085
	/*    Check for ppp-linkdown Script in /usr/local/sbin
1086
	 *    Create reference in mpd.conf
1087
	 */
1088
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1089
		$mpdconf .= <<<EOD
1090
	set iface down-script /usr/local/sbin/ppp-linkdown
1091

    
1092
EOD;
1093
	}
1094

    
1095
	if (isset($pppoecfg['ondemand'])) {
1096
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
1097
			$mpdconf .= <<<EOD
1098
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
1099

    
1100
EOD;
1101
		} else {
1102
			$mpdconf .= <<<EOD
1103
	set iface addrs 192.0.2.112 192.0.2.113
1104

    
1105
EOD;
1106
		}
1107
	}
1108

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

    
1122
EOD;
1123

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

    
1128
EOD;
1129
	}
1130

    
1131
	$mpdconf .= <<<EOD
1132
	open iface
1133

    
1134
EOD;
1135

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

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

    
1146
	$mpdconf = <<<EOD
1147
pppoe:
1148
	set link type pppoe
1149
	set pppoe iface {$wancfg['if']}
1150
	set pppoe service "{$pppoecfg['provider']}"
1151
	set pppoe enable originate
1152
	set pppoe disable incoming
1153

    
1154
EOD;
1155

    
1156
	fwrite($fd, $mpdconf);
1157
	fclose($fd);
1158
	
1159
	if(file_exists("{$g['varrun_path']}/mpd.pid") and $g['booting']) {
1160
		/* if we are booting and mpd has already been started then don't start again. */
1161
	} else {
1162
		/* if mpd is active, lets take it down */
1163
		if(file_exists("{$g['varrun_path']}/mpd.pid")) {
1164
			killbypid("{$g['varrun_path']}/mpd.pid");
1165
			sleep(3);
1166
		}
1167
		/* fire up mpd */	
1168
		mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pppoe");
1169
	}
1170

    
1171
        /* sleep until wan is up - or 30 seconds, whichever comes first */
1172
	for ($count = 0; $count < 30; $count++) {
1173
		if(file_exists("{$g['tmp_path']}/wanup")) {
1174
			break;
1175
		}
1176
		sleep(1);
1177
	}
1178

    
1179
	unlink_if_exists("{$g['tmp_path']}/wanup");
1180

    
1181
	return 0;
1182
}
1183

    
1184
function interfaces_wan_pppoe_down() {
1185
	global $g;
1186
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1187
	sleep(1);
1188
}
1189

    
1190
function interfaces_wan_pppoe_up() {
1191
	global $g;
1192
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1193
	sleep(1);
1194
}
1195

    
1196
function interfaces_wan_pptp_configure() {
1197
	global $config, $g;
1198

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

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

    
1209
	$idle = 0;
1210

    
1211
	if (isset($pptpcfg['ondemand'])) {
1212
		$ondemand = "enable";
1213
		if ($pptpcfg['timeout'])
1214
			$idle = $pptpcfg['timeout'];
1215
	} else {
1216
		$ondemand = "disable";
1217
	}
1218

    
1219
	$mpdconf = <<<EOD
1220
pptp:
1221
	new -i ng0 pptp pptp
1222
	set iface route default
1223
	set iface {$ondemand} on-demand
1224
	set iface idle {$idle}
1225
	set iface up-script /usr/local/sbin/ppp-linkup
1226

    
1227
EOD;
1228

    
1229
	/*   Check for ppp-linkdown Script in /usr/local/sbin
1230
	 *   Create reference in mpd.conf
1231
	 */
1232
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1233
		$mpdconf .= <<<EOD
1234
	set iface down-script /usr/local/sbin/ppp-linkdown
1235

    
1236
EOD;
1237
	}
1238

    
1239
	if (isset($pptpcfg['ondemand'])) {
1240
		$mpdconf .= <<<EOD
1241
	set iface addrs 10.0.0.1 10.0.0.2
1242

    
1243
EOD;
1244
	}
1245

    
1246
	$mpdconf .= <<<EOD
1247
	set bundle disable multilink
1248
	set bundle authname "{$pptpcfg['username']}"
1249
	set bundle password "{$pptpcfg['password']}"
1250
	set link keep-alive 10 60
1251
	set link max-redial 0
1252
	set link no acfcomp protocomp
1253
	set link disable pap chap
1254
	set link accept chap
1255
	set ipcp no vjcomp
1256
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1257

    
1258
EOD;
1259

    
1260
	if (isset($config['system']['dnsallowoverride'])) {
1261
		$mpdconf .= <<<EOD
1262
	set ipcp enable req-pri-dns
1263

    
1264
EOD;
1265
	}
1266

    
1267
	$mpdconf .= <<<EOD
1268
	open
1269

    
1270
EOD;
1271

    
1272
	fwrite($fd, $mpdconf);
1273
	fclose($fd);
1274

    
1275
	/* generate mpd.links */
1276
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1277
	if (!$fd) {
1278
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1279
		return 1;
1280
	}
1281

    
1282
	$mpdconf = <<<EOD
1283
pptp:
1284
	set link type pptp
1285
	set pptp enable originate outcall
1286
	set pptp disable windowing
1287
	set pptp self {$pptpcfg['local']}
1288
	set pptp peer {$pptpcfg['remote']}
1289

    
1290
EOD;
1291

    
1292
	fwrite($fd, $mpdconf);
1293
	fclose($fd);
1294

    
1295
	/* configure interface */
1296
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1297
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1298

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

    
1302
	return 0;
1303
}
1304

    
1305
function interfaces_wan_pptp_down() {
1306
	global $g;
1307
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1308
	sleep(1);
1309
}
1310

    
1311
function interfaces_wan_pptp_up() {
1312
	global $g;
1313
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1314
	sleep(1);
1315
}
1316

    
1317
function interfaces_wan_bigpond_configure($curwanip) {
1318
	global $config, $g;
1319

    
1320
	$bpcfg = $config['bigpond'];
1321

    
1322
	if (!$curwanip) {
1323
		/* IP address not configured yet, exit */
1324
		return 0;
1325
	}
1326

    
1327
	/* kill bpalogin */
1328
	killbyname("bpalogin");
1329

    
1330
	/* wait a moment */
1331
	sleep(1);
1332

    
1333
	/* get the default domain */
1334
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1335
	if ($nfd) {
1336
		$defaultdomain = trim(fgets($nfd));
1337
		fclose($nfd);
1338
	}
1339

    
1340
	/* generate bpalogin.conf */
1341
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1342
	if (!$fd) {
1343
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1344
		return 1;
1345
	}
1346

    
1347
	if (!$bpcfg['authserver'])
1348
		$bpcfg['authserver'] = "dce-server";
1349
	if (!$bpcfg['authdomain'])
1350
		$bpcfg['authdomain'] = $defaultdomain;
1351

    
1352
	$bpconf = <<<EOD
1353
username {$bpcfg['username']}
1354
password {$bpcfg['password']}
1355
authserver {$bpcfg['authserver']}
1356
authdomain {$bpcfg['authdomain']}
1357
localport 5050
1358

    
1359
EOD;
1360

    
1361
	if ($bpcfg['minheartbeatinterval'])
1362
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1363

    
1364
	fwrite($fd, $bpconf);
1365
	fclose($fd);
1366

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

    
1370
	return 0;
1371
}
1372

    
1373
function get_real_wan_interface() {
1374
	global $config, $g;
1375

    
1376
	$wancfg = $config['interfaces']['wan'];
1377

    
1378
	$wanif = $wancfg['if'];
1379
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1380
		$wanif = $g['pppoe_interface'];
1381
	}
1382

    
1383
	return $wanif;
1384
}
1385

    
1386
function get_current_wan_address($interface = "wan") {
1387
	global $config, $g;
1388

    
1389
	$wancfg = $config['interfaces'][$interface];
1390

    
1391
	$interface = filter_translate_type_to_real_interface($interface);
1392

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

    
1397
		if (isset($ifinfo[1])) {
1398
			$aif = preg_split("/\s+/", $ifinfo[1]);
1399
			$curwanip = chop($aif[3]);
1400

    
1401
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1402
				return $curwanip;
1403
		}
1404

    
1405
		return null;		
1406
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1407
		/* dynamic WAN IP address, find out which one */
1408
		$wanif = get_real_wan_interface();
1409

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

    
1413
		if (isset($ifinfo[1])) {
1414
			$aif = preg_split("/\s+/", $ifinfo[1]);
1415
			$curwanip = chop($aif[3]);
1416

    
1417
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1418
				return $curwanip;
1419
		}
1420

    
1421
		return null;
1422
	} else {
1423
		/* static WAN IP address */
1424
		return $wancfg['ipaddr'];
1425
	}
1426
}
1427

    
1428
/****f* interfaces/is_altq_capable
1429
 * NAME
1430
 *   is_altq_capable - Test if interface is capable of using ALTQ
1431
 * INPUTS
1432
 *   $int            - string containing interface name
1433
 * RESULT
1434
 *   boolean         - true or false
1435
 ******/
1436

    
1437
function is_altq_capable($int) {
1438
        /* Per:
1439
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1440
         * Only the following drivers have ALTQ support
1441
         */
1442
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1443
		"em", "fxp", "hme", "lnc", "ndis", "rl", "sf", "sis", "sk",
1444
		"tun", "vr", "wi", "xl", "vlan", "ste");
1445

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

    
1448
        if (in_array($int_family[0], $capable))
1449
                return true;
1450
        else
1451
                return false;
1452
}
1453

    
1454
function get_number_of_bridged_interfaces() {
1455
	$bridges_total = 0;
1456
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1457
	foreach($bridges as $bridge) {
1458
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1459
		if($match_array[1][0] <> "") {
1460
			if($match_array[1][0] > $bridges_total)
1461
				$bridges_total = $match_array[1][0];
1462
		}
1463
	}
1464
	return "{$bridges_total}";
1465
}
1466

    
1467
function get_next_available_bridge_interface() {
1468
	$bridges_total = get_number_of_bridged_interfaces();
1469
	$interfaces = `/sbin/ifconfig -l`;
1470
	$x=0;
1471
	for($x=0; $x<$bridges_total; $x++) {
1472
		if(!stristr($interfaces, "bridge{$x}")) {
1473
			return "{$x}";
1474
		}
1475
	}
1476
	return "{$x}";
1477
}
1478

    
1479
function destroy_bridge($bridge_num) {
1480
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1481
	sleep(1);
1482
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1483
	sleep(1);
1484
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1485
	sleep(1);
1486
	return;
1487
}
1488

    
1489
function discover_bridge($interface1, $interface2) {
1490
	if(!$interface1) return;
1491
	if(!$interface2) return;
1492
	$total_bridges = get_number_of_bridged_interfaces();
1493
	$interfaces = `/sbin/ifconfig -l`;
1494
	$x=0;
1495
	for($x=0; $x<$total_bridges; $x++) {
1496
		$bridge_text = "NA";
1497
		if(!stristr($interfaces, "bridge{$x}")) 
1498
			continue;
1499
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1500
		if(stristr($bridge_text, $interface1) == true and
1501
		   stristr($bridge_text, $interface2) == true) {
1502
			return "{$x}";			
1503
		}
1504
	}
1505
	return "-1";
1506
}
1507

    
1508
?>
(10-10/26)