Project

General

Profile

Download (37.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * gwlb.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2008 Bill Marquette, Seth Mos
7
 * Copyright (c) 2008-2016 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
require_once("config.inc");
24
require_once("rrd.inc");
25

    
26
/* Returns an array of default values used for dpinger */
27
function return_dpinger_defaults() {
28
	return array(
29
		"latencylow" => "200",
30
		"latencyhigh" => "500",
31
		"losslow" => "10",
32
		"losshigh" => "20",
33
		"interval" => "500",
34
		"loss_interval" => "2000",
35
		"time_period" => "60000",
36
		"alert_interval" => "1000",
37
		"data_payload" => "0");
38
}
39

    
40
function running_dpinger_processes() {
41
	global $g;
42

    
43
	$pidfiles = glob("{$g['varrun_path']}/dpinger_*.pid");
44

    
45
	$result = array();
46
	if ($pidfiles === FALSE) {
47
		return $result;
48
	}
49

    
50
	foreach ($pidfiles as $pidfile) {
51
		if (preg_match('/^dpinger_(.+)~([^~]+)~([^~]+)\.pid$/',
52
		    basename($pidfile), $matches)) {
53
			$socket_file = preg_replace('/\.pid$/', '.sock',
54
			    $pidfile);
55
			$result[$matches[1]] = array(
56
			    'srcip'    => $matches[2],
57
			    'targetip' => $matches[3],
58
			    'pidfile'  => $pidfile,
59
			    'socket'   => $socket_file
60
			);
61
			unset($gwinfo);
62
		}
63
	}
64

    
65
	return $result;
66
}
67

    
68
/*
69
 * Stop one or more dpinger process
70
 * default parameter $gwname is '*' that will kill all running sessions
71
 * If a gateway name is passed, only this one will be killed
72
 */
73
function stop_dpinger($gwname = '') {
74
	global $g;
75

    
76
	$running_processes = running_dpinger_processes();
77

    
78
	foreach ($running_processes as $running_gwname => $process) {
79
		if ($gwname != '' && $running_gwname != $gwname) {
80
			continue;
81
		}
82

    
83
		if (isvalidpid($process['pidfile'])) {
84
			killbypid($process['pidfile']);
85
		} else {
86
			@unlink($process['pidfile']);
87
		}
88
	}
89
}
90

    
91
function start_dpinger($gateway) {
92
	global $g;
93

    
94
	if (!isset($gateway['gwifip'])) {
95
		return;
96
	}
97

    
98
	$dpinger_defaults = return_dpinger_defaults();
99

    
100
	$prefix = "{$g['varrun_path']}/dpinger_{$gateway['name']}~" .
101
	    "{$gateway['gwifip']}~{$gateway['monitor']}";
102
	# dpinger socket path should not be longer then uaddr.sun_path
103
	if (strlen($prefix) > 95) {
104
		$prefix = "{$g['varrun_path']}/dpinger_{$gateway['name']}~" .
105
		    substr(md5($gateway['gwifip']),0,8) . "~" .
106
		    $gateway['monitor'];
107
	}
108
	$pidfile = $prefix . ".pid";
109
	$socket = $prefix . ".sock";
110
	$alarm_cmd = "{$g['etc_path']}/rc.gateway_alarm";
111

    
112
	$params  = "-S ";			/* Log warnings via syslog */
113
	$params .= "-r 0 ";			/* Disable unused reporting thread */
114
	$params .= "-i {$gateway['name']} ";	/* Identifier */
115
	$params .= "-B {$gateway['gwifip']} ";	/* Bind src address */
116
	$params .= "-p {$pidfile} ";		/* PID filename */
117
	$params .= "-u {$socket} ";		/* Status Socket */
118
	$params .= "-C \"{$alarm_cmd}\" ";	/* Command to run on alarm */
119

    
120
	$params .= "-d " .
121
	    (isset($gateway['data_payload']) && is_numeric($gateway['data_payload'])
122
	    ? $gateway['data_payload']
123
	    : $dpinger_defaults['data_payload']
124
	    ) . " ";
125

    
126
	$params .= "-s " .
127
	    (isset($gateway['interval']) && is_numeric($gateway['interval'])
128
	    ? $gateway['interval']
129
	    : $dpinger_defaults['interval']
130
	    ) . " ";
131

    
132
	$params .= "-l " .
133
	    (isset($gateway['loss_interval']) && is_numeric($gateway['loss_interval'])
134
	    ?  $gateway['loss_interval']
135
	    : $dpinger_defaults['loss_interval']
136
	    ) . " ";
137

    
138
	$params .= "-t " .
139
	    (isset($gateway['time_period']) && is_numeric($gateway['time_period'])
140
	    ?  $gateway['time_period']
141
	    : $dpinger_defaults['time_period']
142
	    ) . " ";
143

    
144
	$params .= "-A " .
145
	    (isset($gateway['alert_interval']) && is_numeric($gateway['alert_interval'])
146
	    ?  $gateway['alert_interval']
147
	    : $dpinger_defaults['alert_interval']
148
	    ) . " ";
149

    
150
	$params .= "-D " .
151
	    (isset($gateway['latencyhigh']) && is_numeric($gateway['latencyhigh'])
152
	    ?  $gateway['latencyhigh']
153
	    : $dpinger_defaults['latencyhigh']
154
	    ) . " ";
155

    
156
	$params .= "-L " .
157
	    (isset($gateway['losshigh']) && is_numeric($gateway['losshigh'])
158
	    ?  $gateway['losshigh']
159
	    : $dpinger_defaults['losshigh']
160
	    ) . " ";
161

    
162
	/* Make sure we don't end up with 2 process for the same GW */
163
	stop_dpinger($gateway['name']);
164

    
165
	/* Redirect stdout to /dev/null to avoid exec() to wait for dpinger */
166
	return mwexec("/usr/local/bin/dpinger {$params} {$gateway['monitor']} >/dev/null");
167
}
168

    
169
/*
170
 * Starts dpinger processes and adds appropriate static routes for monitor IPs
171
 */
172
function setup_gateways_monitor() {
173
	global $config, $g;
174

    
175
	$gateways_arr = return_gateways_array();
176
	if (!is_array($gateways_arr)) {
177
		log_error(gettext("No gateways to monitor. dpinger will not run."));
178
		stop_dpinger();
179
		return;
180
	}
181

    
182
	$monitor_ips = array();
183
	foreach ($gateways_arr as $gwname => $gateway) {
184
		/* Do not monitor if such was requested */
185
		if (isset($gateway['monitor_disable'])) {
186
			continue;
187
		}
188
		if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
189
			if (is_ipaddr($gateway['gateway'])) {
190
				$gateways_arr[$gwname]['monitor'] = $gateway['gateway'];
191
			} else { /* No chance to get an ip to monitor skip target. */
192
				continue;
193
			}
194
		}
195

    
196
		/* if the monitor address is already used before, skip */
197
		if (in_array($gateway['monitor'], $monitor_ips)) {
198
			continue;
199
		}
200

    
201
		/* Interface ip is needed since dpinger will bind a socket to it.
202
		 * However the config GUI should already have checked this and when
203
		 * PPPoE is used the IP address is set to "dynamic". So using is_ipaddrv4
204
		 * or is_ipaddrv6 to identify packet type would be wrong, especially as
205
		 * further checks (that can cope with the "dynamic" case) are present inside
206
		 * the if block. So using $gateway['ipprotocol'] is the better option.
207
		 */
208
		if ($gateway['ipprotocol'] == "inet") { // This is an IPv4 gateway...
209
			$gwifip = find_interface_ip($gateway['interface'], true);
210
			if (!is_ipaddrv4($gwifip)) {
211
				continue; //Skip this target
212
			}
213

    
214
			if ($gwifip == "0.0.0.0") {
215
				continue; //Skip this target - the gateway is still waiting for DHCP
216
			}
217

    
218
			/*
219
			 * If the gateway is the same as the monitor we do not add a
220
			 * route as this will break the routing table.
221
			 * Add static routes for each gateway with their monitor IP
222
			 * not strictly necessary but is a added level of protection.
223
			 */
224
			if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
225
				log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
226
				$route_to = "-host {$gateway['monitor']}";
227
				if (interface_isppp_type($gateway['friendlyiface'])) {
228
					route_add_or_change("{$route_to} -iface {$gateway['interface']}");
229
				} else {
230
					route_add_or_change("{$route_to} {$gateway['gateway']}");
231
				}
232

    
233
				pfSense_kill_states("0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmp");
234
			}
235
		} else if ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
236
			if (is_linklocal($gateway['gateway']) &&
237
			    get_ll_scope($gateway['gateway']) == '') {
238
				$gateway['gateway'] .= '%' . $gateway['interface'];
239
			}
240

    
241
			if (is_linklocal($gateway['monitor'])) {
242
				if (get_ll_scope($gateway['monitor']) == '') {
243
					$gateways_arr[$gwname]['monitor'] .= '%' . $gateway['interface'];
244
				}
245

    
246
				$gwifip = find_interface_ipv6_ll($gateway['interface'], true);
247

    
248
				if (get_ll_scope($gwifip) == '') {
249
					$gwifip .= '%' . $gateway['interface'];
250
				}
251
			} else {
252
				$gwifip = find_interface_ipv6($gateway['interface'], true);
253
			}
254

    
255
			if (!is_ipaddrv6($gwifip)) {
256
				continue; //Skip this target
257
			}
258

    
259
			/*
260
			 * If the gateway is the same as the monitor we do not add a
261
			 * route as this will break the routing table.
262
			 * Add static routes for each gateway with their monitor IP
263
			 * not strictly necessary but is a added level of protection.
264
			 */
265
			if ($gateway['gateway'] != $gateway['monitor']) {
266
				log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
267
				$route_to = "-host -inet6 {$gateway['monitor']}";
268
				if (interface_isppp_type($gateway['friendlyiface'])) {
269
					route_add_or_change("{$route_to} -iface {$gateway['interface']}");
270
				} else {
271
					route_add_or_change("{$route_to} {$gateway['gateway']}");
272
				}
273

    
274
				pfSense_kill_states("::0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmpv6");
275
			}
276
		} else {
277
			continue;
278
		}
