Project

General

Profile

Download (38.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	system_gateways_edit.php
5
	part of pfSense (https://www.pfsense.org)
6

    
7
	Copyright (C) 2010 Seth Mos <seth.mos@dds.nl>.
8
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20

    
21
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32
/*
33
	pfSense_MODULE:	routing
34
*/
35

    
36
##|+PRIV
37
##|*IDENT=page-system-gateways-editgateway
38
##|*NAME=System: Gateways: Edit Gateway page
39
##|*DESCR=Allow access to the 'System: Gateways: Edit Gateway' page.
40
##|*MATCH=system_gateways_edit.php*
41
##|-PRIV
42

    
43
require("guiconfig.inc");
44
require("pkg-utils.inc");
45

    
46
if (isset($_POST['referer'])) {
47
	$referer = $_POST['referer'];
48
} else {
49
	$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/system_gateways.php');
50
}
51

    
52
$a_gateways = return_gateways_array(true, false, true);
53
$a_gateways_arr = array();
54
foreach($a_gateways as $gw) {
55
	$a_gateways_arr[] = $gw;
56
}
57
$a_gateways = $a_gateways_arr;
58

    
59
if (!is_array($config['gateways']['gateway_item']))
60
	$config['gateways']['gateway_item'] = array();
61

    
62
$a_gateway_item = &$config['gateways']['gateway_item'];
63
$apinger_default = return_apinger_defaults();
64

    
65
if (is_numericint($_GET['id']))
66
	$id = $_GET['id'];
67
if (isset($_POST['id']) && is_numericint($_POST['id']))
68
	$id = $_POST['id'];
69

    
70
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
71
	$id = $_GET['dup'];
72

    
73
if (isset($id) && $a_gateways[$id]) {
74
	$pconfig = array();
75
	$pconfig['name'] = $a_gateways[$id]['name'];
76
	$pconfig['weight'] = $a_gateways[$id]['weight'];
77
	$pconfig['interval'] = $a_gateways[$id]['interval'];
78
	$pconfig['avg_delay_samples'] = $a_gateways[$id]['avg_delay_samples'];
79
	$pconfig['avg_delay_samples_calculated'] = isset($a_gateways[$id]['avg_delay_samples_calculated']);
80
	$pconfig['avg_loss_samples'] = $a_gateways[$id]['avg_loss_samples'];
81
	$pconfig['avg_loss_samples_calculated'] = isset($a_gateways[$id]['avg_loss_samples_calculated']);
82
	$pconfig['avg_loss_delay_samples'] = $a_gateways[$id]['avg_loss_delay_samples'];
83
	$pconfig['avg_loss_delay_samples_calculated'] = isset($a_gateways[$id]['avg_loss_delay_samples_calculated']);
84
	$pconfig['interface'] = $a_gateways[$id]['interface'];
85
	$pconfig['friendlyiface'] = $a_gateways[$id]['friendlyiface'];
86
	$pconfig['ipprotocol'] = $a_gateways[$id]['ipprotocol'];
87
	if (isset($a_gateways[$id]['dynamic']))
88
		$pconfig['dynamic'] = true;
89
	$pconfig['gateway'] = $a_gateways[$id]['gateway'];
90
	$pconfig['defaultgw'] = isset($a_gateways[$id]['defaultgw']);
91
	$pconfig['force_down'] = isset($a_gateways[$id]['force_down']);
92
	$pconfig['latencylow'] = $a_gateways[$id]['latencylow'];
93
	$pconfig['latencyhigh'] = $a_gateways[$id]['latencyhigh'];
94
	$pconfig['losslow'] = $a_gateways[$id]['losslow'];
95
	$pconfig['losshigh'] = $a_gateways[$id]['losshigh'];
96
	$pconfig['down'] = $a_gateways[$id]['down'];
97
	$pconfig['monitor'] = $a_gateways[$id]['monitor'];
98
	$pconfig['monitor_disable'] = isset($a_gateways[$id]['monitor_disable']);
99
	$pconfig['descr'] = $a_gateways[$id]['descr'];
100
	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
101
	$pconfig['disabled'] = isset($a_gateways[$id]['disabled']);
102
}
103

    
104
if (isset($_GET['dup']) && is_numericint($_GET['dup'])) {
105
	unset($id);
106
	unset($pconfig['attribute']);
107
}
108

    
109
if (isset($id) && $a_gateways[$id])
110
	$realid = $a_gateways[$id]['attribute'];
111

    
112
if ($_POST) {
113

    
114
	unset($input_errors);
115

    
116
	/* input validation */
117
	$reqdfields = explode(" ", "name interface");
118
	$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
119

    
120
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
121

    
122
	if (! isset($_POST['name'])) {
123
		$input_errors[] = "A valid gateway name must be specified.";
124
	}
125
	if (! is_validaliasname($_POST['name'])) {
126
		$input_errors[] = gettext("The gateway name must not contain invalid characters.");
127
	}
128
	/* skip system gateways which have been automatically added */
129
	if (($_POST['gateway'] && (!is_ipaddr($_POST['gateway'])) && ($_POST['attribute'] !== "system")) && ($_POST['gateway'] != "dynamic")) {
130
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
131
	}
132

    
133
	if ($_POST['gateway'] && (is_ipaddr($_POST['gateway'])) && !$_REQUEST['isAjax']) {
134
		if(is_ipaddrv4($_POST['gateway'])) {
135
			$parent_ip = get_interface_ip($_POST['interface']);
136
			$parent_sn = get_interface_subnet($_POST['interface']);
137
			if(empty($parent_ip) || empty($parent_sn)) {
138
				$input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
139
			} else {
140
				$subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
141
				$vips = link_interface_to_vips($_POST['interface']);
142
				if (is_array($vips))
143
					foreach($vips as $vip) {
144
						if (!is_ipaddrv4($vip['subnet']))
145
							continue;
146
						$subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
147
					}
148

    
149
				$found = false;
150
				foreach($subnets as $subnet)
151
					if(ip_in_subnet($_POST['gateway'], $subnet)) {
152
						$found = true;
153
						break;
154
					}
155

    
156
				if ($found === false)
157
					$input_errors[] = sprintf(gettext("The gateway address %1\$s does not lie within one of the chosen interface's subnets."), $_POST['gateway']);
158
			}
159
		}
160
		else if(is_ipaddrv6($_POST['gateway'])) {
161
			/* do not do a subnet match on a link local address, it's valid */
162
			if(!is_linklocal($_POST['gateway'])) {
163
				$parent_ip = get_interface_ipv6($_POST['interface']);
164
				$parent_sn = get_interface_subnetv6($_POST['interface']);
165
				if(empty($parent_ip) || empty($parent_sn)) {
166
					$input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
167
				} else {
168
					$subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
169
					$vips = link_interface_to_vips($_POST['interface']);
170
					if (is_array($vips))
171
						foreach($vips as $vip) {
172
							if (!is_ipaddrv6($vip['subnet']))
173
								continue;
174
							$subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
175
						}
176

    
177
					$found = false;
178
					foreach($subnets as $subnet)
179
						if(ip_in_subnet($_POST['gateway'], $subnet)) {
180
							$found = true;
181
							break;
182
						}
183

    
184
					if ($found === false)
185
						$input_errors[] = sprintf(gettext("The gateway address %1\$s does not lie within one of the chosen interface's subnets."), $_POST['gateway']);
186
				}
187
			}
188
		}
189

    
190
		if (!empty($config['interfaces'][$_POST['interface']]['ipaddr'])) {
191
			if (is_ipaddr($config['interfaces'][$_POST['interface']]['ipaddr']) && (empty($_POST['gateway']) || $_POST['gateway'] == "dynamic"))
192
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
193
		}
194
		if (!empty($config['interfaces'][$_POST['interface']]['ipaddrv6'])) {
195
			if (is_ipaddr($config['interfaces'][$_POST['interface']]['ipaddrv6']) && (empty($_POST['gateway']) || $_POST['gateway'] == "dynamic"))
196
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
197
		}
198
	}
199
	if (($_POST['monitor'] <> "") && !is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
200
		$input_errors[] = gettext("A valid monitor IP address must be specified.");
201
	}
202
	/* only allow correct IPv4 and IPv6 gateway addresses */
203
	if (($_POST['gateway'] <> "") && is_ipaddr($_POST['gateway']) && $_POST['gateway'] != "dynamic") {
204
		if(is_ipaddrv6($_POST['gateway']) && ($_POST['ipprotocol'] == "inet")) {
205
			$input_errors[] = gettext("The IPv6 gateway address '{$_POST['gateway']}' can not be used as a IPv4 gateway'.");
206
		}
207
		if(is_ipaddrv4($_POST['gateway']) && ($_POST['ipprotocol'] == "inet6")) {
208
			$input_errors[] = gettext("The IPv4 gateway address '{$_POST['gateway']}' can not be used as a IPv6 gateway'.");
209
		}
210
	}
211
	/* only allow correct IPv4 and IPv6 monitor addresses */
212
	if (($_POST['monitor'] <> "") && is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
213
		if(is_ipaddrv6($_POST['monitor']) && ($_POST['ipprotocol'] == "inet")) {
214
			$input_errors[] = gettext("The IPv6 monitor address '{$_POST['monitor']}' can not be used on a IPv4 gateway'.");
215
		}
216
		if(is_ipaddrv4($_POST['monitor']) && ($_POST['ipprotocol'] == "inet6")) {
217
			$input_errors[] = gettext("The IPv4 monitor address '{$_POST['monitor']}' can not be used on a IPv6 gateway'.");
218
		}
219
	}
220

    
221
	if (isset($_POST['name'])) {
222
		/* check for overlaps */
223
		foreach ($a_gateways as $gateway) {
224
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
225
				if ($gateway['name'] != $_POST['name'])
226
					$input_errors[] = gettext("Changing name on a gateway is not allowed.");
227
				continue;
228
			}
229
			if($_POST['name'] <> "") {
230
				if (($gateway['name'] <> "") && ($_POST['name'] == $gateway['name']) && ($gateway['attribute'] !== "system")) {
231
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $_POST['name']);
232
					break;
233
				}
234
			}
235
			if(is_ipaddr($_POST['gateway'])) {
236
				if (($gateway['gateway'] <> "") && ($_POST['gateway'] == $gateway['gateway']) && ($gateway['attribute'] !== "system")) {
237
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $_POST['gateway']);
238
					break;
239
				}
240
			}
241
			if(is_ipaddr($_POST['monitor'])) {
242
				if (($gateway['monitor'] <> "") && ($_POST['monitor'] == $gateway['monitor']) && ($gateway['attribute'] !== "system")) {
243
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. You must choose a different monitor IP.'), $_POST['monitor']);
244
					break;
245
				}
246
			}
247
		}
248
	}
