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