Project

General

Profile

Download (40.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
	/* 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']} up");
138
		mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$lancfg['if']} stp {$config['interfaces'][$lancfg['bridge']]['if']}");
139

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

    
164
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
165
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
166

    
167
	if (!$g['booting']) {
168
		/* make new hosts file */
169
		system_hosts_generate();
170

    
171
		/* reconfigure static routes (kernel may have deleted them) */
172
		system_routing_configure();
173

    
174
		/* set the reload filter dity flag */
175
		touch("{$g['tmp_path']}/filter_dirty");
176

    
177
		/* reload IPsec tunnels */
178
		vpn_ipsec_configure();
179

    
180
		/* reload dhcpd (gateway may have changed) */
181
		services_dhcpd_configure();
182

    
183
		/* reload dnsmasq */
184
		services_dnsmasq_configure();
185

    
186
		/* reload webgui */
187
		system_webgui_start();
188

    
189
		/* reload captive portal */
190
		captiveportal_configure();
191
	}
192

    
193
	return 0;
194
}
195

    
196
function interfaces_optional_configure() {
197
	global $config, $g;
198
	global $bridgeconfig;
199

    
200
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
201
		interfaces_optional_configure_if($i);
202
	}
203

    
204
	if (!$g['booting']) {
205
		/* reconfigure static routes (kernel may have deleted them) */
206
		system_routing_configure();
207

    
208
		/* reload IPsec tunnels */
209
		vpn_ipsec_configure();
210

    
211
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
212
		services_dhcpd_configure();
213

    
214
		/* restart dnsmasq */
215
		services_dnsmasq_configure();
216

    
217
		/* set the reload filter dity flag */
218
		touch("{$g['tmp_path']}/filter_dirty");				
219
	}
220

    
221
	return 0;
222
}
223

    
224
function interfaces_optional_configure_if($opti) {
225
	global $config, $g;
226
	global $bridgeconfig, $debugging;
227

    
228
	$bridges_total = get_next_available_bridge_interface();
229

    
230
	$optcfg = $config['interfaces']['opt' . $opti];
231

    
232
	if ($g['booting']) {
233
		$optdescr = "";
234
		if ($optcfg['descr'])
235
			$optdescr = " ({$optcfg['descr']})";
236
		print "\tOPT{$opti}{$optdescr}... ";
237
	}
238

    
239
	if (isset($optcfg['enable'])) {
240
		/* wireless configuration? */
241
		if (is_array($optcfg['wireless']))
242
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
243

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

    
264
		/* media */
265
		if ($optcfg['media'] || $optcfg['mediaopt']) {
266
			$cmd = "/sbin/ifconfig " . escapeshellarg($optcfg['if']);
267
			if ($optcfg['media'])
268
				$cmd .= " media " . escapeshellarg($optcfg['media']);
269
			if ($optcfg['mediaopt'])
270
				$cmd .= " mediaopt " . escapeshellarg($optcfg['mediaopt']);
271
			mwexec($cmd);
272
		}
273

    
274
		/* OpenVPN configuration? */
275
 		if (isset($optcfg['ovpn'])) {
276
 			if (strstr($optcfg['if'], "tap"))
277
 				ovpn_link_tap();
278
 		}
279

    
280
		/* bridged? */
281
		if ($optcfg['bridge']) {
282
			mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete up");
283
                        /* use open/netBSD style bridge */
284
			mwexec("/sbin/ifconfig bridge{$bridges_total} create");
285
			
286
			/* force all bridged interfaces to use same mtu */
287
			$mtu = get_interface_mtu($config['interfaces'][$optcfg['bridge']]['if']);
288
			mwexec("/sbin/ifconfig {$optcfg['if']} mtu {$mtu}");
289
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}");			
290

    
291
			/* assign items to a bridge */
292
                        mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up");
293
			mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} stp {$config['interfaces'][$optcfg['bridge']]['if']}");
294

    
295
			/* log commands run for debugging in /tmp/ */
296
			$fd = fopen("{$g['tmp_path']}/bridge_config_{$optcfg['if']}", "w");
297
			fwrite($fd, "/sbin/ifconfig {$optcfg['if']} mtu {$mtu}\n");
298
			fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} mtu {$mtu}\n");
299
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
300
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$optcfg['if']} addm {$config['interfaces'][$optcfg['bridge']]['if']} up\n");
301
			fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$optcfg['if']} stp {$config['interfaces'][$optcfg['bridge']]['if']}\n");
302
			fclose($fd);
303
			
304
			/* bring up interfaces */
305
			mwexec("/sbin/ifconfig {$config['interfaces'][$optcfg['bridge']]['if']} up");
