Project

General

Profile

Download (91.4 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 acc1e9d0 Scott Ullrich
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	interfaces.inc
5 eba938e3 Scott Ullrich
	Copyright (C) 2004-2008 Scott Ullrich
6 58936a34 Ermal Lu?i
	Copyright (C) 2008-2009 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 523855b0 Scott Ullrich
38 abcb2bed Ermal Lu?i
	pfSense_BUILDER_BINARIES:	/usr/sbin/pppd	/sbin/dhclient	/bin/sh	/usr/bin/grep	/usr/bin/xargs	/usr/bin/awk	/usr/local/sbin/choparp
39 89c52814 Ermal
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig	/sbin/route	/usr/sbin/ngctl	/usr/sbin/arp	/bin/kill	/usr/local/sbin/mpd5
40 523855b0 Scott Ullrich
	pfSense_MODULE:	interfaces
41
42 5b237745 Scott Ullrich
*/
43
44
/* include all configuration functions */
45 7387844e Chris Buechler
require_once("globals.inc");
46 483e6de8 Scott Ullrich
require_once("cmd_chain.inc");
47 5b237745 Scott Ullrich
48 b5b957fe Scott Ullrich
function interfaces_bring_up($interface) {
49
	if(!$interface) {
50 ec054b7c Scott Ullrich
		log_error("interfaces_bring_up() was called but no variable defined.");
51
		log_error( "Backtrace: " . debug_backtrace() );
52 b5b957fe Scott Ullrich
		return;
53
	}
54 871768cf Ermal
	pfSense_interface_flags($interface, IFF_UP);
55 b5b957fe Scott Ullrich
}
56
57 52947718 Ermal Lu?i
/*
58
 * Return the interface array
59
 */
60
function get_interface_arr($flush = false) {
61
        global $interface_arr_cache;
62
63
        /* If the cache doesn't exist, build it */
64
        if (!isset($interface_arr_cache) or $flush)
65 8256f324 gnhb
                $interface_arr_cache = explode(" ", trim(`/sbin/ifconfig -l`));
66 52947718 Ermal Lu?i
67
        return $interface_arr_cache;
68
}
69
70
/*
71
 * does_interface_exist($interface): return true or false if a interface is
72
 * detected.
73
 */
74
function does_interface_exist($interface) {
75 8256f324 gnhb
	global $config;
76
	
77
	if(!$interface)
78
			return false;
79 52947718 Ermal Lu?i
80 8256f324 gnhb
	$ints = get_interface_arr();
81 6d5446a2 Ermal
	if (in_array($interface, $ints))
82 8256f324 gnhb
		return true;
83
	else
84
		return false;
85 52947718 Ermal Lu?i
}
86
87 eba938e3 Scott Ullrich
function interfaces_loopback_configure() {
88 7a6f7c55 Scott Ullrich
	if($g['booting'])
89
		echo "Configuring loopback interface...";
90 871768cf Ermal
	pfSense_interface_setaddress("lo0", "127.0.0.1");
91 b5b957fe Scott Ullrich
	interfaces_bring_up("lo0");
92 0aca91d0 Scott Ullrich
	exec("/sbin/route add 127.0.0.2 127.0.0.1");
93 7a6f7c55 Scott Ullrich
	if($g['booting'])
94
		echo "done.\n";
95 5b237745 Scott Ullrich
	return 0;
96
}
97
98 eba938e3 Scott Ullrich
function interfaces_vlan_configure() {
99 7a6f7c55 Scott Ullrich
	global $config, $g;
100 87519eb7 Scott Ullrich
	if($g['booting'])
101 7a6f7c55 Scott Ullrich
		echo "Configuring VLAN interfaces...";
102 5b6eac01 Scott Ullrich
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
103 e1c449c0 Ermal Lu?i
		foreach ($config['vlans']['vlan'] as $vlan) {
104 f620d00d Ermal Luçi
			if(empty($vlan['vlanif']))
105 48315e65 Ermal Luci
				$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
106 5b6eac01 Scott Ullrich
			/* XXX: Maybe we should report any errors?! */
107 5f1e1d26 Ermal Lu?i
			interface_vlan_configure($vlan);
108 517feb1c Seth Mos
		}
109 5b6eac01 Scott Ullrich
	}
110 87519eb7 Scott Ullrich
	if($g['booting'])
111 7a6f7c55 Scott Ullrich
		echo "done.\n";
112 2075fadb Ermal Luçi
}
113 cfc707f7 Scott Ullrich
114 abcb2bed Ermal Lu?i
function interface_vlan_configure(&$vlan) {
115 2075fadb Ermal Luçi
        global $config, $g;
116 161040eb Scott Ullrich
117 5f1e1d26 Ermal Lu?i
	if (!is_array($vlan)) {
118
		log_error("VLAN: called with wrong options. Problems with config!");
119
		return;
120
	}
121
	$if = $vlan['if'];
122 48315e65 Ermal Luci
	$vlanif  = empty($vlan['vlanif']) ? "{$if}_vlan{$vlan['tag']}" : $vlan['vlanif'];
123 5f1e1d26 Ermal Lu?i
	$tag = $vlan['tag'];
124
125 871768cf Ermal
	if (empty($if)) {
126 e1c449c0 Ermal Lu?i
		log_error("interface_vlan_confgure called with if undefined.");
127 3ae4960c Ermal Luçi
		return;
128
	}
129
130 37a53d16 Scott Ullrich
	/* make sure the parent interface is up */
131 07101b63 Ermal Luçi
	interfaces_bring_up($if);
132
	/* Since we are going to add vlan(4) try to enable all that hardware supports. */
133 871768cf Ermal
	pfSense_interface_capabilities($if, IFCAP_VLAN_HWTAGGING|IFCAP_VLAN_MTU|IFCAP_VLAN_HWFILTER);
134 cfc707f7 Scott Ullrich
135 4aca19b3 Scott Ullrich
	if (!empty($vlanif) && does_interface_exist($vlanif)) {
136 37a53d16 Scott Ullrich
		interface_bring_down($vlanif);
137 4aca19b3 Scott Ullrich
	} else {
138 871768cf Ermal
		$tmpvlanif = pfSense_interface_create("vlan");
139
		pfSense_interface_rename($tmpvlanif, $vlanif);
140
		pfSense_ngctl_name("{$tmpvlanif}:", $vlanif);
141 abcb2bed Ermal Lu?i
	}
142 871768cf Ermal
143
	pfSense_vlan_create($vlanif, $if, $tag);
144 2075fadb Ermal Luçi
145 07101b63 Ermal Luçi
	interfaces_bring_up($vlanif);
146 cfc707f7 Scott Ullrich
147 40b0b541 Ermal Lu?i
	/* invalidate interface cache */
148
	get_interface_arr(true);
149 3f7d2120 Bill Marquette
150 4aca19b3 Scott Ullrich
	/* XXX: ermal -- for now leave it here at the moment it does not hurt. */
151 07101b63 Ermal Luçi
	interfaces_bring_up($if);
152 cfc707f7 Scott Ullrich
153 4aca19b3 Scott Ullrich
	return $vlanif;
154 5b237745 Scott Ullrich
}
155
156 abcb2bed Ermal Lu?i
function interface_qinq_configure(&$vlan, $fd = NULL) {
157 5f1e1d26 Ermal Lu?i
        global $config, $g;
158
159 c1289cfd Ermal Lu?i
        if (!is_array($vlan)) {
160
                log_error("QinQ compat VLAN: called with wrong options. Problems with config!\n");
161 5f1e1d26 Ermal Lu?i
                return;
162
        }
163
164 42bad812 Ermal Lu?i
        $qinqif = $vlan['if'];
165 c1289cfd Ermal Lu?i
        $tag = $vlan['tag'];
166 a726c0e8 Ermal Lu?i
        if(empty($qinqif)) {
167 c1289cfd Ermal Lu?i
                log_error("interface_qinq_confgure called with if undefined.\n");
168
                return;
169
        }
170 4400ad66 Ermal Lu?i
	$vlanif = interface_vlan_configure($vlan);
171 5f1e1d26 Ermal Lu?i
172 c1289cfd Ermal Lu?i
        if ($fd == NULL) {
173
                $exec = true;
174
                $fd = fopen("{$g['tmp_path']}/netgraphcmd", "w");
175
        } else
176
                $exec = false;
177 5f1e1d26 Ermal Lu?i
        /* make sure the parent is converted to ng_vlan(4) and is up */
178 42bad812 Ermal Lu?i
        interfaces_bring_up($qinqif);
179 5f1e1d26 Ermal Lu?i
180 abcb2bed Ermal Lu?i
        if (!empty($vlanif) && does_interface_exist($vlanif)) {
181 42bad812 Ermal Lu?i
                fwrite($fd, "shutdown {$qinqif}qinq:\n");
182
                exec("/usr/sbin/ngctl msg {$qinqif}qinq: gettable", $result);
183 c1289cfd Ermal Lu?i
                if (empty($result)) {
184 42bad812 Ermal Lu?i
                        fwrite($fd, "mkpeer {$qinqif}: vlan lower downstream\n");
185 4400ad66 Ermal Lu?i
                        fwrite($fd, "name {$qinqif}:lower {$vlanif}qinq\n");
186
                        fwrite($fd, "connect {$qinqif}: {$vlanif}qinq: upper nomatch\n");
187 c1289cfd Ermal Lu?i
                }
188 5f1e1d26 Ermal Lu?i
        } else {
189 42bad812 Ermal Lu?i
                fwrite($fd, "mkpeer {$qinqif}: vlan lower downstream\n");
190 4400ad66 Ermal Lu?i
                fwrite($fd, "name {$qinqif}:lower {$vlanif}qinq\n");
191
                fwrite($fd, "connect {$qinqif}: {$vlanif}qinq: upper nomatch\n");
192 c1289cfd Ermal Lu?i
        }
193 5f1e1d26 Ermal Lu?i
194
        /* invalidate interface cache */
195
        get_interface_arr(true);
196
197 42bad812 Ermal Lu?i
        if (!stristr($qinqif, "vlan"))
198
                mwexec("/sbin/ifconfig {$qinqif} promisc\n");
199 5f1e1d26 Ermal Lu?i
200 4400ad66 Ermal Lu?i
        $macaddr = get_interface_mac($qinqif);
201 c1289cfd Ermal Lu?i
        if (!empty($vlan['members'])) {
202
                $members = explode(" ", $vlan['members']);
203
                foreach ($members as $qtag) {
204
                        $qinq = array();
205 5f1e1d26 Ermal Lu?i
                        $qinq['tag'] = $qtag;
206
                        $qinq['if'] = $vlanif;
207 c1289cfd Ermal Lu?i
                        interface_qinq2_configure($qinq, $fd, $macaddr);
208
                }
209
        }
210
        if ($exec == true) {
211
                fclose($fd);
212
                mwexec("/usr/sbin/ngctl -f {$g['tmp_path']}/netgraphcmd");
213
        }
214
215 42bad812 Ermal Lu?i
        interfaces_bring_up($qinqif);
216 c1289cfd Ermal Lu?i
        if (!empty($vlan['members'])) {
217
                $members = explode(" ", $vlan['members']);
218
                foreach ($members as $qif)
219 4400ad66 Ermal Lu?i
                        interfaces_bring_up("{$vlanif}_{$qif}");
220 c1289cfd Ermal Lu?i
        }
221 5f1e1d26 Ermal Lu?i
222
        return $vlanif;
223
}
224
225
function interfaces_qinq_configure() {
226 7a6f7c55 Scott Ullrich
	global $config, $g;
227
	if($g['booting'])
228
		echo "Configuring QinQ interfaces...";
229
	if (is_array($config['qinqs']['qinqentry']) && count($config['qinqs']['qinqentry'])) {
230
		foreach ($config['qinqs']['qinqentry'] as $qinq) {
231
			/* XXX: Maybe we should report any errors?! */
232 4400ad66 Ermal Lu?i
			interface_qinq_configure($qinq);
233 7a6f7c55 Scott Ullrich
		}
234 4400ad66 Ermal Lu?i
	}
235
	if($g['booting'])
236
		echo "done.\n";
237 5f1e1d26 Ermal Lu?i
}
238
239 abcb2bed Ermal Lu?i
function interface_qinq2_configure(&$qinq, $fd, $macaddr) {
240 c1289cfd Ermal Lu?i
        global $config, $g;
241 5f1e1d26 Ermal Lu?i
242
        if (!is_array($qinq)) {
243 c1289cfd Ermal Lu?i
                log_error("QinQ compat VLAN: called with wrong options. Problems with config!\n");
244 5f1e1d26 Ermal Lu?i
                return;
245
        }
246
247
        $if = $qinq['if'];
248
        $tag = $qinq['tag'];
249 c1289cfd Ermal Lu?i
        $vlanif = "{$if}_{$tag}";
250 5f1e1d26 Ermal Lu?i
        if(empty($if)) {
251 c1289cfd Ermal Lu?i
                log_error("interface_qinq_confgure called with if undefined.\n");
252 5f1e1d26 Ermal Lu?i
                return;
253
        }
254
255 4400ad66 Ermal Lu?i
        fwrite($fd, "shutdown {$if}h{$tag}:\n");
256 c1289cfd Ermal Lu?i
        fwrite($fd, "mkpeer {$if}qinq: eiface {$if}{$tag} ether\n");
257
        fwrite($fd, "name {$if}qinq:{$if}{$tag} {$if}h{$tag}\n");
258
        fwrite($fd, "msg {$if}qinq: addfilter { vlan={$tag} hook=\"{$if}{$tag}\" }\n");
259
        fwrite($fd, "msg {$if}h{$tag}: setifname \"{$vlanif}\"\n");
260 4400ad66 Ermal Lu?i
        fwrite($fd, "msg {$if}h{$tag}: set {$macaddr}\n");
261 5f1e1d26 Ermal Lu?i
262 c1289cfd Ermal Lu?i
        /* invalidate interface cache */
263 5f1e1d26 Ermal Lu?i
        get_interface_arr(true);
264
265
        return $vlanif;
266
}
267
268 9f428275 Erik Fonnesbeck
function interfaces_create_wireless_clones() {
269
	global $config;
270
271
	if($g['booting'])
272
		echo "Creating other wireless clone interfaces...";
273
	if (is_array($config['wireless']['clone']) && count($config['wireless']['clone'])) {
274
		foreach ($config['wireless']['clone'] as $clone) {
275
			if(empty($clone['cloneif']))
276
				continue;
277
			if(does_interface_exist($clone['cloneif']))
278
				continue;
279
			/* XXX: Maybe we should report any errors?! */
280
			if(interface_wireless_clone($clone['cloneif'], $clone))
281
				if($g['booting'])
282
					echo " " . $clone['cloneif'];
283
		}
284
	}
285
	if($g['booting'])
286
		echo " done.\n";
287
}
288
289 eba938e3 Scott Ullrich
function interfaces_bridge_configure() {
290 bad29bc6 Ermal Luçi
        global $config;
291
292
        $i = 0;
293 3134528d Ermal Luçi
        if (is_array($config['bridges']['bridged']) && count($config['bridges']['bridged'])) {
294
                foreach ($config['bridges']['bridged'] as $bridge) {
295 f620d00d Ermal Luçi
                        if(empty($bridge['bridgeif']))
296 bad29bc6 Ermal Luçi
                                $bridge['bridgeif'] = "bridge{$i}";
297
                        /* XXX: Maybe we should report any errors?! */
298
                        interface_bridge_configure($bridge);
299
                        $i++;
300
                }
301
        }
302
}
303
304 eba938e3 Scott Ullrich
function interface_bridge_configure(&$bridge) {
305 d7147b1c Scott Ullrich
	global $config, $g;
306 bad29bc6 Ermal Luçi
307 d7147b1c Scott Ullrich
	if (!is_array($bridge))
308
	        return -1;
309 bad29bc6 Ermal Luçi
310 dc97efaf Ermal Luçi
	if (empty($bridge['members'])) {
311
		log_error("No members found on {$bridge['bridgeif']}");
312
		return -1;
313
	}
314
315 bad29bc6 Ermal Luçi
	$members = explode(',', $bridge['members']);
316 70720671 Ermal Luçi
	if (!count($members))
317 bad29bc6 Ermal Luçi
		return -1;
318 ea5f6c95 Ermal
319 bad29bc6 Ermal Luçi
	$checklist = get_configured_interface_list();
320
321 fded24de Ermal Luçi
	if ($g['booting'] || !empty($bridge['bridgeif'])) {
322 871768cf Ermal
		pfSense_interface_destroy($bridge['bridgeif']);
323
		pfSense_interface_create($bridge['bridgeif']);
324 d7147b1c Scott Ullrich
		$bridgeif = $bridge['bridgeif'];
325 871768cf Ermal
	} else
326
		$bridgeif = pfSense_interface_create("bridge");
327 bad29bc6 Ermal Luçi
328 b64523c1 Ermal Luçi
	/* Calculate smaller mtu and enforce it */
329 69e53ef0 Ermal Luçi
	$smallermtu = 0;
330 b64523c1 Ermal Luçi
	foreach ($members as $member) {
331
		$realif = get_real_interface($member);
332
		$mtu = get_interface_mtu($realif);
333 69e53ef0 Ermal Luçi
		if ($smallermtu == 0 && !empty($mtu))
334
			$smallermtu = $mtu;
335
		else if (!empty($mtu) && $mtu < $smallermtu)
336 b64523c1 Ermal Luçi
			$smallermtu = $mtu;
337
	}
338
	 
339 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
340
	if ($smallermtu == 0)
341
		$smallermtu = 1500; 
342
343 bad29bc6 Ermal Luçi
	/* Add interfaces to bridge */
344 31241000 Ermal Luçi
	foreach ($members as $member) {
345 d7147b1c Scott Ullrich
		if (!array_key_exists($member, $checklist))
346
			continue;
347 9ecce49f Ermal Lu?i
		$realif1 = get_real_interface($member);
348
		$realif =  escapeshellarg($realif1);
349 d7147b1c Scott Ullrich
		/* make sure the parent interface is up */
350 871768cf Ermal
		pfSense_interface_mtu($realif, $smallermtu);
351 d7147b1c Scott Ullrich
		if(!$realif) 
352
			log_error("realif not defined in interfaces bridge - up");
353 9ecce49f Ermal Lu?i
		interfaces_bring_up($realif1);
354 31241000 Ermal Luçi
		mwexec("/sbin/ifconfig {$bridgeif} addm {$realif}");	
355 d7147b1c Scott Ullrich
	}
356 31241000 Ermal Luçi
357 bad29bc6 Ermal Luçi
	if (isset($bridge['enablestp'])) {
358
		/* Choose spanning tree proto */
359
		mwexec("/sbin/ifconfig {$bridgeif} proto {$bridge['proto']}");	
360
		
361 dc97efaf Ermal Luçi
		if (!empty($bridge['stp'])) {
362
			$stpifs = explode(',', $bridge['stp']);
363
			foreach ($stpifs as $stpif) {
364
				$realif = get_real_interface($stpif);
365
				mwexec("/sbin/ifconfig {$bridgeif} stp {$realif}");
366
			}
367 bad29bc6 Ermal Luçi
		}
368 dc97efaf Ermal Luçi
		if (!empty($bridge['maxage']))
369 bad29bc6 Ermal Luçi
			mwexec("/sbin/ifconfig {$bridgeif} maxage {$bridge['maxage']}");
370 dc97efaf Ermal Luçi
		if (!empty($brige['fwdelay']))
371 bad29bc6 Ermal Luçi
			mwexec("/sbin/ifconfig {$bridgeif} fwddelay {$bridge['fwdelay']}");
372 dc97efaf Ermal Luçi
		if (!empty($brige['hellotime']))
373 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} hellotime {$bridge['hellotime']}");
374 dc97efaf Ermal Luçi
		if (!empty($brige['priority']))
375 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} priority {$bridge['priority']}");
376 dc97efaf Ermal Luçi
		if (!empty($brige['holdcount']))
377 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} holdcnt {$bridge['holdcnt']}");
378 dc97efaf Ermal Luçi
		if (!empty($bridge['ifpriority'])) {
379
			$pconfig = explode(",", $bridge['ifpriority']);
380
			$ifpriority = array();
381
			foreach ($pconfig as $cfg) {
382
				$embcfg = explode(":", $cfg);
383
				foreach ($embcfg as $key => $value)
384
					$ifpriority[$key] = $value;
385
			}
386
			foreach ($ifpriority as $key => $value) {
387
				$realif = get_real_interface($key);
388
				mwexec("/sbin/ifconfig ${bridgeif} ifpriority {$realif} {$value}"); 
389
			}
390 bad29bc6 Ermal Luçi
		}
