Project

General

Profile

Download (87.1 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-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
10
 * All rights reserved.
11
 *
12
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23
 */
24

    
25
require_once("config.inc");
26
require_once("rrd.inc");
27
require_once("ipsec.inc");
28
require_once("interfaces.inc");
29
require_once("util.inc");
30

    
31
global $gateway_state_kill_modes;
32
$gateway_state_kill_modes = array(
33
	'' => gettext("Use global behavior (default)"),
34
	'none' => gettext("Do not kill states on gateway failure"),
35
	'down' => gettext("Kill states using this gateway when it is down"),
36
);
37

    
38
// Initialize the gateways cache variables
39
define('GW_CACHE_DISABLED', 1); // include disabled gateways and gateways on disabled interfaces
40
define('GW_CACHE_LOCALHOST', 2); // include "Null" entries for localhost IP addresses
41
define('GW_CACHE_INACTIVE', 4); // include gateways on inactive interfaces
42
define('GW_CACHE_INDEXED', 8); // index the returned array by integers instead of the name
43
define('GW_CACHE_ALL', GW_CACHE_DISABLED | GW_CACHE_LOCALHOST | GW_CACHE_INACTIVE); // all possible gateways
44
if (!array_key_exists('GatewaysCache', $GLOBALS)) {
45
	$GLOBALS['GatewaysCache'] = null;
46
}
47

    
48
/**
49
 * Caches the gateway list and filters the result. Changes to gateways should
50
 * call refresh_gateways(), e.g. when adding or removing dynamic gateways.
51
 * 
52
 * @param array $flags Additional gateways to include.
53
 * 
54
 * @return array filtered list of gateways
55
 */
56
function get_gateways(int $flags = 0): array {
57
	if (!is_array($GLOBALS['GatewaysCache'])) {
58
		refresh_gateways(); // create the gateway list cache
59
	}
60
	$gateways = $GLOBALS['GatewaysCache'];
61
	$indexed = boolval($flags & GW_CACHE_INDEXED);
62
	if ($indexed) {
63
		$remove_disabled = false;
64
		$remove_localhost = true;
65
		$remove_inactive = false;
66
	} elseif (boolval(($flags & GW_CACHE_ALL) == GW_CACHE_ALL)) {
67
		$remove_disabled = false;
68
		$remove_localhost = false;
69
		$remove_inactive = false;
70
	} else {
71
		$remove_disabled = !boolval($flags & GW_CACHE_DISABLED);
72
		$remove_localhost = !boolval($flags & GW_CACHE_LOCALHOST);
73
		$remove_inactive = !boolval($flags & GW_CACHE_INACTIVE);
74
	}
75

    
76
	if ($remove_localhost) {
77
		unset($gateways['Null4']);
78
		unset($gateways['Null6']);
79
	}
80

    
81
	if ($remove_disabled || $remove_inactive) {
82
		foreach ($gateways as $idx => &$gw) {
83
			if (($idx == 'Null4') || ($idx == 'Null6')) {
84
				continue;
85
			}
86
			if ($remove_disabled && (isset($gw['disabled']) || (isset($gw['friendlyiface']) && !config_path_enabled("interfaces/{$gw['friendlyiface']}")))) {
87
				unset($gateways[$idx]);
88
			}
89
			if ($remove_inactive && isset($gw['inactive'])) {
90
				unset($gateways[$idx]);
91
			}
92
		}
93
	}
94

    
95
	if ($indexed) {
96
		$gateways = array_values($gateways);
97
	}
98

    
99
	if ($remove_disabled || $remove_localhost || $remove_inactive || $indexed) {
100
		$gateways = order_gateways_as_configured($gateways);
101
	}
102

    
103
	return $gateways;
104
}
105

    
106
/**
107
 * Generates a list of all gateways and adds them to the config if missing.
108
 */
109
function refresh_gateways(): void {
110
	// Generate all gateways and cache the result
111
	$GLOBALS['GatewaysCache'] = return_gateways_array(true, true, true);
112

    
113
	// Get gateways that exist in the config
114
	$saved_gateways = array_column(config_get_path('gateways/gateway_item', []), 'name', 'name');
115

    
116
	// Exclude null gateways from being saved to config
117
	foreach (array_diff_key($GLOBALS["GatewaysCache"], array_fill_keys(['Null4', 'Null6'], null)) as $gateway) {
118
		// Save any of the generated gateways that are not found in config
119
		if (!array_key_exists($gateway['name'], $saved_gateways)) {
120
			// Build the gateway elements and save the gateway to config
121
			config_set_path('gateways/gateway_item',
122
				array_merge(config_get_path('gateways/gateway_item', []), [[
123
					'interface' => $gateway['friendlyiface'],
124
					'gateway' => $gateway['dynamic'] ? 'dynamic' : $gateway['gateway'],
125
					'name' => $gateway['name'],
126
					'weight' => $gateway['weight'] ?? 1,
127
					'ipprotocol' => $gateway['ipprotocol'],
128
					'descr' => $gateway['descr'],
129
					'gw_down_kill_states' => $gateway['gw_down_kill_states'] ?? ''
130
				]])
131
			);
132
			if (!isset($write_config)) {
133
				$write_config = true;
134
			}
135
		}
136
	}
137
	if ($write_config) {
138
		write_config(gettext('Synced configured gateways'), true, true);
139
	}
140
}
141

    
142
/* Returns an array of default values used for dpinger */
143
function return_dpinger_defaults() {
144
	return array(
145
		"latencylow" => "200",
146
		"latencyhigh" => "500",
147
		"losslow" => "10",
148
		"losshigh" => "20",
149
		"interval" => "500",
150
		"loss_interval" => "2000",
151
		"time_period" => "60000",
152
		"alert_interval" => "1000",
153
		"data_payload" => "1");
154
}
155

    
156
function running_dpinger_processes() {
157
	$pidfiles = glob(g_get('varrun_path', '') . '/dpinger_*.pid');
158

    
159
	$result = array();
160
	if ($pidfiles === FALSE) {
161
		return $result;
162
	}
163

    
164
	foreach ($pidfiles as $pidfile) {
165
		if (preg_match('/^dpinger_(.+)~([^~]+)~([^~]+)\.pid$/',
166
		    basename($pidfile), $matches)) {
167
			$socket_file = preg_replace('/\.pid$/', '.sock',
168
			    $pidfile);
169
			$result[$matches[1]] = array(
170
			    'srcip'    => $matches[2],
171
			    'targetip' => $matches[3],
172
			    'pidfile'  => $pidfile,
173
			    'socket'   => $socket_file
174
			);
175
			unset($gwinfo);
176
		}
177
	}
178

    
179
	return $result;
180
}
181

    
182
/*
183
 * Stop one or more dpinger process
184
 * default parameter $gwname is '*' that will kill all running sessions
185
 * If a gateway name is passed, only this one will be killed
186
 */
187
function stop_dpinger($gwname = '') {
188
	$running_processes = running_dpinger_processes();
189

    
190
	foreach ($running_processes as $running_gwname => $process) {
191
		if ($gwname != '' && $running_gwname != $gwname) {
192
			continue;
193
		}
194

    
195
		if (isvalidpid($process['pidfile'])) {
196
			killbypid($process['pidfile'], 3);
197
		} else {
198
			@unlink($process['pidfile']);
199
		}
200
	}
201
}
202

    
203
function start_dpinger($gateway) {
204
	if (!isset($gateway['gwifip'])) {
205
		return (false);
206
	}
207

    
208
	$dpinger_defaults = return_dpinger_defaults();
209

    
210
	$prefix = g_get('varrun_path', '') . "/dpinger_{$gateway['name']}~" .
211
	    "{$gateway['gwifip']}~{$gateway['monitor']}";
212
	# dpinger socket path should not be longer then uaddr.sun_path
213
	if (strlen($prefix) > 95) {
214
		$prefix = g_get('varrun_path', '') . "/dpinger_{$gateway['name']}~" .
215
		    substr(md5($gateway['gwifip']),0,8) . "~" .
216
		    $gateway['monitor'];
217
	}
218
	$pidfile = $prefix . ".pid";
219
	$socket = $prefix . ".sock";
220
	$alarm_cmd = g_get('etc_path', '') . '/rc.gateway_alarm';
221

    
222
	$params  = "-S ";			/* Log warnings via syslog */
223
	$params .= "-r 0 ";			/* Disable unused reporting thread */
224
	$params .= "-i {$gateway['name']} ";	/* Identifier */
225
	$params .= "-B {$gateway['gwifip']} ";	/* Bind src address */
226
	$params .= "-p {$pidfile} ";		/* PID filename */
227
	$params .= "-u {$socket} ";		/* Status Socket */
228
	if (!$gateway['action_disable']) {
229
		$params .= "-C \"{$alarm_cmd}\" ";	/* Command to run on alarm */
230
	}
231

    
232
	$params .= "-d " .
233
	    (isset($gateway['data_payload']) && is_numeric($gateway['data_payload'])
234
	    ? $gateway['data_payload']
235
	    : $dpinger_defaults['data_payload']
236
	    ) . " ";
237

    
238
	$params .= "-s " .
239
	    (isset($gateway['interval']) && is_numeric($gateway['interval'])
240
	    ? $gateway['interval']
241
	    : $dpinger_defaults['interval']
242
	    ) . " ";
243

    
244
	$params .= "-l " .
245
	    (isset($gateway['loss_interval']) && is_numeric($gateway['loss_interval'])
246
	    ?  $gateway['loss_interval']
247
	    : $dpinger_defaults['loss_interval']
248
	    ) . " ";
249

    
250
	$params .= "-t " .
251
	    (isset($gateway['time_period']) && is_numeric($gateway['time_period'])
252
	    ?  $gateway['time_period']
253
	    : $dpinger_defaults['time_period']
254
	    ) . " ";
255

    
256
	$params .= "-A " .
257
	    (isset($gateway['alert_interval']) && is_numeric($gateway['alert_interval'])
258
	    ?  $gateway['alert_interval']
259
	    : $dpinger_defaults['alert_interval']
260
	    ) . " ";
261

    
262
	$params .= "-D " .
263
	    (isset($gateway['latencyhigh']) && is_numeric($gateway['latencyhigh'])
264
	    ?  $gateway['latencyhigh']
265
	    : $dpinger_defaults['latencyhigh']
266
	    ) . " ";
267

    
268
	$params .= "-L " .
269
	    (isset($gateway['losshigh']) && is_numeric($gateway['losshigh'])
270
	    ?  $gateway['losshigh']
271
	    : $dpinger_defaults['losshigh']
272
	    ) . " ";
273

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

    
277
	/* Do not try to bind IPv6 where interface is in tentative state */
278
	if (is_ipaddrv6($gateway['gwifip'])) {
279
		$err = interface_wait_tentative(get_real_interface(
280
		    $gateway['interface']));
281
		if ($err == false) {
282
			log_error(gettext("Timeout waiting for IPv6 address in tentative state.  dpinger will not run."));
283
			return (false);
284
		}
285
	}
286

    
287
	/* Redirect stdout to /dev/null to avoid exec() to wait for dpinger */
288
	return mwexec("/usr/local/bin/dpinger {$params} {$gateway['monitor']} >/dev/null");
289
}
290

    
291
/*
292
 * Starts dpinger processes and adds appropriate static routes for monitor IPs
293
 */
294
function setup_gateways_monitor() {
295
	refresh_gateways(); // dynamic gateways may have changed so refresh the cache
296
	$gateways_arr = get_gateways();
297
	if (!is_array($gateways_arr)) {
298
		log_error(gettext("No gateways to monitor. dpinger will not run."));
299
		stop_dpinger();
300
		return;
301
	}
302
	if (is_platform_booting()) {
303
		echo "Setting up gateway monitors...";
304
	}
305
	$monitor_ips = array();
306
	foreach ($gateways_arr as $gwname => $gateway) {
307
		/* Do not monitor if such was requested */
308
		if (isset($gateway['monitor_disable'])) {
309
			continue;
310
		}
311
		if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
312
			if (is_ipaddr($gateway['gateway'])) {
313
				$gateways_arr[$gwname]['monitor'] = $gateway['gateway'];
314
			} else { /* No chance to get an ip to monitor skip target. */
315
				continue;
316
			}
317
		}
318

    
319
		/* if the monitor address is already used before, skip */
320
		if (in_array($gateway['monitor'], $monitor_ips)) {
321
			continue;
322
		}
323

    
324
		/* Interface ip is needed since dpinger will bind a socket to it.
325
		 * However the config GUI should already have checked this and when
326
		 * PPPoE is used the IP address is set to "dynamic". So using is_ipaddrv4
327
		 * or is_ipaddrv6 to identify packet type would be wrong, especially as
328
		 * further checks (that can cope with the "dynamic" case) are present inside
329
		 * the if block. So using $gateway['ipprotocol'] is the better option.
330
		 */
331
		if ($gateway['ipprotocol'] == "inet") { // This is an IPv4 gateway...
332
			$gwifip = find_interface_ip($gateway['interface'], true);
333
			if (!is_ipaddrv4($gwifip)) {
334
				continue; //Skip this target
335
			}
336

    
337
			if ($gwifip == "0.0.0.0") {
338
				continue; //Skip this target - the gateway is still waiting for DHCP
339
			}
340

    
341
			/*
342
			 * If the gateway is the same as the monitor we do not add a
343
			 * route as this will break the routing table.
344
			 * Add static routes for each gateway with their monitor IP
345
			 * not strictly necessary but is a added level of protection.
346
			 */
347
			if (!config_path_enabled('system', 'dpinger_dont_add_static_routes') &&
348
					!isset($gateway['dpinger_dont_add_static_route'])) {
349
				if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
350
					log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
351
					if (interface_isppp_type($gateway['friendlyiface'])) {
352
						route_add_or_change($gateway['monitor'],
353
						    '', $gateway['interface']);
354
						system_staticroutes_configure($gateway['friendlyiface']);
355
					} else {
356
						route_add_or_change($gateway['monitor'],
357
						    $gateway['gateway']);
358
					}
359

    
360
					pfSense_kill_states("0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmp");
361
				}
362
			}
363
		} else if ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
364
			if (is_linklocal($gateway['gateway']) &&
365
			    get_ll_scope($gateway['gateway']) == '') {
366
				$gateway['gateway'] .= '%' . $gateway['interface'];
367
			}
368

    
369
			if (is_linklocal($gateway['monitor'])) {
370
				if (get_ll_scope($gateway['monitor']) == '') {
371
					$gateways_arr[$gwname]['monitor'] .= '%' . $gateway['interface'];
372
				}
373

    
374
				$gwifip = find_interface_ipv6_ll($gateway['interface'], true);
375

    
376
				if (get_ll_scope($gwifip) == '') {
377
					$gwifip .= '%' . $gateway['interface'];
378
				}
379
			} else {
380
				$gwifip = find_interface_ipv6($gateway['interface'], true);
381
			}
382

    
383
			if (!is_ipaddrv6($gwifip)) {
384
				continue; //Skip this target
385
			}
386

    
387
			/*
388
			 * If the gateway is the same as the monitor we do not add a
389
			 * route as this will break the routing table.
390
			 * Add static routes for each gateway with their monitor IP
391
			 * not strictly necessary but is a added level of protection.
392
			 */
393

    
394
			if (!config_path_enabled('system', 'dpinger_dont_add_static_routes') &&
395
					!isset($gateway['dpinger_dont_add_static_route'])) {
396
				if ($gateway['gateway'] != $gateways_arr[$gwname]['monitor']) {
397
					log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
398
					if (interface_isppp_type($gateway['friendlyiface'])) {
399
						route_add_or_change($gateway['monitor'],
400
						    '', $gateway['interface']);
401
						system_staticroutes_configure($gateway['friendlyiface']);
402
					} else {
403
						route_add_or_change($gateway['monitor'],
404
						    $gateway['gateway']);
405
					}
406

    
407
					pfSense_kill_states("::0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmpv6");
408
				}
409
			}
410
		} else {
411
			continue;
412
		}
