Project

General

Profile

Download (11.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_shaper.php
5
	Copyright (C) 2004, 2005 Scott Ullrich
6
	Copyright (C) 2008 Ermal Luçi
7
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
/*
32
	pfSense_BUILDER_BINARIES:	/usr/bin/killall
33
	pfSense_MODULE: shaper
34
*/
35

    
36
##|+PRIV
37
##|*IDENT=page-firewall-trafficshaper
38
##|*NAME=Firewall: Traffic Shaper page
39
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper' page.
40
##|*MATCH=firewall_shaper.php*
41
##|-PRIV
42

    
43
require('classes/Form.class.php');
44

    
45
require("guiconfig.inc");
46
require_once("functions.inc");
47
require_once("filter.inc");
48
require_once("shaper.inc");
49
require_once("rrd.inc");
50

    
51
if($_GET['reset'] != "") {
52
	/* XXX: Huh, why are we killing php? */
53
	mwexec("killall -9 pfctl php");
54
	exit;
55
}
56

    
57
$pgtitle = array(gettext("Firewall"),gettext("Traffic Shaper"));
58
$shortcut_section = "trafficshaper";
59

    
60
$shaperIFlist = get_configured_interface_with_descr();
61
read_altq_config();
62
/*
63
 * The whole logic in these code maybe can be specified.
64
 * If you find a better way contact me :).
65
 */
66

    
67
if ($_GET) {
68
	if ($_GET['queue'])
69
		$qname = htmlspecialchars(trim($_GET['queue']));
70

    
71
	if ($_GET['interface'])
72
			$interface = htmlspecialchars(trim($_GET['interface']));
73

    
74
	if ($_GET['action'])
75
			$action = htmlspecialchars($_GET['action']);
76
}
77

    
78
if ($_POST) {
79
	if ($_POST['name'])
80
			$qname = htmlspecialchars(trim($_POST['name']));
81

    
82
	if ($_POST['interface'])
83
			$interface = htmlspecialchars(trim($_POST['interface']));
84

    
85
	if ($_POST['parentqueue'])
86
		$parentqueue = htmlspecialchars(trim($_POST['parentqueue']));
87
}
88

    
89
if ($interface) {
90
	$altq = $altq_list_queues[$interface];
91

    
92
	if ($altq) {
93
		$queue =& $altq->find_queue($interface, $qname);
94
	} else
95
		$addnewaltq = true;
96
}
97

    
98
$dontshow = false;
99
$newqueue = false;
100
$output_form = "";
101
$dfltmsg = false;
102

    
103
if ($_GET) {
104
	switch ($action) {
105
	case "delete":
106
			if ($queue) {
107
				$queue->delete_queue();
108
				if (write_config())
109
					mark_subsystem_dirty('shaper');
110
			}
111

    
112
			header("Location: firewall_shaper.php");
113
			exit;
114
		break;
115

    
116
	case "resetall":
117
			foreach ($altq_list_queues as $altq)
118
				$altq->delete_all();
119
			unset($altq_list_queues);
120
			$altq_list_queues = array();
121
			$tree = "<ul class=\"tree\" >";
122
			$tree .= get_interface_list_to_show();
123
			$tree .= "</ul>";
124
			unset($config['shaper']['queue']);
125
			unset($queue);
126
			unset($altq);
127
			$can_add = false;
128
			$can_enable = false;
129
			$dontshow = true;
130
			foreach ($config['filter']['rule'] as $key => $rule) {
131
				if (isset($rule['wizard']) && $rule['wizard'] == "yes")
132
					unset($config['filter']['rule'][$key]);
133
			}
134

    
135
			if (write_config()) {
136
				$retval = 0;
137
				$retval |= filter_configure();
138
				$savemsg = get_std_save_message($retval);
139

    
140
				if (stristr($retval, "error") != true)
141
					$savemsg = get_std_save_message($retval);
142
				else
143
					$savemsg = $retval;
144

    
145
			} else {
146
				$savemsg = gettext("Unable to write config.xml (Access Denied?)");
147
			}
148

    
149
			$dfltmsg = true;
150

    
151

    
152
		break;
153

    
154
	case "add":
155
			/* XXX: Find better way because we shouldn't know about this */
156
		if ($altq) {
157

    
158
			switch ($altq->GetScheduler()) {
159
				case "PRIQ":
160
					$q = new priq_queue();
161
				break;
162
				case "FAIRQ":
163
					$q = new fairq_queue();
164
				break;
165
				case "HFSC":
166
					$q = new hfsc_queue();
167
				break;
168
				case "CBQ":
169
						$q = new cbq_queue();
170
				break;
171
				default:
172
					/* XXX: Happens when sched==NONE?! */
173
					$q = new altq_root_queue();
174
				break;
175
			}
176
		} else if ($addnewaltq) {
177
			$q = new altq_root_queue();
178
		} else
179
			$input_errors[] = gettext("Could not create new queue/discipline!");
180

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

    
222
if ($_POST) {
223
	unset($input_errors);
224

    
225
	if ($addnewaltq) {
226
		$altq =& new altq_root_queue();
227
		$altq->SetInterface($interface);
228

    
229
		switch ($altq->GetBwscale()) {
230
				case "Mb":
231
					$factor = 1000 * 1000;
232
					break;
233
				case "Kb":
234
					$factor = 1000;
235
					break;
236
				case "b":
237
					$factor = 1;
238
					break;
239
				case "Gb":
240
					$factor = 1000 * 1000 * 1000;
241
					break;
242
				case "%": /* We don't use it for root_XXX queues. */
243
				default: /* XXX assume Kb by default. */
244
					$factor = 1000;
245
					break;
246
			}
247

    
248
		$altq->SetAvailableBandwidth($altq->GetBandwidth() * $factor);
249
		$altq->ReadConfig($_POST);
250
		$altq->validate_input($_POST, $input_errors);
251
		if (!$input_errors) {
252
			unset($tmppath);
253
			$tmppath[] = $altq->GetInterface();
254
			$altq->SetLink($tmppath);
255
			$altq->wconfig();
256
			if (write_config())
257
				mark_subsystem_dirty('shaper');
258

    
259
			$can_enable = true;
260
			$can_add = true;
261
		}
262

    
263
		read_altq_config();
264
		$output_form .= $altq->build_form();
265

    
266
	} else if ($parentqueue) { /* Add a new queue */
267
		$qtmp =& $altq->find_queue($interface, $parentqueue);
268
		if ($qtmp) {
269
			$tmppath =& $qtmp->GetLink();
270
			array_push($tmppath, $qname);
271
			$tmp =& $qtmp->add_queue($interface, $_POST, $tmppath, $input_errors);
272
			if (!$input_errors) {
273
				array_pop($tmppath);
274
				$tmp->wconfig();
275
				$can_enable = true;
276
				if ($tmp->CanHaveChildren() && $can_enable) {
277
					if ($tmp->GetDefault() != "")
278
						$can_add = false;
279
					else
280
						$can_add = true;
281
				} else
282
					$can_add = false;
283
				if (write_config())
284
					mark_subsystem_dirty('shaper');
285
				$can_enable = true;
286
				if ($altq->GetScheduler() != "PRIQ") /* XXX */
287
					if ($tmp->GetDefault() != "")
288
						$can_add = false;
289
					else
290
						$can_add = true;
291
			}
292
			read_altq_config();
293
			$output_form .= $tmp->build_form();
294
		} else
295
			$input_errors[] = gettext("Could not add new queue.");
296
	} else if ($_POST['apply']) {
297
			write_config();
298

    
299
			$retval = 0;
300
			$retval = filter_configure();
301
			$savemsg = get_std_save_message($retval);
302

    
303
			if (stristr($retval, "error") != true)
304
					$savemsg = get_std_save_message($retval);
305
			else
306
					$savemsg = $retval;
307

    
308
		/* reset rrd queues */
309
		system("rm -f /var/db/rrd/*queuedrops.rrd");
310
		system("rm -f /var/db/rrd/*queues.rrd");
311
		enable_rrd_graphing();
312

    
313
		clear_subsystem_dirty('shaper');
314

    
315
			if ($queue) {
316
				$output_form .= $queue->build_form();
317
				$dontshow = false;
318
			}
319
			else {
320
				$dfltmsg = true;
321
				$dontshow = true;
322
			}
323

    
324
	} else if ($queue) {
325
				$queue->validate_input($_POST, $input_errors);
326
				if (!$input_errors) {
327
					$queue->update_altq_queue_data($_POST);
328
					$queue->wconfig();
329

    
330
					if (write_config())
331
						mark_subsystem_dirty('shaper');
332

    
333
				$dontshow = false;
334
				}
335

    
336
		read_altq_config();
337
		$output_form .= $queue->build_form();
338
	} else	{
339
		$dfltmsg = true;
340
		$dontshow = true;
341
	}
342
	mwexec("killall qstats");
343
}
344

    
345
if(!$_POST && !$_GET){
346
	$dfltmsg = true;
347
	$dontshow = true;
348
}
349

    
350
if ($queue) {
351
	if ($queue->GetEnabled())
352
		$can_enable = true;
353
	else
354
		$can_enable = false;
355

    
356
	if ($queue->CanHaveChildren() && $can_enable) {
357
		if ($altq->GetQname() != $queue->GetQname() && $queue->GetDefault() != "")
358
			$can_add = false;
359
		else
360
			$can_add = true;
361
	} else
362
		$can_add = false;
363
}
364

    
365
//$pgtitle = "Firewall: Shaper: By Interface View";
366
$closehead = false;
367
include("head.inc");
368

    
369
$tree = '<ul class="tree" >';
370
if (is_array($altq_list_queues)) {
371
	foreach ($altq_list_queues as $tmpaltq) {
372
			$tree .= $tmpaltq->build_tree();
373
	}
374

    
375
	$tree .=  get_interface_list_to_show();
376
}
377

    
378
$tree .= "</ul>";
379

    
380
if ($queue)
381
	print($queue->build_javascript());
382

    
383
print($newjavascript);
384

    
385
if ($input_errors)
386
	print_input_errors($input_errors);
387

    
388
if ($savemsg)
389
	print_info_box($savemsg, 'success');
390

    
391
if (is_subsystem_dirty('shaper'))
392
	print_info_box_np(gettext("The traffic shaper configuration has been changed. You must apply the changes in order for them to take effect."));
393

    
394
$tab_array = array();
395
$tab_array[] = array(gettext("By Interface"), true, "firewall_shaper.php");
396
$tab_array[] = array(gettext("By Queue"), false, "firewall_shaper_queues.php");
397
$tab_array[] = array(gettext("Limiter"), false, "firewall_shaper_vinterface.php");
398
$tab_array[] = array(gettext("Layer7"), false, "firewall_shaper_layer7.php");
399
$tab_array[] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
400
display_top_tabs($tab_array);
401

    
402
?>
403
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
404
<script type="text/javascript" src="./tree/tree.js"></script>
405

    
406
<div class="table-responsive">
407
	<table class="table">
408
		<tbody>
409
			<tr class="tabcont">
410
				<td class="col-md-1">
411
<?php
412
// Display the shaper tree
413
print($tree);
414

    
415
if (count($altq_list_queues) > 0) {
416
?>
417
					<a href="firewall_shaper.php?action=resetall" class="btn btn-sm btn-danger"/>
418
						<?=gettext('Remove Shaper')?>
419
					</a>
420
<?php
421
}
422
?>
423
				</td>
424
				<td>
425
<?php
426

    
427
if($dfltmsg)
428
	print_info_box($default_shaper_msg);
429
else {
430
	// Add global buttons
431
	if (!$dontshow || $newqueue) {
432
		if ($can_add || $addnewaltq) {
433
			if($queue)
434
				$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=add';
435
			else
436
				$url = 'firewall_shaper.php?interface='. $interface . '&action=add';
437

    
438
			$sform->addGlobal(new Form_Button(
439
				'add',
440
				'Add new Queue',
441
				$url
442
			))->removeClass('btn-default')->addClass('btn-success');
443
		}
444

    
445
		if($queue)
446
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
447
		else
448
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
449

    
450
		$sform->addGlobal(new Form_Button(
451
			'delete',
452
			$queue ? 'Delete this queue':'Disable shaper on interface',
453
			$url
454
		))->removeClass('btn-default')->addClass('btn-danger');
455
	}
456

    
457
	// Print the form
458
	print($sform);
459
}
460
?>
461
				</td>
462
			</tr>
463
		</tbody>
464
	</table>
465
</div>
466

    
467
<?php
468
include("foot.inc");
(64-64/237)