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