Project

General

Profile

Download (8.79 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * interfaces_fast.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2017 Peter Schofield (parts of this file)
10
 * All rights reserved.
11
 *
12
 * originally based on m0n0wall (http://m0n0.ch/wall)
13
 * Copyright (c) 2004 Manuel Kasper <mk@neon1.net>.
14
 * All rights reserved.
15
 *
16
 * Licensed under the Apache License, Version 2.0 (the "License");
17
 * you may not use this file except in compliance with the License.
18
 * You may obtain a copy of the License at
19
 *
20
 * http://www.apache.org/licenses/LICENSE-2.0
21
 *
22
 * Unless required by applicable law or agreed to in writing, software
23
 * distributed under the License is distributed on an "AS IS" BASIS,
24
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25
 * See the License for the specific language governing permissions and
26
 * limitations under the License.
27
 *
28
 *
29
 * This file contains rewrites of several functions, from both interfaces.inc
30
 * and interfaces_assign.php. The general purpose of these rewrites is not
31
 * necessarily to be faster in and of themselves, though they may be, but to
32
 * replace functions called multiple times with a function that's called only
33
 * once. This results in a significant speedup because there are far fewer
34
 * function calls, fewer loops and suchlike. It does, however, increase memory
35
 * usage somewhat, but it shouldn't be significant in the grand scheme of things.
36
 *
37
 *
38
 * Functions in this file may not require/use parameters when called, however
39
 * in most cases they will accept parameters as per their original forms, for
40
 * consistency.
41

    
42
 */
43
require_once('interfaces.inc');
44
/*
45
* does_interface_exist_fast
46
Returns an array of interfaces which exist on the system.
47
The $interface parameter is not used, but is accepted for
48
consistency with the function it replaces.
49
*/
50
function does_interface_exist_fast($interface='', $flush = true) {
51
	global $config;
52

    
53
	$ints = get_interface_arr($flush);
54
	return array_flip($ints);
55
}
56

    
57
/*
58
 * convert_real_interface_to_friendly_interface_name_fast($interface): convert fxp0 -> wan, etc.
59
 Returns an array of interfaces and friendly names.
60
 */
61
function convert_real_interface_to_friendly_interface_name_fast() {
62
	global $config;
63

    
64
	$out = array();
65
	/* XXX: For speed reasons reference directly the interface array */
66
	init_config_arr(array('interfaces'));
67
	$ifdescrs = &$config['interfaces'];
68
	$iffriendlynames = array_keys($ifdescrs);
69
	$out = array_flip(get_real_interface_fast($iffriendlynames));
70
	return $out;
71
}
72

    
73
/*
74
 * get_real_interface_fast($interfaces, ...)
75
 * Exactly the same as it's namesake, except it takes an array of interfaces and returns an array
76
 *
77
 */
78

    
79
function get_real_interface_fast($interfaces = array(), $family = "all", $realv6iface = false, $flush = true) {
80
	global $config, $g;
81

    
82
	$existing_ifs = does_interface_exist_fast();
83

    
84
	$out = array();
85
	foreach ($interfaces as $interface) {
86
		$wanif = NULL;
87

    
88
		switch ($interface) {
89
			case "l2tp":
90
				$wanif = "l2tp";
91
				break;
92
			case "pptp":
93
				$wanif = "pptp";
94
				break;
95
			case "pppoe":
96
				$wanif = "pppoe";
97
				break;
98
			case "openvpn":
99
				$wanif = "openvpn";
100
				break;
101
			case "IPsec":
102
			case "ipsec":
103
			case "enc0":
104
				$wanif = "enc0";
105
				break;
106
			case "ppp":
107
				$wanif = "ppp";
108
				break;
109
			default:
110
				if (substr($interface, 0, 4) == '_vip') {
111
					$wanif = get_configured_vip_interface($interface);
112
					if (!empty($wanif)) {
113
						$wanif = get_real_interface($wanif);
114
					}
115
					break;
116
				} else if (substr($interface, 0, 5) == '_lloc') {
117
					$interface = substr($interface, 5);
118
				} else if (strstr($interface, "_vlan") || isset($existing_ifs[$interface])) {
119
					/*
120
					 * If a real interface was already passed simply
121
					 * pass the real interface back.  This encourages
122
					 * the usage of this function in more cases so that
123
					 * we can combine logic for more flexibility.
124
					 */
125
					$wanif = $interface;
126
					break;
127
				}
128

    
129
				if (empty($config['interfaces'][$interface])) {
130
					break;
131
				}
132

    
133
				$cfg = &$config['interfaces'][$interface];
134

    
135
				if ($family == "inet6") {
136
					switch ($cfg['ipaddrv6']) {
137
						case "6rd":
138
						case "6to4":
139
							$wanif = "{$interface}_stf";
140
							break;
141
						case 'pppoe':
142
						case 'ppp':
143
						case 'l2tp':
144
						case 'pptp':
145
							if (is_array($cfg['wireless']) || preg_match($g['wireless_regex'], $cfg['if'])) {
146
								$wanif = interface_get_wireless_clone($cfg['if']);
147
							} else {
148
								$wanif = $cfg['if'];
149
							}
150
							break;
151
						default:
152
							switch ($cfg['ipaddr']) {
153
								case 'pppoe':
154
								case 'ppp':
155
								case 'l2tp':
156
								case 'pptp':
157
									if (isset($cfg['dhcp6usev4iface']) && $realv6iface === false) {
158
										$wanif = $cfg['if'];
159
									} else {
160
										$parents = get_parent_interface($interface);
161
										if (!empty($parents[0])) {
162
											$wanif = $parents[0];
163
										} else {
164
											$wanif = $cfg['if'];
165
										}
166
									}
167
									break;
168
								default:
169
									if (is_array($cfg['wireless']) || preg_match($g['wireless_regex'], $cfg['if'])) {
170
										$wanif = interface_get_wireless_clone($cfg['if']);
171
									} else {
172
										$wanif = $cfg['if'];
173
									}
174
									break;
175
							}
176
							break;
177
					}
178
				} else {
179
					// Wireless cloned NIC support (FreeBSD 8+)
180
					// interface name format: $parentnic_wlanparentnic#
181
					// example: ath0_wlan0
182
					if (is_array($cfg['wireless']) || preg_match($g['wireless_regex'], $cfg['if'])) {
183
						$wanif = interface_get_wireless_clone($cfg['if']);
184
					} else {
185
						$wanif = $cfg['if'];
186
					}
187
				}
188
				break;
189
		}
190
		$out[$interface] = $wanif;
191
	}
192

    
193
	return $out;
194
}
195
/*
196
 * interface_assign_description_fast($portlist, $friendlyifnames)
197
 *
198
 * This function replaces the function defined in interfaces_assign.php
199
 *
200
 * I created this version of the function because in interfaces_assign.php
201
 * the interface_assign_description() function is used twice, in both cases
202
 * being called for every iteration through the array of interfaces, and
203
 * was seemingly dragging the performance of the HTML generation code down
204
 * when faced with a large number of VLAN interfaces.
205
 *
206
 * Although this function internally recreates the loop that its namesake was
207
 * called in; the fact it's only called once rather than once per interface * 2
208
 * has resulted in a significant speed gain with a large number of optional
209
 * interfaces configured.
210
 *
211
 * $portlist is the same $portlist as defined in interfaces_assign.php, call this
212
 * function after all the optional interfaces are added to $portlist.
213
 *
214
 * $friendlyifnames is a global variable of my own making, created by calling
215
 * convert_real_interface_to_friendly_interface_name_fast() on the keys of $portlist.
216
 *
217
 * Return value of this function is an associative array of interface descriptions
218
 * indexed by the unique name of the interface.
219
 *
220
 */
221
function interface_assign_description_fast($portlist, $friendlyifnames) {
222
	global $ovpn_descrs, $ipsec_descrs;
223
	$out = array();
224
	$gettext = gettext('on');
225
	foreach($portlist as $portname => $portinfo) {
226
		if ($portinfo['isvlan']) {
227
			$descr = sprintf('VLAN %1$s '.$gettext.' %2$s', $portinfo['tag'], $portinfo['if']);
228
			$iface = $friendlyifnames[$portinfo['if']];
229
			if (isset($iface) && strlen($iface) > 0) {
230
				$descr .= " - $iface";
231
			}
232
			if ($portinfo['descr']) {
233
				$descr .= " (" . $portinfo['descr'] . ")";
234
			}
235
		} elseif ($portinfo['iswlclone']) {
236
			$descr = $portinfo['cloneif'];
237
			if ($portinfo['descr']) {
238
				$descr .= " (" . $portinfo['descr'] . ")";
239
			}
240
		} elseif ($portinfo['isppp']) {
241
			$descr = $portinfo['descr'];
242
		} elseif ($portinfo['isbridge']) {
243
			$descr = strtoupper($portinfo['bridgeif']);
244
			if ($portinfo['descr']) {
245
				$descr .= " (" . $portinfo['descr'] . ")";
246
			}
247
		} elseif ($portinfo['isgre']) {
248
			$descr = "GRE {$portinfo['remote-addr']}";
249
			if ($portinfo['descr']) {
250
				$descr .= " (" . $portinfo['descr'] . ")";
251
			}
252
		} elseif ($portinfo['isgif']) {
253
			$descr = "GIF {$portinfo['remote-addr']}";
254
			if ($portinfo['descr']) {
255
				$descr .= " (" . $portinfo['descr'] . ")";
256
			}
257
		} elseif ($portinfo['isvxlan']) {
258
			$descr = "VXLAN {$portinfo['remote-addr']}";
259
			if ($portinfo['descr']) {
260
				$descr .= " (" . $portinfo['descr'] . ")";
261
			}
262
		} elseif ($portinfo['islagg']) {
263
			$descr = strtoupper($portinfo['laggif']);
264
			if ($portinfo['descr']) {
265
				$descr .= " (" . $portinfo['descr'] . ")";
266
			}
267
		} elseif ($portinfo['isqinq']) {
268
			$descr = $portinfo['descr'];
269
		} elseif (substr($portname, 0, 4) == 'ovpn') {
270
			$descr = $portname . " (" . $ovpn_descrs[substr($portname, 5)] . ")";
271
		} elseif (substr($portname, 0, 5) == 'ipsec') {
272
			$descr = $portname . " (" . $ipsec_descrs[$portname] . ")";
273
		} else {
274
			$descr = $portname . " (" . $portinfo['mac'] . ")";
275
		}
276
		$out[$portname] = htmlspecialchars($descr);
277
	}
278
	return $out;
279
}
280
?>
(23-23/61)