Linux 4.19.133
[linux/fpc-iii.git] / net / bridge / br_input.c
blob2532c1a19645c7b13623df9f0e9581e29774919e
1 /*
2 * Handle incoming frames
3 * Linux ethernet bridge
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
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.
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 #include "br_private_tunnel.h"
26 /* Hook for brouter */
27 br_should_route_hook_t __rcu *br_should_route_hook __read_mostly;
28 EXPORT_SYMBOL(br_should_route_hook);
30 static int
31 br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb)
33 br_drop_fake_rtable(skb);
34 return netif_receive_skb(skb);
37 static int br_pass_frame_up(struct sk_buff *skb)
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);
44 u64_stats_update_begin(&brstats->syncp);
45 brstats->rx_packets++;
46 brstats->rx_bytes += skb->len;
47 u64_stats_update_end(&brstats->syncp);
49 vg = br_vlan_group_rcu(br);
50 /* Bridge is just like any other port. Make sure the
51 * packet is allowed except in promisc modue when someone
52 * may be running packet capture.
54 if (!(brdev->flags & IFF_PROMISC) &&
55 !br_allowed_egress(vg, skb)) {
56 kfree_skb(skb);
57 return NET_RX_DROP;
60 indev = skb->dev;
61 skb->dev = brdev;
62 skb = br_handle_vlan(br, NULL, vg, skb);
63 if (!skb)
64 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);
69 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
70 dev_net(indev), NULL, skb, indev, NULL,
71 br_netif_receive_skb);
74 /* note: already called with rcu_read_lock */
75 int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
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 struct net_bridge *br;
83 u16 vid = 0;
85 if (!p || p->state == BR_STATE_DISABLED)
86 goto drop;
88 if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid))
89 goto out;
91 nbp_switchdev_frame_mark(p, skb);
93 /* insert into forwarding database after filtering to avoid spoofing */
94 br = p->br;
95 if (p->flags & BR_LEARNING)
96 br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, false);
98 local_rcv = !!(br->dev->flags & IFF_PROMISC);
99 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) {
100 /* by definition the broadcast is also a multicast address */
101 if (is_broadcast_ether_addr(eth_hdr(skb)->h_dest)) {
102 pkt_type = BR_PKT_BROADCAST;
103 local_rcv = true;
104 } else {
105 pkt_type = BR_PKT_MULTICAST;
106 if (br_multicast_rcv(br, p, skb, vid))
107 goto drop;
111 if (p->state == BR_STATE_LEARNING)
112 goto drop;
114 BR_INPUT_SKB_CB(skb)->brdev = br->dev;
115 BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED);
117 if (IS_ENABLED(CONFIG_INET) &&
118 (skb->protocol == htons(ETH_P_ARP) ||
119 skb->protocol == htons(ETH_P_RARP))) {
120 br_do_proxy_suppress_arp(skb, br, vid, p);
121 } else if (IS_ENABLED(CONFIG_IPV6) &&
122 skb->protocol == htons(ETH_P_IPV6) &&
123 br->neigh_suppress_enabled &&
124 pskb_may_pull(skb, sizeof(struct ipv6hdr) +
125 sizeof(struct nd_msg)) &&
126 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
127 struct nd_msg *msg, _msg;
129 msg = br_is_nd_neigh_msg(skb, &_msg);
130 if (msg)
131 br_do_suppress_nd(skb, br, vid, p, msg);
134 switch (pkt_type) {
135 case BR_PKT_MULTICAST:
136 mdst = br_mdb_get(br, skb, vid);
137 if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
138 br_multicast_querier_exists(br, eth_hdr(skb))) {
139 if ((mdst && mdst->host_joined) ||
140 br_multicast_is_router(br)) {
141 local_rcv = true;
142 br->dev->stats.multicast++;
144 mcast_hit = true;
145 } else {
146 local_rcv = true;
147 br->dev->stats.multicast++;
149 break;
150 case BR_PKT_UNICAST:
151 dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid);
152 default:
153 break;
156 if (dst) {
157 unsigned long now = jiffies;
159 if (dst->is_local)
160 return br_pass_frame_up(skb);
162 if (now != dst->used)
163 dst->used = now;
164 br_forward(dst->dst, skb, local_rcv, false);
165 } else {
166 if (!mcast_hit)
167 br_flood(br, skb, pkt_type, local_rcv, false);
168 else
169 br_multicast_flood(mdst, skb, local_rcv, false);
172 if (local_rcv)
173 return br_pass_frame_up(skb);
175 out:
176 return 0;
177 drop:
178 kfree_skb(skb);
179 goto out;
181 EXPORT_SYMBOL_GPL(br_handle_frame_finish);
183 static void __br_handle_local_finish(struct sk_buff *skb)
185 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
186 u16 vid = 0;
188 /* check if vlan is allowed, to avoid spoofing */
189 if (p->flags & BR_LEARNING && br_should_learn(p, skb, &vid))
190 br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, false);
193 /* note: already called with rcu_read_lock */
194 static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
196 __br_handle_local_finish(skb);
198 /* return 1 to signal the okfn() was called so it's ok to use the skb */
199 return 1;
203 * Return NULL if skb is handled
204 * note: already called with rcu_read_lock
206 rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
208 struct net_bridge_port *p;
209 struct sk_buff *skb = *pskb;
210 const unsigned char *dest = eth_hdr(skb)->h_dest;
211 br_should_route_hook_t *rhook;
213 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
214 return RX_HANDLER_PASS;
216 if (!is_valid_ether_addr(eth_hdr(skb)->h_source))
217 goto drop;
219 skb = skb_share_check(skb, GFP_ATOMIC);
220 if (!skb)
221 return RX_HANDLER_CONSUMED;
223 p = br_port_get_rcu(skb->dev);
224 if (p->flags & BR_VLAN_TUNNEL) {
225 if (br_handle_ingress_vlan_tunnel(skb, p,
226 nbp_vlan_group_rcu(p)))
227 goto drop;
230 if (unlikely(is_link_local_ether_addr(dest))) {
231 u16 fwd_mask = p->br->group_fwd_mask_required;
234 * See IEEE 802.1D Table 7-10 Reserved addresses
236 * Assignment Value
237 * Bridge Group Address 01-80-C2-00-00-00
238 * (MAC Control) 802.3 01-80-C2-00-00-01
239 * (Link Aggregation) 802.3 01-80-C2-00-00-02
240 * 802.1X PAE address 01-80-C2-00-00-03
242 * 802.1AB LLDP 01-80-C2-00-00-0E
244 * Others reserved for future standardization
246 fwd_mask |= p->group_fwd_mask;
247 switch (dest[5]) {
248 case 0x00: /* Bridge Group Address */
249 /* If STP is turned off,
250 then must forward to keep loop detection */
251 if (p->br->stp_enabled == BR_NO_STP ||
252 fwd_mask & (1u << dest[5]))
253 goto forward;
254 *pskb = skb;
255 __br_handle_local_finish(skb);
256 return RX_HANDLER_PASS;
258 case 0x01: /* IEEE MAC (Pause) */
259 goto drop;
261 case 0x0E: /* 802.1AB LLDP */
262 fwd_mask |= p->br->group_fwd_mask;
263 if (fwd_mask & (1u << dest[5]))
264 goto forward;
265 *pskb = skb;
266 __br_handle_local_finish(skb);
267 return RX_HANDLER_PASS;
269 default:
270 /* Allow selective forwarding for most other protocols */
271 fwd_mask |= p->br->group_fwd_mask;
272 if (fwd_mask & (1u << dest[5]))
273 goto forward;
276 /* The else clause should be hit when nf_hook():
277 * - returns < 0 (drop/error)
278 * - returns = 0 (stolen/nf_queue)
279 * Thus return 1 from the okfn() to signal the skb is ok to pass
281 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN,
282 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
283 br_handle_local_finish) == 1) {
284 return RX_HANDLER_PASS;
285 } else {
286 return RX_HANDLER_CONSUMED;
290 forward:
291 switch (p->state) {
292 case BR_STATE_FORWARDING:
293 rhook = rcu_dereference(br_should_route_hook);
294 if (rhook) {
295 if ((*rhook)(skb)) {
296 *pskb = skb;
297 return RX_HANDLER_PASS;
299 dest = eth_hdr(skb)->h_dest;
301 /* fall through */
302 case BR_STATE_LEARNING:
303 if (ether_addr_equal(p->br->dev->dev_addr, dest))
304 skb->pkt_type = PACKET_HOST;
306 NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING,
307 dev_net(skb->dev), NULL, skb, skb->dev, NULL,
308 br_handle_frame_finish);
309 break;
310 default:
311 drop:
312 kfree_skb(skb);
314 return RX_HANDLER_CONSUMED;