Project

General

Profile

Download (20.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php 
2
/* $Id$ */
3
/*
4
	system_gateways_edit.php
5
	part of pfSense (http://pfsense.com)
6
	
7
	Copyright (C) 2010 Seth Mos <seth.mos@dds.nl>.
8
	All rights reserved.
9
	
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12
	
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15
	
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19
	
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
/*
32
	pfSense_MODULE:	routing
33
*/
34

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

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

    
45
$a_gateways = return_gateways_array(true);
46
$a_gateways_arr = array();
47
foreach($a_gateways as $gw) {
48
	$a_gateways_arr[] = $gw;
49
}
50
$a_gateways = $a_gateways_arr;
51

    
52
if (!is_array($config['gateways']['gateway_item']))
53
        $config['gateways']['gateway_item'] = array();
54
        
55
$a_gateway_item = &$config['gateways']['gateway_item'];
56

    
57
$id = $_GET['id'];
58
if (isset($_POST['id']))
59
	$id = $_POST['id'];
60

    
61
if (isset($_GET['dup'])) {
62
	$id = $_GET['dup'];
63
}
64

    
65
if (isset($id) && $a_gateways[$id]) {
66
	$pconfig = array();
67
	$pconfig['name'] = $a_gateways[$id]['name'];
68
	$pconfig['weight'] = $a_gateways[$id]['weight'];
69
	$pconfig['interval'] = $a_gateways[$id]['interval'];
70
	$pconfig['interface'] = $a_gateways[$id]['interface'];
71
	$pconfig['friendlyiface'] = $a_gateways[$id]['friendlyiface'];
72
	if (isset($a_gateways[$id]['dynamic']))
73
		$pconfig['dynamic'] = true;
74
	$pconfig['gateway'] = $a_gateways[$id]['gateway'];
75
	$pconfig['defaultgw'] = isset($a_gateways[$id]['defaultgw']);
76
	$pconfig['latencylow'] = $a_gateway_item[$id]['latencylow'];
77
        $pconfig['latencyhigh'] = $a_gateway_item[$id]['latencyhigh'];
78
        $pconfig['losslow'] = $a_gateway_item[$id]['losslow'];
79
        $pconfig['losshigh'] = $a_gateway_item[$id]['losshigh'];
80
        $pconfig['down'] = $a_gateway_item[$id]['down'];
81
	$pconfig['monitor'] = $a_gateways[$id]['monitor'];
82
	$pconfig['monitor_disable'] = isset($a_gateways[$id]['monitor_disable']);
83
	$pconfig['descr'] = $a_gateways[$id]['descr'];
84
	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
85
}
86

    
87
if (isset($_GET['dup'])) {
88
	unset($id);
89
	unset($pconfig['attribute']);
90
}
91

    
92
if ($_POST) {
93

    
94
	unset($input_errors);
95

    
96
	/* input validation */
97
	$reqdfields = explode(" ", "name interface");
98
	$reqdfieldsn = array(gettext("Name"), gettext("Interface"));
99

    
100
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
101

    
102
	if (! isset($_POST['name'])) {
103
		$input_errors[] = "A valid gateway name must be specified.";
104
	}
105
	if (! is_validaliasname($_POST['name'])) {
106
		$input_errors[] = gettext("The gateway name must not contain invalid characters.");
107
	}
108
	/* skip system gateways which have been automatically added */
109
	if (($_POST['gateway'] && (!is_ipaddr($_POST['gateway'])) && ($_POST['attribute'] != "system")) && ($_POST['gateway'] != "dynamic")) {
110
		$input_errors[] = gettext("A valid gateway IP address must be specified.");
111
	}
112

    
113
	if ($_POST['gateway'] && (is_ipaddr($_POST['gateway'])) && !$_REQUEST['isAjax']) {
114
		if (!empty($config['interfaces'][$_POST['interface']]['ipaddr'])) {
115
			if (is_ipaddr($config['interfaces'][$_POST['interface']]['ipaddr']) && (empty($_POST['gateway']) || $_POST['gateway'] == "dynamic"))
116
				$input_errors[] = gettext("Dynamic gateway values cannot be specified for interfaces with a static ip configuration.");
117
		}
118
		$parent_ip = get_interface_ip($_POST['interface']);
119
		if (is_ipaddr($parent_ip)) {
120
			$parent_sn = get_interface_subnet($_POST['interface']);
121
			if(!ip_in_subnet($_POST['gateway'], gen_subnet($parent_ip, $parent_sn) . "/" . $parent_sn) && !ip_in_interface_alias_subnet($_POST['interface'], $_POST['gateway'])) {
122
				$input_errors[] = sprintf(gettext("The gateway address %s does not lie within the chosen interface's subnet."), $_POST['gateway']);
123
			}
124
		}
125
	}
126
	if (($_POST['monitor'] <> "") && !is_ipaddr($_POST['monitor']) && $_POST['monitor'] != "dynamic") {
127
		$input_errors[] = gettext("A valid monitor IP address must be specified.");
128
	}
129

    
130
	if (isset($_POST['name'])) {
131
		/* check for overlaps */
132
		foreach ($a_gateways as $gateway) {
133
			if (isset($id) && ($a_gateways[$id]) && ($a_gateways[$id] === $gateway)) {
134
				if ($gateway['name'] != $_POST['name'])
135
					$input_errors[] = gettext("Changing name on a gateway is not allowed.");
136
				continue;
137
			}
138
			if($_POST['name'] <> "") {
139
				if (($gateway['name'] <> "") && ($_POST['name'] == $gateway['name']) && ($gateway['attribute'] != "system")) {
140
					$input_errors[] = sprintf(gettext('The gateway name "%s" already exists.'), $_POST['name']);
141
					break;
142
				}
143
			}
144
			if(is_ipaddr($_POST['gateway'])) {
145
				if (($gateway['gateway'] <> "") && ($_POST['gateway'] == $gateway['gateway']) && ($gateway['attribute'] != "system")) {
146
					$input_errors[] = sprintf(gettext('The gateway IP address "%s" already exists.'), $_POST['gateway']);
147
					break;
148
				}
149
			}
150
			if(is_ipaddr($_POST['monitor'])) {
151
				if (($gateway['monitor'] <> "") && ($_POST['monitor'] == $gateway['monitor']) && ($gateway['attribute'] != "system")) {
152
					$input_errors[] = sprintf(gettext('The monitor IP address "%s" is already in use. You must choose a different monitor IP.'), $_POST['monitor']);
153
					break;
154
				}
155
			}
156
		}
157
	}
158

    
159
	/* input validation */
160
        if($_POST['latencylow']) {
161
                if (! is_numeric($_POST['latencylow'])) {
162
                        $input_errors[] = gettext("The low latency watermark needs to be a numeric value.");
163
                }
164
        }
165

    
166
        if($_POST['latencyhigh']) {
167
                if (! is_numeric($_POST['latencyhigh'])) {
168
                        $input_errors[] = gettext("The high latency watermark needs to be a numeric value.");
169
                }
170
        }
171
        if($_POST['losslow']) {
172
                if (! is_numeric($_POST['losslow'])) {
173
                        $input_errors[] = gettext("The low loss watermark needs to be a numeric value.");
174
                }
175
        }
176
        if($_POST['losshigh']) {
177
                if (! is_numeric($_POST['losshigh'])) {
178
                        $input_errors[] = gettext("The high loss watermark needs to be a numeric value.");
179
                }
180
        }
181

    
182
        if(($_POST['latencylow']) && ($_POST['latencyhigh'])){
183
                if(($_POST['latencylow'] > $_POST['latencyhigh'])) {
184
                        $input_errors[] = gettext("The High latency watermark needs to be higher then the low latency watermark");
185
                }
186
        }
187

    
188
        if(($_POST['losslow']) && ($_POST['losshigh'])){
189
                if($_POST['losslow'] > $_POST['losshigh']) {
190
                        $input_errors[] = gettext("The High packet loss watermark needs to be higher then the low packet loss watermark");
191
                }
192
        }
193
	if($_POST['down']) {
194
                if (! is_numeric($_POST['down']) || $_POST['down'] < 1) {
195
                        $input_errors[] = gettext("The low latency watermark needs to be a numeric value.");
196
                }
197
        }
198

    
199
	if (!$input_errors) {
200
		if (!($_POST['weight'] > 1 || $_POST['latencylow'] || $_POST['latencyhigh'] ||
201
		    $_POST['losslow'] || $_POST['losshigh'] || $_POST['down'] ||
202
		    $_POST['defaultgw'] || is_ipaddr($_POST['monitor']) || is_ipaddr($_POST['gateway']))) {
203
		/* Delete from config if gw is dynamic and user is not saving any additional gateway data that system doesn't know */
204
			if (isset($id) && $a_gateway_item[$id])
205
				unset($a_gateway_item[$id]);
206
			write_config();
207
			header("Location: system_gateways.php");
208
			exit;
209
		}
210

    
211

    
212
		$reloadif = "";
213
		$gateway = array();
214

    
215
		if (empty($_POST['interface']))
216
			$gateway['interface'] = $pconfig['friendlyiface'];
217
		else
218
			$gateway['interface'] = $_POST['interface'];
219
		if (is_ipaddr($_POST['gateway']))
220
			$gateway['gateway'] = $_POST['gateway'];
221
		else
222
			$gateway['gateway'] = "dynamic";
223
		$gateway['name'] = $_POST['name'];
224
		$gateway['weight'] = $_POST['weight'];
225
		$gateway['interval'] = $_POST['interval'];
226
		$gateway['descr'] = $_POST['descr'];
227
		if ($_POST['monitor_disable'] == "yes")
228
			$gateway['monitor_disable'] = true;
229
		else if (is_ipaddr($_POST['monitor']))
230
			$gateway['monitor'] = $_POST['monitor'];
231

    
232
		if ($_POST['defaultgw'] == "yes" || $_POST['defaultgw'] == "on") {
233
			$i = 0;
234
			foreach($a_gateway_item as $gw) {
235
				unset($config['gateways']['gateway_item'][$i]['defaultgw']);
236
				if ($gw['interface'] != $_POST['interface'] && $gw['defaultgw'])
237
					$reloadif = $gw['interface'];
238
				$i++;
239
			}
240
			$gateway['defaultgw'] = true;
241
		}
242

    
243
		if ($_POST['latencylow'])
244
			$gateway['latencylow'] = $_POST['latencylow'];
245
		if ($_POST['latencyhigh'])
246
               		$gateway['latencyhigh'] = $_POST['latencyhigh'];
247
		if ($_POST['losslow'])
248
              			$gateway['losslow'] = $_POST['losslow'];
249
		if ($_POST['losshigh'])
250
               		$gateway['losshigh'] = $_POST['losshigh'];
251
		if ($_POST['down'])
252
               		$gateway['down'] = $_POST['down'];
253

    
254
		/* when saving the manual gateway we use the attribute which has the corresponding id */
255
		if (isset($id) && $a_gateway_item[$id])
256
			$a_gateway_item[$id] = $gateway;
257
		else
258
			$a_gateway_item[] = $gateway;
259

    
260
		mark_subsystem_dirty('staticroutes');
261
	
262
		write_config();
263

    
264
		if($_REQUEST['isAjax']) {
265
			echo $_POST['name'];
266
			exit;
267
		} else if (!empty($reloadif))
268
			send_event("interface reconfigure {$reloadif}");
269
		
270
		header("Location: system_gateways.php");
271
		exit;
272
	} else {
273
		$pconfig = $_POST;
274
		if (empty($_POST['friendlyiface']))
275
			$pconfig['friendlyiface'] = $_POST['interface'];
276
	}
277
}
278

    
279

    
280
$pgtitle = array(gettext("System"),gettext("Gateways"),gettext("Edit gateway"));
281
$statusurl = "status_gateways.php";
282

    
283
include("head.inc");
284

    
285
?>
286

    
287
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
288
<?php include("fbegin.inc"); ?>
289
<script language="JavaScript">
290
function show_advanced_gateway() {
291
        document.getElementById("showadvgatewaybox").innerHTML='';
292
        aodiv = document.getElementById('showgatewayadv');
293
        aodiv.style.display = "block";
294
}
295
function monitor_change() {
296
        document.iform.monitor.disabled = document.iform.monitor_disable.checked;
297
}
298
</script>
299
<?php if ($input_errors) print_input_errors($input_errors); ?>
300
            <form action="system_gateways_edit.php" method="post" name="iform" id="iform">
301
	<?php
302

    
303
	/* If this is a system gateway we need this var */
304
	if(($pconfig['attribute'] == "system") || is_numeric($pconfig['attribute'])) {
305
		echo "<input type='hidden' name='attribute' id='attribute' value='{$pconfig['attribute']}' >\n";
306
	}
307
	echo "<input type='hidden' name='friendlyiface' id='friendlyiface' value='{$pconfig['friendlyiface']}' >\n";
308
	?>
309
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
310
				<tr>
311
					<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit gateway"); ?></td>
312
				</tr>	
313
                <tr> 
314
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Interface"); ?></td>
315
                  <td width="78%" class="vtable">
316
		 	<select name='interface' class='formselect' >
317

    
318
		<?php 
319
                      	$interfaces = get_configured_interface_with_descr(false, true);
320
			foreach ($interfaces as $iface => $ifacename) {
321
				echo "<option value=\"{$iface}\"";
322
				if ($iface == $pconfig['friendlyiface'])
323
					echo " selected";
324
				echo ">" . htmlspecialchars($ifacename) . "</option>";
325
			}
326
			if (is_package_installed("openbgpd") == 1) {
327
				echo "<option value=\"bgpd\"";
328
				if ($pconfig['interface'] == "bgpd") 
329
					echo " selected";
330
				echo ">" . gettext("Use BGPD") . "</option>";
331
			}
332
 		  ?>
333
                    </select> <br>
334
                    <span class="vexpl"><?=gettext("Choose which interface this gateway applies to."); ?></span></td>
335
                </tr>
336
                <tr>
337
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Name"); ?></td>
338
                  <td width="78%" class="vtable"> 
339
                    <input name="name" type="text" class="formfld unknown" id="name" size="20" value="<?=htmlspecialchars($pconfig['name']);?>"> 
340
                    <br> <span class="vexpl"><?=gettext("Gateway name"); ?></span></td>
341
                </tr>
342
		<tr>
343
                  <td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
344
                  <td width="78%" class="vtable"> 
345
                    <input name="gateway" type="text" class="formfld host" id="gateway" size="40" value="<?php if ($pconfig['dynamic']) echo "dynamic"; else echo $pconfig['gateway']; ?>">
346
                    <br> <span class="vexpl"><?=gettext("Gateway IP address"); ?></span></td>
347
                </tr>
348
		<tr>
349
		  <td width="22%" valign="top" class="vncell"><?=gettext("Default Gateway"); ?></td>
350
		  <td width="78%" class="vtable">
351
			<input name="defaultgw" type="checkbox" id="defaultgw" value="yes" <?php if ($pconfig['defaultgw'] == true) echo "checked"; ?> />
352
			<strong><?=gettext("Default Gateway"); ?></strong><br />
353
			<?=gettext("This will select the above gateway as the default gateway"); ?>
354
		  </td>
355
		</tr>
356
		<tr>
357
		  <td width="22%" valign="top" class="vncell"><?=gettext("Disable Gateway Monitoring"); ?></td>
358
		  <td width="78%" class="vtable">
359
			<input name="monitor_disable" type="checkbox" id="monitor_disable" value="yes" <?php if ($pconfig['monitor_disable'] == true) echo "checked"; ?> onClick="monitor_change()" />
360
			<strong><?=gettext("Disable Gateway Monitoring"); ?></strong><br />
361
			<?=gettext("This will consider this gateway as always being up"); ?>
362
		  </td>
363
		</tr>
364
		<tr>
365
		  <td width="22%" valign="top" class="vncell"><?=gettext("Monitor IP"); ?></td>
366
		  <td width="78%" class="vtable">
367
			<?php
368
				if ($pconfig['gateway'] == $pconfig['monitor'])
369
					$monitor = "";
370
				else
371
					$monitor = htmlspecialchars($pconfig['monitor']);
372
			?>
373
			<input name="monitor" type="text" id="monitor" value="<?php echo $monitor; ?>" />
374
			<strong><?=gettext("Alternative monitor IP"); ?></strong> <br />
375
			<?=gettext("Enter an alternative address here to be used to monitor the link. This is used for the " .
376
			"quality RRD graphs as well as the load balancer entries. Use this if the gateway does not respond " .
377
			"to ICMP echo requests (pings)"); ?>.</strong>
378
			<br />
379
		  </td>
380
		</tr>
381
		<tr>
382
		  <td width="22%" valign="top" class="vncell"><?=gettext("Advanced");?></td>
383
		  <td width="78%" class="vtable">
384
			<?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'])); ?>
385
			<div id="showadvgatewaybox" <? if ($showbutton) echo "style='display:none'"; ?>>
386
				<input type="button" onClick="show_advanced_gateway()" value="Advanced"></input> - Show advanced option</a>
387
			</div>
388
			<div id="showgatewayadv" <? if (!$showbutton) echo "style='display:none'"; ?>>
389
                        <table class="tabcont" width="100%" border="0" cellspacing="0" cellpadding="6">
390
			<tr>
391
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Weight");?></td>
392
                                <td width="78%" class="vtable">
393
					<select name='weight' class='formfldselect' id='weight'>
394
				<?php
395
					for ($i = 1; $i < 6; $i++) {
396
                                        	$selected = "";
397
                                        	if ($pconfig['weight'] == $i)
398
                                                	$selected = "selected";
399
                                        	echo "<option value='{$i}' {$selected} >{$i}</option>";
400
                                	}
401
				?>
402
					</select>
403
					<br /><?=gettext("Weight for this gateway when used in a Gateway Group.");?> <br />
404
		   		</td>
405
			</tr>
406
                        <tr>
407
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Latency thresholds");?></td>
408
                                <td width="78%" class="vtable">
409
                                <?=gettext("From");?>
410
                                    <input name="latencylow" type="text" class="formfld unknown" id="latencylow" size="2"
411
                                        value="<?=htmlspecialchars($pconfig['latencylow']);?>">
412
                                <?=gettext("To");?>
413
                                    <input name="latencyhigh" type="text" class="formfld unknown" id="latencyhigh" size="2"
414
                                        value="<?=htmlspecialchars($pconfig['latencyhigh']);?>">
415
                                    <br> <span class="vexpl"><?=gettext("These define the low and high water marks for latency in milliseconds.");?></span></td>
416
                                </td>
417
                        </tr>
418
                        <tr>
419
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Packet Loss thresholds");?></td>
420
                                <td width="78%" class="vtable">
421
                                <?=gettext("From");?>
422
                                    <input name="losslow" type="text" class="formfld unknown" id="losslow" size="2"
423
                                        value="<?=htmlspecialchars($pconfig['losslow']);?>">
424
                                <?=gettext("To");?>
425
                                    <input name="losshigh" type="text" class="formfld unknown" id="losshigh" size="2"
426
                                        value="<?=htmlspecialchars($pconfig['losshigh']);?>">
427
                                    <br> <span class="vexpl"><?=gettext("These define the low and high water marks for packet loss in %.");?></span></td>
428
                                </td>
429
                        </tr>
430
			<tr>
431
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Down");?></td>
432
                                <td width="78%" class="vtable">
433
                                    <input name="down" type="text" class="formfld unknown" id="down" size="2"
434
                                        value="<?=htmlspecialchars($pconfig['down']);?>">
435
                                    <br> <span class="vexpl"><?=gettext("This defines the down time for the alarm to fire, in seconds.");?></span></td>
436
                                </td>
437
                        </tr>
438
			<tr>
439
                                <td width="22%" valign="top" class="vncellreq"><?=gettext("Frequency Probe");?></td>
440
                                <td width="78%" class="vtable">
441
                                    <input name="interval" type="text" class="formfld unknown" id="interval" size="2"
442
                                        value="<?=htmlspecialchars($pconfig['interval']);?>">
443
                                    <br> <span class="vexpl"><?=gettext("This defines the frequency in seconds that an icmp probe will be sent. Default is 1 second.");?></span></td>
444
                                </td>
445
                        </tr>
446
                        </table>
447
			</div>
448
		   </td>
449
		</tr>
450
		<tr>
451
                  <td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
452
                  <td width="78%" class="vtable"> 
453
                    <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
454
                    <br> <span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)"); ?>.</span></td>
455
                </tr>
456
                <tr>
457
                  <td width="22%" valign="top">&nbsp;</td>
458
                  <td width="78%"> 
459
                    <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>"> <input type="button" value="<?=gettext("Cancel");?>" class="formbtn"  onclick="history.back()">
460
                    <?php if (isset($id) && $a_gateways[$id]): ?>
461
                    <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>">
462
                    <?php endif; ?>
463
                  </td>
464
                </tr>
465
              </table>
466
</form>
467
<?php include("fend.inc"); ?>
468
<script language="JavaScript">
469
monitor_change();
470
</script>
471
</body>
472
</html>
(194-194/226)