Project

General

Profile

Download (55.8 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_loopback_configure() {
44
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
45
	mwexec("/sbin/ifconfig lo0 up");
46
	return 0;
47
}
48

    
49
function interfaces_vlan_configure() {
50
        global $config;
51

    
52
	$i = 0;
53
        if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
54
                foreach ($config['vlans']['vlan'] as $vlan) {
55
			if(empty($vlan['vlanif'])) {
56
				$vlan['vlanif'] = "vlan{$i}";
57
			}
58
                        /* XXX: Maybe we should report any errors?! */
59
                        interface_vlan_configure($vlan['if'], $vlan['tag'], $vlan['vlanif']);
60
			$i++;
61
		}
62
        }
63
}
64

    
65
function interface_vlan_configure($if, $tag, $vlanif = "") {
66
        global $config, $g;
67

    
68
        /* devices with native VLAN support */
69
        $vlan_native_supp = $g['vlan_native_supp'];
70

    
71
        /* devices with long frame support */
72
        $vlan_long_frame = $g['vlan_long_frame'];
73

    
74
        /* make sure the parent interface is up */
75
        mwexec("/sbin/ifconfig " . escapeshellarg($if) . " up");
76

    
77
        if ($g['booting'] || !(empty($vlanif))) {
78
		mwexec("/sbin/ifconfig {$vlanif} destroy");
79
		mwexec("/sbin/ifconfig {$vlanif} create");
80
        } else 
81
		$vlanif = exec("/sbin/ifconfig vlan create");
82
	
83
        mwexec("/sbin/ifconfig {$vlanif} vlan " .
84
                escapeshellarg($tag) . " vlandev " .
85
                escapeshellarg($if));
86

    
87
        /* get driver name */
88
        for ($j = 0; $j < strlen($if); $j++) {
89
                if ($if[$j] >= '0' && $if[$j] <= '9')
90
                        break;
91
	}
92
        $drvname = substr($if, 0, $j);
93

    
94
        if (in_array($drvname, $vlan_native_supp))
95
		mwexec("/sbin/ifconfig {$if} vlanhwtag");
96
        else if (in_array($drvname, $vlan_long_frame))
97
                mwexec("/sbin/ifconfig {$if} vlanmtu");
98

    
99
	mwexec("/sbin/ifconfig {$vlanif} up");
100

    
101
        /* invalidate interface cache */
102
        get_interface_arr(true);
103

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

    
114
        /* XXX: ermal -- for now leave it here at the moment it does not hurt. */
115
        mwexec("/sbin/ifconfig " . escapeshellarg($if) . " up");
116

    
117
        return $vlanif;
118
}
119

    
120
function interfaces_bridge_configure() {
121
        global $config;
122

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

    
136
function interface_bridge_configure(&$bridge) {
137
        global $config, $g;
138

    
139
        if (!is_array($bridge))
140
                return -1;
141

    
142
	$members = explode(',', $bridge['members']);
143
	if (!count($members))
144
		return -1;
145
	
146
	$checklist = get_configured_interface_list();
147

    
148
	$cmd = "";
149
	foreach ($members as $member) {
150
		if (!array_key_exists($member, $checklist))
151
			continue;
152
        	$realif = get_real_wan_interface($member);
153
		$realif =  escapeshellarg($realif);
154
        	/* make sure the parent interface is up */
155
        	mwexec("/sbin/ifconfig {$realif} up");
156
		$cmd .= " addm {$realif}";
157
	}
158

    
159

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

    
167
	/* Add interfaces to bridge */
168
        mwexec("/sbin/ifconfig {$bridgeif} {$cmd}");
169
	
170
	if (isset($bridge['enablestp'])) {
171
		/* Choose spanning tree proto */
172
		mwexec("/sbin/ifconfig {$bridgeif} proto {$bridge['proto']}");	
173
		
174
		$stpifs = explode(',', $bridge['stp']);
175
		foreach ($stpifs as $stpif) {
176
			$realif = get_real_wan_interface($stpif);
177
			mwexec("/sbin/ifconfig {$bridgeif} stp {$realif}");
178
		}
179
		if ($bridge['maxage'] <> "")
180
			mwexec("/sbin/ifconfig {$bridgeif} maxage {$bridge['maxage']}");
181
		if ($brige['fwdelay'] <> "")
182
			mwexec("/sbin/ifconfig {$bridgeif} fwddelay {$bridge['fwdelay']}");
183
		if ($brige['hellotime'] <> "")
184
                        mwexec("/sbin/ifconfig {$bridgeif} hellotime {$bridge['hellotime']}");
185
		if ($brige['priority'] <> "")
186
                        mwexec("/sbin/ifconfig {$bridgeif} priority {$bridge['priority']}");
187
		if ($brige['holdcount'] <> "")
188
                        mwexec("/sbin/ifconfig {$bridgeif} holdcnt {$bridge['holdcnt']}");
189
		$pconfig = explode(",", $bridge['ifpriority']);
190
		$ifpriority = array();
191
		foreach ($pconfig as $cfg) {
192
			$embcfg = explode(":", $cfg);
193
			foreach ($embcfg as $key => $value)
194
				$ifpriority[$key] = $value;
195
		}
196
		foreach ($ifpriority as $key => $value) {
197
			$realif = get_real_wan_interface($key);
198
			mwexec("/sbin/ifconfig ${bridgeif} ifpriority {$realif} {$value}"); 
199
		}
200
		$pconfig = explode(",", $bridges['ifpathcost']);
201
		$ifpathcost = array();
202
		foreach ($pconfig as $cfg) {
203
			$embcfg = explode(":", $cfg);
204
			foreach ($embcfg as $key => $value)
205
				$ifpathcost[$key] = $value;
206
		}
207
		foreach ($ifpriority as $key => $value) {
208
                        $realif = get_real_wan_interface($key);
209
                        mwexec("/sbin/ifconfig ${bridgeif} ifpathcost {$realif} {$value}");
210
                }
211

    
212
	}
213

    
214
	if ($bridge['maxaddr'] <> "")
215
		mwexec("/sbin/ifconfig {$bridgeif} maxaddr {$bridge['maxaddr']}");
216
        if ($bridge['timeout'] <> "")
217
                mwexec("/sbin/ifconfig {$bridgeif} timeout {$bridge['timeout']}");
218
        if ($bridge['span'] <> "") {
219
		$realif = get_real_wan_interface($bridge['span']);
220
                mwexec("/sbin/ifconfig {$bridgeif} span {$realif}");
221
	}
222
        $edgeifs = explode(',', $bridge['edge']);
223
        foreach ($edgeifs as $edgeif) {
224
		$realif = get_real_wan_interface($edgeif);
225
                mwexec("/sbin/ifconfig {$bridgeif} edge {$realif}");
226
        }
227
        $edgeifs = explode(',', $bridge['autoedge']);
228
        foreach ($edgeifs as $edgeif) {
229
                $realif = get_real_wan_interface($edgeif);
230
                mwexec("/sbin/ifconfig {$bridgeif} -autoedge {$realif}");
231
        }
232
        $ptpifs = explode(',', $bridge['ptp']);
233
        foreach ($ptpifs as $ptpif) {
234
                $realif = get_real_wan_interface($ptpif);
235
                mwexec("/sbin/ifconfig {$bridgeif} ptp {$realif}");
236
        }
237
        $ptpifs = explode(',', $bridge['autoptp']);
238
        foreach ($ptpifs as $ptpif) {
239
                $realif = get_real_wan_interface($ptpif);
240
                mwexec("/sbin/ifconfig {$bridgeif} -autoptp {$realif}");
241
        }
242
        $stickyifs = explode(',', $bridge['static']);
243
        foreach ($stickyifs as $stickyif) {
244
                $realif = get_real_wan_interface($stickyif);
245
                mwexec("/sbin/ifconfig {$bridgeif} sticky {$realif}");
246
        }
247
        $privateifs = explode(',', $bridge['private']);
248
        foreach ($privateifs as $privateif) {
249
                $realif = get_real_wan_interface($privateif);
250
                mwexec("/sbin/ifconfig {$bridgeif} private {$realif}");
251
        }
252

    
253
        mwexec("/sbin/ifconfig {$bridgeif} up");
254

    
255
        return $bridgeif;
256
}
257

    
258
function interfaces_lagg_configure() {
259
        global $config;
260

    
261
        $i = 0;
262
        if (is_array($config['laggs']['lagg']) && count($config['laggs']['lagg'])) {
263
                foreach ($config['laggs']['lagg'] as $lagg) {
264
                        if(empty($lagg['laggif'])) {
265
                                $lagg['laggif'] = "lagg{$i}";
266
                        }
267
                        /* XXX: Maybe we should report any errors?! */
268
                        interface_lagg_configure($lagg);
269
                        $i++;
270
                }
271
        }
272
}
273

    
274
function interface_lagg_configure(&$lagg) {
275
        global $config, $g;
276

    
277
        if (!is_array($lagg))
278
		return -1;
279

    
280
	$members = explode(',', $lagg['members']);
281
	if (!count($members))
282
		return -1;
283
	
284
	$checklist = get_interface_list();
285

    
286
	$cmd = "";
287
	foreach ($members as $member) {
288
		if (!array_key_exists($member, $checklist))
289
			continue;
290
		$realif =  escapeshellarg($member);
291
		$mtu = get_interface_mtu($realif);
292
        	/* make sure the parent interface is up */
293
        	mwexec("/sbin/ifconfig {$realif} mtu {$mtu} up");
294
		$cmd .= " laggport {$realif}";
295
	}
296

    
297
        if ($g['booting'] || !(empty($lagg['laggif']))) {
298
                mwexec("/sbin/ifconfig {$lagg['laggif']} destroy");
299
                mwexec("/sbin/ifconfig {$lagg['laggif']} create");
300
		$laggif = $lagg['laggif'];
301
        } else
302
                $laggif = exec("/sbin/ifconfig lagg create");
303

    
304

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

    
307
	/* Add interfaces to lagg */
308
        mwexec("/sbin/ifconfig {$laggif} {$cmd}");
309

    
310
	mwexec("/sbin/ifconfig {$laggif} up");
311

    
312
        return $laggif;
313
}
314

    
315
function interfaces_gre_configure() {
316
        global $config;
317

    
318
        $i = 0;
319
        if (is_array($config['gres']['gre']) && count($config['gres']['gre'])) {
320
                foreach ($config['gres']['gre'] as $gre) {
321
                        if(empty($gre['greif'])) {
322
                                $gre['greif'] = "gre{$i}";
323
                        }
324
                        /* XXX: Maybe we should report any errors?! */
325
                        interface_gre_configure($gre);
326
                        $i++;
327
                }
328
        }
329
}
330

    
331
function interface_gre_configure(&$gre) {
332
        global $config, $g;
333

    
334
	if (!is_array($gre))
335
		return -1;
336

    
337
	$realif = get_real_wan_interface($gre['if']);
338
	$realifip = get_current_wan_address($gre['if']);
339

    
340
        /* make sure the parent interface is up */
341
        mwexec("/sbin/ifconfig " . escapeshellarg($if) . " up");
342

    
343
        if ($g['booting'] || !(empty($gre['greif']))) {
344
                mwexec("/sbin/ifconfig {$gre['greif']} destroy");
345
                mwexec("/sbin/ifconfig {$gre['greif']} create");
346
		$greif = $gre['greif'];
347
        } else
348
                $greif = exec("/sbin/ifconfig gre create");
349

    
350
	/* Do not change the order here for more see gre(4) NOTES section. */
351
	mwexec("/sbin/ifconfig {$greif} tunnel {$realifip} {$gre['remote-addr']}");
352
	mwexec("/sbin/ifconfig {$greif} {$gre['tunnel-local-addr']} {$gre['tunnel-remote-addr']} netmask 255.255.255.255 ");
353
	if (isset($gre['link0']) && $gre['link0'])
354
		mwexec("/sbin/ifconfig {$greif} link0");
355
        if (isset($gre['link1']) && $gre['link1'])
356
                mwexec("/sbin/ifconfig {$greif} link1");
357
        if (isset($gre['link2']) && $gre['link2'])
358
                mwexec("/sbin/ifconfig {$greif} link2");
359

    
360
	mwexec("/sbin/ifconfig {$greif} up");
361
	mwexec("/sbin/route add {$gre['remote-addr']}/{$gre['tunnel-remote-net']} {$realifip}");
362

    
363
	return $greif;
364
}
365

    
366
function interfaces_gif_configure() {
367
        global $config;
368

    
369
        $i = 0;
370
        if (is_array($config['gifs']['gif']) && count($config['gifs']['gif'])) {
371
                foreach ($config['gifs']['gif'] as $gif) {
372
                        if(empty($gif['gifif'])) {
373
                                $gre['gifif'] = "gif{$i}";
374
                        }
375
                        /* XXX: Maybe we should report any errors?! */
376
                        interface_gif_configure($gif);
377
                        $i++;
378
                }
379
        }
380
}
381

    
382
function interface_gif_configure(&$gif) {
383
        global $config, $g;
384

    
385
        if (!is_array($gif))
386
                return -1;
387

    
388
        $realif = get_real_wan_interface($gif['if']);
389
        $realifip = get_current_wan_address($gif['if']);
390

    
391
        /* make sure the parent interface is up */
392
        mwexec("/sbin/ifconfig " . escapeshellarg($if) . " up");
393

    
394
        if ($g['booting'] || !(empty($gif['gifif']))) {
395
                mwexec("/sbin/ifconfig {$gif['gifif']} destroy");
396
                mwexec("/sbin/ifconfig {$gif['gifif']} create");
397
                $gifif = $gif['gifif'];
398
        } else
399
                $gifif = exec("/sbin/ifconfig gif create");
400

    
401
        /* Do not change the order here for more see gif(4) NOTES section. */
402
        mwexec("/sbin/ifconfig {$gifif} tunnel {$realifip} {$gif['remote-addr']}");
403
        mwexec("/sbin/ifconfig {$gifif} {$gif['tunnel-local-addr']} {$gif['tunnel-remote-addr']} netmask 255.255.255.255 ");
404
        if (isset($gif['link0']) && $gif['link0'])
405
                mwexec("/sbin/ifconfig {$gifif} link0");
406
        if (isset($gif['link1']) && $gif['link1'])
407
                mwexec("/sbin/ifconfig {$gifif} link1");
408

    
409
        mwexec("/sbin/ifconfig {$gifif} up");
410
        mwexec("/sbin/route add {$gif['remote-addr']}/{$gif['tunnel-remote-net']} {$realifip}");
411

    
412
        return $gifif;
413
}
414

    
415
function interfaces_configure() {
416
	global $config, $g;
417

    
418
        /* set up VLAN virtual interfaces */
419
        interfaces_vlan_configure();
420

    
421
        /* set up LAGG virtual interfaces */
422
        interfaces_lagg_configure();
423

    
424
	/* Set up PPP interfaces */
425
	interfaces_ppp_configure();
426

    
427
	$iflist = get_configured_interface_with_descr();
428
	$delayed_list = array();
429
	$bridge_list = array();
430
	
431
	foreach($iflist as $if => $ifname) {
432
		$realif = $config['interfaces'][$if]['if'];
433

    
434
		if (strstr($realif, "bridge")) 
435
			$bridge_list[$if] = $ifname;
436
		else if (strstr($realif, "gre"))
437
			$delayed_list[$if] = $ifname;
438
		else if (strstr($realif, "gif"))
439
			$delayed_list[$if] = $ifname;
440
		else {
441
			if ($g['booting'])
442
        	                echo "Configuring {$ifname} interface...";
443
                	if($debug)
444
                       		log_error("Configuring {$ifname}");
445

    
446
			interface_configure($if);
447

    
448
			if ($g['booting']) 
449
                		echo "done.\n";
450
		}
451
	}
452

    
453
        /* set up GRE virtual interfaces */
454
        interfaces_gre_configure();
455

    
456
        /* set up GIF virtual interfaces */
457
        interfaces_gif_configure();
458
	
459
	foreach ($delayed_list as $if => $ifname) {
460
		if ($g['booting'])
461
                        echo "Configuring {$ifname} interface...";
462
                if($debug)
463
                       log_error("Configuring {$ifname}");
464

    
465
		interface_configure($if);
466

    
467
		if ($g['booting'])
468
			echo "done.\n";
469
	}
470

    
471
        /* set up BRIDGe virtual interfaces */
472
        interfaces_bridge_configure();
473

    
474
        foreach ($bridge_list as $if => $ifname) {
475
                if ($g['booting'])
476
                        echo "Configuring {$ifname} interface...";
477
                if($debug)
478
                       log_error("Configuring {$ifname}");
479

    
480
                interface_configure($if);
481

    
482
                if ($g['booting'])
483
                        echo "done.\n";
484
        }
485

    
486
        /* bring up carp interfaces */
487
        interfaces_carp_configure();
488

    
489
        /* bring ip IP aliases */
490
        interfaces_ipalias_configure();
491

    
492
	if (!$g['booting']) {
493
		/* reconfigure static routes (kernel may have deleted them) */
494
		system_routing_configure();
495

    
496
		/* reload IPsec tunnels */
497
		vpn_ipsec_configure();
498

    
499
		/* reload dhcpd (interface enabled/disabled/bridged status may have changed) */
500
		services_dhcpd_configure();
501

    
502
		/* restart dnsmasq */
503
		services_dnsmasq_configure();
504

    
505
		/* reload captive portal */
506
		captiveportal_configure();
507

    
508
		/* set the reload filter dity flag */
509
		touch("{$g['tmp_path']}/filter_dirty");
510
	}
511

    
512
	return 0;
513
}
514

    
515
function interface_bring_down($interface) {
516
	global $config;
517

    
518
	$cfg = $config['interfaces'][$interface];
519
	mwexec("/sbin/ifconfig " . escapeshellarg($cfg['if']) . " delete down");
520
}
521

    
522
function interfaces_ppp_configure() {
523
        global $config;
524

    
525
        $i = 0;
526
        if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
527
                foreach ($config['ppps']['ppp'] as $ppp) {
528
                        if(empty($ppp['pppif'])) {
529
                                $ppp['pppif'] = "ppp{$i}";
530
                        }
531
                        /* XXX: Maybe we should report any errors?! */
532
                        interface_vlan_configure($ppp);
533
                        $i++;
534
                }
535
        }
536
}
537

    
538
function interface_ppp_configure($ifcfg) {
539
	global $config;
540
	
541
	/* Remove  the /dev/ from the device name. */
542
	$dev = substr($ifcfg['port'], 4);
543

    
544
	if(file_exists("/var/run/ppp_{$dev}.pid")) {
545
		$pid = trim(file_get_contents("/var/run/ppp_{$dev}.pid"));
546
		mwexec("kill {$pid}");
547
	}
548
	
549
	if ($ifcfg['pppif'] <> "")
550
		mwexec("/sbin/ifconfig {$ifcfg['pppif']} destroy");
551

    
552
	$peerfile = "lcp-echo-failure 0\n";
553
	$peerfile .= "lcp-echo-interval 0\n";
554
	$peerfile .= "connect /etc/ppp/peers/ppp{$dev}-connect-chat\n";
555
	//$peerfile .= "disconnect /etc/ppp/peers/ppp{$dev}-disconnect-chat\n";
556
	$peerfile .= "/dev/{$ifcfg['if']}\n";
557
	$peerfile .= "crtscts\n";
558
	$peerfile .= "local\n";
559
	//$peerfile .= ":{$ifcfg['gateway']}\n";
560
	$peerfile .= "noipdefault\n";
561
	$peerfile .= "ipcp-accept-local\n";
562
	$peerfile .= "novj\n";
563
	$peerfile .= "nobsdcomp\n";
564
	$peerfile .= "novjccomp\n";
565
	$peerfile .= "nopcomp\n";
566
	$peerfile .= "noaccomp\n";
567
	$peerfile .= "noauth\n";
568
	$peerfile .= "persist\n";
569
	$peerfile .= "debug\n";
570
	// KD - test
571
	//$peerfile .= "defaultroute\n";
572
	//$peerfile .= "nodetach\n";
573
	// KD - so I know where to look!
574
	$peerfile .= "# created by /etc/inc/interfaces.inc\n";
575
	file_put_contents("/etc/ppp/peers/ppp_{$dev}", $peerfile);
576

    
577
	// Added single quotes to some strings below:
578
	// the \rAT is *always* going to need it
579
	// and the phone number on a GSM connection ends in a # char
580
	// Kevin Dawson, 22 Jan 2008
581
	// Refer Andrew Curtis
582
			
583
	$chatfile = "#!/bin/sh\n";
584
	$chatfile .= "exec chat \\\n";
585
	$chatfile .= "TIMEOUT 5 \\\n";
586
	$chatfile .= "ECHO ON \\\n";
587
	$chatfile .= "ABORT '\\nBUSY\\r' \\\n";
588
	$chatfile .= "ABORT '\\nERROR\\r' \\\n";
589
	$chatfile .= "ABORT '\\nNO ANSWER\\r' \\\n";
590
	$chatfile .= "ABORT '\\nNO CARRIER\\r' \\\n";
591
	$chatfile .= "ABORT '\\nNO DIALTONE\\r' \\\n";
592
	$chatfile .= "ABORT '\\nRINGING\\r\\n\\r\\nRINGING\\r' \\\n";
593
	// KD
594
	$chatfile .= "'' '\\rAT' \\\n";
595
	$chatfile .= "TIMEOUT 12 \\\n";
596
	$chatfile .= "OK ATH \\\n";
597
	$chatfile .= "OK ATE1 \\\n";
598
	$chatfile .= "OK 'AT+CGDCONT=1,\"IP\",\"{$ifcfg['ap']}\"' \\\n";
599
	// KD
600
	$chatfile .= "OK 'ATD{$ifcfg['phone']}' \\\n";
601
	$chatfile .= "TIMEOUT 22 \\\n";
602
	$chatfile .= "CONNECT \"\" \\\n";
603
	$chatfile .= "SAY \"\\nConnected.\"\n";
604
	file_put_contents("/etc/ppp/peers/ppp{$dev}-connect-chat", $chatfile);
605
	chmod("/etc/ppp/peers/ppp{$dev}-connect-chat", 0755);
606
	
607
	$realif = exec("/sbin/ifconfig ppp create");
608
	
609
	return $realif;
610
}
611

    
612
function interfaces_carp_configure() {
613
	global $g, $config, $debugging;
614
	$balanacing = "";
615
	$pfsyncinterface = "";
616
	$pfsyncenabled = "";
617
	if(isset($config['system']['developerspew'])) {
618
		$mt = microtime();
619
		echo "interfaces_carp_configure() being called $mt\n";
620
	}
621
	// Prepare CmdCHAIN that will be used to execute commands.
622
	$cmdchain = new CmdCHAIN();	
623
	$carp_instances_counter = 0;
624
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
625
	/* destroy previous interfaces */
626
	for($x=0; $x<$total_carp_interfaces_defined; $x++) 
627
		$cmdchain->add("Delete CARP interface", "/sbin/ifconfig carp{$x} delete", false);
628
	if ($g['booting']) {
629
		echo "Configuring CARP interfaces...";
630
		mute_kernel_msgs();
631
	}
632
	/* suck in configuration items */
633
	if($config['installedpackages']['carpsettings']) 
634
		if($config['installedpackages']['carpsettings']['config']) {
635
		foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
636
			$pfsyncenabled = $carp['pfsyncenabled'];
637
			$balanacing = $carp['balancing'];
638
			$pfsyncinterface = $carp['pfsyncinterface'];
639
			$pfsyncpeerip = $carp['pfsyncpeerip'];
640
		}
641
	} else {
642
		unset($pfsyncinterface);
643
		unset($balanacing);
644
		unset($pfsyncenabled);
645
	}