279

    
280
		$monitor_ips[] = $gateway['monitor'];
281
		$gateways_arr[$gwname]['enable_dpinger'] = true;
282
		$gateways_arr[$gwname]['gwifip'] = $gwifip;
283
	}
284

    
285
	stop_dpinger();
286

    
287
	/* Start new processes */
288
	foreach ($gateways_arr as $gateway) {
289
		if (!isset($gateway['enable_dpinger'])) {
290
			continue;
291
		}
292

    
293
		if (start_dpinger($gateway) != 0) {
294
			log_error(sprintf(gettext("Error starting gateway monitor for %s"), $gateway['name']));
295
		}
296
	}
297

    
298
	return;
299
}
300

    
301
function get_dpinger_status($gwname) {
302
	global $g;
303

    
304
	$running_processes = running_dpinger_processes();
305

    
306
	if (!isset($running_processes[$gwname])) {
307
		log_error(sprintf(gettext('dpinger: No dpinger session running for gateway %s'), $gwname));
308
		return false;
309
	}
310

    
311
	$proc = $running_processes[$gwname];
312
	unset($running_processes);
313

    
314
	if (!file_exists($proc['socket'])) {
315
		log_error("dpinger: status socket {$proc['socket']} not found");
316
		return false;
317
	}
318

    
319
	$fp = stream_socket_client("unix://{$proc['socket']}", $errno, $errstr, 10);
320
	if (!$fp) {
321
		log_error(sprintf(gettext('dpinger: cannot connect to status socket %1$s - %2$s (%3$s)'), $proc['socket'], $errstr, $errno));
322
		return false;
323
	}
324

    
325
	$status = '';
326
	while (!feof($fp)) {
327
		$status .= fgets($fp, 1024);
328
	}
329
	fclose($fp);
330

    
331
	$r = array();
332
	list(
333
	    $r['gwname'],
334
	    $r['latency_avg'],
335
	    $r['latency_stddev'],
336
	    $r['loss']
337
	) = explode(' ', preg_replace('/\n/', '', $status));
338

    
339
	$r['srcip'] = $proc['srcip'];
340
	$r['targetip'] = $proc['targetip'];
341

    
342
	$gateways_arr = return_gateways_array();
343
	unset($gw);
344
	if (isset($gateways_arr[$gwname])) {
345
		$gw = $gateways_arr[$gwname];
346
	}
347

    
348
	$r['latency_avg'] = round($r['latency_avg']/1000, 3);
349
	$r['latency_stddev'] = round($r['latency_stddev']/1000, 3);
350

    
351
	$r['status'] = "none";
352
	if (isset($gw) && isset($gw['force_down'])) {
353
		$r['status'] = "force_down";
354
	} else if (isset($gw)) {
355
		$settings = return_dpinger_defaults();
356

    
357
		$keys = array(
358
		    'latencylow',
359
		    'latencyhigh',
360
		    'losslow',
361
		    'losshigh'
362
		);
363

    
364
		/* Replace default values by user-defined */
365
		foreach ($keys as $key) {
366
			if (isset($gw[$key]) && is_numeric($gw[$key])) {
367
				$settings[$key] = $gw[$key];
368
			}
369
		}
370

    
371
		if ($r['latency_avg'] > $settings['latencyhigh'] ||
372
		    $r['loss'] > $settings['losshigh']) {
373
			$r['status'] = "down";
374
		} else if ($r['latency_avg'] > $settings['latencylow']) {
375
			$r['status'] = "delay";
376
		} else if ($r['loss'] > $settings['losslow']) {
377
			$r['status'] = "loss";
378
		}
379
	}
380

    
381
	return $r;
382
}
383

    
384
/* return the status of the dpinger targets as an array */
385
function return_gateways_status($byname = false) {
386
	global $config, $g;
387

    
388
	$dpinger_gws = running_dpinger_processes();
389
	$status = array();
390

    
391
	$gateways_arr = return_gateways_array();
392

    
393
	foreach ($dpinger_gws as $gwname => $gwdata) {
394
		$dpinger_status = get_dpinger_status($gwname);
395
		if ($dpinger_status === false) {
396
			continue;
397
		}
398

    
399
		if ($byname == false) {
400
			$target = $dpinger_status['targetip'];
401
		} else {
402
			$target = $gwname;
403
		}
404

    
405
		$status[$target] = array();
406
		$status[$target]['monitorip'] = $dpinger_status['targetip'];
407
		$status[$target]['srcip'] = $dpinger_status['srcip'];
408
		$status[$target]['name'] = $gwname;
409
		$status[$target]['delay'] = empty($dpinger_status['latency_avg']) ? "0ms" : $dpinger_status['latency_avg'] . "ms";
410
		$status[$target]['stddev'] = empty($dpinger_status['latency_stddev']) ? "0ms" : $dpinger_status['latency_stddev'] . "ms";
411
		$status[$target]['loss'] = empty($dpinger_status['loss']) ? "0.0%" : round($dpinger_status['loss'], 1) . "%";
412
		$status[$target]['status'] = $dpinger_status['status'];
413
	}
414

    
415
	/* tack on any gateways that have monitoring disabled
416
	 * or are down, which could cause gateway groups to fail */
417
	$gateways_arr = return_gateways_array();
418
	foreach ($gateways_arr as $gwitem) {
419
		if (!isset($gwitem['monitor_disable'])) {
420
			continue;
421
		}
422
		if (!is_ipaddr($gwitem['monitor'])) {
423
			$realif = $gwitem['interface'];
424
			$tgtip = get_interface_gateway($realif);
425
			if (!is_ipaddr($tgtip)) {
426
				$tgtip = "none";
427
			}
428
			$srcip = find_interface_ip($realif);
429
		} else {
430
			$tgtip = $gwitem['monitor'];
431
			$srcip = find_interface_ip($realif);
432
		}
433
		if ($byname == true) {
434
			$target = $gwitem['name'];
435
		} else {
436
			$target = $tgtip;
437
		}
438

    
439
		/* failsafe for down interfaces */
440
		if ($target == "none") {
441
			$target = $gwitem['name'];
442
			$status[$target]['name'] = $gwitem['name'];
443
			$status[$target]['delay'] = "0.0ms";
444
			$status[$target]['loss'] = "100.0%";
445
			$status[$target]['status'] = "down";
446
		} else {
447
			$status[$target]['monitorip'] = $tgtip;
448
			$status[$target]['srcip'] = $srcip;
449
			$status[$target]['name'] = $gwitem['name'];
450
			$status[$target]['delay'] = "";
451
			$status[$target]['loss'] = "";
452
			$status[$target]['status'] = "none";
453
		}
454
	}
455
	return($status);
456
}
457

    
458
/* Return all configured gateways on the system */
459
function return_gateways_array($disabled = false, $localhost = false, $inactive = false) {
460
	global $config, $g;
461

    
462
	$gateways_arr = array();
463
	$gateways_arr_temp = array();
464

    
465
	$found_defaultv4 = 0;
466
	$found_defaultv6 = 0;
467

    
468
	// Ensure the interface cache is up to date first
469
	$interfaces = get_interface_arr(true);
470

    
471
	$i = -1;
472
	/* Process/add all the configured gateways. */
473
	if (is_array($config['gateways']['gateway_item'])) {
474
		foreach ($config['gateways']['gateway_item'] as $gateway) {
475
			/* Increment it here to do not skip items */
476
			$i++;
477

    
478
			if (empty($config['interfaces'][$gateway['interface']])) {
479
				if ($inactive === false) {
480
					continue;
481
				} else {
482
					$gateway['inactive'] = true;
483
				}
484
			}
485
			$wancfg = $config['interfaces'][$gateway['interface']];
486

    
487
			/* skip disabled interfaces */
488
			if ($disabled === false && (!isset($wancfg['enable']))) {
489
				continue;
490
			}
491

    
492
			/* if the gateway is dynamic and we can find the IPv4, Great! */
493
			if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
494
				if ($gateway['ipprotocol'] == "inet") {
495
					/* we know which interfaces is dynamic, this should be made a function */
496
					$gateway['gateway'] = get_interface_gateway($gateway['interface']);
497
					/* no IP address found, set to dynamic */
498
					if (!is_ipaddrv4($gateway['gateway'])) {
499
						$gateway['gateway'] = "dynamic";
500
					}
501
					$gateway['dynamic'] = true;
502
				}
503

    
504
				/* if the gateway is dynamic and we can find the IPv6, Great! */
505
				else if ($gateway['ipprotocol'] == "inet6") {
506
					/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
507
					$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
508
					/* no IPv6 address found, set to dynamic */
509
					if (!is_ipaddrv6($gateway['gateway'])) {
510
						$gateway['gateway'] = "dynamic";
511
					}
512
					$gateway['dynamic'] = true;
513
				}
514
			} else {
515
				/* getting this detection right is hard at this point because we still don't
516
				 * store the address family in the gateway item */
517
				if (is_ipaddrv4($gateway['gateway'])) {
518
					$gateway['ipprotocol'] = "inet";
519
				} else if (is_ipaddrv6($gateway['gateway'])) {
520
					$gateway['ipprotocol'] = "inet6";
521
				}
522
			}
523

    
524
			if (isset($gateway['monitor_disable'])) {
525
				$gateway['monitor_disable'] = true;
526
			} else if (empty($gateway['monitor'])) {
527
				$gateway['monitor'] = $gateway['gateway'];
528
			}
529

    
530
			$gateway['friendlyiface'] = $gateway['interface'];
531

    
532
			/* special treatment for tunnel interfaces */
533
			if ($gateway['ipprotocol'] == "inet6") {
534
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
535
			} else {
536
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet", false, false);
537
			}
538

    
539
			/* entry has a default flag, use it */
540
			if (isset($gateway['defaultgw'])) {
541
				if ($gateway['ipprotocol'] == "inet") {
542
					$gateway['defaultgw'] = true;
543
					$found_defaultv4 = 1;
544
				} else if ($gateway['ipprotocol'] == "inet6") {
545
					$gateway['defaultgw'] = true;
546
					$found_defaultv6 = 1;
547
				}
548
			}
549
			/* include the gateway index as the attribute */
550
			$gateway['attribute'] = $i;
551

    
552
			/* Remember all the gateway names, even ones to be skipped because they are disabled. */
553
			/* Then we can easily know and match them later when attempting to add dynamic gateways to the list. */
554
			$gateways_arr_temp[$gateway['name']] = $gateway;
555

    
556
			/* skip disabled gateways if the caller has not asked for them to be returned. */
557
			if (!($disabled === false && isset($gateway['disabled']))) {
558
				$gateways_arr[$gateway['name']] = $gateway;
559
			}
560
		}
561
	}
