Skip to contentSkip to conversation

quota per ip. right patch

PATCH New Diego Woitasen (Lanux) 1 participant 1 message Jun 30, 2003 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 quota per ip. right patch

This patch add support for quotas per source ip. Please I wan 't to hear your opinion about this.

Sorry, this is the correct patch against patch-o-matic and iptables-userspace.

Thanks.

extensions/libipt_quota.c +112 -54
diff -urN iptables-1.2.8-old/extensions/libipt_quota.c iptables-1.2.8/extensions/libipt_quota.c
@@ -2,7 +2,11 @@
  * Shared library add-on to iptables to add quota support
  *
  * Sam Johnston <[email protected]>
+ *
+ * 2003/04/02 Diego Woitasen ([email protected]) add quota per dst ip 
+ *
  */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <getopt.h>
@@ -12,103 +16,157 @@
 #include <linux/netfilter_ipv4/ip_tables.h>
 
 static struct option opts[] = {
-        {"quota", 1, 0, '1'},
-        {0}
+  {"quota", 1, 0, IPT_QUOTA},
+  {"ipdstquota", 1, 0, IPT_IPDSTQUOTA},
+  {"count-headers", 0, 0, IPT_QUOTA_FLAG_COUNT_FULL},
+  {0}
 };
 
 /* print usage */
 static void
-help(void)
+help (void)
 {
-        printf("quota options:\n"
-               " --quota quota			quota (bytes)\n" "\n");
+  printf ("quota options:\n\n"
+	  " --quota [!] quota			quota (bytes)\n"
+	  " --ipdstquota [!] quota		quota per ip (bytes)\n"
+	  "\n"
+	  " --count-headers		count headers as well as data\n");
 }
 
 /* initialise match */
 static void
-init(struct ipt_entry_match *m, unsigned int *nfcache)
+init (struct ipt_entry_match *m, unsigned int *nfcache)
 {
-        /* no can cache */
-        *nfcache |= NFC_UNKNOWN;
+
+  struct ipt_quota_info *info = (struct ipt_quota_info *) m->data;
+
+  info->flags = 0;
+  info->quota = 0;
+  info->ipquota_hash = NULL;
+
+  /* no can cache */
+  *nfcache |= NFC_UNKNOWN;
+
 }
 
 /* print matchinfo */
 static void
-print(const struct ipt_ip *ip, const struct ipt_entry_match *match, int numeric)
+print (const struct ipt_ip *ip, const struct ipt_entry_match *match,
+       int numeric)
 {
-        struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
-        printf("quota: %llu bytes", (unsigned long long) q->quota);
+  struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
+
+  if (IPT_QUOTA & q->flags)
+    printf ("quota");
+  else if (IPT_IPDSTQUOTA & q->flags)
+    printf ("ipdstquota");
+  if (IPT_QUOTA_FLAG_NEGATE & q->flags)
+    printf (" ! ");
+  printf ("%llu bytes", (unsigned long long) q->quota);
+  if (IPT_QUOTA_FLAG_COUNT_FULL & q->flags)
+    printf (" (incl. headers)");
+
 }
 
 /* save matchinfo */
 static void
-save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
+save (const struct ipt_ip *ip, const struct ipt_entry_match *match)
 {
-        struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
-        printf("--quota %llu ", (unsigned long long) q->quota);
+  struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
+
+  if (IPT_QUOTA & q->flags)
+    printf ("--quota");
+  else if (IPT_IPDSTQUOTA & q->flags)
+    printf ("--ipdstquota");
+  if (IPT_QUOTA_FLAG_NEGATE & q->flags)
+    printf (" ! ");
+  printf (" %llu", (unsigned long long) q->quota);
+  if (IPT_QUOTA_FLAG_COUNT_FULL & q->flags)
+    printf (" --count-headers");
+
 }
 
 /* parse quota option */
 static int