249

    
250
	/* input validation of apinger advanced parameters */
251
	if($_POST['latencylow']) {
252
		if (! is_numeric($_POST['latencylow'])) {
253
			$input_errors[] = gettext("The low latency threshold needs to be a numeric value.");
254
		} else {
255
			if ($_POST['latencylow'] < 1) {
256
				$input_errors[] = gettext("The low latency threshold needs to be positive.");
257
			}
258
		}
259
	}
260

    
261
	if($_POST['latencyhigh']) {
262
		if (! is_numeric($_POST['latencyhigh'])) {
263
			$input_errors[] = gettext("The high latency threshold needs to be a numeric value.");
264
		} else {
265
			if ($_POST['latencyhigh'] < 1) {
266
				$input_errors[] = gettext("The high latency threshold needs to be positive.");
267
			}
268
		}
269
	}
270

    
271
	if($_POST['losslow']) {
272
		if (! is_numeric($_POST['losslow'])) {
273
			$input_errors[] = gettext("The low Packet Loss threshold needs to be a numeric value.");
274
		} else {
275
			if ($_POST['losslow'] < 1) {
276
				$input_errors[] = gettext("The low Packet Loss threshold needs to be positive.");
277
			}
278
			if ($_POST['losslow'] >= 100) {
279
				$input_errors[] = gettext("The low Packet Loss threshold needs to be less than 100.");
280
			}
281
		}
282
	}
