Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * firewall_shaper.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
##|+PRIV
23
##|*IDENT=page-firewall-trafficshaper
24
##|*NAME=Firewall: Traffic Shaper
25
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper' page.
26
##|*MATCH=firewall_shaper.php*
27
##|-PRIV
28

    
29
require_once("guiconfig.inc");
30
require_once("functions.inc");
31
require_once("filter.inc");
32
require_once("shaper.inc");
33
require_once("rrd.inc");
34

    
35
if ($_GET['reset'] != "") {
36
	/* XXX: Huh, why are we killing php? */
37
	mwexec("killall -9 pfctl php");
38
	exit;
39
}
40

    
41
$pgtitle = array(gettext("Firewall"), gettext("Traffic Shaper"), gettext("By Interface"));
42
$shortcut_section = "trafficshaper";
43

    
44
$shaperIFlist = get_configured_interface_with_descr();
45
read_altq_config();
46
/*
47
 * The whole logic in these code maybe can be specified.
48
 * If you find a better way contact me :).
49
 */
50

    
51
if ($_GET) {
52
	if ($_GET['queue']) {
53
		$qname = htmlspecialchars(trim($_GET['queue']));
54
	}
55
	if ($_GET['interface']) {
56
		$interface = htmlspecialchars(trim($_GET['interface']));
57
	}
58
	if ($_GET['action']) {
59
		$action = htmlspecialchars($_GET['action']);
60
	}
61
}
62

    
63
if ($_POST) {
64
	if ($_POST['name']) {
65
		$qname = htmlspecialchars(trim($_POST['name']));
66
	}
67
	if ($_POST['interface']) {
68
		$interface = htmlspecialchars(trim($_POST['interface']));
69
	}
70
	if ($_POST['parentqueue']) {
71
		$parentqueue = htmlspecialchars(trim($_POST['parentqueue']));
72
	}
73
}
74

    
75
if ($interface) {
76
	$altq = $altq_list_queues[$interface];
77

    
78
	if ($altq) {
79
		$queue =& $altq->find_queue($interface, $qname);
80
	} else {
81
		$addnewaltq = true;
82
	}
83
}
84

    
85
$dontshow = false;
86
$newqueue = false;
87
$dfltmsg = false;
88

    
89
if ($_GET) {
90
	switch ($action) {
91
		case "delete":
92
			if ($queue) {
93
				$queue->delete_queue();
94
				if (write_config()) {
95
					mark_subsystem_dirty('shaper');
96
				}
97
			}
98

    
99
			header("Location: firewall_shaper.php");
100
			exit;
101
			break;
102
		case "resetall":
103
			foreach ($altq_list_queues as $altq) {
104
				$altq->delete_all();
105
			}
106
			unset($altq_list_queues);
107
			$altq_list_queues = array();
108
			$tree = "<ul class=\"tree\" >";
109
			$tree .= get_interface_list_to_show();
110
			$tree .= "</ul>";
111
			unset($config['shaper']['queue']);
112
			unset($queue);
113
			unset($altq);
114
			$can_add = false;
115
			$can_enable = false;
116
			$dontshow = true;
117
			foreach ($config['filter']['rule'] as $key => $rule) {
118
				if (isset($rule['wizard']) && $rule['wizard'] == "yes") {
119
					unset($config['filter']['rule'][$key]);
120
				}
121
			}
122

    
123
			if (write_config()) {
124
				$retval = 0;
125
				$retval |= filter_configure();
126

    
127
				if (stristr($retval, "error") <> true) {
128
					$savemsg = get_std_save_message($retval);
129
					$class = 'success';
130
				} else {
131
					$savemsg = $retval;
132
					$class = 'warning';
133
				}
134
			} else {
135
				$savemsg = gettext("Unable to write config.xml (Access Denied?).");
136
				$class = 'warning';
137
			}
138

    
139
			$dfltmsg = true;
140

    
141

    
142
		break;
143

    
144
	case "add":
145
			/* XXX: Find better way because we shouldn't know about this */
146
		if ($altq) {
147

    
148
			switch ($altq->GetScheduler()) {
149
				case "PRIQ":
150
					$q = new priq_queue();
151
				break;
152
				case "FAIRQ":
153
					$q = new fairq_queue();
154
				break;
155
				case "HFSC":
156
					$q = new hfsc_queue();
157
				break;
158
				case "CBQ":
159
						$q = new cbq_queue();
160
				break;
161
				default:
162
					/* XXX: Happens when sched==NONE?! */
163
					$q = new altq_root_queue();
164
				break;
165
			}
166
		} else if ($addnewaltq) {
167
			$q = new altq_root_queue();
168
		} else {
169
			$input_errors[] = gettext("Could not create new queue/discipline! Any recent changes may need to be applied first.");
170
		}
171

    
172
		if ($q) {
173
			$q->SetInterface($interface);
174
			$sform = $q->build_form();
175
			$sform->addGlobal(new Form_Input(
176
				'parentqueue',
177
				null,
178
				'hidden',
179
				$qname
180
			));
181

    
182
			$newjavascript = $q->build_javascript();
183
			unset($q);
184
			$newqueue = true;
185
		}
186
		break;
187
		case "show":
188
			if ($queue) {
189
				$sform = $queue->build_form();
190
			} else {
191
				$input_errors[] = gettext("Queue not found!");
192
			}
193
		break;
194
		case "enable":
195
			if ($queue) {
196
				$queue->SetEnabled("on");
197
				$sform = $queue->build_form();
198
				if (write_config()) {
199
					mark_subsystem_dirty('shaper');
200
				}
201
			} else {
202
				$input_errors[] = gettext("Queue not found!");
203
			}
204
			break;
205
		case "disable":
206
			if ($queue) {
207
				$queue->SetEnabled("");
208
				$sform = $queue->build_form();
209
				if (write_config()) {
210
					mark_subsystem_dirty('shaper');
211
				}
212
			} else {
213
				$input_errors[] = gettext("Queue not found!");
214
			}
215
			break;
216
		default:
217
			$dfltmsg = true;
218
			$dontshow = true;
219
			break;
220
	}
221
}
222

    
223
if ($_POST) {
224
	unset($input_errors);
225

    
226
	if ($addnewaltq) {
227
		$altq =& new altq_root_queue();
228
		$altq->SetInterface($interface);
229
		$altq->ReadConfig($_POST);
230
		$altq->validate_input($_POST, $input_errors);
231
		if (!$input_errors) {
232
			unset($tmppath);
233
			$tmppath[] = $altq->GetInterface();
234
			$altq->SetLink($tmppath);
235
			$altq->wconfig();
236
			if (write_config()) {
237
				mark_subsystem_dirty('shaper');
238
			}
239
			$can_enable = true;
240
			$can_add = true;
241
		}
242

    
243
		read_altq_config();
244
		$sform = $altq->build_form();
245
	} else if ($parentqueue) { /* Add a new queue */
246
		$qtmp =& $altq->find_queue($interface, $parentqueue);
247
		if ($qtmp) {
248
			$tmppath =& $qtmp->GetLink();
249
			array_push($tmppath, $qname);
250
			$tmp =& $qtmp->add_queue($interface, $_POST, $tmppath, $input_errors);
251
			if (!$input_errors) {
252
				array_pop($tmppath);
253
				$tmp->wconfig();
254
				$can_enable = true;
255
				if ($tmp->CanHaveChildren() && $can_enable) {
256
					if ($tmp->GetDefault() <> "") {
257
						$can_add = false;
258
					} else {
259
						$can_add = true;
260
					}
261
				} else {
262
					$can_add = false;
263
				}
264
				if (write_config()) {
265
					mark_subsystem_dirty('shaper');
266
				}
267
				$can_enable = true;
268
				if ($altq->GetScheduler() != "PRIQ") { /* XXX */
269
					if ($tmp->GetDefault() <> "") {
270
						$can_add = false;
271
					} else {
272
						$can_add = true;
273
					}
274
				}
275
			}
276
			read_altq_config();
277
			$sform = $tmp->build_form();
278
		} else {
279
			$input_errors[] = gettext("Could not add new queue.");
280
		}
281
	} else if ($_POST['apply']) {
282
		write_config();
283

    
284
		$retval = 0;
285
		$retval = filter_configure();
286

    
287
		if (stristr($retval, "error") <> true) {
288
			$savemsg = get_std_save_message($retval);
289
			$class = 'success';
290
		} else {
291
			$savemsg = $retval;
292
			$class = 'warning';
293
		}
294

    
295
		/* reset rrd queues */
296
		system("rm -f /var/db/rrd/*queuedrops.rrd");
297
		system("rm -f /var/db/rrd/*queues.rrd");
298
		enable_rrd_graphing();
299

    
300
		clear_subsystem_dirty('shaper');
301

    
302
		if ($queue) {
303
			$sform = $queue->build_form();
304
			$dontshow = false;
305
		} else {
306
			$sform = $default_shaper_message;
307
			$dontshow = true;
308
		}
309
	} else if ($queue) {
310
		$queue->validate_input($_POST, $input_errors);
311
		if (!$input_errors) {
312
			$queue->update_altq_queue_data($_POST);
313
			$queue->wconfig();
314
			if (write_config()) {
315
				mark_subsystem_dirty('shaper');
316
			}
317
			$dontshow = false;
318
		}
319
		read_altq_config();
320
		$sform = $queue->build_form();
321
	} else	{
322
		$dfltmsg = true;
323
		$dontshow = true;
324
	}
325
	mwexec("killall qstats");
326
}
327

    
328
if (!$_POST && !$_GET) {
329
	$dfltmsg = true;
330
	$dontshow = true;
331
}
332

    
333
if ($queue) {
334
	if ($queue->GetEnabled()) {
335
		$can_enable = true;
336
	} else {
337
		$can_enable = false;
338
	}
339
	if ($queue->CanHaveChildren() && $can_enable) {
340
		if ($altq->GetQname() <> $queue->GetQname() && $queue->GetDefault() <> "") {
341
			$can_add = false;
342
		} else {
343
			$can_add = true;
344
		}
345
	} else {
346
		$can_add = false;
347
	}
348
}
349

    
350
include("head.inc");
351

    
352
$tree = '<ul class="tree" >';
353
if (is_array($altq_list_queues)) {
354
	foreach ($altq_list_queues as $tmpaltq) {
355
		$tree .= $tmpaltq->build_tree();
356
	}
357
	$tree .= get_interface_list_to_show();
358
}
359

    
360
$tree .= "</ul>";
361

    
362
if ($queue) {
363
	print($queue->build_javascript());
364
}
365

    
366
print($newjavascript);
367

    
368
if ($input_errors) {
369
	print_input_errors($input_errors);
370
}
371

    
372
if ($savemsg) {
373
	print_info_box($savemsg, $class);
374
}
375

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

    
380
$tab_array = array();
381
$tab_array[] = array(gettext("By Interface"), true, "firewall_shaper.php");
382
$tab_array[] = array(gettext("By Queue"), false, "firewall_shaper_queues.php");
383
$tab_array[] = array(gettext("Limiters"), false, "firewall_shaper_vinterface.php");
384
$tab_array[] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
385
display_top_tabs($tab_array);
386

    
387
?>
388
<script type="text/javascript" src="./vendor/tree/tree.js"></script>
389

    
390
<div class="table-responsive">
391
	<table class="table">