-parse_quota(const char *s, u_int64_t * quota)
+parse_quota (const char *s, struct ipt_quota_info *quota, int c)
 {
-        *quota = strtoull(s, (char **) NULL, 10);
+
+  if (s && (*s == '-'))
+    exit_error (PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
+
+  quota->quota = strtoull (s, (char **) NULL, 10);
 
 #ifdef DEBUG_IPT_QUOTA
-        printf("Quota: %llu\n", *quota);
+  printf ("Quota: %llu\n", quota->quota);
 #endif
 
-        if (*quota == -1)
-                exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
-        else
-                return 1;
+  if (quota->quota == -1)
+    exit_error (PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
+  else
+    return 1;
+
 }
 
 /* parse all options, returning true if we found any for us */
 static int
-parse(int c, char **argv, int invert, unsigned int *flags,
-      const struct ipt_entry *entry,
-      unsigned int *nfcache, struct ipt_entry_match **match)
-{
-        struct ipt_quota_info *info = (struct ipt_quota_info *) (*match)->data;
-
-        switch (c) {
-        case '1':
-                if (check_inverse(optarg, &invert, NULL, 0))
-                        exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
-                if (!parse_quota(optarg, &info->quota))
-                        exit_error(PARAMETER_PROBLEM,
-                                   "bad quota: '%s'", optarg);
-                break;
-
-        default:
-                return 0;
-        }
-        return 1;
+parse (int c, char **argv, int invert, unsigned int *flags,
+       const struct ipt_entry *entry,
+       unsigned int *nfcache, struct ipt_entry_match **match)
+{
+  struct ipt_quota_info *info = (struct ipt_quota_info *) (*match)->data;
+
+  switch (c)
+    {
+    case IPT_QUOTA_FLAG_COUNT_FULL:
+      if (check_inverse (optarg, &invert, NULL, 0))
+	exit_error (PARAMETER_PROBLEM, "count-headers: unexpected '!'");
+      info->flags |= IPT_QUOTA_FLAG_COUNT_FULL;
+      break;
+    case IPT_QUOTA:
+    case IPT_IPDSTQUOTA:
+      if ((info->flags & IPT_QUOTA) && (info->flags & IPT_IPDSTQUOTA))
+	exit_error (PARAMETER_PROBLEM,
+		    "syntax error: you can't use --quota and --ipdstquota");
+      check_inverse (optarg, &invert, &optind, 0);
+      if (invert)
+	info->flags |= IPT_QUOTA_FLAG_NEGATE;
+      parse_quota (argv[optind - 1], info, c);
+      info->flags |= (u_int16_t) c;
+      *flags = 1;
+      break;
+    default:
+      exit_error (PARAMETER_PROBLEM, "unknown option: try --help");
+      return 0;
+    }
+  return 1;
 }
 
 /* no final check */
 static void
-final_check(unsigned int flags)
+final_check (unsigned int flags)
 {
+
+  if (flags != 1)
+    exit_error (PARAMETER_PROBLEM, "quota: missing arguments");
+
 }
 
 struct iptables_match quota = { NULL,
-        "quota",
-        IPTABLES_VERSION,
-        IPT_ALIGN(sizeof (struct ipt_quota_info)),
-        IPT_ALIGN(sizeof (struct ipt_quota_info)),
-        &help,
-        &init,
-        &parse,
-        &final_check,
-        &print,
-        &save,
-        opts
+  "quota",
+  NETFILTER_VERSION,
+  IPT_ALIGN (sizeof (struct ipt_quota_info)),
+  IPT_ALIGN (sizeof (struct ipt_quota_info)),
+  &help,
+  &init,
+  &parse,
+  &final_check,
+  &print,
+  &save,
+  opts
 };
 
 void
-_init(void)
+_init (void)
 {
-        register_match(&quota);
+  register_match (&quota);
 }
base/quota.patch +345 -36

443 lines not on this page · raw message

base/quota.patch.configure.help +1 -1
diff -urN patch-o-matic-20030107-old/base/quota.patch.configure.help patch-o-matic-20030107/base/quota.patch.configure.help
@@ -1,7 +1,7 @@
 CONFIG_IP_NF_MATCH_LIMIT
 quota match support
 CONFIG_IP_NF_MATCH_QUOTA
-  This match implements network quotas.
+  This match implements network quotas and quotas per destination ip.
 
   If you want to compile it as a module, say M here and read
   Documentation/modules.txt.  If unsure, say `N'.
base/quota.patch.help +5 -0
diff -urN patch-o-matic-20030107-old/base/quota.patch.help patch-o-matic-20030107/base/quota.patch.help
@@ -3,8 +3,13 @@
 
 This option adds CONFIG_IP_NF_MATCH_QUOTA, which implements network
 quotas by decrementing a byte counter with each packet.
+Also implements quota per destination ip address. 
+(Diego Woitasen <[email protected]>)
 
 Supported options are:
 --quota <bytes>
   The quota in bytes.
+  
+--ipdstquota <bytes>
+  The quota per destination ip address in bytes