391 dc97efaf Ermal Luçi
		if (!empty($bridge['ifpathcost'])) {
392
			$pconfig = explode(",", $bridges['ifpathcost']);
393
			$ifpathcost = array();
394
			foreach ($pconfig as $cfg) {
395
				$embcfg = explode(":", $cfg);
396
				foreach ($embcfg as $key => $value)
397
					$ifpathcost[$key] = $value;
398
			}
399
			foreach ($ifpathcost as $key => $value) {
400
                        	$realif = get_real_interface($key);
401
                        	mwexec("/sbin/ifconfig ${bridgeif} ifpathcost {$realif} {$value}");
402
                	}
403 bad29bc6 Ermal Luçi
		}
404
	}
405
406
	if ($bridge['maxaddr'] <> "")
407
		mwexec("/sbin/ifconfig {$bridgeif} maxaddr {$bridge['maxaddr']}");
408
        if ($bridge['timeout'] <> "")
409
                mwexec("/sbin/ifconfig {$bridgeif} timeout {$bridge['timeout']}");
410
        if ($bridge['span'] <> "") {
411 85a5da13 Ermal Luçi
		$realif = get_real_interface($bridge['span']);
412 bad29bc6 Ermal Luçi
                mwexec("/sbin/ifconfig {$bridgeif} span {$realif}");
413
	}
414 a47a5798 Ermal Luçi
	if (!empty($bridge['edge'])) {
415
        	$edgeifs = explode(',', $bridge['edge']);
416
        	foreach ($edgeifs as $edgeif) {
417
			$realif = get_real_interface($edgeif);
418
                	mwexec("/sbin/ifconfig {$bridgeif} edge {$realif}");
419
        	}
420
	}
421
	if (!empty($bridge['autoedge'])) {
422
        	$edgeifs = explode(',', $bridge['autoedge']);
423
        	foreach ($edgeifs as $edgeif) {
424
                	$realif = get_real_interface($edgeif);
425
                	mwexec("/sbin/ifconfig {$bridgeif} -autoedge {$realif}");
426
        	}
427
	}
428
	if (!empty($bridge['ptp'])) {
429
        	$ptpifs = explode(',', $bridge['ptp']);
430
        	foreach ($ptpifs as $ptpif) {
431
                	$realif = get_real_interface($ptpif);
432
                	mwexec("/sbin/ifconfig {$bridgeif} ptp {$realif}");
433
        	}
434
	}
435
	if (!empty($bridge['autoptp'])) {
436
        	$ptpifs = explode(',', $bridge['autoptp']);
437
        	foreach ($ptpifs as $ptpif) {
438
                	$realif = get_real_interface($ptpif);
439
                	mwexec("/sbin/ifconfig {$bridgeif} -autoptp {$realif}");
440
        	}
441
	}
442
	if (!empty($bridge['static'])) {
443
        	$stickyifs = explode(',', $bridge['static']);
444
        	foreach ($stickyifs as $stickyif) {
445
                	$realif = get_real_interface($stickyif);
446
                	mwexec("/sbin/ifconfig {$bridgeif} sticky {$realif}");
447
        	}
448
	}
449
	if (!empty($bridge['private'])) {
450
        	$privateifs = explode(',', $bridge['private']);
451
        	foreach ($privateifs as $privateif) {
452
                	$realif = get_real_interface($privateif);
453
               	 	mwexec("/sbin/ifconfig {$bridgeif} private {$realif}");
454
        	}
455
	}
456 bad29bc6 Ermal Luçi
457 d7147b1c Scott Ullrich
	if($bridgeif)
458 b5b957fe Scott Ullrich
		interfaces_bring_up($bridgeif);	
459 d7147b1c Scott Ullrich
	else 
460
		log_error("bridgeif not defined -- could not bring interface up");
461 bad29bc6 Ermal Luçi
462 d7147b1c Scott Ullrich
	return $bridgeif;
463 bad29bc6 Ermal Luçi
}
464
465 fcd4a425 Ermal Lu?i
function interface_bridge_add_member($bridgeif, $interface) {
466
467
	if (!does_interface_exist($bridgeif) || !does_interface_exist($interface))
468
		return;
469
470
	$mtu = get_interface_mtu($brigeif);
471
	$mtum = get_interface_mtu($interface);
472
	
473
	if ($mtu != $mtum)
474 871768cf Ermal
		pfSense_interface_mtu($interface, $mtu);
475 fcd4a425 Ermal Lu?i
476 ea5f6c95 Ermal
	pfSense_get_interface_addresses($bridgeif);
477
	if (isset($options['encaps']['txcsum']))
478
		pfSense_interface_capabilities($interface, -IFCAP_TXCSUM);
479
	else
480
		pfSense_interface_capabilities($interface, IFCAP_TXCSUM);
481
482 fcd4a425 Ermal Lu?i
	interfaces_bring_up($interface);
483
	mwexec("/sbin/ifconfig {$bridgeif} addm {$interface}");
484
}
485
486 f620d00d Ermal Luçi
function interfaces_lagg_configure() 
487
{
488 7a6f7c55 Scott Ullrich
        global $config, $g;
489
		if($g['booting']) 
490
			echo "Configuring LAGG interfaces...";
491 cccf624b Ermal Luçi
        $i = 0;
492 7a6f7c55 Scott Ullrich
		if (is_array($config['laggs']['lagg']) && count($config['laggs']['lagg'])) {
493
			foreach ($config['laggs']['lagg'] as $lagg) {
494
				if(empty($lagg['laggif']))
495
					$lagg['laggif'] = "lagg{$i}";
496
				/* XXX: Maybe we should report any errors?! */
497
				interface_lagg_configure($lagg);
498
				$i++;
499
			}
500
		}
501
		if($g['booting']) 
502
			echo "done.\n";
503 cccf624b Ermal Luçi
}
504
505 eba938e3 Scott Ullrich
function interface_lagg_configure(&$lagg) {
506 cccf624b Ermal Luçi
        global $config, $g;
507
508
        if (!is_array($lagg))
509
		return -1;
510
511
	$members = explode(',', $lagg['members']);
512
	if (!count($members))
513
		return -1;
514
	
515 fe281019 Ermal Luçi
	$checklist = get_interface_list();
516 cccf624b Ermal Luçi
517 b64523c1 Ermal Luçi
	if ($g['booting'] || !(empty($lagg['laggif']))) {
518 871768cf Ermal
		pfSense_interface_destroy($lagg['laggif']);
519
		pfSense_interface_create($lagg['laggif']);
520 b64523c1 Ermal Luçi
                $laggif = $lagg['laggif'];
521
        } else
522 871768cf Ermal
		$laggif = pfSense_interface_create("lagg");
523 b64523c1 Ermal Luçi
524
	/* Calculate smaller mtu and enforce it */
525 69e53ef0 Ermal Luçi
        $smallermtu = 0;
526 b64523c1 Ermal Luçi
        foreach ($members as $member) {
527 39fbee97 Ermal Lu?i
                $mtu = get_interface_mtu($member);
528 69e53ef0 Ermal Luçi
		if ($smallermtu == 0 && !empty($mtu))
529
			$smallermtu = $mtu;
530
                else if (!empty($mtu) && $mtu < $smallermtu)
531 b64523c1 Ermal Luçi
                        $smallermtu = $mtu;
532
        }
533
534 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
535
        if ($smallermtu == 0)
536
                $smallermtu = 1500;
537
538 cccf624b Ermal Luçi
	foreach ($members as $member) {
539
		if (!array_key_exists($member, $checklist))
540
			continue;
541 d7147b1c Scott Ullrich
		/* make sure the parent interface is up */
542 871768cf Ermal
		pfSense_interface_mtu($member, $smallermtu);
543 39fbee97 Ermal Lu?i
		interfaces_bring_up($member);
544 f421cbcc Ermal Lu?i
		mwexec("/sbin/ifconfig {$laggif} laggport {$member}");
545 cccf624b Ermal Luçi
	}
546 b5b957fe Scott Ullrich
	
547 39fbee97 Ermal Lu?i
	mwexec("/sbin/ifconfig {$laggif} laggproto {$lagg['proto']}");
548 acc1e9d0 Scott Ullrich
549 b5b957fe Scott Ullrich
	interfaces_bring_up($laggif);
550 cccf624b Ermal Luçi
551 d7147b1c Scott Ullrich
	return $laggif;
552 cccf624b Ermal Luçi
}
553
554 eba938e3 Scott Ullrich
function interfaces_gre_configure() {
555 582d2452 Ermal Luçi
        global $config;
556
557
        $i = 0;
558
        if (is_array($config['gres']['gre']) && count($config['gres']['gre'])) {
559
                foreach ($config['gres']['gre'] as $gre) {
560 f620d00d Ermal Luçi
                        if(empty($gre['greif']))
561 582d2452 Ermal Luçi
                                $gre['greif'] = "gre{$i}";
562
                        /* XXX: Maybe we should report any errors?! */
563
                        interface_gre_configure($gre);
564
                        $i++;
565
                }
566
        }
567
}
568
569 eba938e3 Scott Ullrich
function interface_gre_configure(&$gre) {
570 582d2452 Ermal Luçi
        global $config, $g;
571
572
	if (!is_array($gre))
573
		return -1;
574
575 85a5da13 Ermal Luçi
	$realif = get_real_interface($gre['if']);
576
	$realifip = get_interface_ip($gre['if']);
577 582d2452 Ermal Luçi
578 d7147b1c Scott Ullrich
	/* make sure the parent interface is up */
579 b5b957fe Scott Ullrich
	interfaces_bring_up($realif);
580 582d2452 Ermal Luçi
581 d7147b1c Scott Ullrich
	if ($g['booting'] || !(empty($gre['greif']))) {
582 871768cf Ermal
		pfSense_interface_destroy($gre['greif']);
583
		pfSense_interface_create($gre['greif']);
584 582d2452 Ermal Luçi
		$greif = $gre['greif'];
585 871768cf Ermal
	} else
586
		$greif = pfSense_interface_create("gre");
587 582d2452 Ermal Luçi
588
	/* Do not change the order here for more see gre(4) NOTES section. */
589
	mwexec("/sbin/ifconfig {$greif} tunnel {$realifip} {$gre['remote-addr']}");
590 bd33ee57 Ermal Luçi
	mwexec("/sbin/ifconfig {$greif} {$gre['tunnel-local-addr']} {$gre['tunnel-remote-addr']} netmask " . gen_subnet_mask($gre['tunnel-remote-net']));
591 582d2452 Ermal Luçi
	if (isset($gre['link0']) && $gre['link0'])
592 871768cf Ermal
		pfSense_interface_flags($greif, IFF_LINK0);
593 d7147b1c Scott Ullrich
	if (isset($gre['link1']) && $gre['link1'])
594 871768cf Ermal
		pfSense_interface_flags($greif, IFF_LINK1);
595 d7147b1c Scott Ullrich
	if (isset($gre['link2']) && $gre['link2'])
596 871768cf Ermal
		pfSense_interface_flags($greif, IFF_LINK2);
597 d7147b1c Scott Ullrich
598
	if($greif)
599 b5b957fe Scott Ullrich
		interfaces_bring_up($greif);
600 d7147b1c Scott Ullrich
	else 
601
		log_error("Could not bring greif up -- variable not defined.");
602 582d2452 Ermal Luçi
603 53b0d9d3 Ermal Lu?i
	if (isset($gre['link1']) && $gre['link1'])
604 61b67ab3 Ermal Lu?i
		mwexec("/sbin/route add {$gre['tunnel-remote-addr']}/{$gre['tunnel-remote-net']} {$gre['tunnel-local-addr']}");
605 da179181 Administrator
	file_put_contents("{$g['tmp_path']}/{$greif}_router", $gre['tunnel-remote-addr']);
606 582d2452 Ermal Luçi
607
	return $greif;
608
}
609
610 eba938e3 Scott Ullrich
function interfaces_gif_configure() {
611 9006e9f8 Scott Ullrich
	global $config;
612
	$i = 0;
613
	if (is_array($config['gifs']['gif']) && count($config['gifs']['gif'])) {
614
		foreach ($config['gifs']['gif'] as $gif) {
615
			if(empty($gif['gifif']))
616
				$gre['gifif'] = "gif{$i}";
617
			/* XXX: Maybe we should report any errors?! */
618
			interface_gif_configure($gif);
619
			$i++;
620
		}
621
	}
622 582d2452 Ermal Luçi
}
623
624 eba938e3 Scott Ullrich
function interface_gif_configure(&$gif) {
625 9006e9f8 Scott Ullrich
	global $config, $g;
626 582d2452 Ermal Luçi
627 9006e9f8 Scott Ullrich
	if (!is_array($gif))
628
		return -1;
629 582d2452 Ermal Luçi
630 9006e9f8 Scott Ullrich
	$realif = get_real_interface($gif['if']);
631
	$realifip = get_interface_ip($gif['if']);
632 582d2452 Ermal Luçi
633 9006e9f8 Scott Ullrich
	/* make sure the parent interface is up */
634
	if($realif)
635
		interfaces_bring_up($realif);
636
	else 
637
		log_error("could not bring realif up -- variable not defined -- interface_gif_configure()");
638 582d2452 Ermal Luçi
639 9006e9f8 Scott Ullrich
	if ($g['booting'] || !(empty($gif['gifif']))) {
640 871768cf Ermal
		pfSense_interface_destroy($gif['gifif']);
641
		pfSense_interface_create($gif['gifif']);
642 9006e9f8 Scott Ullrich
		$gifif = $gif['gifif'];
643
	} else
644 871768cf Ermal
		$gifif = pfSense_interface_create("gif");
645 9006e9f8 Scott Ullrich
646
	/* Do not change the order here for more see gif(4) NOTES section. */
647
	mwexec("/sbin/ifconfig {$gifif} tunnel {$realifip} {$gif['remote-addr']}");
648
	mwexec("/sbin/ifconfig {$gifif} {$gif['tunnel-local-addr']} {$gif['tunnel-remote-addr']} netmask " . gen_subnet_mask($gif['tunnel-remote-net']));
649
	if (isset($gif['link0']) && $gif['link0'])
650 871768cf Ermal
		pfSense_interface_flags($gifif, IFF_LINK0);
651 9006e9f8 Scott Ullrich
	if (isset($gif['link1']) && $gif['link1'])
652 871768cf Ermal
		pfSense_interface_flags($gifif, IFF_LINK1);
653 9006e9f8 Scott Ullrich
	if($gifif)
654
		interfaces_bring_up($gifif);
655
	else
656
		log_error("could not bring gifif up -- variable not defined");
657
658 53b0d9d3 Ermal Lu?i
	/* XXX: Needed?! */
659
	//mwexec("/sbin/route add {$gif['tunnel-remote-addr']}/{$gif['tunnel-remote-net']} -iface {$gifif}");
660 61b67ab3 Ermal Lu?i
	file_put_contents("{$g['tmp_path']}/{$gifif}_router", $gif['tunnel-remote-addr']);
661 582d2452 Ermal Luçi
662 9006e9f8 Scott Ullrich
	return $gifif;
663 582d2452 Ermal Luçi
}
664
665 eba938e3 Scott Ullrich
function interfaces_configure() {
666 9b1c39e3 Ermal Luçi
	global $config, $g;
667
668 a5d6f60b Ermal Lu?i
	/* Set up our loopback interface */
669 4aca19b3 Scott Ullrich
	interfaces_loopback_configure();
670 a5d6f60b Ermal Lu?i
671 541b7c56 Scott Ullrich
	/* set up LAGG virtual interfaces */
672
	interfaces_lagg_configure();
673
674 acc1e9d0 Scott Ullrich
	/* set up VLAN virtual interfaces */
675
	interfaces_vlan_configure();
676
677 5f1e1d26 Ermal Lu?i
	interfaces_qinq_configure();
678
679 67ee1ec5 Ermal Luçi
	$iflist = get_configured_interface_with_descr();
680 9b1c39e3 Ermal Luçi
	$delayed_list = array();
681
	$bridge_list = array();
682 b6db9217 Ermal Luçi
	
683 871768cf Ermal
	/* This is needed to speedup interfaces on bootup. */
684
	$reload = false;
685
	if ($g['booting'])
686
		$reload = true;
687
688 67ee1ec5 Ermal Luçi
	foreach($iflist as $if => $ifname) {
689 0dc702f3 Ermal Lu?i
		$realif = $config['interfaces'][$if]['if'];
690 9b1c39e3 Ermal Luçi
		if (strstr($realif, "bridge")) 
691
			$bridge_list[$if] = $ifname;
692
		else if (strstr($realif, "gre"))
693
			$delayed_list[$if] = $ifname;
694
		else if (strstr($realif, "gif"))
695
			$delayed_list[$if] = $ifname;
696
		else {
697
			if ($g['booting'])
698 d7147b1c Scott Ullrich
				echo "Configuring {$ifname} interface...";
699 9006e9f8 Scott Ullrich
			if($g['debug'])
700 d7147b1c Scott Ullrich
				log_error("Configuring {$ifname}");
701 871768cf Ermal
			interface_configure($if, $reload);
702 9b1c39e3 Ermal Luçi
			if ($g['booting']) 
703 53c82ef9 Scott Ullrich
				echo "done.\n";
704 9b1c39e3 Ermal Luçi
		}
705
	}
706
707 9f428275 Erik Fonnesbeck
	/* create the unconfigured wireless clones */
708
	interfaces_create_wireless_clones();
709
710 d7147b1c Scott Ullrich
	/* set up GRE virtual interfaces */
711
	interfaces_gre_configure();
712 9b1c39e3 Ermal Luçi
713 d7147b1c Scott Ullrich
	/* set up GIF virtual interfaces */
714
	interfaces_gif_configure();
715 9b1c39e3 Ermal Luçi
	
716
	foreach ($delayed_list as $if => $ifname) {
717
		if ($g['booting'])
718 d7147b1c Scott Ullrich
			echo "Configuring {$ifname} interface...";
719 a5d6f60b Ermal Lu?i
        	if ($g['debug'])
720
        		log_error("Configuring {$ifname}");
721 67ee1ec5 Ermal Luçi
722 871768cf Ermal
		interface_configure($if, $reload);
723 4476d447 Ermal Luçi
724 9b1c39e3 Ermal Luçi
		if ($g['booting'])
725
			echo "done.\n";
726 67ee1ec5 Ermal Luçi
	}
727 cfc707f7 Scott Ullrich
728 d7147b1c Scott Ullrich
	/* set up BRIDGe virtual interfaces */
729
	interfaces_bridge_configure();
730 9b1c39e3 Ermal Luçi
731 d7147b1c Scott Ullrich
	foreach ($bridge_list as $if => $ifname) {
732
		if ($g['booting'])
733
			echo "Configuring {$ifname} interface...";
734
		if($g['debug'])
735
			log_error("Configuring {$ifname}");
736 9b1c39e3 Ermal Luçi
737 871768cf Ermal
		interface_configure($if, $reload);
738 9b1c39e3 Ermal Luçi
739 d7147b1c Scott Ullrich
		if ($g['booting'])
740
			echo "done.\n";
741
	}
742 9b1c39e3 Ermal Luçi
743 abcb2bed Ermal Lu?i
	/* bring up vip interfaces */
744
	interfaces_vips_configure();
745 9b1c39e3 Ermal Luçi
746 42753d25 Ermal Lu?i
	/* configure interface groups */
747
	interfaces_group_setup();
748
749 5b237745 Scott Ullrich
	if (!$g['booting']) {
750
		/* reconfigure static routes (kernel may have deleted them) */
751
		system_routing_configure();
752 cfc707f7 Scott Ullrich
753 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
754
		vpn_ipsec_configure();
755 cfc707f7 Scott Ullrich
756 f620d00d Ermal Luçi
		/* reload dhcpd (interface enabled/disabled status may have changed) */
757 5b237745 Scott Ullrich
		services_dhcpd_configure();
758 cfc707f7 Scott Ullrich
759 5b237745 Scott Ullrich
		/* restart dnsmasq */
760
		services_dnsmasq_configure();
761 4d18de6a Scott Ullrich
762 c597d50f Scott Ullrich
		/* reload captive portal */
763 769e254e Ermal
		captiveportal_init_rules();
764 c597d50f Scott Ullrich
765 4d18de6a Scott Ullrich
		/* set the reload filter dity flag */
766 be38535c Ermal Luçi
		filter_configure();
767 5b237745 Scott Ullrich
	}
768 cfc707f7 Scott Ullrich
769 5b237745 Scott Ullrich
	return 0;
770
}
771
772 eba938e3 Scott Ullrich
function interface_reconfigure($interface = "wan") {
773 80bf3f4a Ermal Luçi
	interface_bring_down($interface);
774 871768cf Ermal
	interface_configure($interface, true);
775 80bf3f4a Ermal Luçi
}
776
777 abcb2bed Ermal Lu?i
function interface_vip_bring_down(&$vip) {
778 962fd685 Ermal
	global $g;
779
780 abcb2bed Ermal Lu?i
	switch ($vip['mode']) {
781
	case "proxyarp":
782 962fd685 Ermal
		$vipif = get_real_interface($vip['interface']);
783 ca942829 Ermal
		if (file_exists("{$g['varrun_path']}/choparp_{$vipif}.pid"))
784
			killbypid("{$g['varrun_path']}/choparp_{$vipif}.pid");
785 abcb2bed Ermal Lu?i
		break;
786
	case "ipalias":
787 435f11c8 Ermal Lu?i
		$vipif = get_real_interface($vip['interface']);
788
		if(does_interface_exist($vipif))
789 871768cf Ermal
			pfSense_interface_deladdress($vipif, $vip['subnet']);
790 abcb2bed Ermal Lu?i
		break;
791
	case "carp":
792 12fafaf7 Chris Buechler
		$vipif = "vip" . $vip['vhid'];
793 cb58f26c Ermal Lu?i
		if(does_interface_exist($vipif)) 
794 871768cf Ermal
			pfSense_interface_destroy($vipif);
795 12fafaf7 Chris Buechler
		break;
796 abcb2bed Ermal Lu?i
	case "carpdev-dhcp":
797 9e01d6eb Scott Ullrich
		$vipif = "vip" . $vip['vhid'];
798 cb58f26c Ermal Lu?i
		if(does_interface_exist($vipif)) 
799 871768cf Ermal
			pfSense_interface_destroy($vipif);
800 abcb2bed Ermal Lu?i
		break;
801
	}
802
}
803
804 97973ed8 Ermal Luçi
function interface_bring_down($interface = "wan", $destroy = false) {
805 80bf3f4a Ermal Luçi
	global $config, $g;
806
807 99c2a28b Ermal Luçi
	if (!isset($config['interfaces'][$interface]))
808
		return; 
809
810 80bf3f4a Ermal Luçi
	$ifcfg = $config['interfaces'][$interface];
811
812 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
813 80bf3f4a Ermal Luçi
814
	switch ($ifcfg['ipaddr']) {
815 0810c115 gnhb
	case "ppp":
816 80bf3f4a Ermal Luçi
	case "pppoe":
817
	case "pptp":
818 39f750b5 gnhb
	case "l2tp":
819 a138f4fb Ermal
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
820
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
821
				if ($realif == $ppp['if']) {
822 64e6490a Ermal
					killbypid("{$g['varrun_path']}/{$ifcfg['ipaddr']}_{$interface}.pid");
823
					sleep(2);
824
					unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
825 5c49cf58 Ermal
					if (isset($ppp['ondemand']) && !$destroy) {
826 64e6490a Ermal
						interface_configure("wan");
827 a138f4fb Ermal
					}
828
					break;
829
				}
830
			}
831
		}
