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