Project

General

Profile

Download (87.6 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 gateways that are disabled
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)) {
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 ($remove_disabled && !config_path_enabled("interfaces/{$gw['friendlyiface']}")) {
84
				unset($gateways[$idx]);
85
			}
86
			if ($remove_inactive && isset($gw['inactive'])) {
87
				unset($gateways[$idx]);
88
			}
89
		}
90
	}
91

    
92
	if ($indexed) {
93
		$gateways = array_values($gateways);
94
	}
95

    
96
	if ($remove_disabled || $remove_localhost || $remove_inactive || $indexed) {
97
		$gateways = order_gateways_as_configured($gateways);
98
	}
99

    
100
	return $gateways;
101
}
102

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

    
110
	// Get gateways that exist in the config
111
	$saved_gateways = array_column(config_get_path('gateways/gateway_item', []), 'name', 'name');
112

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

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

    
153
function running_dpinger_processes() {
154
	global $g;
155

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

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

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

    
178
	return $result;
179
}
180

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

    
189
	$running_processes = running_dpinger_processes();
190

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

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

    
204
function start_dpinger($gateway) {
205
	global $g;
206

    
207
	if (!isset($gateway['gwifip'])) {
208
		return (false);
209
	}
210

    
211
	$dpinger_defaults = return_dpinger_defaults();
212

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

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

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

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

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

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

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

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

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

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

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

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

    
294
/*
295
 * Starts dpinger processes and adds appropriate static routes for monitor IPs
296
 */
297
function setup_gateways_monitor() {
298
	global $config, $g;
299

    
300
	refresh_gateways(); // dynamic gateways may have changed so refresh the cache
301
	$gateways_arr = get_gateways();
302
	if (!is_array($gateways_arr)) {
303
		log_error(gettext("No gateways to monitor. dpinger will not run."));
304
		stop_dpinger();
305
		return;
306
	}
307
	if (platform_booting()) {
308
		echo "Setting up gateway monitors...";
309
	}
310
	$monitor_ips = array();
311
	foreach ($gateways_arr as $gwname => $gateway) {
312
		/* Do not monitor if such was requested */
313
		if (isset($gateway['monitor_disable'])) {
314
			continue;
315
		}
316
		if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
317
			if (is_ipaddr($gateway['gateway'])) {
318
				$gateways_arr[$gwname]['monitor'] = $gateway['gateway'];
319
			} else { /* No chance to get an ip to monitor skip target. */
320
				continue;
321
			}
322
		}
323

    
324
		/* if the monitor address is already used before, skip */
325
		if (in_array($gateway['monitor'], $monitor_ips)) {
326
			continue;
327
		}
328

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

    
342
			if ($gwifip == "0.0.0.0") {
343
				continue; //Skip this target - the gateway is still waiting for DHCP
344
			}
345

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

    
365
					pfSense_kill_states("0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmp");
366
				}
367
			}
368
		} else if ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
369
			if (is_linklocal($gateway['gateway']) &&
370
			    get_ll_scope($gateway['gateway']) == '') {
371
				$gateway['gateway'] .= '%' . $gateway['interface'];
372
			}
373

    
374
			if (is_linklocal($gateway['monitor'])) {
375
				if (get_ll_scope($gateway['monitor']) == '') {
376
					$gateways_arr[$gwname]['monitor'] .= '%' . $gateway['interface'];
377
				}
378

    
379
				$gwifip = find_interface_ipv6_ll($gateway['interface'], true);
380

    
381
				if (get_ll_scope($gwifip) == '') {
382
					$gwifip .= '%' . $gateway['interface'];
383
				}
384
			} else {
385
				$gwifip = find_interface_ipv6($gateway['interface'], true);
386
			}
387

    
388
			if (!is_ipaddrv6($gwifip)) {
389
				continue; //Skip this target
390
			}
391

    
392
			/*
393
			 * If the gateway is the same as the monitor we do not add a
394
			 * route as this will break the routing table.
395
			 * Add static routes for each gateway with their monitor IP
396
			 * not strictly necessary but is a added level of protection.
397
			 */
398

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

    
412
					pfSense_kill_states("::0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmpv6");
413
				}
414
			}
415
		} else {
416
			continue;
417
		}
418

    
419
		$monitor_ips[] = $gateway['monitor'];
420
		$gateways_arr[$gwname]['enable_dpinger'] = true;
421
		$gateways_arr[$gwname]['gwifip'] = $gwifip;
422
	}
423

    
424
	stop_dpinger();
425

    
426
	/* Start new processes */
427
	foreach ($gateways_arr as $gateway) {
428
		if (!isset($gateway['enable_dpinger'])) {
429
			continue;
430
		}
431

    
432
		if (start_dpinger($gateway) != 0) {
433
			log_error(sprintf(gettext("Error starting gateway monitor for %s"), $gateway['name']));
434
		}
435
	}
436
	if (platform_booting()) {
437
		echo "done.\n";
438
	}
439

    
440
	return;
441
}
442

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

    
459
	foreach (config_get_path('gateways/gateway_group', []) as $group) {
460
		// check if this is the gateway group being queried
461
		if ($group['name'] != $gateway_group) {
462
			continue;
463
		}
464

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

    
478
			$group_gateways[$gateway_item[0]] = intval($gateway_item[1]);
479
		}
480

    
481
		// make sure it's a failover group by checking for different tiers
482
		$group_tiers = array_unique(array_values($group_gateways));
483
		if (count($group_tiers) < 2) {
484
			break;
485
		}
486

    
487
		if (!isset($gateways_states)) {
488
			// get a list of all relevant gateway states
489
			$gateways_states = return_gateways_status(true);
490
		}
491

    
492
		$highest_tier = min($group_tiers);
493
		// remove offline gateways
494
		foreach (array_keys($group_gateways) as $gateway_name) {
495
			if ($gateways_states[$gateway_name]['status'] != 'online') {
496
				unset($group_gateways[$gateway_name]);
497
			}
498
		}
499

    
500
		// no online gateways
501
		if (empty($group_gateways)) {
502
			return 0;
503
		}
504

    
505
		// highest online is highest tier
506
		$highest_online_tier = min(array_values($group_gateways));
507
		if ($highest_online_tier == $highest_tier) {
508
			return 0;
509
		}
510

    
511
		// return the highest-priority failover tier of online gateways
512
		return $highest_online_tier;
513
	}
514

    
515
	return false;
516
}
517

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

    
529
	// if killing states from the firewall itself, check for default failover gateway groups
530
	$remove_states_default = config_get_path('system/remove_failover_states_default');
531
	if ($remove_states_default) {
532
		$default_gateways = [];
533

    
534
		$default_gateway4 = config_get_path('gateways/defaultgw4', '');
535
		if (!empty($default_gateway4)) {
536
			$default_gateways[$default_gateway4] = null;
537
		}
538
		$default_gateway6 = config_get_path('gateways/defaultgw6', '');
539
		if (!empty($default_gateway6)) {
540
			$default_gateways[$default_gateway6] = null;
541
		}
542
	}
543

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

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

    
564
			if (!isset($gateways_states)) {
565
				// get a list of all relevant gateway states
566
				$gateways_states = return_gateways_status(true);
567
			}
568

    
569
			// only process online gateways
570
			if ($gateways_states[$gateway_item[0]]['status'] != 'online') {
571
				continue;
572
			}
573

    
574
			if (!isset($all_gateways)) {
575
				// get a list of all relevant gateways
576
				$all_gateways = get_gateways();
577
			}
578

    
579
			// get the gateway's interface and tier
580
			$gateway_interfaces[$all_gateways[$gateway_item[0]]['interface']] = intval($gateway_item[1]);
581
		}
582

    
583
		// make sure there are at least two online gateways (interfaces) in this gateway group
584
		if (empty($gateway_interfaces) || (count($gateway_interfaces) < 2)) {
585
			continue;
586
		}
587

    
588
		// build a list of interfaces, used to kill states from the firewall itself
589
		if ($remove_states_default && array_key_exists($group['name'], $default_gateways)) {
590
			// get the interfaces of the default gateway group
591
			$default_gateways[$group['name']] = $gateway_interfaces;
592

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

    
599
			// all states are to be killed for this gateway group, skip killing only policy routing states
600
			continue;
601
		}
602

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

    
612
	// kill all states for the default failover gateway groups