283

    
284
	if($_POST['losshigh']) {
285
		if (! is_numeric($_POST['losshigh'])) {
286
			$input_errors[] = gettext("The high Packet Loss threshold needs to be a numeric value.");
287
		} else {
288
			if ($_POST['losshigh'] < 1) {
289
				$input_errors[] = gettext("The high Packet Loss threshold needs to be positive.");
290
			}
291
			if ($_POST['losshigh'] > 100) {
292
				$input_errors[] = gettext("The high Packet Loss threshold needs to be 100 or less.");
293
			}
294
		}
295
	}
296

    
297
	if(($_POST['latencylow']) && ($_POST['latencyhigh'])) {
298
		if ((is_numeric($_POST['latencylow'])) && (is_numeric($_POST['latencyhigh']))) {
299
			if(($_POST['latencylow'] > $_POST['latencyhigh'])) {
300
				$input_errors[] = gettext("The high latency threshold needs to be higher than the low latency threshold");
301
			}
302
		}
303
	} else {
304
		if($_POST['latencylow']){
305
			if (is_numeric($_POST['latencylow'])) {
306
				if($_POST['latencylow'] > $apinger_default['latencyhigh']) {
307
					$input_errors[] = gettext(sprintf("The low latency threshold needs to be less than the default high latency threshold (%d)", $apinger_default['latencyhigh']));
308
				}
309
			}
310
		}
311
		if($_POST['latencyhigh']){
312
			if (is_numeric($_POST['latencyhigh'])) {
313
				if($_POST['latencyhigh'] < $apinger_default['latencylow']) {
314
					$input_errors[] = gettext(sprintf("The high latency threshold needs to be higher than the default low latency threshold (%d)", $apinger_default['latencylow']));
315
				}
316
			}
317
		}
318
	}
319

    
320
	if(($_POST['losslow']) && ($_POST['losshigh'])){
321
		if ((is_numeric($_POST['losslow'])) && (is_numeric($_POST['losshigh']))) {
322
			if($_POST['losslow'] > $_POST['losshigh']) {
323
				$input_errors[] = gettext("The high Packet Loss threshold needs to be higher than the low Packet Loss threshold");
324
			}
325
		}
326
	} else {
327
		if($_POST['losslow']){
328
			if (is_numeric($_POST['losslow'])) {
329
				if($_POST['losslow'] > $apinger_default['losshigh']) {
330
					$input_errors[] = gettext(sprintf("The low Packet Loss threshold needs to be less than the default high Packet Loss threshold (%d)", $apinger_default['losshigh']));
331
				}
332
			}
333
		}
334
		if($_POST['losshigh']){
335
			if (is_numeric($_POST['losshigh'])) {
336
				if($_POST['losshigh'] < $apinger_default['losslow']) {
337
					$input_errors[] = gettext(sprintf("The high Packet Loss threshold needs to be higher than the default low Packet Loss threshold (%d)", $apinger_default['losslow']));
338
				}
339
			}
340
		}
341
	}
342

    
343
	if($_POST['interval']) {
344
		if (! is_numeric($_POST['interval'])) {
345
			$input_errors[] = gettext("The probe interval needs to be a numeric value.");
346
		} else {
347
			if ($_POST['interval'] < 1) {
348
				$input_errors[] = gettext("The probe interval needs to be positive.");
349
			}
350
		}
351
	}
352

    
353
	if($_POST['down']) {
354
		if (! is_numeric($_POST['down'])) {
355
			$input_errors[] = gettext("The down time setting needs to be a numeric value.");
356
		} else {
357
			if ($_POST['down'] < 1) {
358
				$input_errors[] = gettext("The down time setting needs to be positive.");
359
			}
360
		}
361
	}
362

    
363
	if(($_POST['interval']) && ($_POST['down'])){
364
		if ((is_numeric($_POST['interval'])) && (is_numeric($_POST['down']))) {
365
			if($_POST['interval'] > $_POST['down']) {
366
				$input_errors[] = gettext("The probe interval needs to be less than the down time setting.");
367
			}
368
		}
369
	} else {
370
		if($_POST['interval']){
371
			if (is_numeric($_POST['interval'])) {
372
				if($_POST['interval'] > $apinger_default['down']) {
373
					$input_errors[] = gettext(sprintf("The probe interval needs to be less than the default down time setting (%d)", $apinger_default['down']));
374
				}
375
			}
376
		}
377
		if($_POST['down']){
378
			if (is_numeric($_POST['down'])) {
379
				if($_POST['down'] < $apinger_default['interval']) {
380
					$input_errors[] = gettext(sprintf("The down time setting needs to be higher than the default probe interval (%d)", $apinger_default['interval']));
381
				}
382
			}
383
		}
384
	}
