Commit 6b7d31fc authored by Harald Welte's avatar Harald Welte Committed by Arnaldo Carvalho de Melo

[NETFILTER]: Add "revision" support to arp_tables and ip6_tables

Like ip_tables already has it for some time, this adds support for
having multiple revisions for each match/target.  We steal one byte from
the name in order to accomodate a 8 bit version number.
Signed-off-by: default avatarHarald Welte <laforge@netfilter.org>
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@mandriva.com>
parent 6ede2463
...@@ -68,7 +68,8 @@ struct arpt_entry_target ...@@ -68,7 +68,8 @@ struct arpt_entry_target
u_int16_t target_size; u_int16_t target_size;
/* Used by userspace */ /* Used by userspace */
char name[ARPT_FUNCTION_MAXNAMELEN]; char name[ARPT_FUNCTION_MAXNAMELEN-1];
u_int8_t revision;
} user; } user;
struct { struct {
u_int16_t target_size; u_int16_t target_size;
...@@ -148,7 +149,9 @@ struct arpt_entry ...@@ -148,7 +149,9 @@ struct arpt_entry
#define ARPT_SO_GET_INFO (ARPT_BASE_CTL) #define ARPT_SO_GET_INFO (ARPT_BASE_CTL)
#define ARPT_SO_GET_ENTRIES (ARPT_BASE_CTL + 1) #define ARPT_SO_GET_ENTRIES (ARPT_BASE_CTL + 1)
#define ARPT_SO_GET_MAX ARPT_SO_GET_ENTRIES /* #define ARPT_SO_GET_REVISION_MATCH (ARPT_BASE_CTL + 2)*/
#define ARPT_SO_GET_REVISION_TARGET (ARPT_BASE_CTL + 3)
#define ARPT_SO_GET_MAX ARPT_SO_GET_REVISION_TARGET
/* CONTINUE verdict for targets */ /* CONTINUE verdict for targets */
#define ARPT_CONTINUE 0xFFFFFFFF #define ARPT_CONTINUE 0xFFFFFFFF
...@@ -236,6 +239,15 @@ struct arpt_get_entries ...@@ -236,6 +239,15 @@ struct arpt_get_entries
struct arpt_entry entrytable[0]; struct arpt_entry entrytable[0];
}; };
/* The argument to ARPT_SO_GET_REVISION_*. Returns highest revision
* kernel supports, if >= revision. */
struct arpt_get_revision
{
char name[ARPT_FUNCTION_MAXNAMELEN-1];
u_int8_t revision;
};
/* Standard return verdict, or do jump. */ /* Standard return verdict, or do jump. */
#define ARPT_STANDARD_TARGET "" #define ARPT_STANDARD_TARGET ""
/* Error verdict. */ /* Error verdict. */
...@@ -274,7 +286,9 @@ struct arpt_target ...@@ -274,7 +286,9 @@ struct arpt_target
{ {
struct list_head list; struct list_head list;
const char name[ARPT_FUNCTION_MAXNAMELEN]; const char name[ARPT_FUNCTION_MAXNAMELEN-1];
u_int8_t revision;
/* Returns verdict. */ /* Returns verdict. */
unsigned int (*target)(struct sk_buff **pskb, unsigned int (*target)(struct sk_buff **pskb,
......
...@@ -57,7 +57,8 @@ struct ip6t_entry_match ...@@ -57,7 +57,8 @@ struct ip6t_entry_match
u_int16_t match_size; u_int16_t match_size;
/* Used by userspace */ /* Used by userspace */
char name[IP6T_FUNCTION_MAXNAMELEN]; char name[IP6T_FUNCTION_MAXNAMELEN-1];
u_int8_t revision;
} user; } user;
struct { struct {
u_int16_t match_size; u_int16_t match_size;
...@@ -80,7 +81,8 @@ struct ip6t_entry_target ...@@ -80,7 +81,8 @@ struct ip6t_entry_target
u_int16_t target_size; u_int16_t target_size;
/* Used by userspace */ /* Used by userspace */
char name[IP6T_FUNCTION_MAXNAMELEN]; char name[IP6T_FUNCTION_MAXNAMELEN-1];
u_int8_t revision;
} user; } user;
struct { struct {
u_int16_t target_size; u_int16_t target_size;
...@@ -161,7 +163,9 @@ struct ip6t_entry ...@@ -161,7 +163,9 @@ struct ip6t_entry
#define IP6T_SO_GET_INFO (IP6T_BASE_CTL) #define IP6T_SO_GET_INFO (IP6T_BASE_CTL)
#define IP6T_SO_GET_ENTRIES (IP6T_BASE_CTL + 1) #define IP6T_SO_GET_ENTRIES (IP6T_BASE_CTL + 1)
#define IP6T_SO_GET_MAX IP6T_SO_GET_ENTRIES #define IP6T_SO_GET_REVISION_MATCH (IP6T_BASE_CTL + 2)
#define IP6T_SO_GET_REVISION_TARGET (IP6T_BASE_CTL + 3)
#define IP6T_SO_GET_MAX IP6T_SO_GET_REVISION_TARGET
/* CONTINUE verdict for targets */ /* CONTINUE verdict for targets */
#define IP6T_CONTINUE 0xFFFFFFFF #define IP6T_CONTINUE 0xFFFFFFFF
...@@ -291,6 +295,15 @@ struct ip6t_get_entries ...@@ -291,6 +295,15 @@ struct ip6t_get_entries
struct ip6t_entry entrytable[0]; struct ip6t_entry entrytable[0];
}; };
/* The argument to IP6T_SO_GET_REVISION_*. Returns highest revision
* kernel supports, if >= revision. */
struct ip6t_get_revision
{
char name[IP6T_FUNCTION_MAXNAMELEN-1];
u_int8_t revision;
};
/* Standard return verdict, or do jump. */ /* Standard return verdict, or do jump. */
#define IP6T_STANDARD_TARGET "" #define IP6T_STANDARD_TARGET ""
/* Error verdict. */ /* Error verdict. */
...@@ -352,7 +365,9 @@ struct ip6t_match ...@@ -352,7 +365,9 @@ struct ip6t_match
{ {
struct list_head list; struct list_head list;
const char name[IP6T_FUNCTION_MAXNAMELEN]; const char name[IP6T_FUNCTION_MAXNAMELEN-1];
u_int8_t revision;
/* Return true or false: return FALSE and set *hotdrop = 1 to /* Return true or false: return FALSE and set *hotdrop = 1 to
force immediate packet drop. */ force immediate packet drop. */
...@@ -387,7 +402,9 @@ struct ip6t_target ...@@ -387,7 +402,9 @@ struct ip6t_target
{ {
struct list_head list; struct list_head list;
const char name[IP6T_FUNCTION_MAXNAMELEN]; const char name[IP6T_FUNCTION_MAXNAMELEN-1];
u_int8_t revision;
/* Returns verdict. Argument order changed since 2.6.9, as this /* Returns verdict. Argument order changed since 2.6.9, as this
must now handle non-linear skbs, using skb_copy_bits and must now handle non-linear skbs, using skb_copy_bits and
......
...@@ -347,58 +347,106 @@ unsigned int arpt_do_table(struct sk_buff **pskb, ...@@ -347,58 +347,106 @@ unsigned int arpt_do_table(struct sk_buff **pskb,
return verdict; return verdict;
} }
static inline void *find_inlist_lock_noload(struct list_head *head, /*
const char *name, * These are weird, but module loading must not be done with mutex
int *error, * held (since they will register), and we have to have a single
struct semaphore *mutex) * function to use try_then_request_module().
*/
/* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
static inline struct arpt_table *find_table_lock(const char *name)
{ {
void *ret; struct arpt_table *t;
*error = down_interruptible(mutex); if (down_interruptible(&arpt_mutex) != 0)
if (*error != 0) return ERR_PTR(-EINTR);
return NULL;
ret = list_named_find(head, name); list_for_each_entry(t, &arpt_tables, list)
if (!ret) { if (strcmp(t->name, name) == 0 && try_module_get(t->me))
*error = -ENOENT; return t;
up(mutex); up(&arpt_mutex);
} return NULL;
return ret;
} }
#ifndef CONFIG_KMOD
#define find_inlist_lock(h,n,p,e,m) find_inlist_lock_noload((h),(n),(e),(m)) /* Find target, grabs ref. Returns ERR_PTR() on error. */
#else static inline struct arpt_target *find_target(const char *name, u8 revision)
static void *
find_inlist_lock(struct list_head *head,
const char *name,
const char *prefix,
int *error,
struct semaphore *mutex)
{ {
void *ret; struct arpt_target *t;
int err = 0;
if (down_interruptible(&arpt_mutex) != 0)
return ERR_PTR(-EINTR);
ret = find_inlist_lock_noload(head, name, error, mutex); list_for_each_entry(t, &arpt_target, list) {
if (!ret) { if (strcmp(t->name, name) == 0) {
duprintf("find_inlist: loading `%s%s'.\n", prefix, name); if (t->revision == revision) {
request_module("%s%s", prefix, name); if (try_module_get(t->me)) {
ret = find_inlist_lock_noload(head, name, error, mutex); up(&arpt_mutex);
return t;
}
} else
err = -EPROTOTYPE; /* Found something. */
}
} }
up(&arpt_mutex);
return ERR_PTR(err);
}
return ret; struct arpt_target *arpt_find_target(const char *name, u8 revision)
{
struct arpt_target *target;
target = try_then_request_module(find_target(name, revision),
"arpt_%s", name);
if (IS_ERR(target) || !target)
return NULL;
return target;
} }
#endif
static inline struct arpt_table *arpt_find_table_lock(const char *name, int *error, struct semaphore *mutex) static int target_revfn(const char *name, u8 revision, int *bestp)
{ {
return find_inlist_lock(&arpt_tables, name, "arptable_", error, mutex); struct arpt_target *t;
int have_rev = 0;
list_for_each_entry(t, &arpt_target, list) {
if (strcmp(t->name, name) == 0) {
if (t->revision > *bestp)
*bestp = t->revision;
if (t->revision == revision)
have_rev =1;
}
}
return have_rev;
} }
static struct arpt_target *arpt_find_target_lock(const char *name, int *error, struct semaphore *mutex) /* Returns true or false (if no such extension at all) */
static inline int find_revision(const char *name, u8 revision,
int (*revfn)(const char *, u8, int *),
int *err)
{ {
return find_inlist_lock(&arpt_target, name, "arpt_", error, mutex); int have_rev, best = -1;
if (down_interruptible(&arpt_mutex) != 0) {
*err = -EINTR;
return 1;
}
have_rev = revfn(name, revision, &best);
up(&arpt_mutex);
/* Nothing at all? Return 0 to try loading module. */
if (best == -1) {
*err = -ENOENT;
return 0;
}
*err = best;
if (!have_rev)
*err = -EPROTONOSUPPORT;
return 1;
} }
/* All zeroes == unconditional rule. */ /* All zeroes == unconditional rule. */
static inline int unconditional(const struct arpt_arp *arp) static inline int unconditional(const struct arpt_arp *arp)
{ {
...@@ -544,17 +592,15 @@ static inline int check_entry(struct arpt_entry *e, const char *name, unsigned i ...@@ -544,17 +592,15 @@ static inline int check_entry(struct arpt_entry *e, const char *name, unsigned i
} }
t = arpt_get_target(e); t = arpt_get_target(e);
target = arpt_find_target_lock(t->u.user.name, &ret, &arpt_mutex); target = try_then_request_module(find_target(t->u.user.name,
if (!target) { t->u.user.revision),
"arpt_%s", t->u.user.name);
if (IS_ERR(target) || !target) {
duprintf("check_entry: `%s' not found\n", t->u.user.name); duprintf("check_entry: `%s' not found\n", t->u.user.name);
ret = target ? PTR_ERR(target) : -ENOENT;
goto out; goto out;
} }
if (!try_module_get((target->me))) {
ret = -ENOENT;
goto out_unlock;
}
t->u.kernel.target = target; t->u.kernel.target = target;
up(&arpt_mutex);
if (t->u.kernel.target == &arpt_standard_target) { if (t->u.kernel.target == &arpt_standard_target) {
if (!standard_check(t, size)) { if (!standard_check(t, size)) {
...@@ -576,8 +622,6 @@ static inline int check_entry(struct arpt_entry *e, const char *name, unsigned i ...@@ -576,8 +622,6 @@ static inline int check_entry(struct arpt_entry *e, const char *name, unsigned i
(*i)++; (*i)++;
return 0; return 0;
out_unlock:
up(&arpt_mutex);
out: out:
return ret; return ret;
} }
...@@ -846,8 +890,8 @@ static int get_entries(const struct arpt_get_entries *entries, ...@@ -846,8 +890,8 @@ static int get_entries(const struct arpt_get_entries *entries,
int ret; int ret;
struct arpt_table *t; struct arpt_table *t;
t = arpt_find_table_lock(entries->name, &ret, &arpt_mutex); t = find_table_lock(entries->name);
if (t) { if (t || !IS_ERR(t)) {
duprintf("t->private->number = %u\n", duprintf("t->private->number = %u\n",
t->private->number); t->private->number);
if (entries->size == t->private->size) if (entries->size == t->private->size)
...@@ -859,10 +903,10 @@ static int get_entries(const struct arpt_get_entries *entries, ...@@ -859,10 +903,10 @@ static int get_entries(const struct arpt_get_entries *entries,
entries->size); entries->size);
ret = -EINVAL; ret = -EINVAL;
} }
module_put(t->me);
up(&arpt_mutex); up(&arpt_mutex);
} else } else
duprintf("get_entries: Can't find %s!\n", ret = t ? PTR_ERR(t) : -ENOENT;
entries->name);
return ret; return ret;
} }
...@@ -913,22 +957,19 @@ static int do_replace(void __user *user, unsigned int len) ...@@ -913,22 +957,19 @@ static int do_replace(void __user *user, unsigned int len)
duprintf("arp_tables: Translated table\n"); duprintf("arp_tables: Translated table\n");
t = arpt_find_table_lock(tmp.name, &ret, &arpt_mutex); t = try_then_request_module(find_table_lock(tmp.name),
if (!t) "arptable_%s", tmp.name);
if (!t || IS_ERR(t)) {
ret = t ? PTR_ERR(t) : -ENOENT;
goto free_newinfo_counters_untrans; goto free_newinfo_counters_untrans;
}
/* You lied! */ /* You lied! */
if (tmp.valid_hooks != t->valid_hooks) { if (tmp.valid_hooks != t->valid_hooks) {
duprintf("Valid hook crap: %08X vs %08X\n", duprintf("Valid hook crap: %08X vs %08X\n",
tmp.valid_hooks, t->valid_hooks); tmp.valid_hooks, t->valid_hooks);
ret = -EINVAL; ret = -EINVAL;
goto free_newinfo_counters_untrans_unlock; goto put_module;
}
/* Get a reference in advance, we're not allowed fail later */
if (!try_module_get(t->me)) {
ret = -EBUSY;
goto free_newinfo_counters_untrans_unlock;
} }
oldinfo = replace_table(t, tmp.num_counters, newinfo, &ret); oldinfo = replace_table(t, tmp.num_counters, newinfo, &ret);
...@@ -959,7 +1000,6 @@ static int do_replace(void __user *user, unsigned int len) ...@@ -959,7 +1000,6 @@ static int do_replace(void __user *user, unsigned int len)
put_module: put_module:
module_put(t->me); module_put(t->me);
free_newinfo_counters_untrans_unlock:
up(&arpt_mutex); up(&arpt_mutex);
free_newinfo_counters_untrans: free_newinfo_counters_untrans:
ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size, cleanup_entry, NULL); ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size, cleanup_entry, NULL);
...@@ -989,7 +1029,7 @@ static int do_add_counters(void __user *user, unsigned int len) ...@@ -989,7 +1029,7 @@ static int do_add_counters(void __user *user, unsigned int len)
unsigned int i; unsigned int i;
struct arpt_counters_info tmp, *paddc; struct arpt_counters_info tmp, *paddc;
struct arpt_table *t; struct arpt_table *t;
int ret; int ret = 0;
if (copy_from_user(&tmp, user, sizeof(tmp)) != 0) if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
return -EFAULT; return -EFAULT;
...@@ -1006,9 +1046,11 @@ static int do_add_counters(void __user *user, unsigned int len) ...@@ -1006,9 +1046,11 @@ static int do_add_counters(void __user *user, unsigned int len)
goto free; goto free;
} }
t = arpt_find_table_lock(tmp.name, &ret, &arpt_mutex); t = find_table_lock(tmp.name);
if (!t) if (!t || IS_ERR(t)) {
ret = t ? PTR_ERR(t) : -ENOENT;
goto free; goto free;
}
write_lock_bh(&t->lock); write_lock_bh(&t->lock);
if (t->private->number != paddc->num_counters) { if (t->private->number != paddc->num_counters) {
...@@ -1025,6 +1067,7 @@ static int do_add_counters(void __user *user, unsigned int len) ...@@ -1025,6 +1067,7 @@ static int do_add_counters(void __user *user, unsigned int len)
unlock_up_free: unlock_up_free:
write_unlock_bh(&t->lock); write_unlock_bh(&t->lock);
up(&arpt_mutex); up(&arpt_mutex);
module_put(t->me);
free: free:
vfree(paddc); vfree(paddc);
...@@ -1079,8 +1122,10 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len ...@@ -1079,8 +1122,10 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len
break; break;
} }
name[ARPT_TABLE_MAXNAMELEN-1] = '\0'; name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
t = arpt_find_table_lock(name, &ret, &arpt_mutex);
if (t) { t = try_then_request_module(find_table_lock(name),
"arptable_%s", name);
if (t && !IS_ERR(t)) {
struct arpt_getinfo info; struct arpt_getinfo info;
info.valid_hooks = t->valid_hooks; info.valid_hooks = t->valid_hooks;
...@@ -1096,9 +1141,10 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len ...@@ -1096,9 +1141,10 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len
ret = -EFAULT; ret = -EFAULT;
else else
ret = 0; ret = 0;
up(&arpt_mutex); up(&arpt_mutex);
} module_put(t->me);
} else
ret = t ? PTR_ERR(t) : -ENOENT;
} }
break; break;
...@@ -1119,6 +1165,24 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len ...@@ -1119,6 +1165,24 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len
break; break;
} }
case ARPT_SO_GET_REVISION_TARGET: {
struct arpt_get_revision rev;
if (*len != sizeof(rev)) {
ret = -EINVAL;
break;
}
if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
ret = -EFAULT;
break;
}
try_then_request_module(find_revision(rev.name, rev.revision,
target_revfn, &ret),
"arpt_%s", rev.name);
break;
}
default: default:
duprintf("do_arpt_get_ctl: unknown request %i\n", cmd); duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
ret = -EINVAL; ret = -EINVAL;
...@@ -1136,12 +1200,9 @@ int arpt_register_target(struct arpt_target *target) ...@@ -1136,12 +1200,9 @@ int arpt_register_target(struct arpt_target *target)
if (ret != 0) if (ret != 0)
return ret; return ret;
if (!list_named_insert(&arpt_target, target)) { list_add(&target->list, &arpt_target);
duprintf("arpt_register_target: `%s' already in list!\n",
target->name);
ret = -EINVAL;
}
up(&arpt_mutex); up(&arpt_mutex);
return ret; return ret;
} }
......
This diff is collapsed.
...@@ -56,8 +56,12 @@ checkentry(const char *tablename, ...@@ -56,8 +56,12 @@ checkentry(const char *tablename,
return 1; return 1;
} }
static struct ip6t_target ip6t_mark_reg static struct ip6t_target ip6t_mark_reg = {
= { { NULL, NULL }, "MARK", target, checkentry, NULL, THIS_MODULE }; .name = "MARK",
.target = target,
.checkentry = checkentry,
.me = THIS_MODULE
};
static int __init init(void) static int __init init(void)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment