qos free interface list (probably, not tested)
[svn/Prometheus-QoS/.git] / prometheus.c
1 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2 /* Prometheus QoS - you can "steal fire" from your ISP */
3 /* "fair-per-IP" quality of service (QoS) utility */
4 /* requires Linux 2.4.x or 2.6.x with HTB support */
5 /* Copyright(C) 2005-2017 Michael Polak, Arachne Aerospace */
6 /* iptables-restore support Copyright(C) 2007-2008 ludva */
7 /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */
8 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
9
10 /* Modified by: xChaos, 20171012
11 ludva, 20080415
12
13 Prometheus QoS is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2.1 of
16 the License, or (at your option) any later version.
17
18 Prometheus QoS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with Prometheus Qos; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
27 GNU General Public License is located in file COPYING */
28
29 #include "cll1-0.6.2.h"
30 #include "ipstruct.h"
31
32 const char *version = "0.9.0-a";
33
34 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35 /* Versions: 0.9.0 is development release, 1.0 will be "stable" */
36 /* Official Trac URL: https://dev.arachne.cz/svn/prometheus */
37 /* Official SVN URL: https://dev.arachne.cz/repos/prometheus */
38 /* BTC donations account: 19rriLx8vR19wGefPaMhakqnCYNYwjLvxq */
39 /* CZK donations account: 2900242944/2010 (transparent account) */
40 /* Warning: unofficial Github mirror is not supported by author! */
41 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
42
43 const char *stats_html_signature = "<span class=\"small\">Statistics generated by Prometheus QoS version %s<br />GPL+Copyright(C)2005-2017 Michael Polak, <a target=\"_blank\" href=\"http://www.arachne.cz/\">Arachne Labs</a></span>\n";
44
45 #define STRLEN 512
46 #undef DEBUG
47
48 /* ======= All path names are defined here (for RPM patch) ======= */
49
50 const char *tc = "/sbin/tc"; /* requires tc with HTB support */
51 const char *iptables = "/sbin/iptables"; /* requires iptables utility */
52 const char *ip6tables = "/sbin/ip6tables"; /* requires iptables utility */
53 const char *iptablessave = "/sbin/iptables-save"; /* not yet required */
54 const char *iptablesrestore = "/sbin/iptables-restore"; /* requires iptables-restore */
55 const char *ip6tablessave = "/sbin/ip6tables-save"; /* not yet required */
56 const char *ip6tablesrestore = "/sbin/ip6tables-restore"; /* requires iptables-restore */
57 const char *ls = "/bin/ls"; /* this is not user configurable :-) */
58
59 char *config = "/etc/prometheus/prometheus.conf"; /* main configuration file */
60 char *hosts = "/etc/prometheus/hosts"; /* per-IP bandwidth definition file */
61 char *macrosfile = "/etc/prometheus/prometheus.macros"; /* rewrite rules for most common tariffs */
62 char *upstreamfile = "/etc/prometheus/upstream.interfaces"; /* list of interfaces to manage */
63 char *downstreamfile = "/etc/prometheus/downstream.interfaces"; /* list of interfaces to manage */
64 char *qosfreefile = "/etc/prometheus/qosfree.interfaces"; /* list of interfaces to manage */
65 char *iptablesfile = "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/
66 char *ip6tablesfile = "/var/spool/prometheus.ip6tables"; /* temporary file for ip6tables-restore*/
67 char *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */
68 char *classmap = "/var/lib/misc/prometheus.classes"; /* credit log file */
69 char *html = "/var/www/traffic.html"; /* hall of fame - html version */
70 char *preview = "/var/www/preview.html"; /* hall of fame preview - html version */
71 char *json_traffic = "/var/www/logs/traffic.json"; /* hall of fame - json version */
72 char *json_preview = "/var/www/logs/preview.json"; /* hall of fame preview - json version */
73 char *cmdlog = "/var/log/prometheuslog"; /* command log filename */
74 char *log_dir = "/var/www/logs/"; /* log directory pathname, ended with slash */
75 char *log_url = "/logs/"; /* log directory relative URI prefix (partial URL) */
76 char *html_log_dir = "/var/www/logs/html/";
77
78 char *jquery_url = "http://code.jquery.com/jquery-latest.js";
79 char *lms_url = "/lms/?m=customerinfo&amp;id=";
80 int use_jquery_popups = TRUE;
81 int row_odd_even = 0; /*<tr class="odd/even"> */
82
83 /* === Configuraration file values defaults - stored in global variables ==== */
84
85 int filter_type = 1; /*1 mark, 2 classify*/
86 char *final_chain = "DROP"; /* REJECT would be better, but it is impossible in mangle */
87 char *mark = "MARK";
88 char *mark_iptables = "MARK --set-mark ";
89 int dry_run = FALSE; /* preview - use puts() instead of system() */
90 char *iptablespreamble = "*mangle\n:PREROUTING ACCEPT [0:0]\n:POSTROUTING ACCEPT [0:0]\n:INPUT ACCEPT [0:0]\n:OUTPUT ACCEPT [0:0]\n:FORWARD ACCEPT [0:0]";
91 char *ip6preamble = "-A FORWARD -p ipv6-icmp -j ACCEPT\n-A POSTROUTING -p ipv6-icmp -j ACCEPT\n-A FORWARD -s fe80::/10 -j ACCEPT\n-A FORWARD -d ff00::/8 -j ACCEPT\n-A POSTROUTING -s fe80::/10 -j ACCEPT\n-A POSTROUTING -d ff00::/8 -j ACCEPT";
92 FILE *iptables_file = NULL;
93 FILE *ip6tables_file = NULL;
94 int enable_credit = TRUE; /* enable credit file */
95 int use_credit = FALSE; /* use credit file (if enabled)*/
96 char *title = "Hall of Fame - Greatest Suckers"; /* hall of fame title */
97 int hall_of_fame = TRUE; /* enable hall of fame */
98 char *medium = "1000Mbit"; /* 10Mbit/100Mbit ethernet */
99 //obsolete: char *lan = "eth0"; /* LAN interface */
100 //obsolete: char *lan_medium = "1000Mbit"; /* 10Mbit/100Mbit ethernet */
101 char *ip6prefix = NULL; /* Prefix for global /48 IPv6 subnet */
102 char *qos_leaf = "sfq perturb 5"; /* leaf discipline */
103 char *qos_free_zone = NULL; /* QoS free zone */
104 /* int qos_proxy = TRUE; include proxy port to QoS */
105 int found_lmsid = FALSE; /* show links to users in LMS information system */
106 int include_upload = TRUE; /* upload+download=total traffic */
107 /* char *proxy_ip = "192.168.1.1/32"; our IP with proxy port */
108 /* int proxy_port = 3128; proxy port number */
109 //obsolete: long long int line = 1024; /* WAN/ISP download in kbps */
110 //obsolete: long long int up = 1024; /* WAN/ISP upload in kbps */
111 int free_min = 256; /* minimum guaranted bandwidth for all undefined hosts */
112 int free_max = 512; /* maximum allowed bandwidth for all undefined hosts */
113 int overlimit_min = 256; /* minimum guaranted bandwidth for all undefined hosts */
114 int overlimit_max = 512; /* maximum allowed bandwidth for all undefined hosts */
115 int qos_free_delay = 0; /* seconds to sleep before applying new QoS rules */
116 int digital_divide = 2; /* controls digital divide weirdness ratio, 1...3 */
117 int max_nesting = 5; /* /include/uapi/linux/pkt_sched.h: #define TC_HTB_MAXDEPTH 8 [... - 3 parent classes] */
118 int htb_r2q = 256; /* should work for leaf values 512 kbps to 8 Mbps */
119 int burst = 8; /* HTB burst (in kbits) */
120 int burst_main = 64;
121 int burst_group = 32;
122 int magic_treshold = 8; /* reduce ceil by X*magic_treshhold kbps (hard shaping) */
123 int keywordcount = 0;
124 int class_count = 0;
125 int ip_count = 0;
126 FILE *log_file = NULL;
127 char *kwd = "via-prometheus"; /* /etc/hosts comment, eg. #qos-64-128 */
128
129 const int highest_priority = 0; /* highest HTB priority (HTB built-in value is 0) */
130 const int lowest_priority = 7; /* lowest HTB priority /include/uapi/linux/pkt_sched.h: #define TC_HTB_NUMPRIO 8 */
131 const int idxtable_treshold1 = 24; /* this is no longer configurable */
132 const int idxtable_treshold2 = 12; /* this is no longer configurable */
133 const int idxtable_bitmask1 = 3; /* this is no longer configurable */
134 const int idxtable_bitmask2 = 3; /* this is no longer configurable */
135
136 struct IP *ips = NULL, *networks = NULL, *ip, *sharedip;
137 struct Group *groups = NULL, *group;
138 struct Keyword *keyword, *defaultkeyword=NULL, *keywords = NULL;
139 struct Macro *macro, *macros = NULL;
140 struct Index *idxs = NULL, *idx, *metaindex;
141 struct Interface *interfaces = NULL, *interface;
142 struct QosFreeInterface *qosfreeinterfaces = NULL, *qosfreeinterface;
143
144 #define FREE_CLASS 3
145 #define OVERLIMIT_CLASS 4
146
147 void help(void);
148 /* implemented in help.c */
149
150 void get_traffic_statistics(const char *whichiptables, int ipv6);
151 /* implemented in parseiptables.c */
152
153 void parse_ip_log(int argc, char **argv);
154 /* implemented in parselog.c */
155
156 void parse_hosts(char *hosts);
157 /* implemented in parsehosts.c */
158
159 void write_json_traffic(char *json);
160 /* implemented in json.c */
161
162 void write_htmlandlogs(char *html, char *d, int total, int just_preview);
163 /* implemented in htmlandlogs.c */
164
165 void analyse_topology(char *traceroute);
166 /* implemented in networks.c */
167
168 char *parse_datafile_line(char *str);
169 /* implemented in utils.c */
170
171 time_t get_mtime(const char *path);
172 /* implemented in utils.c */
173
174 const char *tr_odd_even(void)
175 {
176 row_odd_even = 1 - row_odd_even;
177 if(row_odd_even)
178 {
179 return "<tr class=\"even\">\n";
180 }
181 else
182 {
183 return "<tr class=\"odd\">\n";
184 }
185 }
186
187
188 /* ====== iptables indexes are used to reduce complexity to log8(N) ===== */
189
190 char *index_id(char *ip, int bitmask);
191 /* function implemented in ipv4subnets.c */
192
193 char *subnet_id(char *ip, int bitmask);
194 /* function implemented in ipv4subnets.c */
195
196 char *index6_id(char *ip, int bitmask);
197 /* function implemented in ipv6subnets.c */
198
199 char *subnet6_id(char *ip, int bitmask);
200 /* function implemented in ipv6subnets.c */
201
202 /* ================= Let's parse configuration file here ================ */
203
204 void reject_config_and_exit(char *filename)
205 {
206 printf("Configuration file %s rejected - abnormal exit.",filename);
207 exit(-1);
208 }
209
210 void get_config(char *config_filename)
211 {
212 char *cnf="mark";
213
214 printf("Configured keywords: ");
215 parse(config_filename)
216 {
217 option("keyword",kwd);
218 if(kwd)
219 {
220 printf("%s ",kwd);
221
222 create(keyword,Keyword);
223 keyword->key = kwd;
224 keyword->asymetry_ratio = 1; /* ratio for ADSL-like upload */
225 keyword->asymetry_fixed = 0; /* fixed treshold for ADSL-like upload */
226 keyword->data_limit = 8; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */
227 keyword->data_prio = 4; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */
228 keyword->fixed_limit = 0; /* fixed data limit for setting lower HTB ceil */
229 keyword->fixed_prio = 0; /* fixed data limit for setting lower HTB prio */
230 keyword->reserve_min = 8; /* bonus for nominal HTB rate bandwidth (in kbps) */
231 keyword->reserve_max = 0; /* malus for nominal HTB ceil (in kbps) */
232 keyword->default_prio = highest_priority+1;
233 keyword->download_aggregation = keyword->upload_aggregation = 0; /* disable by default */
234 keyword->html_color = "000000";
235 keyword->ip_count = 0;
236 keyword->leaf_discipline = "";
237 keyword->allowed_avgmtu = 0;
238
239 push(keyword, keywords);
240 if(!defaultkeyword)
241 {
242 defaultkeyword = keyword;
243 }
244 keywordcount++;
245
246 kwd=NULL;
247 }
248 else
249 {
250 for_each(keyword,keywords)
251 {
252 int l=strlen(keyword->key);
253
254 if(!strncmp(keyword->key,_,l) && strlen(_)>l+2)
255 {
256 char *tmptr=_; /* <---- l+1 ----> */
257 _+=l+1; /* via-prometheus-asymetry-ratio, etc. */
258 foption("asymetry-ratio", keyword->asymetry_ratio);
259 ioption("asymetry-treshold", keyword->asymetry_fixed);
260 ioption("magic-relative-limit", keyword->data_limit);
261 ioption("magic-relative-prio", keyword->data_prio);
262 loption("magic-fixed-limit", keyword->fixed_limit);
263 loption("magic-fixed-prio", keyword->fixed_prio);
264 ioption("htb-default-prio", keyword->default_prio);
265 ioption("htb-rate-bonus", keyword->reserve_min);
266 ioption("htb-ceil-malus", keyword->reserve_max);
267 ioption("download-aggregation", keyword->download_aggregation);
268 ioption("upload-aggregation", keyword->upload_aggregation);
269 option("leaf-discipline", keyword->leaf_discipline);
270 option("html-color", keyword->html_color);
271 ioption("allowed-avgmtu" ,keyword->allowed_avgmtu);
272 _=tmptr;
273
274 if(keyword->data_limit || keyword->fixed_limit ||
275 keyword->data_prio || keyword->fixed_prio)
276 {
277 use_credit=1;
278 }
279 }
280 }
281 }
282
283 option("tc",tc);
284 option("iptables",iptables);
285 option("iptables-save",iptablessave);
286 option("iptables-restore",iptablesrestore);
287 option("ip6tables",ip6tables);
288 option("ip6tables-save",ip6tablessave);
289 option("ip6tables-restore",ip6tablesrestore);
290 option("iptables-in-filename",iptablesfile);
291 option("ip6tables-in-filename",ip6tablesfile);
292 option("hosts",hosts);
293 option("downstream-interfaces-list-filename",downstreamfile);
294 option("upstream-interfaces-list-filename",upstreamfile);
295 option("qos-free-interfaces-list-filename",qosfreefile);
296 option("macros-filename",macrosfile);
297 option("ip6-prefix",ip6prefix);
298 option("medium",medium);
299 ioption("hall-of-fame-enable",hall_of_fame);
300 ioption("digital-divide-weirdness-ratio",digital_divide);
301 option("hall-of-fame-title",title);
302 option("hall-of-fame-filename",html);
303 option("json-filename",json_traffic);
304 option("hall-of-fame-preview",preview);
305 option("json-preview",json_preview);
306 option("log-filename",cmdlog);
307 option("credit-filename",credit);
308 option("classmap-filename",classmap);
309 ioption("credit-enable",enable_credit);
310 option("log-traffic-directory",log_dir);
311 option("log-traffic-html-directory",html_log_dir);
312 option("log-traffic-url-path",log_url);
313 option("jquery-url",jquery_url);
314 option("lms-url",lms_url);
315 ioption("use-jquery-popups",use_jquery_popups);
316 option("qos-free-zone",qos_free_zone);
317 ioption("qos-free-delay",qos_free_delay);
318 /* ioption("qos-proxy-enable",qos_proxy); */
319 /* option("qos-proxy-ip",proxy_ip);*/
320 option("htb-leaf-discipline",qos_leaf);
321 /* ioption("qos-proxy-port",proxy_port); */
322 ioption("free-rate",free_min);
323 ioption("free-ceil",free_max);
324 ioption("overlimit-rate",overlimit_min);
325 ioption("overlimit-ceil",overlimit_max);
326 ioption("htb-burst",burst);
327 ioption("htb-burst-main",burst_main);
328 ioption("htb-burst-group",burst_group);
329 ioption("htb-nesting-limit",max_nesting);
330 ioption("htb-r2q",htb_r2q);
331 ioption("magic-include-upload",include_upload);
332 ioption("magic-treshold",magic_treshold);
333 option("filter-type", cnf);
334 /* not yet implemented:
335 ioption("magic-fixed-packets",fixed_packets);
336 ioption("magic-relative-packets",packet_limit);
337 */
338 }
339 fail
340 {
341 perror(config_filename);
342 puts("Warning - using built-in defaults instead ...");
343 }
344 done; /* ugly macro end */
345 printf("\n");
346
347 /* leaf discipline for keywords */
348 for_each(keyword,keywords)
349 {
350 if(!strcmpi(keyword->leaf_discipline, ""))
351 {
352 keyword->leaf_discipline = qos_leaf;
353 }
354 }
355
356 if(strcmpi(cnf, "mark"))
357 {
358 filter_type = 2;
359 mark = "CLASSIFY";
360 mark_iptables = "CLASSIFY --set-class 1:";
361 }
362 else
363 {
364 filter_type = 1;
365 mark = "MARK";
366 mark_iptables = "MARK --set-mark ";
367 }
368 }
369
370
371 /* ========== This function executes, logs OR ALSO prints command ========== */
372
373 void safe_run(char *cmd)
374 {
375 if(dry_run)
376 {
377 printf("\n=>%s\n",cmd);
378 }
379 else
380 {
381 system(cmd);
382 }
383 if(log_file)
384 {
385 fprintf(log_file,"%s\n",cmd);
386 }
387 }
388
389 void iptables_save_line(char *line, int ipv6)
390 {
391 if(ipv6)
392 {
393 fprintf(ip6tables_file,"%s\n",line);
394 }
395 else
396 {
397 fprintf(iptables_file,"%s\n",line);
398 }
399 }
400
401 #define IPv4 FALSE
402 #define IPv6 TRUE
403
404
405 void run_iptables_restore(void)
406 {
407 char *restor;
408 string(restor, STRLEN);
409
410 /*-----------------------------------------------------------------*/
411 printf("Running %s <%s ...\n", iptablesrestore, iptablesfile);
412 /*-----------------------------------------------------------------*/
413
414 iptables_save_line("COMMIT", IPv4);
415 fclose(iptables_file);
416 if(dry_run)
417 {
418 parse(iptablesfile)
419 {
420 printf("%s\n",_);
421 }
422 done; /* ugly macro end */
423 }
424
425 sprintf(restor,"%s <%s",iptablesrestore, iptablesfile);
426 safe_run(restor);
427
428 if(ip6prefix)
429 {
430 /*-----------------------------------------------------------------*/
431 printf("Running %s <%s ...\n", ip6tablesrestore, ip6tablesfile);
432 /*-----------------------------------------------------------------*/
433 iptables_save_line("COMMIT", IPv6);
434 fclose(ip6tables_file);
435 if(dry_run)
436 {
437 parse(ip6tablesfile)
438 {
439 printf("%s\n",_);
440 }
441 done; /* ugly macro end */
442 }
443 sprintf(restor,"%s <%s",ip6tablesrestore, ip6tablesfile);
444 safe_run(restor);
445 }
446 free(restor);
447 }
448
449 /**/
450
451 char *parse_datafile_line(char *str);
452 time_t get_mtime(const char *path);
453
454 /*-----------------------------------------------------------------*/
455 /* Are you looking for int main(int argc, char **argv) ? :-)) */
456 /*-----------------------------------------------------------------*/
457
458 program
459 {
460 int i=0; /* just plain old Fortran style integer :-) */
461 FILE *f=NULL; /* everything is just stream of bytes... */
462 char *str, *ptr, *d; /* LET A$=B$ :-) */
463 char *substring, *limit_pkts;
464
465 int parent = 1;
466 int just_networks = FALSE;
467 int just_flush = FALSE; /* deactivates all previous actions */
468 int nodelay = FALSE;
469 int just_preview = FALSE; /* preview - generate just stats */
470 int start_shaping = FALSE; /* apply FUP - requires classmap file */
471 int stop_shaping = FALSE; /* lift FUP - requires classmap file */
472 int reduce_ceil = 0; /* allow only rate+(ceil-rate)/2, /4, etc. */
473 int just_logs = FALSE; /* just parse logs */
474 int run = FALSE;
475 int total = 0;
476
477 char *althosts=NULL;
478
479 printf("\n\
480 Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\
481 Version %s - Copyright (C)2005-2017 Michael Polak, Arachne Labs\n\
482 iptables-restore & burst tunning & classify modification by Ludva\n\
483 Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
484
485 /*----- Boring... we have to check command line options first: ----*/
486 arguments
487 {
488 argument("-c") { nextargument(config); }
489 argument("-h") { nextargument(althosts);}
490 argument("-d") { run=TRUE; dry_run=TRUE; }
491 argument("-f") { run=TRUE; just_flush=TRUE; }
492 argument("-9") { run=TRUE; just_flush=9; }
493 argument("-p") { run=TRUE; just_preview=TRUE; }
494 argument("-q") { run=TRUE; just_preview=TRUE; stop_shaping=TRUE; }
495 argument("-2") { run=TRUE; just_preview=TRUE; reduce_ceil=2; }
496 argument("-4") { run=TRUE; just_preview=TRUE; reduce_ceil=4; }
497 argument("-s") { run=TRUE; just_preview=TRUE; start_shaping=TRUE; }
498 argument("-r") { run=TRUE; }
499 argument("-n") { run=TRUE; nodelay=TRUE; }
500 argument("-a") { run=TRUE; just_networks=TRUE; }
501 argument("-l") { just_logs=TRUE; }
502 argument("-m") { just_logs=TRUE; }
503 argument("-y") { just_logs=TRUE; }
504 argument("-?") { help(); exit(0); }
505 argument("--help") { help(); exit(0); }
506 argument("-v") { exit(0); }
507 argument("--version") { exit(0); }
508 }
509
510 if(dry_run)
511 {
512 puts("*** THIS IS JUST DRY RUN ! ***\n");
513 }
514
515 date(d); /* this is typical cll1.h macro - prints current date */
516
517 /*-----------------------------------------------------------------*/
518 printf("Parsing configuration file %s ...\n", config);
519 /*-----------------------------------------------------------------*/
520 get_config(config);
521 /*-----------------------------------------------------------------*/
522 printf("Parsing upstream interfaces list %s ...\n", upstreamfile);
523 /*-----------------------------------------------------------------*/
524 parse(upstreamfile)
525 {
526 ptr = parse_datafile_line(_);
527 if(ptr)
528 {
529 create(interface, Interface);
530 interface->name = _;
531 interface->speed = (long long)atol(ptr);
532 /* is supplied value meaningful ?*/
533 if(interface->speed <= 0)
534 {
535 printf("Illegal value of %s interface bandwidth.\n", interface->name);
536 reject_config_and_exit(upstreamfile);
537 }
538 interface->is_upstream = TRUE;
539 interface->chain = "FORWARD";
540 interface->idxprefix = "forw";
541 push(interface, interfaces);
542 printf("Upstream interface %s: medium %s capacity %ld kbps\n", interface->name, medium, interface->speed);
543 }
544 }
545 done; /* ugly macro end */
546
547 /*-----------------------------------------------------------------*/
548 printf("Parsing downstream interfaces list %s ...\n", downstreamfile);
549 /*-----------------------------------------------------------------*/
550 parse(downstreamfile)
551 {
552 ptr = parse_datafile_line(_);
553 if(ptr)
554 {
555 create(interface, Interface);
556 interface->name = _;
557 interface->speed = (long long)atol(ptr);
558 /* is supplied value meaningful ?*/
559 if(interface->speed <= 0)
560 {
561 printf("Illegal value of %s interface bandwidth.\n", interface->name);
562 reject_config_and_exit(downstreamfile);
563 }
564 interface->is_upstream = FALSE;
565 interface->chain = "POSTROUTING";
566 interface->idxprefix = "post";
567 push(interface, interfaces);
568 printf("Downstream interface %s: medium %s capacity %ld kbps\n", interface->name, medium, interface->speed);
569 }
570 }
571 done; /* ugly macro end */
572
573 if(just_logs)
574 {
575 parse_ip_log(argc,argv);
576 exit(0);
577 }
578 else if(not run)
579 {
580 help();
581 exit(0);
582 }
583
584 if(althosts)
585 {
586 hosts = althosts;
587 }
588
589 if(just_flush<9)
590 {
591 /*-----------------------------------------------------------------*/
592 puts("Parsing iptables verbose output ...");
593 /*-----------------------------------------------------------------*/
594 get_traffic_statistics(iptables, FALSE);
595 if(ip6prefix)
596 {
597 /*-----------------------------------------------------------------*/
598 puts("Parsing ip6tables verbose output ...");
599 /*-----------------------------------------------------------------*/
600 get_traffic_statistics(ip6tables, TRUE);
601 }
602 }
603
604 /*-----------------------------------------------------------------*/
605 /* cll1.h - let's allocate brand new character buffer... */
606 /*-----------------------------------------------------------------*/
607 string(str, STRLEN);
608 string(limit_pkts, STRLEN);
609
610 /*-----------------------------------------------------------------*/
611 printf("Parsing qos free interfaces file %s ...\n", qosfreefile);
612 /*-----------------------------------------------------------------*/
613 load(qosfreeinterface, qosfreeinterfaces,
614 qosfreefile, QosFreeInterface, name);
615
616 /*-----------------------------------------------------------------*/
617 printf("Parsing macro definition file %s ...\n", macrosfile);
618 /*-----------------------------------------------------------------*/
619 parse(macrosfile)
620 {
621 ptr = parse_datafile_line(_);
622 if(ptr)
623 {
624 create(macro, Macro);
625 macro->rewrite_from = _;
626 macro->rewrite_to = ptr;
627 push(macro, macros);
628 printf("%s -> %s\n", macro->rewrite_from, macro->rewrite_to);
629 }
630 }
631 done; /* ugly macro end */
632
633
634
635 /*-----------------------------------------------------------------*/
636 printf("Parsing class defintion file %s ...\n", hosts);
637 /*-----------------------------------------------------------------*/
638 parse_hosts(hosts);
639 /*
640 //this was pretty dumb idea anyway...
641 if(just_networks)
642 {
643 analyse_topology("/usr/sbin/traceroute -n -m 10 -w 2 %s.%d");
644 exit(-1);
645 }
646 */
647
648 /*-----------------------------------------------------------------*/
649 puts("Resolving shared connections ...");
650 /*-----------------------------------------------------------------*/
651 for_each(ip, ips) if(ip->sharing)
652 {
653 for_each(sharedip, ips) if(eq(sharedip->name, ip->sharing))
654 {
655 sharedip->traffic += ip->traffic;
656 sharedip->traffic_down += ip->direct;
657 sharedip->traffic_up += ip->upload;
658 ip->traffic = 0;
659 ip->mark = sharedip->mark;
660 ip->lmsid = sharedip->lmsid;
661 ip->pps_limit = sharedip->pps_limit; /* no other way to do this */
662
663 /* Ugly hack: append IPv4 addresses of sharedip to IPv6 uplinks */
664 ptr = strchr(ip->addr, '+');
665 if(ptr && ptr-ip->addr > 1 && !sharedip->v6)
666 {
667 *(--ptr) = 0;
668 concatenate(ip->addr, sharedip->addr, ptr);
669 ip->name = ip->addr = ptr;
670 ptr = strchr(ip->addr, '.');
671 while(ptr && *ptr)
672 {
673 *ptr = ':';
674 ptr = strchr(ptr, '.');
675 }
676 ip->mask += 64;
677 }
678
679 break;
680 }
681 if(not sharedip)
682 {
683 printf("Unresolved shared connection: %s %s sharing-%s\n",
684 ip->addr, ip->name, ip->sharing);
685 }
686 }
687
688 if(enable_credit && just_flush<9)
689 {
690 /*-----------------------------------------------------------------*/
691 printf("Parsing credit file %s ...\n", credit);
692 /*-----------------------------------------------------------------*/
693 parse(credit)
694 {
695 ptr = parse_datafile_line(_);
696 if(ptr)
697 {
698 if_exists(ip,ips,eq(ip->addr,_))
699 {
700 sscanf(ptr,"%Lu",&(ip->credit));
701 }
702 }
703 }
704 done; /* ugly macro end */
705 }
706
707
708 if(!just_preview)
709 {
710 /*-----------------------------------------------------------------*/
711 puts("Initializing iptables and tc classes ...");
712 /*-----------------------------------------------------------------*/
713
714 iptables_file = fopen(iptablesfile, "w");
715 if(iptables_file == NULL)
716 {
717 perror(iptablesfile);
718 exit(-1);
719 }
720 iptables_save_line(iptablespreamble, IPv4);
721
722 if(ip6prefix)
723 {
724 ip6tables_file = fopen(ip6tablesfile, "w");
725 if(ip6tables_file == NULL)
726 {
727 perror(ip6tablesfile);
728 exit(-1);
729 }
730 iptables_save_line(iptablespreamble, IPv6);
731 iptables_save_line(ip6preamble, IPv6);
732 }
733
734 run_iptables_restore();
735
736 log_file = fopen(cmdlog, "w");
737 if(log_file == NULL)
738 {
739 perror(cmdlog);
740 exit(-1);
741 }
742
743 for_each(interface, interfaces)
744 {
745 sprintf(str,"%s qdisc del dev %s root 2>/dev/null", tc, interface->name);
746 safe_run(str);
747 }
748
749 iptables_file=fopen(iptablesfile,"w");
750 iptables_save_line(iptablespreamble, IPv4);
751 if(ip6prefix)
752 {
753 ip6tables_file=fopen(ip6tablesfile,"w");
754 iptables_save_line(iptablespreamble, IPv6);
755 iptables_save_line(ip6preamble, IPv6);
756 }
757
758 if(qos_free_zone && *qos_free_zone!='0') /* this is currently supported only for IPv4 */
759 {
760 for_each(interface, interfaces)
761 {
762 sprintf(str,"-A %s -%c %s -o %s -j ACCEPT", interface->chain, (interface->is_upstream?'d':'s'), qos_free_zone, interface->name);
763 iptables_save_line(str, IPv4);
764 }
765 }
766
767 for_each(qosfreeinterface, qosfreeinterfaces)
768 {
769 sprintf(str,"-A FORWARD -i %s -j ACCEPT", qosfreeinterface->name);
770 iptables_save_line(str, IPv4);
771 iptables_save_line(str, IPv6);
772 sprintf(str,"-A POSTROUTING -o %s -j ACCEPT", qosfreeinterface->name);
773 iptables_save_line(str, IPv4);
774 iptables_save_line(str, IPv6);
775 }
776
777 if(ip_count > idxtable_treshold1 && !just_flush)
778 {
779 int idxcount=0, bitmask=32-idxtable_bitmask1;
780 char *subnet, *buf;
781 /*-----------------------------------------------------------------*/
782 printf("Detected %d addresses - indexing iptables rules to improve performance...\n",ip_count);
783 /*-----------------------------------------------------------------*/
784
785 iptables_save_line(":post_common - [0:0]", IPv4);
786 iptables_save_line(":forw_common - [0:0]", IPv4);
787 if(ip6prefix)
788 {
789 iptables_save_line(":post_common - [0:0]", IPv6);
790 iptables_save_line(":forw_common - [0:0]", IPv6);
791 }
792
793 for_each(ip,ips) if(ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0"))
794 {
795 if(ip->v6)
796 {
797 buf=index6_id(ip->addr,bitmask+32);
798 }
799 else
800 {
801 buf=index_id(ip->addr, bitmask);
802 }
803
804 if_exists(idx,idxs,eq(idx->id,buf))
805 {
806 idx->children++;
807 }
808 else
809 {
810 create(idx,Index);
811 idx->addr = ip->addr;
812 idx->id = buf;
813 idx->bitmask = bitmask+32*ip->v6;
814 idx->parent = NULL;
815 idx->children = 0;
816 idx->ipv6 = ip->v6;
817 idxcount++;
818 push(idx,idxs);
819 }
820 }
821
822 /* brutal perfomance optimalization */
823 while(idxcount > idxtable_treshold2 && bitmask > 2*idxtable_bitmask2)
824 {
825 bitmask -= idxtable_bitmask2;
826 idxcount = 0;
827
828 for_each(idx,idxs) if(idx->parent == NULL)
829 {
830 if(idx->ipv6)
831 {
832 buf = index6_id(idx->addr, bitmask+32);
833 }
834 else
835 {
836 buf = index_id(idx->addr, bitmask);
837 }
838 if_exists(metaindex,idxs,eq(metaindex->id,buf))
839 {
840 metaindex->children++;
841 }
842 else
843 {
844 create(metaindex,Index);
845 metaindex->addr = idx->addr;
846 metaindex->id = buf;
847 metaindex->bitmask = bitmask+32*idx->ipv6;
848 metaindex->parent = NULL;
849 metaindex->children = 0;
850 metaindex->ipv6 = idx->ipv6;
851 idxcount++;
852 push(metaindex,idxs);
853 }
854 idx->parent=metaindex;
855 }
856 }
857
858 /* this should slightly optimize throughput ... */
859 sort(idx,idxs,desc_order_by,children);
860 sort(idx,idxs,order_by,bitmask);
861
862 i=0;
863 for_each(idx, idxs)
864 {
865 if(idx->ipv6)
866 {
867 subnet=subnet6_id(idx->addr, idx->bitmask);
868 }
869 else
870 {
871 subnet=subnet_id(idx->addr, idx->bitmask);
872 }
873 printf("%d: %s/%d\n", ++i, subnet, idx->bitmask);
874
875 sprintf(str,":post_%s - [0:0]", idx->id);
876 iptables_save_line(str, idx->ipv6);
877
878 sprintf(str,":forw_%s - [0:0]", idx->id);
879 iptables_save_line(str, idx->ipv6);
880
881 for_each(interface, interfaces)
882 {
883 if(idx->parent)
884 {
885 string(buf, strlen(idx->parent->id)+6);
886 sprintf(buf, "%s_%s", interface->idxprefix, idx->parent->id);
887 }
888 else
889 {
890 buf = interface->chain;
891 }
892
893 sprintf(str, "-A %s -%c %s/%d -o %s -j %s_%s",
894 buf, (interface->is_upstream?'s':'d'), subnet, idx->bitmask, interface->name, interface->idxprefix, idx->id);
895 iptables_save_line(str, idx->ipv6);
896
897 sprintf(str, "-A %s -%c %s/%d -o %s -j %s_common",
898 buf, (interface->is_upstream?'s':'d'), subnet, idx->bitmask, interface->name, interface->idxprefix);
899 iptables_save_line(str, idx->ipv6);
900 }
901 }
902 printf("Total indexed iptables chains created: %d\n", i);
903
904 for_each(interface, interfaces)
905 {
906 sprintf(str,"-A %s -o %s -j %s_common", interface->chain, interface->name, interface->idxprefix);
907 iptables_save_line(str, IPv4);
908 if(ip6prefix)
909 {
910 sprintf(str,"-A %s -o %s -j %s_common", interface->chain, interface->name, interface->idxprefix);
911 iptables_save_line(str, IPv6);
912 }
913 }
914 }
915 }
916
917 if(just_flush)
918 {
919 fclose(iptables_file);
920 if(log_file)
921 {
922 fclose(log_file);
923 }
924 puts("Just flushed iptables and tc classes - now exiting ...");
925 exit(0);
926 }
927
928 if(!just_preview)
929 {
930 if(!dry_run && !nodelay && qos_free_delay)
931 {
932 printf("Flushed iptables and tc classes - now sleeping for %d seconds...\n", qos_free_delay);
933 sleep(qos_free_delay);
934 }
935
936 for_each(interface, interfaces)
937 {
938 sprintf(str, "%s qdisc add dev %s root handle 1: htb r2q %d default 1",
939 tc, interface->name, htb_r2q);
940 safe_run(str);
941
942 sprintf(str, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d",
943 tc, interface->name, medium, medium, burst_main, highest_priority);
944 safe_run(str);
945
946 sprintf(str, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d",
947 tc, interface->name, interface->speed, interface->speed, burst_main, highest_priority);
948 safe_run(str);
949 }
950 }
951
952 /*-----------------------------------------------------------------*/
953 puts("Locating heavy downloaders and generating root classes ...");
954 /*-----------------------------------------------------------------*/
955 sort(ip,ips,desc_order_by,traffic);
956
957 /*-----------------------------------------------------------------*/
958 for_each(interface, interfaces)
959 {
960 long long int rate = interface->speed;
961 long long int max = interface->speed;
962 int group_count = 0;
963 //obsolete: FILE *credit_file = NULL;
964
965 //obsolete: if(!just_preview && !dry_run && enable_credit)
966 //obsolete: {
967 //obsolete: credit_file = fopen(credit,"w");
968 //obsolete: }
969
970 for_each(group,groups)
971 {
972 if(!just_preview)
973 {
974 sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d #down desired %d",
975 tc, interface->name, parent, group->id, rate, max, burst_group, highest_priority+1, group->desired);
976 safe_run(str);
977 }
978
979 if(group_count++ < max_nesting)
980 {
981 parent = group->id;
982 }
983
984 rate -= digital_divide*group->min;
985 if(rate < group->min)
986 {
987 rate = group->min;
988 }
989
990 /*shaping of aggresive downloaders, with credit file support */
991 /* obsolete
992 if(use_credit)
993 {
994 int group_rate = group->min, priority_sequence = lowest_priority;
995
996 for_each(ip, ips) if(ip->min == group->min && ip->max > ip->min)
997 {
998 ip->realquota=ip->credit+(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20));
999 if( ip->keyword->data_limit
1000 and not ip->fixedprio
1001 and ip->traffic > ip->realquota )
1002 {
1003 if(group_rate < ip->max)
1004 {
1005 ip->max = group_rate;
1006 }
1007 group_rate+=magic_treshold;
1008 ip->prio=lowest_priority;
1009 if(ip->prio<highest_priority+2)
1010 {
1011 ip->prio=highest_priority+2;
1012 }
1013 }
1014 else
1015 {
1016 if( ip->keyword->data_prio
1017 && !ip->fixedprio
1018 && ( ip->traffic > ip->credit + (ip->min*ip->keyword->data_prio+(ip->keyword->fixed_prio<<20))) )
1019 {
1020 ip->prio=priority_sequence--;
1021 if(ip->prio<highest_priority+1)
1022 {
1023 ip->prio=highest_priority+1;
1024 }
1025 }
1026
1027 if(credit_file)
1028 {
1029 unsigned long long lcredit=0;
1030
1031 if((ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))>ip->traffic)
1032 {
1033 lcredit=(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))-ip->traffic;
1034 }
1035 fprintf(credit_file,"%s %Lu\n",ip->addr,lcredit);
1036 }
1037 }
1038 }
1039 } obsolete */
1040 }
1041 /* obsolete
1042 if(credit_file)
1043 {
1044 fclose(credit_file);
1045 } obsolete */
1046 }
1047
1048 if(just_preview)
1049 {
1050 if(start_shaping || stop_shaping || reduce_ceil)
1051 {
1052 time_t how_much_seconds = time(NULL) - get_mtime(classmap); /* sice start of daily aggregation session */
1053 printf("Reading %s (%ld seconds old) and applying Fair Use Policy and Aggregation rules... \n", classmap, how_much_seconds);
1054
1055 parse(classmap)
1056 {
1057 ptr=strchr(_,' ');
1058 if(ptr)
1059 {
1060 *ptr=0;
1061 ptr++;
1062 if_exists(ip,ips,eq(ip->addr,_))
1063 {
1064 int unshape_this_ip = 0;
1065 long avg_mbps_down = ip->traffic_down * 8 / how_much_seconds;
1066 long avg_mbps_up = ip->traffic_up * 8 / how_much_seconds;
1067 int agreg = 1, print_stats = 1;
1068
1069 if(ip->keyword->download_aggregation)
1070 {
1071 int min_mbps = (ip->min/ip->keyword->download_aggregation)>>10;
1072 if(min_mbps < 1)
1073 {
1074 min_mbps = 1;
1075 }
1076
1077 if(min_mbps <= avg_mbps_down)
1078 {
1079 unshape_this_ip = 0;
1080 agreg = (int)((float)(avg_mbps_down+1)/min_mbps+.5);
1081 ip->max /= agreg;
1082 ip->pps_limit /= agreg;
1083 printf("Download aggregation 1:%d for %s (min: %lu Mbps avg: %ld Mbps)\n", agreg, ip->name, min_mbps, avg_mbps_down);
1084 }
1085 else
1086 {
1087 unshape_this_ip = 1;
1088 }
1089 }
1090 else if(ip->keyword->upload_aggregation)
1091 {
1092 int min_mbps = (ip->min/ip->keyword->upload_aggregation)>>10;
1093 if(min_mbps < 1)
1094 {
1095 min_mbps = 1;
1096 }
1097
1098 if(min_mbps <= avg_mbps_up)
1099 {
1100 unshape_this_ip = 0;
1101 agreg = (int)((float)(avg_mbps_up+1)/min_mbps+.5);
1102 ip->max /= agreg;
1103 printf("Upload aggregation 1:%d for %s: (min: %lu Mbps avg: %ld Mbps)\n", agreg, ip->name, min_mbps, avg_mbps_up);
1104 }
1105 else
1106 {
1107 unshape_this_ip = 1;
1108 }
1109 }
1110 if(stop_shaping)
1111 {
1112 unshape_this_ip = 1;
1113 }
1114 ip->aggregated = agreg;
1115 ip->mark = atoi(ptr);
1116 if(ip->max < ip->desired || unshape_this_ip || reduce_ceil) /* apply or disable FUP limit immediately.... */
1117 {
1118 if(unshape_this_ip)
1119 {
1120 ip->max = ip->desired;
1121 if(stop_shaping) /* all limits removed, but not printed with -s (start_shaping) switch */
1122 {
1123 printf("Removing limit for %s (%s) ", ip->name, ip->addr);
1124 }
1125 else
1126 {
1127 print_stats = 0;
1128 }
1129 }
1130 else
1131 {
1132 printf("Updating %s (%s) ", ip->name, ip->addr);
1133 if(reduce_ceil)
1134 {
1135 ip->max = ip->min + (ip->desired-ip->min)/reduce_ceil;
1136 }
1137 else if(ip->max < ip->min)
1138 {
1139 ip->max = ip->min;
1140 }
1141 }
1142 for_each(interface, interfaces)
1143 {
1144 if(!interface->is_upstream)
1145 {
1146 if(print_stats)
1147 {
1148 printf("[down %s: %dk-%dk wants %d]", interface->name, ip->min, ip->max, ip->desired);
1149 }
1150 sprintf(str, "%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1151 tc, interface->name, ip->group, ip->mark, ip->min, ip->max, burst, ip->prio);
1152 safe_run(str);
1153 }
1154 else
1155 {
1156 if(print_stats)
1157 {
1158 printf("[up %s: %dk-%dk wants %dk]", interface->name, (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),
1159 (int)((ip->desired/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),
1160 (int)((ip->desired/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed));
1161 }
1162 sprintf(str,"%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1163 tc, interface->name, ip->group, ip->mark,
1164 (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),
1165 (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), burst, ip->prio);
1166 safe_run(str);
1167 }
1168 }
1169 if(print_stats)
1170 {
1171 printf("\n");
1172 }
1173 }
1174 }
1175 }
1176 }
1177 fail
1178 {
1179 perror(classmap);
1180 puts("Warning - classmap file not fund, just generating preview ...");
1181 start_shaping=FALSE;
1182 stop_shaping=FALSE;
1183 }
1184 done; /* ugly macro end */
1185 }
1186 html=preview;
1187 json_traffic=json_preview;
1188 }
1189
1190 if(!dry_run && !just_flush)
1191 {
1192 /*-----------------------------------------------------------------*/
1193 printf("Writing json traffic overview %s ... ", json_traffic);
1194 /*-----------------------------------------------------------------*/
1195 write_json_traffic(json_traffic);
1196
1197 /*-----------------------------------------------------------------*/
1198 printf("Writing statistics into HTML page %s ...\n", html);
1199 /*-----------------------------------------------------------------*/
1200 write_htmlandlogs(html, d, total, just_preview);
1201 }
1202
1203 if(just_preview)
1204 {
1205 char swchar='p';
1206 if(start_shaping)
1207 {
1208 swchar='s';
1209 }
1210 else if(reduce_ceil)
1211 {
1212 swchar='0'+reduce_ceil; /* -2, -4 */
1213 }
1214 else if(stop_shaping)
1215 {
1216 swchar='q';
1217 }
1218
1219 printf("Statistics preview generated (-%c switch) - now exiting ...\n", swchar);
1220 exit(0);
1221 }
1222
1223 i=0;
1224 #ifdef DEBUG
1225 printf("%-22s %-15s mark\n","name","ip");
1226 #endif
1227
1228 printf("Writing %s", classmap);
1229 f = fopen(classmap, "w");
1230 if(f < 0)
1231 {
1232 perror(classmap);
1233 }
1234
1235 /*-----------------------------------------------------------------*/
1236 printf(" + generating iptables and tc classes ... ");
1237 /*-----------------------------------------------------------------*/
1238
1239 for_each(ip, ips) if(ip->mark > 0)
1240 {
1241 for_each(interface, interfaces)
1242 {
1243 char *chain;
1244 if(idxs)
1245 {
1246 char *buf;
1247 duplicate(ip->addr,buf);
1248 if(ip->v6)
1249 {
1250 buf=index6_id(ip->addr,64-idxtable_bitmask1);
1251 }
1252 else
1253 {
1254 buf=index_id(ip->addr,32-idxtable_bitmask1);
1255 }
1256
1257 string(chain, 6+strlen(buf));
1258 sprintf(chain, "%s_", interface->idxprefix);
1259 strcat(chain, buf);
1260
1261 free(buf);
1262 }
1263 else
1264 {
1265 chain = interface->chain;
1266 }
1267
1268 /* packet limits - this will be optional in future */
1269 if(ip->pps_limit)
1270 {
1271 sprintf(limit_pkts, "-m limit --limit %d/s --limit-burst %d ",
1272 ip->pps_limit, ip->pps_limit);
1273 }
1274 else
1275 {
1276 *limit_pkts = 0;
1277 }
1278
1279 #ifdef DEBUG
1280 printf("%-22s %-16s %04d %d/s\n", ip->name, ip->addr, ip->mark, ip->pps_limit);
1281 #endif
1282
1283 /* ------------------------------------------------ iptables classify */
1284 sprintf(str, "-A %s -%c %s/%d -o %s -j %s%d",
1285 chain, (interface->is_upstream?'s':'d'), ip->addr, ip->mask,
1286 interface->name, mark_iptables, ip->mark);
1287 iptables_save_line(str, ip->v6);
1288
1289 sprintf(str, "-A %s -%c %s/%d -o %s %s-j ACCEPT",
1290 chain, (interface->is_upstream?'s':'d'),ip->addr, ip->mask,
1291 interface->name, limit_pkts);
1292 iptables_save_line(str, ip->v6);
1293
1294 if(limit_pkts)
1295 {
1296 /* classify overlimit packets to separate overlimit class */
1297 sprintf(str, "-A %s -%c %s/%d -o %s -j %s%d",
1298 chain, (interface->is_upstream?'s':'d'), ip->addr, ip->mask,
1299 interface->name, mark_iptables, OVERLIMIT_CLASS);
1300 iptables_save_line(str, ip->v6);
1301
1302 sprintf(str, "-A %s -%c %s/%d -o %s -j ACCEPT",
1303 chain, (interface->is_upstream?'s':'d'), ip->addr, ip->mask,
1304 interface->name);
1305 iptables_save_line(str, ip->v6);
1306 }
1307
1308 if(ip->min)
1309 {
1310 //TODO - min and max should not exceed interface->speed
1311
1312 /* -------------------------------------------------------- tc class */
1313 #ifdef DEBUG
1314 printf("[down: %dk-%dk]", ip->min, ip->max);
1315 #endif
1316
1317 sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1318 tc, interface->name, ip->group, ip->mark, ip->min, ip->max, burst, ip->prio);
1319 safe_run(str);
1320
1321 if(strcmpi(ip->keyword->leaf_discipline, "none"))
1322 {
1323 sprintf(str, "%s qdisc add dev %s parent 1:%d handle %d %s",
1324 tc, interface->name, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/
1325 safe_run(str);
1326 }
1327
1328 if(filter_type == 1)
1329 {
1330 sprintf(str, "%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",
1331 tc, interface->name, ip->mark, ip->mark);
1332 safe_run(str);
1333 }
1334 }
1335 else
1336 {
1337 #ifdef DEBUG
1338 printf("(sharing %s)\n", ip->sharing);
1339 #endif
1340 }
1341 i++;
1342 }
1343 if(ip->min && f > 0)
1344 {
1345 fprintf(f, "%s %d\n", ip->addr, ip->mark);
1346 }
1347 }
1348 if(f > 0)
1349 {
1350 puts("done.");
1351 fclose(f);
1352 }
1353
1354 for_each(interface, interfaces)
1355 {
1356 char *chain;
1357 if(idxs)
1358 {
1359 string(chain, STRLEN);
1360 sprintf(chain, "%s_common", interface->idxprefix);
1361 }
1362 else
1363 {
1364 chain = interface->chain;
1365 }
1366
1367 if(free_min)
1368 {
1369 final_chain = "ACCEPT";
1370
1371 sprintf(str, "-A %s -o %s -j %s%d",
1372 chain, interface->name, mark_iptables, FREE_CLASS);
1373 iptables_save_line(str, IPv4); /* only for IPv4 */
1374 }
1375
1376 sprintf(str,"-A %s -o %s -j %s", chain, interface->name, final_chain);
1377 iptables_save_line(str, IPv4);
1378 if(ip6prefix)
1379 {
1380 sprintf(str,"-A %s -o %s -j %s", chain, interface->name, final_chain);
1381 iptables_save_line(str, IPv6);
1382 }
1383
1384 if(free_min) /* allocate free bandwith if it is not zero... */
1385 {
1386 /*-----------------------------------------------------------------*/
1387 puts("Generating free bandwith class ...");
1388 /*-----------------------------------------------------------------*/
1389 sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1390 tc, interface->name, parent, FREE_CLASS, free_min, free_max,burst, lowest_priority);
1391 safe_run(str);
1392 /* tc SFQ */
1393 if(strcmpi(qos_leaf, "none"))
1394 {
1395 sprintf(str,"%s qdisc add dev %s parent 1:%d handle %d %s", tc, interface->name, FREE_CLASS, FREE_CLASS, qos_leaf);
1396 safe_run(str);
1397 }
1398 /* tc handle 1 fw flowid */
1399 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d", tc, interface->name, FREE_CLASS, FREE_CLASS);
1400 safe_run(str);
1401 }
1402 /*-----------------------------------------------------------------*/
1403 puts("Generating bandwith class for overlimit packets...");
1404 /*-----------------------------------------------------------------*/
1405 sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1406 tc, interface->name, parent, OVERLIMIT_CLASS, overlimit_min, overlimit_max, burst, lowest_priority);
1407 safe_run(str);
1408 }
1409 printf("Total IP count: %d\n", i);
1410 run_iptables_restore();
1411 if(log_file)
1412 {
1413 fclose(log_file);
1414 }
1415 return 0;
1416 /* that's all folks, thank you for reading it all the way up to this point ;-) */
1417 /* bad luck C<<1 is not yet finished, I promise no sprintf() next time... */
1418 }
This page took 2.069423 seconds and 4 git commands to generate.