Project

General

Profile

Download (73.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * gwlb.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2008 Bill Marquette, Seth Mos
7
 * Copyright (c) 2008-2013 BSD Perimeter
8
 * Copyright (c) 2013-2016 Electric Sheep Fencing
9
 * Copyright (c) 2014-2020 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

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

    
43
function running_dpinger_processes() {
44
	global $g;
45

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

    
48
	$result = array();
49
	if ($pidfiles === FALSE) {
50
		return $result;
51
	}
52

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

    
68
	return $result;
69
}
70

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

    
79
	$running_processes = running_dpinger_processes();
80

    
81
	foreach ($running_processes as $running_gwname => $process) {
82
		if ($gwname != '' && $running_gwname != $gwname) {
83
			continue;
84
		}
85

    
86
		if (isvalidpid($process['pidfile'])) {
87
			killbypid($process['pidfile'], 3);
88
		} else {
89
			@unlink($process['pidfile']);
90
		}
91
	}
92
}
93

    
94
function start_dpinger($gateway) {
95
	global $g;
96

    
97
	if (!isset($gateway['gwifip'])) {
98
		return (false);
99
	}
100

    
101
	$dpinger_defaults = return_dpinger_defaults();
102

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

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

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

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

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

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

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

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

    
161
	$params .= "-L " .
162
	    (isset($gateway['losshigh']) && is_numeric($gateway['losshigh'])
163
	    ?  $gateway['losshigh']
164
	    : $dpinger_defaults['losshigh']
165
	    ) . " ";
166

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

    
170
	/* Do not try to bind IPv6 where interface is in tentative state */
171
	if (is_ipaddrv6($gateway['gwifip'])) {
172
		$err = interface_wait_tentative(get_real_interface(
173
		    $gateway['interface']));
174
		if ($err == false) {
175
			log_error(gettext("Timeout waiting for IPv6 address in tentative state.  dpinger will not run."));
176
			return (false);
177
		}
178
	}
179

    
180
	/* Redirect stdout to /dev/null to avoid exec() to wait for dpinger */
181
	return mwexec("/usr/local/bin/dpinger {$params} {$gateway['monitor']} >/dev/null");
182
}
183

    
184
/*
185
 * Starts dpinger processes and adds appropriate static routes for monitor IPs
186
 */
187
function setup_gateways_monitor() {
188
	global $config, $g;
189

    
190
	$gateways_arr = return_gateways_array();
191
	if (!is_array($gateways_arr)) {
192
		log_error(gettext("No gateways to monitor. dpinger will not run."));
193
		stop_dpinger();
194
		return;
195
	}
196

    
197
	$monitor_ips = array();
198
	foreach ($gateways_arr as $gwname => $gateway) {
199
		/* Do not monitor if such was requested */
200
		if (isset($gateway['monitor_disable'])) {
201
			continue;
202
		}
203
		if (empty($gateway['monitor']) || !is_ipaddr($gateway['monitor'])) {
204
			if (is_ipaddr($gateway['gateway'])) {
205
				$gateways_arr[$gwname]['monitor'] = $gateway['gateway'];
206
			} else { /* No chance to get an ip to monitor skip target. */
207
				continue;
208
			}
209
		}
210

    
211
		/* if the monitor address is already used before, skip */
212
		if (in_array($gateway['monitor'], $monitor_ips)) {
213
			continue;
214
		}
215

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

    
229
			if ($gwifip == "0.0.0.0") {
230
				continue; //Skip this target - the gateway is still waiting for DHCP
231
			}
232

    
233
			/*
234
			 * If the gateway is the same as the monitor we do not add a
235
			 * route as this will break the routing table.
236
			 * Add static routes for each gateway with their monitor IP
237
			 * not strictly necessary but is a added level of protection.
238
			 */
239
			if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
240
				log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
241
				$route_to = "-host {$gateway['monitor']}";
242
				if (interface_isppp_type($gateway['friendlyiface'])) {
243
					route_add_or_change("{$route_to} -iface {$gateway['interface']}");
244
				} else {
245
					route_add_or_change("{$route_to} {$gateway['gateway']}");
246
				}
247

    
248
				pfSense_kill_states("0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmp");
249
			}
250
		} else if ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
251
			if (is_linklocal($gateway['gateway']) &&
252
			    get_ll_scope($gateway['gateway']) == '') {
253
				$gateway['gateway'] .= '%' . $gateway['interface'];
254
			}
255

    
256
			if (is_linklocal($gateway['monitor'])) {
257
				if (get_ll_scope($gateway['monitor']) == '') {
258
					$gateways_arr[$gwname]['monitor'] .= '%' . $gateway['interface'];
259
				}
260

    
261
				$gwifip = find_interface_ipv6_ll($gateway['interface'], true);
262

    
263
				if (get_ll_scope($gwifip) == '') {
264
					$gwifip .= '%' . $gateway['interface'];
265
				}
266
			} else {
267
				$gwifip = find_interface_ipv6($gateway['interface'], true);
268
			}
269

    
270
			if (!is_ipaddrv6($gwifip)) {
271
				continue; //Skip this target
272
			}
273

    
274
			/*
275
			 * If the gateway is the same as the monitor we do not add a
276
			 * route as this will break the routing table.
277
			 * Add static routes for each gateway with their monitor IP
278
			 * not strictly necessary but is a added level of protection.
279
			 */
280
			if ($gateway['gateway'] != $gateway['monitor']) {
281
				log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
282
				$route_to = "-host -inet6 {$gateway['monitor']}";
283
				if (interface_isppp_type($gateway['friendlyiface'])) {
284
					route_add_or_change("{$route_to} -iface {$gateway['interface']}");
285
				} else {
286
					route_add_or_change("{$route_to} {$gateway['gateway']}");
287
				}
288

    
289
				pfSense_kill_states("::0.0.0.0/0", $gateway['monitor'], $gateway['interface'], "icmpv6");
290
			}
291
		} else {
292
			continue;
293
		}
294

    
295
		$monitor_ips[] = $gateway['monitor'];
296
		$gateways_arr[$gwname]['enable_dpinger'] = true;
297
		$gateways_arr[$gwname]['gwifip'] = $gwifip;
298
	}
299

    
300
	stop_dpinger();
301

    
302
	/* Start new processes */
303
	foreach ($gateways_arr as $gateway) {
304
		if (!isset($gateway['enable_dpinger'])) {
305
			continue;
306
		}
307

    
308
		if (start_dpinger($gateway) != 0) {
309
			log_error(sprintf(gettext("Error starting gateway monitor for %s"), $gateway['name']));
310
		}
311
	}
312

    
313
	return;
