Project

General

Profile

Download (56.3 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	interfaces.inc
5 eba938e3 Scott Ullrich
	Copyright (C) 2004-2008 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 eba938e3 Scott Ullrich
function interfaces_loopback_configure() {
53 5b237745 Scott Ullrich
	mwexec("/sbin/ifconfig lo0 127.0.0.1");
54 b5b957fe Scott Ullrich
	interfaces_bring_up("lo0");
55 5b237745 Scott Ullrich
	return 0;
56
}
57
58 eba938e3 Scott Ullrich
function interfaces_vlan_configure() {
59 5b6eac01 Scott Ullrich
	global $config;
60 517feb1c Seth Mos
	$i = 0;
61 5b6eac01 Scott Ullrich
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
62
		foreach ($config['vlans']['vlan'] as $vlan) {
63 f620d00d Ermal Luçi
			if(empty($vlan['vlanif']))
64 517feb1c Seth Mos
				$vlan['vlanif'] = "vlan{$i}";
65 5b6eac01 Scott Ullrich
			/* XXX: Maybe we should report any errors?! */
66
			interface_vlan_configure($vlan['if'], $vlan['tag'], $vlan['vlanif']);
67 39f0cf1d Seth Mos
			$i++;
68 517feb1c Seth Mos
		}
69 5b6eac01 Scott Ullrich
	}
70 2075fadb Ermal Luçi
}
71 cfc707f7 Scott Ullrich
72 eba938e3 Scott Ullrich
function interface_vlan_configure($if, $tag, $vlanif = "") {
73 2075fadb Ermal Luçi
        global $config, $g;
74 161040eb Scott Ullrich
75 3ae4960c Ermal Luçi
	if(empty($if)) {
76
		log_error("interface_vlan_confgure called with if defined.");
77
		return;
78
	}
79
	if(empty($vlanif)) {
80
		log_error("interface_vlan_confgure called with vlanif defined.");
81
		return;
82
	}
83
84 2075fadb Ermal Luçi
        /* make sure the parent interface is up */
85 b5b957fe Scott Ullrich
		interfaces_bring_up($if);
86 d7147b1c Scott Ullrich
		/* Since we are going to add vlan(4) try to enable all that hardware supports. */
87
		mwexec("/sbin/ifconfig {$if} vlanhwtag");
88
		mwexec("/sbin/ifconfig {$if} vlanmtu");
89 cfc707f7 Scott Ullrich
90 04bff45a Ermal Luçi
        if ($g['booting'] || !(empty($vlanif))) {
91
		mwexec("/sbin/ifconfig {$vlanif} destroy");
92 67ee1ec5 Ermal Luçi
		mwexec("/sbin/ifconfig {$vlanif} create");
93
        } else 
94
		$vlanif = exec("/sbin/ifconfig vlan create");
95
	
96 04bff45a Ermal Luçi
        mwexec("/sbin/ifconfig {$vlanif} vlan " .
97 2075fadb Ermal Luçi
                escapeshellarg($tag) . " vlandev " .
98 748225fa Ermal Luçi
                escapeshellarg($if));
99 2075fadb Ermal Luçi
100 b5b957fe Scott Ullrich
		interfaces_bring_up($vlanif);
101 cfc707f7 Scott Ullrich
102 2075fadb Ermal Luçi
        /* invalidate interface cache */
103
        get_interface_arr(true);
104 3f7d2120 Bill Marquette
105 2075fadb Ermal Luçi
        /*   all vlans need to spoof their parent mac address, too.  see
106
         *   ticket #1514: http://cvstrac.pfsense.com/tktview?tn=1514,33
107
         */
108
        foreach($config['interfaces'] as $interfaces) {
109
                if($interfaces['if'] == $if && $interfaces['spoofmac']) {
110
                        mwexec("/sbin/ifconfig " . escapeshellarg($if) .
111
                                " link " . escapeshellarg($interfaces['spoofmac']));
112
                }
113
        }
114 cfc707f7 Scott Ullrich
115 2075fadb Ermal Luçi
        /* XXX: ermal -- for now leave it here at the moment it does not hurt. */
116 b5b957fe Scott Ullrich
		interfaces_bring_up($if);
117 cfc707f7 Scott Ullrich
118 2075fadb Ermal Luçi
        return $vlanif;
119 5b237745 Scott Ullrich
}
120
121 eba938e3 Scott Ullrich
function interfaces_bridge_configure() {
122 bad29bc6 Ermal Luçi
        global $config;
123
124
        $i = 0;
125 3134528d Ermal Luçi
        if (is_array($config['bridges']['bridged']) && count($config['bridges']['bridged'])) {
126
                foreach ($config['bridges']['bridged'] as $bridge) {
127 f620d00d Ermal Luçi
                        if(empty($bridge['bridgeif']))
128 bad29bc6 Ermal Luçi
                                $bridge['bridgeif'] = "bridge{$i}";
129
                        /* XXX: Maybe we should report any errors?! */
130
                        interface_bridge_configure($bridge);
131
                        $i++;
132
                }
133
        }
134
}
135
136 eba938e3 Scott Ullrich
function interface_bridge_configure(&$bridge) {
137 d7147b1c Scott Ullrich
	global $config, $g;
138 bad29bc6 Ermal Luçi
139 d7147b1c Scott Ullrich
	if (!is_array($bridge))
140
	        return -1;
141 bad29bc6 Ermal Luçi
142 dc97efaf Ermal Luçi
	if (empty($bridge['members'])) {
143
		log_error("No members found on {$bridge['bridgeif']}");
144
		return -1;
145
	}
146
147 bad29bc6 Ermal Luçi
	$members = explode(',', $bridge['members']);
148 70720671 Ermal Luçi
	if (!count($members))
149 bad29bc6 Ermal Luçi
		return -1;
150
	
151
	$checklist = get_configured_interface_list();
152
153 fded24de Ermal Luçi
	if ($g['booting'] || !empty($bridge['bridgeif'])) {
154 d7147b1c Scott Ullrich
		mwexec("/sbin/ifconfig {$bridge['bridgeif']} destroy");
155
		mwexec("/sbin/ifconfig {$bridge['bridgeif']} create");
156
		$bridgeif = $bridge['bridgeif'];
157
	} else {
158
		$bridgeif = exec("/sbin/ifconfig bridge create");
159
	}
160 bad29bc6 Ermal Luçi
161 b64523c1 Ermal Luçi
	/* Calculate smaller mtu and enforce it */
162 69e53ef0 Ermal Luçi
	$smallermtu = 0;
163 b64523c1 Ermal Luçi
	foreach ($members as $member) {
164
		$realif = get_real_interface($member);
165
		$mtu = get_interface_mtu($realif);
166 69e53ef0 Ermal Luçi
		if ($smallermtu == 0 && !empty($mtu))
167
			$smallermtu = $mtu;
168
		else if (!empty($mtu) && $mtu < $smallermtu)
169 b64523c1 Ermal Luçi
			$smallermtu = $mtu;
170
	}
171
	 
172 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
173
	if ($smallermtu == 0)
174
		$smallermtu = 1500; 
175
176 bad29bc6 Ermal Luçi
	/* Add interfaces to bridge */
177 31241000 Ermal Luçi
	foreach ($members as $member) {
178 d7147b1c Scott Ullrich
		if (!array_key_exists($member, $checklist))
179
			continue;
180
		$realif = get_real_interface($member);
181
		$realif =  escapeshellarg($realif);
182
		/* make sure the parent interface is up */
183 b64523c1 Ermal Luçi
		mwexec("/sbin/ifconfig {$realif} mtu {$smallermtu}");
184 d7147b1c Scott Ullrich
		if(!$realif) 
185
			log_error("realif not defined in interfaces bridge - up");
186 b5b957fe Scott Ullrich
		interfaces_bring_up($realif);
187 31241000 Ermal Luçi
		mwexec("/sbin/ifconfig {$bridgeif} addm {$realif}");	
188 d7147b1c Scott Ullrich
	}
189 31241000 Ermal Luçi
190 bad29bc6 Ermal Luçi
	if (isset($bridge['enablestp'])) {
191
		/* Choose spanning tree proto */
192
		mwexec("/sbin/ifconfig {$bridgeif} proto {$bridge['proto']}");	
193
		
194 dc97efaf Ermal Luçi
		if (!empty($bridge['stp'])) {
195
			$stpifs = explode(',', $bridge['stp']);
196
			foreach ($stpifs as $stpif) {
197
				$realif = get_real_interface($stpif);
198
				mwexec("/sbin/ifconfig {$bridgeif} stp {$realif}");
199
			}
200 bad29bc6 Ermal Luçi
		}
201 dc97efaf Ermal Luçi
		if (!empty($bridge['maxage']))
202 bad29bc6 Ermal Luçi
			mwexec("/sbin/ifconfig {$bridgeif} maxage {$bridge['maxage']}");
203 dc97efaf Ermal Luçi
		if (!empty($brige['fwdelay']))
204 bad29bc6 Ermal Luçi
			mwexec("/sbin/ifconfig {$bridgeif} fwddelay {$bridge['fwdelay']}");
205 dc97efaf Ermal Luçi
		if (!empty($brige['hellotime']))
206 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} hellotime {$bridge['hellotime']}");
207 dc97efaf Ermal Luçi
		if (!empty($brige['priority']))
208 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} priority {$bridge['priority']}");
209 dc97efaf Ermal Luçi
		if (!empty($brige['holdcount']))
210 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} holdcnt {$bridge['holdcnt']}");
211 dc97efaf Ermal Luçi
		if (!empty($bridge['ifpriority'])) {
212
			$pconfig = explode(",", $bridge['ifpriority']);
213
			$ifpriority = array();
214
			foreach ($pconfig as $cfg) {
215
				$embcfg = explode(":", $cfg);
216
				foreach ($embcfg as $key => $value)
217
					$ifpriority[$key] = $value;
218
			}
219
			foreach ($ifpriority as $key => $value) {
220
				$realif = get_real_interface($key);
221
				mwexec("/sbin/ifconfig ${bridgeif} ifpriority {$realif} {$value}"); 
222
			}
223 bad29bc6 Ermal Luçi
		}
224 dc97efaf Ermal Luçi
		if (!empty($bridge['ifpathcost'])) {
225
			$pconfig = explode(",", $bridges['ifpathcost']);
226
			$ifpathcost = array();
227
			foreach ($pconfig as $cfg) {
228
				$embcfg = explode(":", $cfg);
229
				foreach ($embcfg as $key => $value)
230
					$ifpathcost[$key] = $value;
231
			}
232
			foreach ($ifpathcost as $key => $value) {
233
                        	$realif = get_real_interface($key);
234
                        	mwexec("/sbin/ifconfig ${bridgeif} ifpathcost {$realif} {$value}");
235
                	}
236 bad29bc6 Ermal Luçi
		}
237
	}
238
239
	if ($bridge['maxaddr'] <> "")
240
		mwexec("/sbin/ifconfig {$bridgeif} maxaddr {$bridge['maxaddr']}");
241
        if ($bridge['timeout'] <> "")
242
                mwexec("/sbin/ifconfig {$bridgeif} timeout {$bridge['timeout']}");
243
        if ($bridge['span'] <> "") {
244 85a5da13 Ermal Luçi
		$realif = get_real_interface($bridge['span']);
245 bad29bc6 Ermal Luçi
                mwexec("/sbin/ifconfig {$bridgeif} span {$realif}");
246
	}
247 a47a5798 Ermal Luçi
	if (!empty($bridge['edge'])) {
248
        	$edgeifs = explode(',', $bridge['edge']);
249
        	foreach ($edgeifs as $edgeif) {
250
			$realif = get_real_interface($edgeif);
251
                	mwexec("/sbin/ifconfig {$bridgeif} edge {$realif}");
252
        	}
253
	}
254
	if (!empty($bridge['autoedge'])) {
255
        	$edgeifs = explode(',', $bridge['autoedge']);
256
        	foreach ($edgeifs as $edgeif) {
257
                	$realif = get_real_interface($edgeif);
258
                	mwexec("/sbin/ifconfig {$bridgeif} -autoedge {$realif}");
259
        	}
260
	}
261
	if (!empty($bridge['ptp'])) {
262
        	$ptpifs = explode(',', $bridge['ptp']);
263
        	foreach ($ptpifs as $ptpif) {
264
                	$realif = get_real_interface($ptpif);
265
                	mwexec("/sbin/ifconfig {$bridgeif} ptp {$realif}");
266
        	}
267
	}
268
	if (!empty($bridge['autoptp'])) {
269
        	$ptpifs = explode(',', $bridge['autoptp']);
270
        	foreach ($ptpifs as $ptpif) {
271
                	$realif = get_real_interface($ptpif);
272
                	mwexec("/sbin/ifconfig {$bridgeif} -autoptp {$realif}");
273
        	}
274
	}
