Project

General

Profile

Download (31 KB) Statistics
| Branch: | Tag: | Revision:
1 17623ab5 Bill Marquette
<?php
2
/*
3 cdcea13f Seth Mos
  Copyright (C) 2008 Bill Marquette, Seth Mos
4 02a8dab0 ccesario
  Copyright (C) 2010 Ermal Luçi
5 17623ab5 Bill Marquette
  All rights reserved.
6
7
  Redistribution and use in source and binary forms, with or without
8
  modification, are permitted provided that the following conditions are met:
9
10 c1191d5b Ermal
  1. Redistributions of source code must retain the above copyright notice,
11 17623ab5 Bill Marquette
  this list of conditions and the following disclaimer.
12
13
  2. Redistributions in binary form must reproduce the above copyright
14
  notice, this list of conditions and the following disclaimer in the
15
  documentation and/or other materials provided with the distribution.
16
17
  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
  AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
  POSSIBILITY OF SUCH DAMAGE.
27
28 bf8c7971 Ermal
	pfSense_BUILDER_BINARIES:	/sbin/route	/usr/local/sbin/apinger
29 523855b0 Scott Ullrich
	pfSense_MODULE:	routing
30
31 c1191d5b Ermal
 */
32 c5e53ee6 jim-p
require_once("config.inc");
33 17623ab5 Bill Marquette
34 14661668 Phil Davis
/* Returns an array of default values used for apinger.conf */
35
function return_apinger_defaults() {
36
	return array(
37
		"latencylow" => "200",
38
		"latencyhigh" => "500",
39
		"losslow" => "10",
40
		"losshigh" => "20",
41
		"interval" => "1",
42
		"down" => "10");
43
	}
44
45 3d471a14 Ermal
/*
46
 * Creates monitoring configuration file and
47 14661668 Phil Davis
 * adds appropriate static routes.
48 cdcea13f Seth Mos
 */
