Project

General

Profile

Download (19.8 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['interface'] = $a_gateways[$id]['interface'];
70
	$pconfig['friendlyiface'] = $a_gateways[$id]['friendlyiface'];
71
	if (isset($a_gateways[$id]['dynamic']))
72
		$pconfig['dynamic'] = true;
73
	$pconfig['gateway'] = $a_gateways[$id]['gateway'];
74
	$pconfig['defaultgw'] = isset($a_gateways[$id]['defaultgw']);
75
	$pconfig['latencylow'] = $a_gateway_item[$id]['latencylow'];
76
        $pconfig['latencyhigh'] = $a_gateway_item[$id]['latencyhigh'];
77
        $pconfig['losslow'] = $a_gateway_item[$id]['losslow'];
78
        $pconfig['losshigh'] = $a_gateway_item[$id]['losshigh'];
79
        $pconfig['down'] = $a_gateway_item[$id]['down'];
80
	$pconfig['monitor'] = $a_gateways[$id]['monitor'];
81
	$pconfig['descr'] = $a_gateways[$id]['descr'];
82
	$pconfig['attribute'] = $a_gateways[$id]['attribute'];
83
}
84

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

    
90
if ($_POST) {
91

    
92
	unset($input_errors);
93

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

    
98
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
99

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

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

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

    
167
	/* input validation */
168
        if($_POST['latencylow']) {
169
                if (! is_numeric($_POST['latencylow'])) {
170
                        $input_errors[] = gettext("The low latency watermark needs to be a numeric value.");
171
                }
172
        }
173

    
174
        if($_POST['latencyhigh']) {
175
                if (! is_numeric($_POST['latencyhigh'])) {
176
                        $input_errors[] = gettext("The high latency watermark needs to be a numeric value.");
177
                }
178
        }
179
        if($_POST['losslow']) {
180
                if (! is_numeric($_POST['losslow'])) {
181
                        $input_errors[] = gettext("The low loss watermark needs to be a numeric value.");
182
                }
183
        }
184
        if($_POST['losshigh']) {
185
                if (! is_numeric($_POST['losshigh'])) {
186
                        $input_errors[] = gettext("The high loss watermark needs to be a numeric value.");
187
                }
188
        }
189

    
190
        if(($_POST['latencylow']) && ($_POST['latencyhigh'])){
191
                if(($_POST['latencylow'] > $_POST['latencyhigh'])) {
192
                        $input_errors[] = gettext("The High latency watermark needs to be higher then the low latency watermark");
193
                }
194
        }
195

    
196
        if(($_POST['losslow']) && ($_POST['losshigh'])){
197
                if($_POST['losslow'] > $_POST['losshigh']) {
198
                        $input_errors[] = gettext("The High packet loss watermark needs to be higher then the low packet loss watermark");
199
                }
200
        }
201
	if($_POST['down']) {
202
                if (! is_numeric($_POST['down']) || $_POST['down'] < 1) {
203
                        $input_errors[] = gettext("The low latency watermark needs to be a numeric value.");
204
                }
205
        }
206

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

    
219

    
220
		$reloadif = "";
221
		$gateway = array();
222

    
223
		if (empty($_POST['interface']))
224
			$gateway['interface'] = $pconfig['friendlyiface'];
225
		else
226
			$gateway['interface'] = $_POST['interface'];
227
		if (is_ipaddr($_POST['gateway']))
228
			$gateway['gateway'] = $_POST['gateway'];
229
		else
230
			$gateway['gateway'] = "dynamic";
231
		$gateway['name'] = $_POST['name'];
232
		$gateway['weight'] = $_POST['weight'];
233
		$gateway['descr'] = $_POST['descr'];
234
		if (is_ipaddr($_POST['monitor']))
235
			$gateway['monitor'] = $_POST['monitor'];
236

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

    
248
		if ($_POST['latencylow'])
249
			$gateway['latencylow'] = $_POST['latencylow'];
250
		if ($_POST['latencyhigh'])
251
               		$gateway['latencyhigh'] = $_POST['latencyhigh'];
252
		if ($_POST['losslow'])
253
              			$gateway['losslow'] = $_POST['losslow'];
254
		if ($_POST['losshigh'])
255
               		$gateway['losshigh'] = $_POST['losshigh'];
256
		if ($_POST['down'])
257
               		$gateway['down'] = $_POST['down'];
258

    
259
		/* when saving the manual gateway we use the attribute which has the corresponding id */
260
		if (isset($id) && $a_gateway_item[$id])
261
			$a_gateway_item[$id] = $gateway;
262
		else
263
			$a_gateway_item[] = $gateway;
264

    
265
		mark_subsystem_dirty('staticroutes');
266
	
267
		write_config();
268

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

    
284

    
285
$pgtitle = array(gettext("System"),gettext("Gateways"),gettext("Edit gateway"));
286
$statusurl = "status_gateways.php";
287

    
288
include("head.inc");
289

    
290
?>
291

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

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

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