Project

General

Profile

Download (16.2 KB) Statistics
| Branch: | Tag: | Revision:
1 5da58a38 Renato Botelho
<?php
2 5b237745 Scott Ullrich
/*
3
	system_routes_edit.php
4
	part of m0n0wall (http://m0n0.ch/wall)
5 5da58a38 Renato Botelho
6 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
7 38936dc7 Ermal Lu?i
	Copyright (C) 2010 Scott Ullrich
8 5b237745 Scott Ullrich
	All rights reserved.
9 5da58a38 Renato Botelho
10 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12 5da58a38 Renato Botelho
13 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15 5da58a38 Renato Botelho
16 5b237745 Scott Ullrich
	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 5da58a38 Renato Botelho
20 5b237745 Scott Ullrich
	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 1d333258 Scott Ullrich
/*
32
	pfSense_MODULE:	routing
33
*/
34 5b237745 Scott Ullrich
35 6b07c15a Matthew Grooms
##|+PRIV
36
##|*IDENT=page-system-staticroutes-editroute
37
##|*NAME=System: Static Routes: Edit route page
38
##|*DESCR=Allow access to the 'System: Static Routes: Edit route' page.
39
##|*MATCH=system_routes_edit.php*
40
##|-PRIV
41
42 4504a769 Ermal Lu?i
function staticroutecmp($a, $b) {
43
	return strcmp($a['network'], $b['network']);
44
}
45
46 0d64af59 Ermal Lu?i
function staticroutes_sort() {
47 5da58a38 Renato Botelho
	global $g, $config;
48 0d64af59 Ermal Lu?i
49 5da58a38 Renato Botelho
	if (!is_array($config['staticroutes']['route']))
50
		return;
51 0d64af59 Ermal Lu?i
52 5da58a38 Renato Botelho
	usort($config['staticroutes']['route'], "staticroutecmp");
53 0d64af59 Ermal Lu?i
}
54 6b07c15a Matthew Grooms
55 4fd2fed2 jim-p
require_once("guiconfig.inc");
56
require_once("filter.inc");
57
require_once("util.inc");
58
require_once("gwlb.inc");
59 5b237745 Scott Ullrich
60
if (!is_array($config['staticroutes']['route']))
61
	$config['staticroutes']['route'] = array();
62
63
$a_routes = &$config['staticroutes']['route'];
64 6fdea6a2 smos
$a_gateways = return_gateways_array(true, true);
65 5b237745 Scott Ullrich
66
$id = $_GET['id'];
67
if (isset($_POST['id']))
68
	$id = $_POST['id'];
69
70 18f7352b Seth Mos
if (isset($_GET['dup'])) {
71
	$id = $_GET['dup'];
72
}
73
74 5b237745 Scott Ullrich
if (isset($id) && $a_routes[$id]) {
75 5da58a38 Renato Botelho
	list($pconfig['network'],$pconfig['network_subnet']) =
76 5b237745 Scott Ullrich
		explode('/', $a_routes[$id]['network']);
77
	$pconfig['gateway'] = $a_routes[$id]['gateway'];
78
	$pconfig['descr'] = $a_routes[$id]['descr'];
79 bfe407e5 Warren Baker
	$pconfig['disabled'] = isset($a_routes[$id]['disabled']);
80 5b237745 Scott Ullrich
}
81
82 18f7352b Seth Mos
if (isset($_GET['dup']))
83
	unset($id);
84
85 5b237745 Scott Ullrich
if ($_POST) {
86
87 f0867239 Renato Botelho
	global $aliastable;
88
89 5b237745 Scott Ullrich
	unset($input_errors);
90
	$pconfig = $_POST;
91
92
	/* input validation */
93 dde169d9 Vinicius Coque
	$reqdfields = explode(" ", "network network_subnet gateway");
94 38fb1109 Vinicius Coque
	$reqdfieldsn = explode(",",
95
			gettext("Destination network") . "," .
96
			gettext("Destination network bit count") . "," .
97 5da58a38 Renato Botelho
			gettext("Gateway"));
98
99 5b237745 Scott Ullrich
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
100 5da58a38 Renato Botelho
101 f898c1a9 jim-p
	if (($_POST['network'] && !is_ipaddr($_POST['network']) && !is_alias($_POST['network']))) {
102 ad700f39 Seth Mos
		$input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
103 5b237745 Scott Ullrich
	}
104
	if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
105 169e0008 Carlos Eduardo Ramos
		$input_errors[] = gettext("A valid destination network bit count must be specified.");
106 5b237745 Scott Ullrich
	}
