version 1.0.0.-a - significat iptables parsing speed improvement
[svn/Prometheus-QoS/.git] / prometheus.c
index 4c8b8eaa42892b4cf389c9ea9a77719cda50d5bd..1c917386c82e7d68cb1d747f7fc6733f73bd59e8 100644 (file)
@@ -2,12 +2,12 @@
 /* Prometheus QoS - you can "steal fire" from your ISP         */\r
 /* "fair-per-IP" quality of service (QoS) utility              */\r
 /* requires Linux 2.4.x or 2.6.x with HTB support              */\r
-/* Copyright(C) 2005-2017 Michael Polak, Arachne Aerospace     */\r
+/* Copyright(C) 2005-2020 Michael Polak, Arachne Aerospace     */\r
 /* iptables-restore support Copyright(C) 2007-2008 ludva       */\r
 /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf  */\r
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */\r
 \r
-/* Modified by: xChaos, 20171012\r
+/* Modified by: xChaos, 20200107\r
                  ludva, 20080415\r
  \r
    Prometheus QoS is free software; you can redistribute it and/or\r
@@ -29,7 +29,7 @@
 #include "cll1-0.6.2.h"\r
 #include "ipstruct.h"\r
 \r
-const char *version = "0.9.0-a";\r
+const char *version = "1.0.0-a";\r
 \r
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */\r
 /* Versions: 0.9.0 is development release, 1.0 will be "stable"    */\r
@@ -40,7 +40,7 @@ const char *version = "0.9.0-a";
 /* Warning: unofficial Github mirror is not supported by author!  */\r
 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */\r
 \r
-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";\r
+const char *stats_html_signature = "<span class=\"small\">Statistics generated by Prometheus QoS version %s<br />GPL+Copyright(C)2005-2020 Michael Polak, <a target=\"_blank\" href=\"http://www.arachne.cz/\">Arachne Labs</a></span>\n";\r
 \r
 #define STRLEN 512\r
 #undef DEBUG\r
@@ -61,7 +61,9 @@ char           *hosts = "/etc/prometheus/hosts"; /* per-IP bandwidth definition
 char      *macrosfile = "/etc/prometheus/prometheus.macros"; /* rewrite rules for most common tariffs */\r
 char    *upstreamfile = "/etc/prometheus/upstream.interfaces"; /* list of interfaces to manage */\r
 char  *downstreamfile = "/etc/prometheus/downstream.interfaces"; /* list of interfaces to manage */\r
+char     *qosfreefile = "/etc/prometheus/qosfree.interfaces"; /* list of interfaces to manage */\r
 char    *iptablesfile = "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/\r
+char    *iptablesdump = "/var/spool/prometheus.iptables-dump"; /* temporary file for iptables -L -v -x -n -t mangle */\r
 char   *ip6tablesfile = "/var/spool/prometheus.ip6tables"; /* temporary file for ip6tables-restore*/\r
 char          *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */\r
 char        *classmap = "/var/lib/misc/prometheus.classes"; /* credit log file */\r
@@ -100,6 +102,8 @@ char           *medium = "1000Mbit"; /* 10Mbit/100Mbit ethernet */
 char        *ip6prefix = NULL; /* Prefix for global /48 IPv6 subnet */\r
 char         *qos_leaf = "sfq perturb 5"; /* leaf discipline */\r
 char    *qos_free_zone = NULL; /* QoS free zone */\r
+char *qos_free_dst_ipset = NULL; /* QoS free zone - dst match ipset name, must be prepared outside prometheus */\r
+char *qos_free_src_ipset = NULL; /* QoS free zone - src match ipset name, must be prepared outside prometheus */\r
 /* int          qos_proxy = TRUE; include proxy port to QoS */\r
 int        found_lmsid = FALSE; /* show links to users in LMS information system */\r
 int     include_upload = TRUE; /* upload+download=total traffic */\r
