Project

General

Profile

Download (42.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']}");
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 bridge{$bridges_total} down");
151
		usleep(100);
152
		mwexec("/sbin/ifconfig {$config['interfaces'][$lancfg['bridge']]['if']} up");
153
		usleep(5);
154
		mwexec("/sbin/ifconfig {$lancfg['if']} up");
155
		usleep(5);
156
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
157
		
158
		$bridges_total++;
159
	}
160
	
161
	/* media */
162
	if ($lancfg['media'] || $lancfg['mediaopt']) {
163
		$cmd = "/sbin/ifconfig " . escapeshellarg($lancfg['if']);
164
		if ($lancfg['media'])
165
			$cmd .= " media " . escapeshellarg($lancfg['media']);
166
		if ($lancfg['mediaopt'])
167
			$cmd .= " mediaopt " . escapeshellarg($lancfg['mediaopt']);
168
		mwexec($cmd);
169
	}
170

    
171
	mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) . " " .
172
		escapeshellarg($lancfg['ipaddr'] . "/" . $lancfg['subnet']));
173

    
174
	if (!$g['booting']) {
175
		/* make new hosts file */
176
		system_hosts_generate();
177

    
178
		/* reconfigure static routes (kernel may have deleted them) */
179
		system_routing_configure();
180

    
181
		/* set the reload filter dity flag */
182
		touch("{$g['tmp_path']}/filter_dirty");
183

    
184
		/* reload IPsec tunnels */
185
		vpn_ipsec_configure();
186

    
187
		/* reload dhcpd (gateway may have changed) */
188
		services_dhcpd_configure();
189

    
190
		/* reload dnsmasq */
191
		services_dnsmasq_configure();
192

    
193
		/* reload webgui */
194
		system_webgui_start();
195

    
196
		/* reload captive portal */
197
		captiveportal_configure();
198
	}
199

    
200
	return 0;
201
}
202

    
203
function interfaces_optional_configure() {
204
	global $config, $g;
205
	global $bridgeconfig;
206

    
207
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
208
		interfaces_optional_configure_if($i);
209
	}
210

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

    
215
		/* reload IPsec tunnels */
216
		vpn_ipsec_configure();
217

    
218
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
219
		services_dhcpd_configure();
220

    
221
		/* restart dnsmasq */
222
		services_dnsmasq_configure();
223

    
224
		/* set the reload filter dity flag */
225
		touch("{$g['tmp_path']}/filter_dirty");				
226
	}
227

    
228
	return 0;
