Project

General

Profile

Download (75.5 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-2022 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

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

    
37
/* Returns an array of default values used for dpinger */
38
function return_dpinger_defaults() {
39
	return array(
40
		"latencylow" => "200",
41
		"latencyhigh" => "500",
42
		"losslow" => "10",
43
		"losshigh" => "20",
44
		"interval" => "500",
45
		"loss_interval" => "2000",
46
		"time_period" => "60000",
47
		"alert_interval" => "1000",
48
		"data_payload" => "1");
49
}
50

    
51
function running_dpinger_processes() {
52
	global $g;
53

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

    
56
	$result = array();
57
	if ($pidfiles === FALSE) {
58
		return $result;
59
	}
60

    
61
	foreach ($pidfiles as $pidfile) {
62
		if (preg_match('/^dpinger_(.+)~([^~]+)~([^~]+)\.pid$/',
63
		    basename($pidfile), $matches)) {
64
			$socket_file = preg_replace('/\.pid$/', '.sock',
65
			    $pidfile);
66
			$result[$matches[1]] = array(
67
			    'srcip'    => $matches[2],
68
			    'targetip' => $matches[3],
69
			    'pidfile'  => $pidfile,
70
			    'socket'   => $socket_file
71
			);
72
			unset($gwinfo);
73
		}
74
	}
75

    
76
	return $result;
77
}
78

    
79
/*
80
 * Stop one or more dpinger process
81
 * default parameter $gwname is '*' that will kill all running sessions
82
 * If a gateway name is passed, only this one will be killed
83
 */
84
function stop_dpinger($gwname = '') {
85
	global $g;
86

    
87
	$running_processes = running_dpinger_processes();
88

    
89
	foreach ($running_processes as $running_gwname => $process) {
90
		if ($gwname != '' && $running_gwname != $gwname) {
91
			continue;
92
		}
93

    
94
		if (isvalidpid($process['pidfile'])) {
95
			killbypid($process['pidfile'], 3);
96
		} else {
97
			@unlink($process['pidfile']);
98
		}
99
	}
100
}
101

    
102
function start_dpinger($gateway) {
103
	global $g;
104

    
105
	if (!isset($gateway['gwifip'])) {
106
		return (false);
107
	}
108

    
109
	$dpinger_defaults = return_dpinger_defaults();
110

    
111
	$prefix = "{$g['varrun_path']}/dpinger_{$gateway['name']}~" .
112
	    "{$gateway['gwifip']}~{$gateway['monitor']}";
113
	# dpinger socket path should not be longer then uaddr.sun_path
114
	if (strlen($prefix) > 95) {
115
		$prefix = "{$g['varrun_path']}/dpinger_{$gateway['name']}~" .
116
		    substr(md5($gateway['gwifip']),0,8) . "~" .
117
		    $gateway['monitor'];
118
	}
119
	$pidfile = $prefix . ".pid";
120
	$socket = $prefix . ".sock";
121
	$alarm_cmd = "{$g['etc_path']}/rc.gateway_alarm";
122

    
123
	$params  = "-S ";			/* Log warnings via syslog */
124
	$params .= "-r 0 ";			/* Disable unused reporting thread */
125
	$params .= "-i {$gateway['name']} ";	/* Identifier */
126
	$params .= "-B {$gateway['gwifip']} ";	/* Bind src address */
127
	$params .= "-p {$pidfile} ";		/* PID filename */
128
	$params .= "-u {$socket} ";		/* Status Socket */
129
	if (!$gateway['action_disable']) {
130
		$params .= "-C \"{$alarm_cmd}\" ";	/* Command to run on alarm */
131
	}
132

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

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

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

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

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

    
163
	$params .= "-D " .
164
	    (isset($gateway['latencyhigh']) && is_numeric($gateway['latencyhigh'])
165
	    ?  $gateway['latencyhigh']
166
	    : $dpinger_defaults['latencyhigh']
167
	    ) . " ";
168

    
169
	$params .= "-L " .
170
	    (isset($gateway['losshigh']) && is_numeric($gateway['losshigh'])
171
	    ?  $gateway['losshigh']
172
	    : $dpinger_defaults['losshigh']
173
	    ) . " ";
174

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

    
178
	/* Do not try to bind IPv6 where interface is in tentative state */
179
	if (is_ipaddrv6($gateway['gwifip'])) {
180
		$err = interface_wait_tentative(get_real_interface(
181
		    $gateway['interface']));
182
		if ($err == false) {
183
			log_error(gettext("Timeout waiting for IPv6 address in tentative state.  dpinger will not run."));
184
			return (false);
185
		}
186
	}
187

    
188
	/* Redirect stdout to /dev/null to avoid exec() to wait for dpinger */
189
	return mwexec("/usr/local/bin/dpinger {$params} {$gateway['monitor']} >/dev/null");
190
}
191

    
192
/*
193
 * Starts dpinger processes and adds appropriate static routes for monitor IPs
194
 */
195
function setup_gateways_monitor() {
196
	global $config, $g;
197

    
198
	$gateways_arr = return_gateways_array();
199
	if (!is_array($gateways_arr)) {
200
		log_error(gettext("No gateways to monitor. dpinger will not run."));
201
		stop_dpinger();
202
		return;
203
	}
204
	if (platform_booting()) {
205
		echo "Setting up gateway monitors...";
206
	}
207
	$monitor_ips = array();
208
	foreach ($gateways_arr as $gwname => $gateway) {
209
		/* Do not monitor if such was requested */
210
		if (isset($gateway['monitor_disable'])) {
211
			continue;
212
		}
213
		if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
214
			if (is_ipaddr($gateway['gateway'])) {
215
				$gateways_arr[$gwname]['monitor'] = $gateway['gateway'];
216
			} else { /* No chance to get an ip to monitor skip target. */
217
				continue;
218
			}
219
		}
220

    
221
		/* if the monitor address is already used before, skip */
222
		if (in_array($gateway['monitor'], $monitor_ips)) {
223
			continue;
224
		}
225

    
226
		/* Interface ip is needed since dpinger will bind a socket to it.
227
		 * However the config GUI should already have checked this and when
228
		 * PPPoE is used the IP address is set to "dynamic". So using is_ipaddrv4
229
		 * or is_ipaddrv6 to identify packet type would be wrong, especially as
230
		 * further checks (that can cope with the "dynamic" case) are present inside
231
		 * the if block. So using $gateway['ipprotocol'] is the better option.
232
		 */
233
		if ($gateway['ipprotocol'] == "inet") { // This is an IPv4 gateway...
234
			$gwifip = find_interface_ip($gateway['interface'], true);
235
			if (!is_ipaddrv4($gwifip)) {
236
				continue; //Skip this target
237
			}
238

    
239
			if ($gwifip == "0.0.0.0") {
240
				continue; //Skip this target - the gateway is still waiting for DHCP
241
			}
242

    
243
			/*
244
			 * If the gateway is the same as the monitor we do not add a
245
			 * route as this will break the routing table.
246
			 * Add static routes for each gateway with their monitor IP
247
			 * not strictly necessary but is a added level of protection.
248
			 */
249
			if (!isset($config['system']['dpinger_dont_add_static_routes']) &&
250
					!isset($gateway['dpinger_dont_add_static_route'])) {
251
				if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
252
					log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
253
					if (interface_isppp_type($gateway['friendlyiface'])) {
254
						route_add_or_change($gateway['monitor'],
255
						    '', $gateway['interface']);
256
						system_staticroutes_configure($gateway['friendlyiface']);
257
					} else {
258
						route_add_or_change($gateway['monitor'],
259
						    $gateway['gateway']);
260
					}
261

    
262
					pfSense_kill_states("0.0.0.0/0", utf8_encode($gateway['monitor']), utf8_encode($gateway['interface']), "icmp");
263
				}
264
			}
265
		} else if ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
266
			if (is_linklocal($gateway['gateway']) &&
267
			    get_ll_scope($gateway['gateway']) == '') {
268
				$gateway['gateway'] .= '%' . $gateway['interface'];
269
			}
270

    
271
			if (is_linklocal($gateway['monitor'])) {
272
				if (get_ll_scope($gateway['monitor']) == '') {
273
					$gateways_arr[$gwname]['monitor'] .= '%' . $gateway['interface'];
274
				}
275

    
276
				$gwifip = find_interface_ipv6_ll($gateway['interface'], true);
277

    
278
				if (get_ll_scope($gwifip) == '') {
279
					$gwifip .= '%' . $gateway['interface'];
280
				}
281
			} else {
282
				$gwifip = find_interface_ipv6($gateway['interface'], true);
283
			}
284

    
285
			if (!is_ipaddrv6($gwifip)) {
286
				continue; //Skip this target
287
			}
288

    
289
			/*
290
			 * If the gateway is the same as the monitor we do not add a
291
			 * route as this will break the routing table.
292
			 * Add static routes for each gateway with their monitor IP
293
			 * not strictly necessary but is a added level of protection.
294
			 */
295

    
296
			if (!isset($config['system']['dpinger_dont_add_static_routes']) &&
297
					!isset($gateway['dpinger_dont_add_static_route'])) {
298
				if ($gateway['gateway'] != $gateways_arr[$gwname]['monitor']) {
299
					log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
300
					if (interface_isppp_type($gateway['friendlyiface'])) {
301
						route_add_or_change($gateway['monitor'],
302
						    '', $gateway['interface']);
303
						system_staticroutes_configure($gateway['friendlyiface']);
304
					} else {
305
						route_add_or_change($gateway['monitor'],
306
						    $gateway['gateway']);
307
					}
308

    
309
					pfSense_kill_states("::0.0.0.0/0", utf8_encode($gateway['monitor']), utf8_encode($gateway['interface']), "icmpv6");
310
				}
311
			}
312
		} else {
313
			continue;
314
		}
315

    
316
		$monitor_ips[] = $gateway['monitor'];
317
		$gateways_arr[$gwname]['enable_dpinger'] = true;
318
		$gateways_arr[$gwname]['gwifip'] = $gwifip;
319
	}
320

    
321
	stop_dpinger();
322

    
323
	/* Start new processes */
324
	foreach ($gateways_arr as $gateway) {
325
		if (!isset($gateway['enable_dpinger'])) {
326
			continue;
327
		}
328

    
329
		if (start_dpinger($gateway) != 0) {
330
			log_error(sprintf(gettext("Error starting gateway monitor for %s"), $gateway['name']));
331
		}
332
	}
333
	if (platform_booting()) {
334
		echo "done.\n";
335
	}
336

    
337
	return;
