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