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