413

    
414
		$monitor_ips[] = $gateway['monitor'];
415
		$gateways_arr[$gwname]['enable_dpinger'] = true;
416
		$gateways_arr[$gwname]['gwifip'] = $gwifip;
417
	}
418

    
419
	stop_dpinger();
420

    
421
	/* Start new processes */
422
	foreach ($gateways_arr as $gateway) {
423
		if (!isset($gateway['enable_dpinger'])) {
424
			continue;
425
		}
426

    
427
		if (start_dpinger($gateway) != 0) {
428
			log_error(sprintf(gettext("Error starting gateway monitor for %s"), $gateway['name']));
429
		}
430
	}
431
	if (is_platform_booting()) {
432
		echo "done.\n";
433
	}
434

    
435
	return;
436
}
437

    
438
/**
439
 * Looks for the highest priority failover tier that is currently online in a
440
 * failover gateway group. A failover tier is any tier number greater than the
441
 * lowest configured tier number. A failover gateway group must consist of at
442
 * least two different tiers.
443
 * 
444
 * @param string $gateway_group Name of an existing failover gateway group
445
 * 
446
 * @return int The tier number - returns 0 if no matches are found
447
 * @return false Not a valid failover gateway group
448
 */
449
function get_highest_failover_tier_online(?string $gateway_group): int|false {
450
	if (empty($gateway_group)) {
451
		return false;
452
	}
453

    
454
	foreach (config_get_path('gateways/gateway_group', []) as $group) {
455
		// check if this is the gateway group being queried
456
		if ($group['name'] != $gateway_group) {
457
			continue;
458
		}
459

    
460
		// make sure there are expected values and at least two gateways in the gateway group
461
		if (!is_array($group['item']) || empty($group['item']) || (count($group['item']) < 2)) {
462
			continue;
463
		}
464
		// build a list of gateways and their tiers
465
		$group_gateways = [];
466
		foreach ($group['item'] as $item) {
467
			$gateway_item = explode("|", $item);
468
			// at least a gateway name and tier is expected
469
			if (count($gateway_item) < 2) {
470
				continue;
471
			}
472

    
473
			$group_gateways[$gateway_item[0]] = intval($gateway_item[1]);
474
		}
475

    
476
		// make sure it's a failover group by checking for different tiers
477
		$group_tiers = array_unique(array_values($group_gateways));
478
		if (count($group_tiers) < 2) {
479
			break;
480
		}
481

    
482
		if (!isset($gateways_states)) {
483
			// get a list of all relevant gateway states
484
			$gateways_states = return_gateways_status(true);
485
		}
486

    
487
		$highest_tier = min($group_tiers);
488
		// remove offline gateways
489
		foreach (array_keys($group_gateways) as $gateway_name) {
490
			if ($gateways_states[$gateway_name]['status'] != 'online') {
491
				unset($group_gateways[$gateway_name]);
492
			}
493
		}
494

    
495
		// no online gateways
496
		if (empty($group_gateways)) {
497
			return 0;
498
		}
499

    
500
		// highest online is highest tier
501
		$highest_online_tier = min(array_values($group_gateways));
502
		if ($highest_online_tier == $highest_tier) {
503
			return 0;
504
		}
505

    
506
		// return the highest-priority failover tier of online gateways
507
		return $highest_online_tier;
508
	}
509

    
510
	return false;
511
}
512

    
513
/**
514
 * Loops through each gateway group and kills states for lower-priority gateways.
515
 * Supports the same gateway at different tiers in different gateway groups. A
516
 * failover gateway group name may be provided to restrict the state removal.
517
 * 
518
 * @param string $gateway_name Gateway group name to remove failover states for
519
 */
