Patch with additional options for quota match
I have made up a small patch for the quota match by Sam Johnston to add a couple of options I find useful - negated matching and the option to count total packet size (data + headers) instead of just data size. Examples:
# Old behavior, ACCEPT packets UNTIL 5000000 quota has been consumed iptables -A INPUT -m quota --quota 5000000 -j ACCEPT
# Negated match, DROP packets AFTER 5000000 quota has been consumed # Does not count packet headers when calculating the quota. # Lets packets pass as long as the remaining quota > 0 iptables -A INPUT -m quota --quota ! 5000000 -j DROP
# Negated match, DROP packets AFTER 5000000 quota has been consumed # Packet headers are counted in the quota.
# Lets packets pass as long as the remaining quota > 0 iptables -A INPUT -m quota --quota ! 5000000 --count-headers -j DROP
The patches should apply cleanly to the iptables-1.2.7a and patch-o-matic-20030107 distributions.
Brad Fisher
Info Link Inc.
extensions/libipt_quota.c +43 -7
@@ -2,6 +2,7 @@* Shared library add-on to iptables to add quota support * * Sam Johnston <[email protected]> +* (Modified to support negated matches and add option to count headers by Brad Fisher ([email protected])) */ #include <stdio.h> #include <stdlib.h>
@@ -13,6 +14,7 @@static struct option opts[] = { {"quota" ,1 ,0 ,'1' }, + {"count-headers" ,0 ,0 ,'2' }, {0 } };
@@ -21,7 +23,8 @@help (void) {printf ("quota options:\n" -" --quota quota quota (bytes)\n" "\n" ); +" --quota [!] quota quota (bytes)\n" +" --count-headers count headers as well as data\n" "\n" ); }/* initialise match */
@@ -37,7 +40,18 @@ 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); +char *negate ="" ; +char *headers ="" ; + +if (q->flags &IPT_QUOTA_FLAG_NEGATE ) { + negate ="!" ; + } + +if (q->flags &IPT_QUOTA_FLAG_COUNT_FULL ) { + headers =" (incl. headers)" ; + } + +printf ("quota: %s%llu bytes%s" , negate, (unsigned long long ) q->quota, headers); }/* save matchinfo */
@@ -45,14 +59,28 @@ 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); + +printf ("--quota " ); +if (q->flags &IPT_QUOTA_FLAG_NEGATE ) { +printf ("! " ); + } +printf ("%llu " , (unsigned long long ) q->quota); + +if (q->flags &IPT_QUOTA_FLAG_COUNT_FULL ) { +printf ("--count-headers " ); + } }/* parse quota option */ static int parse_quota (const char *s,u_int64_t * quota) { - *quota =strtoull (s, (char **)NULL ,10 ); +/* Don't allow negative quotas... Not perfect since it doesn't skip whitespace */ +if (s && (*s =='-' )) { + *quota =-1 ; + }else { + *quota =strtoull (s, (char **)NULL ,10 ); + }#ifdef DEBUG_IPT_QUOTA printf ("Quota: %llu\n" , *quota);
@@ -74,12 +102,20 @@switch (c) {case '1' : -if (check_inverse (optarg, &invert,NULL ,0 )) -exit_error (PARAMETER_PROBLEM ,"quota: unexpected '!'" ); -if (!parse_quota (optarg, &info->quota)) +check_inverse (optarg, &invert, &optind,0 ); +if (invert) { + info->flags |=IPT_QUOTA_FLAG_NEGATE ; + } +if (!parse_quota (argv[optind-1 ], &info->quota))exit_error (PARAMETER_PROBLEM ,"bad quota: '%s'" , optarg);break ; + +case '2' : +if (check_inverse (optarg, &invert,NULL ,0 )) +exit_error (PARAMETER_PROBLEM ,"count-headers: unexpected '!'" ); + info->flags |=IPT_QUOTA_FLAG_COUNT_FULL ; +break ;default :return 0 ;
base/quota.patch +39 -13
91 lines not on this page · raw message
base/quota.patch.help +6 -3
@@ -5,6 +5,9 @@ quotas by decrementing a byte counter with each packet. Supported options are: ---quota <bytes> - The quota in bytes. - +--quota [!] <bytes> + The quota in bytes. The normal form matches all packets up to the + quota, while the negated form only matches after quota bytes have + been examined by the rule. +--count-headers + Counts headers as well as data (default only counts data)
Quoted 3 lines · Brad Fisher
I have made up a small patch for the quota match by Sam Johnston to add a couple of options I find useful - negated matching and the option to count total packet size (data + headers) instead of just data size.
I don't want to include this without the orignal author's permission. Please contact Sam Johnston about your changes and ask him if he agrees with those changes.
Thanks.
Signature · 5 lines
- Harald Welte <[email protected]> http://www.netfilter.org/ ============================================================================ "Fragmentation is like classful addressing -- an interesting early architectural error that shows how much experimentation was going on while IP was being designed." -- Paul Vixie
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Harald Welte wrote:
| On Wed, Apr 16, 2003 at 04:24:37PM -0500, Brad Fisher wrote: | |>I have made up a small patch for the quota match by Sam Johnston to add |>a couple of options I find useful - negated matching and the option to |>count total packet size (data + headers) instead of just data size. | | | I don't want to include this without the orignal author's permission. | Please contact Sam Johnston about your changes and ask him if he agrees | with those changes.
Harald,
Thankyou for confirming changes with me. I haven't seen this patch, so can someone please forward it on. The changes requested sound reasonable in principal - for most applications counting the headers is probably what is intended although this is a change in functionality. I'm not sure about the applications for negated matching but no doubt someone somewhere will find it useful.
Sam
- --
Sam Johnston, Director
Australian Online Solutions
1300 132 809
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQE+rZiVj4XJViLpTR0RAmW3AJ48YYYbgoNHI0bc18nRd4rszsmtTQCfcoeN 4mg8d/NVh50OSF9A2fMPTHY=
=obWm
-----END PGP SIGNATURE-----
Sam, I sent you an email earlier following Harald's request. I did neglect to attach the patches to that email, sorry. The patches should be attached to this email. Let me know if you need anything else.
The negated match has been very useful for my purposes. I have been using it to track quotas for individual hosts by ip as follows:
INPUT:
target prot opt in out source destination
i_1.0.0.1 all -- * eth1 1.0.0.1 0.0.0.0/0FORWARD:
target prot opt in out source destination
i_1.0.0.1 all -- * eth1 1.0.0.1 0.0.0.0/0
o_1.0.0.1 all -- * eth1 0.0.0.0/0 1.0.0.1OUTUT:
target prot opt in out source destination
o_1.0.0.1 all -- * eth1 0.0.0.0/0 1.0.0.1i_1.0.0.1:
target prot opt in out source destination
q_1.0.0.1 all -- * * 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0o_1.0.0.1:
target prot opt in out source destination
q_1.0.0.1 all -- * * 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0q_1.0.0.1:
target prot opt in out source destination
DROP all -- * * 0.0.0.0/00.0.0.0/0 quota: !8000000 bytes (incl. headers)
If the quota rule in the q_1.0.0.1 chain matches (ie. the quota is exhausted), the packet is dropped. By using the three chains (i_*, o_*, q_*), all traffic to/from the ip is accounted for and the quotas are enforced for all incoming and outgoing traffic with the same rule.
Brad Fisher
Info Link Inc.
Quoted 32 lines · Sam Johnston
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1Harald Welte wrote:
| On Wed, Apr 16, 2003 at 04:24:37PM -0500, Brad Fisher wrote: | |>I have made up a small patch for the quota match by Sam Johnston to add |>a couple of options I find useful - negated matching and the option to |>count total packet size (data + headers) instead of just data size. | | | I don't want to include this without the orignal author's permission. | Please contact Sam Johnston about your changes and ask him if he agrees | with those changes.Harald,
Thankyou for confirming changes with me. I haven't seen this patch, so can someone please forward it on. The changes requested sound reasonable in principal - for most applications counting the headers is probably what is intended although this is a change in functionality. I'm not sure about the applications for negated matching but no doubt someone somewhere will find it useful.
Sam
- --
Sam Johnston, Director
Australian Online Solutions
1300 132 809
Quoted 8 lines
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.orgiD8DBQE+rZiVj4XJViLpTR0RAmW3AJ48YYYbgoNHI0bc18nRd4rszsmtTQCfcoeN 4mg8d/NVh50OSF9A2fMPTHY=
=obWm
-----END PGP SIGNATURE-----
extensions/libipt_quota.c +43 -7
@@ -2,6 +2,7 @@* Shared library add-on to iptables to add quota support * * Sam Johnston <[email protected]> +* (Modified to support negated matches and add option to count headers by Brad Fisher ([email protected])) */ #include <stdio.h> #include <stdlib.h>
@@ -13,6 +14,7 @@static struct option opts[] = { {"quota" ,1 ,0 ,'1' }, + {"count-headers" ,0 ,0 ,'2' }, {0 } };
@@ -21,7 +23,8 @@help (void) {printf ("quota options:\n" -" --quota quota quota (bytes)\n" "\n" ); +" --quota [!] quota quota (bytes)\n" +" --count-headers count headers as well as data\n" "\n" ); }/* initialise match */
@@ -37,7 +40,18 @@ 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); +char *negate ="" ; +char *headers ="" ; + +if (q->flags &IPT_QUOTA_FLAG_NEGATE ) { + negate ="!" ; + } + +if (q->flags &IPT_QUOTA_FLAG_COUNT_FULL ) { + headers =" (incl. headers)" ; + } + +printf ("quota: %s%llu bytes%s" , negate, (unsigned long long ) q->quota, headers); }/* save matchinfo */
@@ -45,14 +59,28 @@ 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); + +printf ("--quota " ); +if (q->flags &IPT_QUOTA_FLAG_NEGATE ) { +printf ("! " ); + } +printf ("%llu " , (unsigned long long ) q->quota); + +if (q->flags &IPT_QUOTA_FLAG_COUNT_FULL ) { +printf ("--count-headers " ); + } }/* parse quota option */ static int parse_quota (const char *s,u_int64_t * quota) { - *quota =strtoull (s, (char **)NULL ,10 ); +/* Don't allow negative quotas... Not perfect since it doesn't skip whitespace */ +if (s && (*s =='-' )) { + *quota =-1 ; + }else { + *quota =strtoull (s, (char **)NULL ,10 ); + }#ifdef DEBUG_IPT_QUOTA printf ("Quota: %llu\n" , *quota);
@@ -74,12 +102,20 @@switch (c) {case '1' : -if (check_inverse (optarg, &invert,NULL ,0 )) -exit_error (PARAMETER_PROBLEM ,"quota: unexpected '!'" ); -if (!parse_quota (optarg, &info->quota)) +check_inverse (optarg, &invert, &optind,0 ); +if (invert) { + info->flags |=IPT_QUOTA_FLAG_NEGATE ; + } +if (!parse_quota (argv[optind-1 ], &info->quota))exit_error (PARAMETER_PROBLEM ,"bad quota: '%s'" , optarg);break ; + +case '2' : +if (check_inverse (optarg, &invert,NULL ,0 )) +exit_error (PARAMETER_PROBLEM ,"count-headers: unexpected '!'" ); + info->flags |=IPT_QUOTA_FLAG_COUNT_FULL ; +break ;default :return 0 ;
base/quota.patch +39 -13
91 lines not on this page · raw message
base/quota.patch.help +6 -3
@@ -5,6 +5,9 @@ quotas by decrementing a byte counter with each packet. Supported options are: ---quota <bytes> - The quota in bytes. - +--quota [!] <bytes> + The quota in bytes. The normal form matches all packets up to the + quota, while the negated form only matches after quota bytes have + been examined by the rule. +--count-headers + Counts headers as well as data (default only counts data)