Merge remote-tracking branches 'asoc/fix/rt5663', 'asoc/fix/rt5665', 'asoc/fix/samsun...
[sfrench/cifs-2.6.git] / drivers / infiniband / core / addr.c
1 /*
2  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
3  * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4  * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5  * Copyright (c) 2005 Intel Corporation.  All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  */
35
36 #include <linux/mutex.h>
37 #include <linux/inetdevice.h>
38 #include <linux/slab.h>
39 #include <linux/workqueue.h>
40 #include <linux/module.h>
41 #include <net/arp.h>
42 #include <net/neighbour.h>
43 #include <net/route.h>
44 #include <net/netevent.h>
45 #include <net/addrconf.h>
46 #include <net/ip6_route.h>
47 #include <rdma/ib_addr.h>
48 #include <rdma/ib.h>
49 #include <rdma/rdma_netlink.h>
50 #include <net/netlink.h>
51
52 #include "core_priv.h"
53
54 struct addr_req {
55         struct list_head list;
56         struct sockaddr_storage src_addr;
57         struct sockaddr_storage dst_addr;
58         struct rdma_dev_addr *addr;
59         struct rdma_addr_client *client;
60         void *context;
61         void (*callback)(int status, struct sockaddr *src_addr,
62                          struct rdma_dev_addr *addr, void *context);
63         unsigned long timeout;
64         int status;
65         u32 seq;
66 };
67
68 static atomic_t ib_nl_addr_request_seq = ATOMIC_INIT(0);
69
70 static void process_req(struct work_struct *work);
71
72 static DEFINE_MUTEX(lock);
73 static LIST_HEAD(req_list);
74 static DECLARE_DELAYED_WORK(work, process_req);
75 static struct workqueue_struct *addr_wq;
76
77 static const struct nla_policy ib_nl_addr_policy[LS_NLA_TYPE_MAX] = {
78         [LS_NLA_TYPE_DGID] = {.type = NLA_BINARY,
79                 .len = sizeof(struct rdma_nla_ls_gid)},
80 };
81
82 static inline bool ib_nl_is_good_ip_resp(const struct nlmsghdr *nlh)
83 {
84         struct nlattr *tb[LS_NLA_TYPE_MAX] = {};
85         int ret;
86
87         if (nlh->nlmsg_flags & RDMA_NL_LS_F_ERR)
88                 return false;
89
90         ret = nla_parse(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
91                         nlmsg_len(nlh), ib_nl_addr_policy, NULL);
92         if (ret)
93                 return false;
94
95         return true;
96 }
97
98 static void ib_nl_process_good_ip_rsep(const struct nlmsghdr *nlh)
99 {
100         const struct nlattr *head, *curr;
101         union ib_gid gid;
102         struct addr_req *req;
103         int len, rem;
104         int found = 0;
105
106         head = (const struct nlattr *)nlmsg_data(nlh);
107         len = nlmsg_len(nlh);
108
109         nla_for_each_attr(curr, head, len, rem) {
110                 if (curr->nla_type == LS_NLA_TYPE_DGID)
111                         memcpy(&gid, nla_data(curr), nla_len(curr));
112         }
113
114         mutex_lock(&lock);
115         list_for_each_entry(req, &req_list, list) {
116                 if (nlh->nlmsg_seq != req->seq)
117                         continue;
118                 /* We set the DGID part, the rest was set earlier */
119                 rdma_addr_set_dgid(req->addr, &gid);
120                 req->status = 0;
121                 found = 1;
122                 break;
123         }
124         mutex_unlock(&lock);
125
126         if (!found)
127                 pr_info("Couldn't find request waiting for DGID: %pI6\n",
128                         &gid);
129 }
130
131 int ib_nl_handle_ip_res_resp(struct sk_buff *skb,
132                              struct netlink_callback *cb)
133 {
134         const struct nlmsghdr *nlh = (struct nlmsghdr *)cb->nlh;
135
136         if ((nlh->nlmsg_flags & NLM_F_REQUEST) ||
137             !(NETLINK_CB(skb).sk) ||
138             !netlink_capable(skb, CAP_NET_ADMIN))
139                 return -EPERM;
140
141         if (ib_nl_is_good_ip_resp(nlh))
142                 ib_nl_process_good_ip_rsep(nlh);
143
144         return skb->len;
145 }
146
147 static int ib_nl_ip_send_msg(struct rdma_dev_addr *dev_addr,
148                              const void *daddr,
149                              u32 seq, u16 family)
150 {
151         struct sk_buff *skb = NULL;
152         struct nlmsghdr *nlh;
153         struct rdma_ls_ip_resolve_header *header;
154         void *data;
155         size_t size;
156         int attrtype;
157         int len;
158
159         if (family == AF_INET) {
160                 size = sizeof(struct in_addr);
161                 attrtype = RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_IPV4;
162         } else {
163                 size = sizeof(struct in6_addr);
164                 attrtype = RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_IPV6;
165         }
166
167         len = nla_total_size(sizeof(size));
168         len += NLMSG_ALIGN(sizeof(*header));
169
170         skb = nlmsg_new(len, GFP_KERNEL);
171         if (!skb)
172                 return -ENOMEM;
173
174         data = ibnl_put_msg(skb, &nlh, seq, 0, RDMA_NL_LS,
175                             RDMA_NL_LS_OP_IP_RESOLVE, NLM_F_REQUEST);
176         if (!data) {
177                 nlmsg_free(skb);
178                 return -ENODATA;
179         }
180
181         /* Construct the family header first */
182         header = skb_put(skb, NLMSG_ALIGN(sizeof(*header)));
183         header->ifindex = dev_addr->bound_dev_if;
184         nla_put(skb, attrtype, size, daddr);
185
186         /* Repair the nlmsg header length */
187         nlmsg_end(skb, nlh);
188         ibnl_multicast(skb, nlh, RDMA_NL_GROUP_LS, GFP_KERNEL);
189
190         /* Make the request retry, so when we get the response from userspace
191          * we will have something.
192          */
193         return -ENODATA;
194 }
195
196 int rdma_addr_size(struct sockaddr *addr)
197 {
198         switch (addr->sa_family) {
199         case AF_INET:
200                 return sizeof(struct sockaddr_in);
201         case AF_INET6:
202                 return sizeof(struct sockaddr_in6);
203         case AF_IB:
204                 return sizeof(struct sockaddr_ib);
205         default:
206                 return 0;
207         }
208 }
209 EXPORT_SYMBOL(rdma_addr_size);
210
211 static struct rdma_addr_client self;
212
213 void rdma_addr_register_client(struct rdma_addr_client *client)
214 {
215         atomic_set(&client->refcount, 1);
216         init_completion(&client->comp);
217 }
218 EXPORT_SYMBOL(rdma_addr_register_client);
219
220 static inline void put_client(struct rdma_addr_client *client)
221 {
222         if (atomic_dec_and_test(&client->refcount))
223                 complete(&client->comp);
224 }
225
226 void rdma_addr_unregister_client(struct rdma_addr_client *client)
227 {
228         put_client(client);
229         wait_for_completion(&client->comp);
230 }
231 EXPORT_SYMBOL(rdma_addr_unregister_client);
232
233 int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev,
234                      const unsigned char *dst_dev_addr)
235 {
236         dev_addr->dev_type = dev->type;
237         memcpy(dev_addr->src_dev_addr, dev->dev_addr, MAX_ADDR_LEN);
238         memcpy(dev_addr->broadcast, dev->broadcast, MAX_ADDR_LEN);
239         if (dst_dev_addr)
240                 memcpy(dev_addr->dst_dev_addr, dst_dev_addr, MAX_ADDR_LEN);
241         dev_addr->bound_dev_if = dev->ifindex;
242         return 0;
243 }
244 EXPORT_SYMBOL(rdma_copy_addr);
245
246 int rdma_translate_ip(const struct sockaddr *addr,
247                       struct rdma_dev_addr *dev_addr,
248                       u16 *vlan_id)
249 {
250         struct net_device *dev;
251         int ret = -EADDRNOTAVAIL;
252
253         if (dev_addr->bound_dev_if) {
254                 dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
255                 if (!dev)
256                         return -ENODEV;
257                 ret = rdma_copy_addr(dev_addr, dev, NULL);
258                 dev_put(dev);
259                 return ret;
260         }
261
262         switch (addr->sa_family) {
263         case AF_INET:
264                 dev = ip_dev_find(dev_addr->net,
265                         ((const struct sockaddr_in *)addr)->sin_addr.s_addr);
266
267                 if (!dev)
268                         return ret;
269
270                 ret = rdma_copy_addr(dev_addr, dev, NULL);
271                 dev_addr->bound_dev_if = dev->ifindex;
272                 if (vlan_id)
273                         *vlan_id = rdma_vlan_dev_vlan_id(dev);
274                 dev_put(dev);
275                 break;
276 #if IS_ENABLED(CONFIG_IPV6)
277         case AF_INET6:
278                 rcu_read_lock();
279                 for_each_netdev_rcu(dev_addr->net, dev) {
280                         if (ipv6_chk_addr(dev_addr->net,
281                                           &((const struct sockaddr_in6 *)addr)->sin6_addr,
282                                           dev, 1)) {
283                                 ret = rdma_copy_addr(dev_addr, dev, NULL);
284                                 dev_addr->bound_dev_if = dev->ifindex;
285                                 if (vlan_id)
286                                         *vlan_id = rdma_vlan_dev_vlan_id(dev);
287                                 break;
288                         }
289                 }
290                 rcu_read_unlock();
291                 break;
292 #endif
293         }
294         return ret;
295 }
296 EXPORT_SYMBOL(rdma_translate_ip);
297
298 static void set_timeout(unsigned long time)
299 {
300         unsigned long delay;
301
302         delay = time - jiffies;
303         if ((long)delay < 0)
304                 delay = 0;
305
306         mod_delayed_work(addr_wq, &work, delay);
307 }
308
309 static void queue_req(struct addr_req *req)
310 {
311         struct addr_req *temp_req;
312
313         mutex_lock(&lock);
314         list_for_each_entry_reverse(temp_req, &req_list, list) {
315                 if (time_after_eq(req->timeout, temp_req->timeout))
316                         break;
317         }
318
319         list_add(&req->list, &temp_req->list);
320
321         if (req_list.next == &req->list)
322                 set_timeout(req->timeout);
323         mutex_unlock(&lock);
324 }
325
326 static int ib_nl_fetch_ha(struct dst_entry *dst, struct rdma_dev_addr *dev_addr,
327                           const void *daddr, u32 seq, u16 family)
328 {
329         if (ibnl_chk_listeners(RDMA_NL_GROUP_LS))
330                 return -EADDRNOTAVAIL;
331
332         /* We fill in what we can, the response will fill the rest */
333         rdma_copy_addr(dev_addr, dst->dev, NULL);
334         return ib_nl_ip_send_msg(dev_addr, daddr, seq, family);
335 }
336
337 static int dst_fetch_ha(struct dst_entry *dst, struct rdma_dev_addr *dev_addr,
338                         const void *daddr)
339 {
340         struct neighbour *n;
341         int ret;
342
343         n = dst_neigh_lookup(dst, daddr);
344
345         rcu_read_lock();
346         if (!n || !(n->nud_state & NUD_VALID)) {
347                 if (n)
348                         neigh_event_send(n, NULL);
349                 ret = -ENODATA;
350         } else {
351                 ret = rdma_copy_addr(dev_addr, dst->dev, n->ha);
352         }
353         rcu_read_unlock();
354
355         if (n)
356                 neigh_release(n);
357
358         return ret;
359 }
360
361 static bool has_gateway(struct dst_entry *dst, sa_family_t family)
362 {
363         struct rtable *rt;
364         struct rt6_info *rt6;
365
366         if (family == AF_INET) {
367                 rt = container_of(dst, struct rtable, dst);
368                 return rt->rt_uses_gateway;
369         }
370
371         rt6 = container_of(dst, struct rt6_info, dst);
372         return rt6->rt6i_flags & RTF_GATEWAY;
373 }
374
375 static int fetch_ha(struct dst_entry *dst, struct rdma_dev_addr *dev_addr,
376                     const struct sockaddr *dst_in, u32 seq)
377 {
378         const struct sockaddr_in *dst_in4 =
379                 (const struct sockaddr_in *)dst_in;
380         const struct sockaddr_in6 *dst_in6 =
381                 (const struct sockaddr_in6 *)dst_in;
382         const void *daddr = (dst_in->sa_family == AF_INET) ?
383                 (const void *)&dst_in4->sin_addr.s_addr :
384                 (const void *)&dst_in6->sin6_addr;
385         sa_family_t family = dst_in->sa_family;
386
387         /* Gateway + ARPHRD_INFINIBAND -> IB router */
388         if (has_gateway(dst, family) && dst->dev->type == ARPHRD_INFINIBAND)
389                 return ib_nl_fetch_ha(dst, dev_addr, daddr, seq, family);
390         else
391                 return dst_fetch_ha(dst, dev_addr, daddr);
392 }
393
394 static int addr4_resolve(struct sockaddr_in *src_in,
395                          const struct sockaddr_in *dst_in,
396                          struct rdma_dev_addr *addr,
397                          struct rtable **prt)
398 {
399         __be32 src_ip = src_in->sin_addr.s_addr;
400         __be32 dst_ip = dst_in->sin_addr.s_addr;
401         struct rtable *rt;
402         struct flowi4 fl4;
403         int ret;
404
405         memset(&fl4, 0, sizeof(fl4));
406         fl4.daddr = dst_ip;
407         fl4.saddr = src_ip;
408         fl4.flowi4_oif = addr->bound_dev_if;
409         rt = ip_route_output_key(addr->net, &fl4);
410         ret = PTR_ERR_OR_ZERO(rt);
411         if (ret)
412                 return ret;
413
414         src_in->sin_family = AF_INET;
415         src_in->sin_addr.s_addr = fl4.saddr;
416
417         /* If there's a gateway and type of device not ARPHRD_INFINIBAND, we're
418          * definitely in RoCE v2 (as RoCE v1 isn't routable) set the network
419          * type accordingly.
420          */
421         if (rt->rt_uses_gateway && rt->dst.dev->type != ARPHRD_INFINIBAND)
422                 addr->network = RDMA_NETWORK_IPV4;
423
424         addr->hoplimit = ip4_dst_hoplimit(&rt->dst);
425
426         *prt = rt;
427         return 0;
428 }
429
430 #if IS_ENABLED(CONFIG_IPV6)
431 static int addr6_resolve(struct sockaddr_in6 *src_in,
432                          const struct sockaddr_in6 *dst_in,
433                          struct rdma_dev_addr *addr,
434                          struct dst_entry **pdst)
435 {
436         struct flowi6 fl6;
437         struct dst_entry *dst;
438         struct rt6_info *rt;
439         int ret;
440
441         memset(&fl6, 0, sizeof fl6);
442         fl6.daddr = dst_in->sin6_addr;
443         fl6.saddr = src_in->sin6_addr;
444         fl6.flowi6_oif = addr->bound_dev_if;
445
446         ret = ipv6_stub->ipv6_dst_lookup(addr->net, NULL, &dst, &fl6);
447         if (ret < 0)
448                 return ret;
449
450         rt = (struct rt6_info *)dst;
451         if (ipv6_addr_any(&src_in->sin6_addr)) {
452                 src_in->sin6_family = AF_INET6;
453                 src_in->sin6_addr = fl6.saddr;
454         }
455
456         /* If there's a gateway and type of device not ARPHRD_INFINIBAND, we're
457          * definitely in RoCE v2 (as RoCE v1 isn't routable) set the network
458          * type accordingly.
459          */
460         if (rt->rt6i_flags & RTF_GATEWAY &&
461             ip6_dst_idev(dst)->dev->type != ARPHRD_INFINIBAND)
462                 addr->network = RDMA_NETWORK_IPV6;
463
464         addr->hoplimit = ip6_dst_hoplimit(dst);
465
466         *pdst = dst;
467         return 0;
468 }
469 #else
470 static int addr6_resolve(struct sockaddr_in6 *src_in,
471                          const struct sockaddr_in6 *dst_in,
472                          struct rdma_dev_addr *addr,
473                          struct dst_entry **pdst)
474 {
475         return -EADDRNOTAVAIL;
476 }
477 #endif
478
479 static int addr_resolve_neigh(struct dst_entry *dst,
480                               const struct sockaddr *dst_in,
481                               struct rdma_dev_addr *addr,
482                               u32 seq)
483 {
484         if (dst->dev->flags & IFF_LOOPBACK) {
485                 int ret;
486
487                 ret = rdma_translate_ip(dst_in, addr, NULL);
488                 if (!ret)
489                         memcpy(addr->dst_dev_addr, addr->src_dev_addr,
490                                MAX_ADDR_LEN);
491
492                 return ret;
493         }
494
495         /* If the device doesn't do ARP internally */
496         if (!(dst->dev->flags & IFF_NOARP))
497                 return fetch_ha(dst, addr, dst_in, seq);
498
499         return rdma_copy_addr(addr, dst->dev, NULL);
500 }
501
502 static int addr_resolve(struct sockaddr *src_in,
503                         const struct sockaddr *dst_in,
504                         struct rdma_dev_addr *addr,
505                         bool resolve_neigh,
506                         u32 seq)
507 {
508         struct net_device *ndev;
509         struct dst_entry *dst;
510         int ret;
511
512         if (!addr->net) {
513                 pr_warn_ratelimited("%s: missing namespace\n", __func__);
514                 return -EINVAL;
515         }
516
517         if (src_in->sa_family == AF_INET) {
518                 struct rtable *rt = NULL;
519                 const struct sockaddr_in *dst_in4 =
520                         (const struct sockaddr_in *)dst_in;
521
522                 ret = addr4_resolve((struct sockaddr_in *)src_in,
523                                     dst_in4, addr, &rt);
524                 if (ret)
525                         return ret;
526
527                 if (resolve_neigh)
528                         ret = addr_resolve_neigh(&rt->dst, dst_in, addr, seq);
529
530                 if (addr->bound_dev_if) {
531                         ndev = dev_get_by_index(addr->net, addr->bound_dev_if);
532                 } else {
533                         ndev = rt->dst.dev;
534                         dev_hold(ndev);
535                 }
536
537                 ip_rt_put(rt);
538         } else {
539                 const struct sockaddr_in6 *dst_in6 =
540                         (const struct sockaddr_in6 *)dst_in;
541
542                 ret = addr6_resolve((struct sockaddr_in6 *)src_in,
543                                     dst_in6, addr,
544                                     &dst);
545                 if (ret)
546                         return ret;
547
548                 if (resolve_neigh)
549                         ret = addr_resolve_neigh(dst, dst_in, addr, seq);
550
551                 if (addr->bound_dev_if) {
552                         ndev = dev_get_by_index(addr->net, addr->bound_dev_if);
553                 } else {
554                         ndev = dst->dev;
555                         dev_hold(ndev);
556                 }
557
558                 dst_release(dst);
559         }
560
561         if (ndev->flags & IFF_LOOPBACK) {
562                 ret = rdma_translate_ip(dst_in, addr, NULL);
563                 /*
564                  * Put the loopback device and get the translated
565                  * device instead.
566                  */
567                 dev_put(ndev);
568                 ndev = dev_get_by_index(addr->net, addr->bound_dev_if);
569         } else {
570                 addr->bound_dev_if = ndev->ifindex;
571         }
572         dev_put(ndev);
573
574         return ret;
575 }
576
577 static void process_req(struct work_struct *work)
578 {
579         struct addr_req *req, *temp_req;
580         struct sockaddr *src_in, *dst_in;
581         struct list_head done_list;
582
583         INIT_LIST_HEAD(&done_list);
584
585         mutex_lock(&lock);
586         list_for_each_entry_safe(req, temp_req, &req_list, list) {
587                 if (req->status == -ENODATA) {
588                         src_in = (struct sockaddr *) &req->src_addr;
589                         dst_in = (struct sockaddr *) &req->dst_addr;
590                         req->status = addr_resolve(src_in, dst_in, req->addr,
591                                                    true, req->seq);
592                         if (req->status && time_after_eq(jiffies, req->timeout))
593                                 req->status = -ETIMEDOUT;
594                         else if (req->status == -ENODATA)
595                                 continue;
596                 }
597                 list_move_tail(&req->list, &done_list);
598         }
599
600         if (!list_empty(&req_list)) {
601                 req = list_entry(req_list.next, struct addr_req, list);
602                 set_timeout(req->timeout);
603         }
604         mutex_unlock(&lock);
605
606         list_for_each_entry_safe(req, temp_req, &done_list, list) {
607                 list_del(&req->list);
608                 req->callback(req->status, (struct sockaddr *) &req->src_addr,
609                         req->addr, req->context);
610                 put_client(req->client);
611                 kfree(req);
612         }
613 }
614
615 int rdma_resolve_ip(struct rdma_addr_client *client,
616                     struct sockaddr *src_addr, struct sockaddr *dst_addr,
617                     struct rdma_dev_addr *addr, int timeout_ms,
618                     void (*callback)(int status, struct sockaddr *src_addr,
619                                      struct rdma_dev_addr *addr, void *context),
620                     void *context)
621 {
622         struct sockaddr *src_in, *dst_in;
623         struct addr_req *req;
624         int ret = 0;
625
626         req = kzalloc(sizeof *req, GFP_KERNEL);
627         if (!req)
628                 return -ENOMEM;
629
630         src_in = (struct sockaddr *) &req->src_addr;
631         dst_in = (struct sockaddr *) &req->dst_addr;
632
633         if (src_addr) {
634                 if (src_addr->sa_family != dst_addr->sa_family) {
635                         ret = -EINVAL;
636                         goto err;
637                 }
638
639                 memcpy(src_in, src_addr, rdma_addr_size(src_addr));
640         } else {
641                 src_in->sa_family = dst_addr->sa_family;
642         }
643
644         memcpy(dst_in, dst_addr, rdma_addr_size(dst_addr));
645         req->addr = addr;
646         req->callback = callback;
647         req->context = context;
648         req->client = client;
649         atomic_inc(&client->refcount);
650         req->seq = (u32)atomic_inc_return(&ib_nl_addr_request_seq);
651
652         req->status = addr_resolve(src_in, dst_in, addr, true, req->seq);
653         switch (req->status) {
654         case 0:
655                 req->timeout = jiffies;
656                 queue_req(req);
657                 break;
658         case -ENODATA:
659                 req->timeout = msecs_to_jiffies(timeout_ms) + jiffies;
660                 queue_req(req);
661                 break;
662         default:
663                 ret = req->status;
664                 atomic_dec(&client->refcount);
665                 goto err;
666         }
667         return ret;
668 err:
669         kfree(req);
670         return ret;
671 }
672 EXPORT_SYMBOL(rdma_resolve_ip);
673
674 int rdma_resolve_ip_route(struct sockaddr *src_addr,
675                           const struct sockaddr *dst_addr,
676                           struct rdma_dev_addr *addr)
677 {
678         struct sockaddr_storage ssrc_addr = {};
679         struct sockaddr *src_in = (struct sockaddr *)&ssrc_addr;
680
681         if (src_addr) {
682                 if (src_addr->sa_family != dst_addr->sa_family)
683                         return -EINVAL;
684
685                 memcpy(src_in, src_addr, rdma_addr_size(src_addr));
686         } else {
687                 src_in->sa_family = dst_addr->sa_family;
688         }
689
690         return addr_resolve(src_in, dst_addr, addr, false, 0);
691 }
692 EXPORT_SYMBOL(rdma_resolve_ip_route);
693
694 void rdma_addr_cancel(struct rdma_dev_addr *addr)
695 {
696         struct addr_req *req, *temp_req;
697
698         mutex_lock(&lock);
699         list_for_each_entry_safe(req, temp_req, &req_list, list) {
700                 if (req->addr == addr) {
701                         req->status = -ECANCELED;
702                         req->timeout = jiffies;
703                         list_move(&req->list, &req_list);
704                         set_timeout(req->timeout);
705                         break;
706                 }
707         }
708         mutex_unlock(&lock);
709 }
710 EXPORT_SYMBOL(rdma_addr_cancel);
711
712 struct resolve_cb_context {
713         struct rdma_dev_addr *addr;
714         struct completion comp;
715         int status;
716 };
717
718 static void resolve_cb(int status, struct sockaddr *src_addr,
719              struct rdma_dev_addr *addr, void *context)
720 {
721         if (!status)
722                 memcpy(((struct resolve_cb_context *)context)->addr,
723                        addr, sizeof(struct rdma_dev_addr));
724         ((struct resolve_cb_context *)context)->status = status;
725         complete(&((struct resolve_cb_context *)context)->comp);
726 }
727
728 int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
729                                  const union ib_gid *dgid,
730                                  u8 *dmac, u16 *vlan_id, int *if_index,
731                                  int *hoplimit)
732 {
733         int ret = 0;
734         struct rdma_dev_addr dev_addr;
735         struct resolve_cb_context ctx;
736         struct net_device *dev;
737
738         union {
739                 struct sockaddr     _sockaddr;
740                 struct sockaddr_in  _sockaddr_in;
741                 struct sockaddr_in6 _sockaddr_in6;
742         } sgid_addr, dgid_addr;
743
744
745         rdma_gid2ip(&sgid_addr._sockaddr, sgid);
746         rdma_gid2ip(&dgid_addr._sockaddr, dgid);
747
748         memset(&dev_addr, 0, sizeof(dev_addr));
749         if (if_index)
750                 dev_addr.bound_dev_if = *if_index;
751         dev_addr.net = &init_net;
752
753         ctx.addr = &dev_addr;
754         init_completion(&ctx.comp);
755         ret = rdma_resolve_ip(&self, &sgid_addr._sockaddr, &dgid_addr._sockaddr,
756                         &dev_addr, 1000, resolve_cb, &ctx);
757         if (ret)
758                 return ret;
759
760         wait_for_completion(&ctx.comp);
761
762         ret = ctx.status;
763         if (ret)
764                 return ret;
765
766         memcpy(dmac, dev_addr.dst_dev_addr, ETH_ALEN);
767         dev = dev_get_by_index(&init_net, dev_addr.bound_dev_if);
768         if (!dev)
769                 return -ENODEV;
770         if (if_index)
771                 *if_index = dev_addr.bound_dev_if;
772         if (vlan_id)
773                 *vlan_id = rdma_vlan_dev_vlan_id(dev);
774         if (hoplimit)
775                 *hoplimit = dev_addr.hoplimit;
776         dev_put(dev);
777         return ret;
778 }
779 EXPORT_SYMBOL(rdma_addr_find_l2_eth_by_grh);
780
781 int rdma_addr_find_smac_by_sgid(union ib_gid *sgid, u8 *smac, u16 *vlan_id)
782 {
783         int ret = 0;
784         struct rdma_dev_addr dev_addr;
785         union {
786                 struct sockaddr     _sockaddr;
787                 struct sockaddr_in  _sockaddr_in;
788                 struct sockaddr_in6 _sockaddr_in6;
789         } gid_addr;
790
791         rdma_gid2ip(&gid_addr._sockaddr, sgid);
792
793         memset(&dev_addr, 0, sizeof(dev_addr));
794         dev_addr.net = &init_net;
795         ret = rdma_translate_ip(&gid_addr._sockaddr, &dev_addr, vlan_id);
796         if (ret)
797                 return ret;
798
799         memcpy(smac, dev_addr.src_dev_addr, ETH_ALEN);
800         return ret;
801 }
802 EXPORT_SYMBOL(rdma_addr_find_smac_by_sgid);
803
804 static int netevent_callback(struct notifier_block *self, unsigned long event,
805         void *ctx)
806 {
807         if (event == NETEVENT_NEIGH_UPDATE) {
808                 struct neighbour *neigh = ctx;
809
810                 if (neigh->nud_state & NUD_VALID) {
811                         set_timeout(jiffies);
812                 }
813         }
814         return 0;
815 }
816
817 static struct notifier_block nb = {
818         .notifier_call = netevent_callback
819 };
820
821 int addr_init(void)
822 {
823         addr_wq = alloc_workqueue("ib_addr", WQ_MEM_RECLAIM, 0);
824         if (!addr_wq)
825                 return -ENOMEM;
826
827         register_netevent_notifier(&nb);
828         rdma_addr_register_client(&self);
829
830         return 0;
831 }
832
833 void addr_cleanup(void)
834 {
835         rdma_addr_unregister_client(&self);
836         unregister_netevent_notifier(&nb);
837         destroy_workqueue(addr_wq);
838 }