Project

General

Profile

Download (12 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_once("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! Any recent changes may need to be applied first.");
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
		$altq->ReadConfig($_POST);
264
		$altq->validate_input($_POST, $input_errors);
265
		if (!$input_errors) {
266
			unset($tmppath);
267
			$tmppath[] = $altq->GetInterface();
268
			$altq->SetLink($tmppath);
269
			$altq->wconfig();
270
			if (write_config()) {
271
				mark_subsystem_dirty('shaper');
272
			}
273
			$can_enable = true;
274
			$can_add = true;
275
		}
276

    
277
		read_altq_config();
278
		$sform = $altq->build_form();
279
	} else if ($parentqueue) { /* Add a new queue */
280
		$qtmp =& $altq->find_queue($interface, $parentqueue);
281
		if ($qtmp) {
282
			$tmppath =& $qtmp->GetLink();
283
			array_push($tmppath, $qname);
284
			$tmp =& $qtmp->add_queue($interface, $_POST, $tmppath, $input_errors);
285
			if (!$input_errors) {
286
				array_pop($tmppath);
287
				$tmp->wconfig();
288
				$can_enable = true;
289
				if ($tmp->CanHaveChildren() && $can_enable) {
290
					if ($tmp->GetDefault() <> "") {
291
						$can_add = false;
292
					} else {
293
						$can_add = true;
294
					}
295
				} else {
296
					$can_add = false;
297
				}
298
				if (write_config()) {
299
					mark_subsystem_dirty('shaper');
300
				}
301
				$can_enable = true;
302
				if ($altq->GetScheduler() != "PRIQ") { /* XXX */
303
					if ($tmp->GetDefault() <> "") {
304
						$can_add = false;
305
					} else {
306
						$can_add = true;
307
					}
308
				}
309
			}
310
			read_altq_config();
311
			$sform = $tmp->build_form();
312
		} else {
313
			$input_errors[] = gettext("Could not add new queue.");
314
		}
315
	} else if ($_POST['apply']) {
316
		write_config();
317

    
318
		$retval = 0;
319
		$retval = filter_configure();
320

    
321
		if (stristr($retval, "error") <> true) {
322
			$savemsg = get_std_save_message($retval);
323
			$class = 'success';
324
		} else {
325
			$savemsg = $retval;
326
			$class = 'warning';
327
		}
328

    
329
		/* reset rrd queues */
330
		system("rm -f /var/db/rrd/*queuedrops.rrd");
331
		system("rm -f /var/db/rrd/*queues.rrd");
332
		enable_rrd_graphing();
333

    
334
		clear_subsystem_dirty('shaper');
335

    
336
		if ($queue) {
337
			$sform = $queue->build_form();
338
			$dontshow = false;
339
		} else {
340
			$sform = $default_shaper_message;
341
			$dontshow = true;
342
		}
343
	} else if ($queue) {
344
		$queue->validate_input($_POST, $input_errors);
345
		if (!$input_errors) {
346
			$queue->update_altq_queue_data($_POST);
347
			$queue->wconfig();
348
			if (write_config()) {
349
				mark_subsystem_dirty('shaper');
350
			}
351
			$dontshow = false;
352
		}
353
		read_altq_config();
354
		$sform = $queue->build_form();
355
	} else	{
356
		$dfltmsg = true;
357
		$dontshow = true;
358
	}
359
	mwexec("killall qstats");
360
}
361

    
362
if (!$_POST && !$_GET) {
363
	$dfltmsg = true;
364
	$dontshow = true;
365
}
366

    
367
if ($queue) {
368
	if ($queue->GetEnabled()) {
369
		$can_enable = true;
370
	} else {
371
		$can_enable = false;
372
	}
373
	if ($queue->CanHaveChildren() && $can_enable) {
374
		if ($altq->GetQname() <> $queue->GetQname() && $queue->GetDefault() <> "") {
375
			$can_add = false;
376
		} else {
377
			$can_add = true;
378
		}
379
	} else {
380
		$can_add = false;
381
	}
382
}
383

    
384
include("head.inc");
385

    
386
$tree = '<ul class="tree" >';
387
if (is_array($altq_list_queues)) {
388
	foreach ($altq_list_queues as $tmpaltq) {
389
		$tree .= $tmpaltq->build_tree();
390
	}
391
	$tree .= get_interface_list_to_show();
392
}
393

    
394
$tree .= "</ul>";
395

    
396
if ($queue) {
397
	print($queue->build_javascript());
398
}
399

    
400
print($newjavascript);
401

    
402
if ($input_errors) {
403
	print_input_errors($input_errors);
404
}
405

    
406
if ($savemsg) {
407
	print_info_box($savemsg, $class);
408
}
409

    
410
if (is_subsystem_dirty('shaper')) {
411
	print_apply_box(gettext("The traffic shaper configuration has been changed.") . "<br />" . gettext("The changes must be applied for them to take effect."));
412
}
413

    
414
$tab_array = array();
415
$tab_array[] = array(gettext("By Interface"), true, "firewall_shaper.php");
416
$tab_array[] = array(gettext("By Queue"), false, "firewall_shaper_queues.php");
417
$tab_array[] = array(gettext("Limiters"), false, "firewall_shaper_vinterface.php");
418
$tab_array[] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
419
display_top_tabs($tab_array);
420

    
421
?>
422
<script type="text/javascript" src="./vendor/tree/tree.js"></script>
423

    
424
<div class="table-responsive">
425
	<table class="table">
426
		<tbody>
427
			<tr class="tabcont">
428
				<td class="col-md-1">
429
<?php
430
// Display the shaper tree
431
print($tree);
432

    
433
if (count($altq_list_queues) > 0) {
434
?>
435
					<a href="firewall_shaper.php?action=resetall" class="btn btn-sm btn-danger">
436
						<i class="fa fa-trash icon-embed-btn"></i>
437
						<?=gettext('Remove Shaper')?>
438
					</a>
439
<?php
440
}
441
?>
442
				</td>
443
				<td>
444
<?php
445

    
446
if (!$dfltmsg && $sform)  {
447
	// Add global buttons
448
	if (!$dontshow || $newqueue) {
449
		if ($can_add || $addnewaltq) {
450
			if ($queue) {
451
				$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=add';
452
			} else {
453
				$url = 'firewall_shaper.php?interface='. $interface . '&action=add';
454
			}
455

    
456
			$sform->addGlobal(new Form_Button(
457
				'add',
458
				'Add new Queue',
459
				$url,
460
				'fa-plus'
461
			))->addClass('btn-success');
462

    
463
		}
464

    
465
		if ($queue) {
466
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
467
		} else {
468
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
469
		}
470

    
471
		$sform->addGlobal(new Form_Button(
472
			'delete',
473
			$queue ? 'Delete this queue':'Disable shaper on interface',
474
			$url,
475
			'fa-trash'
476
		))->addClass('btn-danger');
477

    
478
	}
479

    
480
	print($sform);
481
}
482
?>
483
				</td>
484
			</tr>
485
		</tbody>
486
	</table>
487
</div>
488

    
489
<?php
490
if ($dfltmsg) {
491
?>
492
<div>
493
	<div class="infoblock">
494
		<?php print_info_box($default_shaper_msg, 'info', false); ?>
495
	</div>
496
</div>
497
<?php
498
}
499
include("foot.inc");
(53-53/226)