Project

General

Profile

Download (8.55 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
	init_config_arr(array('interfaces'));
65
	$ifdescrs = &$config['interfaces'];
66
	$iffriendlynames = array_keys($ifdescrs);
67
	$out = array_flip(get_real_interface_fast($iffriendlynames));
68
	return $out;
69
}
70

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

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

    
80
	$existing_ifs = does_interface_exist_fast();
81

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

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

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

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

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

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