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