Project

General

Profile

Download (55.1 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	interfaces.inc
5 0e16b9ca Scott Ullrich
	Copyright (C) 2004-2006 Scott Ullrich
6 6d7de776 Ermal Luçi
	Copyright (C) 2008 Ermal Lu?i
7 ac3f8318 Espen Johansen
	All rights reserved.
8
9
	function interfaces_wireless_configure is
10
	Copyright (C) 2005 Espen Johansen
11 cfc707f7 Scott Ullrich
	All rights reserved.
12
13
	originally part of m0n0wall (http://m0n0.ch/wall)
14 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
15
	All rights reserved.
16 cfc707f7 Scott Ullrich
17 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
18
	modification, are permitted provided that the following conditions are met:
19 cfc707f7 Scott Ullrich
20 ac3f8318 Espen Johansen
	1. Redistributions of source code must retain the above copyright notices,
21 5b237745 Scott Ullrich
	   this list of conditions and the following disclaimer.
22 cfc707f7 Scott Ullrich
23 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
24 ac3f8318 Espen Johansen
	   notices, this list of conditions and the following disclaimer in the
25 5b237745 Scott Ullrich
	   documentation and/or other materials provided with the distribution.
26 cfc707f7 Scott Ullrich
27 5b237745 Scott Ullrich
	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 7387844e Chris Buechler
require_once("globals.inc");
42 5b237745 Scott Ullrich
43 b5b957fe Scott Ullrich
function interfaces_bring_up($interface) {
44
	if(!$interface) {
45
		log_error("bring_interface_up was called but no variable defined.");
46 93347bbb Scott Ullrich
		log_error( "Backtrace: " . var_dump(debug_backtrace()) );
47 b5b957fe Scott Ullrich
		return;
48
	}
49
	mwexec("/sbin/ifconfig " . escapeshellarg($interface) . " up");
50
}
51
52 f620d00d Ermal Luçi
function interfaces_loopback_configure() 
53
{
54 5b237745 Scott Ullrich
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
55 b5b957fe Scott Ullrich
	interfaces_bring_up("lo0");
56 5b237745 Scott Ullrich
	return 0;
57
}
58
59 f620d00d Ermal Luçi
function interfaces_vlan_configure() 
60
{
61 5b6eac01 Scott Ullrich
	global $config;
62 cfc707f7 Scott Ullrich
63 517feb1c Seth Mos
	$i = 0;
64 5b6eac01 Scott Ullrich
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
65
		foreach ($config['vlans']['vlan'] as $vlan) {
66 f620d00d Ermal Luçi
			if(empty($vlan['vlanif']))
67 517feb1c Seth Mos
				$vlan['vlanif'] = "vlan{$i}";
68 5b6eac01 Scott Ullrich
			/* XXX: Maybe we should report any errors?! */
69
			interface_vlan_configure($vlan['if'], $vlan['tag'], $vlan['vlanif']);
70 39f0cf1d Seth Mos
			$i++;
71 517feb1c Seth Mos
		}
72 5b6eac01 Scott Ullrich
	}
73 2075fadb Ermal Luçi
}
74 cfc707f7 Scott Ullrich
75 f620d00d Ermal Luçi
function interface_vlan_configure($if, $tag, $vlanif = "") 
76
{
77 d7147b1c Scott Ullrich
		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 2075fadb Ermal Luçi
        global $config, $g;
87 161040eb Scott Ullrich
88 2075fadb Ermal Luçi
        /* make sure the parent interface is up */
89 b5b957fe Scott Ullrich
		interfaces_bring_up($if);
90 d7147b1c Scott Ullrich
		/* 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 cfc707f7 Scott Ullrich
94 04bff45a Ermal Luçi
        if ($g['booting'] || !(empty($vlanif))) {
95
		mwexec("/sbin/ifconfig {$vlanif} destroy");
96 67ee1ec5 Ermal Luçi
		mwexec("/sbin/ifconfig {$vlanif} create");
97
        } else 
98
		$vlanif = exec("/sbin/ifconfig vlan create");
99
	
100 04bff45a Ermal Luçi
        mwexec("/sbin/ifconfig {$vlanif} vlan " .
101 2075fadb Ermal Luçi
                escapeshellarg($tag) . " vlandev " .
102 748225fa Ermal Luçi
                escapeshellarg($if));
103 2075fadb Ermal Luçi
104 b5b957fe Scott Ullrich
		interfaces_bring_up($vlanif);
105 cfc707f7 Scott Ullrich
106 2075fadb Ermal Luçi
        /* invalidate interface cache */
107
        get_interface_arr(true);
108 3f7d2120 Bill Marquette
109 2075fadb Ermal Luçi
        /*   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 cfc707f7 Scott Ullrich
119 2075fadb Ermal Luçi
        /* XXX: ermal -- for now leave it here at the moment it does not hurt. */
120 b5b957fe Scott Ullrich
		interfaces_bring_up($if);
121 cfc707f7 Scott Ullrich
122 2075fadb Ermal Luçi
        return $vlanif;
123 5b237745 Scott Ullrich
}
124
125 f620d00d Ermal Luçi
function interfaces_bridge_configure() 
126
{
127 bad29bc6 Ermal Luçi
        global $config;
128
129
        $i = 0;
130 3134528d Ermal Luçi
        if (is_array($config['bridges']['bridged']) && count($config['bridges']['bridged'])) {
131
                foreach ($config['bridges']['bridged'] as $bridge) {
132 f620d00d Ermal Luçi
                        if(empty($bridge['bridgeif']))
133 bad29bc6 Ermal Luçi
                                $bridge['bridgeif'] = "bridge{$i}";
134
                        /* XXX: Maybe we should report any errors?! */
135
                        interface_bridge_configure($bridge);
136
                        $i++;
137
                }
138
        }
139
}
140
141 f620d00d Ermal Luçi
function interface_bridge_configure(&$bridge) 
142
{
143 d7147b1c Scott Ullrich
	global $config, $g;
144 bad29bc6 Ermal Luçi
145 d7147b1c Scott Ullrich
	if (!is_array($bridge))
146
	        return -1;
147 bad29bc6 Ermal Luçi
148 dc97efaf Ermal Luçi
	if (empty($bridge['members'])) {
149
		log_error("No members found on {$bridge['bridgeif']}");
150
		return -1;
151
	}
152
153 bad29bc6 Ermal Luçi
	$members = explode(',', $bridge['members']);
154 70720671 Ermal Luçi
	if (!count($members))
155 bad29bc6 Ermal Luçi
		return -1;
156
	
157
	$checklist = get_configured_interface_list();
158
159 fded24de Ermal Luçi
	if ($g['booting'] || !empty($bridge['bridgeif'])) {
160 d7147b1c Scott Ullrich
		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 bad29bc6 Ermal Luçi
167 b64523c1 Ermal Luçi
	/* Calculate smaller mtu and enforce it */
168 69e53ef0 Ermal Luçi
	$smallermtu = 0;
169 b64523c1 Ermal Luçi
	foreach ($members as $member) {
170
		$realif = get_real_interface($member);
171
		$mtu = get_interface_mtu($realif);
172 69e53ef0 Ermal Luçi
		if ($smallermtu == 0 && !empty($mtu))
173
			$smallermtu = $mtu;
174
		else if (!empty($mtu) && $mtu < $smallermtu)
175 b64523c1 Ermal Luçi
			$smallermtu = $mtu;
176
	}
177
	 
178 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
179
	if ($smallermtu == 0)
180
		$smallermtu = 1500; 
181
182 bad29bc6 Ermal Luçi
	/* Add interfaces to bridge */
183 31241000 Ermal Luçi
	foreach ($members as $member) {
184 d7147b1c Scott Ullrich
		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 b64523c1 Ermal Luçi
		mwexec("/sbin/ifconfig {$realif} mtu {$smallermtu}");
190 d7147b1c Scott Ullrich
		if(!$realif) 
191
			log_error("realif not defined in interfaces bridge - up");
192 b5b957fe Scott Ullrich
		interfaces_bring_up($realif);
193 31241000 Ermal Luçi
		mwexec("/sbin/ifconfig {$bridgeif} addm {$realif}");	
194 d7147b1c Scott Ullrich
	}
195 31241000 Ermal Luçi
196 bad29bc6 Ermal Luçi
	if (isset($bridge['enablestp'])) {
197
		/* Choose spanning tree proto */
198
		mwexec("/sbin/ifconfig {$bridgeif} proto {$bridge['proto']}");	
199
		
200 dc97efaf Ermal Luçi
		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 bad29bc6 Ermal Luçi
		}
207 dc97efaf Ermal Luçi
		if (!empty($bridge['maxage']))
208 bad29bc6 Ermal Luçi
			mwexec("/sbin/ifconfig {$bridgeif} maxage {$bridge['maxage']}");
209 dc97efaf Ermal Luçi
		if (!empty($brige['fwdelay']))
210 bad29bc6 Ermal Luçi
			mwexec("/sbin/ifconfig {$bridgeif} fwddelay {$bridge['fwdelay']}");
211 dc97efaf Ermal Luçi
		if (!empty($brige['hellotime']))
212 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} hellotime {$bridge['hellotime']}");
213 dc97efaf Ermal Luçi
		if (!empty($brige['priority']))
214 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} priority {$bridge['priority']}");
215 dc97efaf Ermal Luçi
		if (!empty($brige['holdcount']))
216 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} holdcnt {$bridge['holdcnt']}");
217 dc97efaf Ermal Luçi
		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 bad29bc6 Ermal Luçi
		}
