Project

General

Profile

Download (28.6 KB) Statistics
| Branch: | Tag: | Revision:
1 0b844b73 Renato Botelho
<?php
2 124aee67 Chris Buechler
/* $Id$ */
3 d173230c Seth Mos
/*
4
	system_gateways_edit.php
5 5721595b Chris Buechler
	part of pfSense (https://www.pfsense.org)
6 0b844b73 Renato Botelho
7 6216690b smos
	Copyright (C) 2010 Seth Mos <seth.mos@dds.nl>.
8 ce77a9c4 Phil Davis
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
9 d173230c Seth Mos
	All rights reserved.
10 0b844b73 Renato Botelho
11 d173230c Seth Mos
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13 0b844b73 Renato Botelho
14 d173230c Seth Mos
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16 0b844b73 Renato Botelho
17 d173230c Seth Mos
	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 0b844b73 Renato Botelho
21 d173230c Seth Mos
	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 1d333258 Scott Ullrich
/*
33
	pfSense_MODULE:	routing
34
*/
35 d173230c Seth Mos
36 6b07c15a Matthew Grooms
##|+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 124aee67 Chris Buechler
require("guiconfig.inc");
44 4666b787 Ermal Lu?i
require("pkg-utils.inc");
45 d173230c Seth Mos
46 62424bdb Renato Botelho
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/system_gateways.php');
47
48 96ef7db0 Renato Botelho
$a_gateways = return_gateways_array(true, false, true);
49 87f0be87 Chris Buechler
$a_gateways_arr = array();
50
foreach($a_gateways as $gw) {
51
	$a_gateways_arr[] = $gw;
52
}
53
$a_gateways = $a_gateways_arr;
54 d173230c Seth Mos
55 616e1956 Seth Mos
if (!is_array($config['gateways']['gateway_item']))
56 0b844b73 Renato Botelho
	$config['gateways']['gateway_item'] = array();
57
58 616e1956 Seth Mos
$a_gateway_item = &$config['gateways']['gateway_item'];
59 eb233517 Phil Davis
$apinger_default = return_apinger_defaults();
60 616e1956 Seth Mos
61 e41ec584 Renato Botelho
if (is_numericint($_GET['id']))
62
	$id = $_GET['id'];
63
if (isset($_POST['id']) && is_numericint($_POST['id']))
64 d173230c Seth Mos
	$id = $_POST['id'];
65
66 e41ec584 Renato Botelho
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
67 d173230c Seth Mos
	$id = $_GET['dup'];
68
69
if (isset($id) && $a_gateways[$id]) {
70 bb849003 Ermal
	$pconfig = array();
71 e90db2b4 Ermal Lu?i
	$pconfig['name'] = $a_gateways[$id]['name'];
72 5f53260a Ermal
	$pconfig['weight'] = $a_gateways[$id]['weight'];
73 f7203985 Ermal
	$pconfig['interval'] = $a_gateways[$id]['interval'];
74 3c6787ed N0YB
	$pconfig['avg_delay_samples'] = $a_gateways[$id]['avg_delay_samples'];
75
	$pconfig['avg_delay_samples_calculated'] = isset($a_gateways[$id]['avg_delay_samples_calculated']);
76
	$pconfig['avg_loss_samples'] = $a_gateways[$id]['avg_loss_samples'];
77
	$pconfig['avg_loss_samples_calculated'] = isset($a_gateways[$id]['avg_loss_samples_calculated']);
78
	$pconfig['avg_loss_delay_samples'] = $a_gateways[$id]['avg_loss_delay_samples'];
79
	$pconfig['avg_loss_delay_samples_calculated'] = isset($a_gateways[$id]['avg_loss_delay_samples_calculated']);
80 e90db2b4 Ermal Lu?i
	$pconfig['interface'] = $a_gateways[$id]['interface'];
81 883c53c9 Ermal Lu?i
	$pconfig['friendlyiface'] = $a_gateways[$id]['friendlyiface'];
82 c1d36d26 smos
	$pconfig['ipprotocol'] = $a_gateways[$id]['ipprotocol'];
83 d44d26c1 Ermal
	if (isset($a_gateways[$id]['dynamic']))
84
		$pconfig['dynamic'] = true;
85
	$pconfig['gateway'] = $a_gateways[$id]['gateway'];
86 883c53c9 Ermal Lu?i
	$pconfig['defaultgw'] = isset($a_gateways[$id]['defaultgw']);
87 81f19476 Renato Botelho
	$pconfig['force_down'] = isset($a_gateways[$id]['force_down']);
88 23d9f686 Renato Botelho
	$pconfig['latencylow'] = $a_gateways[$id]['latencylow'];
89
	$pconfig['latencyhigh'] = $a_gateways[$id]['latencyhigh'];
90
	$pconfig['losslow'] = $a_gateways[$id]['losslow'];
91
	$pconfig['losshigh'] = $a_gateways[$id]['losshigh'];
92
	$pconfig['down'] = $a_gateways[$id]['down'];
93 99c61352 Ermal
	$pconfig['monitor'] = $a_gateways[$id]['monitor'];
94 33c06ef7 Ermal
	$pconfig['monitor_disable'] = isset($a_gateways[$id]['monitor_disable']);
95 e90db2b4 Ermal Lu?i
	$pconfig['descr'] = $a_gateways[$id]['descr'];
96
	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
97 96ef7db0 Renato Botelho
	$pconfig['disabled'] = isset($a_gateways[$id]['disabled']);
98 d173230c Seth Mos
}
99
100 e41ec584 Renato Botelho
if (isset($_GET['dup']) && is_numericint($_GET['dup'])) {
101 d173230c Seth Mos
	unset($id);
102 124aee67 Chris Buechler
	unset($pconfig['attribute']);
103
}
104 d173230c Seth Mos
105 23d9f686 Renato Botelho
if (isset($id) && $a_gateways[$id])
106
	$realid = $a_gateways[$id]['attribute'];
