Project

General

Profile

Download (12.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_shaper.php
5
	Copyright (C) 2004, 2005 Scott Ullrich
6
	Copyright (C) 2008 Ermal Luçi
7
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
/*
32
	pfSense_BUILDER_BINARIES:	/usr/bin/killall
33
	pfSense_MODULE:	shaper
34
*/
35

    
36
##|+PRIV
37
##|*IDENT=page-firewall-trafficshaper
38
##|*NAME=Firewall: Traffic Shaper page
39
##|*DESCR=Allow access to the 'Firewall: Traffic Shaper' page.
40
##|*MATCH=firewall_shaper.php*
41
##|-PRIV
42

    
43
require("guiconfig.inc");
44
require_once("functions.inc");
45
require_once("filter.inc");
46
require_once("shaper.inc");
47
require_once("rrd.inc");
48

    
49
if ($_GET['reset'] <> "") {
50
	/* XXX: Huh, why are we killing php? */
51
	mwexec("killall -9 pfctl php");
52
	exit;
53
}
54

    
55
$pgtitle = array(gettext("Firewall"),gettext("Traffic Shaper"));
56
$shortcut_section = "trafficshaper";
57

    
58
$shaperIFlist = get_configured_interface_with_descr();
59
read_altq_config();
60
/*
61
 * The whole logic in these code maybe can be specified.
62
 * If you find a better way contact me :).
63
 */
64

    
65
if ($_GET) {
66
	if ($_GET['queue']) {
67
		$qname = htmlspecialchars(trim($_GET['queue']));
68
	}
69
	if ($_GET['interface']) {
70
		$interface = htmlspecialchars(trim($_GET['interface']));
71
	}
72
	if ($_GET['action']) {
73
		$action = htmlspecialchars($_GET['action']);
74
	}
75
}
76
if ($_POST) {
77
	if ($_POST['name']) {
78
		$qname = htmlspecialchars(trim($_POST['name']));
79
	}
80
	if ($_POST['interface']) {
81
		$interface = htmlspecialchars(trim($_POST['interface']));
82
	}
83
	if ($_POST['parentqueue']) {
84
		$parentqueue = htmlspecialchars(trim($_POST['parentqueue']));
85
	}
86
}
87

    
88
if ($interface) {
89
	$altq = $altq_list_queues[$interface];
90
	if ($altq) {
91
		$queue =& $altq->find_queue($interface, $qname);
92
	} else {
93
		$addnewaltq = true;
94
	}
95
}
96

    
97
$dontshow = false;
98
$newqueue = false;
99
$output_form = "";
100

    
101
if ($_GET) {
102
	switch ($action) {
103
		case "delete":
104
			if ($queue) {
105
				$queue->delete_queue();
106
				if (write_config()) {
107
					mark_subsystem_dirty('shaper');
108
				}
109
			}
110
			header("Location: firewall_shaper.php");
111
			exit;
112
			break;
113
		case "resetall":
114
			foreach ($altq_list_queues as $altq) {
115
				$altq->delete_all();
116
			}
117
			unset($altq_list_queues);
118
			$altq_list_queues = array();
119
			$tree = "<ul class=\"tree\" >";
120
			$tree .= get_interface_list_to_show();
121
			$tree .= "</ul>";
122
			unset($config['shaper']['queue']);
123
			unset($queue);
124
			unset($altq);
125
			$can_add = false;
126
			$can_enable = false;
127
			$dontshow = true;
128
			foreach ($config['filter']['rule'] as $key => $rule) {
129
				if (isset($rule['wizard']) && $rule['wizard'] == "yes") {
130
					unset($config['filter']['rule'][$key]);
131
				}
132
			}
133
			if (write_config()) {
134
				$retval = 0;
135
				$retval |= filter_configure();
136
				$savemsg = get_std_save_message($retval);
137

    
138
				if (stristr($retval, "error") <> true) {
139
					$savemsg = get_std_save_message($retval);
140
				} else {
141
					$savemsg = $retval;
142
				}
143
			} else {
144
				$savemsg = gettext("Unable to write config.xml (Access Denied?)");
145
			}
146
			$output_form = $default_shaper_message;
147

    
148
			break;
149
		case "add":
150
			/* XXX: Find better way because we shouldn't know about this */
151
			if ($altq) {
152
				switch ($altq->GetScheduler()) {
153
					case "PRIQ":
154
						$q = new priq_queue();
155
						break;
156
					case "FAIRQ":
157
						$q = new fairq_queue();
158
						break;
159
					case "HFSC":
160
						$q = new hfsc_queue();
161
						break;
162
					case "CBQ":
163
						$q = new cbq_queue();
164
						break;
165
					default:
166
						/* XXX: Happens when sched==NONE?! */
167
						$q = new altq_root_queue();
168
						break;
169
				}
170
			} else if ($addnewaltq) {
171
				$q = new altq_root_queue();
172
			} else {
173
			$input_errors[] = gettext("Could not create new queue/discipline!");
174
			}
175

    
176
			if ($q) {
177
				$q->SetInterface($interface);
178
				$output_form .= $q->build_form();
179
				$output_form .= "<input type=\"hidden\" name=\"parentqueue\" id=\"parentqueue\"";
180
				$output_form .= " value=\"".htmlspecialchars($qname)."\" />";
181
				$newjavascript = $q->build_javascript();
182
				unset($q);
183
				$newqueue = true;
184
			}
185
			break;
186
		case "show":
187
			if ($queue) {
188
				$output_form .= $queue->build_form();
189
			} else {
190
				$input_errors[] = gettext("Queue not found!");
191
			}
192
			break;
193
		case "enable":
194
			if ($queue) {
195
					$queue->SetEnabled("on");
196
					$output_form .= $queue->build_form();
197
					if (write_config())
198
						mark_subsystem_dirty('shaper');
199
			} else {
200
					$input_errors[] = gettext("Queue not found!");
201
			}
202
			break;
203
		case "disable":
204
			if ($queue) {
205
				$queue->SetEnabled("");
206
				$output_form .= $queue->build_form();
207
				if (write_config()) {
208
					mark_subsystem_dirty('shaper');
209
				}
210
			} else {
211
					$input_errors[] = gettext("Queue not found!");
212
			}
213
			break;
214
		default:
215
			$output_form .= $default_shaper_msg;
216
			$dontshow = true;
217
			break;
218
	}
219
} else if ($_POST) {
220
	unset($input_errors);
221

    
222
	if ($addnewaltq) {
223
		$altq =& new altq_root_queue();
224
		$altq->SetInterface($interface);
225

    
226
		switch ($altq->GetBwscale()) {
227
			case "Mb":
228
				$factor = 1000 * 1000;
229
				brak;
230
			case "Kb":
231
				$factor = 1000;
232
				break;
233
			case "b":
234
				$factor = 1;
235
				break;
236
			case "Gb":
237
				$factor = 1000 * 1000 * 1000;
238
				break;
239
			case "%": /* We don't use it for root_XXX queues. */
240
			default: /* XXX assume Kb by default. */
241
				$factor = 1000;
242
				break;
243
		}
244
		$altq->SetAvailableBandwidth($altq->GetBandwidth() * $factor);
245
		$altq->ReadConfig($_POST);
246
		$altq->validate_input($_POST, $input_errors);
247
		if (!$input_errors) {
248
			unset($tmppath);
249
			$tmppath[] = $altq->GetInterface();
250
			$altq->SetLink($tmppath);
251
			$altq->wconfig();
252
			if (write_config()) {
253
				mark_subsystem_dirty('shaper');
254
			}
255
			$can_enable = true;
256
			$can_add = true;
257
		}
258
		read_altq_config();
259
		$output_form .= $altq->build_form();
260

    
261
	} else if ($parentqueue) { /* Add a new queue */
262
		$qtmp =& $altq->find_queue($interface, $parentqueue);
263
		if ($qtmp) {
264
			$tmppath =& $qtmp->GetLink();
265
			array_push($tmppath, $qname);
266
			$tmp =& $qtmp->add_queue($interface, $_POST, $tmppath, $input_errors);
267
			if (!$input_errors) {
268
				array_pop($tmppath);
269
				$tmp->wconfig();
270
				$can_enable = true;
271
				if ($tmp->CanHaveChildren() && $can_enable) {
272
					if ($tmp->GetDefault() <> "") {
273
						$can_add = false;
274
					} else {
275
						$can_add = true;
276
					}
277
				} else {
278
					$can_add = false;
279
				}
280
				if (write_config()) {
281
					mark_subsystem_dirty('shaper');
282
				}
283
				$can_enable = true;
284
				if ($altq->GetScheduler() != "PRIQ") { /* XXX */
285
					if ($tmp->GetDefault() <> "") {
286
						$can_add = false;
287
					} else {
288
						$can_add = true;
289
					}
290
				}
291
			}
292
			read_altq_config();
293
			$output_form .= $tmp->build_form();
294
		} else {
295
			$input_errors[] = gettext("Could not add new queue.");
296
		}
297
	} else if ($_POST['apply']) {
298
		write_config();
299

    
300
		$retval = 0;
301
		$retval = filter_configure();
302
		$savemsg = get_std_save_message($retval);
303

    
304
		if (stristr($retval, "error") <> true) {
305
			$savemsg = get_std_save_message($retval);
306
		} else {
307
			$savemsg = $retval;
308
		}
309

    
310
		/* reset rrd queues */
311
		system("rm -f /var/db/rrd/*queuedrops.rrd");
312
		system("rm -f /var/db/rrd/*queues.rrd");
313
		enable_rrd_graphing();
314

    
315
		clear_subsystem_dirty('shaper');
316

    
317
		if ($queue) {
318
			$output_form .= $queue->build_form();
319
			$dontshow = false;
320
		} else {
321
			$output_form .= $default_shaper_message;
322
			$dontshow = true;
323
		}
324
	} else if ($queue) {
325
		$queue->validate_input($_POST, $input_errors);
326
		if (!$input_errors) {
327
			$queue->update_altq_queue_data($_POST);
328
			$queue->wconfig();
329
			if (write_config()) {
330
				mark_subsystem_dirty('shaper');
331
			}
332
			$dontshow = false;
333
		}
334
		read_altq_config();
335
		$output_form .= $queue->build_form();
336
	} else  {
337
		$output_form .= $default_shaper_msg;
338
		$dontshow = true;
339
	}
340
	mwexec("killall qstats");
341
} else {
342
	$output_form .= $default_shaper_msg;
343
	$dontshow = true;
344
}
345

    
346
if ($queue) {
347
	if ($queue->GetEnabled()) {
348
		$can_enable = true;
349
	} else {
350
		$can_enable = false;
351
	}
352
	if ($queue->CanHaveChildren() && $can_enable) {
353
		if ($altq->GetQname() <> $queue->GetQname() && $queue->GetDefault() <> "") {
354
			$can_add = false;
355
		} else {
356
			$can_add = true;
357
		}
358
	} else {
359
		$can_add = false;
360
	}
361
}
362

    
363
$tree = "<ul class=\"tree\" >";
364
if (is_array($altq_list_queues)) {
365
	foreach ($altq_list_queues as $tmpaltq) {
366
		$tree .= $tmpaltq->build_tree();
367
	}
368
	$tree .= get_interface_list_to_show();
369
}
370
$tree .= "</ul>";
371

    
372
if (!$dontshow || $newqueue) {
373

    
374
	$output_form .= "<tr><td width=\"22%\" valign=\"middle\" class=\"vncellreq\">";
375
	$output_form .= "<br />" . gettext("Queue Actions") . "<br />";
376
	$output_form .= "</td><td valign=\"middle\" class=\"vncellreq\" width=\"78%\"><br />";
377

    
378
	$output_form .= "<input type=\"submit\" name=\"Submit\" value=\"" . gettext("Save") . "\" class=\"formbtn\" />";
379
	if ($can_add || $addnewaltq) {
380
		$output_form .= "<a href=\"firewall_shaper.php?interface=";
381
		$output_form .= $interface;
382
		if ($queue) {
383
			$output_form .= "&amp;queue=" . $queue->GetQname();
384
		}
385
		$output_form .= "&amp;action=add\">";
386
		$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"add\" value=\"" . gettext("Add new queue") . "\" />";
387
		$output_form .= "</a>";
388
	}
389
	$output_form .= "<a href=\"firewall_shaper.php?interface=";
390
	$output_form .= $interface . "&amp;queue=";
391
	if ($queue) {
392
		$output_form .= "&amp;queue=" . $queue->GetQname();
393
	}
394
	$output_form .= "&amp;action=delete\">";
395
	$output_form .= "<input type=\"button\" class=\"formbtn\" name=\"delete\"";
396
	if ($queue) {
397
		$output_form .= " value=\"" . gettext("Delete this queue") . "\" />";
398
	} else {
399
		$output_form .= " value=\"" . gettext("Disable shaper on interface") . "\" />";
400
	}
401
	$output_form .= "</a>";
402
	$output_form .= "<br /></td></tr>";
403
	$output_form .= "</table>";
404
} else {
405
	$output_form .= "</table>";
406
}
407

    
408
$output = "<table  summary=\"output form\">";
409
$output .= $output_form;
410

    
411
//$pgtitle = "Firewall: Shaper: By Interface View";
412
$closehead = false;
413
include("head.inc");
414
?>
415
<link rel="stylesheet" type="text/css" media="all" href="./tree/tree.css" />
416
<script type="text/javascript" src="./tree/tree.js"></script>
417
</head>
418

    
419
<body link="#0000CC" vlink="#0000CC" alink="#0000CC" >
420
<?php
421
if ($queue) {
422
		echo $queue->build_javascript();
423
}
424
echo $newjavascript;
425

    
426
include("fbegin.inc");
427
?>
428
<div id="inputerrors"></div>
429
<?php if ($input_errors) print_input_errors($input_errors); ?>
430

    
431
<form action="firewall_shaper.php" method="post" id="iform" name="iform">
432

    
433
<?php if ($savemsg) print_info_box($savemsg); ?>
434
<?php if (is_subsystem_dirty('shaper')): ?><p>
435
<?php print_info_box_np(gettext("The traffic shaper configuration has been changed.")."<br />".gettext("You must apply the changes in order for them to take effect."));?><br /></p>
436
<?php endif; ?>
437
<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="traffic shaper">
438
	<tr><td>
439
<?php
440
	$tab_array = array();
441
	$tab_array[0] = array(gettext("By Interface"), true, "firewall_shaper.php");
442
	$tab_array[1] = array(gettext("By Queue"), false, "firewall_shaper_queues.php");
443
	$tab_array[2] = array(gettext("Limiter"), false, "firewall_shaper_vinterface.php");
444
	$tab_array[3] = array(gettext("Layer7"), false, "firewall_shaper_layer7.php");
445
	$tab_array[4] = array(gettext("Wizards"), false, "firewall_shaper_wizards.php");
446
	display_top_tabs($tab_array);
447
?>
448
	</td></tr>
449
	<tr>
450
		<td>
451
			<div id="mainarea">
452
				<table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0" summary="main area">
453
<?php if (count($altq_list_queues) > 0): ?>
454
					<tr class="tabcont">
455
						<td width="25%" align="left">
456
							<a href="firewall_shaper.php?action=resetall" >
457
								<input type="button" value="<?=gettext("Remove Shaper")?>" class="formbtn" />
458
							</a>
459
						</td>
460
						<td width="75%"> </td>
461
					</tr>
462
<?php endif; ?>
463
					<tr>
464
						<td width="25%" valign="top" align="left">
465
						<?php
466
							echo $tree;
467
						?>
468
						</td>
469
						<td width="75%" valign="top" align="center">
470
							<div id="shaperarea" style="position:relative">
471
							<?php
472
								echo $output;
473
							?>
474
							</div>
475
						</td>
476
					</tr>
477
				</table>
478
			</div>
479
		</td>
480
	</tr>
481
</table>
482
</form>
483
<?php include("fend.inc"); ?>
484
</body>
485
</html>
(75-75/256)