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