107
108 d173230c Seth Mos
if ($_POST) {
109
110
	unset($input_errors);
111
112
	/* input validation */
113 d44d26c1 Ermal
	$reqdfields = explode(" ", "name interface");
114
	$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
115 883c53c9 Ermal Lu?i
116 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
117 883c53c9 Ermal Lu?i
118 d173230c Seth Mos
	if (! isset($_POST['name'])) {
119
		$input_errors[] = "A valid gateway name must be specified.";
120
	}
121 31ace93c Seth Mos
	if (! is_validaliasname($_POST['name'])) {
122 df152a88 Carlos Eduardo Ramos
		$input_errors[] = gettext("The gateway name must not contain invalid characters.");
123 31ace93c Seth Mos
	}
124 87f0be87 Chris Buechler
	/* skip system gateways which have been automatically added */
125 283d78c6 Darren Embry
	if (($_POST['gateway'] && (!is_ipaddr($_POST['gateway'])) && ($_POST['attribute'] !== "system")) && ($_POST['gateway'] != "dynamic")) {
126 df152a88 Carlos Eduardo Ramos
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
127 87f0be87 Chris Buechler
	}
128 15a13dab Scott Ullrich
129 d44d26c1 Ermal
	if ($_POST['gateway'] && (is_ipaddr($_POST['gateway'])) && !$_REQUEST['isAjax']) {
130 9e80d14c Seth Mos
		if(is_ipaddrv4($_POST['gateway'])) {
131 47593ac6 Seth Mos
			$parent_ip = get_interface_ip($_POST['interface']);
132 e227df57 Ermal Lu?i
			$parent_sn = get_interface_subnet($_POST['interface']);
133 9e80d14c Seth Mos
			if(empty($parent_ip) || empty($parent_sn)) {
134 bf755503 Renato Botelho
				$input_errors[] = gettext("Cannot add IPv4 Gateway Address because no IPv4 address could be found on the interface.");
135 9e80d14c Seth Mos
			} else {
136 55705b33 Renato Botelho
				$subnets = array(gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn);
137
				$vips = link_interface_to_vips($_POST['interface']);
138
				if (is_array($vips))
139
					foreach($vips as $vip) {
140
						if (!is_ipaddrv4($vip['subnet']))
141
							continue;
142
						$subnets[] = gen_subnet($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
143
					}
144
145
				$found = false;
146
				foreach($subnets as $subnet)
147
					if(ip_in_subnet($_POST['gateway'], $subnet)) {
148
						$found = true;
149
						break;
150
					}
151
152
				if ($found === false)
153
					$input_errors[] = sprintf(gettext("The gateway address %1\$s does not lie within one of the chosen interface's subnets."), $_POST['gateway']);
154 e227df57 Ermal Lu?i
			}
155 e7d3b8f4 Seth Mos
		}
156 b5b5bcc0 Ermal
		else if(is_ipaddrv6($_POST['gateway'])) {
157 2a9cb3b2 smos
			/* do not do a subnet match on a link local address, it's valid */
158 db7a628c Renato Botelho
			if(!is_linklocal($_POST['gateway'])) {
159 2a9cb3b2 smos
				$parent_ip = get_interface_ipv6($_POST['interface']);
160
				$parent_sn = get_interface_subnetv6($_POST['interface']);
161
				if(empty($parent_ip) || empty($parent_sn)) {
162 bf755503 Renato Botelho
					$input_errors[] = gettext("Cannot add IPv6 Gateway Address because no IPv6 address could be found on the interface.");
163 2a9cb3b2 smos
				} else {
164 55705b33 Renato Botelho
					$subnets = array(gen_subnetv6($parent_ip, $parent_sn) . "/" . $parent_sn);
165
					$vips = link_interface_to_vips($_POST['interface']);
166
					if (is_array($vips))
167
						foreach($vips as $vip) {
168
							if (!is_ipaddrv6($vip['subnet']))
169
								continue;
170
							$subnets[] = gen_subnetv6($vip['subnet'], $vip['subnet_bits']) . "/" . $vip['subnet_bits'];
171
						}
172
173
					$found = false;
174
					foreach($subnets as $subnet)
175
						if(ip_in_subnet($_POST['gateway'], $subnet)) {
176
							$found = true;
177
							break;
178
						}
179
180
					if ($found === false)
181
						$input_errors[] = sprintf(gettext("The gateway address %1\$s does not lie within one of the chosen interface's subnets."), $_POST['gateway']);
182 35af7141 smos
				}
183 7617e245 Seth Mos
			}
184
		}
185 9e80d14c Seth Mos
186
		if (!empty($config['interfaces'][$_POST['interface']]['ipaddr'])) {
187
			if (is_ipaddr($config['interfaces'][$_POST['interface']]['ipaddr']) && (empty($_POST['gateway']) || $_POST['gateway'] == "dynamic"))
188
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv4 configuration.");
189
		}
190
		if (!empty($config['interfaces'][$_POST['interface']]['ipaddrv6'])) {
191
			if (is_ipaddr($config['interfaces'][$_POST['interface']]['ipaddrv6']) && (empty($_POST['gateway']) || $_POST['gateway'] == "dynamic"))
192
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static IPv6 configuration.");
193
		}
194 e7d3b8f4 Seth Mos
	}
195 c3c692a9 Sjon Hortensius
	if (($_POST['monitor'] != "") && !is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
196 df152a88 Carlos Eduardo Ramos
		$input_errors[] = gettext("A valid monitor IP address must be specified.");
197 d173230c Seth Mos
	}
198 2972b3e7 smos
	/* only allow correct IPv4 and IPv6 gateway addresses */
199 c3c692a9 Sjon Hortensius
	if (($_POST['gateway'] != "") && is_ipaddr($_POST['gateway']) && $_POST['gateway'] != "dynamic") {
200 2972b3e7 smos
		if(is_ipaddrv6($_POST['gateway']) && ($_POST['ipprotocol'] == "inet")) {
201
			$input_errors[] = gettext("The IPv6 gateway address '{$_POST['gateway']}' can not be used as a IPv4 gateway'.");
202
		}
203
		if(is_ipaddrv4($_POST['gateway']) && ($_POST['ipprotocol'] == "inet6")) {
204
			$input_errors[] = gettext("The IPv4 gateway address '{$_POST['gateway']}' can not be used as a IPv6 gateway'.");
205
		}
206
	}
207
	/* only allow correct IPv4 and IPv6 monitor addresses */
208 c3c692a9 Sjon Hortensius
	if (($_POST['monitor'] != "") && is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
209 2972b3e7 smos
		if(is_ipaddrv6($_POST['monitor']) && ($_POST['ipprotocol'] == "inet")) {
210
			$input_errors[] = gettext("The IPv6 monitor address '{$_POST['monitor']}' can not be used on a IPv4 gateway'.");
211 c1d36d26 smos
		}
212 2972b3e7 smos
		if(is_ipaddrv4($_POST['monitor']) && ($_POST['ipprotocol'] == "inet6")) {
213
			$input_errors[] = gettext("The IPv4 monitor address '{$_POST['monitor']}' can not be used on a IPv6 gateway'.");
214 9e80d14c Seth Mos
		}
215
	}
216 d173230c Seth Mos
217 8b060357 Seth Mos
	if (isset($_POST['name'])) {
218 31ace93c Seth Mos
		/* check for overlaps */
219
		foreach ($a_gateways as $gateway) {
220 2328dcc5 Seth Mos
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
221 23fafd14 Ermal
				if ($gateway['name'] != $_POST['name'])
222 33802679 Chris Buechler
					$input_errors[] = gettext("Changing name on a gateway is not allowed.");
223 31ace93c Seth Mos
				continue;
224
			}
225 c3c692a9 Sjon Hortensius
			if($_POST['name'] != "") {
226
				if (($gateway['name'] != "") && ($_POST['name'] == $gateway['name']) && ($gateway['attribute'] !== "system")) {
227 0e94685b Renato Botelho
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $_POST['name']);
228 2328dcc5 Seth Mos
					break;
229
				}
230
			}
231
			if(is_ipaddr($_POST['gateway'])) {
232 c3c692a9 Sjon Hortensius
				if (($gateway['gateway'] != "") && ($_POST['gateway'] == $gateway['gateway']) && ($gateway['attribute'] !== "system")) {
233 0e94685b Renato Botelho
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $_POST['gateway']);
234 2328dcc5 Seth Mos
					break;
235
				}
236 31ace93c Seth Mos
			}
237 2328dcc5 Seth Mos
			if(is_ipaddr($_POST['monitor'])) {
238 c3c692a9 Sjon Hortensius
				if (($gateway['monitor'] != "") && ($_POST['monitor'] == $gateway['monitor']) && ($gateway['attribute'] !== "system")) {
239 0e94685b Renato Botelho
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. You must choose a different monitor IP.'), $_POST['monitor']);
240 2328dcc5 Seth Mos
					break;
241
				}
242 31ace93c Seth Mos
			}
243 d173230c Seth Mos
		}