@@ -138,6 +142,7 @@ struct Keyword *keyword, *defaultkeyword=NULL, *keywords = NULL;
 struct Macro *macro, *macros = NULL;\r
 struct Index *idxs = NULL, *idx, *metaindex;\r
 struct Interface *interfaces = NULL, *interface;\r
+struct QosFreeInterface *qosfreeinterfaces = NULL, *qosfreeinterface;\r
 \r
 #define FREE_CLASS      3\r
 #define OVERLIMIT_CLASS 4\r
@@ -286,10 +291,12 @@ void get_config(char *config_filename)
   option("ip6tables-save",ip6tablessave);\r
   option("ip6tables-restore",ip6tablesrestore);\r
   option("iptables-in-filename",iptablesfile);\r
+  option("iptables-dump-filename",iptablesdump);\r
   option("ip6tables-in-filename",ip6tablesfile);\r
   option("hosts",hosts);\r
   option("downstream-interfaces-list-filename",downstreamfile);\r
   option("upstream-interfaces-list-filename",upstreamfile);\r
+  option("qos-free-interfaces-list-filename",qosfreefile);\r
   option("macros-filename",macrosfile);\r
   option("ip6-prefix",ip6prefix);\r
   option("medium",medium);\r
@@ -311,6 +318,8 @@ void get_config(char *config_filename)
   option("lms-url",lms_url);\r
   ioption("use-jquery-popups",use_jquery_popups);\r
   option("qos-free-zone",qos_free_zone);\r
+  option("qos-free-dst-ipset",qos_free_dst_ipset);\r
+  option("qos-free-src-ipset",qos_free_src_ipset);\r
   ioption("qos-free-delay",qos_free_delay);\r
 /*  ioption("qos-proxy-enable",qos_proxy); */\r
 /*  option("qos-proxy-ip",proxy_ip);*/\r
@@ -395,6 +404,10 @@ void iptables_save_line(char *line, int ipv6)
  }\r
 }\r
 \r
+#define IPv4 FALSE\r
+#define IPv6 TRUE\r
+\r
+\r
 void run_iptables_restore(void)\r
 {\r
  char *restor;\r
@@ -404,7 +417,7 @@ void run_iptables_restore(void)
  printf("Running %s <%s ...\n", iptablesrestore, iptablesfile);\r
  /*-----------------------------------------------------------------*/\r
 \r
- iptables_save_line("COMMIT", FALSE);\r
+ iptables_save_line("COMMIT", IPv4);\r
  fclose(iptables_file);\r
  if(dry_run) \r
  {\r
@@ -423,7 +436,7 @@ void run_iptables_restore(void)
   /*-----------------------------------------------------------------*/\r
   printf("Running %s <%s ...\n", ip6tablesrestore, ip6tablesfile);\r
   /*-----------------------------------------------------------------*/\r
-  iptables_save_line("COMMIT", TRUE);\r
+  iptables_save_line("COMMIT", IPv6);\r
   fclose(ip6tables_file);\r
   if(dry_run) \r
   {\r
@@ -562,7 +575,6 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   }\r
  }\r
  done; /* ugly macro end */\r
-\r
  \r
  if(just_logs)\r
  {\r
@@ -601,6 +613,12 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  string(str, STRLEN); \r
  string(limit_pkts, STRLEN);\r
 \r
+ /*-----------------------------------------------------------------*/\r
+ printf("Parsing qos free interfaces file %s ...\n", qosfreefile);\r
+ /*-----------------------------------------------------------------*/\r
+ load(qosfreeinterface, qosfreeinterfaces,\r
+      qosfreefile, QosFreeInterface, name);\r
+\r
  /*-----------------------------------------------------------------*/\r
  printf("Parsing macro definition file %s ...\n", macrosfile);\r
  /*-----------------------------------------------------------------*/\r
@@ -624,14 +642,34 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  printf("Parsing class defintion file %s ...\n", hosts);\r
  /*-----------------------------------------------------------------*/\r
  parse_hosts(hosts);\r
-/*\r
- //this was pretty dumb idea anyway...\r
- if(just_networks)\r
+\r
+#ifdef MONITORINGTRHU_CTU\r
+//special hack only to generate certain required CSV statistics for www.ctu.cz (regulation body)\r
+//not required for everyday use, requires special syntax sugar in hosts file, see parsehosts.c\r
+ for_each(technology, technologies)\r
  {\r
-  analyse_topology("/usr/sbin/traceroute -n -m 10 -w 2 %s.%d");\r
-  exit(-1); \r
- }\r
-*/\r
+  char *filename;\r
+  FILE *f;\r
+  string(filename, strlen(log_dir) + strlen(technology->filename) + 5);\r
+  strcpy(filename, html_log_dir);\r
+  strcat(filename, technology->filename); \r
+  strcat(filename, ".csv");\r
+  /*-----------------------------------------------------------------*/\r
+  printf("Writing report file %s ...\n", filename);\r
+  /*-----------------------------------------------------------------*/\r
+  f = fopen(filename, "w");\r
+  if(f)\r
+  {\r
+   for_each(ip, ips) if(eq(technology->filename, ip->technology_str))\r
+   {\r
+    fprintf(f,"%d,%s,%d\n", ip->lmsid, ip->ruian_id_str, ip->max);\r
+   }\r
+   fclose(f);\r
+  }\r
+  else\r
+   perror(filename); \r
+ } \r
+#endif\r
 \r
  /*-----------------------------------------------------------------*/\r
  puts("Resolving shared connections ...");\r
@@ -705,7 +743,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
     perror(iptablesfile);\r
     exit(-1);\r
   }\r