338
}
339

    
340
function get_dpinger_status($gwname, $gways = false, $action_disable = false) {
341
	global $g;
342

    
343
	$running_processes = running_dpinger_processes();
344

    
345
	if (!isset($running_processes[$gwname])) {
346
		log_error(sprintf(gettext(
347
		    'dpinger: No dpinger session running for gateway %s'),
348
		    $gwname));
349
		return false;
350
	}
351

    
352
	$proc = $running_processes[$gwname];
353
	unset($running_processes);
354

    
355
	$timeoutcounter = 0;
356
	while (true) {
357
		if (!file_exists($proc['socket'])) {
358
			log_error("dpinger: status socket {$proc['socket']} not found");
359
			return false;
360
		}
361
		$fp = @stream_socket_client("unix://{$proc['socket']}", $errno, $errstr, 10);
362
		if (!$fp) {
363
			log_error(sprintf(gettext('dpinger: cannot connect to status socket %1$s - %2$s (%3$s)'), $proc['socket'], $errstr, $errno));
364
			return false;
365
		}
366

    
367
		$status = '';
368
		while (!feof($fp)) {
369
			$status .= fgets($fp, 1024);
370
		}
371
		fclose($fp);
372

    
373
		$r = array();
374
		list(
375
			$r['gwname'],
376
			$r['latency_avg'],
377
			$r['latency_stddev'],
378
			$r['loss']
379
		) = explode(' ', preg_replace('/\n/', '', $status));
380

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

    
386
		if ($ready) {
387
			break;
388
		} else {
389
			$timeoutcounter++;
390
			if ($timeoutcounter > 300) {
391
				log_error(sprintf(gettext('dpinger: timeout while retrieving status for gateway %s'), $gwname));
392
				return false;
393
			}
394
			usleep(10000);
395
		}
396
	}
397

    
398
	$r['srcip'] = $proc['srcip'];
399
	$r['targetip'] = $proc['targetip'];
400

    
401
	if (is_array($gways)) {
402
		$gateways_arr = $gways;
403
	} else {
404
		$gateways_arr = return_gateways_array();
405
	}
406

    
407
	unset($gw);
408
	if (isset($gateways_arr[$gwname])) {
409
		$gw = $gateways_arr[$gwname];
410
	}
411

    
412
	$r['latency_avg'] = round($r['latency_avg']/1000, 3);
413
	$r['latency_stddev'] = round($r['latency_stddev']/1000, 3);
414

    
415
	$r['status'] = "online";
416
	$r['substatus'] = "none";
417
	if (isset($gw) && isset($gw['force_down'])) {
418
		$r['status'] = "down";
419
		$r['substatus'] = "force_down";
420
	} else if (isset($gw)) {
421
		$settings = return_dpinger_defaults();
422

    
423
		$keys = array(
424
		    'latencylow',
425
		    'latencyhigh',
426
		    'losslow',
427
		    'losshigh'
428
		);
429

    
430
		/* Replace default values by user-defined */
431
		foreach ($keys as $key) {
432
			if (isset($gw[$key]) && is_numeric($gw[$key])) {
433
				$settings[$key] = $gw[$key];
434
			}
435
		}
436

    
437
		if ($r['latency_avg'] > $settings['latencyhigh']) {
438
			if (!$action_disable) {
439
				$r['status'] = "down";
440
			}
441
			$r['substatus'] = "highdelay";
442
		} else if ($r['loss'] > $settings['losshigh']) {
443
			if (!$action_disable) {
444
				$r['status'] = "down";
445
			}
446
			$r['substatus'] = "highloss";
447
		} else if ($r['latency_avg'] > $settings['latencylow']) {
448
			$r['substatus'] = "delay";
449
		} else if ($r['loss'] > $settings['losslow']) {
450
			$r['substatus'] = "loss";
451
		}
452
	}
453

    
454
	return $r;
455
}
456

    
457
/* return the status of the dpinger targets as an array */
458
function return_gateways_status($byname = false, $gways = false) {
459
	global $config, $g;
460

    
461
	$dpinger_gws = running_dpinger_processes();
462
	$status = array();
463

    
464
	if (is_array($gways)) {
465
		$gateways_arr = $gways;
466
	} else {
467
		$gateways_arr = return_gateways_array();
468
	}
469

    
470
	foreach ($dpinger_gws as $gwname => $gwdata) {
471
		/*
472
		 * If action is disabled for this gateway, then we want a
473
		 * detailed status.  That reports "highdelay" or "highloss"
474
		 * rather than just "down".  Because reporting the gateway
475
		 * down would be misleading (gateway action is disabled)
476
		 */
477
		$action_disable = $gateways_arr[$gwname]['action_disable'];
478
		$dpinger_status = get_dpinger_status($gwname, $action_disable);
479
		if ($dpinger_status === false) {
480
			continue;
481
		}
482

    
483
		if ($byname == false) {
484
			$target = $dpinger_status['targetip'];
485
		} else {
486
			$target = $gwname;
487
		}
488

    
489
		$status[$target] = array();
490
		$status[$target]['monitorip'] = $dpinger_status['targetip'];
491
		$status[$target]['srcip'] = $dpinger_status['srcip'];
492
		$status[$target]['name'] = $gwname;
493
		$status[$target]['delay'] =
494
		    empty($dpinger_status['latency_avg'])
495
		    ? "0ms"
496
		    : $dpinger_status['latency_avg'] . "ms";
497
		$status[$target]['stddev'] =
498
		    empty($dpinger_status['latency_stddev'])
499
		    ? "0ms"
500
		    : $dpinger_status['latency_stddev'] . "ms";
501
		$status[$target]['loss'] = empty($dpinger_status['loss'])
502
		    ? "0.0%"
503
		    : round($dpinger_status['loss'], 1) . "%";
504
		$status[$target]['status'] = $dpinger_status['status'];
505
		$status[$target]['substatus'] = $dpinger_status['substatus'];
506
	}
507

    
508
	/* tack on any gateways that have monitoring disabled
509
	 * or are down, which could cause gateway groups to fail */
510
	$gateways_arr = return_gateways_array();
511
	foreach ($gateways_arr as $gwitem) {
512
		if (!isset($gwitem['monitor_disable'])) {
513
			continue;
514
		}
515
		if (!is_ipaddr($gwitem['monitor'])) {
516
			$realif = $gwitem['interface'];
517
			$tgtip = get_interface_gateway($realif);
518
			if (!is_ipaddr($tgtip)) {
519
				$tgtip = "none";
520
			}
521
			$srcip = find_interface_ip($realif);
522
		} else {
523
			$tgtip = $gwitem['monitor'];
524
			$srcip = find_interface_ip($realif);
525
		}
526
		if ($byname == true) {
527
			$target = $gwitem['name'];
528
		} else {
529
			$target = $tgtip;
530
		}
531

    
532
		/* failsafe for down interfaces */
533
		if ($target == "none") {
534
			$target = $gwitem['name'];
535
			$status[$target]['name'] = $gwitem['name'];
536
			$status[$target]['delay'] = "0.0ms";
537
			$status[$target]['loss'] = "100.0%";
538
			$status[$target]['status'] = "down";
539
			$status[$target]['substatus'] = "down";
540
		} else {
541
			$status[$target]['monitorip'] = $tgtip;
542
			$status[$target]['srcip'] = $srcip;
543
			$status[$target]['name'] = $gwitem['name'];
544
			$status[$target]['delay'] = "";
545
			$status[$target]['loss'] = "";
546
			$status[$target]['status'] = "online";
547
			$status[$target]['substatus'] = "none";
548
		}
549

    
550
		$status[$target]['monitor_disable'] = true;
551
	}
552
	return($status);
553
}
554

    
555
function return_gateways_status_text($byname = false, $brief = false) {
556
	$gwstat = return_gateways_status($byname);
557
	$output = "";
558
	$widths = array();
559
	$col_sep = 2;
560
	if ($brief) {
561
		$collist = array('status' => "Status");
562
	} else {
563
		$collist = array('monitorip' => "Monitor",
564
				'srcip' => "Source",
565
				'delay' => "Delay",
566
				'stddev' => "StdDev",
567
				'loss' => "Loss",
568
				'status' => "Status",
569
				'substatus' => "Substatus");
570
	}
571
	foreach ($gwstat as $gw) {
572
		foreach ($gw as $gwdidx => $gwdata) {
573
			if (strlen($gwdata) > $widths[$gwdidx]) {
574
				$widths[$gwdidx] = strlen($gwdata);
575
			}
576
		}
577
	}
578

    
579
	$output .= str_pad("Name", $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
580
	foreach ($collist as $hdrcol => $hdrdesc) {
581
		if (strlen($hdrdesc) > $widths[$hdrcol]) {
582
			$widths[$hdrcol] = strlen($hdrdesc);
583
		}
584
		$output .= str_pad($hdrdesc, $widths[$hdrcol] + $col_sep, " ", (substr($hdrcol, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
585
	}
586
	$output .= "\n";
587

    
588
	foreach ($gwstat as $idx => $gw) {
589
		$output .= str_pad($gw['name'], $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
590
		foreach (array_keys($collist) as $col) {
591
			$output .= str_pad($gw[$col], $widths[$col] + $col_sep, " ", (substr($col, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
592
		}
593
		$output .= "\n";
594
	}
595

    
596
	return $output;
597
}
598

    
599
function compare_gateway_order_configured($a, $b) {
600
	/* XXX WAN always has precedence */
601
	if ($a['friendlyiface'] == "wan") {
602
		return -1;
603
	} elseif ($b['friendlyiface'] == "wan") {
604
		return 1;
605
	}
606

    
607
	if ($a['attribute'] === $b['attribute']) {
608
		if ($a['attribute'] === 'system') {
609
			$res = (($a['name'] < $b['name'])) ? -1 : 1;
610
			return $res;
611
		}
612
		return 0;
613
	}
614
	if ($a['attribute'] === 'system' || $b['attribute'] === 'system') {
615
		$res = (($b['attribute'] === 'system')) ? -1 : 1;
616
		return $res;
617
	}
618
	$res = ($a['attribute'] < $b['attribute']) ? -1 : 1;
619
	return $res;
620
}
621

    
622
function order_gateways_as_configured($gateways_arr) {
623
	uasort($gateways_arr, 'compare_gateway_order_configured');
624
	return $gateways_arr;
625
}
626

    
627
/* Return all configured gateways on the system
628
   $disabled = true - include gateways that are disabled
629
   $localhost = true - include "Null" entries for localhost IP addresses
630
   $inactive = true - include gateways on inactive interfaces
631
   $integer_index = true - index the returned array by integers 0,1,2,... instead of by GW name
632
*/
633
function return_gateways_array($disabled = false, $localhost = false, $inactive = false, $integer_index = false) {
634
	global $config, $g;
635

    
636
	$gateways_arr = array();
637
	$gateways_arr_temp = array();
638
	$cgw4 = route_get_default('inet');
639
	$cgw6 = route_get_default('inet6');
640
	$found_defaultv4 = 0;
641
	$found_defaultv6 = 0;
642

    
643
	// Ensure the interface cache is up to date first
644
	$interfaces = get_interface_arr(true);
645

    
646
	$i = -1;
647
	/* Process/add all the configured gateways. */
648
	if (is_array($config['gateways']) && is_array($config['gateways']['gateway_item'])) {
649
		foreach ($config['gateways']['gateway_item'] as $gateway) {
650
			if (!is_array($gateway) || empty($gateway)) {
651
				continue;
652
			}
653

    
654
			/* Increment it here to do not skip items */
655
			$i++;
656
			if (isset($gateway['defaultgw'])) {
657
				unset($gateway['defaultgw']);
658
			}
659

    
660
			if (empty($config['interfaces'][$gateway['interface']])) {
661
				if ($inactive === false) {
662
					continue;
663
				} else {
664
					$gateway['inactive'] = true;
665
				}
666
			}
667
			$wancfg = $config['interfaces'][$gateway['interface']];
668

    
669
			/* skip disabled interfaces */
670
			if ($disabled === false && (!isset($wancfg['enable']))) {
671
				continue;
672
			}
673

    
674
			/* if the gateway is dynamic and we can find the IPv4, Great! */
675
			if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
676
				if ($gateway['ipprotocol'] == "inet") {
677
					/* we know which interfaces is dynamic, this should be made a function */
678
					$gateway['gateway'] = get_interface_gateway($gateway['interface']);
679
					/* no IP address found, set to dynamic */
680
					if (!is_ipaddrv4($gateway['gateway'])) {
681
						$gateway['gateway'] = "dynamic";
682
					}
683
					$gateway['dynamic'] = true;
684
				}
685

    
686
				/* if the gateway is dynamic and we can find the IPv6, Great! */
687
				else if ($gateway['ipprotocol'] == "inet6") {
688
					/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
689
					$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
690
					/* no IPv6 address found, set to dynamic */
691
					if (!is_ipaddrv6($gateway['gateway'])) {
692
						$gateway['gateway'] = "dynamic";
693
					}
694
					$gateway['dynamic'] = true;
695
				}
696
			} else {
697
				/* getting this detection right is hard at this point because we still don't
698
				 * store the address family in the gateway item */
699
				if (is_ipaddrv4($gateway['gateway'])) {
700
					$gateway['ipprotocol'] = "inet";
701
				} else if (is_ipaddrv6($gateway['gateway'])) {
702
					$gateway['ipprotocol'] = "inet6";
703
				}
704
			}
705

    
706
			if (isset($gateway['monitor_disable'])) {
707
				$gateway['monitor_disable'] = true;
708
			} else if (empty($gateway['monitor'])) {
709
				$gateway['monitor'] = $gateway['gateway'];
710
			}
711

    
712
			if (isset($gateway['action_disable'])) {
713
				$gateway['action_disable'] = true;
714
			}
715

    
716
			$gateway['friendlyiface'] = $gateway['interface'];
717
			$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($gateway['interface']);
718

    
719
			/* special treatment for tunnel interfaces */
720
			if ($gateway['ipprotocol'] == "inet6") {
721
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
722
			} else {
723
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet", false, false);
724
			}
725

    
726
			if ($gateway['ipprotocol'] == "inet" &&
727
					($gateway['gateway'] == $cgw4)) {
728
				$gateway['isdefaultgw'] = true;
729
				$found_defaultv4 = 1;
730
			} else if ($gateway['ipprotocol'] == "inet6" &&
731
					($gateway['gateway'] == $cgw6)) {
732
				$gateway['isdefaultgw'] = true;
733
				$found_defaultv6 = 1;
734
			}
735
			/* include the gateway index as the attribute */
736
			$gateway['attribute'] = $i;
737

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

    
742
			/* skip disabled gateways if the caller has not asked for them to be returned. */
743
			if (!($disabled === false && isset($gateway['disabled']))) {
744
				$gateways_arr[$gateway['name']] = $gateway;
745
			}
746
		}
747
	}
748
	unset($gateway);
749

    
750
	//Sort the array by GW name before moving on.
751
	ksort($gateways_arr, SORT_STRING | SORT_FLAG_CASE);
752

    
753
	/* Loop through all interfaces with a gateway and add it to a array */
754
	if ($disabled == false) {
755
		$iflist = get_configured_interface_with_descr();
756
	} else {
757
		$iflist = get_configured_interface_with_descr(true);
758
	}
759

    
760
	/* Process/add dynamic v4 gateways. */
761
	foreach ($iflist as $ifname => $friendly) {
762
		if (!interface_has_gateway($ifname)) {
763
			continue;
764
		}
765

    
766
		if (empty($config['interfaces'][$ifname])) {
767
			continue;
768
		}
769

    
770
		$ifcfg = &$config['interfaces'][$ifname];
771
		if (!isset($ifcfg['enable'])) {
772
			continue;
773
		}
774

    
775
		if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
776
			continue;
777
		}
778

    
779
		$ctype = "";
780
		switch ($ifcfg['ipaddr']) {
781
			case "dhcp":
782
			case "pppoe":
783
			case "l2tp":
784
			case "pptp":
785
			case "ppp":
786
				$ctype = strtoupper($ifcfg['ipaddr']);
787
				break;
788
			default:
789
				$tunnelif = substr($ifcfg['if'], 0, 3);
790
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
791
					switch (substr($ifcfg['if'], 4, 1)) {
792
						case "c":
793
							$ovpntype = "openvpn-client";
794
							break;
795
						case "s":
796
							$ovpntype = "openvpn-server";
797
							break;
798
						default:
799
							// unknown ovpn type
800
							continue 3;
801
					}
802
					$ovpnid = substr($ifcfg['if'], 5);
803
					if (is_array($config['openvpn'][$ovpntype])) {
804
						foreach ($config['openvpn'][$ovpntype] as & $ovpnconf) {
805
							if ($ovpnconf['vpnid'] == $ovpnid) {
806
								// skip IPv6-only interfaces
807
								if ($ovpnconf['create_gw'] == "v6only") {
808
									continue 3;
809
								}
810
								// skip tap interfaces
811
								if ($ovpnconf['dev_mode'] == "tap") {
812
									continue 3;
813
								}
814
							}
815
						}
816
					}
817
					$ctype = "VPNv4";
818
				} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
819
					$ikeid = substr($ifcfg['if'], 5);
820
					if (is_array($config['ipsec']) && is_array($config['ipsec']['phase1']) && is_array($config['ipsec']['phase2'])) {
821
						foreach ($config['ipsec']['phase1'] as $ph1ent) {
822
							if ($ph1ent['disabled']) {
823
								continue;
824
							}
825
							$vtisubnet_spec = ipsec_vti($ph1ent, true);
826
							// Skip non-VTI tunnels
827
							if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
828
								continue;
829
							}
830
							if (!isset($ph1ent['mobile']) && ($keyexchange == 'ikev1' || isset($ph1ent['splitconn']))) {
831
								foreach ($vtisubnet_spec as $idx => $vtisub) {
832
									if ($ifcfg['if'] == ipsec_get_ifname($ph1ent, $vtisub['reqid'])) {
833
										// If this specific VTI remote is v4, then we can make a v4 gw
834
										if (is_ipaddrv4($vtisub['right'])) {
835
											$ctype = "VTIv4";
836
										}
837
									}
838
								}
839
							} else {
840
								if ($ifcfg['if'] == ipsec_get_ifname($ph1ent)) {
841
									// If any of the VTI remotes are v4, then we can make a v4 gw
842
									foreach ($vtisubnet_spec as $vtisub) {
843
										if (is_ipaddrv4($vtisub['right'])) {
844
											$ctype = "VTIv4";
845
										}
846
									}
847
								}
848
							}
849
						}
850
						if (empty($ctype)) {
851
							continue 2;
852
						}
853
					}
854
				} elseif ($tunnelif == "gif" || $tunnelif == "gre") {
855
					$ctype = "TUNNELv4";
856
				}
857
				break;
858
		}
859
		$ctype = "_". strtoupper($ctype);
860

    
861
		$gateway = array();
862
		$gateway['dynamic'] = false;
863
		$gateway['ipprotocol'] = "inet";
864
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
865
		$gateway['interface'] = get_real_interface($ifname);
866
		$gateway['friendlyiface'] = $ifname;
867
		$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
868
		$gateway['name'] = "{$friendly}{$ctype}";
869
		$gateway['attribute'] = "system";
870

    
871
		if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
872
			$gateway['isdefaultgw'] = true;
873
			$gateway['dynamic'] = true;
874
			$found_defaultv4 = 1;
875
		}
876

    
877
		/* Loopback dummy for dynamic interfaces without a IP */
878
		if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
879
			$gateway['gateway'] = "dynamic";
880
		}
881

    
882
		/* automatically skip known static and dynamic gateways that were previously processed */
883
		foreach ($gateways_arr_temp as $gateway_item) {
884
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
885
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
886
				continue 2;
887
			}
888
		}
889

    
890
		if (is_ipaddrv4($gateway['gateway'])) {
891
			$gateway['monitor'] = $gateway['gateway'];
892
		}
893

    
894
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
895
		$gateways_arr[$gateway['name']] = $gateway;
896
	}
897
	unset($gateway);
898

    
899
	/* Process/add dynamic v6 gateways. */
900
	foreach ($iflist as $ifname => $friendly) {
901
		/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
902
		if (!isset($config['system']['ipv6allow'])) {
903
			break;
904
		}
905

    
906
		if (!interface_has_gatewayv6($ifname)) {
907
			continue;
908
		}
909

    
910
		if (empty($config['interfaces'][$ifname])) {
911
			continue;
912
		}
913

    
914
		$ifcfg = &$config['interfaces'][$ifname];
915
		if (!isset($ifcfg['enable'])) {
916
			continue;
917
		}
918

    
919
		if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
920
			continue;
921
		}
922

    
923
		$ctype = "";
924
		switch ($ifcfg['ipaddrv6']) {
925
			case "slaac":
926
			case "dhcp6":
927
			case "6to4":
928
			case "6rd":
929
				$ctype = strtoupper($ifcfg['ipaddrv6']);
930
				break;
931
			default:
932
				$tunnelif = substr($ifcfg['if'], 0, 3);
933
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
934
					switch (substr($ifcfg['if'], 4, 1)) {
935
						case "c":
936
							$ovpntype = "openvpn-client";
937
							break;
938
						case "s":
939
							$ovpntype = "openvpn-server";
940
							break;
941
						default:
942
							// unknown ovpn type
943
							continue 3;
944
					}
945
					$ovpnid = substr($ifcfg['if'], 5);
946
					if (is_array($config['openvpn'][$ovpntype])) {
947
						foreach ($config['openvpn'][$ovpntype] as & $ovpnconf) {
948
							if ($ovpnconf['vpnid'] == $ovpnid) {
949
								// skip IPv4-only interfaces
950
								if ($ovpnconf['create_gw'] == "v4only") {
951
									continue 3;
952
								}
953
								// skip tap interfaces
954
								if ($ovpnconf['dev_mode'] == "tap") {
955
									continue 3;
956
								}
957
							}
958
						}
959
					}
960
					$ctype = "VPNv6";
961
				} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
962
					$ikeid = substr($ifcfg['if'], 5);
963
					if (is_array($config['ipsec']) && is_array($config['ipsec']['phase1']) && is_array($config['ipsec']['phase2'])) {
964
						foreach ($config['ipsec']['phase1'] as $ph1ent) {
965
							if ($ph1ent['disabled']) {
966
								continue;
967
							}
968
							$vtisubnet_spec = ipsec_vti($ph1ent, true);
969
							// Skip non-VTI tunnels
970
							if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
971
								continue;
972
							}
973
							if (!isset($ph1ent['mobile']) && ($keyexchange == 'ikev1' || isset($ph1ent['splitconn']))) {
974
								foreach ($vtisubnet_spec as $idx => $vtisub) {
975
									if ($ifcfg['if'] == ipsec_get_ifname($ph1ent, $vtisub['reqid'])) {
976
										// If this specific VTI remote is v6, then we can make a v6 gw
977
										if (is_ipaddrv6($vtisub['right'])) {
978
											$ctype = "VTIv6";
979
										}
980
									}
981
								}
982
							} else {
983
								if ($ifcfg['if'] == ipsec_get_ifname($ph1ent)) {
984
									// If any of the VTI remotes are v6, then we can make a v6 gw
985
									foreach ($vtisubnet_spec as $vtisub) {
986
										if (is_ipaddrv6($vtisub['right'])) {
987
											$ctype = "VTIv6";
988
										}
989
									}
990
								}
991
							}
992
						}
993
						if (empty($ctype)) {
994
							continue 2;
995
						}
996
					}