562
	unset($gateway);
563

    
564
	/* Loop through all interfaces with a gateway and add it to a array */
565
	if ($disabled == false) {
566
		$iflist = get_configured_interface_with_descr();
567
	} else {
568
		$iflist = get_configured_interface_with_descr(false, true);
569
	}
570

    
571
	/* Process/add dynamic v4 gateways. */
572
	foreach ($iflist as $ifname => $friendly) {
573
		if (!interface_has_gateway($ifname)) {
574
			continue;
575
		}
576

    
577
		if (empty($config['interfaces'][$ifname])) {
578
			continue;
579
		}
580

    
581
		$ifcfg = &$config['interfaces'][$ifname];
582
		if (!isset($ifcfg['enable'])) {
583
			continue;
584
		}
585

    
586
		if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
587
			continue;
588
		}
589

    
590
		$ctype = "";
591
		switch ($ifcfg['ipaddr']) {
592
			case "dhcp":
593
			case "pppoe":
594
			case "l2tp":
595
			case "pptp":
596
			case "ppp":
597
				$ctype = strtoupper($ifcfg['ipaddr']);
598
				break;
599
			default:
600
				$tunnelif = substr($ifcfg['if'], 0, 3);
601
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
602
					// if current iface is an ovpn server endpoint then check its type, skip tap only
603
					if (substr($ifcfg['if'], 4, 1) == 's') {
604
						$ovpnid = substr($ifcfg['if'], 5);
605
						if (is_array($config['openvpn']['openvpn-server'])) {
606
							foreach ($config['openvpn']['openvpn-server'] as & $ovpnserverconf) {
607
								if ($ovpnserverconf['vpnid'] == $ovpnid) {
608
									if ($ovpnserverconf['dev_mode'] == "tap") {
609
										continue 3;
610
									}
611
								}
612
							}
613
						}
614
					}
615
					$ctype = "VPNv4";
616
				} else if ($tunnelif == "gif" || $tunnelif == "gre") {
617
					$ctype = "TUNNELv4";
618
				}