385

    
386
	if($_POST['avg_delay_samples']) {
387
		if (! is_numeric($_POST['avg_delay_samples'])) {
388
			$input_errors[] = gettext("The average delay replies qty needs to be a numeric value.");
389
		} else {
390
			if ($_POST['avg_delay_samples'] < 1) {
391
				$input_errors[] = gettext("The average delay replies qty needs to be positive.");
392
			}
393
		}
394
	}
395

    
396
	if($_POST['avg_loss_samples']) {
397
		if (! is_numeric($_POST['avg_loss_samples'])) {
398
			$input_errors[] = gettext("The average packet loss probes qty needs to be a numeric value.");
399
		} else {
400
			if ($_POST['avg_loss_samples'] < 1) {
401
				$input_errors[] = gettext("The average packet loss probes qty needs to be positive.");
402
			}
403
		}
404
	}
405

    
406
	if($_POST['avg_loss_delay_samples']) {
407
		if (! is_numeric($_POST['avg_loss_delay_samples'])) {
408
			$input_errors[] = gettext("The lost probe delay needs to be a numeric value.");
409
		} else {
410
			if ($_POST['avg_loss_delay_samples'] < 1) {
411
				$input_errors[] = gettext("The lost probe delay needs to be positive.");
412
			}
413
		}
414
	}
415

    
416
	if (!$input_errors) {
417
		$reloadif = "";
418
		$gateway = array();
419

    
420
		if (empty($_POST['interface']))
421
			$gateway['interface'] = $pconfig['friendlyiface'];
422
		else
423
			$gateway['interface'] = $_POST['interface'];
424
		if (is_ipaddr($_POST['gateway']))
425
			$gateway['gateway'] = $_POST['gateway'];
426
		else
427
			$gateway['gateway'] = "dynamic";
428
		$gateway['name'] = $_POST['name'];
429
		$gateway['weight'] = $_POST['weight'];
430
		$gateway['ipprotocol'] = $_POST['ipprotocol'];
431
		$gateway['interval'] = $_POST['interval'];
432

    
433
		$gateway['avg_delay_samples'] = $_POST['avg_delay_samples'];
434
		if ($_POST['avg_delay_samples_calculated'] == "yes" || $_POST['avg_delay_samples_calculated'] == "on")
435
			$gateway['avg_delay_samples_calculated'] = true;
436

    
437
		$gateway['avg_loss_samples'] = $_POST['avg_loss_samples'];
438
		if ($_POST['avg_loss_samples_calculated'] == "yes" || $_POST['avg_loss_samples_calculated'] == "on")
439
			$gateway['avg_loss_samples_calculated'] = true;
440

    
441
		$gateway['avg_loss_delay_samples'] = $_POST['avg_loss_delay_samples'];
442
		if ($_POST['avg_loss_delay_samples_calculated'] == "yes" || $_POST['avg_loss_delay_samples_calculated'] == "on")
443
			$gateway['avg_loss_delay_samples_calculated'] = true;
444

    
445
		$gateway['descr'] = $_POST['descr'];
446
		if ($_POST['monitor_disable'] == "yes")
447
			$gateway['monitor_disable'] = true;
448
		if ($_POST['force_down'] == "yes")
449
			$gateway['force_down'] = true;
450
		if (is_ipaddr($_POST['monitor']))
451
			$gateway['monitor'] = $_POST['monitor'];
452

    
453
		/* NOTE: If monitor ip is changed need to cleanup the old static route */
454
		if ($_POST['monitor'] != "dynamic" && !empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['monitor']) &&
455
		    $_POST['monitor'] != $a_gateway_item[$realid]['monitor'] && $gateway['gateway'] != $a_gateway_item[$realid]['monitor']) {
456
			if (is_ipaddrv4($a_gateway_item[$realid]['monitor']))
457
				mwexec("/sbin/route delete " . escapeshellarg($a_gateway_item[$realid]['monitor']));
458
			else
459
				mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateway_item[$realid]['monitor']));
460
		}
461

    
462
		if ($_POST['defaultgw'] == "yes" || $_POST['defaultgw'] == "on") {
463
			$i = 0;
464
			/* remove the default gateway bits for all gateways with the same address family */
465
			foreach($a_gateway_item as $gw) {
466
				if ($gateway['ipprotocol'] == $gw['ipprotocol']) {
467
					unset($config['gateways']['gateway_item'][$i]['defaultgw']);
468
					if ($gw['interface'] != $_POST['interface'] && $gw['defaultgw'])
469
						$reloadif = $gw['interface'];
470
				}
471
				$i++;
472
			}
473
			$gateway['defaultgw'] = true;
474
		}
475

    
476
		if ($_POST['latencylow'])
477
			$gateway['latencylow'] = $_POST['latencylow'];
478
		if ($_POST['latencyhigh'])
479
			$gateway['latencyhigh'] = $_POST['latencyhigh'];
480
		if ($_POST['losslow'])
481
			$gateway['losslow'] = $_POST['losslow'];
482
		if ($_POST['losshigh'])
