Project

General

Profile

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

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

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

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

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

    
69
	return $result;
70
}
71

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

    
80
	$running_processes = running_dpinger_processes();
81

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

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

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

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

    
102
	$dpinger_defaults = return_dpinger_defaults();
103

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
253
				pfSense_kill_states("0.0.0.0/0", utf8_encode($gateway['monitor']), utf8_encode($gateway['interface']), "icmp");
254
			}
255
		} else if ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
256
			if (is_linklocal($gateway['gateway']) &&
257
			    get_ll_scope($gateway['gateway']) == '') {
258
				$gateway['gateway'] .= '%' . $gateway['interface'];
259
			}
260

    
261
			if (is_linklocal($gateway['monitor'])) {
262
				if (get_ll_scope($gateway['monitor']) == '') {
263
					$gateways_arr[$gwname]['monitor'] .= '%' . $gateway['interface'];
264
				}
265

    
266
				$gwifip = find_interface_ipv6_ll($gateway['interface'], true);
267

    
268
				if (get_ll_scope($gwifip) == '') {
269
					$gwifip .= '%' . $gateway['interface'];
270
				}
271
			} else {
272
				$gwifip = find_interface_ipv6($gateway['interface'], true);
273
			}
274

    
275
			if (!is_ipaddrv6($gwifip)) {
276
				continue; //Skip this target
277
			}
278

    
279
			/*
280
			 * If the gateway is the same as the monitor we do not add a
281
			 * route as this will break the routing table.
282
			 * Add static routes for each gateway with their monitor IP
283
			 * not strictly necessary but is a added level of protection.
284
			 */
285
			if ($gateway['gateway'] != $gateways_arr[$gwname]['monitor']) {
286
				log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
287
				if (interface_isppp_type($gateway['friendlyiface'])) {
288
					route_add_or_change($gateway['monitor'],
289
					    '', $gateway['interface']);
290
					system_staticroutes_configure($gateway['friendlyiface']);
291
				} else {
292
					route_add_or_change($gateway['monitor'],
293
					    $gateway['gateway']);
294
				}
295

    
296
				pfSense_kill_states("::0.0.0.0/0", utf8_encode($gateway['monitor']), utf8_encode($gateway['interface']), "icmpv6");
297
			}
298
		} else {
299
			continue;
300
		}
301

    
302
		$monitor_ips[] = $gateway['monitor'];
303
		$gateways_arr[$gwname]['enable_dpinger'] = true;
304
		$gateways_arr[$gwname]['gwifip'] = $gwifip;
305
	}
306

    
307
	stop_dpinger();
308

    
309
	/* Start new processes */
310
	foreach ($gateways_arr as $gateway) {
311
		if (!isset($gateway['enable_dpinger'])) {
312
			continue;
313
		}
314

    
315
		if (start_dpinger($gateway) != 0) {
316
			log_error(sprintf(gettext("Error starting gateway monitor for %s"), $gateway['name']));
317
		}
318
	}
319
	if (platform_booting()) {
320
		echo "done.\n";
321
	}
322

    
323
	return;
