Skip to content

Commit

Permalink
[NETFILTER]: x_tables: return new table from {arp,ip,ip6}t_register_t…
Browse files Browse the repository at this point in the history
…able()

Typical table module registers xt_table structure (i.e. packet_filter)
and link it to list during it. We can't use one template for it because
corresponding list_head will become corrupted. We also can't unregister
with template because it wasn't changed at all and thus doesn't know in
which list it is.

So, we duplicate template at the very first step of table registration.
Table modules will save it for use during unregistration time and actual
filtering.

Do it at once to not screw bisection.

P.S.: renaming i.e. packet_filter => __packet_filter is temporary until
      full netnsization of table modules is done.

Signed-off-by: Alexey Dobriyan <adobriyan@sw.ru>
Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Alexey Dobriyan authored and David S. Miller committed Feb 1, 2008
1 parent 8d87005 commit 44d34e7
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 99 deletions.
4 changes: 2 additions & 2 deletions include/linux/netfilter_arp/arp_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ struct arpt_error
xt_register_target(tgt); })
#define arpt_unregister_target(tgt) xt_unregister_target(tgt)

extern int arpt_register_table(struct arpt_table *table,
const struct arpt_replace *repl);
extern struct arpt_table *arpt_register_table(struct arpt_table *table,
const struct arpt_replace *repl);
extern void arpt_unregister_table(struct arpt_table *table);
extern unsigned int arpt_do_table(struct sk_buff *skb,
unsigned int hook,
Expand Down
5 changes: 3 additions & 2 deletions include/linux/netfilter_ipv4/ip_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,9 @@ ipt_get_target(struct ipt_entry *e)
#include <linux/init.h>
extern void ipt_init(void) __init;

extern int ipt_register_table(struct xt_table *table,
const struct ipt_replace *repl);
extern struct xt_table *ipt_register_table(struct net *net,
struct xt_table *table,
const struct ipt_replace *repl);
extern void ipt_unregister_table(struct xt_table *table);

/* Standard entry. */
Expand Down
4 changes: 2 additions & 2 deletions include/linux/netfilter_ipv6/ip6_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ ip6t_get_target(struct ip6t_entry *e)
#include <linux/init.h>
extern void ip6t_init(void) __init;

extern int ip6t_register_table(struct xt_table *table,
const struct ip6t_replace *repl);
extern struct xt_table *ip6t_register_table(struct xt_table *table,
const struct ip6t_replace *repl);
extern void ip6t_unregister_table(struct xt_table *table);
extern unsigned int ip6t_do_table(struct sk_buff *skb,
unsigned int hook,
Expand Down
22 changes: 12 additions & 10 deletions net/ipv4/netfilter/arp_tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -1719,8 +1719,8 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len
return ret;
}

int arpt_register_table(struct arpt_table *table,
const struct arpt_replace *repl)
struct arpt_table *arpt_register_table(struct arpt_table *table,
const struct arpt_replace *repl)
{
int ret;
struct xt_table_info *newinfo;
Expand All @@ -1732,7 +1732,7 @@ int arpt_register_table(struct arpt_table *table,
newinfo = xt_alloc_table_info(repl->size);
if (!newinfo) {
ret = -ENOMEM;
return ret;
goto out;
}

/* choose the copy on our node/cpu */
Expand All @@ -1746,18 +1746,20 @@ int arpt_register_table(struct arpt_table *table,
repl->underflow);

duprintf("arpt_register_table: translate table gives %d\n", ret);
if (ret != 0) {
xt_free_table_info(newinfo);
return ret;
}
if (ret != 0)
goto out_free;

new_table = xt_register_table(&init_net, table, &bootstrap, newinfo);
if (IS_ERR(new_table)) {
xt_free_table_info(newinfo);
return PTR_ERR(new_table);
ret = PTR_ERR(new_table);
goto out_free;
}
return new_table;

return 0;
out_free:
xt_free_table_info(newinfo);
out:
return ERR_PTR(ret);
}

void arpt_unregister_table(struct arpt_table *table)
Expand Down
15 changes: 8 additions & 7 deletions net/ipv4/netfilter/arptable_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ static struct
.term = ARPT_ERROR_INIT,
};

static struct arpt_table packet_filter = {
static struct arpt_table __packet_filter = {
.name = "filter",
.valid_hooks = FILTER_VALID_HOOKS,
.lock = RW_LOCK_UNLOCKED,
.private = NULL,
.me = THIS_MODULE,
.af = NF_ARP,
};
static struct arpt_table *packet_filter;

/* The work comes in here from netfilter.c */
static unsigned int arpt_hook(unsigned int hook,
Expand All @@ -61,7 +62,7 @@ static unsigned int arpt_hook(unsigned int hook,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
return arpt_do_table(skb, hook, in, out, &packet_filter);
return arpt_do_table(skb, hook, in, out, packet_filter);
}

static struct nf_hook_ops arpt_ops[] __read_mostly = {
Expand Down Expand Up @@ -90,24 +91,24 @@ static int __init arptable_filter_init(void)
int ret;

/* Register table */
ret = arpt_register_table(&packet_filter, &initial_table.repl);
if (ret < 0)
return ret;
packet_filter = arpt_register_table(&__packet_filter, &initial_table.repl);
if (IS_ERR(packet_filter))
return PTR_ERR(packet_filter);

ret = nf_register_hooks(arpt_ops, ARRAY_SIZE(arpt_ops));
if (ret < 0)
goto cleanup_table;
return ret;

cleanup_table:
arpt_unregister_table(&packet_filter);
arpt_unregister_table(packet_filter);
return ret;
}

static void __exit arptable_filter_fini(void)
{
nf_unregister_hooks(arpt_ops, ARRAY_SIZE(arpt_ops));
arpt_unregister_table(&packet_filter);
arpt_unregister_table(packet_filter);
}

module_init(arptable_filter_init);
Expand Down
28 changes: 17 additions & 11 deletions net/ipv4/netfilter/ip_tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -2048,7 +2048,8 @@ do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
return ret;
}

int ipt_register_table(struct xt_table *table, const struct ipt_replace *repl)
struct xt_table *ipt_register_table(struct net *net, struct xt_table *table,
const struct ipt_replace *repl)
{
int ret;
struct xt_table_info *newinfo;
Expand All @@ -2058,8 +2059,10 @@ int ipt_register_table(struct xt_table *table, const struct ipt_replace *repl)
struct xt_table *new_table;

newinfo = xt_alloc_table_info(repl->size);
if (!newinfo)
return -ENOMEM;
if (!newinfo) {
ret = -ENOMEM;
goto out;
}

/* choose the copy on our node/cpu, but dont care about preemption */
loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
Expand All @@ -2070,18 +2073,21 @@ int ipt_register_table(struct xt_table *table, const struct ipt_replace *repl)
repl->num_entries,
repl->hook_entry,
repl->underflow);
if (ret != 0) {
xt_free_table_info(newinfo);
return ret;
}
if (ret != 0)
goto out_free;

new_table = xt_register_table(&init_net, table, &bootstrap, newinfo);
new_table = xt_register_table(net, table, &bootstrap, newinfo);
if (IS_ERR(new_table)) {
xt_free_table_info(newinfo);
return PTR_ERR(new_table);
ret = PTR_ERR(new_table);
goto out_free;
}

return 0;
return new_table;

out_free:
xt_free_table_info(newinfo);
out:
return ERR_PTR(ret);
}

void ipt_unregister_table(struct xt_table *table)
Expand Down
18 changes: 10 additions & 8 deletions net/ipv4/netfilter/iptable_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ static struct
.term = IPT_ERROR_INIT, /* ERROR */
};