306
			mwexec("/sbin/ifconfig {$optcfg['if']} up");
307

    
308
			
309
		} else {
310
			/* if user has selected DHCP type then act accordingly */
311
			if($optcfg['ipaddr'] == "dhcp") {
312
				interfaces_opt_dhcp_configure("opt{$opti}");
313
			} else {			
314
				mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " " .
315
				escapeshellarg($optcfg['ipaddr'] . "/" . $optcfg['subnet']));
316
			}
317
		}
318
	} else {
319
		mwexec("/sbin/ifconfig " . escapeshellarg($optcfg['if']) . " delete down");
320
	}
321
	return 0;
322
}
323

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

    
425
function interfaces_carp_bring_up_final() {
426
	global $config, $g, $debugging;
427
	if(isset($config['system']['developerspew'])) {
428
		$mt = microtime();
429
		echo "interfaces_carp_bring_up_final() being called $mt\n";
430
	}
431
	$viparr = &$config['virtualip']['vip'];
432
	/* could not locate an array, return */
433
	if(!is_array($viparr)) 
434
		return;
435
	$carp_instances_counter = 0;
436
	$counter = 0;
437
	$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
438
	while($supress > 0) {
439
		sleep(2);
440
		$supress = intval(`/sbin/sysctl net.inet.carp.suppress_preempt | cut -d" " -f2`);
441
		if($counter > 15)
442
			$supress = 0;
443
		$counter++;
444
	}	
445
	sleep(45);
446
	foreach ($viparr as $vip) {
447
		if($debugging)
448
			echo "Upping interface carp{$carp_instances_counter}.\n";
449
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " create");
450
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
451
		if($vip['password'] != "") 
452
			$password = " pass " . $vip['password'];
453
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
454
		if($debugging)
455
			echo "/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password . "\n";
456
		mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . "{$carpdev} advskew " . $vip['advskew'] . $password);
457
		$carp_instances_counter++;
458
	}
459
}
460

    
461
function interfaces_wireless_configure($if, $wlcfg) {
462
	global $config, $g;
463
	
464
	/* set values for /path/program */
465
	$hostapd = "/usr/sbin/hostapd";
466
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
467
	$ifconfig = "/sbin/ifconfig ";
468
	$killall = "/usr/bin/killall ";
469

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

    
472
	/* Set a/b/g standard */
473
	$standard = ("mode " . escapeshellarg($wlcfg['standard']));
474

    
475
	/* set wireless channel value */
476
	$channel = escapeshellarg($wlcfg['channel']);
477
	
478
	if($channel == "") {
479
		$channel = "";
480
	} else { 
481
		$channel = ("channel " . escapeshellarg($wlcfg['channel']));
482
	}
483

    
484
	/* Set ssid */
485
	$ssid = ("ssid " . escapeshellarg($wlcfg['ssid']));
486

    
487
	/* Set stationname */
488
	if (!$wlcfg['stationname'])
489
		$stationname = "pfsense";
490
	else
491
		$stationname = ("stationname " . escapeshellarg($wlcfg['stationname']));
492

    
493
	/* Set wireless hostap mode */
494
	if ($wlcfg['mode'] == hostap)
495
		$hostapmode = "mediaopt hostap";
496
	else
497
		$hostapmode = "-mediaopt hostap";
498

    
499
	/* Set wireless adhoc mode */
500
	if ($wlcfg['mode'] == adhoc)
501
		$adhocmode = "mediaopt adhoc";
502
	else
503
		$adhocmode = "-mediaopt adhoc";
504

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

    
507
	/* handle hide ssid option */
508
	if(isset($wlcfg['hidessid']['enable']))
509
		$hidessid = "hidessid";
510
	else
511
		$hidessid = "-hidessid";
512

    
513
	/* handle pureg (802.11g) only option */
514
	if(isset($wlcfg['pureg']['enable']))
515
		$pureg = "mode 11g pureg";
516
	else
517
		$pureg = "-pureg";
518

    
519
	/* enable apbridge option */
520
	if(isset($wlcfg['apbridge']['enable']))
521
		$apbridge = "apbridge";
522
	else
523
		$apbridge = "-apbridge";
524

    
525
	/* handle turbo option */
526
	if(isset($wlcfg['turbo']['enable']))
527
		$turbo = "mediaopt turbo";
528
	else
529
		$turbo = "-mediaopt turbo";
530

    
531
	/* handle txpower setting */
532
	if($wlcfg['txpower'] <> "")
533
		$txpower = ("txpower " . escapeshellarg($wlcfg['txpower']));
534
	
535
	/* handle wme option */
536
	if(isset($wlcfg['wme']['enable']))
537
		$wme = "wme";
538
	else
539
		$wme = "-wme";
540
	
541
	/* set up wep if enabled */
542
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
543
                $wepset .= "authmode shared wepmode on ";
544

    
545
                $i = 1;
546
                foreach ($wlcfg['wep']['key'] as $wepkey) {
547
                        $wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
548
                        if (isset($wepkey['txkey'])) {
549
                                $wepset .= "weptxkey {$i} ";
550
                        }
551
                        $i++;
552
                }
553
        } else {
554
                $wepset = "authmode open wepmode off";
555
	}