324
}
325

    
326
function get_dpinger_status($gwname, $gways = false, $action_disable = false) {
327
	global $g;
328

    
329
	$running_processes = running_dpinger_processes();
330

    
331
	if (!isset($running_processes[$gwname])) {
332
		log_error(sprintf(gettext(
333
		    'dpinger: No dpinger session running for gateway %s'),
334
		    $gwname));
335
		return false;
336
	}
337

    
338
	$proc = $running_processes[$gwname];
339
	unset($running_processes);
340

    
341
	$timeoutcounter = 0;
342
	while (true) {
343
		if (!file_exists($proc['socket'])) {
344
			log_error("dpinger: status socket {$proc['socket']} not found");
345
			return false;
346
		}
347
		$fp = @stream_socket_client("unix://{$proc['socket']}", $errno, $errstr, 10);
348
		if (!$fp) {
349
			log_error(sprintf(gettext('dpinger: cannot connect to status socket %1$s - %2$s (%3$s)'), $proc['socket'], $errstr, $errno));
350
			return false;
351
		}
352

    
353
		$status = '';
354
		while (!feof($fp)) {
355
			$status .= fgets($fp, 1024);
356
		}
357
		fclose($fp);
358

    
359
		$r = array();
360
		list(
361
			$r['gwname'],
362
			$r['latency_avg'],
363
			$r['latency_stddev'],
364
			$r['loss']
365
		) = explode(' ', preg_replace('/\n/', '', $status));
366

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

    
372
		if ($ready) {
373
			break;
374
		} else {
375
			$timeoutcounter++;
376
			if ($timeoutcounter > 300) {
377
				log_error(sprintf(gettext('dpinger: timeout while retrieving status for gateway %s'), $gwname));
378
				return false;
379
			}
380
			usleep(10000);
381
		}
382
	}
383

    
384
	$r['srcip'] = $proc['srcip'];
385
	$r['targetip'] = $proc['targetip'];
386

    
387
	if (is_array($gways)) {
388
		$gateways_arr = $gways;
389
	} else {
390
		$gateways_arr = return_gateways_array();
391
	}
392

    
393
	unset($gw);
394
	if (isset($gateways_arr[$gwname])) {
395
		$gw = $gateways_arr[$gwname];
396
	}
397

    
398
	$r['latency_avg'] = round($r['latency_avg']/1000, 3);
399
	$r['latency_stddev'] = round($r['latency_stddev']/1000, 3);
400

    
401
	$r['status'] = "online";
402
	$r['substatus'] = "none";
403
	if (isset($gw) && isset($gw['force_down'])) {
404
		$r['status'] = "down";
405
		$r['substatus'] = "force_down";
406
	} else if (isset($gw)) {
407
		$settings = return_dpinger_defaults();
408

    
409
		$keys = array(
410
		    'latencylow',
411
		    'latencyhigh',
412
		    'losslow',
413
		    'losshigh'
414
		);
415

    
416
		/* Replace default values by user-defined */
417
		foreach ($keys as $key) {
418
			if (isset($gw[$key]) && is_numeric($gw[$key])) {
419
				$settings[$key] = $gw[$key];
420
			}
421
		}
422

    
423
		if ($r['latency_avg'] > $settings['latencyhigh']) {
424
			if (!$action_disable) {
425
				$r['status'] = "down";
426
			}
427
			$r['substatus'] = "highdelay";
428
		} else if ($r['loss'] > $settings['losshigh']) {
429
			if (!$action_disable) {
430
				$r['status'] = "down";
431
			}
432
			$r['substatus'] = "highloss";
433
		} else if ($r['latency_avg'] > $settings['latencylow']) {
434
			$r['substatus'] = "delay";
435
		} else if ($r['loss'] > $settings['losslow']) {
436
			$r['substatus'] = "loss";
437
		}
438
	}
439

    
440
	return $r;
441
}
442

    
443
/* return the status of the dpinger targets as an array */
444
function return_gateways_status($byname = false, $gways = false) {
445
	global $config, $g;
446

    
447
	$dpinger_gws = running_dpinger_processes();
448
	$status = array();
449

    
450
	if (is_array($gways)) {
451
		$gateways_arr = $gways;
452
	} else {
453
		$gateways_arr = return_gateways_array();
454
	}
455

    
456
	foreach ($dpinger_gws as $gwname => $gwdata) {
457
		/*
458
		 * If action is disabled for this gateway, then we want a
459
		 * detailed status.  That reports "highdelay" or "highloss"
460
		 * rather than just "down".  Because reporting the gateway
461
		 * down would be misleading (gateway action is disabled)
462
		 */
463
		$action_disable = $gateways_arr[$gwname]['action_disable'];
464
		$dpinger_status = get_dpinger_status($gwname, $action_disable);
465
		if ($dpinger_status === false) {
466
			continue;
467
		}
468

    
469
		if ($byname == false) {
470
			$target = $dpinger_status['targetip'];
471
		} else {
472
			$target = $gwname;
473
		}
474

    
475
		$status[$target] = array();
476
		$status[$target]['monitorip'] = $dpinger_status['targetip'];
477
		$status[$target]['srcip'] = $dpinger_status['srcip'];
478
		$status[$target]['name'] = $gwname;
479
		$status[$target]['delay'] =
480
		    empty($dpinger_status['latency_avg'])
481
		    ? "0ms"
482
		    : $dpinger_status['latency_avg'] . "ms";
483
		$status[$target]['stddev'] =
484
		    empty($dpinger_status['latency_stddev'])
485
		    ? "0ms"
486
		    : $dpinger_status['latency_stddev'] . "ms";
487
		$status[$target]['loss'] = empty($dpinger_status['loss'])
488
		    ? "0.0%"
489
		    : round($dpinger_status['loss'], 1) . "%";
490
		$status[$target]['status'] = $dpinger_status['status'];
491
		$status[$target]['substatus'] = $dpinger_status['substatus'];
492
	}
493

    
494
	/* tack on any gateways that have monitoring disabled
495
	 * or are down, which could cause gateway groups to fail */
496
	$gateways_arr = return_gateways_array();
497
	foreach ($gateways_arr as $gwitem) {
498
		if (!isset($gwitem['monitor_disable'])) {
499
			continue;
500
		}
501
		if (!is_ipaddr($gwitem['monitor'])) {
502
			$realif = $gwitem['interface'];
503
			$tgtip = get_interface_gateway($realif);
504
			if (!is_ipaddr($tgtip)) {
505
				$tgtip = "none";
506
			}
507
			$srcip = find_interface_ip($realif);
508
		} else {
509
			$tgtip = $gwitem['monitor'];
510
			$srcip = find_interface_ip($realif);
511
		}
512
		if ($byname == true) {
513
			$target = $gwitem['name'];
514
		} else {
515
			$target = $tgtip;
516
		}
517

    
518
		/* failsafe for down interfaces */
519
		if ($target == "none") {
520
			$target = $gwitem['name'];
521
			$status[$target]['name'] = $gwitem['name'];
522
			$status[$target]['delay'] = "0.0ms";
523
			$status[$target]['loss'] = "100.0%";
524
			$status[$target]['status'] = "down";
525
			$status[$target]['substatus'] = "down";
526
		} else {
527
			$status[$target]['monitorip'] = $tgtip;
528
			$status[$target]['srcip'] = $srcip;
529
			$status[$target]['name'] = $gwitem['name'];
530
			$status[$target]['delay'] = "";
531
			$status[$target]['loss'] = "";
532
			$status[$target]['status'] = "online";
533
			$status[$target]['substatus'] = "none";
534
		}
535

    
536
		$status[$target]['monitor_disable'] = true;
537
	}
538
	return($status);
539
}
540

    
541
function return_gateways_status_text($byname = false, $brief = false) {
542
	$gwstat = return_gateways_status($byname);
543
	$output = "";
544
	$widths = array();
545
	$col_sep = 2;
546
	if ($brief) {
547
		$collist = array('status' => "Status");
548
	} else {
549
		$collist = array('monitorip' => "Monitor",
550
				'srcip' => "Source",
551
				'delay' => "Delay",
552
				'stddev' => "StdDev",
553
				'loss' => "Loss",
554
				'status' => "Status",
555
				'substatus' => "Substatus");
556
	}
557
	foreach ($gwstat as $gw) {
558
		foreach ($gw as $gwdidx => $gwdata) {
559
			if (strlen($gwdata) > $widths[$gwdidx]) {
560
				$widths[$gwdidx] = strlen($gwdata);
561
			}
562
		}
563
	}
564

    
565
	$output .= str_pad("Name", $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
566
	foreach ($collist as $hdrcol => $hdrdesc) {
567
		if (strlen($hdrdesc) > $widths[$hdrcol]) {
568
			$widths[$hdrcol] = strlen($hdrdesc);
569
		}
570
		$output .= str_pad($hdrdesc, $widths[$hdrcol] + $col_sep, " ", (substr($hdrcol, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
571
	}
572
	$output .= "\n";
573

    
574
	foreach ($gwstat as $idx => $gw) {
575
		$output .= str_pad($gw['name'], $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
576
		foreach (array_keys($collist) as $col) {
577
			$output .= str_pad($gw[$col], $widths[$col] + $col_sep, " ", (substr($col, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
578
		}
579
		$output .= "\n";
580
	}
581

    
582
	return $output;
583
}
584

    
585
function compare_gateway_order_configured($a, $b) {
586
	/* XXX WAN always has precedence */
587
	if ($a['friendlyiface'] == "wan") {
588
		return -1;
589
	} elseif ($b['friendlyiface'] == "wan") {
590
		return 1;
591
	}
592

    
593
	if ($a['attribute'] === $b['attribute']) {
594
		if ($a['attribute'] === 'system') {
595
			$res = (($a['name'] < $b['name'])) ? -1 : 1;
596
			return $res;
597
		}
598
		return 0;
599
	}
600
	if ($a['attribute'] === 'system' || $b['attribute'] === 'system') {
601
		$res = (($b['attribute'] === 'system')) ? -1 : 1;
602
		return $res;
603
	}
604
	$res = ($a['attribute'] < $b['attribute']) ? -1 : 1;
605
	return $res;
606
}
607

    
608
function order_gateways_as_configured($gateways_arr) {
609
	uasort($gateways_arr, 'compare_gateway_order_configured');
610
	return $gateways_arr;
611
}
612

    
613
/* Return all configured gateways on the system
614
   $disabled = true - include gateways that are disabled
615
   $localhost = true - include "Null" entries for localhost IP addresses
616
   $inactive = true - include gateways on inactive interfaces
617
   $integer_index = true - index the returned array by integers 0,1,2,... instead of by GW name
618
*/
619
function return_gateways_array($disabled = false, $localhost = false, $inactive = false, $integer_index = false) {
620
	global $config, $g;
621

    
622
	$gateways_arr = array();
623
	$gateways_arr_temp = array();
624
	$cgw4 = route_get_default('inet');
625
	$cgw6 = route_get_default('inet6');
626
	$found_defaultv4 = 0;
627
	$found_defaultv6 = 0;
628

    
629
	// Ensure the interface cache is up to date first
630
	$interfaces = get_interface_arr(true);
631

    
632
	$i = -1;
633
	/* Process/add all the configured gateways. */
634
	if (is_array($config['gateways']) && is_array($config['gateways']['gateway_item'])) {
635
		foreach ($config['gateways']['gateway_item'] as $gateway) {
636
			if (!is_array($gateway) || empty($gateway)) {
637
				continue;
638
			}
639

    
640
			/* Increment it here to do not skip items */
641
			$i++;
642
			if (isset($gateway['defaultgw'])) {
643
				unset($gateway['defaultgw']);
644
			}
645

    
646
			if (empty($config['interfaces'][$gateway['interface']])) {
647
				if ($inactive === false) {
648
					continue;
649
				} else {
650
					$gateway['inactive'] = true;
651
				}
652
			}
653
			$wancfg = $config['interfaces'][$gateway['interface']];
654

    
655
			/* skip disabled interfaces */
656
			if ($disabled === false && (!isset($wancfg['enable']))) {
657
				continue;
658
			}
659

    
660
			/* if the gateway is dynamic and we can find the IPv4, Great! */
661
			if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
662
				if ($gateway['ipprotocol'] == "inet") {
663
					/* we know which interfaces is dynamic, this should be made a function */
664
					$gateway['gateway'] = get_interface_gateway($gateway['interface']);
665
					/* no IP address found, set to dynamic */
666
					if (!is_ipaddrv4($gateway['gateway'])) {
667
						$gateway['gateway'] = "dynamic";
668
					}
669
					$gateway['dynamic'] = true;
670
				}
671

    
672
				/* if the gateway is dynamic and we can find the IPv6, Great! */
673
				else if ($gateway['ipprotocol'] == "inet6") {
674
					/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
675
					$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
676
					/* no IPv6 address found, set to dynamic */
677
					if (!is_ipaddrv6($gateway['gateway'])) {
678
						$gateway['gateway'] = "dynamic";
679
					}
680
					$gateway['dynamic'] = true;
681
				}
682
			} else {
683
				/* getting this detection right is hard at this point because we still don't
684
				 * store the address family in the gateway item */
685
				if (is_ipaddrv4($gateway['gateway'])) {
686
					$gateway['ipprotocol'] = "inet";
687
				} else if (is_ipaddrv6($gateway['gateway'])) {
688
					$gateway['ipprotocol'] = "inet6";
689
				}
690
			}
691

    
692
			if (isset($gateway['monitor_disable'])) {
693
				$gateway['monitor_disable'] = true;
694
			} else if (empty($gateway['monitor'])) {
695
				$gateway['monitor'] = $gateway['gateway'];
696
			}
697

    
698
			if (isset($gateway['action_disable'])) {
699
				$gateway['action_disable'] = true;
700
			}
701

    
702
			$gateway['friendlyiface'] = $gateway['interface'];
703
			$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($gateway['interface']);
704

    
705
			/* special treatment for tunnel interfaces */
706
			if ($gateway['ipprotocol'] == "inet6") {
707
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
708
			} else {
709
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet", false, false);
710
			}
711

    
712
			if ($gateway['ipprotocol'] == "inet" &&
713
					($gateway['gateway'] == $cgw4)) {
714
				$gateway['isdefaultgw'] = true;
715
				$found_defaultv4 = 1;
716
			} else if ($gateway['ipprotocol'] == "inet6" &&
717
					($gateway['gateway'] == $cgw6)) {
718
				$gateway['isdefaultgw'] = true;
719
				$found_defaultv6 = 1;
720
			}
721
			/* include the gateway index as the attribute */
722
			$gateway['attribute'] = $i;
723

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

    
728
			/* skip disabled gateways if the caller has not asked for them to be returned. */
729
			if (!($disabled === false && isset($gateway['disabled']))) {
730
				$gateways_arr[$gateway['name']] = $gateway;
731
			}
732
		}
733
	}
734
	unset($gateway);
735

    
736
	//Sort the array by GW name before moving on.
737
	ksort($gateways_arr, SORT_STRING | SORT_FLAG_CASE);
738

    
739
	/* Loop through all interfaces with a gateway and add it to a array */
740
	if ($disabled == false) {
741
		$iflist = get_configured_interface_with_descr();
742
	} else {
743
		$iflist = get_configured_interface_with_descr(true);
744
	}
745

    
746
	/* Process/add dynamic v4 gateways. */
747
	foreach ($iflist as $ifname => $friendly) {
748
		if (!interface_has_gateway($ifname)) {
749
			continue;
750
		}
751

    
752
		if (empty($config['interfaces'][$ifname])) {
753
			continue;
754
		}
755

    
756
		$ifcfg = &$config['interfaces'][$ifname];
757
		if (!isset($ifcfg['enable'])) {
758
			continue;
759
		}
760

    
761
		if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
762
			continue;
763
		}
764

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

    
847
		$gateway = array();
848
		$gateway['dynamic'] = false;
849
		$gateway['ipprotocol'] = "inet";
850
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
851
		$gateway['interface'] = get_real_interface($ifname);
852
		$gateway['friendlyiface'] = $ifname;
853
		$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
854
		$gateway['name'] = "{$friendly}{$ctype}";
855
		$gateway['attribute'] = "system";
856

    
857
		if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
858
			$gateway['isdefaultgw'] = true;
859
			$gateway['dynamic'] = true;
860
			$found_defaultv4 = 1;
861
		}
862

    
863
		/* Loopback dummy for dynamic interfaces without a IP */
864
		if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
865
			$gateway['gateway'] = "dynamic";
866
		}
867

    
868
		/* automatically skip known static and dynamic gateways that were previously processed */
869
		foreach ($gateways_arr_temp as $gateway_item) {
870
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
871
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
872
				continue 2;
873
			}
874
		}
875

    
876
		if (is_ipaddrv4($gateway['gateway'])) {
877
			$gateway['monitor'] = $gateway['gateway'];
878
		}
879

    
880
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
881
		$gateways_arr[$gateway['name']] = $gateway;
882
	}
883
	unset($gateway);
884

    
885
	/* Process/add dynamic v6 gateways. */
886
	foreach ($iflist as $ifname => $friendly) {
887
		/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
888
		if (!isset($config['system']['ipv6allow'])) {
889
			break;
890
		}
891

    
892
		if (!interface_has_gatewayv6($ifname)) {
893
			continue;
894
		}
895

    
896
		if (empty($config['interfaces'][$ifname])) {
897
			continue;
898
		}
899

    
900
		$ifcfg = &$config['interfaces'][$ifname];
901
		if (!isset($ifcfg['enable'])) {
902
			continue;
903
		}
904

    
905
		if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
906
			continue;
907
		}
908

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

    
990
		$gateway = array();
991
		$gateway['dynamic'] = false;
992
		$gateway['ipprotocol'] = "inet6";
993
		$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
994
		$gateway['interface'] = get_real_interface($ifname, "inet6");
995
		switch ($ifcfg['ipaddrv6']) {
996
			case "6rd":
997
			case "6to4":
998
				$gateway['dynamic'] = "default";
999
				break;
1000
		}
1001
		$gateway['friendlyiface'] = $ifname;
1002
		$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
1003
		$gateway['name'] = "{$friendly}{$ctype}";
1004
		$gateway['attribute'] = "system";
1005

    
1006
		if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) {
1007
			$gateway['isdefaultgw'] = true;
1008
			$gateway['dynamic'] = true;
1009
			$found_defaultv6 = 1;
1010
		}
1011

    
1012
		/* Loopback dummy for dynamic interfaces without a IP */
1013
		if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
1014
			$gateway['gateway'] = "dynamic";
1015
		}
1016

    
1017
		/* automatically skip known static and dynamic gateways that were previously processed */
1018
		foreach ($gateways_arr_temp as $gateway_item) {
1019
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
1020
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
1021
				continue 2;
1022
			}
1023
		}
1024

    
1025
		if (is_ipaddrv6($gateway['gateway'])) {
1026
			$gateway['monitor'] = $gateway['gateway'];
1027
		}
1028

    
1029
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
1030
		$gateways_arr[$gateway['name']] = $gateway;
1031
	}