832 80bf3f4a Ermal Luçi
		break;
833
	case "carpdev-dhcp":
834
		/* 
835
		 * NB: When carpdev gets enabled it would be better to be handled as all
836 37a53d16 Scott Ullrich
		 *	   other interfaces! 
837 80bf3f4a Ermal Luçi
		 */
838
	case "dhcp":
839 5d478ecc Ermal Lu?i
		$pid = find_dhclient_process($realif);
840 f07bee94 Scott Ullrich
		if($pid)
841
			mwexec("kill {$pid}");
842
		sleep(1);
843
		unlink_if_exists("{$g['varetc_path']}/dhclient_{$interface}.conf");
844
		if(does_interface_exist("$realif")) {
845 aef6d76f Seth Mos
			mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
846 871768cf Ermal
			pfSense_interface_flags($realif, -IFF_UP);
847 5630c91c Ermal Lu?i
			mwexec("/usr/sbin/arp -d -i {$realif} -a");
848 f07bee94 Scott Ullrich
		}
849 80bf3f4a Ermal Luçi
		break;
850
	default:
851 f07bee94 Scott Ullrich
		if(does_interface_exist("$realif")) {
852 aef6d76f Seth Mos
			mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
853 871768cf Ermal
			pfSense_interface_flags($realif, -IFF_UP);
854 5630c91c Ermal Lu?i
			mwexec("/usr/sbin/arp -d -i {$realif} -a");
855 f07bee94 Scott Ullrich
		}
856 80bf3f4a Ermal Luçi
		break;
857
	}
858 eb772abd Scott Ullrich
859 73ee49f2 gnhb
	/* remove interface up file if it exists */
860
	unlink_if_exists("{$g['tmp_path']}/{$realif}up");
861
	unlink_if_exists("{$g['vardb_path']}/{$interface}ip");
862
	unlink_if_exists("{$g['tmp_path']}/{$realif}_router");
863
	
864 b5582f49 Erik Fonnesbeck
	/* hostapd and wpa_supplicant do not need to be running when the interface is down.
865
	 * They will also use 100% CPU if running after the wireless clone gets deleted. */
866
	if (is_array($ifcfg['wireless'])) {
867
		mwexec(kill_hostapd($realif));
868
		mwexec(kill_wpasupplicant($realif));
869
	}
870
871 97973ed8 Ermal Luçi
	if ($destroy == true) {
872 bd414316 jim-p
		if (preg_match("/^vip|^tun|^ovpn|^gif|^gre|^lagg|^bridge|vlan/i", $realif))
873 871768cf Ermal
			pfSense_interface_destroy($realif);
874 f07bee94 Scott Ullrich
	}	
875 9006e9f8 Scott Ullrich
876 80bf3f4a Ermal Luçi
	return;
877 5b237745 Scott Ullrich
}
878
879 e5d558bf gnhb
function interfaces_ptpid_used($ptpid) {
880
	global $config;
881
882
	if (is_array($config['ppps']['ppp']))
883
		foreach ($config['ppps']['ppp'] as & $settings)
884
			if ($ptpid == $settings['ptpid'])
885
				return true;
886
887
	return false;
888
}
889
890
function interfaces_ptpid_next() {
891
892
	$ptpid = 0;
893
	while(interfaces_ptpid_used($ptpid))
894
		$ptpid++;
895
896
	return $ptpid;
897
}
898
899
function getMPDCRONSettings($pppif_) {
900
	global $config;
901 1d7e1d6c gnhb
	$cron_cmd_file = "{$g['varetc_path']}/pppoe_restart_";
902 e5d558bf gnhb
	if (is_array($config['cron']['item'])) {
903
		for ($i = 0; $i < count($config['cron']['item']); $i++) {
904
			$item = $config['cron']['item'][$i];
905 1d7e1d6c gnhb
			if (strpos($item['command'], $cron_cmd_file.$pppif_) !== false) {
906 e5d558bf gnhb
				return array("ID" => $i, "ITEM" => $item);
907
			}
908
		}
909
	}
910
	return NULL;
911
}
912
913
function handle_pppoe_reset($post_array) {
914
	global $config, $g;
915
916 5c8e8a17 gnhb
	$cron_cmd_file = "{$g['varetc_path']}/pppoe_restart_";
917
918 e5d558bf gnhb
	$pppif = $post_array['type'].$post_array['ptpid'];
919
	if (!is_array($config['cron']['item'])) 
920
		$config['cron']['item'] = array(); 
921 1d7e1d6c gnhb
	$itemhash = getMPDCRONSettings($pppif);
922 e5d558bf gnhb
	$item = $itemhash['ITEM'];
923
	
924
	// reset cron items if necessary and return
925
	if (empty($post_array['pppoe-reset-type'])) {
926
		if (isset($item))
927
			unset($config['cron']['item'][$itemhash['ID']]);
928
		sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP");
929
		return;
930
	}
931
932
	if (empty($item)) 
933
		$item = array();
934
	if (isset($post_array['pppoe-reset-type']) && $post_array['pppoe-reset-type'] == "custom") {
935
		$item['minute'] = $post_array['pppoe_resetminute'];
936
		$item['hour'] = $post_array['pppoe_resethour'];
937
		if (isset($post_array['pppoe_resetdate']) && $post_array['pppoe_resetdate'] <> "") {
938
			$date = explode("/", $post_array['pppoe_resetdate']);
939
			$item['mday'] = $date[1];
940
			$item['month'] = $date[0];
941
		} else {
942
			$item['mday'] = "*";
943
			$item['month'] = "*";
944
		}
945
		$item['wday'] = "*";
946
		$item['who'] = "root";
947 5c8e8a17 gnhb
		$item['command'] = $cron_cmd_file.$pppif;
948 e5d558bf gnhb
	} else if (isset($post_array['pppoe-reset-type']) && $post_array['pppoe-reset-type'] == "preset") {
949
		switch ($post_array['pppoe_pr_preset_val']) {
950
			case "monthly":
951
				$item['minute'] = "0";
952
				$item['hour'] = "0";
953
				$item['mday'] = "1";
954
				$item['month'] = "*";
955
				$item['wday'] = "*";
956
				$item['who'] = "root";
957 5c8e8a17 gnhb
				$item['command'] = $cron_cmd_file.$pppif;
958 e5d558bf gnhb
				break;
959
	        case "weekly":
960
				$item['minute'] = "0";
961
				$item['hour'] = "0";
962
				$item['mday'] = "*";
963
				$item['month'] = "*";
964
				$item['wday'] = "0";
965
				$item['who'] = "root";
966 5c8e8a17 gnhb
				$item['command'] = $cron_cmd_file.$pppif;
967 e5d558bf gnhb
				break;
968
			case "daily":
969
				$item['minute'] = "0";
970
				$item['hour'] = "0";
971
				$item['mday'] = "*";
972
				$item['month'] = "*";
973
				$item['wday'] = "*";
974
				$item['who'] = "root";
975 5c8e8a17 gnhb
				$item['command'] = $cron_cmd_file.$pppif;
976 e5d558bf gnhb
				break;
977
			case "hourly":
978
				$item['minute'] = "0";
979
				$item['hour'] = "*";
980
				$item['mday'] = "*";
981
				$item['month'] = "*";
982
				$item['wday'] = "*";
983
				$item['who'] = "root";
984 5c8e8a17 gnhb
				$item['command'] = $cron_cmd_file.$pppif;
985 e5d558bf gnhb
				break;
986
		} // end switch
987 5c8e8a17 gnhb
	} else {
988
		/* test whether a cron item exists and unset() it if necessary */
989 1d7e1d6c gnhb
		$itemhash = getMPDCRONSettings($pppif);
990 5c8e8a17 gnhb
		$item = $itemhash['ITEM'];
991
		if (isset($item))
992
			unset($config['cron']['item'][$itemhash['ID']]); 
993 e5d558bf gnhb
	}// end if
994
	if (isset($itemhash['ID'])) 
995
		$config['cron']['item'][$itemhash['ID']] = $item;
996
	else 
997
		$config['cron']['item'][] = $item;
998
}
999
1000 cb37d8fa gnhb
/*	This function can configure PPPoE, MLPPP (PPPoE), PPtP.
1001 8256f324 gnhb
*	It writes the mpd config file to /var/etc every time the link is opened.
1002 cb37d8fa gnhb
*/
1003
1004
function interface_ppps_configure($interface) {
1005
	global $config, $g;
1006
	
1007 3a906378 gnhb
	// mpd5 requires a /var/spool/lock directory for PPP modem links.
1008
	if(!is_dir("/var/spool/lock")) {
1009
		exec("/bin/mkdir -p /var/spool/lock");
1010
		exec("/bin/chmod a+rw /var/spool/lock/.");
1011
	}
1012
	// mpd5 modem chat script expected in the same directory as the mpd_xxx.conf files	
1013
	if (!file_exists("{$g['varetc_path']}/mpd.script"))
1014
		mwexec("/bin/ln -s /usr/local/sbin/mpd.script {$g['varetc_path']}/.");
1015
		
1016
	$ifcfg = &$config['interfaces'][$interface];
1017 0bca02ca Ermal
	if (!isset($ifcfg['enable']))
1018
		return 0;
1019 cb37d8fa gnhb
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1020
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1021 f7480829 gnhb
			if ($ifcfg['if'] == $ppp['if'])
1022 cb37d8fa gnhb
				break;
1023
		}
1024
	}
1025 f7480829 gnhb
	if (!$ppp || $ifcfg['if'] != $ppp['if']){
1026
		log_error("Can't find PPP config for {$ifcfg['if']} in interface_ppps_configure().");
1027 3a906378 gnhb
		return 0;
1028 cb37d8fa gnhb
	}
1029 3a906378 gnhb
	$pppif = $ifcfg['if'];
1030 cb37d8fa gnhb
	if ($ppp['type'] == "ppp")
1031
		$type = "modem";
1032
	else
1033
		$type = $ppp['type'];
1034 3a906378 gnhb
	$upper_type = strtoupper($ppp['type']);	
1035 cb37d8fa gnhb
	
1036 3a906378 gnhb
	if($g['booting']) {
1037 bfbb9bc0 Ermal
		$descr = isset($ifcfg['descr']) ? $ifcfg['descr'] : strtoupper($interface);
1038
		echo " configuring {$upper_type} on {$descr} interface...\n";
1039 3a906378 gnhb
		// Do not re-configure the interface if we are booting and it's already been started
1040
		if(file_exists("{$g['varrun_path']}/{$ppp['type']}_{$interface}.pid"))
1041
			return 0;
1042
	}
1043
	
1044
	$ports = explode(',',$ppp['ports']);
1045 bfbb9bc0 Ermal
	if ($type != "modem") {
1046
		foreach ($ports as $pid => $port)
1047
			$ports[$pid] = get_real_interface($port);
1048
	}
1049 3a906378 gnhb
	$localips = explode(',',$ppp['localip']);
1050
	$gateways = explode(',',$ppp['gateway']);
1051
	$subnets = explode(',',$ppp['subnet']);
1052
	
1053
	/* We bring up the parent interface first because if DHCP is configured on the parent we need
1054
	to obtain an address first so we can write it in the mpd .conf file for PPtP and L2tP configs
1055
	*/
1056
	foreach($ports as $pid => $port){
1057 23721285 gnhb
		switch ($ppp['type']) {
1058 3a906378 gnhb
			case "pppoe": 
1059
				/* Bring the parent interface up */
1060
				interfaces_bring_up($port);
1061
				break;
1062
			case "pptp":
1063
			case "l2tp":
1064
				/* configure interface */
1065 69c1b043 gnhb
				if(is_ipaddr($localips[$pid])){
1066 3a906378 gnhb
					// Manually configure interface IP/subnet
1067 bfbb9bc0 Ermal
					pfSense_interface_setaddress($port, "{$localips[$pid]}/{$subnets[$pid]}");
1068
					interfaces_bring_up($port);
1069 69c1b043 gnhb
				} else if (empty($localips[$pid]))
1070
					$localips[$pid] = get_interface_ip($port); // try to get the interface IP from the port
1071
				
1072
				if(!is_ipaddr($localips[$pid])){
1073
					log_error("Could not get a Local IP address for PPtP/L2tP link on {$port} in interfaces_ppps_configure.");
1074
					return 0;
1075 3a906378 gnhb
				}
1076 69c1b043 gnhb
				/* XXX: This needs to go away soon! [It's commented out!] */
1077
				/* Configure the gateway (remote IP ) */
1078 bfbb9bc0 Ermal
				if (!$g['booting'] && !is_ipaddr($gateways[$pid]) && is_hostname($gateways[$pid])) {
1079 69c1b043 gnhb
					/* XXX: Fix later 
1080 765664a4 gnhb
					$gateways[$pid] = gethostbyname($gateways[$pid]);
1081
					if(!is_ipaddr($gateways[$pid])) {
1082
						log_error("Could not get a valid Gateway IP from {$port} via DNS in interfaces_ppps_configure.");
1083 23721285 gnhb
						return 0;
1084 743994a6 gnhb
					}
1085 69c1b043 gnhb
					*/
1086
				}
1087
				if(!is_ipaddr($gateways[$pid])){
1088
					log_error("Could not get a PPtP/L2tP Remote IP address from {$dhcp_gateway} for {$gway} in interfaces_ppps_configure.");
1089
					return 0;
1090 3a906378 gnhb
				}
1091
				break;
1092
			case "ppp":
1093
				if (!file_exists("{$port}")) {
1094
					log_error("Device {$port} does not exist. PPP link cannot start without the modem device.");
1095 23721285 gnhb
					return 0;
1096 3a906378 gnhb
				}
1097
				break;
1098
			default:
1099 bfbb9bc0 Ermal
				log_error("Unkown {$type} configured as ppp interface.");
1100 3a906378 gnhb
				break;
1101
		}
1102
	}
1103 cb37d8fa gnhb
1104
	/* generate mpd.conf */
1105
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1106
	if (!$fd) {
1107
		log_error("Error: cannot open mpd_{$interface}.conf in interface_ppps_configure().\n");
1108 23721285 gnhb
		return 0;
1109 cb37d8fa gnhb
	}
1110 00b702cc gnhb
	
1111 cb37d8fa gnhb
	if (is_array($ports) && count($ports) > 1)
1112
		$multilink = "enable";
1113
	else
1114
		$multilink = "disable";
1115
	
1116
	if ($type == "modem"){
1117
		if (is_ipaddr($ppp['localip']))
1118
			$localip = $ppp['localip'];
1119
		else
1120
			$localip = '0.0.0.0';
1121
1122
		if (is_ipaddr($ppp['gateway']))
1123
			$gateway = $ppp['gateway'];
1124
		else
1125 23721285 gnhb
			$gateway = "10.64.64.{$pppid}";
1126 cb37d8fa gnhb
		$ranges = "{$localip}/0 {$gateway}/0";
1127 3a906378 gnhb
		
1128
		if (empty($ppp['apnum']))	
1129
			$ppp['apnum'] = 1;
1130 23721285 gnhb
	} else
1131 cb37d8fa gnhb
		$ranges = "0.0.0.0/0 0.0.0.0/0";
1132 0661b194 gnhb
1133 cb37d8fa gnhb
	if (isset($ppp['ondemand'])) 
1134
		$ondemand = "enable";
1135
	else
1136
		$ondemand = "disable";
1137
	if (!isset($ppp['idletimeout']))
1138
		$ppp['idletimeout'] = 0;
1139 64d124c5 gnhb
1140 cb37d8fa gnhb
	if (empty($ppp['username']) && $type == "modem"){
1141
		$ppp['username'] = "user";
1142
		$ppp['password'] = "none";
1143
	}
1144
	if (empty($ppp['password']) && $type == "modem")
1145 00b702cc gnhb
		$passwd = "none";
1146
	else
1147
		$passwd = base64_decode($ppp['password']);
1148 0661b194 gnhb
1149
	$bandwidths = explode(',',$ppp['bandwidth']);
1150
	$mtus = explode(',',$ppp['mtu']);
1151
	$mrus = explode(',',$ppp['mru']);
1152
1153 c1cc447c gnhb
	if (isset($ppp['mrru']))
1154 0661b194 gnhb
		$mrrus = explode(',',$ppp['mrru']);
1155 c1cc447c gnhb
1156 cb37d8fa gnhb
	// Construct the mpd.conf file
1157
	$mpdconf = <<<EOD
1158
startup:
1159
	# configure the console
1160
	set console close
1161
	# configure the web server
1162
	set web close
1163
1164
default:
1165
{$ppp['type']}client:
1166
	create bundle static {$interface}
1167
	set iface name {$pppif}
1168
1169
EOD;
1170 0661b194 gnhb
	$setdefaultgw = false;
1171
	$founddefaultgw = false;
1172
	if (is_array($config['gateways']['gateway_item'])) {
1173
		foreach($config['gateways']['gateway_item'] as $gateway) {
1174
			if($interface == $gateway['interface'] && isset($gateway['defaultgw'])) {
1175
				$setdefaultgw = true;
1176
				break;
1177
			} else if (isset($gateway['defaultgw']) && !empty($gateway['interface'])) {
1178
				$founddefaultgw = true;
1179
				break;
1180
			}
1181
		}
1182
	}
1183 82effddb gnhb
	
1184
	if (($interface == "wan" && $founddefaultgw == false) || $setdefaultgw == true){
1185
		$setdefaultgw = true;
1186 cb37d8fa gnhb
		$mpdconf .= <<<EOD
1187
	set iface route default
1188
1189
EOD;
1190 82effddb gnhb
	}
1191 cb37d8fa gnhb
	$mpdconf .= <<<EOD
1192
	set iface {$ondemand} on-demand
1193
	set iface idle {$ppp['idletimeout']}
1194
1195
EOD;
1196
1197 0661b194 gnhb
	if (isset($ppp['ondemand']))
1198 cb37d8fa gnhb
		$mpdconf .= <<<EOD
1199 55f3ca1d gnhb
	set iface addrs 10.10.1.1 10.10.1.2
1200 cb37d8fa gnhb
1201
EOD;
1202 0661b194 gnhb
	
1203
	if (isset($ppp['tcpmssfix']))
