Project

General

Profile

Download (39.4 KB) Statistics
| Branch: | Tag: | Revision:
1 17623ab5 Bill Marquette
<?php
2
/*
3 ce77a9c4 Phil Davis
	gwlb.inc
4
	Copyright (C) 2008 Bill Marquette, Seth Mos
5
	Copyright (C) 2010 Ermal Luçi
6
	All rights reserved.
7
8
	Redistribution and use in source and binary forms, with or without
9
	modification, are permitted provided that the following conditions are met:
10
11
	1. Redistributions of source code must retain the above copyright notice,
12
	this list of conditions and the following disclaimer.
13
14
	2. Redistributions in binary form must reproduce the above copyright
15
	notice, this list of conditions and the following disclaimer in the
16
	documentation and/or other materials provided with the distribution.
17
18
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
	POSSIBILITY OF SUCH DAMAGE.
28 17623ab5 Bill Marquette
29 bf8c7971 Ermal
	pfSense_BUILDER_BINARIES:	/sbin/route	/usr/local/sbin/apinger
30 523855b0 Scott Ullrich
	pfSense_MODULE:	routing
31
32 c1191d5b Ermal
 */
33 c5e53ee6 jim-p
require_once("config.inc");
34 d55cba63 N0YB
require_once("rrd.inc");
35 17623ab5 Bill Marquette
36 14661668 Phil Davis
/* Returns an array of default values used for apinger.conf */
37
function return_apinger_defaults() {
38
	return array(
39
		"latencylow" => "200",
40
		"latencyhigh" => "500",
41
		"losslow" => "10",
42
		"losshigh" => "20",
43
		"interval" => "1",
44 3c6787ed N0YB
		"down" => "10",
45
		"avg_delay_samples" => "10",
46
		"avg_loss_samples" => "50",
47
		"avg_loss_delay_samples" => "20");
48 a99547e4 Ermal
}
49 14661668 Phil Davis
50 3d471a14 Ermal
/*
51
 * Creates monitoring configuration file and
52 14661668 Phil Davis
 * adds appropriate static routes.
53 cdcea13f Seth Mos
 */
54
function setup_gateways_monitor() {
55 3d471a14 Ermal
	global $config, $g;
56 ffe76308 Seth Mos
57 3d471a14 Ermal
	$gateways_arr = return_gateways_array();
58
	if (!is_array($gateways_arr)) {
59
		log_error("No gateways to monitor. Apinger will not be run.");
60
		killbypid("{$g['varrun_path']}/apinger.pid");
61 3e3ff931 Ryan Dlugosz
		@unlink("{$g['varrun_path']}/apinger.status");
62 3d471a14 Ermal
		return;
63
	}
64 cdcea13f Seth Mos
65 a99547e4 Ermal
	$apinger_debug = "";
66 9ba87997 Phil Davis
	if (isset($config['system']['apinger_debug'])) {
67 a99547e4 Ermal
		$apinger_debug = "debug on";
68 9ba87997 Phil Davis
	}
69 a99547e4 Ermal
70 14661668 Phil Davis
	$apinger_default = return_apinger_defaults();
71 cdcea13f Seth Mos
	$apingerconfig = <<<EOD
72
73
# pfSense apinger configuration file. Automatically Generated!
74
75 a99547e4 Ermal
{$apinger_debug}
76
77 cdcea13f Seth Mos
## User and group the pinger should run as
78 0534d60a Chris Buechler
user "root"
79
group "wheel"
80 cdcea13f Seth Mos
81
## Mailer to use (default: "/usr/lib/sendmail -t")
82 f040882c Renato Botelho
#mailer "/var/qmail/bin/qmail-inject"
83 cdcea13f Seth Mos
84
## Location of the pid-file (default: "/var/run/apinger.pid")
85
pid_file "{$g['varrun_path']}/apinger.pid"
86
87
## Format of timestamp (%s macro) (default: "%b %d %H:%M:%S")
88
#timestamp_format "%Y%m%d%H%M%S"
89
90
status {
91 14661668 Phil Davis
	## File where the status information should be written to
92 3e3ff931 Ryan Dlugosz
	file "{$g['varrun_path']}/apinger.status"
93 cdcea13f Seth Mos
	## Interval between file updates
94
	## when 0 or not set, file is written only when SIGUSR1 is received
95 ecb08c6a Ermal
	interval 5s
96 cdcea13f Seth Mos
}
97
98
########################################
99
# RRDTool status gathering configuration
100
# Interval between RRD updates
101
rrd interval 60s;
102
103 14661668 Phil Davis
## These parameters can be overridden in a specific alarm configuration
104 f040882c Renato Botelho
alarm default {
105 36777fe4 Ermal
	command on "/usr/local/sbin/pfSctl -c 'service reload dyndns %T' -c 'service reload ipsecdns' -c 'service reload openvpn %T' -c 'filter reload' "
106 be2a18bf Phil Davis
	command off "/usr/local/sbin/pfSctl -c 'service reload dyndns %T' -c 'service reload ipsecdns' -c 'service reload openvpn %T' -c 'filter reload' "
107 cdcea13f Seth Mos
	combine 10s
108
}
109
110 f040882c Renato Botelho
## "Down" alarm definition.
111 cdcea13f Seth Mos
## This alarm will be fired when target doesn't respond for 30 seconds.
112
alarm down "down" {
113 14661668 Phil Davis
	time {$apinger_default['down']}s
114 cdcea13f Seth Mos
}
115
116 f040882c Renato Botelho
## "Delay" alarm definition.
117 cdcea13f Seth Mos
## This alarm will be fired when responses are delayed more than 200ms
118
## it will be canceled, when the delay drops below 100ms
119
alarm delay "delay" {
120 14661668 Phil Davis
	delay_low {$apinger_default['latencylow']}ms
121
	delay_high {$apinger_default['latencyhigh']}ms
122 cdcea13f Seth Mos
}
123
124 f040882c Renato Botelho
## "Loss" alarm definition.
125 cdcea13f Seth Mos
## This alarm will be fired when packet loss goes over 20%
126
## it will be canceled, when the loss drops below 10%
127
alarm loss "loss" {
128 14661668 Phil Davis
	percent_low {$apinger_default['losslow']}
129
	percent_high {$apinger_default['losshigh']}
130 cdcea13f Seth Mos
}
131
132
target default {
133 f040882c Renato Botelho
	## How often the probe should be sent
134 14661668 Phil Davis
	interval {$apinger_default['interval']}s
135 f040882c Renato Botelho
136
	## How many replies should be used to compute average delay
137 cdcea13f Seth Mos
	## for controlling "delay" alarms
138 3c6787ed N0YB
	avg_delay_samples {$apinger_default['avg_delay_samples']}
139 f040882c Renato Botelho
140 cdcea13f Seth Mos
	## How many probes should be used to compute average loss
141 3c6787ed N0YB
	avg_loss_samples {$apinger_default['avg_loss_samples']}
142 cdcea13f Seth Mos
143
	## The delay (in samples) after which loss is computed
144
	## without this delays larger than interval would be treated as loss
145 3c6787ed N0YB
	avg_loss_delay_samples {$apinger_default['avg_loss_delay_samples']}
146 cdcea13f Seth Mos
147
	## Names of the alarms that may be generated for the target
148
	alarms "down","delay","loss"
149
150
	## Location of the RRD
151 1bce2729 Seth Mos
	#rrd file "{$g['vardb_path']}/rrd/apinger-%t.rrd"
152 cdcea13f Seth Mos
}
153
154
EOD;
155
156 74c834f1 smos
	$monitor_ips = array();
157 9ba87997 Phil Davis
	foreach ($gateways_arr as $name => $gateway) {
158 33c06ef7 Ermal
		/* Do not monitor if such was requested */
159 9ba87997 Phil Davis
		if (isset($gateway['monitor_disable'])) {
160 33c06ef7 Ermal
			continue;
161 9ba87997 Phil Davis
		}
162 3d471a14 Ermal
		if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
163 9ba87997 Phil Davis
			if (is_ipaddr($gateway['gateway'])) {
164 3d471a14 Ermal
				$gateway['monitor'] = $gateway['gateway'];
165 9ba87997 Phil Davis
			} else { /* No chance to get an ip to monitor skip target. */
166 f31ab121 Ermal
				continue;
167 9ba87997 Phil Davis
			}
168 3d471a14 Ermal
		}
169 f31ab121 Ermal
170 f040882c Renato Botelho
		/* if the monitor address is already used before, skip */
171 9ba87997 Phil Davis
		if (in_array($gateway['monitor'], $monitor_ips)) {
172 74c834f1 smos
			continue;
173 9ba87997 Phil Davis
		}
174 f040882c Renato Botelho
175 9ba87997 Phil Davis
		/* Interface ip is needed since apinger will bind a socket to it.
176
		 * However the config GUI should already have checked this and when
177 8c7e38ff Camlin
		 * PPoE is used the IP address is set to "dynamic". So using is_ipaddrv4
178
		 * or is_ipaddrv6 to identify packet type would be wrong, especially as
179
		 * further checks (that can cope with the "dynamic" case) are present inside
180
		 * the if block. So using $gateway['ipprotocol'] is the better option.
181
		 */
182
		if ($gateway['ipprotocol'] == "inet") { // This is an IPv4 gateway...
183 640b3a9a Seth Mos
			$gwifip = find_interface_ip($gateway['interface'], true);
184 9ba87997 Phil Davis
			if (!is_ipaddrv4($gwifip)) {
185 e9d156fd Ermal
				continue; //Skip this target
186 9ba87997 Phil Davis
			}
187 32a9eb18 Ermal
188 9ba87997 Phil Davis
			if ($gwifip == "0.0.0.0") {
189 2d793d01 Phil Davis
				continue; //Skip this target - the gateway is still waiting for DHCP
190 9ba87997 Phil Davis
			}
191 2d793d01 Phil Davis
192 32a9eb18 Ermal
			/*
193
			 * If the gateway is the same as the monitor we do not add a
194
			 * route as this will break the routing table.
195
			 * Add static routes for each gateway with their monitor IP
196
			 * not strictly necessary but is a added level of protection.
197
			 */
198
			if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
199
				log_error("Removing static route for monitor {$gateway['monitor']} and adding a new route through {$gateway['gateway']}");
200 9ba87997 Phil Davis
				if (interface_isppp_type($gateway['friendlyiface'])) {
201 7a63d5d0 Ermal LUÇI
					mwexec("/sbin/route change -host " . escapeshellarg($gateway['monitor']) .
202
						" -iface " . escapeshellarg($gateway['interface']), true);
203 9ba87997 Phil Davis
				} else {
204 7a63d5d0 Ermal LUÇI
					mwexec("/sbin/route change -host " . escapeshellarg($gateway['monitor']) .
205
						" " . escapeshellarg($gateway['gateway']), true);
206 9ba87997 Phil Davis
				}
207 7a63d5d0 Ermal LUÇI
208
				pfSense_kill_states("0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmp");
209 32a9eb18 Ermal
			}
210 8c7e38ff Camlin
		} else if ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