997
				} else if ($tunnelif == "gif" || $tunnelif == "gre") {
998
					$ctype = "TUNNELv6";
999
				}
1000
				break;
1001
		}
1002
		$ctype = "_". strtoupper($ctype);
1003

    
1004
		$gateway = array();
1005
		$gateway['dynamic'] = false;
1006
		$gateway['ipprotocol'] = "inet6";
1007
		$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
1008
		$gateway['interface'] = get_real_interface($ifname, "inet6");
1009
		switch ($ifcfg['ipaddrv6']) {
1010
			case "6rd":
1011
			case "6to4":
1012
				$gateway['dynamic'] = "default";
1013
				break;
1014
		}
1015
		$gateway['friendlyiface'] = $ifname;
1016
		$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
1017
		$gateway['name'] = "{$friendly}{$ctype}";
1018
		$gateway['attribute'] = "system";
1019

    
1020
		if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) {
1021
			$gateway['isdefaultgw'] = true;
1022
			$gateway['dynamic'] = true;
1023
			$found_defaultv6 = 1;
1024
		}
1025

    
1026
		/* Loopback dummy for dynamic interfaces without a IP */
1027
		if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
1028
			$gateway['gateway'] = "dynamic";
1029
		}
1030

    
1031
		/* automatically skip known static and dynamic gateways that were previously processed */
