Project

General

Profile

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