Project

General

Profile

Download (140 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 acc1e9d0 Scott Ullrich
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	interfaces.inc
5 eba938e3 Scott Ullrich
	Copyright (C) 2004-2008 Scott Ullrich
6 a687f866 Namezero
	Copyright (C) 2008-2009 Ermal Lu?i
7 ac3f8318 Espen Johansen
	All rights reserved.
8
9
	function interfaces_wireless_configure is
10
	Copyright (C) 2005 Espen Johansen
11 cfc707f7 Scott Ullrich
	All rights reserved.
12
13
	originally part of m0n0wall (http://m0n0.ch/wall)
14 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
15
	All rights reserved.
16 cfc707f7 Scott Ullrich
17 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
18
	modification, are permitted provided that the following conditions are met:
19 cfc707f7 Scott Ullrich
20 ac3f8318 Espen Johansen
	1. Redistributions of source code must retain the above copyright notices,
21 5b237745 Scott Ullrich
	   this list of conditions and the following disclaimer.
22 cfc707f7 Scott Ullrich
23 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
24 ac3f8318 Espen Johansen
	   notices, this list of conditions and the following disclaimer in the
25 5b237745 Scott Ullrich
	   documentation and/or other materials provided with the distribution.
26 cfc707f7 Scott Ullrich
27 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
28
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
	POSSIBILITY OF SUCH DAMAGE.
37 523855b0 Scott Ullrich
38 b0c6a4f1 Ermal
	pfSense_BUILDER_BINARIES:	/sbin/dhclient	/bin/sh	/usr/bin/grep	/usr/bin/xargs	/usr/bin/awk	/usr/local/sbin/choparp
39 89c52814 Ermal
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig	/sbin/route	/usr/sbin/ngctl	/usr/sbin/arp	/bin/kill	/usr/local/sbin/mpd5
40 7149c4e7 Seth Mos
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/dhcp6c
41 523855b0 Scott Ullrich
	pfSense_MODULE:	interfaces
42
43 5b237745 Scott Ullrich
*/
44
45
/* include all configuration functions */
46 7387844e Chris Buechler
require_once("globals.inc");
47 483e6de8 Scott Ullrich
require_once("cmd_chain.inc");
48 9a456170 Darren Embry
require_once("util.inc");
49 5b237745 Scott Ullrich
50 b5b957fe Scott Ullrich
function interfaces_bring_up($interface) {
51
	if(!$interface) {
52 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("interfaces_bring_up() was called but no variable defined."));
53 ec054b7c Scott Ullrich
		log_error( "Backtrace: " . debug_backtrace() );
54 b5b957fe Scott Ullrich
		return;
55
	}
56 871768cf Ermal
	pfSense_interface_flags($interface, IFF_UP);
57 b5b957fe Scott Ullrich
}
58
59 52947718 Ermal Lu?i
/*
60
 * Return the interface array
61
 */
62
function get_interface_arr($flush = false) {
63
        global $interface_arr_cache;
64
65
        /* If the cache doesn't exist, build it */
66
        if (!isset($interface_arr_cache) or $flush)
67 d9adca3b Ermal
                $interface_arr_cache = pfSense_interface_listget();
68 52947718 Ermal Lu?i
69
        return $interface_arr_cache;
70
}
71
72
/*
73
 * does_interface_exist($interface): return true or false if a interface is
74
 * detected.
75
 */
76
function does_interface_exist($interface) {
77 8256f324 gnhb
	global $config;
78
	
79
	if(!$interface)
80 72993196 Ermal
		return false;
81 52947718 Ermal Lu?i
82 72993196 Ermal
	$ints = get_interface_arr(true);
83 6d5446a2 Ermal
	if (in_array($interface, $ints))
84 8256f324 gnhb
		return true;
85
	else
86
		return false;
87 52947718 Ermal Lu?i
}
88
89 2708a5cf Ermal
/*
90
 * does_vip_exist($vip): return true or false if a vip is
91
 * configured.
92
 */
93
function does_vip_exist($vip) {
94
	global $config;
95
	
96
	if(!$vip)
97
		return false;
98
99
100 b526daaf Ermal
	switch ($vip['mode']) {
101 2708a5cf Ermal
	case "carp":
102
	case "carpdev":
103 7b47bd4c Ermal
		$realif = "{$vip['interface']}_vip{$vip['vhid']}";
104 b526daaf Ermal
		if (!does_interface_exist($realif)) {
105
			return false;
106
		}
107
		break;
108 2708a5cf Ermal
	case "ipalias":
109 b526daaf Ermal
		$realif = get_real_interface($vip['interface']);
110
		if (!does_interface_exist($realif)) {
111
			return false;
112 2708a5cf Ermal
		}
113
		break;
114
	case "proxyarp":
115
		/* XXX: Implement this */
116 b526daaf Ermal
	default:
117
		return false;
118
	}
119
120
	$ifacedata = pfSense_getall_interface_addresses($realif);
121
	foreach ($ifacedata as $vipips) {
122
		if ($vipips == "{$vip['subnet']}/{$vip['subnet_bits']}")
123
			return true;
124 2708a5cf Ermal
	}
125
126
	return false;
127
}
128
129 67b057a9 Ermal
function interface_netgraph_needed($interface = "wan") {
130
	global $config;
131
132
	$found = false;
133
	if (!empty($config['pptpd']) &&
134
		$config['pptpd']['mode'] == "server")
135
		$found = true;
136
	if ($found == false && !empty($config['l2tp']) &&
137
		$config['l2tp']['mode'] == "server")
138
		$found = true;
139
	if ($found == false && is_array($config['pppoes']['pppoe'])) {
140
		foreach ($config['pppoes']['pppoe'] as $pppoe) {
141
			if ($pppoe['mode'] != "server")
142
				continue;
143
			if ($pppoe['interface'] == $interface)
144
				$found = true;
145
				break;
146
		}
147
	}
148 3dfc2d1a Ermal
	if ($found == false) {
149
		if (!empty($config['interfaces'][$interface])) {
150
			switch ($config['interfaces'][$interface]['ipaddr']) {
151
			case "ppp":
152
			case "pppoe":
153
			case "l2tp":
154
			case "pptp":
155
				$found = true;
156
				break;
157
			default:
158
				$found = false;
159
				break;
160
			}
161 9d7d2388 Ermal
		}
162
	}
163
	if ($found == false) {
164
		$realif = get_real_interface($interface);
165
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
166
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
167 20cb9803 gnhb
168
/* This if block doesn't do anything. It can be deleted.
169
PPP interfaces are found above in the previous if ($found == false) block.
170
This block of code is only entered for OPTx interfaces that are configured for PPPoE modem access, so $realif != $ppp['if']
171
172 9d7d2388 Ermal
				if ($realif == $ppp['if']) {
173
					$found = true;
174
					break;
175 3dfc2d1a Ermal
				}
176 20cb9803 gnhb
*/			
177 3eb00b49 gnhb
				$ports = explode(',',$ppp['ports']);
178
				foreach($ports as $pid => $port){
179 20cb9803 gnhb
					$port = get_real_interface($port);
180 3eb00b49 gnhb
					if ($realif == $port) {
181
						$found = true;
182
						break;
183
					}
184 20cb9803 gnhb
					/* Find the parent interfaces of the vlans in the MLPPP configs 
185
					* there should be only one element in the array here 
186
					* -- this could be better . . . */
187
					$parent_if = get_parent_interface($port);
188
					if ($realif == $parent_if[0]) {
189
						$found = true;
190
						break;
191
					}
192 3eb00b49 gnhb
				}
193 9d7d2388 Ermal
			}
194 67b057a9 Ermal
		}
195
	}
196 20cb9803 gnhb
	
197 31eee4a6 Ermal
	if ($found == false) {
198
		$realif = get_real_interface($interface);
199 67b057a9 Ermal
		pfSense_ngctl_detach("{$realif}:", $realif);
200 31eee4a6 Ermal
	}
201 92a1c8e6 Ermal
	/* NOTE: We make sure for this on interface_ppps_configure()
202
	 *	no need to do it here agan.
203
	 *	else
204
	 *		pfSense_ngctl_attach(".", $realif);
205
	 */
206 67b057a9 Ermal
}
207
208 eba938e3 Scott Ullrich
function interfaces_loopback_configure() {
209 7734aea6 Andrew Thompson
	global $g;
210
211
	if ($g['platform'] == 'jail')
212
		return;
213 7a6f7c55 Scott Ullrich
	if($g['booting'])
214 07e40c1f Carlos Eduardo Ramos
		echo gettext("Configuring loopback interface...");
215 871768cf Ermal
	pfSense_interface_setaddress("lo0", "127.0.0.1");
216 b5b957fe Scott Ullrich
	interfaces_bring_up("lo0");
217 7a6f7c55 Scott Ullrich
	if($g['booting'])
218 07e40c1f Carlos Eduardo Ramos
		echo gettext("done.") . "\n";
219 5b237745 Scott Ullrich
	return 0;
220
}
221
222 eba938e3 Scott Ullrich
function interfaces_vlan_configure() {
223 7a6f7c55 Scott Ullrich
	global $config, $g;
224 87519eb7 Scott Ullrich
	if($g['booting'])
225 07e40c1f Carlos Eduardo Ramos
		echo gettext("Configuring VLAN interfaces...");
226 5b6eac01 Scott Ullrich
	if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
227 e1c449c0 Ermal Lu?i
		foreach ($config['vlans']['vlan'] as $vlan) {
228 f620d00d Ermal Luçi
			if(empty($vlan['vlanif']))
229 48315e65 Ermal Luci
				$vlan['vlanif'] = "{$vlan['if']}_vlan{$vlan['tag']}";
230 5b6eac01 Scott Ullrich
			/* XXX: Maybe we should report any errors?! */
231 5f1e1d26 Ermal Lu?i
			interface_vlan_configure($vlan);
232 517feb1c Seth Mos
		}
233 5b6eac01 Scott Ullrich
	}
234 87519eb7 Scott Ullrich
	if($g['booting'])
235 07e40c1f Carlos Eduardo Ramos
		echo gettext("done.") . "\n";
236 2075fadb Ermal Luçi
}
237 cfc707f7 Scott Ullrich
238 abcb2bed Ermal Lu?i
function interface_vlan_configure(&$vlan) {
239 2075fadb Ermal Luçi
        global $config, $g;
240 161040eb Scott Ullrich
241 5f1e1d26 Ermal Lu?i
	if (!is_array($vlan)) {
242 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("VLAN: called with wrong options. Problems with config!"));
243 5f1e1d26 Ermal Lu?i
		return;
244
	}
245
	$if = $vlan['if'];
246 48315e65 Ermal Luci
	$vlanif  = empty($vlan['vlanif']) ? "{$if}_vlan{$vlan['tag']}" : $vlan['vlanif'];
247 5f1e1d26 Ermal Lu?i
	$tag = $vlan['tag'];
248
249 871768cf Ermal
	if (empty($if)) {
250 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("interface_vlan_confgure called with if undefined."));
251 3ae4960c Ermal Luçi
		return;
252
	}
253
254 37a53d16 Scott Ullrich
	/* make sure the parent interface is up */
255 07101b63 Ermal Luçi
	interfaces_bring_up($if);
256
	/* Since we are going to add vlan(4) try to enable all that hardware supports. */
257 871768cf Ermal
	pfSense_interface_capabilities($if, IFCAP_VLAN_HWTAGGING|IFCAP_VLAN_MTU|IFCAP_VLAN_HWFILTER);
258 cfc707f7 Scott Ullrich
259 4aca19b3 Scott Ullrich
	if (!empty($vlanif) && does_interface_exist($vlanif)) {
260 df2a0f18 Ermal
		interface_bring_down($vlanif, true);
261 4aca19b3 Scott Ullrich
	} else {
262 871768cf Ermal
		$tmpvlanif = pfSense_interface_create("vlan");
263
		pfSense_interface_rename($tmpvlanif, $vlanif);
264
		pfSense_ngctl_name("{$tmpvlanif}:", $vlanif);
265 abcb2bed Ermal Lu?i
	}
266 871768cf Ermal
267
	pfSense_vlan_create($vlanif, $if, $tag);
268 2075fadb Ermal Luçi
269 07101b63 Ermal Luçi
	interfaces_bring_up($vlanif);
270 cfc707f7 Scott Ullrich
271 40b0b541 Ermal Lu?i
	/* invalidate interface cache */
272
	get_interface_arr(true);
273 3f7d2120 Bill Marquette
274 4aca19b3 Scott Ullrich
	/* XXX: ermal -- for now leave it here at the moment it does not hurt. */
275 07101b63 Ermal Luçi
	interfaces_bring_up($if);
276 cfc707f7 Scott Ullrich
277 4aca19b3 Scott Ullrich
	return $vlanif;
278 5b237745 Scott Ullrich
}
279
280 abcb2bed Ermal Lu?i
function interface_qinq_configure(&$vlan, $fd = NULL) {
281 5f1e1d26 Ermal Lu?i
        global $config, $g;
282
283 c1289cfd Ermal Lu?i
        if (!is_array($vlan)) {
284 07e40c1f Carlos Eduardo Ramos
                log_error(sprintf(gettext("QinQ compat VLAN: called with wrong options. Problems with config!%s"), "\n"));
285 5f1e1d26 Ermal Lu?i
                return;
286
        }
287
288 42bad812 Ermal Lu?i
        $qinqif = $vlan['if'];
289 c1289cfd Ermal Lu?i
        $tag = $vlan['tag'];
290 a726c0e8 Ermal Lu?i
        if(empty($qinqif)) {
291 07e40c1f Carlos Eduardo Ramos
                log_error(sprintf(gettext("interface_qinq_confgure called with if undefined.%s"), "\n"));
292 c1289cfd Ermal Lu?i
                return;
293
        }
294 4400ad66 Ermal Lu?i
	$vlanif = interface_vlan_configure($vlan);
295 5f1e1d26 Ermal Lu?i
296 c1289cfd Ermal Lu?i
        if ($fd == NULL) {
297
                $exec = true;
298
                $fd = fopen("{$g['tmp_path']}/netgraphcmd", "w");
299
        } else
300
                $exec = false;
301 5f1e1d26 Ermal Lu?i
        /* make sure the parent is converted to ng_vlan(4) and is up */
302 42bad812 Ermal Lu?i
        interfaces_bring_up($qinqif);
303 5f1e1d26 Ermal Lu?i
304 9cf46050 Ermal
	pfSense_ngctl_attach(".", $qinqif);
305 abcb2bed Ermal Lu?i
        if (!empty($vlanif) && does_interface_exist($vlanif)) {
306 42bad812 Ermal Lu?i
                fwrite($fd, "shutdown {$qinqif}qinq:\n");
307
                exec("/usr/sbin/ngctl msg {$qinqif}qinq: gettable", $result);
308 c1289cfd Ermal Lu?i
                if (empty($result)) {
309 42bad812 Ermal Lu?i
                        fwrite($fd, "mkpeer {$qinqif}: vlan lower downstream\n");
310 4400ad66 Ermal Lu?i
                        fwrite($fd, "name {$qinqif}:lower {$vlanif}qinq\n");
311
                        fwrite($fd, "connect {$qinqif}: {$vlanif}qinq: upper nomatch\n");
312 c1289cfd Ermal Lu?i
                }
313 5f1e1d26 Ermal Lu?i
        } else {
314 42bad812 Ermal Lu?i
                fwrite($fd, "mkpeer {$qinqif}: vlan lower downstream\n");
315 4400ad66 Ermal Lu?i
                fwrite($fd, "name {$qinqif}:lower {$vlanif}qinq\n");
316
                fwrite($fd, "connect {$qinqif}: {$vlanif}qinq: upper nomatch\n");
317 c1289cfd Ermal Lu?i
        }
318 5f1e1d26 Ermal Lu?i
319
        /* invalidate interface cache */
320
        get_interface_arr(true);
321
322 42bad812 Ermal Lu?i
        if (!stristr($qinqif, "vlan"))
323
                mwexec("/sbin/ifconfig {$qinqif} promisc\n");
324 5f1e1d26 Ermal Lu?i
325 4400ad66 Ermal Lu?i
        $macaddr = get_interface_mac($qinqif);
326 c1289cfd Ermal Lu?i
        if (!empty($vlan['members'])) {
327
                $members = explode(" ", $vlan['members']);
328
                foreach ($members as $qtag) {
329
                        $qinq = array();
330 5f1e1d26 Ermal Lu?i
                        $qinq['tag'] = $qtag;
331
                        $qinq['if'] = $vlanif;
332 c1289cfd Ermal Lu?i
                        interface_qinq2_configure($qinq, $fd, $macaddr);
333
                }
334
        }
335
        if ($exec == true) {
336
                fclose($fd);
337
                mwexec("/usr/sbin/ngctl -f {$g['tmp_path']}/netgraphcmd");
338
        }
339
340 42bad812 Ermal Lu?i
        interfaces_bring_up($qinqif);
341 c1289cfd Ermal Lu?i
        if (!empty($vlan['members'])) {
342
                $members = explode(" ", $vlan['members']);
343
                foreach ($members as $qif)
344 4400ad66 Ermal Lu?i
                        interfaces_bring_up("{$vlanif}_{$qif}");
345 c1289cfd Ermal Lu?i
        }
346 5f1e1d26 Ermal Lu?i
347
        return $vlanif;
348
}
349
350
function interfaces_qinq_configure() {
351 7a6f7c55 Scott Ullrich
	global $config, $g;
352
	if($g['booting'])
353 07e40c1f Carlos Eduardo Ramos
		echo gettext("Configuring QinQ interfaces...");
354 7a6f7c55 Scott Ullrich
	if (is_array($config['qinqs']['qinqentry']) && count($config['qinqs']['qinqentry'])) {
355
		foreach ($config['qinqs']['qinqentry'] as $qinq) {
356
			/* XXX: Maybe we should report any errors?! */
357 4400ad66 Ermal Lu?i
			interface_qinq_configure($qinq);
358 7a6f7c55 Scott Ullrich
		}
359 4400ad66 Ermal Lu?i
	}
360
	if($g['booting'])
361 07e40c1f Carlos Eduardo Ramos
		echo gettext( "done.") . "\n";
362 5f1e1d26 Ermal Lu?i
}
363
364 abcb2bed Ermal Lu?i
function interface_qinq2_configure(&$qinq, $fd, $macaddr) {
365 c1289cfd Ermal Lu?i
        global $config, $g;
366 5f1e1d26 Ermal Lu?i
367
        if (!is_array($qinq)) {
368 07e40c1f Carlos Eduardo Ramos
                log_error(sprintf(gettext("QinQ compat VLAN: called with wrong options. Problems with config!%s"), "\n"));
369 5f1e1d26 Ermal Lu?i
                return;
370
        }
371
372
        $if = $qinq['if'];
373
        $tag = $qinq['tag'];
374 c1289cfd Ermal Lu?i
        $vlanif = "{$if}_{$tag}";
375 5f1e1d26 Ermal Lu?i
        if(empty($if)) {
376 07e40c1f Carlos Eduardo Ramos
                log_error(sprintf(gettext("interface_qinq_confgure called with if undefined.%s"), "\n"));
377 5f1e1d26 Ermal Lu?i
                return;
378
        }
379
380 4400ad66 Ermal Lu?i
        fwrite($fd, "shutdown {$if}h{$tag}:\n");
381 c1289cfd Ermal Lu?i
        fwrite($fd, "mkpeer {$if}qinq: eiface {$if}{$tag} ether\n");
382
        fwrite($fd, "name {$if}qinq:{$if}{$tag} {$if}h{$tag}\n");
383
        fwrite($fd, "msg {$if}qinq: addfilter { vlan={$tag} hook=\"{$if}{$tag}\" }\n");
384
        fwrite($fd, "msg {$if}h{$tag}: setifname \"{$vlanif}\"\n");
385 4400ad66 Ermal Lu?i
        fwrite($fd, "msg {$if}h{$tag}: set {$macaddr}\n");
386 5f1e1d26 Ermal Lu?i
387 c1289cfd Ermal Lu?i
        /* invalidate interface cache */
388 5f1e1d26 Ermal Lu?i
        get_interface_arr(true);
389
390
        return $vlanif;
391
}
392
393 9f428275 Erik Fonnesbeck
function interfaces_create_wireless_clones() {
394
	global $config;
395
396
	if($g['booting'])
397 07e40c1f Carlos Eduardo Ramos
		echo gettext("Creating other wireless clone interfaces...");
398 9f428275 Erik Fonnesbeck
	if (is_array($config['wireless']['clone']) && count($config['wireless']['clone'])) {
399
		foreach ($config['wireless']['clone'] as $clone) {
400
			if(empty($clone['cloneif']))
401
				continue;
402
			if(does_interface_exist($clone['cloneif']))
403
				continue;
404
			/* XXX: Maybe we should report any errors?! */
405
			if(interface_wireless_clone($clone['cloneif'], $clone))
406
				if($g['booting'])
407
					echo " " . $clone['cloneif'];
408
		}
409
	}
410
	if($g['booting'])
411 07e40c1f Carlos Eduardo Ramos
		echo " " . gettext("done.") . "\n";
412 a687f866 Namezero
413 9f428275 Erik Fonnesbeck
}
414
415 d7f1891b Ermal
function interfaces_bridge_configure($checkmember = 0) {
416 bad29bc6 Ermal Luçi
        global $config;
417
418
        $i = 0;
419 3134528d Ermal Luçi
        if (is_array($config['bridges']['bridged']) && count($config['bridges']['bridged'])) {
420
                foreach ($config['bridges']['bridged'] as $bridge) {
421 f620d00d Ermal Luçi
                        if(empty($bridge['bridgeif']))
422 bad29bc6 Ermal Luçi
                                $bridge['bridgeif'] = "bridge{$i}";
423
                        /* XXX: Maybe we should report any errors?! */
424 02de5c07 Ermal
                        interface_bridge_configure($bridge, $checkmember);
425 bad29bc6 Ermal Luçi
                        $i++;
426
                }
427
        }
428
}
429
430 02de5c07 Ermal
function interface_bridge_configure(&$bridge, $checkmember = 0) {
431 d7147b1c Scott Ullrich
	global $config, $g;
432 bad29bc6 Ermal Luçi
433 d7147b1c Scott Ullrich
	if (!is_array($bridge))
434
	        return -1;
435 bad29bc6 Ermal Luçi
436 dc97efaf Ermal Luçi
	if (empty($bridge['members'])) {
437 07e40c1f Carlos Eduardo Ramos
		log_error(sprintf(gettext("No members found on %s"), $bridge['bridgeif']));
438 dc97efaf Ermal Luçi
		return -1;
439
	}
440
441 bad29bc6 Ermal Luçi
	$members = explode(',', $bridge['members']);
442 70720671 Ermal Luçi
	if (!count($members))
443 bad29bc6 Ermal Luçi
		return -1;
444 ea5f6c95 Ermal
445 b64523c1 Ermal Luçi
	/* Calculate smaller mtu and enforce it */
446 69e53ef0 Ermal Luçi
	$smallermtu = 0;
447 07676e36 Ermal
	$commonrx = true;
448
	$commontx = true;
449 02de5c07 Ermal
	$foundgif = false;
450 b64523c1 Ermal Luçi
	foreach ($members as $member) {
451
		$realif = get_real_interface($member);
452 07676e36 Ermal
		$opts = pfSense_get_interface_addresses($realif);
453
		$mtu = $opts['mtu'];
454 02de5c07 Ermal
		if (substr($realif, 0, 3) == "gif") {
455
			$foundgif = true;
456
			if ($checkmember == 1)
457
				return;
458
			if ($mtu <= 1500)
459
				continue;
460
		}
461 07676e36 Ermal
		if (!isset($opts['encaps']['txcsum']))
462
			$commontx = false;
463
		if (!isset($opts['encaps']['rxcsum']))
464
			$commonrx = false;
465 0ac206f9 Ermal
		if (!isset($opts['encaps']['tso4']))
466
			$commontso4 = false;
467
		if (!isset($opts['encaps']['tso6']))
468
			$commontso6 = false;
469
		if (!isset($opts['encaps']['lro']))
470
			$commonlro = false;
471 69e53ef0 Ermal Luçi
		if ($smallermtu == 0 && !empty($mtu))
472
			$smallermtu = $mtu;
473
		else if (!empty($mtu) && $mtu < $smallermtu)
474 b64523c1 Ermal Luçi
			$smallermtu = $mtu;
475
	}
476 02de5c07 Ermal
	if ($foundgif == false && $checkmember == 2)
477
		return;
478
479 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
480
	if ($smallermtu == 0)
481
		$smallermtu = 1500; 
482
483 07676e36 Ermal
	$flags = 0;
484 0ac206f9 Ermal
	if ($commonrx === false)
485 07676e36 Ermal
		$flags |= IFCAP_RXCSUM;
486 0ac206f9 Ermal
	if ($commontx === false)
487 07676e36 Ermal
		$flags |= IFCAP_TXCSUM;
488 0ac206f9 Ermal
	if ($commontso4 === false)
489
		$flags |= IFCAP_TSO4;
490
	if ($commontso6 === false)
491
		$flags |= IFCAP_TSO6;
492
	if ($commonlro === false)
493
		$flags |= IFCAP_LRO;
494 02de5c07 Ermal
495
	if ($g['booting'] || !empty($bridge['bridgeif'])) {
496
		pfSense_interface_destroy($bridge['bridgeif']);
497
		pfSense_interface_create($bridge['bridgeif']);
498
		$bridgeif = $bridge['bridgeif'];
499
	} else
500
		$bridgeif = pfSense_interface_create("bridge");
501
502
	$checklist = get_configured_interface_list();
503
504 bad29bc6 Ermal Luçi
	/* Add interfaces to bridge */
505 31241000 Ermal Luçi
	foreach ($members as $member) {
506 d7147b1c Scott Ullrich
		if (!array_key_exists($member, $checklist))
507
			continue;
508 9ecce49f Ermal Lu?i
		$realif1 = get_real_interface($member);
509
		$realif =  escapeshellarg($realif1);
510 07676e36 Ermal
		if (!$realif) {
511 07e40c1f Carlos Eduardo Ramos
			log_error(gettext("realif not defined in interfaces bridge - up"));
512 07676e36 Ermal
			continue;
513
		}
514
		/* make sure the parent interface is up */
515
		pfSense_interface_mtu($realif1, $smallermtu);
516 51d5aad7 Ermal
		pfSense_interface_capabilities($realif1, -$flags);
517 9ecce49f Ermal Lu?i
		interfaces_bring_up($realif1);
518 2064fa2e Ermal
		pfSense_bridge_add_member($bridgeif, $realif);
519 d7147b1c Scott Ullrich
	}
520 31241000 Ermal Luçi
521 bad29bc6 Ermal Luçi
	if (isset($bridge['enablestp'])) {
522
		/* Choose spanning tree proto */
523
		mwexec("/sbin/ifconfig {$bridgeif} proto {$bridge['proto']}");	
524
		
525 dc97efaf Ermal Luçi
		if (!empty($bridge['stp'])) {
526
			$stpifs = explode(',', $bridge['stp']);
527
			foreach ($stpifs as $stpif) {
528
				$realif = get_real_interface($stpif);
529
				mwexec("/sbin/ifconfig {$bridgeif} stp {$realif}");
530
			}
531 bad29bc6 Ermal Luçi
		}
532 dc97efaf Ermal Luçi
		if (!empty($bridge['maxage']))
533 bad29bc6 Ermal Luçi
			mwexec("/sbin/ifconfig {$bridgeif} maxage {$bridge['maxage']}");
534 a5571287 Chris Buechler
		if (!empty($bridge['fwdelay']))
535 bad29bc6 Ermal Luçi
			mwexec("/sbin/ifconfig {$bridgeif} fwddelay {$bridge['fwdelay']}");
536 a5571287 Chris Buechler
		if (!empty($bridge['hellotime']))
537 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} hellotime {$bridge['hellotime']}");
538 a5571287 Chris Buechler
		if (!empty($bridge['priority']))
539 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} priority {$bridge['priority']}");
540 a5571287 Chris Buechler
		if (!empty($bridge['holdcount']))
541 bad29bc6 Ermal Luçi
                        mwexec("/sbin/ifconfig {$bridgeif} holdcnt {$bridge['holdcnt']}");
542 dc97efaf Ermal Luçi
		if (!empty($bridge['ifpriority'])) {
543
			$pconfig = explode(",", $bridge['ifpriority']);
544
			$ifpriority = array();
545
			foreach ($pconfig as $cfg) {
546 9a456170 Darren Embry
				$embcfg = explode_assoc(":", $cfg);
547 dc97efaf Ermal Luçi
				foreach ($embcfg as $key => $value)
548
					$ifpriority[$key] = $value;
549
			}
550
			foreach ($ifpriority as $key => $value) {
551
				$realif = get_real_interface($key);
552
				mwexec("/sbin/ifconfig ${bridgeif} ifpriority {$realif} {$value}"); 
553
			}
554 bad29bc6 Ermal Luçi
		}
555 dc97efaf Ermal Luçi
		if (!empty($bridge['ifpathcost'])) {
556 da5895bb Darren Embry
			$pconfig = explode(",", $bridge['ifpathcost']);
557 dc97efaf Ermal Luçi
			$ifpathcost = array();
558
			foreach ($pconfig as $cfg) {
559 9a456170 Darren Embry
				$embcfg = explode_assoc(":", $cfg);
560 dc97efaf Ermal Luçi
				foreach ($embcfg as $key => $value)
561
					$ifpathcost[$key] = $value;
562
			}
563
			foreach ($ifpathcost as $key => $value) {
564
                        	$realif = get_real_interface($key);
565
                        	mwexec("/sbin/ifconfig ${bridgeif} ifpathcost {$realif} {$value}");
566
                	}
567 bad29bc6 Ermal Luçi
		}
568
	}
569
570
	if ($bridge['maxaddr'] <> "")
571
		mwexec("/sbin/ifconfig {$bridgeif} maxaddr {$bridge['maxaddr']}");
572
        if ($bridge['timeout'] <> "")
573
                mwexec("/sbin/ifconfig {$bridgeif} timeout {$bridge['timeout']}");
574
        if ($bridge['span'] <> "") {
575 85a5da13 Ermal Luçi
		$realif = get_real_interface($bridge['span']);
576 bad29bc6 Ermal Luçi
                mwexec("/sbin/ifconfig {$bridgeif} span {$realif}");
577
	}
578 a47a5798 Ermal Luçi
	if (!empty($bridge['edge'])) {
579
        	$edgeifs = explode(',', $bridge['edge']);
580
        	foreach ($edgeifs as $edgeif) {
581
			$realif = get_real_interface($edgeif);
582
                	mwexec("/sbin/ifconfig {$bridgeif} edge {$realif}");
583
        	}
584
	}
585
	if (!empty($bridge['autoedge'])) {
586
        	$edgeifs = explode(',', $bridge['autoedge']);
587
        	foreach ($edgeifs as $edgeif) {
588
                	$realif = get_real_interface($edgeif);
589
                	mwexec("/sbin/ifconfig {$bridgeif} -autoedge {$realif}");
590
        	}
591
	}
592
	if (!empty($bridge['ptp'])) {
593
        	$ptpifs = explode(',', $bridge['ptp']);
594
        	foreach ($ptpifs as $ptpif) {
595
                	$realif = get_real_interface($ptpif);
596
                	mwexec("/sbin/ifconfig {$bridgeif} ptp {$realif}");
597
        	}
598
	}
599
	if (!empty($bridge['autoptp'])) {
600
        	$ptpifs = explode(',', $bridge['autoptp']);
601
        	foreach ($ptpifs as $ptpif) {
602
                	$realif = get_real_interface($ptpif);
603
                	mwexec("/sbin/ifconfig {$bridgeif} -autoptp {$realif}");
604
        	}
605
	}
