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 if (!(n->nud_state & NUD_VALID)) { 324 neigh_event_send(n, NULL); 325 ret = -ENODATA; 326 } else { 327 neigh_ha_snapshot(dev_addr->dst_dev_addr, n, dst->dev); 328 } 329 330 neigh_release(n); 331 332 return ret; 333 } 334 335 static bool has_gateway(const struct dst_entry *dst, sa_family_t family) 336 { 337 if (family == AF_INET) 338 return dst_rtable(dst)->rt_uses_gateway; 339 340 return dst_rt6_info(dst)->rt6i_flags & RTF_GATEWAY; 341 } 342 343 static int fetch_ha(const struct dst_entry *dst, struct rdma_dev_addr *dev_addr, 344 const struct sockaddr *dst_in, u32 seq) 345 { 346 const struct sockaddr_in *dst_in4 = 347 (const struct sockaddr_in *)dst_in; 348 const struct sockaddr_in6 *dst_in6 = 349 (const struct sockaddr_in6 *)dst_in; 350 const void *daddr = (dst_in->sa_family == AF_INET) ? 351 (const void *)&dst_in4->sin_addr.s_addr : 352 (const void *)&dst_in6->sin6_addr; 353 sa_family_t family = dst_in->sa_family; 354 355 might_sleep(); 356 357 /* If we have a gateway in IB mode then it must be an IB network */ 358 if (has_gateway(dst, family) && dev_addr->network == RDMA_NETWORK_IB) 359 return ib_nl_fetch_ha(dev_addr, daddr, seq, family); 360 else 361 return dst_fetch_ha(dst, dev_addr, daddr); 362 } 363 364 static int addr4_resolve(struct sockaddr *src_sock, 365 const struct sockaddr *dst_sock, 366 struct rdma_dev_addr *addr, 367 struct rtable **prt) 368 { 369 struct sockaddr_in *src_in = (struct sockaddr_in *)src_sock; 370 const struct sockaddr_in *dst_in = 371 (const struct sockaddr_in *)dst_sock; 372 373 __be32 src_ip = src_in->sin_addr.s_addr; 374 __be32 dst_ip = dst_in->sin_addr.s_addr; 375 struct rtable *rt; 376 struct flowi4 fl4; 377 int ret; 378 379 memset(&fl4, 0, sizeof(fl4)); 380 fl4.daddr = dst_ip; 381 fl4.saddr = src_ip; 382 fl4.flowi4_oif = addr->bound_dev_if; 383 rt = ip_route_output_key(addr->net, &fl4); 384 ret = PTR_ERR_OR_ZERO(rt); 385 if (ret) 386 return ret; 387 388 src_in->sin_addr.s_addr = fl4.saddr; 389 390 addr->hoplimit = ip4_dst_hoplimit(&rt->dst); 391 392 *prt = rt; 393 return 0; 394 } 395 396 #if IS_ENABLED(CONFIG_IPV6) 397 static int addr6_resolve(struct sockaddr *src_sock, 398 const struct sockaddr *dst_sock, 399 struct rdma_dev_addr *addr, 400 struct dst_entry **pdst) 401 { 402 struct sockaddr_in6 *src_in = (struct sockaddr_in6 *)src_sock; 403 const struct sockaddr_in6 *dst_in = 404 (const struct sockaddr_in6 *)dst_sock; 405 struct flowi6 fl6; 406 struct dst_entry *dst; 407 408 memset(&fl6, 0, sizeof fl6); 409 fl6.daddr = dst_in->sin6_addr; 410 fl6.saddr = src_in->sin6_addr; 411 fl6.flowi6_oif = addr->bound_dev_if; 412 413 dst = ip6_dst_lookup_flow(addr->net, NULL, &fl6, NULL); 414 if (IS_ERR(dst)) 415 return PTR_ERR(dst); 416 417 if (ipv6_addr_any(&src_in->sin6_addr)) 418 src_in->sin6_addr = fl6.saddr; 419 420 addr->hoplimit = ip6_dst_hoplimit(dst); 421 422 *pdst = dst; 423 return 0; 424 } 425 #else 426 static int addr6_resolve(struct sockaddr *src_sock, 427 const struct sockaddr *dst_sock, 428 struct rdma_dev_addr *addr, 429 struct dst_entry **pdst) 430 { 431 return -EADDRNOTAVAIL; 432 } 433 #endif 434 435 static bool is_dst_local(const struct dst_entry *dst) 436 { 437 if (dst->ops->family == AF_INET) 438 return !!(dst_rtable(dst)->rt_type & RTN_LOCAL); 439 else if (dst->ops->family == AF_INET6) 440 return !!(dst_rt6_info(dst)->rt6i_flags & RTF_LOCAL); 441 else 442 return false; 443 } 444 445 static int addr_resolve_neigh(const struct dst_entry *dst, 446 const struct sockaddr *dst_in, 447 struct rdma_dev_addr *addr, 448 u32 seq) 449 { 450 if (is_dst_local(dst)) { 451 /* When the destination is local entry, source and destination 452 * are same. Skip the neighbour lookup. 453 */ 454 memcpy(addr->dst_dev_addr, addr->src_dev_addr, MAX_ADDR_LEN); 455 return 0; 456 } 457 458 return fetch_ha(dst, addr, dst_in, seq); 459 } 460 461 static int rdma_set_src_addr_rcu(struct rdma_dev_addr *dev_addr, 462 const struct sockaddr *dst_in, 463 const struct dst_entry *dst) 464 { 465 struct net_device *ndev = READ_ONCE(dst->dev); 466 467 /* A physical device must be the RDMA device to use */ 468 if (is_dst_local(dst)) { 469 int ret; 470 /* 471 * RDMA (IB/RoCE, iWarp) doesn't run on lo interface or 472 * loopback IP address. So if route is resolved to loopback 473 * interface, translate that to a real ndev based on non 474 * loopback IP address. 475 */ 476 ndev = rdma_find_ndev_for_src_ip_rcu(dev_net(ndev), dst_in); 477 if (IS_ERR(ndev)) 478 return -ENODEV; 479 ret = rdma_translate_ip(dst_in, dev_addr); 480 if (ret) 481 return ret; 482 } else { 483 rdma_copy_src_l2_addr(dev_addr, dst->dev); 484 } 485 486 /* 487 * If there's a gateway and type of device not ARPHRD_INFINIBAND, 488 * we're definitely in RoCE v2 (as RoCE v1 isn't routable) set the 489 * network type accordingly. 490 */ 491 if (has_gateway(dst, dst_in->sa_family) && 492 ndev->type != ARPHRD_INFINIBAND) 493 dev_addr->network = dst_in->sa_family == AF_INET ? 494 RDMA_NETWORK_IPV4 : 495 RDMA_NETWORK_IPV6; 496 else 497 dev_addr->network = RDMA_NETWORK_IB; 498 499 return 0; 500 } 501 502 static int set_addr_netns_by_gid_rcu(struct rdma_dev_addr *addr) 503 { 504 struct net_device *ndev; 505 506 ndev = rdma_read_gid_attr_ndev_rcu(addr->sgid_attr); 507 if (IS_ERR(ndev)) 508 return PTR_ERR(ndev); 509 510 /* 511 * Since we are holding the rcu, reading net and ifindex 512 * are safe without any additional reference; because 513 * change_net_namespace() in net/core/dev.c does rcu sync 514 * after it changes the state to IFF_DOWN and before 515 * updating netdev fields {net, ifindex}. 516 */ 517 addr->net = dev_net(ndev); 518 addr->bound_dev_if = ndev->ifindex; 519 return 0; 520 } 521 522 static void rdma_addr_set_net_defaults(struct rdma_dev_addr *addr) 523 { 524 addr->net = &init_net; 525 addr->bound_dev_if = 0; 526 } 527 528 static int addr_resolve(struct sockaddr *src_in, 529 const struct sockaddr *dst_in, 530 struct rdma_dev_addr *addr, 531 bool resolve_neigh, 532 bool resolve_by_gid_attr, 533 u32 seq) 534 { 535 struct dst_entry *dst = NULL; 536 struct rtable *rt = NULL; 537 int ret; 538 539 if (!addr->net) { 540 pr_warn_ratelimited("%s: missing namespace\n", __func__); 541 return -EINVAL; 542 } 543 544 rcu_read_lock(); 545 if (resolve_by_gid_attr) { 546 if (!addr->sgid_attr) { 547 rcu_read_unlock(); 548 pr_warn_ratelimited("%s: missing gid_attr\n", __func__); 549 return -EINVAL; 550 } 551 /* 552 * If the request is for a specific gid attribute of the 553 * rdma_dev_addr, derive net from the netdevice of the 554 * GID attribute. 555 */ 556 ret = set_addr_netns_by_gid_rcu(addr); 557 if (ret) { 558 rcu_read_unlock(); 559 return ret; 560 } 561 } 562 if (src_in->sa_family == AF_INET) { 563 ret = addr4_resolve(src_in, dst_in, addr, &rt); 564 dst = &rt->dst; 565 } else { 566 ret = addr6_resolve(src_in, dst_in, addr, &dst); 567 } 568 if (ret) { 569 rcu_read_unlock(); 570 goto done; 571 } 572 ret = rdma_set_src_addr_rcu(addr, dst_in, dst); 573 rcu_read_unlock(); 574 575 /* 576 * Resolve neighbor destination address if requested and 577 * only if src addr translation didn't fail. 578 */ 579 if (!ret && resolve_neigh) 580 ret = addr_resolve_neigh(dst, dst_in, addr, seq); 581 582 if (src_in->sa_family == AF_INET) 583 ip_rt_put(rt); 584 else 585 dst_release(dst); 586 done: 587 /* 588 * Clear the addr net to go back to its original state, only if it was 589 * derived from GID attribute in this context. 590 */ 591 if (resolve_by_gid_attr) 592 rdma_addr_set_net_defaults(addr); 593 return ret; 594 } 595 596 static void process_one_req(struct work_struct *_work) 597 { 598 struct addr_req *req; 599 struct sockaddr *src_in, *dst_in; 600 601 req = container_of(_work, struct addr_req, work.work); 602 603 if (req->status == -ENODATA) { 604 src_in = (struct sockaddr *)&req->src_addr; 605 dst_in = (struct sockaddr *)&req->dst_addr; 606 req->status = addr_resolve(src_in, dst_in, req->addr, 607 true, req->resolve_by_gid_attr, 608 req->seq); 609 if (req->status && time_after_eq(jiffies, req->timeout)) { 610 req->status = -ETIMEDOUT; 611 } else if (req->status == -ENODATA) { 612 /* requeue the work for retrying again */ 613 spin_lock_bh(&lock); 614 if (!list_empty(&req->list)) 615 set_timeout(req, req->timeout); 616 spin_unlock_bh(&lock); 617 return; 618 } 619 } 620 621 req->callback(req->status, (struct sockaddr *)&req->src_addr, 622 req->addr, req->context); 623 req->callback = NULL; 624 625 spin_lock_bh(&lock); 626 /* 627 * Although the work will normally have been canceled by the workqueue, 628 * it can still be requeued as long as it is on the req_list. 629 */ 630 cancel_delayed_work(&req->work); 631 if (!list_empty(&req->list)) { 632 list_del_init(&req->list); 633 kfree(req); 634 } 635 spin_unlock_bh(&lock); 636 } 637 638 int rdma_resolve_ip(struct sockaddr *src_addr, const struct sockaddr *dst_addr, 639 struct rdma_dev_addr *addr, unsigned long timeout_ms, 640 void (*callback)(int status, struct sockaddr *src_addr, 641 struct rdma_dev_addr *addr, void *context), 642 bool resolve_by_gid_attr, void *context) 643 { 644 struct sockaddr *src_in, *dst_in; 645 struct addr_req *req; 646 int ret = 0; 647 648 req = kzalloc_obj(*req); 649 if (!req) 650 return -ENOMEM; 651 652 src_in = (struct sockaddr *) &req->src_addr; 653 dst_in = (struct sockaddr *) &req->dst_addr; 654 655 if (src_addr) { 656 if (src_addr->sa_family != dst_addr->sa_family) { 657 ret = -EINVAL; 658 goto err; 659 } 660 661 memcpy(src_in, src_addr, rdma_addr_size(src_addr)); 662 } else { 663 src_in->sa_family = dst_addr->sa_family; 664 } 665 666 memcpy(dst_in, dst_addr, rdma_addr_size(dst_addr)); 667 req->addr = addr; 668 req->callback = callback; 669 req->context = context; 670 req->resolve_by_gid_attr = resolve_by_gid_attr; 671 INIT_DELAYED_WORK(&req->work, process_one_req); 672 req->seq = (u32)atomic_inc_return(&ib_nl_addr_request_seq); 673 674 req->status = addr_resolve(src_in, dst_in, addr, true, 675 req->resolve_by_gid_attr, req->seq); 676 switch (req->status) { 677 case 0: 678 req->timeout = jiffies; 679 queue_req(req); 680 break; 681 case -ENODATA: 682 req->timeout = msecs_to_jiffies(timeout_ms) + jiffies; 683 queue_req(req); 684 break; 685 default: 686 ret = req->status; 687 goto err; 688 } 689 return ret; 690 err: 691 kfree(req); 692 return ret; 693 } 694 EXPORT_SYMBOL(rdma_resolve_ip); 695 696 int roce_resolve_route_from_path(struct sa_path_rec *rec, 697 const struct ib_gid_attr *attr) 698 { 699 union { 700 struct sockaddr _sockaddr; 701 struct sockaddr_in _sockaddr_in; 702 struct sockaddr_in6 _sockaddr_in6; 703 } sgid, dgid; 704 struct rdma_dev_addr dev_addr = {}; 705 int ret; 706 707 might_sleep(); 708 709 if (rec->roce.route_resolved) 710 return 0; 711 712 rdma_gid2ip((struct sockaddr *)&sgid, &rec->sgid); 713 rdma_gid2ip((struct sockaddr *)&dgid, &rec->dgid); 714 715 if (sgid._sockaddr.sa_family != dgid._sockaddr.sa_family) 716 return -EINVAL; 717 718 if (!attr || !attr->ndev) 719 return -EINVAL; 720 721 dev_addr.net = &init_net; 722 dev_addr.sgid_attr = attr; 723 724 ret = addr_resolve((struct sockaddr *)&sgid, (struct sockaddr *)&dgid, 725 &dev_addr, false, true, 0); 726 if (ret) 727 return ret; 728 729 if ((dev_addr.network == RDMA_NETWORK_IPV4 || 730 dev_addr.network == RDMA_NETWORK_IPV6) && 731 rec->rec_type != SA_PATH_REC_TYPE_ROCE_V2) 732 return -EINVAL; 733 734 rec->roce.route_resolved = true; 735 return 0; 736 } 737 738 /** 739 * rdma_addr_cancel - Cancel resolve ip request 740 * @addr: Pointer to address structure given previously 741 * during rdma_resolve_ip(). 742 * rdma_addr_cancel() is synchronous function which cancels any pending 743 * request if there is any. 744 */ 745 void rdma_addr_cancel(struct rdma_dev_addr *addr) 746 { 747 struct addr_req *req, *temp_req; 748 struct addr_req *found = NULL; 749 750 spin_lock_bh(&lock); 751 list_for_each_entry_safe(req, temp_req, &req_list, list) { 752 if (req->addr == addr) { 753 /* 754 * Removing from the list means we take ownership of 755 * the req 756 */ 757 list_del_init(&req->list); 758 found = req; 759 break; 760 } 761 } 762 spin_unlock_bh(&lock); 763 764 if (!found) 765 return; 766 767 /* 768 * sync canceling the work after removing it from the req_list 769 * guarentees no work is running and none will be started. 770 */ 771 cancel_delayed_work_sync(&found->work); 772 kfree(found); 773 } 774 EXPORT_SYMBOL(rdma_addr_cancel); 775 776 struct resolve_cb_context { 777 struct completion comp; 778 int status; 779 }; 780 781 static void resolve_cb(int status, struct sockaddr *src_addr, 782 struct rdma_dev_addr *addr, void *context) 783 { 784 ((struct resolve_cb_context *)context)->status = status; 785 complete(&((struct resolve_cb_context *)context)->comp); 786 } 787 788 int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid, 789 const union ib_gid *dgid, 790 u8 *dmac, const struct ib_gid_attr *sgid_attr, 791 int *hoplimit) 792 { 793 struct rdma_dev_addr dev_addr; 794 struct resolve_cb_context ctx; 795 union { 796 struct sockaddr_in _sockaddr_in; 797 struct sockaddr_in6 _sockaddr_in6; 798 } sgid_addr, dgid_addr; 799 int ret; 800 801 rdma_gid2ip((struct sockaddr *)&sgid_addr, sgid); 802 rdma_gid2ip((struct sockaddr *)&dgid_addr, dgid); 803 804 memset(&dev_addr, 0, sizeof(dev_addr)); 805 dev_addr.net = &init_net; 806 dev_addr.sgid_attr = sgid_attr; 807 808 init_completion(&ctx.comp); 809 ret = rdma_resolve_ip((struct sockaddr *)&sgid_addr, 810 (struct sockaddr *)&dgid_addr, &dev_addr, 1000, 811 resolve_cb, true, &ctx); 812 if (ret) 813 return ret; 814 815 wait_for_completion(&ctx.comp); 816 817 ret = ctx.status; 818 if (ret) 819 return ret; 820 821 memcpy(dmac, dev_addr.dst_dev_addr, ETH_ALEN); 822 *hoplimit = dev_addr.hoplimit; 823 return 0; 824 } 825 826 static int netevent_callback(struct notifier_block *self, unsigned long event, 827 void *ctx) 828 { 829 struct addr_req *req; 830 831 if (event == NETEVENT_NEIGH_UPDATE) { 832 struct neighbour *neigh = ctx; 833 834 if (neigh->nud_state & NUD_VALID) { 835 spin_lock_bh(&lock); 836 list_for_each_entry(req, &req_list, list) 837 set_timeout(req, jiffies); 838 spin_unlock_bh(&lock); 839 } 840 } 841 return 0; 842 } 843 844 static struct notifier_block nb = { 845 .notifier_call = netevent_callback 846 }; 847 848 int addr_init(void) 849 { 850 addr_wq = alloc_ordered_workqueue("ib_addr", 0); 851 if (!addr_wq) 852 return -ENOMEM; 853 854 register_netevent_notifier(&nb); 855 856 return 0; 857 } 858 859 void addr_cleanup(void) 860 { 861 unregister_netevent_notifier(&nb); 862 destroy_workqueue(addr_wq); 863 WARN_ON(!list_empty(&req_list)); 864 } 865