314
}
315

    
316
function get_dpinger_status($gwname, $detailed = false, $gways = false) {
317
	global $g;
318

    
319
	$running_processes = running_dpinger_processes();
320

    
321
	if (!isset($running_processes[$gwname])) {
322
		log_error(sprintf(gettext('dpinger: No dpinger session running for gateway %s'), $gwname));
323
		return false;
324
	}
325

    
326
	$proc = $running_processes[$gwname];
327
	unset($running_processes);
328

    
329
	$timeoutcounter = 0;
330
	while (true) {
331
		if (!file_exists($proc['socket'])) {
332
			log_error("dpinger: status socket {$proc['socket']} not found");
333
			return false;
334
		}
335
		$fp = @stream_socket_client("unix://{$proc['socket']}", $errno, $errstr, 10);
336
		if (!$fp) {
337
			log_error(sprintf(gettext('dpinger: cannot connect to status socket %1$s - %2$s (%3$s)'), $proc['socket'], $errstr, $errno));
338
			return false;
339
		}
340

    
341
		$status = '';
342
		while (!feof($fp)) {
343
			$status .= fgets($fp, 1024);
344
		}
345
		fclose($fp);
346

    
347
		$r = array();
348
		list(
349
			$r['gwname'],
350
			$r['latency_avg'],
351
			$r['latency_stddev'],
352
			$r['loss']
353
		) = explode(' ', preg_replace('/\n/', '', $status));
354

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

    
360
		if ($ready) {
361
			break;
362
		} else {
363
			$timeoutcounter++;
364
			if ($timeoutcounter > 300) {
365
				log_error(sprintf(gettext('dpinger: timeout while retrieving status for gateway %s'), $gwname));
366
				return false;
367
			}
368
			usleep(10000);
369
		}
370
	}
371

    
372
	$r['srcip'] = $proc['srcip'];
373
	$r['targetip'] = $proc['targetip'];
374

    
375
	if (is_array($gways)) {
376
		$gateways_arr = $gways;
377
	} else {
378
		$gateways_arr = return_gateways_array();
379
	}
380

    
381
	unset($gw);
382
	if (isset($gateways_arr[$gwname])) {
383
		$gw = $gateways_arr[$gwname];
384
	}
385

    
386
	$r['latency_avg'] = round($r['latency_avg']/1000, 3);
387
	$r['latency_stddev'] = round($r['latency_stddev']/1000, 3);
388

    
389
	$r['status'] = "none";
390
	if (isset($gw) && isset($gw['force_down'])) {
391
		$r['status'] = "force_down";
392
	} else if (isset($gw)) {
393
		$settings = return_dpinger_defaults();
394

    
395
		$keys = array(
396
		    'latencylow',
397
		    'latencyhigh',
398
		    'losslow',
399
		    'losshigh'
400
		);
401

    
402
		/* Replace default values by user-defined */
403
		foreach ($keys as $key) {
404
			if (isset($gw[$key]) && is_numeric($gw[$key])) {
405
				$settings[$key] = $gw[$key];
406
			}
407
		}
408

    
409
		if ($r['latency_avg'] > $settings['latencyhigh']) {
410
			if ($detailed) {
411
				$r['status'] = "highdelay";
412
			} else {
413
				$r['status'] = "down";
414
			}
415
		} else if ($r['loss'] > $settings['losshigh']) {
416
			if ($detailed) {
417
				$r['status'] = "highloss";
418
			} else {
419
				$r['status'] = "down";
420
			}
421
		} else if ($r['latency_avg'] > $settings['latencylow']) {
422
			$r['status'] = "delay";
423
		} else if ($r['loss'] > $settings['losslow']) {
424
			$r['status'] = "loss";
425
		}
426
	}
427

    
428
	return $r;
429
}
430

    
431
/* return the status of the dpinger targets as an array */
432
function return_gateways_status($byname = false, $gways = false) {
433
	global $config, $g;
434

    
435
	$dpinger_gws = running_dpinger_processes();
436
	$status = array();
437

    
438
	if (is_array($gways)) {
439
		$gateways_arr = $gways;
440
	} else {
441
		$gateways_arr = return_gateways_array();
442
	}
443

    
444
	foreach ($dpinger_gws as $gwname => $gwdata) {
445
		// If action is disabled for this gateway, then we want a detailed status.
446
		// That reports "highdelay" or "highloss" rather than just "down".
447
		// Because reporting the gateway down would be misleading (gateway action is disabled)
448
		$detailed = $gateways_arr[$gwname]['action_disable'];
449
		$dpinger_status = get_dpinger_status($gwname, $detailed);
450
		if ($dpinger_status === false) {
451
			continue;
452
		}
453

    
454
		if ($byname == false) {
455
			$target = $dpinger_status['targetip'];
456
		} else {
457
			$target = $gwname;
458
		}
459

    
460
		$status[$target] = array();
461
		$status[$target]['monitorip'] = $dpinger_status['targetip'];
462
		$status[$target]['srcip'] = $dpinger_status['srcip'];
463
		$status[$target]['name'] = $gwname;
464
		$status[$target]['delay'] = empty($dpinger_status['latency_avg']) ? "0ms" : $dpinger_status['latency_avg'] . "ms";
465
		$status[$target]['stddev'] = empty($dpinger_status['latency_stddev']) ? "0ms" : $dpinger_status['latency_stddev'] . "ms";
466
		$status[$target]['loss'] = empty($dpinger_status['loss']) ? "0.0%" : round($dpinger_status['loss'], 1) . "%";
467
		$status[$target]['status'] = $dpinger_status['status'];
468
	}
469

    
470
	/* tack on any gateways that have monitoring disabled
471
	 * or are down, which could cause gateway groups to fail */
472
	$gateways_arr = return_gateways_array();
473
	foreach ($gateways_arr as $gwitem) {
474
		if (!isset($gwitem['monitor_disable'])) {
475
			continue;
476
		}
477
		if (!is_ipaddr($gwitem['monitor'])) {
478
			$realif = $gwitem['interface'];
479
			$tgtip = get_interface_gateway($realif);
480
			if (!is_ipaddr($tgtip)) {
481
				$tgtip = "none";
482
			}
483
			$srcip = find_interface_ip($realif);
484
		} else {
485
			$tgtip = $gwitem['monitor'];
486
			$srcip = find_interface_ip($realif);
487
		}
488
		if ($byname == true) {
489
			$target = $gwitem['name'];
490
		} else {
491
			$target = $tgtip;
492
		}
493

    
494
		/* failsafe for down interfaces */
495
		if ($target == "none") {
496
			$target = $gwitem['name'];
497
			$status[$target]['name'] = $gwitem['name'];
498
			$status[$target]['delay'] = "0.0ms";
499
			$status[$target]['loss'] = "100.0%";
500
			$status[$target]['status'] = "down";
501
		} else {
502
			$status[$target]['monitorip'] = $tgtip;
503
			$status[$target]['srcip'] = $srcip;
504
			$status[$target]['name'] = $gwitem['name'];
505
			$status[$target]['delay'] = "";
506
			$status[$target]['loss'] = "";
507
			$status[$target]['status'] = "none";
508
		}
509

    
510
		$status[$target]['monitor_disable'] = true;
511
	}
512
	return($status);
513
}
514

    
515
function return_gateways_status_text($byname = false, $brief = false) {
516
	$gwstat = return_gateways_status($byname);
517
	$output = "";
518
	$widths = array();
519
	$col_sep = 2;
520
	if ($brief) {
521
		$collist = array('status' => "Status");
522
	} else {
523
		$collist = array('monitorip' => "Monitor",
524
				'srcip' => "Source",
525
				'delay' => "Delay",
526
				'stddev' => "StdDev",
527
				'loss' => "Loss",
528
				'status' => "Status");
529
	}
530
	foreach ($gwstat as $gw) {
531
		foreach ($gw as $gwdidx => $gwdata) {
532
			if (strlen($gwdata) > $widths[$gwdidx]) {
533
				$widths[$gwdidx] = strlen($gwdata);
534
			}
535
		}
536
	}
537

    
538
	$output .= str_pad("Name", $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
539
	foreach ($collist as $hdrcol => $hdrdesc) {
540
		if (strlen($hdrdesc) > $widths[$hdrcol]) {
541
			$widths[$hdrcol] = strlen($hdrdesc);
542
		}
543
		$output .= str_pad($hdrdesc, $widths[$hdrcol] + $col_sep, " ", (substr($hdrcol, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
544
	}
545
	$output .= "\n";
546

    
547
	foreach ($gwstat as $idx => $gw) {
548
		$output .= str_pad($gw['name'], $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
549
		foreach (array_keys($collist) as $col) {
550
			$output .= str_pad($gw[$col], $widths[$col] + $col_sep, " ", (substr($col, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
551
		}
552
		$output .= "\n";
553
	}
554

    
555
	return $output;
556
}
557

    
558
function compare_gateway_order_configured($a, $b) {
559
	/* XXX WAN always has precedence */
560
	if ($a['friendlyiface'] == "wan") {
561
		return -1;
562
	} elseif ($b['friendlyiface'] == "wan") {
563
		return 1;
564
	}
565

    
566
	if ($a['attribute'] === $b['attribute']) {
567
		if ($a['attribute'] === 'system') {
568
			$res = (($a['name'] < $b['name'])) ? -1 : 1;
569
			return $res;
570
		}
571
		return 0;
572
	}
573
	if ($a['attribute'] === 'system' || $b['attribute'] === 'system') {
574
		$res = (($b['attribute'] === 'system')) ? -1 : 1;
575
		return $res;
576
	}
577
	$res = ($a['attribute'] < $b['attribute']) ? -1 : 1;
578
	return $res;
579
}
580

    
581
function order_gateways_as_configured($gateways_arr) {
582
	uasort($gateways_arr, 'compare_gateway_order_configured');
583
	return $gateways_arr;
584
}
585

    
586
/* Return all configured gateways on the system
587
   $disabled = true - include gateways that are disabled
588
   $localhost = true - include "Null" entries for localhost IP addresses
589
   $inactive = true - include gateways on inactive interfaces
590
   $integer_index = true - index the returned array by integers 0,1,2,... instead of by GW name
591
*/
592
function return_gateways_array($disabled = false, $localhost = false, $inactive = false, $integer_index = false) {
593
	global $config, $g;
594

    
595
	$gateways_arr = array();
596
	$gateways_arr_temp = array();
597
	$cgw4 = getcurrentdefaultgatewayip('inet');
598
	$cgw6 = getcurrentdefaultgatewayip('inet6');
599
	$found_defaultv4 = 0;
600
	$found_defaultv6 = 0;
601

    
602
	// Ensure the interface cache is up to date first
603
	$interfaces = get_interface_arr(true);
604

    
605
	$i = -1;
606
	/* Process/add all the configured gateways. */
607
	if (is_array($config['gateways']) && is_array($config['gateways']['gateway_item'])) {
608
		foreach ($config['gateways']['gateway_item'] as $gateway) {
609
			if (!is_array($gateway) || empty($gateway)) {
610
				continue;
611
			}
612

    
613
			/* Increment it here to do not skip items */
614
			$i++;
615
			if (isset($gateway['defaultgw'])) {
616
				unset($gateway['defaultgw']);
617
			}
618

    
619
			if (empty($config['interfaces'][$gateway['interface']])) {
620
				if ($inactive === false) {
621
					continue;
622
				} else {
623
					$gateway['inactive'] = true;
624
				}
625
			}
626
			$wancfg = $config['interfaces'][$gateway['interface']];
627

    
628
			/* skip disabled interfaces */
629
			if ($disabled === false && (!isset($wancfg['enable']))) {
630
				continue;
631
			}
632

    
633
			/* if the gateway is dynamic and we can find the IPv4, Great! */
634
			if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
635
				if ($gateway['ipprotocol'] == "inet") {
636
					/* we know which interfaces is dynamic, this should be made a function */
637
					$gateway['gateway'] = get_interface_gateway($gateway['interface']);
638
					/* no IP address found, set to dynamic */
639
					if (!is_ipaddrv4($gateway['gateway'])) {
640
						$gateway['gateway'] = "dynamic";
641
					}
642
					$gateway['dynamic'] = true;
643
				}
644

    
645
				/* if the gateway is dynamic and we can find the IPv6, Great! */
646
				else if ($gateway['ipprotocol'] == "inet6") {
647
					/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
648
					$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
649
					/* no IPv6 address found, set to dynamic */
650
					if (!is_ipaddrv6($gateway['gateway'])) {
651
						$gateway['gateway'] = "dynamic";
652
					}
653
					$gateway['dynamic'] = true;
654
				}
655
			} else {
656
				/* getting this detection right is hard at this point because we still don't
657
				 * store the address family in the gateway item */
658
				if (is_ipaddrv4($gateway['gateway'])) {
659
					$gateway['ipprotocol'] = "inet";
660
				} else if (is_ipaddrv6($gateway['gateway'])) {
661
					$gateway['ipprotocol'] = "inet6";
662
				}
663
			}
664

    
665
			if (isset($gateway['monitor_disable'])) {
666
				$gateway['monitor_disable'] = true;
667
			} else if (empty($gateway['monitor'])) {
668
				$gateway['monitor'] = $gateway['gateway'];
669
			}
670

    
671
			if (isset($gateway['action_disable'])) {
672
				$gateway['action_disable'] = true;
673
			}
674

    
675
			$gateway['friendlyiface'] = $gateway['interface'];
676
			$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($gateway['interface']);
677

    
678
			/* special treatment for tunnel interfaces */
679
			if ($gateway['ipprotocol'] == "inet6") {
680
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
681
			} else {
682
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet", false, false);
683
			}
684

    
685
			if ($gateway['ipprotocol'] == "inet" &&
686
					($gateway['gateway'] == $cgw4)) {
687
				$gateway['isdefaultgw'] = true;
688
				$found_defaultv4 = 1;
689
			} else if ($gateway['ipprotocol'] == "inet6" &&
690
					($gateway['gateway'] == $cgw6)) {
691
				$gateway['isdefaultgw'] = true;
692
				$found_defaultv6 = 1;
693
			}
694
			/* include the gateway index as the attribute */
695
			$gateway['attribute'] = $i;
696

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

    
701
			/* skip disabled gateways if the caller has not asked for them to be returned. */
702
			if (!($disabled === false && isset($gateway['disabled']))) {
703
				$gateways_arr[$gateway['name']] = $gateway;
704
			}
705
		}
706
	}
707
	unset($gateway);
708

    
709
	//Sort the array by GW name before moving on.
710
	ksort($gateways_arr, SORT_STRING | SORT_FLAG_CASE);
711

    
712
	/* Loop through all interfaces with a gateway and add it to a array */
713
	if ($disabled == false) {
714
		$iflist = get_configured_interface_with_descr();
715
	} else {
716
		$iflist = get_configured_interface_with_descr(true);
717
	}
718

    
719
	/* Process/add dynamic v4 gateways. */
720
	foreach ($iflist as $ifname => $friendly) {
721
		if (!interface_has_gateway($ifname)) {
722
			continue;
723
		}
724

    
725
		if (empty($config['interfaces'][$ifname])) {
726
			continue;
727
		}
728

    
729
		$ifcfg = &$config['interfaces'][$ifname];
730
		if (!isset($ifcfg['enable'])) {
731
			continue;
732
		}
733

    
734
		if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
735
			continue;
736
		}
737

    
738
		$ctype = "";
739
		switch ($ifcfg['ipaddr']) {
740
			case "dhcp":
741
			case "pppoe":
742
			case "l2tp":
743
			case "pptp":
744
			case "ppp":
745
				$ctype = strtoupper($ifcfg['ipaddr']);
746
				break;
747
			default:
748
				$tunnelif = substr($ifcfg['if'], 0, 3);
749
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
750
					switch (substr($ifcfg['if'], 4, 1)) {
751
						case "c":
752
							$ovpntype = "openvpn-client";
753
							break;
754
						case "s":
755
							$ovpntype = "openvpn-server";
756
							break;
757
						default:
758
							// unknown ovpn type
759
							continue 3;
760
					}
761
					$ovpnid = substr($ifcfg['if'], 5);
762
					if (is_array($config['openvpn'][$ovpntype])) {
763
						foreach ($config['openvpn'][$ovpntype] as & $ovpnconf) {
764
							if ($ovpnconf['vpnid'] == $ovpnid) {
765
								// skip IPv6-only interfaces
766
								if ($ovpnconf['create_gw'] == "v6only") {
767
									continue 3;
768
								}
769
								// skip tap interfaces
770
								if ($ovpnconf['dev_mode'] == "tap") {
771
									continue 3;
772
								}
773
							}
774
						}
775
					}
776
					$ctype = "VPNv4";
777
				} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
778
					$ikeid = substr($ifcfg['if'], 5);
779
					if (is_array($config['ipsec']) && is_array($config['ipsec']['phase1']) && is_array($config['ipsec']['phase2'])) {
780
						foreach ($config['ipsec']['phase1'] as $ph1ent) {
781
							if ($ph1ent['disabled']) {
782
								continue;
783
							}
784
							$vtisubnet_spec = ipsec_vti($ph1ent, true);
785
							// Skip non-VTI tunnels
786
							if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
787
								continue;
788
							}
789
							if (!isset($ph1ent['mobile']) && ($keyexchange == 'ikev1' || isset($ph1ent['splitconn']))) {
790
								foreach ($vtisubnet_spec as $idx => $vtisub) {
791
									if ($ifcfg['if'] == "ipsec{$ph1ent['ikeid']}00{$idx}") {
792
										// If this specific VTI remote is v4, then we can make a v4 gw
793
										if (is_ipaddrv4($vtisub['right'])) {
794
											$ctype = "VTIv4";
795
										}
796
									}
797
								}
798
							} else {
799
								if ($ifcfg['if'] == "ipsec{$ph1ent['ikeid']}000") {
800
									// If any of the VTI remotes are v4, then we can make a v4 gw
801
									foreach ($vtisubnet_spec as $vtisub) {
802
										if (is_ipaddrv4($vtisub['right'])) {
803
											$ctype = "VTIv4";
804
										}
805
									}
806
								}
807
							}
808
						}
809
						if (empty($ctype)) {
810
							continue 2;
811
						}
812
					}
813
				} elseif ($tunnelif == "gif" || $tunnelif == "gre") {
814
					$ctype = "TUNNELv4";
815
				}
816
				break;
817
		}
818
		$ctype = "_". strtoupper($ctype);
819

    
820
		$gateway = array();
821
		$gateway['dynamic'] = false;
822
		$gateway['ipprotocol'] = "inet";
823
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
824
		$gateway['interface'] = get_real_interface($ifname);
825
		$gateway['friendlyiface'] = $ifname;
826
		$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
827
		$gateway['name'] = "{$friendly}{$ctype}";
828
		$gateway['attribute'] = "system";
829

    
830
		if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
831
			$gateway['isdefaultgw'] = true;
832
			$gateway['dynamic'] = true;
833
			$found_defaultv4 = 1;
834
		}
835

    
836
		/* Loopback dummy for dynamic interfaces without a IP */
837
		if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
838
			$gateway['gateway'] = "dynamic";
839
		}
840

    
841
		/* automatically skip known static and dynamic gateways that were previously processed */
842
		foreach ($gateways_arr_temp as $gateway_item) {
843
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
844
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
845
				continue 2;
846
			}
847
		}
848

    
849
		if (is_ipaddrv4($gateway['gateway'])) {
850
			$gateway['monitor'] = $gateway['gateway'];
851
		}
852

    
853
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
854
		$gateways_arr[$gateway['name']] = $gateway;
855
	}
856
	unset($gateway);
857

    
858
	/* Process/add dynamic v6 gateways. */
859
	foreach ($iflist as $ifname => $friendly) {
860
		/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
861
		if (!isset($config['system']['ipv6allow'])) {
862
			break;
863
		}
864

    
865
		if (!interface_has_gatewayv6($ifname)) {
866
			continue;
867
		}
868

    
869
		if (empty($config['interfaces'][$ifname])) {
870
			continue;
871
		}
872

    
873
		$ifcfg = &$config['interfaces'][$ifname];
874
		if (!isset($ifcfg['enable'])) {
875
			continue;
876
		}
877

    
878
		if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
879
			continue;
880
		}
881

    
882
		$ctype = "";
883
		switch ($ifcfg['ipaddrv6']) {
884
			case "slaac":
885
			case "dhcp6":
886
			case "6to4":
887
			case "6rd":
888
				$ctype = strtoupper($ifcfg['ipaddrv6']);
889
				break;
890
			default:
891
				$tunnelif = substr($ifcfg['if'], 0, 3);
892
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
893
					switch (substr($ifcfg['if'], 4, 1)) {
894
						case "c":
895
							$ovpntype = "openvpn-client";
896
							break;
897
						case "s":
898
							$ovpntype = "openvpn-server";
899
							break;
900
						default:
901
							// unknown ovpn type
902
							continue 3;
903
					}
904
					$ovpnid = substr($ifcfg['if'], 5);
905
					if (is_array($config['openvpn'][$ovpntype])) {
906
						foreach ($config['openvpn'][$ovpntype] as & $ovpnconf) {
907
							if ($ovpnconf['vpnid'] == $ovpnid) {
908
								// skip IPv4-only interfaces
909
								if ($ovpnconf['create_gw'] == "v4only") {
910
									continue 3;
911
								}
912
								// skip tap interfaces
913
								if ($ovpnconf['dev_mode'] == "tap") {
914
									continue 3;
915
								}
916
							}
917
						}
918
					}
919
					$ctype = "VPNv6";
920
				} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
921
					$ikeid = substr($ifcfg['if'], 5);
922
					if (is_array($config['ipsec']) && is_array($config['ipsec']['phase1']) && is_array($config['ipsec']['phase2'])) {
923
						foreach ($config['ipsec']['phase1'] as $ph1ent) {
924
							if ($ph1ent['disabled']) {
925
								continue;
926
							}
927
							$vtisubnet_spec = ipsec_vti($ph1ent, true);
928
							// Skip non-VTI tunnels
929
							if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
930
								continue;
931
							}
932
							if (!isset($ph1ent['mobile']) && ($keyexchange == 'ikev1' || isset($ph1ent['splitconn']))) {
933
								foreach ($vtisubnet_spec as $idx => $vtisub) {
934
									if ($ifcfg['if'] == "ipsec{$ph1ent['ikeid']}00{$idx}") {
935
										// If this specific VTI remote is v6, then we can make a v6 gw
936
										if (is_ipaddrv6($vtisub['right'])) {
937
											$ctype = "VTIv6";
938
										}
939
									}
940
								}
941
							} else {
942
								if ($ifcfg['if'] == "ipsec{$ph1ent['ikeid']}000") {
943
									// If any of the VTI remotes are v6, then we can make a v6 gw
944
									foreach ($vtisubnet_spec as $vtisub) {
945
										if (is_ipaddrv6($vtisub['right'])) {
946
											$ctype = "VTIv6";
947
										}
948
									}
949
								}
950
							}
951
						}
952
						if (empty($ctype)) {
953
							continue 2;
954
						}
955
					}
