xref: /freebsd/sys/ofed/drivers/infiniband/core/ib_addr.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
5  * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
6  * Copyright (c) 1999-2019, Mellanox Technologies, Inc. All rights reserved.
7  * Copyright (c) 2005 Intel Corporation.  All rights reserved.
8  *
9  * This software is available to you under a choice of one of two
10  * licenses.  You may choose to be licensed under the terms of the GNU
11  * General Public License (GPL) Version 2, available from the file
12  * COPYING in the main directory of this source tree, or the
13  * OpenIB.org BSD license below:
14  *
15  *     Redistribution and use in source and binary forms, with or
16  *     without modification, are permitted provided that the following
17  *     conditions are met:
18  *
19  *      - Redistributions of source code must retain the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer.
22  *
23  *      - Redistributions in binary form must reproduce the above
24  *        copyright notice, this list of conditions and the following
25  *        disclaimer in the documentation and/or other materials
26  *        provided with the distribution.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35  * SOFTWARE.
36  */
37 
38 #include <sys/cdefs.h>
39 #include <linux/mutex.h>
40 #include <linux/slab.h>
41 #include <linux/workqueue.h>
42 #include <linux/module.h>
43 #include <net/if_llatbl.h>
44 #include <net/route.h>
45 #include <net/route/nhop.h>
46 #include <net/netevent.h>
47 #include <rdma/ib_addr.h>
48 #include <rdma/ib.h>
49 
50 #include <netinet/in_fib.h>
51 #include <netinet/if_ether.h>
52 #include <netinet/ip_var.h>
53 #include <netinet6/scope6_var.h>
54 #include <netinet6/in6_pcb.h>
55 #include <netinet6/in6_fib.h>
56 
57 #include "core_priv.h"
58 
59 struct addr_req {
60 	struct list_head list;
61 	struct sockaddr_storage src_addr;
62 	struct sockaddr_storage dst_addr;
63 	struct rdma_dev_addr *addr;
64 	struct rdma_addr_client *client;
65 	void *context;
66 	void (*callback)(int status, struct sockaddr *src_addr,
67 			 struct rdma_dev_addr *addr, void *context);
68 	int timeout;
69 	int status;
70 };
71 
72 static void process_req(struct work_struct *work);
73 
74 static DEFINE_MUTEX(lock);
75 static LIST_HEAD(req_list);
76 static DECLARE_DELAYED_WORK(work, process_req);
77 static struct workqueue_struct *addr_wq;
78 
rdma_addr_size(struct sockaddr * addr)79 int rdma_addr_size(struct sockaddr *addr)
80 {
81 	switch (addr->sa_family) {
82 	case AF_INET:
83 		return sizeof(struct sockaddr_in);
84 	case AF_INET6:
85 		return sizeof(struct sockaddr_in6);
86 	case AF_IB:
87 		return sizeof(struct sockaddr_ib);
88 	default:
89 		return 0;
90 	}
91 }
92 EXPORT_SYMBOL(rdma_addr_size);
93 
rdma_addr_size_in6(struct sockaddr_in6 * addr)94 int rdma_addr_size_in6(struct sockaddr_in6 *addr)
95 {
96 	int ret = rdma_addr_size((struct sockaddr *) addr);
97 
98 	return ret <= sizeof(*addr) ? ret : 0;
99 }
100 EXPORT_SYMBOL(rdma_addr_size_in6);
101 
rdma_addr_size_kss(struct sockaddr_storage * addr)102 int rdma_addr_size_kss(struct sockaddr_storage *addr)
103 {
104 	int ret = rdma_addr_size((struct sockaddr *) addr);
105 
106 	return ret <= sizeof(*addr) ? ret : 0;
107 }
108 EXPORT_SYMBOL(rdma_addr_size_kss);
109 
110 static struct rdma_addr_client self;
111 
rdma_addr_register_client(struct rdma_addr_client * client)112 void rdma_addr_register_client(struct rdma_addr_client *client)
113 {
114 	atomic_set(&client->refcount, 1);
115 	init_completion(&client->comp);
116 }
117 EXPORT_SYMBOL(rdma_addr_register_client);
118 
put_client(struct rdma_addr_client * client)119 static inline void put_client(struct rdma_addr_client *client)
120 {
121 	if (atomic_dec_and_test(&client->refcount))
122 		complete(&client->comp);
123 }
124 
rdma_addr_unregister_client(struct rdma_addr_client * client)125 void rdma_addr_unregister_client(struct rdma_addr_client *client)
126 {
127 	put_client(client);
128 	wait_for_completion(&client->comp);
129 }
130 EXPORT_SYMBOL(rdma_addr_unregister_client);
131 
132 static inline void
rdma_copy_addr_sub(u8 * dst,const u8 * src,unsigned min,unsigned max)133 rdma_copy_addr_sub(u8 *dst, const u8 *src, unsigned min, unsigned max)
134 {
135 	if (min > max)
136 		min = max;
137 	memcpy(dst, src, min);
138 	memset(dst + min, 0, max - min);
139 }
140 
rdma_copy_addr(struct rdma_dev_addr * dev_addr,if_t dev,const unsigned char * dst_dev_addr)141 int rdma_copy_addr(struct rdma_dev_addr *dev_addr, if_t dev,
142 		     const unsigned char *dst_dev_addr)
143 {
144 	int dev_type = if_gettype(dev);
145 
146 	/* check for loopback device */
147 	if (if_getflags(dev) & IFF_LOOPBACK) {
148 		dev_addr->dev_type = ARPHRD_ETHER;
149 		memset(dev_addr->src_dev_addr, 0, MAX_ADDR_LEN);
150 		memset(dev_addr->broadcast, 0, MAX_ADDR_LEN);
151 		memset(dev_addr->dst_dev_addr, 0, MAX_ADDR_LEN);
152 		dev_addr->bound_dev_if = if_getindex(dev);
153 		return (0);
154 	} else if (dev_type == IFT_INFINIBAND)
155 		dev_addr->dev_type = ARPHRD_INFINIBAND;
156 	else if (dev_type == IFT_ETHER || dev_type == IFT_L2VLAN)
157 		dev_addr->dev_type = ARPHRD_ETHER;
158 	else
159 		dev_addr->dev_type = 0;
160 	rdma_copy_addr_sub(dev_addr->src_dev_addr, if_getlladdr(dev),
161 			   if_getaddrlen(dev), MAX_ADDR_LEN);
162 	rdma_copy_addr_sub(dev_addr->broadcast, if_getbroadcastaddr(dev),
163 			   if_getaddrlen(dev), MAX_ADDR_LEN);
164 	if (dst_dev_addr != NULL) {
165 		rdma_copy_addr_sub(dev_addr->dst_dev_addr, dst_dev_addr,
166 				   if_getaddrlen(dev), MAX_ADDR_LEN);
167 	}
168 	dev_addr->bound_dev_if = if_getindex(dev);
169 	return 0;
170 }
171 EXPORT_SYMBOL(rdma_copy_addr);
172 
rdma_translate_ip(const struct sockaddr * addr,struct rdma_dev_addr * dev_addr)173 int rdma_translate_ip(const struct sockaddr *addr,
174 		      struct rdma_dev_addr *dev_addr)
175 {
176 	if_t dev;
177 	int ret;
178 
179 	if (dev_addr->bound_dev_if) {
180 		dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
181 	} else switch (addr->sa_family) {
182 #ifdef INET
183 	case AF_INET:
184 		dev = ip_ifp_find(dev_addr->net,
185 			((const struct sockaddr_in *)addr)->sin_addr.s_addr);
186 		break;
187 #endif
188 #ifdef INET6
189 	case AF_INET6:
190 		dev = ip6_ifp_find(dev_addr->net,
191 			((const struct sockaddr_in6 *)addr)->sin6_addr, 0);
192 		break;
193 #endif
194 	default:
195 		dev = NULL;
196 		break;
197 	}
198 
199 	if (dev != NULL) {
200 		/* disallow connections through 127.0.0.1 itself */
201 		if (if_getflags(dev) & IFF_LOOPBACK)
202 			ret = -EINVAL;
203 		else
204 			ret = rdma_copy_addr(dev_addr, dev, NULL);
205 		dev_put(dev);
206 	} else {
207 		ret = -ENODEV;
208 	}
209 	return ret;
210 }
211 EXPORT_SYMBOL(rdma_translate_ip);
212 
set_timeout(int time)213 static void set_timeout(int time)
214 {
215 	int delay;	/* under FreeBSD ticks are 32-bit */
216 
217 	delay = time - jiffies;
218 	if (delay <= 0)
219 		delay = 1;
220 	else if (delay > hz)
221 		delay = hz;
222 
223 	mod_delayed_work(addr_wq, &work, delay);
224 }
225 
queue_req(struct addr_req * req)226 static void queue_req(struct addr_req *req)
227 {
228 	struct addr_req *temp_req;
229 
230 	mutex_lock(&lock);
231 	list_for_each_entry_reverse(temp_req, &req_list, list) {
232 		if (time_after_eq(req->timeout, temp_req->timeout))
233 			break;
234 	}
235 
236 	list_add(&req->list, &temp_req->list);
237 
238 	if (req_list.next == &req->list)
239 		set_timeout(req->timeout);
240 	mutex_unlock(&lock);
241 }
242 
243 #if defined(INET) || defined(INET6)
addr_resolve_multi(u8 * edst,if_t ifp,struct sockaddr * dst_in)244 static int addr_resolve_multi(u8 *edst, if_t ifp, struct sockaddr *dst_in)
245 {
246 	struct sockaddr *llsa;
247 	struct sockaddr_dl sdl;
248 	int error;
249 
250 	sdl.sdl_len = sizeof(sdl);
251 	llsa = (struct sockaddr *)&sdl;
252 
253 	error = if_resolvemulti(ifp, &llsa, dst_in);
254 	if (error == 0) {
255 		rdma_copy_addr_sub(edst, LLADDR((struct sockaddr_dl *)llsa),
256 		    if_getaddrlen(ifp), MAX_ADDR_LEN);
257 	}
258 	return (error);
259 }
260 #endif
261 
262 #ifdef INET
addr4_resolve(struct sockaddr_in * src_in,const struct sockaddr_in * dst_in,struct rdma_dev_addr * addr,u8 * edst,if_t * ifpp)263 static int addr4_resolve(struct sockaddr_in *src_in,
264 			 const struct sockaddr_in *dst_in,
265 			 struct rdma_dev_addr *addr,
266 			 u8 *edst,
267 			 if_t *ifpp)
268 {
269 	enum {
270 		ADDR_VALID = 0,
271 		ADDR_SRC_ANY = 1,
272 		ADDR_DST_ANY = 2,
273 	};
274 	struct sockaddr_in dst_tmp = *dst_in;
275 	in_port_t src_port;
276 	struct sockaddr *saddr = NULL;
277 	struct nhop_object *nh;
278 	if_t ifp;
279 	int error;
280 	int type;
281 
282 	NET_EPOCH_ASSERT();
283 
284 	/* set VNET, if any */
285 	CURVNET_SET(addr->net);
286 
287 	/* set default TTL limit */
288 	addr->hoplimit = V_ip_defttl;
289 
290 	type = ADDR_VALID;
291 	if (src_in->sin_addr.s_addr == INADDR_ANY)
292 		type |= ADDR_SRC_ANY;
293 	if (dst_tmp.sin_addr.s_addr == INADDR_ANY)
294 		type |= ADDR_DST_ANY;
295 
296 	/*
297 	 * Make sure the socket address length field is set.
298 	 */
299 	dst_tmp.sin_len = sizeof(dst_tmp);
300 
301 	/* Step 1 - lookup destination route if any */
302 	switch (type) {
303 	case ADDR_VALID:
304 	case ADDR_SRC_ANY:
305 		/* regular destination route lookup */
306 		nh = fib4_lookup(RT_DEFAULT_FIB, dst_tmp.sin_addr,0,NHR_NONE,0);
307 		if (nh == NULL) {
308 			error = EHOSTUNREACH;
309 			goto done;
310 		}
311 		break;
312 	default:
313 		error = ENETUNREACH;
314 		goto done;
315 	}
316 
317 	/* Step 2 - find outgoing network interface */
318 	switch (type) {
319 	case ADDR_VALID:
320 		/* get source interface */
321 		if (addr->bound_dev_if != 0) {
322 			ifp = dev_get_by_index(addr->net, addr->bound_dev_if);
323 		} else {
324 			ifp = ip_ifp_find(addr->net, src_in->sin_addr.s_addr);
325 		}
326 
327 		/* check source interface */
328 		if (ifp == NULL) {
329 			error = ENETUNREACH;
330 			goto done;
331 		} else if (if_getflags(ifp) & IFF_LOOPBACK) {
332 			/*
333 			 * Source address cannot be a loopback device.
334 			 */
335 			error = EHOSTUNREACH;
336 			goto error_put_ifp;
337 		} else if (if_getflags(nh->nh_ifp) & IFF_LOOPBACK) {
338 			if (memcmp(&src_in->sin_addr, &dst_in->sin_addr,
339 			    sizeof(src_in->sin_addr))) {
340 				/*
341 				 * Destination is loopback, but source
342 				 * and destination address is not the
343 				 * same.
344 				 */
345 				error = EHOSTUNREACH;
346 				goto error_put_ifp;
347 			}
348 			/* get destination network interface from route */
349 			dev_put(ifp);
350 			ifp = nh->nh_ifp;
351 			dev_hold(ifp);
352 		} else if (ifp != nh->nh_ifp) {
353 			/*
354 			 * Source and destination interfaces are
355 			 * different.
356 			 */
357 			error = ENETUNREACH;
358 			goto error_put_ifp;
359 		}
360 		break;
361 	case ADDR_SRC_ANY:
362 		/* check for loopback device */
363 		if (if_getflags(nh->nh_ifp) & IFF_LOOPBACK)
364 			saddr = (struct sockaddr *)&dst_tmp;
365 		else
366 			saddr = nh->nh_ifa->ifa_addr;
367 
368 		/* get destination network interface from route */
369 		ifp = nh->nh_ifp;
370 		dev_hold(ifp);
371 		break;
372 	default:
373 		break;
374 	}
375 
376 	/*
377 	 * Step 3 - resolve destination MAC address
378 	 */
379 	if (dst_tmp.sin_addr.s_addr == INADDR_BROADCAST) {
380 		rdma_copy_addr_sub(edst, if_getbroadcastaddr(ifp),
381 		    if_getaddrlen(ifp), MAX_ADDR_LEN);
382 		error = 0;
383 	} else if (IN_MULTICAST(ntohl(dst_tmp.sin_addr.s_addr))) {
384 		bool is_gw = (nh->nh_flags & NHF_GATEWAY) != 0;
385 		error = addr_resolve_multi(edst, ifp, (struct sockaddr *)&dst_tmp);
386 		if (error != 0)
387 			goto error_put_ifp;
388 		else if (is_gw)
389 			addr->network = RDMA_NETWORK_IPV4;
390 	} else if (if_getflags(ifp) & IFF_LOOPBACK) {
391 		memset(edst, 0, MAX_ADDR_LEN);
392 		error = 0;
393 	} else {
394 		bool is_gw = (nh->nh_flags & NHF_GATEWAY) != 0;
395 		memset(edst, 0, MAX_ADDR_LEN);
396 #ifdef INET6
397 		if (is_gw && nh->gw_sa.sa_family == AF_INET6)
398 			error = nd6_resolve(ifp, LLE_SF(AF_INET, is_gw), NULL,
399 			    &nh->gw_sa, edst, NULL, NULL);
400 		else
401 #endif
402 			error = arpresolve(ifp, is_gw, NULL, is_gw ?
403 			    &nh->gw_sa : (const struct sockaddr *)&dst_tmp,
404 			    edst, NULL, NULL);
405 
406 		if (error != 0)
407 			goto error_put_ifp;
408 		else if (is_gw)
409 			addr->network = RDMA_NETWORK_IPV4;
410 	}
411 
412 	/*
413 	 * Step 4 - update source address, if any
414 	 */
415 	if (saddr != NULL) {
416 		src_port = src_in->sin_port;
417 		memcpy(src_in, saddr, rdma_addr_size(saddr));
418 		src_in->sin_port = src_port;	/* preserve port number */
419 	}
420 
421 	*ifpp = ifp;
422 
423 	goto done;
424 
425 error_put_ifp:
426 	dev_put(ifp);
427 done:
428 	CURVNET_RESTORE();
429 
430 	if (error == EWOULDBLOCK || error == EAGAIN)
431 		error = ENODATA;
432 	return (-error);
433 }
434 #else
addr4_resolve(struct sockaddr_in * src_in,const struct sockaddr_in * dst_in,struct rdma_dev_addr * addr,u8 * edst,if_t * ifpp)435 static int addr4_resolve(struct sockaddr_in *src_in,
436 			 const struct sockaddr_in *dst_in,
437 			 struct rdma_dev_addr *addr,
438 			 u8 *edst,
439 			 if_t *ifpp)
440 {
441 	return -EADDRNOTAVAIL;
442 }
443 #endif
444 
445 #ifdef INET6
addr6_resolve(struct sockaddr_in6 * src_in,const struct sockaddr_in6 * dst_in,struct rdma_dev_addr * addr,u8 * edst,if_t * ifpp)446 static int addr6_resolve(struct sockaddr_in6 *src_in,
447 			 const struct sockaddr_in6 *dst_in,
448 			 struct rdma_dev_addr *addr,
449 			 u8 *edst,
450 			 if_t *ifpp)
451 {
452 	enum {
453 		ADDR_VALID = 0,
454 		ADDR_SRC_ANY = 1,
455 		ADDR_DST_ANY = 2,
456 	};
457 	struct sockaddr_in6 dst_tmp = *dst_in;
458 	in_port_t src_port;
459 	struct sockaddr *saddr = NULL;
460 	struct nhop_object *nh;
461 	if_t ifp;
462 	int error;
463 	int type;
464 
465 	NET_EPOCH_ASSERT();
466 
467 	/* set VNET, if any */
468 	CURVNET_SET(addr->net);
469 
470 	/* set default TTL limit */
471 	addr->hoplimit = V_ip_defttl;
472 
473 	type = ADDR_VALID;
474 	if (ipv6_addr_any(&src_in->sin6_addr))
475 		type |= ADDR_SRC_ANY;
476 	if (ipv6_addr_any(&dst_tmp.sin6_addr))
477 		type |= ADDR_DST_ANY;
478 
479 	/*
480 	 * Make sure the socket address length field is set.
481 	 */
482 	dst_tmp.sin6_len = sizeof(dst_tmp);
483 
484 	/*
485 	 * Make sure the scope ID gets embedded, else nd6_resolve() will
486 	 * not find the record.
487 	 */
488 	dst_tmp.sin6_scope_id = addr->bound_dev_if;
489 	sa6_embedscope(&dst_tmp, 0);
490 
491 	/* Step 1 - lookup destination route if any */
492 	switch (type) {
493 	case ADDR_VALID:
494 		/* sanity check for IPv4 addresses */
495 		if (ipv6_addr_v4mapped(&src_in->sin6_addr) !=
496 		    ipv6_addr_v4mapped(&dst_tmp.sin6_addr)) {
497 			error = EAFNOSUPPORT;
498 			goto done;
499 		}
500 		/* FALLTHROUGH */
501 	case ADDR_SRC_ANY:
502 		/* regular destination route lookup */
503 		nh = fib6_lookup(RT_DEFAULT_FIB, &dst_in->sin6_addr,
504 		    addr->bound_dev_if, NHR_NONE, 0);
505 		if (nh == NULL) {
506 			error = EHOSTUNREACH;
507 			goto done;
508 		}
509 		break;
510 	default:
511 		error = ENETUNREACH;
512 		goto done;
513 	}
514 
515 	/* Step 2 - find outgoing network interface */
516 	switch (type) {
517 	case ADDR_VALID:
518 		/* get source interface */
519 		if (addr->bound_dev_if != 0) {
520 			ifp = dev_get_by_index(addr->net, addr->bound_dev_if);
521 		} else {
522 			ifp = ip6_ifp_find(addr->net, src_in->sin6_addr, 0);
523 		}
524 
525 		/* check source interface */
526 		if (ifp == NULL) {
527 			error = ENETUNREACH;
528 			goto done;
529 		} else if (if_getflags(ifp) & IFF_LOOPBACK) {
530 			/*
531 			 * Source address cannot be a loopback device.
532 			 */
533 			error = EHOSTUNREACH;
534 			goto error_put_ifp;
535 		} else if (if_getflags(nh->nh_ifp) & IFF_LOOPBACK) {
536 			if (memcmp(&src_in->sin6_addr, &dst_in->sin6_addr,
537 			    sizeof(src_in->sin6_addr))) {
538 				/*
539 				 * Destination is loopback, but source
540 				 * and destination address is not the
541 				 * same.
542 				 */
543 				error = EHOSTUNREACH;
544 				goto error_put_ifp;
545 			}
546 			/* get destination network interface from route */
547 			dev_put(ifp);
548 			ifp = nh->nh_ifp;
549 			dev_hold(ifp);
550 		} else if (ifp != nh->nh_ifp) {
551 			/*
552 			 * Source and destination interfaces are
553 			 * different.
554 			 */
555 			error = ENETUNREACH;
556 			goto error_put_ifp;
557 		}
558 		break;
559 	case ADDR_SRC_ANY:
560 		/* check for loopback device */
561 		if (if_getflags(nh->nh_ifp) & IFF_LOOPBACK)
562 			saddr = (struct sockaddr *)&dst_tmp;
563 		else
564 			saddr = nh->nh_ifa->ifa_addr;
565 
566 		/* get destination network interface from route */
567 		ifp = nh->nh_ifp;
568 		dev_hold(ifp);
569 		break;
570 	default:
571 		break;
572 	}
573 
574 	/*
575 	 * Step 3 - resolve destination MAC address
576 	 */
577 	if (IN6_IS_ADDR_MULTICAST(&dst_tmp.sin6_addr)) {
578 		bool is_gw = (nh->nh_flags & NHF_GATEWAY) != 0;
579 		error = addr_resolve_multi(edst, ifp,
580 		    (struct sockaddr *)&dst_tmp);
581 		if (error != 0)
582 			goto error_put_ifp;
583 		else if (is_gw)
584 			addr->network = RDMA_NETWORK_IPV6;
585 	} else if (if_getflags(nh->nh_ifp) & IFF_LOOPBACK) {
586 		memset(edst, 0, MAX_ADDR_LEN);
587 		error = 0;
588 	} else {
589 		bool is_gw = (nh->nh_flags & NHF_GATEWAY) != 0;
590 		memset(edst, 0, MAX_ADDR_LEN);
591 		error = nd6_resolve(ifp, LLE_SF(AF_INET6, is_gw), NULL,
592 		    is_gw ? &nh->gw_sa : (const struct sockaddr *)&dst_tmp,
593 		    edst, NULL, NULL);
594 		if (error != 0)
595 			goto error_put_ifp;
596 		else if (is_gw)
597 			addr->network = RDMA_NETWORK_IPV6;
598 	}
599 
600 	/*
601 	 * Step 4 - update source address, if any
602 	 */
603 	if (saddr != NULL) {
604 		src_port = src_in->sin6_port;
605 		memcpy(src_in, saddr, rdma_addr_size(saddr));
606 		src_in->sin6_port = src_port;	/* preserve port number */
607 	}
608 
609 	*ifpp = ifp;
610 
611 	goto done;
612 
613 error_put_ifp:
614 	dev_put(ifp);
615 done:
616 	CURVNET_RESTORE();
617 
618 	if (error == EWOULDBLOCK || error == EAGAIN)
619 		error = ENODATA;
620 	return (-error);
621 }
622 #else
addr6_resolve(struct sockaddr_in6 * src_in,const struct sockaddr_in6 * dst_in,struct rdma_dev_addr * addr,u8 * edst,if_t * ifpp)623 static int addr6_resolve(struct sockaddr_in6 *src_in,
624 			 const struct sockaddr_in6 *dst_in,
625 			 struct rdma_dev_addr *addr,
626 			 u8 *edst,
627 			 if_t *ifpp)
628 {
629 	return -EADDRNOTAVAIL;
630 }
631 #endif
632 
addr_resolve_neigh(if_t dev,const struct sockaddr * dst_in,u8 * edst,struct rdma_dev_addr * addr)633 static int addr_resolve_neigh(if_t dev,
634 			      const struct sockaddr *dst_in,
635 			      u8 *edst,
636 			      struct rdma_dev_addr *addr)
637 {
638 	if (if_getflags(dev) & IFF_LOOPBACK) {
639 		int ret;
640 
641 		/*
642 		 * Binding to a loopback device is not allowed. Make
643 		 * sure the destination device address is global by
644 		 * clearing the bound device interface:
645 		 */
646 		if (addr->bound_dev_if == if_getindex(dev))
647 			addr->bound_dev_if = 0;
648 
649 		ret = rdma_translate_ip(dst_in, addr);
650 		if (ret == 0) {
651 			memcpy(addr->dst_dev_addr, addr->src_dev_addr,
652 			       MAX_ADDR_LEN);
653 		}
654 		return ret;
655 	}
656 
657 	/* If the device doesn't do ARP internally */
658 	if (!(if_getflags(dev) & IFF_NOARP))
659 		return rdma_copy_addr(addr, dev, edst);
660 
661 	return rdma_copy_addr(addr, dev, NULL);
662 }
663 
addr_resolve(struct sockaddr * src_in,const struct sockaddr * dst_in,struct rdma_dev_addr * addr)664 static int addr_resolve(struct sockaddr *src_in,
665 			const struct sockaddr *dst_in,
666 			struct rdma_dev_addr *addr)
667 {
668 	struct epoch_tracker et;
669 	if_t ndev = NULL;
670 	u8 edst[MAX_ADDR_LEN];
671 	int ret;
672 
673 	if (dst_in->sa_family != src_in->sa_family)
674 		return -EINVAL;
675 
676 	NET_EPOCH_ENTER(et);
677 	switch (src_in->sa_family) {
678 	case AF_INET:
679 		ret = addr4_resolve((struct sockaddr_in *)src_in,
680 				    (const struct sockaddr_in *)dst_in,
681 				    addr, edst, &ndev);
682 		break;
683 	case AF_INET6:
684 		ret = addr6_resolve((struct sockaddr_in6 *)src_in,
685 				    (const struct sockaddr_in6 *)dst_in, addr,
686 				    edst, &ndev);
687 		break;
688 	default:
689 		ret = -EADDRNOTAVAIL;
690 		break;
691 	}
692 	NET_EPOCH_EXIT(et);
693 
694 	/* check for error */
695 	if (ret != 0)
696 		return ret;
697 
698 	/* store MAC addresses and check for loopback */
699 	ret = addr_resolve_neigh(ndev, dst_in, edst, addr);
700 
701 	/* set belonging VNET, if any */
702 	addr->net = dev_net(ndev);
703 	dev_put(ndev);
704 
705 	return ret;
706 }
707 
process_req(struct work_struct * work)708 static void process_req(struct work_struct *work)
709 {
710 	struct addr_req *req, *temp_req;
711 	struct sockaddr *src_in, *dst_in;
712 	struct list_head done_list;
713 
714 	INIT_LIST_HEAD(&done_list);
715 
716 	mutex_lock(&lock);
717 	list_for_each_entry_safe(req, temp_req, &req_list, list) {
718 		if (req->status == -ENODATA) {
719 			src_in = (struct sockaddr *) &req->src_addr;
720 			dst_in = (struct sockaddr *) &req->dst_addr;
721 			req->status = addr_resolve(src_in, dst_in, req->addr);
722 			if (req->status && time_after_eq(jiffies, req->timeout))
723 				req->status = -ETIMEDOUT;
724 			else if (req->status == -ENODATA)
725 				continue;
726 		}
727 		list_move_tail(&req->list, &done_list);
728 	}
729 
730 	if (!list_empty(&req_list)) {
731 		req = list_entry(req_list.next, struct addr_req, list);
732 		set_timeout(req->timeout);
733 	}
734 	mutex_unlock(&lock);
735 
736 	list_for_each_entry_safe(req, temp_req, &done_list, list) {
737 		list_del(&req->list);
738 		req->callback(req->status, (struct sockaddr *) &req->src_addr,
739 			req->addr, req->context);
740 		put_client(req->client);
741 		kfree(req);
742 	}
743 }
744 
rdma_resolve_ip(struct rdma_addr_client * client,struct sockaddr * src_addr,struct sockaddr * dst_addr,struct rdma_dev_addr * addr,int timeout_ms,void (* callback)(int status,struct sockaddr * src_addr,struct rdma_dev_addr * addr,void * context),void * context)745 int rdma_resolve_ip(struct rdma_addr_client *client,
746 		    struct sockaddr *src_addr, struct sockaddr *dst_addr,
747 		    struct rdma_dev_addr *addr, int timeout_ms,
748 		    void (*callback)(int status, struct sockaddr *src_addr,
749 				     struct rdma_dev_addr *addr, void *context),
750 		    void *context)
751 {
752 	struct sockaddr *src_in, *dst_in;
753 	struct addr_req *req;
754 	int ret = 0;
755 
756 	req = kzalloc(sizeof *req, GFP_KERNEL);
757 	if (!req)
758 		return -ENOMEM;
759 
760 	src_in = (struct sockaddr *) &req->src_addr;
761 	dst_in = (struct sockaddr *) &req->dst_addr;
762 
763 	if (src_addr) {
764 		if (src_addr->sa_family != dst_addr->sa_family) {
765 			ret = -EINVAL;
766 			goto err;
767 		}
768 
769 		memcpy(src_in, src_addr, rdma_addr_size(src_addr));
770 	} else {
771 		src_in->sa_family = dst_addr->sa_family;
772 	}
773 
774 	memcpy(dst_in, dst_addr, rdma_addr_size(dst_addr));
775 	req->addr = addr;
776 	req->callback = callback;
777 	req->context = context;
778 	req->client = client;
779 	atomic_inc(&client->refcount);
780 
781 	req->status = addr_resolve(src_in, dst_in, addr);
782 	switch (req->status) {
783 	case 0:
784 		req->timeout = jiffies;
785 		queue_req(req);
786 		break;
787 	case -ENODATA:
788 		req->timeout = msecs_to_jiffies(timeout_ms) + jiffies;
789 		queue_req(req);
790 		break;
791 	default:
792 		ret = req->status;
793 		atomic_dec(&client->refcount);
794 		goto err;
795 	}
796 	return ret;
797 err:
798 	kfree(req);
799 	return ret;
800 }
801 EXPORT_SYMBOL(rdma_resolve_ip);
802 
rdma_resolve_ip_route(struct sockaddr * src_addr,const struct sockaddr * dst_addr,struct rdma_dev_addr * addr)803 int rdma_resolve_ip_route(struct sockaddr *src_addr,
804 			  const struct sockaddr *dst_addr,
805 			  struct rdma_dev_addr *addr)
806 {
807 	struct sockaddr_storage ssrc_addr = {};
808 	struct sockaddr *src_in = (struct sockaddr *)&ssrc_addr;
809 
810 	if (src_addr) {
811 		if (src_addr->sa_family != dst_addr->sa_family)
812 			return -EINVAL;
813 
814 		memcpy(src_in, src_addr, rdma_addr_size(src_addr));
815 	} else {
816 		src_in->sa_family = dst_addr->sa_family;
817 	}
818 
819 	return addr_resolve(src_in, dst_addr, addr);
820 }
821 EXPORT_SYMBOL(rdma_resolve_ip_route);
822 
rdma_addr_cancel(struct rdma_dev_addr * addr)823 void rdma_addr_cancel(struct rdma_dev_addr *addr)
824 {
825 	struct addr_req *req, *temp_req;
826 
827 	mutex_lock(&lock);
828 	list_for_each_entry_safe(req, temp_req, &req_list, list) {
829 		if (req->addr == addr) {
830 			req->status = -ECANCELED;
831 			req->timeout = jiffies;
832 			list_move(&req->list, &req_list);
833 			set_timeout(req->timeout);
834 			break;
835 		}
836 	}
837 	mutex_unlock(&lock);
838 }
839 EXPORT_SYMBOL(rdma_addr_cancel);
840 
841 struct resolve_cb_context {
842 	struct rdma_dev_addr *addr;
843 	struct completion comp;
844 	int status;
845 };
846 
resolve_cb(int status,struct sockaddr * src_addr,struct rdma_dev_addr * addr,void * context)847 static void resolve_cb(int status, struct sockaddr *src_addr,
848 	     struct rdma_dev_addr *addr, void *context)
849 {
850 	if (!status)
851 		memcpy(((struct resolve_cb_context *)context)->addr,
852 		       addr, sizeof(struct rdma_dev_addr));
853 	((struct resolve_cb_context *)context)->status = status;
854 	complete(&((struct resolve_cb_context *)context)->comp);
855 }
856 
rdma_addr_find_l2_eth_by_grh(const union ib_gid * sgid,const union ib_gid * dgid,u8 * dmac,if_t dev,int * hoplimit)857 int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
858 				 const union ib_gid *dgid,
859 				 u8 *dmac, if_t dev,
860 				 int *hoplimit)
861 {
862 	int ret = 0;
863 	struct rdma_dev_addr dev_addr;
864 	struct resolve_cb_context ctx;
865 
866 	union rdma_sockaddr sgid_addr, dgid_addr;
867 
868 	rdma_gid2ip(&sgid_addr._sockaddr, sgid);
869 	rdma_gid2ip(&dgid_addr._sockaddr, dgid);
870 
871 	memset(&dev_addr, 0, sizeof(dev_addr));
872 
873 	dev_addr.bound_dev_if = if_getindex(dev);
874 	dev_addr.net = dev_net(dev);
875 
876 	ctx.addr = &dev_addr;
877 	init_completion(&ctx.comp);
878 	ret = rdma_resolve_ip(&self, &sgid_addr._sockaddr, &dgid_addr._sockaddr,
879 			&dev_addr, 1000, resolve_cb, &ctx);
880 	if (ret)
881 		return ret;
882 
883 	wait_for_completion(&ctx.comp);
884 
885 	ret = ctx.status;
886 	if (ret)
887 		return ret;
888 
889 	memcpy(dmac, dev_addr.dst_dev_addr, ETH_ALEN);
890 	if (hoplimit)
891 		*hoplimit = dev_addr.hoplimit;
892 	return ret;
893 }
894 EXPORT_SYMBOL(rdma_addr_find_l2_eth_by_grh);
895 
addr_init(void)896 int addr_init(void)
897 {
898 	addr_wq = alloc_workqueue("ib_addr", WQ_MEM_RECLAIM, 0);
899 	if (!addr_wq)
900 		return -ENOMEM;
901 
902 	rdma_addr_register_client(&self);
903 
904 	return 0;
905 }
906 
addr_cleanup(void)907 void addr_cleanup(void)
908 {
909 	rdma_addr_unregister_client(&self);
910 	destroy_workqueue(addr_wq);
911 }
912