ebtable_broute.c 2.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 *  ebtable_broute
 *
 *	Authors:
 *	Bart De Schuymer <bdschuym@pandora.be>
 *
 *  April, 2002
 *
 *  This table lets you choose between routing and bridging for frames
 *  entering on a bridge enslaved nic. This table is traversed before any
 *  other ebtables table. See net/bridge/br_input.c.
 */

#include <linux/netfilter_bridge/ebtables.h>
#include <linux/module.h>
#include <linux/if_bridge.h>

/* EBT_ACCEPT means the frame will be bridged
 * EBT_DROP means the frame will be routed
 */
static struct ebt_entries initial_chain = {
	.name		= "BROUTING",
	.policy		= EBT_ACCEPT,
};

26
static struct ebt_replace_kernel initial_table =
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
{
	.name		= "broute",
	.valid_hooks	= 1 << NF_BR_BROUTING,
	.entries_size	= sizeof(struct ebt_entries),
	.hook_entry	= {
		[NF_BR_BROUTING]	= &initial_chain,
	},
	.entries	= (char *)&initial_chain,
};

static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
{
	if (valid_hooks & ~(1 << NF_BR_BROUTING))
		return -EINVAL;
	return 0;
}

44
static const struct ebt_table broute_table =
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52
{
	.name		= "broute",
	.table		= &initial_table,
	.valid_hooks	= 1 << NF_BR_BROUTING,
	.check		= check,
	.me		= THIS_MODULE,
};

53
static int ebt_broute(struct sk_buff *skb)
L
Linus Torvalds 已提交
54 55 56
{
	int ret;

57
	ret = ebt_do_table(NF_BR_BROUTING, skb, skb->dev, NULL,
58
			   dev_net(skb->dev)->xt.broute_table);
L
Linus Torvalds 已提交
59 60 61 62 63
	if (ret == NF_DROP)
		return 1; /* route it */
	return 0; /* bridge it */
}

64 65 66
static int __net_init broute_net_init(struct net *net)
{
	net->xt.broute_table = ebt_register_table(net, &broute_table);
67
	return PTR_ERR_OR_ZERO(net->xt.broute_table);
68 69 70 71
}

static void __net_exit broute_net_exit(struct net *net)
{
72
	ebt_unregister_table(net, net->xt.broute_table);
73 74 75 76 77 78 79
}

static struct pernet_operations broute_net_ops = {
	.init = broute_net_init,
	.exit = broute_net_exit,
};

80
static int __init ebtable_broute_init(void)
L
Linus Torvalds 已提交
81
{
82 83 84 85 86
	int ret;

	ret = register_pernet_subsys(&broute_net_ops);
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
87
	/* see br_input.c */
88
	RCU_INIT_POINTER(br_should_route_hook,
89
			   (br_should_route_hook_t *)ebt_broute);
90
	return 0;
L
Linus Torvalds 已提交
91 92
}

93
static void __exit ebtable_broute_fini(void)
L
Linus Torvalds 已提交
94
{
95
	RCU_INIT_POINTER(br_should_route_hook, NULL);
L
Linus Torvalds 已提交
96
	synchronize_net();
97
	unregister_pernet_subsys(&broute_net_ops);
L
Linus Torvalds 已提交
98 99
}

100 101
module_init(ebtable_broute_init);
module_exit(ebtable_broute_fini);
L
Linus Torvalds 已提交
102
MODULE_LICENSE("GPL");