Project

General

Profile

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

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

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

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

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

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

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

    
39
/* include all configuration functions */
40
require_once("functions.inc");
41
require_once("globals.inc");
42

    
43
function interfaces_bring_up($interface) {
44
	if(!$interface) {
45
		log_error("bring_interface_up was called but no variable defined.");
46
		log_error( "Backtrace: " . var_dump(debug_backtrace()) );
47
		return;
48
	}
49
	mwexec("/sbin/ifconfig " . escapeshellarg($interface) . " up");
50
}
51

    
52
function interfaces_loopback_configure() 
53
{
54
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
55
	interfaces_bring_up("lo0");
56
	return 0;
57
}
58

    
59
function interfaces_vlan_configure() 
60
{
61
	global $config;
62

    
63
	$i = 0;
64
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
65
		foreach ($config['vlans']['vlan'] as $vlan) {
66
			if(empty($vlan['vlanif']))
67
				$vlan['vlanif'] = "vlan{$i}";
68
			/* XXX: Maybe we should report any errors?! */
69
			interface_vlan_configure($vlan['if'], $vlan['tag'], $vlan['vlanif']);
70
			$i++;
71
		}
72
	}
73
}
74

    
75
function interface_vlan_configure($if, $tag, $vlanif = "") 
76
{
77
		if(!$if) {
78
			log_error("interface_vlan_confgure called with if defined.");
79
			return;
80
		}
81
		if(!$vlanif) {
82
			log_error("interface_vlan_confgure called with vlanif defined.");
83
			return;
84
		}
85

    
86
        global $config, $g;
87

    
88
        /* make sure the parent interface is up */
89
		interfaces_bring_up($if);
90
		/* Since we are going to add vlan(4) try to enable all that hardware supports. */
91
		mwexec("/sbin/ifconfig {$if} vlanhwtag");
92
		mwexec("/sbin/ifconfig {$if} vlanmtu");
93

    
94
        if ($g['booting'] || !(empty($vlanif))) {
95
		mwexec("/sbin/ifconfig {$vlanif} destroy");
96
		mwexec("/sbin/ifconfig {$vlanif} create");
97
        } else 
98
		$vlanif = exec("/sbin/ifconfig vlan create");
99
	
100
        mwexec("/sbin/ifconfig {$vlanif} vlan " .
101
                escapeshellarg($tag) . " vlandev " .
102
                escapeshellarg($if));
103

    
104
		interfaces_bring_up($vlanif);
105

    
106
        /* invalidate interface cache */
107
        get_interface_arr(true);
108

    
109
        /*   all vlans need to spoof their parent mac address, too.  see
110
         *   ticket #1514: http://cvstrac.pfsense.com/tktview?tn=1514,33
111
         */
112
        foreach($config['interfaces'] as $interfaces) {
113
                if($interfaces['if'] == $if && $interfaces['spoofmac']) {
114
                        mwexec("/sbin/ifconfig " . escapeshellarg($if) .
115
                                " link " . escapeshellarg($interfaces['spoofmac']));
116
                }
117
        }
118

    
119
        /* XXX: ermal -- for now leave it here at the moment it does not hurt. */
120
		interfaces_bring_up($if);
121

    
122
        return $vlanif;
123
}
124

    
125
function interfaces_bridge_configure() 
126
{
127
        global $config;
128

    
129
        $i = 0;
130
        if (is_array($config['bridges']['bridged']) && count($config['bridges']['bridged'])) {
131
                foreach ($config['bridges']['bridged'] as $bridge) {
132
                        if(empty($bridge['bridgeif']))
133
                                $bridge['bridgeif'] = "bridge{$i}";
134
                        /* XXX: Maybe we should report any errors?! */
135
                        interface_bridge_configure($bridge);
136
                        $i++;
137
                }
138
        }
139
}
140

    
141
function interface_bridge_configure(&$bridge) 
142
{
143
	global $config, $g;
144

    
145
	if (!is_array($bridge))
146
	        return -1;
147

    
148
	if (empty($bridge['members'])) {
149
		log_error("No members found on {$bridge['bridgeif']}");
150
		return -1;
151
	}
152

    
153
	$members = explode(',', $bridge['members']);
154
	if (!count($members))
155
		return -1;
156
	
157
	$checklist = get_configured_interface_list();
158

    
159
	if ($g['booting'] || !empty($bridge['bridgeif'])) {
160
		mwexec("/sbin/ifconfig {$bridge['bridgeif']} destroy");
161
		mwexec("/sbin/ifconfig {$bridge['bridgeif']} create");
162
		$bridgeif = $bridge['bridgeif'];
163
	} else {
164
		$bridgeif = exec("/sbin/ifconfig bridge create");
165
	}
166

    
167
	/* Calculate smaller mtu and enforce it */
168
	$smallermtu = 0;
169
	foreach ($members as $member) {
170
		$realif = get_real_interface($member);
171
		$mtu = get_interface_mtu($realif);
172
		if ($smallermtu == 0 && !empty($mtu))
173
			$smallermtu = $mtu;
174
		else if (!empty($mtu) && $mtu < $smallermtu)
175
			$smallermtu = $mtu;
176
	}
177
	 
178
	/* Just in case anything is not working well */
179
	if ($smallermtu == 0)
180
		$smallermtu = 1500; 
181

    
182
	/* Add interfaces to bridge */
183
	foreach ($members as $member) {
184
		if (!array_key_exists($member, $checklist))
185
			continue;
186
		$realif = get_real_interface($member);
187
		$realif =  escapeshellarg($realif);
188
		/* make sure the parent interface is up */
189
		mwexec("/sbin/ifconfig {$realif} mtu {$smallermtu}");
190
		if(!$realif) 
191
			log_error("realif not defined in interfaces bridge - up");
192
		interfaces_bring_up($realif);
193
		mwexec("/sbin/ifconfig {$bridgeif} addm {$realif}");	
194
	}
195

    
196
	if (isset($bridge['enablestp'])) {
197
		/* Choose spanning tree proto */
198
		mwexec("/sbin/ifconfig {$bridgeif} proto {$bridge['proto']}");	
199
		
200
		if (!empty($bridge['stp'])) {
201
			$stpifs = explode(',', $bridge['stp']);
202
			foreach ($stpifs as $stpif) {
203
				$realif = get_real_interface($stpif);
204
				mwexec("/sbin/ifconfig {$bridgeif} stp {$realif}");
205
			}
206
		}
207
		if (!empty($bridge['maxage']))
208
			mwexec("/sbin/ifconfig {$bridgeif} maxage {$bridge['maxage']}");
209
		if (!empty($brige['fwdelay']))
210
			mwexec("/sbin/ifconfig {$bridgeif} fwddelay {$bridge['fwdelay']}");
211
		if (!empty($brige['hellotime']))
212
                        mwexec("/sbin/ifconfig {$bridgeif} hellotime {$bridge['hellotime']}");
213
		if (!empty($brige['priority']))
214
                        mwexec("/sbin/ifconfig {$bridgeif} priority {$bridge['priority']}");
215
		if (!empty($brige['holdcount']))
216
                        mwexec("/sbin/ifconfig {$bridgeif} holdcnt {$bridge['holdcnt']}");
217
		if (!empty($bridge['ifpriority'])) {
218
			$pconfig = explode(",", $bridge['ifpriority']);
219
			$ifpriority = array();
220
			foreach ($pconfig as $cfg) {
221
				$embcfg = explode(":", $cfg);
222
				foreach ($embcfg as $key => $value)
223
					$ifpriority[$key] = $value;
224
			}
225
			foreach ($ifpriority as $key => $value) {
226
				$realif = get_real_interface($key);
227
				mwexec("/sbin/ifconfig ${bridgeif} ifpriority {$realif} {$value}"); 
228
			}
229
		}
230
		if (!empty($bridge['ifpathcost'])) {
231
			$pconfig = explode(",", $bridges['ifpathcost']);
232
			$ifpathcost = array();
233
			foreach ($pconfig as $cfg) {
234
				$embcfg = explode(":", $cfg);
235
				foreach ($embcfg as $key => $value)
236
					$ifpathcost[$key] = $value;
237
			}
238
			foreach ($ifpathcost as $key => $value) {
239
                        	$realif = get_real_interface($key);
240
                        	mwexec("/sbin/ifconfig ${bridgeif} ifpathcost {$realif} {$value}");
241
                	}
242
		}
243
	}
244

    
245
	if ($bridge['maxaddr'] <> "")
246
		mwexec("/sbin/ifconfig {$bridgeif} maxaddr {$bridge['maxaddr']}");
247
        if ($bridge['timeout'] <> "")
248
                mwexec("/sbin/ifconfig {$bridgeif} timeout {$bridge['timeout']}");
249
        if ($bridge['span'] <> "") {
250
		$realif = get_real_interface($bridge['span']);
251
                mwexec("/sbin/ifconfig {$bridgeif} span {$realif}");
252
	}
253
	if (!empty($bridge['edge'])) {
254
        	$edgeifs = explode(',', $bridge['edge']);
255
        	foreach ($edgeifs as $edgeif) {
256
			$realif = get_real_interface($edgeif);
257
                	mwexec("/sbin/ifconfig {$bridgeif} edge {$realif}");
258
        	}
259
	}
260
	if (!empty($bridge['autoedge'])) {
261
        	$edgeifs = explode(',', $bridge['autoedge']);
262
        	foreach ($edgeifs as $edgeif) {
263
                	$realif = get_real_interface($edgeif);
264
                	mwexec("/sbin/ifconfig {$bridgeif} -autoedge {$realif}");
265
        	}
266
	}
267
	if (!empty($bridge['ptp'])) {
268
        	$ptpifs = explode(',', $bridge['ptp']);
269
        	foreach ($ptpifs as $ptpif) {
270
                	$realif = get_real_interface($ptpif);
271
                	mwexec("/sbin/ifconfig {$bridgeif} ptp {$realif}");
272
        	}
273
	}
274
	if (!empty($bridge['autoptp'])) {
275
        	$ptpifs = explode(',', $bridge['autoptp']);
276
        	foreach ($ptpifs as $ptpif) {
277
                	$realif = get_real_interface($ptpif);
278
                	mwexec("/sbin/ifconfig {$bridgeif} -autoptp {$realif}");
279
        	}
280
	}
281
	if (!empty($bridge['static'])) {
282
        	$stickyifs = explode(',', $bridge['static']);
283
        	foreach ($stickyifs as $stickyif) {
284
                	$realif = get_real_interface($stickyif);
285
                	mwexec("/sbin/ifconfig {$bridgeif} sticky {$realif}");
286
        	}
287
	}
288
	if (!empty($bridge['private'])) {
289
        	$privateifs = explode(',', $bridge['private']);
290
        	foreach ($privateifs as $privateif) {
291
                	$realif = get_real_interface($privateif);
292
               	 	mwexec("/sbin/ifconfig {$bridgeif} private {$realif}");
293
        	}
294
	}
295

    
296
	if($bridgeif)
297
		interfaces_bring_up($bridgeif);	
298
	else 
299
		log_error("bridgeif not defined -- could not bring interface up");
300

    
301
	return $bridgeif;
302
}
303

    
304
function interfaces_lagg_configure() 
305
{
306
        global $config;
307

    
308
        $i = 0;
309
        if (is_array($config['laggs']['lagg']) && count($config['laggs']['lagg'])) {
310
                foreach ($config['laggs']['lagg'] as $lagg) {
311
                        if(empty($lagg['laggif']))
312
                                $lagg['laggif'] = "lagg{$i}";
313
                        /* XXX: Maybe we should report any errors?! */
314
                        interface_lagg_configure($lagg);
315
                        $i++;
316
                }
317
        }
318
}
319

    
320
function interface_lagg_configure(&$lagg) 
321
{
322
        global $config, $g;
323

    
324
        if (!is_array($lagg))
325
		return -1;
326

    
327
	$members = explode(',', $lagg['members']);
328
	if (!count($members))
329
		return -1;
330
	
331
	$checklist = get_interface_list();
332

    
333
	if ($g['booting'] || !(empty($lagg['laggif']))) {
334
                mwexec("/sbin/ifconfig {$lagg['laggif']} destroy");
335
                mwexec("/sbin/ifconfig {$lagg['laggif']} create");
336
                $laggif = $lagg['laggif'];
337
        } else
338
                $laggif = exec("/sbin/ifconfig lagg create");
339

    
340
	mwexec("/sbin/ifconfig {$laggif} {$lagg['proto']}");
341

    
342
	/* Calculate smaller mtu and enforce it */
343
        $smallermtu = 0;
344
        foreach ($members as $member) {
345
                $realif = get_real_interface($member);
346
                $mtu = get_interface_mtu($realif);
347
		if ($smallermtu == 0 && !empty($mtu))
348
			$smallermtu = $mtu;
349
                else if (!empty($mtu) && $mtu < $smallermtu)
350
                        $smallermtu = $mtu;
351
        }
352

    
353
	/* Just in case anything is not working well */
354
        if ($smallermtu == 0)
355
                $smallermtu = 1500;
356

    
357
	foreach ($members as $member) {
358
		if (!array_key_exists($member, $checklist))
359
			continue;
360
		$realif =  escapeshellarg($member);
361
		/* make sure the parent interface is up */
362
		mwexec("/sbin/ifconfig {$realif} mtu {$smallermtu}");
363
		if($realif)
364
			interfaces_bring_up($realif);
365
		else 
366
			log_error("could not bring realif up -- foreach(memebers)");
367
		mwexec("/sbin/ifconfig {laggif} laggport {$realif}");
368
	}
369
	
370
	interfaces_bring_up($laggif);
371

    
372
	return $laggif;
373
}
374

    
375
function interfaces_gre_configure() 
376
{
377
        global $config;
378

    
379
        $i = 0;
380
        if (is_array($config['gres']['gre']) && count($config['gres']['gre'])) {
381
                foreach ($config['gres']['gre'] as $gre) {
382
                        if(empty($gre['greif']))
383
                                $gre['greif'] = "gre{$i}";
384
                        /* XXX: Maybe we should report any errors?! */
385
                        interface_gre_configure($gre);
386
                        $i++;
387
                }
388
        }
389
}
390

    
391
function interface_gre_configure(&$gre) 
392
{
393
        global $config, $g;
394

    
395
	if (!is_array($gre))
396
		return -1;
397

    
398
	$realif = get_real_interface($gre['if']);
399
	$realifip = get_interface_ip($gre['if']);
400

    
401
	/* make sure the parent interface is up */
402
	interfaces_bring_up($realif);
403

    
404
	if ($g['booting'] || !(empty($gre['greif']))) {
405
		mwexec("/sbin/ifconfig {$gre['greif']} destroy");
406
		mwexec("/sbin/ifconfig {$gre['greif']} create");
407
		$greif = $gre['greif'];
408
	} else {
409
		$greif = exec("/sbin/ifconfig gre create");
410
	}
411

    
412
	/* Do not change the order here for more see gre(4) NOTES section. */
413
	mwexec("/sbin/ifconfig {$greif} tunnel {$realifip} {$gre['remote-addr']}");
414
	mwexec("/sbin/ifconfig {$greif} {$gre['tunnel-local-addr']} {$gre['tunnel-remote-addr']} netmask " . gen_subnet_mask($gre['tunnel-remote-net']));
415
	if (isset($gre['link0']) && $gre['link0'])
416
		mwexec("/sbin/ifconfig {$greif} link0");
417
	if (isset($gre['link1']) && $gre['link1'])
418
		mwexec("/sbin/ifconfig {$greif} link1");
419
	if (isset($gre['link2']) && $gre['link2'])
420
		mwexec("/sbin/ifconfig {$greif} link2");
421

    
422
	if($greif)
423
		interfaces_bring_up($greif);
424
	else 
425
		log_error("Could not bring greif up -- variable not defined.");
426

    
427
	mwexec("/sbin/route add {$gre['remote-addr']}/{$gre['tunnel-remote-net']} {$realifip}");
428
	file_put_contents("/tmp/{$greif}_router", $gre['tunnel-remote-addr']);
429

    
430
	return $greif;
431
}
432

    
433
function interfaces_gif_configure() 
434
{
435
        global $config;
436

    
437
        $i = 0;
438
        if (is_array($config['gifs']['gif']) && count($config['gifs']['gif'])) {
439
                foreach ($config['gifs']['gif'] as $gif) {
440
                        if(empty($gif['gifif']))
441
                                $gre['gifif'] = "gif{$i}";
442
                        /* XXX: Maybe we should report any errors?! */
443
                        interface_gif_configure($gif);
444
                        $i++;
445
                }
446
        }
447
}
448

    
449
function interface_gif_configure(&$gif) 
450
{
451
        global $config, $g;
452

    
453
        if (!is_array($gif))
454
                return -1;
455

    
456
        $realif = get_real_interface($gif['if']);
457
        $realifip = get_interface_ip($gif['if']);
458

    
459
        /* make sure the parent interface is up */
460
		if($realif)
461
			interfaces_bring_up($realif);
462
		else 
463
			log_error("could not bring realif up -- variable not defined -- interface_gif_configure()");
464

    
465
        if ($g['booting'] || !(empty($gif['gifif']))) {
466
                mwexec("/sbin/ifconfig {$gif['gifif']} destroy");
467
                mwexec("/sbin/ifconfig {$gif['gifif']} create");
468
                $gifif = $gif['gifif'];
469
        } else
470
                $gifif = exec("/sbin/ifconfig gif create");
471

    
472
        /* Do not change the order here for more see gif(4) NOTES section. */
473
        mwexec("/sbin/ifconfig {$gifif} tunnel {$realifip} {$gif['remote-addr']}");
474
        mwexec("/sbin/ifconfig {$gifif} {$gif['tunnel-local-addr']} {$gif['tunnel-remote-addr']} netmask " . gen_subnet_mask($gif['tunnel-remote-net']));
475
        if (isset($gif['link0']) && $gif['link0'])
476
                mwexec("/sbin/ifconfig {$gifif} link0");
477
        if (isset($gif['link1']) && $gif['link1'])
478
                mwexec("/sbin/ifconfig {$gifif} link1");
479
		if($gifif)
480
			interfaces_bring_up($gifif);
481
		else
482
		 	log_error("could not bring gifif up -- variable not defined");
483
        mwexec("/sbin/route add {$gif['remote-addr']}/{$gif['tunnel-remote-net']} {$realifip}");
484
		file_put_contents("/tmp/{$gifif}_router", $gif['tunnel-remote-addr']);
485

    
486
        return $gifif;
487
}
488

    
489
function interfaces_configure() 
490
{
491
	global $config, $g;
492

    
493
	/* set up VLAN virtual interfaces */
494
	interfaces_vlan_configure();
495

    
496
	/* set up LAGG virtual interfaces */
497
	interfaces_lagg_configure();
498

    
499
	/* Set up PPP interfaces */
500
	interfaces_ppp_configure();
501

    
502
	$iflist = get_configured_interface_with_descr();
503
	$delayed_list = array();
504
	$bridge_list = array();
505
	
506
	foreach($iflist as $if => $ifname) {
507
		$realif = $config['interfaces'][$if]['if'];
508

    
509
		if (strstr($realif, "bridge")) 
510
			$bridge_list[$if] = $ifname;
511
		else if (strstr($realif, "gre"))
512
			$delayed_list[$if] = $ifname;
513
		else if (strstr($realif, "gif"))
514
			$delayed_list[$if] = $ifname;
515
		else {
516
			if ($g['booting'])
517
				echo "Configuring {$ifname} interface...";
518
        	if($g['debug'])
519
				log_error("Configuring {$ifname}");
520

    
521
			interface_configure($if);
522

    
523
			if ($g['booting']) 
524
                		echo "done.\n";
525
		}
526
	}
527

    
528
	/* set up GRE virtual interfaces */
529
	interfaces_gre_configure();
530

    
531
	/* set up GIF virtual interfaces */
532
	interfaces_gif_configure();
533
	
534
	foreach ($delayed_list as $if => $ifname) {
535
		if ($g['booting'])
536
			echo "Configuring {$ifname} interface...";
537
        if($g['debug'])
538
        	log_error("Configuring {$ifname}");
539

    
540
		interface_configure($if);
541

    
542
		if ($g['booting'])
543
			echo "done.\n";
544
	}
545

    
546
	/* set up BRIDGe virtual interfaces */
547
	interfaces_bridge_configure();
548

    
549
	foreach ($bridge_list as $if => $ifname) {
550
		if ($g['booting'])
551
			echo "Configuring {$ifname} interface...";
552
		if($g['debug'])
553
			log_error("Configuring {$ifname}");
554

    
555
		interface_configure($if);
556

    
557
		if ($g['booting'])
558
			echo "done.\n";
559
	}
560

    
561
	/* bring up carp interfaces */
562
	interfaces_carp_configure();
563

    
564
	/* bring ip IP aliases */
565
	interfaces_ipalias_configure();
566

    
567
	if (!$g['booting']) {
568
		/* reconfigure static routes (kernel may have deleted them) */
569
		system_routing_configure();
570

    
571
		/* reload IPsec tunnels */
572
		vpn_ipsec_configure();
573

    
574
		/* reload dhcpd (interface enabled/disabled status may have changed) */
575
		services_dhcpd_configure();
576

    
577
		/* restart dnsmasq */
578
		services_dnsmasq_configure();
579

    
580
		/* reload captive portal */
581
		captiveportal_configure();
582

    
583
		/* set the reload filter dity flag */
584
		filter_configure();
585
	}
586

    
587
	return 0;
588
}
589

    
590
function interface_reconfigure($interface = "wan") 
591
{
592
	interface_bring_down($interface);
593
	sleep(1);
594
	interface_configure($interface);
595
}
596

    
597
function interface_bring_down($interface = "wan") 
598
{
599
	global $config, $g;
600

    
601
	$ifcfg = $config['interfaces'][$interface];
602

    
603
	$realif = get_real_interface($interface);
604

    
605
	mwexec("/usr/sbin/arp -d -i {$realif} -a");
606

    
607
        /* remove interface up file if it exists */
608
        unlink_if_exists("{$g['tmp_path']}/{$interface}up");
609
        unlink_if_exists("{$g['vardb_path']}/{$interface}ip");
610
        unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
611

    
612
	switch ($ifcfg['ipaddr']) {
613
	case "pppoe":
614
		killbypid("{$g['varrun_path']}/pppoe_{$interface}.pid");
615
        	sleep(2);
616
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
617
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.links");
618
		break;
619
	case "pptp":
620
		killbypid("{$g['varrun_path']}/pptp_{$interface}.pid");
621
        	sleep(2);
622
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
623
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.links");
624
		break;
625
	case "carpdev-dhcp":
626
		/* 
627
		 * NB: When carpdev gets enabled it would be better to be handled as all
628
		 *	other interfaces! 
629
		 */
630
	case "dhcp":
631
        	$pid = find_dhclient_process($interface);
632
        	if($pid)
633
                	mwexec("kill {$pid}");
634
               	sleep(1);
635
                unlink_if_exists("{$g['varetc_path']}/dhclient_{$interface}.conf");
636
               	mwexec("/sbin/ifconfig {$realinterface} delete down");
637
		break;
638
	default:
639
		mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete down");
640
		break;
641
	}
642

    
643
	if (preg_match("/^tun|^ppp|^ovpn|^gif|^gre|^lagg|^bridge|^vlan/i", $realif))
644
                mwexec("/sbin/ifconfig {$realif} destroy");
645
	
646
	unlink_if_exists("/tmp/{$realif}_router");
647
	return;
648
}
649

    
650
function interfaces_ppp_configure() 
651
{
652
        global $config;
653

    
654
        $i = 0;
655
        if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
656
                foreach ($config['ppps']['ppp'] as $ppp) {
657
                        if(empty($ppp['pppif']))
658
                                $ppp['pppif'] = "ppp{$i}";
659
                        /* XXX: Maybe we should report any errors?! */
660
                        interface_ppp_configure($ppp);
661
                        $i++;
662
                }
663
        }