1204 8adc1e49 gnhb
		$tcpmss = "disable";
1205
	else
1206
		$tcpmss = "enable";
1207 64d124c5 gnhb
		$mpdconf .= <<<EOD
1208 8adc1e49 gnhb
	set iface {$tcpmss} tcpmssfix
1209 64d124c5 gnhb
1210
EOD;
1211 0661b194 gnhb
1212 cb37d8fa gnhb
	$mpdconf .= <<<EOD
1213
	set iface up-script /usr/local/sbin/ppp-linkup
1214
	set iface down-script /usr/local/sbin/ppp-linkdown
1215
	set ipcp ranges {$ranges}
1216
1217
EOD;
1218 0661b194 gnhb
	if (isset($ppp['vjcomp']))
1219 cb37d8fa gnhb
		$mpdconf .= <<<EOD
1220 64d124c5 gnhb
	set ipcp no vjcomp
1221 cb37d8fa gnhb
1222
EOD;
1223
1224 bfbb9bc0 Ermal
	if (isset($config['system']['dnsallowoverride']))
1225 64d124c5 gnhb
		$mpdconf .= <<<EOD
1226
	set ipcp enable req-pri-dns
1227
	set ipcp enable req-sec-dns
1228
1229
EOD;
1230 23721285 gnhb
	if (!isset($ppp['verbose_log']))
1231
		$mpdconf .= <<<EOD
1232 5d9d443a gnhb
	#log -bund -ccp -chat -iface -ipcp -lcp -link
1233 0661b194 gnhb
1234 23721285 gnhb
EOD;
1235 64d124c5 gnhb
	foreach($ports as $pid => $port){
1236 bfbb9bc0 Ermal
		$port = get_real_interface($port);
1237 00b702cc gnhb
		$mpdconf .= <<<EOD
1238 cb37d8fa gnhb
1239 0661b194 gnhb
	create link static {$interface}_link{$pid} {$type}
1240 cb37d8fa gnhb
	set link action bundle {$interface}
1241
	set link {$multilink} multilink
1242
	set link keep-alive 10 60
1243
	set link max-redial 0
1244 64d124c5 gnhb
1245
EOD;
1246 0661b194 gnhb
		if (isset($ppp['shortseq']))
1247 00b702cc gnhb
			$mpdconf .= <<<EOD
1248 64d124c5 gnhb
	set link no shortseq
1249
1250
EOD;
1251 0661b194 gnhb
1252
		if (isset($ppp['acfcomp']))
1253 00b702cc gnhb
			$mpdconf .= <<<EOD
1254 64d124c5 gnhb
	set link no acfcomp
1255
1256
EOD;
1257 0661b194 gnhb
1258
		if (isset($ppp['protocomp']))
1259 00b702cc gnhb
			$mpdconf .= <<<EOD
1260 64d124c5 gnhb
	set link no protocomp
1261
1262
EOD;
1263 0661b194 gnhb
1264 00b702cc gnhb
		$mpdconf .= <<<EOD
1265 cb37d8fa gnhb
	set link disable chap pap
1266
	set link accept chap pap eap
1267 64d124c5 gnhb
	set link disable incoming
1268 cb37d8fa gnhb
1269
EOD;
1270 00b702cc gnhb
1271
1272 0661b194 gnhb
		if (!empty($bandwidths[$pid]))
1273 00b702cc gnhb
			$mpdconf .= <<<EOD
1274
	set link bandwidth {$bandwidths[$pid]}
1275 cb37d8fa gnhb
1276
EOD;
1277 0661b194 gnhb
1278 8adc1e49 gnhb
		if (empty($mtus[$pid]))
1279
			$mtus[$pid] = "1492";
1280 00b702cc gnhb
			$mpdconf .= <<<EOD
1281
	set link mtu {$mtus[$pid]}
1282 cb37d8fa gnhb
1283
EOD;
1284 0661b194 gnhb
1285
		if (!empty($mrus[$pid]))
1286 00b702cc gnhb
			$mpdconf .= <<<EOD
1287
	set link mru {$mrus[$pid]}
1288
1289
EOD;
1290 0661b194 gnhb
1291 00b702cc gnhb
		$mpdconf .= <<<EOD
1292 cb37d8fa gnhb
	set auth authname "{$ppp['username']}"
1293
	set auth password {$passwd}
1294
1295
EOD;
1296 00b702cc gnhb
		if ($type == "modem") {
1297
			$mpdconf .= <<<EOD
1298 cb37d8fa gnhb
	set modem device {$ppp['ports']}
1299
	set modem script DialPeer
1300
	set modem idle-script Ringback
1301
	set modem watch -cd
1302
	set modem var \$DialPrefix "DT"
1303
	set modem var \$Telephone "{$ppp['phone']}"
1304
1305
EOD;
1306 00b702cc gnhb
		}
1307
		if (isset($ppp['connect-timeout']) && $type == "modem") {
1308
			$mpdconf .= <<<EOD
1309 cb37d8fa gnhb
	set modem var \$ConnectTimeout "{$ppp['connect-timeout']}"
1310
1311
EOD;
1312 00b702cc gnhb
		}
1313
		if (isset($ppp['initstr']) && $type == "modem") {
1314
			$initstr = base64_decode($ppp['initstr']);
1315
			$mpdconf .= <<<EOD
1316 cb37d8fa gnhb
	set modem var \$InitString "{$initstr}"
1317
1318
EOD;
1319 00b702cc gnhb
		}
1320
		if (isset($ppp['simpin']) && $type == "modem") {
1321
			$mpdconf .= <<<EOD
1322 cb37d8fa gnhb
	set modem var \$SimPin "{$ppp['simpin']}"
1323
	set modem var \$PinWait "{$ppp['pin-wait']}"
1324
1325
EOD;
1326 00b702cc gnhb
		}
1327
		if (isset($ppp['apn']) && $type == "modem") {
1328
			$mpdconf .= <<<EOD
1329 cb37d8fa gnhb
	set modem var \$APN "{$ppp['apn']}"
1330
	set modem var \$APNum "{$ppp['apnum']}"
1331
1332
EOD;
1333 00b702cc gnhb
		}
1334
		if (isset($ppp['provider']) && $type == "pppoe") {
1335
			$mpdconf .= <<<EOD
1336 64d124c5 gnhb
	set pppoe service "{$ppp['provider']}"
1337 cb37d8fa gnhb
1338
EOD;
1339 00b702cc gnhb
		}
1340 0661b194 gnhb
		if ($type == "pppoe")
1341 00b702cc gnhb
			$mpdconf .= <<<EOD
1342 64d124c5 gnhb
	set pppoe iface {$port}
1343 cb37d8fa gnhb
1344
EOD;
1345 0661b194 gnhb
1346 39f750b5 gnhb
		if ($type == "pptp" || $type == "l2tp") {
1347 00b702cc gnhb
			$mpdconf .= <<<EOD
1348 0661b194 gnhb
	set pptp self {$localips[$pid]}
1349
	set pptp peer {$gateways[$pid]}
1350 cb37d8fa gnhb
	set pptp disable windowing
1351
1352
EOD;
1353 00b702cc gnhb
		}
1354 23721285 gnhb
		
1355 00b702cc gnhb
		$mpdconf .= "\topen\r\n";
1356 cb37d8fa gnhb
	} //end foreach($port)
1357
1358 c1cc447c gnhb
	// Write out mpd_ppp.conf
1359 cb37d8fa gnhb
	fwrite($fd, $mpdconf);
1360
	fclose($fd);
1361
1362
	// Create the uptime log if requested and if it doesn't exist already, or delete it if it is no longer requested.
1363
	if (isset($ppp['uptime'])) {
1364
		if (!file_exists("/conf/{$pppif}.log")) {
1365
			conf_mount_rw();
1366
			mwexec("echo /dev/null > /conf/{$pppif}.log");
1367
			conf_mount_ro();
1368
		}
1369
	} else {
1370
		if (file_exists("/conf/{$pppif}.log")) {
1371
			conf_mount_rw();
1372
			mwexec("rm -f /conf/{$pppif}.log");
1373
			conf_mount_ro();
1374
		}
1375
	}
1376 3a906378 gnhb
		
1377
	/* fire up mpd */
1378
	mwexec("/usr/local/sbin/mpd5 -b -k -d {$g['varetc_path']} -f mpd_{$interface}.conf -p {$g['varrun_path']}/{$ppp['type']}_{$interface}.pid -s ppp {$ppp['type']}client");
1379
1380 55f3ca1d gnhb
	// Check for PPPoE periodic reset request 
1381 bfbb9bc0 Ermal
	if ($type == "pppoe") {
1382 766bd6d0 gnhb
		if (isset($ppp['pppoe-reset-type']))
1383 5c8e8a17 gnhb
			setup_pppoe_reset_file($ppp['if'], $interface);
1384 766bd6d0 gnhb
		else
1385 5c8e8a17 gnhb
			setup_pppoe_reset_file($ppp['if']);
1386 cb37d8fa gnhb
	}
1387
1388 23721285 gnhb
	return 1;
1389 cb37d8fa gnhb
}
1390
1391 abcb2bed Ermal Lu?i
function interfaces_carp_setup() {
1392 87a2efd1 Ermal Luçi
	global $g, $config;
1393 abcb2bed Ermal Lu?i
1394 2b9747b9 Scott Ullrich
	$balanacing = "";
1395
	$pfsyncinterface = "";
1396
	$pfsyncenabled = "";
1397 b932ef16 Scott Ullrich
	if(isset($config['system']['developerspew'])) {
1398
		$mt = microtime();
1399 abcb2bed Ermal Lu?i
		echo "interfaces_carp_setup() being called $mt\n";
1400 b932ef16 Scott Ullrich
	}
1401 abcb2bed Ermal Lu?i
1402 e5d43d93 Scott Ullrich
	// Prepare CmdCHAIN that will be used to execute commands.
1403
	$cmdchain = new CmdCHAIN();	
1404 abcb2bed Ermal Lu?i
1405 b932ef16 Scott Ullrich
	if ($g['booting']) {
1406 abcb2bed Ermal Lu?i
		echo "Configuring CARP settings...";
1407 7d0f4544 Scott Ullrich
		mute_kernel_msgs();
1408 a5250ebc Scott Ullrich
	}
1409 abcb2bed Ermal Lu?i
1410 b932ef16 Scott Ullrich
	/* suck in configuration items */
1411 abcb2bed Ermal Lu?i
	if($config['installedpackages']['carpsettings']) {
1412 16ccd95c Scott Ullrich
		if($config['installedpackages']['carpsettings']['config']) {
1413 abcb2bed Ermal Lu?i
			foreach($config['installedpackages']['carpsettings']['config'] as $carp) {
1414
				$pfsyncenabled = $carp['pfsyncenabled'];
1415
				$balanacing = $carp['balancing'];
1416
				$pfsyncinterface = $carp['pfsyncinterface'];
1417
				$pfsyncpeerip = $carp['pfsyncpeerip'];
1418
			}
1419 9f6b1429 Scott Ullrich
		}
1420 b932ef16 Scott Ullrich
	} else {
1421
		unset($pfsyncinterface);
1422
		unset($balanacing);
1423
		unset($pfsyncenabled);
1424 6008210b Scott Ullrich
	}
1425 abcb2bed Ermal Lu?i
1426 79d28f42 Scott Ullrich
	$cmdchain->add("Allow CARP", "/sbin/sysctl net.inet.carp.allow=1", true);			
1427 b932ef16 Scott Ullrich
	if($balanacing) {
1428 79d28f42 Scott Ullrich
		$cmdchain->add("Enable CARP ARP-balancing", "/sbin/sysctl net.inet.carp.arpbalance=1", true);
1429
		$cmdchain->add("Disallow CARP preemption", "/sbin/sysctl net.inet.carp.preempt=0", true);
1430 abcb2bed Ermal Lu?i
	} else
1431 79d28f42 Scott Ullrich
		$cmdchain->add("Enable CARP preemption", "/sbin/sysctl net.inet.carp.preempt=1", true);		
1432 abcb2bed Ermal Lu?i
1433 79d28f42 Scott Ullrich
	$cmdchain->add("Enable CARP logging", "/sbin/sysctl net.inet.carp.log=2", true);
1434 abcb2bed Ermal Lu?i
	if (!empty($pfsyncinterface))
1435
		$carp_sync_int = get_real_interface($pfsyncinterface);
1436
1437 b932ef16 Scott Ullrich
	if($g['booting']) {
1438
		/*    install rules to alllow pfsync to sync up during boot
1439
		 *    carp interfaces will remain down until the bootup sequence finishes
1440
		 */
1441 a6726cf2 Ermal Lu?i
		$fd = fopen("{$g['tmp_path']}/rules.boot", "w");
1442
		if ($fd) {
1443 df9d4110 Ermal Lu?i
			fwrite($fd, "pass quick proto carp all keep state\n");
1444
			fwrite($fd, "pass quick proto pfsync all\n");
1445
			fwrite($fd, "pass out quick from any to any keep state\n");
1446 a6726cf2 Ermal Lu?i
			fclose($fd);
1447
			mwexec("/sbin/pfctl -f {$g['tmp_path']}/rules.boot");
1448
		} else
1449
			log_error("Could not create rules.boot file!");
1450 eb772abd Scott Ullrich
	}
1451 abcb2bed Ermal Lu?i
1452 b932ef16 Scott Ullrich
	/* setup pfsync interface */
1453 b42ad736 Scott Ullrich
	if($carp_sync_int and $pfsyncenabled) {
1454 abcb2bed Ermal Lu?i
		if (is_ipaddr($pfsyncpeerip))
1455 e5d43d93 Scott Ullrich
			$cmdchain->add("Bring up pfsync0 syncpeer", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} syncpeer {$pfsyncpeerip} up", false);						
1456 abcb2bed Ermal Lu?i
		else
1457 e5d43d93 Scott Ullrich
			$cmdchain->add("Bring up pfsync0 syncdev", "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up", false);			
1458 abcb2bed Ermal Lu?i
	} else
1459 e5d43d93 Scott Ullrich
		$cmdchain->add("Bring up pfsync0", "/sbin/ifconfig pfsync0 syncdev lo0 up", false);						
1460 abcb2bed Ermal Lu?i
1461
	if($config['virtualip']['vip'])
1462 79d28f42 Scott Ullrich
		$cmdchain->add("Allow CARP.", "/sbin/sysctl net.inet.carp.allow=1", true);				
1463 abcb2bed Ermal Lu?i
	else
1464 79d28f42 Scott Ullrich
		$cmdchain->add("Disallow CARP.", "/sbin/sysctl net.inet.carp.allow=0", true);		
1465 e5d43d93 Scott Ullrich
	
1466 87a2efd1 Ermal Luçi
	if($g['debug'])
1467 e5d43d93 Scott Ullrich
		$cmdchain->setdebug(); // optional for verbose logging
1468 abcb2bed Ermal Lu?i
1469 e5d43d93 Scott Ullrich
	$cmdchain->execute();
1470
	$cmdchain->clear();
1471
1472 abcb2bed Ermal Lu?i
	if ($g['booting']) {
1473
		unmute_kernel_msgs();
1474
		echo "done.\n";
1475
	}
1476 67ee1ec5 Ermal Luçi
}
1477
1478 962fd685 Ermal
function interface_proxyarp_configure($interface = "") {
1479 9006e9f8 Scott Ullrich
	global $config, $g;
1480
	if(isset($config['system']['developerspew'])) {
1481
		$mt = microtime();
1482
		echo "interface_proxyarp_configure() being called $mt\n";
1483
	}
1484 67ee1ec5 Ermal Luçi
1485 9006e9f8 Scott Ullrich
	/* kill any running choparp */
1486 962fd685 Ermal
	if (empty($interface))
1487
		killbyname("choparp");
1488 1b58b513 Scott Ullrich
1489 9006e9f8 Scott Ullrich
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
1490
		$paa = array();
1491 e5d43d93 Scott Ullrich
1492 9006e9f8 Scott Ullrich
		/* group by interface */
1493
		foreach ($config['virtualip']['vip'] as $vipent) {
1494
			if ($vipent['mode'] === "proxyarp") {
1495
				if ($vipent['interface'])
1496
					$proxyif = $vipent['interface'];
1497
				else
1498
					$proxyif = "wan";
1499 abcb2bed Ermal Lu?i
1500 9006e9f8 Scott Ullrich
				if (!is_array($paa[$if]))
1501
					$paa[$proxyif] = array();
1502 7b2d4769 Bill Marquette
1503 9006e9f8 Scott Ullrich
				$paa[$proxyif][] = $vipent;
1504
			}
1505 962fd685 Ermal
		}
1506 9006e9f8 Scott Ullrich
	}
1507 e5d43d93 Scott Ullrich
1508 962fd685 Ermal
	if (!empty($interface)) {
1509
		if (is_array($paa[$interface])) {
1510
			$paaifip = get_interface_ip($interface);
1511
                        if (!is_ipaddr($paaifip))
1512
                                return;
1513
                        $args = get_real_interface($interface) . " auto";
1514
                        foreach ($paa[$interface] as $paent) {
1515
                                if (isset($paent['subnet']))
1516
                                        $args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
1517
                                else if (isset($paent['range']))
1518
                                        $args .= " " . escapeshellarg($paent['range']['from'] . "-" . $paent['range']['to']);
1519
                        }
1520
                        mwexec_bg("/usr/local/sbin/choparp " . $args);	
1521
		}
1522
	} else if (count($paa)) {
1523 9006e9f8 Scott Ullrich
		foreach ($paa as $paif => $paents) {
1524
			$paaifip = get_interface_ip($paif);
1525
			if (!(is_ipaddr($paaifip)))
1526
				continue;
1527
			$args = get_real_interface($paif) . " auto";
1528
			foreach ($paents as $paent) {
1529
				if (isset($paent['subnet']))
1530
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
1531
				else if (isset($paent['range']))
1532 962fd685 Ermal
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" . $paent['range']['to']);
1533 9006e9f8 Scott Ullrich
			}
1534
			mwexec_bg("/usr/local/sbin/choparp " . $args);
1535
		}
1536
	}
