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