646
	$cmdchain->add("Allow CARP", "/sbin/sysctl net.inet.carp.allow=1", true);			
647
	if($balanacing) {
648
		$cmdchain->add("Enable CARP ARP-balancing", "/sbin/sysctl net.inet.carp.arpbalance=1", true);
649
		$cmdchain->add("Disallow CARP preemption", "/sbin/sysctl net.inet.carp.preempt=0", true);
650
	} else {
651
		$cmdchain->add("Enable CARP preemption", "/sbin/sysctl net.inet.carp.preempt=1", true);		
652
	}
653
	$cmdchain->add("Enable CARP logging", "/sbin/sysctl net.inet.carp.log=2", true);
654
	$carp_sync_int = convert_friendly_interface_to_real_interface_name($pfsyncinterface);
655
	if($g['booting']) {
656
		/*    install rules to alllow pfsync to sync up during boot
657
		 *    carp interfaces will remain down until the bootup sequence finishes
658
		 */
659
		exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
660
		exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
661
		exec("echo pass out quick from any to any keep state >> /tmp/rules.boot");
662
		exec("/sbin/pfctl -f /tmp/rules.boot");
663
	}
664
	/* setup pfsync interface */
665
	if($carp_sync_int and $pfsyncenabled) {
666
		if($pfsyncpeerip) {
667
			$cmdchain->add("Bring up pfsync0 syncpeer", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} syncpeer {$pfsyncpeerip} up", false);						
668
		} else {
669
			$cmdchain->add("Bring up pfsync0 syncdev", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up", false);			
670
		}
671
	} else {
672
		$cmdchain->add("Bring up pfsync0", "/sbin/ifconfig pfsync0 syncdev lo0 up", false);						
673
	}
674
	//$fd = fopen("/tmp/carp.sh", "w");
675
	$viparr = &$config['virtualip']['vip'];
676
	if($config['virtualip']['vip']) {
677
		$cmdchain->add("Allow CARP.", "/sbin/sysctl net.inet.carp.allow=1", true);				
678
	} else {
679
		$viparr = array();
680
		$cmdchain->add("Disallow CARP.", "/sbin/sysctl net.inet.carp.allow=0", true);		
681
	}
682
	if(!$viparr and $config['interfaces']['wan']['ipaddr'] == "carpdev-dhcp") {
683
		/* no vips exist but we need to bring up carpdev... */
684
		$viparr_temp = array();
685
		$viparr_temp['advskew'] = "200";
686
		$viparr_temp['vhid'] = "1";
687
		$viparr_temp['mode'] = "carpdev-dhcp";
688
		$viparr_temp['password'] = $config['system']['hostname'] . "pfS";
689
		$viparr = $viparr_temp;
690
	}
691
	
692
	if($debugging)
693
		$cmdchain->setdebug(); // optional for verbose logging
694
	$cmdchain->execute();
695
	
696
	// Reset CmdCHAIN
697
	$cmdchain->clear();
698

    
699
	if(is_array($viparr))
700
	foreach ($viparr as $vip) {
701
		$vip_password = $vip['password'];
702
		$vip_password = str_replace(" ", "", $vip_password);
703
		if($vip['password'] != "")
704
                	$password = " pass \"" . $vip_password . "\"";
705
		$interface = filter_translate_type_to_real_interface($vip['interface']);
706
		$carpint = "carp" . $carp_instances_counter;
707

    
708
		switch ($vip['mode']) {
709
		case "carp":
710
			/* ensure CARP IP really exists prior to loading up */
711
			$found = false;
712
			$iflist = get_configured_interface_list();
713
			foreach($iflist as $if) {
714
				$ww_subnet_ip = $config['interfaces'][$if]['ipaddr'];
715
				$ww_subnet_bits = $config['interfaces'][$if]['subnet'];
716
				if (ip_in_subnet($vip['subnet'], gen_subnet($ww_subnet_ip, $ww_subnet_bits) . "/" . $ww_subnet_bits))
717
					$found = true;
718
			}
719
			if($found == false) {
720
				file_notice("CARP", "Sorry but we could not find a matching real interface subnet for the virtual IP address {$vip['subnet']}.", "Firewall: Virtual IP", "");
721
				continue;
722
			}			
723
			/* create the carp interface and setup */
724
			$cmdchain->add("create CARP interface", "/sbin/ifconfig {$carpint} create", false);
725

    
726
			/* invalidate interface cache */
727
			get_interface_arr(true);
728
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
729
			$cmdchain->add("config CARP interface", "/sbin/ifconfig {$carpint} " . $vip['subnet'] . "/" . $vip['subnet_bits'] . " broadcast " . $broadcast_address . " vhid " . $vip['vhid'] . " advskew " . $vip['advskew'] . $password, false);
730
			$cmdchain->add("bring CARP interface UP", "/sbin/ifconfig {$carpint} up", false);
731
			$carp_instances_counter++;
732
			break;
733
		case "carpdev-dhcp":
734
			log_error("Found carpdev interface {$vip['interface']} on top of interface {$interface}");
735
			if(!empty($interface)) {
736
				
737
					$cmdchain->add("bring CARP parent interface UP", "/sbin/ifconfig {$interface} up", false);			
738
					$cmdchain->add("create CARP interface", "/sbin/ifconfig {$carpint} create", false);
739
					$cmdchain->add("bring CARP interface UP", "/sbin/ifconfig {$carpint} up", false);
740
					$cmdchain->add("assign CARP CarpDEV directive", "/sbin/ifconfig {$carpint} carpdev ". $interface . " vhid " . $vip['vhid'] . " advskew " . $vip['advskew'] . $password, false);
741
					$cmdchain->add("bring CARP interface UP", "/sbin/ifconfig {$carpint} up", false);
742

    
743
					/*
744
					 * XXX: BIG HACK but carpdev needs ip services active
745
					 * 	before even starting something as dhclient.
746
					 * 	I do not know if this is a feature or a bug
747
					 * 	but better than track it make it work ;) .
748
					 */
749
					//$fakeiptouse = "10.254.254." . ($carp_instances_counter+1);
750
					//$cmdchain->add("CarpDEV hack", "/sbin/ifconfig {$carpint} inet {$fakeiptouse}", false);
751

    
752
        			/* generate dhclient_wan.conf */
753
        			$fd = fopen("{$g['varetc_path']}/dhclient_{$carpint}.conf", "w");
754
        			if ($fd) {
755

    
756
        				$dhclientconf = "";
757

    
758
        				$dhclientconf .= <<<EOD
759
interface "{$carpint}" {
760
timeout 60;
761
retry 1;
762
select-timeout 0;
763
initial-interval 1;
764
script "/sbin/dhclient-script";
765
}
766

    
767
EOD;
768

    
769
 			        fwrite($fd, $dhclientconf);
770
        			fclose($fd);
771

    
772
        			/* fire up dhclient */
773
					$cmdchain->add("bring CARP dhclient UP", "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$carpint}.conf {$carpint} >/tmp/{$carpint}_output >/tmp/{$carpint}_error_output", false);
774
				} else {
775
					log_error("Error: cannot open dhclient_{$carpint}.conf in interfaces_carp_configure() for writing.\n");
776
					$cmdchain->add("bring CARP dhclient UP in background", "/sbin/dhclient -b {$carpint}", false);					
777
				}
778

    
779
        		$fout = fopen("/tmp/ifconfig_{$carpint}","w");
780
        		fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$carpint}.conf {$carpint}");
781
        		fclose($fout);
782

    
783
			} else {
784
				log_error("Could not determine CarpDEV parent interface for {$vip['descr']}.");
785
			}
786
			$carp_instances_counter++;
787
			break;
788
		}
789
	}