520
function remove_failover_states(?string $gateway_name = null): void {
521
	// get the global default for gateway groups
522
	$keep_failover_states = config_path_enabled('system', 'keep_failover_states');
523

    
524
	// if killing states from the firewall itself, check for default failover gateway groups
525
	$remove_states_default = config_get_path('system/remove_failover_states_default');
526
	if ($remove_states_default) {
527
		$default_gateways = [];
528

    
529
		$default_gateway4 = config_get_path('gateways/defaultgw4', '');
530
		if (!empty($default_gateway4) && ($default_gateway4 != '-')) {
531
			$default_gateways[$default_gateway4] = null;
532
		}
533
		$default_gateway6 = config_get_path('gateways/defaultgw6', '');
534
		if (!empty($default_gateway6) && ($default_gateway6 != '-')) {
535
			$default_gateways[$default_gateway6] = null;
536
		}
537
	}
538

    
539
	foreach (config_get_path('gateways/gateway_group', []) as $group) {
540
		// skip if states don't need to be killed for this gateway group
541
		if ((!empty($gateway_name) && ($group['name'] != $gateway_name)) ||
542
		    ($keep_failover_states && ($group['keep_failover_states'] != 'kill'))) {
543
			continue;
544
		}
545

    
546
		// make sure there are expected values and at least two gateways in the gateway group
547
		if (!is_array($group['item']) || empty($group['item']) || (count($group['item']) < 2)) {
548
			continue;
549
		}
550
		// build a list gateway interfaces and tiers
551
		$gateway_interfaces = [];
552
		foreach ($group['item'] as $item) {
553
			$gateway_item = explode("|", $item);
554
			// at least a gateway name and tier is expected
555
			if (count($gateway_item) < 2) {
556
				continue;
557
			}
558

    
559
			if (!isset($gateways_states)) {
560
				// get a list of all relevant gateway states
561
				$gateways_states = return_gateways_status(true);
562
			}
563

    
564
			// only process online gateways
565
			if ($gateways_states[$gateway_item[0]]['status'] != 'online') {
566
				continue;
567
			}
568

    
569
			if (!isset($all_gateways)) {
570
				// get a list of all relevant gateways
571
				$all_gateways = get_gateways();
572
			}
573

    
574
			// get the gateway's interface and tier
575
			$gateway_interfaces[$all_gateways[$gateway_item[0]]['interface']] = intval($gateway_item[1]);
576
		}
577

    
578
		// make sure there are at least two online gateways (interfaces) in this gateway group
579
		if (empty($gateway_interfaces) || (count($gateway_interfaces) < 2)) {
580
			continue;
581
		}
582

    
583
		// build a list of interfaces, used to kill states from the firewall itself
584
		if ($remove_states_default && array_key_exists($group['name'], $default_gateways)) {
585
			// get the interfaces of the default gateway group
586
			$default_gateways[$group['name']] = $gateway_interfaces;
587

    
588
			// for states from the firewall itself (when the default gateway is set to a failover gateway group)
589
			foreach (array_keys($gateway_interfaces, min(array_values($gateway_interfaces))) as $gateway_interface) {
590
				// exclude the interface of the highest priority gateway(s)
591
				unset($default_gateways[$group['name']][$gateway_interface]);
592
			}
593

    
594
			// all states are to be killed for this gateway group, skip killing only policy routing states
595
			continue;
596
		}
597

    
598
		// build a list of tiers to kill policy routing states for
599
		$tiers_to_kill = array_unique(array_values($gateway_interfaces));
600
		// avoid killing states for the higher-tier gateway(s)
601
		$tiers_to_kill = array_diff($tiers_to_kill, [min($tiers_to_kill)]);
602
		foreach ($tiers_to_kill as $tier) {
603
			mwexec_bg('/sbin/pfctl -M -k label -k ' . escapeshellarg("gw{$tier}:{$group['name']}") . ' -q');
604
		}
605
	}
606

    
607
	// kill all states for the default failover gateway groups
608
	if (!$remove_states_default) {
609
		return;
610
	}
611
	foreach ($default_gateways as $gateway_name => $gateway_interfaces) {
612
		if (empty($gateway_interfaces)) {
613
			continue;
614
		}
615

    
616
		/* filtering for the address family allows states that have been created by user
617
		   rules with a gateway of a different address family on the same interface to remain */
618
		if ($remove_states_default == 'addrfamily') {
619
			$host_prefix = ($gateway_name == $default_gateway4) ? '-k 0.0.0.0/0' : '-k ::/0';
620
		} elseif ($remove_states_default == 'all') {
621
			$host_prefix = '-Fs';
622
		} else {
623
			continue;
624
		}
625
		foreach (array_keys($gateway_interfaces) as $gateway_interface) {
626
			mwexec_bg('/sbin/pfctl -M -i ' . escapeshellarg($gateway_interface) . " {$host_prefix} -q");
627
		}
628
	}
629
}
630

    
631
function get_dpinger_status($gwname, $action_disable = false) {
632
	$running_processes = running_dpinger_processes();
633

    
634
	if (!isset($running_processes[$gwname])) {
635
		log_error(sprintf(gettext(
636
		    'dpinger: No dpinger session running for gateway %s'),
637
		    $gwname));
638
		return false;
639
	}
640

    
641
	$proc = $running_processes[$gwname];
642
	unset($running_processes);
643

    
644
	$timeoutcounter = 0;
645
	while (true) {
646
		if (!file_exists($proc['socket'])) {
647
			log_error("dpinger: status socket {$proc['socket']} not found");
648
			return false;
649
		}
650
		$fp = @stream_socket_client("unix://{$proc['socket']}", $errno, $errstr, 10);
651
		if (!$fp) {
652
			log_error(sprintf(gettext('dpinger: cannot connect to status socket %1$s - %2$s (%3$s)'), $proc['socket'], $errstr, $errno));
653
			return false;
654
		}
655

    
656
		$status = '';
657
		while (!feof($fp)) {
658
			$status .= fgets($fp, 1024);
659
		}
660
		fclose($fp);
661

    
662
		$r = array();
663
		list(
664
			$r['gwname'],
665
			$r['latency_avg'],
666
			$r['latency_stddev'],
667
			$r['loss']
668
		) = explode(' ', preg_replace('/\n/', '', $status));
669

    
670
		// dpinger returns '<gwname> 0 0 0' when queried directly after it starts.
671
		// while a latency of 0 and a loss of 0 would be perfect, in a real world it doesnt happen.
672
		// or does it, anyone? if so we must 'detect' the initialization period differently..
673
		$ready = $r['latency_stddev'] != '0' || $r['loss'] != '0';
674

    
675
		if ($ready) {
676
			break;
677
		} else {
678
			$timeoutcounter++;
679
			if ($timeoutcounter > 300) {
680
				log_error(sprintf(gettext('dpinger: timeout while retrieving status for gateway %s'), $gwname));
681
				return false;
682
			}
683
			usleep(10000);
684
		}
685
	}
686

    
687
	$r['srcip'] = $proc['srcip'];
688
	$r['targetip'] = $proc['targetip'];
689

    
690
	$gateways_arr = get_gateways();
691

    
692
	unset($gw);
693
	if (isset($gateways_arr[$gwname])) {
694
		$gw = $gateways_arr[$gwname];
695
	}
696

    
697
	$r['latency_avg'] = round($r['latency_avg']/1000, 3);
698
	$r['latency_stddev'] = round($r['latency_stddev']/1000, 3);
699

    
700
	$r['status'] = "online";
701
	$r['substatus'] = "none";
702
	if (isset($gw) && isset($gw['force_down'])) {
703
		$r['status'] = "down";
704
		$r['substatus'] = "force_down";
705
	} else if (isset($gw)) {
706
		$settings = return_dpinger_defaults();
707

    
708
		$keys = array(
709
		    'latencylow',
710
		    'latencyhigh',
711
		    'losslow',
712
		    'losshigh'
713
		);
714

    
715
		/* Replace default values by user-defined */
716
		foreach ($keys as $key) {
717
			if (isset($gw[$key]) && is_numeric($gw[$key])) {
718
				$settings[$key] = $gw[$key];
719
			}
720
		}
721

    
722
		if ($r['latency_avg'] > $settings['latencyhigh']) {
723
			if (!$action_disable) {
724
				$r['status'] = "down";
725
			}
726
			$r['substatus'] = "highdelay";
727
		} else if ($r['loss'] > $settings['losshigh']) {
728
			if (!$action_disable) {
729
				$r['status'] = "down";
730
			}
731
			$r['substatus'] = "highloss";
732
		} else if ($r['latency_avg'] > $settings['latencylow']) {
733
			$r['substatus'] = "delay";
734
		} else if ($r['loss'] > $settings['losslow']) {
735
			$r['substatus'] = "loss";
736
		}
737
	}
738

    
739
	return $r;
740
}
741

    
742
/* return the status of the dpinger targets as an array */
743
function return_gateways_status($byname = false) {
744
	$dpinger_gws = running_dpinger_processes();
745
	$status = array();
746

    
747
	$gateways_arr = get_gateways();
748

    
749
	foreach ($dpinger_gws as $gwname => $gwdata) {
750
		/*
751
		 * If action is disabled for this gateway, then we want a
752
		 * detailed status.  That reports "highdelay" or "highloss"
753
		 * rather than just "down".  Because reporting the gateway
754
		 * down would be misleading (gateway action is disabled)
755
		 */
756
		$action_disable = $gateways_arr[$gwname]['action_disable'];
757
		$dpinger_status = get_dpinger_status($gwname, $action_disable);
758
		if ($dpinger_status === false) {
759
			continue;
760
		}
761

    
762
		if ($byname == false) {
763
			$target = $dpinger_status['targetip'];
764
		} else {
765
			$target = $gwname;
766
		}
767

    
768
		$status[$target] = array();
769
		$status[$target]['monitorip'] = $dpinger_status['targetip'];
770
		$status[$target]['srcip'] = $dpinger_status['srcip'];
771
		$status[$target]['name'] = $gwname;
772
		$status[$target]['delay'] =
773
		    empty($dpinger_status['latency_avg'])
774
		    ? "0ms"
775
		    : $dpinger_status['latency_avg'] . "ms";
776
		$status[$target]['stddev'] =
777
		    empty($dpinger_status['latency_stddev'])
778
		    ? "0ms"
779
		    : $dpinger_status['latency_stddev'] . "ms";
780
		$status[$target]['loss'] = empty($dpinger_status['loss'])
781
		    ? "0.0%"
782
		    : round($dpinger_status['loss'], 1) . "%";
783
		$status[$target]['status'] = $dpinger_status['status'];
784
		$status[$target]['substatus'] = $dpinger_status['substatus'];
785
	}
786

    
787
	/* tack on any gateways that have monitoring disabled
788
	 * or are down, which could cause gateway groups to fail */
789
	foreach ($gateways_arr as $gwitem) {
790
		if (!isset($gwitem['monitor_disable'])) {
791
			continue;
792
		}
793
		$srcips = get_interface_addresses($gwitem['interface']);
794
		if (is_ipaddrv4($gwitem['gateway'])) {
795
			$tgtip = $srcips['ipaddr'];
796
			$srcip = $tgtip;
797
		} elseif (is_ipaddrv6($gwitem['gateway'])) {
798
			$tgtip = $srcips['ipaddr6'];
799
			$srcip = $tgtip;
800
		} else {
801
			$tgtip = 'none';
802
			$srcip = null;
803
		}
804

    
805
		if ($byname == true) {
806
			$target = $gwitem['name'];
807
		} else {
808
			$target = $tgtip;
809
		}
810

    
811
		/* failsafe for down interfaces */
812
		if ($target == "none") {
813
			$target = $gwitem['name'];
814
			$status[$target]['name'] = $gwitem['name'];
815
			$status[$target]['delay'] = "0.0ms";
816
			$status[$target]['loss'] = "100.0%";
817
			$status[$target]['status'] = "down";
818
			$status[$target]['substatus'] = "down";
819
		} else {
820
			$status[$target]['monitorip'] = $tgtip;
821
			$status[$target]['srcip'] = $srcip;
822
			$status[$target]['name'] = $gwitem['name'];
823
			$status[$target]['delay'] = "";
824
			$status[$target]['loss'] = "";
825
			$status[$target]['status'] = "online";
826
			$status[$target]['substatus'] = "none";
827
		}
828

    
829
		$status[$target]['monitor_disable'] = true;
830
	}
831
	return($status);
832
}
833

    
834
function return_gateways_status_text($byname = false, $brief = false) {
835
	$gwstat = return_gateways_status($byname);
836
	$output = "";
837
	$widths = array();
838
	$col_sep = 2;
839
	if ($brief) {
840
		$collist = array('status' => "Status");
841
	} else {
842
		$collist = array('monitorip' => "Monitor",
843
				'srcip' => "Source",
844
				'delay' => "Delay",
845
				'stddev' => "StdDev",
846
				'loss' => "Loss",
847
				'status' => "Status",
848
				'substatus' => "Substatus");
849
	}
850
	foreach ($gwstat as $gw) {
851
		foreach ($gw as $gwdidx => $gwdata) {
852
			if (strlen($gwdata) > $widths[$gwdidx]) {
853
				$widths[$gwdidx] = strlen($gwdata);
854
			}
855
		}
856
	}
857

    
858
	$output .= str_pad("Name", $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
859
	foreach ($collist as $hdrcol => $hdrdesc) {
860
		if (strlen($hdrdesc) > $widths[$hdrcol]) {
861
			$widths[$hdrcol] = strlen($hdrdesc);
862
		}
863
		$output .= str_pad($hdrdesc, $widths[$hdrcol] + $col_sep, " ", (substr($hdrcol, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
864
	}
865
	$output .= "\n";
866

    
867
	foreach ($gwstat as $idx => $gw) {
868
		$output .= str_pad($gw['name'], $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
869
		foreach (array_keys($collist) as $col) {
870
			$output .= str_pad($gw[$col], $widths[$col] + $col_sep, " ", (substr($col, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
871
		}
872
		$output .= "\n";
873
	}
874

    
875
	return $output;
876
}
877

    
878
function compare_gateway_order_configured($a, $b) {
879
	/* XXX WAN always has precedence */
880
	if ($a['friendlyiface'] == "wan") {
881
		return -1;
882
	} elseif ($b['friendlyiface'] == "wan") {
883
		return 1;
884
	}
885

    
886
	if ($a['attribute'] === $b['attribute']) {
887
		if ($a['attribute'] === 'system') {
888
			$res = (($a['name'] < $b['name'])) ? -1 : 1;
889
			return $res;
890
		}
891
		return 0;
892
	}
893
	if ($a['attribute'] === 'system' || $b['attribute'] === 'system') {
894
		$res = (($b['attribute'] === 'system')) ? -1 : 1;
895
		return $res;
896
	}
897
	$res = ($a['attribute'] < $b['attribute']) ? -1 : 1;
898
	return $res;
899
}
900

    
901
function order_gateways_as_configured($gateways_arr) {
902
	uasort($gateways_arr, 'compare_gateway_order_configured');
903
	return $gateways_arr;
904
}
905

    
906
/* Return all configured gateways on the system
907
   $disabled = true - include gateways that are disabled
908
   $localhost = true - include "Null" entries for localhost IP addresses
909
   $inactive = true - include gateways on inactive interfaces
910
*/
911
function return_gateways_array($disabled = false, $localhost = false, $inactive = false) {
912
	$gateways_arr = array();
913
	$gateways_arr_temp = array();
914
	$cgw4 = route_get_default('inet');
915
	$cgw6 = route_get_default('inet6');
916
	$found_defaultv4 = 0;
917
	$found_defaultv6 = 0;
918

    
919
	// Ensure the interface cache is up to date first
920
	$interfaces = get_interface_arr(true);
921

    
922
	$i = -1;
923
	/* Process/add all the configured gateways. */
924
	foreach (config_get_path('gateways/gateway_item', []) as $gateway) {
925
		if (!is_array($gateway) || empty($gateway)) {
926
			continue;
927
		}
928

    
929
		/* Increment it here to do not skip items */
930
		$i++;
931
		if (isset($gateway['defaultgw'])) {
932
			unset($gateway['defaultgw']);
933
		}
934

    
935
		if (empty(config_get_path("interfaces/{$gateway['interface']}"))) {
936
			if ($inactive === false) {
937
				continue;
938
			} else {
939
				$gateway['inactive'] = true;
940
			}
941
		}
942
		$wancfg = config_get_path("interfaces/{$gateway['interface']}");
943

    
944
		/* skip disabled interfaces */
945
		if ($disabled === false && (!isset($wancfg['enable']))) {
946
			continue;
947
		}
948

    
949
		/* if the gateway is dynamic and we can find the IPv4, Great! */
950
		if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
951
			if ($gateway['ipprotocol'] == "inet") {
952
				/* we know which interfaces is dynamic, this should be made a function */
953
				$gateway['gateway'] = get_interface_gateway($gateway['interface']);
954
				/* no IP address found, set to dynamic */
955
				if (!is_ipaddrv4($gateway['gateway'])) {
956
					$gateway['gateway'] = "dynamic";
957
				}
958
				$gateway['dynamic'] = true;
959
			}
960

    
961
			/* if the gateway is dynamic and we can find the IPv6, Great! */
962
			else if ($gateway['ipprotocol'] == "inet6") {
963
				/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
964
				$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
965
				/* no IPv6 address found, set to dynamic */
966
				if (!is_ipaddrv6($gateway['gateway'])) {
967
					$gateway['gateway'] = "dynamic";
968
				}
969
				$gateway['dynamic'] = true;
970
			}
971
		} else {
972
			/* getting this detection right is hard at this point because we still don't
973
				* store the address family in the gateway item */
974
			if (is_ipaddrv4($gateway['gateway'])) {
975
				$gateway['ipprotocol'] = "inet";
976
			} else if (is_ipaddrv6($gateway['gateway'])) {
977
				$gateway['ipprotocol'] = "inet6";
978
			}
979
		}
980

    
981
		if (isset($gateway['monitor_disable'])) {
982
			$gateway['monitor_disable'] = true;
983
		} else if (empty($gateway['monitor'])) {
984
			$gateway['monitor'] = $gateway['gateway'];
985
		}
986

    
987
		if (isset($gateway['action_disable'])) {
988
			$gateway['action_disable'] = true;
989
		}
990

    
991
		$gateway['friendlyiface'] = $gateway['interface'];
992
		$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($gateway['interface']);
993

    
994
		/* special treatment for tunnel interfaces */
995
		if ($gateway['ipprotocol'] == "inet6") {
996
			$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
997
		} else {
998
			$gateway['interface'] = get_real_interface($gateway['interface'], "inet", false, false);
999
		}
1000

    
1001
		if ($gateway['ipprotocol'] == "inet" &&
1002
				($gateway['gateway'] == $cgw4)) {
1003
			$gateway['isdefaultgw'] = true;
1004
			$found_defaultv4 = 1;
1005
		} else if ($gateway['ipprotocol'] == "inet6" &&
1006
				($gateway['gateway'] == $cgw6)) {
1007
			$gateway['isdefaultgw'] = true;
1008
			$found_defaultv6 = 1;
1009
		}
1010
		/* include the gateway index as the attribute */
1011
		$gateway['attribute'] = $i;
1012

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

    
1017
		/* skip disabled gateways if the caller has not asked for them to be returned. */
1018
		if (!($disabled === false && isset($gateway['disabled']))) {
1019
			$gateways_arr[$gateway['name']] = $gateway;
1020
		}
1021
	}
1022
	unset($gateway);
1023

    
1024
	//Sort the array by GW name before moving on.
1025
	ksort($gateways_arr, SORT_STRING | SORT_FLAG_CASE);
1026

    
1027
	/* Loop through all interfaces with a gateway and add it to a array */
1028
	if ($disabled == false) {
1029
		$iflist = get_configured_interface_with_descr();
1030
	} else {
1031
		$iflist = get_configured_interface_with_descr(true);
1032
	}
1033

    
1034
	/* Process/add dynamic v4 gateways. */
1035
	foreach ($iflist as $ifname => $friendly) {
1036
		if (!interface_has_gateway($ifname)) {
1037
			continue;
1038
		}
1039

    
1040
		if (empty(config_get_path("interfaces/{$ifname}"))) {
1041
			continue;
1042
		}
1043

    
1044
		$ifcfg = config_get_path("interfaces/{$ifname}");
1045
		if (!isset($ifcfg['enable'])) {
1046
			continue;
1047
		}
1048

    
1049
		if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
1050
			continue;
1051
		}
1052

    
1053
		$ctype = "";
1054
		switch ($ifcfg['ipaddr']) {
1055
			case "dhcp":
1056
			case "pppoe":
1057
			case "l2tp":
1058
			case "pptp":
1059
			case "ppp":
1060
				$ctype = strtoupper($ifcfg['ipaddr']);
1061
				break;
1062
			default:
1063
				$tunnelif = substr($ifcfg['if'], 0, 3);
1064
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
1065
					switch (substr($ifcfg['if'], 4, 1)) {
1066
						case "c":
1067
							$ovpntype = "openvpn-client";
1068
							break;
1069
						case "s":
1070
							$ovpntype = "openvpn-server";
1071
							break;
1072
						default:
1073
							// unknown ovpn type
1074
							continue 3;
1075
					}
1076
					$ovpnid = substr($ifcfg['if'], 5);
1077
					$found = false;
1078
					foreach (config_get_path("openvpn/{$ovpntype}", []) as $ovpnconf) {
1079
						if (empty($ovpnconf)) {
1080
							continue;
1081
						}
1082
						if ($ovpnconf['vpnid'] == $ovpnid) {
1083
							$found = true;
1084
							// skip IPv6-only interfaces
1085
							if ($ovpnconf['create_gw'] == "v6only") {
1086
								continue 3;
1087
							}
1088
							// skip tap interfaces
1089
							if ($ovpnconf['dev_mode'] == "tap") {
1090
								continue 3;
1091
							}
1092
						}
1093
					}
1094
					if (!$found) {
1095
						// skip unexpected interfaces, e.g. QinQ such as ovpns6.600.601
1096
						continue 2;
1097
					}
1098
					$ctype = "VPNv4";
1099
				} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
1100
					$ikeid = substr($ifcfg['if'], 5);
1101
					if (!empty(config_get_path('ipsec/phase1', [])) &&
1102
					    !empty(config_get_path('ipsec/phase2', []))) {
1103
						foreach (config_get_path('ipsec/phase1', []) as $ph1ent) {
1104
							if (empty($ph1ent) || $ph1ent['disabled']) {
1105
								continue;
1106
							}
1107
							$vtisubnet_spec = ipsec_vti($ph1ent, true);
1108
							// Skip non-VTI tunnels
1109
							if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
1110
								continue;
1111
							}
1112
							if (!isset($ph1ent['mobile']) && ($ph1ent['iketype'] == 'ikev1' || isset($ph1ent['splitconn']))) {
1113
								foreach ($vtisubnet_spec as $idx => $vtisub) {
1114
									if ($ifcfg['if'] == ipsec_get_ifname($ph1ent, $vtisub['reqid'])) {
1115
										// If this specific VTI remote is v4, then we can make a v4 gw
1116
										if (is_ipaddrv4($vtisub['right'])) {
1117
											$ctype = "VTIv4";
1118
										}
1119
									}
1120
								}
1121
							} else {
1122
								if ($ifcfg['if'] == ipsec_get_ifname($ph1ent)) {
1123
									// If any of the VTI remotes are v4, then we can make a v4 gw
1124
									foreach ($vtisubnet_spec as $vtisub) {
1125
										if (is_ipaddrv4($vtisub['right'])) {
1126
											$ctype = "VTIv4";
1127
										}
1128
									}
1129
								}
1130
							}
1131
						}
1132
						if (empty($ctype)) {
1133
							continue 2;
1134
						}
1135
					}
1136
				} elseif ($tunnelif == "gif" || $tunnelif == "gre") {
1137
					$ctype = "TUNNELv4";
1138
				}
1139
				break;
1140
		}
1141
		$ctype = "_". strtoupper($ctype);
1142

    
1143
		$gateway = array();
1144
		$gateway['dynamic'] = false;
1145
		$gateway['ipprotocol'] = "inet";
1146
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
1147
		$gateway['interface'] = get_real_interface($ifname);
1148
		$gateway['friendlyiface'] = $ifname;
1149
		$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
1150
		$gateway['name'] = "{$friendly}{$ctype}";
1151
		$gateway['attribute'] = "system";
1152

    
1153
		if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
1154
			$gateway['isdefaultgw'] = true;
1155
			$gateway['dynamic'] = true;
1156
			$found_defaultv4 = 1;
1157
		}
1158

    
1159
		/* Loopback dummy for dynamic interfaces without a IP */
1160
		if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
1161
			$gateway['gateway'] = "dynamic";
1162
		}
1163

    
1164
		/* automatically skip known static and dynamic gateways that were previously processed */
1165
		foreach ($gateways_arr_temp as $gateway_item) {
1166
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
1167
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
1168
				continue 2;
1169
			}
1170
		}
1171

    
1172
		if (is_ipaddrv4($gateway['gateway'])) {
1173
			$gateway['monitor'] = $gateway['gateway'];
1174
		}
1175

    
1176
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
1177
		$gateways_arr[$gateway['name']] = $gateway;
1178
	}
1179
	unset($gateway);
1180

    
1181
	/* Process/add dynamic v6 gateways. */
1182
	foreach ($iflist as $ifname => $friendly) {
1183
		/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
1184
		if (!config_path_enabled('system', 'ipv6allow')) {
1185
			break;
1186
		}
1187

    
1188
		if (!interface_has_gatewayv6($ifname)) {
1189
			continue;
1190
		}
1191

    
1192
		if (empty(config_get_path("interfaces/{$ifname}"))) {
1193
			continue;
1194
		}
1195

    
1196
		$ifcfg = config_get_path("interfaces/{$ifname}");
1197
		if (!isset($ifcfg['enable'])) {
1198
			continue;
1199
		}
1200

    
1201
		if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
1202
			continue;
1203
		}
1204

    
1205
		$ctype = "";
1206
		switch ($ifcfg['ipaddrv6']) {
1207
			case "slaac":
1208
			case "dhcp6":
1209
			case "6to4":
1210
			case "6rd":
1211
				$ctype = strtoupper($ifcfg['ipaddrv6']);
1212
				break;
1213
			default:
1214
				$tunnelif = substr($ifcfg['if'], 0, 3);
1215
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
1216
					switch (substr($ifcfg['if'], 4, 1)) {
1217
						case "c":
1218
							$ovpntype = "openvpn-client";
1219
							break;
1220
						case "s":
1221
							$ovpntype = "openvpn-server";
1222
							break;
1223
						default:
1224
							// unknown ovpn type
1225
							continue 3;
1226
					}
1227
					$ovpnid = substr($ifcfg['if'], 5);
1228
					$found = false;
1229
					foreach (config_get_path("openvpn/{$ovpntype}", []) as $ovpnconf) {
1230
						if (empty($ovpnconf)) {
1231
							continue;
1232
						}
1233
						if ($ovpnconf['vpnid'] == $ovpnid) {
1234
							$found = true;
1235
							// skip IPv4-only interfaces
1236
							if ($ovpnconf['create_gw'] == "v4only") {
1237
								continue 3;
1238
							}
1239
							// skip tap interfaces
1240
							if ($ovpnconf['dev_mode'] == "tap") {
1241
								continue 3;
1242
							}
1243
						}
1244
					}
1245
					if (!$found) {
1246
						// skip unexpected interfaces, e.g. QinQ such as ovpns6.600.601
1247
						continue 2;
1248
					}
1249
					$ctype = "VPNv6";
1250
				} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
1251
					$ikeid = substr($ifcfg['if'], 5);
1252
					if (!empty(config_get_path('ipsec/phase1', [])) &&
1253
					    !empty(config_get_path('ipsec/phase2', []))) {
1254
						foreach (config_get_path('ipsec/phase1', []) as $ph1ent) {
1255
							if (empty($ph1ent) || $ph1ent['disabled']) {
1256
								continue;
1257
							}
1258
							$vtisubnet_spec = ipsec_vti($ph1ent, true);
1259
							// Skip non-VTI tunnels
1260
							if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
1261
								continue;
1262
							}
1263
							if (!isset($ph1ent['mobile']) && ($ph1ent['iketype'] == 'ikev1' || isset($ph1ent['splitconn']))) {
1264
								foreach ($vtisubnet_spec as $idx => $vtisub) {
1265
									if ($ifcfg['if'] == ipsec_get_ifname($ph1ent, $vtisub['reqid'])) {
1266
										// If this specific VTI remote is v6, then we can make a v6 gw
1267
										if (is_ipaddrv6($vtisub['right'])) {
1268
											$ctype = "VTIv6";
1269
										}
1270
									}
1271
								}
1272
							} else {
1273
								if ($ifcfg['if'] == ipsec_get_ifname($ph1ent)) {
1274
									// If any of the VTI remotes are v6, then we can make a v6 gw
1275
									foreach ($vtisubnet_spec as $vtisub) {
1276
										if (is_ipaddrv6($vtisub['right'])) {
1277
											$ctype = "VTIv6";
1278
										}
1279
									}
1280
								}
1281
							}
1282
						}
1283
						if (empty($ctype)) {
1284
							continue 2;
1285
						}
1286
					}