49
function setup_gateways_monitor() {
50 3d471a14 Ermal
	global $config, $g;
51 ffe76308 Seth Mos
52 3d471a14 Ermal
	$gateways_arr = return_gateways_array();
53
	if (!is_array($gateways_arr)) {
54
		log_error("No gateways to monitor. Apinger will not be run.");
55
		killbypid("{$g['varrun_path']}/apinger.pid");
56 3e3ff931 Ryan Dlugosz
		@unlink("{$g['varrun_path']}/apinger.status");
57 3d471a14 Ermal
		return;
58
	}
59 cdcea13f Seth Mos
60 14661668 Phil Davis
	$apinger_default = return_apinger_defaults();
61 cdcea13f Seth Mos
	$apingerconfig = <<<EOD
62
63
# pfSense apinger configuration file. Automatically Generated!
64
65
## User and group the pinger should run as
66 0534d60a Chris Buechler
user "root"
67
group "wheel"
68 cdcea13f Seth Mos
69
## Mailer to use (default: "/usr/lib/sendmail -t")
70 f040882c Renato Botelho
#mailer "/var/qmail/bin/qmail-inject"
71 cdcea13f Seth Mos
72
## Location of the pid-file (default: "/var/run/apinger.pid")
73
pid_file "{$g['varrun_path']}/apinger.pid"
74
75
## Format of timestamp (%s macro) (default: "%b %d %H:%M:%S")
76
#timestamp_format "%Y%m%d%H%M%S"
77
78
status {
79 14661668 Phil Davis
	## File where the status information should be written to
80 3e3ff931 Ryan Dlugosz
	file "{$g['varrun_path']}/apinger.status"
81 cdcea13f Seth Mos
	## Interval between file updates
82
	## when 0 or not set, file is written only when SIGUSR1 is received
83 ecb08c6a Ermal
	interval 5s
84 cdcea13f Seth Mos
}
85
86
########################################
87
# RRDTool status gathering configuration
88
# Interval between RRD updates
89
rrd interval 60s;
90
91 14661668 Phil Davis
## These parameters can be overridden in a specific alarm configuration
92 f040882c Renato Botelho
alarm default {
93 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' "
94 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' "
95 cdcea13f Seth Mos
	combine 10s
96
}
97
98 f040882c Renato Botelho
## "Down" alarm definition.
99 cdcea13f Seth Mos
## This alarm will be fired when target doesn't respond for 30 seconds.
100
alarm down "down" {
101 14661668 Phil Davis
	time {$apinger_default['down']}s
102 cdcea13f Seth Mos
}
103
104 f040882c Renato Botelho
## "Delay" alarm definition.
105 cdcea13f Seth Mos
## This alarm will be fired when responses are delayed more than 200ms
106
## it will be canceled, when the delay drops below 100ms
107
alarm delay "delay" {
108 14661668 Phil Davis
	delay_low {$apinger_default['latencylow']}ms
109
	delay_high {$apinger_default['latencyhigh']}ms
110 cdcea13f Seth Mos
}
111
112 f040882c Renato Botelho
## "Loss" alarm definition.
113 cdcea13f Seth Mos
## This alarm will be fired when packet loss goes over 20%
114
## it will be canceled, when the loss drops below 10%
115
alarm loss "loss" {
116 14661668 Phil Davis
	percent_low {$apinger_default['losslow']}
117
	percent_high {$apinger_default['losshigh']}
118 cdcea13f Seth Mos
}
119
120
target default {
121 f040882c Renato Botelho
	## How often the probe should be sent
122 14661668 Phil Davis
	interval {$apinger_default['interval']}s
123 f040882c Renato Botelho
124
	## How many replies should be used to compute average delay
125 cdcea13f Seth Mos
	## for controlling "delay" alarms
126
	avg_delay_samples 10
127 f040882c Renato Botelho
128 cdcea13f Seth Mos
	## How many probes should be used to compute average loss
129
	avg_loss_samples 50
130
131
	## The delay (in samples) after which loss is computed
132
	## without this delays larger than interval would be treated as loss
133
	avg_loss_delay_samples 20
134
135
	## Names of the alarms that may be generated for the target
136
	alarms "down","delay","loss"
137
138
	## Location of the RRD
139 1bce2729 Seth Mos
	#rrd file "{$g['vardb_path']}/rrd/apinger-%t.rrd"
140 cdcea13f Seth Mos
}
141
142
EOD;
143
144 74c834f1 smos
	$monitor_ips = array();
145 3d471a14 Ermal
	foreach($gateways_arr as $name => $gateway) {
146 33c06ef7 Ermal
		/* Do not monitor if such was requested */
147
		if (isset($gateway['monitor_disable']))
148
			continue;
149 3d471a14 Ermal
		if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
150
			if (is_ipaddr($gateway['gateway']))
151
				$gateway['monitor'] = $gateway['gateway'];
152
			else /* No chance to get an ip to monitor skip target. */
153 f31ab121 Ermal
				continue;
154 3d471a14 Ermal
		}
155 f31ab121 Ermal
156 f040882c Renato Botelho
		/* if the monitor address is already used before, skip */
157 74c834f1 smos
		if(in_array($gateway['monitor'], $monitor_ips))
158
			continue;
159 f040882c Renato Botelho
160 3d471a14 Ermal
		/* Interface ip is needed since apinger will bind a socket to it. */
161 640b3a9a Seth Mos
		if (is_ipaddrv4($gateway['gateway'])) {
162
			$gwifip = find_interface_ip($gateway['interface'], true);
163 e9d156fd Ermal
			if (!is_ipaddrv4($gwifip))
164
				continue; //Skip this target
165
		} else if (is_ipaddrv6($gateway['gateway'])) {
166 81a3b6f5 smos
			/* link locals really need a different src ip */
167 db7a628c Renato Botelho
			if(is_linklocal($gateway['gateway'])) {
168 81a3b6f5 smos
				$linklocal = explode("%", find_interface_ipv6_ll($gateway['interface'], true));
169
				$gwifip = $linklocal[0];
170
				$ifscope = "%". $linklocal[1];
171
			} else {
172
				$gwifip = find_interface_ipv6($gateway['interface'], true);
173
			}
174 e9d156fd Ermal
			if (!is_ipaddrv6($gwifip))
175
				continue; //Skip this target
176
		} else
177
			continue;
178 3d471a14 Ermal
179 45eb8aeb Renato Botelho
		$monitor_ips[] = $gateway['monitor'];
180 f44f8eb5 Ermal
		$apingercfg = "target \"{$gateway['monitor']}\" {\n";
181 68f291ff Ermal
		$apingercfg .= "	description \"{$name}\"\n";
182 3d471a14 Ermal
		$apingercfg .= "	srcip \"{$gwifip}\"\n";
183 f7203985 Ermal
		if (!empty($gateway['interval']) && intval($gateway['interval']) > 1)
184
			$apingercfg .= "	interval " . intval($gateway['interval']) . "s\n";
185 3d471a14 Ermal
		$alarms = "";
186 f44f8eb5 Ermal
		$alarmscfg = "";
187 3d471a14 Ermal
		$override = false;
188 02a8dab0 ccesario
		if (!empty($gateway['losslow'])) {
189 68f291ff Ermal
			$alarmscfg .= "alarm loss \"{$name}loss\" {\n";
190 3d471a14 Ermal
			$alarmscfg .= "\tpercent_low {$gateway['losslow']}\n";
191 f040882c Renato Botelho
			$alarmscfg .= "\tpercent_high {$gateway['losshigh']}\n";
192 3d471a14 Ermal
			$alarmscfg .= "}\n";
193 68f291ff Ermal
			$alarms .= "\"{$name}loss\"";
194 3d471a14 Ermal
			$override = true;
195
		} else {
196 023920e7 Ermal
			if ($override == true)
197 3d471a14 Ermal
				$alarms .= ",";
198
			$alarms .= "\"loss\"";
199
			$override = true;
200
		}
201
		if (!empty($gateway['latencylow'])) {
202 68f291ff Ermal
			$alarmscfg .= "alarm delay \"{$name}delay\" {\n";
203 3d471a14 Ermal
			$alarmscfg .= "\tdelay_low {$gateway['latencylow']}ms\n";
204
			$alarmscfg .= "\tdelay_high {$gateway['latencyhigh']}ms\n";
205
			$alarmscfg .= "}\n";
206
			if ($override == true)
207
				$alarms .= ",";
208 68f291ff Ermal
			$alarms .= "\"{$name}delay\"";
209 3d471a14 Ermal
			$override = true;
210
		} else {
211
			if ($override == true)
212
				$alarms .= ",";
213
			$alarms .= "\"delay\"";
214
			$override = true;
215
		}
216
		if (!empty($gateway['down'])) {
217 68f291ff Ermal
			$alarmscfg .= "alarm down \"{$name}down\" {\n";
218 3d471a14 Ermal
			$alarmscfg .= "\ttime {$gateway['down']}s\n";
219
			$alarmscfg .= "}\n";
220
			if ($override == true)
221
				$alarms .= ",";
222 68f291ff Ermal
			$alarms .= "\"{$name}down\"";
223 3d471a14 Ermal
			$override = true;
224
		} else {
225
			if ($override == true)
226
				$alarms .= ",";
227
			$alarms .= "\"down\"";
228
			$override = true;
229 cdcea13f Seth Mos
		}
230 3d471a14 Ermal
		if ($override == true)
231
			$apingercfg .= "\talarms override {$alarms};\n";
232
233
		$apingercfg .= "	rrd file \"{$g['vardb_path']}/rrd/{$gateway['name']}-quality.rrd\"\n";
234
		$apingercfg .= "}\n";
235
		$apingercfg .= "\n";
236
237 023920e7 Ermal
		$apingerconfig .= $alarmscfg;
238
		$apingerconfig .= $apingercfg;
239 cdcea13f Seth Mos
	}
240 de82ec90 Ermal
	@file_put_contents("{$g['varetc_path']}/apinger.conf", $apingerconfig);
241 292da16b Ermal
	unset($apingerconfig);
242 cdcea13f Seth Mos
243 20f26a50 Ermal
	if (is_dir("{$g['tmp_path']}"))
244
		chmod("{$g['tmp_path']}", 01777);
245 17649c87 Ermal
	if (!is_dir("{$g['vardb_path']}/rrd"))
246 21934843 jim-p
		mkdir("{$g['vardb_path']}/rrd", 0775);
247 17649c87 Ermal
248
	@chown("{$g['vardb_path']}/rrd", "nobody");