275
	if (!empty($bridge['static'])) {
276
        	$stickyifs = explode(',', $bridge['static']);
277
        	foreach ($stickyifs as $stickyif) {
278
                	$realif = get_real_interface($stickyif);
279
                	mwexec("/sbin/ifconfig {$bridgeif} sticky {$realif}");
280
        	}
281
	}
282
	if (!empty($bridge['private'])) {
283
        	$privateifs = explode(',', $bridge['private']);
284
        	foreach ($privateifs as $privateif) {
285
                	$realif = get_real_interface($privateif);
286
               	 	mwexec("/sbin/ifconfig {$bridgeif} private {$realif}");
287
        	}
288
	}
289 bad29bc6 Ermal Luçi
290 d7147b1c Scott Ullrich
	if($bridgeif)
291 b5b957fe Scott Ullrich
		interfaces_bring_up($bridgeif);	
292 d7147b1c Scott Ullrich
	else 
293
		log_error("bridgeif not defined -- could not bring interface up");
294 bad29bc6 Ermal Luçi
295 d7147b1c Scott Ullrich
	return $bridgeif;
296 bad29bc6 Ermal Luçi
}
297
298 f620d00d Ermal Luçi
function interfaces_lagg_configure() 
299
{
300 cccf624b Ermal Luçi
        global $config;
301
302
        $i = 0;
303
        if (is_array($config['laggs']['lagg']) && count($config['laggs']['lagg'])) {
304
                foreach ($config['laggs']['lagg'] as $lagg) {
305 f620d00d Ermal Luçi
                        if(empty($lagg['laggif']))
306 cccf624b Ermal Luçi
                                $lagg['laggif'] = "lagg{$i}";
307
                        /* XXX: Maybe we should report any errors?! */
308
                        interface_lagg_configure($lagg);
309
                        $i++;
310
                }
311
        }
312
}
313
314 eba938e3 Scott Ullrich
function interface_lagg_configure(&$lagg) {
315 cccf624b Ermal Luçi
        global $config, $g;
316
317
        if (!is_array($lagg))
318
		return -1;
319
320
	$members = explode(',', $lagg['members']);
321
	if (!count($members))
322
		return -1;
323
	
324 fe281019 Ermal Luçi
	$checklist = get_interface_list();
325 cccf624b Ermal Luçi
326 b64523c1 Ermal Luçi
	if ($g['booting'] || !(empty($lagg['laggif']))) {
327
                mwexec("/sbin/ifconfig {$lagg['laggif']} destroy");
328
                mwexec("/sbin/ifconfig {$lagg['laggif']} create");
329
                $laggif = $lagg['laggif'];
330
        } else
331
                $laggif = exec("/sbin/ifconfig lagg create");
332
333
	mwexec("/sbin/ifconfig {$laggif} {$lagg['proto']}");
334
335
	/* Calculate smaller mtu and enforce it */
336 69e53ef0 Ermal Luçi
        $smallermtu = 0;
337 b64523c1 Ermal Luçi
        foreach ($members as $member) {
338
                $realif = get_real_interface($member);
339
                $mtu = get_interface_mtu($realif);
340 69e53ef0 Ermal Luçi
		if ($smallermtu == 0 && !empty($mtu))
341
			$smallermtu = $mtu;
342
                else if (!empty($mtu) && $mtu < $smallermtu)
343 b64523c1 Ermal Luçi
                        $smallermtu = $mtu;
344
        }
345
346 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
347
        if ($smallermtu == 0)
348
                $smallermtu = 1500;
349
350 cccf624b Ermal Luçi
	foreach ($members as $member) {
351
		if (!array_key_exists($member, $checklist))
352
			continue;
353 fe281019 Ermal Luçi
		$realif =  escapeshellarg($member);
354 d7147b1c Scott Ullrich
		/* make sure the parent interface is up */
355
		mwexec("/sbin/ifconfig {$realif} mtu {$smallermtu}");
356
		if($realif)
357 b5b957fe Scott Ullrich
			interfaces_bring_up($realif);
358 d7147b1c Scott Ullrich
		else 
359
			log_error("could not bring realif up -- foreach(memebers)");
360 b64523c1 Ermal Luçi
		mwexec("/sbin/ifconfig {laggif} laggport {$realif}");
361 cccf624b Ermal Luçi
	}
362 b5b957fe Scott Ullrich
	
363
	interfaces_bring_up($laggif);
364 cccf624b Ermal Luçi
365 d7147b1c Scott Ullrich
	return $laggif;
366 cccf624b Ermal Luçi
}
367
368 eba938e3 Scott Ullrich
function interfaces_gre_configure() {
369 582d2452 Ermal Luçi
        global $config;
370
371
        $i = 0;
372
        if (is_array($config['gres']['gre']) && count($config['gres']['gre'])) {
373
                foreach ($config['gres']['gre'] as $gre) {
374 f620d00d Ermal Luçi
                        if(empty($gre['greif']))
375 582d2452 Ermal Luçi
                                $gre['greif'] = "gre{$i}";
376
                        /* XXX: Maybe we should report any errors?! */
377
                        interface_gre_configure($gre);
378
                        $i++;
379
                }
380
        }
381
}
382
383 eba938e3 Scott Ullrich
function interface_gre_configure(&$gre) {
384 582d2452 Ermal Luçi
        global $config, $g;
385
386
	if (!is_array($gre))
387
		return -1;
388
389 85a5da13 Ermal Luçi
	$realif = get_real_interface($gre['if']);
390
	$realifip = get_interface_ip($gre['if']);
391 582d2452 Ermal Luçi
392 d7147b1c Scott Ullrich
	/* make sure the parent interface is up */
393 b5b957fe Scott Ullrich
	interfaces_bring_up($realif);
394 582d2452 Ermal Luçi
395 d7147b1c Scott Ullrich
	if ($g['booting'] || !(empty($gre['greif']))) {
396
		mwexec("/sbin/ifconfig {$gre['greif']} destroy");
397
		mwexec("/sbin/ifconfig {$gre['greif']} create");
398 582d2452 Ermal Luçi
		$greif = $gre['greif'];
399 d7147b1c Scott Ullrich
	} else {
400
		$greif = exec("/sbin/ifconfig gre create");
401
	}
402 582d2452 Ermal Luçi
403
	/* Do not change the order here for more see gre(4) NOTES section. */
404
	mwexec("/sbin/ifconfig {$greif} tunnel {$realifip} {$gre['remote-addr']}");
405 bd33ee57 Ermal Luçi
	mwexec("/sbin/ifconfig {$greif} {$gre['tunnel-local-addr']} {$gre['tunnel-remote-addr']} netmask " . gen_subnet_mask($gre['tunnel-remote-net']));
406 582d2452 Ermal Luçi
	if (isset($gre['link0']) && $gre['link0'])
407
		mwexec("/sbin/ifconfig {$greif} link0");
408 d7147b1c Scott Ullrich
	if (isset($gre['link1']) && $gre['link1'])
409
		mwexec("/sbin/ifconfig {$greif} link1");
410
	if (isset($gre['link2']) && $gre['link2'])
411
		mwexec("/sbin/ifconfig {$greif} link2");
412
413
	if($greif)
414 b5b957fe Scott Ullrich
		interfaces_bring_up($greif);
415 d7147b1c Scott Ullrich
	else 
416
		log_error("Could not bring greif up -- variable not defined.");
417 582d2452 Ermal Luçi
418
	mwexec("/sbin/route add {$gre['remote-addr']}/{$gre['tunnel-remote-net']} {$realifip}");
419 52d6ad51 Ermal Luçi
	file_put_contents("/tmp/{$greif}_router", $gre['tunnel-remote-addr']);
420 582d2452 Ermal Luçi
421
	return $greif;
422
}
423
424 eba938e3 Scott Ullrich
function interfaces_gif_configure() {
425 582d2452 Ermal Luçi
        global $config;
426
427
        $i = 0;
428
        if (is_array($config['gifs']['gif']) && count($config['gifs']['gif'])) {
429
                foreach ($config['gifs']['gif'] as $gif) {
430 f620d00d Ermal Luçi
                        if(empty($gif['gifif']))
431 582d2452 Ermal Luçi
                                $gre['gifif'] = "gif{$i}";
432
                        /* XXX: Maybe we should report any errors?! */
433
                        interface_gif_configure($gif);
434
                        $i++;
435
                }
436
        }
437
}
438
439 eba938e3 Scott Ullrich
function interface_gif_configure(&$gif) {
440 582d2452 Ermal Luçi
        global $config, $g;
441
442
        if (!is_array($gif))
443
                return -1;
444
445 85a5da13 Ermal Luçi
        $realif = get_real_interface($gif['if']);
446
        $realifip = get_interface_ip($gif['if']);
447 582d2452 Ermal Luçi
448
        /* make sure the parent interface is up */
449 d7147b1c Scott Ullrich
		if($realif)
450 b5b957fe Scott Ullrich
			interfaces_bring_up($realif);
451 d7147b1c Scott Ullrich
		else 
452
			log_error("could not bring realif up -- variable not defined -- interface_gif_configure()");
453 582d2452 Ermal Luçi
454
        if ($g['booting'] || !(empty($gif['gifif']))) {
455
                mwexec("/sbin/ifconfig {$gif['gifif']} destroy");
456
                mwexec("/sbin/ifconfig {$gif['gifif']} create");
457
                $gifif = $gif['gifif'];
458
        } else
459
                $gifif = exec("/sbin/ifconfig gif create");
460
461
        /* Do not change the order here for more see gif(4) NOTES section. */
462
        mwexec("/sbin/ifconfig {$gifif} tunnel {$realifip} {$gif['remote-addr']}");
463 bd33ee57 Ermal Luçi
        mwexec("/sbin/ifconfig {$gifif} {$gif['tunnel-local-addr']} {$gif['tunnel-remote-addr']} netmask " . gen_subnet_mask($gif['tunnel-remote-net']));
464 582d2452 Ermal Luçi
        if (isset($gif['link0']) && $gif['link0'])
465
                mwexec("/sbin/ifconfig {$gifif} link0");
466
        if (isset($gif['link1']) && $gif['link1'])
467
                mwexec("/sbin/ifconfig {$gifif} link1");
468 d7147b1c Scott Ullrich
		if($gifif)
469 b5b957fe Scott Ullrich
			interfaces_bring_up($gifif);
470 d7147b1c Scott Ullrich
		else
471
		 	log_error("could not bring gifif up -- variable not defined");
472 582d2452 Ermal Luçi
        mwexec("/sbin/route add {$gif['remote-addr']}/{$gif['tunnel-remote-net']} {$realifip}");
473 d7147b1c Scott Ullrich
		file_put_contents("/tmp/{$gifif}_router", $gif['tunnel-remote-addr']);
474 582d2452 Ermal Luçi
475
        return $gifif;
476
}
477
478 eba938e3 Scott Ullrich
function interfaces_configure() {
479 9b1c39e3 Ermal Luçi
	global $config, $g;
480
481 d7147b1c Scott Ullrich
	/* set up VLAN virtual interfaces */
482
	interfaces_vlan_configure();
483 9b1c39e3 Ermal Luçi
484 d7147b1c Scott Ullrich
	/* set up LAGG virtual interfaces */
485
	interfaces_lagg_configure();
486 9b1c39e3 Ermal Luçi
487
	/* Set up PPP interfaces */
488
	interfaces_ppp_configure();
489 cfc707f7 Scott Ullrich
490 67ee1ec5 Ermal Luçi
	$iflist = get_configured_interface_with_descr();
491 9b1c39e3 Ermal Luçi
	$delayed_list = array();
492
	$bridge_list = array();
493 b6db9217 Ermal Luçi
	
494 67ee1ec5 Ermal Luçi
	foreach($iflist as $if => $ifname) {
495 9b1c39e3 Ermal Luçi
		$realif = $config['interfaces'][$if]['if'];
496 53c82ef9 Scott Ullrich
		if(isset($realif['pppoe_reset'])) 
497
			setup_pppoe_reset_file($if, true);
498
		else 
499
			setup_pppoe_reset_file($if, false);
500 9b1c39e3 Ermal Luçi
		if (strstr($realif, "bridge")) 
501
			$bridge_list[$if] = $ifname;
502
		else if (strstr($realif, "gre"))
503
			$delayed_list[$if] = $ifname;
504
		else if (strstr($realif, "gif"))
505
			$delayed_list[$if] = $ifname;
506
		else {
507
			if ($g['booting'])
508 d7147b1c Scott Ullrich
				echo "Configuring {$ifname} interface...";
509
        	if($g['debug'])
510
				log_error("Configuring {$ifname}");
511 9b1c39e3 Ermal Luçi
			interface_configure($if);
512
			if ($g['booting']) 
513 53c82ef9 Scott Ullrich
				echo "done.\n";
514 9b1c39e3 Ermal Luçi
		}
515
	}
516
517 d7147b1c Scott Ullrich
	/* set up GRE virtual interfaces */
518
	interfaces_gre_configure();
519 9b1c39e3 Ermal Luçi
520 d7147b1c Scott Ullrich
	/* set up GIF virtual interfaces */
521
	interfaces_gif_configure();
522 9b1c39e3 Ermal Luçi
	
523
	foreach ($delayed_list as $if => $ifname) {
524
		if ($g['booting'])
525 d7147b1c Scott Ullrich
			echo "Configuring {$ifname} interface...";
526
        if($g['debug'])
527
        	log_error("Configuring {$ifname}");
528 67ee1ec5 Ermal Luçi
529 69e5a8be Ermal Luçi
		interface_configure($if);
530 4476d447 Ermal Luçi
531 9b1c39e3 Ermal Luçi
		if ($g['booting'])
532
			echo "done.\n";
533 67ee1ec5 Ermal Luçi
	}
534 cfc707f7 Scott Ullrich
535 d7147b1c Scott Ullrich
	/* set up BRIDGe virtual interfaces */
536
	interfaces_bridge_configure();
537 9b1c39e3 Ermal Luçi
538 d7147b1c Scott Ullrich
	foreach ($bridge_list as $if => $ifname) {
539
		if ($g['booting'])
540
			echo "Configuring {$ifname} interface...";
541
		if($g['debug'])
542
			log_error("Configuring {$ifname}");
543 9b1c39e3 Ermal Luçi
544 d7147b1c Scott Ullrich
		interface_configure($if);
545 9b1c39e3 Ermal Luçi
546 d7147b1c Scott Ullrich
		if ($g['booting'])
547
			echo "done.\n";
548
	}
549 9b1c39e3 Ermal Luçi
550 d7147b1c Scott Ullrich
	/* bring up carp interfaces */
551
	interfaces_carp_configure();
552 9b1c39e3 Ermal Luçi
553 d7147b1c Scott Ullrich
	/* bring ip IP aliases */
554
	interfaces_ipalias_configure();
555 9b1c39e3 Ermal Luçi
556 5b237745 Scott Ullrich
	if (!$g['booting']) {
557
		/* reconfigure static routes (kernel may have deleted them) */
558
		system_routing_configure();
559 cfc707f7 Scott Ullrich
560 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
561
		vpn_ipsec_configure();
562 cfc707f7 Scott Ullrich
563 f620d00d Ermal Luçi
		/* reload dhcpd (interface enabled/disabled status may have changed) */
564 5b237745 Scott Ullrich
		services_dhcpd_configure();
565 cfc707f7 Scott Ullrich
566 5b237745 Scott Ullrich
		/* restart dnsmasq */
567
		services_dnsmasq_configure();
568 4d18de6a Scott Ullrich
569 c597d50f Scott Ullrich
		/* reload captive portal */
570
		captiveportal_configure();
571
572 4d18de6a Scott Ullrich
		/* set the reload filter dity flag */
573 be38535c Ermal Luçi
		filter_configure();
574 5b237745 Scott Ullrich
	}
575 cfc707f7 Scott Ullrich
576 5b237745 Scott Ullrich
	return 0;
577
}
578
579 eba938e3 Scott Ullrich
function interface_reconfigure($interface = "wan") {
580 80bf3f4a Ermal Luçi
	interface_bring_down($interface);
581
	sleep(1);
582
	interface_configure($interface);
583
}
584
585 eba938e3 Scott Ullrich
function interface_bring_down($interface = "wan") {
586 80bf3f4a Ermal Luçi
	global $config, $g;
587
588
	$ifcfg = $config['interfaces'][$interface];
589
590 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
591 80bf3f4a Ermal Luçi
592 adec6851 Ermal Luçi
	mwexec("/usr/sbin/arp -d -i {$realif} -a");
593
594 0c56453c Ermal Luçi
        /* remove interface up file if it exists */
595
        unlink_if_exists("{$g['tmp_path']}/{$interface}up");
596
        unlink_if_exists("{$g['vardb_path']}/{$interface}ip");
597
        unlink_if_exists("{$g['varetc_path']}/nameservers.conf");
598
599 80bf3f4a Ermal Luçi
	switch ($ifcfg['ipaddr']) {
600
	case "pppoe":
601
		killbypid("{$g['varrun_path']}/pppoe_{$interface}.pid");
602
        	sleep(2);
603 0c56453c Ermal Luçi
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
604
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.links");
605 80bf3f4a Ermal Luçi
		break;
606
	case "pptp":
607
		killbypid("{$g['varrun_path']}/pptp_{$interface}.pid");
608
        	sleep(2);
609 0c56453c Ermal Luçi
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
610
                unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.links");
611 80bf3f4a Ermal Luçi
		break;
612
	case "carpdev-dhcp":
613
		/* 
614
		 * NB: When carpdev gets enabled it would be better to be handled as all
615
		 *	other interfaces! 
616
		 */
617
	case "dhcp":
618
        	$pid = find_dhclient_process($interface);
619
        	if($pid)
620
                	mwexec("kill {$pid}");
621 d8acd706 Ermal Luçi
               	sleep(1);
622 0c56453c Ermal Luçi
                unlink_if_exists("{$g['varetc_path']}/dhclient_{$interface}.conf");
623 d8acd706 Ermal Luçi
               	mwexec("/sbin/ifconfig {$realinterface} delete down");
624 80bf3f4a Ermal Luçi
		break;
625
	default:
626
		mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete down");
627
		break;
628
	}
629 eb772abd Scott Ullrich
630 80bf3f4a Ermal Luçi
	if (preg_match("/^tun|^ppp|^ovpn|^gif|^gre|^lagg|^bridge|^vlan/i", $realif))
631
                mwexec("/sbin/ifconfig {$realif} destroy");
632
	
633 0c56453c Ermal Luçi
	unlink_if_exists("/tmp/{$realif}_router");
634 80bf3f4a Ermal Luçi
	return;
635 5b237745 Scott Ullrich
}
636
637 eba938e3 Scott Ullrich
function interfaces_ppp_configure() {
638 9b1c39e3 Ermal Luçi
        global $config;
639
640
        $i = 0;
641
        if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
642
                foreach ($config['ppps']['ppp'] as $ppp) {
643 f620d00d Ermal Luçi
                        if(empty($ppp['pppif']))
644 9b1c39e3 Ermal Luçi
                                $ppp['pppif'] = "ppp{$i}";
645
                        /* XXX: Maybe we should report any errors?! */
646 2d8d4f56 Ermal Luçi
                        interface_ppp_configure($ppp);
647 9b1c39e3 Ermal Luçi
                        $i++;
648
                }
649
        }