1032
		foreach ($gateways_arr_temp as $gateway_item) {
1033
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
1034
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
1035
				continue 2;
1036
			}
1037
		}
1038

    
1039
		if (is_ipaddrv6($gateway['gateway'])) {
1040
			$gateway['monitor'] = $gateway['gateway'];
1041
		}
1042

    
1043
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
1044
		$gateways_arr[$gateway['name']] = $gateway;
1045
	}
1046
	unset($gateway);
1047

    
1048
	/* FIXME: Should this be enabled.
1049
	 * Some interface like wan might be default but have no info recorded
1050
	 * the config. */
1051
	/* this is a fallback if all else fails and we want to get packets out @smos */
1052
	if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
1053
		foreach ($gateways_arr as &$gateway) {
1054
			if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
1055
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw")) {
1056
					$gateway['isdefaultgw'] = true;
1057
					$found_defaultv4 = 1;
1058
				}
1059
			}
1060
			else if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
1061
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgwv6")) {
1062
					$gateway['isdefaultgw'] = true;
1063
					$found_defaultv6 = 1;
1064
				}
1065
			}
1066
		}
1067
	}
1068

    
1069
	if ($localhost === true) {
1070
		/* attach localhost for Null routes */
1071
		$gwlo4 = array();
1072
		$gwlo4['name'] = "Null4";
1073
		$gwlo4['interface'] = "lo0";
1074
		$gwlo4['ipprotocol'] = "inet";
1075
		$gwlo4['gateway'] = "127.0.0.1";
1076
		$gwlo4['attribute'] = "system";
1077
		$gwlo6 = array();
1078
		$gwlo6['name'] = "Null6";
1079
		$gwlo6['interface'] = "lo0";
1080
		$gwlo6['ipprotocol'] = "inet6";
1081
		$gwlo6['gateway'] = "::1";
1082
		$gwlo6['attribute'] = "system";
1083
		$gateways_arr['Null4'] = $gwlo4;
1084
		$gateways_arr['Null6'] = $gwlo6;
1085
	}
1086

    
1087
	if ($integer_index) {
1088
		$gateways_arr = array_values($gateways_arr);
1089
	}
1090

    
1091
	if ($found_defaultv4 != 1 && is_ipaddr($cgw4)) {
1092
		foreach($gateways_arr as &$gw) {
1093
			if ($gw['gateway'] == $cgw4) {
1094
				$gw['isdefaultgw'] = true;
1095
			}
1096
		}
1097
	}
1098
	if ($found_defaultv6 != 1 && is_ipaddr($cgw6)) {
1099
		foreach($gateways_arr as &$gw) {
1100
			if ($gw['gateway'] == $cgw6) {
1101
				$gw['isdefaultgw'] = true;
1102
			}
1103
		}
1104
	}
1105

    
1106
	$gways = order_gateways_as_configured($gateways_arr);
1107

    
1108
	// Add the tier names here so that system_gateways.php doesn't need to
1109
	foreach ($gways as $idx => $gway) {
1110
		$gways[$idx]['tiername'] = gateway_getgwtiername($gways, $idx);
1111
	}
1112

    
1113
	return $gways;
1114
}
1115

    
1116
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) {
1117
	global $config, $g;
1118
	/*
1119
	 * NOTE: The code below is meant to replace the default gateway when it goes down.
1120
	 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
1121
	 */
1122
	$set_dfltgwname = '';
1123

    
1124
	if ($ipprotocol == 'inet') {
1125
		$gwdefault = $config['gateways']['defaultgw4'];
1126
	} else {
1127
		$gwdefault = $config['gateways']['defaultgw6'];
1128
	}
1129
	/* do not delete dynamic (frr/bgp/ospf) route
1130
	 * see https://redmine.pfsense.org/issues/12536 */
1131
	if ($gwdefault == "-") {
1132
		if (is_static_route('default', $ipprotocol)) {
1133
			route_del('default', $ipprotocol); 
1134
		}
1135
		return;
1136
	}
1137
	if (isset($gateways_arr[$gwdefault])) {
1138
		// the configured gateway is a regular one. (not a gwgroup) use it as is..
1139
		$set_dfltgwname = $gwdefault;
1140
	} elseif (empty($gwdefault)) {
1141
		// 'automatic' mode, pick the first one thats 'up' or 'unmonitored' which is always considered up
1142
		$gateways_arr = order_gateways_as_configured($gateways_arr);
1143
		$fallback = "";
1144
		foreach($gateways_arr as $gwname => $gwsttng) {
1145
			if (($gwsttng['ipprotocol'] != $ipprotocol) || isset($gwsttng['force_down'])) {
1146
				continue;
1147
			}
1148

    
1149
			if (isset($gwsttng['monitor_disable']) || isset($gwsttng['action_disable']) ||
1150
			    ($gateways_status[$gwname]['status'] == "online")) {
1151
				$set_dfltgwname = $gwname;
1152
				break;
1153
			}
1154
			if (empty($fallback) && $gwsttng['interface'] != 'lo0') {
1155
				$fallback = $gwname;
1156
			}
1157
		}
1158
		if (empty($set_dfltgwname) && !empty($fallback)) {
1159
			log_error(sprintf("Gateway, none 'available' for %s, use the first one configured. '%s'", $ipprotocol, $fallback));
1160
			$set_dfltgwname = $fallback;
1161
		} else {
1162
			log_error("Gateway, NONE AVAILABLE");
1163
		}
1164
	} else {
1165
		// a gwgroup is selected
1166
		// find the best available gateway given options available..
1167
		$gwg_members = array();
1168
		$viplist = get_configured_vip_list();
1169
		if (is_array($config['gateways']['gateway_group'])) {
1170
			foreach ($config['gateways']['gateway_group'] as $group) {
1171
				if ($group['name'] == $gwdefault) {
1172
					// finds the gw members of the best available tier for this group.
1173
					$gwg_members = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1174
				}
1175
			}
1176
		}
1177

    
1178
		if (count($gwg_members) > 0) {
1179
			$currentdefaultgwip = route_get_default($ipprotocol);
1180
			$found_current = false;
1181
			foreach($gwg_members as $gwgroupitem) {
1182
				if (!empty($currentdefaultgwip) && ($gwgroupitem['gwip'] == $currentdefaultgwip)) {
1183
					$set_dfltgwname = $gwgroupitem['gw'];
1184
					$found_current = true;
1185
					if (isset($config['system']['gw-debug'])) {
1186
						log_error("Keep current gateway, its already part of the group members.");
1187
					}
1188
					break;
1189
				}
1190
			}
1191
			if (!$found_current) {
1192
				$set_dfltgwname = $gwg_members[0]['gw'];
1193
				log_error(sprintf("Gateway, switch to: %s", $set_dfltgwname));
1194
			}
1195
		} else {
1196
			log_error("Gateway, NONE AVAILABLE");
1197
		}
1198
	}
1199
	if (!empty($set_dfltgwname) && isset($gateways_arr[$set_dfltgwname])) {
1200
		setdefaultgateway($gateways_arr[$set_dfltgwname]);
1201
	} elseif (empty($set_dfltgwname)) {
1202
		route_del('default', $ipprotocol); 
1203
	}
1204
}
1205

    
1206
function setdefaultgateway($gw) {
1207
	global $g, $config;
1208
	if (isset($config['system']['route-debug'])) {
1209
		file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']}");
1210
	}
1211
	$ipprotocol = $gw['ipprotocol'];
1212
	if ($gw['gateway'] == "dynamic") {
1213
		if ($ipprotocol == 'inet') {
1214
			$gw['gateway'] = get_interface_gateway($gw['friendlyiface']);
1215
		} else {
1216
			$gw['gateway'] = get_interface_gateway_v6($$gw['friendlyiface']);
1217
		}
1218
	}
1219
	if ($ipprotocol == 'inet6' && !is_ipaddrv6($gw['gateway'])) {
1220
		return;
1221
	}
1222
	if ($ipprotocol == 'inet' && !is_ipaddrv4($gw['gateway'])) {
1223
		return;
1224
	}
1225
	if ($ipprotocol == 'inet6') {
1226
		if (is_linklocal($gw['gateway']) && get_ll_scope($gw['gateway']) == '') {
1227
			$gw['gateway'] .= "%" . $gw['interface'];
1228
		}
1229
	}
1230
	$currentdefaultgwip = route_get_default($ipprotocol);
1231
	if ($currentdefaultgwip != $gw['gateway']) {
1232
		log_error("Default gateway setting {$gw['descr']} as default.");
1233

    
1234
		if ($ipprotocol == 'inet') {
1235
			$inet = '';
1236
		} else {
1237
			$inet = 'v6';
1238
		}
1239
		array_map('unlink', glob("{$g['tmp_path']}/*_defaultgw{$inet}", GLOB_BRACE));
1240
		$defaultif = get_real_interface($gw['interface']);
1241
		if ($defaultif) {
1242
			@file_put_contents("{$g['tmp_path']}/{$defaultif}_defaultgw{$inet}", $gw['gateway']);
1243
		}
1244

    
1245
		if (isset($gw["nonlocalgateway"])) {
1246
			if (is_ipaddr($gw['gateway']) && !empty($gw['interface'])) {
1247
				route_add_or_change($gw['gateway'], '',
1248
				    $gw['interface']);
1249
			}
1250
		}
1251
		if (isset($config['system']['route-debug'])) {
1252
			file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']} ({$gw['gateway']})");
1253
		}
1254
		route_add_or_change("default", $gw['gateway'], '', '',
1255
		    $ipprotocol);
1256
		return true;
1257
	}
1258
}
1259

    
1260
function get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist){
1261
	$result = array();
1262
	/* create array with group gateways members separated by tier */
1263
	$tiers = array();
1264
	$backupplan = array();
1265
	$gwvip_arr = array();
1266
	foreach ($group['item'] as $item) {
1267
		list($gwname, $tier, $vipname) = explode("|", $item);
1268

    
1269
		if (is_ipaddr($viplist[$vipname])) {
1270
			if (!is_array($gwvip_arr[$group['name']])) {
1271
				$gwvip_arr[$group['name']] = array();
1272
			}
1273
			$gwvip_arr[$group['name']][$gwname] = $vipname;
1274
		}
1275

    
1276
		/* Do it here rather than reiterating again the group in case no member is up. */
1277
		if (!is_array($backupplan[$tier])) {
1278
			$backupplan[$tier] = array();
1279
		}
1280
		$backupplan[$tier][] = $gwname;
1281

    
1282
		/* check if the gateway is available before adding it to the array */
1283
		if (is_array($gateways_status[$gwname])) {
1284
			$status = $gateways_status[$gwname];
1285
			$gwdown = false;
1286
			if (stristr($status['status'], "down")) {
1287
				$gwdown = true;
1288
				switch ($status['substatus']) {
1289
					case "highloss":
1290
						$msg = sprintf(gettext('MONITOR: %1$s has packet loss, omitting from routing group %2$s'), $gwname, $group['name']);
1291
						break;
1292
					case "highdelay":
1293
						$msg = sprintf(gettext('MONITOR: %1$s has high latency, omitting from routing group %2$s'), $gwname, $group['name']);
1294
						break;
1295
					default:
1296
						$msg = sprintf(gettext('MONITOR: %1$s is down, omitting from routing group %2$s'), $gwname, $group['name']);
1297
				}
1298
			}
1299
			$statuschanged = false;
1300
			$pluginparams = array();
1301
			$pluginparams['type'] = 'gateway';
1302
			$pluginparams['name'] = $gwname;
1303
			if ($gwdown == true) {
1304
				if (!file_exists("/tmp/.down.{$gwname}")) {
1305
					@touch("/tmp/.down.{$gwname}");
1306
					$msg .= "\n".implode("|", $status);
1307
					$pluginparams['event'] = 'gateway.down';
1308
					$statuschanged = true;
1309
				}
1310
			} else {
1311
				/* Online add member */
1312
				if (!is_array($tiers[$tier])) {
1313
					$tiers[$tier] = array();
1314
				}
1315
				$tiers[$tier][] = $gwname;
1316
				if (unlink_if_exists("/tmp/.down.{$gwname}")) {
1317
					$msg = sprintf(gettext('MONITOR: %1$s is available now, adding to routing group %2$s'), $gwname, $group['name']);
1318
					$msg .= "\n".implode("|", $status);
1319
					$pluginparams['event'] = 'gateway.up';
1320
					$statuschanged = true;
1321
				}
1322
			}
1323
			if ($statuschanged) {
1324
				log_error($msg);
1325
				notify_all_remote($msg);
1326
				if (isset($gateways_arr[$gwname]['interface'])) {
1327
					$pluginparams['interface'] = $gateways_arr[$gwname]['interface'];
1328
				}
1329
				pkg_call_plugins('plugin_gateway', $pluginparams);
1330
			}
1331
		} else if (isset($gateways_arr[$gwname]['monitor_disable']) || isset($gateways_arr[$gwname]['action_disable'])) {
1332
			$tiers[$tier][] = $gwname;
1333
		}
1334
	}
1335
	$tiers_count = count($tiers);
1336
	if ($tiers_count == 0) {
1337
		/* Oh dear, we have no members! Engage Plan B */
1338
		if (isset($config['system']['gw-debug']) && (!platform_booting())) {
1339
			$msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']);
1340
			log_error($msg);
1341
		}
1342
		$tiers = $backupplan;
1343
	}
1344
	/* sort the tiers array by the tier key */
1345
	ksort($tiers);
1346

    
1347
	/* we do not really foreach the tiers as we stop after the first tier */
1348
	foreach ($tiers as $tieridx => $tier) {
1349
		/* process all gateways in this tier */
1350
		foreach ($tier as $member) {
1351
			/* determine interface gateway */
1352
			if (isset($gateways_arr[$member])) {
1353
				$gateway = $gateways_arr[$member];
1354
				$int = $gateway['interface'];
1355
				$gatewayip = "";
1356
				if (is_ipaddr($gateway['gateway'])) {
1357
					$gatewayip = $gateway['gateway'];
1358
				} elseif (!empty($int)) {
1359
					if ($gateway['ipprotocol'] == 'inet') {
1360
						$gatewayip = get_interface_gateway($gateway['friendlyiface']);
1361
					} else {
1362
						$gatewayip = get_interface_gateway_v6($gateway['friendlyiface']);
1363
					}
1364
				}
1365

    
1366
				if (!empty($int)) {
1367
					$result['ipprotocol'] = $gateway['ipprotocol'];
1368
					if (is_ipaddr($gatewayip)) {
1369
						$groupmember = array();
1370
						$groupmember['gw'] = $member;
1371
						$groupmember['int'] = $int;
1372
						$groupmember['gwip'] = $gatewayip;
1373
						/* set correct linklocal gateway address,
1374
						 * see https://redmine.pfsense.org/issues/12721 */
1375
						if (is_ipaddrv6($gatewayip) && is_linklocal($gatewayip) && empty(get_ll_scope($gatewayip))) {
1376
							$groupmember['gwip'] .= '%' . $int;
1377
						}
1378
						$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
1379
						if (is_array($gwvip_arr[$group['name']]) && !empty($gwvip_arr[$group['name']][$member])) {
1380
							$groupmember['vip'] = $gwvip_arr[$group['name']][$member];
1381
						}
1382
						$result[] = $groupmember;
1383
					}
1384
				}
1385
			}
1386
		}
1387
		/* we should have the 1st available tier now, exit stage left */
1388
		if (count($result) > 0) {
1389
			break;
1390
		} else {
1391
			log_error(sprintf(gettext('GATEWAYS: Group %1$s did not have any gateways up on tier %2$s!'), $group['name'], $tieridx));
1392
		}
1393
	}
1394
	// Add description field last to not influence the count() above
1395
	$result['descr'] = $group['descr'];
1396
	return $result;
1397
}
1398

    
1399
function get_gwgroup_members($groupname) {
1400
	global $config;
1401
	$gateways_status = return_gateways_status(true);
1402
	$gateways_arr = return_gateways_array();
1403
	$viplist = get_configured_vip_list();
1404
	foreach ($config['gateways']['gateway_group'] as $group) {
1405
		if ($group['name'] == $groupname) {
1406
			return get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1407
		}
1408
	}
1409
	return array();
1410
}
1411

    
1412
/*
1413
 * Return an array with all gateway groups with name as key
1414
 * All gateway groups will be processed before returning the array.
1415
 */