1287
				} else if ($tunnelif == "gif" || $tunnelif == "gre") {
1288
					$ctype = "TUNNELv6";
1289
				}
1290
				break;
1291
		}
1292
		$ctype = "_". strtoupper($ctype);
1293

    
1294
		$gateway = array();
1295
		$gateway['dynamic'] = false;
1296
		$gateway['ipprotocol'] = "inet6";
1297
		$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
1298
		$gateway['interface'] = get_real_interface($ifname, "inet6");
1299
		switch ($ifcfg['ipaddrv6']) {
1300
			case "6rd":
1301
			case "6to4":
1302
				$gateway['dynamic'] = "default";
1303
				break;
1304
		}
1305
		$gateway['friendlyiface'] = $ifname;
1306
		$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
1307
		$gateway['name'] = "{$friendly}{$ctype}";
1308
		$gateway['attribute'] = "system";
1309

    
1310
		if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) {
1311
			$gateway['isdefaultgw'] = true;
1312
			$gateway['dynamic'] = true;
1313
			$found_defaultv6 = 1;
1314
		}
1315

    
1316
		/* Loopback dummy for dynamic interfaces without a IP */
1317
		if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
1318
			$gateway['gateway'] = "dynamic";
1319
		}
1320

    
1321
		/* automatically skip known static and dynamic gateways that were previously processed */
1322
		foreach ($gateways_arr_temp as $gateway_item) {
1323
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
1324
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
1325
				continue 2;
1326
			}
1327
		}
1328

    
1329
		if (is_ipaddrv6($gateway['gateway'])) {
1330
			$gateway['monitor'] = $gateway['gateway'];
1331
		}
1332

    
1333
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
1334
		$gateways_arr[$gateway['name']] = $gateway;
1335
	}
1336
	unset($gateway);
1337

    
1338
	/* FIXME: Should this be enabled.
1339
	 * Some interface like wan might be default but have no info recorded
1340
	 * the config. */
1341
	/* this is a fallback if all else fails and we want to get packets out @smos */
1342
	if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
1343
		foreach ($gateways_arr as &$gateway) {
1344
			if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
1345
				if (file_exists(g_get('tmp_path', '') . "/{$gateway['interface']}_defaultgw")) {
1346
					$gateway['isdefaultgw'] = true;
1347
					$found_defaultv4 = 1;
1348
				}
1349
			}
1350
			else if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
1351
				if (file_exists(g_get('tmp_path', '') . "/{$gateway['interface']}_defaultgwv6")) {
1352
					$gateway['isdefaultgw'] = true;
1353
					$found_defaultv6 = 1;
1354
				}
1355
			}
1356
		}
1357
	}
1358

    
1359
	if ($localhost === true) {
1360
		/* attach localhost for Null routes */
1361
		$gwlo4 = array();
1362
		$gwlo4['name'] = "Null4";
1363
		$gwlo4['interface'] = "lo0";
1364
		$gwlo4['ipprotocol'] = "inet";
1365
		$gwlo4['gateway'] = "127.0.0.1";
1366
		$gwlo4['attribute'] = "system";
1367
		$gwlo6 = array();
1368
		$gwlo6['name'] = "Null6";
1369
		$gwlo6['interface'] = "lo0";
1370
		$gwlo6['ipprotocol'] = "inet6";
1371
		$gwlo6['gateway'] = "::1";
1372
		$gwlo6['attribute'] = "system";
1373
		$gateways_arr['Null4'] = $gwlo4;
1374
		$gateways_arr['Null6'] = $gwlo6;
1375
	}
1376

    
1377
	if ($found_defaultv4 != 1 && is_ipaddr($cgw4)) {
1378
		foreach($gateways_arr as &$gw) {
1379
			if ($gw['gateway'] == $cgw4) {
1380
				$gw['isdefaultgw'] = true;
1381
			}
1382
		}
1383
	}
1384
	if ($found_defaultv6 != 1 && is_ipaddr($cgw6)) {
1385
		foreach($gateways_arr as &$gw) {
1386
			if ($gw['gateway'] == $cgw6) {
1387
				$gw['isdefaultgw'] = true;
1388
			}
1389
		}
1390
	}
1391

    
1392
	$gways = order_gateways_as_configured($gateways_arr);
1393

    
1394
	// Add the tier names here so that system_gateways.php doesn't need to
1395
	foreach ($gways as $idx => $gway) {
1396
		$gways[$idx]['tiername'] = gateway_getgwtiername($gways, $idx);
1397
	}
1398

    
1399
	return $gways;