107 ad700f39 Seth Mos
	if (($_POST['gateway']) && is_ipaddr($_POST['network'])) {
108 a529aced Ermal
		if (!isset($a_gateways[$_POST['gateway']]))
109 169e0008 Carlos Eduardo Ramos
			$input_errors[] = gettext("A valid gateway must be specified.");
110 1831a00d Seth Mos
		if(!validate_address_family($_POST['network'], lookup_gateway_ip_by_name($_POST['gateway'])))
111
			$input_errors[] = gettext("The gateway '{$a_gateways[$_POST['gateway']]['gateway']}' is a different Address Family as network '{$_POST['network']}'.");
112 5b237745 Scott Ullrich
	}
113
114
	/* check for overlaps */
115 f898c1a9 jim-p
	$current_targets = get_staticroutes(true);
116
	$new_targets = array();
117 14f565b4 Seth Mos
	if(is_ipaddrv6($_POST['network'])) {
118 88cc00db Ermal
		$osn = gen_subnetv6($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
119 f898c1a9 jim-p
		$new_targets[] = $osn;
120 14f565b4 Seth Mos
	}
121 71f4a2b7 smos
	if (is_ipaddrv4($_POST['network'])) {
122 1831a00d Seth Mos
		if($_POST['network_subnet'] > 32)
123
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
124 f898c1a9 jim-p
		else {
125 1831a00d Seth Mos
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
126 f898c1a9 jim-p
			$new_targets[] = $osn;
127
		}
128
	} elseif (is_alias($_POST['network'])) {
129
		$osn = $_POST['network'];
130 8543a5bb Renato Botelho
		$fqdn_found = 0;
131 f0867239 Renato Botelho
		foreach (preg_split('/\s+/', $aliastable[$osn]) as $tgt) {
132
			if (!is_ipaddr($tgt)) {
133 8543a5bb Renato Botelho
				if ($fqdn_found === 0) {
134
					$input_errors[] = sprintf(gettext("The alias (%s) has one or more FQDNs configured and cannot be used to configure a static route."), $_POST['network']);
135
					$fqdn_found = 1;
136
				}
137
				continue;
138
			}
139 87f61101 Renato Botelho
			if (is_ipaddrv4($tgt))
140 f898c1a9 jim-p
				$tgt .= "/32";
141 87f61101 Renato Botelho
			if (is_ipaddrv6($tgt))
142 71f4a2b7 smos
				$tgt .= "/128";
143 f898c1a9 jim-p
			if (!is_subnet($tgt))
144
				continue;
145 06392e40 jim-p
			if (!is_subnetv6($tgt))
146 71f4a2b7 smos
				continue;
147 f898c1a9 jim-p
			$new_targets[] = $tgt;
148
		}
149 14f565b4 Seth Mos
	}
150 f898c1a9 jim-p
	if (!isset($id))
151
		$id = count($a_routes);
152
	$oroute = $a_routes[$id];
153 71f4a2b7 smos
	$old_targets = array();
154 f898c1a9 jim-p
	if (!empty($oroute)) {
155
		if (is_alias($oroute['network'])) {
156
			foreach (filter_expand_alias_array($oroute['network']) as $tgt) {
157 ef593cd3 Renato Botelho
				if (is_ipaddrv4($tgt))
158 f898c1a9 jim-p
					$tgt .= "/32";
159 ef593cd3 Renato Botelho
				else if (is_ipaddrv6($tgt))
160
					$tgt .= "/128";
161 f898c1a9 jim-p
				if (!is_subnet($tgt))
162
					continue;
163
				$old_targets[] = $tgt;
164
			}
165
		} else {
166
			$old_targets[] = $oroute['network'];
167 5b237745 Scott Ullrich
		}
168
	}
169
170 f898c1a9 jim-p
	$overlaps = array_intersect($current_targets, $new_targets);
171
	$overlaps = array_diff($overlaps, $old_targets);
172
	if (count($overlaps)) {
173
		$input_errors[] = gettext("A route to these destination networks already exists") . ": " . implode(", ", $overlaps);
174
	}
175
176 74889b22 Renato Botelho
	if (is_array($config['interfaces'])) {
177
		foreach ($config['interfaces'] as $if) {
178
			if (is_ipaddrv4($_POST['network'])
179
				&& isset($if['ipaddr']) && isset($if['subnet'])
180
				&& is_ipaddrv4($if['ipaddr']) && is_numeric($if['subnet'])
181
				&& ($_POST['network_subnet'] == $if['subnet'])
182
				&& (gen_subnet($_POST['network'], $_POST['network_subnet']) == gen_subnet($if['ipaddr'], $if['subnet'])))
183
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
184
185
			else if (is_ipaddrv6($_POST['network'])
186
				&& isset($if['ipaddrv6']) && isset($if['subnetv6'])
187
				&& is_ipaddrv6($if['ipaddrv6']) && is_numeric($if['subnetv6'])
188
				&& ($_POST['network_subnet'] == $if['subnetv6'])
189
				&& (gen_subnetv6($_POST['network'], $_POST['network_subnet']) == gen_subnetv6($if['ipaddrv6'], $if['subnetv6'])))
190
					$input_errors[] = sprintf(gettext("This network conflicts with address configured on interface %s."), $if['descr']);
191
		}
192
	}
193
194 5b237745 Scott Ullrich
	if (!$input_errors) {
195
		$route = array();
196
		$route['network'] = $osn;
197
		$route['gateway'] = $_POST['gateway'];
198
		$route['descr'] = $_POST['descr'];
199 bfe407e5 Warren Baker
		if ($_POST['disabled'])
200
			$route['disabled'] = true;
201
		else
202
			unset($route['disabled']);
203 5b237745 Scott Ullrich
204 f898c1a9 jim-p
		if (file_exists("{$g['tmp_path']}/.system_routes.apply"))
205
			$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
206
		else
207
			$toapplylist = array();
208 e8471084 Ermal
		$a_routes[$id] = $route;
209
210
		if (!empty($oroute)) {
211 f898c1a9 jim-p
			$delete_targets = array_diff($old_targets, $new_targets);
212
			if (count($delete_targets))
213
				foreach ($delete_targets as $dts) {
214
					if(is_ipaddrv6($dts))
215
						$family = "-inet6";
216 5da58a38 Renato Botelho
					$toapplylist[] = "/sbin/route delete {$family} {$dts}";
217 f898c1a9 jim-p
				}
218 e8471084 Ermal
		}
219
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
220 0e3aa71c Erik Fonnesbeck
		staticroutes_sort();
221 5da58a38 Renato Botelho
222 a368a026 Ermal Lu?i
		mark_subsystem_dirty('staticroutes');
223 5da58a38 Renato Botelho
224 5b237745 Scott Ullrich
		write_config();
225 5da58a38 Renato Botelho
226 5b237745 Scott Ullrich
		header("Location: system_routes.php");
227
		exit;
228
	}
229
}
230 4df96eff Scott Ullrich
231 169e0008 Carlos Eduardo Ramos
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
232 b32dd0a6 jim-p
$shortcut_section = "routing";
233 4df96eff Scott Ullrich
include("head.inc");
234 5b237745 Scott Ullrich
?>
235 4df96eff Scott Ullrich
236 5b237745 Scott Ullrich
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
237 87744d53 Darren Embry
<script type="text/javascript" src="/javascript/jquery.ipv4v6ify.js"></script>
238 dcdff6fa Colin Fleming
<script type="text/javascript" src="/javascript/autosuggest.js"></script>
239
<script type="text/javascript" src="/javascript/suggestions.js"></script>
240 f898c1a9 jim-p
<?php include("fbegin.inc");?>
241 5b237745 Scott Ullrich
<?php if ($input_errors) print_input_errors($input_errors); ?>
242 5da58a38 Renato Botelho
	<form action="system_routes_edit.php" method="post" name="iform" id="iform">