556

    
557
	/* generate wpa_supplicant/hostap config if wpa is enabled */
558

    
559
	switch ($wlcfg['mode']) {
560
		case 'BSS':
561
			if (isset($wlcfg['wpa']['enable'])) {
562

    
563
				$wpa .= <<<EOD
564
ctrl_interface={$g['varrun_path']}/hostapd
565
ctrl_interface_group=0
566
ap_scan=1
567
#fast_reauth=1
568
network={
569
ssid={$wlcfg['ssid']}
570
scan_ssid=2
571
priority=5
572
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
573
psk={$wlcfg['wpa']['passphrase']}
574
pairwise={$wlcfg['wpa']['wpa_pairwise']}
575
group={$wlcfg['wpa']['wpa_pairwise']}
576
}
577
EOD;
578

    
579
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
580
				fwrite($fd, "{$wpa}");
581
				fclose($fd);
582

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

    
588
		case 'hostap':
589
			if (isset($wlcfg['wpa']['enable'])) {
590
				$wpa .= <<<EOD
591
interface={$if}
592
driver=bsd
593
logger_syslog=-1
594
logger_syslog_level=0
595
logger_stdout=-1
596
logger_stdout_level=0
597
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
598
ctrl_interface={$g['varrun_path']}/hostapd
599
ctrl_interface_group=wheel
600
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
601
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
602
ssid={$wlcfg['ssid']}
603
debug={$wlcfg['wpa']['debug_mode']}
604
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
605
auth_algs={$wlcfg['wpa']['auth_algs']}
606
wpa={$wlcfg['wpa']['wpa_mode']}
607
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
608
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
609
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
610
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
611
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
612
wpa_passphrase={$wlcfg['wpa']['passphrase']}
613
ieee8021x={$wlcfg['wpa']['ieee8021x']}
614
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
615
#rsn_preauth=1
616
#rsn_preauth_interfaces=eth0
617
EOD;
618

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

    
623
				if(is_process_running("hostapd"))
624
					mwexec("$killall  hostapd");
625
			}
626
		break;
627

    
628
		case 'adhoc':
629
			if(is_process_running("hostapd"))
630
				mwexec("$killall hostapd");
631

    
632
			if(is_process_running("wpa_supplicant"))
633
				mwexec("$killall wpa_supplicant");
634
		break;
635
	}	
636

    
637
	/* start up everything */
638
	
639
	mwexec("$ifconfig $if" . " -mediaopt hostap,turbo");	// Fix bug with turbomode and reboot (hopefully)        
640
	mwexec("$ifconfig $if" . " down"); 
641
	mwexec("$ifconfig $if" . " " . $standard);
642
	mwexec("$ifconfig $if" . " " . $channel);
643
	mwexec("$ifconfig $if" . " " . $ssid);
644
	mwexec("$ifconfig $if" . " " . $stationname);
645
//	mwexec("$ifconfig $if" . " " . $hostapmode);
646
	mwexec("$ifconfig $if" . " " . $adhocmode);
647
	/* fix turbo mode and reboot */
648
	mwexec("$ifconfig $if" . " up"); 
649
	mwexec("$ifconfig $if" . " " . $hostapmode);
650
	mwexec("$ifconfig $if" . " " . $turbo);
651
	mwexec("$ifconfig $if" . " down"); 
652
	/* fix turbo mode and reboot */
653
	mwexec("$ifconfig $if" . " " . $hidessid);
654
	mwexec("$ifconfig $if" . " " . $pureg);
655
	mwexec("$ifconfig $if" . " " . $apbridge);
656
//	mwexec("$ifconfig $if" . " " . $turbo);
657
	mwexec("$ifconfig $if" . " " . $wme);
658
	mwexec("$ifconfig $if" . " " . $wepset);
659
	mwexec("$ifconfig $if" . " up");
660

    
661

    
662
	if (isset($wlcfg['wpa']['enable'])) {
663
		if ($wlcfg['mode'] == BSS) 
664
			mwexec("$wpa_supplicant -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf");
665
		if ($wlcfg['mode'] == hostap) 
666
			mwexec("$hostapd -B {$g['varetc_path']}/hostapd_{$if}.conf");
667
	}
668

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

    
675
	/* Write wep crap out */
676
//	$fd = fopen("{$g['tmp_path']}/ifconfig_wep", "w");
677
//	fwrite($fd, "sbin/ifconfig {$wepset}");
678
//	fclose($fd);
679
	
680
	if(isset($wlcfg['useolsr']))
681
		setup_wireless_olsr($if);
682

    
683
	return 0;
684

    
685
}
686

    
687
function find_dhclient_process($interface) {
688
	if(filter_translate_type_to_real_interface($interface) <> "")
689
        	$realinterface = filter_translate_type_to_real_interface($interface);
690
	$pid = `ps ax | grep "[d]hclient" | grep {$realinterface} | awk -F" " '{print $1}'`;
691
	return $pid;
692
}
693

    
694
function interfaces_wan_configure() {
695
	global $config, $g;
696

    
697
	$wancfg = $config['interfaces']['wan'];
698

    
699
	if(!$g['booting']) {
700
		mute_kernel_msgs();
701

    
702
		/* find dhclient process for wan and kill it */
703
		killbypid(find_dhclient_process("wan"));
704

    
705
		/* kill PPPoE client (mpd) */
706
		killbypid("{$g['varrun_path']}/mpd.pid");
707

    
708
		/* wait for processes to die */
709
		sleep(1);
710

    
711
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
712
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
713
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
714
		unlink_if_exists("{$g['vardb_path']}/wanip");
715
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
716
	}
717

    
718
	/* remove all addresses first */
719
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
720
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
721

    
722
	/* wireless configuration? */
723
	if (is_array($wancfg['wireless']))
724
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
725

    
726
	if ($wancfg['spoofmac']) {
727
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
728
			" link " . escapeshellarg($wancfg['spoofmac']));
729
	}  else {
730
		$mac = get_interface_mac_address($wancfg['if']);
731
		if($mac == "ff:ff:ff:ff:ff:ff") {
732
			/*   this is not a valid mac address.  generate a
733
			 *   temporary mac address so the machine can get online.
734
			 */
735
			echo "Generating new MAC address.";
736
			$random_mac = generate_random_mac_address();
737
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
738
				" link " . escapeshellarg($random_mac));
739
			$wancfg['spoofmac'] = $random_mac;
740
			write_config();
741
			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");
742
		}