230 dc97efaf Ermal Luçi
		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 bad29bc6 Ermal Luçi
		}
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 85a5da13 Ermal Luçi
		$realif = get_real_interface($bridge['span']);
251 bad29bc6 Ermal Luçi
                mwexec("/sbin/ifconfig {$bridgeif} span {$realif}");
252
	}
253 a47a5798 Ermal Luçi
	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 bad29bc6 Ermal Luçi
296 d7147b1c Scott Ullrich
	if($bridgeif)
297 b5b957fe Scott Ullrich
		interfaces_bring_up($bridgeif);	
298 d7147b1c Scott Ullrich
	else 
299
		log_error("bridgeif not defined -- could not bring interface up");
300 bad29bc6 Ermal Luçi
301 d7147b1c Scott Ullrich
	return $bridgeif;
302 bad29bc6 Ermal Luçi
}
303
304 f620d00d Ermal Luçi
function interfaces_lagg_configure() 
305
{
306 cccf624b Ermal Luçi
        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 f620d00d Ermal Luçi
                        if(empty($lagg['laggif']))
312 cccf624b Ermal Luçi
                                $lagg['laggif'] = "lagg{$i}";
313
                        /* XXX: Maybe we should report any errors?! */
314
                        interface_lagg_configure($lagg);
315
                        $i++;
316
                }
317
        }
318
}
319
320 f620d00d Ermal Luçi
function interface_lagg_configure(&$lagg) 
321
{
322 cccf624b Ermal Luçi
        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 fe281019 Ermal Luçi
	$checklist = get_interface_list();
332 cccf624b Ermal Luçi
333 b64523c1 Ermal Luçi
	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 69e53ef0 Ermal Luçi
        $smallermtu = 0;
344 b64523c1 Ermal Luçi
        foreach ($members as $member) {
345
                $realif = get_real_interface($member);
346
                $mtu = get_interface_mtu($realif);
347 69e53ef0 Ermal Luçi
		if ($smallermtu == 0 && !empty($mtu))
348
			$smallermtu = $mtu;
349
                else if (!empty($mtu) && $mtu < $smallermtu)
350 b64523c1 Ermal Luçi
                        $smallermtu = $mtu;
351
        }
352
353 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
354
        if ($smallermtu == 0)
355
                $smallermtu = 1500;
356
357 cccf624b Ermal Luçi
	foreach ($members as $member) {
358
		if (!array_key_exists($member, $checklist))
359
			continue;
360 fe281019 Ermal Luçi
		$realif =  escapeshellarg($member);
361 d7147b1c Scott Ullrich
		/* make sure the parent interface is up */
362
		mwexec("/sbin/ifconfig {$realif} mtu {$smallermtu}");
363
		if($realif)
364 b5b957fe Scott Ullrich
			interfaces_bring_up($realif);
365 d7147b1c Scott Ullrich
		else 
366
			log_error("could not bring realif up -- foreach(memebers)");
367 b64523c1 Ermal Luçi
		mwexec("/sbin/ifconfig {laggif} laggport {$realif}");
368 cccf624b Ermal Luçi
	}
369 b5b957fe Scott Ullrich
	
370
	interfaces_bring_up($laggif);
371 cccf624b Ermal Luçi
372 d7147b1c Scott Ullrich
	return $laggif;
373 cccf624b Ermal Luçi
}
374
375 f620d00d Ermal Luçi
function interfaces_gre_configure() 
376
{
377 582d2452 Ermal Luçi
        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 f620d00d Ermal Luçi
                        if(empty($gre['greif']))
383 582d2452 Ermal Luçi
                                $gre['greif'] = "gre{$i}";
384
                        /* XXX: Maybe we should report any errors?! */
385
                        interface_gre_configure($gre);
386
                        $i++;
387
                }
388
        }
389
}
390
391 f620d00d Ermal Luçi
function interface_gre_configure(&$gre) 
392
{
393 582d2452 Ermal Luçi
        global $config, $g;
394
395
	if (!is_array($gre))
396
		return -1;
397
398 85a5da13 Ermal Luçi
	$realif = get_real_interface($gre['if']);
399
	$realifip = get_interface_ip($gre['if']);
400 582d2452 Ermal Luçi
401 d7147b1c Scott Ullrich
	/* make sure the parent interface is up */
402 b5b957fe Scott Ullrich
	interfaces_bring_up($realif);
403 582d2452 Ermal Luçi
404 d7147b1c Scott Ullrich
	if ($g['booting'] || !(empty($gre['greif']))) {
405
		mwexec("/sbin/ifconfig {$gre['greif']} destroy");
406
		mwexec("/sbin/ifconfig {$gre['greif']} create");
407 582d2452 Ermal Luçi
		$greif = $gre['greif'];
408 d7147b1c Scott Ullrich
	} else {
409
		$greif = exec("/sbin/ifconfig gre create");
410
	}
411 582d2452 Ermal Luçi
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 bd33ee57 Ermal Luçi
	mwexec("/sbin/ifconfig {$greif} {$gre['tunnel-local-addr']} {$gre['tunnel-remote-addr']} netmask " . gen_subnet_mask($gre['tunnel-remote-net']));
415 582d2452 Ermal Luçi
	if (isset($gre['link0']) && $gre['link0'])
416
		mwexec("/sbin/ifconfig {$greif} link0");
417 d7147b1c Scott Ullrich
	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 b5b957fe Scott Ullrich
		interfaces_bring_up($greif);
424 d7147b1c Scott Ullrich
	else 
425
		log_error("Could not bring greif up -- variable not defined.");
426 582d2452 Ermal Luçi
427
	mwexec("/sbin/route add {$gre['remote-addr']}/{$gre['tunnel-remote-net']} {$realifip}");
428 52d6ad51 Ermal Luçi
	file_put_contents("/tmp/{$greif}_router", $gre['tunnel-remote-addr']);
429 582d2452 Ermal Luçi
430
	return $greif;
431
}
432
433 f620d00d Ermal Luçi
function interfaces_gif_configure() 
434
{
435 582d2452 Ermal Luçi
        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 f620d00d Ermal Luçi
                        if(empty($gif['gifif']))
441 582d2452 Ermal Luçi
                                $gre['gifif'] = "gif{$i}";
442
                        /* XXX: Maybe we should report any errors?! */
443
                        interface_gif_configure($gif);
444
                        $i++;
445
                }
446
        }
447
}
448
449 f620d00d Ermal Luçi
function interface_gif_configure(&$gif) 
450
{
451 582d2452 Ermal Luçi
        global $config, $g;
452
453
        if (!is_array($gif))
454
                return -1;
455
456 85a5da13 Ermal Luçi
        $realif = get_real_interface($gif['if']);
457
        $realifip = get_interface_ip($gif['if']);
458 582d2452 Ermal Luçi
459
        /* make sure the parent interface is up */
460 d7147b1c Scott Ullrich
		if($realif)
461 b5b957fe Scott Ullrich
			interfaces_bring_up($realif);
462 d7147b1c Scott Ullrich
		else 
463
			log_error("could not bring realif up -- variable not defined -- interface_gif_configure()");
464 582d2452 Ermal Luçi
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 bd33ee57 Ermal Luçi
        mwexec("/sbin/ifconfig {$gifif} {$gif['tunnel-local-addr']} {$gif['tunnel-remote-addr']} netmask " . gen_subnet_mask($gif['tunnel-remote-net']));
475 582d2452 Ermal Luçi
        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 d7147b1c Scott Ullrich
		if($gifif)
480 b5b957fe Scott Ullrich
			interfaces_bring_up($gifif);
481 d7147b1c Scott Ullrich
		else
482
		 	log_error("could not bring gifif up -- variable not defined");
483 582d2452 Ermal Luçi
        mwexec("/sbin/route add {$gif['remote-addr']}/{$gif['tunnel-remote-net']} {$realifip}");
484 d7147b1c Scott Ullrich
		file_put_contents("/tmp/{$gifif}_router", $gif['tunnel-remote-addr']);
485 582d2452 Ermal Luçi
486
        return $gifif;
487
}
488
489 f620d00d Ermal Luçi
function interfaces_configure() 
490
{
491 9b1c39e3 Ermal Luçi
	global $config, $g;
492
493 d7147b1c Scott Ullrich
	/* set up VLAN virtual interfaces */
494
	interfaces_vlan_configure();
495 9b1c39e3 Ermal Luçi
496 d7147b1c Scott Ullrich
	/* set up LAGG virtual interfaces */
497
	interfaces_lagg_configure();
498 9b1c39e3 Ermal Luçi
499
	/* Set up PPP interfaces */
500
	interfaces_ppp_configure();
501 cfc707f7 Scott Ullrich
502 67ee1ec5 Ermal Luçi
	$iflist = get_configured_interface_with_descr();
503 9b1c39e3 Ermal Luçi
	$delayed_list = array();
504
	$bridge_list = array();
505 b6db9217 Ermal Luçi
	
506 67ee1ec5 Ermal Luçi
	foreach($iflist as $if => $ifname) {
507 9b1c39e3 Ermal Luçi
		$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 d7147b1c Scott Ullrich
				echo "Configuring {$ifname} interface...";
518
        	if($g['debug'])
519
				log_error("Configuring {$ifname}");
520 9b1c39e3 Ermal Luçi
521
			interface_configure($if);
522
523
			if ($g['booting']) 
524
                		echo "done.\n";
525
		}
526
	}