613
	if (!$remove_states_default) {
614
		return;
615
	}
616
	foreach ($default_gateways as $gateway_name => $gateway_interfaces) {
617
		if (empty($gateway_interfaces)) {
618
			continue;
619
		}
620

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

    
634
function get_dpinger_status($gwname, $action_disable = false) {
635
	global $g;
636

    
637
	$running_processes = running_dpinger_processes();
638

    
639
	if (!isset($running_processes[$gwname])) {
640
		log_error(sprintf(gettext(
641
		    'dpinger: No dpinger session running for gateway %s'),
642
		    $gwname));
643
		return false;
644
	}
645

    
646
	$proc = $running_processes[$gwname];
647
	unset($running_processes);
648

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

    
661
		$status = '';
662
		while (!feof($fp)) {
663
			$status .= fgets($fp, 1024);
664
		}
665
		fclose($fp);
666

    
667
		$r = array();
668
		list(
669
			$r['gwname'],
670
			$r['latency_avg'],
671
			$r['latency_stddev'],
672
			$r['loss']
673
		) = explode(' ', preg_replace('/\n/', '', $status));
674

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

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

    
692
	$r['srcip'] = $proc['srcip'];
693
	$r['targetip'] = $proc['targetip'];
694

    
695
	$gateways_arr = get_gateways(GW_CACHE_ALL);
696

    
697
	unset($gw);
698
	if (isset($gateways_arr[$gwname])) {
699
		$gw = $gateways_arr[$gwname];
700
	}
701

    
702
	$r['latency_avg'] = round($r['latency_avg']/1000, 3);
703
	$r['latency_stddev'] = round($r['latency_stddev']/1000, 3);
704

    
705
	$r['status'] = "online";
706
	$r['substatus'] = "none";
707
	if (isset($gw) && isset($gw['force_down'])) {
708
		$r['status'] = "down";
709
		$r['substatus'] = "force_down";
710
	} else if (isset($gw)) {
711
		$settings = return_dpinger_defaults();
712

    
713
		$keys = array(
714
		    'latencylow',
715
		    'latencyhigh',
716
		    'losslow',
717
		    'losshigh'
718
		);
719

    
720
		/* Replace default values by user-defined */
721
		foreach ($keys as $key) {
722
			if (isset($gw[$key]) && is_numeric($gw[$key])) {
723
				$settings[$key] = $gw[$key];
724
			}
725
		}
726

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

    
744
	return $r;
745
}
746

    
747
/* return the status of the dpinger targets as an array */
748
function return_gateways_status($byname = false) {
749
	global $config, $g;
750

    
751
	$dpinger_gws = running_dpinger_processes();
752
	$status = array();
753

    
754
	$gateways_arr = get_gateways(GW_CACHE_ALL);
755

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

    
769
		if ($byname == false) {
770
			$target = $dpinger_status['targetip'];
771
		} else {
772
			$target = $gwname;
773
		}
774

    
775
		$status[$target] = array();
776
		$status[$target]['monitorip'] = $dpinger_status['targetip'];
777
		$status[$target]['srcip'] = $dpinger_status['srcip'];
778
		$status[$target]['name'] = $gwname;
779
		$status[$target]['delay'] =
780
		    empty($dpinger_status['latency_avg'])
781
		    ? "0ms"
782
		    : $dpinger_status['latency_avg'] . "ms";
783
		$status[$target]['stddev'] =
784
		    empty($dpinger_status['latency_stddev'])
785
		    ? "0ms"
786
		    : $dpinger_status['latency_stddev'] . "ms";
787
		$status[$target]['loss'] = empty($dpinger_status['loss'])
788
		    ? "0.0%"
789
		    : round($dpinger_status['loss'], 1) . "%";
790
		$status[$target]['status'] = $dpinger_status['status'];
791
		$status[$target]['substatus'] = $dpinger_status['substatus'];
792
	}
793

    
794
	/* tack on any gateways that have monitoring disabled
795
	 * or are down, which could cause gateway groups to fail */
796
	foreach ($gateways_arr as $gwitem) {
797
		if (!isset($gwitem['monitor_disable'])) {
798
			continue;
799
		}
800
		if (!is_ipaddr($gwitem['monitor'])) {
801
			$realif = $gwitem['interface'];
802
			$tgtip = get_interface_gateway($realif);
803
			if (!is_ipaddr($tgtip)) {
804
				$tgtip = "none";
805
			}
806
			$srcip = find_interface_ip($realif);
807
		} else {
808
			$tgtip = $gwitem['monitor'];
809
			$srcip = find_interface_ip($realif);
810
		}
811
		if ($byname == true) {
812
			$target = $gwitem['name'];
813
		} else {
814
			$target = $tgtip;
815
		}
816

    
817
		/* failsafe for down interfaces */
818
		if ($target == "none") {
819
			$target = $gwitem['name'];
820
			$status[$target]['name'] = $gwitem['name'];
821
			$status[$target]['delay'] = "0.0ms";
822
			$status[$target]['loss'] = "100.0%";
823
			$status[$target]['status'] = "down";
824
			$status[$target]['substatus'] = "down";
825
		} else {
826
			$status[$target]['monitorip'] = $tgtip;
827
			$status[$target]['srcip'] = $srcip;
828
			$status[$target]['name'] = $gwitem['name'];
829
			$status[$target]['delay'] = "";
830
			$status[$target]['loss'] = "";
831
			$status[$target]['status'] = "online";
832
			$status[$target]['substatus'] = "none";
833
		}
834

    
835
		$status[$target]['monitor_disable'] = true;
836
	}
837
	return($status);
838
}
839

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

    
864
	$output .= str_pad("Name", $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
865
	foreach ($collist as $hdrcol => $hdrdesc) {
866
		if (strlen($hdrdesc) > $widths[$hdrcol]) {
867
			$widths[$hdrcol] = strlen($hdrdesc);
868
		}
869
		$output .= str_pad($hdrdesc, $widths[$hdrcol] + $col_sep, " ", (substr($hdrcol, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
870
	}
871
	$output .= "\n";
872

    
873
	foreach ($gwstat as $idx => $gw) {
874
		$output .= str_pad($gw['name'], $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
875
		foreach (array_keys($collist) as $col) {
876
			$output .= str_pad($gw[$col], $widths[$col] + $col_sep, " ", (substr($col, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
877
		}
878
		$output .= "\n";
879
	}
880

    
881
	return $output;
882
}
883

    
884
function compare_gateway_order_configured($a, $b) {
885
	/* XXX WAN always has precedence */
886
	if ($a['friendlyiface'] == "wan") {
887
		return -1;
888
	} elseif ($b['friendlyiface'] == "wan") {
889
		return 1;
890
	}
891

    
892
	if ($a['attribute'] === $b['attribute']) {
893
		if ($a['attribute'] === 'system') {
894
			$res = (($a['name'] < $b['name'])) ? -1 : 1;
895
			return $res;
896
		}
897
		return 0;
898
	}
899
	if ($a['attribute'] === 'system' || $b['attribute'] === 'system') {
900
		$res = (($b['attribute'] === 'system')) ? -1 : 1;
901
		return $res;
902
	}
903
	$res = ($a['attribute'] < $b['attribute']) ? -1 : 1;
904
	return $res;
905
}
906

    
907
function order_gateways_as_configured($gateways_arr) {
908
	uasort($gateways_arr, 'compare_gateway_order_configured');
909
	return $gateways_arr;
910
}
911

    
912
/* Return all configured gateways on the system
913
   $disabled = true - include gateways that are disabled
914
   $localhost = true - include "Null" entries for localhost IP addresses
915
   $inactive = true - include gateways on inactive interfaces
916
*/
917
function return_gateways_array($disabled = false, $localhost = false, $inactive = false) {
918
	global $config, $g;
919

    
920
	$gateways_arr = array();
921
	$gateways_arr_temp = array();
922
	$cgw4 = route_get_default('inet');
923
	$cgw6 = route_get_default('inet6');
924
	$found_defaultv4 = 0;
925
	$found_defaultv6 = 0;
926

    
927
	// Ensure the interface cache is up to date first
928
	$interfaces = get_interface_arr(true);
929

    
930
	$i = -1;
931
	/* Process/add all the configured gateways. */
932
	if (is_array($config['gateways']) && is_array($config['gateways']['gateway_item'])) {
933
		foreach (config_get_path('gateways/gateway_item', []) as $gateway) {
934
			if (!is_array($gateway) || empty($gateway)) {
935
				continue;
936
			}
937

    
938
			/* Increment it here to do not skip items */
939
			$i++;
940
			if (isset($gateway['defaultgw'])) {
941
				unset($gateway['defaultgw']);
942
			}
943

    
944
			if (empty($config['interfaces'][$gateway['interface']])) {
945
				if ($inactive === false) {
946
					continue;
947
				} else {
948
					$gateway['inactive'] = true;
949
				}
950
			}
951
			$wancfg = config_get_path("interfaces/{$gateway['interface']}");
952

    
953
			/* skip disabled interfaces */
954
			if ($disabled === false && (!isset($wancfg['enable']))) {
955
				continue;
956
			}
957

    
958
			/* if the gateway is dynamic and we can find the IPv4, Great! */
959
			if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
960
				if ($gateway['ipprotocol'] == "inet") {
961
					/* we know which interfaces is dynamic, this should be made a function */
962
					$gateway['gateway'] = get_interface_gateway($gateway['interface']);
963
					/* no IP address found, set to dynamic */
964
					if (!is_ipaddrv4($gateway['gateway'])) {
965
						$gateway['gateway'] = "dynamic";
966
					}
967
					$gateway['dynamic'] = true;
968
				}
969

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

    
990
			if (isset($gateway['monitor_disable'])) {
991
				$gateway['monitor_disable'] = true;
992
			} else if (empty($gateway['monitor'])) {
993
				$gateway['monitor'] = $gateway['gateway'];
994
			}
995

    
996
			if (isset($gateway['action_disable'])) {
997
				$gateway['action_disable'] = true;
998
			}
999

    
1000
			$gateway['friendlyiface'] = $gateway['interface'];
1001
			$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($gateway['interface']);
1002

    
1003
			/* special treatment for tunnel interfaces */
1004
			if ($gateway['ipprotocol'] == "inet6") {
1005
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
1006
			} else {
1007
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet", false, false);
1008
			}
1009

    
1010
			if ($gateway['ipprotocol'] == "inet" &&
1011
					($gateway['gateway'] == $cgw4)) {
1012
				$gateway['isdefaultgw'] = true;
1013
				$found_defaultv4 = 1;
1014
			} else if ($gateway['ipprotocol'] == "inet6" &&
1015
					($gateway['gateway'] == $cgw6)) {
1016
				$gateway['isdefaultgw'] = true;
1017
				$found_defaultv6 = 1;
1018
			}
1019
			/* include the gateway index as the attribute */
1020
			$gateway['attribute'] = $i;
1021

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

    
1026
			/* skip disabled gateways if the caller has not asked for them to be returned. */
1027
			if (!($disabled === false && isset($gateway['disabled']))) {
1028
				$gateways_arr[$gateway['name']] = $gateway;
1029
			}
1030
		}
1031
	}
1032
	unset($gateway);
1033

    
1034
	//Sort the array by GW name before moving on.
1035
	ksort($gateways_arr, SORT_STRING | SORT_FLAG_CASE);
1036

    
1037
	/* Loop through all interfaces with a gateway and add it to a array */
1038
	if ($disabled == false) {
1039
		$iflist = get_configured_interface_with_descr();
1040
	} else {
1041
		$iflist = get_configured_interface_with_descr(true);
1042
	}
1043

    
1044
	/* Process/add dynamic v4 gateways. */
1045
	foreach ($iflist as $ifname => $friendly) {
1046
		if (!interface_has_gateway($ifname)) {
1047
			continue;
1048
		}
1049

    
1050
		if (empty($config['interfaces'][$ifname])) {
1051
			continue;
1052
		}
1053

    
1054
		$ifcfg = &$config['interfaces'][$ifname];
1055
		if (!isset($ifcfg['enable'])) {
1056
			continue;
1057
		}
1058

    
1059
		if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
1060
			continue;
1061
		}
1062

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

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

    
1157
		if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
1158
			$gateway['isdefaultgw'] = true;
1159
			$gateway['dynamic'] = true;
1160
			$found_defaultv4 = 1;
1161
		}
1162

    
1163
		/* Loopback dummy for dynamic interfaces without a IP */
1164
		if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
1165
			$gateway['gateway'] = "dynamic";
1166
		}
1167

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

    
1176
		if (is_ipaddrv4($gateway['gateway'])) {
1177
			$gateway['monitor'] = $gateway['gateway'];
1178
		}
1179

    
1180
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
1181
		$gateways_arr[$gateway['name']] = $gateway;
1182
	}
1183
	unset($gateway);
1184

    
1185
	/* Process/add dynamic v6 gateways. */
1186
	foreach ($iflist as $ifname => $friendly) {
1187
		/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
1188
		if (!isset($config['system']['ipv6allow'])) {
1189
			break;
1190
		}
1191

    
1192
		if (!interface_has_gatewayv6($ifname)) {
1193
			continue;
1194
		}
1195

    
1196
		if (empty($config['interfaces'][$ifname])) {
1197
			continue;
1198
		}
1199

    
1200
		$ifcfg = &$config['interfaces'][$ifname];
1201
		if (!isset($ifcfg['enable'])) {
1202
			continue;
1203
		}
1204

    
1205
		if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
1206
			continue;
1207
		}
1208

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

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

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

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

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

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

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

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

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

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

    
1390
	$gways = order_gateways_as_configured($gateways_arr);
1391

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

    
1397
	return $gways;
1398
}
1399

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

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

    
1433
			if (isset($gwsttng['monitor_disable']) || isset($gwsttng['action_disable']) ||
1434
			    ($gateways_status[$gwname]['status'] == "online")) {
1435
				$set_dfltgwname = $gwname;
1436
				break;
1437
			}
1438
			if (empty($fallback) && $gwsttng['interface'] != 'lo0') {
1439
				$fallback = $gwname;
1440
			}
1441
		}
1442
		if (empty($set_dfltgwname) && !empty($fallback)) {
1443
			log_error(sprintf("Gateway, none 'available' for %s, use the first one configured. '%s'", $ipprotocol, $fallback));
1444
			$set_dfltgwname = $fallback;
1445
		} else {
1446
			log_error("Gateway, NONE AVAILABLE");
1447
		}
1448
	} else {
1449
		// a gwgroup is selected
1450
		// find the best available gateway given options available..
1451
		$gwg_members = array();
1452
		$viplist = get_configured_vip_list();
1453
		if (is_array($config['gateways']['gateway_group'])) {
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

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

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

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

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

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

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

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

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

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

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

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

    
1698
/*
1699
 * Return an array with all gateway groups with name as key
1700
 * All gateway groups will be processed before returning the array.
1701
 */
1702
function return_gateway_groups_array($fixup = false) {
1703
	global $config;
1704

    
1705
	/* fetch the current gateways status */
1706
	$gateways_status = return_gateways_status(true);
1707
	$gateways_arr = get_gateways();
1708
	$gateway_groups_array = array();
1709
	if ($fixup == true) {
1710
		$gw4 = lookup_gateway_or_group_by_name(config_get_path('gateways/defaultgw4', ""));
1711
		$gw6 = lookup_gateway_or_group_by_name(config_get_path('gateways/defaultgw6', ""));
1712
		if ($gw4 && $gw4['type'] == 'gatewaygroup') {
1713
			fixup_default_gateway("inet", $gateways_status, $gateways_arr);
1714
		}
1715
		if ($gw6 && $gw6['type'] == 'gatewaygroup') {
1716
			fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
1717
		}
1718
	}
1719
	init_config_arr(array('gateways', 'gateway_group'));
1720
	if (!empty($config['gateways']['gateway_group'])) {
1721
		$viplist = get_configured_vip_list();
1722
		foreach (config_get_path('gateways/gateway_group', []) as $group) {
1723
			$gateway_groups_array[$group['name']] = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1724
		}
1725
	}
1726

    
1727
	return ($gateway_groups_array);
1728
}
1729

    
1730
/* Update DHCP WAN Interface ip address in gateway group item */
1731
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
1732
	global $config;
1733

    
1734
	if (is_array($config['gateways']['gateway_item'])) {
1735
		$gwlist = config_get_path('gateways/gateway_item', []);
1736
		foreach ($gwlist as & $gw) {
1737
			if ($gw['interface'] != $interface) {
1738
				continue;
1739
			}
1740

    
1741
			$current_gw = get_interface_gateway($interface);
1742
			if ($gw['gateway'] <> $current_gw) {
1743
				$gw['gateway'] = $current_gw;
1744
				$changed = true;
1745
			}
1746
		}
1747
		config_set_path('gateways/gateway_item', $gwlist);
1748
	}
1749

    
1750
	if ($changed && $current_gw) {
1751
		write_config(sprintf(gettext(
1752
		    'Updating gateway group gateway for %1$s - new gateway is %2$s'),
1753
		    $interface, $current_gw));
1754
	}
1755
}
1756

    
1757
function lookup_gateway_or_group_by_name($gwname) {
1758
	global $config;
1759

    
1760
	$gateways_arr = get_gateways(GW_CACHE_ALL);
1761

    
1762
	foreach ($gateways_arr as $gw) {
1763
		if ($gw['name'] == $gwname) {
1764
			$gw['type'] = 'gateway';
1765
			return $gw;
1766
		}
1767
	}
1768

    
1769
	init_config_arr(array('gateways', 'gateway_group'));
1770
	foreach (config_get_path('gateways/gateway_group', []) as $gwg) {
1771
		if ($gwg['name'] == $gwname) {
1772
			$gwg['type'] = 'gatewaygroup';
1773
			return $gwg;
1774
		}
1775
	}
1776

    
1777
	return false;
1778
}
1779

    
1780
function lookup_gateway_ip_by_name($name, $disabled = false) {
1781

    
1782
	$gateways_arr = get_gateways(($disabled ? GW_CACHE_DISABLED : 0) | GW_CACHE_LOCALHOST);
1783
	foreach ($gateways_arr as $gname => $gw) {
1784
		if ($gw['name'] === $name || $gname === $name) {
1785
			return $gw['gateway'];
1786
		}
1787
	}
1788

    
1789
	return false;
1790
}
1791

    
1792
function lookup_gateway_monitor_ip_by_name($name) {
1793

    
1794
	$gateways_arr = get_gateways(GW_CACHE_LOCALHOST);
1795
	if (!empty($gateways_arr[$name])) {
1796
		$gateway = $gateways_arr[$name];
1797
		if (!is_ipaddr($gateway['monitor'])) {
1798
			return $gateway['gateway'];
1799
		}
1800

    
1801
		return $gateway['monitor'];
1802
	}
1803

    
1804
	return (false);
1805
}
1806

    
1807
function lookup_gateway_interface_by_name($name) {
1808

    
1809
	$gateways_arr = get_gateways(GW_CACHE_LOCALHOST);
1810
	if (!empty($gateways_arr[$name])) {
1811
		$interfacegw = $gateways_arr[$name]['friendlyiface'];
1812
		return ($interfacegw);
1813
	}
1814

    
1815
	return (false);
1816
}
1817

    
1818
function get_root_interface($interface) {
1819
	if (substr($interface, 0, 4) == '_vip') {
1820
		$interface = get_configured_vip_interface($interface);
1821
		if (substr($interface, 0, 4) == '_vip') {
1822
			$interface = get_configured_vip_interface($interface);
1823
		}
1824
	}
1825
	return $interface;
1826
}
1827

    
1828
function get_interface_gateway($interface, &$dynamic = false) {
1829
	global $config, $g;
1830

    
1831
	$interface = get_root_interface($interface);
1832

    
1833
	$gw = NULL;
1834
	$gwcfg = config_get_path("interfaces/{$interface}");
1835
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
1836
		foreach (config_get_path('gateways/gateway_item', []) as $gateway) {
1837
			if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
1838
				$gw = $gateway['gateway'];
1839
				break;
1840
			}
1841
		}
1842
	}
1843

    
1844
	// for dynamic interfaces we handle them through the $interface_router file.
1845
	if (($gw == NULL || !is_ipaddrv4($gw)) && !is_ipaddrv4($gwcfg['ipaddr'])) {
1846
		$realif = get_real_interface($interface);
1847
		if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
1848
			$gw = trim(@file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
1849
			$dynamic = true;
1850
		}
1851
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw")) {
1852
			$dynamic = "default";
1853
		}
1854

    
1855
	}
1856

    
1857
	/* return gateway */
1858
	return ($gw);
1859
}
1860

    
1861
function get_interface_gateway_last($interface, $family = 'inet') {
1862
	global $config, $g;
1863
	$interface = get_root_interface($interface);
1864
	$realif = get_real_interface($interface);
1865
	$suffix = ($family == 'inet6') ? 'v6' : '';
1866
	if (file_exists("{$g['tmp_path']}/{$realif}_router{$suffix}.last")) {
1867
		return trim(@file_get_contents("{$g['tmp_path']}/{$realif}_router{$suffix}.last"), " \n");
1868
	}
1869
	return '';
1870
}
1871

    
1872
function get_interface_gateway_v6($interface, &$dynamic = false) {
1873
	global $config, $g;
1874

    
1875
	$interface = get_root_interface($interface);
1876

    
1877
	$gw = NULL;
1878
	$gwcfg = config_get_path("interfaces/{$interface}");
1879
	if (!empty($gwcfg['gatewayv6']) && is_array($config['gateways']['gateway_item'])) {
1880
		foreach (config_get_path('gateways/gateway_item', []) as $gateway) {
1881
			if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
1882
				$gw = $gateway['gateway'];
1883
				break;
1884
			}
1885
		}
1886
	}
1887

    
1888
	// for dynamic interfaces we handle them through the $interface_router file.
1889
	if (($gw == NULL || !is_ipaddrv6($gw)) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
1890
		$realif = get_real_interface($interface);
1891
		if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
1892
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
1893
			$dynamic = true;
1894
		}
1895
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6")) {
1896
			$dynamic = "default";
1897
		}
