Project

General

Profile

Download (22.9 KB) Statistics
| Branch: | Tag: | Revision:
1 ef88e1e1 Renato Botelho
<?php
2 5b237745 Scott Ullrich
/*
3 c5d81585 Renato Botelho
 * interfaces_assign.php
4 092e7a96 Stephen Beaver
 *
5 c5d81585 Renato Botelho
 * part of pfSense (https://www.pfsense.org)
6 81299b5c Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 c5d81585 Renato Botelho
 * All rights reserved.
8 191cb31d Stephen Beaver
 *
9 c5d81585 Renato Botelho
 * originally based on m0n0wall (http://m0n0.ch/wall)
10
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
 * Written by Jim McBeath based on existing m0n0wall files
12
 * All rights reserved.
13 092e7a96 Stephen Beaver
 *
14 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
15
 * you may not use this file except in compliance with the License.
16
 * You may obtain a copy of the License at
17 092e7a96 Stephen Beaver
 *
18 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
19 092e7a96 Stephen Beaver
 *
20 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
21
 * distributed under the License is distributed on an "AS IS" BASIS,
22
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
 * See the License for the specific language governing permissions and
24
 * limitations under the License.
25 092e7a96 Stephen Beaver
 */
26 5b237745 Scott Ullrich
27 6b07c15a Matthew Grooms
##|+PRIV
28
##|*IDENT=page-interfaces-assignnetworkports
29 26b4bef8 k-paulius
##|*NAME=Interfaces: Interface Assignments
30
##|*DESCR=Allow access to the 'Interfaces: Interface Assignments' page.
31 6b07c15a Matthew Grooms
##|*MATCH=interfaces_assign.php*
32
##|-PRIV
33
34 772a7b3d loonylion
//$timealla = microtime(true);
35
36 26b4bef8 k-paulius
$pgtitle = array(gettext("Interfaces"), gettext("Interface Assignments"));
37 b32dd0a6 jim-p
$shortcut_section = "interfaces";
38 af1e2031 jim-p
39 c81ef6e2 Phil Davis
require_once("guiconfig.inc");
40
require_once("functions.inc");
41 f6339216 jim-p
require_once("filter.inc");
42 c81ef6e2 Phil Davis
require_once("shaper.inc");
43
require_once("ipsec.inc");
44
require_once("vpn.inc");
45
require_once("captiveportal.inc");
46 1b34f8a7 Ermal
require_once("rrd.inc");
47 772a7b3d loonylion
require_once("interfaces_fast.inc");
48
49
global $friendlyifnames;
50
//global $profile;
51 5b237745 Scott Ullrich
52 772a7b3d loonylion
/*moved most gettext calls to here, we really don't want to be repeatedly calling gettext() within loops if it can be avoided.*/
53
$gettextArray = array('add'=>gettext('Add'),'addif'=>gettext('Add interface'),'delete'=>gettext('Delete'),'deleteif'=>gettext('Delete interface'),'edit'=>gettext('Edit'),'on'=>gettext('on'));
54
55 5b237745 Scott Ullrich
/*
56
	In this file, "port" refers to the physical port name,
57
	while "interface" refers to LAN, WAN, or OPTn.
58
*/
59
60
/* get list without VLAN interfaces */
61
$portlist = get_interface_list();
62
63 772a7b3d loonylion
/*another *_fast function from interfaces_fast.inc. These functions are basically the same as the 
64
ones they're named after, except they (usually) take an array and (always) return an array. This means that they only
65
need to be called once per script run, the returned array contains all the data necessary for repeated use */
66
$friendlyifnames = convert_real_interface_to_friendly_interface_name_fast(array_keys($portlist));
67
68 7c53bc7b Erik Fonnesbeck
/* add wireless clone interfaces */
69
if (is_array($config['wireless']['clone']) && count($config['wireless']['clone'])) {
70
	foreach ($config['wireless']['clone'] as $clone) {
71
		$portlist[$clone['cloneif']] = $clone;
72
		$portlist[$clone['cloneif']]['iswlclone'] = true;
73
	}
74
}
75
76 5b237745 Scott Ullrich
/* add VLAN interfaces */
77
if (is_array($config['vlans']['vlan']) && count($config['vlans']['vlan'])) {
78 772a7b3d loonylion
	//$timea = microtime(true);
79 5b237745 Scott Ullrich
	foreach ($config['vlans']['vlan'] as $vlan) {
80 d59557dc Ermal Luçi
		$portlist[$vlan['vlanif']] = $vlan;
81
		$portlist[$vlan['vlanif']]['isvlan'] = true;
82 5b237745 Scott Ullrich
	}
83 772a7b3d loonylion
	/*$timeb = microtime(true);
84
	$profile['add_vlan_if'] = $timeb-$timea;*/
85 5b237745 Scott Ullrich
}
86
87 38738505 Ermal Luçi
/* add Bridge interfaces */
88 b47c94bd Ermal Luçi
if (is_array($config['bridges']['bridged']) && count($config['bridges']['bridged'])) {
89 ef88e1e1 Renato Botelho
	foreach ($config['bridges']['bridged'] as $bridge) {
90
		$portlist[$bridge['bridgeif']] = $bridge;
91
		$portlist[$bridge['bridgeif']]['isbridge'] = true;
92
	}
93 38738505 Ermal Luçi
}
94
95
/* add GIF interfaces */
96
if (is_array($config['gifs']['gif']) && count($config['gifs']['gif'])) {
97 ef88e1e1 Renato Botelho
	foreach ($config['gifs']['gif'] as $gif) {
98
		$portlist[$gif['gifif']] = $gif;
99
		$portlist[$gif['gifif']]['isgif'] = true;
100
	}
101 38738505 Ermal Luçi
}
102
103
/* add GRE interfaces */
104
if (is_array($config['gres']['gre']) && count($config['gres']['gre'])) {
105 ef88e1e1 Renato Botelho
	foreach ($config['gres']['gre'] as $gre) {
106
		$portlist[$gre['greif']] = $gre;
107
		$portlist[$gre['greif']]['isgre'] = true;
108
	}
109 38738505 Ermal Luçi
}
110
111 dbdd08af Ermal Luçi
/* add LAGG interfaces */
112 772a7b3d loonylion
if (is_array($config['laggs']['lagg']) && count($config['laggs']['lagg'])) {
113
	foreach ($config['laggs']['lagg'] as $lagg) {
114
		$portlist[$lagg['laggif']] = $lagg;
115
		$portlist[$lagg['laggif']]['islagg'] = true;
116
		/* LAGG members cannot be assigned */
117
		$lagifs = explode(',', $lagg['members']);
118
		foreach ($lagifs as $lagif) {
119
			if (isset($portlist[$lagif])) {
120
				unset($portlist[$lagif]);
121
			}
122 2af86dda Phil Davis
		}
123 ef88e1e1 Renato Botelho
	}
124 c720925c Ermal Luçi
}
125
126 265188ea Ermal Lu?i
/* add QinQ interfaces */
127
if (is_array($config['qinqs']['qinqentry']) && count($config['qinqs']['qinqentry'])) {
128 ef88e1e1 Renato Botelho
	foreach ($config['qinqs']['qinqentry'] as $qinq) {
129 1322ee22 Chris Rowe
		$portlist["{$qinq['vlanif']}"]['descr'] = "VLAN {$qinq['tag']} on {$qinq['if']}";
130
		$portlist["{$qinq['vlanif']}"]['isqinq'] = true;
131 ef88e1e1 Renato Botelho
		/* QinQ members */
132
		$qinqifs = explode(' ', $qinq['members']);
133
		foreach ($qinqifs as $qinqif) {
134 772a7b3d loonylion
			$portlist["{$qinq['vlanif']}_{$qinqif}"]['descr'] = "QinQ {$qinqif} on VLAN {$qinq['tag']} on {$qinq['if']}";
135
			$portlist["{$qinq['vlanif']}_{$qinqif}"]['isqinq'] = true;
136 ef88e1e1 Renato Botelho
		}
137
	}
138 265188ea Ermal Lu?i
}
139 c720925c Ermal Luçi
140 860c4e80 Chris Buechler
/* add PPP interfaces */
141
if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
142 611ae852 Ermal
	foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