790

    
791
	if($debugging)
792
		$cmdchain->setdebug(); // optional for verbose logging
793
	// Execute built up command chain.
794
	$cmdchain->execute();	
795

    
796
	if ($g['booting']) {
797
		unmute_kernel_msgs();
798
		echo "done.\n";
799
	}
800

    
801
	/* update cache */
802
	if ($carp_instances_counter != find_number_of_created_carp_interfaces())
803
		find_number_of_created_carp_interfaces(true);
804

    
805
}
806

    
807
function interfaces_ipalias_configure() {
808
	global $g, $config, $debugging;
809
	if(isset($config['system']['developerspew'])) {
810
		$mt = microtime();
811
		echo "interfaces_ipalias_configure() being called $mt\n";
812
	}
813
	$viparr = &$config['virtualip']['vip'];
814
	if(is_array($viparr)) {
815
		foreach ($viparr as $vip) {
816
			if ($vip['mode'] == "ipalias") {
817
				$if = get_real_wan_interface($vip['interface']);
818
				mwexec("/sbin/ifconfig " . escapeshellarg($if) . " " . $vip['subnet'] . "/" . escapeshellarg($vip['subnet_bits']) . " alias"); 
819
			}
820
		}
821
	}
822
}
823

    
824
function interface_wireless_configure($if, $wlcfg) {
825
	global $config, $g;
826

    
827
	/*    open up a shell script that will be used to output the commands.
828
	 *    since wireless is changing a lot, these series of commands are fragile
829
     *    and will sometimes need to be verified by a operator by executing the command
830
     *    and returning the output of the command to the developers for inspection.  please
831
     *    do not change this routine from a shell script to individul exec commands.  -sullrich
832
	 */
833

    
834
	conf_mount_rw();
835

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

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

    
842
	fwrite($fd_set, "# enable shell debugging\n");
843
	fwrite($fd_set, "set -x\n");
844

    
845
	/* set values for /path/program */
846
	$hostapd = "/usr/sbin/hostapd";
847
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
848
	$ifconfig = "/sbin/ifconfig";
849
	$killall = "/usr/bin/killall";
850

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

    
853
	/* Set a/b/g standard */
854
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
855

    
856
	/* Set 802.11g protection mode */
857
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
858

    
859
	/* set wireless channel value */
860
	if(isset($wlcfg['channel']))
861
		if($wlcfg['channel'] == "0")
862
			$channel = "channel any";
863
		else
864
			$channel = "channel " . escapeshellarg($wlcfg['channel']);
865

    
866
	/* set Distance value */
867
	if($wlcfg['distance'])
868
		$distance = escapeshellarg($wlcfg['distance']);
869

    
870
	/* Set ssid */
871
	if($wlcfg['ssid'])
872
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
873

    
874
	/* Set wireless hostap mode */
875
	if ($wlcfg['mode'] == "hostap")
876
		$hostapmode = "mediaopt hostap";
877
	else
878
		$hostapmode = "-mediaopt hostap";
879

    
880
	/* Set wireless adhoc mode */
881
	if ($wlcfg['mode'] == "adhoc")
882
		$adhocmode = "mediaopt adhoc";
883
	else
884
		$adhocmode = "-mediaopt adhoc";
885

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

    
888
	/* handle hide ssid option */
889
	if(isset($wlcfg['hidessid']['enable']))
890
		$hidessid = "hidessid";
891
	else
892
		$hidessid = "-hidessid";
893

    
894
	/* handle pureg (802.11g) only option */
895
	if(isset($wlcfg['pureg']['enable']))
896
		$pureg = "mode 11g pureg";
897
	else
898
		$pureg = "-pureg";
899

    
900
	/* enable apbridge option */
901
	if(isset($wlcfg['apbridge']['enable']))
902
		$apbridge = "apbridge";
903
	else
904
		$apbridge = "-apbridge";
905

    
906
	/* handle turbo option */
907
	if(isset($wlcfg['turbo']['enable']))
908
		$turbo = "mediaopt turbo";
909
	else
910
		$turbo = "-mediaopt turbo";
911

    
912
	/* handle txpower setting */
913
	if($wlcfg['txpower'] <> "")
914
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
915

    
916
	/* handle wme option */
917
	if(isset($wlcfg['wme']['enable']))
918
		$wme = "wme";
919
	else
920
		$wme = "-wme";
921

    
922
	/* set up wep if enabled */
923
    if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
924
		if($wlcfg['wpa']['auth_algs'] == "1")
925
			$wepset .= "authmode open wepmode on ";
926
		else if($wlcfg['wpa']['auth_algs'] == "2")
927
			$wepset .= "authmode shared wepmode on ";
928
		else if($wlcfg['wpa']['auth_algs'] == "3")
929
			$wepset .= "authmode mixed wepmode on ";
930
		$i = 1;
931
		foreach ($wlcfg['wep']['key'] as $wepkey) {
932
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
933
			if (isset($wepkey['txkey']))
934
				$wepset .= "weptxkey {$i} ";
935
			$i++;
936
		}
937
    } else {
938
    	$wepset .= "authmode open wepmode off ";
939
	}