229
}
230

    
231
function interfaces_optional_configure_if($opti) {
232
	global $config, $g;
233
	global $bridgeconfig, $debugging;
234

    
235
	$bridges_total = get_next_available_bridge_interface();
236

    
237
	$optcfg = $config['interfaces']['opt' . $opti];
238

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

    
246
	if (isset($optcfg['enable'])) {
247
		/* wireless configuration? */
248
		if (is_array($optcfg['wireless']))
249
			interfaces_wireless_configure($optcfg['if'], $optcfg['wireless']);
250

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

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

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

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

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

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

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

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

    
483
function interfaces_wireless_configure($if, $wlcfg) {
484
	global $config, $g;
485
	
486
	/* set values for /path/program */
487
	$hostapd = "/usr/sbin/hostapd";
488
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
489
	$ifconfig = "/sbin/ifconfig ";
490
	$killall = "/usr/bin/killall ";
491

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

    
494
	/* Set a/b/g standard */
495
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
496

    
497
	/* Set 802.11g protection mode */
498
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
499

    
500
	/* set wireless channel value */
501
	if($wlcfg['channel'])
502
		$channel = "channel " . escapeshellarg($wlcfg['channel']);
503

    
504
	/* set Distance value */
505
	if($wlcfg['distance']) 
506
		$distance = escapeshellarg($wlcfg['distance']);
507

    
508
	/* Set ssid */
509
	if($wlcfg['ssid'])  
510
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
511

    
512
	/* Set stationname */
513
	if ($wlcfg['stationname'])
514
		$stationname = "stationname " . escapeshellarg($wlcfg['stationname']);
515
	else
516
		$stationname = "stationname pfsense";
517
		
518
	/* Set wireless hostap mode */
519
	if ($wlcfg['mode'] == "hostap")
520
		$hostapmode = "mediaopt hostap";
521
	else
522
		$hostapmode = "-mediaopt hostap";
523

    
524
	/* Set wireless adhoc mode */
525
	if ($wlcfg['mode'] == "adhoc")
526
		$adhocmode = "mediaopt adhoc";
527
	else
528
		$adhocmode = "-mediaopt adhoc";
529

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

    
532
	/* handle hide ssid option */
533
	if(isset($wlcfg['hidessid']['enable']))
534
		$hidessid = "hidessid";
535
	else
536
		$hidessid = "-hidessid";
537

    
538
	/* handle pureg (802.11g) only option */
539
	if(isset($wlcfg['pureg']['enable']))
540
		$pureg = "mode 11g pureg";
541
	else
542
		$pureg = "-pureg";
543

    
544
	/* enable apbridge option */
545
	if(isset($wlcfg['apbridge']['enable']))
546
		$apbridge = "apbridge";
547
	else
548
		$apbridge = "-apbridge";
549

    
550
	/* handle turbo option */
551
	if(isset($wlcfg['turbo']['enable']))
552
		$turbo = "mediaopt turbo";
553
	else
554
		$turbo = "-mediaopt turbo";
555

    
556
	/* handle txpower setting */
557
	if($wlcfg['txpower'] <> "")
558
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
559
	
560
	/* handle wme option */
561
	if(isset($wlcfg['wme']['enable']))
562
		$wme = "wme";
563
	else
564
		$wme = "-wme";
565
	
566
	/* set up wep if enabled */
567
        if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
568
                $wepset .= "authmode shared wepmode on ";
569
                $i = 1;
570
                foreach ($wlcfg['wep']['key'] as $wepkey) {
571
                        $wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
572
                        if (isset($wepkey['txkey'])) {
573
                                $wepset .= "weptxkey {$i} ";
574
                        }
575
                        $i++;
576
                }
577
        } else {
578
                $wepset = "authmode open wepmode off";
579
	}
580

    
581
	/* generate wpa_supplicant/hostap config if wpa is enabled */
582

    
583
	switch ($wlcfg['mode']) {
584
		case 'bss':
585
			if (isset($wlcfg['wpa']['enable'])) {
586

    
587
				$wpa .= <<<EOD
588
ctrl_interface={$g['varrun_path']}/wpa_supplicant
589
ctrl_interface_group=0
590
ap_scan=1
591
#fast_reauth=1
592
network={
593
ssid="{$wlcfg['ssid']}"
594
scan_ssid=1
595
priority=5
596
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
597
psk="{$wlcfg['wpa']['passphrase']}"
598
pairwise={$wlcfg['wpa']['wpa_pairwise']}
599
group={$wlcfg['wpa']['wpa_pairwise']}
600
}
601
EOD;
602

    
603
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
604
				fwrite($fd, "{$wpa}");
605
				fclose($fd);
606

    
607
				mwexec("{$killall} wpa_supplicant");
608
			}
609
		break;
610

    
611
		case 'hostap':
612
			if (isset($wlcfg['wpa']['enable'])) {
613
				$wpa .= <<<EOD
614
interface={$if}
615
driver=bsd
616
logger_syslog=-1
617
logger_syslog_level=0
618
logger_stdout=-1
619
logger_stdout_level=0
620
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
621
ctrl_interface={$g['varrun_path']}/hostapd
622
ctrl_interface_group=wheel
623
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
624
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
625
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
626
ssid={$wlcfg['ssid']}
627
debug={$wlcfg['wpa']['debug_mode']}
628
auth_algs={$wlcfg['wpa']['auth_algs']}
629
wpa={$wlcfg['wpa']['wpa_mode']}
630
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
631
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
632
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
633
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
634
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
635
wpa_passphrase={$wlcfg['wpa']['passphrase']}
636
ieee8021x={$wlcfg['wpa']['ieee8021x']}
637
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
638
#rsn_preauth=1
639
#rsn_preauth_interfaces=eth0
640
EOD;
641

    
642
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
643
				fwrite($fd, "{$wpa}");
644
				fclose($fd);
645

    
646
				mwexec("{$killall} hostapd");
647
			}
648
		break;
649

    
650
		case 'adhoc':
651
			mwexec("{$killall} hostapd");
652
			mwexec("{$killall} wpa_supplicant");
653
		break;
654
	}	
655

    
656
	/* all variables are set, lets start up everything */
657
	
658
	mwexec("$ifconfig $if" . " -mediaopt hostap,turbo");	// Fix bug with turbomode and reboot (hopefully)        
659
	mwexec("$ifconfig $if" . " down"); 
660
	mwexec("$ifconfig $if" . " " . $standard);
661
	mwexec("$ifconfig $if" . " " . $protmode);
662
	mwexec("$ifconfig $if" . " " . $channel);
663
	mwexec("$ifconfig $if" . " " . $ssid);
664
	mwexec("$ifconfig $if" . " " . $stationname);
665
	mwexec("$ifconfig $if" . " " . $adhocmode);
666
	/* fix turbo mode and reboot */
667
	mwexec("$ifconfig $if" . " up"); 
668
	mwexec("$ifconfig $if" . " " . $hostapmode);
669
	mwexec("$ifconfig $if" . " " . $turbo);
670
	mwexec("$ifconfig $if" . " down"); 
671
	/* fix turbo mode and reboot */
672
	mwexec("$ifconfig $if" . " " . $hidessid);
673
	mwexec("$ifconfig $if" . " " . $pureg);
674
	mwexec("$ifconfig $if" . " " . $apbridge);
675
	mwexec("$ifconfig $if" . " " . $wme);
676
	mwexec("$ifconfig $if" . " " . $wepset);
677
	mwexec("$ifconfig $if" . " up");
678

    
679
	if (isset($wlcfg['wpa']['enable'])) {
680
		if ($wlcfg['mode'] == bss) 
681
			mwexec("{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf");
682
		if ($wlcfg['mode'] == hostap) 
683
			mwexec("{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf");
684
	}
685

    
686
	/* set ack timers according to users preference (if he/she has any) */
687
	if($distance) {
688
		mwexec("/sbin/athctrl.sh -i {$if} -d {$distance}");
689
		$fd = fopen("/tmp/athctrl.txt", "w");
690
		fwrite($fd, "/sbin/athctrl.sh -i {$if} -d {$distance}");
691
		fclose($fd);		
692
	}
693

    
694
	if(isset($wlcfg['useolsr']))
695
		setup_wireless_olsr($if);
696

    
697
	return 0;
698

    
699
}
700

    
701
function find_dhclient_process($interface) {
702
	if(filter_translate_type_to_real_interface($interface) <> "")
703
        	$realinterface = filter_translate_type_to_real_interface($interface);
704
	$pid = `/usr/bin/pgrep -f "dhclient: {$realinterface}(\$| .*)"`;
705
	return $pid;
706
}
707

    
708
function interfaces_wan_configure() {
709
	global $config, $g;
710

    
711
	$wancfg = $config['interfaces']['wan'];
712

    
713
	if(!$g['booting']) {
714
		mute_kernel_msgs();
715

    
716
		/* find dhclient process for wan and kill it */
717
		killbypid(find_dhclient_process("wan"));
718

    
719
		/* remove wanup file if it exists */
720
		unlink_if_exists("{$g['tmp_path']}/wanup");
721

    
722
		/* kill PPPoE client (mpd) */
723
		killbypid("{$g['varrun_path']}/mpd.pid");
724

    
725
		/* wait for processes to die */
726
		sleep(3);
727

    
728
		unlink_if_exists("{$g['varetc_path']}/dhclient_wan.conf");
729
		unlink_if_exists("{$g['varetc_path']}/mpd.conf");
730
		unlink_if_exists("{$g['varetc_path']}/mpd.links");
731
		unlink_if_exists("{$g['vardb_path']}/wanip");
732
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
733
	}
734

    
735
	/* remove all addresses first */
736
	while (mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " -alias") == 0);
737
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
738

    
739
	/* wireless configuration? */
740
	if (is_array($wancfg['wireless']))
741
		interfaces_wireless_configure($wancfg['if'], $wancfg['wireless']);
742

    
743
	if ($wancfg['spoofmac']) {
744
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
745
			" link " . escapeshellarg($wancfg['spoofmac']));
746
	}  else {
747
		$mac = get_interface_mac_address($wancfg['if']);
748
		if($mac == "ff:ff:ff:ff:ff:ff") {
749
			/*   this is not a valid mac address.  generate a
750
			 *   temporary mac address so the machine can get online.
751
			 */
752
			echo "Generating new MAC address.";
753
			$random_mac = generate_random_mac_address();
754
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
755
				" link " . escapeshellarg($random_mac));
756
			$wancfg['spoofmac'] = $random_mac;
757
			write_config();
758
			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");
759
		}
