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