243
		<table width="100%" border="0" cellpadding="6" cellspacing="0" summary="system routes edit">
244
			<tr>
245
				<td colspan="2" valign="top" class="listtopic"><?=gettext("Edit route entry"); ?></td>
246
			</tr>
247
			<tr>
248
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Destination network"); ?></td>
249
				<td width="78%" class="vtable">
250
					<input name="network" type="text" class="formfldalias ipv4v6" id="network" size="20" value="<?=htmlspecialchars($pconfig['network']);?>" />
251
					/
252
					<select name="network_subnet" class="formselect ipv4v6" id="network_subnet">
253
					<?php for ($i = 129; $i >= 1; $i--): ?>
254
						<option value="<?=$i;?>" <?php if ($i == $pconfig['network_subnet']) echo "selected=\"selected\""; ?>>
255
							<?=$i;?>
256
						</option>
257
					<?php endfor; ?>
258
					</select>
259
					<br/><span class="vexpl"><?=gettext("Destination network for this static route"); ?></span>
260
				</td>
261
			</tr>
262
			<tr>
263
				<td width="22%" valign="top" class="vncellreq"><?=gettext("Gateway"); ?></td>
264
				<td width="78%" class="vtable">
265
					<select name="gateway" id="gateway" class="formselect">
266
					<?php