760
	}
761

    
762
	/* media */
763
	if ($wancfg['media'] || $wancfg['mediaopt']) {
764
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
765
		if ($wancfg['media'])
766
			$cmd .= " media " . escapeshellarg($wancfg['media']);
767
		if ($wancfg['mediaopt'])
768
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
769
		mwexec($cmd);
770
	}
771

    
772
	switch ($wancfg['ipaddr']) {
773

    
774
		case 'dhcp':
775
			interfaces_wan_dhcp_configure();
776
			break;
777

    
778
		case 'pppoe':
779
			interfaces_wan_pppoe_configure();
780
			break;
781

    
782
		case 'pptp':
783
			interfaces_wan_pptp_configure();
784
			break;
785

    
786
		case 'bigpond':
787
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
788
			interfaces_wan_dhcp_configure();
789
			break;
790

    
791
		default:
792
			if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
793
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
794
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
795
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
796
			} else {
797
				mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
798
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']));
799
			}
800
			/* install default route */
801
			mwexec("/sbin/route delete default");
802
			mwexec("/sbin/route add default " . escapeshellarg($config['interfaces']['wan']['gateway']));
803

    
804
			/* resync pf (done automatically for DHCP/PPPoE/PPTP) */
805
			filter_configure();
806
	}
807

    
808
	if ($wancfg['bridge']) {
809
		/* use open/netBSD style bridge */
810
		mwexec("/sbin/ifconfig bridge{$bridges_total} create");
811
		
812
		/* force all bridged interfaces to use same mtu */
813
		$mtu = get_interface_mtu($config['interfaces'][$wancfg['bridge']]['if']);
814
		mwexec("/sbin/ifconfig {$wancfg['if']} mtu {$mtu}");
815
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}");
816
		
