Project

General

Profile

Download (69.4 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-2018 Rubicon Communications, LLC (Netgate)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
require_once("config.inc");
24
require_once("rrd.inc");
25
require_once("ipsec.inc");
26

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

    
41
function running_dpinger_processes() {
42
	global $g;
43

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

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

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

    
66
	return $result;
67
}
68

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

    
77
	$running_processes = running_dpinger_processes();
78

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

    
84
		if (isvalidpid($process['pidfile'])) {
85
			killbypid($process['pidfile']);
86
		} else {
87
			@unlink($process['pidfile']);
88
		}
89
	}
90
}
91

    
92
function start_dpinger($gateway) {
93
	global $g;
94

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

    
99
	$dpinger_defaults = return_dpinger_defaults();
100

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
259
				$gwifip = find_interface_ipv6_ll($gateway['interface'], true);
260

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

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

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

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

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

    
298
	stop_dpinger();
299

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

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

    
311
	return;
312
}
313

    
314
function get_dpinger_status($gwname, $detailed = false) {
315
	global $g;
316

    
317
	$running_processes = running_dpinger_processes();
318

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

    
324
	$proc = $running_processes[$gwname];
325
	unset($running_processes);
326

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

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

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

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

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

    
370
	$r['srcip'] = $proc['srcip'];
371
	$r['targetip'] = $proc['targetip'];
372

    
373
	$gateways_arr = return_gateways_array();
374
	unset($gw);
375
	if (isset($gateways_arr[$gwname])) {
376
		$gw = $gateways_arr[$gwname];
377
	}
378

    
379
	$r['latency_avg'] = round($r['latency_avg']/1000, 3);
380
	$r['latency_stddev'] = round($r['latency_stddev']/1000, 3);
381

    
382
	$r['status'] = "none";
383
	if (isset($gw) && isset($gw['force_down'])) {
384
		$r['status'] = "force_down";
385
	} else if (isset($gw)) {
386
		$settings = return_dpinger_defaults();
387

    
388
		$keys = array(
389
		    'latencylow',
390
		    'latencyhigh',
391
		    'losslow',
392
		    'losshigh'
393
		);
394

    
395
		/* Replace default values by user-defined */
396
		foreach ($keys as $key) {
397
			if (isset($gw[$key]) && is_numeric($gw[$key])) {
398
				$settings[$key] = $gw[$key];
399
			}
400
		}
401

    
402
		if ($r['latency_avg'] > $settings['latencyhigh']) {
403
			if ($detailed) {
404
				$r['status'] = "highdelay";
405
			} else {
406
				$r['status'] = "down";
407
			}
408
		} else if ($r['loss'] > $settings['losshigh']) {
409
			if ($detailed) {
410
				$r['status'] = "highloss";
411
			} else {
412
				$r['status'] = "down";
413
			}
414
		} else if ($r['latency_avg'] > $settings['latencylow']) {
415
			$r['status'] = "delay";
416
		} else if ($r['loss'] > $settings['losslow']) {
417
			$r['status'] = "loss";
418
		}
419
	}
420

    
421
	return $r;
422
}
423

    
424
/* return the status of the dpinger targets as an array */
425
function return_gateways_status($byname = false) {
426
	global $config, $g;
427

    
428
	$dpinger_gws = running_dpinger_processes();
429
	$status = array();
430

    
431
	$gateways_arr = return_gateways_array();
432

    
433
	foreach ($dpinger_gws as $gwname => $gwdata) {
434
		// If action is disabled for this gateway, then we want a detailed status.
435
		// That reports "highdelay" or "highloss" rather than just "down".
436
		// Because reporting the gateway down would be misleading (gateway action is disabled)
437
		$detailed = $gateways_arr[$gwname]['action_disable'];
438
		$dpinger_status = get_dpinger_status($gwname, $detailed);
439
		if ($dpinger_status === false) {
440
			continue;
441
		}
442

    
443
		if ($byname == false) {
444
			$target = $dpinger_status['targetip'];
445
		} else {
446
			$target = $gwname;
447
		}
448

    
449
		$status[$target] = array();
450
		$status[$target]['monitorip'] = $dpinger_status['targetip'];
451
		$status[$target]['srcip'] = $dpinger_status['srcip'];
452
		$status[$target]['name'] = $gwname;
453
		$status[$target]['delay'] = empty($dpinger_status['latency_avg']) ? "0ms" : $dpinger_status['latency_avg'] . "ms";
454
		$status[$target]['stddev'] = empty($dpinger_status['latency_stddev']) ? "0ms" : $dpinger_status['latency_stddev'] . "ms";
455
		$status[$target]['loss'] = empty($dpinger_status['loss']) ? "0.0%" : round($dpinger_status['loss'], 1) . "%";
456
		$status[$target]['status'] = $dpinger_status['status'];
457
	}
458

    
459
	/* tack on any gateways that have monitoring disabled
460
	 * or are down, which could cause gateway groups to fail */
461
	$gateways_arr = return_gateways_array();
462
	foreach ($gateways_arr as $gwitem) {
463
		if (!isset($gwitem['monitor_disable'])) {
464
			continue;
465
		}
466
		if (!is_ipaddr($gwitem['monitor'])) {
467
			$realif = $gwitem['interface'];
468
			$tgtip = get_interface_gateway($realif);
469
			if (!is_ipaddr($tgtip)) {
470
				$tgtip = "none";
471
			}
472
			$srcip = find_interface_ip($realif);
473
		} else {
474
			$tgtip = $gwitem['monitor'];
475
			$srcip = find_interface_ip($realif);
476
		}
477
		if ($byname == true) {
478
			$target = $gwitem['name'];
479
		} else {
480
			$target = $tgtip;
481
		}
482

    
483
		/* failsafe for down interfaces */
484
		if ($target == "none") {
485
			$target = $gwitem['name'];
486
			$status[$target]['name'] = $gwitem['name'];
487
			$status[$target]['delay'] = "0.0ms";
488
			$status[$target]['loss'] = "100.0%";
489
			$status[$target]['status'] = "down";
490
		} else {
491
			$status[$target]['monitorip'] = $tgtip;
492
			$status[$target]['srcip'] = $srcip;
493
			$status[$target]['name'] = $gwitem['name'];
494
			$status[$target]['delay'] = "";
495
			$status[$target]['loss'] = "";
496
			$status[$target]['status'] = "none";
497
		}
498

    
499
		$status[$target]['monitor_disable'] = true;
500
	}
501
	return($status);
502
}
503

    
504
function return_gateways_status_text($byname = false, $brief = false) {
505
	$gwstat = return_gateways_status($byname);
506
	$output = "";
507
	$widths = array();
508
	$col_sep = 2;
509
	if ($brief) {
510
		$collist = array('status' => "Status");
511
	} else {
512
		$collist = array('monitorip' => "Monitor",
513
				'srcip' => "Source",
514
				'delay' => "Delay",
515
				'stddev' => "StdDev",
516
				'loss' => "Loss",
517
				'status' => "Status");
518
	}
519
	foreach ($gwstat as $gw) {
520
		foreach ($gw as $gwdidx => $gwdata) {
521
			if (strlen($gwdata) > $widths[$gwdidx]) {
522
				$widths[$gwdidx] = strlen($gwdata);
523
			}
524
		}
525
	}
526

    
527
	$output .= str_pad("Name", $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
528
	foreach ($collist as $hdrcol => $hdrdesc) {
529
		if (strlen($hdrdesc) > $widths[$hdrcol]) {
530
			$widths[$hdrcol] = strlen($hdrdesc);
531
		}
532
		$output .= str_pad($hdrdesc, $widths[$hdrcol] + $col_sep, " ", (substr($hdrcol, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
533
	}
534
	$output .= "\n";
535

    
536
	foreach ($gwstat as $idx => $gw) {
537
		$output .= str_pad($gw['name'], $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
538
		foreach (array_keys($collist) as $col) {
539
			$output .= str_pad($gw[$col], $widths[$col] + $col_sep, " ", (substr($col, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
540
		}
541
		$output .= "\n";
542
	}
543

    
544
	return $output;
545
}
546

    
547
function compare_gateway_order_configured($a, $b) {
548
	if ($a['attribute'] === $b['attribute']) {
549
		if ($a['attribute'] === 'system') {
550
			$res = (($a['name'] < $b['name'])) ? -1 : 1;
551
			return $res;
552
		}
553
		return 0;
554
	}
555
	if ($a['attribute'] === 'system' || $b['attribute'] === 'system') {
556
		$res = (($b['attribute'] === 'system')) ? -1 : 1;
557
		return $res;
558
	}
559
	$res = ($a['attribute'] < $b['attribute']) ? -1 : 1;
560
	return $res;
561
}
562

    
563
function order_gateways_as_configured($gateways_arr) {
564
	uasort($gateways_arr, 'compare_gateway_order_configured');
565
	return $gateways_arr;
566
}
567

    
568
/* Return all configured gateways on the system
569
   $disabled = true - include gateways that are disabled
570
   $localhost = true - include "Null" entries for localhost IP addresses
571
   $inactive = true - include gateways on inactive interfaces
572
   $integer_index = true - index the returned array by integers 0,1,2,... instead of by GW name
573
*/
574
function return_gateways_array($disabled = false, $localhost = false, $inactive = false, $integer_index = false) {
575
	global $config, $g;
576

    
577
	$gateways_arr = array();
578
	$gateways_arr_temp = array();
579
	$cgw4 = getcurrentdefaultgatewayip('inet');
580
	$cgw6 = getcurrentdefaultgatewayip('inet6');
581
	$found_defaultv4 = 0;
582
	$found_defaultv6 = 0;
583

    
584
	// Ensure the interface cache is up to date first
585
	$interfaces = get_interface_arr(true);
586

    
587
	$i = -1;
588
	/* Process/add all the configured gateways. */
589
	if (is_array($config['gateways']) && is_array($config['gateways']['gateway_item'])) {
590
		foreach ($config['gateways']['gateway_item'] as $gateway) {
591
			/* Increment it here to do not skip items */
592
			$i++;
593
			if (isset($gateway['defaultgw'])) {
594
				unset($gateway['defaultgw']);
595
			}
596

    
597
			if (empty($config['interfaces'][$gateway['interface']])) {
598
				if ($inactive === false) {
599
					continue;
600
				} else {
601
					$gateway['inactive'] = true;
602
				}
603
			}
604
			$wancfg = $config['interfaces'][$gateway['interface']];
605

    
606
			/* skip disabled interfaces */
607
			if ($disabled === false && (!isset($wancfg['enable']))) {
608
				continue;
609
			}
610

    
611
			/* if the gateway is dynamic and we can find the IPv4, Great! */
612
			if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
613
				if ($gateway['ipprotocol'] == "inet") {
614
					/* we know which interfaces is dynamic, this should be made a function */
615
					$gateway['gateway'] = get_interface_gateway($gateway['interface']);
616
					/* no IP address found, set to dynamic */
617
					if (!is_ipaddrv4($gateway['gateway'])) {
618
						$gateway['gateway'] = "dynamic";
619
					}
620
					$gateway['dynamic'] = true;
621
				}
622

    
623
				/* if the gateway is dynamic and we can find the IPv6, Great! */
624
				else if ($gateway['ipprotocol'] == "inet6") {
625
					/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
626
					$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
627
					/* no IPv6 address found, set to dynamic */
628
					if (!is_ipaddrv6($gateway['gateway'])) {
629
						$gateway['gateway'] = "dynamic";
630
					}
631
					$gateway['dynamic'] = true;
632
				}
633
			} else {
634
				/* getting this detection right is hard at this point because we still don't
635
				 * store the address family in the gateway item */
636
				if (is_ipaddrv4($gateway['gateway'])) {
637
					$gateway['ipprotocol'] = "inet";
638
				} else if (is_ipaddrv6($gateway['gateway'])) {
639
					$gateway['ipprotocol'] = "inet6";
640
				}
641
			}
642

    
643
			if (isset($gateway['monitor_disable'])) {
644
				$gateway['monitor_disable'] = true;
645
			} else if (empty($gateway['monitor'])) {
646
				$gateway['monitor'] = $gateway['gateway'];
647
			}
648

    
649
			if (isset($gateway['action_disable'])) {
650
				$gateway['action_disable'] = true;
651
			}
652

    
653
			$gateway['friendlyiface'] = $gateway['interface'];
654

    
655
			/* special treatment for tunnel interfaces */
656
			if ($gateway['ipprotocol'] == "inet6") {
657
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
658
			} else {
659
				$gateway['interface'] = get_real_interface($gateway['interface'], "inet", false, false);
660
			}
661

    
662
			if ($gateway['ipprotocol'] == "inet" &&
663
					($config['gateways']['defaultgw4'] == $gateway['name'] || $gateway['gateway'] == $cgw4)) {
664
				$gateway['isdefaultgw'] = true;
665
				$found_defaultv4 = 1;
666
			} else if ($gateway['ipprotocol'] == "inet6" &&
667
					($config['gateways']['defaultgw6'] == $gateway['name'] || $gateway['gateway'] == $cgw6)) {
668
				$gateway['isdefaultgw'] = true;
669
				$found_defaultv6 = 1;
670
			}
671
			/* include the gateway index as the attribute */
672
			$gateway['attribute'] = $i;
673

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

    
678
			/* skip disabled gateways if the caller has not asked for them to be returned. */
679
			if (!($disabled === false && isset($gateway['disabled']))) {
680
				$gateways_arr[$gateway['name']] = $gateway;
681
			}
682
		}
683
	}
684
	unset($gateway);
685

    
686
	//Sort the array by GW name before moving on.
687
	ksort($gateways_arr, SORT_STRING | SORT_FLAG_CASE);
688

    
689
	/* Loop through all interfaces with a gateway and add it to a array */
690
	if ($disabled == false) {
691
		$iflist = get_configured_interface_with_descr();
692
	} else {
693
		$iflist = get_configured_interface_with_descr(true);
694
	}
695

    
696
	/* Process/add dynamic v4 gateways. */
697
	foreach ($iflist as $ifname => $friendly) {
698
		if (!interface_has_gateway($ifname)) {
699
			continue;
700
		}
701

    
702
		if (empty($config['interfaces'][$ifname])) {
703
			continue;
704
		}
705

    
706
		$ifcfg = &$config['interfaces'][$ifname];
707
		if (!isset($ifcfg['enable'])) {
708
			continue;
709
		}
710

    
711
		if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
712
			continue;
713
		}
714

    
715
		$ctype = "";
716
		switch ($ifcfg['ipaddr']) {
717
			case "dhcp":
718
			case "pppoe":
719
			case "l2tp":
720
			case "pptp":
721
			case "ppp":
722
				$ctype = strtoupper($ifcfg['ipaddr']);
723
				break;
724
			default:
725
				$tunnelif = substr($ifcfg['if'], 0, 3);
726
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
727
					switch (substr($ifcfg['if'], 4, 1)) {
728
						case "c":
729
							$ovpntype = "openvpn-client";
730
							break;
731
						case "s":
732
							$ovpntype = "openvpn-server";
733
							break;
734
						default:
735
							// unknown ovpn type
736
							continue 2;
737
					}
738
					$ovpnid = substr($ifcfg['if'], 5);
739
					if (is_array($config['openvpn'][$ovpntype])) {
740
						foreach ($config['openvpn'][$ovpntype] as & $ovpnconf) {
741
							if ($ovpnconf['vpnid'] == $ovpnid) {
742
								// skip IPv6-only interfaces
743
								if ($ovpnconf['create_gw'] == "v6only") {
744
									continue 3;
745
								}
746
								// skip tap interfaces
747
								if ($ovpnconf['dev_mode'] == "tap") {
748
									continue 3;
749
								}
750
							}
751
						}
752
					}
753
					$ctype = "VPNv4";
754
				} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
755
					$ikeid = substr($ifcfg['if'], 5);
756
					if (is_array($config['ipsec']) && is_array($config['ipsec']['phase1']) && is_array($config['ipsec']['phase2'])) {
757
						foreach ($config['ipsec']['phase1'] as $ph1ent) {
758
							if ($ph1ent['disabled']) {
759
								continue;
760
							}
761
							$vtisubnet_spec = ipsec_vti($ph1ent, true);
762
							// Skip non-VTI tunnels
763
							if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
764
								continue;
765
							}
766
							if (!isset($ph1ent['mobile']) && ($keyexchange == 'ikev1' || isset($ph1ent['splitconn']))) {
767
								foreach ($vtisubnet_spec as $idx => $vtisub) {
768
									if ($ifcfg['if'] == "ipsec{$ph1ent['ikeid']}00{$idx}") {
769
										// If this specific VTI remote is v4, then we can make a v4 gw
770
										if (is_ipaddrv4($vtisub['right'])) {
771
											$ctype = "VTIv4";
772
										}
773
									}
774
								}
775
							} else {
776
								if ($ifcfg['if'] == "ipsec{$ph1ent['ikeid']}000") {
777
									// If any of the VTI remotes are v4, then we can make a v4 gw
778
									foreach ($vtisubnet_spec as $vtisub) {
779
										if (is_ipaddrv4($vtisub['right'])) {
780
											$ctype = "VTIv4";
781
										}
782
									}
783
								}
784
							}
785
						}
786
						if (empty($ctype)) {
787
							continue 2;
788
						}
789
					}
790
				} elseif ($tunnelif == "gif" || $tunnelif == "gre") {
791
					$ctype = "TUNNELv4";
792
				}
793
				break;
794
		}
795
		$ctype = "_". strtoupper($ctype);
796

    
797
		$gateway = array();
798
		$gateway['dynamic'] = false;
799
		$gateway['ipprotocol'] = "inet";
800
		$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
801
		$gateway['interface'] = get_real_interface($ifname);
802
		$gateway['friendlyiface'] = $ifname;
803
		$gateway['name'] = "{$friendly}{$ctype}";
804
		$gateway['attribute'] = "system";
805

    
806
		if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
807
			$gateway['isdefaultgw'] = true;
808
			$gateway['dynamic'] = true;
809
			$found_defaultv4 = 1;
810
		}
811

    
812
		/* Loopback dummy for dynamic interfaces without a IP */
813
		if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
814
			$gateway['gateway'] = "dynamic";
815
		}
816

    
817
		/* automatically skip known static and dynamic gateways that were previously processed */
818
		foreach ($gateways_arr_temp as $gateway_item) {
819
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
820
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
821
				continue 2;
822
			}
823
		}
824

    
825
		if (is_ipaddrv4($gateway['gateway'])) {
826
			$gateway['monitor'] = $gateway['gateway'];
827
		}
828

    
829
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
830
		$gateways_arr[$gateway['name']] = $gateway;
831
	}
832
	unset($gateway);
833

    
834
	/* Process/add dynamic v6 gateways. */
835
	foreach ($iflist as $ifname => $friendly) {
836
		/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
837
		if (!isset($config['system']['ipv6allow'])) {
838
			break;
839
		}
840

    
841
		if (!interface_has_gatewayv6($ifname)) {
842
			continue;
843
		}
844

    
845
		if (empty($config['interfaces'][$ifname])) {
846
			continue;
847
		}
848

    
849
		$ifcfg = &$config['interfaces'][$ifname];
850
		if (!isset($ifcfg['enable'])) {
851
			continue;
852
		}
853

    
854
		if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
855
			continue;
856
		}
857

    
858
		$ctype = "";
859
		switch ($ifcfg['ipaddrv6']) {
860
			case "slaac":
861
			case "dhcp6":
862
			case "6to4":
863
			case "6rd":
864
				$ctype = strtoupper($ifcfg['ipaddrv6']);
865
				break;
866
			default:
867
				$tunnelif = substr($ifcfg['if'], 0, 3);
868
				if (substr($ifcfg['if'], 0, 4) == "ovpn") {
869
					switch (substr($ifcfg['if'], 4, 1)) {
870
						case "c":
871
							$ovpntype = "openvpn-client";
872
							break;
873
						case "s":
874
							$ovpntype = "openvpn-server";
875
							break;
876
						default:
877
							// unknown ovpn type
878
							continue 2;
879
					}
880
					$ovpnid = substr($ifcfg['if'], 5);
881
					if (is_array($config['openvpn'][$ovpntype])) {
882
						foreach ($config['openvpn'][$ovpntype] as & $ovpnconf) {
883
							if ($ovpnconf['vpnid'] == $ovpnid) {
884
								// skip IPv4-only interfaces
885
								if ($ovpnconf['create_gw'] == "v4only") {
886
									continue 3;
887
								}
888
								// skip tap interfaces
889
								if ($ovpnconf['dev_mode'] == "tap") {
890
									continue 3;
891
								}
892
							}
893
						}
894
					}
895
					$ctype = "VPNv6";
896
				} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
897
					$ikeid = substr($ifcfg['if'], 5);
898
					if (is_array($config['ipsec']) && is_array($config['ipsec']['phase1']) && is_array($config['ipsec']['phase2'])) {
899
						foreach ($config['ipsec']['phase1'] as $ph1ent) {
900
							if ($ph1ent['disabled']) {
901
								continue;
902
							}
903
							$vtisubnet_spec = ipsec_vti($ph1ent, true);
904
							// Skip non-VTI tunnels
905
							if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
906
								continue;
907
							}
908
							if (!isset($ph1ent['mobile']) && ($keyexchange == 'ikev1' || isset($ph1ent['splitconn']))) {
909
								foreach ($vtisubnet_spec as $idx => $vtisub) {
910
									if ($ifcfg['if'] == "ipsec{$ph1ent['ikeid']}00{$idx}") {
911
										// If this specific VTI remote is v6, then we can make a v6 gw
912
										if (is_ipaddrv6($vtisub['right'])) {
913
											$ctype = "VTIv6";
914
										}
915
									}
916
								}
917
							} else {
918
								if ($ifcfg['if'] == "ipsec{$ph1ent['ikeid']}000") {
919
									// If any of the VTI remotes are v6, then we can make a v6 gw
920
									foreach ($vtisubnet_spec as $vtisub) {
921
										if (is_ipaddrv6($vtisub['right'])) {
922
											$ctype = "VTIv6";
923
										}
924
									}
925
								}
926
							}
927
						}
928
						if (empty($ctype)) {
929
							continue 2;
930
						}
931
					}
932
				} else if ($tunnelif == "gif" || $tunnelif == "gre") {
933
					$ctype = "TUNNELv6";
934
				}
