Project

General

Profile

Download (166 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2
/*
3
	interfaces.inc
4 eba938e3 Scott Ullrich
	Copyright (C) 2004-2008 Scott Ullrich
5 d98a2e6a Jose Luis Duran
	Copyright (C) 2008-2009 Ermal Luçi
6 ac3f8318 Espen Johansen
	All rights reserved.
7
8
	function interfaces_wireless_configure is
9
	Copyright (C) 2005 Espen Johansen
10 cfc707f7 Scott Ullrich
	All rights reserved.
11
12
	originally part of m0n0wall (http://m0n0.ch/wall)
13 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
14
	All rights reserved.
15 cfc707f7 Scott Ullrich
16 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
17
	modification, are permitted provided that the following conditions are met:
18 cfc707f7 Scott Ullrich
19 ac3f8318 Espen Johansen
	1. Redistributions of source code must retain the above copyright notices,
20 5b237745 Scott Ullrich
	   this list of conditions and the following disclaimer.
21 cfc707f7 Scott Ullrich
22 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
23 ac3f8318 Espen Johansen
	   notices, this list of conditions and the following disclaimer in the
24 5b237745 Scott Ullrich
	   documentation and/or other materials provided with the distribution.
25 cfc707f7 Scott Ullrich
26 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
	POSSIBILITY OF SUCH DAMAGE.
36 523855b0 Scott Ullrich
37 b0c6a4f1 Ermal
	pfSense_BUILDER_BINARIES:	/sbin/dhclient	/bin/sh	/usr/bin/grep	/usr/bin/xargs	/usr/bin/awk	/usr/local/sbin/choparp
38 89c52814 Ermal
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig	/sbin/route	/usr/sbin/ngctl	/usr/sbin/arp	/bin/kill	/usr/local/sbin/mpd5
39 d53a9a51 smos
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/dhcp6c
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 9a456170 Darren Embry
require_once("util.inc");
47 36b2d82d smos
require_once("gwlb.inc");
48 5b237745 Scott Ullrich
49 b5b957fe Scott Ullrich
function interfaces_bring_up($interface) {
50 cf73302f Phil Davis
	if (!$interface) {
51 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("interfaces_bring_up() was called but no variable defined."));
52 086cf944 Phil Davis
		log_error("Backtrace: " . debug_backtrace());
53 b5b957fe Scott Ullrich
		return;
54
	}
55 871768cf Ermal
	pfSense_interface_flags($interface, IFF_UP);
56 b5b957fe Scott Ullrich
}
57
58 52947718 Ermal Lu?i
/*
59
 * Return the interface array
60
 */
61
function get_interface_arr($flush = false) {
62 1c3ddd9e Renato Botelho
	global $interface_arr_cache;
63 52947718 Ermal Lu?i
64 1c3ddd9e Renato Botelho
	/* If the cache doesn't exist, build it */
65 cf73302f Phil Davis
	if (!isset($interface_arr_cache) or $flush) {
66 1c3ddd9e Renato Botelho
		$interface_arr_cache = pfSense_interface_listget();
67 cf73302f Phil Davis
	}
68 52947718 Ermal Lu?i
69 1c3ddd9e Renato Botelho
	return $interface_arr_cache;
70 52947718 Ermal Lu?i
}
71
72
/*
73
 * does_interface_exist($interface): return true or false if a interface is
74
 * detected.
75
 */
76 33e71f10 jim-p
function does_interface_exist($interface, $flush = true) {
77 8256f324 gnhb
	global $config;
78 be45aa79 Renato Botelho
79 cf73302f Phil Davis
	if (!$interface) {
80 72993196 Ermal
		return false;
81 cf73302f Phil Davis
	}
82 52947718 Ermal Lu?i
83 33e71f10 jim-p
	$ints = get_interface_arr($flush);
84 cf73302f Phil Davis
	if (in_array($interface, $ints)) {
85 8256f324 gnhb
		return true;
86 cf73302f Phil Davis
	} else {
87 8256f324 gnhb
		return false;
88 cf73302f Phil Davis
	}
89 52947718 Ermal Lu?i
}
90
91 2708a5cf Ermal
/*
92
 * does_vip_exist($vip): return true or false if a vip is
93
 * configured.
94
 */
95
function does_vip_exist($vip) {
96
	global $config;
97 be45aa79 Renato Botelho
98 cf73302f Phil Davis
	if (!$vip) {
99 2708a5cf Ermal
		return false;
100 cf73302f Phil Davis
	}
101 2708a5cf Ermal
102
103 b526daaf Ermal
	switch ($vip['mode']) {
104 cf73302f Phil Davis
		case "carp":
105
		case "ipalias":
106
			/* XXX: Make proper checks? */
107
			$realif = get_real_interface($vip['interface']);
108
			if (!does_interface_exist($realif)) {
109
				return false;
110
			}
111
			break;
112
		case "proxyarp":
113
			/* XXX: Implement this */
114
		default:
115 b526daaf Ermal
			return false;
116
	}
117
118
	$ifacedata = pfSense_getall_interface_addresses($realif);
119
	foreach ($ifacedata as $vipips) {
120 cf73302f Phil Davis
		if ($vipips == "{$vip['subnet']}/{$vip['subnet_bits']}") {
121 b526daaf Ermal
			return true;
122 cf73302f Phil Davis
		}
123 2708a5cf Ermal
	}
124
125
	return false;
126
}
127
128 67b057a9 Ermal
function interface_netgraph_needed($interface = "wan") {
129
	global $config;
130
131
	$found = false;
132 c9d46a8e Renato Botelho
	if (!empty($config['l2tp']) &&
133 cf73302f Phil Davis
	    $config['l2tp']['mode'] == "server") {
134 67b057a9 Ermal
		$found = true;
135 cf73302f Phil Davis
	}
136 67b057a9 Ermal
	if ($found == false && is_array($config['pppoes']['pppoe'])) {
137
		foreach ($config['pppoes']['pppoe'] as $pppoe) {
138 cf73302f Phil Davis
			if ($pppoe['mode'] != "server") {
139 67b057a9 Ermal
				continue;
140 cf73302f Phil Davis
			}
141 7af43cb0 Phil Davis
			if ($pppoe['interface'] == $interface) {
142 67b057a9 Ermal
				$found = true;
143
				break;
144 7af43cb0 Phil Davis
			}
145 67b057a9 Ermal
		}
146
	}
147 cf73302f Phil Davis
	if ($found == false) {
148 a9163efe Ermal LUÇI
		$found = interface_isppp_type($interface);
149 cf73302f Phil Davis
	}
150 a9163efe Ermal LUÇI
151 9d7d2388 Ermal
	if ($found == false) {
152
		$realif = get_real_interface($interface);
153
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
154
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
155 086cf944 Phil Davis
				$ports = explode(',', $ppp['ports']);
156 cf73302f Phil Davis
				foreach ($ports as $pid => $port) {
157 20cb9803 gnhb
					$port = get_real_interface($port);
158 3eb00b49 gnhb
					if ($realif == $port) {
159
						$found = true;
160
						break;
161
					}
162 be45aa79 Renato Botelho
					/* Find the parent interfaces of the vlans in the MLPPP configs
163
					* there should be only one element in the array here
164 20cb9803 gnhb
					* -- this could be better . . . */
165
					$parent_if = get_parent_interface($port);
166
					if ($realif == $parent_if[0]) {
167
						$found = true;
168
						break;
169
					}
170 3eb00b49 gnhb
				}
171 9d7d2388 Ermal
			}
172 67b057a9 Ermal
		}
173
	}
174 be45aa79 Renato Botelho
175 31eee4a6 Ermal
	if ($found == false) {
176
		$realif = get_real_interface($interface);
177 67b057a9 Ermal
		pfSense_ngctl_detach("{$realif}:", $realif);
178 31eee4a6 Ermal
	}
179 92a1c8e6 Ermal
	/* NOTE: We make sure for this on interface_ppps_configure()
180 f416763b Phil Davis
	 *	no need to do it here again.
181 92a1c8e6 Ermal
	 *	else
182
	 *		pfSense_ngctl_attach(".", $realif);
183
	 */
184 67b057a9 Ermal
}
185
186 eba938e3 Scott Ullrich
function interfaces_loopback_configure() {
187 7734aea6 Andrew Thompson
	global $g;
188
189 cf73302f Phil Davis
	if (platform_booting()) {
190 07e40c1f Carlos Eduardo Ramos
		echo gettext("Configuring loopback interface...");
191 cf73302f Phil Davis
	}
192 871768cf Ermal
	pfSense_interface_setaddress("lo0", "127.0.0.1");
193 b5b957fe Scott Ullrich
	interfaces_bring_up("lo0");
194 cf73302f Phil Davis
	if (platform_booting()) {
195 07e40c1f Carlos Eduardo Ramos
		echo gettext("done.") . "\n";
196 cf73302f Phil Davis
	}
197 5b237745 Scott Ullrich
	return 0;
198
}
199
200 00e8315b Ermal
function interfaces_vlan_configure($realif = "") {
201 7a6f7c55 Scott Ullrich
	global $config, $g;
202 cf73302f Phil Davis
	if (platform_booting()) {
203 07e40c1f Carlos Eduardo Ramos
		echo gettext("Configuring VLAN interfaces...");
204 cf73302f Phil Davis
	}
205 5b6eac01 Scott Ullrich
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
206 e1c449c0 Ermal Lu?i
		foreach ($config['vlans']['vlan'] as $vlan) {
207 cf73302f Phil Davis
			if (empty($vlan['vlanif'])) {
208 48315e65 Ermal Luci
				$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
209 cf73302f Phil Davis
			}
210
			if (!empty($realif) && $realif != $vlan['vlanif']) {
211 00e8315b Ermal
				continue;
212 cf73302f Phil Davis
			}
213 00e8315b Ermal
214 5b6eac01 Scott Ullrich
			/* XXX: Maybe we should report any errors?! */
215 5f1e1d26 Ermal Lu?i
			interface_vlan_configure($vlan);
216 517feb1c Seth Mos
		}
217 5b6eac01 Scott Ullrich
	}
218 cf73302f Phil Davis
	if (platform_booting()) {
219 07e40c1f Carlos Eduardo Ramos
		echo gettext("done.") . "\n";
220 cf73302f Phil Davis
	}
221 2075fadb Ermal Luçi
}
222 cfc707f7 Scott Ullrich
223 abcb2bed Ermal Lu?i
function interface_vlan_configure(&$vlan) {
224 1c3ddd9e Renato Botelho
	global $config, $g;
225 161040eb Scott Ullrich
226 5f1e1d26 Ermal Lu?i
	if (!is_array($vlan)) {
227 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("VLAN: called with wrong options. Problems with config!"));
228 5f1e1d26 Ermal Lu?i
		return;
229
	}
230
	$if = $vlan['if'];
231 6c07db48 Phil Davis
	$vlanif = empty($vlan['vlanif']) ? "{$if}_vlan{$vlan['tag']}" : $vlan['vlanif'];
232 5f1e1d26 Ermal Lu?i
	$tag = $vlan['tag'];
233
234 871768cf Ermal
	if (empty($if)) {
235 905ea336 Phil Davis
		log_error(gettext("interface_vlan_configure called with if undefined."));
236 3ae4960c Ermal Luçi
		return;
237
	}
238
239 37a53d16 Scott Ullrich
	/* make sure the parent interface is up */
240 07101b63 Ermal Luçi
	interfaces_bring_up($if);
241
	/* Since we are going to add vlan(4) try to enable all that hardware supports. */
242 871768cf Ermal
	pfSense_interface_capabilities($if, IFCAP_VLAN_HWTAGGING|IFCAP_VLAN_MTU|IFCAP_VLAN_HWFILTER);
243 cfc707f7 Scott Ullrich
244 4aca19b3 Scott Ullrich
	if (!empty($vlanif) && does_interface_exist($vlanif)) {
245 df2a0f18 Ermal
		interface_bring_down($vlanif, true);
246 4aca19b3 Scott Ullrich
	} else {
247 871768cf Ermal
		$tmpvlanif = pfSense_interface_create("vlan");
248
		pfSense_interface_rename($tmpvlanif, $vlanif);
249
		pfSense_ngctl_name("{$tmpvlanif}:", $vlanif);
250 abcb2bed Ermal Lu?i
	}
251 871768cf Ermal
252
	pfSense_vlan_create($vlanif, $if, $tag);
253 2075fadb Ermal Luçi
254 07101b63 Ermal Luçi
	interfaces_bring_up($vlanif);
255 cfc707f7 Scott Ullrich
256 40b0b541 Ermal Lu?i
	/* invalidate interface cache */
257
	get_interface_arr(true);
258 3f7d2120 Bill Marquette
259 4aca19b3 Scott Ullrich
	/* XXX: ermal -- for now leave it here at the moment it does not hurt. */
260 07101b63 Ermal Luçi
	interfaces_bring_up($if);
261 cfc707f7 Scott Ullrich
262 4aca19b3 Scott Ullrich
	return $vlanif;
263 5b237745 Scott Ullrich
}
264
265 abcb2bed Ermal Lu?i
function interface_qinq_configure(&$vlan, $fd = NULL) {
266 1c3ddd9e Renato Botelho
	global $config, $g;
267
268
	if (!is_array($vlan)) {
269
		log_error(sprintf(gettext("QinQ compat VLAN: called with wrong options. Problems with config!%s"), "\n"));
270
		return;
271
	}
272
273
	$qinqif = $vlan['if'];
274
	$tag = $vlan['tag'];
275 cf73302f Phil Davis
	if (empty($qinqif)) {
276 1c3ddd9e Renato Botelho
		log_error(sprintf(gettext("interface_qinq_configure called with if undefined.%s"), "\n"));
277
		return;
278
	}
279 782e33f8 Renato Botelho
280 cf73302f Phil Davis
	if (!does_interface_exist($qinqif)) {
281 782e33f8 Renato Botelho
		log_error(sprintf(gettext("interface_qinq_configure called with invalid if.%s"), "\n"));
282
		return;
283
	}
284
285 4400ad66 Ermal Lu?i
	$vlanif = interface_vlan_configure($vlan);
286 5f1e1d26 Ermal Lu?i
287 1c3ddd9e Renato Botelho
	if ($fd == NULL) {
288
		$exec = true;
289
		$fd = fopen("{$g['tmp_path']}/netgraphcmd", "w");
290 cf73302f Phil Davis
	} else {
291 1c3ddd9e Renato Botelho
		$exec = false;
292 cf73302f Phil Davis
	}
293 1c3ddd9e Renato Botelho
	/* make sure the parent is converted to ng_vlan(4) and is up */
294
	interfaces_bring_up($qinqif);
295 5f1e1d26 Ermal Lu?i
296 9cf46050 Ermal
	pfSense_ngctl_attach(".", $qinqif);
297 1c3ddd9e Renato Botelho
	if (!empty($vlanif) && does_interface_exist($vlanif)) {
298
		fwrite($fd, "shutdown {$qinqif}qinq:\n");
299
		exec("/usr/sbin/ngctl msg {$qinqif}qinq: gettable", $result);
300
		if (empty($result)) {
301
			fwrite($fd, "mkpeer {$qinqif}: vlan lower downstream\n");
302
			fwrite($fd, "name {$qinqif}:lower {$vlanif}qinq\n");
303
			fwrite($fd, "connect {$qinqif}: {$vlanif}qinq: upper nomatch\n");
304
		}
305
	} else {
306
		fwrite($fd, "mkpeer {$qinqif}: vlan lower downstream\n");
307
		fwrite($fd, "name {$qinqif}:lower {$vlanif}qinq\n");
308
		fwrite($fd, "connect {$qinqif}: {$vlanif}qinq: upper nomatch\n");
309
	}
310
311
	/* invalidate interface cache */
312
	get_interface_arr(true);
313
314 cf73302f Phil Davis
	if (!stristr($qinqif, "_vlan")) {
315 1c3ddd9e Renato Botelho
		mwexec("/sbin/ifconfig {$qinqif} promisc\n");
316 cf73302f Phil Davis
	}
317 1c3ddd9e Renato Botelho
318
	$macaddr = get_interface_mac($qinqif);
319
	if (!empty($vlan['members'])) {
320
		$members = explode(" ", $vlan['members']);
321
		foreach ($members as $qtag) {
322
			$qinq = array();
323
			$qinq['tag'] = $qtag;
324
			$qinq['if'] = $vlanif;
325
			interface_qinq2_configure($qinq, $fd, $macaddr);
326
		}
327
	}
328
	if ($exec == true) {
329
		fclose($fd);
330
		mwexec("/usr/sbin/ngctl -f {$g['tmp_path']}/netgraphcmd");
331
	}
332
333
	interfaces_bring_up($qinqif);
334
	if (!empty($vlan['members'])) {
335
		$members = explode(" ", $vlan['members']);
336 cf73302f Phil Davis
		foreach ($members as $qif) {
337 1c3ddd9e Renato Botelho
			interfaces_bring_up("{$vlanif}_{$qif}");
338 cf73302f Phil Davis
		}
339 1c3ddd9e Renato Botelho
	}
340
341
	return $vlanif;
342 5f1e1d26 Ermal Lu?i
}
343
344
function interfaces_qinq_configure() {
345 7a6f7c55 Scott Ullrich
	global $config, $g;
346 cf73302f Phil Davis
	if (platform_booting()) {
347 07e40c1f Carlos Eduardo Ramos
		echo gettext("Configuring QinQ interfaces...");
348 cf73302f Phil Davis
	}
349 7a6f7c55 Scott Ullrich
	if (is_array($config['qinqs']['qinqentry']) && count($config['qinqs']['qinqentry'])) {
350
		foreach ($config['qinqs']['qinqentry'] as $qinq) {
351
			/* XXX: Maybe we should report any errors?! */
352 4400ad66 Ermal Lu?i
			interface_qinq_configure($qinq);
353 7a6f7c55 Scott Ullrich
		}
354 4400ad66 Ermal Lu?i
	}
355 cf73302f Phil Davis
	if (platform_booting()) {
356 086cf944 Phil Davis
		echo gettext("done.") . "\n";
357 cf73302f Phil Davis
	}
358 5f1e1d26 Ermal Lu?i
}
359
360 abcb2bed Ermal Lu?i
function interface_qinq2_configure(&$qinq, $fd, $macaddr) {
361 1c3ddd9e Renato Botelho
	global $config, $g;
362
363
	if (!is_array($qinq)) {
364
		log_error(sprintf(gettext("QinQ compat VLAN: called with wrong options. Problems with config!%s"), "\n"));
365
		return;
366
	}
367
368
	$if = $qinq['if'];
369
	$tag = $qinq['tag'];
370
	$vlanif = "{$if}_{$tag}";
371 cf73302f Phil Davis
	if (empty($if)) {
372 1c3ddd9e Renato Botelho
		log_error(sprintf(gettext("interface_qinq2_configure called with if undefined.%s"), "\n"));
373
		return;
374
	}
375
376
	fwrite($fd, "shutdown {$if}h{$tag}:\n");
377
	fwrite($fd, "mkpeer {$if}qinq: eiface {$if}{$tag} ether\n");
378
	fwrite($fd, "name {$if}qinq:{$if}{$tag} {$if}h{$tag}\n");
379
	fwrite($fd, "msg {$if}qinq: addfilter { vlan={$tag} hook=\"{$if}{$tag}\" }\n");
380
	fwrite($fd, "msg {$if}h{$tag}: setifname \"{$vlanif}\"\n");
381
	fwrite($fd, "msg {$if}h{$tag}: set {$macaddr}\n");
382
383
	/* invalidate interface cache */
384
	get_interface_arr(true);
385
386
	return $vlanif;
387 5f1e1d26 Ermal Lu?i
}
388
389 9f428275 Erik Fonnesbeck
function interfaces_create_wireless_clones() {
390 7bb09580 Erik Fonnesbeck
	global $config, $g;
391 9f428275 Erik Fonnesbeck
392 cf73302f Phil Davis
	if (platform_booting()) {
393 7bb09580 Erik Fonnesbeck
		echo gettext("Creating wireless clone interfaces...");
394 cf73302f Phil Davis
	}
395 7bb09580 Erik Fonnesbeck
396
	$iflist = get_configured_interface_list();
397
398 0fbf7315 Ermal
	foreach ($iflist as $if) {
399 7bb09580 Erik Fonnesbeck
		$realif = $config['interfaces'][$if]['if'];
400 cf73302f Phil Davis
		if (is_interface_wireless($realif)) {
401 7bb09580 Erik Fonnesbeck
			interface_wireless_clone(interface_get_wireless_clone($realif), $config['interfaces'][$if]);
402 cf73302f Phil Davis
		}
403 7bb09580 Erik Fonnesbeck
	}
404
405 6ef2297b Erik Fonnesbeck
	if (isset($config['wireless']['clone']) && is_array($config['wireless']['clone']) && count($config['wireless']['clone'])) {
406 9f428275 Erik Fonnesbeck
		foreach ($config['wireless']['clone'] as $clone) {
407 cf73302f Phil Davis
			if (empty($clone['cloneif'])) {
408 9f428275 Erik Fonnesbeck
				continue;
409 cf73302f Phil Davis
			}
410
			if (does_interface_exist($clone['cloneif'])) {
411 9f428275 Erik Fonnesbeck
				continue;
412 cf73302f Phil Davis
			}
413 9f428275 Erik Fonnesbeck
			/* XXX: Maybe we should report any errors?! */
414 7bb09580 Erik Fonnesbeck
			interface_wireless_clone($clone['cloneif'], $clone);
415 9f428275 Erik Fonnesbeck
		}
416
	}
417 cf73302f Phil Davis
	if (platform_booting()) {
418 7bb09580 Erik Fonnesbeck
		echo gettext("done.") . "\n";
419 cf73302f Phil Davis
	}
420 a687f866 Namezero
421 9f428275 Erik Fonnesbeck
}
422
423 00e8315b Ermal
function interfaces_bridge_configure($checkmember = 0, $realif = "") {
424 1c3ddd9e Renato Botelho
	global $config;
425
426
	$i = 0;
427
	if (is_array($config['bridges']['bridged']) && count($config['bridges']['bridged'])) {
428
		foreach ($config['bridges']['bridged'] as $bridge) {
429 cf73302f Phil Davis
			if (empty($bridge['bridgeif'])) {
430 1c3ddd9e Renato Botelho
				$bridge['bridgeif'] = "bridge{$i}";
431 cf73302f Phil Davis
			}
432
			if (!empty($realif) && $realif != $bridge['bridgeif']) {
433 00e8315b Ermal
				continue;
434 cf73302f Phil Davis
			}
435 00e8315b Ermal
436 ef1c048b Ermal
			if ($checkmember == 1) {
437 89f171b0 Ermal LUÇI
				/* XXX: It should not be possible no? */
438 cf73302f Phil Davis
				if (strstr($bridge['if'], '_vip')) {
439 f2dd61a7 Ermal
					continue;
440 cf73302f Phil Davis
				}
441 ef1c048b Ermal
				$members = explode(',', $bridge['members']);
442
				foreach ($members as $member) {
443 cf73302f Phil Davis
					if (!empty($config['interfaces'][$bridge['if']]) && $config['interfaces'][$bridge['if']]['ipaddrv6'] == "track6") {
444 ef1c048b Ermal
						continue 2;
445 cf73302f Phil Davis
					}
446 ef1c048b Ermal
				}
447
			}
448
			else if ($checkmember == 2) {
449
				$members = explode(',', $bridge['members']);
450
				foreach ($members as $member) {
451 cf73302f Phil Davis
					if (empty($config['interfaces'][$bridge['if']]) || $config['interfaces'][$bridge['if']]['ipaddrv6'] != "track6") {
452 ef1c048b Ermal
						continue 2;
453 cf73302f Phil Davis
					}
454 ef1c048b Ermal
				}
455
			}
456 1c3ddd9e Renato Botelho
			/* XXX: Maybe we should report any errors?! */
457
			interface_bridge_configure($bridge, $checkmember);
458
			$i++;
459
		}
460
	}
461 bad29bc6 Ermal Luçi
}
462
463 02de5c07 Ermal
function interface_bridge_configure(&$bridge, $checkmember = 0) {
464 d7147b1c Scott Ullrich
	global $config, $g;
465 bad29bc6 Ermal Luçi
466 cf73302f Phil Davis
	if (!is_array($bridge)) {
467 0e0002c2 bcyrill
		return;
468 cf73302f Phil Davis
	}
469 bad29bc6 Ermal Luçi
470 dc97efaf Ermal Luçi
	if (empty($bridge['members'])) {
471 07e40c1f Carlos Eduardo Ramos
		log_error(sprintf(gettext("No members found on %s"), $bridge['bridgeif']));
472 0e0002c2 bcyrill
		return;
473 dc97efaf Ermal Luçi
	}
474
475 bad29bc6 Ermal Luçi
	$members = explode(',', $bridge['members']);
476 cf73302f Phil Davis
	if (!count($members)) {
477 0e0002c2 bcyrill
		return;
478 cf73302f Phil Davis
	}
479 ea5f6c95 Ermal
480 b64523c1 Ermal Luçi
	/* Calculate smaller mtu and enforce it */
481 69e53ef0 Ermal Luçi
	$smallermtu = 0;
482 07676e36 Ermal
	$commonrx = true;
483
	$commontx = true;
484 02de5c07 Ermal
	$foundgif = false;
485 b64523c1 Ermal Luçi
	foreach ($members as $member) {
486
		$realif = get_real_interface($member);
487 3740c82b Ermal LUÇI
		$mtu = get_interface_mtu($realif);
488 02de5c07 Ermal
		if (substr($realif, 0, 3) == "gif") {
489
			$foundgif = true;
490 cf73302f Phil Davis
			if ($checkmember == 1) {
491 02de5c07 Ermal
				return;
492 cf73302f Phil Davis
			}
493
			if ($mtu <= 1500) {
494 02de5c07 Ermal
				continue;
495 cf73302f Phil Davis
			}
496 02de5c07 Ermal
		}
497 cf73302f Phil Davis
		if ($smallermtu == 0 && !empty($mtu)) {
498 69e53ef0 Ermal Luçi
			$smallermtu = $mtu;
499 cf73302f Phil Davis
		} else if (!empty($mtu) && $mtu < $smallermtu) {
500 b64523c1 Ermal Luçi
			$smallermtu = $mtu;
501 cf73302f Phil Davis
		}
502 b64523c1 Ermal Luçi
	}
503 cf73302f Phil Davis
	if ($foundgif == false && $checkmember == 2) {
504 02de5c07 Ermal
		return;
505 cf73302f Phil Davis
	}
506 02de5c07 Ermal
507 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
508 cf73302f Phil Davis
	if ($smallermtu == 0) {
509 be45aa79 Renato Botelho
		$smallermtu = 1500;
510 cf73302f Phil Davis
	}
511 69e53ef0 Ermal Luçi
512 285ef132 Ermal LUÇI
	if (platform_booting() || !empty($bridge['bridgeif'])) {
513 02de5c07 Ermal
		pfSense_interface_destroy($bridge['bridgeif']);
514
		pfSense_interface_create($bridge['bridgeif']);
515 b3af5453 Renato Botelho
		$bridgeif = escapeshellarg($bridge['bridgeif']);
516 6d53bbb5 Ermal
	} else {
517 02de5c07 Ermal
		$bridgeif = pfSense_interface_create("bridge");
518 6d53bbb5 Ermal
		$bridge['bridgeif'] = $bridgeif;
519
	}
520 02de5c07 Ermal
521 2c4301fa Ermal LUÇI
	$bridgemtu = interface_find_child_cfgmtu($bridge['bridgeif']);
522 cf73302f Phil Davis
	if ($bridgemtu > $smallermtu) {
523 2c4301fa Ermal LUÇI
		$smallermtu = $bridgemtu;
524 cf73302f Phil Davis
	}
525 2c4301fa Ermal LUÇI
526 02de5c07 Ermal
	$checklist = get_configured_interface_list();
527
528 bad29bc6 Ermal Luçi
	/* Add interfaces to bridge */
529 31241000 Ermal Luçi
	foreach ($members as $member) {
530 cf73302f Phil Davis
		if (empty($checklist[$member])) {
531 d7147b1c Scott Ullrich
			continue;
532 cf73302f Phil Davis
		}
533 19defb88 Ermal
		$realif = get_real_interface($member);
534 07676e36 Ermal
		if (!$realif) {
535 07e40c1f Carlos Eduardo Ramos
			log_error(gettext("realif not defined in interfaces bridge - up"));
536 07676e36 Ermal
			continue;
537
		}
538
		/* make sure the parent interface is up */
539 19defb88 Ermal
		pfSense_interface_mtu($realif, $smallermtu);
540
		interfaces_bring_up($realif);
541 43517fcc Ermal LUÇI
		enable_hardware_offloading($member);
542 e550188f Ermal LUÇI
		pfSense_bridge_add_member($bridge['bridgeif'], $realif);
543 d7147b1c Scott Ullrich
	}
544 31241000 Ermal Luçi
545 bad29bc6 Ermal Luçi
	if (isset($bridge['enablestp'])) {
546
		/* Choose spanning tree proto */
547 b3af5453 Renato Botelho
		mwexec("/sbin/ifconfig {$bridgeif} proto " . escapeshellarg($bridge['proto']));
548 be45aa79 Renato Botelho
549 dc97efaf Ermal Luçi
		if (!empty($bridge['stp'])) {
550
			$stpifs = explode(',', $bridge['stp']);
551
			foreach ($stpifs as $stpif) {
552
				$realif = get_real_interface($stpif);
553
				mwexec("/sbin/ifconfig {$bridgeif} stp {$realif}");
554
			}
555 bad29bc6 Ermal Luçi
		}
556 cf73302f Phil Davis
		if (!empty($bridge['maxage'])) {
557 b3af5453 Renato Botelho
			mwexec("/sbin/ifconfig {$bridgeif} maxage " . escapeshellarg($bridge['maxage']));
558 cf73302f Phil Davis
		}
559
		if (!empty($bridge['fwdelay'])) {
560 b3af5453 Renato Botelho
			mwexec("/sbin/ifconfig {$bridgeif} fwddelay " . escapeshellarg($bridge['fwdelay']));
561 cf73302f Phil Davis
		}
562
		if (!empty($bridge['hellotime'])) {
563 b3af5453 Renato Botelho
			mwexec("/sbin/ifconfig {$bridgeif} hellotime " . escapeshellarg($bridge['hellotime']));
564 cf73302f Phil Davis
		}
565
		if (!empty($bridge['priority'])) {
566 b3af5453 Renato Botelho
			mwexec("/sbin/ifconfig {$bridgeif} priority " . escapeshellarg($bridge['priority']));
567 cf73302f Phil Davis
		}
568
		if (!empty($bridge['holdcnt'])) {
569 b3af5453 Renato Botelho
			mwexec("/sbin/ifconfig {$bridgeif} holdcnt " . escapeshellarg($bridge['holdcnt']));
570 cf73302f Phil Davis
		}
571 dc97efaf Ermal Luçi
		if (!empty($bridge['ifpriority'])) {
572
			$pconfig = explode(",", $bridge['ifpriority']);
573
			$ifpriority = array();
574
			foreach ($pconfig as $cfg) {
575 9a456170 Darren Embry
				$embcfg = explode_assoc(":", $cfg);
576 cf73302f Phil Davis
				foreach ($embcfg as $key => $value) {
577 dc97efaf Ermal Luçi
					$ifpriority[$key] = $value;
578 cf73302f Phil Davis
				}
579 dc97efaf Ermal Luçi
			}
580
			foreach ($ifpriority as $key => $value) {
581
				$realif = get_real_interface($key);
582 b3af5453 Renato Botelho
				mwexec("/sbin/ifconfig ${bridgeif} ifpriority {$realif} " . escapeshellarg($value));
583 dc97efaf Ermal Luçi
			}
584 bad29bc6 Ermal Luçi
		}
585 dc97efaf Ermal Luçi
		if (!empty($bridge['ifpathcost'])) {
586 da5895bb Darren Embry
			$pconfig = explode(",", $bridge['ifpathcost']);
587 dc97efaf Ermal Luçi
			$ifpathcost = array();
588
			foreach ($pconfig as $cfg) {
589 9a456170 Darren Embry
				$embcfg = explode_assoc(":", $cfg);
590 cf73302f Phil Davis
				foreach ($embcfg as $key => $value) {
591 dc97efaf Ermal Luçi
					$ifpathcost[$key] = $value;
592 cf73302f Phil Davis
				}
593 dc97efaf Ermal Luçi
			}
594
			foreach ($ifpathcost as $key => $value) {
595 1c3ddd9e Renato Botelho
				$realif = get_real_interface($key);
596 b3af5453 Renato Botelho
				mwexec("/sbin/ifconfig ${bridgeif} ifpathcost {$realif} " . escapeshellarg($value));
597 1c3ddd9e Renato Botelho
			}
598 bad29bc6 Ermal Luçi
		}
599
	}
600
601 cf73302f Phil Davis
	if ($bridge['maxaddr'] <> "") {
602 873c1701 Renato Botelho
		mwexec("/sbin/ifconfig {$bridgeif} maxaddr " . escapeshellarg($bridge['maxaddr']));
603 cf73302f Phil Davis
	}
604
	if ($bridge['timeout'] <> "") {
605 873c1701 Renato Botelho
		mwexec("/sbin/ifconfig {$bridgeif} timeout " . escapeshellarg($bridge['timeout']));
606 cf73302f Phil Davis
	}
607 1c3ddd9e Renato Botelho
	if ($bridge['span'] <> "") {
608 85a5da13 Ermal Luçi
		$realif = get_real_interface($bridge['span']);
609 1c3ddd9e Renato Botelho
		mwexec("/sbin/ifconfig {$bridgeif} span {$realif}");
610 bad29bc6 Ermal Luçi
	}
611 a47a5798 Ermal Luçi
	if (!empty($bridge['edge'])) {
612 1c3ddd9e Renato Botelho
		$edgeifs = explode(',', $bridge['edge']);
613
		foreach ($edgeifs as $edgeif) {
614 a47a5798 Ermal Luçi
			$realif = get_real_interface($edgeif);
615 1c3ddd9e Renato Botelho
			mwexec("/sbin/ifconfig {$bridgeif} edge {$realif}");
616
		}
617 a47a5798 Ermal Luçi
	}
618
	if (!empty($bridge['autoedge'])) {
619 1c3ddd9e Renato Botelho
		$edgeifs = explode(',', $bridge['autoedge']);
620
		foreach ($edgeifs as $edgeif) {
621
			$realif = get_real_interface($edgeif);
622
			mwexec("/sbin/ifconfig {$bridgeif} -autoedge {$realif}");
623
		}
624 a47a5798 Ermal Luçi
	}
625
	if (!empty($bridge['ptp'])) {
626 1c3ddd9e Renato Botelho
		$ptpifs = explode(',', $bridge['ptp']);
627
		foreach ($ptpifs as $ptpif) {
628
			$realif = get_real_interface($ptpif);
629
			mwexec("/sbin/ifconfig {$bridgeif} ptp {$realif}");
630
		}
631 a47a5798 Ermal Luçi
	}
632
	if (!empty($bridge['autoptp'])) {
633 1c3ddd9e Renato Botelho
		$ptpifs = explode(',', $bridge['autoptp']);
634
		foreach ($ptpifs as $ptpif) {
635
			$realif = get_real_interface($ptpif);
636
			mwexec("/sbin/ifconfig {$bridgeif} -autoptp {$realif}");
637
		}
638 a47a5798 Ermal Luçi
	}
639
	if (!empty($bridge['static'])) {
640 1c3ddd9e Renato Botelho
		$stickyifs = explode(',', $bridge['static']);
641
		foreach ($stickyifs as $stickyif) {
642
			$realif = get_real_interface($stickyif);
643
			mwexec("/sbin/ifconfig {$bridgeif} sticky {$realif}");
644
		}
645 a47a5798 Ermal Luçi
	}
646
	if (!empty($bridge['private'])) {
647 1c3ddd9e Renato Botelho
		$privateifs = explode(',', $bridge['private']);
648
		foreach ($privateifs as $privateif) {
649
			$realif = get_real_interface($privateif);
650
			mwexec("/sbin/ifconfig {$bridgeif} private {$realif}");
651
		}
652 a47a5798 Ermal Luçi
	}
653 bad29bc6 Ermal Luçi
654 cf73302f Phil Davis
	if ($bridge['bridgeif']) {
655 6d53bbb5 Ermal
		interfaces_bring_up($bridge['bridgeif']);
656 cf73302f Phil Davis
	} else {
657 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("bridgeif not defined -- could not bring interface up"));
658 cf73302f Phil Davis
	}
659 bad29bc6 Ermal Luçi
}
660
661 43517fcc Ermal LUÇI
function interface_bridge_add_member($bridgeif, $interface, $flagsapplied = false) {
662 fcd4a425 Ermal Lu?i
663 cf73302f Phil Davis
	if (!does_interface_exist($bridgeif) || !does_interface_exist($interface)) {
664 fcd4a425 Ermal Lu?i
		return;
665 cf73302f Phil Davis
	}
666 fcd4a425 Ermal Lu?i
667 43517fcc Ermal LUÇI
	if ($flagsapplied == false) {
668
		$mtu = get_interface_mtu($bridgeif);
669
		$mtum = get_interface_mtu($interface);
670 cf73302f Phil Davis
		if ($mtu != $mtum && !(substr($interface, 0, 3) == "gif" && $mtu <= 1500)) {
671 43517fcc Ermal LUÇI
			pfSense_interface_mtu($interface, $mtu);
672 cf73302f Phil Davis
		}
673 51d5aad7 Ermal
674 43517fcc Ermal LUÇI
		hardware_offloading_applyflags($interface);
675
		interfaces_bring_up($interface);
676
	}
677 3ca774ac Ermal
678 2064fa2e Ermal
	pfSense_bridge_add_member($bridgeif, $interface);
679 fcd4a425 Ermal Lu?i
}
680
681 00e8315b Ermal
function interfaces_lagg_configure($realif = "") {
682 1c3ddd9e Renato Botelho
	global $config, $g;
683 cf73302f Phil Davis
	if (platform_booting()) {
684 1c3ddd9e Renato Botelho
		echo gettext("Configuring LAGG interfaces...");
685 cf73302f Phil Davis
	}
686 1c3ddd9e Renato Botelho
	$i = 0;
687
	if (is_array($config['laggs']['lagg']) && count($config['laggs']['lagg'])) {
688
		foreach ($config['laggs']['lagg'] as $lagg) {
689 cf73302f Phil Davis
			if (empty($lagg['laggif'])) {
690 1c3ddd9e Renato Botelho
				$lagg['laggif'] = "lagg{$i}";
691 cf73302f Phil Davis
			}
692
			if (!empty($realif) && $realif != $lagg['laggif']) {
693 00e8315b Ermal
				continue;
694 cf73302f Phil Davis
			}
695 1c3ddd9e Renato Botelho
			/* XXX: Maybe we should report any errors?! */
696
			interface_lagg_configure($lagg);
697
			$i++;
698 7a6f7c55 Scott Ullrich
		}
699 1c3ddd9e Renato Botelho
	}
700 cf73302f Phil Davis
	if (platform_booting()) {
701 1c3ddd9e Renato Botelho
		echo gettext("done.") . "\n";
702 cf73302f Phil Davis
	}
703 cccf624b Ermal Luçi
}
704
705 71c26c22 Renato Botelho
function interface_lagg_configure($lagg) {
706 1c3ddd9e Renato Botelho
	global $config, $g;
707 cccf624b Ermal Luçi
708 cf73302f Phil Davis
	if (!is_array($lagg)) {
709 cccf624b Ermal Luçi
		return -1;
710 cf73302f Phil Davis
	}
711 cccf624b Ermal Luçi
712
	$members = explode(',', $lagg['members']);
713 cf73302f Phil Davis
	if (!count($members)) {
714 cccf624b Ermal Luçi
		return -1;
715 cf73302f Phil Davis
	}
716 be45aa79 Renato Botelho
717 285ef132 Ermal LUÇI
	if (platform_booting() || !(empty($lagg['laggif']))) {
718 871768cf Ermal
		pfSense_interface_destroy($lagg['laggif']);
719
		pfSense_interface_create($lagg['laggif']);
720 1c3ddd9e Renato Botelho
		$laggif = $lagg['laggif'];
721 cf73302f Phil Davis
	} else {
722 871768cf Ermal
		$laggif = pfSense_interface_create("lagg");
723 cf73302f Phil Davis
	}
724 b64523c1 Ermal Luçi
725 71c26c22 Renato Botelho
	/* Check if MTU was defined for this lagg interface */
726 2b58f94e Ermal LUÇI
	$lagg_mtu = interface_find_child_cfgmtu($laggif);
727 71c26c22 Renato Botelho
	if ($lagg_mtu == 0) {
728
		/* Calculate smaller mtu and enforce it */
729
		$smallermtu = 0;
730
		foreach ($members as $member) {
731 3740c82b Ermal LUÇI
			$mtu = get_interface_mtu($member);
732 cf73302f Phil Davis
			if ($smallermtu == 0 && !empty($mtu)) {
733 71c26c22 Renato Botelho
				$smallermtu = $mtu;
734 cf73302f Phil Davis
			} else if (!empty($mtu) && $mtu < $smallermtu) {
735 71c26c22 Renato Botelho
				$smallermtu = $mtu;
736 cf73302f Phil Davis
			}
737 71c26c22 Renato Botelho
		}
738
		$lagg_mtu = $smallermtu;
739 1c3ddd9e Renato Botelho
	}
740 b64523c1 Ermal Luçi
741 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
742 cf73302f Phil Davis
	if ($lagg_mtu == 0) {
743 71c26c22 Renato Botelho
		$lagg_mtu = 1500;
744 cf73302f Phil Davis
	}
745 69e53ef0 Ermal Luçi
746 cccf624b Ermal Luçi
	foreach ($members as $member) {
747 cf73302f Phil Davis
		if (!does_interface_exist($member)) {
748 cccf624b Ermal Luçi
			continue;
749 cf73302f Phil Davis
		}
750 d7147b1c Scott Ullrich
		/* make sure the parent interface is up */
751 71c26c22 Renato Botelho
		pfSense_interface_mtu($member, $lagg_mtu);
752 39fbee97 Ermal Lu?i
		interfaces_bring_up($member);
753 43517fcc Ermal LUÇI
		hardware_offloading_applyflags($member);
754
		mwexec("/sbin/ifconfig " . escapeshellarg($laggif) . " laggport " . escapeshellarg($member));
755 cccf624b Ermal Luçi
	}
756 43517fcc Ermal LUÇI
	pfSense_interface_capabilities($laggif, -$flags_off);
757
	pfSense_interface_capabilities($laggif, $flags_on);
758 be45aa79 Renato Botelho
759 873c1701 Renato Botelho
	mwexec("/sbin/ifconfig {$laggif} laggproto " . escapeshellarg($lagg['proto']));
760 acc1e9d0 Scott Ullrich
761 b5b957fe Scott Ullrich
	interfaces_bring_up($laggif);
762 cccf624b Ermal Luçi
763 d7147b1c Scott Ullrich
	return $laggif;
764 cccf624b Ermal Luçi
}
765
766 00e8315b Ermal
function interfaces_gre_configure($checkparent = 0, $realif = "") {
767 1c3ddd9e Renato Botelho
	global $config;
768 582d2452 Ermal Luçi
769 1c3ddd9e Renato Botelho
	if (is_array($config['gres']['gre']) && count($config['gres']['gre'])) {
770
		foreach ($config['gres']['gre'] as $i => $gre) {
771 cf73302f Phil Davis
			if (empty($gre['greif'])) {
772 1c3ddd9e Renato Botelho
				$gre['greif'] = "gre{$i}";
773 cf73302f Phil Davis
			}
774
			if (!empty($realif) && $realif != $gre['greif']) {
775 00e8315b Ermal
				continue;
776 cf73302f Phil Davis
			}
777 00e8315b Ermal
778 ef1c048b Ermal
			if ($checkparent == 1) {
779 cf73302f Phil Davis
				if (substr($gre['if'], 0, 4) == '_vip') {
780 89f171b0 Ermal LUÇI
					continue;
781 cf73302f Phil Davis
				}
782
				if (substr($gre['if'], 0, 5) == '_lloc') {
783 f2dd61a7 Ermal
					continue;
784 cf73302f Phil Davis
				}
785
				if (!empty($config['interfaces'][$gre['if']]) && $config['interfaces'][$gre['if']]['ipaddrv6'] == "track6") {
786 ef1c048b Ermal
					continue;
787 cf73302f Phil Davis
				}
788
			} else if ($checkparent == 2) {
789 b65de558 Ermal LUÇI
				if ((substr($gre['if'], 0, 4) != '_vip' && substr($gre['if'], 0, 5) != '_lloc') &&
790 cf73302f Phil Davis
				    (empty($config['interfaces'][$gre['if']]) || $config['interfaces'][$gre['if']]['ipaddrv6'] != "track6")) {
791 ef1c048b Ermal
					continue;
792 cf73302f Phil Davis
				}
793 ef1c048b Ermal
			}
794 1c3ddd9e Renato Botelho
			/* XXX: Maybe we should report any errors?! */
795
			interface_gre_configure($gre);
796
		}
797
	}
798 582d2452 Ermal Luçi
}
799
800 ed62880b Ermal
/* NOTE: $grekey is not used but useful for passing this function to array_walk. */
801
function interface_gre_configure(&$gre, $grekey = "") {
802 1c3ddd9e Renato Botelho
	global $config, $g;
803 582d2452 Ermal Luçi
804 cf73302f Phil Davis
	if (!is_array($gre)) {
805 582d2452 Ermal Luçi
		return -1;
806 cf73302f Phil Davis
	}
807 582d2452 Ermal Luçi
808 85a5da13 Ermal Luçi
	$realif = get_real_interface($gre['if']);
809
	$realifip = get_interface_ip($gre['if']);
810 582d2452 Ermal Luçi
811 d7147b1c Scott Ullrich
	/* make sure the parent interface is up */
812 b5b957fe Scott Ullrich
	interfaces_bring_up($realif);
813 582d2452 Ermal Luçi
814 285ef132 Ermal LUÇI
	if (platform_booting() || !(empty($gre['greif']))) {
815 871768cf Ermal
		pfSense_interface_destroy($gre['greif']);
816
		pfSense_interface_create($gre['greif']);
817 582d2452 Ermal Luçi
		$greif = $gre['greif'];
818 cf73302f Phil Davis
	} else {
819 871768cf Ermal
		$greif = pfSense_interface_create("gre");
820 cf73302f Phil Davis
	}
821 582d2452 Ermal Luçi
822
	/* Do not change the order here for more see gre(4) NOTES section. */
823 873c1701 Renato Botelho
	mwexec("/sbin/ifconfig {$greif} tunnel {$realifip} " . escapeshellarg($gre['remote-addr']));
824 cf73302f Phil Davis
	if ((is_ipaddrv6($gre['tunnel-local-addr'])) || (is_ipaddrv6($gre['tunnel-remote-addr']))) {
825 cdeaf91e Ermal
		/* XXX: The prefixlen argument for tunnels of ipv6 is useless since it needs to be 128 as enforced by kernel */
826
		//mwexec("/sbin/ifconfig {$greif} inet6 " . escapeshellarg($gre['tunnel-local-addr']) . " " . escapeshellarg($gre['tunnel-remote-addr']) . " prefixlen /" . escapeshellarg($gre['tunnel-remote-net']));
827
		mwexec("/sbin/ifconfig {$greif} inet6 " . escapeshellarg($gre['tunnel-local-addr']) . " " . escapeshellarg($gre['tunnel-remote-addr']) . " prefixlen 128");
828 a59c7fa6 smos
	} else {
829 873c1701 Renato Botelho
		mwexec("/sbin/ifconfig {$greif} " . escapeshellarg($gre['tunnel-local-addr']) . " " . escapeshellarg($gre['tunnel-remote-addr']) . " netmask " . gen_subnet_mask($gre['tunnel-remote-net']));
830 a59c7fa6 smos
	}
831 cf73302f Phil Davis
	if (isset($gre['link0'])) {
832 871768cf Ermal
		pfSense_interface_flags($greif, IFF_LINK0);
833 cf73302f Phil Davis
	}
834
	if (isset($gre['link1'])) {
835 871768cf Ermal
		pfSense_interface_flags($greif, IFF_LINK1);
836 cf73302f Phil Davis
	}
837
	if (isset($gre['link2'])) {
838 871768cf Ermal
		pfSense_interface_flags($greif, IFF_LINK2);
839 cf73302f Phil Davis
	}
840 d7147b1c Scott Ullrich
841 cf73302f Phil Davis
	if ($greif) {
842 b5b957fe Scott Ullrich
		interfaces_bring_up($greif);
843 cf73302f Phil Davis
	} else {
844 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("Could not bring greif up -- variable not defined."));
845 cf73302f Phil Davis
	}
846 582d2452 Ermal Luçi
847 cf73302f Phil Davis
	if (isset($gre['link1']) && $gre['link1']) {
848 873c1701 Renato Botelho
		mwexec("/sbin/route add " . escapeshellarg($gre['tunnel-remote-addr']) . "/" . escapeshellarg($gre['tunnel-remote-net']) . " " . escapeshellarg($gre['tunnel-local-addr']));
849 cf73302f Phil Davis
	}
850
	if (is_ipaddrv4($gre['tunnel-remote-addr'])) {
851 283e9180 Seth Mos
		file_put_contents("{$g['tmp_path']}/{$greif}_router", $gre['tunnel-remote-addr']);
852 cf73302f Phil Davis
	}
853
	if (is_ipaddrv6($gre['tunnel-remote-addr'])) {
854 283e9180 Seth Mos
		file_put_contents("{$g['tmp_path']}/{$greif}_routerv6", $gre['tunnel-remote-addr']);
855 cf73302f Phil Davis
	}
856 582d2452 Ermal Luçi
857 da2ec336 Chris Buechler
	interfaces_bring_up($greif);
858 11ad160e Ermal
859 582d2452 Ermal Luçi
	return $greif;
860
}
861
862 00e8315b Ermal
function interfaces_gif_configure($checkparent = 0, $realif = "") {
863 9006e9f8 Scott Ullrich
	global $config;
864 f1a93dee Ermal
865 9006e9f8 Scott Ullrich
	if (is_array($config['gifs']['gif']) && count($config['gifs']['gif'])) {
866 f1a93dee Ermal
		foreach ($config['gifs']['gif'] as $i => $gif) {
867 cf73302f Phil Davis
			if (empty($gif['gifif'])) {
868 9006e9f8 Scott Ullrich
				$gre['gifif'] = "gif{$i}";
869 cf73302f Phil Davis
			}
870
			if (!empty($realif) && $realif != $gif['gifif']) {
871 00e8315b Ermal
				continue;
872 cf73302f Phil Davis
			}
873 00e8315b Ermal
874 ef1c048b Ermal
			if ($checkparent == 1) {
875 cf73302f Phil Davis
				if (substr($gif['if'], 0, 4) == '_vip') {
876 89f171b0 Ermal LUÇI
					continue;
877 cf73302f Phil Davis
				}
878
				if (substr($gif['if'], 0, 5) == '_lloc') {
879 f2dd61a7 Ermal
					continue;
880 cf73302f Phil Davis
				}
881
				if (!empty($config['interfaces'][$gif['if']]) && $config['interfaces'][$gif['if']]['ipaddrv6'] == "track6") {
882 ef1c048b Ermal
					continue;
883 cf73302f Phil Davis
				}
884 ef1c048b Ermal
			}
885
			else if ($checkparent == 2) {
886 b65de558 Ermal LUÇI
				if ((substr($gif['if'], 0, 4) != '_vip' && substr($gif['if'], 0, 5) != '_lloc') &&
887 cf73302f Phil Davis
				    (empty($config['interfaces'][$gif['if']]) || $config['interfaces'][$gif['if']]['ipaddrv6'] != "track6")) {
888 ef1c048b Ermal
					continue;
889 cf73302f Phil Davis
				}
890 ef1c048b Ermal
			}
891 9006e9f8 Scott Ullrich
			/* XXX: Maybe we should report any errors?! */
892
			interface_gif_configure($gif);
893
		}
894
	}
895 582d2452 Ermal Luçi
}
896
897 ed62880b Ermal
/* NOTE: $gifkey is not used but useful for passing this function to array_walk. */
898
function interface_gif_configure(&$gif, $gifkey = "") {
899 9006e9f8 Scott Ullrich
	global $config, $g;
900 582d2452 Ermal Luçi
901 cf73302f Phil Davis
	if (!is_array($gif)) {
902 9006e9f8 Scott Ullrich
		return -1;
903 cf73302f Phil Davis
	}
904 582d2452 Ermal Luçi
905 9006e9f8 Scott Ullrich
	$realif = get_real_interface($gif['if']);
906 89f171b0 Ermal LUÇI
	$ipaddr = get_interface_ip($gif['if']);
907 582d2452 Ermal Luçi
908 8436caa7 bcyrill
	if (is_ipaddrv4($gif['remote-addr'])) {
909 cf73302f Phil Davis
		if (is_ipaddrv4($ipaddr)) {
910 8436caa7 bcyrill
			$realifip = $ipaddr;
911 cf73302f Phil Davis
		} else {
912 8436caa7 bcyrill
			$realifip = get_interface_ip($gif['if']);
913 cf73302f Phil Davis
		}
914 01a58d89 smos
		$realifgw = get_interface_gateway($gif['if']);
915 8436caa7 bcyrill
	} else if (is_ipaddrv6($gif['remote-addr'])) {
916 cf73302f Phil Davis
		if (is_ipaddrv6($ipaddr)) {
917 8436caa7 bcyrill
			$realifip = $ipaddr;
918 cf73302f Phil Davis
		} else {
919 8436caa7 bcyrill
			$realifip = get_interface_ipv6($gif['if']);
920 cf73302f Phil Davis
		}
921 bb8f3ce1 m0se
		$realifgw = get_interface_gateway_v6($gif['if']);
922 01a58d89 smos
	}
923 9006e9f8 Scott Ullrich
	/* make sure the parent interface is up */
924 cf73302f Phil Davis
	if ($realif) {
925 9006e9f8 Scott Ullrich
		interfaces_bring_up($realif);
926 cf73302f Phil Davis
	} else {
927 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("could not bring realif up -- variable not defined -- interface_gif_configure()"));
928 cf73302f Phil Davis
	}
929 582d2452 Ermal Luçi
930 285ef132 Ermal LUÇI
	if (platform_booting() || !(empty($gif['gifif']))) {
931 871768cf Ermal
		pfSense_interface_destroy($gif['gifif']);
932
		pfSense_interface_create($gif['gifif']);
933 9006e9f8 Scott Ullrich
		$gifif = $gif['gifif'];
934 cf73302f Phil Davis
	} else {
935 871768cf Ermal
		$gifif = pfSense_interface_create("gif");
936 cf73302f Phil Davis
	}
937 9006e9f8 Scott Ullrich
938
	/* Do not change the order here for more see gif(4) NOTES section. */
939 873c1701 Renato Botelho
	mwexec("/sbin/ifconfig {$gifif} tunnel {$realifip} " . escapeshellarg($gif['remote-addr']));
940 cf73302f Phil Davis
	if ((is_ipaddrv6($gif['tunnel-local-addr'])) || (is_ipaddrv6($gif['tunnel-remote-addr']))) {
941 d2c59808 Ermal
		/* XXX: The prefixlen argument for tunnels of ipv6 is useless since it needs to be 128 as enforced by kernel */
942
		//mwexec("/sbin/ifconfig {$gifif} inet6 " . escapeshellarg($gif['tunnel-local-addr']) . " " . escapeshellarg($gif['tunnel-remote-addr']) . " prefixlen /" . escapeshellarg($gif['tunnel-remote-net']));
943 ff9c579f Renato Botelho
		mwexec("/sbin/ifconfig {$gifif} inet6 " . escapeshellarg($gif['tunnel-local-addr']) . " " . escapeshellarg($gif['tunnel-remote-addr']) . " prefixlen 128");
944 9b1ff028 Seth Mos
	} else {
945 873c1701 Renato Botelho
		mwexec("/sbin/ifconfig {$gifif} " . escapeshellarg($gif['tunnel-local-addr']) . " " . escapeshellarg($gif['tunnel-remote-addr']) . " netmask " . gen_subnet_mask($gif['tunnel-remote-net']));
946 9b1ff028 Seth Mos
	}
947 cf73302f Phil Davis
	if (isset($gif['link0'])) {
948 871768cf Ermal
		pfSense_interface_flags($gifif, IFF_LINK0);
949 cf73302f Phil Davis
	}
950
	if (isset($gif['link1'])) {
951 871768cf Ermal
		pfSense_interface_flags($gifif, IFF_LINK1);
952 cf73302f Phil Davis
	}
953
	if ($gifif) {
954 9006e9f8 Scott Ullrich
		interfaces_bring_up($gifif);
955 cf73302f Phil Davis
	} else {
956 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("could not bring gifif up -- variable not defined"));
957 cf73302f Phil Davis
	}
958 9006e9f8 Scott Ullrich
959 285ef132 Ermal LUÇI
	if (!platform_booting()) {
960 e16f6d03 Ermal
		$iflist = get_configured_interface_list();
961 cf73302f Phil Davis
		foreach ($iflist as $ifname) {
962
			if ($config['interfaces'][$ifname]['if'] == $gifif) {
963
				if (get_interface_gateway($ifname)) {
964 e16f6d03 Ermal
					system_routing_configure($ifname);
965
					break;
966
				}
967 cf73302f Phil Davis
				if (get_interface_gateway_v6($ifname)) {
968 e16f6d03 Ermal
					system_routing_configure($ifname);
969
					break;
970
				}
971 7c0571ce Seth Mos
			}
972
		}
973
	}
974 283e9180 Seth Mos
975 a687f866 Namezero
976 cf73302f Phil Davis
	if (is_ipaddrv4($gif['tunnel-remote-addr'])) {
977 283e9180 Seth Mos
		file_put_contents("{$g['tmp_path']}/{$gifif}_router", $gif['tunnel-remote-addr']);
978 cf73302f Phil Davis
	}
979
	if (is_ipaddrv6($gif['tunnel-remote-addr'])) {
980 283e9180 Seth Mos
		file_put_contents("{$g['tmp_path']}/{$gifif}_routerv6", $gif['tunnel-remote-addr']);
981 cf73302f Phil Davis
	}
982 582d2452 Ermal Luçi
983 1c3ddd9e Renato Botelho
	if (is_ipaddrv4($realifgw)) {
984 873c1701 Renato Botelho
		mwexec("/sbin/route change -host " . escapeshellarg($gif['remote-addr']) . " {$realifgw}");
985 1c3ddd9e Renato Botelho
	}
986
	if (is_ipaddrv6($realifgw)) {
987 873c1701 Renato Botelho
		mwexec("/sbin/route change -host -inet6 " . escapeshellarg($gif['remote-addr']) . " {$realifgw}");
988 1c3ddd9e Renato Botelho
	}
989 01a58d89 smos
990 da2ec336 Chris Buechler
	interfaces_bring_up($gifif);
991 11ad160e Ermal
992 9006e9f8 Scott Ullrich
	return $gifif;
993 582d2452 Ermal Luçi
}
994
995 eba938e3 Scott Ullrich
function interfaces_configure() {
996 9b1c39e3 Ermal Luçi
	global $config, $g;
997
998 a5d6f60b Ermal Lu?i
	/* Set up our loopback interface */
999 4aca19b3 Scott Ullrich
	interfaces_loopback_configure();
1000 a5d6f60b Ermal Lu?i
1001 f3bddb47 Ermal
	/* create the unconfigured wireless clones */
1002
	interfaces_create_wireless_clones();
1003
1004 541b7c56 Scott Ullrich
	/* set up LAGG virtual interfaces */
1005
	interfaces_lagg_configure();
1006
1007 acc1e9d0 Scott Ullrich
	/* set up VLAN virtual interfaces */
1008
	interfaces_vlan_configure();
1009
1010 5f1e1d26 Ermal Lu?i
	interfaces_qinq_configure();
1011
1012 67ee1ec5 Ermal Luçi
	$iflist = get_configured_interface_with_descr();
1013 9b1c39e3 Ermal Luçi
	$delayed_list = array();
1014
	$bridge_list = array();
1015 ef1c048b Ermal
	$track6_list = array();
1016 be45aa79 Renato Botelho
1017 871768cf Ermal
	/* This is needed to speedup interfaces on bootup. */
1018
	$reload = false;
1019 cf73302f Phil Davis
	if (!platform_booting()) {
1020 871768cf Ermal
		$reload = true;
1021 cf73302f Phil Davis
	}
1022 871768cf Ermal
1023 cf73302f Phil Davis
	foreach ($iflist as $if => $ifname) {
1024 0dc702f3 Ermal Lu?i
		$realif = $config['interfaces'][$if]['if'];
1025 cf73302f Phil Davis
		if (strstr($realif, "bridge")) {
1026 9b1c39e3 Ermal Luçi
			$bridge_list[$if] = $ifname;
1027 cf73302f Phil Davis
		} else if (strstr($realif, "gre")) {
1028 9b1c39e3 Ermal Luçi
			$delayed_list[$if] = $ifname;
1029 cf73302f Phil Davis
		} else if (strstr($realif, "gif")) {
1030 9b1c39e3 Ermal Luçi
			$delayed_list[$if] = $ifname;
1031 cf73302f Phil Davis
		} else if (strstr($realif, "ovpn")) {
1032 d09d53ac Ermal
			//echo "Delaying OpenVPN interface configuration...done.\n";
1033
			continue;
1034 a823022d Ermal
		} else if (!empty($config['interfaces'][$if]['ipaddrv6']) && $config['interfaces'][$if]['ipaddrv6'] == "track6") {
1035 3d92cd88 Renato Botelho
			$track6_list[$if] = $ifname;
1036 d09d53ac Ermal
		} else {
1037 cf73302f Phil Davis
			if (platform_booting()) {
1038 07e40c1f Carlos Eduardo Ramos
				printf(gettext("Configuring %s interface..."), $ifname);
1039 cf73302f Phil Davis
			}
1040 a687f866 Namezero
1041 cf73302f Phil Davis
			if ($g['debug']) {
1042 07e40c1f Carlos Eduardo Ramos
				log_error(sprintf(gettext("Configuring %s"), $ifname));
1043 cf73302f Phil Davis
			}
1044 871768cf Ermal
			interface_configure($if, $reload);
1045 cf73302f Phil Davis
			if (platform_booting()) {
1046 086cf944 Phil Davis
				echo gettext("done.") . "\n";
1047 cf73302f Phil Davis
			}
1048 9b1c39e3 Ermal Luçi
		}
1049
	}
1050
1051 d7f1891b Ermal
	/*
1052
	 * NOTE: The following function parameter consists of
1053
	 *	1 - Do not load gre/gif/bridge with parent/member as vip
1054
	 *	2 - Do load gre/gif/bridge with parent/member as vip
1055
	 */
1056
1057 d7147b1c Scott Ullrich
	/* set up GRE virtual interfaces */
1058 d7f1891b Ermal
	interfaces_gre_configure(1);
1059 9b1c39e3 Ermal Luçi
1060 d7147b1c Scott Ullrich
	/* set up GIF virtual interfaces */
1061 d7f1891b Ermal
	interfaces_gif_configure(1);
1062
1063
	/* set up BRIDGe virtual interfaces */
1064
	interfaces_bridge_configure(1);
1065
1066 ef1c048b Ermal
	foreach ($track6_list as $if => $ifname) {
1067 cf73302f Phil Davis
		if (platform_booting()) {
1068 ef1c048b Ermal
			printf(gettext("Configuring %s interface..."), $ifname);
1069 cf73302f Phil Davis
		}
1070
		if ($g['debug']) {
1071 ef1c048b Ermal
			log_error(sprintf(gettext("Configuring %s"), $ifname));
1072 cf73302f Phil Davis
		}
1073 ef1c048b Ermal
1074
		interface_configure($if, $reload);
1075
1076 cf73302f Phil Davis
		if (platform_booting()) {
1077 ef1c048b Ermal
			echo gettext("done.") . "\n";
1078 cf73302f Phil Davis
		}
1079 ef1c048b Ermal
	}
1080
1081 d7f1891b Ermal
	/* bring up vip interfaces */
1082
	interfaces_vips_configure();
1083
1084
	/* set up GRE virtual interfaces */
1085
	interfaces_gre_configure(2);
1086
1087
	/* set up GIF virtual interfaces */
1088
	interfaces_gif_configure(2);
1089
1090 9b1c39e3 Ermal Luçi
	foreach ($delayed_list as $if => $ifname) {
1091 cf73302f Phil Davis
		if (platform_booting()) {
1092 07e40c1f Carlos Eduardo Ramos
			printf(gettext("Configuring %s interface..."), $ifname);
1093 cf73302f Phil Davis
		}
1094
		if ($g['debug']) {
1095 1c3ddd9e Renato Botelho
			log_error(sprintf(gettext("Configuring %s"), $ifname));
1096 cf73302f Phil Davis
		}
1097 67ee1ec5 Ermal Luçi
1098 871768cf Ermal
		interface_configure($if, $reload);
1099 4476d447 Ermal Luçi
1100 cf73302f Phil Davis
		if (platform_booting()) {
1101 07e40c1f Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
1102 cf73302f Phil Davis
		}
1103 67ee1ec5 Ermal Luçi
	}
1104 cfc707f7 Scott Ullrich
1105 d7147b1c Scott Ullrich
	/* set up BRIDGe virtual interfaces */
1106 d7f1891b Ermal
	interfaces_bridge_configure(2);
1107 9b1c39e3 Ermal Luçi
1108 d7147b1c Scott Ullrich
	foreach ($bridge_list as $if => $ifname) {
1109 cf73302f Phil Davis
		if (platform_booting()) {
1110 07e40c1f Carlos Eduardo Ramos
			printf(gettext("Configuring %s interface..."), $ifname);
1111 cf73302f Phil Davis
		}
1112
		if ($g['debug']) {
1113 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Configuring %s"), $ifname));
1114 cf73302f Phil Davis
		}
1115 9b1c39e3 Ermal Luçi
1116 871768cf Ermal
		interface_configure($if, $reload);
1117 9b1c39e3 Ermal Luçi
1118 cf73302f Phil Davis
		if (platform_booting()) {
1119 07e40c1f Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
1120 cf73302f Phil Davis
		}
1121 d7147b1c Scott Ullrich
	}
1122 9b1c39e3 Ermal Luçi
1123 42753d25 Ermal Lu?i
	/* configure interface groups */
1124
	interfaces_group_setup();
1125
1126 285ef132 Ermal LUÇI
	if (!platform_booting()) {
1127 5b237745 Scott Ullrich
		/* reconfigure static routes (kernel may have deleted them) */
1128
		system_routing_configure();
1129 cfc707f7 Scott Ullrich
1130 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
1131
		vpn_ipsec_configure();
1132 cfc707f7 Scott Ullrich
1133 f620d00d Ermal Luçi
		/* reload dhcpd (interface enabled/disabled status may have changed) */
1134 5b237745 Scott Ullrich
		services_dhcpd_configure();
1135 cfc707f7 Scott Ullrich
1136 9a51bb64 Warren Baker
		/* restart dnsmasq or unbound */
1137 cf73302f Phil Davis
		if (isset($config['dnsmasq']['enable'])) {
1138 9a51bb64 Warren Baker
			services_dnsmasq_configure();
1139 cf73302f Phil Davis
		} elseif (isset($config['unbound']['enable'])) {
1140 9a51bb64 Warren Baker
			services_unbound_configure();
1141 cf73302f Phil Davis
		}
1142 5b237745 Scott Ullrich
	}
1143 cfc707f7 Scott Ullrich
1144 5b237745 Scott Ullrich
	return 0;
1145
}
1146
1147 7a18dfa4 lgcosta
function interface_reconfigure($interface = "wan", $reloadall = false) {
1148 80bf3f4a Ermal Luçi
	interface_bring_down($interface);
1149 7a18dfa4 lgcosta
	interface_configure($interface, $reloadall);
1150 80bf3f4a Ermal Luçi
}
1151
1152 91a38e1f Ermal
function interface_vip_bring_down($vip) {
1153 962fd685 Ermal
	global $g;
1154
1155 28268a4a Renato Botelho
	if (strpos($vip['interface'], '_vip')) {
1156 cf73302f Phil Davis
		if (is_ipaddrv6($vip['subnet'])) {
1157 9d26addd Renato Botelho
			$family = 'inet6';
1158 cf73302f Phil Davis
		} else {
1159 9d26addd Renato Botelho
			$family = 'inet';
1160 cf73302f Phil Davis
		}
1161 9d26addd Renato Botelho
1162
		$carpvip = get_configured_carp_interface_list($vip['interface'], $family, 'vip');
1163 28268a4a Renato Botelho
		$iface = $carpvip['interface'];
1164 cf73302f Phil Davis
	} else {
1165 28268a4a Renato Botelho
		$iface = $vip['interface'];
1166 cf73302f Phil Davis
	}
1167 28268a4a Renato Botelho
1168
	$vipif = get_real_interface($iface);
1169 abcb2bed Ermal Lu?i
	switch ($vip['mode']) {
1170 cf73302f Phil Davis
		case "proxyarp":
1171
			if (file_exists("{$g['varrun_path']}/choparp_{$vipif}.pid")) {
1172
				killbypid("{$g['varrun_path']}/choparp_{$vipif}.pid");
1173
			}
1174
			break;
1175
		case "ipalias":
1176
			if (does_interface_exist($vipif)) {
1177
				if (is_ipaddrv6($vip['subnet'])) {
1178
					mwexec("/sbin/ifconfig {$vipif} inet6 " . escapeshellarg($vip['subnet']) . " -alias");
1179
				} else {
1180
					pfSense_interface_deladdress($vipif, $vip['subnet']);
1181
				}
1182
			}
1183
			break;
1184
		case "carp":
1185
			/* XXX: Is enough to delete ip address? */
1186
			if (does_interface_exist($vipif)) {
1187
				if (is_ipaddrv6($vip['subnet'])) {
1188
					mwexec("/sbin/ifconfig {$vipif} inet6 " . escapeshellarg($vip['subnet']) . " delete");
1189
				} else {
1190
					pfSense_interface_deladdress($vipif, $vip['subnet']);
1191
				}
1192
			}
1193
			break;
1194 abcb2bed Ermal Lu?i
	}
1195
}
1196
1197 9343d750 Ermal
function interface_bring_down($interface = "wan", $destroy = false, $ifacecfg = false) {
1198 80bf3f4a Ermal Luçi
	global $config, $g;
1199
1200 cf73302f Phil Davis
	if (!isset($config['interfaces'][$interface])) {
1201 be45aa79 Renato Botelho
		return;
1202 cf73302f Phil Davis
	}
1203 203e4bb6 Ermal
1204 cf73302f Phil Davis
	if ($g['debug']) {
1205 203e4bb6 Ermal
		log_error("Calling interface down for interface {$interface}, destroy is " . (($destroy) ? 'true' : 'false'));
1206 cf73302f Phil Davis
	}
1207 37fb708c smos
1208 45c07f16 Ermal
	/*
1209
	 * NOTE: The $realifv6 is needed when WANv4 is type PPP and v6 is DHCP and the option v6 from v4 is used.
1210
	 * In this case the real $realif of v4 is different from that of v6 for operation.
1211
	 * Keep this in mind while doing changes here!
1212
	 */
1213 e12ad49f Renato Botelho
	if ($ifacecfg === false) {
1214 9343d750 Ermal
		$ifcfg = $config['interfaces'][$interface];
1215 e12ad49f Renato Botelho
		$ppps = $config['ppps']['ppp'];
1216
		$realif = get_real_interface($interface);
1217 45c07f16 Ermal
		$realifv6 = get_real_interface($interface, "inet6", true);
1218 e12ad49f Renato Botelho
	} elseif (!is_array($ifacecfg)) {
1219 9343d750 Ermal
		log_error(gettext("Wrong parameters used during interface_bring_down"));
1220 a55dd537 Ermal
		$ifcfg = $config['interfaces'][$interface];
1221
		$ppps = $config['ppps']['ppp'];
1222
		$realif = get_real_interface($interface);
1223 45c07f16 Ermal
		$realifv6 = get_real_interface($interface, "inet6", true);
1224 e12ad49f Renato Botelho
	} else {
1225
		$ifcfg = $ifacecfg['ifcfg'];
1226
		$ppps = $ifacecfg['ppps'];
1227 45c07f16 Ermal
		if (isset($ifacecfg['ifcfg']['realif'])) {
1228 e12ad49f Renato Botelho
			$realif = $ifacecfg['ifcfg']['realif'];
1229 45c07f16 Ermal
			/* XXX: Any better way? */
1230
			$realifv6 = $realif;
1231
		} else {
1232 e12ad49f Renato Botelho
			$realif = get_real_interface($interface);
1233 45c07f16 Ermal
			$realifv6 = get_real_interface($interface, "inet6", true);
1234
		}
1235 e12ad49f Renato Botelho
	}
1236 80bf3f4a Ermal Luçi
1237
	switch ($ifcfg['ipaddr']) {
1238 cf73302f Phil Davis
		case "ppp":
1239
		case "pppoe":
1240
		case "pptp":
1241
		case "l2tp":
1242
			if (is_array($ppps) && count($ppps)) {
1243
				foreach ($ppps as $pppid => $ppp) {
1244
					if ($realif == $ppp['if']) {
1245
						if (isset($ppp['ondemand']) && !$destroy) {
1246
							send_event("interface reconfigure {$interface}");
1247
							break;
1248
						}
1249
						if (file_exists("{$g['varrun_path']}/{$ppp['type']}_{$interface}.pid")) {
1250
							killbypid("{$g['varrun_path']}/{$ppp['type']}_{$interface}.pid");
1251
							sleep(2);
1252
						}
1253
						unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
1254 c8d23069 gnhb
						break;
1255
					}
1256 a138f4fb Ermal
				}
1257
			}
1258 cf73302f Phil Davis
			break;
1259
		case "dhcp":
1260
			kill_dhclient_process($realif);
1261
			unlink_if_exists("{$g['varetc_path']}/dhclient_{$interface}.conf");
1262
			if (does_interface_exist("$realif")) {
1263
				mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
1264
				interface_ipalias_cleanup($interface);
1265
				if ($destroy == true) {
1266
					pfSense_interface_flags($realif, -IFF_UP);
1267
				}
1268
				mwexec("/usr/sbin/arp -d -i " . escapeshellarg($realif) . " -a");
1269
			}
1270
			break;
1271
		default:
1272
			if (does_interface_exist("$realif")) {
1273
				mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
1274
				interface_ipalias_cleanup($interface);
1275
				if ($destroy == true) {
1276
					pfSense_interface_flags($realif, -IFF_UP);
1277
				}
1278
				mwexec("/usr/sbin/arp -d -i " . escapeshellarg($realif) . " -a");
1279
			}
1280
			break;
1281 37fb708c smos
	}
1282
1283 92977616 Ermal
	$track6 = array();
1284 37fb708c smos
	switch ($ifcfg['ipaddrv6']) {
1285 cf73302f Phil Davis
		case "slaac":
1286
		case "dhcp6":
1287
			$pidv6 = find_dhcp6c_process($realif);
1288
			if ($pidv6) {
1289
				posix_kill($pidv6, SIGTERM);
1290
			}
1291
			sleep(3);
1292
			unlink_if_exists("{$g['varetc_path']}/dhcp6c_{$interface}.conf");
1293 db0ec10e k-paulius
			unlink_if_exists("{$g['varetc_path']}/dhcp6c_{$interface}_script.sh");
1294
			unlink_if_exists("{$g['varetc_path']}/rtsold_{$realifv6}_script.sh");
1295 cf73302f Phil Davis
			if (does_interface_exist($realifv6)) {
1296
				$ip6 = find_interface_ipv6($realifv6);
1297
				if (is_ipaddrv6($ip6) && $ip6 != "::") {
1298
					mwexec("/sbin/ifconfig " . escapeshellarg($realifv6) . " inet6 {$ip6} delete", true);
1299
				}
1300
				interface_ipalias_cleanup($interface, "inet6");
1301
				if ($destroy == true) {
1302
					pfSense_interface_flags($realif, -IFF_UP);
1303
				}
1304
				//mwexec("/usr/sbin/arp -d -i " . escapeshellarg($realif) . " -a");
1305
			}
1306
			$track6 = link_interface_to_track6($interface);
1307
			break;
1308
		case "6rd":
1309
		case "6to4":
1310
			$realif = "{$interface}_stf";
1311
			if (does_interface_exist("$realif")) {
1312 df966626 Chris Buechler
				/* destroy stf interface if tunnel is being disabled or tunnel type is being changed */
1313
				if (($ifcfg['ipaddrv6'] == '6rd' && (!isset($config['interfaces'][$interface]['ipaddrv6']) || $config['interfaces'][$interface]['ipaddrv6'] != '6rd')) ||
1314 6990ad35 Phil Davis
				    ($ifcfg['ipaddrv6'] == '6to4' && (!isset($config['interfaces'][$interface]['ipaddrv6']) || $config['interfaces'][$interface]['ipaddrv6'] != '6to4'))) {
1315 df966626 Chris Buechler
					$destroy = true;
1316
				} else {
1317
					/* get_interface_ipv6() returns empty value if interface is being disabled */
1318
					$ip6 = get_interface_ipv6($interface);
1319 086cf944 Phil Davis
					if (is_ipaddrv6($ip6)) {
1320 df966626 Chris Buechler
						mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " inet6 {$ip6} delete", true);
1321 086cf944 Phil Davis
					}
1322 cf73302f Phil Davis
				}
1323
				interface_ipalias_cleanup($interface, "inet6");
1324
				if ($destroy == true) {
1325
					pfSense_interface_flags($realif, -IFF_UP);
1326
				}
1327
			}
1328
			$track6 = link_interface_to_track6($interface);
1329
			break;
1330
		default:
1331
			if (does_interface_exist("$realif")) {
1332
				$ip6 = get_interface_ipv6($interface);
1333
				if (is_ipaddrv6($ip6)) {
1334
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " inet6 {$ip6} delete", true);
1335
				}
1336
				if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
1337
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " inet6 {$ifcfg['ipaddrv6']} delete", true);
1338
				}
1339
				interface_ipalias_cleanup($interface, "inet6");
1340
				if ($destroy == true) {
1341
					pfSense_interface_flags($realif, -IFF_UP);
1342
				}
1343
				//mwexec("/usr/sbin/arp -d -i " . escapeshellarg($realif) . " -a");
1344
			}
1345
			$track6 = link_interface_to_track6($interface);
1346
			break;
1347 80bf3f4a Ermal Luçi
	}
1348 eb772abd Scott Ullrich
1349 92977616 Ermal
	if (!empty($track6) && is_array($track6)) {
1350 cf73302f Phil Davis
		if (!function_exists('services_dhcpd_configure')) {
1351 34340c56 Ermal
			require_once('services.inc');
1352 cf73302f Phil Davis
		}
1353 34340c56 Ermal
		/* Bring down radvd and dhcp6 on these interfaces */
1354
		services_dhcpd_configure('inet6', $track6);
1355 92977616 Ermal
	}
1356
1357 ff3da5db Renato Botelho
	$old_router = '';
1358 cf73302f Phil Davis
	if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
1359 97f7a517 jim-p
		$old_router = trim(file_get_contents("{$g['tmp_path']}/{$realif}_router"));
1360 cf73302f Phil Davis
	}
1361 37fb708c smos
1362 73ee49f2 gnhb
	/* remove interface up file if it exists */
1363
	unlink_if_exists("{$g['tmp_path']}/{$realif}up");
1364
	unlink_if_exists("{$g['vardb_path']}/{$interface}ip");
1365 c495f88b Seth Mos
	unlink_if_exists("{$g['vardb_path']}/{$interface}ipv6");
1366 73ee49f2 gnhb
	unlink_if_exists("{$g['tmp_path']}/{$realif}_router");
1367 c495f88b Seth Mos
	unlink_if_exists("{$g['tmp_path']}/{$realif}_routerv6");
1368 86dcdfc9 Ermal
	unlink_if_exists("{$g['varetc_path']}/nameserver_{$realif}");
1369
	unlink_if_exists("{$g['varetc_path']}/searchdomain_{$realif}");
1370 be45aa79 Renato Botelho
1371 b5582f49 Erik Fonnesbeck
	/* hostapd and wpa_supplicant do not need to be running when the interface is down.
1372
	 * They will also use 100% CPU if running after the wireless clone gets deleted. */
1373
	if (is_array($ifcfg['wireless'])) {
1374 97f3ce0f Phil Davis
		kill_hostapd($realif);
1375 b5582f49 Erik Fonnesbeck
		mwexec(kill_wpasupplicant($realif));
1376
	}
1377
1378 97973ed8 Ermal Luçi
	if ($destroy == true) {
1379 cf73302f Phil Davis
		if (preg_match("/^[a-z0-9]+^tun|^ovpn|^gif|^gre|^lagg|^bridge|vlan|_stf$/i", $realif)) {
1380 871768cf Ermal
			pfSense_interface_destroy($realif);
1381 cf73302f Phil Davis
		}
1382 be45aa79 Renato Botelho
	}
1383 9006e9f8 Scott Ullrich
1384 80bf3f4a Ermal Luçi
	return;
1385 5b237745 Scott Ullrich
}
1386
1387 cf73302f Phil Davis
function interfaces_carp_set_maintenancemode($carp_maintenancemode) {
1388 783f1ee2 PiBa-NL
	global $config;
1389
	if (isset($config["virtualip_carp_maintenancemode"]) && $carp_maintenancemode == false) {
1390
		unset($config["virtualip_carp_maintenancemode"]);
1391
		write_config("Leave CARP maintenance mode");
1392 cf73302f Phil Davis
	} else if (!isset($config["virtualip_carp_maintenancemode"]) && $carp_maintenancemode == true) {
1393 57f58894 Renato Botelho
		$config["virtualip_carp_maintenancemode"] = true;
1394
		write_config("Enter CARP maintenance mode");
1395 783f1ee2 PiBa-NL
	}
1396
1397
	$viparr = &$config['virtualip']['vip'];
1398
	foreach ($viparr as $vip) {
1399
		if ($vip['mode'] == "carp") {
1400
			interface_carp_configure($vip);
1401
		}
1402
	}
1403
}
1404
1405 7a63d5d0 Ermal LUÇI
function interface_isppp_type($interface) {
1406
	global $config;
1407
1408 cf73302f Phil Davis
	if (!is_array($config['interfaces'][$interface])) {
1409 7a63d5d0 Ermal LUÇI
		return false;
1410 cf73302f Phil Davis
	}
1411 7a63d5d0 Ermal LUÇI
1412
	switch ($config['interfaces'][$interface]['ipaddr']) {
1413 cf73302f Phil Davis
		case 'pptp':
1414
		case 'l2tp':
1415
		case 'pppoe':
1416
		case 'ppp':
1417
			return true;
1418
			break;
1419
		default:
1420
			return false;
1421
			break;
1422 7a63d5d0 Ermal LUÇI
	}
1423
}
1424
1425 e5d558bf gnhb
function interfaces_ptpid_used($ptpid) {
1426
	global $config;
1427
1428 cf73302f Phil Davis
	if (is_array($config['ppps']['ppp'])) {
1429
		foreach ($config['ppps']['ppp'] as & $settings) {
1430
			if ($ptpid == $settings['ptpid']) {
1431 e5d558bf gnhb
				return true;
1432 cf73302f Phil Davis
			}
1433
		}
1434
	}
1435 e5d558bf gnhb
1436
	return false;
1437
}
1438
1439
function interfaces_ptpid_next() {
1440
1441
	$ptpid = 0;
1442 cf73302f Phil Davis
	while (interfaces_ptpid_used($ptpid)) {
1443 e5d558bf gnhb
		$ptpid++;
1444 cf73302f Phil Davis
	}
1445 e5d558bf gnhb
1446
	return $ptpid;
1447
}
1448
1449 70e46e62 Ermal
function getMPDCRONSettings($pppif) {
1450 e5d558bf gnhb
	global $config;
1451 70e46e62 Ermal
1452
	$cron_cmd_file = "{$g['varetc_path']}/pppoe_restart_{$pppif}";
1453 e5d558bf gnhb
	if (is_array($config['cron']['item'])) {
1454 70e46e62 Ermal
		foreach ($config['cron']['item'] as $i => $item) {
1455 cf73302f Phil Davis
			if (stripos($item['command'], $cron_cmd_file) !== false) {
1456 e5d558bf gnhb
				return array("ID" => $i, "ITEM" => $item);
1457 cf73302f Phil Davis
			}
1458 e5d558bf gnhb
		}
1459
	}
1460 70e46e62 Ermal
1461 e5d558bf gnhb
	return NULL;
1462
}
1463
1464
function handle_pppoe_reset($post_array) {
1465
	global $config, $g;
1466
1467 70e46e62 Ermal
	$pppif = "{$post_array['type']}{$post_array['ptpid']}";
1468
	$cron_cmd_file = "{$g['varetc_path']}/pppoe_restart_{$pppif}";
1469 5c8e8a17 gnhb
1470 cf73302f Phil Davis
	if (!is_array($config['cron']['item'])) {
1471 be45aa79 Renato Botelho
		$config['cron']['item'] = array();
1472 cf73302f Phil Davis
	}
1473 70e46e62 Ermal
1474 1d7e1d6c gnhb
	$itemhash = getMPDCRONSettings($pppif);
1475 be45aa79 Renato Botelho
1476 e5d558bf gnhb
	// reset cron items if necessary and return
1477
	if (empty($post_array['pppoe-reset-type'])) {
1478 cf73302f Phil Davis
		if (isset($itemhash)) {
1479 e5d558bf gnhb
			unset($config['cron']['item'][$itemhash['ID']]);
1480 cf73302f Phil Davis
		}
1481 e5d558bf gnhb
		sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP");
1482
		return;
1483
	}
1484
1485 cf73302f Phil Davis
	if (empty($itemhash)) {
1486 70e46e62 Ermal
		$itemhash = array();
1487 cf73302f Phil Davis
	}
1488 70e46e62 Ermal
	$item = array();
1489 e5d558bf gnhb
	if (isset($post_array['pppoe-reset-type']) && $post_array['pppoe-reset-type'] == "custom") {
1490
		$item['minute'] = $post_array['pppoe_resetminute'];
1491
		$item['hour'] = $post_array['pppoe_resethour'];
1492
		if (isset($post_array['pppoe_resetdate']) && $post_array['pppoe_resetdate'] <> "") {
1493
			$date = explode("/", $post_array['pppoe_resetdate']);
1494
			$item['mday'] = $date[1];
1495
			$item['month'] = $date[0];
1496
		} else {
1497
			$item['mday'] = "*";
1498
			$item['month'] = "*";
1499
		}
1500
		$item['wday'] = "*";
1501
		$item['who'] = "root";
1502 70e46e62 Ermal
		$item['command'] = $cron_cmd_file;
1503 e5d558bf gnhb
	} else if (isset($post_array['pppoe-reset-type']) && $post_array['pppoe-reset-type'] == "preset") {
1504
		switch ($post_array['pppoe_pr_preset_val']) {
1505 cf73302f Phil Davis
			case "monthly":
1506
				$item['minute'] = "0";
1507
				$item['hour'] = "0";
1508
				$item['mday'] = "1";
1509
				$item['month'] = "*";
1510
				$item['wday'] = "*";
1511
				break;
1512
			case "weekly":
1513
				$item['minute'] = "0";
1514
				$item['hour'] = "0";
1515
				$item['mday'] = "*";
1516
				$item['month'] = "*";
1517
				$item['wday'] = "0";
1518
				break;
1519
			case "daily":
1520
				$item['minute'] = "0";
1521
				$item['hour'] = "0";
1522
				$item['mday'] = "*";
1523
				$item['month'] = "*";
1524
				$item['wday'] = "*";
1525
				break;
1526
			case "hourly":
1527
				$item['minute'] = "0";
1528
				$item['hour'] = "*";
1529
				$item['mday'] = "*";
1530
				$item['month'] = "*";
1531
				$item['wday'] = "*";
1532
				break;
1533 e5d558bf gnhb
		} // end switch
1534 70e46e62 Ermal
		$item['who'] = "root";
1535
		$item['command'] = $cron_cmd_file;
1536
	}
1537 cf73302f Phil Davis
	if (empty($item)) {
1538 70e46e62 Ermal
		return;
1539 cf73302f Phil Davis
	}
1540
	if (isset($itemhash['ID'])) {
1541 f1593bfe Phil Davis
		$config['cron']['item'][$itemhash['ID']] = $item;
1542 cf73302f Phil Davis
	} else {
1543 e5d558bf gnhb
		$config['cron']['item'][] = $item;
1544 cf73302f Phil Davis
	}
1545 e5d558bf gnhb
}
1546
1547 70e46e62 Ermal
/*
1548
 * This function can configure PPPoE, MLPPP (PPPoE), PPTP.
1549
 * It writes the mpd config file to /var/etc every time the link is opened.
1550
 */
1551 cb37d8fa gnhb
function interface_ppps_configure($interface) {
1552
	global $config, $g;
1553 01c201e3 Ermal
1554
	/* Return for unassigned interfaces. This is a minimum requirement. */
1555 cf73302f Phil Davis
	if (empty($config['interfaces'][$interface])) {
1556 01c201e3 Ermal
		return 0;
1557 cf73302f Phil Davis
	}
1558 01c201e3 Ermal
	$ifcfg = $config['interfaces'][$interface];
1559 cf73302f Phil Davis
	if (!isset($ifcfg['enable'])) {
1560 01c201e3 Ermal
		return 0;
1561 cf73302f Phil Davis
	}
1562 01c201e3 Ermal
1563 3a906378 gnhb
	// mpd5 requires a /var/spool/lock directory for PPP modem links.
1564 cf73302f Phil Davis
	if (!is_dir("/var/spool/lock")) {
1565 873c1701 Renato Botelho
		mkdir("/var/spool/lock", 0777, true);
1566 3a906378 gnhb
	}
1567 7e631290 smos
	// mpd5 modem chat script expected in the same directory as the mpd_xxx.conf files
1568 cf73302f Phil Davis
	if (!file_exists("{$g['varetc_path']}/mpd.script")) {
1569 80d708de Renato Botelho
		@symlink("/usr/local/sbin/mpd.script", "{$g['varetc_path']}/mpd.script");
1570 cf73302f Phil Davis
	}
1571 01c201e3 Ermal
1572 cb37d8fa gnhb
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1573
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1574 cf73302f Phil Davis
			if ($ifcfg['if'] == $ppp['if']) {
1575 cb37d8fa gnhb
				break;
1576 cf73302f Phil Davis
			}
1577 cb37d8fa gnhb
		}
1578
	}
1579 cf73302f Phil Davis
	if (!$ppp || $ifcfg['if'] != $ppp['if']) {
1580 07e40c1f Carlos Eduardo Ramos
		log_error(sprintf(gettext("Can't find PPP config for %s in interface_ppps_configure()."), $ifcfg['if']));
1581 3a906378 gnhb
		return 0;
1582 cb37d8fa gnhb
	}
1583 3a906378 gnhb
	$pppif = $ifcfg['if'];
1584 cf73302f Phil Davis
	if ($ppp['type'] == "ppp") {
1585 cb37d8fa gnhb
		$type = "modem";
1586 cf73302f Phil Davis
	} else {
1587 cb37d8fa gnhb
		$type = $ppp['type'];
1588 cf73302f Phil Davis
	}
1589 be45aa79 Renato Botelho
	$upper_type = strtoupper($ppp['type']);
1590 01c201e3 Ermal
1591 08003661 Ermal LUÇI
	/* XXX: This does not make sense and may create trouble
1592
	 * comment it for now to be removed later on.
1593 285ef132 Ermal LUÇI
	if (platform_booting()) {
1594 bfbb9bc0 Ermal
		$descr = isset($ifcfg['descr']) ? $ifcfg['descr'] : strtoupper($interface);
1595 3a90c973 gnhb
		echo "starting {$pppif} link...";
1596 cf73302f Phil Davis
		if (isvalidpid("{$g['varrun_path']}/{$ppp['type']}_{$interface}.pid"))
1597 3a906378 gnhb
			return 0;
1598
	}
1599 08003661 Ermal LUÇI
	*/
1600 01c201e3 Ermal
1601 086cf944 Phil Davis
	$ports = explode(',', $ppp['ports']);
1602 bfbb9bc0 Ermal
	if ($type != "modem") {
1603 8e9d1265 Renato Botelho
		foreach ($ports as $pid => $port) {
1604 bfbb9bc0 Ermal
			$ports[$pid] = get_real_interface($port);
1605 cf73302f Phil Davis
			if (empty($ports[$pid])) {
1606 8e9d1265 Renato Botelho
				return 0;
1607 cf73302f Phil Davis
			}
1608 8e9d1265 Renato Botelho
		}
1609 bfbb9bc0 Ermal
	}
1610 086cf944 Phil Davis
	$localips = explode(',', $ppp['localip']);
1611
	$gateways = explode(',', $ppp['gateway']);
1612
	$subnets = explode(',', $ppp['subnet']);
1613 01c201e3 Ermal
1614 3a906378 gnhb
	/* We bring up the parent interface first because if DHCP is configured on the parent we need
1615 01c201e3 Ermal
	 * to obtain an address first so we can write it in the mpd .conf file for PPTP and L2TP configs
1616
	 */
1617 cf73302f Phil Davis
	foreach ($ports as $pid => $port) {
1618 23721285 gnhb
		switch ($ppp['type']) {
1619 be45aa79 Renato Botelho
			case "pppoe":
1620 3a906378 gnhb
				/* Bring the parent interface up */
1621
				interfaces_bring_up($port);
1622 3d04de61 Ermal
				pfSense_ngctl_attach(".", $port);
1623 84086442 Renato Botelho
				/* Enable setautosrc to automatically change mac address if parent interface's changes */
1624
				mwexec("ngctl msg {$port}: setautosrc 1");
1625 3a906378 gnhb
				break;
1626
			case "pptp":
1627
			case "l2tp":
1628
				/* configure interface */
1629 cf73302f Phil Davis
				if (is_ipaddr($localips[$pid])) {
1630 3a906378 gnhb
					// Manually configure interface IP/subnet
1631 bfbb9bc0 Ermal
					pfSense_interface_setaddress($port, "{$localips[$pid]}/{$subnets[$pid]}");
1632
					interfaces_bring_up($port);
1633 cf73302f Phil Davis
				} else if (empty($localips[$pid])) {
1634 69c1b043 gnhb
					$localips[$pid] = get_interface_ip($port); // try to get the interface IP from the port
1635 cf73302f Phil Davis
				}
1636 be45aa79 Renato Botelho
1637 cf73302f Phil Davis
				if (!is_ipaddr($localips[$pid])) {
1638 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!");
1639
					$localips[$pid] = "0.0.0.0";
1640 3a906378 gnhb
				}
1641 cf73302f Phil Davis
				if (!is_ipaddr($gateways[$pid])) {
1642 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));
1643 69c1b043 gnhb
					return 0;
1644 3a906378 gnhb
				}
1645 3d04de61 Ermal
				pfSense_ngctl_attach(".", $port);
1646 3a906378 gnhb
				break;
1647
			case "ppp":
1648
				if (!file_exists("{$port}")) {
1649 07e40c1f Carlos Eduardo Ramos
					log_error(sprintf(gettext("Device %s does not exist. PPP link cannot start without the modem device."), $port));
1650 23721285 gnhb
					return 0;
1651 3a906378 gnhb
				}
1652
				break;
1653
			default:
1654 f416763b Phil Davis
				log_error(sprintf(gettext("Unknown %s configured as ppp interface."), $type));
1655 3a906378 gnhb
				break;
1656
		}
1657
	}
1658 be45aa79 Renato Botelho
1659 cf73302f Phil Davis
	if (is_array($ports) && count($ports) > 1) {
1660 cb37d8fa gnhb
		$multilink = "enable";
1661 cf73302f Phil Davis
	} else {
1662 cb37d8fa gnhb
		$multilink = "disable";
1663 cf73302f Phil Davis
	}
1664 be45aa79 Renato Botelho
1665 cf73302f Phil Davis
	if ($type == "modem") {
1666
		if (is_ipaddr($ppp['localip'])) {
1667 cb37d8fa gnhb
			$localip = $ppp['localip'];
1668 cf73302f Phil Davis
		} else {
1669 cb37d8fa gnhb
			$localip = '0.0.0.0';
1670 cf73302f Phil Davis
		}
1671 cb37d8fa gnhb
1672 cf73302f Phil Davis
		if (is_ipaddr($ppp['gateway'])) {
1673 cb37d8fa gnhb
			$gateway = $ppp['gateway'];
1674 cf73302f Phil Davis
		} else {
1675 23721285 gnhb
			$gateway = "10.64.64.{$pppid}";
1676 cf73302f Phil Davis
		}
1677 cb37d8fa gnhb
		$ranges = "{$localip}/0 {$gateway}/0";
1678 be45aa79 Renato Botelho
1679 cf73302f Phil Davis
		if (empty($ppp['apnum'])) {
1680 3a906378 gnhb
			$ppp['apnum'] = 1;
1681 cf73302f Phil Davis
		}
1682
	} else {
1683 cb37d8fa gnhb
		$ranges = "0.0.0.0/0 0.0.0.0/0";
1684 cf73302f Phil Davis
	}
1685 0661b194 gnhb
1686 cf73302f Phil Davis
	if (isset($ppp['ondemand'])) {
1687 cb37d8fa gnhb
		$ondemand = "enable";
1688 cf73302f Phil Davis
	} else {
1689 cb37d8fa gnhb
		$ondemand = "disable";
1690 cf73302f Phil Davis
	}
1691
	if (!isset($ppp['idletimeout'])) {
1692 cb37d8fa gnhb
		$ppp['idletimeout'] = 0;
1693 cf73302f Phil Davis
	}
1694 64d124c5 gnhb
1695 cf73302f Phil Davis
	if (empty($ppp['username']) && $type == "modem") {
1696 cb37d8fa gnhb
		$ppp['username'] = "user";
1697
		$ppp['password'] = "none";
1698
	}
1699 cf73302f Phil Davis
	if (empty($ppp['password']) && $type == "modem") {
1700 00b702cc gnhb
		$passwd = "none";
1701 cf73302f Phil Davis
	} else {
1702 00b702cc gnhb
		$passwd = base64_decode($ppp['password']);
1703 cf73302f Phil Davis
	}
1704 0661b194 gnhb
1705 086cf944 Phil Davis
	$bandwidths = explode(',', $ppp['bandwidth']);
1706 6805d2d2 Ermal
	$defaultmtu = "1492";
1707 cf73302f Phil Davis
	if (!empty($ifcfg['mtu'])) {
1708 6805d2d2 Ermal
		$defaultmtu = intval($ifcfg['mtu']);
1709 cf73302f Phil Davis
	}
1710 086cf944 Phil Davis
	$mtus = explode(',', $ppp['mtu']);
1711
	$mrus = explode(',', $ppp['mru']);
1712 0661b194 gnhb
1713 cf73302f Phil Davis
	if (isset($ppp['mrru'])) {
1714 086cf944 Phil Davis
		$mrrus = explode(',', $ppp['mrru']);
1715 cf73302f Phil Davis
	}
1716 c1cc447c gnhb
1717 cb37d8fa gnhb
	// Construct the mpd.conf file
1718
	$mpdconf = <<<EOD
1719
startup:
1720
	# configure the console
1721
	set console close
1722
	# configure the web server
1723
	set web close
1724
1725
default:
1726
{$ppp['type']}client:
1727
	create bundle static {$interface}
1728 07dfd121 Seth Mos
	set bundle enable ipv6cp
1729 cb37d8fa gnhb
	set iface name {$pppif}
1730
1731
EOD;
1732 0661b194 gnhb
	$setdefaultgw = false;
1733
	$founddefaultgw = false;
1734
	if (is_array($config['gateways']['gateway_item'])) {
1735 cf73302f Phil Davis
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1736
			if ($interface == $gateway['interface'] && isset($gateway['defaultgw'])) {
1737 0661b194 gnhb
				$setdefaultgw = true;
1738
				break;
1739
			} else if (isset($gateway['defaultgw']) && !empty($gateway['interface'])) {
1740
				$founddefaultgw = true;
1741
				break;
1742
			}
1743
		}
1744
	}
1745 be45aa79 Renato Botelho
1746 cf73302f Phil Davis
	if (($interface == "wan" && $founddefaultgw == false) || $setdefaultgw == true) {
1747 82effddb gnhb
		$setdefaultgw = true;
1748 cb37d8fa gnhb
		$mpdconf .= <<<EOD
1749
	set iface route default
1750
1751
EOD;
1752 82effddb gnhb
	}
1753 cb37d8fa gnhb
	$mpdconf .= <<<EOD
1754
	set iface {$ondemand} on-demand
1755
	set iface idle {$ppp['idletimeout']}
1756
1757
EOD;
1758
1759 cf73302f Phil Davis
	if (isset($ppp['ondemand'])) {
1760 cb37d8fa gnhb
		$mpdconf .= <<<EOD
1761 55f3ca1d gnhb
	set iface addrs 10.10.1.1 10.10.1.2
1762 cb37d8fa gnhb
1763
EOD;
1764 cf73302f Phil Davis
	}
1765 be45aa79 Renato Botelho
1766 cf73302f Phil Davis
	if (isset($ppp['tcpmssfix'])) {
1767 8adc1e49 gnhb
		$tcpmss = "disable";
1768 cf73302f Phil Davis
	} else {
1769 8adc1e49 gnhb
		$tcpmss = "enable";
1770 cf73302f Phil Davis
	}
1771
	$mpdconf .= <<<EOD
1772 8adc1e49 gnhb
	set iface {$tcpmss} tcpmssfix
1773 64d124c5 gnhb
1774
EOD;
1775 0661b194 gnhb
1776 cb37d8fa gnhb
	$mpdconf .= <<<EOD
1777
	set iface up-script /usr/local/sbin/ppp-linkup
1778
	set iface down-script /usr/local/sbin/ppp-linkdown
1779
	set ipcp ranges {$ranges}
1780
1781
EOD;
1782 cf73302f Phil Davis
	if (isset($ppp['vjcomp'])) {
1783 cb37d8fa gnhb
		$mpdconf .= <<<EOD
1784 64d124c5 gnhb
	set ipcp no vjcomp
1785 cb37d8fa gnhb
1786
EOD;
1787 cf73302f Phil Davis
	}
1788 cb37d8fa gnhb
1789 cf73302f Phil Davis
	if (isset($config['system']['dnsallowoverride'])) {
1790 64d124c5 gnhb
		$mpdconf .= <<<EOD
1791
	set ipcp enable req-pri-dns
1792
	set ipcp enable req-sec-dns
1793
1794
EOD;
1795 cf73302f Phil Davis
	}
1796
1797
	if (!isset($ppp['verbose_log'])) {
1798 23721285 gnhb
		$mpdconf .= <<<EOD
1799 5d9d443a gnhb
	#log -bund -ccp -chat -iface -ipcp -lcp -link
1800 0661b194 gnhb
1801 23721285 gnhb
EOD;
1802 cf73302f Phil Davis
	}
1803
1804
	foreach ($ports as $pid => $port) {
1805 bfbb9bc0 Ermal
		$port = get_real_interface($port);
1806 00b702cc gnhb
		$mpdconf .= <<<EOD
1807 cb37d8fa gnhb
1808 0661b194 gnhb
	create link static {$interface}_link{$pid} {$type}
1809 cb37d8fa gnhb
	set link action bundle {$interface}
1810
	set link {$multilink} multilink
1811
	set link keep-alive 10 60
1812
	set link max-redial 0
1813 64d124c5 gnhb
1814
EOD;
1815 cf73302f Phil Davis
		if (isset($ppp['shortseq'])) {
1816 00b702cc gnhb
			$mpdconf .= <<<EOD
1817 64d124c5 gnhb
	set link no shortseq
1818
1819
EOD;
1820 cf73302f Phil Davis
		}
1821 0661b194 gnhb
1822 cf73302f Phil Davis
		if (isset($ppp['acfcomp'])) {
1823 00b702cc gnhb
			$mpdconf .= <<<EOD
1824 64d124c5 gnhb
	set link no acfcomp
1825
1826
EOD;
1827 cf73302f Phil Davis
		}
1828 0661b194 gnhb
1829 cf73302f Phil Davis
		if (isset($ppp['protocomp'])) {
1830 00b702cc gnhb
			$mpdconf .= <<<EOD
1831 64d124c5 gnhb
	set link no protocomp
1832
1833
EOD;
1834 cf73302f Phil Davis
		}
1835 0661b194 gnhb
1836 00b702cc gnhb
		$mpdconf .= <<<EOD
1837 cb37d8fa gnhb
	set link disable chap pap
1838
	set link accept chap pap eap
1839 64d124c5 gnhb
	set link disable incoming
1840 cb37d8fa gnhb
1841
EOD;
1842 00b702cc gnhb
1843
1844 cf73302f Phil Davis
		if (!empty($bandwidths[$pid])) {
1845 00b702cc gnhb
			$mpdconf .= <<<EOD
1846
	set link bandwidth {$bandwidths[$pid]}
1847 cb37d8fa gnhb
1848
EOD;
1849 cf73302f Phil Davis
		}
1850 0661b194 gnhb
1851 cf73302f Phil Davis
		if (empty($mtus[$pid])) {
1852 6805d2d2 Ermal
			$mtus[$pid] = $defaultmtu;
1853 cf73302f Phil Davis
		}
1854
		$mpdconf .= <<<EOD
1855 00b702cc gnhb
	set link mtu {$mtus[$pid]}
1856 cb37d8fa gnhb
1857
EOD;
1858 0661b194 gnhb
1859 cf73302f Phil Davis
		if (!empty($mrus[$pid])) {
1860 00b702cc gnhb
			$mpdconf .= <<<EOD
1861
	set link mru {$mrus[$pid]}
1862
1863 6a30f701 gnhb
EOD;
1864 cf73302f Phil Davis
		}
1865 6a30f701 gnhb
1866 cf73302f Phil Davis
		if (!empty($mrrus[$pid])) {
1867 6a30f701 gnhb
			$mpdconf .= <<<EOD
1868
	set link mrru {$mrrus[$pid]}
1869
1870 00b702cc gnhb
EOD;
1871 cf73302f Phil Davis
		}
1872 0661b194 gnhb
1873 00b702cc gnhb
		$mpdconf .= <<<EOD
1874 cb37d8fa gnhb
	set auth authname "{$ppp['username']}"
1875
	set auth password {$passwd}
1876
1877
EOD;
1878 00b702cc gnhb
		if ($type == "modem") {
1879
			$mpdconf .= <<<EOD
1880 cb37d8fa gnhb
	set modem device {$ppp['ports']}
1881
	set modem script DialPeer
1882 73472985 Ermal
	set modem idle-script Ringback
1883 cb37d8fa gnhb
	set modem watch -cd
1884
	set modem var \$DialPrefix "DT"
1885
	set modem var \$Telephone "{$ppp['phone']}"
1886
1887
EOD;
1888 00b702cc gnhb
		}
1889
		if (isset($ppp['connect-timeout']) && $type == "modem") {
1890
			$mpdconf .= <<<EOD
1891 cb37d8fa gnhb
	set modem var \$ConnectTimeout "{$ppp['connect-timeout']}"
1892
1893
EOD;
1894 00b702cc gnhb
		}
1895
		if (isset($ppp['initstr']) && $type == "modem") {
1896
			$initstr = base64_decode($ppp['initstr']);
1897
			$mpdconf .= <<<EOD
1898 cb37d8fa gnhb
	set modem var \$InitString "{$initstr}"
1899
1900
EOD;
1901 00b702cc gnhb
		}
1902
		if (isset($ppp['simpin']) && $type == "modem") {
1903 cf73302f Phil Davis
			if ($ppp['pin-wait'] == "") {
1904 2a210730 smos
				$ppp['pin-wait'] = 0;
1905 cf73302f Phil Davis
			}
1906 00b702cc gnhb
			$mpdconf .= <<<EOD
1907 cb37d8fa gnhb
	set modem var \$SimPin "{$ppp['simpin']}"
1908
	set modem var \$PinWait "{$ppp['pin-wait']}"
1909
1910
EOD;
1911 00b702cc gnhb
		}
1912
		if (isset($ppp['apn']) && $type == "modem") {
1913
			$mpdconf .= <<<EOD
1914 cb37d8fa gnhb
	set modem var \$APN "{$ppp['apn']}"
1915
	set modem var \$APNum "{$ppp['apnum']}"
1916
1917
EOD;
1918 00b702cc gnhb
		}
1919 233e2af1 jim-p
		if ($type == "pppoe") {
1920
			// Send a null service name if none is set.
1921
			$provider = isset($ppp['provider']) ? $ppp['provider'] : "";
1922 00b702cc gnhb
			$mpdconf .= <<<EOD
1923 233e2af1 jim-p
	set pppoe service "{$provider}"
1924 cb37d8fa gnhb
1925
EOD;
1926 00b702cc gnhb
		}
1927 cf73302f Phil Davis
		if ($type == "pppoe") {
1928 00b702cc gnhb
			$mpdconf .= <<<EOD
1929 64d124c5 gnhb
	set pppoe iface {$port}
1930 cb37d8fa gnhb
1931
EOD;
1932 cf73302f Phil Davis
		}
1933 0661b194 gnhb
1934 39f750b5 gnhb
		if ($type == "pptp" || $type == "l2tp") {
1935 00b702cc gnhb
			$mpdconf .= <<<EOD
1936 18ec0f13 Ermal
	set {$type} self {$localips[$pid]}
1937
	set {$type} peer {$gateways[$pid]}
1938 cb37d8fa gnhb
1939
EOD;
1940 00b702cc gnhb
		}
1941 be45aa79 Renato Botelho
1942 04f357bc Ermal
		$mpdconf .= "\topen\n";
1943 cf73302f Phil Davis
	} //end foreach ($port)
1944 cb37d8fa gnhb
1945 df309b37 gnhb
1946
	/* Generate mpd.conf. If mpd_[interface].conf exists in the conf path, then link to it instead of generating a fresh conf file. */
1947 cf73302f Phil Davis
	if (file_exists("{$g['conf_path']}/mpd_{$interface}.conf")) {
1948 80d708de Renato Botelho
		@symlink("{$g['conf_path']}/mpd_{$interface}.conf", "{$g['varetc_path']}/mpd_{$interface}.conf");
1949 cf73302f Phil Davis
	} else {
1950 df309b37 gnhb
		$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1951
		if (!$fd) {
1952 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Error: cannot open mpd_%s.conf in interface_ppps_configure().%s"), $interface, "\n"));
1953 df309b37 gnhb
			return 0;
1954
		}
1955
		// Write out mpd_ppp.conf
1956
		fwrite($fd, $mpdconf);
1957
		fclose($fd);
1958 14d079b3 Ermal
		unset($mpdconf);
1959 df309b37 gnhb
	}
1960 cb37d8fa gnhb
1961
	// Create the uptime log if requested and if it doesn't exist already, or delete it if it is no longer requested.
1962
	if (isset($ppp['uptime'])) {
1963
		if (!file_exists("/conf/{$pppif}.log")) {
1964
			conf_mount_rw();
1965 873c1701 Renato Botelho
			file_put_contents("/conf/{$pppif}.log", '');
1966 cb37d8fa gnhb
			conf_mount_ro();
1967
		}
1968
	} else {
1969
		if (file_exists("/conf/{$pppif}.log")) {
1970
			conf_mount_rw();
1971 552f5a6a Renato Botelho
			@unlink("/conf/{$pppif}.log");
1972 cb37d8fa gnhb
			conf_mount_ro();
1973
		}
1974
	}
1975 92a1c8e6 Ermal
1976 7e631290 smos
	/* clean up old lock files */
1977 cf73302f Phil Davis
	foreach ($ports as $port) {
1978
		if (file_exists("{$g['var_path']}/spool/lock/LCK..{$port}")) {
1979 17d656fc smos
			unlink("{$g['var_path']}/spool/lock/LCK..{$port}");
1980 cf73302f Phil Davis
		}
1981 7e631290 smos
	}
1982
1983 3a906378 gnhb
	/* fire up mpd */
1984 873c1701 Renato Botelho
	mwexec("/usr/local/sbin/mpd5 -b -k -d {$g['varetc_path']} -f mpd_{$interface}.conf -p {$g['varrun_path']}/" .
1985
		escapeshellarg($ppp['type']) . "_{$interface}.pid -s ppp " . escapeshellarg($ppp['type']) . "client");
1986 3a906378 gnhb
1987 be45aa79 Renato Botelho
	// Check for PPPoE periodic reset request
1988 bfbb9bc0 Ermal
	if ($type == "pppoe") {
1989 cf73302f Phil Davis
		if (!empty($ppp['pppoe-reset-type'])) {
1990 70e46e62 Ermal
			interface_setup_pppoe_reset_file($ppp['if'], $interface);
1991 cf73302f Phil Davis
		} else {
1992 70e46e62 Ermal
			interface_setup_pppoe_reset_file($ppp['if']);
1993 cf73302f Phil Davis
		}
1994 cb37d8fa gnhb
	}
1995 302d646e smos
	/* wait for upto 10 seconds for the interface to appear (ppp(oe)) */
1996
	$i = 0;
1997 cf73302f Phil Davis
	while ($i < 3) {
1998 42982b22 Ermal LUÇI
		sleep(10);
1999 cf73302f Phil Davis
		if (does_interface_exist($ppp['if'], true)) {
2000 302d646e smos
			break;
2001 cf73302f Phil Davis
		}
2002 302d646e smos
		$i++;
2003
	}
2004 cb37d8fa gnhb
2005 117f8e6f smos
	/* we only support the 3gstats.php for huawei modems for now. Will add more later. */
2006
	/* We should be able to launch the right version for each modem */
2007
	/* We can also guess the mondev from the manufacturer */
2008
	exec("usbconfig | egrep -ie '(huawei)'", $usbmodemoutput);
2009
	mwexec("/bin/ps auxww|grep \"{$interface}\" |grep \"[3]gstats\" | awk '{print $2}' |xargs kill");
2010 cf73302f Phil Davis
	foreach ($ports as $port) {
2011
		if (preg_match("/huawei/i", implode("\n", $usbmodemoutput))) {
2012 6c07db48 Phil Davis
			$mondev = substr(basename($port), 0, -1);
2013 117f8e6f smos
			$devlist = glob("/dev/{$mondev}?");
2014
			$mondev = basename(end($devlist));
2015 284101d3 smos
		}
2016 cf73302f Phil Davis
		if (preg_match("/zte/i", implode("\n", $usbmodemoutput))) {
2017 6c07db48 Phil Davis
			$mondev = substr(basename($port), 0, -1) . "1";
2018 284101d3 smos
		}
2019 cf73302f Phil Davis
		if ($mondev != '') {
2020 d69414fe Chris Buechler
			log_error("Starting 3gstats.php on device '{$mondev}' for interface '{$interface}'");
2021
			mwexec_bg("/usr/local/bin/3gstats.php {$mondev} {$interface}");
2022
		}
2023 5e589685 smos
	}
2024
2025 23721285 gnhb
	return 1;
2026 cb37d8fa gnhb
}
2027
2028 8ff85c39 Ermal
function interfaces_sync_setup() {
2029 87a2efd1 Ermal Luçi
	global $g, $config;
2030 abcb2bed Ermal Lu?i
2031 84b32407 Ermal
	if (isset($config['system']['developerspew'])) {
2032 b932ef16 Scott Ullrich
		$mt = microtime();
2033 8ff85c39 Ermal
		echo "interfaces_sync_setup() being called $mt\n";
2034 b932ef16 Scott Ullrich
	}
2035 abcb2bed Ermal Lu?i
2036 285ef132 Ermal LUÇI
	if (platform_booting()) {
2037 07e40c1f Carlos Eduardo Ramos
		echo gettext("Configuring CARP settings...");
2038 7d0f4544 Scott Ullrich
		mute_kernel_msgs();
2039 a5250ebc Scott Ullrich
	}
2040 abcb2bed Ermal Lu?i
2041 b932ef16 Scott Ullrich
	/* suck in configuration items */
2042 84b32407 Ermal
	if ($config['hasync']) {
2043 f97a5b04 Darren Embry
		$pfsyncenabled = $config['hasync']['pfsyncenabled'];
2044
		$pfsyncinterface = $config['hasync']['pfsyncinterface'];
2045
		$pfsyncpeerip = $config['hasync']['pfsyncpeerip'];
2046 b932ef16 Scott Ullrich
	} else {
2047
		unset($pfsyncinterface);
2048
		unset($pfsyncenabled);
2049 6008210b Scott Ullrich
	}
2050 abcb2bed Ermal Lu?i
2051 971de1f9 Renato Botelho
	set_sysctl(array(
2052
		"net.inet.carp.preempt" => "1",
2053
		"net.inet.carp.log" => "1")
2054
	);
2055 abcb2bed Ermal Lu?i
2056 cf73302f Phil Davis
	if (!empty($pfsyncinterface)) {
2057 abcb2bed Ermal Lu?i
		$carp_sync_int = get_real_interface($pfsyncinterface);
2058 cf73302f Phil Davis
	} else {
2059 84b32407 Ermal
		unset($carp_sync_int);
2060 cf73302f Phil Davis
	}
2061 abcb2bed Ermal Lu?i
2062 b932ef16 Scott Ullrich
	/* setup pfsync interface */
2063 febe0112 Ermal
	if (isset($carp_sync_int) and isset($pfsyncenabled)) {
2064 cf73302f Phil Davis
		if (is_ipaddr($pfsyncpeerip)) {
2065 dc2e5552 Renato Botelho
			$syncpeer = "syncpeer {$pfsyncpeerip}";
2066 cf73302f Phil Davis
		} else {
2067 dc2e5552 Renato Botelho
			$syncpeer = "-syncpeer";
2068 cf73302f Phil Davis
		}
2069 dc2e5552 Renato Botelho
2070
		mwexec("/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} {$syncpeer} up", false);
2071 fd07693e Ermal LUÇI
		mwexec("/sbin/ifconfig pfsync0 -defer", false);
2072 abcb2bed Ermal Lu?i
2073 84b32407 Ermal
		sleep(1);
2074 2eb9c02f Ermal
2075 84b32407 Ermal
		/* XXX: Handle an issue with pfsync(4) and carp(4). In a cluster carp will come up before pfsync(4) has updated and so will cause issues
2076
		 * for existing sessions.
2077
		 */
2078 b32329fc smos
		log_error("waiting for pfsync...");
2079 1be1a67a PiBa-NL
		$i = 0;
2080
		while (intval(trim(`/sbin/ifconfig pfsync0 | /usr/bin/grep 'syncok: 0' | /usr/bin/grep -v grep | /usr/bin/wc -l`)) == 0 && $i < 30) {
2081
			$i++;
2082
			sleep(1);
2083
		}
2084 b32329fc smos
		log_error("pfsync done in $i seconds.");
2085
		log_error("Configuring CARP settings finalize...");
2086 b32ea59d Renato Botelho
	} else {
2087 fb6a3e7a Renato Botelho
		mwexec("/sbin/ifconfig pfsync0 -syncdev -syncpeer down", false);
2088 6930e805 Ermal
	}
2089 abcb2bed Ermal Lu?i
2090 cf73302f Phil Davis
	if ($config['virtualip']['vip']) {
2091 971de1f9 Renato Botelho
		set_single_sysctl("net.inet.carp.allow", "1");
2092 cf73302f Phil Davis
	} else {
2093 971de1f9 Renato Botelho
		set_single_sysctl("net.inet.carp.allow", "0");
2094 cf73302f Phil Davis
	}
2095 be45aa79 Renato Botelho
2096 285ef132 Ermal LUÇI
	if (platform_booting()) {
2097 abcb2bed Ermal Lu?i
		unmute_kernel_msgs();
2098 07e40c1f Carlos Eduardo Ramos
		echo gettext("done.") . "\n";
2099 abcb2bed Ermal Lu?i
	}
2100 67ee1ec5 Ermal Luçi
}
2101
2102 962fd685 Ermal
function interface_proxyarp_configure($interface = "") {
2103 9006e9f8 Scott Ullrich
	global $config, $g;
2104 cf73302f Phil Davis
	if (isset($config['system']['developerspew'])) {
2105 9006e9f8 Scott Ullrich
		$mt = microtime();
2106
		echo "interface_proxyarp_configure() being called $mt\n";
2107
	}
2108 67ee1ec5 Ermal Luçi
2109 9006e9f8 Scott Ullrich
	/* kill any running choparp */
2110 cf73302f Phil Davis
	if (empty($interface)) {
2111 962fd685 Ermal
		killbyname("choparp");
2112 cf73302f Phil Davis
	} else {
2113 7c73f504 Ermal
		$vipif = get_real_interface($interface);
2114 cf73302f Phil Davis
		if (file_exists("{$g['varrun_path']}/choparp_{$vipif}.pid")) {
2115 7c73f504 Ermal
			killbypid("{$g['varrun_path']}/choparp_{$vipif}.pid");
2116 cf73302f Phil Davis
		}
2117 7c73f504 Ermal
	}
2118 1b58b513 Scott Ullrich
2119 7c73f504 Ermal
	$paa = array();
2120
	if (!empty($config['virtualip']) && is_array($config['virtualip']['vip'])) {
2121 e5d43d93 Scott Ullrich
2122 9006e9f8 Scott Ullrich
		/* group by interface */
2123
		foreach ($config['virtualip']['vip'] as $vipent) {
2124
			if ($vipent['mode'] === "proxyarp") {
2125 cf73302f Phil Davis
				if ($vipent['interface']) {
2126 9006e9f8 Scott Ullrich
					$proxyif = $vipent['interface'];
2127 cf73302f Phil Davis
				} else {
2128 9006e9f8 Scott Ullrich
					$proxyif = "wan";
2129 cf73302f Phil Davis
				}
2130 be45aa79 Renato Botelho
2131 cf73302f Phil Davis
				if (!empty($interface) && $interface != $proxyif) {
2132 7e96ca27 Ermal
					continue;
2133 cf73302f Phil Davis
				}
2134 abcb2bed Ermal Lu?i
2135 cf73302f Phil Davis
				if (!is_array($paa[$proxyif])) {
2136 9006e9f8 Scott Ullrich
					$paa[$proxyif] = array();
2137 cf73302f Phil Davis
				}
2138 7b2d4769 Bill Marquette
2139 9006e9f8 Scott Ullrich
				$paa[$proxyif][] = $vipent;
2140
			}
2141 962fd685 Ermal
		}
2142 9006e9f8 Scott Ullrich
	}
2143 e5d43d93 Scott Ullrich
2144 962fd685 Ermal
	if (!empty($interface)) {
2145
		if (is_array($paa[$interface])) {
2146
			$paaifip = get_interface_ip($interface);
2147 cf73302f Phil Davis
			if (!is_ipaddr($paaifip)) {
2148 1c3ddd9e Renato Botelho
				return;
2149 cf73302f Phil Davis
			}
2150 1c3ddd9e Renato Botelho
			$args = get_real_interface($interface) . " auto";
2151
			foreach ($paa[$interface] as $paent) {
2152 cf73302f Phil Davis
				if (isset($paent['subnet'])) {
2153 1c3ddd9e Renato Botelho
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
2154 cf73302f Phil Davis
				} else if (isset($paent['range'])) {
2155 1c3ddd9e Renato Botelho
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" . $paent['range']['to']);
2156 cf73302f Phil Davis
				}
2157 1c3ddd9e Renato Botelho
			}
2158
			mwexec_bg("/usr/local/sbin/choparp " . $args);
2159 962fd685 Ermal
		}
2160 7c73f504 Ermal
	} else if (count($paa) > 0) {
2161 6c07db48 Phil Davis
		foreach ($paa as $paif => $paents) {
2162 9006e9f8 Scott Ullrich
			$paaifip = get_interface_ip($paif);
2163 cf73302f Phil Davis
			if (!is_ipaddr($paaifip)) {
2164 9006e9f8 Scott Ullrich
				continue;
2165 cf73302f Phil Davis
			}
2166 9006e9f8 Scott Ullrich
			$args = get_real_interface($paif) . " auto";
2167
			foreach ($paents as $paent) {
2168 cf73302f Phil Davis
				if (isset($paent['subnet'])) {
2169 9006e9f8 Scott Ullrich
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
2170 cf73302f Phil Davis
				} else if (isset($paent['range'])) {
2171 962fd685 Ermal
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" . $paent['range']['to']);
2172 cf73302f Phil Davis
				}
2173 9006e9f8 Scott Ullrich
			}
2174
			mwexec_bg("/usr/local/sbin/choparp " . $args);
2175
		}
2176
	}
2177 9f6b1429 Scott Ullrich
}
2178
2179 4454f1f3 Ermal
function interface_ipalias_cleanup($interface, $inet = "inet4") {
2180
	global $g, $config;
2181
2182
	if (is_array($config['virtualip']['vip'])) {
2183
		foreach ($config['virtualip']['vip'] as $vip) {
2184
			if ($vip['mode'] == "ipalias" && $vip['interface'] == $interface) {
2185 cf73302f Phil Davis
				if ($inet == "inet6" && is_ipaddrv6($vip['subnet'])) {
2186 4454f1f3 Ermal
					interface_vip_bring_down($vip);
2187 cf73302f Phil Davis
				} else if ($inet == "inet4" && is_ipaddrv4($vip['subnet'])) {
2188 4454f1f3 Ermal
					interface_vip_bring_down($vip);
2189 cf73302f Phil Davis
				}
2190 4454f1f3 Ermal
			}
2191
		}
2192
	}
2193
}
2194
2195 e5ac67ed Ermal Lu?i
function interfaces_vips_configure($interface = "") {
2196 87a2efd1 Ermal Luçi
	global $g, $config;
2197 cf73302f Phil Davis
	if (isset($config['system']['developerspew'])) {
2198 a04de17f Chris Buechler
		$mt = microtime();
2199 123f030c Chris Buechler
		echo "interfaces_vips_configure() being called $mt\n";
2200 a04de17f Chris Buechler
	}
2201 abcb2bed Ermal Lu?i
	$paa = array();
2202 cf73302f Phil Davis
	if (is_array($config['virtualip']['vip'])) {
2203 abcb2bed Ermal Lu?i
		$carp_setuped = false;
2204 e5ac67ed Ermal Lu?i
		$anyproxyarp = false;
2205 abcb2bed Ermal Lu?i
		foreach ($config['virtualip']['vip'] as $vip) {
2206
			switch ($vip['mode']) {
2207 cf73302f Phil Davis
				case "proxyarp":
2208
					/* nothing it is handled on interface_proxyarp_configure() */
2209
					if ($interface <> "" && $vip['interface'] <> $interface) {
2210
						continue;
2211
					}
2212
					$anyproxyarp = true;
2213
					break;
2214
				case "ipalias":
2215
					if ($interface <> "" && $vip['interface'] <> $interface) {
2216
						continue;
2217
					}
2218
					interface_ipalias_configure($vip);
2219
					break;
2220
				case "carp":
2221
					if ($interface <> "" && $vip['interface'] <> $interface) {
2222
						continue;
2223
					}
2224
					if ($carp_setuped == false) {
2225
						$carp_setuped = true;
2226
					}
2227
					interface_carp_configure($vip);
2228
					break;
2229 6a74c90e Scott Ullrich
			}
2230 a04de17f Chris Buechler
		}
2231 cf73302f Phil Davis
		if ($carp_setuped == true) {
2232 8ff85c39 Ermal
			interfaces_sync_setup();
2233 cf73302f Phil Davis
		}
2234
		if ($anyproxyarp == true) {
2235 e5ac67ed Ermal Lu?i
			interface_proxyarp_configure();
2236 cf73302f Phil Davis
		}
2237 abcb2bed Ermal Lu?i
	}
2238
}
2239
2240
function interface_ipalias_configure(&$vip) {
2241 672e28f3 Renato Botelho
	global $config;
2242
2243 cf73302f Phil Davis
	if ($vip['mode'] != 'ipalias') {
2244 672e28f3 Renato Botelho
		return;
2245 cf73302f Phil Davis
	}
2246 672e28f3 Renato Botelho
2247 b0d054ca Ermal
	if ($vip['interface'] != 'lo0' && stripos($vip['interface'], '_vip') === false) {
2248 cf73302f Phil Davis
		if (!isset($config['interfaces'][$vip['interface']])) {
2249 b0d054ca Ermal
			return;
2250 cf73302f Phil Davis
		}
2251 672e28f3 Renato Botelho
2252 cf73302f Phil Davis
		if (!isset($config['interfaces'][$vip['interface']]['enable'])) {
2253 b0d054ca Ermal
			return;
2254 cf73302f Phil Davis
		}
2255 b0d054ca Ermal
	}
2256 672e28f3 Renato Botelho
2257 b0d054ca Ermal
	$af = 'inet';
2258 cf73302f Phil Davis
	if (is_ipaddrv6($vip['subnet'])) {
2259 b0d054ca Ermal
		$af = 'inet6';
2260 cf73302f Phil Davis
	}
2261 b0d054ca Ermal
	$iface = $vip['interface'];
2262
	$vipadd = '';
2263 94115b93 Ermal
	if (strpos($vip['interface'], '_vip')) {
2264 b0d054ca Ermal
		$carpvip = get_configured_carp_interface_list($vip['interface'], $af, 'vip');
2265
		$iface = $carpvip['interface'];
2266
		$vipadd = "vhid {$carpvip['vhid']}";
2267
	}
2268 94115b93 Ermal
	$if = get_real_interface($iface);
2269 b0d054ca Ermal
	mwexec("/sbin/ifconfig " . escapeshellarg($if) ." {$af} ". escapeshellarg($vip['subnet']) ."/" . escapeshellarg($vip['subnet_bits']) . " alias {$vipadd}");
2270
	unset($iface, $af, $if, $carpvip, $vipadd);
2271 a04de17f Chris Buechler
}
2272
2273 abcb2bed Ermal Lu?i
function interface_reload_carps($cif) {
2274
	global $config;
2275
2276
	$carpifs = link_ip_to_carp_interface(find_interface_ip($cif));
2277 cf73302f Phil Davis
	if (empty($carpifs)) {
2278 abcb2bed Ermal Lu?i
		return;
2279 cf73302f Phil Davis
	}
2280 abcb2bed Ermal Lu?i
2281
	$carps = explode(" ", $carpifs);
2282 cf73302f Phil Davis
	if (is_array($config['virtualip']['vip'])) {
2283 9006e9f8 Scott Ullrich
		$viparr = &$config['virtualip']['vip'];
2284
		foreach ($viparr as $vip) {
2285 abcb2bed Ermal Lu?i
			if (in_array($vip['carpif'], $carps)) {
2286 9006e9f8 Scott Ullrich
				switch ($vip['mode']) {
2287 cf73302f Phil Davis
					case "carp":
2288
						interface_vip_bring_down($vip);
2289
						sleep(1);
2290
						interface_carp_configure($vip);
2291
						break;
2292
					case "ipalias":
2293
						interface_vip_bring_down($vip);
2294
						sleep(1);
2295
						interface_ipalias_configure($vip);
2296
						break;
2297 abcb2bed Ermal Lu?i
				}
2298 9006e9f8 Scott Ullrich
			}
2299
		}
2300
	}
2301 abcb2bed Ermal Lu?i
}
2302
2303
function interface_carp_configure(&$vip) {
2304
	global $config, $g;
2305 cf73302f Phil Davis
	if (isset($config['system']['developerspew'])) {
2306 58ebf6bb Scott Ullrich
		$mt = microtime();
2307 0a595d84 Ermal Lu?i
		echo "interface_carp_configure() being called $mt\n";
2308 58ebf6bb Scott Ullrich
	}
2309 abcb2bed Ermal Lu?i
2310 cf73302f Phil Davis
	if ($vip['mode'] != "carp") {
2311 abcb2bed Ermal Lu?i
		return;
2312 cf73302f Phil Davis
	}
2313 abcb2bed Ermal Lu?i
2314 f416763b Phil Davis
	/* NOTE: Maybe its useless nowadays */
2315 58ebf6bb Scott Ullrich
	$realif = get_real_interface($vip['interface']);
2316
	if (!does_interface_exist($realif)) {
2317 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", "");
2318 58ebf6bb Scott Ullrich
		return;
2319
	}
2320 abcb2bed Ermal Lu?i
2321 7b47bd4c Ermal
	$vip_password = $vip['password'];
2322
	$vip_password = escapeshellarg(addslashes(str_replace(" ", "", $vip_password)));
2323 cf73302f Phil Davis
	if ($vip['password'] != "") {
2324 7b47bd4c Ermal
		$password = " pass {$vip_password}";
2325 cf73302f Phil Davis
	}
2326 a687f866 Namezero
2327 100b7219 Ermal
	$advbase = "";
2328 cf73302f Phil Davis
	if (!empty($vip['advbase'])) {
2329 873c1701 Renato Botelho
		$advbase = "advbase " . escapeshellarg($vip['advbase']);
2330 cf73302f Phil Davis
	}
2331 1f74cd2d Seth Mos
2332 783f1ee2 PiBa-NL
	$carp_maintenancemode = isset($config["virtualip_carp_maintenancemode"]);
2333 cf73302f Phil Davis
	if ($carp_maintenancemode) {
2334 783f1ee2 PiBa-NL
		$advskew = "advskew 254";
2335 cf73302f Phil Davis
	} else {
2336 783f1ee2 PiBa-NL
		$advskew = "advskew " . escapeshellarg($vip['advskew']);
2337 cf73302f Phil Davis
	}
2338
2339 783f1ee2 PiBa-NL
	mwexec("/sbin/ifconfig {$realif} vhid " . escapeshellarg($vip['vhid']) . " {$advskew} {$advbase} {$password}");
2340 00a6fcb7 Ermal
2341 cf73302f Phil Davis
	if (is_ipaddrv4($vip['subnet'])) {
2342 00a6fcb7 Ermal
		mwexec("/sbin/ifconfig {$realif} " . escapeshellarg($vip['subnet']) . "/" . escapeshellarg($vip['subnet_bits']) . " alias vhid " . escapeshellarg($vip['vhid']));
2343 cf73302f Phil Davis
	} else if (is_ipaddrv6($vip['subnet'])) {
2344 80a261a2 PiBa-NL
		mwexec("/sbin/ifconfig {$realif} inet6 " . escapeshellarg($vip['subnet']) . " prefixlen " . escapeshellarg($vip['subnet_bits']) . " alias vhid " . escapeshellarg($vip['vhid']));
2345 cf73302f Phil Davis
	}
2346 abcb2bed Ermal Lu?i
2347 7238e0cf Ermal
	return $realif;
2348 abcb2bed Ermal Lu?i
}
2349
2350 854aed18 Ermal Lu?i
function interface_wireless_clone($realif, $wlcfg) {
2351 568b1358 Scott Ullrich
	global $config, $g;
2352 be45aa79 Renato Botelho
	/*   Check to see if interface has been cloned as of yet.
2353 88157f66 Scott Ullrich
	 *   If it has not been cloned then go ahead and clone it.
2354
	 */
2355 2a203afd Seth Mos
	$needs_clone = false;
2356 cf73302f Phil Davis
	if (is_array($wlcfg['wireless'])) {
2357 9f428275 Erik Fonnesbeck
		$wlcfg_mode = $wlcfg['wireless']['mode'];
2358 cf73302f Phil Davis
	} else {
2359 9f428275 Erik Fonnesbeck
		$wlcfg_mode = $wlcfg['mode'];
2360 cf73302f Phil Davis
	}
2361 086cf944 Phil Davis
	switch ($wlcfg_mode) {
2362 cf73302f Phil Davis
		case "hostap":
2363
			$mode = "wlanmode hostap";
2364
			break;
2365
		case "adhoc":
2366
			$mode = "wlanmode adhoc";
2367
			break;
2368
		default:
2369
			$mode = "";
2370
			break;
2371 2a203afd Seth Mos
	}
2372 34808d4e Erik Fonnesbeck
	$baseif = interface_get_wireless_base($wlcfg['if']);
2373 cf73302f Phil Davis
	if (does_interface_exist($realif)) {
2374 873c1701 Renato Botelho
		exec("/sbin/ifconfig " . escapeshellarg($realif), $output, $ret);
2375 2a203afd Seth Mos
		$ifconfig_str = implode($output);
2376 086cf944 Phil Davis
		if (($wlcfg_mode == "hostap") && (!preg_match("/hostap/si", $ifconfig_str))) {
2377 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Interface %s changed to hostap mode"), $realif));
2378 2a203afd Seth Mos
			$needs_clone = true;
2379
		}
2380 086cf944 Phil Davis
		if (($wlcfg_mode == "adhoc") && (!preg_match("/adhoc/si", $ifconfig_str))) {
2381 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Interface %s changed to adhoc mode"), $realif));
2382 2a203afd Seth Mos
			$needs_clone = true;
2383
		}
2384 cf73302f Phil Davis
		if (($wlcfg_mode == "bss") && (preg_match("/hostap|adhoc/si", $ifconfig_str))) {
2385 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Interface %s changed to infrastructure mode"), $realif));
2386 2a203afd Seth Mos
			$needs_clone = true;
2387
		}
2388
	} else {
2389
		$needs_clone = true;
2390 88157f66 Scott Ullrich
	}
2391 2a203afd Seth Mos
2392 cf73302f Phil Davis
	if ($needs_clone == true) {
2393 2a203afd Seth Mos
		/* remove previous instance if it exists */
2394 cf73302f Phil Davis
		if (does_interface_exist($realif)) {
2395 871768cf Ermal
			pfSense_interface_destroy($realif);
2396 cf73302f Phil Davis
		}
2397 854aed18 Ermal Lu?i
2398 07e40c1f Carlos Eduardo Ramos
		log_error(sprintf(gettext("Cloning new wireless interface %s"), $realif));
2399 b99256c1 Scott Ullrich
		// Create the new wlan interface. FreeBSD returns the new interface name.
2400
		// example:  wlan2
2401 6d54e865 Erik Fonnesbeck
		exec("/sbin/ifconfig wlan create wlandev {$baseif} {$mode} bssid 2>&1", $out, $ret);
2402 cf73302f Phil Davis
		if ($ret <> 0) {
2403 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]));
2404 9f428275 Erik Fonnesbeck
			return false;
2405 2a203afd Seth Mos
		}
2406
		$newif = trim($out[0]);
2407
		// Rename the interface to {$parentnic}_wlan{$number}#: EX: ath0_wlan0
2408 871768cf Ermal
		pfSense_interface_rename($newif, $realif);
2409 acb0bce0 Erik Fonnesbeck
		file_put_contents("{$g['tmp_path']}/{$realif}_oldmac", get_interface_mac($realif));
2410 88157f66 Scott Ullrich
	}
2411 9f428275 Erik Fonnesbeck
	return true;
2412 88157f66 Scott Ullrich
}
2413
2414 8f0289e7 Erik Fonnesbeck
function interface_sync_wireless_clones(&$ifcfg, $sync_changes = false) {
2415
	global $config, $g;
2416
2417 56626335 Erik Fonnesbeck
	$shared_settings = array('standard', 'turbo', 'protmode', 'txpower', 'channel',
2418 a8f5790a Renato Botelho
				 'diversity', 'txantenna', 'rxantenna', 'distance',
2419
				 'regdomain', 'regcountry', 'reglocation');
2420 8f0289e7 Erik Fonnesbeck
2421 cf73302f Phil Davis
	if (!is_interface_wireless($ifcfg['if'])) {
2422 7de319a1 Erik Fonnesbeck
		return;
2423 cf73302f Phil Davis
	}
2424 7de319a1 Erik Fonnesbeck
2425 34808d4e Erik Fonnesbeck
	$baseif = interface_get_wireless_base($ifcfg['if']);
2426 8f0289e7 Erik Fonnesbeck
2427 062023a5 Erik Fonnesbeck
	// Sync shared settings for assigned clones
2428 38b7d47d Erik Fonnesbeck
	$iflist = get_configured_interface_list(false, true);
2429 8f0289e7 Erik Fonnesbeck
	foreach ($iflist as $if) {
2430 34808d4e Erik Fonnesbeck
		if ($baseif == interface_get_wireless_base($config['interfaces'][$if]['if']) && $ifcfg['if'] != $config['interfaces'][$if]['if']) {
2431 8f0289e7 Erik Fonnesbeck
			if (isset($config['interfaces'][$if]['wireless']['standard']) || $sync_changes) {
2432
				foreach ($shared_settings as $setting) {
2433
					if ($sync_changes) {
2434 cf73302f Phil Davis
						if (isset($ifcfg['wireless'][$setting])) {
2435 56626335 Erik Fonnesbeck
							$config['interfaces'][$if]['wireless'][$setting] = $ifcfg['wireless'][$setting];
2436 cf73302f Phil Davis
						} else if (isset($config['interfaces'][$if]['wireless'][$setting])) {
2437 56626335 Erik Fonnesbeck
							unset($config['interfaces'][$if]['wireless'][$setting]);
2438 cf73302f Phil Davis
						}
2439 8f0289e7 Erik Fonnesbeck
					} else {
2440 cf73302f Phil Davis
						if (isset($config['interfaces'][$if]['wireless'][$setting])) {
2441 56626335 Erik Fonnesbeck
							$ifcfg['wireless'][$setting] = $config['interfaces'][$if]['wireless'][$setting];
2442 cf73302f Phil Davis
						} else if (isset($ifcfg['wireless'][$setting])) {
2443 56626335 Erik Fonnesbeck
							unset($ifcfg['wireless'][$setting]);
2444 cf73302f Phil Davis
						}
2445 8f0289e7 Erik Fonnesbeck
					}
2446
				}
2447 cf73302f Phil Davis
				if (!$sync_changes) {
2448 8f0289e7 Erik Fonnesbeck
					break;
2449 cf73302f Phil Davis
				}
2450 8f0289e7 Erik Fonnesbeck
			}
2451
		}
2452
	}
2453 263e2b7e Erik Fonnesbeck
2454 062023a5 Erik Fonnesbeck
	// Read or write settings at shared area
2455 6ef2297b Erik Fonnesbeck
	if (isset($config['wireless']['interfaces'][$baseif]) && is_array($config['wireless']['interfaces'][$baseif])) {
2456 f62c44d8 Erik Fonnesbeck
		foreach ($shared_settings as $setting) {
2457
			if ($sync_changes) {
2458 cf73302f Phil Davis
				if (isset($ifcfg['wireless'][$setting])) {
2459 56626335 Erik Fonnesbeck
					$config['wireless']['interfaces'][$baseif][$setting] = $ifcfg['wireless'][$setting];
2460 cf73302f Phil Davis
				} else if (isset($config['wireless']['interfaces'][$baseif][$setting])) {
2461 56626335 Erik Fonnesbeck
					unset($config['wireless']['interfaces'][$baseif][$setting]);
2462 cf73302f Phil Davis
				}
2463 f62c44d8 Erik Fonnesbeck
			} else if (isset($config['wireless']['interfaces'][$baseif][$setting])) {
2464 cf73302f Phil Davis
				if (isset($config['wireless']['interfaces'][$baseif][$setting])) {
2465 56626335 Erik Fonnesbeck
					$ifcfg['wireless'][$setting] = $config['wireless']['interfaces'][$baseif][$setting];
2466 cf73302f Phil Davis
				} else if (isset($ifcfg['wireless'][$setting])) {
2467 56626335 Erik Fonnesbeck
					unset($ifcfg['wireless'][$setting]);
2468 cf73302f Phil Davis
				}
2469 f62c44d8 Erik Fonnesbeck
			}
2470 062023a5 Erik Fonnesbeck
		}
2471
	}
2472
2473
	// Sync the mode on the clone creation page with the configured mode on the interface
2474 6ef2297b Erik Fonnesbeck
	if (interface_is_wireless_clone($ifcfg['if']) && isset($config['wireless']['clone']) && is_array($config['wireless']['clone'])) {
2475 263e2b7e Erik Fonnesbeck
		foreach ($config['wireless']['clone'] as &$clone) {
2476
			if ($clone['cloneif'] == $ifcfg['if']) {
2477
				if ($sync_changes) {
2478
					$clone['mode'] = $ifcfg['wireless']['mode'];
2479
				} else {
2480
					$ifcfg['wireless']['mode'] = $clone['mode'];
2481
				}
2482
				break;
2483
			}
2484
		}
2485 867d444b Erik Fonnesbeck
		unset($clone);
2486 263e2b7e Erik Fonnesbeck
	}
2487 8f0289e7 Erik Fonnesbeck
}
2488
2489 19e83210 Scott Ullrich
function interface_wireless_configure($if, &$wl, &$wlcfg) {
2490 ac3f8318 Espen Johansen
	global $config, $g;
2491 eb772abd Scott Ullrich
2492 4742e927 Scott Ullrich
	/*    open up a shell script that will be used to output the commands.
2493
	 *    since wireless is changing a lot, these series of commands are fragile
2494 905ea336 Phil Davis
	 *    and will sometimes need to be verified by a operator by executing the command
2495
	 *    and returning the output of the command to the developers for inspection.  please
2496
	 *    do not change this routine from a shell script to individual exec commands.  -sullrich
2497 4742e927 Scott Ullrich
	 */
2498 eb772abd Scott Ullrich
2499 b99256c1 Scott Ullrich
	// Remove script file
2500 490b8b2a Scott Ullrich
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
2501 eb772abd Scott Ullrich
2502 0a28d385 Erik Fonnesbeck
	// Clone wireless nic if needed.
2503
	interface_wireless_clone($if, $wl);
2504
2505 8f0289e7 Erik Fonnesbeck
	// Reject inadvertent changes to shared settings in case the interface hasn't been configured.
2506
	interface_sync_wireless_clones($wl, false);
2507
2508 086cf944 Phil Davis
	$fd_set = fopen("{$g['tmp_path']}/{$if}_setup.sh", "w");
2509 4742e927 Scott Ullrich
	fwrite($fd_set, "#!/bin/sh\n");
2510 36d0358b Scott Ullrich
	fwrite($fd_set, "# {$g['product_name']} wireless configuration script.\n\n");
2511 086cf944 Phil Davis
2512
	$wlan_setup_log = fopen("{$g['tmp_path']}/{$if}_setup.log", "w");
2513 eb772abd Scott Ullrich
2514 2ac908dd Espen Johansen
	/* set values for /path/program */
2515
	$hostapd = "/usr/sbin/hostapd";
2516
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
2517 4742e927 Scott Ullrich
	$ifconfig = "/sbin/ifconfig";
2518 56626335 Erik Fonnesbeck
	$sysctl = "/sbin/sysctl";
2519 4742e927 Scott Ullrich
	$killall = "/usr/bin/killall";
2520 2ac908dd Espen Johansen
2521 905ea336 Phil Davis
	/* Set all wireless ifconfig variables (split up to get rid of needed checking) */
2522 5508cf57 Scott Ullrich
2523 2a203afd Seth Mos
	$wlcmd = array();
2524 56626335 Erik Fonnesbeck
	$wl_sysctl = array();
2525 2a203afd Seth Mos
	/* Make sure it's up */
2526
	$wlcmd[] = "up";
2527 ac3f8318 Espen Johansen
	/* Set a/b/g standard */
2528 9be20928 Erik Fonnesbeck
	$standard = str_replace(" Turbo", "", $wlcfg['standard']);
2529 d325e908 Chris Buechler
	/* skip mode entirely for "auto" */
2530
	if ($wlcfg['standard'] != "auto") {
2531
		$wlcmd[] = "mode " . escapeshellarg($standard);
2532
	}
2533 2a203afd Seth Mos
2534 5030b5eb Erik Fonnesbeck
	/* XXX: Disable ampdu for now on mwl when running in 11n mode
2535
	 * to prevent massive packet loss under certain conditions. */
2536 cf73302f Phil Davis
	if (preg_match("/^mwl/i", $if) && ($standard == "11ng" || $standard == "11na")) {
2537 5030b5eb Erik Fonnesbeck
		$wlcmd[] = "-ampdu";
2538 cf73302f Phil Davis
	}
2539 5030b5eb Erik Fonnesbeck
2540 2a203afd Seth Mos
	/* Set ssid */
2541 cf73302f Phil Davis
	if ($wlcfg['ssid']) {
2542 2a203afd Seth Mos
		$wlcmd[] = "ssid " .escapeshellarg($wlcfg['ssid']);
2543 cf73302f Phil Davis
	}
2544 5508cf57 Scott Ullrich
2545 0856c4ac Scott Ullrich
	/* Set 802.11g protection mode */
2546 2a203afd Seth Mos
	$wlcmd[] = "protmode " . escapeshellarg($wlcfg['protmode']);
2547 0856c4ac Scott Ullrich
2548 ac3f8318 Espen Johansen
	/* set wireless channel value */
2549 cf73302f Phil Davis
	if (isset($wlcfg['channel'])) {
2550
		if ($wlcfg['channel'] == "0") {
2551 2a203afd Seth Mos
			$wlcmd[] = "channel any";
2552
		} else {
2553
			$wlcmd[] = "channel " . escapeshellarg($wlcfg['channel']);
2554
		}
2555
	}
2556 2ac908dd Espen Johansen
2557 56626335 Erik Fonnesbeck
	/* Set antenna diversity value */
2558 cf73302f Phil Davis
	if (isset($wlcfg['diversity'])) {
2559 56626335 Erik Fonnesbeck
		$wl_sysctl[] = "diversity=" . escapeshellarg($wlcfg['diversity']);
2560 cf73302f Phil Davis
	}
2561 56626335 Erik Fonnesbeck
2562
	/* Set txantenna value */
2563 cf73302f Phil Davis
	if (isset($wlcfg['txantenna'])) {
2564 56626335 Erik Fonnesbeck
		$wl_sysctl[] = "txantenna=" . escapeshellarg($wlcfg['txantenna']);
2565 cf73302f Phil Davis
	}
2566 56626335 Erik Fonnesbeck
2567
	/* Set rxantenna value */
2568 cf73302f Phil Davis
	if (isset($wlcfg['rxantenna'])) {
2569 56626335 Erik Fonnesbeck
		$wl_sysctl[] = "rxantenna=" . escapeshellarg($wlcfg['rxantenna']);
2570 cf73302f Phil Davis
	}
2571 56626335 Erik Fonnesbeck
2572 f134033e Scott Ullrich
	/* set Distance value */
2573 cf73302f Phil Davis
	if ($wlcfg['distance']) {
2574 f134033e Scott Ullrich
		$distance = escapeshellarg($wlcfg['distance']);
2575 cf73302f Phil Davis
	}
2576 f134033e Scott Ullrich
2577 ac3f8318 Espen Johansen
	/* Set wireless hostap mode */
2578 2a203afd Seth Mos
	if ($wlcfg['mode'] == "hostap") {
2579
		$wlcmd[] = "mediaopt hostap";
2580
	} else {
2581
		$wlcmd[] = "-mediaopt hostap";
2582
	}
2583 ac3f8318 Espen Johansen
2584
	/* Set wireless adhoc mode */
2585 2a203afd Seth Mos
	if ($wlcfg['mode'] == "adhoc") {
2586
		$wlcmd[] = "mediaopt adhoc";
2587
	} else {
2588
		$wlcmd[] = "-mediaopt adhoc";
2589
	}
2590 ac3f8318 Espen Johansen
2591 f416763b Phil Davis
	/* Not necessary to set BSS mode as this is default if adhoc and/or hostap is NOT set */
2592 ac3f8318 Espen Johansen
2593
	/* handle hide ssid option */
2594 cf73302f Phil Davis
	if (isset($wlcfg['hidessid']['enable'])) {
2595 2a203afd Seth Mos
		$wlcmd[] = "hidessid";
2596
	} else {
2597
		$wlcmd[] = "-hidessid";
2598
	}
2599 ac3f8318 Espen Johansen
2600
	/* handle pureg (802.11g) only option */
2601 cf73302f Phil Davis
	if (isset($wlcfg['pureg']['enable'])) {
2602 2a203afd Seth Mos
		$wlcmd[] = "mode 11g pureg";
2603
	} else {
2604
		$wlcmd[] = "-pureg";
2605
	}
2606 ac3f8318 Espen Johansen
2607 ed459692 Erik Fonnesbeck
	/* handle puren (802.11n) only option */
2608 cf73302f Phil Davis
	if (isset($wlcfg['puren']['enable'])) {
2609 ed459692 Erik Fonnesbeck
		$wlcmd[] = "puren";
2610
	} else {
2611
		$wlcmd[] = "-puren";
2612
	}
2613
2614 ac3f8318 Espen Johansen
	/* enable apbridge option */
2615 cf73302f Phil Davis
	if (isset($wlcfg['apbridge']['enable'])) {
2616 2a203afd Seth Mos
		$wlcmd[] = "apbridge";
2617
	} else {
2618
		$wlcmd[] = "-apbridge";
2619
	}
2620 ac3f8318 Espen Johansen
2621
	/* handle turbo option */
2622 cf73302f Phil Davis
	if (isset($wlcfg['turbo']['enable'])) {
2623 2a203afd Seth Mos
		$wlcmd[] = "mediaopt turbo";
2624
	} else {
2625
		$wlcmd[] = "-mediaopt turbo";
2626
	}
2627 ac3f8318 Espen Johansen
2628
	/* handle txpower setting */
2629 f2f34088 Chris Buechler
	// or don't. this has issues at the moment.
2630
	/*
2631 086cf944 Phil Davis
	if ($wlcfg['txpower'] <> "" && is_numeric($wlcfg['txpower'])) {
2632 2a203afd Seth Mos
		$wlcmd[] = "txpower " . escapeshellarg($wlcfg['txpower']);
2633 f2f34088 Chris Buechler
	}*/
2634 086cf944 Phil Davis
2635 ac3f8318 Espen Johansen
	/* handle wme option */
2636 cf73302f Phil Davis
	if (isset($wlcfg['wme']['enable'])) {
2637 2a203afd Seth Mos
		$wlcmd[] = "wme";
2638
	} else {
2639
		$wlcmd[] = "-wme";
2640
	}
2641 eb772abd Scott Ullrich
2642 ac3f8318 Espen Johansen
	/* set up wep if enabled */
2643 2a203afd Seth Mos
	$wepset = "";
2644
	if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
2645 086cf944 Phil Davis
		switch ($wlcfg['wpa']['auth_algs']) {
2646 2a203afd Seth Mos
			case "1":
2647
				$wepset .= "authmode open wepmode on ";
2648
				break;
2649
			case "2":
2650
				$wepset .= "authmode shared wepmode on ";
2651
				break;
2652
			case "3":
2653
				$wepset .= "authmode mixed wepmode on ";
2654
		}
2655 2f19fa14 Scott Ullrich
		$i = 1;
2656
		foreach ($wlcfg['wep']['key'] as $wepkey) {
2657
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
2658 2a203afd Seth Mos
			if (isset($wepkey['txkey'])) {
2659
				$wlcmd[] = "weptxkey {$i} ";
2660
			}
2661 2f19fa14 Scott Ullrich
			$i++;
2662
		}
2663 2a203afd Seth Mos
		$wlcmd[] = $wepset;
2664 ac0c4355 Chris Buechler
	} else if (isset($wlcfg['wpa']['enable'])) {
2665
		$wlcmd[] = "authmode wpa wepmode off ";
2666 2a203afd Seth Mos
	} else {
2667
		$wlcmd[] = "authmode open wepmode off ";
2668 ac3f8318 Espen Johansen
	}
2669
2670 97f3ce0f Phil Davis
	kill_hostapd($if);
2671 c8178bb7 Erik Fonnesbeck
	mwexec(kill_wpasupplicant("{$if}"));
2672
2673 ac3f8318 Espen Johansen
	/* generate wpa_supplicant/hostap config if wpa is enabled */
2674 2a203afd Seth Mos
	conf_mount_rw();
2675 ac3f8318 Espen Johansen
2676
	switch ($wlcfg['mode']) {
2677 cf73302f Phil Davis
		case 'bss':
2678
			if (isset($wlcfg['wpa']['enable'])) {
2679
				$wpa .= <<<EOD
2680 454756b9 Scott Ullrich
ctrl_interface={$g['varrun_path']}/wpa_supplicant
2681 50ad3b7c Scott Ullrich
ctrl_interface_group=0
2682
ap_scan=1
2683 2ac908dd Espen Johansen
#fast_reauth=1
2684 249558a2 Scott Ullrich
network={
2685 454756b9 Scott Ullrich
ssid="{$wlcfg['ssid']}"
2686
scan_ssid=1
2687 2ac908dd Espen Johansen
priority=5
2688
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
2689 454756b9 Scott Ullrich
psk="{$wlcfg['wpa']['passphrase']}"
2690 2ac908dd Espen Johansen
pairwise={$wlcfg['wpa']['wpa_pairwise']}
2691
group={$wlcfg['wpa']['wpa_pairwise']}
2692 50ad3b7c Scott Ullrich
}
2693
EOD;
2694
2695 cf73302f Phil Davis
				@file_put_contents("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", $wpa);
2696
				unset($wpa);
2697
			}
2698
			break;
2699
		case 'hostap':
2700
			if (!empty($wlcfg['wpa']['passphrase'])) {
2701
				$wpa_passphrase = "wpa_passphrase={$wlcfg['wpa']['passphrase']}\n";
2702
			} else {
2703
				$wpa_passphrase = "";
2704
			}
2705
			if (isset($wlcfg['wpa']['enable'])) {
2706
				$wpa .= <<<EOD
2707 459d6351 Scott Ullrich
interface={$if}
2708
driver=bsd
2709
logger_syslog=-1
2710
logger_syslog_level=0
2711
logger_stdout=-1
2712
logger_stdout_level=0
2713 2ac908dd Espen Johansen
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
2714
ctrl_interface={$g['varrun_path']}/hostapd
2715 459d6351 Scott Ullrich
ctrl_interface_group=wheel
2716 2ac908dd Espen Johansen
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
2717
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
2718 b67d192d Scott Ullrich
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
2719 459d6351 Scott Ullrich
ssid={$wlcfg['ssid']}
2720 2ac908dd Espen Johansen
debug={$wlcfg['wpa']['debug_mode']}
2721
auth_algs={$wlcfg['wpa']['auth_algs']}
2722
wpa={$wlcfg['wpa']['wpa_mode']}
2723
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
2724
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
2725 ac3f8318 Espen Johansen
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
2726
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
2727
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
2728 7eadaa9c Scott Ullrich
{$wpa_passphrase}
2729 525d565b Scott Ullrich
2730 459d6351 Scott Ullrich
EOD;
2731 2ac908dd Espen Johansen
2732 cf73302f Phil Davis
				if (isset($wlcfg['wpa']['rsn_preauth'])) {
2733
					$wpa .= <<<EOD
2734 c9e7d30d Scott Ullrich
# Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
2735
rsn_preauth=1
2736
rsn_preauth_interfaces={$if}
2737
2738
EOD;
2739 cf73302f Phil Davis
				}
2740
				if (is_array($wlcfg['wpa']['ieee8021x']) && isset($wlcfg['wpa']['ieee8021x']['enable'])) {
2741
					$wpa .= "ieee8021x=1\n";
2742 c9e7d30d Scott Ullrich
2743 cf73302f Phil Davis
					if (!empty($wlcfg['auth_server_addr']) && !empty($wlcfg['auth_server_shared_secret'])) {
2744
						$auth_server_port = "1812";
2745
						if (!empty($wlcfg['auth_server_port']) && is_numeric($wlcfg['auth_server_port'])) {
2746
							$auth_server_port = intval($wlcfg['auth_server_port']);
2747
						}
2748
						$wpa .= <<<EOD
2749 525d565b Scott Ullrich
2750 5949124c Scott Ullrich
auth_server_addr={$wlcfg['auth_server_addr']}
2751
auth_server_port={$auth_server_port}
2752
auth_server_shared_secret={$wlcfg['auth_server_shared_secret']}
2753 315bdf65 Ermal
2754
EOD;
2755 cf73302f Phil Davis
						if (!empty($wlcfg['auth_server_addr2']) && !empty($wlcfg['auth_server_shared_secret2'])) {
2756
							$auth_server_port2 = "1812";
2757
							if (!empty($wlcfg['auth_server_port2']) && is_numeric($wlcfg['auth_server_port2'])) {
2758
								$auth_server_port2 = intval($wlcfg['auth_server_port2']);
2759
							}
2760 315bdf65 Ermal
2761 cf73302f Phil Davis
							$wpa .= <<<EOD
2762 a687f866 Namezero
auth_server_addr={$wlcfg['auth_server_addr2']}
2763
auth_server_port={$auth_server_port2}
2764
auth_server_shared_secret={$wlcfg['auth_server_shared_secret2']}
2765 525d565b Scott Ullrich
2766 459d6351 Scott Ullrich
EOD;
2767 cf73302f Phil Davis
						}
2768 315bdf65 Ermal
					}
2769 5949124c Scott Ullrich
				}
2770 315bdf65 Ermal
2771 cf73302f Phil Davis
				@file_put_contents("{$g['varetc_path']}/hostapd_{$if}.conf", $wpa);
2772
				unset($wpa);
2773
			}
2774
			break;
2775 eb772abd Scott Ullrich
	}
2776 ac3f8318 Espen Johansen
2777 4742e927 Scott Ullrich
	/*
2778
	 *    all variables are set, lets start up everything
2779 2a203afd Seth Mos
	 */
2780 eb772abd Scott Ullrich
2781 bbfc810e Erik Fonnesbeck
	$baseif = interface_get_wireless_base($if);
2782 56626335 Erik Fonnesbeck
	preg_match("/^(.*?)([0-9]*)$/", $baseif, $baseif_split);
2783
	$wl_sysctl_prefix = 'dev.' . $baseif_split[1] . '.' . $baseif_split[2];
2784
2785
	/* set sysctls for the wireless interface */
2786
	if (!empty($wl_sysctl)) {
2787
		fwrite($fd_set, "# sysctls for {$baseif}\n");
2788
		foreach ($wl_sysctl as $wl_sysctl_line) {
2789
			fwrite($fd_set, "{$sysctl} {$wl_sysctl_prefix}.{$wl_sysctl_line}\n");
2790
		}
2791
	}
2792 bbfc810e Erik Fonnesbeck
2793 78922914 Scott Ullrich
	/* set ack timers according to users preference (if he/she has any) */
2794 cf73302f Phil Davis
	if ($distance) {
2795 4742e927 Scott Ullrich
		fwrite($fd_set, "# Enable ATH distance settings\n");
2796 e327021d Erik Fonnesbeck
		fwrite($fd_set, "/sbin/athctrl.sh -i {$baseif} -d {$distance}\n");
2797 78922914 Scott Ullrich
	}
2798 eb772abd Scott Ullrich
2799 ac3f8318 Espen Johansen
	if (isset($wlcfg['wpa']['enable'])) {
2800 2a203afd Seth Mos
		if ($wlcfg['mode'] == "bss") {
2801 4742e927 Scott Ullrich
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
2802 2a203afd Seth Mos
		}
2803
		if ($wlcfg['mode'] == "hostap") {
2804 864bf774 Erik Fonnesbeck
			/* add line to script to restore old mac to make hostapd happy */
2805 acb0bce0 Erik Fonnesbeck
			if (file_exists("{$g['tmp_path']}/{$if}_oldmac")) {
2806
				$if_oldmac = file_get_contents("{$g['tmp_path']}/{$if}_oldmac");
2807 cf73302f Phil Davis
				if (is_macaddr($if_oldmac)) {
2808 acb0bce0 Erik Fonnesbeck
					fwrite($fd_set, "{$ifconfig} " . escapeshellarg($if) .
2809
						" link " . escapeshellarg($if_oldmac) . "\n");
2810 cf73302f Phil Davis
				}
2811 acb0bce0 Erik Fonnesbeck
			}
2812
2813 97f3ce0f Phil Davis
			fwrite($fd_set, "{$hostapd} -B -P {$g['varrun_path']}/hostapd_{$if}.pid {$g['varetc_path']}/hostapd_{$if}.conf\n");
2814 864bf774 Erik Fonnesbeck
2815
			/* add line to script to restore spoofed mac after running hostapd */
2816
			if (file_exists("{$g['tmp_path']}/{$if}_oldmac")) {
2817 cf73302f Phil Davis
				if ($wl['spoofmac']) {
2818 864bf774 Erik Fonnesbeck
					$if_curmac = $wl['spoofmac'];
2819 cf73302f Phil Davis
				} else {
2820 864bf774 Erik Fonnesbeck
					$if_curmac = get_interface_mac($if);
2821 cf73302f Phil Davis
				}
2822
				if (is_macaddr($if_curmac)) {
2823 864bf774 Erik Fonnesbeck
					fwrite($fd_set, "{$ifconfig} " . escapeshellarg($if) .
2824
						" link " . escapeshellarg($if_curmac) . "\n");
2825 cf73302f Phil Davis
				}
2826 864bf774 Erik Fonnesbeck
			}
2827 2a203afd Seth Mos
		}
2828 ac3f8318 Espen Johansen
	}
2829 191a8175 Scott Ullrich
2830 4742e927 Scott Ullrich
	fclose($fd_set);
2831 8a958125 Scott Ullrich
	conf_mount_ro();
2832
2833 bbfc810e Erik Fonnesbeck
	/* Making sure regulatory settings have actually changed
2834
	 * before applying, because changing them requires bringing
2835
	 * down all wireless networks on the interface. */
2836
	exec("{$ifconfig} " . escapeshellarg($if), $output);
2837
	$ifconfig_str = implode($output);
2838
	unset($output);
2839
	$reg_changing = false;
2840
2841 89e7778f Erik Fonnesbeck
	/* special case for the debug country code */
2842 cf73302f Phil Davis
	if ($wlcfg['regcountry'] == 'DEBUG' && !preg_match("/\sregdomain\s+DEBUG\s/si", $ifconfig_str)) {
2843 89e7778f Erik Fonnesbeck
		$reg_changing = true;
2844 cf73302f Phil Davis
	} else if ($wlcfg['regdomain'] && !preg_match("/\sregdomain\s+{$wlcfg['regdomain']}\s/si", $ifconfig_str)) {
2845 bbfc810e Erik Fonnesbeck
		$reg_changing = true;
2846 cf73302f Phil Davis
	} else if ($wlcfg['regcountry'] && !preg_match("/\scountry\s+{$wlcfg['regcountry']}\s/si", $ifconfig_str)) {
2847 bbfc810e Erik Fonnesbeck
		$reg_changing = true;
2848 cf73302f Phil Davis
	} else if ($wlcfg['reglocation'] == 'anywhere' && preg_match("/\s(indoor|outdoor)\s/si", $ifconfig_str)) {
2849 89e7778f Erik Fonnesbeck
		$reg_changing = true;
2850 cf73302f Phil Davis
	} else if ($wlcfg['reglocation'] && $wlcfg['reglocation'] != 'anywhere' && !preg_match("/\s{$wlcfg['reglocation']}\s/si", $ifconfig_str)) {
2851 bbfc810e Erik Fonnesbeck
		$reg_changing = true;
2852 cf73302f Phil Davis
	}
2853 bbfc810e Erik Fonnesbeck
2854
	if ($reg_changing) {
2855
		/* set regulatory domain */
2856 cf73302f Phil Davis
		if ($wlcfg['regdomain']) {
2857 bbfc810e Erik Fonnesbeck
			$wlregcmd[] = "regdomain " . escapeshellarg($wlcfg['regdomain']);
2858 cf73302f Phil Davis
		}
2859 bbfc810e Erik Fonnesbeck
2860
		/* set country */
2861 cf73302f Phil Davis
		if ($wlcfg['regcountry']) {
2862 bbfc810e Erik Fonnesbeck
			$wlregcmd[] = "country " . escapeshellarg($wlcfg['regcountry']);
2863 cf73302f Phil Davis
		}
2864 bbfc810e Erik Fonnesbeck
2865
		/* set location */
2866 cf73302f Phil Davis
		if ($wlcfg['reglocation']) {
2867 bbfc810e Erik Fonnesbeck
			$wlregcmd[] = escapeshellarg($wlcfg['reglocation']);
2868 cf73302f Phil Davis
		}
2869 bbfc810e Erik Fonnesbeck
2870
		$wlregcmd_args = implode(" ", $wlregcmd);
2871
2872
		/* build a complete list of the wireless clones for this interface */
2873
		$clone_list = array();
2874 cf73302f Phil Davis
		if (does_interface_exist(interface_get_wireless_clone($baseif))) {
2875 bbfc810e Erik Fonnesbeck
			$clone_list[] = interface_get_wireless_clone($baseif);
2876 cf73302f Phil Davis
		}
2877 6ef2297b Erik Fonnesbeck
		if (isset($config['wireless']['clone']) && is_array($config['wireless']['clone'])) {
2878 bbfc810e Erik Fonnesbeck
			foreach ($config['wireless']['clone'] as $clone) {
2879 cf73302f Phil Davis
				if ($clone['if'] == $baseif) {
2880 bbfc810e Erik Fonnesbeck
					$clone_list[] = $clone['cloneif'];
2881 cf73302f Phil Davis
				}
2882 bbfc810e Erik Fonnesbeck
			}
2883
		}
2884
2885
		/* find which clones are up and bring them down */
2886
		$clones_up = array();
2887
		foreach ($clone_list as $clone_if) {
2888 1cf76394 Erik Fonnesbeck
			$clone_status = pfSense_get_interface_addresses($clone_if);
2889 bbfc810e Erik Fonnesbeck
			if ($clone_status['status'] == 'up') {
2890
				$clones_up[] = $clone_if;
2891
				mwexec("{$ifconfig} " . escapeshellarg($clone_if) . " down");
2892
			}
2893
		}
2894
2895
		/* apply the regulatory settings */
2896
		mwexec("{$ifconfig} " . escapeshellarg($if) . " {$wlregcmd_args}");
2897 95d2d24b Chris Buechler
		fwrite($wlan_setup_log, "$ifconfig" . escapeshellarg($if) . "$wlregcmd_args \n");
2898 bbfc810e Erik Fonnesbeck
2899
		/* bring the clones back up that were previously up */
2900
		foreach ($clones_up as $clone_if) {
2901 5e0a3256 Ermal LUÇI
			interfaces_bring_up($clone_if);
2902 67e77adf Erik Fonnesbeck
2903
			/*
2904
			 * Rerun the setup script for the interface if it isn't this interface, the interface
2905
			 * is in infrastructure mode, and WPA is enabled.
2906
			 * This can be removed if wpa_supplicant stops dying when you bring the interface down.
2907
			 */
2908
			if ($clone_if != $if) {
2909
				$friendly_if = convert_real_interface_to_friendly_interface_name($clone_if);
2910 cf73302f Phil Davis
				if ((!empty($friendly_if)) &&
2911
				    ($config['interfaces'][$friendly_if]['wireless']['mode'] == "bss") &&
2912
				    (isset($config['interfaces'][$friendly_if]['wireless']['wpa']['enable']))) {
2913 873c1701 Renato Botelho
					mwexec("/bin/sh {$g['tmp_path']}/" . escapeshellarg($clone_if) . "_setup.sh");
2914 67e77adf Erik Fonnesbeck
				}
2915
			}
2916 bbfc810e Erik Fonnesbeck
		}
2917
	}
2918
2919 086cf944 Phil Davis
	/* 20150318 cmb - Note: the below no longer appears to be true on FreeBSD 10.x, so don't set
2920 d325e908 Chris Buechler
	 * mode twice (for now at least). This can be removed entirely in the future if no problems are found
2921 086cf944 Phil Davis
2922 d325e908 Chris Buechler
	 * The mode must be specified in a separate command before ifconfig
2923 23fdc06e Erik Fonnesbeck
	 * will allow the mode and channel at the same time in the next. */
2924 d325e908 Chris Buechler
	//mwexec("/sbin/ifconfig " . escapeshellarg($if) . " mode " . escapeshellarg($standard));
2925
	//fwrite($wlan_setup_log, "/sbin/ifconfig " . escapeshellarg($if) . " mode " . escapeshellarg($standard) . "\n");
2926 23fdc06e Erik Fonnesbeck
2927 2a48a885 Erik Fonnesbeck
	/* configure wireless */
2928
	$wlcmd_args = implode(" ", $wlcmd);
2929 873c1701 Renato Botelho
	mwexec("/sbin/ifconfig " . escapeshellarg($if) . " " . $wlcmd_args, false);
2930 95d2d24b Chris Buechler
	fwrite($wlan_setup_log, "/sbin/ifconfig " . escapeshellarg($if) . " " . "$wlcmd_args \n");
2931
	fclose($wlan_setup_log);
2932 086cf944 Phil Davis
2933 315bdf65 Ermal
	unset($wlcmd_args, $wlcmd);
2934 2a48a885 Erik Fonnesbeck
2935 be45aa79 Renato Botelho
2936 2a203afd Seth Mos
	sleep(1);
2937
	/* execute hostapd and wpa_supplicant if required in shell */
2938 873c1701 Renato Botelho
	mwexec("/bin/sh {$g['tmp_path']}/" . escapeshellarg($if) . "_setup.sh");
2939 191a8175 Scott Ullrich
2940 ac3f8318 Espen Johansen
	return 0;
2941 cfc707f7 Scott Ullrich
2942 5b237745 Scott Ullrich
}
2943
2944 eba938e3 Scott Ullrich
function kill_hostapd($interface) {
2945 97f3ce0f Phil Davis
	global $g;
2946
2947 cf73302f Phil Davis
	if (isvalidpid("{$g['varrun_path']}/hostapd_{$interface}.pid")) {
2948 97f3ce0f Phil Davis
		return killbypid("{$g['varrun_path']}/hostapd_{$interface}.pid");
2949 cf73302f Phil Davis
	}
2950 4b2a6180 Scott Ullrich
}
2951
2952 eba938e3 Scott Ullrich
function kill_wpasupplicant($interface) {
2953 31b958d5 Erik Fonnesbeck
	return "/bin/pkill -f \"wpa_supplicant .*{$interface}\\.conf\"\n";
2954 4b2a6180 Scott Ullrich
}
2955
2956 eba938e3 Scott Ullrich
function find_dhclient_process($interface) {
2957 cf73302f Phil Davis
	if ($interface) {
2958 05c4bfa0 Ermal
		$pid = `/bin/pgrep -axf "dhclient: {$interface}"`;
2959 cf73302f Phil Davis
	} else {
2960 319cbd5e Ermal
		$pid = 0;
2961 cf73302f Phil Davis
	}
2962 319cbd5e Ermal
2963 bcfe4ae5 Ermal
	return intval($pid);
2964 0311dbd5 Scott Ullrich
}
2965
2966 d9d1bd20 Renato Botelho
function kill_dhclient_process($interface) {
2967 cf73302f Phil Davis
	if (empty($interface) || !does_interface_exist($interface)) {
2968 d9d1bd20 Renato Botelho
		return;
2969 cf73302f Phil Davis
	}
2970 d9d1bd20 Renato Botelho
2971
	$i = 0;
2972
	while ((($pid = find_dhclient_process($interface)) != 0) && ($i < 3)) {
2973
		/* 3rd time make it die for sure */
2974
		$sig = ($i == 2 ? SIGKILL : SIGTERM);
2975
		posix_kill($pid, $sig);
2976
		sleep(1);
2977
		$i++;
2978
	}
2979
	unset($i);
2980
}
2981
2982 c495f88b Seth Mos
function find_dhcp6c_process($interface) {
2983 b0059636 Ermal
	global $g;
2984
2985 cf73302f Phil Davis
	if ($interface && isvalidpid("{$g['varrun_path']}/dhcp6c_{$interface}.pid")) {
2986 4e6667b2 Renato Botelho
		$pid = trim(file_get_contents("{$g['varrun_path']}/dhcp6c_{$interface}.pid"), " \n");
2987 cf73302f Phil Davis
	} else {
2988 74fa57aa smos
		return(false);
2989 cf73302f Phil Davis
	}
2990 c495f88b Seth Mos
2991
	return intval($pid);
2992
}
2993
2994 00e8315b Ermal
function interface_virtual_create($interface) {
2995
	global $config;
2996
2997
	if (strstr($interface, "_vlan")) {
2998
		interfaces_vlan_configure($vlan);
2999
	} else if (substr($interface, 0, 3) == "gre") {
3000
		interfaces_gre_configure(0, $interface);
3001
	} else if (substr($interface, 0, 3) == "gif") {
3002
		interfaces_gif_configure(0, $interface);
3003
	} else if (substr($interface, 0, 5) == "ovpns") {
3004
		if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-server'])) {
3005
			foreach ($config['openvpn']['openvpn-server'] as $server) {
3006
				if ($interface == "ovpns{$server['vpnid']}") {
3007 cf73302f Phil Davis
					if (!function_exists('openvpn_resync')) {
3008 00e8315b Ermal
						require_once('openvpn.inc');
3009 cf73302f Phil Davis
					}
3010 00e8315b Ermal
					log_error("OpenVPN: Resync server {$server['description']}");
3011
					openvpn_resync('server', $server);
3012
				}
3013
			}
3014
			unset($server);
3015
		}
3016
	} else if (substr($interface, 0, 5) == "ovpnc") {
3017
		if (is_array($config['openvpn']) && is_array($config['openvpn']['openvpn-client'])) {
3018
			foreach ($config['openvpn']['openvpn-client'] as $client) {
3019
				if ($interface == "ovpnc{$client['vpnid']}") {
3020 cf73302f Phil Davis
					if (!function_exists('openvpn_resync')) {
3021 00e8315b Ermal
						require_once('openvpn.inc');
3022 cf73302f Phil Davis
					}
3023 00e8315b Ermal
					log_error("OpenVPN: Resync server {$client['description']}");
3024
					openvpn_resync('client', $client);
3025
				}
3026
			}
3027
			unset($client);
3028
		}
3029
	} else if (substr($interface, 0, 4) == "lagg") {
3030
		interfaces_lagg_configure($interface);
3031
	} else if (substr($interface, 0, 6) == "bridge") {
3032
		interfaces_bridge_configure(0, $interface);
3033
	}
3034
}
3035
3036 7d5fc0b3 Ermal LUÇI
function interface_vlan_mtu_configured($realhwif, $mtu) {
3037
	global $config;
3038
3039
	if (is_array($config['vlans']) && is_array($config['vlans']['vlan'])) {
3040
		foreach ($config['vlans']['vlan'] as $vlan) {
3041 cf73302f Phil Davis
			if ($vlan['if'] != $realhwif) {
3042 7d5fc0b3 Ermal LUÇI
				continue;
3043 cf73302f Phil Davis
			}
3044 7d5fc0b3 Ermal LUÇI
			$assignedport = convert_real_interface_to_friendly_interface_name($vlan['vlanif']);
3045
			if (!empty($assignedport) && !empty($config['interfaces'][$assignedport]['mtu'])) {
3046 cf73302f Phil Davis
				if (intval($config['interfaces'][$assignedport]['mtu']) > $mtu) {
3047 7d5fc0b3 Ermal LUÇI
					$mtu = $config['interfaces'][$assignedport]['mtu'];
3048 cf73302f Phil Davis
				}
3049 7d5fc0b3 Ermal LUÇI
			}
3050
		}
3051
	}
3052
3053
	return $mtu;
3054
}
3055
3056 f6d89471 Ermal
function interface_vlan_adapt_mtu($vlanifs, $mtu) {
3057
	global $config;
3058
3059 cf73302f Phil Davis
	if (!is_array($vlanifs)) {
3060 f6d89471 Ermal
		return;
3061 cf73302f Phil Davis
	}
3062 f6d89471 Ermal
3063
	/* All vlans need to use the same mtu value as their parent. */
3064
	foreach ($vlanifs as $vlan) {
3065
		$assignedport = convert_real_interface_to_friendly_interface_name($vlan['vlanif']);
3066
		if (!empty($assignedport)) {
3067
			if (!empty($config['interfaces'][$assignedport]['mtu'])) {
3068 3e8035da Ermal LUÇI
				pfSense_interface_mtu($vlan['vlanif'], $config['interfaces'][$assignedport]['mtu']);
3069 f6d89471 Ermal
			} else {
3070 cf73302f Phil Davis
				if (get_interface_mtu($vlan['vlanif']) != $mtu) {
3071 f6d89471 Ermal
					pfSense_interface_mtu($vlan['vlanif'], $mtu);
3072 cf73302f Phil Davis
				}
3073 f6d89471 Ermal
			}
3074 cf73302f Phil Davis
		} else if (get_interface_mtu($vlan['vlanif']) != $mtu) {
3075 f6d89471 Ermal
			pfSense_interface_mtu($vlan['vlanif'], $mtu);
3076 cf73302f Phil Davis
		}
3077 f6d89471 Ermal
	}
3078
}
3079
3080 7413cbfd Ermal
function interface_configure($interface = "wan", $reloadall = false, $linkupevent = false) {
3081 675aac3d Ermal Luçi
	global $config, $g;
3082 31b24870 Ermal Luçi
	global $interface_sn_arr_cache, $interface_ip_arr_cache;
3083 3502b5b1 Seth Mos
	global $interface_snv6_arr_cache, $interface_ipv6_arr_cache;
3084 cfc707f7 Scott Ullrich
3085 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
3086
3087 cf73302f Phil Davis
	if (!isset($wancfg['enable'])) {
3088 e017a46a Ermal
		return;
3089 cf73302f Phil Davis
	}
3090 e017a46a Ermal
3091 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
3092 20cb9803 gnhb
	$realhwif_array = get_parent_interface($interface);
3093
	// Need code to handle MLPPP if we ever use $realhwif for MLPPP handling
3094
	$realhwif = $realhwif_array[0];
3095 cfc707f7 Scott Ullrich
3096 285ef132 Ermal LUÇI
	if (!platform_booting() && !(substr($realif, 0, 4) == "ovpn")) {
3097 3c5e10fc Seth Mos
		/* remove all IPv4 and IPv6 addresses */
3098 c289c48a Ermal
		$tmpifaces = pfSense_getall_interface_addresses($realif);
3099
		if (is_array($tmpifaces)) {
3100
			foreach ($tmpifaces as $tmpiface) {
3101 6b13f8d2 Renato Botelho
				if (is_ipaddrv6($tmpiface) || is_subnetv6($tmpiface)) {
3102 cf73302f Phil Davis
					if (!is_linklocal($tmpiface)) {
3103 6b13f8d2 Renato Botelho
						mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " inet6 {$tmpiface} delete");
3104 cf73302f Phil Davis
					}
3105 6b13f8d2 Renato Botelho
				} else {
3106 d130cd46 Renato Botelho
					if (is_subnetv4($tmpiface)) {
3107
						$tmpip = explode('/', $tmpiface);
3108
						$tmpip = $tmpip[0];
3109 cf73302f Phil Davis
					} else {
3110 d130cd46 Renato Botelho
						$tmpip = $tmpiface;
3111 cf73302f Phil Davis
					}
3112 d130cd46 Renato Botelho
					pfSense_interface_deladdress($realif, $tmpip);
3113
				}
3114 c289c48a Ermal
			}
3115
		}
3116 3896d93e Erik Fonnesbeck
3117 8103bd1e Seth Mos
		/* only bring down the interface when both v4 and v6 are set to NONE */
3118 cf73302f Phil Davis
		if (empty($wancfg['ipaddr']) && empty($wancfg['ipaddrv6'])) {
3119 8103bd1e Seth Mos
			interface_bring_down($interface);
3120 cf73302f Phil Davis
		}
3121 28d22199 Scott Ullrich
	}
3122 acc1e9d0 Scott Ullrich
3123 00e8315b Ermal
	$interface_to_check = $realif;
3124 cf73302f Phil Davis
	if (interface_isppp_type($interface)) {
3125 00e8315b Ermal
		$interface_to_check = $realhwif;
3126 cf73302f Phil Davis
	}
3127 00e8315b Ermal
3128
	/* Need to check that the interface exists or not in the case where its coming back from disabled state see #3270 */
3129 cf73302f Phil Davis
	if (!platform_booting() && (in_array(substr($realif, 0, 3), array("gre", "gif")) || !does_interface_exist($interface_to_check))) {
3130 00e8315b Ermal
		interface_virtual_create($interface_to_check);
3131 cf73302f Phil Davis
	}
3132 00e8315b Ermal
3133
	/* Disable Accepting router advertisements unless specifically requested */
3134 cf73302f Phil Davis
	if ($g['debug']) {
3135 00e8315b Ermal
		log_error("Deny router advertisements for interface {$interface}");
3136 cf73302f Phil Davis
	}
3137 5b7c2403 Ermal LUÇI
	mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " inet6 -accept_rtadv", true);
3138 00e8315b Ermal
3139 0a28d385 Erik Fonnesbeck
	/* wireless configuration? */
3140 cf73302f Phil Davis
	if (is_array($wancfg['wireless'])) {
3141 0a28d385 Erik Fonnesbeck
		interface_wireless_configure($realif, $wancfg, $wancfg['wireless']);
3142 cf73302f Phil Davis
	}
3143 cfc707f7 Scott Ullrich
3144 49db607f jim-p
	$mac = get_interface_mac($realhwif);
3145 1489e8c8 Renato Botelho
	/*
3146
	 * Don't try to reapply the spoofed MAC if it's already applied.
3147
	 * When ifconfig link is used, it cycles the interface down/up, which triggers
3148
	 * the interface config again, which attempts to spoof the MAC again,
3149
	 * which cycles the link again...
3150
	 */
3151 49db607f jim-p
	if ($wancfg['spoofmac'] && ($wancfg['spoofmac'] != $mac)) {
3152 3e5d0d1d Ermal
		mwexec("/sbin/ifconfig " . escapeshellarg($realhwif) .
3153 5b237745 Scott Ullrich
			" link " . escapeshellarg($wancfg['spoofmac']));
3154 6c07db48 Phil Davis
	} else {
3155 a687f866 Namezero
3156 3e5d0d1d Ermal
		if ($mac == "ff:ff:ff:ff:ff:ff") {
3157 f36d4bd2 Scott Ullrich
			/*   this is not a valid mac address.  generate a
3158
			 *   temporary mac address so the machine can get online.
3159
			 */
3160 07e40c1f Carlos Eduardo Ramos
			echo gettext("Generating new MAC address.");
3161 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
3162 3e5d0d1d Ermal
			mwexec("/sbin/ifconfig " . escapeshellarg($realhwif) .
3163 f36d4bd2 Scott Ullrich
				" link " . escapeshellarg($random_mac));
3164
			$wancfg['spoofmac'] = $random_mac;
3165
			write_config();
3166 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");
3167 f36d4bd2 Scott Ullrich
		}
3168
	}
3169 cfc707f7 Scott Ullrich
3170 5b237745 Scott Ullrich
	/* media */
3171
	if ($wancfg['media'] || $wancfg['mediaopt']) {
3172 3e5d0d1d Ermal
		$cmd = "/sbin/ifconfig " . escapeshellarg($realhwif);
3173 cf73302f Phil Davis
		if ($wancfg['media']) {
3174 5b237745 Scott Ullrich
			$cmd .= " media " . escapeshellarg($wancfg['media']);
3175 cf73302f Phil Davis
		}
3176
		if ($wancfg['mediaopt']) {
3177 5b237745 Scott Ullrich
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
3178 cf73302f Phil Davis
		}
3179 5b237745 Scott Ullrich
		mwexec($cmd);
3180
	}
3181 51d5aad7 Ermal
3182 43517fcc Ermal LUÇI
	/* Apply hw offloading policies as configured */
3183
	enable_hardware_offloading($interface);
3184 51d5aad7 Ermal
3185 a46dc3c7 Chris Buechler
	/* invalidate interface/ip/sn cache */
3186 eba938e3 Scott Ullrich
	get_interface_arr(true);
3187 31b24870 Ermal Luçi
	unset($interface_ip_arr_cache[$realif]);
3188
	unset($interface_sn_arr_cache[$realif]);
3189 5a5413bb Seth Mos
	unset($interface_ipv6_arr_cache[$realif]);
3190
	unset($interface_snv6_arr_cache[$realif]);
3191 ccbd2447 Ermal Luçi
3192 6191b321 Ermal
	$tunnelif = substr($realif, 0, 3);
3193 ffeb5acf Scott Ullrich
3194 cf73302f Phil Davis
	if (does_interface_exist($wancfg['if'])) {
3195 afb38815 Ermal LUÇI
		interfaces_bring_up($wancfg['if']);
3196 cf73302f Phil Davis
	}
3197 5a5413bb Seth Mos
3198 d35233da Ermal
	if (!empty($wancfg['mtu'])) {
3199 7965c38f Ermal
		if (stristr($realif, "_vlan")) {
3200 f6d89471 Ermal
			$assignedparent = convert_real_interface_to_friendly_interface_name($realhwif);
3201 3e8035da Ermal LUÇI
			if (!empty($assignedparent) && !empty($config['interfaces'][$assignedparent]['mtu'])) {
3202 a1d36777 Renato Botelho
				$parentmtu = $config['interfaces'][$assignedparent]['mtu'];
3203 cf73302f Phil Davis
				if ($wancfg['mtu'] > $parentmtu) {
3204 3e8035da Ermal LUÇI
					log_error("There is a conflict on MTU between parent {$realhwif} and VLAN({$realif})");
3205 cf73302f Phil Davis
				}
3206
			} else {
3207 7bc73d5e Ermal LUÇI
				$parentmtu = 0;
3208 cf73302f Phil Davis
			}
3209 be2c39b6 Renato Botelho
3210 3e8035da Ermal LUÇI
			$parentmtu = interface_vlan_mtu_configured($realhwif, $parentmtu);
3211
3212 cf73302f Phil Davis
			if (get_interface_mtu($realhwif) != $parentmtu) {
3213 3e8035da Ermal LUÇI
				pfSense_interface_mtu($realhwif, $parentmtu);
3214 cf73302f Phil Davis
			}
3215 be2c39b6 Renato Botelho
3216 31ddb935 Ermal LUÇI
			/* All vlans need to use the same mtu value as their parent. */
3217 3e8035da Ermal LUÇI
			interface_vlan_adapt_mtu(link_interface_to_vlans($realhwif), $parentmtu);
3218 71c26c22 Renato Botelho
		} else if (substr($realif, 0, 4) == 'lagg') {
3219
			/* LAGG interface must be destroyed and re-created to change MTU */
3220
			if ($wancfg['mtu'] != get_interface_mtu($realif)) {
3221
				if (isset($config['laggs']['lagg']) && is_array($config['laggs']['lagg'])) {
3222
					foreach ($config['laggs']['lagg'] as $lagg) {
3223
						if ($lagg['laggif'] == $realif) {
3224
							interface_lagg_configure($lagg);
3225
							break;
3226
						}
3227
					}
3228
				}
3229
			}
3230 a362a23a Ermal
		} else {
3231 cf73302f Phil Davis
			if ($wancfg['mtu'] != get_interface_mtu($realif)) {
3232 f6d89471 Ermal
				pfSense_interface_mtu($realif, $wancfg['mtu']);
3233 cf73302f Phil Davis
			}
3234 4ffa46bf Ermal
3235 f6d89471 Ermal
			/* This case is needed when the parent of vlans is being configured */
3236 3e8035da Ermal LUÇI
			$vlans = link_interface_to_vlans($realif);
3237 cf73302f Phil Davis
			if (is_array($vlans)) {
3238 3e8035da Ermal LUÇI
				interface_vlan_adapt_mtu($vlans, $wancfg['mtu']);
3239 cf73302f Phil Davis
			}
3240 3e8035da Ermal LUÇI
			unset($vlans);
3241 3f3252f6 Renato Botelho
		}
3242 71c26c22 Renato Botelho
		/* XXX: What about gre/gif/.. ? */
3243 3f3252f6 Renato Botelho
	}
3244 4a735210 Renato Botelho
3245 afb38815 Ermal LUÇI
	switch ($wancfg['ipaddr']) {
3246 cf73302f Phil Davis
		case 'dhcp':
3247
			interface_dhcp_configure($interface);
3248
			break;
3249
		case 'pppoe':
3250
		case 'l2tp':
3251
		case 'pptp':
3252
		case 'ppp':
3253
			interface_ppps_configure($interface);
3254
			break;
3255
		default:
3256
			/* XXX: Kludge for now related to #3280 */
3257
			if (!in_array($tunnelif, array("gif", "gre", "ovp"))) {
3258
				if (is_ipaddrv4($wancfg['ipaddr']) && $wancfg['subnet'] <> "") {
3259
					pfSense_interface_setaddress($realif, "{$wancfg['ipaddr']}/{$wancfg['subnet']}");
3260
				}
3261
			}
3262
			break;
3263 afb38815 Ermal LUÇI
	}
3264
3265
	switch ($wancfg['ipaddrv6']) {
3266 cf73302f Phil Davis
		case 'slaac':
3267
		case 'dhcp6':
3268
			interface_dhcpv6_configure($interface, $wancfg);
3269
			break;
3270
		case '6rd':
3271
			interface_6rd_configure($interface, $wancfg);
3272
			break;
3273
		case '6to4':
3274
			interface_6to4_configure($interface, $wancfg);
3275
			break;
3276
		case 'track6':
3277
			interface_track6_configure($interface, $wancfg, $linkupevent);
3278
			break;
3279
		default:
3280
			/* XXX: Kludge for now related to #3280 */
3281
			if (!in_array($tunnelif, array("gif", "gre", "ovp"))) {
3282
				if (is_ipaddrv6($wancfg['ipaddrv6']) && $wancfg['subnetv6'] <> "") {
3283
					//pfSense_interface_setaddress($realif, "{$wancfg['ipaddrv6']}/{$wancfg['subnetv6']}");
3284
					// FIXME: Add IPv6 Support to the pfSense module
3285
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " inet6 {$wancfg['ipaddrv6']} prefixlen " . escapeshellarg($wancfg['subnetv6']));
3286
				}
3287
			}
3288
			break;
3289 afb38815 Ermal LUÇI
	}
3290 67b057a9 Ermal
3291
	interface_netgraph_needed($interface);
3292 be45aa79 Renato Botelho
3293 285ef132 Ermal LUÇI
	if (!platform_booting()) {
3294 dcadda55 Ermal
		link_interface_to_vips($interface, "update");
3295 6991dcb1 Ermal
3296 7de6a47f Ermal LUÇI
		if ($tunnelif != 'gre') {
3297
			unset($gre);
3298
			$gre = link_interface_to_gre($interface);
3299 cf73302f Phil Davis
			if (!empty($gre)) {
3300 7de6a47f Ermal LUÇI
				array_walk($gre, 'interface_gre_configure');
3301 cf73302f Phil Davis
			}
3302 7de6a47f Ermal LUÇI
		}
3303
3304
		if ($tunnelif != 'gif') {
3305
			unset($gif);
3306 cf73302f Phil Davis
			$gif = link_interface_to_gif ($interface);
3307
			if (!empty($gif)) {
3308 7de6a47f Ermal LUÇI
				array_walk($gif, 'interface_gif_configure');
3309 cf73302f Phil Davis
			}
3310 7de6a47f Ermal LUÇI
		}
3311 a639bb91 Ermal
3312 bf17eb72 Ermal
		if ($linkupevent == false || substr($realif, 0, 4) == "ovpn") {
3313 7413cbfd Ermal
			unset($bridgetmp);
3314
			$bridgetmp = link_interface_to_bridge($interface);
3315 cf73302f Phil Davis
			if (!empty($bridgetmp)) {
3316 7413cbfd Ermal
				interface_bridge_add_member($bridgetmp, $realif);
3317 cf73302f Phil Davis
			}
3318 7413cbfd Ermal
		}
3319 ccbd2447 Ermal Luçi
3320 48f23632 Ermal
		$grouptmp = link_interface_to_group($interface);
3321 cf73302f Phil Davis
		if (!empty($grouptmp)) {
3322 ed62880b Ermal
			array_walk($grouptmp, 'interface_group_add_member');
3323 cf73302f Phil Davis
		}
3324 48f23632 Ermal
3325 cf73302f Phil Davis
		if ($interface == "lan") {
3326 4476d447 Ermal Luçi
			/* make new hosts file */
3327 ffeb5acf Scott Ullrich
			system_hosts_generate();
3328 cf73302f Phil Davis
		}
3329 4476d447 Ermal Luçi
3330 a5d6f60b Ermal Lu?i
		if ($reloadall == true) {
3331 cfc707f7 Scott Ullrich
3332 a5d6f60b Ermal Lu?i
			/* reconfigure static routes (kernel may have deleted them) */
3333 1ea67f2e Ermal
			system_routing_configure($interface);
3334 cfc707f7 Scott Ullrich
3335 a5d6f60b Ermal Lu?i
			/* reload ipsec tunnels */
3336 b2448aa0 Ermal LUÇI
			send_event("service reload ipsecdns");
3337 cfc707f7 Scott Ullrich
3338 9a51bb64 Warren Baker
			/* restart dnsmasq or unbound */
3339 cf73302f Phil Davis
			if (isset($config['dnsmasq']['enable'])) {
3340 9a51bb64 Warren Baker
				services_dnsmasq_configure();
3341 cf73302f Phil Davis
			} elseif (isset($config['unbound']['enable'])) {
3342 9a51bb64 Warren Baker
				services_unbound_configure();
3343 cf73302f Phil Davis
			}
3344 b5eeef07 Ermal
3345 a5d6f60b Ermal Lu?i
			/* update dyndns */
3346 422bc2a7 Ermal
			send_event("service reload dyndns {$interface}");
3347 a23d7248 Scott Ullrich
3348 a5d6f60b Ermal Lu?i
			/* reload captive portal */
3349 cf73302f Phil Davis
			if (!function_exists('captiveportal_init_rules_byinterface')) {
3350 a08db603 Ermal LUÇI
				require_once('captiveportal.inc');
3351 cf73302f Phil Davis
			}
3352 a08db603 Ermal LUÇI
			captiveportal_init_rules_byinterface($interface);
3353 a5d6f60b Ermal Lu?i
		}
3354 5b237745 Scott Ullrich
	}
3355 cfc707f7 Scott Ullrich
3356 c1d8c235 Renato Botelho
	interfaces_staticarp_configure($interface);
3357 5b237745 Scott Ullrich
	return 0;
3358
}
3359
3360 b67e9e62 Ermal
function interface_track6_configure($interface = "lan", $wancfg, $linkupevent = false) {
3361 20a7cb15 smos
	global $config, $g;
3362
3363 cf73302f Phil Davis
	if (!is_array($wancfg)) {
3364 7a04cd20 Ermal
		return;
3365 cf73302f Phil Davis
	}
3366 7a04cd20 Ermal
3367 cf73302f Phil Davis
	if (!isset($wancfg['enable'])) {
3368 b0059636 Ermal
		return;
3369 cf73302f Phil Davis
	}
3370 be45aa79 Renato Botelho
3371 20a7cb15 smos
	/* If the interface is not configured via another, exit */
3372 cf73302f Phil Davis
	if (empty($wancfg['track6-interface'])) {
3373 b0059636 Ermal
		return;
3374 cf73302f Phil Davis
	}
3375 20a7cb15 smos
3376 e90c833a smos
	/* always configure a link-local of fe80::1:1 on the track6 interfaces */
3377
	$realif = get_real_interface($interface);
3378 919ff1f0 Ermal
	$linklocal = find_interface_ipv6_ll($realif);
3379 cf73302f Phil Davis
	if (!empty($linklocal)) {
3380 919ff1f0 Ermal
		mwexec("/sbin/ifconfig {$realif} inet6 {$linklocal} delete");
3381 cf73302f Phil Davis
	}
3382 55909a9a Ermal
	/* XXX: This might break for good on a carp installation using link-local as network ips */
3383
	/* XXX: Probably should remove? */
3384 e90c833a smos
	mwexec("/sbin/ifconfig {$realif} inet6 fe80::1:1%{$realif}");
3385
3386 7a04cd20 Ermal
	$trackcfg = $config['interfaces'][$wancfg['track6-interface']];
3387
	if (!isset($trackcfg['enable'])) {
3388
		log_error("Interface {$interface} tracking non-existant interface {$wancfg['track6-interface']}");
3389
		return;
3390
	}
3391
3392 086cf944 Phil Davis
	switch ($trackcfg['ipaddrv6']) {
3393 cf73302f Phil Davis
		case "6to4":
3394
			if ($g['debug']) {
3395
				log_error("Interface {$interface} configured via {$wancfg['track6-interface']}  type {$type}");
3396
			}
3397
			interface_track6_6to4_configure($interface, $wancfg);
3398
			break;
3399
		case "6rd":
3400
			if ($g['debug']) {
3401
				log_error("Interface {$interface} configured via {$wancfg['track6-interface']}  type {$type}");
3402
			}
3403
			interface_track6_6rd_configure($interface, $wancfg);
3404
			break;
3405
		case "dhcp6":
3406
			if ($linkupevent == true) {
3407
				/*
3408
				 * NOTE: Usually come here from rc.linkup calling so just call directly instead of generating event
3409
				 * 	Instead of disrupting all other v4 configuration just restart DHCPv6 client for now
3410
				 *
3411
				 * XXX: Probably DHCPv6 client should handle this automagically itself?
3412
				 */
3413
				$parentrealif = get_real_interface($wancfg['track6-interface']);
3414
				$pidv6 = find_dhcp6c_process($parentrealif);
3415
				if ($pidv6) {
3416
					posix_kill($pidv6, SIGHUP);
3417
				}
3418
			}
3419
			break;
3420 20a7cb15 smos
	}
3421 b0059636 Ermal
3422 3377dc9d Ermal LUÇI
	if ($linkupevent == false) {
3423 cf73302f Phil Davis
		if (!function_exists('services_dhcpd_configure')) {
3424 7fd67662 Ermal
			require_once("services.inc");
3425 cf73302f Phil Davis
		}
3426 6387590f Ermal
3427 cf73302f Phil Davis
		if (isset($config['unbound']['enable'])) {
3428 a623defd Chris Buechler
			services_unbound_configure();
3429 cf73302f Phil Davis
		}
3430 e26effd3 Chris Buechler
3431 7fd67662 Ermal
		services_dhcpd_configure("inet6");
3432
	}
3433 6387590f Ermal
3434 20a7cb15 smos
	return 0;
3435
}
3436
3437 7a04cd20 Ermal
function interface_track6_6rd_configure($interface = "lan", $lancfg) {
3438 20a7cb15 smos
	global $config, $g;
3439 da9dd1b4 Ermal
	global $interface_ipv6_arr_cache;
3440 a8f5790a Renato Botelho
	global $interface_snv6_arr_cache;
3441 20a7cb15 smos
3442 cf73302f Phil Davis
	if (!is_array($lancfg)) {
3443 b0059636 Ermal
		return;
3444 cf73302f Phil Davis
	}
3445 be45aa79 Renato Botelho
3446 20a7cb15 smos
	/* If the interface is not configured via another, exit */
3447 cf73302f Phil Davis
	if (empty($lancfg['track6-interface'])) {
3448 b0059636 Ermal
		return;
3449 cf73302f Phil Davis
	}
3450 b0059636 Ermal
3451 20a7cb15 smos
	$wancfg = $config['interfaces'][$lancfg['track6-interface']];
3452 7a04cd20 Ermal
	if (empty($wancfg)) {
3453
		log_error("Interface {$interface} tracking non-existant interface {$lancfg['track6-interface']}");
3454 b0059636 Ermal
		return;
3455 7a04cd20 Ermal
	}
3456 be45aa79 Renato Botelho
3457 ff5674dc Ermal
	$ip4address = get_interface_ip($lancfg['track6-interface']);
3458 7a04cd20 Ermal
	if (!is_ipaddrv4($ip4address)) { /* XXX: This should not be needed by 6rd || (is_private_ip($ip4address))) { */
3459 7bc953d1 Chris Buechler
		log_error("The interface IPv4 '{$ip4address}' address on interface '{$lancfg['track6-interface']}' is not valid, not configuring 6RD tunnel");
3460 b0059636 Ermal
		return;
3461 20a7cb15 smos
	}
3462
	$hexwanv4 = return_hex_ipv4($ip4address);
3463 be45aa79 Renato Botelho
3464 20a7cb15 smos
	/* create the long prefix notation for math, save the prefix length */
3465
	$rd6prefix = explode("/", $wancfg['prefix-6rd']);
3466
	$rd6prefixlen = $rd6prefix[1];
3467
	$rd6prefix = Net_IPv6::uncompress($rd6prefix[0]);
3468
3469
	/* binary presentation of the prefix for all 128 bits. */
3470
	$rd6lanbin = convert_ipv6_to_128bit($rd6prefix);
3471 be45aa79 Renato Botelho
3472 20a7cb15 smos
	/* just save the left prefix length bits */
3473
	$rd6lanbin = substr($rd6lanbin, 0, $rd6prefixlen);
3474
	/* add the v4 address, offset n bits from the left */
3475
	$rd6lanbin .= substr(sprintf("%032b", hexdec($hexwanv4)), (0 + $wancfg['prefix-6rd-v4plen']), 32);
3476
3477
	/* add the custom prefix id, max 32bits long? (64 bits - (prefixlen + (32 - v4plen)) */
3478
	/* 64 - (37 + (32 - 17)) = 8 == /52 */
3479
	$restbits = 64 - ($rd6prefixlen + (32 - $wancfg['prefix-6rd-v4plen']));
3480
	// echo "64 - (prefixlen {$rd6prefixlen} + v4len (32 - {$wancfg['prefix-6rd-v4plen']})) = {$restbits} \n";
3481
	$rd6lanbin .= substr(sprintf("%032b", str_pad($lancfg['track6-prefix-id'], 32, "0", STR_PAD_LEFT)), (32 - $restbits), 32);
3482
	/* fill the rest out with zeros */
3483 91f026b0 ayvis
	$rd6lanbin = str_pad($rd6lanbin, 128, "0", STR_PAD_RIGHT);
3484 20a7cb15 smos
3485 be45aa79 Renato Botelho
	/* convert the 128 bits for the lan address back into a valid IPv6 address */
3486 20a7cb15 smos
	$rd6lan = convert_128bit_to_ipv6($rd6lanbin) ."1";
3487 be45aa79 Renato Botelho
3488 b0059636 Ermal
	$lanif = get_real_interface($interface);
3489 c4fc2eae Ermal
	$oip = find_interface_ipv6($lanif);
3490 cf73302f Phil Davis
	if (is_ipaddrv6($oip)) {
3491 c4fc2eae Ermal
		mwexec("/sbin/ifconfig {$lanif} inet6 {$oip} delete");
3492 cf73302f Phil Davis
	}
3493 da9dd1b4 Ermal
	unset($interface_ipv6_arr_cache[$lanif]);
3494
	unset($interface_snv6_arr_cache[$lanif]);
3495 20a7cb15 smos
	log_error("rd6 {$interface} with ipv6 address {$rd6lan} based on {$lancfg['track6-interface']} ipv4 {$ip4address}");
3496
	mwexec("/sbin/ifconfig {$lanif} inet6 {$rd6lan} prefixlen 64");
3497 b0059636 Ermal
3498 20a7cb15 smos
	return 0;
3499
}
3500
3501 7a04cd20 Ermal
function interface_track6_6to4_configure($interface = "lan", $lancfg) {
3502 20a7cb15 smos
	global $config, $g;
3503 da9dd1b4 Ermal
	global $interface_ipv6_arr_cache;
3504 a8f5790a Renato Botelho
	global $interface_snv6_arr_cache;
3505 20a7cb15 smos
3506 cf73302f Phil Davis
	if (!is_array($lancfg)) {
3507 b0059636 Ermal
		return;
3508 cf73302f Phil Davis
	}
3509 be45aa79 Renato Botelho
3510 20a7cb15 smos
	/* If the interface is not configured via another, exit */
3511 cf73302f Phil Davis
	if (empty($lancfg['track6-interface'])) {
3512 b0059636 Ermal
		return;
3513 cf73302f Phil Davis
	}
3514 b0059636 Ermal
3515 7a04cd20 Ermal
	$wancfg = $config['interfaces'][$lancfg['track6-interface']];
3516
	if (empty($wancfg)) {
3517
		log_error("Interface {$interface} tracking non-existant interface {$lancfg['track6-interface']}");
3518
		return;
3519
	}
3520
3521 ff5674dc Ermal
	$ip4address = get_interface_ip($lancfg['track6-interface']);
3522 b0059636 Ermal
	if (!is_ipaddrv4($ip4address) || is_private_ip($ip4address)) {
3523 ff5674dc Ermal
		log_error("The interface IPv4 '{$ip4address}' address on interface '{$lancfg['track6-interface']}' is not public, not configuring 6RD tunnel");
3524 b0059636 Ermal
		return;
3525 20a7cb15 smos
	}
3526
	$hexwanv4 = return_hex_ipv4($ip4address);
3527 be45aa79 Renato Botelho
3528 20a7cb15 smos
	/* create the long prefix notation for math, save the prefix length */
3529
	$sixto4prefix = "2002::";
3530
	$sixto4prefixlen = 16;
3531
	$sixto4prefix = Net_IPv6::uncompress($sixto4prefix);
3532
3533
	/* binary presentation of the prefix for all 128 bits. */
3534
	$sixto4lanbin = convert_ipv6_to_128bit($sixto4prefix);
3535 be45aa79 Renato Botelho
3536 20a7cb15 smos
	/* just save the left prefix length bits */
3537
	$sixto4lanbin = substr($sixto4lanbin, 0, $sixto4prefixlen);
3538
	/* add the v4 address */
3539
	$sixto4lanbin .= sprintf("%032b", hexdec($hexwanv4));
3540
	/* add the custom prefix id */
3541
	$sixto4lanbin .= sprintf("%016b", $lancfg['track6-prefix-id']);
3542
	/* fill the rest out with zeros */
3543 91f026b0 ayvis
	$sixto4lanbin = str_pad($sixto4lanbin, 128, "0", STR_PAD_RIGHT);
3544 be45aa79 Renato Botelho
3545
	/* convert the 128 bits for the lan address back into a valid IPv6 address */
3546 20a7cb15 smos
	$sixto4lan = convert_128bit_to_ipv6($sixto4lanbin) ."1";
3547 be45aa79 Renato Botelho
3548 b0059636 Ermal
	$lanif = get_real_interface($interface);
3549 c4fc2eae Ermal
	$oip = find_interface_ipv6($lanif);
3550 cf73302f Phil Davis
	if (is_ipaddrv6($oip)) {
3551 c4fc2eae Ermal
		mwexec("/sbin/ifconfig {$lanif} inet6 {$oip} delete");
3552 cf73302f Phil Davis
	}
3553 da9dd1b4 Ermal
	unset($interface_ipv6_arr_cache[$lanif]);
3554
	unset($interface_snv6_arr_cache[$lanif]);
3555 20a7cb15 smos
	log_error("sixto4 {$interface} with ipv6 address {$sixto4lan} based on {$lancfg['track6-interface']} ipv4 {$ip4address}");
3556
	mwexec("/sbin/ifconfig {$lanif} inet6 {$sixto4lan} prefixlen 64");
3557 b0059636 Ermal
3558 20a7cb15 smos
	return 0;
3559
}
3560
3561 7a04cd20 Ermal
function interface_6rd_configure($interface = "wan", $wancfg) {
3562 668e8961 smos
	global $config, $g;
3563
3564 be45aa79 Renato Botelho
	/* because this is a tunnel interface we can only function
3565 668e8961 smos
	 *	with a public IPv4 address on the interface */
3566
3567 cf73302f Phil Davis
	if (!is_array($wancfg)) {
3568 b0059636 Ermal
		return;
3569 cf73302f Phil Davis
	}
3570 668e8961 smos
3571 cf73302f Phil Davis
	if (!is_module_loaded('if_stf.ko')) {
3572 ebafd470 Ermal
		mwexec('/sbin/kldload if_stf.ko');
3573 cf73302f Phil Davis
	}
3574 ebafd470 Ermal
3575 668e8961 smos
	$wanif = get_real_interface($interface);
3576
	$ip4address = find_interface_ip($wanif);
3577 5d697e08 Ermal
	if (!is_ipaddrv4($ip4address)) {
3578 668e8961 smos
		log_error("The interface IPv4 '{$ip4address}' address on interface '{$wanif}' is not public, not configuring 6RD tunnel");
3579 1f78ab3a smos
		return false;
3580 668e8961 smos
	}
3581 20a7cb15 smos
	$hexwanv4 = return_hex_ipv4($ip4address);
3582 be45aa79 Renato Botelho
3583 cf73302f Phil Davis
	if (!is_numeric($wancfg['prefix-6rd-v4plen'])) {
3584 20a7cb15 smos
		$wancfg['prefix-6rd-v4plen'] = 0;
3585 cf73302f Phil Davis
	}
3586 668e8961 smos
3587 51c57aae smos
	/* create the long prefix notation for math, save the prefix length */
3588 f87ccbed smos
	$rd6prefix = explode("/", $wancfg['prefix-6rd']);
3589
	$rd6prefixlen = $rd6prefix[1];
3590 f233ddeb Ermal
	$brgw = explode('.', $wancfg['gateway-6rd']);
3591 5e964cfd Ermal LUÇI
	$rd6brgw = substr(Net_IPv6::_ip2Bin($rd6prefix[0]), 0, $rd6prefixlen);
3592
	$rd6brgw .= str_pad(decbin($brgw[0]), 8, '0', STR_PAD_LEFT) . str_pad(decbin($brgw[1]), 8, '0', STR_PAD_LEFT) . str_pad(decbin($brgw[2]), 8, '0', STR_PAD_LEFT) . str_pad(decbin($brgw[3]), 8, '0', STR_PAD_LEFT);
3593 cf73302f Phil Davis
	if (strlen($rd6brgw) < 128) {
3594 5e964cfd Ermal LUÇI
		$rd6brgw = str_pad($rd6brgw, 128, '0', STR_PAD_RIGHT);
3595 cf73302f Phil Davis
	}
3596 5e964cfd Ermal LUÇI
	$rd6brgw = Net_IPv6::compress(Net_IPv6::_bin2Ip($rd6brgw));
3597 f233ddeb Ermal
	unset($brgw);
3598 f87ccbed smos
	$rd6prefix = Net_IPv6::uncompress($rd6prefix[0]);
3599 51c57aae smos
3600
	/* binary presentation of the prefix for all 128 bits. */
3601 20a7cb15 smos
	$rd6prefixbin = convert_ipv6_to_128bit($rd6prefix);
3602 be45aa79 Renato Botelho
3603 51c57aae smos
	/* just save the left prefix length bits */
3604 20a7cb15 smos
	$rd6prefixbin = substr($rd6prefixbin, 0, $rd6prefixlen);
3605 51c57aae smos
	/* if the prefix length is not 32 bits we need to shave bits off from the left of the v4 address. */
3606 4aa569bd smos
	$rd6prefixbin .= substr(sprintf("%032b", hexdec($hexwanv4)), $wancfg['prefix-6rd-v4plen'], 32);
3607 20a7cb15 smos
	/* fill out the rest with 0's */
3608 91f026b0 ayvis
	$rd6prefixbin = str_pad($rd6prefixbin, 128, "0", STR_PAD_RIGHT);
3609 51c57aae smos
3610 be45aa79 Renato Botelho
	/* convert the 128 bits for the broker address back into a valid IPv6 address */
3611 4aa569bd smos
	$rd6prefix = convert_128bit_to_ipv6($rd6prefixbin);
3612 f87ccbed smos
3613 733c6f89 Ermal
3614 7d1f2eac Ermal
	/* XXX: need to extend to support variable prefix size for v4 */
3615 cf73302f Phil Davis
	if (!is_module_loaded("if_stf")) {
3616 b686e5d0 Ermal
		mwexec("/sbin/kldload if_stf.ko");
3617 cf73302f Phil Davis
	}
3618 7d1f2eac Ermal
	$stfiface = "{$interface}_stf";
3619 cf73302f Phil Davis
	if (does_interface_exist($stfiface)) {
3620 c4fc2eae Ermal
		pfSense_interface_destroy($stfiface);
3621 cf73302f Phil Davis
	}
3622 c4fc2eae Ermal
	$tmpstfiface = pfSense_interface_create("stf");
3623
	pfSense_interface_rename($tmpstfiface, $stfiface);
3624 7d1f2eac Ermal
	pfSense_interface_flags($stfiface, IFF_LINK2);
3625
	mwexec("/sbin/ifconfig {$stfiface} inet6 {$rd6prefix}/{$rd6prefixlen}");
3626 f0f714c5 Ermal
	mwexec("/sbin/ifconfig {$stfiface} stfv4br " . escapeshellarg($wancfg['gateway-6rd']));
3627 cf73302f Phil Davis
	if ($wancfg['prefix-6rd-v4plen'] >= 0 && $wancfg['prefix-6rd-v4plen'] <= 32) {
3628 5d697e08 Ermal
		mwexec("/sbin/ifconfig {$stfiface} stfv4net {$ip4address}/" . escapeshellarg($wancfg['prefix-6rd-v4plen']));
3629 cf73302f Phil Davis
	}
3630
	if ($g['debug']) {
3631 7d1f2eac Ermal
		log_error("Created 6rd interface {$stfiface} {$rd6prefix}/{$rd6prefixlen}");
3632 cf73302f Phil Davis
	}
3633 668e8961 smos
3634 f55b6cbb smos
	/* write out a default router file */
3635 20a7cb15 smos
	file_put_contents("{$g['tmp_path']}/{$wanif}_routerv6", "{$rd6brgw}\n");
3636
	file_put_contents("{$g['tmp_path']}/{$wanif}_defaultgwv6", "{$rd6brgw}\n");
3637 2d5ca06e smos
3638 5ee79d32 Ermal
	$ip4gateway = get_interface_gateway($interface);
3639 cf73302f Phil Davis
	if (is_ipaddrv4($ip4gateway)) {
3640 87dfd826 Ermal
		mwexec("/sbin/route change -host " . escapeshellarg($wancfg['gateway-6rd']) . " {$ip4gateway}");
3641 cf73302f Phil Davis
	}
3642 c8ed8142 smos
3643 2d5ca06e smos
	/* configure dependent interfaces */
3644 cf73302f Phil Davis
	if (!platform_booting()) {
3645 7a04cd20 Ermal
		link_interface_to_track6($interface, "update");
3646 cf73302f Phil Davis
	}
3647 66c73aab Ermal
3648 f55b6cbb smos
	return 0;
3649 668e8961 smos
}
3650
3651 cf73302f Phil Davis
function interface_6to4_configure($interface = "wan", $wancfg) {
3652 31c43fd3 smos
	global $config, $g;
3653
3654 be45aa79 Renato Botelho
	/* because this is a tunnel interface we can only function
3655 31c43fd3 smos
	 *	with a public IPv4 address on the interface */
3656
3657 cf73302f Phil Davis
	if (!is_array($wancfg)) {
3658 7a04cd20 Ermal
		return;
3659 cf73302f Phil Davis
	}
3660 31c43fd3 smos
3661
	$wanif = get_real_interface($interface);
3662
	$ip4address = find_interface_ip($wanif);
3663 cf73302f Phil Davis
	if ((!is_ipaddrv4($ip4address)) || (is_private_ip($ip4address))) {
3664 31c43fd3 smos
		log_error("The interface IPv4 '{$ip4address}' address on interface '{$wanif}' is not public, not configuring 6RD tunnel");
3665
		return false;
3666
	}
3667 be45aa79 Renato Botelho
3668 31c43fd3 smos
	/* create the long prefix notation for math, save the prefix length */
3669
	$stfprefixlen = 16;
3670
	$stfprefix = Net_IPv6::uncompress("2002::");
3671
	$stfarr = explode(":", $stfprefix);
3672
	$v4prefixlen = "0";
3673 be45aa79 Renato Botelho
3674 31c43fd3 smos
	/* we need the hex form of the interface IPv4 address */
3675
	$ip4arr = explode(".", $ip4address);
3676
	$hexwanv4 = "";
3677 cf73302f Phil Davis
	foreach ($ip4arr as $octet) {
3678 31c43fd3 smos
		$hexwanv4 .= sprintf("%02x", $octet);
3679 cf73302f Phil Davis
	}
3680 31c43fd3 smos
3681
	/* we need the hex form of the broker IPv4 address */
3682
	$ip4arr = explode(".", "192.88.99.1");
3683
	$hexbrv4 = "";
3684 cf73302f Phil Davis
	foreach ($ip4arr as $octet) {
3685 31c43fd3 smos
		$hexbrv4 .= sprintf("%02x", $octet);
3686 cf73302f Phil Davis
	}
3687 be45aa79 Renato Botelho
3688 31c43fd3 smos
	/* binary presentation of the prefix for all 128 bits. */
3689
	$stfprefixbin = "";
3690 cf73302f Phil Davis
	foreach ($stfarr as $element) {
3691 31c43fd3 smos
		$stfprefixbin .= sprintf("%016b", hexdec($element));
3692
	}
3693
	/* just save the left prefix length bits */
3694
	$stfprefixstartbin = substr($stfprefixbin, 0, $stfprefixlen);
3695
3696
	/* if the prefix length is not 32 bits we need to shave bits off from the left of the v4 address. */
3697
	$stfbrokerbin = substr(sprintf("%032b", hexdec($hexbrv4)), $v4prefixlen, 32);
3698 91f026b0 ayvis
	$stfbrokerbin = str_pad($stfprefixstartbin . $stfbrokerbin, 128, "0", STR_PAD_RIGHT);
3699 31c43fd3 smos
3700
	/* for the local subnet too. */
3701
	$stflanbin = substr(sprintf("%032b", hexdec($hexwanv4)), $v4prefixlen, 32);
3702 91f026b0 ayvis
	$stflanbin = str_pad($stfprefixstartbin . $stflanbin, 128, "0", STR_PAD_RIGHT);
3703 31c43fd3 smos
3704 be45aa79 Renato Botelho
	/* convert the 128 bits for the broker address back into a valid IPv6 address */
3705 31c43fd3 smos
	$stfbrarr = array();
3706
	$stfbrbinarr = array();
3707
	$stfbrbinarr = str_split($stfbrokerbin, 16);
3708 cf73302f Phil Davis
	foreach ($stfbrbinarr as $bin) {
3709 31c43fd3 smos
		$stfbrarr[] = dechex(bindec($bin));
3710 cf73302f Phil Davis
	}
3711 31c43fd3 smos
	$stfbrgw = Net_IPv6::compress(implode(":", $stfbrarr));
3712
3713 be45aa79 Renato Botelho
	/* convert the 128 bits for the broker address back into a valid IPv6 address */
3714 31c43fd3 smos
	$stflanarr = array();
3715
	$stflanbinarr = array();
3716
	$stflanbinarr = str_split($stflanbin, 16);
3717 cf73302f Phil Davis
	foreach ($stflanbinarr as $bin) {
3718 31c43fd3 smos
		$stflanarr[] = dechex(bindec($bin));
3719 cf73302f Phil Davis
	}
3720 31c43fd3 smos
	$stflanpr = Net_IPv6::compress(implode(":", $stflanarr));
3721
	$stflanarr[7] = 1;
3722
	$stflan = Net_IPv6::compress(implode(":", $stflanarr));
3723
3724
	/* setup the stf interface */
3725 cf73302f Phil Davis
	if (!is_module_loaded("if_stf")) {
3726 b686e5d0 Ermal
		mwexec("/sbin/kldload if_stf.ko");
3727 cf73302f Phil Davis
	}
3728 7d1f2eac Ermal
	$stfiface = "{$interface}_stf";
3729 cf73302f Phil Davis
	if (does_interface_exist($stfiface)) {
3730 c4fc2eae Ermal
		pfSense_interface_destroy($stfiface);
3731 cf73302f Phil Davis
	}
3732 c4fc2eae Ermal
	$tmpstfiface = pfSense_interface_create("stf");
3733
	pfSense_interface_rename($tmpstfiface, $stfiface);
3734 7d1f2eac Ermal
	pfSense_interface_flags($stfiface, IFF_LINK2);
3735
	mwexec("/sbin/ifconfig {$stfiface} inet6 {$stflanpr} prefixlen 16");
3736 31c43fd3 smos
3737 cf73302f Phil Davis
	if ($g['debug']) {
3738 7d1f2eac Ermal
		log_error("Set IPv6 address inet6 {$stflanpr} prefixlen 16 for {$stfiface}, route {$stfbrgw}");
3739 cf73302f Phil Davis
	}
3740 be45aa79 Renato Botelho
3741 31c43fd3 smos
	/* write out a default router file */
3742
	file_put_contents("{$g['tmp_path']}/{$wanif}_routerv6", "{$stfbrgw}");
3743
	file_put_contents("{$g['tmp_path']}/{$wanif}_defaultgwv6", "{$stfbrgw}");
3744 2d5ca06e smos
3745 5ee79d32 Ermal
	$ip4gateway = get_interface_gateway($interface);
3746 cf73302f Phil Davis
	if (is_ipaddrv4($ip4gateway)) {
3747 59b99089 Ermal
		mwexec("/sbin/route change -host 192.88.99.1 {$ip4gateway}");
3748 cf73302f Phil Davis
	}
3749 c8ed8142 smos
3750 cf73302f Phil Davis
	if (!platform_booting()) {
3751 7a04cd20 Ermal
		link_interface_to_track6($interface, "update");
3752 cf73302f Phil Davis
	}
3753 be45aa79 Renato Botelho
3754 31c43fd3 smos
	return 0;
3755
}
3756
3757 7a04cd20 Ermal
function interface_dhcpv6_configure($interface = "wan", $wancfg) {
3758 ed395640 Seth Mos
	global $config, $g;
3759
3760 cf73302f Phil Davis
	if (!is_array($wancfg)) {
3761 b0059636 Ermal
		return;
3762 cf73302f Phil Davis
	}
3763 ed395640 Seth Mos
3764 06886ae3 Ermal
	$wanif = get_real_interface($interface, "inet6");
3765 d53a9a51 smos
	$dhcp6cconf = "";
3766 dd5d1a24 k-paulius
3767
	if ($wancfg['adv_dhcp6_config_file_override']) {
3768
		// DHCP6 Config File Override
3769
		$dhcp6cconf = DHCP6_Config_File_Override($wancfg, $wanif);
3770
	} elseif ($wancfg['adv_dhcp6_config_advanced']) {
3771
		// DHCP6 Config File Advanced
3772
		$dhcp6cconf = DHCP6_Config_File_Advanced($interface, $wancfg, $wanif);
3773 d53a9a51 smos
	} else {
3774 dd5d1a24 k-paulius
		// DHCP6 Config File Basic
3775
		$dhcp6cconf .= "interface {$wanif} {\n";
3776
3777
		/* for SLAAC interfaces we do fire off a dhcp6 client for just our name servers */
3778
		if ($wancfg['ipaddrv6'] == "slaac") {
3779 ddd1f864 k-paulius
			$dhcp6cconf .= "\tinformation-only;\n";
3780
			$dhcp6cconf .= "\trequest domain-name-servers;\n";
3781
			$dhcp6cconf .= "\trequest domain-name;\n";
3782
			$dhcp6cconf .= "\tscript \"{$g['varetc_path']}/dhcp6c_{$interface}_script.sh\"; # we'd like some nameservers please\n";
3783 dd5d1a24 k-paulius
			$dhcp6cconf .= "};\n";
3784
		} else {
3785
			$trackiflist = array();
3786
			$iflist = link_interface_to_track6($interface);
3787
			foreach ($iflist as $ifname => $ifcfg) {
3788
				if (is_numeric($ifcfg['track6-prefix-id'])) {
3789
					$trackiflist[$ifname] = $ifcfg;
3790
				}
3791 cf73302f Phil Davis
			}
3792 a13acc0e smos
3793 dd5d1a24 k-paulius
			/* skip address request if this is set */
3794
			if (!isset($wancfg['dhcp6prefixonly'])) {
3795 ddd1f864 k-paulius
				$dhcp6cconf .= "\tsend ia-na 0;\t# request stateful address\n";
3796 dd5d1a24 k-paulius
			}
3797
			if (is_numeric($wancfg['dhcp6-ia-pd-len']) && !empty($trackiflist)) {
3798 ddd1f864 k-paulius
				$dhcp6cconf .= "\tsend ia-pd 0;\t# request prefix delegation\n";
3799 dd5d1a24 k-paulius
			}
3800 d53a9a51 smos
3801 dd5d1a24 k-paulius
			$dhcp6cconf .= "\trequest domain-name-servers;\n";
3802
			$dhcp6cconf .= "\trequest domain-name;\n";
3803
			$dhcp6cconf .= "\tscript \"{$g['varetc_path']}/dhcp6c_{$interface}_script.sh\"; # we'd like some nameservers please\n";
3804
			$dhcp6cconf .= "};\n";
3805 a13acc0e smos
3806 dd5d1a24 k-paulius
			if (!isset($wancfg['dhcp6prefixonly'])) {
3807
				$dhcp6cconf .= "id-assoc na 0 { };\n";
3808 cf73302f Phil Davis
			}
3809 dd5d1a24 k-paulius
3810
			if (is_numeric($wancfg['dhcp6-ia-pd-len']) && !empty($trackiflist)) {
3811
				/* Setup the prefix delegation */
3812
				$dhcp6cconf .= "id-assoc pd 0 {\n";
3813
				$preflen = 64 - $wancfg['dhcp6-ia-pd-len'];
3814
				if (isset($wancfg['dhcp6-ia-pd-send-hint'])) {
3815 ddd1f864 k-paulius
					$dhcp6cconf .= "\tprefix ::/{$preflen} infinity;\n";
3816 cf73302f Phil Davis
				}
3817 dd5d1a24 k-paulius
				foreach ($trackiflist as $friendly => $ifcfg) {
3818
					if ($g['debug']) {
3819
						log_error("setting up $ifdescr - {$ifcfg['track6-prefix-id']}");
3820
					}
3821
					$realif = get_real_interface($friendly);
3822 ddd1f864 k-paulius
					$dhcp6cconf .= "\tprefix-interface {$realif} {\n";
3823
					$dhcp6cconf .= "\t\tsla-id {$ifcfg['track6-prefix-id']};\n";
3824
					$dhcp6cconf .= "\t\tsla-len {$wancfg['dhcp6-ia-pd-len']};\n";
3825
					$dhcp6cconf .= "\t};\n";
3826 dd5d1a24 k-paulius
				}
3827
				unset($preflen, $iflist, $ifcfg, $ifname);
3828
				$dhcp6cconf .= "};\n";
3829 d53a9a51 smos
			}
3830 dd5d1a24 k-paulius
			unset($trackiflist);
3831 d53a9a51 smos
		}
3832 cf73302f Phil Davis
	}
3833 f4dd8b4c N0YB
3834 b0059636 Ermal
	/* wide-dhcp6c works for now. */
3835
	if (!@file_put_contents("{$g['varetc_path']}/dhcp6c_{$interface}.conf", $dhcp6cconf)) {
3836
		printf("Error: cannot open dhcp6c_{$interface}.conf in interface_dhcpv6_configure() for writing.\n");
3837 d12ae241 Renato Botelho
		unset($dhcp6cconf);
3838 b0059636 Ermal
		return 1;
3839
	}
3840 d12ae241 Renato Botelho
	unset($dhcp6cconf);
3841 ed395640 Seth Mos
3842 b0059636 Ermal
	$dhcp6cscript = "#!/bin/sh\n";
3843
	$dhcp6cscript .= "# This shell script launches /etc/rc.newwanipv6 with a interface argument.\n";
3844 9d83d01f Renato Botelho
	$dhcp6cscript .= "dmips=\${new_domain_name_servers}\n";
3845
	$dhcp6cscript .= "dmnames=\${new_domain_name}\n";
3846
	$dhcp6cscript .= "/usr/local/sbin/fcgicli -f /etc/rc.newwanipv6 -d \"interface={$wanif}&dmnames=\${dmnames}&dmips=\${dmips}\"\n";
3847 d53a9a51 smos
	/* Add wide-dhcp6c shell script here. Because we can not pass a argument to it. */
3848 b0059636 Ermal
	if (!@file_put_contents("{$g['varetc_path']}/dhcp6c_{$interface}_script.sh", $dhcp6cscript)) {
3849 d53a9a51 smos
		printf("Error: cannot open dhcp6c_{$interface}_script.sh in interface_dhcpv6_configure() for writing.\n");
3850 d12ae241 Renato Botelho
		unset($dhcp6cscript);
3851 d53a9a51 smos
		return 1;
3852
	}
3853 d12ae241 Renato Botelho
	unset($dhcp6cscript);
3854 b0059636 Ermal
	@chmod("{$g['varetc_path']}/dhcp6c_{$interface}_script.sh", 0755);
3855 d53a9a51 smos
3856 29f2f07a Ermal
	$rtsoldscript = "#!/bin/sh\n";
3857
	$rtsoldscript .= "# This shell script launches dhcp6c and configured gateways for this interface.\n";
3858
	$rtsoldscript .= "echo $2 > {$g['tmp_path']}/{$wanif}_routerv6\n";
3859
	$rtsoldscript .= "echo $2 > {$g['tmp_path']}/{$wanif}_defaultgwv6\n";
3860 c87d89ae Ermal
	$rtsoldscript .= "/usr/bin/logger -t rtsold \"Recieved RA specifying route \$2 for interface {$interface}({$wanif})\"\n";
3861 29f2f07a Ermal
	$rtsoldscript .= "if [ -f {$g['varrun_path']}/dhcp6c_{$wanif}.pid ]; then\n";
3862 b8ded125 Ermal
	$rtsoldscript .= "\t/bin/pkill -F {$g['varrun_path']}/dhcp6c_{$wanif}.pid\n";
3863 b90ae531 Ermal
	$rtsoldscript .= "\t/bin/sleep 1\n";
3864 29f2f07a Ermal
	$rtsoldscript .= "fi\n";
3865
	$rtsoldscript .= "/usr/local/sbin/dhcp6c -d -c {$g['varetc_path']}/dhcp6c_{$interface}.conf -p {$g['varrun_path']}/dhcp6c_{$wanif}.pid {$wanif}\n";
3866 fa4d4be6 Ermal
	$rtsoldscript .= "/usr/bin/logger -t rtsold \"Starting dhcp6 client for interface {$interface}({$wanif})\"\n";
3867 29f2f07a Ermal
	/* Add wide-dhcp6c shell script here. Because we can not pass a argument to it. */
3868
	if (!@file_put_contents("{$g['varetc_path']}/rtsold_{$wanif}_script.sh", $rtsoldscript)) {
3869
		printf("Error: cannot open rtsold_{$interface}_script.sh in interface_dhcpv6_configure() for writing.\n");
3870
		unset($rtsoldscript);
3871
		return 1;
3872
	}
3873
	unset($rtsoldscript);
3874
	@chmod("{$g['varetc_path']}/rtsold_{$wanif}_script.sh", 0755);
3875 d53a9a51 smos
3876 c65d3051 Seth Mos
	/* accept router advertisements for this interface */
3877 971de1f9 Renato Botelho
	set_single_sysctl("net.inet6.ip6.accept_rtadv", "1");
3878 49047fb4 smos
	log_error("Accept router advertisements on interface {$wanif} ");
3879 100c7be0 Seth Mos
	mwexec("/sbin/ifconfig {$wanif} inet6 accept_rtadv");
3880 61c4383d smos
3881 29f2f07a Ermal
	/* fire up rtsold for IPv6 RAs first, this backgrounds immediately. It will call dhcp6c */
3882 7fed35c8 Ermal
	if (isvalidpid("{$g['varrun_path']}/rtsold_{$wanif}.pid")) {
3883 29f2f07a Ermal
		killbypid("{$g['varrun_path']}/rtsold_{$wanif}.pid");
3884 7fed35c8 Ermal
		sleep(2);
3885
	}
3886 9b6010ff Ermal
	mwexec("/usr/sbin/rtsold -1 -p {$g['varrun_path']}/rtsold_{$wanif}.pid -O {$g['varetc_path']}/rtsold_{$wanif}_script.sh {$wanif}");
3887 82769dfe smos
3888 a8f5790a Renato Botelho
	/* NOTE: will be called from rtsold invoked script
3889 29f2f07a Ermal
	 * link_interface_to_track6($interface, "update");
3890
	 */
3891 b0059636 Ermal
3892 ed395640 Seth Mos
	return 0;
3893
}
3894
3895 f4dd8b4c N0YB
function DHCP6_Config_File_Advanced($interface, $wancfg, $wanif) {
3896
	global $g;
3897
3898
	$send_options = "";
3899
	if ($wancfg['adv_dhcp6_interface_statement_send_options'] != '') {
3900 ad451a81 Ermal LUÇI
		$options = explode(',', $wancfg['adv_dhcp6_interface_statement_send_options']);
3901 f4dd8b4c N0YB
		foreach ($options as $option) {
3902
			$send_options .= "\tsend " . trim($option) . ";\n";
3903
		}
3904
	}
3905
3906
	$request_options = "";
3907
	if ($wancfg['adv_dhcp6_interface_statement_request_options'] != '') {
3908 ad451a81 Ermal LUÇI
		$options = explode(',', $wancfg['adv_dhcp6_interface_statement_request_options']);
3909 f4dd8b4c N0YB
		foreach ($options as $option) {
3910
			$request_options .= "\trequest " . trim($option) . ";\n";
3911
		}
3912
	}
3913
3914
	$information_only = "";
3915 cf73302f Phil Davis
	if ($wancfg['adv_dhcp6_interface_statement_information_only_enable'] != '') {
3916 f4dd8b4c N0YB
		$information_only = "\tinformation-only;\n";
3917 cf73302f Phil Davis
	}
3918 f4dd8b4c N0YB
3919
	$script = "\tscript \"{$g['varetc_path']}/dhcp6c_{$interface}_script.sh\";\n";
3920 cf73302f Phil Davis
	if ($wancfg['adv_dhcp6_interface_statement_script'] != '') {
3921 f4dd8b4c N0YB
		$script = "\tscript \"{$wancfg['adv_dhcp6_interface_statement_script']}\";\n";
3922 cf73302f Phil Davis
	}
3923 f4dd8b4c N0YB
3924
	$interface_statement  = "interface";
3925
	$interface_statement .= " {$wanif}";
3926
	$interface_statement .= " {\n";
3927
	$interface_statement .= "$send_options";
3928
	$interface_statement .= "$request_options";
3929
	$interface_statement .= "$information_only";
3930
	$interface_statement .= "$script";
3931
	$interface_statement .= "};\n";
3932
3933
	$id_assoc_statement_address = "";
3934
	if ($wancfg['adv_dhcp6_id_assoc_statement_address_enable'] != '') {
3935
		$id_assoc_statement_address .= "id-assoc";
3936
		$id_assoc_statement_address .= " na";
3937 cf73302f Phil Davis
		if (is_numeric($wancfg['adv_dhcp6_id_assoc_statement_address_id'])) {
3938 f4dd8b4c N0YB
			$id_assoc_statement_address .= " {$wancfg['adv_dhcp6_id_assoc_statement_address_id']}";
3939 cf73302f Phil Davis
		}
3940 f4dd8b4c N0YB
		$id_assoc_statement_address .= " { ";
3941
3942 cf73302f Phil Davis
		if (($wancfg['adv_dhcp6_id_assoc_statement_address'] != '') &&
3943
		    (is_numeric($wancfg['adv_dhcp6_id_assoc_statement_address_pltime']) ||
3944
		    ($wancfg['adv_dhcp6_id_assoc_statement_address_pltime'] == 'infinity'))) {
3945 f4dd8b4c N0YB
			$id_assoc_statement_address .= "\n\taddress";
3946
			$id_assoc_statement_address .= " {$wancfg['adv_dhcp6_id_assoc_statement_address']}";
3947
			$id_assoc_statement_address .= " {$wancfg['adv_dhcp6_id_assoc_statement_address_pltime']}";
3948 cf73302f Phil Davis
			if ((is_numeric($wancfg['adv_dhcp6_id_assoc_statement_address_vltime'])) ||
3949
			    ($wancfg['adv_dhcp6_id_assoc_statement_address_vltime'] == 'infinity')) {
3950 f4dd8b4c N0YB
				$id_assoc_statement_address .= " {$wancfg['adv_dhcp6_id_assoc_statement_address_vltime']}";
3951 cf73302f Phil Davis
			}
3952 f4dd8b4c N0YB
			$id_assoc_statement_address .= ";\n";
3953
		}
3954
3955 6c07db48 Phil Davis
		$id_assoc_statement_address .= "};\n";
3956 f4dd8b4c N0YB
	}
3957
3958
	$id_assoc_statement_prefix = "";
3959
	if ($wancfg['adv_dhcp6_id_assoc_statement_prefix_enable'] != '') {
3960
		$id_assoc_statement_prefix .= "id-assoc";
3961
		$id_assoc_statement_prefix .= " pd";
3962 cf73302f Phil Davis
		if (is_numeric($wancfg['adv_dhcp6_id_assoc_statement_prefix_id'])) {
3963 f4dd8b4c N0YB
			$id_assoc_statement_prefix .= " {$wancfg['adv_dhcp6_id_assoc_statement_prefix_id']}";
3964 cf73302f Phil Davis
		}
3965 f4dd8b4c N0YB
		$id_assoc_statement_prefix .= " { ";
3966
3967 cf73302f Phil Davis
		if (($wancfg['adv_dhcp6_id_assoc_statement_prefix'] != '') &&
3968
		    (is_numeric($wancfg['adv_dhcp6_id_assoc_statement_prefix_pltime']) ||
3969 086cf944 Phil Davis
		    ($wancfg['adv_dhcp6_id_assoc_statement_prefix_pltime'] == 'infinity'))) {
3970 f4dd8b4c N0YB
			$id_assoc_statement_prefix .= "\n\tprefix";
3971
			$id_assoc_statement_prefix .= " {$wancfg['adv_dhcp6_id_assoc_statement_prefix']}";
3972
			$id_assoc_statement_prefix .= " {$wancfg['adv_dhcp6_id_assoc_statement_prefix_pltime']}";
3973 cf73302f Phil Davis
			if ((is_numeric($wancfg['adv_dhcp6_id_assoc_statement_prefix_vltime'])) ||
3974
			    ($wancfg['adv_dhcp6_id_assoc_statement_prefix_vltime'] == 'infinity')) {
3975 f4dd8b4c N0YB
				$id_assoc_statement_prefix .= " {$wancfg['adv_dhcp6_id_assoc_statement_prefix_vltime']}";
3976 cf73302f Phil Davis
			}
3977 f4dd8b4c N0YB
			$id_assoc_statement_prefix .= ";";
3978
		}
3979
3980
		if (is_numeric($wancfg['adv_dhcp6_prefix_interface_statement_sla_id'])) {
3981
			$id_assoc_statement_prefix .= "\n\tprefix-interface";
3982
			$id_assoc_statement_prefix .= " {$wanif}";
3983
			$id_assoc_statement_prefix .= " {\n";
3984
			$id_assoc_statement_prefix .= "\t\tsla-id {$wancfg['adv_dhcp6_prefix_interface_statement_sla_id']};\n";
3985 cf73302f Phil Davis
			if (($wancfg['adv_dhcp6_prefix_interface_statement_sla_len'] >= 0) &&
3986
			    ($wancfg['adv_dhcp6_prefix_interface_statement_sla_len'] <= 128)) {
3987 6990ad35 Phil Davis
				$id_assoc_statement_prefix .= "\t\tsla-len {$wancfg['adv_dhcp6_prefix_interface_statement_sla_len']};\n";
3988 cf73302f Phil Davis
			}
3989 f4dd8b4c N0YB
			$id_assoc_statement_prefix .= "\t};";
3990
		}
3991
3992 cf73302f Phil Davis
		if (($wancfg['adv_dhcp6_id_assoc_statement_prefix'] != '') ||
3993 086cf944 Phil Davis
		    (is_numeric($wancfg['adv_dhcp6_prefix_interface_statement_sla_id']))) {
3994 f4dd8b4c N0YB
			$id_assoc_statement_prefix .= "\n";
3995
		}
3996
3997 6c07db48 Phil Davis
		$id_assoc_statement_prefix .= "};\n";
3998 f4dd8b4c N0YB
	}
3999
4000
	$authentication_statement = "";
4001 cf73302f Phil Davis
	if (($wancfg['adv_dhcp6_authentication_statement_authname'] != '') &&
4002
	    ($wancfg['adv_dhcp6_authentication_statement_protocol'] == 'delayed')) {
4003 f4dd8b4c N0YB
		$authentication_statement .= "authentication";
4004
		$authentication_statement .= " {$wancfg['adv_dhcp6_authentication_statement_authname']}";
4005
		$authentication_statement .= " {\n";
4006
		$authentication_statement .= "\tprotocol {$wancfg['adv_dhcp6_authentication_statement_protocol']};\n";
4007 cf73302f Phil Davis
		if (preg_match("/(hmac(-)?md5)||(HMAC(-)?MD5)/", $wancfg['adv_dhcp6_authentication_statement_algorithm'])) {
4008 f4dd8b4c N0YB
			$authentication_statement .= "\talgorithm {$wancfg['adv_dhcp6_authentication_statement_algorithm']};\n";
4009 cf73302f Phil Davis
		}
4010
		if ($wancfg['adv_dhcp6_authentication_statement_rdm'] == 'monocounter') {
4011 f4dd8b4c N0YB
			$authentication_statement .= "\trdm {$wancfg['adv_dhcp6_authentication_statement_rdm']};\n";
4012 cf73302f Phil Davis
		}
4013 f4dd8b4c N0YB
		$authentication_statement .= "};\n";
4014
	}
4015
4016
	$key_info_statement = "";
4017 cf73302f Phil Davis
	if (($wancfg['adv_dhcp6_key_info_statement_keyname'] != '') &&
4018
	    ($wancfg['adv_dhcp6_key_info_statement_realm'] != '') &&
4019
	    (is_numeric($wancfg['adv_dhcp6_key_info_statement_keyid'])) &&
4020
	    ($wancfg['adv_dhcp6_key_info_statement_secret'] != '')) {
4021 f4dd8b4c N0YB
		$key_info_statement .= "keyinfo";
4022
		$key_info_statement .= " {$wancfg['adv_dhcp6_key_info_statement_keyname']}";
4023
		$key_info_statement .= " {\n";
4024
		$key_info_statement .= "\trealm \"{$wancfg['adv_dhcp6_key_info_statement_realm']}\";\n";
4025
		$key_info_statement .= "\tkeyid {$wancfg['adv_dhcp6_key_info_statement_keyid']};\n";
4026
		$key_info_statement .= "\tsecret \"{$wancfg['adv_dhcp6_key_info_statement_secret']}\";\n";
4027 cf73302f Phil Davis
		if (preg_match("/((([0-9]{4}-)?[0-9]{2}[0-9]{2} )?[0-9]{2}:[0-9]{2})||(foreever)/", $wancfg['adv_dhcp6_key_info_statement_expire'])) {
4028 f4dd8b4c N0YB
			$key_info_statement .= "\texpire \"{$wancfg['adv_dhcp6_key_info_statement_expire']}\";\n";
4029 cf73302f Phil Davis
		}
4030 f4dd8b4c N0YB
		$key_info_statement .= "};\n";
4031
	}
4032
4033
	$dhcp6cconf  = $interface_statement;
4034
	$dhcp6cconf .= $id_assoc_statement_address;
4035
	$dhcp6cconf .= $id_assoc_statement_prefix;
4036
	$dhcp6cconf .= $authentication_statement;
4037
	$dhcp6cconf .= $key_info_statement;
4038
4039
	$dhcp6cconf = DHCP6_Config_File_Substitutions($wancfg, $wanif, $dhcp6cconf);
4040
4041
	return $dhcp6cconf;
4042
}
4043
4044
4045
function DHCP6_Config_File_Override($wancfg, $wanif) {
4046
4047 f31052c7 k-paulius
	$dhcp6cconf = @file_get_contents($wancfg['adv_dhcp6_config_file_override_path']);
4048 f4dd8b4c N0YB
4049 f31052c7 k-paulius
	if ($dhcp6cconf === false) {
4050 fc1f2003 k-paulius
		log_error("Error: cannot open {$wancfg['adv_dhcp6_config_file_override_path']} in DHCP6_Config_File_Override() for reading.\n");
4051 f31052c7 k-paulius
		return '';
4052
	} else {
4053 fc1f2003 k-paulius
		return DHCP6_Config_File_Substitutions($wancfg, $wanif, $dhcp6cconf);;
4054 f31052c7 k-paulius
	}
4055 f4dd8b4c N0YB
}
4056
4057
4058
function DHCP6_Config_File_Substitutions($wancfg, $wanif, $dhcp6cconf) {
4059
4060
	$dhcp6cconf = DHCP_Config_File_Substitutions($wancfg, $wanif, $dhcp6cconf);
4061
4062
	return $dhcp6cconf;
4063
}
4064
4065
4066 8103bd1e Seth Mos
function interface_dhcp_configure($interface = "wan") {
4067 ed395640 Seth Mos
	global $config, $g;
4068
4069
	$wancfg = $config['interfaces'][$interface];
4070
	$wanif = $wancfg['if'];
4071 cf73302f Phil Davis
	if (empty($wancfg)) {
4072 df9e93f0 Ermal
		$wancfg = array();
4073 cf73302f Phil Davis
	}
4074 5b237745 Scott Ullrich
4075 0311dbd5 Scott Ullrich
	/* generate dhclient_wan.conf */
4076 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
4077 5b237745 Scott Ullrich
	if (!$fd) {
4078 905ea336 Phil Davis
		printf(printf(gettext("Error: cannot open dhclient_%s.conf in interface_dhcp_configure() for writing.%s"), $interface, "\n"));
4079 5b237745 Scott Ullrich
		return 1;
4080
	}
4081 eb772abd Scott Ullrich
4082 2305d4c5 Scott Ullrich
	if ($wancfg['dhcphostname']) {
4083
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
4084
		$dhclientconf_hostname .= "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
4085
	} else {
4086
		$dhclientconf_hostname = "";
4087
	}
4088
4089 85a5da13 Ermal Luçi
	$wanif = get_real_interface($interface);
4090 df9e93f0 Ermal
	if (empty($wanif)) {
4091 07e40c1f Carlos Eduardo Ramos
		log_error(sprintf(gettext("Invalid interface \"%s\" in interface_dhcp_configure()"), $interface));
4092 c1cc447c gnhb
		return 0;
4093 3a906378 gnhb
	}
4094 1c3ddd9e Renato Botelho
	$dhclientconf = "";
4095 be45aa79 Renato Botelho
4096 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
4097 67ee1ec5 Ermal Luçi
interface "{$wanif}" {
4098 76d3b9a3 Chris Buechler
timeout 60;
4099 88810240 smos
retry 15;
4100 ce69a638 Scott Ullrich
select-timeout 0;
4101
initial-interval 1;
4102 2305d4c5 Scott Ullrich
	{$dhclientconf_hostname}
4103
	script "/sbin/dhclient-script";
4104 57c83fd6 jim-p
EOD;
4105
4106 cf73302f Phil Davis
	if (is_ipaddrv4($wancfg['dhcprejectfrom'])) {
4107
		$dhclientconf .= <<<EOD
4108 57c83fd6 jim-p
4109
	reject {$wancfg['dhcprejectfrom']};
4110
EOD;
4111 cf73302f Phil Davis
	}
4112 57c83fd6 jim-p
	$dhclientconf .= <<<EOD
4113
4114 5b237745 Scott Ullrich
}
4115
4116
EOD;
4117
4118 f4dd8b4c N0YB
	// DHCP Config File Advanced
4119 cf73302f Phil Davis
	if ($wancfg['adv_dhcp_config_advanced']) {
4120
		$dhclientconf = DHCP_Config_File_Advanced($interface, $wancfg, $wanif);
4121
	}
4122 f4dd8b4c N0YB
4123 cf73302f Phil Davis
	if (is_ipaddr($wancfg['alias-address'])) {
4124
		$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
4125
		$dhclientconf .= <<<EOD
4126 bc40d758 Seth Mos
alias {
4127 6c07db48 Phil Davis
	interface "{$wanif}";
4128 bc40d758 Seth Mos
	fixed-address {$wancfg['alias-address']};
4129
	option subnet-mask {$subnetmask};
4130
}
4131
4132
EOD;
4133 cf73302f Phil Davis
	}
4134 f4dd8b4c N0YB
4135
	// DHCP Config File Override
4136 cf73302f Phil Davis
	if ($wancfg['adv_dhcp_config_file_override']) {
4137
		$dhclientconf = DHCP_Config_File_Override($wancfg, $wanif);
4138
	}
4139 f4dd8b4c N0YB
4140 5b237745 Scott Ullrich
	fwrite($fd, $dhclientconf);
4141
	fclose($fd);
4142 eb772abd Scott Ullrich
4143 d7147b1c Scott Ullrich
	/* bring wan interface up before starting dhclient */
4144 cf73302f Phil Davis
	if ($wanif) {
4145 3a906378 gnhb
		interfaces_bring_up($wanif);
4146 cf73302f Phil Davis
	} else {
4147 07e40c1f Carlos Eduardo Ramos
		log_error(printf(gettext("Could not bring up %s interface in interface_dhcp_configure()"), $wanif));
4148 cf73302f Phil Davis
	}
4149 eacc8c14 Scott Ullrich
4150 60ef0911 Renato Botelho
	/* Make sure dhclient is not running */
4151
	kill_dhclient_process($wanif);
4152
4153 7149c4e7 Seth Mos
	/* fire up dhclient */
4154 15d15c7f bcyrill
	mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif} > {$g['tmp_path']}/{$wanif}_output 2> {$g['tmp_path']}/{$wanif}_error_output");
4155 0119d2f7 Scott Ullrich
4156 5b237745 Scott Ullrich
	return 0;
4157
}
4158
4159 f4dd8b4c N0YB
function DHCP_Config_File_Advanced($interface, $wancfg, $wanif) {
4160
4161
	$hostname = "";
4162
	if ($wancfg['dhcphostname'] != '') {
4163 672d7e7b N0YB
		$hostname = "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
4164 f4dd8b4c N0YB
	}
4165
4166
	/* DHCP Protocol Timings */
4167
	$protocol_timings = array ('adv_dhcp_pt_timeout' => "timeout", 'adv_dhcp_pt_retry' => "retry", 'adv_dhcp_pt_select_timeout' => "select-timeout", 'adv_dhcp_pt_reboot' => "reboot", 'adv_dhcp_pt_backoff_cutoff' => "backoff-cutoff", 'adv_dhcp_pt_initial_interval' => "initial-interval");
4168
	foreach ($protocol_timings as $Protocol_Timing => $PT_Name) {
4169
		$pt_variable = "{$Protocol_Timing}";
4170
		${$pt_variable} = "";
4171
		if ($wancfg[$Protocol_Timing] != "") {
4172
			${$pt_variable} = "{$PT_Name} {$wancfg[$Protocol_Timing]};\n";
4173
		}
4174
	}
4175
4176
	$send_options = "";
4177
	if ($wancfg['adv_dhcp_send_options'] != '') {
4178 ad451a81 Ermal LUÇI
		$options = explode(',', $wancfg['adv_dhcp_send_options']);
4179 f4dd8b4c N0YB
		foreach ($options as $option) {
4180
			$send_options .= "\tsend " . trim($option) . ";\n";
4181
		}
4182
	}
4183
4184
	$request_options = "";
4185
	if ($wancfg['adv_dhcp_request_options'] != '') {
4186
		$request_options = "\trequest {$wancfg['adv_dhcp_request_options']};\n";
4187
	}
4188
4189
	$required_options = "";
4190
	if ($wancfg['adv_dhcp_required_options'] != '') {
4191 f669800c N0YB
		$required_options = "\trequire {$wancfg['adv_dhcp_required_options']};\n";
4192 f4dd8b4c N0YB
	}
4193
4194
	$option_modifiers = "";
4195
	if ($wancfg['adv_dhcp_option_modifiers'] != '') {
4196 ad451a81 Ermal LUÇI
		$modifiers = explode(',', $wancfg['adv_dhcp_option_modifiers']);
4197 f4dd8b4c N0YB
		foreach ($modifiers as $modifier) {
4198
			$option_modifiers .= "\t" . trim($modifier) . ";\n";
4199
		}
4200
	}
4201
4202 cf73302f Phil Davis
	$dhclientconf  = "interface \"{$wanif}\" {\n";
4203
	$dhclientconf .= "\n";
4204
	$dhclientconf .= "# DHCP Protocol Timing Values\n";
4205
	$dhclientconf .= "{$adv_dhcp_pt_timeout}";
4206
	$dhclientconf .= "{$adv_dhcp_pt_retry}";
4207
	$dhclientconf .= "{$adv_dhcp_pt_select_timeout}";
4208
	$dhclientconf .= "{$adv_dhcp_pt_reboot}";
4209
	$dhclientconf .= "{$adv_dhcp_pt_backoff_cutoff}";
4210
	$dhclientconf .= "{$adv_dhcp_pt_initial_interval}";
4211
	$dhclientconf .= "\n";
4212
	$dhclientconf .= "# DHCP Protocol Options\n";
4213
	$dhclientconf .= "{$hostname}";
4214
	$dhclientconf .= "{$send_options}";
4215
	$dhclientconf .= "{$request_options}";
4216
	$dhclientconf .= "{$required_options}";
4217
	$dhclientconf .= "{$option_modifiers}";
4218
	$dhclientconf .= "\n";
4219
	$dhclientconf .= "\tscript \"/sbin/dhclient-script\";\n";
4220
	$dhclientconf .= "}\n";
4221 f4dd8b4c N0YB
4222
	$dhclientconf = DHCP_Config_File_Substitutions($wancfg, $wanif, $dhclientconf);
4223
4224
	return $dhclientconf;
4225
}
4226
4227
4228
function DHCP_Config_File_Override($wancfg, $wanif) {
4229
4230 9329ec08 k-paulius
	$dhclientconf = @file_get_contents($wancfg['adv_dhcp_config_file_override_path']);
4231 f4dd8b4c N0YB
4232 9329ec08 k-paulius
	if ($dhclientconf === false) {
4233
		log_error("Error: cannot open {$wancfg['adv_dhcp_config_file_override_path']} in DHCP_Config_File_Override() for reading.\n");
4234
		return '';
4235
	} else {
4236
		return DHCP_Config_File_Substitutions($wancfg, $wanif, $dhclientconf);
4237
	}
4238 f4dd8b4c N0YB
}
4239
4240
4241
function DHCP_Config_File_Substitutions($wancfg, $wanif, $dhclientconf) {
4242
4243
	/* Apply Interface Substitutions */
4244
	$dhclientconf = str_replace("{interface}", "{$wanif}", $dhclientconf);
4245
4246
	/* Apply Hostname Substitutions */
4247
	$dhclientconf = str_replace("{hostname}", $wancfg['dhcphostname'], $dhclientconf);
4248
4249
	/* Arrays of MAC Address Types, Cases, Delimiters */
4250
	/* ASCII or HEX, Upper or Lower Case, Various Delimiters (none, space, colon, hyphen, period) */
4251
	$various_mac_types      = array("mac_addr_ascii", "mac_addr_hex");
4252
	$various_mac_cases      = array("U", "L");
4253
	$various_mac_delimiters = array("", " ", ":", "-", ".");
4254
4255
	/* Apply MAC Address Substitutions */
4256
	foreach ($various_mac_types as $various_mac_type) {
4257
		foreach ($various_mac_cases as $various_mac_case) {
4258
			foreach ($various_mac_delimiters as $various_mac_delimiter) {
4259
4260
				$res = stripos($dhclientconf, $various_mac_type . $various_mac_case . $various_mac_delimiter);
4261
				if ($res !== false) {
4262
4263 f416763b Phil Davis
					/* Get MAC Address as ASCII String With Colon (:) delimiters */
4264 cf73302f Phil Davis
					if ("$various_mac_case" == "U") {
4265
						$dhcpclientconf_mac = strtoupper(get_interface_mac($wanif));
4266
					}
4267
					if ("$various_mac_case" == "L") {
4268
						$dhcpclientconf_mac = strtolower(get_interface_mac($wanif));
4269
					}
4270 f4dd8b4c N0YB
4271
					if ("$various_mac_type" == "mac_addr_hex") {
4272
						/* Convert MAC ascii string to HEX with colon (:) delimiters. */
4273
						$dhcpclientconf_mac = str_replace(":", "", $dhcpclientconf_mac);
4274
						$dhcpclientconf_mac_hex = "";
4275
						$delimiter = "";
4276 cf73302f Phil Davis
						for ($i = 0; $i < strlen($dhcpclientconf_mac); $i++) {
4277 f4dd8b4c N0YB
							$dhcpclientconf_mac_hex .= $delimiter. bin2hex($dhcpclientconf_mac[$i]);
4278
							$delimiter = ":";
4279
						}
4280
						$dhcpclientconf_mac = $dhcpclientconf_mac_hex;
4281
					}
4282
4283
					/* MAC Address Delimiter Substitutions */
4284
					$dhcpclientconf_mac = str_replace(":", $various_mac_delimiter, $dhcpclientconf_mac);
4285
4286
					/* Apply MAC Address Substitutions */
4287
					$dhclientconf = str_replace("{" . $various_mac_type . $various_mac_case . $various_mac_delimiter . "}", $dhcpclientconf_mac, $dhclientconf);
4288
				}
4289
			}
4290
		}
4291
	}
4292
4293
	return $dhclientconf;
4294
}
4295
4296 42753d25 Ermal Lu?i
function interfaces_group_setup() {
4297
	global $config;
4298
4299 cf73302f Phil Davis
	if (!is_array($config['ifgroups']['ifgroupentry'])) {
4300 42753d25 Ermal Lu?i
		return;
4301 cf73302f Phil Davis
	}
4302 42753d25 Ermal Lu?i
4303 cf73302f Phil Davis
	foreach ($config['ifgroups']['ifgroupentry'] as $groupar) {
4304 42753d25 Ermal Lu?i
		interface_group_setup($groupar);
4305 cf73302f Phil Davis
	}
4306 42753d25 Ermal Lu?i
4307
	return;
4308
}
4309
4310 abcb2bed Ermal Lu?i
function interface_group_setup(&$groupname /* The parameter is an array */) {
4311 42753d25 Ermal Lu?i
	global $config;
4312
4313 cf73302f Phil Davis
	if (!is_array($groupname)) {
4314 42753d25 Ermal Lu?i
		return;
4315 cf73302f Phil Davis
	}
4316 42753d25 Ermal Lu?i
	$members = explode(" ", $groupname['members']);
4317 cf73302f Phil Davis
	foreach ($members as $ifs) {
4318 42753d25 Ermal Lu?i
		$realif = get_real_interface($ifs);
4319 cf73302f Phil Davis
		if ($realif && does_interface_exist($realif)) {
4320 42753d25 Ermal Lu?i
			mwexec("/sbin/ifconfig {$realif} group {$groupname['ifname']}");
4321 cf73302f Phil Davis
		}
4322 42753d25 Ermal Lu?i
	}
4323
4324
	return;
4325
}
4326 48f23632 Ermal
4327 06182467 Renato Botelho
function is_interface_group($if) {
4328
	global $config;
4329
4330 cf73302f Phil Davis
	if (is_array($config['ifgroups']['ifgroupentry'])) {
4331 06182467 Renato Botelho
		foreach ($config['ifgroups']['ifgroupentry'] as $groupentry) {
4332 cf73302f Phil Davis
			if ($groupentry['ifname'] === $if) {
4333 06182467 Renato Botelho
				return true;
4334 cf73302f Phil Davis
			}
4335 06182467 Renato Botelho
		}
4336 cf73302f Phil Davis
	}
4337 06182467 Renato Botelho
4338
	return false;
4339
}
4340
4341 48f23632 Ermal
function interface_group_add_member($interface, $groupname) {
4342 ed62880b Ermal
	$interface = get_real_interface($interface);
4343 cf73302f Phil Davis
	if (does_interface_exist($interface)) {
4344 67de15fc Renato Botelho
		mwexec("/sbin/ifconfig {$interface} group " . escapeshellarg($groupname), true);
4345 cf73302f Phil Davis
	}
4346 48f23632 Ermal
}
4347 be45aa79 Renato Botelho
4348 e8910ad4 Ermal Lu?i
/* COMPAT Function */
4349 afb2de1b Ermal Lu?i
function convert_friendly_interface_to_real_interface_name($interface) {
4350
	return get_real_interface($interface);
4351
}
4352
4353 e8910ad4 Ermal Lu?i
/* COMPAT Function */
4354 eba938e3 Scott Ullrich
function get_real_wan_interface($interface = "wan") {
4355 abb31ea4 Ermal Luçi
	return get_real_interface($interface);
4356
}
4357 afb2de1b Ermal Lu?i
4358 e8910ad4 Ermal Lu?i
/* COMPAT Function */
4359 eba938e3 Scott Ullrich
function get_current_wan_address($interface = "wan") {
4360 abb31ea4 Ermal Luçi
	return get_interface_ip($interface);
4361
}
4362
4363 afb2de1b Ermal Lu?i
/*
4364
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
4365
 */
4366 5e0a3256 Ermal LUÇI
function convert_real_interface_to_friendly_interface_name($interface = "wan", $checkparent = false) {
4367 7061ba0f Renato Botelho
	global $config;
4368 afb2de1b Ermal Lu?i
4369 5063f1df Ermal
	if (stripos($interface, "_vip")) {
4370
		foreach ($config['virtualip']['vip'] as $counter => $vip) {
4371 6c07db48 Phil Davis
			if ($vip['mode'] == "carp") {
4372 cf73302f Phil Davis
				if ($interface == "{$vip['interface']}_vip{$vip['vhid']}") {
4373
					return $vip['interface'];
4374
				}
4375 5063f1df Ermal
			}
4376
		}
4377
	}
4378
4379 7061ba0f Renato Botelho
	/* XXX: For speed reasons reference directly the interface array */
4380 74e1e658 jim-p
	$ifdescrs = &$config['interfaces'];
4381 7061ba0f Renato Botelho
	//$ifdescrs = get_configured_interface_list(false, true);
4382 afb2de1b Ermal Lu?i
4383 7061ba0f Renato Botelho
	foreach ($ifdescrs as $if => $ifname) {
4384 cf73302f Phil Davis
		if ($if == $interface || $ifname['if'] == $interface) {
4385 7061ba0f Renato Botelho
			return $if;
4386 cf73302f Phil Davis
		}
4387 afb2de1b Ermal Lu?i
4388 cf73302f Phil Davis
		if (get_real_interface($if) == $interface) {
4389 7061ba0f Renato Botelho
			return $if;
4390 cf73302f Phil Davis
		}
4391 af637766 Erik Fonnesbeck
4392 cf73302f Phil Davis
		if ($checkparent == false) {
4393 5e0a3256 Ermal LUÇI
			continue;
4394 cf73302f Phil Davis
		}
4395 5e0a3256 Ermal LUÇI
4396 d6101e23 Ermal
		$int = get_parent_interface($if, true);
4397
		if (is_array($int)) {
4398
			foreach ($int as $iface) {
4399 cf73302f Phil Davis
				if ($iface == $interface) {
4400 d6101e23 Ermal
					return $if;
4401 cf73302f Phil Davis
				}
4402 d6101e23 Ermal
			}
4403
		}
4404 7061ba0f Renato Botelho
	}
4405 1d66a364 Ermal
4406 cf73302f Phil Davis
	if ($interface == "enc0") {
4407 5ebb2954 Ermal
		return 'IPsec';
4408 cf73302f Phil Davis
	}
4409 afb2de1b Ermal Lu?i
}
4410
4411
/* attempt to resolve interface to friendly descr */
4412
function convert_friendly_interface_to_friendly_descr($interface) {
4413 1c3ddd9e Renato Botelho
	global $config;
4414 afb2de1b Ermal Lu?i
4415 1c3ddd9e Renato Botelho
	switch ($interface) {
4416 cf73302f Phil Davis
		case "l2tp":
4417
			$ifdesc = "L2TP";
4418
			break;
4419
		case "pptp":
4420
			$ifdesc = "PPTP";
4421 57c52d45 Erik Fonnesbeck
			break;
4422 cf73302f Phil Davis
		case "pppoe":
4423
			$ifdesc = "PPPoE";
4424
			break;
4425
		case "openvpn":
4426
			$ifdesc = "OpenVPN";
4427
			break;
4428
		case "enc0":
4429
		case "ipsec":
4430
		case "IPsec":
4431
			$ifdesc = "IPsec";
4432
			break;
4433
		default:
4434
			if (isset($config['interfaces'][$interface])) {
4435
				if (empty($config['interfaces'][$interface]['descr'])) {
4436
					$ifdesc = strtoupper($interface);
4437
				} else {
4438
					$ifdesc = strtoupper($config['interfaces'][$interface]['descr']);
4439
				}
4440
				break;
4441
			} else if (substr($interface, 0, 4) == '_vip') {
4442
				if (is_array($config['virtualip']['vip'])) {
4443
					foreach ($config['virtualip']['vip'] as $counter => $vip) {
4444 6c07db48 Phil Davis
						if ($vip['mode'] == "carp") {
4445 cf73302f Phil Davis
							if ($interface == "{$vip['interface']}_vip{$vip['vhid']}") {
4446
								return "{$vip['subnet']} - {$vip['descr']}";
4447
							}
4448
						}
4449
					}
4450
				}
4451
			} else if (substr($interface, 0, 5) == '_lloc') {
4452
				return get_interface_linklocal($interface);
4453
			} else {
4454
				/* if list */
4455
				$ifdescrs = get_configured_interface_with_descr(false, true);
4456
				foreach ($ifdescrs as $if => $ifname) {
4457
					if ($if == $interface || $ifname == $interface) {
4458
						return $ifname;
4459 f2dd61a7 Ermal
					}
4460
				}
4461
			}
4462 cf73302f Phil Davis
			break;
4463 1c3ddd9e Renato Botelho
	}
4464 afb2de1b Ermal Lu?i
4465 1c3ddd9e Renato Botelho
	return $ifdesc;
4466 afb2de1b Ermal Lu?i
}
4467
4468
function convert_real_interface_to_friendly_descr($interface) {
4469
4470 1c3ddd9e Renato Botelho
	$ifdesc = convert_real_interface_to_friendly_interface_name("{$interface}");
4471 afb2de1b Ermal Lu?i
4472 cf73302f Phil Davis
	if (!empty($ifdesc)) {
4473 a1b5f07b Ermal
		return convert_friendly_interface_to_friendly_descr($ifdesc);
4474 cf73302f Phil Davis
	}
4475 afb2de1b Ermal Lu?i
4476 1c3ddd9e Renato Botelho
	return $interface;
4477 afb2de1b Ermal Lu?i
}
4478
4479 532b0fb8 Ermal Lu?i
/*
4480 d5dfcb52 gnhb
 *  get_parent_interface($interface):
4481 20cb9803 gnhb
 *			--returns the (real or virtual) parent interface(s) array for a given interface friendly name (i.e. wan)
4482
 *				or virtual interface (i.e. vlan)
4483
 *				(We need array because MLPPP and bridge interfaces have more than one parent.)
4484
 *			-- returns $interface passed in if $interface parent is not found
4485
 *			-- returns empty array if an invalid interface is passed
4486
 *	(Only handles ppps and vlans now.)
4487 532b0fb8 Ermal Lu?i
 */
4488 d6101e23 Ermal
function get_parent_interface($interface, $avoidrecurse = false) {
4489 d5dfcb52 gnhb
	global $config;
4490 532b0fb8 Ermal Lu?i
4491 20cb9803 gnhb
	$parents = array();
4492
	//Check that we got a valid interface passed
4493
	$realif = get_real_interface($interface);
4494 cf73302f Phil Davis
	if ($realif == NULL) {
4495 20cb9803 gnhb
		return $parents;
4496 cf73302f Phil Davis
	}
4497 20cb9803 gnhb
4498
	// If we got a real interface, find it's friendly assigned name
4499 cf73302f Phil Davis
	if ($interface == $realif && $avoidrecurse == false) {
4500 ebcbc110 Ermal
		$interface = convert_real_interface_to_friendly_interface_name($interface);
4501 cf73302f Phil Davis
	}
4502 be45aa79 Renato Botelho
4503 20cb9803 gnhb
	if (!empty($interface) && isset($config['interfaces'][$interface])) {
4504
		$ifcfg = $config['interfaces'][$interface];
4505
		switch ($ifcfg['ipaddr']) {
4506
			case "ppp":
4507
			case "pppoe":
4508
			case "pptp":
4509
			case "l2tp":
4510 cf73302f Phil Davis
				if (empty($parents)) {
4511
					if (is_array($config['ppps']['ppp'])) {
4512 20cb9803 gnhb
						foreach ($config['ppps']['ppp'] as $pppidx => $ppp) {
4513 02b8bfae Renato Botelho
							if ($ifcfg['if'] == $ppp['if']) {
4514 20cb9803 gnhb
								$ports = explode(',', $ppp['ports']);
4515 cf73302f Phil Davis
								foreach ($ports as $pid => $parent_if) {
4516 20cb9803 gnhb
									$parents[$pid] = get_real_interface($parent_if);
4517 cf73302f Phil Davis
								}
4518 20cb9803 gnhb
								break;
4519
							}
4520
						}
4521 cf73302f Phil Davis
					}
4522
				}
4523 20cb9803 gnhb
				break;
4524
			case "dhcp":
4525
			case "static":
4526
			default:
4527
				// Handle _vlans
4528 bc73d959 Ermal LUÇI
				if (strpos($realif, '_vlan') !== FALSE) {
4529
					if (is_array($config['vlans']['vlan'])) {
4530
						foreach ($config['vlans']['vlan'] as $vlanidx => $vlan) {
4531
							if ($ifcfg['if'] == $vlan['vlanif']) {
4532 20cb9803 gnhb
								$parents[0] = $vlan['if'];
4533
								break;
4534
							}
4535 bc73d959 Ermal LUÇI
						}
4536
					}
4537
				}
4538 20cb9803 gnhb
				break;
4539 3e5d0d1d Ermal
		}
4540
	}
4541 be45aa79 Renato Botelho
4542 cf73302f Phil Davis
	if (empty($parents)) {
4543 20cb9803 gnhb
		$parents[0] = $realif;
4544 cf73302f Phil Davis
	}
4545 be45aa79 Renato Botelho
4546 20cb9803 gnhb
	return $parents;
4547 532b0fb8 Ermal Lu?i
}
4548
4549 263e2b7e Erik Fonnesbeck
function interface_is_wireless_clone($wlif) {
4550 cf73302f Phil Davis
	if (!stristr($wlif, "_wlan")) {
4551 263e2b7e Erik Fonnesbeck
		return false;
4552
	} else {
4553
		return true;
4554
	}
4555
}
4556
4557 1d072761 Erik Fonnesbeck
function interface_get_wireless_base($wlif) {
4558 cf73302f Phil Davis
	if (!stristr($wlif, "_wlan")) {
4559 34808d4e Erik Fonnesbeck
		return $wlif;
4560
	} else {
4561
		return substr($wlif, 0, stripos($wlif, "_wlan"));
4562
	}
4563
}
4564
4565 1d072761 Erik Fonnesbeck
function interface_get_wireless_clone($wlif) {
4566 cf73302f Phil Davis
	if (!stristr($wlif, "_wlan")) {
4567 34808d4e Erik Fonnesbeck
		return $wlif . "_wlan0";
4568
	} else {
4569
		return $wlif;
4570
	}
4571
}
4572
4573 33e71f10 jim-p
function get_real_interface($interface = "wan", $family = "all", $realv6iface = false, $flush = true) {
4574 ee3576dd Ermal
	global $config, $g;
4575 cfc707f7 Scott Ullrich
4576 521cfa2f Ermal Lu?i
	$wanif = NULL;
4577 c515ea57 Scott Ullrich
4578 67ee1ec5 Ermal Luçi
	switch ($interface) {
4579 cf73302f Phil Davis
		case "l2tp":
4580
			$wanif = "l2tp";
4581 89f171b0 Ermal LUÇI
			break;
4582 cf73302f Phil Davis
		case "pptp":
4583
			$wanif = "pptp";
4584 6d5446a2 Ermal
			break;
4585 cf73302f Phil Davis
		case "pppoe":
4586
			$wanif = "pppoe";
4587 6d5446a2 Ermal
			break;
4588 cf73302f Phil Davis
		case "openvpn":
4589
			$wanif = "openvpn";
4590
			break;
4591
		case "ipsec":
4592
		case "enc0":
4593
			$wanif = "enc0";
4594
			break;
4595
		case "ppp":
4596
			$wanif = "ppp";
4597
			break;
4598
		default:
4599
			if (substr($interface, 0, 4) == '_vip') {
4600
				$wanif = get_configured_carp_interface_list($interface, '', 'iface');
4601
				if (!empty($wanif)) {
4602
					$wanif = get_real_interface($wanif, $family);
4603
				}
4604
				break;
4605
			} else if (substr($interface, 0, 5) == '_lloc') {
4606
				$interface = substr($interface, 5);
4607
			} else if (does_interface_exist($interface, $flush)) {
4608
				/*
4609
				 * If a real interface was already passed simply
4610
				 * pass the real interface back.  This encourages
4611
				 * the usage of this function in more cases so that
4612
				 * we can combine logic for more flexibility.
4613
				 */
4614
				$wanif = $interface;
4615
				break;
4616
			}
4617 568b1358 Scott Ullrich
4618 cf73302f Phil Davis
			if (empty($config['interfaces'][$interface])) {
4619 b6c1f22f Ermal
				break;
4620 cf73302f Phil Davis
			}
4621
4622
			$cfg = &$config['interfaces'][$interface];
4623
4624
			if ($family == "inet6") {
4625
				switch ($cfg['ipaddrv6']) {
4626
					case "6rd":
4627
					case "6to4":
4628
						$wanif = "{$interface}_stf";
4629
						break;
4630
					case 'pppoe':
4631
					case 'ppp':
4632
					case 'l2tp':
4633
					case 'pptp':
4634 086cf944 Phil Davis
						if (is_array($cfg['wireless']) || preg_match($g['wireless_regex'], $cfg['if'])) {
4635 cf73302f Phil Davis
							$wanif = interface_get_wireless_clone($cfg['if']);
4636
						} else {
4637
							$wanif = $cfg['if'];
4638
						}
4639
						break;
4640
					default:
4641
						switch ($cfg['ipaddr']) {
4642
							case 'pppoe':
4643
							case 'ppp':
4644
							case 'l2tp':
4645
							case 'pptp':
4646
								if (isset($cfg['dhcp6usev4iface']) && $realv6iface === false) {
4647
									$wanif = $cfg['if'];
4648
								} else {
4649
									$parents = get_parent_interface($interface);
4650
									if (!empty($parents[0])) {
4651
										$wanif = $parents[0];
4652
									} else {
4653
										$wanif = $cfg['if'];
4654
									}
4655
								}
4656
								break;
4657
							default:
4658
								if (is_array($cfg['wireless']) || preg_match($g['wireless_regex'], $cfg['if'])) {
4659
									$wanif = interface_get_wireless_clone($cfg['if']);
4660
								} else {
4661
									$wanif = $cfg['if'];
4662
								}
4663
								break;
4664
						}
4665
						break;
4666
				}
4667
			} else {
4668
				// Wireless cloned NIC support (FreeBSD 8+)
4669
				// interface name format: $parentnic_wlanparentnic#
4670
				// example: ath0_wlan0
4671 086cf944 Phil Davis
				if (is_array($cfg['wireless']) || preg_match($g['wireless_regex'], $cfg['if'])) {
4672 4cc3bb6c Ermal
					$wanif = interface_get_wireless_clone($cfg['if']);
4673 cf73302f Phil Davis
				} else {
4674 4cc3bb6c Ermal
					$wanif = $cfg['if'];
4675 15a73ba8 Ermal
				}
4676 b6c1f22f Ermal
			}
4677 cf73302f Phil Davis
			break;
4678 c515ea57 Scott Ullrich
	}
4679
4680 1c3ddd9e Renato Botelho
	return $wanif;
4681 5b237745 Scott Ullrich
}
4682
4683 9ff8c299 Seth Mos
/* Guess the physical interface by providing a IP address */
4684 afb2de1b Ermal Lu?i
function guess_interface_from_ip($ipaddress) {
4685 aa5acb42 Ermal
4686
	$family = '';
4687 cf73302f Phil Davis
	if (is_ipaddrv4($ipaddress)) {
4688 aa5acb42 Ermal
		$family = 'inet';
4689 cf73302f Phil Davis
	}
4690
	if (empty($family) && is_ipaddrv6($ipaddress)) {
4691 aa5acb42 Ermal
		$family = 'inet6';
4692 cf73302f Phil Davis
	}
4693 aa5acb42 Ermal
4694 cf73302f Phil Davis
	if (empty($family)) {
4695 1c3ddd9e Renato Botelho
		return false;
4696 cf73302f Phil Davis
	}
4697 aa5acb42 Ermal
4698
	/* create a route table we can search */
4699
	$output = '';
4700
	$_gb = exec("/sbin/route -n get -{$family} " . escapeshellarg($ipaddress) . " | /usr/bin/awk '/interface/ { print \$2; };'", $output);
4701
	$output[0] = trim($output[0], " \n");
4702 cf73302f Phil Davis
	if (!empty($output[0])) {
4703 aa5acb42 Ermal
		return $output[0];
4704 cf73302f Phil Davis
	}
4705 aa5acb42 Ermal
4706
	return false;
4707 afb2de1b Ermal Lu?i
}
4708
4709
/*
4710
 * find_ip_interface($ip): return the interface where an ip is defined
4711 59231855 Darren Embry
 *   (or if $bits is specified, where an IP within the subnet is defined)
4712 afb2de1b Ermal Lu?i
 */
4713 2027b4c7 bcyrill
function find_ip_interface($ip, $bits = null) {
4714 cf73302f Phil Davis
	if (!is_ipaddr($ip)) {
4715 2027b4c7 bcyrill
		return false;
4716 cf73302f Phil Davis
	}
4717 a8f5790a Renato Botelho
4718 2027b4c7 bcyrill
	$isv6ip = is_ipaddrv6($ip);
4719 a8f5790a Renato Botelho
4720 59231855 Darren Embry
	/* if list */
4721
	$ifdescrs = get_configured_interface_list();
4722 be45aa79 Renato Botelho
4723 59231855 Darren Embry
	foreach ($ifdescrs as $ifdescr => $ifname) {
4724 2027b4c7 bcyrill
		$ifip = ($isv6ip) ? get_interface_ipv6($ifname) : get_interface_ip($ifname);
4725 cf73302f Phil Davis
		if (is_null($ifip)) {
4726 2027b4c7 bcyrill
			continue;
4727 cf73302f Phil Davis
		}
4728 ec8b4d8d Ermal
		if (is_null($bits)) {
4729 2027b4c7 bcyrill
			if ($ip == $ifip) {
4730 59231855 Darren Embry
				$int = get_real_interface($ifname);
4731
				return $int;
4732
			}
4733 cf73302f Phil Davis
		} else {
4734 2027b4c7 bcyrill
			if (ip_in_subnet($ifip, $ip . "/" . $bits)) {
4735 59231855 Darren Embry
				$int = get_real_interface($ifname);
4736
				return $int;
4737
			}
4738
		}
4739
	}
4740 ec8b4d8d Ermal
4741 59231855 Darren Embry
	return false;
4742
}
4743 afb2de1b Ermal Lu?i
4744 59231855 Darren Embry
/*
4745
 * find_virtual_ip_alias($ip): return the virtual IP alias where an IP is found
4746
 *   (or if $bits is specified, where an IP within the subnet is found)
4747
 */
4748
function find_virtual_ip_alias($ip, $bits = null) {
4749
	global $config;
4750 a8f5790a Renato Botelho
4751 59231855 Darren Embry
	if (!is_array($config['virtualip']['vip'])) {
4752
		return false;
4753
	}
4754 cf73302f Phil Davis
	if (!is_ipaddr($ip)) {
4755 6b207f73 bcyrill
		return false;
4756 cf73302f Phil Davis
	}
4757 a8f5790a Renato Botelho
4758 6b207f73 bcyrill
	$isv6ip = is_ipaddrv6($ip);
4759 a8f5790a Renato Botelho
4760 59231855 Darren Embry
	foreach ($config['virtualip']['vip'] as $vip) {
4761
		if ($vip['mode'] === "ipalias") {
4762 cf73302f Phil Davis
			if (is_ipaddrv6($vip['subnet']) != $isv6ip) {
4763 6b207f73 bcyrill
				continue;
4764 cf73302f Phil Davis
			}
4765 ec8b4d8d Ermal
			if (is_null($bits)) {
4766 59231855 Darren Embry
				if (ip_in_subnet($ip, $vip['subnet'] . "/" . $vip['subnet_bits'])) {
4767
					return $vip;
4768
				}
4769 cf73302f Phil Davis
			} else {
4770
				if (($isv6ip && check_subnetsv6_overlap($ip, $bits, $vip['subnet'], $vip['subnet_bits'])) ||
4771
				    (!$isv6ip && check_subnets_overlap($ip, $bits, $vip['subnet'], $vip['subnet_bits']))) {
4772 59231855 Darren Embry
					return $vip;
4773
				}
4774
			}
4775 abcb2bed Ermal Lu?i
		}
4776 59231855 Darren Embry
	}
4777
	return false;
4778 afb2de1b Ermal Lu?i
}
4779
4780 a71b32d2 Scott Ullrich
/*
4781
 *   find_number_of_created_carp_interfaces: return the number of carp interfaces
4782
 */
4783
function find_number_of_created_carp_interfaces() {
4784
	return `/sbin/ifconfig | grep "carp:" | wc -l`;
4785
}
4786
4787 abcb2bed Ermal Lu?i
/*
4788
 * find_carp_interface($ip): return the carp interface where an ip is defined
4789
 */
4790
function find_carp_interface($ip) {
4791 27625b39 Scott Ullrich
	global $config;
4792 abcb2bed Ermal Lu?i
	if (is_array($config['virtualip']['vip'])) {
4793
		foreach ($config['virtualip']['vip'] as $vip) {
4794 3e662cb0 Ermal
			if ($vip['mode'] == "carp") {
4795 cf73302f Phil Davis
				if (is_ipaddrv4($ip)) {
4796 645ad665 Seth Mos
					$carp_ip = get_interface_ip($vip['interface']);
4797
				}
4798 cf73302f Phil Davis
				if (is_ipaddrv6($ip)) {
4799 645ad665 Seth Mos
					$carp_ip = get_interface_ipv6($vip['interface']);
4800
				}
4801
				exec("/sbin/ifconfig", $output, $return);
4802 cf73302f Phil Davis
				foreach ($output as $line) {
4803 645ad665 Seth Mos
					$elements = preg_split("/[ ]+/i", $line);
4804 cf73302f Phil Davis
					if (strstr($elements[0], "vip")) {
4805 645ad665 Seth Mos
						$curif = str_replace(":", "", $elements[0]);
4806 cf73302f Phil Davis
					}
4807
					if (stristr($line, $ip)) {
4808 645ad665 Seth Mos
						$if = $curif;
4809
						continue;
4810
					}
4811
				}
4812 a687f866 Namezero
4813 cf73302f Phil Davis
				if ($if) {
4814 27625b39 Scott Ullrich
					return $if;
4815 cf73302f Phil Davis
				}
4816 abcb2bed Ermal Lu?i
			}
4817
		}
4818
	}
4819
}
4820
4821
function link_carp_interface_to_parent($interface) {
4822 6fb26a17 smos
	global $config;
4823 abcb2bed Ermal Lu?i
4824 cf73302f Phil Davis
	if (empty($interface)) {
4825 6fb26a17 smos
		return;
4826 cf73302f Phil Davis
	}
4827 abcb2bed Ermal Lu?i
4828 6fb26a17 smos
	$carp_ip = get_interface_ip($interface);
4829
	$carp_ipv6 = get_interface_ipv6($interface);
4830 abcb2bed Ermal Lu?i
4831 cf73302f Phil Davis
	if ((!is_ipaddrv4($carp_ip)) && (!is_ipaddrv6($carp_ipv6))) {
4832 6fb26a17 smos
		return;
4833 cf73302f Phil Davis
	}
4834 abcb2bed Ermal Lu?i
4835 6fb26a17 smos
	/* if list */
4836
	$ifdescrs = get_configured_interface_list();
4837
	foreach ($ifdescrs as $ifdescr => $ifname) {
4838
		/* check IPv4 */
4839 cf73302f Phil Davis
		if (is_ipaddrv4($carp_ip)) {
4840 6fb26a17 smos
			$interfaceip = get_interface_ip($ifname);
4841
			$subnet_bits = get_interface_subnet($ifname);
4842
			$subnet_ip = gen_subnet("{$interfaceip}", "{$subnet_bits}");
4843 cf73302f Phil Davis
			if (ip_in_subnet($carp_ip, "{$subnet_ip}/{$subnet_bits}")) {
4844 6fb26a17 smos
				return $ifname;
4845 cf73302f Phil Davis
			}
4846 6fb26a17 smos
		}
4847
		/* Check IPv6 */
4848 cf73302f Phil Davis
		if (is_ipaddrv6($carp_ipv6)) {
4849 6fb26a17 smos
			$interfaceipv6 = get_interface_ipv6($ifname);
4850
			$prefixlen = get_interface_subnetv6($ifname);
4851 cf73302f Phil Davis
			if (ip_in_subnet($carp_ipv6, "{$interfaceipv6}/{$prefixlen}")) {
4852 6fb26a17 smos
				return $ifname;
4853 cf73302f Phil Davis
			}
4854 6fb26a17 smos
		}
4855
	}
4856
	return "";
4857 abcb2bed Ermal Lu?i
}
4858
4859 6fb26a17 smos
4860 abcb2bed Ermal Lu?i
/****f* interfaces/link_ip_to_carp_interface
4861
 * NAME
4862
 *   link_ip_to_carp_interface - Find where a CARP interface links to.
4863
 * INPUTS
4864
 *   $ip
4865
 * RESULT
4866
 *   $carp_ints
4867
 ******/
4868
function link_ip_to_carp_interface($ip) {
4869 1c3ddd9e Renato Botelho
	global $config;
4870 abcb2bed Ermal Lu?i
4871 cf73302f Phil Davis
	if (!is_ipaddr($ip)) {
4872 1c3ddd9e Renato Botelho
		return;
4873 cf73302f Phil Davis
	}
4874 abcb2bed Ermal Lu?i
4875 1c3ddd9e Renato Botelho
	$carp_ints = "";
4876
	if (is_array($config['virtualip']['vip'])) {
4877 1d002dc9 Ermal
		$first = 0;
4878 3fbc3487 Ermal
		$carp_int = array();
4879 1c3ddd9e Renato Botelho
		foreach ($config['virtualip']['vip'] as $vip) {
4880
			if ($vip['mode'] == "carp") {
4881
				$carp_ip = $vip['subnet'];
4882
				$carp_sn = $vip['subnet_bits'];
4883
				$carp_nw = gen_subnet($carp_ip, $carp_sn);
4884
				if (ip_in_subnet($ip, "{$carp_nw}/{$carp_sn}")) {
4885 7238e0cf Ermal
					$carp_int[] = get_real_interface($vip['interface']);
4886 7b47bd4c Ermal
				}
4887 1c3ddd9e Renato Botelho
			}
4888
		}
4889 cf73302f Phil Davis
		if (!empty($carp_int)) {
4890 3fbc3487 Ermal
			$carp_ints = implode(" ", array_unique($carp_int));
4891 cf73302f Phil Davis
		}
4892 1c3ddd9e Renato Botelho
	}
4893 abcb2bed Ermal Lu?i
4894 1c3ddd9e Renato Botelho
	return $carp_ints;
4895 abcb2bed Ermal Lu?i
}
4896
4897 7a04cd20 Ermal
function link_interface_to_track6($int, $action = "") {
4898
	global $config;
4899
4900 cf73302f Phil Davis
	if (empty($int)) {
4901 7a04cd20 Ermal
		return;
4902 cf73302f Phil Davis
	}
4903 7a04cd20 Ermal
4904
	if (is_array($config['interfaces'])) {
4905
		$list = array();
4906
		foreach ($config['interfaces'] as $ifname => $ifcfg) {
4907 cf73302f Phil Davis
			if (!isset($ifcfg['enable'])) {
4908 7a04cd20 Ermal
				continue;
4909 cf73302f Phil Davis
			}
4910 7a04cd20 Ermal
			if (!empty($ifcfg['ipaddrv6']) && $ifcfg['track6-interface'] == $int) {
4911 cf73302f Phil Davis
				if ($action == "update") {
4912 7a04cd20 Ermal
					interface_track6_configure($ifname, $ifcfg);
4913 cf73302f Phil Davis
				} else if ($action == "") {
4914 7a04cd20 Ermal
					$list[$ifname] = $ifcfg;
4915 cf73302f Phil Davis
				}
4916 7a04cd20 Ermal
			}
4917
		}
4918
		return $list;
4919
	}
4920
}
4921
4922 2b58f94e Ermal LUÇI
function interface_find_child_cfgmtu($realiface) {
4923
	global $config;
4924
4925
	$interface = convert_real_interface_to_friendly_interface_name($realiface);
4926
	$vlans = link_interface_to_vlans($realiface);
4927
	$bridge = link_interface_to_bridge($realiface);
4928
	if (!empty($interface)) {
4929
		$gifs = link_interface_to_gif($interface);
4930
		$gres = link_interface_to_gre($interface);
4931
	} else {
4932
		$gifs = array();
4933
		$gres = array();
4934
	}
4935
4936
	$mtu = 0;
4937 2f8f9d5a Ermal LUÇI
	if (is_array($vlans)) {
4938
		foreach ($vlans as $vlan) {
4939
			$ifass = convert_real_interface_to_friendly_interface_name($vlan['vlanif']);
4940 cf73302f Phil Davis
			if (empty($ifass)) {
4941 2f8f9d5a Ermal LUÇI
				continue;
4942 cf73302f Phil Davis
			}
4943 2f8f9d5a Ermal LUÇI
			if (!empty($config['interfaces'][$ifass]['mtu'])) {
4944 cf73302f Phil Davis
				if (intval($config['interfaces'][$ifass]['mtu']) > $mtu) {
4945 2f8f9d5a Ermal LUÇI
					$mtu = intval($config['interfaces'][$ifass]['mtu']);
4946 cf73302f Phil Davis
				}
4947 2f8f9d5a Ermal LUÇI
			}
4948 2b58f94e Ermal LUÇI
		}
4949
	}
4950 2f8f9d5a Ermal LUÇI
	if (is_array($gifs)) {
4951
		foreach ($gifs as $vlan) {
4952
			$ifass = convert_real_interface_to_friendly_interface_name($vlan['gifif']);
4953 cf73302f Phil Davis
			if (empty($ifass)) {
4954 2f8f9d5a Ermal LUÇI
				continue;
4955 cf73302f Phil Davis
			}
4956 2f8f9d5a Ermal LUÇI
			if (!empty($config['interfaces'][$ifass]['mtu'])) {
4957 cf73302f Phil Davis
				if (intval($config['interfaces'][$ifass]['mtu']) > $mtu) {
4958 2f8f9d5a Ermal LUÇI
					$mtu = intval($config['interfaces'][$ifass]['mtu']);
4959 cf73302f Phil Davis
				}
4960 2f8f9d5a Ermal LUÇI
			}
4961 2b58f94e Ermal LUÇI
		}
4962
	}
4963 2f8f9d5a Ermal LUÇI
	if (is_array($gres)) {
4964
		foreach ($gres as $vlan) {
4965
			$ifass = convert_real_interface_to_friendly_interface_name($vlan['greif']);
4966 cf73302f Phil Davis
			if (empty($ifass)) {
4967 2f8f9d5a Ermal LUÇI
				continue;
4968 cf73302f Phil Davis
			}
4969 2f8f9d5a Ermal LUÇI
			if (!empty($config['interfaces'][$ifass]['mtu'])) {
4970 cf73302f Phil Davis
				if (intval($config['interfaces'][$ifass]['mtu']) > $mtu) {
4971 2f8f9d5a Ermal LUÇI
					$mtu = intval($config['interfaces'][$ifass]['mtu']);
4972 cf73302f Phil Davis
				}
4973 2f8f9d5a Ermal LUÇI
			}
4974 2b58f94e Ermal LUÇI
		}
4975
	}
4976
	$ifass = convert_real_interface_to_friendly_interface_name($bridge);
4977
	if (!empty($ifass) && !empty($config['interfaces'][$ifass]['mtu'])) {
4978 cf73302f Phil Davis
		if (intval($config['interfaces'][$ifass]['mtu']) > $mtu) {
4979 2b58f94e Ermal LUÇI
			$mtu = intval($config['interfaces'][$ifass]['mtu']);
4980 cf73302f Phil Davis
		}
4981 2b58f94e Ermal LUÇI
	}
4982
	unset($vlans, $bridge, $gifs, $gres, $ifass, $vlan);
4983
4984
	return $mtu;
4985
}
4986
4987 7850de1c Ermal Lu?i
function link_interface_to_vlans($int, $action = "") {
4988
	global $config;
4989
4990 cf73302f Phil Davis
	if (empty($int)) {
4991 7850de1c Ermal Lu?i
		return;
4992 cf73302f Phil Davis
	}
4993 7850de1c Ermal Lu?i
4994
	if (is_array($config['vlans']['vlan'])) {
4995 a362a23a Ermal
		$ifaces = array();
4996 1c3ddd9e Renato Botelho
		foreach ($config['vlans']['vlan'] as $vlan) {
4997 fa4a331f Ermal
			if ($int == $vlan['if']) {
4998 7850de1c Ermal Lu?i
				if ($action == "update") {
4999 fa4a331f Ermal
					interfaces_bring_up($int);
5000 cf73302f Phil Davis
				} else {
5001 a362a23a Ermal
					$ifaces[$vlan['tag']] = $vlan;
5002 cf73302f Phil Davis
				}
5003 7850de1c Ermal Lu?i
			}
5004
		}
5005 cf73302f Phil Davis
		if (!empty($ifaces)) {
5006 a362a23a Ermal
			return $ifaces;
5007 cf73302f Phil Davis
		}
5008 7850de1c Ermal Lu?i
	}
5009
}
5010
5011 89f171b0 Ermal LUÇI
function link_interface_to_vips($int, $action = "", $vhid = '') {
5012 1c3ddd9e Renato Botelho
	global $config;
5013 e5ac67ed Ermal Lu?i
5014 1c3ddd9e Renato Botelho
	if (is_array($config['virtualip']['vip'])) {
5015 75201355 Ermal
		$result = array();
5016 dcadda55 Ermal
		foreach ($config['virtualip']['vip'] as $vip) {
5017
			if ($int == $vip['interface']) {
5018 cf73302f Phil Davis
				if ($action == "update") {
5019 7b47bd4c Ermal
					interfaces_vips_configure($int);
5020 cf73302f Phil Davis
				} else {
5021
					if (empty($vhid) || ($vhid == $vip['vhid'])) {
5022 89f171b0 Ermal LUÇI
						$result[] = $vip;
5023 cf73302f Phil Davis
					}
5024 89f171b0 Ermal LUÇI
				}
5025 7850de1c Ermal Lu?i
			}
5026 dcadda55 Ermal
		}
5027 75201355 Ermal
		return $result;
5028 dcadda55 Ermal
	}
5029 e5ac67ed Ermal Lu?i
}
5030
5031 afb2de1b Ermal Lu?i
/****f* interfaces/link_interface_to_bridge
5032
 * NAME
5033
 *   link_interface_to_bridge - Finds out a bridge group for an interface
5034
 * INPUTS
5035
 *   $ip
5036
 * RESULT
5037
 *   bridge[0-99]
5038
 ******/
5039
function link_interface_to_bridge($int) {
5040 1c3ddd9e Renato Botelho
	global $config;
5041 afb2de1b Ermal Lu?i
5042 1c3ddd9e Renato Botelho
	if (is_array($config['bridges']['bridged'])) {
5043
		foreach ($config['bridges']['bridged'] as $bridge) {
5044 cf73302f Phil Davis
			if (in_array($int, explode(',', $bridge['members']))) {
5045 1c3ddd9e Renato Botelho
				return "{$bridge['bridgeif']}";
5046 cf73302f Phil Davis
			}
5047 a639bb91 Ermal
		}
5048
	}
5049 afb2de1b Ermal Lu?i
}
5050
5051 48f23632 Ermal
function link_interface_to_group($int) {
5052 1c3ddd9e Renato Botelho
	global $config;
5053 48f23632 Ermal
5054 ed62880b Ermal
	$result = array();
5055
5056 1c3ddd9e Renato Botelho
	if (is_array($config['ifgroups']['ifgroupentry'])) {
5057
		foreach ($config['ifgroups']['ifgroupentry'] as $group) {
5058 cf73302f Phil Davis
			if (in_array($int, explode(" ", $group['members']))) {
5059 ed62880b Ermal
				$result[$group['ifname']] = $int;
5060 cf73302f Phil Davis
			}
5061 48f23632 Ermal
		}
5062
	}
5063 ed62880b Ermal
5064
	return $result;
5065 48f23632 Ermal
}
5066
5067 afb2de1b Ermal Lu?i
function link_interface_to_gre($interface) {
5068 1c3ddd9e Renato Botelho
	global $config;
5069 afb2de1b Ermal Lu?i
5070 ed62880b Ermal
	$result = array();
5071
5072 1c3ddd9e Renato Botelho
	if (is_array($config['gres']['gre'])) {
5073 cf73302f Phil Davis
		foreach ($config['gres']['gre'] as $gre) {
5074
			if ($gre['if'] == $interface) {
5075 ed62880b Ermal
				$result[] = $gre;
5076 cf73302f Phil Davis
			}
5077
		}
5078 ed62880b Ermal
	}
5079
5080
	return $result;
5081 afb2de1b Ermal Lu?i
}
5082
5083
function link_interface_to_gif($interface) {
5084 1c3ddd9e Renato Botelho
	global $config;
5085 afb2de1b Ermal Lu?i
5086 ed62880b Ermal
	$result = array();
5087
5088 1c3ddd9e Renato Botelho
	if (is_array($config['gifs']['gif'])) {
5089 cf73302f Phil Davis
		foreach ($config['gifs']['gif'] as $gif) {
5090
			if ($gif['if'] == $interface) {
5091 1c3ddd9e Renato Botelho
				$result[] = $gif;
5092 cf73302f Phil Davis
			}
5093
		}
5094 ed62880b Ermal
	}
5095
5096
	return $result;
5097 afb2de1b Ermal Lu?i
}
5098
5099
/*
5100
 * find_interface_ip($interface): return the interface ip (first found)
5101
 */
5102 a8f5790a Renato Botelho
function find_interface_ip($interface, $flush = false) {
5103 afb2de1b Ermal Lu?i
	global $interface_ip_arr_cache;
5104 01f1b601 Ermal
	global $interface_sn_arr_cache;
5105 afb2de1b Ermal Lu?i
5106
	$interface = str_replace("\n", "", $interface);
5107 be45aa79 Renato Botelho
5108 cf73302f Phil Davis
	if (!does_interface_exist($interface)) {
5109 afb2de1b Ermal Lu?i
		return;
5110 cf73302f Phil Davis
	}
5111 afb2de1b Ermal Lu?i
5112
	/* Setup IP cache */
5113
	if (!isset($interface_ip_arr_cache[$interface]) or $flush) {
5114 3f70e618 Ermal Lu?i
		$ifinfo = pfSense_get_interface_addresses($interface);
5115
		$interface_ip_arr_cache[$interface] = $ifinfo['ipaddr'];
5116 01f1b601 Ermal
		$interface_sn_arr_cache[$interface] = $ifinfo['subnetbits'];
5117 afb2de1b Ermal Lu?i
	}
5118
5119
	return $interface_ip_arr_cache[$interface];
5120
}
5121
5122 47593ac6 Seth Mos
/*
5123
 * find_interface_ipv6($interface): return the interface ip (first found)
5124
 */
5125 a8f5790a Renato Botelho
function find_interface_ipv6($interface, $flush = false) {
5126 47593ac6 Seth Mos
	global $interface_ipv6_arr_cache;
5127
	global $interface_snv6_arr_cache;
5128 31ace4ea Seth Mos
	global $config;
5129 be45aa79 Renato Botelho
5130 31bdb9e5 smos
	$interface = trim($interface);
5131
	$interface = get_real_interface($interface);
5132 be45aa79 Renato Botelho
5133 cf73302f Phil Davis
	if (!does_interface_exist($interface)) {
5134 47593ac6 Seth Mos
		return;
5135 cf73302f Phil Davis
	}
5136 47593ac6 Seth Mos
5137
	/* Setup IP cache */
5138
	if (!isset($interface_ipv6_arr_cache[$interface]) or $flush) {
5139 ce14e37b Ermal
		$ifinfo = pfSense_get_interface_addresses($interface);
5140
		$interface_ipv6_arr_cache[$interface] = $ifinfo['ipaddr6'];
5141
		$interface_snv6_arr_cache[$interface] = $ifinfo['subnetbits6'];
5142 47593ac6 Seth Mos
	}
5143
5144
	return $interface_ipv6_arr_cache[$interface];
5145
}
5146
5147 81a3b6f5 smos
/*
5148
 * find_interface_ipv6_ll($interface): return the interface ipv6 link local (first found)
5149
 */
5150 a8f5790a Renato Botelho
function find_interface_ipv6_ll($interface, $flush = false) {
5151 58418355 smos
	global $interface_llv6_arr_cache;
5152 81a3b6f5 smos
	global $config;
5153 be45aa79 Renato Botelho
5154 81a3b6f5 smos
	$interface = str_replace("\n", "", $interface);
5155 be45aa79 Renato Botelho
5156 cf73302f Phil Davis
	if (!does_interface_exist($interface)) {
5157 81a3b6f5 smos
		return;
5158 cf73302f Phil Davis
	}
5159 81a3b6f5 smos
5160
	/* Setup IP cache */
5161 58418355 smos
	if (!isset($interface_llv6_arr_cache[$interface]) or $flush) {
5162 ce14e37b Ermal
		$ifinfo = pfSense_getall_interface_addresses($interface);
5163 cf73302f Phil Davis
		foreach ($ifinfo as $line) {
5164 ce14e37b Ermal
			if (strstr($line, ":")) {
5165
				$parts = explode("/", $line);
5166 cf73302f Phil Davis
				if (is_linklocal($parts[0])) {
5167 ce14e37b Ermal
					$ifinfo['linklocal'] = $parts[0];
5168 81a3b6f5 smos
				}
5169
			}
5170
		}
5171 58418355 smos
		$interface_llv6_arr_cache[$interface] = $ifinfo['linklocal'];
5172 81a3b6f5 smos
	}
5173 58418355 smos
	return $interface_llv6_arr_cache[$interface];
5174 81a3b6f5 smos
}
5175
5176 a8f5790a Renato Botelho
function find_interface_subnet($interface, $flush = false) {
5177 afb2de1b Ermal Lu?i
	global $interface_sn_arr_cache;
5178 01f1b601 Ermal
	global $interface_ip_arr_cache;
5179 afb2de1b Ermal Lu?i
5180
	$interface = str_replace("\n", "", $interface);
5181 cf73302f Phil Davis
	if (does_interface_exist($interface) == false) {
5182 afb2de1b Ermal Lu?i
		return;
5183 cf73302f Phil Davis
	}
5184 afb2de1b Ermal Lu?i
5185
	if (!isset($interface_sn_arr_cache[$interface]) or $flush) {
5186 bd96e1fe Ermal Lu?i
		$ifinfo = pfSense_get_interface_addresses($interface);
5187 01f1b601 Ermal
		$interface_ip_arr_cache[$interface] = $ifinfo['ipaddr'];
5188 bd96e1fe Ermal Lu?i
		$interface_sn_arr_cache[$interface] = $ifinfo['subnetbits'];
5189 1c3ddd9e Renato Botelho
	}
5190 afb2de1b Ermal Lu?i
5191
	return $interface_sn_arr_cache[$interface];
5192
}
5193
5194 a8f5790a Renato Botelho
function find_interface_subnetv6($interface, $flush = false) {
5195 47593ac6 Seth Mos
	global $interface_snv6_arr_cache;
5196
	global $interface_ipv6_arr_cache;
5197
5198
	$interface = str_replace("\n", "", $interface);
5199 cf73302f Phil Davis
	if (does_interface_exist($interface) == false) {
5200 47593ac6 Seth Mos
		return;
5201 cf73302f Phil Davis
	}
5202 47593ac6 Seth Mos
5203
	if (!isset($interface_snv6_arr_cache[$interface]) or $flush) {
5204 2c3924a1 Ermal
		$ifinfo = pfSense_get_interface_addresses($interface);
5205
		$interface_ipv6_arr_cache[$interface] = $ifinfo['ipaddr6'];
5206
		$interface_snv6_arr_cache[$interface] = $ifinfo['subnetbits6'];
5207 1c3ddd9e Renato Botelho
	}
5208 47593ac6 Seth Mos
5209
	return $interface_snv6_arr_cache[$interface];
5210
}
5211
5212 e19b7d1e Ermal
function ip_in_interface_alias_subnet($interface, $ipalias) {
5213
	global $config;
5214
5215 cf73302f Phil Davis
	if (empty($interface) || !is_ipaddr($ipalias)) {
5216 e8471084 Ermal
		return false;
5217 cf73302f Phil Davis
	}
5218 e19b7d1e Ermal
	if (is_array($config['virtualip']['vip'])) {
5219 1c3ddd9e Renato Botelho
		foreach ($config['virtualip']['vip'] as $vip) {
5220
			switch ($vip['mode']) {
5221 cf73302f Phil Davis
				case "ipalias":
5222
					if ($vip['interface'] <> $interface) {
5223
						break;
5224
					}
5225
					$subnet = is_ipaddrv6($ipalias) ? gen_subnetv6($vip['subnet'], $vip['subnet_bits']) : gen_subnet($vip['subnet'], $vip['subnet_bits']);
5226
					if (ip_in_subnet($ipalias, $subnet . "/" . $vip['subnet_bits'])) {
5227
						return true;
5228
					}
5229 1c3ddd9e Renato Botelho
					break;
5230
			}
5231
		}
5232 e19b7d1e Ermal
	}
5233 e8471084 Ermal
5234
	return false;
5235 e19b7d1e Ermal
}
5236
5237 89f171b0 Ermal LUÇI
function get_possible_listen_ips($include_ipv6_link_local=false) {
5238
5239
	$interfaces = get_configured_interface_with_descr();
5240
	foreach ($interfaces as $iface => $ifacename) {
5241
		if ($include_ipv6_link_local) {
5242
			/* This is to avoid going though added ll below */
5243 cf73302f Phil Davis
			if (substr($iface, 0, 5) == '_lloc') {
5244 89f171b0 Ermal LUÇI
				continue;
5245 cf73302f Phil Davis
			}
5246 89f171b0 Ermal LUÇI
			$llip = find_interface_ipv6_ll(get_real_interface($iface));
5247
			if (!empty($llip)) {
5248
				$interfaces["_lloc{$iface}"] = "{$ifacename} IPv6 Link-Local";
5249
			}
5250
		}
5251
	}
5252
	/* XXX: Maybe use array_merge below? */
5253
	$carplist = get_configured_carp_interface_list();
5254 cf73302f Phil Davis
	foreach ($carplist as $cif => $carpip) {
5255 89f171b0 Ermal LUÇI
		$interfaces[$cif] = $carpip . ' (' . get_vip_descr($carpip) . ')';
5256 cf73302f Phil Davis
	}
5257 89f171b0 Ermal LUÇI
	$aliaslist = get_configured_ip_aliases_list();
5258 cf73302f Phil Davis
	foreach ($aliaslist as $aliasip => $aliasif) {
5259 89f171b0 Ermal LUÇI
		$interfaces[$aliasip] = $aliasip . ' (' . get_vip_descr($aliasip) . ')';
5260 cf73302f Phil Davis
	}
5261 89f171b0 Ermal LUÇI
5262
	$interfaces['lo0'] = 'Localhost';
5263
5264
	return $interfaces;
5265
}
5266 cf73302f Phil Davis
5267 89f171b0 Ermal LUÇI
function get_possible_traffic_source_addresses($include_ipv6_link_local=false) {
5268
	global $config;
5269
5270
	$sourceips = get_possible_listen_ips($include_ipv6_link_local);
5271
	foreach (array('server', 'client') as $mode) {
5272
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
5273
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
5274
				if (!isset($setting['disable'])) {
5275 a08d5055 Phil Davis
					$sourceips_key = 'ovpn' . substr($mode, 0, 1) . $setting['vpnid'];
5276
					$sourceips[$sourceips_key] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
5277 89f171b0 Ermal LUÇI
				}
5278
			}
5279
		}
5280
	}
5281
	return $sourceips;
5282
}
5283
5284 a8f5790a Renato Botelho
function get_interface_ip($interface = "wan") {
5285 cf73302f Phil Davis
5286 bf001dec smos
	$realif = get_failover_interface($interface);
5287 cf73302f Phil Davis
	if (!$realif) {
5288 89f171b0 Ermal LUÇI
		return null;
5289 cf73302f Phil Davis
	}
5290 89f171b0 Ermal LUÇI
5291 cf73302f Phil Davis
	if (substr($realif, 0, 4) == '_vip') {
5292 89f171b0 Ermal LUÇI
		return get_configured_carp_interface_list($realif, 'inet', 'ip');
5293 cf73302f Phil Davis
	}
5294 6990ad35 Phil Davis
5295 89953fe7 Chris Buechler
	if (strstr($realif, "_vip")) {
5296
		return get_configured_carp_interface_list($realif);
5297
	}
5298 afb2de1b Ermal Lu?i
5299 5e041d5f Scott Ullrich
	$curip = find_interface_ip($realif);
5300 cf73302f Phil Davis
	if ($curip && is_ipaddr($curip) && ($curip != "0.0.0.0")) {
5301 5e041d5f Scott Ullrich
		return $curip;
5302 cf73302f Phil Davis
	} else {
5303 8256f324 gnhb
		return null;
5304 cf73302f Phil Davis
	}
5305 5b237745 Scott Ullrich
}
5306
5307 a8f5790a Renato Botelho
function get_interface_ipv6($interface = "wan", $flush = false) {
5308 479f0fda smos
	global $config;
5309 c4fc2eae Ermal
5310 89f171b0 Ermal LUÇI
	$realif = get_failover_interface($interface, 'inet6');
5311 cf73302f Phil Davis
	if (!$realif) {
5312 89f171b0 Ermal LUÇI
		return null;
5313 cf73302f Phil Davis
	}
5314 89f171b0 Ermal LUÇI
5315 cf73302f Phil Davis
	if (substr($realif, 0, 4) == '_vip') {
5316 89f171b0 Ermal LUÇI
		return get_configured_carp_interface_list($realif, 'inet6', 'ip');
5317 cf73302f Phil Davis
	} else if (substr($realif, 0, 5) == '_lloc') {
5318 89f171b0 Ermal LUÇI
		return get_interface_linklocal($interface);
5319 cf73302f Phil Davis
	}
5320 47593ac6 Seth Mos
5321 f253e928 Ermal
	if (is_array($config['interfaces'][$interface])) {
5322 420aa48b Ermal
		switch ($config['interfaces'][$interface]['ipaddr']) {
5323 cf73302f Phil Davis
			case 'pppoe':
5324
			case 'l2tp':
5325
			case 'pptp':
5326
			case 'ppp':
5327
				if ($config['interfaces'][$interface]['ipaddrv6'] == 'dhcp6') {
5328 5fb4bbfd doktornotor
					$realif = get_real_interface($interface, 'inet6', false);
5329 cf73302f Phil Davis
				}
5330
				break;
5331 420aa48b Ermal
		}
5332 f253e928 Ermal
	}
5333
5334 b6c1f22f Ermal
	$curip = find_interface_ipv6($realif, $flush);
5335 cf73302f Phil Davis
	if ($curip && is_ipaddrv6($curip) && ($curip != "::")) {
5336 47593ac6 Seth Mos
		return $curip;
5337 cf73302f Phil Davis
	} else {
5338 60802fad k-paulius
		/*
5339
		 * NOTE: On the case when only the prefix is requested,
5340
		 * the communication on WAN will be done over link-local.
5341
		 */
5342
		if (is_array($config['interfaces'][$interface]) && isset($config['interfaces'][$interface]['dhcp6prefixonly'])) {
5343
			$curip = find_interface_ipv6_ll($realif, $flush);
5344
			if ($curip && is_ipaddrv6($curip) && ($curip != "::")) {
5345
				return $curip;
5346
			}
5347
		}
5348 cf73302f Phil Davis
	}
5349 60802fad k-paulius
	return null;
5350 47593ac6 Seth Mos
}
5351
5352 a8f5790a Renato Botelho
function get_interface_linklocal($interface = "wan") {
5353 06886ae3 Ermal
5354 89f171b0 Ermal LUÇI
	$realif = get_failover_interface($interface, 'inet6');
5355 cf73302f Phil Davis
	if (!$realif) {
5356 89f171b0 Ermal LUÇI
		return null;
5357 cf73302f Phil Davis
	}
5358 89f171b0 Ermal LUÇI
5359 cf73302f Phil Davis
	if (substr($interface, 0, 4) == '_vip') {
5360 89f171b0 Ermal LUÇI
		$realif = get_real_interface($interface);
5361 cf73302f Phil Davis
	} else if (substr($interface, 0, 5) == '_lloc') {
5362 89f171b0 Ermal LUÇI
		$realif = get_real_interface(substr($interface, 5));
5363 cf73302f Phil Davis
	}
5364 58418355 smos
5365
	$curip = find_interface_ipv6_ll($realif);
5366 cf73302f Phil Davis
	if ($curip && is_ipaddrv6($curip) && ($curip != "::")) {
5367 58418355 smos
		return $curip;
5368 cf73302f Phil Davis
	} else {
5369 58418355 smos
		return null;
5370 cf73302f Phil Davis
	}
5371 58418355 smos
}
5372
5373 a8f5790a Renato Botelho
function get_interface_subnet($interface = "wan") {
5374 89f171b0 Ermal LUÇI
5375 cf73302f Phil Davis
	if (substr($interface, 0, 4) == '_vip') {
5376 89f171b0 Ermal LUÇI
		return get_configured_carp_interface_list($interface, 'inet', 'subnet');
5377 cf73302f Phil Davis
	}
5378 89f171b0 Ermal LUÇI
5379 31b24870 Ermal Luçi
	$realif = get_real_interface($interface);
5380 cf73302f Phil Davis
	if (!$realif) {
5381 89f171b0 Ermal LUÇI
		return null;
5382 cf73302f Phil Davis
	}
5383 e88fbe50 Ermal Lu?i
5384 5e041d5f Scott Ullrich
	$cursn = find_interface_subnet($realif);
5385 cf73302f Phil Davis
	if (!empty($cursn)) {
5386 31b24870 Ermal Luçi
		return $cursn;
5387 cf73302f Phil Davis
	}
5388 31b24870 Ermal Luçi
5389
	return null;
5390
}
5391
5392 a8f5790a Renato Botelho
function get_interface_subnetv6($interface = "wan") {
5393 c4fc2eae Ermal
5394 cf73302f Phil Davis
	if (substr($interface, 0, 4) == '_vip') {
5395 89f171b0 Ermal LUÇI
		return get_configured_carp_interface_list($interface, 'inet6', 'subnet');
5396 cf73302f Phil Davis
	} else if (substr($interface, 0, 5) == '_lloc') {
5397 89f171b0 Ermal LUÇI
		$interface = substr($interface, 5);
5398 cf73302f Phil Davis
	}
5399 89f171b0 Ermal LUÇI
5400
	$realif = get_real_interface($interface, 'inet6');
5401 cf73302f Phil Davis
	if (!$realif) {
5402 89f171b0 Ermal LUÇI
		return null;
5403 cf73302f Phil Davis
	}
5404 47593ac6 Seth Mos
5405
	$cursn = find_interface_subnetv6($realif);
5406 cf73302f Phil Davis
	if (!empty($cursn)) {
5407 47593ac6 Seth Mos
		return $cursn;
5408 cf73302f Phil Davis
	}
5409 47593ac6 Seth Mos
5410
	return null;
5411
}
5412
5413 52947718 Ermal Lu?i
/* return outside interfaces with a gateway */
5414
function get_interfaces_with_gateway() {
5415 77ccab82 Scott Ullrich
	global $config;
5416 52947718 Ermal Lu?i
5417
	$ints = array();
5418
5419
	/* loop interfaces, check config for outbound */
5420 cf73302f Phil Davis
	foreach ($config['interfaces'] as $ifdescr => $ifname) {
5421 77ccab82 Scott Ullrich
		switch ($ifname['ipaddr']) {
5422
			case "dhcp":
5423
			case "pppoe":
5424
			case "pptp":
5425 6d5446a2 Ermal
			case "l2tp":
5426 9cf1dbff Stuart Wyatt
			case "ppp":
5427 6d5446a2 Ermal
				$ints[$ifdescr] = $ifdescr;
5428 cf73302f Phil Davis
				break;
5429 77ccab82 Scott Ullrich
			default:
5430 6c07db48 Phil Davis
				if (substr($ifname['if'], 0, 4) == "ovpn" ||
5431 cf73302f Phil Davis
				    !empty($ifname['gateway'])) {
5432 6d5446a2 Ermal
					$ints[$ifdescr] = $ifdescr;
5433 cf73302f Phil Davis
				}
5434
				break;
5435 77ccab82 Scott Ullrich
		}
5436
	}
5437
	return $ints;
5438 52947718 Ermal Lu?i
}
5439
5440
/* return true if interface has a gateway */
5441
function interface_has_gateway($friendly) {
5442 6d5446a2 Ermal
	global $config;
5443 52947718 Ermal Lu?i
5444 6d5446a2 Ermal
	if (!empty($config['interfaces'][$friendly])) {
5445 43a22ee2 jim-p
		$ifname = &$config['interfaces'][$friendly];
5446 6d5446a2 Ermal
		switch ($ifname['ipaddr']) {
5447
			case "dhcp":
5448
			case "pppoe":
5449
			case "pptp":
5450
			case "l2tp":
5451 9cf1dbff Stuart Wyatt
			case "ppp":
5452 6d5446a2 Ermal
				return true;
5453
			break;
5454
			default:
5455 6c07db48 Phil Davis
				if (substr($ifname['if'], 0, 4) == "ovpn") {
5456 e9d7afeb Ermal
					return true;
5457 cf73302f Phil Davis
				}
5458 2700ef96 PiBa-NL
				$tunnelif = substr($ifname['if'], 0, 3);
5459 cf73302f Phil Davis
				if ($tunnelif == "gif" || $tunnelif == "gre") {
5460 2700ef96 PiBa-NL
					return true;
5461 cf73302f Phil Davis
				}
5462
				if (!empty($ifname['gateway'])) {
5463 6d5446a2 Ermal
					return true;
5464 cf73302f Phil Davis
				}
5465 6d5446a2 Ermal
			break;
5466
		}
5467
	}
5468 52947718 Ermal Lu?i
5469
	return false;
5470
}
5471
5472 2feb85af Seth Mos
/* return true if interface has a gateway */
5473
function interface_has_gatewayv6($friendly) {
5474
	global $config;
5475
5476
	if (!empty($config['interfaces'][$friendly])) {
5477
		$ifname = &$config['interfaces'][$friendly];
5478
		switch ($ifname['ipaddrv6']) {
5479 67102344 smos
			case "slaac":
5480 2feb85af Seth Mos
			case "dhcp6":
5481 a11a839d smos
			case "6to4":
5482 d500e296 smos
			case "6rd":
5483
				return true;
5484 a11a839d smos
				break;
5485 2feb85af Seth Mos
			default:
5486 6c07db48 Phil Davis
				if (substr($ifname['if'], 0, 4) == "ovpn") {
5487 2feb85af Seth Mos
					return true;
5488 cf73302f Phil Davis
				}
5489 cdeaf91e Ermal
				$tunnelif = substr($ifname['if'], 0, 3);
5490 cf73302f Phil Davis
				if ($tunnelif == "gif" || $tunnelif == "gre") {
5491 c32a6b82 Ermal
					return true;
5492 cf73302f Phil Davis
				}
5493
				if (!empty($ifname['gatewayv6'])) {
5494 2feb85af Seth Mos
					return true;
5495 cf73302f Phil Davis
				}
5496 a11a839d smos
				break;
5497 2feb85af Seth Mos
		}
5498
	}
5499
5500
	return false;
5501
}
5502
5503 a57b119e Bill Marquette
/****f* interfaces/is_altq_capable
5504
 * NAME
5505
 *   is_altq_capable - Test if interface is capable of using ALTQ
5506
 * INPUTS
5507
 *   $int            - string containing interface name
5508
 * RESULT
5509
 *   boolean         - true or false
5510
 ******/
5511
5512 eba938e3 Scott Ullrich
function is_altq_capable($int) {
5513 1c3ddd9e Renato Botelho
	/* Per:
5514 3d8e954b Chris Buechler
	 * http://www.freebsd.org/cgi/man.cgi?query=altq&apropos=0&sektion=0&manpath=FreeBSD+8.3-RELEASE&arch=default&format=html
5515 1c3ddd9e Renato Botelho
	 * Only the following drivers have ALTQ support
5516 56effb56 Chris Buechler
	 * 20150328 - removed wireless drivers - ath, awi, bwn, iwi, ipw, ral, rum, run, wi - for now. redmine #4406
5517 1c3ddd9e Renato Botelho
	 */
5518 56effb56 Chris Buechler
	$capable = array("ae", "age", "alc", "ale", "an", "aue", "axe", "bce",
5519 3d8e954b Chris Buechler
			"bfe", "bge", "bridge", "cas", "dc", "de", "ed", "em", "ep", "epair", "et", "fxp", "gem",
5520 56effb56 Chris Buechler
			"hme", "hn", "igb", "ixgbe", "jme", "le", "lem", "msk", "mxge", "my", "nfe",
5521
			"nge", "npe", "nve", "re", "rl", "sf", "sge", "sis", "sk",
5522
			"ste", "stge", "ti", "txp", "udav", "ural", "vge", "vmx", "vr", "vte", "xl",
5523 febca7e8 Ermal
			"ndis", "tun", "ovpns", "ovpnc", "vlan", "pppoe", "pptp", "ng",
5524 cf205dca Ermal
			"l2tp", "ppp", "vtnet");
5525 a57b119e Bill Marquette
5526 2ccac125 Renato Botelho
	$int_family = remove_ifindex($int);
5527 a57b119e Bill Marquette
5528 cf73302f Phil Davis
	if (in_array($int_family, $capable)) {
5529 1c3ddd9e Renato Botelho
		return true;
5530 cf73302f Phil Davis
	} else if (stristr($int, "l2tp")) { /* VLANs are named $parent_$vlan now */
5531 dbe67167 Ermal
		return true;
5532 cf73302f Phil Davis
	} else if (stristr($int, "_vlan")) { /* VLANs are named $parent_$vlan now */
5533 7e627719 Ermal
		return true;
5534 cf73302f Phil Davis
	} else if (stristr($int, "_wlan")) { /* WLANs are named $parent_$wlan now */
5535 2f3446db Ermal Lu?i
		return true;
5536 cf73302f Phil Davis
	} else {
5537 1c3ddd9e Renato Botelho
		return false;
5538 cf73302f Phil Davis
	}
5539 a57b119e Bill Marquette
}
5540
5541 52947718 Ermal Lu?i
/****f* interfaces/is_interface_wireless
5542
 * NAME
5543
 *   is_interface_wireless - Returns if an interface is wireless
5544
 * RESULT
5545
 *   $tmp       - Returns if an interface is wireless
5546
 ******/
5547
function is_interface_wireless($interface) {
5548 1c3ddd9e Renato Botelho
	global $config, $g;
5549
5550
	$friendly = convert_real_interface_to_friendly_interface_name($interface);
5551 cf73302f Phil Davis
	if (!isset($config['interfaces'][$friendly]['wireless'])) {
5552 1c3ddd9e Renato Botelho
		if (preg_match($g['wireless_regex'], $interface)) {
5553 cf73302f Phil Davis
			if (isset($config['interfaces'][$friendly])) {
5554 1c3ddd9e Renato Botelho
				$config['interfaces'][$friendly]['wireless'] = array();
5555 cf73302f Phil Davis
			}
5556 1c3ddd9e Renato Botelho
			return true;
5557
		}
5558
		return false;
5559 cf73302f Phil Davis
	} else {
5560 1c3ddd9e Renato Botelho
		return true;
5561 cf73302f Phil Davis
	}
5562 52947718 Ermal Lu?i
}
5563
5564 eba938e3 Scott Ullrich
function get_wireless_modes($interface) {
5565 d8c67d69 Scott Ullrich
	/* return wireless modes and channels */
5566 92f7d37d Ermal Luçi
	$wireless_modes = array();
5567
5568 5357f386 Erik Fonnesbeck
	$cloned_interface = get_real_interface($interface);
5569 1b773d20 Ermal Lu?i
5570 cf73302f Phil Davis
	if ($cloned_interface && is_interface_wireless($cloned_interface)) {
5571 1b773d20 Ermal Lu?i
		$chan_list = "/sbin/ifconfig {$cloned_interface} list chan";
5572
		$stack_list = "/usr/bin/awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
5573 1de74081 Ermal Lu?i
		$format_list = "/usr/bin/awk '{print \$5 \" \" \$6 \",\" \$1}'";
5574 d8c67d69 Scott Ullrich
5575 4b0e71db Scott Ullrich
		$interface_channels = "";
5576 d8c67d69 Scott Ullrich
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
5577
		$interface_channel_count = count($interface_channels);
5578
5579
		$c = 0;
5580 a8f5790a Renato Botelho
		while ($c < $interface_channel_count) {
5581 d8c67d69 Scott Ullrich
			$channel_line = explode(",", $interface_channels["$c"]);
5582
			$wireless_mode = trim($channel_line[0]);
5583
			$wireless_channel = trim($channel_line[1]);
5584 cf73302f Phil Davis
			if (trim($wireless_mode) != "") {
5585 4066776d Scott Ullrich
				/* if we only have 11g also set 11b channels */
5586 cf73302f Phil Davis
				if ($wireless_mode == "11g") {
5587
					if (!isset($wireless_modes["11b"])) {
5588 1ae54336 Erik Fonnesbeck
						$wireless_modes["11b"] = array();
5589 cf73302f Phil Davis
					}
5590
				} else if ($wireless_mode == "11g ht") {
5591
					if (!isset($wireless_modes["11b"])) {
5592 1ae54336 Erik Fonnesbeck
						$wireless_modes["11b"] = array();
5593 cf73302f Phil Davis
					}
5594
					if (!isset($wireless_modes["11g"])) {
5595 1ae54336 Erik Fonnesbeck
						$wireless_modes["11g"] = array();
5596 cf73302f Phil Davis
					}
5597 39c1349c Erik Fonnesbeck
					$wireless_mode = "11ng";
5598 cf73302f Phil Davis
				} else if ($wireless_mode == "11a ht") {
5599
					if (!isset($wireless_modes["11a"])) {
5600 1ae54336 Erik Fonnesbeck
						$wireless_modes["11a"] = array();
5601 cf73302f Phil Davis
					}
5602 39c1349c Erik Fonnesbeck
					$wireless_mode = "11na";
5603 4066776d Scott Ullrich
				}
5604
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
5605
			}
5606 d8c67d69 Scott Ullrich
			$c++;
5607
		}
5608
	}
5609 4066776d Scott Ullrich
	return($wireless_modes);
5610 d8c67d69 Scott Ullrich
}
5611
5612 f4094f0d Erik Fonnesbeck
/* return channel numbers, frequency, max txpower, and max regulation txpower */
5613
function get_wireless_channel_info($interface) {
5614
	$wireless_channels = array();
5615
5616 5357f386 Erik Fonnesbeck
	$cloned_interface = get_real_interface($interface);
5617 f4094f0d Erik Fonnesbeck
5618 cf73302f Phil Davis
	if ($cloned_interface && is_interface_wireless($cloned_interface)) {
5619 f4094f0d Erik Fonnesbeck
		$chan_list = "/sbin/ifconfig {$cloned_interface} list txpower";
5620
		$stack_list = "/usr/bin/awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
5621
		$format_list = "/usr/bin/awk '{print \$1 \",\" \$3 \" \" \$4 \",\" \$5 \",\" \$7}'";
5622
5623
		$interface_channels = "";
5624
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
5625
5626
		foreach ($interface_channels as $channel_line) {
5627
			$channel_line = explode(",", $channel_line);
5628 cf73302f Phil Davis
			if (!isset($wireless_channels[$channel_line[0]])) {
5629 f4094f0d Erik Fonnesbeck
				$wireless_channels[$channel_line[0]] = $channel_line;
5630 cf73302f Phil Davis
			}
5631 f4094f0d Erik Fonnesbeck
		}
5632
	}
5633
	return($wireless_channels);
5634
}
5635
5636 52947718 Ermal Lu?i
/****f* interfaces/get_interface_mtu
5637
 * NAME
5638
 *   get_interface_mtu - Return the mtu of an interface
5639
 * RESULT
5640
 *   $tmp       - Returns the mtu of an interface
5641
 ******/
5642
function get_interface_mtu($interface) {
5643 3d3dd668 Ermal LUÇI
	$mtu = pfSense_interface_getmtu($interface);
5644 1c3ddd9e Renato Botelho
	return $mtu['mtu'];
5645 52947718 Ermal Lu?i
}
5646
5647 eba938e3 Scott Ullrich
function get_interface_mac($interface) {
5648 7d6076f3 Ermal Lu?i
5649 3f70e618 Ermal Lu?i
	$macinfo = pfSense_get_interface_addresses($interface);
5650
	return $macinfo["macaddr"];
5651 f2ba47f8 Ermal Lu?i
}
5652
5653
/****f* pfsense-utils/generate_random_mac_address
5654
 * NAME
5655
 *   generate_random_mac - generates a random mac address
5656
 * INPUTS
5657
 *   none
5658
 * RESULT
5659
 *   $mac - a random mac address
5660
 ******/
5661
function generate_random_mac_address() {
5662 1c3ddd9e Renato Botelho
	$mac = "02";
5663 6c07db48 Phil Davis
	for ($x = 0; $x < 5; $x++) {
5664 1c3ddd9e Renato Botelho
		$mac .= ":" . dechex(rand(16, 255));
5665 cf73302f Phil Davis
	}
5666 1c3ddd9e Renato Botelho
	return $mac;
5667 53c82ef9 Scott Ullrich
}
5668 b7ec2b9e Scott Ullrich
5669 52947718 Ermal Lu?i
/****f* interfaces/is_jumbo_capable
5670
 * NAME
5671
 *   is_jumbo_capable - Test if interface is jumbo frame capable.  Useful for determining VLAN capability.
5672
 * INPUTS
5673
 *   $int             - string containing interface name
5674
 * RESULT
5675
 *   boolean          - true or false
5676
 ******/
5677 47ee6926 Ermal
function is_jumbo_capable($iface) {
5678
	$iface = trim($iface);
5679
	$capable = pfSense_get_interface_addresses($iface);
5680 a687f866 Namezero
5681 cf73302f Phil Davis
	if (isset($capable['caps']['vlanmtu'])) {
5682 1c3ddd9e Renato Botelho
		return true;
5683 cf73302f Phil Davis
	}
5684 a687f866 Namezero
5685 21e2561f Chris Buechler
	// hack for some lagg modes missing vlanmtu, but work fine w/VLANs
5686 cf73302f Phil Davis
	if (substr($iface, 0, 4) == "lagg") {
5687 21e2561f Chris Buechler
		return true;
5688 cf73302f Phil Davis
	}
5689 21e2561f Chris Buechler
5690 47ee6926 Ermal
	return false;
5691 52947718 Ermal Lu?i
}
5692
5693 70e46e62 Ermal
function interface_setup_pppoe_reset_file($pppif, $iface="") {
5694 55f3ca1d gnhb
	global $g;
5695 70e46e62 Ermal
5696 5c8e8a17 gnhb
	$cron_file = "{$g['varetc_path']}/pppoe_restart_{$pppif}";
5697 766bd6d0 gnhb
5698 cf73302f Phil Davis
	if (!empty($iface) && !empty($pppif)) {
5699 7673cdb5 Ermal
		$cron_cmd = <<<EOD
5700
#!/bin/sh
5701
/usr/local/sbin/pfSctl -c 'interface reload {$iface}'
5702 70e46e62 Ermal
/usr/bin/logger -t {$pppif} "PPPoE periodic reset executed on {$iface}"
5703 7673cdb5 Ermal
5704
EOD;
5705
5706 70e46e62 Ermal
		@file_put_contents($cron_file, $cron_cmd);
5707
		chmod($cron_file, 0755);
5708 55f3ca1d gnhb
		sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP");
5709 cf73302f Phil Davis
	} else {
5710 766bd6d0 gnhb
		unlink_if_exists($cron_file);
5711 cf73302f Phil Davis
	}
5712 b7ec2b9e Scott Ullrich
}
5713
5714 56da23dc Ermal
function get_interface_default_mtu($type = "ethernet") {
5715
	switch ($type) {
5716 cf73302f Phil Davis
		case "gre":
5717
			return 1476;
5718
			break;
5719
		case "gif":
5720
			return 1280;
5721
			break;
5722
		case "tun":
5723
		case "vlan":
5724
		case "tap":
5725
		case "ethernet":
5726
		default:
5727
			return 1500;
5728
			break;
5729 56da23dc Ermal
	}
5730
5731
	/* Never reached */
5732
	return 1500;
5733
}
5734
5735 dd62256f Pierre POMES
function get_vip_descr($ipaddress) {
5736
	global $config;
5737
5738
	foreach ($config['virtualip']['vip'] as $vip) {
5739
		if ($vip['subnet'] == $ipaddress) {
5740
			return ($vip['descr']);
5741
		}
5742
	}
5743
	return "";
5744
}
5745
5746 d368b334 jim-p
function interfaces_staticarp_configure($if) {
5747
	global $config, $g;
5748 cf73302f Phil Davis
	if (isset($config['system']['developerspew'])) {
5749 d368b334 jim-p
		$mt = microtime();
5750
		echo "interfaces_staticarp_configure($if) being called $mt\n";
5751
	}
5752
5753
	$ifcfg = $config['interfaces'][$if];
5754
5755 cf73302f Phil Davis
	if (empty($if) || empty($ifcfg['if']) || !isset($ifcfg['enable'])) {
5756 d368b334 jim-p
		return 0;
5757 cf73302f Phil Davis
	}
5758 d368b334 jim-p
5759
	/* Enable staticarp, if enabled */
5760 cf73302f Phil Davis
	if (isset($config['dhcpd'][$if]['staticarp'])) {
5761 086cf944 Phil Davis
		mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp ");
5762 d368b334 jim-p
		mwexec("/usr/sbin/arp -d -i " . escapeshellarg($ifcfg['if']) . " -a > /dev/null 2>&1 ");
5763
		if (is_array($config['dhcpd'][$if]['staticmap'])) {
5764
			foreach ($config['dhcpd'][$if]['staticmap'] as $arpent) {
5765
				mwexec("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac']));
5766
			}
5767
		}
5768
	} else {
5769 086cf944 Phil Davis
		mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp ");
5770 d368b334 jim-p
		mwexec("/usr/sbin/arp -d -i " . escapeshellarg($ifcfg['if']) . " -a > /dev/null 2>&1 ");
5771 c06e4f69 Ermal
		if (is_array($config['dhcpd'][$if]) && is_array($config['dhcpd'][$if]['staticmap'])) {
5772 25c1ebd5 N0YB
			foreach ($config['dhcpd'][$if]['staticmap'] as $arpent) {
5773
				if (isset($arpent['arp_table_static_entry'])) {
5774
					mwexec("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac']));
5775
				}
5776
			}
5777
		}
5778 d368b334 jim-p
	}
5779
5780
	return 0;
5781
}
5782
5783 909de400 Ermal
function get_failover_interface($interface, $family = "all") {
5784 bf001dec smos
	global $config;
5785 06886ae3 Ermal
5786 e90c833a smos
	/* shortcut to get_real_interface if we find it in the config */
5787 06886ae3 Ermal
	if (is_array($config['interfaces'][$interface])) {
5788
		return get_real_interface($interface, $family);
5789 e90c833a smos
	}
5790
5791 bf001dec smos
	/* compare against gateway groups */
5792
	$a_groups = return_gateway_groups_array();
5793 06886ae3 Ermal
	if (is_array($a_groups[$interface])) {
5794 bf001dec smos
		/* we found a gateway group, fetch the interface or vip */
5795 cf73302f Phil Davis
		if (!empty($a_groups[$interface][0]['vip'])) {
5796 06886ae3 Ermal
			return $a_groups[$interface][0]['vip'];
5797 cf73302f Phil Davis
		} else {
5798 06886ae3 Ermal
			return $a_groups[$interface][0]['int'];
5799 cf73302f Phil Davis
		}
5800 bf001dec smos
	}
5801
	/* fall through to get_real_interface */
5802 06886ae3 Ermal
	/* XXX: Really needed? */
5803
	return get_real_interface($interface, $family);
5804 bf001dec smos
}
5805
5806 2ccac125 Renato Botelho
function remove_ifindex($ifname) {
5807
	return preg_replace("/[0-9]+$/", "", $ifname);
5808
}
5809
5810 6a7dd9bb Ermal
?>