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 lockdep_assert_held(&lock); 1628 1629 if (!bind_list) 1630 return ERR_PTR(-EINVAL); 1631 1632 hlist_for_each_entry(id_priv, &bind_list->owners, node) { 1633 if (cma_match_private_data(id_priv, ib_event->private_data)) { 1634 if (id_priv->id.device == cm_id->device && 1635 cma_match_net_dev(&id_priv->id, net_dev, req)) 1636 return id_priv; 1637 list_for_each_entry(id_priv_dev, 1638 &id_priv->listen_list, 1639 listen_list) { 1640 if (id_priv_dev->id.device == cm_id->device && 1641 cma_match_net_dev(&id_priv_dev->id, 1642 net_dev, req)) 1643 return id_priv_dev; 1644 } 1645 } 1646 } 1647 1648 return ERR_PTR(-EINVAL); 1649 } 1650 1651 static struct rdma_id_private * 1652 cma_ib_id_from_event(struct ib_cm_id *cm_id, 1653 const struct ib_cm_event *ib_event, 1654 struct cma_req_info *req, 1655 struct net_device **net_dev) 1656 { 1657 struct rdma_bind_list *bind_list; 1658 struct rdma_id_private *id_priv; 1659 int err; 1660 1661 err = cma_save_req_info(ib_event, req); 1662 if (err) 1663 return ERR_PTR(err); 1664 1665 *net_dev = cma_get_net_dev(ib_event, req); 1666 if (IS_ERR(*net_dev)) { 1667 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) { 1668 /* Assuming the protocol is AF_IB */ 1669 *net_dev = NULL; 1670 } else { 1671 return ERR_CAST(*net_dev); 1672 } 1673 } 1674 1675 mutex_lock(&lock); 1676 /* 1677 * Net namespace might be getting deleted while route lookup, 1678 * cm_id lookup is in progress. Therefore, perform netdevice 1679 * validation, cm_id lookup under rcu lock. 1680 * RCU lock along with netdevice state check, synchronizes with 1681 * netdevice migrating to different net namespace and also avoids 1682 * case where net namespace doesn't get deleted while lookup is in 1683 * progress. 1684 * If the device state is not IFF_UP, its properties such as ifindex 1685 * and nd_net cannot be trusted to remain valid without rcu lock. 1686 * net/core/dev.c change_net_namespace() ensures to synchronize with 1687 * ongoing operations on net device after device is closed using 1688 * synchronize_net(). 1689 */ 1690 rcu_read_lock(); 1691 if (*net_dev) { 1692 /* 1693 * If netdevice is down, it is likely that it is administratively 1694 * down or it might be migrating to different namespace. 1695 * In that case avoid further processing, as the net namespace 1696 * or ifindex may change. 1697 */ 1698 if (((*net_dev)->flags & IFF_UP) == 0) { 1699 id_priv = ERR_PTR(-EHOSTUNREACH); 1700 goto err; 1701 } 1702 1703 if (!validate_net_dev(*net_dev, 1704 (struct sockaddr *)&req->listen_addr_storage, 1705 (struct sockaddr *)&req->src_addr_storage)) { 1706 id_priv = ERR_PTR(-EHOSTUNREACH); 1707 goto err; 1708 } 1709 } 1710 1711 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net, 1712 rdma_ps_from_service_id(req->service_id), 1713 cma_port_from_service_id(req->service_id)); 1714 id_priv = cma_find_listener(bind_list, cm_id, ib_event, req, *net_dev); 1715 err: 1716 rcu_read_unlock(); 1717 mutex_unlock(&lock); 1718 if (IS_ERR(id_priv) && *net_dev) { 1719 dev_put(*net_dev); 1720 *net_dev = NULL; 1721 } 1722 return id_priv; 1723 } 1724 1725 static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv) 1726 { 1727 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr); 1728 } 1729 1730 static void cma_cancel_route(struct rdma_id_private *id_priv) 1731 { 1732 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) { 1733 if (id_priv->query) 1734 ib_sa_cancel_query(id_priv->query_id, id_priv->query); 1735 } 1736 } 1737 1738 static void cma_cancel_listens(struct rdma_id_private *id_priv) 1739 { 1740 struct rdma_id_private *dev_id_priv; 1741 1742 /* 1743 * Remove from listen_any_list to prevent added devices from spawning 1744 * additional listen requests. 1745 */ 1746 mutex_lock(&lock); 1747 list_del(&id_priv->list); 1748 1749 while (!list_empty(&id_priv->listen_list)) { 1750 dev_id_priv = list_entry(id_priv->listen_list.next, 1751 struct rdma_id_private, listen_list); 1752 /* sync with device removal to avoid duplicate destruction */ 1753 list_del_init(&dev_id_priv->list); 1754 list_del(&dev_id_priv->listen_list); 1755 mutex_unlock(&lock); 1756 1757 rdma_destroy_id(&dev_id_priv->id); 1758 mutex_lock(&lock); 1759 } 1760 mutex_unlock(&lock); 1761 } 1762 1763 static void cma_cancel_operation(struct rdma_id_private *id_priv, 1764 enum rdma_cm_state state) 1765 { 1766 switch (state) { 1767 case RDMA_CM_ADDR_QUERY: 1768 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr); 1769 break; 1770 case RDMA_CM_ROUTE_QUERY: 1771 cma_cancel_route(id_priv); 1772 break; 1773 case RDMA_CM_LISTEN: 1774 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev) 1775 cma_cancel_listens(id_priv); 1776 break; 1777 default: 1778 break; 1779 } 1780 } 1781 1782 static void cma_release_port(struct rdma_id_private *id_priv) 1783 { 1784 struct rdma_bind_list *bind_list = id_priv->bind_list; 1785 struct net *net = id_priv->id.route.addr.dev_addr.net; 1786 1787 if (!bind_list) 1788 return; 1789 1790 mutex_lock(&lock); 1791 hlist_del(&id_priv->node); 1792 if (hlist_empty(&bind_list->owners)) { 1793 cma_ps_remove(net, bind_list->ps, bind_list->port); 1794 kfree(bind_list); 1795 } 1796 mutex_unlock(&lock); 1797 } 1798 1799 static void cma_leave_roce_mc_group(struct rdma_id_private *id_priv, 1800 struct cma_multicast *mc) 1801 { 1802 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 1803 struct net_device *ndev = NULL; 1804 1805 if (dev_addr->bound_dev_if) 1806 ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if); 1807 if (ndev) { 1808 cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid, false); 1809 dev_put(ndev); 1810 } 1811 kref_put(&mc->mcref, release_mc); 1812 } 1813 1814 static void cma_leave_mc_groups(struct rdma_id_private *id_priv) 1815 { 1816 struct cma_multicast *mc; 1817 1818 while (!list_empty(&id_priv->mc_list)) { 1819 mc = container_of(id_priv->mc_list.next, 1820 struct cma_multicast, list); 1821 list_del(&mc->list); 1822 if (rdma_cap_ib_mcast(id_priv->cma_dev->device, 1823 id_priv->id.port_num)) { 1824 ib_sa_free_multicast(mc->multicast.ib); 1825 kfree(mc); 1826 } else { 1827 cma_leave_roce_mc_group(id_priv, mc); 1828 } 1829 } 1830 } 1831 1832 void rdma_destroy_id(struct rdma_cm_id *id) 1833 { 1834 struct rdma_id_private *id_priv; 1835 enum rdma_cm_state state; 1836 1837 id_priv = container_of(id, struct rdma_id_private, id); 1838 trace_cm_id_destroy(id_priv); 1839 state = cma_exch(id_priv, RDMA_CM_DESTROYING); 1840 cma_cancel_operation(id_priv, state); 1841 1842 /* 1843 * Wait for any active callback to finish. New callbacks will find 1844 * the id_priv state set to destroying and abort. 1845 */ 1846 mutex_lock(&id_priv->handler_mutex); 1847 mutex_unlock(&id_priv->handler_mutex); 1848 1849 rdma_restrack_del(&id_priv->res); 1850 if (id_priv->cma_dev) { 1851 if (rdma_cap_ib_cm(id_priv->id.device, 1)) { 1852 if (id_priv->cm_id.ib) 1853 ib_destroy_cm_id(id_priv->cm_id.ib); 1854 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) { 1855 if (id_priv->cm_id.iw) 1856 iw_destroy_cm_id(id_priv->cm_id.iw); 1857 } 1858 cma_leave_mc_groups(id_priv); 1859 cma_release_dev(id_priv); 1860 } 1861 1862 cma_release_port(id_priv); 1863 cma_id_put(id_priv); 1864 wait_for_completion(&id_priv->comp); 1865 1866 if (id_priv->internal_id) 1867 cma_id_put(id_priv->id.context); 1868 1869 kfree(id_priv->id.route.path_rec); 1870 1871 if (id_priv->id.route.addr.dev_addr.sgid_attr) 1872 rdma_put_gid_attr(id_priv->id.route.addr.dev_addr.sgid_attr); 1873 1874 put_net(id_priv->id.route.addr.dev_addr.net); 1875 kfree(id_priv); 1876 } 1877 EXPORT_SYMBOL(rdma_destroy_id); 1878 1879 static int cma_rep_recv(struct rdma_id_private *id_priv) 1880 { 1881 int ret; 1882 1883 ret = cma_modify_qp_rtr(id_priv, NULL); 1884 if (ret) 1885 goto reject; 1886 1887 ret = cma_modify_qp_rts(id_priv, NULL); 1888 if (ret) 1889 goto reject; 1890 1891 trace_cm_send_rtu(id_priv); 1892 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0); 1893 if (ret) 1894 goto reject; 1895 1896 return 0; 1897 reject: 1898 pr_debug_ratelimited("RDMA CM: CONNECT_ERROR: failed to handle reply. status %d\n", ret); 1899 cma_modify_qp_err(id_priv); 1900 trace_cm_send_rej(id_priv); 1901 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED, 1902 NULL, 0, NULL, 0); 1903 return ret; 1904 } 1905 1906 static void cma_set_rep_event_data(struct rdma_cm_event *event, 1907 const struct ib_cm_rep_event_param *rep_data, 1908 void *private_data) 1909 { 1910 event->param.conn.private_data = private_data; 1911 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE; 1912 event->param.conn.responder_resources = rep_data->responder_resources; 1913 event->param.conn.initiator_depth = rep_data->initiator_depth; 1914 event->param.conn.flow_control = rep_data->flow_control; 1915 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count; 1916 event->param.conn.srq = rep_data->srq; 1917 event->param.conn.qp_num = rep_data->remote_qpn; 1918 1919 event->ece.vendor_id = rep_data->ece.vendor_id; 1920 event->ece.attr_mod = rep_data->ece.attr_mod; 1921 } 1922 1923 static int cma_cm_event_handler(struct rdma_id_private *id_priv, 1924 struct rdma_cm_event *event) 1925 { 1926 int ret; 1927 1928 trace_cm_event_handler(id_priv, event); 1929 ret = id_priv->id.event_handler(&id_priv->id, event); 1930 trace_cm_event_done(id_priv, event, ret); 1931 return ret; 1932 } 1933 1934 static int cma_ib_handler(struct ib_cm_id *cm_id, 1935 const struct ib_cm_event *ib_event) 1936 { 1937 struct rdma_id_private *id_priv = cm_id->context; 1938 struct rdma_cm_event event = {}; 1939 int ret = 0; 1940 1941 mutex_lock(&id_priv->handler_mutex); 1942 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT && 1943 id_priv->state != RDMA_CM_CONNECT) || 1944 (ib_event->event == IB_CM_TIMEWAIT_EXIT && 1945 id_priv->state != RDMA_CM_DISCONNECT)) 1946 goto out; 1947 1948 switch (ib_event->event) { 1949 case IB_CM_REQ_ERROR: 1950 case IB_CM_REP_ERROR: 1951 event.event = RDMA_CM_EVENT_UNREACHABLE; 1952 event.status = -ETIMEDOUT; 1953 break; 1954 case IB_CM_REP_RECEIVED: 1955 if (cma_comp(id_priv, RDMA_CM_CONNECT) && 1956 (id_priv->id.qp_type != IB_QPT_UD)) { 1957 trace_cm_send_mra(id_priv); 1958 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0); 1959 } 1960 if (id_priv->id.qp) { 1961 event.status = cma_rep_recv(id_priv); 1962 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR : 1963 RDMA_CM_EVENT_ESTABLISHED; 1964 } else { 1965 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE; 1966 } 1967 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd, 1968 ib_event->private_data); 1969 break; 1970 case IB_CM_RTU_RECEIVED: 1971 case IB_CM_USER_ESTABLISHED: 1972 event.event = RDMA_CM_EVENT_ESTABLISHED; 1973 break; 1974 case IB_CM_DREQ_ERROR: 1975 event.status = -ETIMEDOUT; /* fall through */ 1976 case IB_CM_DREQ_RECEIVED: 1977 case IB_CM_DREP_RECEIVED: 1978 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT, 1979 RDMA_CM_DISCONNECT)) 1980 goto out; 1981 event.event = RDMA_CM_EVENT_DISCONNECTED; 1982 break; 1983 case IB_CM_TIMEWAIT_EXIT: 1984 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT; 1985 break; 1986 case IB_CM_MRA_RECEIVED: 1987 /* ignore event */ 1988 goto out; 1989 case IB_CM_REJ_RECEIVED: 1990 pr_debug_ratelimited("RDMA CM: REJECTED: %s\n", rdma_reject_msg(&id_priv->id, 1991 ib_event->param.rej_rcvd.reason)); 1992 cma_modify_qp_err(id_priv); 1993 event.status = ib_event->param.rej_rcvd.reason; 1994 event.event = RDMA_CM_EVENT_REJECTED; 1995 event.param.conn.private_data = ib_event->private_data; 1996 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE; 1997 break; 1998 default: 1999 pr_err("RDMA CMA: unexpected IB CM event: %d\n", 2000 ib_event->event); 2001 goto out; 2002 } 2003 2004 ret = cma_cm_event_handler(id_priv, &event); 2005 if (ret) { 2006 /* Destroy the CM ID by returning a non-zero value. */ 2007 id_priv->cm_id.ib = NULL; 2008 cma_exch(id_priv, RDMA_CM_DESTROYING); 2009 mutex_unlock(&id_priv->handler_mutex); 2010 rdma_destroy_id(&id_priv->id); 2011 return ret; 2012 } 2013 out: 2014 mutex_unlock(&id_priv->handler_mutex); 2015 return ret; 2016 } 2017 2018 static struct rdma_id_private * 2019 cma_ib_new_conn_id(const struct rdma_cm_id *listen_id, 2020 const struct ib_cm_event *ib_event, 2021 struct net_device *net_dev) 2022 { 2023 struct rdma_id_private *listen_id_priv; 2024 struct rdma_id_private *id_priv; 2025 struct rdma_cm_id *id; 2026 struct rdma_route *rt; 2027 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family; 2028 struct sa_path_rec *path = ib_event->param.req_rcvd.primary_path; 2029 const __be64 service_id = 2030 ib_event->param.req_rcvd.primary_path->service_id; 2031 int ret; 2032 2033 listen_id_priv = container_of(listen_id, struct rdma_id_private, id); 2034 id = __rdma_create_id(listen_id->route.addr.dev_addr.net, 2035 listen_id->event_handler, listen_id->context, 2036 listen_id->ps, ib_event->param.req_rcvd.qp_type, 2037 listen_id_priv->res.kern_name); 2038 if (IS_ERR(id)) 2039 return NULL; 2040 2041 id_priv = container_of(id, struct rdma_id_private, id); 2042 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr, 2043 (struct sockaddr *)&id->route.addr.dst_addr, 2044 listen_id, ib_event, ss_family, service_id)) 2045 goto err; 2046 2047 rt = &id->route; 2048 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1; 2049 rt->path_rec = kmalloc_array(rt->num_paths, sizeof(*rt->path_rec), 2050 GFP_KERNEL); 2051 if (!rt->path_rec) 2052 goto err; 2053 2054 rt->path_rec[0] = *path; 2055 if (rt->num_paths == 2) 2056 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path; 2057 2058 if (net_dev) { 2059 rdma_copy_src_l2_addr(&rt->addr.dev_addr, net_dev); 2060 } else { 2061 if (!cma_protocol_roce(listen_id) && 2062 cma_any_addr(cma_src_addr(id_priv))) { 2063 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND; 2064 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid); 2065 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey)); 2066 } else if (!cma_any_addr(cma_src_addr(id_priv))) { 2067 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr); 2068 if (ret) 2069 goto err; 2070 } 2071 } 2072 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid); 2073 2074 id_priv->state = RDMA_CM_CONNECT; 2075 return id_priv; 2076 2077 err: 2078 rdma_destroy_id(id); 2079 return NULL; 2080 } 2081 2082 static struct rdma_id_private * 2083 cma_ib_new_udp_id(const struct rdma_cm_id *listen_id, 2084 const struct ib_cm_event *ib_event, 2085 struct net_device *net_dev) 2086 { 2087 const struct rdma_id_private *listen_id_priv; 2088 struct rdma_id_private *id_priv; 2089 struct rdma_cm_id *id; 2090 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family; 2091 struct net *net = listen_id->route.addr.dev_addr.net; 2092 int ret; 2093 2094 listen_id_priv = container_of(listen_id, struct rdma_id_private, id); 2095 id = __rdma_create_id(net, listen_id->event_handler, listen_id->context, 2096 listen_id->ps, IB_QPT_UD, 2097 listen_id_priv->res.kern_name); 2098 if (IS_ERR(id)) 2099 return NULL; 2100 2101 id_priv = container_of(id, struct rdma_id_private, id); 2102 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr, 2103 (struct sockaddr *)&id->route.addr.dst_addr, 2104 listen_id, ib_event, ss_family, 2105 ib_event->param.sidr_req_rcvd.service_id)) 2106 goto err; 2107 2108 if (net_dev) { 2109 rdma_copy_src_l2_addr(&id->route.addr.dev_addr, net_dev); 2110 } else { 2111 if (!cma_any_addr(cma_src_addr(id_priv))) { 2112 ret = cma_translate_addr(cma_src_addr(id_priv), 2113 &id->route.addr.dev_addr); 2114 if (ret) 2115 goto err; 2116 } 2117 } 2118 2119 id_priv->state = RDMA_CM_CONNECT; 2120 return id_priv; 2121 err: 2122 rdma_destroy_id(id); 2123 return NULL; 2124 } 2125 2126 static void cma_set_req_event_data(struct rdma_cm_event *event, 2127 const struct ib_cm_req_event_param *req_data, 2128 void *private_data, int offset) 2129 { 2130 event->param.conn.private_data = private_data + offset; 2131 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset; 2132 event->param.conn.responder_resources = req_data->responder_resources; 2133 event->param.conn.initiator_depth = req_data->initiator_depth; 2134 event->param.conn.flow_control = req_data->flow_control; 2135 event->param.conn.retry_count = req_data->retry_count; 2136 event->param.conn.rnr_retry_count = req_data->rnr_retry_count; 2137 event->param.conn.srq = req_data->srq; 2138 event->param.conn.qp_num = req_data->remote_qpn; 2139 2140 event->ece.vendor_id = req_data->ece.vendor_id; 2141 event->ece.attr_mod = req_data->ece.attr_mod; 2142 } 2143 2144 static int cma_ib_check_req_qp_type(const struct rdma_cm_id *id, 2145 const struct ib_cm_event *ib_event) 2146 { 2147 return (((ib_event->event == IB_CM_REQ_RECEIVED) && 2148 (ib_event->param.req_rcvd.qp_type == id->qp_type)) || 2149 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) && 2150 (id->qp_type == IB_QPT_UD)) || 2151 (!id->qp_type)); 2152 } 2153 2154 static int cma_ib_req_handler(struct ib_cm_id *cm_id, 2155 const struct ib_cm_event *ib_event) 2156 { 2157 struct rdma_id_private *listen_id, *conn_id = NULL; 2158 struct rdma_cm_event event = {}; 2159 struct cma_req_info req = {}; 2160 struct net_device *net_dev; 2161 u8 offset; 2162 int ret; 2163 2164 listen_id = cma_ib_id_from_event(cm_id, ib_event, &req, &net_dev); 2165 if (IS_ERR(listen_id)) 2166 return PTR_ERR(listen_id); 2167 2168 trace_cm_req_handler(listen_id, ib_event->event); 2169 if (!cma_ib_check_req_qp_type(&listen_id->id, ib_event)) { 2170 ret = -EINVAL; 2171 goto net_dev_put; 2172 } 2173 2174 mutex_lock(&listen_id->handler_mutex); 2175 if (listen_id->state != RDMA_CM_LISTEN) { 2176 ret = -ECONNABORTED; 2177 goto err1; 2178 } 2179 2180 offset = cma_user_data_offset(listen_id); 2181 event.event = RDMA_CM_EVENT_CONNECT_REQUEST; 2182 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) { 2183 conn_id = cma_ib_new_udp_id(&listen_id->id, ib_event, net_dev); 2184 event.param.ud.private_data = ib_event->private_data + offset; 2185 event.param.ud.private_data_len = 2186 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset; 2187 } else { 2188 conn_id = cma_ib_new_conn_id(&listen_id->id, ib_event, net_dev); 2189 cma_set_req_event_data(&event, &ib_event->param.req_rcvd, 2190 ib_event->private_data, offset); 2191 } 2192 if (!conn_id) { 2193 ret = -ENOMEM; 2194 goto err1; 2195 } 2196 2197 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); 2198 ret = cma_ib_acquire_dev(conn_id, listen_id, &req); 2199 if (ret) 2200 goto err2; 2201 2202 conn_id->cm_id.ib = cm_id; 2203 cm_id->context = conn_id; 2204 cm_id->cm_handler = cma_ib_handler; 2205 2206 /* 2207 * Protect against the user destroying conn_id from another thread 2208 * until we're done accessing it. 2209 */ 2210 cma_id_get(conn_id); 2211 ret = cma_cm_event_handler(conn_id, &event); 2212 if (ret) 2213 goto err3; 2214 /* 2215 * Acquire mutex to prevent user executing rdma_destroy_id() 2216 * while we're accessing the cm_id. 2217 */ 2218 mutex_lock(&lock); 2219 if (cma_comp(conn_id, RDMA_CM_CONNECT) && 2220 (conn_id->id.qp_type != IB_QPT_UD)) { 2221 trace_cm_send_mra(cm_id->context); 2222 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0); 2223 } 2224 mutex_unlock(&lock); 2225 mutex_unlock(&conn_id->handler_mutex); 2226 mutex_unlock(&listen_id->handler_mutex); 2227 cma_id_put(conn_id); 2228 if (net_dev) 2229 dev_put(net_dev); 2230 return 0; 2231 2232 err3: 2233 cma_id_put(conn_id); 2234 /* Destroy the CM ID by returning a non-zero value. */ 2235 conn_id->cm_id.ib = NULL; 2236 err2: 2237 cma_exch(conn_id, RDMA_CM_DESTROYING); 2238 mutex_unlock(&conn_id->handler_mutex); 2239 err1: 2240 mutex_unlock(&listen_id->handler_mutex); 2241 if (conn_id) 2242 rdma_destroy_id(&conn_id->id); 2243 2244 net_dev_put: 2245 if (net_dev) 2246 dev_put(net_dev); 2247 2248 return ret; 2249 } 2250 2251 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr) 2252 { 2253 if (addr->sa_family == AF_IB) 2254 return ((struct sockaddr_ib *) addr)->sib_sid; 2255 2256 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr))); 2257 } 2258 EXPORT_SYMBOL(rdma_get_service_id); 2259 2260 void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid, 2261 union ib_gid *dgid) 2262 { 2263 struct rdma_addr *addr = &cm_id->route.addr; 2264 2265 if (!cm_id->device) { 2266 if (sgid) 2267 memset(sgid, 0, sizeof(*sgid)); 2268 if (dgid) 2269 memset(dgid, 0, sizeof(*dgid)); 2270 return; 2271 } 2272 2273 if (rdma_protocol_roce(cm_id->device, cm_id->port_num)) { 2274 if (sgid) 2275 rdma_ip2gid((struct sockaddr *)&addr->src_addr, sgid); 2276 if (dgid) 2277 rdma_ip2gid((struct sockaddr *)&addr->dst_addr, dgid); 2278 } else { 2279 if (sgid) 2280 rdma_addr_get_sgid(&addr->dev_addr, sgid); 2281 if (dgid) 2282 rdma_addr_get_dgid(&addr->dev_addr, dgid); 2283 } 2284 } 2285 EXPORT_SYMBOL(rdma_read_gids); 2286 2287 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event) 2288 { 2289 struct rdma_id_private *id_priv = iw_id->context; 2290 struct rdma_cm_event event = {}; 2291 int ret = 0; 2292 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr; 2293 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr; 2294 2295 mutex_lock(&id_priv->handler_mutex); 2296 if (id_priv->state != RDMA_CM_CONNECT) 2297 goto out; 2298 2299 switch (iw_event->event) { 2300 case IW_CM_EVENT_CLOSE: 2301 event.event = RDMA_CM_EVENT_DISCONNECTED; 2302 break; 2303 case IW_CM_EVENT_CONNECT_REPLY: 2304 memcpy(cma_src_addr(id_priv), laddr, 2305 rdma_addr_size(laddr)); 2306 memcpy(cma_dst_addr(id_priv), raddr, 2307 rdma_addr_size(raddr)); 2308 switch (iw_event->status) { 2309 case 0: 2310 event.event = RDMA_CM_EVENT_ESTABLISHED; 2311 event.param.conn.initiator_depth = iw_event->ird; 2312 event.param.conn.responder_resources = iw_event->ord; 2313 break; 2314 case -ECONNRESET: 2315 case -ECONNREFUSED: 2316 event.event = RDMA_CM_EVENT_REJECTED; 2317 break; 2318 case -ETIMEDOUT: 2319 event.event = RDMA_CM_EVENT_UNREACHABLE; 2320 break; 2321 default: 2322 event.event = RDMA_CM_EVENT_CONNECT_ERROR; 2323 break; 2324 } 2325 break; 2326 case IW_CM_EVENT_ESTABLISHED: 2327 event.event = RDMA_CM_EVENT_ESTABLISHED; 2328 event.param.conn.initiator_depth = iw_event->ird; 2329 event.param.conn.responder_resources = iw_event->ord; 2330 break; 2331 default: 2332 goto out; 2333 } 2334 2335 event.status = iw_event->status; 2336 event.param.conn.private_data = iw_event->private_data; 2337 event.param.conn.private_data_len = iw_event->private_data_len; 2338 ret = cma_cm_event_handler(id_priv, &event); 2339 if (ret) { 2340 /* Destroy the CM ID by returning a non-zero value. */ 2341 id_priv->cm_id.iw = NULL; 2342 cma_exch(id_priv, RDMA_CM_DESTROYING); 2343 mutex_unlock(&id_priv->handler_mutex); 2344 rdma_destroy_id(&id_priv->id); 2345 return ret; 2346 } 2347 2348 out: 2349 mutex_unlock(&id_priv->handler_mutex); 2350 return ret; 2351 } 2352 2353 static int iw_conn_req_handler(struct iw_cm_id *cm_id, 2354 struct iw_cm_event *iw_event) 2355 { 2356 struct rdma_cm_id *new_cm_id; 2357 struct rdma_id_private *listen_id, *conn_id; 2358 struct rdma_cm_event event = {}; 2359 int ret = -ECONNABORTED; 2360 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr; 2361 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr; 2362 2363 event.event = RDMA_CM_EVENT_CONNECT_REQUEST; 2364 event.param.conn.private_data = iw_event->private_data; 2365 event.param.conn.private_data_len = iw_event->private_data_len; 2366 event.param.conn.initiator_depth = iw_event->ird; 2367 event.param.conn.responder_resources = iw_event->ord; 2368 2369 listen_id = cm_id->context; 2370 2371 mutex_lock(&listen_id->handler_mutex); 2372 if (listen_id->state != RDMA_CM_LISTEN) 2373 goto out; 2374 2375 /* Create a new RDMA id for the new IW CM ID */ 2376 new_cm_id = __rdma_create_id(listen_id->id.route.addr.dev_addr.net, 2377 listen_id->id.event_handler, 2378 listen_id->id.context, 2379 RDMA_PS_TCP, IB_QPT_RC, 2380 listen_id->res.kern_name); 2381 if (IS_ERR(new_cm_id)) { 2382 ret = -ENOMEM; 2383 goto out; 2384 } 2385 conn_id = container_of(new_cm_id, struct rdma_id_private, id); 2386 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); 2387 conn_id->state = RDMA_CM_CONNECT; 2388 2389 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr); 2390 if (ret) { 2391 mutex_unlock(&conn_id->handler_mutex); 2392 rdma_destroy_id(new_cm_id); 2393 goto out; 2394 } 2395 2396 ret = cma_iw_acquire_dev(conn_id, listen_id); 2397 if (ret) { 2398 mutex_unlock(&conn_id->handler_mutex); 2399 rdma_destroy_id(new_cm_id); 2400 goto out; 2401 } 2402 2403 conn_id->cm_id.iw = cm_id; 2404 cm_id->context = conn_id; 2405 cm_id->cm_handler = cma_iw_handler; 2406 2407 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr)); 2408 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr)); 2409 2410 /* 2411 * Protect against the user destroying conn_id from another thread 2412 * until we're done accessing it. 2413 */ 2414 cma_id_get(conn_id); 2415 ret = cma_cm_event_handler(conn_id, &event); 2416 if (ret) { 2417 /* User wants to destroy the CM ID */ 2418 conn_id->cm_id.iw = NULL; 2419 cma_exch(conn_id, RDMA_CM_DESTROYING); 2420 mutex_unlock(&conn_id->handler_mutex); 2421 mutex_unlock(&listen_id->handler_mutex); 2422 cma_id_put(conn_id); 2423 rdma_destroy_id(&conn_id->id); 2424 return ret; 2425 } 2426 2427 mutex_unlock(&conn_id->handler_mutex); 2428 cma_id_put(conn_id); 2429 2430 out: 2431 mutex_unlock(&listen_id->handler_mutex); 2432 return ret; 2433 } 2434 2435 static int cma_ib_listen(struct rdma_id_private *id_priv) 2436 { 2437 struct sockaddr *addr; 2438 struct ib_cm_id *id; 2439 __be64 svc_id; 2440 2441 addr = cma_src_addr(id_priv); 2442 svc_id = rdma_get_service_id(&id_priv->id, addr); 2443 id = ib_cm_insert_listen(id_priv->id.device, 2444 cma_ib_req_handler, svc_id); 2445 if (IS_ERR(id)) 2446 return PTR_ERR(id); 2447 id_priv->cm_id.ib = id; 2448 2449 return 0; 2450 } 2451 2452 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog) 2453 { 2454 int ret; 2455 struct iw_cm_id *id; 2456 2457 id = iw_create_cm_id(id_priv->id.device, 2458 iw_conn_req_handler, 2459 id_priv); 2460 if (IS_ERR(id)) 2461 return PTR_ERR(id); 2462 2463 id->tos = id_priv->tos; 2464 id->tos_set = id_priv->tos_set; 2465 id_priv->cm_id.iw = id; 2466 2467 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv), 2468 rdma_addr_size(cma_src_addr(id_priv))); 2469 2470 ret = iw_cm_listen(id_priv->cm_id.iw, backlog); 2471 2472 if (ret) { 2473 iw_destroy_cm_id(id_priv->cm_id.iw); 2474 id_priv->cm_id.iw = NULL; 2475 } 2476 2477 return ret; 2478 } 2479 2480 static int cma_listen_handler(struct rdma_cm_id *id, 2481 struct rdma_cm_event *event) 2482 { 2483 struct rdma_id_private *id_priv = id->context; 2484 2485 id->context = id_priv->id.context; 2486 id->event_handler = id_priv->id.event_handler; 2487 trace_cm_event_handler(id_priv, event); 2488 return id_priv->id.event_handler(id, event); 2489 } 2490 2491 static void cma_listen_on_dev(struct rdma_id_private *id_priv, 2492 struct cma_device *cma_dev) 2493 { 2494 struct rdma_id_private *dev_id_priv; 2495 struct rdma_cm_id *id; 2496 struct net *net = id_priv->id.route.addr.dev_addr.net; 2497 int ret; 2498 2499 lockdep_assert_held(&lock); 2500 2501 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1)) 2502 return; 2503 2504 id = __rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps, 2505 id_priv->id.qp_type, id_priv->res.kern_name); 2506 if (IS_ERR(id)) 2507 return; 2508 2509 dev_id_priv = container_of(id, struct rdma_id_private, id); 2510 2511 dev_id_priv->state = RDMA_CM_ADDR_BOUND; 2512 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv), 2513 rdma_addr_size(cma_src_addr(id_priv))); 2514 2515 _cma_attach_to_dev(dev_id_priv, cma_dev); 2516 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list); 2517 cma_id_get(id_priv); 2518 dev_id_priv->internal_id = 1; 2519 dev_id_priv->afonly = id_priv->afonly; 2520 dev_id_priv->tos_set = id_priv->tos_set; 2521 dev_id_priv->tos = id_priv->tos; 2522 2523 ret = rdma_listen(id, id_priv->backlog); 2524 if (ret) 2525 dev_warn(&cma_dev->device->dev, 2526 "RDMA CMA: cma_listen_on_dev, error %d\n", ret); 2527 } 2528 2529 static void cma_listen_on_all(struct rdma_id_private *id_priv) 2530 { 2531 struct cma_device *cma_dev; 2532 2533 mutex_lock(&lock); 2534 list_add_tail(&id_priv->list, &listen_any_list); 2535 list_for_each_entry(cma_dev, &dev_list, list) 2536 cma_listen_on_dev(id_priv, cma_dev); 2537 mutex_unlock(&lock); 2538 } 2539 2540 void rdma_set_service_type(struct rdma_cm_id *id, int tos) 2541 { 2542 struct rdma_id_private *id_priv; 2543 2544 id_priv = container_of(id, struct rdma_id_private, id); 2545 id_priv->tos = (u8) tos; 2546 id_priv->tos_set = true; 2547 } 2548 EXPORT_SYMBOL(rdma_set_service_type); 2549 2550 /** 2551 * rdma_set_ack_timeout() - Set the ack timeout of QP associated 2552 * with a connection identifier. 2553 * @id: Communication identifier to associated with service type. 2554 * @timeout: Ack timeout to set a QP, expressed as 4.096 * 2^(timeout) usec. 2555 * 2556 * This function should be called before rdma_connect() on active side, 2557 * and on passive side before rdma_accept(). It is applicable to primary 2558 * path only. The timeout will affect the local side of the QP, it is not 2559 * negotiated with remote side and zero disables the timer. In case it is 2560 * set before rdma_resolve_route, the value will also be used to determine 2561 * PacketLifeTime for RoCE. 2562 * 2563 * Return: 0 for success 2564 */ 2565 int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout) 2566 { 2567 struct rdma_id_private *id_priv; 2568 2569 if (id->qp_type != IB_QPT_RC) 2570 return -EINVAL; 2571 2572 id_priv = container_of(id, struct rdma_id_private, id); 2573 id_priv->timeout = timeout; 2574 id_priv->timeout_set = true; 2575 2576 return 0; 2577 } 2578 EXPORT_SYMBOL(rdma_set_ack_timeout); 2579 2580 static void cma_query_handler(int status, struct sa_path_rec *path_rec, 2581 void *context) 2582 { 2583 struct cma_work *work = context; 2584 struct rdma_route *route; 2585 2586 route = &work->id->id.route; 2587 2588 if (!status) { 2589 route->num_paths = 1; 2590 *route->path_rec = *path_rec; 2591 } else { 2592 work->old_state = RDMA_CM_ROUTE_QUERY; 2593 work->new_state = RDMA_CM_ADDR_RESOLVED; 2594 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR; 2595 work->event.status = status; 2596 pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n", 2597 status); 2598 } 2599 2600 queue_work(cma_wq, &work->work); 2601 } 2602 2603 static int cma_query_ib_route(struct rdma_id_private *id_priv, 2604 unsigned long timeout_ms, struct cma_work *work) 2605 { 2606 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 2607 struct sa_path_rec path_rec; 2608 ib_sa_comp_mask comp_mask; 2609 struct sockaddr_in6 *sin6; 2610 struct sockaddr_ib *sib; 2611 2612 memset(&path_rec, 0, sizeof path_rec); 2613 2614 if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num)) 2615 path_rec.rec_type = SA_PATH_REC_TYPE_OPA; 2616 else 2617 path_rec.rec_type = SA_PATH_REC_TYPE_IB; 2618 rdma_addr_get_sgid(dev_addr, &path_rec.sgid); 2619 rdma_addr_get_dgid(dev_addr, &path_rec.dgid); 2620 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); 2621 path_rec.numb_path = 1; 2622 path_rec.reversible = 1; 2623 path_rec.service_id = rdma_get_service_id(&id_priv->id, 2624 cma_dst_addr(id_priv)); 2625 2626 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID | 2627 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH | 2628 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID; 2629 2630 switch (cma_family(id_priv)) { 2631 case AF_INET: 2632 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos); 2633 comp_mask |= IB_SA_PATH_REC_QOS_CLASS; 2634 break; 2635 case AF_INET6: 2636 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv); 2637 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20); 2638 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS; 2639 break; 2640 case AF_IB: 2641 sib = (struct sockaddr_ib *) cma_src_addr(id_priv); 2642 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20); 2643 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS; 2644 break; 2645 } 2646 2647 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device, 2648 id_priv->id.port_num, &path_rec, 2649 comp_mask, timeout_ms, 2650 GFP_KERNEL, cma_query_handler, 2651 work, &id_priv->query); 2652 2653 return (id_priv->query_id < 0) ? id_priv->query_id : 0; 2654 } 2655 2656 static void cma_work_handler(struct work_struct *_work) 2657 { 2658 struct cma_work *work = container_of(_work, struct cma_work, work); 2659 struct rdma_id_private *id_priv = work->id; 2660 int destroy = 0; 2661 2662 mutex_lock(&id_priv->handler_mutex); 2663 if (!cma_comp_exch(id_priv, work->old_state, work->new_state)) 2664 goto out; 2665 2666 if (cma_cm_event_handler(id_priv, &work->event)) { 2667 cma_exch(id_priv, RDMA_CM_DESTROYING); 2668 destroy = 1; 2669 } 2670 out: 2671 mutex_unlock(&id_priv->handler_mutex); 2672 cma_id_put(id_priv); 2673 if (destroy) 2674 rdma_destroy_id(&id_priv->id); 2675 kfree(work); 2676 } 2677 2678 static void cma_ndev_work_handler(struct work_struct *_work) 2679 { 2680 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work); 2681 struct rdma_id_private *id_priv = work->id; 2682 int destroy = 0; 2683 2684 mutex_lock(&id_priv->handler_mutex); 2685 if (id_priv->state == RDMA_CM_DESTROYING || 2686 id_priv->state == RDMA_CM_DEVICE_REMOVAL) 2687 goto out; 2688 2689 if (cma_cm_event_handler(id_priv, &work->event)) { 2690 cma_exch(id_priv, RDMA_CM_DESTROYING); 2691 destroy = 1; 2692 } 2693 2694 out: 2695 mutex_unlock(&id_priv->handler_mutex); 2696 cma_id_put(id_priv); 2697 if (destroy) 2698 rdma_destroy_id(&id_priv->id); 2699 kfree(work); 2700 } 2701 2702 static void cma_init_resolve_route_work(struct cma_work *work, 2703 struct rdma_id_private *id_priv) 2704 { 2705 work->id = id_priv; 2706 INIT_WORK(&work->work, cma_work_handler); 2707 work->old_state = RDMA_CM_ROUTE_QUERY; 2708 work->new_state = RDMA_CM_ROUTE_RESOLVED; 2709 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; 2710 } 2711 2712 static void enqueue_resolve_addr_work(struct cma_work *work, 2713 struct rdma_id_private *id_priv) 2714 { 2715 /* Balances with cma_id_put() in cma_work_handler */ 2716 cma_id_get(id_priv); 2717 2718 work->id = id_priv; 2719 INIT_WORK(&work->work, cma_work_handler); 2720 work->old_state = RDMA_CM_ADDR_QUERY; 2721 work->new_state = RDMA_CM_ADDR_RESOLVED; 2722 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED; 2723 2724 queue_work(cma_wq, &work->work); 2725 } 2726 2727 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, 2728 unsigned long timeout_ms) 2729 { 2730 struct rdma_route *route = &id_priv->id.route; 2731 struct cma_work *work; 2732 int ret; 2733 2734 work = kzalloc(sizeof *work, GFP_KERNEL); 2735 if (!work) 2736 return -ENOMEM; 2737 2738 cma_init_resolve_route_work(work, id_priv); 2739 2740 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL); 2741 if (!route->path_rec) { 2742 ret = -ENOMEM; 2743 goto err1; 2744 } 2745 2746 ret = cma_query_ib_route(id_priv, timeout_ms, work); 2747 if (ret) 2748 goto err2; 2749 2750 return 0; 2751 err2: 2752 kfree(route->path_rec); 2753 route->path_rec = NULL; 2754 err1: 2755 kfree(work); 2756 return ret; 2757 } 2758 2759 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type, 2760 unsigned long supported_gids, 2761 enum ib_gid_type default_gid) 2762 { 2763 if ((network_type == RDMA_NETWORK_IPV4 || 2764 network_type == RDMA_NETWORK_IPV6) && 2765 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids)) 2766 return IB_GID_TYPE_ROCE_UDP_ENCAP; 2767 2768 return default_gid; 2769 } 2770 2771 /* 2772 * cma_iboe_set_path_rec_l2_fields() is helper function which sets 2773 * path record type based on GID type. 2774 * It also sets up other L2 fields which includes destination mac address 2775 * netdev ifindex, of the path record. 2776 * It returns the netdev of the bound interface for this path record entry. 2777 */ 2778 static struct net_device * 2779 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv) 2780 { 2781 struct rdma_route *route = &id_priv->id.route; 2782 enum ib_gid_type gid_type = IB_GID_TYPE_ROCE; 2783 struct rdma_addr *addr = &route->addr; 2784 unsigned long supported_gids; 2785 struct net_device *ndev; 2786 2787 if (!addr->dev_addr.bound_dev_if) 2788 return NULL; 2789 2790 ndev = dev_get_by_index(addr->dev_addr.net, 2791 addr->dev_addr.bound_dev_if); 2792 if (!ndev) 2793 return NULL; 2794 2795 supported_gids = roce_gid_type_mask_support(id_priv->id.device, 2796 id_priv->id.port_num); 2797 gid_type = cma_route_gid_type(addr->dev_addr.network, 2798 supported_gids, 2799 id_priv->gid_type); 2800 /* Use the hint from IP Stack to select GID Type */ 2801 if (gid_type < ib_network_to_gid_type(addr->dev_addr.network)) 2802 gid_type = ib_network_to_gid_type(addr->dev_addr.network); 2803 route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type); 2804 2805 route->path_rec->roce.route_resolved = true; 2806 sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr); 2807 return ndev; 2808 } 2809 2810 int rdma_set_ib_path(struct rdma_cm_id *id, 2811 struct sa_path_rec *path_rec) 2812 { 2813 struct rdma_id_private *id_priv; 2814 struct net_device *ndev; 2815 int ret; 2816 2817 id_priv = container_of(id, struct rdma_id_private, id); 2818 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, 2819 RDMA_CM_ROUTE_RESOLVED)) 2820 return -EINVAL; 2821 2822 id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec), 2823 GFP_KERNEL); 2824 if (!id->route.path_rec) { 2825 ret = -ENOMEM; 2826 goto err; 2827 } 2828 2829 if (rdma_protocol_roce(id->device, id->port_num)) { 2830 ndev = cma_iboe_set_path_rec_l2_fields(id_priv); 2831 if (!ndev) { 2832 ret = -ENODEV; 2833 goto err_free; 2834 } 2835 dev_put(ndev); 2836 } 2837 2838 id->route.num_paths = 1; 2839 return 0; 2840 2841 err_free: 2842 kfree(id->route.path_rec); 2843 id->route.path_rec = NULL; 2844 err: 2845 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED); 2846 return ret; 2847 } 2848 EXPORT_SYMBOL(rdma_set_ib_path); 2849 2850 static int cma_resolve_iw_route(struct rdma_id_private *id_priv) 2851 { 2852 struct cma_work *work; 2853 2854 work = kzalloc(sizeof *work, GFP_KERNEL); 2855 if (!work) 2856 return -ENOMEM; 2857 2858 cma_init_resolve_route_work(work, id_priv); 2859 queue_work(cma_wq, &work->work); 2860 return 0; 2861 } 2862 2863 static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio) 2864 { 2865 struct net_device *dev; 2866 2867 dev = vlan_dev_real_dev(vlan_ndev); 2868 if (dev->num_tc) 2869 return netdev_get_prio_tc_map(dev, prio); 2870 2871 return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) & 2872 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; 2873 } 2874 2875 struct iboe_prio_tc_map { 2876 int input_prio; 2877 int output_tc; 2878 bool found; 2879 }; 2880 2881 static int get_lower_vlan_dev_tc(struct net_device *dev, void *data) 2882 { 2883 struct iboe_prio_tc_map *map = data; 2884 2885 if (is_vlan_dev(dev)) 2886 map->output_tc = get_vlan_ndev_tc(dev, map->input_prio); 2887 else if (dev->num_tc) 2888 map->output_tc = netdev_get_prio_tc_map(dev, map->input_prio); 2889 else 2890 map->output_tc = 0; 2891 /* We are interested only in first level VLAN device, so always 2892 * return 1 to stop iterating over next level devices. 2893 */ 2894 map->found = true; 2895 return 1; 2896 } 2897 2898 static int iboe_tos_to_sl(struct net_device *ndev, int tos) 2899 { 2900 struct iboe_prio_tc_map prio_tc_map = {}; 2901 int prio = rt_tos2priority(tos); 2902 2903 /* If VLAN device, get it directly from the VLAN netdev */ 2904 if (is_vlan_dev(ndev)) 2905 return get_vlan_ndev_tc(ndev, prio); 2906 2907 prio_tc_map.input_prio = prio; 2908 rcu_read_lock(); 2909 netdev_walk_all_lower_dev_rcu(ndev, 2910 get_lower_vlan_dev_tc, 2911 &prio_tc_map); 2912 rcu_read_unlock(); 2913 /* If map is found from lower device, use it; Otherwise 2914 * continue with the current netdevice to get priority to tc map. 2915 */ 2916 if (prio_tc_map.found) 2917 return prio_tc_map.output_tc; 2918 else if (ndev->num_tc) 2919 return netdev_get_prio_tc_map(ndev, prio); 2920 else 2921 return 0; 2922 } 2923 2924 static __be32 cma_get_roce_udp_flow_label(struct rdma_id_private *id_priv) 2925 { 2926 struct sockaddr_in6 *addr6; 2927 u16 dport, sport; 2928 u32 hash, fl; 2929 2930 addr6 = (struct sockaddr_in6 *)cma_src_addr(id_priv); 2931 fl = be32_to_cpu(addr6->sin6_flowinfo) & IB_GRH_FLOWLABEL_MASK; 2932 if ((cma_family(id_priv) != AF_INET6) || !fl) { 2933 dport = be16_to_cpu(cma_port(cma_dst_addr(id_priv))); 2934 sport = be16_to_cpu(cma_port(cma_src_addr(id_priv))); 2935 hash = (u32)sport * 31 + dport; 2936 fl = hash & IB_GRH_FLOWLABEL_MASK; 2937 } 2938 2939 return cpu_to_be32(fl); 2940 } 2941 2942 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv) 2943 { 2944 struct rdma_route *route = &id_priv->id.route; 2945 struct rdma_addr *addr = &route->addr; 2946 struct cma_work *work; 2947 int ret; 2948 struct net_device *ndev; 2949 2950 u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num - 2951 rdma_start_port(id_priv->cma_dev->device)]; 2952 u8 tos = id_priv->tos_set ? id_priv->tos : default_roce_tos; 2953 2954 2955 work = kzalloc(sizeof *work, GFP_KERNEL); 2956 if (!work) 2957 return -ENOMEM; 2958 2959 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL); 2960 if (!route->path_rec) { 2961 ret = -ENOMEM; 2962 goto err1; 2963 } 2964 2965 route->num_paths = 1; 2966 2967 ndev = cma_iboe_set_path_rec_l2_fields(id_priv); 2968 if (!ndev) { 2969 ret = -ENODEV; 2970 goto err2; 2971 } 2972 2973 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr, 2974 &route->path_rec->sgid); 2975 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr, 2976 &route->path_rec->dgid); 2977 2978 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB) 2979 /* TODO: get the hoplimit from the inet/inet6 device */ 2980 route->path_rec->hop_limit = addr->dev_addr.hoplimit; 2981 else 2982 route->path_rec->hop_limit = 1; 2983 route->path_rec->reversible = 1; 2984 route->path_rec->pkey = cpu_to_be16(0xffff); 2985 route->path_rec->mtu_selector = IB_SA_EQ; 2986 route->path_rec->sl = iboe_tos_to_sl(ndev, tos); 2987 route->path_rec->traffic_class = tos; 2988 route->path_rec->mtu = iboe_get_mtu(ndev->mtu); 2989 route->path_rec->rate_selector = IB_SA_EQ; 2990 route->path_rec->rate = iboe_get_rate(ndev); 2991 dev_put(ndev); 2992 route->path_rec->packet_life_time_selector = IB_SA_EQ; 2993 /* In case ACK timeout is set, use this value to calculate 2994 * PacketLifeTime. As per IBTA 12.7.34, 2995 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay). 2996 * Assuming a negligible local ACK delay, we can use 2997 * PacketLifeTime = local ACK timeout/2 2998 * as a reasonable approximation for RoCE networks. 2999 */ 3000 route->path_rec->packet_life_time = id_priv->timeout_set ? 3001 id_priv->timeout - 1 : CMA_IBOE_PACKET_LIFETIME; 3002 3003 if (!route->path_rec->mtu) { 3004 ret = -EINVAL; 3005 goto err2; 3006 } 3007 3008 if (rdma_protocol_roce_udp_encap(id_priv->id.device, 3009 id_priv->id.port_num)) 3010 route->path_rec->flow_label = 3011 cma_get_roce_udp_flow_label(id_priv); 3012 3013 cma_init_resolve_route_work(work, id_priv); 3014 queue_work(cma_wq, &work->work); 3015 3016 return 0; 3017 3018 err2: 3019 kfree(route->path_rec); 3020 route->path_rec = NULL; 3021 route->num_paths = 0; 3022 err1: 3023 kfree(work); 3024 return ret; 3025 } 3026 3027 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms) 3028 { 3029 struct rdma_id_private *id_priv; 3030 int ret; 3031 3032 id_priv = container_of(id, struct rdma_id_private, id); 3033 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY)) 3034 return -EINVAL; 3035 3036 cma_id_get(id_priv); 3037 if (rdma_cap_ib_sa(id->device, id->port_num)) 3038 ret = cma_resolve_ib_route(id_priv, timeout_ms); 3039 else if (rdma_protocol_roce(id->device, id->port_num)) 3040 ret = cma_resolve_iboe_route(id_priv); 3041 else if (rdma_protocol_iwarp(id->device, id->port_num)) 3042 ret = cma_resolve_iw_route(id_priv); 3043 else 3044 ret = -ENOSYS; 3045 3046 if (ret) 3047 goto err; 3048 3049 return 0; 3050 err: 3051 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED); 3052 cma_id_put(id_priv); 3053 return ret; 3054 } 3055 EXPORT_SYMBOL(rdma_resolve_route); 3056 3057 static void cma_set_loopback(struct sockaddr *addr) 3058 { 3059 switch (addr->sa_family) { 3060 case AF_INET: 3061 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK); 3062 break; 3063 case AF_INET6: 3064 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr, 3065 0, 0, 0, htonl(1)); 3066 break; 3067 default: 3068 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr, 3069 0, 0, 0, htonl(1)); 3070 break; 3071 } 3072 } 3073 3074 static int cma_bind_loopback(struct rdma_id_private *id_priv) 3075 { 3076 struct cma_device *cma_dev, *cur_dev; 3077 union ib_gid gid; 3078 enum ib_port_state port_state; 3079 unsigned int p; 3080 u16 pkey; 3081 int ret; 3082 3083 cma_dev = NULL; 3084 mutex_lock(&lock); 3085 list_for_each_entry(cur_dev, &dev_list, list) { 3086 if (cma_family(id_priv) == AF_IB && 3087 !rdma_cap_ib_cm(cur_dev->device, 1)) 3088 continue; 3089 3090 if (!cma_dev) 3091 cma_dev = cur_dev; 3092 3093 rdma_for_each_port (cur_dev->device, p) { 3094 if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) && 3095 port_state == IB_PORT_ACTIVE) { 3096 cma_dev = cur_dev; 3097 goto port_found; 3098 } 3099 } 3100 } 3101 3102 if (!cma_dev) { 3103 ret = -ENODEV; 3104 goto out; 3105 } 3106 3107 p = 1; 3108 3109 port_found: 3110 ret = rdma_query_gid(cma_dev->device, p, 0, &gid); 3111 if (ret) 3112 goto out; 3113 3114 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey); 3115 if (ret) 3116 goto out; 3117 3118 id_priv->id.route.addr.dev_addr.dev_type = 3119 (rdma_protocol_ib(cma_dev->device, p)) ? 3120 ARPHRD_INFINIBAND : ARPHRD_ETHER; 3121 3122 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid); 3123 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey); 3124 id_priv->id.port_num = p; 3125 cma_attach_to_dev(id_priv, cma_dev); 3126 cma_set_loopback(cma_src_addr(id_priv)); 3127 out: 3128 mutex_unlock(&lock); 3129 return ret; 3130 } 3131 3132 static void addr_handler(int status, struct sockaddr *src_addr, 3133 struct rdma_dev_addr *dev_addr, void *context) 3134 { 3135 struct rdma_id_private *id_priv = context; 3136 struct rdma_cm_event event = {}; 3137 struct sockaddr *addr; 3138 struct sockaddr_storage old_addr; 3139 3140 mutex_lock(&id_priv->handler_mutex); 3141 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, 3142 RDMA_CM_ADDR_RESOLVED)) 3143 goto out; 3144 3145 /* 3146 * Store the previous src address, so that if we fail to acquire 3147 * matching rdma device, old address can be restored back, which helps 3148 * to cancel the cma listen operation correctly. 3149 */ 3150 addr = cma_src_addr(id_priv); 3151 memcpy(&old_addr, addr, rdma_addr_size(addr)); 3152 memcpy(addr, src_addr, rdma_addr_size(src_addr)); 3153 if (!status && !id_priv->cma_dev) { 3154 status = cma_acquire_dev_by_src_ip(id_priv); 3155 if (status) 3156 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n", 3157 status); 3158 } else if (status) { 3159 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status); 3160 } 3161 3162 if (status) { 3163 memcpy(addr, &old_addr, 3164 rdma_addr_size((struct sockaddr *)&old_addr)); 3165 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, 3166 RDMA_CM_ADDR_BOUND)) 3167 goto out; 3168 event.event = RDMA_CM_EVENT_ADDR_ERROR; 3169 event.status = status; 3170 } else 3171 event.event = RDMA_CM_EVENT_ADDR_RESOLVED; 3172 3173 if (cma_cm_event_handler(id_priv, &event)) { 3174 cma_exch(id_priv, RDMA_CM_DESTROYING); 3175 mutex_unlock(&id_priv->handler_mutex); 3176 rdma_destroy_id(&id_priv->id); 3177 return; 3178 } 3179 out: 3180 mutex_unlock(&id_priv->handler_mutex); 3181 } 3182 3183 static int cma_resolve_loopback(struct rdma_id_private *id_priv) 3184 { 3185 struct cma_work *work; 3186 union ib_gid gid; 3187 int ret; 3188 3189 work = kzalloc(sizeof *work, GFP_KERNEL); 3190 if (!work) 3191 return -ENOMEM; 3192 3193 if (!id_priv->cma_dev) { 3194 ret = cma_bind_loopback(id_priv); 3195 if (ret) 3196 goto err; 3197 } 3198 3199 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid); 3200 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid); 3201 3202 enqueue_resolve_addr_work(work, id_priv); 3203 return 0; 3204 err: 3205 kfree(work); 3206 return ret; 3207 } 3208 3209 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv) 3210 { 3211 struct cma_work *work; 3212 int ret; 3213 3214 work = kzalloc(sizeof *work, GFP_KERNEL); 3215 if (!work) 3216 return -ENOMEM; 3217 3218 if (!id_priv->cma_dev) { 3219 ret = cma_resolve_ib_dev(id_priv); 3220 if (ret) 3221 goto err; 3222 } 3223 3224 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *) 3225 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr)); 3226 3227 enqueue_resolve_addr_work(work, id_priv); 3228 return 0; 3229 err: 3230 kfree(work); 3231 return ret; 3232 } 3233 3234 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, 3235 const struct sockaddr *dst_addr) 3236 { 3237 if (!src_addr || !src_addr->sa_family) { 3238 src_addr = (struct sockaddr *) &id->route.addr.src_addr; 3239 src_addr->sa_family = dst_addr->sa_family; 3240 if (IS_ENABLED(CONFIG_IPV6) && 3241 dst_addr->sa_family == AF_INET6) { 3242 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr; 3243 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr; 3244 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id; 3245 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL) 3246 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id; 3247 } else if (dst_addr->sa_family == AF_IB) { 3248 ((struct sockaddr_ib *) src_addr)->sib_pkey = 3249 ((struct sockaddr_ib *) dst_addr)->sib_pkey; 3250 } 3251 } 3252 return rdma_bind_addr(id, src_addr); 3253 } 3254 3255 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, 3256 const struct sockaddr *dst_addr, unsigned long timeout_ms) 3257 { 3258 struct rdma_id_private *id_priv; 3259 int ret; 3260 3261 id_priv = container_of(id, struct rdma_id_private, id); 3262 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr)); 3263 if (id_priv->state == RDMA_CM_IDLE) { 3264 ret = cma_bind_addr(id, src_addr, dst_addr); 3265 if (ret) { 3266 memset(cma_dst_addr(id_priv), 0, 3267 rdma_addr_size(dst_addr)); 3268 return ret; 3269 } 3270 } 3271 3272 if (cma_family(id_priv) != dst_addr->sa_family) { 3273 memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr)); 3274 return -EINVAL; 3275 } 3276 3277 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) { 3278 memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr)); 3279 return -EINVAL; 3280 } 3281 3282 if (cma_any_addr(dst_addr)) { 3283 ret = cma_resolve_loopback(id_priv); 3284 } else { 3285 if (dst_addr->sa_family == AF_IB) { 3286 ret = cma_resolve_ib_addr(id_priv); 3287 } else { 3288 ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr, 3289 &id->route.addr.dev_addr, 3290 timeout_ms, addr_handler, 3291 false, id_priv); 3292 } 3293 } 3294 if (ret) 3295 goto err; 3296 3297 return 0; 3298 err: 3299 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND); 3300 return ret; 3301 } 3302 EXPORT_SYMBOL(rdma_resolve_addr); 3303 3304 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse) 3305 { 3306 struct rdma_id_private *id_priv; 3307 unsigned long flags; 3308 int ret; 3309 3310 id_priv = container_of(id, struct rdma_id_private, id); 3311 spin_lock_irqsave(&id_priv->lock, flags); 3312 if (reuse || id_priv->state == RDMA_CM_IDLE) { 3313 id_priv->reuseaddr = reuse; 3314 ret = 0; 3315 } else { 3316 ret = -EINVAL; 3317 } 3318 spin_unlock_irqrestore(&id_priv->lock, flags); 3319 return ret; 3320 } 3321 EXPORT_SYMBOL(rdma_set_reuseaddr); 3322 3323 int rdma_set_afonly(struct rdma_cm_id *id, int afonly) 3324 { 3325 struct rdma_id_private *id_priv; 3326 unsigned long flags; 3327 int ret; 3328 3329 id_priv = container_of(id, struct rdma_id_private, id); 3330 spin_lock_irqsave(&id_priv->lock, flags); 3331 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) { 3332 id_priv->options |= (1 << CMA_OPTION_AFONLY); 3333 id_priv->afonly = afonly; 3334 ret = 0; 3335 } else { 3336 ret = -EINVAL; 3337 } 3338 spin_unlock_irqrestore(&id_priv->lock, flags); 3339 return ret; 3340 } 3341 EXPORT_SYMBOL(rdma_set_afonly); 3342 3343 static void cma_bind_port(struct rdma_bind_list *bind_list, 3344 struct rdma_id_private *id_priv) 3345 { 3346 struct sockaddr *addr; 3347 struct sockaddr_ib *sib; 3348 u64 sid, mask; 3349 __be16 port; 3350 3351 lockdep_assert_held(&lock); 3352 3353 addr = cma_src_addr(id_priv); 3354 port = htons(bind_list->port); 3355 3356 switch (addr->sa_family) { 3357 case AF_INET: 3358 ((struct sockaddr_in *) addr)->sin_port = port; 3359 break; 3360 case AF_INET6: 3361 ((struct sockaddr_in6 *) addr)->sin6_port = port; 3362 break; 3363 case AF_IB: 3364 sib = (struct sockaddr_ib *) addr; 3365 sid = be64_to_cpu(sib->sib_sid); 3366 mask = be64_to_cpu(sib->sib_sid_mask); 3367 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port)); 3368 sib->sib_sid_mask = cpu_to_be64(~0ULL); 3369 break; 3370 } 3371 id_priv->bind_list = bind_list; 3372 hlist_add_head(&id_priv->node, &bind_list->owners); 3373 } 3374 3375 static int cma_alloc_port(enum rdma_ucm_port_space ps, 3376 struct rdma_id_private *id_priv, unsigned short snum) 3377 { 3378 struct rdma_bind_list *bind_list; 3379 int ret; 3380 3381 lockdep_assert_held(&lock); 3382 3383 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL); 3384 if (!bind_list) 3385 return -ENOMEM; 3386 3387 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list, 3388 snum); 3389 if (ret < 0) 3390 goto err; 3391 3392 bind_list->ps = ps; 3393 bind_list->port = snum; 3394 cma_bind_port(bind_list, id_priv); 3395 return 0; 3396 err: 3397 kfree(bind_list); 3398 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret; 3399 } 3400 3401 static int cma_port_is_unique(struct rdma_bind_list *bind_list, 3402 struct rdma_id_private *id_priv) 3403 { 3404 struct rdma_id_private *cur_id; 3405 struct sockaddr *daddr = cma_dst_addr(id_priv); 3406 struct sockaddr *saddr = cma_src_addr(id_priv); 3407 __be16 dport = cma_port(daddr); 3408 3409 lockdep_assert_held(&lock); 3410 3411 hlist_for_each_entry(cur_id, &bind_list->owners, node) { 3412 struct sockaddr *cur_daddr = cma_dst_addr(cur_id); 3413 struct sockaddr *cur_saddr = cma_src_addr(cur_id); 3414 __be16 cur_dport = cma_port(cur_daddr); 3415 3416 if (id_priv == cur_id) 3417 continue; 3418 3419 /* different dest port -> unique */ 3420 if (!cma_any_port(daddr) && 3421 !cma_any_port(cur_daddr) && 3422 (dport != cur_dport)) 3423 continue; 3424 3425 /* different src address -> unique */ 3426 if (!cma_any_addr(saddr) && 3427 !cma_any_addr(cur_saddr) && 3428 cma_addr_cmp(saddr, cur_saddr)) 3429 continue; 3430 3431 /* different dst address -> unique */ 3432 if (!cma_any_addr(daddr) && 3433 !cma_any_addr(cur_daddr) && 3434 cma_addr_cmp(daddr, cur_daddr)) 3435 continue; 3436 3437 return -EADDRNOTAVAIL; 3438 } 3439 return 0; 3440 } 3441 3442 static int cma_alloc_any_port(enum rdma_ucm_port_space ps, 3443 struct rdma_id_private *id_priv) 3444 { 3445 static unsigned int last_used_port; 3446 int low, high, remaining; 3447 unsigned int rover; 3448 struct net *net = id_priv->id.route.addr.dev_addr.net; 3449 3450 lockdep_assert_held(&lock); 3451 3452 inet_get_local_port_range(net, &low, &high); 3453 remaining = (high - low) + 1; 3454 rover = prandom_u32() % remaining + low; 3455 retry: 3456 if (last_used_port != rover) { 3457 struct rdma_bind_list *bind_list; 3458 int ret; 3459 3460 bind_list = cma_ps_find(net, ps, (unsigned short)rover); 3461 3462 if (!bind_list) { 3463 ret = cma_alloc_port(ps, id_priv, rover); 3464 } else { 3465 ret = cma_port_is_unique(bind_list, id_priv); 3466 if (!ret) 3467 cma_bind_port(bind_list, id_priv); 3468 } 3469 /* 3470 * Remember previously used port number in order to avoid 3471 * re-using same port immediately after it is closed. 3472 */ 3473 if (!ret) 3474 last_used_port = rover; 3475 if (ret != -EADDRNOTAVAIL) 3476 return ret; 3477 } 3478 if (--remaining) { 3479 rover++; 3480 if ((rover < low) || (rover > high)) 3481 rover = low; 3482 goto retry; 3483 } 3484 return -EADDRNOTAVAIL; 3485 } 3486 3487 /* 3488 * Check that the requested port is available. This is called when trying to 3489 * bind to a specific port, or when trying to listen on a bound port. In 3490 * the latter case, the provided id_priv may already be on the bind_list, but 3491 * we still need to check that it's okay to start listening. 3492 */ 3493 static int cma_check_port(struct rdma_bind_list *bind_list, 3494 struct rdma_id_private *id_priv, uint8_t reuseaddr) 3495 { 3496 struct rdma_id_private *cur_id; 3497 struct sockaddr *addr, *cur_addr; 3498 3499 lockdep_assert_held(&lock); 3500 3501 addr = cma_src_addr(id_priv); 3502 hlist_for_each_entry(cur_id, &bind_list->owners, node) { 3503 if (id_priv == cur_id) 3504 continue; 3505 3506 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr && 3507 cur_id->reuseaddr) 3508 continue; 3509 3510 cur_addr = cma_src_addr(cur_id); 3511 if (id_priv->afonly && cur_id->afonly && 3512 (addr->sa_family != cur_addr->sa_family)) 3513 continue; 3514 3515 if (cma_any_addr(addr) || cma_any_addr(cur_addr)) 3516 return -EADDRNOTAVAIL; 3517 3518 if (!cma_addr_cmp(addr, cur_addr)) 3519 return -EADDRINUSE; 3520 } 3521 return 0; 3522 } 3523 3524 static int cma_use_port(enum rdma_ucm_port_space ps, 3525 struct rdma_id_private *id_priv) 3526 { 3527 struct rdma_bind_list *bind_list; 3528 unsigned short snum; 3529 int ret; 3530 3531 lockdep_assert_held(&lock); 3532 3533 snum = ntohs(cma_port(cma_src_addr(id_priv))); 3534 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) 3535 return -EACCES; 3536 3537 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum); 3538 if (!bind_list) { 3539 ret = cma_alloc_port(ps, id_priv, snum); 3540 } else { 3541 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr); 3542 if (!ret) 3543 cma_bind_port(bind_list, id_priv); 3544 } 3545 return ret; 3546 } 3547 3548 static int cma_bind_listen(struct rdma_id_private *id_priv) 3549 { 3550 struct rdma_bind_list *bind_list = id_priv->bind_list; 3551 int ret = 0; 3552 3553 mutex_lock(&lock); 3554 if (bind_list->owners.first->next) 3555 ret = cma_check_port(bind_list, id_priv, 0); 3556 mutex_unlock(&lock); 3557 return ret; 3558 } 3559 3560 static enum rdma_ucm_port_space 3561 cma_select_inet_ps(struct rdma_id_private *id_priv) 3562 { 3563 switch (id_priv->id.ps) { 3564 case RDMA_PS_TCP: 3565 case RDMA_PS_UDP: 3566 case RDMA_PS_IPOIB: 3567 case RDMA_PS_IB: 3568 return id_priv->id.ps; 3569 default: 3570 3571 return 0; 3572 } 3573 } 3574 3575 static enum rdma_ucm_port_space 3576 cma_select_ib_ps(struct rdma_id_private *id_priv) 3577 { 3578 enum rdma_ucm_port_space ps = 0; 3579 struct sockaddr_ib *sib; 3580 u64 sid_ps, mask, sid; 3581 3582 sib = (struct sockaddr_ib *) cma_src_addr(id_priv); 3583 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK; 3584 sid = be64_to_cpu(sib->sib_sid) & mask; 3585 3586 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) { 3587 sid_ps = RDMA_IB_IP_PS_IB; 3588 ps = RDMA_PS_IB; 3589 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) && 3590 (sid == (RDMA_IB_IP_PS_TCP & mask))) { 3591 sid_ps = RDMA_IB_IP_PS_TCP; 3592 ps = RDMA_PS_TCP; 3593 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) && 3594 (sid == (RDMA_IB_IP_PS_UDP & mask))) { 3595 sid_ps = RDMA_IB_IP_PS_UDP; 3596 ps = RDMA_PS_UDP; 3597 } 3598 3599 if (ps) { 3600 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib))); 3601 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK | 3602 be64_to_cpu(sib->sib_sid_mask)); 3603 } 3604 return ps; 3605 } 3606 3607 static int cma_get_port(struct rdma_id_private *id_priv) 3608 { 3609 enum rdma_ucm_port_space ps; 3610 int ret; 3611 3612 if (cma_family(id_priv) != AF_IB) 3613 ps = cma_select_inet_ps(id_priv); 3614 else 3615 ps = cma_select_ib_ps(id_priv); 3616 if (!ps) 3617 return -EPROTONOSUPPORT; 3618 3619 mutex_lock(&lock); 3620 if (cma_any_port(cma_src_addr(id_priv))) 3621 ret = cma_alloc_any_port(ps, id_priv); 3622 else 3623 ret = cma_use_port(ps, id_priv); 3624 mutex_unlock(&lock); 3625 3626 return ret; 3627 } 3628 3629 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr, 3630 struct sockaddr *addr) 3631 { 3632 #if IS_ENABLED(CONFIG_IPV6) 3633 struct sockaddr_in6 *sin6; 3634 3635 if (addr->sa_family != AF_INET6) 3636 return 0; 3637 3638 sin6 = (struct sockaddr_in6 *) addr; 3639 3640 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)) 3641 return 0; 3642 3643 if (!sin6->sin6_scope_id) 3644 return -EINVAL; 3645 3646 dev_addr->bound_dev_if = sin6->sin6_scope_id; 3647 #endif 3648 return 0; 3649 } 3650 3651 int rdma_listen(struct rdma_cm_id *id, int backlog) 3652 { 3653 struct rdma_id_private *id_priv; 3654 int ret; 3655 3656 id_priv = container_of(id, struct rdma_id_private, id); 3657 if (id_priv->state == RDMA_CM_IDLE) { 3658 id->route.addr.src_addr.ss_family = AF_INET; 3659 ret = rdma_bind_addr(id, cma_src_addr(id_priv)); 3660 if (ret) 3661 return ret; 3662 } 3663 3664 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) 3665 return -EINVAL; 3666 3667 if (id_priv->reuseaddr) { 3668 ret = cma_bind_listen(id_priv); 3669 if (ret) 3670 goto err; 3671 } 3672 3673 id_priv->backlog = backlog; 3674 if (id->device) { 3675 if (rdma_cap_ib_cm(id->device, 1)) { 3676 ret = cma_ib_listen(id_priv); 3677 if (ret) 3678 goto err; 3679 } else if (rdma_cap_iw_cm(id->device, 1)) { 3680 ret = cma_iw_listen(id_priv, backlog); 3681 if (ret) 3682 goto err; 3683 } else { 3684 ret = -ENOSYS; 3685 goto err; 3686 } 3687 } else 3688 cma_listen_on_all(id_priv); 3689 3690 return 0; 3691 err: 3692 id_priv->backlog = 0; 3693 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND); 3694 return ret; 3695 } 3696 EXPORT_SYMBOL(rdma_listen); 3697 3698 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr) 3699 { 3700 struct rdma_id_private *id_priv; 3701 int ret; 3702 struct sockaddr *daddr; 3703 3704 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 && 3705 addr->sa_family != AF_IB) 3706 return -EAFNOSUPPORT; 3707 3708 id_priv = container_of(id, struct rdma_id_private, id); 3709 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND)) 3710 return -EINVAL; 3711 3712 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr); 3713 if (ret) 3714 goto err1; 3715 3716 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr)); 3717 if (!cma_any_addr(addr)) { 3718 ret = cma_translate_addr(addr, &id->route.addr.dev_addr); 3719 if (ret) 3720 goto err1; 3721 3722 ret = cma_acquire_dev_by_src_ip(id_priv); 3723 if (ret) 3724 goto err1; 3725 } 3726 3727 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) { 3728 if (addr->sa_family == AF_INET) 3729 id_priv->afonly = 1; 3730 #if IS_ENABLED(CONFIG_IPV6) 3731 else if (addr->sa_family == AF_INET6) { 3732 struct net *net = id_priv->id.route.addr.dev_addr.net; 3733 3734 id_priv->afonly = net->ipv6.sysctl.bindv6only; 3735 } 3736 #endif 3737 } 3738 daddr = cma_dst_addr(id_priv); 3739 daddr->sa_family = addr->sa_family; 3740 3741 ret = cma_get_port(id_priv); 3742 if (ret) 3743 goto err2; 3744 3745 return 0; 3746 err2: 3747 rdma_restrack_del(&id_priv->res); 3748 if (id_priv->cma_dev) 3749 cma_release_dev(id_priv); 3750 err1: 3751 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE); 3752 return ret; 3753 } 3754 EXPORT_SYMBOL(rdma_bind_addr); 3755 3756 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv) 3757 { 3758 struct cma_hdr *cma_hdr; 3759 3760 cma_hdr = hdr; 3761 cma_hdr->cma_version = CMA_VERSION; 3762 if (cma_family(id_priv) == AF_INET) { 3763 struct sockaddr_in *src4, *dst4; 3764 3765 src4 = (struct sockaddr_in *) cma_src_addr(id_priv); 3766 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv); 3767 3768 cma_set_ip_ver(cma_hdr, 4); 3769 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; 3770 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; 3771 cma_hdr->port = src4->sin_port; 3772 } else if (cma_family(id_priv) == AF_INET6) { 3773 struct sockaddr_in6 *src6, *dst6; 3774 3775 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv); 3776 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv); 3777 3778 cma_set_ip_ver(cma_hdr, 6); 3779 cma_hdr->src_addr.ip6 = src6->sin6_addr; 3780 cma_hdr->dst_addr.ip6 = dst6->sin6_addr; 3781 cma_hdr->port = src6->sin6_port; 3782 } 3783 return 0; 3784 } 3785 3786 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id, 3787 const struct ib_cm_event *ib_event) 3788 { 3789 struct rdma_id_private *id_priv = cm_id->context; 3790 struct rdma_cm_event event = {}; 3791 const struct ib_cm_sidr_rep_event_param *rep = 3792 &ib_event->param.sidr_rep_rcvd; 3793 int ret = 0; 3794 3795 mutex_lock(&id_priv->handler_mutex); 3796 if (id_priv->state != RDMA_CM_CONNECT) 3797 goto out; 3798 3799 switch (ib_event->event) { 3800 case IB_CM_SIDR_REQ_ERROR: 3801 event.event = RDMA_CM_EVENT_UNREACHABLE; 3802 event.status = -ETIMEDOUT; 3803 break; 3804 case IB_CM_SIDR_REP_RECEIVED: 3805 event.param.ud.private_data = ib_event->private_data; 3806 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE; 3807 if (rep->status != IB_SIDR_SUCCESS) { 3808 event.event = RDMA_CM_EVENT_UNREACHABLE; 3809 event.status = ib_event->param.sidr_rep_rcvd.status; 3810 pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n", 3811 event.status); 3812 break; 3813 } 3814 ret = cma_set_qkey(id_priv, rep->qkey); 3815 if (ret) { 3816 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret); 3817 event.event = RDMA_CM_EVENT_ADDR_ERROR; 3818 event.status = ret; 3819 break; 3820 } 3821 ib_init_ah_attr_from_path(id_priv->id.device, 3822 id_priv->id.port_num, 3823 id_priv->id.route.path_rec, 3824 &event.param.ud.ah_attr, 3825 rep->sgid_attr); 3826 event.param.ud.qp_num = rep->qpn; 3827 event.param.ud.qkey = rep->qkey; 3828 event.event = RDMA_CM_EVENT_ESTABLISHED; 3829 event.status = 0; 3830 break; 3831 default: 3832 pr_err("RDMA CMA: unexpected IB CM event: %d\n", 3833 ib_event->event); 3834 goto out; 3835 } 3836 3837 ret = cma_cm_event_handler(id_priv, &event); 3838 3839 rdma_destroy_ah_attr(&event.param.ud.ah_attr); 3840 if (ret) { 3841 /* Destroy the CM ID by returning a non-zero value. */ 3842 id_priv->cm_id.ib = NULL; 3843 cma_exch(id_priv, RDMA_CM_DESTROYING); 3844 mutex_unlock(&id_priv->handler_mutex); 3845 rdma_destroy_id(&id_priv->id); 3846 return ret; 3847 } 3848 out: 3849 mutex_unlock(&id_priv->handler_mutex); 3850 return ret; 3851 } 3852 3853 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv, 3854 struct rdma_conn_param *conn_param) 3855 { 3856 struct ib_cm_sidr_req_param req; 3857 struct ib_cm_id *id; 3858 void *private_data; 3859 u8 offset; 3860 int ret; 3861 3862 memset(&req, 0, sizeof req); 3863 offset = cma_user_data_offset(id_priv); 3864 req.private_data_len = offset + conn_param->private_data_len; 3865 if (req.private_data_len < conn_param->private_data_len) 3866 return -EINVAL; 3867 3868 if (req.private_data_len) { 3869 private_data = kzalloc(req.private_data_len, GFP_ATOMIC); 3870 if (!private_data) 3871 return -ENOMEM; 3872 } else { 3873 private_data = NULL; 3874 } 3875 3876 if (conn_param->private_data && conn_param->private_data_len) 3877 memcpy(private_data + offset, conn_param->private_data, 3878 conn_param->private_data_len); 3879 3880 if (private_data) { 3881 ret = cma_format_hdr(private_data, id_priv); 3882 if (ret) 3883 goto out; 3884 req.private_data = private_data; 3885 } 3886 3887 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler, 3888 id_priv); 3889 if (IS_ERR(id)) { 3890 ret = PTR_ERR(id); 3891 goto out; 3892 } 3893 id_priv->cm_id.ib = id; 3894 3895 req.path = id_priv->id.route.path_rec; 3896 req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr; 3897 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv)); 3898 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8); 3899 req.max_cm_retries = CMA_MAX_CM_RETRIES; 3900 3901 trace_cm_send_sidr_req(id_priv); 3902 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req); 3903 if (ret) { 3904 ib_destroy_cm_id(id_priv->cm_id.ib); 3905 id_priv->cm_id.ib = NULL; 3906 } 3907 out: 3908 kfree(private_data); 3909 return ret; 3910 } 3911 3912 static int cma_connect_ib(struct rdma_id_private *id_priv, 3913 struct rdma_conn_param *conn_param) 3914 { 3915 struct ib_cm_req_param req; 3916 struct rdma_route *route; 3917 void *private_data; 3918 struct ib_cm_id *id; 3919 u8 offset; 3920 int ret; 3921 3922 memset(&req, 0, sizeof req); 3923 offset = cma_user_data_offset(id_priv); 3924 req.private_data_len = offset + conn_param->private_data_len; 3925 if (req.private_data_len < conn_param->private_data_len) 3926 return -EINVAL; 3927 3928 if (req.private_data_len) { 3929 private_data = kzalloc(req.private_data_len, GFP_ATOMIC); 3930 if (!private_data) 3931 return -ENOMEM; 3932 } else { 3933 private_data = NULL; 3934 } 3935 3936 if (conn_param->private_data && conn_param->private_data_len) 3937 memcpy(private_data + offset, conn_param->private_data, 3938 conn_param->private_data_len); 3939 3940 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv); 3941 if (IS_ERR(id)) { 3942 ret = PTR_ERR(id); 3943 goto out; 3944 } 3945 id_priv->cm_id.ib = id; 3946 3947 route = &id_priv->id.route; 3948 if (private_data) { 3949 ret = cma_format_hdr(private_data, id_priv); 3950 if (ret) 3951 goto out; 3952 req.private_data = private_data; 3953 } 3954 3955 req.primary_path = &route->path_rec[0]; 3956 if (route->num_paths == 2) 3957 req.alternate_path = &route->path_rec[1]; 3958 3959 req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr; 3960 /* Alternate path SGID attribute currently unsupported */ 3961 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv)); 3962 req.qp_num = id_priv->qp_num; 3963 req.qp_type = id_priv->id.qp_type; 3964 req.starting_psn = id_priv->seq_num; 3965 req.responder_resources = conn_param->responder_resources; 3966 req.initiator_depth = conn_param->initiator_depth; 3967 req.flow_control = conn_param->flow_control; 3968 req.retry_count = min_t(u8, 7, conn_param->retry_count); 3969 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count); 3970 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; 3971 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; 3972 req.max_cm_retries = CMA_MAX_CM_RETRIES; 3973 req.srq = id_priv->srq ? 1 : 0; 3974 req.ece.vendor_id = id_priv->ece.vendor_id; 3975 req.ece.attr_mod = id_priv->ece.attr_mod; 3976 3977 trace_cm_send_req(id_priv); 3978 ret = ib_send_cm_req(id_priv->cm_id.ib, &req); 3979 out: 3980 if (ret && !IS_ERR(id)) { 3981 ib_destroy_cm_id(id); 3982 id_priv->cm_id.ib = NULL; 3983 } 3984 3985 kfree(private_data); 3986 return ret; 3987 } 3988 3989 static int cma_connect_iw(struct rdma_id_private *id_priv, 3990 struct rdma_conn_param *conn_param) 3991 { 3992 struct iw_cm_id *cm_id; 3993 int ret; 3994 struct iw_cm_conn_param iw_param; 3995 3996 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv); 3997 if (IS_ERR(cm_id)) 3998 return PTR_ERR(cm_id); 3999 4000 cm_id->tos = id_priv->tos; 4001 cm_id->tos_set = id_priv->tos_set; 4002 id_priv->cm_id.iw = cm_id; 4003 4004 memcpy(&cm_id->local_addr, cma_src_addr(id_priv), 4005 rdma_addr_size(cma_src_addr(id_priv))); 4006 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv), 4007 rdma_addr_size(cma_dst_addr(id_priv))); 4008 4009 ret = cma_modify_qp_rtr(id_priv, conn_param); 4010 if (ret) 4011 goto out; 4012 4013 if (conn_param) { 4014 iw_param.ord = conn_param->initiator_depth; 4015 iw_param.ird = conn_param->responder_resources; 4016 iw_param.private_data = conn_param->private_data; 4017 iw_param.private_data_len = conn_param->private_data_len; 4018 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num; 4019 } else { 4020 memset(&iw_param, 0, sizeof iw_param); 4021 iw_param.qpn = id_priv->qp_num; 4022 } 4023 ret = iw_cm_connect(cm_id, &iw_param); 4024 out: 4025 if (ret) { 4026 iw_destroy_cm_id(cm_id); 4027 id_priv->cm_id.iw = NULL; 4028 } 4029 return ret; 4030 } 4031 4032 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) 4033 { 4034 struct rdma_id_private *id_priv; 4035 int ret; 4036 4037 id_priv = container_of(id, struct rdma_id_private, id); 4038 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT)) 4039 return -EINVAL; 4040 4041 if (!id->qp) { 4042 id_priv->qp_num = conn_param->qp_num; 4043 id_priv->srq = conn_param->srq; 4044 } 4045 4046 if (rdma_cap_ib_cm(id->device, id->port_num)) { 4047 if (id->qp_type == IB_QPT_UD) 4048 ret = cma_resolve_ib_udp(id_priv, conn_param); 4049 else 4050 ret = cma_connect_ib(id_priv, conn_param); 4051 } else if (rdma_cap_iw_cm(id->device, id->port_num)) 4052 ret = cma_connect_iw(id_priv, conn_param); 4053 else 4054 ret = -ENOSYS; 4055 if (ret) 4056 goto err; 4057 4058 return 0; 4059 err: 4060 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED); 4061 return ret; 4062 } 4063 EXPORT_SYMBOL(rdma_connect); 4064 4065 /** 4066 * rdma_connect_ece - Initiate an active connection request with ECE data. 4067 * @id: Connection identifier to connect. 4068 * @conn_param: Connection information used for connected QPs. 4069 * @ece: ECE parameters 4070 * 4071 * See rdma_connect() explanation. 4072 */ 4073 int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param, 4074 struct rdma_ucm_ece *ece) 4075 { 4076 struct rdma_id_private *id_priv = 4077 container_of(id, struct rdma_id_private, id); 4078 4079 id_priv->ece.vendor_id = ece->vendor_id; 4080 id_priv->ece.attr_mod = ece->attr_mod; 4081 4082 return rdma_connect(id, conn_param); 4083 } 4084 EXPORT_SYMBOL(rdma_connect_ece); 4085 4086 static int cma_accept_ib(struct rdma_id_private *id_priv, 4087 struct rdma_conn_param *conn_param) 4088 { 4089 struct ib_cm_rep_param rep; 4090 int ret; 4091 4092 ret = cma_modify_qp_rtr(id_priv, conn_param); 4093 if (ret) 4094 goto out; 4095 4096 ret = cma_modify_qp_rts(id_priv, conn_param); 4097 if (ret) 4098 goto out; 4099 4100 memset(&rep, 0, sizeof rep); 4101 rep.qp_num = id_priv->qp_num; 4102 rep.starting_psn = id_priv->seq_num; 4103 rep.private_data = conn_param->private_data; 4104 rep.private_data_len = conn_param->private_data_len; 4105 rep.responder_resources = conn_param->responder_resources; 4106 rep.initiator_depth = conn_param->initiator_depth; 4107 rep.failover_accepted = 0; 4108 rep.flow_control = conn_param->flow_control; 4109 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count); 4110 rep.srq = id_priv->srq ? 1 : 0; 4111 rep.ece.vendor_id = id_priv->ece.vendor_id; 4112 rep.ece.attr_mod = id_priv->ece.attr_mod; 4113 4114 trace_cm_send_rep(id_priv); 4115 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep); 4116 out: 4117 return ret; 4118 } 4119 4120 static int cma_accept_iw(struct rdma_id_private *id_priv, 4121 struct rdma_conn_param *conn_param) 4122 { 4123 struct iw_cm_conn_param iw_param; 4124 int ret; 4125 4126 if (!conn_param) 4127 return -EINVAL; 4128 4129 ret = cma_modify_qp_rtr(id_priv, conn_param); 4130 if (ret) 4131 return ret; 4132 4133 iw_param.ord = conn_param->initiator_depth; 4134 iw_param.ird = conn_param->responder_resources; 4135 iw_param.private_data = conn_param->private_data; 4136 iw_param.private_data_len = conn_param->private_data_len; 4137 if (id_priv->id.qp) { 4138 iw_param.qpn = id_priv->qp_num; 4139 } else 4140 iw_param.qpn = conn_param->qp_num; 4141 4142 return iw_cm_accept(id_priv->cm_id.iw, &iw_param); 4143 } 4144 4145 static int cma_send_sidr_rep(struct rdma_id_private *id_priv, 4146 enum ib_cm_sidr_status status, u32 qkey, 4147 const void *private_data, int private_data_len) 4148 { 4149 struct ib_cm_sidr_rep_param rep; 4150 int ret; 4151 4152 memset(&rep, 0, sizeof rep); 4153 rep.status = status; 4154 if (status == IB_SIDR_SUCCESS) { 4155 ret = cma_set_qkey(id_priv, qkey); 4156 if (ret) 4157 return ret; 4158 rep.qp_num = id_priv->qp_num; 4159 rep.qkey = id_priv->qkey; 4160 4161 rep.ece.vendor_id = id_priv->ece.vendor_id; 4162 rep.ece.attr_mod = id_priv->ece.attr_mod; 4163 } 4164 4165 rep.private_data = private_data; 4166 rep.private_data_len = private_data_len; 4167 4168 trace_cm_send_sidr_rep(id_priv); 4169 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep); 4170 } 4171 4172 int __rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param, 4173 const char *caller) 4174 { 4175 struct rdma_id_private *id_priv; 4176 int ret; 4177 4178 id_priv = container_of(id, struct rdma_id_private, id); 4179 4180 rdma_restrack_set_task(&id_priv->res, caller); 4181 4182 if (!cma_comp(id_priv, RDMA_CM_CONNECT)) 4183 return -EINVAL; 4184 4185 if (!id->qp && conn_param) { 4186 id_priv->qp_num = conn_param->qp_num; 4187 id_priv->srq = conn_param->srq; 4188 } 4189 4190 if (rdma_cap_ib_cm(id->device, id->port_num)) { 4191 if (id->qp_type == IB_QPT_UD) { 4192 if (conn_param) 4193 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS, 4194 conn_param->qkey, 4195 conn_param->private_data, 4196 conn_param->private_data_len); 4197 else 4198 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS, 4199 0, NULL, 0); 4200 } else { 4201 if (conn_param) 4202 ret = cma_accept_ib(id_priv, conn_param); 4203 else 4204 ret = cma_rep_recv(id_priv); 4205 } 4206 } else if (rdma_cap_iw_cm(id->device, id->port_num)) 4207 ret = cma_accept_iw(id_priv, conn_param); 4208 else 4209 ret = -ENOSYS; 4210 4211 if (ret) 4212 goto reject; 4213 4214 return 0; 4215 reject: 4216 cma_modify_qp_err(id_priv); 4217 rdma_reject(id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED); 4218 return ret; 4219 } 4220 EXPORT_SYMBOL(__rdma_accept); 4221 4222 int __rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param, 4223 const char *caller, struct rdma_ucm_ece *ece) 4224 { 4225 struct rdma_id_private *id_priv = 4226 container_of(id, struct rdma_id_private, id); 4227 4228 id_priv->ece.vendor_id = ece->vendor_id; 4229 id_priv->ece.attr_mod = ece->attr_mod; 4230 4231 return __rdma_accept(id, conn_param, caller); 4232 } 4233 EXPORT_SYMBOL(__rdma_accept_ece); 4234 4235 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event) 4236 { 4237 struct rdma_id_private *id_priv; 4238 int ret; 4239 4240 id_priv = container_of(id, struct rdma_id_private, id); 4241 if (!id_priv->cm_id.ib) 4242 return -EINVAL; 4243 4244 switch (id->device->node_type) { 4245 case RDMA_NODE_IB_CA: 4246 ret = ib_cm_notify(id_priv->cm_id.ib, event); 4247 break; 4248 default: 4249 ret = 0; 4250 break; 4251 } 4252 return ret; 4253 } 4254 EXPORT_SYMBOL(rdma_notify); 4255 4256 int rdma_reject(struct rdma_cm_id *id, const void *private_data, 4257 u8 private_data_len, u8 reason) 4258 { 4259 struct rdma_id_private *id_priv; 4260 int ret; 4261 4262 id_priv = container_of(id, struct rdma_id_private, id); 4263 if (!id_priv->cm_id.ib) 4264 return -EINVAL; 4265 4266 if (rdma_cap_ib_cm(id->device, id->port_num)) { 4267 if (id->qp_type == IB_QPT_UD) { 4268 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0, 4269 private_data, private_data_len); 4270 } else { 4271 trace_cm_send_rej(id_priv); 4272 ret = ib_send_cm_rej(id_priv->cm_id.ib, reason, NULL, 0, 4273 private_data, private_data_len); 4274 } 4275 } else if (rdma_cap_iw_cm(id->device, id->port_num)) { 4276 ret = iw_cm_reject(id_priv->cm_id.iw, 4277 private_data, private_data_len); 4278 } else 4279 ret = -ENOSYS; 4280 4281 return ret; 4282 } 4283 EXPORT_SYMBOL(rdma_reject); 4284 4285 int rdma_disconnect(struct rdma_cm_id *id) 4286 { 4287 struct rdma_id_private *id_priv; 4288 int ret; 4289 4290 id_priv = container_of(id, struct rdma_id_private, id); 4291 if (!id_priv->cm_id.ib) 4292 return -EINVAL; 4293 4294 if (rdma_cap_ib_cm(id->device, id->port_num)) { 4295 ret = cma_modify_qp_err(id_priv); 4296 if (ret) 4297 goto out; 4298 /* Initiate or respond to a disconnect. */ 4299 trace_cm_disconnect(id_priv); 4300 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) { 4301 if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0)) 4302 trace_cm_sent_drep(id_priv); 4303 } else { 4304 trace_cm_sent_dreq(id_priv); 4305 } 4306 } else if (rdma_cap_iw_cm(id->device, id->port_num)) { 4307 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0); 4308 } else 4309 ret = -EINVAL; 4310 4311 out: 4312 return ret; 4313 } 4314 EXPORT_SYMBOL(rdma_disconnect); 4315 4316 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast) 4317 { 4318 struct rdma_id_private *id_priv; 4319 struct cma_multicast *mc = multicast->context; 4320 struct rdma_cm_event event = {}; 4321 int ret = 0; 4322 4323 id_priv = mc->id_priv; 4324 mutex_lock(&id_priv->handler_mutex); 4325 if (id_priv->state != RDMA_CM_ADDR_BOUND && 4326 id_priv->state != RDMA_CM_ADDR_RESOLVED) 4327 goto out; 4328 4329 if (!status) 4330 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey)); 4331 else 4332 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n", 4333 status); 4334 mutex_lock(&id_priv->qp_mutex); 4335 if (!status && id_priv->id.qp) { 4336 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid, 4337 be16_to_cpu(multicast->rec.mlid)); 4338 if (status) 4339 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to attach QP. status %d\n", 4340 status); 4341 } 4342 mutex_unlock(&id_priv->qp_mutex); 4343 4344 event.status = status; 4345 event.param.ud.private_data = mc->context; 4346 if (!status) { 4347 struct rdma_dev_addr *dev_addr = 4348 &id_priv->id.route.addr.dev_addr; 4349 struct net_device *ndev = 4350 dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if); 4351 enum ib_gid_type gid_type = 4352 id_priv->cma_dev->default_gid_type[id_priv->id.port_num - 4353 rdma_start_port(id_priv->cma_dev->device)]; 4354 4355 event.event = RDMA_CM_EVENT_MULTICAST_JOIN; 4356 ret = ib_init_ah_from_mcmember(id_priv->id.device, 4357 id_priv->id.port_num, 4358 &multicast->rec, 4359 ndev, gid_type, 4360 &event.param.ud.ah_attr); 4361 if (ret) 4362 event.event = RDMA_CM_EVENT_MULTICAST_ERROR; 4363 4364 event.param.ud.qp_num = 0xFFFFFF; 4365 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey); 4366 if (ndev) 4367 dev_put(ndev); 4368 } else 4369 event.event = RDMA_CM_EVENT_MULTICAST_ERROR; 4370 4371 ret = cma_cm_event_handler(id_priv, &event); 4372 4373 rdma_destroy_ah_attr(&event.param.ud.ah_attr); 4374 if (ret) { 4375 cma_exch(id_priv, RDMA_CM_DESTROYING); 4376 mutex_unlock(&id_priv->handler_mutex); 4377 rdma_destroy_id(&id_priv->id); 4378 return 0; 4379 } 4380 4381 out: 4382 mutex_unlock(&id_priv->handler_mutex); 4383 return 0; 4384 } 4385 4386 static void cma_set_mgid(struct rdma_id_private *id_priv, 4387 struct sockaddr *addr, union ib_gid *mgid) 4388 { 4389 unsigned char mc_map[MAX_ADDR_LEN]; 4390 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 4391 struct sockaddr_in *sin = (struct sockaddr_in *) addr; 4392 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr; 4393 4394 if (cma_any_addr(addr)) { 4395 memset(mgid, 0, sizeof *mgid); 4396 } else if ((addr->sa_family == AF_INET6) && 4397 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) == 4398 0xFF10A01B)) { 4399 /* IPv6 address is an SA assigned MGID. */ 4400 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); 4401 } else if (addr->sa_family == AF_IB) { 4402 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid); 4403 } else if (addr->sa_family == AF_INET6) { 4404 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map); 4405 if (id_priv->id.ps == RDMA_PS_UDP) 4406 mc_map[7] = 0x01; /* Use RDMA CM signature */ 4407 *mgid = *(union ib_gid *) (mc_map + 4); 4408 } else { 4409 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map); 4410 if (id_priv->id.ps == RDMA_PS_UDP) 4411 mc_map[7] = 0x01; /* Use RDMA CM signature */ 4412 *mgid = *(union ib_gid *) (mc_map + 4); 4413 } 4414 } 4415 4416 static int cma_join_ib_multicast(struct rdma_id_private *id_priv, 4417 struct cma_multicast *mc) 4418 { 4419 struct ib_sa_mcmember_rec rec; 4420 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 4421 ib_sa_comp_mask comp_mask; 4422 int ret; 4423 4424 ib_addr_get_mgid(dev_addr, &rec.mgid); 4425 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num, 4426 &rec.mgid, &rec); 4427 if (ret) 4428 return ret; 4429 4430 ret = cma_set_qkey(id_priv, 0); 4431 if (ret) 4432 return ret; 4433 4434 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid); 4435 rec.qkey = cpu_to_be32(id_priv->qkey); 4436 rdma_addr_get_sgid(dev_addr, &rec.port_gid); 4437 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); 4438 rec.join_state = mc->join_state; 4439 4440 if ((rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) && 4441 (!ib_sa_sendonly_fullmem_support(&sa_client, 4442 id_priv->id.device, 4443 id_priv->id.port_num))) { 4444 dev_warn( 4445 &id_priv->id.device->dev, 4446 "RDMA CM: port %u Unable to multicast join: SM doesn't support Send Only Full Member option\n", 4447 id_priv->id.port_num); 4448 return -EOPNOTSUPP; 4449 } 4450 4451 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID | 4452 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE | 4453 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL | 4454 IB_SA_MCMEMBER_REC_FLOW_LABEL | 4455 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS; 4456 4457 if (id_priv->id.ps == RDMA_PS_IPOIB) 4458 comp_mask |= IB_SA_MCMEMBER_REC_RATE | 4459 IB_SA_MCMEMBER_REC_RATE_SELECTOR | 4460 IB_SA_MCMEMBER_REC_MTU_SELECTOR | 4461 IB_SA_MCMEMBER_REC_MTU | 4462 IB_SA_MCMEMBER_REC_HOP_LIMIT; 4463 4464 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device, 4465 id_priv->id.port_num, &rec, 4466 comp_mask, GFP_KERNEL, 4467 cma_ib_mc_handler, mc); 4468 return PTR_ERR_OR_ZERO(mc->multicast.ib); 4469 } 4470 4471 static void iboe_mcast_work_handler(struct work_struct *work) 4472 { 4473 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work); 4474 struct cma_multicast *mc = mw->mc; 4475 struct ib_sa_multicast *m = mc->multicast.ib; 4476 4477 mc->multicast.ib->context = mc; 4478 cma_ib_mc_handler(0, m); 4479 kref_put(&mc->mcref, release_mc); 4480 kfree(mw); 4481 } 4482 4483 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid, 4484 enum ib_gid_type gid_type) 4485 { 4486 struct sockaddr_in *sin = (struct sockaddr_in *)addr; 4487 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; 4488 4489 if (cma_any_addr(addr)) { 4490 memset(mgid, 0, sizeof *mgid); 4491 } else if (addr->sa_family == AF_INET6) { 4492 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); 4493 } else { 4494 mgid->raw[0] = 4495 (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff; 4496 mgid->raw[1] = 4497 (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e; 4498 mgid->raw[2] = 0; 4499 mgid->raw[3] = 0; 4500 mgid->raw[4] = 0; 4501 mgid->raw[5] = 0; 4502 mgid->raw[6] = 0; 4503 mgid->raw[7] = 0; 4504 mgid->raw[8] = 0; 4505 mgid->raw[9] = 0; 4506 mgid->raw[10] = 0xff; 4507 mgid->raw[11] = 0xff; 4508 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr; 4509 } 4510 } 4511 4512 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv, 4513 struct cma_multicast *mc) 4514 { 4515 struct iboe_mcast_work *work; 4516 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; 4517 int err = 0; 4518 struct sockaddr *addr = (struct sockaddr *)&mc->addr; 4519 struct net_device *ndev = NULL; 4520 enum ib_gid_type gid_type; 4521 bool send_only; 4522 4523 send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN); 4524 4525 if (cma_zero_addr((struct sockaddr *)&mc->addr)) 4526 return -EINVAL; 4527 4528 work = kzalloc(sizeof *work, GFP_KERNEL); 4529 if (!work) 4530 return -ENOMEM; 4531 4532 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL); 4533 if (!mc->multicast.ib) { 4534 err = -ENOMEM; 4535 goto out1; 4536 } 4537 4538 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num - 4539 rdma_start_port(id_priv->cma_dev->device)]; 4540 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid, gid_type); 4541 4542 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff); 4543 if (id_priv->id.ps == RDMA_PS_UDP) 4544 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY); 4545 4546 if (dev_addr->bound_dev_if) 4547 ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if); 4548 if (!ndev) { 4549 err = -ENODEV; 4550 goto out2; 4551 } 4552 mc->multicast.ib->rec.rate = iboe_get_rate(ndev); 4553 mc->multicast.ib->rec.hop_limit = 1; 4554 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu); 4555 4556 if (addr->sa_family == AF_INET) { 4557 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) { 4558 mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT; 4559 if (!send_only) { 4560 err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid, 4561 true); 4562 } 4563 } 4564 } else { 4565 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) 4566 err = -ENOTSUPP; 4567 } 4568 dev_put(ndev); 4569 if (err || !mc->multicast.ib->rec.mtu) { 4570 if (!err) 4571 err = -EINVAL; 4572 goto out2; 4573 } 4574 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr, 4575 &mc->multicast.ib->rec.port_gid); 4576 work->id = id_priv; 4577 work->mc = mc; 4578 INIT_WORK(&work->work, iboe_mcast_work_handler); 4579 kref_get(&mc->mcref); 4580 queue_work(cma_wq, &work->work); 4581 4582 return 0; 4583 4584 out2: 4585 kfree(mc->multicast.ib); 4586 out1: 4587 kfree(work); 4588 return err; 4589 } 4590 4591 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr, 4592 u8 join_state, void *context) 4593 { 4594 struct rdma_id_private *id_priv; 4595 struct cma_multicast *mc; 4596 int ret; 4597 4598 if (!id->device) 4599 return -EINVAL; 4600 4601 id_priv = container_of(id, struct rdma_id_private, id); 4602 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) && 4603 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED)) 4604 return -EINVAL; 4605 4606 mc = kmalloc(sizeof *mc, GFP_KERNEL); 4607 if (!mc) 4608 return -ENOMEM; 4609 4610 memcpy(&mc->addr, addr, rdma_addr_size(addr)); 4611 mc->context = context; 4612 mc->id_priv = id_priv; 4613 mc->join_state = join_state; 4614 4615 if (rdma_protocol_roce(id->device, id->port_num)) { 4616 kref_init(&mc->mcref); 4617 ret = cma_iboe_join_multicast(id_priv, mc); 4618 if (ret) 4619 goto out_err; 4620 } else if (rdma_cap_ib_mcast(id->device, id->port_num)) { 4621 ret = cma_join_ib_multicast(id_priv, mc); 4622 if (ret) 4623 goto out_err; 4624 } else { 4625 ret = -ENOSYS; 4626 goto out_err; 4627 } 4628 4629 spin_lock(&id_priv->lock); 4630 list_add(&mc->list, &id_priv->mc_list); 4631 spin_unlock(&id_priv->lock); 4632 4633 return 0; 4634 out_err: 4635 kfree(mc); 4636 return ret; 4637 } 4638 EXPORT_SYMBOL(rdma_join_multicast); 4639 4640 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr) 4641 { 4642 struct rdma_id_private *id_priv; 4643 struct cma_multicast *mc; 4644 4645 id_priv = container_of(id, struct rdma_id_private, id); 4646 spin_lock_irq(&id_priv->lock); 4647 list_for_each_entry(mc, &id_priv->mc_list, list) { 4648 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) { 4649 list_del(&mc->list); 4650 spin_unlock_irq(&id_priv->lock); 4651 4652 if (id->qp) 4653 ib_detach_mcast(id->qp, 4654 &mc->multicast.ib->rec.mgid, 4655 be16_to_cpu(mc->multicast.ib->rec.mlid)); 4656 4657 BUG_ON(id_priv->cma_dev->device != id->device); 4658 4659 if (rdma_cap_ib_mcast(id->device, id->port_num)) { 4660 ib_sa_free_multicast(mc->multicast.ib); 4661 kfree(mc); 4662 } else if (rdma_protocol_roce(id->device, id->port_num)) { 4663 cma_leave_roce_mc_group(id_priv, mc); 4664 } 4665 return; 4666 } 4667 } 4668 spin_unlock_irq(&id_priv->lock); 4669 } 4670 EXPORT_SYMBOL(rdma_leave_multicast); 4671 4672 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv) 4673 { 4674 struct rdma_dev_addr *dev_addr; 4675 struct cma_ndev_work *work; 4676 4677 dev_addr = &id_priv->id.route.addr.dev_addr; 4678 4679 if ((dev_addr->bound_dev_if == ndev->ifindex) && 4680 (net_eq(dev_net(ndev), dev_addr->net)) && 4681 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) { 4682 pr_info("RDMA CM addr change for ndev %s used by id %p\n", 4683 ndev->name, &id_priv->id); 4684 work = kzalloc(sizeof *work, GFP_KERNEL); 4685 if (!work) 4686 return -ENOMEM; 4687 4688 INIT_WORK(&work->work, cma_ndev_work_handler); 4689 work->id = id_priv; 4690 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE; 4691 cma_id_get(id_priv); 4692 queue_work(cma_wq, &work->work); 4693 } 4694 4695 return 0; 4696 } 4697 4698 static int cma_netdev_callback(struct notifier_block *self, unsigned long event, 4699 void *ptr) 4700 { 4701 struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 4702 struct cma_device *cma_dev; 4703 struct rdma_id_private *id_priv; 4704 int ret = NOTIFY_DONE; 4705 4706 if (event != NETDEV_BONDING_FAILOVER) 4707 return NOTIFY_DONE; 4708 4709 if (!netif_is_bond_master(ndev)) 4710 return NOTIFY_DONE; 4711 4712 mutex_lock(&lock); 4713 list_for_each_entry(cma_dev, &dev_list, list) 4714 list_for_each_entry(id_priv, &cma_dev->id_list, list) { 4715 ret = cma_netdev_change(ndev, id_priv); 4716 if (ret) 4717 goto out; 4718 } 4719 4720 out: 4721 mutex_unlock(&lock); 4722 return ret; 4723 } 4724 4725 static struct notifier_block cma_nb = { 4726 .notifier_call = cma_netdev_callback 4727 }; 4728 4729 static int cma_add_one(struct ib_device *device) 4730 { 4731 struct cma_device *cma_dev; 4732 struct rdma_id_private *id_priv; 4733 unsigned int i; 4734 unsigned long supported_gids = 0; 4735 int ret; 4736 4737 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL); 4738 if (!cma_dev) 4739 return -ENOMEM; 4740 4741 cma_dev->device = device; 4742 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt, 4743 sizeof(*cma_dev->default_gid_type), 4744 GFP_KERNEL); 4745 if (!cma_dev->default_gid_type) { 4746 ret = -ENOMEM; 4747 goto free_cma_dev; 4748 } 4749 4750 cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt, 4751 sizeof(*cma_dev->default_roce_tos), 4752 GFP_KERNEL); 4753 if (!cma_dev->default_roce_tos) { 4754 ret = -ENOMEM; 4755 goto free_gid_type; 4756 } 4757 4758 rdma_for_each_port (device, i) { 4759 supported_gids = roce_gid_type_mask_support(device, i); 4760 WARN_ON(!supported_gids); 4761 if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE)) 4762 cma_dev->default_gid_type[i - rdma_start_port(device)] = 4763 CMA_PREFERRED_ROCE_GID_TYPE; 4764 else 4765 cma_dev->default_gid_type[i - rdma_start_port(device)] = 4766 find_first_bit(&supported_gids, BITS_PER_LONG); 4767 cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0; 4768 } 4769 4770 init_completion(&cma_dev->comp); 4771 refcount_set(&cma_dev->refcount, 1); 4772 INIT_LIST_HEAD(&cma_dev->id_list); 4773 ib_set_client_data(device, &cma_client, cma_dev); 4774 4775 mutex_lock(&lock); 4776 list_add_tail(&cma_dev->list, &dev_list); 4777 list_for_each_entry(id_priv, &listen_any_list, list) 4778 cma_listen_on_dev(id_priv, cma_dev); 4779 mutex_unlock(&lock); 4780 4781 trace_cm_add_one(device); 4782 return 0; 4783 4784 free_gid_type: 4785 kfree(cma_dev->default_gid_type); 4786 4787 free_cma_dev: 4788 kfree(cma_dev); 4789 return ret; 4790 } 4791 4792 static int cma_remove_id_dev(struct rdma_id_private *id_priv) 4793 { 4794 struct rdma_cm_event event = {}; 4795 enum rdma_cm_state state; 4796 int ret = 0; 4797 4798 /* Record that we want to remove the device */ 4799 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL); 4800 if (state == RDMA_CM_DESTROYING) 4801 return 0; 4802 4803 cma_cancel_operation(id_priv, state); 4804 mutex_lock(&id_priv->handler_mutex); 4805 4806 /* Check for destruction from another callback. */ 4807 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL)) 4808 goto out; 4809 4810 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL; 4811 ret = cma_cm_event_handler(id_priv, &event); 4812 out: 4813 mutex_unlock(&id_priv->handler_mutex); 4814 return ret; 4815 } 4816 4817 static void cma_process_remove(struct cma_device *cma_dev) 4818 { 4819 struct rdma_id_private *id_priv; 4820 int ret; 4821 4822 mutex_lock(&lock); 4823 while (!list_empty(&cma_dev->id_list)) { 4824 id_priv = list_entry(cma_dev->id_list.next, 4825 struct rdma_id_private, list); 4826 4827 list_del(&id_priv->listen_list); 4828 list_del_init(&id_priv->list); 4829 cma_id_get(id_priv); 4830 mutex_unlock(&lock); 4831 4832 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv); 4833 cma_id_put(id_priv); 4834 if (ret) 4835 rdma_destroy_id(&id_priv->id); 4836 4837 mutex_lock(&lock); 4838 } 4839 mutex_unlock(&lock); 4840 4841 cma_dev_put(cma_dev); 4842 wait_for_completion(&cma_dev->comp); 4843 } 4844 4845 static void cma_remove_one(struct ib_device *device, void *client_data) 4846 { 4847 struct cma_device *cma_dev = client_data; 4848 4849 trace_cm_remove_one(device); 4850 4851 mutex_lock(&lock); 4852 list_del(&cma_dev->list); 4853 mutex_unlock(&lock); 4854 4855 cma_process_remove(cma_dev); 4856 kfree(cma_dev->default_roce_tos); 4857 kfree(cma_dev->default_gid_type); 4858 kfree(cma_dev); 4859 } 4860 4861 static int cma_init_net(struct net *net) 4862 { 4863 struct cma_pernet *pernet = cma_pernet(net); 4864 4865 xa_init(&pernet->tcp_ps); 4866 xa_init(&pernet->udp_ps); 4867 xa_init(&pernet->ipoib_ps); 4868 xa_init(&pernet->ib_ps); 4869 4870 return 0; 4871 } 4872 4873 static void cma_exit_net(struct net *net) 4874 { 4875 struct cma_pernet *pernet = cma_pernet(net); 4876 4877 WARN_ON(!xa_empty(&pernet->tcp_ps)); 4878 WARN_ON(!xa_empty(&pernet->udp_ps)); 4879 WARN_ON(!xa_empty(&pernet->ipoib_ps)); 4880 WARN_ON(!xa_empty(&pernet->ib_ps)); 4881 } 4882 4883 static struct pernet_operations cma_pernet_operations = { 4884 .init = cma_init_net, 4885 .exit = cma_exit_net, 4886 .id = &cma_pernet_id, 4887 .size = sizeof(struct cma_pernet), 4888 }; 4889 4890 static int __init cma_init(void) 4891 { 4892 int ret; 4893 4894 /* 4895 * There is a rare lock ordering dependency in cma_netdev_callback() 4896 * that only happens when bonding is enabled. Teach lockdep that rtnl 4897 * must never be nested under lock so it can find these without having 4898 * to test with bonding. 4899 */ 4900 if (IS_ENABLED(CONFIG_LOCKDEP)) { 4901 rtnl_lock(); 4902 mutex_lock(&lock); 4903 mutex_unlock(&lock); 4904 rtnl_unlock(); 4905 } 4906 4907 cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM); 4908 if (!cma_wq) 4909 return -ENOMEM; 4910 4911 ret = register_pernet_subsys(&cma_pernet_operations); 4912 if (ret) 4913 goto err_wq; 4914 4915 ib_sa_register_client(&sa_client); 4916 register_netdevice_notifier(&cma_nb); 4917 4918 ret = ib_register_client(&cma_client); 4919 if (ret) 4920 goto err; 4921 4922 ret = cma_configfs_init(); 4923 if (ret) 4924 goto err_ib; 4925 4926 return 0; 4927 4928 err_ib: 4929 ib_unregister_client(&cma_client); 4930 err: 4931 unregister_netdevice_notifier(&cma_nb); 4932 ib_sa_unregister_client(&sa_client); 4933 unregister_pernet_subsys(&cma_pernet_operations); 4934 err_wq: 4935 destroy_workqueue(cma_wq); 4936 return ret; 4937 } 4938 4939 static void __exit cma_cleanup(void) 4940 { 4941 cma_configfs_exit(); 4942 ib_unregister_client(&cma_client); 4943 unregister_netdevice_notifier(&cma_nb); 4944 ib_sa_unregister_client(&sa_client); 4945 unregister_pernet_subsys(&cma_pernet_operations); 4946 destroy_workqueue(cma_wq); 4947 } 4948 4949 module_init(cma_init); 4950 module_exit(cma_cleanup); 4951