619
				break;
620
		}
621
		$ctype = "_". strtoupper($ctype);
622

    
623
		$gateway = array();
624
		$gateway['dynamic'] = false;
625
		$gateway['ipprotocol'] = "inet";
626
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
627
		$gateway['interface'] = get_real_interface($ifname);
628
		$gateway['friendlyiface'] = $ifname;
629
		$gateway['name'] = "{$friendly}{$ctype}";
630
		$gateway['attribute'] = "system";
631

    
632
		if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
633
			$gateway['defaultgw'] = true;
634
			$gateway['dynamic'] = true;
635
			$found_defaultv4 = 1;
636
		}
637
		/* Loopback dummy for dynamic interfaces without a IP */
638
		if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
639
			$gateway['gateway'] = "dynamic";
640
		}
641

    
642
		/* automatically skip known static and dynamic gateways that were previously processed */
643
		foreach ($gateways_arr_temp as $gateway_item) {
644
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
645
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
646
				continue 2;
647
			}
648
		}
649

    
650
		if (is_ipaddrv4($gateway['gateway'])) {
651
			$gateway['monitor'] = $gateway['gateway'];
652
		}
653

    
654
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
655
		$gateways_arr[$gateway['name']] = $gateway;
656
	}
657
	unset($gateway);
658

    
659
	/* Process/add dynamic v6 gateways. */
