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 (!isset($config['system']['dpinger_dont_add_static_routes']) &&
|
243
|
!isset($gateway['dpinger_dont_add_static_route'])) {
|
244
|
if (is_ipaddrv4($gateway['gateway']) && $gateway['monitor'] != $gateway['gateway']) {
|
245
|
log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
|
246
|
if (interface_isppp_type($gateway['friendlyiface'])) {
|
247
|
route_add_or_change($gateway['monitor'],
|
248
|
'', $gateway['interface']);
|
249
|
system_staticroutes_configure($gateway['friendlyiface']);
|
250
|
} else {
|
251
|
route_add_or_change($gateway['monitor'],
|
252
|
$gateway['gateway']);
|
253
|
}
|
254
|
|
255
|
pfSense_kill_states("0.0.0.0/0", utf8_encode($gateway['monitor']), utf8_encode($gateway['interface']), "icmp");
|
256
|
}
|
257
|
}
|
258
|
} else if ($gateway['ipprotocol'] == "inet6") { // This is an IPv6 gateway...
|
259
|
if (is_linklocal($gateway['gateway']) &&
|
260
|
get_ll_scope($gateway['gateway']) == '') {
|
261
|
$gateway['gateway'] .= '%' . $gateway['interface'];
|
262
|
}
|
263
|
|
264
|
if (is_linklocal($gateway['monitor'])) {
|
265
|
if (get_ll_scope($gateway['monitor']) == '') {
|
266
|
$gateways_arr[$gwname]['monitor'] .= '%' . $gateway['interface'];
|
267
|
}
|
268
|
|
269
|
$gwifip = find_interface_ipv6_ll($gateway['interface'], true);
|
270
|
|
271
|
if (get_ll_scope($gwifip) == '') {
|
272
|
$gwifip .= '%' . $gateway['interface'];
|
273
|
}
|
274
|
} else {
|
275
|
$gwifip = find_interface_ipv6($gateway['interface'], true);
|
276
|
}
|
277
|
|
278
|
if (!is_ipaddrv6($gwifip)) {
|
279
|
continue; //Skip this target
|
280
|
}
|
281
|
|
282
|
/*
|
283
|
* If the gateway is the same as the monitor we do not add a
|
284
|
* route as this will break the routing table.
|
285
|
* Add static routes for each gateway with their monitor IP
|
286
|
* not strictly necessary but is a added level of protection.
|
287
|
*/
|
288
|
|
289
|
if (!isset($config['system']['dpinger_dont_add_static_routes']) &&
|
290
|
!isset($gateway['dpinger_dont_add_static_route'])) {
|
291
|
if ($gateway['gateway'] != $gateways_arr[$gwname]['monitor']) {
|
292
|
log_error(sprintf(gettext('Removing static route for monitor %1$s and adding a new route through %2$s'), $gateway['monitor'], $gateway['gateway']));
|
293
|
if (interface_isppp_type($gateway['friendlyiface'])) {
|
294
|
route_add_or_change($gateway['monitor'],
|
295
|
'', $gateway['interface']);
|
296
|
system_staticroutes_configure($gateway['friendlyiface']);
|
297
|
} else {
|
298
|
route_add_or_change($gateway['monitor'],
|
299
|
$gateway['gateway']);
|
300
|
}
|
301
|
|
302
|
pfSense_kill_states("::0.0.0.0/0", utf8_encode($gateway['monitor']), utf8_encode($gateway['interface']), "icmpv6");
|
303
|
}
|
304
|
}
|
305
|
} else {
|
306
|
continue;
|
307
|
}
|
308
|
|
309
|
$monitor_ips[] = $gateway['monitor'];
|
310
|
$gateways_arr[$gwname]['enable_dpinger'] = true;
|
311
|
$gateways_arr[$gwname]['gwifip'] = $gwifip;
|
312
|
}
|
313
|
|
314
|
stop_dpinger();
|
315
|
|
316
|
/* Start new processes */
|
317
|
foreach ($gateways_arr as $gateway) {
|
318
|
if (!isset($gateway['enable_dpinger'])) {
|
319
|
continue;
|
320
|
}
|
321
|
|
322
|
if (start_dpinger($gateway) != 0) {
|
323
|
log_error(sprintf(gettext("Error starting gateway monitor for %s"), $gateway['name']));
|
324
|
}
|
325
|
}
|
326
|
if (platform_booting()) {
|
327
|
echo "done.\n";
|
328
|
}
|
329
|
|
330
|
return;
|
331
|
}
|
332
|
|
333
|
function get_dpinger_status($gwname, $gways = false, $action_disable = false) {
|
334
|
global $g;
|
335
|
|
336
|
$running_processes = running_dpinger_processes();
|
337
|
|
338
|
if (!isset($running_processes[$gwname])) {
|
339
|
log_error(sprintf(gettext(
|
340
|
'dpinger: No dpinger session running for gateway %s'),
|
341
|
$gwname));
|
342
|
return false;
|
343
|
}
|
344
|
|
345
|
$proc = $running_processes[$gwname];
|
346
|
unset($running_processes);
|
347
|
|
348
|
$timeoutcounter = 0;
|
349
|
while (true) {
|
350
|
if (!file_exists($proc['socket'])) {
|
351
|
log_error("dpinger: status socket {$proc['socket']} not found");
|
352
|
return false;
|
353
|
}
|
354
|
$fp = @stream_socket_client("unix://{$proc['socket']}", $errno, $errstr, 10);
|
355
|
if (!$fp) {
|
356
|
log_error(sprintf(gettext('dpinger: cannot connect to status socket %1$s - %2$s (%3$s)'), $proc['socket'], $errstr, $errno));
|
357
|
return false;
|
358
|
}
|
359
|
|
360
|
$status = '';
|
361
|
while (!feof($fp)) {
|
362
|
$status .= fgets($fp, 1024);
|
363
|
}
|
364
|
fclose($fp);
|
365
|
|
366
|
$r = array();
|
367
|
list(
|
368
|
$r['gwname'],
|
369
|
$r['latency_avg'],
|
370
|
$r['latency_stddev'],
|
371
|
$r['loss']
|
372
|
) = explode(' ', preg_replace('/\n/', '', $status));
|
373
|
|
374
|
// dpinger returns '<gwname> 0 0 0' when queried directly after it starts.
|
375
|
// while a latency of 0 and a loss of 0 would be perfect, in a real world it doesnt happen.
|
376
|
// or does it, anyone? if so we must 'detect' the initialization period differently..
|
377
|
$ready = $r['latency_stddev'] != '0' || $r['loss'] != '0';
|
378
|
|
379
|
if ($ready) {
|
380
|
break;
|
381
|
} else {
|
382
|
$timeoutcounter++;
|
383
|
if ($timeoutcounter > 300) {
|
384
|
log_error(sprintf(gettext('dpinger: timeout while retrieving status for gateway %s'), $gwname));
|
385
|
return false;
|
386
|
}
|
387
|
usleep(10000);
|
388
|
}
|
389
|
}
|
390
|
|
391
|
$r['srcip'] = $proc['srcip'];
|
392
|
$r['targetip'] = $proc['targetip'];
|
393
|
|
394
|
if (is_array($gways)) {
|
395
|
$gateways_arr = $gways;
|
396
|
} else {
|
397
|
$gateways_arr = return_gateways_array();
|
398
|
}
|
399
|
|
400
|
unset($gw);
|
401
|
if (isset($gateways_arr[$gwname])) {
|
402
|
$gw = $gateways_arr[$gwname];
|
403
|
}
|
404
|
|
405
|
$r['latency_avg'] = round($r['latency_avg']/1000, 3);
|
406
|
$r['latency_stddev'] = round($r['latency_stddev']/1000, 3);
|
407
|
|
408
|
$r['status'] = "online";
|
409
|
$r['substatus'] = "none";
|
410
|
if (isset($gw) && isset($gw['force_down'])) {
|
411
|
$r['status'] = "down";
|
412
|
$r['substatus'] = "force_down";
|
413
|
} else if (isset($gw)) {
|
414
|
$settings = return_dpinger_defaults();
|
415
|
|
416
|
$keys = array(
|
417
|
'latencylow',
|
418
|
'latencyhigh',
|
419
|
'losslow',
|
420
|
'losshigh'
|
421
|
);
|
422
|
|
423
|
/* Replace default values by user-defined */
|
424
|
foreach ($keys as $key) {
|
425
|
if (isset($gw[$key]) && is_numeric($gw[$key])) {
|
426
|
$settings[$key] = $gw[$key];
|
427
|
}
|
428
|
}
|
429
|
|
430
|
if ($r['latency_avg'] > $settings['latencyhigh']) {
|
431
|
if (!$action_disable) {
|
432
|
$r['status'] = "down";
|
433
|
}
|
434
|
$r['substatus'] = "highdelay";
|
435
|
} else if ($r['loss'] > $settings['losshigh']) {
|
436
|
if (!$action_disable) {
|
437
|
$r['status'] = "down";
|
438
|
}
|
439
|
$r['substatus'] = "highloss";
|
440
|
} else if ($r['latency_avg'] > $settings['latencylow']) {
|
441
|
$r['substatus'] = "delay";
|
442
|
} else if ($r['loss'] > $settings['losslow']) {
|
443
|
$r['substatus'] = "loss";
|
444
|
}
|
445
|
}
|
446
|
|
447
|
return $r;
|
448
|
}
|
449
|
|
450
|
/* return the status of the dpinger targets as an array */
|
451
|
function return_gateways_status($byname = false, $gways = false) {
|
452
|
global $config, $g;
|
453
|
|
454
|
$dpinger_gws = running_dpinger_processes();
|
455
|
$status = array();
|
456
|
|
457
|
if (is_array($gways)) {
|
458
|
$gateways_arr = $gways;
|
459
|
} else {
|
460
|
$gateways_arr = return_gateways_array();
|
461
|
}
|
462
|
|
463
|
foreach ($dpinger_gws as $gwname => $gwdata) {
|
464
|
/*
|
465
|
* If action is disabled for this gateway, then we want a
|
466
|
* detailed status. That reports "highdelay" or "highloss"
|
467
|
* rather than just "down". Because reporting the gateway
|
468
|
* down would be misleading (gateway action is disabled)
|
469
|
*/
|
470
|
$action_disable = $gateways_arr[$gwname]['action_disable'];
|
471
|
$dpinger_status = get_dpinger_status($gwname, $action_disable);
|
472
|
if ($dpinger_status === false) {
|
473
|
continue;
|
474
|
}
|
475
|
|
476
|
if ($byname == false) {
|
477
|
$target = $dpinger_status['targetip'];
|
478
|
} else {
|
479
|
$target = $gwname;
|
480
|
}
|
481
|
|
482
|
$status[$target] = array();
|
483
|
$status[$target]['monitorip'] = $dpinger_status['targetip'];
|
484
|
$status[$target]['srcip'] = $dpinger_status['srcip'];
|
485
|
$status[$target]['name'] = $gwname;
|
486
|
$status[$target]['delay'] =
|
487
|
empty($dpinger_status['latency_avg'])
|
488
|
? "0ms"
|
489
|
: $dpinger_status['latency_avg'] . "ms";
|
490
|
$status[$target]['stddev'] =
|
491
|
empty($dpinger_status['latency_stddev'])
|
492
|
? "0ms"
|
493
|
: $dpinger_status['latency_stddev'] . "ms";
|
494
|
$status[$target]['loss'] = empty($dpinger_status['loss'])
|
495
|
? "0.0%"
|
496
|
: round($dpinger_status['loss'], 1) . "%";
|
497
|
$status[$target]['status'] = $dpinger_status['status'];
|
498
|
$status[$target]['substatus'] = $dpinger_status['substatus'];
|
499
|
}
|
500
|
|
501
|
/* tack on any gateways that have monitoring disabled
|
502
|
* or are down, which could cause gateway groups to fail */
|
503
|
$gateways_arr = return_gateways_array();
|
504
|
foreach ($gateways_arr as $gwitem) {
|
505
|
if (!isset($gwitem['monitor_disable'])) {
|
506
|
continue;
|
507
|
}
|
508
|
if (!is_ipaddr($gwitem['monitor'])) {
|
509
|
$realif = $gwitem['interface'];
|
510
|
$tgtip = get_interface_gateway($realif);
|
511
|
if (!is_ipaddr($tgtip)) {
|
512
|
$tgtip = "none";
|
513
|
}
|
514
|
$srcip = find_interface_ip($realif);
|
515
|
} else {
|
516
|
$tgtip = $gwitem['monitor'];
|
517
|
$srcip = find_interface_ip($realif);
|
518
|
}
|
519
|
if ($byname == true) {
|
520
|
$target = $gwitem['name'];
|
521
|
} else {
|
522
|
$target = $tgtip;
|
523
|
}
|
524
|
|
525
|
/* failsafe for down interfaces */
|
526
|
if ($target == "none") {
|
527
|
$target = $gwitem['name'];
|
528
|
$status[$target]['name'] = $gwitem['name'];
|
529
|
$status[$target]['delay'] = "0.0ms";
|
530
|
$status[$target]['loss'] = "100.0%";
|
531
|
$status[$target]['status'] = "down";
|
532
|
$status[$target]['substatus'] = "down";
|
533
|
} else {
|
534
|
$status[$target]['monitorip'] = $tgtip;
|
535
|
$status[$target]['srcip'] = $srcip;
|
536
|
$status[$target]['name'] = $gwitem['name'];
|
537
|
$status[$target]['delay'] = "";
|
538
|
$status[$target]['loss'] = "";
|
539
|
$status[$target]['status'] = "online";
|
540
|
$status[$target]['substatus'] = "none";
|
541
|
}
|
542
|
|
543
|
$status[$target]['monitor_disable'] = true;
|
544
|
}
|
545
|
return($status);
|
546
|
}
|
547
|
|
548
|
function return_gateways_status_text($byname = false, $brief = false) {
|
549
|
$gwstat = return_gateways_status($byname);
|
550
|
$output = "";
|
551
|
$widths = array();
|
552
|
$col_sep = 2;
|
553
|
if ($brief) {
|
554
|
$collist = array('status' => "Status");
|
555
|
} else {
|
556
|
$collist = array('monitorip' => "Monitor",
|
557
|
'srcip' => "Source",
|
558
|
'delay' => "Delay",
|
559
|
'stddev' => "StdDev",
|
560
|
'loss' => "Loss",
|
561
|
'status' => "Status",
|
562
|
'substatus' => "Substatus");
|
563
|
}
|
564
|
foreach ($gwstat as $gw) {
|
565
|
foreach ($gw as $gwdidx => $gwdata) {
|
566
|
if (strlen($gwdata) > $widths[$gwdidx]) {
|
567
|
$widths[$gwdidx] = strlen($gwdata);
|
568
|
}
|
569
|
}
|
570
|
}
|
571
|
|
572
|
$output .= str_pad("Name", $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
|
573
|
foreach ($collist as $hdrcol => $hdrdesc) {
|
574
|
if (strlen($hdrdesc) > $widths[$hdrcol]) {
|
575
|
$widths[$hdrcol] = strlen($hdrdesc);
|
576
|
}
|
577
|
$output .= str_pad($hdrdesc, $widths[$hdrcol] + $col_sep, " ", (substr($hdrcol, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
|
578
|
}
|
579
|
$output .= "\n";
|
580
|
|
581
|
foreach ($gwstat as $idx => $gw) {
|
582
|
$output .= str_pad($gw['name'], $widths['name'] + $col_sep, " ", STR_PAD_RIGHT);
|
583
|
foreach (array_keys($collist) as $col) {
|
584
|
$output .= str_pad($gw[$col], $widths[$col] + $col_sep, " ", (substr($col, -2, 2) == "ip") ? STR_PAD_RIGHT : STR_PAD_LEFT);
|
585
|
}
|
586
|
$output .= "\n";
|
587
|
}
|
588
|
|
589
|
return $output;
|
590
|
}
|
591
|
|
592
|
function compare_gateway_order_configured($a, $b) {
|
593
|
/* XXX WAN always has precedence */
|
594
|
if ($a['friendlyiface'] == "wan") {
|
595
|
return -1;
|
596
|
} elseif ($b['friendlyiface'] == "wan") {
|
597
|
return 1;
|
598
|
}
|
599
|
|
600
|
if ($a['attribute'] === $b['attribute']) {
|
601
|
if ($a['attribute'] === 'system') {
|
602
|
$res = (($a['name'] < $b['name'])) ? -1 : 1;
|
603
|
return $res;
|
604
|
}
|
605
|
return 0;
|
606
|
}
|
607
|
if ($a['attribute'] === 'system' || $b['attribute'] === 'system') {
|
608
|
$res = (($b['attribute'] === 'system')) ? -1 : 1;
|
609
|
return $res;
|
610
|
}
|
611
|
$res = ($a['attribute'] < $b['attribute']) ? -1 : 1;
|
612
|
return $res;
|
613
|
}
|
614
|
|
615
|
function order_gateways_as_configured($gateways_arr) {
|
616
|
uasort($gateways_arr, 'compare_gateway_order_configured');
|
617
|
return $gateways_arr;
|
618
|
}
|
619
|
|
620
|
/* Return all configured gateways on the system
|
621
|
$disabled = true - include gateways that are disabled
|
622
|
$localhost = true - include "Null" entries for localhost IP addresses
|
623
|
$inactive = true - include gateways on inactive interfaces
|
624
|
$integer_index = true - index the returned array by integers 0,1,2,... instead of by GW name
|
625
|
*/
|
626
|
function return_gateways_array($disabled = false, $localhost = false, $inactive = false, $integer_index = false) {
|
627
|
global $config, $g;
|
628
|
|
629
|
$gateways_arr = array();
|
630
|
$gateways_arr_temp = array();
|
631
|
$cgw4 = route_get_default('inet');
|
632
|
$cgw6 = route_get_default('inet6');
|
633
|
$found_defaultv4 = 0;
|
634
|
$found_defaultv6 = 0;
|
635
|
|
636
|
// Ensure the interface cache is up to date first
|
637
|
$interfaces = get_interface_arr(true);
|
638
|
|
639
|
$i = -1;
|
640
|
/* Process/add all the configured gateways. */
|
641
|
if (is_array($config['gateways']) && is_array($config['gateways']['gateway_item'])) {
|
642
|
foreach ($config['gateways']['gateway_item'] as $gateway) {
|
643
|
if (!is_array($gateway) || empty($gateway)) {
|
644
|
continue;
|
645
|
}
|
646
|
|
647
|
/* Increment it here to do not skip items */
|
648
|
$i++;
|
649
|
if (isset($gateway['defaultgw'])) {
|
650
|
unset($gateway['defaultgw']);
|
651
|
}
|
652
|
|
653
|
$ifinfo = get_interface_info($gateway['interface']);
|
654
|
if (empty($config['interfaces'][$gateway['interface']]) ||
|
655
|
($ifinfo['status'] == 'down') || ($ifinfo['status'] == 'no carrier')) {
|
656
|
if ($inactive === false) {
|
657
|
continue;
|
658
|
} else {
|
659
|
$gateway['inactive'] = true;
|
660
|
}
|
661
|
}
|
662
|
$wancfg = $config['interfaces'][$gateway['interface']];
|
663
|
|
664
|
/* skip disabled interfaces */
|
665
|
if ($disabled === false && (!isset($wancfg['enable']))) {
|
666
|
continue;
|
667
|
}
|
668
|
|
669
|
/* if the gateway is dynamic and we can find the IPv4, Great! */
|
670
|
if (empty($gateway['gateway']) || $gateway['gateway'] == "dynamic") {
|
671
|
if ($gateway['ipprotocol'] == "inet") {
|
672
|
/* we know which interfaces is dynamic, this should be made a function */
|
673
|
$gateway['gateway'] = get_interface_gateway($gateway['interface']);
|
674
|
/* no IP address found, set to dynamic */
|
675
|
if (!is_ipaddrv4($gateway['gateway'])) {
|
676
|
$gateway['gateway'] = "dynamic";
|
677
|
}
|
678
|
$gateway['dynamic'] = true;
|
679
|
}
|
680
|
|
681
|
/* if the gateway is dynamic and we can find the IPv6, Great! */
|
682
|
else if ($gateway['ipprotocol'] == "inet6") {
|
683
|
/* we know which interfaces is dynamic, this should be made a function, and for v6 too */
|
684
|
$gateway['gateway'] = get_interface_gateway_v6($gateway['interface']);
|
685
|
/* no IPv6 address found, set to dynamic */
|
686
|
if (!is_ipaddrv6($gateway['gateway'])) {
|
687
|
$gateway['gateway'] = "dynamic";
|
688
|
}
|
689
|
$gateway['dynamic'] = true;
|
690
|
}
|
691
|
} else {
|
692
|
/* getting this detection right is hard at this point because we still don't
|
693
|
* store the address family in the gateway item */
|
694
|
if (is_ipaddrv4($gateway['gateway'])) {
|
695
|
$gateway['ipprotocol'] = "inet";
|
696
|
} else if (is_ipaddrv6($gateway['gateway'])) {
|
697
|
$gateway['ipprotocol'] = "inet6";
|
698
|
}
|
699
|
}
|
700
|
|
701
|
if (isset($gateway['monitor_disable'])) {
|
702
|
$gateway['monitor_disable'] = true;
|
703
|
} else if (empty($gateway['monitor'])) {
|
704
|
$gateway['monitor'] = $gateway['gateway'];
|
705
|
}
|
706
|
|
707
|
if (isset($gateway['action_disable'])) {
|
708
|
$gateway['action_disable'] = true;
|
709
|
}
|
710
|
|
711
|
$gateway['friendlyiface'] = $gateway['interface'];
|
712
|
$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($gateway['interface']);
|
713
|
|
714
|
/* special treatment for tunnel interfaces */
|
715
|
if ($gateway['ipprotocol'] == "inet6") {
|
716
|
$gateway['interface'] = get_real_interface($gateway['interface'], "inet6", false, false);
|
717
|
} else {
|
718
|
$gateway['interface'] = get_real_interface($gateway['interface'], "inet", false, false);
|
719
|
}
|
720
|
|
721
|
if ($gateway['ipprotocol'] == "inet" &&
|
722
|
($gateway['gateway'] == $cgw4)) {
|
723
|
$gateway['isdefaultgw'] = true;
|
724
|
$found_defaultv4 = 1;
|
725
|
} else if ($gateway['ipprotocol'] == "inet6" &&
|
726
|
($gateway['gateway'] == $cgw6)) {
|
727
|
$gateway['isdefaultgw'] = true;
|
728
|
$found_defaultv6 = 1;
|
729
|
}
|
730
|
/* include the gateway index as the attribute */
|
731
|
$gateway['attribute'] = $i;
|
732
|
|
733
|
/* Remember all the gateway names, even ones to be skipped because they are disabled. */
|
734
|
/* Then we can easily know and match them later when attempting to add dynamic gateways to the list. */
|
735
|
$gateways_arr_temp[$gateway['name']] = $gateway;
|
736
|
|
737
|
/* skip disabled gateways if the caller has not asked for them to be returned. */
|
738
|
if (!($disabled === false && isset($gateway['disabled']))) {
|
739
|
$gateways_arr[$gateway['name']] = $gateway;
|
740
|
}
|
741
|
}
|
742
|
}
|
743
|
unset($gateway);
|
744
|
|
745
|
//Sort the array by GW name before moving on.
|
746
|
ksort($gateways_arr, SORT_STRING | SORT_FLAG_CASE);
|
747
|
|
748
|
/* Loop through all interfaces with a gateway and add it to a array */
|
749
|
if ($disabled == false) {
|
750
|
$iflist = get_configured_interface_with_descr();
|
751
|
} else {
|
752
|
$iflist = get_configured_interface_with_descr(true);
|
753
|
}
|
754
|
|
755
|
/* Process/add dynamic v4 gateways. */
|
756
|
foreach ($iflist as $ifname => $friendly) {
|
757
|
if (!interface_has_gateway($ifname)) {
|
758
|
continue;
|
759
|
}
|
760
|
|
761
|
if (empty($config['interfaces'][$ifname])) {
|
762
|
continue;
|
763
|
}
|
764
|
|
765
|
$ifcfg = &$config['interfaces'][$ifname];
|
766
|
if (!isset($ifcfg['enable'])) {
|
767
|
continue;
|
768
|
}
|
769
|
|
770
|
if (!empty($ifcfg['ipaddr']) && is_ipaddrv4($ifcfg['ipaddr'])) {
|
771
|
continue;
|
772
|
}
|
773
|
|
774
|
$ctype = "";
|
775
|
switch ($ifcfg['ipaddr']) {
|
776
|
case "dhcp":
|
777
|
case "pppoe":
|
778
|
case "l2tp":
|
779
|
case "pptp":
|
780
|
case "ppp":
|
781
|
$ctype = strtoupper($ifcfg['ipaddr']);
|
782
|
break;
|
783
|
default:
|
784
|
$tunnelif = substr($ifcfg['if'], 0, 3);
|
785
|
if (substr($ifcfg['if'], 0, 4) == "ovpn") {
|
786
|
switch (substr($ifcfg['if'], 4, 1)) {
|
787
|
case "c":
|
788
|
$ovpntype = "openvpn-client";
|
789
|
break;
|
790
|
case "s":
|
791
|
$ovpntype = "openvpn-server";
|
792
|
break;
|
793
|
default:
|
794
|
// unknown ovpn type
|
795
|
continue 3;
|
796
|
}
|
797
|
$ovpnid = substr($ifcfg['if'], 5);
|
798
|
if (is_array($config['openvpn'][$ovpntype])) {
|
799
|
foreach ($config['openvpn'][$ovpntype] as & $ovpnconf) {
|
800
|
if ($ovpnconf['vpnid'] == $ovpnid) {
|
801
|
// skip IPv6-only interfaces
|
802
|
if ($ovpnconf['create_gw'] == "v6only") {
|
803
|
continue 3;
|
804
|
}
|
805
|
// skip tap interfaces
|
806
|
if ($ovpnconf['dev_mode'] == "tap") {
|
807
|
continue 3;
|
808
|
}
|
809
|
}
|
810
|
}
|
811
|
}
|
812
|
$ctype = "VPNv4";
|
813
|
} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
|
814
|
$ikeid = substr($ifcfg['if'], 5);
|
815
|
if (is_array($config['ipsec']) && is_array($config['ipsec']['phase1']) && is_array($config['ipsec']['phase2'])) {
|
816
|
foreach ($config['ipsec']['phase1'] as $ph1ent) {
|
817
|
if ($ph1ent['disabled']) {
|
818
|
continue;
|
819
|
}
|
820
|
$vtisubnet_spec = ipsec_vti($ph1ent, true);
|
821
|
// Skip non-VTI tunnels
|
822
|
if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
|
823
|
continue;
|
824
|
}
|
825
|
if (!isset($ph1ent['mobile']) && ($keyexchange == 'ikev1' || isset($ph1ent['splitconn']))) {
|
826
|
foreach ($vtisubnet_spec as $idx => $vtisub) {
|
827
|
if ($ifcfg['if'] == ipsec_get_ifname($ph1ent, $vtisub['reqid'])) {
|
828
|
// If this specific VTI remote is v4, then we can make a v4 gw
|
829
|
if (is_ipaddrv4($vtisub['right'])) {
|
830
|
$ctype = "VTIv4";
|
831
|
}
|
832
|
}
|
833
|
}
|
834
|
} else {
|
835
|
if ($ifcfg['if'] == ipsec_get_ifname($ph1ent)) {
|
836
|
// If any of the VTI remotes are v4, then we can make a v4 gw
|
837
|
foreach ($vtisubnet_spec as $vtisub) {
|
838
|
if (is_ipaddrv4($vtisub['right'])) {
|
839
|
$ctype = "VTIv4";
|
840
|
}
|
841
|
}
|
842
|
}
|
843
|
}
|
844
|
}
|
845
|
if (empty($ctype)) {
|
846
|
continue 2;
|
847
|
}
|
848
|
}
|
849
|
} elseif ($tunnelif == "gif" || $tunnelif == "gre") {
|
850
|
$ctype = "TUNNELv4";
|
851
|
}
|
852
|
break;
|
853
|
}
|
854
|
$ctype = "_". strtoupper($ctype);
|
855
|
|
856
|
$gateway = array();
|
857
|
$gateway['dynamic'] = false;
|
858
|
$gateway['ipprotocol'] = "inet";
|
859
|
$gateway['gateway'] = get_interface_gateway($ifname, $gateway['dynamic']);
|
860
|
$gateway['interface'] = get_real_interface($ifname);
|
861
|
$gateway['friendlyiface'] = $ifname;
|
862
|
$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
|
863
|
$gateway['name'] = "{$friendly}{$ctype}";
|
864
|
$gateway['attribute'] = "system";
|
865
|
|
866
|
if (($gateway['dynamic'] === "default") && ($found_defaultv4 == 0)) {
|
867
|
$gateway['isdefaultgw'] = true;
|
868
|
$gateway['dynamic'] = true;
|
869
|
$found_defaultv4 = 1;
|
870
|
}
|
871
|
|
872
|
/* Loopback dummy for dynamic interfaces without a IP */
|
873
|
if (!is_ipaddrv4($gateway['gateway']) && $gateway['dynamic'] == true) {
|
874
|
$gateway['gateway'] = "dynamic";
|
875
|
}
|
876
|
|
877
|
/* automatically skip known static and dynamic gateways that were previously processed */
|
878
|
foreach ($gateways_arr_temp as $gateway_item) {
|
879
|
if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name'])&& ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
|
880
|
(($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
|
881
|
continue 2;
|
882
|
}
|
883
|
}
|
884
|
|
885
|
if (is_ipaddrv4($gateway['gateway'])) {
|
886
|
$gateway['monitor'] = $gateway['gateway'];
|
887
|
}
|
888
|
|
889
|
$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
|
890
|
$gateways_arr[$gateway['name']] = $gateway;
|
891
|
}
|
892
|
unset($gateway);
|
893
|
|
894
|
/* Process/add dynamic v6 gateways. */
|
895
|
foreach ($iflist as $ifname => $friendly) {
|
896
|
/* If the user has disabled IPv6, they probably don't want any IPv6 gateways. */
|
897
|
if (!isset($config['system']['ipv6allow'])) {
|
898
|
break;
|
899
|
}
|
900
|
|
901
|
if (!interface_has_gatewayv6($ifname)) {
|
902
|
continue;
|
903
|
}
|
904
|
|
905
|
if (empty($config['interfaces'][$ifname])) {
|
906
|
continue;
|
907
|
}
|
908
|
|
909
|
$ifcfg = &$config['interfaces'][$ifname];
|
910
|
if (!isset($ifcfg['enable'])) {
|
911
|
continue;
|
912
|
}
|
913
|
|
914
|
if (!empty($ifcfg['ipaddrv6']) && is_ipaddrv6($ifcfg['ipaddrv6'])) {
|
915
|
continue;
|
916
|
}
|
917
|
|
918
|
$ctype = "";
|
919
|
switch ($ifcfg['ipaddrv6']) {
|
920
|
case "slaac":
|
921
|
case "dhcp6":
|
922
|
case "6to4":
|
923
|
case "6rd":
|
924
|
$ctype = strtoupper($ifcfg['ipaddrv6']);
|
925
|
break;
|
926
|
default:
|
927
|
$tunnelif = substr($ifcfg['if'], 0, 3);
|
928
|
if (substr($ifcfg['if'], 0, 4) == "ovpn") {
|
929
|
switch (substr($ifcfg['if'], 4, 1)) {
|
930
|
case "c":
|
931
|
$ovpntype = "openvpn-client";
|
932
|
break;
|
933
|
case "s":
|
934
|
$ovpntype = "openvpn-server";
|
935
|
break;
|
936
|
default:
|
937
|
// unknown ovpn type
|
938
|
continue 3;
|
939
|
}
|
940
|
$ovpnid = substr($ifcfg['if'], 5);
|
941
|
if (is_array($config['openvpn'][$ovpntype])) {
|
942
|
foreach ($config['openvpn'][$ovpntype] as & $ovpnconf) {
|
943
|
if ($ovpnconf['vpnid'] == $ovpnid) {
|
944
|
// skip IPv4-only interfaces
|
945
|
if ($ovpnconf['create_gw'] == "v4only") {
|
946
|
continue 3;
|
947
|
}
|
948
|
// skip tap interfaces
|
949
|
if ($ovpnconf['dev_mode'] == "tap") {
|
950
|
continue 3;
|
951
|
}
|
952
|
}
|
953
|
}
|
954
|
}
|
955
|
$ctype = "VPNv6";
|
956
|
} elseif (substr($ifcfg['if'], 0, 5) == "ipsec") {
|
957
|
$ikeid = substr($ifcfg['if'], 5);
|
958
|
if (is_array($config['ipsec']) && is_array($config['ipsec']['phase1']) && is_array($config['ipsec']['phase2'])) {
|
959
|
foreach ($config['ipsec']['phase1'] as $ph1ent) {
|
960
|
if ($ph1ent['disabled']) {
|
961
|
continue;
|
962
|
}
|
963
|
$vtisubnet_spec = ipsec_vti($ph1ent, true);
|
964
|
// Skip non-VTI tunnels
|
965
|
if (!$vtisubnet_spec || !is_array($vtisubnet_spec)) {
|
966
|
continue;
|
967
|
}
|
968
|
if (!isset($ph1ent['mobile']) && ($keyexchange == 'ikev1' || isset($ph1ent['splitconn']))) {
|
969
|
foreach ($vtisubnet_spec as $idx => $vtisub) {
|
970
|
if ($ifcfg['if'] == ipsec_get_ifname($ph1ent, $vtisub['reqid'])) {
|
971
|
// If this specific VTI remote is v6, then we can make a v6 gw
|
972
|
if (is_ipaddrv6($vtisub['right'])) {
|
973
|
$ctype = "VTIv6";
|
974
|
}
|
975
|
}
|
976
|
}
|
977
|
} else {
|
978
|
if ($ifcfg['if'] == ipsec_get_ifname($ph1ent)) {
|
979
|
// If any of the VTI remotes are v6, then we can make a v6 gw
|
980
|
foreach ($vtisubnet_spec as $vtisub) {
|
981
|
if (is_ipaddrv6($vtisub['right'])) {
|
982
|
$ctype = "VTIv6";
|
983
|
}
|
984
|
}
|
985
|
}
|
986
|
}
|
987
|
}
|
988
|
if (empty($ctype)) {
|
989
|
continue 2;
|
990
|
}
|
991
|
}
|
992
|
} else if ($tunnelif == "gif" || $tunnelif == "gre") {
|
993
|
$ctype = "TUNNELv6";
|
994
|
}
|
995
|
break;
|
996
|
}
|
997
|
$ctype = "_". strtoupper($ctype);
|
998
|
|
999
|
$gateway = array();
|
1000
|
$gateway['dynamic'] = false;
|
1001
|
$gateway['ipprotocol'] = "inet6";
|
1002
|
$gateway['gateway'] = get_interface_gateway_v6($ifname, $gateway['dynamic']);
|
1003
|
$gateway['interface'] = get_real_interface($ifname, "inet6");
|
1004
|
switch ($ifcfg['ipaddrv6']) {
|
1005
|
case "6rd":
|
1006
|
case "6to4":
|
1007
|
$gateway['dynamic'] = "default";
|
1008
|
break;
|
1009
|
}
|
1010
|
$gateway['friendlyiface'] = $ifname;
|
1011
|
$gateway['friendlyifdescr'] = convert_friendly_interface_to_friendly_descr($ifname);
|
1012
|
$gateway['name'] = "{$friendly}{$ctype}";
|
1013
|
$gateway['attribute'] = "system";
|
1014
|
|
1015
|
if (($gateway['dynamic'] === "default") && ($found_defaultv6 == 0)) {
|
1016
|
$gateway['isdefaultgw'] = true;
|
1017
|
$gateway['dynamic'] = true;
|
1018
|
$found_defaultv6 = 1;
|
1019
|
}
|
1020
|
|
1021
|
/* Loopback dummy for dynamic interfaces without a IP */
|
1022
|
if (!is_ipaddrv6($gateway['gateway']) && $gateway['dynamic'] == true) {
|
1023
|
$gateway['gateway'] = "dynamic";
|
1024
|
}
|
1025
|
|
1026
|
/* automatically skip known static and dynamic gateways that were previously processed */
|
1027
|
foreach ($gateways_arr_temp as $gateway_item) {
|
1028
|
if ((($ifname == $gateway_item['friendlyiface'] && $friendly == $gateway_item['name']) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol'])) ||
|
1029
|
(($ifname == $gateway_item['friendlyiface'] && $gateway_item['dynamic'] == true) && ($gateway['ipprotocol'] == $gateway_item['ipprotocol']))) {
|
1030
|
continue 2;
|
1031
|
}
|
1032
|
}
|
1033
|
|
1034
|
if (is_ipaddrv6($gateway['gateway'])) {
|
1035
|
$gateway['monitor'] = $gateway['gateway'];
|
1036
|
}
|
1037
|
|
1038
|
$gateway['descr'] = "Interface {$friendly}{$ctype} Gateway";
|
1039
|
$gateways_arr[$gateway['name']] = $gateway;
|
1040
|
}
|
1041
|
unset($gateway);
|
1042
|
|
1043
|
/* FIXME: Should this be enabled.
|
1044
|
* Some interface like wan might be default but have no info recorded
|
1045
|
* the config. */
|
1046
|
/* this is a fallback if all else fails and we want to get packets out @smos */
|
1047
|
if ($found_defaultv4 == 0 || $found_defaultv6 == 0) {
|
1048
|
foreach ($gateways_arr as &$gateway) {
|
1049
|
if (($gateway['friendlyiface'] == "wan") && ($found_defaultv4 == 0) && (!isset($gateway['ipprotocol']) || ($gateway['ipprotocol'] == "inet"))) {
|
1050
|
if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgw")) {
|
1051
|
$gateway['isdefaultgw'] = true;
|
1052
|
$found_defaultv4 = 1;
|
1053
|
}
|
1054
|
}
|
1055
|
else if (($gateway['friendlyiface'] == "wan") && ($found_defaultv6 == 0) && ($gateway['ipprotocol'] == "inet6")) {
|
1056
|
if (file_exists("{$g['tmp_path']}/{$gateway['interface']}_defaultgwv6")) {
|
1057
|
$gateway['isdefaultgw'] = true;
|
1058
|
$found_defaultv6 = 1;
|
1059
|
}
|
1060
|
}
|
1061
|
}
|
1062
|
}
|
1063
|
|
1064
|
if ($localhost === true) {
|
1065
|
/* attach localhost for Null routes */
|
1066
|
$gwlo4 = array();
|
1067
|
$gwlo4['name'] = "Null4";
|
1068
|
$gwlo4['interface'] = "lo0";
|
1069
|
$gwlo4['ipprotocol'] = "inet";
|
1070
|
$gwlo4['gateway'] = "127.0.0.1";
|
1071
|
$gwlo4['attribute'] = "system";
|
1072
|
$gwlo6 = array();
|
1073
|
$gwlo6['name'] = "Null6";
|
1074
|
$gwlo6['interface'] = "lo0";
|
1075
|
$gwlo6['ipprotocol'] = "inet6";
|
1076
|
$gwlo6['gateway'] = "::1";
|
1077
|
$gwlo6['attribute'] = "system";
|
1078
|
$gateways_arr['Null4'] = $gwlo4;
|
1079
|
$gateways_arr['Null6'] = $gwlo6;
|
1080
|
}
|
1081
|
|
1082
|
if ($integer_index) {
|
1083
|
$gateways_arr = array_values($gateways_arr);
|
1084
|
}
|
1085
|
|
1086
|
if ($found_defaultv4 != 1 && is_ipaddr($cgw4)) {
|
1087
|
foreach($gateways_arr as &$gw) {
|
1088
|
if ($gw['gateway'] == $cgw4) {
|
1089
|
$gw['isdefaultgw'] = true;
|
1090
|
}
|
1091
|
}
|
1092
|
}
|
1093
|
if ($found_defaultv6 != 1 && is_ipaddr($cgw6)) {
|
1094
|
foreach($gateways_arr as &$gw) {
|
1095
|
if ($gw['gateway'] == $cgw6) {
|
1096
|
$gw['isdefaultgw'] = true;
|
1097
|
}
|
1098
|
}
|
1099
|
}
|
1100
|
|
1101
|
$gways = order_gateways_as_configured($gateways_arr);
|
1102
|
|
1103
|
// Add the tier names here so that system_gateways.php doesn't need to
|
1104
|
foreach ($gways as $idx => $gway) {
|
1105
|
$gways[$idx]['tiername'] = gateway_getgwtiername($gways, $idx);
|
1106
|
}
|
1107
|
|
1108
|
return $gways;
|
1109
|
}
|
1110
|
|
1111
|
function fixup_default_gateway($ipprotocol, $gateways_status, $gateways_arr) {
|
1112
|
global $config, $g;
|
1113
|
/*
|
1114
|
* NOTE: The code below is meant to replace the default gateway when it goes down.
|
1115
|
* This facilitates services running on pfSense itself and are not handled by a PBR to continue working.
|
1116
|
*/
|
1117
|
$set_dfltgwname = '';
|
1118
|
|
1119
|
if ($ipprotocol == 'inet') {
|
1120
|
$gwdefault = $config['gateways']['defaultgw4'];
|
1121
|
} else {
|
1122
|
$gwdefault = $config['gateways']['defaultgw6'];
|
1123
|
}
|
1124
|
/* do not delete dynamic (frr/bgp/ospf) route
|
1125
|
* see https://redmine.pfsense.org/issues/12536 */
|
1126
|
if ($gwdefault == "-") {
|
1127
|
if (is_static_route('default', $ipprotocol)) {
|
1128
|
route_del('default', $ipprotocol);
|
1129
|
}
|
1130
|
return;
|
1131
|
}
|
1132
|
if (isset($gateways_arr[$gwdefault])) {
|
1133
|
// the configured gateway is a regular one. (not a gwgroup) use it as is..
|
1134
|
$set_dfltgwname = $gwdefault;
|
1135
|
} elseif (empty($gwdefault)) {
|
1136
|
// 'automatic' mode, pick the first one thats 'up' or 'unmonitored' which is always considered up
|
1137
|
$gateways_arr = order_gateways_as_configured($gateways_arr);
|
1138
|
$fallback = "";
|
1139
|
foreach($gateways_arr as $gwname => $gwsttng) {
|
1140
|
if (($gwsttng['ipprotocol'] != $ipprotocol) || isset($gwsttng['force_down'])) {
|
1141
|
continue;
|
1142
|
}
|
1143
|
|
1144
|
if (isset($gwsttng['monitor_disable']) || isset($gwsttng['action_disable']) ||
|
1145
|
($gateways_status[$gwname]['status'] == "online")) {
|
1146
|
$set_dfltgwname = $gwname;
|
1147
|
break;
|
1148
|
}
|
1149
|
if (empty($fallback) && $gwsttng['interface'] != 'lo0') {
|
1150
|
$fallback = $gwname;
|
1151
|
}
|
1152
|
}
|
1153
|
if (empty($set_dfltgwname) && !empty($fallback)) {
|
1154
|
log_error(sprintf("Gateway, none 'available' for %s, use the first one configured. '%s'", $ipprotocol, $fallback));
|
1155
|
$set_dfltgwname = $fallback;
|
1156
|
} else {
|
1157
|
log_error("Gateway, NONE AVAILABLE");
|
1158
|
}
|
1159
|
} else {
|
1160
|
// a gwgroup is selected
|
1161
|
// find the best available gateway given options available..
|
1162
|
$gwg_members = array();
|
1163
|
$viplist = get_configured_vip_list();
|
1164
|
if (is_array($config['gateways']['gateway_group'])) {
|
1165
|
foreach ($config['gateways']['gateway_group'] as $group) {
|
1166
|
if ($group['name'] == $gwdefault) {
|
1167
|
// finds the gw members of the best available tier for this group.
|
1168
|
$gwg_members = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
|
1169
|
}
|
1170
|
}
|
1171
|
}
|
1172
|
|
1173
|
if (count($gwg_members) > 0) {
|
1174
|
$currentdefaultgwip = route_get_default($ipprotocol);
|
1175
|
$found_current = false;
|
1176
|
foreach($gwg_members as $gwgroupitem) {
|
1177
|
if (!empty($currentdefaultgwip) && ($gwgroupitem['gwip'] == $currentdefaultgwip)) {
|
1178
|
$set_dfltgwname = $gwgroupitem['gw'];
|
1179
|
$found_current = true;
|
1180
|
if (isset($config['system']['gw-debug'])) {
|
1181
|
log_error("Keep current gateway, its already part of the group members.");
|
1182
|
}
|
1183
|
break;
|
1184
|
}
|
1185
|
}
|
1186
|
if (!$found_current) {
|
1187
|
$set_dfltgwname = $gwg_members[0]['gw'];
|
1188
|
log_error(sprintf("Gateway, switch to: %s", $set_dfltgwname));
|
1189
|
}
|
1190
|
} else {
|
1191
|
log_error("Gateway, NONE AVAILABLE");
|
1192
|
}
|
1193
|
}
|
1194
|
if (!empty($set_dfltgwname) && isset($gateways_arr[$set_dfltgwname])) {
|
1195
|
setdefaultgateway($gateways_arr[$set_dfltgwname]);
|
1196
|
} elseif (empty($set_dfltgwname)) {
|
1197
|
route_del('default', $ipprotocol);
|
1198
|
}
|
1199
|
}
|
1200
|
|
1201
|
function setdefaultgateway($gw) {
|
1202
|
global $g, $config;
|
1203
|
if (isset($config['system']['route-debug'])) {
|
1204
|
file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']}");
|
1205
|
}
|
1206
|
$ipprotocol = $gw['ipprotocol'];
|
1207
|
if ($gw['gateway'] == "dynamic") {
|
1208
|
if ($ipprotocol == 'inet') {
|
1209
|
$gw['gateway'] = get_interface_gateway($gw['friendlyiface']);
|
1210
|
} else {
|
1211
|
$gw['gateway'] = get_interface_gateway_v6($$gw['friendlyiface']);
|
1212
|
}
|
1213
|
}
|
1214
|
if ($ipprotocol == 'inet6' && !is_ipaddrv6($gw['gateway'])) {
|
1215
|
return;
|
1216
|
}
|
1217
|
if ($ipprotocol == 'inet' && !is_ipaddrv4($gw['gateway'])) {
|
1218
|
return;
|
1219
|
}
|
1220
|
if ($ipprotocol == 'inet6') {
|
1221
|
if (is_linklocal($gw['gateway']) && get_ll_scope($gw['gateway']) == '') {
|
1222
|
$gw['gateway'] .= "%" . $gw['interface'];
|
1223
|
}
|
1224
|
}
|
1225
|
$currentdefaultgwip = route_get_default($ipprotocol);
|
1226
|
if ($currentdefaultgwip != $gw['gateway']) {
|
1227
|
log_error("Default gateway setting {$gw['descr']} as default.");
|
1228
|
|
1229
|
if ($ipprotocol == 'inet') {
|
1230
|
$inet = '';
|
1231
|
} else {
|
1232
|
$inet = 'v6';
|
1233
|
}
|
1234
|
array_map('unlink', glob("{$g['tmp_path']}/*_defaultgw{$inet}", GLOB_BRACE));
|
1235
|
$defaultif = get_real_interface($gw['interface']);
|
1236
|
if ($defaultif) {
|
1237
|
@file_put_contents("{$g['tmp_path']}/{$defaultif}_defaultgw{$inet}", $gw['gateway']);
|
1238
|
}
|
1239
|
|
1240
|
if (isset($gw["nonlocalgateway"])) {
|
1241
|
if (is_ipaddr($gw['gateway']) && !empty($gw['interface'])) {
|
1242
|
route_add_or_change($gw['gateway'], '',
|
1243
|
$gw['interface']);
|
1244
|
}
|
1245
|
}
|
1246
|
if (isset($config['system']['route-debug'])) {
|
1247
|
file_put_contents("/dev/console", "\n[".getmypid()."] SET DEF GW: {$gw['name']} ({$gw['gateway']})");
|
1248
|
}
|
1249
|
route_add_or_change("default", $gw['gateway'], '', '',
|
1250
|
$ipprotocol);
|
1251
|
return true;
|
1252
|
}
|
1253
|
}
|
1254
|
|
1255
|
function get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist){
|
1256
|
$result = array();
|
1257
|
/* create array with group gateways members separated by tier */
|
1258
|
$tiers = array();
|
1259
|
$backupplan = array();
|
1260
|
$gwvip_arr = array();
|
1261
|
foreach ($group['item'] as $item) {
|
1262
|
list($gwname, $tier, $vipname) = explode("|", $item);
|
1263
|
|
1264
|
if (is_ipaddr($viplist[$vipname])) {
|
1265
|
if (!is_array($gwvip_arr[$group['name']])) {
|
1266
|
$gwvip_arr[$group['name']] = array();
|
1267
|
}
|
1268
|
$gwvip_arr[$group['name']][$gwname] = $vipname;
|
1269
|
}
|
1270
|
|
1271
|
/* Do it here rather than reiterating again the group in case no member is up. */
|
1272
|
if (!is_array($backupplan[$tier])) {
|
1273
|
$backupplan[$tier] = array();
|
1274
|
}
|
1275
|
$backupplan[$tier][] = $gwname;
|
1276
|
|
1277
|
/* check if the gateway is available before adding it to the array */
|
1278
|
if (is_array($gateways_status[$gwname])) {
|
1279
|
$status = $gateways_status[$gwname];
|
1280
|
$gwdown = false;
|
1281
|
if (stristr($status['status'], "down")) {
|
1282
|
$gwdown = true;
|
1283
|
switch ($status['substatus']) {
|
1284
|
case "highloss":
|
1285
|
$msg = sprintf(gettext('MONITOR: %1$s has packet loss, omitting from routing group %2$s'), $gwname, $group['name']);
|
1286
|
break;
|
1287
|
case "highdelay":
|
1288
|
$msg = sprintf(gettext('MONITOR: %1$s has high latency, omitting from routing group %2$s'), $gwname, $group['name']);
|
1289
|
break;
|
1290
|
default:
|
1291
|
$msg = sprintf(gettext('MONITOR: %1$s is down, omitting from routing group %2$s'), $gwname, $group['name']);
|
1292
|
}
|
1293
|
}
|
1294
|
$statuschanged = false;
|
1295
|
$pluginparams = array();
|
1296
|
$pluginparams['type'] = 'gateway';
|
1297
|
$pluginparams['name'] = $gwname;
|
1298
|
if ($gwdown == true) {
|
1299
|
if (!file_exists("/tmp/.down.{$gwname}")) {
|
1300
|
@touch("/tmp/.down.{$gwname}");
|
1301
|
$msg .= "\n".implode("|", $status);
|
1302
|
$pluginparams['event'] = 'gateway.down';
|
1303
|
$statuschanged = true;
|
1304
|
}
|
1305
|
} else {
|
1306
|
/* Online add member */
|
1307
|
if (!is_array($tiers[$tier])) {
|
1308
|
$tiers[$tier] = array();
|
1309
|
}
|
1310
|
$tiers[$tier][] = $gwname;
|
1311
|
if (unlink_if_exists("/tmp/.down.{$gwname}")) {
|
1312
|
$msg = sprintf(gettext('MONITOR: %1$s is available now, adding to routing group %2$s'), $gwname, $group['name']);
|
1313
|
$msg .= "\n".implode("|", $status);
|
1314
|
$pluginparams['event'] = 'gateway.up';
|
1315
|
$statuschanged = true;
|
1316
|
}
|
1317
|
}
|
1318
|
if ($statuschanged) {
|
1319
|
log_error($msg);
|
1320
|
notify_all_remote($msg);
|
1321
|
if (isset($gateways_arr[$gwname]['interface'])) {
|
1322
|
$pluginparams['interface'] = $gateways_arr[$gwname]['interface'];
|
1323
|
}
|
1324
|
pkg_call_plugins('plugin_gateway', $pluginparams);
|
1325
|
}
|
1326
|
} else if (isset($gateways_arr[$gwname]['monitor_disable']) || isset($gateways_arr[$gwname]['action_disable'])) {
|
1327
|
$tiers[$tier][] = $gwname;
|
1328
|
}
|
1329
|
}
|
1330
|
$tiers_count = count($tiers);
|
1331
|
if ($tiers_count == 0) {
|
1332
|
/* Oh dear, we have no members! Engage Plan B */
|
1333
|
if (isset($config['system']['gw-debug']) && (!platform_booting())) {
|
1334
|
$msg = sprintf(gettext('Gateways status could not be determined, considering all as up/active. (Group: %s)'), $group['name']);
|
1335
|
log_error($msg);
|
1336
|
}
|
1337
|
$tiers = $backupplan;
|
1338
|
}
|
1339
|
/* sort the tiers array by the tier key */
|
1340
|
ksort($tiers);
|
1341
|
|
1342
|
/* we do not really foreach the tiers as we stop after the first tier */
|
1343
|
foreach ($tiers as $tieridx => $tier) {
|
1344
|
/* process all gateways in this tier */
|
1345
|
foreach ($tier as $member) {
|
1346
|
/* determine interface gateway */
|
1347
|
if (isset($gateways_arr[$member])) {
|
1348
|
$gateway = $gateways_arr[$member];
|
1349
|
$int = $gateway['interface'];
|
1350
|
$gatewayip = "";
|
1351
|
if (is_ipaddr($gateway['gateway'])) {
|
1352
|
$gatewayip = $gateway['gateway'];
|
1353
|
} elseif (!empty($int)) {
|
1354
|
if ($gateway['ipprotocol'] == 'inet') {
|
1355
|
$gatewayip = get_interface_gateway($gateway['friendlyiface']);
|
1356
|
} else {
|
1357
|
$gatewayip = get_interface_gateway_v6($gateway['friendlyiface']);
|
1358
|
}
|
1359
|
}
|
1360
|
|
1361
|
if (!empty($int)) {
|
1362
|
$result['ipprotocol'] = $gateway['ipprotocol'];
|
1363
|
if (is_ipaddr($gatewayip)) {
|
1364
|
$groupmember = array();
|
1365
|
$groupmember['gw'] = $member;
|
1366
|
$groupmember['int'] = $int;
|
1367
|
$groupmember['gwip'] = $gatewayip;
|
1368
|
/* set correct linklocal gateway address,
|
1369
|
* see https://redmine.pfsense.org/issues/12721 */
|
1370
|
if (is_ipaddrv6($gatewayip) && is_linklocal($gatewayip) && empty(get_ll_scope($gatewayip))) {
|
1371
|
$groupmember['gwip'] .= '%' . $int;
|
1372
|
}
|
1373
|
$groupmember['weight'] = isset($gateway['weight']) ? $gateway['weight'] : 1;
|
1374
|
if (is_array($gwvip_arr[$group['name']]) && !empty($gwvip_arr[$group['name']][$member])) {
|
1375
|
$groupmember['vip'] = $gwvip_arr[$group['name']][$member];
|
1376
|
}
|
1377
|
$result[] = $groupmember;
|
1378
|
}
|
1379
|
}
|
1380
|
}
|
1381
|
}
|
1382
|
/* we should have the 1st available tier now, exit stage left */
|
1383
|
if (count($result) > 0) {
|
1384
|
break;
|
1385
|
} else {
|
1386
|
log_error(sprintf(gettext('GATEWAYS: Group %1$s did not have any gateways up on tier %2$s!'), $group['name'], $tieridx));
|
1387
|
}
|
1388
|
}
|
1389
|
// Add description field last to not influence the count() above
|
1390
|
$result['descr'] = $group['descr'];
|
1391
|
return $result;
|
1392
|
}
|
1393
|
|
1394
|
function get_gwgroup_members($groupname) {
|
1395
|
global $config;
|
1396
|
$gateways_status = return_gateways_status(true);
|
1397
|
$gateways_arr = return_gateways_array();
|
1398
|
$viplist = get_configured_vip_list();
|
1399
|
foreach ($config['gateways']['gateway_group'] as $group) {
|
1400
|
if ($group['name'] == $groupname) {
|
1401
|
return get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
|
1402
|
}
|
1403
|
}
|
1404
|
return array();
|
1405
|
}
|
1406
|
|
1407
|
/*
|
1408
|
* Return an array with all gateway groups with name as key
|
1409
|
* All gateway groups will be processed before returning the array.
|
1410
|
*/
|
1411
|
function return_gateway_groups_array($fixup = false, $gways = false) {
|
1412
|
global $config;
|
1413
|
|
1414
|
/* fetch the current gateways status */
|
1415
|
if (is_array($gways)) {
|
1416
|
$gateways_status = $gways;
|
1417
|
} else {
|
1418
|
$gateways_status = return_gateways_status(true);
|
1419
|
}
|
1420
|
|
1421
|
$gateways_arr = return_gateways_array();
|
1422
|
$gateway_groups_array = array();
|
1423
|
if ($fixup == true) {
|
1424
|
$gw4 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw4'], $gways);
|
1425
|
$gw6 = lookup_gateway_or_group_by_name($config['gateways']['defaultgw6'], $gways);
|
1426
|
if ($gw4 && $gw4['type'] == 'gatewaygroup') {
|
1427
|
fixup_default_gateway("inet", $gateways_status, $gateways_arr);
|
1428
|
}
|
1429
|
if ($gw6 && $gw6['type'] == 'gatewaygroup') {
|
1430
|
fixup_default_gateway("inet6", $gateways_status, $gateways_arr);
|
1431
|
}
|
1432
|
}
|
1433
|
init_config_arr(array('gateways', 'gateway_group'));
|
1434
|
if (!empty($config['gateways']['gateway_group'])) {
|
1435
|
$viplist = get_configured_vip_list();
|
1436
|
foreach ($config['gateways']['gateway_group'] as $group) {
|
1437
|
$gateway_groups_array[$group['name']] = get_gwgroup_members_inner($group, $gateways_status, $gateways_arr, $viplist);
|
1438
|
}
|
1439
|
}
|
1440
|
|
1441
|
return ($gateway_groups_array);
|
1442
|
}
|
1443
|
|
1444
|
/* Update DHCP WAN Interface ip address in gateway group item */
|
1445
|
function dhclient_update_gateway_groups_defaultroute($interface = "wan") {
|
1446
|
global $config;
|
1447
|
|
1448
|
if (is_array($config['gateways']['gateway_item'])) {
|
1449
|
foreach ($config['gateways']['gateway_item'] as & $gw) {
|
1450
|
if ($gw['interface'] != $interface) {
|
1451
|
continue;
|
1452
|
}
|
1453
|
|
1454
|
$current_gw = get_interface_gateway($interface);
|
1455
|
if ($gw['gateway'] <> $current_gw) {
|
1456
|
$gw['gateway'] = $current_gw;
|
1457
|
$changed = true;
|
1458
|
}
|
1459
|
}
|
1460
|
}
|
1461
|
|
1462
|
if ($changed && $current_gw) {
|
1463
|
write_config(sprintf(gettext(
|
1464
|
'Updating gateway group gateway for %1$s - new gateway is %2$s'),
|
1465
|
$interface, $current_gw));
|
1466
|
}
|
1467
|
}
|
1468
|
|
1469
|
function lookup_gateway_or_group_by_name($gwname, $gways = false) {
|
1470
|
global $config;
|
1471
|
|
1472
|
if (is_array($gways)) {
|
1473
|
$gateways_arr = $gways;
|
1474
|
} else {
|
1475
|
$gateways_arr = return_gateways_array();
|
1476
|
}
|
1477
|
|
1478
|
foreach ($gateways_arr as $gw) {
|
1479
|
if ($gw['name'] == $gwname) {
|
1480
|
$gw['type'] = 'gateway';
|
1481
|
return $gw;
|
1482
|
}
|
1483
|
}
|
1484
|
|
1485
|
init_config_arr(array('gateways', 'gateway_group'));
|
1486
|
foreach ($config['gateways']['gateway_group'] as $gwg) {
|
1487
|
if ($gwg['name'] == $gwname) {
|
1488
|
$gwg['type'] = 'gatewaygroup';
|
1489
|
return $gwg;
|
1490
|
}
|
1491
|
}
|
1492
|
|
1493
|
return false;
|
1494
|
}
|
1495
|
|
1496
|
function lookup_gateway_ip_by_name($name, $disabled = false) {
|
1497
|
|
1498
|
$gateways_arr = return_gateways_array($disabled, true);
|
1499
|
foreach ($gateways_arr as $gname => $gw) {
|
1500
|
if ($gw['name'] === $name || $gname === $name) {
|
1501
|
return $gw['gateway'];
|
1502
|
}
|
1503
|
}
|
1504
|
|
1505
|
return false;
|
1506
|
}
|
1507
|
|
1508
|
function lookup_gateway_monitor_ip_by_name($name) {
|
1509
|
|
1510
|
$gateways_arr = return_gateways_array(false, true);
|
1511
|
if (!empty($gateways_arr[$name])) {
|
1512
|
$gateway = $gateways_arr[$name];
|
1513
|
if (!is_ipaddr($gateway['monitor'])) {
|
1514
|
return $gateway['gateway'];
|
1515
|
}
|
1516
|
|
1517
|
return $gateway['monitor'];
|
1518
|
}
|
1519
|
|
1520
|
return (false);
|
1521
|
}
|
1522
|
|
1523
|
function lookup_gateway_interface_by_name($name) {
|
1524
|
|
1525
|
$gateways_arr = return_gateways_array(false, true);
|
1526
|
if (!empty($gateways_arr[$name])) {
|
1527
|
$interfacegw = $gateways_arr[$name]['friendlyiface'];
|
1528
|
return ($interfacegw);
|
1529
|
}
|
1530
|
|
1531
|
return (false);
|
1532
|
}
|
1533
|
|
1534
|
function get_root_interface($interface) {
|
1535
|
if (substr($interface, 0, 4) == '_vip') {
|
1536
|
$interface = get_configured_vip_interface($interface);
|
1537
|
if (substr($interface, 0, 4) == '_vip') {
|
1538
|
$interface = get_configured_vip_interface($interface);
|
1539
|
}
|
1540
|
}
|
1541
|
return $interface;
|
1542
|
}
|
1543
|
|
1544
|
function get_interface_gateway($interface, &$dynamic = false) {
|
1545
|
global $config, $g;
|
1546
|
|
1547
|
$interface = get_root_interface($interface);
|
1548
|
|
1549
|
$gw = NULL;
|
1550
|
$gwcfg = $config['interfaces'][$interface];
|
1551
|
if (!empty($gwcfg['gateway']) && is_array($config['gateways']['gateway_item'])) {
|
1552
|
foreach ($config['gateways']['gateway_item'] as $gateway) {
|
1553
|
if (($gateway['name'] == $gwcfg['gateway']) && (is_ipaddrv4($gateway['gateway']))) {
|
1554
|
$gw = $gateway['gateway'];
|
1555
|
break;
|
1556
|
}
|
1557
|
}
|
1558
|
}
|
1559
|
|
1560
|
// for dynamic interfaces we handle them through the $interface_router file.
|
1561
|
if (($gw == NULL || !is_ipaddrv4($gw)) && !is_ipaddrv4($gwcfg['ipaddr'])) {
|
1562
|
$realif = get_real_interface($interface);
|
1563
|
if (file_exists("{$g['tmp_path']}/{$realif}_router")) {
|
1564
|
$gw = trim(@file_get_contents("{$g['tmp_path']}/{$realif}_router"), " \n");
|
1565
|
$dynamic = true;
|
1566
|
}
|
1567
|
if (file_exists("{$g['tmp_path']}/{$realif}_defaultgw")) {
|
1568
|
$dynamic = "default";
|
1569
|
}
|
1570
|
|
1571
|
}
|
1572
|
|
1573
|
/* return gateway */
|
1574
|
return ($gw);
|
1575
|
}
|
1576
|
|
1577
|
function get_interface_gateway_v6($interface, &$dynamic = false) {
|
1578
|
global $config, $g;
|
1579
|
|
1580
|
$interface = get_root_interface($interface);
|
1581
|
|
1582
|
$gw = NULL;
|
1583
|
$gwcfg = $config['interfaces'][$interface];
|
1584
|
if (!empty($gwcfg['gatewayv6']) && is_array($config['gateways']['gateway_item'])) {
|
1585
|
foreach ($config['gateways']['gateway_item'] as $gateway) {
|
1586
|
if (($gateway['name'] == $gwcfg['gatewayv6']) && (is_ipaddrv6($gateway['gateway']))) {
|
1587
|
$gw = $gateway['gateway'];
|
1588
|
break;
|
1589
|
}
|
1590
|
}
|
1591
|
}
|
1592
|
|
1593
|
// for dynamic interfaces we handle them through the $interface_router file.
|
1594
|
if (($gw == NULL || !is_ipaddrv6($gw)) && !is_ipaddrv6($gwcfg['ipaddrv6'])) {
|
1595
|
$realif = get_real_interface($interface);
|
1596
|
if (file_exists("{$g['tmp_path']}/{$realif}_routerv6")) {
|
1597
|
$gw = trim(file_get_contents("{$g['tmp_path']}/{$realif}_routerv6"), " \n");
|
1598
|
$dynamic = true;
|
1599
|
}
|
1600
|
if (file_exists("{$g['tmp_path']}/{$realif}_defaultgwv6")) {
|
1601
|
$dynamic = "default";
|
1602
|
}
|
1603
|
}
|
1604
|
/* return gateway */
|
1605
|
return ($gw);
|
1606
|
}
|
1607
|
|
1608
|
/* Check a IP address against a gateway IP or name
|
1609
|
* to verify it's address family */
|
1610
|
function validate_address_family($ipaddr, $gwname, $disabled = false) {
|
1611
|
$v4ip = false;
|
1612
|
$v6ip = false;
|
1613
|
$v4gw = false;
|
1614
|
$v6gw = false;
|
1615
|
|
1616
|
if (is_ipaddrv4($ipaddr)) {
|
1617
|
$v4ip = true;
|
1618
|
}
|
1619
|
if (is_ipaddrv6($ipaddr)) {
|
1620
|
$v6ip = true;
|
1621
|
}
|
1622
|
if (is_ipaddrv4($gwname)) {
|
1623
|
$v4gw = true;
|
1624
|
}
|
1625
|
if (is_ipaddrv6($gwname)) {
|
1626
|
$v6gw = true;
|
1627
|
}
|
1628
|
|
1629
|
if ($v4ip && $v4gw) {
|
1630
|
return true;
|
1631
|
}
|
1632
|
if ($v6ip && $v6gw) {
|
1633
|
return true;
|
1634
|
}
|
1635
|
|
1636
|
/* still no match, carry on, lookup gateways */
|
1637
|
if (is_ipaddrv4(lookup_gateway_ip_by_name($gwname, $disabled))) {
|
1638
|
$v4gw = true;
|
1639
|
}
|
1640
|
if (is_ipaddrv6(lookup_gateway_ip_by_name($gwname, $disabled))) {
|
1641
|
$v6gw = true;
|
1642
|
}
|
1643
|
|
1644
|
$gw_array = return_gateways_array();
|
1645
|
if (is_array($gw_array[$gwname])) {
|
1646
|
switch ($gw_array[$gwname]['ipprotocol']) {
|
1647
|
case "inet":
|
1648
|
$v4gw = true;
|
1649
|
break;
|
1650
|
case "inet6":
|
1651
|
$v6gw = true;
|
1652
|
break;
|
1653
|
}
|
1654
|
}
|
1655
|
|
1656
|
if ($v4ip && $v4gw) {
|
1657
|
return true;
|
1658
|
}
|
1659
|
if ($v6ip && $v6gw) {
|
1660
|
return true;
|
1661
|
}
|
1662
|
|
1663
|
return false;
|
1664
|
}
|
1665
|
|
1666
|
/* check if a interface is part of a gateway group */
|
1667
|
function interface_gateway_group_member($interface, $gwgroup_name = "") {
|
1668
|
global $config;
|
1669
|
|
1670
|
if (is_array($config['gateways']['gateway_group'])) {
|
1671
|
$groups = $config['gateways']['gateway_group'];
|
1672
|
} else {
|
1673
|
return false;
|
1674
|
}
|
1675
|
|
1676
|
$gateways_arr = return_gateways_array(false, true);
|
1677
|
foreach ($groups as $group) {
|
1678
|
if (is_array($group['item'])) {
|
1679
|
foreach ($group['item'] as $item) {
|
1680
|
$elements = explode("|", $item);
|
1681
|
$gwname = $elements[0];
|
1682
|
if ($interface == $gateways_arr[$gwname]['interface'] &&
|
1683
|
(empty($gwgroup_name) || $gwgroup_name == $group['name'])) {
|
1684
|
unset($gateways_arr);
|
1685
|
return true;
|
1686
|
}
|
1687
|
}
|
1688
|
}
|
1689
|
}
|
1690
|
unset($gateways_arr);
|
1691
|
|
1692
|
return false;
|
1693
|
}
|
1694
|
|
1695
|
function gateway_is_gwgroup_member($name, $detail=false) {
|
1696
|
global $config;
|
1697
|
|
1698
|
if (is_array($config['gateways']['gateway_group'])) {
|
1699
|
$groups = $config['gateways']['gateway_group'];
|
1700
|
} else {
|
1701
|
return false;
|
1702
|
}
|
1703
|
|
1704
|
$members = array();
|
1705
|
foreach ($groups as $group) {
|
1706
|
if (is_array($group['item'])) {
|
1707
|
foreach ($group['item'] as $item) {
|
1708
|
list($gwname, $tier, $vipname) = explode("|", $item);
|
1709
|
if ($name == $gwname) {
|
1710
|
if ($detail) {
|
1711
|
$newitem = array();
|
1712
|
$newitem['name'] = $group['name'];
|
1713
|
$newitem['tier'] = $tier;
|
1714
|
$newitem['vipname'] = $vipname;
|
1715
|
$members[] = $newitem;
|
1716
|
} else {
|
1717
|
$members[] = $group['name'];
|
1718
|
}
|
1719
|
}
|
1720
|
}
|
1721
|
}
|
1722
|
}
|
1723
|
|
1724
|
return $members;
|
1725
|
}
|
1726
|
/*
|
1727
|
Check the proposed gateway settings to see if they are valid.
|
1728
|
$gateway_settings - the proposed array of proposed gateway settings
|
1729
|
$id - the index of the gateway proposed to be modified (otherwise "" if adding a new gateway)
|
1730
|
$parent_ip - the IP (v4 or v6) address about to be set on the corresponding interface (if any)
|
1731
|
$parent_sn - the subnet about to be set on the corresponding interface (if any)
|
1732
|
(Note: the above 2 parameters allow gateway parameters to be validated concurrently with saving
|
1733
|
an interface, before the new interface parameters are actually saved in the config.)
|
1734
|
Return completed $input_errors array if there is any problem.
|
1735
|
Otherwise return an empty $input_errors array
|
1736
|
*/
|
1737
|
function validate_gateway($gateway_settings, $id = "", $parent_ip = "", $parent_sn = "") {
|
1738
|
global $config;
|
1739
|
|
1740
|
$a_gateways = return_gateways_array(true, false, true, true);
|
1741
|
$input_errors = array();
|
1742
|
|
1743
|
/* input validation */
|
1744
|
$reqdfields = explode(" ", "name interface");
|
1745
|
$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
|
1746
|
|
1747
|
do_input_validation($gateway_settings, $reqdfields, $reqdfieldsn, $input_errors);
|
1748
|
|
1749
|
if (!isset($gateway_settings['name'])) {
|
1750
|
$input_errors[] = "A valid gateway name must be specified.";
|
1751
|
}
|
1752
|
if (!is_validaliasname($gateway_settings['name'])) {
|
1753
|
$input_errors[] = invalidaliasnamemsg($gateway_settings['name'], gettext("gateway"));
|
1754
|
} else if (isset($gateway_settings['disabled'])) {
|
1755
|
// We have a valid gateway name that the user wants to mark as disabled.
|
1756
|
// Check if the gateway name is used in any gateway group.
|
1757
|
if (is_array($config['gateways']['gateway_group'])) {
|
1758
|
foreach ($config['gateways']['gateway_group'] as $group) {
|
1759
|
foreach ($group['item'] as $item) {
|
1760
|
$items = explode("|", $item);
|
1761
|
if ($items[0] == $gateway_settings['name']) {
|
1762
|
$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']);
|
1763
|
}
|
1764
|
}
|
1765
|
}
|
1766
|
}
|
1767
|
|
1768
|
// Check if the gateway name is used in any enabled Static Route.
|
1769
|
if (is_array($config['staticroutes']['route'])) {
|
1770
|
foreach ($config['staticroutes']['route'] as $route) {
|
1771
|
if ($route['gateway'] == $gateway_settings['name']) {
|
1772
|
if (!isset($route['disabled'])) {
|
1773
|
// There is a static route that uses this gateway and is enabled (not disabled).
|
1774
|
$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']);
|
1775
|
}
|
1776
|
}
|
1777
|
}
|
1778
|
}
|
1779
|
|
1780
|
// Check if the gateway name is used by any DNS Server
|
1781
|
$dnsgw_counter = 1;
|
1782
|
while (isset($config["system"]["dns{$dnsgw_counter}gw"])) {
|
1783
|
if ($gateway_settings['name'] == $config["system"]["dns{$dnsgw_counter}gw"]) {
|
1784
|
// The user wants to disable this gateway, but there is a static route to the DNS server that refers to the gateway.
|
1785
|
$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"]);
|
1786
|
}
|
1787
|
$dnsgw_counter++;
|
1788
|
}
|
1789
|
}
|
1790
|
/* skip system gateways which have been automatically added */
|
1791
|
if (($gateway_settings['gateway'] && (!is_ipaddr($gateway_settings['gateway'])) && ($gateway_settings['attribute'] !== "system")) && ($gateway_settings['gateway'] != "dynamic")) {
|
1792
|
$input_errors[] = gettext("A valid gateway IP address must be specified.");
|
1793
|
}
|
1794
|
|
1795
|
if ($gateway_settings['gateway'] && is_ipaddr($gateway_settings['gateway'])) {
|
1796
|
if (is_ipaddrv4($gateway_settings['gateway'])) {
|
1797
|
if ($parent_ip == '') {
|
1798
|
$parent_ip = get_interface_ip($gateway_settings['interface']);
|
1799
|
$parent_sn = get_interface_subnet($gateway_settings['interface']);
|
1800
|
}
|
1801
|
if (empty($parent_ip) || empty($parent_sn)) {
|
1802
|
$input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
|
1803
|
} elseif (!isset($gateway_settings["nonlocalgateway"])) {
|
1804
|
$subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
|
1805
|
$vips = link_interface_to_vips($gateway_settings['interface']);
|
1806
|
if (is_array($vips)) {
|
1807
|
foreach ($vips as $vip) {
|
1808
|
if (!is_ipaddrv4($vip['subnet'])) {
|
1809
|
continue;
|
1810
|
}
|
1811
|
$subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
|
1812
|
}
|
1813
|
}
|
1814
|
|
1815
|
$found = false;
|
1816
|
foreach ($subnets as $subnet) {
|
1817
|
if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
|
1818
|
$found = true;
|
1819
|
break;
|
1820
|
}
|
1821
|
}
|
1822
|
|
1823
|
if ($found === false) {
|
1824
|
$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
|
1825
|
}
|
1826
|
}
|
1827
|
} else if (is_ipaddrv6($gateway_settings['gateway'])) {
|
1828
|
/* do not do a subnet match on a link local address, it's valid */
|
1829
|
if (!is_linklocal($gateway_settings['gateway'])) {
|
1830
|
if ($parent_ip == '') {
|
1831
|
$parent_ip = get_interface_ipv6($gateway_settings['interface']);
|
1832
|
$parent_sn = get_interface_subnetv6($gateway_settings['interface']);
|
1833
|
}
|
1834
|
if (empty($parent_ip) || empty($parent_sn)) {
|
1835
|
$input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
|
1836
|
} elseif (!isset($gateway_settings["nonlocalgateway"])) {
|
1837
|
$subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
|
1838
|
$vips = link_interface_to_vips($gateway_settings['interface']);
|
1839
|
if (is_array($vips)) {
|
1840
|
foreach ($vips as $vip) {
|
1841
|
if (!is_ipaddrv6($vip['subnet'])) {
|
1842
|
continue;
|
1843
|
}
|
1844
|
$subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
|
1845
|
}
|
1846
|
}
|
1847
|
|
1848
|
$found = false;
|
1849
|
foreach ($subnets as $subnet) {
|
1850
|
if (ip_in_subnet($gateway_settings['gateway'], $subnet)) {
|
1851
|
$found = true;
|
1852
|
break;
|
1853
|
}
|
1854
|
}
|
1855
|
|
1856
|
if ($found === false) {
|
1857
|
$input_errors[] = sprintf(gettext("The gateway address %s does not lie within one of the chosen interface's subnets."), $gateway_settings['gateway']);
|
1858
|
}
|
1859
|
}
|
1860
|
}
|
1861
|
}
|
1862
|
|
1863
|
if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddr'])) {
|
1864
|
if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddr']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
|
1865
|
$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
|
1866
|
}
|
1867
|
}
|
1868
|
if (!empty($config['interfaces'][$gateway_settings['interface']]['ipaddrv6'])) {
|
1869
|
if (is_ipaddr($config['interfaces'][$gateway_settings['interface']]['ipaddrv6']) && (empty($gateway_settings['gateway']) || $gateway_settings['gateway'] == "dynamic")) {
|
1870
|
$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
|
1871
|
}
|
1872
|
}
|
1873
|
}
|
1874
|
if (($gateway_settings['monitor'] != "") && ($gateway_settings['monitor'] != "dynamic")) {
|
1875
|
validateipaddr($gateway_settings['monitor'], IPV4V6, "Monitor IP", $input_errors, false);
|
1876
|
}
|
1877
|
if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] < 0) {
|
1878
|
$input_errors[] = gettext("A valid data payload must be specified.");
|
1879
|
}
|
1880
|
/* only allow correct IPv4 and IPv6 gateway addresses */
|
1881
|
if (($gateway_settings['gateway'] <> "") && is_ipaddr($gateway_settings['gateway']) && $gateway_settings['gateway'] != "dynamic") {
|
1882
|
if (is_ipaddrv6($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet")) {
|
1883
|
$input_errors[] = sprintf(gettext("The IPv6 gateway address '%s' can not be used as a IPv4 gateway."), $gateway_settings['gateway']);
|
1884
|
}
|
1885
|
if (is_ipaddrv4($gateway_settings['gateway']) && ($gateway_settings['ipprotocol'] == "inet6")) {
|
1886
|
$input_errors[] = sprintf(gettext("The IPv4 gateway address '%s' can not be used as a IPv6 gateway."), $gateway_settings['gateway']);
|
1887
|
}
|
1888
|
}
|
1889
|
/* only allow correct IPv4 and IPv6 monitor addresses */
|
1890
|
if (($gateway_settings['monitor'] <> "") && is_ipaddr($gateway_settings['monitor']) && $gateway_settings['monitor'] != "dynamic") {
|
1891
|
if (is_ipaddrv6($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet")) {
|
1892
|
$input_errors[] = sprintf(gettext("The IPv6 monitor address '%s' can not be used on a IPv4 gateway."), $gateway_settings['monitor']);
|
1893
|
}
|
1894
|
if (is_ipaddrv4($gateway_settings['monitor']) && ($gateway_settings['ipprotocol'] == "inet6")) {
|
1895
|
$input_errors[] = sprintf(gettext("The IPv4 monitor address '%s' can not be used on a IPv6 gateway."), $gateway_settings['monitor']);
|
1896
|
}
|
1897
|
}
|
1898
|
|
1899
|
if (isset($gateway_settings['name'])) {
|
1900
|
/* check for overlaps */
|
1901
|
foreach ($a_gateways as $gateway) {
|
1902
|
if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
|
1903
|
if ($gateway['name'] != $gateway_settings['name']) {
|
1904
|
$input_errors[] = gettext("Changing name on a gateway is not allowed.");
|
1905
|
}
|
1906
|
continue;
|
1907
|
}
|
1908
|
if ($gateway_settings['name'] <> "") {
|
1909
|
if (($gateway['name'] <> "") && ($gateway_settings['name'] == $gateway['name']) && ($gateway['attribute'] !== "system")) {
|
1910
|
$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $gateway_settings['name']);
|
1911
|
break;
|
1912
|
}
|
1913
|
}
|
1914
|
if (is_ipaddr($gateway_settings['gateway'])) {
|
1915
|
if (($gateway['gateway'] <> "") && ($gateway_settings['gateway'] == $gateway['gateway']) && ($gateway['attribute'] !== "system")) {
|
1916
|
$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $gateway_settings['gateway']);
|
1917
|
break;
|
1918
|
}
|
1919
|
}
|
1920
|
if (is_ipaddr($gateway_settings['monitor'])) {
|
1921
|
if (($gateway['monitor'] <> "") && ($gateway_settings['monitor'] == $gateway['monitor']) && ($gateway['attribute'] !== "system")) {
|
1922
|
$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. A different monitor IP must be chosen.'), $gateway_settings['monitor']);
|
1923
|
break;
|
1924
|
}
|
1925
|
}
|
1926
|
}
|
1927
|
}
|
1928
|
|
1929
|
/* input validation of dpinger advanced parameters */
|
1930
|
|
1931
|
$dpinger_default = return_dpinger_defaults();
|
1932
|
$latencylow = $dpinger_default['latencylow'];
|
1933
|
if ($gateway_settings['latencylow']) {
|
1934
|
if (!is_numeric($gateway_settings['latencylow'])) {
|
1935
|
$input_errors[] = gettext("The low latency threshold needs to be a numeric value.");
|
1936
|
} else if ($gateway_settings['latencylow'] < 1) {
|
1937
|
$input_errors[] = gettext("The low latency threshold needs to be positive.");
|
1938
|
} else {
|
1939
|
$latencylow = $gateway_settings['latencylow'];
|
1940
|
}
|
1941
|
}
|
1942
|
|
1943
|
$latencyhigh = $dpinger_default['latencyhigh'];
|
1944
|
if ($gateway_settings['latencyhigh']) {
|
1945
|
if (!is_numeric($gateway_settings['latencyhigh'])) {
|
1946
|
$input_errors[] = gettext("The high latency threshold needs to be a numeric value.");
|
1947
|
} else if ($gateway_settings['latencyhigh'] < 1) {
|
1948
|
$input_errors[] = gettext("The high latency threshold needs to be positive.");
|
1949
|
} else {
|
1950
|
$latencyhigh = $gateway_settings['latencyhigh'];
|
1951
|
}
|
1952
|
}
|
1953
|
|
1954
|
$losslow = $dpinger_default['losslow'];
|
1955
|
if ($gateway_settings['losslow']) {
|
1956
|
if (!is_numeric($gateway_settings['losslow'])) {
|
1957
|
$input_errors[] = gettext("The low Packet Loss threshold needs to be a numeric value.");
|
1958
|
} else if ($gateway_settings['losslow'] < 1) {
|
1959
|
$input_errors[] = gettext("The low Packet Loss threshold needs to be positive.");
|
1960
|
} else if ($gateway_settings['losslow'] >= 100) {
|
1961
|
$input_errors[] = gettext("The low Packet Loss threshold needs to be less than 100.");
|
1962
|
} else {
|
1963
|
$losslow = $gateway_settings['losslow'];
|
1964
|
}
|
1965
|
}
|
1966
|
|
1967
|
$losshigh = $dpinger_default['losshigh'];
|
1968
|
if ($gateway_settings['losshigh']) {
|
1969
|
if (!is_numeric($gateway_settings['losshigh'])) {
|
1970
|
$input_errors[] = gettext("The high Packet Loss threshold needs to be a numeric value.");
|
1971
|
} else if ($gateway_settings['losshigh'] < 1) {
|
1972
|
$input_errors[] = gettext("The high Packet Loss threshold needs to be positive.");
|
1973
|
} else if ($gateway_settings['losshigh'] > 100) {
|
1974
|
$input_errors[] = gettext("The high Packet Loss threshold needs to be 100 or less.");
|
1975
|
} else {
|
1976
|
$losshigh = $gateway_settings['losshigh'];
|
1977
|
}
|
1978
|
}
|
1979
|
|
1980
|
$time_period = $dpinger_default['time_period'];
|
1981
|
if ($gateway_settings['time_period']) {
|
1982
|
if (!is_numeric($gateway_settings['time_period'])) {
|
1983
|
$input_errors[] = gettext("The time period over which results are averaged needs to be a numeric value.");
|
1984
|
} else if ($gateway_settings['time_period'] < 1) {
|
1985
|
$input_errors[] = gettext("The time period over which results are averaged needs to be positive.");
|
1986
|
} else {
|
1987
|
$time_period = $gateway_settings['time_period'];
|
1988
|
}
|
1989
|
}
|
1990
|
|
1991
|
$interval = $dpinger_default['interval'];
|
1992
|
if ($gateway_settings['interval']) {
|
1993
|
if (!is_numeric($gateway_settings['interval'])) {
|
1994
|
$input_errors[] = gettext("The probe interval needs to be a numeric value.");
|
1995
|
} else if ($gateway_settings['interval'] < 1) {
|
1996
|
$input_errors[] = gettext("The probe interval needs to be positive.");
|
1997
|
} else {
|
1998
|
$interval = $gateway_settings['interval'];
|
1999
|
}
|
2000
|
}
|
2001
|
|
2002
|
$loss_interval = $dpinger_default['loss_interval'];
|
2003
|
if ($gateway_settings['loss_interval']) {
|
2004
|
if (!is_numeric($gateway_settings['loss_interval'])) {
|
2005
|
$input_errors[] = gettext("The loss interval needs to be a numeric value.");
|
2006
|
} else if ($gateway_settings['loss_interval'] < 1) {
|
2007
|
$input_errors[] = gettext("The loss interval setting needs to be positive.");
|
2008
|
} else {
|
2009
|
$loss_interval = $gateway_settings['loss_interval'];
|
2010
|
}
|
2011
|
}
|
2012
|
|
2013
|
$alert_interval = $dpinger_default['alert_interval'];
|
2014
|
if ($gateway_settings['alert_interval']) {
|
2015
|
if (!is_numeric($gateway_settings['alert_interval'])) {
|
2016
|
$input_errors[] = gettext("The alert interval needs to be a numeric value.");
|
2017
|
} else if ($gateway_settings['alert_interval'] < 1) {
|
2018
|
$input_errors[] = gettext("The alert interval setting needs to be positive.");
|
2019
|
} else {
|
2020
|
$alert_interval = $gateway_settings['alert_interval'];
|
2021
|
}
|
2022
|
}
|
2023
|
|
2024
|
if ($latencylow >= $latencyhigh) {
|
2025
|
$input_errors[] = gettext(
|
2026
|
"The high latency threshold needs to be greater than the low latency threshold");
|
2027
|
}
|
2028
|
|
2029
|
if ($losslow >= $losshigh) {
|
2030
|
$input_errors[] = gettext(
|
2031
|
"The high packet loss threshold needs to be higher than the low packet loss threshold");
|
2032
|
}
|
2033
|
|
2034
|
// If the loss interval is less than latencyhigh, then high latency could never be recorded
|
2035
|
// because those high latency packets would be considered as lost. So do not allow that.
|
2036
|
if ($latencyhigh > $loss_interval) {
|
2037
|
$input_errors[] = gettext("The loss interval needs to be greater than or equal to the high latency threshold.");
|
2038
|
}
|
2039
|
|
2040
|
// Ensure that the time period is greater than 2 times the probe interval plus the loss interval.
|
2041
|
if (($interval * 2 + $loss_interval) >= $time_period) {
|
2042
|
$input_errors[] = gettext("The time period needs to be greater than twice the probe interval plus the loss interval.");
|
2043
|
}
|
2044
|
|
2045
|
// There is no point recalculating the average latency and loss more often than the probe interval.
|
2046
|
// So the alert interval needs to be >= probe interval.
|
2047
|
if ($interval > $alert_interval) {
|
2048
|
$input_errors[] = gettext("The alert interval needs to be greater than or equal to the probe interval.");
|
2049
|
}
|
2050
|
|
2051
|
return $input_errors;
|
2052
|
}
|
2053
|
|
2054
|
// Save gateway settings.
|
2055
|
// $gateway_settings - the array of gateway setting parameters
|
2056
|
// $realid - the index of the gateway to be modified (otherwise "" if adding a new gateway)
|
2057
|
|
2058
|
// This function is responsible to:
|
2059
|
// Setup the gateway parameter structure from the gateway settings input parameter
|
2060
|
// Save the structure into the config
|
2061
|
// Remove any run-time settings from gateway parameters that are changed (e.g. remove routes to addresses that are changing)
|
2062
|
|
2063
|
// A subsequent "apply" step will implement the added/changed gateway.
|
2064
|
|
2065
|
function save_gateway($gateway_settings, $realid = "") {
|
2066
|
global $config;
|
2067
|
|
2068
|
init_config_arr(array('gateways', 'gateway_item'));
|
2069
|
$a_gateway_item = &$config['gateways']['gateway_item'];
|
2070
|
$reloadif = "";
|
2071
|
$gateway = array();
|
2072
|
|
2073
|
if (empty($gateway_settings['interface'])) {
|
2074
|
$gateway['interface'] = $gateway_settings['friendlyiface'];
|
2075
|
} else {
|
2076
|
$gateway['interface'] = $gateway_settings['interface'];
|
2077
|
}
|
2078
|
if (is_ipaddr($gateway_settings['gateway'])) {
|
2079
|
$gateway['gateway'] = $gateway_settings['gateway'];
|
2080
|
} else {
|
2081
|
$gateway['gateway'] = "dynamic";
|
2082
|
}
|
2083
|
$gateway['name'] = $gateway_settings['name'];
|
2084
|
$gateway['weight'] = $gateway_settings['weight'];
|
2085
|
$gateway['ipprotocol'] = $gateway_settings['ipprotocol'];
|
2086
|
if ($gateway_settings['interval']) {
|
2087
|
$gateway['interval'] = $gateway_settings['interval'];
|
2088
|
}
|
2089
|
|
2090
|
if ($gateway_settings['time_period']) {
|
2091
|
$gateway['time_period'] = $gateway_settings['time_period'];
|
2092
|
}
|
2093
|
if ($gateway_settings['alert_interval']) {
|
2094
|
$gateway['alert_interval'] = $gateway_settings['alert_interval'];
|
2095
|
}
|
2096
|
|
2097
|
$gateway['descr'] = $gateway_settings['descr'];
|
2098
|
if ($gateway_settings['monitor_disable'] == "yes") {
|
2099
|
$gateway['monitor_disable'] = true;
|
2100
|
}
|
2101
|
if ($gateway_settings['action_disable'] == "yes") {
|
2102
|
$gateway['action_disable'] = true;
|
2103
|
}
|
2104
|
if ($gateway_settings['nonlocalgateway'] == "yes") {
|
2105
|
$gateway['nonlocalgateway'] = true;
|
2106
|
}
|
2107
|
if ($gateway_settings['dpinger_dont_add_static_route'] == "yes") {
|
2108
|
$gateway['dpinger_dont_add_static_route'] = true;
|
2109
|
}
|
2110
|
if ($gateway_settings['force_down'] == "yes") {
|
2111
|
$gateway['force_down'] = true;
|
2112
|
}
|
2113
|
if (is_ipaddr($gateway_settings['monitor'])) {
|
2114
|
$gateway['monitor'] = $gateway_settings['monitor'];
|
2115
|
}
|
2116
|
if (isset($gateway_settings['data_payload']) && is_numeric($gateway_settings['data_payload']) && $gateway_settings['data_payload'] >= 0) {
|
2117
|
$gateway['data_payload'] = $gateway_settings['data_payload'];
|
2118
|
}
|
2119
|
|
2120
|
/* NOTE: If gateway ip is changed need to cleanup the old static interface route */
|
2121
|
if ($gateway_settings['monitor'] != "dynamic" &&
|
2122
|
!empty($a_gateway_item[$realid]) &&
|
2123
|
is_ipaddr($a_gateway_item[$realid]['gateway']) &&
|
2124
|
$gateway['gateway'] != $a_gateway_item[$realid]['gateway'] &&
|
2125
|
isset($a_gateway_item[$realid]["nonlocalgateway"])) {
|
2126
|
route_del($a_gateway_item[$realid]['gateway']);
|
2127
|
}
|
2128
|
|
2129
|
/* NOTE: If monitor ip is changed need to cleanup the old static route */
|
2130
|
if ($gateway_settings['monitor'] != "dynamic" &&
|
2131
|
!empty($a_gateway_item[$realid]) &&
|
2132
|
is_ipaddr($a_gateway_item[$realid]['monitor']) &&
|
2133
|
$gateway_settings['monitor'] != $a_gateway_item[$realid]['monitor'] &&
|
2134
|
$gateway['gateway'] != $a_gateway_item[$realid]['monitor']) {
|
2135
|
route_del($a_gateway_item[$realid]['monitor']);
|
2136
|
}
|
2137
|
|
2138
|
if ($gateway_settings['defaultgw'] == "yes" || $gateway_settings['defaultgw'] == "on") {
|
2139
|
// a new default gateway is being saved.
|
2140
|
$i = 0;
|
2141
|
/* remove the default gateway bits for all gateways with the same address family */
|
2142
|
if (is_array($a_gateway_item)) {
|
2143
|
foreach ($a_gateway_item as $gw) {
|
2144
|
if ($gateway['ipprotocol'] == $gw['ipprotocol']) {
|
2145
|
if ($gw['interface'] != $gateway_settings['interface'] &&
|
2146
|
($gw['name'] == $config['gateways']['defaultgw4'] || $gw['name'] == $config['gateways']['defaultgw6'])) {
|
2147
|
// remember the old default gateway interface to call with a "interface reconfigure" event.
|
2148
|
$reloadif = $gw['interface'];
|
2149
|
}
|
2150
|
}
|
2151
|
$i++;
|
2152
|
}
|
2153
|
}
|
2154
|
if ($gateway['ipprotocol'] == "inet") {
|
2155
|
$config['gateways']['defaultgw4'] = $gateway['name'];
|
2156
|
} elseif ($gateway['ipprotocol'] == "inet6") {
|
2157
|
$config['gateways']['defaultgw6'] = $gateway['name'];
|
2158
|
}
|
2159
|
}
|
2160
|
|
2161
|
if ($gateway_settings['latencylow']) {
|
2162
|
$gateway['latencylow'] = $gateway_settings['latencylow'];
|
2163
|
}
|
2164
|
if ($gateway_settings['latencyhigh']) {
|
2165
|
$gateway['latencyhigh'] = $gateway_settings['latencyhigh'];
|
2166
|
}
|
2167
|
if ($gateway_settings['losslow']) {
|
2168
|
$gateway['losslow'] = $gateway_settings['losslow'];
|
2169
|
}
|
2170
|
if ($gateway_settings['losshigh']) {
|
2171
|
$gateway['losshigh'] = $gateway_settings['losshigh'];
|
2172
|
}
|
2173
|
if ($gateway_settings['loss_interval']) {
|
2174
|
$gateway['loss_interval'] = $gateway_settings['loss_interval'];
|
2175
|
}
|
2176
|
/* when saving the manual gateway we use the attribute which has the corresponding id */
|
2177
|
if (isset($realid) && $a_gateway_item[$realid]) {
|
2178
|
$preserve_disabled = isset($a_gateway_item[$realid]['disabled']);
|
2179
|
$a_gateway_item[$realid] = $gateway;
|
2180
|
if ($preserve_disabled) {
|
2181
|
$a_gateway_item[$realid]['disabled'] = true;
|
2182
|
}
|
2183
|
} else {
|
2184
|
$a_gateway_item[] = $gateway;
|
2185
|
}
|
2186
|
gateway_set_enabled($gateway_settings['name'], !isset($gateway_settings['disabled']));
|
2187
|
|
2188
|
mark_subsystem_dirty('staticroutes');
|
2189
|
|
2190
|
write_config("Gateway settings changed");
|
2191
|
|
2192
|
if (!empty($reloadif)) {
|
2193
|
send_event("interface reconfigure {$reloadif}");
|
2194
|
}
|
2195
|
}
|
2196
|
|
2197
|
function gateway_set_enabled($name, $enabled) {
|
2198
|
global $config;
|
2199
|
if (is_array($config['gateways']['gateway_item'])) {
|
2200
|
foreach($config['gateways']['gateway_item'] as &$item) {
|
2201
|
if ($item['name'] == $name) {
|
2202
|
$gateway = &$item;
|
2203
|
}
|
2204
|
}
|
2205
|
}
|
2206
|
if (!isset($gateway)) {
|
2207
|
return;
|
2208
|
}
|
2209
|
if ($enabled) {
|
2210
|
unset($gateway['disabled']);
|
2211
|
} else {
|
2212
|
/* Check if the gateway was enabled but changed to disabled. */
|
2213
|
if (!isset($gateway['disabled'])) {
|
2214
|
/* If the disabled gateway was the default route, remove the default route */
|
2215
|
if (is_ipaddr($gateway['gateway'])) {
|
2216
|
$inet = (!is_ipaddrv4($gateway['gateway']) ? 'inet6' : 'inet');
|
2217
|
if ($inet == 'inet') {
|
2218
|
$cgw = route_get_default('inet');
|
2219
|
} else {
|
2220
|
$cgw = route_get_default('inet6');
|
2221
|
}
|
2222
|
if ($gateway['gateway'] == $cgw) {
|
2223
|
route_del("default", $inet);
|
2224
|
}
|
2225
|
}
|
2226
|
$gateway['disabled'] = true;
|
2227
|
}
|
2228
|
}
|
2229
|
}
|
2230
|
|
2231
|
function gateway_or_gwgroup_exists($name) {
|
2232
|
global $config;
|
2233
|
if (is_array($config['gateways']['gateway_item'])) {
|
2234
|
foreach($config['gateways']['gateway_item'] as $item) {
|
2235
|
if ($item['name'] == $name) {
|
2236
|
return true;
|
2237
|
}
|
2238
|
}
|
2239
|
}
|
2240
|
if (is_array($config['gateways']['gateway_group'])) {
|
2241
|
foreach($config['gateways']['gateway_group'] as $item) {
|
2242
|
if ($item['name'] == $name) {
|
2243
|
return true;
|
2244
|
}
|
2245
|
}
|
2246
|
}
|
2247
|
return false;
|
2248
|
}
|
2249
|
|
2250
|
// These two replacement functions avoid the need to call return_gateways_array() multiple times and
|
2251
|
// allow system_gateways.php to get everything it needs in a single function call
|
2252
|
function gateway_getgwtiername($gways, $idx) {
|
2253
|
global $config;
|
2254
|
|
2255
|
$result = "";
|
2256
|
$gwname = $gways[$idx]['name'];
|
2257
|
|
2258
|
$gw = get_gateway_or_group_by_name($gwname, $gways);
|
2259
|
init_config_arr(array('gateways'));
|
2260
|
if ($config['gateways']['defaultgw4'] == $gwname || $config['gateways']['defaultgw6'] == $gwname) {
|
2261
|
$result = "Default";
|
2262
|
} else {
|
2263
|
if ($gw['ipprotocol'] == 'inet') {
|
2264
|
$defgw = get_gateway_or_group_by_name($config['gateways']['defaultgw4'], $gways);
|
2265
|
} else {
|
2266
|
$defgw = get_gateway_or_group_by_name($config['gateways']['defaultgw6'], $gways);
|
2267
|
}
|
2268
|
|
2269
|
if ($defgw['type'] == "gatewaygroup") {
|
2270
|
$detail = gateway_is_gwgroup_member($gwname, true);
|
2271
|
foreach($detail as $gwitem) {
|
2272
|
if ($gwitem['name'] == $defgw['name']) {
|
2273
|
if (isset($gwitem['tier'])) {
|
2274
|
$result = "Tier " . $gwitem['tier'];
|
2275
|
break;
|
2276
|
}
|
2277
|
}
|
2278
|
}
|
2279
|
}
|
2280
|
}
|
2281
|
|
2282
|
if (!empty($result)) {
|
2283
|
if ($gw['ipprotocol'] == "inet") {
|
2284
|
$result .= " (IPv4)";
|
2285
|
} elseif ($gw['ipprotocol'] == "inet6") {
|
2286
|
$result .= " (IPv6)";
|
2287
|
}
|
2288
|
}
|
2289
|
|
2290
|
return $result;
|
2291
|
}
|
2292
|
|
2293
|
function get_gateway_or_group_by_name($gwname, $gateways_arr) {
|
2294
|
global $config;
|
2295
|
|
2296
|
foreach ($gateways_arr as $gw) {
|
2297
|
if ($gw['name'] == $gwname) {
|
2298
|
$gw['type'] = 'gateway';
|
2299
|
return $gw;
|
2300
|
}
|
2301
|
}
|
2302
|
|
2303
|
init_config_arr(array('gateways', 'gateway_group'));
|
2304
|
foreach ($config['gateways']['gateway_group'] as $gwg) {
|
2305
|
if ($gwg['name'] == $gwname) {
|
2306
|
$gwg['type'] = 'gatewaygroup';
|
2307
|
return $gwg;
|
2308
|
}
|
2309
|
}
|
2310
|
|
2311
|
return false;
|
2312
|
}
|
2313
|
|
2314
|
// Compose a list of available gateways but without the need to call return_gateways_array() multiple times
|
2315
|
// Previously that function could be called eight times per gateway!
|
2316
|
function available_default_gateways() {
|
2317
|
global $config;
|
2318
|
|
2319
|
$gways = return_gateways_array(true, false, true, true);
|
2320
|
|
2321
|
$items4 = array();
|
2322
|
$items6 = array();
|
2323
|
$items4[''] = "Automatic";
|
2324
|
$items6[''] = "Automatic";
|
2325
|
foreach($gways as $gw) {
|
2326
|
$gwn = $gw['name'];
|
2327
|
if ($gw['ipprotocol'] == "inet6") {
|
2328
|
$items6[$gwn] = $gwn;
|
2329
|
} else {
|
2330
|
$items4[$gwn] = $gwn;
|
2331
|
}
|
2332
|
}
|
2333
|
|
2334
|
$groups = return_gateway_groups_array(false, $gways);
|
2335
|
foreach ($groups as $key => $group) {
|
2336
|
$gwn = $group['descr'];
|
2337
|
if ($group['ipprotocol'] == "inet6") {
|
2338
|
$items6[$key] = "$key ($gwn)";
|
2339
|
} else {
|
2340
|
$items4[$key] = "$key ($gwn)";
|
2341
|
}
|
2342
|
}
|
2343
|
$items4['-'] = "None";
|
2344
|
$items6['-'] = "None";
|
2345
|
|
2346
|
$defaults = array();
|
2347
|
$defaults['v4'] = $items4;
|
2348
|
$defaults['v6'] = $items6;
|
2349
|
$defaults['defaultgw4'] = $config['gateways']['defaultgw4'];
|
2350
|
$defaults['defaultgw6'] = $config['gateways']['defaultgw6'];
|
2351
|
|
2352
|
return $defaults;
|
2353
|
}
|
2354
|
|
2355
|
|
2356
|
?>
|