664
}
665

    
666
function interface_ppp_configure($ifcfg) 
667
{
668
	global $config, $g;
669
	
670
	/* Remove  the /dev/ from the device name. */
671
	$dev = substr($ifcfg['port'], 5);
672

    
673
	$realif  = $ifcfg['pppif'];
674
	if ($realif <> "") {
675
		$i = 0;
676
		while ($realif != "ppp{$i}")
677
			$i++;
678
		if(file_exists("/var/run/ppp{$i}.pid")) {
679
			$pid = trim(file_get_contents("/var/run/ppp{$i}.pid"));
680
			mwexec("kill {$pid}");
681
		}
682
	}
683
	
684
	if ($g['booting'] || $realif <> "") {
685
                mwexec("/sbin/ifconfig {$realif} destroy");
686
                mwexec("/sbin/ifconfig {$realif} create");
687
        } else
688
                $realif = exec("/sbin/ifconfig ppp create");
689

    
690

    
691
	$peerfile = "lcp-echo-failure 0\n";
692
	$peerfile .= "lcp-echo-interval 0\n";
693
	$peerfile .= "connect /etc/ppp/peers/ppp{$dev}-connect-chat\n";
694
	//$peerfile .= "disconnect /etc/ppp/peers/ppp{$dev}-disconnect-chat\n";
695
	$peerfile .= "{$ifcfg['port']} {$ifcfg['linespeed']}\n";
696
	$peerfile .= "crtscts\n";
697
	if ($ifcfg['connect-max-attempts'] <> "")
698
		$peerfile .= "connect-max-attempts {$ifcfg['connect-max-attempts']}";
699
	$peerfile .= "local\n";
700
	if ($ifcfg['localip'] <> "") {
701
		$peerfile .= ":{$ifcfg['gateway']}\n";
702
		$peerfile .= "{$ifcfg['localip']}:{$ifcfg['gateway']}";
703
	} else if ($ifcfg['gateway'] <> "") {
704
		$peerfile .= ":{$ifcfg['gateway']}\n";
705
		$peerfile .= "noipdefault\n";
706
	} else 
707
		$peerfile .= "noipdefault\n";
708
	$peerfile .= "ipcp-accept-local\n";
709
	$peerfile .= "novj\n";
710
	$peerfile .= "nobsdcomp\n";
711
	$peerfile .= "novjccomp\n";
712
	$peerfile .= "nopcomp\n";
713
	$peerfile .= "noaccomp\n";
714
	$peerfile .= "noauth\n";
715
	//$peerfile .= "nodetach\n";
716
	$peerfile .= "persist\n";
717
	$peerfile .= "debug\n";
718
	// KD - test
719
	//$peerfile .= "defaultroute\n";
720
	//$peerfile .= "nodetach\n";
721
	// KD - so I know where to look!
722
	$peerfile .= "# created by /etc/inc/interfaces.inc\n";
723

    
724
	// Added single quotes to some strings below:
725
	// the \rAT is *always* going to need it
726
	// and the phone number on a GSM connection ends in a # char
727
	// Kevin Dawson, 22 Jan 2008
728
	// Refer Andrew Curtis
729
			
730
	$chatfile = "#!/bin/sh\n";
731
	$chatfile .= "exec chat \\\n";
732
	$chatfile .= "TIMEOUT 5 \\\n";
733
	$chatfile .= "ECHO ON \\\n";
734
	$chatfile .= "ABORT '\\nBUSY\\r' \\\n";
735
	$chatfile .= "ABORT '\\nERROR\\r' \\\n";
736
	$chatfile .= "ABORT '\\nNO ANSWER\\r' \\\n";
737
	$chatfile .= "ABORT '\\nNO CARRIER\\r' \\\n";
738
	$chatfile .= "ABORT '\\nNO DIALTONE\\r' \\\n";
739
	$chatfile .= "ABORT '\\nRINGING\\r\\n\\r\\nRINGING\\r' \\\n";
740
	// KD
741
	$chatfile .= "'' '\\rAT' \\\n";
742
	$chatfile .= "TIMEOUT 12 \\\n";
743
	$chatfile .= "OK ATH \\\n";
744
	$chatfile .= "OK ATE1 \\\n";
745
	$chatfile .= "OK 'AT+CGDCONT=1,\"IP\",\"{$ifcfg['ap']}\"' \\\n";
746
	// KD
747
	$chatfile .= "OK 'ATD{$ifcfg['phone']}' \\\n";
748
	$chatfile .= "TIMEOUT 22 \\\n";
749
	if ($ifcfg['username'] <> "") {
750
		$chatfile .= "CONNECT \"\" TIMEOUT 10 \\\n";
751
		$chatfile .= "ogin:-\\r-ogin: {$ifcfg['username']}\\\n";
752
		$chatfile .= " TIMEOUT 5 sword: {$ifcfg['password']} \\\n";
753
	} else
754
		$chatfile .= "CONNECT \"\" \\\n";
755
	$chatfile .= "SAY \"\\nConnected.\"\n";
756

    
757
	config_lock();
758
	conf_mount_rw();
759
	safe_mkdir("/etc/ppp/peers", "0755");
760
	file_put_contents("/etc/ppp/peers/ppp_{$dev}", $peerfile);
761
	file_put_contents("/etc/ppp/peers/ppp{$dev}-connect-chat", $chatfile);
762
	chmod("/etc/ppp/peers/ppp{$dev}-connect-chat", 0755);
763
	conf_mount_ro();
764
	config_unlock();
765
	
766
	sleep(1);
767
	mwexec("/usr/sbin/pppd call ppp_{$dev}");
768

    
769
	return $realif;
770
}
771

    
772
function interfaces_carp_configure() 
773
{
774
	global $g, $config;
775
	$balanacing = "";
776
	$pfsyncinterface = "";
777
	$pfsyncenabled = "";
778
	if(isset($config['system']['developerspew'])) {
779
		$mt = microtime();
780
		echo "interfaces_carp_configure() being called $mt\n";
781
	}
782
	// Prepare CmdCHAIN that will be used to execute commands.
783
	$cmdchain = new CmdCHAIN();	
784
	$carp_instances_counter = 0;
785
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
786
	/* destroy previous interfaces */
787
	for($x=0; $x<$total_carp_interfaces_defined; $x++) 
788
		$cmdchain->add("Delete CARP interface", "/sbin/ifconfig carp{$x} delete", false);
789
	if ($g['booting']) {
790
		echo "Configuring CARP interfaces...";
791
		mute_kernel_msgs();
792
	}
793
	/* suck in configuration items */
794
	if($config['installedpackages']['carpsettings']) 
795
		if($config['installedpackages']['carpsettings']['config']) {
796
		foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
797
			$pfsyncenabled = $carp['pfsyncenabled'];
798
			$balanacing = $carp['balancing'];
799
			$pfsyncinterface = $carp['pfsyncinterface'];
800
			$pfsyncpeerip = $carp['pfsyncpeerip'];
801
		}
802
	} else {
803
		unset($pfsyncinterface);
804
		unset($balanacing);
805
		unset($pfsyncenabled);
806
	}
