Project

General

Profile

Download (8.58 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-2025 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
	$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
	return array_flip(get_real_interface_fast(array_keys(config_get_path('interfaces', []))));
61
}
62

    
63
/*
64
 * get_real_interface_fast($interfaces, ...)
65
 * Exactly the same as it's namesake, except it takes an array of interfaces and returns an array
66
 *
67
 */
68

    
69
function get_real_interface_fast($interfaces = array(), $family = "all", $realv6iface = false, $flush = true) {
70
	global $g;
71

    
72
	$existing_ifs = does_interface_exist_fast();
73
	$if_config = config_get_path('interfaces', []);
74

    
75
	$out = array();
76
	foreach ($interfaces as $interface) {
77
		$wanif = NULL;
78

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

    
120
				$cfg = &$if_config[$interface];
121
				if (empty($cfg)) {
122
					break;
123
				}
124

    
125
				if ($family == "inet6") {
126
					switch ($cfg['ipaddrv6']) {
127
						case "6rd":
128
						case "6to4":
129
							$wanif = "{$interface}_stf";
130
							break;
131
						case 'pppoe':
132
						case 'ppp':
133
						case 'l2tp':
134
						case 'pptp':
135
							if (is_array($cfg['wireless']) || preg_match(g_get('wireless_regex'), $cfg['if'])) {
136
								$wanif = interface_get_wireless_clone($cfg['if']);
137
							} else {
138
								$wanif = $cfg['if'];
139
							}
140
							break;
141
						default:
142
							switch ($cfg['ipaddr']) {
143
								case 'pppoe':
144
								case 'ppp':
145
								case 'l2tp':
146
								case 'pptp':
147
									// Added catch for static v6 but using v4 link. Sets things to use pppoe link
148
									if ((isset($cfg['dhcp6usev4iface']) && $realv6iface === false) ||
149
									    isset($cfg['ipv6usev4iface']) || isset($cfg['slaacusev4iface'])) {
150
										$wanif = $cfg['if'];
151
									} else {
152
										$parents = get_parent_interface($interface);
153
										if (!empty($parents[0])) {
154
											$wanif = $parents[0];
155
										} else {
156
											$wanif = $cfg['if'];
157
										}
158
									}
159
									break;
160
								default:
161
									if (is_array($cfg['wireless']) || preg_match(g_get('wireless_regex'), $cfg['if'])) {
162
										$wanif = interface_get_wireless_clone($cfg['if']);
163
									} else {
164
										$wanif = $cfg['if'];
165
									}
166
									break;
167
							}
168
							break;
169
					}
170
				} else {
171
					// Wireless cloned NIC support (FreeBSD 8+)
172
					// interface name format: $parentnic_wlanparentnic#
173
					// example: ath0_wlan0
174
					if (is_array($cfg['wireless']) || preg_match(g_get('wireless_regex'), $cfg['if'])) {
175
						$wanif = interface_get_wireless_clone($cfg['if']);
176
					} else {
177
						$wanif = $cfg['if'];
178
					}
179
				}
180
				break;
181
		}
182
		$out[$interface] = $wanif;
183
	}
184

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