935
				break;
936
		}
937
		$ctype = "_". strtoupper($ctype);
938

    
939
		$gateway = array();
940
		$gateway['dynamic'] = false;
941
		$gateway['ipprotocol'] = "inet6";
942
		$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
943
		$gateway['interface'] = get_real_interface($ifname, "inet6");
944
		switch ($ifcfg['ipaddrv6']) {
945
			case "6rd":
946
			case "6to4":
947
				$gateway['dynamic'] = "default";
948
				break;
949
		}
950
		$gateway['friendlyiface'] = $ifname;
951
		$gateway['name'] = "{$friendly}{$ctype}";
952
		$gateway['attribute'] = "system";
953

    
954
		if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) {
955
			$gateway['isdefaultgw'] = true;
956
			$gateway['dynamic'] = true;
957
			$found_defaultv6 = 1;
958
		}
959

    
960
		/* Loopback dummy for dynamic interfaces without a IP */
961
		if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
962
			$gateway['gateway'] = "dynamic";
963
		}
964

    
965
		/* automatically skip known static and dynamic gateways that were previously processed */
966
		foreach ($gateways_arr_temp as $gateway_item) {
967
			if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
968
			    (($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
969
				continue 2;
970
			}
971
		}
972

    
973
		if (is_ipaddrv6($gateway['gateway'])) {
974
			$gateway['monitor'] = $gateway['gateway'];
975
		}
976

    
977
		$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
978
		$gateways_arr[$gateway['name']] = $gateway;
979
	}