483
			$gateway['losshigh'] = $_POST['losshigh'];
484
		if ($_POST['down'])
485
			$gateway['down'] = $_POST['down'];
486

    
487
		if(isset($_POST['disabled']))
488
			$gateway['disabled'] = true;
489
		else
490
			unset($gateway['disabled']);
491

    
492
		/* when saving the manual gateway we use the attribute which has the corresponding id */
493
		if (isset($realid) && $a_gateway_item[$realid])
494
			$a_gateway_item[$realid] = $gateway;
495
		else
496
			$a_gateway_item[] = $gateway;
497

    
498
		mark_subsystem_dirty('staticroutes');
499

    
500
		write_config();
501

    
502
		if($_REQUEST['isAjax']) {
503
			echo $_POST['name'];
504
			exit;
505
		} else if (!empty($reloadif))
506
			send_event("interface reconfigure {$reloadif}");
507

    
508
		header("Location: system_gateways.php");
509
		exit;
510
	} else {
511
		if ($_REQUEST['isAjax']) {
512
			header("HTTP/1.0 500 Internal Server Error");
513
			header("Content-type: text/plain");
514
			foreach ($input_errors as $error) {
515
				echo("$error\n");
516
			}
517
			exit;
518
		}
519

    
520
		$pconfig = $_POST;
521
		if (empty($_POST['friendlyiface']))
522
			$pconfig['friendlyiface'] = $_POST['interface'];
523
	}
524
}
525

    
526

    
527
$pgtitle = array(gettext("System"),gettext("Gateways"),gettext("Edit gateway"));
528
$shortcut_section = "gateways";
529

    
530
include("head.inc");
531

    
532
?>
533

    
534
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
535
<?php include("fbegin.inc"); ?>
536
<script type="text/javascript">
537
//<![CDATA[
538
function show_advanced_gateway() {
539
	document.getElementById("showadvgatewaybox").innerHTML='';
540
	aodiv = document.getElementById('showgatewayadv');
541
	aodiv.style.display = "block";
542
}
543
function monitor_change() {
544
	document.iform.monitor.disabled = document.iform.monitor_disable.checked;
545
}
546

    
547
function interval_change(interval_obj) {
548
	valid_value(interval_obj, 1, 86400);
549
	calculate_state_change();
550

    
551
	calculated_change(document.iform.avg_delay_samples_calculated, document.iform.avg_delay_samples);
552
	calculated_change(document.iform.avg_loss_samples_calculated, document.iform.avg_loss_samples);
553
	calculated_change(document.iform.avg_loss_delay_samples_calculated, document.iform.avg_loss_delay_samples);
554
}
555

    
556
function samples_change(calculated_obj, samples_obj) {
557
	calculated_change(calculated_obj, samples_obj);
558
}
559

    
560
function calculated_change(calculated_obj, samples_obj) {
561
	switch (samples_obj.name) {
562

    
563
	case 'avg_delay_samples':
564
		// How many replies should be used to compute average delay 
565
		// for controlling "delay" alarms.
566
		// Calculate a reasonable value based on gateway probe interval and RRD 1 minute average graph step size (60).
567
		if (calculated_obj.checked && (document.iform.interval.value > 0))
568
			samples_obj.value = 60 * (1/6) / Math.pow(document.iform.interval.value, 0.333);	// Calculate & Round to Integer
569
		valid_value(samples_obj, 1, 100);
570
		break;
571

    
572
	case 'avg_loss_samples':
573
		// How many probes should be used to compute average loss.
574
		// Calculate a reasonable value based on gateway probe interval and RRD 1 minute average graph step size (60).
575
		if (calculated_obj.checked && (document.iform.interval.value > 0))
576
			samples_obj.value = 60 / document.iform.interval.value;	// Calculate & Round to Integer
577
		valid_value(samples_obj, 1, 1000);
578
		break;
579

    
580
	case 'avg_loss_delay_samples':
581
		// The delay (in samples) after which loss is computed
582
		// without this delays larger than interval would be treated as loss.
583
		// Calculate a reasonable value based on gateway probe interval and RRD 1 minute average graph step size (60).
584
		if (calculated_obj.checked && (document.iform.interval.value > 0))
585
			samples_obj.value = 60 * (1/3) / document.iform.interval.value;	// Calculate & Round to Integer
586
		valid_value(samples_obj, 1, 200);
587
		break;
588
	default:
589
	}
590

    
591
	calculate_state_change();
592
}
593

    
594
function valid_value(object, min, max) {
595
	if (object.value) {
596
		object.value = Math.round(object.value);		// Round to integer
597
		if (object.value < min)  object.value = min;	// Min Value
598
		if (object.value > max)  object.value = max;	// Max Value
599
		if (isNaN(object.value)) object.value =  '';	// Empty Value
600
	}
601
}
602

    
603
function calculate_state_change() {
604
	if (document.iform.interval.value > 0) {
605
		document.iform.avg_delay_samples_calculated.disabled = false;
606
		document.iform.avg_loss_samples_calculated.disabled = false;
607
		document.iform.avg_loss_delay_samples_calculated.disabled = false;
608

    
609
		document.iform.avg_delay_samples.disabled = document.iform.avg_delay_samples_calculated.checked;
610
		document.iform.avg_loss_samples.disabled = document.iform.avg_loss_samples_calculated.checked;
611
		document.iform.avg_loss_delay_samples.disabled = document.iform.avg_loss_delay_samples_calculated.checked;
612
	}
613
	else {
614
		document.iform.avg_delay_samples_calculated.disabled = true;
615
		document.iform.avg_loss_samples_calculated.disabled = true;
616
		document.iform.avg_loss_delay_samples_calculated.disabled = true;
617
		document.iform.interval.value = '';
618

    
619
		document.iform.avg_delay_samples.disabled = false;
620
		document.iform.avg_loss_samples.disabled = false;
621
		document.iform.avg_loss_delay_samples.disabled = false;
622

    
623
		document.iform.avg_delay_samples_calculated.checked = false;
624
		document.iform.avg_loss_samples_calculated.checked = false;
625
		document.iform.avg_loss_delay_samples_calculated.checked = false;
626
	}
627
}
628

    
629
function enable_change() {
630
	document.iform.avg_delay_samples.disabled = false;
631
	document.iform.avg_loss_samples.disabled = false;
632
	document.iform.avg_loss_delay_samples.disabled = false;
633
}
634
//]]>
635
</script>
636
<?php if ($input_errors) print_input_errors($input_errors); ?>
637
	<form action="system_gateways_edit.php" method="post" name="iform" id="iform">