1537 9f6b1429 Scott Ullrich
}
1538
1539 e5ac67ed Ermal Lu?i
function interfaces_vips_configure($interface = "") {
1540 87a2efd1 Ermal Luçi
	global $g, $config;
1541 a04de17f Chris Buechler
	if(isset($config['system']['developerspew'])) {
1542
		$mt = microtime();
1543 123f030c Chris Buechler
		echo "interfaces_vips_configure() being called $mt\n";
1544 a04de17f Chris Buechler
	}
1545 abcb2bed Ermal Lu?i
	$paa = array();
1546
	if(is_array($config['virtualip']['vip'])) {
1547
		$carp_setuped = false;
1548 e5ac67ed Ermal Lu?i
		$anyproxyarp = false;
1549 abcb2bed Ermal Lu?i
		foreach ($config['virtualip']['vip'] as $vip) {
1550
			switch ($vip['mode']) {
1551
			case "proxyarp":
1552 123f030c Chris Buechler
				/* nothing it is handled on interface_proxyarp_configure() */
1553 e5ac67ed Ermal Lu?i
				if ($interface <> "" && $vip['interface'] <> $interface)
1554
					continue;
1555
				$anyproxyarp = true;
1556 abcb2bed Ermal Lu?i
				break;
1557
			case "ipalias":
1558 e5ac67ed Ermal Lu?i
				if ($interface <> "" && $vip['interface'] <> $interface)
1559
					continue;
1560 abcb2bed Ermal Lu?i
				interface_ipalias_configure(&$vip);
1561
				break;
1562
			case "carp":
1563 e5ac67ed Ermal Lu?i
				if ($interface <> "" && $vip['interface'] <> $interface)
1564
					continue;
1565 abcb2bed Ermal Lu?i
				if ($carp_setuped == false) {
1566
					interfaces_carp_setup();
1567
					$carp_setuped = true;
1568
				}
1569
				interface_carp_configure($vip);
1570
				break;
1571
			case "carpdev-dhcp":
1572 e5ac67ed Ermal Lu?i
				if ($interface <> "" && $vip['interface'] <> $interface)
1573
					continue;
1574 abcb2bed Ermal Lu?i
				interface_carpdev_configure($vip);
1575
				break;
1576 6a74c90e Scott Ullrich
			}
1577 a04de17f Chris Buechler
		}
1578 abcb2bed Ermal Lu?i
		
1579 e5ac67ed Ermal Lu?i
		if ($anyproxyarp == true)
1580
			interface_proxyarp_configure();
1581 abcb2bed Ermal Lu?i
	}
1582
}
1583
1584
function interface_ipalias_configure(&$vip) {
1585
1586
	if ($vip['mode'] == "ipalias") {
1587
		$if = get_real_interface($vip['interface']);
1588
		mwexec("/sbin/ifconfig " . escapeshellarg($if) . " " . $vip['subnet'] . "/" . escapeshellarg($vip['subnet_bits']) . " alias");
1589 a04de17f Chris Buechler
	}
1590
}
1591
1592 abcb2bed Ermal Lu?i
function interface_reload_carps($cif) {
1593
	global $config;
1594
1595
	$carpifs = link_ip_to_carp_interface(find_interface_ip($cif));
1596 9006e9f8 Scott Ullrich
	if (empty($carpifs))
1597 abcb2bed Ermal Lu?i
		return;
1598
1599
	$carps = explode(" ", $carpifs);
1600
	if(is_array($config['virtualip']['vip'])) {
1601 9006e9f8 Scott Ullrich
		$viparr = &$config['virtualip']['vip'];
1602
		foreach ($viparr as $vip) {
1603 abcb2bed Ermal Lu?i
			if (in_array($vip['carpif'], $carps)) {
1604 9006e9f8 Scott Ullrich
				switch ($vip['mode']) {
1605
					case "carp":
1606 abcb2bed Ermal Lu?i
					interface_vip_bring_down($vip);
1607
					sleep(1);
1608 9006e9f8 Scott Ullrich
					interface_carp_configure($vip);
1609
					break;
1610
					case "carpdev-dhcp":
1611 abcb2bed Ermal Lu?i
					interface_vip_bring_down($vip);
1612
					sleep(1);
1613 9006e9f8 Scott Ullrich
					interface_carpdev_configure($vip);
1614
					break;
1615 abcb2bed Ermal Lu?i
				}
1616 9006e9f8 Scott Ullrich
			}
1617
		}
1618
	}
1619 abcb2bed Ermal Lu?i
}
1620
1621
function interface_carp_configure(&$vip) {
1622
	global $config, $g;
1623
	if(isset($config['system']['developerspew'])) {
1624 58ebf6bb Scott Ullrich
		$mt = microtime();
1625 0a595d84 Ermal Lu?i
		echo "interface_carp_configure() being called $mt\n";
1626 58ebf6bb Scott Ullrich
	}
1627 abcb2bed Ermal Lu?i
1628
	if ($vip['mode'] != "carp")
1629
		return;
1630
1631
	$vip_password = $vip['password'];
1632 942fdd55 jim-p
	$vip_password = escapeshellarg(addslashes(str_replace(" ", "", $vip_password)));
1633 abcb2bed Ermal Lu?i
	if ($vip['password'] != "")
1634 942fdd55 jim-p
		$password = " pass {$vip_password}";
1635 58ebf6bb Scott Ullrich
1636 12fafaf7 Chris Buechler
	// set the vip interface to the vhid
1637
	$vipif = "vip{$vip['vhid']}";
1638 58ebf6bb Scott Ullrich
1639 abcb2bed Ermal Lu?i
	$interface = interface_translate_type_to_real($vip['interface']);
1640
	/*
1641
	 * ensure the interface containing the VIP really exists
1642 58ebf6bb Scott Ullrich
 	 * prevents a panic if the interface is missing or invalid
1643
	 */
1644
	$realif = get_real_interface($vip['interface']);
1645
	if (!does_interface_exist($realif)) {
1646
		file_notice("CARP", "Interface specified for the virtual IP address {$vip['subnet']} does not exist. Skipping this VIP.", "Firewall: Virtual IP", "");
1647
		return;
1648
	}
1649 abcb2bed Ermal Lu?i
1650 7de83e03 Ermal
	/* Ensure CARP IP really exists prior to loading up. */
1651
	$ww_subnet_ip = find_interface_ip($realif);
1652
	$ww_subnet_bits = find_interface_subnet($realif);
1653
	if (!ip_in_subnet($vip['subnet'], gen_subnet($ww_subnet_ip, $ww_subnet_bits) . "/" . $ww_subnet_bits)) {
1654 abcb2bed Ermal Lu?i
		file_notice("CARP", "Sorry but we could not find a matching real interface subnet for the virtual IP address {$vip['subnet']}.", "Firewall: Virtual IP", "");
1655
		return;
1656
	}
1657
1658
	/* create the carp interface and setup */
1659 37a53d16 Scott Ullrich
	if (does_interface_exist($vipif)) {
1660 871768cf Ermal
		pfSense_interface_flags($vipif, -IFF_UP);
1661 37a53d16 Scott Ullrich
	} else {
1662 871768cf Ermal
		$carpif = pfSense_interface_create("carp");
1663
		pfSense_interface_rename($carpif, $vipif);
1664
		pfSense_ngctl_name("{$carpif}:", $vipif);
1665 abcb2bed Ermal Lu?i
	}
1666
1667
	/* invalidate interface cache */
1668
	get_interface_arr(true);
1669
1670
	$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
1671 290d312d Ermal Lu?i
	mwexec("/sbin/ifconfig {$vipif} {$vip['subnet']}/{$vip['subnet_bits']} vhid {$vip['vhid']} advskew {$vip['advskew']} {$password}");
1672 abcb2bed Ermal Lu?i
1673
	interfaces_bring_up($vipif);
1674
	
1675
	return $vipif;
1676
}
1677
1678
function interface_carpdev_configure(&$vip) {
1679
	global $g;
1680
1681
	if ($vip['mode'] != "carpdev-dhcp")
1682 9006e9f8 Scott Ullrich
		return;
1683 abcb2bed Ermal Lu?i
1684 9006e9f8 Scott Ullrich
	$vip_password = $vip['password'];
1685
	$vip_password = str_replace(" ", "", $vip_password);
1686
	if($vip['password'] != "")
1687
		$password = " pass \"" . $vip_password . "\"";
1688 abcb2bed Ermal Lu?i
1689
	log_error("Found carpdev interface {$vip['interface']} on top of interface {$interface}");
1690
	if (empty($vip['interface']))
1691
		return;
1692
1693
	$vipif = "vip" . $vip['vhid'];
1694
	$realif = interface_translate_type_to_real($vip['interface']);
1695 ec054b7c Scott Ullrich
	interfaces_bring_up($realif);
1696 9006e9f8 Scott Ullrich
	/*
1697
	 * ensure the interface containing the VIP really exists
1698
	 * prevents a panic if the interface is missing or invalid
1699
	 */
1700
	if (!does_interface_exist($realif)) {
1701
		file_notice("CARP", "Interface specified for the virtual IP address {$vip['subnet']} does not exist. Skipping this VIP.", "Firewall: Virtual IP", "");
1702
		return;
1703
	}
1704 abcb2bed Ermal Lu?i
1705 f07bee94 Scott Ullrich
	if (does_interface_exist($vipif)) {
1706 37a53d16 Scott Ullrich
		interface_bring_down($vipif);
1707 f07bee94 Scott Ullrich
	} else {
1708 abcb2bed Ermal Lu?i
		$carpdevif = exec("/sbin/ifconfig carp create");
1709
		mwexec("/sbin/ifconfig {$carpdevif} name {$vipif}");
1710 871768cf Ermal
		pfSense_ngctl_name("{$carpdevif}:", $vipif);
1711 abcb2bed Ermal Lu?i
	}
1712
1713
	mwexec("/sbin/ifconfig {$vipif} carpdev {$realif} vhid {$vip['vhid']} advskew {$vip['advskew']} {$password}");
1714 ec054b7c Scott Ullrich
	interfaces_bring_up($vipif);
1715 abcb2bed Ermal Lu?i
1716
	/*
1717
	 * XXX: BIG HACK but carpdev needs ip services active
1718
	 *      before even starting something as dhclient.
1719
	 *      I do not know if this is a feature or a bug
1720
	 *      but better than track it make it work ;) .
1721
	 */
1722
	//$fakeiptouse = "10.254.254." . ($carp_instances_counter+1);
1723
	//$cmdchain->add("CarpDEV hack", "/sbin/ifconfig {$carpint} inet {$fakeiptouse}", false);
1724
1725
	/* generate dhclient_wan.conf */
1726
	$fd = fopen("{$g['varetc_path']}/dhclient_{$vipif}.conf", "w");
1727
	if ($fd) {
1728
		$dhclientconf = "";
1729
1730
		$dhclientconf .= <<<EOD
1731
interface "{$vipif}" {
1732
timeout 60;
1733
retry 1;
1734
select-timeout 0;
1735
initial-interval 1;
1736
script "/sbin/dhclient-script";
1737
}
1738
1739
EOD;
1740
1741
		fwrite($fd, $dhclientconf);
1742
		fclose($fd);
1743
1744
		/* fire up dhclient */
1745 6955830f Ermal Lu?i
		mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$vipif}.conf {$vipif} > {$g['tmp_path']}/{$vipif}_output > {$g['tmp_path']}/{$vipif}_error_output", false);
1746 abcb2bed Ermal Lu?i
	} else {
1747
		log_error("Error: cannot open dhclient_{$vipif}.conf in interfaces_carpdev_configure() for writing.\n");
1748
		mwexec("/sbin/dhclient -b {$vipif}");
1749
	}
1750
1751
	return $vipif;
1752
}
1753
1754 854aed18 Ermal Lu?i
function interface_wireless_clone($realif, $wlcfg) {
1755 568b1358 Scott Ullrich
	global $config, $g;
1756 88157f66 Scott Ullrich
	/*   Check to see if interface has been cloned as of yet.  
1757
	 *   If it has not been cloned then go ahead and clone it.
1758
	 */
1759 2a203afd Seth Mos
	$needs_clone = false;
1760 9f428275 Erik Fonnesbeck
	if(is_array($wlcfg['wireless']))
1761
		$wlcfg_mode = $wlcfg['wireless']['mode'];
1762
	else
1763
		$wlcfg_mode = $wlcfg['mode'];
1764
	switch($wlcfg_mode) {
1765 2a203afd Seth Mos
		 case "hostap":
1766
			$mode = "wlanmode hostap";
1767
			break;
1768
		 case "adhoc":
1769
			$mode = "wlanmode adhoc";
1770
			break;
1771
		 default:
1772
			$mode = "";
1773
			break;
1774
	}
1775 34808d4e Erik Fonnesbeck
	$baseif = interface_get_wireless_base($wlcfg['if']);
1776 854aed18 Ermal Lu?i
	if(does_interface_exist($realif)) {
1777
		exec("/sbin/ifconfig {$realif}", $output, $ret);
1778 2a203afd Seth Mos
		$ifconfig_str = implode($output);
1779 9f428275 Erik Fonnesbeck
		if(($wlcfg_mode == "hostap") && (! preg_match("/hostap/si", $ifconfig_str))) {
1780 fa71a9b6 Erik Fonnesbeck
			log_error("Interface {$realif} changed to hostap mode");
1781 2a203afd Seth Mos
			$needs_clone = true;
1782
		}
1783 9f428275 Erik Fonnesbeck
		if(($wlcfg_mode == "adhoc") && (! preg_match("/adhoc/si", $ifconfig_str))) {
1784 fa71a9b6 Erik Fonnesbeck
			log_error("Interface {$realif} changed to adhoc mode");
1785 2a203afd Seth Mos
			$needs_clone = true;
1786
		}
1787 9f428275 Erik Fonnesbeck
		if(($wlcfg_mode == "bss") && (preg_match("/hostap|adhoc/si", $ifconfig_str))) {
1788 fa71a9b6 Erik Fonnesbeck
			log_error("Interface {$realif} changed to infrastructure mode");
1789 2a203afd Seth Mos
			$needs_clone = true;
1790
		}
1791
	} else {
1792
		$needs_clone = true;
1793 88157f66 Scott Ullrich
	}
1794 2a203afd Seth Mos
1795 19e83210 Scott Ullrich
	if($needs_clone == true) {
1796 2a203afd Seth Mos
		/* remove previous instance if it exists */
1797 854aed18 Ermal Lu?i
		if(does_interface_exist($realif))
1798 871768cf Ermal
			pfSense_interface_destroy($realif);
1799 854aed18 Ermal Lu?i
1800
		log_error("Cloning new wireless interface {$realif}");
1801 b99256c1 Scott Ullrich
		// Create the new wlan interface. FreeBSD returns the new interface name.
1802
		// example:  wlan2
1803 6d54e865 Erik Fonnesbeck
		exec("/sbin/ifconfig wlan create wlandev {$baseif} {$mode} bssid 2>&1", $out, $ret);
1804 2a203afd Seth Mos
		if($ret <> 0) {
1805 fa71a9b6 Erik Fonnesbeck
			log_error("Failed to clone interface {$baseif} with error code {$ret}, output {$out[0]}");
1806 9f428275 Erik Fonnesbeck
			return false;
1807 2a203afd Seth Mos
		}
1808
		$newif = trim($out[0]);
1809
		// Rename the interface to {$parentnic}_wlan{$number}#: EX: ath0_wlan0
1810 871768cf Ermal
		pfSense_interface_rename($newif, $realif);
1811 2a203afd Seth Mos
		// FIXME: not sure what ngctl is for. Doesn't work.
1812 fa71a9b6 Erik Fonnesbeck
		// mwexec("/usr/sbin/ngctl name {$newif}: {$realif}", false);
1813 88157f66 Scott Ullrich
	}
1814 9f428275 Erik Fonnesbeck
	return true;
1815 88157f66 Scott Ullrich
}
1816
1817 8f0289e7 Erik Fonnesbeck
function interface_sync_wireless_clones(&$ifcfg, $sync_changes = false) {
1818
	global $config, $g;
1819
1820 20f09b3b Erik Fonnesbeck
	$shared_settings = array('standard', 'turbo', 'protmode', 'txpower', 'channel', 'distance', 'regdomain', 'regcountry', 'reglocation');
1821 8f0289e7 Erik Fonnesbeck
1822 263e2b7e Erik Fonnesbeck
	if(!is_interface_wireless($ifcfg['if']))
1823 7de319a1 Erik Fonnesbeck
		return;
1824
1825 34808d4e Erik Fonnesbeck
	$baseif = interface_get_wireless_base($ifcfg['if']);
1826 8f0289e7 Erik Fonnesbeck
1827 38b7d47d Erik Fonnesbeck
	$iflist = get_configured_interface_list(false, true);
1828 8f0289e7 Erik Fonnesbeck
	foreach ($iflist as $if) {
1829 34808d4e Erik Fonnesbeck
		if ($baseif == interface_get_wireless_base($config['interfaces'][$if]['if']) && $ifcfg['if'] != $config['interfaces'][$if]['if']) {
1830 8f0289e7 Erik Fonnesbeck
			if (isset($config['interfaces'][$if]['wireless']['standard']) || $sync_changes) {
1831
				foreach ($shared_settings as $setting) {
1832
					if ($sync_changes) {
1833
						$config['interfaces'][$if]['wireless'][$setting] = $ifcfg['wireless'][$setting];
1834
					} else {
1835
						$ifcfg['wireless'][$setting] = $config['interfaces'][$if]['wireless'][$setting];
1836
					}
1837
				}
1838
				if (!$sync_changes)
1839
					break;
1840
			}
1841
		}
1842
	}
1843 263e2b7e Erik Fonnesbeck
1844
	if (interface_is_wireless_clone($ifcfg['if'])) {
1845
		foreach ($config['wireless']['clone'] as &$clone) {
1846
			if ($clone['cloneif'] == $ifcfg['if']) {
1847
				if ($sync_changes) {
1848
					$clone['mode'] = $ifcfg['wireless']['mode'];
1849
				} else {
1850
					$ifcfg['wireless']['mode'] = $clone['mode'];
1851
				}
1852
				break;
1853
			}
1854
		}
1855 867d444b Erik Fonnesbeck
		unset($clone);
1856 263e2b7e Erik Fonnesbeck
	}
1857 8f0289e7 Erik Fonnesbeck
}
1858
1859 19e83210 Scott Ullrich
function interface_wireless_configure($if, &$wl, &$wlcfg) {
1860 ac3f8318 Espen Johansen
	global $config, $g;
1861 eb772abd Scott Ullrich
1862 4742e927 Scott Ullrich
	/*    open up a shell script that will be used to output the commands.
1863
	 *    since wireless is changing a lot, these series of commands are fragile
1864
     *    and will sometimes need to be verified by a operator by executing the command
1865
     *    and returning the output of the command to the developers for inspection.  please
1866
     *    do not change this routine from a shell script to individul exec commands.  -sullrich
1867
	 */
1868 eb772abd Scott Ullrich
1869 b99256c1 Scott Ullrich
	// Remove script file
1870 490b8b2a Scott Ullrich
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
1871 eb772abd Scott Ullrich
1872 b99256c1 Scott Ullrich
	// Clone wireless nic if needed.
1873 19e83210 Scott Ullrich
	interface_wireless_clone($if, $wl);
1874 2a203afd Seth Mos
1875 8f0289e7 Erik Fonnesbeck
	// Reject inadvertent changes to shared settings in case the interface hasn't been configured.
1876
	interface_sync_wireless_clones($wl, false);
1877
1878 6955830f Ermal Lu?i
	$fd_set = fopen("{$g['tmp_path']}/{$if}_setup.sh","w");
1879 4742e927 Scott Ullrich
	fwrite($fd_set, "#!/bin/sh\n");
1880 36d0358b Scott Ullrich
	fwrite($fd_set, "# {$g['product_name']} wireless configuration script.\n\n");
1881 eb772abd Scott Ullrich
1882 2ac908dd Espen Johansen
	/* set values for /path/program */
1883
	$hostapd = "/usr/sbin/hostapd";
1884
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
1885 4742e927 Scott Ullrich
	$ifconfig = "/sbin/ifconfig";
1886
	$killall = "/usr/bin/killall";
1887 2ac908dd Espen Johansen
1888 a59abc65 Scott Ullrich
	/* Set all wireless ifconfig variables (splitt up to get rid of needed checking) */
1889 5508cf57 Scott Ullrich
1890 2a203afd Seth Mos
	$wlcmd = array();
1891
	/* Make sure it's up */
1892
	$wlcmd[] = "up";
1893 ac3f8318 Espen Johansen
	/* Set a/b/g standard */
1894 9be20928 Erik Fonnesbeck
	$standard = str_replace(" Turbo", "", $wlcfg['standard']);
1895
	$wlcmd[] = "mode " . escapeshellarg($standard);
1896 2a203afd Seth Mos
1897 5030b5eb Erik Fonnesbeck
	/* XXX: Disable ampdu for now on mwl when running in 11n mode
1898
	 * to prevent massive packet loss under certain conditions. */
1899 9be20928 Erik Fonnesbeck
	if(preg_match("/^mwl/i", $if) && ($standard == "11ng" || $standard == "11na"))
1900 5030b5eb Erik Fonnesbeck
		$wlcmd[] = "-ampdu";
1901
1902 2a203afd Seth Mos
	/* Set ssid */
1903
	if($wlcfg['ssid'])
1904
		$wlcmd[] = "ssid " .escapeshellarg($wlcfg['ssid']);
1905 5508cf57 Scott Ullrich
1906 0856c4ac Scott Ullrich
	/* Set 802.11g protection mode */
1907 2a203afd Seth Mos
	$wlcmd[] = "protmode " . escapeshellarg($wlcfg['protmode']);
1908 0856c4ac Scott Ullrich
1909 ac3f8318 Espen Johansen
	/* set wireless channel value */
1910 2a203afd Seth Mos
	if(isset($wlcfg['channel'])) {
1911
		if($wlcfg['channel'] == "0") {
1912
			$wlcmd[] = "channel any";
1913
		} else {
1914
			$wlcmd[] = "channel " . escapeshellarg($wlcfg['channel']);
1915
		}
1916
	}
1917 2ac908dd Espen Johansen
1918 f134033e Scott Ullrich
	/* set Distance value */
1919 eb772abd Scott Ullrich
	if($wlcfg['distance'])
1920 f134033e Scott Ullrich
		$distance = escapeshellarg($wlcfg['distance']);
1921
1922 ac3f8318 Espen Johansen
	/* Set wireless hostap mode */
1923 2a203afd Seth Mos
	if ($wlcfg['mode'] == "hostap") {
1924
		$wlcmd[] = "mediaopt hostap";
1925
	} else {
1926
		$wlcmd[] = "-mediaopt hostap";
1927
	}
1928 ac3f8318 Espen Johansen
1929
	/* Set wireless adhoc mode */
1930 2a203afd Seth Mos
	if ($wlcfg['mode'] == "adhoc") {
1931
		$wlcmd[] = "mediaopt adhoc";
1932
	} else {
1933
		$wlcmd[] = "-mediaopt adhoc";
1934
	}
1935 ac3f8318 Espen Johansen
1936
	/* Not neccesary to set BSS mode as this is default if adhoc and/or hostap is NOT set */
1937
1938
	/* handle hide ssid option */
1939 2a203afd Seth Mos
	if(isset($wlcfg['hidessid']['enable'])) {
1940
		$wlcmd[] = "hidessid";
1941
	} else {
1942
		$wlcmd[] = "-hidessid";
1943
	}
1944 ac3f8318 Espen Johansen
1945
	/* handle pureg (802.11g) only option */
1946 2a203afd Seth Mos
	if(isset($wlcfg['pureg']['enable'])) {
1947
		$wlcmd[] = "mode 11g pureg";
1948
	} else {
1949
		$wlcmd[] = "-pureg";
1950
	}
1951 ac3f8318 Espen Johansen
1952 ed459692 Erik Fonnesbeck
	/* handle puren (802.11n) only option */
1953
	if(isset($wlcfg['puren']['enable'])) {
1954
		$wlcmd[] = "puren";
1955
	} else {
1956
		$wlcmd[] = "-puren";
1957
	}
1958
1959 ac3f8318 Espen Johansen
	/* enable apbridge option */
1960 2a203afd Seth Mos
	if(isset($wlcfg['apbridge']['enable'])) {
1961
		$wlcmd[] = "apbridge";
1962
	} else {
1963
		$wlcmd[] = "-apbridge";
1964
	}
1965 ac3f8318 Espen Johansen
1966
	/* handle turbo option */
1967 2a203afd Seth Mos
	if(isset($wlcfg['turbo']['enable'])) {
1968
		$wlcmd[] = "mediaopt turbo";
1969
	} else {
1970
		$wlcmd[] = "-mediaopt turbo";
1971
	}
1972 ac3f8318 Espen Johansen
1973
	/* handle txpower setting */
1974 2a203afd Seth Mos
	/* if($wlcfg['txpower'] <> "")
1975
		$wlcmd[] = "txpower " . escapeshellarg($wlcfg['txpower']);
1976
	*/
1977 ac3f8318 Espen Johansen
	/* handle wme option */
1978 2a203afd Seth Mos
	if(isset($wlcfg['wme']['enable'])) {
1979
		$wlcmd[] = "wme";
1980
	} else {
1981
		$wlcmd[] = "-wme";
1982
	}
1983 eb772abd Scott Ullrich
1984 ac3f8318 Espen Johansen
	/* set up wep if enabled */
1985 2a203afd Seth Mos
	$wepset = "";
1986
	if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
1987
		switch($wlcfg['wpa']['auth_algs']) {
1988
			case "1":
1989
				$wepset .= "authmode open wepmode on ";
1990
				break;
1991
			case "2":
1992
				$wepset .= "authmode shared wepmode on ";
1993
				break;
1994
			case "3":
1995
				$wepset .= "authmode mixed wepmode on ";
1996
		}
1997 2f19fa14 Scott Ullrich
		$i = 1;
1998
		foreach ($wlcfg['wep']['key'] as $wepkey) {
1999
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
2000 2a203afd Seth Mos
			if (isset($wepkey['txkey'])) {
2001
				$wlcmd[] = "weptxkey {$i} ";
2002
			}
2003 2f19fa14 Scott Ullrich
			$i++;
2004
		}
2005 2a203afd Seth Mos
		$wlcmd[] = $wepset;
2006
	} else {
2007
		$wlcmd[] = "authmode open wepmode off ";
2008 ac3f8318 Espen Johansen
	}
