3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/netpoll.h>
19 #include <linux/skbuff.h>
20 #include <linux/if_vlan.h>
21 #include <linux/netfilter_bridge.h>
22 #include "br_private.h"
24 static int deliver_clone(const struct net_bridge_port
*prev
,
26 void (*__packet_hook
)(const struct net_bridge_port
*p
,
27 struct sk_buff
*skb
));
29 /* Don't forward packets to originating port or forwarding disabled */
30 static inline int should_deliver(const struct net_bridge_port
*p
,
31 const struct sk_buff
*skb
)
33 return ((p
->flags
& BR_HAIRPIN_MODE
) || skb
->dev
!= p
->dev
) &&
34 br_allowed_egress(p
->br
, nbp_get_vlan_info(p
), skb
) &&
35 p
->state
== BR_STATE_FORWARDING
;
38 int br_dev_queue_push_xmit(struct sock
*sk
, struct sk_buff
*skb
)
40 if (!is_skb_forwardable(skb
->dev
, skb
))
43 skb_push(skb
, ETH_HLEN
);
44 br_drop_fake_rtable(skb
);
45 skb_sender_cpu_clear(skb
);
47 if (skb
->ip_summed
== CHECKSUM_PARTIAL
&&
48 (skb
->protocol
== htons(ETH_P_8021Q
) ||
49 skb
->protocol
== htons(ETH_P_8021AD
))) {
52 if (!__vlan_get_protocol(skb
, skb
->protocol
, &depth
))
55 skb_set_network_header(skb
, depth
);
66 EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit
);
68 int br_forward_finish(struct sock
*sk
, struct sk_buff
*skb
)
70 return NF_HOOK(NFPROTO_BRIDGE
, NF_BR_POST_ROUTING
, sk
, skb
,
72 br_dev_queue_push_xmit
);
75 EXPORT_SYMBOL_GPL(br_forward_finish
);
77 static void __br_deliver(const struct net_bridge_port
*to
, struct sk_buff
*skb
)
79 skb
= br_handle_vlan(to
->br
, nbp_get_vlan_info(to
), skb
);
85 if (unlikely(netpoll_tx_running(to
->br
->dev
))) {
86 if (!is_skb_forwardable(skb
->dev
, skb
))
89 skb_push(skb
, ETH_HLEN
);
90 br_netpoll_send_skb(to
, skb
);
95 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_LOCAL_OUT
, NULL
, skb
,
100 static void __br_forward(const struct net_bridge_port
*to
, struct sk_buff
*skb
)
102 struct net_device
*indev
;
104 if (skb_warn_if_lro(skb
)) {
109 skb
= br_handle_vlan(to
->br
, nbp_get_vlan_info(to
), skb
);
115 skb_forward_csum(skb
);
117 NF_HOOK(NFPROTO_BRIDGE
, NF_BR_FORWARD
, NULL
, skb
,
122 /* called with rcu_read_lock */
123 void br_deliver(const struct net_bridge_port
*to
, struct sk_buff
*skb
)
125 if (to
&& should_deliver(to
, skb
)) {
126 __br_deliver(to
, skb
);
132 EXPORT_SYMBOL_GPL(br_deliver
);
134 /* called with rcu_read_lock */
135 void br_forward(const struct net_bridge_port
*to
, struct sk_buff
*skb
, struct sk_buff
*skb0
)
137 if (should_deliver(to
, skb
)) {
139 deliver_clone(to
, skb
, __br_forward
);
141 __br_forward(to
, skb
);
149 static int deliver_clone(const struct net_bridge_port
*prev
,
151 void (*__packet_hook
)(const struct net_bridge_port
*p
,
152 struct sk_buff
*skb
))
154 struct net_device
*dev
= BR_INPUT_SKB_CB(skb
)->brdev
;
156 skb
= skb_clone(skb
, GFP_ATOMIC
);
158 dev
->stats
.tx_dropped
++;
162 __packet_hook(prev
, skb
);
166 static struct net_bridge_port
*maybe_deliver(
167 struct net_bridge_port
*prev
, struct net_bridge_port
*p
,
169 void (*__packet_hook
)(const struct net_bridge_port
*p
,
170 struct sk_buff
*skb
))
174 if (!should_deliver(p
, skb
))
180 err
= deliver_clone(prev
, skb
, __packet_hook
);
188 /* called under bridge lock */
189 static void br_flood(struct net_bridge
*br
, struct sk_buff
*skb
,
190 struct sk_buff
*skb0
,
191 void (*__packet_hook
)(const struct net_bridge_port
*p
,
192 struct sk_buff
*skb
),
195 struct net_bridge_port
*p
;
196 struct net_bridge_port
*prev
;
200 list_for_each_entry_rcu(p
, &br
->port_list
, list
) {
201 /* Do not flood unicast traffic to ports that turn it off */
202 if (unicast
&& !(p
->flags
& BR_FLOOD
))
205 /* Do not flood to ports that enable proxy ARP */
206 if (p
->flags
& BR_PROXYARP
)
208 if ((p
->flags
& BR_PROXYARP_WIFI
) &&
209 BR_INPUT_SKB_CB(skb
)->proxyarp_replied
)
212 prev
= maybe_deliver(prev
, p
, skb
, __packet_hook
);
221 deliver_clone(prev
, skb
, __packet_hook
);
223 __packet_hook(prev
, skb
);
232 /* called with rcu_read_lock */
233 void br_flood_deliver(struct net_bridge
*br
, struct sk_buff
*skb
, bool unicast
)
235 br_flood(br
, skb
, NULL
, __br_deliver
, unicast
);
238 /* called under bridge lock */
239 void br_flood_forward(struct net_bridge
*br
, struct sk_buff
*skb
,
240 struct sk_buff
*skb2
, bool unicast
)
242 br_flood(br
, skb
, skb2
, __br_forward
, unicast
);
245 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
246 /* called with rcu_read_lock */
247 static void br_multicast_flood(struct net_bridge_mdb_entry
*mdst
,
248 struct sk_buff
*skb
, struct sk_buff
*skb0
,
249 void (*__packet_hook
)(
250 const struct net_bridge_port
*p
,
251 struct sk_buff
*skb
))
253 struct net_device
*dev
= BR_INPUT_SKB_CB(skb
)->brdev
;
254 struct net_bridge
*br
= netdev_priv(dev
);
255 struct net_bridge_port
*prev
= NULL
;
256 struct net_bridge_port_group
*p
;
257 struct hlist_node
*rp
;
259 rp
= rcu_dereference(hlist_first_rcu(&br
->router_list
));
260 p
= mdst
? rcu_dereference(mdst
->ports
) : NULL
;
262 struct net_bridge_port
*port
, *lport
, *rport
;
264 lport
= p
? p
->port
: NULL
;
265 rport
= rp
? hlist_entry(rp
, struct net_bridge_port
, rlist
) :
268 port
= (unsigned long)lport
> (unsigned long)rport
?
271 prev
= maybe_deliver(prev
, port
, skb
, __packet_hook
);
275 if ((unsigned long)lport
>= (unsigned long)port
)
276 p
= rcu_dereference(p
->next
);
277 if ((unsigned long)rport
>= (unsigned long)port
)
278 rp
= rcu_dereference(hlist_next_rcu(rp
));
285 deliver_clone(prev
, skb
, __packet_hook
);
287 __packet_hook(prev
, skb
);
295 /* called with rcu_read_lock */
296 void br_multicast_deliver(struct net_bridge_mdb_entry
*mdst
,
299 br_multicast_flood(mdst
, skb
, NULL
, __br_deliver
);
302 /* called with rcu_read_lock */
303 void br_multicast_forward(struct net_bridge_mdb_entry
*mdst
,
304 struct sk_buff
*skb
, struct sk_buff
*skb2
)
306 br_multicast_flood(mdst
, skb
, skb2
, __br_forward
);