Loading...
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 <net/arp.h>
41#include <net/neighbour.h>
42#include <net/route.h>
43#include <net/netevent.h>
44#include <net/ipv6_stubs.h>
45#include <net/ip6_route.h>
46#include <rdma/ib_addr.h>
47#include <rdma/ib_cache.h>
48#include <rdma/ib_sa.h>
49#include <rdma/ib.h>
50#include <rdma/rdma_netlink.h>
51#include <net/netlink.h>
52
53#include "core_priv.h"
54
55struct addr_req {
56 struct list_head list;
57 struct sockaddr_storage src_addr;
58 struct sockaddr_storage dst_addr;
59 struct rdma_dev_addr *addr;
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 struct delayed_work work;
65 bool resolve_by_gid_attr; /* Consider gid attr in resolve phase */
66 int status;
67 u32 seq;
68};
69
70static atomic_t ib_nl_addr_request_seq = ATOMIC_INIT(0);
71
72static DEFINE_SPINLOCK(lock);
73static LIST_HEAD(req_list);
74static struct workqueue_struct *addr_wq;
75
76static const struct nla_policy ib_nl_addr_policy[LS_NLA_TYPE_MAX] = {
77 [LS_NLA_TYPE_DGID] = {.type = NLA_BINARY,
78 .len = sizeof(struct rdma_nla_ls_gid),
79 .validation_type = NLA_VALIDATE_MIN,
80 .min = sizeof(struct rdma_nla_ls_gid)},
81};
82
83static inline bool ib_nl_is_good_ip_resp(const struct nlmsghdr *nlh)
84{
85 struct nlattr *tb[LS_NLA_TYPE_MAX] = {};
86 int ret;
87
88 if (nlh->nlmsg_flags & RDMA_NL_LS_F_ERR)
89 return false;
90
91 ret = nla_parse_deprecated(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
92 nlmsg_len(nlh), ib_nl_addr_policy, NULL);
93 if (ret)
94 return false;
95
96 return true;
97}
98
99static void ib_nl_process_good_ip_rsep(const struct nlmsghdr *nlh)
100{
101 const struct nlattr *head, *curr;
102 union ib_gid gid;
103 struct addr_req *req;
104 int len, rem;
105 int found = 0;
106
107 head = (const struct nlattr *)nlmsg_data(nlh);
108 len = nlmsg_len(nlh);
109
110 nla_for_each_attr(curr, head, len, rem) {
111 if (curr->nla_type == LS_NLA_TYPE_DGID)
112 memcpy(&gid, nla_data(curr), nla_len(curr));
113 }
114
115 spin_lock_bh(&lock);
116 list_for_each_entry(req, &req_list, list) {
117 if (nlh->nlmsg_seq != req->seq)
118 continue;
119 /* We set the DGID part, the rest was set earlier */
120 rdma_addr_set_dgid(req->addr, &gid);
121 req->status = 0;
122 found = 1;
123 break;
124 }
125 spin_unlock_bh(&lock);
126
127 if (!found)
128 pr_info("Couldn't find request waiting for DGID: %pI6\n",
129 &gid);
130}
131
132int ib_nl_handle_ip_res_resp(struct sk_buff *skb,
133 struct nlmsghdr *nlh,
134 struct netlink_ext_ack *extack)
135{
136 if ((nlh->nlmsg_flags & NLM_F_REQUEST) ||
137 !(NETLINK_CB(skb).sk))
138 return -EPERM;
139
140 if (ib_nl_is_good_ip_resp(nlh))
141 ib_nl_process_good_ip_rsep(nlh);
142
143 return 0;
144}
145
146static int ib_nl_ip_send_msg(struct rdma_dev_addr *dev_addr,
147 const void *daddr,
148 u32 seq, u16 family)
149{
150 struct sk_buff *skb = NULL;
151 struct nlmsghdr *nlh;
152 struct rdma_ls_ip_resolve_header *header;
153 void *data;
154 size_t size;
155 int attrtype;
156 int len;
157
158 if (family == AF_INET) {
159 size = sizeof(struct in_addr);
160 attrtype = RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_IPV4;
161 } else {
162 size = sizeof(struct in6_addr);
163 attrtype = RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_IPV6;
164 }
165
166 len = nla_total_size(sizeof(size));
167 len += NLMSG_ALIGN(sizeof(*header));
168
169 skb = nlmsg_new(len, GFP_KERNEL);
170 if (!skb)
171 return -ENOMEM;
172
173 data = ibnl_put_msg(skb, &nlh, seq, 0, RDMA_NL_LS,
174 RDMA_NL_LS_OP_IP_RESOLVE, NLM_F_REQUEST);
175 if (!data) {
176 nlmsg_free(skb);
177 return -ENODATA;
178 }
179
180 /* Construct the family header first */
181 header = skb_put(skb, NLMSG_ALIGN(sizeof(*header)));
182 header->ifindex = dev_addr->bound_dev_if;
183 nla_put(skb, attrtype, size, daddr);
184
185 /* Repair the nlmsg header length */
186 nlmsg_end(skb, nlh);
187 rdma_nl_multicast(&init_net, skb, RDMA_NL_GROUP_LS, GFP_KERNEL);
188
189 /* Make the request retry, so when we get the response from userspace
190 * we will have something.
191 */
192 return -ENODATA;
193}
194
195int rdma_addr_size(const struct sockaddr *addr)
196{
197 switch (addr->sa_family) {
198 case AF_INET:
199 return sizeof(struct sockaddr_in);
200 case AF_INET6:
201 return sizeof(struct sockaddr_in6);
202 case AF_IB:
203 return sizeof(struct sockaddr_ib);
204 default:
205 return 0;
206 }
207}
208EXPORT_SYMBOL(rdma_addr_size);
209
210int rdma_addr_size_in6(struct sockaddr_in6 *addr)
211{
212 int ret = rdma_addr_size((struct sockaddr *) addr);
213
214 return ret <= sizeof(*addr) ? ret : 0;
215}
216EXPORT_SYMBOL(rdma_addr_size_in6);
217
218int rdma_addr_size_kss(struct __kernel_sockaddr_storage *addr)
219{
220 int ret = rdma_addr_size((struct sockaddr *) addr);
221
222 return ret <= sizeof(*addr) ? ret : 0;
223}
224EXPORT_SYMBOL(rdma_addr_size_kss);
225
226/**
227 * rdma_copy_src_l2_addr - Copy netdevice source addresses
228 * @dev_addr: Destination address pointer where to copy the addresses
229 * @dev: Netdevice whose source addresses to copy
230 *
231 * rdma_copy_src_l2_addr() copies source addresses from the specified netdevice.
232 * This includes unicast address, broadcast address, device type and
233 * interface index.
234 */
235void rdma_copy_src_l2_addr(struct rdma_dev_addr *dev_addr,
236 const struct net_device *dev)
237{
238 dev_addr->dev_type = dev->type;
239 memcpy(dev_addr->src_dev_addr, dev->dev_addr, MAX_ADDR_LEN);
240 memcpy(dev_addr->broadcast, dev->broadcast, MAX_ADDR_LEN);
241 dev_addr->bound_dev_if = dev->ifindex;
242}
243EXPORT_SYMBOL(rdma_copy_src_l2_addr);
244
245static struct net_device *
246rdma_find_ndev_for_src_ip_rcu(struct net *net, const struct sockaddr *src_in)
247{
248 struct net_device *dev = NULL;
249 int ret = -EADDRNOTAVAIL;
250
251 switch (src_in->sa_family) {
252 case AF_INET:
253 dev = __ip_dev_find(net,
254 ((const struct sockaddr_in *)src_in)->sin_addr.s_addr,
255 false);
256 if (dev)
257 ret = 0;
258 break;
259#if IS_ENABLED(CONFIG_IPV6)
260 case AF_INET6:
261 for_each_netdev_rcu(net, dev) {
262 if (ipv6_chk_addr(net,
263 &((const struct sockaddr_in6 *)src_in)->sin6_addr,
264 dev, 1)) {
265 ret = 0;
266 break;
267 }
268 }
269 break;
270#endif
271 }
272 return ret ? ERR_PTR(ret) : dev;
273}
274
275int rdma_translate_ip(const struct sockaddr *addr,
276 struct rdma_dev_addr *dev_addr)
277{
278 struct net_device *dev;
279
280 if (dev_addr->bound_dev_if) {
281 dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
282 if (!dev)
283 return -ENODEV;
284 rdma_copy_src_l2_addr(dev_addr, dev);
285 dev_put(dev);
286 return 0;
287 }
288
289 rcu_read_lock();
290 dev = rdma_find_ndev_for_src_ip_rcu(dev_addr->net, addr);
291 if (!IS_ERR(dev))
292 rdma_copy_src_l2_addr(dev_addr, dev);
293 rcu_read_unlock();
294 return PTR_ERR_OR_ZERO(dev);
295}
296EXPORT_SYMBOL(rdma_translate_ip);
297
298static void set_timeout(struct addr_req *req, 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, &req->work, delay);
307}
308
309static void queue_req(struct addr_req *req)
310{
311 spin_lock_bh(&lock);
312 list_add_tail(&req->list, &req_list);
313 set_timeout(req, req->timeout);
314 spin_unlock_bh(&lock);
315}
316
317static int ib_nl_fetch_ha(struct rdma_dev_addr *dev_addr,
318 const void *daddr, u32 seq, u16 family)
319{
320 if (!rdma_nl_chk_listeners(RDMA_NL_GROUP_LS))
321 return -EADDRNOTAVAIL;
322
323 return ib_nl_ip_send_msg(dev_addr, daddr, seq, family);
324}
325
326static int dst_fetch_ha(const struct dst_entry *dst,
327 struct rdma_dev_addr *dev_addr,
328 const void *daddr)
329{
330 struct neighbour *n;
331 int ret = 0;
332
333 n = dst_neigh_lookup(dst, daddr);
334 if (!n)
335 return -ENODATA;
336
337 if (!(n->nud_state & NUD_VALID)) {
338 neigh_event_send(n, NULL);
339 ret = -ENODATA;
340 } else {
341 neigh_ha_snapshot(dev_addr->dst_dev_addr, n, dst->dev);
342 }
343
344 neigh_release(n);
345
346 return ret;
347}
348
349static bool has_gateway(const struct dst_entry *dst, sa_family_t family)
350{
351 if (family == AF_INET)
352 return dst_rtable(dst)->rt_uses_gateway;
353
354 return dst_rt6_info(dst)->rt6i_flags & RTF_GATEWAY;
355}
356
357static int fetch_ha(const struct dst_entry *dst, struct rdma_dev_addr *dev_addr,
358 const struct sockaddr *dst_in, u32 seq)
359{
360 const struct sockaddr_in *dst_in4 =
361 (const struct sockaddr_in *)dst_in;
362 const struct sockaddr_in6 *dst_in6 =
363 (const struct sockaddr_in6 *)dst_in;
364 const void *daddr = (dst_in->sa_family == AF_INET) ?
365 (const void *)&dst_in4->sin_addr.s_addr :
366 (const void *)&dst_in6->sin6_addr;
367 sa_family_t family = dst_in->sa_family;
368
369 might_sleep();
370
371 /* If we have a gateway in IB mode then it must be an IB network */
372 if (has_gateway(dst, family) && dev_addr->network == RDMA_NETWORK_IB)
373 return ib_nl_fetch_ha(dev_addr, daddr, seq, family);
374 else
375 return dst_fetch_ha(dst, dev_addr, daddr);
376}
377
378static int addr4_resolve(struct sockaddr *src_sock,
379 const struct sockaddr *dst_sock,
380 struct rdma_dev_addr *addr,
381 struct rtable **prt)
382{
383 struct sockaddr_in *src_in = (struct sockaddr_in *)src_sock;
384 const struct sockaddr_in *dst_in =
385 (const struct sockaddr_in *)dst_sock;
386
387 __be32 src_ip = src_in->sin_addr.s_addr;
388 __be32 dst_ip = dst_in->sin_addr.s_addr;
389 struct rtable *rt;
390 struct flowi4 fl4;
391 int ret;
392
393 memset(&fl4, 0, sizeof(fl4));
394 fl4.daddr = dst_ip;
395 fl4.saddr = src_ip;
396 fl4.flowi4_oif = addr->bound_dev_if;
397 rt = ip_route_output_key(addr->net, &fl4);
398 ret = PTR_ERR_OR_ZERO(rt);
399 if (ret)
400 return ret;
401
402 src_in->sin_addr.s_addr = fl4.saddr;
403
404 addr->hoplimit = ip4_dst_hoplimit(&rt->dst);
405
406 *prt = rt;
407 return 0;
408}
409
410#if IS_ENABLED(CONFIG_IPV6)
411static int addr6_resolve(struct sockaddr *src_sock,
412 const struct sockaddr *dst_sock,
413 struct rdma_dev_addr *addr,
414 struct dst_entry **pdst)
415{
416 struct sockaddr_in6 *src_in = (struct sockaddr_in6 *)src_sock;
417 const struct sockaddr_in6 *dst_in =
418 (const struct sockaddr_in6 *)dst_sock;
419 struct flowi6 fl6;
420 struct dst_entry *dst;
421
422 memset(&fl6, 0, sizeof fl6);
423 fl6.daddr = dst_in->sin6_addr;
424 fl6.saddr = src_in->sin6_addr;
425 fl6.flowi6_oif = addr->bound_dev_if;
426
427 dst = ipv6_stub->ipv6_dst_lookup_flow(addr->net, NULL, &fl6, NULL);
428 if (IS_ERR(dst))
429 return PTR_ERR(dst);
430
431 if (ipv6_addr_any(&src_in->sin6_addr))
432 src_in->sin6_addr = fl6.saddr;
433
434 addr->hoplimit = ip6_dst_hoplimit(dst);
435
436 *pdst = dst;
437 return 0;
438}
439#else
440static int addr6_resolve(struct sockaddr *src_sock,
441 const struct sockaddr *dst_sock,
442 struct rdma_dev_addr *addr,
443 struct dst_entry **pdst)
444{
445 return -EADDRNOTAVAIL;
446}
447#endif
448
449static int addr_resolve_neigh(const struct dst_entry *dst,
450 const struct sockaddr *dst_in,
451 struct rdma_dev_addr *addr,
452 unsigned int ndev_flags,
453 u32 seq)
454{
455 int ret = 0;
456
457 if (ndev_flags & IFF_LOOPBACK) {
458 memcpy(addr->dst_dev_addr, addr->src_dev_addr, MAX_ADDR_LEN);
459 } else {
460 if (!(ndev_flags & IFF_NOARP)) {
461 /* If the device doesn't do ARP internally */
462 ret = fetch_ha(dst, addr, dst_in, seq);
463 }
464 }
465 return ret;
466}
467
468static int copy_src_l2_addr(struct rdma_dev_addr *dev_addr,
469 const struct sockaddr *dst_in,
470 const struct dst_entry *dst,
471 const struct net_device *ndev)
472{
473 int ret = 0;
474
475 if (dst->dev->flags & IFF_LOOPBACK)
476 ret = rdma_translate_ip(dst_in, dev_addr);
477 else
478 rdma_copy_src_l2_addr(dev_addr, dst->dev);
479
480 /*
481 * If there's a gateway and type of device not ARPHRD_INFINIBAND,
482 * we're definitely in RoCE v2 (as RoCE v1 isn't routable) set the
483 * network type accordingly.
484 */
485 if (has_gateway(dst, dst_in->sa_family) &&
486 ndev->type != ARPHRD_INFINIBAND)
487 dev_addr->network = dst_in->sa_family == AF_INET ?
488 RDMA_NETWORK_IPV4 :
489 RDMA_NETWORK_IPV6;
490 else
491 dev_addr->network = RDMA_NETWORK_IB;
492
493 return ret;
494}
495
496static int rdma_set_src_addr_rcu(struct rdma_dev_addr *dev_addr,
497 unsigned int *ndev_flags,
498 const struct sockaddr *dst_in,
499 const struct dst_entry *dst)
500{
501 struct net_device *ndev = READ_ONCE(dst->dev);
502
503 *ndev_flags = ndev->flags;
504 /* A physical device must be the RDMA device to use */
505 if (ndev->flags & IFF_LOOPBACK) {
506 /*
507 * RDMA (IB/RoCE, iWarp) doesn't run on lo interface or
508 * loopback IP address. So if route is resolved to loopback
509 * interface, translate that to a real ndev based on non
510 * loopback IP address.
511 */
512 ndev = rdma_find_ndev_for_src_ip_rcu(dev_net(ndev), dst_in);
513 if (IS_ERR(ndev))
514 return -ENODEV;
515 }
516
517 return copy_src_l2_addr(dev_addr, dst_in, dst, ndev);
518}
519
520static int set_addr_netns_by_gid_rcu(struct rdma_dev_addr *addr)
521{
522 struct net_device *ndev;
523
524 ndev = rdma_read_gid_attr_ndev_rcu(addr->sgid_attr);
525 if (IS_ERR(ndev))
526 return PTR_ERR(ndev);
527
528 /*
529 * Since we are holding the rcu, reading net and ifindex
530 * are safe without any additional reference; because
531 * change_net_namespace() in net/core/dev.c does rcu sync
532 * after it changes the state to IFF_DOWN and before
533 * updating netdev fields {net, ifindex}.
534 */
535 addr->net = dev_net(ndev);
536 addr->bound_dev_if = ndev->ifindex;
537 return 0;
538}
539
540static void rdma_addr_set_net_defaults(struct rdma_dev_addr *addr)
541{
542 addr->net = &init_net;
543 addr->bound_dev_if = 0;
544}
545
546static int addr_resolve(struct sockaddr *src_in,
547 const struct sockaddr *dst_in,
548 struct rdma_dev_addr *addr,
549 bool resolve_neigh,
550 bool resolve_by_gid_attr,
551 u32 seq)
552{
553 struct dst_entry *dst = NULL;
554 unsigned int ndev_flags = 0;
555 struct rtable *rt = NULL;
556 int ret;
557
558 if (!addr->net) {
559 pr_warn_ratelimited("%s: missing namespace\n", __func__);
560 return -EINVAL;
561 }
562
563 rcu_read_lock();
564 if (resolve_by_gid_attr) {
565 if (!addr->sgid_attr) {
566 rcu_read_unlock();
567 pr_warn_ratelimited("%s: missing gid_attr\n", __func__);
568 return -EINVAL;
569 }
570 /*
571 * If the request is for a specific gid attribute of the
572 * rdma_dev_addr, derive net from the netdevice of the
573 * GID attribute.
574 */
575 ret = set_addr_netns_by_gid_rcu(addr);
576 if (ret) {
577 rcu_read_unlock();
578 return ret;
579 }
580 }
581 if (src_in->sa_family == AF_INET) {
582 ret = addr4_resolve(src_in, dst_in, addr, &rt);
583 dst = &rt->dst;
584 } else {
585 ret = addr6_resolve(src_in, dst_in, addr, &dst);
586 }
587 if (ret) {
588 rcu_read_unlock();
589 goto done;
590 }
591 ret = rdma_set_src_addr_rcu(addr, &ndev_flags, dst_in, dst);
592 rcu_read_unlock();
593
594 /*
595 * Resolve neighbor destination address if requested and
596 * only if src addr translation didn't fail.
597 */
598 if (!ret && resolve_neigh)
599 ret = addr_resolve_neigh(dst, dst_in, addr, ndev_flags, seq);
600
601 if (src_in->sa_family == AF_INET)
602 ip_rt_put(rt);
603 else
604 dst_release(dst);
605done:
606 /*
607 * Clear the addr net to go back to its original state, only if it was
608 * derived from GID attribute in this context.
609 */
610 if (resolve_by_gid_attr)
611 rdma_addr_set_net_defaults(addr);
612 return ret;
613}
614
615static void process_one_req(struct work_struct *_work)
616{
617 struct addr_req *req;
618 struct sockaddr *src_in, *dst_in;
619
620 req = container_of(_work, struct addr_req, work.work);
621
622 if (req->status == -ENODATA) {
623 src_in = (struct sockaddr *)&req->src_addr;
624 dst_in = (struct sockaddr *)&req->dst_addr;
625 req->status = addr_resolve(src_in, dst_in, req->addr,
626 true, req->resolve_by_gid_attr,
627 req->seq);
628 if (req->status && time_after_eq(jiffies, req->timeout)) {
629 req->status = -ETIMEDOUT;
630 } else if (req->status == -ENODATA) {
631 /* requeue the work for retrying again */
632 spin_lock_bh(&lock);
633 if (!list_empty(&req->list))
634 set_timeout(req, req->timeout);
635 spin_unlock_bh(&lock);
636 return;
637 }
638 }
639
640 req->callback(req->status, (struct sockaddr *)&req->src_addr,
641 req->addr, req->context);
642 req->callback = NULL;
643
644 spin_lock_bh(&lock);
645 /*
646 * Although the work will normally have been canceled by the workqueue,
647 * it can still be requeued as long as it is on the req_list.
648 */
649 cancel_delayed_work(&req->work);
650 if (!list_empty(&req->list)) {
651 list_del_init(&req->list);
652 kfree(req);
653 }
654 spin_unlock_bh(&lock);
655}
656
657int rdma_resolve_ip(struct sockaddr *src_addr, const struct sockaddr *dst_addr,
658 struct rdma_dev_addr *addr, unsigned long timeout_ms,
659 void (*callback)(int status, struct sockaddr *src_addr,
660 struct rdma_dev_addr *addr, void *context),
661 bool resolve_by_gid_attr, void *context)
662{
663 struct sockaddr *src_in, *dst_in;
664 struct addr_req *req;
665 int ret = 0;
666
667 req = kzalloc(sizeof *req, GFP_KERNEL);
668 if (!req)
669 return -ENOMEM;
670
671 src_in = (struct sockaddr *) &req->src_addr;
672 dst_in = (struct sockaddr *) &req->dst_addr;
673
674 if (src_addr) {
675 if (src_addr->sa_family != dst_addr->sa_family) {
676 ret = -EINVAL;
677 goto err;
678 }
679
680 memcpy(src_in, src_addr, rdma_addr_size(src_addr));
681 } else {
682 src_in->sa_family = dst_addr->sa_family;
683 }
684
685 memcpy(dst_in, dst_addr, rdma_addr_size(dst_addr));
686 req->addr = addr;
687 req->callback = callback;
688 req->context = context;
689 req->resolve_by_gid_attr = resolve_by_gid_attr;
690 INIT_DELAYED_WORK(&req->work, process_one_req);
691 req->seq = (u32)atomic_inc_return(&ib_nl_addr_request_seq);
692
693 req->status = addr_resolve(src_in, dst_in, addr, true,
694 req->resolve_by_gid_attr, req->seq);
695 switch (req->status) {
696 case 0:
697 req->timeout = jiffies;
698 queue_req(req);
699 break;
700 case -ENODATA:
701 req->timeout = msecs_to_jiffies(timeout_ms) + jiffies;
702 queue_req(req);
703 break;
704 default:
705 ret = req->status;
706 goto err;
707 }
708 return ret;
709err:
710 kfree(req);
711 return ret;
712}
713EXPORT_SYMBOL(rdma_resolve_ip);
714
715int roce_resolve_route_from_path(struct sa_path_rec *rec,
716 const struct ib_gid_attr *attr)
717{
718 union {
719 struct sockaddr _sockaddr;
720 struct sockaddr_in _sockaddr_in;
721 struct sockaddr_in6 _sockaddr_in6;
722 } sgid, dgid;
723 struct rdma_dev_addr dev_addr = {};
724 int ret;
725
726 might_sleep();
727
728 if (rec->roce.route_resolved)
729 return 0;
730
731 rdma_gid2ip((struct sockaddr *)&sgid, &rec->sgid);
732 rdma_gid2ip((struct sockaddr *)&dgid, &rec->dgid);
733
734 if (sgid._sockaddr.sa_family != dgid._sockaddr.sa_family)
735 return -EINVAL;
736
737 if (!attr || !attr->ndev)
738 return -EINVAL;
739
740 dev_addr.net = &init_net;
741 dev_addr.sgid_attr = attr;
742
743 ret = addr_resolve((struct sockaddr *)&sgid, (struct sockaddr *)&dgid,
744 &dev_addr, false, true, 0);
745 if (ret)
746 return ret;
747
748 if ((dev_addr.network == RDMA_NETWORK_IPV4 ||
749 dev_addr.network == RDMA_NETWORK_IPV6) &&
750 rec->rec_type != SA_PATH_REC_TYPE_ROCE_V2)
751 return -EINVAL;
752
753 rec->roce.route_resolved = true;
754 return 0;
755}
756
757/**
758 * rdma_addr_cancel - Cancel resolve ip request
759 * @addr: Pointer to address structure given previously
760 * during rdma_resolve_ip().
761 * rdma_addr_cancel() is synchronous function which cancels any pending
762 * request if there is any.
763 */
764void rdma_addr_cancel(struct rdma_dev_addr *addr)
765{
766 struct addr_req *req, *temp_req;
767 struct addr_req *found = NULL;
768
769 spin_lock_bh(&lock);
770 list_for_each_entry_safe(req, temp_req, &req_list, list) {
771 if (req->addr == addr) {
772 /*
773 * Removing from the list means we take ownership of
774 * the req
775 */
776 list_del_init(&req->list);
777 found = req;
778 break;
779 }
780 }
781 spin_unlock_bh(&lock);
782
783 if (!found)
784 return;
785
786 /*
787 * sync canceling the work after removing it from the req_list
788 * guarentees no work is running and none will be started.
789 */
790 cancel_delayed_work_sync(&found->work);
791 kfree(found);
792}
793EXPORT_SYMBOL(rdma_addr_cancel);
794
795struct resolve_cb_context {
796 struct completion comp;
797 int status;
798};
799
800static void resolve_cb(int status, struct sockaddr *src_addr,
801 struct rdma_dev_addr *addr, void *context)
802{
803 ((struct resolve_cb_context *)context)->status = status;
804 complete(&((struct resolve_cb_context *)context)->comp);
805}
806
807int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
808 const union ib_gid *dgid,
809 u8 *dmac, const struct ib_gid_attr *sgid_attr,
810 int *hoplimit)
811{
812 struct rdma_dev_addr dev_addr;
813 struct resolve_cb_context ctx;
814 union {
815 struct sockaddr_in _sockaddr_in;
816 struct sockaddr_in6 _sockaddr_in6;
817 } sgid_addr, dgid_addr;
818 int ret;
819
820 rdma_gid2ip((struct sockaddr *)&sgid_addr, sgid);
821 rdma_gid2ip((struct sockaddr *)&dgid_addr, dgid);
822
823 memset(&dev_addr, 0, sizeof(dev_addr));
824 dev_addr.net = &init_net;
825 dev_addr.sgid_attr = sgid_attr;
826
827 init_completion(&ctx.comp);
828 ret = rdma_resolve_ip((struct sockaddr *)&sgid_addr,
829 (struct sockaddr *)&dgid_addr, &dev_addr, 1000,
830 resolve_cb, true, &ctx);
831 if (ret)
832 return ret;
833
834 wait_for_completion(&ctx.comp);
835
836 ret = ctx.status;
837 if (ret)
838 return ret;
839
840 memcpy(dmac, dev_addr.dst_dev_addr, ETH_ALEN);
841 *hoplimit = dev_addr.hoplimit;
842 return 0;
843}
844
845static int netevent_callback(struct notifier_block *self, unsigned long event,
846 void *ctx)
847{
848 struct addr_req *req;
849
850 if (event == NETEVENT_NEIGH_UPDATE) {
851 struct neighbour *neigh = ctx;
852
853 if (neigh->nud_state & NUD_VALID) {
854 spin_lock_bh(&lock);
855 list_for_each_entry(req, &req_list, list)
856 set_timeout(req, jiffies);
857 spin_unlock_bh(&lock);
858 }
859 }
860 return 0;
861}
862
863static struct notifier_block nb = {
864 .notifier_call = netevent_callback
865};
866
867int addr_init(void)
868{
869 addr_wq = alloc_ordered_workqueue("ib_addr", 0);
870 if (!addr_wq)
871 return -ENOMEM;
872
873 register_netevent_notifier(&nb);
874
875 return 0;
876}
877
878void addr_cleanup(void)
879{
880 unregister_netevent_notifier(&nb);
881 destroy_workqueue(addr_wq);
882 WARN_ON(!list_empty(&req_list));
883}
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
50MODULE_AUTHOR("Sean Hefty");
51MODULE_DESCRIPTION("IB Address Translation");
52MODULE_LICENSE("Dual BSD/GPL");
53
54struct 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};
66
67static void process_req(struct work_struct *work);
68
69static DEFINE_MUTEX(lock);
70static LIST_HEAD(req_list);
71static DECLARE_DELAYED_WORK(work, process_req);
72static struct workqueue_struct *addr_wq;
73
74int rdma_addr_size(struct sockaddr *addr)
75{
76 switch (addr->sa_family) {
77 case AF_INET:
78 return sizeof(struct sockaddr_in);
79 case AF_INET6:
80 return sizeof(struct sockaddr_in6);
81 case AF_IB:
82 return sizeof(struct sockaddr_ib);
83 default:
84 return 0;
85 }
86}
87EXPORT_SYMBOL(rdma_addr_size);
88
89static struct rdma_addr_client self;
90
91void rdma_addr_register_client(struct rdma_addr_client *client)
92{
93 atomic_set(&client->refcount, 1);
94 init_completion(&client->comp);
95}
96EXPORT_SYMBOL(rdma_addr_register_client);
97
98static inline void put_client(struct rdma_addr_client *client)
99{
100 if (atomic_dec_and_test(&client->refcount))
101 complete(&client->comp);
102}
103
104void rdma_addr_unregister_client(struct rdma_addr_client *client)
105{
106 put_client(client);
107 wait_for_completion(&client->comp);
108}
109EXPORT_SYMBOL(rdma_addr_unregister_client);
110
111int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev,
112 const unsigned char *dst_dev_addr)
113{
114 dev_addr->dev_type = dev->type;
115 memcpy(dev_addr->src_dev_addr, dev->dev_addr, MAX_ADDR_LEN);
116 memcpy(dev_addr->broadcast, dev->broadcast, MAX_ADDR_LEN);
117 if (dst_dev_addr)
118 memcpy(dev_addr->dst_dev_addr, dst_dev_addr, MAX_ADDR_LEN);
119 dev_addr->bound_dev_if = dev->ifindex;
120 return 0;
121}
122EXPORT_SYMBOL(rdma_copy_addr);
123
124int rdma_translate_ip(const struct sockaddr *addr,
125 struct rdma_dev_addr *dev_addr,
126 u16 *vlan_id)
127{
128 struct net_device *dev;
129 int ret = -EADDRNOTAVAIL;
130
131 if (dev_addr->bound_dev_if) {
132 dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
133 if (!dev)
134 return -ENODEV;
135 ret = rdma_copy_addr(dev_addr, dev, NULL);
136 dev_put(dev);
137 return ret;
138 }
139
140 switch (addr->sa_family) {
141 case AF_INET:
142 dev = ip_dev_find(dev_addr->net,
143 ((const struct sockaddr_in *)addr)->sin_addr.s_addr);
144
145 if (!dev)
146 return ret;
147
148 ret = rdma_copy_addr(dev_addr, dev, NULL);
149 if (vlan_id)
150 *vlan_id = rdma_vlan_dev_vlan_id(dev);
151 dev_put(dev);
152 break;
153#if IS_ENABLED(CONFIG_IPV6)
154 case AF_INET6:
155 rcu_read_lock();
156 for_each_netdev_rcu(dev_addr->net, dev) {
157 if (ipv6_chk_addr(dev_addr->net,
158 &((const struct sockaddr_in6 *)addr)->sin6_addr,
159 dev, 1)) {
160 ret = rdma_copy_addr(dev_addr, dev, NULL);
161 if (vlan_id)
162 *vlan_id = rdma_vlan_dev_vlan_id(dev);
163 break;
164 }
165 }
166 rcu_read_unlock();
167 break;
168#endif
169 }
170 return ret;
171}
172EXPORT_SYMBOL(rdma_translate_ip);
173
174static void set_timeout(unsigned long time)
175{
176 unsigned long delay;
177
178 delay = time - jiffies;
179 if ((long)delay < 0)
180 delay = 0;
181
182 mod_delayed_work(addr_wq, &work, delay);
183}
184
185static void queue_req(struct addr_req *req)
186{
187 struct addr_req *temp_req;
188
189 mutex_lock(&lock);
190 list_for_each_entry_reverse(temp_req, &req_list, list) {
191 if (time_after_eq(req->timeout, temp_req->timeout))
192 break;
193 }
194
195 list_add(&req->list, &temp_req->list);
196
197 if (req_list.next == &req->list)
198 set_timeout(req->timeout);
199 mutex_unlock(&lock);
200}
201
202static int dst_fetch_ha(struct dst_entry *dst, struct rdma_dev_addr *dev_addr,
203 const void *daddr)
204{
205 struct neighbour *n;
206 int ret;
207
208 n = dst_neigh_lookup(dst, daddr);
209
210 rcu_read_lock();
211 if (!n || !(n->nud_state & NUD_VALID)) {
212 if (n)
213 neigh_event_send(n, NULL);
214 ret = -ENODATA;
215 } else {
216 ret = rdma_copy_addr(dev_addr, dst->dev, n->ha);
217 }
218 rcu_read_unlock();
219
220 if (n)
221 neigh_release(n);
222
223 return ret;
224}
225
226static int addr4_resolve(struct sockaddr_in *src_in,
227 const struct sockaddr_in *dst_in,
228 struct rdma_dev_addr *addr,
229 struct rtable **prt)
230{
231 __be32 src_ip = src_in->sin_addr.s_addr;
232 __be32 dst_ip = dst_in->sin_addr.s_addr;
233 struct rtable *rt;
234 struct flowi4 fl4;
235 int ret;
236
237 memset(&fl4, 0, sizeof(fl4));
238 fl4.daddr = dst_ip;
239 fl4.saddr = src_ip;
240 fl4.flowi4_oif = addr->bound_dev_if;
241 rt = ip_route_output_key(addr->net, &fl4);
242 if (IS_ERR(rt)) {
243 ret = PTR_ERR(rt);
244 goto out;
245 }
246 src_in->sin_family = AF_INET;
247 src_in->sin_addr.s_addr = fl4.saddr;
248
249 /* If there's a gateway, we're definitely in RoCE v2 (as RoCE v1 isn't
250 * routable) and we could set the network type accordingly.
251 */
252 if (rt->rt_uses_gateway)
253 addr->network = RDMA_NETWORK_IPV4;
254
255 addr->hoplimit = ip4_dst_hoplimit(&rt->dst);
256
257 *prt = rt;
258 return 0;
259out:
260 return ret;
261}
262
263#if IS_ENABLED(CONFIG_IPV6)
264static int addr6_resolve(struct sockaddr_in6 *src_in,
265 const struct sockaddr_in6 *dst_in,
266 struct rdma_dev_addr *addr,
267 struct dst_entry **pdst)
268{
269 struct flowi6 fl6;
270 struct dst_entry *dst;
271 struct rt6_info *rt;
272 int ret;
273
274 memset(&fl6, 0, sizeof fl6);
275 fl6.daddr = dst_in->sin6_addr;
276 fl6.saddr = src_in->sin6_addr;
277 fl6.flowi6_oif = addr->bound_dev_if;
278
279 dst = ip6_route_output(addr->net, NULL, &fl6);
280 if ((ret = dst->error))
281 goto put;
282
283 rt = (struct rt6_info *)dst;
284 if (ipv6_addr_any(&fl6.saddr)) {
285 ret = ipv6_dev_get_saddr(addr->net, ip6_dst_idev(dst)->dev,
286 &fl6.daddr, 0, &fl6.saddr);
287 if (ret)
288 goto put;
289
290 src_in->sin6_family = AF_INET6;
291 src_in->sin6_addr = fl6.saddr;
292 }
293
294 /* If there's a gateway, we're definitely in RoCE v2 (as RoCE v1 isn't
295 * routable) and we could set the network type accordingly.
296 */
297 if (rt->rt6i_flags & RTF_GATEWAY)
298 addr->network = RDMA_NETWORK_IPV6;
299
300 addr->hoplimit = ip6_dst_hoplimit(dst);
301
302 *pdst = dst;
303 return 0;
304put:
305 dst_release(dst);
306 return ret;
307}
308#else
309static int addr6_resolve(struct sockaddr_in6 *src_in,
310 const struct sockaddr_in6 *dst_in,
311 struct rdma_dev_addr *addr,
312 struct dst_entry **pdst)
313{
314 return -EADDRNOTAVAIL;
315}
316#endif
317
318static int addr_resolve_neigh(struct dst_entry *dst,
319 const struct sockaddr *dst_in,
320 struct rdma_dev_addr *addr)
321{
322 if (dst->dev->flags & IFF_LOOPBACK) {
323 int ret;
324
325 ret = rdma_translate_ip(dst_in, addr, NULL);
326 if (!ret)
327 memcpy(addr->dst_dev_addr, addr->src_dev_addr,
328 MAX_ADDR_LEN);
329
330 return ret;
331 }
332
333 /* If the device doesn't do ARP internally */
334 if (!(dst->dev->flags & IFF_NOARP)) {
335 const struct sockaddr_in *dst_in4 =
336 (const struct sockaddr_in *)dst_in;
337 const struct sockaddr_in6 *dst_in6 =
338 (const struct sockaddr_in6 *)dst_in;
339
340 return dst_fetch_ha(dst, addr,
341 dst_in->sa_family == AF_INET ?
342 (const void *)&dst_in4->sin_addr.s_addr :
343 (const void *)&dst_in6->sin6_addr);
344 }
345
346 return rdma_copy_addr(addr, dst->dev, NULL);
347}
348
349static int addr_resolve(struct sockaddr *src_in,
350 const struct sockaddr *dst_in,
351 struct rdma_dev_addr *addr,
352 bool resolve_neigh)
353{
354 struct net_device *ndev;
355 struct dst_entry *dst;
356 int ret;
357
358 if (src_in->sa_family == AF_INET) {
359 struct rtable *rt = NULL;
360 const struct sockaddr_in *dst_in4 =
361 (const struct sockaddr_in *)dst_in;
362
363 ret = addr4_resolve((struct sockaddr_in *)src_in,
364 dst_in4, addr, &rt);
365 if (ret)
366 return ret;
367
368 if (resolve_neigh)
369 ret = addr_resolve_neigh(&rt->dst, dst_in, addr);
370
371 ndev = rt->dst.dev;
372 dev_hold(ndev);
373
374 ip_rt_put(rt);
375 } else {
376 const struct sockaddr_in6 *dst_in6 =
377 (const struct sockaddr_in6 *)dst_in;
378
379 ret = addr6_resolve((struct sockaddr_in6 *)src_in,
380 dst_in6, addr,
381 &dst);
382 if (ret)
383 return ret;
384
385 if (resolve_neigh)
386 ret = addr_resolve_neigh(dst, dst_in, addr);
387
388 ndev = dst->dev;
389 dev_hold(ndev);
390
391 dst_release(dst);
392 }
393
394 addr->bound_dev_if = ndev->ifindex;
395 addr->net = dev_net(ndev);
396 dev_put(ndev);
397
398 return ret;
399}
400
401static void process_req(struct work_struct *work)
402{
403 struct addr_req *req, *temp_req;
404 struct sockaddr *src_in, *dst_in;
405 struct list_head done_list;
406
407 INIT_LIST_HEAD(&done_list);
408
409 mutex_lock(&lock);
410 list_for_each_entry_safe(req, temp_req, &req_list, list) {
411 if (req->status == -ENODATA) {
412 src_in = (struct sockaddr *) &req->src_addr;
413 dst_in = (struct sockaddr *) &req->dst_addr;
414 req->status = addr_resolve(src_in, dst_in, req->addr,
415 true);
416 if (req->status && time_after_eq(jiffies, req->timeout))
417 req->status = -ETIMEDOUT;
418 else if (req->status == -ENODATA)
419 continue;
420 }
421 list_move_tail(&req->list, &done_list);
422 }
423
424 if (!list_empty(&req_list)) {
425 req = list_entry(req_list.next, struct addr_req, list);
426 set_timeout(req->timeout);
427 }
428 mutex_unlock(&lock);
429
430 list_for_each_entry_safe(req, temp_req, &done_list, list) {
431 list_del(&req->list);
432 req->callback(req->status, (struct sockaddr *) &req->src_addr,
433 req->addr, req->context);
434 put_client(req->client);
435 kfree(req);
436 }
437}
438
439int rdma_resolve_ip(struct rdma_addr_client *client,
440 struct sockaddr *src_addr, struct sockaddr *dst_addr,
441 struct rdma_dev_addr *addr, int timeout_ms,
442 void (*callback)(int status, struct sockaddr *src_addr,
443 struct rdma_dev_addr *addr, void *context),
444 void *context)
445{
446 struct sockaddr *src_in, *dst_in;
447 struct addr_req *req;
448 int ret = 0;
449
450 req = kzalloc(sizeof *req, GFP_KERNEL);
451 if (!req)
452 return -ENOMEM;
453
454 src_in = (struct sockaddr *) &req->src_addr;
455 dst_in = (struct sockaddr *) &req->dst_addr;
456
457 if (src_addr) {
458 if (src_addr->sa_family != dst_addr->sa_family) {
459 ret = -EINVAL;
460 goto err;
461 }
462
463 memcpy(src_in, src_addr, rdma_addr_size(src_addr));
464 } else {
465 src_in->sa_family = dst_addr->sa_family;
466 }
467
468 memcpy(dst_in, dst_addr, rdma_addr_size(dst_addr));
469 req->addr = addr;
470 req->callback = callback;
471 req->context = context;
472 req->client = client;
473 atomic_inc(&client->refcount);
474
475 req->status = addr_resolve(src_in, dst_in, addr, true);
476 switch (req->status) {
477 case 0:
478 req->timeout = jiffies;
479 queue_req(req);
480 break;
481 case -ENODATA:
482 req->timeout = msecs_to_jiffies(timeout_ms) + jiffies;
483 queue_req(req);
484 break;
485 default:
486 ret = req->status;
487 atomic_dec(&client->refcount);
488 goto err;
489 }
490 return ret;
491err:
492 kfree(req);
493 return ret;
494}
495EXPORT_SYMBOL(rdma_resolve_ip);
496
497int rdma_resolve_ip_route(struct sockaddr *src_addr,
498 const struct sockaddr *dst_addr,
499 struct rdma_dev_addr *addr)
500{
501 struct sockaddr_storage ssrc_addr = {};
502 struct sockaddr *src_in = (struct sockaddr *)&ssrc_addr;
503
504 if (src_addr) {
505 if (src_addr->sa_family != dst_addr->sa_family)
506 return -EINVAL;
507
508 memcpy(src_in, src_addr, rdma_addr_size(src_addr));
509 } else {
510 src_in->sa_family = dst_addr->sa_family;
511 }
512
513 return addr_resolve(src_in, dst_addr, addr, false);
514}
515EXPORT_SYMBOL(rdma_resolve_ip_route);
516
517void rdma_addr_cancel(struct rdma_dev_addr *addr)
518{
519 struct addr_req *req, *temp_req;
520
521 mutex_lock(&lock);
522 list_for_each_entry_safe(req, temp_req, &req_list, list) {
523 if (req->addr == addr) {
524 req->status = -ECANCELED;
525 req->timeout = jiffies;
526 list_move(&req->list, &req_list);
527 set_timeout(req->timeout);
528 break;
529 }
530 }
531 mutex_unlock(&lock);
532}
533EXPORT_SYMBOL(rdma_addr_cancel);
534
535struct resolve_cb_context {
536 struct rdma_dev_addr *addr;
537 struct completion comp;
538};
539
540static void resolve_cb(int status, struct sockaddr *src_addr,
541 struct rdma_dev_addr *addr, void *context)
542{
543 memcpy(((struct resolve_cb_context *)context)->addr, addr, sizeof(struct
544 rdma_dev_addr));
545 complete(&((struct resolve_cb_context *)context)->comp);
546}
547
548int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
549 const union ib_gid *dgid,
550 u8 *dmac, u16 *vlan_id, int *if_index,
551 int *hoplimit)
552{
553 int ret = 0;
554 struct rdma_dev_addr dev_addr;
555 struct resolve_cb_context ctx;
556 struct net_device *dev;
557
558 union {
559 struct sockaddr _sockaddr;
560 struct sockaddr_in _sockaddr_in;
561 struct sockaddr_in6 _sockaddr_in6;
562 } sgid_addr, dgid_addr;
563
564
565 rdma_gid2ip(&sgid_addr._sockaddr, sgid);
566 rdma_gid2ip(&dgid_addr._sockaddr, dgid);
567
568 memset(&dev_addr, 0, sizeof(dev_addr));
569 if (if_index)
570 dev_addr.bound_dev_if = *if_index;
571 dev_addr.net = &init_net;
572
573 ctx.addr = &dev_addr;
574 init_completion(&ctx.comp);
575 ret = rdma_resolve_ip(&self, &sgid_addr._sockaddr, &dgid_addr._sockaddr,
576 &dev_addr, 1000, resolve_cb, &ctx);
577 if (ret)
578 return ret;
579
580 wait_for_completion(&ctx.comp);
581
582 memcpy(dmac, dev_addr.dst_dev_addr, ETH_ALEN);
583 dev = dev_get_by_index(&init_net, dev_addr.bound_dev_if);
584 if (!dev)
585 return -ENODEV;
586 if (if_index)
587 *if_index = dev_addr.bound_dev_if;
588 if (vlan_id)
589 *vlan_id = rdma_vlan_dev_vlan_id(dev);
590 if (hoplimit)
591 *hoplimit = dev_addr.hoplimit;
592 dev_put(dev);
593 return ret;
594}
595EXPORT_SYMBOL(rdma_addr_find_l2_eth_by_grh);
596
597int rdma_addr_find_smac_by_sgid(union ib_gid *sgid, u8 *smac, u16 *vlan_id)
598{
599 int ret = 0;
600 struct rdma_dev_addr dev_addr;
601 union {
602 struct sockaddr _sockaddr;
603 struct sockaddr_in _sockaddr_in;
604 struct sockaddr_in6 _sockaddr_in6;
605 } gid_addr;
606
607 rdma_gid2ip(&gid_addr._sockaddr, sgid);
608
609 memset(&dev_addr, 0, sizeof(dev_addr));
610 dev_addr.net = &init_net;
611 ret = rdma_translate_ip(&gid_addr._sockaddr, &dev_addr, vlan_id);
612 if (ret)
613 return ret;
614
615 memcpy(smac, dev_addr.src_dev_addr, ETH_ALEN);
616 return ret;
617}
618EXPORT_SYMBOL(rdma_addr_find_smac_by_sgid);
619
620static int netevent_callback(struct notifier_block *self, unsigned long event,
621 void *ctx)
622{
623 if (event == NETEVENT_NEIGH_UPDATE) {
624 struct neighbour *neigh = ctx;
625
626 if (neigh->nud_state & NUD_VALID) {
627 set_timeout(jiffies);
628 }
629 }
630 return 0;
631}
632
633static struct notifier_block nb = {
634 .notifier_call = netevent_callback
635};
636
637static int __init addr_init(void)
638{
639 addr_wq = create_singlethread_workqueue("ib_addr");
640 if (!addr_wq)
641 return -ENOMEM;
642
643 register_netevent_notifier(&nb);
644 rdma_addr_register_client(&self);
645 return 0;
646}
647
648static void __exit addr_cleanup(void)
649{
650 rdma_addr_unregister_client(&self);
651 unregister_netevent_notifier(&nb);
652 destroy_workqueue(addr_wq);
653}
654
655module_init(addr_init);
656module_exit(addr_cleanup);