980
	unset($gateway);
981

    
982
	/* FIXME: Should this be enabled.
983
	 * Some interface like wan might be default but have no info recorded
984
	 * the config. */
985
	/* this is a fallback if all else fails and we want to get packets out @smos */
986
	if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
987
		foreach ($gateways_arr as &$gateway) {
988
			if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
989
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw")) {
990
					$gateway['isdefaultgw'] = true;
991
					$found_defaultv4 = 1;
992
				}
993
			}
994
			else if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
995
				if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgwv6")) {
996
					$gateway['isdefaultgw'] = true;
997
					$found_defaultv6 = 1;
998
				}
999
			}
1000
		}
1001
	}
1002

    
1003
	if ($localhost === true) {
1004
		/* attach localhost for Null routes */
1005
		$gwlo4 = array();
1006
		$gwlo4['name'] = "Null4";
1007
		$gwlo4['interface'] = "lo0";
1008
		$gwlo4['ipprotocol'] = "inet";
1009
		$gwlo4['gateway'] = "127.0.0.1";
1010
		$gwlo4['attribute'] = "system";
1011
		$gwlo6 = array();
1012
		$gwlo6['name'] = "Null6";
1013
		$gwlo6['interface'] = "lo0";
1014
		$gwlo6['ipprotocol'] = "inet6";
1015
		$gwlo6['gateway'] = "::1";
1016
		$gwlo6['attribute'] = "system";
1017
		$gateways_arr['Null4'] = $gwlo4;
1018
		$gateways_arr['Null6'] = $gwlo6;
1019
	}
1020

    
1021
	if ($integer_index) {
1022
		$gateways_arr = array_values($gateways_arr);
1023
	}
1024

    
1025
	if ($found_defaultv4 != 1 && is_ipaddr($cgw4)) {
1026
		foreach($gateways_arr as &$gw) {
1027
			if ($gw['gateway'] == $cgw4) {
1028
				$gw['isdefaultgw'] = true;
1029
			}
1030
		}
1031
	}
1032
	if ($found_defaultv6 != 1 && is_ipaddr($cgw6)) {
1033
		foreach($gateways_arr as &$gw) {
1034
			if ($gw['gateway'] == $cgw6) {
1035
				$gw['isdefaultgw'] = true;
1036
			}
1037
		}
1038
	}
1039
	return order_gateways_as_configured($gateways_arr);
