1 /*
2 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
6 *
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
12 *
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
16 *
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
34 */
35
36 #include <linux/completion.h>
37 #include <linux/in.h>
38 #include <linux/in6.h>
39 #include <linux/mutex.h>
40 #include <linux/random.h>
41 #include <linux/igmp.h>
42 #include <linux/idr.h>
43 #include <linux/inetdevice.h>
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <net/route.h>
47
48 #include <net/net_namespace.h>
49 #include <net/netns/generic.h>
50 #include <net/tcp.h>
51 #include <net/ipv6.h>
52 #include <net/ip_fib.h>
53 #include <net/ip6_route.h>
54
55 #include <rdma/rdma_cm.h>
56 #include <rdma/rdma_cm_ib.h>
57 #include <rdma/rdma_netlink.h>
58 #include <rdma/ib.h>
59 #include <rdma/ib_cache.h>
60 #include <rdma/ib_cm.h>
61 #include <rdma/ib_sa.h>
62 #include <rdma/iw_cm.h>
63
64 #include "core_priv.h"
65
66 MODULE_AUTHOR("Sean Hefty");
67 MODULE_DESCRIPTION("Generic RDMA CM Agent");
68 MODULE_LICENSE("Dual BSD/GPL");
69
70 #define CMA_CM_RESPONSE_TIMEOUT 20
71 #define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
72 #define CMA_MAX_CM_RETRIES 15
73 #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
74 #define CMA_IBOE_PACKET_LIFETIME 18
75
76 static const char * const cma_events[] = {
77 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
78 [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
79 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
80 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
81 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
82 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
83 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
84 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
85 [RDMA_CM_EVENT_REJECTED] = "rejected",
86 [RDMA_CM_EVENT_ESTABLISHED] = "established",
87 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
88 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
89 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
90 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
91 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
92 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
93 };
94
rdma_event_msg(enum rdma_cm_event_type event)95 const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
96 {
97 size_t index = event;
98
99 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
100 cma_events[index] : "unrecognized event";
101 }
102 EXPORT_SYMBOL(rdma_event_msg);
103
104 static void cma_add_one(struct ib_device *device);
105 static void cma_remove_one(struct ib_device *device, void *client_data);
106
107 static struct ib_client cma_client = {
108 .name = "cma",
109 .add = cma_add_one,
110 .remove = cma_remove_one
111 };
112
113 static struct ib_sa_client sa_client;
114 static struct rdma_addr_client addr_client;
115 static LIST_HEAD(dev_list);
116 static LIST_HEAD(listen_any_list);
117 static DEFINE_MUTEX(lock);
118 static struct workqueue_struct *cma_wq;
119 static int cma_pernet_id;
120
121 struct cma_pernet {
122 struct idr tcp_ps;
123 struct idr udp_ps;
124 struct idr ipoib_ps;
125 struct idr ib_ps;
126 };
127
cma_pernet(struct net * net)128 static struct cma_pernet *cma_pernet(struct net *net)
129 {
130 return net_generic(net, cma_pernet_id);
131 }
132
cma_pernet_idr(struct net * net,enum rdma_port_space ps)133 static struct idr *cma_pernet_idr(struct net *net, enum rdma_port_space ps)
134 {
135 struct cma_pernet *pernet = cma_pernet(net);
136
137 switch (ps) {
138 case RDMA_PS_TCP:
139 return &pernet->tcp_ps;
140 case RDMA_PS_UDP:
141 return &pernet->udp_ps;
142 case RDMA_PS_IPOIB:
143 return &pernet->ipoib_ps;
144 case RDMA_PS_IB:
145 return &pernet->ib_ps;
146 default:
147 return NULL;
148 }
149 }
150
151 struct cma_device {
152 struct list_head list;
153 struct ib_device *device;
154 struct completion comp;
155 atomic_t refcount;
156 struct list_head id_list;
157 enum ib_gid_type *default_gid_type;
158 };
159
160 struct rdma_bind_list {
161 enum rdma_port_space ps;
162 struct hlist_head owners;
163 unsigned short port;
164 };
165
166 struct class_port_info_context {
167 struct ib_class_port_info *class_port_info;
168 struct ib_device *device;
169 struct completion done;
170 struct ib_sa_query *sa_query;
171 u8 port_num;
172 };
173
cma_ps_alloc(struct net * net,enum rdma_port_space ps,struct rdma_bind_list * bind_list,int snum)174 static int cma_ps_alloc(struct net *net, enum rdma_port_space ps,
175 struct rdma_bind_list *bind_list, int snum)
176 {
177 struct idr *idr = cma_pernet_idr(net, ps);
178
179 return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL);
180 }
181
cma_ps_find(struct net * net,enum rdma_port_space ps,int snum)182 static struct rdma_bind_list *cma_ps_find(struct net *net,
183 enum rdma_port_space ps, int snum)
184 {
185 struct idr *idr = cma_pernet_idr(net, ps);
186
187 return idr_find(idr, snum);
188 }
189
cma_ps_remove(struct net * net,enum rdma_port_space ps,int snum)190 static void cma_ps_remove(struct net *net, enum rdma_port_space ps, int snum)
191 {
192 struct idr *idr = cma_pernet_idr(net, ps);
193
194 idr_remove(idr, snum);
195 }
196
197 enum {
198 CMA_OPTION_AFONLY,
199 };
200
cma_ref_dev(struct cma_device * cma_dev)201 void cma_ref_dev(struct cma_device *cma_dev)
202 {
203 atomic_inc(&cma_dev->refcount);
204 }
205
cma_enum_devices_by_ibdev(cma_device_filter filter,void * cookie)206 struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter,
207 void *cookie)
208 {
209 struct cma_device *cma_dev;
210 struct cma_device *found_cma_dev = NULL;
211
212 mutex_lock(&lock);
213
214 list_for_each_entry(cma_dev, &dev_list, list)
215 if (filter(cma_dev->device, cookie)) {
216 found_cma_dev = cma_dev;
217 break;
218 }
219
220 if (found_cma_dev)
221 cma_ref_dev(found_cma_dev);
222 mutex_unlock(&lock);
223 return found_cma_dev;
224 }
225
cma_get_default_gid_type(struct cma_device * cma_dev,unsigned int port)226 int cma_get_default_gid_type(struct cma_device *cma_dev,
227 unsigned int port)
228 {
229 if (port < rdma_start_port(cma_dev->device) ||
230 port > rdma_end_port(cma_dev->device))
231 return -EINVAL;
232
233 return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
234 }
235
cma_set_default_gid_type(struct cma_device * cma_dev,unsigned int port,enum ib_gid_type default_gid_type)236 int cma_set_default_gid_type(struct cma_device *cma_dev,
237 unsigned int port,
238 enum ib_gid_type default_gid_type)
239 {
240 unsigned long supported_gids;
241
242 if (port < rdma_start_port(cma_dev->device) ||
243 port > rdma_end_port(cma_dev->device))
244 return -EINVAL;
245
246 supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
247
248 if (!(supported_gids & 1 << default_gid_type))
249 return -EINVAL;
250
251 cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
252 default_gid_type;
253
254 return 0;
255 }
256
cma_get_ib_dev(struct cma_device * cma_dev)257 struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
258 {
259 return cma_dev->device;
260 }
261
262 /*
263 * Device removal can occur at anytime, so we need extra handling to
264 * serialize notifying the user of device removal with other callbacks.
265 * We do this by disabling removal notification while a callback is in process,
266 * and reporting it after the callback completes.
267 */
268 struct rdma_id_private {
269 struct rdma_cm_id id;
270
271 struct rdma_bind_list *bind_list;
272 struct hlist_node node;
273 struct list_head list; /* listen_any_list or cma_device.list */
274 struct list_head listen_list; /* per device listens */
275 struct cma_device *cma_dev;
276 struct list_head mc_list;
277
278 int internal_id;
279 enum rdma_cm_state state;
280 spinlock_t lock;
281 struct mutex qp_mutex;
282
283 struct completion comp;
284 atomic_t refcount;
285 struct mutex handler_mutex;
286
287 int backlog;
288 int timeout_ms;
289 struct ib_sa_query *query;
290 int query_id;
291 union {
292 struct ib_cm_id *ib;
293 struct iw_cm_id *iw;
294 } cm_id;
295
296 u32 seq_num;
297 u32 qkey;
298 u32 qp_num;
299 pid_t owner;
300 u32 options;
301 u8 srq;
302 u8 tos;
303 u8 reuseaddr;
304 u8 afonly;
305 enum ib_gid_type gid_type;
306 };
307
308 struct cma_multicast {
309 struct rdma_id_private *id_priv;
310 union {
311 struct ib_sa_multicast *ib;
312 } multicast;
313 struct list_head list;
314 void *context;
315 struct sockaddr_storage addr;
316 struct kref mcref;
317 bool igmp_joined;
318 u8 join_state;
319 };
320
321 struct cma_work {
322 struct work_struct work;
323 struct rdma_id_private *id;
324 enum rdma_cm_state old_state;
325 enum rdma_cm_state new_state;
326 struct rdma_cm_event event;
327 };
328
329 struct cma_ndev_work {
330 struct work_struct work;
331 struct rdma_id_private *id;
332 struct rdma_cm_event event;
333 };
334
335 struct iboe_mcast_work {
336 struct work_struct work;
337 struct rdma_id_private *id;
338 struct cma_multicast *mc;
339 };
340
341 union cma_ip_addr {
342 struct in6_addr ip6;
343 struct {
344 __be32 pad[3];
345 __be32 addr;
346 } ip4;
347 };
348
349 struct cma_hdr {
350 u8 cma_version;
351 u8 ip_version; /* IP version: 7:4 */
352 __be16 port;
353 union cma_ip_addr src_addr;
354 union cma_ip_addr dst_addr;
355 };
356
357 #define CMA_VERSION 0x00
358
359 struct cma_req_info {
360 struct ib_device *device;
361 int port;
362 union ib_gid local_gid;
363 __be64 service_id;
364 u16 pkey;
365 bool has_gid:1;
366 };
367
cma_comp(struct rdma_id_private * id_priv,enum rdma_cm_state comp)368 static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
369 {
370 unsigned long flags;
371 int ret;
372
373 spin_lock_irqsave(&id_priv->lock, flags);
374 ret = (id_priv->state == comp);
375 spin_unlock_irqrestore(&id_priv->lock, flags);
376 return ret;
377 }
378
cma_comp_exch(struct rdma_id_private * id_priv,enum rdma_cm_state comp,enum rdma_cm_state exch)379 static int cma_comp_exch(struct rdma_id_private *id_priv,
380 enum rdma_cm_state comp, enum rdma_cm_state exch)
381 {
382 unsigned long flags;
383 int ret;
384
385 spin_lock_irqsave(&id_priv->lock, flags);
386 if ((ret = (id_priv->state == comp)))
387 id_priv->state = exch;
388 spin_unlock_irqrestore(&id_priv->lock, flags);
389 return ret;
390 }
391
cma_exch(struct rdma_id_private * id_priv,enum rdma_cm_state exch)392 static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
393 enum rdma_cm_state exch)
394 {
395 unsigned long flags;
396 enum rdma_cm_state old;
397
398 spin_lock_irqsave(&id_priv->lock, flags);
399 old = id_priv->state;
400 id_priv->state = exch;
401 spin_unlock_irqrestore(&id_priv->lock, flags);
402 return old;
403 }
404
cma_get_ip_ver(const struct cma_hdr * hdr)405 static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
406 {
407 return hdr->ip_version >> 4;
408 }
409
cma_set_ip_ver(struct cma_hdr * hdr,u8 ip_ver)410 static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
411 {
412 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
413 }
414
cma_igmp_send(struct net_device * ndev,union ib_gid * mgid,bool join)415 static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
416 {
417 struct in_device *in_dev = NULL;
418
419 if (ndev) {
420 rtnl_lock();
421 in_dev = __in_dev_get_rtnl(ndev);
422 if (in_dev) {
423 if (join)
424 ip_mc_inc_group(in_dev,
425 *(__be32 *)(mgid->raw + 12));
426 else
427 ip_mc_dec_group(in_dev,
428 *(__be32 *)(mgid->raw + 12));
429 }
430 rtnl_unlock();
431 }
432 return (in_dev) ? 0 : -ENODEV;
433 }
434
_cma_attach_to_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev)435 static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
436 struct cma_device *cma_dev)
437 {
438 cma_ref_dev(cma_dev);
439 id_priv->cma_dev = cma_dev;
440 id_priv->gid_type = 0;
441 id_priv->id.device = cma_dev->device;
442 id_priv->id.route.addr.dev_addr.transport =
443 rdma_node_get_transport(cma_dev->device->node_type);
444 list_add_tail(&id_priv->list, &cma_dev->id_list);
445 }
446
cma_attach_to_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev)447 static void cma_attach_to_dev(struct rdma_id_private *id_priv,
448 struct cma_device *cma_dev)
449 {
450 _cma_attach_to_dev(id_priv, cma_dev);
451 id_priv->gid_type =
452 cma_dev->default_gid_type[id_priv->id.port_num -
453 rdma_start_port(cma_dev->device)];
454 }
455
cma_deref_dev(struct cma_device * cma_dev)456 void cma_deref_dev(struct cma_device *cma_dev)
457 {
458 if (atomic_dec_and_test(&cma_dev->refcount))
459 complete(&cma_dev->comp);
460 }
461
release_mc(struct kref * kref)462 static inline void release_mc(struct kref *kref)
463 {
464 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
465
466 kfree(mc->multicast.ib);
467 kfree(mc);
468 }
469
cma_release_dev(struct rdma_id_private * id_priv)470 static void cma_release_dev(struct rdma_id_private *id_priv)
471 {
472 mutex_lock(&lock);
473 list_del(&id_priv->list);
474 cma_deref_dev(id_priv->cma_dev);
475 id_priv->cma_dev = NULL;
476 mutex_unlock(&lock);
477 }
478
cma_src_addr(struct rdma_id_private * id_priv)479 static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
480 {
481 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
482 }
483
cma_dst_addr(struct rdma_id_private * id_priv)484 static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
485 {
486 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
487 }
488
cma_family(struct rdma_id_private * id_priv)489 static inline unsigned short cma_family(struct rdma_id_private *id_priv)
490 {
491 return id_priv->id.route.addr.src_addr.ss_family;
492 }
493
cma_set_qkey(struct rdma_id_private * id_priv,u32 qkey)494 static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
495 {
496 struct ib_sa_mcmember_rec rec;
497 int ret = 0;
498
499 if (id_priv->qkey) {
500 if (qkey && id_priv->qkey != qkey)
501 return -EINVAL;
502 return 0;
503 }
504
505 if (qkey) {
506 id_priv->qkey = qkey;
507 return 0;
508 }
509
510 switch (id_priv->id.ps) {
511 case RDMA_PS_UDP:
512 case RDMA_PS_IB:
513 id_priv->qkey = RDMA_UDP_QKEY;
514 break;
515 case RDMA_PS_IPOIB:
516 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
517 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
518 id_priv->id.port_num, &rec.mgid,
519 &rec);
520 if (!ret)
521 id_priv->qkey = be32_to_cpu(rec.qkey);
522 break;
523 default:
524 break;
525 }
526 return ret;
527 }
528
cma_translate_ib(struct sockaddr_ib * sib,struct rdma_dev_addr * dev_addr)529 static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
530 {
531 dev_addr->dev_type = ARPHRD_INFINIBAND;
532 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
533 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
534 }
535
cma_translate_addr(struct sockaddr * addr,struct rdma_dev_addr * dev_addr)536 static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
537 {
538 int ret;
539
540 if (addr->sa_family != AF_IB) {
541 ret = rdma_translate_ip(addr, dev_addr, NULL);
542 } else {
543 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
544 ret = 0;
545 }
546
547 return ret;
548 }
549
cma_validate_port(struct ib_device * device,u8 port,enum ib_gid_type gid_type,union ib_gid * gid,int dev_type,int bound_if_index)550 static inline int cma_validate_port(struct ib_device *device, u8 port,
551 enum ib_gid_type gid_type,
552 union ib_gid *gid, int dev_type,
553 int bound_if_index)
554 {
555 int ret = -ENODEV;
556 struct net_device *ndev = NULL;
557
558 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
559 return ret;
560
561 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
562 return ret;
563
564 if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
565 ndev = dev_get_by_index(&init_net, bound_if_index);
566 if (ndev && ndev->flags & IFF_LOOPBACK) {
567 pr_info("detected loopback device\n");
568 dev_put(ndev);
569
570 if (!device->get_netdev)
571 return -EOPNOTSUPP;
572
573 ndev = device->get_netdev(device, port);
574 if (!ndev)
575 return -ENODEV;
576 }
577 } else {
578 gid_type = IB_GID_TYPE_IB;
579 }
580
581 ret = ib_find_cached_gid_by_port(device, gid, gid_type, port,
582 ndev, NULL);
583
584 if (ndev)
585 dev_put(ndev);
586
587 return ret;
588 }
589
cma_acquire_dev(struct rdma_id_private * id_priv,struct rdma_id_private * listen_id_priv)590 static int cma_acquire_dev(struct rdma_id_private *id_priv,
591 struct rdma_id_private *listen_id_priv)
592 {
593 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
594 struct cma_device *cma_dev;
595 union ib_gid gid, iboe_gid, *gidp;
596 int ret = -ENODEV;
597 u8 port;
598
599 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
600 id_priv->id.ps == RDMA_PS_IPOIB)
601 return -EINVAL;
602
603 mutex_lock(&lock);
604 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
605 &iboe_gid);
606
607 memcpy(&gid, dev_addr->src_dev_addr +
608 rdma_addr_gid_offset(dev_addr), sizeof gid);
609
610 if (listen_id_priv) {
611 cma_dev = listen_id_priv->cma_dev;
612 port = listen_id_priv->id.port_num;
613 gidp = rdma_protocol_roce(cma_dev->device, port) ?
614 &iboe_gid : &gid;
615
616 ret = cma_validate_port(cma_dev->device, port,
617 rdma_protocol_ib(cma_dev->device, port) ?
618 IB_GID_TYPE_IB :
619 listen_id_priv->gid_type, gidp,
620 dev_addr->dev_type,
621 dev_addr->bound_dev_if);
622 if (!ret) {
623 id_priv->id.port_num = port;
624 goto out;
625 }
626 }
627
628 list_for_each_entry(cma_dev, &dev_list, list) {
629 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
630 if (listen_id_priv &&
631 listen_id_priv->cma_dev == cma_dev &&
632 listen_id_priv->id.port_num == port)
633 continue;
634
635 gidp = rdma_protocol_roce(cma_dev->device, port) ?
636 &iboe_gid : &gid;
637
638 ret = cma_validate_port(cma_dev->device, port,
639 rdma_protocol_ib(cma_dev->device, port) ?
640 IB_GID_TYPE_IB :
641 cma_dev->default_gid_type[port - 1],
642 gidp, dev_addr->dev_type,
643 dev_addr->bound_dev_if);
644 if (!ret) {
645 id_priv->id.port_num = port;
646 goto out;
647 }
648 }
649 }
650
651 out:
652 if (!ret)
653 cma_attach_to_dev(id_priv, cma_dev);
654
655 mutex_unlock(&lock);
656 return ret;
657 }
658
659 /*
660 * Select the source IB device and address to reach the destination IB address.
661 */
cma_resolve_ib_dev(struct rdma_id_private * id_priv)662 static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
663 {
664 struct cma_device *cma_dev, *cur_dev;
665 struct sockaddr_ib *addr;
666 union ib_gid gid, sgid, *dgid;
667 u16 pkey, index;
668 u8 p;
669 int i;
670
671 cma_dev = NULL;
672 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
673 dgid = (union ib_gid *) &addr->sib_addr;
674 pkey = ntohs(addr->sib_pkey);
675
676 list_for_each_entry(cur_dev, &dev_list, list) {
677 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
678 if (!rdma_cap_af_ib(cur_dev->device, p))
679 continue;
680
681 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
682 continue;
683
684 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i,
685 &gid, NULL);
686 i++) {
687 if (!memcmp(&gid, dgid, sizeof(gid))) {
688 cma_dev = cur_dev;
689 sgid = gid;
690 id_priv->id.port_num = p;
691 goto found;
692 }
693
694 if (!cma_dev && (gid.global.subnet_prefix ==
695 dgid->global.subnet_prefix)) {
696 cma_dev = cur_dev;
697 sgid = gid;
698 id_priv->id.port_num = p;
699 }
700 }
701 }
702 }
703
704 if (!cma_dev)
705 return -ENODEV;
706
707 found:
708 cma_attach_to_dev(id_priv, cma_dev);
709 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
710 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
711 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
712 return 0;
713 }
714
cma_deref_id(struct rdma_id_private * id_priv)715 static void cma_deref_id(struct rdma_id_private *id_priv)
716 {
717 if (atomic_dec_and_test(&id_priv->refcount))
718 complete(&id_priv->comp);
719 }
720
rdma_create_id(struct net * net,rdma_cm_event_handler event_handler,void * context,enum rdma_port_space ps,enum ib_qp_type qp_type)721 struct rdma_cm_id *rdma_create_id(struct net *net,
722 rdma_cm_event_handler event_handler,
723 void *context, enum rdma_port_space ps,
724 enum ib_qp_type qp_type)
725 {
726 struct rdma_id_private *id_priv;
727
728 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
729 if (!id_priv)
730 return ERR_PTR(-ENOMEM);
731
732 id_priv->owner = task_pid_nr(current);
733 id_priv->state = RDMA_CM_IDLE;
734 id_priv->id.context = context;
735 id_priv->id.event_handler = event_handler;
736 id_priv->id.ps = ps;
737 id_priv->id.qp_type = qp_type;
738 spin_lock_init(&id_priv->lock);
739 mutex_init(&id_priv->qp_mutex);
740 init_completion(&id_priv->comp);
741 atomic_set(&id_priv->refcount, 1);
742 mutex_init(&id_priv->handler_mutex);
743 INIT_LIST_HEAD(&id_priv->listen_list);
744 INIT_LIST_HEAD(&id_priv->mc_list);
745 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
746 id_priv->id.route.addr.dev_addr.net = get_net(net);
747 id_priv->seq_num &= 0x00ffffff;
748
749 return &id_priv->id;
750 }
751 EXPORT_SYMBOL(rdma_create_id);
752
cma_init_ud_qp(struct rdma_id_private * id_priv,struct ib_qp * qp)753 static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
754 {
755 struct ib_qp_attr qp_attr;
756 int qp_attr_mask, ret;
757
758 qp_attr.qp_state = IB_QPS_INIT;
759 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
760 if (ret)
761 return ret;
762
763 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
764 if (ret)
765 return ret;
766
767 qp_attr.qp_state = IB_QPS_RTR;
768 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
769 if (ret)
770 return ret;
771
772 qp_attr.qp_state = IB_QPS_RTS;
773 qp_attr.sq_psn = 0;
774 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
775
776 return ret;
777 }
778
cma_init_conn_qp(struct rdma_id_private * id_priv,struct ib_qp * qp)779 static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
780 {
781 struct ib_qp_attr qp_attr;
782 int qp_attr_mask, ret;
783
784 qp_attr.qp_state = IB_QPS_INIT;
785 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
786 if (ret)
787 return ret;
788
789 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
790 }
791
rdma_create_qp(struct rdma_cm_id * id,struct ib_pd * pd,struct ib_qp_init_attr * qp_init_attr)792 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
793 struct ib_qp_init_attr *qp_init_attr)
794 {
795 struct rdma_id_private *id_priv;
796 struct ib_qp *qp;
797 int ret;
798
799 id_priv = container_of(id, struct rdma_id_private, id);
800 if (id->device != pd->device)
801 return -EINVAL;
802
803 qp_init_attr->port_num = id->port_num;
804 qp = ib_create_qp(pd, qp_init_attr);
805 if (IS_ERR(qp))
806 return PTR_ERR(qp);
807
808 if (id->qp_type == IB_QPT_UD)
809 ret = cma_init_ud_qp(id_priv, qp);
810 else
811 ret = cma_init_conn_qp(id_priv, qp);
812 if (ret)
813 goto err;
814
815 id->qp = qp;
816 id_priv->qp_num = qp->qp_num;
817 id_priv->srq = (qp->srq != NULL);
818 return 0;
819 err:
820 ib_destroy_qp(qp);
821 return ret;
822 }
823 EXPORT_SYMBOL(rdma_create_qp);
824
rdma_destroy_qp(struct rdma_cm_id * id)825 void rdma_destroy_qp(struct rdma_cm_id *id)
826 {
827 struct rdma_id_private *id_priv;
828
829 id_priv = container_of(id, struct rdma_id_private, id);
830 mutex_lock(&id_priv->qp_mutex);
831 ib_destroy_qp(id_priv->id.qp);
832 id_priv->id.qp = NULL;
833 mutex_unlock(&id_priv->qp_mutex);
834 }
835 EXPORT_SYMBOL(rdma_destroy_qp);
836
cma_modify_qp_rtr(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)837 static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
838 struct rdma_conn_param *conn_param)
839 {
840 struct ib_qp_attr qp_attr;
841 int qp_attr_mask, ret;
842 union ib_gid sgid;
843
844 mutex_lock(&id_priv->qp_mutex);
845 if (!id_priv->id.qp) {
846 ret = 0;
847 goto out;
848 }
849
850 /* Need to update QP attributes from default values. */
851 qp_attr.qp_state = IB_QPS_INIT;
852 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
853 if (ret)
854 goto out;
855
856 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
857 if (ret)
858 goto out;
859
860 qp_attr.qp_state = IB_QPS_RTR;
861 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
862 if (ret)
863 goto out;
864
865 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
866 qp_attr.ah_attr.grh.sgid_index, &sgid, NULL);
867 if (ret)
868 goto out;
869
870 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
871
872 if (conn_param)
873 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
874 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
875 out:
876 mutex_unlock(&id_priv->qp_mutex);
877 return ret;
878 }
879
cma_modify_qp_rts(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)880 static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
881 struct rdma_conn_param *conn_param)
882 {
883 struct ib_qp_attr qp_attr;
884 int qp_attr_mask, ret;
885
886 mutex_lock(&id_priv->qp_mutex);
887 if (!id_priv->id.qp) {
888 ret = 0;
889 goto out;
890 }
891
892 qp_attr.qp_state = IB_QPS_RTS;
893 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
894 if (ret)
895 goto out;
896
897 if (conn_param)
898 qp_attr.max_rd_atomic = conn_param->initiator_depth;
899 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
900 out:
901 mutex_unlock(&id_priv->qp_mutex);
902 return ret;
903 }
904
cma_modify_qp_err(struct rdma_id_private * id_priv)905 static int cma_modify_qp_err(struct rdma_id_private *id_priv)
906 {
907 struct ib_qp_attr qp_attr;
908 int ret;
909
910 mutex_lock(&id_priv->qp_mutex);
911 if (!id_priv->id.qp) {
912 ret = 0;
913 goto out;
914 }
915
916 qp_attr.qp_state = IB_QPS_ERR;
917 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
918 out:
919 mutex_unlock(&id_priv->qp_mutex);
920 return ret;
921 }
922
cma_ib_init_qp_attr(struct rdma_id_private * id_priv,struct ib_qp_attr * qp_attr,int * qp_attr_mask)923 static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
924 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
925 {
926 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
927 int ret;
928 u16 pkey;
929
930 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
931 pkey = 0xffff;
932 else
933 pkey = ib_addr_get_pkey(dev_addr);
934
935 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
936 pkey, &qp_attr->pkey_index);
937 if (ret)
938 return ret;
939
940 qp_attr->port_num = id_priv->id.port_num;
941 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
942
943 if (id_priv->id.qp_type == IB_QPT_UD) {
944 ret = cma_set_qkey(id_priv, 0);
945 if (ret)
946 return ret;
947
948 qp_attr->qkey = id_priv->qkey;
949 *qp_attr_mask |= IB_QP_QKEY;
950 } else {
951 qp_attr->qp_access_flags = 0;
952 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
953 }
954 return 0;
955 }
956
rdma_init_qp_attr(struct rdma_cm_id * id,struct ib_qp_attr * qp_attr,int * qp_attr_mask)957 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
958 int *qp_attr_mask)
959 {
960 struct rdma_id_private *id_priv;
961 int ret = 0;
962
963 id_priv = container_of(id, struct rdma_id_private, id);
964 if (rdma_cap_ib_cm(id->device, id->port_num)) {
965 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
966 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
967 else
968 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
969 qp_attr_mask);
970
971 if (qp_attr->qp_state == IB_QPS_RTR)
972 qp_attr->rq_psn = id_priv->seq_num;
973 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
974 if (!id_priv->cm_id.iw) {
975 qp_attr->qp_access_flags = 0;
976 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
977 } else
978 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
979 qp_attr_mask);
980 qp_attr->port_num = id_priv->id.port_num;
981 *qp_attr_mask |= IB_QP_PORT;
982 } else
983 ret = -ENOSYS;
984
985 return ret;
986 }
987 EXPORT_SYMBOL(rdma_init_qp_attr);
988
cma_zero_addr(struct sockaddr * addr)989 static inline int cma_zero_addr(struct sockaddr *addr)
990 {
991 switch (addr->sa_family) {
992 case AF_INET:
993 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
994 case AF_INET6:
995 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
996 case AF_IB:
997 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
998 default:
999 return 0;
1000 }
1001 }
1002
cma_loopback_addr(struct sockaddr * addr)1003 static inline int cma_loopback_addr(struct sockaddr *addr)
1004 {
1005 switch (addr->sa_family) {
1006 case AF_INET:
1007 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
1008 case AF_INET6:
1009 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
1010 case AF_IB:
1011 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
1012 default:
1013 return 0;
1014 }
1015 }
1016
cma_any_addr(struct sockaddr * addr)1017 static inline int cma_any_addr(struct sockaddr *addr)
1018 {
1019 return cma_zero_addr(addr) || cma_loopback_addr(addr);
1020 }
1021
cma_addr_cmp(struct sockaddr * src,struct sockaddr * dst)1022 static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
1023 {
1024 if (src->sa_family != dst->sa_family)
1025 return -1;
1026
1027 switch (src->sa_family) {
1028 case AF_INET:
1029 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
1030 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
1031 case AF_INET6:
1032 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
1033 &((struct sockaddr_in6 *) dst)->sin6_addr);
1034 default:
1035 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1036 &((struct sockaddr_ib *) dst)->sib_addr);
1037 }
1038 }
1039
cma_port(struct sockaddr * addr)1040 static __be16 cma_port(struct sockaddr *addr)
1041 {
1042 struct sockaddr_ib *sib;
1043
1044 switch (addr->sa_family) {
1045 case AF_INET:
1046 return ((struct sockaddr_in *) addr)->sin_port;
1047 case AF_INET6:
1048 return ((struct sockaddr_in6 *) addr)->sin6_port;
1049 case AF_IB:
1050 sib = (struct sockaddr_ib *) addr;
1051 return htons((u16) (be64_to_cpu(sib->sib_sid) &
1052 be64_to_cpu(sib->sib_sid_mask)));
1053 default:
1054 return 0;
1055 }
1056 }
1057
cma_any_port(struct sockaddr * addr)1058 static inline int cma_any_port(struct sockaddr *addr)
1059 {
1060 return !cma_port(addr);
1061 }
1062
cma_save_ib_info(struct sockaddr * src_addr,struct sockaddr * dst_addr,struct rdma_cm_id * listen_id,struct ib_sa_path_rec * path)1063 static void cma_save_ib_info(struct sockaddr *src_addr,
1064 struct sockaddr *dst_addr,
1065 struct rdma_cm_id *listen_id,
1066 struct ib_sa_path_rec *path)
1067 {
1068 struct sockaddr_ib *listen_ib, *ib;
1069
1070 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
1071 if (src_addr) {
1072 ib = (struct sockaddr_ib *)src_addr;
1073 ib->sib_family = AF_IB;
1074 if (path) {
1075 ib->sib_pkey = path->pkey;
1076 ib->sib_flowinfo = path->flow_label;
1077 memcpy(&ib->sib_addr, &path->sgid, 16);
1078 ib->sib_sid = path->service_id;
1079 ib->sib_scope_id = 0;
1080 } else {
1081 ib->sib_pkey = listen_ib->sib_pkey;
1082 ib->sib_flowinfo = listen_ib->sib_flowinfo;
1083 ib->sib_addr = listen_ib->sib_addr;
1084 ib->sib_sid = listen_ib->sib_sid;
1085 ib->sib_scope_id = listen_ib->sib_scope_id;
1086 }
1087 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
1088 }
1089 if (dst_addr) {
1090 ib = (struct sockaddr_ib *)dst_addr;
1091 ib->sib_family = AF_IB;
1092 if (path) {
1093 ib->sib_pkey = path->pkey;
1094 ib->sib_flowinfo = path->flow_label;
1095 memcpy(&ib->sib_addr, &path->dgid, 16);
1096 }
1097 }
1098 }
1099
cma_save_ip4_info(struct sockaddr_in * src_addr,struct sockaddr_in * dst_addr,struct cma_hdr * hdr,__be16 local_port)1100 static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1101 struct sockaddr_in *dst_addr,
1102 struct cma_hdr *hdr,
1103 __be16 local_port)
1104 {
1105 if (src_addr) {
1106 *src_addr = (struct sockaddr_in) {
1107 .sin_family = AF_INET,
1108 .sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1109 .sin_port = local_port,
1110 };
1111 }
1112
1113 if (dst_addr) {
1114 *dst_addr = (struct sockaddr_in) {
1115 .sin_family = AF_INET,
1116 .sin_addr.s_addr = hdr->src_addr.ip4.addr,
1117 .sin_port = hdr->port,
1118 };
1119 }
1120 }
1121
cma_save_ip6_info(struct sockaddr_in6 * src_addr,struct sockaddr_in6 * dst_addr,struct cma_hdr * hdr,__be16 local_port)1122 static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1123 struct sockaddr_in6 *dst_addr,
1124 struct cma_hdr *hdr,
1125 __be16 local_port)
1126 {
1127 if (src_addr) {
1128 *src_addr = (struct sockaddr_in6) {
1129 .sin6_family = AF_INET6,
1130 .sin6_addr = hdr->dst_addr.ip6,
1131 .sin6_port = local_port,
1132 };
1133 }
1134
1135 if (dst_addr) {
1136 *dst_addr = (struct sockaddr_in6) {
1137 .sin6_family = AF_INET6,
1138 .sin6_addr = hdr->src_addr.ip6,
1139 .sin6_port = hdr->port,
1140 };
1141 }
1142 }
1143
cma_port_from_service_id(__be64 service_id)1144 static u16 cma_port_from_service_id(__be64 service_id)
1145 {
1146 return (u16)be64_to_cpu(service_id);
1147 }
1148
cma_save_ip_info(struct sockaddr * src_addr,struct sockaddr * dst_addr,struct ib_cm_event * ib_event,__be64 service_id)1149 static int cma_save_ip_info(struct sockaddr *src_addr,
1150 struct sockaddr *dst_addr,
1151 struct ib_cm_event *ib_event,
1152 __be64 service_id)
1153 {
1154 struct cma_hdr *hdr;
1155 __be16 port;
1156
1157 hdr = ib_event->private_data;
1158 if (hdr->cma_version != CMA_VERSION)
1159 return -EINVAL;
1160
1161 port = htons(cma_port_from_service_id(service_id));
1162
1163 switch (cma_get_ip_ver(hdr)) {
1164 case 4:
1165 cma_save_ip4_info((struct sockaddr_in *)src_addr,
1166 (struct sockaddr_in *)dst_addr, hdr, port);
1167 break;
1168 case 6:
1169 cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1170 (struct sockaddr_in6 *)dst_addr, hdr, port);
1171 break;
1172 default:
1173 return -EAFNOSUPPORT;
1174 }
1175
1176 return 0;
1177 }
1178
cma_save_net_info(struct sockaddr * src_addr,struct sockaddr * dst_addr,struct rdma_cm_id * listen_id,struct ib_cm_event * ib_event,sa_family_t sa_family,__be64 service_id)1179 static int cma_save_net_info(struct sockaddr *src_addr,
1180 struct sockaddr *dst_addr,
1181 struct rdma_cm_id *listen_id,
1182 struct ib_cm_event *ib_event,
1183 sa_family_t sa_family, __be64 service_id)
1184 {
1185 if (sa_family == AF_IB) {
1186 if (ib_event->event == IB_CM_REQ_RECEIVED)
1187 cma_save_ib_info(src_addr, dst_addr, listen_id,
1188 ib_event->param.req_rcvd.primary_path);
1189 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1190 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1191 return 0;
1192 }
1193
1194 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1195 }
1196
cma_save_req_info(const struct ib_cm_event * ib_event,struct cma_req_info * req)1197 static int cma_save_req_info(const struct ib_cm_event *ib_event,
1198 struct cma_req_info *req)
1199 {
1200 const struct ib_cm_req_event_param *req_param =
1201 &ib_event->param.req_rcvd;
1202 const struct ib_cm_sidr_req_event_param *sidr_param =
1203 &ib_event->param.sidr_req_rcvd;
1204
1205 switch (ib_event->event) {
1206 case IB_CM_REQ_RECEIVED:
1207 req->device = req_param->listen_id->device;
1208 req->port = req_param->port;
1209 memcpy(&req->local_gid, &req_param->primary_path->sgid,
1210 sizeof(req->local_gid));
1211 req->has_gid = true;
1212 req->service_id = req_param->primary_path->service_id;
1213 req->pkey = be16_to_cpu(req_param->primary_path->pkey);
1214 if (req->pkey != req_param->bth_pkey)
1215 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1216 "RDMA CMA: in the future this may cause the request to be dropped\n",
1217 req_param->bth_pkey, req->pkey);
1218 break;
1219 case IB_CM_SIDR_REQ_RECEIVED:
1220 req->device = sidr_param->listen_id->device;
1221 req->port = sidr_param->port;
1222 req->has_gid = false;
1223 req->service_id = sidr_param->service_id;
1224 req->pkey = sidr_param->pkey;
1225 if (req->pkey != sidr_param->bth_pkey)
1226 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1227 "RDMA CMA: in the future this may cause the request to be dropped\n",
1228 sidr_param->bth_pkey, req->pkey);
1229 break;
1230 default:
1231 return -EINVAL;
1232 }
1233
1234 return 0;
1235 }
1236
validate_ipv4_net_dev(struct net_device * net_dev,const struct sockaddr_in * dst_addr,const struct sockaddr_in * src_addr)1237 static bool validate_ipv4_net_dev(struct net_device *net_dev,
1238 const struct sockaddr_in *dst_addr,
1239 const struct sockaddr_in *src_addr)
1240 {
1241 __be32 daddr = dst_addr->sin_addr.s_addr,
1242 saddr = src_addr->sin_addr.s_addr;
1243 struct fib_result res;
1244 struct flowi4 fl4;
1245 int err;
1246 bool ret;
1247
1248 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1249 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1250 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1251 ipv4_is_loopback(saddr))
1252 return false;
1253
1254 memset(&fl4, 0, sizeof(fl4));
1255 fl4.flowi4_iif = net_dev->ifindex;
1256 fl4.daddr = daddr;
1257 fl4.saddr = saddr;
1258
1259 rcu_read_lock();
1260 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1261 ret = err == 0 && FIB_RES_DEV(res) == net_dev;
1262 rcu_read_unlock();
1263
1264 return ret;
1265 }
1266
validate_ipv6_net_dev(struct net_device * net_dev,const struct sockaddr_in6 * dst_addr,const struct sockaddr_in6 * src_addr)1267 static bool validate_ipv6_net_dev(struct net_device *net_dev,
1268 const struct sockaddr_in6 *dst_addr,
1269 const struct sockaddr_in6 *src_addr)
1270 {
1271 #if IS_ENABLED(CONFIG_IPV6)
1272 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1273 IPV6_ADDR_LINKLOCAL;
1274 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1275 &src_addr->sin6_addr, net_dev->ifindex,
1276 strict);
1277 bool ret;
1278
1279 if (!rt)
1280 return false;
1281
1282 ret = rt->rt6i_idev->dev == net_dev;
1283 ip6_rt_put(rt);
1284
1285 return ret;
1286 #else
1287 return false;
1288 #endif
1289 }
1290
validate_net_dev(struct net_device * net_dev,const struct sockaddr * daddr,const struct sockaddr * saddr)1291 static bool validate_net_dev(struct net_device *net_dev,
1292 const struct sockaddr *daddr,
1293 const struct sockaddr *saddr)
1294 {
1295 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1296 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1297 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1298 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1299
1300 switch (daddr->sa_family) {
1301 case AF_INET:
1302 return saddr->sa_family == AF_INET &&
1303 validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1304
1305 case AF_INET6:
1306 return saddr->sa_family == AF_INET6 &&
1307 validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1308
1309 default:
1310 return false;
1311 }
1312 }
1313
cma_get_net_dev(struct ib_cm_event * ib_event,const struct cma_req_info * req)1314 static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1315 const struct cma_req_info *req)
1316 {
1317 struct sockaddr_storage listen_addr_storage, src_addr_storage;
1318 struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1319 *src_addr = (struct sockaddr *)&src_addr_storage;
1320 struct net_device *net_dev;
1321 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1322 int err;
1323
1324 err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1325 req->service_id);
1326 if (err)
1327 return ERR_PTR(err);
1328
1329 net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
1330 gid, listen_addr);
1331 if (!net_dev)
1332 return ERR_PTR(-ENODEV);
1333
1334 if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1335 dev_put(net_dev);
1336 return ERR_PTR(-EHOSTUNREACH);
1337 }
1338
1339 return net_dev;
1340 }
1341
rdma_ps_from_service_id(__be64 service_id)1342 static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1343 {
1344 return (be64_to_cpu(service_id) >> 16) & 0xffff;
1345 }
1346
cma_match_private_data(struct rdma_id_private * id_priv,const struct cma_hdr * hdr)1347 static bool cma_match_private_data(struct rdma_id_private *id_priv,
1348 const struct cma_hdr *hdr)
1349 {
1350 struct sockaddr *addr = cma_src_addr(id_priv);
1351 __be32 ip4_addr;
1352 struct in6_addr ip6_addr;
1353
1354 if (cma_any_addr(addr) && !id_priv->afonly)
1355 return true;
1356
1357 switch (addr->sa_family) {
1358 case AF_INET:
1359 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1360 if (cma_get_ip_ver(hdr) != 4)
1361 return false;
1362 if (!cma_any_addr(addr) &&
1363 hdr->dst_addr.ip4.addr != ip4_addr)
1364 return false;
1365 break;
1366 case AF_INET6:
1367 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1368 if (cma_get_ip_ver(hdr) != 6)
1369 return false;
1370 if (!cma_any_addr(addr) &&
1371 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1372 return false;
1373 break;
1374 case AF_IB:
1375 return true;
1376 default:
1377 return false;
1378 }
1379
1380 return true;
1381 }
1382
cma_protocol_roce_dev_port(struct ib_device * device,int port_num)1383 static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1384 {
1385 enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1386 enum rdma_transport_type transport =
1387 rdma_node_get_transport(device->node_type);
1388
1389 return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1390 }
1391
cma_protocol_roce(const struct rdma_cm_id * id)1392 static bool cma_protocol_roce(const struct rdma_cm_id *id)
1393 {
1394 struct ib_device *device = id->device;
1395 const int port_num = id->port_num ?: rdma_start_port(device);
1396
1397 return cma_protocol_roce_dev_port(device, port_num);
1398 }
1399
cma_match_net_dev(const struct rdma_cm_id * id,const struct net_device * net_dev,u8 port_num)1400 static bool cma_match_net_dev(const struct rdma_cm_id *id,
1401 const struct net_device *net_dev,
1402 u8 port_num)
1403 {
1404 const struct rdma_addr *addr = &id->route.addr;
1405
1406 if (!net_dev)
1407 /* This request is an AF_IB request or a RoCE request */
1408 return (!id->port_num || id->port_num == port_num) &&
1409 (addr->src_addr.ss_family == AF_IB ||
1410 cma_protocol_roce_dev_port(id->device, port_num));
1411
1412 return !addr->dev_addr.bound_dev_if ||
1413 (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1414 addr->dev_addr.bound_dev_if == net_dev->ifindex);
1415 }
1416
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)1417 static struct rdma_id_private *cma_find_listener(
1418 const struct rdma_bind_list *bind_list,
1419 const struct ib_cm_id *cm_id,
1420 const struct ib_cm_event *ib_event,
1421 const struct cma_req_info *req,
1422 const struct net_device *net_dev)
1423 {
1424 struct rdma_id_private *id_priv, *id_priv_dev;
1425
1426 if (!bind_list)
1427 return ERR_PTR(-EINVAL);
1428
1429 hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1430 if (cma_match_private_data(id_priv, ib_event->private_data)) {
1431 if (id_priv->id.device == cm_id->device &&
1432 cma_match_net_dev(&id_priv->id, net_dev, req->port))
1433 return id_priv;
1434 list_for_each_entry(id_priv_dev,
1435 &id_priv->listen_list,
1436 listen_list) {
1437 if (id_priv_dev->id.device == cm_id->device &&
1438 cma_match_net_dev(&id_priv_dev->id, net_dev, req->port))
1439 return id_priv_dev;
1440 }
1441 }
1442 }
1443
1444 return ERR_PTR(-EINVAL);
1445 }
1446
cma_id_from_event(struct ib_cm_id * cm_id,struct ib_cm_event * ib_event,struct net_device ** net_dev)1447 static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
1448 struct ib_cm_event *ib_event,
1449 struct net_device **net_dev)
1450 {
1451 struct cma_req_info req;
1452 struct rdma_bind_list *bind_list;
1453 struct rdma_id_private *id_priv;
1454 int err;
1455
1456 err = cma_save_req_info(ib_event, &req);
1457 if (err)
1458 return ERR_PTR(err);
1459
1460 *net_dev = cma_get_net_dev(ib_event, &req);
1461 if (IS_ERR(*net_dev)) {
1462 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
1463 /* Assuming the protocol is AF_IB */
1464 *net_dev = NULL;
1465 } else if (cma_protocol_roce_dev_port(req.device, req.port)) {
1466 /* TODO find the net dev matching the request parameters
1467 * through the RoCE GID table */
1468 *net_dev = NULL;
1469 } else {
1470 return ERR_CAST(*net_dev);
1471 }
1472 }
1473
1474 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
1475 rdma_ps_from_service_id(req.service_id),
1476 cma_port_from_service_id(req.service_id));
1477 id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
1478 if (IS_ERR(id_priv) && *net_dev) {
1479 dev_put(*net_dev);
1480 *net_dev = NULL;
1481 }
1482
1483 return id_priv;
1484 }
1485
cma_user_data_offset(struct rdma_id_private * id_priv)1486 static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv)
1487 {
1488 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
1489 }
1490
cma_cancel_route(struct rdma_id_private * id_priv)1491 static void cma_cancel_route(struct rdma_id_private *id_priv)
1492 {
1493 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
1494 if (id_priv->query)
1495 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
1496 }
1497 }
1498
cma_cancel_listens(struct rdma_id_private * id_priv)1499 static void cma_cancel_listens(struct rdma_id_private *id_priv)
1500 {
1501 struct rdma_id_private *dev_id_priv;
1502
1503 /*
1504 * Remove from listen_any_list to prevent added devices from spawning
1505 * additional listen requests.
1506 */
1507 mutex_lock(&lock);
1508 list_del(&id_priv->list);
1509
1510 while (!list_empty(&id_priv->listen_list)) {
1511 dev_id_priv = list_entry(id_priv->listen_list.next,
1512 struct rdma_id_private, listen_list);
1513 /* sync with device removal to avoid duplicate destruction */
1514 list_del_init(&dev_id_priv->list);
1515 list_del(&dev_id_priv->listen_list);
1516 mutex_unlock(&lock);
1517
1518 rdma_destroy_id(&dev_id_priv->id);
1519 mutex_lock(&lock);
1520 }
1521 mutex_unlock(&lock);
1522 }
1523
cma_cancel_operation(struct rdma_id_private * id_priv,enum rdma_cm_state state)1524 static void cma_cancel_operation(struct rdma_id_private *id_priv,
1525 enum rdma_cm_state state)
1526 {
1527 switch (state) {
1528 case RDMA_CM_ADDR_QUERY:
1529 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1530 break;
1531 case RDMA_CM_ROUTE_QUERY:
1532 cma_cancel_route(id_priv);
1533 break;
1534 case RDMA_CM_LISTEN:
1535 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
1536 cma_cancel_listens(id_priv);
1537 break;
1538 default:
1539 break;
1540 }
1541 }
1542
cma_release_port(struct rdma_id_private * id_priv)1543 static void cma_release_port(struct rdma_id_private *id_priv)
1544 {
1545 struct rdma_bind_list *bind_list = id_priv->bind_list;
1546 struct net *net = id_priv->id.route.addr.dev_addr.net;
1547
1548 if (!bind_list)
1549 return;
1550
1551 mutex_lock(&lock);
1552 hlist_del(&id_priv->node);
1553 if (hlist_empty(&bind_list->owners)) {
1554 cma_ps_remove(net, bind_list->ps, bind_list->port);
1555 kfree(bind_list);
1556 }
1557 mutex_unlock(&lock);
1558 }
1559
cma_leave_mc_groups(struct rdma_id_private * id_priv)1560 static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1561 {
1562 struct cma_multicast *mc;
1563
1564 while (!list_empty(&id_priv->mc_list)) {
1565 mc = container_of(id_priv->mc_list.next,
1566 struct cma_multicast, list);
1567 list_del(&mc->list);
1568 if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
1569 id_priv->id.port_num)) {
1570 ib_sa_free_multicast(mc->multicast.ib);
1571 kfree(mc);
1572 } else {
1573 if (mc->igmp_joined) {
1574 struct rdma_dev_addr *dev_addr =
1575 &id_priv->id.route.addr.dev_addr;
1576 struct net_device *ndev = NULL;
1577
1578 if (dev_addr->bound_dev_if)
1579 ndev = dev_get_by_index(&init_net,
1580 dev_addr->bound_dev_if);
1581 if (ndev) {
1582 cma_igmp_send(ndev,
1583 &mc->multicast.ib->rec.mgid,
1584 false);
1585 dev_put(ndev);
1586 }
1587 }
1588 kref_put(&mc->mcref, release_mc);
1589 }
1590 }
1591 }
1592
rdma_destroy_id(struct rdma_cm_id * id)1593 void rdma_destroy_id(struct rdma_cm_id *id)
1594 {
1595 struct rdma_id_private *id_priv;
1596 enum rdma_cm_state state;
1597
1598 id_priv = container_of(id, struct rdma_id_private, id);
1599 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
1600 cma_cancel_operation(id_priv, state);
1601
1602 /*
1603 * Wait for any active callback to finish. New callbacks will find
1604 * the id_priv state set to destroying and abort.
1605 */
1606 mutex_lock(&id_priv->handler_mutex);
1607 mutex_unlock(&id_priv->handler_mutex);
1608
1609 if (id_priv->cma_dev) {
1610 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
1611 if (id_priv->cm_id.ib)
1612 ib_destroy_cm_id(id_priv->cm_id.ib);
1613 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
1614 if (id_priv->cm_id.iw)
1615 iw_destroy_cm_id(id_priv->cm_id.iw);
1616 }
1617 cma_leave_mc_groups(id_priv);
1618 cma_release_dev(id_priv);
1619 }
1620
1621 cma_release_port(id_priv);
1622 cma_deref_id(id_priv);
1623 wait_for_completion(&id_priv->comp);
1624
1625 if (id_priv->internal_id)
1626 cma_deref_id(id_priv->id.context);
1627
1628 kfree(id_priv->id.route.path_rec);
1629 put_net(id_priv->id.route.addr.dev_addr.net);
1630 kfree(id_priv);
1631 }
1632 EXPORT_SYMBOL(rdma_destroy_id);
1633
cma_rep_recv(struct rdma_id_private * id_priv)1634 static int cma_rep_recv(struct rdma_id_private *id_priv)
1635 {
1636 int ret;
1637
1638 ret = cma_modify_qp_rtr(id_priv, NULL);
1639 if (ret)
1640 goto reject;
1641
1642 ret = cma_modify_qp_rts(id_priv, NULL);
1643 if (ret)
1644 goto reject;
1645
1646 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1647 if (ret)
1648 goto reject;
1649
1650 return 0;
1651 reject:
1652 cma_modify_qp_err(id_priv);
1653 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1654 NULL, 0, NULL, 0);
1655 return ret;
1656 }
1657
cma_set_rep_event_data(struct rdma_cm_event * event,struct ib_cm_rep_event_param * rep_data,void * private_data)1658 static void cma_set_rep_event_data(struct rdma_cm_event *event,
1659 struct ib_cm_rep_event_param *rep_data,
1660 void *private_data)
1661 {
1662 event->param.conn.private_data = private_data;
1663 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1664 event->param.conn.responder_resources = rep_data->responder_resources;
1665 event->param.conn.initiator_depth = rep_data->initiator_depth;
1666 event->param.conn.flow_control = rep_data->flow_control;
1667 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1668 event->param.conn.srq = rep_data->srq;
1669 event->param.conn.qp_num = rep_data->remote_qpn;
1670 }
1671
cma_ib_handler(struct ib_cm_id * cm_id,struct ib_cm_event * ib_event)1672 static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1673 {
1674 struct rdma_id_private *id_priv = cm_id->context;
1675 struct rdma_cm_event event;
1676 int ret = 0;
1677
1678 mutex_lock(&id_priv->handler_mutex);
1679 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
1680 id_priv->state != RDMA_CM_CONNECT) ||
1681 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
1682 id_priv->state != RDMA_CM_DISCONNECT))
1683 goto out;
1684
1685 memset(&event, 0, sizeof event);
1686 switch (ib_event->event) {
1687 case IB_CM_REQ_ERROR:
1688 case IB_CM_REP_ERROR:
1689 event.event = RDMA_CM_EVENT_UNREACHABLE;
1690 event.status = -ETIMEDOUT;
1691 break;
1692 case IB_CM_REP_RECEIVED:
1693 if (id_priv->id.qp) {
1694 event.status = cma_rep_recv(id_priv);
1695 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1696 RDMA_CM_EVENT_ESTABLISHED;
1697 } else {
1698 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1699 }
1700 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1701 ib_event->private_data);
1702 break;
1703 case IB_CM_RTU_RECEIVED:
1704 case IB_CM_USER_ESTABLISHED:
1705 event.event = RDMA_CM_EVENT_ESTABLISHED;
1706 break;
1707 case IB_CM_DREQ_ERROR:
1708 event.status = -ETIMEDOUT; /* fall through */
1709 case IB_CM_DREQ_RECEIVED:
1710 case IB_CM_DREP_RECEIVED:
1711 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1712 RDMA_CM_DISCONNECT))
1713 goto out;
1714 event.event = RDMA_CM_EVENT_DISCONNECTED;
1715 break;
1716 case IB_CM_TIMEWAIT_EXIT:
1717 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1718 break;
1719 case IB_CM_MRA_RECEIVED:
1720 /* ignore event */
1721 goto out;
1722 case IB_CM_REJ_RECEIVED:
1723 cma_modify_qp_err(id_priv);
1724 event.status = ib_event->param.rej_rcvd.reason;
1725 event.event = RDMA_CM_EVENT_REJECTED;
1726 event.param.conn.private_data = ib_event->private_data;
1727 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
1728 break;
1729 default:
1730 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
1731 ib_event->event);
1732 goto out;
1733 }
1734
1735 ret = id_priv->id.event_handler(&id_priv->id, &event);
1736 if (ret) {
1737 /* Destroy the CM ID by returning a non-zero value. */
1738 id_priv->cm_id.ib = NULL;
1739 cma_exch(id_priv, RDMA_CM_DESTROYING);
1740 mutex_unlock(&id_priv->handler_mutex);
1741 rdma_destroy_id(&id_priv->id);
1742 return ret;
1743 }
1744 out:
1745 mutex_unlock(&id_priv->handler_mutex);
1746 return ret;
1747 }
1748
cma_new_conn_id(struct rdma_cm_id * listen_id,struct ib_cm_event * ib_event,struct net_device * net_dev)1749 static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1750 struct ib_cm_event *ib_event,
1751 struct net_device *net_dev)
1752 {
1753 struct rdma_id_private *id_priv;
1754 struct rdma_cm_id *id;
1755 struct rdma_route *rt;
1756 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1757 const __be64 service_id =
1758 ib_event->param.req_rcvd.primary_path->service_id;
1759 int ret;
1760
1761 id = rdma_create_id(listen_id->route.addr.dev_addr.net,
1762 listen_id->event_handler, listen_id->context,
1763 listen_id->ps, ib_event->param.req_rcvd.qp_type);
1764 if (IS_ERR(id))
1765 return NULL;
1766
1767 id_priv = container_of(id, struct rdma_id_private, id);
1768 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1769 (struct sockaddr *)&id->route.addr.dst_addr,
1770 listen_id, ib_event, ss_family, service_id))
1771 goto err;
1772
1773 rt = &id->route;
1774 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1775 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1776 GFP_KERNEL);
1777 if (!rt->path_rec)
1778 goto err;
1779
1780 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1781 if (rt->num_paths == 2)
1782 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1783
1784 if (net_dev) {
1785 ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
1786 if (ret)
1787 goto err;
1788 } else {
1789 if (!cma_protocol_roce(listen_id) &&
1790 cma_any_addr(cma_src_addr(id_priv))) {
1791 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1792 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1793 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1794 } else if (!cma_any_addr(cma_src_addr(id_priv))) {
1795 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1796 if (ret)
1797 goto err;
1798 }
1799 }
1800 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
1801
1802 id_priv->state = RDMA_CM_CONNECT;
1803 return id_priv;
1804
1805 err:
1806 rdma_destroy_id(id);
1807 return NULL;
1808 }
1809
cma_new_udp_id(struct rdma_cm_id * listen_id,struct ib_cm_event * ib_event,struct net_device * net_dev)1810 static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1811 struct ib_cm_event *ib_event,
1812 struct net_device *net_dev)
1813 {
1814 struct rdma_id_private *id_priv;
1815 struct rdma_cm_id *id;
1816 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1817 struct net *net = listen_id->route.addr.dev_addr.net;
1818 int ret;
1819
1820 id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
1821 listen_id->ps, IB_QPT_UD);
1822 if (IS_ERR(id))
1823 return NULL;
1824
1825 id_priv = container_of(id, struct rdma_id_private, id);
1826 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1827 (struct sockaddr *)&id->route.addr.dst_addr,
1828 listen_id, ib_event, ss_family,
1829 ib_event->param.sidr_req_rcvd.service_id))
1830 goto err;
1831
1832 if (net_dev) {
1833 ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
1834 if (ret)
1835 goto err;
1836 } else {
1837 if (!cma_any_addr(cma_src_addr(id_priv))) {
1838 ret = cma_translate_addr(cma_src_addr(id_priv),
1839 &id->route.addr.dev_addr);
1840 if (ret)
1841 goto err;
1842 }
1843 }
1844
1845 id_priv->state = RDMA_CM_CONNECT;
1846 return id_priv;
1847 err:
1848 rdma_destroy_id(id);
1849 return NULL;
1850 }
1851
cma_set_req_event_data(struct rdma_cm_event * event,struct ib_cm_req_event_param * req_data,void * private_data,int offset)1852 static void cma_set_req_event_data(struct rdma_cm_event *event,
1853 struct ib_cm_req_event_param *req_data,
1854 void *private_data, int offset)
1855 {
1856 event->param.conn.private_data = private_data + offset;
1857 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1858 event->param.conn.responder_resources = req_data->responder_resources;
1859 event->param.conn.initiator_depth = req_data->initiator_depth;
1860 event->param.conn.flow_control = req_data->flow_control;
1861 event->param.conn.retry_count = req_data->retry_count;
1862 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1863 event->param.conn.srq = req_data->srq;
1864 event->param.conn.qp_num = req_data->remote_qpn;
1865 }
1866
cma_check_req_qp_type(struct rdma_cm_id * id,struct ib_cm_event * ib_event)1867 static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1868 {
1869 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
1870 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1871 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1872 (id->qp_type == IB_QPT_UD)) ||
1873 (!id->qp_type));
1874 }
1875
cma_req_handler(struct ib_cm_id * cm_id,struct ib_cm_event * ib_event)1876 static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1877 {
1878 struct rdma_id_private *listen_id, *conn_id = NULL;
1879 struct rdma_cm_event event;
1880 struct net_device *net_dev;
1881 u8 offset;
1882 int ret;
1883
1884 listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
1885 if (IS_ERR(listen_id))
1886 return PTR_ERR(listen_id);
1887
1888 if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
1889 ret = -EINVAL;
1890 goto net_dev_put;
1891 }
1892
1893 mutex_lock(&listen_id->handler_mutex);
1894 if (listen_id->state != RDMA_CM_LISTEN) {
1895 ret = -ECONNABORTED;
1896 goto err1;
1897 }
1898
1899 memset(&event, 0, sizeof event);
1900 offset = cma_user_data_offset(listen_id);
1901 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1902 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
1903 conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
1904 event.param.ud.private_data = ib_event->private_data + offset;
1905 event.param.ud.private_data_len =
1906 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1907 } else {
1908 conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
1909 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1910 ib_event->private_data, offset);
1911 }
1912 if (!conn_id) {
1913 ret = -ENOMEM;
1914 goto err1;
1915 }
1916
1917 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
1918 ret = cma_acquire_dev(conn_id, listen_id);
1919 if (ret)
1920 goto err2;
1921
1922 conn_id->cm_id.ib = cm_id;
1923 cm_id->context = conn_id;
1924 cm_id->cm_handler = cma_ib_handler;
1925
1926 /*
1927 * Protect against the user destroying conn_id from another thread
1928 * until we're done accessing it.
1929 */
1930 atomic_inc(&conn_id->refcount);
1931 ret = conn_id->id.event_handler(&conn_id->id, &event);
1932 if (ret)
1933 goto err3;
1934 /*
1935 * Acquire mutex to prevent user executing rdma_destroy_id()
1936 * while we're accessing the cm_id.
1937 */
1938 mutex_lock(&lock);
1939 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1940 (conn_id->id.qp_type != IB_QPT_UD))
1941 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1942 mutex_unlock(&lock);
1943 mutex_unlock(&conn_id->handler_mutex);
1944 mutex_unlock(&listen_id->handler_mutex);
1945 cma_deref_id(conn_id);
1946 if (net_dev)
1947 dev_put(net_dev);
1948 return 0;
1949
1950 err3:
1951 cma_deref_id(conn_id);
1952 /* Destroy the CM ID by returning a non-zero value. */
1953 conn_id->cm_id.ib = NULL;
1954 err2:
1955 cma_exch(conn_id, RDMA_CM_DESTROYING);
1956 mutex_unlock(&conn_id->handler_mutex);
1957 err1:
1958 mutex_unlock(&listen_id->handler_mutex);
1959 if (conn_id)
1960 rdma_destroy_id(&conn_id->id);
1961
1962 net_dev_put:
1963 if (net_dev)
1964 dev_put(net_dev);
1965
1966 return ret;
1967 }
1968
rdma_get_service_id(struct rdma_cm_id * id,struct sockaddr * addr)1969 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
1970 {
1971 if (addr->sa_family == AF_IB)
1972 return ((struct sockaddr_ib *) addr)->sib_sid;
1973
1974 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
1975 }
1976 EXPORT_SYMBOL(rdma_get_service_id);
1977
cma_iw_handler(struct iw_cm_id * iw_id,struct iw_cm_event * iw_event)1978 static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1979 {
1980 struct rdma_id_private *id_priv = iw_id->context;
1981 struct rdma_cm_event event;
1982 int ret = 0;
1983 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1984 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
1985
1986 mutex_lock(&id_priv->handler_mutex);
1987 if (id_priv->state != RDMA_CM_CONNECT)
1988 goto out;
1989
1990 memset(&event, 0, sizeof event);
1991 switch (iw_event->event) {
1992 case IW_CM_EVENT_CLOSE:
1993 event.event = RDMA_CM_EVENT_DISCONNECTED;
1994 break;
1995 case IW_CM_EVENT_CONNECT_REPLY:
1996 memcpy(cma_src_addr(id_priv), laddr,
1997 rdma_addr_size(laddr));
1998 memcpy(cma_dst_addr(id_priv), raddr,
1999 rdma_addr_size(raddr));
2000 switch (iw_event->status) {
2001 case 0:
2002 event.event = RDMA_CM_EVENT_ESTABLISHED;
2003 event.param.conn.initiator_depth = iw_event->ird;
2004 event.param.conn.responder_resources = iw_event->ord;
2005 break;
2006 case -ECONNRESET:
2007 case -ECONNREFUSED:
2008 event.event = RDMA_CM_EVENT_REJECTED;
2009 break;
2010 case -ETIMEDOUT:
2011 event.event = RDMA_CM_EVENT_UNREACHABLE;
2012 break;
2013 default:
2014 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2015 break;
2016 }
2017 break;
2018 case IW_CM_EVENT_ESTABLISHED:
2019 event.event = RDMA_CM_EVENT_ESTABLISHED;
2020 event.param.conn.initiator_depth = iw_event->ird;
2021 event.param.conn.responder_resources = iw_event->ord;
2022 break;
2023 default:
2024 BUG_ON(1);
2025 }
2026
2027 event.status = iw_event->status;
2028 event.param.conn.private_data = iw_event->private_data;
2029 event.param.conn.private_data_len = iw_event->private_data_len;
2030 ret = id_priv->id.event_handler(&id_priv->id, &event);
2031 if (ret) {
2032 /* Destroy the CM ID by returning a non-zero value. */
2033 id_priv->cm_id.iw = NULL;
2034 cma_exch(id_priv, RDMA_CM_DESTROYING);
2035 mutex_unlock(&id_priv->handler_mutex);
2036 rdma_destroy_id(&id_priv->id);
2037 return ret;
2038 }
2039
2040 out:
2041 mutex_unlock(&id_priv->handler_mutex);
2042 return ret;
2043 }
2044
iw_conn_req_handler(struct iw_cm_id * cm_id,struct iw_cm_event * iw_event)2045 static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2046 struct iw_cm_event *iw_event)
2047 {
2048 struct rdma_cm_id *new_cm_id;
2049 struct rdma_id_private *listen_id, *conn_id;
2050 struct rdma_cm_event event;
2051 int ret = -ECONNABORTED;
2052 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2053 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
2054
2055 listen_id = cm_id->context;
2056
2057 mutex_lock(&listen_id->handler_mutex);
2058 if (listen_id->state != RDMA_CM_LISTEN)
2059 goto out;
2060
2061 /* Create a new RDMA id for the new IW CM ID */
2062 new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2063 listen_id->id.event_handler,
2064 listen_id->id.context,
2065 RDMA_PS_TCP, IB_QPT_RC);
2066 if (IS_ERR(new_cm_id)) {
2067 ret = -ENOMEM;
2068 goto out;
2069 }
2070 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
2071 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
2072 conn_id->state = RDMA_CM_CONNECT;
2073
2074 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
2075 if (ret) {
2076 mutex_unlock(&conn_id->handler_mutex);
2077 rdma_destroy_id(new_cm_id);
2078 goto out;
2079 }
2080
2081 ret = cma_acquire_dev(conn_id, listen_id);
2082 if (ret) {
2083 mutex_unlock(&conn_id->handler_mutex);
2084 rdma_destroy_id(new_cm_id);
2085 goto out;
2086 }
2087
2088 conn_id->cm_id.iw = cm_id;
2089 cm_id->context = conn_id;
2090 cm_id->cm_handler = cma_iw_handler;
2091
2092 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2093 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
2094
2095 memset(&event, 0, sizeof event);
2096 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2097 event.param.conn.private_data = iw_event->private_data;
2098 event.param.conn.private_data_len = iw_event->private_data_len;
2099 event.param.conn.initiator_depth = iw_event->ird;
2100 event.param.conn.responder_resources = iw_event->ord;
2101
2102 /*
2103 * Protect against the user destroying conn_id from another thread
2104 * until we're done accessing it.
2105 */
2106 atomic_inc(&conn_id->refcount);
2107 ret = conn_id->id.event_handler(&conn_id->id, &event);
2108 if (ret) {
2109 /* User wants to destroy the CM ID */
2110 conn_id->cm_id.iw = NULL;
2111 cma_exch(conn_id, RDMA_CM_DESTROYING);
2112 mutex_unlock(&conn_id->handler_mutex);
2113 cma_deref_id(conn_id);
2114 rdma_destroy_id(&conn_id->id);
2115 goto out;
2116 }
2117
2118 mutex_unlock(&conn_id->handler_mutex);
2119 cma_deref_id(conn_id);
2120
2121 out:
2122 mutex_unlock(&listen_id->handler_mutex);
2123 return ret;
2124 }
2125
cma_ib_listen(struct rdma_id_private * id_priv)2126 static int cma_ib_listen(struct rdma_id_private *id_priv)
2127 {
2128 struct sockaddr *addr;
2129 struct ib_cm_id *id;
2130 __be64 svc_id;
2131
2132 addr = cma_src_addr(id_priv);
2133 svc_id = rdma_get_service_id(&id_priv->id, addr);
2134 id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
2135 if (IS_ERR(id))
2136 return PTR_ERR(id);
2137 id_priv->cm_id.ib = id;
2138
2139 return 0;
2140 }
2141
cma_iw_listen(struct rdma_id_private * id_priv,int backlog)2142 static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2143 {
2144 int ret;
2145 struct iw_cm_id *id;
2146
2147 id = iw_create_cm_id(id_priv->id.device,
2148 iw_conn_req_handler,
2149 id_priv);
2150 if (IS_ERR(id))
2151 return PTR_ERR(id);
2152
2153 id->tos = id_priv->tos;
2154 id_priv->cm_id.iw = id;
2155
2156 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2157 rdma_addr_size(cma_src_addr(id_priv)));
2158
2159 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2160
2161 if (ret) {
2162 iw_destroy_cm_id(id_priv->cm_id.iw);
2163 id_priv->cm_id.iw = NULL;
2164 }
2165
2166 return ret;
2167 }
2168
cma_listen_handler(struct rdma_cm_id * id,struct rdma_cm_event * event)2169 static int cma_listen_handler(struct rdma_cm_id *id,
2170 struct rdma_cm_event *event)
2171 {
2172 struct rdma_id_private *id_priv = id->context;
2173
2174 id->context = id_priv->id.context;
2175 id->event_handler = id_priv->id.event_handler;
2176 return id_priv->id.event_handler(id, event);
2177 }
2178
cma_listen_on_dev(struct rdma_id_private * id_priv,struct cma_device * cma_dev)2179 static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2180 struct cma_device *cma_dev)
2181 {
2182 struct rdma_id_private *dev_id_priv;
2183 struct rdma_cm_id *id;
2184 struct net *net = id_priv->id.route.addr.dev_addr.net;
2185 int ret;
2186
2187 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
2188 return;
2189
2190 id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
2191 id_priv->id.qp_type);
2192 if (IS_ERR(id))
2193 return;
2194
2195 dev_id_priv = container_of(id, struct rdma_id_private, id);
2196
2197 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
2198 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2199 rdma_addr_size(cma_src_addr(id_priv)));
2200
2201 _cma_attach_to_dev(dev_id_priv, cma_dev);
2202 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
2203 atomic_inc(&id_priv->refcount);
2204 dev_id_priv->internal_id = 1;
2205 dev_id_priv->afonly = id_priv->afonly;
2206
2207 ret = rdma_listen(id, id_priv->backlog);
2208 if (ret)
2209 pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2210 ret, cma_dev->device->name);
2211 }
2212
cma_listen_on_all(struct rdma_id_private * id_priv)2213 static void cma_listen_on_all(struct rdma_id_private *id_priv)
2214 {
2215 struct cma_device *cma_dev;
2216
2217 mutex_lock(&lock);
2218 list_add_tail(&id_priv->list, &listen_any_list);
2219 list_for_each_entry(cma_dev, &dev_list, list)
2220 cma_listen_on_dev(id_priv, cma_dev);
2221 mutex_unlock(&lock);
2222 }
2223
rdma_set_service_type(struct rdma_cm_id * id,int tos)2224 void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2225 {
2226 struct rdma_id_private *id_priv;
2227
2228 id_priv = container_of(id, struct rdma_id_private, id);
2229 id_priv->tos = (u8) tos;
2230 }
2231 EXPORT_SYMBOL(rdma_set_service_type);
2232
cma_query_handler(int status,struct ib_sa_path_rec * path_rec,void * context)2233 static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
2234 void *context)
2235 {
2236 struct cma_work *work = context;
2237 struct rdma_route *route;
2238
2239 route = &work->id->id.route;
2240
2241 if (!status) {
2242 route->num_paths = 1;
2243 *route->path_rec = *path_rec;
2244 } else {
2245 work->old_state = RDMA_CM_ROUTE_QUERY;
2246 work->new_state = RDMA_CM_ADDR_RESOLVED;
2247 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
2248 work->event.status = status;
2249 }
2250
2251 queue_work(cma_wq, &work->work);
2252 }
2253
cma_query_ib_route(struct rdma_id_private * id_priv,int timeout_ms,struct cma_work * work)2254 static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2255 struct cma_work *work)
2256 {
2257 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2258 struct ib_sa_path_rec path_rec;
2259 ib_sa_comp_mask comp_mask;
2260 struct sockaddr_in6 *sin6;
2261 struct sockaddr_ib *sib;
2262
2263 memset(&path_rec, 0, sizeof path_rec);
2264 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2265 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2266 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2267 path_rec.numb_path = 1;
2268 path_rec.reversible = 1;
2269 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
2270
2271 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2272 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2273 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2274
2275 switch (cma_family(id_priv)) {
2276 case AF_INET:
2277 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2278 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
2279 break;
2280 case AF_INET6:
2281 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2282 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2283 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2284 break;
2285 case AF_IB:
2286 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2287 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2288 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2289 break;
2290 }
2291
2292 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
2293 id_priv->id.port_num, &path_rec,
2294 comp_mask, timeout_ms,
2295 GFP_KERNEL, cma_query_handler,
2296 work, &id_priv->query);
2297
2298 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2299 }
2300
cma_work_handler(struct work_struct * _work)2301 static void cma_work_handler(struct work_struct *_work)
2302 {
2303 struct cma_work *work = container_of(_work, struct cma_work, work);
2304 struct rdma_id_private *id_priv = work->id;
2305 int destroy = 0;
2306
2307 mutex_lock(&id_priv->handler_mutex);
2308 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2309 goto out;
2310
2311 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
2312 cma_exch(id_priv, RDMA_CM_DESTROYING);
2313 destroy = 1;
2314 }
2315 out:
2316 mutex_unlock(&id_priv->handler_mutex);
2317 cma_deref_id(id_priv);
2318 if (destroy)
2319 rdma_destroy_id(&id_priv->id);
2320 kfree(work);
2321 }
2322
cma_ndev_work_handler(struct work_struct * _work)2323 static void cma_ndev_work_handler(struct work_struct *_work)
2324 {
2325 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
2326 struct rdma_id_private *id_priv = work->id;
2327 int destroy = 0;
2328
2329 mutex_lock(&id_priv->handler_mutex);
2330 if (id_priv->state == RDMA_CM_DESTROYING ||
2331 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
2332 goto out;
2333
2334 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
2335 cma_exch(id_priv, RDMA_CM_DESTROYING);
2336 destroy = 1;
2337 }
2338
2339 out:
2340 mutex_unlock(&id_priv->handler_mutex);
2341 cma_deref_id(id_priv);
2342 if (destroy)
2343 rdma_destroy_id(&id_priv->id);
2344 kfree(work);
2345 }
2346
cma_resolve_ib_route(struct rdma_id_private * id_priv,int timeout_ms)2347 static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
2348 {
2349 struct rdma_route *route = &id_priv->id.route;
2350 struct cma_work *work;
2351 int ret;
2352
2353 work = kzalloc(sizeof *work, GFP_KERNEL);
2354 if (!work)
2355 return -ENOMEM;
2356
2357 work->id = id_priv;
2358 INIT_WORK(&work->work, cma_work_handler);
2359 work->old_state = RDMA_CM_ROUTE_QUERY;
2360 work->new_state = RDMA_CM_ROUTE_RESOLVED;
2361 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2362
2363 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2364 if (!route->path_rec) {
2365 ret = -ENOMEM;
2366 goto err1;
2367 }
2368
2369 ret = cma_query_ib_route(id_priv, timeout_ms, work);
2370 if (ret)
2371 goto err2;
2372
2373 return 0;
2374 err2:
2375 kfree(route->path_rec);
2376 route->path_rec = NULL;
2377 err1:
2378 kfree(work);
2379 return ret;
2380 }
2381
rdma_set_ib_paths(struct rdma_cm_id * id,struct ib_sa_path_rec * path_rec,int num_paths)2382 int rdma_set_ib_paths(struct rdma_cm_id *id,
2383 struct ib_sa_path_rec *path_rec, int num_paths)
2384 {
2385 struct rdma_id_private *id_priv;
2386 int ret;
2387
2388 id_priv = container_of(id, struct rdma_id_private, id);
2389 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2390 RDMA_CM_ROUTE_RESOLVED))
2391 return -EINVAL;
2392
2393 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2394 GFP_KERNEL);
2395 if (!id->route.path_rec) {
2396 ret = -ENOMEM;
2397 goto err;
2398 }
2399
2400 id->route.num_paths = num_paths;
2401 return 0;
2402 err:
2403 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
2404 return ret;
2405 }
2406 EXPORT_SYMBOL(rdma_set_ib_paths);
2407
cma_resolve_iw_route(struct rdma_id_private * id_priv,int timeout_ms)2408 static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2409 {
2410 struct cma_work *work;
2411
2412 work = kzalloc(sizeof *work, GFP_KERNEL);
2413 if (!work)
2414 return -ENOMEM;
2415
2416 work->id = id_priv;
2417 INIT_WORK(&work->work, cma_work_handler);
2418 work->old_state = RDMA_CM_ROUTE_QUERY;
2419 work->new_state = RDMA_CM_ROUTE_RESOLVED;
2420 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2421 queue_work(cma_wq, &work->work);
2422 return 0;
2423 }
2424
iboe_tos_to_sl(struct net_device * ndev,int tos)2425 static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2426 {
2427 int prio;
2428 struct net_device *dev;
2429
2430 prio = rt_tos2priority(tos);
2431 dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
2432 vlan_dev_real_dev(ndev) : ndev;
2433
2434 if (dev->num_tc)
2435 return netdev_get_prio_tc_map(dev, prio);
2436
2437 #if IS_ENABLED(CONFIG_VLAN_8021Q)
2438 if (ndev->priv_flags & IFF_802_1Q_VLAN)
2439 return (vlan_dev_get_egress_qos_mask(ndev, prio) &
2440 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2441 #endif
2442 return 0;
2443 }
2444
cma_route_gid_type(enum rdma_network_type network_type,unsigned long supported_gids,enum ib_gid_type default_gid)2445 static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2446 unsigned long supported_gids,
2447 enum ib_gid_type default_gid)
2448 {
2449 if ((network_type == RDMA_NETWORK_IPV4 ||
2450 network_type == RDMA_NETWORK_IPV6) &&
2451 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2452 return IB_GID_TYPE_ROCE_UDP_ENCAP;
2453
2454 return default_gid;
2455 }
2456
cma_resolve_iboe_route(struct rdma_id_private * id_priv)2457 static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2458 {
2459 struct rdma_route *route = &id_priv->id.route;
2460 struct rdma_addr *addr = &route->addr;
2461 struct cma_work *work;
2462 int ret;
2463 struct net_device *ndev = NULL;
2464
2465
2466 work = kzalloc(sizeof *work, GFP_KERNEL);
2467 if (!work)
2468 return -ENOMEM;
2469
2470 work->id = id_priv;
2471 INIT_WORK(&work->work, cma_work_handler);
2472
2473 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2474 if (!route->path_rec) {
2475 ret = -ENOMEM;
2476 goto err1;
2477 }
2478
2479 route->num_paths = 1;
2480
2481 if (addr->dev_addr.bound_dev_if) {
2482 unsigned long supported_gids;
2483
2484 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
2485 if (!ndev) {
2486 ret = -ENODEV;
2487 goto err2;
2488 }
2489
2490 if (ndev->flags & IFF_LOOPBACK) {
2491 dev_put(ndev);
2492 if (!id_priv->id.device->get_netdev) {
2493 ret = -EOPNOTSUPP;
2494 goto err2;
2495 }
2496
2497 ndev = id_priv->id.device->get_netdev(id_priv->id.device,
2498 id_priv->id.port_num);
2499 if (!ndev) {
2500 ret = -ENODEV;
2501 goto err2;
2502 }
2503 }
2504
2505 route->path_rec->net = &init_net;
2506 route->path_rec->ifindex = ndev->ifindex;
2507 supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2508 id_priv->id.port_num);
2509 route->path_rec->gid_type =
2510 cma_route_gid_type(addr->dev_addr.network,
2511 supported_gids,
2512 id_priv->gid_type);
2513 }
2514 if (!ndev) {
2515 ret = -ENODEV;
2516 goto err2;
2517 }
2518
2519 memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
2520
2521 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2522 &route->path_rec->sgid);
2523 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2524 &route->path_rec->dgid);
2525
2526 /* Use the hint from IP Stack to select GID Type */
2527 if (route->path_rec->gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2528 route->path_rec->gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2529 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
2530 /* TODO: get the hoplimit from the inet/inet6 device */
2531 route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2532 else
2533 route->path_rec->hop_limit = 1;
2534 route->path_rec->reversible = 1;
2535 route->path_rec->pkey = cpu_to_be16(0xffff);
2536 route->path_rec->mtu_selector = IB_SA_EQ;
2537 route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
2538 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2539 route->path_rec->rate_selector = IB_SA_EQ;
2540 route->path_rec->rate = iboe_get_rate(ndev);
2541 dev_put(ndev);
2542 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2543 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2544 if (!route->path_rec->mtu) {
2545 ret = -EINVAL;
2546 goto err2;
2547 }
2548
2549 work->old_state = RDMA_CM_ROUTE_QUERY;
2550 work->new_state = RDMA_CM_ROUTE_RESOLVED;
2551 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2552 work->event.status = 0;
2553
2554 queue_work(cma_wq, &work->work);
2555
2556 return 0;
2557
2558 err2:
2559 kfree(route->path_rec);
2560 route->path_rec = NULL;
2561 err1:
2562 kfree(work);
2563 return ret;
2564 }
2565
rdma_resolve_route(struct rdma_cm_id * id,int timeout_ms)2566 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2567 {
2568 struct rdma_id_private *id_priv;
2569 int ret;
2570
2571 id_priv = container_of(id, struct rdma_id_private, id);
2572 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
2573 return -EINVAL;
2574
2575 atomic_inc(&id_priv->refcount);
2576 if (rdma_cap_ib_sa(id->device, id->port_num))
2577 ret = cma_resolve_ib_route(id_priv, timeout_ms);
2578 else if (rdma_protocol_roce(id->device, id->port_num))
2579 ret = cma_resolve_iboe_route(id_priv);
2580 else if (rdma_protocol_iwarp(id->device, id->port_num))
2581 ret = cma_resolve_iw_route(id_priv, timeout_ms);
2582 else
2583 ret = -ENOSYS;
2584
2585 if (ret)
2586 goto err;
2587
2588 return 0;
2589 err:
2590 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
2591 cma_deref_id(id_priv);
2592 return ret;
2593 }
2594 EXPORT_SYMBOL(rdma_resolve_route);
2595
cma_set_loopback(struct sockaddr * addr)2596 static void cma_set_loopback(struct sockaddr *addr)
2597 {
2598 switch (addr->sa_family) {
2599 case AF_INET:
2600 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2601 break;
2602 case AF_INET6:
2603 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2604 0, 0, 0, htonl(1));
2605 break;
2606 default:
2607 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2608 0, 0, 0, htonl(1));
2609 break;
2610 }
2611 }
2612
cma_bind_loopback(struct rdma_id_private * id_priv)2613 static int cma_bind_loopback(struct rdma_id_private *id_priv)
2614 {
2615 struct cma_device *cma_dev, *cur_dev;
2616 struct ib_port_attr port_attr;
2617 union ib_gid gid;
2618 u16 pkey;
2619 int ret;
2620 u8 p;
2621
2622 cma_dev = NULL;
2623 mutex_lock(&lock);
2624 list_for_each_entry(cur_dev, &dev_list, list) {
2625 if (cma_family(id_priv) == AF_IB &&
2626 !rdma_cap_ib_cm(cur_dev->device, 1))
2627 continue;
2628
2629 if (!cma_dev)
2630 cma_dev = cur_dev;
2631
2632 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2633 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2634 port_attr.state == IB_PORT_ACTIVE) {
2635 cma_dev = cur_dev;
2636 goto port_found;
2637 }
2638 }
2639 }
2640
2641 if (!cma_dev) {
2642 ret = -ENODEV;
2643 goto out;
2644 }
2645
2646 p = 1;
2647
2648 port_found:
2649 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
2650 if (ret)
2651 goto out;
2652
2653 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2654 if (ret)
2655 goto out;
2656
2657 id_priv->id.route.addr.dev_addr.dev_type =
2658 (rdma_protocol_ib(cma_dev->device, p)) ?
2659 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2660
2661 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2662 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2663 id_priv->id.port_num = p;
2664 cma_attach_to_dev(id_priv, cma_dev);
2665 cma_set_loopback(cma_src_addr(id_priv));
2666 out:
2667 mutex_unlock(&lock);
2668 return ret;
2669 }
2670
addr_handler(int status,struct sockaddr * src_addr,struct rdma_dev_addr * dev_addr,void * context)2671 static void addr_handler(int status, struct sockaddr *src_addr,
2672 struct rdma_dev_addr *dev_addr, void *context)
2673 {
2674 struct rdma_id_private *id_priv = context;
2675 struct rdma_cm_event event;
2676
2677 memset(&event, 0, sizeof event);
2678 mutex_lock(&id_priv->handler_mutex);
2679 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2680 RDMA_CM_ADDR_RESOLVED))
2681 goto out;
2682
2683 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
2684 if (!status && !id_priv->cma_dev)
2685 status = cma_acquire_dev(id_priv, NULL);
2686
2687 if (status) {
2688 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2689 RDMA_CM_ADDR_BOUND))
2690 goto out;
2691 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2692 event.status = status;
2693 } else
2694 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2695
2696 if (id_priv->id.event_handler(&id_priv->id, &event)) {
2697 cma_exch(id_priv, RDMA_CM_DESTROYING);
2698 mutex_unlock(&id_priv->handler_mutex);
2699 cma_deref_id(id_priv);
2700 rdma_destroy_id(&id_priv->id);
2701 return;
2702 }
2703 out:
2704 mutex_unlock(&id_priv->handler_mutex);
2705 cma_deref_id(id_priv);
2706 }
2707
cma_resolve_loopback(struct rdma_id_private * id_priv)2708 static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2709 {
2710 struct cma_work *work;
2711 union ib_gid gid;
2712 int ret;
2713
2714 work = kzalloc(sizeof *work, GFP_KERNEL);
2715 if (!work)
2716 return -ENOMEM;
2717
2718 if (!id_priv->cma_dev) {
2719 ret = cma_bind_loopback(id_priv);
2720 if (ret)
2721 goto err;
2722 }
2723
2724 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2725 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
2726
2727 work->id = id_priv;
2728 INIT_WORK(&work->work, cma_work_handler);
2729 work->old_state = RDMA_CM_ADDR_QUERY;
2730 work->new_state = RDMA_CM_ADDR_RESOLVED;
2731 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2732 queue_work(cma_wq, &work->work);
2733 return 0;
2734 err:
2735 kfree(work);
2736 return ret;
2737 }
2738
cma_resolve_ib_addr(struct rdma_id_private * id_priv)2739 static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2740 {
2741 struct cma_work *work;
2742 int ret;
2743
2744 work = kzalloc(sizeof *work, GFP_KERNEL);
2745 if (!work)
2746 return -ENOMEM;
2747
2748 if (!id_priv->cma_dev) {
2749 ret = cma_resolve_ib_dev(id_priv);
2750 if (ret)
2751 goto err;
2752 }
2753
2754 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2755 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2756
2757 work->id = id_priv;
2758 INIT_WORK(&work->work, cma_work_handler);
2759 work->old_state = RDMA_CM_ADDR_QUERY;
2760 work->new_state = RDMA_CM_ADDR_RESOLVED;
2761 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2762 queue_work(cma_wq, &work->work);
2763 return 0;
2764 err:
2765 kfree(work);
2766 return ret;
2767 }
2768
cma_bind_addr(struct rdma_cm_id * id,struct sockaddr * src_addr,struct sockaddr * dst_addr)2769 static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2770 struct sockaddr *dst_addr)
2771 {
2772 if (!src_addr || !src_addr->sa_family) {
2773 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2774 src_addr->sa_family = dst_addr->sa_family;
2775 if (IS_ENABLED(CONFIG_IPV6) &&
2776 dst_addr->sa_family == AF_INET6) {
2777 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2778 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2779 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2780 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
2781 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
2782 } else if (dst_addr->sa_family == AF_IB) {
2783 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2784 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
2785 }
2786 }
2787 return rdma_bind_addr(id, src_addr);
2788 }
2789
rdma_resolve_addr(struct rdma_cm_id * id,struct sockaddr * src_addr,struct sockaddr * dst_addr,int timeout_ms)2790 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2791 struct sockaddr *dst_addr, int timeout_ms)
2792 {
2793 struct rdma_id_private *id_priv;
2794 int ret;
2795
2796 id_priv = container_of(id, struct rdma_id_private, id);
2797 if (id_priv->state == RDMA_CM_IDLE) {
2798 ret = cma_bind_addr(id, src_addr, dst_addr);
2799 if (ret)
2800 return ret;
2801 }
2802
2803 if (cma_family(id_priv) != dst_addr->sa_family)
2804 return -EINVAL;
2805
2806 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
2807 return -EINVAL;
2808
2809 atomic_inc(&id_priv->refcount);
2810 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
2811 if (cma_any_addr(dst_addr)) {
2812 ret = cma_resolve_loopback(id_priv);
2813 } else {
2814 if (dst_addr->sa_family == AF_IB) {
2815 ret = cma_resolve_ib_addr(id_priv);
2816 } else {
2817 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2818 dst_addr, &id->route.addr.dev_addr,
2819 timeout_ms, addr_handler, id_priv);
2820 }
2821 }
2822 if (ret)
2823 goto err;
2824
2825 return 0;
2826 err:
2827 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
2828 cma_deref_id(id_priv);
2829 return ret;
2830 }
2831 EXPORT_SYMBOL(rdma_resolve_addr);
2832
rdma_set_reuseaddr(struct rdma_cm_id * id,int reuse)2833 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2834 {
2835 struct rdma_id_private *id_priv;
2836 unsigned long flags;
2837 int ret;
2838
2839 id_priv = container_of(id, struct rdma_id_private, id);
2840 spin_lock_irqsave(&id_priv->lock, flags);
2841 if (reuse || id_priv->state == RDMA_CM_IDLE) {
2842 id_priv->reuseaddr = reuse;
2843 ret = 0;
2844 } else {
2845 ret = -EINVAL;
2846 }
2847 spin_unlock_irqrestore(&id_priv->lock, flags);
2848 return ret;
2849 }
2850 EXPORT_SYMBOL(rdma_set_reuseaddr);
2851
rdma_set_afonly(struct rdma_cm_id * id,int afonly)2852 int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2853 {
2854 struct rdma_id_private *id_priv;
2855 unsigned long flags;
2856 int ret;
2857
2858 id_priv = container_of(id, struct rdma_id_private, id);
2859 spin_lock_irqsave(&id_priv->lock, flags);
2860 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2861 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2862 id_priv->afonly = afonly;
2863 ret = 0;
2864 } else {
2865 ret = -EINVAL;
2866 }
2867 spin_unlock_irqrestore(&id_priv->lock, flags);
2868 return ret;
2869 }
2870 EXPORT_SYMBOL(rdma_set_afonly);
2871
cma_bind_port(struct rdma_bind_list * bind_list,struct rdma_id_private * id_priv)2872 static void cma_bind_port(struct rdma_bind_list *bind_list,
2873 struct rdma_id_private *id_priv)
2874 {
2875 struct sockaddr *addr;
2876 struct sockaddr_ib *sib;
2877 u64 sid, mask;
2878 __be16 port;
2879
2880 addr = cma_src_addr(id_priv);
2881 port = htons(bind_list->port);
2882
2883 switch (addr->sa_family) {
2884 case AF_INET:
2885 ((struct sockaddr_in *) addr)->sin_port = port;
2886 break;
2887 case AF_INET6:
2888 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2889 break;
2890 case AF_IB:
2891 sib = (struct sockaddr_ib *) addr;
2892 sid = be64_to_cpu(sib->sib_sid);
2893 mask = be64_to_cpu(sib->sib_sid_mask);
2894 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2895 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2896 break;
2897 }
2898 id_priv->bind_list = bind_list;
2899 hlist_add_head(&id_priv->node, &bind_list->owners);
2900 }
2901
cma_alloc_port(enum rdma_port_space ps,struct rdma_id_private * id_priv,unsigned short snum)2902 static int cma_alloc_port(enum rdma_port_space ps,
2903 struct rdma_id_private *id_priv, unsigned short snum)
2904 {
2905 struct rdma_bind_list *bind_list;
2906 int ret;
2907
2908 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
2909 if (!bind_list)
2910 return -ENOMEM;
2911
2912 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
2913 snum);
2914 if (ret < 0)
2915 goto err;
2916
2917 bind_list->ps = ps;
2918 bind_list->port = (unsigned short)ret;
2919 cma_bind_port(bind_list, id_priv);
2920 return 0;
2921 err:
2922 kfree(bind_list);
2923 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
2924 }
2925
cma_alloc_any_port(enum rdma_port_space ps,struct rdma_id_private * id_priv)2926 static int cma_alloc_any_port(enum rdma_port_space ps,
2927 struct rdma_id_private *id_priv)
2928 {
2929 static unsigned int last_used_port;
2930 int low, high, remaining;
2931 unsigned int rover;
2932 struct net *net = id_priv->id.route.addr.dev_addr.net;
2933
2934 inet_get_local_port_range(net, &low, &high);
2935 remaining = (high - low) + 1;
2936 rover = prandom_u32() % remaining + low;
2937 retry:
2938 if (last_used_port != rover &&
2939 !cma_ps_find(net, ps, (unsigned short)rover)) {
2940 int ret = cma_alloc_port(ps, id_priv, rover);
2941 /*
2942 * Remember previously used port number in order to avoid
2943 * re-using same port immediately after it is closed.
2944 */
2945 if (!ret)
2946 last_used_port = rover;
2947 if (ret != -EADDRNOTAVAIL)
2948 return ret;
2949 }
2950 if (--remaining) {
2951 rover++;
2952 if ((rover < low) || (rover > high))
2953 rover = low;
2954 goto retry;
2955 }
2956 return -EADDRNOTAVAIL;
2957 }
2958
2959 /*
2960 * Check that the requested port is available. This is called when trying to
2961 * bind to a specific port, or when trying to listen on a bound port. In
2962 * the latter case, the provided id_priv may already be on the bind_list, but
2963 * we still need to check that it's okay to start listening.
2964 */
cma_check_port(struct rdma_bind_list * bind_list,struct rdma_id_private * id_priv,uint8_t reuseaddr)2965 static int cma_check_port(struct rdma_bind_list *bind_list,
2966 struct rdma_id_private *id_priv, uint8_t reuseaddr)
2967 {
2968 struct rdma_id_private *cur_id;
2969 struct sockaddr *addr, *cur_addr;
2970
2971 addr = cma_src_addr(id_priv);
2972 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
2973 if (id_priv == cur_id)
2974 continue;
2975
2976 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2977 cur_id->reuseaddr)
2978 continue;
2979
2980 cur_addr = cma_src_addr(cur_id);
2981 if (id_priv->afonly && cur_id->afonly &&
2982 (addr->sa_family != cur_addr->sa_family))
2983 continue;
2984
2985 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2986 return -EADDRNOTAVAIL;
2987
2988 if (!cma_addr_cmp(addr, cur_addr))
2989 return -EADDRINUSE;
2990 }
2991 return 0;
2992 }
2993
cma_use_port(enum rdma_port_space ps,struct rdma_id_private * id_priv)2994 static int cma_use_port(enum rdma_port_space ps,
2995 struct rdma_id_private *id_priv)
2996 {
2997 struct rdma_bind_list *bind_list;
2998 unsigned short snum;
2999 int ret;
3000
3001 snum = ntohs(cma_port(cma_src_addr(id_priv)));
3002 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3003 return -EACCES;
3004
3005 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
3006 if (!bind_list) {
3007 ret = cma_alloc_port(ps, id_priv, snum);
3008 } else {
3009 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3010 if (!ret)
3011 cma_bind_port(bind_list, id_priv);
3012 }
3013 return ret;
3014 }
3015
cma_bind_listen(struct rdma_id_private * id_priv)3016 static int cma_bind_listen(struct rdma_id_private *id_priv)
3017 {
3018 struct rdma_bind_list *bind_list = id_priv->bind_list;
3019 int ret = 0;
3020
3021 mutex_lock(&lock);
3022 if (bind_list->owners.first->next)
3023 ret = cma_check_port(bind_list, id_priv, 0);
3024 mutex_unlock(&lock);
3025 return ret;
3026 }
3027
cma_select_inet_ps(struct rdma_id_private * id_priv)3028 static enum rdma_port_space cma_select_inet_ps(
3029 struct rdma_id_private *id_priv)
3030 {
3031 switch (id_priv->id.ps) {
3032 case RDMA_PS_TCP:
3033 case RDMA_PS_UDP:
3034 case RDMA_PS_IPOIB:
3035 case RDMA_PS_IB:
3036 return id_priv->id.ps;
3037 default:
3038
3039 return 0;
3040 }
3041 }
3042
cma_select_ib_ps(struct rdma_id_private * id_priv)3043 static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
3044 {
3045 enum rdma_port_space ps = 0;
3046 struct sockaddr_ib *sib;
3047 u64 sid_ps, mask, sid;
3048
3049 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
3050 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3051 sid = be64_to_cpu(sib->sib_sid) & mask;
3052
3053 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3054 sid_ps = RDMA_IB_IP_PS_IB;
3055 ps = RDMA_PS_IB;
3056 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3057 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3058 sid_ps = RDMA_IB_IP_PS_TCP;
3059 ps = RDMA_PS_TCP;
3060 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3061 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3062 sid_ps = RDMA_IB_IP_PS_UDP;
3063 ps = RDMA_PS_UDP;
3064 }
3065
3066 if (ps) {
3067 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3068 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3069 be64_to_cpu(sib->sib_sid_mask));
3070 }
3071 return ps;
3072 }
3073
cma_get_port(struct rdma_id_private * id_priv)3074 static int cma_get_port(struct rdma_id_private *id_priv)
3075 {
3076 enum rdma_port_space ps;
3077 int ret;
3078
3079 if (cma_family(id_priv) != AF_IB)
3080 ps = cma_select_inet_ps(id_priv);
3081 else
3082 ps = cma_select_ib_ps(id_priv);
3083 if (!ps)
3084 return -EPROTONOSUPPORT;
3085
3086 mutex_lock(&lock);
3087 if (cma_any_port(cma_src_addr(id_priv)))
3088 ret = cma_alloc_any_port(ps, id_priv);
3089 else
3090 ret = cma_use_port(ps, id_priv);
3091 mutex_unlock(&lock);
3092
3093 return ret;
3094 }
3095
cma_check_linklocal(struct rdma_dev_addr * dev_addr,struct sockaddr * addr)3096 static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3097 struct sockaddr *addr)
3098 {
3099 #if IS_ENABLED(CONFIG_IPV6)
3100 struct sockaddr_in6 *sin6;
3101
3102 if (addr->sa_family != AF_INET6)
3103 return 0;
3104
3105 sin6 = (struct sockaddr_in6 *) addr;
3106
3107 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3108 return 0;
3109
3110 if (!sin6->sin6_scope_id)
3111 return -EINVAL;
3112
3113 dev_addr->bound_dev_if = sin6->sin6_scope_id;
3114 #endif
3115 return 0;
3116 }
3117
rdma_listen(struct rdma_cm_id * id,int backlog)3118 int rdma_listen(struct rdma_cm_id *id, int backlog)
3119 {
3120 struct rdma_id_private *id_priv;
3121 int ret;
3122
3123 id_priv = container_of(id, struct rdma_id_private, id);
3124 if (id_priv->state == RDMA_CM_IDLE) {
3125 id->route.addr.src_addr.ss_family = AF_INET;
3126 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
3127 if (ret)
3128 return ret;
3129 }
3130
3131 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
3132 return -EINVAL;
3133
3134 if (id_priv->reuseaddr) {
3135 ret = cma_bind_listen(id_priv);
3136 if (ret)
3137 goto err;
3138 }
3139
3140 id_priv->backlog = backlog;
3141 if (id->device) {
3142 if (rdma_cap_ib_cm(id->device, 1)) {
3143 ret = cma_ib_listen(id_priv);
3144 if (ret)
3145 goto err;
3146 } else if (rdma_cap_iw_cm(id->device, 1)) {
3147 ret = cma_iw_listen(id_priv, backlog);
3148 if (ret)
3149 goto err;
3150 } else {
3151 ret = -ENOSYS;
3152 goto err;
3153 }
3154 } else
3155 cma_listen_on_all(id_priv);
3156
3157 return 0;
3158 err:
3159 id_priv->backlog = 0;
3160 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
3161 return ret;
3162 }
3163 EXPORT_SYMBOL(rdma_listen);
3164
rdma_bind_addr(struct rdma_cm_id * id,struct sockaddr * addr)3165 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3166 {
3167 struct rdma_id_private *id_priv;
3168 int ret;
3169
3170 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3171 addr->sa_family != AF_IB)
3172 return -EAFNOSUPPORT;
3173
3174 id_priv = container_of(id, struct rdma_id_private, id);
3175 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
3176 return -EINVAL;
3177
3178 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3179 if (ret)
3180 goto err1;
3181
3182 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
3183 if (!cma_any_addr(addr)) {
3184 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
3185 if (ret)
3186 goto err1;
3187
3188 ret = cma_acquire_dev(id_priv, NULL);
3189 if (ret)
3190 goto err1;
3191 }
3192
3193 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3194 if (addr->sa_family == AF_INET)
3195 id_priv->afonly = 1;
3196 #if IS_ENABLED(CONFIG_IPV6)
3197 else if (addr->sa_family == AF_INET6) {
3198 struct net *net = id_priv->id.route.addr.dev_addr.net;
3199
3200 id_priv->afonly = net->ipv6.sysctl.bindv6only;
3201 }
3202 #endif
3203 }
3204 ret = cma_get_port(id_priv);
3205 if (ret)
3206 goto err2;
3207
3208 return 0;
3209 err2:
3210 if (id_priv->cma_dev)
3211 cma_release_dev(id_priv);
3212 err1:
3213 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
3214 return ret;
3215 }
3216 EXPORT_SYMBOL(rdma_bind_addr);
3217
cma_format_hdr(void * hdr,struct rdma_id_private * id_priv)3218 static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
3219 {
3220 struct cma_hdr *cma_hdr;
3221
3222 cma_hdr = hdr;
3223 cma_hdr->cma_version = CMA_VERSION;
3224 if (cma_family(id_priv) == AF_INET) {
3225 struct sockaddr_in *src4, *dst4;
3226
3227 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3228 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
3229
3230 cma_set_ip_ver(cma_hdr, 4);
3231 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3232 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3233 cma_hdr->port = src4->sin_port;
3234 } else if (cma_family(id_priv) == AF_INET6) {
3235 struct sockaddr_in6 *src6, *dst6;
3236
3237 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3238 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
3239
3240 cma_set_ip_ver(cma_hdr, 6);
3241 cma_hdr->src_addr.ip6 = src6->sin6_addr;
3242 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3243 cma_hdr->port = src6->sin6_port;
3244 }
3245 return 0;
3246 }
3247
cma_sidr_rep_handler(struct ib_cm_id * cm_id,struct ib_cm_event * ib_event)3248 static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3249 struct ib_cm_event *ib_event)
3250 {
3251 struct rdma_id_private *id_priv = cm_id->context;
3252 struct rdma_cm_event event;
3253 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3254 int ret = 0;
3255
3256 mutex_lock(&id_priv->handler_mutex);
3257 if (id_priv->state != RDMA_CM_CONNECT)
3258 goto out;
3259
3260 memset(&event, 0, sizeof event);
3261 switch (ib_event->event) {
3262 case IB_CM_SIDR_REQ_ERROR:
3263 event.event = RDMA_CM_EVENT_UNREACHABLE;
3264 event.status = -ETIMEDOUT;
3265 break;
3266 case IB_CM_SIDR_REP_RECEIVED:
3267 event.param.ud.private_data = ib_event->private_data;
3268 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3269 if (rep->status != IB_SIDR_SUCCESS) {
3270 event.event = RDMA_CM_EVENT_UNREACHABLE;
3271 event.status = ib_event->param.sidr_rep_rcvd.status;
3272 break;
3273 }
3274 ret = cma_set_qkey(id_priv, rep->qkey);
3275 if (ret) {
3276 event.event = RDMA_CM_EVENT_ADDR_ERROR;
3277 event.status = ret;
3278 break;
3279 }
3280 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
3281 id_priv->id.route.path_rec,
3282 &event.param.ud.ah_attr);
3283 event.param.ud.qp_num = rep->qpn;
3284 event.param.ud.qkey = rep->qkey;
3285 event.event = RDMA_CM_EVENT_ESTABLISHED;
3286 event.status = 0;
3287 break;
3288 default:
3289 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
3290 ib_event->event);
3291 goto out;
3292 }
3293
3294 ret = id_priv->id.event_handler(&id_priv->id, &event);
3295 if (ret) {
3296 /* Destroy the CM ID by returning a non-zero value. */
3297 id_priv->cm_id.ib = NULL;
3298 cma_exch(id_priv, RDMA_CM_DESTROYING);
3299 mutex_unlock(&id_priv->handler_mutex);
3300 rdma_destroy_id(&id_priv->id);
3301 return ret;
3302 }
3303 out:
3304 mutex_unlock(&id_priv->handler_mutex);
3305 return ret;
3306 }
3307
cma_resolve_ib_udp(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)3308 static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3309 struct rdma_conn_param *conn_param)
3310 {
3311 struct ib_cm_sidr_req_param req;
3312 struct ib_cm_id *id;
3313 void *private_data;
3314 u8 offset;
3315 int ret;
3316
3317 memset(&req, 0, sizeof req);
3318 offset = cma_user_data_offset(id_priv);
3319 req.private_data_len = offset + conn_param->private_data_len;
3320 if (req.private_data_len < conn_param->private_data_len)
3321 return -EINVAL;
3322
3323 if (req.private_data_len) {
3324 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3325 if (!private_data)
3326 return -ENOMEM;
3327 } else {
3328 private_data = NULL;
3329 }
3330
3331 if (conn_param->private_data && conn_param->private_data_len)
3332 memcpy(private_data + offset, conn_param->private_data,
3333 conn_param->private_data_len);
3334
3335 if (private_data) {
3336 ret = cma_format_hdr(private_data, id_priv);
3337 if (ret)
3338 goto out;
3339 req.private_data = private_data;
3340 }
3341
3342 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3343 id_priv);
3344 if (IS_ERR(id)) {
3345 ret = PTR_ERR(id);
3346 goto out;
3347 }
3348 id_priv->cm_id.ib = id;
3349
3350 req.path = id_priv->id.route.path_rec;
3351 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3352 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3353 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3354
3355 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3356 if (ret) {
3357 ib_destroy_cm_id(id_priv->cm_id.ib);
3358 id_priv->cm_id.ib = NULL;
3359 }
3360 out:
3361 kfree(private_data);
3362 return ret;
3363 }
3364
cma_connect_ib(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)3365 static int cma_connect_ib(struct rdma_id_private *id_priv,
3366 struct rdma_conn_param *conn_param)
3367 {
3368 struct ib_cm_req_param req;
3369 struct rdma_route *route;
3370 void *private_data;
3371 struct ib_cm_id *id;
3372 u8 offset;
3373 int ret;
3374
3375 memset(&req, 0, sizeof req);
3376 offset = cma_user_data_offset(id_priv);
3377 req.private_data_len = offset + conn_param->private_data_len;
3378 if (req.private_data_len < conn_param->private_data_len)
3379 return -EINVAL;
3380
3381 if (req.private_data_len) {
3382 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3383 if (!private_data)
3384 return -ENOMEM;
3385 } else {
3386 private_data = NULL;
3387 }
3388
3389 if (conn_param->private_data && conn_param->private_data_len)
3390 memcpy(private_data + offset, conn_param->private_data,
3391 conn_param->private_data_len);
3392
3393 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3394 if (IS_ERR(id)) {
3395 ret = PTR_ERR(id);
3396 goto out;
3397 }
3398 id_priv->cm_id.ib = id;
3399
3400 route = &id_priv->id.route;
3401 if (private_data) {
3402 ret = cma_format_hdr(private_data, id_priv);
3403 if (ret)
3404 goto out;
3405 req.private_data = private_data;
3406 }
3407
3408 req.primary_path = &route->path_rec[0];
3409 if (route->num_paths == 2)
3410 req.alternate_path = &route->path_rec[1];
3411
3412 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
3413 req.qp_num = id_priv->qp_num;
3414 req.qp_type = id_priv->id.qp_type;
3415 req.starting_psn = id_priv->seq_num;
3416 req.responder_resources = conn_param->responder_resources;
3417 req.initiator_depth = conn_param->initiator_depth;
3418 req.flow_control = conn_param->flow_control;
3419 req.retry_count = min_t(u8, 7, conn_param->retry_count);
3420 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3421 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3422 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3423 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3424 req.srq = id_priv->srq ? 1 : 0;
3425
3426 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3427 out:
3428 if (ret && !IS_ERR(id)) {
3429 ib_destroy_cm_id(id);
3430 id_priv->cm_id.ib = NULL;
3431 }
3432
3433 kfree(private_data);
3434 return ret;
3435 }
3436
cma_connect_iw(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)3437 static int cma_connect_iw(struct rdma_id_private *id_priv,
3438 struct rdma_conn_param *conn_param)
3439 {
3440 struct iw_cm_id *cm_id;
3441 int ret;
3442 struct iw_cm_conn_param iw_param;
3443
3444 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
3445 if (IS_ERR(cm_id))
3446 return PTR_ERR(cm_id);
3447
3448 cm_id->tos = id_priv->tos;
3449 id_priv->cm_id.iw = cm_id;
3450
3451 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3452 rdma_addr_size(cma_src_addr(id_priv)));
3453 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3454 rdma_addr_size(cma_dst_addr(id_priv)));
3455
3456 ret = cma_modify_qp_rtr(id_priv, conn_param);
3457 if (ret)
3458 goto out;
3459
3460 if (conn_param) {
3461 iw_param.ord = conn_param->initiator_depth;
3462 iw_param.ird = conn_param->responder_resources;
3463 iw_param.private_data = conn_param->private_data;
3464 iw_param.private_data_len = conn_param->private_data_len;
3465 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3466 } else {
3467 memset(&iw_param, 0, sizeof iw_param);
3468 iw_param.qpn = id_priv->qp_num;
3469 }
3470 ret = iw_cm_connect(cm_id, &iw_param);
3471 out:
3472 if (ret) {
3473 iw_destroy_cm_id(cm_id);
3474 id_priv->cm_id.iw = NULL;
3475 }
3476 return ret;
3477 }
3478
rdma_connect(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)3479 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3480 {
3481 struct rdma_id_private *id_priv;
3482 int ret;
3483
3484 id_priv = container_of(id, struct rdma_id_private, id);
3485 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
3486 return -EINVAL;
3487
3488 if (!id->qp) {
3489 id_priv->qp_num = conn_param->qp_num;
3490 id_priv->srq = conn_param->srq;
3491 }
3492
3493 if (rdma_cap_ib_cm(id->device, id->port_num)) {
3494 if (id->qp_type == IB_QPT_UD)
3495 ret = cma_resolve_ib_udp(id_priv, conn_param);
3496 else
3497 ret = cma_connect_ib(id_priv, conn_param);
3498 } else if (rdma_cap_iw_cm(id->device, id->port_num))
3499 ret = cma_connect_iw(id_priv, conn_param);
3500 else
3501 ret = -ENOSYS;
3502 if (ret)
3503 goto err;
3504
3505 return 0;
3506 err:
3507 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
3508 return ret;
3509 }
3510 EXPORT_SYMBOL(rdma_connect);
3511
cma_accept_ib(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)3512 static int cma_accept_ib(struct rdma_id_private *id_priv,
3513 struct rdma_conn_param *conn_param)
3514 {
3515 struct ib_cm_rep_param rep;
3516 int ret;
3517
3518 ret = cma_modify_qp_rtr(id_priv, conn_param);
3519 if (ret)
3520 goto out;
3521
3522 ret = cma_modify_qp_rts(id_priv, conn_param);
3523 if (ret)
3524 goto out;
3525
3526 memset(&rep, 0, sizeof rep);
3527 rep.qp_num = id_priv->qp_num;
3528 rep.starting_psn = id_priv->seq_num;
3529 rep.private_data = conn_param->private_data;
3530 rep.private_data_len = conn_param->private_data_len;
3531 rep.responder_resources = conn_param->responder_resources;
3532 rep.initiator_depth = conn_param->initiator_depth;
3533 rep.failover_accepted = 0;
3534 rep.flow_control = conn_param->flow_control;
3535 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
3536 rep.srq = id_priv->srq ? 1 : 0;
3537
3538 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3539 out:
3540 return ret;
3541 }
3542
cma_accept_iw(struct rdma_id_private * id_priv,struct rdma_conn_param * conn_param)3543 static int cma_accept_iw(struct rdma_id_private *id_priv,
3544 struct rdma_conn_param *conn_param)
3545 {
3546 struct iw_cm_conn_param iw_param;
3547 int ret;
3548
3549 if (!conn_param)
3550 return -EINVAL;
3551
3552 ret = cma_modify_qp_rtr(id_priv, conn_param);
3553 if (ret)
3554 return ret;
3555
3556 iw_param.ord = conn_param->initiator_depth;
3557 iw_param.ird = conn_param->responder_resources;
3558 iw_param.private_data = conn_param->private_data;
3559 iw_param.private_data_len = conn_param->private_data_len;
3560 if (id_priv->id.qp) {
3561 iw_param.qpn = id_priv->qp_num;
3562 } else
3563 iw_param.qpn = conn_param->qp_num;
3564
3565 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3566 }
3567
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)3568 static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
3569 enum ib_cm_sidr_status status, u32 qkey,
3570 const void *private_data, int private_data_len)
3571 {
3572 struct ib_cm_sidr_rep_param rep;
3573 int ret;
3574
3575 memset(&rep, 0, sizeof rep);
3576 rep.status = status;
3577 if (status == IB_SIDR_SUCCESS) {
3578 ret = cma_set_qkey(id_priv, qkey);
3579 if (ret)
3580 return ret;
3581 rep.qp_num = id_priv->qp_num;
3582 rep.qkey = id_priv->qkey;
3583 }
3584 rep.private_data = private_data;
3585 rep.private_data_len = private_data_len;
3586
3587 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3588 }
3589
rdma_accept(struct rdma_cm_id * id,struct rdma_conn_param * conn_param)3590 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3591 {
3592 struct rdma_id_private *id_priv;
3593 int ret;
3594
3595 id_priv = container_of(id, struct rdma_id_private, id);
3596
3597 id_priv->owner = task_pid_nr(current);
3598
3599 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
3600 return -EINVAL;
3601
3602 if (!id->qp && conn_param) {
3603 id_priv->qp_num = conn_param->qp_num;
3604 id_priv->srq = conn_param->srq;
3605 }
3606
3607 if (rdma_cap_ib_cm(id->device, id->port_num)) {
3608 if (id->qp_type == IB_QPT_UD) {
3609 if (conn_param)
3610 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3611 conn_param->qkey,
3612 conn_param->private_data,
3613 conn_param->private_data_len);
3614 else
3615 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
3616 0, NULL, 0);
3617 } else {
3618 if (conn_param)
3619 ret = cma_accept_ib(id_priv, conn_param);
3620 else
3621 ret = cma_rep_recv(id_priv);
3622 }
3623 } else if (rdma_cap_iw_cm(id->device, id->port_num))
3624 ret = cma_accept_iw(id_priv, conn_param);
3625 else
3626 ret = -ENOSYS;
3627
3628 if (ret)
3629 goto reject;
3630
3631 return 0;
3632 reject:
3633 cma_modify_qp_err(id_priv);
3634 rdma_reject(id, NULL, 0);
3635 return ret;
3636 }
3637 EXPORT_SYMBOL(rdma_accept);
3638
rdma_notify(struct rdma_cm_id * id,enum ib_event_type event)3639 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3640 {
3641 struct rdma_id_private *id_priv;
3642 int ret;
3643
3644 id_priv = container_of(id, struct rdma_id_private, id);
3645 if (!id_priv->cm_id.ib)
3646 return -EINVAL;
3647
3648 switch (id->device->node_type) {
3649 case RDMA_NODE_IB_CA:
3650 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3651 break;
3652 default:
3653 ret = 0;
3654 break;
3655 }
3656 return ret;
3657 }
3658 EXPORT_SYMBOL(rdma_notify);
3659
rdma_reject(struct rdma_cm_id * id,const void * private_data,u8 private_data_len)3660 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3661 u8 private_data_len)
3662 {
3663 struct rdma_id_private *id_priv;
3664 int ret;
3665
3666 id_priv = container_of(id, struct rdma_id_private, id);
3667 if (!id_priv->cm_id.ib)
3668 return -EINVAL;
3669
3670 if (rdma_cap_ib_cm(id->device, id->port_num)) {
3671 if (id->qp_type == IB_QPT_UD)
3672 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
3673 private_data, private_data_len);
3674 else
3675 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3676 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3677 0, private_data, private_data_len);
3678 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3679 ret = iw_cm_reject(id_priv->cm_id.iw,
3680 private_data, private_data_len);
3681 } else
3682 ret = -ENOSYS;
3683
3684 return ret;
3685 }
3686 EXPORT_SYMBOL(rdma_reject);
3687
rdma_disconnect(struct rdma_cm_id * id)3688 int rdma_disconnect(struct rdma_cm_id *id)
3689 {
3690 struct rdma_id_private *id_priv;
3691 int ret;
3692
3693 id_priv = container_of(id, struct rdma_id_private, id);
3694 if (!id_priv->cm_id.ib)
3695 return -EINVAL;
3696
3697 if (rdma_cap_ib_cm(id->device, id->port_num)) {
3698 ret = cma_modify_qp_err(id_priv);
3699 if (ret)
3700 goto out;
3701 /* Initiate or respond to a disconnect. */
3702 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3703 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
3704 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
3705 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
3706 } else
3707 ret = -EINVAL;
3708
3709 out:
3710 return ret;
3711 }
3712 EXPORT_SYMBOL(rdma_disconnect);
3713
cma_ib_mc_handler(int status,struct ib_sa_multicast * multicast)3714 static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3715 {
3716 struct rdma_id_private *id_priv;
3717 struct cma_multicast *mc = multicast->context;
3718 struct rdma_cm_event event;
3719 int ret = 0;
3720
3721 id_priv = mc->id_priv;
3722 mutex_lock(&id_priv->handler_mutex);
3723 if (id_priv->state != RDMA_CM_ADDR_BOUND &&
3724 id_priv->state != RDMA_CM_ADDR_RESOLVED)
3725 goto out;
3726
3727 if (!status)
3728 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
3729 mutex_lock(&id_priv->qp_mutex);
3730 if (!status && id_priv->id.qp)
3731 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
3732 be16_to_cpu(multicast->rec.mlid));
3733 mutex_unlock(&id_priv->qp_mutex);
3734
3735 memset(&event, 0, sizeof event);
3736 event.status = status;
3737 event.param.ud.private_data = mc->context;
3738 if (!status) {
3739 struct rdma_dev_addr *dev_addr =
3740 &id_priv->id.route.addr.dev_addr;
3741 struct net_device *ndev =
3742 dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3743 enum ib_gid_type gid_type =
3744 id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3745 rdma_start_port(id_priv->cma_dev->device)];
3746
3747 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3748 ib_init_ah_from_mcmember(id_priv->id.device,
3749 id_priv->id.port_num, &multicast->rec,
3750 ndev, gid_type,
3751 &event.param.ud.ah_attr);
3752 event.param.ud.qp_num = 0xFFFFFF;
3753 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3754 if (ndev)
3755 dev_put(ndev);
3756 } else
3757 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3758
3759 ret = id_priv->id.event_handler(&id_priv->id, &event);
3760 if (ret) {
3761 cma_exch(id_priv, RDMA_CM_DESTROYING);
3762 mutex_unlock(&id_priv->handler_mutex);
3763 rdma_destroy_id(&id_priv->id);
3764 return 0;
3765 }
3766
3767 out:
3768 mutex_unlock(&id_priv->handler_mutex);
3769 return 0;
3770 }
3771
cma_set_mgid(struct rdma_id_private * id_priv,struct sockaddr * addr,union ib_gid * mgid)3772 static void cma_set_mgid(struct rdma_id_private *id_priv,
3773 struct sockaddr *addr, union ib_gid *mgid)
3774 {
3775 unsigned char mc_map[MAX_ADDR_LEN];
3776 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3777 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3778 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3779
3780 if (cma_any_addr(addr)) {
3781 memset(mgid, 0, sizeof *mgid);
3782 } else if ((addr->sa_family == AF_INET6) &&
3783 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
3784 0xFF10A01B)) {
3785 /* IPv6 address is an SA assigned MGID. */
3786 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3787 } else if (addr->sa_family == AF_IB) {
3788 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
3789 } else if ((addr->sa_family == AF_INET6)) {
3790 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3791 if (id_priv->id.ps == RDMA_PS_UDP)
3792 mc_map[7] = 0x01; /* Use RDMA CM signature */
3793 *mgid = *(union ib_gid *) (mc_map + 4);
3794 } else {
3795 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
3796 if (id_priv->id.ps == RDMA_PS_UDP)
3797 mc_map[7] = 0x01; /* Use RDMA CM signature */
3798 *mgid = *(union ib_gid *) (mc_map + 4);
3799 }
3800 }
3801
cma_query_sa_classport_info_cb(int status,struct ib_class_port_info * rec,void * context)3802 static void cma_query_sa_classport_info_cb(int status,
3803 struct ib_class_port_info *rec,
3804 void *context)
3805 {
3806 struct class_port_info_context *cb_ctx = context;
3807
3808 WARN_ON(!context);
3809
3810 if (status || !rec) {
3811 pr_debug("RDMA CM: %s port %u failed query ClassPortInfo status: %d\n",
3812 cb_ctx->device->name, cb_ctx->port_num, status);
3813 goto out;
3814 }
3815
3816 memcpy(cb_ctx->class_port_info, rec, sizeof(struct ib_class_port_info));
3817
3818 out:
3819 complete(&cb_ctx->done);
3820 }
3821
cma_query_sa_classport_info(struct ib_device * device,u8 port_num,struct ib_class_port_info * class_port_info)3822 static int cma_query_sa_classport_info(struct ib_device *device, u8 port_num,
3823 struct ib_class_port_info *class_port_info)
3824 {
3825 struct class_port_info_context *cb_ctx;
3826 int ret;
3827
3828 cb_ctx = kmalloc(sizeof(*cb_ctx), GFP_KERNEL);
3829 if (!cb_ctx)
3830 return -ENOMEM;
3831
3832 cb_ctx->device = device;
3833 cb_ctx->class_port_info = class_port_info;
3834 cb_ctx->port_num = port_num;
3835 init_completion(&cb_ctx->done);
3836
3837 ret = ib_sa_classport_info_rec_query(&sa_client, device, port_num,
3838 CMA_QUERY_CLASSPORT_INFO_TIMEOUT,
3839 GFP_KERNEL, cma_query_sa_classport_info_cb,
3840 cb_ctx, &cb_ctx->sa_query);
3841 if (ret < 0) {
3842 pr_err("RDMA CM: %s port %u failed to send ClassPortInfo query, ret: %d\n",
3843 device->name, port_num, ret);
3844 goto out;
3845 }
3846
3847 wait_for_completion(&cb_ctx->done);
3848
3849 out:
3850 kfree(cb_ctx);
3851 return ret;
3852 }
3853
cma_join_ib_multicast(struct rdma_id_private * id_priv,struct cma_multicast * mc)3854 static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3855 struct cma_multicast *mc)
3856 {
3857 struct ib_sa_mcmember_rec rec;
3858 struct ib_class_port_info class_port_info;
3859 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3860 ib_sa_comp_mask comp_mask;
3861 int ret;
3862
3863 ib_addr_get_mgid(dev_addr, &rec.mgid);
3864 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3865 &rec.mgid, &rec);
3866 if (ret)
3867 return ret;
3868
3869 ret = cma_set_qkey(id_priv, 0);
3870 if (ret)
3871 return ret;
3872
3873 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
3874 rec.qkey = cpu_to_be32(id_priv->qkey);
3875 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
3876 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3877 rec.join_state = mc->join_state;
3878
3879 if (rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) {
3880 ret = cma_query_sa_classport_info(id_priv->id.device,
3881 id_priv->id.port_num,
3882 &class_port_info);
3883
3884 if (ret)
3885 return ret;
3886
3887 if (!(ib_get_cpi_capmask2(&class_port_info) &
3888 IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT)) {
3889 pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
3890 "RDMA CM: SM doesn't support Send Only Full Member option\n",
3891 id_priv->id.device->name, id_priv->id.port_num);
3892 return -EOPNOTSUPP;
3893 }
3894 }
3895
3896 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3897 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3898 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3899 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3900 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3901
3902 if (id_priv->id.ps == RDMA_PS_IPOIB)
3903 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
3904 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3905 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3906 IB_SA_MCMEMBER_REC_MTU |
3907 IB_SA_MCMEMBER_REC_HOP_LIMIT;
3908
3909 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3910 id_priv->id.port_num, &rec,
3911 comp_mask, GFP_KERNEL,
3912 cma_ib_mc_handler, mc);
3913 return PTR_ERR_OR_ZERO(mc->multicast.ib);
3914 }
3915
iboe_mcast_work_handler(struct work_struct * work)3916 static void iboe_mcast_work_handler(struct work_struct *work)
3917 {
3918 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3919 struct cma_multicast *mc = mw->mc;
3920 struct ib_sa_multicast *m = mc->multicast.ib;
3921
3922 mc->multicast.ib->context = mc;
3923 cma_ib_mc_handler(0, m);
3924 kref_put(&mc->mcref, release_mc);
3925 kfree(mw);
3926 }
3927
cma_iboe_set_mgid(struct sockaddr * addr,union ib_gid * mgid)3928 static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3929 {
3930 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3931 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3932
3933 if (cma_any_addr(addr)) {
3934 memset(mgid, 0, sizeof *mgid);
3935 } else if (addr->sa_family == AF_INET6) {
3936 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3937 } else {
3938 mgid->raw[0] = 0xff;
3939 mgid->raw[1] = 0x0e;
3940 mgid->raw[2] = 0;
3941 mgid->raw[3] = 0;
3942 mgid->raw[4] = 0;
3943 mgid->raw[5] = 0;
3944 mgid->raw[6] = 0;
3945 mgid->raw[7] = 0;
3946 mgid->raw[8] = 0;
3947 mgid->raw[9] = 0;
3948 mgid->raw[10] = 0xff;
3949 mgid->raw[11] = 0xff;
3950 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3951 }
3952 }
3953
cma_iboe_join_multicast(struct rdma_id_private * id_priv,struct cma_multicast * mc)3954 static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3955 struct cma_multicast *mc)
3956 {
3957 struct iboe_mcast_work *work;
3958 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3959 int err = 0;
3960 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3961 struct net_device *ndev = NULL;
3962 enum ib_gid_type gid_type;
3963 bool send_only;
3964
3965 send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
3966
3967 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3968 return -EINVAL;
3969
3970 work = kzalloc(sizeof *work, GFP_KERNEL);
3971 if (!work)
3972 return -ENOMEM;
3973
3974 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3975 if (!mc->multicast.ib) {
3976 err = -ENOMEM;
3977 goto out1;
3978 }
3979
3980 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3981
3982 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3983 if (id_priv->id.ps == RDMA_PS_UDP)
3984 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3985
3986 if (dev_addr->bound_dev_if)
3987 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3988 if (!ndev) {
3989 err = -ENODEV;
3990 goto out2;
3991 }
3992 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3993 mc->multicast.ib->rec.hop_limit = 1;
3994 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3995
3996 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3997 rdma_start_port(id_priv->cma_dev->device)];
3998 if (addr->sa_family == AF_INET) {
3999 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4000 mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
4001 if (!send_only) {
4002 err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid,
4003 true);
4004 if (!err)
4005 mc->igmp_joined = true;
4006 }
4007 }
4008 } else {
4009 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4010 err = -ENOTSUPP;
4011 }
4012 dev_put(ndev);
4013 if (err || !mc->multicast.ib->rec.mtu) {
4014 if (!err)
4015 err = -EINVAL;
4016 goto out2;
4017 }
4018 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4019 &mc->multicast.ib->rec.port_gid);
4020 work->id = id_priv;
4021 work->mc = mc;
4022 INIT_WORK(&work->work, iboe_mcast_work_handler);
4023 kref_get(&mc->mcref);
4024 queue_work(cma_wq, &work->work);
4025
4026 return 0;
4027
4028 out2:
4029 kfree(mc->multicast.ib);
4030 out1:
4031 kfree(work);
4032 return err;
4033 }
4034
rdma_join_multicast(struct rdma_cm_id * id,struct sockaddr * addr,u8 join_state,void * context)4035 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
4036 u8 join_state, void *context)
4037 {
4038 struct rdma_id_private *id_priv;
4039 struct cma_multicast *mc;
4040 int ret;
4041
4042 if (!id->device)
4043 return -EINVAL;
4044
4045 id_priv = container_of(id, struct rdma_id_private, id);
4046 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
4047 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
4048 return -EINVAL;
4049
4050 mc = kmalloc(sizeof *mc, GFP_KERNEL);
4051 if (!mc)
4052 return -ENOMEM;
4053
4054 memcpy(&mc->addr, addr, rdma_addr_size(addr));
4055 mc->context = context;
4056 mc->id_priv = id_priv;
4057 mc->igmp_joined = false;
4058 mc->join_state = join_state;
4059 spin_lock(&id_priv->lock);
4060 list_add(&mc->list, &id_priv->mc_list);
4061 spin_unlock(&id_priv->lock);
4062
4063 if (rdma_protocol_roce(id->device, id->port_num)) {
4064 kref_init(&mc->mcref);
4065 ret = cma_iboe_join_multicast(id_priv, mc);
4066 } else if (rdma_cap_ib_mcast(id->device, id->port_num))
4067 ret = cma_join_ib_multicast(id_priv, mc);
4068 else
4069 ret = -ENOSYS;
4070
4071 if (ret) {
4072 spin_lock_irq(&id_priv->lock);
4073 list_del(&mc->list);
4074 spin_unlock_irq(&id_priv->lock);
4075 kfree(mc);
4076 }
4077 return ret;
4078 }
4079 EXPORT_SYMBOL(rdma_join_multicast);
4080
rdma_leave_multicast(struct rdma_cm_id * id,struct sockaddr * addr)4081 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4082 {
4083 struct rdma_id_private *id_priv;
4084 struct cma_multicast *mc;
4085
4086 id_priv = container_of(id, struct rdma_id_private, id);
4087 spin_lock_irq(&id_priv->lock);
4088 list_for_each_entry(mc, &id_priv->mc_list, list) {
4089 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
4090 list_del(&mc->list);
4091 spin_unlock_irq(&id_priv->lock);
4092
4093 if (id->qp)
4094 ib_detach_mcast(id->qp,
4095 &mc->multicast.ib->rec.mgid,
4096 be16_to_cpu(mc->multicast.ib->rec.mlid));
4097
4098 BUG_ON(id_priv->cma_dev->device != id->device);
4099
4100 if (rdma_cap_ib_mcast(id->device, id->port_num)) {
4101 ib_sa_free_multicast(mc->multicast.ib);
4102 kfree(mc);
4103 } else if (rdma_protocol_roce(id->device, id->port_num)) {
4104 if (mc->igmp_joined) {
4105 struct rdma_dev_addr *dev_addr =
4106 &id->route.addr.dev_addr;
4107 struct net_device *ndev = NULL;
4108
4109 if (dev_addr->bound_dev_if)
4110 ndev = dev_get_by_index(&init_net,
4111 dev_addr->bound_dev_if);
4112 if (ndev) {
4113 cma_igmp_send(ndev,
4114 &mc->multicast.ib->rec.mgid,
4115 false);
4116 dev_put(ndev);
4117 }
4118 mc->igmp_joined = false;
4119 }
4120 kref_put(&mc->mcref, release_mc);
4121 }
4122 return;
4123 }
4124 }
4125 spin_unlock_irq(&id_priv->lock);
4126 }
4127 EXPORT_SYMBOL(rdma_leave_multicast);
4128
cma_netdev_change(struct net_device * ndev,struct rdma_id_private * id_priv)4129 static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4130 {
4131 struct rdma_dev_addr *dev_addr;
4132 struct cma_ndev_work *work;
4133
4134 dev_addr = &id_priv->id.route.addr.dev_addr;
4135
4136 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
4137 (net_eq(dev_net(ndev), dev_addr->net)) &&
4138 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
4139 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4140 ndev->name, &id_priv->id);
4141 work = kzalloc(sizeof *work, GFP_KERNEL);
4142 if (!work)
4143 return -ENOMEM;
4144
4145 INIT_WORK(&work->work, cma_ndev_work_handler);
4146 work->id = id_priv;
4147 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4148 atomic_inc(&id_priv->refcount);
4149 queue_work(cma_wq, &work->work);
4150 }
4151
4152 return 0;
4153 }
4154
cma_netdev_callback(struct notifier_block * self,unsigned long event,void * ptr)4155 static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
4156 void *ptr)
4157 {
4158 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
4159 struct cma_device *cma_dev;
4160 struct rdma_id_private *id_priv;
4161 int ret = NOTIFY_DONE;
4162
4163 if (event != NETDEV_BONDING_FAILOVER)
4164 return NOTIFY_DONE;
4165
4166 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
4167 return NOTIFY_DONE;
4168
4169 mutex_lock(&lock);
4170 list_for_each_entry(cma_dev, &dev_list, list)
4171 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4172 ret = cma_netdev_change(ndev, id_priv);
4173 if (ret)
4174 goto out;
4175 }
4176
4177 out:
4178 mutex_unlock(&lock);
4179 return ret;
4180 }
4181
4182 static struct notifier_block cma_nb = {
4183 .notifier_call = cma_netdev_callback
4184 };
4185
cma_add_one(struct ib_device * device)4186 static void cma_add_one(struct ib_device *device)
4187 {
4188 struct cma_device *cma_dev;
4189 struct rdma_id_private *id_priv;
4190 unsigned int i;
4191 unsigned long supported_gids = 0;
4192
4193 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4194 if (!cma_dev)
4195 return;
4196
4197 cma_dev->device = device;
4198 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4199 sizeof(*cma_dev->default_gid_type),
4200 GFP_KERNEL);
4201 if (!cma_dev->default_gid_type) {
4202 kfree(cma_dev);
4203 return;
4204 }
4205 for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4206 supported_gids = roce_gid_type_mask_support(device, i);
4207 WARN_ON(!supported_gids);
4208 cma_dev->default_gid_type[i - rdma_start_port(device)] =
4209 find_first_bit(&supported_gids, BITS_PER_LONG);
4210 }
4211
4212 init_completion(&cma_dev->comp);
4213 atomic_set(&cma_dev->refcount, 1);
4214 INIT_LIST_HEAD(&cma_dev->id_list);
4215 ib_set_client_data(device, &cma_client, cma_dev);
4216
4217 mutex_lock(&lock);
4218 list_add_tail(&cma_dev->list, &dev_list);
4219 list_for_each_entry(id_priv, &listen_any_list, list)
4220 cma_listen_on_dev(id_priv, cma_dev);
4221 mutex_unlock(&lock);
4222 }
4223
cma_remove_id_dev(struct rdma_id_private * id_priv)4224 static int cma_remove_id_dev(struct rdma_id_private *id_priv)
4225 {
4226 struct rdma_cm_event event;
4227 enum rdma_cm_state state;
4228 int ret = 0;
4229
4230 /* Record that we want to remove the device */
4231 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
4232 if (state == RDMA_CM_DESTROYING)
4233 return 0;
4234
4235 cma_cancel_operation(id_priv, state);
4236 mutex_lock(&id_priv->handler_mutex);
4237
4238 /* Check for destruction from another callback. */
4239 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
4240 goto out;
4241
4242 memset(&event, 0, sizeof event);
4243 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
4244 ret = id_priv->id.event_handler(&id_priv->id, &event);
4245 out:
4246 mutex_unlock(&id_priv->handler_mutex);
4247 return ret;
4248 }
4249
cma_process_remove(struct cma_device * cma_dev)4250 static void cma_process_remove(struct cma_device *cma_dev)
4251 {
4252 struct rdma_id_private *id_priv;
4253 int ret;
4254
4255 mutex_lock(&lock);
4256 while (!list_empty(&cma_dev->id_list)) {
4257 id_priv = list_entry(cma_dev->id_list.next,
4258 struct rdma_id_private, list);
4259
4260 list_del(&id_priv->listen_list);
4261 list_del_init(&id_priv->list);
4262 atomic_inc(&id_priv->refcount);
4263 mutex_unlock(&lock);
4264
4265 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
4266 cma_deref_id(id_priv);
4267 if (ret)
4268 rdma_destroy_id(&id_priv->id);
4269
4270 mutex_lock(&lock);
4271 }
4272 mutex_unlock(&lock);
4273
4274 cma_deref_dev(cma_dev);
4275 wait_for_completion(&cma_dev->comp);
4276 }
4277
cma_remove_one(struct ib_device * device,void * client_data)4278 static void cma_remove_one(struct ib_device *device, void *client_data)
4279 {
4280 struct cma_device *cma_dev = client_data;
4281
4282 if (!cma_dev)
4283 return;
4284
4285 mutex_lock(&lock);
4286 list_del(&cma_dev->list);
4287 mutex_unlock(&lock);
4288
4289 cma_process_remove(cma_dev);
4290 kfree(cma_dev->default_gid_type);
4291 kfree(cma_dev);
4292 }
4293
cma_get_id_stats(struct sk_buff * skb,struct netlink_callback * cb)4294 static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
4295 {
4296 struct nlmsghdr *nlh;
4297 struct rdma_cm_id_stats *id_stats;
4298 struct rdma_id_private *id_priv;
4299 struct rdma_cm_id *id = NULL;
4300 struct cma_device *cma_dev;
4301 int i_dev = 0, i_id = 0;
4302
4303 /*
4304 * We export all of the IDs as a sequence of messages. Each
4305 * ID gets its own netlink message.
4306 */
4307 mutex_lock(&lock);
4308
4309 list_for_each_entry(cma_dev, &dev_list, list) {
4310 if (i_dev < cb->args[0]) {
4311 i_dev++;
4312 continue;
4313 }
4314
4315 i_id = 0;
4316 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4317 if (i_id < cb->args[1]) {
4318 i_id++;
4319 continue;
4320 }
4321
4322 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
4323 sizeof *id_stats, RDMA_NL_RDMA_CM,
4324 RDMA_NL_RDMA_CM_ID_STATS,
4325 NLM_F_MULTI);
4326 if (!id_stats)
4327 goto out;
4328
4329 memset(id_stats, 0, sizeof *id_stats);
4330 id = &id_priv->id;
4331 id_stats->node_type = id->route.addr.dev_addr.dev_type;
4332 id_stats->port_num = id->port_num;
4333 id_stats->bound_dev_if =
4334 id->route.addr.dev_addr.bound_dev_if;
4335
4336 if (ibnl_put_attr(skb, nlh,
4337 rdma_addr_size(cma_src_addr(id_priv)),
4338 cma_src_addr(id_priv),
4339 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
4340 goto out;
4341 if (ibnl_put_attr(skb, nlh,
4342 rdma_addr_size(cma_dst_addr(id_priv)),
4343 cma_dst_addr(id_priv),
4344 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
4345 goto out;
4346
4347 id_stats->pid = id_priv->owner;
4348 id_stats->port_space = id->ps;
4349 id_stats->cm_state = id_priv->state;
4350 id_stats->qp_num = id_priv->qp_num;
4351 id_stats->qp_type = id->qp_type;
4352
4353 i_id++;
4354 }
4355
4356 cb->args[1] = 0;
4357 i_dev++;
4358 }
4359
4360 out:
4361 mutex_unlock(&lock);
4362 cb->args[0] = i_dev;
4363 cb->args[1] = i_id;
4364
4365 return skb->len;
4366 }
4367
4368 static const struct ibnl_client_cbs cma_cb_table[] = {
4369 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
4370 .module = THIS_MODULE },
4371 };
4372
cma_init_net(struct net * net)4373 static int cma_init_net(struct net *net)
4374 {
4375 struct cma_pernet *pernet = cma_pernet(net);
4376
4377 idr_init(&pernet->tcp_ps);
4378 idr_init(&pernet->udp_ps);
4379 idr_init(&pernet->ipoib_ps);
4380 idr_init(&pernet->ib_ps);
4381
4382 return 0;
4383 }
4384
cma_exit_net(struct net * net)4385 static void cma_exit_net(struct net *net)
4386 {
4387 struct cma_pernet *pernet = cma_pernet(net);
4388
4389 idr_destroy(&pernet->tcp_ps);
4390 idr_destroy(&pernet->udp_ps);
4391 idr_destroy(&pernet->ipoib_ps);
4392 idr_destroy(&pernet->ib_ps);
4393 }
4394
4395 static struct pernet_operations cma_pernet_operations = {
4396 .init = cma_init_net,
4397 .exit = cma_exit_net,
4398 .id = &cma_pernet_id,
4399 .size = sizeof(struct cma_pernet),
4400 };
4401
cma_init(void)4402 static int __init cma_init(void)
4403 {
4404 int ret;
4405
4406 cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
4407 if (!cma_wq)
4408 return -ENOMEM;
4409
4410 ret = register_pernet_subsys(&cma_pernet_operations);
4411 if (ret)
4412 goto err_wq;
4413
4414 ib_sa_register_client(&sa_client);
4415 rdma_addr_register_client(&addr_client);
4416 register_netdevice_notifier(&cma_nb);
4417
4418 ret = ib_register_client(&cma_client);
4419 if (ret)
4420 goto err;
4421
4422 if (ibnl_add_client(RDMA_NL_RDMA_CM, ARRAY_SIZE(cma_cb_table),
4423 cma_cb_table))
4424 pr_warn("RDMA CMA: failed to add netlink callback\n");
4425 cma_configfs_init();
4426
4427 return 0;
4428
4429 err:
4430 unregister_netdevice_notifier(&cma_nb);
4431 rdma_addr_unregister_client(&addr_client);
4432 ib_sa_unregister_client(&sa_client);
4433 err_wq:
4434 destroy_workqueue(cma_wq);
4435 return ret;
4436 }
4437
cma_cleanup(void)4438 static void __exit cma_cleanup(void)
4439 {
4440 cma_configfs_exit();
4441 ibnl_remove_client(RDMA_NL_RDMA_CM);
4442 ib_unregister_client(&cma_client);
4443 unregister_netdevice_notifier(&cma_nb);
4444 rdma_addr_unregister_client(&addr_client);
4445 ib_sa_unregister_client(&sa_client);
4446 unregister_pernet_subsys(&cma_pernet_operations);
4447 destroy_workqueue(cma_wq);
4448 }
4449
4450 module_init(cma_init);
4451 module_exit(cma_cleanup);
4452