650
}
651
652 eba938e3 Scott Ullrich
function interface_ppp_configure($ifcfg) {
653 abc1cbc5 Ermal Luçi
	global $config, $g;
654 860c4e80 Chris Buechler
	
655 9b1c39e3 Ermal Luçi
	/* Remove  the /dev/ from the device name. */
656 2d8d4f56 Ermal Luçi
	$dev = substr($ifcfg['port'], 5);
657 9b1c39e3 Ermal Luçi
658 abc1cbc5 Ermal Luçi
	$realif  = $ifcfg['pppif'];
659
	if ($realif <> "") {
660
		$i = 0;
661
		while ($realif != "ppp{$i}")
662
			$i++;
663
		if(file_exists("/var/run/ppp{$i}.pid")) {
664
			$pid = trim(file_get_contents("/var/run/ppp{$i}.pid"));
665
			mwexec("kill {$pid}");
666
		}
667 860c4e80 Chris Buechler
	}
668
	
669 2d8d4f56 Ermal Luçi
	if ($g['booting'] || $realif <> "") {
670 228d6f9a Ermal Luçi
                mwexec("/sbin/ifconfig {$realif} destroy");
671
                mwexec("/sbin/ifconfig {$realif} create");
672
        } else
673
                $realif = exec("/sbin/ifconfig ppp create");
674
675 860c4e80 Chris Buechler
676
	$peerfile = "lcp-echo-failure 0\n";
677
	$peerfile .= "lcp-echo-interval 0\n";
678 9b1c39e3 Ermal Luçi
	$peerfile .= "connect /etc/ppp/peers/ppp{$dev}-connect-chat\n";
679
	//$peerfile .= "disconnect /etc/ppp/peers/ppp{$dev}-disconnect-chat\n";
680 abc1cbc5 Ermal Luçi
	$peerfile .= "{$ifcfg['port']} {$ifcfg['linespeed']}\n";
681 860c4e80 Chris Buechler
	$peerfile .= "crtscts\n";
682 abc1cbc5 Ermal Luçi
	if ($ifcfg['connect-max-attempts'] <> "")
683
		$peerfile .= "connect-max-attempts {$ifcfg['connect-max-attempts']}";
684 860c4e80 Chris Buechler
	$peerfile .= "local\n";
685 abc1cbc5 Ermal Luçi
	if ($ifcfg['localip'] <> "") {
686
		$peerfile .= ":{$ifcfg['gateway']}\n";
687
		$peerfile .= "{$ifcfg['localip']}:{$ifcfg['gateway']}";
688
	} else if ($ifcfg['gateway'] <> "") {
689
		$peerfile .= ":{$ifcfg['gateway']}\n";
690
		$peerfile .= "noipdefault\n";
691
	} else 
692
		$peerfile .= "noipdefault\n";
693 860c4e80 Chris Buechler
	$peerfile .= "ipcp-accept-local\n";
694
	$peerfile .= "novj\n";
695
	$peerfile .= "nobsdcomp\n";
696
	$peerfile .= "novjccomp\n";
697
	$peerfile .= "nopcomp\n";
698
	$peerfile .= "noaccomp\n";
699
	$peerfile .= "noauth\n";
700 abc1cbc5 Ermal Luçi
	//$peerfile .= "nodetach\n";
701 860c4e80 Chris Buechler
	$peerfile .= "persist\n";
702
	$peerfile .= "debug\n";
703
	// KD - test
704
	//$peerfile .= "defaultroute\n";
705
	//$peerfile .= "nodetach\n";
706
	// KD - so I know where to look!
707
	$peerfile .= "# created by /etc/inc/interfaces.inc\n";
708
709
	// Added single quotes to some strings below:
710
	// the \rAT is *always* going to need it
711
	// and the phone number on a GSM connection ends in a # char
712
	// Kevin Dawson, 22 Jan 2008
713
	// Refer Andrew Curtis
714
			
715
	$chatfile = "#!/bin/sh\n";
716
	$chatfile .= "exec chat \\\n";
717
	$chatfile .= "TIMEOUT 5 \\\n";
718
	$chatfile .= "ECHO ON \\\n";
719
	$chatfile .= "ABORT '\\nBUSY\\r' \\\n";
720
	$chatfile .= "ABORT '\\nERROR\\r' \\\n";
721
	$chatfile .= "ABORT '\\nNO ANSWER\\r' \\\n";
722
	$chatfile .= "ABORT '\\nNO CARRIER\\r' \\\n";
723
	$chatfile .= "ABORT '\\nNO DIALTONE\\r' \\\n";
724
	$chatfile .= "ABORT '\\nRINGING\\r\\n\\r\\nRINGING\\r' \\\n";
725
	// KD
726
	$chatfile .= "'' '\\rAT' \\\n";
727
	$chatfile .= "TIMEOUT 12 \\\n";
728
	$chatfile .= "OK ATH \\\n";
729
	$chatfile .= "OK ATE1 \\\n";
730
	$chatfile .= "OK 'AT+CGDCONT=1,\"IP\",\"{$ifcfg['ap']}\"' \\\n";
731
	// KD
732
	$chatfile .= "OK 'ATD{$ifcfg['phone']}' \\\n";
733
	$chatfile .= "TIMEOUT 22 \\\n";
734 abc1cbc5 Ermal Luçi
	if ($ifcfg['username'] <> "") {
735
		$chatfile .= "CONNECT \"\" TIMEOUT 10 \\\n";
736
		$chatfile .= "ogin:-\\r-ogin: {$ifcfg['username']}\\\n";
737
		$chatfile .= " TIMEOUT 5 sword: {$ifcfg['password']} \\\n";
738
	} else
739
		$chatfile .= "CONNECT \"\" \\\n";
740 860c4e80 Chris Buechler
	$chatfile .= "SAY \"\\nConnected.\"\n";
741 228d6f9a Ermal Luçi
742
	config_lock();
743
	conf_mount_rw();
744 2d8d4f56 Ermal Luçi
	safe_mkdir("/etc/ppp/peers", "0755");
745 228d6f9a Ermal Luçi
	file_put_contents("/etc/ppp/peers/ppp_{$dev}", $peerfile);
746 9b1c39e3 Ermal Luçi
	file_put_contents("/etc/ppp/peers/ppp{$dev}-connect-chat", $chatfile);
747
	chmod("/etc/ppp/peers/ppp{$dev}-connect-chat", 0755);
748 228d6f9a Ermal Luçi
	conf_mount_ro();
749
	config_unlock();
750 9b1c39e3 Ermal Luçi
	
751 abc1cbc5 Ermal Luçi
	sleep(1);
752
	mwexec("/usr/sbin/pppd call ppp_{$dev}");
753
754 9b1c39e3 Ermal Luçi
	return $realif;
755 860c4e80 Chris Buechler
}
756
757 eba938e3 Scott Ullrich
function interfaces_carp_configure() {
758 87a2efd1 Ermal Luçi
	global $g, $config;
759 2b9747b9 Scott Ullrich
	$balanacing = "";
760
	$pfsyncinterface = "";
761
	$pfsyncenabled = "";
762 b932ef16 Scott Ullrich
	if(isset($config['system']['developerspew'])) {
763
		$mt = microtime();
764
		echo "interfaces_carp_configure() being called $mt\n";
765
	}
766 e5d43d93 Scott Ullrich
	// Prepare CmdCHAIN that will be used to execute commands.
767
	$cmdchain = new CmdCHAIN();	
768 6008210b Scott Ullrich
	$carp_instances_counter = 0;
769
	$total_carp_interfaces_defined = find_number_of_created_carp_interfaces();
770 467c2c89 Scott Ullrich
	/* destroy previous interfaces */
771 e5d43d93 Scott Ullrich
	for($x=0; $x<$total_carp_interfaces_defined; $x++) 
772
		$cmdchain->add("Delete CARP interface", "/sbin/ifconfig carp{$x} delete", false);
773 b932ef16 Scott Ullrich
	if ($g['booting']) {
774 7d0f4544 Scott Ullrich
		echo "Configuring CARP interfaces...";
775
		mute_kernel_msgs();
776 a5250ebc Scott Ullrich
	}
777 b932ef16 Scott Ullrich
	/* suck in configuration items */
778 16ccd95c Scott Ullrich
	if($config['installedpackages']['carpsettings']) 
779
		if($config['installedpackages']['carpsettings']['config']) {
780 b932ef16 Scott Ullrich
		foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
781
			$pfsyncenabled = $carp['pfsyncenabled'];
782
			$balanacing = $carp['balancing'];
783
			$pfsyncinterface = $carp['pfsyncinterface'];
784 b42ad736 Scott Ullrich
			$pfsyncpeerip = $carp['pfsyncpeerip'];
785 9f6b1429 Scott Ullrich
		}
786 b932ef16 Scott Ullrich
	} else {
787
		unset($pfsyncinterface);
788
		unset($balanacing);
789
		unset($pfsyncenabled);
790 6008210b Scott Ullrich
	}