2009
2010 c8178bb7 Erik Fonnesbeck
	mwexec(kill_hostapd("{$if}"));
2011
	mwexec(kill_wpasupplicant("{$if}"));
2012
2013 ac3f8318 Espen Johansen
	/* generate wpa_supplicant/hostap config if wpa is enabled */
2014 2a203afd Seth Mos
	conf_mount_rw();
2015 ac3f8318 Espen Johansen
2016
	switch ($wlcfg['mode']) {
2017 b67d192d Scott Ullrich
		case 'bss':
2018 ac3f8318 Espen Johansen
			if (isset($wlcfg['wpa']['enable'])) {
2019
				$wpa .= <<<EOD
2020 454756b9 Scott Ullrich
ctrl_interface={$g['varrun_path']}/wpa_supplicant
2021 50ad3b7c Scott Ullrich
ctrl_interface_group=0
2022
ap_scan=1
2023 2ac908dd Espen Johansen
#fast_reauth=1
2024 249558a2 Scott Ullrich
network={
2025 454756b9 Scott Ullrich
ssid="{$wlcfg['ssid']}"
2026
scan_ssid=1
2027 2ac908dd Espen Johansen
priority=5
2028
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
2029 454756b9 Scott Ullrich
psk="{$wlcfg['wpa']['passphrase']}"
2030 2ac908dd Espen Johansen
pairwise={$wlcfg['wpa']['wpa_pairwise']}
2031
group={$wlcfg['wpa']['wpa_pairwise']}
2032 50ad3b7c Scott Ullrich
}
2033
EOD;
2034
2035 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
2036 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
2037
				fclose($fd);
2038
			}
2039 2a203afd Seth Mos
			break;
2040 ac3f8318 Espen Johansen
		case 'hostap':
2041 7eadaa9c Scott Ullrich
			if($wlcfg['wpa']['passphrase']) 
2042
				$wpa_passphrase = "wpa_passphrase={$wlcfg['wpa']['passphrase']}\n";
2043 abfd0c9b Scott Ullrich
			else 
2044
				$wpa_passphrase = "";
2045 ac3f8318 Espen Johansen
			if (isset($wlcfg['wpa']['enable'])) {
2046
				$wpa .= <<<EOD
2047 459d6351 Scott Ullrich
interface={$if}
2048
driver=bsd
2049
logger_syslog=-1
2050
logger_syslog_level=0
2051
logger_stdout=-1
2052
logger_stdout_level=0
2053 2ac908dd Espen Johansen
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
2054
ctrl_interface={$g['varrun_path']}/hostapd
2055 459d6351 Scott Ullrich
ctrl_interface_group=wheel
2056 2ac908dd Espen Johansen
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
2057
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
2058 b67d192d Scott Ullrich
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
2059 459d6351 Scott Ullrich
ssid={$wlcfg['ssid']}
2060 2ac908dd Espen Johansen
debug={$wlcfg['wpa']['debug_mode']}
2061
auth_algs={$wlcfg['wpa']['auth_algs']}
2062
wpa={$wlcfg['wpa']['wpa_mode']}
2063
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
2064
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
2065 ac3f8318 Espen Johansen
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
2066
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
2067
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
2068 7eadaa9c Scott Ullrich
{$wpa_passphrase}
2069 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
2070
#rsn_preauth=1
2071
#rsn_preauth_interfaces=eth0
2072 525d565b Scott Ullrich
2073 459d6351 Scott Ullrich
EOD;
2074 2ac908dd Espen Johansen
2075 5949124c Scott Ullrich
				if($wlcfg['auth_server_addr'] && $wlcfg['auth_server_shared_secret']) {
2076
					$auth_server_port = "1812";
2077
					if($wlcfg['auth_server_port']) 
2078
						$auth_server_port = $wlcfg['auth_server_port'];
2079
					$wpa .= <<<EOD
2080 525d565b Scott Ullrich
2081 5949124c Scott Ullrich
ieee8021x=1
2082
auth_server_addr={$wlcfg['auth_server_addr']}
2083
auth_server_port={$auth_server_port}
2084
auth_server_shared_secret={$wlcfg['auth_server_shared_secret']}
2085 525d565b Scott Ullrich
2086 459d6351 Scott Ullrich
EOD;
2087 5949124c Scott Ullrich
				} else {
2088
					$wpa .= "ieee8021x={$wlcfg['wpa']['ieee8021x']}\n";
2089
				}
2090 2ac908dd Espen Johansen
2091 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
2092 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
2093
				fclose($fd);
2094 2ac908dd Espen Johansen
2095 ac3f8318 Espen Johansen
			}
2096 2a203afd Seth Mos
			break;
2097 eb772abd Scott Ullrich
	}
2098 ac3f8318 Espen Johansen
2099 4742e927 Scott Ullrich
	/*
2100
	 *    all variables are set, lets start up everything
2101 2a203afd Seth Mos
	 */
2102 eb772abd Scott Ullrich
2103 bbfc810e Erik Fonnesbeck
	$baseif = interface_get_wireless_base($if);
2104
2105 78922914 Scott Ullrich
	/* set ack timers according to users preference (if he/she has any) */
2106
	if($distance) {
2107 4742e927 Scott Ullrich
		fwrite($fd_set, "# Enable ATH distance settings\n");
2108 e327021d Erik Fonnesbeck
		fwrite($fd_set, "/sbin/athctrl.sh -i {$baseif} -d {$distance}\n");
2109 78922914 Scott Ullrich
	}
2110 eb772abd Scott Ullrich
2111 ac3f8318 Espen Johansen
	if (isset($wlcfg['wpa']['enable'])) {
2112 2a203afd Seth Mos
		if ($wlcfg['mode'] == "bss") {
2113 4742e927 Scott Ullrich
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
2114 2a203afd Seth Mos
		}
2115
		if ($wlcfg['mode'] == "hostap") {
2116 4742e927 Scott Ullrich
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
2117 2a203afd Seth Mos
		}
2118 ac3f8318 Espen Johansen
	}
2119 191a8175 Scott Ullrich
2120 4742e927 Scott Ullrich
	fclose($fd_set);
2121 8a958125 Scott Ullrich
	conf_mount_ro();
2122
2123 bbfc810e Erik Fonnesbeck
	/* Making sure regulatory settings have actually changed
2124
	 * before applying, because changing them requires bringing
2125
	 * down all wireless networks on the interface. */
2126
	exec("{$ifconfig} " . escapeshellarg($if), $output);
2127
	$ifconfig_str = implode($output);
2128
	unset($output);
2129
	$reg_changing = false;
2130
2131 89e7778f Erik Fonnesbeck
	/* special case for the debug country code */
2132
	if ($wlcfg['regcountry'] == 'DEBUG' && !preg_match("/\sregdomain\s+DEBUG\s/si", $ifconfig_str))
2133
		$reg_changing = true;
2134
	else if ($wlcfg['regdomain'] && !preg_match("/\sregdomain\s+{$wlcfg['regdomain']}\s/si", $ifconfig_str))
2135 bbfc810e Erik Fonnesbeck
		$reg_changing = true;
2136
	else if ($wlcfg['regcountry'] && !preg_match("/\scountry\s+{$wlcfg['regcountry']}\s/si", $ifconfig_str))
2137
		$reg_changing = true;
2138 89e7778f Erik Fonnesbeck
	else if ($wlcfg['reglocation'] == 'anywhere' && preg_match("/\s(indoor|outdoor)\s/si", $ifconfig_str))
2139
		$reg_changing = true;
2140 06cb2656 Erik Fonnesbeck
	else if ($wlcfg['reglocation'] && $wlcfg['reglocation'] != 'anywhere' && !preg_match("/\s{$wlcfg['reglocation']}\s/si", $ifconfig_str))
2141 bbfc810e Erik Fonnesbeck
		$reg_changing = true;
2142
2143
	if ($reg_changing) {
2144
		/* set regulatory domain */
2145
		if($wlcfg['regdomain'])
2146
			$wlregcmd[] = "regdomain " . escapeshellarg($wlcfg['regdomain']);
2147
2148
		/* set country */
2149
		if($wlcfg['regcountry'])
2150
			$wlregcmd[] = "country " . escapeshellarg($wlcfg['regcountry']);
2151
2152
		/* set location */
2153
		if($wlcfg['reglocation'])
2154
			$wlregcmd[] = escapeshellarg($wlcfg['reglocation']);
2155
2156
		$wlregcmd_args = implode(" ", $wlregcmd);
2157
2158
		/* build a complete list of the wireless clones for this interface */
2159
		$clone_list = array();
2160
		if (does_interface_exist(interface_get_wireless_clone($baseif)))
2161
			$clone_list[] = interface_get_wireless_clone($baseif);
2162
		if (is_array($config['wireless']['clone'])) {
2163
			foreach ($config['wireless']['clone'] as $clone) {
2164
				if ($clone['if'] == $baseif)
2165
					$clone_list[] = $clone['cloneif'];
2166
			}
2167
		}
2168
2169
		/* find which clones are up and bring them down */
2170
		$clones_up = array();
2171
		foreach ($clone_list as $clone_if) {
2172 1cf76394 Erik Fonnesbeck
			$clone_status = pfSense_get_interface_addresses($clone_if);
2173 bbfc810e Erik Fonnesbeck
			if ($clone_status['status'] == 'up') {
2174
				$clones_up[] = $clone_if;
2175
				mwexec("{$ifconfig} " . escapeshellarg($clone_if) . " down");
2176
			}
2177
		}
2178
2179
		/* apply the regulatory settings */
2180
		mwexec("{$ifconfig} " . escapeshellarg($if) . " {$wlregcmd_args}");
2181
2182
		/* bring the clones back up that were previously up */
2183
		foreach ($clones_up as $clone_if) {
2184
			mwexec("{$ifconfig} " . escapeshellarg($clone_if) . " up");
2185
		}
2186
	}
2187
2188 23fdc06e Erik Fonnesbeck
	/* The mode must be specified in a separate command before ifconfig
2189
	 * will allow the mode and channel at the same time in the next. */
2190 9be20928 Erik Fonnesbeck
	mwexec("/sbin/ifconfig {$if} mode " . escapeshellarg($standard));
2191 23fdc06e Erik Fonnesbeck
2192 2a48a885 Erik Fonnesbeck
	/* configure wireless */
2193
	$wlcmd_args = implode(" ", $wlcmd);
2194
	mwexec("/sbin/ifconfig {$if} $wlcmd_args", false);
2195
2196 2a203afd Seth Mos
	
2197
	sleep(1);
2198
	/* execute hostapd and wpa_supplicant if required in shell */
2199 6955830f Ermal Lu?i
	mwexec("/bin/sh {$g['tmp_path']}/{$if}_setup.sh");
2200 191a8175 Scott Ullrich
2201 ac3f8318 Espen Johansen
	return 0;
2202 cfc707f7 Scott Ullrich
2203 5b237745 Scott Ullrich
}
2204
2205 eba938e3 Scott Ullrich
function kill_hostapd($interface) {
2206 6f76920c thompsa
	return "/bin/pkill -f \"hostapd .*{$interface}\"\n";
2207 4b2a6180 Scott Ullrich
}
2208
2209 eba938e3 Scott Ullrich
function kill_wpasupplicant($interface) {
2210 6f76920c thompsa
	return "/bin/pkill -f \"wpa_supplicant .*{$interface}\"\n";
2211 4b2a6180 Scott Ullrich
}
2212
2213 eba938e3 Scott Ullrich
function find_dhclient_process($interface) {
2214 79d3a7cf Ermal Lu?i
	if($interface) {
2215 6f76920c thompsa
		$pid = `/bin/pgrep -xf "dhclient: {$interface}"`;
2216 79d3a7cf Ermal Lu?i
	}
2217 0311dbd5 Scott Ullrich
	return $pid;
2218
}
2219
2220 a5d6f60b Ermal Lu?i
function interface_configure($interface = "wan", $reloadall = false) {
2221 675aac3d Ermal Luçi
	global $config, $g;
2222 31b24870 Ermal Luçi
	global $interface_sn_arr_cache, $interface_ip_arr_cache;
2223 cfc707f7 Scott Ullrich
2224 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
2225
2226 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
2227 cfc707f7 Scott Ullrich
2228 28d22199 Scott Ullrich
	if (!$g['booting']) {
2229 acc1e9d0 Scott Ullrich
		/* remove all IPv4 addresses */
2230 332683cb Seth Mos
		while (mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " -alias", true) == 0);
2231 5c49cf58 Ermal
			interface_bring_down($interface, true);
2232 28d22199 Scott Ullrich
	}
2233 acc1e9d0 Scott Ullrich
2234 5b237745 Scott Ullrich
	/* wireless configuration? */
2235
	if (is_array($wancfg['wireless']))
2236 19e83210 Scott Ullrich
		interface_wireless_configure($realif, $wancfg, $wancfg['wireless']);
2237 cfc707f7 Scott Ullrich
2238 f36d4bd2 Scott Ullrich
	if ($wancfg['spoofmac']) {
2239 b99256c1 Scott Ullrich
		mwexec("/sbin/ifconfig " . escapeshellarg($realif) .
2240 5b237745 Scott Ullrich
			" link " . escapeshellarg($wancfg['spoofmac']));
2241 ac8ff0a4 Ermal
2242 871768cf Ermal
                /*
2243
                 * All vlans need to spoof their parent mac address, too.  see
2244
                 * ticket #1514: http://cvstrac.pfsense.com/tktview?tn=1514,33
2245
                 */
2246
                if (is_array($config['vlans']['vlan'])) {
2247
                        foreach ($config['vlans']['vlan'] as $vlan) {
2248
                                if ($vlan['if'] == $realif)
2249
                                        mwexec("/sbin/ifconfig " . escapeshellarg($vlan['vlanif']) .
2250
                                                " link " . escapeshellarg($wancfg['spoofmac']));
2251
                        }
2252
                }
2253 f36d4bd2 Scott Ullrich
	}  else {
2254 3ad5fd63 Ermal
		$mac = get_interface_mac(get_real_interface($wancfg['if']));
2255 f36d4bd2 Scott Ullrich
		if($mac == "ff:ff:ff:ff:ff:ff") {
2256
			/*   this is not a valid mac address.  generate a
2257
			 *   temporary mac address so the machine can get online.
2258
			 */
2259 9315ef83 Scott Ullrich
			echo "Generating new MAC address.";
2260 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
2261 b99256c1 Scott Ullrich
			mwexec("/sbin/ifconfig " . escapeshellarg(get_real_interface($wancfg['if'])) .
2262 f36d4bd2 Scott Ullrich
				" link " . escapeshellarg($random_mac));
2263
			$wancfg['spoofmac'] = $random_mac;
2264
			write_config();
2265 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");
2266 f36d4bd2 Scott Ullrich
		}
2267
	}
2268 cfc707f7 Scott Ullrich
2269 5b237745 Scott Ullrich
	/* media */
2270
	if ($wancfg['media'] || $wancfg['mediaopt']) {
2271 b99256c1 Scott Ullrich
		$cmd = "/sbin/ifconfig " . escapeshellarg(get_real_interface($wancfg['if']));
2272 5b237745 Scott Ullrich
		if ($wancfg['media'])
2273
			$cmd .= " media " . escapeshellarg($wancfg['media']);
2274
		if ($wancfg['mediaopt'])
2275
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
2276
		mwexec($cmd);
2277
	}
2278 e57a441e Ermal Lu?i
	if (!empty($wancfg['mtu']))
2279 871768cf Ermal
		pfSense_interface_mtu($realif, $wancfg['mtu']);
2280 cfc707f7 Scott Ullrich
2281 31b24870 Ermal Luçi
	/* invalidate interface/ip/sn cache */
2282 eba938e3 Scott Ullrich
	get_interface_arr(true);
2283 31b24870 Ermal Luçi
	unset($interface_ip_arr_cache[$realif]);
2284
	unset($interface_sn_arr_cache[$realif]);
2285 ccbd2447 Ermal Luçi
2286 5b237745 Scott Ullrich
	switch ($wancfg['ipaddr']) {
2287 d5d00b83 Scott Ullrich
		case 'carpdev-dhcp':
2288 1fb7c265 Ermal Luçi
			interface_carpdev_dhcp_configure($interface);
2289 d5d00b83 Scott Ullrich
			break;
2290 5b237745 Scott Ullrich
		case 'dhcp':
2291 1fb7c265 Ermal Luçi
			interface_dhcp_configure($interface);
2292 5b237745 Scott Ullrich
			break;
2293
		case 'pppoe':
2294 8af6c46d gnhb
		case 'l2tp':
2295 5b237745 Scott Ullrich
		case 'pptp':
2296 9ebe7028 gnhb
		case 'ppp':
2297 64d124c5 gnhb
			interface_ppps_configure($interface);
2298 9ebe7028 gnhb
			break;
2299 5b237745 Scott Ullrich
		default:
2300 4b176ed2 Ermal Luçi
			if ($wancfg['ipaddr'] <> "" && $wancfg['subnet'] <> "") {
2301 871768cf Ermal
				pfSense_interface_setaddress($realif, "{$wancfg['ipaddr']}/{$wancfg['subnet']}");
2302 d1eea523 Ermal
			} else if (substr($realif, 0, 3) == "gre") {
2303
				if (is_array($config['gres']['gre'])) {
2304
					foreach ($config['gres']['gre'] as $gre)
2305
						if ($gre['greif'] == $realif)
2306
							interface_gre_configure($gre);
2307
				}
2308
			} else if (substr($realif, 0, 3) == "gif") {
2309
				 if (is_array($config['gifs']['gif'])) {
2310
					foreach ($config['gifs']['gif'] as $gif)
2311
						if($gif['gifif'] == $interface)
2312
							interface_gif_configure($gif);
2313
				}
2314
			} else if (substr($realif, 0, 4) == "ovpn") {
2315
				/* XXX: Should be done anything?! */
2316 acc1e9d0 Scott Ullrich
			}
2317
2318 5e041d5f Scott Ullrich
			if (is_ipaddr($wancfg['gateway']))
2319 6955830f Ermal Lu?i
				file_put_contents("{$g['tmp_path']}/{$realif}_router", $wancfg['gateway']);
2320 d1eea523 Ermal
			break;
2321 5b237745 Scott Ullrich
	}