211 dd8d9bdc Camlin
			if ($gateway['monitor'] == $gateway['gateway']) {
212
				/* link locals really need a different src ip */
213
				if (is_linklocal($gateway['gateway'])) {
214 9ba87997 Phil Davis
					if (!strpos($gateway['gateway'], '%')) {
215 f4443dce Ermal
						$gateway['gateway'] .= '%' . $gateway['interface'];
216 9ba87997 Phil Davis
					}
217 dd8d9bdc Camlin
					$gwifip = find_interface_ipv6_ll($gateway['interface'], true);
218
				} else {
219
					$gwifip = find_interface_ipv6($gateway['interface'], true);
220
				}
221 81a3b6f5 smos
			} else {
222 dd8d9bdc Camlin
				/* 'monitor' has been set, so makes sure it has precedence over
223
				 * 'gateway' in defining the source IP. Otherwise if 'gateway'
224
				 * is a local link and 'monitor' is global routable then the
225
				 * ICMP6 response would not find its way back home...
226
				 */
227 81a3b6f5 smos
				$gwifip = find_interface_ipv6($gateway['interface'], true);
228
			}
229 3f6525c1 Renato Botelho
230 118218cb Renato Botelho
			/* Make sure srcip and target have scope defined when they are ll */
231 9ba87997 Phil Davis
			if (is_linklocal($gwifip) && !strpos($gwifip, '%')) {
232 3f6525c1 Renato Botelho
				$gwifip .= '%' . $gateway['interface'];
233 9ba87997 Phil Davis
			}
234
			if (is_linklocal($gateway['monitor']) && !strpos($gateway['monitor'], '%')) {
235 118218cb Renato Botelho
				$gateway['monitor'] .= "%{$gateway['interface']}";
236 9ba87997 Phil Davis
			}
237 3f6525c1 Renato Botelho
238 9ba87997 Phil Davis
			if (!is_ipaddrv6($gwifip)) {
239 e9d156fd Ermal
				continue; //Skip this target
240 9ba87997 Phil Davis
			}
241 32a9eb18 Ermal
242
			/*
243
			 * If the gateway is the same as the monitor we do not add a
244
			 * route as this will break the routing table.
245
			 * Add static routes for each gateway with their monitor IP
246
			 * not strictly necessary but is a added level of protection.
247
			 */
248 f4443dce Ermal
			if ($gateway['gateway'] != $gateway['monitor']) {
249 32a9eb18 Ermal
				log_error("Removing static route for monitor {$gateway['monitor']} and adding a new route through {$gateway['gateway']}");
250 9ba87997 Phil Davis
				if (interface_isppp_type($gateway['friendlyiface'])) {
251 7a63d5d0 Ermal LUÇI
					mwexec("/sbin/route change -host -inet6 " . escapeshellarg($gateway['monitor']) .
252
						" -iface " . escapeshellarg($gateway['interface']), true);
253 9ba87997 Phil Davis
				} else {
254 7a63d5d0 Ermal LUÇI
					mwexec("/sbin/route change -host -inet6 " . escapeshellarg($gateway['monitor']) .
255
						" " . escapeshellarg($gateway['gateway']), true);
256 9ba87997 Phil Davis
				}
257 7a63d5d0 Ermal LUÇI
258
				pfSense_kill_states("::0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmpv6");
259 32a9eb18 Ermal
			}
260 9ba87997 Phil Davis
		} else {
261 e9d156fd Ermal
			continue;
262 9ba87997 Phil Davis
		}
263 3d471a14 Ermal
264 45eb8aeb Renato Botelho
		$monitor_ips[] = $gateway['monitor'];
265 f44f8eb5 Ermal
		$apingercfg = "target \"{$gateway['monitor']}\" {\n";
266 68f291ff Ermal
		$apingercfg .= "	description \"{$name}\"\n";
267 3d471a14 Ermal
		$apingercfg .= "	srcip \"{$gwifip}\"\n";
268 3c6787ed N0YB
269
		## How often the probe should be sent
270 6c07db48 Phil Davis
		if (!empty($gateway['interval']) && is_numeric($gateway['interval'])) {
271 3c6787ed N0YB
			$interval = intval($gateway['interval']);	# Restrict to Integer
272 6c07db48 Phil Davis
			if ($interval < 1) {
273
				$interval = 1;	# Minimum
274 918bdf0d Phil Davis
			}
275
			if ($interval != $apinger_default['interval']) {	# If not default value
276 3c6787ed N0YB
				$apingercfg .= "	interval " . $interval . "s\n";
277 918bdf0d Phil Davis
			}
278 3c6787ed N0YB
		}
279
280 9ba87997 Phil Davis
		## How many replies should be used to compute average delay
281 3c6787ed N0YB
		## for controlling "delay" alarms
282
		if (!empty($gateway['avg_delay_samples']) && is_numeric($gateway['avg_delay_samples'])) {
283
			$avg_delay_samples = intval($gateway['avg_delay_samples']);	# Restrict to Integer
284 6c07db48 Phil Davis
			if ($avg_delay_samples < 1) {
285
				$avg_delay_samples = 1;	# Minimum
286 918bdf0d Phil Davis
			}
287
			if ($avg_delay_samples != $apinger_default['avg_delay_samples']) {	# If not default value
288 3c6787ed N0YB
				$apingercfg .= "	avg_delay_samples " . $avg_delay_samples . "\n";
289 918bdf0d Phil Davis
			}
290 3c6787ed N0YB
		}
291
292
		## How many probes should be used to compute average loss
293
		if (!empty($gateway['avg_loss_samples']) && is_numeric($gateway['avg_loss_samples'])) {
294
			$avg_loss_samples = intval($gateway['avg_loss_samples']);	# Restrict to Integer
295 9ba87997 Phil Davis
			if ($avg_loss_samples < 1) {
296
				$avg_loss_samples = 1;	# Minimum
297
			}
298
			if ($avg_loss_samples != $apinger_default['avg_loss_samples']) { # If not default value
299 3c6787ed N0YB
				$apingercfg .= "	avg_loss_samples " . $avg_loss_samples . "\n";
300 9ba87997 Phil Davis
			}
301 3c6787ed N0YB
		}
302
303
		## The delay (in samples) after which loss is computed
304
		## without this delays larger than interval would be treated as loss
305
		if (!empty($gateway['avg_loss_delay_samples']) && is_numeric($gateway['avg_loss_delay_samples'])) {
306
			$avg_loss_delay_samples = intval($gateway['avg_loss_delay_samples']);	# Restrict to Integer
307 9ba87997 Phil Davis
			if ($avg_loss_delay_samples < 1) {
308
				$avg_loss_delay_samples = 1;	# Minimum
309
			}
310
			if ($avg_loss_delay_samples != $apinger_default['avg_loss_delay_samples']) { # If not default value
311 3c6787ed N0YB
				$apingercfg .= "	avg_loss_delay_samples " . $avg_loss_delay_samples . "\n";
312 9ba87997 Phil Davis
			}
313 3c6787ed N0YB
		}
