Project

General

Profile

Download (12.4 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
63
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper' page.
64
##|*MATCH=firewall_shaper.php*
65
##|-PRIV
66

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

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

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

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

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

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

    
113
if ($interface) {
114
	$altq = $altq_list_queues[$interface];
115

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

    
123
$dontshow = false;
124
$newqueue = false;
125
$output_form = "";
126
$dfltmsg = false;
127

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

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

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

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

    
176
			$dfltmsg = true;
177

    
178

    
179
		break;
180

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

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

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

    
254
if ($_POST) {
255
	unset($input_errors);
256

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

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

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

    
295
		read_altq_config();
296
		$output_form .= $altq->build_form();
297

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

    
337
		$retval = 0;
338
		$retval = filter_configure();
339
		$savemsg = get_std_save_message($retval);
340

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

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

    
352
		clear_subsystem_dirty('shaper');
353

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

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

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

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

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

    
414
$tree .= "</ul>";
415

    
416
if ($queue)
417
	print($queue->build_javascript());
418

    
419
print($newjavascript);
420

    
421
if ($input_errors)
422
	print_input_errors($input_errors);
423

    
424
if ($savemsg)
425
	print_info_box($savemsg, 'success');
426

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

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

    
437
?>
438
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
439
<script type="text/javascript" src="./tree/tree.js"></script>
440

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

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

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

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

    
480
		if ($queue)
481
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
482
		else
483
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
484

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

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

    
502
<?php
503
include("foot.inc");
(63-63/228)