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