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 136 if ((dev->flags & IFF_NOARP) || 137 !pskb_may_pull(skb, arp_hdr_len(dev))) 138 return; 139 140 parp = arp_hdr(skb); 141 142 if (parp->ar_pro != htons(ETH_P_IP) || 143 parp->ar_hln != dev->addr_len || 144 parp->ar_pln != 4) 145 return; 146 147 arpptr = (u8 *)parp + sizeof(struct arphdr); 148 sha = arpptr; 149 arpptr += dev->addr_len; /* sha */ 150 memcpy(&sip, arpptr, sizeof(sip)); 151 arpptr += sizeof(sip); 152 arpptr += dev->addr_len; /* tha */ 153 memcpy(&tip, arpptr, sizeof(tip)); 154 155 if (ipv4_is_loopback(tip) || 156 ipv4_is_multicast(tip)) 157 return; 158 159 if (br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) { 160 if (br_is_neigh_suppress_enabled(p, vid)) 161 return; 162 if (is_unicast_ether_addr(eth_hdr(skb)->h_dest) && 163 parp->ar_op == htons(ARPOP_REQUEST)) 164 return; 165 if (parp->ar_op != htons(ARPOP_RREQUEST) && 166 parp->ar_op != htons(ARPOP_RREPLY) && 167 (ipv4_is_zeronet(sip) || sip == tip)) { 168 /* prevent flooding to neigh suppress ports */ 169 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1; 170 return; 171 } 172 } 173 174 if (parp->ar_op != htons(ARPOP_REQUEST)) 175 return; 176 177 if (vid != 0) { 178 vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto, 179 vid); 180 if (!vlandev) 181 return; 182 } 183 184 if (br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) && 185 br_is_local_ip(vlandev, tip)) { 186 /* its our local ip, so don't proxy reply 187 * and don't forward to neigh suppress ports 188 */ 189 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1; 190 return; 191 } 192 193 n = neigh_lookup(&arp_tbl, &tip, vlandev); 194 if (n) { 195 struct net_bridge_fdb_entry *f; 196 197 if (!(READ_ONCE(n->nud_state) & NUD_VALID)) { 198 neigh_release(n); 199 return; 200 } 201 202 f = br_fdb_find_rcu(br, n->ha, vid); 203 if (f) { 204 bool replied = false; 205 206 if ((p && (p->flags & BR_PROXYARP)) || 207 (f->dst && (f->dst->flags & BR_PROXYARP_WIFI)) || 208 br_is_neigh_suppress_enabled(f->dst, vid)) { 209 if (!vid) 210 br_arp_send(br, p, skb->dev, sip, tip, 211 sha, n->ha, sha, 0, 0); 212 else 213 br_arp_send(br, p, skb->dev, sip, tip, 214 sha, n->ha, sha, 215 skb->vlan_proto, 216 skb_vlan_tag_get(skb)); 217 replied = true; 218 } 219 220 /* If we have replied or as long as we know the 221 * mac, indicate to arp replied 222 */ 223 if (replied || 224 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) 225 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1; 226 } 227 228 neigh_release(n); 229 } 230 } 231 #endif 232 233 #if IS_ENABLED(CONFIG_IPV6) 234 struct nd_msg *br_is_nd_neigh_msg(const struct sk_buff *skb, struct nd_msg *msg) 235 { 236 struct nd_msg *m; 237 238 m = skb_header_pointer(skb, skb_network_offset(skb) + 239 sizeof(struct ipv6hdr), sizeof(*msg), msg); 240 if (!m) 241 return NULL; 242 243 if (m->icmph.icmp6_code != 0 || 244 (m->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION && 245 m->icmph.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT)) 246 return NULL; 247 248 return m; 249 } 250 251 static void br_nd_send(struct net_bridge *br, struct net_bridge_port *p, 252 struct sk_buff *request, struct neighbour *n, 253 __be16 vlan_proto, u16 vlan_tci, struct nd_msg *ns) 254 { 255 struct net_device *dev = request->dev; 256 struct net_bridge_vlan_group *vg; 257 struct sk_buff *reply; 258 struct nd_msg *na; 259 struct ipv6hdr *pip6; 260 int na_olen = 8; /* opt hdr + ETH_ALEN for target */ 261 int ns_olen; 262 int i, len; 263 u8 *daddr; 264 u16 pvid; 265 266 if (!dev) 267 return; 268 269 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) + 270 sizeof(*na) + na_olen + dev->needed_tailroom; 271 272 reply = alloc_skb(len, GFP_ATOMIC); 273 if (!reply) 274 return; 275 276 reply->protocol = htons(ETH_P_IPV6); 277 reply->dev = dev; 278 skb_reserve(reply, LL_RESERVED_SPACE(dev)); 279 skb_push(reply, sizeof(struct ethhdr)); 280 skb_set_mac_header(reply, 0); 281 282 daddr = eth_hdr(request)->h_source; 283 284 /* Do we need option processing ? */ 285 ns_olen = request->len - (skb_network_offset(request) + 286 sizeof(struct ipv6hdr)) - sizeof(*ns); 287 for (i = 0; i < ns_olen - 1; i += (ns->opt[i + 1] << 3)) { 288 if (!ns->opt[i + 1]) { 289 kfree_skb(reply); 290 return; 291 } 292 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) { 293 daddr = ns->opt + i + sizeof(struct nd_opt_hdr); 294 break; 295 } 296 } 297 298 /* Ethernet header */ 299 ether_addr_copy(eth_hdr(reply)->h_dest, daddr); 300 ether_addr_copy(eth_hdr(reply)->h_source, n->ha); 301 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6); 302 reply->protocol = htons(ETH_P_IPV6); 303 304 skb_pull(reply, sizeof(struct ethhdr)); 305 skb_set_network_header(reply, 0); 306 skb_put(reply, sizeof(struct ipv6hdr)); 307 308 /* IPv6 header */ 309 pip6 = ipv6_hdr(reply); 310 memset(pip6, 0, sizeof(struct ipv6hdr)); 311 pip6->version = 6; 312 pip6->priority = ipv6_hdr(request)->priority; 313 pip6->nexthdr = IPPROTO_ICMPV6; 314 pip6->hop_limit = 255; 315 pip6->daddr = ipv6_hdr(request)->saddr; 316 pip6->saddr = *(struct in6_addr *)n->primary_key; 317 318 skb_pull(reply, sizeof(struct ipv6hdr)); 319 skb_set_transport_header(reply, 0); 320 321 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen); 322 323 /* Neighbor Advertisement */ 324 memset(na, 0, sizeof(*na) + na_olen); 325 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT; 326 na->icmph.icmp6_router = (n->flags & NTF_ROUTER) ? 1 : 0; 327 na->icmph.icmp6_override = 1; 328 na->icmph.icmp6_solicited = 1; 329 na->target = ns->target; 330 ether_addr_copy(&na->opt[2], n->ha); 331 na->opt[0] = ND_OPT_TARGET_LL_ADDR; 332 na->opt[1] = na_olen >> 3; 333 334 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr, 335 &pip6->daddr, 336 sizeof(*na) + na_olen, 337 IPPROTO_ICMPV6, 338 csum_partial(na, sizeof(*na) + na_olen, 0)); 339 340 pip6->payload_len = htons(sizeof(*na) + na_olen); 341 342 skb_push(reply, sizeof(struct ipv6hdr)); 343 skb_push(reply, sizeof(struct ethhdr)); 344 345 reply->ip_summed = CHECKSUM_UNNECESSARY; 346 347 if (p) 348 vg = nbp_vlan_group_rcu(p); 349 else 350 vg = br_vlan_group_rcu(br); 351 pvid = br_get_pvid(vg); 352 if (pvid == (vlan_tci & VLAN_VID_MASK)) 353 vlan_tci = 0; 354 355 if (vlan_tci) 356 __vlan_hwaccel_put_tag(reply, vlan_proto, vlan_tci); 357 358 netdev_dbg(dev, "nd send dev %s dst %pI6 dst_hw %pM src %pI6 src_hw %pM\n", 359 dev->name, &pip6->daddr, daddr, &pip6->saddr, n->ha); 360 361 if (p) { 362 dev_queue_xmit(reply); 363 } else { 364 skb_reset_mac_header(reply); 365 __skb_pull(reply, skb_network_offset(reply)); 366 reply->ip_summed = CHECKSUM_UNNECESSARY; 367 reply->pkt_type = PACKET_HOST; 368 369 netif_rx(reply); 370 } 371 } 372 373 static int br_chk_addr_ip6(struct net_device *dev, 374 struct netdev_nested_priv *priv) 375 { 376 struct in6_addr *addr = (struct in6_addr *)priv->data; 377 378 if (ipv6_chk_addr(dev_net(dev), addr, dev, 0)) 379 return 1; 380 381 return 0; 382 } 383 384 static bool br_is_local_ip6(struct net_device *dev, struct in6_addr *addr) 385 386 { 387 struct netdev_nested_priv priv = { 388 .data = (void *)addr, 389 }; 390 391 if (br_chk_addr_ip6(dev, &priv)) 392 return true; 393 394 /* check if ip is configured on upper dev */ 395 if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip6, &priv)) 396 return true; 397 398 return false; 399 } 400 401 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br, 402 u16 vid, struct net_bridge_port *p, struct nd_msg *msg) 403 { 404 struct net_device *dev = br->dev; 405 struct net_device *vlandev = NULL; 406 struct in6_addr *saddr, *daddr; 407 struct ipv6hdr *iphdr; 408 struct neighbour *n; 409 410 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 0; 411 412 if (br_is_neigh_suppress_enabled(p, vid)) 413 return; 414 415 if (is_unicast_ether_addr(eth_hdr(skb)->h_dest) && 416 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) 417 return; 418 419 if (msg->icmph.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT && 420 !msg->icmph.icmp6_solicited) { 421 /* prevent flooding to neigh suppress ports */ 422 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1; 423 return; 424 } 425 426 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION) 427 return; 428 429 iphdr = ipv6_hdr(skb); 430 saddr = &iphdr->saddr; 431 daddr = &iphdr->daddr; 432 433 if (ipv6_addr_any(saddr) || !ipv6_addr_cmp(saddr, daddr)) { 434 /* prevent flooding to neigh suppress ports */ 435 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1; 436 return; 437 } 438 439 if (vid != 0) { 440 /* build neigh table lookup on the vlan device */ 441 vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto, 442 vid); 443 if (!vlandev) 444 return; 445 } else { 446 vlandev = dev; 447 } 448 449 if (br_is_local_ip6(vlandev, &msg->target)) { 450 /* its our own ip, so don't proxy reply 451 * and don't forward to arp suppress ports 452 */ 453 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1; 454 return; 455 } 456 457 n = neigh_lookup(&nd_tbl, &msg->target, vlandev); 458 if (n) { 459 struct net_bridge_fdb_entry *f; 460 461 if (!(READ_ONCE(n->nud_state) & NUD_VALID)) { 462 neigh_release(n); 463 return; 464 } 465 466 f = br_fdb_find_rcu(br, n->ha, vid); 467 if (f) { 468 bool replied = false; 469 470 if (br_is_neigh_suppress_enabled(f->dst, vid)) { 471 if (vid != 0) 472 br_nd_send(br, p, skb, n, 473 skb->vlan_proto, 474 skb_vlan_tag_get(skb), msg); 475 else 476 br_nd_send(br, p, skb, n, 0, 0, msg); 477 replied = true; 478 } 479 480 /* If we have replied or as long as we know the 481 * mac, indicate to NEIGH_SUPPRESS ports that we 482 * have replied 483 */ 484 if (replied || 485 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) 486 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1; 487 } 488 neigh_release(n); 489 } 490 } 491 #endif 492 493 bool br_is_neigh_suppress_enabled(const struct net_bridge_port *p, u16 vid) 494 { 495 if (!p) 496 return false; 497 498 if (!vid) 499 return !!(p->flags & BR_NEIGH_SUPPRESS); 500 501 if (p->flags & BR_NEIGH_VLAN_SUPPRESS) { 502 struct net_bridge_vlan_group *vg = nbp_vlan_group_rcu(p); 503 struct net_bridge_vlan *v; 504 505 v = br_vlan_find(vg, vid); 506 if (!v) 507 return false; 508 return !!(v->priv_flags & BR_VLFLAG_NEIGH_SUPPRESS_ENABLED); 509 } else { 510 return !!(p->flags & BR_NEIGH_SUPPRESS); 511 } 512 } 513