527
528 d7147b1c Scott Ullrich
	/* set up GRE virtual interfaces */
529
	interfaces_gre_configure();
530 9b1c39e3 Ermal Luçi
531 d7147b1c Scott Ullrich
	/* set up GIF virtual interfaces */
532
	interfaces_gif_configure();
533 9b1c39e3 Ermal Luçi
	
534
	foreach ($delayed_list as $if => $ifname) {
535
		if ($g['booting'])
536 d7147b1c Scott Ullrich
			echo "Configuring {$ifname} interface...";
537
        if($g['debug'])
538
        	log_error("Configuring {$ifname}");
539 67ee1ec5 Ermal Luçi
540 69e5a8be Ermal Luçi
		interface_configure($if);
541 4476d447 Ermal Luçi
542 9b1c39e3 Ermal Luçi
		if ($g['booting'])
543
			echo "done.\n";
544 67ee1ec5 Ermal Luçi
	}
545 cfc707f7 Scott Ullrich
546 d7147b1c Scott Ullrich
	/* set up BRIDGe virtual interfaces */
547
	interfaces_bridge_configure();
548 9b1c39e3 Ermal Luçi
549 d7147b1c Scott Ullrich
	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 9b1c39e3 Ermal Luçi
555 d7147b1c Scott Ullrich
		interface_configure($if);
556 9b1c39e3 Ermal Luçi
557 d7147b1c Scott Ullrich
		if ($g['booting'])
558
			echo "done.\n";
559
	}
560 9b1c39e3 Ermal Luçi
561 d7147b1c Scott Ullrich
	/* bring up carp interfaces */
562
	interfaces_carp_configure();
563 9b1c39e3 Ermal Luçi
564 d7147b1c Scott Ullrich
	/* bring ip IP aliases */
565
	interfaces_ipalias_configure();
566 9b1c39e3 Ermal Luçi
567 5b237745 Scott Ullrich
	if (!$g['booting']) {
568
		/* reconfigure static routes (kernel may have deleted them) */
569
		system_routing_configure();
570 cfc707f7 Scott Ullrich
571 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
572
		vpn_ipsec_configure();
573 cfc707f7 Scott Ullrich
574 f620d00d Ermal Luçi
		/* reload dhcpd (interface enabled/disabled status may have changed) */
575 5b237745 Scott Ullrich
		services_dhcpd_configure();
576 cfc707f7 Scott Ullrich
577 5b237745 Scott Ullrich
		/* restart dnsmasq */
578
		services_dnsmasq_configure();
579 4d18de6a Scott Ullrich
580 c597d50f Scott Ullrich
		/* reload captive portal */
581
		captiveportal_configure();
582
583 4d18de6a Scott Ullrich
		/* set the reload filter dity flag */
584 be38535c Ermal Luçi
		filter_configure();
585 5b237745 Scott Ullrich
	}
586 cfc707f7 Scott Ullrich
587 5b237745 Scott Ullrich
	return 0;
588
}
589
590 80bf3f4a Ermal Luçi
function interface_reconfigure($interface = "wan") 
591 f620d00d Ermal Luçi
{
592 80bf3f4a Ermal Luçi
	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 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
604 80bf3f4a Ermal Luçi
605 adec6851 Ermal Luçi
	mwexec("/usr/sbin/arp -d -i {$realif} -a");
606
607 0c56453c Ermal Luçi
        /* 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 80bf3f4a Ermal Luçi
	switch ($ifcfg['ipaddr']) {
613
	case "pppoe":
614
		killbypid("{$g['varrun_path']}/pppoe_{$interface}.pid");
615
        	sleep(2);
616 0c56453c Ermal Luçi
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
617
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.links");
618 80bf3f4a Ermal Luçi
		break;
619
	case "pptp":
620
		killbypid("{$g['varrun_path']}/pptp_{$interface}.pid");
621
        	sleep(2);
622 0c56453c Ermal Luçi
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
623
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.links");
624 80bf3f4a Ermal Luçi
		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 d8acd706 Ermal Luçi
               	sleep(1);
635 0c56453c Ermal Luçi
                unlink_if_exists("{$g['varetc_path']}/dhclient_{$interface}.conf");
636 d8acd706 Ermal Luçi
               	mwexec("/sbin/ifconfig {$realinterface} delete down");
637 80bf3f4a Ermal Luçi
		break;
638
	default:
639
		mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete down");
640
		break;
641
	}
642 eb772abd Scott Ullrich
643 80bf3f4a Ermal Luçi
	if (preg_match("/^tun|^ppp|^ovpn|^gif|^gre|^lagg|^bridge|^vlan/i", $realif))
644
                mwexec("/sbin/ifconfig {$realif} destroy");
645
	
646 0c56453c Ermal Luçi
	unlink_if_exists("/tmp/{$realif}_router");
647 80bf3f4a Ermal Luçi
	return;
648 5b237745 Scott Ullrich
}
649
650 f620d00d Ermal Luçi
function interfaces_ppp_configure() 
651
{
652 9b1c39e3 Ermal Luçi
        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 f620d00d Ermal Luçi
                        if(empty($ppp['pppif']))
658 9b1c39e3 Ermal Luçi
                                $ppp['pppif'] = "ppp{$i}";
659
                        /* XXX: Maybe we should report any errors?! */
660 2d8d4f56 Ermal Luçi
                        interface_ppp_configure($ppp);
661 9b1c39e3 Ermal Luçi
                        $i++;
662
                }
663
        }
664
}
665
666 f620d00d Ermal Luçi
function interface_ppp_configure($ifcfg) 
667
{
668 abc1cbc5 Ermal Luçi
	global $config, $g;
669 860c4e80 Chris Buechler
	
670 9b1c39e3 Ermal Luçi
	/* Remove  the /dev/ from the device name. */
671 2d8d4f56 Ermal Luçi
	$dev = substr($ifcfg['port'], 5);
672 9b1c39e3 Ermal Luçi
673 abc1cbc5 Ermal Luçi
	$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 860c4e80 Chris Buechler
	}
683
	
684 2d8d4f56 Ermal Luçi
	if ($g['booting'] || $realif <> "") {
685 228d6f9a Ermal Luçi
                mwexec("/sbin/ifconfig {$realif} destroy");
686
                mwexec("/sbin/ifconfig {$realif} create");
687
        } else
688
                $realif = exec("/sbin/ifconfig ppp create");
689
690 860c4e80 Chris Buechler
691
	$peerfile = "lcp-echo-failure 0\n";
692
	$peerfile .= "lcp-echo-interval 0\n";
693 9b1c39e3 Ermal Luçi
	$peerfile .= "connect /etc/ppp/peers/ppp{$dev}-connect-chat\n";
694
	//$peerfile .= "disconnect /etc/ppp/peers/ppp{$dev}-disconnect-chat\n";
695 abc1cbc5 Ermal Luçi
	$peerfile .= "{$ifcfg['port']} {$ifcfg['linespeed']}\n";
696 860c4e80 Chris Buechler
	$peerfile .= "crtscts\n";
697 abc1cbc5 Ermal Luçi
	if ($ifcfg['connect-max-attempts'] <> "")
698
		$peerfile .= "connect-max-attempts {$ifcfg['connect-max-attempts']}";
699 860c4e80 Chris Buechler
	$peerfile .= "local\n";
700 abc1cbc5 Ermal Luçi
	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 860c4e80 Chris Buechler
	$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 abc1cbc5 Ermal Luçi
	//$peerfile .= "nodetach\n";
716 860c4e80 Chris Buechler
	$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 abc1cbc5 Ermal Luçi
	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 860c4e80 Chris Buechler
	$chatfile .= "SAY \"\\nConnected.\"\n";
756 228d6f9a Ermal Luçi
757
	config_lock();
758
	conf_mount_rw();
759 2d8d4f56 Ermal Luçi
	safe_mkdir("/etc/ppp/peers", "0755");
760 228d6f9a Ermal Luçi
	file_put_contents("/etc/ppp/peers/ppp_{$dev}", $peerfile);
761 9b1c39e3 Ermal Luçi
	file_put_contents("/etc/ppp/peers/ppp{$dev}-connect-chat", $chatfile);
762
	chmod("/etc/ppp/peers/ppp{$dev}-connect-chat", 0755);
763 228d6f9a Ermal Luçi
	conf_mount_ro();
764
	config_unlock();
765 9b1c39e3 Ermal Luçi
	
766 abc1cbc5 Ermal Luçi
	sleep(1);
767
	mwexec("/usr/sbin/pppd call ppp_{$dev}");
768
769 9b1c39e3 Ermal Luçi
	return $realif;