791 79d28f42 Scott Ullrich
	$cmdchain->add("Allow CARP", "/sbin/sysctl net.inet.carp.allow=1", true);			
792 b932ef16 Scott Ullrich
	if($balanacing) {
793 79d28f42 Scott Ullrich
		$cmdchain->add("Enable CARP ARP-balancing", "/sbin/sysctl net.inet.carp.arpbalance=1", true);
794
		$cmdchain->add("Disallow CARP preemption", "/sbin/sysctl net.inet.carp.preempt=0", true);
795 b932ef16 Scott Ullrich
	} else {
796 79d28f42 Scott Ullrich
		$cmdchain->add("Enable CARP preemption", "/sbin/sysctl net.inet.carp.preempt=1", true);		
797 b932ef16 Scott Ullrich
	}
798 79d28f42 Scott Ullrich
	$cmdchain->add("Enable CARP logging", "/sbin/sysctl net.inet.carp.log=2", true);
799 b932ef16 Scott Ullrich
	$carp_sync_int = convert_friendly_interface_to_real_interface_name($pfsyncinterface);
800
	if($g['booting']) {
801
		/*    install rules to alllow pfsync to sync up during boot
802
		 *    carp interfaces will remain down until the bootup sequence finishes
803
		 */
804
		exec("echo pass quick proto carp all keep state > /tmp/rules.boot");
805
		exec("echo pass quick proto pfsync all >> /tmp/rules.boot");
806 499994ff Scott Ullrich
		exec("echo pass out quick from any to any keep state >> /tmp/rules.boot");
807 e5d43d93 Scott Ullrich
		exec("/sbin/pfctl -f /tmp/rules.boot");
808 eb772abd Scott Ullrich
	}
809 b932ef16 Scott Ullrich
	/* setup pfsync interface */
810 b42ad736 Scott Ullrich
	if($carp_sync_int and $pfsyncenabled) {
811
		if($pfsyncpeerip) {
812 e5d43d93 Scott Ullrich
			$cmdchain->add("Bring up pfsync0 syncpeer", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} syncpeer {$pfsyncpeerip} up", false);						
813 b42ad736 Scott Ullrich
		} else {
814 e5d43d93 Scott Ullrich
			$cmdchain->add("Bring up pfsync0 syncdev", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up", false);			
815 b42ad736 Scott Ullrich
		}
816
	} else {
817 e5d43d93 Scott Ullrich
		$cmdchain->add("Bring up pfsync0", "/sbin/ifconfig pfsync0 syncdev lo0 up", false);						
818 b42ad736 Scott Ullrich
	}
819 67ee1ec5 Ermal Luçi
	//$fd = fopen("/tmp/carp.sh", "w");
820 3fceab6c Scott Ullrich
	$viparr = &$config['virtualip']['vip'];
821 eb772abd Scott Ullrich
	if($config['virtualip']['vip']) {
822 79d28f42 Scott Ullrich
		$cmdchain->add("Allow CARP.", "/sbin/sysctl net.inet.carp.allow=1", true);				
823 b932ef16 Scott Ullrich
	} else {
824
		$viparr = array();
825 79d28f42 Scott Ullrich
		$cmdchain->add("Disallow CARP.", "/sbin/sysctl net.inet.carp.allow=0", true);		
826 b932ef16 Scott Ullrich
	}
827 93ca4555 Scott Ullrich
	if(!$viparr and $config['interfaces']['wan']['ipaddr'] == "carpdev-dhcp") {
828
		/* no vips exist but we need to bring up carpdev... */
829
		$viparr_temp = array();
830
		$viparr_temp['advskew'] = "200";
831
		$viparr_temp['vhid'] = "1";
832
		$viparr_temp['mode'] = "carpdev-dhcp";
833
		$viparr_temp['password'] = $config['system']['hostname'] . "pfS";
834
		$viparr = $viparr_temp;
835
	}
836 e5d43d93 Scott Ullrich
	
837 87a2efd1 Ermal Luçi
	if($g['debug'])
838 e5d43d93 Scott Ullrich
		$cmdchain->setdebug(); // optional for verbose logging
839
	$cmdchain->execute();
840
	
841
	// Reset CmdCHAIN
842
	$cmdchain->clear();
843
844 67ee1ec5 Ermal Luçi
	if(is_array($viparr))
845 6008210b Scott Ullrich
	foreach ($viparr as $vip) {
846 67ee1ec5 Ermal Luçi
		$vip_password = $vip['password'];
847
		$vip_password = str_replace(" ", "", $vip_password);
848
		if($vip['password'] != "")
849
                	$password = " pass \"" . $vip_password . "\"";
850
		$interface = filter_translate_type_to_real_interface($vip['interface']);
851
		$carpint = "carp" . $carp_instances_counter;
852
853
		switch ($vip['mode']) {
854
		case "carp":
855 bb0e29e8 Scott Ullrich
			/* ensure CARP IP really exists prior to loading up */
856
			$found = false;
857 16d0ed50 Ermal Luçi
			$iflist = get_configured_interface_list();
858 bb0e29e8 Scott Ullrich
			foreach($iflist as $if) {
859
				$ww_subnet_ip = $config['interfaces'][$if]['ipaddr'];
860
				$ww_subnet_bits = $config['interfaces'][$if]['subnet'];
861
				if (ip_in_subnet($vip['subnet'], gen_subnet($ww_subnet_ip, $ww_subnet_bits) . "/" . $ww_subnet_bits))
862
					$found = true;
863
			}
864 67ee1ec5 Ermal Luçi
			if($found == false) {
865 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", "");
866
				continue;
867
			}			
868 b932ef16 Scott Ullrich
			/* create the carp interface and setup */
869 e5d43d93 Scott Ullrich
			$cmdchain->add("create CARP interface", "/sbin/ifconfig {$carpint} create", false);
870 3f7d2120 Bill Marquette
871
			/* invalidate interface cache */
872
			get_interface_arr(true);
873 67ee1ec5 Ermal Luçi
			$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
874 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);
875
			$cmdchain->add("bring CARP interface UP", "/sbin/ifconfig {$carpint} up", false);
876 67ee1ec5 Ermal Luçi
			$carp_instances_counter++;
877
			break;
878
		case "carpdev-dhcp":
879
			log_error("Found carpdev interface {$vip['interface']} on top of interface {$interface}");
880
			if(!empty($interface)) {
881 e5d43d93 Scott Ullrich
				
882
					$cmdchain->add("bring CARP parent interface UP", "/sbin/ifconfig {$interface} up", false);			
883
					$cmdchain->add("create CARP interface", "/sbin/ifconfig {$carpint} create", false);
884
					$cmdchain->add("bring CARP interface UP", "/sbin/ifconfig {$carpint} up", false);
885
					$cmdchain->add("assign CARP CarpDEV directive", "/sbin/ifconfig {$carpint} carpdev ". $interface . " vhid " . $vip['vhid'] . " advskew " . $vip['advskew'] . $password, false);
886
					$cmdchain->add("bring CARP interface UP", "/sbin/ifconfig {$carpint} up", false);
887
888
					/*
889
					 * XXX: BIG HACK but carpdev needs ip services active
890
					 * 	before even starting something as dhclient.
891
					 * 	I do not know if this is a feature or a bug
892
					 * 	but better than track it make it work ;) .
893
					 */
894
					//$fakeiptouse = "10.254.254." . ($carp_instances_counter+1);
895
					//$cmdchain->add("CarpDEV hack", "/sbin/ifconfig {$carpint} inet {$fakeiptouse}", false);
896
897 67ee1ec5 Ermal Luçi
        			/* generate dhclient_wan.conf */
898
        			$fd = fopen("{$g['varetc_path']}/dhclient_{$carpint}.conf", "w");
899
        			if ($fd) {
900 d5d00b83 Scott Ullrich
901 67ee1ec5 Ermal Luçi
        				$dhclientconf = "";
902
903
        				$dhclientconf .= <<<EOD
904
interface "{$carpint}" {
905
timeout 60;
906
retry 1;
907
select-timeout 0;
908
initial-interval 1;
909
script "/sbin/dhclient-script";
910
}
911
912
EOD;
913
914
 			        fwrite($fd, $dhclientconf);
915
        			fclose($fd);
916
917
        			/* fire up dhclient */
918 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);
919 d5d00b83 Scott Ullrich
				} else {
920 e5d43d93 Scott Ullrich
					log_error("Error: cannot open dhclient_{$carpint}.conf in interfaces_carp_configure() for writing.\n");
921
					$cmdchain->add("bring CARP dhclient UP in background", "/sbin/dhclient -b {$carpint}", false);					
922 d5d00b83 Scott Ullrich
				}
923 67ee1ec5 Ermal Luçi
924 e5d43d93 Scott Ullrich
        		$fout = fopen("/tmp/ifconfig_{$carpint}","w");
925
        		fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$carpint}.conf {$carpint}");
926
        		fclose($fout);
927 67ee1ec5 Ermal Luçi
928
			} else {
929
				log_error("Could not determine CarpDEV parent interface for {$vip['descr']}.");
930 d5d00b83 Scott Ullrich
			}
931 6008210b Scott Ullrich
			$carp_instances_counter++;
932 67ee1ec5 Ermal Luçi
			break;
933 6008210b Scott Ullrich
		}
934 67ee1ec5 Ermal Luçi
	}
935 1b58b513 Scott Ullrich
936 87a2efd1 Ermal Luçi
	if($g['debug'])
937 e5d43d93 Scott Ullrich
		$cmdchain->setdebug(); // optional for verbose logging
938
	// Execute built up command chain.
939
	$cmdchain->execute();	
940
941 3c18fbc9 Scott Ullrich
	if ($g['booting']) {
942 deebaae1 Scott Ullrich
		unmute_kernel_msgs();
943 74dbce1f Scott Ullrich
		echo "done.\n";
944 669e1adb Bill Marquette
	}
945 7b2d4769 Bill Marquette
946
	/* update cache */
947 9e097ada Bill Marquette
	if ($carp_instances_counter != find_number_of_created_carp_interfaces())