940

    
941
	/* generate wpa_supplicant/hostap config if wpa is enabled */
942

    
943
	switch ($wlcfg['mode']) {
944
		case 'bss':
945
			if (isset($wlcfg['wpa']['enable'])) {
946

    
947
				$wpa .= <<<EOD
948
ctrl_interface={$g['varrun_path']}/wpa_supplicant
949
ctrl_interface_group=0
950
ap_scan=1
951
#fast_reauth=1
952
network={
953
ssid="{$wlcfg['ssid']}"
954
scan_ssid=1
955
priority=5
956
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
957
psk="{$wlcfg['wpa']['passphrase']}"
958
pairwise={$wlcfg['wpa']['wpa_pairwise']}
959
group={$wlcfg['wpa']['wpa_pairwise']}
960
}
961
EOD;
962

    
963
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
964
				fwrite($fd, "{$wpa}");
965
				fclose($fd);
966

    
967
				fwrite($fd_set, kill_wpasupplicant($if));
968
			}
969
		break;
970

    
971
		case 'hostap':
972
			if (isset($wlcfg['wpa']['enable'])) {
973
				$wpa .= <<<EOD
974
interface={$if}
975
driver=bsd
976
logger_syslog=-1
977
logger_syslog_level=0
978
logger_stdout=-1
979
logger_stdout_level=0
980
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
981
ctrl_interface={$g['varrun_path']}/hostapd
982
ctrl_interface_group=wheel
983
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
984
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
985
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
986
ssid={$wlcfg['ssid']}
987
debug={$wlcfg['wpa']['debug_mode']}
988
auth_algs={$wlcfg['wpa']['auth_algs']}
989
wpa={$wlcfg['wpa']['wpa_mode']}
990
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
991
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
992
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
993
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
994
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
995
wpa_passphrase={$wlcfg['wpa']['passphrase']}
996
ieee8021x={$wlcfg['wpa']['ieee8021x']}
997
#Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
998
#rsn_preauth=1
999
#rsn_preauth_interfaces=eth0
1000
EOD;
1001

    
1002
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
1003
				fwrite($fd, "{$wpa}");
1004
				fclose($fd);
1005

    
1006
				fwrite($fd_set, kill_hostapd($if));
1007
			}
1008
		break;
1009

    
1010
		case 'adhoc':
1011
			fwrite($fd_set, kill_hostapd($if));
1012
			fwrite($fd_set, kill_wpasupplicant($if));
1013
		break;
1014
	}