956
				} else if ($tunnelif == "gif" || $tunnelif == "gre") {
957
					$ctype = "TUNNELv6";
958
				}
959
				break;
960
		}
961
		$ctype = "_". strtoupper($ctype);
962

    
963
		$gateway = array();
964
		$gateway['dynamic'] = false;
965
		$gateway['ipprotocol'] = "inet6";
966
		$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
967
		$gateway['interface'] = get_real_interface($ifname, "inet6");
968
		switch ($ifcfg['ipaddrv6']) {
969
			case "6rd":
970
			case "6to4":
971
				$gateway['dynamic'] = "default";
972
				break;
973
		}
974
		$gateway['friendlyiface'] = $ifname;
975
		$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
976
		$gateway['name'] = "{$friendly}{$ctype}";
977
		$gateway['attribute'] = "system";
978

    
979
		if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) {
980
			$gateway['isdefaultgw'] = true;
981
			$gateway['dynamic'] = true;
982
			$found_defaultv6 = 1;
983
		}
984

    
985
		/* Loopback dummy for dynamic interfaces without a IP */
986
		if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
987
			$gateway['gateway'] = "dynamic";
988
		}
989

    
990
		/* automatically skip known static and dynamic gateways that were previously processed */
991
		foreach ($gateways_arr_temp as $gateway_item) {
992
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
993
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
994
				continue 2;
995
			}
996
		}
997

    
998
		if (is_ipaddrv6($gateway['gateway'])) {
999
			$gateway['monitor'] = $gateway['gateway'];
1000
		}
1001

    
1002
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
1003
		$gateways_arr[$gateway['name']] = $gateway;
1004
	}
1005
	unset($gateway);
1006

    
1007
	/* FIXME: Should this be enabled.
1008
	 * Some interface like wan might be default but have no info recorded
1009
	 * the config. */