606
	if (!empty($bridge['static'])) {
607
        	$stickyifs = explode(',', $bridge['static']);
608
        	foreach ($stickyifs as $stickyif) {
609
                	$realif = get_real_interface($stickyif);
610
                	mwexec("/sbin/ifconfig {$bridgeif} sticky {$realif}");
611
        	}
612
	}
613
	if (!empty($bridge['private'])) {
614
        	$privateifs = explode(',', $bridge['private']);
615
        	foreach ($privateifs as $privateif) {
616
                	$realif = get_real_interface($privateif);
617
               	 	mwexec("/sbin/ifconfig {$bridgeif} private {$realif}");
618
        	}
619
	}
620 bad29bc6 Ermal Luçi
621 d7147b1c Scott Ullrich
	if($bridgeif)
622 b5b957fe Scott Ullrich
		interfaces_bring_up($bridgeif);	
623 d7147b1c Scott Ullrich
	else 
624 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("bridgeif not defined -- could not bring interface up"));
625 bad29bc6 Ermal Luçi
626 d7147b1c Scott Ullrich
	return $bridgeif;
627 bad29bc6 Ermal Luçi
}
628
629 fcd4a425 Ermal Lu?i
function interface_bridge_add_member($bridgeif, $interface) {
630
631
	if (!does_interface_exist($bridgeif) || !does_interface_exist($interface))
632
		return;
633
634 a5571287 Chris Buechler
	$mtu = get_interface_mtu($bridgeif);
635 fcd4a425 Ermal Lu?i
	$mtum = get_interface_mtu($interface);
636
	
637 73481ad3 Ermal
	if ($mtu != $mtum && !(substr($interface, 0, 3) == "gif" && $mtu <= 1500))
638 871768cf Ermal
		pfSense_interface_mtu($interface, $mtu);
639 fcd4a425 Ermal Lu?i
640 0c77c314 Ermal
	$options = pfSense_get_interface_addresses($bridgeif);
641 51d5aad7 Ermal
	$flags = 0;
642
	if (!isset($options['encaps']['txcsum']))
643
		$flags |= IFCAP_TXCSUM;
644 ea5f6c95 Ermal
645 51d5aad7 Ermal
	if (!isset($options['encaps']['rxcsum']))
646
		$flags |= IFCAP_RXCSUM;
647
648
	pfSense_interface_capabilities($interface, -$flags);
649 3ca774ac Ermal
650 fcd4a425 Ermal Lu?i
	interfaces_bring_up($interface);
651 2064fa2e Ermal
	pfSense_bridge_add_member($bridgeif, $interface);
652 fcd4a425 Ermal Lu?i
}
653
654 f620d00d Ermal Luçi
function interfaces_lagg_configure() 
655
{
656 7a6f7c55 Scott Ullrich
        global $config, $g;
657
		if($g['booting']) 
658 07e40c1f Carlos Eduardo Ramos
			echo gettext("Configuring LAGG interfaces...");
659 cccf624b Ermal Luçi
        $i = 0;
660 7a6f7c55 Scott Ullrich
		if (is_array($config['laggs']['lagg']) && count($config['laggs']['lagg'])) {
661
			foreach ($config['laggs']['lagg'] as $lagg) {
662
				if(empty($lagg['laggif']))
663
					$lagg['laggif'] = "lagg{$i}";
664
				/* XXX: Maybe we should report any errors?! */
665
				interface_lagg_configure($lagg);
666
				$i++;
667
			}
668
		}
669
		if($g['booting']) 
670 07e40c1f Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
671 cccf624b Ermal Luçi
}
672
673 eba938e3 Scott Ullrich
function interface_lagg_configure(&$lagg) {
674 cccf624b Ermal Luçi
        global $config, $g;
675
676
        if (!is_array($lagg))
677
		return -1;
678
679
	$members = explode(',', $lagg['members']);
680
	if (!count($members))
681
		return -1;
682
	
683 b64523c1 Ermal Luçi
	if ($g['booting'] || !(empty($lagg['laggif']))) {
684 871768cf Ermal
		pfSense_interface_destroy($lagg['laggif']);
685
		pfSense_interface_create($lagg['laggif']);
686 b64523c1 Ermal Luçi
                $laggif = $lagg['laggif'];
687
        } else
688 871768cf Ermal
		$laggif = pfSense_interface_create("lagg");
689 b64523c1 Ermal Luçi
690
	/* Calculate smaller mtu and enforce it */
691 69e53ef0 Ermal Luçi
        $smallermtu = 0;
692 b64523c1 Ermal Luçi
        foreach ($members as $member) {
693 0ac206f9 Ermal
		$opts = pfSense_get_interface_addresses($member);
694
                $mtu = $opts['mtu'];
695
		if (!isset($opts['encaps']['txcsum']))
696
                        $commontx = false;
697
                if (!isset($opts['encaps']['rxcsum']))
698
                        $commonrx = false;
699
		if (!isset($opts['encaps']['tso4']))
700
			$commontso4 = false;
701
		if (!isset($opts['encaps']['tso6']))
702
			$commontso6 = false;
703
		if (!isset($opts['encaps']['lro']))
704
			$commonlro = false;
705 69e53ef0 Ermal Luçi
		if ($smallermtu == 0 && !empty($mtu))
706
			$smallermtu = $mtu;
707
                else if (!empty($mtu) && $mtu < $smallermtu)
708 b64523c1 Ermal Luçi
                        $smallermtu = $mtu;
709
        }
710
711 69e53ef0 Ermal Luçi
	/* Just in case anything is not working well */
712
        if ($smallermtu == 0)
713
                $smallermtu = 1500;
714
715 0ac206f9 Ermal
	$flags = 0;
716
        if ($commonrx === false)
717
                $flags |= IFCAP_RXCSUM;
718
        if ($commontx === false)
719
                $flags |= IFCAP_TXCSUM;
720
	if ($commontso4 === false)
721
                $flags |= IFCAP_TSO4;
722
        if ($commontso6 === false)
723
                $flags |= IFCAP_TSO6;
724
        if ($commonlro === false)
725
                $flags |= IFCAP_LRO;
726
727 02de5c07 Ermal
	$checklist = get_interface_list();
728
729 cccf624b Ermal Luçi
	foreach ($members as $member) {
730
		if (!array_key_exists($member, $checklist))
731
			continue;
732 d7147b1c Scott Ullrich
		/* make sure the parent interface is up */
733 871768cf Ermal
		pfSense_interface_mtu($member, $smallermtu);
734 0ac206f9 Ermal
		pfSense_interface_capabilities($member, -$flags);
735 39fbee97 Ermal Lu?i
		interfaces_bring_up($member);
736 f421cbcc Ermal Lu?i
		mwexec("/sbin/ifconfig {$laggif} laggport {$member}");
737 cccf624b Ermal Luçi
	}
738 b5b957fe Scott Ullrich
	
739 39fbee97 Ermal Lu?i
	mwexec("/sbin/ifconfig {$laggif} laggproto {$lagg['proto']}");
740 acc1e9d0 Scott Ullrich
741 b5b957fe Scott Ullrich
	interfaces_bring_up($laggif);
742 cccf624b Ermal Luçi
743 d7147b1c Scott Ullrich
	return $laggif;
744 cccf624b Ermal Luçi
}
745
746 d7f1891b Ermal
function interfaces_gre_configure($checkparent = 0) {
747 582d2452 Ermal Luçi
        global $config;
748
749
        if (is_array($config['gres']['gre']) && count($config['gres']['gre'])) {
750 f1a93dee Ermal
                foreach ($config['gres']['gre'] as $i => $gre) {
751 f620d00d Ermal Luçi
                        if(empty($gre['greif']))
752 582d2452 Ermal Luçi
                                $gre['greif'] = "gre{$i}";
753 7b47bd4c Ermal
			if ($checkparent == 1 && strstr($gre['if'], "_vip"))
754 d7f1891b Ermal
				continue;
755 7b47bd4c Ermal
			if ($checkparent == 2 && !strstr($gre['if'], "_vip"))
756 d7f1891b Ermal
				continue;
757 582d2452 Ermal Luçi
                        /* XXX: Maybe we should report any errors?! */
758
                        interface_gre_configure($gre);
759
                }
760
        }
761
}
762
763 ed62880b Ermal
/* NOTE: $grekey is not used but useful for passing this function to array_walk. */
764
function interface_gre_configure(&$gre, $grekey = "") {
765 582d2452 Ermal Luçi
        global $config, $g;
766
767
	if (!is_array($gre))
768
		return -1;
769
770 85a5da13 Ermal Luçi
	$realif = get_real_interface($gre['if']);
771
	$realifip = get_interface_ip($gre['if']);
772 582d2452 Ermal Luçi
773 d7147b1c Scott Ullrich
	/* make sure the parent interface is up */
774 b5b957fe Scott Ullrich
	interfaces_bring_up($realif);
775 582d2452 Ermal Luçi
776 d7147b1c Scott Ullrich
	if ($g['booting'] || !(empty($gre['greif']))) {
777 871768cf Ermal
		pfSense_interface_destroy($gre['greif']);
778
		pfSense_interface_create($gre['greif']);
779 582d2452 Ermal Luçi
		$greif = $gre['greif'];
780 871768cf Ermal
	} else
781
		$greif = pfSense_interface_create("gre");
782 582d2452 Ermal Luçi
783
	/* Do not change the order here for more see gre(4) NOTES section. */
784
	mwexec("/sbin/ifconfig {$greif} tunnel {$realifip} {$gre['remote-addr']}");
785 a59c7fa6 smos
	if((is_ipaddrv6($gre['tunnel-local-addr'])) || (is_ipaddrv6($gre['tunnel-remote-addr']))) {
786
		mwexec("/sbin/ifconfig {$greif} inet6 {$gre['tunnel-local-addr']} {$gre['tunnel-remote-addr']} prefixlen /{$gre['tunnel-remote-net']} ");
787
	} else {
788
		mwexec("/sbin/ifconfig {$greif} {$gif['tunnel-local-addr']} {$gre['tunnel-remote-addr']} netmask " . gen_subnet_mask($gre['tunnel-remote-net']));
789
	}
790 582d2452 Ermal Luçi
	if (isset($gre['link0']) && $gre['link0'])
791 871768cf Ermal
		pfSense_interface_flags($greif, IFF_LINK0);
792 d7147b1c Scott Ullrich
	if (isset($gre['link1']) && $gre['link1'])
793 871768cf Ermal
		pfSense_interface_flags($greif, IFF_LINK1);
794 d7147b1c Scott Ullrich
	if (isset($gre['link2']) && $gre['link2'])
795 871768cf Ermal
		pfSense_interface_flags($greif, IFF_LINK2);
796 d7147b1c Scott Ullrich
797
	if($greif)
798 b5b957fe Scott Ullrich
		interfaces_bring_up($greif);
799 d7147b1c Scott Ullrich
	else 
800 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("Could not bring greif up -- variable not defined."));
801 582d2452 Ermal Luçi
802 53b0d9d3 Ermal Lu?i
	if (isset($gre['link1']) && $gre['link1'])
803 61b67ab3 Ermal Lu?i
		mwexec("/sbin/route add {$gre['tunnel-remote-addr']}/{$gre['tunnel-remote-net']} {$gre['tunnel-local-addr']}");
804 283e9180 Seth Mos
	if(is_ipaddrv4($gre['tunnel-remote-addr']))
805
		file_put_contents("{$g['tmp_path']}/{$greif}_router", $gre['tunnel-remote-addr']);
806
	if(is_ipaddrv6($gre['tunnel-remote-addr']))
807
		file_put_contents("{$g['tmp_path']}/{$greif}_routerv6", $gre['tunnel-remote-addr']);
808 582d2452 Ermal Luçi
809
	return $greif;
810
}
811
812 d7f1891b Ermal
function interfaces_gif_configure($checkparent = 0) {
813 9006e9f8 Scott Ullrich
	global $config;
814 f1a93dee Ermal
815 9006e9f8 Scott Ullrich
	if (is_array($config['gifs']['gif']) && count($config['gifs']['gif'])) {
816 f1a93dee Ermal
		foreach ($config['gifs']['gif'] as $i => $gif) {
817 9006e9f8 Scott Ullrich
			if(empty($gif['gifif']))
818
				$gre['gifif'] = "gif{$i}";
819 7b47bd4c Ermal
			if ($checkparent == 1 && strstr($gif['if'], "_vip"))
820 d7f1891b Ermal
				continue;
821 7b47bd4c Ermal
			if ($checkparent == 2 && !strstr($gif['if'], "_vip"))
822 d7f1891b Ermal
				continue;
823 9006e9f8 Scott Ullrich
			/* XXX: Maybe we should report any errors?! */
824
			interface_gif_configure($gif);
825
		}
826
	}
827 582d2452 Ermal Luçi
}
828
829 ed62880b Ermal
/* NOTE: $gifkey is not used but useful for passing this function to array_walk. */
830
function interface_gif_configure(&$gif, $gifkey = "") {
831 9006e9f8 Scott Ullrich
	global $config, $g;
832 582d2452 Ermal Luçi
833 9006e9f8 Scott Ullrich
	if (!is_array($gif))
834
		return -1;
835 582d2452 Ermal Luçi
836 9006e9f8 Scott Ullrich
	$realif = get_real_interface($gif['if']);
837 582d2452 Ermal Luçi
838 01a58d89 smos
	if(is_ipaddrv4($gif['remote-addr'])) {
839
		$realifip = get_interface_ip($gif['if']);
840
		$realifgw = get_interface_gateway($gif['if']);
841
	}
842
	if(is_ipaddrv6($gif['remote-addr'])) {
843
		$realifip = get_interface_ipv6($gif['if']);
844
		$realifgw = get_interface_gatewayv6($gif['if']);
845
	}
846 9006e9f8 Scott Ullrich
	/* make sure the parent interface is up */
847
	if($realif)
848
		interfaces_bring_up($realif);
849
	else 
850 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("could not bring realif up -- variable not defined -- interface_gif_configure()"));
851 582d2452 Ermal Luçi
852 9006e9f8 Scott Ullrich
	if ($g['booting'] || !(empty($gif['gifif']))) {
853 871768cf Ermal
		pfSense_interface_destroy($gif['gifif']);
854
		pfSense_interface_create($gif['gifif']);
855 9006e9f8 Scott Ullrich
		$gifif = $gif['gifif'];
856
	} else
857 871768cf Ermal
		$gifif = pfSense_interface_create("gif");
858 9006e9f8 Scott Ullrich
859
	/* Do not change the order here for more see gif(4) NOTES section. */
860
	mwexec("/sbin/ifconfig {$gifif} tunnel {$realifip} {$gif['remote-addr']}");
861 9b1ff028 Seth Mos
	if((is_ipaddrv6($gif['tunnel-local-addr'])) || (is_ipaddrv6($gif['tunnel-remote-addr']))) {
862 5a8371cd smos
		mwexec("/sbin/ifconfig {$gifif} inet6 {$gif['tunnel-local-addr']} {$gif['tunnel-remote-addr']} prefixlen /{$gif['tunnel-remote-net']} ");
863 9b1ff028 Seth Mos
	} else {
864
		mwexec("/sbin/ifconfig {$gifif} {$gif['tunnel-local-addr']} {$gif['tunnel-remote-addr']} netmask " . gen_subnet_mask($gif['tunnel-remote-net']));
865
	}
866 9006e9f8 Scott Ullrich
	if (isset($gif['link0']) && $gif['link0'])
867 871768cf Ermal
		pfSense_interface_flags($gifif, IFF_LINK0);
868 9006e9f8 Scott Ullrich
	if (isset($gif['link1']) && $gif['link1'])
869 871768cf Ermal
		pfSense_interface_flags($gifif, IFF_LINK1);
870 9006e9f8 Scott Ullrich
	if($gifif)
871
		interfaces_bring_up($gifif);
872
	else
873 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("could not bring gifif up -- variable not defined"));
874 9006e9f8 Scott Ullrich
875 7c0571ce Seth Mos
	$iflist = get_configured_interface_list();
876
	foreach($iflist as $ifname) {
877
		if($config['interfaces'][$ifname]['if'] == $gifif) {
878 e2b6e604 Seth Mos
			if(get_interface_gateway($ifname)) {
879
				system_routing_configure($ifname);
880
				break;
881
			}
882 7c0571ce Seth Mos
			if(get_interface_gateway_v6($ifname)) {
883
				system_routing_configure($ifname);
884
				break;
885
			}
886
		}
887
	}
888 283e9180 Seth Mos
889 a687f866 Namezero
890 283e9180 Seth Mos
	if(is_ipaddrv4($gif['tunnel-remote-addr']))
891
		file_put_contents("{$g['tmp_path']}/{$gifif}_router", $gif['tunnel-remote-addr']);
892
	if(is_ipaddrv6($gif['tunnel-remote-addr']))
893
		file_put_contents("{$g['tmp_path']}/{$gifif}_routerv6", $gif['tunnel-remote-addr']);
894 582d2452 Ermal Luçi
895 01a58d89 smos
        if (is_ipaddrv4($realifgw)) {
896
                mwexec("route change -host {$gif['remote-addr']} {$realifgw}");
897
        }
898
        if (is_ipaddrv6($realifgw)) {
899
                mwexec("route change -host -inet6 {$gif['remote-addr']} {$realifgw}");
900
        }
901
902 9006e9f8 Scott Ullrich
	return $gifif;
903 582d2452 Ermal Luçi
}
904
905 eba938e3 Scott Ullrich
function interfaces_configure() {
906 9b1c39e3 Ermal Luçi
	global $config, $g;
907
908 7734aea6 Andrew Thompson
	if ($g['platform'] == 'jail')
909
		return;
910
911 a5d6f60b Ermal Lu?i
	/* Set up our loopback interface */
912 4aca19b3 Scott Ullrich
	interfaces_loopback_configure();
913 a5d6f60b Ermal Lu?i
914 541b7c56 Scott Ullrich
	/* set up LAGG virtual interfaces */
915
	interfaces_lagg_configure();
916
917 acc1e9d0 Scott Ullrich
	/* set up VLAN virtual interfaces */
918
	interfaces_vlan_configure();
919
920 5f1e1d26 Ermal Lu?i
	interfaces_qinq_configure();
921
922 67ee1ec5 Ermal Luçi
	$iflist = get_configured_interface_with_descr();
923 9b1c39e3 Ermal Luçi
	$delayed_list = array();
924
	$bridge_list = array();
925 b6db9217 Ermal Luçi
	
926 871768cf Ermal
	/* This is needed to speedup interfaces on bootup. */
927
	$reload = false;
928
	if ($g['booting'])
929
		$reload = true;
930
931 67ee1ec5 Ermal Luçi
	foreach($iflist as $if => $ifname) {
932 0dc702f3 Ermal Lu?i
		$realif = $config['interfaces'][$if]['if'];
933 9b1c39e3 Ermal Luçi
		if (strstr($realif, "bridge")) 
934
			$bridge_list[$if] = $ifname;
935
		else if (strstr($realif, "gre"))
936
			$delayed_list[$if] = $ifname;
937
		else if (strstr($realif, "gif"))
938
			$delayed_list[$if] = $ifname;
939 d09d53ac Ermal
		else if (strstr($realif, "ovpn")) {
940
			//echo "Delaying OpenVPN interface configuration...done.\n";
941
			continue;
942
		} else {
943 9b1c39e3 Ermal Luçi
			if ($g['booting'])
944 07e40c1f Carlos Eduardo Ramos
				printf(gettext("Configuring %s interface..."), $ifname);
945 a687f866 Namezero
946 9006e9f8 Scott Ullrich
			if($g['debug'])
947 07e40c1f Carlos Eduardo Ramos
				log_error(sprintf(gettext("Configuring %s"), $ifname));
948 871768cf Ermal
			interface_configure($if, $reload);
949 9b1c39e3 Ermal Luçi
			if ($g['booting']) 
950 07e40c1f Carlos Eduardo Ramos
				echo gettext( "done.") . "\n";
951 9b1c39e3 Ermal Luçi
		}
952
	}
953
954 9f428275 Erik Fonnesbeck
	/* create the unconfigured wireless clones */
955
	interfaces_create_wireless_clones();
956
957 d7f1891b Ermal
	/*
958
	 * NOTE: The following function parameter consists of
959
	 *	1 - Do not load gre/gif/bridge with parent/member as vip
960
	 *	2 - Do load gre/gif/bridge with parent/member as vip
961
	 */
962
963 d7147b1c Scott Ullrich
	/* set up GRE virtual interfaces */
964 d7f1891b Ermal
	interfaces_gre_configure(1);
965 9b1c39e3 Ermal Luçi
966 d7147b1c Scott Ullrich
	/* set up GIF virtual interfaces */
967 d7f1891b Ermal
	interfaces_gif_configure(1);
968
969
	/* set up BRIDGe virtual interfaces */
970
	interfaces_bridge_configure(1);
971
972
	/* bring up vip interfaces */
973
	interfaces_vips_configure();
974
975
	/* set up GRE virtual interfaces */
976
	interfaces_gre_configure(2);
977
978
	/* set up GIF virtual interfaces */
979
	interfaces_gif_configure(2);
980
981 9b1c39e3 Ermal Luçi
	foreach ($delayed_list as $if => $ifname) {
982
		if ($g['booting'])
983 07e40c1f Carlos Eduardo Ramos
			printf(gettext("Configuring %s interface..."), $ifname);
984 a5d6f60b Ermal Lu?i
        	if ($g['debug'])
985 07e40c1f Carlos Eduardo Ramos
        		log_error(sprintf(gettext("Configuring %s"), $ifname));
986 67ee1ec5 Ermal Luçi
987 871768cf Ermal
		interface_configure($if, $reload);
988 4476d447 Ermal Luçi
989 9b1c39e3 Ermal Luçi
		if ($g['booting'])
990 07e40c1f Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
991 67ee1ec5 Ermal Luçi
	}
992 cfc707f7 Scott Ullrich
993 d7147b1c Scott Ullrich
	/* set up BRIDGe virtual interfaces */
994 d7f1891b Ermal
	interfaces_bridge_configure(2);
995 9b1c39e3 Ermal Luçi
996 d7147b1c Scott Ullrich
	foreach ($bridge_list as $if => $ifname) {
997
		if ($g['booting'])
998 07e40c1f Carlos Eduardo Ramos
			printf(gettext("Configuring %s interface..."), $ifname);
999 d7147b1c Scott Ullrich
		if($g['debug'])
1000 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Configuring %s"), $ifname));
1001 9b1c39e3 Ermal Luçi
1002 871768cf Ermal
		interface_configure($if, $reload);
1003 9b1c39e3 Ermal Luçi
1004 d7147b1c Scott Ullrich
		if ($g['booting'])
1005 07e40c1f Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
1006 d7147b1c Scott Ullrich
	}
1007 9b1c39e3 Ermal Luçi
1008 42753d25 Ermal Lu?i
	/* configure interface groups */
1009
	interfaces_group_setup();
1010
1011 5b237745 Scott Ullrich
	if (!$g['booting']) {
1012
		/* reconfigure static routes (kernel may have deleted them) */
1013
		system_routing_configure();
1014 cfc707f7 Scott Ullrich
1015 5b237745 Scott Ullrich
		/* reload IPsec tunnels */
1016
		vpn_ipsec_configure();
1017 cfc707f7 Scott Ullrich
1018 f620d00d Ermal Luçi
		/* reload dhcpd (interface enabled/disabled status may have changed) */
1019 5b237745 Scott Ullrich
		services_dhcpd_configure();
1020 cfc707f7 Scott Ullrich
1021 5b237745 Scott Ullrich
		/* restart dnsmasq */
1022
		services_dnsmasq_configure();
1023 4d18de6a Scott Ullrich
1024 c597d50f Scott Ullrich
		/* reload captive portal */
1025 769e254e Ermal
		captiveportal_init_rules();
1026 5b237745 Scott Ullrich
	}
1027 cfc707f7 Scott Ullrich
1028 5b237745 Scott Ullrich
	return 0;
1029
}
1030
1031 7a18dfa4 lgcosta
function interface_reconfigure($interface = "wan", $reloadall = false) {
1032 80bf3f4a Ermal Luçi
	interface_bring_down($interface);
1033 7a18dfa4 lgcosta
	interface_configure($interface, $reloadall);
1034 80bf3f4a Ermal Luçi
}
1035
1036 91a38e1f Ermal
function interface_vip_bring_down($vip) {
1037 962fd685 Ermal
	global $g;
1038
1039 abcb2bed Ermal Lu?i
	switch ($vip['mode']) {
1040
	case "proxyarp":
1041 962fd685 Ermal
		$vipif = get_real_interface($vip['interface']);
1042 ca942829 Ermal
		if (file_exists("{$g['varrun_path']}/choparp_{$vipif}.pid"))
1043
			killbypid("{$g['varrun_path']}/choparp_{$vipif}.pid");
1044 abcb2bed Ermal Lu?i
		break;
1045
	case "ipalias":
1046 435f11c8 Ermal Lu?i
		$vipif = get_real_interface($vip['interface']);
1047
		if(does_interface_exist($vipif))
1048 871768cf Ermal
			pfSense_interface_deladdress($vipif, $vip['subnet']);
1049 abcb2bed Ermal Lu?i
		break;
1050
	case "carp":
1051
	case "carpdev-dhcp":
1052 7b47bd4c Ermal
		$vipif = "{$vip['interface']}_vip{$vip['vhid']}";
1053
		if (does_interface_exist($vipif)) 
1054 871768cf Ermal
			pfSense_interface_destroy($vipif);
1055 abcb2bed Ermal Lu?i
		break;
1056
	}
1057
}
1058
1059 97973ed8 Ermal Luçi
function interface_bring_down($interface = "wan", $destroy = false) {
1060 80bf3f4a Ermal Luçi
	global $config, $g;
1061
1062 99c2a28b Ermal Luçi
	if (!isset($config['interfaces'][$interface]))
1063
		return; 
1064
1065 37fb708c smos
	log_error("Calling interface down for interface {$interface}, destroy is {$destroy}");
1066
1067 80bf3f4a Ermal Luçi
	$ifcfg = $config['interfaces'][$interface];
1068
1069 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
1070 80bf3f4a Ermal Luçi
1071
	switch ($ifcfg['ipaddr']) {
1072 0810c115 gnhb
	case "ppp":
1073 80bf3f4a Ermal Luçi
	case "pppoe":
1074
	case "pptp":
1075 39f750b5 gnhb
	case "l2tp":
1076 a138f4fb Ermal
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1077
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1078
				if ($realif == $ppp['if']) {
1079 c8d23069 gnhb
					if (isset($ppp['ondemand']) && !$destroy){
1080
						send_event("interface reconfigure {$interface}");
1081
						break;
1082
					}
1083 a8d6ac1a Ermal
					if (file_exists("{$g['varrun_path']}/{$ppp['type']}_{$interface}.pid")) {
1084
						killbypid("{$g['varrun_path']}/{$ppp['type']}_{$interface}.pid");
1085 c8d23069 gnhb
						sleep(2);
1086 8d9cbe6f Ermal
					}
1087 64e6490a Ermal
					unlink_if_exists("{$g['varetc_path']}/mpd_{$interface}.conf");
1088 a138f4fb Ermal
					break;
1089
				}
1090
			}
1091
		}
1092 80bf3f4a Ermal Luçi
		break;
1093
	case "carpdev-dhcp":
1094
		/* 
1095
		 * NB: When carpdev gets enabled it would be better to be handled as all
1096 37a53d16 Scott Ullrich
		 *	   other interfaces! 
1097 80bf3f4a Ermal Luçi
		 */
1098
	case "dhcp":
1099 5d478ecc Ermal Lu?i
		$pid = find_dhclient_process($realif);
1100 f07bee94 Scott Ullrich
		if($pid)
1101 bcfe4ae5 Ermal
			mwexec("/bin/kill {$pid}");
1102 37fb708c smos
		sleep(1);
1103
		unlink_if_exists("{$g['varetc_path']}/dhclient_{$interface}.conf");
1104
		if(does_interface_exist("$realif")) {
1105
			mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
1106
			if ($destroy == true)
1107
				pfSense_interface_flags($realif, -IFF_UP);
1108
			mwexec("/usr/sbin/arp -d -i {$realif} -a");
1109
		}
1110
		break;
1111
	default:
1112
		if(does_interface_exist("$realif")) {
1113
			mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
1114
			if ($destroy == true)
1115
				pfSense_interface_flags($realif, -IFF_UP);
1116
			mwexec("/usr/sbin/arp -d -i {$realif} -a");
1117
		}
1118
		break;
1119
	}
1120
1121
	switch ($ifcfg['ipaddrv6']) {
1122 feb88a14 smos
	case "slaac":
1123 37fb708c smos
	case "dhcp6":
1124 c65d3051 Seth Mos
		$pidv6 = find_dhcp6c_process($realif);
1125 c495f88b Seth Mos
		if($pidv6)
1126
			mwexec("/bin/kill {$pidv6}");
1127 74fa57aa smos
		sleep(3);
1128 c495f88b Seth Mos
		unlink_if_exists("{$g['varetc_path']}/dhcp6c_{$interface}.conf");
1129 f07bee94 Scott Ullrich
		if(does_interface_exist("$realif")) {
1130 aef6d76f Seth Mos
			mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
1131 e49a2031 Ermal
			if ($destroy == true)
1132
				pfSense_interface_flags($realif, -IFF_UP);
1133 5630c91c Ermal Lu?i
			mwexec("/usr/sbin/arp -d -i {$realif} -a");
1134 f07bee94 Scott Ullrich
		}
1135 80bf3f4a Ermal Luçi
		break;
1136 3f383504 smos
	case "6rd":
1137 20a7cb15 smos
		$realif = "srd0";
1138 3f383504 smos
		if(does_interface_exist("$realif")) {
1139
			mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
1140
			if ($destroy == true)
1141
				pfSense_interface_flags($realif, -IFF_UP);
1142
		}		
1143
		break;
1144 31c43fd3 smos
	case "6to4":
1145 20a7cb15 smos
		$realif = "stf0";
1146 31c43fd3 smos
		if(does_interface_exist("$realif")) {
1147
			mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
1148
			if ($destroy == true)
1149
				pfSense_interface_flags($realif, -IFF_UP);
1150
		}		
1151
		break;
1152 80bf3f4a Ermal Luçi
	default:
1153 f07bee94 Scott Ullrich
		if(does_interface_exist("$realif")) {
1154 aef6d76f Seth Mos
			mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
1155 e49a2031 Ermal
			if ($destroy == true)
1156
				pfSense_interface_flags($realif, -IFF_UP);
1157 5630c91c Ermal Lu?i
			mwexec("/usr/sbin/arp -d -i {$realif} -a");
1158 f07bee94 Scott Ullrich
		}
1159 80bf3f4a Ermal Luçi
		break;
1160
	}
1161 eb772abd Scott Ullrich
1162 37fb708c smos
1163 73ee49f2 gnhb
	/* remove interface up file if it exists */
1164
	unlink_if_exists("{$g['tmp_path']}/{$realif}up");
1165
	unlink_if_exists("{$g['vardb_path']}/{$interface}ip");
1166 c495f88b Seth Mos
	unlink_if_exists("{$g['vardb_path']}/{$interface}ipv6");
1167 73ee49f2 gnhb
	unlink_if_exists("{$g['tmp_path']}/{$realif}_router");
1168 c495f88b Seth Mos
	unlink_if_exists("{$g['tmp_path']}/{$realif}_routerv6");
1169 86dcdfc9 Ermal
	unlink_if_exists("{$g['varetc_path']}/nameserver_{$realif}");
