Project

General

Profile

Download (12.6 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_shaper.php
5
*/
6
/* ====================================================================
7
 *  Copyright (c)  2004-2015  Electric Sheep Fencing, LLC. All rights reserved. 
8
 *  Copyright (c)  2004, 2005 Scott Ullrich
9
 *  Copyright (c)  2008 Ermal Luçi
10
 *
11
 *  Redistribution and use in source and binary forms, with or without modification, 
12
 *  are permitted provided that the following conditions are met: 
13
 *
14
 *  1. Redistributions of source code must retain the above copyright notice,
15
 *      this list of conditions and the following disclaimer.
16
 *
17
 *  2. Redistributions in binary form must reproduce the above copyright
18
 *      notice, this list of conditions and the following disclaimer in
19
 *      the documentation and/or other materials provided with the
20
 *      distribution. 
21
 *
22
 *  3. All advertising materials mentioning features or use of this software 
23
 *      must display the following acknowledgment:
24
 *      "This product includes software developed by the pfSense Project
25
 *       for use in the pfSense software distribution. (http://www.pfsense.org/). 
26
 *
27
 *  4. The names "pfSense" and "pfSense Project" must not be used to
28
 *       endorse or promote products derived from this software without
29
 *       prior written permission. For written permission, please contact
30
 *       coreteam@pfsense.org.
31
 *
32
 *  5. Products derived from this software may not be called "pfSense"
33
 *      nor may "pfSense" appear in their names without prior written
34
 *      permission of the Electric Sheep Fencing, LLC.
35
 *
36
 *  6. Redistributions of any form whatsoever must retain the following
37
 *      acknowledgment:
38
 *
39
 *  "This product includes software developed by the pfSense Project
40
 *  for use in the pfSense software distribution (http://www.pfsense.org/).
41
  *
42
 *  THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
43
 *  EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45
 *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
46
 *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
 *  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
54
 *
55
 *  ====================================================================
56
 *
57
 */
58
/*
59
	pfSense_BUILDER_BINARIES:	/usr/bin/killall
60
	pfSense_MODULE: shaper
61
*/
62

    
63
##|+PRIV
64
##|*IDENT=page-firewall-trafficshaper
65
##|*NAME=Firewall: Traffic Shaper page
66
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper' page.
67
##|*MATCH=firewall_shaper.php*
68
##|-PRIV
69

    
70
require_once('classes/Form.class.php');
71

    
72
require("guiconfig.inc");
73
require_once("functions.inc");
74
require_once("filter.inc");
75
require_once("shaper.inc");
76
require_once("rrd.inc");
77

    
78
if ($_GET['reset'] != "") {
79
	/* XXX: Huh, why are we killing php? */
80
	mwexec("killall -9 pfctl php");
81
	exit;
82
}
83

    
84
$pgtitle = array(gettext("Firewall"), gettext("Traffic Shaper"));
85
$shortcut_section = "trafficshaper";
86

    
87
$shaperIFlist = get_configured_interface_with_descr();
88
read_altq_config();
89
/*
90
 * The whole logic in these code maybe can be specified.
91
 * If you find a better way contact me :).
92
 */
93

    
94
if ($_GET) {
95
	if ($_GET['queue']) {
96
		$qname = htmlspecialchars(trim($_GET['queue']));
97
	}
98
	if ($_GET['interface']) {
99
		$interface = htmlspecialchars(trim($_GET['interface']));
100
	}
101
	if ($_GET['action']) {
102
		$action = htmlspecialchars($_GET['action']);
103
	}
104
}
105

    
106
if ($_POST) {
107
	if ($_POST['name']) {
108
		$qname = htmlspecialchars(trim($_POST['name']));
109
	}
110
	if ($_POST['interface']) {
111
		$interface = htmlspecialchars(trim($_POST['interface']));
112
	}
113
	if ($_POST['parentqueue']) {
114
		$parentqueue = htmlspecialchars(trim($_POST['parentqueue']));
115
	}
116
}
117

    
118
if ($interface) {
119
	$altq = $altq_list_queues[$interface];
120

    
121
	if ($altq) {
122
		$queue =& $altq->find_queue($interface, $qname);
123
	} else {
124
		$addnewaltq = true;
125
	}
126
}
127

    
128
$dontshow = false;
129
$newqueue = false;
130
$output_form = "";
131
$dfltmsg = false;
132

    
133
if ($_GET) {
134
	switch ($action) {
135
		case "delete":
136
			if ($queue) {
137
				$queue->delete_queue();
138
				if (write_config()) {
139
					mark_subsystem_dirty('shaper');
140
				}
141
			}
142

    
143
			header("Location: firewall_shaper.php");
144
			exit;
145
			break;
146
		case "resetall":
147
			foreach ($altq_list_queues as $altq) {
148
				$altq->delete_all();
149
			}
150
			unset($altq_list_queues);
151
			$altq_list_queues = array();
152
			$tree = "<ul class=\"tree\" >";
153
			$tree .= get_interface_list_to_show();
154
			$tree .= "</ul>";
155
			unset($config['shaper']['queue']);
156
			unset($queue);
157
			unset($altq);
158
			$can_add = false;
159
			$can_enable = false;
160
			$dontshow = true;
161
			foreach ($config['filter']['rule'] as $key => $rule) {
162
				if (isset($rule['wizard']) && $rule['wizard'] == "yes") {
163
					unset($config['filter']['rule'][$key]);
164
				}
165
			}
166

    
167
			if (write_config()) {
168
				$retval = 0;
169
				$retval |= filter_configure();
170
				$savemsg = get_std_save_message($retval);
171

    
172
				if (stristr($retval, "error") <> true) {
173
					$savemsg = get_std_save_message($retval);
174
				} else {
175
					$savemsg = $retval;
176
				}
177
			} else {
178
				$savemsg = gettext("Unable to write config.xml (Access Denied?)");
179
			}
180

    
181
			$dfltmsg = true;
182

    
183

    
184
		break;
185

    
186
	case "add":
187
			/* XXX: Find better way because we shouldn't know about this */
188
		if ($altq) {
189

    
190
			switch ($altq->GetScheduler()) {
191
				case "PRIQ":
192
					$q = new priq_queue();
193
				break;
194
				case "FAIRQ":
195
					$q = new fairq_queue();
196
				break;
197
				case "HFSC":
198
					$q = new hfsc_queue();
199
				break;
200
				case "CBQ":
201
						$q = new cbq_queue();
202
				break;
203
				default:
204
					/* XXX: Happens when sched==NONE?! */
205
					$q = new altq_root_queue();
206
				break;
207
			}
208
		} else if ($addnewaltq) {
209
			$q = new altq_root_queue();
210
		} else {
211
			$input_errors[] = gettext("Could not create new queue/discipline!");
212
			}
213

    
214
			if ($q) {
215
				$q->SetInterface($interface);
216
				$sform = $q->build_form();
217
				$newjavascript = $q->build_javascript();
218
				unset($q);
219
				$newqueue = true;
220
			}
221
			break;
222
		case "show":
223
			if ($queue) {
224
				$sform = $queue->build_form();
225
				//$output_form .= $queue->build_form();
226
			}
227
			else
228
				$input_errors[] = gettext("Queue not found!");
229
		break;
230
		case "enable":
231
			if ($queue) {
232
					$queue->SetEnabled("on");
233
					$output_form .= $queue->build_form();
234
					if (write_config()) {
235
						mark_subsystem_dirty('shaper');
236
					}
237
			} else {
238
					$input_errors[] = gettext("Queue not found!");
239
			}
240
			break;
241
		case "disable":
242
			if ($queue) {
243
				$queue->SetEnabled("");
244
				$output_form .= $queue->build_form();
245
				if (write_config()) {
246
					mark_subsystem_dirty('shaper');
247
				}
248
			} else {
249
					$input_errors[] = gettext("Queue not found!");
250
			}
251
			break;
252
		default:
253
			$dfltmsg = true;
254
			$dontshow = true;
255
			break;
256
	}
257
}
258

    
259
if ($_POST) {
260
	unset($input_errors);
261

    
262
	if ($addnewaltq) {
263
		$altq =& new altq_root_queue();
264
		$altq->SetInterface($interface);
265

    
266
		switch ($altq->GetBwscale()) {
267
				case "Mb":
268
					$factor = 1000 * 1000;
269
					break;
270
				case "Kb":
271
					$factor = 1000;
272
					break;
273
				case "b":
274
					$factor = 1;
275
					break;
276
				case "Gb":
277
					$factor = 1000 * 1000 * 1000;
278
					break;
279
				case "%": /* We don't use it for root_XXX queues. */
280
				default: /* XXX assume Kb by default. */
281
					$factor = 1000;
282
					break;
283
			}
284

    
285
		$altq->SetAvailableBandwidth($altq->GetBandwidth() * $factor);
286
		$altq->ReadConfig($_POST);
287
		$altq->validate_input($_POST, $input_errors);
288
		if (!$input_errors) {
289
			unset($tmppath);
290
			$tmppath[] = $altq->GetInterface();
291
			$altq->SetLink($tmppath);
292
			$altq->wconfig();
293
			if (write_config()) {
294
				mark_subsystem_dirty('shaper');
295
			}
296
			$can_enable = true;
297
			$can_add = true;
298
		}
299

    
300
		read_altq_config();
301
		$output_form .= $altq->build_form();
302

    
303
	} else if ($parentqueue) { /* Add a new queue */
304
		$qtmp =& $altq->find_queue($interface, $parentqueue);
305
		if ($qtmp) {
306
			$tmppath =& $qtmp->GetLink();
307
			array_push($tmppath, $qname);
308
			$tmp =& $qtmp->add_queue($interface, $_POST, $tmppath, $input_errors);
309
			if (!$input_errors) {
310
				array_pop($tmppath);
311
				$tmp->wconfig();
312
				$can_enable = true;
313
				if ($tmp->CanHaveChildren() && $can_enable) {
314
					if ($tmp->GetDefault() <> "") {
315
						$can_add = false;
316
					} else {
317
						$can_add = true;
318
					}
319
				} else {
320
					$can_add = false;
321
				}
322
				if (write_config()) {
323
					mark_subsystem_dirty('shaper');
324
				}
325
				$can_enable = true;
326
				if ($altq->GetScheduler() != "PRIQ") { /* XXX */
327
					if ($tmp->GetDefault() <> "") {
328
						$can_add = false;
329
					} else {
330
						$can_add = true;
331
					}
332
				}
333
			}
334
			read_altq_config();
335
			$output_form .= $tmp->build_form();
336
		} else {
337
			$input_errors[] = gettext("Could not add new queue.");
338
		}
339
	} else if ($_POST['apply']) {
340
		write_config();
341

    
342
		$retval = 0;
343
		$retval = filter_configure();
344
		$savemsg = get_std_save_message($retval);
345

    
346
		if (stristr($retval, "error") <> true) {
347
			$savemsg = get_std_save_message($retval);
348
		} else {
349
			$savemsg = $retval;
350
		}
351

    
352
		/* reset rrd queues */
353
		system("rm -f /var/db/rrd/*queuedrops.rrd");
354
		system("rm -f /var/db/rrd/*queues.rrd");
355
		enable_rrd_graphing();
356

    
357
		clear_subsystem_dirty('shaper');
358

    
359
		if ($queue) {
360
			$output_form .= $queue->build_form();
361
			$dontshow = false;
362
		} else {
363
			$output_form .= $default_shaper_message;
364
			$dontshow = true;
365
		}
366
	} else if ($queue) {
367
		$queue->validate_input($_POST, $input_errors);
368
		if (!$input_errors) {
369
			$queue->update_altq_queue_data($_POST);
370
			$queue->wconfig();
371
			if (write_config()) {
372
				mark_subsystem_dirty('shaper');
373
			}
374
			$dontshow = false;
375
		}
376
		read_altq_config();
377
		$output_form .= $queue->build_form();
378
	} else	{
379
		$dfltmsg = true;
380
		$dontshow = true;
381
	}
382
	mwexec("killall qstats");
383
}
384

    
385
if (!$_POST && !$_GET) {
386
	$dfltmsg = true;
387
	$dontshow = true;
388
}
389

    
390
if ($queue) {
391
	if ($queue->GetEnabled()) {
392
		$can_enable = true;
393
	} else {
394
		$can_enable = false;
395
	}
396
	if ($queue->CanHaveChildren() && $can_enable) {
397
		if ($altq->GetQname() <> $queue->GetQname() && $queue->GetDefault() <> "") {
398
			$can_add = false;
399
		} else {
400
			$can_add = true;
401
		}
402
	} else {
403
		$can_add = false;
404
	}
405
}
406

    
407
//$pgtitle = "Firewall: Shaper: By Interface View";
408
$closehead = false;
409
include("head.inc");
410

    
411
$tree = '<ul class="tree" >';
412
if (is_array($altq_list_queues)) {
413
	foreach ($altq_list_queues as $tmpaltq) {
414
		$tree .= $tmpaltq->build_tree();
415
	}
416
	$tree .= get_interface_list_to_show();
417
}
418

    
419
$tree .= "</ul>";
420

    
421
if ($queue)
422
	print($queue->build_javascript());
423

    
424
print($newjavascript);
425

    
426
if ($input_errors)
427
	print_input_errors($input_errors);
428

    
429
if ($savemsg)
430
	print_info_box($savemsg, 'success');
431

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

    
435
$tab_array = array();
436
$tab_array[] = array(gettext("By Interface"), true, "firewall_shaper.php");
437
$tab_array[] = array(gettext("By Queue"), false, "firewall_shaper_queues.php");
438
$tab_array[] = array(gettext("Limiter"), false, "firewall_shaper_vinterface.php");
439
$tab_array[] = array(gettext("Layer7"), false, "firewall_shaper_layer7.php");
440
$tab_array[] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
441
display_top_tabs($tab_array);
442

    
443
?>
444
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
445
<script type="text/javascript" src="./tree/tree.js"></script>
446

    
447
<div class="table-responsive">
448
	<table class="table">
449
		<tbody>
450
			<tr class="tabcont">
451
				<td class="col-md-1">
452
<?php
453
// Display the shaper tree
454
print($tree);
455

    
456
if (count($altq_list_queues) > 0) {
457
?>
458
					<a href="firewall_shaper.php?action=resetall" class="btn btn-sm btn-danger"/>
459
						<?=gettext('Remove Shaper')?>
460
					</a>
461
<?php
462
}
463
?>
464
				</td>
465
				<td>
466
<?php
467

    
468
if ($dfltmsg)
469
	print_info_box($default_shaper_msg);
470
else {
471
	// Add global buttons
472
	if (!$dontshow || $newqueue) {
473
		if ($can_add || $addnewaltq) {
474
			if ($queue)
475
				$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=add';
476
			else
477
				$url = 'firewall_shaper.php?interface='. $interface . '&action=add';
478

    
479
			$sform->addGlobal(new Form_Button(
480
				'add',
481
				'Add new Queue',
482
				$url
483
			))->removeClass('btn-default')->addClass('btn-success');
484
		}
485

    
486
		if ($queue)
487
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
488
		else
489
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
490

    
491
		$sform->addGlobal(new Form_Button(
492
			'delete',
493
			$queue ? 'Delete this queue':'Disable shaper on interface',
494
			$url
495
		))->removeClass('btn-default')->addClass('btn-danger');
496
	}
497

    
498
	// Print the form
499
	print($sform);
500
}
501
?>
502
				</td>
503
			</tr>
504
		</tbody>
505
	</table>
506
</div>
507

    
508
<?php
509
include("foot.inc");
(65-65/235)