1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ebtable_broute
4 *
5 * Authors:
6 * Bart De Schuymer <bdschuym@pandora.be>
7 *
8 * April, 2002
9 *
10 * This table lets you choose between routing and bridging for frames
11 * entering on a bridge enslaved nic. This table is traversed before any
12 * other ebtables table. See net/bridge/br_input.c.
13 */
14
15 #include <linux/netfilter_bridge/ebtables.h>
16 #include <linux/module.h>
17 #include <linux/if_bridge.h>
18
19 #include "../br_private.h"
20
21 /* EBT_ACCEPT means the frame will be bridged
22 * EBT_DROP means the frame will be routed
23 */
24 static struct ebt_entries initial_chain = {
25 .name = "BROUTING",
26 .policy = EBT_ACCEPT,
27 };
28
29 static struct ebt_replace_kernel initial_table = {
30 .name = "broute",
31 .valid_hooks = 1 << NF_BR_BROUTING,
32 .entries_size = sizeof(struct ebt_entries),
33 .hook_entry = {
34 [NF_BR_BROUTING] = &initial_chain,
35 },
36 .entries = (char *)&initial_chain,
37 };
38
39 static const struct ebt_table broute_table = {
40 .name = "broute",
41 .table = &initial_table,
42 .valid_hooks = 1 << NF_BR_BROUTING,
43 .me = THIS_MODULE,
44 };
45
ebt_broute(void * priv,struct sk_buff * skb,const struct nf_hook_state * s)46 static unsigned int ebt_broute(void *priv, struct sk_buff *skb,
47 const struct nf_hook_state *s)
48 {
49 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
50 struct nf_hook_state state;
51 unsigned char *dest;
52 int ret;
53
54 if (!p || p->state != BR_STATE_FORWARDING)
55 return NF_ACCEPT;
56
57 nf_hook_state_init(&state, NF_BR_BROUTING,
58 NFPROTO_BRIDGE, s->in, NULL, NULL,
59 s->net, NULL);
60
61 ret = ebt_do_table(skb, &state, state.net->xt.broute_table);
62
63 if (ret != NF_DROP)
64 return ret;
65
66 /* DROP in ebtables -t broute means that the
67 * skb should be routed, not bridged.
68 * This is awkward, but can't be changed for compatibility
69 * reasons.
70 *
71 * We map DROP to ACCEPT and set the ->br_netfilter_broute flag.
72 */
73 BR_INPUT_SKB_CB(skb)->br_netfilter_broute = 1;
74
75 /* undo PACKET_HOST mangling done in br_input in case the dst
76 * address matches the logical bridge but not the port.
77 */
78 dest = eth_hdr(skb)->h_dest;
79 if (skb->pkt_type == PACKET_HOST &&
80 !ether_addr_equal(skb->dev->dev_addr, dest) &&
81 ether_addr_equal(p->br->dev->dev_addr, dest))
82 skb->pkt_type = PACKET_OTHERHOST;
83
84 return NF_ACCEPT;
85 }
86
87 static const struct nf_hook_ops ebt_ops_broute = {
88 .hook = ebt_broute,
89 .pf = NFPROTO_BRIDGE,
90 .hooknum = NF_BR_PRE_ROUTING,
91 .priority = NF_BR_PRI_FIRST,
92 };
93
broute_net_init(struct net * net)94 static int __net_init broute_net_init(struct net *net)
95 {
96 return ebt_register_table(net, &broute_table, &ebt_ops_broute,
97 &net->xt.broute_table);
98 }
99
broute_net_pre_exit(struct net * net)100 static void __net_exit broute_net_pre_exit(struct net *net)
101 {
102 ebt_unregister_table_pre_exit(net, "broute", &ebt_ops_broute);
103 }
104
broute_net_exit(struct net * net)105 static void __net_exit broute_net_exit(struct net *net)
106 {
107 ebt_unregister_table(net, net->xt.broute_table);
108 }
109
110 static struct pernet_operations broute_net_ops = {
111 .init = broute_net_init,
112 .exit = broute_net_exit,
113 .pre_exit = broute_net_pre_exit,
114 };
115
ebtable_broute_init(void)116 static int __init ebtable_broute_init(void)
117 {
118 return register_pernet_subsys(&broute_net_ops);
119 }
120
ebtable_broute_fini(void)121 static void __exit ebtable_broute_fini(void)
122 {
123 unregister_pernet_subsys(&broute_net_ops);
124 }
125
126 module_init(ebtable_broute_init);
127 module_exit(ebtable_broute_fini);
128 MODULE_LICENSE("GPL");
129