Merge remote-tracking branch 'origin/next' into thirteen
[people/pmueller/ipfire-2.x.git] / config / qos / makeqosscripts.pl
1 #!/usr/bin/perl
2 ###############################################################################
3 #                                                                             #
4 # IPFire.org - A linux based firewall                                         #
5 # Copyright (C) 2010  IPFire Team  <info@ipfire.org>                          #
6 #                                                                             #
7 # This program is free software: you can redistribute it and/or modify        #
8 # it under the terms of the GNU General Public License as published by        #
9 # the Free Software Foundation, either version 3 of the License, or           #
10 # (at your option) any later version.                                         #
11 #                                                                             #
12 # This program is distributed in the hope that it will be useful,             #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of              #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
15 # GNU General Public License for more details.                                #
16 #                                                                             #
17 # You should have received a copy of the GNU General Public License           #
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
19 #                                                                             #
20 ###############################################################################
21
22 use strict;
23 # enable only the following on debugging purpose
24 # use warnings;
25
26 require '/var/ipfire/general-functions.pl';
27 require "${General::swroot}/lang.pl";
28 require "${General::swroot}/header.pl";
29
30 my %qossettings = ();
31 my %checked = ();
32 my %netsettings = ();
33 my $message = "";
34 my $errormessage = "";
35 my $c = "";
36 my $direntry = "";
37 my $classentry = "";
38 my $subclassentry = "";
39 my $l7ruleentry = "";
40 my $portruleentry = "";
41 my $tosruleentry = "";
42 my @tmp = ();
43 my @classes = ();
44 my @subclasses = ();
45 my @l7rules = ();
46 my @portrules = ();
47 my @tosrules = ();
48 my @tmpline = ();
49 my @classline = ();
50 my @subclassline = ();
51 my @tosruleline = ();
52 my @l7ruleline = ();
53 my @portruleline = ();
54 my @proto = ();
55 my %selected= () ;
56 my $classfile = "/var/ipfire/qos/classes";
57 my $subclassfile = "/var/ipfire/qos/subclasses";
58 my $level7file = "/var/ipfire/qos/level7config";
59 my $portfile = "/var/ipfire/qos/portconfig";
60 my $tosfile = "/var/ipfire/qos/tosconfig";
61
62 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
63
64 $qossettings{'ENABLED'} = 'off';
65 $qossettings{'EDIT'} = 'no';
66 $qossettings{'OUT_SPD'} = '';
67 $qossettings{'INC_SPD'} = '';
68 $qossettings{'DEF_OUT_SPD'} = '';
69 $qossettings{'DEF_INC_SPD'} = '';
70 $qossettings{'DEFCLASS_INC'} = '';
71 $qossettings{'DEFCLASS_OUT'} = '';
72 $qossettings{'ACK'} = '';
73 $qossettings{'MTU'} = '1492';
74 $qossettings{'RED_DEV'} = `cat /var/ipfire/red/iface`;
75 $qossettings{'IMQ_DEV'} = 'imq0';
76 $qossettings{'TOS'} = '';
77 $qossettings{'VALID'} = 'yes';
78 $qossettings{'IMQ_MODE'} = 'PREROUTING';
79 $qossettings{'QLENGTH'} = '1000';
80
81 &General::readhash("${General::swroot}/qos/settings", \%qossettings);
82
83 open( FILE, "< $classfile" ) or die "Unable to read $classfile";
84 @classes = <FILE>;
85 close FILE;
86 open( FILE, "< $subclassfile" ) or die "Unable to read $subclassfile";
87 @subclasses = <FILE>;
88 close FILE;
89 open( FILE, "< $level7file" ) or die "Unable to read $level7file";
90 @l7rules = <FILE>;
91 close FILE;
92 open( FILE, "< $portfile" ) or die "Unable to read $portfile";
93 @portrules = <FILE>;
94 close FILE;
95 open( FILE, "< $tosfile" ) or die "Unable to read $tosfile";
96 @tosrules = <FILE>;
97 close FILE;
98
99 ############################################################################################################################
100 ############################################################################################################################
101
102 print <<END
103 #/bin/bash
104 #################################################
105 # This is an autocreated QoS-Script for         #
106 # IPFIRE                                        #
107 # Copyright by the IPFire Team (GPLv2)          #
108 # www.ipfire.org                                #
109 #################################################
110
111 ### SYSTEMVARIABLES:
112 # RED INTERFACE:        $qossettings{'RED_DEV'}
113 # IMQ DEVICE:           $qossettings{'IMQ_DEV'}
114
115 eval \$(/usr/local/bin/readhash /var/ipfire/main/settings)
116 if [ "\$RRDLOG" == "" ]; then
117         RRDLOG=/var/log/rrd
118 fi
119
120 case "\$1" in
121
122   status)
123         case "\$2" in
124           qdisc)
125                 echo "[qdisc]"
126                 tc -s qdisc show dev $qossettings{'RED_DEV'}
127                 tc -s qdisc show dev $qossettings{'IMQ_DEV'}
128                 exit 0
129           ;;
130           class)
131                 echo "[class]"
132                 tc -s class show dev $qossettings{'RED_DEV'}
133                 tc -s class show dev $qossettings{'IMQ_DEV'}
134                 exit 0
135           ;;
136           filter)
137                 echo "[filter]"
138                 tc -s filter show dev $qossettings{'RED_DEV'}
139                 tc -s filter show dev $qossettings{'IMQ_DEV'}
140                 exit 0
141           ;;
142           iptables)
143                 echo "[iptables]"
144                 iptables -t mangle -n -L QOS-OUT -v -x 2> /dev/null
145                 iptables -t mangle -n -L QOS-INC -v -x 2> /dev/null
146                 iptables -t mangle -n -L QOS-TOS -v -x 2> /dev/null
147                 exit 0
148           ;;
149         esac
150         \$0 \$1 qdisc
151         \$0 \$1 class
152         \$0 \$1 filter
153         \$0 \$1 iptables
154         exit 0
155   ;;
156   start)
157         ###
158         ### $qossettings{'RED_DEV'}
159         ###
160
161         ### INIT KERNEL
162         modprobe sch_htb
163
164         ### SET QUEUE LENGTH & MTU - has just to be tested!!! IMPORTANT
165         ip link set dev $qossettings{'RED_DEV'} qlen $qossettings{'QLENGTH'}
166         #ip link set dev $qossettings{'RED_DEV'} mtu $qossettings{'MTU'}
167
168         ### ADD HTB QDISC FOR $qossettings{'RED_DEV'}
169         tc qdisc add dev $qossettings{'RED_DEV'} root handle 1: htb default $qossettings{'DEFCLASS_OUT'}
170
171         ### MAIN RATE LIMIT
172         tc class add dev $qossettings{'RED_DEV'} parent 1: classid 1:1 htb rate $qossettings{'OUT_SPD'}kbit
173
174         ### CLASSES FOR $qossettings{'RED_DEV'}
175 END
176 ;
177 foreach $classentry (sort @classes)
178 {
179         @classline = split( /\;/, $classentry );
180         if ($qossettings{'RED_DEV'} eq $classline[0]) {
181                 $qossettings{'DEVICE'} = $classline[0];
182                 $qossettings{'CLASS'} = $classline[1];
183                 $qossettings{'PRIO'} = $classline[2];
184                 $qossettings{'RATE'} = $classline[3];
185                 $qossettings{'CEIL'} = $classline[4];
186                 $qossettings{'BURST'} = $classline[5];
187                 $qossettings{'CBURST'} = $classline[6];
188                 print "\ttc class add dev $qossettings{'DEVICE'} parent 1:1 classid 1:$qossettings{'CLASS'} htb rate $qossettings{'RATE'}kbit ceil $qossettings{'CEIL'}kbit prio $qossettings{'PRIO'} ";
189                 if (($qossettings{'BURST'} ne '') && ($qossettings{'BURST'} ne 0)) {
190                         print "burst $qossettings{'BURST'}k ";
191                 }
192                 if (($qossettings{'CBURST'} ne '') && ($qossettings{'CBURST'} ne 0)) {
193                         print "cburst $qossettings{'CBURST'}k";
194                 }
195                 print "\n";
196         }
197 }
198 foreach $subclassentry (sort @subclasses) {
199         @subclassline = split( /\;/, $subclassentry );
200         if ($qossettings{'RED_DEV'} eq $subclassline[0]) {
201                 $qossettings{'DEVICE'} = $subclassline[0];
202                 $qossettings{'CLASS'} = $subclassline[1];
203                 $qossettings{'SCLASS'} = $subclassline[2];
204                 $qossettings{'SPRIO'} = $subclassline[3];
205                 $qossettings{'SRATE'} = $subclassline[4];
206                 $qossettings{'SCEIL'} = $subclassline[5];
207                 $qossettings{'SBURST'} = $subclassline[6];
208                 $qossettings{'SCBURST'} = $subclassline[7];
209                 print "\ttc class add dev $qossettings{'DEVICE'} parent 1:$qossettings{'CLASS'} classid 1:$qossettings{'SCLASS'} htb rate $qossettings{'SRATE'}kbit ceil $qossettings{'SCEIL'}kbit prio $qossettings{'SPRIO'} ";
210                 if ($qossettings{'SBURST'} > 0) {
211                         print "burst $qossettings{'SBURST'}k ";
212                 }
213                 if (($qossettings{'SCBURST'} ne '') && ($qossettings{'SCBURST'} ne 0)) {
214                         print "cburst $qossettings{'CBURST'}k";
215                 }
216                 print "\n";
217         }
218 }
219
220 print "\n\t### ATTACH QDISC TO LEAF CLASSES\n";
221 foreach $classentry (sort @classes)
222 {
223         @classline = split( /\;/, $classentry );
224         if ($qossettings{'RED_DEV'} eq $classline[0]) {
225                 $qossettings{'DEVICE'} = $classline[0];
226                 $qossettings{'CLASS'} = $classline[1];
227                 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 1:$qossettings{'CLASS'} handle $qossettings{'CLASS'}: sfq perturb $qossettings{'SFQ_PERTUB'}\n";
228         }
229 }
230 foreach $subclassentry (sort @subclasses) {
231         @subclassline = split( /\;/, $subclassentry );
232         if ($qossettings{'RED_DEV'} eq $subclassline[0]) {
233                 $qossettings{'DEVICE'} = $subclassline[0];
234                 $qossettings{'SCLASS'} = $subclassline[2];
235                 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 1:$qossettings{'SCLASS'} handle $qossettings{'SCLASS'}: sfq perturb $qossettings{'SFQ_PERTUB'}\n";
236         }
237 }
238 print "\n\t### FILTER TRAFFIC INTO CLASSES\n";
239 foreach $classentry (sort @classes)
240 {
241         @classline = split( /\;/, $classentry );
242         if ($qossettings{'RED_DEV'} eq $classline[0]) {
243                 $qossettings{'DEVICE'} = $classline[0];
244                 $qossettings{'CLASS'} = $classline[1];
245                 print "\ttc filter add dev $qossettings{'DEVICE'} parent 1:0 prio 0 protocol ip handle $qossettings{'CLASS'} fw flowid 1:$qossettings{'CLASS'}\n";
246         }
247 }
248 foreach $subclassentry (sort @subclasses) {
249         @subclassline = split( /\;/, $subclassentry );
250         if ($qossettings{'RED_DEV'} eq $subclassline[0]) {
251                 $qossettings{'DEVICE'} = $subclassline[0];
252                 $qossettings{'CLASS'} = $subclassline[1];
253                 $qossettings{'SCLASS'} = $subclassline[2];
254                 print "\ttc filter add dev $qossettings{'DEVICE'} parent 1:0 prio 0 protocol ip handle $qossettings{'SCLASS'} fw flowid 1:$qossettings{'SCLASS'}\n";
255         }
256 }
257 print <<END
258
259         ### add l7-filter to POSTROUTING chain to see all traffic
260         iptables -t mangle -A POSTROUTING -m layer7 --l7proto unset
261
262         ### ADD QOS-OUT CHAIN TO THE MANGLE TABLE IN IPTABLES
263         iptables -t mangle -N QOS-OUT
264         iptables -t mangle -N QOS-TOS
265         iptables -t mangle -I POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-OUT
266         iptables -t mangle -A POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-TOS
267
268         ### Don't change mark on traffic for the ipsec tunnel
269         iptables -t mangle -A QOS-OUT -m mark --mark 50 -j RETURN
270
271         ### MARK ACKs
272         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags SYN,RST SYN -j TOS --set-tos 4
273         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags SYN,RST SYN -j MARK --set-mark $qossettings{'ACK'}
274         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags SYN,RST SYN -j RETURN
275
276         iptables -t mangle -A QOS-OUT -p icmp -m length --length 40:100 -j MARK --set-mark $qossettings{'ACK'}
277         iptables -t mangle -A QOS-OUT -p icmp -m length --length 40:100 -j RETURN
278
279         iptables -t mangle -A QOS-OUT -p tcp --syn -m length --length 40:68 -j TOS --set-tos 4
280         iptables -t mangle -A QOS-OUT -p tcp --syn -m length --length 40:68 -j MARK --set-mark $qossettings{'ACK'}
281         iptables -t mangle -A QOS-OUT -p tcp --syn -m length --length 40:68 -j RETURN
282
283         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL SYN,ACK -m length --length 40:68 -j TOS --set-tos 4
284         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL SYN,ACK -m length --length 40:68 -j MARK --set-mark $qossettings{'ACK'}
285         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL SYN,ACK -m length --length 40:68 -j RETURN
286
287         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL ACK -m length --length 40:100 -j TOS --set-tos 4
288         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL ACK -m length --length 40:100 -j MARK --set-mark $qossettings{'ACK'}
289         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL ACK -m length --length 40:100 -j RETURN
290
291         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL RST -j TOS --set-tos 4
292         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL RST -j MARK --set-mark $qossettings{'ACK'}
293         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL RST -j RETURN
294
295         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL ACK,RST -j TOS --set-tos 4
296         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL ACK,RST -j MARK --set-mark $qossettings{'ACK'}
297         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL ACK,RST -j RETURN
298
299         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL ACK,FIN -j TOS --set-tos 4
300         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL ACK,FIN -j MARK --set-mark $qossettings{'ACK'}
301         iptables -t mangle -A QOS-OUT -p tcp --tcp-flags ALL ACK,FIN -j RETURN
302
303         ### SET TOS
304 END
305 ;
306         foreach $tosruleentry (sort @tosrules)
307         {
308                 @tosruleline = split( /\;/, $tosruleentry );
309                 $qossettings{'CLASS'} = $tosruleline[0];
310                 $qossettings{'TOS'} = abs $tosruleline[2] * 2;
311                 if ( $tosruleline[1] eq $qossettings{'RED_DEV'} )
312                 {
313                         print "\tiptables -t mangle -A QOS-OUT -m tos --tos $qossettings{'TOS'} -j MARK --set-mark $qossettings{'CLASS'}\n";
314                         print "\tiptables -t mangle -A QOS-OUT -m tos --tos $qossettings{'TOS'} -j RETURN\n";
315                 }
316         }
317
318 print "\n\t### SET PORT-RULES\n";
319         foreach $portruleentry (sort @portrules)
320         {
321                 @portruleline = split( /\;/, $portruleentry );
322                 if ( $portruleline[1] eq $qossettings{'RED_DEV'} )
323                 {
324                         $qossettings{'CLASS'} = $portruleline[0];
325                         $qossettings{'DEVICE'} = $portruleline[1];
326                         $qossettings{'PPROT'} = $portruleline[2];
327                         $qossettings{'QIP'} = $portruleline[3];
328                         $qossettings{'QPORT'} = $portruleline[4];
329                         $qossettings{'DIP'} = $portruleline[5];
330                         $qossettings{'DPORT'} = $portruleline[6];
331                         print "\tiptables -t mangle -A QOS-OUT ";
332                         if ($qossettings{'QIP'} ne ''){
333                                 print "-s $qossettings{'QIP'} ";
334                         }
335                         if ($qossettings{'DIP'} ne ''){
336                                 print "-d $qossettings{'DIP'} ";
337                         }
338                         print "-p $qossettings{'PPROT'} ";
339 #                       if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
340 #                               print "-m multiport ";
341 #                       }
342                         if ($qossettings{'QPORT'} ne ''){
343                                 print "--sport $qossettings{'QPORT'} ";
344                         }
345                         if ($qossettings{'DPORT'} ne ''){
346                                 print "--dport $qossettings{'DPORT'} ";
347                         }
348                         print "-j MARK --set-mark $qossettings{'CLASS'}\n";
349                         print "\tiptables -t mangle -A QOS-OUT ";
350                         if ($qossettings{'QIP'} ne ''){
351                                 print "-s $qossettings{'QIP'} ";
352                         }
353                         if ($qossettings{'DIP'} ne ''){
354                                 print "-d $qossettings{'DIP'} ";
355                         }
356                         print "-p $qossettings{'PPROT'} ";
357 #                       if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
358 #                               print "-m multiport ";
359 #                       }
360                         if ($qossettings{'QPORT'} ne ''){
361                                 print "--sport $qossettings{'QPORT'} ";
362                         }
363                         if ($qossettings{'DPORT'} ne ''){
364                                 print "--dport $qossettings{'DPORT'} ";
365                         }
366                         print "-j RETURN\n\n";
367                 }
368         }
369
370 print <<END
371
372         ### SET LEVEL7-RULES
373 END
374 ;
375         foreach $l7ruleentry (sort @l7rules)
376         {
377                 @l7ruleline = split( /\;/, $l7ruleentry );
378                 if ( $l7ruleline[1] eq $qossettings{'RED_DEV'} )
379                 {
380                         $qossettings{'CLASS'} = $l7ruleline[0];
381                         $qossettings{'DEVICE'} = $l7ruleline[1];
382                         $qossettings{'L7PROT'} = $l7ruleline[2];
383                         $qossettings{'QIP'} = $l7ruleline[3];
384                         $qossettings{'DIP'} = $l7ruleline[4];
385                         print "\tiptables -t mangle -A QOS-OUT ";
386                         if ($qossettings{'QIP'} ne ''){
387                                 print "-s $qossettings{'QIP'} ";
388                         }
389                         if ($qossettings{'DIP'} ne ''){
390                                 print "-d $qossettings{'DIP'} ";
391                         }
392                         print "-m layer7 --l7dir /etc/l7-protocols/protocols --l7proto $qossettings{'L7PROT'} -j MARK --set-mark $qossettings{'CLASS'}\n";
393                         print "\tiptables -t mangle -A QOS-OUT ";
394                         if ($qossettings{'QIP'} ne ''){
395                                 print "-s $qossettings{'QIP'} ";
396                         }
397                         if ($qossettings{'DIP'} ne ''){
398                                 print "-d $qossettings{'DIP'} ";
399                         }
400                         print "-m layer7 --l7dir /etc/l7-protocols/protocols --l7proto $qossettings{'L7PROT'} -j RETURN\n";
401                 }
402         }
403
404 print <<END
405
406         ### REDUNDANT: SET ALL NONMARKED PACKETS TO DEFAULT CLASS
407         iptables -t mangle -A QOS-OUT -m mark --mark 0 -j MARK --set-mark $qossettings{'DEFCLASS_OUT'}
408
409         ###
410         ### $qossettings{'IMQ_DEV'}
411         ###
412
413         ### BRING UP $qossettings{'IMQ_DEV'}
414         if [ `lsmod | grep -q ipt_IMQ` ]; then
415                 insmod ipt_IMQ
416                 sleep 2
417         fi
418         modprobe imq numdevs=1
419         ip link set $qossettings{'IMQ_DEV'} up
420
421         ### SET QUEUE LENGTH & MTU - has just to be tested!!! IMPORTANT
422         ip link set dev $qossettings{'IMQ_DEV'} qlen $qossettings{'QLENGTH'}
423         # ip link set dev $qossettings{'IMQ_DEV'} mtu $qossettings{'MTU'}
424
425         ### ADD HTB QDISC FOR $qossettings{'IMQ_DEV'}
426         tc qdisc add dev $qossettings{'IMQ_DEV'} root handle 2: htb default $qossettings{'DEFCLASS_INC'}
427
428         ### MAIN RATE LIMIT
429         tc class add dev $qossettings{'IMQ_DEV'} parent 2: classid 2:1 htb rate $qossettings{'INC_SPD'}kbit
430
431         ### CLASSES FOR $qossettings{'IMQ_DEV'}
432 END
433 ;
434 foreach $classentry (sort @classes)
435 {
436         @classline = split( /\;/, $classentry );
437         if ($qossettings{'IMQ_DEV'} eq $classline[0]) {
438                 $qossettings{'DEVICE'} = $classline[0];
439                 $qossettings{'CLASS'} = $classline[1];
440                 $qossettings{'PRIO'} = $classline[2];
441                 $qossettings{'RATE'} = $classline[3];
442                 $qossettings{'CEIL'} = $classline[4];
443                 $qossettings{'BURST'} = $classline[5];
444                 $qossettings{'CBURST'} = $classline[6];
445                 print "\ttc class add dev $qossettings{'DEVICE'} parent 2:1 classid 2:$qossettings{'CLASS'} htb rate $qossettings{'RATE'}kbit ceil $qossettings{'CEIL'}kbit prio $qossettings{'PRIO'} ";
446                 if (($qossettings{'BURST'} ne '') && ($qossettings{'BURST'} ne 0)) {
447                         print "burst $qossettings{'BURST'}k ";
448                 }
449                 if (($qossettings{'CBURST'} ne '') && ($qossettings{'CBURST'} ne 0)) {
450                         print "cburst $qossettings{'CBURST'}k";
451                 }
452                 print "\n";
453         }
454 }
455 foreach $subclassentry (sort @subclasses) {
456         @subclassline = split( /\;/, $subclassentry );
457         if ($qossettings{'IMQ_DEV'} eq $subclassline[0]) {
458                 $qossettings{'DEVICE'} = $subclassline[0];
459                 $qossettings{'CLASS'} = $subclassline[1];
460                 $qossettings{'SCLASS'} = $subclassline[2];
461                 $qossettings{'SPRIO'} = $subclassline[3];
462                 $qossettings{'SRATE'} = $subclassline[4];
463                 $qossettings{'SCEIL'} = $subclassline[5];
464                 $qossettings{'SBURST'} = $subclassline[6];
465                 $qossettings{'SCBURST'} = $subclassline[7];
466                 print "\ttc class add dev $qossettings{'DEVICE'} parent 2:$qossettings{'CLASS'} classid 2:$qossettings{'SCLASS'} htb rate $qossettings{'SRATE'}kbit ceil $qossettings{'SCEIL'}kbit prio $qossettings{'SPRIO'} ";
467                 if ($qossettings{'SBURST'} > 0) {
468                         print "burst $qossettings{'SBURST'}k ";
469                 }
470                 if (($qossettings{'SCBURST'} ne '') && ($qossettings{'SCBURST'} ne 0)) {
471                         print "cburst $qossettings{'CBURST'}k";
472                 }
473                 print "\n";
474         }
475 }
476
477 print "\n\t### ATTACH QDISC TO LEAF CLASSES\n";
478 foreach $classentry (sort @classes)
479 {
480         @classline = split( /\;/, $classentry );
481         if ($qossettings{'IMQ_DEV'} eq $classline[0]) {
482                 $qossettings{'DEVICE'} = $classline[0];
483                 $qossettings{'CLASS'} = $classline[1];
484                 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 2:$qossettings{'CLASS'} handle $qossettings{'CLASS'}: fq_codel\n";
485         }
486 }
487 foreach $subclassentry (sort @subclasses) {
488         @subclassline = split( /\;/, $subclassentry );
489         if ($qossettings{'IMQ_DEV'} eq $subclassline[0]) {
490                 $qossettings{'DEVICE'} = $subclassline[0];
491                 $qossettings{'SCLASS'} = $subclassline[2];
492                 print "\ttc qdisc add dev $qossettings{'DEVICE'} parent 2:$qossettings{'SCLASS'} handle $qossettings{'SCLASS'}: fq_codel\n";
493         }
494 }
495 print "\n\t### FILTER TRAFFIC INTO CLASSES\n";
496 foreach $classentry (sort @classes)
497 {
498         @classline = split( /\;/, $classentry );
499         if ($qossettings{'IMQ_DEV'} eq $classline[0]) {
500                 $qossettings{'DEVICE'} = $classline[0];
501                 $qossettings{'CLASS'} = $classline[1];
502                 print "\ttc filter add dev $qossettings{'DEVICE'} parent 2:0 prio 0 protocol ip handle $qossettings{'CLASS'} fw flowid 2:$qossettings{'CLASS'}\n";
503         }
504 }
505 foreach $subclassentry (sort @subclasses) {
506         @subclassline = split( /\;/, $subclassentry );
507         if ($qossettings{'IMQ_DEV'} eq $subclassline[0]) {
508                 $qossettings{'DEVICE'} = $subclassline[0];
509                 $qossettings{'CLASS'} = $subclassline[1];
510                 $qossettings{'SCLASS'} = $subclassline[2];
511                 print "\ttc filter add dev $qossettings{'DEVICE'} parent 2:0 prio 0 protocol ip handle $qossettings{'SCLASS'} fw flowid 2:$qossettings{'SCLASS'}\n";
512         }
513 }
514
515 if ( $qossettings{'IMQ_MODE'} eq 'POSTROUTING' )
516 {
517 print <<END
518
519         ### ADD QOS-INC CHAIN TO THE MANGLE TABLE IN IPTABLES
520         iptables -t mangle -N QOS-INC
521         iptables -t mangle -A POSTROUTING -i $qossettings{'RED_DEV'} -p ah -j RETURN
522         iptables -t mangle -A POSTROUTING -i $qossettings{'RED_DEV'} -p esp -j RETURN
523         iptables -t mangle -A POSTROUTING -i $qossettings{'RED_DEV'} -p ip -j RETURN
524         iptables -t mangle -A POSTROUTING -m mark ! --mark 0 ! -o $qossettings{'RED_DEV'} -j IMQ --todev 0
525         iptables -t mangle -I FORWARD -i $qossettings{'RED_DEV'} -j QOS-INC
526         iptables -t mangle -A FORWARD -i $qossettings{'RED_DEV'} -j QOS-TOS
527
528         ### SET TOS
529 END
530 ;
531 }
532 else
533 {
534 print <<END
535
536         ### ADD QOS-INC CHAIN TO THE MANGLE TABLE IN IPTABLES
537         iptables -t mangle -N QOS-INC
538         iptables -t mangle -A PREROUTING -i $qossettings{'RED_DEV'} -p ah -j RETURN
539         iptables -t mangle -A PREROUTING -i $qossettings{'RED_DEV'} -p esp -j RETURN
540         iptables -t mangle -A PREROUTING -i $qossettings{'RED_DEV'} -p ip -j RETURN
541         iptables -t mangle -A PREROUTING -i $qossettings{'RED_DEV'} -j IMQ --todev 0
542         iptables -t mangle -I PREROUTING -i $qossettings{'RED_DEV'} -j QOS-INC
543         iptables -t mangle -A PREROUTING -i $qossettings{'RED_DEV'} -j QOS-TOS
544
545         ### SET TOS
546 END
547 ;
548 }
549
550         foreach $tosruleentry (sort @tosrules)
551         {
552                 @tosruleline = split( /\;/, $tosruleentry );
553                 $qossettings{'CLASS'} = $tosruleline[0];
554                 $qossettings{'TOS'} = abs $tosruleline[2] * 2;
555                 if ( $tosruleline[1] eq $qossettings{'IMQ_DEV'} )
556                 {
557                         print "\tiptables -t mangle -A QOS-INC -m tos --tos $qossettings{'TOS'} -j MARK --set-mark $qossettings{'CLASS'}\n";
558                         print "\tiptables -t mangle -A QOS-INC -m tos --tos $qossettings{'TOS'} -j RETURN\n";
559                 }
560
561         }
562
563 print "\n\t### SET PORT-RULES\n";
564         foreach $portruleentry (sort @portrules)
565         {
566                 @portruleline = split( /\;/, $portruleentry );
567                 if ( $portruleline[1] eq $qossettings{'IMQ_DEV'} )
568                 {
569                         $qossettings{'CLASS'} = $portruleline[0];
570                         $qossettings{'DEVICE'} = $portruleline[1];
571                         $qossettings{'PPROT'} = $portruleline[2];
572                         $qossettings{'QIP'} = $portruleline[3];
573                         $qossettings{'QPORT'} = $portruleline[4];
574                         $qossettings{'DIP'} = $portruleline[5];
575                         $qossettings{'DPORT'} = $portruleline[6];
576                         print "\tiptables -t mangle -A QOS-INC ";
577                         if ($qossettings{'QIP'} ne ''){
578                                 print "-s $qossettings{'QIP'} ";
579                         }
580                         if ($qossettings{'DIP'} ne ''){
581                                 print "-d $qossettings{'DIP'} ";
582                         }
583                         print "-p $qossettings{'PPROT'} ";
584 #                       if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
585 #                               print "-m multiport ";
586 #                       }
587                         if ($qossettings{'QPORT'} ne ''){
588                                 print "--sport $qossettings{'QPORT'} ";
589                         }
590                         if ($qossettings{'DPORT'} ne ''){
591                                 print "--dport $qossettings{'DPORT'} ";
592                         }
593                         print "-j MARK --set-mark $qossettings{'CLASS'}\n";
594                         print "\tiptables -t mangle -A QOS-INC ";
595                         if ($qossettings{'QIP'} ne ''){
596                                 print "-s $qossettings{'QIP'} ";
597                         }
598                         if ($qossettings{'DIP'} ne ''){
599                                 print "-d $qossettings{'DIP'} ";
600                         }
601                         print "-p $qossettings{'PPROT'} ";
602 #                       if (($qossettings{'QPORT'} ne '') || ($qossettings{'DPORT'} ne '')){
603 #                               print "-m multiport ";
604 #                       }
605                         if ($qossettings{'QPORT'} ne ''){
606                                 print "--sport $qossettings{'QPORT'} ";
607                         }
608                         if ($qossettings{'DPORT'} ne ''){
609                                 print "--dport $qossettings{'DPORT'} ";
610                         }
611                         print "-j RETURN\n\n";
612                 }
613         }
614
615 print <<END
616
617         ### SET LEVEL7-RULES
618 END
619 ;
620         foreach $l7ruleentry (sort @l7rules)
621         {
622                 @l7ruleline = split( /\;/, $l7ruleentry );
623                 if ( $l7ruleline[1] eq $qossettings{'IMQ_DEV'} )
624                 {
625                         $qossettings{'CLASS'} = $l7ruleline[0];
626                         $qossettings{'DEVICE'} = $l7ruleline[1];
627                         $qossettings{'L7PROT'} = $l7ruleline[2];
628                         $qossettings{'QIP'} = $l7ruleline[3];
629                         $qossettings{'DIP'} = $l7ruleline[4];
630                         print "\tiptables -t mangle -A QOS-INC ";
631                         if ($qossettings{'QIP'} ne ''){
632                                 print "-s $qossettings{'QIP'} ";
633                         }
634                         if ($qossettings{'DIP'} ne ''){
635                                 print "-d $qossettings{'DIP'} ";
636                         }
637                         print "-m layer7 --l7dir /etc/l7-protocols/protocols --l7proto $qossettings{'L7PROT'} -j MARK --set-mark $qossettings{'CLASS'}\n";
638                         print "\tiptables -t mangle -A QOS-INC ";
639                         if ($qossettings{'QIP'} ne ''){
640                                 print "-s $qossettings{'QIP'} ";
641                         }
642                         if ($qossettings{'DIP'} ne ''){
643                                 print "-d $qossettings{'DIP'} ";
644                         }
645                         print "-m layer7 --l7dir /etc/l7-protocols/protocols --l7proto $qossettings{'L7PROT'} -j RETURN\n";
646                 }
647         }
648
649 print <<END
650         ### REDUNDANT: SET ALL NONMARKED PACKETS TO DEFAULT CLASS
651         iptables -t mangle -A QOS-INC -m mark --mark 0 -j MARK --set-mark $qossettings{'DEFCLASS_INC'}
652
653         ### SETTING TOS BITS
654 END
655 ;
656         foreach $classentry (sort @classes)
657         {
658                 @classline = split( /\;/, $classentry );
659                 $qossettings{'CLASS'} = $classline[1];
660                 $qossettings{'TOS'} = abs $classline[7] * 2;
661                 if ($qossettings{'TOS'} ne "0") {
662                         print "\tiptables -t mangle -A QOS-TOS -m mark --mark $qossettings{'CLASS'} -j TOS --set-tos $qossettings{'TOS'}\n";
663                         print "\tiptables -t mangle -A QOS-TOS -m mark --mark $qossettings{'CLASS'} -j RETURN\n";
664                 }
665         }
666         foreach $subclassentry (sort @subclasses)
667         {
668                 @subclassline = split( /\;/, $subclassentry );
669                 $qossettings{'SUBCLASS'} = $subclassline[1];
670                 $qossettings{'TOS'} = $subclassline[8];
671                 $qossettings{'TOS'} = abs $qossettings{'TOS'} * 2;
672                 if ($qossettings{'TOS'} ne "0") {
673                         print "\tiptables -t mangle -A QOS-TOS -m mark --mark $qossettings{'SUBCLASS'} -j TOS --set-tos $qossettings{'TOS'}\n";
674                         print "\tiptables -t mangle -A QOS-TOS -m mark --mark $qossettings{'SUBCLASS'} -j RETURN\n";
675                 }
676         }
677
678 print <<END
679
680         ## STARTING COLLECTOR
681         ( sleep 10 && /usr/local/bin/qosd $qossettings{'RED_DEV'} >/dev/null 2>&1) &
682         ( sleep 10 && /usr/local/bin/qosd $qossettings{'IMQ_DEV'} >/dev/null 2>&1) &
683
684         for i in \$(ls \$RRDLOG/class_*.rrd); do
685                 rrdtool update \$i \$(date +%s):
686         done
687
688         echo "Quality of Service was successfully started!"
689         exit 0
690   ;;
691   clear|stop)
692         ### RESET EVERYTHING TO A KNOWN STATE
693         killall qosd >/dev/null 2>&1
694         (sleep 3 && killall -9 qosd &>/dev/null) &
695         # DELETE QDISCS
696         tc qdisc del dev $qossettings{'RED_DEV'} root >/dev/null 2>&1
697         tc qdisc del dev $qossettings{'IMQ_DEV'} root >/dev/null 2>&1
698         # STOP IMQ-DEVICE
699         ip link set $qossettings{'IMQ_DEV'} down >/dev/null 2>&1
700         iptables -t mangle --delete POSTROUTING -i $qossettings{'RED_DEV'} -p ah -j RETURN >/dev/null 2>&1
701         iptables -t mangle --delete POSTROUTING -i $qossettings{'RED_DEV'} -p esp -j RETURN >/dev/null 2>&1
702         iptables -t mangle --delete POSTROUTING -i $qossettings{'RED_DEV'} -p ip -j RETURN >/dev/null 2>&1
703         iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -p ah -j RETURN >/dev/null 2>&1
704         iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -p esp -j RETURN >/dev/null 2>&1
705         iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -p ip -j RETURN >/dev/null 2>&1
706         iptables -t mangle --delete POSTROUTING -m mark ! --mark 0 ! -o $qossettings{'RED_DEV'} -j IMQ --todev 0 >/dev/null 2>&1
707         iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -j IMQ --todev 0  >/dev/null 2>&1
708         # rmmod imq # this crash on 2.6.25.xx
709         # REMOVE & FLUSH CHAINS
710         iptables -t mangle --delete POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-OUT >/dev/null 2>&1
711         iptables -t mangle --delete POSTROUTING -o $qossettings{'RED_DEV'} -j QOS-TOS >/dev/null 2>&1
712         iptables -t mangle --flush  QOS-OUT >/dev/null 2>&1
713         iptables -t mangle --delete-chain QOS-OUT >/dev/null 2>&1
714         iptables -t mangle --delete FORWARD -i $qossettings{'RED_DEV'} -j QOS-INC >/dev/null 2>&1
715         iptables -t mangle --delete FORWARD -i $qossettings{'RED_DEV'} -j QOS-TOS >/dev/null 2>&1
716         iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -j QOS-INC >/dev/null 2>&1
717         iptables -t mangle --delete PREROUTING -i $qossettings{'RED_DEV'} -j QOS-TOS >/dev/null 2>&1
718         iptables -t mangle --flush  QOS-INC >/dev/null 2>&1
719         iptables -t mangle --delete-chain QOS-INC >/dev/null 2>&1
720         iptables -t mangle --flush  QOS-TOS >/dev/null 2>&1
721         iptables -t mangle --delete-chain QOS-TOS >/dev/null 2>&1
722         # remove l7-filter
723         iptables -t mangle --delete POSTROUTING -m layer7 --l7proto unset
724
725         rmmod sch_htb >/dev/null 2>&1
726
727         for i in \$(ls \$RRDLOG/class_*.rrd); do
728                 rrdtool update \$i \$(date +%s):
729         done
730
731         echo "Quality of Service was successfully cleared!"
732   ;;
733   gen|generate)
734         echo -n "Generateing the QoS-Scripts..."
735         /usr/bin/perl /var/ipfire/qos/bin/makeqosscripts.pl > /var/ipfire/qos/bin/qos.sh
736         echo ".Done!"
737         exit 0
738         ;;
739   restart)
740         ### FIRST CLEAR EVERYTHING
741         \$0 clear
742
743         ### THEN START
744         \$0 start
745         ;;
746 esac
747 ### EOF
748 END
749 ;
750
751 ############################################################################################################################
752 ############################################################################################################################