807
	$cmdchain->add("Allow CARP", "/sbin/sysctl net.inet.carp.allow=1", true);			
808
	if($balanacing) {
809
		$cmdchain->add("Enable CARP ARP-balancing", "/sbin/sysctl net.inet.carp.arpbalance=1", true);
810
		$cmdchain->add("Disallow CARP preemption", "/sbin/sysctl net.inet.carp.preempt=0", true);
811
	} else {
812
		$cmdchain->add("Enable CARP preemption", "/sbin/sysctl net.inet.carp.preempt=1", true);		
813
	}
814
	$cmdchain->add("Enable CARP logging", "/sbin/sysctl net.inet.carp.log=2", true);
815
	$carp_sync_int = convert_friendly_interface_to_real_interface_name($pfsyncinterface);
816
	if($g['booting']) {
817
		/*    install rules to alllow pfsync to sync up during boot
818
		 *    carp interfaces will remain down until the bootup sequence finishes
819
		 */
820
		exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
821
		exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
822
		exec("echo pass out quick from any to any keep state >> /tmp/rules.boot");
823
		exec("/sbin/pfctl -f /tmp/rules.boot");
824
	}
825
	/* setup pfsync interface */
826
	if($carp_sync_int and $pfsyncenabled) {
827
		if($pfsyncpeerip) {
828
			$cmdchain->add("Bring up pfsync0 syncpeer", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} syncpeer {$pfsyncpeerip} up", false);						
829
		} else {
830
			$cmdchain->add("Bring up pfsync0 syncdev", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up", false);			
831
		}
832
	} else {
833
		$cmdchain->add("Bring up pfsync0", "/sbin/ifconfig pfsync0 syncdev lo0 up", false);						
834
	}
835
	//$fd = fopen("/tmp/carp.sh", "w");
836
	$viparr = &$config['virtualip']['vip'];
837
	if($config['virtualip']['vip']) {
838
		$cmdchain->add("Allow CARP.", "/sbin/sysctl net.inet.carp.allow=1", true);				
839
	} else {
840
		$viparr = array();
841
		$cmdchain->add("Disallow CARP.", "/sbin/sysctl net.inet.carp.allow=0", true);		
842
	}
843
	if(!$viparr and $config['interfaces']['wan']['ipaddr'] == "carpdev-dhcp") {
844
		/* no vips exist but we need to bring up carpdev... */
845
		$viparr_temp = array();
846
		$viparr_temp['advskew'] = "200";
847
		$viparr_temp['vhid'] = "1";
848
		$viparr_temp['mode'] = "carpdev-dhcp";
849
		$viparr_temp['password'] = $config['system']['hostname'] . "pfS";
850
		$viparr = $viparr_temp;
851
	}
852
	
853
	if($g['debug'])
854
		$cmdchain->setdebug(); // optional for verbose logging
855
	$cmdchain->execute();
856
	
857
	// Reset CmdCHAIN
858
	$cmdchain->clear();
859

    
860
	if(is_array($viparr))
861
	foreach ($viparr as $vip) {
862
		$vip_password = $vip['password'];
863
		$vip_password = str_replace(" ", "", $vip_password);
864
		if($vip['password'] != "")
865
                	$password = " pass \"" . $vip_password . "\"";
866
		$interface = filter_translate_type_to_real_interface($vip['interface']);
867
		$carpint = "carp" . $carp_instances_counter;
868

    
869
		switch ($vip['mode']) {
870
		case "carp":
871
			/* ensure CARP IP really exists prior to loading up */
872
			$found = false;
873
			$iflist = get_configured_interface_list();
874
			foreach($iflist as $if) {
875
				$ww_subnet_ip = $config['interfaces'][$if]['ipaddr'];
876
				$ww_subnet_bits = $config['interfaces'][$if]['subnet'];
877
				if (ip_in_subnet($vip['subnet'], gen_subnet($ww_subnet_ip, $ww_subnet_bits) . "/" . $ww_subnet_bits))
878
					$found = true;
879
			}
880
			if($found == false) {
881
				file_notice("CARP", "Sorry but we could not find a matching real interface subnet for the virtual IP address {$vip['subnet']}.", "Firewall: Virtual IP", "");
882
				continue;
883
			}			
884
			/* create the carp interface and setup */
885
			$cmdchain->add("create CARP interface", "/sbin/ifconfig {$carpint} create", false);
886

    
887
			/* invalidate interface cache */
888
			get_interface_arr(true);
889
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
890
			$cmdchain->add("config CARP interface", "/sbin/ifconfig {$carpint} " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . " advskew " . $vip['advskew'] . $password, false);
891
			$cmdchain->add("bring CARP interface UP", "/sbin/ifconfig {$carpint} up", false);
892
			$carp_instances_counter++;
893
			break;
894
		case "carpdev-dhcp":
895
			log_error("Found carpdev interface {$vip['interface']} on top of interface {$interface}");
896
			if(!empty($interface)) {
897
				
898
					$cmdchain->add("bring CARP parent interface UP", "/sbin/ifconfig {$interface} up", false);			
899
					$cmdchain->add("create CARP interface", "/sbin/ifconfig {$carpint} create", false);
900
					$cmdchain->add("bring CARP interface UP", "/sbin/ifconfig {$carpint} up", false);
901
					$cmdchain->add("assign CARP CarpDEV directive", "/sbin/ifconfig {$carpint} carpdev ". $interface . " vhid " . $vip['vhid'] . " advskew " . $vip['advskew'] . $password, false);
902
					$cmdchain->add("bring CARP interface UP", "/sbin/ifconfig {$carpint} up", false);
903

    
904
					/*
905
					 * XXX: BIG HACK but carpdev needs ip services active
906
					 * 	before even starting something as dhclient.
907
					 * 	I do not know if this is a feature or a bug
908
					 * 	but better than track it make it work ;) .
909
					 */
910
					//$fakeiptouse = "10.254.254." . ($carp_instances_counter+1);
911
					//$cmdchain->add("CarpDEV hack", "/sbin/ifconfig {$carpint} inet {$fakeiptouse}", false);
912

    
913
        			/* generate dhclient_wan.conf */
914
        			$fd = fopen("{$g['varetc_path']}/dhclient_{$carpint}.conf", "w");
915
        			if ($fd) {
916

    
917
        				$dhclientconf = "";
918

    
919
        				$dhclientconf .= <<<EOD
920
interface "{$carpint}" {
921
timeout 60;
922
retry 1;
923
select-timeout 0;
924
initial-interval 1;
925
script "/sbin/dhclient-script";
926
}
927

    
928
EOD;
929

    
930
 			        fwrite($fd, $dhclientconf);
931
        			fclose($fd);
932

    
933
        			/* fire up dhclient */
934
					$cmdchain->add("bring CARP dhclient UP", "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$carpint}.conf {$carpint} >/tmp/{$carpint}_output >/tmp/{$carpint}_error_output", false);
935
				} else {
936
					log_error("Error: cannot open dhclient_{$carpint}.conf in interfaces_carp_configure() for writing.\n");
937
					$cmdchain->add("bring CARP dhclient UP in background", "/sbin/dhclient -b {$carpint}", false);					
938
				}
939

    
940
        		$fout = fopen("/tmp/ifconfig_{$carpint}","w");
941
        		fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$carpint}.conf {$carpint}");
942
        		fclose($fout);
943

    
944
			} else {
945
				log_error("Could not determine CarpDEV parent interface for {$vip['descr']}.");
946
			}
947
			$carp_instances_counter++;
948
			break;
949
		}
950
	}
