Project

General

Profile

Download (87 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
 */
1699
function return_gateway_groups_array($fixup = false) {
1700
	/* fetch the current gateways status */
1701
	$gateways_status = return_gateways_status(true);
1702
	$gateways_arr = get_gateways();
1703
	$gateway_groups_array = array();
1704
	if ($fixup == true) {
1705
		$gw4 = lookup_gateway_or_group_by_name(config_get_path('gateways/defaultgw4', ""));
1706
		$gw6 = lookup_gateway_or_group_by_name(config_get_path('gateways/defaultgw6', ""));
1707
		if ($gw4 && $gw4['type'] == 'gatewaygroup') {
1708
			fixup_default_gateway("inet", $gateways_status, $gateways_arr);
1709
		}
1710
		if ($gw6 && $gw6['type'] == 'gatewaygroup') {
1711
			fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
1712
		}
1713
	}
1714
	if (!empty(config_get_path('gateways/gateway_group'))) {
1715
		$viplist = get_configured_vip_list();
1716
		foreach (config_get_path('gateways/gateway_group', []) as $group) {
1717
			$gateway_groups_array[$group['name']] = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1718
		}
1719
	}
1720

    
1721
	return ($gateway_groups_array);
1722
}
1723

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

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

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

    
1747
function lookup_gateway_or_group_by_name($gwname) {
1748
	$gateways_arr = get_gateways();
1749

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

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

    
1764
	return false;
1765
}
1766

    
1767
function lookup_gateway_ip_by_name($name, $disabled = false) {
1768

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

    
1776
	return false;
1777
}
1778

    
1779
function lookup_gateway_monitor_ip_by_name($name) {
1780

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

    
1788
		return $gateway['monitor'];
1789
	}
1790

    
1791
	return (false);
1792
}
1793

    
1794
function lookup_gateway_interface_by_name($name) {
1795

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

    
1802
	return (false);
1803
}
1804

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

    
1815
function get_interface_gateway($interface, &$dynamic = false) {
1816
	$interface = get_root_interface($interface);
1817

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

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

    
1840
	}
1841

    
1842
	/* return gateway */
1843
	return ($gw);
1844
}
1845

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

    
1856
function get_interface_gateway_v6($interface, &$dynamic = false) {
1857
	$interface = get_root_interface($interface);
1858

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

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

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

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

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

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

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

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

    
1944
	return false;
1945
}
1946

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

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

    
1969
	return false;
1970
}
1971

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

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

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

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

    
2024
	do_input_validation($gateway_settings, $reqdfields, $reqdfieldsn, $input_errors);
2025

    
2026
	if (!isset($gateway_settings['name'])) {
2027
		$input_errors[] = "A valid gateway name must be specified.";
2028
	}
2029
	if (!is_validaliasname($gateway_settings['name'])) {
2030
		$input_errors[] = invalidaliasnamemsg($gateway_settings['name'], gettext("gateway"));
2031
	} else if (isset($gateway_settings['disabled'])) {
2032
		// We have a valid gateway name that the user wants to mark as disabled.
2033
		// Check if the gateway name is used in any gateway group.
2034
		foreach (config_get_path('gateways/gateway_group', []) as $group) {
2035
			foreach ($group['item'] as $item) {
2036
				$items = explode("|", $item);
2037
				if ($items[0] == $gateway_settings['name']) {
2038
					$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']);
2039
				}
2040
			}
2041
		}
2042

    
2043
		// Check if the gateway name is used in any enabled Static Route.
2044
		foreach (config_get_path('staticroutes/route', []) as $route) {
2045
			if ($route['gateway'] == $gateway_settings['name']) {
2046
				if (!isset($route['disabled'])) {
2047
					// There is a static route that uses this gateway and is enabled (not disabled).
2048
					$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']);
2049
				}
2050
			}
2051
		}
2052

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

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

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

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

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

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

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

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

    
2203
	/* input validation of dpinger advanced parameters */
2204

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2325
	if ($json) {
2326
		return json_encode($input_errors);
2327
	}
2328
	return $input_errors;
2329
}
2330

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

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

    
2340
// A subsequent "apply" step will implement the added/changed gateway.
2341

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

    
2347
	$reloadif = "";
2348
	$gateway = array();
2349

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

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

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

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

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

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

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

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

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

    
2481
	mark_subsystem_dirty('staticroutes');
2482

    
2483
	write_config("Gateway settings changed");
2484

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

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

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

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

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

    
2551
	$gw = get_gateway_or_group_by_name($gwname, $gways);
2552

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

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

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

    
2585
	return $result;
2586
}
2587

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

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

    
2603
	return false;
2604
}
2605

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

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

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

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

    
2642
	return $defaults;
2643
}
2644

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

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

    
2673
?>
(21-21/61)