Project

General

Profile

Download (8.83 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_routes.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * originally based on m0n0wall (http://m0n0.ch/wall)
12
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13
 * All rights reserved.
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 * http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27

    
28
##|+PRIV
29
##|*IDENT=page-system-staticroutes
30
##|*NAME=System: Static Routes
31
##|*DESCR=Allow access to the 'System: Static Routes' page.
32
##|*MATCH=system_routes.php*
33
##|-PRIV
34

    
35
require_once("guiconfig.inc");
36
require_once("functions.inc");
37
require_once("filter.inc");
38
require_once("shaper.inc");
39

    
40
init_config_arr(array('staticroutes', 'route'));
41
$a_routes = &$config['staticroutes']['route'];
42
$a_gateways = return_gateways_array(true, true, true);
43
$changedesc_prefix = gettext("Static Routes") . ": ";
44
unset($input_errors);
45

    
46
if ($_POST['apply']) {
47
	$pconfig = $_POST;
48
	$retval = 0;
49

    
50
	if (file_exists("{$g['tmp_path']}/.system_routes.apply")) {
51
		$toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply"));
52
		foreach ($toapplylist as $toapply) {
53
			mwexec("{$toapply}");
54
		}
55

    
56
		@unlink("{$g['tmp_path']}/.system_routes.apply");
57
	}
58

    
59
	$retval |= system_routing_configure();
60
	$retval |= filter_configure();
61
	/* reconfigure our gateway monitor */
62
	setup_gateways_monitor();
63

    
64
	if ($retval == 0) {
65
		clear_subsystem_dirty('staticroutes');
66
	}
67
}
68

    
69
function delete_static_route($id) {
70
	global $config, $a_routes, $changedesc_prefix, $a_gateways;
71

    
72
	if (!isset($a_routes[$id])) {
73
		return;
74
	}
75

    
76
	$targets = array();
77
	if (is_alias($a_routes[$id]['network'])) {
78
		foreach (filter_expand_alias_array($a_routes[$id]['network']) as $tgt) {
79
			if (is_ipaddrv4($tgt)) {
80
				$tgt .= "/32";
81
			} else if (is_ipaddrv6($tgt)) {
82
				$tgt .= "/128";
83
			}
84
			if (!is_subnet($tgt)) {
85
				continue;
86
			}
87
			$targets[] = $tgt;
88
		}
89
	} else {
90
		$targets[] = $a_routes[$id]['network'];
91
	}
92

    
93
	foreach ($targets as $tgt) {
94
		$family = (is_subnetv6($tgt) ? "-inet6" : "-inet");
95
		$gateway = $a_gateways[$a_routes[$id]['gateway']]['gateway'];
96
		mwexec("/sbin/route delete {$family} " . escapeshellarg($tgt) . " " . escapeshellarg($gateway));
97
	}
98

    
99
	unset($targets);
100
}
101

    
102
if ($_POST['act'] == "del") {
103
	if ($a_routes[$_POST['id']]) {
104
		$changedesc = $changedesc_prefix . sprintf(gettext("removed route to %s"), $a_routes[$_POST['id']]['network']);
105
		delete_static_route($_POST['id']);
106
		unset($a_routes[$_POST['id']]);
107
		write_config($changedesc);
108
		header("Location: system_routes.php");
109
		exit;
110
	}
111
}
112

    
113
if (isset($_POST['del_x'])) {
114
	/* delete selected routes */
115
	if (is_array($_POST['route']) && count($_POST['route'])) {
116
		$deleted_routes = "";
117
		foreach ($_POST['route'] as $routei) {
118
			$deleted_routes .= " " . $a_routes[$routei]['network'];
119
			delete_static_route($routei);
120
			unset($a_routes[$routei]);
121
		}
122
		$changedesc = $changedesc_prefix . sprintf(gettext("removed route to%s"), $deleted_routes);
123
		write_config($changedesc);
124
		header("Location: system_routes.php");
125
		exit;
126
	}
127

    
128
}
129

    
130
if ($_POST['act'] == "toggle") {
131
	if ($a_routes[$_POST['id']]) {
132
		$do_update_config = true;
133
		if (isset($a_routes[$_POST['id']]['disabled'])) {
134
			// Do not enable a route whose gateway is disabled
135
			if (isset($a_gateways[$a_routes[$_POST['id']]['gateway']]['disabled'])) {
136
				$do_update_config = false;
137
				$input_errors[] = $changedesc_prefix . sprintf(gettext("gateway is disabled, cannot enable route to %s"), $a_routes[$_POST['id']]['network']);
138
			} else {
139
				unset($a_routes[$_POST['id']]['disabled']);
140
				$changedesc = $changedesc_prefix . sprintf(gettext("enabled route to %s"), $a_routes[$_POST['id']]['network']);
141
			}
142
		} else {
143
			delete_static_route($_POST['id']);
144
			$a_routes[$_POST['id']]['disabled'] = true;
145
			$changedesc = $changedesc_prefix . sprintf(gettext("disabled route to %s"), $a_routes[$_POST['id']]['network']);
146
		}
147

    
148
		if ($do_update_config) {
149
			if (write_config($changedesc)) {
150
				mark_subsystem_dirty('staticroutes');
151
			}
152
			header("Location: system_routes.php");
153
			exit;
154
		}
155
	}
156
}
157

    
158
if($_POST['save']) {
159
	/* yuck - IE won't send value attributes for image buttons, while Mozilla does - so we use .x/.y to find move button clicks instead... */
160
	unset($movebtn);
161
	foreach ($_POST as $pn => $pd) {
162
		if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
163
			$movebtn = $matches[1];
164
			break;
165
		}
166
	}
167
	/* move selected routes before this route */
168
	if (isset($movebtn) && is_array($_POST['route']) && count($_POST['route'])) {
169
		$a_routes_new = array();
170

    
171
		/* copy all routes < $movebtn and not selected */
172
		for ($i = 0; $i < $movebtn; $i++) {
173
			if (!in_array($i, $_POST['route'])) {
174
				$a_routes_new[] = $a_routes[$i];
175
			}
176
		}
177

    
178
		/* copy all selected routes */
179
		for ($i = 0; $i < count($a_routes); $i++) {
180
			if ($i == $movebtn) {
181
				continue;
182
			}
183
			if (in_array($i, $_POST['route'])) {
184
				$a_routes_new[] = $a_routes[$i];
185
			}
186
		}
187

    
188
		/* copy $movebtn route */
189
		if ($movebtn < count($a_routes)) {
190
			$a_routes_new[] = $a_routes[$movebtn];
191
		}
192

    
193
		/* copy all routes > $movebtn and not selected */
194
		for ($i = $movebtn+1; $i < count($a_routes); $i++) {
195
			if (!in_array($i, $_POST['route'])) {
196
				$a_routes_new[] = $a_routes[$i];
197
			}
198
		}
199
		if (count($a_routes_new) > 0) {
200
			$a_routes = $a_routes_new;
201
		}
202

    
203
		if (write_config(gettext("Saved static routes configuration."))) {
204
			mark_subsystem_dirty('staticroutes');
205
		}
206
		header("Location: system_routes.php");
207
		exit;
208
	}
209
}
210

    
211
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Static Routes"));
212
$pglinks = array("", "system_gateways.php", "@self");
213
$shortcut_section = "routing";
214

    
215
include("head.inc");
216

    
217
if ($input_errors) {
218
	print_input_errors($input_errors);
219
}
220
if ($_POST['apply']) {
221
	print_apply_result_box($retval);
222
}
223
if (is_subsystem_dirty('staticroutes')) {
224
	print_apply_box(gettext("The static route configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
225
}
226

    
227
$tab_array = array();
228
$tab_array[0] = array(gettext("Gateways"), false, "system_gateways.php");
229
$tab_array[1] = array(gettext("Static Routes"), true, "system_routes.php");
230
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
231
display_top_tabs($tab_array);
232

    
233
?>
234
<div class="panel panel-default">
235
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Static Routes')?></h2></div>
236
	<div class="panel-body">
237
		<div class="table-responsive">
238
			<table class="table table-striped table-hover table-condensed table-rowdblclickedit">
239
				<thead>
240
					<tr>
241
						<th></th>
242
						<th><?=gettext("Network")?></th>
243
						<th><?=gettext("Gateway")?></th>
244
						<th><?=gettext("Interface")?></th>
245
						<th><?=gettext("Description")?></th>
246
						<th><?=gettext("Actions")?></th>
247
					</tr>
248
				</thead>
249
				<tbody>
250
<?php
251
foreach ($a_routes as $i => $route):
252
	if (isset($route['disabled'])) {
253
		$icon = 'fa-ban';
254
	} else {
255
		$icon = 'fa-check-circle-o';
256
	}
257
?>
258
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?>>
259
					<td><i class="fa <?=$icon?>"></i></td>
260
					<td>
261
						<?=strtolower($route['network'])?>
262
					</td>
263
					<td>
264
						<?=htmlentities($a_gateways[$route['gateway']]['name']) . " - " . htmlentities($a_gateways[$route['gateway']]['gateway'])?>
265
					</td>
266
					<td>
267
						<?=convert_friendly_interface_to_friendly_descr($a_gateways[$route['gateway']]['friendlyiface'])?>
268
					</td>
269
					<td>
270
						<?=htmlspecialchars($route['descr'])?>
271
					</td>
272
					<td>
273
						<a href="system_routes_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit route')?>"></a>
274

    
275
						<a href="system_routes_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy route')?>"></a>
276

    
277
				<?php if (isset($route['disabled'])) {
278
				?>
279
						<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable route')?>" usepost></a>
280
				<?php } else {
281
				?>
282
						<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable route')?>" usepost></a>
283
				<?php }
284
				?>
285
						<a href="system_routes.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete route')?>" usepost></a>
286

    
287
					</td>
288
				</tr>
289
<?php endforeach; ?>
290
			</table>
291
		</div>
292
	</div>
293
</div>
294

    
295
<nav class="action-buttons">
296
	<a href="system_routes_edit.php" role="button" class="btn btn-success btn-sm">
297
		<i class="fa fa-plus icon-embed-btn"></i>
298
		<?=gettext("Add")?>
299
	</a>
300
</nav>
301
<?php
302

    
303
include("foot.inc");
(203-203/227)