提交 223fd0ad 编写于 作者: F Florian Westphal 提交者: Pablo Neira Ayuso

bridge: broute: make broute a real ebtables table

This makes broute a normal ebtables table, hooking at PREROUTING.
The broute hook is removed.

It uses skb->cb to signal to bridge rx handler that the skb should be
routed instead of being bridged.

This change is backwards compatible with ebtables as no userspace visible
parts are changed.

This means we can also remove the !ops test in ebt_register_table,
it was only there for broute table sake.
Signed-off-by: NFlorian Westphal <fw@strlen.de>
Acked-by: NDavid S. Miller <davem@davemloft.net>
Acked-by: NNikolay Aleksandrov <nikolay@cumulusnetworks.com>
Signed-off-by: NPablo Neira Ayuso <pablo@netfilter.org>
上级 971502d7
...@@ -56,9 +56,6 @@ struct br_ip_list { ...@@ -56,9 +56,6 @@ struct br_ip_list {
extern void brioctl_set(int (*ioctl_hook)(struct net *, unsigned int, void __user *)); extern void brioctl_set(int (*ioctl_hook)(struct net *, unsigned int, void __user *));
typedef int br_should_route_hook_t(struct sk_buff *skb);
extern br_should_route_hook_t __rcu *br_should_route_hook;
#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING) #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
int br_multicast_list_adjacent(struct net_device *dev, int br_multicast_list_adjacent(struct net_device *dev,
struct list_head *br_ip_list); struct list_head *br_ip_list);
......
...@@ -24,10 +24,6 @@ ...@@ -24,10 +24,6 @@
#include "br_private.h" #include "br_private.h"
#include "br_private_tunnel.h" #include "br_private_tunnel.h"
/* Hook for brouter */
br_should_route_hook_t __rcu *br_should_route_hook __read_mostly;
EXPORT_SYMBOL(br_should_route_hook);
static int static int
br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb) br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
{ {
...@@ -234,6 +230,10 @@ static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb) ...@@ -234,6 +230,10 @@ static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb)
verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state); verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state);
switch (verdict & NF_VERDICT_MASK) { switch (verdict & NF_VERDICT_MASK) {
case NF_ACCEPT: case NF_ACCEPT:
if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) {
*pskb = skb;
return RX_HANDLER_PASS;
}
break; break;
case NF_DROP: case NF_DROP:
kfree_skb(skb); kfree_skb(skb);
...@@ -265,7 +265,6 @@ rx_handler_result_t br_handle_frame(struct sk_buff **pskb) ...@@ -265,7 +265,6 @@ rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
struct net_bridge_port *p; struct net_bridge_port *p;
struct sk_buff *skb = *pskb; struct sk_buff *skb = *pskb;
const unsigned char *dest = eth_hdr(skb)->h_dest; const unsigned char *dest = eth_hdr(skb)->h_dest;
br_should_route_hook_t *rhook;
if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
return RX_HANDLER_PASS; return RX_HANDLER_PASS;
...@@ -341,15 +340,6 @@ rx_handler_result_t br_handle_frame(struct sk_buff **pskb) ...@@ -341,15 +340,6 @@ rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
forward: forward:
switch (p->state) { switch (p->state) {
case BR_STATE_FORWARDING: case BR_STATE_FORWARDING:
rhook = rcu_dereference(br_should_route_hook);
if (rhook) {
if ((*rhook)(skb)) {
*pskb = skb;
return RX_HANDLER_PASS;
}
dest = eth_hdr(skb)->h_dest;
}
/* fall through */
case BR_STATE_LEARNING: case BR_STATE_LEARNING:
if (ether_addr_equal(p->br->dev->dev_addr, dest)) if (ether_addr_equal(p->br->dev->dev_addr, dest))
skb->pkt_type = PACKET_HOST; skb->pkt_type = PACKET_HOST;
......
...@@ -433,6 +433,9 @@ struct br_input_skb_cb { ...@@ -433,6 +433,9 @@ struct br_input_skb_cb {
#ifdef CONFIG_BRIDGE_VLAN_FILTERING #ifdef CONFIG_BRIDGE_VLAN_FILTERING
u8 vlan_filtered:1; u8 vlan_filtered:1;
#endif #endif
#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
u8 br_netfilter_broute:1;
#endif
#ifdef CONFIG_NET_SWITCHDEV #ifdef CONFIG_NET_SWITCHDEV
int offload_fwd_mark; int offload_fwd_mark;
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/if_bridge.h> #include <linux/if_bridge.h>
#include "../br_private.h"
/* EBT_ACCEPT means the frame will be bridged /* EBT_ACCEPT means the frame will be bridged
* EBT_DROP means the frame will be routed * EBT_DROP means the frame will be routed
*/ */
...@@ -48,30 +50,63 @@ static const struct ebt_table broute_table = { ...@@ -48,30 +50,63 @@ static const struct ebt_table broute_table = {
.me = THIS_MODULE, .me = THIS_MODULE,
}; };
static int ebt_broute(struct sk_buff *skb) static unsigned int ebt_broute(void *priv, struct sk_buff *skb,
const struct nf_hook_state *s)
{ {
struct net_bridge_port *p = br_port_get_rcu(skb->dev);
struct nf_hook_state state; struct nf_hook_state state;
unsigned char *dest;
int ret; int ret;
if (!p || p->state != BR_STATE_FORWARDING)
return NF_ACCEPT;
nf_hook_state_init(&state, NF_BR_BROUTING, nf_hook_state_init(&state, NF_BR_BROUTING,
NFPROTO_BRIDGE, skb->dev, NULL, NULL, NFPROTO_BRIDGE, s->in, NULL, NULL,
dev_net(skb->dev), NULL); s->net, NULL);
ret = ebt_do_table(skb, &state, state.net->xt.broute_table); ret = ebt_do_table(skb, &state, state.net->xt.broute_table);
if (ret == NF_DROP)
return 1; /* route it */ if (ret != NF_DROP)
return 0; /* bridge it */ return ret;
/* DROP in ebtables -t broute means that the
* skb should be routed, not bridged.
* This is awkward, but can't be changed for compatibility
* reasons.
*
* We map DROP to ACCEPT and set the ->br_netfilter_broute flag.
*/
BR_INPUT_SKB_CB(skb)->br_netfilter_broute = 1;
/* undo PACKET_HOST mangling done in br_input in case the dst
* address matches the logical bridge but not the port.
*/
dest = eth_hdr(skb)->h_dest;
if (skb->pkt_type == PACKET_HOST &&
!ether_addr_equal(skb->dev->dev_addr, dest) &&
ether_addr_equal(p->br->dev->dev_addr, dest))
skb->pkt_type = PACKET_OTHERHOST;
return NF_ACCEPT;
} }
static const struct nf_hook_ops ebt_ops_broute = {
.hook = ebt_broute,
.pf = NFPROTO_BRIDGE,
.hooknum = NF_BR_PRE_ROUTING,
.priority = NF_BR_PRI_FIRST,
};
static int __net_init broute_net_init(struct net *net) static int __net_init broute_net_init(struct net *net)
{ {
return ebt_register_table(net, &broute_table, NULL, return ebt_register_table(net, &broute_table, &ebt_ops_broute,
&net->xt.broute_table); &net->xt.broute_table);
} }
static void __net_exit broute_net_exit(struct net *net) static void __net_exit broute_net_exit(struct net *net)
{ {
ebt_unregister_table(net, net->xt.broute_table, NULL); ebt_unregister_table(net, net->xt.broute_table, &ebt_ops_broute);
} }
static struct pernet_operations broute_net_ops = { static struct pernet_operations broute_net_ops = {
...@@ -81,21 +116,11 @@ static struct pernet_operations broute_net_ops = { ...@@ -81,21 +116,11 @@ static struct pernet_operations broute_net_ops = {
static int __init ebtable_broute_init(void) static int __init ebtable_broute_init(void)
{ {
int ret; return register_pernet_subsys(&broute_net_ops);
ret = register_pernet_subsys(&broute_net_ops);
if (ret < 0)
return ret;
/* see br_input.c */
RCU_INIT_POINTER(br_should_route_hook,
(br_should_route_hook_t *)ebt_broute);
return 0;
} }
static void __exit ebtable_broute_fini(void) static void __exit ebtable_broute_fini(void)
{ {
RCU_INIT_POINTER(br_should_route_hook, NULL);
synchronize_net();
unregister_pernet_subsys(&broute_net_ops); unregister_pernet_subsys(&broute_net_ops);
} }
......
...@@ -1221,10 +1221,6 @@ int ebt_register_table(struct net *net, const struct ebt_table *input_table, ...@@ -1221,10 +1221,6 @@ int ebt_register_table(struct net *net, const struct ebt_table *input_table,
mutex_unlock(&ebt_mutex); mutex_unlock(&ebt_mutex);
WRITE_ONCE(*res, table); WRITE_ONCE(*res, table);
if (!ops)
return 0;
ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks)); ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
if (ret) { if (ret) {
__ebt_unregister_table(net, table); __ebt_unregister_table(net, table);
...@@ -1248,8 +1244,7 @@ int ebt_register_table(struct net *net, const struct ebt_table *input_table, ...@@ -1248,8 +1244,7 @@ int ebt_register_table(struct net *net, const struct ebt_table *input_table,
void ebt_unregister_table(struct net *net, struct ebt_table *table, void ebt_unregister_table(struct net *net, struct ebt_table *table,
const struct nf_hook_ops *ops) const struct nf_hook_ops *ops)
{ {
if (ops) nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
__ebt_unregister_table(net, table); __ebt_unregister_table(net, table);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册