1010
	/* this is a fallback if all else fails and we want to get packets out @smos */
1011
	if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
1012
		foreach ($gateways_arr as &$gateway) {
1013
			if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
1014
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw")) {
1015
					$gateway['isdefaultgw'] = true;
1016
					$found_defaultv4 = 1;
1017
				}
1018
			}
1019
			else if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
1020
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgwv6")) {
1021
					$gateway['isdefaultgw'] = true;
1022
					$found_defaultv6 = 1;
1023
				}
1024
			}
1025
		}
1026
	}
1027

    
1028
	if ($localhost === true) {
1029
		/* attach localhost for Null routes */
1030
		$gwlo4 = array();
1031
		$gwlo4['name'] = "Null4";
1032
		$gwlo4['interface'] = "lo0";
1033
		$gwlo4['ipprotocol'] = "inet";
1034
		$gwlo4['gateway'] = "127.0.0.1";
1035
		$gwlo4['attribute'] = "system";
1036
		$gwlo6 = array();
1037
		$gwlo6['name'] = "Null6";
1038
		$gwlo6['interface'] = "lo0";
1039
		$gwlo6['ipprotocol'] = "inet6";
1040
		$gwlo6['gateway'] = "::1";
1041
		$gwlo6['attribute'] = "system";
1042
		$gateways_arr['Null4'] = $gwlo4;
1043
		$gateways_arr['Null6'] = $gwlo6;
1044
	}
1045

    
1046
	if ($integer_index) {
1047
		$gateways_arr = array_values($gateways_arr);
1048
	}
1049

    
1050
	if ($found_defaultv4 != 1 && is_ipaddr($cgw4)) {
1051
		foreach($gateways_arr as &$gw) {
1052
			if ($gw['gateway'] == $cgw4) {
1053
				$gw['isdefaultgw'] = true;
1054
			}
1055
		}
1056
	}
1057
	if ($found_defaultv6 != 1 && is_ipaddr($cgw6)) {
1058
		foreach($gateways_arr as &$gw) {
1059
			if ($gw['gateway'] == $cgw6) {
1060
				$gw['isdefaultgw'] = true;
1061
			}
1062
		}
1063
	}
1064

    
1065
	$gways = order_gateways_as_configured($gateways_arr);
1066

    
1067
	// Add the tier names here so that system_gateways.php doesn't need to
1068
	foreach ($gways as $idx => $gway) {
1069
		$gways[$idx]['tiername'] = gateway_getgwtiername($gways, $idx);
1070
	}
1071

    
1072
	return $gways;
1073
}
1074

    
1075
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) {
1076
	global $config, $g;
1077
	/*
1078
	 * NOTE: The code below is meant to replace the default gateway when it goes down.
1079
	 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
1080
	 */
1081
	$set_dfltgwname = '';
1082

    
1083
	if ($ipprotocol == 'inet') {
1084
		$gwdefault = $config['gateways']['defaultgw4'];
1085
	} else {
1086
		$gwdefault = $config['gateways']['defaultgw6'];
1087
	}
1088
	if ($gwdefault == "-") {
1089
		// 'none', dont set the default gateway, useful if routes are managed by frr/bgp/ospf or similar
1090
		return;
1091
	}
1092
	if (isset($gateways_arr[$gwdefault])) {
1093
		// the configured gateway is a regular one. (not a gwgroup) use it as is..
1094
		$set_dfltgwname = $gwdefault;
1095
	} elseif (empty($gwdefault)) {
1096
		// 'automatic' mode, pick the first one thats 'up' or 'unmonitored' which is always considered up
1097
		$gateways_arr = order_gateways_as_configured($gateways_arr);
1098
		$fallback = "";
1099
		foreach($gateways_arr as $gwname => $gwsttng) {
1100
			if ($gwsttng['ipprotocol'] != $ipprotocol) {
1101
				continue;
1102
			}
1103

    
1104
			if ((isset($gwsttng['monitor_disable']) || isset($gwsttng['action_disable']) || $gateways_status[$gwname]['status'] == "none")) {
1105
				$set_dfltgwname = $gwname;
1106
				break;
1107
			}
1108
			if (empty($fallback) && $gwsttng['interface'] != 'lo0') {
1109
				$fallback = $gwname;
1110
			}
1111
		}
1112
		if (empty($set_dfltgwname)) {
1113
			openlog("", LOG_PID, LOG_LOCAL0);
1114
			log_error(sprintf("Gateway, none 'available' for %s, use the first one configured. '%s'", $ipprotocol, $fallback));
1115
			$set_dfltgwname = $fallback;
1116
		}
1117
	} else {
1118
		// a gwgroup is selected
1119
		// find the best available gateway given options available..
1120
		$gwg_members = array();
1121
		$viplist = get_configured_vip_list();
1122
		if (is_array($config['gateways']['gateway_group'])) {
1123
			foreach ($config['gateways']['gateway_group'] as $group) {
1124
				if ($group['name'] == $gwdefault) {
1125
					// finds the gw members of the best available tier for this group.
1126
					$gwg_members = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1127
				}
1128
			}
1129
		}
1130

    
1131
		if (count($gwg_members) > 0) {
1132
			$currentdefaultgwip = getcurrentdefaultgatewayip($ipprotocol);
1133
			$found_current = false;
1134
			foreach($gwg_members as $gwgroupitem) {
1135
				if (!empty($currentdefaultgwip) && ($gwgroupitem['gwip'] == $currentdefaultgwip)) {
1136
					$set_dfltgwname = $gwgroupitem['gw'];
1137
					$found_current = true;
1138
					if (isset($config['system']['gw-debug'])) {
1139
						log_error("Keep current gateway, its already part of the group members.");
1140
					}
1141
					break;
1142
				}
1143
			}
1144
			if (!$found_current) {
1145
				$set_dfltgwname = $gwg_members[0]['gw'];
1146
				log_error(sprintf("Gateway, switch to: %s", $set_dfltgwname));
1147
			}
1148
		} else {
1149
			log_error("Gateway, NONE AVAILABLE");
1150
		}
1151
	}
1152
	if (!empty($set_dfltgwname) && isset($gateways_arr[$set_dfltgwname])) {
1153
		setdefaultgateway($gateways_arr[$set_dfltgwname]);
1154
	}
1155
}
1156

    
1157
function getcurrentdefaultgatewayip($ipprotocol) {
1158
	return trim(exec("/sbin/route -n get -{$ipprotocol} default 2>/dev/null | /usr/bin/awk '/gateway:/ {print $2}'"), " \n");
1159
}
1160

    
1161
function setdefaultgateway($gw) {
1162
	global $g, $config;
1163
	if (isset($config['system']['route-debug'])) {
1164
		file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']}");
1165
	}
1166
	$ipprotocol = $gw['ipprotocol'];
1167
	if ($gw['gateway'] == "dynamic") {
1168
		if ($ipprotocol == 'inet') {
1169
			$gw['gateway'] = get_interface_gateway($gw['friendlyiface']);
1170
		} else {
1171
			$gw['gateway'] = get_interface_gateway_v6($$gw['friendlyiface']);
1172
		}
1173
	}
1174
	if ($ipprotocol == 'inet6' && !is_ipaddrv6($gw['gateway'])) {
1175
		return;
1176
	}
1177
	if ($ipprotocol == 'inet' && !is_ipaddrv4($gw['gateway'])) {
1178
		return;
1179
	}
1180
	if ($ipprotocol == 'inet6') {
1181
		if (is_linklocal($gw['gateway']) && get_ll_scope($gw['gateway']) == '') {
1182
			$gw['gateway'] .= "%" . $gw['interface'];
1183
		}
1184
	}
1185
	$currentdefaultgwip = getcurrentdefaultgatewayip($ipprotocol);
1186
	if ($currentdefaultgwip != $gw['gateway']) {
1187
		log_error("Default gateway setting {$gw['descr']} as default.");
1188

    
1189
		if ($ipprotocol == 'inet') {
1190
			array_map('unlink', glob("{$g['tmp_path']}/*_defaultgw", GLOB_BRACE));
1191
		} else {
1192
			array_map('unlink', glob("{$g['tmp_path']}/*_defaultgwv6", GLOB_BRACE));
1193
		}
1194
		$defaultif = get_real_interface($gw['interface']);
1195
		if ($defaultif) {
1196
			@file_put_contents("{$g['tmp_path']}/{$defaultif}_defaultgw", $gw['gateway']);
1197
		}
1198

    
1199
		if (isset($gw["nonlocalgateway"])) {
1200
			if (is_ipaddr($gw['gateway']) && !empty($gw['interface'])) {
1201
				route_add_or_change("-{$ipprotocol} {$gw['gateway']} -iface {$gw['interface']}");
1202
			}
1203
		}
1204
		if (isset($config['system']['route-debug'])) {
1205
			file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']} ({$gw['gateway']})");
1206
		}
1207
		route_add_or_change("-{$ipprotocol} default {$gw['gateway']}");
1208
		return true;
1209
	}
1210
}
1211

    
1212
function get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist){
1213
	$result = array();
1214
	/* create array with group gateways members separated by tier */
1215
	$tiers = array();
1216
	$backupplan = array();
1217
	$gwvip_arr = array();
1218
	foreach ($group['item'] as $item) {
1219
		list($gwname, $tier, $vipname) = explode("|", $item);
1220

    
1221
		if (is_ipaddr($viplist[$vipname])) {
1222
			if (!is_array($gwvip_arr[$group['name']])) {
1223
				$gwvip_arr[$group['name']] = array();
1224
			}
1225
			$gwvip_arr[$group['name']][$gwname] = $vipname;
1226
		}
1227

    
1228
		/* Do it here rather than reiterating again the group in case no member is up. */
1229
		if (!is_array($backupplan[$tier])) {
1230
			$backupplan[$tier] = array();
1231
		}
1232
		$backupplan[$tier][] = $gwname;
1233

    
1234
		/* check if the gateway is available before adding it to the array */
1235
		if (is_array($gateways_status[$gwname])) {
1236
			$status = $gateways_status[$gwname];
1237
			$gwdown = false;
1238
			if (stristr($status['status'], "down")) {
1239
				$msg = sprintf(gettext('MONITOR: %1$s is down, omitting from routing group %2$s'), $gwname, $group['name']);
1240
				$gwdown = true;
1241
			} else if (stristr($status['status'], "loss") && strstr($group['trigger'], "loss")) {
1242
				/* packet loss */
1243
				$msg = sprintf(gettext('MONITOR: %1$s has packet loss, omitting from routing group %2$s'), $gwname, $group['name']);
1244
				$gwdown = true;
1245
			} else if (stristr($status['status'], "delay") && strstr($group['trigger'] , "latency")) {
1246
				/* high latency */
1247
				$msg = sprintf(gettext('MONITOR: %1$s has high latency, omitting from routing group %2$s'), $gwname, $group['name']);
1248
				$gwdown = true;
1249
			}
1250
			$statuschanged = false;
1251
			$pluginparams = array();
1252
			$pluginparams['type'] = 'gateway';
1253
			$pluginparams['name'] = $gwname;
1254
			if ($gwdown == true) {
1255
				if (!file_exists("/tmp/.down.{$gwname}")) {
1256
					@touch("/tmp/.down.{$gwname}");
1257
					$msg .= "\n".implode("|", $status);
1258
					$pluginparams['event'] = 'gateway.down';
1259
					$statuschanged = true;
1260
				}
1261
			} else {
1262
				/* Online add member */
1263
				if (!is_array($tiers[$tier])) {
1264
					$tiers[$tier] = array();
1265
				}
1266
				$tiers[$tier][] = $gwname;
1267
				if (unlink_if_exists("/tmp/.down.{$gwname}")) {
1268
					$msg = sprintf(getmypid () . gettext('MONITOR: %1$s is available now, adding to routing group %2$s'), $gwname, $group['name']);
1269
					$msg .= "\n".implode("|", $status);
1270
					$pluginparams['event'] = 'gateway.up';
1271
					$statuschanged = true;
1272
				}
1273
			}
1274
			if ($statuschanged) {
1275
				log_error($msg);
1276
				notify_all_remote($msg);
1277
				if (isset($gateways_arr[$gwname]['interface'])) {
1278
					$pluginparams['interface'] = $gateways_arr[$gwname]['interface'];
1279
				}
1280
				pkg_call_plugins('plugin_gateway', $pluginparams);
1281
			}
1282
		} else if (isset($gateways_arr[$gwname]['monitor_disable']) || isset($gateways_arr[$gwname]['action_disable'])) {
1283
			$tiers[$tier][] = $gwname;
1284
		}
1285
	}
1286
	$tiers_count = count($tiers);
1287
	if ($tiers_count == 0) {
1288
		/* Oh dear, we have no members! Engage Plan B */
1289
		if (isset($config['system']['gw-debug']) && (!platform_booting())) {
1290
			$msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']);
1291
			log_error($msg);
1292
		}
1293
		$tiers = $backupplan;
1294
	}
1295
	/* sort the tiers array by the tier key */
1296
	ksort($tiers);
1297

    
1298
	/* we do not really foreach the tiers as we stop after the first tier */
1299
	foreach ($tiers as $tieridx => $tier) {
1300
		/* process all gateways in this tier */
1301
		foreach ($tier as $member) {
1302
			/* determine interface gateway */
1303
			if (isset($gateways_arr[$member])) {
1304
				$gateway = $gateways_arr[$member];
1305
				$int = $gateway['interface'];
1306
				$gatewayip = "";
1307
				if (is_ipaddr($gateway['gateway'])) {
1308
					$gatewayip = $gateway['gateway'];
1309
				} else if (!empty($int)) {
1310
					$gatewayip = get_interface_gateway($gateway['friendlyiface']);
1311
				}
1312

    
1313
				if (!empty($int)) {
1314
					$result['ipprotocol'] = $gateway['ipprotocol'];
1315
					if (is_ipaddr($gatewayip)) {
1316
						$groupmember = array();
1317
						$groupmember['gw'] = $member;
1318
						$groupmember['int'] = $int;
1319
						$groupmember['gwip'] = $gatewayip;
1320
						$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
1321
						if (is_array($gwvip_arr[$group['name']]) && !empty($gwvip_arr[$group['name']][$member])) {
1322
							$groupmember['vip'] = $gwvip_arr[$group['name']][$member];
1323
						}
1324
						$result[] = $groupmember;
1325
					}
1326
				}
1327
			}
1328
		}
1329
		/* we should have the 1st available tier now, exit stage left */
1330
		if (count($result) > 0) {
1331
			break;
1332
		} else {
1333
			log_error(sprintf(gettext('GATEWAYS: Group %1$s did not have any gateways up on tier %2$s!'), $group['name'], $tieridx));
1334
		}
1335
	}
1336
	// Add description field last to not influence the count() above
1337
	$result['descr'] = $group['descr'];
1338
	return $result;
1339
}
1340

    
1341
function get_gwgroup_members($groupname) {
1342
	global $config;
1343
	$gateways_status = return_gateways_status(true);
1344
	$gateways_arr = return_gateways_array();
1345
	$viplist = get_configured_vip_list();
1346
	foreach ($config['gateways']['gateway_group'] as $group) {
1347
		if ($group['name'] == $groupname) {
1348
			return get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1349
		}
1350
	}
1351
	return array();
1352
}
1353

    
1354
/*
1355
 * Return an array with all gateway groups with name as key
1356
 * All gateway groups will be processed before returning the array.
1357
 */
