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