770 860c4e80 Chris Buechler
}
771
772 f620d00d Ermal Luçi
function interfaces_carp_configure() 
773
{
774 87a2efd1 Ermal Luçi
	global $g, $config;
775 2b9747b9 Scott Ullrich
	$balanacing = "";
776
	$pfsyncinterface = "";
777
	$pfsyncenabled = "";
778 b932ef16 Scott Ullrich
	if(isset($config['system']['developerspew'])) {
779
		$mt = microtime();
780
		echo "interfaces_carp_configure() being called $mt\n";
781
	}
782 e5d43d93 Scott Ullrich
	// Prepare CmdCHAIN that will be used to execute commands.
783
	$cmdchain = new CmdCHAIN();	
784 6008210b Scott Ullrich
	$carp_instances_counter = 0;
785
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
786 467c2c89 Scott Ullrich
	/* destroy previous interfaces */
787 e5d43d93 Scott Ullrich
	for($x=0; $x<$total_carp_interfaces_defined; $x++) 
788
		$cmdchain->add("Delete CARP interface", "/sbin/ifconfig carp{$x} delete", false);
789 b932ef16 Scott Ullrich
	if ($g['booting']) {
790 7d0f4544 Scott Ullrich
		echo "Configuring CARP interfaces...";
791
		mute_kernel_msgs();
792 a5250ebc Scott Ullrich
	}
793 b932ef16 Scott Ullrich
	/* suck in configuration items */
794 16ccd95c Scott Ullrich
	if($config['installedpackages']['carpsettings']) 
795
		if($config['installedpackages']['carpsettings']['config']) {
796 b932ef16 Scott Ullrich
		foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
797
			$pfsyncenabled = $carp['pfsyncenabled'];
798
			$balanacing = $carp['balancing'];
799
			$pfsyncinterface = $carp['pfsyncinterface'];
800 b42ad736 Scott Ullrich
			$pfsyncpeerip = $carp['pfsyncpeerip'];
801 9f6b1429 Scott Ullrich
		}
802 b932ef16 Scott Ullrich
	} else {
803
		unset($pfsyncinterface);
804
		unset($balanacing);
805
		unset($pfsyncenabled);
806 6008210b Scott Ullrich
	}
807 79d28f42 Scott Ullrich
	$cmdchain->add("Allow CARP", "/sbin/sysctl net.inet.carp.allow=1", true);			
808 b932ef16 Scott Ullrich
	if($balanacing) {
809 79d28f42 Scott Ullrich
		$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 b932ef16 Scott Ullrich
	} else {
812 79d28f42 Scott Ullrich
		$cmdchain->add("Enable CARP preemption", "/sbin/sysctl net.inet.carp.preempt=1", true);		
813 b932ef16 Scott Ullrich
	}
814 79d28f42 Scott Ullrich
	$cmdchain->add("Enable CARP logging", "/sbin/sysctl net.inet.carp.log=2", true);
815 b932ef16 Scott Ullrich
	$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 499994ff Scott Ullrich
		exec("echo pass out quick from any to any keep state >> /tmp/rules.boot");
823 e5d43d93 Scott Ullrich
		exec("/sbin/pfctl -f /tmp/rules.boot");
824 eb772abd Scott Ullrich
	}
825 b932ef16 Scott Ullrich
	/* setup pfsync interface */
826 b42ad736 Scott Ullrich
	if($carp_sync_int and $pfsyncenabled) {
827
		if($pfsyncpeerip) {
828 e5d43d93 Scott Ullrich
			$cmdchain->add("Bring up pfsync0 syncpeer", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} syncpeer {$pfsyncpeerip} up", false);						
829 b42ad736 Scott Ullrich
		} else {
830 e5d43d93 Scott Ullrich
			$cmdchain->add("Bring up pfsync0 syncdev", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up", false);			
831 b42ad736 Scott Ullrich
		}
832
	} else {
833 e5d43d93 Scott Ullrich
		$cmdchain->add("Bring up pfsync0", "/sbin/ifconfig pfsync0 syncdev lo0 up", false);						
834 b42ad736 Scott Ullrich
	}
835 67ee1ec5 Ermal Luçi
	//$fd = fopen("/tmp/carp.sh", "w");
836 3fceab6c Scott Ullrich
	$viparr = &$config['virtualip']['vip'];
837 eb772abd Scott Ullrich
	if($config['virtualip']['vip']) {
838 79d28f42 Scott Ullrich
		$cmdchain->add("Allow CARP.", "/sbin/sysctl net.inet.carp.allow=1", true);				
839 b932ef16 Scott Ullrich
	} else {
840
		$viparr = array();
841 79d28f42 Scott Ullrich
		$cmdchain->add("Disallow CARP.", "/sbin/sysctl net.inet.carp.allow=0", true);		
842 b932ef16 Scott Ullrich
	}
843 93ca4555 Scott Ullrich
	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 e5d43d93 Scott Ullrich
	
853 87a2efd1 Ermal Luçi
	if($g['debug'])
854 e5d43d93 Scott Ullrich
		$cmdchain->setdebug(); // optional for verbose logging
855
	$cmdchain->execute();
856
	
857
	// Reset CmdCHAIN
858
	$cmdchain->clear();
859
860 67ee1ec5 Ermal Luçi
	if(is_array($viparr))
861 6008210b Scott Ullrich
	foreach ($viparr as $vip) {
862 67ee1ec5 Ermal Luçi
		$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 bb0e29e8 Scott Ullrich
			/* ensure CARP IP really exists prior to loading up */
872
			$found = false;
873 16d0ed50 Ermal Luçi
			$iflist = get_configured_interface_list();
874 bb0e29e8 Scott Ullrich
			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 67ee1ec5 Ermal Luçi
			if($found == false) {
881 ca7a3a5c Scott Ullrich
				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 b932ef16 Scott Ullrich
			/* create the carp interface and setup */
885 e5d43d93 Scott Ullrich
			$cmdchain->add("create CARP interface", "/sbin/ifconfig {$carpint} create", false);
886 3f7d2120 Bill Marquette
887
			/* invalidate interface cache */
888
			get_interface_arr(true);
889 67ee1ec5 Ermal Luçi
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
890 e5d43d93 Scott Ullrich
			$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 67ee1ec5 Ermal Luçi
			$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 e5d43d93 Scott Ullrich
				
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 67ee1ec5 Ermal Luçi
        			/* generate dhclient_wan.conf */
914
        			$fd = fopen("{$g['varetc_path']}/dhclient_{$carpint}.conf", "w");
915
        			if ($fd) {
916 d5d00b83 Scott Ullrich
917 67ee1ec5 Ermal Luçi
        				$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 e5d43d93 Scott Ullrich
					$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 d5d00b83 Scott Ullrich
				} else {
936 e5d43d93 Scott Ullrich
					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 d5d00b83 Scott Ullrich
				}
939 67ee1ec5 Ermal Luçi
940 e5d43d93 Scott Ullrich
        		$fout = fopen("/tmp/ifconfig_{$carpint}","w");
941
        		fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$carpint}.conf {$carpint}");
942
        		fclose($fout);
943 67ee1ec5 Ermal Luçi
944
			} else {
945
				log_error("Could not determine CarpDEV parent interface for {$vip['descr']}.");
946 d5d00b83 Scott Ullrich
			}
947 6008210b Scott Ullrich
			$carp_instances_counter++;
948 67ee1ec5 Ermal Luçi
			break;
949 6008210b Scott Ullrich
		}
950 67ee1ec5 Ermal Luçi
	}
951 1b58b513 Scott Ullrich
952 87a2efd1 Ermal Luçi
	if($g['debug'])
953 e5d43d93 Scott Ullrich
		$cmdchain->setdebug(); // optional for verbose logging
954
	// Execute built up command chain.
955
	$cmdchain->execute();	
956
957 3c18fbc9 Scott Ullrich
	if ($g['booting']) {
958 deebaae1 Scott Ullrich
		unmute_kernel_msgs();
959 74dbce1f Scott Ullrich
		echo "done.\n";
960 669e1adb Bill Marquette
	}
961 7b2d4769 Bill Marquette
962
	/* update cache */
963 9e097ada Bill Marquette
	if ($carp_instances_counter != find_number_of_created_carp_interfaces())
964 eb772abd Scott Ullrich
		find_number_of_created_carp_interfaces(true);