314
315 3d471a14 Ermal
		$alarms = "";
316 f44f8eb5 Ermal
		$alarmscfg = "";
317 3d471a14 Ermal
		$override = false;
318 02a8dab0 ccesario
		if (!empty($gateway['losslow'])) {
319 68f291ff Ermal
			$alarmscfg .= "alarm loss \"{$name}loss\" {\n";
320 3d471a14 Ermal
			$alarmscfg .= "\tpercent_low {$gateway['losslow']}\n";
321 f040882c Renato Botelho
			$alarmscfg .= "\tpercent_high {$gateway['losshigh']}\n";
322 3d471a14 Ermal
			$alarmscfg .= "}\n";
323 68f291ff Ermal
			$alarms .= "\"{$name}loss\"";
324 3d471a14 Ermal
			$override = true;
325
		} else {
326 9ba87997 Phil Davis
			if ($override == true) {
327 3d471a14 Ermal
				$alarms .= ",";
328 9ba87997 Phil Davis
			}
329 3d471a14 Ermal
			$alarms .= "\"loss\"";
330
			$override = true;
331
		}
332
		if (!empty($gateway['latencylow'])) {
333 68f291ff Ermal
			$alarmscfg .= "alarm delay \"{$name}delay\" {\n";
334 3d471a14 Ermal
			$alarmscfg .= "\tdelay_low {$gateway['latencylow']}ms\n";
335
			$alarmscfg .= "\tdelay_high {$gateway['latencyhigh']}ms\n";
336
			$alarmscfg .= "}\n";
337 9ba87997 Phil Davis
			if ($override == true) {
338 3d471a14 Ermal
				$alarms .= ",";
339 9ba87997 Phil Davis
			}
340 68f291ff Ermal
			$alarms .= "\"{$name}delay\"";
341 3d471a14 Ermal
			$override = true;
342
		} else {
343 9ba87997 Phil Davis
			if ($override == true) {
344 3d471a14 Ermal
				$alarms .= ",";
345 9ba87997 Phil Davis
			}
346 3d471a14 Ermal
			$alarms .= "\"delay\"";
347
			$override = true;
348
		}
349
		if (!empty($gateway['down'])) {
350 68f291ff Ermal
			$alarmscfg .= "alarm down \"{$name}down\" {\n";
351 3d471a14 Ermal
			$alarmscfg .= "\ttime {$gateway['down']}s\n";
352
			$alarmscfg .= "}\n";
353 9ba87997 Phil Davis
			if ($override == true) {
354 3d471a14 Ermal
				$alarms .= ",";
355 9ba87997 Phil Davis
			}
356 68f291ff Ermal
			$alarms .= "\"{$name}down\"";
357 3d471a14 Ermal
			$override = true;
358
		} else {
359 9ba87997 Phil Davis
			if ($override == true) {
360 3d471a14 Ermal
				$alarms .= ",";
361 9ba87997 Phil Davis
			}
362 3d471a14 Ermal
			$alarms .= "\"down\"";
363
			$override = true;
364 cdcea13f Seth Mos
		}
365 9ba87997 Phil Davis
		if ($override == true) {
366 3d471a14 Ermal
			$apingercfg .= "\talarms override {$alarms};\n";
367 9ba87997 Phil Davis
		}
368 3d471a14 Ermal
369 9ba87997 Phil Davis
		if (isset($gateway['force_down'])) {
370 81f19476 Renato Botelho
			$apingercfg .= "\tforce_down on\n";
371 9ba87997 Phil Davis
		}
372 81f19476 Renato Botelho
373 3d471a14 Ermal
		$apingercfg .= "	rrd file \"{$g['vardb_path']}/rrd/{$gateway['name']}-quality.rrd\"\n";
374
		$apingercfg .= "}\n";
375
		$apingercfg .= "\n";
376
377 023920e7 Ermal
		$apingerconfig .= $alarmscfg;
378
		$apingerconfig .= $apingercfg;
379 d55cba63 N0YB
380 9ba87997 Phil Davis
		# Create gateway quality RRD with settings more suitable for pfSense graph set,
381
		# since apinger uses default step (300; 5 minutes) and other settings that don't
382 d55cba63 N0YB
		# match the pfSense gateway quality graph set.
383
		create_gateway_quality_rrd("{$g['vardb_path']}/rrd/{$gateway['name']}-quality.rrd");
384 cdcea13f Seth Mos
	}
385 de82ec90 Ermal
	@file_put_contents("{$g['varetc_path']}/apinger.conf", $apingerconfig);
386 292da16b Ermal
	unset($apingerconfig);
387 cdcea13f Seth Mos
388 9ba87997 Phil Davis
	/* Restart apinger process */
389
	if (isvalidpid("{$g['varrun_path']}/apinger.pid")) {
390 746f0afb Ermal
		sigkillbypid("{$g['varrun_path']}/apinger.pid", "HUP");
391 9ba87997 Phil Davis
	} else {
392 746f0afb Ermal
		/* start a new apinger process */
393
		@unlink("{$g['varrun_path']}/apinger.status");
394
		sleep(1);
395
		mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
396
		sleep(1);
397
		sigkillbypid("{$g['varrun_path']}/apinger.pid", "USR1");
398
	}
399 4f060616 Ermal
400 cdcea13f Seth Mos
	return 0;
