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