267
						foreach ($a_gateways as $gateway) {
268
							echo "<option value='{$gateway['name']}' ";
269
							if ($gateway['name'] == $pconfig['gateway'])
270
								echo "selected=\"selected\"";
271
							echo ">" . htmlspecialchars($gateway['name']) . " - " . htmlspecialchars($gateway['gateway']) . "</option>\n";
272
						}
273
					?>
274
					</select> <br />
275
					<div id='addgwbox'>
276
						<?=gettext("Choose which gateway this route applies to or"); ?> <a onclick="show_add_gateway();" href="#"><?=gettext("add a new one.");?></a>
277
					</div>
278
					<div id='notebox'>
279
					</div>
280
					<div style="display:none" id="status">
281
					</div>
282
					<div style="display:none" id="addgateway">
283
						<table border="1" style="background:#990000; border-style: none none none none; width:225px;" summary="add gateway">
284
							<tr>
285
								<td>
286
									<table bgcolor="#990000" cellpadding="1" cellspacing="1" summary="add">
287
										<tr><td>&nbsp;</td></tr>
288
										<tr>
289
											<td colspan="2" align="center"><b><font color="white"><?=gettext("Add new gateway:"); ?></font></b></td>
290
										</tr>
291
										<tr><td>&nbsp;</td></tr>
292
										<tr>
293
											<td width="45%" align="right"><font color="white"><?=gettext("Default gateway:"); ?></font></td><td><input type="checkbox" id="defaultgw" name="defaultgw" /></td>
294
										</tr>
295
										<tr>
296
											<td width="45%" align="right"><font color="white"><?=gettext("Interface:"); ?></font></td>
297
											<td>
298
												<select name="addinterfacegw" id="addinterfacegw">
299 38936dc7 Ermal Lu?i
												<?php $gwifs = get_configured_interface_with_descr();
300
													foreach($gwifs as $fif => $dif)
301
														echo "<option value=\"{$fif}\">{$dif}</option>\n";
302
												?>
303 5da58a38 Renato Botelho
												</select>
304
											</td>
305
										</tr>
306
										<tr>
307
											<td align="right"><font color="white"><?=gettext("Gateway Name:"); ?></font></td><td><input id="name" name="name" value="GW" /></td>
308
										</tr>
309
										<tr>
310
											<td align="right"><font color="white"><?=gettext("Gateway IP:"); ?></font></td><td><input id="gatewayip" name="gatewayip" /></td>
311
										</tr>
312
										<tr>
313
											<td align="right"><font color="white"><?=gettext("Description:"); ?></font></td><td><input id="gatewaydescr" name="gatewaydescr" /></td>
314
										</tr>
315
										<tr><td>&nbsp;</td></tr>
316
										<tr>
317
											<td colspan="2" align="center">
318
												<div id='savebuttondiv'>
319
													<input type="hidden" name="addrtype" id="addrtype" value="IPv4" />
320
													<input id="gwsave" type="button" value="<?=gettext("Save Gateway"); ?>" onclick='hide_add_gatewaysave();' />
321
													<input id="gwcancel" type="button" value="<?=gettext("Cancel"); ?>" onclick='hide_add_gateway();' />
322
												</div>
323
											</td>
324
										</tr>
325
										<tr><td>&nbsp;</td></tr>
326
									</table>
327
								</td>
328
							</tr>
329
						</table>
330
					</div>
331
				</td>
332
			</tr>
333
			<tr>
334
				<td width="22%" valign="top" class="vncell"><?=gettext("Disabled");?></td>
335
				<td width="78%" class="vtable">
336
					<input name="disabled" type="checkbox" id="disabled" value="yes" <?php if ($pconfig['disabled']) echo "checked=\"checked\""; ?> />
337
					<strong><?=gettext("Disable this static route");?></strong><br />
338
					<span class="vexpl"><?=gettext("Set this option to disable this static route without removing it from the list.");?></span>
339
				</td>
340
			</tr>
341
			<tr>
342
				<td width="22%" valign="top" class="vncell"><?=gettext("Description"); ?></td>
