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/ipv6_stubs.h> 45 #include <net/ip6_route.h> 46 #include <rdma/ib_addr.h> 47 #include <rdma/ib_cache.h> 48 #include <rdma/ib_sa.h> 49 #include <rdma/ib.h> 50 #include <rdma/rdma_netlink.h> 51 #include <net/netlink.h> 52 53 #include "core_priv.h" 54 55 struct addr_req { 56 struct list_head list; 57 struct sockaddr_storage src_addr; 58 struct sockaddr_storage dst_addr; 59 struct rdma_dev_addr *addr; 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 bool resolve_by_gid_attr; /* Consider gid attr in resolve phase */ 66 int status; 67 u32 seq; 68 }; 69 70 static atomic_t ib_nl_addr_request_seq = ATOMIC_INIT(0); 71 72 static DEFINE_SPINLOCK(lock); 73 static LIST_HEAD(req_list); 74 static struct workqueue_struct *addr_wq; 75 76 static const struct nla_policy ib_nl_addr_policy[LS_NLA_TYPE_MAX] = { 77 [LS_NLA_TYPE_DGID] = {.type = NLA_BINARY, 78 .len = sizeof(struct rdma_nla_ls_gid), 79 .validation_type = NLA_VALIDATE_MIN, 80 .min = 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_deprecated(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 spin_lock_bh(&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 spin_unlock_bh(&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 0; 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(&init_net, 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(const 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 int rdma_addr_size_in6(struct sockaddr_in6 *addr) 211 { 212 int ret = rdma_addr_size((struct sockaddr *) addr); 213 214 return ret <= sizeof(*addr) ? ret : 0; 215 } 216 EXPORT_SYMBOL(rdma_addr_size_in6); 217 218 int rdma_addr_size_kss(struct __kernel_sockaddr_storage *addr) 219 { 220 int ret = rdma_addr_size((struct sockaddr *) addr); 221 222 return ret <= sizeof(*addr) ? ret : 0; 223 } 224 EXPORT_SYMBOL(rdma_addr_size_kss); 225 226 /** 227 * rdma_copy_src_l2_addr - Copy netdevice source addresses 228 * @dev_addr: Destination address pointer where to copy the addresses 229 * @dev: Netdevice whose source addresses to copy 230 * 231 * rdma_copy_src_l2_addr() copies source addresses from the specified netdevice. 232 * This includes unicast address, broadcast address, device type and 233 * interface index. 234 */ 235 void rdma_copy_src_l2_addr(struct rdma_dev_addr *dev_addr, 236 const struct net_device *dev) 237 { 238 dev_addr->dev_type = dev->type; 239 memcpy(dev_addr->src_dev_addr, dev->dev_addr, MAX_ADDR_LEN); 240 memcpy(dev_addr->broadcast, dev->broadcast, MAX_ADDR_LEN); 241 dev_addr->bound_dev_if = dev->ifindex; 242 } 243 EXPORT_SYMBOL(rdma_copy_src_l2_addr); 244 245 static struct net_device * 246 rdma_find_ndev_for_src_ip_rcu(struct net *net, const struct sockaddr *src_in) 247 { 248 struct net_device *dev = NULL; 249 int ret = -EADDRNOTAVAIL; 250 251 switch (src_in->sa_family) { 252 case AF_INET: 253 dev = __ip_dev_find(net, 254 ((const struct sockaddr_in *)src_in)->sin_addr.s_addr, 255 false); 256 if (dev) 257 ret = 0; 258 break; 259 #if IS_ENABLED(CONFIG_IPV6) 260 case AF_INET6: 261 for_each_netdev_rcu(net, dev) { 262 if (ipv6_chk_addr(net, 263 &((const struct sockaddr_in6 *)src_in)->sin6_addr, 264 dev, 1)) { 265 ret = 0; 266 break; 267 } 268 } 269 break; 270 #endif 271 } 272 if (!ret && dev && is_vlan_dev(dev)) 273 dev = vlan_dev_real_dev(dev); 274 return ret ? ERR_PTR(ret) : dev; 275 } 276 277 int rdma_translate_ip(const struct sockaddr *addr, 278 struct rdma_dev_addr *dev_addr) 279 { 280 struct net_device *dev; 281 282 if (dev_addr->bound_dev_if) { 283 dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if); 284 if (!dev) 285 return -ENODEV; 286 rdma_copy_src_l2_addr(dev_addr, dev); 287 dev_put(dev); 288 return 0; 289 } 290 291 rcu_read_lock(); 292 dev = rdma_find_ndev_for_src_ip_rcu(dev_addr->net, addr); 293 if (!IS_ERR(dev)) 294 rdma_copy_src_l2_addr(dev_addr, dev); 295 rcu_read_unlock(); 296 return PTR_ERR_OR_ZERO(dev); 297 } 298 EXPORT_SYMBOL(rdma_translate_ip); 299 300 static void set_timeout(struct addr_req *req, unsigned long time) 301 { 302 unsigned long delay; 303 304 delay = time - jiffies; 305 if ((long)delay < 0) 306 delay = 0; 307 308 mod_delayed_work(addr_wq, &req->work, delay); 309 } 310 311 static void queue_req(struct addr_req *req) 312 { 313 spin_lock_bh(&lock); 314 list_add_tail(&req->list, &req_list); 315 set_timeout(req, req->timeout); 316 spin_unlock_bh(&lock); 317 } 318 319 static int ib_nl_fetch_ha(struct rdma_dev_addr *dev_addr, 320 const void *daddr, u32 seq, u16 family) 321 { 322 if (!rdma_nl_chk_listeners(RDMA_NL_GROUP_LS)) 323 return -EADDRNOTAVAIL; 324 325 return ib_nl_ip_send_msg(dev_addr, daddr, seq, family); 326 } 327 328 static int dst_fetch_ha(const struct dst_entry *dst, 329 struct rdma_dev_addr *dev_addr, 330 const void *daddr) 331 { 332 struct neighbour *n; 333 int ret = 0; 334 335 n = dst_neigh_lookup(dst, daddr); 336 if (!n) 337 return -ENODATA; 338 339 if (!(n->nud_state & NUD_VALID)) { 340 neigh_event_send(n, NULL); 341 ret = -ENODATA; 342 } else { 343 neigh_ha_snapshot(dev_addr->dst_dev_addr, n, dst->dev); 344 } 345 346 neigh_release(n); 347 348 return ret; 349 } 350 351 static bool has_gateway(const struct dst_entry *dst, sa_family_t family) 352 { 353 if (family == AF_INET) 354 return dst_rtable(dst)->rt_uses_gateway; 355 356 return dst_rt6_info(dst)->rt6i_flags & RTF_GATEWAY; 357 } 358 359 static int fetch_ha(const struct dst_entry *dst, struct rdma_dev_addr *dev_addr, 360 const struct sockaddr *dst_in, u32 seq) 361 { 362 const struct sockaddr_in *dst_in4 = 363 (const struct sockaddr_in *)dst_in; 364 const struct sockaddr_in6 *dst_in6 = 365 (const struct sockaddr_in6 *)dst_in; 366 const void *daddr = (dst_in->sa_family == AF_INET) ? 367 (const void *)&dst_in4->sin_addr.s_addr : 368 (const void *)&dst_in6->sin6_addr; 369 sa_family_t family = dst_in->sa_family; 370 371 might_sleep(); 372 373 /* If we have a gateway in IB mode then it must be an IB network */ 374 if (has_gateway(dst, family) && dev_addr->network == RDMA_NETWORK_IB) 375 return ib_nl_fetch_ha(dev_addr, daddr, seq, family); 376 else 377 return dst_fetch_ha(dst, dev_addr, daddr); 378 } 379 380 static int addr4_resolve(struct sockaddr *src_sock, 381 const struct sockaddr *dst_sock, 382 struct rdma_dev_addr *addr, 383 struct rtable **prt) 384 { 385 struct sockaddr_in *src_in = (struct sockaddr_in *)src_sock; 386 const struct sockaddr_in *dst_in = 387 (const struct sockaddr_in *)dst_sock; 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_addr.s_addr = fl4.saddr; 405 406 addr->hoplimit = ip4_dst_hoplimit(&rt->dst); 407 408 *prt = rt; 409 return 0; 410 } 411 412 #if IS_ENABLED(CONFIG_IPV6) 413 static int addr6_resolve(struct sockaddr *src_sock, 414 const struct sockaddr *dst_sock, 415 struct rdma_dev_addr *addr, 416 struct dst_entry **pdst) 417 { 418 struct sockaddr_in6 *src_in = (struct sockaddr_in6 *)src_sock; 419 const struct sockaddr_in6 *dst_in = 420 (const struct sockaddr_in6 *)dst_sock; 421 struct flowi6 fl6; 422 struct dst_entry *dst; 423 424 memset(&fl6, 0, sizeof fl6); 425 fl6.daddr = dst_in->sin6_addr; 426 fl6.saddr = src_in->sin6_addr; 427 fl6.flowi6_oif = addr->bound_dev_if; 428 429 dst = ipv6_stub->ipv6_dst_lookup_flow(addr->net, NULL, &fl6, NULL); 430 if (IS_ERR(dst)) 431 return PTR_ERR(dst); 432 433 if (ipv6_addr_any(&src_in->sin6_addr)) 434 src_in->sin6_addr = fl6.saddr; 435 436 addr->hoplimit = ip6_dst_hoplimit(dst); 437 438 *pdst = dst; 439 return 0; 440 } 441 #else 442 static int addr6_resolve(struct sockaddr *src_sock, 443 const struct sockaddr *dst_sock, 444 struct rdma_dev_addr *addr, 445 struct dst_entry **pdst) 446 { 447 return -EADDRNOTAVAIL; 448 } 449 #endif 450 451 static int addr_resolve_neigh(const struct dst_entry *dst, 452 const struct sockaddr *dst_in, 453 struct rdma_dev_addr *addr, 454 unsigned int ndev_flags, 455 u32 seq) 456 { 457 int ret = 0; 458 459 if (ndev_flags & IFF_LOOPBACK) { 460 memcpy(addr->dst_dev_addr, addr->src_dev_addr, MAX_ADDR_LEN); 461 } else { 462 if (!(ndev_flags & IFF_NOARP)) { 463 /* If the device doesn't do ARP internally */ 464 ret = fetch_ha(dst, addr, dst_in, seq); 465 } 466 } 467 return ret; 468 } 469 470 static int copy_src_l2_addr(struct rdma_dev_addr *dev_addr, 471 const struct sockaddr *dst_in, 472 const struct dst_entry *dst, 473 const struct net_device *ndev) 474 { 475 int ret = 0; 476 477 if (dst->dev->flags & IFF_LOOPBACK) 478 ret = rdma_translate_ip(dst_in, dev_addr); 479 else 480 rdma_copy_src_l2_addr(dev_addr, dst->dev); 481 482 /* 483 * If there's a gateway and type of device not ARPHRD_INFINIBAND, 484 * we're definitely in RoCE v2 (as RoCE v1 isn't routable) set the 485 * network type accordingly. 486 */ 487 if (has_gateway(dst, dst_in->sa_family) && 488 ndev->type != ARPHRD_INFINIBAND) 489 dev_addr->network = dst_in->sa_family == AF_INET ? 490 RDMA_NETWORK_IPV4 : 491 RDMA_NETWORK_IPV6; 492 else 493 dev_addr->network = RDMA_NETWORK_IB; 494 495 return ret; 496 } 497 498 static int rdma_set_src_addr_rcu(struct rdma_dev_addr *dev_addr, 499 unsigned int *ndev_flags, 500 const struct sockaddr *dst_in, 501 const struct dst_entry *dst) 502 { 503 struct net_device *ndev = READ_ONCE(dst->dev); 504 505 *ndev_flags = ndev->flags; 506 /* A physical device must be the RDMA device to use */ 507 if (ndev->flags & IFF_LOOPBACK) { 508 /* 509 * RDMA (IB/RoCE, iWarp) doesn't run on lo interface or 510 * loopback IP address. So if route is resolved to loopback 511 * interface, translate that to a real ndev based on non 512 * loopback IP address. 513 */ 514 ndev = rdma_find_ndev_for_src_ip_rcu(dev_net(ndev), dst_in); 515 if (IS_ERR(ndev)) 516 return -ENODEV; 517 } 518 519 return copy_src_l2_addr(dev_addr, dst_in, dst, ndev); 520 } 521 522 static int set_addr_netns_by_gid_rcu(struct rdma_dev_addr *addr) 523 { 524 struct net_device *ndev; 525 526 ndev = rdma_read_gid_attr_ndev_rcu(addr->sgid_attr); 527 if (IS_ERR(ndev)) 528 return PTR_ERR(ndev); 529 530 /* 531 * Since we are holding the rcu, reading net and ifindex 532 * are safe without any additional reference; because 533 * change_net_namespace() in net/core/dev.c does rcu sync 534 * after it changes the state to IFF_DOWN and before 535 * updating netdev fields {net, ifindex}. 536 */ 537 addr->net = dev_net(ndev); 538 addr->bound_dev_if = ndev->ifindex; 539 return 0; 540 } 541 542 static void rdma_addr_set_net_defaults(struct rdma_dev_addr *addr) 543 { 544 addr->net = &init_net; 545 addr->bound_dev_if = 0; 546 } 547 548 static int addr_resolve(struct sockaddr *src_in, 549 const struct sockaddr *dst_in, 550 struct rdma_dev_addr *addr, 551 bool resolve_neigh, 552 bool resolve_by_gid_attr, 553 u32 seq) 554 { 555 struct dst_entry *dst = NULL; 556 unsigned int ndev_flags = 0; 557 struct rtable *rt = NULL; 558 int ret; 559 560 if (!addr->net) { 561 pr_warn_ratelimited("%s: missing namespace\n", __func__); 562 return -EINVAL; 563 } 564 565 rcu_read_lock(); 566 if (resolve_by_gid_attr) { 567 if (!addr->sgid_attr) { 568 rcu_read_unlock(); 569 pr_warn_ratelimited("%s: missing gid_attr\n", __func__); 570 return -EINVAL; 571 } 572 /* 573 * If the request is for a specific gid attribute of the 574 * rdma_dev_addr, derive net from the netdevice of the 575 * GID attribute. 576 */ 577 ret = set_addr_netns_by_gid_rcu(addr); 578 if (ret) { 579 rcu_read_unlock(); 580 return ret; 581 } 582 } 583 if (src_in->sa_family == AF_INET) { 584 ret = addr4_resolve(src_in, dst_in, addr, &rt); 585 dst = &rt->dst; 586 } else { 587 ret = addr6_resolve(src_in, dst_in, addr, &dst); 588 } 589 if (ret) { 590 rcu_read_unlock(); 591 goto done; 592 } 593 ret = rdma_set_src_addr_rcu(addr, &ndev_flags, dst_in, dst); 594 rcu_read_unlock(); 595 596 /* 597 * Resolve neighbor destination address if requested and 598 * only if src addr translation didn't fail. 599 */ 600 if (!ret && resolve_neigh) 601 ret = addr_resolve_neigh(dst, dst_in, addr, ndev_flags, seq); 602 603 if (src_in->sa_family == AF_INET) 604 ip_rt_put(rt); 605 else 606 dst_release(dst); 607 done: 608 /* 609 * Clear the addr net to go back to its original state, only if it was 610 * derived from GID attribute in this context. 611 */ 612 if (resolve_by_gid_attr) 613 rdma_addr_set_net_defaults(addr); 614 return ret; 615 } 616 617 static void process_one_req(struct work_struct *_work) 618 { 619 struct addr_req *req; 620 struct sockaddr *src_in, *dst_in; 621 622 req = container_of(_work, struct addr_req, work.work); 623 624 if (req->status == -ENODATA) { 625 src_in = (struct sockaddr *)&req->src_addr; 626 dst_in = (struct sockaddr *)&req->dst_addr; 627 req->status = addr_resolve(src_in, dst_in, req->addr, 628 true, req->resolve_by_gid_attr, 629 req->seq); 630 if (req->status && time_after_eq(jiffies, req->timeout)) { 631 req->status = -ETIMEDOUT; 632 } else if (req->status == -ENODATA) { 633 /* requeue the work for retrying again */ 634 spin_lock_bh(&lock); 635 if (!list_empty(&req->list)) 636 set_timeout(req, req->timeout); 637 spin_unlock_bh(&lock); 638 return; 639 } 640 } 641 642 req->callback(req->status, (struct sockaddr *)&req->src_addr, 643 req->addr, req->context); 644 req->callback = NULL; 645 646 spin_lock_bh(&lock); 647 /* 648 * Although the work will normally have been canceled by the workqueue, 649 * it can still be requeued as long as it is on the req_list. 650 */ 651 cancel_delayed_work(&req->work); 652 if (!list_empty(&req->list)) { 653 list_del_init(&req->list); 654 kfree(req); 655 } 656 spin_unlock_bh(&lock); 657 } 658 659 int rdma_resolve_ip(struct sockaddr *src_addr, const struct sockaddr *dst_addr, 660 struct rdma_dev_addr *addr, unsigned long timeout_ms, 661 void (*callback)(int status, struct sockaddr *src_addr, 662 struct rdma_dev_addr *addr, void *context), 663 bool resolve_by_gid_attr, void *context) 664 { 665 struct sockaddr *src_in, *dst_in; 666 struct addr_req *req; 667 int ret = 0; 668 669 req = kzalloc(sizeof *req, GFP_KERNEL); 670 if (!req) 671 return -ENOMEM; 672 673 src_in = (struct sockaddr *) &req->src_addr; 674 dst_in = (struct sockaddr *) &req->dst_addr; 675 676 if (src_addr) { 677 if (src_addr->sa_family != dst_addr->sa_family) { 678 ret = -EINVAL; 679 goto err; 680 } 681 682 memcpy(src_in, src_addr, rdma_addr_size(src_addr)); 683 } else { 684 src_in->sa_family = dst_addr->sa_family; 685 } 686 687 memcpy(dst_in, dst_addr, rdma_addr_size(dst_addr)); 688 req->addr = addr; 689 req->callback = callback; 690 req->context = context; 691 req->resolve_by_gid_attr = resolve_by_gid_attr; 692 INIT_DELAYED_WORK(&req->work, process_one_req); 693 req->seq = (u32)atomic_inc_return(&ib_nl_addr_request_seq); 694 695 req->status = addr_resolve(src_in, dst_in, addr, true, 696 req->resolve_by_gid_attr, req->seq); 697 switch (req->status) { 698 case 0: 699 req->timeout = jiffies; 700 queue_req(req); 701 break; 702 case -ENODATA: 703 req->timeout = msecs_to_jiffies(timeout_ms) + jiffies; 704 queue_req(req); 705 break; 706 default: 707 ret = req->status; 708 goto err; 709 } 710 return ret; 711 err: 712 kfree(req); 713 return ret; 714 } 715 EXPORT_SYMBOL(rdma_resolve_ip); 716 717 int roce_resolve_route_from_path(struct sa_path_rec *rec, 718 const struct ib_gid_attr *attr) 719 { 720 union { 721 struct sockaddr _sockaddr; 722 struct sockaddr_in _sockaddr_in; 723 struct sockaddr_in6 _sockaddr_in6; 724 } sgid, dgid; 725 struct rdma_dev_addr dev_addr = {}; 726 int ret; 727 728 might_sleep(); 729 730 if (rec->roce.route_resolved) 731 return 0; 732 733 rdma_gid2ip((struct sockaddr *)&sgid, &rec->sgid); 734 rdma_gid2ip((struct sockaddr *)&dgid, &rec->dgid); 735 736 if (sgid._sockaddr.sa_family != dgid._sockaddr.sa_family) 737 return -EINVAL; 738 739 if (!attr || !attr->ndev) 740 return -EINVAL; 741 742 dev_addr.net = &init_net; 743 dev_addr.sgid_attr = attr; 744 745 ret = addr_resolve((struct sockaddr *)&sgid, (struct sockaddr *)&dgid, 746 &dev_addr, false, true, 0); 747 if (ret) 748 return ret; 749 750 if ((dev_addr.network == RDMA_NETWORK_IPV4 || 751 dev_addr.network == RDMA_NETWORK_IPV6) && 752 rec->rec_type != SA_PATH_REC_TYPE_ROCE_V2) 753 return -EINVAL; 754 755 rec->roce.route_resolved = true; 756 return 0; 757 } 758 759 /** 760 * rdma_addr_cancel - Cancel resolve ip request 761 * @addr: Pointer to address structure given previously 762 * during rdma_resolve_ip(). 763 * rdma_addr_cancel() is synchronous function which cancels any pending 764 * request if there is any. 765 */ 766 void rdma_addr_cancel(struct rdma_dev_addr *addr) 767 { 768 struct addr_req *req, *temp_req; 769 struct addr_req *found = NULL; 770 771 spin_lock_bh(&lock); 772 list_for_each_entry_safe(req, temp_req, &req_list, list) { 773 if (req->addr == addr) { 774 /* 775 * Removing from the list means we take ownership of 776 * the req 777 */ 778 list_del_init(&req->list); 779 found = req; 780 break; 781 } 782 } 783 spin_unlock_bh(&lock); 784 785 if (!found) 786 return; 787 788 /* 789 * sync canceling the work after removing it from the req_list 790 * guarentees no work is running and none will be started. 791 */ 792 cancel_delayed_work_sync(&found->work); 793 kfree(found); 794 } 795 EXPORT_SYMBOL(rdma_addr_cancel); 796 797 struct resolve_cb_context { 798 struct completion comp; 799 int status; 800 }; 801 802 static void resolve_cb(int status, struct sockaddr *src_addr, 803 struct rdma_dev_addr *addr, void *context) 804 { 805 ((struct resolve_cb_context *)context)->status = status; 806 complete(&((struct resolve_cb_context *)context)->comp); 807 } 808 809 int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid, 810 const union ib_gid *dgid, 811 u8 *dmac, const struct ib_gid_attr *sgid_attr, 812 int *hoplimit) 813 { 814 struct rdma_dev_addr dev_addr; 815 struct resolve_cb_context ctx; 816 union { 817 struct sockaddr_in _sockaddr_in; 818 struct sockaddr_in6 _sockaddr_in6; 819 } sgid_addr, dgid_addr; 820 int ret; 821 822 rdma_gid2ip((struct sockaddr *)&sgid_addr, sgid); 823 rdma_gid2ip((struct sockaddr *)&dgid_addr, dgid); 824 825 memset(&dev_addr, 0, sizeof(dev_addr)); 826 dev_addr.net = &init_net; 827 dev_addr.sgid_attr = sgid_attr; 828 829 init_completion(&ctx.comp); 830 ret = rdma_resolve_ip((struct sockaddr *)&sgid_addr, 831 (struct sockaddr *)&dgid_addr, &dev_addr, 1000, 832 resolve_cb, true, &ctx); 833 if (ret) 834 return ret; 835 836 wait_for_completion(&ctx.comp); 837 838 ret = ctx.status; 839 if (ret) 840 return ret; 841 842 memcpy(dmac, dev_addr.dst_dev_addr, ETH_ALEN); 843 *hoplimit = dev_addr.hoplimit; 844 return 0; 845 } 846 847 static int netevent_callback(struct notifier_block *self, unsigned long event, 848 void *ctx) 849 { 850 struct addr_req *req; 851 852 if (event == NETEVENT_NEIGH_UPDATE) { 853 struct neighbour *neigh = ctx; 854 855 if (neigh->nud_state & NUD_VALID) { 856 spin_lock_bh(&lock); 857 list_for_each_entry(req, &req_list, list) 858 set_timeout(req, jiffies); 859 spin_unlock_bh(&lock); 860 } 861 } 862 return 0; 863 } 864 865 static struct notifier_block nb = { 866 .notifier_call = netevent_callback 867 }; 868 869 int addr_init(void) 870 { 871 addr_wq = alloc_ordered_workqueue("ib_addr", 0); 872 if (!addr_wq) 873 return -ENOMEM; 874 875 register_netevent_notifier(&nb); 876 877 return 0; 878 } 879 880 void addr_cleanup(void) 881 { 882 unregister_netevent_notifier(&nb); 883 destroy_workqueue(addr_wq); 884 WARN_ON(!list_empty(&req_list)); 885 } 886