1416
function return_gateway_groups_array($fixup = false, $gways = false) {
1417
	global $config;
1418

    
1419
	/* fetch the current gateways status */
1420
	if (is_array($gways)) {
1421
		$gateways_status = $gways;
1422
	} else {
1423
		$gateways_status = return_gateways_status(true);
1424
	}
1425

    
1426
	$gateways_arr = return_gateways_array();
1427
	$gateway_groups_array = array();
1428
	if ($fixup == true) {
1429
		$gw4 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw4'], $gways);
1430
		$gw6 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw6'], $gways);
1431
		if ($gw4 && $gw4['type'] == 'gatewaygroup') {
1432
			fixup_default_gateway("inet", $gateways_status, $gateways_arr);
1433
		}
1434
		if ($gw6 && $gw6['type'] == 'gatewaygroup') {
1435
			fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
1436
		}
1437
	}
1438
	init_config_arr(array('gateways', 'gateway_group'));
1439
	if (!empty($config['gateways']['gateway_group'])) {
1440
		$viplist = get_configured_vip_list();
1441
		foreach ($config['gateways']['gateway_group'] as $group) {
1442
			$gateway_groups_array[$group['name']] = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1443
		}
1444
	}
1445

    
1446
	return ($gateway_groups_array);
1447
}
1448

    
1449
/* Update DHCP WAN Interface ip address in gateway group item */
1450
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
1451
	global $config;
1452

    
1453
	if (is_array($config['gateways']['gateway_item'])) {
1454
		foreach ($config['gateways']['gateway_item'] as & $gw) {
1455
			if ($gw['interface'] != $interface) {
1456
				continue;
1457
			}
1458

    
1459
			$current_gw = get_interface_gateway($interface);
1460
			if ($gw['gateway'] <> $current_gw) {
1461
				$gw['gateway'] = $current_gw;
1462
				$changed = true;
1463
			}
1464
		}
1465
	}
1466

    
1467
	if ($changed && $current_gw) {
1468
		write_config(sprintf(gettext(
1469
		    'Updating gateway group gateway for %1$s - new gateway is %2$s'),
1470
		    $interface, $current_gw));
1471
	}
1472
}
1473

    
1474
function lookup_gateway_or_group_by_name($gwname, $gways = false) {
1475
	global $config;
1476

    
1477
	if (is_array($gways)) {
1478
		$gateways_arr = $gways;
1479
	} else {
1480
		$gateways_arr = return_gateways_array();
1481
	}
1482

    
1483
	foreach ($gateways_arr as $gw) {
1484
		if ($gw['name'] == $gwname) {
1485
			$gw['type'] = 'gateway';
1486
			return $gw;
1487
		}
1488
	}
1489

    
1490
	init_config_arr(array('gateways', 'gateway_group'));
1491
	foreach ($config['gateways']['gateway_group'] as $gwg) {
1492
		if ($gwg['name'] == $gwname) {
1493
			$gwg['type'] = 'gatewaygroup';
1494
			return $gwg;
1495
		}
1496
	}
1497

    
1498
	return false;
1499
}
1500

    
1501
function lookup_gateway_ip_by_name($name, $disabled = false) {
1502

    
1503
	$gateways_arr = return_gateways_array($disabled, true);
1504
	foreach ($gateways_arr as $gname => $gw) {
1505
		if ($gw['name'] === $name || $gname === $name) {
1506
			return $gw['gateway'];
1507
		}
1508
	}
1509

    
1510
	return false;
1511
}
1512

    
1513
function lookup_gateway_monitor_ip_by_name($name) {
1514

    
1515
	$gateways_arr = return_gateways_array(false, true);
1516
	if (!empty($gateways_arr[$name])) {
1517
		$gateway = $gateways_arr[$name];
1518
		if (!is_ipaddr($gateway['monitor'])) {
1519
			return $gateway['gateway'];
1520
		}
1521

    
1522
		return $gateway['monitor'];
1523
	}
1524

    
1525
	return (false);
1526
}
1527

    
1528
function lookup_gateway_interface_by_name($name) {
1529

    
1530
	$gateways_arr = return_gateways_array(false, true);
1531
	if (!empty($gateways_arr[$name])) {
1532
		$interfacegw = $gateways_arr[$name]['friendlyiface'];
1533
		return ($interfacegw);
1534
	}
1535

    
1536
	return (false);
1537
}
1538

    
1539
function get_root_interface($interface) {
1540
	if (substr($interface, 0, 4) == '_vip') {
1541
		$interface = get_configured_vip_interface($interface);
1542
		if (substr($interface, 0, 4) == '_vip') {
1543
			$interface = get_configured_vip_interface($interface);
1544
		}
1545
	}
1546
	return $interface;
1547
}
1548

    
1549
function get_interface_gateway($interface, &$dynamic = false) {
1550
	global $config, $g;
1551

    
1552
	$interface = get_root_interface($interface);
1553

    
1554
	$gw = NULL;
1555
	$gwcfg = $config['interfaces'][$interface];
1556
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
1557
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1558
			if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
1559
				$gw = $gateway['gateway'];
1560
				break;
1561
			}
