Project

General

Profile

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