401
}
402
403
/* return the status of the apinger targets as a array */
404 3d471a14 Ermal
function return_gateways_status($byname = false) {
405 ae9618af Ermal
	global $config, $g;
406 cdcea13f Seth Mos
407
	$apingerstatus = array();
408 2db93ddb Ermal
	/* Always get the latest status from apinger */
409 9ba87997 Phil Davis
	if (file_exists("{$g['varrun_path']}/apinger.pid")) {
410
		sigkillbypid("{$g['varrun_path']}/apinger.pid", "USR1");
411
	}
412 3e3ff931 Ryan Dlugosz
	if (file_exists("{$g['varrun_path']}/apinger.status")) {
413
		$apingerstatus = file("{$g['varrun_path']}/apinger.status");
414 9ba87997 Phil Davis
	} else {
415 2db93ddb Ermal
		$apingerstatus = array();
416 9ba87997 Phil Davis
	}
417 cdcea13f Seth Mos
418 67ee1ec5 Ermal Luçi
	$status = array();
419 9ba87997 Phil Davis
	foreach ($apingerstatus as $line) {
420 75466131 Ermal
		$info = explode("|", $line);
421 9ba87997 Phil Davis
		if ($byname == false) {
422 3d471a14 Ermal
			$target = $info[0];
423 9ba87997 Phil Davis
		} else {
424 3d471a14 Ermal
			$target = $info[2];
425 9ba87997 Phil Davis
		}
426 47c48e28 smos
427 dea0921d Ermal
		$status[$target] = array();
428 3d471a14 Ermal
		$status[$target]['monitorip'] = $info[0];
429 79b7f498 Ermal
		$status[$target]['srcip'] = $info[1];
430
		$status[$target]['name'] = $info[2];
431
		$status[$target]['lastcheck'] = $info[5] ? date('r', $info[5]) : date('r');
432 603f19f0 smos
		$status[$target]['delay'] = empty($info[6]) ? "0ms" : round($info[6], 1) ."ms" ;
433
		$status[$target]['loss'] = empty($info[7]) ? "0.0%" : round($info[7], 1) . "%";
434 79b7f498 Ermal
		$status[$target]['status'] = trim($info[8]);
435 cdcea13f Seth Mos
	}
436 68f291ff Ermal
437 47c48e28 smos
	/* tack on any gateways that have monitoring disabled
438
	 * or are down, which could cause gateway groups to fail */
439 16106d2e smos
	$gateways_arr = return_gateways_array();
440 9ba87997 Phil Davis
	foreach ($gateways_arr as $gwitem) {
441
		if (!isset($gwitem['monitor_disable'])) {
442 fd34d6a9 Ermal
			continue;
443 9ba87997 Phil Davis
		}
444
		if (!is_ipaddr($gwitem['monitorip'])) {
445 fd34d6a9 Ermal
			$realif = $gwitem['interface'];
446
			$tgtip = get_interface_gateway($realif);
447 9ba87997 Phil Davis
			if (!is_ipaddr($tgtip)) {
448 fd34d6a9 Ermal
				$tgtip = "none";
449 9ba87997 Phil Davis
			}
450 fd34d6a9 Ermal
			$srcip = find_interface_ip($realif);
451
		} else {
452
			$tgtip = $gwitem['monitorip'];
453
			$srcip = find_interface_ip($realif);
454
		}
455 9ba87997 Phil Davis
		if ($byname == true) {
456 fd34d6a9 Ermal
			$target = $gwitem['name'];
457 9ba87997 Phil Davis
		} else {
458 fd34d6a9 Ermal
			$target = $tgtip;
459 9ba87997 Phil Davis
		}
460 fd34d6a9 Ermal
461
		/* failsafe for down interfaces */
462 9ba87997 Phil Davis
		if ($target == "none") {
463 fd34d6a9 Ermal
			$target = $gwitem['name'];
464
			$status[$target]['name'] = $gwitem['name'];
465
			$status[$target]['lastcheck'] = date('r');
466
			$status[$target]['delay'] = "0.0ms";
467
			$status[$target]['loss'] = "100.0%";
468
			$status[$target]['status'] = "down";
469
		} else {
470
			$status[$target]['monitorip'] = $tgtip;
471
			$status[$target]['srcip'] = $srcip;
472
			$status[$target]['name'] = $gwitem['name'];
473
			$status[$target]['lastcheck'] = date('r');
474
			$status[$target]['delay'] = "0.0ms";
475
			$status[$target]['loss'] = "0.0%";
476
			$status[$target]['status'] = "none";
477 16106d2e smos
		}
478
	}
479 cdcea13f Seth Mos
	return($status);
480 17623ab5 Bill Marquette
}
481
482 2328dcc5 Seth Mos
/* Return all configured gateways on the system */
483 06b8d43c Renato Botelho
function return_gateways_array($disabled = false, $localhost = false, $inactive = false) {
484 8d3556c2 gnhb
	global $config, $g;
485 7901dff5 Ermal Luçi
486 c66b4242 Seth Mos
	$gateways_arr = array();
487 8d848bdf Phil Davis
	$gateways_arr_temp = array();
488 c66b4242 Seth Mos
489 74c834f1 smos
	$found_defaultv4 = 0;
490
	$found_defaultv6 = 0;
491
492 33e71f10 jim-p
	// Ensure the interface cache is up to date first
493
	$interfaces = get_interface_arr(true);
494 3930a9c0 smos
	$interfaces_v4 = array();
495
	$interfaces_v6 = array();
496
497 23d9f686 Renato Botelho
	$i = -1;
498 3d471a14 Ermal
	/* Process/add all the configured gateways. */
499 ef05ae5f Ermal
	if (is_array($config['gateways']['gateway_item'])) {
500 1500614c Ermal
		foreach ($config['gateways']['gateway_item'] as $gateway) {
501 23d9f686 Renato Botelho
			/* Increment it here to do not skip items */
502
			$i++;
503
504 06b8d43c Renato Botelho
			if (empty($config['interfaces'][$gateway['interface']])) {
505 9ba87997 Phil Davis
				if ($inactive === false) {
506 06b8d43c Renato Botelho
					continue;
507 9ba87997 Phil Davis
				} else {
508 06b8d43c Renato Botelho
					$gateway['inactive'] = true;
509 9ba87997 Phil Davis
				}
510 06b8d43c Renato Botelho
			}
511 3930a9c0 smos
			$wancfg = $config['interfaces'][$gateway['interface']];
512 1500614c Ermal
513
			/* skip disabled interfaces */
514 9ba87997 Phil Davis
			if ($disabled === false && (!isset($wancfg['enable']))) {
515 1500614c Ermal
				continue;
516 9ba87997 Phil Davis
			}
517 c1d36d26 smos
518 3930a9c0 smos
			/* if the gateway is dynamic and we can find the IPv4, Great! */
519 d07bc322 Renato Botelho
			if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
520 1500614c Ermal
				if ($gateway['ipprotocol'] == "inet") {
521
					/* we know which interfaces is dynamic, this should be made a function */
522 65cfd0ca Renato Botelho
					$gateway['gateway'] = get_interface_gateway($gateway['interface']);
523
					/* no IP address found, set to dynamic */
524 9ba87997 Phil Davis
					if (!is_ipaddrv4($gateway['gateway'])) {
525 65cfd0ca Renato Botelho
						$gateway['gateway'] = "dynamic";
526 9ba87997 Phil Davis
					}
527 65cfd0ca Renato Botelho
					$gateway['dynamic'] = true;
528 74c834f1 smos
				}
529 c1d36d26 smos
530 d07bc322 Renato Botelho
				/* if the gateway is dynamic and we can find the IPv6, Great! */
531 8e78c82c Renato Botelho
				else if ($gateway['ipprotocol'] == "inet6") {
532 1500614c Ermal
					/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
533 65cfd0ca Renato Botelho
					$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
534 d07bc322 Renato Botelho
					/* no IPv6 address found, set to dynamic */
535 9ba87997 Phil Davis
					if (!is_ipaddrv6($gateway['gateway'])) {
536 d07bc322 Renato Botelho
						$gateway['gateway'] = "dynamic";
537 9ba87997 Phil Davis
					}
538 65cfd0ca Renato Botelho
					$gateway['dynamic'] = true;
539 7671c940 Seth Mos
				}
540 1500614c Ermal
			} else {
541
				/* getting this detection right is hard at this point because we still don't
542
				 * store the address family in the gateway item */
543 9ba87997 Phil Davis
				if (is_ipaddrv4($gateway['gateway'])) {
544 1500614c Ermal
					$gateway['ipprotocol'] = "inet";
545 9ba87997 Phil Davis
				} else if (is_ipaddrv6($gateway['gateway'])) {
546 1500614c Ermal
					$gateway['ipprotocol'] = "inet6";
547 9ba87997 Phil Davis
				}
548 2328dcc5 Seth Mos
			}
549 c1d36d26 smos
550 9ba87997 Phil Davis
			if (isset($gateway['monitor_disable'])) {
551 33c06ef7 Ermal
				$gateway['monitor_disable'] = true;
552 9ba87997 Phil Davis
			} else if (empty($gateway['monitor'])) {
553 2328dcc5 Seth Mos
				$gateway['monitor'] = $gateway['gateway'];
554 9ba87997 Phil Davis
			}
555 3d471a14 Ermal
556 883c53c9 Ermal Lu?i
			$gateway['friendlyiface'] = $gateway['interface'];
557 c1d36d26 smos
558
			/* special treatment for tunnel interfaces */
559 1500614c Ermal
			if ($gateway['ipprotocol'] == "inet6") {
560 33e71f10 jim-p
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
561 1500614c Ermal
				$interfaces_v6[$gateway['friendlyiface']] = $gateway['friendlyiface'];
562
			} else {
563 33e71f10 jim-p
				$gateway['interface'] = get_real_interface($gateway['interface'], "all", false, false);
564 1500614c Ermal
				$interfaces_v4[$gateway['friendlyiface']] = $gateway['friendlyiface'];
565
			}
566 74c834f1 smos
567
			/* entry has a default flag, use it */
568
			if (isset($gateway['defaultgw'])) {
569 1500614c Ermal
				if ($gateway['ipprotocol'] == "inet") {
570 74c834f1 smos
					$gateway['defaultgw'] = true;
571
					$found_defaultv4 = 1;
572 1500614c Ermal
				} else if ($gateway['ipprotocol'] == "inet6") {
573 74c834f1 smos
					$gateway['defaultgw'] = true;
574
					$found_defaultv6 = 1;
575
				}
576
			}
577 3d471a14 Ermal
			/* include the gateway index as the attribute */
578
			$gateway['attribute'] = $i;
579 2328dcc5 Seth Mos
580 8d848bdf Phil Davis
			/* Remember all the gateway names, even ones to be skipped because they are disabled. */
581
			/* Then we can easily know and match them later when attempting to add dynamic gateways to the list. */
582
			$gateways_arr_temp[$gateway['name']] = $gateway;
583
584
			/* skip disabled gateways if the caller has not asked for them to be returned. */
585 9ba87997 Phil Davis
			if (!($disabled === false && isset($gateway['disabled']))) {
586 8d848bdf Phil Davis
				$gateways_arr[$gateway['name']] = $gateway;
587 9ba87997 Phil Davis
			}
588 2328dcc5 Seth Mos
		}
589 c1d36d26 smos
	}
590 1500614c Ermal
	unset($gateway);
591 883c53c9 Ermal Lu?i
592 ef05ae5f Ermal
	/* Loop through all interfaces with a gateway and add it to a array */
593 9ba87997 Phil Davis
	if ($disabled == false) {
594 ef05ae5f Ermal
		$iflist = get_configured_interface_with_descr();
595 9ba87997 Phil Davis
	} else {
596 ef05ae5f Ermal
		$iflist = get_configured_interface_with_descr(false, true);
597 9ba87997 Phil Davis
	}
598 ef05ae5f Ermal
599 c3a0d2a5 Seth Mos
	/* Process/add dynamic v4 gateways. */
600 086cf944 Phil Davis
	foreach ($iflist as $ifname => $friendly) {
601
		if (!interface_has_gateway($ifname)) {
602 883c53c9 Ermal Lu?i
			continue;
603 9ba87997 Phil Davis
		}
604 3d471a14 Ermal
605 9ba87997 Phil Davis
		if (empty($config['interfaces'][$ifname])) {
606 86df2846 jim-p
			continue;
607 9ba87997 Phil Davis
		}
608 86df2846 jim-p
609
		$ifcfg = &$config['interfaces'][$ifname];
610 9ba87997 Phil Davis
		if (!isset($ifcfg['enable'])) {
611 e62fe748 Ermal
			continue;
612 9ba87997 Phil Davis
		}
613 e62fe748 Ermal
614 9ba87997 Phil Davis
		if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
615 74c834f1 smos
			continue;
616 9ba87997 Phil Davis
		}