343
				<td width="78%" class="vtable">
344
					<input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>" />
345
					<br/><span class="vexpl"><?=gettext("You may enter a description here for your reference (not parsed)."); ?></span>
346
				</td>
347
			</tr>
348
			<tr>
349
				<td width="22%" valign="top">&nbsp;</td>
350
				<td width="78%">
351
					<input id="save" name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>" /> <input id="cancel" type="button" value="<?=gettext("Cancel"); ?>" class="formbtn"  onclick="history.back()" />
352
					<?php if (isset($id) && $a_routes[$id]): ?>
353
						<input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
354
					<?php endif; ?>
355
				</td>
356
			</tr>
357
		</table>
358
	</form>
359 38936dc7 Ermal Lu?i
<script type="text/javascript">
360 dcdff6fa Colin Fleming
//<![CDATA[
361 5da58a38 Renato Botelho
	var gatewayip;
362
	var name;
363
	function show_add_gateway() {
364
		document.getElementById("addgateway").style.display = '';
365
		document.getElementById("addgwbox").style.display = 'none';
366
		document.getElementById("gateway").style.display = 'none';
367
		document.getElementById("save").style.display = 'none';
368
		document.getElementById("cancel").style.display = 'none';
369
		document.getElementById("gwsave").style.display = '';
370
		document.getElementById("gwcancel").style.display = '';
371
		jQuery('#notebox').html("");
372
	}
373
	function hide_add_gateway() {
374
		document.getElementById("addgateway").style.display = 'none';
375
		document.getElementById("addgwbox").style.display = '';
376
		document.getElementById("gateway").style.display = '';
377
		document.getElementById("save").style.display = '';
378
		document.getElementById("cancel").style.display = '';
379
		document.getElementById("gwsave").style.display = '';
380
		document.getElementById("gwcancel").style.display = '';
381
	}
382
	function hide_add_gatewaysave() {
383
		document.getElementById("addgateway").style.display = 'none';
384
		jQuery('#status').html('<img src="/themes/metallic/images/misc/loader.gif"> One moment please...');
385
		var iface = jQuery('#addinterfacegw').val();
386
		name = jQuery('#name').val();
387
		var descr = jQuery('#gatewaydescr').val();
388
		gatewayip = jQuery('#gatewayip').val();
389
		addrtype = jQuery('#addrtype').val();
390
		var defaultgw = '';
391
		if (jQuery('#defaultgw').checked)
392
			defaultgw = 'yes';
393
		var url = "system_gateways_edit.php";
394
		var pars = 'isAjax=true&defaultgw=' + escape(defaultgw) + '&interface=' + escape(iface) + '&name=' + escape(name) + '&descr=' + escape(descr) + '&gateway=' + escape(gatewayip) + '&type=' + escape(addrtype);
395
		jQuery.ajax(
396
			url,
397
		{
398
			type: 'post',
399
				data: pars,
400
				error: report_failure,
401
				complete: save_callback
402
		});
403
	}
404
	function addOption(selectbox,text,value)
405
	{
406
		var optn = document.createElement("OPTION");
407
		optn.text = text;
408
		optn.value = value;
409
		selectbox.append(optn);
410
		selectbox.prop('selectedIndex',selectbox.children('option').length-1);
411
		jQuery('#notebox').html("<p><strong><?=gettext("NOTE:");?><\/strong> <?php printf(gettext("You can manage Gateways %shere%s."), "<a target='_blank' href='system_gateways.php'>", "<\/a>");?> <\/strong><\/p>");
412
	}
413
	function report_failure() {
414
		alert("<?=gettext("Sorry, we could not create your gateway at this time."); ?>");
415
		hide_add_gateway();
416
	}
417
	function save_callback(transport) {
418
		var response = transport.responseText;
419
		if (response) {
420
			document.getElementById("addgateway").style.display = 'none';
421
			hide_add_gateway();
422
			jQuery('#status').html('');
423
			addOption(jQuery('#gateway'), name, name);
424
		} else {
425
			report_failure();
426
		}
427
	}
428
	var addressarray = <?= json_encode(get_alias_list(array("host", "network"))) ?>;
429
	var oTextbox1 = new AutoSuggestControl(document.getElementById("network"), new StateSuggestions(addressarray));
430 dcdff6fa Colin Fleming
//]]>
431 5da58a38 Renato Botelho
</script>
432 5b237745 Scott Ullrich
<?php include("fend.inc"); ?>
433
</body>
434
</html>