1032
	unset($gateway);
1033

    
1034
	/* FIXME: Should this be enabled.
1035
	 * Some interface like wan might be default but have no info recorded
1036
	 * the config. */
1037
	/* this is a fallback if all else fails and we want to get packets out @smos */
1038
	if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
1039
		foreach ($gateways_arr as &$gateway) {
1040
			if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
1041
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw")) {
1042
					$gateway['isdefaultgw'] = true;
1043
					$found_defaultv4 = 1;
1044
				}
1045
			}
1046
			else if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
1047
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgwv6")) {
1048
					$gateway['isdefaultgw'] = true;
1049
					$found_defaultv6 = 1;
1050
				}
1051
			}
1052
		}
1053
	}
1054

    
1055
	if ($localhost === true) {
1056
		/* attach localhost for Null routes */
1057
		$gwlo4 = array();
1058
		$gwlo4['name'] = "Null4";
1059
		$gwlo4['interface'] = "lo0";
1060
		$gwlo4['ipprotocol'] = "inet";
1061
		$gwlo4['gateway'] = "127.0.0.1";
1062
		$gwlo4['attribute'] = "system";
1063
		$gwlo6 = array();
1064
		$gwlo6['name'] = "Null6";
1065
		$gwlo6['interface'] = "lo0";
1066
		$gwlo6['ipprotocol'] = "inet6";
1067
		$gwlo6['gateway'] = "::1";
1068
		$gwlo6['attribute'] = "system";
1069
		$gateways_arr['Null4'] = $gwlo4;
1070
		$gateways_arr['Null6'] = $gwlo6;
1071
	}
1072

    
1073
	if ($integer_index) {
1074
		$gateways_arr = array_values($gateways_arr);
1075
	}
1076

    
1077
	if ($found_defaultv4 != 1 && is_ipaddr($cgw4)) {
1078
		foreach($gateways_arr as &$gw) {
1079
			if ($gw['gateway'] == $cgw4) {
1080
				$gw['isdefaultgw'] = true;
1081
			}
1082
		}
1083
	}
1084
	if ($found_defaultv6 != 1 && is_ipaddr($cgw6)) {
1085
		foreach($gateways_arr as &$gw) {
1086
			if ($gw['gateway'] == $cgw6) {
1087
				$gw['isdefaultgw'] = true;
1088
			}
1089
		}
1090
	}
1091

    
1092
	$gways = order_gateways_as_configured($gateways_arr);
1093

    
1094
	// Add the tier names here so that system_gateways.php doesn't need to
1095
	foreach ($gways as $idx => $gway) {
1096
		$gways[$idx]['tiername'] = gateway_getgwtiername($gways, $idx);
1097
	}
1098

    
1099
	return $gways;