743
	}
744

    
745
	/* media */
746
	if ($wancfg['media'] || $wancfg['mediaopt']) {
747
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
748
		if ($wancfg['media'])
749
			$cmd .= " media " . escapeshellarg($wancfg['media']);
750
		if ($wancfg['mediaopt'])
751
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
752
		mwexec($cmd);
753
	}
754

    
755
	switch ($wancfg['ipaddr']) {
756

    
757
		case 'dhcp':
758
			interfaces_wan_dhcp_configure();
759
			break;
760

    
761
		case 'pppoe':
762
			interfaces_wan_pppoe_configure();
763
			break;
764

    
765
		case 'pptp':
766
			interfaces_wan_pptp_configure();
767
			break;
768

    
769
		case 'bigpond':
770
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
771
			interfaces_wan_dhcp_configure();
772
			break;
773

    
774
		default:
775
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
776
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
777
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
778
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
779
			} else {
780
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
781
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
782
			}
783
			/* install default route */
784
			mwexec("/sbin/route delete default");
785
			mwexec("/sbin/route add default " . escapeshellarg($config['interfaces']['wan']['gateway']));
786

    
787
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
788
			filter_configure();
789
	}
790

    
791
	if (!$g['booting']) {
792
		/* reconfigure static routes (kernel may have deleted them) */
793
		system_routing_configure();
794

    
795
		/* set the reload filter dity flag */
796
		touch("{$g['tmp_path']}/filter_dirty");
797

    
798
		/* reload ipsec tunnels */
799
		vpn_ipsec_configure();
800

    
801
		/* restart ez-ipupdate */
802
		services_dyndns_configure();
803

    
804
		/* force DNS update */
805
		services_dnsupdate_process();
806

    
807
		/* restart dnsmasq */
808
		services_dnsmasq_configure();
809
	}
810

    
811
	unmute_kernel_msgs();
812

    
813
	return 0;
