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.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
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.
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
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>
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>
49 #include <rdma/rdma_netlink.h>
50 #include <net/netlink.h>
52 #include "core_priv.h"
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
;
61 void (*callback
)(int status
, struct sockaddr
*src_addr
,
62 struct rdma_dev_addr
*addr
, void *context
);
63 unsigned long timeout
;
68 static atomic_t ib_nl_addr_request_seq
= ATOMIC_INIT(0);
70 static void process_req(struct work_struct
*work
);
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
;
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
)},
82 static inline bool ib_nl_is_good_ip_resp(const struct nlmsghdr
*nlh
)
84 struct nlattr
*tb
[LS_NLA_TYPE_MAX
] = {};
87 if (nlh
->nlmsg_flags
& RDMA_NL_LS_F_ERR
)
90 ret
= nla_parse(tb
, LS_NLA_TYPE_MAX
- 1, nlmsg_data(nlh
),
91 nlmsg_len(nlh
), ib_nl_addr_policy
, NULL
);
98 static void ib_nl_process_good_ip_rsep(const struct nlmsghdr
*nlh
)
100 const struct nlattr
*head
, *curr
;
102 struct addr_req
*req
;
106 head
= (const struct nlattr
*)nlmsg_data(nlh
);
107 len
= nlmsg_len(nlh
);
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
));
115 list_for_each_entry(req
, &req_list
, list
) {
116 if (nlh
->nlmsg_seq
!= req
->seq
)
118 /* We set the DGID part, the rest was set earlier */
119 rdma_addr_set_dgid(req
->addr
, &gid
);
127 pr_info("Couldn't find request waiting for DGID: %pI6\n",
131 int ib_nl_handle_ip_res_resp(struct sk_buff
*skb
,
132 struct netlink_callback
*cb
)
134 const struct nlmsghdr
*nlh
= (struct nlmsghdr
*)cb
->nlh
;
136 if ((nlh
->nlmsg_flags
& NLM_F_REQUEST
) ||
137 !(NETLINK_CB(skb
).sk
) ||
138 !netlink_capable(skb
, CAP_NET_ADMIN
))
141 if (ib_nl_is_good_ip_resp(nlh
))
142 ib_nl_process_good_ip_rsep(nlh
);
147 static int ib_nl_ip_send_msg(struct rdma_dev_addr
*dev_addr
,
151 struct sk_buff
*skb
= NULL
;
152 struct nlmsghdr
*nlh
;
153 struct rdma_ls_ip_resolve_header
*header
;
159 if (family
== AF_INET
) {
160 size
= sizeof(struct in_addr
);
161 attrtype
= RDMA_NLA_F_MANDATORY
| LS_NLA_TYPE_IPV4
;
163 size
= sizeof(struct in6_addr
);
164 attrtype
= RDMA_NLA_F_MANDATORY
| LS_NLA_TYPE_IPV6
;
167 len
= nla_total_size(sizeof(size
));
168 len
+= NLMSG_ALIGN(sizeof(*header
));
170 skb
= nlmsg_new(len
, GFP_KERNEL
);
174 data
= ibnl_put_msg(skb
, &nlh
, seq
, 0, RDMA_NL_LS
,
175 RDMA_NL_LS_OP_IP_RESOLVE
, NLM_F_REQUEST
);
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
);
186 /* Repair the nlmsg header length */
188 ibnl_multicast(skb
, nlh
, RDMA_NL_GROUP_LS
, GFP_KERNEL
);
190 /* Make the request retry, so when we get the response from userspace
191 * we will have something.
196 int rdma_addr_size(struct sockaddr
*addr
)
198 switch (addr
->sa_family
) {
200 return sizeof(struct sockaddr_in
);
202 return sizeof(struct sockaddr_in6
);
204 return sizeof(struct sockaddr_ib
);
209 EXPORT_SYMBOL(rdma_addr_size
);
211 static struct rdma_addr_client self
;
213 void rdma_addr_register_client(struct rdma_addr_client
*client
)
215 atomic_set(&client
->refcount
, 1);
216 init_completion(&client
->comp
);
218 EXPORT_SYMBOL(rdma_addr_register_client
);
220 static inline void put_client(struct rdma_addr_client
*client
)
222 if (atomic_dec_and_test(&client
->refcount
))
223 complete(&client
->comp
);
226 void rdma_addr_unregister_client(struct rdma_addr_client
*client
)
229 wait_for_completion(&client
->comp
);
231 EXPORT_SYMBOL(rdma_addr_unregister_client
);
233 int rdma_copy_addr(struct rdma_dev_addr
*dev_addr
, struct net_device
*dev
,
234 const unsigned char *dst_dev_addr
)
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
);
240 memcpy(dev_addr
->dst_dev_addr
, dst_dev_addr
, MAX_ADDR_LEN
);
241 dev_addr
->bound_dev_if
= dev
->ifindex
;
244 EXPORT_SYMBOL(rdma_copy_addr
);
246 int rdma_translate_ip(const struct sockaddr
*addr
,
247 struct rdma_dev_addr
*dev_addr
,
250 struct net_device
*dev
;
251 int ret
= -EADDRNOTAVAIL
;
253 if (dev_addr
->bound_dev_if
) {
254 dev
= dev_get_by_index(dev_addr
->net
, dev_addr
->bound_dev_if
);
257 ret
= rdma_copy_addr(dev_addr
, dev
, NULL
);
262 switch (addr
->sa_family
) {
264 dev
= ip_dev_find(dev_addr
->net
,
265 ((const struct sockaddr_in
*)addr
)->sin_addr
.s_addr
);
270 ret
= rdma_copy_addr(dev_addr
, dev
, NULL
);
272 *vlan_id
= rdma_vlan_dev_vlan_id(dev
);
275 #if IS_ENABLED(CONFIG_IPV6)
278 for_each_netdev_rcu(dev_addr
->net
, dev
) {
279 if (ipv6_chk_addr(dev_addr
->net
,
280 &((const struct sockaddr_in6
*)addr
)->sin6_addr
,
282 ret
= rdma_copy_addr(dev_addr
, dev
, NULL
);
284 *vlan_id
= rdma_vlan_dev_vlan_id(dev
);
294 EXPORT_SYMBOL(rdma_translate_ip
);
296 static void set_timeout(unsigned long time
)
300 delay
= time
- jiffies
;
304 mod_delayed_work(addr_wq
, &work
, delay
);
307 static void queue_req(struct addr_req
*req
)
309 struct addr_req
*temp_req
;
312 list_for_each_entry_reverse(temp_req
, &req_list
, list
) {
313 if (time_after_eq(req
->timeout
, temp_req
->timeout
))
317 list_add(&req
->list
, &temp_req
->list
);
319 if (req_list
.next
== &req
->list
)
320 set_timeout(req
->timeout
);
324 static int ib_nl_fetch_ha(struct dst_entry
*dst
, struct rdma_dev_addr
*dev_addr
,
325 const void *daddr
, u32 seq
, u16 family
)
327 if (ibnl_chk_listeners(RDMA_NL_GROUP_LS
))
328 return -EADDRNOTAVAIL
;
330 /* We fill in what we can, the response will fill the rest */
331 rdma_copy_addr(dev_addr
, dst
->dev
, NULL
);
332 return ib_nl_ip_send_msg(dev_addr
, daddr
, seq
, family
);
335 static int dst_fetch_ha(struct dst_entry
*dst
, struct rdma_dev_addr
*dev_addr
,
341 n
= dst_neigh_lookup(dst
, daddr
);
344 if (!n
|| !(n
->nud_state
& NUD_VALID
)) {
346 neigh_event_send(n
, NULL
);
349 ret
= rdma_copy_addr(dev_addr
, dst
->dev
, n
->ha
);
359 static bool has_gateway(struct dst_entry
*dst
, sa_family_t family
)
362 struct rt6_info
*rt6
;
364 if (family
== AF_INET
) {
365 rt
= container_of(dst
, struct rtable
, dst
);
366 return rt
->rt_uses_gateway
;
369 rt6
= container_of(dst
, struct rt6_info
, dst
);
370 return rt6
->rt6i_flags
& RTF_GATEWAY
;
373 static int fetch_ha(struct dst_entry
*dst
, struct rdma_dev_addr
*dev_addr
,
374 const struct sockaddr
*dst_in
, u32 seq
)
376 const struct sockaddr_in
*dst_in4
=
377 (const struct sockaddr_in
*)dst_in
;
378 const struct sockaddr_in6
*dst_in6
=
379 (const struct sockaddr_in6
*)dst_in
;
380 const void *daddr
= (dst_in
->sa_family
== AF_INET
) ?
381 (const void *)&dst_in4
->sin_addr
.s_addr
:
382 (const void *)&dst_in6
->sin6_addr
;
383 sa_family_t family
= dst_in
->sa_family
;
385 /* Gateway + ARPHRD_INFINIBAND -> IB router */
386 if (has_gateway(dst
, family
) && dst
->dev
->type
== ARPHRD_INFINIBAND
)
387 return ib_nl_fetch_ha(dst
, dev_addr
, daddr
, seq
, family
);
389 return dst_fetch_ha(dst
, dev_addr
, daddr
);
392 static int addr4_resolve(struct sockaddr_in
*src_in
,
393 const struct sockaddr_in
*dst_in
,
394 struct rdma_dev_addr
*addr
,
397 __be32 src_ip
= src_in
->sin_addr
.s_addr
;
398 __be32 dst_ip
= dst_in
->sin_addr
.s_addr
;
403 memset(&fl4
, 0, sizeof(fl4
));
406 fl4
.flowi4_oif
= addr
->bound_dev_if
;
407 rt
= ip_route_output_key(addr
->net
, &fl4
);
412 src_in
->sin_family
= AF_INET
;
413 src_in
->sin_addr
.s_addr
= fl4
.saddr
;
415 /* If there's a gateway and type of device not ARPHRD_INFINIBAND, we're
416 * definitely in RoCE v2 (as RoCE v1 isn't routable) set the network
419 if (rt
->rt_uses_gateway
&& rt
->dst
.dev
->type
!= ARPHRD_INFINIBAND
)
420 addr
->network
= RDMA_NETWORK_IPV4
;
422 addr
->hoplimit
= ip4_dst_hoplimit(&rt
->dst
);
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
)
437 struct dst_entry
*dst
;
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
;
446 ret
= ipv6_stub
->ipv6_dst_lookup(addr
->net
, NULL
, &dst
, &fl6
);
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
;
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
460 if (rt
->rt6i_flags
& RTF_GATEWAY
&&
461 ip6_dst_idev(dst
)->dev
->type
!= ARPHRD_INFINIBAND
)
462 addr
->network
= RDMA_NETWORK_IPV6
;
464 addr
->hoplimit
= ip6_dst_hoplimit(dst
);
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
)
475 return -EADDRNOTAVAIL
;
479 static int addr_resolve_neigh(struct dst_entry
*dst
,
480 const struct sockaddr
*dst_in
,
481 struct rdma_dev_addr
*addr
,
484 if (dst
->dev
->flags
& IFF_LOOPBACK
) {
487 ret
= rdma_translate_ip(dst_in
, addr
, NULL
);
489 memcpy(addr
->dst_dev_addr
, addr
->src_dev_addr
,
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
);
499 return rdma_copy_addr(addr
, dst
->dev
, NULL
);
502 static int addr_resolve(struct sockaddr
*src_in
,
503 const struct sockaddr
*dst_in
,
504 struct rdma_dev_addr
*addr
,
508 struct net_device
*ndev
;
509 struct dst_entry
*dst
;
512 if (src_in
->sa_family
== AF_INET
) {
513 struct rtable
*rt
= NULL
;
514 const struct sockaddr_in
*dst_in4
=
515 (const struct sockaddr_in
*)dst_in
;
517 ret
= addr4_resolve((struct sockaddr_in
*)src_in
,
523 ret
= addr_resolve_neigh(&rt
->dst
, dst_in
, addr
, seq
);
530 const struct sockaddr_in6
*dst_in6
=
531 (const struct sockaddr_in6
*)dst_in
;
533 ret
= addr6_resolve((struct sockaddr_in6
*)src_in
,
540 ret
= addr_resolve_neigh(dst
, dst_in
, addr
, seq
);
548 addr
->bound_dev_if
= ndev
->ifindex
;
549 addr
->net
= dev_net(ndev
);
555 static void process_req(struct work_struct
*work
)
557 struct addr_req
*req
, *temp_req
;
558 struct sockaddr
*src_in
, *dst_in
;
559 struct list_head done_list
;
561 INIT_LIST_HEAD(&done_list
);
564 list_for_each_entry_safe(req
, temp_req
, &req_list
, list
) {
565 if (req
->status
== -ENODATA
) {
566 src_in
= (struct sockaddr
*) &req
->src_addr
;
567 dst_in
= (struct sockaddr
*) &req
->dst_addr
;
568 req
->status
= addr_resolve(src_in
, dst_in
, req
->addr
,
570 if (req
->status
&& time_after_eq(jiffies
, req
->timeout
))
571 req
->status
= -ETIMEDOUT
;
572 else if (req
->status
== -ENODATA
)
575 list_move_tail(&req
->list
, &done_list
);
578 if (!list_empty(&req_list
)) {
579 req
= list_entry(req_list
.next
, struct addr_req
, list
);
580 set_timeout(req
->timeout
);
584 list_for_each_entry_safe(req
, temp_req
, &done_list
, list
) {
585 list_del(&req
->list
);
586 req
->callback(req
->status
, (struct sockaddr
*) &req
->src_addr
,
587 req
->addr
, req
->context
);
588 put_client(req
->client
);
593 int rdma_resolve_ip(struct rdma_addr_client
*client
,
594 struct sockaddr
*src_addr
, struct sockaddr
*dst_addr
,
595 struct rdma_dev_addr
*addr
, int timeout_ms
,
596 void (*callback
)(int status
, struct sockaddr
*src_addr
,
597 struct rdma_dev_addr
*addr
, void *context
),
600 struct sockaddr
*src_in
, *dst_in
;
601 struct addr_req
*req
;
604 req
= kzalloc(sizeof *req
, GFP_KERNEL
);
608 src_in
= (struct sockaddr
*) &req
->src_addr
;
609 dst_in
= (struct sockaddr
*) &req
->dst_addr
;
612 if (src_addr
->sa_family
!= dst_addr
->sa_family
) {
617 memcpy(src_in
, src_addr
, rdma_addr_size(src_addr
));
619 src_in
->sa_family
= dst_addr
->sa_family
;
622 memcpy(dst_in
, dst_addr
, rdma_addr_size(dst_addr
));
624 req
->callback
= callback
;
625 req
->context
= context
;
626 req
->client
= client
;
627 atomic_inc(&client
->refcount
);
628 req
->seq
= (u32
)atomic_inc_return(&ib_nl_addr_request_seq
);
630 req
->status
= addr_resolve(src_in
, dst_in
, addr
, true, req
->seq
);
631 switch (req
->status
) {
633 req
->timeout
= jiffies
;
637 req
->timeout
= msecs_to_jiffies(timeout_ms
) + jiffies
;
642 atomic_dec(&client
->refcount
);
650 EXPORT_SYMBOL(rdma_resolve_ip
);
652 int rdma_resolve_ip_route(struct sockaddr
*src_addr
,
653 const struct sockaddr
*dst_addr
,
654 struct rdma_dev_addr
*addr
)
656 struct sockaddr_storage ssrc_addr
= {};
657 struct sockaddr
*src_in
= (struct sockaddr
*)&ssrc_addr
;
660 if (src_addr
->sa_family
!= dst_addr
->sa_family
)
663 memcpy(src_in
, src_addr
, rdma_addr_size(src_addr
));
665 src_in
->sa_family
= dst_addr
->sa_family
;
668 return addr_resolve(src_in
, dst_addr
, addr
, false, 0);
670 EXPORT_SYMBOL(rdma_resolve_ip_route
);
672 void rdma_addr_cancel(struct rdma_dev_addr
*addr
)
674 struct addr_req
*req
, *temp_req
;
677 list_for_each_entry_safe(req
, temp_req
, &req_list
, list
) {
678 if (req
->addr
== addr
) {
679 req
->status
= -ECANCELED
;
680 req
->timeout
= jiffies
;
681 list_move(&req
->list
, &req_list
);
682 set_timeout(req
->timeout
);
688 EXPORT_SYMBOL(rdma_addr_cancel
);
690 struct resolve_cb_context
{
691 struct rdma_dev_addr
*addr
;
692 struct completion comp
;
696 static void resolve_cb(int status
, struct sockaddr
*src_addr
,
697 struct rdma_dev_addr
*addr
, void *context
)
700 memcpy(((struct resolve_cb_context
*)context
)->addr
,
701 addr
, sizeof(struct rdma_dev_addr
));
702 ((struct resolve_cb_context
*)context
)->status
= status
;
703 complete(&((struct resolve_cb_context
*)context
)->comp
);
706 int rdma_addr_find_l2_eth_by_grh(const union ib_gid
*sgid
,
707 const union ib_gid
*dgid
,
708 u8
*dmac
, u16
*vlan_id
, int *if_index
,
712 struct rdma_dev_addr dev_addr
;
713 struct resolve_cb_context ctx
;
714 struct net_device
*dev
;
717 struct sockaddr _sockaddr
;
718 struct sockaddr_in _sockaddr_in
;
719 struct sockaddr_in6 _sockaddr_in6
;
720 } sgid_addr
, dgid_addr
;
723 rdma_gid2ip(&sgid_addr
._sockaddr
, sgid
);
724 rdma_gid2ip(&dgid_addr
._sockaddr
, dgid
);
726 memset(&dev_addr
, 0, sizeof(dev_addr
));
728 dev_addr
.bound_dev_if
= *if_index
;
729 dev_addr
.net
= &init_net
;
731 ctx
.addr
= &dev_addr
;
732 init_completion(&ctx
.comp
);
733 ret
= rdma_resolve_ip(&self
, &sgid_addr
._sockaddr
, &dgid_addr
._sockaddr
,
734 &dev_addr
, 1000, resolve_cb
, &ctx
);
738 wait_for_completion(&ctx
.comp
);
744 memcpy(dmac
, dev_addr
.dst_dev_addr
, ETH_ALEN
);
745 dev
= dev_get_by_index(&init_net
, dev_addr
.bound_dev_if
);
749 *if_index
= dev_addr
.bound_dev_if
;
751 *vlan_id
= rdma_vlan_dev_vlan_id(dev
);
753 *hoplimit
= dev_addr
.hoplimit
;
757 EXPORT_SYMBOL(rdma_addr_find_l2_eth_by_grh
);
759 int rdma_addr_find_smac_by_sgid(union ib_gid
*sgid
, u8
*smac
, u16
*vlan_id
)
762 struct rdma_dev_addr dev_addr
;
764 struct sockaddr _sockaddr
;
765 struct sockaddr_in _sockaddr_in
;
766 struct sockaddr_in6 _sockaddr_in6
;
769 rdma_gid2ip(&gid_addr
._sockaddr
, sgid
);
771 memset(&dev_addr
, 0, sizeof(dev_addr
));
772 dev_addr
.net
= &init_net
;
773 ret
= rdma_translate_ip(&gid_addr
._sockaddr
, &dev_addr
, vlan_id
);
777 memcpy(smac
, dev_addr
.src_dev_addr
, ETH_ALEN
);
780 EXPORT_SYMBOL(rdma_addr_find_smac_by_sgid
);
782 static int netevent_callback(struct notifier_block
*self
, unsigned long event
,
785 if (event
== NETEVENT_NEIGH_UPDATE
) {
786 struct neighbour
*neigh
= ctx
;
788 if (neigh
->nud_state
& NUD_VALID
) {
789 set_timeout(jiffies
);
795 static struct notifier_block nb
= {
796 .notifier_call
= netevent_callback
801 addr_wq
= alloc_workqueue("ib_addr", WQ_MEM_RECLAIM
, 0);
805 register_netevent_notifier(&nb
);
806 rdma_addr_register_client(&self
);
811 void addr_cleanup(void)
813 rdma_addr_unregister_client(&self
);
814 unregister_netevent_notifier(&nb
);
815 destroy_workqueue(addr_wq
);