948 eb772abd Scott Ullrich
		find_number_of_created_carp_interfaces(true);
949 e5d43d93 Scott Ullrich
950 9f6b1429 Scott Ullrich
}
951
952 eba938e3 Scott Ullrich
function interfaces_ipalias_configure() {
953 87a2efd1 Ermal Luçi
	global $g, $config;
954 a04de17f Chris Buechler
	if(isset($config['system']['developerspew'])) {
955
		$mt = microtime();
956
		echo "interfaces_ipalias_configure() being called $mt\n";
957
	}
958
	$viparr = &$config['virtualip']['vip'];
959 6a74c90e Scott Ullrich
	if(is_array($viparr)) {
960
		foreach ($viparr as $vip) {
961
			if ($vip['mode'] == "ipalias") {
962 85a5da13 Ermal Luçi
				$if = get_real_interface($vip['interface']);
963 1fb7c265 Ermal Luçi
				mwexec("/sbin/ifconfig " . escapeshellarg($if) . " " . $vip['subnet'] . "/" . escapeshellarg($vip['subnet_bits']) . " alias"); 
964 6a74c90e Scott Ullrich
			}
965 a04de17f Chris Buechler
		}
966
	}
967
}
968
969 eba938e3 Scott Ullrich
function interface_wireless_configure($if, $wlcfg) {
970 ac3f8318 Espen Johansen
	global $config, $g;
971 eb772abd Scott Ullrich
972 4742e927 Scott Ullrich
	/*    open up a shell script that will be used to output the commands.
973
	 *    since wireless is changing a lot, these series of commands are fragile
974
     *    and will sometimes need to be verified by a operator by executing the command
975
     *    and returning the output of the command to the developers for inspection.  please
976
     *    do not change this routine from a shell script to individul exec commands.  -sullrich
977
	 */
978 eb772abd Scott Ullrich
979 8a958125 Scott Ullrich
	conf_mount_rw();
980 eb772abd Scott Ullrich
981 490b8b2a Scott Ullrich
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
982 eb772abd Scott Ullrich
983 4742e927 Scott Ullrich
	$fd_set = fopen("/tmp/{$if}_setup.sh","w");
984
	fwrite($fd_set, "#!/bin/sh\n");
985 36d0358b Scott Ullrich
	fwrite($fd_set, "# {$g['product_name']} wireless configuration script.\n\n");
986 eb772abd Scott Ullrich
987 4742e927 Scott Ullrich
	fwrite($fd_set, "# enable shell debugging\n");
988
	fwrite($fd_set, "set -x\n");
989 eb772abd Scott Ullrich
990 2ac908dd Espen Johansen
	/* set values for /path/program */
991
	$hostapd = "/usr/sbin/hostapd";
992
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
993 4742e927 Scott Ullrich
	$ifconfig = "/sbin/ifconfig";
994
	$killall = "/usr/bin/killall";
995 2ac908dd Espen Johansen
996 a59abc65 Scott Ullrich
	/* Set all wireless ifconfig variables (splitt up to get rid of needed checking) */
997 5508cf57 Scott Ullrich
998 ac3f8318 Espen Johansen
	/* Set a/b/g standard */
999 f4c9d138 Scott Ullrich
	$standard = "mode " . escapeshellarg($wlcfg['standard']);
1000 5508cf57 Scott Ullrich
1001 0856c4ac Scott Ullrich
	/* Set 802.11g protection mode */
1002
	$protmode = "protmode " . escapeshellarg($wlcfg['protmode']);
1003
1004 ac3f8318 Espen Johansen
	/* set wireless channel value */
1005 ea9d29fa Scott Ullrich
	if(isset($wlcfg['channel']))
1006 9f6f3e04 Chris Buechler
		if($wlcfg['channel'] == "0")
1007
			$channel = "channel any";
1008
		else
1009
			$channel = "channel " . escapeshellarg($wlcfg['channel']);
1010 2ac908dd Espen Johansen
1011 f134033e Scott Ullrich
	/* set Distance value */
1012 eb772abd Scott Ullrich
	if($wlcfg['distance'])
1013 f134033e Scott Ullrich
		$distance = escapeshellarg($wlcfg['distance']);
1014
1015 ac3f8318 Espen Johansen
	/* Set ssid */
1016 eb772abd Scott Ullrich
	if($wlcfg['ssid'])
1017 191a8175 Scott Ullrich
		$ssid = "ssid " . escapeshellarg($wlcfg['ssid']);
1018 eb772abd Scott Ullrich
1019 ac3f8318 Espen Johansen
	/* Set wireless hostap mode */
1020 a59abc65 Scott Ullrich
	if ($wlcfg['mode'] == "hostap")
1021 ac3f8318 Espen Johansen
		$hostapmode = "mediaopt hostap";
1022
	else
1023
		$hostapmode = "-mediaopt hostap";
1024
1025
	/* Set wireless adhoc mode */
1026 a59abc65 Scott Ullrich
	if ($wlcfg['mode'] == "adhoc")
1027 ac3f8318 Espen Johansen
		$adhocmode = "mediaopt adhoc";
1028
	else
1029
		$adhocmode = "-mediaopt adhoc";
1030
1031
	/* Not neccesary to set BSS mode as this is default if adhoc and/or hostap is NOT set */
1032
1033
	/* handle hide ssid option */
1034
	if(isset($wlcfg['hidessid']['enable']))
1035
		$hidessid = "hidessid";
1036
	else
1037
		$hidessid = "-hidessid";
1038
1039
	/* handle pureg (802.11g) only option */
1040
	if(isset($wlcfg['pureg']['enable']))
1041
		$pureg = "mode 11g pureg";
1042
	else
1043
		$pureg = "-pureg";
1044
1045
	/* enable apbridge option */
1046
	if(isset($wlcfg['apbridge']['enable']))
1047
		$apbridge = "apbridge";
1048
	else
1049
		$apbridge = "-apbridge";
1050
1051
	/* handle turbo option */
1052
	if(isset($wlcfg['turbo']['enable']))
1053
		$turbo = "mediaopt turbo";
1054
	else
1055
		$turbo = "-mediaopt turbo";
1056
1057
	/* handle txpower setting */
1058
	if($wlcfg['txpower'] <> "")
1059 f4c9d138 Scott Ullrich
		$txpower = "txpower " . escapeshellarg($wlcfg['txpower']);
1060 eb772abd Scott Ullrich
1061 ac3f8318 Espen Johansen
	/* handle wme option */
1062
	if(isset($wlcfg['wme']['enable']))
1063
		$wme = "wme";
1064
	else
1065
		$wme = "-wme";
1066 eb772abd Scott Ullrich
1067 ac3f8318 Espen Johansen
	/* set up wep if enabled */
1068 2f19fa14 Scott Ullrich
    if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
1069 eb772abd Scott Ullrich
		if($wlcfg['wpa']['auth_algs'] == "1")
1070 2f19fa14 Scott Ullrich
			$wepset .= "authmode open wepmode on ";
1071
		else if($wlcfg['wpa']['auth_algs'] == "2")
1072
			$wepset .= "authmode shared wepmode on ";
1073
		else if($wlcfg['wpa']['auth_algs'] == "3")
1074 eb772abd Scott Ullrich
			$wepset .= "authmode mixed wepmode on ";
1075 2f19fa14 Scott Ullrich
		$i = 1;
1076
		foreach ($wlcfg['wep']['key'] as $wepkey) {
1077
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
1078 eb772abd Scott Ullrich
			if (isset($wepkey['txkey']))
1079 2f19fa14 Scott Ullrich
				$wepset .= "weptxkey {$i} ";
1080
			$i++;
1081
		}
1082
    } else {
1083
    	$wepset .= "authmode open wepmode off ";
1084 ac3f8318 Espen Johansen
	}
1085
1086
	/* generate wpa_supplicant/hostap config if wpa is enabled */
1087
1088
	switch ($wlcfg['mode']) {
1089 b67d192d Scott Ullrich
		case 'bss':
1090 ac3f8318 Espen Johansen
			if (isset($wlcfg['wpa']['enable'])) {
1091
1092
				$wpa .= <<<EOD
1093 454756b9 Scott Ullrich
ctrl_interface={$g['varrun_path']}/wpa_supplicant
1094 50ad3b7c Scott Ullrich
ctrl_interface_group=0
1095
ap_scan=1
1096 2ac908dd Espen Johansen
#fast_reauth=1
1097 249558a2 Scott Ullrich
network={
1098 454756b9 Scott Ullrich
ssid="{$wlcfg['ssid']}"
1099
scan_ssid=1
1100 2ac908dd Espen Johansen
priority=5
1101
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
1102 454756b9 Scott Ullrich
psk="{$wlcfg['wpa']['passphrase']}"
1103 2ac908dd Espen Johansen
pairwise={$wlcfg['wpa']['wpa_pairwise']}
1104
group={$wlcfg['wpa']['wpa_pairwise']}
1105 50ad3b7c Scott Ullrich
}
1106
EOD;
1107
1108 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
1109 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
1110
				fclose($fd);
1111 8d27a5fe Espen Johansen
1112 99e72ce8 Scott Ullrich
				fwrite($fd_set, kill_wpasupplicant($if));
1113 ac3f8318 Espen Johansen
			}
1114
		break;
1115 459d6351 Scott Ullrich
1116 ac3f8318 Espen Johansen
		case 'hostap':
1117
			if (isset($wlcfg['wpa']['enable'])) {
1118
				$wpa .= <<<EOD
1119 459d6351 Scott Ullrich
interface={$if}
1120
driver=bsd
1121
logger_syslog=-1
1122
logger_syslog_level=0
1123
logger_stdout=-1
1124
logger_stdout_level=0
1125 2ac908dd Espen Johansen
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
1126
ctrl_interface={$g['varrun_path']}/hostapd
1127 459d6351 Scott Ullrich
ctrl_interface_group=wheel
1128 2ac908dd Espen Johansen
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
1129
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
1130 b67d192d Scott Ullrich
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
1131 459d6351 Scott Ullrich
ssid={$wlcfg['ssid']}
1132 2ac908dd Espen Johansen
debug={$wlcfg['wpa']['debug_mode']}
1133
auth_algs={$wlcfg['wpa']['auth_algs']}
1134
wpa={$wlcfg['wpa']['wpa_mode']}
1135
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
1136
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
1137 ac3f8318 Espen Johansen
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
1138
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
1139
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
1140 2ac908dd Espen Johansen
wpa_passphrase={$wlcfg['wpa']['passphrase']}
1141
ieee8021x={$wlcfg['wpa']['ieee8021x']}
1142 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
1143
#rsn_preauth=1
1144
#rsn_preauth_interfaces=eth0
1145 459d6351 Scott Ullrich
EOD;
1146 2ac908dd Espen Johansen
1147 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
1148 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
1149
				fclose($fd);
1150 2ac908dd Espen Johansen
1151 99e72ce8 Scott Ullrich
				fwrite($fd_set, kill_hostapd($if));
1152 ac3f8318 Espen Johansen
			}
1153
		break;
1154 5508cf57 Scott Ullrich
1155 ac3f8318 Espen Johansen
		case 'adhoc':
1156 99e72ce8 Scott Ullrich
			fwrite($fd_set, kill_hostapd($if));
1157
			fwrite($fd_set, kill_wpasupplicant($if));
1158 ac3f8318 Espen Johansen
		break;
1159 eb772abd Scott Ullrich
	}
1160 ac3f8318 Espen Johansen
1161 4742e927 Scott Ullrich
	/*
1162
	 *    all variables are set, lets start up everything
1163
     */
1164 eb772abd Scott Ullrich
1165 78922914 Scott Ullrich
	/* set ack timers according to users preference (if he/she has any) */
1166
	if($distance) {
1167 4742e927 Scott Ullrich
		fwrite($fd_set, "# Enable ATH distance settings\n");
1168
		fwrite($fd_set, "/sbin/athctrl.sh -i {$if} -d {$distance}\n");
1169 78922914 Scott Ullrich
	}
1170 eb772abd Scott Ullrich
1171 4d857dcf Scott Ullrich
	$standard_no_turbo = str_replace(" Turbo", "", $standard);
1172 eb772abd Scott Ullrich
1173 4742e927 Scott Ullrich
	$settings = <<<EOD
