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-2022 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
									// Added catch for static v6 but using v4 link. Sets things to use pppoe link
158
									if ((isset($cfg['dhcp6usev4iface']) && $realv6iface === false) ||
159
									    isset($cfg['ipv6usev4iface']) || isset($cfg['slaacusev4iface'])) {
160
										$wanif = $cfg['if'];
161
									} else {
162
										$parents = get_parent_interface($interface);
163
										if (!empty($parents[0])) {
164
											$wanif = $parents[0];
165
										} else {
166
											$wanif = $cfg['if'];
167
										}
168
									}
169
									break;
170
								default:
171
									if (is_array($cfg['wireless']) || preg_match($g['wireless_regex'], $cfg['if'])) {
172
										$wanif = interface_get_wireless_clone($cfg['if']);
173
									} else {
174
										$wanif = $cfg['if'];
175
									}
176
									break;
177
							}
178
							break;
179
					}
180
				} else {
181
					// Wireless cloned NIC support (FreeBSD 8+)
182
					// interface name format: $parentnic_wlanparentnic#
183
					// example: ath0_wlan0
184
					if (is_array($cfg['wireless']) || preg_match($g['wireless_regex'], $cfg['if'])) {
185
						$wanif = interface_get_wireless_clone($cfg['if']);
186
					} else {
187
						$wanif = $cfg['if'];
188
					}
189
				}
190
				break;
191
		}
192
		$out[$interface] = $wanif;
193
	}
194

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