817
		/* assign items to a bridge */
818
		mwexec("/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']}");
819
		mwexec("/sbin/ifconfig bridge{$bridges_total} stp {$wancfg['if']} stp {$config['interfaces'][$wancfg['bridge']]['if']}");
820
		
821
		/* log commands run for debugging in /tmp/ */
822
		$fd = fopen("{$g['tmp_path']}/bridge_config_{$wancfg['if']}", "w");
823
		fwrite($fd, "/sbin/ifconfig {$wancfg['if']} mtu {$mtu}\n");
824
		fwrite($fd, "/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} mtu {$mtu}\n");
825
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} create\n");
826
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} addm {$wancfg['if']} addm {$config['interfaces'][$wancfg['bridge']]['if']} up\n");
827
		fwrite($fd, "/sbin/ifconfig bridge{$bridges_total} stp {$wancfg['if']} stp {$config['interfaces'][$wancfg['bridge']]['if']}\n");
828
		fclose($fd);
829
		
830
		/* bring up interfaces */
831
		mwexec("/sbin/ifconfig bridge{$bridges_total} down");
832
		usleep(100);
833
		mwexec("/sbin/ifconfig {$config['interfaces'][$wancfg['bridge']]['if']} up");
834
		usleep(5);
835
		mwexec("/sbin/ifconfig {$wancfg['if']} up");
836
		usleep(5);
837
		mwexec("/sbin/ifconfig bridge{$bridges_total} up");
838
		
839
		$bridges_total++;
840
	}
841

    
842
	if (!$g['booting']) {
843
		/* reconfigure static routes (kernel may have deleted them) */
844
		system_routing_configure();
845

    
846
		/* set the reload filter dity flag */
847
		touch("{$g['tmp_path']}/filter_dirty");
848

    
849
		/* reload ipsec tunnels */
850
		vpn_ipsec_configure();
851

    
852
		/* restart ez-ipupdate */
853
		services_dyndns_configure();
854

    
855
		/* force DNS update */
856
		services_dnsupdate_process();
857

    
858
		/* restart dnsmasq */
859
		services_dnsmasq_configure();
860
	}
861

    
862
	unmute_kernel_msgs();
863

    
864
	return 0;
865
}
866

    
867
function interfaces_opt_dhcp_configure($interface) {
868
	global $config, $g;
869

    
870
	$optcfg = $config['interfaces'][$interface];
871
	$optif = $optcfg['if'];
872

    
873
	/* generate dhclient_wan.conf */
874
	$fd = fopen("{$g['varetc_path']}/dhclient_{$optif}.conf", "w");
875
	if (!$fd) {
876
		printf("Error: cannot open dhclient_{$optif}.conf in interfaces_opt_dhcp_configure({$optif}) for writing.\n");
877
		return 1;
878
	}
879

    
880
	if ($optcfg['dhcphostname']) {
881
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
882
	} else {
883
		$dhclientconf_hostname = "";
884
	}
885

    
886
 	$dhclientconf = "";
887

    
888
	$dhclientconf .= <<<EOD
889
interface "{$optif}" {
890
	send host-name "{$optcfg['dhcphostname']}";
891
	script "/sbin/dhclient-script";
892
	{$dhclientconf_hostname}
893
}
894

    
895
EOD;
896

    
897
	fwrite($fd, $dhclientconf);
898
	fclose($fd);
899

    
900
        /* bring interface up before starting dhclient */
901
        mwexec("/sbin/ifconfig {$optif} up");
902

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

    
906
	return 0;
907
}
908

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

    
912
	if(filter_translate_type_to_real_interface($interface) <> "")