1015

    
1016
	/*
1017
	 *    all variables are set, lets start up everything
1018
     */
1019

    
1020
	/* set ack timers according to users preference (if he/she has any) */
1021
	if($distance) {
1022
		fwrite($fd_set, "# Enable ATH distance settings\n");
1023
		fwrite($fd_set, "/sbin/athctrl.sh -i {$if} -d {$distance}\n");
1024
	}
1025

    
1026
	$standard_no_turbo = str_replace(" Turbo", "", $standard);
1027

    
1028
	$settings = <<<EOD
1029

    
1030
{$ifconfig} {$if} down
1031
{$ifconfig} {$if} {$hostapmode}
1032
{$ifconfig} {$if} {$standard_no_turbo}
1033
{$ifconfig} {$if} {$channel}
1034
{$ifconfig} {$if} {$turbo}
1035
{$ifconfig} {$if} {$ssid}
1036
{$ifconfig} {$if} {$hidessid}
1037
{$ifconfig} {$if} {$adhocmode}
1038
{$ifconfig} {$if} {$protmode}
1039
{$ifconfig} {$if} {$pureg}
1040
{$ifconfig} {$if} {$apbridge}
1041
{$ifconfig} {$if} {$wme}
1042
{$ifconfig} {$if} {$wepset}
1043
{$ifconfig} {$if} {$txpower}
1044
{$ifconfig} {$if} up
1045

    
1046
EOD;
1047

    
1048
	/* write out above <<EOD stuff */
1049
	fwrite($fd_set, $settings);
1050

    
1051
	if (isset($wlcfg['wpa']['enable'])) {
1052
		if ($wlcfg['mode'] == "bss")
1053
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
1054
		if ($wlcfg['mode'] == "hostap")
1055
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
1056
	}
1057

    
1058
	fclose($fd_set);
1059

    
1060
	conf_mount_ro();
1061

    
1062
	/* execute commands now in shell */
1063
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
1064
	sleep(2);
1065
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
1066

    
1067
	return 0;
1068

    
1069
}
1070

    
1071
function kill_hostapd($interface) {
1072
	return "/bin/ps awwuxx | grep hostapd | grep $interface | awk '{ print \$2 }' | xargs kill\n";
1073
}
1074

    
1075
function kill_wpasupplicant($interface) {
1076
	return "/bin/ps awwuxx | grep wpa_supplicant | grep $interface | awk '{ print \$2 }' | xargs kill\n";
1077
}
1078

    
1079
function find_dhclient_process($interface) {
1080
	if (get_real_wan_interface($interface) <> "")
1081
        	$realinterface = get_real_wan_interface($interface);
1082
	if($realinterface)
1083
		$pid = `ps awwwux | grep dhclient | grep -v grep | grep {$realinterface} | awk '{ print \$2 }'`;
1084
	return $pid;
1085
}
1086

    
1087
function interface_configure($interface = "wan") {
1088
	global $config, $g;
1089

    
1090
	$wancfg = $config['interfaces'][$interface];
1091

    
1092
	$realif = get_real_wan_interface($interface);
1093

    
1094
	if(file_exists("/tmp/{$realif}_router")) 
1095
		unlink("/tmp/{$realif}_router");
1096

    
1097
	if(!$g['booting']) {
1098
		mute_kernel_msgs();
1099

    
1100
		/* find dhclient process for wan and kill it */
1101
		killbypid(find_dhclient_process($interface));
1102

    
1103
		/* remove wanup file if it exists */
1104
		unlink_if_exists("{$g['tmp_path']}/{$interface}up");
1105

    
1106
		/* kill PPPoE client (mpd) */
1107
		killbypid("{$g['varrun_path']}/pppoe_{$interface}.pid");
1108
		killbypid("{$g['varrun_path']}/pptp_{$interface}.pid");
1109

    
1110
		/* wait for processes to die */
1111
		sleep(3);
1112

    
1113
		unlink_if_exists("{$g['varetc_path']}/dhclient_{$interface}.conf");
1114
		unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
1115
		unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.links");
1116
		unlink_if_exists("{$g['vardb_path']}/{$interface}ip");
1117
		unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
1118
	}
1119

    
1120
	if (!$g['booting']) {
1121
		/* remove all addresses first */
1122
		while (mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " -alias") == 0);
1123
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
1124
	}
1125
	/* wireless configuration? */
1126
	if (is_array($wancfg['wireless']))
1127
		interfaces_wireless_configure($realif, $wancfg['wireless']);
1128

    
1129
	if ($wancfg['spoofmac']) {
1130
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
1131
			" link " . escapeshellarg($wancfg['spoofmac']));