951

    
952
	if($g['debug'])
953
		$cmdchain->setdebug(); // optional for verbose logging
954
	// Execute built up command chain.
955
	$cmdchain->execute();	
956

    
957
	if ($g['booting']) {
958
		unmute_kernel_msgs();
959
		echo "done.\n";
960
	}
961

    
962
	/* update cache */
963
	if ($carp_instances_counter != find_number_of_created_carp_interfaces())
964
		find_number_of_created_carp_interfaces(true);
965

    
966
}
967

    
968
function interfaces_ipalias_configure() 
969
{
970
	global $g, $config;
971
	if(isset($config['system']['developerspew'])) {
972
		$mt = microtime();
973
		echo "interfaces_ipalias_configure() being called $mt\n";
974
	}
975
	$viparr = &$config['virtualip']['vip'];
976
	if(is_array($viparr)) {
977
		foreach ($viparr as $vip) {
978
			if ($vip['mode'] == "ipalias") {
979
				$if = get_real_interface($vip['interface']);
980
				mwexec("/sbin/ifconfig " . escapeshellarg($if) . " " . $vip['subnet'] . "/" . escapeshellarg($vip['subnet_bits']) . " alias"); 
981
			}
982
		}
983
	}
984
}
985

    
986
function interface_wireless_configure($if, $wlcfg) 
987
{
988
	global $config, $g;
989

    
990
	/*    open up a shell script that will be used to output the commands.
991
	 *    since wireless is changing a lot, these series of commands are fragile
992
     *    and will sometimes need to be verified by a operator by executing the command
993
     *    and returning the output of the command to the developers for inspection.  please
994
     *    do not change this routine from a shell script to individul exec commands.  -sullrich
995
	 */
996

    
997
	conf_mount_rw();
998

    
999
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
1000

    
1001
	$fd_set = fopen("/tmp/{$if}_setup.sh","w");
1002
	fwrite($fd_set, "#!/bin/sh\n");
1003
	fwrite($fd_set, "# {$g['product_name']} wireless configuration script.\n\n");
1004

    
1005
	fwrite($fd_set, "# enable shell debugging\n");
1006
	fwrite($fd_set, "set -x\n");
1007

    
1008
	/* set values for /path/program */
1009
	$hostapd = "/usr/sbin/hostapd";
1010
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
1011
	$ifconfig = "/sbin/ifconfig";
1012
	$killall = "/usr/bin/killall";
1013

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

    
1016
	/* Set a/b/g standard */
1017
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
1018

    
1019
	/* Set 802.11g protection mode */
1020
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
1021

    
1022
	/* set wireless channel value */
1023
	if(isset($wlcfg['channel']))
1024
		if($wlcfg['channel'] == "0")
1025
			$channel = "channel any";
1026
		else
1027
			$channel = "channel " . escapeshellarg($wlcfg['channel']);
1028

    
1029
	/* set Distance value */
1030
	if($wlcfg['distance'])
1031
		$distance = escapeshellarg($wlcfg['distance']);
1032

    
1033
	/* Set ssid */
1034
	if($wlcfg['ssid'])
1035
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
1036

    
1037
	/* Set wireless hostap mode */
1038
	if ($wlcfg['mode'] == "hostap")
1039
		$hostapmode = "mediaopt hostap";
1040
	else
1041
		$hostapmode = "-mediaopt hostap";
1042

    
1043
	/* Set wireless adhoc mode */
1044
	if ($wlcfg['mode'] == "adhoc")
1045
		$adhocmode = "mediaopt adhoc";
1046
	else
1047
		$adhocmode = "-mediaopt adhoc";
1048

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

    
1051
	/* handle hide ssid option */
1052
	if(isset($wlcfg['hidessid']['enable']))
1053
		$hidessid = "hidessid";
1054
	else
1055
		$hidessid = "-hidessid";
1056

    
1057
	/* handle pureg (802.11g) only option */
1058
	if(isset($wlcfg['pureg']['enable']))
1059
		$pureg = "mode 11g pureg";
1060
	else
1061
		$pureg = "-pureg";
1062

    
1063
	/* enable apbridge option */
1064
	if(isset($wlcfg['apbridge']['enable']))
1065
		$apbridge = "apbridge";
1066
	else
1067
		$apbridge = "-apbridge";
1068

    
1069
	/* handle turbo option */
1070
	if(isset($wlcfg['turbo']['enable']))
1071
		$turbo = "mediaopt turbo";
1072
	else
1073
		$turbo = "-mediaopt turbo";
1074

    
1075
	/* handle txpower setting */
1076
	if($wlcfg['txpower'] <> "")
1077
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
1078

    
1079
	/* handle wme option */
1080
	if(isset($wlcfg['wme']['enable']))
1081
		$wme = "wme";
1082
	else
1083
		$wme = "-wme";
1084

    
1085
	/* set up wep if enabled */
1086
    if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
1087
		if($wlcfg['wpa']['auth_algs'] == "1")
1088
			$wepset .= "authmode open wepmode on ";
1089
		else if($wlcfg['wpa']['auth_algs'] == "2")
1090
			$wepset .= "authmode shared wepmode on ";
1091
		else if($wlcfg['wpa']['auth_algs'] == "3")
1092
			$wepset .= "authmode mixed wepmode on ";
1093
		$i = 1;
1094
		foreach ($wlcfg['wep']['key'] as $wepkey) {
1095
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
1096
			if (isset($wepkey['txkey']))
1097
				$wepset .= "weptxkey {$i} ";
1098
			$i++;
1099
		}
1100
    } else {
1101
    	$wepset .= "authmode open wepmode off ";
1102
	}
