br_input.c 8.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *	Handle incoming frames
 *	Linux ethernet bridge
 *
 *	Authors:
 *	Lennert Buytenhek		<buytenh@gnu.org>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

14
#include <linux/slab.h>
L
Linus Torvalds 已提交
15 16 17 18
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/netfilter_bridge.h>
19 20
#include <linux/neighbour.h>
#include <net/arp.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 28 29
/* Hook for brouter */
br_should_route_hook_t __rcu *br_should_route_hook __read_mostly;
EXPORT_SYMBOL(br_should_route_hook);

30 31
static int
br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
32
{
33
	br_drop_fake_rtable(skb);
34 35 36
	return netif_receive_skb(skb);
}

37
static int br_pass_frame_up(struct sk_buff *skb)
L
Linus Torvalds 已提交
38
{
39
	struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev;
40
	struct net_bridge *br = netdev_priv(brdev);
41
	struct net_bridge_vlan_group *vg;
42
	struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
L
Linus Torvalds 已提交
43

E
Eric Dumazet 已提交
44
	u64_stats_update_begin(&brstats->syncp);
45 46
	brstats->rx_packets++;
	brstats->rx_bytes += skb->len;
E
Eric Dumazet 已提交
47
	u64_stats_update_end(&brstats->syncp);
L
Linus Torvalds 已提交
48

49
	vg = br_vlan_group_rcu(br);
50 51 52 53 54
	/* 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) &&
55
	    !br_allowed_egress(vg, skb)) {
56 57 58 59
		kfree_skb(skb);
		return NET_RX_DROP;
	}

L
Linus Torvalds 已提交
60
	indev = skb->dev;
61
	skb->dev = brdev;
62
	skb = br_handle_vlan(br, NULL, vg, skb);
63 64
	if (!skb)
		return NET_RX_DROP;
65
	/* update the multicast stats if the packet is IGMP/MLD */
66
	br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb),
67
			   BR_MCAST_DIR_TX);
L
Linus Torvalds 已提交
68

69 70
	return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
		       dev_net(indev), NULL, skb, indev, NULL,
71
		       br_netif_receive_skb);
L
Linus Torvalds 已提交
72 73
}

74
/* note: already called with rcu_read_lock */
75
int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
L
Linus Torvalds 已提交
76
{
77
	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
78
	enum br_pkt_type pkt_type = BR_PKT_UNICAST;
79
	struct net_bridge_fdb_entry *dst = NULL;
80
	struct net_bridge_mdb_entry *mdst;
81
	bool local_rcv, mcast_hit = false;
82
	const unsigned char *dest;
83
	struct net_bridge *br;
84
	u16 vid = 0;
L
Linus Torvalds 已提交
85

86 87 88
	if (!p || p->state == BR_STATE_DISABLED)
		goto drop;

89
	if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid))
90
		goto out;
91

92 93
	nbp_switchdev_frame_mark(p, skb);

94
	/* insert into forwarding database after filtering to avoid spoofing */
95
	br = p->br;
96
	if (p->flags & BR_LEARNING)
97
		br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, false);
98

99
	local_rcv = !!(br->dev->flags & IFF_PROMISC);
100
	dest = eth_hdr(skb)->h_dest;
101 102 103 104 105 106 107 108 109 110 111
	if (is_multicast_ether_addr(dest)) {
		/* by definition the broadcast is also a multicast address */
		if (is_broadcast_ether_addr(dest)) {
			pkt_type = BR_PKT_BROADCAST;
			local_rcv = true;
		} else {
			pkt_type = BR_PKT_MULTICAST;
			if (br_multicast_rcv(br, p, skb, vid))
				goto drop;
		}
	}
112

113 114
	if (p->state == BR_STATE_LEARNING)
		goto drop;
115

116
	BR_INPUT_SKB_CB(skb)->brdev = br->dev;
117
	BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED);
118

119 120 121 122
	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);
123 124
	} else if (IS_ENABLED(CONFIG_IPV6) &&
		   skb->protocol == htons(ETH_P_IPV6) &&
125
		   br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
126 127 128 129 130 131 132 133
		   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);
134
	}
135

136 137
	switch (pkt_type) {
	case BR_PKT_MULTICAST:
138
		mdst = br_mdb_get(br, skb, vid);
139
		if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
140
		    br_multicast_querier_exists(br, eth_hdr(skb))) {
141
			if ((mdst && mdst->host_joined) ||
142
			    br_multicast_is_router(br)) {
143
				local_rcv = true;
144 145 146
				br->dev->stats.multicast++;
			}
			mcast_hit = true;
147
		} else {
148
			local_rcv = true;
149
			br->dev->stats.multicast++;
150
		}
151 152
		break;
	case BR_PKT_UNICAST:
153
		dst = br_fdb_find_rcu(br, dest, vid);
154 155
	default:
		break;
L
Linus Torvalds 已提交
156 157
	}

