Debian Codegraph @

net/bridge/br_arp_nd_proxy.c

raw
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Handle bridge arp/nd proxy/suppress
4 *
5 * Copyright (C) 2017 Cumulus Networks
6 * Copyright (c) 2017 Roopa Prabhu <[email protected]>
7 *
8 * Authors:
9 * Roopa Prabhu <[email protected]>
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/neighbour.h>
16 #include <net/arp.h>
17 #include <linux/if_vlan.h>
18 #include <linux/inetdevice.h>
19 #include <net/addrconf.h>
20 #include <net/ipv6_stubs.h>
21 #if IS_ENABLED(CONFIG_IPV6)
22 #include <net/ip6_checksum.h>
23 #endif
24
25 #include "br_private.h"
26
27 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br)
28 {
29 struct net_bridge_port *p;
30 bool neigh_suppress = false;
31
32 list_for_each_entry(p, &br->port_list, list) {
33 if (p->flags & (BR_NEIGH_SUPPRESS | BR_NEIGH_VLAN_SUPPRESS)) {
34 neigh_suppress = true;
35 break;
36 }
37 }
38
39 br_opt_toggle(br, BROPT_NEIGH_SUPPRESS_ENABLED, neigh_suppress);
40 }
41
42 #if IS_ENABLED(CONFIG_INET)
43 static void br_arp_send(struct net_bridge *br, struct net_bridge_port *p,
44 struct net_device *dev, __be32 dest_ip, __be32 src_ip,
45 const unsigned char *dest_hw,
46 const unsigned char *src_hw,
47 const unsigned char *target_hw,
48 __be16 vlan_proto, u16 vlan_tci)
49 {
50 struct net_bridge_vlan_group *vg;
51 struct sk_buff *skb;
52 u16 pvid;
53
54 netdev_dbg(dev, "arp send dev %s dst %pI4 dst_hw %pM src %pI4 src_hw %pM\n",
55 dev->name, &dest_ip, dest_hw, &src_ip, src_hw);
56
57 if (!vlan_tci) {
58 arp_send(ARPOP_REPLY, ETH_P_ARP, dest_ip, dev, src_ip,
59 dest_hw, src_hw, target_hw);
60 return;
61 }
62
63 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, dest_ip, dev, src_ip,
64 dest_hw, src_hw, target_hw);
65 if (!skb)
66 return;
67
68 if (p)
69 vg = nbp_vlan_group_rcu(p);
70 else
71 vg = br_vlan_group_rcu(br);
72 pvid = br_get_pvid(vg);
73 if (pvid == (vlan_tci & VLAN_VID_MASK))
74 vlan_tci = 0;
75
76 if (vlan_tci)
77 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
78
79 if (p) {
80 arp_xmit(skb);
81 } else {
82 skb_reset_mac_header(skb);
83 __skb_pull(skb, skb_network_offset(skb));
84 skb->ip_summed = CHECKSUM_UNNECESSARY;
85 skb->pkt_type = PACKET_HOST;
86
87 netif_rx(skb);
88 }
89 }
90
91 static int br_chk_addr_ip(struct net_device *dev,
92 struct netdev_nested_priv *priv)
93 {
94 __be32 ip = *(__be32 *)priv->data;
95 struct in_device *in_dev;
96 __be32 addr = 0;
97
98 in_dev = __in_dev_get_rcu(dev);
99 if (in_dev)
100 addr = inet_confirm_addr(dev_net(dev), in_dev, 0, ip,
101 RT_SCOPE_HOST);
102
103 if (addr == ip)
104 return 1;
105
106 return 0;
107 }
108
109 static bool br_is_local_ip(struct net_device *dev, __be32 ip)
110 {
111 struct netdev_nested_priv priv = {
112 .data = (void *)&ip,
113 };
114
115 if (br_chk_addr_ip(dev, &priv))
116 return true;
117
118 /* check if ip is configured on upper dev */
119 if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip, &priv))
120 return true;
121
122 return false;
123 }
124
125 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
126 u16 vid, struct net_bridge_port *p)
127 {
128 struct net_device *dev = br->dev;
129 struct net_device *vlandev = dev;
130 struct neighbour *n;
131 struct arphdr *parp;
132 u8 *arpptr, *sha;
133 __be32 sip, tip;
134
135 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 0;
136
137 if ((dev->flags & IFF_NOARP) ||
138 !pskb_may_pull(skb, arp_hdr_len(dev)))
139 return;
140
141 parp = arp_hdr(skb);
142
143 if (parp->ar_pro != htons(ETH_P_IP) ||
144 parp->ar_hln != dev->addr_len ||
145 parp->ar_pln != 4)
146 return;
147
148 arpptr = (u8 *)parp + sizeof(struct arphdr);
149 sha = arpptr;
150 arpptr += dev->addr_len; /* sha */
151 memcpy(&sip, arpptr, sizeof(sip));
152 arpptr += sizeof(sip);
153 arpptr += dev->addr_len; /* tha */
154 memcpy(&tip, arpptr, sizeof(tip));
155
156 if (ipv4_is_loopback(tip) ||
157 ipv4_is_multicast(tip))
158 return;
159
160 if (br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) {
161 if (br_is_neigh_suppress_enabled(p, vid))
162 return;
163 if (is_unicast_ether_addr(eth_hdr(skb)->h_dest) &&
164 parp->ar_op == htons(ARPOP_REQUEST))
165 return;
166 if (parp->ar_op != htons(ARPOP_RREQUEST) &&
167 parp->ar_op != htons(ARPOP_RREPLY) &&
168 (ipv4_is_zeronet(sip) || sip == tip)) {
169 /* prevent flooding to neigh suppress ports */
170 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
171 return;
172 }
173 }
174
175 if (parp->ar_op != htons(ARPOP_REQUEST))
176 return;
177
178 if (vid != 0) {
179 vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto,
180 vid);
181 if (!vlandev)
182 return;
183 }
184
185 if (br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
186 br_is_local_ip(vlandev, tip)) {
187 /* its our local ip, so don't proxy reply
188 * and don't forward to neigh suppress ports
189 */
190 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
191 return;
192 }
193
194 n = neigh_lookup(&arp_tbl, &tip, vlandev);
195 if (n) {
196 struct net_bridge_fdb_entry *f;
197
198 if (!(READ_ONCE(n->nud_state) & NUD_VALID)) {
199 neigh_release(n);
200 return;
201 }
202
203 f = br_fdb_find_rcu(br, n->ha, vid);
204 if (f) {
205 const struct net_bridge_port *dst = READ_ONCE(f->dst);
206 bool replied = false;
207
208 if ((p && (p->flags & BR_PROXYARP)) ||
209 (dst && (dst->flags & BR_PROXYARP_WIFI)) ||
210 br_is_neigh_suppress_enabled(dst, vid)) {
211 if (!vid)
212 br_arp_send(br, p, skb->dev, sip, tip,
213 sha, n->ha, sha, 0, 0);
214 else
215 br_arp_send(br, p, skb->dev, sip, tip,
216 sha, n->ha, sha,
217 skb->vlan_proto,
218 skb_vlan_tag_get(skb));
219 replied = true;
220 }
221
222 /* If we have replied or as long as we know the
223 * mac, indicate to arp replied
224 */
225 if (replied ||
226 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED))
227 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
228 }
229
230 neigh_release(n);
231 }
232 }
233 #endif
234
235 #if IS_ENABLED(CONFIG_IPV6)
236 struct nd_msg *br_is_nd_neigh_msg(const struct sk_buff *skb, struct nd_msg *msg)
237 {
238 struct nd_msg *m;
239
240 m = skb_header_pointer(skb, skb_network_offset(skb) +
241 sizeof(struct ipv6hdr), sizeof(*msg), msg);
242 if (!m)
243 return NULL;
244
245 if (m->icmph.icmp6_code != 0 ||
246 (m->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
247 m->icmph.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
248 return NULL;
249
250 return m;
251 }
252
253 static void br_nd_send(struct net_bridge *br, struct net_bridge_port *p,
254 struct sk_buff *request, struct neighbour *n,
255 __be16 vlan_proto, u16 vlan_tci)
256 {
257 struct net_device *dev = request->dev;
258 struct net_bridge_vlan_group *vg;
259 struct nd_msg *na, *ns;
260 struct sk_buff *reply;
261 struct ipv6hdr *pip6;
262 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
263 int ns_olen;
264 int i, len;
265 u8 *daddr;
266 u16 pvid;
267
268 if (!dev || skb_linearize(request))
269 return;
270
271 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
272 sizeof(*na) + na_olen + dev->needed_tailroom;
273
274 reply = alloc_skb(len, GFP_ATOMIC);
275 if (!reply)
276 return;
277
278 reply->protocol = htons(ETH_P_IPV6);
279 reply->dev = dev;
280 skb_reserve(reply, LL_RESERVED_SPACE(dev));
281 skb_push(reply, sizeof(struct ethhdr));
282 skb_set_mac_header(reply, 0);
283
284 daddr = eth_hdr(request)->h_source;
285 ns = (struct nd_msg *)(skb_network_header(request) +
286 sizeof(struct ipv6hdr));
287
288 /* Do we need option processing ? */
289 ns_olen = request->len - (skb_network_offset(request) +
290 sizeof(struct ipv6hdr)) - sizeof(*ns);
291 for (i = 0; i < ns_olen - 1; i += (ns->opt[i + 1] << 3)) {
292 if (!ns->opt[i + 1] || i + (ns->opt[i + 1] << 3) > ns_olen) {
293 kfree_skb(reply);
294 return;
295 }
296 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
297 if ((ns->opt[i + 1] << 3) >=
298 sizeof(struct nd_opt_hdr) + ETH_ALEN)
299 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
300 break;
301 }
302 }
303
304 /* Ethernet header */
305 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
306 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
307 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
308 reply->protocol = htons(ETH_P_IPV6);
309
310 skb_pull(reply, sizeof(struct ethhdr));
311 skb_set_network_header(reply, 0);
312 skb_put(reply, sizeof(struct ipv6hdr));
313
314 /* IPv6 header */
315 pip6 = ipv6_hdr(reply);
316 memset(pip6, 0, sizeof(struct ipv6hdr));
317 pip6->version = 6;
318 pip6->priority = ipv6_hdr(request)->priority;
319 pip6->nexthdr = IPPROTO_ICMPV6;
320 pip6->hop_limit = 255;
321 pip6->daddr = ipv6_hdr(request)->saddr;
322 pip6->saddr = *(struct in6_addr *)n->primary_key;
323
324 skb_pull(reply, sizeof(struct ipv6hdr));
325 skb_set_transport_header(reply, 0);
326
327 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
328
329 /* Neighbor Advertisement */
330 memset(na, 0, sizeof(*na) + na_olen);
331 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
332 na->icmph.icmp6_router = (n->flags & NTF_ROUTER) ? 1 : 0;
333 na->icmph.icmp6_override = 1;
334 na->icmph.icmp6_solicited = 1;
335 na->target = ns->target;
336 ether_addr_copy(&na->opt[2], n->ha);
337 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
338 na->opt[1] = na_olen >> 3;
339
340 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
341 &pip6->daddr,
342 sizeof(*na) + na_olen,
343 IPPROTO_ICMPV6,
344 csum_partial(na, sizeof(*na) + na_olen, 0));
345
346 pip6->payload_len = htons(sizeof(*na) + na_olen);
347
348 skb_push(reply, sizeof(struct ipv6hdr));
349 skb_push(reply, sizeof(struct ethhdr));
350
351 reply->ip_summed = CHECKSUM_UNNECESSARY;
352
353 if (p)
354 vg = nbp_vlan_group_rcu(p);
355 else
356 vg = br_vlan_group_rcu(br);
357 pvid = br_get_pvid(vg);
358 if (pvid == (vlan_tci & VLAN_VID_MASK))
359 vlan_tci = 0;
360
361 if (vlan_tci)
362 __vlan_hwaccel_put_tag(reply, vlan_proto, vlan_tci);
363
364 netdev_dbg(dev, "nd send dev %s dst %pI6 dst_hw %pM src %pI6 src_hw %pM\n",
365 dev->name, &pip6->daddr, daddr, &pip6->saddr, n->ha);
366
367 if (p) {
368 dev_queue_xmit(reply);
369 } else {
370 skb_reset_mac_header(reply);
371 __skb_pull(reply, skb_network_offset(reply));
372 reply->ip_summed = CHECKSUM_UNNECESSARY;
373 reply->pkt_type = PACKET_HOST;
374
375 netif_rx(reply);
376 }
377 }
378
379 static int br_chk_addr_ip6(struct net_device *dev,
380 struct netdev_nested_priv *priv)
381 {
382 struct in6_addr *addr = (struct in6_addr *)priv->data;
383
384 if (ipv6_chk_addr(dev_net(dev), addr, dev, 0))
385 return 1;
386
387 return 0;
388 }
389
390 static bool br_is_local_ip6(struct net_device *dev, struct in6_addr *addr)
391
392 {
393 struct netdev_nested_priv priv = {
394 .data = (void *)addr,
395 };
396
397 if (br_chk_addr_ip6(dev, &priv))
398 return true;
399
400 /* check if ip is configured on upper dev */
401 if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip6, &priv))
402 return true;
403
404 return false;
405 }
406
407 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
408 u16 vid, struct net_bridge_port *p, struct nd_msg *msg)
409 {
410 struct net_device *dev = br->dev;
411 struct net_device *vlandev = NULL;
412 struct in6_addr *saddr, *daddr;
413 struct ipv6hdr *iphdr;
414 struct neighbour *n;
415
416 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 0;
417
418 if (br_is_neigh_suppress_enabled(p, vid))
419 return;
420
421 if (is_unicast_ether_addr(eth_hdr(skb)->h_dest) &&
422 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
423 return;
424
425 if (msg->icmph.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT &&
426 !msg->icmph.icmp6_solicited) {
427 /* prevent flooding to neigh suppress ports */
428 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
429 return;
430 }
431
432 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
433 return;
434
435 iphdr = ipv6_hdr(skb);
436 saddr = &iphdr->saddr;
437 daddr = &iphdr->daddr;
438
439 if (ipv6_addr_any(saddr) || !ipv6_addr_cmp(saddr, daddr)) {
440 /* prevent flooding to neigh suppress ports */
441 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
442 return;
443 }
444
445 if (vid != 0) {
446 /* build neigh table lookup on the vlan device */
447 vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto,
448 vid);
449 if (!vlandev)
450 return;
451 } else {
452 vlandev = dev;
453 }
454
455 if (br_is_local_ip6(vlandev, &msg->target)) {
456 /* its our own ip, so don't proxy reply
457 * and don't forward to arp suppress ports
458 */
459 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
460 return;
461 }
462
463 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, vlandev);
464 if (n) {
465 struct net_bridge_fdb_entry *f;
466
467 if (!(READ_ONCE(n->nud_state) & NUD_VALID)) {
468 neigh_release(n);
469 return;
470 }
471
472 f = br_fdb_find_rcu(br, n->ha, vid);
473 if (f) {
474 const struct net_bridge_port *dst = READ_ONCE(f->dst);
475 bool replied = false;
476
477 if (br_is_neigh_suppress_enabled(dst, vid)) {
478 if (vid != 0)
479 br_nd_send(br, p, skb, n,
480 skb->vlan_proto,
481 skb_vlan_tag_get(skb));
482 else
483 br_nd_send(br, p, skb, n, 0, 0);
484 replied = true;
485 }
486
487 /* If we have replied or as long as we know the
488 * mac, indicate to NEIGH_SUPPRESS ports that we
489 * have replied
490 */
491 if (replied ||
492 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED))
493 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
494 }
495 neigh_release(n);
496 }
497 }
498 #endif
499
500 bool br_is_neigh_suppress_enabled(const struct net_bridge_port *p, u16 vid)
501 {
502 if (!p)
503 return false;
504
505 if (!vid)
506 return !!(p->flags & BR_NEIGH_SUPPRESS);
507
508 if (p->flags & BR_NEIGH_VLAN_SUPPRESS) {
509 struct net_bridge_vlan_group *vg = nbp_vlan_group_rcu(p);
510 struct net_bridge_vlan *v;
511
512 v = br_vlan_find(vg, vid);
513 if (!v)
514 return false;
515 return !!(v->priv_flags & BR_VLFLAG_NEIGH_SUPPRESS_ENABLED);
516 } else {
517 return !!(p->flags & BR_NEIGH_SUPPRESS);
518 }
519 }