Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Handle incoming frames | |
4 | * Linux ethernet bridge | |
5 | * | |
6 | * Authors: | |
7 | * Lennert Buytenhek <buytenh@gnu.org> | |
1da177e4 LT |
8 | */ |
9 | ||
5a0e3ad6 | 10 | #include <linux/slab.h> |
1da177e4 LT |
11 | #include <linux/kernel.h> |
12 | #include <linux/netdevice.h> | |
13 | #include <linux/etherdevice.h> | |
14 | #include <linux/netfilter_bridge.h> | |
dc2f4189 | 15 | #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE |
971502d7 | 16 | #include <net/netfilter/nf_queue.h> |
dc2f4189 | 17 | #endif |
95850116 KP |
18 | #include <linux/neighbour.h> |
19 | #include <net/arp.h> | |
bc3b2d7f | 20 | #include <linux/export.h> |
a37b85c9 | 21 | #include <linux/rculist.h> |
1da177e4 | 22 | #include "br_private.h" |
11538d03 | 23 | #include "br_private_tunnel.h" |
1da177e4 | 24 | |
0c4b51f0 EB |
25 | static int |
26 | br_netif_receive_skb(struct net *net, struct sock *sk, struct sk_buff *skb) | |
04eb4489 | 27 | { |
a13b2082 | 28 | br_drop_fake_rtable(skb); |
04eb4489 EB |
29 | return netif_receive_skb(skb); |
30 | } | |
31 | ||
68b7c895 | 32 | static int br_pass_frame_up(struct sk_buff *skb) |
1da177e4 | 33 | { |
68b7c895 | 34 | struct net_device *indev, *brdev = BR_INPUT_SKB_CB(skb)->brdev; |
14bb4789 | 35 | struct net_bridge *br = netdev_priv(brdev); |
2594e906 | 36 | struct net_bridge_vlan_group *vg; |
8f84985f | 37 | struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats); |
1da177e4 | 38 | |
406818ff | 39 | u64_stats_update_begin(&brstats->syncp); |
14bb4789 | 40 | brstats->rx_packets++; |
41 | brstats->rx_bytes += skb->len; | |
406818ff | 42 | u64_stats_update_end(&brstats->syncp); |
1da177e4 | 43 | |
907b1e6e | 44 | vg = br_vlan_group_rcu(br); |
85f46c6b VY |
45 | /* Bridge is just like any other port. Make sure the |
46 | * packet is allowed except in promisc modue when someone | |
47 | * may be running packet capture. | |
48 | */ | |
49 | if (!(brdev->flags & IFF_PROMISC) && | |
2594e906 | 50 | !br_allowed_egress(vg, skb)) { |
85f46c6b VY |
51 | kfree_skb(skb); |
52 | return NET_RX_DROP; | |
53 | } | |
54 | ||
1da177e4 | 55 | indev = skb->dev; |
a339f1c8 | 56 | skb->dev = brdev; |
11538d03 | 57 | skb = br_handle_vlan(br, NULL, vg, skb); |
fc92f745 VY |
58 | if (!skb) |
59 | return NET_RX_DROP; | |
1080ab95 | 60 | /* update the multicast stats if the packet is IGMP/MLD */ |
a65056ec | 61 | br_multicast_count(br, NULL, skb, br_multicast_igmp_type(skb), |
1080ab95 | 62 | BR_MCAST_DIR_TX); |
1da177e4 | 63 | |
29a26a56 EB |
64 | return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, |
65 | dev_net(indev), NULL, skb, indev, NULL, | |
04eb4489 | 66 | br_netif_receive_skb); |
1da177e4 LT |
67 | } |
68 | ||
eeaf61d8 | 69 | /* note: already called with rcu_read_lock */ |
0c4b51f0 | 70 | int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
1da177e4 | 71 | { |
f350a0a8 | 72 | struct net_bridge_port *p = br_port_get_rcu(skb->dev); |
8addd5e7 | 73 | enum br_pkt_type pkt_type = BR_PKT_UNICAST; |
46c0772d | 74 | struct net_bridge_fdb_entry *dst = NULL; |
c4fcb78c | 75 | struct net_bridge_mdb_entry *mdst; |
8addd5e7 | 76 | bool local_rcv, mcast_hit = false; |
46c0772d | 77 | struct net_bridge *br; |
78851988 | 78 | u16 vid = 0; |
1da177e4 | 79 | |
b3f1be4b SH |
80 | if (!p || p->state == BR_STATE_DISABLED) |
81 | goto drop; | |
82 | ||
907b1e6e | 83 | if (!br_allowed_ingress(p->br, nbp_vlan_group_rcu(p), skb, &vid)) |
eb707618 | 84 | goto out; |
a37b85c9 | 85 | |
6bc506b4 IS |
86 | nbp_switchdev_frame_mark(p, skb); |
87 | ||
85967bb4 | 88 | /* insert into forwarding database after filtering to avoid spoofing */ |
b3f1be4b | 89 | br = p->br; |
9ba18891 | 90 | if (p->flags & BR_LEARNING) |
be0c5677 | 91 | br_fdb_update(br, p, eth_hdr(skb)->h_source, vid, 0); |
85967bb4 | 92 | |
8addd5e7 | 93 | local_rcv = !!(br->dev->flags & IFF_PROMISC); |
3d26eb8a | 94 | if (is_multicast_ether_addr(eth_hdr(skb)->h_dest)) { |
8addd5e7 | 95 | /* by definition the broadcast is also a multicast address */ |
3d26eb8a | 96 | if (is_broadcast_ether_addr(eth_hdr(skb)->h_dest)) { |
8addd5e7 NA |
97 | pkt_type = BR_PKT_BROADCAST; |
98 | local_rcv = true; | |
99 | } else { | |
100 | pkt_type = BR_PKT_MULTICAST; | |
101 | if (br_multicast_rcv(br, p, skb, vid)) | |
102 | goto drop; | |
103 | } | |
104 | } | |
c4fcb78c | 105 | |
b3f1be4b SH |
106 | if (p->state == BR_STATE_LEARNING) |
107 | goto drop; | |
0e5eabac | 108 | |
68b7c895 | 109 | BR_INPUT_SKB_CB(skb)->brdev = br->dev; |
7d850abd | 110 | BR_INPUT_SKB_CB(skb)->src_port_isolated = !!(p->flags & BR_ISOLATED); |
68b7c895 | 111 | |
057658cb RP |
112 | if (IS_ENABLED(CONFIG_INET) && |
113 | (skb->protocol == htons(ETH_P_ARP) || | |
114 | skb->protocol == htons(ETH_P_RARP))) { | |
115 | br_do_proxy_suppress_arp(skb, br, vid, p); | |
ed842fae RP |
116 | } else if (IS_ENABLED(CONFIG_IPV6) && |
117 | skb->protocol == htons(ETH_P_IPV6) && | |
c69c2cd4 | 118 | br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) && |
ed842fae RP |
119 | pskb_may_pull(skb, sizeof(struct ipv6hdr) + |
120 | sizeof(struct nd_msg)) && | |
121 | ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) { | |
122 | struct nd_msg *msg, _msg; | |
123 | ||
124 | msg = br_is_nd_neigh_msg(skb, &_msg); | |
125 | if (msg) | |
126 | br_do_suppress_nd(skb, br, vid, p, msg); | |
057658cb | 127 | } |
95850116 | 128 | |
8addd5e7 NA |
129 | switch (pkt_type) { |
130 | case BR_PKT_MULTICAST: | |
fbca58a2 | 131 | mdst = br_mdb_get(br, skb, vid); |
b00589af | 132 | if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) && |
cc0fdd80 | 133 | br_multicast_querier_exists(br, eth_hdr(skb))) { |
ff0fd34e | 134 | if ((mdst && mdst->host_joined) || |
e151aab9 | 135 | br_multicast_is_router(br)) { |
b35c5f63 | 136 | local_rcv = true; |
e151aab9 NA |
137 | br->dev->stats.multicast++; |
138 | } | |
139 | mcast_hit = true; | |
46c0772d | 140 | } else { |
b35c5f63 | 141 | local_rcv = true; |
e151aab9 | 142 | br->dev->stats.multicast++; |
46c0772d | 143 | } |
8addd5e7 NA |
144 | break; |
145 | case BR_PKT_UNICAST: | |
3d26eb8a | 146 | dst = br_fdb_find_rcu(br, eth_hdr(skb)->h_dest, vid); |
8addd5e7 NA |
147 | default: |
148 | break; | |
1da177e4 LT |
149 | } |
150 | ||
e151aab9 | 151 | if (dst) { |
ca6d4480 | 152 | unsigned long now = jiffies; |
153 | ||
6869c3b0 | 154 | if (test_bit(BR_FDB_LOCAL, &dst->flags)) |
8addd5e7 NA |
155 | return br_pass_frame_up(skb); |
156 | ||
ca6d4480 | 157 | if (now != dst->used) |
158 | dst->used = now; | |
37b090e6 | 159 | br_forward(dst->dst, skb, local_rcv, false); |
e151aab9 NA |
160 | } else { |
161 | if (!mcast_hit) | |
8addd5e7 | 162 | br_flood(br, skb, pkt_type, local_rcv, false); |
e151aab9 | 163 | else |
37b090e6 | 164 | br_multicast_flood(mdst, skb, local_rcv, false); |
e081e1e3 | 165 | } |
1da177e4 | 166 | |
b35c5f63 NA |
167 | if (local_rcv) |
168 | return br_pass_frame_up(skb); | |
87557c18 | 169 | |
1da177e4 LT |
170 | out: |
171 | return 0; | |
b3f1be4b SH |
172 | drop: |
173 | kfree_skb(skb); | |
174 | goto out; | |
1da177e4 | 175 | } |
34666d46 | 176 | EXPORT_SYMBOL_GPL(br_handle_frame_finish); |
1da177e4 | 177 | |
56fae404 | 178 | static void __br_handle_local_finish(struct sk_buff *skb) |
cf0f02d0 | 179 | { |
f350a0a8 | 180 | struct net_bridge_port *p = br_port_get_rcu(skb->dev); |
2ba071ec | 181 | u16 vid = 0; |
cf0f02d0 | 182 | |
e0d7968a | 183 | /* check if vlan is allowed, to avoid spoofing */ |
70e4272b | 184 | if ((p->flags & BR_LEARNING) && |
5d1fcaf3 | 185 | nbp_state_should_learn(p) && |
70e4272b NA |
186 | !br_opt_get(p->br, BROPT_NO_LL_LEARN) && |
187 | br_should_learn(p, skb, &vid)) | |
be0c5677 | 188 | br_fdb_update(p->br, p, eth_hdr(skb)->h_source, vid, 0); |
56fae404 IS |
189 | } |
190 | ||
191 | /* note: already called with rcu_read_lock */ | |
192 | static int br_handle_local_finish(struct net *net, struct sock *sk, struct sk_buff *skb) | |
193 | { | |
56fae404 | 194 | __br_handle_local_finish(skb); |
8626c56c | 195 | |
3b2e2904 NA |
196 | /* return 1 to signal the okfn() was called so it's ok to use the skb */ |
197 | return 1; | |
cf0f02d0 SH |
198 | } |
199 | ||
971502d7 FW |
200 | static int nf_hook_bridge_pre(struct sk_buff *skb, struct sk_buff **pskb) |
201 | { | |
202 | #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE | |
203 | struct nf_hook_entries *e = NULL; | |
204 | struct nf_hook_state state; | |
205 | unsigned int verdict, i; | |
206 | struct net *net; | |
207 | int ret; | |
208 | ||
209 | net = dev_net(skb->dev); | |
210 | #ifdef HAVE_JUMP_LABEL | |
211 | if (!static_key_false(&nf_hooks_needed[NFPROTO_BRIDGE][NF_BR_PRE_ROUTING])) | |
212 | goto frame_finish; | |
213 | #endif | |
214 | ||
215 | e = rcu_dereference(net->nf.hooks_bridge[NF_BR_PRE_ROUTING]); | |
216 | if (!e) | |
217 | goto frame_finish; | |
218 | ||
219 | nf_hook_state_init(&state, NF_BR_PRE_ROUTING, | |
220 | NFPROTO_BRIDGE, skb->dev, NULL, NULL, | |
221 | net, br_handle_frame_finish); | |
222 | ||
223 | for (i = 0; i < e->num_hook_entries; i++) { | |
224 | verdict = nf_hook_entry_hookfn(&e->hooks[i], skb, &state); | |
225 | switch (verdict & NF_VERDICT_MASK) { | |
226 | case NF_ACCEPT: | |
223fd0ad FW |
227 | if (BR_INPUT_SKB_CB(skb)->br_netfilter_broute) { |
228 | *pskb = skb; | |
229 | return RX_HANDLER_PASS; | |
230 | } | |
971502d7 FW |
231 | break; |
232 | case NF_DROP: | |
233 | kfree_skb(skb); | |
234 | return RX_HANDLER_CONSUMED; | |
235 | case NF_QUEUE: | |
0d9cb300 | 236 | ret = nf_queue(skb, &state, i, verdict); |
971502d7 FW |
237 | if (ret == 1) |
238 | continue; | |
239 | return RX_HANDLER_CONSUMED; | |
240 | default: /* STOLEN */ | |
241 | return RX_HANDLER_CONSUMED; | |
242 | } | |
243 | } | |
244 | frame_finish: | |
245 | net = dev_net(skb->dev); | |
246 | br_handle_frame_finish(net, NULL, skb); | |
247 | #else | |
248 | br_handle_frame_finish(dev_net(skb->dev), NULL, skb); | |
249 | #endif | |
250 | return RX_HANDLER_CONSUMED; | |
251 | } | |
252 | ||
1da177e4 | 253 | /* |
6229e362 | 254 | * Return NULL if skb is handled |
eeaf61d8 | 255 | * note: already called with rcu_read_lock |
1da177e4 | 256 | */ |
8a4eb573 | 257 | rx_handler_result_t br_handle_frame(struct sk_buff **pskb) |
1da177e4 | 258 | { |
ab95bfe0 | 259 | struct net_bridge_port *p; |
8a4eb573 | 260 | struct sk_buff *skb = *pskb; |
1da177e4 LT |
261 | const unsigned char *dest = eth_hdr(skb)->h_dest; |
262 | ||
c2368e79 | 263 | if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) |
8a4eb573 | 264 | return RX_HANDLER_PASS; |
ab95bfe0 | 265 | |
1da177e4 | 266 | if (!is_valid_ether_addr(eth_hdr(skb)->h_source)) |
467aea0d | 267 | goto drop; |
1da177e4 | 268 | |
7b995651 HX |
269 | skb = skb_share_check(skb, GFP_ATOMIC); |
270 | if (!skb) | |
8a4eb573 | 271 | return RX_HANDLER_CONSUMED; |
7b995651 | 272 | |
f12064d1 FW |
273 | memset(skb->cb, 0, sizeof(struct br_input_skb_cb)); |
274 | ||
f350a0a8 | 275 | p = br_port_get_rcu(skb->dev); |
11538d03 RP |
276 | if (p->flags & BR_VLAN_TUNNEL) { |
277 | if (br_handle_ingress_vlan_tunnel(skb, p, | |
278 | nbp_vlan_group_rcu(p))) | |
279 | goto drop; | |
280 | } | |
ab95bfe0 | 281 | |
46acc460 | 282 | if (unlikely(is_link_local_ether_addr(dest))) { |
f2808d22 TM |
283 | u16 fwd_mask = p->br->group_fwd_mask_required; |
284 | ||
515853cc | 285 | /* |
286 | * See IEEE 802.1D Table 7-10 Reserved addresses | |
287 | * | |
288 | * Assignment Value | |
289 | * Bridge Group Address 01-80-C2-00-00-00 | |
290 | * (MAC Control) 802.3 01-80-C2-00-00-01 | |
291 | * (Link Aggregation) 802.3 01-80-C2-00-00-02 | |
292 | * 802.1X PAE address 01-80-C2-00-00-03 | |
293 | * | |
294 | * 802.1AB LLDP 01-80-C2-00-00-0E | |
295 | * | |
296 | * Others reserved for future standardization | |
297 | */ | |
5af48b59 | 298 | fwd_mask |= p->group_fwd_mask; |
515853cc | 299 | switch (dest[5]) { |
300 | case 0x00: /* Bridge Group Address */ | |
301 | /* If STP is turned off, | |
302 | then must forward to keep loop detection */ | |
f2808d22 TM |
303 | if (p->br->stp_enabled == BR_NO_STP || |
304 | fwd_mask & (1u << dest[5])) | |
515853cc | 305 | goto forward; |
56fae404 IS |
306 | *pskb = skb; |
307 | __br_handle_local_finish(skb); | |
308 | return RX_HANDLER_PASS; | |
515853cc | 309 | |
310 | case 0x01: /* IEEE MAC (Pause) */ | |
2111f8b9 SH |
311 | goto drop; |
312 | ||
baedbe55 IS |
313 | case 0x0E: /* 802.1AB LLDP */ |
314 | fwd_mask |= p->br->group_fwd_mask; | |
315 | if (fwd_mask & (1u << dest[5])) | |
316 | goto forward; | |
317 | *pskb = skb; | |
318 | __br_handle_local_finish(skb); | |
319 | return RX_HANDLER_PASS; | |
320 | ||
515853cc | 321 | default: |
322 | /* Allow selective forwarding for most other protocols */ | |
f2808d22 TM |
323 | fwd_mask |= p->br->group_fwd_mask; |
324 | if (fwd_mask & (1u << dest[5])) | |
515853cc | 325 | goto forward; |
326 | } | |
a598f6ae | 327 | |
3b2e2904 NA |
328 | /* The else clause should be hit when nf_hook(): |
329 | * - returns < 0 (drop/error) | |
330 | * - returns = 0 (stolen/nf_queue) | |
331 | * Thus return 1 from the okfn() to signal the skb is ok to pass | |
332 | */ | |
333 | if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, | |
334 | dev_net(skb->dev), NULL, skb, skb->dev, NULL, | |
335 | br_handle_local_finish) == 1) { | |
336 | return RX_HANDLER_PASS; | |
337 | } else { | |
338 | return RX_HANDLER_CONSUMED; | |
339 | } | |
2111f8b9 | 340 | } |
1da177e4 | 341 | |
a598f6ae | 342 | forward: |
467aea0d SH |
343 | switch (p->state) { |
344 | case BR_STATE_FORWARDING: | |
467aea0d | 345 | case BR_STATE_LEARNING: |
9a7b6ef9 | 346 | if (ether_addr_equal(p->br->dev->dev_addr, dest)) |
1da177e4 LT |
347 | skb->pkt_type = PACKET_HOST; |
348 | ||
971502d7 | 349 | return nf_hook_bridge_pre(skb, pskb); |
467aea0d SH |
350 | default: |
351 | drop: | |
352 | kfree_skb(skb); | |
1da177e4 | 353 | } |
8a4eb573 | 354 | return RX_HANDLER_CONSUMED; |
1da177e4 | 355 | } |