br_input.c 9.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9
/*
 *	Handle incoming frames
 *	Linux ethernet bridge
 *
 *	Authors:
 *	Lennert Buytenhek		<buytenh@gnu.org>
 */

10
#include <linux/slab.h>
L
Linus Torvalds 已提交
11 12 13 14
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/netfilter_bridge.h>
15
#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
16
#include <net/netfilter/nf_queue.h>
17
#endif
18 19
#include <linux/neighbour.h>
#include <net/arp.h>
20
#include <net/dsa.h>
21
#include <linux/export.h>
22
#include <linux/rculist.h>
L
Linus Torvalds 已提交
23
#include "br_private.h"
24
#include "br_private_tunnel.h"
L
Linus Torvalds 已提交
25

26 27
static int
br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
28
{
29
	br_drop_fake_rtable(skb);
30 31 32
	return netif_receive_skb(skb);
}

33
static int br_pass_frame_up(struct sk_buff *skb)
L
Linus Torvalds 已提交
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);
L
Linus Torvalds 已提交
39

E
Eric Dumazet 已提交
40
	u64_stats_update_begin(&brstats->syncp);
41 42
	brstats->rx_packets++;
	brstats->rx_bytes += skb->len;
E
Eric Dumazet 已提交
43
	u64_stats_update_end(&brstats->syncp);
L
Linus Torvalds 已提交
44

45
	vg = br_vlan_group_rcu(br);
46 47 48 49 50
	/* Bridge is just like any other port.  Make sure the
	 * packet is allowed except in promisc modue when someone
	 * may be running packet capture.
	 */
	if (!(brdev->flags & IFF_PROMISC) &&
51
	    !br_allowed_egress(vg, skb)) {
52 53 54 55
		kfree_skb(skb);
		return NET_RX_DROP;
	}

L
Linus Torvalds 已提交
56
	indev = skb->dev;
57
	skb->dev = brdev;
58
	skb = br_handle_vlan(br, NULL, vg, skb);
59 60
	if (!skb)
		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);
L
Linus Torvalds 已提交
64

65 66
	return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
		       dev_net(indev), NULL, skb, indev, NULL,
67
		       br_netif_receive_skb);
L
Linus Torvalds 已提交
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)
L
Linus Torvalds 已提交
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;
L
Linus Torvalds 已提交
81

82 83 84
	if (!p || p->state == BR_STATE_DISABLED)
		goto drop;

85 86 87
	state = p->state;
	if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid,
				&state))
88
		goto out;
89

90 91
	nbp_switchdev_frame_mark(p, skb);

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 102 103 104 105 106 107 108
			pkt_type = BR_PKT_BROADCAST;
			local_rcv = true;
		} else {
			pkt_type = BR_PKT_MULTICAST;
			if (br_multicast_rcv(br, p, skb, vid))
				goto drop;
		}
	}
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 117 118 119
	if (IS_ENABLED(CONFIG_INET) &&
	    (skb->protocol == htons(ETH_P_ARP) ||
	     skb->protocol == htons(ETH_P_RARP))) {
		br_do_proxy_suppress_arp(skb, br, vid, p);
120 121
	} else if (IS_ENABLED(CONFIG_IPV6) &&
		   skb->protocol == htons(ETH_P_IPV6) &&
122
		   br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
123 124 125 126 127 128 129 130
		   pskb_may_pull(skb, sizeof(struct ipv6hdr) +
				 sizeof(struct nd_msg)) &&
		   ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
			struct nd_msg *msg, _msg;

			msg = br_is_nd_neigh_msg(skb, &_msg);
			if (msg)
				br_do_suppress_nd(skb, br, vid, p, msg);
131
	}
132

133 134
	switch (pkt_type) {
	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 142 143
				br->dev->stats.multicast++;
			}
			mcast_hit = true;
144
		} else {
145
			local_rcv = true;
146
			br->dev->stats.multicast++;
147
		}
148 149
		break;
	case BR_PKT_UNICAST:
150
		dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid);
151 152
	default:
		break;
L
Linus Torvalds 已提交
153 154
	}