638
	<?php
639

    
640
	/* If this is a system gateway we need this var */
641
	if(($pconfig['attribute'] == "system") || is_numeric($pconfig['attribute'])) {
642
		echo "<input type='hidden' name='attribute' id='attribute' value=\"" . htmlspecialchars($pconfig['attribute']) . "\" />\n";
643
	}
644
	echo "<input type='hidden' name='friendlyiface' id='friendlyiface' value=\"" . htmlspecialchars($pconfig['friendlyiface']) . "\" />\n";
645
	?>
646
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="system gateways edit">
647
			<tr>
648
				<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit gateway"); ?></td>
649
			</tr>
650
			<tr>
651
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Disabled");?></td>
652
				<td width="78%" class="vtable">
653
					<input name="disabled" type="checkbox" id="disabled" value="yes" <?php if ($pconfig['disabled']) echo "checked=\"checked\""; ?> />
654
					<strong><?=gettext("Disable this gateway");?></strong><br />
655
					<span class="vexpl"><?=gettext("Set this option to disable this gateway without removing it from the list.");?></span>
656
				</td>
657
			</tr>
658
			<tr>
659
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Interface"); ?></td>
660
				<td width="78%" class="vtable">
661
					<select name='interface' class='formselect'>
662
					<?php
663
						$interfaces = get_configured_interface_with_descr(false, true);
664
						foreach ($interfaces as $iface => $ifacename) {
665
							echo "<option value=\"{$iface}\"";
666
							if ($iface == $pconfig['friendlyiface'])
667
								echo " selected='selected'";
668
							echo ">" . htmlspecialchars($ifacename) . "</option>";
669
						}
670
					?>
671
					</select><br />
672
					<span class="vexpl"><?=gettext("Choose which interface this gateway applies to."); ?></span>
673
				</td>
674
			</tr>
675
			<tr>
676
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Address Family"); ?></td>
677
				<td width="78%" class="vtable">
678
					<select name='ipprotocol' class='formselect' >
679
					<?php
680
						$options = array("inet" => "IPv4", "inet6" => "IPv6");
681
						foreach ($options as $name => $string) {
682
							echo "<option value=\"{$name}\"";
683
							if ($name == $pconfig['ipprotocol'])
684
								echo " selected='selected'";
685
							echo ">" . htmlspecialchars($string) . "</option>\n";
686
						}
687
					?>
688
					</select><br />
689
					<span class="vexpl"><?=gettext("Choose the Internet Protocol this gateway uses."); ?></span>
690
				</td>
691
			</tr>
692
			<tr>
693
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Name"); ?></td>
694
				<td width="78%" class="vtable">
695
					<input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>" />
696
					<br /><span class="vexpl"><?=gettext("Gateway name"); ?></span>
697
				</td>
698
			</tr>
699
			<tr>
700
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
701
				<td width="78%" class="vtable">
702
					<input name="gateway" type="text" class="formfld host" id="gateway" size="28" value="<?php if ($pconfig['dynamic']) echo "dynamic"; else echo htmlspecialchars($pconfig['gateway']); ?>" />
703
					<br /><span class="vexpl"><?=gettext("Gateway IP address"); ?></span>
704
				</td>
705
			</tr>
706
			<tr>
707
				<td width="22%" valign="top" class="vncell"><?=gettext("Default Gateway"); ?></td>
708
				<td width="78%" class="vtable">
709
					<input name="defaultgw" type="checkbox" id="defaultgw" value="yes" <?php if ($pconfig['defaultgw'] == true) echo "checked=\"checked\""; ?> />
710
					<strong><?=gettext("Default Gateway"); ?></strong><br />
711
					<?=gettext("This will select the above gateway as the default gateway"); ?>
712
				</td>
713
			</tr>
714
			<tr>
715
				<td width="22%" valign="top" class="vncell"><?=gettext("Disable Gateway Monitoring"); ?></td>
716
				<td width="78%" class="vtable">
717
					<input name="monitor_disable" type="checkbox" id="monitor_disable" value="yes" <?php if ($pconfig['monitor_disable'] == true) echo "checked=\"checked\""; ?> onclick="monitor_change()" />
718
					<strong><?=gettext("Disable Gateway Monitoring"); ?></strong><br />
719
					<?=gettext("This will consider this gateway as always being up"); ?>
720
				</td>
721
			</tr>
722
			<tr>
723
				<td width="22%" valign="top" class="vncell"><?=gettext("Monitor IP"); ?></td>