1358
function return_gateway_groups_array($fixup = false, $gways = false) {
1359
	global $config;
1360

    
1361
	/* fetch the current gateways status */
1362
	if (is_array($gways)) {
1363
		$gateways_status = $gways;
1364
	} else {
1365
		$gateways_status = return_gateways_status(true);
1366
	}
1367

    
1368
	$gateways_arr = return_gateways_array();
1369
	$gateway_groups_array = array();
1370
	if ($fixup == true) {
1371
		$gw4 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw4'], $gways);
1372
		$gw6 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw6'], $gways);
1373
		if ($gw4 && $gw4['type'] == 'gatewaygroup') {
1374
			fixup_default_gateway("inet", $gateways_status, $gateways_arr);
1375
		}
1376
		if ($gw6 && $gw6['type'] == 'gatewaygroup') {
1377
			fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
1378
		}
1379
	}
1380
	if (is_array($config['gateways']['gateway_group'])) {
1381
		$viplist = get_configured_vip_list();
1382
		foreach ($config['gateways']['gateway_group'] as $group) {
1383
			$gateway_groups_array[$group['name']] = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1384
		}
1385
	}
1386

    
1387
	return ($gateway_groups_array);
1388
}
1389

    
1390
/* Update DHCP WAN Interface ip address in gateway group item */
1391
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
1392
	global $config;
1393

    
1394
	if (is_array($config['gateways']['gateway_item'])) {
1395
		foreach ($config['gateways']['gateway_item'] as & $gw) {
1396
			if ($gw['interface'] != $interface) {
1397
				continue;
1398
			}
1399

    
1400
			$current_gw = get_interface_gateway($interface);
1401
			if ($gw['gateway'] <> $current_gw) {
1402
				$gw['gateway'] = $current_gw;
1403
				$changed = true;
1404
			}
1405
		}
1406
	}
1407

    
1408
	if ($changed && $current_gw) {
1409
		write_config(sprintf(gettext(
1410
		    'Updating gateway group gateway for %1$s - new gateway is %2$s'),
1411
		    $interface, $current_gw));
1412
	}
1413
}
1414

    
1415
function lookup_gateway_or_group_by_name($gwname, $gways = false) {
1416
	global $config;
1417

    
1418
	if (is_array($gways)) {
1419
		$gateways_arr = $gways;
1420
	} else {
1421
		$gateways_arr = return_gateways_array();
1422
	}
1423

    
1424
	foreach ($gateways_arr as $gw) {
1425
		if ($gw['name'] == $gwname) {
1426
			$gw['type'] = 'gateway';
1427
			return $gw;
1428
		}
1429
	}
1430

    
1431
	if (is_array($config['gateways']['gateway_group'])) {
1432
		foreach ($config['gateways']['gateway_group'] as $gwg) {
1433
			if ($gwg['name'] == $gwname) {
1434
				$gwg['type'] = 'gatewaygroup';
1435
				return $gwg;
1436
			}
1437
		}
1438
	}
1439

    
1440
	return false;
1441
}
1442

    
1443
function lookup_gateway_ip_by_name($name, $disabled = false) {
1444

    
1445
	$gateways_arr = return_gateways_array($disabled, true);
1446
	foreach ($gateways_arr as $gname => $gw) {
1447
		if ($gw['name'] === $name || $gname === $name) {
1448
			return $gw['gateway'];
1449
		}
1450
	}
1451

    
1452
	return false;
1453
}
1454

    
1455
function lookup_gateway_monitor_ip_by_name($name) {
1456

    
1457
	$gateways_arr = return_gateways_array(false, true);
1458
	if (!empty($gateways_arr[$name])) {
1459
		$gateway = $gateways_arr[$name];
1460
		if (!is_ipaddr($gateway['monitor'])) {
1461
			return $gateway['gateway'];
1462
		}
1463

    
1464
		return $gateway['monitor'];
1465
	}
1466

    
1467
	return (false);
1468
}
1469

    
1470
function lookup_gateway_interface_by_name($name) {
1471

    
1472
	$gateways_arr = return_gateways_array(false, true);
1473
	if (!empty($gateways_arr[$name])) {
1474
		$interfacegw = $gateways_arr[$name]['friendlyiface'];
1475
		return ($interfacegw);
1476
	}
1477

    
1478
	return (false);
1479
}
1480

    
1481
function get_root_interface($interface) {
1482
	if (substr($interface, 0, 4) == '_vip') {
1483
		$interface = get_configured_vip_interface($interface);
1484
		if (substr($interface, 0, 4) == '_vip') {
1485
			$interface = get_configured_vip_interface($interface);
1486
		}
1487
	}
1488
	return $interface;
1489
}
1490

    
1491
function get_interface_gateway($interface, &$dynamic = false) {
1492
	global $config, $g;
1493

    
1494
	$interface = get_root_interface($interface);
1495

    
1496
	$gw = NULL;
1497
	$gwcfg = $config['interfaces'][$interface];
1498
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
1499
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1500
			if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
1501
				$gw = $gateway['gateway'];
1502
				break;
1503
			}
1504
		}
1505
	}
1506

    
1507
	// for dynamic interfaces we handle them through the $interface_router file.
1508
	if (($gw == NULL || !is_ipaddrv4($gw)) && !is_ipaddrv4($gwcfg['ipaddr'])) {
1509
		$realif = get_real_interface($interface);
1510
		if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
1511
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
1512
			$dynamic = true;
1513
		}
1514
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw")) {
1515
			$dynamic = "default";
1516
		}
1517

    
1518
	}
1519

    
1520
	/* return gateway */
1521
	return ($gw);