1562
		}
1563
	}
1564

    
1565
	// for dynamic interfaces we handle them through the $interface_router file.
1566
	if (($gw == NULL || !is_ipaddrv4($gw)) && !is_ipaddrv4($gwcfg['ipaddr'])) {
1567
		$realif = get_real_interface($interface);
1568
		if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
1569
			$gw = trim(@file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
1570
			$dynamic = true;
1571
		}
1572
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw")) {
1573
			$dynamic = "default";
1574
		}
1575

    
1576
	}
1577

    
1578
	/* return gateway */
1579
	return ($gw);
1580
}
1581

    
1582
function get_interface_gateway_v6($interface, &$dynamic = false) {
1583
	global $config, $g;
1584

    
1585
	$interface = get_root_interface($interface);
1586

    
1587
	$gw = NULL;
1588
	$gwcfg = $config['interfaces'][$interface];
1589
	if (!empty($gwcfg['gatewayv6']) && is_array($config['gateways']['gateway_item'])) {
1590
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1591
			if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
1592
				$gw = $gateway['gateway'];
1593
				break;
1594
			}
1595
		}
1596
	}
1597

    
1598
	// for dynamic interfaces we handle them through the $interface_router file.
1599
	if (($gw == NULL || !is_ipaddrv6($gw)) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
1600
		$realif = get_real_interface($interface);
1601
		if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
1602
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
1603
			$dynamic = true;
1604
		}
1605
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6")) {
1606
			$dynamic = "default";
1607
		}
1608
	}
1609
	/* return gateway */
1610
	return ($gw);
1611
}
1612

    
1613
/* Check a IP address against a gateway IP or name
1614
 * to verify it's address family */
1615
function validate_address_family($ipaddr, $gwname, $disabled = false) {
1616
	$v4ip = false;
1617
	$v6ip = false;
1618
	$v4gw = false;
1619
	$v6gw = false;
1620

    
1621
	if (is_ipaddrv4($ipaddr)) {
1622
		$v4ip = true;
1623
	}
1624
	if (is_ipaddrv6($ipaddr)) {
1625
		$v6ip = true;
1626
	}
1627
	if (is_ipaddrv4($gwname)) {
1628
		$v4gw = true;
1629
	}
1630
	if (is_ipaddrv6($gwname)) {
1631
		$v6gw = true;
1632
	}
1633

    
1634
	if ($v4ip && $v4gw) {
1635
		return true;
1636
	}
1637
	if ($v6ip && $v6gw) {
1638
		return true;
1639
	}
1640

    
1641
	/* still no match, carry on, lookup gateways */
1642
	if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname, $disabled))) {
1643
		$v4gw = true;
1644
	}
1645
	if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname, $disabled))) {
1646
		$v6gw = true;
1647
	}
1648

    
1649
	$gw_array = return_gateways_array();
1650
	if (is_array($gw_array[$gwname])) {
1651
		switch ($gw_array[$gwname]['ipprotocol']) {
1652
			case "inet":
1653
				$v4gw = true;
1654
				break;
1655
			case "inet6":
1656
				$v6gw = true;
1657
				break;
1658
		}
1659
	}
1660

    
1661
	if ($v4ip && $v4gw) {
1662
		return true;
1663
	}
1664
	if ($v6ip && $v6gw) {
1665
		return true;
1666
	}
1667

    
1668
	return false;
1669
}
1670

    
1671
/* check if a interface is part of a gateway group */
1672
function interface_gateway_group_member($interface, $gwgroup_name = "") {
1673
	global $config;
1674

    
1675
	if (is_array($config['gateways']['gateway_group'])) {
1676
		$groups = $config['gateways']['gateway_group'];
1677
	} else {
1678
		return false;
1679
	}
1680

    
1681
	$gateways_arr = return_gateways_array(false, true);
1682
	foreach ($groups as $group) {
1683
		if (is_array($group['item'])) {
1684
			foreach ($group['item'] as $item) {
1685
				$elements = explode("|", $item);
1686
				$gwname = $elements[0];
1687
				if ($interface == $gateways_arr[$gwname]['interface'] &&
1688
				    (empty($gwgroup_name) || $gwgroup_name == $group['name'])) {
1689
					unset($gateways_arr);
1690
					return true;
1691
				}
1692
			}
1693
		}
1694
	}
1695
	unset($gateways_arr);
1696

    
1697
	return false;
1698
}
1699

    
1700
function gateway_is_gwgroup_member($name, $detail=false) {
1701
	global $config;
1702

    
1703
	if (is_array($config['gateways']['gateway_group'])) {
1704
		$groups = $config['gateways']['gateway_group'];
1705
	} else {
1706
		return false;
1707
	}
1708

    
1709
	$members = array();
1710
	foreach ($groups as $group) {
1711
		if (is_array($group['item'])) {
1712
			foreach ($group['item'] as $item) {
1713
				list($gwname, $tier, $vipname) = explode("|", $item);
1714
				if ($name == $gwname) {
1715
					if ($detail) {
1716
						$newitem = array();
1717
						$newitem['name'] = $group['name'];
1718
						$newitem['tier'] = $tier;
1719
						$newitem['vipname'] = $vipname;
1720
						$members[] = $newitem;
1721
					} else {
1722
						$members[] = $group['name'];
1723
					}
1724
				}
1725
			}
1726
		}
1727
	}
1728

    
1729
	return $members;
1730
}
1731
/*
1732
  Check the proposed gateway settings to see if they are valid.
1733
  $gateway_settings - the proposed array of proposed gateway settings
1734
  $id - the index of the gateway proposed to be modified (otherwise "" if adding a new gateway)
1735
  $parent_ip - the IP (v4 or v6) address about to be set on the corresponding interface (if any)
1736
  $parent_sn - the subnet about to be set on the corresponding interface (if any)
1737
  (Note: the above 2 parameters allow gateway parameters to be validated concurrently with saving
1738
   an interface, before the new interface parameters are actually saved in the config.)
1739
  Return completed $input_errors array if there is any problem.
1740
  Otherwise return an empty $input_errors array
1741
*/
1742
function validate_gateway($gateway_settings, $id = "", $parent_ip = "", $parent_sn = "") {
1743
	global $config, $gateway_state_kill_modes;
1744

    
1745
	$a_gateways = return_gateways_array(true, false, true, true);
1746
	$input_errors = array();
1747

    
1748
	/* input validation */
1749
	$reqdfields = explode(" ", "name interface");
1750
	$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
1751

    
1752
	do_input_validation($gateway_settings, $reqdfields, $reqdfieldsn, $input_errors);
1753

    
1754
	if (!isset($gateway_settings['name'])) {
1755
		$input_errors[] = "A valid gateway name must be specified.";
1756
	}
1757
	if (!is_validaliasname($gateway_settings['name'])) {
1758
		$input_errors[] = invalidaliasnamemsg($gateway_settings['name'], gettext("gateway"));
1759
	} else if (isset($gateway_settings['disabled'])) {
1760
		// We have a valid gateway name that the user wants to mark as disabled.
1761
		// Check if the gateway name is used in any gateway group.
1762
		if (is_array($config['gateways']['gateway_group'])) {
1763
			foreach ($config['gateways']['gateway_group'] as $group) {
1764
				foreach ($group['item'] as $item) {
1765
					$items = explode("|", $item);
1766
					if ($items[0] == $gateway_settings['name']) {
1767
						$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']);
1768
					}
1769
				}
1770
			}
1771
		}
1772

    
1773
		// Check if the gateway name is used in any enabled Static Route.
1774
		if (is_array($config['staticroutes']['route'])) {
1775
			foreach ($config['staticroutes']['route'] as $route) {
1776
				if ($route['gateway'] == $gateway_settings['name']) {
1777
					if (!isset($route['disabled'])) {
1778
						// There is a static route that uses this gateway and is enabled (not disabled).
1779
						$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']);
1780
					}
1781
				}
1782
			}
1783
		}
1784

    
1785
		// Check if the gateway name is used by any DNS Server
1786
		$dnsgw_counter = 1;
1787
		while (isset($config["system"]["dns{$dnsgw_counter}gw"])) {
1788
			if ($gateway_settings['name'] == $config["system"]["dns{$dnsgw_counter}gw"]) {
1789
				// The user wants to disable this gateway, but there is a static route to the DNS server that refers to the gateway.
1790
				$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"]);
1791
			}
1792
			$dnsgw_counter++;
1793
		}
1794
	}
1795
	/* skip system gateways which have been automatically added */
1796
	if (($gateway_settings['gateway'] && (!is_ipaddr($gateway_settings['gateway'])) && ($gateway_settings['attribute'] !== "system")) && ($gateway_settings['gateway'] != "dynamic")) {
1797
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
1798
	}
1799

    
1800
	if ($gateway_settings['gateway'] && is_ipaddr($gateway_settings['gateway'])) {
1801
		if (is_ipaddrv4($gateway_settings['gateway'])) {
1802
			if ($parent_ip == '') {
1803
				$parent_ip = get_interface_ip($gateway_settings['interface']);
1804
				$parent_sn = get_interface_subnet($gateway_settings['interface']);
1805
			}
1806
			if (empty($parent_ip) || empty($parent_sn)) {
1807
				$input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
1808
			} elseif (!isset($gateway_settings["nonlocalgateway"])) {
1809
				$subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
1810
				$vips = link_interface_to_vips($gateway_settings['interface']);
1811
				if (is_array($vips)) {
1812
					foreach ($vips as $vip) {
1813
						if (!is_ipaddrv4($vip['subnet'])) {
1814
							continue;
1815
						}
1816
						$subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
1817
					}
1818
				}
1819

    
1820
				$found = false;
1821
				foreach ($subnets as $subnet) {
1822
					if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
1823
						$found = true;
1824
						break;
1825
					}
1826
				}
1827

    
1828
				if ($found === false) {
1829
					$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
1830
				}
1831
			}
1832
		} else if (is_ipaddrv6($gateway_settings['gateway'])) {
1833
			/* do not do a subnet match on a link local address, it's valid */
1834
			if (!is_linklocal($gateway_settings['gateway'])) {
1835
				if ($parent_ip == '') {
1836
					$parent_ip = get_interface_ipv6($gateway_settings['interface']);
1837
					$parent_sn = get_interface_subnetv6($gateway_settings['interface']);
1838
				}
1839
				if (empty($parent_ip) || empty($parent_sn)) {
1840
					$input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
1841
				} elseif (!isset($gateway_settings["nonlocalgateway"])) {
1842
					$subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
1843
					$vips = link_interface_to_vips($gateway_settings['interface']);
1844
					if (is_array($vips)) {
1845
						foreach ($vips as $vip) {
1846
							if (!is_ipaddrv6($vip['subnet'])) {
1847
								continue;
1848
							}
1849
							$subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
1850
						}
1851
					}
1852

    
1853
					$found = false;
1854
					foreach ($subnets as $subnet) {
1855
						if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
1856
							$found = true;
1857
							break;
1858
						}
1859
					}
1860

    
1861
					if ($found === false) {
1862
						$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
1863
					}
1864
				}
1865
			}
1866
		}