965 e5d43d93 Scott Ullrich
966 9f6b1429 Scott Ullrich
}
967
968 f620d00d Ermal Luçi
function interfaces_ipalias_configure() 
969
{
970 87a2efd1 Ermal Luçi
	global $g, $config;
971 a04de17f Chris Buechler
	if(isset($config['system']['developerspew'])) {
972
		$mt = microtime();
973
		echo "interfaces_ipalias_configure() being called $mt\n";
974
	}
975
	$viparr = &$config['virtualip']['vip'];
976 6a74c90e Scott Ullrich
	if(is_array($viparr)) {
977
		foreach ($viparr as $vip) {
978
			if ($vip['mode'] == "ipalias") {
979 85a5da13 Ermal Luçi
				$if = get_real_interface($vip['interface']);
980 1fb7c265 Ermal Luçi
				mwexec("/sbin/ifconfig " . escapeshellarg($if) . " " . $vip['subnet'] . "/" . escapeshellarg($vip['subnet_bits']) . " alias"); 
981 6a74c90e Scott Ullrich
			}
982 a04de17f Chris Buechler
		}
983
	}
984
}
985
986 f620d00d Ermal Luçi
function interface_wireless_configure($if, $wlcfg) 
987
{
988 ac3f8318 Espen Johansen
	global $config, $g;
989 eb772abd Scott Ullrich
990 4742e927 Scott Ullrich
	/*    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 eb772abd Scott Ullrich
997 8a958125 Scott Ullrich
	conf_mount_rw();
998 eb772abd Scott Ullrich
999 490b8b2a Scott Ullrich
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
1000 eb772abd Scott Ullrich
1001 4742e927 Scott Ullrich
	$fd_set = fopen("/tmp/{$if}_setup.sh","w");
1002
	fwrite($fd_set, "#!/bin/sh\n");
1003 36d0358b Scott Ullrich
	fwrite($fd_set, "# {$g['product_name']} wireless configuration script.\n\n");
1004 eb772abd Scott Ullrich
1005 4742e927 Scott Ullrich
	fwrite($fd_set, "# enable shell debugging\n");
1006
	fwrite($fd_set, "set -x\n");
1007 eb772abd Scott Ullrich
1008 2ac908dd Espen Johansen
	/* set values for /path/program */
1009
	$hostapd = "/usr/sbin/hostapd";
1010
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
1011 4742e927 Scott Ullrich
	$ifconfig = "/sbin/ifconfig";
1012
	$killall = "/usr/bin/killall";
1013 2ac908dd Espen Johansen
1014 a59abc65 Scott Ullrich
	/* Set all wireless ifconfig variables (splitt up to get rid of needed checking) */
1015 5508cf57 Scott Ullrich
1016 ac3f8318 Espen Johansen
	/* Set a/b/g standard */
1017 f4c9d138 Scott Ullrich
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
1018 5508cf57 Scott Ullrich
1019 0856c4ac Scott Ullrich
	/* Set 802.11g protection mode */
1020
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
1021
1022 ac3f8318 Espen Johansen
	/* set wireless channel value */
1023 ea9d29fa Scott Ullrich
	if(isset($wlcfg['channel']))
1024 9f6f3e04 Chris Buechler
		if($wlcfg['channel'] == "0")
1025
			$channel = "channel any";
1026
		else
1027
			$channel = "channel " . escapeshellarg($wlcfg['channel']);
1028 2ac908dd Espen Johansen
1029 f134033e Scott Ullrich
	/* set Distance value */
1030 eb772abd Scott Ullrich
	if($wlcfg['distance'])
1031 f134033e Scott Ullrich
		$distance = escapeshellarg($wlcfg['distance']);
1032
1033 ac3f8318 Espen Johansen
	/* Set ssid */
1034 eb772abd Scott Ullrich
	if($wlcfg['ssid'])
1035 191a8175 Scott Ullrich
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
1036 eb772abd Scott Ullrich
1037 ac3f8318 Espen Johansen
	/* Set wireless hostap mode */
1038 a59abc65 Scott Ullrich
	if ($wlcfg['mode'] == "hostap")
1039 ac3f8318 Espen Johansen
		$hostapmode = "mediaopt hostap";
1040
	else
1041
		$hostapmode = "-mediaopt hostap";
1042
1043
	/* Set wireless adhoc mode */
1044 a59abc65 Scott Ullrich
	if ($wlcfg['mode'] == "adhoc")
1045 ac3f8318 Espen Johansen
		$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 f4c9d138 Scott Ullrich
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
1078 eb772abd Scott Ullrich
1079 ac3f8318 Espen Johansen
	/* handle wme option */
1080
	if(isset($wlcfg['wme']['enable']))
1081
		$wme = "wme";
1082
	else
1083
		$wme = "-wme";
1084 eb772abd Scott Ullrich
1085 ac3f8318 Espen Johansen
	/* set up wep if enabled */
1086 2f19fa14 Scott Ullrich
    if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
1087 eb772abd Scott Ullrich
		if($wlcfg['wpa']['auth_algs'] == "1")
1088 2f19fa14 Scott Ullrich
			$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 eb772abd Scott Ullrich
			$wepset .= "authmode mixed wepmode on ";
1093 2f19fa14 Scott Ullrich
		$i = 1;
1094
		foreach ($wlcfg['wep']['key'] as $wepkey) {
1095
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
1096 eb772abd Scott Ullrich
			if (isset($wepkey['txkey']))
1097 2f19fa14 Scott Ullrich
				$wepset .= "weptxkey {$i} ";
1098
			$i++;
1099
		}
1100
    } else {
1101
    	$wepset .= "authmode open wepmode off ";
1102 ac3f8318 Espen Johansen
	}
1103
1104
	/* generate wpa_supplicant/hostap config if wpa is enabled */
1105
1106
	switch ($wlcfg['mode']) {
1107 b67d192d Scott Ullrich
		case 'bss':
1108 ac3f8318 Espen Johansen
			if (isset($wlcfg['wpa']['enable'])) {
1109
1110
				$wpa .= <<<EOD
1111 454756b9 Scott Ullrich
ctrl_interface={$g['varrun_path']}/wpa_supplicant
1112 50ad3b7c Scott Ullrich
ctrl_interface_group=0
1113
ap_scan=1
1114 2ac908dd Espen Johansen
#fast_reauth=1
1115 249558a2 Scott Ullrich
network={
1116 454756b9 Scott Ullrich
ssid="{$wlcfg['ssid']}"
1117
scan_ssid=1
1118 2ac908dd Espen Johansen
priority=5
1119
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
1120 454756b9 Scott Ullrich
psk="{$wlcfg['wpa']['passphrase']}"
1121 2ac908dd Espen Johansen
pairwise={$wlcfg['wpa']['wpa_pairwise']}
1122
group={$wlcfg['wpa']['wpa_pairwise']}
1123 50ad3b7c Scott Ullrich
}
1124
EOD;
1125
1126 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
1127 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
1128
				fclose($fd);
1129 8d27a5fe Espen Johansen
1130 99e72ce8 Scott Ullrich
				fwrite($fd_set, kill_wpasupplicant($if));
1131 ac3f8318 Espen Johansen
			}
1132
		break;
1133 459d6351 Scott Ullrich
1134 ac3f8318 Espen Johansen
		case 'hostap':
1135
			if (isset($wlcfg['wpa']['enable'])) {
1136
				$wpa .= <<<EOD
1137 459d6351 Scott Ullrich
interface={$if}
1138
driver=bsd
1139
logger_syslog=-1
1140
logger_syslog_level=0
1141
logger_stdout=-1
1142
logger_stdout_level=0
1143 2ac908dd Espen Johansen
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
1144
ctrl_interface={$g['varrun_path']}/hostapd
1145 459d6351 Scott Ullrich
ctrl_interface_group=wheel
1146 2ac908dd Espen Johansen
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
1147
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
1148 b67d192d Scott Ullrich
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
1149 459d6351 Scott Ullrich
ssid={$wlcfg['ssid']}
1150 2ac908dd Espen Johansen
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 ac3f8318 Espen Johansen
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 2ac908dd Espen Johansen
wpa_passphrase={$wlcfg['wpa']['passphrase']}
1159
ieee8021x={$wlcfg['wpa']['ieee8021x']}
1160 53dfd34e Espen Johansen
#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 459d6351 Scott Ullrich
EOD;
1164 2ac908dd Espen Johansen
1165 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
1166 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
1167
				fclose($fd);
1168 2ac908dd Espen Johansen
1169 99e72ce8 Scott Ullrich
				fwrite($fd_set, kill_hostapd($if));
1170 ac3f8318 Espen Johansen
			}
1171
		break;
1172 5508cf57 Scott Ullrich
1173 ac3f8318 Espen Johansen
		case 'adhoc':
1174 99e72ce8 Scott Ullrich
			fwrite($fd_set, kill_hostapd($if));
1175
			fwrite($fd_set, kill_wpasupplicant($if));
1176 ac3f8318 Espen Johansen
		break;
1177 eb772abd Scott Ullrich
	}
1178 ac3f8318 Espen Johansen
1179 4742e927 Scott Ullrich
	/*
1180
	 *    all variables are set, lets start up everything
1181
     */
1182 eb772abd Scott Ullrich
1183 78922914 Scott Ullrich
	/* set ack timers according to users preference (if he/she has any) */
1184
	if($distance) {
1185 4742e927 Scott Ullrich
		fwrite($fd_set, "# Enable ATH distance settings\n");
1186
		fwrite($fd_set, "/sbin/athctrl.sh -i {$if} -d {$distance}\n");
1187 78922914 Scott Ullrich
	}
1188 eb772abd Scott Ullrich
1189 4d857dcf Scott Ullrich
	$standard_no_turbo = str_replace(" Turbo", "", $standard);
1190 eb772abd Scott Ullrich
1191 4742e927 Scott Ullrich
	$settings = <<<EOD
1192
1193 2f19fa14 Scott Ullrich
{$ifconfig} {$if} down
1194 490b8b2a Scott Ullrich
{$ifconfig} {$if} {$standard_no_turbo}
1195 8a958125 Scott Ullrich
{$ifconfig} {$if} {$channel}
1196 4742e927 Scott Ullrich
{$ifconfig} {$if} {$turbo}
1197 8a958125 Scott Ullrich
{$ifconfig} {$if} {$ssid}
1198 4742e927 Scott Ullrich
{$ifconfig} {$if} {$hidessid}
1199 8a958125 Scott Ullrich
{$ifconfig} {$if} {$adhocmode}
1200
{$ifconfig} {$if} {$protmode}
1201 4742e927 Scott Ullrich
{$ifconfig} {$if} {$pureg}
1202
{$ifconfig} {$if} {$apbridge}
1203
{$ifconfig} {$if} {$wme}
1204
{$ifconfig} {$if} {$wepset}
1205
{$ifconfig} {$if} {$txpower}
1206 77045939 Chris Buechler
{$ifconfig} {$if} {$hostapmode}
1207 4742e927 Scott Ullrich
{$ifconfig} {$if} up
1208
1209
EOD;
1210 eb772abd Scott Ullrich
1211 4742e927 Scott Ullrich
	/* write out above <<EOD stuff */