724
				<td width="78%" class="vtable">
725
					<?php
726
						if ($pconfig['gateway'] == $pconfig['monitor'])
727
							$monitor = "";
728
						else
729
							$monitor = htmlspecialchars($pconfig['monitor']);
730
					?>
731
					<input name="monitor" type="text" id="monitor" value="<?php echo htmlspecialchars($monitor); ?>" size="28" />
732
					<strong><?=gettext("Alternative monitor IP"); ?></strong> <br />
733
					<?=gettext("Enter an alternative address here to be used to monitor the link. This is used for the " .
734
					"quality RRD graphs as well as the load balancer entries. Use this if the gateway does not respond " .
735
					"to ICMP echo requests (pings)"); ?>.
736
					<br />
737
				</td>
738
			</tr>
739
			<tr>
740
				<td width="22%" valign="top" class="vncell"><?=gettext("Mark Gateway as Down"); ?></td>
741
				<td width="78%" class="vtable">
742
					<input name="force_down" type="checkbox" id="force_down" value="yes" <?php if ($pconfig['force_down'] == true) echo "checked=\"checked\""; ?> />
743
					<strong><?=gettext("Mark Gateway as Down"); ?></strong><br />
744
					<?=gettext("This will force this gateway to be considered Down"); ?>
745
				</td>
746
			</tr>
747
			<tr>
748
				<td width="22%" valign="top" class="vncell"><?=gettext("Advanced");?></td>
749
				<td width="78%" class="vtable">
750
					<?php $showbutton = (!empty($pconfig['latencylow']) || !empty($pconfig['latencyhigh']) || !empty($pconfig['losslow']) || !empty($pconfig['losshigh']) || (isset($pconfig['weight']) && $pconfig['weight'] > 1) || (isset($pconfig['interval']) && ($pconfig['interval'] > $apinger_default['interval'])) || (isset($pconfig['down']) && !($pconfig['down'] == $apinger_default['down']))); ?>
751
					<div id="showadvgatewaybox" <?php if ($showbutton) echo "style='display:none'"; ?>>
752
						<input type="button" onclick="show_advanced_gateway()" value="Advanced" /><?=gettext(" - Show advanced option"); ?>
753
					</div>
754
					<div id="showgatewayadv" <?php if (!$showbutton) echo "style='display:none'"; ?>>
755
						<table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="6" summary="advanced options">
756
							<tr>
757
								<td width="22%" valign="top" class="vncellreq"><?=gettext("Weight");?></td>
758
								<td width="78%" class="vtable">
759
									<select name='weight' class='formfldselect' id='weight'>
760
									<?php
761
										for ($i = 1; $i < 6; $i++) {
762
											$selected = "";
763
											if ($pconfig['weight'] == $i)
764
												$selected = "selected='selected'";
765
											echo "<option value='{$i}' {$selected} >{$i}</option>";
766
										}
767
									?>
768
									</select>
769
									<br /><?=gettext("Weight for this gateway when used in a Gateway Group.");?> <br />
770
								</td>
771
							</tr>
772
							<tr>
773
								<td width="22%" valign="top" class="vncellreq"><?=gettext("Latency thresholds");?></td>
774
								<td width="78%" class="vtable">
775
									<?=gettext("From");?>
776
									<input name="latencylow" type="text" class="formfld unknown" id="latencylow" size="2"
777
										value="<?=htmlspecialchars($pconfig['latencylow']);?>" />
778
									<?=gettext("To");?>
779
									<input name="latencyhigh" type="text" class="formfld unknown" id="latencyhigh" size="2"
780
										value="<?=htmlspecialchars($pconfig['latencyhigh']);?>" />
781
									<br /><span class="vexpl"><?=gettext(sprintf("Low and high thresholds for latency in milliseconds. Default is %d/%d.", $apinger_default['latencylow'], $apinger_default['latencyhigh']));?></span>
782
								</td>
783
							</tr>
784
							<tr>
785
								<td width="22%" valign="top" class="vncellreq"><?=gettext("Packet Loss thresholds");?></td>
786
								<td width="78%" class="vtable">
787
									<?=gettext("From");?>
788
									<input name="losslow" type="text" class="formfld unknown" id="losslow" size="2"
789
										value="<?=htmlspecialchars($pconfig['losslow']);?>" />
790
									<?=gettext("To");?>
791
									<input name="losshigh" type="text" class="formfld unknown" id="losshigh" size="2"
792
										value="<?=htmlspecialchars($pconfig['losshigh']);?>" />
793
									<br /><span class="vexpl"><?=gettext(sprintf("Low and high thresholds for packet loss in %%. Default is %d/%d.", $apinger_default['losslow'], $apinger_default['losshigh']));?></span>
794
								</td>
795
							</tr>
796
							<tr>
797
								<td width="22%" valign="top" class="vncellreq"><?=gettext("Probe Interval");?></td>
798
								<td width="78%" class="vtable">
799
									<input name="interval" type="text" class="formfld unknown" id="interval" size="2"
800
										value="<?=htmlspecialchars($pconfig['interval']);?>" onchange="interval_change(this)" />
801
									<br /><span class="vexpl">
802
										<?=gettext(sprintf("How often that an ICMP probe will be sent in seconds. Default is %d.", $apinger_default['interval']));?><br /><br />
803
										<?=gettext("NOTE: The quality graph is averaged over seconds, not intervals, so as the probe interval is increased the accuracy of the quality graph is decreased.");?>
804
									</span>
805
								</td>