660
	foreach ($iflist as $ifname => $friendly) {
661
		/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
662
		if (!isset($config['system']['ipv6allow'])) {
663
			break;
664
		}
665

    
666
		if (!interface_has_gatewayv6($ifname)) {
667
			continue;
668
		}
669

    
670
		if (empty($config['interfaces'][$ifname])) {
671
			continue;
672
		}
673

    
674
		$ifcfg = &$config['interfaces'][$ifname];
675
		if (!isset($ifcfg['enable'])) {
676
			continue;
677
		}
678

    
679
		if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
680
			continue;
681
		}
682

    
683
		$ctype = "";
684
		switch ($ifcfg['ipaddrv6']) {
685
			case "slaac":
686
			case "dhcp6":
687
			case "6to4":
688
			case "6rd":
689
				$ctype = strtoupper($ifcfg['ipaddrv6']);
690
				break;
691
			default:
692
				$tunnelif = substr($ifcfg['if'], 0, 3);
693
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
694
					// if current iface is an ovpn server endpoint then check its type, skip tap only
695
					if (substr($ifcfg['if'], 4, 1) == 's') {
696
						$ovpnid = substr($ifcfg['if'], 5);
697
						if (is_array($config['openvpn']['openvpn-server'])) {
698
							foreach ($config['openvpn']['openvpn-server'] as & $ovpnserverconf) {
699
								if ($ovpnserverconf['vpnid'] == $ovpnid) {
700
									if ($ovpnserverconf['dev_mode'] == "tap") {
701
										continue 3;
702
									}
703
								}
704
							}
705
						}
706
					}
707
					$ctype = "VPNv6";
708
				} else if ($tunnelif == "gif" || $tunnelif == "gre") {
709
					$ctype = "TUNNELv6";
710
				}
711
				break;
712
		}
713
		$ctype = "_". strtoupper($ctype);
714

    
715
		$gateway = array();
716
		$gateway['dynamic'] = false;
717
		$gateway['ipprotocol'] = "inet6";
718
		$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
719
		$gateway['interface'] = get_real_interface($ifname, "inet6");
720
		switch ($ifcfg['ipaddrv6']) {
721
			case "6rd":
722
			case "6to4":
723
				$gateway['dynamic'] = "default";
724
				break;
725
		}
726
		$gateway['friendlyiface'] = $ifname;
727
		$gateway['name'] = "{$friendly}{$ctype}";
728
		$gateway['attribute'] = "system";
729

    
730
		if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) {
731
			$gateway['defaultgw'] = true;
732
			$gateway['dynamic'] = true;
733
			$found_defaultv6 = 1;
734
		}
735

    
736
		/* Loopback dummy for dynamic interfaces without a IP */
737
		if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
738
			$gateway['gateway'] = "dynamic";
739
		}
740

    
741
		/* automatically skip known static and dynamic gateways that were previously processed */
742
		foreach ($gateways_arr_temp as $gateway_item) {
743
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
744
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
745
				continue 2;
746
			}
747
		}
748

    
749
		if (is_ipaddrv6($gateway['gateway'])) {
750
			$gateway['monitor'] = $gateway['gateway'];
751
		}
752

    
753
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
754
		$gateways_arr[$gateway['name']] = $gateway;
755
	}
756
	unset($gateway);
757

    
758
	/* FIXME: Should this be enabled.
759
	 * Some interface like wan might be default but have no info recorded
760
	 * the config. */
761
	/* this is a fallback if all else fails and we want to get packets out @smos */
762
	if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
763
		foreach ($gateways_arr as &$gateway) {
764
			if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
765
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw")) {
766
					$gateway['defaultgw'] = true;
767
					$found_defaultv4 = 1;
768
				}
769
			}
770
			else if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
771
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgwv6")) {
772
					$gateway['defaultgw'] = true;
773
					$found_defaultv6 = 1;
774
				}
775
			}
776
		}
777
	}
778

    
779
	if ($localhost === true) {
780
		/* attach localhost for Null routes */
781
		$gwlo4 = array();
782
		$gwlo4['name'] = "Null4";
783
		$gwlo4['interface'] = "lo0";
784
		$gwlo4['ipprotocol'] = "inet";
785
		$gwlo4['gateway'] = "127.0.0.1";
786
		$gwlo6 = array();
787
		$gwlo6['name'] = "Null6";
788
		$gwlo6['interface'] = "lo0";
789
		$gwlo6['ipprotocol'] = "inet6";
790
		$gwlo6['gateway'] = "::1";
791
		$gateways_arr['Null4'] = $gwlo4;
792
		$gateways_arr['Null6'] = $gwlo6;
793
	}
794
	return($gateways_arr);
