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