1898
	}
1899
	/* return gateway */
1900
	return ($gw);
1901
}
1902

    
1903
/* Check a IP address against a gateway IP or name
1904
 * to verify it's address family */
1905
function validate_address_family($ipaddr, $gwname, $disabled = false) {
1906
	$v4ip = false;
1907
	$v6ip = false;
1908
	$v4gw = false;
1909
	$v6gw = false;
1910

    
1911
	if (is_ipaddrv4($ipaddr)) {
1912
		$v4ip = true;
1913
	}
1914
	if (is_ipaddrv6($ipaddr)) {
1915
		$v6ip = true;
1916
	}
1917
	if (is_ipaddrv4($gwname)) {
1918
		$v4gw = true;
1919
	}
1920
	if (is_ipaddrv6($gwname)) {
1921
		$v6gw = true;
1922
	}
1923

    
1924
	if ($v4ip && $v4gw) {
1925
		return true;
1926
	}
1927
	if ($v6ip && $v6gw) {
1928
		return true;
1929
	}
1930

    
1931
	/* still no match, carry on, lookup gateways */
1932
	if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname, $disabled))) {
1933
		$v4gw = true;
1934
	}
1935
	if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname, $disabled))) {
1936
		$v6gw = true;
1937
	}
1938

    
1939
	/* Check gateways including disabled gateways and gateways for disabled
1940
	 * interfaces.
1941
	 * https://redmine.pfsense.org/issues/8846
1942
	 */
