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