1132
	}  else {
1133
		$mac = get_interface_mac_address($wancfg['if']);
1134
		if($mac == "ff:ff:ff:ff:ff:ff") {
1135
			/*   this is not a valid mac address.  generate a
1136
			 *   temporary mac address so the machine can get online.
1137
			 */
1138
			echo "Generating new MAC address.";
1139
			$random_mac = generate_random_mac_address();
1140
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
1141
				" link " . escapeshellarg($random_mac));
1142
			$wancfg['spoofmac'] = $random_mac;
1143
			write_config();
1144
			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");
1145
		}
1146
	}
1147

    
1148
	/* media */
1149
	if ($wancfg['media'] || $wancfg['mediaopt']) {
1150
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
1151
		if ($wancfg['media'])
1152
			$cmd .= " media " . escapeshellarg($wancfg['media']);
1153
		if ($wancfg['mediaopt'])
1154
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
1155
		mwexec($cmd);
1156
	}
1157

    
1158
	switch ($wancfg['ipaddr']) {
1159

    
1160
		case 'carpdev-dhcp':
1161
			interface_carpdev_dhcp_configure($interface);
1162
			break;
1163
		case 'dhcp':
1164
			interface_dhcp_configure($interface);
1165
			break;
1166

    
1167
		case 'pppoe':
1168
			interface_pppoe_configure($interface);
1169
			break;
1170

    
1171
		case 'pptp':
1172
			interface_pptp_configure($interface);
1173
			break;
1174

    
1175
		case 'bigpond':
1176
			/* just configure DHCP for now; fire up bpalogin when we've got the lease */
1177
			interface_dhcp_configure($interface);
1178
			break;
1179

    
1180
		default:
1181
			if ($wancfg['ipaddr'] <> "" && $wancfg['subnet'] <> "") {
1182
				if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
1183
				mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " " .
1184
					escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
1185
					" " . escapeshellarg($wancfg['pointtopoint']) . " up");
1186
				} else {
1187
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) .
1188
					" " . escapeshellarg($wancfg['ipaddr'] . "/" . 
1189
					$wancfg['subnet']));
1190
				}
1191
			}
1192

    
1193
			if (is_ipaddr($wancfg['gateway']))
1194
				system("echo " . $wancfg['gateway'] . " > /tmp/" . $wancfg['if'] . "_router");
1195

    
1196
	}
1197

    
1198
	mwexec("/sbin/ifconfig {$wancfg['if']} up");
1199

    
1200
	/* XXX: Shouldn't the caller do this?! */
1201
	if (!$g['booting']) {
1202
		/* XXX */
1203
		if ($interface = "lan")
1204
			/* make new hosts file */
1205
                	system_hosts_generate();
1206

    
1207
		/* reconfigure static routes (kernel may have deleted them) */
1208
		system_routing_configure();
1209

    
1210
		/* set the reload filter dity flag */
1211
		touch("{$g['tmp_path']}/filter_dirty");
1212

    
1213
		/* reload ipsec tunnels */
1214
		vpn_ipsec_configure();
1215

    
1216
		/* update dyndns */
1217
		services_dyndns_configure();
1218

    
1219
		/* force DNS update */
1220
		services_dnsupdate_process();
1221

    
1222
		/* restart dnsmasq */
1223
		services_dnsmasq_configure();
1224

    
1225
		/* reload captive portal */
1226
		captiveportal_configure();
1227
	}
1228

    
1229

    
1230
	unmute_kernel_msgs();
1231

    
1232
	return 0;
1233
}
1234

    
1235
function interface_carpdev_dhcp_configure($interface = "wan") {
1236
	global $config, $g;
1237

    
1238
	$wancfg = $config['interfaces'][$interface];
1239
	$wanif = $wancfg['if'];
1240
	/* bring wan interface up before starting dhclient */
1241
	mwexec("/sbin/ifconfig {$wanif} up");
1242

    
1243
	return 0;
1244
}
1245

    
1246
function interface_dhcp_configure($interface = "wan") {
1247
	global $config, $g;
1248

    
1249
	$wancfg = $config['interfaces'][$interface];
1250

    
1251
	/* generate dhclient_wan.conf */
1252
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
1253
	if (!$fd) {
1254
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_wan_dhcp_configure() for writing.\n");
1255
		return 1;
1256
	}
1257

    
1258
	if ($wancfg['dhcphostname']) {
1259
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
1260
		$dhclientconf_hostname .= "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
1261
	} else {
1262
		$dhclientconf_hostname = "";
1263
	}
1264

    
1265
	$wanif = get_real_wan_interface($interface);
1266

    
1267
 	$dhclientconf = "";
1268
	
1269
	$dhclientconf .= <<<EOD
1270
interface "{$wanif}" {
1271
timeout 60;
1272
retry 1;
1273
select-timeout 0;
1274
initial-interval 1;
1275
	{$dhclientconf_hostname}
1276
	script "/sbin/dhclient-script";
1277
}
1278

    
1279
EOD;
1280

    
1281
if(is_ipaddr($wancfg['alias-address'])) {
1282
	$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
1283
	$dhclientconf .= <<<EOD
1284
alias {
1285
	interface  "{$wanif}";
1286
	fixed-address {$wancfg['alias-address']};
1287
	option subnet-mask {$subnetmask};
1288
}
1289

    
1290
EOD;
1291
}
1292
	fwrite($fd, $dhclientconf);
1293
	fclose($fd);
1294

    
1295
	$relwanif = $wancfg['if'];
1296

    
1297
        /* bring wan interface up before starting dhclient */
1298
        mwexec("/sbin/ifconfig {$realwanif} up");
1299

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

    
1303
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1304
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif}");
1305
	fclose($fout);
1306

    
1307
	return 0;
1308
}
1309

    
1310
function interface_dhcp_down($interface = "wan") {
1311
	global $config;
1312
	if(get_real_wan_interface($interface) <> "")
1313
		$realinterface = get_real_wan_interface($interface);
1314
	mwexec("/sbin/ifconfig {$realinterface} down");
1315
	sleep(1);
1316
	$pid = find_dhclient_process($interface);
1317
	if($pid)
1318
		mwexec("kill {$pid}");
1319
}
1320

    
1321
function interface_dhcp_up($interface = "wan") {
1322
	interfaces_dhcp_configure($interface);
1323
	sleep(1);
1324
}
1325

    
1326
function interface_pppoe_configure($interface = "wan") {
1327
	global $config, $g;
1328

    
1329
	$wancfg = $config['interfaces'][$interface];
1330

    
1331
	/* generate mpd.conf */
1332
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1333
	if (!$fd) {
1334
		printf("Error: cannot open mpd_{$interface}.conf in interface_pppoe_configure().\n");
1335
		return 1;
1336
	}
1337

    
1338
	$idle = 0;
1339

    
1340
	if (isset($wancfg['ondemand'])) {
1341
		$ondemand = "enable";
1342
		if ($wancfg['timeout'])
1343
			$idle = $wancfg['timeout'];
1344
	} else {
1345
		$ondemand = "disable";
1346
	}
1347

    
1348
	$mpdconf = <<<EOD
1349
startup:
1350
pppoeclient:
1351

    
1352
EOD;
1353

    
1354
	if ($interface == "wan")
1355
		$realif = "pppoe0";
1356
	else {
1357
		// Here code assumes only that strings of form "opt#" will be passed.
1358
		$realif = "pppoe" . substr($interface, 3); 
1359
	}
1360
	
1361
	$mpdconf .= <<<EOD
1362
	new -i {$realif} pppoeclient pppoeclient
1363

    
1364
EOD;
1365
	if ($interface == "wan")
1366
		$mpdconf .= <<<EOD
1367
	set iface route default
1368

    
1369
EOD;
1370
	
1371
	$mpdconf .= <<<EOD
1372
	set iface {$ondemand} on-demand
1373
	set iface idle {$idle}
1374
	set iface enable tcpmssfix
1375
	set iface up-script /usr/local/sbin/ppp-linkup
1376
	set iface down-script /usr/local/sbin/ppp-linkdown
1377

    
1378
EOD;
1379

    
1380
	if (isset($wancfg['ondemand'])) {
1381
		if (isset($wancfg['local-ip']) && isset($wancfg['remote-ip'])) {
1382
			$mpdconf .= <<<EOD
1383
	set iface addrs {$wancfg['local-ip']} {$wancfg['remote-ip']}
1384

    
1385
EOD;
1386
		} else {
1387
			$mpdconf .= <<<EOD
1388
	set iface addrs 192.0.2.112 192.0.2.113
1389

    
1390
EOD;
1391
		}
1392
	}
1393

    
1394
	$mpdconf .= <<<EOD
1395
	set bundle disable multilink
1396
	set auth authname "{$wancfg['pppoe_username']}"
1397
	set auth password "{$wancfg['pppoe_password']}"
1398
	set link keep-alive 10 60
1399
	set link max-redial 0
1400
	set link no acfcomp protocomp
1401
	set link disable pap chap
1402
	set link accept chap
1403
	set link mtu 1492
1404
	set ipcp yes vjcomp
1405
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1406

    
1407
EOD;
1408

    
1409
	if (isset($config['system']['dnsallowoverride'])) {
1410
		$mpdconf .= <<<EOD
1411
	set ipcp enable req-pri-dns
1412

    
1413
EOD;
1414
	}
1415

    
1416
	if (!isset($wancfg['dnsnosec'])) {
1417
			$mpdconf .= <<<EOD
1418
	set ipcp enable req-sec-dns
1419

    
1420
EOD;
1421
	}
1422
	
1423
	$mpdconf .= <<<EOD
1424
	open
1425

    
1426
EOD;
1427

    
1428
	fwrite($fd, $mpdconf);
1429
	fclose($fd);
1430

    
1431
	/* generate mpd.links */
1432
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.links", "w");
1433
	if (!$fd) {
1434
		printf("Error: cannot open mpd_{$interface}.links in interface_pppoe_configure().\n");
1435
		return 1;
1436
	}
1437

    
1438
	$mpdconf = <<<EOD
1439
pppoeclient:
1440
	set link type pppoe
1441
	set pppoe iface {$wancfg['if']}
1442
	set pppoe service "{$wancfg['provider']}"
1443
	set pppoe enable originate
1444
	set pppoe disable incoming
1445

    
1446
EOD;
1447

    
1448
	fwrite($fd, $mpdconf);
1449
	fclose($fd);
1450

    
1451
	if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid") and $g['booting']) {
1452
		/* if we are booting and mpd has already been started then don't start again. */
1453
	} else {
1454
		/* if mpd is active, lets take it down */
1455
		if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid")) {
1456
			killbypid("{$g['varrun_path']}/pppoe_{$interface}.pid");
1457
			sleep(3);
1458
		}
