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