1170
	unlink_if_exists("{$g['varetc_path']}/searchdomain_{$realif}");
1171 73ee49f2 gnhb
	
1172 b5582f49 Erik Fonnesbeck
	/* hostapd and wpa_supplicant do not need to be running when the interface is down.
1173
	 * They will also use 100% CPU if running after the wireless clone gets deleted. */
1174
	if (is_array($ifcfg['wireless'])) {
1175
		mwexec(kill_hostapd($realif));
1176
		mwexec(kill_wpasupplicant($realif));
1177
	}
1178
1179 97973ed8 Ermal Luçi
	if ($destroy == true) {
1180 20a7cb15 smos
		if (preg_match("/^vip|^tun|^ovpn|^gif|^gre|^lagg|^bridge|vlan|^stf|^srd/i", $realif))
1181 871768cf Ermal
			pfSense_interface_destroy($realif);
1182 f07bee94 Scott Ullrich
	}	
1183 9006e9f8 Scott Ullrich
1184 80bf3f4a Ermal Luçi
	return;
1185 5b237745 Scott Ullrich
}
1186
1187 e5d558bf gnhb
function interfaces_ptpid_used($ptpid) {
1188
	global $config;
1189
1190
	if (is_array($config['ppps']['ppp']))
1191
		foreach ($config['ppps']['ppp'] as & $settings)
1192
			if ($ptpid == $settings['ptpid'])
1193
				return true;
1194
1195
	return false;
1196
}
1197
1198
function interfaces_ptpid_next() {
1199
1200
	$ptpid = 0;
1201
	while(interfaces_ptpid_used($ptpid))
1202
		$ptpid++;
1203
1204
	return $ptpid;
1205
}
1206
1207
function getMPDCRONSettings($pppif_) {
1208
	global $config;
1209 1d7e1d6c gnhb
	$cron_cmd_file = "{$g['varetc_path']}/pppoe_restart_";
1210 e5d558bf gnhb
	if (is_array($config['cron']['item'])) {
1211
		for ($i = 0; $i < count($config['cron']['item']); $i++) {
1212
			$item = $config['cron']['item'][$i];
1213 1d7e1d6c gnhb
			if (strpos($item['command'], $cron_cmd_file.$pppif_) !== false) {
1214 e5d558bf gnhb
				return array("ID" => $i, "ITEM" => $item);
1215
			}
1216
		}
1217
	}
1218
	return NULL;
1219
}
1220
1221
function handle_pppoe_reset($post_array) {
1222
	global $config, $g;
1223
1224 5c8e8a17 gnhb
	$cron_cmd_file = "{$g['varetc_path']}/pppoe_restart_";
1225
1226 e5d558bf gnhb
	$pppif = $post_array['type'].$post_array['ptpid'];
1227
	if (!is_array($config['cron']['item'])) 
1228
		$config['cron']['item'] = array(); 
1229 1d7e1d6c gnhb
	$itemhash = getMPDCRONSettings($pppif);
1230 e5d558bf gnhb
	$item = $itemhash['ITEM'];
1231
	
1232
	// reset cron items if necessary and return
1233
	if (empty($post_array['pppoe-reset-type'])) {
1234
		if (isset($item))
1235
			unset($config['cron']['item'][$itemhash['ID']]);
1236
		sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP");
1237
		return;
1238
	}
1239
1240
	if (empty($item)) 
1241
		$item = array();
1242
	if (isset($post_array['pppoe-reset-type']) && $post_array['pppoe-reset-type'] == "custom") {
1243
		$item['minute'] = $post_array['pppoe_resetminute'];
1244
		$item['hour'] = $post_array['pppoe_resethour'];
1245
		if (isset($post_array['pppoe_resetdate']) && $post_array['pppoe_resetdate'] <> "") {
1246
			$date = explode("/", $post_array['pppoe_resetdate']);
1247
			$item['mday'] = $date[1];
1248
			$item['month'] = $date[0];
1249
		} else {
1250
			$item['mday'] = "*";
1251
			$item['month'] = "*";
1252
		}
1253
		$item['wday'] = "*";
1254
		$item['who'] = "root";
1255 5c8e8a17 gnhb
		$item['command'] = $cron_cmd_file.$pppif;
1256 e5d558bf gnhb
	} else if (isset($post_array['pppoe-reset-type']) && $post_array['pppoe-reset-type'] == "preset") {
1257
		switch ($post_array['pppoe_pr_preset_val']) {
1258
			case "monthly":
1259
				$item['minute'] = "0";
1260
				$item['hour'] = "0";
1261
				$item['mday'] = "1";
1262
				$item['month'] = "*";
1263
				$item['wday'] = "*";
1264
				$item['who'] = "root";
1265 5c8e8a17 gnhb
				$item['command'] = $cron_cmd_file.$pppif;
1266 e5d558bf gnhb
				break;
1267
	        case "weekly":
1268
				$item['minute'] = "0";
1269
				$item['hour'] = "0";
1270
				$item['mday'] = "*";
1271
				$item['month'] = "*";
1272
				$item['wday'] = "0";
1273
				$item['who'] = "root";
1274 5c8e8a17 gnhb
				$item['command'] = $cron_cmd_file.$pppif;
1275 e5d558bf gnhb
				break;
1276
			case "daily":
1277
				$item['minute'] = "0";
1278
				$item['hour'] = "0";
1279
				$item['mday'] = "*";
1280
				$item['month'] = "*";
1281
				$item['wday'] = "*";
1282
				$item['who'] = "root";
1283 5c8e8a17 gnhb
				$item['command'] = $cron_cmd_file.$pppif;
1284 e5d558bf gnhb
				break;
1285
			case "hourly":
1286
				$item['minute'] = "0";
1287
				$item['hour'] = "*";
1288
				$item['mday'] = "*";
1289
				$item['month'] = "*";
1290
				$item['wday'] = "*";
1291
				$item['who'] = "root";
1292 5c8e8a17 gnhb
				$item['command'] = $cron_cmd_file.$pppif;
1293 e5d558bf gnhb
				break;
1294
		} // end switch
1295 5c8e8a17 gnhb
	} else {
1296
		/* test whether a cron item exists and unset() it if necessary */
1297 1d7e1d6c gnhb
		$itemhash = getMPDCRONSettings($pppif);
1298 5c8e8a17 gnhb
		$item = $itemhash['ITEM'];
1299
		if (isset($item))
1300
			unset($config['cron']['item'][$itemhash['ID']]); 
1301 e5d558bf gnhb
	}// end if
1302
	if (isset($itemhash['ID'])) 
1303
		$config['cron']['item'][$itemhash['ID']] = $item;
1304
	else 
1305
		$config['cron']['item'][] = $item;
1306
}
1307
1308 349e9ec1 Erik Fonnesbeck
/*	This function can configure PPPoE, MLPPP (PPPoE), PPTP.
1309 8256f324 gnhb
*	It writes the mpd config file to /var/etc every time the link is opened.
1310 cb37d8fa gnhb
*/
1311
1312
function interface_ppps_configure($interface) {
1313
	global $config, $g;
1314 01c201e3 Ermal
1315
	/* Return for unassigned interfaces. This is a minimum requirement. */
1316
	if (empty($config['interfaces'][$interface]))
1317
		return 0;
1318
	$ifcfg = $config['interfaces'][$interface];
1319
	if (!isset($ifcfg['enable']))
1320
		return 0;
1321
1322 3a906378 gnhb
	// mpd5 requires a /var/spool/lock directory for PPP modem links.
1323
	if(!is_dir("/var/spool/lock")) {
1324
		exec("/bin/mkdir -p /var/spool/lock");
1325
		exec("/bin/chmod a+rw /var/spool/lock/.");
1326
	}
1327
	// mpd5 modem chat script expected in the same directory as the mpd_xxx.conf files	
1328
	if (!file_exists("{$g['varetc_path']}/mpd.script"))
1329
		mwexec("/bin/ln -s /usr/local/sbin/mpd.script {$g['varetc_path']}/.");
1330 01c201e3 Ermal
1331 cb37d8fa gnhb
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1332
		foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1333 f7480829 gnhb
			if ($ifcfg['if'] == $ppp['if'])
1334 cb37d8fa gnhb
				break;
1335
		}
1336
	}
1337 f7480829 gnhb
	if (!$ppp || $ifcfg['if'] != $ppp['if']){
1338 07e40c1f Carlos Eduardo Ramos
		log_error(sprintf(gettext("Can't find PPP config for %s in interface_ppps_configure()."), $ifcfg['if']));
1339 3a906378 gnhb
		return 0;
1340 cb37d8fa gnhb
	}
1341 3a906378 gnhb
	$pppif = $ifcfg['if'];
1342 cb37d8fa gnhb
	if ($ppp['type'] == "ppp")
1343
		$type = "modem";
1344
	else
1345
		$type = $ppp['type'];
1346 3a906378 gnhb
	$upper_type = strtoupper($ppp['type']);	
1347 01c201e3 Ermal
1348 3a906378 gnhb
	if($g['booting']) {
1349 bfbb9bc0 Ermal
		$descr = isset($ifcfg['descr']) ? $ifcfg['descr'] : strtoupper($interface);
1350 3a90c973 gnhb
		echo "starting {$pppif} link...";
1351 3a906378 gnhb
		// Do not re-configure the interface if we are booting and it's already been started
1352
		if(file_exists("{$g['varrun_path']}/{$ppp['type']}_{$interface}.pid"))
1353
			return 0;
1354
	}
1355 01c201e3 Ermal
1356 3a906378 gnhb
	$ports = explode(',',$ppp['ports']);
1357 bfbb9bc0 Ermal
	if ($type != "modem") {
1358
		foreach ($ports as $pid => $port)
1359
			$ports[$pid] = get_real_interface($port);
1360
	}
1361 3a906378 gnhb
	$localips = explode(',',$ppp['localip']);
1362
	$gateways = explode(',',$ppp['gateway']);
1363
	$subnets = explode(',',$ppp['subnet']);
1364 01c201e3 Ermal
1365 3a906378 gnhb
	/* We bring up the parent interface first because if DHCP is configured on the parent we need
1366 01c201e3 Ermal
	 * to obtain an address first so we can write it in the mpd .conf file for PPTP and L2TP configs
1367
	 */
1368 3a906378 gnhb
	foreach($ports as $pid => $port){
1369 23721285 gnhb
		switch ($ppp['type']) {
1370 3a906378 gnhb
			case "pppoe": 
1371
				/* Bring the parent interface up */
1372
				interfaces_bring_up($port);
1373 3d04de61 Ermal
				pfSense_ngctl_attach(".", $port);
1374 3a906378 gnhb
				break;
1375
			case "pptp":
1376
			case "l2tp":
1377
				/* configure interface */
1378 69c1b043 gnhb
				if(is_ipaddr($localips[$pid])){
1379 3a906378 gnhb
					// Manually configure interface IP/subnet
1380 bfbb9bc0 Ermal
					pfSense_interface_setaddress($port, "{$localips[$pid]}/{$subnets[$pid]}");
1381
					interfaces_bring_up($port);
1382 69c1b043 gnhb
				} else if (empty($localips[$pid]))
1383
					$localips[$pid] = get_interface_ip($port); // try to get the interface IP from the port
1384
				
1385
				if(!is_ipaddr($localips[$pid])){
1386 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!");
1387
					$localips[$pid] = "0.0.0.0";
1388 3a906378 gnhb
				}
1389 69c1b043 gnhb
				/* XXX: This needs to go away soon! [It's commented out!] */
1390
				/* Configure the gateway (remote IP ) */
1391 bfbb9bc0 Ermal
				if (!$g['booting'] && !is_ipaddr($gateways[$pid]) && is_hostname($gateways[$pid])) {
1392 69c1b043 gnhb
					/* XXX: Fix later 
1393 765664a4 gnhb
					$gateways[$pid] = gethostbyname($gateways[$pid]);
1394
					if(!is_ipaddr($gateways[$pid])) {
1395
						log_error("Could not get a valid Gateway IP from {$port} via DNS in interfaces_ppps_configure.");
1396 23721285 gnhb
						return 0;
1397 743994a6 gnhb
					}
1398 69c1b043 gnhb
					*/
1399
				}
1400
				if(!is_ipaddr($gateways[$pid])){
1401 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));
1402 69c1b043 gnhb
					return 0;
1403 3a906378 gnhb
				}
1404 3d04de61 Ermal
				pfSense_ngctl_attach(".", $port);
1405 3a906378 gnhb
				break;
1406
			case "ppp":
1407
				if (!file_exists("{$port}")) {
1408 07e40c1f Carlos Eduardo Ramos
					log_error(sprintf(gettext("Device %s does not exist. PPP link cannot start without the modem device."), $port));
1409 23721285 gnhb
					return 0;
1410 3a906378 gnhb
				}
1411
				break;
1412
			default:
1413 07e40c1f Carlos Eduardo Ramos
				log_error(sprintf(gettext("Unkown %s configured as ppp interface."), $type));
1414 3a906378 gnhb
				break;
1415
		}
1416
	}
1417 00b702cc gnhb
	
1418 cb37d8fa gnhb
	if (is_array($ports) && count($ports) > 1)
1419
		$multilink = "enable";
1420
	else
1421
		$multilink = "disable";
1422
	
1423
	if ($type == "modem"){
1424
		if (is_ipaddr($ppp['localip']))
1425
			$localip = $ppp['localip'];
1426
		else
1427
			$localip = '0.0.0.0';
1428
1429
		if (is_ipaddr($ppp['gateway']))
1430
			$gateway = $ppp['gateway'];
1431
		else
1432 23721285 gnhb
			$gateway = "10.64.64.{$pppid}";
1433 cb37d8fa gnhb
		$ranges = "{$localip}/0 {$gateway}/0";
1434 3a906378 gnhb
		
1435
		if (empty($ppp['apnum']))	
1436
			$ppp['apnum'] = 1;
1437 23721285 gnhb
	} else
1438 cb37d8fa gnhb
		$ranges = "0.0.0.0/0 0.0.0.0/0";
1439 0661b194 gnhb
1440 cb37d8fa gnhb
	if (isset($ppp['ondemand'])) 
1441
		$ondemand = "enable";
1442
	else
1443
		$ondemand = "disable";
1444
	if (!isset($ppp['idletimeout']))
1445
		$ppp['idletimeout'] = 0;
1446 64d124c5 gnhb
1447 cb37d8fa gnhb
	if (empty($ppp['username']) && $type == "modem"){
1448
		$ppp['username'] = "user";
1449
		$ppp['password'] = "none";
1450
	}
1451
	if (empty($ppp['password']) && $type == "modem")
1452 00b702cc gnhb
		$passwd = "none";
1453
	else
1454
		$passwd = base64_decode($ppp['password']);
1455 0661b194 gnhb
1456
	$bandwidths = explode(',',$ppp['bandwidth']);
1457
	$mtus = explode(',',$ppp['mtu']);
1458
	$mrus = explode(',',$ppp['mru']);
1459
1460 c1cc447c gnhb
	if (isset($ppp['mrru']))
1461 0661b194 gnhb
		$mrrus = explode(',',$ppp['mrru']);
1462 c1cc447c gnhb
1463 cb37d8fa gnhb
	// Construct the mpd.conf file
1464
	$mpdconf = <<<EOD
1465
startup:
1466
	# configure the console
1467
	set console close
1468
	# configure the web server
1469
	set web close
1470
1471
default:
1472
{$ppp['type']}client:
1473
	create bundle static {$interface}
1474 07dfd121 Seth Mos
	set bundle enable ipv6cp
1475 cb37d8fa gnhb
	set iface name {$pppif}
1476
1477
EOD;
1478 0661b194 gnhb
	$setdefaultgw = false;
1479
	$founddefaultgw = false;
1480
	if (is_array($config['gateways']['gateway_item'])) {
1481
		foreach($config['gateways']['gateway_item'] as $gateway) {
1482
			if($interface == $gateway['interface'] && isset($gateway['defaultgw'])) {
1483
				$setdefaultgw = true;
1484
				break;
1485
			} else if (isset($gateway['defaultgw']) && !empty($gateway['interface'])) {
1486
				$founddefaultgw = true;
1487
				break;
1488
			}
1489
		}
1490
	}
1491 82effddb gnhb
	
1492
	if (($interface == "wan" && $founddefaultgw == false) || $setdefaultgw == true){
1493
		$setdefaultgw = true;
1494 cb37d8fa gnhb
		$mpdconf .= <<<EOD
1495
	set iface route default
1496
1497
EOD;
1498 82effddb gnhb
	}
1499 cb37d8fa gnhb
	$mpdconf .= <<<EOD
1500
	set iface {$ondemand} on-demand
1501
	set iface idle {$ppp['idletimeout']}
1502
1503
EOD;
1504
1505 0661b194 gnhb
	if (isset($ppp['ondemand']))
1506 cb37d8fa gnhb
		$mpdconf .= <<<EOD
1507 55f3ca1d gnhb
	set iface addrs 10.10.1.1 10.10.1.2
1508 cb37d8fa gnhb
1509
EOD;
1510 0661b194 gnhb
	
1511
	if (isset($ppp['tcpmssfix']))
1512 8adc1e49 gnhb
		$tcpmss = "disable";
1513
	else
1514
		$tcpmss = "enable";
1515 64d124c5 gnhb
		$mpdconf .= <<<EOD
1516 8adc1e49 gnhb
	set iface {$tcpmss} tcpmssfix
1517 64d124c5 gnhb
1518
EOD;
1519 0661b194 gnhb
1520 cb37d8fa gnhb
	$mpdconf .= <<<EOD
1521
	set iface up-script /usr/local/sbin/ppp-linkup
1522
	set iface down-script /usr/local/sbin/ppp-linkdown
1523
	set ipcp ranges {$ranges}
1524
1525
EOD;
1526 0661b194 gnhb
	if (isset($ppp['vjcomp']))
1527 cb37d8fa gnhb
		$mpdconf .= <<<EOD
1528 64d124c5 gnhb
	set ipcp no vjcomp
1529 cb37d8fa gnhb
1530
EOD;
1531
1532 bfbb9bc0 Ermal
	if (isset($config['system']['dnsallowoverride']))
1533 64d124c5 gnhb
		$mpdconf .= <<<EOD
1534
	set ipcp enable req-pri-dns
1535
	set ipcp enable req-sec-dns
1536
1537
EOD;
1538 23721285 gnhb
	if (!isset($ppp['verbose_log']))
1539
		$mpdconf .= <<<EOD
1540 5d9d443a gnhb
	#log -bund -ccp -chat -iface -ipcp -lcp -link
1541 0661b194 gnhb
1542 23721285 gnhb
EOD;
1543 64d124c5 gnhb
	foreach($ports as $pid => $port){
1544 bfbb9bc0 Ermal
		$port = get_real_interface($port);
1545 00b702cc gnhb
		$mpdconf .= <<<EOD
1546 cb37d8fa gnhb
1547 0661b194 gnhb
	create link static {$interface}_link{$pid} {$type}
1548 cb37d8fa gnhb
	set link action bundle {$interface}
1549
	set link {$multilink} multilink
1550
	set link keep-alive 10 60
1551
	set link max-redial 0
1552 64d124c5 gnhb
1553
EOD;
1554 0661b194 gnhb
		if (isset($ppp['shortseq']))
1555 00b702cc gnhb
			$mpdconf .= <<<EOD
1556 64d124c5 gnhb
	set link no shortseq
1557
1558
EOD;
1559 0661b194 gnhb
1560
		if (isset($ppp['acfcomp']))
1561 00b702cc gnhb
			$mpdconf .= <<<EOD
1562 64d124c5 gnhb
	set link no acfcomp
1563
1564
EOD;
1565 0661b194 gnhb
1566
		if (isset($ppp['protocomp']))
1567 00b702cc gnhb
			$mpdconf .= <<<EOD
1568 64d124c5 gnhb
	set link no protocomp
1569
1570
EOD;
1571 0661b194 gnhb
1572 00b702cc gnhb
		$mpdconf .= <<<EOD
1573 cb37d8fa gnhb
	set link disable chap pap
1574
	set link accept chap pap eap
1575 64d124c5 gnhb
	set link disable incoming
1576 cb37d8fa gnhb
1577
EOD;
1578 00b702cc gnhb
1579
1580 0661b194 gnhb
		if (!empty($bandwidths[$pid]))
1581 00b702cc gnhb
			$mpdconf .= <<<EOD
1582
	set link bandwidth {$bandwidths[$pid]}
1583 cb37d8fa gnhb
1584
EOD;
1585 0661b194 gnhb
1586 8adc1e49 gnhb
		if (empty($mtus[$pid]))
1587
			$mtus[$pid] = "1492";
1588 00b702cc gnhb
			$mpdconf .= <<<EOD
1589
	set link mtu {$mtus[$pid]}
1590 cb37d8fa gnhb
1591
EOD;
1592 0661b194 gnhb
1593
		if (!empty($mrus[$pid]))
1594 00b702cc gnhb
			$mpdconf .= <<<EOD
1595
	set link mru {$mrus[$pid]}
1596
1597 6a30f701 gnhb
EOD;
1598
1599
		if (!empty($mrrus[$pid]))
1600
			$mpdconf .= <<<EOD
1601
	set link mrru {$mrrus[$pid]}
1602
1603 00b702cc gnhb
EOD;
1604 0661b194 gnhb
1605 00b702cc gnhb
		$mpdconf .= <<<EOD
1606 cb37d8fa gnhb
	set auth authname "{$ppp['username']}"
1607
	set auth password {$passwd}
1608
1609
EOD;
1610 00b702cc gnhb
		if ($type == "modem") {
1611
			$mpdconf .= <<<EOD
1612 cb37d8fa gnhb
	set modem device {$ppp['ports']}
1613
	set modem script DialPeer
1614
	set modem idle-script Ringback
1615
	set modem watch -cd
1616
	set modem var \$DialPrefix "DT"
1617
	set modem var \$Telephone "{$ppp['phone']}"
1618
1619
EOD;
1620 00b702cc gnhb
		}
1621
		if (isset($ppp['connect-timeout']) && $type == "modem") {
1622
			$mpdconf .= <<<EOD
1623 cb37d8fa gnhb
	set modem var \$ConnectTimeout "{$ppp['connect-timeout']}"
1624
1625
EOD;
1626 00b702cc gnhb
		}
1627
		if (isset($ppp['initstr']) && $type == "modem") {
1628
			$initstr = base64_decode($ppp['initstr']);
1629
			$mpdconf .= <<<EOD
1630 cb37d8fa gnhb
	set modem var \$InitString "{$initstr}"
1631
1632
EOD;
1633 00b702cc gnhb
		}
1634
		if (isset($ppp['simpin']) && $type == "modem") {
1635
			$mpdconf .= <<<EOD
1636 cb37d8fa gnhb
	set modem var \$SimPin "{$ppp['simpin']}"
1637
	set modem var \$PinWait "{$ppp['pin-wait']}"
1638
1639
EOD;
1640 00b702cc gnhb
		}
1641
		if (isset($ppp['apn']) && $type == "modem") {
1642
			$mpdconf .= <<<EOD
1643 cb37d8fa gnhb
	set modem var \$APN "{$ppp['apn']}"
1644
	set modem var \$APNum "{$ppp['apnum']}"
1645
1646
EOD;
1647 00b702cc gnhb
		}
1648 233e2af1 jim-p
		if ($type == "pppoe") {
1649
			// Send a null service name if none is set.
1650
			$provider = isset($ppp['provider']) ? $ppp['provider'] : "";
1651 00b702cc gnhb
			$mpdconf .= <<<EOD
1652 233e2af1 jim-p
	set pppoe service "{$provider}"
1653 cb37d8fa gnhb
1654
EOD;
1655 00b702cc gnhb
		}
1656 0661b194 gnhb
		if ($type == "pppoe")
1657 00b702cc gnhb
			$mpdconf .= <<<EOD
1658 64d124c5 gnhb
	set pppoe iface {$port}
1659 cb37d8fa gnhb
1660
EOD;
1661 0661b194 gnhb
1662 39f750b5 gnhb
		if ($type == "pptp" || $type == "l2tp") {
1663 00b702cc gnhb
			$mpdconf .= <<<EOD
1664 18ec0f13 Ermal
	set {$type} self {$localips[$pid]}
1665
	set {$type} peer {$gateways[$pid]}
1666 cb37d8fa gnhb
1667
EOD;
1668 00b702cc gnhb
		}
1669 23721285 gnhb
		
1670 00b702cc gnhb
		$mpdconf .= "\topen\r\n";
1671 cb37d8fa gnhb
	} //end foreach($port)
1672
1673 df309b37 gnhb
1674
	/* Generate mpd.conf. If mpd_[interface].conf exists in the conf path, then link to it instead of generating a fresh conf file. */
1675
	if (file_exists("{$g['conf_path']}/mpd_{$interface}.conf"))
1676
		mwexec("/bin/ln -s {$g['conf_path']}/mpd_{$interface}.conf {$g['varetc_path']}/.");
1677
	else {
1678
		$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
1679
		if (!$fd) {
1680 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Error: cannot open mpd_%s.conf in interface_ppps_configure().%s"), $interface, "\n"));
1681 df309b37 gnhb
			return 0;
1682
		}
1683
		// Write out mpd_ppp.conf
1684
		fwrite($fd, $mpdconf);
1685
		fclose($fd);
1686
	}
1687 cb37d8fa gnhb
1688
	// Create the uptime log if requested and if it doesn't exist already, or delete it if it is no longer requested.
1689
	if (isset($ppp['uptime'])) {
1690
		if (!file_exists("/conf/{$pppif}.log")) {
1691
			conf_mount_rw();
1692
			mwexec("echo /dev/null > /conf/{$pppif}.log");
1693
			conf_mount_ro();
1694
		}
1695
	} else {
1696
		if (file_exists("/conf/{$pppif}.log")) {
1697
			conf_mount_rw();
1698
			mwexec("rm -f /conf/{$pppif}.log");
1699
			conf_mount_ro();
1700
		}
1701
	}
1702 92a1c8e6 Ermal
1703 3a906378 gnhb
	/* fire up mpd */
1704
	mwexec("/usr/local/sbin/mpd5 -b -k -d {$g['varetc_path']} -f mpd_{$interface}.conf -p {$g['varrun_path']}/{$ppp['type']}_{$interface}.pid -s ppp {$ppp['type']}client");
1705
1706 55f3ca1d gnhb
	// Check for PPPoE periodic reset request 
1707 bfbb9bc0 Ermal
	if ($type == "pppoe") {
1708 766bd6d0 gnhb
		if (isset($ppp['pppoe-reset-type']))
1709 5c8e8a17 gnhb
			setup_pppoe_reset_file($ppp['if'], $interface);
1710 766bd6d0 gnhb
		else
1711 5c8e8a17 gnhb
			setup_pppoe_reset_file($ppp['if']);
1712 cb37d8fa gnhb
	}
1713 302d646e smos
	/* wait for upto 10 seconds for the interface to appear (ppp(oe)) */
1714
	$i = 0;
1715
	while($i < 10) {
1716
		exec("/sbin/ifconfig {$ppp['if']} 2>&1", $out, $ret);
1717
		if($ret == 0)
1718
			break;
1719
		sleep(1);
1720
		$i++;
1721
	}
1722 cb37d8fa gnhb
1723 23721285 gnhb
	return 1;