1040
}
1041

    
1042
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) {
1043
	global $config, $g;
1044
	/*
1045
	 * NOTE: The code below is meant to replace the default gateway when it goes down.
1046
	 *	This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
1047
	 */
1048
	$set_dfltgwname = '';
1049

    
1050
	if ($ipprotocol == 'inet') {
1051
		$gwdefault = $config['gateways']['defaultgw4'];
1052
	} else {
1053
		$gwdefault = $config['gateways']['defaultgw6'];
1054
	}
1055
	if ($gwdefault == "-") {
1056
		// 'none', dont set the default gateway, useful if routes are managed by frr/bgp/ospf or similar
1057
		return;
1058
	}
1059
	if (isset($gateways_arr[$gwdefault])) {
1060
		// the configured gateway is a regular one. (not a gwgroup) use it as is..
1061
		$set_dfltgwname = $gwdefault;
1062
	} elseif (empty($gwdefault)) {
1063
		// 'automatic' mode, pick the first one thats 'up' or 'unmonitored' which is always considered up
1064
		$gateways_arr = order_gateways_as_configured($gateways_arr);
1065

    
1066
		foreach($gateways_arr as $gwname => $gwsttng) {
1067
			if ($gwsttng['ipprotocol'] != $ipprotocol) {
1068
				continue;
1069
			}
1070
			
1071
			if ((isset($gwsttng['monitor_disable']) || isset($gwsttng['action_disable']) || $gateways_status[$gwname]['status'] == "none")) {
1072
				$set_dfltgwname = $gwname;
1073
				break;
1074
			}
1075
		}
1076
	} else {
1077
		// a gwgroup is selected
1078
		// find the best available gateway given options available..
1079
		$gwg_members = array();
1080
		$viplist = get_configured_vip_list();
1081
		if (is_array($config['gateways']['gateway_group'])) {
1082
			foreach ($config['gateways']['gateway_group'] as $group) {
1083
				if ($group['name'] == $gwdefault) {
1084
					// finds the gw members of the best available tier for this group.
1085
					$gwg_members = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1086
				}
1087
			}
1088
		}
1089

    
1090
		if (count($gwg_members) > 0) {
1091
			$currentdefaultgwip = getcurrentdefaultgatewayip($ipprotocol);
1092
			$found_current = false;
1093
			foreach($gwg_members as $gwgroupitem) {
1094
				if ($gwgroupitem['gwip'] == $currentdefaultgwip) {
1095
					$set_dfltgwname = $gwgroupitem['gw'];
1096
					$found_current = true;
1097
					log_error("Keep current gateway, its already part of the group members.");
1098
					break;
1099
				}
1100
			}
1101
			if (!$found_current) {
1102
				$set_dfltgwname = $gwg_members[0]['gw'];
1103
				log_error(sprintf("Gateway, switch to: %s", $set_dfltgwname));
1104
			}
1105
		} else {
1106
			log_error("Gateway, NONE AVAILABLE");
1107
		}
1108
	}
1109
	if (!empty($set_dfltgwname) && isset($gateways_arr[$set_dfltgwname])) {
1110
		setdefaultgateway($gateways_arr[$set_dfltgwname]);
1111
	}
1112
}
1113

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

    
1118
function setdefaultgateway($gw) {
1119
	global $g, $config;
1120
	if (isset($config['system']['route-debug'])) {
1121
		file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']}");
1122
	}
1123
	$ipprotocol = $gw['ipprotocol'];
1124
	if ($gw['gateway'] == "dynamic") {
1125
		if ($ipprotocol == 'inet') {
1126
			$gw['gateway'] = get_interface_gateway($gw['friendlyiface']);
1127
		} else {
1128
			$gw['gateway'] = get_interface_gateway_v6($$gw['friendlyiface']);
1129
		}
1130
	}
1131
	if ($ipprotocol == 'inet6' && !is_ipaddrv6($gw['gateway'])) {
1132
		return;
1133
	}
1134
	if ($ipprotocol == 'inet' && !is_ipaddrv4($gw['gateway'])) {
1135
		return;
1136
	}
1137
	if ($ipprotocol == 'inet6') {
1138
		if (is_linklocal($gw['gateway']) && get_ll_scope($gw['gateway']) == '') {
1139
			$gw['gateway'] .= "%" . $gw['interface'];
1140
		}
1141
	}
1142
	$currentdefaultgwip = getcurrentdefaultgatewayip($ipprotocol);
1143
	if ($currentdefaultgwip != $gw['gateway']) {
1144
		log_error("Default gateway setting {$gw['descr']} as default.");
1145

    
1146
		if ($ipprotocol == 'inet') {
1147
			array_map('unlink', glob("{$g['tmp_path']}/*_defaultgw", GLOB_BRACE));
1148
		} else {
1149
			array_map('unlink', glob("{$g['tmp_path']}/*_defaultgwv6", GLOB_BRACE));
1150
		}
1151
		$defaultif = get_real_interface($gw['interface']);
1152
		if ($defaultif) {
1153
			@file_put_contents("{$g['tmp_path']}/{$defaultif}_defaultgw", $gw['gateway']);
1154
		}
1155

    
1156
		if (isset($gw["nonlocalgateway"])) {
1157
			if (is_ipaddr($gw['gateway']) && !empty($gw['interface'])) {
1158
				route_add_or_change("-{$ipprotocol} {$gw['gateway']} -iface {$gw['interface']}");
1159
			}
1160
		}
1161
		if (isset($config['system']['route-debug'])) {
1162
			file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']} ({$gw['gateway']})");
1163
		}
1164
		route_add_or_change("-{$ipprotocol} default {$gw['gateway']}");
1165
		return true;
1166
	}
1167
}
1168

    
1169
function get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist){
1170
	$result = array();
1171
	/* create array with group gateways members separated by tier */
1172
	$tiers = array();
1173
	$backupplan = array();
1174
	$gwvip_arr = array();
1175
	foreach ($group['item'] as $item) {
1176
		list($gwname, $tier, $vipname) = explode("|", $item);
1177

    
1178
		if (is_ipaddr($viplist[$vipname])) {
1179
			if (!is_array($gwvip_arr[$group['name']])) {
1180
				$gwvip_arr[$group['name']] = array();
1181
			}
1182
			$gwvip_arr[$group['name']][$gwname] = $vipname;
1183
		}
1184

    
1185
		/* Do it here rather than reiterating again the group in case no member is up. */
1186
		if (!is_array($backupplan[$tier])) {
1187
			$backupplan[$tier] = array();
1188
		}
1189
		$backupplan[$tier][] = $gwname;
1190

    
1191
		/* check if the gateway is available before adding it to the array */
1192
		if (is_array($gateways_status[$gwname])) {
1193
			$status = $gateways_status[$gwname];
1194
			$gwdown = false;
1195
			if (stristr($status['status'], "down")) {
1196
				$msg = sprintf(gettext('MONITOR: %1$s is down, omitting from routing group %2$s'), $gwname, $group['name']);
1197
				$gwdown = true;
1198
			} else if (stristr($status['status'], "loss") && strstr($group['trigger'], "loss")) {
1199
				/* packet loss */
1200
				$msg = sprintf(gettext('MONITOR: %1$s has packet loss, omitting from routing group %2$s'), $gwname, $group['name']);
1201
				$gwdown = true;
1202
			} else if (stristr($status['status'], "delay") && strstr($group['trigger'] , "latency")) {
1203
				/* high latency */
1204
				$msg = sprintf(gettext('MONITOR: %1$s has high latency, omitting from routing group %2$s'), $gwname, $group['name']);
1205
				$gwdown = true;
1206
			}
1207
			$statuschanged = false;
1208
			$pluginparams = array();
1209
			$pluginparams['type'] = 'gateway';
1210
			$pluginparams['name'] = $gwname;
1211
			if ($gwdown == true) {
1212
				if (!file_exists("/tmp/.down.{$gwname}")) {
1213
					touch("/tmp/.down.{$gwname}");
1214
					$msg .= "\n".implode("|", $status);
1215
					$pluginparams['event'] = 'gateway.down';
1216
					$statuschanged = true;
1217
				}
1218
			} else {
1219
				/* Online add member */
1220
				if (!is_array($tiers[$tier])) {
1221
					$tiers[$tier] = array();
1222
				}
1223
				$tiers[$tier][] = $gwname;
1224
				if (unlink_if_exists("/tmp/.down.{$gwname}")) {
1225
					$msg = sprintf(getmypid () . gettext('MONITOR: %1$s is available now, adding to routing group %2$s'), $gwname, $group['name']);
1226
					$msg .= "\n".implode("|", $status);
1227
					$pluginparams['event'] = 'gateway.up';
1228
					$statuschanged = true;
1229
				}
1230
			}
1231
			if ($statuschanged) {
1232
				log_error($msg);
1233
				notify_via_growl($msg);
1234
				notify_via_smtp($msg);
1235
				if (isset($gateways_arr[$gwname]['interface'])) {
1236
					$pluginparams['interface'] = $gateways_arr[$gwname]['interface'];
1237
				}
1238
				pkg_call_plugins('plugin_gateway', $pluginparams);
1239
			}
1240
		} else if (isset($gateways_arr[$gwname]['monitor_disable']) || isset($gateways_arr[$gwname]['action_disable'])) {
1241
			$tiers[$tier][] = $gwname;
1242
		}
1243
	}
1244
	$tiers_count = count($tiers);
1245
	if ($tiers_count == 0) {
1246
		/* Oh dear, we have no members! Engage Plan B */
1247
		if (!platform_booting()) {
1248
			$msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']);
1249
			log_error($msg);
1250
			notify_via_growl($msg);
1251
			//notify_via_smtp($msg);
1252
		}
1253
		$tiers = $backupplan;
1254
	}
1255
	/* sort the tiers array by the tier key */
1256
	ksort($tiers);
1257

    
1258
	/* we do not really foreach the tiers as we stop after the first tier */
1259
	foreach ($tiers as $tieridx => $tier) {
1260
		/* process all gateways in this tier */
1261
		foreach ($tier as $member) {
1262
			/* determine interface gateway */
1263
			if (isset($gateways_arr[$member])) {
1264
				$gateway = $gateways_arr[$member];
1265
				$int = $gateway['interface'];
1266
				$gatewayip = "";
1267
				if (is_ipaddr($gateway['gateway'])) {
1268
					$gatewayip = $gateway['gateway'];
1269
				} else if (!empty($int)) {
1270
					$gatewayip = get_interface_gateway($gateway['friendlyiface']);
1271
				}
1272

    
1273
				if (!empty($int)) {
1274
					$result['ipprotocol'] = $gateway['ipprotocol'];
1275
					if (is_ipaddr($gatewayip)) {
1276
						$groupmember = array();
1277
						$groupmember['gw'] = $member;
1278
						$groupmember['int'] = $int;
1279
						$groupmember['gwip'] = $gatewayip;
1280
						$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
1281
						if (is_array($gwvip_arr[$group['name']]) && !empty($gwvip_arr[$group['name']][$member])) {
1282
							$groupmember['vip'] = $gwvip_arr[$group['name']][$member];
1283
						}
1284
						$result[] = $groupmember;
1285
					}
1286
				}
1287
			}
1288
		}
1289
		/* we should have the 1st available tier now, exit stage left */
1290
		if (count($result) > 0) {
1291
			break;
1292
		} else {
1293
			log_error(sprintf(gettext('GATEWAYS: Group %1$s did not have any gateways up on tier %2$s!'), $group['name'], $tieridx));
1294
		}
1295
	}
1296
	// Add description field last to not influence the count() above
1297
	$result['descr'] = $group['descr'];
1298
	return $result;
1299
}
1300

    
1301
function get_gwgroup_members($groupname) {
1302
	global $config;
1303
	$gateways_status = return_gateways_status(true);
1304
	$gateways_arr = return_gateways_array();
1305
	$viplist = get_configured_vip_list();
1306
	foreach ($config['gateways']['gateway_group'] as $group) {
1307
		if ($group['name'] == $groupname) {
1308
			return get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1309
		}
1310
	}
1311
	return array();
1312
}
1313

    
1314
/*
1315
 * Return an array with all gateway groups with name as key
1316
 * All gateway groups will be processed before returning the array.
1317
 */
