Project

General

Profile

Download (8.63 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	system_routes_edit.php
4
	part of m0n0wall (http://m0n0.ch/wall)
5
	part of pfSense
6
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
7
	Copyright (C) 2010 Scott Ullrich
8
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	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

    
21
	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
/*
33
	pfSense_MODULE:	routing
34
*/
35

    
36
##|+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
require_once("guiconfig.inc");
44
require_once("filter.inc");
45
require_once("util.inc");
46
require_once("gwlb.inc");
47

    
48
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/system_routes.php');
49

    
50
if (!is_array($config['staticroutes']['route']))
51
	$config['staticroutes']['route'] = array();
52

    
53
$a_routes = &$config['staticroutes']['route'];
54
$a_gateways = return_gateways_array(true, true);
55

    
56
if (is_numericint($_GET['id']))
57
	$id = $_GET['id'];
58
if (isset($_POST['id']) && is_numericint($_POST['id']))
59
	$id = $_POST['id'];
60

    
61
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
62
	$id = $_GET['dup'];
63

    
64
if (isset($id) && $a_routes[$id]) {
65
	list($pconfig['network'],$pconfig['network_subnet']) =
66
		explode('/', $a_routes[$id]['network']);
67
	$pconfig['gateway'] = $a_routes[$id]['gateway'];
68
	$pconfig['descr'] = $a_routes[$id]['descr'];
69
	$pconfig['disabled'] = isset($a_routes[$id]['disabled']);
70
}
71

    
72
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
73
	unset($id);
74

    
75
if ($_POST) {
76

    
77
	global $aliastable;
78

    
79
	unset($input_errors);
80
	$pconfig = $_POST;
81

    
82
	/* input validation */
83
	$reqdfields = explode(" ", "network network_subnet gateway");
84
	$reqdfieldsn = explode(",",
85
			gettext("Destination network") . "," .
86
			gettext("Destination network bit count") . "," .
87
			gettext("Gateway"));
88

    
89
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
90

    
91
	if (($_POST['network'] && !is_ipaddr($_POST['network']) && !is_alias($_POST['network']))) {
92
		$input_errors[] = gettext("A valid IPv4 or IPv6 destination network must be specified.");
93
	}
94
	if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) {
95
		$input_errors[] = gettext("A valid destination network bit count must be specified.");
96
	}
97
	if (($_POST['gateway']) && is_ipaddr($_POST['network'])) {
98
		if (!isset($a_gateways[$_POST['gateway']]))
99
			$input_errors[] = gettext("A valid gateway must be specified.");
100
		if(!validate_address_family($_POST['network'], $_POST['gateway']))
101
			$input_errors[] = gettext("The gateway '{$a_gateways[$_POST['gateway']]['gateway']}' is a different Address Family as network '{$_POST['network']}'.");
102
	}
103

    
104
	/* check for overlaps */
105
	$current_targets = get_staticroutes(true);
106
	$new_targets = array();
107
	if(is_ipaddrv6($_POST['network'])) {
108
		$osn = gen_subnetv6($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
109
		$new_targets[] = $osn;
110
	}
111
	if (is_ipaddrv4($_POST['network'])) {
112
		if($_POST['network_subnet'] > 32)
113
			$input_errors[] = gettext("A IPv4 subnet can not be over 32 bits.");
114
		else {
115
			$osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet'];
116
			$new_targets[] = $osn;
117
		}
118
	} elseif (is_alias($_POST['network'])) {
119
		$osn = $_POST['network'];
120
		foreach (preg_split('/\s+/', $aliastable[$osn]) as $tgt) {
121
			if (is_ipaddrv4($tgt))
122
				$tgt .= "/32";
123
			if (is_ipaddrv6($tgt))
124
				$tgt .= "/128";
125
			if (!is_subnet($tgt))
126
				continue;
127
			if (!is_subnetv6($tgt))
128
				continue;
129
			$new_targets[] = $tgt;
130
		}
131
	}
132
	if (!isset($id))
133
		$id = count($a_routes);
134
	$oroute = $a_routes[$id];
135
	$old_targets = array();
136
	if (!empty($oroute)) {
137
		if (is_alias($oroute['network'])) {
138
			foreach (filter_expand_alias_array($oroute['network']) as $tgt) {
139
				if (is_ipaddrv4($tgt))
140
					$tgt .= "/32";
141
				else if (is_ipaddrv6($tgt))
142
					$tgt .= "/128";
143
				if (!is_subnet($tgt))
144
					continue;
145
				$old_targets[] = $tgt;
146
			}
147
		} else {
148
			$old_targets[] = $oroute['network'];
149
		}
150
	}
151

    
152
	$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
	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
	if (!$input_errors) {
177
		$route = array();
178
		$route['network'] = $osn;
179
		$route['gateway'] = $_POST['gateway'];
180
		$route['descr'] = $_POST['descr'];
181
		if ($_POST['disabled'])
182
			$route['disabled'] = true;
183
		else
184
			unset($route['disabled']);
185

    
186
		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
		$a_routes[$id] = $route;
191

    
192
		if (!empty($oroute)) {
193
			$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
					$toapplylist[] = "/sbin/route delete {$family} {$dts}";
199
				}
200
		}
201
		file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist));
202

    
203
		mark_subsystem_dirty('staticroutes');
204

    
205
		write_config();
206

    
207
		header("Location: system_routes.php");
208
		exit;
209
	}
210
}
211

    
212
$pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route"));
213
$shortcut_section = "routing";
214
include("head.inc");
215

    
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
248
))->setHelp('Choose which gateway this route applies to or <a href="'.
249
	'/system_gateways_edit.php">add a new one first</a>');
250

    
251
$section->addInput(new Form_Checkbox(
252
	'disabled',
253
	'Disabled',
254
	'Disable this static route',
255
	$pconfig['disabled']
256
))->setHelp('Set this option to disable this static route without removing it from '.
257
	'the list.');
258

    
259
$section->addInput(new Form_Input(
260
	'descr',
261
	'Description',
262
	'text',
263
	htmlspecialchars($pconfig['descr'])
264
))->setHelp('You may enter a description here for your reference (not parsed).');
265

    
266
$form->add($section);
267

    
268
print $form;
269

    
270
include("foot.inc");
(214-214/241)