GNU Linux-libre 4.19.211-gnu1
[releases.git] / net / bridge / br_arp_nd_proxy.c
1 /*
2  *  Handle bridge arp/nd proxy/suppress
3  *
4  *  Copyright (C) 2017 Cumulus Networks
5  *  Copyright (c) 2017 Roopa Prabhu <roopa@cumulusnetworks.com>
6  *
7  *  Authors:
8  *      Roopa Prabhu <roopa@cumulusnetworks.com>
9  *
10  *  This program is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation; either version
13  *  2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/neighbour.h>
20 #include <net/arp.h>
21 #include <linux/if_vlan.h>
22 #include <linux/inetdevice.h>
23 #include <net/addrconf.h>
24 #if IS_ENABLED(CONFIG_IPV6)
25 #include <net/ip6_checksum.h>
26 #endif
27
28 #include "br_private.h"
29
30 void br_recalculate_neigh_suppress_enabled(struct net_bridge *br)
31 {
32         struct net_bridge_port *p;
33         bool neigh_suppress = false;
34
35         list_for_each_entry(p, &br->port_list, list) {
36                 if (p->flags & BR_NEIGH_SUPPRESS) {
37                         neigh_suppress = true;
38                         break;
39                 }
40         }
41
42         br->neigh_suppress_enabled = neigh_suppress;
43 }
44
45 #if IS_ENABLED(CONFIG_INET)
46 static void br_arp_send(struct net_bridge *br, struct net_bridge_port *p,
47                         struct net_device *dev, __be32 dest_ip, __be32 src_ip,
48                         const unsigned char *dest_hw,
49                         const unsigned char *src_hw,
50                         const unsigned char *target_hw,
51                         __be16 vlan_proto, u16 vlan_tci)
52 {
53         struct net_bridge_vlan_group *vg;
54         struct sk_buff *skb;
55         u16 pvid;
56
57         netdev_dbg(dev, "arp send dev %s dst %pI4 dst_hw %pM src %pI4 src_hw %pM\n",
58                    dev->name, &dest_ip, dest_hw, &src_ip, src_hw);
59
60         if (!vlan_tci) {
61                 arp_send(ARPOP_REPLY, ETH_P_ARP, dest_ip, dev, src_ip,
62                          dest_hw, src_hw, target_hw);
63                 return;
64         }
65
66         skb = arp_create(ARPOP_REPLY, ETH_P_ARP, dest_ip, dev, src_ip,
67                          dest_hw, src_hw, target_hw);
68         if (!skb)
69                 return;
70
71         if (p)
72                 vg = nbp_vlan_group_rcu(p);
73         else
74                 vg = br_vlan_group_rcu(br);
75         pvid = br_get_pvid(vg);
76         if (pvid == (vlan_tci & VLAN_VID_MASK))
77                 vlan_tci = 0;
78
79         if (vlan_tci)
80                 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
81
82         if (p) {
83                 arp_xmit(skb);
84         } else {
85                 skb_reset_mac_header(skb);
86                 __skb_pull(skb, skb_network_offset(skb));
87                 skb->ip_summed = CHECKSUM_UNNECESSARY;
88                 skb->pkt_type = PACKET_HOST;
89
90                 netif_rx_ni(skb);
91         }
92 }
93
94 static int br_chk_addr_ip(struct net_device *dev, void *data)
95 {
96         __be32 ip = *(__be32 *)data;
97         struct in_device *in_dev;
98         __be32 addr = 0;
99
100         in_dev = __in_dev_get_rcu(dev);
101         if (in_dev)
102                 addr = inet_confirm_addr(dev_net(dev), in_dev, 0, ip,
103                                          RT_SCOPE_HOST);
104
105         if (addr == ip)
106                 return 1;
107
108         return 0;
109 }
110
111 static bool br_is_local_ip(struct net_device *dev, __be32 ip)
112 {
113         if (br_chk_addr_ip(dev, &ip))
114                 return true;
115
116         /* check if ip is configured on upper dev */
117         if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip, &ip))
118                 return true;
119
120         return false;
121 }
122
123 void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
124                               u16 vid, struct net_bridge_port *p)
125 {
126         struct net_device *dev = br->dev;
127         struct net_device *vlandev = dev;
128         struct neighbour *n;
129         struct arphdr *parp;
130         u8 *arpptr, *sha;
131         __be32 sip, tip;
132
133         BR_INPUT_SKB_CB(skb)->proxyarp_replied = false;
134
135         if ((dev->flags & IFF_NOARP) ||
136             !pskb_may_pull(skb, arp_hdr_len(dev)))
137                 return;
138
139         parp = arp_hdr(skb);
140
141         if (parp->ar_pro != htons(ETH_P_IP) ||
142             parp->ar_hln != dev->addr_len ||
143             parp->ar_pln != 4)
144                 return;
145
146         arpptr = (u8 *)parp + sizeof(struct arphdr);
147         sha = arpptr;
148         arpptr += dev->addr_len;        /* sha */
149         memcpy(&sip, arpptr, sizeof(sip));
150         arpptr += sizeof(sip);
151         arpptr += dev->addr_len;        /* tha */
152         memcpy(&tip, arpptr, sizeof(tip));
153
154         if (ipv4_is_loopback(tip) ||
155             ipv4_is_multicast(tip))
156                 return;
157
158         if (br->neigh_suppress_enabled) {
159                 if (p && (p->flags & BR_NEIGH_SUPPRESS))
160                         return;
161                 if (parp->ar_op != htons(ARPOP_RREQUEST) &&
162                     parp->ar_op != htons(ARPOP_RREPLY) &&
163                     (ipv4_is_zeronet(sip) || sip == tip)) {
164                         /* prevent flooding to neigh suppress ports */
165                         BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
166                         return;
167                 }
168         }
169
170         if (parp->ar_op != htons(ARPOP_REQUEST))
171                 return;
172
173         if (vid != 0) {
174                 vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto,
175                                                    vid);
176                 if (!vlandev)
177                         return;
178         }
179
180         if (br->neigh_suppress_enabled && br_is_local_ip(vlandev, tip)) {
181                 /* its our local ip, so don't proxy reply
182                  * and don't forward to neigh suppress ports
183                  */
184                 BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
185                 return;
186         }
187
188         n = neigh_lookup(&arp_tbl, &tip, vlandev);
189         if (n) {
190                 struct net_bridge_fdb_entry *f;
191
192                 if (!(n->nud_state & NUD_VALID)) {
193                         neigh_release(n);
194                         return;
195                 }
196
197                 f = br_fdb_find_rcu(br, n->ha, vid);
198                 if (f) {
199                         bool replied = false;
200
201                         if ((p && (p->flags & BR_PROXYARP)) ||
202                             (f->dst && (f->dst->flags & (BR_PROXYARP_WIFI |
203                                                          BR_NEIGH_SUPPRESS)))) {
204                                 if (!vid)
205                                         br_arp_send(br, p, skb->dev, sip, tip,
206                                                     sha, n->ha, sha, 0, 0);
207                                 else
208                                         br_arp_send(br, p, skb->dev, sip, tip,
209                                                     sha, n->ha, sha,
210                                                     skb->vlan_proto,
211                                                     skb_vlan_tag_get(skb));
212                                 replied = true;
213                         }
214
215                         /* If we have replied or as long as we know the
216                          * mac, indicate to arp replied
217                          */
218                         if (replied || br->neigh_suppress_enabled)
219                                 BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
220                 }
221
222                 neigh_release(n);
223         }
224 }
225 #endif
226
227 #if IS_ENABLED(CONFIG_IPV6)
228 struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *msg)
229 {
230         struct nd_msg *m;
231
232         m = skb_header_pointer(skb, skb_network_offset(skb) +
233                                sizeof(struct ipv6hdr), sizeof(*msg), msg);
234         if (!m)
235                 return NULL;
236
237         if (m->icmph.icmp6_code != 0 ||
238             (m->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
239              m->icmph.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
240                 return NULL;
241
242         return m;
243 }
244
245 static void br_nd_send(struct net_bridge *br, struct net_bridge_port *p,
246                        struct sk_buff *request, struct neighbour *n,
247                        __be16 vlan_proto, u16 vlan_tci, struct nd_msg *ns)
248 {
249         struct net_device *dev = request->dev;
250         struct net_bridge_vlan_group *vg;
251         struct sk_buff *reply;
252         struct nd_msg *na;
253         struct ipv6hdr *pip6;
254         int na_olen = 8; /* opt hdr + ETH_ALEN for target */
255         int ns_olen;
256         int i, len;
257         u8 *daddr;
258         u16 pvid;
259
260         if (!dev)
261                 return;
262
263         len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
264                 sizeof(*na) + na_olen + dev->needed_tailroom;
265
266         reply = alloc_skb(len, GFP_ATOMIC);
267         if (!reply)
268                 return;
269
270         reply->protocol = htons(ETH_P_IPV6);
271         reply->dev = dev;
272         skb_reserve(reply, LL_RESERVED_SPACE(dev));
273         skb_push(reply, sizeof(struct ethhdr));
274         skb_set_mac_header(reply, 0);
275
276         daddr = eth_hdr(request)->h_source;
277
278         /* Do we need option processing ? */
279         ns_olen = request->len - (skb_network_offset(request) +
280                                   sizeof(struct ipv6hdr)) - sizeof(*ns);
281         for (i = 0; i < ns_olen - 1; i += (ns->opt[i + 1] << 3)) {
282                 if (!ns->opt[i + 1]) {
283                         kfree_skb(reply);
284                         return;
285                 }
286                 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
287                         daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
288                         break;
289                 }
290         }
291
292         /* Ethernet header */
293         ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
294         ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
295         eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
296         reply->protocol = htons(ETH_P_IPV6);
297
298         skb_pull(reply, sizeof(struct ethhdr));
299         skb_set_network_header(reply, 0);
300         skb_put(reply, sizeof(struct ipv6hdr));
301
302         /* IPv6 header */
303         pip6 = ipv6_hdr(reply);
304         memset(pip6, 0, sizeof(struct ipv6hdr));
305         pip6->version = 6;
306         pip6->priority = ipv6_hdr(request)->priority;
307         pip6->nexthdr = IPPROTO_ICMPV6;
308         pip6->hop_limit = 255;
309         pip6->daddr = ipv6_hdr(request)->saddr;
310         pip6->saddr = *(struct in6_addr *)n->primary_key;
311
312         skb_pull(reply, sizeof(struct ipv6hdr));
313         skb_set_transport_header(reply, 0);
314
315         na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
316
317         /* Neighbor Advertisement */
318         memset(na, 0, sizeof(*na) + na_olen);
319         na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
320         na->icmph.icmp6_router = (n->flags & NTF_ROUTER) ? 1 : 0;
321         na->icmph.icmp6_override = 1;
322         na->icmph.icmp6_solicited = 1;
323         na->target = ns->target;
324         ether_addr_copy(&na->opt[2], n->ha);
325         na->opt[0] = ND_OPT_TARGET_LL_ADDR;
326         na->opt[1] = na_olen >> 3;
327
328         na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
329                                                 &pip6->daddr,
330                                                 sizeof(*na) + na_olen,
331                                                 IPPROTO_ICMPV6,
332                                                 csum_partial(na, sizeof(*na) + na_olen, 0));
333
334         pip6->payload_len = htons(sizeof(*na) + na_olen);
335
336         skb_push(reply, sizeof(struct ipv6hdr));
337         skb_push(reply, sizeof(struct ethhdr));
338
339         reply->ip_summed = CHECKSUM_UNNECESSARY;
340
341         if (p)
342                 vg = nbp_vlan_group_rcu(p);
343         else
344                 vg = br_vlan_group_rcu(br);
345         pvid = br_get_pvid(vg);
346         if (pvid == (vlan_tci & VLAN_VID_MASK))
347                 vlan_tci = 0;
348
349         if (vlan_tci)
350                 __vlan_hwaccel_put_tag(reply, vlan_proto, vlan_tci);
351
352         netdev_dbg(dev, "nd send dev %s dst %pI6 dst_hw %pM src %pI6 src_hw %pM\n",
353                    dev->name, &pip6->daddr, daddr, &pip6->saddr, n->ha);
354
355         if (p) {
356                 dev_queue_xmit(reply);
357         } else {
358                 skb_reset_mac_header(reply);
359                 __skb_pull(reply, skb_network_offset(reply));
360                 reply->ip_summed = CHECKSUM_UNNECESSARY;
361                 reply->pkt_type = PACKET_HOST;
362
363                 netif_rx_ni(reply);
364         }
365 }
366
367 static int br_chk_addr_ip6(struct net_device *dev, void *data)
368 {
369         struct in6_addr *addr = (struct in6_addr *)data;
370
371         if (ipv6_chk_addr(dev_net(dev), addr, dev, 0))
372                 return 1;
373
374         return 0;
375 }
376
377 static bool br_is_local_ip6(struct net_device *dev, struct in6_addr *addr)
378
379 {
380         if (br_chk_addr_ip6(dev, addr))
381                 return true;
382
383         /* check if ip is configured on upper dev */
384         if (netdev_walk_all_upper_dev_rcu(dev, br_chk_addr_ip6, addr))
385                 return true;
386
387         return false;
388 }
389
390 void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
391                        u16 vid, struct net_bridge_port *p, struct nd_msg *msg)
392 {
393         struct net_device *dev = br->dev;
394         struct net_device *vlandev = NULL;
395         struct in6_addr *saddr, *daddr;
396         struct ipv6hdr *iphdr;
397         struct neighbour *n;
398
399         BR_INPUT_SKB_CB(skb)->proxyarp_replied = false;
400
401         if (p && (p->flags & BR_NEIGH_SUPPRESS))
402                 return;
403
404         if (msg->icmph.icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT &&
405             !msg->icmph.icmp6_solicited) {
406                 /* prevent flooding to neigh suppress ports */
407                 BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
408                 return;
409         }
410
411         if (msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
412                 return;
413
414         iphdr = ipv6_hdr(skb);
415         saddr = &iphdr->saddr;
416         daddr = &iphdr->daddr;
417
418         if (ipv6_addr_any(saddr) || !ipv6_addr_cmp(saddr, daddr)) {
419                 /* prevent flooding to neigh suppress ports */
420                 BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
421                 return;
422         }
423
424         if (vid != 0) {
425                 /* build neigh table lookup on the vlan device */
426                 vlandev = __vlan_find_dev_deep_rcu(br->dev, skb->vlan_proto,
427                                                    vid);
428                 if (!vlandev)
429                         return;
430         } else {
431                 vlandev = dev;
432         }
433
434         if (br_is_local_ip6(vlandev, &msg->target)) {
435                 /* its our own ip, so don't proxy reply
436                  * and don't forward to arp suppress ports
437                  */
438                 BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
439                 return;
440         }
441
442         n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, vlandev);
443         if (n) {
444                 struct net_bridge_fdb_entry *f;
445
446                 if (!(n->nud_state & NUD_VALID)) {
447                         neigh_release(n);
448                         return;
449                 }
450
451                 f = br_fdb_find_rcu(br, n->ha, vid);
452                 if (f) {
453                         bool replied = false;
454
455                         if (f->dst && (f->dst->flags & BR_NEIGH_SUPPRESS)) {
456                                 if (vid != 0)
457                                         br_nd_send(br, p, skb, n,
458                                                    skb->vlan_proto,
459                                                    skb_vlan_tag_get(skb), msg);
460                                 else
461                                         br_nd_send(br, p, skb, n, 0, 0, msg);
462                                 replied = true;
463                         }
464
465                         /* If we have replied or as long as we know the
466                          * mac, indicate to NEIGH_SUPPRESS ports that we
467                          * have replied
468                          */
469                         if (replied || br->neigh_suppress_enabled)
470                                 BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
471                 }
472                 neigh_release(n);
473         }
474 }
475 #endif