1459

    
1460
		/* Bring the parent interface up */
1461
		mwexec("/sbin/ifconfig {$wancfg['if']} up");
1462

    
1463
		/* fire up mpd */
1464
		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");
1465
	}
1466

    
1467
        /* sleep until wan is up - or 30 seconds, whichever comes first */
1468
	for ($count = 0; $count < 30; $count++) {
1469
		if(file_exists("{$g['tmp_path']}/{$interface}up")) {
1470
			break;
1471
		}
1472
		sleep(1);
1473
	}
1474

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

    
1477
	return 0;
1478
}
1479

    
1480
function interface_pppoe_restart($interface = "wan") {
1481
	interface_pppoe_down($interface);
1482
	sleep(1);
1483
	interface_pppoe_up($interface);
1484
}
1485

    
1486
function interface_pppoe_down($interface = "wan") {
1487
	global $g;
1488
	sigkillbypid("{$g['varrun_path']}/pppoe_{$interface}.pid", "SIGUSR2");
1489
	sleep(1);
1490
}
1491

    
1492
function interface_pppoe_up($interface = "wan") {
1493
	global $g;
1494
	sigkillbypid("{$g['varrun_path']}/pppoe_{$interface}.pid", "SIGUSR1");
1495
	sleep(1);
1496
}
1497

    
1498
function interface_pptp_configure($interface) {
1499
	global $config, $g;
1500

    
1501
	$wancfg = $config['interfaces'][$interface];
1502

    
1503
	/* generate mpd.conf */
1504
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1505
	if (!$fd) {
1506
		printf("Error: cannot open mpd_{$interface}.conf in interface_pptp_configure().\n");
1507
		return 1;
1508
	}
1509

    
1510
	$idle = 0;
1511

    
1512
	if (isset($wancfg['ondemand'])) {
1513
		$ondemand = "enable";
1514
		if ($wancfg['timeout'])
1515
			$idle = $wancfg['timeout'];
1516
	} else {
1517
		$ondemand = "disable";
1518
	}
1519

    
1520
	$mpdconf = <<<EOD
1521
startup:
1522
pptp:
1523

    
1524
EOD;
1525

    
1526
        if ($interface == "wan")
1527
                $realif = "pptp0";
1528
        else {
1529
                // Here code assumes only that strings of form "opt#" will be passed.
1530
                $realif = "pptp" . substr($interface, 3);
1531
	}
1532

    
1533
        $mpdconf .= <<<EOD
1534
        new -i {$realif} pptp pptp 
1535

    
1536
EOD;
1537
        if ($interface == "wan")
1538
                $mpdconf .= <<<EOD
1539
        set iface route default
1540

    
1541
EOD;
1542

    
1543
        $mpdconf .= <<<EOD
1544
	set iface {$ondemand} on-demand
1545
	set iface idle {$idle}
1546
	set iface up-script /usr/local/sbin/ppp-linkup
1547
	set iface down-script /usr/local/sbin/ppp-linkdown
1548

    
1549
EOD;
1550

    
1551
	if (isset($wanfg['ondemand'])) {
1552
		$mpdconf .= <<<EOD
1553
	set iface addrs 10.0.0.1 10.0.0.2
1554

    
1555
EOD;
1556
	}
1557

    
1558
	$mpdconf .= <<<EOD
1559
	set bundle disable multilink
1560
	set bundle authname "{$wancfg['pptp_username']}"
1561
	set bundle password "{$wancfg['pptp_password']}"
1562
	set bundle no noretry
1563
	set link keep-alive 10 60
1564
	set link max-redial 0
1565
	set link no acfcomp protocomp
1566
	set link disable pap chap
1567
	set link accept chap
1568
	set ipcp no vjcomp
1569
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1570

    
1571
EOD;
1572
	if (isset($config['system']['dnsallowoverride'])) {
1573
		$mpdconf .= <<<EOD
1574
	set ipcp enable req-pri-dns
1575

    
1576
EOD;
1577
	}
1578

    
1579
	$mpdconf .= <<<EOD
1580
	open
1581

    
1582
EOD;
1583

    
1584
	fwrite($fd, $mpdconf);
1585
	fclose($fd);
1586

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

    
1594
	$mpdconf = <<<EOD
1595
pptp:
1596
	set link type pptp
1597
	set pptp enable originate outcall
1598
	set pptp disable windowing
1599
	set pptp self {$wancfg['local']}
1600
	set pptp peer {$wancfg['remote']}
1601

    
1602
EOD;
1603

    
1604
	fwrite($fd, $mpdconf);
1605
	fclose($fd);
1606

    
1607
	/* configure interface */
1608
	mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1609
		escapeshellarg($wancfg['local'] . "/" . $wancfg['subnet']) . " up");
1610

    
1611
	/* fire up mpd */
1612
	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");
1613

    
1614
	return 0;