617 3930a9c0 smos
618 9ba87997 Phil Davis
		if (isset($interfaces_v4[$ifname])) {
619 3930a9c0 smos
			continue;
620 9ba87997 Phil Davis
		}
621 f040882c Renato Botelho
622 74c834f1 smos
		$ctype = "";
623 9ba87997 Phil Davis
		switch ($ifcfg['ipaddr']) {
624 74c834f1 smos
			case "dhcp":
625
			case "pppoe":
626
			case "pptp":
627 b2ff5d17 smos
			case "ppp":
628 74c834f1 smos
				$ctype = strtoupper($ifcfg['ipaddr']);
629
				break;
630 daac437f jim-p
			default:
631 11fb4543 Chris Buechler
				$tunnelif = substr($ifcfg['if'], 0, 3);
632 6c07db48 Phil Davis
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
633 687ff78c Chris Buechler
					// if current iface is an ovpn server endpoint then check its type, skip tap only
634
					if (substr($ifcfg['if'], 4, 1) == 's') {
635
						$ovpnid = substr($ifcfg['if'], 5);
636
						if (is_array($config['openvpn']['openvpn-server'])) {
637
							foreach ($config['openvpn']['openvpn-server'] as & $ovpnserverconf) {
638
								if ($ovpnserverconf['vpnid'] == $ovpnid) {
639 9ba87997 Phil Davis
									if ($ovpnserverconf['dev_mode'] == "tap") {
640 687ff78c Chris Buechler
										continue 3;
641 9ba87997 Phil Davis
									}
642 687ff78c Chris Buechler
								}
643
							}
644
						}
645
					}
646 daac437f jim-p
					$ctype = "VPNv4";
647 9ba87997 Phil Davis
				} else if ($tunnelif == "gif" || $tunnelif == "gre") {
648 bac17444 Chris Buechler
					$ctype = "TUNNELv4";
649 9ba87997 Phil Davis
				}
650 daac437f jim-p
				break;
651 74c834f1 smos
		}
652
		$ctype = "_". strtoupper($ctype);
653
654 883c53c9 Ermal Lu?i
		$gateway = array();
655
		$gateway['dynamic'] = false;
656 c3a0d2a5 Seth Mos
		$gateway['ipprotocol'] = "inet";
657 883c53c9 Ermal Lu?i
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
658
		$gateway['interface'] = get_real_interface($ifname);
659
		$gateway['friendlyiface'] = $ifname;
660 74c834f1 smos
		$gateway['name'] = "{$friendly}{$ctype}";
661 883c53c9 Ermal Lu?i
		$gateway['attribute'] = "system";
662 f040882c Renato Botelho
663 74c834f1 smos
		if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
664 999111cb Ermal
			$gateway['defaultgw'] = true;
665
			$gateway['dynamic'] = true;
666 74c834f1 smos
			$found_defaultv4 = 1;
667 999111cb Ermal
		}
668 883c53c9 Ermal Lu?i
		/* Loopback dummy for dynamic interfaces without a IP */
669 9ba87997 Phil Davis
		if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
670 999111cb Ermal
			$gateway['gateway'] = "dynamic";
671 9ba87997 Phil Davis
		}
672 3439b033 Seth Mos
673 8d848bdf Phil Davis
		/* automatically skip known static and dynamic gateways that were previously processed */
674 9ba87997 Phil Davis
		foreach ($gateways_arr_temp as $gateway_item) {
675 c3a0d2a5 Seth Mos
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
676 918bdf0d Phil Davis
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
677 9ba87997 Phil Davis
				continue 2;
678
			}
679 883c53c9 Ermal Lu?i
		}
680 c66b4242 Seth Mos
681 9ba87997 Phil Davis
		if (is_ipaddrv4($gateway['gateway'])) {
682 883c53c9 Ermal Lu?i
			$gateway['monitor'] = $gateway['gateway'];
683 9ba87997 Phil Davis
		}
684 c65e1e0d Ermal
685 74c834f1 smos
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
686 c3a0d2a5 Seth Mos
		$gateways_arr[$gateway['name']] = $gateway;
687
	}
688 1500614c Ermal
	unset($gateway);
689 c3a0d2a5 Seth Mos
690
	/* Process/add dynamic v6 gateways. */
691 086cf944 Phil Davis
	foreach ($iflist as $ifname => $friendly) {
692 0715ad50 jim-p
		/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
693 9ba87997 Phil Davis
		if (!isset($config['system']['ipv6allow'])) {
694 0715ad50 jim-p
			break;
695 9ba87997 Phil Davis
		}
696 0715ad50 jim-p
697 086cf944 Phil Davis
		if (!interface_has_gatewayv6($ifname)) {
698 c3a0d2a5 Seth Mos
			continue;
699 9ba87997 Phil Davis
		}
700 c3a0d2a5 Seth Mos
701 9ba87997 Phil Davis
		if (empty($config['interfaces'][$ifname])) {
702 c3a0d2a5 Seth Mos
			continue;
703 9ba87997 Phil Davis
		}
704 c3a0d2a5 Seth Mos
705
		$ifcfg = &$config['interfaces'][$ifname];
706 9ba87997 Phil Davis
		if (!isset($ifcfg['enable'])) {
707 74c834f1 smos
			continue;
708 9ba87997 Phil Davis
		}
709 74c834f1 smos
710 9ba87997 Phil Davis
		if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
711 1500614c Ermal
			continue;
712 9ba87997 Phil Davis
		}
713 f040882c Renato Botelho
714 9ba87997 Phil Davis
		if (isset($interfaces_v6[$ifname])) {
715 3930a9c0 smos
			continue;
716 9ba87997 Phil Davis
		}
717 3930a9c0 smos
718 74c834f1 smos
		$ctype = "";
719 9ba87997 Phil Davis
		switch ($ifcfg['ipaddrv6']) {
720 c0b1bc81 smos
			case "slaac":
721 74c834f1 smos
			case "dhcp6":
722
			case "6to4":
723
			case "6rd":
724
				$ctype = strtoupper($ifcfg['ipaddrv6']);
725
				break;
726 daac437f jim-p
			default:
727 bf36dafa Renato Botelho
				$tunnelif = substr($ifcfg['if'], 0, 3);
728 6c07db48 Phil Davis
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
729 687ff78c Chris Buechler
					// if current iface is an ovpn server endpoint then check its type, skip tap only
730
					if (substr($ifcfg['if'], 4, 1) == 's') {
731
						$ovpnid = substr($ifcfg['if'], 5);
732
						if (is_array($config['openvpn']['openvpn-server'])) {
733
							foreach ($config['openvpn']['openvpn-server'] as & $ovpnserverconf) {
734
								if ($ovpnserverconf['vpnid'] == $ovpnid) {
735 9ba87997 Phil Davis
									if ($ovpnserverconf['dev_mode'] == "tap") {
736 687ff78c Chris Buechler
										continue 3;
737 9ba87997 Phil Davis
									}
738 687ff78c Chris Buechler
								}
739
							}
740
						}
741
					}
742 daac437f jim-p
					$ctype = "VPNv6";
743 9ba87997 Phil Davis
				} else if ($tunnelif == "gif" || $tunnelif == "gre") {
744 c32a6b82 Ermal
					$ctype = "TUNNELv6";
745 9ba87997 Phil Davis
				}
746 daac437f jim-p
				break;
747 74c834f1 smos
		}
748
		$ctype = "_". strtoupper($ctype);
749 c3a0d2a5 Seth Mos
750
		$gateway = array();
751
		$gateway['dynamic'] = false;
752
		$gateway['ipprotocol'] = "inet6";
753
		$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
754 909de400 Ermal
		$gateway['interface'] = get_real_interface($ifname, "inet6");
755 9ba87997 Phil Davis
		switch ($ifcfg['ipaddrv6']) {
756 d500e296 smos
			case "6rd":
757 909de400 Ermal
			case "6to4":
758 d500e296 smos
				$gateway['dynamic'] = "default";
759
				break;
760
		}
761 c3a0d2a5 Seth Mos
		$gateway['friendlyiface'] = $ifname;
762 74c834f1 smos
		$gateway['name'] = "{$friendly}{$ctype}";
763 c3a0d2a5 Seth Mos
		$gateway['attribute'] = "system";
764 f040882c Renato Botelho
765 6c07db48 Phil Davis
		if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) {
766 c3a0d2a5 Seth Mos
			$gateway['defaultgw'] = true;
767
			$gateway['dynamic'] = true;
768 74c834f1 smos
			$found_defaultv6 = 1;
769 c3a0d2a5 Seth Mos
		}
770
771
		/* Loopback dummy for dynamic interfaces without a IP */