1522
}
1523

    
1524
function get_interface_gateway_v6($interface, &$dynamic = false) {
1525
	global $config, $g;
1526

    
1527
	$interface = get_root_interface($interface);
1528

    
1529
	$gw = NULL;
1530
	$gwcfg = $config['interfaces'][$interface];
1531
	if (!empty($gwcfg['gatewayv6']) && is_array($config['gateways']['gateway_item'])) {
1532
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1533
			if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
1534
				$gw = $gateway['gateway'];
1535
				break;
1536
			}
1537
		}
1538
	}
1539

    
1540
	// for dynamic interfaces we handle them through the $interface_router file.
1541
	if (($gw == NULL || !is_ipaddrv6($gw)) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
1542
		$realif = get_real_interface($interface);
1543
		if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
1544
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
1545
			$dynamic = true;
1546
		}
1547
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6")) {
1548
			$dynamic = "default";
1549
		}
1550
	}
1551
	/* return gateway */
1552
	return ($gw);
1553
}
1554

    
1555
/* Check a IP address against a gateway IP or name
1556
 * to verify it's address family */
1557
function validate_address_family($ipaddr, $gwname, $disabled = false) {
1558
	$v4ip = false;
1559
	$v6ip = false;
1560
	$v4gw = false;
1561
	$v6gw = false;
1562

    
1563
	if (is_ipaddrv4($ipaddr)) {
1564
		$v4ip = true;
1565
	}
1566
	if (is_ipaddrv6($ipaddr)) {
1567
		$v6ip = true;
1568
	}
1569
	if (is_ipaddrv4($gwname)) {
1570
		$v4gw = true;
1571
	}
1572
	if (is_ipaddrv6($gwname)) {
1573
		$v6gw = true;
1574
	}
1575

    
1576
	if ($v4ip && $v4gw) {
1577
		return true;
1578
	}
1579
	if ($v6ip && $v6gw) {
1580
		return true;
1581
	}
1582

    
1583
	/* still no match, carry on, lookup gateways */
1584
	if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname, $disabled))) {
1585
		$v4gw = true;
1586
	}
1587
	if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname, $disabled))) {
1588
		$v6gw = true;
1589
	}
1590

    
1591
	$gw_array = return_gateways_array();
1592
	if (is_array($gw_array[$gwname])) {
1593
		switch ($gw_array[$gwname]['ipprotocol']) {
1594
			case "inet":
1595
				$v4gw = true;
1596
				break;
1597
			case "inet6":
1598
				$v6gw = true;
1599
				break;
1600
		}
1601
	}
1602

    
1603
	if ($v4ip && $v4gw) {
1604
		return true;
1605
	}
1606
	if ($v6ip && $v6gw) {
1607
		return true;
1608
	}
1609

    
1610
	return false;
1611
}
1612

    
1613
/* check if a interface is part of a gateway group */
1614
function interface_gateway_group_member($interface, $gwgroup_name = "") {
1615
	global $config;
1616

    
1617
	if (is_array($config['gateways']['gateway_group'])) {
1618
		$groups = $config['gateways']['gateway_group'];
1619
	} else {
1620
		return false;
1621
	}
1622

    
1623
	$gateways_arr = return_gateways_array(false, true);
1624
	foreach ($groups as $group) {
1625
		if (is_array($group['item'])) {
1626
			foreach ($group['item'] as $item) {
1627
				$elements = explode("|", $item);
1628
				$gwname = $elements[0];
1629
				if ($interface == $gateways_arr[$gwname]['interface'] &&
1630
				    (empty($gwgroup_name) || $gwgroup_name == $group['name'])) {
1631
					unset($gateways_arr);
1632
					return true;
1633
				}
1634
			}
1635
		}
1636
	}
1637
	unset($gateways_arr);
1638

    
1639
	return false;
1640
}
1641

    
1642
function gateway_is_gwgroup_member($name, $detail=false) {
1643
	global $config;
1644

    
1645
	if (is_array($config['gateways']['gateway_group'])) {
1646
		$groups = $config['gateways']['gateway_group'];
1647
	} else {
1648
		return false;
1649
	}
1650

    
1651
	$members = array();
1652
	foreach ($groups as $group) {
1653
		if (is_array($group['item'])) {
1654
			foreach ($group['item'] as $item) {
1655
				list($gwname, $tier, $vipname) = explode("|", $item);
1656
				if ($name == $gwname) {
1657
					if ($detail) {
1658
						$newitem = array();
1659
						$newitem['name'] = $group['name'];
1660
						$newitem['tier'] = $tier;
1661
						$newitem['vipname'] = $vipname;
1662
						$members[] = $newitem;
1663
					} else {
1664
						$members[] = $group['name'];
1665
					}
1666
				}
1667
			}
1668
		}
1669
	}
1670

    
1671
	return $members;
1672
}
1673
/*
1674
  Check the proposed gateway settings to see if they are valid.
1675
  $gateway_settings - the proposed array of proposed gateway settings
1676
  $id - the index of the gateway proposed to be modified (otherwise "" if adding a new gateway)
1677
  $parent_ip - the IP (v4 or v6) address about to be set on the corresponding interface (if any)
1678
  $parent_sn - the subnet about to be set on the corresponding interface (if any)
1679
  (Note: the above 2 parameters allow gateway parameters to be validated concurrently with saving
1680
   an interface, before the new interface parameters are actually saved in the config.)
1681
  Return completed $input_errors array if there is any problem.
1682
  Otherwise return an empty $input_errors array
1683
*/
1684
function validate_gateway($gateway_settings, $id = "", $parent_ip = "", $parent_sn = "") {
1685
	global $config;
1686

    
1687
	$a_gateways = return_gateways_array(true, false, true, true);
1688
	$input_errors = array();
1689

    
1690
	/* input validation */
1691
	$reqdfields = explode(" ", "name interface");
1692
	$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
1693

    
1694
	do_input_validation($gateway_settings, $reqdfields, $reqdfieldsn, $input_errors);
1695

    
1696
	if (!isset($gateway_settings['name'])) {
1697
		$input_errors[] = "A valid gateway name must be specified.";
1698
	}
1699
	if (!is_validaliasname($gateway_settings['name'])) {
1700
		$input_errors[] = invalidaliasnamemsg($gateway_settings['name'], gettext("gateway"));
1701
	} else if (isset($gateway_settings['disabled'])) {
1702
		// We have a valid gateway name that the user wants to mark as disabled.
1703
		// Check if the gateway name is used in any gateway group.
1704
		if (is_array($config['gateways']['gateway_group'])) {
1705
			foreach ($config['gateways']['gateway_group'] as $group) {
1706
				foreach ($group['item'] as $item) {
1707
					$items = explode("|", $item);
1708
					if ($items[0] == $gateway_settings['name']) {
1709
						$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']);
1710
					}
1711
				}
1712
			}
1713
		}
1714

    
1715
		// Check if the gateway name is used in any enabled Static Route.
1716
		if (is_array($config['staticroutes']['route'])) {
1717
			foreach ($config['staticroutes']['route'] as $route) {
1718
				if ($route['gateway'] == $gateway_settings['name']) {
1719
					if (!isset($route['disabled'])) {
1720
						// There is a static route that uses this gateway and is enabled (not disabled).
1721
						$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']);
1722
					}
1723
				}
1724
			}
1725
		}
1726
	}
1727
	/* skip system gateways which have been automatically added */
1728
	if (($gateway_settings['gateway'] && (!is_ipaddr($gateway_settings['gateway'])) && ($gateway_settings['attribute'] !== "system")) && ($gateway_settings['gateway'] != "dynamic")) {
1729
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
1730
	}
1731

    
1732
	if ($gateway_settings['gateway'] && is_ipaddr($gateway_settings['gateway'])) {
1733
		if (is_ipaddrv4($gateway_settings['gateway'])) {
1734
			if ($parent_ip == '') {
1735
				$parent_ip = get_interface_ip($gateway_settings['interface']);
1736
				$parent_sn = get_interface_subnet($gateway_settings['interface']);
1737
			}
1738
			if (empty($parent_ip) || empty($parent_sn)) {
1739
				$input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
1740
			} elseif (!isset($gateway_settings["nonlocalgateway"])) {
1741
				$subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
1742
				$vips = link_interface_to_vips($gateway_settings['interface']);
1743
				if (is_array($vips)) {
1744
					foreach ($vips as $vip) {
1745
						if (!is_ipaddrv4($vip['subnet'])) {
1746
							continue;
1747
						}
1748
						$subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
1749
					}
1750
				}
1751

    
1752
				$found = false;
1753
				foreach ($subnets as $subnet) {
1754
					if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
1755
						$found = true;
1756
						break;
1757
					}
1758
				}
1759

    
1760
				if ($found === false) {
1761
					$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
1762
				}
1763
			}
1764
		} else if (is_ipaddrv6($gateway_settings['gateway'])) {
1765
			/* do not do a subnet match on a link local address, it's valid */
1766
			if (!is_linklocal($gateway_settings['gateway'])) {
1767
				if ($parent_ip == '') {
1768
					$parent_ip = get_interface_ipv6($gateway_settings['interface']);
1769
					$parent_sn = get_interface_subnetv6($gateway_settings['interface']);
1770
				}
1771
				if (empty($parent_ip) || empty($parent_sn)) {
1772
					$input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
1773
				} elseif (!isset($gateway_settings["nonlocalgateway"])) {
1774
					$subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
1775
					$vips = link_interface_to_vips($gateway_settings['interface']);
1776
					if (is_array($vips)) {
1777
						foreach ($vips as $vip) {
1778
							if (!is_ipaddrv6($vip['subnet'])) {
1779
								continue;
1780
							}
1781
							$subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
1782
						}
1783
					}
1784

    
1785
					$found = false;
1786
					foreach ($subnets as $subnet) {
1787
						if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
1788
							$found = true;
1789
							break;
1790
						}
1791
					}
1792

    
1793
					if ($found === false) {
1794
						$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
1795
					}
1796
				}
1797
			}
1798
		}
1799

    
1800
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddr'])) {
1801
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddr']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
1802
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
1803
			}
1804
		}
1805
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddrv6'])) {
1806
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddrv6']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
1807
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
1808
			}
1809
		}
1810
	}
1811
	if (($gateway_settings['monitor'] != "") && ($gateway_settings['monitor'] != "dynamic")) {
1812
		validateipaddr($gateway_settings['monitor'], IPV4V6, "Monitor IP", $input_errors, false);
1813
	}
1814
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] < 0) {
1815
		$input_errors[] = gettext("A valid data payload must be specified.");
1816
	}
