]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-pom-ng-geoip.patch
- use CUBIC by default:
[packages/kernel.git] / kernel-pom-ng-geoip.patch
1 diff -NurpP --minimal linux-2.6.21.a/include/linux/netfilter_ipv4/ipt_geoip.h linux-2.6.21.b/include/linux/netfilter_ipv4/ipt_geoip.h
2 --- linux-2.6.21.a/include/linux/netfilter_ipv4/ipt_geoip.h     1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21.b/include/linux/netfilter_ipv4/ipt_geoip.h     2007-05-30 12:08:43.000000000 +0200
4 @@ -0,0 +1,50 @@
5 +/* ipt_geoip.h header file for libipt_geoip.c and ipt_geoip.c
6 + * 
7 + * This program is free software; you can redistribute it and/or modify
8 + * it under the terms of the GNU General Public License as published by
9 + * the Free Software Foundation; either version 2 of the License, or
10 + * (at your option) any later version.
11 + *
12 + * Copyright (c) 2004, 2005, 2006 Samuel Jean & Nicolas Bouliane
13 + */
14 +#ifndef _IPT_GEOIP_H
15 +#define _IPT_GEOIP_H
16 +
17 +#define IPT_GEOIP_SRC         0x01     /* Perform check on Source IP */
18 +#define IPT_GEOIP_DST         0x02     /* Perform check on Destination IP */
19 +#define IPT_GEOIP_INV         0x04     /* Negate the condition */
20 +
21 +#define IPT_GEOIP_MAX         15       /* Maximum of countries */
22 +
23 +struct geoip_subnet {
24 +   u_int32_t begin;
25 +   u_int32_t end;
26 +};
27 +
28 +struct geoip_info {
29 +   struct geoip_subnet *subnets;
30 +   u_int32_t count;
31 +   u_int32_t ref;
32 +   u_int16_t cc;
33 +   struct geoip_info *next;
34 +   struct geoip_info *prev;
35 +};
36 +
37 +struct ipt_geoip_info {
38 +   u_int8_t flags;
39 +   u_int8_t count;
40 +   u_int16_t cc[IPT_GEOIP_MAX];
41 +
42 +   /* Used internally by the kernel */
43 +   struct geoip_info *mem[IPT_GEOIP_MAX];
44 +   u_int8_t *refcount;
45 +
46 +   /* not implemented yet:
47 +   void *fini;
48 +   */
49 +};
50 +
51 +#define COUNTRY(cc) (cc >> 8), (cc & 0x00FF)
52 +
53 +#endif
54 +
55 diff -NurpP --minimal linux-2.6.21.a/net/ipv4/netfilter/Kconfig linux-2.6.21.b/net/ipv4/netfilter/Kconfig
56 --- linux-2.6.21.a/net/ipv4/netfilter/Kconfig   2007-05-30 12:07:14.000000000 +0200
57 +++ linux-2.6.21.b/net/ipv4/netfilter/Kconfig   2007-05-30 12:08:43.000000000 +0200
58 @@ -921,5 +921,21 @@ config IP_NF_MATCH_CONNLIMIT
59           If you want to compile it as a module, say M here and read
60           Documentation/modules.txt.  If unsure, say `N'.
61  
62 +config IP_NF_MATCH_GEOIP
63 +   tristate  'geoip match support'
64 +   depends on IP_NF_IPTABLES
65 +   help
66 +          This option allows you to match a packet by its source or
67 +          destination country.  Basically, you need a country's
68 +          database containing all subnets and associated countries.
69 +
70 +          For the complete procedure and understanding, read :
71 +          http://people.netfilter.org/peejix/geoip/howto/geoip-HOWTO.html
72 +
73 +          If you want to compile it as a module, say M here and read
74 +          <file:Documentation/modules.txt>.  The module will be
75 +          called `ipt_geoip'.  If unsure, say `N'.
76 +
77 +
78  endmenu
79  
80 diff -NurpP --minimal linux-2.6.21.a/net/ipv4/netfilter/Makefile linux-2.6.21.b/net/ipv4/netfilter/Makefile
81 --- linux-2.6.21.a/net/ipv4/netfilter/Makefile  2007-05-30 12:07:14.000000000 +0200
82 +++ linux-2.6.21.b/net/ipv4/netfilter/Makefile  2007-05-30 12:08:43.000000000 +0200
83 @@ -0,0 +0,1 @@
84 +obj-$(CONFIG_IP_NF_MATCH_GEOIP) += ipt_geoip.o
85 diff -NurpP --minimal linux-2.6.21.a/net/ipv4/netfilter/ipt_geoip.c linux-2.6.21.b/net/ipv4/netfilter/ipt_geoip.c
86 --- linux-2.6.21.a/net/ipv4/netfilter/ipt_geoip.c       1970-01-01 01:00:00.000000000 +0100
87 +++ linux-2.6.21.b/net/ipv4/netfilter/ipt_geoip.c       2007-05-30 12:08:43.000000000 +0200
88 @@ -0,0 +1,289 @@
89 +/* iptables kernel module for the geoip match
90 + * 
91 + * This program is free software; you can redistribute it and/or modify
92 + * it under the terms of the GNU General Public License as published by
93 + * the Free Software Foundation; either version 2 of the License, or
94 + * (at your option) any later version.
95 + *
96 + * Copyright (c) 2004, 2005, 2006 Samuel Jean & Nicolas Bouliane
97 + */
98 +#include <linux/module.h>
99 +#include <linux/kernel.h>
100 +#include <linux/version.h>
101 +#include <linux/skbuff.h>
102 +#include <linux/netdevice.h>
103 +#include <asm/uaccess.h>
104 +#include <asm/atomic.h>
105 +#include <linux/netfilter/x_tables.h>
106 +#include <linux/netfilter_ipv4/ipt_geoip.h>
107 +#include <linux/netfilter_ipv4/ip_tables.h>
108 +
109 +MODULE_LICENSE("GPL");
110 +MODULE_AUTHOR("Samuel Jean, Nicolas Bouliane");
111 +MODULE_DESCRIPTION("iptables module for geoip match");
112 +
113 +struct geoip_info *head = NULL;
114 +static spinlock_t geoip_lock = SPIN_LOCK_UNLOCKED;
115 +
116 +static struct geoip_info *add_node(struct geoip_info *memcpy)
117 +{
118 +   struct geoip_info *p =
119 +      (struct geoip_info *)kmalloc(sizeof(struct geoip_info), GFP_KERNEL);
120 +
121 +   struct geoip_subnet *s;
122 +   
123 +   if ((p == NULL) || (copy_from_user(p, memcpy, sizeof(struct geoip_info)) != 0))
124 +      return NULL;
125 +
126 +   s = (struct geoip_subnet *)kmalloc(p->count * sizeof(struct geoip_subnet), GFP_KERNEL);
127 +   if ((s == NULL) || (copy_from_user(s, p->subnets, p->count * sizeof(struct geoip_subnet)) != 0))
128 +      return NULL;
129 +  
130 +   spin_lock_bh(&geoip_lock);
131 +
132 +   p->subnets = s;
133 +   p->ref = 1;
134 +   p->next = head;
135 +   p->prev = NULL;
136 +   if (p->next) p->next->prev = p;
137 +   head = p;
138 +
139 +   spin_unlock_bh(&geoip_lock);
140 +   return p;
141 +}
142 +
143 +static void remove_node(struct geoip_info *p)
144 + {
145 +   spin_lock_bh(&geoip_lock);
146 +   
147 +   if (p->next) { /* Am I following a node ? */
148 +      p->next->prev = p->prev;
149 +      if (p->prev) p->prev->next = p->next; /* Is there a node behind me ? */
150 +      else head = p->next; /* No? Then I was the head */
151 +   }
152 +   
153 +   else 
154 +      if (p->prev) /* Is there a node behind me ? */
155 +         p->prev->next = NULL;
156 +      else
157 +         head = NULL; /* No, we're alone */
158 +
159 +   /* So now am unlinked or the only one alive, right ?
160 +    * What are you waiting ? Free up some memory!
161 +    */
162 +
163 +   kfree(p->subnets);
164 +   kfree(p);
165 +   
166 +   spin_unlock_bh(&geoip_lock);   
167 +   return;
168 +}
169 +
170 +static struct geoip_info *find_node(u_int16_t cc)
171 +{
172 +   struct geoip_info *p = head;
173 +   spin_lock_bh(&geoip_lock);
174 +   
175 +   while (p) {
176 +      if (p->cc == cc) {
177 +         spin_unlock_bh(&geoip_lock);         
178 +         return p;
179 +      }
180 +      p = p->next;
181 +   }
182 +   spin_unlock_bh(&geoip_lock);
183 +   return NULL;
184 +}
185 +
186 +static bool match(const struct sk_buff *skb,
187 +                 const struct net_device *in,
188 +                 const struct net_device *out,
189 +                 const struct xt_match *match,
190 +                 const void *matchinfo,
191 +                 int offset,
192 +                 unsigned int protoff,
193 +                 bool *hotdrop)
194 +{
195 +   const struct ipt_geoip_info *info = matchinfo;
196 +   const struct geoip_info *node; /* This keeps the code sexy */
197 +   const struct iphdr *iph = ip_hdr(skb);
198 +   u_int32_t ip, j;
199 +   u_int8_t i;
200 +
201 +   if (info->flags & IPT_GEOIP_SRC)
202 +      ip = ntohl(iph->saddr);
203 +   else
204 +      ip = ntohl(iph->daddr);
205 +
206 +   spin_lock_bh(&geoip_lock);
207 +   for (i = 0; i < info->count; i++) {
208 +      if ((node = info->mem[i]) == NULL) {
209 +         printk(KERN_ERR "ipt_geoip: what the hell ?? '%c%c' isn't loaded into memory... skip it!\n",
210 +               COUNTRY(info->cc[i]));
211 +         
212 +         continue;
213 +      }
214 +
215 +      for (j = 0; j < node->count; j++)
216 +         if ((ip > node->subnets[j].begin) && (ip < node->subnets[j].end)) {
217 +            spin_unlock_bh(&geoip_lock);
218 +            return (info->flags & IPT_GEOIP_INV) ? 0 : 1;
219 +         }
220 +   }
221 +   
222 +   spin_unlock_bh(&geoip_lock);
223 +   return (info->flags & IPT_GEOIP_INV) ? 1 : 0;
224 +}
225 +
226 +static bool geoip_checkentry(const char *tablename,
227 +                            const void *ip,
228 +                            const struct xt_match *match,
229 +                            void *matchinfo,
230 +                            unsigned int hook_mask)
231 +{
232 +   struct ipt_geoip_info *info = matchinfo;
233 +   struct geoip_info *node;
234 +   u_int8_t i;
235 +
236 +   /* FIXME:   Call a function to free userspace allocated memory.
237 +    *          As Martin J. said; this match might eat lot of memory
238 +    *          if commited with iptables-restore --noflush
239 +   void (*gfree)(struct geoip_info *oldmem);
240 +   gfree = info->fini;
241 +   */
242 +
243 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
244 +   if (matchsize != IPT_ALIGN(sizeof(struct ipt_geoip_info))) {
245 +      printk(KERN_ERR "ipt_geoip: matchsize differ, you may have forgotten to recompile me\n");
246 +      return 0;
247 +   }
248 +#endif
249 +
250 +   /* If info->refcount isn't NULL, then
251 +    * it means that checkentry() already
252 +    * initialized this entry. Increase a
253 +    * refcount to prevent destroy() of
254 +    * this entry. */
255 +   if (info->refcount != NULL) {
256 +      atomic_inc((atomic_t *)info->refcount);
257 +      return 1;
258 +   }
259 +   
260 +   
261 +   for (i = 0; i < info->count; i++) {
262 +     
263 +      if ((node = find_node(info->cc[i])) != NULL)
264 +            atomic_inc((atomic_t *)&node->ref);   //increase the reference
265 +      else
266 +         if ((node = add_node(info->mem[i])) == NULL) {
267 +            printk(KERN_ERR
268 +                  "ipt_geoip: unable to load '%c%c' into memory\n",
269 +                  COUNTRY(info->cc[i]));
270 +            return 0;
271 +         }
272 +
273 +      /* Free userspace allocated memory for that country.
274 +       * FIXME:   It's a bit odd to call this function everytime
275 +       *          we process a country.  Would be nice to call
276 +       *          it once after all countries've been processed.
277 +       *          - SJ
278 +       * *not implemented for now*
279 +      gfree(info->mem[i]);
280 +      */
281 +
282 +      /* Overwrite the now-useless pointer info->mem[i] with
283 +       * a pointer to the node's kernelspace structure.
284 +       * This avoids searching for a node in the match() and
285 +       * destroy() functions.
286 +       */
287 +      info->mem[i] = node;
288 +   }
289 +
290 +   /* We allocate some memory and give info->refcount a pointer
291 +    * to this memory.  This prevents checkentry() from increasing a refcount
292 +    * different from the one used by destroy().
293 +    * For explanation, see http://www.mail-archive.com/netfilter-devel@lists.samba.org/msg00625.html
294 +    */
295 +   info->refcount = kmalloc(sizeof(u_int8_t), GFP_KERNEL);
296 +   if (info->refcount == NULL) {
297 +      printk(KERN_ERR "ipt_geoip: failed to allocate `refcount' memory\n");
298 +      return 0;
299 +   }
300 +   *(info->refcount) = 1;
301 +   
302 +   return 1;
303 +}
304 +
305 +static void geoip_destroy(
306 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
307 +                          const struct xt_match *match, void *matchinfo)
308 +#else
309 +                          void *matchinfo, unsigned int matchsize)
310 +#endif
311 +{
312 +   u_int8_t i;
313 +   struct geoip_info *node; /* this keeps the code sexy */
314
315 +   struct ipt_geoip_info *info = matchinfo;
316 +   /* Decrease the previously increased refcount in checkentry()
317 +    * If it's equal to 1, we know this entry is just moving
318 +    * but not removed. We simply return to avoid useless destroy()
319 +    * processing.
320 +    */
321 +   atomic_dec((atomic_t *)info->refcount);
322 +   if (*info->refcount)
323 +      return;
324 +
325 +   /* Don't leak my memory, you idiot.
326 +    * Bug found with nfsim.. the netfilter's best
327 +    * friend. --peejix */
328 +   kfree(info->refcount);
329
330 +   /* This entry has been removed from the table so
331 +    * decrease the refcount of all countries it is
332 +    * using.
333 +    */
334 +  
335 +   for (i = 0; i < info->count; i++)
336 +      if ((node = info->mem[i]) != NULL) {
337 +         atomic_dec((atomic_t *)&node->ref);
338 +
339 +         /* Free up some memory if that node isn't used
340 +          * anymore. */
341 +         if (node->ref < 1)
342 +            remove_node(node);
343 +      }
344 +      else
345 +         /* Something strange happened. There's no memory allocated for this
346 +          * country.  Please send this bug to the mailing list. */
347 +         printk(KERN_ERR
348 +               "ipt_geoip: What happened peejix ? What happened acidmen ?\n"
349 +               "ipt_geoip: please report this bug to the maintainers\n");
350 +   return;
351 +}
352 +
353 +static struct xt_match geoip_match = {
354 +   .name    = "geoip",
355 +   .family  = AF_INET,
356 +   .match      = &match,
357 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
358 +   .matchsize  = sizeof (struct ipt_geoip_info),
359 +#endif
360 +   .checkentry = &geoip_checkentry,
361 +   .destroy    = &geoip_destroy,
362 +   .me      = THIS_MODULE
363 +};
364 +
365 +static int __init init(void)
366 +{
367 +   return xt_register_match(&geoip_match);
368 +}
369 +
370 +static void __exit fini(void)
371 +{
372 +  xt_unregister_match(&geoip_match);
373 +  return;
374 +}
375 +
376 +module_init(init);
377 +module_exit(fini);
This page took 0.055944 seconds and 3 git commands to generate.