249
250 746f0afb Ermal
	if (isvalidpid("{$g['varrun_path']}/apinger.pid"))
251
		sigkillbypid("{$g['varrun_path']}/apinger.pid", "HUP");
252
	else {
253
		/* start a new apinger process */
254
		@unlink("{$g['varrun_path']}/apinger.status");
255
		sleep(1);
256
		mwexec_bg("/usr/local/sbin/apinger -c {$g['varetc_path']}/apinger.conf");
257
		sleep(1);
258
		sigkillbypid("{$g['varrun_path']}/apinger.pid", "USR1");
259
	}
260 4f060616 Ermal
261 cdcea13f Seth Mos
	return 0;
262
}
263
264
/* return the status of the apinger targets as a array */
265 3d471a14 Ermal
function return_gateways_status($byname = false) {
266 ae9618af Ermal
	global $config, $g;
267 cdcea13f Seth Mos
268
	$apingerstatus = array();
269 3e3ff931 Ryan Dlugosz
	if (file_exists("{$g['varrun_path']}/apinger.status")) {
270
		$apingerstatus = file("{$g['varrun_path']}/apinger.status");
271 2328dcc5 Seth Mos
	}
272 cdcea13f Seth Mos
273 67ee1ec5 Ermal Luçi
	$status = array();
274 cdcea13f Seth Mos
	foreach($apingerstatus as $line) {
275 75466131 Ermal
		$info = explode("|", $line);
276 3d471a14 Ermal
		if ($byname == false)
277
			$target = $info[0];
278
		else
279
			$target = $info[2];
280 47c48e28 smos
281 dea0921d Ermal
		$status[$target] = array();
282 3d471a14 Ermal
		$status[$target]['monitorip'] = $info[0];
283 79b7f498 Ermal
		$status[$target]['srcip'] = $info[1];
284
		$status[$target]['name'] = $info[2];
285
		$status[$target]['lastcheck'] = $info[5] ? date('r', $info[5]) : date('r');
286 603f19f0 smos
		$status[$target]['delay'] = empty($info[6]) ? "0ms" : round($info[6], 1) ."ms" ;
287
		$status[$target]['loss'] = empty($info[7]) ? "0.0%" : round($info[7], 1) . "%";
288 79b7f498 Ermal
		$status[$target]['status'] = trim($info[8]);
289 cdcea13f Seth Mos
	}
290 68f291ff Ermal
291 47c48e28 smos
	/* tack on any gateways that have monitoring disabled
292
	 * or are down, which could cause gateway groups to fail */
293 16106d2e smos
	$gateways_arr = return_gateways_array();
294
	foreach($gateways_arr as $gwitem) {
295 2ba95a31 Ermal
		if(!isset($gwitem['monitor_disable']))
296 fd34d6a9 Ermal
			continue;
297
		if(!is_ipaddr($gwitem['monitorip'])) {
298
			$realif = $gwitem['interface'];
299
			$tgtip = get_interface_gateway($realif);
300
			if (!is_ipaddr($tgtip))
301
				$tgtip = "none";
302
			$srcip = find_interface_ip($realif);
303
		} else {
304
			$tgtip = $gwitem['monitorip'];
305
			$srcip = find_interface_ip($realif);
306
		}
307
		if($byname == true)
308
			$target = $gwitem['name'];
309
		else
310
			$target = $tgtip;
311
312
		/* failsafe for down interfaces */
313
		if($target == "none") {
314
			$target = $gwitem['name'];
315
			$status[$target]['name'] = $gwitem['name'];
316
			$status[$target]['lastcheck'] = date('r');
317
			$status[$target]['delay'] = "0.0ms";
318
			$status[$target]['loss'] = "100.0%";
319
			$status[$target]['status'] = "down";
320
		} else {
321
			$status[$target]['monitorip'] = $tgtip;
322
			$status[$target]['srcip'] = $srcip;
323
			$status[$target]['name'] = $gwitem['name'];
324
			$status[$target]['lastcheck'] = date('r');
325
			$status[$target]['delay'] = "0.0ms";
326
			$status[$target]['loss'] = "0.0%";
327
			$status[$target]['status'] = "none";
328 16106d2e smos
		}
329
	}
330 cdcea13f Seth Mos
	return($status);
331 17623ab5 Bill Marquette
}
332
333 2328dcc5 Seth Mos
/* Return all configured gateways on the system */
334 7bfa645c smos
function return_gateways_array($disabled = false, $localhost = false) {
335 8d3556c2 gnhb
	global $config, $g;
336 7901dff5 Ermal Luçi
337 c66b4242 Seth Mos
	$gateways_arr = array();
338
339 74c834f1 smos
	$found_defaultv4 = 0;
340
	$found_defaultv6 = 0;
341
342 3930a9c0 smos
	$interfaces_v4 = array();
343
	$interfaces_v6 = array();
344
345 01ba0a72 Ermal Lu?i
	$i = 0;
346 3d471a14 Ermal
	/* Process/add all the configured gateways. */
347 ef05ae5f Ermal
	if (is_array($config['gateways']['gateway_item'])) {
348 1500614c Ermal
		foreach ($config['gateways']['gateway_item'] as $gateway) {
349
			if (empty($config['interfaces'][$gateway['interface']]))
350 74c834f1 smos
				continue;
351 3930a9c0 smos
			$wancfg = $config['interfaces'][$gateway['interface']];
352 1500614c Ermal
353
			/* skip disabled interfaces */
354
			if (!isset($wancfg['enable']))
355
				continue;
356 c1d36d26 smos
357 3930a9c0 smos
			/* if the gateway is dynamic and we can find the IPv4, Great! */
358 26190b11 Renato Botelho
			if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic" || $gateway['gateway'] == "dynamic6") {
359 1500614c Ermal
				if ($gateway['ipprotocol'] == "inet") {
360
					/* we know which interfaces is dynamic, this should be made a function */
361 65cfd0ca Renato Botelho
					$gateway['ipprotocol'] = "inet";
362
					$gateway['gateway'] = get_interface_gateway($gateway['interface']);
363
					/* no IP address found, set to dynamic */
364
					if (!is_ipaddrv4($gateway['gateway']))
365
						$gateway['gateway'] = "dynamic";
366
					$gateway['dynamic'] = true;
367 74c834f1 smos
				}
368 c1d36d26 smos
369 1500614c Ermal
				/* if the gateway is dynamic6 and we can find the IPv6, Great! */
370 8e78c82c Renato Botelho
				else if ($gateway['ipprotocol'] == "inet6") {
371 1500614c Ermal
					/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
372 65cfd0ca Renato Botelho
					$gateway['ipprotocol'] = "inet6";
373
					$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
374
					/* no IPv6 address found, set to dynamic6 */
375
					if (!is_ipaddrv6($gateway['gateway']))
376
						$gateway['gateway'] = "dynamic6";
377
					$gateway['dynamic'] = true;
378 7671c940 Seth Mos
				}
379 1500614c Ermal
			} else {
380
				/* getting this detection right is hard at this point because we still don't
381
				 * store the address family in the gateway item */
382
				if (is_ipaddrv4($gateway['gateway']))
383
					$gateway['ipprotocol'] = "inet";
384
				else if(is_ipaddrv6($gateway['gateway']))
385
					$gateway['ipprotocol'] = "inet6";
386 2328dcc5 Seth Mos
			}
387 c1d36d26 smos
388 33c06ef7 Ermal
			if (isset($gateway['monitor_disable']))
389
				$gateway['monitor_disable'] = true;
390
			else if (empty($gateway['monitor']))
391 2328dcc5 Seth Mos
				$gateway['monitor'] = $gateway['gateway'];
392 3d471a14 Ermal
393 883c53c9 Ermal Lu?i
			$gateway['friendlyiface'] = $gateway['interface'];
394 c1d36d26 smos
395
			/* special treatment for tunnel interfaces */
396 1500614c Ermal
			if ($gateway['ipprotocol'] == "inet6") {
397 909de400 Ermal
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet6");
398 1500614c Ermal
				$interfaces_v6[$gateway['friendlyiface']] = $gateway['friendlyiface'];
399
			} else {
400 c1d36d26 smos
				$gateway['interface'] = get_real_interface($gateway['interface']);
401 1500614c Ermal
				$interfaces_v4[$gateway['friendlyiface']] = $gateway['friendlyiface'];
402
			}
403 74c834f1 smos
404
			/* entry has a default flag, use it */
405
			if (isset($gateway['defaultgw'])) {
406 1500614c Ermal
				if ($gateway['ipprotocol'] == "inet") {
407 74c834f1 smos
					$gateway['defaultgw'] = true;
408
					$found_defaultv4 = 1;
409 1500614c Ermal
				} else if ($gateway['ipprotocol'] == "inet6") {
410 74c834f1 smos
					$gateway['defaultgw'] = true;
411
					$found_defaultv6 = 1;
412
				}
413
			}
414 3d471a14 Ermal
			/* include the gateway index as the attribute */
415
			$gateway['attribute'] = $i;
416 2328dcc5 Seth Mos
417
			$gateways_arr[$gateway['name']] = $gateway;
418
			$i++;
419
		}
420 c1d36d26 smos
	}
