Project

General

Profile

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