1318
function return_gateway_groups_array($fixup = false) {
1319
	global $config;
1320

    
1321
	/* fetch the current gateways status */
1322
	$gateways_status = return_gateways_status(true);
1323
	$gateways_arr = return_gateways_array();
1324
	$gateway_groups_array = array();
1325
	if ($fixup == true) {
1326
		$gw4 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw4']);
1327
		$gw6 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw6']);
1328
		if ($gw4 && $gw4['type'] == 'gatewaygroup') {
1329
			fixup_default_gateway("inet", $gateways_status, $gateways_arr);
1330
		}
1331
		if ($gw6 && $gw6['type'] == 'gatewaygroup') {
1332
			fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
1333
		}
1334
	}
1335
	if (is_array($config['gateways']['gateway_group'])) {
1336
		$viplist = get_configured_vip_list();
1337
		foreach ($config['gateways']['gateway_group'] as $group) {
1338
			$gateway_groups_array[$group['name']] = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
1339
		}
1340
	}
1341

    
1342
	return ($gateway_groups_array);
1343
}
1344

    
1345
/* Update DHCP WAN Interface ip address in gateway group item */
1346
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
1347
	global $config;
1348

    
1349
	if (is_array($config['gateways']['gateway_item'])) {
1350
		foreach ($config['gateways']['gateway_item'] as & $gw) {
1351
			if ($gw['interface'] != $interface) {
1352
				continue;
1353
			}
1354

    
1355
			$current_gw = get_interface_gateway($interface);
1356
			if ($gw['gateway'] <> $current_gw) {
1357
				$gw['gateway'] = $current_gw;
1358
				$changed = true;
1359
			}
1360
		}
1361
	}
1362

    
1363
	if ($changed && $current_gw) {
1364
		write_config(sprintf(gettext(
1365
		    'Updating gateway group gateway for %1$s - new gateway is %2$s'),
1366
		    $interface, $current_gw));
1367
	}
1368
}
1369

    
1370
function lookup_gateway_or_group_by_name($gwname) {
1371
	global $config;
1372

    
1373
	$gateways_arr = return_gateways_array();
1374
	foreach ($gateways_arr as $gw) {
1375
		if ($gw['name'] == $gwname) {
1376
			$gw['type'] = 'gateway';
1377
			return $gw;
1378
		}
1379
	}
1380

    
1381
	if (is_array($config['gateways']['gateway_group'])) {
1382
		foreach ($config['gateways']['gateway_group'] as $gwg) {
1383
			if ($gwg['name'] == $gwname) {
1384
				$gwg['type'] = 'gatewaygroup';
1385
				return $gwg;
1386
			}
1387
		}
1388
	}
1389

    
1390
	return false;
1391
}
1392

    
1393
function lookup_gateway_ip_by_name($name, $disabled = false) {
1394

    
1395
	$gateways_arr = return_gateways_array($disabled, true);
1396
	foreach ($gateways_arr as $gname => $gw) {
1397
		if ($gw['name'] === $name || $gname === $name) {
1398
			return $gw['gateway'];
1399
		}
1400
	}
1401

    
1402
	return false;
1403
}
1404

    
1405
function lookup_gateway_monitor_ip_by_name($name) {
1406

    
1407
	$gateways_arr = return_gateways_array(false, true);
1408
	if (!empty($gateways_arr[$name])) {
1409
		$gateway = $gateways_arr[$name];
1410
		if (!is_ipaddr($gateway['monitor'])) {
1411
			return $gateway['gateway'];
1412
		}
1413

    
1414
		return $gateway['monitor'];
1415
	}
1416

    
1417
	return (false);
1418
}
1419

    
1420
function lookup_gateway_interface_by_name($name) {
1421

    
1422
	$gateways_arr = return_gateways_array(false, true);
1423
	if (!empty($gateways_arr[$name])) {
1424
		$interfacegw = $gateways_arr[$name]['friendlyiface'];
1425
		return ($interfacegw);
1426
	}
1427

    
1428
	return (false);
1429
}
1430

    
1431
function get_interface_gateway($interface, &$dynamic = false) {
1432
	global $config, $g;
1433

    
1434
	if (substr($interface, 0, 4) == '_vip') {
1435
		$interface = get_configured_vip_interface($interface);
1436
		if (substr($interface, 0, 4) == '_vip') {
1437
			$interface = get_configured_vip_interface($interface);
1438
		}
1439
	}
1440

    
1441
	$gw = NULL;
1442
	$gwcfg = $config['interfaces'][$interface];
1443
	if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
1444
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1445
			if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
1446
				$gw = $gateway['gateway'];
1447
				break;
1448
			}
1449
		}
1450
	}
1451

    
1452
	// for dynamic interfaces we handle them through the $interface_router file.
1453
	if (($gw == NULL || !is_ipaddrv4($gw)) && !is_ipaddrv4($gwcfg['ipaddr'])) {
1454
		$realif = get_real_interface($interface);
1455
		if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
1456
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
1457
			$dynamic = true;
1458
		}
1459
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw")) {
1460
			$dynamic = "default";
1461
		}
1462

    
1463
	}
1464

    
1465
	/* return gateway */
1466
	return ($gw);
1467
}
1468

    
1469
function get_interface_gateway_v6($interface, &$dynamic = false) {
1470
	global $config, $g;
1471

    
1472
	if (substr($interface, 0, 4) == '_vip') {
1473
		$interface = get_configured_vip_interface($interface);
1474
		if (substr($interface, 0, 4) == '_vip') {
1475
			$interface = get_configured_vip_interface($interface);
1476
		}
1477
	}
1478

    
1479
	$gw = NULL;
1480
	$gwcfg = $config['interfaces'][$interface];
1481
	if (!empty($gwcfg['gatewayv6']) && is_array($config['gateways']['gateway_item'])) {
1482
		foreach ($config['gateways']['gateway_item'] as $gateway) {
1483
			if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
1484
				$gw = $gateway['gateway'];
1485
				break;
1486
			}
1487
		}
1488
	}
1489

    
1490
	// for dynamic interfaces we handle them through the $interface_router file.
1491
	if (($gw == NULL || !is_ipaddrv6($gw)) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
1492
		$realif = get_real_interface($interface);
1493
		if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
1494
			$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
1495
			$dynamic = true;
1496
		}
1497
		if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6")) {
1498
			$dynamic = "default";
1499
		}
1500
	}
1501
	/* return gateway */
1502
	return ($gw);
1503
}
1504

    
1505
/* Check a IP address against a gateway IP or name
1506
 * to verify it's address family */
