Project

General

Profile

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