772 9ba87997 Phil Davis
		if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
773 d07bc322 Renato Botelho
			$gateway['gateway'] = "dynamic";
774 9ba87997 Phil Davis
		}
775 c3a0d2a5 Seth Mos
776 8d848bdf Phil Davis
		/* automatically skip known static and dynamic gateways that were previously processed */
777 9ba87997 Phil Davis
		foreach ($gateways_arr_temp as $gateway_item) {
778 c3a0d2a5 Seth Mos
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
779 918bdf0d Phil Davis
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
780 9ba87997 Phil Davis
				continue 2;
781
			}
782 c3a0d2a5 Seth Mos
		}
783
784 9ba87997 Phil Davis
		if (is_ipaddrv6($gateway['gateway'])) {
785 c3a0d2a5 Seth Mos
			$gateway['monitor'] = $gateway['gateway'];
786 9ba87997 Phil Davis
		}
787 c3a0d2a5 Seth Mos
788 74c834f1 smos
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
789 c3a0d2a5 Seth Mos
		$gateways_arr[$gateway['name']] = $gateway;
790 c66b4242 Seth Mos
	}
791 1500614c Ermal
	unset($gateway);
792 6fdea6a2 smos
793 08c3810c Renato Botelho
	/* FIXME: Should this be enabled.
794
	 * Some interface like wan might be default but have no info recorded
795
	 * the config. */
796
	/* this is a fallback if all else fails and we want to get packets out @smos */
797
	if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
798
		foreach ($gateways_arr as &$gateway) {
799
			if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
800
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw")) {
801
					$gateway['defaultgw'] = true;
802
					$found_defaultv4 = 1;
803
				}
804
			}
805 6704590b Ermal
			else if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
806 08c3810c Renato Botelho
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgwv6")) {
807
					$gateway['defaultgw'] = true;
808
					$found_defaultv6 = 1;
809
				}
810
			}
811
		}
812
	}
813
814 9ba87997 Phil Davis
	if ($localhost === true) {
815 6fdea6a2 smos
		/* attach localhost for Null routes */
816
		$gwlo4 = array();
817
		$gwlo4['name'] = "Null4";
818
		$gwlo4['interface'] = "lo0";
819
		$gwlo4['ipprotocol'] = "inet";
820
		$gwlo4['gateway'] = "127.0.0.1";
821
		$gwlo6 = array();
822
		$gwlo6['name'] = "Null6";
823
		$gwlo6['interface'] = "lo0";
824
		$gwlo6['ipprotocol'] = "inet6";
825
		$gwlo6['gateway'] = "::1";
826
		$gateways_arr['Null4'] = $gwlo4;
827
		$gateways_arr['Null6'] = $gwlo6;
828
	}
829 c66b4242 Seth Mos
	return($gateways_arr);
830
}
831
832 fd3515f2 jim-p
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) {
833
	global $config, $g;
834
	/*
835
	 * NOTE: The code below is meant to replace the default gateway when it goes down.
836
	 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
837
	 */
838 894a0159 Ermal
	$upgw = '';
839
	$dfltgwname = '';
840 fd3515f2 jim-p
	$dfltgwdown = false;
841
	$dfltgwfound = false;
842
	foreach ($gateways_arr as $gwname => $gwsttng) {
843
		if (($gwsttng['ipprotocol'] == $ipprotocol) && isset($gwsttng['defaultgw'])) {
844
			$dfltgwfound = true;
845
			$dfltgwname = $gwname;
846 23cb4055 Chris Buechler
			if (!isset($gwsttng['monitor_disable']) && $gateways_status[$gwname]['status'] != "none") {
847 fd3515f2 jim-p
				$dfltgwdown = true;
848 9ba87997 Phil Davis
			}
849 fd3515f2 jim-p
		}
850
		/* Keep a record of the last up gateway */
851
		/* XXX: Blacklist lan for now since it might cause issues to those who have a gateway set for it */
852 23cb4055 Chris Buechler
		if (empty($upgw) && ($gwsttng['ipprotocol'] == $ipprotocol) && (isset($gwsttng['monitor_disable']) || $gateways_status[$gwname]['status'] == "none") && $gwsttng[$gwname]['friendlyiface'] != "lan") {
853 fd3515f2 jim-p
			$upgw = $gwname;
854 9ba87997 Phil Davis
		}
855
		if ($dfltgwdown == true && !empty($upgw)) {
856 fd3515f2 jim-p
			break;
857 9ba87997 Phil Davis
		}
858 fd3515f2 jim-p
	}
859
	if ($dfltgwfound == false) {
860
		$gwname = convert_friendly_interface_to_friendly_descr("wan");
861 9ba87997 Phil Davis
		if (!empty($gateways_status[$gwname]) && stristr($gateways_status[$gwname]['status'], "down")) {
862 fd3515f2 jim-p
			$dfltgwdown = true;
863 9ba87997 Phil Davis
		}
864 fd3515f2 jim-p
	}
865
	if ($dfltgwdown == true && !empty($upgw)) {
866 9ba87997 Phil Davis
		if ($gateways_arr[$upgw]['gateway'] == "dynamic") {
867 fd3515f2 jim-p
			$gateways_arr[$upgw]['gateway'] = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']);
868 9ba87997 Phil Davis
		}
869 fd3515f2 jim-p
		if (is_ipaddr($gateways_arr[$upgw]['gateway'])) {
870
			log_error("Default gateway down setting {$upgw} as default!");
871 9ba87997 Phil Davis
			if (is_ipaddrv6($gateways_arr[$upgw]['gateway'])) {
872 fd3515f2 jim-p
				$inetfamily = "-inet6";
873
			} else {
874
				$inetfamily = "-inet";
875
			}
876
			mwexec("/sbin/route change {$inetfamily} default {$gateways_arr[$upgw]['gateway']}");
877
		}
878 fea0b652 Ermal LUÇI
	} else if (!empty($dfltgwname)) {
879 96fcabaa Renato Botelho
		$defaultgw = trim(exec("/sbin/route -n get -{$ipprotocol} default | /usr/bin/awk '/gateway:/ {print $2}'"), " \n");
880 9ba87997 Phil Davis
		if ($ipprotocol == 'inet6' && !is_ipaddrv6($gateways_arr[$dfltgwname]['gateway'])) {
881 894a0159 Ermal
			return;
882 9ba87997 Phil Davis
		}
883
		if ($ipprotocol == 'inet' && !is_ipaddrv4($gateways_arr[$dfltgwname]['gateway'])) {
884 894a0159 Ermal
			return;
885 9ba87997 Phil Davis
		}
886
		if ($defaultgw != $gateways_arr[$dfltgwname]['gateway']) {
887 894a0159 Ermal
			mwexec("/sbin/route change -{$ipprotocol} default {$gateways_arr[$dfltgwname]['gateway']}");
888 9ba87997 Phil Davis
		}
889 fd3515f2 jim-p
	}
890
}
891
892 3d471a14 Ermal
/*
893
 * Return an array with all gateway groups with name as key
894 fd8eeda5 Seth Mos
 * All gateway groups will be processed before returning the array.
895 3d471a14 Ermal
 */
