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-2006 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/completion.h> 37 #include <linux/in.h> 38 #include <linux/in6.h> 39 #include <linux/mutex.h> 40 #include <linux/random.h> 41 #include <linux/igmp.h> 42 #include <linux/idr.h> 43 #include <linux/inetdevice.h> 44 #include <linux/slab.h> 45 #include <linux/module.h> 46 #include <net/route.h> 47 48 #include <net/net_namespace.h> 49 #include <net/netns/generic.h> 50 #include <net/tcp.h> 51 #include <net/ipv6.h> 52 #include <net/ip_fib.h> 53 #include <net/ip6_route.h> 54 55 #include <rdma/rdma_cm.h> 56 #include <rdma/rdma_cm_ib.h> 57 #include <rdma/rdma_netlink.h> 58 #include <rdma/ib.h> 59 #include <rdma/ib_cache.h> 60 #include <rdma/ib_cm.h> 61 #include <rdma/ib_sa.h> 62 #include <rdma/iw_cm.h> 63 64 #include "core_priv.h" 65 66 MODULE_AUTHOR("Sean Hefty"); 67 MODULE_DESCRIPTION("Generic RDMA CM Agent"); 68 MODULE_LICENSE("Dual BSD/GPL"); 69 70 #define CMA_CM_RESPONSE_TIMEOUT 20 71 #define CMA_MAX_CM_RETRIES 15 72 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24) 73 #define CMA_IBOE_PACKET_LIFETIME 18 74 75 static const char * const cma_events[] = { 76 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved", 77 [RDMA_CM_EVENT_ADDR_ERROR] = "address error", 78 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ", 79 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error", 80 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request", 81 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response", 82 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error", 83 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable", 84 [RDMA_CM_EVENT_REJECTED] = "rejected", 85 [RDMA_CM_EVENT_ESTABLISHED] = "established", 86 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected", 87 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal", 88 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join", 89 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error", 90 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change", 91 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit", 92 }; 93 94 const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event) 95 { 96 size_t index = event; 97 98 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ? 99 cma_events[index] : "unrecognized event"; 100 } 101 EXPORT_SYMBOL(rdma_event_msg); 102 103 static void cma_add_one(struct ib_device *device); 104 static void cma_remove_one(struct ib_device *device, void *client_data); 105 106 static struct ib_client cma_client = { 107 .name = "cma", 108 .add = cma_add_one, 109 .remove = cma_remove_one 110 }; 111 112 static struct ib_sa_client sa_client; 113 static struct rdma_addr_client addr_client; 114 static LIST_HEAD(dev_list); 115 static LIST_HEAD(listen_any_list); 116 static DEFINE_MUTEX(lock); 117 static struct workqueue_struct *cma_wq; 118 static int cma_pernet_id; 119 120 struct cma_pernet { 121 struct idr tcp_ps; 122 struct idr udp_ps; 123 struct idr ipoib_ps; 124 struct idr ib_ps; 125 }; 126 127 static struct cma_pernet *cma_pernet(struct net *net) 128 { 129 return net_generic(net, cma_pernet_id); 130 } 131 132 static struct idr *cma_pernet_idr(struct net *net, enum rdma_port_space ps) 133 { 134 struct cma_pernet *pernet = cma_pernet(net); 135 136 switch (ps) { 137 case RDMA_PS_TCP: 138 return &pernet->tcp_ps; 139 case RDMA_PS_UDP: 140 return &pernet->udp_ps; 141 case RDMA_PS_IPOIB: 142 return &pernet->ipoib_ps; 143 case RDMA_PS_IB: 144 return &pernet->ib_ps; 145 default: 146 return NULL; 147 } 148 } 149 150 struct cma_device { 151 struct list_head list; 152 struct ib_device *device; 153 struct completion comp; 154 atomic_t refcount; 155 struct list_head id_list; 156 enum ib_gid_type *default_gid_type; 157 }; 158 159 struct rdma_bind_list { 160 enum rdma_port_space ps; 161 struct hlist_head owners; 162 unsigned short port; 163 }; 164 165 static int cma_ps_alloc(struct net *net, enum rdma_port_space ps, 166 struct rdma_bind_list *bind_list, int snum) 167 { 168 struct idr *idr = cma_pernet_idr(net, ps); 169 170 return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL); 171 } 172 173 static struct rdma_bind_list *cma_ps_find(struct net *net, 174 enum rdma_port_space ps, int snum) 175 { 176 struct idr *idr = cma_pernet_idr(net, ps); 177 178 return idr_find(idr, snum); 179 } 180 181 static void cma_ps_remove(struct net *net, enum rdma_port_space ps, int snum) 182 { 183 struct idr *idr = cma_pernet_idr(net, ps); 184 185 idr_remove(idr, snum); 186 } 187 188 enum { 189 CMA_OPTION_AFONLY, 190 }; 191 192 void cma_ref_dev(struct cma_device *cma_dev) 193 { 194 atomic_inc(&cma_dev->refcount); 195 } 196 197 struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter, 198 void *cookie) 199 { 200 struct cma_device *cma_dev; 201 struct cma_device *found_cma_dev = NULL; 202 203 mutex_lock(&lock); 204 205 list_for_each_entry(cma_dev, &dev_list, list) 206 if (filter(cma_dev->device, cookie)) { 207 found_cma_dev = cma_dev; 208 break; 209 } 210 211 if (found_cma_dev) 212 cma_ref_dev(found_cma_dev); 213 mutex_unlock(&lock); 214 return found_cma_dev; 215 } 216 217 int cma_get_default_gid_type(struct cma_device *cma_dev, 218 unsigned int port) 219 { 220 if (port < rdma_start_port(cma_dev->device) || 221 port > rdma_end_port(cma_dev->device)) 222 return -EINVAL; 223 224 return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)]; 225 } 226 227 int cma_set_default_gid_type(struct cma_device *cma_dev, 228 unsigned int port, 229 enum ib_gid_type default_gid_type) 230 { 231 unsigned long supported_gids; 232 233 if (port < rdma_start_port(cma_dev->device) || 234 port > rdma_end_port(cma_dev->device)) 235 return -EINVAL; 236 237 supported_gids = roce_gid_type_mask_support(cma_dev->device, port); 238 239 if (!(supported_gids & 1 << default_gid_type)) 240 return -EINVAL; 241 242 cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] = 243 default_gid_type; 244 245 return 0; 246 } 247 248 struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev) 249 { 250 return cma_dev->device; 251 } 252 253 /* 254 * Device removal can occur at anytime, so we need extra handling to 255 * serialize notifying the user of device removal with other callbacks. 256 * We do this by disabling removal notification while a callback is in process, 257 * and reporting it after the callback completes. 258 */ 259 struct rdma_id_private { 260 struct rdma_cm_id id; 261 262 struct rdma_bind_list *bind_list; 263 struct hlist_node node; 264 struct list_head list; /* listen_any_list or cma_device.list */ 265 struct list_head listen_list; /* per device listens */ 266 struct cma_device *cma_dev; 267 struct list_head mc_list; 268 269 int internal_id; 270 enum rdma_cm_state state; 271 spinlock_t lock; 272 struct mutex qp_mutex; 273 274 struct completion comp; 275 atomic_t refcount; 276 struct mutex handler_mutex; 277 278 int backlog; 279 int timeout_ms; 280 struct ib_sa_query *query; 281 int query_id; 282 union { 283 struct ib_cm_id *ib; 284 struct iw_cm_id *iw; 285 } cm_id; 286 287 u32 seq_num; 288 u32 qkey; 289 u32 qp_num; 290 pid_t owner; 291 u32 options; 292 u8 srq; 293 u8 tos; 294 u8 reuseaddr; 295 u8 afonly; 296 enum ib_gid_type gid_type; 297 }; 298 299 struct cma_multicast { 300 struct rdma_id_private *id_priv; 301 union { 302 struct ib_sa_multicast *ib; 303 } multicast; 304 struct list_head list; 305 void *context; 306 struct sockaddr_storage addr; 307 struct kref mcref; 308 bool igmp_joined; 309 }; 310 311 struct cma_work { 312 struct work_struct work; 313 struct rdma_id_private *id; 314 enum rdma_cm_state old_state; 315 enum rdma_cm_state new_state; 316 struct rdma_cm_event event; 317 }; 318 319 struct cma_ndev_work { 320 struct work_struct work; 321 struct rdma_id_private *id; 322 struct rdma_cm_event event; 323 }; 324 325 struct iboe_mcast_work { 326 struct work_struct work; 327 struct rdma_id_private *id; 328 struct cma_multicast *mc; 329 }; 330 331 union cma_ip_addr { 332 struct in6_addr ip6; 333 struct { 334 __be32 pad[3]; 335 __be32 addr; 336 } ip4; 337 }; 338 339 struct cma_hdr { 340 u8 cma_version; 341 u8 ip_version; /* IP version: 7:4 */ 342 __be16 port; 343 union cma_ip_addr src_addr; 344 union cma_ip_addr dst_addr; 345 }; 346 347 #define CMA_VERSION 0x00 348 349 struct cma_req_info { 350 struct ib_device *device; 351 int port; 352 union ib_gid local_gid; 353 __be64 service_id; 354 u16 pkey; 355 bool has_gid:1; 356 }; 357 358 static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp) 359 { 360 unsigned long flags; 361 int ret; 362 363 spin_lock_irqsave(&id_priv->lock, flags); 364 ret = (id_priv->state == comp); 365 spin_unlock_irqrestore(&id_priv->lock, flags); 366 return ret; 367 } 368 369 static int cma_comp_exch(struct rdma_id_private *id_priv, 370 enum rdma_cm_state comp, enum rdma_cm_state exch) 371 { 372 unsigned long flags; 373 int ret; 374 375 spin_lock_irqsave(&id_priv->lock, flags); 376 if ((ret = (id_priv->state == comp))) 377 id_priv->state = exch; 378 spin_unlock_irqrestore(&id_priv->lock, flags); 379 return ret; 380 } 381 382 static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv, 383 enum rdma_cm_state exch) 384 { 385 unsigned long flags; 386 enum rdma_cm_state old; 387 388 spin_lock_irqsave(&id_priv->lock, flags); 389 old = id_priv->state; 390 id_priv->state = exch; 391 spin_unlock_irqrestore(&id_priv->lock, flags); 392 return old; 393 } 394 395 static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr) 396 { 397 return hdr->ip_version >> 4; 398 } 399 400 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver) 401 { 402 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF); 403 } 404 405 static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join) 406 { 407 struct in_device *in_dev = NULL; 408 409 if (ndev) { 410 rtnl_lock(); 411 in_dev = __in_dev_get_rtnl(ndev); 412 if (in_dev) { 413 if (join) 414 ip_mc_inc_group(in_dev, 415 *(__be32 *)(mgid->raw + 12)); 416 else 417 ip_mc_dec_group(in_dev, 418 *(__be32 *)(mgid->raw + 12)); 419 } 420 rtnl_unlock(); 421 } 422 return (in_dev) ? 0 : -ENODEV; 423 } 424 425 static void _cma_attach_to_dev(struct rdma_id_private *id_priv, 426 struct cma_device *cma_dev) 427 { 428 cma_ref_dev(cma_dev); 429 id_priv->cma_dev = cma_dev; 430 id_priv->gid_type = 0; 431 id_priv->id.device = cma_dev->device; 432 id_priv->id.route.addr.dev_addr.transport = 433 rdma_node_get_transport(cma_dev->device->node_type); 434 list_add_tail(&id_priv->list, &cma_dev->id_list); 435 } 436 437 static void cma_attach_to_dev(struct rdma_id_private *id_priv, 438 struct cma_device *cma_dev) 439 { 440 _cma_attach_to_dev(id_priv, cma_dev); 441 id_priv->gid_type = 442 cma_dev->default_gid_type[id_priv->id.port_num - 443 rdma_start_port(cma_dev->device)]; 444 } 445 446 void cma_deref_dev(struct cma_device *cma_dev) 447 { 448 if (atomic_dec_and_test(&cma_dev->refcount)) 449 complete(&cma_dev->comp); 450 } 451 452 static inline void release_mc(struct kref *kref) 453 { 454 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref); 455 456 kfree(mc->multicast.ib); 457 kfree(mc); 458 } 459 460 static void cma_release_dev(struct rdma_id_private *id_priv) 461 { 462 mutex_lock(&lock); 463 list_del(&id_priv->list); 464 cma_deref_dev(id_priv->cma_dev); 465 id_priv->cma_dev = NULL; 466 mutex_unlock(&lock); 467 } 468 469 static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv) 470 { 471 return (struct sockaddr *) &id_priv->id.route.addr.src_addr; 472 } 473 474 static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv) 475 { 476 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr; 477 } 478 479 static inline unsigned short cma_family(struct rdma_id_private *id_priv) 480 { 481 return id_priv->id.route.addr.src_addr.ss_family; 482 } 483 484 static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey) 485 { 486 struct ib_sa_mcmember_rec rec; 487 int ret = 0; 488 489 if (id_priv->qkey) { 490 if (qkey && id_priv->qkey != qkey) 491 return -EINVAL; 492 return 0; 493 } 494 495 if (qkey) { 496 id_priv->qkey = qkey; 497 return 0; 498 } 499 500 switch (id_priv->id.ps) { 501 case RDMA_PS_UDP: 502 case RDMA_PS_IB: 503 id_priv->qkey = RDMA_UDP_QKEY; 504 break; 505 case RDMA_PS_IPOIB: 506 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid); 507 ret = ib_sa_get_mcmember_rec(id_priv->id.device, 508 id_priv->id.port_num, &rec.mgid, 509 &rec); 510 if (!ret) 511 id_priv->qkey = be32_to_cpu(rec.qkey); 512 break; 513 default: 514 break; 515 } 516 return ret; 517 } 518 519 static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr) 520 { 521 dev_addr->dev_type = ARPHRD_INFINIBAND; 522 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr); 523 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey)); 524 } 525 526 static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr) 527 { 528 int ret; 529 530 if (addr->sa_family != AF_IB) { 531 ret = rdma_translate_ip(addr, dev_addr, NULL); 532 } else { 533 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr); 534 ret = 0; 535 } 536 537 return ret; 538 } 539 540 static inline int cma_validate_port(struct ib_device *device, u8 port, 541 enum ib_gid_type gid_type, 542 union ib_gid *gid, int dev_type, 543 int bound_if_index) 544 { 545 int ret = -ENODEV; 546 struct net_device *ndev = NULL; 547 548 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port)) 549 return ret; 550 551 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port)) 552 return ret; 553 554 if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) { 555 ndev = dev_get_by_index(&init_net, bound_if_index); 556 if (ndev && ndev->flags & IFF_LOOPBACK) { 557 pr_info("detected loopback device\n"); 558 dev_put(ndev); 559 560 if (!device->get_netdev) 561 return -EOPNOTSUPP; 562 563 ndev = device->get_netdev(device, port); 564 if (!ndev) 565 return -ENODEV; 566 } 567 } else { 568 gid_type = IB_GID_TYPE_IB; 569 } 570 571 ret = ib_find_cached_gid_by_port(device, gid, gid_type, port, 572 ndev, NULL); 573 574 if (ndev) 575 dev_put(ndev); 576 577 return ret; 578 } 579 580 static int cma_acquire_dev(struct rdma_id_private *id_priv, 581 struct rdma_id_private *listen_id_priv) 582 { 583 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 584 struct cma_device *cma_dev; 585 union ib_gid gid, iboe_gid, *gidp; 586 int ret = -ENODEV; 587 u8 port; 588 589 if (dev_addr->dev_type != ARPHRD_INFINIBAND && 590 id_priv->id.ps == RDMA_PS_IPOIB) 591 return -EINVAL; 592 593 mutex_lock(&lock); 594 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr, 595 &iboe_gid); 596 597 memcpy(&gid, dev_addr->src_dev_addr + 598 rdma_addr_gid_offset(dev_addr), sizeof gid); 599 600 if (listen_id_priv) { 601 cma_dev = listen_id_priv->cma_dev; 602 port = listen_id_priv->id.port_num; 603 gidp = rdma_protocol_roce(cma_dev->device, port) ? 604 &iboe_gid : &gid; 605 606 ret = cma_validate_port(cma_dev->device, port, 607 rdma_protocol_ib(cma_dev->device, port) ? 608 IB_GID_TYPE_IB : 609 listen_id_priv->gid_type, gidp, 610 dev_addr->dev_type, 611 dev_addr->bound_dev_if); 612 if (!ret) { 613 id_priv->id.port_num = port; 614 goto out; 615 } 616 } 617 618 list_for_each_entry(cma_dev, &dev_list, list) { 619 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) { 620 if (listen_id_priv && 621 listen_id_priv->cma_dev == cma_dev && 622 listen_id_priv->id.port_num == port) 623 continue; 624 625 gidp = rdma_protocol_roce(cma_dev->device, port) ? 626 &iboe_gid : &gid; 627 628 ret = cma_validate_port(cma_dev->device, port, 629 rdma_protocol_ib(cma_dev->device, port) ? 630 IB_GID_TYPE_IB : 631 cma_dev->default_gid_type[port - 1], 632 gidp, dev_addr->dev_type, 633 dev_addr->bound_dev_if); 634 if (!ret) { 635 id_priv->id.port_num = port; 636 goto out; 637 } 638 } 639 } 640 641 out: 642 if (!ret) 643 cma_attach_to_dev(id_priv, cma_dev); 644 645 mutex_unlock(&lock); 646 return ret; 647 } 648 649 /* 650 * Select the source IB device and address to reach the destination IB address. 651 */ 652 static int cma_resolve_ib_dev(struct rdma_id_private *id_priv) 653 { 654 struct cma_device *cma_dev, *cur_dev; 655 struct sockaddr_ib *addr; 656 union ib_gid gid, sgid, *dgid; 657 u16 pkey, index; 658 u8 p; 659 int i; 660 661 cma_dev = NULL; 662 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv); 663 dgid = (union ib_gid *) &addr->sib_addr; 664 pkey = ntohs(addr->sib_pkey); 665 666 list_for_each_entry(cur_dev, &dev_list, list) { 667 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) { 668 if (!rdma_cap_af_ib(cur_dev->device, p)) 669 continue; 670 671 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index)) 672 continue; 673 674 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i, 675 &gid, NULL); 676 i++) { 677 if (!memcmp(&gid, dgid, sizeof(gid))) { 678 cma_dev = cur_dev; 679 sgid = gid; 680 id_priv->id.port_num = p; 681 goto found; 682 } 683 684 if (!cma_dev && (gid.global.subnet_prefix == 685 dgid->global.subnet_prefix)) { 686 cma_dev = cur_dev; 687 sgid = gid; 688 id_priv->id.port_num = p; 689 } 690 } 691 } 692 } 693 694 if (!cma_dev) 695 return -ENODEV; 696 697 found: 698 cma_attach_to_dev(id_priv, cma_dev); 699 addr = (struct sockaddr_ib *) cma_src_addr(id_priv); 700 memcpy(&addr->sib_addr, &sgid, sizeof sgid); 701 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr); 702 return 0; 703 } 704 705 static void cma_deref_id(struct rdma_id_private *id_priv) 706 { 707 if (atomic_dec_and_test(&id_priv->refcount)) 708 complete(&id_priv->comp); 709 } 710 711 struct rdma_cm_id *rdma_create_id(struct net *net, 712 rdma_cm_event_handler event_handler, 713 void *context, enum rdma_port_space ps, 714 enum ib_qp_type qp_type) 715 { 716 struct rdma_id_private *id_priv; 717 718 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL); 719 if (!id_priv) 720 return ERR_PTR(-ENOMEM); 721 722 id_priv->owner = task_pid_nr(current); 723 id_priv->state = RDMA_CM_IDLE; 724 id_priv->id.context = context; 725 id_priv->id.event_handler = event_handler; 726 id_priv->id.ps = ps; 727 id_priv->id.qp_type = qp_type; 728 spin_lock_init(&id_priv->lock); 729 mutex_init(&id_priv->qp_mutex); 730 init_completion(&id_priv->comp); 731 atomic_set(&id_priv->refcount, 1); 732 mutex_init(&id_priv->handler_mutex); 733 INIT_LIST_HEAD(&id_priv->listen_list); 734 INIT_LIST_HEAD(&id_priv->mc_list); 735 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num); 736 id_priv->id.route.addr.dev_addr.net = get_net(net); 737 738 return &id_priv->id; 739 } 740 EXPORT_SYMBOL(rdma_create_id); 741 742 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) 743 { 744 struct ib_qp_attr qp_attr; 745 int qp_attr_mask, ret; 746 747 qp_attr.qp_state = IB_QPS_INIT; 748 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); 749 if (ret) 750 return ret; 751 752 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); 753 if (ret) 754 return ret; 755 756 qp_attr.qp_state = IB_QPS_RTR; 757 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE); 758 if (ret) 759 return ret; 760 761 qp_attr.qp_state = IB_QPS_RTS; 762 qp_attr.sq_psn = 0; 763 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN); 764 765 return ret; 766 } 767 768 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) 769 { 770 struct ib_qp_attr qp_attr; 771 int qp_attr_mask, ret; 772 773 qp_attr.qp_state = IB_QPS_INIT; 774 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); 775 if (ret) 776 return ret; 777 778 return ib_modify_qp(qp, &qp_attr, qp_attr_mask); 779 } 780 781 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd, 782 struct ib_qp_init_attr *qp_init_attr) 783 { 784 struct rdma_id_private *id_priv; 785 struct ib_qp *qp; 786 int ret; 787 788 id_priv = container_of(id, struct rdma_id_private, id); 789 if (id->device != pd->device) 790 return -EINVAL; 791 792 qp_init_attr->port_num = id->port_num; 793 qp = ib_create_qp(pd, qp_init_attr); 794 if (IS_ERR(qp)) 795 return PTR_ERR(qp); 796 797 if (id->qp_type == IB_QPT_UD) 798 ret = cma_init_ud_qp(id_priv, qp); 799 else 800 ret = cma_init_conn_qp(id_priv, qp); 801 if (ret) 802 goto err; 803 804 id->qp = qp; 805 id_priv->qp_num = qp->qp_num; 806 id_priv->srq = (qp->srq != NULL); 807 return 0; 808 err: 809 ib_destroy_qp(qp); 810 return ret; 811 } 812 EXPORT_SYMBOL(rdma_create_qp); 813 814 void rdma_destroy_qp(struct rdma_cm_id *id) 815 { 816 struct rdma_id_private *id_priv; 817 818 id_priv = container_of(id, struct rdma_id_private, id); 819 mutex_lock(&id_priv->qp_mutex); 820 ib_destroy_qp(id_priv->id.qp); 821 id_priv->id.qp = NULL; 822 mutex_unlock(&id_priv->qp_mutex); 823 } 824 EXPORT_SYMBOL(rdma_destroy_qp); 825 826 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv, 827 struct rdma_conn_param *conn_param) 828 { 829 struct ib_qp_attr qp_attr; 830 int qp_attr_mask, ret; 831 union ib_gid sgid; 832 833 mutex_lock(&id_priv->qp_mutex); 834 if (!id_priv->id.qp) { 835 ret = 0; 836 goto out; 837 } 838 839 /* Need to update QP attributes from default values. */ 840 qp_attr.qp_state = IB_QPS_INIT; 841 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); 842 if (ret) 843 goto out; 844 845 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); 846 if (ret) 847 goto out; 848 849 qp_attr.qp_state = IB_QPS_RTR; 850 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); 851 if (ret) 852 goto out; 853 854 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num, 855 qp_attr.ah_attr.grh.sgid_index, &sgid, NULL); 856 if (ret) 857 goto out; 858 859 BUG_ON(id_priv->cma_dev->device != id_priv->id.device); 860 861 if (conn_param) 862 qp_attr.max_dest_rd_atomic = conn_param->responder_resources; 863 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); 864 out: 865 mutex_unlock(&id_priv->qp_mutex); 866 return ret; 867 } 868 869 static int cma_modify_qp_rts(struct rdma_id_private *id_priv, 870 struct rdma_conn_param *conn_param) 871 { 872 struct ib_qp_attr qp_attr; 873 int qp_attr_mask, ret; 874 875 mutex_lock(&id_priv->qp_mutex); 876 if (!id_priv->id.qp) { 877 ret = 0; 878 goto out; 879 } 880 881 qp_attr.qp_state = IB_QPS_RTS; 882 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); 883 if (ret) 884 goto out; 885 886 if (conn_param) 887 qp_attr.max_rd_atomic = conn_param->initiator_depth; 888 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); 889 out: 890 mutex_unlock(&id_priv->qp_mutex); 891 return ret; 892 } 893 894 static int cma_modify_qp_err(struct rdma_id_private *id_priv) 895 { 896 struct ib_qp_attr qp_attr; 897 int ret; 898 899 mutex_lock(&id_priv->qp_mutex); 900 if (!id_priv->id.qp) { 901 ret = 0; 902 goto out; 903 } 904 905 qp_attr.qp_state = IB_QPS_ERR; 906 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE); 907 out: 908 mutex_unlock(&id_priv->qp_mutex); 909 return ret; 910 } 911 912 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv, 913 struct ib_qp_attr *qp_attr, int *qp_attr_mask) 914 { 915 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 916 int ret; 917 u16 pkey; 918 919 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num)) 920 pkey = 0xffff; 921 else 922 pkey = ib_addr_get_pkey(dev_addr); 923 924 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num, 925 pkey, &qp_attr->pkey_index); 926 if (ret) 927 return ret; 928 929 qp_attr->port_num = id_priv->id.port_num; 930 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT; 931 932 if (id_priv->id.qp_type == IB_QPT_UD) { 933 ret = cma_set_qkey(id_priv, 0); 934 if (ret) 935 return ret; 936 937 qp_attr->qkey = id_priv->qkey; 938 *qp_attr_mask |= IB_QP_QKEY; 939 } else { 940 qp_attr->qp_access_flags = 0; 941 *qp_attr_mask |= IB_QP_ACCESS_FLAGS; 942 } 943 return 0; 944 } 945 946 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr, 947 int *qp_attr_mask) 948 { 949 struct rdma_id_private *id_priv; 950 int ret = 0; 951 952 id_priv = container_of(id, struct rdma_id_private, id); 953 if (rdma_cap_ib_cm(id->device, id->port_num)) { 954 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD)) 955 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask); 956 else 957 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr, 958 qp_attr_mask); 959 960 if (qp_attr->qp_state == IB_QPS_RTR) 961 qp_attr->rq_psn = id_priv->seq_num; 962 } else if (rdma_cap_iw_cm(id->device, id->port_num)) { 963 if (!id_priv->cm_id.iw) { 964 qp_attr->qp_access_flags = 0; 965 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS; 966 } else 967 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr, 968 qp_attr_mask); 969 } else 970 ret = -ENOSYS; 971 972 return ret; 973 } 974 EXPORT_SYMBOL(rdma_init_qp_attr); 975 976 static inline int cma_zero_addr(struct sockaddr *addr) 977 { 978 switch (addr->sa_family) { 979 case AF_INET: 980 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr); 981 case AF_INET6: 982 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr); 983 case AF_IB: 984 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr); 985 default: 986 return 0; 987 } 988 } 989 990 static inline int cma_loopback_addr(struct sockaddr *addr) 991 { 992 switch (addr->sa_family) { 993 case AF_INET: 994 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr); 995 case AF_INET6: 996 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr); 997 case AF_IB: 998 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr); 999 default: 1000 return 0; 1001 } 1002 } 1003 1004 static inline int cma_any_addr(struct sockaddr *addr) 1005 { 1006 return cma_zero_addr(addr) || cma_loopback_addr(addr); 1007 } 1008 1009 static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst) 1010 { 1011 if (src->sa_family != dst->sa_family) 1012 return -1; 1013 1014 switch (src->sa_family) { 1015 case AF_INET: 1016 return ((struct sockaddr_in *) src)->sin_addr.s_addr != 1017 ((struct sockaddr_in *) dst)->sin_addr.s_addr; 1018 case AF_INET6: 1019 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr, 1020 &((struct sockaddr_in6 *) dst)->sin6_addr); 1021 default: 1022 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr, 1023 &((struct sockaddr_ib *) dst)->sib_addr); 1024 } 1025 } 1026 1027 static __be16 cma_port(struct sockaddr *addr) 1028 { 1029 struct sockaddr_ib *sib; 1030 1031 switch (addr->sa_family) { 1032 case AF_INET: 1033 return ((struct sockaddr_in *) addr)->sin_port; 1034 case AF_INET6: 1035 return ((struct sockaddr_in6 *) addr)->sin6_port; 1036 case AF_IB: 1037 sib = (struct sockaddr_ib *) addr; 1038 return htons((u16) (be64_to_cpu(sib->sib_sid) & 1039 be64_to_cpu(sib->sib_sid_mask))); 1040 default: 1041 return 0; 1042 } 1043 } 1044 1045 static inline int cma_any_port(struct sockaddr *addr) 1046 { 1047 return !cma_port(addr); 1048 } 1049 1050 static void cma_save_ib_info(struct sockaddr *src_addr, 1051 struct sockaddr *dst_addr, 1052 struct rdma_cm_id *listen_id, 1053 struct ib_sa_path_rec *path) 1054 { 1055 struct sockaddr_ib *listen_ib, *ib; 1056 1057 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr; 1058 if (src_addr) { 1059 ib = (struct sockaddr_ib *)src_addr; 1060 ib->sib_family = AF_IB; 1061 if (path) { 1062 ib->sib_pkey = path->pkey; 1063 ib->sib_flowinfo = path->flow_label; 1064 memcpy(&ib->sib_addr, &path->sgid, 16); 1065 ib->sib_sid = path->service_id; 1066 ib->sib_scope_id = 0; 1067 } else { 1068 ib->sib_pkey = listen_ib->sib_pkey; 1069 ib->sib_flowinfo = listen_ib->sib_flowinfo; 1070 ib->sib_addr = listen_ib->sib_addr; 1071 ib->sib_sid = listen_ib->sib_sid; 1072 ib->sib_scope_id = listen_ib->sib_scope_id; 1073 } 1074 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL); 1075 } 1076 if (dst_addr) { 1077 ib = (struct sockaddr_ib *)dst_addr; 1078 ib->sib_family = AF_IB; 1079 if (path) { 1080 ib->sib_pkey = path->pkey; 1081 ib->sib_flowinfo = path->flow_label; 1082 memcpy(&ib->sib_addr, &path->dgid, 16); 1083 } 1084 } 1085 } 1086 1087 static void cma_save_ip4_info(struct sockaddr *src_addr, 1088 struct sockaddr *dst_addr, 1089 struct cma_hdr *hdr, 1090 __be16 local_port) 1091 { 1092 struct sockaddr_in *ip4; 1093 1094 if (src_addr) { 1095 ip4 = (struct sockaddr_in *)src_addr; 1096 ip4->sin_family = AF_INET; 1097 ip4->sin_addr.s_addr = hdr->dst_addr.ip4.addr; 1098 ip4->sin_port = local_port; 1099 } 1100 1101 if (dst_addr) { 1102 ip4 = (struct sockaddr_in *)dst_addr; 1103 ip4->sin_family = AF_INET; 1104 ip4->sin_addr.s_addr = hdr->src_addr.ip4.addr; 1105 ip4->sin_port = hdr->port; 1106 } 1107 } 1108 1109 static void cma_save_ip6_info(struct sockaddr *src_addr, 1110 struct sockaddr *dst_addr, 1111 struct cma_hdr *hdr, 1112 __be16 local_port) 1113 { 1114 struct sockaddr_in6 *ip6; 1115 1116 if (src_addr) { 1117 ip6 = (struct sockaddr_in6 *)src_addr; 1118 ip6->sin6_family = AF_INET6; 1119 ip6->sin6_addr = hdr->dst_addr.ip6; 1120 ip6->sin6_port = local_port; 1121 } 1122 1123 if (dst_addr) { 1124 ip6 = (struct sockaddr_in6 *)dst_addr; 1125 ip6->sin6_family = AF_INET6; 1126 ip6->sin6_addr = hdr->src_addr.ip6; 1127 ip6->sin6_port = hdr->port; 1128 } 1129 } 1130 1131 static u16 cma_port_from_service_id(__be64 service_id) 1132 { 1133 return (u16)be64_to_cpu(service_id); 1134 } 1135 1136 static int cma_save_ip_info(struct sockaddr *src_addr, 1137 struct sockaddr *dst_addr, 1138 struct ib_cm_event *ib_event, 1139 __be64 service_id) 1140 { 1141 struct cma_hdr *hdr; 1142 __be16 port; 1143 1144 hdr = ib_event->private_data; 1145 if (hdr->cma_version != CMA_VERSION) 1146 return -EINVAL; 1147 1148 port = htons(cma_port_from_service_id(service_id)); 1149 1150 switch (cma_get_ip_ver(hdr)) { 1151 case 4: 1152 cma_save_ip4_info(src_addr, dst_addr, hdr, port); 1153 break; 1154 case 6: 1155 cma_save_ip6_info(src_addr, dst_addr, hdr, port); 1156 break; 1157 default: 1158 return -EAFNOSUPPORT; 1159 } 1160 1161 return 0; 1162 } 1163 1164 static int cma_save_net_info(struct sockaddr *src_addr, 1165 struct sockaddr *dst_addr, 1166 struct rdma_cm_id *listen_id, 1167 struct ib_cm_event *ib_event, 1168 sa_family_t sa_family, __be64 service_id) 1169 { 1170 if (sa_family == AF_IB) { 1171 if (ib_event->event == IB_CM_REQ_RECEIVED) 1172 cma_save_ib_info(src_addr, dst_addr, listen_id, 1173 ib_event->param.req_rcvd.primary_path); 1174 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) 1175 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL); 1176 return 0; 1177 } 1178 1179 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id); 1180 } 1181 1182 static int cma_save_req_info(const struct ib_cm_event *ib_event, 1183 struct cma_req_info *req) 1184 { 1185 const struct ib_cm_req_event_param *req_param = 1186 &ib_event->param.req_rcvd; 1187 const struct ib_cm_sidr_req_event_param *sidr_param = 1188 &ib_event->param.sidr_req_rcvd; 1189 1190 switch (ib_event->event) { 1191 case IB_CM_REQ_RECEIVED: 1192 req->device = req_param->listen_id->device; 1193 req->port = req_param->port; 1194 memcpy(&req->local_gid, &req_param->primary_path->sgid, 1195 sizeof(req->local_gid)); 1196 req->has_gid = true; 1197 req->service_id = req_param->primary_path->service_id; 1198 req->pkey = be16_to_cpu(req_param->primary_path->pkey); 1199 if (req->pkey != req_param->bth_pkey) 1200 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n" 1201 "RDMA CMA: in the future this may cause the request to be dropped\n", 1202 req_param->bth_pkey, req->pkey); 1203 break; 1204 case IB_CM_SIDR_REQ_RECEIVED: 1205 req->device = sidr_param->listen_id->device; 1206 req->port = sidr_param->port; 1207 req->has_gid = false; 1208 req->service_id = sidr_param->service_id; 1209 req->pkey = sidr_param->pkey; 1210 if (req->pkey != sidr_param->bth_pkey) 1211 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n" 1212 "RDMA CMA: in the future this may cause the request to be dropped\n", 1213 sidr_param->bth_pkey, req->pkey); 1214 break; 1215 default: 1216 return -EINVAL; 1217 } 1218 1219 return 0; 1220 } 1221 1222 static bool validate_ipv4_net_dev(struct net_device *net_dev, 1223 const struct sockaddr_in *dst_addr, 1224 const struct sockaddr_in *src_addr) 1225 { 1226 __be32 daddr = dst_addr->sin_addr.s_addr, 1227 saddr = src_addr->sin_addr.s_addr; 1228 struct fib_result res; 1229 struct flowi4 fl4; 1230 int err; 1231 bool ret; 1232 1233 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || 1234 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) || 1235 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) || 1236 ipv4_is_loopback(saddr)) 1237 return false; 1238 1239 memset(&fl4, 0, sizeof(fl4)); 1240 fl4.flowi4_iif = net_dev->ifindex; 1241 fl4.daddr = daddr; 1242 fl4.saddr = saddr; 1243 1244 rcu_read_lock(); 1245 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0); 1246 ret = err == 0 && FIB_RES_DEV(res) == net_dev; 1247 rcu_read_unlock(); 1248 1249 return ret; 1250 } 1251 1252 static bool validate_ipv6_net_dev(struct net_device *net_dev, 1253 const struct sockaddr_in6 *dst_addr, 1254 const struct sockaddr_in6 *src_addr) 1255 { 1256 #if IS_ENABLED(CONFIG_IPV6) 1257 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) & 1258 IPV6_ADDR_LINKLOCAL; 1259 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr, 1260 &src_addr->sin6_addr, net_dev->ifindex, 1261 strict); 1262 bool ret; 1263 1264 if (!rt) 1265 return false; 1266 1267 ret = rt->rt6i_idev->dev == net_dev; 1268 ip6_rt_put(rt); 1269 1270 return ret; 1271 #else 1272 return false; 1273 #endif 1274 } 1275 1276 static bool validate_net_dev(struct net_device *net_dev, 1277 const struct sockaddr *daddr, 1278 const struct sockaddr *saddr) 1279 { 1280 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr; 1281 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr; 1282 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr; 1283 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr; 1284 1285 switch (daddr->sa_family) { 1286 case AF_INET: 1287 return saddr->sa_family == AF_INET && 1288 validate_ipv4_net_dev(net_dev, daddr4, saddr4); 1289 1290 case AF_INET6: 1291 return saddr->sa_family == AF_INET6 && 1292 validate_ipv6_net_dev(net_dev, daddr6, saddr6); 1293 1294 default: 1295 return false; 1296 } 1297 } 1298 1299 static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event, 1300 const struct cma_req_info *req) 1301 { 1302 struct sockaddr_storage listen_addr_storage, src_addr_storage; 1303 struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage, 1304 *src_addr = (struct sockaddr *)&src_addr_storage; 1305 struct net_device *net_dev; 1306 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL; 1307 int err; 1308 1309 err = cma_save_ip_info(listen_addr, src_addr, ib_event, 1310 req->service_id); 1311 if (err) 1312 return ERR_PTR(err); 1313 1314 net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey, 1315 gid, listen_addr); 1316 if (!net_dev) 1317 return ERR_PTR(-ENODEV); 1318 1319 if (!validate_net_dev(net_dev, listen_addr, src_addr)) { 1320 dev_put(net_dev); 1321 return ERR_PTR(-EHOSTUNREACH); 1322 } 1323 1324 return net_dev; 1325 } 1326 1327 static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id) 1328 { 1329 return (be64_to_cpu(service_id) >> 16) & 0xffff; 1330 } 1331 1332 static bool cma_match_private_data(struct rdma_id_private *id_priv, 1333 const struct cma_hdr *hdr) 1334 { 1335 struct sockaddr *addr = cma_src_addr(id_priv); 1336 __be32 ip4_addr; 1337 struct in6_addr ip6_addr; 1338 1339 if (cma_any_addr(addr) && !id_priv->afonly) 1340 return true; 1341 1342 switch (addr->sa_family) { 1343 case AF_INET: 1344 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; 1345 if (cma_get_ip_ver(hdr) != 4) 1346 return false; 1347 if (!cma_any_addr(addr) && 1348 hdr->dst_addr.ip4.addr != ip4_addr) 1349 return false; 1350 break; 1351 case AF_INET6: 1352 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr; 1353 if (cma_get_ip_ver(hdr) != 6) 1354 return false; 1355 if (!cma_any_addr(addr) && 1356 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr))) 1357 return false; 1358 break; 1359 case AF_IB: 1360 return true; 1361 default: 1362 return false; 1363 } 1364 1365 return true; 1366 } 1367 1368 static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num) 1369 { 1370 enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num); 1371 enum rdma_transport_type transport = 1372 rdma_node_get_transport(device->node_type); 1373 1374 return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB; 1375 } 1376 1377 static bool cma_protocol_roce(const struct rdma_cm_id *id) 1378 { 1379 struct ib_device *device = id->device; 1380 const int port_num = id->port_num ?: rdma_start_port(device); 1381 1382 return cma_protocol_roce_dev_port(device, port_num); 1383 } 1384 1385 static bool cma_match_net_dev(const struct rdma_cm_id *id, 1386 const struct net_device *net_dev, 1387 u8 port_num) 1388 { 1389 const struct rdma_addr *addr = &id->route.addr; 1390 1391 if (!net_dev) 1392 /* This request is an AF_IB request or a RoCE request */ 1393 return (!id->port_num || id->port_num == port_num) && 1394 (addr->src_addr.ss_family == AF_IB || 1395 cma_protocol_roce_dev_port(id->device, port_num)); 1396 1397 return !addr->dev_addr.bound_dev_if || 1398 (net_eq(dev_net(net_dev), addr->dev_addr.net) && 1399 addr->dev_addr.bound_dev_if == net_dev->ifindex); 1400 } 1401 1402 static struct rdma_id_private *cma_find_listener( 1403 const struct rdma_bind_list *bind_list, 1404 const struct ib_cm_id *cm_id, 1405 const struct ib_cm_event *ib_event, 1406 const struct cma_req_info *req, 1407 const struct net_device *net_dev) 1408 { 1409 struct rdma_id_private *id_priv, *id_priv_dev; 1410 1411 if (!bind_list) 1412 return ERR_PTR(-EINVAL); 1413 1414 hlist_for_each_entry(id_priv, &bind_list->owners, node) { 1415 if (cma_match_private_data(id_priv, ib_event->private_data)) { 1416 if (id_priv->id.device == cm_id->device && 1417 cma_match_net_dev(&id_priv->id, net_dev, req->port)) 1418 return id_priv; 1419 list_for_each_entry(id_priv_dev, 1420 &id_priv->listen_list, 1421 listen_list) { 1422 if (id_priv_dev->id.device == cm_id->device && 1423 cma_match_net_dev(&id_priv_dev->id, net_dev, req->port)) 1424 return id_priv_dev; 1425 } 1426 } 1427 } 1428 1429 return ERR_PTR(-EINVAL); 1430 } 1431 1432 static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id, 1433 struct ib_cm_event *ib_event, 1434 struct net_device **net_dev) 1435 { 1436 struct cma_req_info req; 1437 struct rdma_bind_list *bind_list; 1438 struct rdma_id_private *id_priv; 1439 int err; 1440 1441 err = cma_save_req_info(ib_event, &req); 1442 if (err) 1443 return ERR_PTR(err); 1444 1445 *net_dev = cma_get_net_dev(ib_event, &req); 1446 if (IS_ERR(*net_dev)) { 1447 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) { 1448 /* Assuming the protocol is AF_IB */ 1449 *net_dev = NULL; 1450 } else if (cma_protocol_roce_dev_port(req.device, req.port)) { 1451 /* TODO find the net dev matching the request parameters 1452 * through the RoCE GID table */ 1453 *net_dev = NULL; 1454 } else { 1455 return ERR_CAST(*net_dev); 1456 } 1457 } 1458 1459 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net, 1460 rdma_ps_from_service_id(req.service_id), 1461 cma_port_from_service_id(req.service_id)); 1462 id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev); 1463 if (IS_ERR(id_priv) && *net_dev) { 1464 dev_put(*net_dev); 1465 *net_dev = NULL; 1466 } 1467 1468 return id_priv; 1469 } 1470 1471 static inline int cma_user_data_offset(struct rdma_id_private *id_priv) 1472 { 1473 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr); 1474 } 1475 1476 static void cma_cancel_route(struct rdma_id_private *id_priv) 1477 { 1478 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) { 1479 if (id_priv->query) 1480 ib_sa_cancel_query(id_priv->query_id, id_priv->query); 1481 } 1482 } 1483 1484 static void cma_cancel_listens(struct rdma_id_private *id_priv) 1485 { 1486 struct rdma_id_private *dev_id_priv; 1487 1488 /* 1489 * Remove from listen_any_list to prevent added devices from spawning 1490 * additional listen requests. 1491 */ 1492 mutex_lock(&lock); 1493 list_del(&id_priv->list); 1494 1495 while (!list_empty(&id_priv->listen_list)) { 1496 dev_id_priv = list_entry(id_priv->listen_list.next, 1497 struct rdma_id_private, listen_list); 1498 /* sync with device removal to avoid duplicate destruction */ 1499 list_del_init(&dev_id_priv->list); 1500 list_del(&dev_id_priv->listen_list); 1501 mutex_unlock(&lock); 1502 1503 rdma_destroy_id(&dev_id_priv->id); 1504 mutex_lock(&lock); 1505 } 1506 mutex_unlock(&lock); 1507 } 1508 1509 static void cma_cancel_operation(struct rdma_id_private *id_priv, 1510 enum rdma_cm_state state) 1511 { 1512 switch (state) { 1513 case RDMA_CM_ADDR_QUERY: 1514 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr); 1515 break; 1516 case RDMA_CM_ROUTE_QUERY: 1517 cma_cancel_route(id_priv); 1518 break; 1519 case RDMA_CM_LISTEN: 1520 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev) 1521 cma_cancel_listens(id_priv); 1522 break; 1523 default: 1524 break; 1525 } 1526 } 1527 1528 static void cma_release_port(struct rdma_id_private *id_priv) 1529 { 1530 struct rdma_bind_list *bind_list = id_priv->bind_list; 1531 struct net *net = id_priv->id.route.addr.dev_addr.net; 1532 1533 if (!bind_list) 1534 return; 1535 1536 mutex_lock(&lock); 1537 hlist_del(&id_priv->node); 1538 if (hlist_empty(&bind_list->owners)) { 1539 cma_ps_remove(net, bind_list->ps, bind_list->port); 1540 kfree(bind_list); 1541 } 1542 mutex_unlock(&lock); 1543 } 1544 1545 static void cma_leave_mc_groups(struct rdma_id_private *id_priv) 1546 { 1547 struct cma_multicast *mc; 1548 1549 while (!list_empty(&id_priv->mc_list)) { 1550 mc = container_of(id_priv->mc_list.next, 1551 struct cma_multicast, list); 1552 list_del(&mc->list); 1553 if (rdma_cap_ib_mcast(id_priv->cma_dev->device, 1554 id_priv->id.port_num)) { 1555 ib_sa_free_multicast(mc->multicast.ib); 1556 kfree(mc); 1557 } else { 1558 if (mc->igmp_joined) { 1559 struct rdma_dev_addr *dev_addr = 1560 &id_priv->id.route.addr.dev_addr; 1561 struct net_device *ndev = NULL; 1562 1563 if (dev_addr->bound_dev_if) 1564 ndev = dev_get_by_index(&init_net, 1565 dev_addr->bound_dev_if); 1566 if (ndev) { 1567 cma_igmp_send(ndev, 1568 &mc->multicast.ib->rec.mgid, 1569 false); 1570 dev_put(ndev); 1571 } 1572 } 1573 kref_put(&mc->mcref, release_mc); 1574 } 1575 } 1576 } 1577 1578 void rdma_destroy_id(struct rdma_cm_id *id) 1579 { 1580 struct rdma_id_private *id_priv; 1581 enum rdma_cm_state state; 1582 1583 id_priv = container_of(id, struct rdma_id_private, id); 1584 state = cma_exch(id_priv, RDMA_CM_DESTROYING); 1585 cma_cancel_operation(id_priv, state); 1586 1587 /* 1588 * Wait for any active callback to finish. New callbacks will find 1589 * the id_priv state set to destroying and abort. 1590 */ 1591 mutex_lock(&id_priv->handler_mutex); 1592 mutex_unlock(&id_priv->handler_mutex); 1593 1594 if (id_priv->cma_dev) { 1595 if (rdma_cap_ib_cm(id_priv->id.device, 1)) { 1596 if (id_priv->cm_id.ib) 1597 ib_destroy_cm_id(id_priv->cm_id.ib); 1598 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) { 1599 if (id_priv->cm_id.iw) 1600 iw_destroy_cm_id(id_priv->cm_id.iw); 1601 } 1602 cma_leave_mc_groups(id_priv); 1603 cma_release_dev(id_priv); 1604 } 1605 1606 cma_release_port(id_priv); 1607 cma_deref_id(id_priv); 1608 wait_for_completion(&id_priv->comp); 1609 1610 if (id_priv->internal_id) 1611 cma_deref_id(id_priv->id.context); 1612 1613 kfree(id_priv->id.route.path_rec); 1614 put_net(id_priv->id.route.addr.dev_addr.net); 1615 kfree(id_priv); 1616 } 1617 EXPORT_SYMBOL(rdma_destroy_id); 1618 1619 static int cma_rep_recv(struct rdma_id_private *id_priv) 1620 { 1621 int ret; 1622 1623 ret = cma_modify_qp_rtr(id_priv, NULL); 1624 if (ret) 1625 goto reject; 1626 1627 ret = cma_modify_qp_rts(id_priv, NULL); 1628 if (ret) 1629 goto reject; 1630 1631 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0); 1632 if (ret) 1633 goto reject; 1634 1635 return 0; 1636 reject: 1637 cma_modify_qp_err(id_priv); 1638 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED, 1639 NULL, 0, NULL, 0); 1640 return ret; 1641 } 1642 1643 static void cma_set_rep_event_data(struct rdma_cm_event *event, 1644 struct ib_cm_rep_event_param *rep_data, 1645 void *private_data) 1646 { 1647 event->param.conn.private_data = private_data; 1648 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE; 1649 event->param.conn.responder_resources = rep_data->responder_resources; 1650 event->param.conn.initiator_depth = rep_data->initiator_depth; 1651 event->param.conn.flow_control = rep_data->flow_control; 1652 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count; 1653 event->param.conn.srq = rep_data->srq; 1654 event->param.conn.qp_num = rep_data->remote_qpn; 1655 } 1656 1657 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) 1658 { 1659 struct rdma_id_private *id_priv = cm_id->context; 1660 struct rdma_cm_event event; 1661 int ret = 0; 1662 1663 mutex_lock(&id_priv->handler_mutex); 1664 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT && 1665 id_priv->state != RDMA_CM_CONNECT) || 1666 (ib_event->event == IB_CM_TIMEWAIT_EXIT && 1667 id_priv->state != RDMA_CM_DISCONNECT)) 1668 goto out; 1669 1670 memset(&event, 0, sizeof event); 1671 switch (ib_event->event) { 1672 case IB_CM_REQ_ERROR: 1673 case IB_CM_REP_ERROR: 1674 event.event = RDMA_CM_EVENT_UNREACHABLE; 1675 event.status = -ETIMEDOUT; 1676 break; 1677 case IB_CM_REP_RECEIVED: 1678 if (id_priv->id.qp) { 1679 event.status = cma_rep_recv(id_priv); 1680 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR : 1681 RDMA_CM_EVENT_ESTABLISHED; 1682 } else { 1683 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE; 1684 } 1685 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd, 1686 ib_event->private_data); 1687 break; 1688 case IB_CM_RTU_RECEIVED: 1689 case IB_CM_USER_ESTABLISHED: 1690 event.event = RDMA_CM_EVENT_ESTABLISHED; 1691 break; 1692 case IB_CM_DREQ_ERROR: 1693 event.status = -ETIMEDOUT; /* fall through */ 1694 case IB_CM_DREQ_RECEIVED: 1695 case IB_CM_DREP_RECEIVED: 1696 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT, 1697 RDMA_CM_DISCONNECT)) 1698 goto out; 1699 event.event = RDMA_CM_EVENT_DISCONNECTED; 1700 break; 1701 case IB_CM_TIMEWAIT_EXIT: 1702 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT; 1703 break; 1704 case IB_CM_MRA_RECEIVED: 1705 /* ignore event */ 1706 goto out; 1707 case IB_CM_REJ_RECEIVED: 1708 cma_modify_qp_err(id_priv); 1709 event.status = ib_event->param.rej_rcvd.reason; 1710 event.event = RDMA_CM_EVENT_REJECTED; 1711 event.param.conn.private_data = ib_event->private_data; 1712 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE; 1713 break; 1714 default: 1715 pr_err("RDMA CMA: unexpected IB CM event: %d\n", 1716 ib_event->event); 1717 goto out; 1718 } 1719 1720 ret = id_priv->id.event_handler(&id_priv->id, &event); 1721 if (ret) { 1722 /* Destroy the CM ID by returning a non-zero value. */ 1723 id_priv->cm_id.ib = NULL; 1724 cma_exch(id_priv, RDMA_CM_DESTROYING); 1725 mutex_unlock(&id_priv->handler_mutex); 1726 rdma_destroy_id(&id_priv->id); 1727 return ret; 1728 } 1729 out: 1730 mutex_unlock(&id_priv->handler_mutex); 1731 return ret; 1732 } 1733 1734 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id, 1735 struct ib_cm_event *ib_event, 1736 struct net_device *net_dev) 1737 { 1738 struct rdma_id_private *id_priv; 1739 struct rdma_cm_id *id; 1740 struct rdma_route *rt; 1741 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family; 1742 const __be64 service_id = 1743 ib_event->param.req_rcvd.primary_path->service_id; 1744 int ret; 1745 1746 id = rdma_create_id(listen_id->route.addr.dev_addr.net, 1747 listen_id->event_handler, listen_id->context, 1748 listen_id->ps, ib_event->param.req_rcvd.qp_type); 1749 if (IS_ERR(id)) 1750 return NULL; 1751 1752 id_priv = container_of(id, struct rdma_id_private, id); 1753 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr, 1754 (struct sockaddr *)&id->route.addr.dst_addr, 1755 listen_id, ib_event, ss_family, service_id)) 1756 goto err; 1757 1758 rt = &id->route; 1759 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1; 1760 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths, 1761 GFP_KERNEL); 1762 if (!rt->path_rec) 1763 goto err; 1764 1765 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path; 1766 if (rt->num_paths == 2) 1767 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path; 1768 1769 if (net_dev) { 1770 ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL); 1771 if (ret) 1772 goto err; 1773 } else { 1774 if (!cma_protocol_roce(listen_id) && 1775 cma_any_addr(cma_src_addr(id_priv))) { 1776 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND; 1777 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid); 1778 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey)); 1779 } else if (!cma_any_addr(cma_src_addr(id_priv))) { 1780 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr); 1781 if (ret) 1782 goto err; 1783 } 1784 } 1785 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid); 1786 1787 id_priv->state = RDMA_CM_CONNECT; 1788 return id_priv; 1789 1790 err: 1791 rdma_destroy_id(id); 1792 return NULL; 1793 } 1794 1795 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id, 1796 struct ib_cm_event *ib_event, 1797 struct net_device *net_dev) 1798 { 1799 struct rdma_id_private *id_priv; 1800 struct rdma_cm_id *id; 1801 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family; 1802 struct net *net = listen_id->route.addr.dev_addr.net; 1803 int ret; 1804 1805 id = rdma_create_id(net, listen_id->event_handler, listen_id->context, 1806 listen_id->ps, IB_QPT_UD); 1807 if (IS_ERR(id)) 1808 return NULL; 1809 1810 id_priv = container_of(id, struct rdma_id_private, id); 1811 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr, 1812 (struct sockaddr *)&id->route.addr.dst_addr, 1813 listen_id, ib_event, ss_family, 1814 ib_event->param.sidr_req_rcvd.service_id)) 1815 goto err; 1816 1817 if (net_dev) { 1818 ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL); 1819 if (ret) 1820 goto err; 1821 } else { 1822 if (!cma_any_addr(cma_src_addr(id_priv))) { 1823 ret = cma_translate_addr(cma_src_addr(id_priv), 1824 &id->route.addr.dev_addr); 1825 if (ret) 1826 goto err; 1827 } 1828 } 1829 1830 id_priv->state = RDMA_CM_CONNECT; 1831 return id_priv; 1832 err: 1833 rdma_destroy_id(id); 1834 return NULL; 1835 } 1836 1837 static void cma_set_req_event_data(struct rdma_cm_event *event, 1838 struct ib_cm_req_event_param *req_data, 1839 void *private_data, int offset) 1840 { 1841 event->param.conn.private_data = private_data + offset; 1842 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset; 1843 event->param.conn.responder_resources = req_data->responder_resources; 1844 event->param.conn.initiator_depth = req_data->initiator_depth; 1845 event->param.conn.flow_control = req_data->flow_control; 1846 event->param.conn.retry_count = req_data->retry_count; 1847 event->param.conn.rnr_retry_count = req_data->rnr_retry_count; 1848 event->param.conn.srq = req_data->srq; 1849 event->param.conn.qp_num = req_data->remote_qpn; 1850 } 1851 1852 static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event) 1853 { 1854 return (((ib_event->event == IB_CM_REQ_RECEIVED) && 1855 (ib_event->param.req_rcvd.qp_type == id->qp_type)) || 1856 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) && 1857 (id->qp_type == IB_QPT_UD)) || 1858 (!id->qp_type)); 1859 } 1860 1861 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) 1862 { 1863 struct rdma_id_private *listen_id, *conn_id = NULL; 1864 struct rdma_cm_event event; 1865 struct net_device *net_dev; 1866 int offset, ret; 1867 1868 listen_id = cma_id_from_event(cm_id, ib_event, &net_dev); 1869 if (IS_ERR(listen_id)) 1870 return PTR_ERR(listen_id); 1871 1872 if (!cma_check_req_qp_type(&listen_id->id, ib_event)) { 1873 ret = -EINVAL; 1874 goto net_dev_put; 1875 } 1876 1877 mutex_lock(&listen_id->handler_mutex); 1878 if (listen_id->state != RDMA_CM_LISTEN) { 1879 ret = -ECONNABORTED; 1880 goto err1; 1881 } 1882 1883 memset(&event, 0, sizeof event); 1884 offset = cma_user_data_offset(listen_id); 1885 event.event = RDMA_CM_EVENT_CONNECT_REQUEST; 1886 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) { 1887 conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev); 1888 event.param.ud.private_data = ib_event->private_data + offset; 1889 event.param.ud.private_data_len = 1890 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset; 1891 } else { 1892 conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev); 1893 cma_set_req_event_data(&event, &ib_event->param.req_rcvd, 1894 ib_event->private_data, offset); 1895 } 1896 if (!conn_id) { 1897 ret = -ENOMEM; 1898 goto err1; 1899 } 1900 1901 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); 1902 ret = cma_acquire_dev(conn_id, listen_id); 1903 if (ret) 1904 goto err2; 1905 1906 conn_id->cm_id.ib = cm_id; 1907 cm_id->context = conn_id; 1908 cm_id->cm_handler = cma_ib_handler; 1909 1910 /* 1911 * Protect against the user destroying conn_id from another thread 1912 * until we're done accessing it. 1913 */ 1914 atomic_inc(&conn_id->refcount); 1915 ret = conn_id->id.event_handler(&conn_id->id, &event); 1916 if (ret) 1917 goto err3; 1918 /* 1919 * Acquire mutex to prevent user executing rdma_destroy_id() 1920 * while we're accessing the cm_id. 1921 */ 1922 mutex_lock(&lock); 1923 if (cma_comp(conn_id, RDMA_CM_CONNECT) && 1924 (conn_id->id.qp_type != IB_QPT_UD)) 1925 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0); 1926 mutex_unlock(&lock); 1927 mutex_unlock(&conn_id->handler_mutex); 1928 mutex_unlock(&listen_id->handler_mutex); 1929 cma_deref_id(conn_id); 1930 if (net_dev) 1931 dev_put(net_dev); 1932 return 0; 1933 1934 err3: 1935 cma_deref_id(conn_id); 1936 /* Destroy the CM ID by returning a non-zero value. */ 1937 conn_id->cm_id.ib = NULL; 1938 err2: 1939 cma_exch(conn_id, RDMA_CM_DESTROYING); 1940 mutex_unlock(&conn_id->handler_mutex); 1941 err1: 1942 mutex_unlock(&listen_id->handler_mutex); 1943 if (conn_id) 1944 rdma_destroy_id(&conn_id->id); 1945 1946 net_dev_put: 1947 if (net_dev) 1948 dev_put(net_dev); 1949 1950 return ret; 1951 } 1952 1953 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr) 1954 { 1955 if (addr->sa_family == AF_IB) 1956 return ((struct sockaddr_ib *) addr)->sib_sid; 1957 1958 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr))); 1959 } 1960 EXPORT_SYMBOL(rdma_get_service_id); 1961 1962 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event) 1963 { 1964 struct rdma_id_private *id_priv = iw_id->context; 1965 struct rdma_cm_event event; 1966 int ret = 0; 1967 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr; 1968 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr; 1969 1970 mutex_lock(&id_priv->handler_mutex); 1971 if (id_priv->state != RDMA_CM_CONNECT) 1972 goto out; 1973 1974 memset(&event, 0, sizeof event); 1975 switch (iw_event->event) { 1976 case IW_CM_EVENT_CLOSE: 1977 event.event = RDMA_CM_EVENT_DISCONNECTED; 1978 break; 1979 case IW_CM_EVENT_CONNECT_REPLY: 1980 memcpy(cma_src_addr(id_priv), laddr, 1981 rdma_addr_size(laddr)); 1982 memcpy(cma_dst_addr(id_priv), raddr, 1983 rdma_addr_size(raddr)); 1984 switch (iw_event->status) { 1985 case 0: 1986 event.event = RDMA_CM_EVENT_ESTABLISHED; 1987 event.param.conn.initiator_depth = iw_event->ird; 1988 event.param.conn.responder_resources = iw_event->ord; 1989 break; 1990 case -ECONNRESET: 1991 case -ECONNREFUSED: 1992 event.event = RDMA_CM_EVENT_REJECTED; 1993 break; 1994 case -ETIMEDOUT: 1995 event.event = RDMA_CM_EVENT_UNREACHABLE; 1996 break; 1997 default: 1998 event.event = RDMA_CM_EVENT_CONNECT_ERROR; 1999 break; 2000 } 2001 break; 2002 case IW_CM_EVENT_ESTABLISHED: 2003 event.event = RDMA_CM_EVENT_ESTABLISHED; 2004 event.param.conn.initiator_depth = iw_event->ird; 2005 event.param.conn.responder_resources = iw_event->ord; 2006 break; 2007 default: 2008 BUG_ON(1); 2009 } 2010 2011 event.status = iw_event->status; 2012 event.param.conn.private_data = iw_event->private_data; 2013 event.param.conn.private_data_len = iw_event->private_data_len; 2014 ret = id_priv->id.event_handler(&id_priv->id, &event); 2015 if (ret) { 2016 /* Destroy the CM ID by returning a non-zero value. */ 2017 id_priv->cm_id.iw = NULL; 2018 cma_exch(id_priv, RDMA_CM_DESTROYING); 2019 mutex_unlock(&id_priv->handler_mutex); 2020 rdma_destroy_id(&id_priv->id); 2021 return ret; 2022 } 2023 2024 out: 2025 mutex_unlock(&id_priv->handler_mutex); 2026 return ret; 2027 } 2028 2029 static int iw_conn_req_handler(struct iw_cm_id *cm_id, 2030 struct iw_cm_event *iw_event) 2031 { 2032 struct rdma_cm_id *new_cm_id; 2033 struct rdma_id_private *listen_id, *conn_id; 2034 struct rdma_cm_event event; 2035 int ret = -ECONNABORTED; 2036 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr; 2037 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr; 2038 2039 listen_id = cm_id->context; 2040 2041 mutex_lock(&listen_id->handler_mutex); 2042 if (listen_id->state != RDMA_CM_LISTEN) 2043 goto out; 2044 2045 /* Create a new RDMA id for the new IW CM ID */ 2046 new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net, 2047 listen_id->id.event_handler, 2048 listen_id->id.context, 2049 RDMA_PS_TCP, IB_QPT_RC); 2050 if (IS_ERR(new_cm_id)) { 2051 ret = -ENOMEM; 2052 goto out; 2053 } 2054 conn_id = container_of(new_cm_id, struct rdma_id_private, id); 2055 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); 2056 conn_id->state = RDMA_CM_CONNECT; 2057 2058 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL); 2059 if (ret) { 2060 mutex_unlock(&conn_id->handler_mutex); 2061 rdma_destroy_id(new_cm_id); 2062 goto out; 2063 } 2064 2065 ret = cma_acquire_dev(conn_id, listen_id); 2066 if (ret) { 2067 mutex_unlock(&conn_id->handler_mutex); 2068 rdma_destroy_id(new_cm_id); 2069 goto out; 2070 } 2071 2072 conn_id->cm_id.iw = cm_id; 2073 cm_id->context = conn_id; 2074 cm_id->cm_handler = cma_iw_handler; 2075 2076 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr)); 2077 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr)); 2078 2079 memset(&event, 0, sizeof event); 2080 event.event = RDMA_CM_EVENT_CONNECT_REQUEST; 2081 event.param.conn.private_data = iw_event->private_data; 2082 event.param.conn.private_data_len = iw_event->private_data_len; 2083 event.param.conn.initiator_depth = iw_event->ird; 2084 event.param.conn.responder_resources = iw_event->ord; 2085 2086 /* 2087 * Protect against the user destroying conn_id from another thread 2088 * until we're done accessing it. 2089 */ 2090 atomic_inc(&conn_id->refcount); 2091 ret = conn_id->id.event_handler(&conn_id->id, &event); 2092 if (ret) { 2093 /* User wants to destroy the CM ID */ 2094 conn_id->cm_id.iw = NULL; 2095 cma_exch(conn_id, RDMA_CM_DESTROYING); 2096 mutex_unlock(&conn_id->handler_mutex); 2097 cma_deref_id(conn_id); 2098 rdma_destroy_id(&conn_id->id); 2099 goto out; 2100 } 2101 2102 mutex_unlock(&conn_id->handler_mutex); 2103 cma_deref_id(conn_id); 2104 2105 out: 2106 mutex_unlock(&listen_id->handler_mutex); 2107 return ret; 2108 } 2109 2110 static int cma_ib_listen(struct rdma_id_private *id_priv) 2111 { 2112 struct sockaddr *addr; 2113 struct ib_cm_id *id; 2114 __be64 svc_id; 2115 2116 addr = cma_src_addr(id_priv); 2117 svc_id = rdma_get_service_id(&id_priv->id, addr); 2118 id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id); 2119 if (IS_ERR(id)) 2120 return PTR_ERR(id); 2121 id_priv->cm_id.ib = id; 2122 2123 return 0; 2124 } 2125 2126 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog) 2127 { 2128 int ret; 2129 struct iw_cm_id *id; 2130 2131 id = iw_create_cm_id(id_priv->id.device, 2132 iw_conn_req_handler, 2133 id_priv); 2134 if (IS_ERR(id)) 2135 return PTR_ERR(id); 2136 2137 id->tos = id_priv->tos; 2138 id_priv->cm_id.iw = id; 2139 2140 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv), 2141 rdma_addr_size(cma_src_addr(id_priv))); 2142 2143 ret = iw_cm_listen(id_priv->cm_id.iw, backlog); 2144 2145 if (ret) { 2146 iw_destroy_cm_id(id_priv->cm_id.iw); 2147 id_priv->cm_id.iw = NULL; 2148 } 2149 2150 return ret; 2151 } 2152 2153 static int cma_listen_handler(struct rdma_cm_id *id, 2154 struct rdma_cm_event *event) 2155 { 2156 struct rdma_id_private *id_priv = id->context; 2157 2158 id->context = id_priv->id.context; 2159 id->event_handler = id_priv->id.event_handler; 2160 return id_priv->id.event_handler(id, event); 2161 } 2162 2163 static void cma_listen_on_dev(struct rdma_id_private *id_priv, 2164 struct cma_device *cma_dev) 2165 { 2166 struct rdma_id_private *dev_id_priv; 2167 struct rdma_cm_id *id; 2168 struct net *net = id_priv->id.route.addr.dev_addr.net; 2169 int ret; 2170 2171 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1)) 2172 return; 2173 2174 id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps, 2175 id_priv->id.qp_type); 2176 if (IS_ERR(id)) 2177 return; 2178 2179 dev_id_priv = container_of(id, struct rdma_id_private, id); 2180 2181 dev_id_priv->state = RDMA_CM_ADDR_BOUND; 2182 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv), 2183 rdma_addr_size(cma_src_addr(id_priv))); 2184 2185 _cma_attach_to_dev(dev_id_priv, cma_dev); 2186 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list); 2187 atomic_inc(&id_priv->refcount); 2188 dev_id_priv->internal_id = 1; 2189 dev_id_priv->afonly = id_priv->afonly; 2190 2191 ret = rdma_listen(id, id_priv->backlog); 2192 if (ret) 2193 pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n", 2194 ret, cma_dev->device->name); 2195 } 2196 2197 static void cma_listen_on_all(struct rdma_id_private *id_priv) 2198 { 2199 struct cma_device *cma_dev; 2200 2201 mutex_lock(&lock); 2202 list_add_tail(&id_priv->list, &listen_any_list); 2203 list_for_each_entry(cma_dev, &dev_list, list) 2204 cma_listen_on_dev(id_priv, cma_dev); 2205 mutex_unlock(&lock); 2206 } 2207 2208 void rdma_set_service_type(struct rdma_cm_id *id, int tos) 2209 { 2210 struct rdma_id_private *id_priv; 2211 2212 id_priv = container_of(id, struct rdma_id_private, id); 2213 id_priv->tos = (u8) tos; 2214 } 2215 EXPORT_SYMBOL(rdma_set_service_type); 2216 2217 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec, 2218 void *context) 2219 { 2220 struct cma_work *work = context; 2221 struct rdma_route *route; 2222 2223 route = &work->id->id.route; 2224 2225 if (!status) { 2226 route->num_paths = 1; 2227 *route->path_rec = *path_rec; 2228 } else { 2229 work->old_state = RDMA_CM_ROUTE_QUERY; 2230 work->new_state = RDMA_CM_ADDR_RESOLVED; 2231 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR; 2232 work->event.status = status; 2233 } 2234 2235 queue_work(cma_wq, &work->work); 2236 } 2237 2238 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms, 2239 struct cma_work *work) 2240 { 2241 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 2242 struct ib_sa_path_rec path_rec; 2243 ib_sa_comp_mask comp_mask; 2244 struct sockaddr_in6 *sin6; 2245 struct sockaddr_ib *sib; 2246 2247 memset(&path_rec, 0, sizeof path_rec); 2248 rdma_addr_get_sgid(dev_addr, &path_rec.sgid); 2249 rdma_addr_get_dgid(dev_addr, &path_rec.dgid); 2250 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); 2251 path_rec.numb_path = 1; 2252 path_rec.reversible = 1; 2253 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv)); 2254 2255 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID | 2256 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH | 2257 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID; 2258 2259 switch (cma_family(id_priv)) { 2260 case AF_INET: 2261 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos); 2262 comp_mask |= IB_SA_PATH_REC_QOS_CLASS; 2263 break; 2264 case AF_INET6: 2265 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv); 2266 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20); 2267 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS; 2268 break; 2269 case AF_IB: 2270 sib = (struct sockaddr_ib *) cma_src_addr(id_priv); 2271 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20); 2272 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS; 2273 break; 2274 } 2275 2276 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device, 2277 id_priv->id.port_num, &path_rec, 2278 comp_mask, timeout_ms, 2279 GFP_KERNEL, cma_query_handler, 2280 work, &id_priv->query); 2281 2282 return (id_priv->query_id < 0) ? id_priv->query_id : 0; 2283 } 2284 2285 static void cma_work_handler(struct work_struct *_work) 2286 { 2287 struct cma_work *work = container_of(_work, struct cma_work, work); 2288 struct rdma_id_private *id_priv = work->id; 2289 int destroy = 0; 2290 2291 mutex_lock(&id_priv->handler_mutex); 2292 if (!cma_comp_exch(id_priv, work->old_state, work->new_state)) 2293 goto out; 2294 2295 if (id_priv->id.event_handler(&id_priv->id, &work->event)) { 2296 cma_exch(id_priv, RDMA_CM_DESTROYING); 2297 destroy = 1; 2298 } 2299 out: 2300 mutex_unlock(&id_priv->handler_mutex); 2301 cma_deref_id(id_priv); 2302 if (destroy) 2303 rdma_destroy_id(&id_priv->id); 2304 kfree(work); 2305 } 2306 2307 static void cma_ndev_work_handler(struct work_struct *_work) 2308 { 2309 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work); 2310 struct rdma_id_private *id_priv = work->id; 2311 int destroy = 0; 2312 2313 mutex_lock(&id_priv->handler_mutex); 2314 if (id_priv->state == RDMA_CM_DESTROYING || 2315 id_priv->state == RDMA_CM_DEVICE_REMOVAL) 2316 goto out; 2317 2318 if (id_priv->id.event_handler(&id_priv->id, &work->event)) { 2319 cma_exch(id_priv, RDMA_CM_DESTROYING); 2320 destroy = 1; 2321 } 2322 2323 out: 2324 mutex_unlock(&id_priv->handler_mutex); 2325 cma_deref_id(id_priv); 2326 if (destroy) 2327 rdma_destroy_id(&id_priv->id); 2328 kfree(work); 2329 } 2330 2331 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms) 2332 { 2333 struct rdma_route *route = &id_priv->id.route; 2334 struct cma_work *work; 2335 int ret; 2336 2337 work = kzalloc(sizeof *work, GFP_KERNEL); 2338 if (!work) 2339 return -ENOMEM; 2340 2341 work->id = id_priv; 2342 INIT_WORK(&work->work, cma_work_handler); 2343 work->old_state = RDMA_CM_ROUTE_QUERY; 2344 work->new_state = RDMA_CM_ROUTE_RESOLVED; 2345 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; 2346 2347 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL); 2348 if (!route->path_rec) { 2349 ret = -ENOMEM; 2350 goto err1; 2351 } 2352 2353 ret = cma_query_ib_route(id_priv, timeout_ms, work); 2354 if (ret) 2355 goto err2; 2356 2357 return 0; 2358 err2: 2359 kfree(route->path_rec); 2360 route->path_rec = NULL; 2361 err1: 2362 kfree(work); 2363 return ret; 2364 } 2365 2366 int rdma_set_ib_paths(struct rdma_cm_id *id, 2367 struct ib_sa_path_rec *path_rec, int num_paths) 2368 { 2369 struct rdma_id_private *id_priv; 2370 int ret; 2371 2372 id_priv = container_of(id, struct rdma_id_private, id); 2373 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, 2374 RDMA_CM_ROUTE_RESOLVED)) 2375 return -EINVAL; 2376 2377 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths, 2378 GFP_KERNEL); 2379 if (!id->route.path_rec) { 2380 ret = -ENOMEM; 2381 goto err; 2382 } 2383 2384 id->route.num_paths = num_paths; 2385 return 0; 2386 err: 2387 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED); 2388 return ret; 2389 } 2390 EXPORT_SYMBOL(rdma_set_ib_paths); 2391 2392 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms) 2393 { 2394 struct cma_work *work; 2395 2396 work = kzalloc(sizeof *work, GFP_KERNEL); 2397 if (!work) 2398 return -ENOMEM; 2399 2400 work->id = id_priv; 2401 INIT_WORK(&work->work, cma_work_handler); 2402 work->old_state = RDMA_CM_ROUTE_QUERY; 2403 work->new_state = RDMA_CM_ROUTE_RESOLVED; 2404 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; 2405 queue_work(cma_wq, &work->work); 2406 return 0; 2407 } 2408 2409 static int iboe_tos_to_sl(struct net_device *ndev, int tos) 2410 { 2411 int prio; 2412 struct net_device *dev; 2413 2414 prio = rt_tos2priority(tos); 2415 dev = ndev->priv_flags & IFF_802_1Q_VLAN ? 2416 vlan_dev_real_dev(ndev) : ndev; 2417 2418 if (dev->num_tc) 2419 return netdev_get_prio_tc_map(dev, prio); 2420 2421 #if IS_ENABLED(CONFIG_VLAN_8021Q) 2422 if (ndev->priv_flags & IFF_802_1Q_VLAN) 2423 return (vlan_dev_get_egress_qos_mask(ndev, prio) & 2424 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; 2425 #endif 2426 return 0; 2427 } 2428 2429 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv) 2430 { 2431 struct rdma_route *route = &id_priv->id.route; 2432 struct rdma_addr *addr = &route->addr; 2433 struct cma_work *work; 2434 int ret; 2435 struct net_device *ndev = NULL; 2436 2437 2438 work = kzalloc(sizeof *work, GFP_KERNEL); 2439 if (!work) 2440 return -ENOMEM; 2441 2442 work->id = id_priv; 2443 INIT_WORK(&work->work, cma_work_handler); 2444 2445 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL); 2446 if (!route->path_rec) { 2447 ret = -ENOMEM; 2448 goto err1; 2449 } 2450 2451 route->num_paths = 1; 2452 2453 if (addr->dev_addr.bound_dev_if) { 2454 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if); 2455 if (!ndev) 2456 return -ENODEV; 2457 2458 if (ndev->flags & IFF_LOOPBACK) { 2459 dev_put(ndev); 2460 if (!id_priv->id.device->get_netdev) 2461 return -EOPNOTSUPP; 2462 2463 ndev = id_priv->id.device->get_netdev(id_priv->id.device, 2464 id_priv->id.port_num); 2465 if (!ndev) 2466 return -ENODEV; 2467 } 2468 2469 route->path_rec->net = &init_net; 2470 route->path_rec->ifindex = ndev->ifindex; 2471 route->path_rec->gid_type = id_priv->gid_type; 2472 } 2473 if (!ndev) { 2474 ret = -ENODEV; 2475 goto err2; 2476 } 2477 2478 memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN); 2479 2480 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr, 2481 &route->path_rec->sgid); 2482 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr, 2483 &route->path_rec->dgid); 2484 2485 /* Use the hint from IP Stack to select GID Type */ 2486 if (route->path_rec->gid_type < ib_network_to_gid_type(addr->dev_addr.network)) 2487 route->path_rec->gid_type = ib_network_to_gid_type(addr->dev_addr.network); 2488 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB) 2489 /* TODO: get the hoplimit from the inet/inet6 device */ 2490 route->path_rec->hop_limit = addr->dev_addr.hoplimit; 2491 else 2492 route->path_rec->hop_limit = 1; 2493 route->path_rec->reversible = 1; 2494 route->path_rec->pkey = cpu_to_be16(0xffff); 2495 route->path_rec->mtu_selector = IB_SA_EQ; 2496 route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos); 2497 route->path_rec->mtu = iboe_get_mtu(ndev->mtu); 2498 route->path_rec->rate_selector = IB_SA_EQ; 2499 route->path_rec->rate = iboe_get_rate(ndev); 2500 dev_put(ndev); 2501 route->path_rec->packet_life_time_selector = IB_SA_EQ; 2502 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME; 2503 if (!route->path_rec->mtu) { 2504 ret = -EINVAL; 2505 goto err2; 2506 } 2507 2508 work->old_state = RDMA_CM_ROUTE_QUERY; 2509 work->new_state = RDMA_CM_ROUTE_RESOLVED; 2510 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; 2511 work->event.status = 0; 2512 2513 queue_work(cma_wq, &work->work); 2514 2515 return 0; 2516 2517 err2: 2518 kfree(route->path_rec); 2519 route->path_rec = NULL; 2520 err1: 2521 kfree(work); 2522 return ret; 2523 } 2524 2525 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms) 2526 { 2527 struct rdma_id_private *id_priv; 2528 int ret; 2529 2530 id_priv = container_of(id, struct rdma_id_private, id); 2531 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY)) 2532 return -EINVAL; 2533 2534 atomic_inc(&id_priv->refcount); 2535 if (rdma_cap_ib_sa(id->device, id->port_num)) 2536 ret = cma_resolve_ib_route(id_priv, timeout_ms); 2537 else if (rdma_protocol_roce(id->device, id->port_num)) 2538 ret = cma_resolve_iboe_route(id_priv); 2539 else if (rdma_protocol_iwarp(id->device, id->port_num)) 2540 ret = cma_resolve_iw_route(id_priv, timeout_ms); 2541 else 2542 ret = -ENOSYS; 2543 2544 if (ret) 2545 goto err; 2546 2547 return 0; 2548 err: 2549 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED); 2550 cma_deref_id(id_priv); 2551 return ret; 2552 } 2553 EXPORT_SYMBOL(rdma_resolve_route); 2554 2555 static void cma_set_loopback(struct sockaddr *addr) 2556 { 2557 switch (addr->sa_family) { 2558 case AF_INET: 2559 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 2560 break; 2561 case AF_INET6: 2562 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr, 2563 0, 0, 0, htonl(1)); 2564 break; 2565 default: 2566 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr, 2567 0, 0, 0, htonl(1)); 2568 break; 2569 } 2570 } 2571 2572 static int cma_bind_loopback(struct rdma_id_private *id_priv) 2573 { 2574 struct cma_device *cma_dev, *cur_dev; 2575 struct ib_port_attr port_attr; 2576 union ib_gid gid; 2577 u16 pkey; 2578 int ret; 2579 u8 p; 2580 2581 cma_dev = NULL; 2582 mutex_lock(&lock); 2583 list_for_each_entry(cur_dev, &dev_list, list) { 2584 if (cma_family(id_priv) == AF_IB && 2585 !rdma_cap_ib_cm(cur_dev->device, 1)) 2586 continue; 2587 2588 if (!cma_dev) 2589 cma_dev = cur_dev; 2590 2591 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) { 2592 if (!ib_query_port(cur_dev->device, p, &port_attr) && 2593 port_attr.state == IB_PORT_ACTIVE) { 2594 cma_dev = cur_dev; 2595 goto port_found; 2596 } 2597 } 2598 } 2599 2600 if (!cma_dev) { 2601 ret = -ENODEV; 2602 goto out; 2603 } 2604 2605 p = 1; 2606 2607 port_found: 2608 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL); 2609 if (ret) 2610 goto out; 2611 2612 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey); 2613 if (ret) 2614 goto out; 2615 2616 id_priv->id.route.addr.dev_addr.dev_type = 2617 (rdma_protocol_ib(cma_dev->device, p)) ? 2618 ARPHRD_INFINIBAND : ARPHRD_ETHER; 2619 2620 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid); 2621 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey); 2622 id_priv->id.port_num = p; 2623 cma_attach_to_dev(id_priv, cma_dev); 2624 cma_set_loopback(cma_src_addr(id_priv)); 2625 out: 2626 mutex_unlock(&lock); 2627 return ret; 2628 } 2629 2630 static void addr_handler(int status, struct sockaddr *src_addr, 2631 struct rdma_dev_addr *dev_addr, void *context) 2632 { 2633 struct rdma_id_private *id_priv = context; 2634 struct rdma_cm_event event; 2635 2636 memset(&event, 0, sizeof event); 2637 mutex_lock(&id_priv->handler_mutex); 2638 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, 2639 RDMA_CM_ADDR_RESOLVED)) 2640 goto out; 2641 2642 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr)); 2643 if (!status && !id_priv->cma_dev) 2644 status = cma_acquire_dev(id_priv, NULL); 2645 2646 if (status) { 2647 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, 2648 RDMA_CM_ADDR_BOUND)) 2649 goto out; 2650 event.event = RDMA_CM_EVENT_ADDR_ERROR; 2651 event.status = status; 2652 } else 2653 event.event = RDMA_CM_EVENT_ADDR_RESOLVED; 2654 2655 if (id_priv->id.event_handler(&id_priv->id, &event)) { 2656 cma_exch(id_priv, RDMA_CM_DESTROYING); 2657 mutex_unlock(&id_priv->handler_mutex); 2658 cma_deref_id(id_priv); 2659 rdma_destroy_id(&id_priv->id); 2660 return; 2661 } 2662 out: 2663 mutex_unlock(&id_priv->handler_mutex); 2664 cma_deref_id(id_priv); 2665 } 2666 2667 static int cma_resolve_loopback(struct rdma_id_private *id_priv) 2668 { 2669 struct cma_work *work; 2670 union ib_gid gid; 2671 int ret; 2672 2673 work = kzalloc(sizeof *work, GFP_KERNEL); 2674 if (!work) 2675 return -ENOMEM; 2676 2677 if (!id_priv->cma_dev) { 2678 ret = cma_bind_loopback(id_priv); 2679 if (ret) 2680 goto err; 2681 } 2682 2683 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid); 2684 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid); 2685 2686 work->id = id_priv; 2687 INIT_WORK(&work->work, cma_work_handler); 2688 work->old_state = RDMA_CM_ADDR_QUERY; 2689 work->new_state = RDMA_CM_ADDR_RESOLVED; 2690 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED; 2691 queue_work(cma_wq, &work->work); 2692 return 0; 2693 err: 2694 kfree(work); 2695 return ret; 2696 } 2697 2698 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv) 2699 { 2700 struct cma_work *work; 2701 int ret; 2702 2703 work = kzalloc(sizeof *work, GFP_KERNEL); 2704 if (!work) 2705 return -ENOMEM; 2706 2707 if (!id_priv->cma_dev) { 2708 ret = cma_resolve_ib_dev(id_priv); 2709 if (ret) 2710 goto err; 2711 } 2712 2713 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *) 2714 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr)); 2715 2716 work->id = id_priv; 2717 INIT_WORK(&work->work, cma_work_handler); 2718 work->old_state = RDMA_CM_ADDR_QUERY; 2719 work->new_state = RDMA_CM_ADDR_RESOLVED; 2720 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED; 2721 queue_work(cma_wq, &work->work); 2722 return 0; 2723 err: 2724 kfree(work); 2725 return ret; 2726 } 2727 2728 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, 2729 struct sockaddr *dst_addr) 2730 { 2731 if (!src_addr || !src_addr->sa_family) { 2732 src_addr = (struct sockaddr *) &id->route.addr.src_addr; 2733 src_addr->sa_family = dst_addr->sa_family; 2734 if (dst_addr->sa_family == AF_INET6) { 2735 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr; 2736 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr; 2737 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id; 2738 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL) 2739 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id; 2740 } else if (dst_addr->sa_family == AF_IB) { 2741 ((struct sockaddr_ib *) src_addr)->sib_pkey = 2742 ((struct sockaddr_ib *) dst_addr)->sib_pkey; 2743 } 2744 } 2745 return rdma_bind_addr(id, src_addr); 2746 } 2747 2748 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, 2749 struct sockaddr *dst_addr, int timeout_ms) 2750 { 2751 struct rdma_id_private *id_priv; 2752 int ret; 2753 2754 id_priv = container_of(id, struct rdma_id_private, id); 2755 if (id_priv->state == RDMA_CM_IDLE) { 2756 ret = cma_bind_addr(id, src_addr, dst_addr); 2757 if (ret) 2758 return ret; 2759 } 2760 2761 if (cma_family(id_priv) != dst_addr->sa_family) 2762 return -EINVAL; 2763 2764 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) 2765 return -EINVAL; 2766 2767 atomic_inc(&id_priv->refcount); 2768 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr)); 2769 if (cma_any_addr(dst_addr)) { 2770 ret = cma_resolve_loopback(id_priv); 2771 } else { 2772 if (dst_addr->sa_family == AF_IB) { 2773 ret = cma_resolve_ib_addr(id_priv); 2774 } else { 2775 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv), 2776 dst_addr, &id->route.addr.dev_addr, 2777 timeout_ms, addr_handler, id_priv); 2778 } 2779 } 2780 if (ret) 2781 goto err; 2782 2783 return 0; 2784 err: 2785 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND); 2786 cma_deref_id(id_priv); 2787 return ret; 2788 } 2789 EXPORT_SYMBOL(rdma_resolve_addr); 2790 2791 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse) 2792 { 2793 struct rdma_id_private *id_priv; 2794 unsigned long flags; 2795 int ret; 2796 2797 id_priv = container_of(id, struct rdma_id_private, id); 2798 spin_lock_irqsave(&id_priv->lock, flags); 2799 if (reuse || id_priv->state == RDMA_CM_IDLE) { 2800 id_priv->reuseaddr = reuse; 2801 ret = 0; 2802 } else { 2803 ret = -EINVAL; 2804 } 2805 spin_unlock_irqrestore(&id_priv->lock, flags); 2806 return ret; 2807 } 2808 EXPORT_SYMBOL(rdma_set_reuseaddr); 2809 2810 int rdma_set_afonly(struct rdma_cm_id *id, int afonly) 2811 { 2812 struct rdma_id_private *id_priv; 2813 unsigned long flags; 2814 int ret; 2815 2816 id_priv = container_of(id, struct rdma_id_private, id); 2817 spin_lock_irqsave(&id_priv->lock, flags); 2818 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) { 2819 id_priv->options |= (1 << CMA_OPTION_AFONLY); 2820 id_priv->afonly = afonly; 2821 ret = 0; 2822 } else { 2823 ret = -EINVAL; 2824 } 2825 spin_unlock_irqrestore(&id_priv->lock, flags); 2826 return ret; 2827 } 2828 EXPORT_SYMBOL(rdma_set_afonly); 2829 2830 static void cma_bind_port(struct rdma_bind_list *bind_list, 2831 struct rdma_id_private *id_priv) 2832 { 2833 struct sockaddr *addr; 2834 struct sockaddr_ib *sib; 2835 u64 sid, mask; 2836 __be16 port; 2837 2838 addr = cma_src_addr(id_priv); 2839 port = htons(bind_list->port); 2840 2841 switch (addr->sa_family) { 2842 case AF_INET: 2843 ((struct sockaddr_in *) addr)->sin_port = port; 2844 break; 2845 case AF_INET6: 2846 ((struct sockaddr_in6 *) addr)->sin6_port = port; 2847 break; 2848 case AF_IB: 2849 sib = (struct sockaddr_ib *) addr; 2850 sid = be64_to_cpu(sib->sib_sid); 2851 mask = be64_to_cpu(sib->sib_sid_mask); 2852 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port)); 2853 sib->sib_sid_mask = cpu_to_be64(~0ULL); 2854 break; 2855 } 2856 id_priv->bind_list = bind_list; 2857 hlist_add_head(&id_priv->node, &bind_list->owners); 2858 } 2859 2860 static int cma_alloc_port(enum rdma_port_space ps, 2861 struct rdma_id_private *id_priv, unsigned short snum) 2862 { 2863 struct rdma_bind_list *bind_list; 2864 int ret; 2865 2866 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL); 2867 if (!bind_list) 2868 return -ENOMEM; 2869 2870 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list, 2871 snum); 2872 if (ret < 0) 2873 goto err; 2874 2875 bind_list->ps = ps; 2876 bind_list->port = (unsigned short)ret; 2877 cma_bind_port(bind_list, id_priv); 2878 return 0; 2879 err: 2880 kfree(bind_list); 2881 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret; 2882 } 2883 2884 static int cma_alloc_any_port(enum rdma_port_space ps, 2885 struct rdma_id_private *id_priv) 2886 { 2887 static unsigned int last_used_port; 2888 int low, high, remaining; 2889 unsigned int rover; 2890 struct net *net = id_priv->id.route.addr.dev_addr.net; 2891 2892 inet_get_local_port_range(net, &low, &high); 2893 remaining = (high - low) + 1; 2894 rover = prandom_u32() % remaining + low; 2895 retry: 2896 if (last_used_port != rover && 2897 !cma_ps_find(net, ps, (unsigned short)rover)) { 2898 int ret = cma_alloc_port(ps, id_priv, rover); 2899 /* 2900 * Remember previously used port number in order to avoid 2901 * re-using same port immediately after it is closed. 2902 */ 2903 if (!ret) 2904 last_used_port = rover; 2905 if (ret != -EADDRNOTAVAIL) 2906 return ret; 2907 } 2908 if (--remaining) { 2909 rover++; 2910 if ((rover < low) || (rover > high)) 2911 rover = low; 2912 goto retry; 2913 } 2914 return -EADDRNOTAVAIL; 2915 } 2916 2917 /* 2918 * Check that the requested port is available. This is called when trying to 2919 * bind to a specific port, or when trying to listen on a bound port. In 2920 * the latter case, the provided id_priv may already be on the bind_list, but 2921 * we still need to check that it's okay to start listening. 2922 */ 2923 static int cma_check_port(struct rdma_bind_list *bind_list, 2924 struct rdma_id_private *id_priv, uint8_t reuseaddr) 2925 { 2926 struct rdma_id_private *cur_id; 2927 struct sockaddr *addr, *cur_addr; 2928 2929 addr = cma_src_addr(id_priv); 2930 hlist_for_each_entry(cur_id, &bind_list->owners, node) { 2931 if (id_priv == cur_id) 2932 continue; 2933 2934 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr && 2935 cur_id->reuseaddr) 2936 continue; 2937 2938 cur_addr = cma_src_addr(cur_id); 2939 if (id_priv->afonly && cur_id->afonly && 2940 (addr->sa_family != cur_addr->sa_family)) 2941 continue; 2942 2943 if (cma_any_addr(addr) || cma_any_addr(cur_addr)) 2944 return -EADDRNOTAVAIL; 2945 2946 if (!cma_addr_cmp(addr, cur_addr)) 2947 return -EADDRINUSE; 2948 } 2949 return 0; 2950 } 2951 2952 static int cma_use_port(enum rdma_port_space ps, 2953 struct rdma_id_private *id_priv) 2954 { 2955 struct rdma_bind_list *bind_list; 2956 unsigned short snum; 2957 int ret; 2958 2959 snum = ntohs(cma_port(cma_src_addr(id_priv))); 2960 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) 2961 return -EACCES; 2962 2963 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum); 2964 if (!bind_list) { 2965 ret = cma_alloc_port(ps, id_priv, snum); 2966 } else { 2967 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr); 2968 if (!ret) 2969 cma_bind_port(bind_list, id_priv); 2970 } 2971 return ret; 2972 } 2973 2974 static int cma_bind_listen(struct rdma_id_private *id_priv) 2975 { 2976 struct rdma_bind_list *bind_list = id_priv->bind_list; 2977 int ret = 0; 2978 2979 mutex_lock(&lock); 2980 if (bind_list->owners.first->next) 2981 ret = cma_check_port(bind_list, id_priv, 0); 2982 mutex_unlock(&lock); 2983 return ret; 2984 } 2985 2986 static enum rdma_port_space cma_select_inet_ps( 2987 struct rdma_id_private *id_priv) 2988 { 2989 switch (id_priv->id.ps) { 2990 case RDMA_PS_TCP: 2991 case RDMA_PS_UDP: 2992 case RDMA_PS_IPOIB: 2993 case RDMA_PS_IB: 2994 return id_priv->id.ps; 2995 default: 2996 2997 return 0; 2998 } 2999 } 3000 3001 static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv) 3002 { 3003 enum rdma_port_space ps = 0; 3004 struct sockaddr_ib *sib; 3005 u64 sid_ps, mask, sid; 3006 3007 sib = (struct sockaddr_ib *) cma_src_addr(id_priv); 3008 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK; 3009 sid = be64_to_cpu(sib->sib_sid) & mask; 3010 3011 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) { 3012 sid_ps = RDMA_IB_IP_PS_IB; 3013 ps = RDMA_PS_IB; 3014 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) && 3015 (sid == (RDMA_IB_IP_PS_TCP & mask))) { 3016 sid_ps = RDMA_IB_IP_PS_TCP; 3017 ps = RDMA_PS_TCP; 3018 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) && 3019 (sid == (RDMA_IB_IP_PS_UDP & mask))) { 3020 sid_ps = RDMA_IB_IP_PS_UDP; 3021 ps = RDMA_PS_UDP; 3022 } 3023 3024 if (ps) { 3025 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib))); 3026 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK | 3027 be64_to_cpu(sib->sib_sid_mask)); 3028 } 3029 return ps; 3030 } 3031 3032 static int cma_get_port(struct rdma_id_private *id_priv) 3033 { 3034 enum rdma_port_space ps; 3035 int ret; 3036 3037 if (cma_family(id_priv) != AF_IB) 3038 ps = cma_select_inet_ps(id_priv); 3039 else 3040 ps = cma_select_ib_ps(id_priv); 3041 if (!ps) 3042 return -EPROTONOSUPPORT; 3043 3044 mutex_lock(&lock); 3045 if (cma_any_port(cma_src_addr(id_priv))) 3046 ret = cma_alloc_any_port(ps, id_priv); 3047 else 3048 ret = cma_use_port(ps, id_priv); 3049 mutex_unlock(&lock); 3050 3051 return ret; 3052 } 3053 3054 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr, 3055 struct sockaddr *addr) 3056 { 3057 #if IS_ENABLED(CONFIG_IPV6) 3058 struct sockaddr_in6 *sin6; 3059 3060 if (addr->sa_family != AF_INET6) 3061 return 0; 3062 3063 sin6 = (struct sockaddr_in6 *) addr; 3064 3065 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)) 3066 return 0; 3067 3068 if (!sin6->sin6_scope_id) 3069 return -EINVAL; 3070 3071 dev_addr->bound_dev_if = sin6->sin6_scope_id; 3072 #endif 3073 return 0; 3074 } 3075 3076 int rdma_listen(struct rdma_cm_id *id, int backlog) 3077 { 3078 struct rdma_id_private *id_priv; 3079 int ret; 3080 3081 id_priv = container_of(id, struct rdma_id_private, id); 3082 if (id_priv->state == RDMA_CM_IDLE) { 3083 id->route.addr.src_addr.ss_family = AF_INET; 3084 ret = rdma_bind_addr(id, cma_src_addr(id_priv)); 3085 if (ret) 3086 return ret; 3087 } 3088 3089 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) 3090 return -EINVAL; 3091 3092 if (id_priv->reuseaddr) { 3093 ret = cma_bind_listen(id_priv); 3094 if (ret) 3095 goto err; 3096 } 3097 3098 id_priv->backlog = backlog; 3099 if (id->device) { 3100 if (rdma_cap_ib_cm(id->device, 1)) { 3101 ret = cma_ib_listen(id_priv); 3102 if (ret) 3103 goto err; 3104 } else if (rdma_cap_iw_cm(id->device, 1)) { 3105 ret = cma_iw_listen(id_priv, backlog); 3106 if (ret) 3107 goto err; 3108 } else { 3109 ret = -ENOSYS; 3110 goto err; 3111 } 3112 } else 3113 cma_listen_on_all(id_priv); 3114 3115 return 0; 3116 err: 3117 id_priv->backlog = 0; 3118 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND); 3119 return ret; 3120 } 3121 EXPORT_SYMBOL(rdma_listen); 3122 3123 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr) 3124 { 3125 struct rdma_id_private *id_priv; 3126 int ret; 3127 3128 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 && 3129 addr->sa_family != AF_IB) 3130 return -EAFNOSUPPORT; 3131 3132 id_priv = container_of(id, struct rdma_id_private, id); 3133 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND)) 3134 return -EINVAL; 3135 3136 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr); 3137 if (ret) 3138 goto err1; 3139 3140 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr)); 3141 if (!cma_any_addr(addr)) { 3142 ret = cma_translate_addr(addr, &id->route.addr.dev_addr); 3143 if (ret) 3144 goto err1; 3145 3146 ret = cma_acquire_dev(id_priv, NULL); 3147 if (ret) 3148 goto err1; 3149 } 3150 3151 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) { 3152 if (addr->sa_family == AF_INET) 3153 id_priv->afonly = 1; 3154 #if IS_ENABLED(CONFIG_IPV6) 3155 else if (addr->sa_family == AF_INET6) { 3156 struct net *net = id_priv->id.route.addr.dev_addr.net; 3157 3158 id_priv->afonly = net->ipv6.sysctl.bindv6only; 3159 } 3160 #endif 3161 } 3162 ret = cma_get_port(id_priv); 3163 if (ret) 3164 goto err2; 3165 3166 return 0; 3167 err2: 3168 if (id_priv->cma_dev) 3169 cma_release_dev(id_priv); 3170 err1: 3171 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE); 3172 return ret; 3173 } 3174 EXPORT_SYMBOL(rdma_bind_addr); 3175 3176 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv) 3177 { 3178 struct cma_hdr *cma_hdr; 3179 3180 cma_hdr = hdr; 3181 cma_hdr->cma_version = CMA_VERSION; 3182 if (cma_family(id_priv) == AF_INET) { 3183 struct sockaddr_in *src4, *dst4; 3184 3185 src4 = (struct sockaddr_in *) cma_src_addr(id_priv); 3186 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv); 3187 3188 cma_set_ip_ver(cma_hdr, 4); 3189 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; 3190 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; 3191 cma_hdr->port = src4->sin_port; 3192 } else if (cma_family(id_priv) == AF_INET6) { 3193 struct sockaddr_in6 *src6, *dst6; 3194 3195 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv); 3196 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv); 3197 3198 cma_set_ip_ver(cma_hdr, 6); 3199 cma_hdr->src_addr.ip6 = src6->sin6_addr; 3200 cma_hdr->dst_addr.ip6 = dst6->sin6_addr; 3201 cma_hdr->port = src6->sin6_port; 3202 } 3203 return 0; 3204 } 3205 3206 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id, 3207 struct ib_cm_event *ib_event) 3208 { 3209 struct rdma_id_private *id_priv = cm_id->context; 3210 struct rdma_cm_event event; 3211 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd; 3212 int ret = 0; 3213 3214 mutex_lock(&id_priv->handler_mutex); 3215 if (id_priv->state != RDMA_CM_CONNECT) 3216 goto out; 3217 3218 memset(&event, 0, sizeof event); 3219 switch (ib_event->event) { 3220 case IB_CM_SIDR_REQ_ERROR: 3221 event.event = RDMA_CM_EVENT_UNREACHABLE; 3222 event.status = -ETIMEDOUT; 3223 break; 3224 case IB_CM_SIDR_REP_RECEIVED: 3225 event.param.ud.private_data = ib_event->private_data; 3226 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE; 3227 if (rep->status != IB_SIDR_SUCCESS) { 3228 event.event = RDMA_CM_EVENT_UNREACHABLE; 3229 event.status = ib_event->param.sidr_rep_rcvd.status; 3230 break; 3231 } 3232 ret = cma_set_qkey(id_priv, rep->qkey); 3233 if (ret) { 3234 event.event = RDMA_CM_EVENT_ADDR_ERROR; 3235 event.status = ret; 3236 break; 3237 } 3238 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num, 3239 id_priv->id.route.path_rec, 3240 &event.param.ud.ah_attr); 3241 event.param.ud.qp_num = rep->qpn; 3242 event.param.ud.qkey = rep->qkey; 3243 event.event = RDMA_CM_EVENT_ESTABLISHED; 3244 event.status = 0; 3245 break; 3246 default: 3247 pr_err("RDMA CMA: unexpected IB CM event: %d\n", 3248 ib_event->event); 3249 goto out; 3250 } 3251 3252 ret = id_priv->id.event_handler(&id_priv->id, &event); 3253 if (ret) { 3254 /* Destroy the CM ID by returning a non-zero value. */ 3255 id_priv->cm_id.ib = NULL; 3256 cma_exch(id_priv, RDMA_CM_DESTROYING); 3257 mutex_unlock(&id_priv->handler_mutex); 3258 rdma_destroy_id(&id_priv->id); 3259 return ret; 3260 } 3261 out: 3262 mutex_unlock(&id_priv->handler_mutex); 3263 return ret; 3264 } 3265 3266 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv, 3267 struct rdma_conn_param *conn_param) 3268 { 3269 struct ib_cm_sidr_req_param req; 3270 struct ib_cm_id *id; 3271 void *private_data; 3272 int offset, ret; 3273 3274 memset(&req, 0, sizeof req); 3275 offset = cma_user_data_offset(id_priv); 3276 req.private_data_len = offset + conn_param->private_data_len; 3277 if (req.private_data_len < conn_param->private_data_len) 3278 return -EINVAL; 3279 3280 if (req.private_data_len) { 3281 private_data = kzalloc(req.private_data_len, GFP_ATOMIC); 3282 if (!private_data) 3283 return -ENOMEM; 3284 } else { 3285 private_data = NULL; 3286 } 3287 3288 if (conn_param->private_data && conn_param->private_data_len) 3289 memcpy(private_data + offset, conn_param->private_data, 3290 conn_param->private_data_len); 3291 3292 if (private_data) { 3293 ret = cma_format_hdr(private_data, id_priv); 3294 if (ret) 3295 goto out; 3296 req.private_data = private_data; 3297 } 3298 3299 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler, 3300 id_priv); 3301 if (IS_ERR(id)) { 3302 ret = PTR_ERR(id); 3303 goto out; 3304 } 3305 id_priv->cm_id.ib = id; 3306 3307 req.path = id_priv->id.route.path_rec; 3308 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv)); 3309 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8); 3310 req.max_cm_retries = CMA_MAX_CM_RETRIES; 3311 3312 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req); 3313 if (ret) { 3314 ib_destroy_cm_id(id_priv->cm_id.ib); 3315 id_priv->cm_id.ib = NULL; 3316 } 3317 out: 3318 kfree(private_data); 3319 return ret; 3320 } 3321 3322 static int cma_connect_ib(struct rdma_id_private *id_priv, 3323 struct rdma_conn_param *conn_param) 3324 { 3325 struct ib_cm_req_param req; 3326 struct rdma_route *route; 3327 void *private_data; 3328 struct ib_cm_id *id; 3329 int offset, ret; 3330 3331 memset(&req, 0, sizeof req); 3332 offset = cma_user_data_offset(id_priv); 3333 req.private_data_len = offset + conn_param->private_data_len; 3334 if (req.private_data_len < conn_param->private_data_len) 3335 return -EINVAL; 3336 3337 if (req.private_data_len) { 3338 private_data = kzalloc(req.private_data_len, GFP_ATOMIC); 3339 if (!private_data) 3340 return -ENOMEM; 3341 } else { 3342 private_data = NULL; 3343 } 3344 3345 if (conn_param->private_data && conn_param->private_data_len) 3346 memcpy(private_data + offset, conn_param->private_data, 3347 conn_param->private_data_len); 3348 3349 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv); 3350 if (IS_ERR(id)) { 3351 ret = PTR_ERR(id); 3352 goto out; 3353 } 3354 id_priv->cm_id.ib = id; 3355 3356 route = &id_priv->id.route; 3357 if (private_data) { 3358 ret = cma_format_hdr(private_data, id_priv); 3359 if (ret) 3360 goto out; 3361 req.private_data = private_data; 3362 } 3363 3364 req.primary_path = &route->path_rec[0]; 3365 if (route->num_paths == 2) 3366 req.alternate_path = &route->path_rec[1]; 3367 3368 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv)); 3369 req.qp_num = id_priv->qp_num; 3370 req.qp_type = id_priv->id.qp_type; 3371 req.starting_psn = id_priv->seq_num; 3372 req.responder_resources = conn_param->responder_resources; 3373 req.initiator_depth = conn_param->initiator_depth; 3374 req.flow_control = conn_param->flow_control; 3375 req.retry_count = min_t(u8, 7, conn_param->retry_count); 3376 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count); 3377 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; 3378 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; 3379 req.max_cm_retries = CMA_MAX_CM_RETRIES; 3380 req.srq = id_priv->srq ? 1 : 0; 3381 3382 ret = ib_send_cm_req(id_priv->cm_id.ib, &req); 3383 out: 3384 if (ret && !IS_ERR(id)) { 3385 ib_destroy_cm_id(id); 3386 id_priv->cm_id.ib = NULL; 3387 } 3388 3389 kfree(private_data); 3390 return ret; 3391 } 3392 3393 static int cma_connect_iw(struct rdma_id_private *id_priv, 3394 struct rdma_conn_param *conn_param) 3395 { 3396 struct iw_cm_id *cm_id; 3397 int ret; 3398 struct iw_cm_conn_param iw_param; 3399 3400 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv); 3401 if (IS_ERR(cm_id)) 3402 return PTR_ERR(cm_id); 3403 3404 cm_id->tos = id_priv->tos; 3405 id_priv->cm_id.iw = cm_id; 3406 3407 memcpy(&cm_id->local_addr, cma_src_addr(id_priv), 3408 rdma_addr_size(cma_src_addr(id_priv))); 3409 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv), 3410 rdma_addr_size(cma_dst_addr(id_priv))); 3411 3412 ret = cma_modify_qp_rtr(id_priv, conn_param); 3413 if (ret) 3414 goto out; 3415 3416 if (conn_param) { 3417 iw_param.ord = conn_param->initiator_depth; 3418 iw_param.ird = conn_param->responder_resources; 3419 iw_param.private_data = conn_param->private_data; 3420 iw_param.private_data_len = conn_param->private_data_len; 3421 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num; 3422 } else { 3423 memset(&iw_param, 0, sizeof iw_param); 3424 iw_param.qpn = id_priv->qp_num; 3425 } 3426 ret = iw_cm_connect(cm_id, &iw_param); 3427 out: 3428 if (ret) { 3429 iw_destroy_cm_id(cm_id); 3430 id_priv->cm_id.iw = NULL; 3431 } 3432 return ret; 3433 } 3434 3435 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) 3436 { 3437 struct rdma_id_private *id_priv; 3438 int ret; 3439 3440 id_priv = container_of(id, struct rdma_id_private, id); 3441 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT)) 3442 return -EINVAL; 3443 3444 if (!id->qp) { 3445 id_priv->qp_num = conn_param->qp_num; 3446 id_priv->srq = conn_param->srq; 3447 } 3448 3449 if (rdma_cap_ib_cm(id->device, id->port_num)) { 3450 if (id->qp_type == IB_QPT_UD) 3451 ret = cma_resolve_ib_udp(id_priv, conn_param); 3452 else 3453 ret = cma_connect_ib(id_priv, conn_param); 3454 } else if (rdma_cap_iw_cm(id->device, id->port_num)) 3455 ret = cma_connect_iw(id_priv, conn_param); 3456 else 3457 ret = -ENOSYS; 3458 if (ret) 3459 goto err; 3460 3461 return 0; 3462 err: 3463 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED); 3464 return ret; 3465 } 3466 EXPORT_SYMBOL(rdma_connect); 3467 3468 static int cma_accept_ib(struct rdma_id_private *id_priv, 3469 struct rdma_conn_param *conn_param) 3470 { 3471 struct ib_cm_rep_param rep; 3472 int ret; 3473 3474 ret = cma_modify_qp_rtr(id_priv, conn_param); 3475 if (ret) 3476 goto out; 3477 3478 ret = cma_modify_qp_rts(id_priv, conn_param); 3479 if (ret) 3480 goto out; 3481 3482 memset(&rep, 0, sizeof rep); 3483 rep.qp_num = id_priv->qp_num; 3484 rep.starting_psn = id_priv->seq_num; 3485 rep.private_data = conn_param->private_data; 3486 rep.private_data_len = conn_param->private_data_len; 3487 rep.responder_resources = conn_param->responder_resources; 3488 rep.initiator_depth = conn_param->initiator_depth; 3489 rep.failover_accepted = 0; 3490 rep.flow_control = conn_param->flow_control; 3491 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count); 3492 rep.srq = id_priv->srq ? 1 : 0; 3493 3494 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep); 3495 out: 3496 return ret; 3497 } 3498 3499 static int cma_accept_iw(struct rdma_id_private *id_priv, 3500 struct rdma_conn_param *conn_param) 3501 { 3502 struct iw_cm_conn_param iw_param; 3503 int ret; 3504 3505 ret = cma_modify_qp_rtr(id_priv, conn_param); 3506 if (ret) 3507 return ret; 3508 3509 iw_param.ord = conn_param->initiator_depth; 3510 iw_param.ird = conn_param->responder_resources; 3511 iw_param.private_data = conn_param->private_data; 3512 iw_param.private_data_len = conn_param->private_data_len; 3513 if (id_priv->id.qp) { 3514 iw_param.qpn = id_priv->qp_num; 3515 } else 3516 iw_param.qpn = conn_param->qp_num; 3517 3518 return iw_cm_accept(id_priv->cm_id.iw, &iw_param); 3519 } 3520 3521 static int cma_send_sidr_rep(struct rdma_id_private *id_priv, 3522 enum ib_cm_sidr_status status, u32 qkey, 3523 const void *private_data, int private_data_len) 3524 { 3525 struct ib_cm_sidr_rep_param rep; 3526 int ret; 3527 3528 memset(&rep, 0, sizeof rep); 3529 rep.status = status; 3530 if (status == IB_SIDR_SUCCESS) { 3531 ret = cma_set_qkey(id_priv, qkey); 3532 if (ret) 3533 return ret; 3534 rep.qp_num = id_priv->qp_num; 3535 rep.qkey = id_priv->qkey; 3536 } 3537 rep.private_data = private_data; 3538 rep.private_data_len = private_data_len; 3539 3540 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep); 3541 } 3542 3543 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) 3544 { 3545 struct rdma_id_private *id_priv; 3546 int ret; 3547 3548 id_priv = container_of(id, struct rdma_id_private, id); 3549 3550 id_priv->owner = task_pid_nr(current); 3551 3552 if (!cma_comp(id_priv, RDMA_CM_CONNECT)) 3553 return -EINVAL; 3554 3555 if (!id->qp && conn_param) { 3556 id_priv->qp_num = conn_param->qp_num; 3557 id_priv->srq = conn_param->srq; 3558 } 3559 3560 if (rdma_cap_ib_cm(id->device, id->port_num)) { 3561 if (id->qp_type == IB_QPT_UD) { 3562 if (conn_param) 3563 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS, 3564 conn_param->qkey, 3565 conn_param->private_data, 3566 conn_param->private_data_len); 3567 else 3568 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS, 3569 0, NULL, 0); 3570 } else { 3571 if (conn_param) 3572 ret = cma_accept_ib(id_priv, conn_param); 3573 else 3574 ret = cma_rep_recv(id_priv); 3575 } 3576 } else if (rdma_cap_iw_cm(id->device, id->port_num)) 3577 ret = cma_accept_iw(id_priv, conn_param); 3578 else 3579 ret = -ENOSYS; 3580 3581 if (ret) 3582 goto reject; 3583 3584 return 0; 3585 reject: 3586 cma_modify_qp_err(id_priv); 3587 rdma_reject(id, NULL, 0); 3588 return ret; 3589 } 3590 EXPORT_SYMBOL(rdma_accept); 3591 3592 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event) 3593 { 3594 struct rdma_id_private *id_priv; 3595 int ret; 3596 3597 id_priv = container_of(id, struct rdma_id_private, id); 3598 if (!id_priv->cm_id.ib) 3599 return -EINVAL; 3600 3601 switch (id->device->node_type) { 3602 case RDMA_NODE_IB_CA: 3603 ret = ib_cm_notify(id_priv->cm_id.ib, event); 3604 break; 3605 default: 3606 ret = 0; 3607 break; 3608 } 3609 return ret; 3610 } 3611 EXPORT_SYMBOL(rdma_notify); 3612 3613 int rdma_reject(struct rdma_cm_id *id, const void *private_data, 3614 u8 private_data_len) 3615 { 3616 struct rdma_id_private *id_priv; 3617 int ret; 3618 3619 id_priv = container_of(id, struct rdma_id_private, id); 3620 if (!id_priv->cm_id.ib) 3621 return -EINVAL; 3622 3623 if (rdma_cap_ib_cm(id->device, id->port_num)) { 3624 if (id->qp_type == IB_QPT_UD) 3625 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0, 3626 private_data, private_data_len); 3627 else 3628 ret = ib_send_cm_rej(id_priv->cm_id.ib, 3629 IB_CM_REJ_CONSUMER_DEFINED, NULL, 3630 0, private_data, private_data_len); 3631 } else if (rdma_cap_iw_cm(id->device, id->port_num)) { 3632 ret = iw_cm_reject(id_priv->cm_id.iw, 3633 private_data, private_data_len); 3634 } else 3635 ret = -ENOSYS; 3636 3637 return ret; 3638 } 3639 EXPORT_SYMBOL(rdma_reject); 3640 3641 int rdma_disconnect(struct rdma_cm_id *id) 3642 { 3643 struct rdma_id_private *id_priv; 3644 int ret; 3645 3646 id_priv = container_of(id, struct rdma_id_private, id); 3647 if (!id_priv->cm_id.ib) 3648 return -EINVAL; 3649 3650 if (rdma_cap_ib_cm(id->device, id->port_num)) { 3651 ret = cma_modify_qp_err(id_priv); 3652 if (ret) 3653 goto out; 3654 /* Initiate or respond to a disconnect. */ 3655 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) 3656 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0); 3657 } else if (rdma_cap_iw_cm(id->device, id->port_num)) { 3658 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0); 3659 } else 3660 ret = -EINVAL; 3661 3662 out: 3663 return ret; 3664 } 3665 EXPORT_SYMBOL(rdma_disconnect); 3666 3667 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast) 3668 { 3669 struct rdma_id_private *id_priv; 3670 struct cma_multicast *mc = multicast->context; 3671 struct rdma_cm_event event; 3672 int ret = 0; 3673 3674 id_priv = mc->id_priv; 3675 mutex_lock(&id_priv->handler_mutex); 3676 if (id_priv->state != RDMA_CM_ADDR_BOUND && 3677 id_priv->state != RDMA_CM_ADDR_RESOLVED) 3678 goto out; 3679 3680 if (!status) 3681 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey)); 3682 mutex_lock(&id_priv->qp_mutex); 3683 if (!status && id_priv->id.qp) 3684 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid, 3685 be16_to_cpu(multicast->rec.mlid)); 3686 mutex_unlock(&id_priv->qp_mutex); 3687 3688 memset(&event, 0, sizeof event); 3689 event.status = status; 3690 event.param.ud.private_data = mc->context; 3691 if (!status) { 3692 struct rdma_dev_addr *dev_addr = 3693 &id_priv->id.route.addr.dev_addr; 3694 struct net_device *ndev = 3695 dev_get_by_index(&init_net, dev_addr->bound_dev_if); 3696 enum ib_gid_type gid_type = 3697 id_priv->cma_dev->default_gid_type[id_priv->id.port_num - 3698 rdma_start_port(id_priv->cma_dev->device)]; 3699 3700 event.event = RDMA_CM_EVENT_MULTICAST_JOIN; 3701 ib_init_ah_from_mcmember(id_priv->id.device, 3702 id_priv->id.port_num, &multicast->rec, 3703 ndev, gid_type, 3704 &event.param.ud.ah_attr); 3705 event.param.ud.qp_num = 0xFFFFFF; 3706 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey); 3707 if (ndev) 3708 dev_put(ndev); 3709 } else 3710 event.event = RDMA_CM_EVENT_MULTICAST_ERROR; 3711 3712 ret = id_priv->id.event_handler(&id_priv->id, &event); 3713 if (ret) { 3714 cma_exch(id_priv, RDMA_CM_DESTROYING); 3715 mutex_unlock(&id_priv->handler_mutex); 3716 rdma_destroy_id(&id_priv->id); 3717 return 0; 3718 } 3719 3720 out: 3721 mutex_unlock(&id_priv->handler_mutex); 3722 return 0; 3723 } 3724 3725 static void cma_set_mgid(struct rdma_id_private *id_priv, 3726 struct sockaddr *addr, union ib_gid *mgid) 3727 { 3728 unsigned char mc_map[MAX_ADDR_LEN]; 3729 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 3730 struct sockaddr_in *sin = (struct sockaddr_in *) addr; 3731 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr; 3732 3733 if (cma_any_addr(addr)) { 3734 memset(mgid, 0, sizeof *mgid); 3735 } else if ((addr->sa_family == AF_INET6) && 3736 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) == 3737 0xFF10A01B)) { 3738 /* IPv6 address is an SA assigned MGID. */ 3739 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); 3740 } else if (addr->sa_family == AF_IB) { 3741 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid); 3742 } else if ((addr->sa_family == AF_INET6)) { 3743 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map); 3744 if (id_priv->id.ps == RDMA_PS_UDP) 3745 mc_map[7] = 0x01; /* Use RDMA CM signature */ 3746 *mgid = *(union ib_gid *) (mc_map + 4); 3747 } else { 3748 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map); 3749 if (id_priv->id.ps == RDMA_PS_UDP) 3750 mc_map[7] = 0x01; /* Use RDMA CM signature */ 3751 *mgid = *(union ib_gid *) (mc_map + 4); 3752 } 3753 } 3754 3755 static int cma_join_ib_multicast(struct rdma_id_private *id_priv, 3756 struct cma_multicast *mc) 3757 { 3758 struct ib_sa_mcmember_rec rec; 3759 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 3760 ib_sa_comp_mask comp_mask; 3761 int ret; 3762 3763 ib_addr_get_mgid(dev_addr, &rec.mgid); 3764 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num, 3765 &rec.mgid, &rec); 3766 if (ret) 3767 return ret; 3768 3769 ret = cma_set_qkey(id_priv, 0); 3770 if (ret) 3771 return ret; 3772 3773 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid); 3774 rec.qkey = cpu_to_be32(id_priv->qkey); 3775 rdma_addr_get_sgid(dev_addr, &rec.port_gid); 3776 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); 3777 rec.join_state = 1; 3778 3779 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID | 3780 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE | 3781 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL | 3782 IB_SA_MCMEMBER_REC_FLOW_LABEL | 3783 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS; 3784 3785 if (id_priv->id.ps == RDMA_PS_IPOIB) 3786 comp_mask |= IB_SA_MCMEMBER_REC_RATE | 3787 IB_SA_MCMEMBER_REC_RATE_SELECTOR | 3788 IB_SA_MCMEMBER_REC_MTU_SELECTOR | 3789 IB_SA_MCMEMBER_REC_MTU | 3790 IB_SA_MCMEMBER_REC_HOP_LIMIT; 3791 3792 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device, 3793 id_priv->id.port_num, &rec, 3794 comp_mask, GFP_KERNEL, 3795 cma_ib_mc_handler, mc); 3796 return PTR_ERR_OR_ZERO(mc->multicast.ib); 3797 } 3798 3799 static void iboe_mcast_work_handler(struct work_struct *work) 3800 { 3801 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work); 3802 struct cma_multicast *mc = mw->mc; 3803 struct ib_sa_multicast *m = mc->multicast.ib; 3804 3805 mc->multicast.ib->context = mc; 3806 cma_ib_mc_handler(0, m); 3807 kref_put(&mc->mcref, release_mc); 3808 kfree(mw); 3809 } 3810 3811 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid) 3812 { 3813 struct sockaddr_in *sin = (struct sockaddr_in *)addr; 3814 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; 3815 3816 if (cma_any_addr(addr)) { 3817 memset(mgid, 0, sizeof *mgid); 3818 } else if (addr->sa_family == AF_INET6) { 3819 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); 3820 } else { 3821 mgid->raw[0] = 0xff; 3822 mgid->raw[1] = 0x0e; 3823 mgid->raw[2] = 0; 3824 mgid->raw[3] = 0; 3825 mgid->raw[4] = 0; 3826 mgid->raw[5] = 0; 3827 mgid->raw[6] = 0; 3828 mgid->raw[7] = 0; 3829 mgid->raw[8] = 0; 3830 mgid->raw[9] = 0; 3831 mgid->raw[10] = 0xff; 3832 mgid->raw[11] = 0xff; 3833 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr; 3834 } 3835 } 3836 3837 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv, 3838 struct cma_multicast *mc) 3839 { 3840 struct iboe_mcast_work *work; 3841 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 3842 int err = 0; 3843 struct sockaddr *addr = (struct sockaddr *)&mc->addr; 3844 struct net_device *ndev = NULL; 3845 enum ib_gid_type gid_type; 3846 3847 if (cma_zero_addr((struct sockaddr *)&mc->addr)) 3848 return -EINVAL; 3849 3850 work = kzalloc(sizeof *work, GFP_KERNEL); 3851 if (!work) 3852 return -ENOMEM; 3853 3854 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL); 3855 if (!mc->multicast.ib) { 3856 err = -ENOMEM; 3857 goto out1; 3858 } 3859 3860 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid); 3861 3862 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff); 3863 if (id_priv->id.ps == RDMA_PS_UDP) 3864 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY); 3865 3866 if (dev_addr->bound_dev_if) 3867 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if); 3868 if (!ndev) { 3869 err = -ENODEV; 3870 goto out2; 3871 } 3872 mc->multicast.ib->rec.rate = iboe_get_rate(ndev); 3873 mc->multicast.ib->rec.hop_limit = 1; 3874 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu); 3875 3876 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num - 3877 rdma_start_port(id_priv->cma_dev->device)]; 3878 if (addr->sa_family == AF_INET) { 3879 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) { 3880 mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT; 3881 err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid, 3882 true); 3883 if (!err) 3884 mc->igmp_joined = true; 3885 } 3886 } else { 3887 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) 3888 err = -ENOTSUPP; 3889 } 3890 dev_put(ndev); 3891 if (err || !mc->multicast.ib->rec.mtu) { 3892 if (!err) 3893 err = -EINVAL; 3894 goto out2; 3895 } 3896 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr, 3897 &mc->multicast.ib->rec.port_gid); 3898 work->id = id_priv; 3899 work->mc = mc; 3900 INIT_WORK(&work->work, iboe_mcast_work_handler); 3901 kref_get(&mc->mcref); 3902 queue_work(cma_wq, &work->work); 3903 3904 return 0; 3905 3906 out2: 3907 kfree(mc->multicast.ib); 3908 out1: 3909 kfree(work); 3910 return err; 3911 } 3912 3913 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr, 3914 void *context) 3915 { 3916 struct rdma_id_private *id_priv; 3917 struct cma_multicast *mc; 3918 int ret; 3919 3920 id_priv = container_of(id, struct rdma_id_private, id); 3921 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) && 3922 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED)) 3923 return -EINVAL; 3924 3925 mc = kmalloc(sizeof *mc, GFP_KERNEL); 3926 if (!mc) 3927 return -ENOMEM; 3928 3929 memcpy(&mc->addr, addr, rdma_addr_size(addr)); 3930 mc->context = context; 3931 mc->id_priv = id_priv; 3932 mc->igmp_joined = false; 3933 spin_lock(&id_priv->lock); 3934 list_add(&mc->list, &id_priv->mc_list); 3935 spin_unlock(&id_priv->lock); 3936 3937 if (rdma_protocol_roce(id->device, id->port_num)) { 3938 kref_init(&mc->mcref); 3939 ret = cma_iboe_join_multicast(id_priv, mc); 3940 } else if (rdma_cap_ib_mcast(id->device, id->port_num)) 3941 ret = cma_join_ib_multicast(id_priv, mc); 3942 else 3943 ret = -ENOSYS; 3944 3945 if (ret) { 3946 spin_lock_irq(&id_priv->lock); 3947 list_del(&mc->list); 3948 spin_unlock_irq(&id_priv->lock); 3949 kfree(mc); 3950 } 3951 return ret; 3952 } 3953 EXPORT_SYMBOL(rdma_join_multicast); 3954 3955 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr) 3956 { 3957 struct rdma_id_private *id_priv; 3958 struct cma_multicast *mc; 3959 3960 id_priv = container_of(id, struct rdma_id_private, id); 3961 spin_lock_irq(&id_priv->lock); 3962 list_for_each_entry(mc, &id_priv->mc_list, list) { 3963 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) { 3964 list_del(&mc->list); 3965 spin_unlock_irq(&id_priv->lock); 3966 3967 if (id->qp) 3968 ib_detach_mcast(id->qp, 3969 &mc->multicast.ib->rec.mgid, 3970 be16_to_cpu(mc->multicast.ib->rec.mlid)); 3971 3972 BUG_ON(id_priv->cma_dev->device != id->device); 3973 3974 if (rdma_cap_ib_mcast(id->device, id->port_num)) { 3975 ib_sa_free_multicast(mc->multicast.ib); 3976 kfree(mc); 3977 } else if (rdma_protocol_roce(id->device, id->port_num)) { 3978 if (mc->igmp_joined) { 3979 struct rdma_dev_addr *dev_addr = 3980 &id->route.addr.dev_addr; 3981 struct net_device *ndev = NULL; 3982 3983 if (dev_addr->bound_dev_if) 3984 ndev = dev_get_by_index(&init_net, 3985 dev_addr->bound_dev_if); 3986 if (ndev) { 3987 cma_igmp_send(ndev, 3988 &mc->multicast.ib->rec.mgid, 3989 false); 3990 dev_put(ndev); 3991 } 3992 mc->igmp_joined = false; 3993 } 3994 kref_put(&mc->mcref, release_mc); 3995 } 3996 return; 3997 } 3998 } 3999 spin_unlock_irq(&id_priv->lock); 4000 } 4001 EXPORT_SYMBOL(rdma_leave_multicast); 4002 4003 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv) 4004 { 4005 struct rdma_dev_addr *dev_addr; 4006 struct cma_ndev_work *work; 4007 4008 dev_addr = &id_priv->id.route.addr.dev_addr; 4009 4010 if ((dev_addr->bound_dev_if == ndev->ifindex) && 4011 (net_eq(dev_net(ndev), dev_addr->net)) && 4012 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) { 4013 pr_info("RDMA CM addr change for ndev %s used by id %p\n", 4014 ndev->name, &id_priv->id); 4015 work = kzalloc(sizeof *work, GFP_KERNEL); 4016 if (!work) 4017 return -ENOMEM; 4018 4019 INIT_WORK(&work->work, cma_ndev_work_handler); 4020 work->id = id_priv; 4021 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE; 4022 atomic_inc(&id_priv->refcount); 4023 queue_work(cma_wq, &work->work); 4024 } 4025 4026 return 0; 4027 } 4028 4029 static int cma_netdev_callback(struct notifier_block *self, unsigned long event, 4030 void *ptr) 4031 { 4032 struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 4033 struct cma_device *cma_dev; 4034 struct rdma_id_private *id_priv; 4035 int ret = NOTIFY_DONE; 4036 4037 if (event != NETDEV_BONDING_FAILOVER) 4038 return NOTIFY_DONE; 4039 4040 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING)) 4041 return NOTIFY_DONE; 4042 4043 mutex_lock(&lock); 4044 list_for_each_entry(cma_dev, &dev_list, list) 4045 list_for_each_entry(id_priv, &cma_dev->id_list, list) { 4046 ret = cma_netdev_change(ndev, id_priv); 4047 if (ret) 4048 goto out; 4049 } 4050 4051 out: 4052 mutex_unlock(&lock); 4053 return ret; 4054 } 4055 4056 static struct notifier_block cma_nb = { 4057 .notifier_call = cma_netdev_callback 4058 }; 4059 4060 static void cma_add_one(struct ib_device *device) 4061 { 4062 struct cma_device *cma_dev; 4063 struct rdma_id_private *id_priv; 4064 unsigned int i; 4065 unsigned long supported_gids = 0; 4066 4067 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL); 4068 if (!cma_dev) 4069 return; 4070 4071 cma_dev->device = device; 4072 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt, 4073 sizeof(*cma_dev->default_gid_type), 4074 GFP_KERNEL); 4075 if (!cma_dev->default_gid_type) { 4076 kfree(cma_dev); 4077 return; 4078 } 4079 for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) { 4080 supported_gids = roce_gid_type_mask_support(device, i); 4081 WARN_ON(!supported_gids); 4082 cma_dev->default_gid_type[i - rdma_start_port(device)] = 4083 find_first_bit(&supported_gids, BITS_PER_LONG); 4084 } 4085 4086 init_completion(&cma_dev->comp); 4087 atomic_set(&cma_dev->refcount, 1); 4088 INIT_LIST_HEAD(&cma_dev->id_list); 4089 ib_set_client_data(device, &cma_client, cma_dev); 4090 4091 mutex_lock(&lock); 4092 list_add_tail(&cma_dev->list, &dev_list); 4093 list_for_each_entry(id_priv, &listen_any_list, list) 4094 cma_listen_on_dev(id_priv, cma_dev); 4095 mutex_unlock(&lock); 4096 } 4097 4098 static int cma_remove_id_dev(struct rdma_id_private *id_priv) 4099 { 4100 struct rdma_cm_event event; 4101 enum rdma_cm_state state; 4102 int ret = 0; 4103 4104 /* Record that we want to remove the device */ 4105 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL); 4106 if (state == RDMA_CM_DESTROYING) 4107 return 0; 4108 4109 cma_cancel_operation(id_priv, state); 4110 mutex_lock(&id_priv->handler_mutex); 4111 4112 /* Check for destruction from another callback. */ 4113 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL)) 4114 goto out; 4115 4116 memset(&event, 0, sizeof event); 4117 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL; 4118 ret = id_priv->id.event_handler(&id_priv->id, &event); 4119 out: 4120 mutex_unlock(&id_priv->handler_mutex); 4121 return ret; 4122 } 4123 4124 static void cma_process_remove(struct cma_device *cma_dev) 4125 { 4126 struct rdma_id_private *id_priv; 4127 int ret; 4128 4129 mutex_lock(&lock); 4130 while (!list_empty(&cma_dev->id_list)) { 4131 id_priv = list_entry(cma_dev->id_list.next, 4132 struct rdma_id_private, list); 4133 4134 list_del(&id_priv->listen_list); 4135 list_del_init(&id_priv->list); 4136 atomic_inc(&id_priv->refcount); 4137 mutex_unlock(&lock); 4138 4139 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv); 4140 cma_deref_id(id_priv); 4141 if (ret) 4142 rdma_destroy_id(&id_priv->id); 4143 4144 mutex_lock(&lock); 4145 } 4146 mutex_unlock(&lock); 4147 4148 cma_deref_dev(cma_dev); 4149 wait_for_completion(&cma_dev->comp); 4150 } 4151 4152 static void cma_remove_one(struct ib_device *device, void *client_data) 4153 { 4154 struct cma_device *cma_dev = client_data; 4155 4156 if (!cma_dev) 4157 return; 4158 4159 mutex_lock(&lock); 4160 list_del(&cma_dev->list); 4161 mutex_unlock(&lock); 4162 4163 cma_process_remove(cma_dev); 4164 kfree(cma_dev->default_gid_type); 4165 kfree(cma_dev); 4166 } 4167 4168 static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb) 4169 { 4170 struct nlmsghdr *nlh; 4171 struct rdma_cm_id_stats *id_stats; 4172 struct rdma_id_private *id_priv; 4173 struct rdma_cm_id *id = NULL; 4174 struct cma_device *cma_dev; 4175 int i_dev = 0, i_id = 0; 4176 4177 /* 4178 * We export all of the IDs as a sequence of messages. Each 4179 * ID gets its own netlink message. 4180 */ 4181 mutex_lock(&lock); 4182 4183 list_for_each_entry(cma_dev, &dev_list, list) { 4184 if (i_dev < cb->args[0]) { 4185 i_dev++; 4186 continue; 4187 } 4188 4189 i_id = 0; 4190 list_for_each_entry(id_priv, &cma_dev->id_list, list) { 4191 if (i_id < cb->args[1]) { 4192 i_id++; 4193 continue; 4194 } 4195 4196 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq, 4197 sizeof *id_stats, RDMA_NL_RDMA_CM, 4198 RDMA_NL_RDMA_CM_ID_STATS, 4199 NLM_F_MULTI); 4200 if (!id_stats) 4201 goto out; 4202 4203 memset(id_stats, 0, sizeof *id_stats); 4204 id = &id_priv->id; 4205 id_stats->node_type = id->route.addr.dev_addr.dev_type; 4206 id_stats->port_num = id->port_num; 4207 id_stats->bound_dev_if = 4208 id->route.addr.dev_addr.bound_dev_if; 4209 4210 if (ibnl_put_attr(skb, nlh, 4211 rdma_addr_size(cma_src_addr(id_priv)), 4212 cma_src_addr(id_priv), 4213 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) 4214 goto out; 4215 if (ibnl_put_attr(skb, nlh, 4216 rdma_addr_size(cma_src_addr(id_priv)), 4217 cma_dst_addr(id_priv), 4218 RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) 4219 goto out; 4220 4221 id_stats->pid = id_priv->owner; 4222 id_stats->port_space = id->ps; 4223 id_stats->cm_state = id_priv->state; 4224 id_stats->qp_num = id_priv->qp_num; 4225 id_stats->qp_type = id->qp_type; 4226 4227 i_id++; 4228 } 4229 4230 cb->args[1] = 0; 4231 i_dev++; 4232 } 4233 4234 out: 4235 mutex_unlock(&lock); 4236 cb->args[0] = i_dev; 4237 cb->args[1] = i_id; 4238 4239 return skb->len; 4240 } 4241 4242 static const struct ibnl_client_cbs cma_cb_table[] = { 4243 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats, 4244 .module = THIS_MODULE }, 4245 }; 4246 4247 static int cma_init_net(struct net *net) 4248 { 4249 struct cma_pernet *pernet = cma_pernet(net); 4250 4251 idr_init(&pernet->tcp_ps); 4252 idr_init(&pernet->udp_ps); 4253 idr_init(&pernet->ipoib_ps); 4254 idr_init(&pernet->ib_ps); 4255 4256 return 0; 4257 } 4258 4259 static void cma_exit_net(struct net *net) 4260 { 4261 struct cma_pernet *pernet = cma_pernet(net); 4262 4263 idr_destroy(&pernet->tcp_ps); 4264 idr_destroy(&pernet->udp_ps); 4265 idr_destroy(&pernet->ipoib_ps); 4266 idr_destroy(&pernet->ib_ps); 4267 } 4268 4269 static struct pernet_operations cma_pernet_operations = { 4270 .init = cma_init_net, 4271 .exit = cma_exit_net, 4272 .id = &cma_pernet_id, 4273 .size = sizeof(struct cma_pernet), 4274 }; 4275 4276 static int __init cma_init(void) 4277 { 4278 int ret; 4279 4280 cma_wq = create_singlethread_workqueue("rdma_cm"); 4281 if (!cma_wq) 4282 return -ENOMEM; 4283 4284 ret = register_pernet_subsys(&cma_pernet_operations); 4285 if (ret) 4286 goto err_wq; 4287 4288 ib_sa_register_client(&sa_client); 4289 rdma_addr_register_client(&addr_client); 4290 register_netdevice_notifier(&cma_nb); 4291 4292 ret = ib_register_client(&cma_client); 4293 if (ret) 4294 goto err; 4295 4296 if (ibnl_add_client(RDMA_NL_RDMA_CM, ARRAY_SIZE(cma_cb_table), 4297 cma_cb_table)) 4298 pr_warn("RDMA CMA: failed to add netlink callback\n"); 4299 cma_configfs_init(); 4300 4301 return 0; 4302 4303 err: 4304 unregister_netdevice_notifier(&cma_nb); 4305 rdma_addr_unregister_client(&addr_client); 4306 ib_sa_unregister_client(&sa_client); 4307 err_wq: 4308 destroy_workqueue(cma_wq); 4309 return ret; 4310 } 4311 4312 static void __exit cma_cleanup(void) 4313 { 4314 cma_configfs_exit(); 4315 ibnl_remove_client(RDMA_NL_RDMA_CM); 4316 ib_unregister_client(&cma_client); 4317 unregister_netdevice_notifier(&cma_nb); 4318 rdma_addr_unregister_client(&addr_client); 4319 ib_sa_unregister_client(&sa_client); 4320 unregister_pernet_subsys(&cma_pernet_operations); 4321 destroy_workqueue(cma_wq); 4322 } 4323 4324 module_init(cma_init); 4325 module_exit(cma_cleanup); 4326