814
}
815

    
816
function interfaces_opt_dhcp_configure($interface) {
817
	global $config, $g;
818

    
819
	$optcfg = $config['interfaces'][$interface];
820
	$optif = $optcfg['if'];
821

    
822
	/* generate dhclient_wan.conf */
823
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
824
	if (!$fd) {
825
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
826
		return 1;
827
	}
828

    
829
	if ($optcfg['dhcphostname']) {
830
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
831
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
832
	} else {
833
		$dhclientconf_hostname = "";
834
	}
835

    
836
 	$dhclientconf = "";
837

    
838
	$dhclientconf .= <<<EOD
839
interface "{$optif}" {
840
	send host-name "{$optcfg['dhcphostname']}";
841
	script "/sbin/dhclient-script";
842
	{$dhclientconf_hostname}
843
}
844

    
845
EOD;
846

    
847
	fwrite($fd, $dhclientconf);
848
	fclose($fd);
849

    
850
        /* bring interface up before starting dhclient */
851
        mwexec("/sbin/ifconfig {$optif} up");
852

    
853
        /* fire up dhclient */
854
        mwexec("/sbin/dhclient -cf {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
855

    
856
	return 0;
857
}
858

    
859
function interfaces_dhcp_configure($interface) {
860
	global $config, $g;
861

    
862
	if(filter_translate_type_to_real_interface($interface) <> "")
863
        	$realinterface = filter_translate_type_to_real_interface($interface);
864

    
865
	$optcfg = $config['interfaces'][$interface];
866

    
867
	/* generate dhclient_$interface.conf */
868
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
869
	if (!$fd) {
870
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
871
		return 1;
872
	}
873

    
874
	if ($optcfg['dhcphostname']) {
875
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
876
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
877
	} else {
878
		$dhclientconf_hostname = "";
879
	}
880

    
881
 	$dhclientconf = "";
882

    
883
	$dhclientconf .= <<<EOD
884
interface "{$realinterface}" {
885
	script "/sbin/dhclient-script";
886
	{$dhclientconf_hostname}
887
}
888

    
889
EOD;
890

    
891
	fwrite($fd, $dhclientconf);
892
	fclose($fd);
893
	
894
	$optif = $optcfg['if'];
895
	
896
        /* bring wan interface up before starting dhclient */
897
        mwexec("/sbin/ifconfig {$optif} up");
898

    
899
        /* fire up dhclient */
900
        mwexec("/sbin/dhclient -cf {$g['varetc_path']}/dhclient_{$optif}.conf {$optif} >/tmp/{$optif}_output >/tmp/{$optif}_error_output");
901

    
902
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
903
	fwrite($fout, "/sbin/dhclient -cf {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
904
	fclose($fout);
905

    
906
	return 0;
907
}
908

    
909
function interfaces_wan_dhcp_configure() {
910
	global $config, $g;
911

    
912
	$wancfg = $config['interfaces']['wan'];
913

    
914
	/* generate dhclient_wan.conf */
915
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
916
	if (!$fd) {
917
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
918
		return 1;
919
	}
920
	
921
	if ($wancfg['dhcphostname']) {
922
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
923
		$dhclientconf_hostname = "	send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
924
	} else {
925
		$dhclientconf_hostname = "";
926
	}
927

    
928
 	$dhclientconf = "";
929

    
930
	$dhclientconf .= <<<EOD
931
interface "{$wancfg['if']}" {
932
	script "/sbin/dhclient-script";
933
	{$dhclientconf_hostname}
934
}
935

    
936
EOD;
937

    
938
	fwrite($fd, $dhclientconf);
939
	fclose($fd);
940
	
941
	$wanif = $wancfg['if'];
942
	
943
        /* bring wan interface up before starting dhclient */
944
        mwexec("/sbin/ifconfig {$wanif} up");
945

    
946
        /* fire up dhclient */
947
        mwexec("/sbin/dhclient -cf {$g['varetc_path']}/dhclient_wan.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
948

    
949
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
950
	fwrite($fout, "/sbin/dhclient -cf {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
951
	fclose($fout);
952

    
953
	return 0;
954
}
955

    
956
function interfaces_wan_dhcp_down() {
957
	global $config;
958
	$wancfg = $config['interfaces']['wan'];
959
	$wanif = $wancfg['if'];
960
	mwexec("/sbin/ifconfig {$wanif} delete");
961
	sleep(1);
962
}
963

    
964
function interfaces_dhcp_down($interface) {
965
	global $config;
966
	if(filter_translate_type_to_real_interface($interface) <> "")
967
		$realinterface = filter_translate_type_to_real_interface($interface);
968
	mwexec("/sbin/ifconfig {$realinterface} down");
969
	sleep(1);
970
	$pid = find_dhclient_process($interface);
971
	if($pid)
972
		mwexec("kill {$pid}");
973
}
974

    
975
function interfaces_dhcp_up($interface) {
976
	interfaces_dhcp_configure($interface);
977
	sleep(1);
978
}
979

    
980
function interfaces_wan_dhcp_up() {
981
	interfaces_wan_dhcp_configure();
982
	sleep(1);
983
}
984

    
985
function interfaces_wan_pppoe_configure() {
986
	global $config, $g;
987

    
988
	$wancfg = $config['interfaces']['wan'];
989
	$pppoecfg = $config['pppoe'];
990

    
991
	/* generate mpd.conf */
992
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
993
	if (!$fd) {
994
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
995
		return 1;
996
	}
997

    
998
	$idle = 0;
999

    
1000
	if (isset($pppoecfg['ondemand'])) {
1001
		$ondemand = "enable";
1002
		if ($pppoecfg['timeout'])
1003
			$idle = $pppoecfg['timeout'];
1004
	} else {
1005
		$ondemand = "disable";
1006
	}
1007

    
1008
	$mpdconf = <<<EOD
1009
pppoe:
1010
	new -i ng0 pppoe pppoe
1011
	set iface route default
1012
	set iface {$ondemand} on-demand
1013
	set iface idle {$idle}
1014
	set iface up-script /usr/local/sbin/ppp-linkup
1015

    
1016
EOD;
1017

    
1018
	/*    Check for ppp-linkdown Script in /usr/local/sbin
1019
	 *    Create reference in mpd.conf
1020
	 */
1021
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1022
		$mpdconf .= <<<EOD
1023
	set iface down-script /usr/local/sbin/ppp-linkdown
1024

    
1025
EOD;
1026
	}
1027

    
1028
	if (isset($pppoecfg['ondemand'])) {
1029
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
1030
			$mpdconf .= <<<EOD
1031
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
1032

    
1033
EOD;
1034
		} else {
1035
			$mpdconf .= <<<EOD
1036
	set iface addrs 192.0.2.112 192.0.2.113
1037

    
1038
EOD;
1039
		}
1040
	}
1041

    
1042
	$mpdconf .= <<<EOD
1043
	set bundle disable multilink
1044
	set bundle authname "{$pppoecfg['username']}"
1045
	set bundle password "{$pppoecfg['password']}"
1046
	set link keep-alive 10 60
1047
	set link max-redial 0
1048
	set link no acfcomp protocomp
1049
	set link disable pap chap
1050
	set link accept chap
1051
	set link mtu 1492
1052
	set ipcp yes vjcomp
1053
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1054

    
1055
EOD;
1056

    
1057
	if (isset($config['system']['dnsallowoverride'])) {
1058
		$mpdconf .= <<<EOD
1059
	set ipcp enable req-pri-dns
1060

    
1061
EOD;
1062
	}
1063

    
1064
	$mpdconf .= <<<EOD
1065
	open iface
1066

    
1067
EOD;
1068

    
1069
	fwrite($fd, $mpdconf);
1070
	fclose($fd);
1071

    
1072
	/* generate mpd.links */
1073
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1074
	if (!$fd) {
1075
		printf("Error: cannot open mpd.links in interfaces_wan_pppoe_configure().\n");
1076
		return 1;
1077
	}
1078

    
1079
	$mpdconf = <<<EOD
1080
pppoe:
1081
	set link type pppoe
1082
	set pppoe iface {$wancfg['if']}
1083
	set pppoe service "{$pppoecfg['provider']}"
1084
	set pppoe enable originate
1085
	set pppoe disable incoming
1086

    
1087
EOD;
1088

    
1089
	fwrite($fd, $mpdconf);
1090
	fclose($fd);
1091

    
1092
	/* if mpd is active, lets take it down */
1093
	if(file_exists("{$g['varrun_path']}/mpd.pid")) {
1094
		killbypid(file_get_contents("{$g['varrun_path']}/mpd.pid"));
1095
		sleep(1);
1096
	}
1097

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

    
1101
        /* sleep until wan is up - or 30 seconds, whichever comes first */
1102
	for ($count = 0; $count < 30; $count++) {
1103
		if(file_exists("{$g['tmp_path']}/wanup")) {
1104
			break;
1105
		}
1106
	
1107
		sleep(1);
1108
	}
1109
	unlink_if_exists("{$g['tmp_path']}/wanup");
1110

    
1111
	return 0;
1112
}
1113

    
1114
function interfaces_wan_pppoe_down() {
1115
	global $g;
1116
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1117
	sleep(1);
1118
}
1119

    
1120
function interfaces_wan_pppoe_up() {
1121
	global $g;
1122
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1123
	sleep(1);
1124
}
1125

    
1126
function interfaces_wan_pptp_configure() {
1127
	global $config, $g;
1128

    
1129
	$wancfg = $config['interfaces']['wan'];
1130
	$pptpcfg = $config['pptp'];
1131

    
1132
	/* generate mpd.conf */
1133
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1134
	if (!$fd) {
1135
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1136
		return 1;
1137
	}
1138

    
1139
	$idle = 0;
1140

    
1141
	if (isset($pptpcfg['ondemand'])) {
1142
		$ondemand = "enable";
1143
		if ($pptpcfg['timeout'])
1144
			$idle = $pptpcfg['timeout'];
1145
	} else {
1146
		$ondemand = "disable";
1147
	}
1148

    
1149
	$mpdconf = <<<EOD
1150
pptp:
1151
	new -i ng0 pptp pptp
1152
	set iface route default
1153
	set iface {$ondemand} on-demand
1154
	set iface idle {$idle}
1155
	set iface up-script /usr/local/sbin/ppp-linkup
1156

    
1157
EOD;
1158

    
1159
	/*   Check for ppp-linkdown Script in /usr/local/sbin
1160
	 *   Create reference in mpd.conf
1161
	 */
1162
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1163
		$mpdconf .= <<<EOD
1164
	set iface down-script /usr/local/sbin/ppp-linkdown
1165

    
1166
EOD;
1167
	}
1168

    
1169
	if (isset($pptpcfg['ondemand'])) {
1170
		$mpdconf .= <<<EOD
1171
	set iface addrs 10.0.0.1 10.0.0.2
1172

    
1173
EOD;
1174
	}
1175

    
1176
	$mpdconf .= <<<EOD
1177
	set bundle disable multilink
1178
	set bundle authname "{$pptpcfg['username']}"
1179
	set bundle password "{$pptpcfg['password']}"
1180
	set link keep-alive 10 60
1181
	set link max-redial 0
1182
	set link no acfcomp protocomp
1183
	set link disable pap chap
1184
	set link accept chap
1185
	set ipcp no vjcomp
1186
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1187

    
1188
EOD;
1189

    
1190
	if (isset($config['system']['dnsallowoverride'])) {
1191
		$mpdconf .= <<<EOD
1192
	set ipcp enable req-pri-dns
1193

    
1194
EOD;
1195
	}
1196

    
1197
	$mpdconf .= <<<EOD
1198
	open
1199

    
1200
EOD;
1201

    
1202
	fwrite($fd, $mpdconf);
1203
	fclose($fd);
1204

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

    
1212
	$mpdconf = <<<EOD
1213
pptp:
1214
	set link type pptp
1215
	set pptp enable originate outcall
1216
	set pptp disable windowing
1217
	set pptp self {$pptpcfg['local']}
1218
	set pptp peer {$pptpcfg['remote']}
1219

    
1220
EOD;
1221

    
1222
	fwrite($fd, $mpdconf);
1223
	fclose($fd);
1224

    
1225
	/* configure interface */
1226
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1227
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1228

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

    
1232
	return 0;
1233
}
1234

    
1235
function interfaces_wan_pptp_down() {
1236
	global $g;
1237
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1238
	sleep(1);
1239
}
1240

    
1241
function interfaces_wan_pptp_up() {
1242
	global $g;
1243
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1244
	sleep(1);
1245
}
1246

    
1247
function interfaces_wan_bigpond_configure($curwanip) {
1248
	global $config, $g;
1249

    
1250
	$bpcfg = $config['bigpond'];
1251

    
1252
	if (!$curwanip) {
1253
		/* IP address not configured yet, exit */
1254
		return 0;
1255
	}
1256

    
1257
	/* kill bpalogin */
1258
	killbyname("bpalogin");
1259

    
1260
	/* wait a moment */
1261
	sleep(1);
1262

    
1263
	/* get the default domain */
1264
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1265
	if ($nfd) {
1266
		$defaultdomain = trim(fgets($nfd));
1267
		fclose($nfd);
1268
	}
1269

    
1270
	/* generate bpalogin.conf */
1271
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1272
	if (!$fd) {
1273
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1274
		return 1;
1275
	}
1276

    
1277
	if (!$bpcfg['authserver'])
1278
		$bpcfg['authserver'] = "dce-server";
1279
	if (!$bpcfg['authdomain'])
1280
		$bpcfg['authdomain'] = $defaultdomain;
1281

    
1282
	$bpconf = <<<EOD
1283
username {$bpcfg['username']}
1284
password {$bpcfg['password']}
1285
authserver {$bpcfg['authserver']}
1286
authdomain {$bpcfg['authdomain']}
1287
localport 5050
1288

    
1289
EOD;
1290

    
1291
	if ($bpcfg['minheartbeatinterval'])
1292
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1293

    
1294
	fwrite($fd, $bpconf);
1295
	fclose($fd);
1296

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

    
1300
	return 0;
1301
}
1302

    
1303
function get_real_wan_interface() {
1304
	global $config, $g;
1305

    
1306
	$wancfg = $config['interfaces']['wan'];
1307

    
1308
	$wanif = $wancfg['if'];
1309
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1310
		$wanif = $g['pppoe_interface'];
1311
	}
1312

    
1313
	return $wanif;
1314
}
1315

    
1316
function get_current_wan_address($interface = "wan") {
1317
	global $config, $g;
1318

    
1319
	$wancfg = $config['interfaces'][$interface];
1320

    
1321
	$interface = filter_translate_type_to_real_interface($interface);
1322

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

    
1327
		if (isset($ifinfo[1])) {
1328
			$aif = preg_split("/\s+/", $ifinfo[1]);
1329
			$curwanip = chop($aif[3]);
1330

    
1331
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1332
				return $curwanip;
1333
		}
1334

    
1335
		return null;		
1336
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1337
		/* dynamic WAN IP address, find out which one */
1338
		$wanif = get_real_wan_interface();
1339

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

    
1343
		if (isset($ifinfo[1])) {
1344
			$aif = preg_split("/\s+/", $ifinfo[1]);
1345
			$curwanip = chop($aif[3]);
1346

    
1347
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1348
				return $curwanip;
1349
		}
1350

    
1351
		return null;
1352
	} else {
1353
		/* static WAN IP address */
1354
		return $wancfg['ipaddr'];
1355
	}