896 fd8eeda5 Seth Mos
function return_gateway_groups_array() {
897 962625aa Ermal
	global $config, $g;
898 fd8eeda5 Seth Mos
899
	/* fetch the current gateways status */
900 3d471a14 Ermal
	$gateways_status = return_gateways_status(true);
901 fd8eeda5 Seth Mos
	$gateways_arr = return_gateways_array();
902
	$gateway_groups_array = array();
903
904 7af360ce Ermal
	if (isset($config['system']['gw_switch_default'])) {
905 fd3515f2 jim-p
		fixup_default_gateway("inet", $gateways_status, $gateways_arr);
906
		fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
907 ee7f1647 Ermal
	}
908 fd8eeda5 Seth Mos
	if (is_array($config['gateways']['gateway_group'])) {
909 1500614c Ermal
		$carplist = get_configured_carp_interface_list();
910
		foreach ($config['gateways']['gateway_group'] as $group) {
911 5aa68a55 Renato Botelho
			/* create array with group gateways members separated by tier */
912 fd8eeda5 Seth Mos
			$tiers = array();
913 3d471a14 Ermal
			$backupplan = array();
914 1500614c Ermal
			$gwvip_arr = array();
915
			foreach ($group['item'] as $item) {
916 dea0921d Ermal
				list($gwname, $tier, $vipname) = explode("|", $item);
917 1500614c Ermal
918
				if (is_ipaddr($carplist[$vipname])) {
919 9ba87997 Phil Davis
					if (!is_array($gwvip_arr[$group['name']])) {
920 1500614c Ermal
						$gwvip_arr[$group['name']] = array();
921 9ba87997 Phil Davis
					}
922 ab1112da smos
					$gwvip_arr[$group['name']][$gwname] = $vipname;
923 1500614c Ermal
				}
924 3d471a14 Ermal
925
				/* Do it here rather than reiterating again the group in case no member is up. */
926 9ba87997 Phil Davis
				if (!is_array($backupplan[$tier])) {
927 dea0921d Ermal
					$backupplan[$tier] = array();
928 9ba87997 Phil Davis
				}
929 d8bf779b Ermal
				$backupplan[$tier][] = $gwname;
930 3d471a14 Ermal
931 fd8eeda5 Seth Mos
				/* check if the gateway is available before adding it to the array */
932 e0ba24f7 smos
				if (is_array($gateways_status[$gwname])) {
933 54b78cc1 Ermal
					$status = $gateways_status[$gwname];
934 3d471a14 Ermal
					$gwdown = false;
935 9af4b31b Ermal
					if (stristr($status['status'], "down")) {
936 b22f436a Chris Buechler
						$msg = sprintf(gettext("MONITOR: %s is down, omitting from routing group {$group['name']}"), $gwname);
937 3d471a14 Ermal
						$gwdown = true;
938
					} else if (stristr($status['status'], "loss") && strstr($group['trigger'], "loss")) {
939
						/* packet loss */
940 b22f436a Chris Buechler
						$msg = sprintf(gettext("MONITOR: %s has packet loss, omitting from routing group {$group['name']}"), $gwname);
941 3d471a14 Ermal
						$gwdown = true;
942
					} else if (stristr($status['status'], "delay") && strstr($group['trigger'] , "latency")) {
943
						/* high latency */
944 b22f436a Chris Buechler
						$msg = sprintf(gettext("MONITOR: %s has high latency, omitting from routing group {$group['name']}"), $gwname);
945 3d471a14 Ermal
						$gwdown = true;
946
					}
947
					if ($gwdown == true) {
948 9f1d61d3 Scott Ullrich
						log_error($msg);
949
						notify_via_growl($msg);
950 10c3d4c0 Scott Ullrich
						notify_via_smtp($msg);
951 e0ba24f7 smos
					} else {
952 857ce5f3 Seth Mos
						/* Online add member */
953 9ba87997 Phil Davis
						if (!is_array($tiers[$tier])) {
954 dea0921d Ermal
							$tiers[$tier] = array();
955 9ba87997 Phil Davis
						}
956 857ce5f3 Seth Mos
						$tiers[$tier][] = $gwname;
957 e0ba24f7 smos
					}
958 9ba87997 Phil Davis
				} else if (isset($gateways_arr[$gwname]['monitor_disable'])) {
959 33c06ef7 Ermal
					$tiers[$tier][] = $gwname;
960 9ba87997 Phil Davis
				}
961 fd8eeda5 Seth Mos
			}
962
			$tiers_count = count($tiers);
963 1500614c Ermal
			if ($tiers_count == 0) {
964 fd8eeda5 Seth Mos
				/* Oh dear, we have no members! Engage Plan B */
965 285ef132 Ermal LUÇI
				if (!platform_booting()) {
966 50006cca PiBa-NL
					$msg = gettext("Gateways status could not be determined, considering all as up/active. (Group: {$group['name']})");
967 9132ae35 Ermal
					log_error($msg);
968
					notify_via_growl($msg);
969 72b4a1e8 jim-p
					//notify_via_smtp($msg);
970 9132ae35 Ermal
				}
971 3d471a14 Ermal
				$tiers = $backupplan;
972 fd8eeda5 Seth Mos
			}
973 09ae0c17 Seth Mos
			/* sort the tiers array by the tier key */
974
			ksort($tiers);
975 3d471a14 Ermal
976 fd8eeda5 Seth Mos
			/* we do not really foreach the tiers as we stop after the first tier */
977 1500614c Ermal
			foreach ($tiers as $tieridx => $tier) {
978 fd8eeda5 Seth Mos
				/* process all gateways in this tier */
979 1500614c Ermal
				foreach ($tier as $member) {
980 fd8eeda5 Seth Mos
					/* determine interface gateway */
981 5f53260a Ermal
					if (isset($gateways_arr[$member])) {
982
						$gateway = $gateways_arr[$member];
983
						$int = $gateway['interface'];
984
						$gatewayip = "";
985 9ba87997 Phil Davis
						if (is_ipaddr($gateway['gateway'])) {
986 5f53260a Ermal
							$gatewayip = $gateway['gateway'];
987 9ba87997 Phil Davis
						} else if (!empty($int)) {
988 5f53260a Ermal
							$gatewayip = get_interface_gateway($gateway['friendlyiface']);
989 9ba87997 Phil Davis
						}
990 f040882c Renato Botelho
991 f60003fa Phil Davis
						if (!empty($int)) {
992 44b054ce smos
							$gateway_groups_array[$group['name']]['ipprotocol'] = $gateway['ipprotocol'];
993 f60003fa Phil Davis
							if (is_ipaddr($gatewayip)) {
994
								$groupmember = array();
995 6c07db48 Phil Davis
								$groupmember['int'] = $int;
996
								$groupmember['gwip'] = $gatewayip;
997
								$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
998 9ba87997 Phil Davis
								if (is_array($gwvip_arr[$group['name']])&& !empty($gwvip_arr[$group['name']][$member])) {
999 f60003fa Phil Davis
									$groupmember['vip'] = $gwvip_arr[$group['name']][$member];
1000 9ba87997 Phil Davis
								}
1001 f60003fa Phil Davis
								$gateway_groups_array[$group['name']][] = $groupmember;
1002
							}
1003 3d471a14 Ermal
						}
1004 fd8eeda5 Seth Mos
					}
1005
				}
1006
				/* we should have the 1st available tier now, exit stage left */
1007 9ba87997 Phil Davis
				if (count($gateway_groups_array[$group['name']]) > 0) {
1008 47c48e28 smos
					break;
1009 9ba87997 Phil Davis
				} else {
1010 1500614c Ermal
					log_error("GATEWAYS: Group {$group['name']} did not have any gateways up on tier {$tieridx}!");
1011 9ba87997 Phil Davis
				}
1012 fd8eeda5 Seth Mos
			}
1013
		}
1014
	}
1015 1500614c Ermal
1016 fe22a89b Ermal
	return ($gateway_groups_array);
1017 fd8eeda5 Seth Mos
}
1018
1019 2468ae76 Scott Ullrich
/* Update DHCP WAN Interface ip address in gateway group item */
1020
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
1021
	global $config, $g;
1022 9ba87997 Phil Davis
	foreach ($config['gateways']['gateway_item'] as & $gw) {
1023
		if ($gw['interface'] == $interface) {
1024 2468ae76 Scott Ullrich
			$current_gw = get_interface_gateway($interface);
1025 9ba87997 Phil Davis
			if ($gw['gateway'] <> $current_gw) {
1026 2468ae76 Scott Ullrich
				$gw['gateway'] = $current_gw;
1027
				$changed = true;
1028
			}
1029
		}
1030
	}
1031 9ba87997 Phil Davis
	if ($changed && $current_gw) {
1032 addc0439 Renato Botelho
		write_config(sprintf(gettext('Updating gateway group gateway for %1$s - new gateway is %2$s'), $interfac, $current_gw));
1033 9ba87997 Phil Davis
	}
1034 2468ae76 Scott Ullrich
}
1035 fd8eeda5 Seth Mos
1036 028ff8f8 Phil Davis
function lookup_gateway_ip_by_name($name, $disabled = false) {
1037 fe22a89b Ermal
1038 028ff8f8 Phil Davis
	$gateways_arr = return_gateways_array($disabled, true);
1039 f040882c Renato Botelho
	foreach ($gateways_arr as $gname => $gw) {
1040 9ba87997 Phil Davis
		if ($gw['name'] === $name || $gname === $name) {
1041 f040882c Renato Botelho
			return $gw['gateway'];
1042 9ba87997 Phil Davis
		}
1043 f040882c Renato Botelho
	}
1044 66a96e72 Ermal
1045
	return false;
1046 6dc3a5c2 Ermal Lu?i
}
1047
1048
function lookup_gateway_monitor_ip_by_name($name) {
1049 fe22a89b Ermal
1050 f040882c Renato Botelho
	$gateways_arr = return_gateways_array(false, true);
1051 fe22a89b Ermal
	if (!empty($gateways_arr[$name])) {
1052
		$gateway = $gateways_arr[$name];
1053 9ba87997 Phil Davis
		if (!is_ipaddr($gateway['monitor'])) {
1054 68f291ff Ermal
			return $gateway['gateway'];
1055 9ba87997 Phil Davis
		}
1056 6dc3a5c2 Ermal Lu?i
1057 9fd19334 gnhb
		return $gateway['monitor'];
1058 f040882c Renato Botelho
	}
1059 fe22a89b Ermal
1060 f040882c Renato Botelho
	return (false);
1061 6dc3a5c2 Ermal Lu?i
}
1062
1063
function lookup_gateway_interface_by_name($name) {
1064
1065 f040882c Renato Botelho
	$gateways_arr = return_gateways_array(false, true);
1066 fe22a89b Ermal
	if (!empty($gateways_arr[$name])) {
1067 be2a18bf Phil Davis
		$interfacegw = $gateways_arr[$name]['friendlyiface'];
1068 fe22a89b Ermal
		return ($interfacegw);
1069 f040882c Renato Botelho
	}
1070 fe22a89b Ermal
1071 f040882c Renato Botelho
	return (false);
1072 6dc3a5c2 Ermal Lu?i
}
1073
1074 a2532739 Ermal Lu?i
function get_interface_gateway($interface, &$dynamic = false) {
1075 5a5413bb Seth Mos
	global $config, $g;
1076 6dc3a5c2 Ermal Lu?i
1077 9ba87997 Phil Davis
	if (substr($interface, 0, 4) == '_vip') {
1078 2525ea04 Ermal LUÇI
		$interface = get_configured_carp_interface_list($interface, 'inet', 'iface');
1079 9ba87997 Phil Davis
	}
1080 6dc3a5c2 Ermal Lu?i
1081 89f171b0 Ermal LUÇI
	$gw = NULL;
1082 5a5413bb Seth Mos
	$gwcfg = $config['interfaces'][$interface];
1083
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
1084 9ba87997 Phil Davis
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1085
			if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
1086 5a5413bb Seth Mos
				$gw = $gateway['gateway'];
1087 66a96e72 Ermal
				break;
1088
			}