1400
}
1401

    
1402
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) {
1403
	/*
1404
	 * NOTE: The code below is meant to replace the default gateway when it goes down.
1405
	 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
1406
	 */
1407
	$set_dfltgwname = '';
1408

    
1409
	if ($ipprotocol == 'inet') {
1410
		$gwdefault = config_get_path('gateways/defaultgw4', "");
1411
	} else {
1412
		$gwdefault = config_get_path('gateways/defaultgw6', "");
1413
	}
1414
	/* do not delete dynamic (frr/bgp/ospf) route
1415
	 * see https://redmine.pfsense.org/issues/12536 */
1416
	if ($gwdefault == "-") {
1417
		if (!is_dynamic_route('default', $ipprotocol)) {
1418
			route_del('default', $ipprotocol);
1419
		}
1420
		return;
1421
	}
1422
	if (isset($gateways_arr[$gwdefault])) {
1423
		// the configured gateway is a regular one. (not a gwgroup) use it as is..
1424
		$set_dfltgwname = $gwdefault;
1425
	} elseif (empty($gwdefault)) {
1426
		// 'automatic' mode, pick the first one thats 'up' or 'unmonitored' which is always considered up
1427
		$gateways_arr = order_gateways_as_configured($gateways_arr);
1428
		$fallback = "";
1429
		foreach($gateways_arr as $gwname => $gwsttng) {
1430
			if (($gwsttng['ipprotocol'] != $ipprotocol) || isset($gwsttng['force_down'])) {
1431
				continue;
1432
			}
1433

    
1434
			if (isset($gwsttng['monitor_disable']) || isset($gwsttng['action_disable']) ||
1435
			    ($gateways_status[$gwname]['status'] == "online")) {
1436
				$set_dfltgwname = $gwname;
1437
				break;
1438
			}
1439
			if (empty($fallback) && $gwsttng['interface'] != 'lo0') {
1440
				$fallback = $gwname;
1441
			}
1442
		}
1443
		if (empty($set_dfltgwname) && !empty($fallback)) {
1444
			log_error(sprintf("Gateway, none 'available' for %s, use the first one configured. '%s'", $ipprotocol, $fallback));
1445
			$set_dfltgwname = $fallback;
1446
		} else {
1447
			log_error("Gateway, NONE AVAILABLE");
1448
		}
1449
	} else {
1450
		// a gwgroup is selected
1451
		// find the best available gateway given options available..
1452
		$gwg_members = array();
1453
		$viplist = get_configured_vip_list();
1454
		foreach (config_get_path('gateways/gateway_group', []) as $group) {
1455
			if ($group['name'] == $gwdefault) {
1456
				// finds the gw members of the best available tier for this group.
1457
				$gwg_members = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1458
			}
1459
		}
1460

    
1461
		if (count($gwg_members) > 0) {
1462
			$currentdefaultgwip = route_get_default($ipprotocol);
1463
			$found_current = false;
1464
			foreach($gwg_members as $gwgroupitem) {
1465
				if (!empty($currentdefaultgwip) &&
1466
				    is_array($gwgroupitem) &&
1467
				    ($gwgroupitem['gwip'] == $currentdefaultgwip)) {
1468
					$set_dfltgwname = $gwgroupitem['gw'];
1469
					$found_current = true;
1470
					if (config_path_enabled('system', 'gw-debug')) {
1471
						log_error("Keep current gateway, its already part of the group members.");
1472
					}
1473
					break;
1474
				}
1475
			}
1476
			if (!$found_current) {
1477
				$set_dfltgwname = $gwg_members[0]['gw'];
1478
				log_error(sprintf("Gateway, switch to: %s", $set_dfltgwname));
1479
			}
1480
		} else {
1481
			log_error("Gateway, NONE AVAILABLE");
1482
		}
1483
	}
1484
	if (!empty($set_dfltgwname) && isset($gateways_arr[$set_dfltgwname])) {
1485
		setdefaultgateway($gateways_arr[$set_dfltgwname]);
1486
	} elseif (empty($set_dfltgwname)) {
1487
		route_del('default', $ipprotocol);
1488
	}
1489
}
1490

    
1491
function setdefaultgateway($gw) {
1492
	if (config_path_enabled('system', 'route-debug')) {
1493
		file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']}");
1494
	}
1495
	$ipprotocol = $gw['ipprotocol'];
1496
	if ($gw['gateway'] == "dynamic") {
1497
		if ($ipprotocol == 'inet') {
1498
			$gw['gateway'] = get_interface_gateway($gw['friendlyiface']);
1499
		} else {
1500
			$gw['gateway'] = get_interface_gateway_v6($gw['friendlyiface']);
1501
		}
1502
	}
1503
	if ($ipprotocol == 'inet6' && !is_ipaddrv6($gw['gateway'])) {
1504
		return;
1505
	}
1506
	if ($ipprotocol == 'inet' && !is_ipaddrv4($gw['gateway'])) {
1507
		return;
1508
	}
1509
	if ($ipprotocol == 'inet6') {
1510
		if (is_linklocal($gw['gateway']) && get_ll_scope($gw['gateway']) == '') {
1511
			$gw['gateway'] .= "%" . $gw['interface'];
1512
		}
1513
	}
1514
	$currentdefaultgwip = route_get_default($ipprotocol);
1515
	if ($currentdefaultgwip != $gw['gateway']) {
1516
		log_error("Default gateway setting {$gw['descr']} as default.");
1517

    
1518
		if ($ipprotocol == 'inet') {
1519
			$inet = '';
1520
		} else {
1521
			$inet = 'v6';
1522
		}
1523
		unlink_if_exists(g_get('tmp_path', '') . "/*_defaultgw{$inet}");
1524
		$defaultif = get_real_interface($gw['interface']);
1525
		if ($defaultif) {
1526
			@file_put_contents(g_get('tmp_path', '') . "/{$defaultif}_defaultgw{$inet}", $gw['gateway']);
1527
		}
1528

    
1529
		if (isset($gw["nonlocalgateway"])) {
1530
			if (is_ipaddr($gw['gateway']) && !empty($gw['interface'])) {
1531
				route_add_or_change($gw['gateway'], '',
1532
				    $gw['interface']);
1533
			}
1534
		}
1535
		if (config_path_enabled('system', 'route-debug')) {
1536
			file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']} ({$gw['gateway']})");
1537
		}
1538
		route_add_or_change("default", $gw['gateway'], '', '',
1539
		    $ipprotocol);
1540
		return true;
1541
	}
1542
}
1543

    
1544
function get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist){
1545
	$result = array();
1546
	/* create array with group gateways members separated by tier */
1547
	$tiers = array();
1548
	$backupplan = array();
1549
	$gwvip_arr = array();
1550
	foreach ($group['item'] as $item) {
1551
		list($gwname, $tier, $vipname) = explode("|", $item);
1552

    
1553
		if (is_ipaddr($viplist[$vipname])) {
1554
			if (!is_array($gwvip_arr[$group['name']])) {
1555
				$gwvip_arr[$group['name']] = array();
1556
			}
1557
			$gwvip_arr[$group['name']][$gwname] = $vipname;
1558
		}
1559

    
1560
		/* Do it here rather than reiterating again the group in case no member is up. */
1561
		if (!is_array($backupplan[$tier])) {
1562
			$backupplan[$tier] = array();
1563
		}
1564
		$backupplan[$tier][] = $gwname;
1565

    
1566
		/* check if the gateway is available before adding it to the array */
1567
		if (is_array($gateways_status[$gwname])) {
1568
			$status = $gateways_status[$gwname];
1569
			$gwdown = false;
1570
			if (stristr($status['status'], "down")) {
1571
				$gwdown = true;
1572
				switch ($status['substatus']) {
1573
					case "highloss":
1574
						$msg = sprintf(gettext('MONITOR: %1$s has packet loss, omitting from routing group %2$s'), $gwname, $group['name']);
1575
						break;
1576
					case "highdelay":
1577
						$msg = sprintf(gettext('MONITOR: %1$s has high latency, omitting from routing group %2$s'), $gwname, $group['name']);
1578
						break;
1579
					default:
1580
						$msg = sprintf(gettext('MONITOR: %1$s is down, omitting from routing group %2$s'), $gwname, $group['name']);
1581
				}
1582
			}
1583
			$statuschanged = false;
1584
			$pluginparams = array();
1585
			$pluginparams['type'] = 'gateway';
1586
			$pluginparams['name'] = $gwname;
1587
			if ($gwdown == true) {
1588
				if (!file_exists("/tmp/.down.{$gwname}")) {
1589
					@touch("/tmp/.down.{$gwname}");
1590
					$msg .= "\n".implode("|", $status);
1591
					$pluginparams['event'] = 'gateway.down';
1592
					$statuschanged = true;
1593
				}
1594
			} else {
1595
				/* Online add member */
1596
				if (!is_array($tiers[$tier])) {
1597
					$tiers[$tier] = array();
1598
				}
1599
				$tiers[$tier][] = $gwname;
1600
				if (unlink_if_exists("/tmp/.down.{$gwname}")) {
1601
					$msg = sprintf(gettext('MONITOR: %1$s is available now, adding to routing group %2$s'), $gwname, $group['name']);
1602
					$msg .= "\n".implode("|", $status);
1603
					$pluginparams['event'] = 'gateway.up';
1604
					$statuschanged = true;
1605
				}
1606
			}
1607
			if ($statuschanged) {
1608
				log_error($msg);
1609
				notify_all_remote($msg);
1610
				if (isset($gateways_arr[$gwname]['interface'])) {
1611
					$pluginparams['interface'] = $gateways_arr[$gwname]['interface'];
1612
				}
1613
				pkg_call_plugins('plugin_gateway', $pluginparams);
1614
			}
1615
		} else if (isset($gateways_arr[$gwname]['monitor_disable']) || isset($gateways_arr[$gwname]['action_disable'])) {
1616
			$tiers[$tier][] = $gwname;
1617
		}
1618
	}
1619
	$tiers_count = count($tiers);
1620
	if ($tiers_count == 0) {
1621
		/* Oh dear, we have no members! Engage Plan B */
1622
		if (config_path_enabled('system', 'gw-debug') && (!is_platform_booting())) {
1623
			$msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']);
1624
			log_error($msg);
1625
		}
1626
		$tiers = $backupplan;
1627
	}
1628
	/* sort the tiers array by the tier key */
1629
	ksort($tiers);
1630

    
1631
	/* we do not really foreach the tiers as we stop after the first tier */
1632
	foreach ($tiers as $tieridx => $tier) {
1633
		/* process all gateways in this tier */
1634
		foreach ($tier as $member) {
1635
			/* determine interface gateway */
1636
			if (isset($gateways_arr[$member])) {
1637
				$gateway = $gateways_arr[$member];
1638
				$int = $gateway['interface'];
1639
				$gatewayip = "";
1640
				if (is_ipaddr($gateway['gateway'])) {
1641
					$gatewayip = $gateway['gateway'];
1642
				} elseif (!empty($int)) {
1643
					if ($gateway['ipprotocol'] == 'inet') {
1644
						$gatewayip = get_interface_gateway($gateway['friendlyiface']);
1645
					} else {
1646
						$gatewayip = get_interface_gateway_v6($gateway['friendlyiface']);
1647
					}
1648
				}
1649

    
1650
				if (!empty($int)) {
1651
					$result['ipprotocol'] = $gateway['ipprotocol'];
1652
					if (is_ipaddr($gatewayip)) {
1653
						$groupmember = array();
1654
						$groupmember['gw'] = $member;
1655
						$groupmember['int'] = $int;
1656
						$groupmember['gwip'] = $gatewayip;
1657
						/* set correct linklocal gateway address,
1658
						 * see https://redmine.pfsense.org/issues/12721 */
1659
						if (is_ipaddrv6($gatewayip) && is_linklocal($gatewayip) && empty(get_ll_scope($gatewayip))) {
1660
							$groupmember['gwip'] .= '%' . $int;
1661
						}
1662
						$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
1663
						if (is_array($gwvip_arr[$group['name']]) && !empty($gwvip_arr[$group['name']][$member])) {
1664
							$groupmember['vip'] = $gwvip_arr[$group['name']][$member];
1665
						}
1666
						$result[] = $groupmember;
1667
					}
1668
				}
1669
			}
1670
		}
1671
		/* we should have the 1st available tier now, exit stage left */
1672
		if (count($result) > 0) {
1673
			break;
1674
		} else {
1675
			log_error(sprintf(gettext('GATEWAYS: Group %1$s did not have any gateways up on tier %2$s!'), $group['name'], $tieridx));
1676
		}
1677
	}
1678
	// Add description field last to not influence the count() above
1679
	$result['descr'] = $group['descr'];
1680
	return $result;
1681
}
1682

    
1683
function get_gwgroup_members($groupname) {
1684
	$gateways_status = return_gateways_status(true);
1685
	$gateways_arr = get_gateways();
1686
	$viplist = get_configured_vip_list();
1687
	foreach (config_get_path('gateways/gateway_group', []) as $group) {
1688
		if ($group['name'] == $groupname) {
1689
			return get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1690
		}
1691
	}
1692
	return array();
1693
}
1694

    
1695
/*
1696
 * Return an array with all gateway groups with name as key
1697
 * All gateway groups will be processed before returning the array.
1698
 * Returns the first online tier (potentially with multiple members);
1699
 * return the first tier if none are online.
1700
 */
1701
function return_gateway_groups_array($fixup = false) {
1702
	/* fetch the current gateways status */
1703
	$gateways_status = return_gateways_status(true);
1704
	$gateways_arr = get_gateways();
1705
	$gateway_groups_array = array();
1706
	if ($fixup == true) {
1707
		$gw4 = lookup_gateway_or_group_by_name(config_get_path('gateways/defaultgw4', ""));
1708
		$gw6 = lookup_gateway_or_group_by_name(config_get_path('gateways/defaultgw6', ""));
1709
		if ($gw4 && $gw4['type'] == 'gatewaygroup') {
1710
			fixup_default_gateway("inet", $gateways_status, $gateways_arr);
1711
		}
1712
		if ($gw6 && $gw6['type'] == 'gatewaygroup') {
1713
			fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
1714
		}
1715
	}
1716
	if (!empty(config_get_path('gateways/gateway_group'))) {
1717
		$viplist = get_configured_vip_list();
1718
		foreach (config_get_path('gateways/gateway_group', []) as $group) {
1719
			$gateway_groups_array[$group['name']] = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1720
		}
1721
	}
1722

    
1723
	return ($gateway_groups_array);
1724
}
1725

    
1726
/* Update DHCP WAN Interface ip address in gateway group item */
1727
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
1728
	$gwlist = config_get_path('gateways/gateway_item', []);
1729
	foreach ($gwlist as & $gw) {
1730
		if ($gw['interface'] != $interface) {
1731
			continue;
1732
		}
1733

    
1734
		$current_gw = get_interface_gateway($interface);
1735
		if ($gw['gateway'] <> $current_gw) {
1736
			$gw['gateway'] = $current_gw;
1737
			$changed = true;
1738
		}
1739
	}