421 1500614c Ermal
	unset($gateway);
422 883c53c9 Ermal Lu?i
423 ef05ae5f Ermal
	/* Loop through all interfaces with a gateway and add it to a array */
424
	if ($disabled == false)
425
		$iflist = get_configured_interface_with_descr();
426
	else
427
		$iflist = get_configured_interface_with_descr(false, true);
428
429 c3a0d2a5 Seth Mos
	/* Process/add dynamic v4 gateways. */
430 883c53c9 Ermal Lu?i
	foreach($iflist as $ifname => $friendly ) {
431 3d471a14 Ermal
		if(! interface_has_gateway($ifname))
432 883c53c9 Ermal Lu?i
			continue;
433 3d471a14 Ermal
434 86df2846 jim-p
		if (empty($config['interfaces'][$ifname]))
435
			continue;
436
437
		$ifcfg = &$config['interfaces'][$ifname];
438 1500614c Ermal
		if(!isset($ifcfg['enable']))
439 e62fe748 Ermal
			continue;
440
441 1500614c Ermal
		if(!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr']))
442 74c834f1 smos
			continue;
443 3930a9c0 smos
444 1500614c Ermal
		if (isset($interfaces_v4[$ifname]))
445 3930a9c0 smos
			continue;
446 f040882c Renato Botelho
447 74c834f1 smos
		$ctype = "";
448
		switch($ifcfg['ipaddr']) {
449
			case "dhcp":
450
			case "pppoe":
451
			case "pptp":
452 b2ff5d17 smos
			case "ppp":
453 74c834f1 smos
				$ctype = strtoupper($ifcfg['ipaddr']);
454
				break;
455 daac437f jim-p
			default:
456 c822154c jim-p
				if (substr($ifcfg['if'], 0, 4) ==  "ovpn")
457 daac437f jim-p
					$ctype = "VPNv4";
458
				break;
459 74c834f1 smos
		}
460
		$ctype = "_". strtoupper($ctype);
461
462 883c53c9 Ermal Lu?i
		$gateway = array();
463
		$gateway['dynamic'] = false;
464 c3a0d2a5 Seth Mos
		$gateway['ipprotocol'] = "inet";
465 883c53c9 Ermal Lu?i
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
466
		$gateway['interface'] = get_real_interface($ifname);
467
		$gateway['friendlyiface'] = $ifname;
468 74c834f1 smos
		$gateway['name'] = "{$friendly}{$ctype}";
469 883c53c9 Ermal Lu?i
		$gateway['attribute'] = "system";
470 f040882c Renato Botelho
471 74c834f1 smos
		if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
472 999111cb Ermal
			$gateway['defaultgw'] = true;
473
			$gateway['dynamic'] = true;
474 74c834f1 smos
			$found_defaultv4 = 1;
475 999111cb Ermal
		}
476 883c53c9 Ermal Lu?i
		/* Loopback dummy for dynamic interfaces without a IP */
477 c3a0d2a5 Seth Mos
		if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true)
478 999111cb Ermal
			$gateway['gateway'] = "dynamic";
479 3439b033 Seth Mos
480 883c53c9 Ermal Lu?i
		/* automatically skip known static and dynamic gateways we have a array entry for */
481
		foreach($gateways_arr as $gateway_item) {
482 c3a0d2a5 Seth Mos
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
483
				($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))