1100
}
1101

    
1102
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) {
1103
	global $config, $g;
1104
	/*
1105
	 * NOTE: The code below is meant to replace the default gateway when it goes down.
1106
	 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
1107
	 */
1108
	$set_dfltgwname = '';
1109

    
1110
	if ($ipprotocol == 'inet') {
1111
		$gwdefault = $config['gateways']['defaultgw4'];
1112
	} else {
1113
		$gwdefault = $config['gateways']['defaultgw6'];
1114
	}
1115
	if ($gwdefault == "-") {
1116
		// 'none', dont set the default gateway, useful if routes are managed by frr/bgp/ospf or similar
1117
		return;
1118
	}
1119
	if (isset($gateways_arr[$gwdefault])) {
1120
		// the configured gateway is a regular one. (not a gwgroup) use it as is..
1121
		$set_dfltgwname = $gwdefault;
1122
	} elseif (empty($gwdefault)) {
1123
		// 'automatic' mode, pick the first one thats 'up' or 'unmonitored' which is always considered up
1124
		$gateways_arr = order_gateways_as_configured($gateways_arr);
1125
		$fallback = "";
1126
		foreach($gateways_arr as $gwname => $gwsttng) {
1127
			if ($gwsttng['ipprotocol'] != $ipprotocol) {
1128
				continue;
1129
			}
1130

    
1131
			if (isset($gwsttng['monitor_disable']) || isset($gwsttng['action_disable']) ||
1132
			    ($gateways_status[$gwname]['status'] == "online")) {
1133
				$set_dfltgwname = $gwname;
1134
				break;
1135
			}
1136
			if (empty($fallback) && $gwsttng['interface'] != 'lo0') {
1137
				$fallback = $gwname;
1138
			}
1139
		}
1140
		if (empty($set_dfltgwname)) {
1141
			log_error(sprintf("Gateway, none 'available' for %s, use the first one configured. '%s'", $ipprotocol, $fallback));
1142
			$set_dfltgwname = $fallback;
1143
		}
1144
	} else {
1145
		// a gwgroup is selected
1146
		// find the best available gateway given options available..
1147
		$gwg_members = array();
1148
		$viplist = get_configured_vip_list();
1149
		if (is_array($config['gateways']['gateway_group'])) {
1150
			foreach ($config['gateways']['gateway_group'] as $group) {
1151
				if ($group['name'] == $gwdefault) {
1152
					// finds the gw members of the best available tier for this group.
1153
					$gwg_members = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1154
				}
1155
			}
1156
		}
1157

    
1158
		if (count($gwg_members) > 0) {
1159
			$currentdefaultgwip = route_get_default($ipprotocol);
1160
			$found_current = false;
1161
			foreach($gwg_members as $gwgroupitem) {
1162
				if (!empty($currentdefaultgwip) && ($gwgroupitem['gwip'] == $currentdefaultgwip)) {
1163
					$set_dfltgwname = $gwgroupitem['gw'];
1164
					$found_current = true;
1165
					if (isset($config['system']['gw-debug'])) {
1166
						log_error("Keep current gateway, its already part of the group members.");
1167
					}
1168
					break;
1169
				}
1170
			}
1171
			if (!$found_current) {
1172
				$set_dfltgwname = $gwg_members[0]['gw'];
1173
				log_error(sprintf("Gateway, switch to: %s", $set_dfltgwname));
1174
			}
1175
		} else {
1176
			log_error("Gateway, NONE AVAILABLE");
1177
		}
1178
	}
1179
	if (!empty($set_dfltgwname) && isset($gateways_arr[$set_dfltgwname])) {
1180
		setdefaultgateway($gateways_arr[$set_dfltgwname]);
1181
	}
1182
}
1183

    
1184
function setdefaultgateway($gw) {
1185
	global $g, $config;
1186
	if (isset($config['system']['route-debug'])) {
1187
		file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']}");
1188
	}
1189
	$ipprotocol = $gw['ipprotocol'];
1190
	if ($gw['gateway'] == "dynamic") {
1191
		if ($ipprotocol == 'inet') {
1192
			$gw['gateway'] = get_interface_gateway($gw['friendlyiface']);
1193
		} else {
1194
			$gw['gateway'] = get_interface_gateway_v6($$gw['friendlyiface']);
1195
		}
1196
	}
1197
	if ($ipprotocol == 'inet6' && !is_ipaddrv6($gw['gateway'])) {
1198
		return;
1199
	}
1200
	if ($ipprotocol == 'inet' && !is_ipaddrv4($gw['gateway'])) {
1201
		return;
1202
	}
1203
	if ($ipprotocol == 'inet6') {
1204
		if (is_linklocal($gw['gateway']) && get_ll_scope($gw['gateway']) == '') {
1205
			$gw['gateway'] .= "%" . $gw['interface'];
1206
		}
1207
	}
1208
	$currentdefaultgwip = route_get_default($ipprotocol);
1209
	if ($currentdefaultgwip != $gw['gateway']) {
1210
		log_error("Default gateway setting {$gw['descr']} as default.");
1211

    
1212
		if ($ipprotocol == 'inet') {
1213
			$inet = '';
1214
		} else {
1215
			$inet = 'v6';
1216
		}
1217
		array_map('unlink', glob("{$g['tmp_path']}/*_defaultgw{$inet}", GLOB_BRACE));
1218
		$defaultif = get_real_interface($gw['interface']);
1219
		if ($defaultif) {
1220
			@file_put_contents("{$g['tmp_path']}/{$defaultif}_defaultgw{$inet}", $gw['gateway']);
1221
		}
1222

    
1223
		if (isset($gw["nonlocalgateway"])) {
1224
			if (is_ipaddr($gw['gateway']) && !empty($gw['interface'])) {
1225
				route_add_or_change($gw['gateway'], '',
1226
				    $gw['interface']);
1227
			}
1228
		}
1229
		if (isset($config['system']['route-debug'])) {
1230
			file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']} ({$gw['gateway']})");
1231
		}
1232
		route_add_or_change("default", $gw['gateway'], '', '',
1233
		    $ipprotocol);
1234
		return true;
1235
	}
1236
}
1237

    
1238
function get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist){
1239
	$result = array();
1240
	/* create array with group gateways members separated by tier */
1241
	$tiers = array();
1242
	$backupplan = array();
1243
	$gwvip_arr = array();
1244
	foreach ($group['item'] as $item) {
1245
		list($gwname, $tier, $vipname) = explode("|", $item);
1246

    
1247
		if (is_ipaddr($viplist[$vipname])) {
1248
			if (!is_array($gwvip_arr[$group['name']])) {
1249
				$gwvip_arr[$group['name']] = array();
1250
			}
1251
			$gwvip_arr[$group['name']][$gwname] = $vipname;
1252
		}
1253

    
1254
		/* Do it here rather than reiterating again the group in case no member is up. */
1255
		if (!is_array($backupplan[$tier])) {
1256
			$backupplan[$tier] = array();
1257
		}
1258
		$backupplan[$tier][] = $gwname;
1259

    
1260
		/* check if the gateway is available before adding it to the array */
1261
		if (is_array($gateways_status[$gwname])) {
1262
			$status = $gateways_status[$gwname];
1263
			$gwdown = false;
1264
			if (stristr($status['status'], "down")) {
1265
				$gwdown = true;
1266
				switch ($status['substatus']) {
1267
					case "highloss":
1268
						$msg = sprintf(gettext('MONITOR: %1$s has packet loss, omitting from routing group %2$s'), $gwname, $group['name']);
1269
						break;
1270
					case "highdelay":
1271
						$msg = sprintf(gettext('MONITOR: %1$s has high latency, omitting from routing group %2$s'), $gwname, $group['name']);
1272
						break;
1273
					default:
1274
						$msg = sprintf(gettext('MONITOR: %1$s is down, omitting from routing group %2$s'), $gwname, $group['name']);
1275
				}
1276
			}
1277
			$statuschanged = false;
1278
			$pluginparams = array();
1279
			$pluginparams['type'] = 'gateway';
1280
			$pluginparams['name'] = $gwname;
1281
			if ($gwdown == true) {
1282
				if (!file_exists("/tmp/.down.{$gwname}")) {
1283
					@touch("/tmp/.down.{$gwname}");
1284
					$msg .= "\n".implode("|", $status);
1285
					$pluginparams['event'] = 'gateway.down';
1286
					$statuschanged = true;
1287
				}
1288
			} else {
1289
				/* Online add member */
1290
				if (!is_array($tiers[$tier])) {
1291
					$tiers[$tier] = array();
1292
				}
1293
				$tiers[$tier][] = $gwname;
1294
				if (unlink_if_exists("/tmp/.down.{$gwname}")) {
1295
					$msg = sprintf(gettext('MONITOR: %1$s is available now, adding to routing group %2$s'), $gwname, $group['name']);
1296
					$msg .= "\n".implode("|", $status);
1297
					$pluginparams['event'] = 'gateway.up';
1298
					$statuschanged = true;
1299
				}
1300
			}
1301
			if ($statuschanged) {
1302
				log_error($msg);
1303
				notify_all_remote($msg);
1304
				if (isset($gateways_arr[$gwname]['interface'])) {
1305
					$pluginparams['interface'] = $gateways_arr[$gwname]['interface'];
1306
				}
1307
				pkg_call_plugins('plugin_gateway', $pluginparams);
1308
			}
1309
		} else if (isset($gateways_arr[$gwname]['monitor_disable']) || isset($gateways_arr[$gwname]['action_disable'])) {
1310
			$tiers[$tier][] = $gwname;
1311
		}
1312
	}
1313
	$tiers_count = count($tiers);
1314
	if ($tiers_count == 0) {
1315
		/* Oh dear, we have no members! Engage Plan B */
1316
		if (isset($config['system']['gw-debug']) && (!platform_booting())) {
1317
			$msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']);
1318
			log_error($msg);
1319
		}
1320
		$tiers = $backupplan;
1321
	}
1322
	/* sort the tiers array by the tier key */
1323
	ksort($tiers);
1324

    
1325
	/* we do not really foreach the tiers as we stop after the first tier */
1326
	foreach ($tiers as $tieridx => $tier) {
1327
		/* process all gateways in this tier */
1328
		foreach ($tier as $member) {
1329
			/* determine interface gateway */
1330
			if (isset($gateways_arr[$member])) {
1331
				$gateway = $gateways_arr[$member];
1332
				$int = $gateway['interface'];
1333
				$gatewayip = "";
1334
				if (is_ipaddr($gateway['gateway'])) {
1335
					$gatewayip = $gateway['gateway'];
1336
				} else if (!empty($int)) {
1337
					$gatewayip = get_interface_gateway($gateway['friendlyiface']);
1338
				}
1339

    
1340
				if (!empty($int)) {
1341
					$result['ipprotocol'] = $gateway['ipprotocol'];
1342
					if (is_ipaddr($gatewayip)) {
1343
						$groupmember = array();
1344
						$groupmember['gw'] = $member;
1345
						$groupmember['int'] = $int;
1346
						$groupmember['gwip'] = $gatewayip;
1347
						$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
1348
						if (is_array($gwvip_arr[$group['name']]) && !empty($gwvip_arr[$group['name']][$member])) {
1349
							$groupmember['vip'] = $gwvip_arr[$group['name']][$member];
1350
						}
1351
						$result[] = $groupmember;
1352
					}
1353
				}
1354
			}
1355
		}
1356
		/* we should have the 1st available tier now, exit stage left */
1357
		if (count($result) > 0) {
1358
			break;
1359
		} else {
1360
			log_error(sprintf(gettext('GATEWAYS: Group %1$s did not have any gateways up on tier %2$s!'), $group['name'], $tieridx));
1361
		}
1362
	}
1363
	// Add description field last to not influence the count() above
1364
	$result['descr'] = $group['descr'];
1365
	return $result;
1366
}
1367

    
1368
function get_gwgroup_members($groupname) {
1369
	global $config;
1370
	$gateways_status = return_gateways_status(true);
1371
	$gateways_arr = return_gateways_array();
1372
	$viplist = get_configured_vip_list();
1373
	foreach ($config['gateways']['gateway_group'] as $group) {
1374
		if ($group['name'] == $groupname) {
1375
			return get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1376
		}
1377
	}
1378
	return array();
1379
}
1380

    
1381
/*
1382
 * Return an array with all gateway groups with name as key
1383
 * All gateway groups will be processed before returning the array.
1384
 */