1867

    
1868
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddr'])) {
1869
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddr']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
1870
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
1871
			}
1872
		}
1873
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddrv6'])) {
1874
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddrv6']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
1875
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
1876
			}
1877
		}
1878
	}
1879
	if (($gateway_settings['monitor'] != "") && ($gateway_settings['monitor'] != "dynamic")) {
1880
		validateipaddr($gateway_settings['monitor'], IPV4V6, "Monitor IP", $input_errors, false);
1881
	}
1882
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] < 0) {
1883
		$input_errors[] = gettext("A valid data payload must be specified.");
1884
	}
1885
	if (!empty($gateway_settings['skip_rules_gw_down']) && !array_key_exists($gateway_settings['skip_rules_gw_down'], $gateway_state_kill_modes)) {
1886
		$input_errors[] = gettext("Please select a valid State Killing on Gateway Failure mode.");
1887
	}
1888
	/* only allow correct IPv4 and IPv6 gateway addresses */
1889
	if (($gateway_settings['gateway'] <> "") && is_ipaddr($gateway_settings['gateway']) && $gateway_settings['gateway'] != "dynamic") {
1890
		if (is_ipaddrv6($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet")) {
1891
			$input_errors[] = sprintf(gettext("The IPv6 gateway address '%s' can not be used as a IPv4 gateway."), $gateway_settings['gateway']);
1892
		}
1893
		if (is_ipaddrv4($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet6")) {
1894
			$input_errors[] = sprintf(gettext("The IPv4 gateway address '%s' can not be used as a IPv6 gateway."), $gateway_settings['gateway']);
1895
		}
1896
	}
1897
	/* only allow correct IPv4 and IPv6 monitor addresses */
1898
	if (($gateway_settings['monitor'] <> "") && is_ipaddr($gateway_settings['monitor']) && $gateway_settings['monitor'] != "dynamic") {
1899
		if (is_ipaddrv6($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet")) {
1900
			$input_errors[] = sprintf(gettext("The IPv6 monitor address '%s' can not be used on a IPv4 gateway."), $gateway_settings['monitor']);
1901
		}
1902
		if (is_ipaddrv4($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet6")) {
1903
			$input_errors[] = sprintf(gettext("The IPv4 monitor address '%s' can not be used on a IPv6 gateway."), $gateway_settings['monitor']);
1904
		}
1905
	}
1906

    
1907
	if (isset($gateway_settings['name'])) {
1908
		/* check for overlaps */
1909
		foreach ($a_gateways as $gateway) {
1910
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
1911
				if ($gateway['name'] != $gateway_settings['name']) {
1912
					$input_errors[] = gettext("Changing name on a gateway is not allowed.");
1913
				}
1914
				continue;
1915
			}
1916
			if ($gateway_settings['name'] <> "") {
1917
				if (($gateway['name'] <> "") && ($gateway_settings['name'] == $gateway['name']) && ($gateway['attribute'] !== "system")) {
1918
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $gateway_settings['name']);
1919
					break;
1920
				}
1921
			}
1922
			if (is_ipaddr($gateway_settings['gateway'])) {
1923
				if (($gateway['gateway'] <> "") && ($gateway_settings['gateway'] == $gateway['gateway']) && ($gateway['attribute'] !== "system")) {
1924
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $gateway_settings['gateway']);
1925
					break;
1926
				}
1927
			}
1928
			if (is_ipaddr($gateway_settings['monitor'])) {
1929
				if (($gateway['monitor'] <> "") && ($gateway_settings['monitor'] == $gateway['monitor']) && ($gateway['attribute'] !== "system")) {
1930
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. A different monitor IP must be chosen.'), $gateway_settings['monitor']);
1931
					break;
1932
				}
1933
			}
1934
		}
1935
	}
1936

    
1937
	/* input validation of dpinger advanced parameters */
1938

    
1939
	$dpinger_default = return_dpinger_defaults();
1940
	$latencylow = $dpinger_default['latencylow'];
1941
	if ($gateway_settings['latencylow']) {
1942
		if (!is_numeric($gateway_settings['latencylow'])) {
1943
			$input_errors[] = gettext("The low latency threshold needs to be a numeric value.");
1944
		} else if ($gateway_settings['latencylow'] < 1) {
1945
			$input_errors[] = gettext("The low latency threshold needs to be positive.");
1946
		} else {
1947
			$latencylow = $gateway_settings['latencylow'];
1948
		}
1949
	}
1950

    
1951
	$latencyhigh = $dpinger_default['latencyhigh'];
1952
	if ($gateway_settings['latencyhigh']) {
1953
		if (!is_numeric($gateway_settings['latencyhigh'])) {
1954
			$input_errors[] = gettext("The high latency threshold needs to be a numeric value.");
1955
		} else if ($gateway_settings['latencyhigh'] < 1) {
1956
			$input_errors[] = gettext("The high latency threshold needs to be positive.");
1957
		} else {
1958
			$latencyhigh = $gateway_settings['latencyhigh'];
1959
		}
1960
	}
1961

    
1962
	$losslow = $dpinger_default['losslow'];
1963
	if ($gateway_settings['losslow']) {
1964
		if (!is_numeric($gateway_settings['losslow'])) {
1965
			$input_errors[] = gettext("The low Packet Loss threshold needs to be a numeric value.");
1966
		} else if ($gateway_settings['losslow'] < 1) {
1967
			$input_errors[] = gettext("The low Packet Loss threshold needs to be positive.");
1968
		} else if ($gateway_settings['losslow'] >= 100) {
1969
			$input_errors[] = gettext("The low Packet Loss threshold needs to be less than 100.");
1970
		} else {
1971
			$losslow = $gateway_settings['losslow'];
1972
		}
1973
	}
1974

    
1975
	$losshigh = $dpinger_default['losshigh'];
1976
	if ($gateway_settings['losshigh']) {
1977
		if (!is_numeric($gateway_settings['losshigh'])) {
1978
			$input_errors[] = gettext("The high Packet Loss threshold needs to be a numeric value.");
1979
		} else if ($gateway_settings['losshigh'] < 1) {
1980
			$input_errors[] = gettext("The high Packet Loss threshold needs to be positive.");
1981
		} else if ($gateway_settings['losshigh'] > 100) {
1982
			$input_errors[] = gettext("The high Packet Loss threshold needs to be 100 or less.");
1983
		} else {
1984
			$losshigh = $gateway_settings['losshigh'];
1985
		}
1986
	}
1987

    
1988
	$time_period = $dpinger_default['time_period'];
1989
	if ($gateway_settings['time_period']) {
1990
		if (!is_numeric($gateway_settings['time_period'])) {
1991
			$input_errors[] = gettext("The time period over which results are averaged needs to be a numeric value.");
1992
		} else if ($gateway_settings['time_period'] < 1) {
1993
			$input_errors[] = gettext("The time period over which results are averaged needs to be positive.");
1994
		} else {
1995
			$time_period = $gateway_settings['time_period'];
1996
		}
1997
	}
1998

    
1999
	$interval = $dpinger_default['interval'];
2000
	if ($gateway_settings['interval']) {
2001
		if (!is_numeric($gateway_settings['interval'])) {
2002
			$input_errors[] = gettext("The probe interval needs to be a numeric value.");
2003
		} else if ($gateway_settings['interval'] < 1) {
2004
			$input_errors[] = gettext("The probe interval needs to be positive.");
2005
		} else {
2006
			$interval = $gateway_settings['interval'];
2007
		}
2008
	}
2009

    
2010
	$loss_interval = $dpinger_default['loss_interval'];
2011
	if ($gateway_settings['loss_interval']) {
2012
		if (!is_numeric($gateway_settings['loss_interval'])) {
2013
			$input_errors[] = gettext("The loss interval needs to be a numeric value.");
2014
		} else if ($gateway_settings['loss_interval'] < 1) {
2015
			$input_errors[] = gettext("The loss interval setting needs to be positive.");
2016
		} else {
2017
			$loss_interval = $gateway_settings['loss_interval'];
2018
		}
2019
	}
2020

    
2021
	$alert_interval = $dpinger_default['alert_interval'];
2022
	if ($gateway_settings['alert_interval']) {
2023
		if (!is_numeric($gateway_settings['alert_interval'])) {
2024
			$input_errors[] = gettext("The alert interval needs to be a numeric value.");
2025
		} else if ($gateway_settings['alert_interval'] < 1) {
2026
			$input_errors[] = gettext("The alert interval setting needs to be positive.");
2027
		} else {
2028
			$alert_interval = $gateway_settings['alert_interval'];
2029
		}
2030
	}
2031

    
2032
	if ($latencylow >= $latencyhigh) {
2033
		$input_errors[] = gettext(
2034
		    "The high latency threshold needs to be greater than the low latency threshold");
2035
	}
2036

    
2037
	if ($losslow >= $losshigh) {
2038
		$input_errors[] = gettext(
2039
		    "The high packet loss threshold needs to be higher than the low packet loss threshold");
2040
	}
2041

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

    
2048
	// Ensure that the time period is greater than 2 times the probe interval plus the loss interval.
2049
	if (($interval * 2 + $loss_interval) >= $time_period) {
2050
		$input_errors[] = gettext("The time period needs to be greater than twice the probe interval plus the loss interval.");
2051
	}
2052

    
2053
	// There is no point recalculating the average latency and loss more often than the probe interval.
2054
	// So the alert interval needs to be >= probe interval.
2055
	if ($interval > $alert_interval) {
2056
		$input_errors[] = gettext("The alert interval needs to be greater than or equal to the probe interval.");
2057
	}
2058

    
2059
	return $input_errors;
2060
}
2061

    
2062
// Save gateway settings.
2063
// $gateway_settings - the array of gateway setting parameters
2064
// $realid - the index of the gateway to be modified (otherwise "" if adding a new gateway)
2065

    
2066
// This function is responsible to:
2067
//   Setup the gateway parameter structure from the gateway settings input parameter
2068
//   Save the structure into the config
2069
//   Remove any run-time settings from gateway parameters that are changed (e.g. remove routes to addresses that are changing)
2070

    
2071
// A subsequent "apply" step will implement the added/changed gateway.
2072

    
2073
function save_gateway($gateway_settings, $realid = "") {
2074
	global $config;
2075

    
2076
	init_config_arr(array('gateways', 'gateway_item'));
2077
	$a_gateway_item = &$config['gateways']['gateway_item'];
2078
	$reloadif = "";
2079
	$gateway = array();
2080

    
2081
	if (empty($gateway_settings['interface'])) {
2082
		$gateway['interface'] = $gateway_settings['friendlyiface'];
2083
	} else {
2084
		$gateway['interface'] = $gateway_settings['interface'];
2085
	}
2086
	if (is_ipaddr($gateway_settings['gateway'])) {
2087
		$gateway['gateway'] = $gateway_settings['gateway'];
2088
	} else {
2089
		$gateway['gateway'] = "dynamic";
2090
	}
2091
	$gateway['name'] = $gateway_settings['name'];
2092
	$gateway['weight'] = $gateway_settings['weight'];
2093
	$gateway['ipprotocol'] = $gateway_settings['ipprotocol'];
2094
	if ($gateway_settings['interval']) {
2095
		$gateway['interval'] = $gateway_settings['interval'];
2096
	}
2097

    
2098
	if ($gateway_settings['time_period']) {
2099
		$gateway['time_period'] = $gateway_settings['time_period'];
2100
	}
2101
	if ($gateway_settings['alert_interval']) {
2102
		$gateway['alert_interval'] = $gateway_settings['alert_interval'];
2103
	}
2104

    
2105
	$gateway['descr'] = $gateway_settings['descr'];
2106
	if ($gateway_settings['monitor_disable'] == "yes") {
2107
		$gateway['monitor_disable'] = true;
2108
	}
2109
	if ($gateway_settings['action_disable'] == "yes") {
2110
		$gateway['action_disable'] = true;
2111
	}
2112
	if ($gateway_settings['nonlocalgateway'] == "yes") {
2113
		$gateway['nonlocalgateway'] = true;
2114
	}
2115
	if ($gateway_settings['dpinger_dont_add_static_route'] == "yes") {
2116
		$gateway['dpinger_dont_add_static_route'] = true;
2117
	}
2118
	if ($gateway_settings['force_down'] == "yes") {
2119
		$gateway['force_down'] = true;
2120
	}
2121
	$gateway['gw_down_kill_states'] = $gateway_settings['gw_down_kill_states'];
2122
	if (is_ipaddr($gateway_settings['monitor'])) {
2123
		$gateway['monitor'] = $gateway_settings['monitor'];
2124
	}
2125
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] >= 0) {
2126
		$gateway['data_payload'] = $gateway_settings['data_payload'];
2127
	}
