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