1385
function return_gateway_groups_array($fixup = false, $gways = false) {
1386
	global $config;
1387

    
1388
	/* fetch the current gateways status */
1389
	if (is_array($gways)) {
1390
		$gateways_status = $gways;
1391
	} else {
1392
		$gateways_status = return_gateways_status(true);
1393
	}
1394

    
1395
	$gateways_arr = return_gateways_array();
1396
	$gateway_groups_array = array();
1397
	if ($fixup == true) {
1398
		$gw4 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw4'], $gways);
1399
		$gw6 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw6'], $gways);
1400
		if ($gw4 && $gw4['type'] == 'gatewaygroup') {
1401
			fixup_default_gateway("inet", $gateways_status, $gateways_arr);
1402
		}
1403
		if ($gw6 && $gw6['type'] == 'gatewaygroup') {
1404
			fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
1405
		}
1406
	}
1407
	init_config_arr(array('gateways', 'gateway_group'));
1408
	if (!empty($config['gateways']['gateway_group'])) {
1409
		$viplist = get_configured_vip_list();
1410
		foreach ($config['gateways']['gateway_group'] as $group) {
1411
			$gateway_groups_array[$group['name']] = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1412
		}
1413
	}
1414

    
1415
	return ($gateway_groups_array);
1416
}
1417

    
1418
/* Update DHCP WAN Interface ip address in gateway group item */
1419
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
1420
	global $config;
1421

    
1422
	if (is_array($config['gateways']['gateway_item'])) {
1423
		foreach ($config['gateways']['gateway_item'] as & $gw) {
1424
			if ($gw['interface'] != $interface) {
1425
				continue;
1426
			}
1427

    
1428
			$current_gw = get_interface_gateway($interface);
1429
			if ($gw['gateway'] <> $current_gw) {
1430
				$gw['gateway'] = $current_gw;
1431
				$changed = true;
1432
			}
1433
		}
1434
	}
1435

    
1436
	if ($changed && $current_gw) {
1437
		write_config(sprintf(gettext(
1438
		    'Updating gateway group gateway for %1$s - new gateway is %2$s'),
1439
		    $interface, $current_gw));
1440
	}
1441
}
1442

    
1443
function lookup_gateway_or_group_by_name($gwname, $gways = false) {
1444
	global $config;
1445

    
1446
	if (is_array($gways)) {
1447
		$gateways_arr = $gways;
1448
	} else {
1449
		$gateways_arr = return_gateways_array();
1450
	}
1451

    
1452
	foreach ($gateways_arr as $gw) {
1453
		if ($gw['name'] == $gwname) {
1454
			$gw['type'] = 'gateway';
1455
			return $gw;
1456
		}
1457
	}
1458

    
1459
	init_config_arr(array('gateways', 'gateway_group'));
1460
	foreach ($config['gateways']['gateway_group'] as $gwg) {
1461
		if ($gwg['name'] == $gwname) {
1462
			$gwg['type'] = 'gatewaygroup';
1463
			return $gwg;
1464
		}
1465
	}
1466

    
1467
	return false;
1468
}
1469

    
1470
function lookup_gateway_ip_by_name($name, $disabled = false) {
1471

    
1472
	$gateways_arr = return_gateways_array($disabled, true);
1473
	foreach ($gateways_arr as $gname => $gw) {
1474
		if ($gw['name'] === $name || $gname === $name) {
1475
			return $gw['gateway'];
1476
		}
1477
	}
1478

    
1479
	return false;
1480
}
1481

    
1482
function lookup_gateway_monitor_ip_by_name($name) {
1483

    
1484
	$gateways_arr = return_gateways_array(false, true);
1485
	if (!empty($gateways_arr[$name])) {
1486
		$gateway = $gateways_arr[$name];
1487
		if (!is_ipaddr($gateway['monitor'])) {
1488
			return $gateway['gateway'];
1489
		}
1490

    
1491
		return $gateway['monitor'];
1492
	}
1493

    
1494
	return (false);
1495
}
1496

    
1497
function lookup_gateway_interface_by_name($name) {
1498

    
1499
	$gateways_arr = return_gateways_array(false, true);
1500
	if (!empty($gateways_arr[$name])) {
1501
		$interfacegw = $gateways_arr[$name]['friendlyiface'];
1502
		return ($interfacegw);
1503
	}
1504

    
1505
	return (false);
1506
}
1507

    
1508
function get_root_interface($interface) {
1509
	if (substr($interface, 0, 4) == '_vip') {
1510
		$interface = get_configured_vip_interface($interface);
1511
		if (substr($interface, 0, 4) == '_vip') {
1512
			$interface = get_configured_vip_interface($interface);
1513
		}
1514
	}
1515
	return $interface;
1516
}
1517

    
1518
function get_interface_gateway($interface, &$dynamic = false) {
1519
	global $config, $g;
1520

    
1521
	$interface = get_root_interface($interface);
1522

    
1523
	$gw = NULL;
1524
	$gwcfg = $config['interfaces'][$interface];
1525
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
1526
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1527
			if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
1528
				$gw = $gateway['gateway'];
1529
				break;
1530
			}
1531
		}
1532
	}
1533

    
1534
	// for dynamic interfaces we handle them through the $interface_router file.
1535
	if (($gw == NULL || !is_ipaddrv4($gw)) && !is_ipaddrv4($gwcfg['ipaddr'])) {
1536
		$realif = get_real_interface($interface);
1537
		if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
1538
			$gw = trim(@file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
1539
			$dynamic = true;
1540
		}
1541
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw")) {
1542
			$dynamic = "default";
1543
		}
1544

    
1545
	}
1546

    
1547
	/* return gateway */
1548
	return ($gw);
1549
}
1550

    
1551
function get_interface_gateway_v6($interface, &$dynamic = false) {
1552
	global $config, $g;
1553

    
1554
	$interface = get_root_interface($interface);
1555

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

    
1567
	// for dynamic interfaces we handle them through the $interface_router file.
1568
	if (($gw == NULL || !is_ipaddrv6($gw)) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
1569
		$realif = get_real_interface($interface);
1570
		if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
1571
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
1572
			$dynamic = true;
1573
		}
1574
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6")) {
1575
			$dynamic = "default";
1576
		}
1577
	}
1578
	/* return gateway */
1579
	return ($gw);
1580
}
1581

    
1582
/* Check a IP address against a gateway IP or name
1583
 * to verify it's address family */
1584
function validate_address_family($ipaddr, $gwname, $disabled = false) {
1585
	$v4ip = false;
1586
	$v6ip = false;
1587
	$v4gw = false;
1588
	$v6gw = false;
1589

    
1590
	if (is_ipaddrv4($ipaddr)) {
1591
		$v4ip = true;
1592
	}
1593
	if (is_ipaddrv6($ipaddr)) {
1594
		$v6ip = true;
1595
	}
1596
	if (is_ipaddrv4($gwname)) {
1597
		$v4gw = true;
1598
	}
1599
	if (is_ipaddrv6($gwname)) {
1600
		$v6gw = true;
1601
	}
1602

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

    
1610
	/* still no match, carry on, lookup gateways */
1611
	if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname, $disabled))) {
1612
		$v4gw = true;
1613
	}
1614
	if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname, $disabled))) {
1615
		$v6gw = true;
1616
	}
1617

    
1618
	$gw_array = return_gateways_array();
1619
	if (is_array($gw_array[$gwname])) {
1620
		switch ($gw_array[$gwname]['ipprotocol']) {
1621
			case "inet":
1622
				$v4gw = true;
1623
				break;
1624
			case "inet6":
1625
				$v6gw = true;
1626
				break;
1627
		}
1628
	}
1629

    
1630
	if ($v4ip && $v4gw) {
1631
		return true;
1632
	}
1633
	if ($v6ip && $v6gw) {
1634
		return true;
1635
	}
1636

    
1637
	return false;