1740
	config_set_path('gateways/gateway_item', $gwlist);
1741

    
1742
	if ($changed && $current_gw) {
1743
		write_config(sprintf(gettext(
1744
		    'Updating gateway group gateway for %1$s - new gateway is %2$s'),
1745
		    $interface, $current_gw));
1746
	}
1747
}
1748

    
1749
function lookup_gateway_or_group_by_name($gwname) {
1750
	$gateways_arr = get_gateways();
1751

    
1752
	foreach ($gateways_arr as $gw) {
1753
		if ($gw['name'] == $gwname) {
1754
			$gw['type'] = 'gateway';
1755
			return $gw;
1756
		}
1757
	}
1758

    
1759
	foreach (config_get_path('gateways/gateway_group', []) as $gwg) {
1760
		if ($gwg['name'] == $gwname) {
1761
			$gwg['type'] = 'gatewaygroup';
1762
			return $gwg;
1763
		}
1764
	}
1765

    
1766
	return false;
1767
}
1768

    
1769
function lookup_gateway_ip_by_name($name, $disabled = false) {
1770

    
1771
	$gateways_arr = get_gateways(($disabled ? GW_CACHE_DISABLED : 0) | GW_CACHE_LOCALHOST);
1772
	foreach ($gateways_arr as $gname => $gw) {
1773
		if ($gw['name'] === $name || $gname === $name) {
1774
			return $gw['gateway'];
1775
		}
1776
	}
1777

    
1778
	return false;
1779
}
1780

    
1781
function lookup_gateway_monitor_ip_by_name($name) {
1782

    
1783
	$gateways_arr = get_gateways(GW_CACHE_LOCALHOST);
1784
	if (!empty($gateways_arr[$name])) {
1785
		$gateway = $gateways_arr[$name];
1786
		if (!is_ipaddr($gateway['monitor'])) {
1787
			return $gateway['gateway'];
1788
		}
1789

    
1790
		return $gateway['monitor'];
1791
	}
1792

    
1793
	return (false);
1794
}
1795

    
1796
function lookup_gateway_interface_by_name($name) {
1797

    
1798
	$gateways_arr = get_gateways(GW_CACHE_LOCALHOST);
1799
	if (!empty($gateways_arr[$name])) {
1800
		$interfacegw = $gateways_arr[$name]['friendlyiface'];
1801
		return ($interfacegw);
1802
	}
1803

    
1804
	return (false);
1805
}
1806

    
1807
function get_root_interface($interface) {
1808
	if (substr($interface, 0, 4) == '_vip') {
1809
		$interface = get_configured_vip_interface($interface);
1810
		if (substr($interface, 0, 4) == '_vip') {
1811
			$interface = get_configured_vip_interface($interface);
1812
		}
1813
	}
1814
	return $interface;
1815
}
1816

    
1817
function get_interface_gateway($interface, &$dynamic = false) {
1818
	$interface = get_root_interface($interface);
1819

    
1820
	$gw = NULL;
1821
	$gwcfg = config_get_path("interfaces/{$interface}");
1822
	if (!empty($gwcfg['gateway'])) {
1823
		foreach (config_get_path('gateways/gateway_item', []) as $gateway) {
1824
			if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
1825
				$gw = $gateway['gateway'];
1826
				break;
1827
			}
1828
		}
1829
	}
1830

    
1831
	// for dynamic interfaces we handle them through the $interface_router file.
1832
	if (($gw == NULL || !is_ipaddrv4($gw)) && !is_ipaddrv4($gwcfg['ipaddr'])) {
1833
		$realif = get_real_interface($interface);
1834
		if (file_exists(g_get('tmp_path', '') . "/{$realif}_router")) {
1835
			$gw = trim(@file_get_contents(g_get('tmp_path', '') . "/{$realif}_router"), " \n");
1836
			$dynamic = true;
1837
		}
1838
		if (file_exists(g_get('tmp_path', '') . "/{$realif}_defaultgw")) {
1839
			$dynamic = "default";
1840
		}
1841

    
1842
	}
1843

    
1844
	/* return gateway */
1845
	return ($gw);
1846
}
1847

    
1848
function get_interface_gateway_last($interface, $family = 'inet') {
1849
	$interface = get_root_interface($interface);
1850
	$realif = get_real_interface($interface);
1851
	$suffix = ($family == 'inet6') ? 'v6' : '';
1852
	if (file_exists(g_get('tmp_path', '') . "/{$realif}_router{$suffix}.last")) {
1853
		return trim(@file_get_contents(g_get('tmp_path', '') . "/{$realif}_router{$suffix}.last"), " \n");
1854
	}
1855
	return '';
1856
}
1857

    
1858
function get_interface_gateway_v6($interface, &$dynamic = false) {
1859
	$interface = get_root_interface($interface);
1860

    
1861
	$gw = NULL;
1862
	$gwcfg = config_get_path("interfaces/{$interface}");
1863
	if (!empty($gwcfg['gatewayv6'])) {
1864
		foreach (config_get_path('gateways/gateway_item', []) as $gateway) {
1865
			if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
1866
				$gw = $gateway['gateway'];
1867
				break;
1868
			}
1869
		}
1870
	}
1871

    
1872
	// for dynamic interfaces we handle them through the $interface_router file.
1873
	if (($gw == NULL || !is_ipaddrv6($gw)) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
1874
		$realif = get_real_interface($interface);
1875
		if (file_exists(g_get('tmp_path', '') . "/{$realif}_routerv6")) {
1876
			$gw = trim(file_get_contents(g_get('tmp_path', '') . "/{$realif}_routerv6"), " \n");
1877
			$dynamic = true;
1878
		}
1879
		if (file_exists(g_get('tmp_path', '') . "/{$realif}_defaultgwv6")) {
1880
			$dynamic = "default";
1881
		}
1882
	}
1883
	/* return gateway */
1884
	return ($gw);
1885
}
1886

    
1887
/* Check a IP address against a gateway IP or name
1888
 * to verify it's address family */
1889
function validate_address_family($ipaddr, $gwname, $disabled = false) {
1890
	$v4ip = false;
1891
	$v6ip = false;
1892
	$v4gw = false;
1893
	$v6gw = false;
1894

    
1895
	if (is_ipaddrv4($ipaddr)) {
1896
		$v4ip = true;
1897
	}
1898
	if (is_ipaddrv6($ipaddr)) {
1899
		$v6ip = true;
1900
	}
1901
	if (is_ipaddrv4($gwname)) {
1902
		$v4gw = true;
1903
	}
1904
	if (is_ipaddrv6($gwname)) {
1905
		$v6gw = true;
1906
	}
1907

    
1908
	if ($v4ip && $v4gw) {
1909
		return true;
1910
	}
1911
	if ($v6ip && $v6gw) {
1912
		return true;
1913
	}
1914

    
1915
	/* still no match, carry on, lookup gateways */
1916
	if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname, $disabled))) {
1917
		$v4gw = true;
1918
	}
1919
	if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname, $disabled))) {
1920
		$v6gw = true;
1921
	}
1922

    
1923
	/* Check gateways including disabled gateways and gateways for disabled
1924
	 * interfaces.
1925
	 * https://redmine.pfsense.org/issues/8846
1926
	 */
1927
	$gw_array = get_gateways(GW_CACHE_DISABLED | GW_CACHE_INACTIVE);
1928
	if (is_array($gw_array[$gwname])) {
1929
		switch ($gw_array[$gwname]['ipprotocol']) {
1930
			case "inet":
1931
				$v4gw = true;
1932
				break;
1933
			case "inet6":
1934
				$v6gw = true;
1935
				break;
1936
		}
1937
	}
1938

    
1939
	if ($v4ip && $v4gw) {
1940
		return true;
1941
	}
1942
	if ($v6ip && $v6gw) {
1943
		return true;
1944
	}
1945

    
1946
	return false;
1947
}
1948

    
1949
/* check if a interface is part of a gateway group */
1950
function interface_gateway_group_member($interface, $gwgroup_name = "") {
1951
	if (empty(config_get_path('gateways/gateway_group'))) {
1952
		return false;
1953
	}
1954

    
1955
	$gateways_arr = get_gateways(GW_CACHE_LOCALHOST);
1956
	foreach (config_get_path('gateways/gateway_group', []) as $group) {
1957
		if (is_array($group['item'])) {
1958
			foreach ($group['item'] as $item) {
1959
				$elements = explode("|", $item);
1960
				$gwname = $elements[0];
1961
				if ($interface == $gateways_arr[$gwname]['interface'] &&
1962
				    (empty($gwgroup_name) || $gwgroup_name == $group['name'])) {
1963
					unset($gateways_arr);
1964
					return true;
1965
				}
1966
			}
1967
		}
1968
	}
1969
	unset($gateways_arr);
1970

    
1971
	return false;
1972
}
1973

    
1974
function gateway_is_gwgroup_member($name, $detail=false) {
1975
	$members = array();
1976
	foreach (config_get_path('gateways/gateway_group', []) as $group) {
1977
		if (is_array($group['item'])) {
1978
			foreach ($group['item'] as $item) {
1979
				list($gwname, $tier, $vipname) = explode("|", $item);
1980
				if ($name == $gwname) {
1981
					if ($detail) {
1982
						$newitem = array();
1983
						$newitem['name'] = $group['name'];
1984
						$newitem['tier'] = $tier;
1985
						$newitem['vipname'] = $vipname;
1986
						$members[] = $newitem;
1987
					} else {
1988
						$members[] = $group['name'];
1989
					}
1990
				}
1991
			}
1992
		}
1993
	}
1994

    
1995
	if (empty($members)) {
1996
		return false;
1997
	}
1998
	
1999
	return $members;
2000
}
2001
/*
2002
  Check the proposed gateway settings to see if they are valid.
2003
  $gateway_settings - the proposed array of proposed gateway settings
2004
  $id - the index of the gateway proposed to be modified (otherwise "" if adding a new gateway)
2005
  $parent_ip - the IP (v4 or v6) address about to be set on the corresponding interface (if any)
2006
  $parent_sn - the subnet about to be set on the corresponding interface (if any)
2007
  (Note: the above 2 parameters allow gateway parameters to be validated concurrently with saving
2008
   an interface, before the new interface parameters are actually saved in the config.)
2009
  Return completed $input_errors array if there is any problem.
2010
  Otherwise return an empty $input_errors array
2011
*/
2012
function validate_gateway($gateway_settings, $id = "", $parent_ip = "", $parent_sn = "", $json = false) {
2013
	if ($json) {
2014
		$gateway_settings = json_decode($gateway_settings, true);
2015
	}
2016
	global $gateway_state_kill_modes;
2017

    
2018
	refresh_gateways(); // make sure we're validating against a current gateway list
2019
	$a_gateways = get_gateways(GW_CACHE_INDEXED);
2020
	$input_errors = array();
2021

    
2022
	/* input validation */
2023
	$reqdfields = explode(" ", "name interface");
2024
	$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
2025

    
2026
	do_input_validation($gateway_settings, $reqdfields, $reqdfieldsn, $input_errors);
2027

    
2028
	if (!isset($gateway_settings['name'])) {
2029
		$input_errors[] = "A valid gateway name must be specified.";
2030
	}
2031
	if (!is_validaliasname($gateway_settings['name'])) {
2032
		$input_errors[] = invalidaliasnamemsg($gateway_settings['name'], gettext("gateway"));
2033
	} else if (isset($gateway_settings['disabled'])) {
2034
		// We have a valid gateway name that the user wants to mark as disabled.
2035
		// Check if the gateway name is used in any gateway group.
2036
		foreach (config_get_path('gateways/gateway_group', []) as $group) {
2037
			foreach ($group['item'] as $item) {
2038
				$items = explode("|", $item);
2039
				if ($items[0] == $gateway_settings['name']) {
2040
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use on Gateway Group "%2$s"'), $gateway_settings['name'], $group['name']);
2041
				}
2042
			}
2043
		}
2044

    
2045
		// Check if the gateway name is used in any enabled Static Route.
2046
		foreach (config_get_path('staticroutes/route', []) as $route) {
2047
			if ($route['gateway'] == $gateway_settings['name']) {
2048
				if (!isset($route['disabled'])) {
2049
					// There is a static route that uses this gateway and is enabled (not disabled).
2050
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use on Static Route "%2$s"'), $gateway_settings['name'], $route['network']);
2051
				}
2052
			}
2053
		}
2054

    
2055
		// Check if the gateway name is used by any DNS Server
2056
		for ($dnsgw_counter = 1; !is_null($dnsgw = config_get_path("system/dns{$dnsgw_counter}gw")); $dnsgw_counter++) {
2057
			if ($gateway_settings['name'] == $dnsgw) {
2058
				// The user wants to disable this gateway, but there is a static route to the DNS server that refers to the gateway.
2059
				$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use by DNS Server "%2$s"'), $gateway_settings['name'], $dnsgw);
2060
			}
2061
		}
2062
	}
2063
	/* skip system gateways which have been automatically added */
2064
	if (($gateway_settings['gateway'] && (!is_ipaddr($gateway_settings['gateway'])) && ($gateway_settings['attribute'] !== "system")) && ($gateway_settings['gateway'] != "dynamic")) {
2065
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
2066
	}
2067

    
2068
	if ($gateway_settings['gateway'] && is_ipaddr($gateway_settings['gateway'])) {
2069
		if (is_ipaddrv4($gateway_settings['gateway'])) {
2070
			if ($parent_ip == '') {
2071
				$parent_ip = get_interface_ip($gateway_settings['interface']);
2072
				$parent_sn = get_interface_subnet($gateway_settings['interface']);
2073
			}
2074
			if (empty($parent_ip) || empty($parent_sn)) {
2075
				$input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
2076
			} elseif (!isset($gateway_settings["nonlocalgateway"])) {
2077
				$subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
2078
				$vips = link_interface_to_vips($gateway_settings['interface']);
2079
				if (is_array($vips)) {
2080
					foreach ($vips as $vip) {
2081
						if (!is_ipaddrv4($vip['subnet'])) {
2082
							continue;
2083
						}
2084
						$subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
2085
					}
2086
				}
2087

    
2088
				$found = false;
2089
				foreach ($subnets as $subnet) {
2090
					if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
2091
						$found = true;
2092
						break;
2093
					}
2094
				}
2095

    
2096
				if ($found === false) {
2097
					$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
2098
				}
2099
			}
2100
		} else if (is_ipaddrv6($gateway_settings['gateway'])) {
2101
			/* do not do a subnet match on a link local address, it's valid */
2102
			if (!is_linklocal($gateway_settings['gateway'])) {
2103
				if ($parent_ip == '') {
2104
					$parent_ip = get_interface_ipv6($gateway_settings['interface']);
2105
					$parent_sn = get_interface_subnetv6($gateway_settings['interface']);
2106
				}
2107
				if (empty($parent_ip) || empty($parent_sn)) {
2108
					$input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
2109
				} elseif (!isset($gateway_settings["nonlocalgateway"])) {
2110
					$subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
2111
					$vips = link_interface_to_vips($gateway_settings['interface']);
2112
					if (is_array($vips)) {
2113
						foreach ($vips as $vip) {
2114
							if (!is_ipaddrv6($vip['subnet'])) {
2115
								continue;
2116
							}
2117
							$subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
2118
						}
2119
					}
2120

    
2121
					$found = false;
2122
					foreach ($subnets as $subnet) {
2123
						if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
2124
							$found = true;
2125
							break;
2126
						}
2127
					}
2128

    
2129
					if ($found === false) {
2130
						$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
2131
					}
2132
				}
2133
			}
2134
		}