795
}
796

    
797
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) {
798
	global $config, $g;
799
	/*
800
	 * NOTE: The code below is meant to replace the default gateway when it goes down.
801
	 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
802
	 */
803
	$upgw = '';
804
	$dfltgwname = '';
805
	$dfltgwdown = false;
806
	$dfltgwfound = false;
807
	foreach ($gateways_arr as $gwname => $gwsttng) {
808
		if (($gwsttng['ipprotocol'] == $ipprotocol) && isset($gwsttng['defaultgw'])) {
809
			$dfltgwfound = true;
810
			$dfltgwname = $gwname;
811
			if (!isset($gwsttng['monitor_disable']) && $gateways_status[$gwname]['status'] != "none") {
812
				$dfltgwdown = true;
813
			}
814
		}
815
		/* Keep a record of the last up gateway */
816
		/* XXX: Blacklist lan for now since it might cause issues to those who have a gateway set for it */
817
		if (empty($upgw) && ($gwsttng['ipprotocol'] == $ipprotocol) && (isset($gwsttng['monitor_disable']) || $gateways_status[$gwname]['status'] == "none") && $gwsttng[$gwname]['friendlyiface'] != "lan") {
818
			$upgw = $gwname;
819
		}
820
		if ($dfltgwdown == true && !empty($upgw)) {
821
			break;
822
		}
823
	}
824
	if ($dfltgwfound == false) {
825
		$gwname = convert_friendly_interface_to_friendly_descr("wan");
826
		if (!empty($gateways_status[$gwname]) && stristr($gateways_status[$gwname]['status'], "down")) {
827
			$dfltgwdown = true;
828
		}
829
	}
830
	if ($dfltgwdown == true && !empty($upgw)) {
831
		if ($gateways_arr[$upgw]['gateway'] == "dynamic") {
832
			$gateways_arr[$upgw]['gateway'] = get_interface_gateway($gateways_arr[$upgw]['friendlyiface']);
833
		}
834
		if (is_ipaddr($gateways_arr[$upgw]['gateway'])) {
835
			log_error("Default gateway down setting {$upgw} as default!");
836
			if (is_ipaddrv6($gateways_arr[$upgw]['gateway'])) {
837
				$inetfamily = "-inet6";
838
				if (is_linklocal($gateways_arr[$upgw]['gateway']) && get_ll_scope($gateways_arr[$upgw]['gateway']) == '') {
839
					$gateways_arr[$upgw]['gateway'] .= "%" . $gateways_arr[$upgw]['interface'];
840
				}
841
			} else {
842
				$inetfamily = "-inet";
843
			}
844
			route_add_or_change("{$inetfamily} default {$gateways_arr[$upgw]['gateway']}");
845
		}
846
	} else if (!empty($dfltgwname)) {
847
		$defaultgw = trim(exec("/sbin/route -n get -{$ipprotocol} default | /usr/bin/awk '/gateway:/ {print $2}'"), " \n");
848
		if ($ipprotocol == 'inet6' && !is_ipaddrv6($gateways_arr[$dfltgwname]['gateway'])) {
849
			return;
850
		}
851
		if ($ipprotocol == 'inet' && !is_ipaddrv4($gateways_arr[$dfltgwname]['gateway'])) {
852
			return;
853
		}
854
		if ($ipprotocol == 'inet6') {
855
			if (is_linklocal($gateways_arr[$upgw]['gateway']) && get_ll_scope($gateways_arr[$upgw]['gateway']) == '') {
856
				$gateways_arr[$upgw]['gateway'] .= "%" . $gateways_arr[$upgw]['interface'];
857
			}
858
			if (is_linklocal($gateways_arr[$dfltgwname]['gateway']) && get_ll_scope($gateways_arr[$dfltgwname]['gateway']) == '') {
859
				$gateways_arr[$dfltgwname]['gateway'] .= "%" . $gateways_arr[$dfltgwname]['interface'];
860
			}
861
		}
862
		if ($defaultgw != $gateways_arr[$dfltgwname]['gateway']) {
863
			route_add_or_change("-{$ipprotocol} default {$gateways_arr[$dfltgwname]['gateway']}");
864
		}
865
	}
866
}
867

    
868
/*
869
 * Return an array with all gateway groups with name as key
870
 * All gateway groups will be processed before returning the array.
871
 */