1638
}
1639

    
1640
/* check if a interface is part of a gateway group */
1641
function interface_gateway_group_member($interface, $gwgroup_name = "") {
1642
	global $config;
1643

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

    
1650
	$gateways_arr = return_gateways_array(false, true);
1651
	foreach ($groups as $group) {
1652
		if (is_array($group['item'])) {
1653
			foreach ($group['item'] as $item) {
1654
				$elements = explode("|", $item);
1655
				$gwname = $elements[0];
1656
				if ($interface == $gateways_arr[$gwname]['interface'] &&
1657
				    (empty($gwgroup_name) || $gwgroup_name == $group['name'])) {
1658
					unset($gateways_arr);
1659
					return true;
1660
				}
1661
			}
1662
		}
1663
	}
1664
	unset($gateways_arr);
1665

    
1666
	return false;
1667
}
1668

    
1669
function gateway_is_gwgroup_member($name, $detail=false) {
1670
	global $config;
1671

    
1672
	if (is_array($config['gateways']['gateway_group'])) {
1673
		$groups = $config['gateways']['gateway_group'];
1674
	} else {
1675
		return false;
1676
	}
1677

    
1678
	$members = array();
1679
	foreach ($groups as $group) {
1680
		if (is_array($group['item'])) {
1681
			foreach ($group['item'] as $item) {
1682
				list($gwname, $tier, $vipname) = explode("|", $item);
1683
				if ($name == $gwname) {
1684
					if ($detail) {
1685
						$newitem = array();
1686
						$newitem['name'] = $group['name'];
1687
						$newitem['tier'] = $tier;
1688
						$newitem['vipname'] = $vipname;
1689
						$members[] = $newitem;
1690
					} else {
1691
						$members[] = $group['name'];
1692
					}
1693
				}
1694
			}
1695
		}
1696
	}
1697

    
1698
	return $members;
1699
}
1700
/*
1701
  Check the proposed gateway settings to see if they are valid.
1702
  $gateway_settings - the proposed array of proposed gateway settings
1703
  $id - the index of the gateway proposed to be modified (otherwise "" if adding a new gateway)
1704
  $parent_ip - the IP (v4 or v6) address about to be set on the corresponding interface (if any)
1705
  $parent_sn - the subnet about to be set on the corresponding interface (if any)
1706
  (Note: the above 2 parameters allow gateway parameters to be validated concurrently with saving
1707
   an interface, before the new interface parameters are actually saved in the config.)
1708
  Return completed $input_errors array if there is any problem.
1709
  Otherwise return an empty $input_errors array
1710
*/
1711
function validate_gateway($gateway_settings, $id = "", $parent_ip = "", $parent_sn = "") {
1712
	global $config;
1713

    
1714
	$a_gateways = return_gateways_array(true, false, true, true);
1715
	$input_errors = array();
1716

    
1717
	/* input validation */
1718
	$reqdfields = explode(" ", "name interface");
1719
	$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
1720

    
1721
	do_input_validation($gateway_settings, $reqdfields, $reqdfieldsn, $input_errors);
1722

    
1723
	if (!isset($gateway_settings['name'])) {
1724
		$input_errors[] = "A valid gateway name must be specified.";
1725
	}
1726
	if (!is_validaliasname($gateway_settings['name'])) {
1727
		$input_errors[] = invalidaliasnamemsg($gateway_settings['name'], gettext("gateway"));
1728
	} else if (isset($gateway_settings['disabled'])) {
1729
		// We have a valid gateway name that the user wants to mark as disabled.
1730
		// Check if the gateway name is used in any gateway group.
1731
		if (is_array($config['gateways']['gateway_group'])) {
1732
			foreach ($config['gateways']['gateway_group'] as $group) {
1733
				foreach ($group['item'] as $item) {
1734
					$items = explode("|", $item);
1735
					if ($items[0] == $gateway_settings['name']) {
1736
						$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']);
1737
					}
1738
				}
1739
			}
1740
		}
1741

    
1742
		// Check if the gateway name is used in any enabled Static Route.
1743
		if (is_array($config['staticroutes']['route'])) {
1744
			foreach ($config['staticroutes']['route'] as $route) {
1745
				if ($route['gateway'] == $gateway_settings['name']) {
1746
					if (!isset($route['disabled'])) {
1747
						// There is a static route that uses this gateway and is enabled (not disabled).
1748
						$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']);
1749
					}
1750
				}
1751
			}
1752
		}
1753

    
1754
		// Check if the gateway name is used by any DNS Server
1755
		$dnsgw_counter = 1;
1756
		while (isset($config["system"]["dns{$dnsgw_counter}gw"])) {
1757
			if ($gateway_settings['name'] == $config["system"]["dns{$dnsgw_counter}gw"]) {
1758
				// The user wants to disable this gateway, but there is a static route to the DNS server that refers to the gateway.
1759
				$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"]);
1760
			}
1761
			$dnsgw_counter++;
1762
		}
1763
	}
1764
	/* skip system gateways which have been automatically added */
1765
	if (($gateway_settings['gateway'] && (!is_ipaddr($gateway_settings['gateway'])) && ($gateway_settings['attribute'] !== "system")) && ($gateway_settings['gateway'] != "dynamic")) {
1766
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
1767
	}
1768

    
1769
	if ($gateway_settings['gateway'] && is_ipaddr($gateway_settings['gateway'])) {
1770
		if (is_ipaddrv4($gateway_settings['gateway'])) {
1771
			if ($parent_ip == '') {
1772
				$parent_ip = get_interface_ip($gateway_settings['interface']);
1773
				$parent_sn = get_interface_subnet($gateway_settings['interface']);
1774
			}
1775
			if (empty($parent_ip) || empty($parent_sn)) {
1776
				$input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
1777
			} elseif (!isset($gateway_settings["nonlocalgateway"])) {
1778
				$subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
1779
				$vips = link_interface_to_vips($gateway_settings['interface']);
1780
				if (is_array($vips)) {
1781
					foreach ($vips as $vip) {
1782
						if (!is_ipaddrv4($vip['subnet'])) {
1783
							continue;
1784
						}
1785
						$subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
1786
					}
1787
				}
1788

    
1789
				$found = false;
1790
				foreach ($subnets as $subnet) {
1791
					if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
1792
						$found = true;
1793
						break;
1794
					}
1795
				}
1796

    
1797
				if ($found === false) {
1798
					$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
1799
				}
1800
			}
1801
		} else if (is_ipaddrv6($gateway_settings['gateway'])) {
1802
			/* do not do a subnet match on a link local address, it's valid */
1803
			if (!is_linklocal($gateway_settings['gateway'])) {
1804
				if ($parent_ip == '') {
1805
					$parent_ip = get_interface_ipv6($gateway_settings['interface']);
1806
					$parent_sn = get_interface_subnetv6($gateway_settings['interface']);
1807
				}
1808
				if (empty($parent_ip) || empty($parent_sn)) {
1809
					$input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
1810
				} elseif (!isset($gateway_settings["nonlocalgateway"])) {
1811
					$subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
1812
					$vips = link_interface_to_vips($gateway_settings['interface']);
1813
					if (is_array($vips)) {
1814
						foreach ($vips as $vip) {
1815
							if (!is_ipaddrv6($vip['subnet'])) {
1816
								continue;
1817
							}
1818
							$subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
1819
						}
1820
					}
1821

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

    
1830
					if ($found === false) {
1831
						$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
1832
					}
1833
				}
1834
			}
1835
		}
1836

    
1837
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddr'])) {
1838
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddr']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
1839
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
1840
			}
1841
		}
1842
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddrv6'])) {
1843
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddrv6']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
1844
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
1845
			}
1846
		}
1847
	}
1848
	if (($gateway_settings['monitor'] != "") && ($gateway_settings['monitor'] != "dynamic")) {
1849
		validateipaddr($gateway_settings['monitor'], IPV4V6, "Monitor IP", $input_errors, false);
1850
	}
1851
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] < 0) {
1852
		$input_errors[] = gettext("A valid data payload must be specified.");
1853
	}
1854
	/* only allow correct IPv4 and IPv6 gateway addresses */