155
	if (dst) {
156 157
		unsigned long now = jiffies;

158
		if (test_bit(BR_FDB_LOCAL, &dst->flags))
159 160
			return br_pass_frame_up(skb);

161 162
		if (now != dst->used)
			dst->used = now;
163
		br_forward(dst->dst, skb, local_rcv, false);
164 165
	} else {
		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
	}
L
Linus Torvalds 已提交
170

171 172
	if (local_rcv)
		return br_pass_frame_up(skb);
173

L
Linus Torvalds 已提交
174 175
out:
	return 0;
176 177 178
drop:
	kfree_skb(skb);
	goto out;
L
Linus Torvalds 已提交
179
}
180
EXPORT_SYMBOL_GPL(br_handle_frame_finish);
L
Linus Torvalds 已提交
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 191
	    !br_opt_get(p->br, BROPT_NO_LL_LEARN) &&
	    br_should_learn(p, skb, &vid))
192
		br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, 0);
193 194 195 196 197 198
}

/* note: already called with rcu_read_lock */
static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
{
	__br_handle_local_finish(skb);
199

200 201
	/* return 1 to signal the okfn() was called so it's ok to use the skb */
	return 1;
202 203
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb)
{
#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
	struct nf_hook_entries *e = NULL;
	struct nf_hook_state state;
	unsigned int verdict, i;
	struct net *net;
	int ret;

	net = dev_net(skb->dev);
#ifdef HAVE_JUMP_LABEL
	if (!static_key_false(&nf_hooks_needed[NFPROTO_BRIDGE][NF_BR_PRE_ROUTING]))
		goto frame_finish;
#endif

	e = rcu_dereference(net->nf.hooks_bridge[NF_BR_PRE_ROUTING]);
	if (!e)
		goto frame_finish;

	nf_hook_state_init(&state, NF_BR_PRE_ROUTING,
			   NFPROTO_BRIDGE, skb->dev, NULL, NULL,
			   net, br_handle_frame_finish);

	for (i = 0; i < e->num_hook_entries; i++) {
		verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state);
		switch (verdict & NF_VERDICT_MASK) {
		case NF_ACCEPT:
231 232 233 234
			if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) {
				*pskb = skb;
				return RX_HANDLER_PASS;
			}
235 236 237 238 239
			break;
		case NF_DROP:
			kfree_skb(skb);
			return RX_HANDLER_CONSUMED;
		case NF_QUEUE:
240
			ret = nf_queue(skb, &state, i, verdict);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
			if (ret == 1)
				continue;
			return RX_HANDLER_CONSUMED;
		default: /* STOLEN */
			return RX_HANDLER_CONSUMED;
		}
	}
frame_finish:
	net = dev_net(skb->dev);
	br_handle_frame_finish(net, NULL, skb);
#else
	br_handle_frame_finish(dev_net(skb->dev), NULL, skb);
#endif
	return RX_HANDLER_CONSUMED;
}

L
Linus Torvalds 已提交
257
/*
258
 * Return NULL if skb is handled
259
 * note: already called with rcu_read_lock
L
Linus Torvalds 已提交
260
 */