872
function return_gateway_groups_array() {
873
	global $config, $g;
874

    
875
	/* fetch the current gateways status */
876
	$gateways_status = return_gateways_status(true);
877
	$gateways_arr = return_gateways_array();
878
	$gateway_groups_array = array();
879

    
880
	if (isset($config['system']['gw_switch_default'])) {
881
		fixup_default_gateway("inet", $gateways_status, $gateways_arr);
882
		fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
883
	}
884
	if (is_array($config['gateways']['gateway_group'])) {
885
		$viplist = get_configured_vip_list();
886
		foreach ($config['gateways']['gateway_group'] as $group) {
887
			$gateway_groups_array[$group['name']]['descr'] = $group['descr'];
888
			/* create array with group gateways members separated by tier */
889
			$tiers = array();
890
			$backupplan = array();
891
			$gwvip_arr = array();
892
			foreach ($group['item'] as $item) {
893
				list($gwname, $tier, $vipname) = explode("|", $item);
894

    
895
				if (is_ipaddr($viplist[$vipname])) {
896
					if (!is_array($gwvip_arr[$group['name']])) {
897
						$gwvip_arr[$group['name']] = array();
898
					}
899
					$gwvip_arr[$group['name']][$gwname] = $vipname;
900
				}
901

    
902
				/* Do it here rather than reiterating again the group in case no member is up. */
903
				if (!is_array($backupplan[$tier])) {
904
					$backupplan[$tier] = array();
905
				}
906
				$backupplan[$tier][] = $gwname;
907

    
908
				/* check if the gateway is available before adding it to the array */
909
				if (is_array($gateways_status[$gwname])) {
910
					$status = $gateways_status[$gwname];
911
					$gwdown = false;
912
					if (stristr($status['status'], "down")) {
913
						$msg = sprintf(gettext('MONITOR: %1$s is down, omitting from routing group %2$s'), $gwname, $group['name']);
914
						$gwdown = true;
915
					} else if (stristr($status['status'], "loss") && strstr($group['trigger'], "loss")) {
916
						/* packet loss */
917
						$msg = sprintf(gettext('MONITOR: %1$s has packet loss, omitting from routing group %2$s'), $gwname, $group['name']);
918
						$gwdown = true;
919
					} else if (stristr($status['status'], "delay") && strstr($group['trigger'] , "latency")) {
920
						/* high latency */
921
						$msg = sprintf(gettext('MONITOR: %1$s has high latency, omitting from routing group %2$s'), $gwname, $group['name']);
922
						$gwdown = true;
923
					}
924
					if ($gwdown == true) {
925
						if (!file_exists("/tmp/.down.{$gwname}")) {
926
							$msg .= "\n".implode("|", $status);
927
							touch("/tmp/.down.{$gwname}");
928
							log_error($msg);
929
							notify_via_growl($msg);
930
							notify_via_smtp($msg);
931
						}
932
					} else {
933
						/* Online add member */
934
						if (!is_array($tiers[$tier])) {
935
							$tiers[$tier] = array();
936
						}
937
						$tiers[$tier][] = $gwname;
938
						if (unlink_if_exists("/tmp/.down.{$gwname}")) {
939
							$msg = sprintf(gettext('MONITOR: %1$s is available now, adding to routing group %2$s'), $gwname, $group['name']);
940
							$msg .= "\n".implode("|", $status);
941
							log_error($msg);
942
							notify_via_growl($msg);
943
							notify_via_smtp($msg);
944
						}
945
					}
946
				} else if (isset($gateways_arr[$gwname]['monitor_disable'])) {
947
					$tiers[$tier][] = $gwname;
948
				}
949
			}
950
			$tiers_count = count($tiers);
951
			if ($tiers_count == 0) {
952
				/* Oh dear, we have no members! Engage Plan B */
953
				if (!platform_booting()) {
954
					$msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']);
955
					log_error($msg);
956
					notify_via_growl($msg);
957
					//notify_via_smtp($msg);
958
				}
959
				$tiers = $backupplan;
960
			}
961
			/* sort the tiers array by the tier key */
962
			ksort($tiers);
963

    
964
			/* we do not really foreach the tiers as we stop after the first tier */
965
			foreach ($tiers as $tieridx => $tier) {
966
				/* process all gateways in this tier */
967
				foreach ($tier as $member) {
968
					/* determine interface gateway */
969
					if (isset($gateways_arr[$member])) {
970
						$gateway = $gateways_arr[$member];
971
						$int = $gateway['interface'];
972
						$gatewayip = "";
973
						if (is_ipaddr($gateway['gateway'])) {
974
							$gatewayip = $gateway['gateway'];
975
						} else if (!empty($int)) {
976
							$gatewayip = get_interface_gateway($gateway['friendlyiface']);
977
						}
978

    
979
						if (!empty($int)) {
980
							$gateway_groups_array[$group['name']]['ipprotocol'] = $gateway['ipprotocol'];
981
							if (is_ipaddr($gatewayip)) {
982
								$groupmember = array();
983
								$groupmember['int'] = $int;
984
								$groupmember['gwip'] = $gatewayip;
985
								$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
986
								if (is_array($gwvip_arr[$group['name']]) && !empty($gwvip_arr[$group['name']][$member]))
987
									$groupmember['vip'] = $gwvip_arr[$group['name']][$member];
988
								$gateway_groups_array[$group['name']][] = $groupmember;
989
							}
990
						}
991
					}
992
				}
993
				/* we should have the 1st available tier now, exit stage left */
994
				if (count($gateway_groups_array[$group['name']]) > 0) {
995
					break;
996
				} else {
997
					log_error(sprintf(gettext('GATEWAYS: Group %1$s did not have any gateways up on tier %2$s!'), $group['name'], $tieridx));
998
				}
999
			}
1000
		}
1001
	}
1002

    
1003
	return ($gateway_groups_array);
1004
}
1005

    
1006
/* Update DHCP WAN Interface ip address in gateway group item */
1007
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
1008
	global $config, $g;
1009
	foreach ($config['gateways']['gateway_item'] as & $gw) {
1010
		if ($gw['interface'] == $interface) {
1011
			$current_gw = get_interface_gateway($interface);
1012
			if ($gw['gateway'] <> $current_gw) {
1013
				$gw['gateway'] = $current_gw;
1014
				$changed = true;
1015
			}
1016
		}
1017
	}
1018
	if ($changed && $current_gw) {
1019
		write_config(sprintf(gettext('Updating gateway group gateway for %1$s - new gateway is %2$s'), $interface, $current_gw));
1020
	}
1021
}
1022

    
1023
function lookup_gateway_ip_by_name($name, $disabled = false) {
1024

    
1025
	$gateways_arr = return_gateways_array($disabled, true);
1026
	foreach ($gateways_arr as $gname => $gw) {
1027
		if ($gw['name'] === $name || $gname === $name) {
1028
			return $gw['gateway'];
1029
		}
1030
	}
1031

    
1032
	return false;
1033
}
1034

    
1035
function lookup_gateway_monitor_ip_by_name($name) {
1036

    
1037
	$gateways_arr = return_gateways_array(false, true);
1038
	if (!empty($gateways_arr[$name])) {
1039
		$gateway = $gateways_arr[$name];
1040
		if (!is_ipaddr($gateway['monitor'])) {
1041
			return $gateway['gateway'];
1042
		}
1043

    
1044
		return $gateway['monitor'];
1045
	}
1046

    
1047
	return (false);
1048
}
1049

    
1050
function lookup_gateway_interface_by_name($name) {
1051

    
1052
	$gateways_arr = return_gateways_array(false, true);
1053
	if (!empty($gateways_arr[$name])) {
1054
		$interfacegw = $gateways_arr[$name]['friendlyiface'];
1055
		return ($interfacegw);
1056
	}
1057

    
1058
	return (false);
1059
}
1060

    
1061
function get_interface_gateway($interface, &$dynamic = false) {
1062
	global $config, $g;
1063

    
1064
	if (substr($interface, 0, 4) == '_vip') {
1065
		$interface = get_configured_vip_interface($interface);
1066
		if (substr($interface, 0, 4) == '_vip') {
1067
			$interface = get_configured_vip_interface($interface);
1068
		}
1069
	}
1070

    
1071
	$gw = NULL;
1072
	$gwcfg = $config['interfaces'][$interface];
1073
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
1074
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1075
			if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
1076
				$gw = $gateway['gateway'];
1077
				break;
1078
			}
