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
br_netif_receive_skb(struct net * net,struct sock * sk,struct sk_buff * skb)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 
br_pass_frame_up(struct sk_buff * skb)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 */
br_handle_frame_finish(struct net * net,struct sock * sk,struct sk_buff * skb)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 				DEV_STATS_INC(br->dev, multicast);
185 			}
186 			mcast_hit = true;
187 		} else {
188 			local_rcv = true;
189 			DEV_STATS_INC(br->dev, 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, vid);
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 
__br_handle_local_finish(struct sk_buff * skb)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 */
br_handle_local_finish(struct net * net,struct sock * sk,struct sk_buff * skb)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 
nf_hook_bridge_pre(struct sk_buff * skb,struct sk_buff ** pskb)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  */
br_process_frame_type(struct net_bridge_port * p,struct sk_buff * skb)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  */
br_handle_frame(struct sk_buff ** pskb)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 	br_tc_skb_miss_set(skb, false);
338 
339 	p = br_port_get_rcu(skb->dev);
340 	if (p->flags & BR_VLAN_TUNNEL)
341 		br_handle_ingress_vlan_tunnel(skb, p, nbp_vlan_group_rcu(p));
342 
343 	if (unlikely(is_link_local_ether_addr(dest))) {
344 		u16 fwd_mask = p->br->group_fwd_mask_required;
345 
346 		/*
347 		 * See IEEE 802.1D Table 7-10 Reserved addresses
348 		 *
349 		 * Assignment		 		Value
350 		 * Bridge Group Address		01-80-C2-00-00-00
351 		 * (MAC Control) 802.3		01-80-C2-00-00-01
352 		 * (Link Aggregation) 802.3	01-80-C2-00-00-02
353 		 * 802.1X PAE address		01-80-C2-00-00-03
354 		 *
355 		 * 802.1AB LLDP 		01-80-C2-00-00-0E
356 		 *
357 		 * Others reserved for future standardization
358 		 */
359 		fwd_mask |= p->group_fwd_mask;
360 		switch (dest[5]) {
361 		case 0x00:	/* Bridge Group Address */
362 			/* If STP is turned off,
363 			   then must forward to keep loop detection */
364 			if (p->br->stp_enabled == BR_NO_STP ||
365 			    fwd_mask & (1u << dest[5]))
366 				goto forward;
367 			*pskb = skb;
368 			__br_handle_local_finish(skb);
369 			return RX_HANDLER_PASS;
370 
371 		case 0x01:	/* IEEE MAC (Pause) */
372 			goto drop;
373 
374 		case 0x0E:	/* 802.1AB LLDP */
375 			fwd_mask |= p->br->group_fwd_mask;
376 			if (fwd_mask & (1u << dest[5]))
377 				goto forward;
378 			*pskb = skb;
379 			__br_handle_local_finish(skb);
380 			return RX_HANDLER_PASS;
381 
382 		default:
383 			/* Allow selective forwarding for most other protocols */
384 			fwd_mask |= p->br->group_fwd_mask;
385 			if (fwd_mask & (1u << dest[5]))
386 				goto forward;
387 		}
388 
389 		/* The else clause should be hit when nf_hook():
390 		 *   - returns < 0 (drop/error)
391 		 *   - returns = 0 (stolen/nf_queue)
392 		 * Thus return 1 from the okfn() to signal the skb is ok to pass
393 		 */
394 		if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
395 			    dev_net(skb->dev), NULL, skb, skb->dev, NULL,
396 			    br_handle_local_finish) == 1) {
397 			return RX_HANDLER_PASS;
398 		} else {
399 			return RX_HANDLER_CONSUMED;
400 		}
401 	}
402 
403 	if (unlikely(br_process_frame_type(p, skb)))
404 		return RX_HANDLER_PASS;
405 
406 forward:
407 	if (br_mst_is_enabled(p->br))
408 		goto defer_stp_filtering;
409 
410 	switch (p->state) {
411 	case BR_STATE_FORWARDING:
412 	case BR_STATE_LEARNING:
413 defer_stp_filtering:
414 		if (ether_addr_equal(p->br->dev->dev_addr, dest))
415 			skb->pkt_type = PACKET_HOST;
416 
417 		return nf_hook_bridge_pre(skb, pskb);
418 	default:
419 drop:
420 		kfree_skb(skb);
421 	}
422 	return RX_HANDLER_CONSUMED;
423 }
424 
425 /* This function has no purpose other than to appease the br_port_get_rcu/rtnl
426  * helpers which identify bridged ports according to the rx_handler installed
427  * on them (so there _needs_ to be a bridge rx_handler even if we don't need it
428  * to do anything useful). This bridge won't support traffic to/from the stack,
429  * but only hardware bridging. So return RX_HANDLER_PASS so we don't steal
430  * frames from the ETH_P_XDSA packet_type handler.
431  */
br_handle_frame_dummy(struct sk_buff ** pskb)432 static rx_handler_result_t br_handle_frame_dummy(struct sk_buff **pskb)
433 {
434 	return RX_HANDLER_PASS;
435 }
436 
br_get_rx_handler(const struct net_device * dev)437 rx_handler_func_t *br_get_rx_handler(const struct net_device *dev)
438 {
439 	if (netdev_uses_dsa(dev))
440 		return br_handle_frame_dummy;
441 
442 	return br_handle_frame;
443 }
444 
br_add_frame(struct net_bridge * br,struct br_frame_type * ft)445 void br_add_frame(struct net_bridge *br, struct br_frame_type *ft)
446 {
447 	hlist_add_head_rcu(&ft->list, &br->frame_type_list);
448 }
449 
br_del_frame(struct net_bridge * br,struct br_frame_type * ft)450 void br_del_frame(struct net_bridge *br, struct br_frame_type *ft)
451 {
452 	struct br_frame_type *tmp;
453 
454 	hlist_for_each_entry(tmp, &br->frame_type_list, list)
455 		if (ft == tmp) {
456 			hlist_del_rcu(&ft->list);
457 			return;
458 		}
459 }
460