-  iptables_save_line(iptablespreamble, FALSE);\r
+  iptables_save_line(iptablespreamble, IPv4);\r
 \r
   if(ip6prefix)\r
   {\r
@@ -715,8 +753,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
      perror(ip6tablesfile);\r
      exit(-1);\r
    }\r
-   iptables_save_line(iptablespreamble, TRUE);\r
-   iptables_save_line(ip6preamble, TRUE);\r
+   iptables_save_line(iptablespreamble, IPv6);\r
+   iptables_save_line(ip6preamble, IPv6);\r
   }\r
 \r
   run_iptables_restore();\r
@@ -735,23 +773,51 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   }\r
   \r
   iptables_file=fopen(iptablesfile,"w");\r
-  iptables_save_line(iptablespreamble, FALSE);\r
+  iptables_save_line(iptablespreamble, IPv4);\r
   if(ip6prefix)\r
   {\r
    ip6tables_file=fopen(ip6tablesfile,"w");\r
-   iptables_save_line(iptablespreamble, TRUE);\r
-   iptables_save_line(ip6preamble, TRUE);\r
+   iptables_save_line(iptablespreamble, IPv6);\r
+   iptables_save_line(ip6preamble, IPv6);\r
+  }\r
+\r
+  if(qos_free_zone && *qos_free_zone != '0') /* this is currently supported only for IPv4 */\r
+  {\r
+   for_each(interface, interfaces)\r
+   {\r
+    sprintf(str,"-A %s -%c %s -o %s -j ACCEPT", interface->chain, (interface->is_upstream?'d':'s'), qos_free_zone, interface->name);\r
+    iptables_save_line(str, IPv4);\r
+   }\r
   }\r
 \r