1174
1175 2f19fa14 Scott Ullrich
{$ifconfig} {$if} down
1176 490b8b2a Scott Ullrich
{$ifconfig} {$if} {$standard_no_turbo}
1177 8a958125 Scott Ullrich
{$ifconfig} {$if} {$channel}
1178 4742e927 Scott Ullrich
{$ifconfig} {$if} {$turbo}
1179 8a958125 Scott Ullrich
{$ifconfig} {$if} {$ssid}
1180 4742e927 Scott Ullrich
{$ifconfig} {$if} {$hidessid}
1181 8a958125 Scott Ullrich
{$ifconfig} {$if} {$adhocmode}
1182
{$ifconfig} {$if} {$protmode}
1183 4742e927 Scott Ullrich
{$ifconfig} {$if} {$pureg}
1184
{$ifconfig} {$if} {$apbridge}
1185
{$ifconfig} {$if} {$wme}
1186
{$ifconfig} {$if} {$wepset}
1187
{$ifconfig} {$if} {$txpower}
1188 77045939 Chris Buechler
{$ifconfig} {$if} {$hostapmode}
1189 4742e927 Scott Ullrich
{$ifconfig} {$if} up
1190
1191
EOD;
1192 eb772abd Scott Ullrich
1193 4742e927 Scott Ullrich
	/* write out above <<EOD stuff */
1194
	fwrite($fd_set, $settings);
1195 da1dab20 Scott Ullrich
1196 ac3f8318 Espen Johansen
	if (isset($wlcfg['wpa']['enable'])) {
1197 eb772abd Scott Ullrich
		if ($wlcfg['mode'] == "bss")
1198 4742e927 Scott Ullrich
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
1199 eb772abd Scott Ullrich
		if ($wlcfg['mode'] == "hostap")
1200 4742e927 Scott Ullrich
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
1201 ac3f8318 Espen Johansen
	}
1202 191a8175 Scott Ullrich
1203 4742e927 Scott Ullrich
	fclose($fd_set);
1204
1205 8a958125 Scott Ullrich
	conf_mount_ro();
1206
1207 4742e927 Scott Ullrich
	/* execute commands now in shell */
1208
	mwexec("/bin/sh /tmp/{$if}_setup.sh");
1209 99e72ce8 Scott Ullrich
	sleep(2);
1210 fed043ef Ermal Luçi
	// XXX: ermal - This seems like not needed!? 
1211
	//mwexec("/bin/sh /tmp/{$if}_setup.sh");
1212 191a8175 Scott Ullrich
1213 ac3f8318 Espen Johansen
	return 0;
1214 cfc707f7 Scott Ullrich
1215 5b237745 Scott Ullrich
}
1216
1217 eba938e3 Scott Ullrich
function kill_hostapd($interface) {
1218 99e72ce8 Scott Ullrich
	return "/bin/ps awwuxx | grep hostapd | grep $interface | awk '{ print \$2 }' | xargs kill\n";
1219 4b2a6180 Scott Ullrich
}
1220
1221 eba938e3 Scott Ullrich
function kill_wpasupplicant($interface) {
1222 99e72ce8 Scott Ullrich
	return "/bin/ps awwuxx | grep wpa_supplicant | grep $interface | awk '{ print \$2 }' | xargs kill\n";
1223 4b2a6180 Scott Ullrich
}
1224
1225 eba938e3 Scott Ullrich
function find_dhclient_process($interface) {
1226
	$realinterface = get_real_interface($interface);
1227 89fcabc4 Scott Ullrich
	if($realinterface)
1228
		$pid = `ps awwwux | grep dhclient | grep -v grep | grep {$realinterface} | awk '{ print \$2 }'`;
1229 0311dbd5 Scott Ullrich
	return $pid;
1230
}
1231
1232 eba938e3 Scott Ullrich
function interface_configure($interface = "wan") {
1233 675aac3d Ermal Luçi
	global $config, $g;
1234 31b24870 Ermal Luçi
	global $interface_sn_arr_cache, $interface_ip_arr_cache;
1235 cfc707f7 Scott Ullrich
1236 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1237
1238 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
1239 cfc707f7 Scott Ullrich
1240 5c6d0f65 Colin Smith
	if(!$g['booting']) {
1241 1fb7c265 Ermal Luçi
		/* remove all addresses first */
1242 332683cb Seth Mos
		while (mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " -alias", true) == 0);
1243 039f7491 Ermal Luçi
		interface_bring_down($interface);
1244 8c167904 Ermal Luçi
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " down");
1245 1fb7c265 Ermal Luçi
	}
1246 5b237745 Scott Ullrich
	/* wireless configuration? */
1247
	if (is_array($wancfg['wireless']))
1248 2d8d4f56 Ermal Luçi
		interface_wireless_configure($realif, $wancfg['wireless']);
1249 cfc707f7 Scott Ullrich
1250 f36d4bd2 Scott Ullrich
	if ($wancfg['spoofmac']) {
1251 1fb7c265 Ermal Luçi
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
1252 5b237745 Scott Ullrich
			" link " . escapeshellarg($wancfg['spoofmac']));
1253 f36d4bd2 Scott Ullrich
	}  else {
1254 1fb7c265 Ermal Luçi
		$mac = get_interface_mac_address($wancfg['if']);
1255 f36d4bd2 Scott Ullrich
		if($mac == "ff:ff:ff:ff:ff:ff") {
1256
			/*   this is not a valid mac address.  generate a
1257
			 *   temporary mac address so the machine can get online.
1258
			 */
1259 9315ef83 Scott Ullrich
			echo "Generating new MAC address.";
1260 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
1261 1fb7c265 Ermal Luçi
			mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) .
1262 f36d4bd2 Scott Ullrich
				" link " . escapeshellarg($random_mac));
1263
			$wancfg['spoofmac'] = $random_mac;
1264
			write_config();
1265 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");
1266 f36d4bd2 Scott Ullrich
		}
1267
	}
1268 cfc707f7 Scott Ullrich
1269 5b237745 Scott Ullrich
	/* media */
1270
	if ($wancfg['media'] || $wancfg['mediaopt']) {
1271 1fb7c265 Ermal Luçi
		$cmd = "/sbin/ifconfig " . escapeshellarg($wancfg['if']);
1272 5b237745 Scott Ullrich
		if ($wancfg['media'])
1273
			$cmd .= " media " . escapeshellarg($wancfg['media']);
1274
		if ($wancfg['mediaopt'])
1275
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
1276
		mwexec($cmd);
1277
	}
1278 cfc707f7 Scott Ullrich
1279 31b24870 Ermal Luçi
	/* invalidate interface/ip/sn cache */
1280 eba938e3 Scott Ullrich
	get_interface_arr(true);
1281 31b24870 Ermal Luçi
	unset($interface_ip_arr_cache[$realif]);
1282
	unset($interface_sn_arr_cache[$realif]);
1283 ccbd2447 Ermal Luçi
1284 5b237745 Scott Ullrich
	switch ($wancfg['ipaddr']) {
1285 cfc707f7 Scott Ullrich
1286 d5d00b83 Scott Ullrich
		case 'carpdev-dhcp':
1287 1fb7c265 Ermal Luçi
			interface_carpdev_dhcp_configure($interface);
1288 d5d00b83 Scott Ullrich
			break;
1289 5b237745 Scott Ullrich
		case 'dhcp':
1290 1fb7c265 Ermal Luçi
			interface_dhcp_configure($interface);
1291 5b237745 Scott Ullrich
			break;
1292 cfc707f7 Scott Ullrich
1293 5b237745 Scott Ullrich
		case 'pppoe':
1294 1fb7c265 Ermal Luçi
			interface_pppoe_configure($interface);
1295 5b237745 Scott Ullrich
			break;
1296 cfc707f7 Scott Ullrich
1297 5b237745 Scott Ullrich
		case 'pptp':
1298 1fb7c265 Ermal Luçi
			interface_pptp_configure($interface);
1299 5b237745 Scott Ullrich
			break;
1300 cfc707f7 Scott Ullrich
1301 5b237745 Scott Ullrich
		default:
1302 4b176ed2 Ermal Luçi
			if ($wancfg['ipaddr'] <> "" && $wancfg['subnet'] <> "") {
1303
				if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
1304 d7147b1c Scott Ullrich
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " " .
1305
						escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
1306
						" " . escapeshellarg($wancfg['pointtopoint']) . " up");
1307 4b176ed2 Ermal Luçi
				} else {
1308 630cfa6c Scott Ullrich
					if($wancfg['ipaddr'] && $wancfg['subnet'])
1309
						mwexec("/sbin/ifconfig " . escapeshellarg($realif) .
1310
							" " . escapeshellarg($wancfg['ipaddr'] . "/" . 
1311
							$wancfg['subnet']));
1312 4b176ed2 Ermal Luçi
				}
1313 a23d7248 Scott Ullrich
			}
1314 cfc707f7 Scott Ullrich
1315 67ee1ec5 Ermal Luçi
			if (is_ipaddr($wancfg['gateway']))
1316 723e16b8 Ermal Luçi
				file_put_contents("/tmp/{$realif}_router", $wancfg['gateway']);
1317 5b237745 Scott Ullrich
	}
1318 d7147b1c Scott Ullrich
	if($wancfg['if'])
1319 b5b957fe Scott Ullrich
		interfaces_bring_up($wancfg['if']);
1320 d7147b1c Scott Ullrich
	else 
1321
		log_error("Could not bring wancfg['if'] up -- variable not defined in interface_configure()");
1322 1f15f43f Ermal Luçi
	
1323 5b237745 Scott Ullrich
	if (!$g['booting']) {
1324 ccbd2447 Ermal Luçi
		if (link_interface_to_gre($interface)) {
1325
			foreach ($config['gres']['gre'] as $gre)
1326
				if ($gre['if'] == $interface)
1327
					interface_gre_configure($gre);
1328
		}
1329
		if (link_interface_to_gif($interface)) {
1330
                	foreach ($config['gifs']['gif'] as $gif)
1331
				if ($gif['if'] == $interface)
1332
                        		interface_gre_configure($gif);
1333
        	}
1334
		if (link_interface_to_bridge($interface)) {
1335
			foreach ($config['bridges']['bridged'] as $bridge)
1336
				if (stristr($bridge['members'], "{$interface}"))
1337
					interface_bridge_configure($bridge);
1338
		}
1339
1340
		/* XXX: Shouldn't the caller do this?! */
1341 4476d447 Ermal Luçi
		/* XXX */
1342
		if ($interface = "lan")
1343
			/* make new hosts file */
1344
                	system_hosts_generate();
1345
1346 5b237745 Scott Ullrich
		/* reconfigure static routes (kernel may have deleted them) */
1347
		system_routing_configure();
1348 cfc707f7 Scott Ullrich
1349 e239df5a Scott Ullrich
		/* set the reload filter dity flag */
1350 be38535c Ermal Luçi
		filter_configure();
1351 cfc707f7 Scott Ullrich
1352 5b237745 Scott Ullrich
		/* reload ipsec tunnels */
1353
		vpn_ipsec_configure();
1354 cfc707f7 Scott Ullrich
1355 4476d447 Ermal Luçi
		/* update dyndns */
1356 5b237745 Scott Ullrich
		services_dyndns_configure();
1357 cfc707f7 Scott Ullrich
1358 a23d7248 Scott Ullrich
		/* force DNS update */
1359
		services_dnsupdate_process();
1360
1361 5b237745 Scott Ullrich
		/* restart dnsmasq */
1362
		services_dnsmasq_configure();
1363 eb772abd Scott Ullrich
1364 c597d50f Scott Ullrich
		/* reload captive portal */
1365
		captiveportal_configure();
1366 5b237745 Scott Ullrich
	}
1367 cfc707f7 Scott Ullrich
1368 5e99d81a Scott Ullrich
1369 c1627786 Scott Ullrich
	unmute_kernel_msgs();
1370
1371 5b237745 Scott Ullrich
	return 0;