1724 cb37d8fa gnhb
}
1725
1726 abcb2bed Ermal Lu?i
function interfaces_carp_setup() {
1727 87a2efd1 Ermal Luçi
	global $g, $config;
1728 abcb2bed Ermal Lu?i
1729 2b9747b9 Scott Ullrich
	$balanacing = "";
1730
	$pfsyncinterface = "";
1731
	$pfsyncenabled = "";
1732 b932ef16 Scott Ullrich
	if(isset($config['system']['developerspew'])) {
1733
		$mt = microtime();
1734 abcb2bed Ermal Lu?i
		echo "interfaces_carp_setup() being called $mt\n";
1735 b932ef16 Scott Ullrich
	}
1736 abcb2bed Ermal Lu?i
1737 e5d43d93 Scott Ullrich
	// Prepare CmdCHAIN that will be used to execute commands.
1738
	$cmdchain = new CmdCHAIN();	
1739 abcb2bed Ermal Lu?i
1740 b932ef16 Scott Ullrich
	if ($g['booting']) {
1741 07e40c1f Carlos Eduardo Ramos
		echo gettext("Configuring CARP settings...");
1742 7d0f4544 Scott Ullrich
		mute_kernel_msgs();
1743 a5250ebc Scott Ullrich
	}
1744 abcb2bed Ermal Lu?i
1745 b932ef16 Scott Ullrich
	/* suck in configuration items */
1746 f97a5b04 Darren Embry
	if($config['hasync']) {
1747
		$pfsyncenabled = $config['hasync']['pfsyncenabled'];
1748
		$balanacing = $config['hasync']['balancing'];
1749
		$pfsyncinterface = $config['hasync']['pfsyncinterface'];
1750
		$pfsyncpeerip = $config['hasync']['pfsyncpeerip'];
1751 b932ef16 Scott Ullrich
	} else {
1752
		unset($pfsyncinterface);
1753
		unset($balanacing);
1754
		unset($pfsyncenabled);
1755 6008210b Scott Ullrich
	}
1756 abcb2bed Ermal Lu?i
1757 b932ef16 Scott Ullrich
	if($balanacing) {
1758 07e40c1f Carlos Eduardo Ramos
		$cmdchain->add(gettext("Enable CARP ARP-balancing"), "/sbin/sysctl net.inet.carp.arpbalance=1", true);
1759
		$cmdchain->add(gettext("Disallow CARP preemption"), "/sbin/sysctl net.inet.carp.preempt=0", true);
1760 abcb2bed Ermal Lu?i
	} else
1761 07e40c1f Carlos Eduardo Ramos
		$cmdchain->add(gettext("Enable CARP preemption"), "/sbin/sysctl net.inet.carp.preempt=1", true);		
1762 abcb2bed Ermal Lu?i
1763 c92ccac7 Vinicius Coque
	$cmdchain->add(gettext("Enable CARP logging"), "/sbin/sysctl net.inet.carp.log=1", true);
1764 abcb2bed Ermal Lu?i
	if (!empty($pfsyncinterface))
1765
		$carp_sync_int = get_real_interface($pfsyncinterface);
1766
1767 b932ef16 Scott Ullrich
	if($g['booting']) {
1768
		/*    install rules to alllow pfsync to sync up during boot
1769
		 *    carp interfaces will remain down until the bootup sequence finishes
1770
		 */
1771 a6726cf2 Ermal Lu?i
		$fd = fopen("{$g['tmp_path']}/rules.boot", "w");
1772
		if ($fd) {
1773 359f6307 Ermal
			fwrite($fd, "block quick proto carp \n");
1774
			fwrite($fd, "block quick proto pfsync \n");
1775 df9d4110 Ermal Lu?i
			fwrite($fd, "pass out quick from any to any keep state\n");
1776 a6726cf2 Ermal Lu?i
			fclose($fd);
1777
			mwexec("/sbin/pfctl -f {$g['tmp_path']}/rules.boot");
1778
		} else
1779 07e40c1f Carlos Eduardo Ramos
			log_error(gettext("Could not create rules.boot file!"));
1780 eb772abd Scott Ullrich
	}
1781 abcb2bed Ermal Lu?i
1782 b932ef16 Scott Ullrich
	/* setup pfsync interface */
1783 b42ad736 Scott Ullrich
	if($carp_sync_int and $pfsyncenabled) {
1784 abcb2bed Ermal Lu?i
		if (is_ipaddr($pfsyncpeerip))
1785 07e40c1f Carlos Eduardo Ramos
			$cmdchain->add(gettext("Bring up pfsync0 syncpeer"), "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} syncpeer {$pfsyncpeerip} up", false);						
1786 abcb2bed Ermal Lu?i
		else
1787 07e40c1f Carlos Eduardo Ramos
			$cmdchain->add(gettext("Bring up pfsync0 syncdev"), "/sbin/ifconfig pfsync0 syncdev {$carp_sync_int} up", false);			
1788 abcb2bed Ermal Lu?i
	} else
1789 07e40c1f Carlos Eduardo Ramos
		$cmdchain->add(gettext("Bring up pfsync0"), "/sbin/ifconfig pfsync0 syncdev lo0 up", false);						
1790 abcb2bed Ermal Lu?i
1791 156ecb64 Ermal
	sleep(1);
1792 2eb9c02f Ermal
1793 156ecb64 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 issuese
1794
	 * for exiting sessions.
1795
	 */
1796
	$i = 0;
1797
	while (intval(trim(`/sbin/ifconfig pfsync0 | /usr/bin/grep 'syncok: 0' | /usr/bin/grep -v grep | /usr/bin/wc -l`)) == 0 && $i < 30) {
1798
		$i++;
1799
		sleep(1);
1800 6930e805 Ermal
	}
1801 abcb2bed Ermal Lu?i
1802
	if($config['virtualip']['vip'])
1803 07e40c1f Carlos Eduardo Ramos
		$cmdchain->add(gettext("Allow CARP."), "/sbin/sysctl net.inet.carp.allow=1", true);				
1804 abcb2bed Ermal Lu?i
	else
1805 07e40c1f Carlos Eduardo Ramos
		$cmdchain->add(gettext("Disallow CARP."), "/sbin/sysctl net.inet.carp.allow=0", true);		
1806 e5d43d93 Scott Ullrich
	
1807 87a2efd1 Ermal Luçi
	if($g['debug'])
1808 e5d43d93 Scott Ullrich
		$cmdchain->setdebug(); // optional for verbose logging
1809 abcb2bed Ermal Lu?i
1810 e5d43d93 Scott Ullrich
	$cmdchain->execute();
1811
	$cmdchain->clear();
1812
1813 abcb2bed Ermal Lu?i
	if ($g['booting']) {
1814
		unmute_kernel_msgs();
1815 07e40c1f Carlos Eduardo Ramos
		echo gettext("done.") . "\n";
1816 abcb2bed Ermal Lu?i
	}
1817 67ee1ec5 Ermal Luçi
}
1818
1819 962fd685 Ermal
function interface_proxyarp_configure($interface = "") {
1820 9006e9f8 Scott Ullrich
	global $config, $g;
1821
	if(isset($config['system']['developerspew'])) {
1822
		$mt = microtime();
1823
		echo "interface_proxyarp_configure() being called $mt\n";
1824
	}
1825 67ee1ec5 Ermal Luçi
1826 9006e9f8 Scott Ullrich
	/* kill any running choparp */
1827 962fd685 Ermal
	if (empty($interface))
1828
		killbyname("choparp");
1829 7c73f504 Ermal
	else {
1830
		$vipif = get_real_interface($interface);
1831
		if (file_exists("{$g['varrun_path']}/choparp_{$vipif}.pid"))
1832
			killbypid("{$g['varrun_path']}/choparp_{$vipif}.pid");
1833
	}
1834 1b58b513 Scott Ullrich
1835 7c73f504 Ermal
	$paa = array();
1836
	if (!empty($config['virtualip']) && is_array($config['virtualip']['vip'])) {
1837 e5d43d93 Scott Ullrich
1838 9006e9f8 Scott Ullrich
		/* group by interface */
1839
		foreach ($config['virtualip']['vip'] as $vipent) {
1840
			if ($vipent['mode'] === "proxyarp") {
1841
				if ($vipent['interface'])
1842
					$proxyif = $vipent['interface'];
1843
				else
1844
					$proxyif = "wan";
1845 7e96ca27 Ermal
				
1846
				if (!empty($interface) && $interface != $proxyif)
1847
					continue;
1848 abcb2bed Ermal Lu?i
1849 7c73f504 Ermal
				if (!is_array($paa[$proxyif]))
1850 9006e9f8 Scott Ullrich
					$paa[$proxyif] = array();
1851 7b2d4769 Bill Marquette
1852 9006e9f8 Scott Ullrich
				$paa[$proxyif][] = $vipent;
1853
			}
1854 962fd685 Ermal
		}
1855 9006e9f8 Scott Ullrich
	}
1856 e5d43d93 Scott Ullrich
1857 962fd685 Ermal
	if (!empty($interface)) {
1858
		if (is_array($paa[$interface])) {
1859
			$paaifip = get_interface_ip($interface);
1860
                        if (!is_ipaddr($paaifip))
1861
                                return;
1862
                        $args = get_real_interface($interface) . " auto";
1863
                        foreach ($paa[$interface] as $paent) {
1864
                                if (isset($paent['subnet']))
1865
                                        $args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
1866
                                else if (isset($paent['range']))
1867
                                        $args .= " " . escapeshellarg($paent['range']['from'] . "-" . $paent['range']['to']);
1868
                        }
1869
                        mwexec_bg("/usr/local/sbin/choparp " . $args);	
1870
		}
1871 7c73f504 Ermal
	} else if (count($paa) > 0) {
1872
		foreach ($paa as $paif => $paents)  {
1873 9006e9f8 Scott Ullrich
			$paaifip = get_interface_ip($paif);
1874 f814d3a6 Ermal
			if (!is_ipaddr($paaifip))
1875 9006e9f8 Scott Ullrich
				continue;
1876
			$args = get_real_interface($paif) . " auto";
1877
			foreach ($paents as $paent) {
1878
				if (isset($paent['subnet']))
1879
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
1880
				else if (isset($paent['range']))
1881 962fd685 Ermal
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" . $paent['range']['to']);
1882 9006e9f8 Scott Ullrich
			}
1883
			mwexec_bg("/usr/local/sbin/choparp " . $args);
1884
		}
1885
	}
1886 9f6b1429 Scott Ullrich
}
1887
1888 e5ac67ed Ermal Lu?i
function interfaces_vips_configure($interface = "") {
1889 87a2efd1 Ermal Luçi
	global $g, $config;
1890 a04de17f Chris Buechler
	if(isset($config['system']['developerspew'])) {
1891
		$mt = microtime();
1892 123f030c Chris Buechler
		echo "interfaces_vips_configure() being called $mt\n";
1893 a04de17f Chris Buechler
	}
1894 abcb2bed Ermal Lu?i
	$paa = array();
1895
	if(is_array($config['virtualip']['vip'])) {
1896
		$carp_setuped = false;
1897 e5ac67ed Ermal Lu?i
		$anyproxyarp = false;
1898 abcb2bed Ermal Lu?i
		foreach ($config['virtualip']['vip'] as $vip) {
1899
			switch ($vip['mode']) {
1900
			case "proxyarp":
1901 123f030c Chris Buechler
				/* nothing it is handled on interface_proxyarp_configure() */
1902 e5ac67ed Ermal Lu?i
				if ($interface <> "" && $vip['interface'] <> $interface)
1903
					continue;
1904
				$anyproxyarp = true;
1905 abcb2bed Ermal Lu?i
				break;
1906
			case "ipalias":
1907 e5ac67ed Ermal Lu?i
				if ($interface <> "" && $vip['interface'] <> $interface)
1908
					continue;
1909 abcb2bed Ermal Lu?i
				interface_ipalias_configure(&$vip);
1910
				break;
1911
			case "carp":
1912 e5ac67ed Ermal Lu?i
				if ($interface <> "" && $vip['interface'] <> $interface)
1913
					continue;
1914 bce14123 Ermal
				if ($carp_setuped == false)
1915 abcb2bed Ermal Lu?i
					$carp_setuped = true;
1916
				interface_carp_configure($vip);
1917
				break;
1918
			case "carpdev-dhcp":
1919 e5ac67ed Ermal Lu?i
				if ($interface <> "" && $vip['interface'] <> $interface)
1920
					continue;
1921 abcb2bed Ermal Lu?i
				interface_carpdev_configure($vip);
1922
				break;
1923 6a74c90e Scott Ullrich
			}
1924 a04de17f Chris Buechler
		}
1925 bce14123 Ermal
		if ($carp_setuped == true)
1926
			interfaces_carp_setup();
1927 e5ac67ed Ermal Lu?i
		if ($anyproxyarp == true)
1928
			interface_proxyarp_configure();
1929 abcb2bed Ermal Lu?i
	}
1930
}
1931
1932
function interface_ipalias_configure(&$vip) {
1933
1934
	if ($vip['mode'] == "ipalias") {
1935
		$if = get_real_interface($vip['interface']);
1936
		mwexec("/sbin/ifconfig " . escapeshellarg($if) . " " . $vip['subnet'] . "/" . escapeshellarg($vip['subnet_bits']) . " alias");
1937 a04de17f Chris Buechler
	}
1938
}
1939
1940 abcb2bed Ermal Lu?i
function interface_reload_carps($cif) {
1941
	global $config;
1942
1943
	$carpifs = link_ip_to_carp_interface(find_interface_ip($cif));
1944 9006e9f8 Scott Ullrich
	if (empty($carpifs))
1945 abcb2bed Ermal Lu?i
		return;
1946
1947
	$carps = explode(" ", $carpifs);
1948
	if(is_array($config['virtualip']['vip'])) {
1949 9006e9f8 Scott Ullrich
		$viparr = &$config['virtualip']['vip'];
1950
		foreach ($viparr as $vip) {
1951 abcb2bed Ermal Lu?i
			if (in_array($vip['carpif'], $carps)) {
1952 9006e9f8 Scott Ullrich
				switch ($vip['mode']) {
1953 89830b60 Ermal
				case "carp":
1954 abcb2bed Ermal Lu?i
					interface_vip_bring_down($vip);
1955
					sleep(1);
1956 9006e9f8 Scott Ullrich
					interface_carp_configure($vip);
1957
					break;
1958 89830b60 Ermal
				case "carpdev-dhcp":
1959 abcb2bed Ermal Lu?i
					interface_vip_bring_down($vip);
1960
					sleep(1);
1961 9006e9f8 Scott Ullrich
					interface_carpdev_configure($vip);
1962
					break;
1963 89830b60 Ermal
				case "ipalias":
1964
					interface_vip_bring_down($vip);
1965
					sleep(1);
1966
					interface_ipalias_configure($vip);
1967
					break;
1968 abcb2bed Ermal Lu?i
				}
1969 9006e9f8 Scott Ullrich
			}
1970
		}
1971
	}
1972 abcb2bed Ermal Lu?i
}
1973
1974
function interface_carp_configure(&$vip) {
1975
	global $config, $g;
1976
	if(isset($config['system']['developerspew'])) {
1977 58ebf6bb Scott Ullrich
		$mt = microtime();
1978 0a595d84 Ermal Lu?i
		echo "interface_carp_configure() being called $mt\n";
1979 58ebf6bb Scott Ullrich
	}
1980 abcb2bed Ermal Lu?i
1981
	if ($vip['mode'] != "carp")
1982
		return;
1983
1984
	/*
1985
	 * ensure the interface containing the VIP really exists
1986 58ebf6bb Scott Ullrich
 	 * prevents a panic if the interface is missing or invalid
1987
	 */
1988
	$realif = get_real_interface($vip['interface']);
1989
	if (!does_interface_exist($realif)) {
1990 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", "");
1991 58ebf6bb Scott Ullrich
		return;
1992
	}
1993 abcb2bed Ermal Lu?i
1994 3502b5b1 Seth Mos
	if(is_ipaddrv4($vip['subnet'])) {
1995
		/* Ensure CARP IP really exists prior to loading up. */
1996
		$ww_subnet_ip = find_interface_ip($realif);
1997
		$ww_subnet_bits = find_interface_subnet($realif);
1998
		if (!ip_in_subnet($vip['subnet'], gen_subnet($ww_subnet_ip, $ww_subnet_bits) . "/" . $ww_subnet_bits) && !ip_in_interface_alias_subnet($vip['interface'], $vip['subnet'])) {
1999 8b6313a4 jim-p
			file_notice("CARP", sprintf(gettext("Sorry but we could not find a matching real interface subnet for the virtual IP address %s."), $vip['subnet']), "Firewall: Virtual IP", "");
2000 3502b5b1 Seth Mos
			return;
2001
		}
2002
	}
2003
	if(is_ipaddrv6($vip['subnet'])) {
2004
		/* Ensure CARP IP really exists prior to loading up. */
2005
		$ww_subnet_ip = find_interface_ipv6($realif);
2006
		$ww_subnet_bits = find_interface_subnetv6($realif);
2007
		if (!ip_in_subnet($vip['subnet'], gen_subnetv6($ww_subnet_ip, $ww_subnet_bits) . "/" . $ww_subnet_bits) && !ip_in_interface_alias_subnet($vip['interface'], $vip['subnet'])) {
2008 8b6313a4 jim-p
			file_notice("CARP", sprintf(gettext("Sorry but we could not find a matching real interface subnet for the virtual IPv6 address %s."), $vip['subnet']), "Firewall: Virtual IP", "");
2009 3502b5b1 Seth Mos
			return;
2010
		}
2011 f99aa333 Ermal
	}
2012
2013 7b47bd4c Ermal
	// set the vip interface to the vhid
2014
	$vipif = "{$vip['interface']}_vip{$vip['vhid']}";
2015 abcb2bed Ermal Lu?i
2016
	/* create the carp interface and setup */
2017 37a53d16 Scott Ullrich
	if (does_interface_exist($vipif)) {
2018 871768cf Ermal
		pfSense_interface_flags($vipif, -IFF_UP);
2019 37a53d16 Scott Ullrich
	} else {
2020 871768cf Ermal
		$carpif = pfSense_interface_create("carp");
2021
		pfSense_interface_rename($carpif, $vipif);
2022
		pfSense_ngctl_name("{$carpif}:", $vipif);
2023 abcb2bed Ermal Lu?i
	}
2024
2025
	/* invalidate interface cache */
2026
	get_interface_arr(true);
2027
2028 7b47bd4c Ermal
	$vip_password = $vip['password'];
2029
	$vip_password = escapeshellarg(addslashes(str_replace(" ", "", $vip_password)));
2030
	if ($vip['password'] != "")
2031
		$password = " pass {$vip_password}";
2032 a687f866 Namezero
2033 7b47bd4c Ermal
	$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
2034 100b7219 Ermal
	$advbase = "";
2035
	if (!empty($vip['advbase']))
2036
		$advbase = "advbase {$vip['advbase']}";
2037 1f74cd2d Seth Mos
2038 3502b5b1 Seth Mos
	if(is_ipaddrv4($vip['subnet'])) {
2039
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
2040 9caffe86 Seth Mos
		mwexec("/sbin/ifconfig {$vipif} {$vip['subnet']}/{$vip['subnet_bits']} vhid {$vip['vhid']} advskew {$vip['advskew']} {$advbase} {$password}");
2041 3502b5b1 Seth Mos
	}
2042
	if(is_ipaddrv6($vip['subnet'])) {
2043
		$broadcast_address = gen_subnet_max($vip['subnet'], $vip['subnet_bits']);
2044 9caffe86 Seth Mos
		mwexec("/sbin/ifconfig {$vipif} inet6 {$vip['subnet']} prefixlen {$vip['subnet_bits']} vhid {$vip['vhid']} advskew {$vip['advskew']} {$advbase} {$password}");
2045 42f74491 jim-p
		/* make sure to add a link local address too */
2046 350471d5 smos
		mwexec("/sbin/ifconfig {$vipif} inet6 fe80::5:{$vip['vhid']} vhid {$vip['vhid']} advskew {$vip['advskew']} {$advbase} {$password}");
2047 3502b5b1 Seth Mos
	}
2048 abcb2bed Ermal Lu?i
2049
	interfaces_bring_up($vipif);
2050 7b47bd4c Ermal
2051 abcb2bed Ermal Lu?i
	return $vipif;
2052
}
2053
2054
function interface_carpdev_configure(&$vip) {
2055
	global $g;
2056
2057
	if ($vip['mode'] != "carpdev-dhcp")
2058 9006e9f8 Scott Ullrich
		return;
2059 abcb2bed Ermal Lu?i
2060
	if (empty($vip['interface']))
2061
		return;
2062
2063 265c88c6 Ermal
	$realif = get_real_interface($vip['interface']);
2064 ec054b7c Scott Ullrich
	interfaces_bring_up($realif);
2065 7b47bd4c Ermal
2066 9006e9f8 Scott Ullrich
	/*
2067
	 * ensure the interface containing the VIP really exists
2068
	 * prevents a panic if the interface is missing or invalid
2069
	 */
2070
	if (!does_interface_exist($realif)) {
2071 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", "");
2072 9006e9f8 Scott Ullrich
		return;
2073
	}
2074 abcb2bed Ermal Lu?i
2075 7b47bd4c Ermal
	$vipif = "{$vip['interface']}_vip{$vip['vhid']}";
2076 f07bee94 Scott Ullrich
	if (does_interface_exist($vipif)) {
2077 37a53d16 Scott Ullrich
		interface_bring_down($vipif);
2078 f07bee94 Scott Ullrich
	} else {
2079 abcb2bed Ermal Lu?i
		$carpdevif = exec("/sbin/ifconfig carp create");
2080
		mwexec("/sbin/ifconfig {$carpdevif} name {$vipif}");
2081 871768cf Ermal
		pfSense_ngctl_name("{$carpdevif}:", $vipif);
2082 abcb2bed Ermal Lu?i
	}
2083
2084 7b47bd4c Ermal
	$vip_password = $vip['password'];
2085
	$vip_password = str_replace(" ", "", $vip_password);
2086
	if ($vip['password'] != "")
2087
		$password = " pass \"" . $vip_password . "\"";
2088
2089
2090 6f247d1f Ermal
	mwexec("/sbin/ifconfig {$vipif} carpdev {$realif} vhid {$vip['vhid']} advskew {$vip['advskew']} advbase {$vip['advbase']} {$password}");
2091 ec054b7c Scott Ullrich
	interfaces_bring_up($vipif);
2092 abcb2bed Ermal Lu?i
2093
	/*
2094
	 * XXX: BIG HACK but carpdev needs ip services active
2095
	 *      before even starting something as dhclient.
2096
	 *      I do not know if this is a feature or a bug
2097
	 *      but better than track it make it work ;) .
2098
	 */
2099
	//$fakeiptouse = "10.254.254." . ($carp_instances_counter+1);
2100
	//$cmdchain->add("CarpDEV hack", "/sbin/ifconfig {$carpint} inet {$fakeiptouse}", false);
2101
2102
	/* generate dhclient_wan.conf */
2103
	$fd = fopen("{$g['varetc_path']}/dhclient_{$vipif}.conf", "w");
2104
	if ($fd) {
2105
		$dhclientconf = "";
2106
2107
		$dhclientconf .= <<<EOD
2108
interface "{$vipif}" {
2109
timeout 60;
2110
retry 1;
2111
select-timeout 0;
2112
initial-interval 1;
2113
script "/sbin/dhclient-script";
2114
}
2115
2116
EOD;
2117
2118
		fwrite($fd, $dhclientconf);
2119
		fclose($fd);
2120
2121
		/* fire up dhclient */
2122 0dcdbc85 Scott Ullrich
		mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$vipif}.conf {$vipif} >{$g['tmp_path']}/{$vipif}_output 2>{$g['tmp_path']}/{$vipif}_error_output", false);
2123 abcb2bed Ermal Lu?i
	} else {
2124 07e40c1f Carlos Eduardo Ramos
		log_error(sprintf(gettext("Error: cannot open dhclient_%s.conf in interfaces_carpdev_configure() for writing.%s"), $vipif, "\n"));
2125 abcb2bed Ermal Lu?i
		mwexec("/sbin/dhclient -b {$vipif}");
2126
	}
2127
2128
	return $vipif;
2129
}
2130
2131 854aed18 Ermal Lu?i
function interface_wireless_clone($realif, $wlcfg) {
2132 568b1358 Scott Ullrich
	global $config, $g;
2133 88157f66 Scott Ullrich
	/*   Check to see if interface has been cloned as of yet.  
2134
	 *   If it has not been cloned then go ahead and clone it.
2135
	 */
2136 2a203afd Seth Mos
	$needs_clone = false;
2137 9f428275 Erik Fonnesbeck
	if(is_array($wlcfg['wireless']))
2138
		$wlcfg_mode = $wlcfg['wireless']['mode'];
2139
	else
2140
		$wlcfg_mode = $wlcfg['mode'];
2141
	switch($wlcfg_mode) {
2142 2a203afd Seth Mos
		 case "hostap":
2143
			$mode = "wlanmode hostap";
2144
			break;
2145
		 case "adhoc":
2146
			$mode = "wlanmode adhoc";
2147
			break;
2148
		 default:
2149
			$mode = "";
2150
			break;
2151
	}
2152 34808d4e Erik Fonnesbeck
	$baseif = interface_get_wireless_base($wlcfg['if']);
2153 854aed18 Ermal Lu?i
	if(does_interface_exist($realif)) {
2154
		exec("/sbin/ifconfig {$realif}", $output, $ret);
2155 2a203afd Seth Mos
		$ifconfig_str = implode($output);
2156 9f428275 Erik Fonnesbeck
		if(($wlcfg_mode == "hostap") && (! preg_match("/hostap/si", $ifconfig_str))) {
2157 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Interface %s changed to hostap mode"), $realif));
2158 2a203afd Seth Mos
			$needs_clone = true;
2159
		}
2160 9f428275 Erik Fonnesbeck
		if(($wlcfg_mode == "adhoc") && (! preg_match("/adhoc/si", $ifconfig_str))) {
2161 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Interface %s changed to adhoc mode"), $realif));
2162 2a203afd Seth Mos
			$needs_clone = true;
2163
		}
2164 9f428275 Erik Fonnesbeck
		if(($wlcfg_mode == "bss") && (preg_match("/hostap|adhoc/si", $ifconfig_str))) {
2165 07e40c1f Carlos Eduardo Ramos
			log_error(sprintf(gettext("Interface %s changed to infrastructure mode"), $realif));
2166 2a203afd Seth Mos
			$needs_clone = true;
2167
		}
2168
	} else {
2169
		$needs_clone = true;
2170 88157f66 Scott Ullrich
	}
2171 2a203afd Seth Mos
2172 19e83210 Scott Ullrich
	if($needs_clone == true) {
2173 2a203afd Seth Mos
		/* remove previous instance if it exists */
2174 854aed18 Ermal Lu?i
		if(does_interface_exist($realif))
2175 871768cf Ermal
			pfSense_interface_destroy($realif);
2176 854aed18 Ermal Lu?i
2177 07e40c1f Carlos Eduardo Ramos
		log_error(sprintf(gettext("Cloning new wireless interface %s"), $realif));
2178 b99256c1 Scott Ullrich
		// Create the new wlan interface. FreeBSD returns the new interface name.
2179
		// example:  wlan2
2180 6d54e865 Erik Fonnesbeck
		exec("/sbin/ifconfig wlan create wlandev {$baseif} {$mode} bssid 2>&1", $out, $ret);
2181 2a203afd Seth Mos
		if($ret <> 0) {
2182 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]));
2183 9f428275 Erik Fonnesbeck
			return false;
2184 2a203afd Seth Mos
		}
2185
		$newif = trim($out[0]);
2186
		// Rename the interface to {$parentnic}_wlan{$number}#: EX: ath0_wlan0
2187 871768cf Ermal
		pfSense_interface_rename($newif, $realif);
2188 2a203afd Seth Mos
		// FIXME: not sure what ngctl is for. Doesn't work.
2189 fa71a9b6 Erik Fonnesbeck
		// mwexec("/usr/sbin/ngctl name {$newif}: {$realif}", false);
2190 acb0bce0 Erik Fonnesbeck
		file_put_contents("{$g['tmp_path']}/{$realif}_oldmac", get_interface_mac($realif));
2191 88157f66 Scott Ullrich
	}
2192 9f428275 Erik Fonnesbeck
	return true;