392
		<tbody>
393
			<tr class="tabcont">
394
				<td class="col-md-1">
395
<?php
396
// Display the shaper tree
397
print($tree);
398

    
399
if (count($altq_list_queues) > 0) {
400
?>
401
					<a href="firewall_shaper.php?action=resetall" class="btn btn-sm btn-danger">
402
						<i class="fa fa-trash icon-embed-btn"></i>
403
						<?=gettext('Remove Shaper')?>
404
					</a>
405
<?php
406
}
407
?>
408
				</td>
409
				<td>
410
<?php
411

    
412
if (!$dfltmsg && $sform)  {
413
	// Add global buttons
414
	if (!$dontshow || $newqueue) {
415
		if ($can_add || $addnewaltq) {
416
			if ($queue) {
417
				$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=add';
418
			} else {
419
				$url = 'firewall_shaper.php?interface='. $interface . '&action=add';
420
			}
421

    
422
			$sform->addGlobal(new Form_Button(
423
				'add',
424
				'Add new Queue',
425
				$url,
426
				'fa-plus'
427
			))->addClass('btn-success');
428

    
429
		}
430

    
431
		if ($queue) {
432
			$url = 'firewall_shaper.php?interface='. $interface . '&queue=' . $queue->GetQname() . '&action=delete';
433
		} else {
434
			$url = 'firewall_shaper.php?interface='. $interface . '&action=delete';
435
		}
436

    
437
		$sform->addGlobal(new Form_Button(
438
			'delete',
439
			$queue ? 'Delete this queue':'Disable shaper on interface',
440
			$url,
441
			'fa-trash'
442
		))->addClass('btn-danger');
443

    
444
	}
445

    
446
	print($sform);
447
}
448
?>
449
				</td>
450
			</tr>
451
		</tbody>
452
	</table>
453
</div>
454

    
455
<?php
456
if ($dfltmsg) {
457
?>
458
<div>
459
	<div class="infoblock">
460
		<?php print_info_box($default_shaper_msg, 'info', false); ?>
461
	</div>
462
</div>
463
<?php
464
}
465
include("foot.inc");
(51-51/225)