2135

    
2136
		if (!empty(config_get_path("interfaces/{$gateway_settings['interface']}/ipaddr"))) {
2137
			if (is_ipaddr(config_get_path("interfaces/{$gateway_settings['interface']}/ipaddr")) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
2138
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
2139
			}
2140
		}
2141
		if (!empty(config_get_path("interfaces/{$gateway_settings['interface']}/ipaddrv6"))) {
2142
			if (is_ipaddr(config_get_path("interfaces/{$gateway_settings['interface']}/ipaddrv6")) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
2143
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
2144
			}
2145
		}
2146
	}
2147
	if (($gateway_settings['monitor'] != "") && ($gateway_settings['monitor'] != "dynamic")) {
2148
		validateipaddr($gateway_settings['monitor'], IPV4V6, "Monitor IP", $input_errors, false);
2149
	}
2150
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] < 0) {
2151
		$input_errors[] = gettext("A valid data payload must be specified.");
2152
	}
2153
	if (!empty($gateway_settings['skip_rules_gw_down']) && !array_key_exists($gateway_settings['skip_rules_gw_down'], $gateway_state_kill_modes)) {
2154
		$input_errors[] = gettext("Please select a valid State Killing on Gateway Failure mode.");
2155
	}
2156
	/* only allow correct IPv4 and IPv6 gateway addresses */
2157
	if (($gateway_settings['gateway'] <> "") && is_ipaddr($gateway_settings['gateway']) && $gateway_settings['gateway'] != "dynamic") {
2158
		if (is_ipaddrv6($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet")) {
2159
			$input_errors[] = sprintf(gettext("The IPv6 gateway address '%s' can not be used as a IPv4 gateway."), $gateway_settings['gateway']);
2160
		}
2161
		if (is_ipaddrv4($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet6")) {
2162
			$input_errors[] = sprintf(gettext("The IPv4 gateway address '%s' can not be used as a IPv6 gateway."), $gateway_settings['gateway']);
2163
		}
2164
	}
2165
	/* only allow correct IPv4 and IPv6 monitor addresses */
2166
	if (($gateway_settings['monitor'] <> "") && is_ipaddr($gateway_settings['monitor']) && $gateway_settings['monitor'] != "dynamic") {
2167
		if (is_ipaddrv6($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet")) {
2168
			$input_errors[] = sprintf(gettext("The IPv6 monitor address '%s' can not be used on a IPv4 gateway."), $gateway_settings['monitor']);
2169
		}
2170
		if (is_ipaddrv4($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet6")) {
2171
			$input_errors[] = sprintf(gettext("The IPv4 monitor address '%s' can not be used on a IPv6 gateway."), $gateway_settings['monitor']);
2172
		}
2173
	}
2174

    
2175
	if (isset($gateway_settings['name'])) {
2176
		/* check for overlaps */
2177
		foreach ($a_gateways as $gateway) {
2178
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
2179
				if ($gateway['name'] != $gateway_settings['name']) {
2180
					$input_errors[] = gettext("Changing name on a gateway is not allowed.");
2181
				}
2182
				continue;
2183
			}
2184
			if ($gateway_settings['name'] <> "") {
2185
				if (($gateway['name'] <> "") && ($gateway_settings['name'] == $gateway['name']) && ($gateway['attribute'] !== "system")) {
2186
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $gateway_settings['name']);
2187
					break;
2188
				}
2189
			}
2190
			if (is_ipaddr($gateway_settings['gateway'])) {
2191
				if (($gateway['gateway'] <> "") && ($gateway_settings['gateway'] == $gateway['gateway']) && ($gateway['attribute'] !== "system")) {
2192
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $gateway_settings['gateway']);
2193
					break;
2194
				}
2195
			}
2196
			if (is_ipaddr($gateway_settings['monitor'])) {
2197
				if (($gateway['monitor'] <> "") && ($gateway_settings['monitor'] == $gateway['monitor']) && ($gateway['attribute'] !== "system")) {
2198
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. A different monitor IP must be chosen.'), $gateway_settings['monitor']);
2199
					break;
2200
				}
2201
			}
2202
		}
2203
	}
2204

    
2205
	/* input validation of dpinger advanced parameters */
2206

    
2207
	$dpinger_default = return_dpinger_defaults();
2208
	$latencylow = $dpinger_default['latencylow'];
2209
	if ($gateway_settings['latencylow']) {
2210
		if (!is_numeric($gateway_settings['latencylow'])) {
2211
			$input_errors[] = gettext("The low latency threshold needs to be a numeric value.");
2212
		} else if ($gateway_settings['latencylow'] < 1) {
2213
			$input_errors[] = gettext("The low latency threshold needs to be positive.");
2214
		} else {
2215
			$latencylow = $gateway_settings['latencylow'];
2216
		}
2217
	}
2218

    
2219
	$latencyhigh = $dpinger_default['latencyhigh'];
2220
	if ($gateway_settings['latencyhigh']) {
2221
		if (!is_numeric($gateway_settings['latencyhigh'])) {
2222
			$input_errors[] = gettext("The high latency threshold needs to be a numeric value.");
2223
		} else if ($gateway_settings['latencyhigh'] < 1) {
2224
			$input_errors[] = gettext("The high latency threshold needs to be positive.");
2225
		} else {
2226
			$latencyhigh = $gateway_settings['latencyhigh'];
2227
		}
2228
	}
2229

    
2230
	$losslow = $dpinger_default['losslow'];
2231
	if ($gateway_settings['losslow']) {
2232
		if (!is_numeric($gateway_settings['losslow'])) {
2233
			$input_errors[] = gettext("The low Packet Loss threshold needs to be a numeric value.");
2234
		} else if ($gateway_settings['losslow'] < 1) {
2235
			$input_errors[] = gettext("The low Packet Loss threshold needs to be positive.");
2236
		} else if ($gateway_settings['losslow'] >= 100) {
2237
			$input_errors[] = gettext("The low Packet Loss threshold needs to be less than 100.");
2238
		} else {
2239
			$losslow = $gateway_settings['losslow'];
2240
		}
2241
	}
2242

    
2243
	$losshigh = $dpinger_default['losshigh'];
2244
	if ($gateway_settings['losshigh']) {
2245
		if (!is_numeric($gateway_settings['losshigh'])) {
2246
			$input_errors[] = gettext("The high Packet Loss threshold needs to be a numeric value.");
2247
		} else if ($gateway_settings['losshigh'] < 1) {
2248
			$input_errors[] = gettext("The high Packet Loss threshold needs to be positive.");
2249
		} else if ($gateway_settings['losshigh'] > 100) {
2250
			$input_errors[] = gettext("The high Packet Loss threshold needs to be 100 or less.");
2251
		} else {
2252
			$losshigh = $gateway_settings['losshigh'];
2253
		}
2254
	}
2255

    
2256
	$time_period = $dpinger_default['time_period'];
2257
	if ($gateway_settings['time_period']) {
2258
		if (!is_numeric($gateway_settings['time_period'])) {
2259
			$input_errors[] = gettext("The time period over which results are averaged needs to be a numeric value.");
2260
		} else if ($gateway_settings['time_period'] < 1) {
2261
			$input_errors[] = gettext("The time period over which results are averaged needs to be positive.");
2262
		} else {
2263
			$time_period = $gateway_settings['time_period'];
2264
		}
2265
	}
2266

    
2267
	$interval = $dpinger_default['interval'];
2268
	if ($gateway_settings['interval']) {
2269
		if (!is_numeric($gateway_settings['interval'])) {
2270
			$input_errors[] = gettext("The probe interval needs to be a numeric value.");
2271
		} else if ($gateway_settings['interval'] < 1) {
2272
			$input_errors[] = gettext("The probe interval needs to be positive.");
2273
		} else {
2274
			$interval = $gateway_settings['interval'];
2275
		}
2276
	}
2277

    
2278
	$loss_interval = $dpinger_default['loss_interval'];
2279
	if ($gateway_settings['loss_interval']) {
2280
		if (!is_numeric($gateway_settings['loss_interval'])) {
2281
			$input_errors[] = gettext("The loss interval needs to be a numeric value.");
2282
		} else if ($gateway_settings['loss_interval'] < 1) {
2283
			$input_errors[] = gettext("The loss interval setting needs to be positive.");
2284
		} else {
2285
			$loss_interval = $gateway_settings['loss_interval'];
2286
		}
2287
	}
2288

    
2289
	$alert_interval = $dpinger_default['alert_interval'];
2290
	if ($gateway_settings['alert_interval']) {
2291
		if (!is_numeric($gateway_settings['alert_interval'])) {
2292
			$input_errors[] = gettext("The alert interval needs to be a numeric value.");
2293
		} else if ($gateway_settings['alert_interval'] < 1) {
2294
			$input_errors[] = gettext("The alert interval setting needs to be positive.");
2295
		} else {
2296
			$alert_interval = $gateway_settings['alert_interval'];
2297
		}
2298
	}
2299

    
2300
	if ($latencylow >= $latencyhigh) {
2301
		$input_errors[] = gettext(
2302
		    "The high latency threshold needs to be greater than the low latency threshold");
2303
	}
2304

    
2305
	if ($losslow >= $losshigh) {
2306
		$input_errors[] = gettext(
2307
		    "The high packet loss threshold needs to be higher than the low packet loss threshold");
2308
	}
2309

    
2310
	// If the loss interval is less than latencyhigh, then high latency could never be recorded
2311
	// because those high latency packets would be considered as lost. So do not allow that.
2312
	if ($latencyhigh > $loss_interval) {
2313
		$input_errors[] = gettext("The loss interval needs to be greater than or equal to the high latency threshold.");
2314
	}
2315

    
2316
	// Ensure that the time period is greater than 2 times the probe interval plus the loss interval.
2317
	if (($interval * 2 + $loss_interval) >= $time_period) {
2318
		$input_errors[] = gettext("The time period needs to be greater than twice the probe interval plus the loss interval.");
2319
	}
2320

    
2321
	// There is no point recalculating the average latency and loss more often than the probe interval.
2322
	// So the alert interval needs to be >= probe interval.
2323
	if ($interval > $alert_interval) {
2324
		$input_errors[] = gettext("The alert interval needs to be greater than or equal to the probe interval.");
2325
	}
2326

    
2327
	if ($json) {
2328
		return json_encode($input_errors);
2329
	}
2330
	return $input_errors;
2331
}
2332

    
2333
// Save gateway settings.
2334
// $gateway_settings - the array of gateway setting parameters
2335
// $realid - the index of the gateway to be modified (otherwise "" if adding a new gateway)
2336

    
2337
// This function is responsible to:
2338
//   Setup the gateway parameter structure from the gateway settings input parameter
2339
//   Save the structure into the config
2340
//   Remove any run-time settings from gateway parameters that are changed (e.g. remove routes to addresses that are changing)
2341

    
2342
// A subsequent "apply" step will implement the added/changed gateway.
2343

    
2344
function save_gateway($gateway_settings, $realid = "", $json = false) {
2345
	if ($json) {
2346
		$gateway_settings = json_decode($gateway_settings, true);
2347
	}
2348

    
2349
	$reloadif = "";
2350
	$gateway = array();
2351

    
2352
	if (empty($gateway_settings['interface'])) {
2353
		$gateway['interface'] = $gateway_settings['friendlyiface'];
2354
	} else {
2355
		$gateway['interface'] = $gateway_settings['interface'];
2356
	}
2357
	if (is_ipaddr($gateway_settings['gateway'])) {
2358
		$gateway['gateway'] = $gateway_settings['gateway'];
2359
	} else {
2360
		$gateway['gateway'] = "dynamic";
2361
	}
2362
	$gateway['name'] = $gateway_settings['name'];
2363
	$gateway['weight'] = $gateway_settings['weight'];
2364
	$gateway['ipprotocol'] = $gateway_settings['ipprotocol'];
2365
	if ($gateway_settings['interval']) {
2366
		$gateway['interval'] = $gateway_settings['interval'];
2367
	}
2368

    
2369
	if ($gateway_settings['time_period']) {
2370
		$gateway['time_period'] = $gateway_settings['time_period'];
2371
	}
2372
	if ($gateway_settings['alert_interval']) {
2373
		$gateway['alert_interval'] = $gateway_settings['alert_interval'];
2374
	}
2375

    
2376
	$gateway['descr'] = $gateway_settings['descr'];
2377
	if ($gateway_settings['monitor_disable'] == "yes") {
2378
		$gateway['monitor_disable'] = true;
2379
	}
2380
	if ($gateway_settings['action_disable'] == "yes") {
2381
		$gateway['action_disable'] = true;
2382
	}
2383
	if ($gateway_settings['nonlocalgateway'] == "yes") {
2384
		$gateway['nonlocalgateway'] = true;
2385
	}
2386
	if ($gateway_settings['dpinger_dont_add_static_route'] == "yes") {
2387
		$gateway['dpinger_dont_add_static_route'] = true;
2388
	}
2389
	if ($gateway_settings['force_down'] == "yes") {
2390
		$gateway['force_down'] = true;
2391
	}
2392
	$gateway['gw_down_kill_states'] = $gateway_settings['gw_down_kill_states'];
2393
	if (is_ipaddr($gateway_settings['monitor'])) {
2394
		$gateway['monitor'] = $gateway_settings['monitor'];
2395
	}
2396
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] >= 0) {
2397
		$gateway['data_payload'] = $gateway_settings['data_payload'];
2398
	}
