blob: 8498e35036059e083d5ccd49ae36ca8c9bc25fcd [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);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +020083 skb = br_handle_vlan(to->br, 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) {
Vlad Yasevich867a5942013-06-05 10:08:01 -0400186 /* Do not flood unicast traffic to ports that turn it off */
Nikolay Aleksandrov8addd5e2016-08-31 15:36:51 +0200187 if (pkt_type == BR_PKT_UNICAST && !(p->flags & BR_FLOOD))
Vlad Yasevich867a5942013-06-05 10:08:01 -0400188 continue;
Mike Manningfa7c48f2017-03-01 09:55:28 +0000189 /* Do not flood if mc off, except for traffic we originate */
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200190 if (pkt_type == BR_PKT_MULTICAST &&
Mike Manningfa7c48f2017-03-01 09:55:28 +0000191 !(p->flags & BR_MCAST_FLOOD) && skb->dev != br->dev)
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200192 continue;
Kyeyoon Park95850112014-10-23 14:49:17 -0700193
194 /* Do not flood to ports that enable proxy ARP */
195 if (p->flags & BR_PROXYARP)
196 continue;
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200197 if ((p->flags & BR_PROXYARP_WIFI) &&
198 BR_INPUT_SKB_CB(skb)->proxyarp_replied)
199 continue;
Kyeyoon Park95850112014-10-23 14:49:17 -0700200
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300201 prev = maybe_deliver(prev, p, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000202 if (IS_ERR(prev))
203 goto out;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200204 if (prev == p)
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700205 br_multicast_count(p->br, p, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200206 BR_MCAST_DIR_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
Herbert Xub33084b2010-02-27 19:41:41 +0000209 if (!prev)
210 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300212 if (local_rcv)
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300213 deliver_clone(prev, skb, local_orig);
Herbert Xu025d89c2010-02-27 19:41:43 +0000214 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300215 __br_forward(prev, skb, local_orig);
Herbert Xub33084b2010-02-27 19:41:41 +0000216 return;
217
218out:
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300219 if (!local_rcv)
Herbert Xub33084b2010-02-27 19:41:41 +0000220 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Herbert Xu5cb5e942010-02-27 19:41:46 +0000223#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
224/* called with rcu_read_lock */
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300225void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
226 struct sk_buff *skb,
227 bool local_rcv, bool local_orig)
Herbert Xu5cb5e942010-02-27 19:41:46 +0000228{
229 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200230 u8 igmp_type = br_multicast_igmp_type(skb);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000231 struct net_bridge *br = netdev_priv(dev);
stephen hemmingerafe01592010-04-27 15:01:07 +0000232 struct net_bridge_port *prev = NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000233 struct net_bridge_port_group *p;
234 struct hlist_node *rp;
235
Eric Dumazete8051682010-11-15 06:38:10 +0000236 rp = rcu_dereference(hlist_first_rcu(&br->router_list));
stephen hemminger83f6a742010-04-27 15:01:06 +0000237 p = mdst ? rcu_dereference(mdst->ports) : NULL;
Herbert Xu5cb5e942010-02-27 19:41:46 +0000238 while (p || rp) {
stephen hemmingerafe01592010-04-27 15:01:07 +0000239 struct net_bridge_port *port, *lport, *rport;
240
Herbert Xu5cb5e942010-02-27 19:41:46 +0000241 lport = p ? p->port : NULL;
242 rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
243 NULL;
244
245 port = (unsigned long)lport > (unsigned long)rport ?
246 lport : rport;
247
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300248 prev = maybe_deliver(prev, port, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000249 if (IS_ERR(prev))
250 goto out;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200251 if (prev == port)
Nikolay Aleksandrova65056e2016-07-06 12:12:21 -0700252 br_multicast_count(port->br, port, skb, igmp_type,
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200253 BR_MCAST_DIR_TX);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000254
255 if ((unsigned long)lport >= (unsigned long)port)
stephen hemminger83f6a742010-04-27 15:01:06 +0000256 p = rcu_dereference(p->next);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000257 if ((unsigned long)rport >= (unsigned long)port)
Eric Dumazete8051682010-11-15 06:38:10 +0000258 rp = rcu_dereference(hlist_next_rcu(rp));
Herbert Xu5cb5e942010-02-27 19:41:46 +0000259 }
260
261 if (!prev)
262 goto out;
263
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300264 if (local_rcv)
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300265 deliver_clone(prev, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000266 else
Nikolay Aleksandrov37b090e2016-07-14 06:10:02 +0300267 __br_forward(prev, skb, local_orig);
Herbert Xu5cb5e942010-02-27 19:41:46 +0000268 return;
269
270out:
Nikolay Aleksandrovb35c5f62016-07-14 06:10:01 +0300271 if (!local_rcv)
Herbert Xu5cb5e942010-02-27 19:41:46 +0000272 kfree_skb(skb);
273}
Herbert Xu5cb5e942010-02-27 19:41:46 +0000274#endif