Project

General

Profile

Download (8.38 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-2016 Rubicon Communications, LLC (Netgate) (pfSense)
7
 * Copyright (c) 2017 Peter Schofield (parts of this file)
8
 * All rights reserved.
9
 *
10
 * originally based on m0n0wall (http://m0n0.ch/wall)
11
 * Copyright (c) 2004 Manuel Kasper <mk@neon1.net>.
12
 * All rights reserved.
13
 *
14
 * 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
 *
18
 * http://www.apache.org/licenses/LICENSE-2.0
19
 *
20
 * 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
 *
26
 *
27
 * This file contains rewrites of several functions, from both interfaces.inc
28
 * and interfaces_assign.php. The general purpose of these rewrites is not
29
 * necessarily to be faster in and of themselves, though they may be, but to
30
 * replace functions called multiple times with a function that's called only
31
 * once. This results in a significant speedup because there are far fewer
32
 * function calls, fewer loops and suchlike. It does, however, increase memory
33
 * usage somewhat, but it shouldn't be significant in the grand scheme of things.
34
 *
35
 *
36
 * Functions in this file may not require/use parameters when called, however
37
 * in most cases they will accept parameters as per their original forms, for
38
 * consistency.
39

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

    
51
	$ints = get_interface_arr($flush);
52
	return array_flip($ints);
53
}
54

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

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

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

    
76
function get_real_interface_fast($interfaces = array(), $family = "all", $realv6iface = false, $flush = true) {
77
	global $config, $g;
78

    
79
	$existing_ifs = does_interface_exist_fast();
80

    
81
	$out = array();
82
	foreach ($interfaces as $interface) {
83
		$wanif = NULL;
84

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

    
126
				if (empty($config['interfaces'][$interface])) {
127
					break;
128
				}
129

    
130
				$cfg = &$config['interfaces'][$interface];
131

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

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