Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * firewall_shaper.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-2022 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
##|+PRIV
25
##|*IDENT=page-firewall-trafficshaper
26
##|*NAME=Firewall: Traffic Shaper
27
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper' page.
28
##|*MATCH=firewall_shaper.php*
29
##|-PRIV
30

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

    
37
if ($_GET['reset'] != "") {
38
	/* XXX: Huh, why are we killing php? */
39
	mwexec("/usr/bin/killall -9 pfctl php");
40
	exit;
41
}
42

    
43
$pgtitle = array(gettext("Firewall"), gettext("Traffic Shaper"), gettext("By Interface"));
44
$pglinks = array("", "@self", "@self");
45
$shortcut_section = "trafficshaper";
46

    
47
$shaperIFlist = get_configured_interface_with_descr(true);
48
read_altq_config();
49
/*
50
 * The whole logic in these code maybe can be specified.
51
 * If you find a better way contact me :).
52
 */
53

    
54
if ($_GET) {
55
	if ($_GET['queue']) {
56
		$qname = htmlspecialchars(trim($_GET['queue']));
57
	}
58
	if ($_GET['interface']) {
59
		$interface = htmlspecialchars(trim($_GET['interface']));
60
	}
61
	if ($_GET['action']) {
62
		$action = htmlspecialchars($_GET['action']);
63
	}
64
}
65

    
66
if ($_POST) {
67
	if ($_POST['name']) {
68
		$qname = htmlspecialchars(trim($_POST['name']));
69
	}
70
	if ($_POST['interface']) {
71
		$interface = htmlspecialchars(trim($_POST['interface']));
72
	}
73
	if ($_POST['parentqueue']) {
74
		$parentqueue = htmlspecialchars(trim($_POST['parentqueue']));
75
	}
76
}
77

    
78
if ($interface) {
79
	$altq = $altq_list_queues[$interface];
80

    
81
	if ($altq) {
82
		$queue =& $altq->find_queue($interface, $qname);
83
	} else {
84
		$addnewaltq = true;
85
	}
86
}
87

    
88
$dontshow = false;
89
$newqueue = false;
90
$dfltmsg = false;
91

    
92
if ($_GET) {
93
	switch ($action) {
94
	case "delete":
95
		if ($queue) {
96
			$queue->delete_queue();
97
			if (write_config("Traffic Shaper: Item deleted")) {
98
				mark_subsystem_dirty('shaper');
99
			}
100
		}
101

    
102
		header("Location: firewall_shaper.php");
103
		exit;
104
		break;
105
	case "resetall":
106
		foreach ($altq_list_queues as $altq) {
107
			$altq->delete_all();
108
		}
109
		unset($altq_list_queues);
110
		$altq_list_queues = array();
111
		$tree = "<ul class=\"tree\" >";
112
		$tree .= get_interface_list_to_show();
113
		$tree .= "</ul>";
114
		unset($config['shaper']['queue']);
115
		unset($queue);
116
		unset($altq);
117
		$can_add = false;
118
		$can_enable = false;
119
		$dontshow = true;
120
		foreach ($config['filter']['rule'] as $key => $rule) {
121
			if (isset($rule['wizard']) && $rule['wizard'] == "yes") {
122
				unset($config['filter']['rule'][$key]);
123
			}
124
		}
125

    
126
		if (write_config("Traffic Shapper: Reset all")) {
127
			$changes_applied = true;
128
			$retval = 0;
129
			$retval |= filter_configure();
130
		} else {
131
			$no_write_config_msg = gettext("Unable to write config.xml (Access Denied?).");
132
		}
133

    
134
		$dfltmsg = true;
135

    
136

    
137
		break;
138

    
139
	case "add":
140
		/* XXX: Find better way because we shouldn't know about this */
141
		if ($altq) {
142

    
143
			switch ($altq->GetScheduler()) {
144
			case "PRIQ":
145
				$q = new priq_queue();
146
				break;
147
			case "FAIRQ":
148
				$q = new fairq_queue();
149
				break;
150
			case "HFSC":
151
				$q = new hfsc_queue();
152
				break;
153
			case "CBQ":
154
				$q = new cbq_queue();
155
				break;
156
			default:
157
				/* XXX: Happens when sched==NONE?! */
158
				$q = new altq_root_queue();
159
				break;
160
			}
161
		} else if ($addnewaltq) {
162
			$q = new altq_root_queue();
163
		} else {
164
			$input_errors[] = gettext("Could not create new queue/discipline! Any recent changes may need to be applied first.");
165
		}
166

    
167
		if ($q) {
168
			$q->SetInterface($interface);
169
			$sform = $q->build_form();
170
			$sform->addGlobal(new Form_Input(
171
			    'parentqueue',
172
			    null,
173
			    'hidden',
174
			    $qname
175
			    ));
176

    
177
			$newjavascript = $q->build_javascript();
178
			unset($q);
179
			$newqueue = true;
180
		}
181
		break;
182
	case "show":
183
		if ($queue) {
184
			$sform = $queue->build_form();
185
		} else {
186
			$input_errors[] = gettext("Queue not found!");
187
		}
188
		break;
189
	case "enable":
190
		if ($queue) {
191
			$queue->SetEnabled("on");
192
			$sform = $queue->build_form();
193
			if (write_config("Traffic Shaper: Queue enabled")) {
194
				mark_subsystem_dirty('shaper');
195
			}
196
		} else {
197
			$input_errors[] = gettext("Queue not found!");
198
		}
199
		break;
200
	case "disable":
201
		if ($queue) {
202
			$queue->SetEnabled("");
203
			$sform = $queue->build_form();
204
			if (write_config("Traffic Shaper: Queue disabled")) {
205
				mark_subsystem_dirty('shaper');
206
			}
207
		} else {
208
			$input_errors[] = gettext("Queue not found!");
209
		}
210
		break;
211
	default:
212
		$dfltmsg = true;
213
		$dontshow = true;
214
		break;
215
	}
216
}
217

    
218
if ($_POST) {
219
	unset($input_errors);
220

    
221
	if ($addnewaltq) {
222
		$__tmp_altq = new altq_root_queue(); $altq =& $__tmp_altq;
223
		$altq->SetInterface($interface);
224
		$altq->ReadConfig($_POST);
225
		$altq->validate_input($_POST, $input_errors);
226
		if (!$input_errors) {
227
			unset($tmppath);
228
			$tmppath[] = $altq->GetInterface();
229
			$altq->SetLink($tmppath);
230
			$altq->wconfig();
231
			if (write_config("Traffic Shaper: Added root queue")) {
232
				mark_subsystem_dirty('shaper');
233
			}
234
			$can_enable = true;
235
			$can_add = true;
236
		}
237

    
238
		read_altq_config();
239
		$sform = $altq->build_form();
240
	} else if ($parentqueue) { /* Add a new queue */
241
		$qtmp =& $altq->find_queue($interface, $parentqueue);
242
		if ($qtmp) {
243
			$tmppath =& $qtmp->GetLink();
244
			array_push($tmppath, $qname);
245
			$tmp =& $qtmp->add_queue($interface, $_POST, $tmppath, $input_errors);
246
			if (!$input_errors) {
247
				array_pop($tmppath);
248
				$tmp->wconfig();
249
				$can_enable = true;
250
				if ($tmp->CanHaveChildren() && $can_enable) {
251
					if ($tmp->GetDefault() <> "") {
252
						$can_add = false;
253
					} else {
254
						$can_add = true;
255
					}
256
				} else {
257
					$can_add = false;
258
				}
259
				if (write_config("Traffic Shaper: Added new queue")) {
260
					mark_subsystem_dirty('shaper');
261
				}
262
				$can_enable = true;
263
				if ($altq->GetScheduler() != "PRIQ") { /* XXX */
264
					if ($tmp->GetDefault() <> "") {
265
						$can_add = false;
266
					} else {
267
						$can_add = true;
268
					}
269
				}
270
			}
271
			read_altq_config();
272
			$sform = $tmp->build_form();
273
		} else {
274
			$input_errors[] = gettext("Could not add new queue.");
275
		}
276
	} else if ($_POST['apply']) {
277
		write_config("Traffic Shaper: Apply changes");
278
		$changes_applied = true;
279
		$retval = 0;
280
		$retval |= filter_configure();
281

    
282
		/* reset rrd queues */
283
		system("rm -f /var/db/rrd/*queuedrops.rrd");
284
		system("rm -f /var/db/rrd/*queues.rrd");
285
		enable_rrd_graphing();
286

    
287
		clear_subsystem_dirty('shaper');
288

    
289
		if ($queue) {
290
			$sform = $queue->build_form();
291
			$dontshow = false;
292
		} else {
293
			$sform = $default_shaper_message;
294
			$dontshow = true;
295
		}
296
	} else if ($queue) {
297
		$queue->validate_input($_POST, $input_errors);
298
		if (!$input_errors) {
299
			$queue->update_altq_queue_data($_POST);
300
			$queue->wconfig();
301
			if (write_config("Traffic Shaper: Changed queue")) {
302
				mark_subsystem_dirty('shaper');
303
			}
304
			$dontshow = false;
305
		}
306
		read_altq_config();
307
		$sform = $queue->build_form();
308
	} else	{
309
		$dfltmsg = true;
310
		$dontshow = true;
311
	}
312
	mwexec("/usr/bin/killall -q qstats");
313
}
314

    
315
if (!$_POST && !$_GET) {
316
	$dfltmsg = true;
317
	$dontshow = true;
318
}
319

    
320
if ($queue) {
321
	if ($queue->GetEnabled()) {
322
		$can_enable = true;
323
	} else {
324
		$can_enable = false;
325
	}
326
	if ($queue->CanHaveChildren() && $can_enable) {
327
		if ($altq->GetQname() <> $queue->GetQname() && $queue->GetDefault() <> "") {
328
			$can_add = false;
329
		} else {
330
			$can_add = true;
331
		}
332
	} else {
333
		$can_add = false;
334
	}
335
}
336

    
337
include("head.inc");
338

    
339
$tree = '<ul class="tree" >';
340
if (is_array($altq_list_queues)) {
341
	foreach ($altq_list_queues as $tmpaltq) {
342
		$tree .= $tmpaltq->build_tree();
343
	}
344
	$tree .= get_interface_list_to_show();
345
}
346

    
347
$tree .= "</ul>";
348

    
349
if ($queue) {
350
	print($queue->build_javascript());
351
}
352

    
353
print($newjavascript);
354

    
355
if ($input_errors) {
356
	print_input_errors($input_errors);
357
}
358

    
359
if ($no_write_config_msg) {
360
	print_info_box($no_write_config_msg, 'danger');
361
}
362

    
363
if ($changes_applied) {
364
	print_apply_result_box($retval);
365
}
366

    
367
if (is_subsystem_dirty('shaper')) {
368
	print_apply_box(gettext("The traffic shaper configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
369
}
370

    
371
$tab_array = array();
372
$tab_array[] = array(gettext("By Interface"), true, "firewall_shaper.php");
373
$tab_array[] = array(gettext("By Queue"), false, "firewall_shaper_queues.php");
374
$tab_array[] = array(gettext("Limiters"), false, "firewall_shaper_vinterface.php");
375
$tab_array[] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
376
display_top_tabs($tab_array);
377

    
378
?>
379
<script type="text/javascript" src="./vendor/tree/tree.js"></script>
380

    
381
<div class="table-responsive">
382
	<table class="table">
383
		<tbody>
384
			<tr class="tabcont">
385
				<td class="col-md-1">
386
<?php
387
// Display the shaper tree
388
print($tree);
389

    
390
if (count($altq_list_queues) > 0) {
391
?>
392
					<a href="firewall_shaper.php?action=resetall" class="btn btn-sm btn-danger">
393
						<i class="fa fa-trash icon-embed-btn"></i>
394
						<?=gettext('Remove Shaper')?>
395
					</a>
396
<?php
397
}
398
?>
399
				</td>
400
				<td>
401
<?php
402

    
403
if (!$dfltmsg && $sform)  {
404
	// Add global buttons
405
	if (!$dontshow || $newqueue) {
406
		if ($can_add || $addnewaltq) {
407
			if ($queue) {
408
				$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=add';
409
			} else {
410
				$url = 'firewall_shaper.php?interface='. $interface . '&action=add';
411
			}
412

    
413
			$sform->addGlobal(new Form_Button(
414
				'add',
415
				'Add new Queue',
416
				$url,
417
				'fa-plus'
418
			))->addClass('btn-success');
419

    
420
		}
421

    
422
		if ($queue) {
423
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
424
		} else {
425
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
426
		}
427

    
428
		$sform->addGlobal(new Form_Button(
429
			'delete',
430
			$queue ? 'Delete this queue':'Disable shaper on interface',
431
			$url,
432
			'fa-trash'
433
		))->addClass('btn-danger nowarn');
434

    
435
	}
436

    
437
	print($sform);
438
}
439
?>
440
				</td>
441
			</tr>
442
		</tbody>
443
	</table>
444
</div>
445

    
446
<?php if (empty(get_interface_list_to_show()) && (!is_array($altq_list_queues) || (count($altq_list_queues) == 0))): ?>
447
<div>
448
	<div class="infoblock blockopen">
449
		<?php print_info_box(gettext("This firewall does not have any interfaces assigned that are capable of using ALTQ traffic shaping."), 'danger', false); ?>
450
	</div>
451
</div>
452
<?php endif; ?>
453

    
454
<?php
455
if ($dfltmsg) {
456
?>
457
<div>
458
	<div class="infoblock">
459
		<?php print_info_box($default_shaper_msg, 'info', false); ?>
460
	</div>
461
</div>
462
<?php
463
}
464
include("foot.inc");
(54-54/228)