1 /*
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 */
21 static struct ebt_entries initial_chain = {
22 .name = "BROUTING",
23 .policy = EBT_ACCEPT,
24 };
25
26 static struct ebt_replace_kernel initial_table =
27 {
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
check(const struct ebt_table_info * info,unsigned int valid_hooks)37 static 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
44 static const struct ebt_table broute_table =
45 {
46 .name = "broute",
47 .table = &initial_table,
48 .valid_hooks = 1 << NF_BR_BROUTING,
49 .check = check,
50 .me = THIS_MODULE,
51 };
52
ebt_broute(struct sk_buff * skb)53 static int ebt_broute(struct sk_buff *skb)
54 {
55 int ret;
56
57 ret = ebt_do_table(NF_BR_BROUTING, skb, skb->dev, NULL,
58 dev_net(skb->dev)->xt.broute_table);
59 if (ret == NF_DROP)
60 return 1; /* route it */
61 return 0; /* bridge it */
62 }
63
broute_net_init(struct net * net)64 static int __net_init broute_net_init(struct net *net)
65 {
66 net->xt.broute_table = ebt_register_table(net, &broute_table);
67 return PTR_RET(net->xt.broute_table);
68 }
69
broute_net_exit(struct net * net)70 static void __net_exit broute_net_exit(struct net *net)
71 {
72 ebt_unregister_table(net, net->xt.broute_table);
73 }
74
75 static struct pernet_operations broute_net_ops = {
76 .init = broute_net_init,
77 .exit = broute_net_exit,
78 };
79
ebtable_broute_init(void)80 static int __init ebtable_broute_init(void)
81 {
82 int ret;
83
84 ret = register_pernet_subsys(&broute_net_ops);
85 if (ret < 0)
86 return ret;
87 /* see br_input.c */
88 RCU_INIT_POINTER(br_should_route_hook,
89 (br_should_route_hook_t *)ebt_broute);
90 return 0;
91 }
92
ebtable_broute_fini(void)93 static void __exit ebtable_broute_fini(void)
94 {
95 RCU_INIT_POINTER(br_should_route_hook, NULL);
96 synchronize_net();
97 unregister_pernet_subsys(&broute_net_ops);
98 }
99
100 module_init(ebtable_broute_init);
101 module_exit(ebtable_broute_fini);
102 MODULE_LICENSE("GPL");
103