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 18
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
rdma_event_msg(enum rdma_cm_event_type event)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
rdma_reject_msg(struct rdma_cm_id * id,int reason)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 */
rdma_is_consumer_reject(struct rdma_cm_id * id,int reason)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
rdma_consumer_reject_data(struct rdma_cm_id * id,struct rdma_cm_event * ev,u8 * data_len)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 */
rdma_iw_cm_id(struct rdma_cm_id * id)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 */
rdma_res_to_id(struct rdma_restrack_entry * res)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
cma_pernet(struct net * net)186 static struct cma_pernet *cma_pernet(struct net *net)
187 {
188 return net_generic(net, cma_pernet_id);
189 }
190
191 static
cma_pernet_xa(struct net * net,enum rdma_ucm_port_space ps)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
cma_ps_alloc(struct net * net,enum rdma_ucm_port_space ps,struct rdma_bind_list * bind_list,int snum)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
cma_ps_find(struct net * net,enum rdma_ucm_port_space ps,int snum)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
cma_ps_remove(struct net * net,enum rdma_ucm_port_space ps,int snum)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
cma_dev_get(struct cma_device * cma_dev)259 void cma_dev_get(struct cma_device *cma_dev)
260 {
261 refcount_inc(&cma_dev->refcount);
262 }
263
cma_dev_put(struct cma_device * cma_dev)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
cma_enum_devices_by_ibdev(cma_device_filter filter,void * cookie)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
cma_get_default_gid_type(struct cma_device * cma_dev,u32 port)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
cma_set_default_gid_type(struct cma_device * cma_dev,u32 port,enum ib_gid_type default_gid_type)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
cma_get_default_roce_tos(struct cma_device * cma_dev,u32 port)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
cma_set_default_roce_tos(struct cma_device * cma_dev,u32 port,u8 default_roce_tos)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 }
cma_get_ib_dev(struct cma_device * cma_dev)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
cma_comp_exch(struct rdma_id_private * id_priv,enum rdma_cm_state comp,enum rdma_cm_state exch)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
cma_get_ip_ver(const struct cma_hdr * hdr)428 static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
429 {
430 return hdr->ip_version >> 4;
431 }
432
cma_set_ip_ver(struct cma_hdr * hdr,u8 ip_ver)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
cma_src_addr(struct rdma_id_private * id_priv)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
cma_dst_addr(struct rdma_id_private * id_priv)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
cma_igmp_send(struct net_device * ndev,union ib_gid * mgid,bool join)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
compare_netdev_and_ip(int ifindex_a,struct sockaddr * sa,struct id_table_entry * entry_b)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
cma_add_id_to_tree(struct rdma_id_private * node_id_priv)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 *
node_from_ndev_ip(struct rb_root * root,int ifindex,struct sockaddr * sa)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
cma_remove_id_from_tree(struct rdma_id_private * id_priv)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
_cma_attach_to_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev)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
cma_attach_to_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev)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
cma_release_dev(struct rdma_id_private * id_priv)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
cma_family(struct rdma_id_private * id_priv)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
cma_set_default_qkey(struct rdma_id_private * id_priv)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
cma_set_qkey(struct rdma_id_private * id_priv,u32 qkey)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
cma_translate_ib(struct sockaddr_ib * sib,struct rdma_dev_addr * dev_addr)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
cma_translate_addr(struct sockaddr * addr,struct rdma_dev_addr * dev_addr)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 *
cma_validate_port(struct ib_device * device,u32 port,enum ib_gid_type gid_type,union ib_gid * gid,struct rdma_id_private * id_priv)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
cma_bind_sgid_attr(struct rdma_id_private * id_priv,const struct ib_gid_attr * sgid_attr)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 */
cma_acquire_dev_by_src_ip(struct rdma_id_private * id_priv)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 */
cma_ib_acquire_dev(struct rdma_id_private * id_priv,const struct rdma_id_private * listen_id_priv,struct cma_req_info * req)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
cma_iw_acquire_dev(struct rdma_id_private * id_priv,const struct rdma_id_private * listen_id_priv)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 */
cma_resolve_ib_dev(struct rdma_id_private * id_priv)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
cma_id_get(struct rdma_id_private * id_priv)954 static void cma_id_get(struct rdma_id_private *id_priv)
955 {
956 refcount_inc(&id_priv->refcount);
957 }
958
cma_id_put(struct rdma_id_private * id_priv)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 *
__rdma_create_id(struct net * net,rdma_cm_event_handler event_handler,void * context,enum rdma_ucm_port_space ps,enum ib_qp_type qp_type,const struct rdma_id_private * parent)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 *
__rdma_create_kernel_id(struct net * net,rdma_cm_event_handler event_handler,void * context,enum rdma_ucm_port_space ps,enum ib_qp_type qp_type,const char * caller)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
rdma_create_user_id(rdma_cm_event_handler event_handler,void * context,enum rdma_ucm_port_space ps,enum ib_qp_type qp_type)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
cma_init_ud_qp(struct rdma_id_private * id_priv,struct ib_qp * qp)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
cma_init_conn_qp(struct rdma_id_private * id_priv,struct ib_qp * qp)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
rdma_create_qp(struct rdma_cm_id * id,struct ib_pd * pd,struct ib_qp_init_attr * qp_init_attr)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
rdma_destroy_qp(struct rdma_cm_id * id)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
cma_modify_qp_rtr(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)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
cma_modify_qp_rts(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)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
cma_modify_qp_err(struct rdma_id_private * id_priv)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
cma_ib_init_qp_attr(struct rdma_id_private * id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)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
rdma_init_qp_attr(struct rdma_cm_id * id,struct ib_qp_attr * qp_attr,int * qp_attr_mask)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
cma_zero_addr(const struct sockaddr * addr)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
cma_loopback_addr(const struct sockaddr * addr)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
cma_any_addr(const struct sockaddr * addr)1314 static inline bool cma_any_addr(const struct sockaddr *addr)
1315 {
1316 return cma_zero_addr(addr) || cma_loopback_addr(addr);
1317 }
1318
cma_addr_cmp(const struct sockaddr * src,const struct sockaddr * dst)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
cma_port(const struct sockaddr * addr)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
cma_any_port(const struct sockaddr * addr)1368 static inline int cma_any_port(const struct sockaddr *addr)
1369 {
1370 return !cma_port(addr);
1371 }
1372
cma_save_ib_info(struct sockaddr * src_addr,struct sockaddr * dst_addr,const struct rdma_cm_id * listen_id,const struct sa_path_rec * path)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
cma_save_ip4_info(struct sockaddr_in * src_addr,struct sockaddr_in * dst_addr,struct cma_hdr * hdr,__be16 local_port)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
cma_save_ip6_info(struct sockaddr_in6 * src_addr,struct sockaddr_in6 * dst_addr,struct cma_hdr * hdr,__be16 local_port)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
cma_port_from_service_id(__be64 service_id)1454 static u16 cma_port_from_service_id(__be64 service_id)
1455 {
1456 return (u16)be64_to_cpu(service_id);
1457 }
1458
cma_save_ip_info(struct sockaddr * src_addr,struct sockaddr * dst_addr,const struct ib_cm_event * ib_event,__be64 service_id)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
cma_save_net_info(struct sockaddr * src_addr,struct sockaddr * dst_addr,const struct rdma_cm_id * listen_id,const struct ib_cm_event * ib_event,sa_family_t sa_family,__be64 service_id)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
cma_save_req_info(const struct ib_cm_event * ib_event,struct cma_req_info * req)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
validate_ipv4_net_dev(struct net_device * net_dev,const struct sockaddr_in * dst_addr,const struct sockaddr_in * src_addr)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
validate_ipv6_net_dev(struct net_device * net_dev,const struct sockaddr_in6 * dst_addr,const struct sockaddr_in6 * src_addr)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
validate_net_dev(struct net_device * net_dev,const struct sockaddr * daddr,const struct sockaddr * saddr)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 *
roce_get_net_dev_by_cm_event(const struct ib_cm_event * ib_event)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
cma_get_net_dev(const struct ib_cm_event * ib_event,struct cma_req_info * req)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
rdma_ps_from_service_id(__be64 service_id)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
cma_match_private_data(struct rdma_id_private * id_priv,const struct cma_hdr * hdr)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
cma_protocol_roce(const struct rdma_cm_id * id)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
cma_is_req_ipv6_ll(const struct cma_req_info * req)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
cma_match_net_dev(const struct rdma_cm_id * id,const struct net_device * net_dev,const struct cma_req_info * req)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
cma_find_listener(const struct rdma_bind_list * bind_list,const struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event,const struct cma_req_info * req,const struct net_device * net_dev)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 *
cma_ib_id_from_event(struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event,struct cma_req_info * req,struct net_device ** net_dev)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
cma_user_data_offset(struct rdma_id_private * id_priv)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
cma_cancel_route(struct rdma_id_private * id_priv)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
_cma_cancel_listens(struct rdma_id_private * id_priv)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
cma_cancel_listens(struct rdma_id_private * id_priv)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
cma_cancel_operation(struct rdma_id_private * id_priv,enum rdma_cm_state state)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
cma_release_port(struct rdma_id_private * id_priv)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
destroy_mc(struct rdma_id_private * id_priv,struct cma_multicast * mc)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
cma_leave_mc_groups(struct rdma_id_private * id_priv)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
_destroy_id(struct rdma_id_private * id_priv,enum rdma_cm_state state)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 */
destroy_id_handler_unlock(struct rdma_id_private * id_priv)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
rdma_destroy_id(struct rdma_cm_id * id)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
cma_rep_recv(struct rdma_id_private * id_priv)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
cma_set_rep_event_data(struct rdma_cm_event * event,const struct ib_cm_rep_event_param * rep_data,void * private_data)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
cma_cm_event_handler(struct rdma_id_private * id_priv,struct rdma_cm_event * event)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
cma_ib_handler(struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event)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 *
cma_ib_new_conn_id(const struct rdma_cm_id * listen_id,const struct ib_cm_event * ib_event,struct net_device * net_dev)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 *
cma_ib_new_udp_id(const struct rdma_cm_id * listen_id,const struct ib_cm_event * ib_event,struct net_device * net_dev)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
cma_set_req_event_data(struct rdma_cm_event * event,const struct ib_cm_req_event_param * req_data,void * private_data,int offset)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
cma_ib_check_req_qp_type(const struct rdma_cm_id * id,const struct ib_cm_event * ib_event)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
cma_ib_req_handler(struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event)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
rdma_get_service_id(struct rdma_cm_id * id,struct sockaddr * addr)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
rdma_read_gids(struct rdma_cm_id * cm_id,union ib_gid * sgid,union ib_gid * dgid)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
cma_iw_handler(struct iw_cm_id * iw_id,struct iw_cm_event * iw_event)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
iw_conn_req_handler(struct iw_cm_id * cm_id,struct iw_cm_event * iw_event)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
cma_ib_listen(struct rdma_id_private * id_priv)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
cma_iw_listen(struct rdma_id_private * id_priv,int backlog)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
cma_listen_handler(struct rdma_cm_id * id,struct rdma_cm_event * event)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
cma_listen_on_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev,struct rdma_id_private ** to_destroy)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
cma_listen_on_all(struct rdma_id_private * id_priv)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
rdma_set_service_type(struct rdma_cm_id * id,int tos)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 */
rdma_set_ack_timeout(struct rdma_cm_id * id,u8 timeout)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 */
rdma_set_min_rnr_timer(struct rdma_cm_id * id,u8 min_rnr_timer)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
route_set_path_rec_inbound(struct cma_work * work,struct sa_path_rec * path_rec)2828 static void 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;
2838 }
2839
2840 *route->path_rec_inbound = *path_rec;
2841 }
2842
route_set_path_rec_outbound(struct cma_work * work,struct sa_path_rec * path_rec)2843 static void route_set_path_rec_outbound(struct cma_work *work,
2844 struct sa_path_rec *path_rec)
2845 {
2846 struct rdma_route *route = &work->id->id.route;
2847
2848 if (!route->path_rec_outbound) {
2849 route->path_rec_outbound =
2850 kzalloc(sizeof(*route->path_rec_outbound), GFP_KERNEL);
2851 if (!route->path_rec_outbound)
2852 return;
2853 }
2854
2855 *route->path_rec_outbound = *path_rec;
2856 }
2857
cma_query_handler(int status,struct sa_path_rec * path_rec,int num_prs,void * context)2858 static void cma_query_handler(int status, struct sa_path_rec *path_rec,
2859 int num_prs, void *context)
2860 {
2861 struct cma_work *work = context;
2862 struct rdma_route *route;
2863 int i;
2864
2865 route = &work->id->id.route;
2866
2867 if (status)
2868 goto fail;
2869
2870 for (i = 0; i < num_prs; i++) {
2871 if (!path_rec[i].flags || (path_rec[i].flags & IB_PATH_GMP))
2872 *route->path_rec = path_rec[i];
2873 else if (path_rec[i].flags & IB_PATH_INBOUND)
2874 route_set_path_rec_inbound(work, &path_rec[i]);
2875 else if (path_rec[i].flags & IB_PATH_OUTBOUND)
2876 route_set_path_rec_outbound(work, &path_rec[i]);
2877 }
2878 if (!route->path_rec) {
2879 status = -EINVAL;
2880 goto fail;
2881 }
2882
2883 route->num_pri_alt_paths = 1;
2884 queue_work(cma_wq, &work->work);
2885 return;
2886
2887 fail:
2888 work->old_state = RDMA_CM_ROUTE_QUERY;
2889 work->new_state = RDMA_CM_ADDR_RESOLVED;
2890 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2891 work->event.status = status;
2892 pr_debug_ratelimited("RDMA CM: ROUTE_ERROR: failed to query path. status %d\n",
2893 status);
2894 queue_work(cma_wq, &work->work);
2895 }
2896
cma_query_ib_route(struct rdma_id_private * id_priv,unsigned long timeout_ms,struct cma_work * work)2897 static int cma_query_ib_route(struct rdma_id_private *id_priv,
2898 unsigned long timeout_ms, struct cma_work *work)
2899 {
2900 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2901 struct sa_path_rec path_rec;
2902 ib_sa_comp_mask comp_mask;
2903 struct sockaddr_in6 *sin6;
2904 struct sockaddr_ib *sib;
2905
2906 memset(&path_rec, 0, sizeof path_rec);
2907
2908 if (rdma_cap_opa_ah(id_priv->id.device, id_priv->id.port_num))
2909 path_rec.rec_type = SA_PATH_REC_TYPE_OPA;
2910 else
2911 path_rec.rec_type = SA_PATH_REC_TYPE_IB;
2912 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2913 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2914 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2915 path_rec.numb_path = 1;
2916 path_rec.reversible = 1;
2917 path_rec.service_id = rdma_get_service_id(&id_priv->id,
2918 cma_dst_addr(id_priv));
2919
2920 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2921 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2922 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2923
2924 switch (cma_family(id_priv)) {
2925 case AF_INET:
2926 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2927 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2928 break;
2929 case AF_INET6:
2930 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2931 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2932 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2933 break;
2934 case AF_IB:
2935 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2936 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2937 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2938 break;
2939 }
2940
2941 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2942 id_priv->id.port_num, &path_rec,
2943 comp_mask, timeout_ms,
2944 GFP_KERNEL, cma_query_handler,
2945 work, &id_priv->query);
2946
2947 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2948 }
2949
cma_iboe_join_work_handler(struct work_struct * work)2950 static void cma_iboe_join_work_handler(struct work_struct *work)
2951 {
2952 struct cma_multicast *mc =
2953 container_of(work, struct cma_multicast, iboe_join.work);
2954 struct rdma_cm_event *event = &mc->iboe_join.event;
2955 struct rdma_id_private *id_priv = mc->id_priv;
2956 int ret;
2957
2958 mutex_lock(&id_priv->handler_mutex);
2959 if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2960 READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2961 goto out_unlock;
2962
2963 ret = cma_cm_event_handler(id_priv, event);
2964 WARN_ON(ret);
2965
2966 out_unlock:
2967 mutex_unlock(&id_priv->handler_mutex);
2968 if (event->event == RDMA_CM_EVENT_MULTICAST_JOIN)
2969 rdma_destroy_ah_attr(&event->param.ud.ah_attr);
2970 }
2971
cma_work_handler(struct work_struct * _work)2972 static void cma_work_handler(struct work_struct *_work)
2973 {
2974 struct cma_work *work = container_of(_work, struct cma_work, work);
2975 struct rdma_id_private *id_priv = work->id;
2976
2977 mutex_lock(&id_priv->handler_mutex);
2978 if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
2979 READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
2980 goto out_unlock;
2981 if (work->old_state != 0 || work->new_state != 0) {
2982 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2983 goto out_unlock;
2984 }
2985
2986 if (cma_cm_event_handler(id_priv, &work->event)) {
2987 cma_id_put(id_priv);
2988 destroy_id_handler_unlock(id_priv);
2989 goto out_free;
2990 }
2991
2992 out_unlock:
2993 mutex_unlock(&id_priv->handler_mutex);
2994 cma_id_put(id_priv);
2995 out_free:
2996 if (work->event.event == RDMA_CM_EVENT_MULTICAST_JOIN)
2997 rdma_destroy_ah_attr(&work->event.param.ud.ah_attr);
2998 kfree(work);
2999 }
3000
cma_init_resolve_route_work(struct cma_work * work,struct rdma_id_private * id_priv)3001 static void cma_init_resolve_route_work(struct cma_work *work,
3002 struct rdma_id_private *id_priv)
3003 {
3004 work->id = id_priv;
3005 INIT_WORK(&work->work, cma_work_handler);
3006 work->old_state = RDMA_CM_ROUTE_QUERY;
3007 work->new_state = RDMA_CM_ROUTE_RESOLVED;
3008 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
3009 }
3010
enqueue_resolve_addr_work(struct cma_work * work,struct rdma_id_private * id_priv)3011 static void enqueue_resolve_addr_work(struct cma_work *work,
3012 struct rdma_id_private *id_priv)
3013 {
3014 /* Balances with cma_id_put() in cma_work_handler */
3015 cma_id_get(id_priv);
3016
3017 work->id = id_priv;
3018 INIT_WORK(&work->work, cma_work_handler);
3019 work->old_state = RDMA_CM_ADDR_QUERY;
3020 work->new_state = RDMA_CM_ADDR_RESOLVED;
3021 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3022
3023 queue_work(cma_wq, &work->work);
3024 }
3025
cma_resolve_ib_route(struct rdma_id_private * id_priv,unsigned long timeout_ms)3026 static int cma_resolve_ib_route(struct rdma_id_private *id_priv,
3027 unsigned long timeout_ms)
3028 {
3029 struct rdma_route *route = &id_priv->id.route;
3030 struct cma_work *work;
3031 int ret;
3032
3033 work = kzalloc(sizeof *work, GFP_KERNEL);
3034 if (!work)
3035 return -ENOMEM;
3036
3037 cma_init_resolve_route_work(work, id_priv);
3038
3039 if (!route->path_rec)
3040 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
3041 if (!route->path_rec) {
3042 ret = -ENOMEM;
3043 goto err1;
3044 }
3045
3046 ret = cma_query_ib_route(id_priv, timeout_ms, work);
3047 if (ret)
3048 goto err2;
3049
3050 return 0;
3051 err2:
3052 kfree(route->path_rec);
3053 route->path_rec = NULL;
3054 err1:
3055 kfree(work);
3056 return ret;
3057 }
3058
cma_route_gid_type(enum rdma_network_type network_type,unsigned long supported_gids,enum ib_gid_type default_gid)3059 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
3060 unsigned long supported_gids,
3061 enum ib_gid_type default_gid)
3062 {
3063 if ((network_type == RDMA_NETWORK_IPV4 ||
3064 network_type == RDMA_NETWORK_IPV6) &&
3065 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
3066 return IB_GID_TYPE_ROCE_UDP_ENCAP;
3067
3068 return default_gid;
3069 }
3070
3071 /*
3072 * cma_iboe_set_path_rec_l2_fields() is helper function which sets
3073 * path record type based on GID type.
3074 * It also sets up other L2 fields which includes destination mac address
3075 * netdev ifindex, of the path record.
3076 * It returns the netdev of the bound interface for this path record entry.
3077 */
3078 static struct net_device *
cma_iboe_set_path_rec_l2_fields(struct rdma_id_private * id_priv)3079 cma_iboe_set_path_rec_l2_fields(struct rdma_id_private *id_priv)
3080 {
3081 struct rdma_route *route = &id_priv->id.route;
3082 enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
3083 struct rdma_addr *addr = &route->addr;
3084 unsigned long supported_gids;
3085 struct net_device *ndev;
3086
3087 if (!addr->dev_addr.bound_dev_if)
3088 return NULL;
3089
3090 ndev = dev_get_by_index(addr->dev_addr.net,
3091 addr->dev_addr.bound_dev_if);
3092 if (!ndev)
3093 return NULL;
3094
3095 supported_gids = roce_gid_type_mask_support(id_priv->id.device,
3096 id_priv->id.port_num);
3097 gid_type = cma_route_gid_type(addr->dev_addr.network,
3098 supported_gids,
3099 id_priv->gid_type);
3100 /* Use the hint from IP Stack to select GID Type */
3101 if (gid_type < ib_network_to_gid_type(addr->dev_addr.network))
3102 gid_type = ib_network_to_gid_type(addr->dev_addr.network);
3103 route->path_rec->rec_type = sa_conv_gid_to_pathrec_type(gid_type);
3104
3105 route->path_rec->roce.route_resolved = true;
3106 sa_path_set_dmac(route->path_rec, addr->dev_addr.dst_dev_addr);
3107 return ndev;
3108 }
3109
rdma_set_ib_path(struct rdma_cm_id * id,struct sa_path_rec * path_rec)3110 int rdma_set_ib_path(struct rdma_cm_id *id,
3111 struct sa_path_rec *path_rec)
3112 {
3113 struct rdma_id_private *id_priv;
3114 struct net_device *ndev;
3115 int ret;
3116
3117 id_priv = container_of(id, struct rdma_id_private, id);
3118 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3119 RDMA_CM_ROUTE_RESOLVED))
3120 return -EINVAL;
3121
3122 id->route.path_rec = kmemdup(path_rec, sizeof(*path_rec),
3123 GFP_KERNEL);
3124 if (!id->route.path_rec) {
3125 ret = -ENOMEM;
3126 goto err;
3127 }
3128
3129 if (rdma_protocol_roce(id->device, id->port_num)) {
3130 ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
3131 if (!ndev) {
3132 ret = -ENODEV;
3133 goto err_free;
3134 }
3135 dev_put(ndev);
3136 }
3137
3138 id->route.num_pri_alt_paths = 1;
3139 return 0;
3140
3141 err_free:
3142 kfree(id->route.path_rec);
3143 id->route.path_rec = NULL;
3144 err:
3145 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
3146 return ret;
3147 }
3148 EXPORT_SYMBOL(rdma_set_ib_path);
3149
cma_resolve_iw_route(struct rdma_id_private * id_priv)3150 static int cma_resolve_iw_route(struct rdma_id_private *id_priv)
3151 {
3152 struct cma_work *work;
3153
3154 work = kzalloc(sizeof *work, GFP_KERNEL);
3155 if (!work)
3156 return -ENOMEM;
3157
3158 cma_init_resolve_route_work(work, id_priv);
3159 queue_work(cma_wq, &work->work);
3160 return 0;
3161 }
3162
get_vlan_ndev_tc(struct net_device * vlan_ndev,int prio)3163 static int get_vlan_ndev_tc(struct net_device *vlan_ndev, int prio)
3164 {
3165 struct net_device *dev;
3166
3167 dev = vlan_dev_real_dev(vlan_ndev);
3168 if (dev->num_tc)
3169 return netdev_get_prio_tc_map(dev, prio);
3170
3171 return (vlan_dev_get_egress_qos_mask(vlan_ndev, prio) &
3172 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
3173 }
3174
3175 struct iboe_prio_tc_map {
3176 int input_prio;
3177 int output_tc;
3178 bool found;
3179 };
3180
get_lower_vlan_dev_tc(struct net_device * dev,struct netdev_nested_priv * priv)3181 static int get_lower_vlan_dev_tc(struct net_device *dev,
3182 struct netdev_nested_priv *priv)
3183 {
3184 struct iboe_prio_tc_map *map = (struct iboe_prio_tc_map *)priv->data;
3185
3186 if (is_vlan_dev(dev))
3187 map->output_tc = get_vlan_ndev_tc(dev, map->input_prio);
3188 else if (dev->num_tc)
3189 map->output_tc = netdev_get_prio_tc_map(dev, map->input_prio);
3190 else
3191 map->output_tc = 0;
3192 /* We are interested only in first level VLAN device, so always
3193 * return 1 to stop iterating over next level devices.
3194 */
3195 map->found = true;
3196 return 1;
3197 }
3198
iboe_tos_to_sl(struct net_device * ndev,int tos)3199 static int iboe_tos_to_sl(struct net_device *ndev, int tos)
3200 {
3201 struct iboe_prio_tc_map prio_tc_map = {};
3202 int prio = rt_tos2priority(tos);
3203 struct netdev_nested_priv priv;
3204
3205 /* If VLAN device, get it directly from the VLAN netdev */
3206 if (is_vlan_dev(ndev))
3207 return get_vlan_ndev_tc(ndev, prio);
3208
3209 prio_tc_map.input_prio = prio;
3210 priv.data = (void *)&prio_tc_map;
3211 rcu_read_lock();
3212 netdev_walk_all_lower_dev_rcu(ndev,
3213 get_lower_vlan_dev_tc,
3214 &priv);
3215 rcu_read_unlock();
3216 /* If map is found from lower device, use it; Otherwise
3217 * continue with the current netdevice to get priority to tc map.
3218 */
3219 if (prio_tc_map.found)
3220 return prio_tc_map.output_tc;
3221 else if (ndev->num_tc)
3222 return netdev_get_prio_tc_map(ndev, prio);
3223 else
3224 return 0;
3225 }
3226
cma_get_roce_udp_flow_label(struct rdma_id_private * id_priv)3227 static __be32 cma_get_roce_udp_flow_label(struct rdma_id_private *id_priv)
3228 {
3229 struct sockaddr_in6 *addr6;
3230 u16 dport, sport;
3231 u32 hash, fl;
3232
3233 addr6 = (struct sockaddr_in6 *)cma_src_addr(id_priv);
3234 fl = be32_to_cpu(addr6->sin6_flowinfo) & IB_GRH_FLOWLABEL_MASK;
3235 if ((cma_family(id_priv) != AF_INET6) || !fl) {
3236 dport = be16_to_cpu(cma_port(cma_dst_addr(id_priv)));
3237 sport = be16_to_cpu(cma_port(cma_src_addr(id_priv)));
3238 hash = (u32)sport * 31 + dport;
3239 fl = hash & IB_GRH_FLOWLABEL_MASK;
3240 }
3241
3242 return cpu_to_be32(fl);
3243 }
3244
cma_resolve_iboe_route(struct rdma_id_private * id_priv)3245 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
3246 {
3247 struct rdma_route *route = &id_priv->id.route;
3248 struct rdma_addr *addr = &route->addr;
3249 struct cma_work *work;
3250 int ret;
3251 struct net_device *ndev;
3252
3253 u8 default_roce_tos = id_priv->cma_dev->default_roce_tos[id_priv->id.port_num -
3254 rdma_start_port(id_priv->cma_dev->device)];
3255 u8 tos;
3256
3257 mutex_lock(&id_priv->qp_mutex);
3258 tos = id_priv->tos_set ? id_priv->tos : default_roce_tos;
3259 mutex_unlock(&id_priv->qp_mutex);
3260
3261 work = kzalloc(sizeof *work, GFP_KERNEL);
3262 if (!work)
3263 return -ENOMEM;
3264
3265 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
3266 if (!route->path_rec) {
3267 ret = -ENOMEM;
3268 goto err1;
3269 }
3270
3271 route->num_pri_alt_paths = 1;
3272
3273 ndev = cma_iboe_set_path_rec_l2_fields(id_priv);
3274 if (!ndev) {
3275 ret = -ENODEV;
3276 goto err2;
3277 }
3278
3279 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3280 &route->path_rec->sgid);
3281 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
3282 &route->path_rec->dgid);
3283
3284 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
3285 /* TODO: get the hoplimit from the inet/inet6 device */
3286 route->path_rec->hop_limit = addr->dev_addr.hoplimit;
3287 else
3288 route->path_rec->hop_limit = 1;
3289 route->path_rec->reversible = 1;
3290 route->path_rec->pkey = cpu_to_be16(0xffff);
3291 route->path_rec->mtu_selector = IB_SA_EQ;
3292 route->path_rec->sl = iboe_tos_to_sl(ndev, tos);
3293 route->path_rec->traffic_class = tos;
3294 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
3295 route->path_rec->rate_selector = IB_SA_EQ;
3296 route->path_rec->rate = IB_RATE_PORT_CURRENT;
3297 dev_put(ndev);
3298 route->path_rec->packet_life_time_selector = IB_SA_EQ;
3299 /* In case ACK timeout is set, use this value to calculate
3300 * PacketLifeTime. As per IBTA 12.7.34,
3301 * local ACK timeout = (2 * PacketLifeTime + Local CA’s ACK delay).
3302 * Assuming a negligible local ACK delay, we can use
3303 * PacketLifeTime = local ACK timeout/2
3304 * as a reasonable approximation for RoCE networks.
3305 */
3306 mutex_lock(&id_priv->qp_mutex);
3307 if (id_priv->timeout_set && id_priv->timeout)
3308 route->path_rec->packet_life_time = id_priv->timeout - 1;
3309 else
3310 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
3311 mutex_unlock(&id_priv->qp_mutex);
3312
3313 if (!route->path_rec->mtu) {
3314 ret = -EINVAL;
3315 goto err2;
3316 }
3317
3318 if (rdma_protocol_roce_udp_encap(id_priv->id.device,
3319 id_priv->id.port_num))
3320 route->path_rec->flow_label =
3321 cma_get_roce_udp_flow_label(id_priv);
3322
3323 cma_init_resolve_route_work(work, id_priv);
3324 queue_work(cma_wq, &work->work);
3325
3326 return 0;
3327
3328 err2:
3329 kfree(route->path_rec);
3330 route->path_rec = NULL;
3331 route->num_pri_alt_paths = 0;
3332 err1:
3333 kfree(work);
3334 return ret;
3335 }
3336
rdma_resolve_route(struct rdma_cm_id * id,unsigned long timeout_ms)3337 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms)
3338 {
3339 struct rdma_id_private *id_priv;
3340 int ret;
3341
3342 if (!timeout_ms)
3343 return -EINVAL;
3344
3345 id_priv = container_of(id, struct rdma_id_private, id);
3346 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
3347 return -EINVAL;
3348
3349 cma_id_get(id_priv);
3350 if (rdma_cap_ib_sa(id->device, id->port_num))
3351 ret = cma_resolve_ib_route(id_priv, timeout_ms);
3352 else if (rdma_protocol_roce(id->device, id->port_num)) {
3353 ret = cma_resolve_iboe_route(id_priv);
3354 if (!ret)
3355 cma_add_id_to_tree(id_priv);
3356 }
3357 else if (rdma_protocol_iwarp(id->device, id->port_num))
3358 ret = cma_resolve_iw_route(id_priv);
3359 else
3360 ret = -ENOSYS;
3361
3362 if (ret)
3363 goto err;
3364
3365 return 0;
3366 err:
3367 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
3368 cma_id_put(id_priv);
3369 return ret;
3370 }
3371 EXPORT_SYMBOL(rdma_resolve_route);
3372
cma_set_loopback(struct sockaddr * addr)3373 static void cma_set_loopback(struct sockaddr *addr)
3374 {
3375 switch (addr->sa_family) {
3376 case AF_INET:
3377 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
3378 break;
3379 case AF_INET6:
3380 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
3381 0, 0, 0, htonl(1));
3382 break;
3383 default:
3384 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
3385 0, 0, 0, htonl(1));
3386 break;
3387 }
3388 }
3389
cma_bind_loopback(struct rdma_id_private * id_priv)3390 static int cma_bind_loopback(struct rdma_id_private *id_priv)
3391 {
3392 struct cma_device *cma_dev, *cur_dev;
3393 union ib_gid gid;
3394 enum ib_port_state port_state;
3395 unsigned int p;
3396 u16 pkey;
3397 int ret;
3398
3399 cma_dev = NULL;
3400 mutex_lock(&lock);
3401 list_for_each_entry(cur_dev, &dev_list, list) {
3402 if (cma_family(id_priv) == AF_IB &&
3403 !rdma_cap_ib_cm(cur_dev->device, 1))
3404 continue;
3405
3406 if (!cma_dev)
3407 cma_dev = cur_dev;
3408
3409 rdma_for_each_port (cur_dev->device, p) {
3410 if (!ib_get_cached_port_state(cur_dev->device, p, &port_state) &&
3411 port_state == IB_PORT_ACTIVE) {
3412 cma_dev = cur_dev;
3413 goto port_found;
3414 }
3415 }
3416 }
3417
3418 if (!cma_dev) {
3419 ret = -ENODEV;
3420 goto out;
3421 }
3422
3423 p = 1;
3424
3425 port_found:
3426 ret = rdma_query_gid(cma_dev->device, p, 0, &gid);
3427 if (ret)
3428 goto out;
3429
3430 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
3431 if (ret)
3432 goto out;
3433
3434 id_priv->id.route.addr.dev_addr.dev_type =
3435 (rdma_protocol_ib(cma_dev->device, p)) ?
3436 ARPHRD_INFINIBAND : ARPHRD_ETHER;
3437
3438 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3439 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
3440 id_priv->id.port_num = p;
3441 cma_attach_to_dev(id_priv, cma_dev);
3442 rdma_restrack_add(&id_priv->res);
3443 cma_set_loopback(cma_src_addr(id_priv));
3444 out:
3445 mutex_unlock(&lock);
3446 return ret;
3447 }
3448
addr_handler(int status,struct sockaddr * src_addr,struct rdma_dev_addr * dev_addr,void * context)3449 static void addr_handler(int status, struct sockaddr *src_addr,
3450 struct rdma_dev_addr *dev_addr, void *context)
3451 {
3452 struct rdma_id_private *id_priv = context;
3453 struct rdma_cm_event event = {};
3454 struct sockaddr *addr;
3455 struct sockaddr_storage old_addr;
3456
3457 mutex_lock(&id_priv->handler_mutex);
3458 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
3459 RDMA_CM_ADDR_RESOLVED))
3460 goto out;
3461
3462 /*
3463 * Store the previous src address, so that if we fail to acquire
3464 * matching rdma device, old address can be restored back, which helps
3465 * to cancel the cma listen operation correctly.
3466 */
3467 addr = cma_src_addr(id_priv);
3468 memcpy(&old_addr, addr, rdma_addr_size(addr));
3469 memcpy(addr, src_addr, rdma_addr_size(src_addr));
3470 if (!status && !id_priv->cma_dev) {
3471 status = cma_acquire_dev_by_src_ip(id_priv);
3472 if (status)
3473 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to acquire device. status %d\n",
3474 status);
3475 rdma_restrack_add(&id_priv->res);
3476 } else if (status) {
3477 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to resolve IP. status %d\n", status);
3478 }
3479
3480 if (status) {
3481 memcpy(addr, &old_addr,
3482 rdma_addr_size((struct sockaddr *)&old_addr));
3483 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
3484 RDMA_CM_ADDR_BOUND))
3485 goto out;
3486 event.event = RDMA_CM_EVENT_ADDR_ERROR;
3487 event.status = status;
3488 } else
3489 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
3490
3491 if (cma_cm_event_handler(id_priv, &event)) {
3492 destroy_id_handler_unlock(id_priv);
3493 return;
3494 }
3495 out:
3496 mutex_unlock(&id_priv->handler_mutex);
3497 }
3498
cma_resolve_loopback(struct rdma_id_private * id_priv)3499 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
3500 {
3501 struct cma_work *work;
3502 union ib_gid gid;
3503 int ret;
3504
3505 work = kzalloc(sizeof *work, GFP_KERNEL);
3506 if (!work)
3507 return -ENOMEM;
3508
3509 if (!id_priv->cma_dev) {
3510 ret = cma_bind_loopback(id_priv);
3511 if (ret)
3512 goto err;
3513 }
3514
3515 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
3516 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
3517
3518 enqueue_resolve_addr_work(work, id_priv);
3519 return 0;
3520 err:
3521 kfree(work);
3522 return ret;
3523 }
3524
cma_resolve_ib_addr(struct rdma_id_private * id_priv)3525 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
3526 {
3527 struct cma_work *work;
3528 int ret;
3529
3530 work = kzalloc(sizeof *work, GFP_KERNEL);
3531 if (!work)
3532 return -ENOMEM;
3533
3534 if (!id_priv->cma_dev) {
3535 ret = cma_resolve_ib_dev(id_priv);
3536 if (ret)
3537 goto err;
3538 }
3539
3540 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
3541 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
3542
3543 enqueue_resolve_addr_work(work, id_priv);
3544 return 0;
3545 err:
3546 kfree(work);
3547 return ret;
3548 }
3549
cma_bind_addr(struct rdma_cm_id * id,struct sockaddr * src_addr,const struct sockaddr * dst_addr)3550 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3551 const struct sockaddr *dst_addr)
3552 {
3553 struct sockaddr_storage zero_sock = {};
3554
3555 if (src_addr && src_addr->sa_family)
3556 return rdma_bind_addr(id, src_addr);
3557
3558 /*
3559 * When the src_addr is not specified, automatically supply an any addr
3560 */
3561 zero_sock.ss_family = dst_addr->sa_family;
3562 if (IS_ENABLED(CONFIG_IPV6) && dst_addr->sa_family == AF_INET6) {
3563 struct sockaddr_in6 *src_addr6 =
3564 (struct sockaddr_in6 *)&zero_sock;
3565 struct sockaddr_in6 *dst_addr6 =
3566 (struct sockaddr_in6 *)dst_addr;
3567
3568 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
3569 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
3570 id->route.addr.dev_addr.bound_dev_if =
3571 dst_addr6->sin6_scope_id;
3572 } else if (dst_addr->sa_family == AF_IB) {
3573 ((struct sockaddr_ib *)&zero_sock)->sib_pkey =
3574 ((struct sockaddr_ib *)dst_addr)->sib_pkey;
3575 }
3576 return rdma_bind_addr(id, (struct sockaddr *)&zero_sock);
3577 }
3578
3579 /*
3580 * If required, resolve the source address for bind and leave the id_priv in
3581 * state RDMA_CM_ADDR_BOUND. This oddly uses the state to determine the prior
3582 * calls made by ULP, a previously bound ID will not be re-bound and src_addr is
3583 * ignored.
3584 */
resolve_prepare_src(struct rdma_id_private * id_priv,struct sockaddr * src_addr,const struct sockaddr * dst_addr)3585 static int resolve_prepare_src(struct rdma_id_private *id_priv,
3586 struct sockaddr *src_addr,
3587 const struct sockaddr *dst_addr)
3588 {
3589 int ret;
3590
3591 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
3592 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) {
3593 /* For a well behaved ULP state will be RDMA_CM_IDLE */
3594 ret = cma_bind_addr(&id_priv->id, src_addr, dst_addr);
3595 if (ret)
3596 goto err_dst;
3597 if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
3598 RDMA_CM_ADDR_QUERY))) {
3599 ret = -EINVAL;
3600 goto err_dst;
3601 }
3602 }
3603
3604 if (cma_family(id_priv) != dst_addr->sa_family) {
3605 ret = -EINVAL;
3606 goto err_state;
3607 }
3608 return 0;
3609
3610 err_state:
3611 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3612 err_dst:
3613 memset(cma_dst_addr(id_priv), 0, rdma_addr_size(dst_addr));
3614 return ret;
3615 }
3616
rdma_resolve_addr(struct rdma_cm_id * id,struct sockaddr * src_addr,const struct sockaddr * dst_addr,unsigned long timeout_ms)3617 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
3618 const struct sockaddr *dst_addr, unsigned long timeout_ms)
3619 {
3620 struct rdma_id_private *id_priv =
3621 container_of(id, struct rdma_id_private, id);
3622 int ret;
3623
3624 ret = resolve_prepare_src(id_priv, src_addr, dst_addr);
3625 if (ret)
3626 return ret;
3627
3628 if (cma_any_addr(dst_addr)) {
3629 ret = cma_resolve_loopback(id_priv);
3630 } else {
3631 if (dst_addr->sa_family == AF_IB) {
3632 ret = cma_resolve_ib_addr(id_priv);
3633 } else {
3634 /*
3635 * The FSM can return back to RDMA_CM_ADDR_BOUND after
3636 * rdma_resolve_ip() is called, eg through the error
3637 * path in addr_handler(). If this happens the existing
3638 * request must be canceled before issuing a new one.
3639 * Since canceling a request is a bit slow and this
3640 * oddball path is rare, keep track once a request has
3641 * been issued. The track turns out to be a permanent
3642 * state since this is the only cancel as it is
3643 * immediately before rdma_resolve_ip().
3644 */
3645 if (id_priv->used_resolve_ip)
3646 rdma_addr_cancel(&id->route.addr.dev_addr);
3647 else
3648 id_priv->used_resolve_ip = 1;
3649 ret = rdma_resolve_ip(cma_src_addr(id_priv), dst_addr,
3650 &id->route.addr.dev_addr,
3651 timeout_ms, addr_handler,
3652 false, id_priv);
3653 }
3654 }
3655 if (ret)
3656 goto err;
3657
3658 return 0;
3659 err:
3660 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
3661 return ret;
3662 }
3663 EXPORT_SYMBOL(rdma_resolve_addr);
3664
rdma_set_reuseaddr(struct rdma_cm_id * id,int reuse)3665 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
3666 {
3667 struct rdma_id_private *id_priv;
3668 unsigned long flags;
3669 int ret;
3670
3671 id_priv = container_of(id, struct rdma_id_private, id);
3672 spin_lock_irqsave(&id_priv->lock, flags);
3673 if ((reuse && id_priv->state != RDMA_CM_LISTEN) ||
3674 id_priv->state == RDMA_CM_IDLE) {
3675 id_priv->reuseaddr = reuse;
3676 ret = 0;
3677 } else {
3678 ret = -EINVAL;
3679 }
3680 spin_unlock_irqrestore(&id_priv->lock, flags);
3681 return ret;
3682 }
3683 EXPORT_SYMBOL(rdma_set_reuseaddr);
3684
rdma_set_afonly(struct rdma_cm_id * id,int afonly)3685 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
3686 {
3687 struct rdma_id_private *id_priv;
3688 unsigned long flags;
3689 int ret;
3690
3691 id_priv = container_of(id, struct rdma_id_private, id);
3692 spin_lock_irqsave(&id_priv->lock, flags);
3693 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
3694 id_priv->options |= (1 << CMA_OPTION_AFONLY);
3695 id_priv->afonly = afonly;
3696 ret = 0;
3697 } else {
3698 ret = -EINVAL;
3699 }
3700 spin_unlock_irqrestore(&id_priv->lock, flags);
3701 return ret;
3702 }
3703 EXPORT_SYMBOL(rdma_set_afonly);
3704
cma_bind_port(struct rdma_bind_list * bind_list,struct rdma_id_private * id_priv)3705 static void cma_bind_port(struct rdma_bind_list *bind_list,
3706 struct rdma_id_private *id_priv)
3707 {
3708 struct sockaddr *addr;
3709 struct sockaddr_ib *sib;
3710 u64 sid, mask;
3711 __be16 port;
3712
3713 lockdep_assert_held(&lock);
3714
3715 addr = cma_src_addr(id_priv);
3716 port = htons(bind_list->port);
3717
3718 switch (addr->sa_family) {
3719 case AF_INET:
3720 ((struct sockaddr_in *) addr)->sin_port = port;
3721 break;
3722 case AF_INET6:
3723 ((struct sockaddr_in6 *) addr)->sin6_port = port;
3724 break;
3725 case AF_IB:
3726 sib = (struct sockaddr_ib *) addr;
3727 sid = be64_to_cpu(sib->sib_sid);
3728 mask = be64_to_cpu(sib->sib_sid_mask);
3729 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
3730 sib->sib_sid_mask = cpu_to_be64(~0ULL);
3731 break;
3732 }
3733 id_priv->bind_list = bind_list;
3734 hlist_add_head(&id_priv->node, &bind_list->owners);
3735 }
3736
cma_alloc_port(enum rdma_ucm_port_space ps,struct rdma_id_private * id_priv,unsigned short snum)3737 static int cma_alloc_port(enum rdma_ucm_port_space ps,
3738 struct rdma_id_private *id_priv, unsigned short snum)
3739 {
3740 struct rdma_bind_list *bind_list;
3741 int ret;
3742
3743 lockdep_assert_held(&lock);
3744
3745 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
3746 if (!bind_list)
3747 return -ENOMEM;
3748
3749 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
3750 snum);
3751 if (ret < 0)
3752 goto err;
3753
3754 bind_list->ps = ps;
3755 bind_list->port = snum;
3756 cma_bind_port(bind_list, id_priv);
3757 return 0;
3758 err:
3759 kfree(bind_list);
3760 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
3761 }
3762
cma_port_is_unique(struct rdma_bind_list * bind_list,struct rdma_id_private * id_priv)3763 static int cma_port_is_unique(struct rdma_bind_list *bind_list,
3764 struct rdma_id_private *id_priv)
3765 {
3766 struct rdma_id_private *cur_id;
3767 struct sockaddr *daddr = cma_dst_addr(id_priv);
3768 struct sockaddr *saddr = cma_src_addr(id_priv);
3769 __be16 dport = cma_port(daddr);
3770
3771 lockdep_assert_held(&lock);
3772
3773 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3774 struct sockaddr *cur_daddr = cma_dst_addr(cur_id);
3775 struct sockaddr *cur_saddr = cma_src_addr(cur_id);
3776 __be16 cur_dport = cma_port(cur_daddr);
3777
3778 if (id_priv == cur_id)
3779 continue;
3780
3781 /* different dest port -> unique */
3782 if (!cma_any_port(daddr) &&
3783 !cma_any_port(cur_daddr) &&
3784 (dport != cur_dport))
3785 continue;
3786
3787 /* different src address -> unique */
3788 if (!cma_any_addr(saddr) &&
3789 !cma_any_addr(cur_saddr) &&
3790 cma_addr_cmp(saddr, cur_saddr))
3791 continue;
3792
3793 /* different dst address -> unique */
3794 if (!cma_any_addr(daddr) &&
3795 !cma_any_addr(cur_daddr) &&
3796 cma_addr_cmp(daddr, cur_daddr))
3797 continue;
3798
3799 return -EADDRNOTAVAIL;
3800 }
3801 return 0;
3802 }
3803
cma_alloc_any_port(enum rdma_ucm_port_space ps,struct rdma_id_private * id_priv)3804 static int cma_alloc_any_port(enum rdma_ucm_port_space ps,
3805 struct rdma_id_private *id_priv)
3806 {
3807 static unsigned int last_used_port;
3808 int low, high, remaining;
3809 unsigned int rover;
3810 struct net *net = id_priv->id.route.addr.dev_addr.net;
3811
3812 lockdep_assert_held(&lock);
3813
3814 inet_get_local_port_range(net, &low, &high);
3815 remaining = (high - low) + 1;
3816 rover = prandom_u32_max(remaining) + low;
3817 retry:
3818 if (last_used_port != rover) {
3819 struct rdma_bind_list *bind_list;
3820 int ret;
3821
3822 bind_list = cma_ps_find(net, ps, (unsigned short)rover);
3823
3824 if (!bind_list) {
3825 ret = cma_alloc_port(ps, id_priv, rover);
3826 } else {
3827 ret = cma_port_is_unique(bind_list, id_priv);
3828 if (!ret)
3829 cma_bind_port(bind_list, id_priv);
3830 }
3831 /*
3832 * Remember previously used port number in order to avoid
3833 * re-using same port immediately after it is closed.
3834 */
3835 if (!ret)
3836 last_used_port = rover;
3837 if (ret != -EADDRNOTAVAIL)
3838 return ret;
3839 }
3840 if (--remaining) {
3841 rover++;
3842 if ((rover < low) || (rover > high))
3843 rover = low;
3844 goto retry;
3845 }
3846 return -EADDRNOTAVAIL;
3847 }
3848
3849 /*
3850 * Check that the requested port is available. This is called when trying to
3851 * bind to a specific port, or when trying to listen on a bound port. In
3852 * the latter case, the provided id_priv may already be on the bind_list, but
3853 * we still need to check that it's okay to start listening.
3854 */
cma_check_port(struct rdma_bind_list * bind_list,struct rdma_id_private * id_priv,uint8_t reuseaddr)3855 static int cma_check_port(struct rdma_bind_list *bind_list,
3856 struct rdma_id_private *id_priv, uint8_t reuseaddr)
3857 {
3858 struct rdma_id_private *cur_id;
3859 struct sockaddr *addr, *cur_addr;
3860
3861 lockdep_assert_held(&lock);
3862
3863 addr = cma_src_addr(id_priv);
3864 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
3865 if (id_priv == cur_id)
3866 continue;
3867
3868 if (reuseaddr && cur_id->reuseaddr)
3869 continue;
3870
3871 cur_addr = cma_src_addr(cur_id);
3872 if (id_priv->afonly && cur_id->afonly &&
3873 (addr->sa_family != cur_addr->sa_family))
3874 continue;
3875
3876 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
3877 return -EADDRNOTAVAIL;
3878
3879 if (!cma_addr_cmp(addr, cur_addr))
3880 return -EADDRINUSE;
3881 }
3882 return 0;
3883 }
3884
cma_use_port(enum rdma_ucm_port_space ps,struct rdma_id_private * id_priv)3885 static int cma_use_port(enum rdma_ucm_port_space ps,
3886 struct rdma_id_private *id_priv)
3887 {
3888 struct rdma_bind_list *bind_list;
3889 unsigned short snum;
3890 int ret;
3891
3892 lockdep_assert_held(&lock);
3893
3894 snum = ntohs(cma_port(cma_src_addr(id_priv)));
3895 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3896 return -EACCES;
3897
3898 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3899 if (!bind_list) {
3900 ret = cma_alloc_port(ps, id_priv, snum);
3901 } else {
3902 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3903 if (!ret)
3904 cma_bind_port(bind_list, id_priv);
3905 }
3906 return ret;
3907 }
3908
3909 static enum rdma_ucm_port_space
cma_select_inet_ps(struct rdma_id_private * id_priv)3910 cma_select_inet_ps(struct rdma_id_private *id_priv)
3911 {
3912 switch (id_priv->id.ps) {
3913 case RDMA_PS_TCP:
3914 case RDMA_PS_UDP:
3915 case RDMA_PS_IPOIB:
3916 case RDMA_PS_IB:
3917 return id_priv->id.ps;
3918 default:
3919
3920 return 0;
3921 }
3922 }
3923
3924 static enum rdma_ucm_port_space
cma_select_ib_ps(struct rdma_id_private * id_priv)3925 cma_select_ib_ps(struct rdma_id_private *id_priv)
3926 {
3927 enum rdma_ucm_port_space ps = 0;
3928 struct sockaddr_ib *sib;
3929 u64 sid_ps, mask, sid;
3930
3931 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3932 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3933 sid = be64_to_cpu(sib->sib_sid) & mask;
3934
3935 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3936 sid_ps = RDMA_IB_IP_PS_IB;
3937 ps = RDMA_PS_IB;
3938 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3939 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3940 sid_ps = RDMA_IB_IP_PS_TCP;
3941 ps = RDMA_PS_TCP;
3942 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3943 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3944 sid_ps = RDMA_IB_IP_PS_UDP;
3945 ps = RDMA_PS_UDP;
3946 }
3947
3948 if (ps) {
3949 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3950 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3951 be64_to_cpu(sib->sib_sid_mask));
3952 }
3953 return ps;
3954 }
3955
cma_get_port(struct rdma_id_private * id_priv)3956 static int cma_get_port(struct rdma_id_private *id_priv)
3957 {
3958 enum rdma_ucm_port_space ps;
3959 int ret;
3960
3961 if (cma_family(id_priv) != AF_IB)
3962 ps = cma_select_inet_ps(id_priv);
3963 else
3964 ps = cma_select_ib_ps(id_priv);
3965 if (!ps)
3966 return -EPROTONOSUPPORT;
3967
3968 mutex_lock(&lock);
3969 if (cma_any_port(cma_src_addr(id_priv)))
3970 ret = cma_alloc_any_port(ps, id_priv);
3971 else
3972 ret = cma_use_port(ps, id_priv);
3973 mutex_unlock(&lock);
3974
3975 return ret;
3976 }
3977
cma_check_linklocal(struct rdma_dev_addr * dev_addr,struct sockaddr * addr)3978 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3979 struct sockaddr *addr)
3980 {
3981 #if IS_ENABLED(CONFIG_IPV6)
3982 struct sockaddr_in6 *sin6;
3983
3984 if (addr->sa_family != AF_INET6)
3985 return 0;
3986
3987 sin6 = (struct sockaddr_in6 *) addr;
3988
3989 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3990 return 0;
3991
3992 if (!sin6->sin6_scope_id)
3993 return -EINVAL;
3994
3995 dev_addr->bound_dev_if = sin6->sin6_scope_id;
3996 #endif
3997 return 0;
3998 }
3999
rdma_listen(struct rdma_cm_id * id,int backlog)4000 int rdma_listen(struct rdma_cm_id *id, int backlog)
4001 {
4002 struct rdma_id_private *id_priv =
4003 container_of(id, struct rdma_id_private, id);
4004 int ret;
4005
4006 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) {
4007 struct sockaddr_in any_in = {
4008 .sin_family = AF_INET,
4009 .sin_addr.s_addr = htonl(INADDR_ANY),
4010 };
4011
4012 /* For a well behaved ULP state will be RDMA_CM_IDLE */
4013 ret = rdma_bind_addr(id, (struct sockaddr *)&any_in);
4014 if (ret)
4015 return ret;
4016 if (WARN_ON(!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND,
4017 RDMA_CM_LISTEN)))
4018 return -EINVAL;
4019 }
4020
4021 /*
4022 * Once the ID reaches RDMA_CM_LISTEN it is not allowed to be reusable
4023 * any more, and has to be unique in the bind list.
4024 */
4025 if (id_priv->reuseaddr) {
4026 mutex_lock(&lock);
4027 ret = cma_check_port(id_priv->bind_list, id_priv, 0);
4028 if (!ret)
4029 id_priv->reuseaddr = 0;
4030 mutex_unlock(&lock);
4031 if (ret)
4032 goto err;
4033 }
4034
4035 id_priv->backlog = backlog;
4036 if (id_priv->cma_dev) {
4037 if (rdma_cap_ib_cm(id->device, 1)) {
4038 ret = cma_ib_listen(id_priv);
4039 if (ret)
4040 goto err;
4041 } else if (rdma_cap_iw_cm(id->device, 1)) {
4042 ret = cma_iw_listen(id_priv, backlog);
4043 if (ret)
4044 goto err;
4045 } else {
4046 ret = -ENOSYS;
4047 goto err;
4048 }
4049 } else {
4050 ret = cma_listen_on_all(id_priv);
4051 if (ret)
4052 goto err;
4053 }
4054
4055 return 0;
4056 err:
4057 id_priv->backlog = 0;
4058 /*
4059 * All the failure paths that lead here will not allow the req_handler's
4060 * to have run.
4061 */
4062 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
4063 return ret;
4064 }
4065 EXPORT_SYMBOL(rdma_listen);
4066
rdma_bind_addr(struct rdma_cm_id * id,struct sockaddr * addr)4067 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
4068 {
4069 struct rdma_id_private *id_priv;
4070 int ret;
4071 struct sockaddr *daddr;
4072
4073 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
4074 addr->sa_family != AF_IB)
4075 return -EAFNOSUPPORT;
4076
4077 id_priv = container_of(id, struct rdma_id_private, id);
4078 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
4079 return -EINVAL;
4080
4081 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
4082 if (ret)
4083 goto err1;
4084
4085 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
4086 if (!cma_any_addr(addr)) {
4087 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
4088 if (ret)
4089 goto err1;
4090
4091 ret = cma_acquire_dev_by_src_ip(id_priv);
4092 if (ret)
4093 goto err1;
4094 }
4095
4096 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
4097 if (addr->sa_family == AF_INET)
4098 id_priv->afonly = 1;
4099 #if IS_ENABLED(CONFIG_IPV6)
4100 else if (addr->sa_family == AF_INET6) {
4101 struct net *net = id_priv->id.route.addr.dev_addr.net;
4102
4103 id_priv->afonly = net->ipv6.sysctl.bindv6only;
4104 }
4105 #endif
4106 }
4107 daddr = cma_dst_addr(id_priv);
4108 daddr->sa_family = addr->sa_family;
4109
4110 ret = cma_get_port(id_priv);
4111 if (ret)
4112 goto err2;
4113
4114 if (!cma_any_addr(addr))
4115 rdma_restrack_add(&id_priv->res);
4116 return 0;
4117 err2:
4118 if (id_priv->cma_dev)
4119 cma_release_dev(id_priv);
4120 err1:
4121 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
4122 return ret;
4123 }
4124 EXPORT_SYMBOL(rdma_bind_addr);
4125
cma_format_hdr(void * hdr,struct rdma_id_private * id_priv)4126 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
4127 {
4128 struct cma_hdr *cma_hdr;
4129
4130 cma_hdr = hdr;
4131 cma_hdr->cma_version = CMA_VERSION;
4132 if (cma_family(id_priv) == AF_INET) {
4133 struct sockaddr_in *src4, *dst4;
4134
4135 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
4136 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
4137
4138 cma_set_ip_ver(cma_hdr, 4);
4139 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
4140 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
4141 cma_hdr->port = src4->sin_port;
4142 } else if (cma_family(id_priv) == AF_INET6) {
4143 struct sockaddr_in6 *src6, *dst6;
4144
4145 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
4146 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
4147
4148 cma_set_ip_ver(cma_hdr, 6);
4149 cma_hdr->src_addr.ip6 = src6->sin6_addr;
4150 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
4151 cma_hdr->port = src6->sin6_port;
4152 }
4153 return 0;
4154 }
4155
cma_sidr_rep_handler(struct ib_cm_id * cm_id,const struct ib_cm_event * ib_event)4156 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
4157 const struct ib_cm_event *ib_event)
4158 {
4159 struct rdma_id_private *id_priv = cm_id->context;
4160 struct rdma_cm_event event = {};
4161 const struct ib_cm_sidr_rep_event_param *rep =
4162 &ib_event->param.sidr_rep_rcvd;
4163 int ret;
4164
4165 mutex_lock(&id_priv->handler_mutex);
4166 if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4167 goto out;
4168
4169 switch (ib_event->event) {
4170 case IB_CM_SIDR_REQ_ERROR:
4171 event.event = RDMA_CM_EVENT_UNREACHABLE;
4172 event.status = -ETIMEDOUT;
4173 break;
4174 case IB_CM_SIDR_REP_RECEIVED:
4175 event.param.ud.private_data = ib_event->private_data;
4176 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
4177 if (rep->status != IB_SIDR_SUCCESS) {
4178 event.event = RDMA_CM_EVENT_UNREACHABLE;
4179 event.status = ib_event->param.sidr_rep_rcvd.status;
4180 pr_debug_ratelimited("RDMA CM: UNREACHABLE: bad SIDR reply. status %d\n",
4181 event.status);
4182 break;
4183 }
4184 ret = cma_set_qkey(id_priv, rep->qkey);
4185 if (ret) {
4186 pr_debug_ratelimited("RDMA CM: ADDR_ERROR: failed to set qkey. status %d\n", ret);
4187 event.event = RDMA_CM_EVENT_ADDR_ERROR;
4188 event.status = ret;
4189 break;
4190 }
4191 ib_init_ah_attr_from_path(id_priv->id.device,
4192 id_priv->id.port_num,
4193 id_priv->id.route.path_rec,
4194 &event.param.ud.ah_attr,
4195 rep->sgid_attr);
4196 event.param.ud.qp_num = rep->qpn;
4197 event.param.ud.qkey = rep->qkey;
4198 event.event = RDMA_CM_EVENT_ESTABLISHED;
4199 event.status = 0;
4200 break;
4201 default:
4202 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
4203 ib_event->event);
4204 goto out;
4205 }
4206
4207 ret = cma_cm_event_handler(id_priv, &event);
4208
4209 rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4210 if (ret) {
4211 /* Destroy the CM ID by returning a non-zero value. */
4212 id_priv->cm_id.ib = NULL;
4213 destroy_id_handler_unlock(id_priv);
4214 return ret;
4215 }
4216 out:
4217 mutex_unlock(&id_priv->handler_mutex);
4218 return 0;
4219 }
4220
cma_resolve_ib_udp(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4221 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
4222 struct rdma_conn_param *conn_param)
4223 {
4224 struct ib_cm_sidr_req_param req;
4225 struct ib_cm_id *id;
4226 void *private_data;
4227 u8 offset;
4228 int ret;
4229
4230 memset(&req, 0, sizeof req);
4231 offset = cma_user_data_offset(id_priv);
4232 if (check_add_overflow(offset, conn_param->private_data_len, &req.private_data_len))
4233 return -EINVAL;
4234
4235 if (req.private_data_len) {
4236 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4237 if (!private_data)
4238 return -ENOMEM;
4239 } else {
4240 private_data = NULL;
4241 }
4242
4243 if (conn_param->private_data && conn_param->private_data_len)
4244 memcpy(private_data + offset, conn_param->private_data,
4245 conn_param->private_data_len);
4246
4247 if (private_data) {
4248 ret = cma_format_hdr(private_data, id_priv);
4249 if (ret)
4250 goto out;
4251 req.private_data = private_data;
4252 }
4253
4254 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
4255 id_priv);
4256 if (IS_ERR(id)) {
4257 ret = PTR_ERR(id);
4258 goto out;
4259 }
4260 id_priv->cm_id.ib = id;
4261
4262 req.path = id_priv->id.route.path_rec;
4263 req.sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4264 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4265 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
4266 req.max_cm_retries = CMA_MAX_CM_RETRIES;
4267
4268 trace_cm_send_sidr_req(id_priv);
4269 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
4270 if (ret) {
4271 ib_destroy_cm_id(id_priv->cm_id.ib);
4272 id_priv->cm_id.ib = NULL;
4273 }
4274 out:
4275 kfree(private_data);
4276 return ret;
4277 }
4278
cma_connect_ib(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4279 static int cma_connect_ib(struct rdma_id_private *id_priv,
4280 struct rdma_conn_param *conn_param)
4281 {
4282 struct ib_cm_req_param req;
4283 struct rdma_route *route;
4284 void *private_data;
4285 struct ib_cm_id *id;
4286 u8 offset;
4287 int ret;
4288
4289 memset(&req, 0, sizeof req);
4290 offset = cma_user_data_offset(id_priv);
4291 if (check_add_overflow(offset, conn_param->private_data_len, &req.private_data_len))
4292 return -EINVAL;
4293
4294 if (req.private_data_len) {
4295 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
4296 if (!private_data)
4297 return -ENOMEM;
4298 } else {
4299 private_data = NULL;
4300 }
4301
4302 if (conn_param->private_data && conn_param->private_data_len)
4303 memcpy(private_data + offset, conn_param->private_data,
4304 conn_param->private_data_len);
4305
4306 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
4307 if (IS_ERR(id)) {
4308 ret = PTR_ERR(id);
4309 goto out;
4310 }
4311 id_priv->cm_id.ib = id;
4312
4313 route = &id_priv->id.route;
4314 if (private_data) {
4315 ret = cma_format_hdr(private_data, id_priv);
4316 if (ret)
4317 goto out;
4318 req.private_data = private_data;
4319 }
4320
4321 req.primary_path = &route->path_rec[0];
4322 req.primary_path_inbound = route->path_rec_inbound;
4323 req.primary_path_outbound = route->path_rec_outbound;
4324 if (route->num_pri_alt_paths == 2)
4325 req.alternate_path = &route->path_rec[1];
4326
4327 req.ppath_sgid_attr = id_priv->id.route.addr.dev_addr.sgid_attr;
4328 /* Alternate path SGID attribute currently unsupported */
4329 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
4330 req.qp_num = id_priv->qp_num;
4331 req.qp_type = id_priv->id.qp_type;
4332 req.starting_psn = id_priv->seq_num;
4333 req.responder_resources = conn_param->responder_resources;
4334 req.initiator_depth = conn_param->initiator_depth;
4335 req.flow_control = conn_param->flow_control;
4336 req.retry_count = min_t(u8, 7, conn_param->retry_count);
4337 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4338 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4339 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
4340 req.max_cm_retries = CMA_MAX_CM_RETRIES;
4341 req.srq = id_priv->srq ? 1 : 0;
4342 req.ece.vendor_id = id_priv->ece.vendor_id;
4343 req.ece.attr_mod = id_priv->ece.attr_mod;
4344
4345 trace_cm_send_req(id_priv);
4346 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
4347 out:
4348 if (ret && !IS_ERR(id)) {
4349 ib_destroy_cm_id(id);
4350 id_priv->cm_id.ib = NULL;
4351 }
4352
4353 kfree(private_data);
4354 return ret;
4355 }
4356
cma_connect_iw(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4357 static int cma_connect_iw(struct rdma_id_private *id_priv,
4358 struct rdma_conn_param *conn_param)
4359 {
4360 struct iw_cm_id *cm_id;
4361 int ret;
4362 struct iw_cm_conn_param iw_param;
4363
4364 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
4365 if (IS_ERR(cm_id))
4366 return PTR_ERR(cm_id);
4367
4368 mutex_lock(&id_priv->qp_mutex);
4369 cm_id->tos = id_priv->tos;
4370 cm_id->tos_set = id_priv->tos_set;
4371 mutex_unlock(&id_priv->qp_mutex);
4372
4373 id_priv->cm_id.iw = cm_id;
4374
4375 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
4376 rdma_addr_size(cma_src_addr(id_priv)));
4377 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
4378 rdma_addr_size(cma_dst_addr(id_priv)));
4379
4380 ret = cma_modify_qp_rtr(id_priv, conn_param);
4381 if (ret)
4382 goto out;
4383
4384 if (conn_param) {
4385 iw_param.ord = conn_param->initiator_depth;
4386 iw_param.ird = conn_param->responder_resources;
4387 iw_param.private_data = conn_param->private_data;
4388 iw_param.private_data_len = conn_param->private_data_len;
4389 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
4390 } else {
4391 memset(&iw_param, 0, sizeof iw_param);
4392 iw_param.qpn = id_priv->qp_num;
4393 }
4394 ret = iw_cm_connect(cm_id, &iw_param);
4395 out:
4396 if (ret) {
4397 iw_destroy_cm_id(cm_id);
4398 id_priv->cm_id.iw = NULL;
4399 }
4400 return ret;
4401 }
4402
4403 /**
4404 * rdma_connect_locked - Initiate an active connection request.
4405 * @id: Connection identifier to connect.
4406 * @conn_param: Connection information used for connected QPs.
4407 *
4408 * Same as rdma_connect() but can only be called from the
4409 * RDMA_CM_EVENT_ROUTE_RESOLVED handler callback.
4410 */
rdma_connect_locked(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)4411 int rdma_connect_locked(struct rdma_cm_id *id,
4412 struct rdma_conn_param *conn_param)
4413 {
4414 struct rdma_id_private *id_priv =
4415 container_of(id, struct rdma_id_private, id);
4416 int ret;
4417
4418 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
4419 return -EINVAL;
4420
4421 if (!id->qp) {
4422 id_priv->qp_num = conn_param->qp_num;
4423 id_priv->srq = conn_param->srq;
4424 }
4425
4426 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4427 if (id->qp_type == IB_QPT_UD)
4428 ret = cma_resolve_ib_udp(id_priv, conn_param);
4429 else
4430 ret = cma_connect_ib(id_priv, conn_param);
4431 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4432 ret = cma_connect_iw(id_priv, conn_param);
4433 } else {
4434 ret = -ENOSYS;
4435 }
4436 if (ret)
4437 goto err_state;
4438 return 0;
4439 err_state:
4440 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
4441 return ret;
4442 }
4443 EXPORT_SYMBOL(rdma_connect_locked);
4444
4445 /**
4446 * rdma_connect - Initiate an active connection request.
4447 * @id: Connection identifier to connect.
4448 * @conn_param: Connection information used for connected QPs.
4449 *
4450 * Users must have resolved a route for the rdma_cm_id to connect with by having
4451 * called rdma_resolve_route before calling this routine.
4452 *
4453 * This call will either connect to a remote QP or obtain remote QP information
4454 * for unconnected rdma_cm_id's. The actual operation is based on the
4455 * rdma_cm_id's port space.
4456 */
rdma_connect(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)4457 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4458 {
4459 struct rdma_id_private *id_priv =
4460 container_of(id, struct rdma_id_private, id);
4461 int ret;
4462
4463 mutex_lock(&id_priv->handler_mutex);
4464 ret = rdma_connect_locked(id, conn_param);
4465 mutex_unlock(&id_priv->handler_mutex);
4466 return ret;
4467 }
4468 EXPORT_SYMBOL(rdma_connect);
4469
4470 /**
4471 * rdma_connect_ece - Initiate an active connection request with ECE data.
4472 * @id: Connection identifier to connect.
4473 * @conn_param: Connection information used for connected QPs.
4474 * @ece: ECE parameters
4475 *
4476 * See rdma_connect() explanation.
4477 */
rdma_connect_ece(struct rdma_cm_id * id,struct rdma_conn_param * conn_param,struct rdma_ucm_ece * ece)4478 int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4479 struct rdma_ucm_ece *ece)
4480 {
4481 struct rdma_id_private *id_priv =
4482 container_of(id, struct rdma_id_private, id);
4483
4484 id_priv->ece.vendor_id = ece->vendor_id;
4485 id_priv->ece.attr_mod = ece->attr_mod;
4486
4487 return rdma_connect(id, conn_param);
4488 }
4489 EXPORT_SYMBOL(rdma_connect_ece);
4490
cma_accept_ib(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4491 static int cma_accept_ib(struct rdma_id_private *id_priv,
4492 struct rdma_conn_param *conn_param)
4493 {
4494 struct ib_cm_rep_param rep;
4495 int ret;
4496
4497 ret = cma_modify_qp_rtr(id_priv, conn_param);
4498 if (ret)
4499 goto out;
4500
4501 ret = cma_modify_qp_rts(id_priv, conn_param);
4502 if (ret)
4503 goto out;
4504
4505 memset(&rep, 0, sizeof rep);
4506 rep.qp_num = id_priv->qp_num;
4507 rep.starting_psn = id_priv->seq_num;
4508 rep.private_data = conn_param->private_data;
4509 rep.private_data_len = conn_param->private_data_len;
4510 rep.responder_resources = conn_param->responder_resources;
4511 rep.initiator_depth = conn_param->initiator_depth;
4512 rep.failover_accepted = 0;
4513 rep.flow_control = conn_param->flow_control;
4514 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
4515 rep.srq = id_priv->srq ? 1 : 0;
4516 rep.ece.vendor_id = id_priv->ece.vendor_id;
4517 rep.ece.attr_mod = id_priv->ece.attr_mod;
4518
4519 trace_cm_send_rep(id_priv);
4520 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
4521 out:
4522 return ret;
4523 }
4524
cma_accept_iw(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)4525 static int cma_accept_iw(struct rdma_id_private *id_priv,
4526 struct rdma_conn_param *conn_param)
4527 {
4528 struct iw_cm_conn_param iw_param;
4529 int ret;
4530
4531 if (!conn_param)
4532 return -EINVAL;
4533
4534 ret = cma_modify_qp_rtr(id_priv, conn_param);
4535 if (ret)
4536 return ret;
4537
4538 iw_param.ord = conn_param->initiator_depth;
4539 iw_param.ird = conn_param->responder_resources;
4540 iw_param.private_data = conn_param->private_data;
4541 iw_param.private_data_len = conn_param->private_data_len;
4542 if (id_priv->id.qp)
4543 iw_param.qpn = id_priv->qp_num;
4544 else
4545 iw_param.qpn = conn_param->qp_num;
4546
4547 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
4548 }
4549
cma_send_sidr_rep(struct rdma_id_private * id_priv,enum ib_cm_sidr_status status,u32 qkey,const void * private_data,int private_data_len)4550 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
4551 enum ib_cm_sidr_status status, u32 qkey,
4552 const void *private_data, int private_data_len)
4553 {
4554 struct ib_cm_sidr_rep_param rep;
4555 int ret;
4556
4557 memset(&rep, 0, sizeof rep);
4558 rep.status = status;
4559 if (status == IB_SIDR_SUCCESS) {
4560 if (qkey)
4561 ret = cma_set_qkey(id_priv, qkey);
4562 else
4563 ret = cma_set_default_qkey(id_priv);
4564 if (ret)
4565 return ret;
4566 rep.qp_num = id_priv->qp_num;
4567 rep.qkey = id_priv->qkey;
4568
4569 rep.ece.vendor_id = id_priv->ece.vendor_id;
4570 rep.ece.attr_mod = id_priv->ece.attr_mod;
4571 }
4572
4573 rep.private_data = private_data;
4574 rep.private_data_len = private_data_len;
4575
4576 trace_cm_send_sidr_rep(id_priv);
4577 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
4578 }
4579
4580 /**
4581 * rdma_accept - Called to accept a connection request or response.
4582 * @id: Connection identifier associated with the request.
4583 * @conn_param: Information needed to establish the connection. This must be
4584 * provided if accepting a connection request. If accepting a connection
4585 * response, this parameter must be NULL.
4586 *
4587 * Typically, this routine is only called by the listener to accept a connection
4588 * request. It must also be called on the active side of a connection if the
4589 * user is performing their own QP transitions.
4590 *
4591 * In the case of error, a reject message is sent to the remote side and the
4592 * state of the qp associated with the id is modified to error, such that any
4593 * previously posted receive buffers would be flushed.
4594 *
4595 * This function is for use by kernel ULPs and must be called from under the
4596 * handler callback.
4597 */
rdma_accept(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)4598 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
4599 {
4600 struct rdma_id_private *id_priv =
4601 container_of(id, struct rdma_id_private, id);
4602 int ret;
4603
4604 lockdep_assert_held(&id_priv->handler_mutex);
4605
4606 if (READ_ONCE(id_priv->state) != RDMA_CM_CONNECT)
4607 return -EINVAL;
4608
4609 if (!id->qp && conn_param) {
4610 id_priv->qp_num = conn_param->qp_num;
4611 id_priv->srq = conn_param->srq;
4612 }
4613
4614 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4615 if (id->qp_type == IB_QPT_UD) {
4616 if (conn_param)
4617 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4618 conn_param->qkey,
4619 conn_param->private_data,
4620 conn_param->private_data_len);
4621 else
4622 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
4623 0, NULL, 0);
4624 } else {
4625 if (conn_param)
4626 ret = cma_accept_ib(id_priv, conn_param);
4627 else
4628 ret = cma_rep_recv(id_priv);
4629 }
4630 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4631 ret = cma_accept_iw(id_priv, conn_param);
4632 } else {
4633 ret = -ENOSYS;
4634 }
4635 if (ret)
4636 goto reject;
4637
4638 return 0;
4639 reject:
4640 cma_modify_qp_err(id_priv);
4641 rdma_reject(id, NULL, 0, IB_CM_REJ_CONSUMER_DEFINED);
4642 return ret;
4643 }
4644 EXPORT_SYMBOL(rdma_accept);
4645
rdma_accept_ece(struct rdma_cm_id * id,struct rdma_conn_param * conn_param,struct rdma_ucm_ece * ece)4646 int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param,
4647 struct rdma_ucm_ece *ece)
4648 {
4649 struct rdma_id_private *id_priv =
4650 container_of(id, struct rdma_id_private, id);
4651
4652 id_priv->ece.vendor_id = ece->vendor_id;
4653 id_priv->ece.attr_mod = ece->attr_mod;
4654
4655 return rdma_accept(id, conn_param);
4656 }
4657 EXPORT_SYMBOL(rdma_accept_ece);
4658
rdma_lock_handler(struct rdma_cm_id * id)4659 void rdma_lock_handler(struct rdma_cm_id *id)
4660 {
4661 struct rdma_id_private *id_priv =
4662 container_of(id, struct rdma_id_private, id);
4663
4664 mutex_lock(&id_priv->handler_mutex);
4665 }
4666 EXPORT_SYMBOL(rdma_lock_handler);
4667
rdma_unlock_handler(struct rdma_cm_id * id)4668 void rdma_unlock_handler(struct rdma_cm_id *id)
4669 {
4670 struct rdma_id_private *id_priv =
4671 container_of(id, struct rdma_id_private, id);
4672
4673 mutex_unlock(&id_priv->handler_mutex);
4674 }
4675 EXPORT_SYMBOL(rdma_unlock_handler);
4676
rdma_notify(struct rdma_cm_id * id,enum ib_event_type event)4677 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
4678 {
4679 struct rdma_id_private *id_priv;
4680 int ret;
4681
4682 id_priv = container_of(id, struct rdma_id_private, id);
4683 if (!id_priv->cm_id.ib)
4684 return -EINVAL;
4685
4686 switch (id->device->node_type) {
4687 case RDMA_NODE_IB_CA:
4688 ret = ib_cm_notify(id_priv->cm_id.ib, event);
4689 break;
4690 default:
4691 ret = 0;
4692 break;
4693 }
4694 return ret;
4695 }
4696 EXPORT_SYMBOL(rdma_notify);
4697
rdma_reject(struct rdma_cm_id * id,const void * private_data,u8 private_data_len,u8 reason)4698 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
4699 u8 private_data_len, u8 reason)
4700 {
4701 struct rdma_id_private *id_priv;
4702 int ret;
4703
4704 id_priv = container_of(id, struct rdma_id_private, id);
4705 if (!id_priv->cm_id.ib)
4706 return -EINVAL;
4707
4708 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4709 if (id->qp_type == IB_QPT_UD) {
4710 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
4711 private_data, private_data_len);
4712 } else {
4713 trace_cm_send_rej(id_priv);
4714 ret = ib_send_cm_rej(id_priv->cm_id.ib, reason, NULL, 0,
4715 private_data, private_data_len);
4716 }
4717 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4718 ret = iw_cm_reject(id_priv->cm_id.iw,
4719 private_data, private_data_len);
4720 } else {
4721 ret = -ENOSYS;
4722 }
4723
4724 return ret;
4725 }
4726 EXPORT_SYMBOL(rdma_reject);
4727
rdma_disconnect(struct rdma_cm_id * id)4728 int rdma_disconnect(struct rdma_cm_id *id)
4729 {
4730 struct rdma_id_private *id_priv;
4731 int ret;
4732
4733 id_priv = container_of(id, struct rdma_id_private, id);
4734 if (!id_priv->cm_id.ib)
4735 return -EINVAL;
4736
4737 if (rdma_cap_ib_cm(id->device, id->port_num)) {
4738 ret = cma_modify_qp_err(id_priv);
4739 if (ret)
4740 goto out;
4741 /* Initiate or respond to a disconnect. */
4742 trace_cm_disconnect(id_priv);
4743 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) {
4744 if (!ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0))
4745 trace_cm_sent_drep(id_priv);
4746 } else {
4747 trace_cm_sent_dreq(id_priv);
4748 }
4749 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
4750 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
4751 } else
4752 ret = -EINVAL;
4753
4754 out:
4755 return ret;
4756 }
4757 EXPORT_SYMBOL(rdma_disconnect);
4758
cma_make_mc_event(int status,struct rdma_id_private * id_priv,struct ib_sa_multicast * multicast,struct rdma_cm_event * event,struct cma_multicast * mc)4759 static void cma_make_mc_event(int status, struct rdma_id_private *id_priv,
4760 struct ib_sa_multicast *multicast,
4761 struct rdma_cm_event *event,
4762 struct cma_multicast *mc)
4763 {
4764 struct rdma_dev_addr *dev_addr;
4765 enum ib_gid_type gid_type;
4766 struct net_device *ndev;
4767
4768 if (status)
4769 pr_debug_ratelimited("RDMA CM: MULTICAST_ERROR: failed to join multicast. status %d\n",
4770 status);
4771
4772 event->status = status;
4773 event->param.ud.private_data = mc->context;
4774 if (status) {
4775 event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4776 return;
4777 }
4778
4779 dev_addr = &id_priv->id.route.addr.dev_addr;
4780 ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4781 gid_type =
4782 id_priv->cma_dev
4783 ->default_gid_type[id_priv->id.port_num -
4784 rdma_start_port(
4785 id_priv->cma_dev->device)];
4786
4787 event->event = RDMA_CM_EVENT_MULTICAST_JOIN;
4788 if (ib_init_ah_from_mcmember(id_priv->id.device, id_priv->id.port_num,
4789 &multicast->rec, ndev, gid_type,
4790 &event->param.ud.ah_attr)) {
4791 event->event = RDMA_CM_EVENT_MULTICAST_ERROR;
4792 goto out;
4793 }
4794
4795 event->param.ud.qp_num = 0xFFFFFF;
4796 event->param.ud.qkey = id_priv->qkey;
4797
4798 out:
4799 if (ndev)
4800 dev_put(ndev);
4801 }
4802
cma_ib_mc_handler(int status,struct ib_sa_multicast * multicast)4803 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
4804 {
4805 struct cma_multicast *mc = multicast->context;
4806 struct rdma_id_private *id_priv = mc->id_priv;
4807 struct rdma_cm_event event = {};
4808 int ret = 0;
4809
4810 mutex_lock(&id_priv->handler_mutex);
4811 if (READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL ||
4812 READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING)
4813 goto out;
4814
4815 ret = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
4816 if (!ret) {
4817 cma_make_mc_event(status, id_priv, multicast, &event, mc);
4818 ret = cma_cm_event_handler(id_priv, &event);
4819 }
4820 rdma_destroy_ah_attr(&event.param.ud.ah_attr);
4821 WARN_ON(ret);
4822
4823 out:
4824 mutex_unlock(&id_priv->handler_mutex);
4825 return 0;
4826 }
4827
cma_set_mgid(struct rdma_id_private * id_priv,struct sockaddr * addr,union ib_gid * mgid)4828 static void cma_set_mgid(struct rdma_id_private *id_priv,
4829 struct sockaddr *addr, union ib_gid *mgid)
4830 {
4831 unsigned char mc_map[MAX_ADDR_LEN];
4832 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4833 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
4834 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
4835
4836 if (cma_any_addr(addr)) {
4837 memset(mgid, 0, sizeof *mgid);
4838 } else if ((addr->sa_family == AF_INET6) &&
4839 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
4840 0xFF10A01B)) {
4841 /* IPv6 address is an SA assigned MGID. */
4842 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4843 } else if (addr->sa_family == AF_IB) {
4844 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
4845 } else if (addr->sa_family == AF_INET6) {
4846 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
4847 if (id_priv->id.ps == RDMA_PS_UDP)
4848 mc_map[7] = 0x01; /* Use RDMA CM signature */
4849 *mgid = *(union ib_gid *) (mc_map + 4);
4850 } else {
4851 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
4852 if (id_priv->id.ps == RDMA_PS_UDP)
4853 mc_map[7] = 0x01; /* Use RDMA CM signature */
4854 *mgid = *(union ib_gid *) (mc_map + 4);
4855 }
4856 }
4857
cma_join_ib_multicast(struct rdma_id_private * id_priv,struct cma_multicast * mc)4858 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
4859 struct cma_multicast *mc)
4860 {
4861 struct ib_sa_mcmember_rec rec;
4862 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4863 ib_sa_comp_mask comp_mask;
4864 int ret;
4865
4866 ib_addr_get_mgid(dev_addr, &rec.mgid);
4867 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
4868 &rec.mgid, &rec);
4869 if (ret)
4870 return ret;
4871
4872 if (!id_priv->qkey) {
4873 ret = cma_set_default_qkey(id_priv);
4874 if (ret)
4875 return ret;
4876 }
4877
4878 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
4879 rec.qkey = cpu_to_be32(id_priv->qkey);
4880 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
4881 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
4882 rec.join_state = mc->join_state;
4883
4884 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
4885 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
4886 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
4887 IB_SA_MCMEMBER_REC_FLOW_LABEL |
4888 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
4889
4890 if (id_priv->id.ps == RDMA_PS_IPOIB)
4891 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
4892 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
4893 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
4894 IB_SA_MCMEMBER_REC_MTU |
4895 IB_SA_MCMEMBER_REC_HOP_LIMIT;
4896
4897 mc->sa_mc = ib_sa_join_multicast(&sa_client, id_priv->id.device,
4898 id_priv->id.port_num, &rec, comp_mask,
4899 GFP_KERNEL, cma_ib_mc_handler, mc);
4900 return PTR_ERR_OR_ZERO(mc->sa_mc);
4901 }
4902
cma_iboe_set_mgid(struct sockaddr * addr,union ib_gid * mgid,enum ib_gid_type gid_type)4903 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid,
4904 enum ib_gid_type gid_type)
4905 {
4906 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
4907 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
4908
4909 if (cma_any_addr(addr)) {
4910 memset(mgid, 0, sizeof *mgid);
4911 } else if (addr->sa_family == AF_INET6) {
4912 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
4913 } else {
4914 mgid->raw[0] =
4915 (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0xff;
4916 mgid->raw[1] =
4917 (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? 0 : 0x0e;
4918 mgid->raw[2] = 0;
4919 mgid->raw[3] = 0;
4920 mgid->raw[4] = 0;
4921 mgid->raw[5] = 0;
4922 mgid->raw[6] = 0;
4923 mgid->raw[7] = 0;
4924 mgid->raw[8] = 0;
4925 mgid->raw[9] = 0;
4926 mgid->raw[10] = 0xff;
4927 mgid->raw[11] = 0xff;
4928 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
4929 }
4930 }
4931
cma_iboe_join_multicast(struct rdma_id_private * id_priv,struct cma_multicast * mc)4932 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
4933 struct cma_multicast *mc)
4934 {
4935 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
4936 int err = 0;
4937 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
4938 struct net_device *ndev = NULL;
4939 struct ib_sa_multicast ib = {};
4940 enum ib_gid_type gid_type;
4941 bool send_only;
4942
4943 send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
4944
4945 if (cma_zero_addr(addr))
4946 return -EINVAL;
4947
4948 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4949 rdma_start_port(id_priv->cma_dev->device)];
4950 cma_iboe_set_mgid(addr, &ib.rec.mgid, gid_type);
4951
4952 ib.rec.pkey = cpu_to_be16(0xffff);
4953 if (dev_addr->bound_dev_if)
4954 ndev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
4955 if (!ndev)
4956 return -ENODEV;
4957
4958 ib.rec.rate = IB_RATE_PORT_CURRENT;
4959 ib.rec.hop_limit = 1;
4960 ib.rec.mtu = iboe_get_mtu(ndev->mtu);
4961
4962 if (addr->sa_family == AF_INET) {
4963 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4964 ib.rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4965 if (!send_only) {
4966 err = cma_igmp_send(ndev, &ib.rec.mgid,
4967 true);
4968 }
4969 }
4970 } else {
4971 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4972 err = -ENOTSUPP;
4973 }
4974 dev_put(ndev);
4975 if (err || !ib.rec.mtu)
4976 return err ?: -EINVAL;
4977
4978 if (!id_priv->qkey)
4979 cma_set_default_qkey(id_priv);
4980
4981 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4982 &ib.rec.port_gid);
4983 INIT_WORK(&mc->iboe_join.work, cma_iboe_join_work_handler);
4984 cma_make_mc_event(0, id_priv, &ib, &mc->iboe_join.event, mc);
4985 queue_work(cma_wq, &mc->iboe_join.work);
4986 return 0;
4987 }
4988
rdma_join_multicast(struct rdma_cm_id * id,struct sockaddr * addr,u8 join_state,void * context)4989 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4990 u8 join_state, void *context)
4991 {
4992 struct rdma_id_private *id_priv =
4993 container_of(id, struct rdma_id_private, id);
4994 struct cma_multicast *mc;
4995 int ret;
4996
4997 /* Not supported for kernel QPs */
4998 if (WARN_ON(id->qp))
4999 return -EINVAL;
5000
5001 /* ULP is calling this wrong. */
5002 if (!id->device || (READ_ONCE(id_priv->state) != RDMA_CM_ADDR_BOUND &&
5003 READ_ONCE(id_priv->state) != RDMA_CM_ADDR_RESOLVED))
5004 return -EINVAL;
5005
5006 if (id_priv->id.qp_type != IB_QPT_UD)
5007 return -EINVAL;
5008
5009 mc = kzalloc(sizeof(*mc), GFP_KERNEL);
5010 if (!mc)
5011 return -ENOMEM;
5012
5013 memcpy(&mc->addr, addr, rdma_addr_size(addr));
5014 mc->context = context;
5015 mc->id_priv = id_priv;
5016 mc->join_state = join_state;
5017
5018 if (rdma_protocol_roce(id->device, id->port_num)) {
5019 ret = cma_iboe_join_multicast(id_priv, mc);
5020 if (ret)
5021 goto out_err;
5022 } else if (rdma_cap_ib_mcast(id->device, id->port_num)) {
5023 ret = cma_join_ib_multicast(id_priv, mc);
5024 if (ret)
5025 goto out_err;
5026 } else {
5027 ret = -ENOSYS;
5028 goto out_err;
5029 }
5030
5031 spin_lock(&id_priv->lock);
5032 list_add(&mc->list, &id_priv->mc_list);
5033 spin_unlock(&id_priv->lock);
5034
5035 return 0;
5036 out_err:
5037 kfree(mc);
5038 return ret;
5039 }
5040 EXPORT_SYMBOL(rdma_join_multicast);
5041
rdma_leave_multicast(struct rdma_cm_id * id,struct sockaddr * addr)5042 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
5043 {
5044 struct rdma_id_private *id_priv;
5045 struct cma_multicast *mc;
5046
5047 id_priv = container_of(id, struct rdma_id_private, id);
5048 spin_lock_irq(&id_priv->lock);
5049 list_for_each_entry(mc, &id_priv->mc_list, list) {
5050 if (memcmp(&mc->addr, addr, rdma_addr_size(addr)) != 0)
5051 continue;
5052 list_del(&mc->list);
5053 spin_unlock_irq(&id_priv->lock);
5054
5055 WARN_ON(id_priv->cma_dev->device != id->device);
5056 destroy_mc(id_priv, mc);
5057 return;
5058 }
5059 spin_unlock_irq(&id_priv->lock);
5060 }
5061 EXPORT_SYMBOL(rdma_leave_multicast);
5062
cma_netdev_change(struct net_device * ndev,struct rdma_id_private * id_priv)5063 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
5064 {
5065 struct rdma_dev_addr *dev_addr;
5066 struct cma_work *work;
5067
5068 dev_addr = &id_priv->id.route.addr.dev_addr;
5069
5070 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
5071 (net_eq(dev_net(ndev), dev_addr->net)) &&
5072 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
5073 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
5074 ndev->name, &id_priv->id);
5075 work = kzalloc(sizeof *work, GFP_KERNEL);
5076 if (!work)
5077 return -ENOMEM;
5078
5079 INIT_WORK(&work->work, cma_work_handler);
5080 work->id = id_priv;
5081 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
5082 cma_id_get(id_priv);
5083 queue_work(cma_wq, &work->work);
5084 }
5085
5086 return 0;
5087 }
5088
cma_netdev_callback(struct notifier_block * self,unsigned long event,void * ptr)5089 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
5090 void *ptr)
5091 {
5092 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
5093 struct cma_device *cma_dev;
5094 struct rdma_id_private *id_priv;
5095 int ret = NOTIFY_DONE;
5096
5097 if (event != NETDEV_BONDING_FAILOVER)
5098 return NOTIFY_DONE;
5099
5100 if (!netif_is_bond_master(ndev))
5101 return NOTIFY_DONE;
5102
5103 mutex_lock(&lock);
5104 list_for_each_entry(cma_dev, &dev_list, list)
5105 list_for_each_entry(id_priv, &cma_dev->id_list, device_item) {
5106 ret = cma_netdev_change(ndev, id_priv);
5107 if (ret)
5108 goto out;
5109 }
5110
5111 out:
5112 mutex_unlock(&lock);
5113 return ret;
5114 }
5115
cma_netevent_work_handler(struct work_struct * _work)5116 static void cma_netevent_work_handler(struct work_struct *_work)
5117 {
5118 struct rdma_id_private *id_priv =
5119 container_of(_work, struct rdma_id_private, id.net_work);
5120 struct rdma_cm_event event = {};
5121
5122 mutex_lock(&id_priv->handler_mutex);
5123
5124 if (READ_ONCE(id_priv->state) == RDMA_CM_DESTROYING ||
5125 READ_ONCE(id_priv->state) == RDMA_CM_DEVICE_REMOVAL)
5126 goto out_unlock;
5127
5128 event.event = RDMA_CM_EVENT_UNREACHABLE;
5129 event.status = -ETIMEDOUT;
5130
5131 if (cma_cm_event_handler(id_priv, &event)) {
5132 __acquire(&id_priv->handler_mutex);
5133 id_priv->cm_id.ib = NULL;
5134 cma_id_put(id_priv);
5135 destroy_id_handler_unlock(id_priv);
5136 return;
5137 }
5138
5139 out_unlock:
5140 mutex_unlock(&id_priv->handler_mutex);
5141 cma_id_put(id_priv);
5142 }
5143
cma_netevent_callback(struct notifier_block * self,unsigned long event,void * ctx)5144 static int cma_netevent_callback(struct notifier_block *self,
5145 unsigned long event, void *ctx)
5146 {
5147 struct id_table_entry *ips_node = NULL;
5148 struct rdma_id_private *current_id;
5149 struct neighbour *neigh = ctx;
5150 unsigned long flags;
5151
5152 if (event != NETEVENT_NEIGH_UPDATE)
5153 return NOTIFY_DONE;
5154
5155 spin_lock_irqsave(&id_table_lock, flags);
5156 if (neigh->tbl->family == AF_INET6) {
5157 struct sockaddr_in6 neigh_sock_6;
5158
5159 neigh_sock_6.sin6_family = AF_INET6;
5160 neigh_sock_6.sin6_addr = *(struct in6_addr *)neigh->primary_key;
5161 ips_node = node_from_ndev_ip(&id_table, neigh->dev->ifindex,
5162 (struct sockaddr *)&neigh_sock_6);
5163 } else if (neigh->tbl->family == AF_INET) {
5164 struct sockaddr_in neigh_sock_4;
5165
5166 neigh_sock_4.sin_family = AF_INET;
5167 neigh_sock_4.sin_addr.s_addr = *(__be32 *)(neigh->primary_key);
5168 ips_node = node_from_ndev_ip(&id_table, neigh->dev->ifindex,
5169 (struct sockaddr *)&neigh_sock_4);
5170 } else
5171 goto out;
5172
5173 if (!ips_node)
5174 goto out;
5175
5176 list_for_each_entry(current_id, &ips_node->id_list, id_list_entry) {
5177 if (!memcmp(current_id->id.route.addr.dev_addr.dst_dev_addr,
5178 neigh->ha, ETH_ALEN))
5179 continue;
5180 INIT_WORK(¤t_id->id.net_work, cma_netevent_work_handler);
5181 cma_id_get(current_id);
5182 queue_work(cma_wq, ¤t_id->id.net_work);
5183 }
5184 out:
5185 spin_unlock_irqrestore(&id_table_lock, flags);
5186 return NOTIFY_DONE;
5187 }
5188
5189 static struct notifier_block cma_nb = {
5190 .notifier_call = cma_netdev_callback
5191 };
5192
5193 static struct notifier_block cma_netevent_cb = {
5194 .notifier_call = cma_netevent_callback
5195 };
5196
cma_send_device_removal_put(struct rdma_id_private * id_priv)5197 static void cma_send_device_removal_put(struct rdma_id_private *id_priv)
5198 {
5199 struct rdma_cm_event event = { .event = RDMA_CM_EVENT_DEVICE_REMOVAL };
5200 enum rdma_cm_state state;
5201 unsigned long flags;
5202
5203 mutex_lock(&id_priv->handler_mutex);
5204 /* Record that we want to remove the device */
5205 spin_lock_irqsave(&id_priv->lock, flags);
5206 state = id_priv->state;
5207 if (state == RDMA_CM_DESTROYING || state == RDMA_CM_DEVICE_REMOVAL) {
5208 spin_unlock_irqrestore(&id_priv->lock, flags);
5209 mutex_unlock(&id_priv->handler_mutex);
5210 cma_id_put(id_priv);
5211 return;
5212 }
5213 id_priv->state = RDMA_CM_DEVICE_REMOVAL;
5214 spin_unlock_irqrestore(&id_priv->lock, flags);
5215
5216 if (cma_cm_event_handler(id_priv, &event)) {
5217 /*
5218 * At this point the ULP promises it won't call
5219 * rdma_destroy_id() concurrently
5220 */
5221 cma_id_put(id_priv);
5222 mutex_unlock(&id_priv->handler_mutex);
5223 trace_cm_id_destroy(id_priv);
5224 _destroy_id(id_priv, state);
5225 return;
5226 }
5227 mutex_unlock(&id_priv->handler_mutex);
5228
5229 /*
5230 * If this races with destroy then the thread that first assigns state
5231 * to a destroying does the cancel.
5232 */
5233 cma_cancel_operation(id_priv, state);
5234 cma_id_put(id_priv);
5235 }
5236
cma_process_remove(struct cma_device * cma_dev)5237 static void cma_process_remove(struct cma_device *cma_dev)
5238 {
5239 mutex_lock(&lock);
5240 while (!list_empty(&cma_dev->id_list)) {
5241 struct rdma_id_private *id_priv = list_first_entry(
5242 &cma_dev->id_list, struct rdma_id_private, device_item);
5243
5244 list_del_init(&id_priv->listen_item);
5245 list_del_init(&id_priv->device_item);
5246 cma_id_get(id_priv);
5247 mutex_unlock(&lock);
5248
5249 cma_send_device_removal_put(id_priv);
5250
5251 mutex_lock(&lock);
5252 }
5253 mutex_unlock(&lock);
5254
5255 cma_dev_put(cma_dev);
5256 wait_for_completion(&cma_dev->comp);
5257 }
5258
cma_supported(struct ib_device * device)5259 static bool cma_supported(struct ib_device *device)
5260 {
5261 u32 i;
5262
5263 rdma_for_each_port(device, i) {
5264 if (rdma_cap_ib_cm(device, i) || rdma_cap_iw_cm(device, i))
5265 return true;
5266 }
5267 return false;
5268 }
5269
cma_add_one(struct ib_device * device)5270 static int cma_add_one(struct ib_device *device)
5271 {
5272 struct rdma_id_private *to_destroy;
5273 struct cma_device *cma_dev;
5274 struct rdma_id_private *id_priv;
5275 unsigned long supported_gids = 0;
5276 int ret;
5277 u32 i;
5278
5279 if (!cma_supported(device))
5280 return -EOPNOTSUPP;
5281
5282 cma_dev = kmalloc(sizeof(*cma_dev), GFP_KERNEL);
5283 if (!cma_dev)
5284 return -ENOMEM;
5285
5286 cma_dev->device = device;
5287 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
5288 sizeof(*cma_dev->default_gid_type),
5289 GFP_KERNEL);
5290 if (!cma_dev->default_gid_type) {
5291 ret = -ENOMEM;
5292 goto free_cma_dev;
5293 }
5294
5295 cma_dev->default_roce_tos = kcalloc(device->phys_port_cnt,
5296 sizeof(*cma_dev->default_roce_tos),
5297 GFP_KERNEL);
5298 if (!cma_dev->default_roce_tos) {
5299 ret = -ENOMEM;
5300 goto free_gid_type;
5301 }
5302
5303 rdma_for_each_port (device, i) {
5304 supported_gids = roce_gid_type_mask_support(device, i);
5305 WARN_ON(!supported_gids);
5306 if (supported_gids & (1 << CMA_PREFERRED_ROCE_GID_TYPE))
5307 cma_dev->default_gid_type[i - rdma_start_port(device)] =
5308 CMA_PREFERRED_ROCE_GID_TYPE;
5309 else
5310 cma_dev->default_gid_type[i - rdma_start_port(device)] =
5311 find_first_bit(&supported_gids, BITS_PER_LONG);
5312 cma_dev->default_roce_tos[i - rdma_start_port(device)] = 0;
5313 }
5314
5315 init_completion(&cma_dev->comp);
5316 refcount_set(&cma_dev->refcount, 1);
5317 INIT_LIST_HEAD(&cma_dev->id_list);
5318 ib_set_client_data(device, &cma_client, cma_dev);
5319
5320 mutex_lock(&lock);
5321 list_add_tail(&cma_dev->list, &dev_list);
5322 list_for_each_entry(id_priv, &listen_any_list, listen_any_item) {
5323 ret = cma_listen_on_dev(id_priv, cma_dev, &to_destroy);
5324 if (ret)
5325 goto free_listen;
5326 }
5327 mutex_unlock(&lock);
5328
5329 trace_cm_add_one(device);
5330 return 0;
5331
5332 free_listen:
5333 list_del(&cma_dev->list);
5334 mutex_unlock(&lock);
5335
5336 /* cma_process_remove() will delete to_destroy */
5337 cma_process_remove(cma_dev);
5338 kfree(cma_dev->default_roce_tos);
5339 free_gid_type:
5340 kfree(cma_dev->default_gid_type);
5341
5342 free_cma_dev:
5343 kfree(cma_dev);
5344 return ret;
5345 }
5346
cma_remove_one(struct ib_device * device,void * client_data)5347 static void cma_remove_one(struct ib_device *device, void *client_data)
5348 {
5349 struct cma_device *cma_dev = client_data;
5350
5351 trace_cm_remove_one(device);
5352
5353 mutex_lock(&lock);
5354 list_del(&cma_dev->list);
5355 mutex_unlock(&lock);
5356
5357 cma_process_remove(cma_dev);
5358 kfree(cma_dev->default_roce_tos);
5359 kfree(cma_dev->default_gid_type);
5360 kfree(cma_dev);
5361 }
5362
cma_init_net(struct net * net)5363 static int cma_init_net(struct net *net)
5364 {
5365 struct cma_pernet *pernet = cma_pernet(net);
5366
5367 xa_init(&pernet->tcp_ps);
5368 xa_init(&pernet->udp_ps);
5369 xa_init(&pernet->ipoib_ps);
5370 xa_init(&pernet->ib_ps);
5371
5372 return 0;
5373 }
5374
cma_exit_net(struct net * net)5375 static void cma_exit_net(struct net *net)
5376 {
5377 struct cma_pernet *pernet = cma_pernet(net);
5378
5379 WARN_ON(!xa_empty(&pernet->tcp_ps));
5380 WARN_ON(!xa_empty(&pernet->udp_ps));
5381 WARN_ON(!xa_empty(&pernet->ipoib_ps));
5382 WARN_ON(!xa_empty(&pernet->ib_ps));
5383 }
5384
5385 static struct pernet_operations cma_pernet_operations = {
5386 .init = cma_init_net,
5387 .exit = cma_exit_net,
5388 .id = &cma_pernet_id,
5389 .size = sizeof(struct cma_pernet),
5390 };
5391
cma_init(void)5392 static int __init cma_init(void)
5393 {
5394 int ret;
5395
5396 /*
5397 * There is a rare lock ordering dependency in cma_netdev_callback()
5398 * that only happens when bonding is enabled. Teach lockdep that rtnl
5399 * must never be nested under lock so it can find these without having
5400 * to test with bonding.
5401 */
5402 if (IS_ENABLED(CONFIG_LOCKDEP)) {
5403 rtnl_lock();
5404 mutex_lock(&lock);
5405 mutex_unlock(&lock);
5406 rtnl_unlock();
5407 }
5408
5409 cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
5410 if (!cma_wq)
5411 return -ENOMEM;
5412
5413 ret = register_pernet_subsys(&cma_pernet_operations);
5414 if (ret)
5415 goto err_wq;
5416
5417 ib_sa_register_client(&sa_client);
5418 register_netdevice_notifier(&cma_nb);
5419 register_netevent_notifier(&cma_netevent_cb);
5420
5421 ret = ib_register_client(&cma_client);
5422 if (ret)
5423 goto err;
5424
5425 ret = cma_configfs_init();
5426 if (ret)
5427 goto err_ib;
5428
5429 return 0;
5430
5431 err_ib:
5432 ib_unregister_client(&cma_client);
5433 err:
5434 unregister_netevent_notifier(&cma_netevent_cb);
5435 unregister_netdevice_notifier(&cma_nb);
5436 ib_sa_unregister_client(&sa_client);
5437 unregister_pernet_subsys(&cma_pernet_operations);
5438 err_wq:
5439 destroy_workqueue(cma_wq);
5440 return ret;
5441 }
5442
cma_cleanup(void)5443 static void __exit cma_cleanup(void)
5444 {
5445 cma_configfs_exit();
5446 ib_unregister_client(&cma_client);
5447 unregister_netevent_notifier(&cma_netevent_cb);
5448 unregister_netdevice_notifier(&cma_nb);
5449 ib_sa_unregister_client(&sa_client);
5450 unregister_pernet_subsys(&cma_pernet_operations);
5451 destroy_workqueue(cma_wq);
5452 }
5453
5454 module_init(cma_init);
5455 module_exit(cma_cleanup);
5456