1212
	fwrite($fd_set, $settings);
1213 da1dab20 Scott Ullrich
1214 ac3f8318 Espen Johansen
	if (isset($wlcfg['wpa']['enable'])) {
1215 eb772abd Scott Ullrich
		if ($wlcfg['mode'] == "bss")
1216 4742e927 Scott Ullrich
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
1217 eb772abd Scott Ullrich
		if ($wlcfg['mode'] == "hostap")
1218 4742e927 Scott Ullrich
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
1219 ac3f8318 Espen Johansen
	}
1220 191a8175 Scott Ullrich
1221 4742e927 Scott Ullrich
	fclose($fd_set);
1222
1223 8a958125 Scott Ullrich
	conf_mount_ro();
1224
1225 4742e927 Scott Ullrich
	/* execute commands now in shell */
1226
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
1227 99e72ce8 Scott Ullrich
	sleep(2);
1228 fed043ef Ermal Luçi
	// XXX: ermal - This seems like not needed!? 
1229
	//mwexec("/bin/sh /tmp/{$if}_setup.sh");
1230 191a8175 Scott Ullrich
1231 ac3f8318 Espen Johansen
	return 0;
1232 cfc707f7 Scott Ullrich
1233 5b237745 Scott Ullrich
}
1234
1235 f620d00d Ermal Luçi
function kill_hostapd($interface) 
1236
{
1237 99e72ce8 Scott Ullrich
	return "/bin/ps awwuxx | grep hostapd | grep $interface | awk '{ print \$2 }' | xargs kill\n";
1238 4b2a6180 Scott Ullrich
}
1239
1240 f620d00d Ermal Luçi
function kill_wpasupplicant($interface) 
1241
{
1242 99e72ce8 Scott Ullrich
	return "/bin/ps awwuxx | grep wpa_supplicant | grep $interface | awk '{ print \$2 }' | xargs kill\n";
1243 4b2a6180 Scott Ullrich
}
1244
1245 f620d00d Ermal Luçi
function find_dhclient_process($interface) 
1246
{
1247 85a5da13 Ermal Luçi
       	$realinterface = get_real_interface($interface);
1248 89fcabc4 Scott Ullrich
	if($realinterface)
1249
		$pid = `ps awwwux | grep dhclient | grep -v grep | grep {$realinterface} | awk '{ print \$2 }'`;
1250 0311dbd5 Scott Ullrich
	return $pid;
1251
}
1252
1253 f620d00d Ermal Luçi
function interface_configure($interface = "wan") 
1254
{
1255 675aac3d Ermal Luçi
	global $config, $g;
1256 cfc707f7 Scott Ullrich
1257 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1258
1259 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
1260 cfc707f7 Scott Ullrich
1261 5c6d0f65 Colin Smith
	if(!$g['booting']) {
1262 1fb7c265 Ermal Luçi
		/* remove all addresses first */
1263 332683cb Seth Mos
		while (mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " -alias", true) == 0);
1264 1fb7c265 Ermal Luçi
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
1265 039f7491 Ermal Luçi
1266
		interface_bring_down($interface);
1267 1fb7c265 Ermal Luçi
	}
1268 5b237745 Scott Ullrich
	/* wireless configuration? */
1269
	if (is_array($wancfg['wireless']))
1270 2d8d4f56 Ermal Luçi
		interface_wireless_configure($realif, $wancfg['wireless']);
1271 cfc707f7 Scott Ullrich
1272 f36d4bd2 Scott Ullrich
	if ($wancfg['spoofmac']) {
1273 1fb7c265 Ermal Luçi
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
1274 5b237745 Scott Ullrich
			" link " . escapeshellarg($wancfg['spoofmac']));
1275 f36d4bd2 Scott Ullrich
	}  else {
1276 1fb7c265 Ermal Luçi
		$mac = get_interface_mac_address($wancfg['if']);
1277 f36d4bd2 Scott Ullrich
		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 9315ef83 Scott Ullrich
			echo "Generating new MAC address.";
1282 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
1283 1fb7c265 Ermal Luçi
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
1284 f36d4bd2 Scott Ullrich
				" link " . escapeshellarg($random_mac));
1285
			$wancfg['spoofmac'] = $random_mac;
1286
			write_config();
1287 571f89fa Ermal Luçi
			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 f36d4bd2 Scott Ullrich
		}
1289
	}
1290 cfc707f7 Scott Ullrich
1291 5b237745 Scott Ullrich
	/* media */
1292
	if ($wancfg['media'] || $wancfg['mediaopt']) {
1293 1fb7c265 Ermal Luçi
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
1294 5b237745 Scott Ullrich
		if ($wancfg['media'])
1295
			$cmd .= " media " . escapeshellarg($wancfg['media']);
1296
		if ($wancfg['mediaopt'])
1297
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
1298
		mwexec($cmd);
1299
	}
1300 cfc707f7 Scott Ullrich
1301 ccbd2447 Ermal Luçi
	/* invalidate interface cache */
1302
        get_interface_arr(true);
1303
1304 5b237745 Scott Ullrich
	switch ($wancfg['ipaddr']) {
1305 cfc707f7 Scott Ullrich
1306 d5d00b83 Scott Ullrich
		case 'carpdev-dhcp':
1307 1fb7c265 Ermal Luçi
			interface_carpdev_dhcp_configure($interface);
1308 d5d00b83 Scott Ullrich
			break;
1309 5b237745 Scott Ullrich
		case 'dhcp':
1310 1fb7c265 Ermal Luçi
			interface_dhcp_configure($interface);
1311 5b237745 Scott Ullrich
			break;
1312 cfc707f7 Scott Ullrich
1313 5b237745 Scott Ullrich
		case 'pppoe':
1314 1fb7c265 Ermal Luçi
			interface_pppoe_configure($interface);
1315 5b237745 Scott Ullrich
			break;
1316 cfc707f7 Scott Ullrich
1317 5b237745 Scott Ullrich
		case 'pptp':
1318 1fb7c265 Ermal Luçi
			interface_pptp_configure($interface);
1319 5b237745 Scott Ullrich
			break;
1320 cfc707f7 Scott Ullrich
1321 5b237745 Scott Ullrich
		default:
1322 4b176ed2 Ermal Luçi
			if ($wancfg['ipaddr'] <> "" && $wancfg['subnet'] <> "") {
1323
				if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
1324 d7147b1c Scott Ullrich
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " " .
1325
						escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
1326
						" " . escapeshellarg($wancfg['pointtopoint']) . " up");
1327 4b176ed2 Ermal Luçi
				} else {
1328
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) .
1329 d7147b1c Scott Ullrich
						" " . escapeshellarg($wancfg['ipaddr'] . "/" . 
1330
						$wancfg['subnet']));
1331 4b176ed2 Ermal Luçi
				}
1332 a23d7248 Scott Ullrich
			}
1333 cfc707f7 Scott Ullrich
1334 67ee1ec5 Ermal Luçi
			if (is_ipaddr($wancfg['gateway']))
1335 723e16b8 Ermal Luçi
				file_put_contents("/tmp/{$realif}_router", $wancfg['gateway']);
1336 5b237745 Scott Ullrich
	}
1337 d7147b1c Scott Ullrich
	if($wancfg['if'])
1338 b5b957fe Scott Ullrich
		interfaces_bring_up($wancfg['if']);
1339 d7147b1c Scott Ullrich
	else 
1340
		log_error("Could not bring wancfg['if'] up -- variable not defined in interface_configure()");
1341 1f15f43f Ermal Luçi
	
1342 5b237745 Scott Ullrich
	if (!$g['booting']) {
1343 ccbd2447 Ermal Luçi
		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 4476d447 Ermal Luçi
		/* XXX */
1361
		if ($interface = "lan")
1362
			/* make new hosts file */
1363
                	system_hosts_generate();
1364
1365 5b237745 Scott Ullrich
		/* reconfigure static routes (kernel may have deleted them) */
1366
		system_routing_configure();
1367 cfc707f7 Scott Ullrich
1368 e239df5a Scott Ullrich
		/* set the reload filter dity flag */
1369 be38535c Ermal Luçi
		filter_configure();
1370 cfc707f7 Scott Ullrich
1371 5b237745 Scott Ullrich
		/* reload ipsec tunnels */
1372
		vpn_ipsec_configure();
1373 cfc707f7 Scott Ullrich
1374 4476d447 Ermal Luçi
		/* update dyndns */
1375 5b237745 Scott Ullrich
		services_dyndns_configure();
1376 cfc707f7 Scott Ullrich
1377 a23d7248 Scott Ullrich
		/* force DNS update */
1378
		services_dnsupdate_process();
1379
1380 5b237745 Scott Ullrich
		/* restart dnsmasq */
1381
		services_dnsmasq_configure();
1382 eb772abd Scott Ullrich
1383 c597d50f Scott Ullrich
		/* reload captive portal */
1384
		captiveportal_configure();
1385 5b237745 Scott Ullrich
	}
1386 cfc707f7 Scott Ullrich
1387 5e99d81a Scott Ullrich
1388 c1627786 Scott Ullrich
	unmute_kernel_msgs();
1389
1390 5b237745 Scott Ullrich
	return 0;
