Project

General

Profile

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

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

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

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

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

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

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

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

    
76
if ($interface) {
77
	$altq = $altq_list_queues[$interface];
78

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

    
86
$dontshow = false;
87
$newqueue = false;
88
$dfltmsg = false;
89

    
90
if ($_GET) {
91
	switch ($action) {
92
		case "delete":
93
			if ($queue) {
94
				$queue->delete_queue();
95
				if (write_config()) {
96
					mark_subsystem_dirty('shaper');
97
				}
98
			}
99

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

    
124
			if (write_config()) {
125
				$changes_applied = true;
126
				$retval = 0;
127
				$retval |= filter_configure();
128
			} else {
129
				$no_write_config_msg = gettext("Unable to write config.xml (Access Denied?).");
130
			}
131

    
132
			$dfltmsg = true;
133

    
134

    
135
		break;
136

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

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

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

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

    
216
if ($_POST) {
217
	unset($input_errors);
218

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

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

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

    
285
		clear_subsystem_dirty('shaper');
286

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

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

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

    
335
include("head.inc");
336

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

    
345
$tree .= "</ul>";
346

    
347
if ($queue) {
348
	print($queue->build_javascript());
349
}
350

    
351
print($newjavascript);
352

    
353
if ($input_errors) {
354
	print_input_errors($input_errors);
355
}
356

    
357
if ($no_write_config_msg) {
358
	print_info_box($no_write_config_msg, 'danger');
359
}
360

    
361
if ($changes_applied) {
362
	print_apply_result_box($retval);
363
}
364

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

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

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

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

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

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

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

    
418
		}
419

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

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

    
433
	}
434

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

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

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