forked from chilek/lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lms-traffic-htbiptlimits
executable file
·351 lines (305 loc) · 11.5 KB
/
lms-traffic-htbiptlimits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/perl -Tw
#
# LMS version 1.11-git
#
# Copyright (C) 2001-2013 LMS Developers
#
# Please, see the doc/AUTHORS for more information about authors!
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
# $Id$
use strict;
use DBI;
use Config::IniFiles;
use Getopt::Long;
use vars qw($configfile $quiet $help $version);
$ENV{'PATH'}='/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin';
sub u32todotquad($)
{
my $p = shift @_;
return sprintf "%d.%d.%d.%d", ($p>>24)&0xff,($p>>16)&0xff, ($p>>8)&0xff,$p&0xff;
}
sub mask2prefix($)
{
my $mask = shift @_;
my @tmp = split('\.',$mask,4);
my $q = sprintf("%b%b%b%b",$tmp[0],$tmp[1],$tmp[2],$tmp[3]);
$q =~ s/0*$//;
if ($q =~ /0/) {
return -1;
}
my $len = length($q) ;
return $len;
}
my $_version = '1.11-git';
my %options = (
"--config-file|C=s" => \$configfile,
"--quiet|q" => \$quiet,
"--help|h" => \$help,
"--version|v" => \$version
);
Getopt::Long::config("no_ignore_case");
GetOptions(%options);
if($help)
{
print STDERR <<EOF;
lms-traffic-htbiptlimits, version $_version
(C) 2001-2013 LMS Developers
-C, --config-file=/etc/lms/lms.ini alternate config file (default: /etc/lms/lms.ini);
-h, --help print this help and exit;
-v, --version print version info and exit;
-q, --quiet suppress any output, except errors;
EOF
exit 0;
}
if($version)
{
print STDERR <<EOF;
lms-traffic-htbiptlimits, version $_version
(C) 2001-2013 LMS Developers
EOF
exit 0;
}
if(!$configfile)
{
$configfile = "/etc/lms/lms.ini";
}
if(!$quiet)
{
print STDOUT "lms-traffic-htbiptlimits, version $_version\n";
print STDOUT "(C) 2001-2013 LMS Developers\n";
print STDOUT "Using file $configfile as config.\n";
}
if(! -r $configfile)
{
print STDERR "Fatal error: Unable to read configuration file $configfile, exiting.\n";
exit 1;
}
my $ini = new Config::IniFiles -file => $configfile;
print @Config::IniFiles::errors;
my $networks_list = $ini->val('traffic-htbiptlimits', 'networks') || '';
my $excluded_networks_list = $ini->val('traffic-htbiptlimits', 'excluded_networks') || '';
my $outfile = $ini->val('traffic-htbiptlimits','outfile') || '/etc/rc.d/rc.limits';
my $iptables_binary = $ini->val('traffic-htbiptlimits','iptables_binary') || '/usr/sbin/iptables';
my $tc_binary = $ini->val('traffic-htbiptlimits','tc_binary') || '/sbin/tc';
my $wan_interfaces = $ini->val('traffic-htbiptlimits','wan_interfaces') || '';
my $local_ports = $ini->val('traffic-htbiptlimits','local_ports') || '';
my $local_ips = $ini->val('traffic-htbiptlimits','local_ips') || '';
my $local_dests = $ini->val('traffic-htbiptlimits','local_dests') || '';
my $cuid = $ini->val('traffic-htbiptlimits', 'script_owneruid') || '0';
my $cgid = $ini->val('traffic-htbiptlimits', 'script_ownergid') || '0';
my $cperm = $ini->val('traffic-htbiptlimits', 'script_permission') || '700';
my $dbtype = $ini->val('database', 'type') || 'mysql';
my $dbhost = $ini->val('database', 'host') || 'localhost';
my $dbuser = $ini->val('database', 'user') || 'root';
my $dbpasswd = $ini->val('database', 'password') || '';
my $dbname = $ini->val('database', 'database') || 'lms';
my $dbase;
my $utsfmt;
my %iftable;
my $totalspd;
if(! $wan_interfaces)
{
print STDERR "Fatal error: 'wan_interfaces' not defined, exiting.\n";
exit 1;
}
if($dbtype =~ /mysql/)
{
$dbase = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 });
$utsfmt = "UNIX_TIMESTAMP()";
}
elsif($dbtype eq "postgres")
{
$dbase = DBI->connect("DBI:Pg:dbname=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 });
$utsfmt = "EXTRACT(EPOCH FROM CURRENT_TIMESTAMP(0))";
}
else
{
print STDERR "Fatal error: unsupported database type: $dbtype, exiting.\n";
exit 1;
}
# No networks_list in lms.ini, we fill networks_list variable with all networks in database
if(!$networks_list)
{
my $dbq = $dbase->prepare("SELECT name FROM networks ORDER BY interface,name");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref())
{
$networks_list = "$networks_list $row->{'name'}";
}
}
my @networks = split ' ',$networks_list;
# Check for excluded_networks_list in lms.ini, if it is empty, than get excluded networks from LMSUI.
# Case 1: excluded_networks_list in lms.ini
if($excluded_networks_list)
{
my @excluded_networks = split ' ', $excluded_networks_list;
foreach my $excluded_network (@excluded_networks)
{
@networks = grep(!/^$excluded_network$/, @networks);
}
}
# Case 2: excluded_networks_list from LMSUI
else
{
my $dbq = $dbase->prepare("SELECT name FROM networks WHERE disabled=1 ORDER BY interface,name");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref())
{
$excluded_networks_list = "$excluded_networks_list $row->{'name'}";
}
my @excluded_networks = split ' ', $excluded_networks_list;
foreach my $excluded_network (@excluded_networks)
{
@networks = grep(!/^$excluded_network$/, @networks);
}
}
open(OUTFILE, ">$outfile") or die("Fatal error: Unable to write $outfile, exiting.\n");
my @local_ports_list = split(' ',$local_ports);
my @wan_interfaces_list = split(' ',$wan_interfaces);
my @local_ips_list = split(' ',$local_ips);
my @local_dests_list = split(' ',$local_dests);
print OUTFILE "#!/bin/bash\n";
foreach my $wan_interface (@wan_interfaces_list)
{
my ($if,$spd) = split(':', $wan_interface);
print OUTFILE "$iptables_binary -t mangle -D FORWARD -i $if -j LIMITS >/dev/null 2>&1\n";
print OUTFILE "$iptables_binary -t mangle -D FORWARD -o $if -j LIMITS >/dev/null 2>&1\n";
$iftable{$if} = $spd;
$totalspd = 0 if not defined $totalspd;
$totalspd += $spd;
}
foreach my $port (@local_ports_list)
{
print OUTFILE "$iptables_binary -t mangle -D INPUT -p tcp --dport $port -j LIMITS >/dev/null 2>&1\n";
print OUTFILE "$iptables_binary -t mangle -D OUTPUT -p tcp --sport $port -j LIMITS >/dev/null 2>&1\n";
}
foreach my $local_ip (@local_ips_list)
{
print OUTFILE "$iptables_binary -t mangle -D FORWARD -s $local_ip -j LIMITS >/dev/null 2>&1\n";
print OUTFILE "$iptables_binary -t mangle -D FORWRRD -d $local_ip -j LIMITS >/dev/null 2>&1\n";
}
foreach my $local_dest (@local_dests_list)
{
my ($ip,$port) = split(':',$local_dest);
print OUTFILE "$iptables_binary -t mangle -D FORWARD -s $ip -p tcp --sport $port -j LIMITS >/dev/null 2>&1\n";
print OUTFILE "$iptables_binary -t mangle -D FORWARD -d $ip -p tcp --dport $port -j LIMITS >/dev/null 2>&1\n";
}
print OUTFILE "$iptables_binary -t mangle -F LIMITS >/dev/null 2>&1
$iptables_binary -t mangle -X LIMITS >/dev/null 2>&1
$iptables_binary -t mangle -N LIMITS
";
foreach my $wan_interface (@wan_interfaces_list)
{
my ($if,$spd) = split(':', $wan_interface);
print OUTFILE "$iptables_binary -t mangle -I FORWARD -i $if -j LIMITS\n";
print OUTFILE "$iptables_binary -t mangle -I FORWARD -o $if -j LIMITS\n";
}
foreach my $port (@local_ports_list)
{
print OUTFILE "$iptables_binary -t mangle -I INPUT -p tcp --dport $port -j LIMITS\n";
print OUTFILE "$iptables_binary -t mangle -I OUTPUT -p tcp --sport $port -j LIMITS\n";
}
foreach my $local_ip (@local_ips_list)
{
print OUTFILE "$iptables_binary -t mangle -A FORWARD -s $local_ip -j LIMITS >/dev/null 2>&1\n";
print OUTFILE "$iptables_binary -t mangle -A FORWARD -d $local_ip -j LIMITS >/dev/null 2>&1\n";
}
foreach my $local_dest (@local_dests_list)
{
my ($ip,$port) = split(':',$local_dest);
print OUTFILE "$iptables_binary -t mangle -A FORWARD -s $ip -p tcp --sport $port -j LIMITS >/dev/null 2>&1\n";
print OUTFILE "$iptables_binary -t mangle -A FORWARD -d $ip -p tcp --dport $port -j LIMITS >/dev/null 2>&1\n";
}
my %nodetable;
foreach my $key (@networks)
{
my $dbq = $dbase->prepare("SELECT address, mask, interface FROM networks WHERE name = UPPER('$key')");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref())
{
my $netstart = $row->{'address'};
my $prefix = mask2prefix($row->{'mask'});
my $netsize = 2**(32 - $prefix);
my $netend = $netstart + $netsize - 1;
my $sdbq = $dbase->prepare("SELECT ipaddr, ownerid FROM vnodes WHERE ipaddr >= $netstart AND ipaddr <= $netend");
$sdbq->execute();
while (my $srow = $sdbq->fetchrow_hashref())
{
my $ipaddr = u32todotquad($srow->{'ipaddr'});
$nodetable{$srow->{'ownerid'}} = "" if not defined $nodetable{$srow->{'ownerid'}};
$nodetable{$srow->{'ownerid'}} = $nodetable{$srow->{'ownerid'}} . " " . $ipaddr . ":" . $row->{'interface'};
$iftable{$row->{'interface'}} = $totalspd if not defined $iftable{$row->{'interface'}};
}
$sdbq->finish();
}
$dbq->finish();
}
foreach my $key (keys %iftable)
{
print OUTFILE "$tc_binary qdisc del root dev $key\n";
print OUTFILE "$tc_binary qdisc add dev $key root handle 1: htb\n";
print OUTFILE "$tc_binary class add dev $key parent 1: classid 1:9998 htb rate 10mbit burst 1mbit\n";
print OUTFILE "$tc_binary class add dev $key parent 1:9998 classid 1:9999 htb rate ".$iftable{$key}."kbit burst ".($iftable{$key} / 10)."kbit\n";
}
my $iptid = 0;
my %htbid;
my $dbq = $dbase->prepare("SELECT customerid, SUM(uprate) AS uprate, SUM(downrate) AS downrate, SUM(upceil) AS upceil, SUM(downceil) AS downceil FROM assignments, customers, tariffs WHERE customers.id = customerid AND deleted = 0 AND tariffid = tariffs.id AND (datefrom <= $utsfmt OR datefrom = 0) AND (dateto > $utsfmt OR dateto = 0) GROUP BY customerid");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref())
{
if($nodetable{$row->{'customerid'}} && $row->{'downrate'})
{
my %ifaces;
$iptid ++;
foreach my $ippair (split(' ',$nodetable{$row->{'customerid'}}))
{
my ($ipaddr, $if) = split(':',$ippair);
$ifaces{$if} = 1;
print OUTFILE "$iptables_binary -t mangle -A LIMITS -d $ipaddr -j MARK --set-mark $iptid\n";
}
foreach my $key (keys %ifaces)
{
$htbid{$key} = 0 if not defined $htbid{$key};
$htbid{$key} ++;
print OUTFILE "$tc_binary class add dev $key parent 1:9999 classid 1:".$htbid{$key}." htb rate ".$row->{'downrate'}."kbit ceil ".$row->{'downceil'}."kbit burst ".($row->{'downrate'} / 8)."kbit\n";
print OUTFILE "$tc_binary filter add dev $key protocol ip parent 1: handle $iptid fw classid 1:".$htbid{$key}."\n";
}
}
if($nodetable{$row->{'customerid'}} && $row->{'uprate'})
{
$iptid ++;
foreach my $ippair (split(' ',$nodetable{$row->{'customerid'}}))
{
my ($ipaddr, $if) = split(':',$ippair);
print OUTFILE "$iptables_binary -t mangle -A LIMITS -s $ipaddr -j MARK --set-mark $iptid\n";
}
foreach my $wan_interface (@wan_interfaces_list)
{
my ($if,$spd) = split(':', $wan_interface);
$htbid{$if} = 0 if not defined $htbid{$if};
$htbid{$if} ++;
print OUTFILE "$tc_binary class add dev $if parent 1:9999 classid 1:".$htbid{$if}." htb rate ".$row->{'uprate'}."kbit ceil ".$row->{'upceil'}."kbit burst ".($row->{'uprate'} / 8). "kbit\n";
print OUTFILE "$tc_binary filter add dev $if protocol ip parent 1: handle $iptid fw classid 1:".$htbid{$if}."\n";
}
}
}
close(OUTFILE);
chown $cuid, $cgid, $outfile or print "Warning! Unable to set owner of $outfile to $cuid.$cgid.\n";
chmod oct($cperm), $outfile or print "Warning! Unable to set permission $cperm to $outfile.\n";
system("$outfile 1>/dev/null 2>&1");
$dbq->finish();
$dbase->disconnect();