244
	}
245
246 eb233517 Phil Davis
	/* input validation of apinger advanced parameters */
247
	if($_POST['latencylow']) {
248
		if (! is_numeric($_POST['latencylow'])) {
249
			$input_errors[] = gettext("The low latency threshold needs to be a numeric value.");
250
		} else {
251
			if ($_POST['latencylow'] < 1) {
252
				$input_errors[] = gettext("The low latency threshold needs to be positive.");
253
			}
254
		}
255
	}
256
257
	if($_POST['latencyhigh']) {
258
		if (! is_numeric($_POST['latencyhigh'])) {
259
			$input_errors[] = gettext("The high latency threshold needs to be a numeric value.");
260
		} else {
261
			if ($_POST['latencyhigh'] < 1) {
262
				$input_errors[] = gettext("The high latency threshold needs to be positive.");
263
			}
264
		}
265
	}
266
267
	if($_POST['losslow']) {
268
		if (! is_numeric($_POST['losslow'])) {
269
			$input_errors[] = gettext("The low Packet Loss threshold needs to be a numeric value.");
270
		} else {
271
			if ($_POST['losslow'] < 1) {
272
				$input_errors[] = gettext("The low Packet Loss threshold needs to be positive.");
273
			}
274
			if ($_POST['losslow'] >= 100) {
275
				$input_errors[] = gettext("The low Packet Loss threshold needs to be less than 100.");
276
			}
277
		}
278
	}