1372
}
1373
1374 eba938e3 Scott Ullrich
function interface_carpdev_dhcp_configure($interface = "wan") {
1375 d5d00b83 Scott Ullrich
	global $config, $g;
1376
1377 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1378 499994ff Scott Ullrich
	$wanif = $wancfg['if'];
1379 d5d00b83 Scott Ullrich
	/* bring wan interface up before starting dhclient */
1380 d7147b1c Scott Ullrich
	if($wanif)
1381 b5b957fe Scott Ullrich
		interfaces_bring_up($wanif);
1382 d7147b1c Scott Ullrich
	else 
1383
		log_error("Could not bring wanif up in terface_carpdev_dhcp_configure()");
1384 d5d00b83 Scott Ullrich
1385
	return 0;
1386
}
1387
1388 eba938e3 Scott Ullrich
function interface_dhcp_configure($interface = "wan") {
1389 5b237745 Scott Ullrich
	global $config, $g;
1390 cfc707f7 Scott Ullrich
1391 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1392 5b237745 Scott Ullrich
1393 0311dbd5 Scott Ullrich
	/* generate dhclient_wan.conf */
1394 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
1395 5b237745 Scott Ullrich
	if (!$fd) {
1396 67ee1ec5 Ermal Luçi
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_wan_dhcp_configure() for writing.\n");
1397 5b237745 Scott Ullrich
		return 1;
1398
	}
1399 eb772abd Scott Ullrich
1400 2305d4c5 Scott Ullrich
	if ($wancfg['dhcphostname']) {
1401
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
1402
		$dhclientconf_hostname .= "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
1403
	} else {
1404
		$dhclientconf_hostname = "";
1405
	}
1406
1407 85a5da13 Ermal Luçi
	$wanif = get_real_interface($interface);
1408 cfc707f7 Scott Ullrich
1409 67ee1ec5 Ermal Luçi
 	$dhclientconf = "";
1410
	
1411 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
1412 67ee1ec5 Ermal Luçi
interface "{$wanif}" {
1413 76d3b9a3 Chris Buechler
timeout 60;
1414 ce69a638 Scott Ullrich
retry 1;
1415
select-timeout 0;
1416
initial-interval 1;
1417 2305d4c5 Scott Ullrich
	{$dhclientconf_hostname}
1418
	script "/sbin/dhclient-script";
1419 5b237745 Scott Ullrich
}
1420
1421
EOD;
1422
1423 bc40d758 Seth Mos
if(is_ipaddr($wancfg['alias-address'])) {
1424
	$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
1425
	$dhclientconf .= <<<EOD
1426
alias {
1427 67ee1ec5 Ermal Luçi
	interface  "{$wanif}";
1428 bc40d758 Seth Mos
	fixed-address {$wancfg['alias-address']};
1429
	option subnet-mask {$subnetmask};
1430
}
1431
1432
EOD;
1433
}
1434 5b237745 Scott Ullrich
	fwrite($fd, $dhclientconf);
1435
	fclose($fd);
1436 eb772abd Scott Ullrich
1437 67ee1ec5 Ermal Luçi
	$relwanif = $wancfg['if'];
1438 eb772abd Scott Ullrich
1439 d7147b1c Scott Ullrich
	/* bring wan interface up before starting dhclient */
1440
	if($realwanif)
1441 b5b957fe Scott Ullrich
		interfaces_bring_up($realwanif);
1442
	else 
1443
		log_error("Could not bring realwanif up in interface_dhcp_configure()");
1444 eacc8c14 Scott Ullrich
1445 d7147b1c Scott Ullrich
	/* fire up dhclient */
1446
	mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif} >/tmp/{$wanif}_output >/tmp/{$wanif}_error_output");
1447 cfc707f7 Scott Ullrich
1448 fdca0ea8 Scott Ullrich
	$fout = fopen("/tmp/ifconfig_{$wanif}","w");
1449 67ee1ec5 Ermal Luçi
	fwrite($fout, "/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif}");
1450 0119d2f7 Scott Ullrich
	fclose($fout);
1451
1452 5b237745 Scott Ullrich
	return 0;
1453
}
1454
1455 f620d00d Ermal Luçi
function interface_pppoe_configure($interface = "wan") 
1456
{
1457 5b237745 Scott Ullrich
	global $config, $g;
1458 cfc707f7 Scott Ullrich
1459 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1460 cfc707f7 Scott Ullrich
1461 5b237745 Scott Ullrich
	/* generate mpd.conf */
1462 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1463 5b237745 Scott Ullrich
	if (!$fd) {
1464 1fb7c265 Ermal Luçi
		printf("Error: cannot open mpd_{$interface}.conf in interface_pppoe_configure().\n");
1465 5b237745 Scott Ullrich
		return 1;
1466
	}
1467 cfc707f7 Scott Ullrich
1468 5b237745 Scott Ullrich
	$idle = 0;
1469 cfc707f7 Scott Ullrich
1470 67ee1ec5 Ermal Luçi
	if (isset($wancfg['ondemand'])) {
1471 5b237745 Scott Ullrich
		$ondemand = "enable";
1472 67ee1ec5 Ermal Luçi
		if ($wancfg['timeout'])
1473
			$idle = $wancfg['timeout'];
1474 5b237745 Scott Ullrich
	} else {
1475
		$ondemand = "disable";
1476
	}
1477 cfc707f7 Scott Ullrich
1478 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1479 07cae4b2 Scott Ullrich
startup:
1480
pppoeclient:
1481 cc936773 Scott Ullrich
1482 5b237745 Scott Ullrich
EOD;
1483 389741e5 Scott Ullrich
1484 67ee1ec5 Ermal Luçi
	if ($interface == "wan")
1485
		$realif = "pppoe0";
1486
	else {
1487
		// Here code assumes only that strings of form "opt#" will be passed.
1488
		$realif = "pppoe" . substr($interface, 3); 
1489
	}
1490
	
1491
	$mpdconf .= <<<EOD
1492
	new -i {$realif} pppoeclient pppoeclient
1493
1494
EOD;
1495
	if ($interface == "wan")
1496 389741e5 Scott Ullrich
		$mpdconf .= <<<EOD
1497 67ee1ec5 Ermal Luçi
	set iface route default
1498
1499
EOD;
1500
	
1501
	$mpdconf .= <<<EOD
1502
	set iface {$ondemand} on-demand
1503
	set iface idle {$idle}
1504 0750014f Ermal Luçi
	set iface enable tcpmssfix
1505 67ee1ec5 Ermal Luçi
	set iface up-script /usr/local/sbin/ppp-linkup
1506 389741e5 Scott Ullrich
	set iface down-script /usr/local/sbin/ppp-linkdown
1507
1508
EOD;
1509
1510 67ee1ec5 Ermal Luçi
	if (isset($wancfg['ondemand'])) {
1511
		if (isset($wancfg['local-ip']) && isset($wancfg['remote-ip'])) {
1512 41404ef1 Scott Ullrich
			$mpdconf .= <<<EOD
1513 67ee1ec5 Ermal Luçi
	set iface addrs {$wancfg['local-ip']} {$wancfg['remote-ip']}
1514 5b237745 Scott Ullrich
1515
EOD;
1516 41404ef1 Scott Ullrich
		} else {
1517
			$mpdconf .= <<<EOD
1518
	set iface addrs 192.0.2.112 192.0.2.113
1519
1520
EOD;
1521
		}
1522 5b237745 Scott Ullrich
	}
1523 cfc707f7 Scott Ullrich
1524 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
1525
	set bundle disable multilink
1526 8da53af8 Ermal Luçi
	set auth authname "{$wancfg['pppoe_username']}"
1527
	set auth password "{$wancfg['pppoe_password']}"
1528 5b237745 Scott Ullrich
	set link keep-alive 10 60
1529
	set link max-redial 0
1530
	set link no acfcomp protocomp
1531
	set link disable pap chap
1532
	set link accept chap
1533 b4914b78 Ermal Luçi
	
1534
EOD;
1535
	if (empty($wancfg['mtu']))
1536
		$mpdmtu = "1492";
1537
	else 
1538
		$mpdmtu = "{$wancfg['mtu']}";
1539
1540
	$mpdconf .= <<<EOD
1541
	set link mtu {$mpdmtu}
1542 5b237745 Scott Ullrich
	set ipcp yes vjcomp
1543
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1544 a23d7248 Scott Ullrich
1545
EOD;
1546
1547
	if (isset($config['system']['dnsallowoverride'])) {
1548
		$mpdconf .= <<<EOD
1549 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
1550 a23d7248 Scott Ullrich
1551
EOD;
1552
	}
1553 a0ff9696 Scott Ullrich
1554 56e5f847 Ermal Luçi
	if (!isset($wancfg['dnsnosec']) && isset($config['system']['dnsallowoverride'])) {
1555 98e392c5 Scott Ullrich
			$mpdconf .= <<<EOD
1556
	set ipcp enable req-sec-dns
1557
1558
EOD;
1559
	}
1560
	
1561 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
1562 07cae4b2 Scott Ullrich
	open
1563 5b237745 Scott Ullrich
1564
EOD;
1565
1566
	fwrite($fd, $mpdconf);
1567
	fclose($fd);
1568 cfc707f7 Scott Ullrich
1569 5b237745 Scott Ullrich
	/* generate mpd.links */
1570 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.links", "w");
1571 5b237745 Scott Ullrich
	if (!$fd) {
1572 1fb7c265 Ermal Luçi
		printf("Error: cannot open mpd_{$interface}.links in interface_pppoe_configure().\n");
1573 5b237745 Scott Ullrich
		return 1;
1574
	}
1575 cfc707f7 Scott Ullrich
1576 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1577 07cae4b2 Scott Ullrich
pppoeclient:
1578 5b237745 Scott Ullrich
	set link type pppoe
1579
	set pppoe iface {$wancfg['if']}
1580 67ee1ec5 Ermal Luçi
	set pppoe service "{$wancfg['provider']}"
1581 5b237745 Scott Ullrich
	set pppoe enable originate
1582
	set pppoe disable incoming
1583
1584
EOD;
1585
1586
	fwrite($fd, $mpdconf);
1587
	fclose($fd);
1588 eb772abd Scott Ullrich
1589 67ee1ec5 Ermal Luçi
	if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid") and $g['booting']) {
1590 d7a6517a Scott Ullrich
		/* if we are booting and mpd has already been started then don't start again. */
1591
	} else {
1592
		/* if mpd is active, lets take it down */
1593 67ee1ec5 Ermal Luçi
		if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid")) {
1594
			killbypid("{$g['varrun_path']}/pppoe_{$interface}.pid");
1595 d7a6517a Scott Ullrich
			sleep(3);
1596
		}
1597 571f89fa Ermal Luçi
1598
		/* Bring the parent interface up */
1599 d7147b1c Scott Ullrich
		if($wancfg['if'])
1600 b5b957fe Scott Ullrich
			interfaces_bring_up($wancfg['if']);
1601 d7147b1c Scott Ullrich
		else 
1602
			log_error("Could not bring wancfg['if'] up in interface_pppoe_configure()");
1603 571f89fa Ermal Luçi
1604 eb772abd Scott Ullrich
		/* fire up mpd */
1605 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");
1606 ec11a1ad Scott Ullrich
	}
1607
1608 b5b957fe Scott Ullrich
	/* sleep until wan is up - or 30 seconds, whichever comes first */
1609 a205d904 Scott Ullrich
	for ($count = 0; $count < 30; $count++) {
1610 571f89fa Ermal Luçi
		if(file_exists("{$g['tmp_path']}/{$interface}up")) {
1611 a205d904 Scott Ullrich
			break;
1612
		}
1613
		sleep(1);
1614
	}
1615 d7a6517a Scott Ullrich
1616 571f89fa Ermal Luçi
	unlink_if_exists("{$g['tmp_path']}/{$interface}up");
1617 e1c8cdf5 Scott Ullrich
1618 5b237745 Scott Ullrich
	return 0;
