• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *	Handle incoming frames
3  *	Linux ethernet bridge
4  *
5  *	Authors:
6  *	Lennert Buytenhek		<buytenh@gnu.org>
7  *
8  *	This program is free software; you can redistribute it and/or
9  *	modify it under the terms of the GNU General Public License
10  *	as published by the Free Software Foundation; either version
11  *	2 of the License, or (at your option) any later version.
12  */
13 
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/netfilter_bridge.h>
19 #include <linux/neighbour.h>
20 #include <net/arp.h>
21 #include <linux/export.h>
22 #include <linux/rculist.h>
23 #include "br_private.h"
24 
25 /* Hook for brouter */
26 br_should_route_hook_t __rcu *br_should_route_hook __read_mostly;
27 EXPORT_SYMBOL(br_should_route_hook);
28 
29 static int
br_netif_receive_skb(struct net * net,struct sock * sk,struct sk_buff * skb)30 br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
31 {
32 	br_drop_fake_rtable(skb);
33 	return netif_receive_skb(skb);
34 }
35 
br_pass_frame_up(struct sk_buff * skb)36 static int br_pass_frame_up(struct sk_buff *skb)
37 {
38 	struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev;
39 	struct net_bridge *br = netdev_priv(brdev);
40 	struct net_bridge_vlan_group *vg;
41 	struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
42 
43 	u64_stats_update_begin(&brstats->syncp);
44 	brstats->rx_packets++;
45 	brstats->rx_bytes += skb->len;
46 	u64_stats_update_end(&brstats->syncp);
47 
48 	vg = br_vlan_group_rcu(br);
49 	/* Bridge is just like any other port.  Make sure the
50 	 * packet is allowed except in promisc modue 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, 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 
br_do_proxy_arp(struct sk_buff * skb,struct net_bridge * br,u16 vid,struct net_bridge_port * p)73 static void br_do_proxy_arp(struct sk_buff *skb, struct net_bridge *br,
74 			    u16 vid, struct net_bridge_port *p)
75 {
76 	struct net_device *dev = br->dev;
77 	struct neighbour *n;
78 	struct arphdr *parp;
79 	u8 *arpptr, *sha;
80 	__be32 sip, tip;
81 
82 	BR_INPUT_SKB_CB(skb)->proxyarp_replied = false;
83 
84 	if ((dev->flags & IFF_NOARP) ||
85 	    !pskb_may_pull(skb, arp_hdr_len(dev)))
86 		return;
87 
88 	parp = arp_hdr(skb);
89 
90 	if (parp->ar_pro != htons(ETH_P_IP) ||
91 	    parp->ar_op != htons(ARPOP_REQUEST) ||
92 	    parp->ar_hln != dev->addr_len ||
93 	    parp->ar_pln != 4)
94 		return;
95 
96 	arpptr = (u8 *)parp + sizeof(struct arphdr);
97 	sha = arpptr;
98 	arpptr += dev->addr_len;	/* sha */
99 	memcpy(&sip, arpptr, sizeof(sip));
100 	arpptr += sizeof(sip);
101 	arpptr += dev->addr_len;	/* tha */
102 	memcpy(&tip, arpptr, sizeof(tip));
103 
104 	if (ipv4_is_loopback(tip) ||
105 	    ipv4_is_multicast(tip))
106 		return;
107 
108 	n = neigh_lookup(&arp_tbl, &tip, dev);
109 	if (n) {
110 		struct net_bridge_fdb_entry *f;
111 
112 		if (!(n->nud_state & NUD_VALID)) {
113 			neigh_release(n);
114 			return;
115 		}
116 
117 		f = __br_fdb_get(br, n->ha, vid);
118 		if (f && ((p->flags & BR_PROXYARP) ||
119 			  (f->dst && (f->dst->flags & BR_PROXYARP_WIFI)))) {
120 			arp_send(ARPOP_REPLY, ETH_P_ARP, sip, skb->dev, tip,
121 				 sha, n->ha, sha);
122 			BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
123 		}
124 
125 		neigh_release(n);
126 	}
127 }
128 
129 /* note: already called with rcu_read_lock */
br_handle_frame_finish(struct net * net,struct sock * sk,struct sk_buff * skb)130 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
131 {
132 	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
133 	const unsigned char *dest = eth_hdr(skb)->h_dest;
134 	enum br_pkt_type pkt_type = BR_PKT_UNICAST;
135 	struct net_bridge_fdb_entry *dst = NULL;
136 	struct net_bridge_mdb_entry *mdst;
137 	bool local_rcv, mcast_hit = false;
138 	struct net_bridge *br;
139 	u16 vid = 0;
140 
141 	if (!p || p->state == BR_STATE_DISABLED)
142 		goto drop;
143 
144 	if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid))
145 		goto out;
146 
147 	nbp_switchdev_frame_mark(p, skb);
148 
149 	/* insert into forwarding database after filtering to avoid spoofing */
150 	br = p->br;
151 	if (p->flags & BR_LEARNING)
152 		br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, false);
153 
154 	local_rcv = !!(br->dev->flags & IFF_PROMISC);
155 	if (is_multicast_ether_addr(dest)) {
156 		/* by definition the broadcast is also a multicast address */
157 		if (is_broadcast_ether_addr(dest)) {
158 			pkt_type = BR_PKT_BROADCAST;
159 			local_rcv = true;
160 		} else {
161 			pkt_type = BR_PKT_MULTICAST;
162 			if (br_multicast_rcv(br, p, skb, vid))
163 				goto drop;
164 		}
165 	}
166 
167 	if (p->state == BR_STATE_LEARNING)
168 		goto drop;
169 
170 	BR_INPUT_SKB_CB(skb)->brdev = br->dev;
171 
172 	if (IS_ENABLED(CONFIG_INET) && skb->protocol == htons(ETH_P_ARP))
173 		br_do_proxy_arp(skb, br, vid, p);
174 
175 	switch (pkt_type) {
176 	case BR_PKT_MULTICAST:
177 		mdst = br_mdb_get(br, skb, vid);
178 		if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
179 		    br_multicast_querier_exists(br, eth_hdr(skb))) {
180 			if ((mdst && mdst->mglist) ||
181 			    br_multicast_is_router(br)) {
182 				local_rcv = true;
183 				br->dev->stats.multicast++;
184 			}
185 			mcast_hit = true;
186 		} else {
187 			local_rcv = true;
188 			br->dev->stats.multicast++;
189 		}
190 		break;
191 	case BR_PKT_UNICAST:
192 		dst = __br_fdb_get(br, dest, vid);
193 	default:
194 		break;
195 	}
196 
197 	if (dst) {
198 		if (dst->is_local)
199 			return br_pass_frame_up(skb);
200 
201 		dst->used = jiffies;
202 		br_forward(dst->dst, skb, local_rcv, false);
203 	} else {
204 		if (!mcast_hit)
205 			br_flood(br, skb, pkt_type, local_rcv, false);
206 		else
207 			br_multicast_flood(mdst, skb, local_rcv, false);
208 	}
209 
210 	if (local_rcv)
211 		return br_pass_frame_up(skb);
212 
213 out:
214 	return 0;
215 drop:
216 	kfree_skb(skb);
217 	goto out;
218 }
219 EXPORT_SYMBOL_GPL(br_handle_frame_finish);
220 
__br_handle_local_finish(struct sk_buff * skb)221 static void __br_handle_local_finish(struct sk_buff *skb)
222 {
223 	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
224 	u16 vid = 0;
225 
226 	/* check if vlan is allowed, to avoid spoofing */
227 	if (p->flags & BR_LEARNING && br_should_learn(p, skb, &vid))
228 		br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false);
229 }
230 
231 /* note: already called with rcu_read_lock */
br_handle_local_finish(struct net * net,struct sock * sk,struct sk_buff * skb)232 static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
233 {
234 	struct net_bridge_port *p = br_port_get_rcu(skb->dev);
235 
236 	__br_handle_local_finish(skb);
237 
238 	BR_INPUT_SKB_CB(skb)->brdev = p->br->dev;
239 	br_pass_frame_up(skb);
240 	return 0;
241 }
242 
243 /*
244  * Return NULL if skb is handled
245  * note: already called with rcu_read_lock
246  */
br_handle_frame(struct sk_buff ** pskb)247 rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
248 {
249 	struct net_bridge_port *p;
250 	struct sk_buff *skb = *pskb;
251 	const unsigned char *dest = eth_hdr(skb)->h_dest;
252 	br_should_route_hook_t *rhook;
253 
254 	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
255 		return RX_HANDLER_PASS;
256 
257 	if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
258 		goto drop;
259 
260 	skb = skb_share_check(skb, GFP_ATOMIC);
261 	if (!skb)
262 		return RX_HANDLER_CONSUMED;
263 
264 	p = br_port_get_rcu(skb->dev);
265 
266 	if (unlikely(is_link_local_ether_addr(dest))) {
267 		u16 fwd_mask = p->br->group_fwd_mask_required;
268 
269 		/*
270 		 * See IEEE 802.1D Table 7-10 Reserved addresses
271 		 *
272 		 * Assignment		 		Value
273 		 * Bridge Group Address		01-80-C2-00-00-00
274 		 * (MAC Control) 802.3		01-80-C2-00-00-01
275 		 * (Link Aggregation) 802.3	01-80-C2-00-00-02
276 		 * 802.1X PAE address		01-80-C2-00-00-03
277 		 *
278 		 * 802.1AB LLDP 		01-80-C2-00-00-0E
279 		 *
280 		 * Others reserved for future standardization
281 		 */
282 		switch (dest[5]) {
283 		case 0x00:	/* Bridge Group Address */
284 			/* If STP is turned off,
285 			   then must forward to keep loop detection */
286 			if (p->br->stp_enabled == BR_NO_STP ||
287 			    fwd_mask & (1u << dest[5]))
288 				goto forward;
289 			*pskb = skb;
290 			__br_handle_local_finish(skb);
291 			return RX_HANDLER_PASS;
292 
293 		case 0x01:	/* IEEE MAC (Pause) */
294 			goto drop;
295 
296 		case 0x0E:	/* 802.1AB LLDP */
297 			fwd_mask |= p->br->group_fwd_mask;
298 			if (fwd_mask & (1u << dest[5]))
299 				goto forward;
300 			*pskb = skb;
301 			__br_handle_local_finish(skb);
302 			return RX_HANDLER_PASS;
303 
304 		default:
305 			/* Allow selective forwarding for most other protocols */
306 			fwd_mask |= p->br->group_fwd_mask;
307 			if (fwd_mask & (1u << dest[5]))
308 				goto forward;
309 		}
310 
311 		/* Deliver packet to local host only */
312 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, dev_net(skb->dev),
313 			NULL, skb, skb->dev, NULL, br_handle_local_finish);
314 		return RX_HANDLER_CONSUMED;
315 	}
316 
317 forward:
318 	switch (p->state) {
319 	case BR_STATE_FORWARDING:
320 		rhook = rcu_dereference(br_should_route_hook);
321 		if (rhook) {
322 			if ((*rhook)(skb)) {
323 				*pskb = skb;
324 				return RX_HANDLER_PASS;
325 			}
326 			dest = eth_hdr(skb)->h_dest;
327 		}
328 		/* fall through */
329 	case BR_STATE_LEARNING:
330 		if (ether_addr_equal(p->br->dev->dev_addr, dest))
331 			skb->pkt_type = PACKET_HOST;
332 
333 		NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING,
334 			dev_net(skb->dev), NULL, skb, skb->dev, NULL,
335 			br_handle_frame_finish);
336 		break;
337 	default:
338 drop:
339 		kfree_skb(skb);
340 	}
341 	return RX_HANDLER_CONSUMED;
342 }
343