279
280
	if($_POST['losshigh']) {
281
		if (! is_numeric($_POST['losshigh'])) {
282
			$input_errors[] = gettext("The high Packet Loss threshold needs to be a numeric value.");
283
		} else {
284
			if ($_POST['losshigh'] < 1) {
285
				$input_errors[] = gettext("The high Packet Loss threshold needs to be positive.");
286
			}
287
			if ($_POST['losshigh'] > 100) {
288
				$input_errors[] = gettext("The high Packet Loss threshold needs to be 100 or less.");
289
			}
290
		}
291
	}
292
293
	if(($_POST['latencylow']) && ($_POST['latencyhigh'])) {
294
		if ((is_numeric($_POST['latencylow'])) && (is_numeric($_POST['latencyhigh']))) {
295
			if(($_POST['latencylow'] > $_POST['latencyhigh'])) {
296
				$input_errors[] = gettext("The high latency threshold needs to be higher than the low latency threshold");
297
			}
298
		}
299
	} else {
300
		if($_POST['latencylow']){
301
			if (is_numeric($_POST['latencylow'])) {
302
				if($_POST['latencylow'] > $apinger_default['latencyhigh']) {
303
					$input_errors[] = gettext(sprintf("The low latency threshold needs to be less than the default high latency threshold (%d)", $apinger_default['latencyhigh']));
304
				}
305
			}
306
		}
307
		if($_POST['latencyhigh']){
308
			if (is_numeric($_POST['latencyhigh'])) {
309
				if($_POST['latencyhigh'] < $apinger_default['latencylow']) {
310
					$input_errors[] = gettext(sprintf("The high latency threshold needs to be higher than the default low latency threshold (%d)", $apinger_default['latencylow']));
311
				}
312
			}
313
		}
314
	}
315
316
	if(($_POST['losslow']) && ($_POST['losshigh'])){
317
		if ((is_numeric($_POST['losslow'])) && (is_numeric($_POST['losshigh']))) {
318
			if($_POST['losslow'] > $_POST['losshigh']) {
319
				$input_errors[] = gettext("The high Packet Loss threshold needs to be higher than the low Packet Loss threshold");
320
			}
321
		}
322
	} else {
323
		if($_POST['losslow']){
324
			if (is_numeric($_POST['losslow'])) {
325
				if($_POST['losslow'] > $apinger_default['losshigh']) {
326
					$input_errors[] = gettext(sprintf("The low Packet Loss threshold needs to be less than the default high Packet Loss threshold (%d)", $apinger_default['losshigh']));
327
				}
328
			}
329
		}
330
		if($_POST['losshigh']){
331
			if (is_numeric($_POST['losshigh'])) {
332
				if($_POST['losshigh'] < $apinger_default['losslow']) {
333
					$input_errors[] = gettext(sprintf("The high Packet Loss threshold needs to be higher than the default low Packet Loss threshold (%d)", $apinger_default['losslow']));
334
				}
335
			}
336
		}
337
	}
338
339
	if($_POST['interval']) {
340
		if (! is_numeric($_POST['interval'])) {
341 490cd438 Phil Davis
			$input_errors[] = gettext("The probe interval needs to be a numeric value.");
342 eb233517 Phil Davis
		} else {
343
			if ($_POST['interval'] < 1) {
344 490cd438 Phil Davis
				$input_errors[] = gettext("The probe interval needs to be positive.");
345 eb233517 Phil Davis
			}
346
		}
347
	}
348
349 023920e7 Ermal
	if($_POST['down']) {
350 eb233517 Phil Davis
		if (! is_numeric($_POST['down'])) {
351
			$input_errors[] = gettext("The down time setting needs to be a numeric value.");
352
		} else {
353
			if ($_POST['down'] < 1) {
354
				$input_errors[] = gettext("The down time setting needs to be positive.");
355
			}
356
		}
357
	}
358
359
	if(($_POST['interval']) && ($_POST['down'])){
360
		if ((is_numeric($_POST['interval'])) && (is_numeric($_POST['down']))) {
361
			if($_POST['interval'] > $_POST['down']) {
362 490cd438 Phil Davis
				$input_errors[] = gettext("The probe interval needs to be less than the down time setting.");
363 eb233517 Phil Davis
			}
364
		}
365
	} else {
366
		if($_POST['interval']){
367
			if (is_numeric($_POST['interval'])) {
368
				if($_POST['interval'] > $apinger_default['down']) {
369 490cd438 Phil Davis
					$input_errors[] = gettext(sprintf("The probe interval needs to be less than the default down time setting (%d)", $apinger_default['down']));
370 eb233517 Phil Davis
				}
371
			}
372
		}
373
		if($_POST['down']){
374
			if (is_numeric($_POST['down'])) {
375
				if($_POST['down'] < $apinger_default['interval']) {
376 490cd438 Phil Davis
					$input_errors[] = gettext(sprintf("The down time setting needs to be higher than the default probe interval (%d)", $apinger_default['interval']));
377 eb233517 Phil Davis
				}
378
			}
379
		}
380
	}
