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