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