484 7bed8222 Ermal
					continue 2;
485 883c53c9 Ermal Lu?i
		}
486 c66b4242 Seth Mos
487 c3a0d2a5 Seth Mos
		if (is_ipaddrv4($gateway['gateway']))
488 883c53c9 Ermal Lu?i
			$gateway['monitor'] = $gateway['gateway'];
489 c65e1e0d Ermal
490 74c834f1 smos
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
491 c3a0d2a5 Seth Mos
		$gateways_arr[$gateway['name']] = $gateway;
492
	}
493 1500614c Ermal
	unset($gateway);
494 c3a0d2a5 Seth Mos
495
	/* Process/add dynamic v6 gateways. */
496
	foreach($iflist as $ifname => $friendly ) {
497 0715ad50 jim-p
		/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
498
		if (!isset($config['system']['ipv6allow']))
499
			break;
500
501 67102344 smos
		if(! interface_has_gatewayv6($ifname))
502 c3a0d2a5 Seth Mos
			continue;
503
504
		if (empty($config['interfaces'][$ifname]))
505
			continue;
506
507
		$ifcfg = &$config['interfaces'][$ifname];
508 74c834f1 smos
		if(!isset($ifcfg['enable']))
509
			continue;
510
511 1500614c Ermal
		if(!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6']))
512
			continue;
513 f040882c Renato Botelho
514 1500614c Ermal
		if(isset($interfaces_v6[$ifname]))
515 3930a9c0 smos
			continue;
516
517 74c834f1 smos
		$ctype = "";
518
		switch($ifcfg['ipaddrv6']) {
519 c0b1bc81 smos
			case "slaac":
520 74c834f1 smos
			case "dhcp6":
521
			case "6to4":
522
			case "6rd":
523
				$ctype = strtoupper($ifcfg['ipaddrv6']);
524
				break;
525 daac437f jim-p
			default:
526 c822154c jim-p
				if (substr($ifcfg['if'], 0, 4) ==  "ovpn")
527 daac437f jim-p
					$ctype = "VPNv6";
528
				break;
529 74c834f1 smos
		}
530
		$ctype = "_". strtoupper($ctype);
531 c3a0d2a5 Seth Mos
532
		$gateway = array();
533
		$gateway['dynamic'] = false;
534
		$gateway['ipprotocol'] = "inet6";
535
		$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
536 909de400 Ermal
		$gateway['interface'] = get_real_interface($ifname, "inet6");
537 d500e296 smos
		switch($ifcfg['ipaddrv6']) {
538
			case "6rd":
539 909de400 Ermal
			case "6to4":
540 d500e296 smos
				$gateway['dynamic'] = "default";
541
				break;
542
		}
543 c3a0d2a5 Seth Mos
		$gateway['friendlyiface'] = $ifname;
544 74c834f1 smos
		$gateway['name'] = "{$friendly}{$ctype}";
545 c3a0d2a5 Seth Mos
		$gateway['attribute'] = "system";
546 f040882c Renato Botelho
547 74c834f1 smos
		if (($gateway['dynamic'] === "default")  && ($found_defaultv6 == 0)) {
548 c3a0d2a5 Seth Mos
			$gateway['defaultgw'] = true;
549
			$gateway['dynamic'] = true;
550 74c834f1 smos
			$found_defaultv6 = 1;
551 c3a0d2a5 Seth Mos
		}
552
553
		/* Loopback dummy for dynamic interfaces without a IP */
554
		if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true)
555 791d3ac9 Seth Mos
			$gateway['gateway'] = "dynamic6";
556 c3a0d2a5 Seth Mos
557
		/* automatically skip known static and dynamic gateways we have a array entry for */
558
		foreach($gateways_arr as $gateway_item) {
559
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
560
				($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))
561
					continue 2;
562
		}
563
564
		if (is_ipaddrv6($gateway['gateway']))
565
			$gateway['monitor'] = $gateway['gateway'];
566
567 74c834f1 smos
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
568 c3a0d2a5 Seth Mos
		$gateways_arr[$gateway['name']] = $gateway;
569 c66b4242 Seth Mos
	}
570 1500614c Ermal
	unset($gateway);
571 6fdea6a2 smos
572 08c3810c Renato Botelho
	/* FIXME: Should this be enabled.
573
	 * Some interface like wan might be default but have no info recorded
574
	 * the config. */
575
	/* this is a fallback if all else fails and we want to get packets out @smos */
576
	if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
577
		foreach ($gateways_arr as &$gateway) {
578
			if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
579
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw")) {
580
					$gateway['defaultgw'] = true;
581
					$found_defaultv4 = 1;
582
				}
583
			}
584
			if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
585
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgwv6")) {
586
					$gateway['defaultgw'] = true;
587
					$found_defaultv6 = 1;
588
				}
589
			}
590
		}
591
	}
592
593 6fdea6a2 smos
	if($localhost === true) {
594
		/* attach localhost for Null routes */
595
		$gwlo4 = array();
596
		$gwlo4['name'] = "Null4";
597
		$gwlo4['interface'] = "lo0";
598
		$gwlo4['ipprotocol'] = "inet";
599
		$gwlo4['gateway'] = "127.0.0.1";
600
		$gwlo6 = array();
601
		$gwlo6['name'] = "Null6";
602
		$gwlo6['interface'] = "lo0";
603
		$gwlo6['ipprotocol'] = "inet6";
604
		$gwlo6['gateway'] = "::1";
605
		$gateways_arr['Null4'] = $gwlo4;
606
		$gateways_arr['Null6'] = $gwlo6;
607
	}
608 c66b4242 Seth Mos
	return($gateways_arr);