1391
}
1392
1393 f620d00d Ermal Luçi
function interface_carpdev_dhcp_configure($interface = "wan") 
1394
{
1395 d5d00b83 Scott Ullrich
	global $config, $g;
1396
1397 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1398 499994ff Scott Ullrich
	$wanif = $wancfg['if'];
1399 d5d00b83 Scott Ullrich
	/* bring wan interface up before starting dhclient */
1400 d7147b1c Scott Ullrich
	if($wanif)
1401 b5b957fe Scott Ullrich
		interfaces_bring_up($wanif);
1402 d7147b1c Scott Ullrich
	else 
1403
		log_error("Could not bring wanif up in terface_carpdev_dhcp_configure()");
1404 d5d00b83 Scott Ullrich
1405
	return 0;
1406
}
1407
1408 f620d00d Ermal Luçi
function interface_dhcp_configure($interface = "wan") 
1409
{
1410 5b237745 Scott Ullrich
	global $config, $g;
1411 cfc707f7 Scott Ullrich
1412 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1413 5b237745 Scott Ullrich
1414 0311dbd5 Scott Ullrich
	/* generate dhclient_wan.conf */
1415 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
1416 5b237745 Scott Ullrich
	if (!$fd) {
1417 67ee1ec5 Ermal Luçi
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_wan_dhcp_configure() for writing.\n");
1418 5b237745 Scott Ullrich
		return 1;
1419
	}
1420 eb772abd Scott Ullrich
1421 2305d4c5 Scott Ullrich
	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 85a5da13 Ermal Luçi
	$wanif = get_real_interface($interface);
1429 cfc707f7 Scott Ullrich
1430 67ee1ec5 Ermal Luçi
 	$dhclientconf = "";
1431
	
1432 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
1433 67ee1ec5 Ermal Luçi
interface "{$wanif}" {
1434 76d3b9a3 Chris Buechler
timeout 60;
1435 ce69a638 Scott Ullrich
retry 1;
1436
select-timeout 0;
1437
initial-interval 1;
1438 2305d4c5 Scott Ullrich
	{$dhclientconf_hostname}
1439
	script "/sbin/dhclient-script";
1440 5b237745 Scott Ullrich
}
1441
1442
EOD;
1443
1444 bc40d758 Seth Mos
if(is_ipaddr($wancfg['alias-address'])) {
1445
	$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
1446
	$dhclientconf .= <<<EOD
1447
alias {
1448 67ee1ec5 Ermal Luçi
	interface  "{$wanif}";
1449 bc40d758 Seth Mos
	fixed-address {$wancfg['alias-address']};
1450
	option subnet-mask {$subnetmask};
1451
}
1452
1453
EOD;
1454
}
1455 5b237745 Scott Ullrich
	fwrite($fd, $dhclientconf);
1456
	fclose($fd);
1457 eb772abd Scott Ullrich
1458 67ee1ec5 Ermal Luçi
	$relwanif = $wancfg['if'];
1459 eb772abd Scott Ullrich
1460 d7147b1c Scott Ullrich
	/* bring wan interface up before starting dhclient */
1461
	if($realwanif)
1462 b5b957fe Scott Ullrich
		interfaces_bring_up($realwanif);
1463
	else 
1464
		log_error("Could not bring realwanif up in interface_dhcp_configure()");
1465 eacc8c14 Scott Ullrich
1466 d7147b1c Scott Ullrich
	/* fire up dhclient */
1467
	mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
1468 cfc707f7 Scott Ullrich
1469 fdca0ea8 Scott Ullrich
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1470 67ee1ec5 Ermal Luçi
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif}");
1471 0119d2f7 Scott Ullrich
	fclose($fout);
1472
1473 5b237745 Scott Ullrich
	return 0;
1474
}
1475
1476 f620d00d Ermal Luçi
function interface_pppoe_configure($interface = "wan") 
1477
{
1478 5b237745 Scott Ullrich
	global $config, $g;
1479 cfc707f7 Scott Ullrich
1480 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1481 cfc707f7 Scott Ullrich
1482 5b237745 Scott Ullrich
	/* generate mpd.conf */
1483 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1484 5b237745 Scott Ullrich
	if (!$fd) {
1485 1fb7c265 Ermal Luçi
		printf("Error: cannot open mpd_{$interface}.conf in interface_pppoe_configure().\n");
1486 5b237745 Scott Ullrich
		return 1;
1487
	}
1488 cfc707f7 Scott Ullrich
1489 5b237745 Scott Ullrich
	$idle = 0;
1490 cfc707f7 Scott Ullrich
1491 67ee1ec5 Ermal Luçi
	if (isset($wancfg['ondemand'])) {
1492 5b237745 Scott Ullrich
		$ondemand = "enable";
1493 67ee1ec5 Ermal Luçi
		if ($wancfg['timeout'])
1494
			$idle = $wancfg['timeout'];
1495 5b237745 Scott Ullrich
	} else {
1496
		$ondemand = "disable";
1497
	}
1498 cfc707f7 Scott Ullrich
1499 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1500 07cae4b2 Scott Ullrich
startup:
1501
pppoeclient:
1502 cc936773 Scott Ullrich
1503 5b237745 Scott Ullrich
EOD;
1504 389741e5 Scott Ullrich
1505 67ee1ec5 Ermal Luçi
	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 389741e5 Scott Ullrich
		$mpdconf .= <<<EOD
1518 67ee1ec5 Ermal Luçi
	set iface route default
1519
1520
EOD;
1521
	
1522
	$mpdconf .= <<<EOD
1523
	set iface {$ondemand} on-demand
1524
	set iface idle {$idle}
1525 0750014f Ermal Luçi
	set iface enable tcpmssfix
1526 67ee1ec5 Ermal Luçi
	set iface up-script /usr/local/sbin/ppp-linkup
1527 389741e5 Scott Ullrich
	set iface down-script /usr/local/sbin/ppp-linkdown
1528
1529
EOD;
1530
1531 67ee1ec5 Ermal Luçi
	if (isset($wancfg['ondemand'])) {
1532
		if (isset($wancfg['local-ip']) && isset($wancfg['remote-ip'])) {
1533 41404ef1 Scott Ullrich
			$mpdconf .= <<<EOD
1534 67ee1ec5 Ermal Luçi
	set iface addrs {$wancfg['local-ip']} {$wancfg['remote-ip']}
1535 5b237745 Scott Ullrich
1536
EOD;
1537 41404ef1 Scott Ullrich
		} else {
1538
			$mpdconf .= <<<EOD
1539
	set iface addrs 192.0.2.112 192.0.2.113
1540
1541
EOD;
1542
		}
1543 5b237745 Scott Ullrich
	}
1544 cfc707f7 Scott Ullrich
1545 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
1546
	set bundle disable multilink
1547 8da53af8 Ermal Luçi
	set auth authname "{$wancfg['pppoe_username']}"
1548
	set auth password "{$wancfg['pppoe_password']}"
1549 5b237745 Scott Ullrich
	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 b4914b78 Ermal Luçi
	
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 5b237745 Scott Ullrich
	set ipcp yes vjcomp
1564
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1565 a23d7248 Scott Ullrich
1566
EOD;
1567
1568
	if (isset($config['system']['dnsallowoverride'])) {
1569
		$mpdconf .= <<<EOD
1570 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
1571 a23d7248 Scott Ullrich
1572
EOD;
1573
	}
1574 a0ff9696 Scott Ullrich
1575 67ee1ec5 Ermal Luçi
	if (!isset($wancfg['dnsnosec'])) {
1576 98e392c5 Scott Ullrich
			$mpdconf .= <<<EOD
1577
	set ipcp enable req-sec-dns
1578
1579
EOD;
1580
	}
1581
	
1582 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
1583 07cae4b2 Scott Ullrich
	open
1584 5b237745 Scott Ullrich
1585
EOD;
1586
1587
	fwrite($fd, $mpdconf);
1588
	fclose($fd);
1589 cfc707f7 Scott Ullrich
1590 5b237745 Scott Ullrich
	/* generate mpd.links */
1591 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.links", "w");
1592 5b237745 Scott Ullrich
	if (!$fd) {
1593 1fb7c265 Ermal Luçi
		printf("Error: cannot open mpd_{$interface}.links in interface_pppoe_configure().\n");
1594 5b237745 Scott Ullrich
		return 1;
1595
	}
1596 cfc707f7 Scott Ullrich
1597 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1598 07cae4b2 Scott Ullrich
pppoeclient:
1599 5b237745 Scott Ullrich
	set link type pppoe
1600
	set pppoe iface {$wancfg['if']}
1601 67ee1ec5 Ermal Luçi
	set pppoe service "{$wancfg['provider']}"
1602 5b237745 Scott Ullrich
	set pppoe enable originate
1603
	set pppoe disable incoming
1604
1605
EOD;
1606
1607
	fwrite($fd, $mpdconf);
1608
	fclose($fd);
1609 eb772abd Scott Ullrich
1610 67ee1ec5 Ermal Luçi
	if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid") and $g['booting']) {
1611 d7a6517a Scott Ullrich
		/* 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 67ee1ec5 Ermal Luçi
		if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid")) {
1615
			killbypid("{$g['varrun_path']}/pppoe_{$interface}.pid");
1616 d7a6517a Scott Ullrich
			sleep(3);
1617
		}
1618 571f89fa Ermal Luçi
1619
		/* Bring the parent interface up */
1620 d7147b1c Scott Ullrich
		if($wancfg['if'])
1621 b5b957fe Scott Ullrich
			interfaces_bring_up($wancfg['if']);
1622 d7147b1c Scott Ullrich
		else 
1623
			log_error("Could not bring wancfg['if'] up in interface_pppoe_configure()");
1624 571f89fa Ermal Luçi
1625 eb772abd Scott Ullrich
		/* fire up mpd */
1626 67ee1ec5 Ermal Luçi
		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 ec11a1ad Scott Ullrich
	}