1855
	if (($gateway_settings['gateway'] <> "") && is_ipaddr($gateway_settings['gateway']) && $gateway_settings['gateway'] != "dynamic") {
1856
		if (is_ipaddrv6($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet")) {
1857
			$input_errors[] = sprintf(gettext("The IPv6 gateway address '%s' can not be used as a IPv4 gateway."), $gateway_settings['gateway']);
1858
		}
1859
		if (is_ipaddrv4($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet6")) {
1860
			$input_errors[] = sprintf(gettext("The IPv4 gateway address '%s' can not be used as a IPv6 gateway."), $gateway_settings['gateway']);
1861
		}
1862
	}
1863
	/* only allow correct IPv4 and IPv6 monitor addresses */
1864
	if (($gateway_settings['monitor'] <> "") && is_ipaddr($gateway_settings['monitor']) && $gateway_settings['monitor'] != "dynamic") {
1865
		if (is_ipaddrv6($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet")) {
1866
			$input_errors[] = sprintf(gettext("The IPv6 monitor address '%s' can not be used on a IPv4 gateway."), $gateway_settings['monitor']);
1867
		}
1868
		if (is_ipaddrv4($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet6")) {
1869
			$input_errors[] = sprintf(gettext("The IPv4 monitor address '%s' can not be used on a IPv6 gateway."), $gateway_settings['monitor']);
1870
		}
1871
	}
1872

    
1873
	if (isset($gateway_settings['name'])) {
1874
		/* check for overlaps */
1875
		foreach ($a_gateways as $gateway) {
1876
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
1877
				if ($gateway['name'] != $gateway_settings['name']) {
1878
					$input_errors[] = gettext("Changing name on a gateway is not allowed.");
1879
				}
1880
				continue;
1881
			}
1882
			if ($gateway_settings['name'] <> "") {
1883
				if (($gateway['name'] <> "") && ($gateway_settings['name'] == $gateway['name']) && ($gateway['attribute'] !== "system")) {
1884
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $gateway_settings['name']);
1885
					break;
1886
				}
1887
			}
1888
			if (is_ipaddr($gateway_settings['gateway'])) {
1889
				if (($gateway['gateway'] <> "") && ($gateway_settings['gateway'] == $gateway['gateway']) && ($gateway['attribute'] !== "system")) {
1890
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $gateway_settings['gateway']);
1891
					break;
1892
				}
1893
			}
1894
			if (is_ipaddr($gateway_settings['monitor'])) {
1895
				if (($gateway['monitor'] <> "") && ($gateway_settings['monitor'] == $gateway['monitor']) && ($gateway['attribute'] !== "system")) {
1896
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. A different monitor IP must be chosen.'), $gateway_settings['monitor']);
1897
					break;
1898
				}
1899
			}
1900
		}
1901
	}
1902

    
1903
	/* input validation of dpinger advanced parameters */
1904

    
1905
	$dpinger_default = return_dpinger_defaults();
1906
	$latencylow = $dpinger_default['latencylow'];
1907
	if ($gateway_settings['latencylow']) {
1908
		if (!is_numeric($gateway_settings['latencylow'])) {
1909
			$input_errors[] = gettext("The low latency threshold needs to be a numeric value.");
1910
		} else if ($gateway_settings['latencylow'] < 1) {
1911
			$input_errors[] = gettext("The low latency threshold needs to be positive.");
1912
		} else {
1913
			$latencylow = $gateway_settings['latencylow'];
1914
		}
1915
	}
1916

    
1917
	$latencyhigh = $dpinger_default['latencyhigh'];
1918
	if ($gateway_settings['latencyhigh']) {
1919
		if (!is_numeric($gateway_settings['latencyhigh'])) {
1920
			$input_errors[] = gettext("The high latency threshold needs to be a numeric value.");
1921
		} else if ($gateway_settings['latencyhigh'] < 1) {
1922
			$input_errors[] = gettext("The high latency threshold needs to be positive.");
1923
		} else {
1924
			$latencyhigh = $gateway_settings['latencyhigh'];
1925
		}
1926
	}
1927

    
1928
	$losslow = $dpinger_default['losslow'];
1929
	if ($gateway_settings['losslow']) {
1930
		if (!is_numeric($gateway_settings['losslow'])) {
1931
			$input_errors[] = gettext("The low Packet Loss threshold needs to be a numeric value.");
1932
		} else if ($gateway_settings['losslow'] < 1) {
1933
			$input_errors[] = gettext("The low Packet Loss threshold needs to be positive.");
1934
		} else if ($gateway_settings['losslow'] >= 100) {
1935
			$input_errors[] = gettext("The low Packet Loss threshold needs to be less than 100.");
1936
		} else {
1937
			$losslow = $gateway_settings['losslow'];
1938
		}
1939
	}
1940

    
1941
	$losshigh = $dpinger_default['losshigh'];
1942
	if ($gateway_settings['losshigh']) {
1943
		if (!is_numeric($gateway_settings['losshigh'])) {
1944
			$input_errors[] = gettext("The high Packet Loss threshold needs to be a numeric value.");
1945
		} else if ($gateway_settings['losshigh'] < 1) {
1946
			$input_errors[] = gettext("The high Packet Loss threshold needs to be positive.");
1947
		} else if ($gateway_settings['losshigh'] > 100) {
1948
			$input_errors[] = gettext("The high Packet Loss threshold needs to be 100 or less.");
1949
		} else {
1950
			$losshigh = $gateway_settings['losshigh'];
1951
		}
1952
	}
1953

    
1954
	$time_period = $dpinger_default['time_period'];
1955
	if ($gateway_settings['time_period']) {
1956
		if (!is_numeric($gateway_settings['time_period'])) {
1957
			$input_errors[] = gettext("The time period over which results are averaged needs to be a numeric value.");
1958
		} else if ($gateway_settings['time_period'] < 1) {
1959
			$input_errors[] = gettext("The time period over which results are averaged needs to be positive.");
1960
		} else {
1961
			$time_period = $gateway_settings['time_period'];
1962
		}
1963
	}
1964

    
1965
	$interval = $dpinger_default['interval'];
1966
	if ($gateway_settings['interval']) {
1967
		if (!is_numeric($gateway_settings['interval'])) {
1968
			$input_errors[] = gettext("The probe interval needs to be a numeric value.");
1969
		} else if ($gateway_settings['interval'] < 1) {
1970
			$input_errors[] = gettext("The probe interval needs to be positive.");
1971
		} else {
1972
			$interval = $gateway_settings['interval'];
1973
		}
1974
	}
1975

    
1976
	$loss_interval = $dpinger_default['loss_interval'];
1977
	if ($gateway_settings['loss_interval']) {
1978
		if (!is_numeric($gateway_settings['loss_interval'])) {
1979
			$input_errors[] = gettext("The loss interval needs to be a numeric value.");
1980
		} else if ($gateway_settings['loss_interval'] < 1) {
1981
			$input_errors[] = gettext("The loss interval setting needs to be positive.");
1982
		} else {
1983
			$loss_interval = $gateway_settings['loss_interval'];
1984
		}
1985
	}
1986

    
1987
	$alert_interval = $dpinger_default['alert_interval'];
1988
	if ($gateway_settings['alert_interval']) {
1989
		if (!is_numeric($gateway_settings['alert_interval'])) {
1990
			$input_errors[] = gettext("The alert interval needs to be a numeric value.");
1991
		} else if ($gateway_settings['alert_interval'] < 1) {
1992
			$input_errors[] = gettext("The alert interval setting needs to be positive.");
1993
		} else {
1994
			$alert_interval = $gateway_settings['alert_interval'];
1995
		}
1996
	}
1997

    
1998
	if ($latencylow >= $latencyhigh) {
1999
		$input_errors[] = gettext(
2000
		    "The high latency threshold needs to be greater than the low latency threshold");
2001
	}
2002

    
2003
	if ($losslow >= $losshigh) {
2004
		$input_errors[] = gettext(
2005
		    "The high packet loss threshold needs to be higher than the low packet loss threshold");
2006
	}
2007

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

    
2014
	// Ensure that the time period is greater than 2 times the probe interval plus the loss interval.
2015
	if (($interval * 2 + $loss_interval) >= $time_period) {
2016
		$input_errors[] = gettext("The time period needs to be greater than twice the probe interval plus the loss interval.");
2017
	}
2018

    
2019
	// There is no point recalculating the average latency and loss more often than the probe interval.
2020
	// So the alert interval needs to be >= probe interval.
2021
	if ($interval > $alert_interval) {
2022
		$input_errors[] = gettext("The alert interval needs to be greater than or equal to the probe interval.");
2023
	}
2024

    
2025
	return $input_errors;
2026
}
2027

    
2028
// Save gateway settings.
2029
// $gateway_settings - the array of gateway setting parameters
2030
// $realid - the index of the gateway to be modified (otherwise "" if adding a new gateway)
2031

    
2032
// This function is responsible to:
2033
//   Setup the gateway parameter structure from the gateway settings input parameter
2034
//   Save the structure into the config
2035
//   Remove any run-time settings from gateway parameters that are changed (e.g. remove routes to addresses that are changing)
2036

    
2037
// A subsequent "apply" step will implement the added/changed gateway.
2038

    
2039
function save_gateway($gateway_settings, $realid = "") {
2040
	global $config;
2041

    
2042
	init_config_arr(array('gateways', 'gateway_item'));
2043
	$a_gateway_item = &$config['gateways']['gateway_item'];
2044
	$reloadif = "";
2045
	$gateway = array();
2046

    
2047
	if (empty($gateway_settings['interface'])) {
2048
		$gateway['interface'] = $gateway_settings['friendlyiface'];
2049
	} else {
2050
		$gateway['interface'] = $gateway_settings['interface'];
2051
	}
2052
	if (is_ipaddr($gateway_settings['gateway'])) {
2053
		$gateway['gateway'] = $gateway_settings['gateway'];
2054
	} else {
2055
		$gateway['gateway'] = "dynamic";
2056
	}
2057
	$gateway['name'] = $gateway_settings['name'];
2058
	$gateway['weight'] = $gateway_settings['weight'];
2059
	$gateway['ipprotocol'] = $gateway_settings['ipprotocol'];
2060
	if ($gateway_settings['interval']) {
2061
		$gateway['interval'] = $gateway_settings['interval'];
2062
	}
2063

    
2064
	if ($gateway_settings['time_period']) {
2065
		$gateway['time_period'] = $gateway_settings['time_period'];
2066
	}
2067
	if ($gateway_settings['alert_interval']) {
2068
		$gateway['alert_interval'] = $gateway_settings['alert_interval'];
2069
	}
2070

    
2071
	$gateway['descr'] = $gateway_settings['descr'];
2072
	if ($gateway_settings['monitor_disable'] == "yes") {
2073
		$gateway['monitor_disable'] = true;
2074
	}
2075
	if ($gateway_settings['action_disable'] == "yes") {
2076
		$gateway['action_disable'] = true;
2077
	}
2078
	if ($gateway_settings['nonlocalgateway'] == "yes") {
2079
		$gateway['nonlocalgateway'] = true;
2080
	}
2081
	if ($gateway_settings['force_down'] == "yes") {
2082
		$gateway['force_down'] = true;
2083
	}
2084
	if (is_ipaddr($gateway_settings['monitor'])) {
2085
		$gateway['monitor'] = $gateway_settings['monitor'];
2086
	}
2087
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] >= 0) {
2088
		$gateway['data_payload'] = $gateway_settings['data_payload'];
2089
	}
2090

    
2091
	/* NOTE: If gateway ip is changed need to cleanup the old static interface route */
2092
	if ($gateway_settings['monitor'] != "dynamic" &&
2093
	    !empty($a_gateway_item[$realid]) &&
2094
	    is_ipaddr($a_gateway_item[$realid]['gateway']) &&
2095
	    $gateway['gateway'] != $a_gateway_item[$realid]['gateway'] &&
2096
	    isset($a_gateway_item[$realid]["nonlocalgateway"])) {
2097
		route_del($a_gateway_item[$realid]['gateway']);
2098
	}
