blob: 48fb17417fac3397e74cb712388076ed1ee87865 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Forwarding decision
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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.
12 */
13
Herbert Xu025d89c2010-02-27 19:41:43 +000014#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
17#include <linux/netdevice.h>
WANG Congc06ee962010-05-06 00:48:24 -070018#include <linux/netpoll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/skbuff.h>
Stephen Hemminger85ca7192006-04-26 02:39:19 -070020#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/netfilter_bridge.h>
22#include "br_private.h"
23
tanxiaojun1a81a2e2013-12-16 21:32:46 +080024/* Don't forward packets to originating port or forwarding disabled */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090025static inline int should_deliver(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 const struct sk_buff *skb)
27{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020028 struct net_bridge_vlan_group *vg;
29
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020030 vg = nbp_vlan_group_rcu(p);
tanxiaojuna97bfc12013-12-19 13:28:11 +080031 return ((p->flags & BR_HAIRPIN_MODE) || skb->dev != p->dev) &&
Ido Schimmel6bc506b2016-08-25 18:42:37 +020032 br_allowed_egress(vg, skb) && p->state == BR_STATE_FORWARDING &&
33 nbp_switchdev_allowed_egress(p, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050036int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Toshiaki Makitadf356d52015-07-28 19:05:37 +090038 if (!is_skb_forwardable(skb->dev, skb))
39 goto drop;
40
41 skb_push(skb, ETH_HLEN);
42 br_drop_fake_rtable(skb);
Toshiaki Makitadf356d52015-07-28 19:05:37 +090043
44 if (skb->ip_summed == CHECKSUM_PARTIAL &&
45 (skb->protocol == htons(ETH_P_8021Q) ||
46 skb->protocol == htons(ETH_P_8021AD))) {
47 int depth;
48
49 if (!__vlan_get_protocol(skb, skb->protocol, &depth))
50 goto drop;
51
52 skb_set_network_header(skb, depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 }
54
Toshiaki Makitadf356d52015-07-28 19:05:37 +090055 dev_queue_xmit(skb);
56
57 return 0;
58
59drop:
60 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return 0;
62}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +020063EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -050065int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Eric W. Biederman29a26a52015-09-15 20:04:16 -050067 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING,
68 net, sk, skb, NULL, skb->dev,
Stephen Hemminger9ef513b2006-05-25 15:58:54 -070069 br_dev_queue_push_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +020072EXPORT_SYMBOL_GPL(br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030074static void __br_forward(const struct net_bridge_port *to,
75 struct sk_buff *skb, bool local_orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020077 struct net_bridge_vlan_group *vg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct net_device *indev;
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030079 struct net *net;
80 int br_hook;
Herbert Xu4906f992009-02-09 15:07:18 -080081
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +020082 vg = nbp_vlan_group_rcu(to);
Roopa Prabhu11538d02017-01-31 22:59:55 -080083 skb = br_handle_vlan(to->br, to, vg, skb);
Vlad Yasevich78851982013-02-13 12:00:14 +000084 if (!skb)
85 return;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 indev = skb->dev;
88 skb->dev = to->dev;
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +030089 if (!local_orig) {
90 if (skb_warn_if_lro(skb)) {
91 kfree_skb(skb);
92 return;
93 }
94 br_hook = NF_BR_FORWARD;
95 skb_forward_csum(skb);
96 net = dev_net(indev);
97 } else {
98 if (unlikely(netpoll_tx_running(to->br->dev))) {
99 if (!is_skb_forwardable(skb->dev, skb)) {
100 kfree_skb(skb);
101 } else {
102 skb_push(skb, ETH_HLEN);
103 br_netpoll_send_skb(to, skb);
104 }
105 return;
106 }
107 br_hook = NF_BR_LOCAL_OUT;
108 net = dev_net(skb->dev);
109 indev = NULL;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300112 NF_HOOK(NFPROTO_BRIDGE, br_hook,
113 net, NULL, skb, indev, skb->dev,
Jan Engelhardt713aefa2010-03-23 04:07:21 +0100114 br_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
David S. Miller87faf3c2010-03-16 14:37:47 -0700117static int deliver_clone(const struct net_bridge_port *prev,
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300118 struct sk_buff *skb, bool local_orig)
Herbert Xu025d89c2010-02-27 19:41:43 +0000119{
Herbert Xufed396a2010-06-15 21:43:07 -0700120 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
121
Herbert Xu025d89c2010-02-27 19:41:43 +0000122 skb = skb_clone(skb, GFP_ATOMIC);
123 if (!skb) {
Herbert Xu025d89c2010-02-27 19:41:43 +0000124 dev->stats.tx_dropped++;
125 return -ENOMEM;
126 }
127
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300128 __br_forward(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000129 return 0;
130}
131
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300132/**
133 * br_forward - forward a packet to a specific port
134 * @to: destination port
135 * @skb: packet being forwarded
136 * @local_rcv: packet will be received locally after forwarding
137 * @local_orig: packet is locally originated
138 *
139 * Should be called with rcu_read_lock.
140 */
141void br_forward(const struct net_bridge_port *to,
142 struct sk_buff *skb, bool local_rcv, bool local_orig)
143{
144 if (to && should_deliver(to, skb)) {
145 if (local_rcv)
146 deliver_clone(to, skb, local_orig);
147 else
148 __br_forward(to, skb, local_orig);
149 return;
150 }
151
152 if (!local_rcv)
153 kfree_skb(skb);
154}
155EXPORT_SYMBOL_GPL(br_forward);
156
Herbert Xu025d89c2010-02-27 19:41:43 +0000157static struct net_bridge_port *maybe_deliver(
158 struct net_bridge_port *prev, struct net_bridge_port *p,
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300159 struct sk_buff *skb, bool local_orig)
Herbert Xu025d89c2010-02-27 19:41:43 +0000160{
161 int err;
162
163 if (!should_deliver(p, skb))
164 return prev;
165
166 if (!prev)
167 goto out;
168
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300169 err = deliver_clone(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000170 if (err)
171 return ERR_PTR(err);
172
173out:
174 return p;
175}
176
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300177/* called under rcu_read_lock */
178void br_flood(struct net_bridge *br, struct sk_buff *skb,
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200179 enum br_pkt_type pkt_type, bool local_rcv, bool local_orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200181 u8 igmp_type = br_multicast_igmp_type(skb);
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300182 struct net_bridge_port *prev = NULL;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200183 struct net_bridge_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 list_for_each_entry_rcu(p, &br->port_list, list) {
Mike Manning99f906e2017-04-26 14:48:09 +0100186 /* Do not flood unicast traffic to ports that turn it off, nor
187 * other traffic if flood off, except for traffic we originate
188 */
189 switch (pkt_type) {
190 case BR_PKT_UNICAST:
191 if (!(p->flags & BR_FLOOD))
192 continue;
193 break;
194 case BR_PKT_MULTICAST:
195 if (!(p->flags & BR_MCAST_FLOOD) && skb->dev != br->dev)
196 continue;
197 break;
198 case BR_PKT_BROADCAST:
199 if (!(p->flags & BR_BCAST_FLOOD) && skb->dev != br->dev)
200 continue;
201 break;
202 }
Kyeyoon Park95850112014-10-23 14:49:17 -0700203
204 /* Do not flood to ports that enable proxy ARP */
205 if (p->flags & BR_PROXYARP)
206 continue;
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200207 if ((p->flags & BR_PROXYARP_WIFI) &&
208 BR_INPUT_SKB_CB(skb)->proxyarp_replied)
209 continue;
Kyeyoon Park95850112014-10-23 14:49:17 -0700210
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300211 prev = maybe_deliver(prev, p, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000212 if (IS_ERR(prev))
213 goto out;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200214 if (prev == p)
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700215 br_multicast_count(p->br, p, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200216 BR_MCAST_DIR_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
Herbert Xub33084b2010-02-27 19:41:41 +0000219 if (!prev)
220 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300222 if (local_rcv)
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300223 deliver_clone(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000224 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300225 __br_forward(prev, skb, local_orig);
Herbert Xub33084b2010-02-27 19:41:41 +0000226 return;
227
228out:
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300229 if (!local_rcv)
Herbert Xub33084b2010-02-27 19:41:41 +0000230 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Herbert Xu5cb5e942010-02-27 19:41:46 +0000233#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Arnd Bergmann5b9d6b12017-01-25 23:29:33 +0100234static void maybe_deliver_addr(struct net_bridge_port *p, struct sk_buff *skb,
235 const unsigned char *addr, bool local_orig)
236{
237 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
238 const unsigned char *src = eth_hdr(skb)->h_source;
239
240 if (!should_deliver(p, skb))
241 return;
242
243 /* Even with hairpin, no soliloquies - prevent breaking IPv6 DAD */
244 if (skb->dev == p->dev && ether_addr_equal(src, addr))
245 return;
246
247 skb = skb_copy(skb, GFP_ATOMIC);
248 if (!skb) {
249 dev->stats.tx_dropped++;
250 return;
251 }
252
253 if (!is_broadcast_ether_addr(addr))
254 memcpy(eth_hdr(skb)->h_dest, addr, ETH_ALEN);
255
256 __br_forward(p, skb, local_orig);
257}
258
Herbert Xu5cb5e942010-02-27 19:41:46 +0000259/* called with rcu_read_lock */
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300260void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
261 struct sk_buff *skb,
262 bool local_rcv, bool local_orig)
Herbert Xu5cb5e942010-02-27 19:41:46 +0000263{
264 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200265 u8 igmp_type = br_multicast_igmp_type(skb);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000266 struct net_bridge *br = netdev_priv(dev);
stephen hemmingerafe01592010-04-27 15:01:07 +0000267 struct net_bridge_port *prev = NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000268 struct net_bridge_port_group *p;
269 struct hlist_node *rp;
270
Eric Dumazete8051682010-11-15 06:38:10 +0000271 rp = rcu_dereference(hlist_first_rcu(&br->router_list));
stephen hemminger83f6a742010-04-27 15:01:06 +0000272 p = mdst ? rcu_dereference(mdst->ports) : NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000273 while (p || rp) {
stephen hemmingerafe01592010-04-27 15:01:07 +0000274 struct net_bridge_port *port, *lport, *rport;
275
Herbert Xu5cb5e942010-02-27 19:41:46 +0000276 lport = p ? p->port : NULL;
277 rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
278 NULL;
279
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100280 if ((unsigned long)lport > (unsigned long)rport) {
281 port = lport;
282
283 if (port->flags & BR_MULTICAST_TO_UNICAST) {
284 maybe_deliver_addr(lport, skb, p->eth_addr,
285 local_orig);
286 goto delivered;
287 }
288 } else {
289 port = rport;
290 }
Herbert Xu5cb5e942010-02-27 19:41:46 +0000291
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300292 prev = maybe_deliver(prev, port, skb, local_orig);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100293delivered:
Herbert Xu5cb5e942010-02-27 19:41:46 +0000294 if (IS_ERR(prev))
295 goto out;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200296 if (prev == port)
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700297 br_multicast_count(port->br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200298 BR_MCAST_DIR_TX);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000299
300 if ((unsigned long)lport >= (unsigned long)port)
stephen hemminger83f6a742010-04-27 15:01:06 +0000301 p = rcu_dereference(p->next);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000302 if ((unsigned long)rport >= (unsigned long)port)
Eric Dumazete8051682010-11-15 06:38:10 +0000303 rp = rcu_dereference(hlist_next_rcu(rp));
Herbert Xu5cb5e942010-02-27 19:41:46 +0000304 }
305
306 if (!prev)
307 goto out;
308
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300309 if (local_rcv)
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300310 deliver_clone(prev, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000311 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300312 __br_forward(prev, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000313 return;
314
315out:
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300316 if (!local_rcv)
Herbert Xu5cb5e942010-02-27 19:41:46 +0000317 kfree_skb(skb);
318}
Herbert Xu5cb5e942010-02-27 19:41:46 +0000319#endif