2399

    
2400
	$a_gateway_item = config_get_path("gateways/gateway_item/{$realid}");
2401
	/* NOTE: If gateway ip is changed need to cleanup the old static interface route */
2402
	if ($gateway_settings['monitor'] != "dynamic" &&
2403
	    !empty($a_gateway_item) &&
2404
	    is_ipaddr($a_gateway_item['gateway']) &&
2405
	    $gateway['gateway'] != $a_gateway_item['gateway'] &&
2406
	    isset($a_gateway_item["nonlocalgateway"])) {
2407
		route_del($a_gateway_item['gateway']);
2408
	}
2409

    
2410
	/* NOTE: If monitor ip is changed need to cleanup the old static route */
2411
	if ($gateway_settings['monitor'] != "dynamic" &&
2412
	    !empty($a_gateway_item) &&
2413
	    is_ipaddr($a_gateway_item['monitor']) &&
2414
	    $gateway_settings['monitor'] != $a_gateway_item['monitor'] &&
2415
	    $gateway['gateway'] != $a_gateway_item['monitor']) {
2416
		route_del($a_gateway_item['monitor']);
2417
	}
2418

    
2419
	if ($gateway_settings['defaultgw'] == "yes" || $gateway_settings['defaultgw'] == "on") {
2420
		// a new default gateway is being saved.
2421
		$i = 0;
2422
		/* remove the default gateway bits for all gateways with the same address family */
2423
		foreach (config_get_path('gateways/gateway_item', []) as $gw) {
2424
			if ($gateway['ipprotocol'] == $gw['ipprotocol']) {
2425
				if ($gw['interface'] != $gateway_settings['interface'] &&
2426
					($gw['name'] == config_get_path('gateways/defaultgw4', "") ||
2427
					 $gw['name'] == config_get_path('gateways/defaultgw6', ""))) {
2428
					// remember the old default gateway interface to call with a "interface reconfigure" event.
2429
					$reloadif = $gw['interface'];
2430
				}
2431
			}
2432
			$i++;
2433
		}
2434
		if ($gateway['ipprotocol'] == "inet") {
2435
			config_set_path('gateways/defaultgw4', $gateway['name']);
2436
		} elseif ($gateway['ipprotocol'] == "inet6") {
2437
			config_set_path('gateways/defaultgw6', $gateway['name']);
2438
		}
2439
	}
2440

    
2441
	if ($gateway_settings['latencylow']) {
2442
		$gateway['latencylow'] = $gateway_settings['latencylow'];
2443
	}
2444
	if ($gateway_settings['latencyhigh']) {
2445
		$gateway['latencyhigh'] = $gateway_settings['latencyhigh'];
2446
	}
2447
	if ($gateway_settings['losslow']) {
2448
		$gateway['losslow'] = $gateway_settings['losslow'];
2449
	}
2450
	if ($gateway_settings['losshigh']) {
2451
		$gateway['losshigh'] = $gateway_settings['losshigh'];
2452
	}
2453
	if ($gateway_settings['loss_interval']) {
2454
		$gateway['loss_interval'] = $gateway_settings['loss_interval'];
2455
	}
2456

    
2457
	$a_gateway_item = config_get_path("gateways/gateway_item/{$realid}");
2458
	/* reload IPsec and OpenVPN on gateway IP, 'Mark Gateway as Down', or 'Disabled' option change
2459
	 * see https://redmine.pfsense.org/issues/13076 */
2460
	if (!empty($a_gateway_item) &&
2461
	    (isset($gateway['disabled']) ^ isset($a_gateway_item['disabled'])) ||
2462
	    (!isset($a_gateway_item['disabled']) &&
2463
	    ((($gateway['monitor'] != "dynamic") &&
2464
	    is_ipaddr($a_gateway_item['gateway']) &&
2465
	    ($gateway['gateway'] != $a_gateway_item['gateway'])) ||
2466
	    (isset($gateway['force_down']) ^ isset($a_gateway_item['force_down']))))) {
2467
		$reloadvpn = true;
2468
	}
2469

    
2470
	/* when saving the manual gateway we use the attribute which has the corresponding id */
2471
	if (isset($realid) && $a_gateway_item) {
2472
		$preserve_disabled = isset($a_gateway_item['disabled']);
2473
		$a_gateway_item = $gateway;
2474
		if ($preserve_disabled) {
2475
			$a_gateway_item['disabled'] = true;
2476
		}
2477
		config_set_path("gateways/gateway_item/{$realid}", $a_gateway_item);
2478
	} else {
2479
		config_set_path("gateways/gateway_item/", $gateway);
2480
	}
2481
	gateway_set_enabled($gateway_settings['name'], !isset($gateway_settings['disabled']));
2482

    
2483
	mark_subsystem_dirty('staticroutes');
2484

    
2485
	write_config("Gateway settings changed");
2486

    
2487
	if ($reloadvpn) {
2488
		send_event("service reload ipsec " . escapeshellarg($gateway['name']));
2489
		send_event("service reload openvpn " . escapeshellarg($gateway['name']));
2490
	}
2491

    
2492
	if (!empty($reloadif)) {
2493
		send_event("interface reconfigure {$reloadif}");
2494
	}
2495
}
2496

    
2497
function gateway_set_enabled($name, $enabled) {
2498
	$gwlist = config_get_path('gateways/gateway_item', []);
2499
	if (is_array($gwlist)) {
2500
		foreach($gwlist as $key => $item) {
2501
			if ($item['name'] == $name) {
2502
				$gateway = &$gwlist[$key];
2503
			}
2504
		}
2505
	}
2506
	if (!isset($gateway)) {
2507
		return;
2508
	}
2509
	if ($enabled) {
2510
		unset($gateway['disabled']);
2511
	} else {
2512
		/* Check if the gateway was enabled but changed to disabled. */
2513
		if (!isset($gateway['disabled'])) {
2514
			/*  If the disabled gateway was the default route, remove the default route */
2515
			if (is_ipaddr($gateway['gateway'])) {
2516
				$inet = (!is_ipaddrv4($gateway['gateway']) ? 'inet6' : 'inet');
2517
				if ($inet == 'inet') {
2518
					$cgw = route_get_default('inet');
2519
				} else {
2520
					$cgw = route_get_default('inet6');
2521
				}
2522
				if ($gateway['gateway'] == $cgw) {
2523
					route_del("default", $inet);
2524
				}
2525
			}
2526
			$gateway['disabled'] = true;
2527
		}
2528
	}
2529
	unset($gateway);
2530
	config_set_path('gateways/gateway_item', $gwlist);
2531
}
2532

    
2533
function gateway_or_gwgroup_exists($name) {
2534
	foreach(config_get_path('gateways/gateway_item', []) as $item) {
2535
		if ($item['name'] == $name) {
2536
			return true;
2537
		}
2538
	}
2539
	foreach(config_get_path('gateways/gateway_group', []) as $item) {
2540
		if ($item['name'] == $name) {
2541
			return true;
2542
		}
2543
	}
2544
	return false;
2545
}
2546

    
2547
// These two replacement functions avoid the need to call return_gateways_array() multiple times and
2548
// allow system_gateways.php to get everything it needs in a single function call
2549
function gateway_getgwtiername($gways, $idx) {
2550
	$result = "";
2551
	$gwname = $gways[$idx]['name'];
2552

    
2553
	$gw = get_gateway_or_group_by_name($gwname, $gways);
2554

    
2555
	$gw4 = config_get_path('gateways/defaultgw4', "");
2556
	$gw6 = config_get_path('gateways/defaultgw6', "");
2557
	if ($gw4 == $gwname || $gw6 == $gwname) {
2558
		$result = "Default";
2559
	} else {
2560
		if ($gw['ipprotocol'] == 'inet') {
2561
			$defgw = get_gateway_or_group_by_name($gw4, $gways);
2562
		} else {
2563
			$defgw = get_gateway_or_group_by_name($gw6, $gways);
2564
		}
2565

    
2566
		if ($defgw['type'] == "gatewaygroup") {
2567
			$detail = gateway_is_gwgroup_member($gwname, true);
2568
			foreach($detail as $gwitem) {
2569
				if ($gwitem['name'] == $defgw['name']) {
2570
					if (isset($gwitem['tier'])) {
2571
						$result = "Tier " . $gwitem['tier'];
2572
						break;
2573
					}
2574
				}
2575
			}
2576
		}
2577
    }
2578

    
2579
	if (!empty($result)) {
2580
		if ($gw['ipprotocol'] == "inet") {
2581
			$result .= " (IPv4)";
2582
		} elseif ($gw['ipprotocol'] == "inet6") {
2583
			$result .= " (IPv6)";
2584
		}
2585
	}
2586

    
2587
	return $result;
2588
}
2589

    
2590
function get_gateway_or_group_by_name($gwname, $gateways_arr) {
2591
	foreach ($gateways_arr as $gw) {
2592
		if ($gw['name'] == $gwname) {
2593
			$gw['type'] = 'gateway';
2594
			return $gw;
2595
		}
2596
	}
2597

    
2598
	foreach (config_get_path('gateways/gateway_group', []) as $gwg) {
2599
		if ($gwg['name'] == $gwname) {
2600
			$gwg['type'] = 'gatewaygroup';
2601
			return $gwg;
2602
		}
2603
	}
2604

    
2605
	return false;
2606
}
2607

    
2608
// Compose a list of available gateways but without the need to call return_gateways_array() multiple times
2609
// Previously that function could be called eight times per gateway!
2610
function available_default_gateways() {
2611
	$gways = get_gateways(GW_CACHE_DISABLED | GW_CACHE_INACTIVE);
2612

    
2613
	$items4 = array();
2614
	$items6 = array();
2615
	$items4[''] = "Automatic";
2616
	$items6[''] = "Automatic";
2617
	foreach($gways as $gw) {
2618
		$gwn = $gw['name'];
2619
		if ($gw['ipprotocol'] == "inet6") {
2620
			$items6[$gwn] = $gwn;
2621
		} else {
2622
			$items4[$gwn] = $gwn;
2623
		}
2624
	}
2625

    
2626
	$groups = return_gateway_groups_array(false);
2627
	foreach ($groups as $key => $group) {
2628
		$gwn = $group['descr'];
2629
		if ($group['ipprotocol'] == "inet6") {
2630
			$items6[$key] = "$key ($gwn)";
2631
		} else {
2632
			$items4[$key] = "$key ($gwn)";
2633
		}
2634
	}
2635
	$items4['-'] = "None";
2636
	$items6['-'] = "None";
2637

    
2638
	$defaults = array();
2639
	$defaults['v4'] = $items4;
2640
	$defaults['v6'] = $items6;
2641
	$defaults['defaultgw4'] = config_get_path('gateways/defaultgw4', "");
2642
	$defaults['defaultgw6'] = config_get_path('gateways/defaultgw6', "");
2643

    
2644
	return $defaults;
2645
}
2646

    
2647
function get_interface_gateway_name($friendlyiface, $family = 'inet') {
2648
	/* Try to locate gateway directly (e.g. static) */
2649
	if ($family == 'inet') {
2650
		$gw = config_get_path("interfaces/{$friendlyiface}/gateway", '');
2651
	} else {
2652
		$gw = config_get_path("interfaces/{$friendlyiface}/gatewayv6", '');
2653
	}
2654

    
2655
	/* If the gateway is empty that means it's dynamic so we have to hunt it down */
2656
	if (empty($gw)) {
2657
		/* Check each gateway entry for a dynamic gateway matching
2658
		 * this interface and the requested address family.
2659
		 */
2660
		foreach (get_gateways() as $gwname => $gwcfg) {
2661
			if (empty($gwcfg) ||
2662
			    !is_array($gwcfg) ||
2663
			    ($gwcfg['dynamic'] == false) ||
2664
			    ($gwcfg['friendlyiface'] != $friendlyiface) ||
2665
			    ($gwcfg['ipprotocol'] != $family)) {
2666
				continue;
2667
			}
2668
			$gw = $gwname;
2669
			break;
2670
		}
2671
	}
2672
	return $gw;
2673
}
2674

    
2675
?>
(21-21/61)