1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Handle bridge arp/nd proxy/suppress
5 * Copyright (C) 2017 Cumulus Networks
12 #include <linux/kernel.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/neighbour.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>
25 #include "br_private.h"
27 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br)
29 struct net_bridge_port *p;
30 bool neigh_suppress = false;
32 list_for_each_entry(p, &br->port_list, list) {
33 if (p->flags & BR_NEIGH_SUPPRESS) {
34 neigh_suppress = true;
39 br_opt_toggle(br, BROPT_NEIGH_SUPPRESS_ENABLED, neigh_suppress);
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)
50 struct net_bridge_vlan_group *vg;
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);
58 arp_send(ARPOP_REPLY, ETH_P_ARP, dest_ip, dev, src_ip,
59 dest_hw, src_hw, target_hw);
63 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, dest_ip, dev, src_ip,
64 dest_hw, src_hw, target_hw);
69 vg = nbp_vlan_group_rcu(p);
71 vg = br_vlan_group_rcu(br);
72 pvid = br_get_pvid(vg);
73 if (pvid == (vlan_tci & VLAN_VID_MASK))
77 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
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;
91 static int br_chk_addr_ip(struct net_device *dev,
92 struct netdev_nested_priv *priv)
94 __be32 ip = *(__be32 *)priv->data;
95 struct in_device *in_dev;
98 in_dev = __in_dev_get_rcu(dev);
100 addr = inet_confirm_addr(dev_net(dev), in_dev, 0, ip,
109 static bool br_is_local_ip(struct net_device *dev, __be32 ip)
111 struct netdev_nested_priv priv = {
115 if (br_chk_addr_ip(dev, &priv))
118 /* check if ip is configured on upper dev */
119 if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip, &priv))
125 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
126 u16 vid, struct net_bridge_port *p)
128 struct net_device *dev = br->dev;
129 struct net_device *vlandev = dev;
135 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 0;
137 if ((dev->flags & IFF_NOARP) ||
138 !pskb_may_pull(skb, arp_hdr_len(dev)))
143 if (parp->ar_pro != htons(ETH_P_IP) ||
144 parp->ar_hln != dev->addr_len ||
148 arpptr = (u8 *)parp + sizeof(struct arphdr);
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));
156 if (ipv4_is_loopback(tip) ||
157 ipv4_is_multicast(tip))
160 if (br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED)) {
161 if (p && (p->flags & BR_NEIGH_SUPPRESS))
163 if (parp->ar_op != htons(ARPOP_RREQUEST) &&
164 parp->ar_op != htons(ARPOP_RREPLY) &&
165 (ipv4_is_zeronet(sip) || sip == tip)) {
166 /* prevent flooding to neigh suppress ports */
167 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
172 if (parp->ar_op != htons(ARPOP_REQUEST))
176 vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto,
182 if (br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED) &&
183 br_is_local_ip(vlandev, tip)) {
184 /* its our local ip, so don't proxy reply
185 * and don't forward to neigh suppress ports
187 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
191 n = neigh_lookup(&arp_tbl, &tip, vlandev);
193 struct net_bridge_fdb_entry *f;
195 if (!(n->nud_state & NUD_VALID)) {
200 f = br_fdb_find_rcu(br, n->ha, vid);
202 bool replied = false;
204 if ((p && (p->flags & BR_PROXYARP)) ||
205 (f->dst && (f->dst->flags & (BR_PROXYARP_WIFI |
206 BR_NEIGH_SUPPRESS)))) {
208 br_arp_send(br, p, skb->dev, sip, tip,
209 sha, n->ha, sha, 0, 0);
211 br_arp_send(br, p, skb->dev, sip, tip,
214 skb_vlan_tag_get(skb));
218 /* If we have replied or as long as we know the
219 * mac, indicate to arp replied
222 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED))
223 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
231 #if IS_ENABLED(CONFIG_IPV6)
232 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *msg)
236 m = skb_header_pointer(skb, skb_network_offset(skb) +
237 sizeof(struct ipv6hdr), sizeof(*msg), msg);
241 if (m->icmph.icmp6_code != 0 ||
242 (m->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
243 m->icmph.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
249 static void br_nd_send(struct net_bridge *br, struct net_bridge_port *p,
250 struct sk_buff *request, struct neighbour *n,
251 __be16 vlan_proto, u16 vlan_tci, struct nd_msg *ns)
253 struct net_device *dev = request->dev;
254 struct net_bridge_vlan_group *vg;
255 struct sk_buff *reply;
257 struct ipv6hdr *pip6;
258 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
267 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
268 sizeof(*na) + na_olen + dev->needed_tailroom;
270 reply = alloc_skb(len, GFP_ATOMIC);
274 reply->protocol = htons(ETH_P_IPV6);
276 skb_reserve(reply, LL_RESERVED_SPACE(dev));
277 skb_push(reply, sizeof(struct ethhdr));
278 skb_set_mac_header(reply, 0);
280 daddr = eth_hdr(request)->h_source;
282 /* Do we need option processing ? */
283 ns_olen = request->len - (skb_network_offset(request) +
284 sizeof(struct ipv6hdr)) - sizeof(*ns);
285 for (i = 0; i < ns_olen - 1; i += (ns->opt[i + 1] << 3)) {
286 if (!ns->opt[i + 1]) {
290 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
291 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
296 /* Ethernet header */
297 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
298 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
299 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
300 reply->protocol = htons(ETH_P_IPV6);
302 skb_pull(reply, sizeof(struct ethhdr));
303 skb_set_network_header(reply, 0);
304 skb_put(reply, sizeof(struct ipv6hdr));
307 pip6 = ipv6_hdr(reply);
308 memset(pip6, 0, sizeof(struct ipv6hdr));
310 pip6->priority = ipv6_hdr(request)->priority;
311 pip6->nexthdr = IPPROTO_ICMPV6;
312 pip6->hop_limit = 255;
313 pip6->daddr = ipv6_hdr(request)->saddr;
314 pip6->saddr = *(struct in6_addr *)n->primary_key;
316 skb_pull(reply, sizeof(struct ipv6hdr));
317 skb_set_transport_header(reply, 0);
319 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
321 /* Neighbor Advertisement */
322 memset(na, 0, sizeof(*na) + na_olen);
323 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
324 na->icmph.icmp6_router = (n->flags & NTF_ROUTER) ? 1 : 0;
325 na->icmph.icmp6_override = 1;
326 na->icmph.icmp6_solicited = 1;
327 na->target = ns->target;
328 ether_addr_copy(&na->opt[2], n->ha);
329 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
330 na->opt[1] = na_olen >> 3;
332 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
334 sizeof(*na) + na_olen,
336 csum_partial(na, sizeof(*na) + na_olen, 0));
338 pip6->payload_len = htons(sizeof(*na) + na_olen);
340 skb_push(reply, sizeof(struct ipv6hdr));
341 skb_push(reply, sizeof(struct ethhdr));
343 reply->ip_summed = CHECKSUM_UNNECESSARY;
346 vg = nbp_vlan_group_rcu(p);
348 vg = br_vlan_group_rcu(br);
349 pvid = br_get_pvid(vg);
350 if (pvid == (vlan_tci & VLAN_VID_MASK))
354 __vlan_hwaccel_put_tag(reply, vlan_proto, vlan_tci);
356 netdev_dbg(dev, "nd send dev %s dst %pI6 dst_hw %pM src %pI6 src_hw %pM\n",
357 dev->name, &pip6->daddr, daddr, &pip6->saddr, n->ha);
360 dev_queue_xmit(reply);
362 skb_reset_mac_header(reply);
363 __skb_pull(reply, skb_network_offset(reply));
364 reply->ip_summed = CHECKSUM_UNNECESSARY;
365 reply->pkt_type = PACKET_HOST;
371 static int br_chk_addr_ip6(struct net_device *dev,
372 struct netdev_nested_priv *priv)
374 struct in6_addr *addr = (struct in6_addr *)priv->data;
376 if (ipv6_chk_addr(dev_net(dev), addr, dev, 0))
382 static bool br_is_local_ip6(struct net_device *dev, struct in6_addr *addr)
385 struct netdev_nested_priv priv = {
386 .data = (void *)addr,
389 if (br_chk_addr_ip6(dev, &priv))
392 /* check if ip is configured on upper dev */
393 if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip6, &priv))
399 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
400 u16 vid, struct net_bridge_port *p, struct nd_msg *msg)
402 struct net_device *dev = br->dev;
403 struct net_device *vlandev = NULL;
404 struct in6_addr *saddr, *daddr;
405 struct ipv6hdr *iphdr;
408 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 0;
410 if (p && (p->flags & BR_NEIGH_SUPPRESS))
413 if (msg->icmph.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT &&
414 !msg->icmph.icmp6_solicited) {
415 /* prevent flooding to neigh suppress ports */
416 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
420 if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
423 iphdr = ipv6_hdr(skb);
424 saddr = &iphdr->saddr;
425 daddr = &iphdr->daddr;
427 if (ipv6_addr_any(saddr) || !ipv6_addr_cmp(saddr, daddr)) {
428 /* prevent flooding to neigh suppress ports */
429 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
434 /* build neigh table lookup on the vlan device */
435 vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto,
443 if (br_is_local_ip6(vlandev, &msg->target)) {
444 /* its our own ip, so don't proxy reply
445 * and don't forward to arp suppress ports
447 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;
451 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, vlandev);
453 struct net_bridge_fdb_entry *f;
455 if (!(n->nud_state & NUD_VALID)) {
460 f = br_fdb_find_rcu(br, n->ha, vid);
462 bool replied = false;
464 if (f->dst && (f->dst->flags & BR_NEIGH_SUPPRESS)) {
466 br_nd_send(br, p, skb, n,
468 skb_vlan_tag_get(skb), msg);
470 br_nd_send(br, p, skb, n, 0, 0, msg);
474 /* If we have replied or as long as we know the
475 * mac, indicate to NEIGH_SUPPRESS ports that we
479 br_opt_get(br, BROPT_NEIGH_SUPPRESS_ENABLED))
480 BR_INPUT_SKB_CB(skb)->proxyarp_replied = 1;