2193 88157f66 Scott Ullrich
}
2194
2195 8f0289e7 Erik Fonnesbeck
function interface_sync_wireless_clones(&$ifcfg, $sync_changes = false) {
2196
	global $config, $g;
2197
2198 56626335 Erik Fonnesbeck
	$shared_settings = array('standard', 'turbo', 'protmode', 'txpower', 'channel',
2199
	                         'diversity', 'txantenna', 'rxantenna', 'distance',
2200
	                         'regdomain', 'regcountry', 'reglocation');
2201 8f0289e7 Erik Fonnesbeck
2202 263e2b7e Erik Fonnesbeck
	if(!is_interface_wireless($ifcfg['if']))
2203 7de319a1 Erik Fonnesbeck
		return;
2204
2205 34808d4e Erik Fonnesbeck
	$baseif = interface_get_wireless_base($ifcfg['if']);
2206 8f0289e7 Erik Fonnesbeck
2207 062023a5 Erik Fonnesbeck
	// Sync shared settings for assigned clones
2208 38b7d47d Erik Fonnesbeck
	$iflist = get_configured_interface_list(false, true);
2209 8f0289e7 Erik Fonnesbeck
	foreach ($iflist as $if) {
2210 34808d4e Erik Fonnesbeck
		if ($baseif == interface_get_wireless_base($config['interfaces'][$if]['if']) && $ifcfg['if'] != $config['interfaces'][$if]['if']) {
2211 8f0289e7 Erik Fonnesbeck
			if (isset($config['interfaces'][$if]['wireless']['standard']) || $sync_changes) {
2212
				foreach ($shared_settings as $setting) {
2213
					if ($sync_changes) {
2214 56626335 Erik Fonnesbeck
						if (isset($ifcfg['wireless'][$setting]))
2215
							$config['interfaces'][$if]['wireless'][$setting] = $ifcfg['wireless'][$setting];
2216
						else if (isset($config['interfaces'][$if]['wireless'][$setting]))
2217
							unset($config['interfaces'][$if]['wireless'][$setting]);
2218 8f0289e7 Erik Fonnesbeck
					} else {
2219 56626335 Erik Fonnesbeck
						if (isset($config['interfaces'][$if]['wireless'][$setting]))
2220
							$ifcfg['wireless'][$setting] = $config['interfaces'][$if]['wireless'][$setting];
2221
						else if (isset($ifcfg['wireless'][$setting]))
2222
							unset($ifcfg['wireless'][$setting]);
2223 8f0289e7 Erik Fonnesbeck
					}
2224
				}
2225
				if (!$sync_changes)
2226
					break;
2227
			}
2228
		}
2229
	}
2230 263e2b7e Erik Fonnesbeck
2231 062023a5 Erik Fonnesbeck
	// Read or write settings at shared area
2232 f62c44d8 Erik Fonnesbeck
	if (isset($config['wireless']['interfaces'][$baseif])) {
2233
		foreach ($shared_settings as $setting) {
2234
			if ($sync_changes) {
2235 56626335 Erik Fonnesbeck
				if (isset($ifcfg['wireless'][$setting]))
2236
					$config['wireless']['interfaces'][$baseif][$setting] = $ifcfg['wireless'][$setting];
2237
				else if (isset($config['wireless']['interfaces'][$baseif][$setting]))
2238
					unset($config['wireless']['interfaces'][$baseif][$setting]);
2239 f62c44d8 Erik Fonnesbeck
			} else if (isset($config['wireless']['interfaces'][$baseif][$setting])) {
2240 56626335 Erik Fonnesbeck
				if (isset($config['wireless']['interfaces'][$baseif][$setting]))
2241
					$ifcfg['wireless'][$setting] = $config['wireless']['interfaces'][$baseif][$setting];
2242
				else if (isset($ifcfg['wireless'][$setting]))
2243
					unset($ifcfg['wireless'][$setting]);
2244 f62c44d8 Erik Fonnesbeck
			}
2245 062023a5 Erik Fonnesbeck
		}
2246
	}
2247
2248
	// Sync the mode on the clone creation page with the configured mode on the interface
2249 263e2b7e Erik Fonnesbeck
	if (interface_is_wireless_clone($ifcfg['if'])) {
2250
		foreach ($config['wireless']['clone'] as &$clone) {
2251
			if ($clone['cloneif'] == $ifcfg['if']) {
2252
				if ($sync_changes) {
2253
					$clone['mode'] = $ifcfg['wireless']['mode'];
2254
				} else {
2255
					$ifcfg['wireless']['mode'] = $clone['mode'];
2256
				}
2257
				break;
2258
			}
2259
		}
2260 867d444b Erik Fonnesbeck
		unset($clone);
2261 263e2b7e Erik Fonnesbeck
	}
2262 8f0289e7 Erik Fonnesbeck
}
2263
2264 19e83210 Scott Ullrich
function interface_wireless_configure($if, &$wl, &$wlcfg) {
2265 ac3f8318 Espen Johansen
	global $config, $g;
2266 eb772abd Scott Ullrich
2267 4742e927 Scott Ullrich
	/*    open up a shell script that will be used to output the commands.
2268
	 *    since wireless is changing a lot, these series of commands are fragile
2269
     *    and will sometimes need to be verified by a operator by executing the command
2270
     *    and returning the output of the command to the developers for inspection.  please
2271
     *    do not change this routine from a shell script to individul exec commands.  -sullrich
2272
	 */
2273 eb772abd Scott Ullrich
2274 b99256c1 Scott Ullrich
	// Remove script file
2275 490b8b2a Scott Ullrich
	unlink_if_exists("{$g['tmp_path']}/{$if}_setup.sh");
2276 eb772abd Scott Ullrich
2277 0a28d385 Erik Fonnesbeck
	// Clone wireless nic if needed.
2278
	interface_wireless_clone($if, $wl);
2279
2280 8f0289e7 Erik Fonnesbeck
	// Reject inadvertent changes to shared settings in case the interface hasn't been configured.
2281
	interface_sync_wireless_clones($wl, false);
2282
2283 6955830f Ermal Lu?i
	$fd_set = fopen("{$g['tmp_path']}/{$if}_setup.sh","w");
2284 4742e927 Scott Ullrich
	fwrite($fd_set, "#!/bin/sh\n");
2285 36d0358b Scott Ullrich
	fwrite($fd_set, "# {$g['product_name']} wireless configuration script.\n\n");
2286 eb772abd Scott Ullrich
2287 2ac908dd Espen Johansen
	/* set values for /path/program */
2288
	$hostapd = "/usr/sbin/hostapd";
2289
	$wpa_supplicant = "/usr/sbin/wpa_supplicant";
2290 4742e927 Scott Ullrich
	$ifconfig = "/sbin/ifconfig";
2291 56626335 Erik Fonnesbeck
	$sysctl = "/sbin/sysctl";
2292 4742e927 Scott Ullrich
	$killall = "/usr/bin/killall";
2293 2ac908dd Espen Johansen
2294 a59abc65 Scott Ullrich
	/* Set all wireless ifconfig variables (splitt up to get rid of needed checking) */
2295 5508cf57 Scott Ullrich
2296 2a203afd Seth Mos
	$wlcmd = array();
2297 56626335 Erik Fonnesbeck
	$wl_sysctl = array();
2298 2a203afd Seth Mos
	/* Make sure it's up */
2299
	$wlcmd[] = "up";
2300 ac3f8318 Espen Johansen
	/* Set a/b/g standard */
2301 9be20928 Erik Fonnesbeck
	$standard = str_replace(" Turbo", "", $wlcfg['standard']);
2302
	$wlcmd[] = "mode " . escapeshellarg($standard);
2303 2a203afd Seth Mos
2304 5030b5eb Erik Fonnesbeck
	/* XXX: Disable ampdu for now on mwl when running in 11n mode
2305
	 * to prevent massive packet loss under certain conditions. */
2306 9be20928 Erik Fonnesbeck
	if(preg_match("/^mwl/i", $if) && ($standard == "11ng" || $standard == "11na"))
2307 5030b5eb Erik Fonnesbeck
		$wlcmd[] = "-ampdu";
2308
2309 2a203afd Seth Mos
	/* Set ssid */
2310
	if($wlcfg['ssid'])
2311
		$wlcmd[] = "ssid " .escapeshellarg($wlcfg['ssid']);
2312 5508cf57 Scott Ullrich
2313 0856c4ac Scott Ullrich
	/* Set 802.11g protection mode */
2314 2a203afd Seth Mos
	$wlcmd[] = "protmode " . escapeshellarg($wlcfg['protmode']);
2315 0856c4ac Scott Ullrich
2316 ac3f8318 Espen Johansen
	/* set wireless channel value */
2317 2a203afd Seth Mos
	if(isset($wlcfg['channel'])) {
2318
		if($wlcfg['channel'] == "0") {
2319
			$wlcmd[] = "channel any";
2320
		} else {
2321
			$wlcmd[] = "channel " . escapeshellarg($wlcfg['channel']);
2322
		}
2323
	}
2324 2ac908dd Espen Johansen
2325 56626335 Erik Fonnesbeck
	/* Set antenna diversity value */
2326
	if(isset($wlcfg['diversity']))
2327
		$wl_sysctl[] = "diversity=" . escapeshellarg($wlcfg['diversity']);
2328
2329
	/* Set txantenna value */
2330
	if(isset($wlcfg['txantenna']))
2331
		$wl_sysctl[] = "txantenna=" . escapeshellarg($wlcfg['txantenna']);
2332
2333
	/* Set rxantenna value */
2334
	if(isset($wlcfg['rxantenna']))
2335
		$wl_sysctl[] = "rxantenna=" . escapeshellarg($wlcfg['rxantenna']);
2336
2337 f134033e Scott Ullrich
	/* set Distance value */
2338 eb772abd Scott Ullrich
	if($wlcfg['distance'])
2339 f134033e Scott Ullrich
		$distance = escapeshellarg($wlcfg['distance']);
2340
2341 ac3f8318 Espen Johansen
	/* Set wireless hostap mode */
2342 2a203afd Seth Mos
	if ($wlcfg['mode'] == "hostap") {
2343
		$wlcmd[] = "mediaopt hostap";
2344
	} else {
2345
		$wlcmd[] = "-mediaopt hostap";
2346
	}
2347 ac3f8318 Espen Johansen
2348
	/* Set wireless adhoc mode */
2349 2a203afd Seth Mos
	if ($wlcfg['mode'] == "adhoc") {
2350
		$wlcmd[] = "mediaopt adhoc";
2351
	} else {
2352
		$wlcmd[] = "-mediaopt adhoc";
2353
	}
2354 ac3f8318 Espen Johansen
2355
	/* Not neccesary to set BSS mode as this is default if adhoc and/or hostap is NOT set */
2356
2357
	/* handle hide ssid option */
2358 2a203afd Seth Mos
	if(isset($wlcfg['hidessid']['enable'])) {
2359
		$wlcmd[] = "hidessid";
2360
	} else {
2361
		$wlcmd[] = "-hidessid";
2362
	}
2363 ac3f8318 Espen Johansen
2364
	/* handle pureg (802.11g) only option */
2365 2a203afd Seth Mos
	if(isset($wlcfg['pureg']['enable'])) {
2366
		$wlcmd[] = "mode 11g pureg";
2367
	} else {
2368
		$wlcmd[] = "-pureg";
2369
	}
2370 ac3f8318 Espen Johansen
2371 ed459692 Erik Fonnesbeck
	/* handle puren (802.11n) only option */
2372
	if(isset($wlcfg['puren']['enable'])) {
2373
		$wlcmd[] = "puren";
2374
	} else {
2375
		$wlcmd[] = "-puren";
2376
	}
2377
2378 ac3f8318 Espen Johansen
	/* enable apbridge option */
2379 2a203afd Seth Mos
	if(isset($wlcfg['apbridge']['enable'])) {
2380
		$wlcmd[] = "apbridge";
2381
	} else {
2382
		$wlcmd[] = "-apbridge";
2383
	}
2384 ac3f8318 Espen Johansen
2385
	/* handle turbo option */
2386 2a203afd Seth Mos
	if(isset($wlcfg['turbo']['enable'])) {
2387
		$wlcmd[] = "mediaopt turbo";
2388
	} else {
2389
		$wlcmd[] = "-mediaopt turbo";
2390
	}
2391 ac3f8318 Espen Johansen
2392
	/* handle txpower setting */
2393 2a203afd Seth Mos
	/* if($wlcfg['txpower'] <> "")
2394
		$wlcmd[] = "txpower " . escapeshellarg($wlcfg['txpower']);
2395
	*/
2396 ac3f8318 Espen Johansen
	/* handle wme option */
2397 2a203afd Seth Mos
	if(isset($wlcfg['wme']['enable'])) {
2398
		$wlcmd[] = "wme";
2399
	} else {
2400
		$wlcmd[] = "-wme";
2401
	}
2402 eb772abd Scott Ullrich
2403 ac3f8318 Espen Johansen
	/* set up wep if enabled */
2404 2a203afd Seth Mos
	$wepset = "";
2405
	if (isset($wlcfg['wep']['enable']) && is_array($wlcfg['wep']['key'])) {
2406
		switch($wlcfg['wpa']['auth_algs']) {
2407
			case "1":
2408
				$wepset .= "authmode open wepmode on ";
2409
				break;
2410
			case "2":
2411
				$wepset .= "authmode shared wepmode on ";
2412
				break;
2413
			case "3":
2414
				$wepset .= "authmode mixed wepmode on ";
2415
		}
2416 2f19fa14 Scott Ullrich
		$i = 1;
2417
		foreach ($wlcfg['wep']['key'] as $wepkey) {
2418
			$wepset .= "wepkey " . escapeshellarg("{$i}:{$wepkey['value']}") . " ";
2419 2a203afd Seth Mos
			if (isset($wepkey['txkey'])) {
2420
				$wlcmd[] = "weptxkey {$i} ";
2421
			}
2422 2f19fa14 Scott Ullrich
			$i++;
2423
		}
2424 2a203afd Seth Mos
		$wlcmd[] = $wepset;
2425
	} else {
2426
		$wlcmd[] = "authmode open wepmode off ";
2427 ac3f8318 Espen Johansen
	}
2428
2429 c8178bb7 Erik Fonnesbeck
	mwexec(kill_hostapd("{$if}"));
2430
	mwexec(kill_wpasupplicant("{$if}"));
2431
2432 ac3f8318 Espen Johansen
	/* generate wpa_supplicant/hostap config if wpa is enabled */
2433 2a203afd Seth Mos
	conf_mount_rw();
2434 ac3f8318 Espen Johansen
2435
	switch ($wlcfg['mode']) {
2436 b67d192d Scott Ullrich
		case 'bss':
2437 ac3f8318 Espen Johansen
			if (isset($wlcfg['wpa']['enable'])) {
2438
				$wpa .= <<<EOD
2439 454756b9 Scott Ullrich
ctrl_interface={$g['varrun_path']}/wpa_supplicant
2440 50ad3b7c Scott Ullrich
ctrl_interface_group=0
2441
ap_scan=1
2442 2ac908dd Espen Johansen
#fast_reauth=1
2443 249558a2 Scott Ullrich
network={
2444 454756b9 Scott Ullrich
ssid="{$wlcfg['ssid']}"
2445
scan_ssid=1
2446 2ac908dd Espen Johansen
priority=5
2447
key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
2448 454756b9 Scott Ullrich
psk="{$wlcfg['wpa']['passphrase']}"
2449 2ac908dd Espen Johansen
pairwise={$wlcfg['wpa']['wpa_pairwise']}
2450
group={$wlcfg['wpa']['wpa_pairwise']}
2451 50ad3b7c Scott Ullrich
}
2452
EOD;
2453
2454 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/wpa_supplicant_{$if}.conf", "w");
2455 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
2456
				fclose($fd);
2457
			}
2458 2a203afd Seth Mos
			break;
2459 ac3f8318 Espen Johansen
		case 'hostap':
2460 7eadaa9c Scott Ullrich
			if($wlcfg['wpa']['passphrase']) 
2461
				$wpa_passphrase = "wpa_passphrase={$wlcfg['wpa']['passphrase']}\n";
2462 abfd0c9b Scott Ullrich
			else 
2463
				$wpa_passphrase = "";
2464 ac3f8318 Espen Johansen
			if (isset($wlcfg['wpa']['enable'])) {
2465
				$wpa .= <<<EOD
2466 459d6351 Scott Ullrich
interface={$if}
2467
driver=bsd
2468
logger_syslog=-1
2469
logger_syslog_level=0
2470
logger_stdout=-1
2471
logger_stdout_level=0
2472 2ac908dd Espen Johansen
dump_file={$g['tmp_path']}/hostapd_{$if}.dump
2473
ctrl_interface={$g['varrun_path']}/hostapd
2474 459d6351 Scott Ullrich
ctrl_interface_group=wheel
2475 2ac908dd Espen Johansen
#accept_mac_file={$g['tmp_path']}/hostapd_{$if}.accept
2476
#deny_mac_file={$g['tmp_path']}/hostapd_{$if}.deny
2477 b67d192d Scott Ullrich
#macaddr_acl={$wlcfg['wpa']['macaddr_acl']}
2478 459d6351 Scott Ullrich
ssid={$wlcfg['ssid']}
2479 2ac908dd Espen Johansen
debug={$wlcfg['wpa']['debug_mode']}
2480
auth_algs={$wlcfg['wpa']['auth_algs']}
2481
wpa={$wlcfg['wpa']['wpa_mode']}
2482
wpa_key_mgmt={$wlcfg['wpa']['wpa_key_mgmt']}
2483
wpa_pairwise={$wlcfg['wpa']['wpa_pairwise']}
2484 ac3f8318 Espen Johansen
wpa_group_rekey={$wlcfg['wpa']['wpa_group_rekey']}
2485
wpa_gmk_rekey={$wlcfg['wpa']['wpa_gmk_rekey']}
2486
wpa_strict_rekey={$wlcfg['wpa']['wpa_strict_rekey']}
2487 7eadaa9c Scott Ullrich
{$wpa_passphrase}
2488 525d565b Scott Ullrich
2489 459d6351 Scott Ullrich
EOD;
2490 2ac908dd Espen Johansen
2491 c9e7d30d Scott Ullrich
if (isset($wlcfg['wpa']['rsn_preauth'])) {
2492
	$wpa .= <<<EOD
2493
# Enable the next lines for preauth when roaming. Interface = wired or wireless interface talking to the AP you want to roam from/to
2494
rsn_preauth=1
2495
rsn_preauth_interfaces={$if}
2496
2497
EOD;
2498
2499
}
2500 5949124c Scott Ullrich
				if($wlcfg['auth_server_addr'] && $wlcfg['auth_server_shared_secret']) {
2501
					$auth_server_port = "1812";
2502
					if($wlcfg['auth_server_port']) 
2503
						$auth_server_port = $wlcfg['auth_server_port'];
2504 a687f866 Namezero
					$auth_server_port2 = "1812";
2505
					if($wlcfg['auth_server_port2']) 
2506
						$auth_server_port2 = $wlcfg['auth_server_port2'];
2507 5949124c Scott Ullrich
					$wpa .= <<<EOD
2508 525d565b Scott Ullrich
2509 5949124c Scott Ullrich
ieee8021x=1
2510
auth_server_addr={$wlcfg['auth_server_addr']}
2511
auth_server_port={$auth_server_port}
2512
auth_server_shared_secret={$wlcfg['auth_server_shared_secret']}
2513 a687f866 Namezero
auth_server_addr={$wlcfg['auth_server_addr2']}
2514
auth_server_port={$auth_server_port2}
2515
auth_server_shared_secret={$wlcfg['auth_server_shared_secret2']}
2516 525d565b Scott Ullrich
2517 459d6351 Scott Ullrich
EOD;
2518 5949124c Scott Ullrich
				} else {
2519
					$wpa .= "ieee8021x={$wlcfg['wpa']['ieee8021x']}\n";
2520
				}
2521 2ac908dd Espen Johansen
2522 80ec5eaa Scott Ullrich
				$fd = fopen("{$g['varetc_path']}/hostapd_{$if}.conf", "w");
2523 ac3f8318 Espen Johansen
				fwrite($fd, "{$wpa}");
2524
				fclose($fd);
2525 2ac908dd Espen Johansen
2526 ac3f8318 Espen Johansen
			}
2527 2a203afd Seth Mos
			break;
2528 eb772abd Scott Ullrich
	}
2529 ac3f8318 Espen Johansen
2530 4742e927 Scott Ullrich
	/*
2531
	 *    all variables are set, lets start up everything
2532 2a203afd Seth Mos
	 */
2533 eb772abd Scott Ullrich
2534 bbfc810e Erik Fonnesbeck
	$baseif = interface_get_wireless_base($if);
2535 56626335 Erik Fonnesbeck
	preg_match("/^(.*?)([0-9]*)$/", $baseif, $baseif_split);
2536
	$wl_sysctl_prefix = 'dev.' . $baseif_split[1] . '.' . $baseif_split[2];
2537
2538
	/* set sysctls for the wireless interface */
2539
	if (!empty($wl_sysctl)) {
2540
		fwrite($fd_set, "# sysctls for {$baseif}\n");
2541
		foreach ($wl_sysctl as $wl_sysctl_line) {
2542
			fwrite($fd_set, "{$sysctl} {$wl_sysctl_prefix}.{$wl_sysctl_line}\n");
2543
		}
2544
	}
2545 bbfc810e Erik Fonnesbeck
2546 78922914 Scott Ullrich
	/* set ack timers according to users preference (if he/she has any) */
2547
	if($distance) {
2548 4742e927 Scott Ullrich
		fwrite($fd_set, "# Enable ATH distance settings\n");
2549 e327021d Erik Fonnesbeck
		fwrite($fd_set, "/sbin/athctrl.sh -i {$baseif} -d {$distance}\n");
2550 78922914 Scott Ullrich
	}
2551 eb772abd Scott Ullrich
2552 ac3f8318 Espen Johansen
	if (isset($wlcfg['wpa']['enable'])) {
2553 2a203afd Seth Mos
		if ($wlcfg['mode'] == "bss") {
2554 4742e927 Scott Ullrich
			fwrite($fd_set, "{$wpa_supplicant} -B -i {$if} -c {$g['varetc_path']}/wpa_supplicant_{$if}.conf\n");
2555 2a203afd Seth Mos
		}
2556
		if ($wlcfg['mode'] == "hostap") {
2557 864bf774 Erik Fonnesbeck
			/* add line to script to restore old mac to make hostapd happy */
2558 acb0bce0 Erik Fonnesbeck
			if (file_exists("{$g['tmp_path']}/{$if}_oldmac")) {
2559
				$if_oldmac = file_get_contents("{$g['tmp_path']}/{$if}_oldmac");
2560
				if (is_macaddr($if_oldmac))
2561
					fwrite($fd_set, "{$ifconfig} " . escapeshellarg($if) .
2562
						" link " . escapeshellarg($if_oldmac) . "\n");
2563
			}
2564
2565 4742e927 Scott Ullrich
			fwrite($fd_set, "{$hostapd} -B {$g['varetc_path']}/hostapd_{$if}.conf\n");
2566 864bf774 Erik Fonnesbeck
2567
			/* add line to script to restore spoofed mac after running hostapd */
2568
			if (file_exists("{$g['tmp_path']}/{$if}_oldmac")) {
2569
				if ($wl['spoofmac'])
2570
					$if_curmac = $wl['spoofmac'];
2571
				else
2572
					$if_curmac = get_interface_mac($if);
2573
				if (is_macaddr($if_curmac))
2574
					fwrite($fd_set, "{$ifconfig} " . escapeshellarg($if) .
2575
						" link " . escapeshellarg($if_curmac) . "\n");
2576
			}
2577 2a203afd Seth Mos
		}
2578 ac3f8318 Espen Johansen
	}
2579 191a8175 Scott Ullrich
2580 4742e927 Scott Ullrich
	fclose($fd_set);
2581 8a958125 Scott Ullrich
	conf_mount_ro();
2582
2583 bbfc810e Erik Fonnesbeck
	/* Making sure regulatory settings have actually changed
2584
	 * before applying, because changing them requires bringing
2585
	 * down all wireless networks on the interface. */
2586
	exec("{$ifconfig} " . escapeshellarg($if), $output);
2587
	$ifconfig_str = implode($output);
2588
	unset($output);
2589
	$reg_changing = false;
2590
2591 89e7778f Erik Fonnesbeck
	/* special case for the debug country code */
2592
	if ($wlcfg['regcountry'] == 'DEBUG' && !preg_match("/\sregdomain\s+DEBUG\s/si", $ifconfig_str))
2593
		$reg_changing = true;
2594
	else if ($wlcfg['regdomain'] && !preg_match("/\sregdomain\s+{$wlcfg['regdomain']}\s/si", $ifconfig_str))
2595 bbfc810e Erik Fonnesbeck
		$reg_changing = true;
2596
	else if ($wlcfg['regcountry'] && !preg_match("/\scountry\s+{$wlcfg['regcountry']}\s/si", $ifconfig_str))
2597
		$reg_changing = true;
2598 89e7778f Erik Fonnesbeck
	else if ($wlcfg['reglocation'] == 'anywhere' && preg_match("/\s(indoor|outdoor)\s/si", $ifconfig_str))
2599
		$reg_changing = true;
2600 06cb2656 Erik Fonnesbeck
	else if ($wlcfg['reglocation'] && $wlcfg['reglocation'] != 'anywhere' && !preg_match("/\s{$wlcfg['reglocation']}\s/si", $ifconfig_str))
2601 bbfc810e Erik Fonnesbeck
		$reg_changing = true;
2602
2603
	if ($reg_changing) {
2604
		/* set regulatory domain */
2605
		if($wlcfg['regdomain'])
2606
			$wlregcmd[] = "regdomain " . escapeshellarg($wlcfg['regdomain']);
2607
2608
		/* set country */
2609
		if($wlcfg['regcountry'])
2610
			$wlregcmd[] = "country " . escapeshellarg($wlcfg['regcountry']);
2611
2612
		/* set location */
2613
		if($wlcfg['reglocation'])
2614
			$wlregcmd[] = escapeshellarg($wlcfg['reglocation']);
2615
2616
		$wlregcmd_args = implode(" ", $wlregcmd);
2617
2618
		/* build a complete list of the wireless clones for this interface */
2619
		$clone_list = array();
2620
		if (does_interface_exist(interface_get_wireless_clone($baseif)))
2621
			$clone_list[] = interface_get_wireless_clone($baseif);
2622
		if (is_array($config['wireless']['clone'])) {
2623
			foreach ($config['wireless']['clone'] as $clone) {
2624
				if ($clone['if'] == $baseif)
2625
					$clone_list[] = $clone['cloneif'];
2626
			}
2627
		}
2628
2629
		/* find which clones are up and bring them down */
2630
		$clones_up = array();
2631
		foreach ($clone_list as $clone_if) {
2632 1cf76394 Erik Fonnesbeck
			$clone_status = pfSense_get_interface_addresses($clone_if);
2633 bbfc810e Erik Fonnesbeck
			if ($clone_status['status'] == 'up') {
2634
				$clones_up[] = $clone_if;
2635
				mwexec("{$ifconfig} " . escapeshellarg($clone_if) . " down");
2636
			}
2637
		}
2638
2639
		/* apply the regulatory settings */
2640
		mwexec("{$ifconfig} " . escapeshellarg($if) . " {$wlregcmd_args}");
2641
2642
		/* bring the clones back up that were previously up */
2643
		foreach ($clones_up as $clone_if) {
2644
			mwexec("{$ifconfig} " . escapeshellarg($clone_if) . " up");
2645 67e77adf Erik Fonnesbeck
2646
			/*
2647
			 * Rerun the setup script for the interface if it isn't this interface, the interface
2648
			 * is in infrastructure mode, and WPA is enabled.
2649
			 * This can be removed if wpa_supplicant stops dying when you bring the interface down.
2650
			 */
2651
			if ($clone_if != $if) {
2652
				$friendly_if = convert_real_interface_to_friendly_interface_name($clone_if);
2653
				if ( !empty($friendly_if)
2654
				    && $config['interfaces'][$friendly_if]['wireless']['mode'] == "bss"
2655
				    && isset($config['interfaces'][$friendly_if]['wireless']['wpa']['enable']) ) {
2656
					mwexec("/bin/sh {$g['tmp_path']}/{$clone_if}_setup.sh");
2657
				}
2658
			}
2659 bbfc810e Erik Fonnesbeck
		}
2660
	}
2661
2662 23fdc06e Erik Fonnesbeck
	/* The mode must be specified in a separate command before ifconfig
2663
	 * will allow the mode and channel at the same time in the next. */
2664 9be20928 Erik Fonnesbeck
	mwexec("/sbin/ifconfig {$if} mode " . escapeshellarg($standard));
2665 23fdc06e Erik Fonnesbeck
2666 2a48a885 Erik Fonnesbeck
	/* configure wireless */
2667
	$wlcmd_args = implode(" ", $wlcmd);
2668
	mwexec("/sbin/ifconfig {$if} $wlcmd_args", false);
2669
2670 2a203afd Seth Mos
	
2671
	sleep(1);
2672
	/* execute hostapd and wpa_supplicant if required in shell */
2673 6955830f Ermal Lu?i
	mwexec("/bin/sh {$g['tmp_path']}/{$if}_setup.sh");
2674 191a8175 Scott Ullrich
2675 ac3f8318 Espen Johansen
	return 0;
2676 cfc707f7 Scott Ullrich
2677 5b237745 Scott Ullrich
}
2678
2679 eba938e3 Scott Ullrich
function kill_hostapd($interface) {
2680 6f76920c thompsa
	return "/bin/pkill -f \"hostapd .*{$interface}\"\n";
2681 4b2a6180 Scott Ullrich
}
2682
2683 eba938e3 Scott Ullrich
function kill_wpasupplicant($interface) {
2684 6f76920c thompsa
	return "/bin/pkill -f \"wpa_supplicant .*{$interface}\"\n";
2685 4b2a6180 Scott Ullrich
}
2686
2687 eba938e3 Scott Ullrich
function find_dhclient_process($interface) {
2688 319cbd5e Ermal
	if ($interface)
2689 05c4bfa0 Ermal
		$pid = `/bin/pgrep -axf "dhclient: {$interface}"`;
2690 319cbd5e Ermal
	else
2691
		$pid = 0;
2692
2693 bcfe4ae5 Ermal
	return intval($pid);
2694 0311dbd5 Scott Ullrich
}
2695
2696 c495f88b Seth Mos
function find_dhcp6c_process($interface) {
2697
	if ($interface)
2698 74fa57aa smos
		$pid = `/bin/ps auxw|grep "[d]hcp6c" |grep "{$interface}"|awk '{print $2}'`;
2699 c495f88b Seth Mos
	else
2700 74fa57aa smos
		return(false);
2701 c495f88b Seth Mos
2702
	return intval($pid);
2703
}
2704
2705 7413cbfd Ermal
function interface_configure($interface = "wan", $reloadall = false, $linkupevent = false) {
2706 675aac3d Ermal Luçi
	global $config, $g;
2707 31b24870 Ermal Luçi
	global $interface_sn_arr_cache, $interface_ip_arr_cache;
2708 3502b5b1 Seth Mos
	global $interface_snv6_arr_cache, $interface_ipv6_arr_cache;
2709 cfc707f7 Scott Ullrich
2710 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
2711
2712 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
2713 20cb9803 gnhb
	$realhwif_array = get_parent_interface($interface);
2714
	// Need code to handle MLPPP if we ever use $realhwif for MLPPP handling
2715
	$realhwif = $realhwif_array[0];
2716 cfc707f7 Scott Ullrich
2717 5a3031ea smos
	/* Disable Accepting router advertisements unless specifically requested */
2718
	log_error("Deny router advertisements for interface {$interface}");
2719
	mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " inet6 -accept_rtadv");
2720 65531b4b Ermal
			
2721
	if (!$g['booting'] && !substr($realif, 0, 4) == "ovpn") {
2722 3c5e10fc Seth Mos
		/* remove all IPv4 and IPv6 addresses */
2723 332683cb Seth Mos
		while (mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " -alias", true) == 0);
2724 3502b5b1 Seth Mos
		while (mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " inet6 -alias", true) == 0);
2725 3896d93e Erik Fonnesbeck
2726 8103bd1e Seth Mos
		/* only bring down the interface when both v4 and v6 are set to NONE */
2727
		if(($wancfg['ipaddr'] <> "none") && ($wancfg['ipaddrv6'] <> "none")) {
2728 a687f866 Namezero
2729
2730
2731
2732
2733
2734
2735
2736 8103bd1e Seth Mos
			interface_bring_down($interface);
2737 a687f866 Namezero
2738 3896d93e Erik Fonnesbeck
		}
2739 28d22199 Scott Ullrich
	}
2740 acc1e9d0 Scott Ullrich
2741 0a28d385 Erik Fonnesbeck
	/* wireless configuration? */
2742 5b237745 Scott Ullrich
	if (is_array($wancfg['wireless']))
2743 0a28d385 Erik Fonnesbeck
		interface_wireless_configure($realif, $wancfg, $wancfg['wireless']);
2744 cfc707f7 Scott Ullrich
2745 49db607f jim-p
	$mac = get_interface_mac($realhwif);
2746
	/*	Don't try to reapply the spoofed MAC if it's already applied.
2747
		When ifconfig link is used, it cycles the interface down/up, which triggers 
2748
		the interface config again, which attempts to spoof the MAC again, 
2749
		which cycles the link again... */
2750
	if ($wancfg['spoofmac'] && ($wancfg['spoofmac'] != $mac)) {
2751 3e5d0d1d Ermal
		mwexec("/sbin/ifconfig " . escapeshellarg($realhwif) .
2752 5b237745 Scott Ullrich
			" link " . escapeshellarg($wancfg['spoofmac']));
2753 ac8ff0a4 Ermal
2754 871768cf Ermal
                /*
2755
                 * All vlans need to spoof their parent mac address, too.  see
2756
                 * ticket #1514: http://cvstrac.pfsense.com/tktview?tn=1514,33
2757
                 */
2758
                if (is_array($config['vlans']['vlan'])) {
2759
                        foreach ($config['vlans']['vlan'] as $vlan) {
2760 3e5d0d1d Ermal
                                if ($vlan['if'] == $realhwif)
2761 871768cf Ermal
                                        mwexec("/sbin/ifconfig " . escapeshellarg($vlan['vlanif']) .
2762
                                                " link " . escapeshellarg($wancfg['spoofmac']));
2763
                        }
2764
                }
2765 f36d4bd2 Scott Ullrich
	}  else {
2766 a687f866 Namezero
2767 3e5d0d1d Ermal
		if ($mac == "ff:ff:ff:ff:ff:ff") {
2768 f36d4bd2 Scott Ullrich
			/*   this is not a valid mac address.  generate a
2769
			 *   temporary mac address so the machine can get online.
2770
			 */
2771 07e40c1f Carlos Eduardo Ramos
			echo gettext("Generating new MAC address.");
2772 f36d4bd2 Scott Ullrich
			$random_mac = generate_random_mac_address();
2773 3e5d0d1d Ermal
			mwexec("/sbin/ifconfig " . escapeshellarg($realhwif) .
2774 f36d4bd2 Scott Ullrich
				" link " . escapeshellarg($random_mac));
2775
			$wancfg['spoofmac'] = $random_mac;
2776
			write_config();
2777 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");
2778 f36d4bd2 Scott Ullrich
		}
2779
	}
2780 cfc707f7 Scott Ullrich
2781 5b237745 Scott Ullrich
	/* media */
2782
	if ($wancfg['media'] || $wancfg['mediaopt']) {
2783 3e5d0d1d Ermal
		$cmd = "/sbin/ifconfig " . escapeshellarg($realhwif);
2784 5b237745 Scott Ullrich
		if ($wancfg['media'])
2785
			$cmd .= " media " . escapeshellarg($wancfg['media']);
2786
		if ($wancfg['mediaopt'])
2787
			$cmd .= " mediaopt " . escapeshellarg($wancfg['mediaopt']);
2788
		mwexec($cmd);
2789
	}
2790 e57a441e Ermal Lu?i
	if (!empty($wancfg['mtu']))
2791 3e5d0d1d Ermal
		pfSense_interface_mtu($realhwif, $wancfg['mtu']);
2792 56da23dc Ermal
	else {
2793
		$mtu = get_interface_default_mtu(remove_numbers($realhwif));
2794
		if ($mtu != get_interface_mtu($realhwif))
2795 2b094d21 jim-p
			pfSense_interface_mtu($realhwif, $mtu);
2796 56da23dc Ermal
	}
2797 cfc707f7 Scott Ullrich
2798 3e5d0d1d Ermal
	$options = pfSense_get_interface_addresses($realhwif);
2799 9a4c3eed Ermal
	if (is_array($options) && isset($options['caps']['polling'])) {
2800
		if (isset($config['system']['polling']))
2801
			pfSense_interface_capabilities($realif, IFCAP_POLLING);
2802
		else
2803
			pfSense_interface_capabilities($realif, -IFCAP_POLLING);
2804
	}
2805
2806 51d5aad7 Ermal
	/* skip vlans for checksumming and polling */
2807 3e5d0d1d Ermal
        if (!stristr($realhwif, "vlan") && is_array($options)) {
2808 51d5aad7 Ermal
		$flags = 0;
2809
		if(isset($config['system']['disablechecksumoffloading'])) {
2810
			if (isset($options['encaps']['txcsum']))
2811
				$flags |= IFCAP_TXCSUM;
2812
			if (isset($options['encaps']['rxcsum']))
2813
				$flags |= IFCAP_RXCSUM;
2814
        	} else {
2815
 			if (!isset($options['caps']['txcsum']))
2816
				$flags |= IFCAP_TXCSUM;
2817
			if (!isset($options['caps']['rxcsum']))
2818
				$flags |= IFCAP_RXCSUM;
2819
        	}
2820
2821
        	if(isset($config['system']['disablesegmentationoffloading'])) {
2822
                	if (isset($options['encaps']['tso4']))
2823
				$flags |= IFCAP_TSO;
2824
                	if (isset($options['encaps']['tso6']))
2825
				$flags |= IFCAP_TSO;
2826
        	} else {
2827
                	if (!isset($options['caps']['tso4']))
2828
				$flags |= IFCAP_TSO;
2829
                	if (!isset($options['caps']['tso6']))
2830
				$flags |= IFCAP_TSO;
2831
        	}
2832
2833
        	if(isset($config['system']['disablelargereceiveoffloading'])) {
2834
                	if (isset($options['encaps']['lro']))
2835
				$flags |= IFCAP_LRO;
2836
        	} else {
2837
                	if (!isset($options['caps']['lro']))
2838
				$flags |= IFCAP_LRO;
2839
        	}
2840
2841
        	/* if the NIC supports polling *AND* it is enabled in the GUI */
2842
        	if (!isset($config['system']['polling']) || !isset($options['caps']['polling'])) {
2843
			$flags |= IFCAP_POLLING;
2844
		}
2845 3e5d0d1d Ermal
               	pfSense_interface_capabilities($realhwif, -$flags);
2846 51d5aad7 Ermal
	}
2847
2848 31b24870 Ermal Luçi
	/* invalidate interface/ip/sn cache */
2849 eba938e3 Scott Ullrich
	get_interface_arr(true);
2850 31b24870 Ermal Luçi
	unset($interface_ip_arr_cache[$realif]);