1356
}
1357

    
1358
/****f* interfaces/is_altq_capable
1359
 * NAME
1360
 *   is_altq_capable - Test if interface is capable of using ALTQ
1361
 * INPUTS
1362
 *   $int            - string containing interface name
1363
 * RESULT
1364
 *   boolean         - true or false
1365
 ******/
1366

    
1367
function is_altq_capable($int) {
1368
        /* Per:
1369
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1370
         * Only the following drivers have ALTQ support
1371
         */
1372
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1373
		"em", "fxp", "hme", "lnc", "ndis", "rl", "sf", "sis", "sk",
1374
		"tun", "vr", "wi", "xl", "vlan", "ste");
1375

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

    
1378
        if (in_array($int_family[0], $capable))
1379
                return true;
1380
        else
1381
                return false;
1382
}
1383

    
1384
function get_number_of_bridged_interfaces() {
1385
	$bridges_total = 0;
1386
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1387
	foreach($bridges as $bridge) {
1388
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1389
		if($match_array[1][0] <> "") {
1390
			if($match_array[1][0] > $bridges_total)
1391
				$bridges_total = $match_array[1][0];
1392
		}
1393
	}
1394
	return "{$bridges_total}";
1395
}
1396

    
1397
function get_next_available_bridge_interface() {
1398
	$bridges_total = get_number_of_bridged_interfaces();
1399
	$interfaces = `/sbin/ifconfig -l`;
1400
	$x=0;
1401
	for($x=0; $x<$bridges_total; $x++) {
1402
		if(!stristr($interfaces, "bridge{$x}")) {
1403
			return "{$x}";
1404
		}
1405
	}
1406
	return "{$x}";
1407
}
1408

    
1409
function destroy_bridge($bridge_num) {
1410
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1411
	sleep(1);
1412
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1413
	sleep(1);
1414
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1415
	sleep(1);
1416
	return;
1417
}
1418

    
1419
function discover_bridge($interface1, $interface2) {
1420
	if(!$interface1) return;
1421
	if(!$interface2) return;
1422
	$total_bridges = get_number_of_bridged_interfaces();
1423
	$interfaces = `/sbin/ifconfig -l`;
1424
	$x=0;
1425
	for($x=0; $x<$total_bridges; $x++) {
1426
		$bridge_text = "NA";
1427
		if(!stristr($interfaces, "bridge{$x}")) 
1428
			continue;
1429
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1430
		if(stristr($bridge_text, $interface1) == true and
1431
		   stristr($bridge_text, $interface2) == true) {
1432
			return "{$x}";			
1433
		}
1434
	}
1435
	return "-1";
1436
}
1437

    
1438
?>
(9-9/25)