1943
	$gw_array = get_gateways(GW_CACHE_DISABLED | GW_CACHE_INACTIVE);
1944
	if (is_array($gw_array[$gwname])) {
1945
		switch ($gw_array[$gwname]['ipprotocol']) {
1946
			case "inet":
1947
				$v4gw = true;
1948
				break;
1949
			case "inet6":
1950
				$v6gw = true;
1951
				break;
1952
		}
1953
	}
1954

    
1955
	if ($v4ip && $v4gw) {
1956
		return true;
1957
	}
1958
	if ($v6ip && $v6gw) {
1959
		return true;
1960
	}
1961

    
1962
	return false;
1963
}
1964

    
1965
/* check if a interface is part of a gateway group */
1966
function interface_gateway_group_member($interface, $gwgroup_name = "") {
1967
	global $config;
1968

    
1969
	if (is_array($config['gateways']['gateway_group'])) {
1970
		$groups = config_get_path('gateways/gateway_group');
1971
	} else {
1972
		return false;
1973
	}
1974

    
1975
	$gateways_arr = get_gateways(GW_CACHE_LOCALHOST);
1976
	foreach ($groups as $group) {
1977
		if (is_array($group['item'])) {
1978
			foreach ($group['item'] as $item) {
1979
				$elements = explode("|", $item);
1980
				$gwname = $elements[0];
1981
				if ($interface == $gateways_arr[$gwname]['interface'] &&
1982
				    (empty($gwgroup_name) || $gwgroup_name == $group['name'])) {
1983
					unset($gateways_arr);
1984
					return true;
1985
				}
1986
			}
1987
		}
1988
	}
1989
	unset($gateways_arr);
1990

    
1991
	return false;
1992
}
1993

    
1994
function gateway_is_gwgroup_member($name, $detail=false) {
1995
	global $config;
1996

    
1997
	if (is_array($config['gateways']['gateway_group'])) {
1998
		$groups = config_get_path('gateways/gateway_group');
1999
	} else {
2000
		return false;
2001
	}
2002

    
2003
	$members = array();
2004
	foreach ($groups as $group) {
2005
		if (is_array($group['item'])) {
2006
			foreach ($group['item'] as $item) {
2007
				list($gwname, $tier, $vipname) = explode("|", $item);
2008
				if ($name == $gwname) {
2009
					if ($detail) {
2010
						$newitem = array();
2011
						$newitem['name'] = $group['name'];
2012
						$newitem['tier'] = $tier;
2013
						$newitem['vipname'] = $vipname;
2014
						$members[] = $newitem;
2015
					} else {
2016
						$members[] = $group['name'];
2017
					}
2018
				}
2019
			}
2020
		}
2021
	}
2022

    
2023
	return $members;
2024
}
2025
/*
2026
  Check the proposed gateway settings to see if they are valid.
2027
  $gateway_settings - the proposed array of proposed gateway settings
2028
  $id - the index of the gateway proposed to be modified (otherwise "" if adding a new gateway)
2029
  $parent_ip - the IP (v4 or v6) address about to be set on the corresponding interface (if any)
2030
  $parent_sn - the subnet about to be set on the corresponding interface (if any)
2031
  (Note: the above 2 parameters allow gateway parameters to be validated concurrently with saving
2032
   an interface, before the new interface parameters are actually saved in the config.)
2033
  Return completed $input_errors array if there is any problem.
2034
  Otherwise return an empty $input_errors array
2035
*/
2036
function validate_gateway($gateway_settings, $id = "", $parent_ip = "", $parent_sn = "", $json = false) {
2037
	if ($json) {
2038
		$gateway_settings = json_decode($gateway_settings, true);
2039
	}
2040
	global $config, $gateway_state_kill_modes;
2041

    
2042
	refresh_gateways(); // make sure we're validating against a current gateway list
2043
	$a_gateways = get_gateways(GW_CACHE_INDEXED);
2044
	$input_errors = array();
2045

    
2046
	/* input validation */
2047
	$reqdfields = explode(" ", "name interface");
2048
	$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
2049

    
2050
	do_input_validation($gateway_settings, $reqdfields, $reqdfieldsn, $input_errors);
2051

    
2052
	if (!isset($gateway_settings['name'])) {
2053
		$input_errors[] = "A valid gateway name must be specified.";
2054
	}
2055
	if (!is_validaliasname($gateway_settings['name'])) {
2056
		$input_errors[] = invalidaliasnamemsg($gateway_settings['name'], gettext("gateway"));
2057
	} else if (isset($gateway_settings['disabled'])) {
2058
		// We have a valid gateway name that the user wants to mark as disabled.
2059
		// Check if the gateway name is used in any gateway group.
2060
		foreach (config_get_path('gateways/gateway_group', []) as $group) {
2061
			foreach ($group['item'] as $item) {
2062
				$items = explode("|", $item);
2063
				if ($items[0] == $gateway_settings['name']) {
2064
					$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']);
2065
				}
2066
			}
2067
		}
2068

    
2069
		// Check if the gateway name is used in any enabled Static Route.
2070
		foreach (config_get_path('staticroutes/route', []) as $route) {
2071
			if ($route['gateway'] == $gateway_settings['name']) {
2072
				if (!isset($route['disabled'])) {
2073
					// There is a static route that uses this gateway and is enabled (not disabled).
2074
					$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']);
2075
				}
2076
			}
2077
		}
2078

    
2079
		// Check if the gateway name is used by any DNS Server
2080
		$dnsgw_counter = 1;
2081
		while (isset($config["system"]["dns{$dnsgw_counter}gw"])) {
2082
			if ($gateway_settings['name'] == $config["system"]["dns{$dnsgw_counter}gw"]) {
2083
				// The user wants to disable this gateway, but there is a static route to the DNS server that refers to the gateway.
2084
				$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use by DNS Server "%2$s"'), $gateway_settings['name'], $config["system"]["dns{$dnsgw_counter}gw"]);
2085
			}
2086
			$dnsgw_counter++;
2087
		}
2088
	}
2089
	/* skip system gateways which have been automatically added */
2090
	if (($gateway_settings['gateway'] && (!is_ipaddr($gateway_settings['gateway'])) && ($gateway_settings['attribute'] !== "system")) && ($gateway_settings['gateway'] != "dynamic")) {
2091
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
2092
	}
2093

    
2094
	if ($gateway_settings['gateway'] && is_ipaddr($gateway_settings['gateway'])) {
2095
		if (is_ipaddrv4($gateway_settings['gateway'])) {
2096
			if ($parent_ip == '') {
2097
				$parent_ip = get_interface_ip($gateway_settings['interface']);
2098
				$parent_sn = get_interface_subnet($gateway_settings['interface']);
2099
			}
2100
			if (empty($parent_ip) || empty($parent_sn)) {
2101
				$input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
2102
			} elseif (!isset($gateway_settings["nonlocalgateway"])) {
2103
				$subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
2104
				$vips = link_interface_to_vips($gateway_settings['interface']);
2105
				if (is_array($vips)) {
2106
					foreach ($vips as $vip) {
2107
						if (!is_ipaddrv4($vip['subnet'])) {
2108
							continue;
2109
						}
2110
						$subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
2111
					}
2112
				}
2113

    
2114
				$found = false;
2115
				foreach ($subnets as $subnet) {
2116
					if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
2117
						$found = true;
2118
						break;
2119
					}
2120
				}
2121

    
2122
				if ($found === false) {
2123
					$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
2124
				}
2125
			}
2126
		} else if (is_ipaddrv6($gateway_settings['gateway'])) {
2127
			/* do not do a subnet match on a link local address, it's valid */
2128
			if (!is_linklocal($gateway_settings['gateway'])) {
2129
				if ($parent_ip == '') {
2130
					$parent_ip = get_interface_ipv6($gateway_settings['interface']);
2131
					$parent_sn = get_interface_subnetv6($gateway_settings['interface']);
2132
				}
2133
				if (empty($parent_ip) || empty($parent_sn)) {
2134
					$input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
2135
				} elseif (!isset($gateway_settings["nonlocalgateway"])) {
2136
					$subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
2137
					$vips = link_interface_to_vips($gateway_settings['interface']);
2138
					if (is_array($vips)) {
2139
						foreach ($vips as $vip) {
2140
							if (!is_ipaddrv6($vip['subnet'])) {
2141
								continue;
2142
							}
2143
							$subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
2144
						}
2145
					}
2146

    
2147
					$found = false;
2148
					foreach ($subnets as $subnet) {
2149
						if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
2150
							$found = true;
2151
							break;
2152
						}
2153
					}
2154

    
2155
					if ($found === false) {
2156
						$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
2157
					}
2158
				}
2159
			}
2160
		}