1615
}
1616

    
1617
function interface_pptp_restart($interface = "wan") {
1618
	interface_pptp_down($interface);
1619
	sleep(1);
1620
	interface_pptp_up($interface);
1621
}
1622

    
1623
function interface_pptp_down($interface = "wan") {
1624
	global $g;
1625
	sigkillbypid("{$g['varrun_path']}/pptp_{$interface}.pid", "SIGUSR2");
1626
	sleep(1);
1627
}
1628

    
1629
function interface_pptp_up($interface = "wan") {
1630
	global $g;
1631
	sigkillbypid("{$g['varrun_path']}/pptp_{$interface}.pid", "SIGUSR1");
1632
	sleep(1);
1633
}
1634

    
1635
function interface_bigpond_configure($curwanip) {
1636
	global $config, $g;
1637

    
1638
	$bpcfg = $config['bigpond'];
1639

    
1640
	if (!$curwanip) {
1641
		/* IP address not configured yet, exit */
1642
		return 0;
1643
	}
1644

    
1645
	/* kill bpalogin */
1646
	killbyname("bpalogin");
1647

    
1648
	/* wait a moment */
1649
	sleep(1);
1650

    
1651
	/* get the default domain */
1652
	$nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
1653
	if ($nfd) {
1654
		$defaultdomain = trim(fgets($nfd));
1655
		fclose($nfd);
1656
	}
1657

    
1658
	/* generate bpalogin.conf */
1659
	$fd = fopen("{$g['varetc_path']}/bpalogin.conf", "w");
1660
	if (!$fd) {
1661
		printf("Error: cannot open bpalogin.conf in interfaces_wan_bigpond_configure().\n");
1662
		return 1;
1663
	}
1664

    
1665
	if (!$bpcfg['authserver'])
1666
		$bpcfg['authserver'] = "dce-server";
1667
	if (!$bpcfg['authdomain'])
1668
		$bpcfg['authdomain'] = $defaultdomain;
1669

    
1670
	$bpconf = <<<EOD
1671
username {$bpcfg['username']}
1672
password {$bpcfg['password']}
1673
authserver {$bpcfg['authserver']}
1674
authdomain {$bpcfg['authdomain']}
1675
localport 5050
1676

    
1677
EOD;
1678

    
1679
	if ($bpcfg['minheartbeatinterval'])
1680
		$bpconf .= "minheartbeatinterval {$bpcfg['minheartbeatinterval']}\n";
1681

    
1682
	fwrite($fd, $bpconf);
1683
	fclose($fd);
1684

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

    
1688
	return 0;
1689
}
1690

    
1691
function get_real_wan_interface($interface = "wan") {
1692
    global $config;
1693

    
1694
	$wanif = $interface;
1695

    
1696
	switch ($interface) {
1697
	case "pptp":
1698
		$wanif = "pptp";
1699
		break;
1700
	case "pppoe":
1701
		$wanif = "pppoe";
1702
		break;
1703
	case "openvpn":
1704
		$wanif = "openvpn";
1705
		break;
1706
	case "enc0":
1707
		$wanif = "enc0";
1708
		break;
1709
	/* XXX: dial in support?!
1710
	case "ppp":
1711
		$wanif = "ppp";
1712
		break;
1713
	*/
1714
	default:
1715
		$iflist = get_configured_interface_with_descr(false, true);
1716

    
1717
		foreach ($iflist as $if => $ifdesc) {
1718
			if ($interface == $if || $interface == $ifdesc) {
1719

    
1720
			$cfg = $config['interfaces'][$if];
1721

    
1722
			switch ($cfg['ipaddr']) {
1723
			case "carpdev-dhcp":
1724
				$viparr = &$config['virtualip']['vip'];
1725
				$counter = 0;
1726
				if(is_array($viparr))
1727
				foreach ($viparr as $vip) {
1728
					if ($vip['mode'] == "carpdev-dhcp") {
1729
						if($vip['interface'] == $if) {
1730
							$wanif =  "carp{$counter}";
1731
							break;
1732
						}
1733
						$counter++;
1734
					} else if ($vip['mode'] = "carp") 
1735
						$counter++;
1736
				}
1737
				break;
1738
			case "pppoe": 
1739
				if ($if == "wan")
1740
					$wanif = "pppoe0";
1741
				else
1742
					$wanif = "pppoe" . substr($if,3);
1743
				break;
1744
			case "pptp": 
1745
				if ($if == "wan")
1746
					$wanif = "pptp0";
1747
				else
1748
					$wanif = "pptp" . substr($if, 3);
1749
				break;
1750
			default:
1751
				$wanif = $cfg['if'];
1752
				break;
1753
			}
1754
			
1755
			break;
1756
			}
1757
		}
1758
		break;
1759
	}
1760

    
1761
    return $wanif;
1762
}
1763

    
1764
function get_current_wan_address($interface = "wan") {
1765
	global $config, $g;
1766

    
1767
	$realif = get_real_wan_interface($interface);
1768
	/* Do we really come here for these interfaces ?! */
1769
	if (in_array($realif, array("pptp", "pppoe", "openvpn", "enc0" /* , "ppp" */)))
1770
			return "";
1771

    
1772
	$wancfg = $config['interfaces'][$interface];
1773

    
1774
	$ifinfo = "";
1775
	switch ($wancfg['ipaddr']) {
1776
	case "dhcp":
1777
		/* get interface info with netstat */
1778
		exec("/usr/bin/netstat -nWI " . escapeshellarg($realif) . " -f inet", $ifinfo);
1779

    
1780
		if (isset($ifinfo[1])) {
1781
			$aif = preg_split("/\s+/", $ifinfo[1]);
1782
			$curwanip = chop($aif[3]);
1783

    
1784
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1785
				return $curwanip;
1786
		}
1787

    
1788
		return null;
1789
		break;
1790
	case "pppoe":
1791
	case "pptp":
1792
	case "bigpond":
1793
		/* get interface info with netstat */
1794
		exec("/usr/bin/netstat -nWI " . escapeshellarg($realif) . " -f inet", $ifinfo);
1795
		if (isset($ifinfo[1])) {
1796
			$aif = preg_split("/\s+/", $ifinfo[1]);
1797
			$curwanip = chop($aif[3]);
1798

    
1799
			if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1800
				return $curwanip;
1801
		}
1802

    
1803
		return null;
1804
		break;
1805
	 /* carpdev support */
1806
	 case "carpdev-dhcp":
1807
	        $viparr = &$config['virtualip']['vip'];
1808
	        $counter = 0;
1809
		if (is_array($viparr))
1810
	        	foreach ($viparr as $vip) {
1811
	                        if ($vip['mode'] == "carpdev-dhcp" && 
1812
	                        	$vip['interface'] == $interface) {
1813
					return str_replace("\n", "", `ifconfig carp{$counter} | grep inet | awk '{ print $2 }'`);
1814
	                        	$counter++;
1815
	                        } else if ($vip['mode'] == "carp")
1816
					$counter++;
1817
	                }
1818
		return null;
1819
		break;
1820
	 default:	
1821
		if (isset($cfg['ispointtopoint']) && $cfg['pointtopoint']) {
1822
			/* get interface info with netstat */
1823
                	exec("/usr/bin/netstat -nWI " . escapeshellarg($realif) . " -f inet", $ifinfo);
1824
                	if (isset($ifinfo[1])) {
1825
 	                       	$aif = preg_split("/\s+/", $ifinfo[1]);
1826
                       		$curwanip = chop($aif[3]);
1827
                        	if ($curwanip && is_ipaddr($curwanip) && ($curwanip != "0.0.0.0"))
1828
                                	return $curwanip;
1829
                	}
1830

    
1831
                	return null;
1832
		} else if (stristr($realif, "gre")) {
1833
			if (is_array($config['gres']['gre'])) 
1834
				foreach ($config['gres']['gre'] as $grecfg) 
1835
					if ($grecfg['greif'] == $realif)
1836
						return ($grecfg['tunnel-local-addr']);
1837
		} else if (stristr($realif, "gif")) {
1838
                        if (is_array($config['gifs']['gre']))
1839
                                foreach ($config['gifs']['gif'] as $gifcfg)
1840
                                        if ($gifcfg['gifif'] == $realif)
1841
                                                return ($gifcfg['tunnel-local-addr']);
1842
		}	
1843

    
1844
		break;
1845
	}
1846

    
1847
	/* static WAN IP address */
1848
	return $wancfg['ipaddr'];
1849
}
1850

    
1851
/****f* interfaces/is_altq_capable
1852
 * NAME
1853
 *   is_altq_capable - Test if interface is capable of using ALTQ
1854
 * INPUTS
1855
 *   $int            - string containing interface name
1856
 * RESULT
1857
 *   boolean         - true or false
1858
 ******/
1859

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

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

    
1872
        if (in_array($int_family[0], $capable))
1873
                return true;
1874
        else
1875
                return false;
1876
}
1877

    
1878
function get_wireless_modes($interface)
1879
{
1880
	/* return wireless modes and channels */
1881
	$wireless_modes = array();
1882

    
1883
	if(is_interface_wireless($interface)) {
1884
		$wi = 1;
1885
		$ifconfig = "/sbin/ifconfig";
1886
		$awk = "/usr/bin/awk";
1887
		$chan_list = "$ifconfig $interface list chan";
1888
		$stack_list = "$awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
1889
		$format_list = "$awk '{print \$5 \" \" \$6 \",\" \$1}'";
1890

    
1891
		$interface_channels = "";
1892
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
1893
		$interface_channel_count = count($interface_channels);
1894

    
1895
		$c = 0;
1896
		while ($c < $interface_channel_count)
1897
		{
1898
			$channel_line = explode(",", $interface_channels["$c"]);
1899
			$wireless_mode = trim($channel_line[0]);
1900
			$wireless_channel = trim($channel_line[1]);
1901
			if(trim($wireless_mode) != "") {
1902
				/* if we only have 11g also set 11b channels */
1903
				if($wireless_mode == "11g") {
1904
					$wireless_modes["11b"] = array();
1905
				}
1906
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
1907
			}
1908
			$c++;
1909
		}
1910
	}
1911
	return($wireless_modes);
1912
}
1913

    
1914
function get_interface_mac($interface) {
1915

    
1916
        /* build interface list with netstat */
1917
        $linkinfo = "";
1918
        exec("/usr/bin/netstat -I $interface -nW -f link", $linkinfo);
1919
        array_shift($linkinfo);
1920
        $alink = preg_split("/\s+/", $linkinfo[0]);
1921
        $mac = chop($alink[3]);
1922
        return $mac;
1923
}
1924

    
1925
?>
(15-15/37)