2099

    
2100
	/* NOTE: If monitor ip is changed need to cleanup the old static route */
2101
	if ($gateway_settings['monitor'] != "dynamic" &&
2102
	    !empty($a_gateway_item[$realid]) &&
2103
	    is_ipaddr($a_gateway_item[$realid]['monitor']) &&
2104
	    $gateway_settings['monitor'] != $a_gateway_item[$realid]['monitor'] &&
2105
	    $gateway['gateway'] != $a_gateway_item[$realid]['monitor']) {
2106
		route_del($a_gateway_item[$realid]['monitor']);
2107
	}
2108

    
2109
	if ($gateway_settings['defaultgw'] == "yes" || $gateway_settings['defaultgw'] == "on") {
2110
		// a new default gateway is being saved.
2111
		$i = 0;
2112
		/* remove the default gateway bits for all gateways with the same address family */
2113
		if (is_array($a_gateway_item)) {
2114
			foreach ($a_gateway_item as $gw) {
2115
				if ($gateway['ipprotocol'] == $gw['ipprotocol']) {
2116
					if ($gw['interface'] != $gateway_settings['interface'] &&
2117
						($gw['name'] == $config['gateways']['defaultgw4'] || $gw['name'] == $config['gateways']['defaultgw6'])) {
2118
						// remember the old default gateway interface to call with a "interface reconfigure" event.
2119
						$reloadif = $gw['interface'];
2120
					}
2121
				}
2122
				$i++;
2123
			}
2124
		}
2125
		if ($gateway['ipprotocol'] == "inet") {
2126
			$config['gateways']['defaultgw4'] = $gateway['name'];
2127
		} elseif ($gateway['ipprotocol'] == "inet6") {
2128
			$config['gateways']['defaultgw6'] = $gateway['name'];
2129
		}
2130
	}
2131

    
2132
	if ($gateway_settings['latencylow']) {
2133
		$gateway['latencylow'] = $gateway_settings['latencylow'];
2134
	}
2135
	if ($gateway_settings['latencyhigh']) {
2136
		$gateway['latencyhigh'] = $gateway_settings['latencyhigh'];
2137
	}
2138
	if ($gateway_settings['losslow']) {
2139
		$gateway['losslow'] = $gateway_settings['losslow'];
2140
	}
2141
	if ($gateway_settings['losshigh']) {
2142
		$gateway['losshigh'] = $gateway_settings['losshigh'];
2143
	}
2144
	if ($gateway_settings['loss_interval']) {
2145
		$gateway['loss_interval'] = $gateway_settings['loss_interval'];
2146
	}
2147
	/* when saving the manual gateway we use the attribute which has the corresponding id */
2148
	if (isset($realid) && $a_gateway_item[$realid]) {
2149
		$preserve_disabled = isset($a_gateway_item[$realid]['disabled']);
2150
		$a_gateway_item[$realid] = $gateway;
2151
		if ($preserve_disabled) {
2152
			$a_gateway_item[$realid]['disabled'] = true;
2153
		}
2154
	} else {
2155
		$a_gateway_item[] = $gateway;
2156
	}
2157
	gateway_set_enabled($gateway_settings['name'], !isset($gateway_settings['disabled']));
2158

    
2159
	mark_subsystem_dirty('staticroutes');
2160

    
2161
	write_config("Gateway settings changed");
2162

    
2163
	if (!empty($reloadif)) {
2164
		send_event("interface reconfigure {$reloadif}");
2165
	}
2166
}
2167

    
2168
function gateway_set_enabled($name, $enabled) {
2169
	global $config;
2170
	if (is_array($config['gateways']['gateway_item'])) {
2171
		foreach($config['gateways']['gateway_item'] as &$item) {
2172
			if ($item['name'] == $name) {
2173
				$gateway = &$item;
2174
			}
2175
		}
2176
	}
2177
	if (!isset($gateway)) {
2178
		return;
2179
	}
2180
	if ($enabled) {
2181
		unset($gateway['disabled']);
2182
	} else {
2183
		/* Check if the gateway was enabled but changed to disabled. */
2184
		if (!isset($gateway['disabled'])) {
2185
			/*  If the disabled gateway was the default route, remove the default route */
2186
			if (is_ipaddr($gateway['gateway'])) {
2187
				$inet = (!is_ipaddrv4($gateway['gateway']) ? 'inet6' : 'inet');
2188
				if ($inet == 'inet') {
2189
					$cgw = route_get_default('inet');
2190
				} else {
2191
					$cgw = route_get_default('inet6');
2192
				}
2193
				if ($gateway['gateway'] == $cgw) {
2194
					route_del("default", $inet);
2195
				}
2196
			}
2197
			$gateway['disabled'] = true;
2198
		}
2199
	}
2200
}
2201

    
2202
function gateway_or_gwgroup_exists($name) {
2203
	global $config;
2204
	if (is_array($config['gateways']['gateway_item'])) {
2205
		foreach($config['gateways']['gateway_item'] as $item) {
2206
			if ($item['name'] == $name) {
2207
				return true;
2208
			}
2209
		}
2210
	}
2211
	if (is_array($config['gateways']['gateway_group'])) {
2212
		foreach($config['gateways']['gateway_group'] as $item) {
2213
			if ($item['name'] == $name) {
2214
				return true;
2215
			}
2216
		}
2217
	}
2218
	return false;
2219
}
2220

    
2221
// These two replacement functions avoid the need to call return_gateways_array() multiple times and
2222
// allow system_gateways.php to get everything it needs in a single function call
2223
function gateway_getgwtiername($gways, $idx) {
2224
	global $config;
2225

    
2226
	$result = "";
2227
	$gwname = $gways[$idx]['name'];
2228

    
2229
	$gw = get_gateway_or_group_by_name($gwname, $gways);
2230
	init_config_arr(array('gateways'));
2231
	if ($config['gateways']['defaultgw4'] == $gwname || $config['gateways']['defaultgw6'] == $gwname) {
2232
		$result = "Default";
2233
	} else {
2234
		if ($gw['ipprotocol'] == 'inet') {
2235
			$defgw = get_gateway_or_group_by_name($config['gateways']['defaultgw4'], $gways);
2236
		} else {
2237
			$defgw = get_gateway_or_group_by_name($config['gateways']['defaultgw6'], $gways);
2238
		}
2239

    
2240
		if ($defgw['type'] == "gatewaygroup") {
2241
			$detail = gateway_is_gwgroup_member($gwname, true);
2242
			foreach($detail as $gwitem) {
2243
				if ($gwitem['name'] == $defgw['name']) {
2244
					if (isset($gwitem['tier'])) {
2245
						$result = "Tier " . $gwitem['tier'];
2246
						break;
2247
					}
2248
				}
2249
			}
2250
		}
2251
    }
2252

    
2253
	if (!empty($result)) {
2254
		if ($gw['ipprotocol'] == "inet") {
2255
			$result .= " (IPv4)";
2256
		} elseif ($gw['ipprotocol'] == "inet6") {
2257
			$result .= " (IPv6)";
2258
		}
2259
	}
2260

    
2261
	return $result;
2262
}
2263

    
2264
function get_gateway_or_group_by_name($gwname, $gateways_arr) {
2265
	global $config;
2266

    
2267
	foreach ($gateways_arr as $gw) {
2268
		if ($gw['name'] == $gwname) {
2269
			$gw['type'] = 'gateway';
2270
			return $gw;
2271
		}
2272
	}
2273

    
2274
	init_config_arr(array('gateways', 'gateway_group'));
2275
	foreach ($config['gateways']['gateway_group'] as $gwg) {
2276
		if ($gwg['name'] == $gwname) {
2277
			$gwg['type'] = 'gatewaygroup';
2278
			return $gwg;
2279
		}
2280
	}
2281

    
2282
	return false;
2283
}
2284

    
2285
// Compose a list of available gateways but without the need to call return_gateways_array() multiple times
2286
// Previously that function could be called eight times per gateway!
2287
function available_default_gateways() {
2288
	global $config;
2289

    
2290
	$gways = return_gateways_array(true, false, true, true);
2291

    
2292
	$items4 = array();
2293
	$items6 = array();
2294
	$items4[''] = "Automatic";
2295
	$items6[''] = "Automatic";
2296
	foreach($gways as $gw) {
2297
		$gwn = $gw['name'];
2298
		if ($gw['ipprotocol'] == "inet6") {
2299
			$items6[$gwn] = $gwn;
2300
		} else {
2301
			$items4[$gwn] = $gwn;
2302
		}
2303
	}
2304

    
2305
	$groups = return_gateway_groups_array(false, $gways);
2306
	foreach ($groups as $key => $group) {
2307
		$gwn = $group['descr'];
2308
		if ($group['ipprotocol'] == "inet6") {
2309
			$items6[$key] = "$key ($gwn)";
2310
		} else {
2311
			$items4[$key] = "$key ($gwn)";
2312
		}
2313
	}
2314
	$items4['-'] = "None";
2315
	$items6['-'] = "None";
2316

    
2317
	$defaults = array();
2318
	$defaults['v4'] = $items4;
2319
	$defaults['v6'] = $items6;
2320
	$defaults['defaultgw4'] = $config['gateways']['defaultgw4'];
2321
	$defaults['defaultgw6'] = $config['gateways']['defaultgw6'];
2322

    
2323
	return $defaults;
2324
}
2325

    
2326

    
2327
?>
(21-21/61)