2161

    
2162
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddr'])) {
2163
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddr']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
2164
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
2165
			}
2166
		}
2167
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddrv6'])) {
2168
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddrv6']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
2169
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
2170
			}
2171
		}
2172
	}
2173
	if (($gateway_settings['monitor'] != "") && ($gateway_settings['monitor'] != "dynamic")) {
2174
		validateipaddr($gateway_settings['monitor'], IPV4V6, "Monitor IP", $input_errors, false);
2175
	}
2176
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] < 0) {
2177
		$input_errors[] = gettext("A valid data payload must be specified.");
2178
	}
2179
	if (!empty($gateway_settings['skip_rules_gw_down']) && !array_key_exists($gateway_settings['skip_rules_gw_down'], $gateway_state_kill_modes)) {
2180
		$input_errors[] = gettext("Please select a valid State Killing on Gateway Failure mode.");
2181
	}
2182
	/* only allow correct IPv4 and IPv6 gateway addresses */
2183
	if (($gateway_settings['gateway'] <> "") && is_ipaddr($gateway_settings['gateway']) && $gateway_settings['gateway'] != "dynamic") {
2184
		if (is_ipaddrv6($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet")) {
2185
			$input_errors[] = sprintf(gettext("The IPv6 gateway address '%s' can not be used as a IPv4 gateway."), $gateway_settings['gateway']);
2186
		}
2187
		if (is_ipaddrv4($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet6")) {
2188
			$input_errors[] = sprintf(gettext("The IPv4 gateway address '%s' can not be used as a IPv6 gateway."), $gateway_settings['gateway']);
2189
		}
2190
	}
2191
	/* only allow correct IPv4 and IPv6 monitor addresses */
2192
	if (($gateway_settings['monitor'] <> "") && is_ipaddr($gateway_settings['monitor']) && $gateway_settings['monitor'] != "dynamic") {
2193
		if (is_ipaddrv6($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet")) {
2194
			$input_errors[] = sprintf(gettext("The IPv6 monitor address '%s' can not be used on a IPv4 gateway."), $gateway_settings['monitor']);
2195
		}
2196
		if (is_ipaddrv4($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet6")) {
2197
			$input_errors[] = sprintf(gettext("The IPv4 monitor address '%s' can not be used on a IPv6 gateway."), $gateway_settings['monitor']);
2198
		}
2199
	}
2200

    
2201
	if (isset($gateway_settings['name'])) {
2202
		/* check for overlaps */
2203
		foreach ($a_gateways as $gateway) {
2204
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
2205
				if ($gateway['name'] != $gateway_settings['name']) {
2206
					$input_errors[] = gettext("Changing name on a gateway is not allowed.");
2207
				}
2208
				continue;
2209
			}
2210
			if ($gateway_settings['name'] <> "") {
2211
				if (($gateway['name'] <> "") && ($gateway_settings['name'] == $gateway['name']) && ($gateway['attribute'] !== "system")) {
2212
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $gateway_settings['name']);
2213
					break;
2214
				}
2215
			}
2216
			if (is_ipaddr($gateway_settings['gateway'])) {
2217
				if (($gateway['gateway'] <> "") && ($gateway_settings['gateway'] == $gateway['gateway']) && ($gateway['attribute'] !== "system")) {
2218
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $gateway_settings['gateway']);
2219
					break;
2220
				}
2221
			}
2222
			if (is_ipaddr($gateway_settings['monitor'])) {
2223
				if (($gateway['monitor'] <> "") && ($gateway_settings['monitor'] == $gateway['monitor']) && ($gateway['attribute'] !== "system")) {
2224
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. A different monitor IP must be chosen.'), $gateway_settings['monitor']);
2225
					break;
2226
				}
2227
			}
2228
		}
2229
	}
2230

    
2231
	/* input validation of dpinger advanced parameters */
2232

    
2233
	$dpinger_default = return_dpinger_defaults();
2234
	$latencylow = $dpinger_default['latencylow'];
2235
	if ($gateway_settings['latencylow']) {
2236
		if (!is_numeric($gateway_settings['latencylow'])) {
2237
			$input_errors[] = gettext("The low latency threshold needs to be a numeric value.");
2238
		} else if ($gateway_settings['latencylow'] < 1) {
2239
			$input_errors[] = gettext("The low latency threshold needs to be positive.");
2240
		} else {
2241
			$latencylow = $gateway_settings['latencylow'];
2242
		}
2243
	}
2244

    
2245
	$latencyhigh = $dpinger_default['latencyhigh'];
2246
	if ($gateway_settings['latencyhigh']) {
2247
		if (!is_numeric($gateway_settings['latencyhigh'])) {
2248
			$input_errors[] = gettext("The high latency threshold needs to be a numeric value.");
2249
		} else if ($gateway_settings['latencyhigh'] < 1) {
2250
			$input_errors[] = gettext("The high latency threshold needs to be positive.");
2251
		} else {
2252
			$latencyhigh = $gateway_settings['latencyhigh'];
2253
		}
2254
	}
2255

    
2256
	$losslow = $dpinger_default['losslow'];
2257
	if ($gateway_settings['losslow']) {
2258
		if (!is_numeric($gateway_settings['losslow'])) {
2259
			$input_errors[] = gettext("The low Packet Loss threshold needs to be a numeric value.");
2260
		} else if ($gateway_settings['losslow'] < 1) {
2261
			$input_errors[] = gettext("The low Packet Loss threshold needs to be positive.");
2262
		} else if ($gateway_settings['losslow'] >= 100) {
2263
			$input_errors[] = gettext("The low Packet Loss threshold needs to be less than 100.");
2264
		} else {
2265
			$losslow = $gateway_settings['losslow'];
2266
		}
2267
	}
2268

    
2269
	$losshigh = $dpinger_default['losshigh'];
2270
	if ($gateway_settings['losshigh']) {
2271
		if (!is_numeric($gateway_settings['losshigh'])) {
2272
			$input_errors[] = gettext("The high Packet Loss threshold needs to be a numeric value.");
2273
		} else if ($gateway_settings['losshigh'] < 1) {
2274
			$input_errors[] = gettext("The high Packet Loss threshold needs to be positive.");
2275
		} else if ($gateway_settings['losshigh'] > 100) {
2276
			$input_errors[] = gettext("The high Packet Loss threshold needs to be 100 or less.");
2277
		} else {
2278
			$losshigh = $gateway_settings['losshigh'];
2279
		}
2280
	}
2281

    
2282
	$time_period = $dpinger_default['time_period'];
2283
	if ($gateway_settings['time_period']) {
2284
		if (!is_numeric($gateway_settings['time_period'])) {
2285
			$input_errors[] = gettext("The time period over which results are averaged needs to be a numeric value.");
2286
		} else if ($gateway_settings['time_period'] < 1) {
2287
			$input_errors[] = gettext("The time period over which results are averaged needs to be positive.");
2288
		} else {
2289
			$time_period = $gateway_settings['time_period'];
2290
		}
2291
	}
2292

    
2293
	$interval = $dpinger_default['interval'];
2294
	if ($gateway_settings['interval']) {
2295
		if (!is_numeric($gateway_settings['interval'])) {
2296
			$input_errors[] = gettext("The probe interval needs to be a numeric value.");
2297
		} else if ($gateway_settings['interval'] < 1) {
2298
			$input_errors[] = gettext("The probe interval needs to be positive.");
2299
		} else {
2300
			$interval = $gateway_settings['interval'];
2301
		}
2302
	}
2303

    
2304
	$loss_interval = $dpinger_default['loss_interval'];
2305
	if ($gateway_settings['loss_interval']) {
2306
		if (!is_numeric($gateway_settings['loss_interval'])) {
2307
			$input_errors[] = gettext("The loss interval needs to be a numeric value.");
2308
		} else if ($gateway_settings['loss_interval'] < 1) {
2309
			$input_errors[] = gettext("The loss interval setting needs to be positive.");
2310
		} else {
2311
			$loss_interval = $gateway_settings['loss_interval'];
2312
		}
2313
	}
2314

    
2315
	$alert_interval = $dpinger_default['alert_interval'];
2316
	if ($gateway_settings['alert_interval']) {
2317
		if (!is_numeric($gateway_settings['alert_interval'])) {
2318
			$input_errors[] = gettext("The alert interval needs to be a numeric value.");
2319
		} else if ($gateway_settings['alert_interval'] < 1) {
2320
			$input_errors[] = gettext("The alert interval setting needs to be positive.");
2321
		} else {
2322
			$alert_interval = $gateway_settings['alert_interval'];
2323
		}
2324
	}
2325

    
2326
	if ($latencylow >= $latencyhigh) {
2327
		$input_errors[] = gettext(
2328
		    "The high latency threshold needs to be greater than the low latency threshold");
2329
	}
2330

    
2331
	if ($losslow >= $losshigh) {
2332
		$input_errors[] = gettext(
2333
		    "The high packet loss threshold needs to be higher than the low packet loss threshold");
2334
	}
2335

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

    
2342
	// Ensure that the time period is greater than 2 times the probe interval plus the loss interval.
2343
	if (($interval * 2 + $loss_interval) >= $time_period) {
2344
		$input_errors[] = gettext("The time period needs to be greater than twice the probe interval plus the loss interval.");
2345
	}
2346

    
2347
	// There is no point recalculating the average latency and loss more often than the probe interval.
2348
	// So the alert interval needs to be >= probe interval.
2349
	if ($interval > $alert_interval) {
2350
		$input_errors[] = gettext("The alert interval needs to be greater than or equal to the probe interval.");
2351
	}
2352

    
2353
	if ($json) {
2354
		return json_encode($input_errors);
2355
	}
2356
	return $input_errors;
2357
}
2358

    
2359
// Save gateway settings.
2360
// $gateway_settings - the array of gateway setting parameters
2361
// $realid - the index of the gateway to be modified (otherwise "" if adding a new gateway)
2362

    
2363
// This function is responsible to:
2364
//   Setup the gateway parameter structure from the gateway settings input parameter
2365
//   Save the structure into the config
2366
//   Remove any run-time settings from gateway parameters that are changed (e.g. remove routes to addresses that are changing)
2367

    
2368
// A subsequent "apply" step will implement the added/changed gateway.
2369

    
2370
function save_gateway($gateway_settings, $realid = "", $json = false) {
2371
	if ($json) {
2372
		$gateway_settings = json_decode($gateway_settings, true);
2373
	}
2374
	global $config;
2375

    
2376
	init_config_arr(array('gateways', 'gateway_item'));
2377
	$a_gateway_item = &$config['gateways']['gateway_item'];
2378
	$reloadif = "";
2379
	$gateway = array();
2380

    
2381
	if (empty($gateway_settings['interface'])) {
2382
		$gateway['interface'] = $gateway_settings['friendlyiface'];
2383
	} else {
2384
		$gateway['interface'] = $gateway_settings['interface'];
2385
	}
2386
	if (is_ipaddr($gateway_settings['gateway'])) {
2387
		$gateway['gateway'] = $gateway_settings['gateway'];
2388
	} else {
2389
		$gateway['gateway'] = "dynamic";
2390
	}
2391
	$gateway['name'] = $gateway_settings['name'];
2392
	$gateway['weight'] = $gateway_settings['weight'];
2393
	$gateway['ipprotocol'] = $gateway_settings['ipprotocol'];
2394
	if ($gateway_settings['interval']) {
2395
		$gateway['interval'] = $gateway_settings['interval'];
2396
	}
2397

    
2398
	if ($gateway_settings['time_period']) {
2399
		$gateway['time_period'] = $gateway_settings['time_period'];
2400
	}
2401
	if ($gateway_settings['alert_interval']) {
2402
		$gateway['alert_interval'] = $gateway_settings['alert_interval'];
2403
	}
2404

    
2405
	$gateway['descr'] = $gateway_settings['descr'];
2406
	if ($gateway_settings['monitor_disable'] == "yes") {
2407
		$gateway['monitor_disable'] = true;
2408
	}
2409
	if ($gateway_settings['action_disable'] == "yes") {
2410
		$gateway['action_disable'] = true;
2411
	}
2412
	if ($gateway_settings['nonlocalgateway'] == "yes") {
2413
		$gateway['nonlocalgateway'] = true;
2414
	}
2415
	if ($gateway_settings['dpinger_dont_add_static_route'] == "yes") {
2416
		$gateway['dpinger_dont_add_static_route'] = true;
2417
	}
2418
	if ($gateway_settings['force_down'] == "yes") {
2419
		$gateway['force_down'] = true;
2420
	}
2421
	$gateway['gw_down_kill_states'] = $gateway_settings['gw_down_kill_states'];
2422
	if (is_ipaddr($gateway_settings['monitor'])) {
2423
		$gateway['monitor'] = $gateway_settings['monitor'];
2424
	}
2425
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] >= 0) {
2426
		$gateway['data_payload'] = $gateway_settings['data_payload'];
2427
	}
