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