609
}
610
611 fd3515f2 jim-p
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) {
612
	global $config, $g;
613
	/*
614
	 * NOTE: The code below is meant to replace the default gateway when it goes down.
615
	 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
616
	 */
617
	$upgw = "";
618
	$dfltgwdown = false;
619
	$dfltgwfound = false;
620
	foreach ($gateways_arr as $gwname => $gwsttng) {
621
		if (($gwsttng['ipprotocol'] == $ipprotocol) && isset($gwsttng['defaultgw'])) {
622
			$dfltgwfound = true;
623
			$dfltgwname = $gwname;
624
			if (!isset($gwsttng['monitor_disable']) && stristr($gateways_status[$gwname]['status'], "down"))
625
				$dfltgwdown = true;
626
		}
627
		/* Keep a record of the last up gateway */
628
		/* XXX: Blacklist lan for now since it might cause issues to those who have a gateway set for it */
629
		if (empty($upgw) && ($gwsttng['ipprotocol'] == $ipprotocol) && (isset($gwsttng['monitor_disable']) || !stristr($gateways_status[$gwname]['status'], "down")) && $gwsttng[$gwname]['friendlyiface'] != "lan")
630
			$upgw = $gwname;
631
		if ($dfltgwdown == true && !empty($upgw))
632
			break;
633
	}
634
	if ($dfltgwfound == false) {
635
		$gwname = convert_friendly_interface_to_friendly_descr("wan");
636
		if (!empty($gateways_status[$gwname]) && stristr($gateways_status[$gwname]['status'], "down"))
637
			$dfltgwdown = true;
638
	}
639
	if ($dfltgwdown == true && !empty($upgw)) {
640
		if (preg_match("/dynamic/i", $gateways_arr[$upgw]['gateway']))
641
			$gateways_arr[$upgw]['gateway'] = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']);
642
		if (is_ipaddr($gateways_arr[$upgw]['gateway'])) {
643
			log_error("Default gateway down setting {$upgw} as default!");
644
			if(is_ipaddrv6($gateways_arr[$upgw]['gateway'])) {
645
				$inetfamily = "-inet6";
646
			} else {
647
				$inetfamily = "-inet";
648
			}
649
			mwexec("/sbin/route change {$inetfamily} default {$gateways_arr[$upgw]['gateway']}");
650
		}
651
	} else {
652
		$defaultgw = trim(`/sbin/route -n get -{$ipprotocol} default | /usr/bin/grep gateway | /usr/bin/sed 's/gateway://g'`, " \n");
653
		if(is_ipaddrv6($gateways_arr[$dfltgwname]['gateway'])) {
654
			$inetfamily = "-inet6";
655
		} else {
656
			$inetfamily = "-inet";
657
		}
658
		if ($defaultgw != $gateways_arr[$dfltgwname]['gateway'])
659
			mwexec("/sbin/route change {$inetfamily} default {$gateways_arr[$dfltgwname]['gateway']}");
660
	}
661
}
662
663 3d471a14 Ermal
/*
664
 * Return an array with all gateway groups with name as key
665 fd8eeda5 Seth Mos
 * All gateway groups will be processed before returning the array.
666 3d471a14 Ermal
 */
