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