-  if(qos_free_zone && *qos_free_zone!='0') /* this is currently supported only for IPv4 */\r
+  if(qos_free_dst_ipset && *qos_free_dst_ipset != '0') /* this is currently supported only for IPv4 */\r
   {\r
    for_each(interface, interfaces)\r
    {\r
-    sprintf(str,"-A %s -s %s -o %s -j ACCEPT", interface->chain, qos_free_zone, interface->name);\r
-    iptables_save_line(str, FALSE);\r
+    sprintf(str,"-A %s -m set --match-set %s %s -o %s -j ACCEPT", interface->chain, qos_free_dst_ipset, (interface->is_upstream?"dst":"src"), interface->name);\r
+    iptables_save_line(str, IPv4);\r
+   }\r
+  }\r
+\r
+  if(qos_free_src_ipset && *qos_free_src_ipset != '0') /* this is currently supported only for IPv4 */\r
+  {\r
+   for_each(interface, interfaces)\r
+   {\r
+    sprintf(str,"-A %s -m set --match-set %s %s -o %s -j ACCEPT", interface->chain, qos_free_src_ipset, (interface->is_upstream?"src":"dst"), interface->name);\r
+    iptables_save_line(str, IPv4);\r
    }\r
   }\r
   \r
+  for_each(qosfreeinterface, qosfreeinterfaces)\r
+  {\r
+    sprintf(str,"-A FORWARD -i %s -j ACCEPT", qosfreeinterface->name);\r
+    iptables_save_line(str, IPv4);  \r
+    iptables_save_line(str, IPv6);\r
+    sprintf(str,"-A POSTROUTING -o %s -j ACCEPT", qosfreeinterface->name);\r
+    iptables_save_line(str, IPv4);\r
+    iptables_save_line(str, IPv6);\r
+  }\r
+  \r
   if(ip_count > idxtable_treshold1 && !just_flush)\r
   {\r
    int idxcount=0, bitmask=32-idxtable_bitmask1;\r
@@ -760,12 +826,12 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
    printf("Detected %d addresses - indexing iptables rules to improve performance...\n",ip_count);\r
    /*-----------------------------------------------------------------*/\r
 \r
-   iptables_save_line(":post_common - [0:0]", FALSE);\r
-   iptables_save_line(":forw_common - [0:0]", FALSE);\r
+   iptables_save_line(":post_common - [0:0]", IPv4);\r
+   iptables_save_line(":forw_common - [0:0]", IPv4);\r
    if(ip6prefix)\r
    {\r
-    iptables_save_line(":post_common - [0:0]", TRUE);\r
-    iptables_save_line(":forw_common - [0:0]", TRUE);\r
+    iptables_save_line(":post_common - [0:0]", IPv6);\r
+    iptables_save_line(":forw_common - [0:0]", IPv6);\r
    }\r
 \r
    for_each(ip,ips) if(ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0")) \r
@@ -868,10 +934,12 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
       buf = interface->chain;\r
      }\r
 \r
-     sprintf(str,"-A %s -d %s/%d -o %s -j %s_%s", buf, subnet, idx->bitmask, interface->name, interface->idxprefix, idx->id);\r
+     sprintf(str, "-A %s -%c %s/%d -o %s -j %s_%s", \r
+                  buf, (interface->is_upstream?'s':'d'), subnet, idx->bitmask, interface->name, interface->idxprefix, idx->id);\r
      iptables_save_line(str, idx->ipv6);\r
 \r
-     sprintf(str,"-A %s -d %s/%d -o %s -j %s_common", buf, subnet, idx->bitmask, interface->name, interface->idxprefix);\r
+     sprintf(str, "-A %s -%c %s/%d -o %s -j %s_common",\r
+                  buf, (interface->is_upstream?'s':'d'), subnet, idx->bitmask, interface->name, interface->idxprefix);\r
      iptables_save_line(str, idx->ipv6);\r
     }\r
    }\r
@@ -880,11 +948,11 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
    for_each(interface, interfaces)\r
    {\r
     sprintf(str,"-A %s -o %s -j %s_common", interface->chain, interface->name, interface->idxprefix);\r
-    iptables_save_line(str, FALSE);\r
+    iptables_save_line(str, IPv4);\r
     if(ip6prefix)\r
     {\r
      sprintf(str,"-A %s -o %s -j %s_common", interface->chain, interface->name, interface->idxprefix);\r
-     iptables_save_line(str, TRUE);\r
+     iptables_save_line(str, IPv6);\r
     }\r
    }\r
   }\r