667 fd8eeda5 Seth Mos
function return_gateway_groups_array() {
668 962625aa Ermal
	global $config, $g;
669 fd8eeda5 Seth Mos
670
	/* fetch the current gateways status */
671 3d471a14 Ermal
	$gateways_status = return_gateways_status(true);
672 fd8eeda5 Seth Mos
	$gateways_arr = return_gateways_array();
673
	$gateway_groups_array = array();
674
675 7af360ce Ermal
	if (isset($config['system']['gw_switch_default'])) {
676 fd3515f2 jim-p
		fixup_default_gateway("inet", $gateways_status, $gateways_arr);
677
		fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
678 ee7f1647 Ermal
	}
679 fd8eeda5 Seth Mos
	if (is_array($config['gateways']['gateway_group'])) {
680 1500614c Ermal
		$carplist = get_configured_carp_interface_list();
681
		foreach ($config['gateways']['gateway_group'] as $group) {
682 5aa68a55 Renato Botelho
			/* create array with group gateways members separated by tier */
683 fd8eeda5 Seth Mos
			$tiers = array();
684 3d471a14 Ermal
			$backupplan = array();
685 1500614c Ermal
			$gwvip_arr = array();
686
			foreach ($group['item'] as $item) {
687 dea0921d Ermal
				list($gwname, $tier, $vipname) = explode("|", $item);
688 1500614c Ermal
689
				if (is_ipaddr($carplist[$vipname])) {
690
					if (!is_array($group['name']))
691
						$gwvip_arr[$group['name']] = array();
692 ab1112da smos
					$gwvip_arr[$group['name']][$gwname] = $vipname;
693 1500614c Ermal
				}
694 3d471a14 Ermal
695
				/* Do it here rather than reiterating again the group in case no member is up. */
696 dea0921d Ermal
				if (!is_array($backupplan[$tier]))
697
					$backupplan[$tier] = array();
698 d8bf779b Ermal
				$backupplan[$tier][] = $gwname;
699 3d471a14 Ermal
700 fd8eeda5 Seth Mos
				/* check if the gateway is available before adding it to the array */
701 e0ba24f7 smos
				if (is_array($gateways_status[$gwname])) {
702 54b78cc1 Ermal
					$status = $gateways_status[$gwname];
703 3d471a14 Ermal
					$gwdown = false;
704 9af4b31b Ermal
					if (stristr($status['status'], "down")) {
705 50006cca PiBa-NL
						$msg = sprintf(gettext("MONITOR: %s is down, removing from routing group {$group['name']}"), $gwname);
706 3d471a14 Ermal
						$gwdown = true;
707
					} else if (stristr($status['status'], "loss") && strstr($group['trigger'], "loss")) {
708
						/* packet loss */
709 50006cca PiBa-NL
						$msg = sprintf(gettext("MONITOR: %s has packet loss, removing from routing group {$group['name']}"), $gwname);
710 3d471a14 Ermal
						$gwdown = true;
711
					} else if (stristr($status['status'], "delay") && strstr($group['trigger'] , "latency")) {
712
						/* high latency */
713 50006cca PiBa-NL
						$msg = sprintf(gettext("MONITOR: %s has high latency, removing from routing group {$group['name']}"), $gwname);
714 3d471a14 Ermal
						$gwdown = true;
715
					}
716
					if ($gwdown == true) {
717 9f1d61d3 Scott Ullrich
						log_error($msg);
718
						notify_via_growl($msg);
719 10c3d4c0 Scott Ullrich
						notify_via_smtp($msg);
720 e0ba24f7 smos
					} else {
721 857ce5f3 Seth Mos
						/* Online add member */
722 dea0921d Ermal
						if (!is_array($tiers[$tier]))
723
							$tiers[$tier] = array();
724 857ce5f3 Seth Mos
						$tiers[$tier][] = $gwname;
725 e0ba24f7 smos
					}
726 33c06ef7 Ermal
				} else if (isset($gateways_arr[$gwname]['monitor_disable']))
727
					$tiers[$tier][] = $gwname;
728 fd8eeda5 Seth Mos
			}
729
			$tiers_count = count($tiers);
730 1500614c Ermal
			if ($tiers_count == 0) {
731 fd8eeda5 Seth Mos
				/* Oh dear, we have no members! Engage Plan B */
732 9132ae35 Ermal
				if (!$g['booting']) {
733 50006cca PiBa-NL
					$msg = gettext("Gateways status could not be determined, considering all as up/active. (Group: {$group['name']})");
734 9132ae35 Ermal
					log_error($msg);
735
					notify_via_growl($msg);
736 72b4a1e8 jim-p
					//notify_via_smtp($msg);
737 9132ae35 Ermal
				}
738 3d471a14 Ermal
				$tiers = $backupplan;
739 fd8eeda5 Seth Mos
			}
740 09ae0c17 Seth Mos
			/* sort the tiers array by the tier key */
741
			ksort($tiers);
742 3d471a14 Ermal
743 fd8eeda5 Seth Mos
			/* we do not really foreach the tiers as we stop after the first tier */
744 1500614c Ermal
			foreach ($tiers as $tieridx => $tier) {
745 fd8eeda5 Seth Mos
				/* process all gateways in this tier */
746 1500614c Ermal
				foreach ($tier as $member) {
747 fd8eeda5 Seth Mos
					/* determine interface gateway */
748 5f53260a Ermal
					if (isset($gateways_arr[$member])) {
749
						$gateway = $gateways_arr[$member];
750
						$int = $gateway['interface'];
751
						$gatewayip = "";
752 f040882c Renato Botelho
						if(is_ipaddr($gateway['gateway']))
753 5f53260a Ermal
							$gatewayip = $gateway['gateway'];
754 1500614c Ermal
						else if (!empty($int))
755 5f53260a Ermal
							$gatewayip = get_interface_gateway($gateway['friendlyiface']);
756 f040882c Renato Botelho
757 1500614c Ermal
						if (!empty($int) && is_ipaddr($gatewayip)) {
758 3d471a14 Ermal
							$groupmember = array();
759
							$groupmember['int']  = $int;
760
							$groupmember['gwip']  = $gatewayip;
761
							$groupmember['weight']  = isset($gateway['weight']) ? $gateway['weight'] : 1;
762 1500614c Ermal
							if (is_array($gwvip_arr[$group['name']])&& !empty($gwvip_arr[$group['name']][$gwname]))
763 ab1112da smos
								$groupmember['vip'] = $gwvip_arr[$group['name']][$gwname];
764 44b054ce smos
							$gateway_groups_array[$group['name']]['ipprotocol'] = $gateway['ipprotocol'];
765 3d471a14 Ermal
							$gateway_groups_array[$group['name']][] = $groupmember;
766
						}
767 fd8eeda5 Seth Mos
					}
768
				}
769
				/* we should have the 1st available tier now, exit stage left */
770 1500614c Ermal
				if (count($gateway_groups_array[$group['name']]) > 0)
771 47c48e28 smos
					break;
772
				else
773 1500614c Ermal
					log_error("GATEWAYS: Group {$group['name']} did not have any gateways up on tier {$tieridx}!");
774 fd8eeda5 Seth Mos
			}
775
		}
776
	}
777 1500614c Ermal
778 fe22a89b Ermal
	return ($gateway_groups_array);
779 fd8eeda5 Seth Mos
}
780
781 2468ae76 Scott Ullrich
/* Update DHCP WAN Interface ip address in gateway group item */
782
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
783
	global $config, $g;
784 f040882c Renato Botelho
	foreach($config['gateways']['gateway_item'] as & $gw) {
785 2468ae76 Scott Ullrich
		if($gw['interface'] == $interface) {
786
			$current_gw = get_interface_gateway($interface);
787
			if($gw['gateway'] <> $current_gw) {
788
				$gw['gateway'] = $current_gw;
789
				$changed = true;
790
			}
791
		}
792
	}
793
	if($changed && $current_gw)
794 addc0439 Renato Botelho
		write_config(sprintf(gettext('Updating gateway group gateway for %1$s - new gateway is %2$s'), $interfac, $current_gw));
795 2468ae76 Scott Ullrich
}
796 fd8eeda5 Seth Mos
797 6dc3a5c2 Ermal Lu?i
function lookup_gateway_ip_by_name($name) {
798 fe22a89b Ermal
799 fbf0d4d3 smos
	$gateways_arr = return_gateways_array(false, true);
800 f040882c Renato Botelho
	foreach ($gateways_arr as $gname => $gw) {
801 d97eccf9 Renato Botelho
		if ($gw['name'] === $name || $gname === $name)
802 f040882c Renato Botelho
			return $gw['gateway'];
803
	}
804 66a96e72 Ermal
805
	return false;
806 6dc3a5c2 Ermal Lu?i
}
807
808
function lookup_gateway_monitor_ip_by_name($name) {
809 fe22a89b Ermal
810 f040882c Renato Botelho
	$gateways_arr = return_gateways_array(false, true);
811 fe22a89b Ermal
	if (!empty($gateways_arr[$name])) {
812
		$gateway = $gateways_arr[$name];
813 68f291ff Ermal
		if(!is_ipaddr($gateway['monitor']))
814
			return $gateway['gateway'];
815 6dc3a5c2 Ermal Lu?i
816 9fd19334 gnhb
		return $gateway['monitor'];
817 f040882c Renato Botelho
	}
818 fe22a89b Ermal
819 f040882c Renato Botelho
	return (false);
820 6dc3a5c2 Ermal Lu?i
}
821
822
function lookup_gateway_interface_by_name($name) {
823
824 f040882c Renato Botelho
	$gateways_arr = return_gateways_array(false, true);
825 fe22a89b Ermal
	if (!empty($gateways_arr[$name])) {
826 be2a18bf Phil Davis
		$interfacegw = $gateways_arr[$name]['friendlyiface'];
827 fe22a89b Ermal
		return ($interfacegw);
828 f040882c Renato Botelho
	}
829 fe22a89b Ermal
830 f040882c Renato Botelho
	return (false);
831 6dc3a5c2 Ermal Lu?i
}
832
833 a2532739 Ermal Lu?i
function get_interface_gateway($interface, &$dynamic = false) {
834 5a5413bb Seth Mos
	global $config, $g;
835 6dc3a5c2 Ermal Lu?i
836 5a5413bb Seth Mos
	$gw = NULL;
837 6dc3a5c2 Ermal Lu?i
838 5a5413bb Seth Mos
	$gwcfg = $config['interfaces'][$interface];
839
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
840
		foreach($config['gateways']['gateway_item'] as $gateway) {
841
			if(($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
842
				$gw = $gateway['gateway'];
843 66a96e72 Ermal
				break;
844
			}
845 5a5413bb Seth Mos
		}
846 66a96e72 Ermal
	}
847 6dc3a5c2 Ermal Lu?i
848 5a5413bb Seth Mos
	// for dynamic interfaces we handle them through the $interface_router file.
849 2feb85af Seth Mos
	if (!is_ipaddrv4($gw) && !is_ipaddrv4($gwcfg['ipaddr'])) {
850 5a5413bb Seth Mos
		$realif = get_real_interface($interface);
851
		if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
852
				$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
853
			$dynamic = true;
854
		}
855
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw"))
856
			$dynamic = "default";
857 f040882c Renato Botelho
858 5a5413bb Seth Mos
	}