158
	if (dst) {
159 160
		unsigned long now = jiffies;

161 162 163
		if (dst->is_local)
			return br_pass_frame_up(skb);

164 165
		if (now != dst->used)
			dst->used = now;
166
		br_forward(dst->dst, skb, local_rcv, false);
167 168
	} else {
		if (!mcast_hit)
169
			br_flood(br, skb, pkt_type, local_rcv, false);
170
		else
171
			br_multicast_flood(mdst, skb, local_rcv, false);
172
	}
L
Linus Torvalds 已提交
173

174 175
	if (local_rcv)
		return br_pass_frame_up(skb);
176

L
Linus Torvalds 已提交
177 178
out:
	return 0;
179 180 181
drop:
	kfree_skb(skb);
	goto out;
L
Linus Torvalds 已提交
182
}
183
EXPORT_SYMBOL_GPL(br_handle_frame_finish);
L
Linus Torvalds 已提交
184

185
static void __br_handle_local_finish(struct sk_buff *skb)
186
{
187
	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
188
	u16 vid = 0;
189

190
	/* check if vlan is allowed, to avoid spoofing */
191 192 193
	if ((p->flags & BR_LEARNING) &&
	    !br_opt_get(p->br, BROPT_NO_LL_LEARN) &&
	    br_should_learn(p, skb, &vid))
194
		br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false);
195 196 197 198 199 200 201 202
}

/* note: already called with rcu_read_lock */
static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
{
	struct net_bridge_port *p = br_port_get_rcu(skb->dev);

	__br_handle_local_finish(skb);
203 204 205 206

	BR_INPUT_SKB_CB(skb)->brdev = p->br->dev;
	br_pass_frame_up(skb);
	return 0;
207 208
}

L
Linus Torvalds 已提交
209
/*
210
 * Return NULL if skb is handled
211
 * note: already called with rcu_read_lock
L
Linus Torvalds 已提交
212
 */
213
rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
L
Linus Torvalds 已提交
214
{
215
	struct net_bridge_port *p;
216
	struct sk_buff *skb = *pskb;
L
Linus Torvalds 已提交
217
	const unsigned char *dest = eth_hdr(skb)->h_dest;
218
	br_should_route_hook_t *rhook;
L
Linus Torvalds 已提交
219

220
	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
221
		return RX_HANDLER_PASS;
222

L
Linus Torvalds 已提交
223
	if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
224
		goto drop;
L
Linus Torvalds 已提交
225

H
Herbert Xu 已提交
226 227
	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
228
		return RX_HANDLER_CONSUMED;
H
Herbert Xu 已提交
229

230
	p = br_port_get_rcu(skb->dev);
231 232 233 234 235
	if (p->flags & BR_VLAN_TUNNEL) {
		if (br_handle_ingress_vlan_tunnel(skb, p,
						  nbp_vlan_group_rcu(p)))
			goto drop;
	}
236

237
	if (unlikely(is_link_local_ether_addr(dest))) {
238 239
		u16 fwd_mask = p->br->group_fwd_mask_required;

240 241 242 243 244 245 246 247 248 249 250 251 252
		/*
		 * 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
		 */
253
		fwd_mask |= p->group_fwd_mask;
254 255 256 257
		switch (dest[5]) {
		case 0x00:	/* Bridge Group Address */
			/* If STP is turned off,
			   then must forward to keep loop detection */
258 259
			if (p->br->stp_enabled == BR_NO_STP ||
			    fwd_mask & (1u << dest[5]))
260
				goto forward;
261 262 263
			*pskb = skb;
			__br_handle_local_finish(skb);
			return RX_HANDLER_PASS;
264 265

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

268 269 270 271 272 273 274 275
		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;

276 277
		default:
			/* Allow selective forwarding for most other protocols */
278 279
			fwd_mask |= p->br->group_fwd_mask;
			if (fwd_mask & (1u << dest[5]))
280 281
				goto forward;
		}
282

283
		/* Deliver packet to local host only */
284 285 286
		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, dev_net(skb->dev),
			NULL, skb, skb->dev, NULL, br_handle_local_finish);
		return RX_HANDLER_CONSUMED;
S
Stephen Hemminger 已提交
287
	}
L
Linus Torvalds 已提交
288

289
forward:
290 291
	switch (p->state) {
	case BR_STATE_FORWARDING:
292
		rhook = rcu_dereference(br_should_route_hook);
293
		if (rhook) {
294 295 296 297
			if ((*rhook)(skb)) {
				*pskb = skb;
				return RX_HANDLER_PASS;
			}
L
Linus Torvalds 已提交
298 299
			dest = eth_hdr(skb)->h_dest;
		}
300 301
		/* fall through */
	case BR_STATE_LEARNING:
302
		if (ether_addr_equal(p->br->dev->dev_addr, dest))
L
Linus Torvalds 已提交
303 304
			skb->pkt_type = PACKET_HOST;

305 306
		NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING,
			dev_net(skb->dev), NULL, skb, skb->dev, NULL,
L
Linus Torvalds 已提交
307
			br_handle_frame_finish);
308 309 310 311
		break;
	default:
drop:
		kfree_skb(skb);
L
Linus Torvalds 已提交
312
	}
313
	return RX_HANDLER_CONSUMED;
L
Linus Torvalds 已提交
314
}