381 023920e7 Ermal
382 3c6787ed N0YB
	if($_POST['avg_delay_samples']) {
383
		if (! is_numeric($_POST['avg_delay_samples'])) {
384
			$input_errors[] = gettext("The average delay replies qty needs to be a numeric value.");
385
		} else {
386
			if ($_POST['avg_delay_samples'] < 1) {
387
				$input_errors[] = gettext("The average delay replies qty needs to be positive.");
388
			}
389
		}
390
	}
391
392
	if($_POST['avg_loss_samples']) {
393
		if (! is_numeric($_POST['avg_loss_samples'])) {
394
			$input_errors[] = gettext("The average packet loss probes qty needs to be a numeric value.");
395
		} else {
396
			if ($_POST['avg_loss_samples'] < 1) {
397
				$input_errors[] = gettext("The average packet loss probes qty needs to be positive.");
398
			}
399
		}
400
	}
401
402
	if($_POST['avg_loss_delay_samples']) {
403
		if (! is_numeric($_POST['avg_loss_delay_samples'])) {
404
			$input_errors[] = gettext("The lost probe delay needs to be a numeric value.");
405
		} else {
406
			if ($_POST['avg_loss_delay_samples'] < 1) {
407
				$input_errors[] = gettext("The lost probe delay needs to be positive.");
408
			}
409
		}
410
	}
411
412 d173230c Seth Mos
	if (!$input_errors) {
413 c6865c5e Ermal
		$reloadif = "";
414 d44d26c1 Ermal
		$gateway = array();
415
416
		if (empty($_POST['interface']))
417
			$gateway['interface'] = $pconfig['friendlyiface'];
418
		else
419 3befe730 Ermal
			$gateway['interface'] = $_POST['interface'];
420 d44d26c1 Ermal
		if (is_ipaddr($_POST['gateway']))
421
			$gateway['gateway'] = $_POST['gateway'];
422
		else
423
			$gateway['gateway'] = "dynamic";
424
		$gateway['name'] = $_POST['name'];
425
		$gateway['weight'] = $_POST['weight'];
426 e02caf4a smos
		$gateway['ipprotocol'] = $_POST['ipprotocol'];
427 f7203985 Ermal
		$gateway['interval'] = $_POST['interval'];
428 3c6787ed N0YB
429
		$gateway['avg_delay_samples'] = $_POST['avg_delay_samples'];
430
		if ($_POST['avg_delay_samples_calculated'] == "yes" || $_POST['avg_delay_samples_calculated'] == "on")
431
			$gateway['avg_delay_samples_calculated'] = true;
432
433
		$gateway['avg_loss_samples'] = $_POST['avg_loss_samples'];
434
		if ($_POST['avg_loss_samples_calculated'] == "yes" || $_POST['avg_loss_samples_calculated'] == "on")
435
			$gateway['avg_loss_samples_calculated'] = true;
436
437
		$gateway['avg_loss_delay_samples'] = $_POST['avg_loss_delay_samples'];
438
		if ($_POST['avg_loss_delay_samples_calculated'] == "yes" || $_POST['avg_loss_delay_samples_calculated'] == "on")
439
			$gateway['avg_loss_delay_samples_calculated'] = true;
440
441 d44d26c1 Ermal
		$gateway['descr'] = $_POST['descr'];
442 33c06ef7 Ermal
		if ($_POST['monitor_disable'] == "yes")
443
			$gateway['monitor_disable'] = true;
444 81f19476 Renato Botelho
		if ($_POST['force_down'] == "yes")
445
			$gateway['force_down'] = true;
446 fcd01c8a Renato Botelho
		if (is_ipaddr($_POST['monitor']))
447 d44d26c1 Ermal
			$gateway['monitor'] = $_POST['monitor'];
448 fcd01c8a Renato Botelho
449
		/* NOTE: If monitor ip is changed need to cleanup the old static route */
450
		if ($_POST['monitor'] != "dynamic" && !empty($a_gateway_item[$realid]) && is_ipaddr($a_gateway_item[$realid]['monitor']) &&
451 c3c692a9 Sjon Hortensius
			$_POST['monitor'] != $a_gateway_item[$realid]['monitor'] && $gateway['gateway'] != $a_gateway_item[$realid]['monitor']) {
452 fcd01c8a Renato Botelho
			if (is_ipaddrv4($a_gateway_item[$realid]['monitor']))
453
				mwexec("/sbin/route delete " . escapeshellarg($a_gateway_item[$realid]['monitor']));
454
			else
455
				mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateway_item[$realid]['monitor']));
456 32a9eb18 Ermal
		}