2322 ffeb5acf Scott Ullrich
2323 435f11c8 Ermal Lu?i
	if(does_interface_exist($wancfg['if']))
2324 7284d850 Scott Ullrich
		interfaces_bring_up($wancfg['if']);
2325 3d8237f4 sullrich
 	
2326 f700f8da Ermal Lu?i
	if (!$g['booting'])
2327
		interface_reload_carps($realif);
2328 3d8237f4 sullrich
	
2329 5b237745 Scott Ullrich
	if (!$g['booting']) {
2330 ccbd2447 Ermal Luçi
		if (link_interface_to_gre($interface)) {
2331
			foreach ($config['gres']['gre'] as $gre)
2332
				if ($gre['if'] == $interface)
2333
					interface_gre_configure($gre);
2334
		}
2335
		if (link_interface_to_gif($interface)) {
2336
                	foreach ($config['gifs']['gif'] as $gif)
2337
				if ($gif['if'] == $interface)
2338 c0481e1c Ermal Lu?i
                        		interface_gif_configure($gif);
2339 ccbd2447 Ermal Luçi
        	}
2340
		if (link_interface_to_bridge($interface)) {
2341
			foreach ($config['bridges']['bridged'] as $bridge)
2342
				if (stristr($bridge['members'], "{$interface}"))
2343 fcd4a425 Ermal Lu?i
					interface_bridge_add_member($bridge['bridgeif'], $realif);
2344 ccbd2447 Ermal Luçi
		}
2345
2346 7850de1c Ermal Lu?i
		link_interface_to_vips($interface, "update");
2347 e5ac67ed Ermal Lu?i
2348 a5d6f60b Ermal Lu?i
		if ($interface == "lan")
2349 4476d447 Ermal Luçi
			/* make new hosts file */
2350 ffeb5acf Scott Ullrich
			system_hosts_generate();
2351 4476d447 Ermal Luçi
2352 a5d6f60b Ermal Lu?i
		if ($reloadall == true) {
2353 cfc707f7 Scott Ullrich
2354 a5d6f60b Ermal Lu?i
			/* reconfigure static routes (kernel may have deleted them) */
2355 1ea67f2e Ermal
			system_routing_configure($interface);
2356 cfc707f7 Scott Ullrich
2357 a5d6f60b Ermal Lu?i
			/* reload ipsec tunnels */
2358
			vpn_ipsec_configure();
2359 cfc707f7 Scott Ullrich
2360 a5d6f60b Ermal Lu?i
			/* update dyndns */
2361 2ec2a374 Ermal Lu?i
			services_dyndns_configure($interface);
2362 cfc707f7 Scott Ullrich
2363 a5d6f60b Ermal Lu?i
			/* force DNS update */
2364 2ec2a374 Ermal Lu?i
			services_dnsupdate_process($interface);
2365 a23d7248 Scott Ullrich
2366 a5d6f60b Ermal Lu?i
			/* restart dnsmasq */
2367
			services_dnsmasq_configure();
2368 eb772abd Scott Ullrich
2369 a5d6f60b Ermal Lu?i
			/* reload captive portal */
2370 769e254e Ermal
			captiveportal_init_rules();
2371 a5d6f60b Ermal Lu?i
2372
			/* set the reload filter dity flag */
2373
			filter_configure();
2374
		}
2375 5b237745 Scott Ullrich
	}
2376 cfc707f7 Scott Ullrich
2377 5b237745 Scott Ullrich
	return 0;
2378
}
2379
2380 eba938e3 Scott Ullrich
function interface_carpdev_dhcp_configure($interface = "wan") {
2381 d5d00b83 Scott Ullrich
	global $config, $g;
2382
2383 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
2384 499994ff Scott Ullrich
	$wanif = $wancfg['if'];
2385 d5d00b83 Scott Ullrich
	/* bring wan interface up before starting dhclient */
2386 d7147b1c Scott Ullrich
	if($wanif)
2387 b5b957fe Scott Ullrich
		interfaces_bring_up($wanif);
2388 d7147b1c Scott Ullrich
	else 
2389
		log_error("Could not bring wanif up in terface_carpdev_dhcp_configure()");
2390 d5d00b83 Scott Ullrich
2391
	return 0;
2392
}
2393
2394 eba938e3 Scott Ullrich
function interface_dhcp_configure($interface = "wan") {
2395 5b237745 Scott Ullrich
	global $config, $g;
2396 cfc707f7 Scott Ullrich
2397 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
2398 df9e93f0 Ermal
	if (empty($wancfg))
2399
		$wancfg = array();
2400 5b237745 Scott Ullrich
2401 0311dbd5 Scott Ullrich
	/* generate dhclient_wan.conf */
2402 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
2403 5b237745 Scott Ullrich
	if (!$fd) {
2404 67ee1ec5 Ermal Luçi
		printf("Error: cannot open dhclient_{$interface}.conf in interfaces_wan_dhcp_configure() for writing.\n");
2405 5b237745 Scott Ullrich
		return 1;
2406
	}
2407 eb772abd Scott Ullrich
2408 2305d4c5 Scott Ullrich
	if ($wancfg['dhcphostname']) {
2409
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
2410
		$dhclientconf_hostname .= "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
2411
	} else {
2412
		$dhclientconf_hostname = "";
2413
	}
2414
2415 85a5da13 Ermal Luçi
	$wanif = get_real_interface($interface);
2416 df9e93f0 Ermal
	if (empty($wanif)) {
2417
		log_error("Invalid interface \"{$interface}\" in interface_dhcp_configure()");
2418 c1cc447c gnhb
		return 0;
2419 3a906378 gnhb
	}
2420 67ee1ec5 Ermal Luçi
 	$dhclientconf = "";
2421
	
2422 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
2423 67ee1ec5 Ermal Luçi
interface "{$wanif}" {
2424 76d3b9a3 Chris Buechler
timeout 60;
2425 ce69a638 Scott Ullrich
retry 1;
2426
select-timeout 0;
2427
initial-interval 1;
2428 2305d4c5 Scott Ullrich
	{$dhclientconf_hostname}
2429
	script "/sbin/dhclient-script";
2430 5b237745 Scott Ullrich
}
2431
2432
EOD;
2433
2434 bc40d758 Seth Mos
if(is_ipaddr($wancfg['alias-address'])) {
2435
	$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
2436
	$dhclientconf .= <<<EOD
2437
alias {
2438 67ee1ec5 Ermal Luçi
	interface  "{$wanif}";
2439 bc40d758 Seth Mos
	fixed-address {$wancfg['alias-address']};
2440
	option subnet-mask {$subnetmask};
2441
}
2442
2443
EOD;
2444
}
2445 5b237745 Scott Ullrich
	fwrite($fd, $dhclientconf);
2446
	fclose($fd);
2447 eb772abd Scott Ullrich
2448 d7147b1c Scott Ullrich
	/* bring wan interface up before starting dhclient */
2449 3a906378 gnhb
	if($wanif)
2450
		interfaces_bring_up($wanif);
2451 b5b957fe Scott Ullrich
	else 
2452 3a906378 gnhb
		log_error("Could not bring up {$wanif} interface in interface_dhcp_configure()");
2453 eacc8c14 Scott Ullrich
2454 d7147b1c Scott Ullrich
	/* fire up dhclient */
2455 6955830f Ermal Lu?i
	mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif} > {$g['tmp_path']}/{$wanif}_output > {$g['tmp_path']}/{$wanif}_error_output");
2456 0119d2f7 Scott Ullrich
2457 5b237745 Scott Ullrich
	return 0;
2458
}
2459
2460 42753d25 Ermal Lu?i
function interfaces_group_setup() {
2461
	global $config;
2462
2463
	if (!is_array($config['ifgroups']['ifgroupentry']))
2464
		return;
2465
2466 482961e3 Ermal Lu?i
	foreach ($config['ifgroups']['ifgroupentry'] as $groupar)
2467 42753d25 Ermal Lu?i
		interface_group_setup($groupar);
2468
2469
	return;
2470
}
2471
2472 abcb2bed Ermal Lu?i
function interface_group_setup(&$groupname /* The parameter is an array */) {
2473 42753d25 Ermal Lu?i
	global $config;
2474
2475
	if (!is_array($groupname))
2476
		return;
2477
	$members = explode(" ", $groupname['members']);
2478
	foreach($members as $ifs) {
2479
		$realif = get_real_interface($ifs);
2480
		if ($realif)
2481
			mwexec("/sbin/ifconfig {$realif} group {$groupname['ifname']}");
2482
	}
2483
2484
	return;
2485
}
2486 f6b761fb Scott Ullrich
 
2487 e8910ad4 Ermal Lu?i
/* COMPAT Function */
2488 afb2de1b Ermal Lu?i
function convert_friendly_interface_to_real_interface_name($interface) {
2489
	return get_real_interface($interface);
2490
}
2491
2492 e8910ad4 Ermal Lu?i
/* COMPAT Function */
2493 eba938e3 Scott Ullrich
function get_real_wan_interface($interface = "wan") {
2494 abb31ea4 Ermal Luçi
	return get_real_interface($interface);
2495
}
2496 afb2de1b Ermal Lu?i
2497 e8910ad4 Ermal Lu?i
/* COMPAT Function */
2498 eba938e3 Scott Ullrich
function get_current_wan_address($interface = "wan") {
2499 abb31ea4 Ermal Luçi
	return get_interface_ip($interface);
2500
}
2501
2502 afb2de1b Ermal Lu?i
/*
2503
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
2504
 */
2505
function convert_real_interface_to_friendly_interface_name($interface = "wan") {
2506
        global $config;
2507
2508 bfbb9bc0 Ermal
	if (stristr($interface, "vip")) {
2509 6d5446a2 Ermal
                $index = intval(substr($interface, 3));
2510
                foreach ($config['virtualip']['vip'] as $counter => $vip) {
2511 564df7c2 Ermal Lu?i
                        if ($vip['mode'] == "carpdev-dhcp" || $vip['mode'] == "carp")  {
2512 6d5446a2 Ermal
                                if ($index == $counter)
2513 564df7c2 Ermal Lu?i
                                        return $vip['interface'];
2514
                        }
2515
                }
2516 afb2de1b Ermal Lu?i
        } else if (stristr($interface, "carp")) {
2517 6d5446a2 Ermal
                $index = intval(substr($interface, 4));
2518
                foreach ($config['virtualip']['vip'] as $counter => $vip) {
2519 afb2de1b Ermal Lu?i
                        if ($vip['mode'] == "carpdev-dhcp" || $vip['mode'] == "carp")  {
2520 6d5446a2 Ermal
                                if ($index == $counter)
2521 afb2de1b Ermal Lu?i
                                        return $vip['interface'];
2522
                        }
2523
                }
2524
        }
2525
2526 6d5446a2 Ermal
        /* XXX: For speed reasons reference directly the interface array */
2527
	$ifdescrs = $config['interfaces'];
2528
        //$ifdescrs = get_configured_interface_list(false, true);
2529 afb2de1b Ermal Lu?i
2530
        foreach ($ifdescrs as $if => $ifname) {
2531 6d5446a2 Ermal
                if ($config['interfaces'][$if]['if'] == $interface)
2532
                        return $if;
2533 afb2de1b Ermal Lu?i
2534
                /* XXX: ermal - The 3 lines below are totally bogus code. */
2535 532b0fb8 Ermal Lu?i
                $int = interface_translate_type_to_real($if);
2536 6d5446a2 Ermal
                if ($int == $interface)
2537 afb2de1b Ermal Lu?i
                        return $ifname;
2538
        }
2539
        return NULL;
2540
}
2541
2542
/* attempt to resolve interface to friendly descr */
2543
function convert_friendly_interface_to_friendly_descr($interface) {
2544
        global $config;
2545
2546
        switch ($interface) {
2547
                case "l2tp":
2548
                                $ifdesc = "L2TP";
2549
                                break;
2550
                case "pptp":
2551
                                $ifdesc = "pptp";
2552
                                break;
2553
                case "pppoe":
2554
                                $ifdesc = "pppoe";
2555
                                break;
2556
                case "openvpn":
2557
                                $ifdesc = "OpenVPN";
2558
                                break;
2559
                case "enc0":
2560
                        case "ipsec":
2561
                                $ifdesc = "IPsec";
2562
                                break;
2563
        default:
2564
                /* if list */
2565
                $ifdescrs = get_configured_interface_with_descr(false, true);
2566
                foreach ($ifdescrs as $if => $ifname) {
2567
                                if ($if == $interface || $ifname == $interface)
2568
                                        return $ifname;
2569
                }
2570
                break;
2571
        }
2572
2573
        return $ifdesc;
2574
}
2575
2576
function convert_real_interface_to_friendly_descr($interface) {
2577
        global $config;
2578
2579
        $ifdesc = convert_real_interface_to_friendly_interface_name("{$interface}");
2580
2581
        if ($ifdesc) {
2582 c795339e Ermal Lu?i
                $iflist = get_configured_interface_with_descr(false, true);
2583 afb2de1b Ermal Lu?i
                return $iflist[$ifdesc];
2584
        }
2585
2586
        return $interface;
2587
}
2588
2589 532b0fb8 Ermal Lu?i
/*
2590
 *  interface_translate_type_to_real($interface):
2591
 *              returns the real hardware interface name for a friendly interface.  ie: wan
2592
 */
2593
function interface_translate_type_to_real($interface) {
2594
        global $config;
2595
2596
        if ($config['interfaces'][$interface]['if'] <> "")
2597
                return $config['interfaces'][$interface]['if'];
2598
        else
2599
		return $interface;
2600
}
2601
2602 263e2b7e Erik Fonnesbeck
function interface_is_wireless_clone($wlif) {
2603
	if(!stristr($wlif, "_wlan")) {
2604
		return false;
2605
	} else {
2606
		return true;
2607
	}
2608
}
2609
2610 1d072761 Erik Fonnesbeck
function interface_get_wireless_base($wlif) {
2611 34808d4e Erik Fonnesbeck
	if(!stristr($wlif, "_wlan")) {
2612
		return $wlif;
2613
	} else {
2614
		return substr($wlif, 0, stripos($wlif, "_wlan"));
2615
	}
2616
}
2617
2618 1d072761 Erik Fonnesbeck
function interface_get_wireless_clone($wlif) {
2619 34808d4e Erik Fonnesbeck
	if(!stristr($wlif, "_wlan")) {
2620
		return $wlif . "_wlan0";
2621
	} else {
2622
		return $wlif;
2623
	}
2624
}
2625
2626 df9e93f0 Ermal
function get_real_interface($interface = "wan") {
2627 67ee1ec5 Ermal Luçi
    global $config;
2628 cfc707f7 Scott Ullrich
2629 521cfa2f Ermal Lu?i
	$wanif = NULL;
2630 c515ea57 Scott Ullrich
2631 67ee1ec5 Ermal Luçi
	switch ($interface) {
2632 acc1e9d0 Scott Ullrich
	case "l2tp":
2633
		$wanif = "l2tp";
2634
		break;
2635 67ee1ec5 Ermal Luçi
	case "pptp":
2636
		$wanif = "pptp";
2637
		break;
2638
	case "pppoe":
2639
		$wanif = "pppoe";
2640
		break;
2641
	case "openvpn":
2642
		$wanif = "openvpn";
2643
		break;
2644 4563d12f Seth Mos
	case "ipsec":
2645 67ee1ec5 Ermal Luçi
	case "enc0":
2646
		$wanif = "enc0";
2647
		break;
2648
	case "ppp":
2649
		$wanif = "ppp";
2650
		break;
2651
	default:
2652 6d5446a2 Ermal
		// If a real interface was alread passed simply
2653
		// pass the real interface back.  This encourages
2654
		// the usage of this function in more cases so that
2655
		// we can combine logic for more flexibility.
2656
		if(does_interface_exist($interface)) {
2657
			$wanif = $interface;
2658
			break;
2659
		}
2660
		if (empty($config['interfaces'][$interface]))
2661
			break;
2662 568b1358 Scott Ullrich
2663 6d5446a2 Ermal
		$cfg =& $config['interfaces'][$interface];
2664 2ebf3945 Scott Ullrich
2665 6d5446a2 Ermal
		// Wireless cloned NIC support (FreeBSD 8+)
2666
		// interface name format: $parentnic_wlanparentnic#
2667
		// example: ath0_wlan0
2668
		if (is_interface_wireless($cfg['if'])) {
2669
			$wanif = interface_get_wireless_clone($cfg['if']);
2670
			break;
2671
		}
2672
		/*
2673
		if (empty($cfg['if'])) {
2674
			$wancfg = $cfg['if'];
2675
			break;
2676
		}
2677
		*/
2678 e7693c09 Ermal Lu?i
2679 6d5446a2 Ermal
		switch ($cfg['ipaddr']) {
2680
			case "carpdev-dhcp":
2681
				$viparr = &$config['virtualip']['vip'];
2682
				if(is_array($viparr))
2683
				foreach ($viparr as $counter => $vip) {
2684
					if ($vip['mode'] == "carpdev-dhcp") {
2685
						if($vip['interface'] == $interface) {
2686
							$wanif = "carp{$counter}";
2687
							break;
2688 3a906378 gnhb
						}
2689
					}
2690
				}
2691 b99256c1 Scott Ullrich
				break;
2692 6d5446a2 Ermal
			case "pppoe": 
2693
			case "pptp": 
2694
			case "l2tp": 
2695
			case "ppp":
2696 277d0250 gnhb
				$wanif = $cfg['if'];
2697 6d5446a2 Ermal
				break;
2698
			default:
2699
				$wanif = $cfg['if'];
2700
				break;
2701 c515ea57 Scott Ullrich
		}
2702 67ee1ec5 Ermal Luçi
		break;
2703 c515ea57 Scott Ullrich
	}
2704
2705 67ee1ec5 Ermal Luçi
    return $wanif;
2706 5b237745 Scott Ullrich
}
2707
2708 9ff8c299 Seth Mos
/* Guess the physical interface by providing a IP address */
2709 afb2de1b Ermal Lu?i
function guess_interface_from_ip($ipaddress) {
2710 80a2c1e6 Seth Mos
	if(! is_ipaddr($ipaddress)) {
2711 9ff8c299 Seth Mos
		return false;
2712
	}
2713
	/* create a route table we can search */
2714 629208a2 Ermal Lu?i
	exec("netstat -rnW", $output, $ret);
2715 9ff8c299 Seth Mos
	foreach($output as $line) {
2716
		if(preg_match("/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+[ ]+link[#]/", $line)) {
2717
			$fields = preg_split("/[ ]+/", $line);
2718
			if(ip_in_subnet($ipaddress, $fields[0])) {
2719 629208a2 Ermal Lu?i
				return $fields[6];
2720 9ff8c299 Seth Mos
			}
2721
		}
2722
	}
2723
	$ret = exec_command("/sbin/route -n get {$ipaddress} | /usr/bin/awk '/interface/ { print \$2; };'");
2724
	if(empty($ret)) {
2725
        	return false;
2726
	}
2727
	return $ret;
2728 afb2de1b Ermal Lu?i
}
2729
2730
/*
2731
 * find_ip_interface($ip): return the interface where an ip is defined
2732
 */
2733
function find_ip_interface($ip)
2734
{
2735
        /* if list */
2736
        $ifdescrs = get_configured_interface_list();
2737
2738
        foreach ($ifdescrs as $ifdescr => $ifname) {
2739 abcb2bed Ermal Lu?i
		if ($ip == get_interface_ip($ifname)) {
2740
                	$int = get_real_interface($ifname);
2741
			return $int;
2742
		}
2743 afb2de1b Ermal Lu?i
        }
2744
        return false;
2745
}
2746
2747 a71b32d2 Scott Ullrich
/*
2748
 *   find_number_of_created_carp_interfaces: return the number of carp interfaces
2749
 */
2750
function find_number_of_created_carp_interfaces() {
2751
	return `/sbin/ifconfig | grep "carp:" | wc -l`;
2752
}
2753
2754
function get_all_carp_interfaces() {
2755
	$ints = str_replace("\n", " ", `ifconfig | grep "carp:" -B2 | grep ": flag" | cut -d: -f1`);
2756
	return $ints;
2757
}
2758
2759 abcb2bed Ermal Lu?i
/*
2760
 * find_carp_interface($ip): return the carp interface where an ip is defined
2761
 */
