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