static struct xt_table packet_filter = {
static struct xt_table __packet_filter = {
.name = "filter",
.valid_hooks = FILTER_VALID_HOOKS,
.lock = RW_LOCK_UNLOCKED,
.me = THIS_MODULE,
.af = AF_INET,
};
static struct xt_table *packet_filter;

/* The work comes in here from netfilter.c. */
static unsigned int
Expand All @@ -69,7 +70,7 @@ ipt_hook(unsigned int hook,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
return ipt_do_table(skb, hook, in, out, &packet_filter);
return ipt_do_table(skb, hook, in, out, packet_filter);
}

static unsigned int
Expand All @@ -88,7 +89,7 @@ ipt_local_out_hook(unsigned int hook,
return NF_ACCEPT;
}

return ipt_do_table(skb, hook, in, out, &packet_filter);
return ipt_do_table(skb, hook, in, out, packet_filter);
}

static struct nf_hook_ops ipt_ops[] __read_mostly = {
Expand Down Expand Up @@ -132,9 +133,10 @@ static int __init iptable_filter_init(void)
initial_table.entries[1].target.verdict = -forward - 1;

/* Register table */
ret = ipt_register_table(&packet_filter, &initial_table.repl);
if (ret < 0)
return ret;
packet_filter = ipt_register_table(&init_net, &__packet_filter,
&initial_table.repl);
if (IS_ERR(packet_filter))
return PTR_ERR(packet_filter);

/* Register hooks */
ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Expand All @@ -144,14 +146,14 @@ static int __init iptable_filter_init(void)
return ret;

cleanup_table:
ipt_unregister_table(&packet_filter);
ipt_unregister_table(packet_filter);
return ret;
}