143 99c26d44 gnhb
		$portname = $ppp['if'];
144 860c4e80 Chris Buechler
		$portlist[$portname] = $ppp;
145
		$portlist[$portname]['isppp'] = true;
146 3f2ef8d7 gnhb
		$ports_base = basename($ppp['ports']);
147 2af86dda Phil Davis
		if (isset($ppp['descr'])) {
148 99c26d44 gnhb
			$portlist[$portname]['descr'] = strtoupper($ppp['if']). "({$ports_base}) - {$ppp['descr']}";
149 2af86dda Phil Davis
		} else if (isset($ppp['username'])) {
150 99c26d44 gnhb
			$portlist[$portname]['descr'] = strtoupper($ppp['if']). "({$ports_base}) - {$ppp['username']}";
151 2af86dda Phil Davis
		} else {
152 99c26d44 gnhb
			$portlist[$portname]['descr'] = strtoupper($ppp['if']). "({$ports_base})";
153 2af86dda Phil Davis
		}
154 860c4e80 Chris Buechler
	}
155
}
156
157 144fbff2 jim-p
$ovpn_descrs = array();
158
if (is_array($config['openvpn'])) {
159 2af86dda Phil Davis
	if (is_array($config['openvpn']['openvpn-server'])) {
160
		foreach ($config['openvpn']['openvpn-server'] as $s) {
161 bfa7b33e doktornotor
			$portname = "ovpns{$s['vpnid']}";
162
			$portlist[$portname] = $s;
163 144fbff2 jim-p
			$ovpn_descrs[$s['vpnid']] = $s['description'];
164 2af86dda Phil Davis
		}
165
	}
166
	if (is_array($config['openvpn']['openvpn-client'])) {
167
		foreach ($config['openvpn']['openvpn-client'] as $c) {
168 bfa7b33e doktornotor
			$portname = "ovpnc{$c['vpnid']}";
169
			$portlist[$portname] = $c;
170 144fbff2 jim-p
			$ovpn_descrs[$c['vpnid']] = $c['description'];
171 2af86dda Phil Davis
		}
172
	}
173 144fbff2 jim-p
}
174
175 772a7b3d loonylion
//$timea = microtime(true);
176
$ifdescrs = interface_assign_description_fast($portlist,$friendlyifnames);
177
/*$timeb = microtime(true);
178
$profile['build_if_descrs'] = $timeb - $timea;*/
179
180 4401107f Steve Beaver
if (isset($_REQUEST['add']) && isset($_REQUEST['if_add'])) {
181 7c611a3e Renato Botelho
	/* Be sure this port is not being used */
182
	$portused = false;
183 772a7b3d loonylion
	//$timea = microtime(true);
184 7c611a3e Renato Botelho
	foreach ($config['interfaces'] as $ifname => $ifdata) {
185 4401107f Steve Beaver
		if ($ifdata['if'] == $_REQUEST['if_add']) {
186 7c611a3e Renato Botelho
			$portused = true;
187
			break;
188
		}
189
	}
190 772a7b3d loonylion
	/*$timeb = microtime(true);
191
	$profile['if_add_portused'] = $timeb-$timea;*/
192 7c611a3e Renato Botelho
193
	if ($portused === false) {
194
		/* find next free optional interface number */
195 2af86dda Phil Davis
		if (!$config['interfaces']['lan']) {
196 7c611a3e Renato Botelho
			$newifname = gettext("lan");
197
			$descr = gettext("LAN");
198
		} else {
199 772a7b3d loonylion
			/*get first available OPT interface number. This code scales better than the foreach it replaces. 
200
			* might not work if theres ifs other than 'wan','lan' and 'optx';
201
			* The performance increase isn't substantial over the foreach; however as the number of OPT interfaces
202
			* increases, so does the performance gain; from ~0.0003s improvement with 100 VLANs to ~0.0009s with 400.
203
			* It is, however, marginally slower (~0.000036s at 50 VLANS) than the foreach with less than 100 VLANs, and 
204
			* therefore may not be worth the loss of code readability or performance for the majority of use cases. */
205
			//$timea = microtime(true);
206
			$step1 = array_keys($config['interfaces']);
207
			unset($step1['lan'],$step1['wan']);
208
			$step2 = str_replace("opt","",$step1);
209
			$step3 = array_fill(0,end($step2),'x');
210
			$step4 = array_flip($step2);
211
			$step5 = array_replace($step3,$step2);
212
			$step6 = array_unique($step5);
213
			$step7 = array_flip($step6);
214
			if (isset($step7['x']))
215
				$i = $step7['x'];
216
			else
217
				$i = count($config['interfaces'])-1;
218
219 7c611a3e Renato Botelho
			$newifname = 'opt' . $i;
220
			$descr = "OPT" . $i;
221
		}
222 772a7b3d loonylion
		/*$timeb = microtime(true);
223
		$profile['if_add_get_free_opt'] = $timeb-$timea;*/
224 7c611a3e Renato Botelho
225
		$config['interfaces'][$newifname] = array();
226
		$config['interfaces'][$newifname]['descr'] = $descr;
227
		$config['interfaces'][$newifname]['if'] = $_POST['if_add'];
228
		if (preg_match($g['wireless_regex'], $_POST['if_add'])) {
229
			$config['interfaces'][$newifname]['wireless'] = array();
230
			interface_sync_wireless_clones($config['interfaces'][$newifname], false);
231
		}
232
233 772a7b3d loonylion
		//$timea = microtime(true);
234 7c611a3e Renato Botelho
		uksort($config['interfaces'], "compare_interface_friendly_names");
235 772a7b3d loonylion
		/*$timeb = microtime(true);
236
		$profile['if_add_uksort'] = $timeb-$timea;*/
237 7c611a3e Renato Botelho
238
		/* XXX: Do not remove this. */
239
		unlink_if_exists("{$g['tmp_path']}/config.cache");
240
241 772a7b3d loonylion
		//$timea = microtime(true);
242 7c611a3e Renato Botelho
		write_config();
243 772a7b3d loonylion
		/*$timeb = microtime(true);
244
		$profile['if_add_write_config'] = $timeb-$timea;*/
245 7c611a3e Renato Botelho
246 44c42356 Phil Davis
		$action_msg = gettext("Interface has been added.");
247 c8532336 Phil Davis
		$class = "success";
248 7c611a3e Renato Botelho
	}
249
250
} else if (isset($_POST['apply'])) {
251 ca4acbcd Scott Ullrich
	if (file_exists("/var/run/interface_mismatch_reboot_needed")) {
252 eef52225 jim-p
		system_reboot();
253 ca4acbcd Scott Ullrich
		$rebootingnow = true;
254
	} else {
255 d59557dc Ermal Luçi
		write_config();
256
257 44c42356 Phil Davis
		$changes_applied = true;
258
		$retval = 0;
259
		$retval |= filter_configure();
260 d59557dc Ermal Luçi
	}
261
262 7c611a3e Renato Botelho
} else if (isset($_POST['Submit'])) {
263 5b237745 Scott Ullrich
264
	unset($input_errors);
265
266
	/* input validation */
267
268
	/* Build a list of the port names so we can see how the interfaces map */
269
	$portifmap = array();
270 772a7b3d loonylion
	//$timea = microtime(true);
271 2af86dda Phil Davis
	foreach ($portlist as $portname => $portinfo) {
272 5b237745 Scott Ullrich
		$portifmap[$portname] = array();
273 2af86dda Phil Davis
	}
274 772a7b3d loonylion
	/*$timeb = microtime(true);
275
	$profile['post_list_port_names'] = $timeb - $timea;*/
276 5b237745 Scott Ullrich
277
	/* Go through the list of ports selected by the user,
278 ef88e1e1 Renato Botelho
	build a list of port-to-interface mappings in portifmap */
279 772a7b3d loonylion
	//$timea = microtime(true);
280 5b237745 Scott Ullrich
	foreach ($_POST as $ifname => $ifport) {
281 2af86dda Phil Davis
		if (($ifname == 'lan') || ($ifname == 'wan') || (substr($ifname, 0, 3) == 'opt')) {
282 5b237745 Scott Ullrich
			$portifmap[$ifport][] = strtoupper($ifname);
283 2af86dda Phil Davis
		}
284 5b237745 Scott Ullrich
	}
285 772a7b3d loonylion
	/*$timeb = microtime(true);
286
	$profile['post_build_port_if_map']=$timeb-$timea;*/
287 5b237745 Scott Ullrich
288
	/* Deliver error message for any port with more than one assignment */
289 772a7b3d loonylion
	//$timea = microtime(true);
290 5b237745 Scott Ullrich
	foreach ($portifmap as $portname => $ifnames) {
291
		if (count($ifnames) > 1) {
292 ddc55e12 Erik Fonnesbeck
			$errstr = sprintf(gettext('Port %1$s '.
293
				' was assigned to %2$s' .
294
				' interfaces:'), $portname, count($ifnames));
295 ef88e1e1 Renato Botelho
296 772a7b3d loonylion
			//$timea2 = microtime(true);
297 2af86dda Phil Davis
			foreach ($portifmap[$portname] as $ifn) {
298 f0eef2ef Phil Davis
				$errstr .= " " . convert_friendly_interface_to_friendly_descr(strtolower($ifn)) . " (" . $ifn . ")";
299 2af86dda Phil Davis
			}
300 772a7b3d loonylion
			/*$timeb2 = microtime(true);
301
			if ($timeb2-$timea2 > $profile['post_error_multiple_assign_convert_friendly_if_friendly_desc'])
302
				$profile['post_error_multiple_assign_convert_friendly_if_friendly_desc'] = $timeb2 - $timea2;*/
303 ef88e1e1 Renato Botelho
304 5b237745 Scott Ullrich
			$input_errors[] = $errstr;
305 95540233 Renato Botelho
		} else if (count($ifnames) == 1 && preg_match('/^bridge[0-9]/', $portname) && is_array($config['bridges']['bridged']) && count($config['bridges']['bridged'])) {
306
			foreach ($config['bridges']['bridged'] as $bridge) {
307 2af86dda Phil Davis
				if ($bridge['bridgeif'] != $portname) {
308 95540233 Renato Botelho
					continue;
309 2af86dda Phil Davis
				}
310 95540233 Renato Botelho
311
				$members = explode(",", strtoupper($bridge['members']));
312
				foreach ($members as $member) {
313
					if ($member == $ifnames[0]) {
314 45654aa0 NOYB
						$input_errors[] = sprintf(gettext('Cannot set port %1$s to interface %2$s because this interface is a member of %3$s.'), $portname, $member, $portname);
315 95540233 Renato Botelho
						break;
316
					}
317
				}
318
			}
319 5b237745 Scott Ullrich
		}
320
	}
321 772a7b3d loonylion
	/*$timeb = $microtime(true);
322
	$profile['post_error_multiple_assign'] = $timeb-$timea;*/
323 5b237745 Scott Ullrich
324 66bcba1b Ermal
	if (is_array($config['vlans']['vlan'])) {
325 772a7b3d loonylion
		//$timea = microtime(true);
326 66bcba1b Ermal
		foreach ($config['vlans']['vlan'] as $vlan) {
327 2af86dda Phil Davis
			if (does_interface_exist($vlan['if']) == false) {
328 0fc3de67 Phil Davis
				$input_errors[] = sprintf(gettext('Vlan parent interface %1$s does not exist anymore so vlan id %2$s cannot be created please fix the issue before continuing.'), $vlan['if'], $vlan['tag']);
329 2af86dda Phil Davis
			}
330 66bcba1b Ermal
		}
331 772a7b3d loonylion
		/*$timeb = microtime(true);
332
		$profile['post_error_vlan_parent_not_exist'] = $timeb - $timea;*/
333 66bcba1b Ermal
	}
334 5b237745 Scott Ullrich
335
	if (!$input_errors) {
336
		/* No errors detected, so update the config */
337 772a7b3d loonylion
		//$timea = microtime(true);
338 5b237745 Scott Ullrich
		foreach ($_POST as $ifname => $ifport) {
339 ef88e1e1 Renato Botelho
340
			if (($ifname == 'lan') || ($ifname == 'wan') || (substr($ifname, 0, 3) == 'opt')) {
341
342 5b237745 Scott Ullrich
				if (!is_array($ifport)) {
343 e7bfa231 Ermal Lu?i
					$reloadif = false;
344 44088ce8 Ermal Lu?i
					if (!empty($config['interfaces'][$ifname]['if']) && $config['interfaces'][$ifname]['if'] <> $ifport) {
345
						interface_bring_down($ifname);
346 e7bfa231 Ermal Lu?i
						/* Mark this to be reconfigured in any case. */
347
						$reloadif = true;
348 44088ce8 Ermal Lu?i
					}
349 5b237745 Scott Ullrich
					$config['interfaces'][$ifname]['if'] = $ifport;
350 2af86dda Phil Davis
					if (isset($portlist[$ifport]['isppp'])) {
351 c86e8f76 gnhb
						$config['interfaces'][$ifname]['ipaddr'] = $portlist[$ifport]['type'];
352 2af86dda Phil Davis
					}
353 ef88e1e1 Renato Botelho
354 0b5fc1d1 Ermal
					if (substr($ifport, 0, 3) == 'gre' || substr($ifport, 0, 3) == 'gif') {
355 f3d88511 Renato Botelho
						unset($config['interfaces'][$ifname]['ipaddr']);
356
						unset($config['interfaces'][$ifname]['subnet']);
357
						unset($config['interfaces'][$ifname]['ipaddrv6']);
358
						unset($config['interfaces'][$ifname]['subnetv6']);
359
					}
360
361 5b237745 Scott Ullrich
					/* check for wireless interfaces, set or clear ['wireless'] */
362 94a77286 Scott Ullrich
					if (preg_match($g['wireless_regex'], $ifport)) {
363 2af86dda Phil Davis
						if (!is_array($config['interfaces'][$ifname]['wireless'])) {
364 5b237745 Scott Ullrich
							$config['interfaces'][$ifname]['wireless'] = array();
365 2af86dda Phil Davis
						}
366 5b237745 Scott Ullrich
					} else {
367
						unset($config['interfaces'][$ifname]['wireless']);
368
					}
369 ef88e1e1 Renato Botelho
370 d59557dc Ermal Luçi
					/* make sure there is a descr for all interfaces */
371 2af86dda Phil Davis
					if (!isset($config['interfaces'][$ifname]['descr'])) {
372 d59557dc Ermal Luçi
						$config['interfaces'][$ifname]['descr'] = strtoupper($ifname);
373 2af86dda Phil Davis
					}
374 ef88e1e1 Renato Botelho
375 b13efb03 Erik Fonnesbeck
					if ($reloadif == true) {
376 2af86dda Phil Davis
						if (preg_match($g['wireless_regex'], $ifport)) {
377 b13efb03 Erik Fonnesbeck
							interface_sync_wireless_clones($config['interfaces'][$ifname], false);
378 2af86dda Phil Davis
						}
379 e7bfa231 Ermal Lu?i
						/* Reload all for the interface. */
380
						interface_configure($ifname, true);
381 b13efb03 Erik Fonnesbeck
					}
382 5b237745 Scott Ullrich
				}
383
			}
384
		}
385 772a7b3d loonylion
		/*$timeb = microtime(true);
386
		$profile['post_build_config'] = $timeb-$timea;
387 ef88e1e1 Renato Botelho
388 772a7b3d loonylion
		$timea = microtime(true);*/
389 5b237745 Scott Ullrich
		write_config();
390 772a7b3d loonylion
		/*$timeb = microtime(true);
391
		$profile['post_write_config']=$timeb-$timea;*/
392 ef88e1e1 Renato Botelho
393 6e6233d0 sullrich
		enable_rrd_graphing();
394 5b237745 Scott Ullrich
	}
395 7c611a3e Renato Botelho
} else {
396
	unset($delbtn);
397 aa82505e Phil Davis
	if (!empty($_POST['del'])) {
398 41ea4cf3 Sjon Hortensius
		$delbtn = key($_POST['del']);
399 aa82505e Phil Davis
	}
400 5b237745 Scott Ullrich
401 7c611a3e Renato Botelho
	if (isset($delbtn)) {
402
		$id = $delbtn;
403
404 2af86dda Phil Davis
		if (link_interface_to_group($id)) {
405 7c611a3e Renato Botelho
			$input_errors[] = gettext("The interface is part of a group. Please remove it from the group to continue");
406 2af86dda Phil Davis
		} else if (link_interface_to_bridge($id)) {
407 7c611a3e Renato Botelho
			$input_errors[] = gettext("The interface is part of a bridge. Please remove it from the bridge to continue");
408 2af86dda Phil Davis
		} else if (link_interface_to_gre($id)) {
409 7c611a3e Renato Botelho
			$input_errors[] = gettext("The interface is part of a gre tunnel. Please delete the tunnel to continue");
410 2af86dda Phil Davis
		} else if (link_interface_to_gif($id)) {
411 7c611a3e Renato Botelho
			$input_errors[] = gettext("The interface is part of a gif tunnel. Please delete the tunnel to continue");
412 85ea9d46 Steve Beaver
		} else if (interface_has_queue($id)) {
413 9267c6c9 Steve Beaver
			$input_errors[] = gettext("The interface has a traffic shaper queue configured.\nPlease remove all queues on the interface to continue.");
414 2af86dda Phil Davis
		} else {
415 7c611a3e Renato Botelho
			unset($config['interfaces'][$id]['enable']);
416 772a7b3d loonylion
			//$timea = microtime(true);
417 7c611a3e Renato Botelho
			$realid = get_real_interface($id);
418 772a7b3d loonylion
			/*$timeb = microtime(true);
419
			$profile['del_if_get_real_if'] = $timeb-$timea;*/
420 7c611a3e Renato Botelho
			interface_bring_down($id);   /* down the interface */
421
422
			unset($config['interfaces'][$id]);	/* delete the specified OPTn or LAN*/
423
424
			if (is_array($config['dhcpd']) && is_array($config['dhcpd'][$id])) {
425
				unset($config['dhcpd'][$id]);
426 6756052b NewEraCracker
				services_dhcpd_configure('inet');
427 7c611a3e Renato Botelho
			}
428 c3bc7432 Ermal Luçi
429 ff3f0016 Chris Buechler
			if (is_array($config['dhcpdv6']) && is_array($config['dhcpdv6'][$id])) {
430
				unset($config['dhcpdv6'][$id]);
431 6756052b NewEraCracker
				services_dhcpd_configure('inet6');
432 ff3f0016 Chris Buechler
			}
433
434 7c611a3e Renato Botelho
			if (count($config['filter']['rule']) > 0) {
435
				foreach ($config['filter']['rule'] as $x => $rule) {
436 2af86dda Phil Davis
					if ($rule['interface'] == $id) {
437 7c611a3e Renato Botelho
						unset($config['filter']['rule'][$x]);
438 2af86dda Phil Davis
					}
439 7c611a3e Renato Botelho
				}
440 e27d0494 Ermal
			}
441 7c611a3e Renato Botelho
			if (is_array($config['nat']['rule']) && count($config['nat']['rule']) > 0) {
442
				foreach ($config['nat']['rule'] as $x => $rule) {
443 2af86dda Phil Davis
					if ($rule['interface'] == $id) {
444 7c611a3e Renato Botelho
						unset($config['nat']['rule'][$x]['interface']);
445 2af86dda Phil Davis
					}
446 7c611a3e Renato Botelho
				}
447 e27d0494 Ermal
			}
448 c3bc7432 Ermal Luçi
449 772a7b3d loonylion
			//$timea = microtime(true);
450 7c611a3e Renato Botelho
			write_config();
451 772a7b3d loonylion
			/*$timeb = microtime(true);
452
			$profile['del_if_write_config']=$timeb-$timea;
453 ef88e1e1 Renato Botelho
454 7c611a3e Renato Botelho
			/* If we are in firewall/routing mode (not single interface)
455
			 * then ensure that we are not running DHCP on the wan which
456
			 * will make a lot of ISP's unhappy.
457
			 */
458 2af86dda Phil Davis
			if ($config['interfaces']['lan'] && $config['dhcpd']['wan']) {
459 7c611a3e Renato Botelho
				unset($config['dhcpd']['wan']);
460
			}
461 7850de1c Ermal Lu?i
462 772a7b3d loonylion
			//$timea = microtime(true);
463 7c611a3e Renato Botelho
			link_interface_to_vlans($realid, "update");
464 772a7b3d loonylion
			/*$timeb = microtime(true);
465
			$profile['del_if_link_if_vlans'] = $timeb-$timea;*/
466 ef88e1e1 Renato Botelho
467 44c42356 Phil Davis
			$action_msg = gettext("Interface has been deleted.");
468 c8532336 Phil Davis
			$class = "success";
469 8a648100 Ermal Luçi
		}
470 bda86e8f Scott Ullrich
	}
471 7c611a3e Renato Botelho
}
472 cec4323f Erik Fonnesbeck
473 7c611a3e Renato Botelho
/* Create a list of unused ports */
474
$unused_portlist = array();
475 772a7b3d loonylion
//$timea = microtime(true);
476
$portArray = array_keys($portlist);
477
478
/*  this code scales much much better 
479
0.0065770149230957 seconds
480
vs
481
0.49271988868713 seconds with 400 vlans*/
482
483
	$ifaceArray = array_column($config['interfaces'],'if');