261
static rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
L
Linus Torvalds 已提交
262
{
263
	struct net_bridge_port *p;
264
	struct sk_buff *skb = *pskb;
L
Linus Torvalds 已提交
265 266
	const unsigned char *dest = eth_hdr(skb)->h_dest;

267
	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
268
		return RX_HANDLER_PASS;
269

L
Linus Torvalds 已提交
270
	if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
271
		goto drop;
L
Linus Torvalds 已提交
272

H
Herbert Xu 已提交
273 274
	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
275
		return RX_HANDLER_CONSUMED;
H
Herbert Xu 已提交
276

277 278
	memset(skb->cb, 0, sizeof(struct br_input_skb_cb));

279
	p = br_port_get_rcu(skb->dev);
280 281 282 283 284
	if (p->flags & BR_VLAN_TUNNEL) {
		if (br_handle_ingress_vlan_tunnel(skb, p,
						  nbp_vlan_group_rcu(p)))
			goto drop;
	}
285

286
	if (unlikely(is_link_local_ether_addr(dest))) {
287 288
		u16 fwd_mask = p->br->group_fwd_mask_required;

289 290 291 292 293 294 295 296 297 298 299 300 301
		/*
		 * See IEEE 802.1D Table 7-10 Reserved addresses
		 *
		 * Assignment		 		Value
		 * Bridge Group Address		01-80-C2-00-00-00
		 * (MAC Control) 802.3		01-80-C2-00-00-01
		 * (Link Aggregation) 802.3	01-80-C2-00-00-02
		 * 802.1X PAE address		01-80-C2-00-00-03
		 *
		 * 802.1AB LLDP 		01-80-C2-00-00-0E
		 *
		 * Others reserved for future standardization
		 */
302
		fwd_mask |= p->group_fwd_mask;
303 304 305 306
		switch (dest[5]) {
		case 0x00:	/* Bridge Group Address */
			/* If STP is turned off,
			   then must forward to keep loop detection */
307 308
			if (p->br->stp_enabled == BR_NO_STP ||
			    fwd_mask & (1u << dest[5]))
309
				goto forward;
310 311 312
			*pskb = skb;
			__br_handle_local_finish(skb);
			return RX_HANDLER_PASS;
313 314

		case 0x01:	/* IEEE MAC (Pause) */
S
Stephen Hemminger 已提交
315 316
			goto drop;

317 318 319 320 321 322 323 324
		case 0x0E:	/* 802.1AB LLDP */
			fwd_mask |= p->br->group_fwd_mask;
			if (fwd_mask & (1u << dest[5]))
				goto forward;
			*pskb = skb;
			__br_handle_local_finish(skb);
			return RX_HANDLER_PASS;

325 326
		default:
			/* Allow selective forwarding for most other protocols */
327 328
			fwd_mask |= p->br->group_fwd_mask;
			if (fwd_mask & (1u << dest[5]))
329 330
				goto forward;
		}
331

332 333 334 335 336 337 338 339 340 341 342 343
		/* The else clause should be hit when nf_hook():
		 *   - returns < 0 (drop/error)
		 *   - returns = 0 (stolen/nf_queue)
		 * Thus return 1 from the okfn() to signal the skb is ok to pass
		 */
		if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
			    dev_net(skb->dev), NULL, skb, skb->dev, NULL,
			    br_handle_local_finish) == 1) {
			return RX_HANDLER_PASS;
		} else {
			return RX_HANDLER_CONSUMED;
		}
S
Stephen Hemminger 已提交
344
	}
L
Linus Torvalds 已提交
345

346 347 348
	if (unlikely(br_mrp_process(p, skb)))
		return RX_HANDLER_PASS;

349
forward:
350 351 352
	switch (p->state) {
	case BR_STATE_FORWARDING:
	case BR_STATE_LEARNING:
353
		if (ether_addr_equal(p->br->dev->dev_addr, dest))
L
Linus Torvalds 已提交
354 355
			skb->pkt_type = PACKET_HOST;

356
		return nf_hook_bridge_pre(skb, pskb);
357 358 359
	default:
drop:
		kfree_skb(skb);
L
Linus Torvalds 已提交
360
	}
361
	return RX_HANDLER_CONSUMED;
L
Linus Torvalds 已提交
362
}
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

/* This function has no purpose other than to appease the br_port_get_rcu/rtnl
 * helpers which identify bridged ports according to the rx_handler installed
 * on them (so there _needs_ to be a bridge rx_handler even if we don't need it
 * to do anything useful). This bridge won't support traffic to/from the stack,
 * but only hardware bridging. So return RX_HANDLER_PASS so we don't steal
 * frames from the ETH_P_XDSA packet_type handler.
 */
static rx_handler_result_t br_handle_frame_dummy(struct sk_buff **pskb)
{
	return RX_HANDLER_PASS;
}

rx_handler_func_t *br_get_rx_handler(const struct net_device *dev)
{
	if (netdev_uses_dsa(dev))
		return br_handle_frame_dummy;

	return br_handle_frame;
}