Project

General

Profile

Download (12.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * system_gateways.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2018 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2010 Seth Mos <seth.mos@dds.nl>
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

    
23
##|+PRIV
24
##|*IDENT=page-system-gateways
25
##|*NAME=System: Gateways
26
##|*DESCR=Allow access to the 'System: Gateways' page.
27
##|*MATCH=system_gateways.php*
28
##|-PRIV
29

    
30
require_once("guiconfig.inc");
31
require_once("functions.inc");
32
require_once("filter.inc");
33
require_once("shaper.inc");
34
require_once("gwlb.inc");
35

    
36
$simplefields = array('defaultgw4', 'defaultgw6');
37

    
38
$a_gateways = return_gateways_array(true, false, true, true);
39

    
40
if (!is_array($config['gateways'])) {
41
	$config['gateways'] = array();
42
}
43

    
44
if (!is_array($config['gateways']['gateway_item'])) {
45
	$config['gateways']['gateway_item'] = array();
46
}
47

    
48
$a_gateway_item = &$config['gateways']['gateway_item'];
49

    
50
$pconfig = $_REQUEST;
51

    
52
if ($_POST['save']) {
53
	unset($input_errors);
54
	$pconfig = $_POST;
55
	foreach($simplefields as $field) {
56
		$config['gateways'][$field] = $pconfig[$field];
57
	}
58
	mark_subsystem_dirty('staticroutes');
59
	write_config("System - Gateways: save default gateway");
60
}
61

    
62
if ($_POST['apply']) {
63

    
64
	$retval = 0;
65

    
66
	$retval |= system_routing_configure();
67
	$retval |= system_resolvconf_generate();
68
	$retval |= filter_configure();
69
	/* reconfigure our gateway monitor */
70
	setup_gateways_monitor();
71
	/* Dynamic DNS on gw groups may have changed */
72
	send_event("service reload dyndnsall");
73

    
74
	if ($retval == 0) {
75
		clear_subsystem_dirty('staticroutes');
76
	}
77
}
78

    
79

    
80
function can_delete_disable_gateway_item($id, $disable = false) {
81
	global $config, $input_errors, $a_gateways;
82

    
83
	if (!isset($a_gateways[$id])) {
84
		return false;
85
	}
86

    
87
	if (is_array($config['gateways']['gateway_group'])) {
88
		foreach ($config['gateways']['gateway_group'] as $group) {
89
			foreach ($group['item'] as $item) {
90
				$items = explode("|", $item);
91
				if ($items[0] == $a_gateways[$id]['name']) {
92
					if (!$disable) {
93
						$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be deleted because it is in use on Gateway Group "%2$s"'), $a_gateways[$id]['name'], $group['name']);
94
					} else {
95
						$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use on Gateway Group "%2$s"'), $a_gateways[$id]['name'], $group['name']);
96
					}
97
				}
98
			}
99
		}
100
	}
101

    
102
	if (is_array($config['staticroutes']['route'])) {
103
		foreach ($config['staticroutes']['route'] as $route) {
104
			if ($route['gateway'] == $a_gateways[$id]['name']) {
105
				if (!$disable) {
106
					// The user wants to delete this gateway, but there is a static route (enabled or disabled) that refers to the gateway.
107
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be deleted because it is in use on Static Route "%2$s"'), $a_gateways[$id]['name'], $route['network']);
108
				} else if (!isset($route['disabled'])) {
109
					// The user wants to disable this gateway.
110
					// But there is a static route that uses this gateway and is enabled (not disabled).
111
					$input_errors[] = sprintf(gettext('Gateway "%1$s" cannot be disabled because it is in use on Static Route "%2$s"'), $a_gateways[$id]['name'], $route['network']);
112
				}
113
			}
114
		}
115
	}
116

    
117
	if (isset($input_errors)) {
118
		return false;
119
	}
120

    
121
	return true;
122
}
123

    
124
function delete_gateway_item($id) {
125
	global $config, $a_gateways;
126

    
127
	if (!isset($a_gateways[$id])) {
128
		return;
129
	}
130

    
131
	/* If the removed gateway was the default route, remove the default route */
132
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
133
	    !isset($a_gateways[$id]['disabled']) &&
134
	    isset($a_gateways[$id]['isdefaultgw'])) {
135
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? '-inet6' : '-inet');
136
		file_put_contents("/dev/console", "\n[".getmypid()."] DEL_GW, route= delete {$inet} default");
137
		mwexec("/sbin/route delete {$inet} default");
138
	}