1619
}
1620
1621 eba938e3 Scott Ullrich
function interface_pptp_configure($interface) {
1622 5b237745 Scott Ullrich
	global $config, $g;
1623 cfc707f7 Scott Ullrich
1624 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
1625 cfc707f7 Scott Ullrich
1626 5b237745 Scott Ullrich
	/* generate mpd.conf */
1627 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1628 5b237745 Scott Ullrich
	if (!$fd) {
1629 1fb7c265 Ermal Luçi
		printf("Error: cannot open mpd_{$interface}.conf in interface_pptp_configure().\n");
1630 5b237745 Scott Ullrich
		return 1;
1631
	}
1632 cfc707f7 Scott Ullrich
1633 5b237745 Scott Ullrich
	$idle = 0;
1634 cfc707f7 Scott Ullrich
1635 67ee1ec5 Ermal Luçi
	if (isset($wancfg['ondemand'])) {
1636 5b237745 Scott Ullrich
		$ondemand = "enable";
1637 67ee1ec5 Ermal Luçi
		if ($wancfg['timeout'])
1638
			$idle = $wancfg['timeout'];
1639 5b237745 Scott Ullrich
	} else {
1640
		$ondemand = "disable";
1641
	}
1642 cfc707f7 Scott Ullrich
1643 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1644 67ee1ec5 Ermal Luçi
startup:
1645 5b237745 Scott Ullrich
pptp:
1646
1647
EOD;
1648 cfc707f7 Scott Ullrich
1649 67ee1ec5 Ermal Luçi
        if ($interface == "wan")
1650
                $realif = "pptp0";
1651
        else {
1652
                // Here code assumes only that strings of form "opt#" will be passed.
1653
                $realif = "pptp" . substr($interface, 3);
1654
	}
1655
1656
        $mpdconf .= <<<EOD
1657
        new -i {$realif} pptp pptp 
1658
1659
EOD;
1660
        if ($interface == "wan")
1661
                $mpdconf .= <<<EOD
1662
        set iface route default
1663
1664
EOD;
1665
1666
        $mpdconf .= <<<EOD
1667
	set iface {$ondemand} on-demand
1668
	set iface idle {$idle}
1669
	set iface up-script /usr/local/sbin/ppp-linkup
1670 389741e5 Scott Ullrich
	set iface down-script /usr/local/sbin/ppp-linkdown
1671
1672
EOD;
1673
1674 67ee1ec5 Ermal Luçi
	if (isset($wanfg['ondemand'])) {
1675 5b237745 Scott Ullrich
		$mpdconf .= <<<EOD
1676 a23d7248 Scott Ullrich
	set iface addrs 10.0.0.1 10.0.0.2
1677 5b237745 Scott Ullrich
1678
EOD;
1679
	}
1680 cfc707f7 Scott Ullrich
1681 5b237745 Scott Ullrich
	$mpdconf .= <<<EOD
1682
	set bundle disable multilink
1683 8da53af8 Ermal Luçi
	set bundle authname "{$wancfg['pptp_username']}"
1684
	set bundle password "{$wancfg['pptp_password']}"
1685 58384045 Chris Buechler
	set bundle no noretry
1686 5b237745 Scott Ullrich
	set link keep-alive 10 60
1687
	set link max-redial 0
1688
	set link no acfcomp protocomp
1689
	set link disable pap chap
1690
	set link accept chap
1691
	set ipcp no vjcomp
1692
	set ipcp ranges 0.0.0.0/0 0.0.0.0/0
1693 a23d7248 Scott Ullrich
1694
EOD;
1695
	if (isset($config['system']['dnsallowoverride'])) {
1696
		$mpdconf .= <<<EOD
1697 5b237745 Scott Ullrich
	set ipcp enable req-pri-dns
1698 a23d7248 Scott Ullrich
1699
EOD;
1700
	}
1701 a0ff9696 Scott Ullrich
1702 a23d7248 Scott Ullrich
	$mpdconf .= <<<EOD
1703 5b237745 Scott Ullrich
	open
1704
1705
EOD;
1706
1707
	fwrite($fd, $mpdconf);
1708
	fclose($fd);
1709 cfc707f7 Scott Ullrich
1710 5b237745 Scott Ullrich
	/* generate mpd.links */
1711 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.links", "w");
1712 5b237745 Scott Ullrich
	if (!$fd) {
1713 1fb7c265 Ermal Luçi
		printf("Error: cannot open mpd_{$interface}.links in interface_pptp_configure().\n");
1714 5b237745 Scott Ullrich
		return 1;
1715
	}
1716 cfc707f7 Scott Ullrich
1717 5b237745 Scott Ullrich
	$mpdconf = <<<EOD
1718
pptp:
1719
	set link type pptp
1720
	set pptp enable originate outcall
1721
	set pptp disable windowing
1722 67ee1ec5 Ermal Luçi
	set pptp self {$wancfg['local']}
1723
	set pptp peer {$wancfg['remote']}
1724 5b237745 Scott Ullrich
1725
EOD;
1726
1727
	fwrite($fd, $mpdconf);
1728
	fclose($fd);
1729 cfc707f7 Scott Ullrich
1730 5b237745 Scott Ullrich
	/* configure interface */
1731 d7147b1c Scott Ullrich
	if($wancfg['if'])
1732
		mwexec("/sbin/ifconfig " . escapeshellarg($wancfg['if']) . " " .
1733
			escapeshellarg($wancfg['local'] . "/" . $wancfg['subnet']) . " up");
1734
	else 
1735
		log_error("Could not bring interface wancfg['if'] up in interface_pptp_configure()");
1736 5b237745 Scott Ullrich
	/* fire up mpd */
1737 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");
1738 cfc707f7 Scott Ullrich
1739 5b237745 Scott Ullrich
	return 0;
1740
}
1741 f6b761fb Scott Ullrich
 
1742 abb31ea4 Ermal Luçi
/* XXX: stub for code that references the old functions(mostly packages) */
1743 eba938e3 Scott Ullrich
function get_real_wan_interface($interface = "wan") {
1744 abb31ea4 Ermal Luçi
	return get_real_interface($interface);
1745
}
1746 eba938e3 Scott Ullrich
function get_current_wan_address($interface = "wan") {
1747 abb31ea4 Ermal Luçi
	return get_interface_ip($interface);
1748
}
1749
1750 eba938e3 Scott Ullrich
function get_real_interface($interface = "wan") {
1751 67ee1ec5 Ermal Luçi
    global $config;
1752 cfc707f7 Scott Ullrich
1753 67ee1ec5 Ermal Luçi
	$wanif = $interface;
1754 c515ea57 Scott Ullrich
1755 67ee1ec5 Ermal Luçi
	switch ($interface) {
1756
	case "pptp":
1757
		$wanif = "pptp";
1758
		break;
1759
	case "pppoe":
1760
		$wanif = "pppoe";
1761
		break;
1762
	case "openvpn":
1763
		$wanif = "openvpn";
1764
		break;
1765
	case "enc0":
1766
		$wanif = "enc0";
1767
		break;
1768
	/* XXX: dial in support?!
1769
	case "ppp":
1770
		$wanif = "ppp";
1771
		break;
1772
	*/
1773
	default:
1774 008760d0 Ermal Luçi
		$iflist = get_configured_interface_with_descr(false, true);
1775 67ee1ec5 Ermal Luçi
1776
		foreach ($iflist as $if => $ifdesc) {
1777
			if ($interface == $if || $interface == $ifdesc) {
1778
1779
			$cfg = $config['interfaces'][$if];
1780
1781
			switch ($cfg['ipaddr']) {
1782
			case "carpdev-dhcp":
1783
				$viparr = &$config['virtualip']['vip'];
1784
				$counter = 0;
1785
				if(is_array($viparr))
1786
				foreach ($viparr as $vip) {
1787
					if ($vip['mode'] == "carpdev-dhcp") {
1788
						if($vip['interface'] == $if) {
1789
							$wanif =  "carp{$counter}";
1790
							break;
1791
						}
1792
						$counter++;
1793
					} else if ($vip['mode'] = "carp") 
1794
						$counter++;
1795 c515ea57 Scott Ullrich
				}
1796 67ee1ec5 Ermal Luçi
				break;
1797
			case "pppoe": 
1798
				if ($if == "wan")
1799
					$wanif = "pppoe0";
1800
				else
1801
					$wanif = "pppoe" . substr($if,3);
1802
				break;
1803
			case "pptp": 
1804
				if ($if == "wan")
1805
					$wanif = "pptp0";
1806
				else
1807
					$wanif = "pptp" . substr($if, 3);
1808
				break;
1809
			default:
1810 9b1c39e3 Ermal Luçi
				$wanif = $cfg['if'];
1811 67ee1ec5 Ermal Luçi
				break;
1812
			}
1813
			
1814
			break;
1815 c515ea57 Scott Ullrich
			}
1816
		}
1817 67ee1ec5 Ermal Luçi
		break;
1818 c515ea57 Scott Ullrich
	}
1819
1820 67ee1ec5 Ermal Luçi
    return $wanif;
1821 5b237745 Scott Ullrich
}
1822
1823 eba938e3 Scott Ullrich
function get_interface_ip($interface = "wan") {
1824 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
1825 67ee1ec5 Ermal Luçi
	/* Do we really come here for these interfaces ?! */
1826
	if (in_array($realif, array("pptp", "pppoe", "openvpn", "enc0" /* , "ppp" */)))
1827
			return "";
1828 cfc707f7 Scott Ullrich
1829 85a5da13 Ermal Luçi
	$curip = find_interface_ip($realif);
1830
	if ($curip && is_ipaddr($curip) && ($curip != "0.0.0.0"))
1831
		return $curip;
1832 67ee1ec5 Ermal Luçi
1833 85a5da13 Ermal Luçi
	return null;
1834 5b237745 Scott Ullrich
}
1835
1836 eba938e3 Scott Ullrich
function get_interface_subnet($interface = "wan") {
1837 31b24870 Ermal Luçi
	$realif = get_real_interface($interface);
1838
	/* Do we really come here for these interfaces ?! */
1839
	if (in_array($realif, array("pptp", "pppoe", "openvpn", "enc0" /* , "ppp" */)))
1840
		return "";
1841
1842
	$cursn = find_interface_subnet($realif);
1843
	if (!empty($cursn))
1844
		return $cursn;
1845
1846
	return null;
1847
}
1848
1849 a57b119e Bill Marquette
/****f* interfaces/is_altq_capable
1850
 * NAME
1851
 *   is_altq_capable - Test if interface is capable of using ALTQ
1852
 * INPUTS
1853
 *   $int            - string containing interface name
1854
 * RESULT
1855
 *   boolean         - true or false
1856
 ******/
1857
1858 eba938e3 Scott Ullrich
function is_altq_capable($int) {
1859 a57b119e Bill Marquette
        /* Per:
1860
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+6.0-current&format=html
1861
         * Only the following drivers have ALTQ support
1862
         */
1863
        $capable = array("an", "ath", "awi", "bfe", "bge", "dc", "de", "ed",
1864 acdbfa04 Scott Ullrich
			"em", "fxp", "hme", "le", "nve", "re", "rl", "ndis", "sf", "sis", "sk",
1865
			"tun", "vr", "wi", "xl", "vlan", "ste", "aue", "bce", "ep", "gem", "ipw", 
1866 b380c491 Ermal Luçi
			"iwi", "msk", "mxge", "my", "nfe", "npe", "ral", "rum", "stge", "udav", 
1867
			"ural", "pppoe", "pptp", "ng", "ppp");
1868 a57b119e Bill Marquette
1869
        $int_family = preg_split("/[0-9]+/", $int);
1870
1871
        if (in_array($int_family[0], $capable))
1872
                return true;
1873
        else
1874
                return false;
1875
}
1876
1877 eba938e3 Scott Ullrich
function get_wireless_modes($interface) {
1878 d8c67d69 Scott Ullrich
	/* return wireless modes and channels */
1879 92f7d37d Ermal Luçi
	$wireless_modes = array();
1880
1881 d8c67d69 Scott Ullrich
	if(is_interface_wireless($interface)) {
1882
		$wi = 1;
1883
		$ifconfig = "/sbin/ifconfig";
1884
		$awk = "/usr/bin/awk";
1885
		$chan_list = "$ifconfig $interface list chan";
1886 4066776d Scott Ullrich
		$stack_list = "$awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
1887 d8c67d69 Scott Ullrich
		$format_list = "$awk '{print \$5 \" \" \$6 \",\" \$1}'";
1888
1889 4b0e71db Scott Ullrich
		$interface_channels = "";
1890 d8c67d69 Scott Ullrich
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
1891
		$interface_channel_count = count($interface_channels);
1892
1893
		$c = 0;
1894
		while ($c < $interface_channel_count)
1895
		{
1896
			$channel_line = explode(",", $interface_channels["$c"]);
1897
			$wireless_mode = trim($channel_line[0]);
1898
			$wireless_channel = trim($channel_line[1]);
1899 4066776d Scott Ullrich
			if(trim($wireless_mode) != "") {
1900
				/* if we only have 11g also set 11b channels */
1901
				if($wireless_mode == "11g") {
1902
					$wireless_modes["11b"] = array();
1903
				}
1904
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
1905
			}
1906 d8c67d69 Scott Ullrich
			$c++;
1907
		}
1908
	}
1909 4066776d Scott Ullrich
	return($wireless_modes);
1910 d8c67d69 Scott Ullrich
}
1911
1912 eba938e3 Scott Ullrich
function get_interface_mac($interface) {
1913 53c82ef9 Scott Ullrich
	/* build interface list with netstat */
1914
	$linkinfo = "";
1915
	exec("/usr/bin/netstat -I $interface -nW -f link", $linkinfo);
1916
	array_shift($linkinfo);
1917
	$alink = preg_split("/\s+/", $linkinfo[0]);
1918
	$mac = chop($alink[3]);
1919
	return $mac;
1920
}
1921 b7ec2b9e Scott Ullrich
1922 53c82ef9 Scott Ullrich
function setup_pppoe_reset_file($interface, $status) {
1923
	define("CRON_PPPOE_CMD_FILE", "/conf/pppoe{$interface}restart");
1924
	define("CRON_PPPOE_CMD", "#!/bin/sh\necho '<?php require(\"interfaces.inc\"); interface_reconfigure(\"$interface\"); services_dyndns_reset(\"$interface\"); filter_configure_sync(); ?>' | /usr/local/bin/php -q");
1925
	if($status == true) {
1926
		if(!file_exists(CRON_PPPOE_CMD_FILE)) {
1927
			file_put_contents(CRON_PPPOE_CMD_FILE, CRON_PPPOE_CMD);
1928
			chmod(CRON_PPPOE_CMD_FILE, 0700);
1929
		}	
1930
	} else {
1931
		unlink_if_exists(CRON_PPPOE_CMD_FILE);
1932
	}
1933 b7ec2b9e Scott Ullrich
}
1934
1935 3ae4960c Ermal Luçi
?>