2851
	unset($interface_sn_arr_cache[$realif]);
2852 5a5413bb Seth Mos
	unset($interface_ipv6_arr_cache[$realif]);
2853
	unset($interface_snv6_arr_cache[$realif]);
2854 ccbd2447 Ermal Luçi
2855 5b237745 Scott Ullrich
	switch ($wancfg['ipaddr']) {
2856 d5d00b83 Scott Ullrich
		case 'carpdev-dhcp':
2857 1fb7c265 Ermal Luçi
			interface_carpdev_dhcp_configure($interface);
2858 d5d00b83 Scott Ullrich
			break;
2859 5b237745 Scott Ullrich
		case 'dhcp':
2860 1fb7c265 Ermal Luçi
			interface_dhcp_configure($interface);
2861 5b237745 Scott Ullrich
			break;
2862
		case 'pppoe':
2863 8af6c46d gnhb
		case 'l2tp':
2864 5b237745 Scott Ullrich
		case 'pptp':
2865 9ebe7028 gnhb
		case 'ppp':
2866 64d124c5 gnhb
			interface_ppps_configure($interface);
2867 9ebe7028 gnhb
			break;
2868 5b237745 Scott Ullrich
		default:
2869 8103bd1e Seth Mos
			if (is_ipaddr($wancfg['ipaddr']) && $wancfg['subnet'] <> "") {
2870 871768cf Ermal
				pfSense_interface_setaddress($realif, "{$wancfg['ipaddr']}/{$wancfg['subnet']}");
2871 d1eea523 Ermal
			} else if (substr($realif, 0, 3) == "gre") {
2872
				if (is_array($config['gres']['gre'])) {
2873
					foreach ($config['gres']['gre'] as $gre)
2874
						if ($gre['greif'] == $realif)
2875
							interface_gre_configure($gre);
2876
				}
2877
			} else if (substr($realif, 0, 3) == "gif") {
2878
				 if (is_array($config['gifs']['gif'])) {
2879
					foreach ($config['gifs']['gif'] as $gif)
2880 d1ae9705 Ermal
						if($gif['gifif'] == $realif)
2881 d1eea523 Ermal
							interface_gif_configure($gif);
2882
				}
2883
			} else if (substr($realif, 0, 4) == "ovpn") {
2884
				/* XXX: Should be done anything?! */
2885 acc1e9d0 Scott Ullrich
			}
2886 d1eea523 Ermal
			break;
2887 5b237745 Scott Ullrich
	}
2888 ffeb5acf Scott Ullrich
2889 5a5413bb Seth Mos
	switch ($wancfg['ipaddrv6']) {
2890 feb88a14 smos
		case 'slaac':
2891 8103bd1e Seth Mos
		case 'dhcp6':
2892
			interface_dhcpv6_configure($interface);
2893
			break;
2894 3f383504 smos
		case '6rd':
2895
			interface_6rd_configure($interface);
2896
			break;
2897 31c43fd3 smos
		case '6to4':
2898
			interface_6to4_configure($interface);
2899
			break;
2900 20a7cb15 smos
		case 'track6':
2901
			interface_track6_configure($interface);
2902
			break;
2903 5a5413bb Seth Mos
		default:
2904 8103bd1e Seth Mos
			if (is_ipaddr($wancfg['ipaddrv6']) && $wancfg['subnetv6'] <> "") {
2905 5a5413bb Seth Mos
				pfSense_interface_setaddress($realif, "{$wancfg['ipaddrv6']}/{$wancfg['subnetv6']}");
2906 3c5e10fc Seth Mos
				// FIXME: Add IPv6 Support to the pfSense module
2907 5a5413bb Seth Mos
				mwexec("/sbin/ifconfig {$realif} inet6 {$wancfg['ipaddrv6']} prefixlen {$wancfg['subnetv6']} ");
2908
			}
2909
			break;
2910
	}
2911
2912 435f11c8 Ermal Lu?i
	if(does_interface_exist($wancfg['if']))
2913 7284d850 Scott Ullrich
		interfaces_bring_up($wancfg['if']);
2914 67b057a9 Ermal
2915
	interface_netgraph_needed($interface);
2916 3d8237f4 sullrich
 	
2917 5b237745 Scott Ullrich
	if (!$g['booting']) {
2918 dcadda55 Ermal
		link_interface_to_vips($interface, "update");
2919 6991dcb1 Ermal
2920 a639bb91 Ermal
		unset($gre);
2921
		$gre = link_interface_to_gre($interface);
2922
		if (!empty($gre))
2923 ed62880b Ermal
			array_walk($gre, 'interface_gre_configure');
2924 a639bb91 Ermal
2925
		unset($gif);
2926
		$gif = link_interface_to_gif($interface);
2927
		if (!empty($gif))
2928 8103bd1e Seth Mos
			array_walk($gif, 'interface_gif_configure');
2929 a639bb91 Ermal
2930 bf17eb72 Ermal
		if ($linkupevent == false || substr($realif, 0, 4) == "ovpn") {
2931 7413cbfd Ermal
			unset($bridgetmp);
2932
			$bridgetmp = link_interface_to_bridge($interface);
2933
			if (!empty($bridgetmp))
2934
				interface_bridge_add_member($bridgetmp, $realif);
2935
		}
2936 ccbd2447 Ermal Luçi
2937 48f23632 Ermal
		$grouptmp = link_interface_to_group($interface);
2938
		if (!empty($grouptmp))
2939 ed62880b Ermal
			array_walk($grouptmp, 'interface_group_add_member');
2940 48f23632 Ermal
2941 a5d6f60b Ermal Lu?i
		if ($interface == "lan")
2942 4476d447 Ermal Luçi
			/* make new hosts file */
2943 ffeb5acf Scott Ullrich
			system_hosts_generate();
2944 4476d447 Ermal Luçi
2945 a5d6f60b Ermal Lu?i
		if ($reloadall == true) {
2946 cfc707f7 Scott Ullrich
2947 a5d6f60b Ermal Lu?i
			/* reconfigure static routes (kernel may have deleted them) */
2948 1ea67f2e Ermal
			system_routing_configure($interface);
2949 cfc707f7 Scott Ullrich
2950 a5d6f60b Ermal Lu?i
			/* reload ipsec tunnels */
2951
			vpn_ipsec_configure();
2952 cfc707f7 Scott Ullrich
2953 b5eeef07 Ermal
			/* restart dnsmasq */
2954
			services_dnsmasq_configure();
2955
2956 a5d6f60b Ermal Lu?i
			/* update dyndns */
2957 422bc2a7 Ermal
			send_event("service reload dyndns {$interface}");
2958 a23d7248 Scott Ullrich
2959 a5d6f60b Ermal Lu?i
			/* reload captive portal */
2960 769e254e Ermal
			captiveportal_init_rules();
2961 a5d6f60b Ermal Lu?i
		}
2962 5b237745 Scott Ullrich
	}
2963 cfc707f7 Scott Ullrich
2964 5b237745 Scott Ullrich
	return 0;
2965
}
2966
2967 eba938e3 Scott Ullrich
function interface_carpdev_dhcp_configure($interface = "wan") {
2968 d5d00b83 Scott Ullrich
	global $config, $g;
2969
2970 67ee1ec5 Ermal Luçi
	$wancfg = $config['interfaces'][$interface];
2971 499994ff Scott Ullrich
	$wanif = $wancfg['if'];
2972 d5d00b83 Scott Ullrich
	/* bring wan interface up before starting dhclient */
2973 d7147b1c Scott Ullrich
	if($wanif)
2974 b5b957fe Scott Ullrich
		interfaces_bring_up($wanif);
2975 d7147b1c Scott Ullrich
	else 
2976 07e40c1f Carlos Eduardo Ramos
		log_error(gettext("Could not bring wanif up in terface_carpdev_dhcp_configure()"));
2977 d5d00b83 Scott Ullrich
2978
	return 0;
2979
}
2980
2981 20a7cb15 smos
function interface_track6_configure($interface = "lan") {
2982
	global $config, $g;
2983
	$iflist = get_configured_interface_with_descr(false, true);
2984
2985
	$wancfg = $config['interfaces'][$interface];
2986
	$wanif = $wancfg['if'];
2987
	if (empty($wancfg))
2988
		$wancfg = array();
2989
2990
	$wanif = get_real_interface($interface);
2991
	
2992
	/* If the interface is not configured via another, exit */
2993
	if(!$wancfg['track6-interface'] <> "") {
2994
		return(false);
2995
	}
2996
2997 2d5ca06e smos
	$type = $config['interfaces'][$wancfg['track6-interface']]['ipaddrv6'];
2998
	switch($type) {
2999 20a7cb15 smos
		case "6to4":
3000 2d5ca06e smos
			log_error("Interface {$interface} configured via {$wancfg['track6-interface']}  type {$type}");
3001 20a7cb15 smos
			interface_track6_6to4_configure($interface);
3002
			break;
3003
		case "6rd":
3004 2d5ca06e smos
			log_error("Interface {$interface} configured via {$wancfg['track6-interface']}  type {$type}");
3005 20a7cb15 smos
			interface_track6_6rd_configure($interface);
3006
			break;
3007
		case "dhcp6":
3008 2d5ca06e smos
			log_error("Interface {$interface} configured via {$wancfg['track6-interface']}  type {$type}");
3009 20a7cb15 smos
			interface_track6_dhcp6_configure($interface);
3010
			break;
3011
		default:
3012 2d5ca06e smos
			log_error("Interface {$interface} configured via {$wancfg['track6-interface']} but unknown type {$type}");
3013 20a7cb15 smos
			break;
3014
	}
3015
	return 0;
3016
}
3017
3018
3019
function interface_track6_6rd_configure($interface = "lan") {
3020
	global $config, $g;
3021
	$iflist = get_configured_interface_with_descr(false, true);
3022
3023
	$lancfg = $config['interfaces'][$interface];
3024
	$lanif = $lancfg['if'];
3025
	if (empty($lancfg))
3026
		$lancfg = array();
3027
3028
	$lanif = get_real_interface($interface);
3029
	
3030
	/* If the interface is not configured via another, exit */
3031
	if(!$lancfg['track6-interface'] <> "") {
3032
		return(false);
3033
	}
3034
	if(!is_numeric($lancfg['track6-prefix-id']))
3035
		$lancfg['track6-prefix-id'] = 0;
3036
	
3037
	$wancfg = $config['interfaces'][$lancfg['track6-interface']];
3038
	$wanif = $wancfg['if'];
3039
	if (empty($wancfg))
3040
		$wancfg = array();
3041
	
3042
	$ip4address = find_interface_ip($wanif);
3043
	if((!is_ipaddrv4($ip4address)) || (is_private_ip($ip4address))) {
3044
		log_error("The interface IPv4 '{$ip4address}' address on interface '{$wanif}' is not public, not configuring 6RD tunnel");
3045
		return false;
3046
	}
3047
	$hexwanv4 = return_hex_ipv4($ip4address);
3048
	
3049
	/* create the long prefix notation for math, save the prefix length */
3050
	$rd6prefix = explode("/", $wancfg['prefix-6rd']);
3051
	$rd6prefixlen = $rd6prefix[1];
3052
	$rd6prefix = Net_IPv6::uncompress($rd6prefix[0]);
3053
3054
	/* binary presentation of the prefix for all 128 bits. */
3055
	$rd6lanbin = convert_ipv6_to_128bit($rd6prefix);
3056
	
3057
	/* just save the left prefix length bits */
3058
	$rd6lanbin = substr($rd6lanbin, 0, $rd6prefixlen);
3059
	/* add the v4 address, offset n bits from the left */
3060
	$rd6lanbin .= substr(sprintf("%032b", hexdec($hexwanv4)), (0 + $wancfg['prefix-6rd-v4plen']), 32);
3061
3062
	/* add the custom prefix id, max 32bits long? (64 bits - (prefixlen + (32 - v4plen)) */
3063
	/* 64 - (37 + (32 - 17)) = 8 == /52 */
3064
	$restbits = 64 - ($rd6prefixlen + (32 - $wancfg['prefix-6rd-v4plen']));
3065
	// echo "64 - (prefixlen {$rd6prefixlen} + v4len (32 - {$wancfg['prefix-6rd-v4plen']})) = {$restbits} \n";
3066
	$rd6lanbin .= substr(sprintf("%032b", str_pad($lancfg['track6-prefix-id'], 32, "0", STR_PAD_LEFT)), (32 - $restbits), 32);
3067
	/* fill the rest out with zeros */
3068
	$rd6lanbin = str_pad($rd6lanbin, 128, "0", STR_PAD_RIGHT);;
3069
3070
	/* convert the 128 bits for the lan address back into a valid IPv6 address */ 
3071
	$rd6lan = convert_128bit_to_ipv6($rd6lanbin) ."1";
3072
	
3073
	log_error("rd6 {$interface} with ipv6 address {$rd6lan} based on {$lancfg['track6-interface']} ipv4 {$ip4address}");
3074
	mwexec("/sbin/ifconfig {$lanif} inet6 {$rd6lan} prefixlen 64");
3075
	return 0;
3076
}
3077
3078
function interface_track6_6to4_configure($interface = "lan") {
3079
	global $config, $g;
3080
	$iflist = get_configured_interface_with_descr(false, true);
3081
3082
	$lancfg = $config['interfaces'][$interface];
3083
	$lanif = $lancfg['if'];
3084
	if (empty($lancfg))
3085
		$lancfg = array();
3086
3087
	$lanif = get_real_interface($interface);
3088
	
3089
	/* If the interface is not configured via another, exit */
3090
	if(!$lancfg['track6-interface'] <> "") {
3091
		return(false);
3092
	}
3093
	if(!is_numeric($lancfg['track6-prefix-id']))
3094
		$lancfg['track6-prefix-id'] = 0;
3095
	
3096
	$wancfg = $config['interfaces'][$lancfg['track6-interface']];
3097
	$wanif = $wancfg['if'];
3098
	if (empty($wancfg))
3099
		$wancfg = array();
3100
	
3101
	$ip4address = find_interface_ip($wanif);
3102
	if((!is_ipaddrv4($ip4address)) || (is_private_ip($ip4address))) {
3103
		log_error("The interface IPv4 '{$ip4address}' address on interface '{$wanif}' is not public, not configuring 6RD tunnel");
3104
		return false;
3105
	}
3106
	$hexwanv4 = return_hex_ipv4($ip4address);
3107
	
3108
	/* create the long prefix notation for math, save the prefix length */
3109
	$sixto4prefix = "2002::";
3110
	$sixto4prefixlen = 16;
3111
	$sixto4prefix = Net_IPv6::uncompress($sixto4prefix);
3112
3113
	/* binary presentation of the prefix for all 128 bits. */
3114
	$sixto4lanbin = convert_ipv6_to_128bit($sixto4prefix);
3115
	
3116
	/* just save the left prefix length bits */
3117
	$sixto4lanbin = substr($sixto4lanbin, 0, $sixto4prefixlen);
3118
	/* add the v4 address */
3119
	$sixto4lanbin .= sprintf("%032b", hexdec($hexwanv4));
3120
	/* add the custom prefix id */
3121
	$sixto4lanbin .= sprintf("%016b", $lancfg['track6-prefix-id']);
3122
	/* fill the rest out with zeros */
3123
	$sixto4lanbin = str_pad($sixto4lanbin, 128, "0", STR_PAD_RIGHT);;
3124
	
3125
	/* convert the 128 bits for the lan address back into a valid IPv6 address */ 
3126
	$sixto4lan = convert_128bit_to_ipv6($sixto4lanbin) ."1";
3127
	
3128
	log_error("sixto4 {$interface} with ipv6 address {$sixto4lan} based on {$lancfg['track6-interface']} ipv4 {$ip4address}");
3129
	mwexec("/sbin/ifconfig {$lanif} inet6 {$sixto4lan} prefixlen 64");
3130
	return 0;
3131
}
3132
3133 239e817a smos
function interface_track6_dhcp6_configure($interface = "lan") {
3134
	global $config, $g;
3135
	$iflist = get_configured_interface_with_descr(false, true);
3136
3137
	$lancfg = $config['interfaces'][$interface];
3138
	$lanif = $lancfg['if'];
3139
	if (empty($lancfg))
3140
		$lancfg = array();
3141
3142
	$lanif = get_real_interface($interface);
3143
	
3144
	/* If the interface is not configured via another, exit */
3145
	if(!$lancfg['track6-interface'] <> "") {
3146
		return(false);
3147
	}
3148
	if(!is_numeric($lancfg['track6-prefix-id']))
3149
		$lancfg['track6-prefix-id'] = 0;
3150
	
3151
	$wancfg = $config['interfaces'][$lancfg['track6-interface']];
3152
	$wanif = $wancfg['if'];
3153
	if (empty($wancfg))
3154
		$wancfg = array();
3155
	
3156
	
3157
	$ifcfgipv6 = find_interface_ipv6($lanif);
3158
	if(is_ipaddrv6($ifcfgipv6)) {
3159
		$dhcp6lanarr = explode(":", Net_IPv6::uncompress($ifcfgipv6));
3160 73778c3f smos
		$dhcp6lanarr[4] = 0;
3161
		$dhcp6lanarr[5] = 0;
3162
		$dhcp6lanarr[6] = 0;
3163 239e817a smos
		$dhcp6lanarr[7] = 1;
3164 73778c3f smos
		$dhcp6lan =  Net_IPv6::compress(implode(":", $dhcp6lanarr));
3165 239e817a smos
		log_error("dhcp6 {$interface} with ipv6 address {$dhcp6lan} based on {$lancfg['track6-interface']}");
3166
		mwexec("/sbin/ifconfig {$lanif} inet6 {$dhcp6lan} prefixlen 64");
3167
	}
3168
	return 0;
3169
}
3170
3171 20a7cb15 smos
function interface_6rd_configure($interface = "wan") {
3172 668e8961 smos
	global $config, $g;
3173
	$iflist = get_configured_interface_with_descr(false, true);
3174
3175
	/* because this is a tunnel interface we can only function 
3176
	 *	with a public IPv4 address on the interface */
3177
3178
	$wancfg = $config['interfaces'][$interface];
3179
	$wanif = $wancfg['if'];
3180
	if (empty($wancfg))
3181
		$wancfg = array();
3182
3183
	$wanif = get_real_interface($interface);
3184
	
3185
	$ip4address = find_interface_ip($wanif);
3186 c8ed8142 smos
	$ip4gateway = get_interface_gateway($wanif);
3187 668e8961 smos
	if((!is_ipaddrv4($ip4address)) || (is_private_ip($ip4address))) {
3188
		log_error("The interface IPv4 '{$ip4address}' address on interface '{$wanif}' is not public, not configuring 6RD tunnel");
3189 1f78ab3a smos
		return false;
3190 668e8961 smos
	}
3191 20a7cb15 smos
	$hexwanv4 = return_hex_ipv4($ip4address);
3192 51c57aae smos
	
3193 3f383504 smos
	if(!is_numeric($wancfg['prefix-6rd-v4plen']))
3194 20a7cb15 smos
		$wancfg['prefix-6rd-v4plen'] = 0;
3195 668e8961 smos
3196 51c57aae smos
	/* create the long prefix notation for math, save the prefix length */
3197 f87ccbed smos
	$rd6prefix = explode("/", $wancfg['prefix-6rd']);
3198
	$rd6prefixlen = $rd6prefix[1];
3199
	$rd6prefix = Net_IPv6::uncompress($rd6prefix[0]);
3200 51c57aae smos
3201
	/* we need the hex form of the broker IPv4 address */
3202 20a7cb15 smos
	$hexbrv4 = return_hex_ipv4($wancfg['gateway-6rd']);
3203 51c57aae smos
	
3204
	/* binary presentation of the prefix for all 128 bits. */
3205 20a7cb15 smos
	$rd6prefixbin = convert_ipv6_to_128bit($rd6prefix);
3206 4aa569bd smos
		
3207
	/* just save the left prefix length bits */
3208
	$rd6brprefixbin = substr($rd6prefixbin, 0, $rd6prefixlen);
3209
	/* if the prefix length is not 32 bits we need to shave bits off from the left of the v4 address. */
3210
	$rd6brprefixbin .= substr(sprintf("%032b", hexdec($hexbrv4)), $wancfg['prefix-6rd-v4plen'], 32);
3211
	/* fill out the rest with 0's */
3212
	$rd6brprefixbin = str_pad($rd6brprefixbin, 128, "0", STR_PAD_RIGHT);;
3213
3214
	/* convert the 128 bits for the broker address back into a valid IPv6 address */ 
3215
	$rd6brgw = convert_128bit_to_ipv6($rd6brprefixbin);
3216
3217 51c57aae smos
	/* just save the left prefix length bits */
3218 20a7cb15 smos
	$rd6prefixbin = substr($rd6prefixbin, 0, $rd6prefixlen);
3219 51c57aae smos
	/* if the prefix length is not 32 bits we need to shave bits off from the left of the v4 address. */
3220 4aa569bd smos
	$rd6prefixbin .= substr(sprintf("%032b", hexdec($hexwanv4)), $wancfg['prefix-6rd-v4plen'], 32);
3221 20a7cb15 smos
	/* fill out the rest with 0's */
3222
	$rd6prefixbin = str_pad($rd6prefixbin, 128, "0", STR_PAD_RIGHT);;
3223 51c57aae smos
3224
	/* convert the 128 bits for the broker address back into a valid IPv6 address */ 
3225 4aa569bd smos
	$rd6prefix = convert_128bit_to_ipv6($rd6prefixbin);
3226 f87ccbed smos
3227 668e8961 smos
	/* setup the stf interface */
3228 4aa569bd smos
	/* use the srd device
3229 3f383504 smos
	mwexec("/sbin/ifconfig srd0 destroy");
3230
	mwexec("/sbin/ifconfig srd0 create");
3231 20a7cb15 smos
	mwexec("/sbin/ifconfig srd0 v4plen {$wancfg['prefix-6rd-v4plen']} pfix {$rd6prefix} plen {$rd6prefixlen} braddr {$wancfg['gateway-6rd']}");
3232
	log_error("Created 6rd interface srd0 v4plen {$wancfg['prefix-6rd-v4plen']} pfix {$rd6prefix} plen {$rd6prefixlen} braddr {$wancfg['gateway-6rd']}");
3233 4aa569bd smos
	*/
3234
	/* stf approach only works when embedding the entire 32 bits of the v4 */
3235
	mwexec("/sbin/ifconfig stf0 destroy");
3236
	mwexec("/sbin/ifconfig stf0 create");
3237
	mwexec("/sbin/ifconfig stf0 inet6 {$rd6prefix}/{$rd6prefixlen}");
3238
	log_error("Created 6rd interface stf0 {$rd6prefix}/{$rd6prefixlen}");
3239 668e8961 smos
3240 f55b6cbb smos
	/* write out a default router file */
3241 20a7cb15 smos
	file_put_contents("{$g['tmp_path']}/{$wanif}_routerv6", "{$rd6brgw}\n");
3242
	file_put_contents("{$g['tmp_path']}/{$wanif}_defaultgwv6", "{$rd6brgw}\n");
3243 2d5ca06e smos
3244 c8ed8142 smos
	if (is_ipaddrv4($ip4gateway)) {
3245
		mwexec("route change -host " . $wancfg['gateway-6rd'] . " {$ip4gateway}");
3246
	}
3247
3248 2d5ca06e smos
	/* configure dependent interfaces */
3249
	foreach($iflist as $if => $ifname) {
3250
		if($config['interfaces'][$if]['track6-interface'] == $interface)
3251
			interface_track6_configure($if);
3252
	}
3253 f55b6cbb smos
	
3254
	return 0;
3255 668e8961 smos
}
3256
3257 31c43fd3 smos
function interface_6to4_configure($interface = "wan"){
3258
	global $config, $g;
3259
	$iflist = get_configured_interface_with_descr(false, true);
3260
3261
	/* because this is a tunnel interface we can only function 
3262
	 *	with a public IPv4 address on the interface */
3263
3264
	$wancfg = $config['interfaces'][$interface];
3265
	$wanif = $wancfg['if'];
3266
	if (empty($wancfg))
3267
		$wancfg = array();
3268
3269
	$wanif = get_real_interface($interface);
3270
	
3271
	$ip4address = find_interface_ip($wanif);
3272 c8ed8142 smos
	$ip4gateway = get_interface_gateway($wanif);
3273 31c43fd3 smos
	if((!is_ipaddrv4($ip4address)) || (is_private_ip($ip4address))) {
3274
		log_error("The interface IPv4 '{$ip4address}' address on interface '{$wanif}' is not public, not configuring 6RD tunnel");
3275
		return false;
3276
	}
3277
	
3278
	/* create the long prefix notation for math, save the prefix length */
3279
	$stfprefixlen = 16;
3280
	$stfprefix = Net_IPv6::uncompress("2002::");
3281
	$stfarr = explode(":", $stfprefix);
3282
	$v4prefixlen = "0";
3283
	
3284
	/* we need the hex form of the interface IPv4 address */
3285
	$ip4arr = explode(".", $ip4address);
3286
	$hexwanv4 = "";
3287
	foreach($ip4arr as $octet)
3288
		$hexwanv4 .= sprintf("%02x", $octet);
3289
3290
	/* we need the hex form of the broker IPv4 address */
3291
	$ip4arr = explode(".", "192.88.99.1");
3292
	$hexbrv4 = "";
3293
	foreach($ip4arr as $octet)
3294
		$hexbrv4 .= sprintf("%02x", $octet);
3295
	
3296
	/* binary presentation of the prefix for all 128 bits. */
3297
	$stfprefixbin = "";
3298
	foreach($stfarr as $element) {
3299
		$stfprefixbin .= sprintf("%016b", hexdec($element));
3300
	}
3301
	/* just save the left prefix length bits */
3302
	$stfprefixstartbin = substr($stfprefixbin, 0, $stfprefixlen);
3303
3304
	/* if the prefix length is not 32 bits we need to shave bits off from the left of the v4 address. */
3305
	$stfbrokerbin = substr(sprintf("%032b", hexdec($hexbrv4)), $v4prefixlen, 32);
3306
	$stfbrokerbin = str_pad($stfprefixstartbin . $stfbrokerbin, 128, "0", STR_PAD_RIGHT);;
3307
3308
	/* for the local subnet too. */
3309
	$stflanbin = substr(sprintf("%032b", hexdec($hexwanv4)), $v4prefixlen, 32);
3310
	$stflanbin = str_pad($stfprefixstartbin . $stflanbin, 128, "0", STR_PAD_RIGHT);;
3311
3312
	/* convert the 128 bits for the broker address back into a valid IPv6 address */ 
3313
	$stfbrarr = array();
3314
	$stfbrbinarr = array();
3315
	$stfbrbinarr = str_split($stfbrokerbin, 16);
3316
	foreach($stfbrbinarr as $bin)
3317
		$stfbrarr[] = dechex(bindec($bin));
3318
	$stfbrgw = Net_IPv6::compress(implode(":", $stfbrarr));
3319
3320
	/* convert the 128 bits for the broker address back into a valid IPv6 address */ 
3321
	$stflanarr = array();
3322
	$stflanbinarr = array();
3323
	$stflanbinarr = str_split($stflanbin, 16);
3324
	foreach($stflanbinarr as $bin)
3325
		$stflanarr[] = dechex(bindec($bin));
3326
	$stflanpr = Net_IPv6::compress(implode(":", $stflanarr));
3327
	$stflanarr[7] = 1;
3328
	$stflan = Net_IPv6::compress(implode(":", $stflanarr));
3329
3330
	/* setup the stf interface */
3331
	mwexec("/sbin/ifconfig stf0 destroy");
3332
	mwexec("/sbin/ifconfig stf0 create");
3333
	mwexec("/sbin/ifconfig stf0 inet6 {$stflanpr} prefixlen 16");
3334
3335
	log_error("Set IPv6 address inet6 {$stflanpr} prefixlen 16 for stf0, route {$stfbrgw}");
3336
	
3337
	/* write out a default router file */
3338
	file_put_contents("{$g['tmp_path']}/{$wanif}_routerv6", "{$stfbrgw}");
3339
	file_put_contents("{$g['tmp_path']}/{$wanif}_defaultgwv6", "{$stfbrgw}");
3340 2d5ca06e smos
3341 c8ed8142 smos
	if (is_ipaddrv4($ip4gateway)) {
3342
		mwexec("route change -host 192.88.99.1 {$ip4gateway}");
3343
	}
3344
3345 2d5ca06e smos
	/* configure dependent interfaces */
3346
	foreach($iflist as $if => $ifname) {
3347
		if($config['interfaces'][$if]['track6-interface'] == $interface)
3348
			interface_track6_configure($if);
3349
	}
3350 31c43fd3 smos
	
3351
	return 0;
3352
}
3353
3354 ed395640 Seth Mos
function interface_dhcpv6_configure($interface = "wan") {
3355
	global $config, $g;
3356
	$iflist = get_configured_interface_with_descr(false, true);
3357
3358
	$wancfg = $config['interfaces'][$interface];
3359
	$wanif = $wancfg['if'];
3360
	if (empty($wancfg))
3361
		$wancfg = array();
3362
3363
	$wanif = get_real_interface($interface);
3364
3365 8103bd1e Seth Mos
	/* Add ISC IPv6 dhclient here, only wide-dhcp6c works for now. */
3366 ed395640 Seth Mos
	$fd = fopen("{$g['varetc_path']}/dhcp6c_{$interface}.conf", "w");
3367
	if (!$fd) {
3368 8103bd1e Seth Mos
		printf("Error: cannot open dhcp6c_{$interface}.conf in interfaces_wan_dhcpv6_configure() for writing.\n");
3369 ed395640 Seth Mos
		return 1;
3370
	}
3371
3372
	$dhcp6cconf = "";
3373 feb88a14 smos
	$dhcp6cconf .= "interface {$wanif} {\n";
3374
3375
	/* for SLAAC interfaces we do fire off a dhcp6 client for just our name servers */
3376
	if($wancfg['ipaddrv6'] == "slaac") {
3377
		$dhcp6cconf .= "	information-only;\n";
3378
		$dhcp6cconf .= "	request domain-name-servers;\n";
3379
		$dhcp6cconf .= "	request domain-name;\n";
3380
		$dhcp6cconf .= "	script \"{$g['varetc_path']}/dhcp6c_{$interface}_script.sh\"; # we'd like some nameservers please\n";
3381
		$dhcp6cconf .= "};\n";
3382
	} else {
3383
	
3384
		$dhcp6cconf .= " 	send ia-na 0;	# request stateful address\n";
3385
		if(is_numeric($wancfg['dhcp6-ia-pd-len'])) {
3386
			$dhcp6cconf .= "	send ia-pd 0;	# request prefix delegation\n";
3387 ed395640 Seth Mos
		}
3388 feb88a14 smos
		$dhcp6cconf .= "request domain-name-servers;\n";
3389
		$dhcp6cconf .= "request domain-name;\n";
3390
		$dhcp6cconf .= "script \"{$g['varetc_path']}/dhcp6c_{$interface}_script.sh\"; # we'd like some nameservers please\n";
3391
3392 ed395640 Seth Mos
		$dhcp6cconf .= "};\n";
3393 feb88a14 smos
		$dhcp6cconf .= "id-assoc na 0 { };\n";
3394
		if(is_numeric($wancfg['dhcp6-ia-pd-len'])) {
3395
			/* Setup the prefix delegation */
3396
			$dhcp6cconf .= "id-assoc pd 0 {\n";
3397
			foreach($iflist as $friendly => $ifdescr) {
3398
				if($config['interfaces'][$friendly]['track6-interface'] != $interface)
3399
					continue;
3400
				if(is_numeric($config['interfaces'][$friendly]['track6-prefix-id'])) {
3401
					log_error("setting up $friendly - {$config['interfaces'][$friendly]['track6-prefix-id']}");
3402
					$realif = get_real_interface($friendly);
3403
					$dhcp6cconf .= "	prefix-interface {$realif} {\n";
3404
					$dhcp6cconf .= "		sla-id {$config['interfaces'][$friendly]['track6-prefix-id']};\n";
3405
					$dhcp6cconf .= "		sla-len {$wancfg['dhcp6-ia-pd-len']};\n";
3406
					$dhcp6cconf .= "	};\n";
3407
				}
3408
			}
3409
			$dhcp6cconf .= "};\n";
3410
		}
3411 ed395640 Seth Mos
	}
3412
	fwrite($fd, $dhcp6cconf);
3413
	fclose($fd);
3414
3415 baf9fdca smos
	/* Add wide-dhcp6c shell script here. Because we can not pass a argument to it. */
3416
	$fds = fopen("{$g['varetc_path']}/dhcp6c_{$interface}_script.sh", "w");
3417
	if (!$fds) {
3418
		printf("Error: cannot open dhcp6c_{$interface}_script.sh in interfaces_wan_dhcpv6_configure() for writing.\n");
3419
		return 1;
3420
	}
3421
	$dhcp6cscript = "#!/bin/sh\n";
3422
	$dhcp6cscript .= "# This shell script launches /etc/rc.newwanipv6 with a interface argument.\n";
3423
	$dhcp6cscript .= "/etc/rc.newwanipv6 $interface \n";
3424
3425
	fwrite($fds, $dhcp6cscript);
3426
	fclose($fds);
3427
	chmod("{$g['varetc_path']}/dhcp6c_{$interface}_script.sh", 0755);
3428
3429
3430 c65d3051 Seth Mos
	/* accept router advertisements for this interface */
3431 ef851fed smos
	mwexec("/sbin/sysctl -w net.inet6.ip6.accept_rtadv=1");
3432 49047fb4 smos
	log_error("Accept router advertisements on interface {$wanif} ");
3433 100c7be0 Seth Mos
	mwexec("/sbin/ifconfig {$wanif} inet6 accept_rtadv");
3434 28ba77e4 smos
	mwexec("/sbin/ifconfig {$wanif} inet6 defroute_rtadv");
3435 82769dfe smos
3436 ed395640 Seth Mos
	/* fire up dhcp6c for IPv6 first, this backgrounds immediately */
3437 de06b5b7 Seth Mos
	mwexec("/usr/local/sbin/dhcp6c -d -c {$g['varetc_path']}/dhcp6c_{$interface}.conf {$wanif}");
3438 baf9fdca smos
	exec("/sbin/rtsol -d {$wanif} 2>&1", $out, $ret);
3439
	if(!empty($out)) {
3440
		foreach($out as $line) {
3441
			if(stristr($line, "received")) {
3442
				$parts = explode(" ", $line);
3443
				if(is_ipaddrv6($parts[3])) {
3444
					log_error("Found IPv6 default gateway '{$parts[3]}' by RA.");
3445
					file_put_contents("{$g['tmp_path']}/{$wanif}_routerv6", "{$parts[3]}\n");
3446 2d5ca06e smos
					file_put_contents("{$g['tmp_path']}/{$wanif}_defaultgwv6", "{$parts[3]}\n");
3447 feb88a14 smos
					break;
3448 baf9fdca smos
				}
3449
			}
3450
		}
3451
	}
3452 82769dfe smos
	/* sleep a few seconds before returning to give the client some time
3453
	 * to configure a lan interface with a prefix */
3454
	sleep(5);
3455 2d5ca06e smos
3456 feb88a14 smos
	if($wancfg['ippaddrv6'] != "slaac") {
3457
		/* configure dependent interfaces */
3458
		foreach($iflist as $if => $ifname) {
3459
			if($config['interfaces'][$if]['track6-interface'] == $interface)
3460
				interface_track6_configure($if);
3461
		}
3462 2d5ca06e smos
	}
3463 ed395640 Seth Mos
	return 0;
3464
}
3465
3466 8103bd1e Seth Mos
function interface_dhcp_configure($interface = "wan") {
3467 ed395640 Seth Mos
	global $config, $g;
3468
3469
	$wancfg = $config['interfaces'][$interface];
3470
	$wanif = $wancfg['if'];
3471 df9e93f0 Ermal
	if (empty($wancfg))
3472
		$wancfg = array();
3473 5b237745 Scott Ullrich
3474 0311dbd5 Scott Ullrich
	/* generate dhclient_wan.conf */
3475 67ee1ec5 Ermal Luçi
	$fd = fopen("{$g['varetc_path']}/dhclient_{$interface}.conf", "w");
3476 5b237745 Scott Ullrich
	if (!$fd) {
3477 07e40c1f Carlos Eduardo Ramos
		printf(printf(gettext("Error: cannot open dhclient_%s.conf in interfaces_wan_dhcp_configure() for writing.%s"), $interface, "\n"));
3478 5b237745 Scott Ullrich
		return 1;
3479
	}
3480 eb772abd Scott Ullrich
3481 2305d4c5 Scott Ullrich
	if ($wancfg['dhcphostname']) {
3482
		$dhclientconf_hostname = "send dhcp-client-identifier \"{$wancfg['dhcphostname']}\";\n";
3483
		$dhclientconf_hostname .= "\tsend host-name \"{$wancfg['dhcphostname']}\";\n";
3484
	} else {
3485
		$dhclientconf_hostname = "";
3486
	}
3487
3488 85a5da13 Ermal Luçi
	$wanif = get_real_interface($interface);
3489 df9e93f0 Ermal
	if (empty($wanif)) {
3490 07e40c1f Carlos Eduardo Ramos
		log_error(sprintf(gettext("Invalid interface \"%s\" in interface_dhcp_configure()"), $interface));
3491 c1cc447c gnhb
		return 0;
3492 3a906378 gnhb
	}
3493 67ee1ec5 Ermal Luçi
 	$dhclientconf = "";
3494
	
3495 6d76590c Scott Ullrich
	$dhclientconf .= <<<EOD
3496 67ee1ec5 Ermal Luçi
interface "{$wanif}" {
3497 76d3b9a3 Chris Buechler
timeout 60;
3498 ce69a638 Scott Ullrich
retry 1;
3499
select-timeout 0;
3500
initial-interval 1;
3501 2305d4c5 Scott Ullrich
	{$dhclientconf_hostname}
3502
	script "/sbin/dhclient-script";
3503 5b237745 Scott Ullrich
}
3504
3505
EOD;
3506
3507 bc40d758 Seth Mos
if(is_ipaddr($wancfg['alias-address'])) {
3508
	$subnetmask = gen_subnet_mask($wancfg['alias-subnet']);
3509
	$dhclientconf .= <<<EOD
3510
alias {
3511 67ee1ec5 Ermal Luçi
	interface  "{$wanif}";
3512 bc40d758 Seth Mos
	fixed-address {$wancfg['alias-address']};
3513
	option subnet-mask {$subnetmask};
3514
}
3515
3516
EOD;
3517
}
3518 5b237745 Scott Ullrich
	fwrite($fd, $dhclientconf);
3519
	fclose($fd);
3520 eb772abd Scott Ullrich
3521 d7147b1c Scott Ullrich
	/* bring wan interface up before starting dhclient */
3522 3a906378 gnhb
	if($wanif)
3523
		interfaces_bring_up($wanif);
3524 b5b957fe Scott Ullrich
	else 
3525 07e40c1f Carlos Eduardo Ramos
		log_error(printf(gettext("Could not bring up %s interface in interface_dhcp_configure()"), $wanif));
3526 eacc8c14 Scott Ullrich
3527 7149c4e7 Seth Mos
	/* fire up dhclient */
3528 85936586 Charlie
	mwexec("/sbin/dhclient -c {$g['varetc_path']}/dhclient_{$interface}.conf {$wanif} > {$g['tmp_path']}/{$wanif}_output > {$g['tmp_path']}/{$wanif}_error_output");
3529 0119d2f7 Scott Ullrich
3530 5b237745 Scott Ullrich
	return 0;
3531
}
3532
3533 42753d25 Ermal Lu?i
function interfaces_group_setup() {
3534
	global $config;
3535
3536
	if (!is_array($config['ifgroups']['ifgroupentry']))
3537
		return;
3538
3539 482961e3 Ermal Lu?i
	foreach ($config['ifgroups']['ifgroupentry'] as $groupar)
3540 42753d25 Ermal Lu?i
		interface_group_setup($groupar);
3541
3542
	return;
3543
}
3544
3545 abcb2bed Ermal Lu?i
function interface_group_setup(&$groupname /* The parameter is an array */) {
3546 42753d25 Ermal Lu?i
	global $config;
3547
3548
	if (!is_array($groupname))
3549
		return;
3550
	$members = explode(" ", $groupname['members']);
3551
	foreach($members as $ifs) {
3552
		$realif = get_real_interface($ifs);
3553
		if ($realif)
3554
			mwexec("/sbin/ifconfig {$realif} group {$groupname['ifname']}");
3555
	}
3556
3557
	return;
3558
}
3559 48f23632 Ermal
3560
function interface_group_add_member($interface, $groupname) {
3561 ed62880b Ermal
	$interface = get_real_interface($interface);
3562 48f23632 Ermal
	mwexec("/sbin/ifconfig {$interface} group {$groupname}", true);
3563
}
3564 f6b761fb Scott Ullrich
 