2428

    
2429
	/* NOTE: If gateway ip is changed need to cleanup the old static interface route */
2430
	if ($gateway_settings['monitor'] != "dynamic" &&
2431
	    !empty($a_gateway_item[$realid]) &&
2432
	    is_ipaddr($a_gateway_item[$realid]['gateway']) &&
2433
	    $gateway['gateway'] != $a_gateway_item[$realid]['gateway'] &&
2434
	    isset($a_gateway_item[$realid]["nonlocalgateway"])) {
2435
		route_del($a_gateway_item[$realid]['gateway']);
2436
	}
2437

    
2438
	/* NOTE: If monitor ip is changed need to cleanup the old static route */
2439
	if ($gateway_settings['monitor'] != "dynamic" &&
2440
	    !empty($a_gateway_item[$realid]) &&
2441
	    is_ipaddr($a_gateway_item[$realid]['monitor']) &&
2442
	    $gateway_settings['monitor'] != $a_gateway_item[$realid]['monitor'] &&
2443
	    $gateway['gateway'] != $a_gateway_item[$realid]['monitor']) {
2444
		route_del($a_gateway_item[$realid]['monitor']);
2445
	}
2446

    
2447
	if ($gateway_settings['defaultgw'] == "yes" || $gateway_settings['defaultgw'] == "on") {
2448
		// a new default gateway is being saved.
2449
		$i = 0;
2450
		/* remove the default gateway bits for all gateways with the same address family */
2451
		if (is_array($a_gateway_item)) {
2452
			foreach ($a_gateway_item as $gw) {
2453
				if ($gateway['ipprotocol'] == $gw['ipprotocol']) {
2454
					if ($gw['interface'] != $gateway_settings['interface'] &&
2455
						($gw['name'] == config_get_path('gateways/defaultgw4', "") ||
2456
						 $gw['name'] == config_get_path('gateways/defaultgw6', ""))) {
2457
						// remember the old default gateway interface to call with a "interface reconfigure" event.
2458
						$reloadif = $gw['interface'];
2459
					}
2460
				}
2461
				$i++;
2462
			}
2463
		}
2464
		if ($gateway['ipprotocol'] == "inet") {
2465
			config_set_path('gateways/defaultgw4', $gateway['name']);
2466
		} elseif ($gateway['ipprotocol'] == "inet6") {
2467
			config_set_path('gateways/defaultgw4', $gateway['name']);
2468
		}
2469
	}
