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