913
        	$realinterface = filter_translate_type_to_real_interface($interface);
914

    
915
	$optcfg = $config['interfaces'][$interface];
916

    
917
	/* generate dhclient_$interface.conf */
918
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
919
	if (!$fd) {
920
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_dhcp_configure({$$interface}) for writing.\n");
921
		return 1;
922
	}
923

    
924
	if ($optcfg['dhcphostname']) {
925
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$optcfg['dhcphostname']}\";\n";
926
	} else {
927
		$dhclientconf_hostname = "";
928
	}
929

    
930
 	$dhclientconf = "";
931

    
932
	$dhclientconf .= <<<EOD
933
interface "{$realinterface}" {
934
	script "/sbin/dhclient-script";
935
	{$dhclientconf_hostname}
936
}
937

    
938
EOD;
939

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

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

    
951
	$fout = fopen("/tmp/ifconfig_{$optif}","w");
952
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$optif}.conf {$optif}");
953
	fclose($fout);
954

    
955
	return 0;
956
}
957

    
958
function interfaces_wan_dhcp_configure() {
959
	global $config, $g;
960

    
961
	$wancfg = $config['interfaces']['wan'];
962

    
963
	/* generate dhclient_wan.conf */
964
	$fd = fopen("{$g['varetc_path']}/dhclient_wan.conf", "w");
965
	if (!$fd) {
966
		printf("Error: cannot open dhclient_wan.conf in interfaces_wan_dhcp_configure() for writing.\n");
967
		return 1;
968
	}
969
	
970
	if ($wancfg['dhcphostname']) {
971
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
972
	} else {
973
		$dhclientconf_hostname = "";
974
	}
975

    
976
 	$dhclientconf = "";
977

    
978
	$dhclientconf .= <<<EOD
979
interface "{$wancfg['if']}" {
980
	script "/sbin/dhclient-script";
981
	{$dhclientconf_hostname}
982
}
983

    
984
EOD;
985

    
986
	fwrite($fd, $dhclientconf);
987
	fclose($fd);
988
	
989
	$wanif = $wancfg['if'];
990
	
991
        /* bring wan interface up before starting dhclient */
992
        mwexec("/sbin/ifconfig {$wanif} up");
993

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

    
997
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
998
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_wan.conf {$wanif}");
999
	fclose($fout);
1000

    
1001
	return 0;
1002
}
1003

    
1004
function interfaces_wan_dhcp_down() {
1005
	global $config;
1006
	$wancfg = $config['interfaces']['wan'];
1007
	$wanif = $wancfg['if'];
1008
	mwexec("/sbin/ifconfig {$wanif} delete");
1009
	sleep(1);
1010
}
1011

    
1012
function interfaces_dhcp_down($interface) {
1013
	global $config;
1014
	if(filter_translate_type_to_real_interface($interface) <> "")
1015
		$realinterface = filter_translate_type_to_real_interface($interface);
1016
	mwexec("/sbin/ifconfig {$realinterface} down");
1017
	sleep(1);
1018
	$pid = find_dhclient_process($interface);
1019
	if($pid)
1020
		mwexec("kill {$pid}");
1021
}
1022

    
1023
function interfaces_dhcp_up($interface) {
1024
	interfaces_dhcp_configure($interface);
1025
	sleep(1);
1026
}
1027

    
1028
function interfaces_wan_dhcp_up() {
1029
	interfaces_wan_dhcp_configure();
1030
	sleep(1);
1031
}
1032

    
1033
function interfaces_wan_pppoe_configure() {
1034
	global $config, $g;
1035

    
1036
	$wancfg = $config['interfaces']['wan'];
1037
	$pppoecfg = $config['pppoe'];
1038

    
1039
	/* generate mpd.conf */
1040
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1041
	if (!$fd) {
1042
		printf("Error: cannot open mpd.conf in interfaces_wan_pppoe_configure().\n");
1043
		return 1;
1044
	}
1045

    
1046
	$idle = 0;
1047

    
1048
	if (isset($pppoecfg['ondemand'])) {
1049
		$ondemand = "enable";
1050
		if ($pppoecfg['timeout'])
1051
			$idle = $pppoecfg['timeout'];
1052
	} else {
1053
		$ondemand = "disable";
1054
	}
1055

    
1056
	$mpdconf = <<<EOD
1057
pppoe:
1058
	new -i ng0 pppoe pppoe
1059
	set iface route default
1060
	set iface {$ondemand} on-demand
1061
	set iface idle {$idle}
1062
	set iface up-script /usr/local/sbin/ppp-linkup
1063

    
1064
EOD;
1065

    
1066
	/*    Check for ppp-linkdown Script in /usr/local/sbin
1067
	 *    Create reference in mpd.conf
1068
	 */
1069
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1070
		$mpdconf .= <<<EOD
1071
	set iface down-script /usr/local/sbin/ppp-linkdown
1072

    
1073
EOD;
1074
	}