139

    
140
	/* NOTE: Cleanup static routes for the interface route if any */
141
	if (!empty($a_gateways[$id]) && is_ipaddr($a_gateways[$id]['gateway']) &&
142
	    $gateway['gateway'] != $a_gateways[$id]['gateway'] &&
143
	    isset($a_gateways[$id]["nonlocalgateway"])) {
144
		$realif = get_real_interface($a_gateways[$id]['interface']);
145
		$inet = (!is_ipaddrv4($a_gateways[$id]['gateway']) ? "-inet6" : "-inet");
146
		file_put_contents("/dev/console", "\n[".getmypid()."] DEL_GW, route= $inet " . escapeshellarg($a_gateways[$id]['gateway']) . " -iface " . escapeshellarg($realif));
147
		$cmd = "/sbin/route delete $inet " . escapeshellarg($a_gateways[$id]['gateway']) . " -iface " . escapeshellarg($realif);
148
		mwexec($cmd);
149
	}
150
	/* NOTE: Cleanup static routes for the monitor ip if any */
151
	if (!empty($a_gateways[$id]['monitor']) &&
152
	    $a_gateways[$id]['monitor'] != "dynamic" &&
153
	    is_ipaddr($a_gateways[$id]['monitor']) &&
154
	    $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
155
		if (is_ipaddrv4($a_gateways[$id]['monitor'])) {
156
			mwexec("/sbin/route delete " . escapeshellarg($a_gateways[$id]['monitor']));
157
		} else {
158
			mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateways[$id]['monitor']));
159
		}
160
	}
161

    
162
	if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
163
		unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
164
	}
165
	unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
