]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/bridge/br_forward.c
992ec49a96aa7e289bd3c74bca9a606762c63614
[mirror_ubuntu-bionic-kernel.git] / net / bridge / br_forward.c
1 /*
2 * Forwarding decision
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
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.
12 */
13
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"
23
24 static int deliver_clone(const struct net_bridge_port *prev,
25 struct sk_buff *skb,
26 void (*__packet_hook)(const struct net_bridge_port *p,
27 struct sk_buff *skb));
28
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)
32 {
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;
36 }
37
38 int br_dev_queue_push_xmit(struct sk_buff *skb)
39 {
40 /* ip_fragment doesn't copy the MAC header */
41 if (nf_bridge_maybe_copy_header(skb) ||
42 !is_skb_forwardable(skb->dev, skb)) {
43 kfree_skb(skb);
44 } else {
45 skb_push(skb, ETH_HLEN);
46 br_drop_fake_rtable(skb);
47 dev_queue_xmit(skb);
48 }
49
50 return 0;
51 }
52 EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit);
53
54 int br_forward_finish(struct sk_buff *skb)
55 {
56 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev,
57 br_dev_queue_push_xmit);
58
59 }
60 EXPORT_SYMBOL_GPL(br_forward_finish);
61
62 static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
63 {
64 skb = br_handle_vlan(to->br, nbp_get_vlan_info(to), skb);
65 if (!skb)
66 return;
67
68 skb->dev = to->dev;
69
70 if (unlikely(netpoll_tx_running(to->br->dev))) {
71 if (!is_skb_forwardable(skb->dev, skb))
72 kfree_skb(skb);
73 else {
74 skb_push(skb, ETH_HLEN);
75 br_netpoll_send_skb(to, skb);
76 }
77 return;
78 }
79
80 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
81 br_forward_finish);
82 }
83
84 static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
85 {
86 struct net_device *indev;
87
88 if (skb_warn_if_lro(skb)) {
89 kfree_skb(skb);
90 return;
91 }
92
93 skb = br_handle_vlan(to->br, nbp_get_vlan_info(to), skb);
94 if (!skb)
95 return;
96
97 indev = skb->dev;
98 skb->dev = to->dev;
99 skb_forward_csum(skb);
100
101 NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev,
102 br_forward_finish);
103 }
104
105 /* called with rcu_read_lock */
106 void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
107 {
108 if (to && should_deliver(to, skb)) {
109 __br_deliver(to, skb);
110 return;
111 }
112
113 kfree_skb(skb);
114 }
115
116 /* called with rcu_read_lock */
117 void br_forward(const struct net_bridge_port *to, struct sk_buff *skb, struct sk_buff *skb0)
118 {
119 if (should_deliver(to, skb)) {
120 if (skb0)
121 deliver_clone(to, skb, __br_forward);
122 else
123 __br_forward(to, skb);
124 return;
125 }
126
127 if (!skb0)
128 kfree_skb(skb);
129 }
130
131 static int deliver_clone(const struct net_bridge_port *prev,
132 struct sk_buff *skb,
133 void (*__packet_hook)(const struct net_bridge_port *p,
134 struct sk_buff *skb))
135 {
136 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
137
138 skb = skb_clone(skb, GFP_ATOMIC);
139 if (!skb) {
140 dev->stats.tx_dropped++;
141 return -ENOMEM;
142 }
143
144 __packet_hook(prev, skb);
145 return 0;
146 }
147
148 static struct net_bridge_port *maybe_deliver(
149 struct net_bridge_port *prev, struct net_bridge_port *p,
150 struct sk_buff *skb,
151 void (*__packet_hook)(const struct net_bridge_port *p,
152 struct sk_buff *skb))
153 {
154 int err;
155
156 if (!should_deliver(p, skb))
157 return prev;
158
159 if (!prev)
160 goto out;
161
162 err = deliver_clone(prev, skb, __packet_hook);
163 if (err)
164 return ERR_PTR(err);
165
166 out:
167 return p;
168 }
169
170 /* called under bridge lock */
171 static void br_flood(struct net_bridge *br, struct sk_buff *skb,
172 struct sk_buff *skb0,
173 void (*__packet_hook)(const struct net_bridge_port *p,
174 struct sk_buff *skb),
175 bool unicast)
176 {
177 struct net_bridge_port *p;
178 struct net_bridge_port *prev;
179
180 prev = NULL;
181
182 list_for_each_entry_rcu(p, &br->port_list, list) {
183 /* Do not flood unicast traffic to ports that turn it off */
184 if (unicast && !(p->flags & BR_FLOOD))
185 continue;
186 prev = maybe_deliver(prev, p, skb, __packet_hook);
187 if (IS_ERR(prev))
188 goto out;
189 }
190
191 if (!prev)
192 goto out;
193
194 if (skb0)
195 deliver_clone(prev, skb, __packet_hook);
196 else
197 __packet_hook(prev, skb);
198 return;
199
200 out:
201 if (!skb0)
202 kfree_skb(skb);
203 }
204
205
206 /* called with rcu_read_lock */
207 void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, bool unicast)
208 {
209 br_flood(br, skb, NULL, __br_deliver, unicast);
210 }
211
212 /* called under bridge lock */
213 void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
214 struct sk_buff *skb2, bool unicast)
215 {
216 br_flood(br, skb, skb2, __br_forward, unicast);
217 }
218
219 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
220 /* called with rcu_read_lock */
221 static void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
222 struct sk_buff *skb, struct sk_buff *skb0,
223 void (*__packet_hook)(
224 const struct net_bridge_port *p,
225 struct sk_buff *skb))
226 {
227 struct net_device *dev = BR_INPUT_SKB_CB(skb)->brdev;
228 struct net_bridge *br = netdev_priv(dev);
229 struct net_bridge_port *prev = NULL;
230 struct net_bridge_port_group *p;
231 struct hlist_node *rp;
232
233 rp = rcu_dereference(hlist_first_rcu(&br->router_list));
234 p = mdst ? rcu_dereference(mdst->ports) : NULL;
235 while (p || rp) {
236 struct net_bridge_port *port, *lport, *rport;
237
238 lport = p ? p->port : NULL;
239 rport = rp ? hlist_entry(rp, struct net_bridge_port, rlist) :
240 NULL;
241
242 port = (unsigned long)lport > (unsigned long)rport ?
243 lport : rport;
244
245 prev = maybe_deliver(prev, port, skb, __packet_hook);
246 if (IS_ERR(prev))
247 goto out;
248
249 if ((unsigned long)lport >= (unsigned long)port)
250 p = rcu_dereference(p->next);
251 if ((unsigned long)rport >= (unsigned long)port)
252 rp = rcu_dereference(hlist_next_rcu(rp));
253 }
254
255 if (!prev)
256 goto out;
257
258 if (skb0)
259 deliver_clone(prev, skb, __packet_hook);
260 else
261 __packet_hook(prev, skb);
262 return;
263
264 out:
265 if (!skb0)
266 kfree_skb(skb);
267 }
268
269 /* called with rcu_read_lock */
270 void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
271 struct sk_buff *skb)
272 {
273 br_multicast_flood(mdst, skb, NULL, __br_deliver);
274 }
275
276 /* called with rcu_read_lock */
277 void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
278 struct sk_buff *skb, struct sk_buff *skb2)
279 {
280 br_multicast_flood(mdst, skb, skb2, __br_forward);
281 }
282 #endif