1507
function validate_address_family($ipaddr, $gwname, $disabled = false) {
1508
	$v4ip = false;
1509
	$v6ip = false;
1510
	$v4gw = false;
1511
	$v6gw = false;
1512

    
1513
	if (is_ipaddrv4($ipaddr)) {
1514
		$v4ip = true;
1515
	}
1516
	if (is_ipaddrv6($ipaddr)) {
1517
		$v6ip = true;
1518
	}
1519
	if (is_ipaddrv4($gwname)) {
1520
		$v4gw = true;
1521
	}
1522
	if (is_ipaddrv6($gwname)) {
1523
		$v6gw = true;
1524
	}
1525

    
1526
	if ($v4ip && $v4gw) {
1527
		return true;
1528
	}
1529
	if ($v6ip && $v6gw) {
1530
		return true;
1531
	}
1532

    
1533
	/* still no match, carry on, lookup gateways */
1534
	if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname, $disabled))) {
1535
		$v4gw = true;
1536
	}
1537
	if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname, $disabled))) {
1538
		$v6gw = true;
1539
	}
1540

    
1541
	$gw_array = return_gateways_array();
1542
	if (is_array($gw_array[$gwname])) {
1543
		switch ($gw_array[$gwname]['ipprotocol']) {
1544
			case "inet":
1545
				$v4gw = true;
1546
				break;
1547
			case "inet6":
1548
				$v6gw = true;
1549
				break;
1550
		}
1551
	}
1552

    
1553
	if ($v4ip && $v4gw) {
1554
		return true;
1555
	}
1556
	if ($v6ip && $v6gw) {
1557
		return true;
1558
	}
1559

    
1560
	return false;
1561
}
1562

    
1563
/* check if a interface is part of a gateway group */
1564
function interface_gateway_group_member($interface, $gwgroup_name = "") {
1565
	global $config;
1566

    
1567
	if (is_array($config['gateways']['gateway_group'])) {
1568
		$groups = $config['gateways']['gateway_group'];
1569
	} else {
1570
		return false;
1571
	}
1572

    
1573
	$gateways_arr = return_gateways_array(false, true);
1574
	foreach ($groups as $group) {
1575
		if (is_array($group['item'])) {
1576
			foreach ($group['item'] as $item) {
1577
				$elements = explode("|", $item);
1578
				$gwname = $elements[0];
1579
				if ($interface == $gateways_arr[$gwname]['interface'] &&
1580
				    (empty($gwgroup_name) || $gwgroup_name == $group['name'])) {
1581
					unset($gateways_arr);
1582
					return true;
1583
				}
1584
			}
1585
		}
1586
	}
1587
	unset($gateways_arr);
1588

    
1589
	return false;
1590
}
1591

    
1592
function gateway_is_gwgroup_member($name, $detail=false) {
1593
	global $config;
1594

    
1595
	if (is_array($config['gateways']['gateway_group'])) {
1596
		$groups = $config['gateways']['gateway_group'];
1597
	} else {
1598
		return false;
1599
	}
1600

    
1601
	$members = array();
1602
	foreach ($groups as $group) {
1603
		if (is_array($group['item'])) {
1604
			foreach ($group['item'] as $item) {
1605
				list($gwname, $tier, $vipname) = explode("|", $item);
1606
				if ($name == $gwname) {
1607
					if ($detail) {
1608
						$newitem = array();
1609
						$newitem['name'] = $group['name'];
1610
						$newitem['tier'] = $tier;
1611
						$newitem['vipname'] = $vipname;
1612
						$members[] = $newitem;
1613
					} else {
1614
						$members[] = $group['name'];
1615
					}
1616
				}
1617
			}
1618
		}
1619
	}
1620

    
1621
	return $members;
1622
}
1623
/*
1624
  Check the proposed gateway settings to see if they are valid.
1625
  $gateway_settings - the proposed array of proposed gateway settings
1626
  $id - the index of the gateway proposed to be modified (otherwise "" if adding a new gateway)
1627
  $parent_ip - the IP (v4 or v6) address about to be set on the corresponding interface (if any)
1628
  $parent_sn - the subnet about to be set on the corresponding interface (if any)
1629
  (Note: the above 2 parameters allow gateway parameters to be validated concurrently with saving
1630
   an interface, before the new interface parameters are actually saved in the config.)
1631
  Return completed $input_errors array if there is any problem.
1632
  Otherwise return an empty $input_errors array
1633
*/
1634
function validate_gateway($gateway_settings, $id = "", $parent_ip = "", $parent_sn = "") {
1635
	global $config;
1636

    
1637
	$a_gateways = return_gateways_array(true, false, true, true);
1638
	$input_errors = array();
1639

    
1640
	/* input validation */
1641
	$reqdfields = explode(" ", "name interface");
1642
	$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
1643

    
1644
	do_input_validation($gateway_settings, $reqdfields, $reqdfieldsn, $input_errors);
1645

    
1646
	if (!isset($gateway_settings['name'])) {
1647
		$input_errors[] = "A valid gateway name must be specified.";
1648
	}
1649
	if (!is_validaliasname($gateway_settings['name'])) {
1650
		$input_errors[] = invalidaliasnamemsg($gateway_settings['name'], gettext("gateway"));
1651
	} else if (isset($gateway_settings['disabled'])) {
1652
		// We have a valid gateway name that the user wants to mark as disabled.
1653
		// Check if the gateway name is used in any gateway group.
1654
		if (is_array($config['gateways']['gateway_group'])) {
1655
			foreach ($config['gateways']['gateway_group'] as $group) {
1656
				foreach ($group['item'] as $item) {
1657
					$items = explode("|", $item);
1658
					if ($items[0] == $gateway_settings['name']) {
1659
						$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']);
1660
					}
1661
				}
1662
			}
1663
		}
1664

    
1665
		// Check if the gateway name is used in any enabled Static Route.
1666
		if (is_array($config['staticroutes']['route'])) {
1667
			foreach ($config['staticroutes']['route'] as $route) {
1668
				if ($route['gateway'] == $gateway_settings['name']) {
1669
					if (!isset($route['disabled'])) {
1670
						// There is a static route that uses this gateway and is enabled (not disabled).
1671
						$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']);
1672
					}
1673
				}
1674
			}
1675
		}
1676
	}
1677
	/* skip system gateways which have been automatically added */
1678
	if (($gateway_settings['gateway'] && (!is_ipaddr($gateway_settings['gateway'])) && ($gateway_settings['attribute'] !== "system")) && ($gateway_settings['gateway'] != "dynamic")) {
1679
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
1680
	}
1681

    
1682
	if ($gateway_settings['gateway'] && is_ipaddr($gateway_settings['gateway'])) {
1683
		if (is_ipaddrv4($gateway_settings['gateway'])) {
1684
			if ($parent_ip == '') {
1685
				$parent_ip = get_interface_ip($gateway_settings['interface']);
1686
				$parent_sn = get_interface_subnet($gateway_settings['interface']);
1687
			}
1688
			if (empty($parent_ip) || empty($parent_sn)) {
1689
				$input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
1690
			} elseif (!isset($gateway_settings["nonlocalgateway"])) {
1691
				$subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
1692
				$vips = link_interface_to_vips($gateway_settings['interface']);
1693
				if (is_array($vips)) {
1694
					foreach ($vips as $vip) {
1695
						if (!is_ipaddrv4($vip['subnet'])) {
1696
							continue;
1697
						}
1698
						$subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
1699
					}
1700
				}
1701

    
1702
				$found = false;
1703
				foreach ($subnets as $subnet) {
1704
					if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
1705
						$found = true;
1706
						break;
1707
					}
1708
				}
1709

    
1710
				if ($found === false) {
1711
					$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
1712
				}
1713
			}
1714
		} else if (is_ipaddrv6($gateway_settings['gateway'])) {
1715
			/* do not do a subnet match on a link local address, it's valid */
1716
			if (!is_linklocal($gateway_settings['gateway'])) {
1717
				if ($parent_ip == '') {
1718
					$parent_ip = get_interface_ipv6($gateway_settings['interface']);
1719
					$parent_sn = get_interface_subnetv6($gateway_settings['interface']);
1720
				}
1721
				if (empty($parent_ip) || empty($parent_sn)) {
1722
					$input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
1723
				} elseif (!isset($gateway_settings["nonlocalgateway"])) {
1724
					$subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
1725
					$vips = link_interface_to_vips($gateway_settings['interface']);
1726
					if (is_array($vips)) {
1727
						foreach ($vips as $vip) {
1728
							if (!is_ipaddrv6($vip['subnet'])) {
1729
								continue;
1730
							}
1731
							$subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
1732
						}
1733
					}
1734

    
1735
					$found = false;
1736
					foreach ($subnets as $subnet) {
1737
						if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
1738
							$found = true;
1739
							break;
1740
						}
1741
					}
1742

    
1743
					if ($found === false) {
1744
						$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
1745
					}
1746
				}
1747
			}
1748
		}
1749

    
1750
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddr'])) {
1751
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddr']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
1752
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
1753
			}
1754
		}
1755
		if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddrv6'])) {
1756
			if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddrv6']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
1757
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
1758
			}
1759
		}
1760
	}
