xt_geoip: cleanups, preparations for IPv6 geoip

This commit is contained in:
Jan Engelhardt
2011-02-02 01:52:26 +01:00
parent 93a17fdde0
commit 19f241a09c
2 changed files with 16 additions and 15 deletions

View File

@@ -29,6 +29,12 @@ MODULE_AUTHOR("Samuel Jean");
MODULE_DESCRIPTION("xtables module for geoip match"); MODULE_DESCRIPTION("xtables module for geoip match");
MODULE_ALIAS("ipt_geoip"); MODULE_ALIAS("ipt_geoip");
/**
* @list: anchor point for geoip_head
* @subnets: packed ordered list of ranges
* @count: number of ranges
* @cc: country code
*/
struct geoip_country_kernel { struct geoip_country_kernel {
struct list_head list; struct list_head list;
struct geoip_subnet *subnets; struct geoip_subnet *subnets;
@@ -45,7 +51,7 @@ geoip_add_node(const struct geoip_country_user __user *umem_ptr)
{ {
struct geoip_country_user umem; struct geoip_country_user umem;
struct geoip_country_kernel *p; struct geoip_country_kernel *p;
struct geoip_subnet *s; struct geoip_subnet *subnet;
int ret; int ret;
if (copy_from_user(&umem, umem_ptr, sizeof(umem)) != 0) if (copy_from_user(&umem, umem_ptr, sizeof(umem)) != 0)
@@ -58,18 +64,19 @@ geoip_add_node(const struct geoip_country_user __user *umem_ptr)
p->count = umem.count; p->count = umem.count;
p->cc = umem.cc; p->cc = umem.cc;
s = vmalloc(p->count * sizeof(struct geoip_subnet)); subnet = vmalloc(p->count * sizeof(struct geoip_subnet));
if (s == NULL) { if (subnet == NULL) {
ret = -ENOMEM; ret = -ENOMEM;
goto free_p; goto free_p;
} }
if (copy_from_user(s, (const void __user *)(unsigned long)umem.subnets, if (copy_from_user(subnet,
(const void __user *)(unsigned long)umem.subnets,
p->count * sizeof(struct geoip_subnet)) != 0) { p->count * sizeof(struct geoip_subnet)) != 0) {
ret = -EFAULT; ret = -EFAULT;
goto free_s; goto free_s;
} }
p->subnets = s; p->subnets = subnet;
atomic_set(&p->ref, 1); atomic_set(&p->ref, 1);
INIT_LIST_HEAD(&p->list); INIT_LIST_HEAD(&p->list);
@@ -80,7 +87,7 @@ geoip_add_node(const struct geoip_country_user __user *umem_ptr)
return p; return p;
free_s: free_s:
vfree(s); vfree(subnet);
free_p: free_p:
kfree(p); kfree(p);
return ERR_PTR(ret); return ERR_PTR(ret);
@@ -149,20 +156,14 @@ xt_geoip_mt(const struct sk_buff *skb, struct xt_action_param *par)
unsigned int i; unsigned int i;
uint32_t ip; uint32_t ip;
if (info->flags & XT_GEOIP_SRC) ip = ntohl((info->flags & XT_GEOIP_SRC) ? iph->saddr : iph->daddr);
ip = ntohl(iph->saddr);
else
ip = ntohl(iph->daddr);
rcu_read_lock(); rcu_read_lock();
for (i = 0; i < info->count; i++) { for (i = 0; i < info->count; i++) {
if ((node = info->mem[i].kernel) == NULL) { if ((node = info->mem[i].kernel) == NULL) {
printk(KERN_ERR "xt_geoip: what the hell ?? '%c%c' isn't loaded into memory... skip it!\n", printk(KERN_ERR "xt_geoip: what the hell ?? '%c%c' isn't loaded into memory... skip it!\n",
COUNTRY(info->cc[i])); COUNTRY(info->cc[i]));
continue; continue;
} }
if (geoip_bsearch(node->subnets, ip, 0, node->count)) { if (geoip_bsearch(node->subnets, ip, 0, node->count)) {
rcu_read_unlock(); rcu_read_unlock();
return !(info->flags & XT_GEOIP_INV); return !(info->flags & XT_GEOIP_INV);

View File

@@ -36,7 +36,7 @@ struct geoip_country_user {
struct geoip_country_kernel; struct geoip_country_kernel;
union geoip_country_group { union geoip_country_group {
aligned_u64 user; aligned_u64 user; /* struct geoip_country_user * */
struct geoip_country_kernel *kernel; struct geoip_country_kernel *kernel;
}; };
@@ -49,6 +49,6 @@ struct xt_geoip_match_info {
union geoip_country_group mem[XT_GEOIP_MAX]; union geoip_country_group mem[XT_GEOIP_MAX];
}; };
#define COUNTRY(cc) (cc >> 8), (cc & 0x00FF) #define COUNTRY(cc) ((cc) >> 8), ((cc) & 0x00FF)
#endif /* _LINUX_NETFILTER_XT_GEOIP_H */ #endif /* _LINUX_NETFILTER_XT_GEOIP_H */