br_input.c 7.5 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 117
	BR_INPUT_SKB_CB(skb)->brdev = br->dev;

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 125
	switch (pkt_type) {
	case BR_PKT_MULTICAST:
126
		mdst = br_mdb_get(br, skb, vid);
127
		if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
128
		    br_multicast_querier_exists(br, eth_hdr(skb))) {
129
			if ((mdst && mdst->mglist) ||
130
			    br_multicast_is_router(br)) {
131
				local_rcv = true;
132 133 134
				br->dev->stats.multicast++;
			}
			mcast_hit = true;
135
		} else {
136
			local_rcv = true;
137
			br->dev->stats.multicast++;
138
		}
139 140
		break;
	case BR_PKT_UNICAST:
141
		dst = br_fdb_find_rcu(br, dest, vid);
142 143
	default:
		break;
L
Linus Torvalds 已提交
144 145
	}

146
	if (dst) {
147 148
		unsigned long now = jiffies;

149 150 151
		if (dst->is_local)
			return br_pass_frame_up(skb);

152 153
		if (now != dst->used)
			dst->used = now;
154
		br_forward(dst->dst, skb, local_rcv, false);
155 156
	} else {
		if (!mcast_hit)
157
			br_flood(br, skb, pkt_type, local_rcv, false);
158
		else
159
			br_multicast_flood(mdst, skb, local_rcv, false);
160
	}
L
Linus Torvalds 已提交
161

162 163
	if (local_rcv)
		return br_pass_frame_up(skb);
164

L
Linus Torvalds 已提交
165 166
out:
	return 0;
167 168 169
drop:
	kfree_skb(skb);
	goto out;
L
Linus Torvalds 已提交
170
}
171
EXPORT_SYMBOL_GPL(br_handle_frame_finish);
L
Linus Torvalds 已提交
172

173
static void __br_handle_local_finish(struct sk_buff *skb)
174
{
175
	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
176
	u16 vid = 0;
177

178 179
	/* check if vlan is allowed, to avoid spoofing */
	if (p->flags & BR_LEARNING && br_should_learn(p, skb, &vid))
180
		br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false);
181 182 183 184 185 186 187 188
}

/* 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);
189 190 191 192

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

L
Linus Torvalds 已提交
195
/*
196
 * Return NULL if skb is handled
197
 * note: already called with rcu_read_lock
L
Linus Torvalds 已提交
198
 */
199
rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
L
Linus Torvalds 已提交
200
{
201
	struct net_bridge_port *p;
202
	struct sk_buff *skb = *pskb;
L
Linus Torvalds 已提交
203
	const unsigned char *dest = eth_hdr(skb)->h_dest;
204
	br_should_route_hook_t *rhook;
L
Linus Torvalds 已提交
205

206
	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
207
		return RX_HANDLER_PASS;
208

L
Linus Torvalds 已提交
209
	if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
210
		goto drop;
L
Linus Torvalds 已提交
211

H
Herbert Xu 已提交
212 213
	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
214
		return RX_HANDLER_CONSUMED;
H
Herbert Xu 已提交
215

216
	p = br_port_get_rcu(skb->dev);
217 218 219 220 221
	if (p->flags & BR_VLAN_TUNNEL) {
		if (br_handle_ingress_vlan_tunnel(skb, p,
						  nbp_vlan_group_rcu(p)))
			goto drop;
	}
222

223
	if (unlikely(is_link_local_ether_addr(dest))) {
224 225
		u16 fwd_mask = p->br->group_fwd_mask_required;

226 227 228 229 230 231 232 233 234 235 236 237 238
		/*
		 * 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
		 */
239
		fwd_mask |= p->group_fwd_mask;
240 241 242 243
		switch (dest[5]) {
		case 0x00:	/* Bridge Group Address */
			/* If STP is turned off,
			   then must forward to keep loop detection */
244 245
			if (p->br->stp_enabled == BR_NO_STP ||
			    fwd_mask & (1u << dest[5]))
246
				goto forward;
247 248 249
			*pskb = skb;
			__br_handle_local_finish(skb);
			return RX_HANDLER_PASS;
250 251

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

254 255 256 257 258 259 260 261
		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;

262 263
		default:
			/* Allow selective forwarding for most other protocols */
264 265
			fwd_mask |= p->br->group_fwd_mask;
			if (fwd_mask & (1u << dest[5]))
266 267
				goto forward;
		}
268

269
		/* Deliver packet to local host only */
270 271 272
		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 已提交
273
	}
L
Linus Torvalds 已提交
274

275
forward:
276 277
	switch (p->state) {
	case BR_STATE_FORWARDING:
278
		rhook = rcu_dereference(br_should_route_hook);
279
		if (rhook) {
280 281 282 283
			if ((*rhook)(skb)) {
				*pskb = skb;
				return RX_HANDLER_PASS;
			}
L
Linus Torvalds 已提交
284 285
			dest = eth_hdr(skb)->h_dest;
		}
286 287
		/* fall through */
	case BR_STATE_LEARNING:
288
		if (ether_addr_equal(p->br->dev->dev_addr, dest))
L
Linus Torvalds 已提交
289 290
			skb->pkt_type = PACKET_HOST;

291 292
		NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING,
			dev_net(skb->dev), NULL, skb, skb->dev, NULL,
L
Linus Torvalds 已提交
293
			br_handle_frame_finish);
294 295 296 297
		break;
	default:
drop:
		kfree_skb(skb);
L
Linus Torvalds 已提交
298
	}
299
	return RX_HANDLER_CONSUMED;
L
Linus Torvalds 已提交
300
}