1103

    
1104
	/* generate wpa_supplicant/hostap config if wpa is enabled */
1105

    
1106
	switch ($wlcfg['mode']) {
1107
		case 'bss':
1108
			if (isset($wlcfg['wpa']['enable'])) {
1109

    
1110
				$wpa .= <<<EOD
1111
ctrl_interface={$g['varrun_path']}/wpa_supplicant
1112
ctrl_interface_group=0
1113
ap_scan=1
1114
#fast_reauth=1
1115
network={
1116
ssid="{$wlcfg['ssid']}"
1117
scan_ssid=1
1118
priority=5
1119
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
1120
psk="{$wlcfg['wpa']['passphrase']}"
1121
pairwise={$wlcfg['wpa']['wpa_pairwise']}
1122
group={$wlcfg['wpa']['wpa_pairwise']}
1123
}
1124
EOD;
1125

    
1126
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
1127
				fwrite($fd, "{$wpa}");
1128
				fclose($fd);
1129

    
1130
				fwrite($fd_set, kill_wpasupplicant($if));
1131
			}
1132
		break;
1133

    
1134
		case 'hostap':
1135
			if (isset($wlcfg['wpa']['enable'])) {
1136
				$wpa .= <<<EOD
1137
interface={$if}
1138
driver=bsd
1139
logger_syslog=-1
1140
logger_syslog_level=0
1141
logger_stdout=-1
1142
logger_stdout_level=0
1143
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
1144
ctrl_interface={$g['varrun_path']}/hostapd
1145
ctrl_interface_group=wheel
1146
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
1147
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
1148
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
1149
ssid={$wlcfg['ssid']}
1150
debug={$wlcfg['wpa']['debug_mode']}
1151
auth_algs={$wlcfg['wpa']['auth_algs']}
1152
wpa={$wlcfg['wpa']['wpa_mode']}
1153
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
1154
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
1155
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
1156
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
1157
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
1158
wpa_passphrase={$wlcfg['wpa']['passphrase']}
1159
ieee8021x={$wlcfg['wpa']['ieee8021x']}
1160
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
1161
#rsn_preauth=1
1162
#rsn_preauth_interfaces=eth0
1163
EOD;
1164

    
1165
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
1166
				fwrite($fd, "{$wpa}");
1167
				fclose($fd);
1168

    
1169
				fwrite($fd_set, kill_hostapd($if));
1170
			}
1171
		break;
1172

    
1173
		case 'adhoc':
1174
			fwrite($fd_set, kill_hostapd($if));
1175
			fwrite($fd_set, kill_wpasupplicant($if));
1176
		break;
1177
	}