1628
1629 b5b957fe Scott Ullrich
	/* sleep until wan is up - or 30 seconds, whichever comes first */
1630 a205d904 Scott Ullrich
	for ($count = 0; $count < 30; $count++) {
1631 571f89fa Ermal Luçi
		if(file_exists("{$g['tmp_path']}/{$interface}up")) {
1632 a205d904 Scott Ullrich
			break;
1633
		}
1634
		sleep(1);
1635
	}
1636 d7a6517a Scott Ullrich
1637 571f89fa Ermal Luçi
	unlink_if_exists("{$g['tmp_path']}/{$interface}up");
1638 e1c8cdf5 Scott Ullrich
1639 5b237745 Scott Ullrich
	return 0;
1640
}
1641
1642 f620d00d Ermal Luçi
function interface_pptp_configure($interface) 
1643
{
1644 5b237745 Scott Ullrich
	global $config, $g;
1645 cfc707f7 Scott Ullrich
1646 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1647 cfc707f7 Scott Ullrich
1648 5b237745 Scott Ullrich
	/* generate mpd.conf */
1649 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1650 5b237745 Scott Ullrich
	if (!$fd) {
1651 1fb7c265 Ermal Luçi
		printf("Error: cannot open mpd_{$interface}.conf in interface_pptp_configure().\n");
1652 5b237745 Scott Ullrich
		return 1;
1653
	}
1654 cfc707f7 Scott Ullrich
1655 5b237745 Scott Ullrich
	$idle = 0;
1656 cfc707f7 Scott Ullrich
1657 67ee1ec5 Ermal Luçi
	if (isset($wancfg['ondemand'])) {
1658 5b237745 Scott Ullrich
		$ondemand = "enable";
1659 67ee1ec5 Ermal Luçi
		if ($wancfg['timeout'])
1660
			$idle = $wancfg['timeout'];
1661 5b237745 Scott Ullrich
	} else {
1662
		$ondemand = "disable";
1663
	}
1664 cfc707f7 Scott Ullrich
1665 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1666 67ee1ec5 Ermal Luçi
startup:
1667 5b237745 Scott Ullrich
pptp:
1668
1669
EOD;
1670 cfc707f7 Scott Ullrich
1671 67ee1ec5 Ermal Luçi
        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 389741e5 Scott Ullrich
	set iface down-script /usr/local/sbin/ppp-linkdown
1693
1694
EOD;
1695
1696 67ee1ec5 Ermal Luçi
	if (isset($wanfg['ondemand'])) {
1697 5b237745 Scott Ullrich
		$mpdconf .= <<<EOD
1698 a23d7248 Scott Ullrich
	set iface addrs 10.0.0.1 10.0.0.2
1699 5b237745 Scott Ullrich
1700
EOD;
1701
	}
1702 cfc707f7 Scott Ullrich
1703 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
1704
	set bundle disable multilink
1705 8da53af8 Ermal Luçi
	set bundle authname "{$wancfg['pptp_username']}"
1706
	set bundle password "{$wancfg['pptp_password']}"
1707 58384045 Chris Buechler
	set bundle no noretry
1708 5b237745 Scott Ullrich
	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 a23d7248 Scott Ullrich
1716
EOD;
1717
	if (isset($config['system']['dnsallowoverride'])) {
1718
		$mpdconf .= <<<EOD
1719 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
1720 a23d7248 Scott Ullrich
1721
EOD;
1722
	}
1723 a0ff9696 Scott Ullrich
1724 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
1725 5b237745 Scott Ullrich
	open
1726
1727
EOD;
1728
1729
	fwrite($fd, $mpdconf);
1730
	fclose($fd);
1731 cfc707f7 Scott Ullrich
1732 5b237745 Scott Ullrich
	/* generate mpd.links */
1733 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.links", "w");
1734 5b237745 Scott Ullrich
	if (!$fd) {
1735 1fb7c265 Ermal Luçi
		printf("Error: cannot open mpd_{$interface}.links in interface_pptp_configure().\n");
1736 5b237745 Scott Ullrich
		return 1;
1737
	}
1738 cfc707f7 Scott Ullrich
1739 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1740
pptp:
1741
	set link type pptp
1742
	set pptp enable originate outcall
1743
	set pptp disable windowing
1744 67ee1ec5 Ermal Luçi
	set pptp self {$wancfg['local']}
1745
	set pptp peer {$wancfg['remote']}
1746 5b237745 Scott Ullrich
1747
EOD;
1748
1749
	fwrite($fd, $mpdconf);
1750
	fclose($fd);
1751 cfc707f7 Scott Ullrich
1752 5b237745 Scott Ullrich
	/* configure interface */
1753 d7147b1c Scott Ullrich
	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 5b237745 Scott Ullrich
	/* fire up mpd */
1759 67ee1ec5 Ermal Luçi
	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 cfc707f7 Scott Ullrich
1761 5b237745 Scott Ullrich
	return 0;
1762
}
1763 f6b761fb Scott Ullrich
 
1764 abb31ea4 Ermal Luçi
/* XXX: stub for code that references the old functions(mostly packages) */
1765 f6b761fb Scott Ullrich
function get_real_wan_interface($interface = "wan") 
1766 abb31ea4 Ermal Luçi
{
1767
	return get_real_interface($interface);
1768
}
1769 f6b761fb Scott Ullrich
function get_current_wan_address($interface = "wan")
1770 abb31ea4 Ermal Luçi
{
1771
	return get_interface_ip($interface);
1772
}
1773
1774 85a5da13 Ermal Luçi
function get_real_interface($interface = "wan") 
1775 f620d00d Ermal Luçi
{
1776 67ee1ec5 Ermal Luçi
    global $config;
1777 cfc707f7 Scott Ullrich
1778 67ee1ec5 Ermal Luçi
	$wanif = $interface;
1779 c515ea57 Scott Ullrich
1780 67ee1ec5 Ermal Luçi
	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 008760d0 Ermal Luçi
		$iflist = get_configured_interface_with_descr(false, true);
1800 67ee1ec5 Ermal Luçi
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 c515ea57 Scott Ullrich
				}
1821 67ee1ec5 Ermal Luçi
				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 9b1c39e3 Ermal Luçi
				$wanif = $cfg['if'];
1836 67ee1ec5 Ermal Luçi
				break;
1837
			}
1838
			
1839
			break;
1840 c515ea57 Scott Ullrich
			}
1841
		}
1842 67ee1ec5 Ermal Luçi
		break;
1843 c515ea57 Scott Ullrich
	}
1844
1845 67ee1ec5 Ermal Luçi
    return $wanif;
1846 5b237745 Scott Ullrich
}
1847
1848 85a5da13 Ermal Luçi
function get_interface_ip($interface = "wan") 
1849 f620d00d Ermal Luçi
{
1850 5b237745 Scott Ullrich
	global $config, $g;
1851 cfc707f7 Scott Ullrich
1852 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
1853 67ee1ec5 Ermal Luçi
	/* Do we really come here for these interfaces ?! */
1854
	if (in_array($realif, array("pptp", "pppoe", "openvpn", "enc0" /* , "ppp" */)))
1855
			return "";
1856 cfc707f7 Scott Ullrich
1857 85a5da13 Ermal Luçi
	$curip = find_interface_ip($realif);
1858
	if ($curip && is_ipaddr($curip) && ($curip != "0.0.0.0"))
1859
		return $curip;
1860 67ee1ec5 Ermal Luçi
1861 85a5da13 Ermal Luçi
	return null;
1862 5b237745 Scott Ullrich
}
1863
1864 a57b119e Bill Marquette
/****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 f620d00d Ermal Luçi
function is_altq_capable($int) 
1874
{
1875 a57b119e Bill Marquette
        /* 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 acdbfa04 Scott Ullrich
			"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 a57b119e Bill Marquette
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 d8c67d69 Scott Ullrich
function get_wireless_modes($interface)
1893
{
1894
	/* return wireless modes and channels */
1895 92f7d37d Ermal Luçi
	$wireless_modes = array();
1896
1897 d8c67d69 Scott Ullrich
	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 4066776d Scott Ullrich
		$stack_list = "$awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
1903 d8c67d69 Scott Ullrich
		$format_list = "$awk '{print \$5 \" \" \$6 \",\" \$1}'";
1904
1905 4b0e71db Scott Ullrich
		$interface_channels = "";
1906 d8c67d69 Scott Ullrich
		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 4066776d Scott Ullrich
			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 d8c67d69 Scott Ullrich
			$c++;
1923
		}
1924
	}
1925 4066776d Scott Ullrich
	return($wireless_modes);
1926 d8c67d69 Scott Ullrich
}
1927
1928 f620d00d Ermal Luçi
function get_interface_mac($interface) 
1929
{
1930 b7ec2b9e Scott Ullrich
1931
        /* build interface list with netstat */
1932 84e5047d Scott Ullrich
        $linkinfo = "";
1933 b7ec2b9e Scott Ullrich
        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 b5b957fe Scott Ullrich
?>