2470

    
2471
	if ($gateway_settings['latencylow']) {
2472
		$gateway['latencylow'] = $gateway_settings['latencylow'];
2473
	}
2474
	if ($gateway_settings['latencyhigh']) {
2475
		$gateway['latencyhigh'] = $gateway_settings['latencyhigh'];
2476
	}
2477
	if ($gateway_settings['losslow']) {
2478
		$gateway['losslow'] = $gateway_settings['losslow'];
2479
	}
2480
	if ($gateway_settings['losshigh']) {
2481
		$gateway['losshigh'] = $gateway_settings['losshigh'];
2482
	}
2483
	if ($gateway_settings['loss_interval']) {
2484
		$gateway['loss_interval'] = $gateway_settings['loss_interval'];
2485
	}
2486

    
2487
	/* reload IPsec and OpenVPN on gateway IP, 'Mark Gateway as Down', or 'Disabled' option change
2488
	 * see https://redmine.pfsense.org/issues/13076 */
2489
	if (!empty($a_gateway_item[$realid]) &&
2490
	    (isset($gateway['disabled']) ^ isset($a_gateway_item[$realid]['disabled'])) ||
2491
	    (!isset($a_gateway_item[$realid]['disabled']) &&
2492
	    ((($gateway['monitor'] != "dynamic") &&
2493
	    is_ipaddr($a_gateway_item[$realid]['gateway']) &&
2494
	    ($gateway['gateway'] != $a_gateway_item[$realid]['gateway'])) ||
2495
	    (isset($gateway['force_down']) ^ isset($a_gateway_item[$realid]['force_down']))))) {
2496
		$reloadvpn = true;
2497
	}
2498

    
2499
	/* when saving the manual gateway we use the attribute which has the corresponding id */