1817
	/* only allow correct IPv4 and IPv6 gateway addresses */
1818
	if (($gateway_settings['gateway'] <> "") && is_ipaddr($gateway_settings['gateway']) && $gateway_settings['gateway'] != "dynamic") {
1819
		if (is_ipaddrv6($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet")) {
1820
			$input_errors[] = sprintf(gettext("The IPv6 gateway address '%s' can not be used as a IPv4 gateway."), $gateway_settings['gateway']);
1821
		}
1822
		if (is_ipaddrv4($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet6")) {
1823
			$input_errors[] = sprintf(gettext("The IPv4 gateway address '%s' can not be used as a IPv6 gateway."), $gateway_settings['gateway']);
1824
		}
1825
	}
1826
	/* only allow correct IPv4 and IPv6 monitor addresses */
1827
	if (($gateway_settings['monitor'] <> "") && is_ipaddr($gateway_settings['monitor']) && $gateway_settings['monitor'] != "dynamic") {
1828
		if (is_ipaddrv6($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet")) {
1829
			$input_errors[] = sprintf(gettext("The IPv6 monitor address '%s' can not be used on a IPv4 gateway."), $gateway_settings['monitor']);
1830
		}
1831
		if (is_ipaddrv4($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet6")) {
1832
			$input_errors[] = sprintf(gettext("The IPv4 monitor address '%s' can not be used on a IPv6 gateway."), $gateway_settings['monitor']);
1833
		}
1834
	}
1835

    
1836
	if (isset($gateway_settings['name'])) {
1837
		/* check for overlaps */
1838
		foreach ($a_gateways as $gateway) {
1839
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
1840
				if ($gateway['name'] != $gateway_settings['name']) {
1841
					$input_errors[] = gettext("Changing name on a gateway is not allowed.");
1842
				}
1843
				continue;
1844
			}
1845
			if ($gateway_settings['name'] <> "") {
1846
				if (($gateway['name'] <> "") && ($gateway_settings['name'] == $gateway['name']) && ($gateway['attribute'] !== "system")) {
1847
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $gateway_settings['name']);
1848
					break;
1849
				}
1850
			}
1851
			if (is_ipaddr($gateway_settings['gateway'])) {
1852
				if (($gateway['gateway'] <> "") && ($gateway_settings['gateway'] == $gateway['gateway']) && ($gateway['attribute'] !== "system")) {
1853
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $gateway_settings['gateway']);
1854
					break;
1855
				}
1856
			}
1857
			if (is_ipaddr($gateway_settings['monitor'])) {
1858
				if (($gateway['monitor'] <> "") && ($gateway_settings['monitor'] == $gateway['monitor']) && ($gateway['attribute'] !== "system")) {
1859
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. A different monitor IP must be chosen.'), $gateway_settings['monitor']);
1860
					break;
1861
				}
1862
			}
1863
		}
1864
	}
1865

    
1866
	/* input validation of dpinger advanced parameters */
1867

    
1868
	$dpinger_default = return_dpinger_defaults();
1869
	$latencylow = $dpinger_default['latencylow'];
1870
	if ($gateway_settings['latencylow']) {
1871
		if (!is_numeric($gateway_settings['latencylow'])) {
1872
			$input_errors[] = gettext("The low latency threshold needs to be a numeric value.");
1873
		} else if ($gateway_settings['latencylow'] < 1) {
1874
			$input_errors[] = gettext("The low latency threshold needs to be positive.");
1875
		} else {
1876
			$latencylow = $gateway_settings['latencylow'];
1877
		}
1878
	}
1879

    
1880
	$latencyhigh = $dpinger_default['latencyhigh'];
1881
	if ($gateway_settings['latencyhigh']) {
1882
		if (!is_numeric($gateway_settings['latencyhigh'])) {
1883
			$input_errors[] = gettext("The high latency threshold needs to be a numeric value.");
1884
		} else if ($gateway_settings['latencyhigh'] < 1) {
1885
			$input_errors[] = gettext("The high latency threshold needs to be positive.");
1886
		} else {
1887
			$latencyhigh = $gateway_settings['latencyhigh'];
1888
		}
1889
	}
1890

    
1891
	$losslow = $dpinger_default['losslow'];
1892
	if ($gateway_settings['losslow']) {
1893
		if (!is_numeric($gateway_settings['losslow'])) {
1894
			$input_errors[] = gettext("The low Packet Loss threshold needs to be a numeric value.");
1895
		} else if ($gateway_settings['losslow'] < 1) {
1896
			$input_errors[] = gettext("The low Packet Loss threshold needs to be positive.");
1897
		} else if ($gateway_settings['losslow'] >= 100) {
1898
			$input_errors[] = gettext("The low Packet Loss threshold needs to be less than 100.");
1899
		} else {
1900
			$losslow = $gateway_settings['losslow'];
1901
		}
1902
	}
1903

    
1904
	$losshigh = $dpinger_default['losshigh'];
1905
	if ($gateway_settings['losshigh']) {
1906
		if (!is_numeric($gateway_settings['losshigh'])) {
1907
			$input_errors[] = gettext("The high Packet Loss threshold needs to be a numeric value.");
1908
		} else if ($gateway_settings['losshigh'] < 1) {
1909
			$input_errors[] = gettext("The high Packet Loss threshold needs to be positive.");
1910
		} else if ($gateway_settings['losshigh'] > 100) {
1911
			$input_errors[] = gettext("The high Packet Loss threshold needs to be 100 or less.");
1912
		} else {
1913
			$losshigh = $gateway_settings['losshigh'];
1914
		}
1915
	}
1916

    
1917
	$time_period = $dpinger_default['time_period'];
1918
	if ($gateway_settings['time_period']) {
1919
		if (!is_numeric($gateway_settings['time_period'])) {
1920
			$input_errors[] = gettext("The time period over which results are averaged needs to be a numeric value.");
1921
		} else if ($gateway_settings['time_period'] < 1) {
1922
			$input_errors[] = gettext("The time period over which results are averaged needs to be positive.");
1923
		} else {
1924
			$time_period = $gateway_settings['time_period'];
1925
		}
1926
	}
1927

    
1928
	$interval = $dpinger_default['interval'];
1929
	if ($gateway_settings['interval']) {
1930
		if (!is_numeric($gateway_settings['interval'])) {
1931
			$input_errors[] = gettext("The probe interval needs to be a numeric value.");
1932
		} else if ($gateway_settings['interval'] < 1) {
1933
			$input_errors[] = gettext("The probe interval needs to be positive.");
1934
		} else {
1935
			$interval = $gateway_settings['interval'];
1936
		}
1937
	}
1938

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

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

    
1961
	if ($latencylow >= $latencyhigh) {
1962
		$input_errors[] = gettext(
1963
		    "The high latency threshold needs to be greater than the low latency threshold");
1964
	}
1965

    
1966
	if ($losslow >= $losshigh) {
1967
		$input_errors[] = gettext(
1968
		    "The high packet loss threshold needs to be higher than the low packet loss threshold");
1969
	}
1970

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

    
1977
	// Ensure that the time period is greater than 2 times the probe interval plus the loss interval.
1978
	if (($interval * 2 + $loss_interval) >= $time_period) {
1979
		$input_errors[] = gettext("The time period needs to be greater than twice the probe interval plus the loss interval.");
1980
	}
1981

    
1982
	// There is no point recalculating the average latency and loss more often than the probe interval.
1983
	// So the alert interval needs to be >= probe interval.
1984
	if ($interval > $alert_interval) {
1985
		$input_errors[] = gettext("The alert interval needs to be greater than or equal to the probe interval.");
1986
	}
1987

    
1988
	return $input_errors;
1989
}
1990

    
1991
// Save gateway settings.
1992
// $gateway_settings - the array of gateway setting parameters
1993
// $realid - the index of the gateway to be modified (otherwise "" if adding a new gateway)
1994

    
1995
// This function is responsible to:
1996
//   Setup the gateway parameter structure from the gateway settings input parameter
1997
//   Save the structure into the config
1998
//   Remove any run-time settings from gateway parameters that are changed (e.g. remove routes to addresses that are changing)
1999

    
2000
// A subsequent "apply" step will implement the added/changed gateway.
2001

    
2002
function save_gateway($gateway_settings, $realid = "") {
2003
	global $config;
2004

    
2005
	init_config_arr(array('gateways', 'gateway_item'));
2006
	$a_gateway_item = &$config['gateways']['gateway_item'];
2007
	$reloadif = "";
2008
	$gateway = array();
2009

    
2010
	if (empty($gateway_settings['interface'])) {
2011
		$gateway['interface'] = $gateway_settings['friendlyiface'];
2012
	} else {
2013
		$gateway['interface'] = $gateway_settings['interface'];
2014
	}
2015
	if (is_ipaddr($gateway_settings['gateway'])) {
2016
		$gateway['gateway'] = $gateway_settings['gateway'];
2017
	} else {
2018
		$gateway['gateway'] = "dynamic";
2019
	}
2020
	$gateway['name'] = $gateway_settings['name'];
2021
	$gateway['weight'] = $gateway_settings['weight'];
2022
	$gateway['ipprotocol'] = $gateway_settings['ipprotocol'];
2023
	if ($gateway_settings['interval']) {
2024
		$gateway['interval'] = $gateway_settings['interval'];
2025
	}
2026

    
2027
	if ($gateway_settings['time_period']) {
2028
		$gateway['time_period'] = $gateway_settings['time_period'];
2029
	}
2030
	if ($gateway_settings['alert_interval']) {
2031
		$gateway['alert_interval'] = $gateway_settings['alert_interval'];
2032
	}
2033

    
2034
	$gateway['descr'] = $gateway_settings['descr'];
2035
	if ($gateway_settings['monitor_disable'] == "yes") {
2036
		$gateway['monitor_disable'] = true;
2037
	}
2038
	if ($gateway_settings['action_disable'] == "yes") {
2039
		$gateway['action_disable'] = true;
2040
	}
2041
	if ($gateway_settings['nonlocalgateway'] == "yes") {
2042
		$gateway['nonlocalgateway'] = true;
2043
	}
2044
	if ($gateway_settings['force_down'] == "yes") {
2045
		$gateway['force_down'] = true;
2046
	}
2047
	if (is_ipaddr($gateway_settings['monitor'])) {
2048
		$gateway['monitor'] = $gateway_settings['monitor'];
2049
	}
2050
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] >= 0) {
2051
		$gateway['data_payload'] = $gateway_settings['data_payload'];
2052
	}