@@ -1174,6 +1242,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   printf("Writing statistics into HTML page %s ...\n", html);\r
   /*-----------------------------------------------------------------*/\r
   write_htmlandlogs(html, d, total, just_preview);\r
+  printf("\n");\r
  }\r
 \r
  if(just_preview)\r
@@ -1257,24 +1326,29 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  #endif\r
 \r
    /* ------------------------------------------------ iptables classify */\r
-   sprintf(str, "-A %s -d %s/%d -o %s -j %s%d",\r
-                chain, ip->addr, ip->mask,\r
+   sprintf(str, "-A %s -%c %s/%d -o %s -j %s%d",\r
+                chain, (interface->is_upstream?'s':'d'), ip->addr, ip->mask,\r
                 interface->name, mark_iptables, ip->mark);\r
    iptables_save_line(str, ip->v6);\r
 \r
-   sprintf(str, "-A %s -d %s/%d -o %s %s-j ACCEPT",\r
-                chain, ip->addr, ip->mask, interface->name, limit_pkts);\r
+   sprintf(str, "-A %s -%c %s/%d -o %s %s-j ACCEPT",\r
+                chain, (interface->is_upstream?'s':'d'),ip->addr, ip->mask,\r
+                interface->name, limit_pkts);\r
    iptables_save_line(str, ip->v6);\r
 \r
-   /* classify overlimit packets to separate overlimit class */\r
-   sprintf(str, "-A %s -d %s/%d -o %s -j %s%d",\r
-                chain, ip->addr, ip->mask,\r
-                interface->name, mark_iptables, OVERLIMIT_CLASS);\r
-   iptables_save_line(str, ip->v6);\r
-\r
-   sprintf(str, "-A %s -d %s/%d -o %s -j ACCEPT",\r
-                chain, ip->addr, ip->mask, interface->name);\r
-   iptables_save_line(str, ip->v6);\r
+   if(*limit_pkts) /* non-empty string?*/\r
+   {\r
+    /* classify overlimit packets to separate overlimit class */\r
+    sprintf(str, "-A %s -%c %s/%d -o %s -j %s%d",\r
+                 chain, (interface->is_upstream?'s':'d'), ip->addr, ip->mask,\r
+                 interface->name, mark_iptables, OVERLIMIT_CLASS);\r
+    iptables_save_line(str, ip->v6);\r
+\r
+    sprintf(str, "-A %s -%c %s/%d -o %s -j ACCEPT",\r
+                 chain, (interface->is_upstream?'s':'d'), ip->addr, ip->mask,\r
+                 interface->name);\r
+    iptables_save_line(str, ip->v6);\r
+   }\r
 \r
    if(ip->min)\r
    {\r
@@ -1341,15 +1415,15 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
 \r
    sprintf(str, "-A %s -o %s -j %s%d",\r
                 chain, interface->name, mark_iptables, FREE_CLASS);\r
-   iptables_save_line(str, FALSE); /* only for IPv4 */\r
+   iptables_save_line(str, IPv4); /* only for IPv4 */\r
   }\r
 \r
   sprintf(str,"-A %s -o %s -j %s", chain, interface->name, final_chain);\r
-  iptables_save_line(str, FALSE);\r
+  iptables_save_line(str, IPv4);\r
   if(ip6prefix)\r
   {\r
    sprintf(str,"-A %s -o %s -j %s", chain, interface->name, final_chain);\r
-   iptables_save_line(str, TRUE);\r
+   iptables_save_line(str, IPv6);\r
   }\r
 \r
   if(free_min) /* allocate free bandwith if it is not zero... */ \r
This page took 0.225955 seconds and 4 git commands to generate.