1178

    
1179
	/*
1180
	 *    all variables are set, lets start up everything
1181
     */
1182

    
1183
	/* set ack timers according to users preference (if he/she has any) */
1184
	if($distance) {
1185
		fwrite($fd_set, "# Enable ATH distance settings\n");
1186
		fwrite($fd_set, "/sbin/athctrl.sh -i {$if} -d {$distance}\n");
1187
	}
1188

    
1189
	$standard_no_turbo = str_replace(" Turbo", "", $standard);
1190

    
1191
	$settings = <<<EOD
1192

    
1193
{$ifconfig} {$if} down
1194
{$ifconfig} {$if} {$standard_no_turbo}
1195
{$ifconfig} {$if} {$channel}
1196
{$ifconfig} {$if} {$turbo}
1197
{$ifconfig} {$if} {$ssid}
1198
{$ifconfig} {$if} {$hidessid}
1199
{$ifconfig} {$if} {$adhocmode}
1200
{$ifconfig} {$if} {$protmode}
1201
{$ifconfig} {$if} {$pureg}
1202
{$ifconfig} {$if} {$apbridge}
1203
{$ifconfig} {$if} {$wme}
1204
{$ifconfig} {$if} {$wepset}
1205
{$ifconfig} {$if} {$txpower}
1206
{$ifconfig} {$if} {$hostapmode}
1207
{$ifconfig} {$if} up
1208

    
1209
EOD;
1210

    
1211
	/* write out above <<EOD stuff */
1212
	fwrite($fd_set, $settings);
1213

    
1214
	if (isset($wlcfg['wpa']['enable'])) {
1215
		if ($wlcfg['mode'] == "bss")
1216
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
1217
		if ($wlcfg['mode'] == "hostap")
1218
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
1219
	}
1220

    
1221
	fclose($fd_set);
1222

    
1223
	conf_mount_ro();
1224

    
1225
	/* execute commands now in shell */
1226
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
1227
	sleep(2);
1228
	// XXX: ermal - This seems like not needed!? 
1229
	//mwexec("/bin/sh /tmp/{$if}_setup.sh");
1230

    
1231
	return 0;
1232

    
1233
}
1234

    
1235
function kill_hostapd($interface) 
1236
{
1237
	return "/bin/ps awwuxx | grep hostapd | grep $interface | awk '{ print \$2 }' | xargs kill\n";
1238
}
1239

    
1240
function kill_wpasupplicant($interface) 
1241
{
1242
	return "/bin/ps awwuxx | grep wpa_supplicant | grep $interface | awk '{ print \$2 }' | xargs kill\n";
1243
}
1244

    
1245
function find_dhclient_process($interface) 
1246
{
1247
       	$realinterface = get_real_interface($interface);
1248
	if($realinterface)
1249
		$pid = `ps awwwux | grep dhclient | grep -v grep | grep {$realinterface} | awk '{ print \$2 }'`;
1250
	return $pid;
1251
}
1252

    
1253
function interface_configure($interface = "wan") 
1254
{
1255
	global $config, $g;
1256

    
1257
	$wancfg = $config['interfaces'][$interface];
1258

    
1259
	$realif = get_real_interface($interface);
1260

    
1261
	if(!$g['booting']) {
1262
		/* remove all addresses first */
1263
		while (mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " -alias", true) == 0);
1264
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
1265

    
1266
		interface_bring_down($interface);
1267
	}
1268
	/* wireless configuration? */
1269
	if (is_array($wancfg['wireless']))
1270
		interface_wireless_configure($realif, $wancfg['wireless']);
1271

    
1272
	if ($wancfg['spoofmac']) {
1273
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
1274
			" link " . escapeshellarg($wancfg['spoofmac']));
1275
	}  else {
1276
		$mac = get_interface_mac_address($wancfg['if']);
1277
		if($mac == "ff:ff:ff:ff:ff:ff") {
1278
			/*   this is not a valid mac address.  generate a
1279
			 *   temporary mac address so the machine can get online.
1280
			 */
1281
			echo "Generating new MAC address.";
1282
			$random_mac = generate_random_mac_address();
1283
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
1284
				" link " . escapeshellarg($random_mac));
1285
			$wancfg['spoofmac'] = $random_mac;
1286
			write_config();
1287
			file_notice("MAC Address altered", "The INVALID MAC address (ff:ff:ff:ff:ff:ff) on interface {$realif} has been automatically replaced with {$random_mac}", "Interfaces");
1288
		}
1289
	}
1290

    
1291
	/* media */
1292
	if ($wancfg['media'] || $wancfg['mediaopt']) {
1293
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
1294
		if ($wancfg['media'])
1295
			$cmd .= " media " . escapeshellarg($wancfg['media']);
1296
		if ($wancfg['mediaopt'])
1297
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
1298
		mwexec($cmd);
1299
	}
1300

    
1301
	/* invalidate interface cache */
1302
        get_interface_arr(true);
1303

    
1304
	switch ($wancfg['ipaddr']) {
1305

    
1306
		case 'carpdev-dhcp':
1307
			interface_carpdev_dhcp_configure($interface);
1308
			break;
1309
		case 'dhcp':
1310
			interface_dhcp_configure($interface);
1311
			break;
1312

    
1313
		case 'pppoe':
1314
			interface_pppoe_configure($interface);
1315
			break;
1316

    
1317
		case 'pptp':
1318
			interface_pptp_configure($interface);
1319
			break;
1320

    
1321
		default:
1322
			if ($wancfg['ipaddr'] <> "" && $wancfg['subnet'] <> "") {
1323
				if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
1324
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " " .
1325
						escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
1326
						" " . escapeshellarg($wancfg['pointtopoint']) . " up");
1327
				} else {
1328
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) .
1329
						" " . escapeshellarg($wancfg['ipaddr'] . "/" . 
1330
						$wancfg['subnet']));
1331
				}
1332
			}
1333

    
1334
			if (is_ipaddr($wancfg['gateway']))
1335
				file_put_contents("/tmp/{$realif}_router", $wancfg['gateway']);
1336
	}
1337
	if($wancfg['if'])
1338
		interfaces_bring_up($wancfg['if']);
1339
	else 
1340
		log_error("Could not bring wancfg['if'] up -- variable not defined in interface_configure()");
1341
	
1342
	if (!$g['booting']) {
1343
		if (link_interface_to_gre($interface)) {
1344
			foreach ($config['gres']['gre'] as $gre)
1345
				if ($gre['if'] == $interface)
1346
					interface_gre_configure($gre);
1347
		}
1348
		if (link_interface_to_gif($interface)) {
1349
                	foreach ($config['gifs']['gif'] as $gif)
1350
				if ($gif['if'] == $interface)
1351
                        		interface_gre_configure($gif);
1352
        	}
1353
		if (link_interface_to_bridge($interface)) {
1354
			foreach ($config['bridges']['bridged'] as $bridge)
1355
				if (stristr($bridge['members'], "{$interface}"))
1356
					interface_bridge_configure($bridge);
1357
		}
1358

    
1359
		/* XXX: Shouldn't the caller do this?! */
1360
		/* XXX */
1361
		if ($interface = "lan")
1362
			/* make new hosts file */
1363
                	system_hosts_generate();
1364

    
1365
		/* reconfigure static routes (kernel may have deleted them) */
1366
		system_routing_configure();
1367

    
1368
		/* set the reload filter dity flag */
1369
		filter_configure();
1370

    
1371
		/* reload ipsec tunnels */
1372
		vpn_ipsec_configure();
1373

    
1374
		/* update dyndns */
1375
		services_dyndns_configure();
1376

    
1377
		/* force DNS update */
1378
		services_dnsupdate_process();
1379

    
1380
		/* restart dnsmasq */
1381
		services_dnsmasq_configure();
1382

    
1383
		/* reload captive portal */
1384
		captiveportal_configure();
1385
	}
1386

    
1387

    
1388
	unmute_kernel_msgs();
