ec2652a459da87664ec6847c2fbad6516e3eafac
[linux-2.6-block.git] / net / bridge / netfilter / ebtable_broute.c
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 #include "../br_private.h"
19
20 /* EBT_ACCEPT means the frame will be bridged
21  * EBT_DROP means the frame will be routed
22  */
23 static struct ebt_entries initial_chain = {
24         .name           = "BROUTING",
25         .policy         = EBT_ACCEPT,
26 };
27
28 static struct ebt_replace_kernel initial_table = {
29         .name           = "broute",
30         .valid_hooks    = 1 << NF_BR_BROUTING,
31         .entries_size   = sizeof(struct ebt_entries),
32         .hook_entry     = {
33                 [NF_BR_BROUTING]        = &initial_chain,
34         },
35         .entries        = (char *)&initial_chain,
36 };
37
38 static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
39 {
40         if (valid_hooks & ~(1 << NF_BR_BROUTING))
41                 return -EINVAL;
42         return 0;
43 }
44
45 static const struct ebt_table broute_table = {
46         .name           = "broute",
47         .table          = &initial_table,
48         .valid_hooks    = 1 << NF_BR_BROUTING,
49         .check          = check,
50         .me             = THIS_MODULE,
51 };
52
53 static unsigned int ebt_broute(void *priv, struct sk_buff *skb,
54                                const struct nf_hook_state *s)
55 {
56         struct net_bridge_port *p = br_port_get_rcu(skb->dev);
57         struct nf_hook_state state;
58         unsigned char *dest;
59         int ret;
60
61         if (!p || p->state != BR_STATE_FORWARDING)
62                 return NF_ACCEPT;
63
64         nf_hook_state_init(&state, NF_BR_BROUTING,
65                            NFPROTO_BRIDGE, s->in, NULL, NULL,
66                            s->net, NULL);
67
68         ret = ebt_do_table(skb, &state, state.net->xt.broute_table);
69
70         if (ret != NF_DROP)
71                 return ret;
72
73         /* DROP in ebtables -t broute means that the
74          * skb should be routed, not bridged.
75          * This is awkward, but can't be changed for compatibility
76          * reasons.
77          *
78          * We map DROP to ACCEPT and set the ->br_netfilter_broute flag.
79          */
80         BR_INPUT_SKB_CB(skb)->br_netfilter_broute = 1;
81
82         /* undo PACKET_HOST mangling done in br_input in case the dst
83          * address matches the logical bridge but not the port.
84          */
85         dest = eth_hdr(skb)->h_dest;
86         if (skb->pkt_type == PACKET_HOST &&
87             !ether_addr_equal(skb->dev->dev_addr, dest) &&
88              ether_addr_equal(p->br->dev->dev_addr, dest))
89                 skb->pkt_type = PACKET_OTHERHOST;
90
91         return NF_ACCEPT;
92 }
93
94 static const struct nf_hook_ops ebt_ops_broute = {
95         .hook           = ebt_broute,
96         .pf             = NFPROTO_BRIDGE,
97         .hooknum        = NF_BR_PRE_ROUTING,
98         .priority       = NF_BR_PRI_FIRST,
99 };
100
101 static int __net_init broute_net_init(struct net *net)
102 {
103         return ebt_register_table(net, &broute_table, &ebt_ops_broute,
104                                   &net->xt.broute_table);
105 }
106
107 static void __net_exit broute_net_exit(struct net *net)
108 {
109         ebt_unregister_table(net, net->xt.broute_table, &ebt_ops_broute);
110 }
111
112 static struct pernet_operations broute_net_ops = {
113         .init = broute_net_init,
114         .exit = broute_net_exit,
115 };
116
117 static int __init ebtable_broute_init(void)
118 {
119         return register_pernet_subsys(&broute_net_ops);
120 }
121
122 static void __exit ebtable_broute_fini(void)
123 {
124         unregister_pernet_subsys(&broute_net_ops);
125 }
126
127 module_init(ebtable_broute_init);
128 module_exit(ebtable_broute_fini);
129 MODULE_LICENSE("GPL");