457 d44d26c1 Ermal
458
		if ($_POST['defaultgw'] == "yes" || $_POST['defaultgw'] == "on") {
459
			$i = 0;
460 2f14d021 Seth Mos
			/* remove the default gateway bits for all gateways with the same address family */
461 a531d687 Ermal
			foreach($a_gateway_item as $gw) {
462 beb7cd97 Renato Botelho
				if ($gateway['ipprotocol'] == $gw['ipprotocol']) {
463 2f14d021 Seth Mos
					unset($config['gateways']['gateway_item'][$i]['defaultgw']);
464
					if ($gw['interface'] != $_POST['interface'] && $gw['defaultgw'])
465
						$reloadif = $gw['interface'];
466
				}
467 d44d26c1 Ermal
				$i++;
468 bb849003 Ermal
			}
469 d44d26c1 Ermal
			$gateway['defaultgw'] = true;
470 124aee67 Chris Buechler
		}
471 d44d26c1 Ermal
472
		if ($_POST['latencylow'])
473
			$gateway['latencylow'] = $_POST['latencylow'];
474
		if ($_POST['latencyhigh'])
475 0b844b73 Renato Botelho
			$gateway['latencyhigh'] = $_POST['latencyhigh'];
476 d44d26c1 Ermal
		if ($_POST['losslow'])
477 a0edece9 Darren Embry
			$gateway['losslow'] = $_POST['losslow'];
478 d44d26c1 Ermal
		if ($_POST['losshigh'])
479 0b844b73 Renato Botelho
			$gateway['losshigh'] = $_POST['losshigh'];
480 d44d26c1 Ermal
		if ($_POST['down'])
481 0b844b73 Renato Botelho
			$gateway['down'] = $_POST['down'];
482 d44d26c1 Ermal
483 96ef7db0 Renato Botelho
		if(isset($_POST['disabled']))
484
			$gateway['disabled'] = true;
485
		else
486
			unset($gateway['disabled']);
487
488 d44d26c1 Ermal
		/* when saving the manual gateway we use the attribute which has the corresponding id */
489 23d9f686 Renato Botelho
		if (isset($realid) && $a_gateway_item[$realid])
490
			$a_gateway_item[$realid] = $gateway;
491 d44d26c1 Ermal
		else
492
			$a_gateway_item[] = $gateway;
493
494 a368a026 Ermal Lu?i
		mark_subsystem_dirty('staticroutes');
495 0b844b73 Renato Botelho
496 d173230c Seth Mos
		write_config();
497 d44d26c1 Ermal
498 ad862220 Charlie
		if($_REQUEST['isAjax']) {
499
			echo $_POST['name'];
500
			exit;
501 c6865c5e Ermal
		} else if (!empty($reloadif))
502
			send_event("interface reconfigure {$reloadif}");
503 0b844b73 Renato Botelho
504 d173230c Seth Mos
		header("Location: system_gateways.php");
505
		exit;
506 ea939fc3 Ermal
	} else {
507 a0edece9 Darren Embry
		if ($_REQUEST['isAjax']) {
508
			header("HTTP/1.0 500 Internal Server Error");
509
			header("Content-type: text/plain");
510
			foreach ($input_errors as $error) {
511
				echo("$error\n");
512
			}
513
			exit;
514
		}
515 0b844b73 Renato Botelho
516 883c53c9 Ermal Lu?i
		$pconfig = $_POST;
517 ea939fc3 Ermal
		if (empty($_POST['friendlyiface']))
518
			$pconfig['friendlyiface'] = $_POST['interface'];
519
	}
520 d173230c Seth Mos
}
521
522 2328dcc5 Seth Mos
523 df152a88 Carlos Eduardo Ramos
$pgtitle = array(gettext("System"),gettext("Gateways"),gettext("Edit gateway"));
524 b32dd0a6 jim-p
$shortcut_section = "gateways";
525 02ca24c9 jim-p
526 d173230c Seth Mos
include("head.inc");
527
528 c3c692a9 Sjon Hortensius
if ($input_errors)
529
	print_input_errors($input_errors);
