Project

General

Profile

Download (9.23 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 ce77a9c4 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 f3bb71cf Sjon Hortensius
216
if ($input_errors)
217
	print_input_errors($input_errors);
218
219
require('classes/Form.class.php');
220
$form = new Form;
221
222
if (isset($id) && $a_routes[$id]) {
223
	$form->addGlobal(new Form_Input(
224
		'id',
225
		null,
226
		'hidden',
227
		$id
228
	));
229
}
230
231
$section = new Form_Section('Edit route entry');
232
233
$section->addInput(new Form_IpAddress(
234
	'network_subnet',
235
	'Destination network',
236
	$pconfig['network']
237
))->addMask('network_subnet', $pconfig['network_subnet'])->setHelp('Destination network for this static route');
238
239
$allGateways = array_combine(
240
	array_map(function($g){ return $g['name']; }, $a_gateways),
241
	array_map(function($g){ return $g['name'] .' - '. $g['gateway']; }, $a_gateways)
242
);
243
$section->addInput(new Form_Select(
244
	'gateway',
245
	'Gateway',
246
	$pconfig['gateway'],
247
	$allGateways + ['_pfNewGateway_' => 'Create a new gateway below']
248
))->toggles(null)->setHelp('Choose which gateway this route applies to or add a new one');
249
250
$section->addInput(new Form_Checkbox(
251
	'disabled',
252
	'Disabled',
253
	'Disable this static route',
254
	$pconfig['disabled']
255
))->setHelp('Set this option to disable this static route without removing it from '.
256
	'the list.');
257
258
$section->addInput(new Form_Input(
259
	'descr',
260
	'Description',
261
	'text',
262
	htmlspecialchars($pconfig['descr'])
263
))->setHelp('You may enter a description here for your reference (not parsed).');
264
265
$form->add($section);
266
267
$section = new Form_Section('Add a new gateway');
268
$section->addClass('toggle-_pfNewGateway_ collapse');
269
270
$section->addInput(new Form_Checkbox(
271
	'defaultgw',
272
	'Default gateway',
273
	'Make this the default',
274
	false
275
));
276
277
$section->addInput(new Form_Select(
278
	'addinterfacegw',
279
	'Interface',
280
	null,
281
	get_configured_interface_with_descr()
282
));
283
284
$section->addInput(new Form_Input(
285
	'name',
286
	'Gateway Name',
287
	'text',
288
	'GW'
289
));
290
291
$section->addInput(new Form_Input(
292
	'gatewayip',
293
	'Gateway IP',
294
	'text'
295
));
296
297
$section->addInput(new Form_Input(
298
	'gatewaydescr',
299
	'Description',
300
	'text'
301
));
302
303
$form->add($section);
304
305
print $form;
306
307
include("foot.inc");