static void __exit iptable_filter_fini(void)
{
nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
ipt_unregister_table(&packet_filter);
ipt_unregister_table(packet_filter);
}

module_init(iptable_filter_init);
Expand Down
18 changes: 10 additions & 8 deletions net/ipv4/netfilter/iptable_mangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ static struct
.term = IPT_ERROR_INIT, /* ERROR */
};

static struct xt_table packet_mangler = {
static struct xt_table __packet_mangler = {
.name = "mangle",
.valid_hooks = MANGLE_VALID_HOOKS,
.lock = RW_LOCK_UNLOCKED,
.me = THIS_MODULE,
.af = AF_INET,
};
static struct xt_table *packet_mangler;

/* The work comes in here from netfilter.c. */
static unsigned int
Expand All @@ -80,7 +81,7 @@ ipt_route_hook(unsigned int hook,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
return ipt_do_table(skb, hook, in, out, &packet_mangler);
return ipt_do_table(skb, hook, in, out, packet_mangler);
}

static unsigned int
Expand Down Expand Up @@ -112,7 +113,7 @@ ipt_local_hook(unsigned int hook,
daddr = iph->daddr;
tos = iph->tos;

ret = ipt_do_table(skb, hook, in, out, &packet_mangler);
ret = ipt_do_table(skb, hook, in, out, packet_mangler);
/* Reroute for ANY change. */
if (ret != NF_DROP && ret != NF_STOLEN && ret != NF_QUEUE) {
iph = ip_hdr(skb);
Expand Down Expand Up @@ -171,9 +172,10 @@ static int __init iptable_mangle_init(void)
int ret;

/* Register table */
ret = ipt_register_table(&packet_mangler, &initial_table.repl);
if (ret < 0)
return ret;
packet_mangler = ipt_register_table(&init_net, &__packet_mangler,
&initial_table.repl);
if (IS_ERR(packet_mangler))
return PTR_ERR(packet_mangler);

/* Register hooks */
ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Expand All @@ -183,14 +185,14 @@ static int __init iptable_mangle_init(void)
return ret;

cleanup_table:
ipt_unregister_table(&packet_mangler);
ipt_unregister_table(packet_mangler);
return ret;
}

static void __exit iptable_mangle_fini(void)
{
nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
ipt_unregister_table(&packet_mangler);
ipt_unregister_table(packet_mangler);
}

module_init(iptable_mangle_init);
Expand Down
18 changes: 10 additions & 8 deletions net/ipv4/netfilter/iptable_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ static struct
.term = IPT_ERROR_INIT, /* ERROR */
};

static struct xt_table packet_raw = {
static struct xt_table __packet_raw = {
.name = "raw",
.valid_hooks = RAW_VALID_HOOKS,
.lock = RW_LOCK_UNLOCKED,
.me = THIS_MODULE,
.af = AF_INET,
};
static struct xt_table *packet_raw;

/* The work comes in here from netfilter.c. */
static unsigned int
Expand All @@ -52,7 +53,7 @@ ipt_hook(unsigned int hook,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
return ipt_do_table(skb, hook, in, out, &packet_raw);
return ipt_do_table(skb, hook, in, out, packet_raw);
}

static unsigned int
Expand All @@ -70,7 +71,7 @@ ipt_local_hook(unsigned int hook,
"packet.\n");
return NF_ACCEPT;
}
return ipt_do_table(skb, hook, in, out, &packet_raw);
return ipt_do_table(skb, hook, in, out, packet_raw);
}

/* 'raw' is the very first table. */
Expand All @@ -96,9 +97,10 @@ static int __init iptable_raw_init(void)
int ret;

/* Register table */
ret = ipt_register_table(&packet_raw, &initial_table.repl);
if (ret < 0)
return ret;
packet_raw = ipt_register_table(&init_net, &__packet_raw,
&initial_table.repl);
if (IS_ERR(packet_raw))
return PTR_ERR(packet_raw);

/* Register hooks */
ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
Expand All @@ -108,14 +110,14 @@ static int __init iptable_raw_init(void)
return ret;

cleanup_table:
ipt_unregister_table(&packet_raw);
ipt_unregister_table(packet_raw);
return ret;
}

static void __exit iptable_raw_fini(void)
{
nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
ipt_unregister_table(&packet_raw);
ipt_unregister_table(packet_raw);
}

module_init(iptable_raw_init);
Expand Down
Loading

0 comments on commit 44d34e7

Please sign in to comment.