3565 e8910ad4 Ermal Lu?i
/* COMPAT Function */
3566 afb2de1b Ermal Lu?i
function convert_friendly_interface_to_real_interface_name($interface) {
3567
	return get_real_interface($interface);
3568
}
3569
3570 e8910ad4 Ermal Lu?i
/* COMPAT Function */
3571 eba938e3 Scott Ullrich
function get_real_wan_interface($interface = "wan") {
3572 abb31ea4 Ermal Luçi
	return get_real_interface($interface);
3573
}
3574 afb2de1b Ermal Lu?i
3575 e8910ad4 Ermal Lu?i
/* COMPAT Function */
3576 eba938e3 Scott Ullrich
function get_current_wan_address($interface = "wan") {
3577 abb31ea4 Ermal Luçi
	return get_interface_ip($interface);
3578
}
3579
3580 afb2de1b Ermal Lu?i
/*
3581
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
3582
 */
3583
function convert_real_interface_to_friendly_interface_name($interface = "wan") {
3584
        global $config;
3585
3586 7b47bd4c Ermal
	if (stristr($interface, "_vip")) {
3587 6d5446a2 Ermal
                foreach ($config['virtualip']['vip'] as $counter => $vip) {
3588 564df7c2 Ermal Lu?i
                        if ($vip['mode'] == "carpdev-dhcp" || $vip['mode'] == "carp")  {
3589 7b47bd4c Ermal
                                if ($interface == "{$vip['interface']}_vip{$vip['vhid']}")
3590 564df7c2 Ermal Lu?i
                                        return $vip['interface'];
3591
                        }
3592
                }
3593 afb2de1b Ermal Lu?i
        }
3594
3595 6d5446a2 Ermal
        /* XXX: For speed reasons reference directly the interface array */
3596 74e1e658 jim-p
	$ifdescrs = &$config['interfaces'];
3597 6d5446a2 Ermal
        //$ifdescrs = get_configured_interface_list(false, true);
3598 afb2de1b Ermal Lu?i
3599
        foreach ($ifdescrs as $if => $ifname) {
3600 6d5446a2 Ermal
                if ($config['interfaces'][$if]['if'] == $interface)
3601
                        return $if;
3602 afb2de1b Ermal Lu?i
3603 52ab1d44 Erik Fonnesbeck
                if (stristr($interface, "_wlan0") && $config['interfaces'][$if]['if'] == interface_get_wireless_base($interface))
3604 af637766 Erik Fonnesbeck
                        return $if;
3605
3606 d11e01f4 Erik Fonnesbeck
		// XXX: This case doesn't work anymore (segfaults - recursion?) - should be replaced with something else or just removed.
3607
		//      Not to be replaced with get_real_interface - causes slow interface listings here because of recursion!
3608
		/*
3609 a1476a94 Erik Fonnesbeck
                $int = get_parent_interface($if);
3610 56919157 Erik Fonnesbeck
                if ($int[0] == $interface)
3611 afb2de1b Ermal Lu?i
                        return $ifname;
3612 d11e01f4 Erik Fonnesbeck
		*/
3613 afb2de1b Ermal Lu?i
        }
3614
        return NULL;
3615
}
3616
3617
/* attempt to resolve interface to friendly descr */
3618
function convert_friendly_interface_to_friendly_descr($interface) {
3619
        global $config;
3620
3621
        switch ($interface) {
3622 68ef6e03 Ermal
        case "l2tp":
3623
        	$ifdesc = "L2TP";
3624
                break;
3625
	case "pptp":
3626
		$ifdesc = "PPTP";
3627
		break;
3628
	case "pppoe":
3629
		$ifdesc = "PPPoE";
3630
		break;
3631
	case "openvpn":
3632
		$ifdesc = "OpenVPN";
3633
		break;
3634
	case "enc0":
3635
	case "ipsec":
3636
		$ifdesc = "IPsec";
3637
		break;
3638 afb2de1b Ermal Lu?i
        default:
3639 57c52d45 Erik Fonnesbeck
                if (isset($config['interfaces'][$interface])) {
3640
                        if (empty($config['interfaces'][$interface]['descr']))
3641
                                $ifdesc = strtoupper($interface);
3642
                        else
3643
                                $ifdesc = strtoupper($config['interfaces'][$interface]['descr']);
3644
			break;
3645 7b47bd4c Ermal
		} else if (stristr($interface, "_vip")) {
3646 68ef6e03 Ermal
			if (is_array($config['virtualip']['vip'])) {
3647
				foreach ($config['virtualip']['vip'] as $counter => $vip) {
3648
					if ($vip['mode'] == "carpdev-dhcp" || $vip['mode'] == "carp")  {
3649 7b47bd4c Ermal
						if ($interface == "{$vip['interface']}_vip{$vip['vhid']}")
3650 68ef6e03 Ermal
							return "{$vip['subnet']} - {$vip['descr']}";
3651
					}
3652
				}
3653
                        }
3654
                } else {
3655
			/* if list */
3656
			$ifdescrs = get_configured_interface_with_descr(false, true);
3657
			foreach ($ifdescrs as $if => $ifname) {
3658 7b47bd4c Ermal
				if ($if == $interface || $ifname == $interface)
3659
					return $ifname;
3660 68ef6e03 Ermal
			}
3661 57c52d45 Erik Fonnesbeck
		}
3662 afb2de1b Ermal Lu?i
                break;
3663
        }
3664
3665
        return $ifdesc;
3666
}
3667
3668
function convert_real_interface_to_friendly_descr($interface) {
3669
        global $config;
3670
3671
        $ifdesc = convert_real_interface_to_friendly_interface_name("{$interface}");
3672
3673
        if ($ifdesc) {
3674 c795339e Ermal Lu?i
                $iflist = get_configured_interface_with_descr(false, true);
3675 afb2de1b Ermal Lu?i
                return $iflist[$ifdesc];
3676
        }
3677
3678
        return $interface;
3679
}
3680
3681 532b0fb8 Ermal Lu?i
/*
3682 d5dfcb52 gnhb
 *  get_parent_interface($interface):
3683 20cb9803 gnhb
 *			--returns the (real or virtual) parent interface(s) array for a given interface friendly name (i.e. wan)
3684
 *				or virtual interface (i.e. vlan)
3685
 *				(We need array because MLPPP and bridge interfaces have more than one parent.)
3686
 *			-- returns $interface passed in if $interface parent is not found
3687
 *			-- returns empty array if an invalid interface is passed
3688
 *	(Only handles ppps and vlans now.)
3689 532b0fb8 Ermal Lu?i
 */
3690 d5dfcb52 gnhb
function get_parent_interface($interface) {
3691
	global $config;
3692 532b0fb8 Ermal Lu?i
3693 20cb9803 gnhb
	$parents = array();
3694
	//Check that we got a valid interface passed
3695
	$realif = get_real_interface($interface);
3696
	if ($realif == NULL)
3697
		return $parents;
3698
3699
	// If we got a real interface, find it's friendly assigned name
3700
	$interface = convert_real_interface_to_friendly_interface_name($interface);
3701
		
3702
	if (!empty($interface) && isset($config['interfaces'][$interface])) {
3703
		$ifcfg = $config['interfaces'][$interface];
3704
		switch ($ifcfg['ipaddr']) {
3705
			case "ppp":
3706
			case "pppoe":
3707
			case "pptp":
3708
			case "l2tp":
3709
				if (empty($parents))
3710
					if (is_array($config['ppps']['ppp']))
3711
						foreach ($config['ppps']['ppp'] as $pppidx => $ppp) {
3712
							if ($ppp_if == $ppp['if']) {
3713
								$ports = explode(',', $ppp['ports']);
3714
								foreach ($ports as $pid => $parent_if) 
3715
									$parents[$pid] = get_real_interface($parent_if);
3716
								break;
3717
							}
3718
						}
3719
				break;
3720
			case "dhcp":
3721
			case "static":
3722
			default:
3723
				// Handle _vlans
3724
				if (strstr($realif,"_vlan"))
3725
					if (is_array($config['vlans']['vlan'])) 
3726
						foreach ($config['vlans']['vlan'] as $vlanidx => $vlan)
3727
							if ($ifcfg['if'] == $vlan['vlanif']){
3728
								$parents[0] = $vlan['if'];
3729
								break;
3730
							}
3731
				break;
3732 3e5d0d1d Ermal
		}
3733
	}
3734 20cb9803 gnhb
	
3735
	if (empty($parents))
3736
		$parents[0] = $realif;
3737
	
3738
	return $parents;
3739 532b0fb8 Ermal Lu?i
}
3740
3741 263e2b7e Erik Fonnesbeck
function interface_is_wireless_clone($wlif) {
3742
	if(!stristr($wlif, "_wlan")) {
3743
		return false;
3744
	} else {
3745
		return true;
3746
	}
3747
}
3748
3749 1d072761 Erik Fonnesbeck
function interface_get_wireless_base($wlif) {
3750 34808d4e Erik Fonnesbeck
	if(!stristr($wlif, "_wlan")) {
3751
		return $wlif;
3752
	} else {
3753
		return substr($wlif, 0, stripos($wlif, "_wlan"));
3754
	}
3755
}
3756
3757 1d072761 Erik Fonnesbeck
function interface_get_wireless_clone($wlif) {
3758 34808d4e Erik Fonnesbeck
	if(!stristr($wlif, "_wlan")) {
3759
		return $wlif . "_wlan0";
3760
	} else {
3761
		return $wlif;
3762
	}
3763
}
3764
3765 df9e93f0 Ermal
function get_real_interface($interface = "wan") {
3766 67ee1ec5 Ermal Luçi
    global $config;
3767 cfc707f7 Scott Ullrich
3768 521cfa2f Ermal Lu?i
	$wanif = NULL;
3769 c515ea57 Scott Ullrich
3770 67ee1ec5 Ermal Luçi
	switch ($interface) {
3771 acc1e9d0 Scott Ullrich
	case "l2tp":
3772
		$wanif = "l2tp";
3773
		break;
3774 67ee1ec5 Ermal Luçi
	case "pptp":
3775
		$wanif = "pptp";
3776
		break;
3777
	case "pppoe":
3778
		$wanif = "pppoe";
3779
		break;
3780
	case "openvpn":
3781
		$wanif = "openvpn";
3782
		break;
3783 4563d12f Seth Mos
	case "ipsec":
3784 67ee1ec5 Ermal Luçi
	case "enc0":
3785
		$wanif = "enc0";
3786
		break;
3787
	case "ppp":
3788
		$wanif = "ppp";
3789
		break;
3790
	default:
3791 6d5446a2 Ermal
		// If a real interface was alread passed simply
3792
		// pass the real interface back.  This encourages
3793
		// the usage of this function in more cases so that
3794
		// we can combine logic for more flexibility.
3795
		if(does_interface_exist($interface)) {
3796
			$wanif = $interface;
3797
			break;
3798
		}
3799
		if (empty($config['interfaces'][$interface]))
3800
			break;
3801 568b1358 Scott Ullrich
3802 6447bde5 jim-p
		$cfg = &$config['interfaces'][$interface];
3803 2ebf3945 Scott Ullrich
3804 6d5446a2 Ermal
		// Wireless cloned NIC support (FreeBSD 8+)
3805
		// interface name format: $parentnic_wlanparentnic#
3806
		// example: ath0_wlan0
3807
		if (is_interface_wireless($cfg['if'])) {
3808
			$wanif = interface_get_wireless_clone($cfg['if']);
3809
			break;
3810
		}
3811
		/*
3812
		if (empty($cfg['if'])) {
3813
			$wancfg = $cfg['if'];
3814
			break;
3815
		}
3816
		*/
3817 e7693c09 Ermal Lu?i
3818 6d5446a2 Ermal
		switch ($cfg['ipaddr']) {
3819
			case "carpdev-dhcp":
3820
				$viparr = &$config['virtualip']['vip'];
3821
				if(is_array($viparr))
3822
				foreach ($viparr as $counter => $vip) {
3823
					if ($vip['mode'] == "carpdev-dhcp") {
3824
						if($vip['interface'] == $interface) {
3825
							$wanif = "carp{$counter}";
3826
							break;
3827 3a906378 gnhb
						}
3828
					}
3829
				}
3830 b99256c1 Scott Ullrich
				break;
3831 6d5446a2 Ermal
			case "pppoe": 
3832
			case "pptp": 
3833
			case "l2tp": 
3834
			case "ppp":
3835 277d0250 gnhb
				$wanif = $cfg['if'];
3836 6d5446a2 Ermal
				break;
3837
			default:
3838
				$wanif = $cfg['if'];
3839
				break;
3840 c515ea57 Scott Ullrich
		}
3841 67ee1ec5 Ermal Luçi
		break;
3842 c515ea57 Scott Ullrich
	}
3843
3844 67ee1ec5 Ermal Luçi
    return $wanif;
3845 5b237745 Scott Ullrich
}
3846
3847 9ff8c299 Seth Mos
/* Guess the physical interface by providing a IP address */
3848 afb2de1b Ermal Lu?i
function guess_interface_from_ip($ipaddress) {
3849 80a2c1e6 Seth Mos
	if(! is_ipaddr($ipaddress)) {
3850 9ff8c299 Seth Mos
		return false;
3851
	}
3852 a05b2f42 Seth Mos
	if(is_ipaddrv4($ipaddress)) {
3853
		/* create a route table we can search */
3854
		exec("netstat -rnWf inet", $output, $ret);
3855
		foreach($output as $line) {
3856
			if(preg_match("/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+[ ]+link[#]/", $line)) {
3857
				$fields = preg_split("/[ ]+/", $line);
3858
				if(ip_in_subnet($ipaddress, $fields[0])) {
3859
					return $fields[6];
3860
				}
3861
			}
3862
		}
3863
	}
3864
	/* FIXME: This works from cursory testing, regexp might need fine tuning */
3865
	if(is_ipaddrv6($ipaddress)) {
3866
		/* create a route table we can search */
3867
		exec("netstat -rnWf inet6", $output, $ret);
3868
		foreach($output as $line) {
3869
			if(preg_match("/[0-9a-f]+[:]+[0-9a-f]+[:]+[\/][0-9]+/", $line)) {
3870
				$fields = preg_split("/[ ]+/", $line);
3871
				if(ip_in_subnet($ipaddress, $fields[0])) {
3872
					return $fields[6];
3873
				}
3874 9ff8c299 Seth Mos
			}
3875
		}
3876
	}
3877
	$ret = exec_command("/sbin/route -n get {$ipaddress} | /usr/bin/awk '/interface/ { print \$2; };'");
3878
	if(empty($ret)) {
3879
        	return false;
3880
	}
3881
	return $ret;
3882 afb2de1b Ermal Lu?i
}
3883
3884
/*
3885
 * find_ip_interface($ip): return the interface where an ip is defined
3886 59231855 Darren Embry
 *   (or if $bits is specified, where an IP within the subnet is defined)
3887 afb2de1b Ermal Lu?i
 */
3888 59231855 Darren Embry
function find_ip_interface($ip, $bits = null)
3889 afb2de1b Ermal Lu?i
{
3890 59231855 Darren Embry
	/* if list */
3891
	$ifdescrs = get_configured_interface_list();
3892
		
3893
	foreach ($ifdescrs as $ifdescr => $ifname) {
3894
		if ($bits === null) {
3895
			if ($ip == get_interface_ip($ifname)) {
3896
				$int = get_real_interface($ifname);
3897
				return $int;
3898
			}
3899
		}
3900
		else {
3901
			if (ip_in_subnet(get_interface_ip($ifname), $ip . "/" . $bits)) {
3902
				$int = get_real_interface($ifname);
3903
				return $int;
3904
			}
3905
		}
3906
	}
3907
	return false;
3908
}
3909 afb2de1b Ermal Lu?i
3910 59231855 Darren Embry
/*
3911
 * find_virtual_ip_alias($ip): return the virtual IP alias where an IP is found
3912
 *   (or if $bits is specified, where an IP within the subnet is found)
3913
 */
3914
function find_virtual_ip_alias($ip, $bits = null) {
3915
	global $config;
3916
	if (!is_array($config['virtualip']['vip'])) {
3917
		return false;
3918
	}
3919
	foreach ($config['virtualip']['vip'] as $vip) {
3920
		if ($vip['mode'] === "ipalias") {
3921
			if ($bits === null) {
3922
				if (ip_in_subnet($ip, $vip['subnet'] . "/" . $vip['subnet_bits'])) {
3923
					return $vip;
3924
				}
3925
			}
3926
			else {
3927
				if (check_subnets_overlap($ip, $bits, $vip['subnet'], $vip['subnet_bits'])) {
3928
					return $vip;
3929
				}
3930
			}
3931 abcb2bed Ermal Lu?i
		}
3932 59231855 Darren Embry
	}
3933
	return false;
3934 afb2de1b Ermal Lu?i
}
3935
3936 a71b32d2 Scott Ullrich
/*
3937
 *   find_number_of_created_carp_interfaces: return the number of carp interfaces
3938
 */
3939
function find_number_of_created_carp_interfaces() {
3940
	return `/sbin/ifconfig | grep "carp:" | wc -l`;
3941
}
3942
3943
function get_all_carp_interfaces() {
3944
	$ints = str_replace("\n", " ", `ifconfig | grep "carp:" -B2 | grep ": flag" | cut -d: -f1`);
3945 81c64284 Chris Buechler
	$ints = explode(" ", $ints);
3946 a71b32d2 Scott Ullrich
	return $ints;
3947
}
3948
3949 abcb2bed Ermal Lu?i
/*
3950
 * find_carp_interface($ip): return the carp interface where an ip is defined
3951
 */
3952
function find_carp_interface($ip) {
3953 27625b39 Scott Ullrich
	global $config;
3954 abcb2bed Ermal Lu?i
	if (is_array($config['virtualip']['vip'])) {
3955
		foreach ($config['virtualip']['vip'] as $vip) {
3956
			if ($vip['mode'] == "carp" || $vip['mode'] == "carpdev") {
3957 645ad665 Seth Mos
				if(is_ipaddrv4($ip)) {
3958
					$carp_ip = get_interface_ip($vip['interface']);
3959
				}
3960
				if(is_ipaddrv6($ip)) {
3961
					$carp_ip = get_interface_ipv6($vip['interface']);
3962
				}
3963
				exec("/sbin/ifconfig", $output, $return);
3964
				foreach($output as $line) {
3965
					$elements = preg_split("/[ ]+/i", $line);
3966
					if(strstr($elements[0], "vip"))
3967
						$curif = str_replace(":", "", $elements[0]);
3968
					if(stristr($line, $ip)) {
3969
						$if = $curif;
3970
						continue;
3971
					}
3972
				}
3973 a687f866 Namezero
3974 27625b39 Scott Ullrich
				if ($if)
3975
					return $if;
3976 abcb2bed Ermal Lu?i
			}
3977
		}
3978
	}
3979
}
3980
3981
function link_carp_interface_to_parent($interface) {
3982
        global $config;
3983
3984
        if ($interface == "")
3985
                return;
3986
3987 564df7c2 Ermal Lu?i
        $carp_ip = get_interface_ip($interface);
3988 abcb2bed Ermal Lu?i
        if (!is_ipaddr($carp_ip))
3989
                return;
3990
3991
        /* if list */
3992
        $ifdescrs = get_configured_interface_list();
3993
        foreach ($ifdescrs as $ifdescr => $ifname) {
3994
                $interfaceip = get_interface_ip($ifname);
3995
                $subnet_bits = get_interface_subnet($ifname);
3996
                $subnet_ip = gen_subnet("{$interfaceip}", "{$subnet_bits}");
3997
                if(ip_in_subnet($carp_ip, "{$subnet_ip}/{$subnet_bits}"))
3998
                        return $ifname;
3999
        }
4000
4001
        return "";
4002
}
4003
4004
/****f* interfaces/link_ip_to_carp_interface
4005
 * NAME
4006
 *   link_ip_to_carp_interface - Find where a CARP interface links to.
4007
 * INPUTS
4008
 *   $ip
4009
 * RESULT
4010
 *   $carp_ints
4011
 ******/
4012
function link_ip_to_carp_interface($ip) {
4013
        global $config;
4014
4015
        if (!is_ipaddr($ip))
4016
                return;
4017
4018
        $carp_ints = "";
4019
        if (is_array($config['virtualip']['vip'])) {
4020 1d002dc9 Ermal
		$first = 0;
4021 3fbc3487 Ermal
		$carp_int = array();
4022 abcb2bed Ermal Lu?i
                foreach ($config['virtualip']['vip'] as $vip) {
4023
                        if ($vip['mode'] == "carp" || $vip['mode'] == "carpdev") {
4024 6b060a2f Scott Ullrich
                                $carp_ip = $vip['subnet'];
4025 abcb2bed Ermal Lu?i
                                $carp_sn = $vip['subnet_bits'];
4026
                                $carp_nw = gen_subnet($carp_ip, $carp_sn);
4027 7b47bd4c Ermal
                                if (ip_in_subnet($ip, "{$carp_nw}/{$carp_sn}")) {
4028
					$carp_int[] = "{$vip['interface']}_vip{$vip['vhid']}";
4029
				}
4030 abcb2bed Ermal Lu?i
                        }
4031
                }
4032 3fbc3487 Ermal
		if (!empty($carp_int))
4033
			$carp_ints = implode(" ", array_unique($carp_int));
4034 abcb2bed Ermal Lu?i
        }
4035
4036
        return $carp_ints;
4037
}
4038
4039 7850de1c Ermal Lu?i
function link_interface_to_vlans($int, $action = "") {
4040
	global $config;
4041
4042
	if (empty($int))
4043
		return;
4044
4045
	if (is_array($config['vlans']['vlan'])) {
4046
                foreach ($config['vlans']['vlan'] as $vlan) {
4047 fa4a331f Ermal
			if ($int == $vlan['if']) {
4048 7850de1c Ermal Lu?i
				if ($action == "update") {
4049 fa4a331f Ermal
					interfaces_bring_up($int);
4050 7850de1c Ermal Lu?i
				} else if ($action == "")
4051
					return $vlan;
4052
			}
4053
		}
4054
	}
4055
}
4056
4057
function link_interface_to_vips($int, $action = "") {
4058 e5ac67ed Ermal Lu?i
        global $config;
4059
4060 dcadda55 Ermal
        if (is_array($config['virtualip']['vip'])) {
4061
		foreach ($config['virtualip']['vip'] as $vip) {
4062
			if ($int == $vip['interface']) {
4063 7b47bd4c Ermal
				if ($action == "update")
4064
					interfaces_vips_configure($int);
4065
				else
4066 dcadda55 Ermal
					return $vip;
4067 7850de1c Ermal Lu?i
			}
4068 dcadda55 Ermal
		}
4069
	}
4070 e5ac67ed Ermal Lu?i
}
4071
4072 afb2de1b Ermal Lu?i
/****f* interfaces/link_interface_to_bridge
4073
 * NAME
4074
 *   link_interface_to_bridge - Finds out a bridge group for an interface
4075
 * INPUTS
4076
 *   $ip
4077
 * RESULT
4078
 *   bridge[0-99]
4079
 ******/