2500
	if (isset($realid) && $a_gateway_item[$realid]) {
2501
		$preserve_disabled = isset($a_gateway_item[$realid]['disabled']);
2502
		$a_gateway_item[$realid] = $gateway;
2503
		if ($preserve_disabled) {
2504
			$a_gateway_item[$realid]['disabled'] = true;
2505
		}
2506
	} else {
2507
		$a_gateway_item[] = $gateway;
2508
	}
2509
	gateway_set_enabled($gateway_settings['name'], !isset($gateway_settings['disabled']));
2510

    
2511
	mark_subsystem_dirty('staticroutes');
2512

    
2513
	write_config("Gateway settings changed");
2514

    
2515
	if ($reloadvpn) {
2516
		send_event("service reload ipsec " . escapeshellarg($gateway['name']));
2517
		send_event("service reload openvpn " . escapeshellarg($gateway['name']));
2518
	}
2519

    
2520
	if (!empty($reloadif)) {
2521
		send_event("interface reconfigure {$reloadif}");
2522
	}
2523
}
2524

    
2525
function gateway_set_enabled($name, $enabled) {
2526
	$gwlist = config_get_path('gateways/gateway_item', []);
2527
	if (is_array($gwlist)) {
2528
		foreach($gwlist as $key => $item) {
2529
			if ($item['name'] == $name) {
2530
				$gateway = &$gwlist[$key];
2531
			}
2532
		}
2533
	}
2534
	if (!isset($gateway)) {
2535
		return;
2536
	}
2537
	if ($enabled) {
2538
		unset($gateway['disabled']);
2539
	} else {
2540
		/* Check if the gateway was enabled but changed to disabled. */
2541
		if (!isset($gateway['disabled'])) {
2542
			/*  If the disabled gateway was the default route, remove the default route */
2543
			if (is_ipaddr($gateway['gateway'])) {
2544
				$inet = (!is_ipaddrv4($gateway['gateway']) ? 'inet6' : 'inet');
2545
				if ($inet == 'inet') {
2546
					$cgw = route_get_default('inet');
2547
				} else {
2548
					$cgw = route_get_default('inet6');
2549
				}
2550
				if ($gateway['gateway'] == $cgw) {
2551
					route_del("default", $inet);
2552
				}
2553
			}
2554
			$gateway['disabled'] = true;
2555
		}
2556
	}
2557
	unset($gateway);
2558
	config_set_path('gateways/gateway_item', $gwlist);
2559
}
2560

    
2561
function gateway_or_gwgroup_exists($name) {
2562
	global $config;
2563
	if (is_array($config['gateways']['gateway_item'])) {
2564
		foreach(config_get_path('gateways/gateway_item', []) as $item) {
2565
			if ($item['name'] == $name) {
2566
				return true;
2567
			}
2568
		}
2569
	}
2570
	if (is_array($config['gateways']['gateway_group'])) {
2571
		foreach(config_get_path('gateways/gateway_group', []) as $item) {
2572
			if ($item['name'] == $name) {
2573
				return true;
2574
			}
2575
		}
2576
	}
2577
	return false;
2578
}
2579

    
2580
// These two replacement functions avoid the need to call return_gateways_array() multiple times and
2581
// allow system_gateways.php to get everything it needs in a single function call
2582
function gateway_getgwtiername($gways, $idx) {
2583
	global $config;
2584

    
2585
	$result = "";
2586
	$gwname = $gways[$idx]['name'];
2587

    
2588
	$gw = get_gateway_or_group_by_name($gwname, $gways);
2589

    
2590
	init_config_arr(array('gateways'));
2591
	$gw4 = config_get_path('gateways/defaultgw4', "");
2592
	$gw6 = config_get_path('gateways/defaultgw6', "");
2593
	if ($gw4 == $gwname || $gw6 == $gwname) {
2594
		$result = "Default";
2595
	} else {
2596
		if ($gw['ipprotocol'] == 'inet') {
2597
			$defgw = get_gateway_or_group_by_name($gw4, $gways);
2598
		} else {
2599
			$defgw = get_gateway_or_group_by_name($gw6, $gways);
2600
		}
2601

    
2602
		if ($defgw['type'] == "gatewaygroup") {
2603
			$detail = gateway_is_gwgroup_member($gwname, true);
2604
			foreach($detail as $gwitem) {
2605
				if ($gwitem['name'] == $defgw['name']) {
2606
					if (isset($gwitem['tier'])) {
2607
						$result = "Tier " . $gwitem['tier'];
2608
						break;
2609
					}
2610
				}
2611
			}
2612
		}
2613
    }
2614

    
2615
	if (!empty($result)) {
2616
		if ($gw['ipprotocol'] == "inet") {
2617
			$result .= " (IPv4)";
2618
		} elseif ($gw['ipprotocol'] == "inet6") {
2619
			$result .= " (IPv6)";
2620
		}
2621
	}
2622

    
2623
	return $result;
2624
}
2625

    
2626
function get_gateway_or_group_by_name($gwname, $gateways_arr) {
2627
	global $config;
2628

    
2629
	foreach ($gateways_arr as $gw) {
2630
		if ($gw['name'] == $gwname) {
2631
			$gw['type'] = 'gateway';
2632
			return $gw;
2633
		}
2634
	}
2635

    
2636
	init_config_arr(array('gateways', 'gateway_group'));
2637
	foreach (config_get_path('gateways/gateway_group', []) as $gwg) {
2638
		if ($gwg['name'] == $gwname) {
2639
			$gwg['type'] = 'gatewaygroup';
2640
			return $gwg;
2641
		}
2642
	}
2643

    
2644
	return false;
2645
}
2646

    
2647
// Compose a list of available gateways but without the need to call return_gateways_array() multiple times
2648
// Previously that function could be called eight times per gateway!
2649
function available_default_gateways() {
2650
	global $config;
2651

    
2652
	$gways = get_gateways(GW_CACHE_DISABLED | GW_CACHE_INACTIVE);
2653

    
2654
	$items4 = array();
2655
	$items6 = array();
2656
	$items4[''] = "Automatic";
2657
	$items6[''] = "Automatic";
2658
	foreach($gways as $gw) {
2659
		$gwn = $gw['name'];
2660
		if ($gw['ipprotocol'] == "inet6") {
2661
			$items6[$gwn] = $gwn;
2662
		} else {
2663
			$items4[$gwn] = $gwn;
2664
		}
2665
	}
2666

    
2667
	$groups = return_gateway_groups_array(false);
2668
	foreach ($groups as $key => $group) {
2669
		$gwn = $group['descr'];
2670
		if ($group['ipprotocol'] == "inet6") {
2671
			$items6[$key] = "$key ($gwn)";
2672
		} else {
2673
			$items4[$key] = "$key ($gwn)";
2674
		}
2675
	}
2676
	$items4['-'] = "None";
2677
	$items6['-'] = "None";
2678

    
2679
	$defaults = array();
2680
	$defaults['v4'] = $items4;
2681
	$defaults['v6'] = $items6;
2682
	$defaults['defaultgw4'] = config_get_path('gateways/defaultgw4', "");
2683
	$defaults['defaultgw6'] = config_get_path('gateways/defaultgw6', "");
2684

    
2685
	return $defaults;
2686
}
2687

    
2688
function get_interface_gateway_name($friendlyiface, $family = 'inet') {
2689
	/* Try to locate gateway directly (e.g. static) */
2690
	if ($family == 'inet') {
2691
		$gw = config_get_path("interfaces/{$friendlyiface}/gateway", '');
2692
	} else {
2693
		$gw = config_get_path("interfaces/{$friendlyiface}/gatewayv6", '');
2694
	}
2695

    
2696
	/* If the gateway is empty that means it's dynamic so we have to hunt it down */
2697
	if (empty($gw)) {
2698
		/* Check each gateway entry for a dynamic gateway matching
2699
		 * this interface and the requested address family.
2700
		 */
2701
		foreach (get_gateways(GW_CACHE_ALL) as $gwname => $gwcfg) {
2702
			if (empty($gwcfg) ||
2703
			    !is_array($gwcfg) ||
2704
			    ($gwcfg['dynamic'] == false) ||
2705
			    ($gwcfg['friendlyiface'] != $friendlyiface) ||
2706
			    ($gwcfg['ipprotocol'] != $family)) {
2707
				continue;
2708
			}
2709
			$gw = $gwname;
2710
			break;
2711
		}
2712
	}
2713
	return $gw;
2714
}
2715

    
2716
?>
(21-21/61)