1075

    
1076
	if (isset($pppoecfg['ondemand'])) {
1077
		if (isset($pppoecfg['local-ip']) && isset($pppoecfg['remote-ip'])) {
1078
			$mpdconf .= <<<EOD
1079
	set iface addrs {$pppoecfg['local-ip']} {$pppoecfg['remote-ip']}
1080

    
1081
EOD;
1082
		} else {
1083
			$mpdconf .= <<<EOD
1084
	set iface addrs 192.0.2.112 192.0.2.113
1085

    
1086
EOD;
1087
		}
1088
	}
1089

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

    
1103
EOD;
1104

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

    
1109
EOD;
1110
	}
1111

    
1112
	$mpdconf .= <<<EOD
1113
	open iface
1114

    
1115
EOD;
1116

    
1117
	fwrite($fd, $mpdconf);
1118
	fclose($fd);
1119

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

    
1127
	$mpdconf = <<<EOD
1128
pppoe:
1129
	set link type pppoe
1130
	set pppoe iface {$wancfg['if']}
1131
	set pppoe service "{$pppoecfg['provider']}"
1132
	set pppoe enable originate
1133
	set pppoe disable incoming
1134

    
1135
EOD;
1136

    
1137
	fwrite($fd, $mpdconf);
1138
	fclose($fd);
1139
	
1140
	if(file_exists("{$g['varrun_path']}/mpd.pid") and $g['booting']) {
1141
		/* if we are booting and mpd has already been started then don't start again. */
1142
	} else {
1143
		/* if mpd is active, lets take it down */
1144
		if(file_exists("{$g['varrun_path']}/mpd.pid")) {
1145
			killbypid("{$g['varrun_path']}/mpd.pid");
1146
			sleep(3);
1147
		}
1148
		/* fire up mpd */	
1149
		mwexec("/usr/local/sbin/mpd -b -d {$g['varetc_path']} -p {$g['varrun_path']}/mpd.pid pppoe");
1150
	}
1151

    
1152
        /* sleep until wan is up - or 30 seconds, whichever comes first */
1153
	for ($count = 0; $count < 30; $count++) {
1154
		if(file_exists("{$g['tmp_path']}/wanup")) {
1155
			break;
1156
		}
1157
		sleep(1);
1158
	}
1159

    
1160
	unlink_if_exists("{$g['tmp_path']}/wanup");
1161

    
1162
	return 0;