2762
function find_carp_interface($ip) {
2763 27625b39 Scott Ullrich
	global $config;
2764 abcb2bed Ermal Lu?i
	if (is_array($config['virtualip']['vip'])) {
2765
		foreach ($config['virtualip']['vip'] as $vip) {
2766
			if ($vip['mode'] == "carp" || $vip['mode'] == "carpdev") {
2767 564df7c2 Ermal Lu?i
				$carp_ip = get_interface_ip($vip['interface']);
2768 c55e4580 Chris Buechler
				$if = `ifconfig | grep '$ip ' -B1 | head -n1 | cut -d: -f1`;
2769 27625b39 Scott Ullrich
				if ($if)
2770
					return $if;
2771 abcb2bed Ermal Lu?i
			}
2772
		}
2773
	}
2774
}
2775
2776
function link_carp_interface_to_parent($interface) {
2777
        global $config;
2778
2779
        if ($interface == "")
2780
                return;
2781
2782 564df7c2 Ermal Lu?i
        $carp_ip = get_interface_ip($interface);
2783 abcb2bed Ermal Lu?i
        if (!is_ipaddr($carp_ip))
2784
                return;
2785
2786
        /* if list */
2787
        $ifdescrs = get_configured_interface_list();
2788
        foreach ($ifdescrs as $ifdescr => $ifname) {
2789
                $interfaceip = get_interface_ip($ifname);
2790
                $subnet_bits = get_interface_subnet($ifname);
2791
                $subnet_ip = gen_subnet("{$interfaceip}", "{$subnet_bits}");
2792
                if(ip_in_subnet($carp_ip, "{$subnet_ip}/{$subnet_bits}"))
2793
                        return $ifname;
2794
        }
2795
2796
        return "";
2797
}
2798
2799
/****f* interfaces/link_ip_to_carp_interface
2800
 * NAME
2801
 *   link_ip_to_carp_interface - Find where a CARP interface links to.
2802
 * INPUTS
2803
 *   $ip
2804
 * RESULT
2805
 *   $carp_ints
2806
 ******/
2807
function link_ip_to_carp_interface($ip) {
2808
        global $config;
2809
2810
        if (!is_ipaddr($ip))
2811
                return;
2812
2813
        $carp_ints = "";
2814
        if (is_array($config['virtualip']['vip'])) {
2815 1d002dc9 Ermal
		$first = 0;
2816 3fbc3487 Ermal
		$carp_int = array();
2817 abcb2bed Ermal Lu?i
                foreach ($config['virtualip']['vip'] as $vip) {
2818
                        if ($vip['mode'] == "carp" || $vip['mode'] == "carpdev") {
2819 6b060a2f Scott Ullrich
                                $carp_ip = $vip['subnet'];
2820 abcb2bed Ermal Lu?i
                                $carp_sn = $vip['subnet_bits'];
2821
                                $carp_nw = gen_subnet($carp_ip, $carp_sn);
2822 3fbc3487 Ermal
                                if (ip_in_subnet($ip, "{$carp_nw}/{$carp_sn}"))
2823
					$carp_int[] = "vip{$vip['vhid']}";
2824 abcb2bed Ermal Lu?i
                        }
2825
                }
2826 3fbc3487 Ermal
		if (!empty($carp_int))
2827
			$carp_ints = implode(" ", array_unique($carp_int));
2828 abcb2bed Ermal Lu?i
        }
2829
2830
        return $carp_ints;
2831
}
2832
2833 7850de1c Ermal Lu?i
function link_interface_to_vlans($int, $action = "") {
2834
	global $config;
2835
2836
	if (empty($int))
2837
		return;
2838
2839
	if (is_array($config['vlans']['vlan'])) {
2840
                foreach ($config['vlans']['vlan'] as $vlan) {
2841 fa4a331f Ermal
			if ($int == $vlan['if']) {
2842 7850de1c Ermal Lu?i
				if ($action == "update") {
2843 fa4a331f Ermal
					interfaces_bring_up($int);
2844 7850de1c Ermal Lu?i
				} else if ($action == "")
2845
					return $vlan;
2846
			}
2847
		}
2848
	}
2849
}
2850
2851
function link_interface_to_vips($int, $action = "") {
2852 e5ac67ed Ermal Lu?i
        global $config;
2853
2854
        if (is_array($config['virtualip']['vip']))
2855
                foreach ($config['virtualip']['vip'] as $vip)
2856 7850de1c Ermal Lu?i
                        if ($int == $vip['interface']) {
2857
				if ($action == "update")
2858
					interfaces_vips_configure($int);
2859
				else
2860
                                	return $vip;
2861
			}
2862 e5ac67ed Ermal Lu?i
}
2863
2864 afb2de1b Ermal Lu?i
/****f* interfaces/link_interface_to_bridge
2865
 * NAME
2866
 *   link_interface_to_bridge - Finds out a bridge group for an interface
2867
 * INPUTS
2868
 *   $ip
2869
 * RESULT
2870
 *   bridge[0-99]
2871
 ******/
2872
function link_interface_to_bridge($int) {
2873
        global $config;
2874
2875
        if (is_array($config['bridges']['bridged']))
2876
                foreach ($config['bridges']['bridged'] as $bridge)
2877
                        if(stristr($bridge['members'], "{$int}"))
2878
                                return "{$bridge['bridgeif']}";
2879
}
2880
2881
function link_interface_to_gre($interface) {
2882
        global $config;
2883
2884
        if (is_array($config['gres']['gre']))
2885
                foreach ($config['gres']['gre'] as $gre)
2886
                        if($gre['if'] == $interface)
2887
                                return "{$gre['greif']}";
2888
}
2889
2890
function link_interface_to_gif($interface) {
2891
        global $config;
2892
2893
        if (is_array($config['gifs']['gif']))
2894
                foreach ($config['gifs']['gif'] as $gif)
2895
                        if($gif['if'] == $interface)
2896
                                return "{$gif['gifif']}";
2897
}
2898
2899
/*
2900
 * find_interface_ip($interface): return the interface ip (first found)
2901
 */
2902
function find_interface_ip($interface, $flush = false)
2903
{
2904
	global $interface_ip_arr_cache;
2905
2906
	$interface = str_replace("\n", "", $interface);
2907 00380613 Scott Ullrich
	
2908 8256f324 gnhb
	if (!does_interface_exist($interface))
2909 afb2de1b Ermal Lu?i
		return;
2910
2911
	/* Setup IP cache */
2912
	if (!isset($interface_ip_arr_cache[$interface]) or $flush) {
2913 3f70e618 Ermal Lu?i
		$ifinfo = pfSense_get_interface_addresses($interface);
2914
		$interface_ip_arr_cache[$interface] = $ifinfo['ipaddr'];
2915 afb2de1b Ermal Lu?i
	}
2916
2917
	return $interface_ip_arr_cache[$interface];
2918
}
2919
2920
function find_interface_subnet($interface, $flush = false)
2921
{
2922
	global $interface_sn_arr_cache;
2923
2924
	$interface = str_replace("\n", "", $interface);
2925
	if (does_interface_exist($interface) == false)
2926
		return;
2927
2928
	if (!isset($interface_sn_arr_cache[$interface]) or $flush) {
2929 bd96e1fe Ermal Lu?i
		$ifinfo = pfSense_get_interface_addresses($interface);
2930
		$interface_sn_arr_cache[$interface] = $ifinfo['subnetbits'];
2931 afb2de1b Ermal Lu?i
        }
2932
2933
	return $interface_sn_arr_cache[$interface];
2934
}
2935
2936 e88fbe50 Ermal Lu?i
function get_interface_ip($interface = "wan")
2937
{
2938 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
2939 afb2de1b Ermal Lu?i
	if (!$realif) {
2940
		if (preg_match("/^carp/i", $interface))
2941
			$realif = $interface;
2942 564df7c2 Ermal Lu?i
		else if (preg_match("/^vip/i", $interface))
2943
			$realif = $interface;
2944 afb2de1b Ermal Lu?i
		else
2945
			return null;
2946
	}
2947
2948 5e041d5f Scott Ullrich
	$curip = find_interface_ip($realif);
2949
	if ($curip && is_ipaddr($curip) && ($curip != "0.0.0.0"))
2950
		return $curip;
2951 8256f324 gnhb
	else
2952
		return null;
2953 5b237745 Scott Ullrich
}
2954
2955 e88fbe50 Ermal Lu?i
function get_interface_subnet($interface = "wan")
2956
{
2957 31b24870 Ermal Luçi
	$realif = get_real_interface($interface);
2958 e88fbe50 Ermal Lu?i
	if (!$realif) {
2959
                if (preg_match("/^carp/i", $interface))
2960
                        $realif = $interface;
2961 564df7c2 Ermal Lu?i
                else if (preg_match("/^vip/i", $interface))
2962
                        $realif = $interface;
2963 e88fbe50 Ermal Lu?i
                else
2964
                        return null;
2965
        }
2966
2967 5e041d5f Scott Ullrich
	$cursn = find_interface_subnet($realif);
2968
	if (!empty($cursn))
2969 31b24870 Ermal Luçi
		return $cursn;
2970
2971
	return null;
2972
}
2973
2974 52947718 Ermal Lu?i
/* return outside interfaces with a gateway */
2975
function get_interfaces_with_gateway() {
2976 77ccab82 Scott Ullrich
	global $config;
2977 52947718 Ermal Lu?i
2978
	$ints = array();
2979
2980
	/* loop interfaces, check config for outbound */
2981 77ccab82 Scott Ullrich
	foreach($config['interfaces'] as $ifdescr => $ifname) {
2982 9ebe7028 gnhb
2983 77ccab82 Scott Ullrich
		switch ($ifname['ipaddr']) {
2984
			case "dhcp":
2985
			case "carpdev-dhcp":
2986 39f750b5 gnhb
			case "ppp";
2987 77ccab82 Scott Ullrich
			case "pppoe":
2988
			case "pptp":
2989 6d5446a2 Ermal
			case "l2tp":
2990 9ebe7028 gnhb
			case "ppp";
2991 6d5446a2 Ermal
				$ints[$ifdescr] = $ifdescr;
2992 77ccab82 Scott Ullrich
			break;
2993
			default:
2994 611ae852 Ermal
				if (!empty($ifname['gateway']))
2995 6d5446a2 Ermal
					$ints[$ifdescr] = $ifdescr;
2996 77ccab82 Scott Ullrich
			break;
2997
		}
2998
	}
2999
	return $ints;
3000 52947718 Ermal Lu?i
}
3001
3002
/* return true if interface has a gateway */
3003
function interface_has_gateway($friendly) {
3004 6d5446a2 Ermal
	global $config;
3005 52947718 Ermal Lu?i
3006 6d5446a2 Ermal
	if (!empty($config['interfaces'][$friendly])) {
3007
		$ifname =& $config['interfaces'][$friendly];
3008
		switch ($ifname['ipaddr']) {
3009
			case "dhcp":
3010
			case "carpdev-dhcp":
3011
			case "pppoe":
3012
			case "pptp":
3013
			case "l2tp":
3014
			case "ppp";
3015
				return true;
3016
			break;
3017
			default:
3018
				if (!empty($ifname['gateway']))
3019
					return true;
3020
			break;
3021
		}
3022
	}
3023 52947718 Ermal Lu?i
3024
	return false;
3025
}
3026
3027 a57b119e Bill Marquette
/****f* interfaces/is_altq_capable
3028
 * NAME
3029
 *   is_altq_capable - Test if interface is capable of using ALTQ
3030
 * INPUTS
3031
 *   $int            - string containing interface name
3032
 * RESULT
3033
 *   boolean         - true or false
3034
 ******/
3035
3036 eba938e3 Scott Ullrich
function is_altq_capable($int) {
3037 a57b119e Bill Marquette
        /* Per:
3038 64fe3233 Seth Mos
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+7.2-current&format=html
3039 a57b119e Bill Marquette
         * Only the following drivers have ALTQ support
3040
         */
3041 64fe3233 Seth Mos
	$capable = array("age", "ale", "an", "ath", "aue", "awi", "bce",
3042
			"bfe", "bge", "dc", "de", "ed", "em", "ep", "fxp", "gem",
3043
			"hme", "ipw", "iwi", "jme", "le", "msk", "mxge", "my", "nfe",
3044
			"npe", "nve", "ral", "re", "rl", "rum", "sf", "sis", "sk",
3045
			"ste", "stge", "txp", "udav", "ural", "vge", "vr", "wi", "xl",
3046 58507f89 Ermal Lu?i
			"ndis", "tun", "ovpns", "ovpnc", "vlan", "pppoe", "pptp", "ng", "ppp");
3047 a57b119e Bill Marquette
3048
        $int_family = preg_split("/[0-9]+/", $int);
3049
3050
        if (in_array($int_family[0], $capable))
3051
                return true;
3052 2f3446db Ermal Lu?i
	else if (stristr($int_family, "vlan")) /* VLANs are name $parent.$vlan now */
3053
		return true;
3054 a57b119e Bill Marquette
        else
3055
                return false;
3056
}
3057
3058 52947718 Ermal Lu?i
/****f* interfaces/is_interface_wireless
3059
 * NAME
3060
 *   is_interface_wireless - Returns if an interface is wireless
3061
 * RESULT
3062
 *   $tmp       - Returns if an interface is wireless
3063
 ******/
3064
function is_interface_wireless($interface) {
3065
        global $config, $g;
3066
3067
        $friendly = convert_real_interface_to_friendly_interface_name($interface);
3068 10394059 Scott Ullrich
        if(!isset($config['interfaces'][$friendly]['wireless'])) {
3069 52947718 Ermal Lu?i
                if (preg_match($g['wireless_regex'], $interface)) {
3070
                        $config['interfaces'][$friendly]['wireless'] = array();
3071
                        return true;
3072
                }
3073
                unset($config['interfaces'][$friendly]['wireless']);
3074
                return false;
3075
        } else
3076
                return true;
3077
}
3078
3079 eba938e3 Scott Ullrich
function get_wireless_modes($interface) {
3080 d8c67d69 Scott Ullrich
	/* return wireless modes and channels */
3081 92f7d37d Ermal Luçi
	$wireless_modes = array();
3082
3083 1b773d20 Ermal Lu?i
	$wlif = interface_translate_type_to_real($interface);
3084
3085
	if(is_interface_wireless($wlif)) {
3086 10394059 Scott Ullrich
		$cloned_interface = get_real_interface($interface);
3087 1b773d20 Ermal Lu?i
		$chan_list = "/sbin/ifconfig {$cloned_interface} list chan";
3088
		$stack_list = "/usr/bin/awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
3089 1de74081 Ermal Lu?i
		$format_list = "/usr/bin/awk '{print \$5 \" \" \$6 \",\" \$1}'";
3090 d8c67d69 Scott Ullrich
3091 4b0e71db Scott Ullrich
		$interface_channels = "";
3092 d8c67d69 Scott Ullrich
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
3093
		$interface_channel_count = count($interface_channels);
3094
3095
		$c = 0;
3096
		while ($c < $interface_channel_count)
3097
		{
3098
			$channel_line = explode(",", $interface_channels["$c"]);
3099
			$wireless_mode = trim($channel_line[0]);
3100
			$wireless_channel = trim($channel_line[1]);
3101 4066776d Scott Ullrich
			if(trim($wireless_mode) != "") {
3102
				/* if we only have 11g also set 11b channels */
3103
				if($wireless_mode == "11g") {
3104 1ae54336 Erik Fonnesbeck
					if(!isset($wireless_modes["11b"]))
3105
						$wireless_modes["11b"] = array();
3106 39c1349c Erik Fonnesbeck
				} else if($wireless_mode == "11g ht") {
3107 1ae54336 Erik Fonnesbeck
					if(!isset($wireless_modes["11b"]))
3108
						$wireless_modes["11b"] = array();
3109
					if(!isset($wireless_modes["11g"]))
3110
						$wireless_modes["11g"] = array();
3111 39c1349c Erik Fonnesbeck
					$wireless_mode = "11ng";
3112
				} else if($wireless_mode == "11a ht") {
3113 1ae54336 Erik Fonnesbeck
					if(!isset($wireless_modes["11a"]))
3114
						$wireless_modes["11a"] = array();
3115 39c1349c Erik Fonnesbeck
					$wireless_mode = "11na";
3116 4066776d Scott Ullrich
				}
3117
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
3118
			}
3119 d8c67d69 Scott Ullrich
			$c++;
3120
		}
3121
	}
3122 4066776d Scott Ullrich
	return($wireless_modes);
3123 d8c67d69 Scott Ullrich
}
3124
3125 f4094f0d Erik Fonnesbeck
/* return channel numbers, frequency, max txpower, and max regulation txpower */
3126
function get_wireless_channel_info($interface) {
3127
	$wireless_channels = array();
3128
3129
	$wlif = interface_translate_type_to_real($interface);
3130
3131
	if(is_interface_wireless($wlif)) {
3132
		$cloned_interface = get_real_interface($interface);
3133
		$chan_list = "/sbin/ifconfig {$cloned_interface} list txpower";
3134
		$stack_list = "/usr/bin/awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
3135
		$format_list = "/usr/bin/awk '{print \$1 \",\" \$3 \" \" \$4 \",\" \$5 \",\" \$7}'";
3136
3137
		$interface_channels = "";
3138
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
3139
3140
		foreach ($interface_channels as $channel_line) {
3141
			$channel_line = explode(",", $channel_line);
3142
			if(!isset($wireless_channels[$channel_line[0]]))
3143
				$wireless_channels[$channel_line[0]] = $channel_line;
3144
		}
3145
	}
3146
	return($wireless_channels);
3147
}
3148
3149 52947718 Ermal Lu?i
/****f* interfaces/get_interface_mtu
3150
 * NAME
3151
 *   get_interface_mtu - Return the mtu of an interface
3152
 * RESULT
3153
 *   $tmp       - Returns the mtu of an interface
3154
 ******/
3155
function get_interface_mtu($interface) {
3156 bd96e1fe Ermal Lu?i
        $mtu = pfSense_get_interface_addresses($interface);
3157
        return $mtu['mtu'];
3158 52947718 Ermal Lu?i
}
3159
3160 eba938e3 Scott Ullrich
function get_interface_mac($interface) {
3161 7d6076f3 Ermal Lu?i
3162 3f70e618 Ermal Lu?i
	$macinfo = pfSense_get_interface_addresses($interface);
3163
	return $macinfo["macaddr"];
3164 f2ba47f8 Ermal Lu?i
}
3165
3166
/****f* pfsense-utils/generate_random_mac_address
3167
 * NAME
3168
 *   generate_random_mac - generates a random mac address
3169
 * INPUTS
3170
 *   none
3171
 * RESULT
3172
 *   $mac - a random mac address
3173
 ******/
3174
function generate_random_mac_address() {
3175
        $mac = "02";
3176
        for($x=0; $x<5; $x++)
3177
                $mac .= ":" . dechex(rand(16, 255));
3178
        return $mac;
3179 53c82ef9 Scott Ullrich
}
3180 b7ec2b9e Scott Ullrich
3181 52947718 Ermal Lu?i
/****f* interfaces/is_jumbo_capable
3182
 * NAME
3183
 *   is_jumbo_capable - Test if interface is jumbo frame capable.  Useful for determining VLAN capability.
3184
 * INPUTS
3185
 *   $int             - string containing interface name
3186
 * RESULT
3187
 *   boolean          - true or false
3188
 ******/
3189
function is_jumbo_capable($int) {
3190
        global $g;
3191
3192
        $int_family = preg_split("/[0-9]+/", $int);
3193
3194
        if (in_array($int_family[0], $g['vlan_long_frame']))
3195
                return true;
3196
        else
3197
                return false;
3198
}
3199
3200 5c8e8a17 gnhb
function setup_pppoe_reset_file($pppif, $iface="") {
3201 55f3ca1d gnhb
	global $g;
3202 5c8e8a17 gnhb
	$cron_file = "{$g['varetc_path']}/pppoe_restart_{$pppif}";
3203 766bd6d0 gnhb
3204 5c8e8a17 gnhb
	if(!empty($iface) && !empty($pppif)){
3205 766bd6d0 gnhb
		$cron_cmd = "#!/bin/sh\necho '<?php require(\"config.inc\"); require(\"interfaces.inc\"); interface_reconfigure({$iface}); log_error(\"PPPoE periodic reset executed on {$iface}\"); ?>' | /usr/local/bin/php -q";
3206
		file_put_contents($cron_file, $cron_cmd);
3207
		chmod($cron_file, 0700);
3208 55f3ca1d gnhb
		sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP");
3209 a5d6f60b Ermal Lu?i
	} else
3210 766bd6d0 gnhb
		unlink_if_exists($cron_file);
3211 b7ec2b9e Scott Ullrich
}
3212
3213 c55e4580 Chris Buechler
?>