4080
function link_interface_to_bridge($int) {
4081
        global $config;
4082
4083 a639bb91 Ermal
        if (is_array($config['bridges']['bridged'])) {
4084
                foreach ($config['bridges']['bridged'] as $bridge) {
4085
			if (in_array($int, explode(',', $bridge['members'])))
4086 afb2de1b Ermal Lu?i
                                return "{$bridge['bridgeif']}";
4087 a639bb91 Ermal
		}
4088
	}
4089 afb2de1b Ermal Lu?i
}
4090
4091 48f23632 Ermal
function link_interface_to_group($int) {
4092
        global $config;
4093
4094 ed62880b Ermal
	$result = array();
4095
4096 48f23632 Ermal
        if (is_array($config['ifgroups']['ifgroupentry'])) {
4097
                foreach ($config['ifgroups']['ifgroupentry'] as $group) {
4098 1dbc0c43 Ermal
			if (in_array($int, explode(" ", $group['members'])))
4099 ed62880b Ermal
				$result[$group['ifname']] = $int;
4100 48f23632 Ermal
		}
4101
	}
4102 ed62880b Ermal
4103
	return $result;
4104 48f23632 Ermal
}
4105
4106 afb2de1b Ermal Lu?i
function link_interface_to_gre($interface) {
4107
        global $config;
4108
4109 ed62880b Ermal
	$result = array();
4110
4111
        if (is_array($config['gres']['gre'])) {
4112 afb2de1b Ermal Lu?i
                foreach ($config['gres']['gre'] as $gre)
4113
                        if($gre['if'] == $interface)
4114 ed62880b Ermal
				$result[] = $gre;
4115
	}
4116
4117
	return $result;
4118 afb2de1b Ermal Lu?i
}
4119
4120
function link_interface_to_gif($interface) {
4121
        global $config;
4122
4123 ed62880b Ermal
	$result = array();
4124
4125
        if (is_array($config['gifs']['gif'])) {
4126 afb2de1b Ermal Lu?i
                foreach ($config['gifs']['gif'] as $gif)
4127
                        if($gif['if'] == $interface)
4128 ed62880b Ermal
                                $result[] = $gif;
4129
	}
4130
4131
	return $result;
4132 afb2de1b Ermal Lu?i
}
4133
4134
/*
4135
 * find_interface_ip($interface): return the interface ip (first found)
4136
 */
4137
function find_interface_ip($interface, $flush = false)
4138
{
4139
	global $interface_ip_arr_cache;
4140 01f1b601 Ermal
	global $interface_sn_arr_cache;
4141 afb2de1b Ermal Lu?i
4142
	$interface = str_replace("\n", "", $interface);
4143 00380613 Scott Ullrich
	
4144 8256f324 gnhb
	if (!does_interface_exist($interface))
4145 afb2de1b Ermal Lu?i
		return;
4146
4147
	/* Setup IP cache */
4148
	if (!isset($interface_ip_arr_cache[$interface]) or $flush) {
4149 3f70e618 Ermal Lu?i
		$ifinfo = pfSense_get_interface_addresses($interface);
4150
		$interface_ip_arr_cache[$interface] = $ifinfo['ipaddr'];
4151 01f1b601 Ermal
		$interface_sn_arr_cache[$interface] = $ifinfo['subnetbits'];
4152 afb2de1b Ermal Lu?i
	}
4153
4154
	return $interface_ip_arr_cache[$interface];
4155
}
4156
4157 47593ac6 Seth Mos
/*
4158
 * find_interface_ipv6($interface): return the interface ip (first found)
4159
 */
4160
function find_interface_ipv6($interface, $flush = false)
4161
{
4162
	global $interface_ipv6_arr_cache;
4163
	global $interface_snv6_arr_cache;
4164 31ace4ea Seth Mos
	global $config;
4165
	
4166 47593ac6 Seth Mos
	$interface = str_replace("\n", "", $interface);
4167
	
4168
	if (!does_interface_exist($interface))
4169
		return;
4170
4171
	/* Setup IP cache */
4172
	if (!isset($interface_ipv6_arr_cache[$interface]) or $flush) {
4173
		$ifinfo = pfSense_get_interface_addresses($interface);
4174 3c5e10fc Seth Mos
		// FIXME: Add IPv6 support to the pfSense module
4175 31ace4ea Seth Mos
		exec("/sbin/ifconfig {$interface} inet6", $output);
4176
		foreach($output as $line) {
4177
			if(preg_match("/inet6/", $line)) {
4178
				$parts = explode(" ", $line);
4179 c9d174df Seth Mos
				if(! preg_match("/fe80::/", $parts[1])) {
4180 31ace4ea Seth Mos
					$ifinfo['ipaddrv6'] = $parts[1];
4181 a23a99cb Seth Mos
					if($parts[2] == "-->") {
4182 cf6bc278 Seth Mos
						$parts[5] = "126";
4183 9991ff2c Seth Mos
						$ifinfo['subnetbitsv6'] = $parts[5];
4184 a23a99cb Seth Mos
					} else {
4185 9991ff2c Seth Mos
						$ifinfo['subnetbitsv6'] = $parts[3];
4186 a23a99cb Seth Mos
					}
4187 31ace4ea Seth Mos
				}
4188
			}
4189
		}
4190 47593ac6 Seth Mos
		$interface_ipv6_arr_cache[$interface] = $ifinfo['ipaddrv6'];
4191
		$interface_snv6_arr_cache[$interface] = $ifinfo['subnetbitsv6'];
4192
	}
4193
4194
	return $interface_ipv6_arr_cache[$interface];
4195
}
4196
4197 81a3b6f5 smos
/*
4198
 * find_interface_ipv6_ll($interface): return the interface ipv6 link local (first found)
4199
 */
4200
function find_interface_ipv6_ll($interface, $flush = false)
4201
{
4202 58418355 smos
	global $interface_llv6_arr_cache;
4203 81a3b6f5 smos
	global $config;
4204
	
4205
	$interface = str_replace("\n", "", $interface);
4206
	
4207
	if (!does_interface_exist($interface))
4208
		return;
4209
4210
	/* Setup IP cache */
4211 58418355 smos
	if (!isset($interface_llv6_arr_cache[$interface]) or $flush) {
4212 81a3b6f5 smos
		$ifinfo = pfSense_get_interface_addresses($interface);
4213
		// FIXME: Add IPv6 support to the pfSense module
4214
		exec("/sbin/ifconfig {$interface} inet6", $output);
4215
		foreach($output as $line) {
4216
			if(preg_match("/inet6/", $line)) {
4217
				$parts = explode(" ", $line);
4218
				if(preg_match("/fe80::/", $parts[1])) {
4219 58418355 smos
					$partsaddress = explode("%", $parts[1]);
4220
					$ifinfo['linklocal'] = $partsaddress[0];
4221 81a3b6f5 smos
				}
4222
			}
4223
		}
4224 58418355 smos
		$interface_llv6_arr_cache[$interface] = $ifinfo['linklocal'];
4225 81a3b6f5 smos
	}
4226 58418355 smos
	return $interface_llv6_arr_cache[$interface];
4227 81a3b6f5 smos
}
4228
4229 afb2de1b Ermal Lu?i
function find_interface_subnet($interface, $flush = false)
4230
{
4231
	global $interface_sn_arr_cache;
4232 01f1b601 Ermal
	global $interface_ip_arr_cache;
4233 afb2de1b Ermal Lu?i
4234
	$interface = str_replace("\n", "", $interface);
4235
	if (does_interface_exist($interface) == false)
4236
		return;
4237
4238
	if (!isset($interface_sn_arr_cache[$interface]) or $flush) {
4239 bd96e1fe Ermal Lu?i
		$ifinfo = pfSense_get_interface_addresses($interface);
4240 01f1b601 Ermal
		$interface_ip_arr_cache[$interface] = $ifinfo['ipaddr'];
4241 bd96e1fe Ermal Lu?i
		$interface_sn_arr_cache[$interface] = $ifinfo['subnetbits'];
4242 afb2de1b Ermal Lu?i
        }
4243
4244
	return $interface_sn_arr_cache[$interface];
4245
}
4246
4247 47593ac6 Seth Mos
function find_interface_subnetv6($interface, $flush = false)
4248
{
4249
	global $interface_snv6_arr_cache;
4250
	global $interface_ipv6_arr_cache;
4251
4252
	$interface = str_replace("\n", "", $interface);
4253
	if (does_interface_exist($interface) == false)
4254
		return;
4255
4256
	if (!isset($interface_snv6_arr_cache[$interface]) or $flush) {
4257
		$ifinfo = pfSense_get_interface_addresses($interface);
4258 3c5e10fc Seth Mos
		// FIXME: Add IPv6 support to the pfSense module
4259 9991ff2c Seth Mos
		exec("/sbin/ifconfig {$interface} inet6", $output);
4260
		foreach($output as $line) {
4261
			if(preg_match("/inet6/", $line)) {
4262
				$parts = explode(" ", $line);
4263
				if(! preg_match("/fe80::/", $parts[1])) {
4264
					$ifinfo['ipaddrv6'] = $parts[1];
4265 a23a99cb Seth Mos
					if($parts[2] == "-->") {
4266 cf6bc278 Seth Mos
						$parts[5] = "126";
4267 9991ff2c Seth Mos
						$ifinfo['subnetbitsv6'] = $parts[5];
4268 a23a99cb Seth Mos
					} else {
4269 9991ff2c Seth Mos
						$ifinfo['subnetbitsv6'] = $parts[3];
4270 a23a99cb Seth Mos
					}
4271 9991ff2c Seth Mos
				}
4272
			}
4273
		}
4274 47593ac6 Seth Mos
		$interface_ipv6_arr_cache[$interface] = $ifinfo['ipaddrv6'];
4275
		$interface_snv6_arr_cache[$interface] = $ifinfo['subnetbitsv6'];
4276
        }
4277
4278
	return $interface_snv6_arr_cache[$interface];
4279
}
4280
4281 e19b7d1e Ermal
function ip_in_interface_alias_subnet($interface, $ipalias) {
4282
	global $config;
4283
4284
	if (empty($interface) || !is_ipaddr($ipalias))
4285 e8471084 Ermal
		return false;
4286 e19b7d1e Ermal
	if (is_array($config['virtualip']['vip'])) {
4287
                foreach ($config['virtualip']['vip'] as $vip) {
4288
                        switch ($vip['mode']) {
4289
                        case "ipalias":
4290
                                if ($vip['interface'] <> $interface)
4291 e8471084 Ermal
                                        break;
4292 e19b7d1e Ermal
				if (ip_in_subnet($ipalias, gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits']))
4293 e8471084 Ermal
					return true;
4294 e19b7d1e Ermal
                                break;
4295
                        }
4296
                }
4297
	}
4298 e8471084 Ermal
4299
	return false;
4300 e19b7d1e Ermal
}
4301
4302 e88fbe50 Ermal Lu?i
function get_interface_ip($interface = "wan")
4303
{
4304 85a5da13 Ermal Luçi
	$realif = get_real_interface($interface);
4305 afb2de1b Ermal Lu?i
	if (!$realif) {
4306
		if (preg_match("/^carp/i", $interface))
4307
			$realif = $interface;
4308 564df7c2 Ermal Lu?i
		else if (preg_match("/^vip/i", $interface))
4309
			$realif = $interface;
4310 afb2de1b Ermal Lu?i
		else
4311
			return null;
4312
	}
4313
4314 5e041d5f Scott Ullrich
	$curip = find_interface_ip($realif);
4315
	if ($curip && is_ipaddr($curip) && ($curip != "0.0.0.0"))
4316
		return $curip;
4317 8256f324 gnhb
	else
4318
		return null;
4319 5b237745 Scott Ullrich
}
4320
4321 47593ac6 Seth Mos
function get_interface_ipv6($interface = "wan")
4322
{
4323 479f0fda smos
	global $config;
4324 47593ac6 Seth Mos
	$realif = get_real_interface($interface);
4325 479f0fda smos
	switch($config['interfaces'][$interface]['ipaddrv6']) {
4326
		case "6rd":
4327
		case "6to4":
4328
			$realif = "stf0";
4329
			break;
4330
	}
4331 47593ac6 Seth Mos
	if (!$realif) {
4332
		if (preg_match("/^carp/i", $interface))
4333
			$realif = $interface;
4334
		else if (preg_match("/^vip/i", $interface))
4335
			$realif = $interface;
4336
		else
4337
			return null;
4338
	}
4339
4340
	$curip = find_interface_ipv6($realif);
4341
	if ($curip && is_ipaddrv6($curip) && ($curip != "::"))
4342
		return $curip;
4343
	else
4344
		return null;
4345
}
4346
4347 58418355 smos
function get_interface_linklocal($interface = "wan")
4348
{
4349
	$realif = get_real_interface($interface);
4350
	if (!$realif) {
4351
		if (preg_match("/^carp/i", $interface))
4352
			$realif = $interface;
4353
		else if (preg_match("/^vip/i", $interface))
4354
			$realif = $interface;
4355
		else
4356
			return null;
4357
	}
4358
4359
	$curip = find_interface_ipv6_ll($realif);
4360
	if ($curip && is_ipaddrv6($curip) && ($curip != "::"))
4361
		return $curip;
4362
	else
4363
		return null;
4364
}
4365
4366 e88fbe50 Ermal Lu?i
function get_interface_subnet($interface = "wan")
4367
{
4368 31b24870 Ermal Luçi
	$realif = get_real_interface($interface);
4369 e88fbe50 Ermal Lu?i
	if (!$realif) {
4370
                if (preg_match("/^carp/i", $interface))
4371
                        $realif = $interface;
4372 564df7c2 Ermal Lu?i
                else if (preg_match("/^vip/i", $interface))
4373
                        $realif = $interface;
4374 e88fbe50 Ermal Lu?i
                else
4375
                        return null;
4376
        }
4377
4378 5e041d5f Scott Ullrich
	$cursn = find_interface_subnet($realif);
4379
	if (!empty($cursn))
4380 31b24870 Ermal Luçi
		return $cursn;
4381
4382
	return null;
4383
}
4384
4385 47593ac6 Seth Mos
function get_interface_subnetv6($interface = "wan")
4386
{
4387
	$realif = get_real_interface($interface);
4388
	if (!$realif) {
4389
                if (preg_match("/^carp/i", $interface))
4390
                        $realif = $interface;
4391
                else if (preg_match("/^vip/i", $interface))
4392
                        $realif = $interface;
4393
                else
4394
                        return null;
4395
        }
4396
4397
	$cursn = find_interface_subnetv6($realif);
4398
	if (!empty($cursn))
4399
		return $cursn;
4400
4401
	return null;
4402
}
4403
4404 52947718 Ermal Lu?i
/* return outside interfaces with a gateway */
4405
function get_interfaces_with_gateway() {
4406 77ccab82 Scott Ullrich
	global $config;
4407 52947718 Ermal Lu?i
4408
	$ints = array();
4409
4410
	/* loop interfaces, check config for outbound */
4411 77ccab82 Scott Ullrich
	foreach($config['interfaces'] as $ifdescr => $ifname) {
4412
		switch ($ifname['ipaddr']) {
4413
			case "dhcp":
4414
			case "carpdev-dhcp":
4415 39f750b5 gnhb
			case "ppp";
4416 77ccab82 Scott Ullrich
			case "pppoe":
4417
			case "pptp":
4418 6d5446a2 Ermal
			case "l2tp":
4419 9ebe7028 gnhb
			case "ppp";
4420 6d5446a2 Ermal
				$ints[$ifdescr] = $ifdescr;
4421 77ccab82 Scott Ullrich
			break;
4422
			default:
4423 f6b30142 Ermal
				if (substr($ifname['if'], 0, 5) ==  "ovpnc" ||
4424
				    !empty($ifname['gateway']))
4425 6d5446a2 Ermal
					$ints[$ifdescr] = $ifdescr;
4426 77ccab82 Scott Ullrich
			break;
4427
		}
4428
	}
4429
	return $ints;
4430 52947718 Ermal Lu?i
}
4431
4432
/* return true if interface has a gateway */
4433
function interface_has_gateway($friendly) {
4434 6d5446a2 Ermal
	global $config;
4435 52947718 Ermal Lu?i
4436 6d5446a2 Ermal
	if (!empty($config['interfaces'][$friendly])) {
4437 43a22ee2 jim-p
		$ifname = &$config['interfaces'][$friendly];
4438 6d5446a2 Ermal
		switch ($ifname['ipaddr']) {
4439
			case "dhcp":
4440
			case "carpdev-dhcp":
4441
			case "pppoe":
4442
			case "pptp":
4443
			case "l2tp":
4444
			case "ppp";
4445
				return true;
4446
			break;
4447
			default:
4448 e9d7afeb Ermal
				if (substr($ifname['if'], 0, 5) ==  "ovpnc")
4449
					return true;
4450 6d5446a2 Ermal
				if (!empty($ifname['gateway']))
4451
					return true;
4452
			break;
4453
		}
4454
	}
4455 52947718 Ermal Lu?i
4456
	return false;
4457
}
4458
4459 2feb85af Seth Mos
/* return true if interface has a gateway */
4460
function interface_has_gatewayv6($friendly) {
4461
	global $config;
4462
4463
	if (!empty($config['interfaces'][$friendly])) {
4464
		$ifname = &$config['interfaces'][$friendly];
4465
		switch ($ifname['ipaddrv6']) {
4466 67102344 smos
			case "slaac":
4467 2feb85af Seth Mos
			case "dhcp6":
4468
				return true;
4469 a11a839d smos
				break;
4470
			case "6to4":
4471
				return true;
4472
				break;
4473 d500e296 smos
			case "6rd":
4474
				return true;
4475 a11a839d smos
				break;
4476 2feb85af Seth Mos
			default:
4477
				if (substr($ifname['if'], 0, 5) ==  "ovpnc")
4478
					return true;
4479
				if (!empty($ifname['gatewayv6']))
4480
					return true;
4481 a11a839d smos
				break;
4482 2feb85af Seth Mos
		}
4483
	}
4484
4485
	return false;
4486
}
4487
4488 a57b119e Bill Marquette
/****f* interfaces/is_altq_capable
4489
 * NAME
4490
 *   is_altq_capable - Test if interface is capable of using ALTQ
4491
 * INPUTS
4492
 *   $int            - string containing interface name
4493
 * RESULT
4494
 *   boolean         - true or false
4495
 ******/
4496
4497 eba938e3 Scott Ullrich
function is_altq_capable($int) {
4498 a57b119e Bill Marquette
        /* Per:
4499 64fe3233 Seth Mos
         * http://www.freebsd.org/cgi/man.cgi?query=altq&manpath=FreeBSD+7.2-current&format=html
4500 a57b119e Bill Marquette
         * Only the following drivers have ALTQ support
4501
         */
4502 c2d7074e Ermal
	$capable = array("age", "alc", "ale", "an", "ath", "aue", "awi", "bce",
4503 a5ccf623 jim-p
			"bfe", "bge", "bridge", "cas", "dc", "de", "ed", "em", "ep", "fxp", "gem",
4504 be888d7f Ermal
			"hme", "igb", "ipw", "iwi", "jme", "le", "lem", "msk", "mxge", "my", "nfe",
4505 8c62fa48 jim-p
			"npe", "nve", "ral", "re", "rl", "rum", "run", "bwn", "sf", "sis", "sk",
4506 64fe3233 Seth Mos
			"ste", "stge", "txp", "udav", "ural", "vge", "vr", "wi", "xl",
4507 febca7e8 Ermal
			"ndis", "tun", "ovpns", "ovpnc", "vlan", "pppoe", "pptp", "ng",
4508
			"l2tp", "ppp");
4509 a57b119e Bill Marquette
4510
        $int_family = preg_split("/[0-9]+/", $int);
4511
4512
        if (in_array($int_family[0], $capable))
4513
                return true;
4514 dbe67167 Ermal
	else if (stristr($int, "l2tp")) /* VLANs are name $parent_$vlan now */
4515
		return true;
4516 21699e76 Ermal
	else if (stristr($int, "vlan")) /* VLANs are name $parent_$vlan now */
4517 7e627719 Ermal
		return true;
4518 21699e76 Ermal
	else if (stristr($int, "_wlan")) /* WLANs are name $parent_$wlan now */
4519 2f3446db Ermal Lu?i
		return true;
4520 a57b119e Bill Marquette
        else
4521
                return false;
4522
}
4523
4524 52947718 Ermal Lu?i
/****f* interfaces/is_interface_wireless
4525
 * NAME
4526
 *   is_interface_wireless - Returns if an interface is wireless
4527
 * RESULT
4528
 *   $tmp       - Returns if an interface is wireless
4529
 ******/
4530
function is_interface_wireless($interface) {
4531
        global $config, $g;
4532
4533
        $friendly = convert_real_interface_to_friendly_interface_name($interface);
4534 10394059 Scott Ullrich
        if(!isset($config['interfaces'][$friendly]['wireless'])) {
4535 52947718 Ermal Lu?i
                if (preg_match($g['wireless_regex'], $interface)) {
4536 38032730 Erik Fonnesbeck
                        if (isset($config['interfaces'][$friendly]))
4537
                                $config['interfaces'][$friendly]['wireless'] = array();
4538 52947718 Ermal Lu?i
                        return true;
4539
                }
4540
                return false;
4541
        } else
4542
                return true;
4543
}
4544
4545 eba938e3 Scott Ullrich
function get_wireless_modes($interface) {
4546 d8c67d69 Scott Ullrich
	/* return wireless modes and channels */
4547 92f7d37d Ermal Luçi
	$wireless_modes = array();
4548
4549 5357f386 Erik Fonnesbeck
	$cloned_interface = get_real_interface($interface);
4550 1b773d20 Ermal Lu?i
4551 5357f386 Erik Fonnesbeck
	if($cloned_interface && is_interface_wireless($cloned_interface)) {
4552 1b773d20 Ermal Lu?i
		$chan_list = "/sbin/ifconfig {$cloned_interface} list chan";
4553
		$stack_list = "/usr/bin/awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
4554 1de74081 Ermal Lu?i
		$format_list = "/usr/bin/awk '{print \$5 \" \" \$6 \",\" \$1}'";
4555 d8c67d69 Scott Ullrich
4556 4b0e71db Scott Ullrich
		$interface_channels = "";
4557 d8c67d69 Scott Ullrich
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
4558
		$interface_channel_count = count($interface_channels);
4559
4560
		$c = 0;
4561
		while ($c < $interface_channel_count)
4562
		{
4563
			$channel_line = explode(",", $interface_channels["$c"]);
4564
			$wireless_mode = trim($channel_line[0]);
4565
			$wireless_channel = trim($channel_line[1]);
4566 4066776d Scott Ullrich
			if(trim($wireless_mode) != "") {
4567
				/* if we only have 11g also set 11b channels */
4568
				if($wireless_mode == "11g") {
4569 1ae54336 Erik Fonnesbeck
					if(!isset($wireless_modes["11b"]))
4570
						$wireless_modes["11b"] = array();
4571 39c1349c Erik Fonnesbeck
				} else if($wireless_mode == "11g ht") {
4572 1ae54336 Erik Fonnesbeck
					if(!isset($wireless_modes["11b"]))
4573
						$wireless_modes["11b"] = array();
4574
					if(!isset($wireless_modes["11g"]))
4575
						$wireless_modes["11g"] = array();
4576 39c1349c Erik Fonnesbeck
					$wireless_mode = "11ng";
4577
				} else if($wireless_mode == "11a ht") {
4578 1ae54336 Erik Fonnesbeck
					if(!isset($wireless_modes["11a"]))
4579
						$wireless_modes["11a"] = array();
4580 39c1349c Erik Fonnesbeck
					$wireless_mode = "11na";
4581 4066776d Scott Ullrich
				}
4582
				$wireless_modes["$wireless_mode"]["$c"] = $wireless_channel;
4583
			}
4584 d8c67d69 Scott Ullrich
			$c++;
4585
		}
4586
	}
4587 4066776d Scott Ullrich
	return($wireless_modes);
4588 d8c67d69 Scott Ullrich
}
4589
4590 f4094f0d Erik Fonnesbeck
/* return channel numbers, frequency, max txpower, and max regulation txpower */
4591
function get_wireless_channel_info($interface) {
4592
	$wireless_channels = array();
4593
4594 5357f386 Erik Fonnesbeck
	$cloned_interface = get_real_interface($interface);
4595 f4094f0d Erik Fonnesbeck
4596 5357f386 Erik Fonnesbeck
	if($cloned_interface && is_interface_wireless($cloned_interface)) {
4597 f4094f0d Erik Fonnesbeck
		$chan_list = "/sbin/ifconfig {$cloned_interface} list txpower";
4598
		$stack_list = "/usr/bin/awk -F\"Channel \" '{ gsub(/\\*/, \" \"); print \$2 \"\\\n\" \$3 }'";
4599
		$format_list = "/usr/bin/awk '{print \$1 \",\" \$3 \" \" \$4 \",\" \$5 \",\" \$7}'";
4600
4601
		$interface_channels = "";
4602
		exec("$chan_list | $stack_list | sort -u | $format_list 2>&1", $interface_channels);
4603
4604
		foreach ($interface_channels as $channel_line) {
4605
			$channel_line = explode(",", $channel_line);
4606
			if(!isset($wireless_channels[$channel_line[0]]))
4607
				$wireless_channels[$channel_line[0]] = $channel_line;
4608
		}
4609
	}
4610
	return($wireless_channels);
4611
}
4612
4613 52947718 Ermal Lu?i
/****f* interfaces/get_interface_mtu
4614
 * NAME
4615
 *   get_interface_mtu - Return the mtu of an interface
4616
 * RESULT
4617
 *   $tmp       - Returns the mtu of an interface
4618
 ******/
4619
function get_interface_mtu($interface) {
4620 bd96e1fe Ermal Lu?i
        $mtu = pfSense_get_interface_addresses($interface);
4621
        return $mtu['mtu'];
4622 52947718 Ermal Lu?i
}
4623
4624 eba938e3 Scott Ullrich
function get_interface_mac($interface) {
4625 7d6076f3 Ermal Lu?i
4626 3f70e618 Ermal Lu?i
	$macinfo = pfSense_get_interface_addresses($interface);
4627
	return $macinfo["macaddr"];
4628 f2ba47f8 Ermal Lu?i
}
4629
4630
/****f* pfsense-utils/generate_random_mac_address
4631
 * NAME
4632
 *   generate_random_mac - generates a random mac address
4633
 * INPUTS
4634
 *   none
4635
 * RESULT
4636
 *   $mac - a random mac address
4637
 ******/
4638
function generate_random_mac_address() {
4639
        $mac = "02";
4640
        for($x=0; $x<5; $x++)
4641
                $mac .= ":" . dechex(rand(16, 255));
4642
        return $mac;
4643 53c82ef9 Scott Ullrich
}
4644 b7ec2b9e Scott Ullrich
4645 52947718 Ermal Lu?i
/****f* interfaces/is_jumbo_capable
4646
 * NAME
4647
 *   is_jumbo_capable - Test if interface is jumbo frame capable.  Useful for determining VLAN capability.
4648
 * INPUTS
4649
 *   $int             - string containing interface name
4650
 * RESULT
4651
 *   boolean          - true or false
4652
 ******/
4653 47ee6926 Ermal
function is_jumbo_capable($iface) {
4654 52947718 Ermal Lu?i
4655 a687f866 Namezero
4656 47ee6926 Ermal
	$iface = trim($iface);
4657
	$capable = pfSense_get_interface_addresses($iface);
4658 802a40eb Ermal
	if (isset($capable['caps']['vlanmtu']))
4659 52947718 Ermal Lu?i
                return true;
4660 47ee6926 Ermal
4661 a687f866 Namezero
4662
4663
4664
4665 47ee6926 Ermal
	return false;
4666 52947718 Ermal Lu?i
}
4667
4668 5c8e8a17 gnhb
function setup_pppoe_reset_file($pppif, $iface="") {
4669 55f3ca1d gnhb
	global $g;
4670 5c8e8a17 gnhb
	$cron_file = "{$g['varetc_path']}/pppoe_restart_{$pppif}";
4671 766bd6d0 gnhb
4672 5c8e8a17 gnhb
	if(!empty($iface) && !empty($pppif)){
4673 7673cdb5 Ermal
		$cron_cmd = <<<EOD
4674
#!/bin/sh
4675
/usr/local/sbin/pfSctl -c 'interface reload {$iface}'
4676
/usr/bin/logger -t pppoe{$iface} "PPPoE periodic reset executed on {$iface}"
4677
4678
EOD;
4679
4680 766bd6d0 gnhb
		file_put_contents($cron_file, $cron_cmd);
4681
		chmod($cron_file, 0700);
4682 55f3ca1d gnhb
		sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP");
4683 a5d6f60b Ermal Lu?i
	} else
4684 766bd6d0 gnhb
		unlink_if_exists($cron_file);
4685 b7ec2b9e Scott Ullrich
}
4686
4687 56da23dc Ermal
function get_interface_default_mtu($type = "ethernet") {
4688
	switch ($type) {
4689
	case "gre":
4690
		return 1476;
4691
		break;
4692
	case "gif":
4693
		return 1280;
4694
		break;
4695
	case "tun":
4696
	case "vlan":
4697
	case "tap":
4698
	case "ethernet":
4699
	default:
4700
		return 1500;
4701
		break;
4702
	}
4703
4704
	/* Never reached */
4705
	return 1500;
4706
}
4707
4708 dd62256f Pierre POMES
function get_vip_descr($ipaddress) {
4709
	global $config;
4710
4711
	foreach ($config['virtualip']['vip'] as $vip) {
4712
		if ($vip['subnet'] == $ipaddress) {
4713
			return ($vip['descr']);
4714
		}
4715
	}
4716
	return "";
4717
}
4718
4719 d368b334 jim-p
function interfaces_staticarp_configure($if) {
4720
	global $config, $g;
4721
	if(isset($config['system']['developerspew'])) {
4722
		$mt = microtime();
4723
		echo "interfaces_staticarp_configure($if) being called $mt\n";
4724
	}
4725
4726
	$ifcfg = $config['interfaces'][$if];
4727
4728
	if (empty($if) || empty($ifcfg['if']))
4729
		return 0;
4730
4731
	/* Enable staticarp, if enabled */
4732
	if(isset($config['dhcpd'][$if]['staticarp'])) {
4733
		mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " );
4734
		mwexec("/usr/sbin/arp -d -i " . escapeshellarg($ifcfg['if']) . " -a > /dev/null 2>&1 ");
4735
		if (is_array($config['dhcpd'][$if]['staticmap'])) {
4736
4737
			foreach ($config['dhcpd'][$if]['staticmap'] as $arpent) {
4738
				mwexec("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac']));
4739
4740
			}
4741
4742
		}
4743
	} else {
4744
		mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " );
4745
		mwexec("/usr/sbin/arp -d -i " . escapeshellarg($ifcfg['if']) . " -a > /dev/null 2>&1 ");
4746
	}
4747
4748
	return 0;
4749
}
4750
4751 6a7dd9bb Ermal
?>