1079
		}
1080
	}
1081

    
1082
	// for dynamic interfaces we handle them through the $interface_router file.
1083
	if (($gw == NULL || !is_ipaddrv4($gw)) && !is_ipaddrv4($gwcfg['ipaddr'])) {
1084
		$realif = get_real_interface($interface);
1085
		if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
1086
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
1087
			$dynamic = true;
1088
		}
1089
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw")) {
1090
			$dynamic = "default";
1091
		}
1092

    
1093
	}
1094

    
1095
	/* return gateway */
1096
	return ($gw);
1097
}
1098

    
1099
function get_interface_gateway_v6($interface, &$dynamic = false) {
1100
	global $config, $g;
1101

    
1102
	if (substr($interface, 0, 4) == '_vip') {
1103
		$interface = get_configured_vip_interface($interface);
1104
		if (substr($interface, 0, 4) == '_vip') {
1105
			$interface = get_configured_vip_interface($interface);
1106
		}
1107
	}
1108

    
1109
	$gw = NULL;
1110
	$gwcfg = $config['interfaces'][$interface];
1111
	if (!empty($gwcfg['gatewayv6']) && is_array($config['gateways']['gateway_item'])) {
1112
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1113
			if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
1114
				$gw = $gateway['gateway'];
1115
				break;
1116
			}
1117
		}
1118
	}
1119

    
1120
	// for dynamic interfaces we handle them through the $interface_router file.
1121
	if (($gw == NULL || !is_ipaddrv6($gw)) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
1122
		$realif = get_real_interface($interface);
1123
		if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
1124
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
1125
			$dynamic = true;
1126
		}
1127
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6")) {
1128
			$dynamic = "default";
1129
		}
1130
	}
1131
	/* return gateway */
1132
	return ($gw);
1133
}
1134

    
1135
/* Check a IP address against a gateway IP or name
1136
 * to verify it's address family */
1137
function validate_address_family($ipaddr, $gwname, $disabled = false) {
1138
	$v4ip = false;
1139
	$v6ip = false;
1140
	$v4gw = false;
1141
	$v6gw = false;
1142

    
1143
	if (is_ipaddrv4($ipaddr)) {
1144
		$v4ip = true;
1145
	}
1146
	if (is_ipaddrv6($ipaddr)) {
1147
		$v6ip = true;
1148
	}
1149
	if (is_ipaddrv4($gwname)) {
1150
		$v4gw = true;
1151
	}
1152
	if (is_ipaddrv6($gwname)) {
1153
		$v6gw = true;
1154
	}
1155

    
1156
	if ($v4ip && $v4gw) {
1157
		return true;
1158
	}
1159
	if ($v6ip && $v6gw) {
1160
		return true;
1161
	}
1162

    
1163
	/* still no match, carry on, lookup gateways */
1164
	if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname, $disabled))) {
1165
		$v4gw = true;
1166
	}
1167
	if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname, $disabled))) {
1168
		$v6gw = true;
1169
	}
1170

    
1171
	$gw_array = return_gateways_array();
1172
	if (is_array($gw_array[$gwname])) {
1173
		switch ($gw_array[$gwname]['ipprotocol']) {
1174
			case "inet":
1175
				$v4gw = true;
1176
				break;
1177
			case "inet6":
1178
				$v6gw = true;
1179
				break;
1180
		}
1181
	}
1182

    
1183
	if ($v4ip && $v4gw) {
1184
		return true;
1185
	}
1186
	if ($v6ip && $v6gw) {
1187
		return true;
1188
	}
1189

    
1190
	return false;
1191
}
1192

    
1193
/* check if a interface is part of a gateway group */
1194
function interface_gateway_group_member($interface) {
1195
	global $config;
1196

    
1197
	if (is_array($config['gateways']['gateway_group'])) {
1198
		$groups = $config['gateways']['gateway_group'];
1199
	} else {
1200
		return false;
1201
	}
1202

    
1203
	$gateways_arr = return_gateways_array(false, true);
1204
	foreach ($groups as $group) {
1205
		if (is_array($group['item'])) {
1206
			foreach ($group['item'] as $item) {
1207
				$elements = explode("|", $item);
1208
				$gwname = $elements[0];
1209
				if ($interface == $gateways_arr[$gwname]['interface']) {
1210
					unset($gateways_arr);
1211
					return true;
1212
				}
1213
			}
1214
		}
1215
	}
1216
	unset($gateways_arr);
1217

    
1218
	return false;
1219
}
1220

    
1221
function gateway_is_gwgroup_member($name) {
1222
	global $config;
1223

    
1224
	if (is_array($config['gateways']['gateway_group'])) {
1225
		$groups = $config['gateways']['gateway_group'];
1226
	} else {
1227
		return false;
1228
	}
1229

    
1230
	$members = array();
1231
	foreach ($groups as $group) {
1232
		if (is_array($group['item'])) {
1233
			foreach ($group['item'] as $item) {
1234
				$elements = explode("|", $item);
1235
				$gwname = $elements[0];
1236
				if ($name == $elements[0]) {
1237
					$members[] = $group['name'];
1238
				}
1239
			}
1240
		}
1241
	}
1242

    
1243
	return $members;
1244
}
1245
?>
(17-17/51)