2053

    
2054
	/* NOTE: If gateway ip is changed need to cleanup the old static interface route */
2055
	if ($gateway_settings['monitor'] != "dynamic" && !empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['gateway']) &&
2056
		$gateway['gateway'] != $a_gateway_item[$realid]['gateway'] &&
2057
		isset($a_gateway_item[$realid]["nonlocalgateway"])) {
2058
		$realif = get_real_interface($a_gateway_item[$realid]['interface']);
2059
		$inet = (!is_ipaddrv4($a_gateway_item[$realid]['gateway']) ? "-inet6" : "-inet");
2060
		$rgateway = $a_gateway_item[$realid]['gateway'];
2061
		$cmd = "/sbin/route delete $inet " . escapeshellarg($a_gateway_item[$realid]['gateway']) . " -iface " . escapeshellarg($realif) . " " . escapeshellarg($rgateway);
2062
		mwexec($cmd);
2063
	}
2064

    
2065
	/* NOTE: If monitor ip is changed need to cleanup the old static route */
2066
	if ($gateway_settings['monitor'] != "dynamic" && !empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['monitor']) &&
2067
		$gateway_settings['monitor'] != $a_gateway_item[$realid]['monitor'] && $gateway['gateway'] != $a_gateway_item[$realid]['monitor']) {
2068
		$rgateway = $a_gateway_item[$realid]['gateway'];
2069
		if (is_ipaddrv4($a_gateway_item[$realid]['monitor'])) {
2070
			mwexec("/sbin/route delete " . escapeshellarg($a_gateway_item[$realid]['monitor']) . " " . escapeshellarg($rgateway));
2071
		} else {
2072
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateway_item[$realid]['monitor']) . " " . escapeshellarg($rgateway));
2073
		}
2074
	}
2075

    
2076
	if ($gateway_settings['defaultgw'] == "yes" || $gateway_settings['defaultgw'] == "on") {
2077
		// a new default gateway is being saved.
2078
		$i = 0;
2079
		/* remove the default gateway bits for all gateways with the same address family */
2080
		if (is_array($a_gateway_item)) {
2081
			foreach ($a_gateway_item as $gw) {
2082
				if ($gateway['ipprotocol'] == $gw['ipprotocol']) {
2083
					if ($gw['interface'] != $gateway_settings['interface'] &&
2084
						($gw['name'] == $config['gateways']['defaultgw4'] || $gw['name'] == $config['gateways']['defaultgw6'])) {
2085
						// remember the old default gateway interface to call with a "interface reconfigure" event.
2086
						$reloadif = $gw['interface'];
2087
					}
2088
				}
2089
				$i++;
2090
			}
2091
		}
2092
		if ($gateway['ipprotocol'] == "inet") {
2093
			$config['gateways']['defaultgw4'] = $gateway['name'];
2094
		} elseif ($gateway['ipprotocol'] == "inet6") {
2095
			$config['gateways']['defaultgw6'] = $gateway['name'];
2096
		}
2097
	}
2098

    
2099
	if ($gateway_settings['latencylow']) {
2100
		$gateway['latencylow'] = $gateway_settings['latencylow'];
2101
	}
2102
	if ($gateway_settings['latencyhigh']) {
2103
		$gateway['latencyhigh'] = $gateway_settings['latencyhigh'];
2104
	}
2105
	if ($gateway_settings['losslow']) {
2106
		$gateway['losslow'] = $gateway_settings['losslow'];
2107
	}
2108
	if ($gateway_settings['losshigh']) {
2109
		$gateway['losshigh'] = $gateway_settings['losshigh'];
2110
	}
2111
	if ($gateway_settings['loss_interval']) {
2112
		$gateway['loss_interval'] = $gateway_settings['loss_interval'];
2113
	}
2114
	/* when saving the manual gateway we use the attribute which has the corresponding id */
2115
	if (isset($realid) && $a_gateway_item[$realid]) {
2116
		$preserve_disabled = isset($a_gateway_item[$realid]['disabled']);
2117
		$a_gateway_item[$realid] = $gateway;
2118
		if ($preserve_disabled) {
2119
			$a_gateway_item[$realid]['disabled'] = true;
2120
		}
2121
	} else {
2122
		$a_gateway_item[] = $gateway;
2123
	}
2124
	gateway_set_enabled($gateway_settings['name'], !isset($gateway_settings['disabled']));
2125

    
2126
	mark_subsystem_dirty('staticroutes');
2127

    
2128
	write_config();
2129

    
2130
	if (!empty($reloadif)) {
2131
		send_event("interface reconfigure {$reloadif}");
2132
	}
2133
}
2134

    
2135
function gateway_set_enabled($name, $enabled) {
2136
	global $config;
2137
	if (is_array($config['gateways']['gateway_item'])) {
2138
		foreach($config['gateways']['gateway_item'] as &$item) {
2139
			if ($item['name'] == $name) {
2140
				$gateway = &$item;
2141
			}
2142
		}
2143
	}
2144
	if (!isset($gateway)) {
2145
		return;
2146
	}
2147
	if ($enabled) {
2148
		unset($gateway['disabled']);
2149
	} else {
2150
		/* Check if the gateway was enabled but changed to disabled. */
2151
		if (!isset($gateway['disabled'])) {
2152
			/*  If the disabled gateway was the default route, remove the default route */
2153
			if (is_ipaddr($gateway['gateway'])) {
2154
				$inet = (!is_ipaddrv4($gateway['gateway']) ? 'inet6' : 'inet');
2155
				if ($inet == 'inet') {
2156
					$cgw = getcurrentdefaultgatewayip('inet');
2157
				} else {
2158
					$cgw = getcurrentdefaultgatewayip('inet6');
2159
				}
2160
				if ($gateway['gateway'] == $cgw) {
2161
					mwexec("/sbin/route delete -{$inet} default " . escapeshellarg($cgw));
2162
				}
2163
			}
2164
			$gateway['disabled'] = true;
2165
		}
2166
	}
2167
}
2168

    
2169
function gateway_or_gwgroup_exists($name) {
2170
	global $config;
2171
	if (is_array($config['gateways']['gateway_item'])) {
2172
		foreach($config['gateways']['gateway_item'] as $item) {
2173
			if ($item['name'] == $name) {
2174
				return true;
2175
			}
2176
		}
2177
	}
2178
	if (is_array($config['gateways']['gateway_group'])) {
2179
		foreach($config['gateways']['gateway_group'] as $item) {
2180
			if ($item['name'] == $name) {
2181
				return true;
2182
			}
2183
		}
2184
	}
2185
	return false;
2186
}
2187

    
2188
// These two replacement functions avoid the need to call return_gateways_array() multiple times and
2189
// allow system_gateways.php to get everything it needs in a single function call
2190
function gateway_getgwtiername($gways, $idx) {
2191
	global $config;
2192

    
2193
	$result = "";
2194
	$gwname = $gways[$idx]['name'];
2195

    
2196
	$gw = get_gateway_or_group_by_name($gwname, $gways);
2197
	if ($config['gateways']['defaultgw4'] == $gwname || $config['gateways']['defaultgw6'] == $gwname) {
2198
		$result = "Default";
2199
	} else {
2200
		if ($gw['ipprotocol'] == 'inet') {
2201
			$defgw = get_gateway_or_group_by_name($config['gateways']['defaultgw4'], $gways);
2202
		} else {
2203
			$defgw = get_gateway_or_group_by_name($config['gateways']['defaultgw6'], $gways);
2204
		}
2205

    
2206
		if ($defgw['type'] == "gatewaygroup") {
2207
			$detail = gateway_is_gwgroup_member($gwname, true);
2208
			foreach($detail as $gwitem) {
2209
				if ($gwitem['name'] == $defgw['name']) {
2210
					if (isset($gwitem['tier'])) {
2211
						$result = "Tier " . $gwitem['tier'];
2212
						break;
2213
					}
2214
				}
2215
			}
2216
		}
2217
    }
2218

    
2219
	if (!empty($result)) {
2220
		if ($gw['ipprotocol'] == "inet") {
2221
			$result .= " (IPv4)";
2222
		} elseif ($gw['ipprotocol'] == "inet6") {
2223
			$result .= " (IPv6)";
2224
		}
2225
	}
2226

    
2227
	return $result;
2228
}
2229

    
2230
function get_gateway_or_group_by_name($gwname, $gateways_arr) {
2231
	global $config;
2232

    
2233
	foreach ($gateways_arr as $gw) {
2234
		if ($gw['name'] == $gwname) {
2235
			$gw['type'] = 'gateway';
2236
			return $gw;
2237
		}
2238
	}
2239

    
2240
	if (is_array($config['gateways']['gateway_group'])) {
2241
		foreach ($config['gateways']['gateway_group'] as $gwg) {
2242
			if ($gwg['name'] == $gwname) {
2243
				$gwg['type'] = 'gatewaygroup';
2244
				return $gwg;
2245
			}
2246
		}
2247
	}
2248

    
2249
	return false;
2250
}
2251

    
2252
// Compose a list of available gateways but without the need to call return_gateways_array() multiple times
2253
// Previously that function could be called eight times per gateway!
2254
function available_default_gateways() {
2255
	global $config;
2256

    
2257
	$gways = return_gateways_array(true, false, true, true);
2258

    
2259
	$items4 = array();
2260
	$items6 = array();
2261
	$items4[''] = "Automatic";
2262
	$items6[''] = "Automatic";
2263
	foreach($gways as $gw) {
2264
		$gwn = $gw['name'];
2265
		if ($gw['ipprotocol'] == "inet6") {
2266
			$items6[$gwn] = $gwn;
2267
		} else {
2268
			$items4[$gwn] = $gwn;
2269
		}
2270
	}
2271

    
2272
	$groups = return_gateway_groups_array(false, $gways);
2273
	foreach ($groups as $key => $group) {
2274
		$gwn = $group['descr'];
2275
		if ($group['ipprotocol'] == "inet6") {
2276
			$items6[$key] = "$key ($gwn)";
2277
		} else {
2278
			$items4[$key] = "$key ($gwn)";
2279
		}
2280
	}
2281
	$items4['-'] = "None";
2282
	$items6['-'] = "None";
2283

    
2284
	$defaults = array();
2285
	$defaults['v4'] = $items4;
2286
	$defaults['v6'] = $items6;
2287
	$defaults['defaultgw4'] = $config['gateways']['defaultgw4'];
2288
	$defaults['defaultgw6'] = $config['gateways']['defaultgw6'];
2289

    
2290
	return $defaults;
2291
}
2292

    
2293

    
2294
?>
(21-21/60)