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
##|+PRIV
57
##|*IDENT=page-firewall-trafficshaper
58
##|*NAME=Firewall: Traffic Shaper
59
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper' page.
60
##|*MATCH=firewall_shaper.php*
61
##|-PRIV
62

    
63
require("guiconfig.inc");
64
require_once("functions.inc");
65
require_once("filter.inc");
66
require_once("shaper.inc");
67
require_once("rrd.inc");
68

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

    
75
$pgtitle = array(gettext("Firewall"), gettext("Traffic Shaper"), gettext("By Interface"));
76
$shortcut_section = "trafficshaper";
77

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

    
85
if ($_GET) {
86
	if ($_GET['queue']) {
87
		$qname = htmlspecialchars(trim($_GET['queue']));
88
	}
89
	if ($_GET['interface']) {
90
		$interface = htmlspecialchars(trim($_GET['interface']));
91
	}
92
	if ($_GET['action']) {
93
		$action = htmlspecialchars($_GET['action']);
94
	}
95
}
96

    
97
if ($_POST) {
98
	if ($_POST['name']) {
99
		$qname = htmlspecialchars(trim($_POST['name']));
100
	}
101
	if ($_POST['interface']) {
102
		$interface = htmlspecialchars(trim($_POST['interface']));
103
	}
104
	if ($_POST['parentqueue']) {
105
		$parentqueue = htmlspecialchars(trim($_POST['parentqueue']));
106
	}
107
}
108

    
109
if ($interface) {
110
	$altq = $altq_list_queues[$interface];
111

    
112
	if ($altq) {
113
		$queue =& $altq->find_queue($interface, $qname);
114
	} else {
115
		$addnewaltq = true;
116
	}
117
}
118

    
119
$dontshow = false;
120
$newqueue = false;
121
$dfltmsg = false;
122

    
123
if ($_GET) {
124
	switch ($action) {
125
		case "delete":
126
			if ($queue) {
127
				$queue->delete_queue();
128
				if (write_config()) {
129
					mark_subsystem_dirty('shaper');
130
				}
131
			}
132

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

    
157
			if (write_config()) {
158
				$retval = 0;
159
				$retval |= filter_configure();
160

    
161
				if (stristr($retval, "error") <> true) {
162
					$savemsg = get_std_save_message($retval);
163
					$class = 'success';
164
				} else {
165
					$savemsg = $retval;
166
					$class = 'warning';
167
				}
168
			} else {
169
				$savemsg = gettext("Unable to write config.xml (Access Denied?).");
170
				$class = 'warning';
171
			}
172

    
173
			$dfltmsg = true;
174

    
175

    
176
		break;
177

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

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

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

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

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

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

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

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

    
298
		read_altq_config();
299
		$sform = $altq->build_form();
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
			$sform = $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

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

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

    
355
		clear_subsystem_dirty('shaper');
356

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

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

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

    
405
include("head.inc");
406

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

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

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

    
421
print($newjavascript);
422

    
423
if ($input_errors) {
424
	print_input_errors($input_errors);
425
}
426

    
427
if ($savemsg) {
428
	print_info_box($savemsg, $class);
429
}
430

    
431
if (is_subsystem_dirty('shaper')) {
432
	print_apply_box(gettext("The traffic shaper configuration has been changed.") . "<br />" . gettext("You must apply the changes in order for them to take effect."));
433
}
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("Limiters"), false, "firewall_shaper_vinterface.php");
439
$tab_array[] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
440
display_top_tabs($tab_array);
441

    
442
?>
443
<script type="text/javascript" src="./tree/tree.js"></script>
444

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

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

    
466
if (!$dfltmsg && $sform)  {
467
	// Add global buttons
468
	if (!$dontshow || $newqueue) {
469
		if ($can_add || $addnewaltq) {
470
			if ($queue) {
471
				$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=add';
472
			} else {
473
				$url = 'firewall_shaper.php?interface='. $interface . '&action=add';
474
			}
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

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

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

    
496
	}
497

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

    
507
<?php
508
if ($dfltmsg) {
509
?>
510
<div>
511
	<div class="infoblock">
512
		<?php print_info_box($default_shaper_msg, 'info', false); ?>
513
	</div>
514
</div>
515
<?php
516
}
517
include("foot.inc");
(53-53/229)