1761
	if (($gateway_settings['monitor'] != "") && ($gateway_settings['monitor'] != "dynamic")) {
1762
		validateipaddr($gateway_settings['monitor'], IPV4V6, "Monitor IP", $input_errors, false);
1763
	}
1764
	if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] < 0) {
1765
		$input_errors[] = gettext("A valid data payload must be specified.");
1766
	}
1767
	/* only allow correct IPv4 and IPv6 gateway addresses */
1768
	if (($gateway_settings['gateway'] <> "") && is_ipaddr($gateway_settings['gateway']) && $gateway_settings['gateway'] != "dynamic") {
1769
		if (is_ipaddrv6($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet")) {
1770
			$input_errors[] = sprintf(gettext("The IPv6 gateway address '%s' can not be used as a IPv4 gateway."), $gateway_settings['gateway']);
1771
		}
1772
		if (is_ipaddrv4($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet6")) {
1773
			$input_errors[] = sprintf(gettext("The IPv4 gateway address '%s' can not be used as a IPv6 gateway."), $gateway_settings['gateway']);
1774
		}
1775
	}
1776
	/* only allow correct IPv4 and IPv6 monitor addresses */
1777
	if (($gateway_settings['monitor'] <> "") && is_ipaddr($gateway_settings['monitor']) && $gateway_settings['monitor'] != "dynamic") {
1778
		if (is_ipaddrv6($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet")) {
1779
			$input_errors[] = sprintf(gettext("The IPv6 monitor address '%s' can not be used on a IPv4 gateway."), $gateway_settings['monitor']);
1780
		}
1781
		if (is_ipaddrv4($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet6")) {
1782
			$input_errors[] = sprintf(gettext("The IPv4 monitor address '%s' can not be used on a IPv6 gateway."), $gateway_settings['monitor']);
1783
		}
1784
	}
1785

    
1786
	if (isset($gateway_settings['name'])) {
1787
		/* check for overlaps */
1788
		foreach ($a_gateways as $gateway) {
1789
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
1790
				if ($gateway['name'] != $gateway_settings['name']) {
1791
					$input_errors[] = gettext("Changing name on a gateway is not allowed.");
1792
				}
1793
				continue;
1794
			}
1795
			if ($gateway_settings['name'] <> "") {
1796
				if (($gateway['name'] <> "") && ($gateway_settings['name'] == $gateway['name']) && ($gateway['attribute'] !== "system")) {
1797
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $gateway_settings['name']);
1798
					break;
1799
				}
1800
			}
1801
			if (is_ipaddr($gateway_settings['gateway'])) {
1802
				if (($gateway['gateway'] <> "") && ($gateway_settings['gateway'] == $gateway['gateway']) && ($gateway['attribute'] !== "system")) {
1803
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $gateway_settings['gateway']);
1804
					break;
1805
				}
1806
			}
1807
			if (is_ipaddr($gateway_settings['monitor'])) {
1808
				if (($gateway['monitor'] <> "") && ($gateway_settings['monitor'] == $gateway['monitor']) && ($gateway['attribute'] !== "system")) {
1809
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. A different monitor IP must be chosen.'), $gateway_settings['monitor']);
1810
					break;
1811
				}
1812
			}
1813
		}
1814
	}
1815

    
1816
	/* input validation of dpinger advanced parameters */
1817

    
1818
	$dpinger_default = return_dpinger_defaults();
1819
	$latencylow = $dpinger_default['latencylow'];
1820
	if ($gateway_settings['latencylow']) {
1821
		if (!is_numeric($gateway_settings['latencylow'])) {
1822
			$input_errors[] = gettext("The low latency threshold needs to be a numeric value.");
1823
		} else if ($gateway_settings['latencylow'] < 1) {
1824
			$input_errors[] = gettext("The low latency threshold needs to be positive.");
1825
		} else {
1826
			$latencylow = $gateway_settings['latencylow'];
1827
		}
1828
	}
1829

    
1830
	$latencyhigh = $dpinger_default['latencyhigh'];
1831
	if ($gateway_settings['latencyhigh']) {
1832
		if (!is_numeric($gateway_settings['latencyhigh'])) {
1833
			$input_errors[] = gettext("The high latency threshold needs to be a numeric value.");
1834
		} else if ($gateway_settings['latencyhigh'] < 1) {
1835
			$input_errors[] = gettext("The high latency threshold needs to be positive.");
1836
		} else {
1837
			$latencyhigh = $gateway_settings['latencyhigh'];
1838
		}
1839
	}
1840

    
1841
	$losslow = $dpinger_default['losslow'];
1842
	if ($gateway_settings['losslow']) {
1843
		if (!is_numeric($gateway_settings['losslow'])) {
1844
			$input_errors[] = gettext("The low Packet Loss threshold needs to be a numeric value.");
1845
		} else if ($gateway_settings['losslow'] < 1) {
1846
			$input_errors[] = gettext("The low Packet Loss threshold needs to be positive.");
1847
		} else if ($gateway_settings['losslow'] >= 100) {
1848
			$input_errors[] = gettext("The low Packet Loss threshold needs to be less than 100.");
1849
		} else {
1850
			$losslow = $gateway_settings['losslow'];
1851
		}
1852
	}
1853

    
1854
	$losshigh = $dpinger_default['losshigh'];
1855
	if ($gateway_settings['losshigh']) {
1856
		if (!is_numeric($gateway_settings['losshigh'])) {
1857
			$input_errors[] = gettext("The high Packet Loss threshold needs to be a numeric value.");
1858
		} else if ($gateway_settings['losshigh'] < 1) {
1859
			$input_errors[] = gettext("The high Packet Loss threshold needs to be positive.");
1860
		} else if ($gateway_settings['losshigh'] > 100) {
1861
			$input_errors[] = gettext("The high Packet Loss threshold needs to be 100 or less.");
1862
		} else {
1863
			$losshigh = $gateway_settings['losshigh'];
1864
		}
1865
	}
1866

    
1867
	$time_period = $dpinger_default['time_period'];
1868
	if ($gateway_settings['time_period']) {
1869
		if (!is_numeric($gateway_settings['time_period'])) {
1870
			$input_errors[] = gettext("The time period over which results are averaged needs to be a numeric value.");
1871
		} else if ($gateway_settings['time_period'] < 1) {
1872
			$input_errors[] = gettext("The time period over which results are averaged needs to be positive.");
1873
		} else {
1874
			$time_period = $gateway_settings['time_period'];
1875
		}
1876
	}
1877

    
1878
	$interval = $dpinger_default['interval'];
1879
	if ($gateway_settings['interval']) {
1880
		if (!is_numeric($gateway_settings['interval'])) {
1881
			$input_errors[] = gettext("The probe interval needs to be a numeric value.");
1882
		} else if ($gateway_settings['interval'] < 1) {
1883
			$input_errors[] = gettext("The probe interval needs to be positive.");
1884
		} else {
1885
			$interval = $gateway_settings['interval'];
1886
		}
1887
	}
1888

    
1889
	$loss_interval = $dpinger_default['loss_interval'];
1890
	if ($gateway_settings['loss_interval']) {
1891
		if (!is_numeric($gateway_settings['loss_interval'])) {
1892
			$input_errors[] = gettext("The loss interval needs to be a numeric value.");
1893
		} else if ($gateway_settings['loss_interval'] < 1) {
1894
			$input_errors[] = gettext("The loss interval setting needs to be positive.");
1895
		} else {
1896
			$loss_interval = $gateway_settings['loss_interval'];
1897
		}
1898
	}
1899

    
1900
	$alert_interval = $dpinger_default['alert_interval'];
1901
	if ($gateway_settings['alert_interval']) {
1902
		if (!is_numeric($gateway_settings['alert_interval'])) {
1903
			$input_errors[] = gettext("The alert interval needs to be a numeric value.");
1904
		} else if ($gateway_settings['alert_interval'] < 1) {
1905
			$input_errors[] = gettext("The alert interval setting needs to be positive.");
1906
		} else {
1907
			$alert_interval = $gateway_settings['alert_interval'];
1908
		}
1909
	}
1910

    
1911
	if ($latencylow >= $latencyhigh) {
1912
		$input_errors[] = gettext(
1913
		    "The high latency threshold needs to be greater than the low latency threshold");
1914
	}
1915

    
1916
	if ($losslow >= $losshigh) {
1917
		$input_errors[] = gettext(
1918
		    "The high packet loss threshold needs to be higher than the low packet loss threshold");
1919
	}
1920

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

    
1927
	// Ensure that the time period is greater than 2 times the probe interval plus the loss interval.
1928
	if (($interval * 2 + $loss_interval) >= $time_period) {
1929
		$input_errors[] = gettext("The time period needs to be greater than twice the probe interval plus the loss interval.");
1930
	}
1931

    
1932
	// There is no point recalculating the average latency and loss more often than the probe interval.
1933
	// So the alert interval needs to be >= probe interval.
1934
	if ($interval > $alert_interval) {
1935
		$input_errors[] = gettext("The alert interval needs to be greater than or equal to the probe interval.");
1936
	}
1937

    
1938
	return $input_errors;
1939
}
1940

    
1941
// Save gateway settings.
1942
// $gateway_settings - the array of gateway setting parameters
1943
// $realid - the index of the gateway to be modified (otherwise "" if adding a new gateway)
1944

    
1945
// This function is responsible to:
1946
//   Setup the gateway parameter structure from the gateway settings input parameter
1947
//   Save the structure into the config
1948
//   Remove any run-time settings from gateway parameters that are changed (e.g. remove routes to addresses that are changing)
1949

    
1950
// A subsequent "apply" step will implement the added/changed gateway.
1951

    
1952
function save_gateway($gateway_settings, $realid = "") {
1953
	global $config;
1954

    
1955
	$a_gateway_item = &$config['gateways']['gateway_item'];
1956
	$reloadif = "";
1957
	$gateway = array();
1958

    
1959
	if (empty($gateway_settings['interface'])) {
1960
		$gateway['interface'] = $gateway_settings['friendlyiface'];
1961
	} else {
1962
		$gateway['interface'] = $gateway_settings['interface'];
1963
	}
1964
	if (is_ipaddr($gateway_settings['gateway'])) {
1965
		$gateway['gateway'] = $gateway_settings['gateway'];
1966
	} else {
1967
		$gateway['gateway'] = "dynamic";
1968
	}
1969
	$gateway['name'] = $gateway_settings['name'];
1970
	$gateway['weight'] = $gateway_settings['weight'];
1971
	$gateway['ipprotocol'] = $gateway_settings['ipprotocol'];
1972
	if ($gateway_settings['interval']) {
1973
		$gateway['interval'] = $gateway_settings['interval'];
1974
	}
1975

    
1976
	if ($gateway_settings['time_period']) {
1977
		$gateway['time_period'] = $gateway_settings['time_period'];
1978
	}
1979
	if ($gateway_settings['alert_interval']) {
1980
		$gateway['alert_interval'] = $gateway_settings['alert_interval'];
1981
	}
1982

    
1983
	$gateway['descr'] = $gateway_settings['descr'];
1984
	if ($gateway_settings['monitor_disable'] == "yes") {
1985
		$gateway['monitor_disable'] = true;
1986
	}
1987
	if ($gateway_settings['action_disable'] == "yes") {
1988
		$gateway['action_disable'] = true;
1989
	}
1990
	if ($gateway_settings['nonlocalgateway'] == "yes") {
1991
		$gateway['nonlocalgateway'] = true;
1992
	}
1993
	if ($gateway_settings['force_down'] == "yes") {
1994
		$gateway['force_down'] = true;
1995
	}
1996
	if (is_ipaddr($gateway_settings['monitor'])) {
1997
		$gateway['monitor'] = $gateway_settings['monitor'];
1998
	}
1999
	if (isset($gateway_settings['data_payload']) && $gateway_settings['data_payload'] > 0) {
2000
		$gateway['data_payload'] = $gateway_settings['data_payload'];
2001
	}
2002

    
2003
	/* NOTE: If gateway ip is changed need to cleanup the old static interface route */
2004
	if ($gateway_settings['monitor'] != "dynamic" && !empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['gateway']) &&
2005
		$gateway['gateway'] != $a_gateway_item[$realid]['gateway'] &&
2006
		isset($a_gateway_item[$realid]["nonlocalgateway"])) {
2007
		$realif = get_real_interface($a_gateway_item[$realid]['interface']);
2008
		$inet = (!is_ipaddrv4($a_gateway_item[$realid]['gateway']) ? "-inet6" : "-inet");
2009
		$cmd = "/sbin/route delete $inet " . escapeshellarg($a_gateway_item[$realid]['gateway']) . " -iface " . escapeshellarg($realif);
2010
		mwexec($cmd);
2011
	}