2128

    
2129
	/* NOTE: If gateway ip is changed need to cleanup the old static interface route */
2130
	if ($gateway_settings['monitor'] != "dynamic" &&
2131
	    !empty($a_gateway_item[$realid]) &&
2132
	    is_ipaddr($a_gateway_item[$realid]['gateway']) &&
2133
	    $gateway['gateway'] != $a_gateway_item[$realid]['gateway'] &&
2134
	    isset($a_gateway_item[$realid]["nonlocalgateway"])) {
2135
		route_del($a_gateway_item[$realid]['gateway']);
2136
	}
2137

    
2138
	/* NOTE: If monitor ip is changed need to cleanup the old static route */
2139
	if ($gateway_settings['monitor'] != "dynamic" &&
2140
	    !empty($a_gateway_item[$realid]) &&
2141
	    is_ipaddr($a_gateway_item[$realid]['monitor']) &&
2142
	    $gateway_settings['monitor'] != $a_gateway_item[$realid]['monitor'] &&
2143
	    $gateway['gateway'] != $a_gateway_item[$realid]['monitor']) {
2144
		route_del($a_gateway_item[$realid]['monitor']);
2145
	}
2146

    
2147
	if ($gateway_settings['defaultgw'] == "yes" || $gateway_settings['defaultgw'] == "on") {
2148
		// a new default gateway is being saved.
2149
		$i = 0;
2150
		/* remove the default gateway bits for all gateways with the same address family */
2151
		if (is_array($a_gateway_item)) {
2152
			foreach ($a_gateway_item as $gw) {
2153
				if ($gateway['ipprotocol'] == $gw['ipprotocol']) {
2154
					if ($gw['interface'] != $gateway_settings['interface'] &&
2155
						($gw['name'] == $config['gateways']['defaultgw4'] || $gw['name'] == $config['gateways']['defaultgw6'])) {
2156
						// remember the old default gateway interface to call with a "interface reconfigure" event.
2157
						$reloadif = $gw['interface'];
2158
					}
2159
				}
2160
				$i++;
2161
			}
2162
		}
2163
		if ($gateway['ipprotocol'] == "inet") {
2164
			$config['gateways']['defaultgw4'] = $gateway['name'];
2165
		} elseif ($gateway['ipprotocol'] == "inet6") {
2166
			$config['gateways']['defaultgw6'] = $gateway['name'];
2167
		}
2168
	}
2169

    
2170
	if ($gateway_settings['latencylow']) {
2171
		$gateway['latencylow'] = $gateway_settings['latencylow'];
2172
	}
2173
	if ($gateway_settings['latencyhigh']) {
2174
		$gateway['latencyhigh'] = $gateway_settings['latencyhigh'];
2175
	}
2176
	if ($gateway_settings['losslow']) {
2177
		$gateway['losslow'] = $gateway_settings['losslow'];
2178
	}
2179
	if ($gateway_settings['losshigh']) {
2180
		$gateway['losshigh'] = $gateway_settings['losshigh'];
2181
	}
2182
	if ($gateway_settings['loss_interval']) {
2183
		$gateway['loss_interval'] = $gateway_settings['loss_interval'];
2184
	}
2185
	/* when saving the manual gateway we use the attribute which has the corresponding id */
2186
	if (isset($realid) && $a_gateway_item[$realid]) {
2187
		$preserve_disabled = isset($a_gateway_item[$realid]['disabled']);
2188
		$a_gateway_item[$realid] = $gateway;
2189
		if ($preserve_disabled) {
2190
			$a_gateway_item[$realid]['disabled'] = true;
2191
		}
2192
	} else {
2193
		$a_gateway_item[] = $gateway;
2194
	}
2195
	gateway_set_enabled($gateway_settings['name'], !isset($gateway_settings['disabled']));
2196

    
2197
	mark_subsystem_dirty('staticroutes');
2198

    
2199
	write_config("Gateway settings changed");
2200

    
2201
	if (!empty($reloadif)) {
2202
		send_event("interface reconfigure {$reloadif}");
2203
	}
2204
}
2205

    
2206
function gateway_set_enabled($name, $enabled) {
2207
	global $config;
2208
	if (is_array($config['gateways']['gateway_item'])) {
2209
		foreach($config['gateways']['gateway_item'] as &$item) {
2210
			if ($item['name'] == $name) {
2211
				$gateway = &$item;
2212
			}
2213
		}
2214
	}
2215
	if (!isset($gateway)) {
2216
		return;
2217
	}
2218
	if ($enabled) {
2219
		unset($gateway['disabled']);
2220
	} else {
2221
		/* Check if the gateway was enabled but changed to disabled. */
2222
		if (!isset($gateway['disabled'])) {
2223
			/*  If the disabled gateway was the default route, remove the default route */
2224
			if (is_ipaddr($gateway['gateway'])) {
2225
				$inet = (!is_ipaddrv4($gateway['gateway']) ? 'inet6' : 'inet');
2226
				if ($inet == 'inet') {
2227
					$cgw = route_get_default('inet');
2228
				} else {
2229
					$cgw = route_get_default('inet6');
2230
				}
2231
				if ($gateway['gateway'] == $cgw) {
2232
					route_del("default", $inet);
2233
				}
2234
			}
2235
			$gateway['disabled'] = true;
2236
		}
2237
	}
2238
}
2239

    
2240
function gateway_or_gwgroup_exists($name) {
2241
	global $config;
2242
	if (is_array($config['gateways']['gateway_item'])) {
2243
		foreach($config['gateways']['gateway_item'] as $item) {
2244
			if ($item['name'] == $name) {
2245
				return true;
2246
			}
2247
		}
2248
	}
2249
	if (is_array($config['gateways']['gateway_group'])) {
2250
		foreach($config['gateways']['gateway_group'] as $item) {
2251
			if ($item['name'] == $name) {
2252
				return true;
2253
			}
2254
		}
2255
	}
2256
	return false;
2257
}
2258

    
2259
// These two replacement functions avoid the need to call return_gateways_array() multiple times and
2260
// allow system_gateways.php to get everything it needs in a single function call
2261
function gateway_getgwtiername($gways, $idx) {
2262
	global $config;
2263

    
2264
	$result = "";
2265
	$gwname = $gways[$idx]['name'];
2266

    
2267
	$gw = get_gateway_or_group_by_name($gwname, $gways);
2268
	init_config_arr(array('gateways'));
2269
	if ($config['gateways']['defaultgw4'] == $gwname || $config['gateways']['defaultgw6'] == $gwname) {
2270
		$result = "Default";
2271
	} else {
2272
		if ($gw['ipprotocol'] == 'inet') {
2273
			$defgw = get_gateway_or_group_by_name($config['gateways']['defaultgw4'], $gways);
2274
		} else {
2275
			$defgw = get_gateway_or_group_by_name($config['gateways']['defaultgw6'], $gways);
2276
		}
2277

    
2278
		if ($defgw['type'] == "gatewaygroup") {
2279
			$detail = gateway_is_gwgroup_member($gwname, true);
2280
			foreach($detail as $gwitem) {
2281
				if ($gwitem['name'] == $defgw['name']) {
2282
					if (isset($gwitem['tier'])) {
2283
						$result = "Tier " . $gwitem['tier'];
2284
						break;
2285
					}
2286
				}
2287
			}
2288
		}
2289
    }
2290

    
2291
	if (!empty($result)) {
2292
		if ($gw['ipprotocol'] == "inet") {
2293
			$result .= " (IPv4)";
2294
		} elseif ($gw['ipprotocol'] == "inet6") {
2295
			$result .= " (IPv6)";
2296
		}
2297
	}
2298

    
2299
	return $result;
2300
}
2301

    
2302
function get_gateway_or_group_by_name($gwname, $gateways_arr) {
2303
	global $config;
2304

    
2305
	foreach ($gateways_arr as $gw) {
2306
		if ($gw['name'] == $gwname) {
2307
			$gw['type'] = 'gateway';
2308
			return $gw;
2309
		}
2310
	}
2311

    
2312
	init_config_arr(array('gateways', 'gateway_group'));
2313
	foreach ($config['gateways']['gateway_group'] as $gwg) {
2314
		if ($gwg['name'] == $gwname) {
2315
			$gwg['type'] = 'gatewaygroup';
2316
			return $gwg;
2317
		}
2318
	}
2319

    
2320
	return false;
2321
}
2322

    
2323
// Compose a list of available gateways but without the need to call return_gateways_array() multiple times
2324
// Previously that function could be called eight times per gateway!
2325
function available_default_gateways() {
2326
	global $config;
2327

    
2328
	$gways = return_gateways_array(true, false, true, true);
2329

    
2330
	$items4 = array();
2331
	$items6 = array();
2332
	$items4[''] = "Automatic";
2333
	$items6[''] = "Automatic";
2334
	foreach($gways as $gw) {
2335
		$gwn = $gw['name'];
2336
		if ($gw['ipprotocol'] == "inet6") {
2337
			$items6[$gwn] = $gwn;
2338
		} else {
2339
			$items4[$gwn] = $gwn;
2340
		}
2341
	}
2342

    
2343
	$groups = return_gateway_groups_array(false, $gways);
2344
	foreach ($groups as $key => $group) {
2345
		$gwn = $group['descr'];
2346
		if ($group['ipprotocol'] == "inet6") {
2347
			$items6[$key] = "$key ($gwn)";
2348
		} else {
2349
			$items4[$key] = "$key ($gwn)";
2350
		}
2351
	}
2352
	$items4['-'] = "None";
2353
	$items6['-'] = "None";
2354

    
2355
	$defaults = array();
2356
	$defaults['v4'] = $items4;
2357
	$defaults['v6'] = $items6;
2358
	$defaults['defaultgw4'] = $config['gateways']['defaultgw4'];
2359
	$defaults['defaultgw6'] = $config['gateways']['defaultgw6'];
2360

    
2361
	return $defaults;
2362
}
2363

    
2364

    
2365
?>
(21-21/61)