Project

General

Profile

Download (43.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	interfaces.inc
5
	Copyright (C) 2004-2005 Scott Ullrich
6
	All rights reserved.
7

    
8
	function interfaces_wireless_configure is
9
	Copyright (C) 2005 Espen Johansen
10
	All rights reserved.
11

    
12
	originally part of m0n0wall (http://m0n0.ch/wall)
13
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
	All rights reserved.
15

    
16
	Redistribution and use in source and binary forms, with or without
17
	modification, are permitted provided that the following conditions are met:
18

    
19
	1. Redistributions of source code must retain the above copyright notices,
20
	   this list of conditions and the following disclaimer.
21

    
22
	2. Redistributions in binary form must reproduce the above copyright
23
	   notices, this list of conditions and the following disclaimer in the
24
	   documentation and/or other materials provided with the distribution.
25

    
26
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
	POSSIBILITY OF SUCH DAMAGE.
36
*/
37

    
38
/* include all configuration functions */
39
require_once("functions.inc");
40

    
41
function interfaces_loopback_configure() {
42
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
43

    
44
	return 0;
45
}
46

    
47
function interfaces_vlan_configure() {
48
	global $config;
49

    
50
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
51

    
52
		/* devices with native VLAN support */
53
		$vlan_native_supp = explode(" ", "bge em gx nge ti txp");
54

    
55
		/* devices with long frame support */
56
		$vlan_long_supp = explode(" ", "dc fxp sis ste tl tx xl");
57

    
58
		$i = 0;
59

    
60
		foreach ($config['vlans']['vlan'] as $vlan) {
61

    
62
			$cmd = "/sbin/ifconfig vlan{$i} create vlan " .
63
				escapeshellarg($vlan['tag']) . " vlandev " .
64
				escapeshellarg($vlan['if']);
65

    
66
			/* get driver name */
67
			for ($j = 0; $j < strlen($vlan['if']); $j++) {
68
				if ($vlan['if'][$j] >= '0' && $vlan['if'][$j] <= '9')
69
					break;
70
			}
71
			$drvname = substr($vlan['if'], 0, $j);
72

    
73
			if (in_array($drvname, $vlan_native_supp))
74
				$cmd .= " link0";
75
			else if (in_array($drvname, $vlan_long_supp))
76
				$cmd .= " mtu 1500";
77

    
78
			mwexec($cmd);
79

    
80
			/* make sure the parent interface is up */
81
			mwexec("/sbin/ifconfig " . escapeshellarg($vlan['if']) . " up");
82

    
83
			$i++;
84
		}
85
	}
86

    
87
	return 0;
88
}
89

    
90
function interfaces_lan_configure() {
91
	global $config, $g;
92

    
93
	$bridges_total = get_next_available_bridge_interface();
94

    
95
	$lancfg = $config['interfaces']['lan'];
96

    
97
	/* if user has removed ip address, clear it*/
98
	if($lancfg['ipaddr'] == "")
99
		mwexec("/sbin/ifconfig {$lancfg['if']} delete");
100

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

    
105
	/* MAC spoofing? */
106
	if ($lancfg['spoofmac']) {
107
		mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
108
			" link " . escapeshellarg($lancfg['spoofmac']));
109
	} else {
110
		$mac = get_interface_mac_address($lancfg['if']);
111
		if($mac == "ff:ff:ff:ff:ff:ff") {
112
			/*   this is not a valid mac address.  generate a
113
			 *   temporary mac address so the machine can get online.
114
			 */
115
			echo "Generating new MAC address.";
116
			$random_mac = generate_random_mac_address();
117
			mwexec("/sbin/ifconfig " . escapeshellarg($lancfg['if']) .
118
				" link " . escapeshellarg($random_mac));
119
			$lancfg['spoofmac'] = $random_mac;
120
			write_config();
121
			file_notice("MAC Address altered", "The INVALID MAC address (ff:ff:ff:ff:ff:ff) on interface {$lancfg['if']} has been automatically replaced with {$random_mac}", "Interfaces");
122
		}
123
	}	
124

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

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

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

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

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

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

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

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

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

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

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

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

    
204
	return 0;
205
}
206

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

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

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

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

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

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

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

    
232
	return 0;