1389

    
1390
	return 0;
1391
}
1392

    
1393
function interface_carpdev_dhcp_configure($interface = "wan") 
1394
{
1395
	global $config, $g;
1396

    
1397
	$wancfg = $config['interfaces'][$interface];
1398
	$wanif = $wancfg['if'];
1399
	/* bring wan interface up before starting dhclient */
1400
	if($wanif)
1401
		interfaces_bring_up($wanif);
1402
	else 
1403
		log_error("Could not bring wanif up in terface_carpdev_dhcp_configure()");
1404

    
1405
	return 0;
1406
}
1407

    
1408
function interface_dhcp_configure($interface = "wan") 
1409
{
1410
	global $config, $g;
1411

    
1412
	$wancfg = $config['interfaces'][$interface];
1413

    
1414
	/* generate dhclient_wan.conf */
1415
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
1416
	if (!$fd) {
1417
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_wan_dhcp_configure() for writing.\n");
1418
		return 1;
1419
	}
1420

    
1421
	if ($wancfg['dhcphostname']) {
1422
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
1423
		$dhclientconf_hostname .= "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
1424
	} else {
1425
		$dhclientconf_hostname = "";
1426
	}
1427

    
1428
	$wanif = get_real_interface($interface);
1429

    
1430
 	$dhclientconf = "";
1431
	
1432
	$dhclientconf .= <<<EOD
1433
interface "{$wanif}" {
1434
timeout 60;
1435
retry 1;
1436
select-timeout 0;
1437
initial-interval 1;
1438
	{$dhclientconf_hostname}
1439
	script "/sbin/dhclient-script";
1440
}
1441

    
1442
EOD;
1443

    
1444
if(is_ipaddr($wancfg['alias-address'])) {
1445
	$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
1446
	$dhclientconf .= <<<EOD
1447
alias {
1448
	interface  "{$wanif}";
1449
	fixed-address {$wancfg['alias-address']};
1450
	option subnet-mask {$subnetmask};
1451
}
1452

    
1453
EOD;
1454
}
1455
	fwrite($fd, $dhclientconf);
1456
	fclose($fd);
1457

    
1458
	$relwanif = $wancfg['if'];
1459

    
1460
	/* bring wan interface up before starting dhclient */
1461
	if($realwanif)
1462
		interfaces_bring_up($realwanif);
1463
	else 
1464
		log_error("Could not bring realwanif up in interface_dhcp_configure()");
1465

    
1466
	/* fire up dhclient */
1467
	mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
1468

    
1469
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1470
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif}");
1471
	fclose($fout);
1472

    
1473
	return 0;
1474
}
1475

    
1476
function interface_pppoe_configure($interface = "wan") 
1477
{
1478
	global $config, $g;
1479

    
1480
	$wancfg = $config['interfaces'][$interface];
1481

    
1482
	/* generate mpd.conf */
1483
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1484
	if (!$fd) {
1485
		printf("Error: cannot open mpd_{$interface}.conf in interface_pppoe_configure().\n");
1486
		return 1;
1487
	}
1488

    
1489
	$idle = 0;
1490

    
1491
	if (isset($wancfg['ondemand'])) {
1492
		$ondemand = "enable";
1493
		if ($wancfg['timeout'])
1494
			$idle = $wancfg['timeout'];
1495
	} else {
1496
		$ondemand = "disable";
1497
	}
1498

    
1499
	$mpdconf = <<<EOD
1500
startup:
1501
pppoeclient:
1502

    
1503
EOD;
1504

    
1505
	if ($interface == "wan")
1506
		$realif = "pppoe0";
1507
	else {
1508
		// Here code assumes only that strings of form "opt#" will be passed.
1509
		$realif = "pppoe" . substr($interface, 3); 
1510
	}
1511
	
1512
	$mpdconf .= <<<EOD
1513
	new -i {$realif} pppoeclient pppoeclient
1514

    
1515
EOD;
1516
	if ($interface == "wan")
1517
		$mpdconf .= <<<EOD
1518
	set iface route default
1519

    
1520
EOD;
1521
	
1522
	$mpdconf .= <<<EOD
1523
	set iface {$ondemand} on-demand
1524
	set iface idle {$idle}
1525
	set iface enable tcpmssfix
1526
	set iface up-script /usr/local/sbin/ppp-linkup
1527
	set iface down-script /usr/local/sbin/ppp-linkdown
1528

    
1529
EOD;
1530

    
1531
	if (isset($wancfg['ondemand'])) {
1532
		if (isset($wancfg['local-ip']) && isset($wancfg['remote-ip'])) {
1533
			$mpdconf .= <<<EOD
1534
	set iface addrs {$wancfg['local-ip']} {$wancfg['remote-ip']}
1535

    
1536
EOD;
1537
		} else {
1538
			$mpdconf .= <<<EOD
1539
	set iface addrs 192.0.2.112 192.0.2.113
1540

    
1541
EOD;
1542
		}
1543
	}
1544

    
1545
	$mpdconf .= <<<EOD
1546
	set bundle disable multilink
1547
	set auth authname "{$wancfg['pppoe_username']}"
1548
	set auth password "{$wancfg['pppoe_password']}"
1549
	set link keep-alive 10 60
1550
	set link max-redial 0
1551
	set link no acfcomp protocomp
1552
	set link disable pap chap
1553
	set link accept chap
1554
	
1555
EOD;
1556
	if (empty($wancfg['mtu']))
1557
		$mpdmtu = "1492";
1558
	else 
1559
		$mpdmtu = "{$wancfg['mtu']}";
1560

    
1561
	$mpdconf .= <<<EOD
1562
	set link mtu {$mpdmtu}
1563
	set ipcp yes vjcomp
1564
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1565

    
1566
EOD;
1567

    
1568
	if (isset($config['system']['dnsallowoverride'])) {
1569
		$mpdconf .= <<<EOD
1570
	set ipcp enable req-pri-dns
1571

    
1572
EOD;
1573
	}
1574

    
1575
	if (!isset($wancfg['dnsnosec'])) {
1576
			$mpdconf .= <<<EOD
1577
	set ipcp enable req-sec-dns
1578

    
1579
EOD;
1580
	}
1581
	
1582
	$mpdconf .= <<<EOD
1583
	open
1584

    
1585
EOD;
1586

    
1587
	fwrite($fd, $mpdconf);
1588
	fclose($fd);
1589

    
1590
	/* generate mpd.links */
1591
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.links", "w");
1592
	if (!$fd) {
1593
		printf("Error: cannot open mpd_{$interface}.links in interface_pppoe_configure().\n");
1594
		return 1;
1595
	}
1596

    
1597
	$mpdconf = <<<EOD
1598
pppoeclient:
1599
	set link type pppoe
1600
	set pppoe iface {$wancfg['if']}
1601
	set pppoe service "{$wancfg['provider']}"
1602
	set pppoe enable originate
1603
	set pppoe disable incoming
1604

    
1605
EOD;
1606

    
1607
	fwrite($fd, $mpdconf);
1608
	fclose($fd);
1609

    
1610
	if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid") and $g['booting']) {
1611
		/* if we are booting and mpd has already been started then don't start again. */
1612
	} else {
1613
		/* if mpd is active, lets take it down */
1614
		if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid")) {
1615
			killbypid("{$g['varrun_path']}/pppoe_{$interface}.pid");
1616
			sleep(3);
1617
		}
1618

    
1619
		/* Bring the parent interface up */
1620
		if($wancfg['if'])
1621
			interfaces_bring_up($wancfg['if']);
1622
		else 
1623
			log_error("Could not bring wancfg['if'] up in interface_pppoe_configure()");
1624

    
1625
		/* fire up mpd */
1626
		mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']} -f mpd_{$interface}.conf -l mpd_{$interface}.links -p {$g['varrun_path']}/pppoe_{$interface}.pid pppoeclient");
1627
	}
1628

    
1629
	/* sleep until wan is up - or 30 seconds, whichever comes first */
1630
	for ($count = 0; $count < 30; $count++) {
1631
		if(file_exists("{$g['tmp_path']}/{$interface}up")) {
1632
			break;
1633
		}
1634
		sleep(1);
1635
	}
1636

    
1637
	unlink_if_exists("{$g['tmp_path']}/{$interface}up");
1638

    
1639
	return 0;