859
860
	/* return gateway */
861
	return ($gw);
862
}
863
864
function get_interface_gateway_v6($interface, &$dynamic = false) {
865
	global $config, $g;
866 6dc3a5c2 Ermal Lu?i
867 5a5413bb Seth Mos
	$gw = NULL;
868
	$gwcfg = $config['interfaces'][$interface];
869 ed5c640d Seth Mos
	if (!empty($gwcfg['gatewayv6']) && is_array($config['gateways']['gateway_item'])) {
870 5a5413bb Seth Mos
		foreach($config['gateways']['gateway_item'] as $gateway) {
871 640b3a9a Seth Mos
			if(($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
872 5a5413bb Seth Mos
				$gw = $gateway['gateway'];
873
				break;
874
			}
875
		}
876
	}
877
878
	// for dynamic interfaces we handle them through the $interface_router file.
879 2feb85af Seth Mos
	if (!is_ipaddrv6($gw) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
880 5a5413bb Seth Mos
			$realif = get_real_interface($interface);
881
			if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
882
				$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
883
				$dynamic = true;
884
			}
885
			if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6"))
886
				$dynamic = "default";
887 f040882c Renato Botelho
888 5a5413bb Seth Mos
	}
889
	/* return gateway */
890
	return ($gw);
891 6dc3a5c2 Ermal Lu?i
}
892
893 318189b6 smos
/* Check a IP address against a gateway IP or name
894
 * to verify it's address family */
895
function validate_address_family($ipaddr, $gwname) {
896
	$v4ip = false;
897
	$v6ip = false;
898
	$v4gw = false;
899
	$v6gw = false;
900
901
	if(is_ipaddrv4($ipaddr))
902
		$v4ip = true;
903
	if(is_ipaddrv6($ipaddr))
904
		$v6ip = true;
905
	if(is_ipaddrv4($gwname))
906
		$v4gw = true;
907
	if(is_ipaddrv6($gwname))
908
		$v6gw = true;
909
910
	if($v4ip && $v4gw)
911
		return true;
912
	if($v6ip && $v6gw)
913
		return true;
914
915
	/* still no match, carry on, lookup gateways */
916
	if(is_ipaddrv4(lookup_gateway_ip_by_name($gwname)))
917
		$v4gw = true;
918
	if(is_ipaddrv6(lookup_gateway_ip_by_name($gwname)))
919
		$v6gw = true;
920 96cd928b smos
921
	$gw_array = return_gateways_array();
922
	if(is_array($gw_array[$gwname])) {
923
		switch($gw_array[$gwname]['ipprotocol']) {
924
			case "inet":
925
				$v4gw = true;
926
				break;
927
			case "inet6":
928
				$v6gw = true;
929
				break;
930
		}
931 748ff86f Seth Mos
	}
932 318189b6 smos
933
	if($v4ip && $v4gw)
934
		return true;
935
	if($v6ip && $v6gw)
936
		return true;
937 f040882c Renato Botelho
938 318189b6 smos
	return false;
939
}
940
941 cdb0df65 smos
/* check if a interface is part of a gateway group */
942
function interface_gateway_group_member($interface) {
943
	global $config;
944 c447a9fe Ermal
945
	if (is_array($config['gateways']['gateway_group']))
946 cdb0df65 smos
		$groups = $config['gateways']['gateway_group'];
947
	else
948
		return false;
949
950 f040882c Renato Botelho
	$gateways_arr = return_gateways_array(false, true);
951 cdb0df65 smos
	foreach($groups as $group) {
952
		if(is_array($group['item'])) {
953
			foreach($group['item'] as $item) {
954
				$elements = explode("|", $item);
955
				$gwname = $elements[0];
956 c447a9fe Ermal
				if ($interface == $gateways_arr[$gwname]['interface']) {
957
					unset($gateways_arr);
958 017817c2 smos
					return true;
959 c447a9fe Ermal
				}
960 cdb0df65 smos
			}
961
		}
962
	}
963 c447a9fe Ermal
	unset($gateways_arr);
964
965 cdb0df65 smos
	return false;
966
}
967 318189b6 smos
968 58b1112f Ermal
function gateway_is_gwgroup_member($name) {
969
	global $config;
970
971
	if (is_array($config['gateways']['gateway_group']))
972
		$groups = $config['gateways']['gateway_group'];
973
	else
974
		return false;
975
976
	$members = array();
977
	foreach($groups as $group) {
978
		if (is_array($group['item'])) {
979
			foreach($group['item'] as $item) {
980
				$elements = explode("|", $item);
981
				$gwname = $elements[0];
982
				if ($name == $elements[0])
983
					$members[] = $group['name'];
984
			}
985
		}
986
	}
987
988
	return $members;
989
}
990 c832f6bf smos
?>