1163
}
1164

    
1165
function interfaces_wan_pppoe_down() {
1166
	global $g;
1167
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1168
	sleep(1);
1169
}
1170

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

    
1177
function interfaces_wan_pptp_configure() {
1178
	global $config, $g;
1179

    
1180
	$wancfg = $config['interfaces']['wan'];
1181
	$pptpcfg = $config['pptp'];
1182

    
1183
	/* generate mpd.conf */
1184
	$fd = fopen("{$g['varetc_path']}/mpd.conf", "w");
1185
	if (!$fd) {
1186
		printf("Error: cannot open mpd.conf in interfaces_wan_pptp_configure().\n");
1187
		return 1;
1188
	}
1189

    
1190
	$idle = 0;
1191

    
1192
	if (isset($pptpcfg['ondemand'])) {
1193
		$ondemand = "enable";
1194
		if ($pptpcfg['timeout'])
1195
			$idle = $pptpcfg['timeout'];
1196
	} else {
1197
		$ondemand = "disable";
1198
	}
1199

    
1200
	$mpdconf = <<<EOD
1201
pptp:
1202
	new -i ng0 pptp pptp
1203
	set iface route default
1204
	set iface {$ondemand} on-demand
1205
	set iface idle {$idle}
1206
	set iface up-script /usr/local/sbin/ppp-linkup
1207

    
1208
EOD;
1209

    
1210
	/*   Check for ppp-linkdown Script in /usr/local/sbin
1211
	 *   Create reference in mpd.conf
1212
	 */
1213
	if ( file_exists("/usr/local/sbin/ppp-linkdown") ){
1214
		$mpdconf .= <<<EOD
1215
	set iface down-script /usr/local/sbin/ppp-linkdown
1216

    
1217
EOD;
1218
	}
1219

    
1220
	if (isset($pptpcfg['ondemand'])) {
1221
		$mpdconf .= <<<EOD
1222
	set iface addrs 10.0.0.1 10.0.0.2
1223

    
1224
EOD;
1225
	}
1226

    
1227
	$mpdconf .= <<<EOD
1228
	set bundle disable multilink
1229
	set bundle authname "{$pptpcfg['username']}"
1230
	set bundle password "{$pptpcfg['password']}"
1231
	set link keep-alive 10 60
1232
	set link max-redial 0
1233
	set link no acfcomp protocomp
1234
	set link disable pap chap
1235
	set link accept chap
1236
	set ipcp no vjcomp
1237
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1238

    
1239
EOD;
1240

    
1241
	if (isset($config['system']['dnsallowoverride'])) {
1242
		$mpdconf .= <<<EOD
1243
	set ipcp enable req-pri-dns
1244

    
1245
EOD;
1246
	}
1247

    
1248
	$mpdconf .= <<<EOD
1249
	open
1250

    
1251
EOD;
1252

    
1253
	fwrite($fd, $mpdconf);
1254
	fclose($fd);
1255

    
1256
	/* generate mpd.links */
1257
	$fd = fopen("{$g['varetc_path']}/mpd.links", "w");
1258
	if (!$fd) {
1259
		printf("Error: cannot open mpd.links in interfaces_wan_pptp_configure().\n");
1260
		return 1;
1261
	}
1262

    
1263
	$mpdconf = <<<EOD
1264
pptp:
1265
	set link type pptp
1266
	set pptp enable originate outcall
1267
	set pptp disable windowing
1268
	set pptp self {$pptpcfg['local']}
1269
	set pptp peer {$pptpcfg['remote']}
1270

    
1271
EOD;
1272

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

    
1276
	/* configure interface */
1277
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1278
		escapeshellarg($pptpcfg['local'] . "/" . $pptpcfg['subnet']));
1279

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

    
1283
	return 0;
1284
}
1285

    
1286
function interfaces_wan_pptp_down() {
1287
	global $g;
1288
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR2");
1289
	sleep(1);
1290
}
1291

    
1292
function interfaces_wan_pptp_up() {
1293
	global $g;
1294
	sigkillbypid("{$g['varrun_path']}/mpd.pid", "SIGUSR1");
1295
	sleep(1);
1296
}
1297

    
1298
function interfaces_wan_bigpond_configure($curwanip) {
1299
	global $config, $g;
1300

    
1301
	$bpcfg = $config['bigpond'];
1302

    
1303
	if (!$curwanip) {
1304
		/* IP address not configured yet, exit */
1305
		return 0;
1306
	}
1307

    
1308
	/* kill bpalogin */
1309
	killbyname("bpalogin");
1310

    
1311
	/* wait a moment */
1312
	sleep(1);
1313

    
1314
	/* get the default domain */
1315
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1316
	if ($nfd) {
1317
		$defaultdomain = trim(fgets($nfd));
1318
		fclose($nfd);
1319
	}
1320

    
1321
	/* generate bpalogin.conf */
1322
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1323
	if (!$fd) {
1324
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1325
		return 1;
1326
	}
1327

    
1328
	if (!$bpcfg['authserver'])
1329
		$bpcfg['authserver'] = "dce-server";
1330
	if (!$bpcfg['authdomain'])
1331
		$bpcfg['authdomain'] = $defaultdomain;
1332

    
1333
	$bpconf = <<<EOD
1334
username {$bpcfg['username']}
1335
password {$bpcfg['password']}
1336
authserver {$bpcfg['authserver']}
1337
authdomain {$bpcfg['authdomain']}
1338
localport 5050
1339

    
1340
EOD;
1341

    
1342
	if ($bpcfg['minheartbeatinterval'])
1343
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1344

    
1345
	fwrite($fd, $bpconf);
1346
	fclose($fd);
1347

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

    
1351
	return 0;
1352
}
1353

    
1354
function get_real_wan_interface() {
1355
	global $config, $g;
1356

    
1357
	$wancfg = $config['interfaces']['wan'];
1358

    
1359
	$wanif = $wancfg['if'];
1360
	if (($wancfg['ipaddr'] == "pppoe") || ($wancfg['ipaddr'] == "pptp")) {
1361
		$wanif = $g['pppoe_interface'];
1362
	}
1363

    
1364
	return $wanif;
1365
}
1366

    
1367
function get_current_wan_address($interface = "wan") {
1368
	global $config, $g;
1369

    
1370
	$wancfg = $config['interfaces'][$interface];
1371

    
1372
	$interface = filter_translate_type_to_real_interface($interface);
1373

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

    
1378
		if (isset($ifinfo[1])) {
1379
			$aif = preg_split("/\s+/", $ifinfo[1]);
1380
			$curwanip = chop($aif[3]);
1381

    
1382
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1383
				return $curwanip;
1384
		}
1385

    
1386
		return null;		
1387
	} else if (in_array($wancfg['ipaddr'], array('pppoe','pptp','bigpond'))) {
1388
		/* dynamic WAN IP address, find out which one */
1389
		$wanif = get_real_wan_interface();
1390

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

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

    
1398
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1399
				return $curwanip;
1400
		}