233
}
234

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

    
239
	$bridges_total = get_next_available_bridge_interface();
240

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
597
	/* generate wpa_supplicant/hostap config if wpa is enabled */
598

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

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

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

    
623
				mwexec("{$killall} wpa_supplicant");
624
			}
625
		break;
626

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

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

    
662
				mwexec("{$killall} hostapd");
663
			}
664
		break;
665

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

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

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

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

    
710
	if(isset($wlcfg['useolsr']))
711
		setup_wireless_olsr($if);
712

    
713
	return 0;
714

    
715
}
716

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

    
724
function interfaces_wan_configure() {
725
	global $config, $g;
726

    
727
	$wancfg = $config['interfaces']['wan'];
728

    
729
	if(!$g['booting']) {
730
		mute_kernel_msgs();
731

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

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

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

    
741
		/* wait for processes to die */
742
		sleep(3);
743

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

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

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

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

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

    
788
	switch ($wancfg['ipaddr']) {
789

    
790
		case 'dhcp':
791
			interfaces_wan_dhcp_configure();
792
			break;
793

    
794
		case 'pppoe':
795
			interfaces_wan_pppoe_configure();
796
			break;
797

    
798
		case 'pptp':
799
			interfaces_wan_pptp_configure();
800
			break;
801

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

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

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

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

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

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

    
866
		/* set the reload filter dity flag */
867
		touch("{$g['tmp_path']}/filter_dirty");
868

    
869
		/* reload ipsec tunnels */
870
		vpn_ipsec_configure();
871

    
872
		/* restart ez-ipupdate */
873
		services_dyndns_configure();
874

    
875
		/* force DNS update */
876
		services_dnsupdate_process();
877

    
878
		/* restart dnsmasq */
879
		services_dnsmasq_configure();
880
	}
881

    
882
	unmute_kernel_msgs();
883

    
884
	return 0;
885
}
886

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

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

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

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

    
906
 	$dhclientconf = "";
907

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

    
915
EOD;
916

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

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

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

    
926
	return 0;
927
}
928

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

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

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

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

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

    
950
 	$dhclientconf = "";
951

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

    
958
EOD;
959

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

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

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

    
975
	return 0;
976
}
977

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

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

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

    
996
 	$dhclientconf = "";
997

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

    
1004
EOD;
1005

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

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

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

    
1021
	return 0;
1022
}
1023

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

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

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

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

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

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

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

    
1066
	$idle = 0;
1067

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

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

    
1084
EOD;
1085

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

    
1093
EOD;
1094
	}
1095

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

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

    
1106
EOD;
1107
		}
1108
	}
1109

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

    
1123
EOD;
1124

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

    
1129
EOD;
1130
	}
1131

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

    
1135
EOD;
1136

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

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

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

    
1155
EOD;
1156

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

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

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

    
1182
	return 0;
1183
}
1184

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

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

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

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

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

    
1210
	$idle = 0;
1211

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

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

    
1228
EOD;
1229

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

    
1237
EOD;
1238
	}
1239

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

    
1244
EOD;
1245
	}
1246

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

    
1259
EOD;
1260

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

    
1265
EOD;
1266
	}
1267

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

    
1271
EOD;
1272

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

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

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

    
1291
EOD;
1292

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

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

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

    
1303
	return 0;
1304
}
1305

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

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

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

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

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

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

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

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

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

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

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

    
1360
EOD;
1361

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

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

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

    
1371
	return 0;
1372
}
1373

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

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

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

    
1384
	return $wanif;
1385
}
1386

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

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

    
1392
	$interface = filter_translate_type_to_real_interface($interface);
1393

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1509
?>
(10-10/26)