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("Interfaces"));
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
				$savemsg = get_std_save_message($retval);
161

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

    
171
			$dfltmsg = true;
172

    
173

    
174
		break;
175

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

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

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

    
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
			} else {
223
				$input_errors[] = gettext("Queue not found!");
224
			}
225
		break;
226
		case "enable":
227
			if ($queue) {
228
				$queue->SetEnabled("on");
229
				$sform = $queue->build_form();
230
				if (write_config()) {
231
					mark_subsystem_dirty('shaper');
232
				}
233
			} else {
234
				$input_errors[] = gettext("Queue not found!");
235
			}
236
			break;
237
		case "disable":
238
			if ($queue) {
239
				$queue->SetEnabled("");
240
				$sform = $queue->build_form();
241
				if (write_config()) {
242
					mark_subsystem_dirty('shaper');
243
				}
244
			} else {
245
				$input_errors[] = gettext("Queue not found!");
246
			}
247
			break;
248
		default:
249
			$dfltmsg = true;
250
			$dontshow = true;
251
			break;
252
	}
253
}
254

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

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

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

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

    
296
		read_altq_config();
297
		$sform = $altq->build_form();
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
			$sform = $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
			$sform = $queue->build_form();
356
			$dontshow = false;
357
		} else {
358
			$sform = $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
		$sform = $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
include("head.inc");
404

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

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

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

    
419
print($newjavascript);
420

    
421
if ($input_errors) {
422
	print_input_errors($input_errors);
423
}
424

    
425
if ($savemsg) {
426
	print_info_box($savemsg, 'success');
427
}
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

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

    
440
?>
441
<script type="text/javascript" src="./tree/tree.js"></script>
442

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

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

    
464
if (!$dfltmsg && $sform)  {
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

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

    
480
		}
481

    
482
		if ($queue) {
483
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
484
		} else {
485
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
486
		}
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

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

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