1640
}
1641

    
1642
function interface_pptp_configure($interface) 
1643
{
1644
	global $config, $g;
1645

    
1646
	$wancfg = $config['interfaces'][$interface];
1647

    
1648
	/* generate mpd.conf */
1649
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1650
	if (!$fd) {
1651
		printf("Error: cannot open mpd_{$interface}.conf in interface_pptp_configure().\n");
1652
		return 1;
1653
	}
1654

    
1655
	$idle = 0;
1656

    
1657
	if (isset($wancfg['ondemand'])) {
1658
		$ondemand = "enable";
1659
		if ($wancfg['timeout'])
1660
			$idle = $wancfg['timeout'];
1661
	} else {
1662
		$ondemand = "disable";
1663
	}
1664

    
1665
	$mpdconf = <<<EOD
1666
startup:
1667
pptp:
1668

    
1669
EOD;
1670

    
1671
        if ($interface == "wan")
1672
                $realif = "pptp0";
1673
        else {
1674
                // Here code assumes only that strings of form "opt#" will be passed.
1675
                $realif = "pptp" . substr($interface, 3);
1676
	}
1677

    
1678
        $mpdconf .= <<<EOD
1679
        new -i {$realif} pptp pptp 
1680

    
1681
EOD;
1682
        if ($interface == "wan")
1683
                $mpdconf .= <<<EOD
1684
        set iface route default
1685

    
1686
EOD;
1687

    
1688
        $mpdconf .= <<<EOD
1689
	set iface {$ondemand} on-demand
1690
	set iface idle {$idle}
1691
	set iface up-script /usr/local/sbin/ppp-linkup
1692
	set iface down-script /usr/local/sbin/ppp-linkdown
1693

    
1694
EOD;
1695

    
1696
	if (isset($wanfg['ondemand'])) {
1697
		$mpdconf .= <<<EOD
1698
	set iface addrs 10.0.0.1 10.0.0.2
1699

    
1700
EOD;
1701
	}
1702

    
1703
	$mpdconf .= <<<EOD
1704
	set bundle disable multilink
1705
	set bundle authname "{$wancfg['pptp_username']}"
1706
	set bundle password "{$wancfg['pptp_password']}"
1707
	set bundle no noretry
1708
	set link keep-alive 10 60
1709
	set link max-redial 0
1710
	set link no acfcomp protocomp
1711
	set link disable pap chap
1712
	set link accept chap
1713
	set ipcp no vjcomp
1714
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1715

    
1716
EOD;
1717
	if (isset($config['system']['dnsallowoverride'])) {
1718
		$mpdconf .= <<<EOD
1719
	set ipcp enable req-pri-dns
1720

    
1721
EOD;
1722
	}
1723

    
1724
	$mpdconf .= <<<EOD
1725
	open
1726

    
1727
EOD;
1728

    
1729
	fwrite($fd, $mpdconf);
1730
	fclose($fd);
1731

    
1732
	/* generate mpd.links */
1733
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.links", "w");
1734
	if (!$fd) {
1735
		printf("Error: cannot open mpd_{$interface}.links in interface_pptp_configure().\n");
1736
		return 1;
1737
	}
1738

    
1739
	$mpdconf = <<<EOD
1740
pptp:
1741
	set link type pptp
1742
	set pptp enable originate outcall
1743
	set pptp disable windowing
1744
	set pptp self {$wancfg['local']}
1745
	set pptp peer {$wancfg['remote']}
1746

    
1747
EOD;
1748

    
1749
	fwrite($fd, $mpdconf);
1750
	fclose($fd);
1751

    
1752
	/* configure interface */
1753
	if($wancfg['if'])
1754
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1755
			escapeshellarg($wancfg['local'] . "/" . $wancfg['subnet']) . " up");
1756
	else 
1757
		log_error("Could not bring interface wancfg['if'] up in interface_pptp_configure()");
1758
	/* fire up mpd */
1759
	mwexec("/usr/local/sbin/mpd4 -b -d {$g['varetc_path']} -f mpd_{$interface}.conf -l mpd_{$interface}.links -p {$g['varrun_path']}/pptp_{$interface}.pid pptp");
1760

    
1761
	return 0;
1762
}
1763
 
1764
/* XXX: stub for code that references the old functions(mostly packages) */
1765
function get_real_wan_interface($interface = "wan") 
1766
{
1767
	return get_real_interface($interface);
1768
}
1769
function get_current_wan_address($interface = "wan")
1770
{
1771
	return get_interface_ip($interface);
1772
}
1773

    
1774
function get_real_interface($interface = "wan") 
1775
{
1776
    global $config;
1777

    
1778
	$wanif = $interface;
1779

    
1780
	switch ($interface) {
1781
	case "pptp":
1782
		$wanif = "pptp";
1783
		break;
1784
	case "pppoe":
1785
		$wanif = "pppoe";
1786
		break;
1787
	case "openvpn":
1788
		$wanif = "openvpn";
1789
		break;
1790
	case "enc0":
1791
		$wanif = "enc0";
1792
		break;
1793
	/* XXX: dial in support?!
1794
	case "ppp":
1795
		$wanif = "ppp";
1796
		break;
1797
	*/
1798
	default:
1799
		$iflist = get_configured_interface_with_descr(false, true);
1800

    
1801
		foreach ($iflist as $if => $ifdesc) {
1802
			if ($interface == $if || $interface == $ifdesc) {
1803

    
1804
			$cfg = $config['interfaces'][$if];
1805

    
1806
			switch ($cfg['ipaddr']) {
1807
			case "carpdev-dhcp":
1808
				$viparr = &$config['virtualip']['vip'];
1809
				$counter = 0;
1810
				if(is_array($viparr))
1811
				foreach ($viparr as $vip) {
1812
					if ($vip['mode'] == "carpdev-dhcp") {
1813
						if($vip['interface'] == $if) {
1814
							$wanif =  "carp{$counter}";
1815
							break;
1816
						}
1817
						$counter++;
1818
					} else if ($vip['mode'] = "carp") 
1819
						$counter++;
1820
				}
1821
				break;
1822
			case "pppoe": 
1823
				if ($if == "wan")
1824
					$wanif = "pppoe0";
1825
				else
1826
					$wanif = "pppoe" . substr($if,3);
1827
				break;
1828
			case "pptp": 
1829
				if ($if == "wan")
1830
					$wanif = "pptp0";
1831
				else
1832
					$wanif = "pptp" . substr($if, 3);
1833
				break;
1834
			default:
1835
				$wanif = $cfg['if'];
1836
				break;
1837
			}
1838
			
1839
			break;
1840
			}
1841
		}
1842
		break;
1843
	}
1844

    
1845
    return $wanif;
1846
}
1847

    
1848
function get_interface_ip($interface = "wan") 
1849
{
1850
	global $config, $g;
1851

    
1852
	$realif = get_real_interface($interface);
1853
	/* Do we really come here for these interfaces ?! */
1854
	if (in_array($realif, array("pptp", "pppoe", "openvpn", "enc0" /* , "ppp" */)))
1855
			return "";
1856

    
1857
	$curip = find_interface_ip($realif);
1858
	if ($curip && is_ipaddr($curip) && ($curip != "0.0.0.0"))
1859
		return $curip;
1860

    
1861
	return null;
1862
}
1863

    
1864
/****f* interfaces/is_altq_capable
1865
 * NAME
1866
 *   is_altq_capable - Test if interface is capable of using ALTQ
1867
 * INPUTS
1868
 *   $int            - string containing interface name
1869
 * RESULT
1870
 *   boolean         - true or false
1871
 ******/
1872

    
1873
function is_altq_capable($int) 
1874
{
1875
        /* Per:
1876
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1877
         * Only the following drivers have ALTQ support
1878
         */
1879
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1880
			"em", "fxp", "hme", "le", "nve", "re", "rl", "ndis", "sf", "sis", "sk",
1881
			"tun", "vr", "wi", "xl", "vlan", "ste", "aue", "bce", "ep", "gem", "ipw", 
1882
			"iwi", "msk", "mxge", "my", "nfe", "npe", "ral", "rum", "stge", "udav", "ural");
1883

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

    
1886
        if (in_array($int_family[0], $capable))
1887
                return true;
1888
        else
1889
                return false;
1890
}
1891

    
1892
function get_wireless_modes($interface)
1893
{
1894
	/* return wireless modes and channels */
1895
	$wireless_modes = array();
1896

    
1897
	if(is_interface_wireless($interface)) {
1898
		$wi = 1;
1899
		$ifconfig = "/sbin/ifconfig";
1900
		$awk = "/usr/bin/awk";
1901
		$chan_list = "$ifconfig $interface list chan";
1902
		$stack_list = "$awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
1903
		$format_list = "$awk '{print \$5 \" \" \$6 \",\" \$1}'";
1904

    
1905
		$interface_channels = "";
1906
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
1907
		$interface_channel_count = count($interface_channels);
1908

    
1909
		$c = 0;
1910
		while ($c < $interface_channel_count)
1911
		{
1912
			$channel_line = explode(",", $interface_channels["$c"]);
1913
			$wireless_mode = trim($channel_line[0]);
1914
			$wireless_channel = trim($channel_line[1]);
1915
			if(trim($wireless_mode) != "") {
1916
				/* if we only have 11g also set 11b channels */
1917
				if($wireless_mode == "11g") {
1918
					$wireless_modes["11b"] = array();
1919
				}
1920
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
1921
			}
1922
			$c++;
1923
		}
1924
	}
1925
	return($wireless_modes);
1926
}
1927

    
1928
function get_interface_mac($interface) 
1929
{
1930

    
1931
        /* build interface list with netstat */
1932
        $linkinfo = "";
1933
        exec("/usr/bin/netstat -I $interface -nW -f link", $linkinfo);
1934
        array_shift($linkinfo);
1935
        $alink = preg_split("/\s+/", $linkinfo[0]);
1936
        $mac = chop($alink[3]);
1937
        return $mac;
1938
}
1939

    
1940
?>
(15-15/37)