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