blob: 8604dfc1fc3be0f2c36f6b84b3bceecb930cdfba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ebtable_broute
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
6 *
7 * April, 2002
8 *
9 * This table lets you choose between routing and bridging for frames
10 * entering on a bridge enslaved nic. This table is traversed before any
11 * other ebtables table. See net/bridge/br_input.c.
12 */
13
14#include <linux/netfilter_bridge/ebtables.h>
15#include <linux/module.h>
16#include <linux/if_bridge.h>
17
18/* EBT_ACCEPT means the frame will be bridged
19 * EBT_DROP means the frame will be routed
20 */
21static struct ebt_entries initial_chain = {
22 .name = "BROUTING",
23 .policy = EBT_ACCEPT,
24};
25
Al Viro1e419cd2006-11-30 19:28:48 -080026static struct ebt_replace_kernel initial_table =
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 .name = "broute",
29 .valid_hooks = 1 << NF_BR_BROUTING,
30 .entries_size = sizeof(struct ebt_entries),
31 .hook_entry = {
32 [NF_BR_BROUTING] = &initial_chain,
33 },
34 .entries = (char *)&initial_chain,
35};
36
37static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
38{
39 if (valid_hooks & ~(1 << NF_BR_BROUTING))
40 return -EINVAL;
41 return 0;
42}
43
Alexey Dobriyan8157e6d2008-11-04 14:29:03 +010044static struct ebt_table broute_table =
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 .name = "broute",
47 .table = &initial_table,
48 .valid_hooks = 1 << NF_BR_BROUTING,
Alexey Dobriyan8157e6d2008-11-04 14:29:03 +010049 .lock = __RW_LOCK_UNLOCKED(broute_table.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 .check = check,
51 .me = THIS_MODULE,
52};
53
Herbert Xu3db05fe2007-10-15 00:53:15 -070054static int ebt_broute(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 int ret;
57
Herbert Xu3db05fe2007-10-15 00:53:15 -070058 ret = ebt_do_table(NF_BR_BROUTING, skb, skb->dev, NULL,
Alexey Dobriyan8157e6d2008-11-04 14:29:03 +010059 dev_net(skb->dev)->xt.broute_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (ret == NF_DROP)
61 return 1; /* route it */
62 return 0; /* bridge it */
63}
64
Alexey Dobriyan8157e6d2008-11-04 14:29:03 +010065static int __net_init broute_net_init(struct net *net)
66{
67 net->xt.broute_table = ebt_register_table(net, &broute_table);
68 if (IS_ERR(net->xt.broute_table))
69 return PTR_ERR(net->xt.broute_table);
70 return 0;
71}
72
73static void __net_exit broute_net_exit(struct net *net)
74{
75 ebt_unregister_table(net->xt.broute_table);
76}
77
78static struct pernet_operations broute_net_ops = {
79 .init = broute_net_init,
80 .exit = broute_net_exit,
81};
82
Andrew Morton65b4b4e2006-03-28 16:37:06 -080083static int __init ebtable_broute_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Alexey Dobriyan8157e6d2008-11-04 14:29:03 +010085 int ret;
86
87 ret = register_pernet_subsys(&broute_net_ops);
88 if (ret < 0)
89 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /* see br_input.c */
Pavel Emelyanov82de3822007-11-29 23:58:58 +110091 rcu_assign_pointer(br_should_route_hook, ebt_broute);
Alexey Dobriyan6beceee2008-11-04 14:27:15 +010092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Andrew Morton65b4b4e2006-03-28 16:37:06 -080095static void __exit ebtable_broute_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Pavel Emelyanov82de3822007-11-29 23:58:58 +110097 rcu_assign_pointer(br_should_route_hook, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 synchronize_net();
Alexey Dobriyan8157e6d2008-11-04 14:29:03 +010099 unregister_pernet_subsys(&broute_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800102module_init(ebtable_broute_init);
103module_exit(ebtable_broute_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104MODULE_LICENSE("GPL");