Project

General

Profile

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