1401

    
1402
		return null;
1403
	} else {
1404
		/* static WAN IP address */
1405
		return $wancfg['ipaddr'];
1406
	}
1407
}
1408

    
1409
/****f* interfaces/is_altq_capable
1410
 * NAME
1411
 *   is_altq_capable - Test if interface is capable of using ALTQ
1412
 * INPUTS
1413
 *   $int            - string containing interface name
1414
 * RESULT
1415
 *   boolean         - true or false
1416
 ******/
1417

    
1418
function is_altq_capable($int) {
1419
        /* Per:
1420
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1421
         * Only the following drivers have ALTQ support
1422
         */
1423
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1424
		"em", "fxp", "hme", "lnc", "ndis", "rl", "sf", "sis", "sk",
1425
		"tun", "vr", "wi", "xl", "vlan", "ste");
1426

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

    
1429
        if (in_array($int_family[0], $capable))
1430
                return true;
1431
        else
1432
                return false;
1433
}
1434

    
1435
function get_number_of_bridged_interfaces() {
1436
	$bridges_total = 0;
1437
	$bridges = split("\n", `/sbin/ifconfig -a | /usr/bin/grep bridge | grep flags`);
1438
	foreach($bridges as $bridge) {
1439
		preg_match_all("/bridge(.*):/",$bridge,$match_array);
1440
		if($match_array[1][0] <> "") {
1441
			if($match_array[1][0] > $bridges_total)
1442
				$bridges_total = $match_array[1][0];
1443
		}
1444
	}
1445
	return "{$bridges_total}";
1446
}
1447

    
1448
function get_next_available_bridge_interface() {
1449
	$bridges_total = get_number_of_bridged_interfaces();
1450
	$interfaces = `/sbin/ifconfig -l`;
1451
	$x=0;
1452
	for($x=0; $x<$bridges_total; $x++) {
1453
		if(!stristr($interfaces, "bridge{$x}")) {
1454
			return "{$x}";
1455
		}
1456
	}
1457
	return "{$x}";
1458
}
1459

    
1460
function destroy_bridge($bridge_num) {
1461
	mwexec("/sbin/ifconfig bridge{$bridge_num} down");
1462
	sleep(1);
1463
	mwexec("/sbin/ifconfig bridge{$bridge_num} delete");
1464
	sleep(1);
1465
	mwexec("/sbin/ifconfig bridge{$bridge_num} destroy");
1466
	sleep(1);
1467
	return;
1468
}
1469

    
1470
function discover_bridge($interface1, $interface2) {
1471
	if(!$interface1) return;
1472
	if(!$interface2) return;
1473
	$total_bridges = get_number_of_bridged_interfaces();
1474
	$interfaces = `/sbin/ifconfig -l`;
1475
	$x=0;
1476
	for($x=0; $x<$total_bridges; $x++) {
1477
		$bridge_text = "NA";
1478
		if(!stristr($interfaces, "bridge{$x}")) 
1479
			continue;
1480
		$bridge_text = `/sbin/ifconfig bridge{$x} | grep member`;
1481
		if(stristr($bridge_text, $interface1) == true and
1482
		   stristr($bridge_text, $interface2) == true) {
1483
			return "{$x}";			
1484
		}
1485
	}
1486
	return "-1";
1487
}
1488

    
1489
?>
(10-10/26)