xref: /openbmc/linux/net/bridge/br_input.c (revision b1c3d2be)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Handle incoming frames
4  *	Linux ethernet bridge
5  *
6  *	Authors:
7  *	Lennert Buytenhek		<buytenh@gnu.org>
8  */
9 
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/netfilter_bridge.h>
15 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
16 #include <net/netfilter/nf_queue.h>
17 #endif
18 #include <linux/neighbour.h>
19 #include <net/arp.h>
20 #include <net/dsa.h>
21 #include <linux/export.h>
22 #include <linux/rculist.h>
23 #include "br_private.h"
24 #include "br_private_tunnel.h"
25 
26 static int
27 br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
28 {
29 	br_drop_fake_rtable(skb);
30 	return netif_receive_skb(skb);
31 }
32 
33 static int br_pass_frame_up(struct sk_buff *skb)
34 {
35 	struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev;
36 	struct net_bridge *br = netdev_priv(brdev);
37 	struct net_bridge_vlan_group *vg;
38 
39 	dev_sw_netstats_rx_add(brdev, skb->len);
40 
41 	vg = br_vlan_group_rcu(br);
42 
43 	/* Reset the offload_fwd_mark because there could be a stacked
44 	 * bridge above, and it should not think this bridge it doing
45 	 * that bridge's work forwarding out its ports.
46 	 */
47 	br_switchdev_frame_unmark(skb);
48 
49 	/* Bridge is just like any other port.  Make sure the
50 	 * packet is allowed except in promisc mode when someone
51 	 * may be running packet capture.
52 	 */
53 	if (!(brdev->flags & IFF_PROMISC) &&
54 	    !br_allowed_egress(vg, skb)) {
55 		kfree_skb(skb);
56 		return NET_RX_DROP;
57 	}
58 
59 	indev = skb->dev;
60 	skb->dev = brdev;
61 	skb = br_handle_vlan(br, NULL, vg, skb);
62 	if (!skb)
63 		return NET_RX_DROP;
64 	/* update the multicast stats if the packet is IGMP/MLD */
65 	br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb),
66 			   BR_MCAST_DIR_TX);
67 
68 	return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
69 		       dev_net(indev), NULL, skb, indev, NULL,
70 		       br_netif_receive_skb);
71 }
72 
73 /* note: already called with rcu_read_lock */
74 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
75 {
76 	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
77 	enum br_pkt_type pkt_type = BR_PKT_UNICAST;
78 	struct net_bridge_fdb_entry *dst = NULL;
79 	struct net_bridge_mcast_port *pmctx;
80 	struct net_bridge_mdb_entry *mdst;
81 	bool local_rcv, mcast_hit = false;
82 	struct net_bridge_mcast *brmctx;
83 	struct net_bridge_vlan *vlan;
84 	struct net_bridge *br;
85 	u16 vid = 0;
86 	u8 state;
87 
88 	if (!p)
89 		goto drop;
90 
91 	br = p->br;
92 
93 	if (br_mst_is_enabled(br)) {
94 		state = BR_STATE_FORWARDING;
95 	} else {
96 		if (p->state == BR_STATE_DISABLED)
97 			goto drop;
98 
99 		state = p->state;
100 	}
101 
102 	brmctx = &p->br->multicast_ctx;
103 	pmctx = &p->multicast_ctx;
104 	if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid,
105 				&state, &vlan))
106 		goto out;
107 
108 	if (p->flags & BR_PORT_LOCKED) {
109 		struct net_bridge_fdb_entry *fdb_src =
110 			br_fdb_find_rcu(br, eth_hdr(skb)->h_source, vid);
111 
112 		if (!fdb_src) {
113 			/* FDB miss. Create locked FDB entry if MAB is enabled
114 			 * and drop the packet.
115 			 */
116 			if (p->flags & BR_PORT_MAB)
117 				br_fdb_update(br, p, eth_hdr(skb)->h_source,
118 					      vid, BIT(BR_FDB_LOCKED));
119 			goto drop;
120 		} else if (READ_ONCE(fdb_src->dst) != p ||
121 			   test_bit(BR_FDB_LOCAL, &fdb_src->flags)) {
122 			/* FDB mismatch. Drop the packet without roaming. */
123 			goto drop;
124 		} else if (test_bit(BR_FDB_LOCKED, &fdb_src->flags)) {
125 			/* FDB match, but entry is locked. Refresh it and drop
126 			 * the packet.
127 			 */
128 			br_fdb_update(br, p, eth_hdr(skb)->h_source, vid,
129 				      BIT(BR_FDB_LOCKED));
130 			goto drop;
131 		}
132 	}
133 
134 	nbp_switchdev_frame_mark(p, skb);
135 
136 	/* insert into forwarding database after filtering to avoid spoofing */
137 	if (p->flags & BR_LEARNING)
138 		br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, 0);
139 
140 	local_rcv = !!(br->dev->flags & IFF_PROMISC);
141 	if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) {
142 		/* by definition the broadcast is also a multicast address */
143 		if (is_broadcast_ether_addr(eth_hdr(skb)->h_dest)) {
144 			pkt_type = BR_PKT_BROADCAST;
145 			local_rcv = true;
146 		} else {
147 			pkt_type = BR_PKT_MULTICAST;
148 			if (br_multicast_rcv(&brmctx, &pmctx, vlan, skb, vid))
149 				goto drop;
150 		}
151 	}
152 
153 	if (state == BR_STATE_LEARNING)
154 		goto drop;
155 
156 	BR_INPUT_SKB_CB(skb)->brdev = br->dev;
157 	BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED);
158 
159 	if (IS_ENABLED(CONFIG_INET) &&
160 	    (skb->protocol == htons(ETH_P_ARP) ||
161 	     skb->protocol == htons(ETH_P_RARP))) {
162 		br_do_proxy_suppress_arp(skb, br, vid, p);
163 	} else if (IS_ENABLED(CONFIG_IPV6) &&
164 		   skb->protocol == htons(ETH_P_IPV6) &&
165 		   br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
166 		   pskb_may_pull(skb, sizeof(struct ipv6hdr) +
167 				 sizeof(struct nd_msg)) &&
168 		   ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
169 			struct nd_msg *msg, _msg;
170 
171 			msg = br_is_nd_neigh_msg(skb, &_msg);
172 			if (msg)
173 				br_do_suppress_nd(skb, br, vid, p, msg);
174 	}
175 
176 	switch (pkt_type) {
177 	case BR_PKT_MULTICAST:
178 		mdst = br_mdb_get(brmctx, skb, vid);
179 		if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
180 		    br_multicast_querier_exists(brmctx, eth_hdr(skb), mdst)) {
181 			if ((mdst && mdst->host_joined) ||
182 			    br_multicast_is_router(brmctx, skb)) {
183 				local_rcv = true;
184 				br->dev->stats.multicast++;
185 			}
186 			mcast_hit = true;
187 		} else {
188 			local_rcv = true;
189 			br->dev->stats.multicast++;
190 		}
191 		break;
192 	case BR_PKT_UNICAST:
193 		dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid);
194 		break;
195 	default:
196 		break;
197 	}
198 
199 	if (dst) {
200 		unsigned long now = jiffies;
201 
202 		if (test_bit(BR_FDB_LOCAL, &dst->flags))
203 			return br_pass_frame_up(skb);
204 
205 		if (now != dst->used)
206 			dst->used = now;
207 		br_forward(dst->dst, skb, local_rcv, false);
208 	} else {
209 		if (!mcast_hit)
210 			br_flood(br, skb, pkt_type, local_rcv, false);
211 		else
212 			br_multicast_flood(mdst, skb, brmctx, local_rcv, false);
213 	}
214 
215 	if (local_rcv)
216 		return br_pass_frame_up(skb);
217 
218 out:
219 	return 0;
220 drop:
221 	kfree_skb(skb);
222 	goto out;
223 }
224 EXPORT_SYMBOL_GPL(br_handle_frame_finish);
225 
226 static void __br_handle_local_finish(struct sk_buff *skb)
227 {
228 	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
229 	u16 vid = 0;
230 
231 	/* check if vlan is allowed, to avoid spoofing */
232 	if ((p->flags & BR_LEARNING) &&
233 	    nbp_state_should_learn(p) &&
234 	    !br_opt_get(p->br, BROPT_NO_LL_LEARN) &&
235 	    br_should_learn(p, skb, &vid))
236 		br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, 0);
237 }
238 
239 /* note: already called with rcu_read_lock */
240 static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
241 {
242 	__br_handle_local_finish(skb);
243 
244 	/* return 1 to signal the okfn() was called so it's ok to use the skb */
245 	return 1;
246 }
247 
248 static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb)
249 {
250 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
251 	struct nf_hook_entries *e = NULL;
252 	struct nf_hook_state state;
253 	unsigned int verdict, i;
254 	struct net *net;
255 	int ret;
256 
257 	net = dev_net(skb->dev);
258 #ifdef HAVE_JUMP_LABEL
259 	if (!static_key_false(&nf_hooks_needed[NFPROTO_BRIDGE][NF_BR_PRE_ROUTING]))
260 		goto frame_finish;
261 #endif
262 
263 	e = rcu_dereference(net->nf.hooks_bridge[NF_BR_PRE_ROUTING]);
264 	if (!e)
265 		goto frame_finish;
266 
267 	nf_hook_state_init(&state, NF_BR_PRE_ROUTING,
268 			   NFPROTO_BRIDGE, skb->dev, NULL, NULL,
269 			   net, br_handle_frame_finish);
270 
271 	for (i = 0; i < e->num_hook_entries; i++) {
272 		verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state);
273 		switch (verdict & NF_VERDICT_MASK) {
274 		case NF_ACCEPT:
275 			if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) {
276 				*pskb = skb;
277 				return RX_HANDLER_PASS;
278 			}
279 			break;
280 		case NF_DROP:
281 			kfree_skb(skb);
282 			return RX_HANDLER_CONSUMED;
283 		case NF_QUEUE:
284 			ret = nf_queue(skb, &state, i, verdict);
285 			if (ret == 1)
286 				continue;
287 			return RX_HANDLER_CONSUMED;
288 		default: /* STOLEN */
289 			return RX_HANDLER_CONSUMED;
290 		}
291 	}
292 frame_finish:
293 	net = dev_net(skb->dev);
294 	br_handle_frame_finish(net, NULL, skb);
295 #else
296 	br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
297 #endif
298 	return RX_HANDLER_CONSUMED;
299 }
300 
301 /* Return 0 if the frame was not processed otherwise 1
302  * note: already called with rcu_read_lock
303  */
304 static int br_process_frame_type(struct net_bridge_port *p,
305 				 struct sk_buff *skb)
306 {
307 	struct br_frame_type *tmp;
308 
309 	hlist_for_each_entry_rcu(tmp, &p->br->frame_type_list, list)
310 		if (unlikely(tmp->type == skb->protocol))
311 			return tmp->frame_handler(p, skb);
312 
313 	return 0;
314 }
315 
316 /*
317  * Return NULL if skb is handled
318  * note: already called with rcu_read_lock
319  */
320 static rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
321 {
322 	struct net_bridge_port *p;
323 	struct sk_buff *skb = *pskb;
324 	const unsigned char *dest = eth_hdr(skb)->h_dest;
325 
326 	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
327 		return RX_HANDLER_PASS;
328 
329 	if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
330 		goto drop;
331 
332 	skb = skb_share_check(skb, GFP_ATOMIC);
333 	if (!skb)
334 		return RX_HANDLER_CONSUMED;
335 
336 	memset(skb->cb, 0, sizeof(struct br_input_skb_cb));
337 
338 	p = br_port_get_rcu(skb->dev);
339 	if (p->flags & BR_VLAN_TUNNEL)
340 		br_handle_ingress_vlan_tunnel(skb, p, nbp_vlan_group_rcu(p));
341 
342 	if (unlikely(is_link_local_ether_addr(dest))) {
343 		u16 fwd_mask = p->br->group_fwd_mask_required;
344 
345 		/*
346 		 * See IEEE 802.1D Table 7-10 Reserved addresses
347 		 *
348 		 * Assignment		 		Value
349 		 * Bridge Group Address		01-80-C2-00-00-00
350 		 * (MAC Control) 802.3		01-80-C2-00-00-01
351 		 * (Link Aggregation) 802.3	01-80-C2-00-00-02
352 		 * 802.1X PAE address		01-80-C2-00-00-03
353 		 *
354 		 * 802.1AB LLDP 		01-80-C2-00-00-0E
355 		 *
356 		 * Others reserved for future standardization
357 		 */
358 		fwd_mask |= p->group_fwd_mask;
359 		switch (dest[5]) {
360 		case 0x00:	/* Bridge Group Address */
361 			/* If STP is turned off,
362 			   then must forward to keep loop detection */
363 			if (p->br->stp_enabled == BR_NO_STP ||
364 			    fwd_mask & (1u << dest[5]))
365 				goto forward;
366 			*pskb = skb;
367 			__br_handle_local_finish(skb);
368 			return RX_HANDLER_PASS;
369 
370 		case 0x01:	/* IEEE MAC (Pause) */
371 			goto drop;
372 
373 		case 0x0E:	/* 802.1AB LLDP */
374 			fwd_mask |= p->br->group_fwd_mask;
375 			if (fwd_mask & (1u << dest[5]))
376 				goto forward;
377 			*pskb = skb;
378 			__br_handle_local_finish(skb);
379 			return RX_HANDLER_PASS;
380 
381 		default:
382 			/* Allow selective forwarding for most other protocols */
383 			fwd_mask |= p->br->group_fwd_mask;
384 			if (fwd_mask & (1u << dest[5]))
385 				goto forward;
386 		}
387 
388 		/* The else clause should be hit when nf_hook():
389 		 *   - returns < 0 (drop/error)
390 		 *   - returns = 0 (stolen/nf_queue)
391 		 * Thus return 1 from the okfn() to signal the skb is ok to pass
392 		 */
393 		if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
394 			    dev_net(skb->dev), NULL, skb, skb->dev, NULL,
395 			    br_handle_local_finish) == 1) {
396 			return RX_HANDLER_PASS;
397 		} else {
398 			return RX_HANDLER_CONSUMED;
399 		}
400 	}
401 
402 	if (unlikely(br_process_frame_type(p, skb)))
403 		return RX_HANDLER_PASS;
404 
405 forward:
406 	if (br_mst_is_enabled(p->br))
407 		goto defer_stp_filtering;
408 
409 	switch (p->state) {
410 	case BR_STATE_FORWARDING:
411 	case BR_STATE_LEARNING:
412 defer_stp_filtering:
413 		if (ether_addr_equal(p->br->dev->dev_addr, dest))
414 			skb->pkt_type = PACKET_HOST;
415 
416 		return nf_hook_bridge_pre(skb, pskb);
417 	default:
418 drop:
419 		kfree_skb(skb);
420 	}
421 	return RX_HANDLER_CONSUMED;
422 }
423 
424 /* This function has no purpose other than to appease the br_port_get_rcu/rtnl
425  * helpers which identify bridged ports according to the rx_handler installed
426  * on them (so there _needs_ to be a bridge rx_handler even if we don't need it
427  * to do anything useful). This bridge won't support traffic to/from the stack,
428  * but only hardware bridging. So return RX_HANDLER_PASS so we don't steal
429  * frames from the ETH_P_XDSA packet_type handler.
430  */
431 static rx_handler_result_t br_handle_frame_dummy(struct sk_buff **pskb)
432 {
433 	return RX_HANDLER_PASS;
434 }
435 
436 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev)
437 {
438 	if (netdev_uses_dsa(dev))
439 		return br_handle_frame_dummy;
440 
441 	return br_handle_frame;
442 }
443 
444 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft)
445 {
446 	hlist_add_head_rcu(&ft->list, &br->frame_type_list);
447 }
448 
449 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft)
450 {
451 	struct br_frame_type *tmp;
452 
453 	hlist_for_each_entry(tmp, &br->frame_type_list, list)
454 		if (ft == tmp) {
455 			hlist_del_rcu(&ft->list);
456 			return;
457 		}
458 }
459