Project

General

Profile

Download (40.7 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
			if($g['booting']) 
412
				mwexec("/sbin/ifconfig carp" . $carp_instances_counter . " up");
413
			$carp_instances_counter++;
414
		}
415
	}
416
	mwexec("/bin/sh /tmp/carp.sh");
417
	fclose($fd);
418
	if ($g['booting']) {
419
		unmute_kernel_msgs();
420
		echo "done.\n";
421
	}
422
}
423

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
555
	/* generate wpa_supplicant/hostap config if wpa is enabled */
556

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

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

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

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

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

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

    
621
				if(is_process_running("hostapd"))
622
					mwexec("$killall  hostapd");
623
			}
624
		break;
625

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

    
630
			if(is_process_running("wpa_supplicant"))
631
				mwexec("$killall wpa_supplicant");
632
		break;
633
	}	
634

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

    
659

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

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

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

    
681
	return 0;
682

    
683
}
684

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

    
692
function interfaces_wan_configure() {
693
	global $config, $g;
694

    
695
	$wancfg = $config['interfaces']['wan'];
696

    
697
	if(!$g['booting']) {
698
		mute_kernel_msgs();
699

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

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

    
706
		/* wait for processes to die */
707
		sleep(1);
708

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

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

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

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

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

    
753
	switch ($wancfg['ipaddr']) {
754

    
755
		case 'dhcp':
756
			interfaces_wan_dhcp_configure();
757
			break;
758

    
759
		case 'pppoe':
760
			interfaces_wan_pppoe_configure();
761
			break;
762

    
763
		case 'pptp':
764
			interfaces_wan_pptp_configure();
765
			break;
766

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

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

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

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

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

    
796
		/* reload ipsec tunnels */
797
		vpn_ipsec_configure();
798

    
799
		/* restart ez-ipupdate */
800
		services_dyndns_configure();
801

    
802
		/* force DNS update */
803
		services_dnsupdate_process();
804

    
805
		/* restart dnsmasq */
806
		services_dnsmasq_configure();
807
	}
808

    
809
	unmute_kernel_msgs();
810

    
811
	return 0;
812
}
813

    
814
function interfaces_opt_dhcp_configure($interface) {
815
	global $config, $g;
816

    
817
	$optcfg = $config['interfaces'][$interface];
818
	$optif = $optcfg['if'];
819

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

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

    
834
 	$dhclientconf = "";
835

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

    
843
EOD;
844

    
845
	fwrite($fd, $dhclientconf);
846
	fclose($fd);
847

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

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

    
854
	return 0;
855
}
856

    
857
function interfaces_dhcp_configure($interface) {
858
	global $config, $g;
859

    
860
	if(filter_translate_type_to_real_interface($interface) <> "")
861
        	$realinterface = filter_translate_type_to_real_interface($interface);
862

    
863
	$optcfg = $config['interfaces'][$interface];
864

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

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

    
879
 	$dhclientconf = "";
880

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

    
887
EOD;
888

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

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

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

    
904
	return 0;
905
}
906

    
907
function interfaces_wan_dhcp_configure() {
908
	global $config, $g;
909

    
910
	$wancfg = $config['interfaces']['wan'];
911

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

    
926
 	$dhclientconf = "";
927

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

    
934
EOD;
935

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

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

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

    
951
	return 0;
952
}
953

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

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

    
973
function interfaces_dhcp_up($interface) {
974
	interfaces_dhcp_configure($interface);
975
	sleep(1);
976
}
977

    
978
function interfaces_wan_dhcp_up() {
979
	interfaces_wan_dhcp_configure();
980
	sleep(1);
981
}
982

    
983
function interfaces_wan_pppoe_configure() {
984
	global $config, $g;
985

    
986
	$wancfg = $config['interfaces']['wan'];
987
	$pppoecfg = $config['pppoe'];
988

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

    
996
	$idle = 0;
997

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

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

    
1014
EOD;
1015

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

    
1023
EOD;
1024
	}
1025

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

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

    
1036
EOD;
1037
		}
1038
	}
1039

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

    
1053
EOD;
1054

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

    
1059
EOD;
1060
	}
1061

    
1062
	$mpdconf .= <<<EOD
1063
	open iface
1064

    
1065
EOD;
1066

    
1067
	fwrite($fd, $mpdconf);
1068
	fclose($fd);
1069

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

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

    
1085
EOD;
1086

    
1087
	fwrite($fd, $mpdconf);
1088
	fclose($fd);
1089

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

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

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

    
1109
	return 0;
1110
}
1111

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

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

    
1124
function interfaces_wan_pptp_configure() {
1125
	global $config, $g;
1126

    
1127
	$wancfg = $config['interfaces']['wan'];
1128
	$pptpcfg = $config['pptp'];
1129

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

    
1137
	$idle = 0;
1138

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

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

    
1155
EOD;
1156

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

    
1164
EOD;
1165
	}
1166

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

    
1171
EOD;
1172
	}
1173

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

    
1186
EOD;
1187

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

    
1192
EOD;
1193
	}
1194

    
1195
	$mpdconf .= <<<EOD
1196
	open
1197

    
1198
EOD;
1199

    
1200
	fwrite($fd, $mpdconf);
1201
	fclose($fd);
1202

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

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

    
1218
EOD;
1219

    
1220
	fwrite($fd, $mpdconf);
1221
	fclose($fd);
1222

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

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

    
1230
	return 0;
1231
}
1232

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

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

    
1245
function interfaces_wan_bigpond_configure($curwanip) {
1246
	global $config, $g;
1247

    
1248
	$bpcfg = $config['bigpond'];
1249

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

    
1255
	/* kill bpalogin */
1256
	killbyname("bpalogin");
1257

    
1258
	/* wait a moment */
1259
	sleep(1);
1260

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

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

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

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

    
1287
EOD;
1288

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

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

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

    
1298
	return 0;
1299
}
1300

    
1301
function get_real_wan_interface() {
1302
	global $config, $g;
1303

    
1304
	$wancfg = $config['interfaces']['wan'];
1305

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

    
1311
	return $wanif;
1312
}
1313

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

    
1317
	$wancfg = $config['interfaces'][$interface];
1318

    
1319
	$interface = filter_translate_type_to_real_interface($interface);
1320

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1436
?>
(9-9/25)