484
	$unused = array_diff($portArray,$ifaceArray);
485
	$unused = array_flip($unused);
486
	$unused_portlist = array_intersect_key($portlist,$unused);//*/
487
	unset($unused,$portArray,$ifaceArray);
488 529fbbfd loonylion
489 772a7b3d loonylion
/*$timeb = microtime(true);
490
$profile['build_unused_port_list'] = $timeb-$timea;*/
491 5b237745 Scott Ullrich
492 7f43ca88 Scott Ullrich
include("head.inc");
493
494 2af86dda Phil Davis
if (file_exists("/var/run/interface_mismatch_reboot_needed")) {
495 ca4acbcd Scott Ullrich
	if ($_POST) {
496 2af86dda Phil Davis
		if ($rebootingnow) {
497 44c42356 Phil Davis
			$action_msg = gettext("The system is now rebooting. Please wait.");
498 c8532336 Phil Davis
			$class = "success";
499 2af86dda Phil Davis
		} else {
500 21c18c3d Phil Davis
			$applymsg = gettext("Reboot is needed. Please apply the settings in order to reboot.");
501 c8532336 Phil Davis
			$class = "warning";
502 2af86dda Phil Davis
		}
503 ca4acbcd Scott Ullrich
	} else {
504 44c42356 Phil Davis
		$action_msg = gettext("Interface mismatch detected. Please resolve the mismatch, save and then click 'Apply Changes'. The firewall will reboot afterwards.");
505 c8532336 Phil Davis
		$class = "warning";
506 ca4acbcd Scott Ullrich
	}
507 41ea4cf3 Sjon Hortensius
}
508 f2cb6a9f Scott Ullrich
509 1b0e073e Renato Botelho
if (file_exists("/tmp/reload_interfaces")) {
510
	echo "<p>\n";
511 45654aa0 NOYB
	print_apply_box(gettext("The interface configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
512 1b0e073e Renato Botelho
	echo "<br /></p>\n";
513 21c18c3d Phil Davis
} elseif ($applymsg) {
514
	print_apply_box($applymsg);
515 44c42356 Phil Davis
} elseif ($action_msg) {
516
	print_info_box($action_msg, $class);
517
} elseif ($changes_applied) {
518
	print_apply_result_box($retval);
519 2af86dda Phil Davis
}
520 1b0e073e Renato Botelho
521
pfSense_handle_custom_code("/usr/local/pkg/interfaces_assign/pre_input_errors");
522 a4af095c Renato Botelho
523 aa82505e Phil Davis
if ($input_errors) {
524 1b0e073e Renato Botelho
	print_input_errors($input_errors);
525 aa82505e Phil Davis
}
526 f2cb6a9f Scott Ullrich
527 41ea4cf3 Sjon Hortensius
$tab_array = array();
528 26b4bef8 k-paulius
$tab_array[] = array(gettext("Interface Assignments"), true, "interfaces_assign.php");
529 50e6c063 Renato Botelho
$tab_array[] = array(gettext("Interface Groups"), false, "interfaces_groups.php");
530
$tab_array[] = array(gettext("Wireless"), false, "interfaces_wireless.php");
531
$tab_array[] = array(gettext("VLANs"), false, "interfaces_vlan.php");
532
$tab_array[] = array(gettext("QinQs"), false, "interfaces_qinq.php");
533
$tab_array[] = array(gettext("PPPs"), false, "interfaces_ppps.php");
534 26b4bef8 k-paulius
$tab_array[] = array(gettext("GREs"), false, "interfaces_gre.php");
535
$tab_array[] = array(gettext("GIFs"), false, "interfaces_gif.php");
536 50e6c063 Renato Botelho
$tab_array[] = array(gettext("Bridges"), false, "interfaces_bridge.php");
537 26b4bef8 k-paulius
$tab_array[] = array(gettext("LAGGs"), false, "interfaces_lagg.php");
538 41ea4cf3 Sjon Hortensius
display_top_tabs($tab_array);
539 772a7b3d loonylion
//$timea = microtime(true);
540 529fbbfd loonylion
541
/*generate the port select box only once. 
542
Not indenting the HTML and keeping each option to one line in
543
this function results in HTML code that is 8.9KB smaller with 500
544
VLANS than the original code structure. Multiplied by 500 VLANs 
545
this means the page is ~4.5MB smaller and takes over 1s less to 
546
transmit and also renders significantly faster in the browser.
547
This is too much of an improvement to ignore. Tested with 500
548
VLANS, total page output is 17,197.33KB with 8456ms spent 
549
waiting for page generation and transmission, compared to 
550
21,697.89KB with a wait of 9734ms*/
551
552
//$timea2 = microtime();
553
$portselect='';
554
foreach ($portlist as $portname => $portinfo) {
555
	$portselect.='<option value="'.$portname.'"'; 
556 68eacaa2 loonylion
	/*if($portname == $iface['if']) {
557 529fbbfd loonylion
		$portselect.=' selected';
558 68eacaa2 loonylion
	}*/
559 529fbbfd loonylion
	$portselect.=">".$ifdescrs[$portname]."</option>\n";
560
}
561
/*$timeb2 = microtime();
562
$profile['html_generate_port_select'];
563
$profile['html_generated_port_select'] = strlen($portselect);*/
564
565 1b0e073e Renato Botelho
?>
566 41ea4cf3 Sjon Hortensius
<form action="interfaces_assign.php" method="post">
567 89f64f0f Sander van Leeuwen
	<div class="table-responsive">
568
	<table class="table table-striped table-hover">
569 41ea4cf3 Sjon Hortensius
	<thead>
570
		<tr>
571
			<th><?=gettext("Interface")?></th>
572
			<th><?=gettext("Network port")?></th>
573 290566ed NOYB
			<th>&nbsp;</th>
574 41ea4cf3 Sjon Hortensius
		</tr>
575
	</thead>
576
	<tbody>
577 1b0e073e Renato Botelho
<?php
578 772a7b3d loonylion
	//$timea2 = microtime(true);
579 68eacaa2 loonylion
	$i=0;
580 41ea4cf3 Sjon Hortensius
	foreach ($config['interfaces'] as $ifname => $iface):
581 aa82505e Phil Davis
		if ($iface['descr']) {
582 41ea4cf3 Sjon Hortensius
			$ifdescr = $iface['descr'];
583 aa82505e Phil Davis
		} else {
584 41ea4cf3 Sjon Hortensius
			$ifdescr = strtoupper($ifname);
585 aa82505e Phil Davis
		}
586 7c611a3e Renato Botelho
?>
587 41ea4cf3 Sjon Hortensius
		<tr>
588 4401107f Steve Beaver
			<td><a href="/interfaces.php?if=<?=$ifname?>"><?=$ifdescr?></a></td>
589 41ea4cf3 Sjon Hortensius
			<td>
590
				<select name="<?=$ifname?>" id="<?=$ifname?>" class="form-control">
591 772a7b3d loonylion
<?php //$timea3 = microtime(true);
592 529fbbfd loonylion
/* replacing the port select menu generation loop that has count(interfaces) iterations 
593
and is run count(interfaces) times with a pre-prepared select menu generated outside of
594
this loop has produced a significant improvement in page generation and load time */
595 68eacaa2 loonylion
//foreach ($portlist as $portname => $portinfo):
596
echo str_replace('value="'.$iface['if'].'">','value="'.$iface['if'].'" selected>',$portselect);
597
 //endforeach;
598 772a7b3d loonylion
/*$timeb3 = microtime(true);
599
$profile['html_if_assign_desc'] = $timeb3-$timea3;*/
600
?>
601 41ea4cf3 Sjon Hortensius
				</select>
602
			</td>
603
			<td>
604
<?php if ($ifname != 'wan'):?>
605 772a7b3d loonylion
				<button type="submit" name="del[<?=$ifname?>]" class="btn btn-danger btn-sm" title="<?=$gettextArray['deleteif']?>">
606 4419e274 Stephen Beaver
					<i class="fa fa-trash icon-embed-btn"></i>
607 772a7b3d loonylion
					<?=$gettextArray["delete"]?>
608 4419e274 Stephen Beaver
				</button>
609 41ea4cf3 Sjon Hortensius
<?php endif;?>
610
			</td>
611
		</tr>
612 68eacaa2 loonylion
<?php $i++; 
613
endforeach;
614 772a7b3d loonylion
/*$timeb2 = microtime(true);
615
$profile['html_display_ifs'] = $timeb2-$timea2;*/
616 41ea4cf3 Sjon Hortensius
	if (count($config['interfaces']) < count($portlist)):
617 1b0e073e Renato Botelho
?>
618 41ea4cf3 Sjon Hortensius
		<tr>
619
			<th>
620
				<?=gettext("Available network ports:")?>
621
			</th>
622
			<td>
623
				<select name="if_add" id="if_add" class="form-control">
624 772a7b3d loonylion
<?php //$timea2 = microtime(true);
625 529fbbfd loonylion
/* As with the gettext() calls, I've removed the interface_assign_description() calls and
626
replaced them with my own interface_assign_description_fast() function that's called once
627
outside of the loop. Also like the gettext() edits, this change keeps paying for itself.
628
 Also, removing the indents and newlines saves potentially ~9KB of HTML with 500 unassigned VLANs */
629 772a7b3d loonylion
foreach ($unused_portlist as $portname => $portinfo):?>
630 529fbbfd loonylion
<option value="<?=$portname?>" <?=($portname == $iface['if']) ? ' selected': ''?>><?=$ifdescrs[$portname]?></option>
631 772a7b3d loonylion
<?php endforeach;
632
/*$timeb2 = microtime(true);
633
$profile['html_available_ports_list'] = $timeb2-$timea2;*/
634
?>
635 41ea4cf3 Sjon Hortensius
				</select>
636
			</td>
637
			<td>
638 4419e274 Stephen Beaver
				<button type="submit" name="add" title="<?=gettext("Add selected interface")?>" value="add interface" class="btn btn-success btn-sm" >
639
					<i class="fa fa-plus icon-embed-btn"></i>
640 772a7b3d loonylion
					<?=$gettextArray["add"]?>
641 4419e274 Stephen Beaver
				</button>
642 41ea4cf3 Sjon Hortensius
			</td>
643
		</tr>
644
<?php endif;?>
645
		</tbody>
646
	</table>
647 89f64f0f Sander van Leeuwen
	</div>
648 41ea4cf3 Sjon Hortensius
649 27d6a45b jim-p
	<button name="Submit" type="submit" class="btn btn-primary" value="<?=gettext('Save')?>"><i class="fa fa-save icon-embed-btn"></i><?=gettext('Save')?></button>
650 5b237745 Scott Ullrich
</form>
651 4419e274 Stephen Beaver
<br />
652 7c945f74 k-paulius
653
<?php
654 772a7b3d loonylion
/*$timeb = microtime(true);
655
$profile['html'] = $timeb-$timea;*/
656 481db4fe jim-p
print_info_box(gettext("Interfaces that are configured as members of a lagg(4) interface will not be shown.") .
657
    '<br/><br/>' .
658
    gettext("Wireless interfaces must be created on the Wireless tab before they can be assigned."), 'info', false);
659 772a7b3d loonylion
/*$timeallb = microtime(true);
660
$profile['total'] = $timeallb - $timealla;
661 529fbbfd loonylion
print_r($profile);*/
662 7c945f74 k-paulius
?>
663 89f64f0f Sander van Leeuwen
664 bfa7b33e doktornotor
<?php include("foot.inc")?>