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
5 +/* ipt_geoip.h header file for libipt_geoip.c and ipt_geoip.c
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.
12 + * Copyright (c) 2004, 2005, 2006 Samuel Jean & Nicolas Bouliane
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 */
21 +#define IPT_GEOIP_MAX 15 /* Maximum of countries */
23 +struct geoip_subnet {
29 + struct geoip_subnet *subnets;
33 + struct geoip_info *next;
34 + struct geoip_info *prev;
37 +struct ipt_geoip_info {
40 + u_int16_t cc[IPT_GEOIP_MAX];
42 + /* Used internally by the kernel */
43 + struct geoip_info *mem[IPT_GEOIP_MAX];
46 + /* not implemented yet:
51 +#define COUNTRY(cc) (cc >> 8), (cc & 0x00FF)
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'.
62 +config IP_NF_MATCH_GEOIP
63 + tristate 'geoip match support'
64 + depends on IP_NF_IPTABLES
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.
70 + For the complete procedure and understanding, read :
71 + http://people.netfilter.org/peejix/geoip/howto/geoip-HOWTO.html
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'.
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
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
89 +/* iptables kernel module for the geoip match
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.
96 + * Copyright (c) 2004, 2005, 2006 Samuel Jean & Nicolas Bouliane
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>
109 +MODULE_LICENSE("GPL");
110 +MODULE_AUTHOR("Samuel Jean, Nicolas Bouliane");
111 +MODULE_DESCRIPTION("iptables module for geoip match");
113 +struct geoip_info *head = NULL;
114 +static spinlock_t geoip_lock = SPIN_LOCK_UNLOCKED;
116 +static struct geoip_info *add_node(struct geoip_info *memcpy)
118 + struct geoip_info *p =
119 + (struct geoip_info *)kmalloc(sizeof(struct geoip_info), GFP_KERNEL);
121 + struct geoip_subnet *s;
123 + if ((p == NULL) || (copy_from_user(p, memcpy, sizeof(struct geoip_info)) != 0))
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))
130 + spin_lock_bh(&geoip_lock);
136 + if (p->next) p->next->prev = p;
139 + spin_unlock_bh(&geoip_lock);
143 +static void remove_node(struct geoip_info *p)
145 + spin_lock_bh(&geoip_lock);
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 */
154 + if (p->prev) /* Is there a node behind me ? */
155 + p->prev->next = NULL;
157 + head = NULL; /* No, we're alone */
159 + /* So now am unlinked or the only one alive, right ?
160 + * What are you waiting ? Free up some memory!
166 + spin_unlock_bh(&geoip_lock);
170 +static struct geoip_info *find_node(u_int16_t cc)
172 + struct geoip_info *p = head;
173 + spin_lock_bh(&geoip_lock);
177 + spin_unlock_bh(&geoip_lock);
182 + spin_unlock_bh(&geoip_lock);
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,
192 + unsigned int protoff,
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);
201 + if (info->flags & IPT_GEOIP_SRC)
202 + ip = ntohl(iph->saddr);
204 + ip = ntohl(iph->daddr);
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]));
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;
222 + spin_unlock_bh(&geoip_lock);
223 + return (info->flags & IPT_GEOIP_INV) ? 1 : 0;
226 +static bool geoip_checkentry(const char *tablename,
228 + const struct xt_match *match,
230 + unsigned int hook_mask)
232 + struct ipt_geoip_info *info = matchinfo;
233 + struct geoip_info *node;
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;
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");
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
255 + if (info->refcount != NULL) {
256 + atomic_inc((atomic_t *)info->refcount);
261 + for (i = 0; i < info->count; i++) {
263 + if ((node = find_node(info->cc[i])) != NULL)
264 + atomic_inc((atomic_t *)&node->ref); //increase the reference
266 + if ((node = add_node(info->mem[i])) == NULL) {
268 + "ipt_geoip: unable to load '%c%c' into memory\n",
269 + COUNTRY(info->cc[i]));
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.
278 + * *not implemented for now*
279 + gfree(info->mem[i]);
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.
287 + info->mem[i] = node;
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
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");
300 + *(info->refcount) = 1;
305 +static void geoip_destroy(
306 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
307 + const struct xt_match *match, void *matchinfo)
309 + void *matchinfo, unsigned int matchsize)
313 + struct geoip_info *node; /* this keeps the code sexy */
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()
321 + atomic_dec((atomic_t *)info->refcount);
322 + if (*info->refcount)
325 + /* Don't leak my memory, you idiot.
326 + * Bug found with nfsim.. the netfilter's best
327 + * friend. --peejix */
328 + kfree(info->refcount);
330 + /* This entry has been removed from the table so
331 + * decrease the refcount of all countries it is
335 + for (i = 0; i < info->count; i++)
336 + if ((node = info->mem[i]) != NULL) {
337 + atomic_dec((atomic_t *)&node->ref);
339 + /* Free up some memory if that node isn't used
345 + /* Something strange happened. There's no memory allocated for this
346 + * country. Please send this bug to the mailing list. */
348 + "ipt_geoip: What happened peejix ? What happened acidmen ?\n"
349 + "ipt_geoip: please report this bug to the maintainers\n");
353 +static struct xt_match geoip_match = {
357 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
358 + .matchsize = sizeof (struct ipt_geoip_info),
360 + .checkentry = &geoip_checkentry,
361 + .destroy = &geoip_destroy,
365 +static int __init init(void)
367 + return xt_register_match(&geoip_match);
370 +static void __exit fini(void)
372 + xt_unregister_match(&geoip_match);