Project

General

Profile

Download (10.6 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
$pgtitle = array(gettext("Firewall"), gettext("Traffic Shaper"), gettext("By Interface"));
38
$pglinks = array("", "@self", "@self");
39
$shortcut_section = "trafficshaper";
40

    
41
$shaperIFlist = get_configured_interface_with_descr(true);
42
read_altq_config();
43
/*
44
 * The whole logic in these code maybe can be specified.
45
 * If you find a better way contact me :).
46
 */
47

    
48
if ($_GET) {
49
	if ($_GET['queue']) {
50
		$qname = htmlspecialchars(trim($_GET['queue']));
51
	}
52
	if ($_GET['interface']) {
53
		$interface = htmlspecialchars(trim($_GET['interface']));
54
	}
55
	if ($_GET['action']) {
56
		$action = htmlspecialchars($_GET['action']);
57
	}
58
}
59

    
60
if ($_POST) {
61
	if ($_POST['name']) {
62
		$qname = htmlspecialchars(trim($_POST['name']));
63
	}
64
	if ($_POST['interface']) {
65
		$interface = htmlspecialchars(trim($_POST['interface']));
66
	}
67
	if ($_POST['parentqueue']) {
68
		$parentqueue = htmlspecialchars(trim($_POST['parentqueue']));
69
	}
70
}
71

    
72
if ($interface) {
73
	$altq = $altq_list_queues[$interface];
74

    
75
	if ($altq) {
76
		$queue =& $altq->find_queue($interface, $qname);
77
	} else {
78
		$addnewaltq = true;
79
	}
80
}
81

    
82
$dontshow = false;
83
$newqueue = false;
84
$dfltmsg = false;
85

    
86
if ($_GET) {
87
	switch ($action) {
88
	case "delete":
89
		if ($queue) {
90
			$queue->delete_queue();
91
			if (write_config("Traffic Shaper: Item deleted")) {
92
				mark_subsystem_dirty('shaper');
93
			}
94
		}
95

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

    
120
		if (write_config("Traffic Shapper: Reset all")) {
121
			$changes_applied = true;
122
			$retval = 0;
123
			$retval |= filter_configure();
124
		} else {
125
			$no_write_config_msg = gettext("Unable to write config.xml (Access Denied?).");
126
		}
127

    
128
		$dfltmsg = true;
129

    
130

    
131
		break;
132

    
133
	case "add":
134
		/* XXX: Find better way because we shouldn't know about this */
135
		if ($altq) {
136

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

    
161
		if ($q) {
162
			$q->SetInterface($interface);
163
			$sform = $q->build_form();
164
			$sform->addGlobal(new Form_Input(
165
			    'parentqueue',
166
			    null,
167
			    'hidden',
168
			    $qname
169
			    ));
170

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

    
212
if ($_POST) {
213
	unset($input_errors);
214

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

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

    
276
		/* reset rrd queues */
277
		system("rm -f /var/db/rrd/*queuedrops.rrd");
278
		system("rm -f /var/db/rrd/*queues.rrd");
279
		enable_rrd_graphing();
280

    
281
		clear_subsystem_dirty('shaper');
282

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

    
309
if (!$_POST && !$_GET) {
310
	$dfltmsg = true;
311
	$dontshow = true;
312
}
313

    
314
if ($queue) {
315
	if ($queue->GetEnabled()) {
316
		$can_enable = true;
317
	} else {
318
		$can_enable = false;
319
	}
320
	if ($queue->CanHaveChildren() && $can_enable) {
321
		if ($altq->GetQname() <> $queue->GetQname() && $queue->GetDefault() <> "") {
322
			$can_add = false;
323
		} else {
324
			$can_add = true;
325
		}
326
	} else {
327
		$can_add = false;
328
	}
329
}
330

    
331
include("head.inc");
332

    
333
$tree = '<ul class="tree" >';
334
if (is_array($altq_list_queues)) {
335
	foreach ($altq_list_queues as $tmpaltq) {
336
		$tree .= $tmpaltq->build_tree();
337
	}
338
	$tree .= get_interface_list_to_show();
339
}
340

    
341
$tree .= "</ul>";
342

    
343
if ($queue) {
344
	print($queue->build_javascript());
345
}
346

    
347
print($newjavascript);
348

    
349
if ($input_errors) {
350
	print_input_errors($input_errors);
351
}
352

    
353
if ($no_write_config_msg) {
354
	print_info_box($no_write_config_msg, 'danger');
355
}
356

    
357
if ($changes_applied) {
358
	print_apply_result_box($retval);
359
}
360

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

    
365
$tab_array = array();
366
$tab_array[] = array(gettext("By Interface"), true, "firewall_shaper.php");
367
$tab_array[] = array(gettext("By Queue"), false, "firewall_shaper_queues.php");
368
$tab_array[] = array(gettext("Limiters"), false, "firewall_shaper_vinterface.php");
369
$tab_array[] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
370
display_top_tabs($tab_array);
371

    
372
?>
373
<script type="text/javascript" src="./vendor/tree/tree.js"></script>
374

    
375
<div class="table-responsive">
376
	<table class="table">
377
		<tbody>
378
			<tr class="tabcont">
379
				<td class="col-md-1">
380
<?php
381
// Display the shaper tree
382
print($tree);
383

    
384
if (count($altq_list_queues) > 0) {
385
?>
386
					<a href="firewall_shaper.php?action=resetall" class="btn btn-sm btn-danger">
387
						<i class="fa fa-trash icon-embed-btn"></i>
388
						<?=gettext('Remove Shaper')?>
389
					</a>
390
<?php
391
}
392
?>
393
				</td>
394
				<td>
395
<?php
396

    
397
if (!$dfltmsg && $sform)  {
398
	// Add global buttons
399
	if (!$dontshow || $newqueue) {
400
		if ($can_add || $addnewaltq) {
401
			if ($queue) {
402
				$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=add';
403
			} else {
404
				$url = 'firewall_shaper.php?interface='. $interface . '&action=add';
405
			}
406

    
407
			$sform->addGlobal(new Form_Button(
408
				'add',
409
				'Add new Queue',
410
				$url,
411
				'fa-plus'
412
			))->addClass('btn-success');
413

    
414
		}
415

    
416
		if ($queue) {
417
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
418
		} else {
419
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
420
		}
421

    
422
		$sform->addGlobal(new Form_Button(
423
			'delete',
424
			$queue ? 'Delete this queue':'Disable shaper on interface',
425
			$url,
426
			'fa-trash'
427
		))->addClass('btn-danger nowarn');
428

    
429
	}
430

    
431
	print($sform);
432
}
433
?>
434
				</td>
435
			</tr>
436
		</tbody>
437
	</table>
438
</div>
439

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

    
448
<?php
449
if ($dfltmsg) {
450
?>
451
<div>
452
	<div class="infoblock">
453
		<?php print_info_box($default_shaper_msg, 'info', false); ?>
454
	</div>
455
</div>
456
<?php
457
}
458
include("foot.inc");
(54-54/228)