1089 5a5413bb Seth Mos
		}
1090 66a96e72 Ermal
	}
1091 6dc3a5c2 Ermal Lu?i
1092 5a5413bb Seth Mos
	// for dynamic interfaces we handle them through the $interface_router file.
1093 d3c269d3 Ermal
	if (($gw == NULL || !is_ipaddrv4($gw)) && !is_ipaddrv4($gwcfg['ipaddr'])) {
1094 5a5413bb Seth Mos
		$realif = get_real_interface($interface);
1095
		if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
1096 9ba87997 Phil Davis
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
1097 5a5413bb Seth Mos
			$dynamic = true;
1098
		}
1099 9ba87997 Phil Davis
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw")) {
1100 5a5413bb Seth Mos
			$dynamic = "default";
1101 9ba87997 Phil Davis
		}
1102 f040882c Renato Botelho
1103 5a5413bb Seth Mos
	}
1104
1105
	/* return gateway */
1106
	return ($gw);
1107
}
1108
1109
function get_interface_gateway_v6($interface, &$dynamic = false) {
1110
	global $config, $g;
1111 6dc3a5c2 Ermal Lu?i
1112 9ba87997 Phil Davis
	if (substr($interface, 0, 4) == '_vip') {
1113 89f171b0 Ermal LUÇI
		$interface = get_configured_carp_interface_list($interface, 'inet6', 'iface');
1114 9ba87997 Phil Davis
	}
1115 89f171b0 Ermal LUÇI
1116 5a5413bb Seth Mos
	$gw = NULL;
1117
	$gwcfg = $config['interfaces'][$interface];
1118 ed5c640d Seth Mos
	if (!empty($gwcfg['gatewayv6']) && is_array($config['gateways']['gateway_item'])) {
1119 9ba87997 Phil Davis
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1120
			if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
1121 5a5413bb Seth Mos
				$gw = $gateway['gateway'];
1122
				break;
1123
			}
1124
		}
1125
	}
1126
1127
	// for dynamic interfaces we handle them through the $interface_router file.
1128 d3c269d3 Ermal
	if (($gw == NULL || !is_ipaddrv6($gw)) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
1129 9ba87997 Phil Davis
		$realif = get_real_interface($interface);
1130
		if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
1131
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
1132
			$dynamic = true;
1133
		}
1134
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6")) {
1135
			$dynamic = "default";
1136
		}
1137 5a5413bb Seth Mos
	}
1138
	/* return gateway */
1139
	return ($gw);
1140 6dc3a5c2 Ermal Lu?i
}
1141
1142 318189b6 smos
/* Check a IP address against a gateway IP or name
1143
 * to verify it's address family */
1144 028ff8f8 Phil Davis
function validate_address_family($ipaddr, $gwname, $disabled = false) {
1145 318189b6 smos
	$v4ip = false;
1146
	$v6ip = false;
1147
	$v4gw = false;
1148
	$v6gw = false;
1149
1150 9ba87997 Phil Davis
	if (is_ipaddrv4($ipaddr)) {
1151 318189b6 smos
		$v4ip = true;
1152 9ba87997 Phil Davis
	}
1153
	if (is_ipaddrv6($ipaddr)) {
1154 318189b6 smos
		$v6ip = true;
1155 9ba87997 Phil Davis
	}
1156
	if (is_ipaddrv4($gwname)) {
1157 318189b6 smos
		$v4gw = true;
1158 9ba87997 Phil Davis
	}
1159
	if (is_ipaddrv6($gwname)) {
1160 318189b6 smos
		$v6gw = true;
1161 9ba87997 Phil Davis
	}
1162 318189b6 smos
1163 9ba87997 Phil Davis
	if ($v4ip && $v4gw) {
1164 318189b6 smos
		return true;
1165 9ba87997 Phil Davis
	}
1166
	if ($v6ip && $v6gw) {
1167 318189b6 smos
		return true;
1168 9ba87997 Phil Davis
	}
1169 318189b6 smos
1170
	/* still no match, carry on, lookup gateways */
1171 028ff8f8 Phil Davis
	if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname, $disabled))) {
1172 318189b6 smos
		$v4gw = true;
1173 9ba87997 Phil Davis
	}
1174 028ff8f8 Phil Davis
	if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname, $disabled))) {
1175 318189b6 smos
		$v6gw = true;
1176 9ba87997 Phil Davis
	}
1177 96cd928b smos
1178
	$gw_array = return_gateways_array();
1179 9ba87997 Phil Davis
	if (is_array($gw_array[$gwname])) {
1180
		switch ($gw_array[$gwname]['ipprotocol']) {
1181 96cd928b smos
			case "inet":
1182
				$v4gw = true;
1183
				break;
1184
			case "inet6":
1185
				$v6gw = true;
1186
				break;
1187
		}
1188 748ff86f Seth Mos
	}
1189 318189b6 smos
1190 9ba87997 Phil Davis
	if ($v4ip && $v4gw) {
1191 318189b6 smos
		return true;
1192 9ba87997 Phil Davis
	}
1193
	if ($v6ip && $v6gw) {
1194 318189b6 smos
		return true;
1195 9ba87997 Phil Davis
	}
1196 f040882c Renato Botelho
1197 318189b6 smos
	return false;
1198
}
1199
1200 cdb0df65 smos
/* check if a interface is part of a gateway group */
1201
function interface_gateway_group_member($interface) {
1202
	global $config;
1203 c447a9fe Ermal
1204 9ba87997 Phil Davis
	if (is_array($config['gateways']['gateway_group'])) {
1205 cdb0df65 smos
		$groups = $config['gateways']['gateway_group'];
1206 9ba87997 Phil Davis
	} else {
1207 cdb0df65 smos
		return false;
1208 9ba87997 Phil Davis
	}
1209 cdb0df65 smos
1210 f040882c Renato Botelho
	$gateways_arr = return_gateways_array(false, true);
1211 9ba87997 Phil Davis
	foreach ($groups as $group) {
1212
		if (is_array($group['item'])) {
1213
			foreach ($group['item'] as $item) {
1214 cdb0df65 smos
				$elements = explode("|", $item);
1215
				$gwname = $elements[0];
1216 c447a9fe Ermal
				if ($interface == $gateways_arr[$gwname]['interface']) {
1217
					unset($gateways_arr);
1218 017817c2 smos
					return true;
1219 c447a9fe Ermal
				}
1220 cdb0df65 smos
			}
1221
		}
1222
	}
1223 c447a9fe Ermal
	unset($gateways_arr);
1224
1225 cdb0df65 smos
	return false;
1226
}
1227 318189b6 smos
1228 58b1112f Ermal
function gateway_is_gwgroup_member($name) {
1229
	global $config;
1230
1231 9ba87997 Phil Davis
	if (is_array($config['gateways']['gateway_group'])) {
1232 58b1112f Ermal
		$groups = $config['gateways']['gateway_group'];
1233 9ba87997 Phil Davis
	} else {
1234 58b1112f Ermal
		return false;
1235 9ba87997 Phil Davis
	}
1236 58b1112f Ermal
1237
	$members = array();
1238 9ba87997 Phil Davis
	foreach ($groups as $group) {
1239 58b1112f Ermal
		if (is_array($group['item'])) {
1240 9ba87997 Phil Davis
			foreach ($group['item'] as $item) {
1241 58b1112f Ermal
				$elements = explode("|", $item);
1242
				$gwname = $elements[0];
1243 9ba87997 Phil Davis
				if ($name == $elements[0]) {
1244 58b1112f Ermal
					$members[] = $group['name'];
1245 9ba87997 Phil Davis
				}
1246 58b1112f Ermal
			}
1247
		}
1248
	}
1249
1250
	return $members;
1251
}
1252 23cb4055 Chris Buechler
?>