finally move buildroot-ng to trunk
[openwrt.git] / target / linux / generic-2.4 / patches / 612-netfilter_quota.patch
1 diff -ruN linux-2.4.30-old/Documentation/Configure.help linux-2.4.30-new/Documentation/Configure.help
2 --- linux-2.4.30-old/Documentation/Configure.help       2005-11-13 22:30:42.000000000 +0100
3 +++ linux-2.4.30-new/Documentation/Configure.help       2005-11-13 22:31:17.000000000 +0100
4 @@ -2888,6 +2888,13 @@
5    If you want to compile it as a module, say M here and read
6    <file:Documentation/modules.txt>.  If unsure, say `N'.
7  
8 +quota match support
9 +CONFIG_IP_NF_MATCH_QUOTA
10 +  This match implements network quotas.
11 +
12 +  If you want to compile it as a module, say M here and read
13 +  Documentation/modules.txt.  If unsure, say `N'.
14 +
15  skb->pkt_type packet match support
16  CONFIG_IP_NF_MATCH_PKTTYPE
17    This patch allows you to match packet in accrodance
18 diff -ruN linux-2.4.30-old/include/linux/netfilter_ipv4/ipt_quota.h linux-2.4.30-new/include/linux/netfilter_ipv4/ipt_quota.h
19 --- linux-2.4.30-old/include/linux/netfilter_ipv4/ipt_quota.h   1970-01-01 01:00:00.000000000 +0100
20 +++ linux-2.4.30-new/include/linux/netfilter_ipv4/ipt_quota.h   2005-11-13 22:31:17.000000000 +0100
21 @@ -0,0 +1,12 @@
22 +#ifndef _IPT_QUOTA_H
23 +#define _IPT_QUOTA_H
24 +
25 +/* print debug info in both kernel/netfilter module & iptable library */
26 +//#define DEBUG_IPT_QUOTA
27 +
28 +struct ipt_quota_info {
29 +        u_int64_t quota;
30 +       struct ipt_quota_info *master;
31 +};
32 +
33 +#endif /*_IPT_QUOTA_H*/
34 diff -ruN linux-2.4.30-old/net/ipv4/netfilter/Config.in linux-2.4.30-new/net/ipv4/netfilter/Config.in
35 --- linux-2.4.30-old/net/ipv4/netfilter/Config.in       2005-11-13 22:30:42.000000000 +0100
36 +++ linux-2.4.30-new/net/ipv4/netfilter/Config.in       2005-11-13 22:31:17.000000000 +0100
37 @@ -24,6 +24,7 @@
38  if [ "$CONFIG_IP_NF_IPTABLES" != "n" ]; then
39  # The simple matches.
40    dep_tristate '  limit match support' CONFIG_IP_NF_MATCH_LIMIT $CONFIG_IP_NF_IPTABLES
41 +  dep_tristate '  quota match support' CONFIG_IP_NF_MATCH_QUOTA $CONFIG_IP_NF_IPTABLES
42  
43    dep_tristate '  IP set support' CONFIG_IP_NF_SET $CONFIG_IP_NF_IPTABLES
44    if [ "$CONFIG_IP_NF_SET" != "n" ]; then
45 diff -ruN linux-2.4.30-old/net/ipv4/netfilter/Makefile linux-2.4.30-new/net/ipv4/netfilter/Makefile
46 --- linux-2.4.30-old/net/ipv4/netfilter/Makefile        2005-11-13 22:30:42.000000000 +0100
47 +++ linux-2.4.30-new/net/ipv4/netfilter/Makefile        2005-11-13 22:31:17.000000000 +0100
48 @@ -74,6 +74,7 @@
49  # matches
50  obj-$(CONFIG_IP_NF_MATCH_HELPER) += ipt_helper.o
51  obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
52 +obj-$(CONFIG_IP_NF_MATCH_QUOTA) += ipt_quota.o
53  obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
54  obj-$(CONFIG_IP_NF_MATCH_SET) += ipt_set.o
55  obj-$(CONFIG_IP_NF_TARGET_SET) += ipt_SET.o
56 diff -ruN linux-2.4.30-old/net/ipv4/netfilter/ipt_quota.c linux-2.4.30-new/net/ipv4/netfilter/ipt_quota.c
57 --- linux-2.4.30-old/net/ipv4/netfilter/ipt_quota.c     1970-01-01 01:00:00.000000000 +0100
58 +++ linux-2.4.30-new/net/ipv4/netfilter/ipt_quota.c     2005-11-13 22:31:17.000000000 +0100
59 @@ -0,0 +1,88 @@
60 +/* 
61 + * netfilter module to enforce network quotas
62 + *
63 + * Sam Johnston <samj@samj.net>
64 + *
65 + * 30/01/05: Fixed on SMP --Pablo Neira <pablo@eurodev.net>
66 + */
67 +#include <linux/module.h>
68 +#include <linux/skbuff.h>
69 +#include <linux/spinlock.h>
70 +#include <linux/interrupt.h>
71 +
72 +#include <linux/netfilter_ipv4/ip_tables.h>
73 +#include <linux/netfilter_ipv4/ipt_quota.h>
74 +
75 +MODULE_LICENSE("GPL");
76 +
77 +static spinlock_t quota_lock = SPIN_LOCK_UNLOCKED;
78 +
79 +static int
80 +match(const struct sk_buff *skb,
81 +      const struct net_device *in,
82 +      const struct net_device *out,
83 +      const void *matchinfo,
84 +      int offset, const void *hdr, u_int16_t datalen, int *hotdrop)
85 +{
86 +       struct ipt_quota_info *q =
87 +               ((struct ipt_quota_info *) matchinfo)->master;
88 +
89 +        spin_lock_bh(&quota_lock);
90 +
91 +        if (q->quota >= datalen) {
92 +                /* we can afford this one */
93 +                q->quota -= datalen;
94 +                spin_unlock_bh(&quota_lock);
95 +
96 +#ifdef DEBUG_IPT_QUOTA
97 +                printk("IPT Quota OK: %llu datlen %d \n", q->quota, datalen);
98 +#endif
99 +                return 1;
100 +        }
101 +
102 +        /* so we do not allow even small packets from now on */
103 +        q->quota = 0;
104 +
105 +#ifdef DEBUG_IPT_QUOTA
106 +        printk("IPT Quota Failed: %llu datlen %d \n", q->quota, datalen);
107 +#endif
108 +
109 +        spin_unlock_bh(&quota_lock);
110 +        return 0;
111 +}
112 +
113 +static int
114 +checkentry(const char *tablename,
115 +           const struct ipt_ip *ip,
116 +           void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
117 +{
118 +        /* TODO: spinlocks? sanity checks? */
119 +       struct ipt_quota_info *q = (struct ipt_quota_info *) matchinfo;
120 +
121 +        if (matchsize != IPT_ALIGN(sizeof (struct ipt_quota_info)))
122 +                return 0;
123 +       
124 +       /* For SMP, we only want to use one set of counters. */
125 +       q->master = q;
126 +
127 +        return 1;
128 +}
129 +
130 +static struct ipt_match quota_match
131 +    = { {NULL, NULL}, "quota", &match, &checkentry, NULL, THIS_MODULE };
132 +
133 +static int __init
134 +init(void)
135 +{
136 +        return ipt_register_match(&quota_match);
137 +}
138 +
139 +static void __exit
140 +fini(void)
141 +{
142 +        ipt_unregister_match(&quota_match);
143 +}
144 +
145 +module_init(init);
146 +module_exit(fini);
147 +