2012

    
2013
	/* NOTE: If monitor ip is changed need to cleanup the old static route */
2014
	if ($gateway_settings['monitor'] != "dynamic" && !empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['monitor']) &&
2015
		$gateway_settings['monitor'] != $a_gateway_item[$realid]['monitor'] && $gateway['gateway'] != $a_gateway_item[$realid]['monitor']) {
2016
		if (is_ipaddrv4($a_gateway_item[$realid]['monitor'])) {
2017
			mwexec("/sbin/route delete " . escapeshellarg($a_gateway_item[$realid]['monitor']));
2018
		} else {
2019
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateway_item[$realid]['monitor']));
2020
		}
2021
	}
2022

    
2023
	if ($gateway_settings['defaultgw'] == "yes" || $gateway_settings['defaultgw'] == "on") {
2024
		// a new default gateway is being saved.
2025
		$i = 0;
2026
		/* remove the default gateway bits for all gateways with the same address family */
2027
		if (is_array($a_gateway_item)) {
2028
			foreach ($a_gateway_item as $gw) {
2029
				if ($gateway['ipprotocol'] == $gw['ipprotocol']) {
2030
					if ($gw['interface'] != $gateway_settings['interface'] &&
2031
						($gw['name'] == $config['gateways']['defaultgw4'] || $gw['name'] == $config['gateways']['defaultgw6'])) {
2032
						// remember the old default gateway interface to call with a "interface reconfigure" event.
2033
						$reloadif = $gw['interface'];
2034
					}
2035
				}
2036
				$i++;
2037
			}
2038
		}
2039
		if ($gateway['ipprotocol'] == "inet") {
2040
			$config['gateways']['defaultgw4'] = $gateway['name'];
2041
		} elseif ($gateway['ipprotocol'] == "inet6") {
2042
			$config['gateways']['defaultgw6'] = $gateway['name'];
2043
		}
2044
	}
2045

    
2046
	if ($gateway_settings['latencylow']) {
2047
		$gateway['latencylow'] = $gateway_settings['latencylow'];
2048
	}
2049
	if ($gateway_settings['latencyhigh']) {
2050
		$gateway['latencyhigh'] = $gateway_settings['latencyhigh'];
2051
	}
2052
	if ($gateway_settings['losslow']) {
2053
		$gateway['losslow'] = $gateway_settings['losslow'];
2054
	}
2055
	if ($gateway_settings['losshigh']) {
2056
		$gateway['losshigh'] = $gateway_settings['losshigh'];
2057
	}
2058
	if ($gateway_settings['loss_interval']) {
2059
		$gateway['loss_interval'] = $gateway_settings['loss_interval'];
2060
	}
2061
	/* when saving the manual gateway we use the attribute which has the corresponding id */
2062
	if (isset($realid) && $a_gateway_item[$realid]) {
2063
		$preserve_disabled = isset($a_gateway_item[$realid]['disabled']);
2064
		$a_gateway_item[$realid] = $gateway;
2065
		if ($preserve_disabled) {
2066
			$a_gateway_item[$realid]['disabled'] = true;
2067
		}
2068
	} else {
2069
		$a_gateway_item[] = $gateway;
2070
	}
2071
	gateway_set_enabled($gateway_settings['name'], !isset($gateway_settings['disabled']));
2072

    
2073
	mark_subsystem_dirty('staticroutes');
2074

    
2075
	write_config();
2076

    
2077
	if (!empty($reloadif)) {
2078
		send_event("interface reconfigure {$reloadif}");
2079
	}
2080
}
2081

    
2082
function gateway_set_enabled($name, $enabled) {
2083
	global $config;
2084
	if (is_array($config['gateways']['gateway_item'])) {
2085
		foreach($config['gateways']['gateway_item'] as &$item) {
2086
			if ($item['name'] == $name) {
2087
				$gateway = &$item;
2088
			}
2089
		}
2090
	}
2091
	if (!isset($gateway)) {
2092
		return;
2093
	}
2094
	if ($enabled) {
2095
		unset($gateway['disabled']);
2096
	} else {
2097
		/* Check if the gateway was enabled but changed to disabled. */
2098
		if (!isset($gateway['disabled'])) {
2099
			/*  If the disabled gateway was the default route, remove the default route */
2100
			if (is_ipaddr($gateway['gateway'])) {
2101
				$inet = (!is_ipaddrv4($gateway['gateway']) ? 'inet6' : 'inet');
2102
				if ($inet == 'inet') {
2103
					$cgw = getcurrentdefaultgatewayip('inet');
2104
				} else {
2105
					$cgw = getcurrentdefaultgatewayip('inet6');
2106
				}
2107
				if ($gateway['gateway'] == $cgw) {
2108
					mwexec("/sbin/route delete -{$inet} default");
2109
				}
2110
			}
2111
			$gateway['disabled'] = true;
2112
		}
2113
	}
2114
}
2115

    
2116
function gateway_or_gwgroup_exists($name) {
2117
	global $config;
2118
	if (is_array($config['gateways']['gateway_item'])) {
2119
		foreach($config['gateways']['gateway_item'] as $item) {
2120
			if ($item['name'] == $name) {
2121
				return true;
2122
			}
2123
		}
2124
	}
2125
	if (is_array($config['gateways']['gateway_group'])) {
2126
		foreach($config['gateways']['gateway_group'] as $item) {
2127
			if ($item['name'] == $name) {
2128
				return true;
2129
			}
2130
		}
2131
	}
2132
	return false;
2133
}
2134

    
2135
?>
(22-22/60)