Project

General

Profile

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

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

    
67
require_once('classes/Form.class.php');
68

    
69
require("guiconfig.inc");
70
require_once("functions.inc");
71
require_once("filter.inc");
72
require_once("shaper.inc");
73
require_once("rrd.inc");
74

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

    
81
$pgtitle = array(gettext("Firewall"), gettext("Traffic Shaper"));
82
$shortcut_section = "trafficshaper";
83

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

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

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

    
115
if ($interface) {
116
	$altq = $altq_list_queues[$interface];
117

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

    
125
$dontshow = false;
126
$newqueue = false;
127
$output_form = "";
128
$dfltmsg = false;
129

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

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

    
164
			if (write_config()) {
165
				$retval = 0;
166
				$retval |= filter_configure();
167
				$savemsg = get_std_save_message($retval);
168

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

    
178
			$dfltmsg = true;
179

    
180

    
181
		break;
182

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

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

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

    
256
if ($_POST) {
257
	unset($input_errors);
258

    
259
	if ($addnewaltq) {
260
		$altq =& new altq_root_queue();
261
		$altq->SetInterface($interface);
262

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

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

    
297
		read_altq_config();
298
		$output_form .= $altq->build_form();
299

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

    
339
		$retval = 0;
340
		$retval = filter_configure();
341
		$savemsg = get_std_save_message($retval);
342

    
343
		if (stristr($retval, "error") <> true) {
344
			$savemsg = get_std_save_message($retval);
345
		} else {
346
			$savemsg = $retval;
347
		}
348

    
349
		/* reset rrd queues */
350
		system("rm -f /var/db/rrd/*queuedrops.rrd");
351
		system("rm -f /var/db/rrd/*queues.rrd");
352
		enable_rrd_graphing();
353

    
354
		clear_subsystem_dirty('shaper');
355

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

    
382
if (!$_POST && !$_GET) {
383
	$dfltmsg = true;
384
	$dontshow = true;
385
}
386

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

    
404
//$pgtitle = "Firewall: Shaper: By Interface View";
405
$closehead = false;
406
include("head.inc");
407

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

    
416
$tree .= "</ul>";
417

    
418
if ($queue)
419
	print($queue->build_javascript());
420

    
421
print($newjavascript);
422

    
423
if ($input_errors)
424
	print_input_errors($input_errors);
425

    
426
if ($savemsg)
427
	print_info_box($savemsg, 'success');
428

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

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

    
440
?>
441
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
442
<script type="text/javascript" src="./tree/tree.js"></script>
443

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

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

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

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

    
483
		if ($queue)
484
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
485
		else
486
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
487

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

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

    
505
<?php
506
include("foot.inc");
(64-64/234)