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
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"), gettext("Interfaces"));
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
$dfltmsg = false;
126

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

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

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

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

    
175
			$dfltmsg = true;
176

    
177

    
178
		break;
179

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

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

    
208
		if ($q) {
209
			$q->SetInterface($interface);
210
			$sform = $q->build_form();
211
			$sform->addGlobal(new Form_Input(
212
				'parentqueue',
213
				null,
214
				'hidden',
215
				$qname
216
			));
217

    
218
			$newjavascript = $q->build_javascript();
219
			unset($q);
220
			$newqueue = true;
221
		}
222
		break;
223
		case "show":
224
			if ($queue) {
225
				$sform = $queue->build_form();
226
			} else {
227
				$input_errors[] = gettext("Queue not found!");
228
			}
229
		break;
230
		case "enable":
231
			if ($queue) {
232
				$queue->SetEnabled("on");
233
				$sform = $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
				$sform = $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
		$sform = $altq->build_form();
302
	} else if ($parentqueue) { /* Add a new queue */
303
		$qtmp =& $altq->find_queue($interface, $parentqueue);
304
		if ($qtmp) {
305
			$tmppath =& $qtmp->GetLink();
306
			array_push($tmppath, $qname);
307
			$tmp =& $qtmp->add_queue($interface, $_POST, $tmppath, $input_errors);
308
			if (!$input_errors) {
309
				array_pop($tmppath);
310
				$tmp->wconfig();
311
				$can_enable = true;
312
				if ($tmp->CanHaveChildren() && $can_enable) {
313
					if ($tmp->GetDefault() <> "") {
314
						$can_add = false;
315
					} else {
316
						$can_add = true;
317
					}
318
				} else {
319
					$can_add = false;
320
				}
321
				if (write_config()) {
322
					mark_subsystem_dirty('shaper');
323
				}
324
				$can_enable = true;
325
				if ($altq->GetScheduler() != "PRIQ") { /* XXX */
326
					if ($tmp->GetDefault() <> "") {
327
						$can_add = false;
328
					} else {
329
						$can_add = true;
330
					}
331
				}
332
			}
333
			read_altq_config();
334
			$sform = $tmp->build_form();
335
		} else {
336
			$input_errors[] = gettext("Could not add new queue.");
337
		}
338
	} else if ($_POST['apply']) {
339
		write_config();
340

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

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

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

    
356
		clear_subsystem_dirty('shaper');
357

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

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

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

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

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

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

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

    
424
print($newjavascript);
425

    
426
if ($input_errors) {
427
	print_input_errors($input_errors);
428
}
429

    
430
if ($savemsg) {
431
	print_info_box($savemsg, 'success');
432
}
433

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

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

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

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

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

    
470
if (!$dfltmsg)  {
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

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

    
486
		}
487

    
488
		if ($queue) {
489
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
490
		} else {
491
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
492
		}
493

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

    
500
	}
501

    
502
	print($sform);
503
}
504
?>
505
				</td>
506
			</tr>
507
		</tbody>
508
	</table>
509
</div>
510

    
511
<?php
512
if ($dfltmsg) {
513
?>
514
<div>
515
	<div id="infoblock">
516
		<?=print_info_box($default_shaper_msg, info)?>
517
	</div>
518
</div>
519
<?php
520
}
521
include("foot.inc");
(63-63/228)