166
}
167

    
168
unset($input_errors);
169
if ($_REQUEST['act'] == "del") {
170
	if (can_delete_disable_gateway_item($_REQUEST['id'])) {
171
		$realid = $a_gateways[$_REQUEST['id']]['attribute'];
172
		delete_gateway_item($_REQUEST['id']);
173
		write_config("Gateways: removed gateway {$realid}");
174
		mark_subsystem_dirty('staticroutes');
175
		header("Location: system_gateways.php");
176
		exit;
177
	}
178
}
179

    
180
if (isset($_REQUEST['del_x'])) {
181
	/* delete selected items */
182
	if (is_array($_REQUEST['rule']) && count($_REQUEST['rule'])) {
183
		foreach ($_REQUEST['rule'] as $rulei) {
184
			if (!can_delete_disable_gateway_item($rulei)) {
185
				break;
186
			}
187
		}
188

    
189
		if (!isset($input_errors)) {
190
			$items_deleted = "";
191
			foreach ($_REQUEST['rule'] as $rulei) {
192
				delete_gateway_item($rulei);
193
				$items_deleted .= "{$rulei} ";
194
			}
195
			if (!empty($items_deleted)) {
196
				write_config(sprintf(gettext("Gateways: removed gateways %s", $items_deleted)));
197
				mark_subsystem_dirty('staticroutes');
198
			}
199
			header("Location: system_gateways.php");
200
			exit;
201
		}
202
	}
203

    
204
} else if ($_REQUEST['act'] == "toggle" && $a_gateways[$_REQUEST['id']]) {
205
	$realid = $a_gateways[$_REQUEST['id']]['attribute'];
206
	$disable_gw = !isset($a_gateway_item[$realid]['disabled']);
207
	if ($disable_gw) {
208
		// The user wants to disable the gateway, so check if that is OK.
209
		$ok_to_toggle = can_delete_disable_gateway_item($_REQUEST['id'], $disable_gw);
210
	} else {
211
		// The user wants to enable the gateway. That is always OK.
212
		$ok_to_toggle = true;
213
	}
214
	if ($ok_to_toggle) {
215
		gateway_set_enabled($a_gateway_item[$realid]['name'], !$disable_gw);
216

    
217
		if (write_config("Gateways: enable/disable")) {
218
			mark_subsystem_dirty('staticroutes');
219
		}
220

    
221
		header("Location: system_gateways.php");
222
		exit;
223
	}
224
}
225

    
226
foreach($simplefields as $field) {
227
	$pconfig[$field] = $config['gateways'][$field];
228
}
229

    
230
function gateway_displaygwtiername($gwname) {
231
	global $config;
232
	$gw = lookup_gateway_or_group_by_name($gwname);
233
	if ($config['gateways']['defaultgw4'] == $gwname || $config['gateways']['defaultgw6'] == $gwname) {
234
		$result = "Default";
235
	} else {
236
		if ($gw['ipprotocol'] == 'inet') {
237
			$defgw = lookup_gateway_or_group_by_name($config['gateways']['defaultgw4']);
238
		} else {
239
			$defgw = lookup_gateway_or_group_by_name($config['gateways']['defaultgw6']);
240
		}
241
		if ($defgw['type'] == "gatewaygroup") {
242
			$detail = gateway_is_gwgroup_member($gwname, true);
243
			foreach($detail as $gwitem) {
244
				if ($gwitem['name'] == $defgw['name']) {
245
					if (isset($gwitem['tier'])) {
246
						$result = "Tier " . $gwitem['tier'];
247
						break;
248
					}
249
				}
250
			}
251
		}
252
	}
253
	if (!empty($result)) {
254
		$result .= $gw['ipprotocol'] == "inet" ? " (IPv4)" : " (IPv6)";
255
	}
256
	return $result;
257
}
258

    
259
$pgtitle = array(gettext("System"), gettext("Routing"), gettext("Gateways"));
260
$pglinks = array("", "@self", "@self");
261
$shortcut_section = "gateways";
262

    
263
include("head.inc");
264

    
265
if ($input_errors) {
266
	print_input_errors($input_errors);
267
}
268

    
269
if ($_POST['apply']) {
270
	print_apply_result_box($retval);
271
}
272

    
273
if (is_subsystem_dirty('staticroutes')) {
274
	print_apply_box(gettext("The gateway configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
275
}
276

    
277
$tab_array = array();
278
$tab_array[0] = array(gettext("Gateways"), true, "system_gateways.php");
279
$tab_array[1] = array(gettext("Static Routes"), false, "system_routes.php");
280
$tab_array[2] = array(gettext("Gateway Groups"), false, "system_gateway_groups.php");
281
display_top_tabs($tab_array);
282

    
283
?>
284
<div class="panel panel-default">
285
	<div class="panel-heading"><h2 class="panel-title"><?=gettext('Gateways')?></h2></div>
286
	<div class="panel-body">
287
		<div class="table-responsive">
288
			<table class="table table-striped table-hover table-condensed table-rowdblclickedit">
289
				<thead>
290
					<tr>
291
						<th></th>
292
						<th><?=gettext("Name")?></th>
293
						<th><?=gettext("Default")?></th>
294
						<th><?=gettext("Interface")?></th>
295
						<th><?=gettext("Gateway")?></th>
296
						<th><?=gettext("Monitor IP")?></th>
297
						<th><?=gettext("Description")?></th>
298
						<th><?=gettext("Actions")?></th>
299
					</tr>
300
				</thead>
301
				<tbody>
302
<?php
303
foreach ($a_gateways as $i => $gateway):
304
	if (isset($gateway['inactive'])) {
305
		$icon = 'fa-times-circle-o';
306
	} elseif (isset($gateway['disabled'])) {
307
		$icon = 'fa-ban';
308
	} else {
309
		$icon = 'fa-check-circle-o';
310
	}
311

    
312
	if (isset($gateway['inactive'])) {
313
		$title = gettext("This gateway is inactive because interface is missing");
314
	} else {
315
		$title = '';
316
	}
317
?>
318
				<tr<?=($icon != 'fa-check-circle-o')? ' class="disabled"' : ''?>>
319
					<td title="<?=$title?>"><i class="fa <?=$icon?>"></i></td>
320
					<td>
321
						<?=htmlspecialchars($gateway['name'])?>
322
<?php
323
						if (isset($gateway['isdefaultgw'])) {
324
							echo " <strong>(default)</strong>";
325
						}
326
?>
327
						</td>
328
						<td>
329
							<?=gateway_displaygwtiername($gateway['name'])?>
330
						</td>
331
						<td>
332
							<?=htmlspecialchars(convert_friendly_interface_to_friendly_descr($gateway['friendlyiface']))?>
333
						</td>
334
						<td>
335
							<?=htmlspecialchars($gateway['gateway'])?>
336
						</td>
337
						<td>
338
							<?=htmlspecialchars($gateway['monitor'])?>
339
						</td>
340
						<td>
341
							<?=htmlspecialchars($gateway['descr'])?>
342
						</td>
343
						<td>
344
							<a href="system_gateways_edit.php?id=<?=$i?>" class="fa fa-pencil" title="<?=gettext('Edit gateway');?>"></a>
345
							<a href="system_gateways_edit.php?dup=<?=$i?>" class="fa fa-clone" title="<?=gettext('Copy gateway')?>"></a>
346

    
347
<?php if (is_numeric($gateway['attribute'])): ?>
348
	<?php if (isset($gateway['disabled'])) {
349
	?>
350
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-check-square-o" title="<?=gettext('Enable gateway')?>" usepost></a>
351
	<?php } else {
352
	?>
353
							<a href="?act=toggle&amp;id=<?=$i?>" class="fa fa-ban" title="<?=gettext('Disable gateway')?>" usepost></a>
354
	<?php }
355
	?>
356
							<a href="system_gateways.php?act=del&amp;id=<?=$i?>" class="fa fa-trash" title="<?=gettext('Delete gateway')?>" usepost></a>
357

    
358
<?php endif; ?>
359
						</td>
360
					</tr>
361
<?php endforeach; ?>
362
				</tbody>
363
			</table>
364
		</div>
365
	</div>
366
</div>
367

    
368
<nav class="action-buttons">
369
	<a href="system_gateways_edit.php" role="button" class="btn btn-success">
370
		<i class="fa fa-plus icon-embed-btn"></i>
371
		<?=gettext("Add");?>
372
	</a>
373
</nav>
374
<?php
375

    
376
$form = new Form;
377
$section = new Form_Section('Default gateway');
378

    
379
$items4 = array();
380
$items6 = array();
381
$items4['-'] = "None";
382
$items6['-'] = "None";
383
foreach($a_gateways as $gw) {
384
	$gwn = $gw['name'];
385
	if ($gw['ipprotocol'] == "inet6") {
386
		$items6[$gwn] = $gwn;
387
	} else {
388
		$items4[$gwn] = $gwn;
389
	}
390
}
391
$groups = return_gateway_groups_array();
392
foreach ($groups as $key => $group) {
393
	$gwn = $group['descr'];
394
	if ($group['ipprotocol'] == "inet6") {
395
		$items6[$key] = "$key ($gwn)";
396
	} else {
397
		$items4[$key] = "$key ($gwn)";
398
	}
399
}
400

    
401
$section->addInput(new Form_Select(
402
	'defaultgw4',
403
	'Default gateway IPv4',
404
	$pconfig['defaultgw4'],
405
	$items4
406
))->setHelp('Select the gateway or gatewaygroup to use as the default gateway.');
407

    
408
$section->addInput(new Form_Select(
409
	'defaultgw6',
410
	'Default gateway IPv6',
411
	$pconfig['defaultgw6'],
412
	$items6
413
))->setHelp('Select the gateway or gatewaygroup to use as the default gateway.');
414

    
415
$form->add($section);
416
print $form;
417

    
418
include("foot.inc");
(204-204/233)