530 3c6787ed N0YB
531 c3c692a9 Sjon Hortensius
?>
532
<script>
533
var systemGatewaysEditRecalculate = function(){
534
	var interval = $('#interval')[0].value;
535
536
	$('input[name$=_calculated]').each(function (idx, c){
537
		c = $(c);
538
		i = $(c.parents('.form-group').find('input[type=number]')[0]);
539
540
		c.prop('disabled', interval==0);
541
542
		if (interval==0)
543
			c.prop('checked', false);
544
545
		if (!c.prop('checked'))
546
			return i.prop('readonly', false);
547
548
		switch (i.attr('name'))
549
		{
550
			case 'avg_delay_samples':
551
				// How many replies should be used to compute average delay
552
				// for controlling "delay" alarms.
553
				// Calculate a reasonable value based on gateway probe interval and RRD 1 minute average graph step size (60).
554
				i.attr('value', Math.round(60 * (1/6) / Math.pow(interval, 0.333)));
555
			break;
556
557
			case 'avg_loss_samples':
558
				// How many probes should be used to compute average loss.
559
				// Calculate a reasonable value based on gateway probe interval and RRD 1 minute average graph step size (60).
560
				i.attr('value', Math.round(60 / interval));
561
			break;
562
563
			case 'avg_loss_delay_samples':
564
				// The delay (in samples) after which loss is computed
565
				// without this delays larger than interval would be treated as loss.
566
				// Calculate a reasonable value based on gateway probe interval and RRD 1 minute average graph step size (60).
567
				i.attr('value', Math.round(60 * (1/3) / interval));
568
			break;
569
		}
570 3c6787ed N0YB
571 c3c692a9 Sjon Hortensius
		i.prop('readonly', true);
572
	});
573
};
574 ec5c28cd N0YB
575 c3c692a9 Sjon Hortensius
events.push(function(){
576
	$('#interval').on('change', systemGatewaysEditRecalculate);
577
	$('input[name$=_calculated]').on('change', systemGatewaysEditRecalculate);
578 3c6787ed N0YB
579 c3c692a9 Sjon Hortensius
	systemGatewaysEditRecalculate();
580
});
581
</script>
582
<?php
583 ec5c28cd N0YB
584 c3c692a9 Sjon Hortensius
require('classes/Form.class.php');
585
$form = new Form;
586
587
/* If this is a system gateway we need this var */
588
if(($pconfig['attribute'] == "system") || is_numeric($pconfig['attribute'])) {
589
	$form->addGlobal(new Form_Input(
590
		'attribute',
591
		null,
592
		'hidden',
593
		$pconfig['attribute']
594
	));
595 3c6787ed N0YB
}
596 ec5c28cd N0YB
597 c3c692a9 Sjon Hortensius
if (isset($id) && $a_gateways[$id]) {
598
	$form->addGlobal(new Form_Input(
599
		'id',
600
		null,
601
		'hidden',
602
		$id
603
	));
604 ec5c28cd N0YB
}
605 e9df5769 Seth Mos
606 c3c692a9 Sjon Hortensius
$form->addGlobal(new Form_Input(
607
	'friendlyiface',
608
	null,
609
	'hidden',
610
	$pconfig['friendlyiface']
611
));
612
613
$section = new Form_Section('Edit gateway');
614
615
$section->addInput(new Form_Checkbox(
616
	'disabled',
617
	'Disabled',
618
	'Disable this gateway',
619
	$pconfig['disabled']
620
))->setHelp('Set this option to disable this gateway without removing it from the '.
621
	'list.');
622
623
$section->addInput(new Form_Select(
624
	'interface',
625
	'Interface',
626
	$pconfig['friendlyiface'],
627
	get_configured_interface_with_descr(false, true)
628
))->setHelp('Choose which interface this gateway applies to.');
629
630
$section->addInput(new Form_Select(
631
	'ipprotocol',
632
	'Address Family',
633
	$pconfig['ipprotocol'],
634
	array(
635
		"inet" => "IPv4",
636
		"inet6" => "IPv6"
637
	)
638
))->setHelp('Choose the Internet Protocol this gateway uses.');
639
640
$section->addInput(new Form_Input(
641
	'name',
642
	'Name',
643
	'text',
644
	$pconfig['name']
645
))->setHelp('Gateway name');
646
647
$section->addInput(new Form_Input(
648
	'gateway',
649
	'Gateway',
650
	'text',
651
	($pconfig['dynamic'] ? 'dynamic' : $pconfig['gateway'])
652
))->setHelp('Gateway IP address');
653
654
$section->addInput(new Form_Checkbox(
655
	'defaultgw',
656
	'Default Gateway',
657
	'This will select the above gateway as the default gateway',
658
	$pconfig['defaultgw']
659
));
660
661
$section->addInput(new Form_Checkbox(
662
	'monitor_disable',
663
	'Gateway Monitoring',
664
	'Disable Gateway Monitoring',
665
	$pconfig['monitor_disable']
666
))->toggles('.toggle-monitor-ip')->setHelp('This will consider this gateway as always being up');
667
668
$group = new Form_Group('Monitor IP');
669
$group->addClass('toggle-monitor-ip', 'collapse');
670
671
if (!$pconfig['monitor_disable'])
672
	$group->addClass('in');
673
674
$group->add(new Form_Input(
675
	'monitor',
676
	null,
677
	'text',
678
	($pconfig['gateway'] == $pconfig['monitor'] ? '' : $pconfig['monitor'])
679
))->setHelp('Enter an alternative address here to be '.
680
	'used to monitor the link. This is used for the quality RRD graphs as well as the '.
681
	'load balancer entries. Use this if the gateway does not respond to ICMP echo '.
682
	'requests (pings).');