806
							</tr>
807
							<tr>
808
								<td width="22%" valign="top" class="vncellreq"><?=gettext("Down");?></td>
809
								<td width="78%" class="vtable">
810
									<input name="down" type="text" class="formfld unknown" id="down" size="2"
811
										value="<?=htmlspecialchars($pconfig['down']);?>" />
812
									<br /><span class="vexpl"><?=gettext(sprintf("The number of seconds of failed probes before the alarm will fire. Default is %d.", $apinger_default['down']));?></span>
813
								</td>
814
							</tr>
815
							<tr>
816
								<td width="22%" valign="top" class="vncellreq"><?=gettext("Average Delay Replies Qty");?></td>
817
								<td width="78%" class="vtable">
818
									<input name="avg_delay_samples" type="text" class="formfld unknown" id="avg_delay_samples" size="2"
819
										value="<?=htmlspecialchars($pconfig['avg_delay_samples']);?>" onchange="samples_change(document.iform.avg_delay_samples_calculated, this)" /> 
820
									<input name="avg_delay_samples_calculated" type="checkbox" id="avg_delay_samples_calculated" value="yes" <?php if ($pconfig['avg_delay_samples_calculated'] == true) echo "checked=\"checked\""; ?> onclick="calculated_change(this, document.iform.avg_delay_samples)" />
821
										<?=gettext("Use calculated value."); ?>
822
									<br /><span class="vexpl"><?=gettext(sprintf("How many replies should be used to compute average delay for controlling \"delay\" alarms?  Default is %d.", $apinger_default['avg_delay_samples']));?><br /><br /></span>
823
								</td>
824
							</tr>
825
							<tr>
826
								<td width="22%" valign="top" class="vncellreq"><?=gettext("Average Packet Loss Probes Qty");?></td>
827
								<td width="78%" class="vtable">
828
									<input name="avg_loss_samples" type="text" class="formfld unknown" id="avg_loss_samples" size="2"
829
										value="<?=htmlspecialchars($pconfig['avg_loss_samples']);?>" onchange="samples_change(document.iform.avg_loss_samples_calculated, this)" />
830
									<input name="avg_loss_samples_calculated" type="checkbox" id="avg_loss_samples_calculated" value="yes" <?php if ($pconfig['avg_loss_samples_calculated'] == true) echo "checked=\"checked\""; ?> onclick="calculated_change(this, document.iform.avg_loss_samples)" />
831
										<?=gettext("Use calculated value."); ?>
832
									<br /><span class="vexpl"><?=gettext(sprintf("How many probes should be useds to compute average packet loss?  Default is %d.", $apinger_default['avg_loss_samples']));?><br /><br /></span>
833
								</td>
834
							</tr>
835
							<tr>
836
								<td width="22%" valign="top" class="vncellreq"><?=gettext("Lost Probe Delay");?></td>
837
								<td width="78%" class="vtable">
838
									<input name="avg_loss_delay_samples" type="text" class="formfld unknown" id="avg_loss_delay_samples" size="2"
839
										value="<?=htmlspecialchars($pconfig['avg_loss_delay_samples']);?>" onchange="samples_change(document.iform.avg_loss_delay_samples_calculated, this)" />
840
									<input name="avg_loss_delay_samples_calculated" type="checkbox" id="avg_loss_delay_samples_calculated" value="yes" <?php if ($pconfig['avg_loss_delay_samples_calculated'] == true) echo "checked=\"checked\""; ?> onclick="calculated_change(this, document.iform.avg_loss_delay_samples)" />
841
										<?=gettext("Use calculated value."); ?>
842
									<br /><span class="vexpl"><?=gettext(sprintf("The delay (in qty of probe samples) after which loss is computed.  Without this, delays longer than the probe interval would be treated as packet loss.  Default is %d.", $apinger_default['avg_loss_delay_samples']));?><br /><br /></span>
843
								</td>
844
							</tr>
845
							<tr>
846
								<td colspan="2">
847
									<?= gettext("The probe interval must be less than the down time, otherwise the gateway will seem to go down then come up again at the next probe."); ?><br /><br />
848
									<?= gettext("The down time defines the length of time before the gateway is marked as down, but the accuracy is controlled by the probe interval. For example, if your down time is 40 seconds but on a 30 second probe interval, only one probe would have to fail before the gateway is marked down at the 40 second mark. By default, the gateway is considered down after 10 seconds, and the probe interval is 1 second, so 10 probes would have to fail before the gateway is marked down."); ?><br />
849
								</td>
850
							</tr>
851
						</table>
852
					</div>
853
				</td>
854
			</tr>
855
			<tr>
856
				<td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
857
				<td width="78%" class="vtable">
858
					<input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>" />
859
					<br /><span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)"); ?>.</span>
860
				</td>
861
			</tr>
862
			<tr>
863
				<td width="22%" valign="top">&nbsp;</td>
864
				<td width="78%">
865
					<input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>" onclick="enable_change()" />
866
					<input type="button" class="formbtn" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=$referer;?>'" />
867
					<input name="referer" type="hidden" value="<?=$referer;?>" />
868
					<?php if (isset($id) && $a_gateways[$id]): ?>
869
					<input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
870
					<?php endif; ?>
871
				</td>
872
			</tr>
873
		</table>
874
	</form>
875
<?php include("fend.inc"); ?>
876
<script type="text/javascript">
877
//<![CDATA[
878
monitor_change();
879
calculate_state_change();
880
//]]>
881
</script>
882
</body>
883
</html>
(218-218/251)