683
$section->add($group);
684
685
$section->addInput(new Form_Checkbox(
686
	'force_down',
687
	'Force state',
688
	'Mark Gateway as Down',
689
	$pconfig['force_down']
690
))->setHelp('This will force this gateway to be considered Down');
691
692
$section->addInput(new Form_Input(
693
	'descr',
694
	'Description',
695
	'text',
696
	$pconfig['descr']
697
))->setHelp('You may enter a description here for your reference (not parsed).');
698
699
$form->add($section);
700
$section = new Form_Section('Advanced');
701
702
$section->addInput(new Form_Select(
703
	'weight',
704
	'Weight',
705
	$pconfig['weight'],
706
	array_combine(range(1, 5), range(1, 5))
707
))->setHelp('Weight for this gateway when used in a Gateway Group.');
708
709
$group = new Form_Group('Latency thresholds');
710
$group->add(new Form_Input(
711
	'latencylow',
712
	'From',
713
	'number',
714
	$pconfig['latencylow'],
715
	['placeholder' => $apinger_default['latencylow']]
716
));
717
$group->add(new Form_Input(
718
	'latencyhigh',
719
	'To',
720
	'number',
721
	$pconfig['latencyhigh'],
722
	['placeholder' => $apinger_default['latencyhigh']]
723
));
724
$group->setHelp('Low and high thresholds for latency in milliseconds.
725
	Default is %d/%d.', [$apinger_default['latencylow'], $apinger_default['latencyhigh']]);
726
727
$section->add($group);
728
729
$group = new Form_Group('Packet Loss thresholds');
730
$group->add(new Form_Input(
731
	'losslow',
732
	'From',
733
	'number',
734
	$pconfig['losslow'],
735
	['placeholder' => $apinger_default['losslow']]
736
));
737
$group->add(new Form_Input(
738
	'losshigh',
739
	'To',
740
	'number',
741
	$pconfig['losshigh'],
742
	['placeholder' => $apinger_default['losshigh']]
743
));
744
$group->setHelp('Low and high thresholds for packet loss in milliseconds.
745
	Default is %d/%d.', [$apinger_default['losslow'], $apinger_default['losshigh']]);
746
$section->add($group);
747
748
$section->addInput(new Form_Input(
749
	'interval',
750
	'Probe Interval',
751
	'number',
752
	$pconfig['interval'],
753
	[
754
		'placeholder' => $apinger_default['interval'],
755
		'max' => 86400
756
	]
757
))->setHelp('How often an ICMP probe will be sent in seconds. Default is %d.'.
758
	'NOTE: The quality graph is averaged over seconds, not intervals, so as '.
759
	'the probe interval is increased the accuracy of the quality graph is '.
760
	'decreased.', [$apinger_default['interval']]);
761
762
$section->addInput(new Form_Input(
763
	'down',
764
	'Down',
765
	'number',
766
	$pconfig['down'],
767
	['placeholder' => $apinger_default['down']]
768
))->setHelp('The number of seconds of failed probes before the alarm '.
769
	'will fire. Default is %d.', [$apinger_default['down']]);
770
771
$group = new Form_Group('Avg. Delay Replies Qty');
772
$group->add(new Form_Input(
773
	'avg_delay_samples',
774
	null,
775
	'number',
776
	$pconfig['avg_delay_samples'],
777
	[
778
		'placeholder' => $apinger_default['avg_delay_samples'],
779
		'max' => 100
780
	]
781
));
782
$group->add(new Form_Checkbox(
783
	'avg_delay_samples_calculated',
784
	null,
785
	'Use calculated value.',
786
	$pconfig['avg_delay_samples_calculated']
787
));
788
$group->setHelp('How many replies should be used to compute average delay for '.
789
	'controlling "delay" alarms? Default is %d.',
790
	[$apinger_default['avg_delay_samples']]);
791
$section->add($group);
792
793
$group = new Form_Group('Avg. Packet Loss Probes');
794
$group->add(new Form_Input(
795
	'avg_loss_samples',
796
	null,
797
	'number',
798
	$pconfig['avg_loss_samples'],
799
	[
800
		'placeholder' => $apinger_default['avg_loss_samples'],
801
		'max' => 1000
802
	]
803
));
804
$group->add(new Form_Checkbox(
805
	'avg_loss_samples_calculated',
806
	null,
807
	'Use calculated value.',
808
	$pconfig['avg_loss_samples_calculated']
809
));
810
$group->setHelp('How many probes should be useds to compute average packet loss? '.
811
	'Default is %d.',
812
	[$apinger_default['avg_loss_samples']]);
813
$section->add($group);
814
815
$group = new Form_Group('Lost Probe Delay');
816
$group->add(new Form_Input(
817
	'avg_loss_delay_samples',
818
	null,
819
	'number',
820
	$pconfig['avg_loss_delay_samples'],
821
	[
822
		'placeholder' => $apinger_default['avg_loss_delay_samples'],
823
		'max' => 200
824
	]
825
));
826
$group->add(new Form_Checkbox(
827
	'avg_loss_delay_samples_calculated',
828
	null,
829
	'Use calculated value.',
830
	$pconfig['avg_loss_samples_calculated']
831
));
832
$group->setHelp('The delay (in qty of probe samples) after which loss is '.
833
	'computed. Without this, delays longer than the probe interval would be '.
834
	'treated as packet loss.  Default is %d.',
835
	[$apinger_default['avg_loss_delay_samples']]);
836
$section->add($group);
837
838
$section->addInput(new Form_StaticText(
839
	'Additional information',
840
	'<span class="help-block">'.
841
	gettext('The probe interval must be less than the down time, otherwise the '.
842
		'gateway will seem to go down then come up again at the next probe.').
843
	'<br/><br/>'.
844
	gettext('The down time defines the length of time before the gateway is marked '.
845
		'as down, but the accuracy is controlled by the probe interval. For example, '.
846
		'if your down time is 40 seconds but on a 30 second probe interval, only one '.
847
		'probe would have to fail before the gateway is marked down at the 40 second '.
848
		'mark. By default, the gateway is considered down after 10 seconds, and the '.
849
		'probe interval is 1 second, so 10 probes would have to fail before the gateway '.
850
		'is marked down.').
851
	'</span>'
852
));
853
854
$form->add($section);
855
856
print $form;
857
include("foot.inc");