]> Git Repo - J-linux.git/blob - drivers/infiniband/core/nldev.c
RDMA/netlink: Audit policy settings for netlink attributes
[J-linux.git] / drivers / infiniband / core / nldev.c
1 /*
2  * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Neither the names of the copyright holders nor the names of its
13  *    contributors may be used to endorse or promote products derived from
14  *    this software without specific prior written permission.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") version 2 as published by the Free
18  * Software Foundation.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <linux/module.h>
34 #include <linux/pid.h>
35 #include <linux/pid_namespace.h>
36 #include <linux/mutex.h>
37 #include <net/netlink.h>
38 #include <rdma/rdma_cm.h>
39 #include <rdma/rdma_netlink.h>
40
41 #include "core_priv.h"
42 #include "cma_priv.h"
43 #include "restrack.h"
44
45 /*
46  * Sort array elements by the netlink attribute name
47  */
48 static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
49         [RDMA_NLDEV_ATTR_CHARDEV]               = { .type = NLA_U64 },
50         [RDMA_NLDEV_ATTR_CHARDEV_ABI]           = { .type = NLA_U64 },
51         [RDMA_NLDEV_ATTR_CHARDEV_NAME]          = { .type = NLA_NUL_STRING,
52                                         .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
53         [RDMA_NLDEV_ATTR_CHARDEV_TYPE]          = { .type = NLA_NUL_STRING,
54                                         .len = RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE },
55         [RDMA_NLDEV_ATTR_DEV_INDEX]             = { .type = NLA_U32 },
56         [RDMA_NLDEV_ATTR_DEV_NAME]              = { .type = NLA_NUL_STRING,
57                                         .len = IB_DEVICE_NAME_MAX },
58         [RDMA_NLDEV_ATTR_DEV_NODE_TYPE]         = { .type = NLA_U8 },
59         [RDMA_NLDEV_ATTR_DEV_PROTOCOL]          = { .type = NLA_NUL_STRING,
60                                         .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
61         [RDMA_NLDEV_ATTR_DRIVER]                = { .type = NLA_NESTED },
62         [RDMA_NLDEV_ATTR_DRIVER_ENTRY]          = { .type = NLA_NESTED },
63         [RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE]     = { .type = NLA_U8 },
64         [RDMA_NLDEV_ATTR_DRIVER_STRING]         = { .type = NLA_NUL_STRING,
65                                         .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
66         [RDMA_NLDEV_ATTR_DRIVER_S32]            = { .type = NLA_S32 },
67         [RDMA_NLDEV_ATTR_DRIVER_S64]            = { .type = NLA_S64 },
68         [RDMA_NLDEV_ATTR_DRIVER_U32]            = { .type = NLA_U32 },
69         [RDMA_NLDEV_ATTR_DRIVER_U64]            = { .type = NLA_U64 },
70         [RDMA_NLDEV_ATTR_FW_VERSION]            = { .type = NLA_NUL_STRING,
71                                         .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
72         [RDMA_NLDEV_ATTR_LID]                   = { .type = NLA_U32 },
73         [RDMA_NLDEV_ATTR_LINK_TYPE]             = { .type = NLA_NUL_STRING,
74                                         .len = IFNAMSIZ },
75         [RDMA_NLDEV_ATTR_LMC]                   = { .type = NLA_U8 },
76         [RDMA_NLDEV_ATTR_NDEV_INDEX]            = { .type = NLA_U32 },
77         [RDMA_NLDEV_ATTR_NDEV_NAME]             = { .type = NLA_NUL_STRING,
78                                         .len = IFNAMSIZ },
79         [RDMA_NLDEV_ATTR_NODE_GUID]             = { .type = NLA_U64 },
80         [RDMA_NLDEV_ATTR_PORT_INDEX]            = { .type = NLA_U32 },
81         [RDMA_NLDEV_ATTR_PORT_PHYS_STATE]       = { .type = NLA_U8 },
82         [RDMA_NLDEV_ATTR_PORT_STATE]            = { .type = NLA_U8 },
83         [RDMA_NLDEV_ATTR_RES_CM_ID]             = { .type = NLA_NESTED },
84         [RDMA_NLDEV_ATTR_RES_CM_IDN]            = { .type = NLA_U32 },
85         [RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY]       = { .type = NLA_NESTED },
86         [RDMA_NLDEV_ATTR_RES_CQ]                = { .type = NLA_NESTED },
87         [RDMA_NLDEV_ATTR_RES_CQE]               = { .type = NLA_U32 },
88         [RDMA_NLDEV_ATTR_RES_CQN]               = { .type = NLA_U32 },
89         [RDMA_NLDEV_ATTR_RES_CQ_ENTRY]          = { .type = NLA_NESTED },
90         [RDMA_NLDEV_ATTR_RES_CTXN]              = { .type = NLA_U32 },
91         [RDMA_NLDEV_ATTR_RES_DST_ADDR]          = {
92                         .len = sizeof(struct __kernel_sockaddr_storage) },
93         [RDMA_NLDEV_ATTR_RES_IOVA]              = { .type = NLA_U64 },
94         [RDMA_NLDEV_ATTR_RES_KERN_NAME]         = { .type = NLA_NUL_STRING,
95                                         .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
96         [RDMA_NLDEV_ATTR_RES_LKEY]              = { .type = NLA_U32 },
97         [RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY]    = { .type = NLA_U32 },
98         [RDMA_NLDEV_ATTR_RES_LQPN]              = { .type = NLA_U32 },
99         [RDMA_NLDEV_ATTR_RES_MR]                = { .type = NLA_NESTED },
100         [RDMA_NLDEV_ATTR_RES_MRLEN]             = { .type = NLA_U64 },
101         [RDMA_NLDEV_ATTR_RES_MRN]               = { .type = NLA_U32 },
102         [RDMA_NLDEV_ATTR_RES_MR_ENTRY]          = { .type = NLA_NESTED },
103         [RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE]    = { .type = NLA_U8 },
104         [RDMA_NLDEV_ATTR_RES_PD]                = { .type = NLA_NESTED },
105         [RDMA_NLDEV_ATTR_RES_PDN]               = { .type = NLA_U32 },
106         [RDMA_NLDEV_ATTR_RES_PD_ENTRY]          = { .type = NLA_NESTED },
107         [RDMA_NLDEV_ATTR_RES_PID]               = { .type = NLA_U32 },
108         [RDMA_NLDEV_ATTR_RES_POLL_CTX]          = { .type = NLA_U8 },
109         [RDMA_NLDEV_ATTR_RES_PS]                = { .type = NLA_U32 },
110         [RDMA_NLDEV_ATTR_RES_QP]                = { .type = NLA_NESTED },
111         [RDMA_NLDEV_ATTR_RES_QP_ENTRY]          = { .type = NLA_NESTED },
112         [RDMA_NLDEV_ATTR_RES_RKEY]              = { .type = NLA_U32 },
113         [RDMA_NLDEV_ATTR_RES_RQPN]              = { .type = NLA_U32 },
114         [RDMA_NLDEV_ATTR_RES_RQ_PSN]            = { .type = NLA_U32 },
115         [RDMA_NLDEV_ATTR_RES_SQ_PSN]            = { .type = NLA_U32 },
116         [RDMA_NLDEV_ATTR_RES_SRC_ADDR]          = {
117                         .len = sizeof(struct __kernel_sockaddr_storage) },
118         [RDMA_NLDEV_ATTR_RES_STATE]             = { .type = NLA_U8 },
119         [RDMA_NLDEV_ATTR_RES_SUMMARY]           = { .type = NLA_NESTED },
120         [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY]     = { .type = NLA_NESTED },
121         [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]= { .type = NLA_U64 },
122         [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]= { .type = NLA_NUL_STRING,
123                                         .len = RDMA_NLDEV_ATTR_EMPTY_STRING },
124         [RDMA_NLDEV_ATTR_RES_TYPE]              = { .type = NLA_U8 },
125         [RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY]= { .type = NLA_U32 },
126         [RDMA_NLDEV_ATTR_RES_USECNT]            = { .type = NLA_U64 },
127         [RDMA_NLDEV_ATTR_SM_LID]                = { .type = NLA_U32 },
128         [RDMA_NLDEV_ATTR_SUBNET_PREFIX]         = { .type = NLA_U64 },
129         [RDMA_NLDEV_ATTR_SYS_IMAGE_GUID]        = { .type = NLA_U64 },
130         [RDMA_NLDEV_ATTR_UVERBS_DRIVER_ID]      = { .type = NLA_U32 },
131         [RDMA_NLDEV_NET_NS_FD]                  = { .type = NLA_U32 },
132         [RDMA_NLDEV_SYS_ATTR_NETNS_MODE]        = { .type = NLA_U8 },
133 };
134
135 static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
136                                       enum rdma_nldev_print_type print_type)
137 {
138         if (nla_put_string(msg, RDMA_NLDEV_ATTR_DRIVER_STRING, name))
139                 return -EMSGSIZE;
140         if (print_type != RDMA_NLDEV_PRINT_TYPE_UNSPEC &&
141             nla_put_u8(msg, RDMA_NLDEV_ATTR_DRIVER_PRINT_TYPE, print_type))
142                 return -EMSGSIZE;
143
144         return 0;
145 }
146
147 static int _rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name,
148                                    enum rdma_nldev_print_type print_type,
149                                    u32 value)
150 {
151         if (put_driver_name_print_type(msg, name, print_type))
152                 return -EMSGSIZE;
153         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DRIVER_U32, value))
154                 return -EMSGSIZE;
155
156         return 0;
157 }
158
159 static int _rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name,
160                                    enum rdma_nldev_print_type print_type,
161                                    u64 value)
162 {
163         if (put_driver_name_print_type(msg, name, print_type))
164                 return -EMSGSIZE;
165         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_DRIVER_U64, value,
166                               RDMA_NLDEV_ATTR_PAD))
167                 return -EMSGSIZE;
168
169         return 0;
170 }
171
172 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value)
173 {
174         return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
175                                        value);
176 }
177 EXPORT_SYMBOL(rdma_nl_put_driver_u32);
178
179 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
180                                u32 value)
181 {
182         return _rdma_nl_put_driver_u32(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
183                                        value);
184 }
185 EXPORT_SYMBOL(rdma_nl_put_driver_u32_hex);
186
187 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value)
188 {
189         return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_UNSPEC,
190                                        value);
191 }
192 EXPORT_SYMBOL(rdma_nl_put_driver_u64);
193
194 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, u64 value)
195 {
196         return _rdma_nl_put_driver_u64(msg, name, RDMA_NLDEV_PRINT_TYPE_HEX,
197                                        value);
198 }
199 EXPORT_SYMBOL(rdma_nl_put_driver_u64_hex);
200
201 static int fill_nldev_handle(struct sk_buff *msg, struct ib_device *device)
202 {
203         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_DEV_INDEX, device->index))
204                 return -EMSGSIZE;
205         if (nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_NAME,
206                            dev_name(&device->dev)))
207                 return -EMSGSIZE;
208
209         return 0;
210 }
211
212 static int fill_dev_info(struct sk_buff *msg, struct ib_device *device)
213 {
214         char fw[IB_FW_VERSION_NAME_MAX];
215         int ret = 0;
216         u8 port;
217
218         if (fill_nldev_handle(msg, device))
219                 return -EMSGSIZE;
220
221         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, rdma_end_port(device)))
222                 return -EMSGSIZE;
223
224         BUILD_BUG_ON(sizeof(device->attrs.device_cap_flags) != sizeof(u64));
225         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
226                               device->attrs.device_cap_flags,
227                               RDMA_NLDEV_ATTR_PAD))
228                 return -EMSGSIZE;
229
230         ib_get_device_fw_str(device, fw);
231         /* Device without FW has strlen(fw) = 0 */
232         if (strlen(fw) && nla_put_string(msg, RDMA_NLDEV_ATTR_FW_VERSION, fw))
233                 return -EMSGSIZE;
234
235         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_NODE_GUID,
236                               be64_to_cpu(device->node_guid),
237                               RDMA_NLDEV_ATTR_PAD))
238                 return -EMSGSIZE;
239         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SYS_IMAGE_GUID,
240                               be64_to_cpu(device->attrs.sys_image_guid),
241                               RDMA_NLDEV_ATTR_PAD))
242                 return -EMSGSIZE;
243         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_DEV_NODE_TYPE, device->node_type))
244                 return -EMSGSIZE;
245
246         /*
247          * Link type is determined on first port and mlx4 device
248          * which can potentially have two different link type for the same
249          * IB device is considered as better to be avoided in the future,
250          */
251         port = rdma_start_port(device);
252         if (rdma_cap_opa_mad(device, port))
253                 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "opa");
254         else if (rdma_protocol_ib(device, port))
255                 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "ib");
256         else if (rdma_protocol_iwarp(device, port))
257                 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "iw");
258         else if (rdma_protocol_roce(device, port))
259                 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL, "roce");
260         else if (rdma_protocol_usnic(device, port))
261                 ret = nla_put_string(msg, RDMA_NLDEV_ATTR_DEV_PROTOCOL,
262                                      "usnic");
263         return ret;
264 }
265
266 static int fill_port_info(struct sk_buff *msg,
267                           struct ib_device *device, u32 port,
268                           const struct net *net)
269 {
270         struct net_device *netdev = NULL;
271         struct ib_port_attr attr;
272         int ret;
273         u64 cap_flags = 0;
274
275         if (fill_nldev_handle(msg, device))
276                 return -EMSGSIZE;
277
278         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, port))
279                 return -EMSGSIZE;
280
281         ret = ib_query_port(device, port, &attr);
282         if (ret)
283                 return ret;
284
285         if (rdma_protocol_ib(device, port)) {
286                 BUILD_BUG_ON((sizeof(attr.port_cap_flags) +
287                                 sizeof(attr.port_cap_flags2)) > sizeof(u64));
288                 cap_flags = attr.port_cap_flags |
289                         ((u64)attr.port_cap_flags2 << 32);
290                 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CAP_FLAGS,
291                                       cap_flags, RDMA_NLDEV_ATTR_PAD))
292                         return -EMSGSIZE;
293                 if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_SUBNET_PREFIX,
294                                       attr.subnet_prefix, RDMA_NLDEV_ATTR_PAD))
295                         return -EMSGSIZE;
296                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_LID, attr.lid))
297                         return -EMSGSIZE;
298                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_SM_LID, attr.sm_lid))
299                         return -EMSGSIZE;
300                 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_LMC, attr.lmc))
301                         return -EMSGSIZE;
302         }
303         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_STATE, attr.state))
304                 return -EMSGSIZE;
305         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_PORT_PHYS_STATE, attr.phys_state))
306                 return -EMSGSIZE;
307
308         netdev = ib_device_get_netdev(device, port);
309         if (netdev && net_eq(dev_net(netdev), net)) {
310                 ret = nla_put_u32(msg,
311                                   RDMA_NLDEV_ATTR_NDEV_INDEX, netdev->ifindex);
312                 if (ret)
313                         goto out;
314                 ret = nla_put_string(msg,
315                                      RDMA_NLDEV_ATTR_NDEV_NAME, netdev->name);
316         }
317
318 out:
319         if (netdev)
320                 dev_put(netdev);
321         return ret;
322 }
323
324 static int fill_res_info_entry(struct sk_buff *msg,
325                                const char *name, u64 curr)
326 {
327         struct nlattr *entry_attr;
328
329         entry_attr = nla_nest_start_noflag(msg,
330                                            RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY);
331         if (!entry_attr)
332                 return -EMSGSIZE;
333
334         if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME, name))
335                 goto err;
336         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR, curr,
337                               RDMA_NLDEV_ATTR_PAD))
338                 goto err;
339
340         nla_nest_end(msg, entry_attr);
341         return 0;
342
343 err:
344         nla_nest_cancel(msg, entry_attr);
345         return -EMSGSIZE;
346 }
347
348 static int fill_res_info(struct sk_buff *msg, struct ib_device *device)
349 {
350         static const char * const names[RDMA_RESTRACK_MAX] = {
351                 [RDMA_RESTRACK_PD] = "pd",
352                 [RDMA_RESTRACK_CQ] = "cq",
353                 [RDMA_RESTRACK_QP] = "qp",
354                 [RDMA_RESTRACK_CM_ID] = "cm_id",
355                 [RDMA_RESTRACK_MR] = "mr",
356                 [RDMA_RESTRACK_CTX] = "ctx",
357         };
358
359         struct nlattr *table_attr;
360         int ret, i, curr;
361
362         if (fill_nldev_handle(msg, device))
363                 return -EMSGSIZE;
364
365         table_attr = nla_nest_start_noflag(msg, RDMA_NLDEV_ATTR_RES_SUMMARY);
366         if (!table_attr)
367                 return -EMSGSIZE;
368
369         for (i = 0; i < RDMA_RESTRACK_MAX; i++) {
370                 if (!names[i])
371                         continue;
372                 curr = rdma_restrack_count(device, i,
373                                            task_active_pid_ns(current));
374                 ret = fill_res_info_entry(msg, names[i], curr);
375                 if (ret)
376                         goto err;
377         }
378
379         nla_nest_end(msg, table_attr);
380         return 0;
381
382 err:
383         nla_nest_cancel(msg, table_attr);
384         return ret;
385 }
386
387 static int fill_res_name_pid(struct sk_buff *msg,
388                              struct rdma_restrack_entry *res)
389 {
390         /*
391          * For user resources, user is should read /proc/PID/comm to get the
392          * name of the task file.
393          */
394         if (rdma_is_kernel_res(res)) {
395                 if (nla_put_string(msg, RDMA_NLDEV_ATTR_RES_KERN_NAME,
396                     res->kern_name))
397                         return -EMSGSIZE;
398         } else {
399                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PID,
400                     task_pid_vnr(res->task)))
401                         return -EMSGSIZE;
402         }
403         return 0;
404 }
405
406 static bool fill_res_entry(struct ib_device *dev, struct sk_buff *msg,
407                            struct rdma_restrack_entry *res)
408 {
409         if (!dev->ops.fill_res_entry)
410                 return false;
411         return dev->ops.fill_res_entry(msg, res);
412 }
413
414 static int fill_res_qp_entry(struct sk_buff *msg, bool has_cap_net_admin,
415                              struct rdma_restrack_entry *res, uint32_t port)
416 {
417         struct ib_qp *qp = container_of(res, struct ib_qp, res);
418         struct ib_device *dev = qp->device;
419         struct ib_qp_init_attr qp_init_attr;
420         struct ib_qp_attr qp_attr;
421         int ret;
422
423         ret = ib_query_qp(qp, &qp_attr, 0, &qp_init_attr);
424         if (ret)
425                 return ret;
426
427         if (port && port != qp_attr.port_num)
428                 return -EAGAIN;
429
430         /* In create_qp() port is not set yet */
431         if (qp_attr.port_num &&
432             nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, qp_attr.port_num))
433                 goto err;
434
435         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, qp->qp_num))
436                 goto err;
437         if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC) {
438                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQPN,
439                                 qp_attr.dest_qp_num))
440                         goto err;
441                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RQ_PSN,
442                                 qp_attr.rq_psn))
443                         goto err;
444         }
445
446         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_SQ_PSN, qp_attr.sq_psn))
447                 goto err;
448
449         if (qp->qp_type == IB_QPT_RC || qp->qp_type == IB_QPT_UC ||
450             qp->qp_type == IB_QPT_XRC_INI || qp->qp_type == IB_QPT_XRC_TGT) {
451                 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_PATH_MIG_STATE,
452                                qp_attr.path_mig_state))
453                         goto err;
454         }
455         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, qp->qp_type))
456                 goto err;
457         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, qp_attr.qp_state))
458                 goto err;
459
460         if (!rdma_is_kernel_res(res) &&
461             nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, qp->pd->res.id))
462                 goto err;
463
464         if (fill_res_name_pid(msg, res))
465                 goto err;
466
467         if (fill_res_entry(dev, msg, res))
468                 goto err;
469
470         return 0;
471
472 err:    return -EMSGSIZE;
473 }
474
475 static int fill_res_cm_id_entry(struct sk_buff *msg, bool has_cap_net_admin,
476                                 struct rdma_restrack_entry *res, uint32_t port)
477 {
478         struct rdma_id_private *id_priv =
479                                 container_of(res, struct rdma_id_private, res);
480         struct ib_device *dev = id_priv->id.device;
481         struct rdma_cm_id *cm_id = &id_priv->id;
482
483         if (port && port != cm_id->port_num)
484                 return 0;
485
486         if (cm_id->port_num &&
487             nla_put_u32(msg, RDMA_NLDEV_ATTR_PORT_INDEX, cm_id->port_num))
488                 goto err;
489
490         if (id_priv->qp_num) {
491                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LQPN, id_priv->qp_num))
492                         goto err;
493                 if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_TYPE, cm_id->qp_type))
494                         goto err;
495         }
496
497         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PS, cm_id->ps))
498                 goto err;
499
500         if (nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_STATE, id_priv->state))
501                 goto err;
502
503         if (cm_id->route.addr.src_addr.ss_family &&
504             nla_put(msg, RDMA_NLDEV_ATTR_RES_SRC_ADDR,
505                     sizeof(cm_id->route.addr.src_addr),
506                     &cm_id->route.addr.src_addr))
507                 goto err;
508         if (cm_id->route.addr.dst_addr.ss_family &&
509             nla_put(msg, RDMA_NLDEV_ATTR_RES_DST_ADDR,
510                     sizeof(cm_id->route.addr.dst_addr),
511                     &cm_id->route.addr.dst_addr))
512                 goto err;
513
514         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CM_IDN, res->id))
515                 goto err;
516
517         if (fill_res_name_pid(msg, res))
518                 goto err;
519
520         if (fill_res_entry(dev, msg, res))
521                 goto err;
522
523         return 0;
524
525 err: return -EMSGSIZE;
526 }
527
528 static int fill_res_cq_entry(struct sk_buff *msg, bool has_cap_net_admin,
529                              struct rdma_restrack_entry *res, uint32_t port)
530 {
531         struct ib_cq *cq = container_of(res, struct ib_cq, res);
532         struct ib_device *dev = cq->device;
533
534         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQE, cq->cqe))
535                 goto err;
536         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
537                               atomic_read(&cq->usecnt), RDMA_NLDEV_ATTR_PAD))
538                 goto err;
539
540         /* Poll context is only valid for kernel CQs */
541         if (rdma_is_kernel_res(res) &&
542             nla_put_u8(msg, RDMA_NLDEV_ATTR_RES_POLL_CTX, cq->poll_ctx))
543                 goto err;
544
545         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CQN, res->id))
546                 goto err;
547         if (!rdma_is_kernel_res(res) &&
548             nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
549                         cq->uobject->context->res.id))
550                 goto err;
551
552         if (fill_res_name_pid(msg, res))
553                 goto err;
554
555         if (fill_res_entry(dev, msg, res))
556                 goto err;
557
558         return 0;
559
560 err:    return -EMSGSIZE;
561 }
562
563 static int fill_res_mr_entry(struct sk_buff *msg, bool has_cap_net_admin,
564                              struct rdma_restrack_entry *res, uint32_t port)
565 {
566         struct ib_mr *mr = container_of(res, struct ib_mr, res);
567         struct ib_device *dev = mr->pd->device;
568
569         if (has_cap_net_admin) {
570                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_RKEY, mr->rkey))
571                         goto err;
572                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LKEY, mr->lkey))
573                         goto err;
574         }
575
576         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_MRLEN, mr->length,
577                               RDMA_NLDEV_ATTR_PAD))
578                 goto err;
579
580         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_MRN, res->id))
581                 goto err;
582
583         if (!rdma_is_kernel_res(res) &&
584             nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, mr->pd->res.id))
585                 goto err;
586
587         if (fill_res_name_pid(msg, res))
588                 goto err;
589
590         if (fill_res_entry(dev, msg, res))
591                 goto err;
592
593         return 0;
594
595 err:    return -EMSGSIZE;
596 }
597
598 static int fill_res_pd_entry(struct sk_buff *msg, bool has_cap_net_admin,
599                              struct rdma_restrack_entry *res, uint32_t port)
600 {
601         struct ib_pd *pd = container_of(res, struct ib_pd, res);
602         struct ib_device *dev = pd->device;
603
604         if (has_cap_net_admin) {
605                 if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_LOCAL_DMA_LKEY,
606                                 pd->local_dma_lkey))
607                         goto err;
608                 if ((pd->flags & IB_PD_UNSAFE_GLOBAL_RKEY) &&
609                     nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_UNSAFE_GLOBAL_RKEY,
610                                 pd->unsafe_global_rkey))
611                         goto err;
612         }
613         if (nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_RES_USECNT,
614                               atomic_read(&pd->usecnt), RDMA_NLDEV_ATTR_PAD))
615                 goto err;
616
617         if (nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_PDN, res->id))
618                 goto err;
619
620         if (!rdma_is_kernel_res(res) &&
621             nla_put_u32(msg, RDMA_NLDEV_ATTR_RES_CTXN,
622                         pd->uobject->context->res.id))
623                 goto err;
624
625         if (fill_res_name_pid(msg, res))
626                 goto err;
627
628         if (fill_res_entry(dev, msg, res))
629                 goto err;
630
631         return 0;
632
633 err:    return -EMSGSIZE;
634 }
635
636 static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
637                           struct netlink_ext_ack *extack)
638 {
639         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
640         struct ib_device *device;
641         struct sk_buff *msg;
642         u32 index;
643         int err;
644
645         err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
646                                      nldev_policy, extack);
647         if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
648                 return -EINVAL;
649
650         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
651
652         device = ib_device_get_by_index(sock_net(skb->sk), index);
653         if (!device)
654                 return -EINVAL;
655
656         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
657         if (!msg) {
658                 err = -ENOMEM;
659                 goto err;
660         }
661
662         nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
663                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
664                         0, 0);
665
666         err = fill_dev_info(msg, device);
667         if (err)
668                 goto err_free;
669
670         nlmsg_end(msg, nlh);
671
672         ib_device_put(device);
673         return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
674
675 err_free:
676         nlmsg_free(msg);
677 err:
678         ib_device_put(device);
679         return err;
680 }
681
682 static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
683                           struct netlink_ext_ack *extack)
684 {
685         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
686         struct ib_device *device;
687         u32 index;
688         int err;
689
690         err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
691                                      nldev_policy, extack);
692         if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
693                 return -EINVAL;
694
695         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
696         device = ib_device_get_by_index(sock_net(skb->sk), index);
697         if (!device)
698                 return -EINVAL;
699
700         if (tb[RDMA_NLDEV_ATTR_DEV_NAME]) {
701                 char name[IB_DEVICE_NAME_MAX] = {};
702
703                 nla_strlcpy(name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
704                             IB_DEVICE_NAME_MAX);
705                 err = ib_device_rename(device, name);
706                 goto done;
707         }
708
709         if (tb[RDMA_NLDEV_NET_NS_FD]) {
710                 u32 ns_fd;
711
712                 ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
713                 err = ib_device_set_netns_put(skb, device, ns_fd);
714                 goto put_done;
715         }
716
717 done:
718         ib_device_put(device);
719 put_done:
720         return err;
721 }
722
723 static int _nldev_get_dumpit(struct ib_device *device,
724                              struct sk_buff *skb,
725                              struct netlink_callback *cb,
726                              unsigned int idx)
727 {
728         int start = cb->args[0];
729         struct nlmsghdr *nlh;
730
731         if (idx < start)
732                 return 0;
733
734         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
735                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
736                         0, NLM_F_MULTI);
737
738         if (fill_dev_info(skb, device)) {
739                 nlmsg_cancel(skb, nlh);
740                 goto out;
741         }
742
743         nlmsg_end(skb, nlh);
744
745         idx++;
746
747 out:    cb->args[0] = idx;
748         return skb->len;
749 }
750
751 static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
752 {
753         /*
754          * There is no need to take lock, because
755          * we are relying on ib_core's locking.
756          */
757         return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
758 }
759
760 static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
761                                struct netlink_ext_ack *extack)
762 {
763         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
764         struct ib_device *device;
765         struct sk_buff *msg;
766         u32 index;
767         u32 port;
768         int err;
769
770         err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
771                                      nldev_policy, extack);
772         if (err ||
773             !tb[RDMA_NLDEV_ATTR_DEV_INDEX] ||
774             !tb[RDMA_NLDEV_ATTR_PORT_INDEX])
775                 return -EINVAL;
776
777         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
778         device = ib_device_get_by_index(sock_net(skb->sk), index);
779         if (!device)
780                 return -EINVAL;
781
782         port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
783         if (!rdma_is_port_valid(device, port)) {
784                 err = -EINVAL;
785                 goto err;
786         }
787
788         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
789         if (!msg) {
790                 err = -ENOMEM;
791                 goto err;
792         }
793
794         nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
795                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET),
796                         0, 0);
797
798         err = fill_port_info(msg, device, port, sock_net(skb->sk));
799         if (err)
800                 goto err_free;
801
802         nlmsg_end(msg, nlh);
803         ib_device_put(device);
804
805         return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
806
807 err_free:
808         nlmsg_free(msg);
809 err:
810         ib_device_put(device);
811         return err;
812 }
813
814 static int nldev_port_get_dumpit(struct sk_buff *skb,
815                                  struct netlink_callback *cb)
816 {
817         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
818         struct ib_device *device;
819         int start = cb->args[0];
820         struct nlmsghdr *nlh;
821         u32 idx = 0;
822         u32 ifindex;
823         int err;
824         unsigned int p;
825
826         err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
827                                      nldev_policy, NULL);
828         if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
829                 return -EINVAL;
830
831         ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
832         device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
833         if (!device)
834                 return -EINVAL;
835
836         rdma_for_each_port (device, p) {
837                 /*
838                  * The dumpit function returns all information from specific
839                  * index. This specific index is taken from the netlink
840                  * messages request sent by user and it is available
841                  * in cb->args[0].
842                  *
843                  * Usually, the user doesn't fill this field and it causes
844                  * to return everything.
845                  *
846                  */
847                 if (idx < start) {
848                         idx++;
849                         continue;
850                 }
851
852                 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
853                                 cb->nlh->nlmsg_seq,
854                                 RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
855                                                  RDMA_NLDEV_CMD_PORT_GET),
856                                 0, NLM_F_MULTI);
857
858                 if (fill_port_info(skb, device, p, sock_net(skb->sk))) {
859                         nlmsg_cancel(skb, nlh);
860                         goto out;
861                 }
862                 idx++;
863                 nlmsg_end(skb, nlh);
864         }
865
866 out:
867         ib_device_put(device);
868         cb->args[0] = idx;
869         return skb->len;
870 }
871
872 static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
873                               struct netlink_ext_ack *extack)
874 {
875         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
876         struct ib_device *device;
877         struct sk_buff *msg;
878         u32 index;
879         int ret;
880
881         ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
882                                      nldev_policy, extack);
883         if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
884                 return -EINVAL;
885
886         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
887         device = ib_device_get_by_index(sock_net(skb->sk), index);
888         if (!device)
889                 return -EINVAL;
890
891         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
892         if (!msg) {
893                 ret = -ENOMEM;
894                 goto err;
895         }
896
897         nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
898                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
899                         0, 0);
900
901         ret = fill_res_info(msg, device);
902         if (ret)
903                 goto err_free;
904
905         nlmsg_end(msg, nlh);
906         ib_device_put(device);
907         return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
908
909 err_free:
910         nlmsg_free(msg);
911 err:
912         ib_device_put(device);
913         return ret;
914 }
915
916 static int _nldev_res_get_dumpit(struct ib_device *device,
917                                  struct sk_buff *skb,
918                                  struct netlink_callback *cb,
919                                  unsigned int idx)
920 {
921         int start = cb->args[0];
922         struct nlmsghdr *nlh;
923
924         if (idx < start)
925                 return 0;
926
927         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
928                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_RES_GET),
929                         0, NLM_F_MULTI);
930
931         if (fill_res_info(skb, device)) {
932                 nlmsg_cancel(skb, nlh);
933                 goto out;
934         }
935         nlmsg_end(skb, nlh);
936
937         idx++;
938
939 out:
940         cb->args[0] = idx;
941         return skb->len;
942 }
943
944 static int nldev_res_get_dumpit(struct sk_buff *skb,
945                                 struct netlink_callback *cb)
946 {
947         return ib_enum_all_devs(_nldev_res_get_dumpit, skb, cb);
948 }
949
950 struct nldev_fill_res_entry {
951         int (*fill_res_func)(struct sk_buff *msg, bool has_cap_net_admin,
952                              struct rdma_restrack_entry *res, u32 port);
953         enum rdma_nldev_attr nldev_attr;
954         enum rdma_nldev_command nldev_cmd;
955         u8 flags;
956         u32 entry;
957         u32 id;
958 };
959
960 enum nldev_res_flags {
961         NLDEV_PER_DEV = 1 << 0,
962 };
963
964 static const struct nldev_fill_res_entry fill_entries[RDMA_RESTRACK_MAX] = {
965         [RDMA_RESTRACK_QP] = {
966                 .fill_res_func = fill_res_qp_entry,
967                 .nldev_cmd = RDMA_NLDEV_CMD_RES_QP_GET,
968                 .nldev_attr = RDMA_NLDEV_ATTR_RES_QP,
969                 .entry = RDMA_NLDEV_ATTR_RES_QP_ENTRY,
970                 .id = RDMA_NLDEV_ATTR_RES_LQPN,
971         },
972         [RDMA_RESTRACK_CM_ID] = {
973                 .fill_res_func = fill_res_cm_id_entry,
974                 .nldev_cmd = RDMA_NLDEV_CMD_RES_CM_ID_GET,
975                 .nldev_attr = RDMA_NLDEV_ATTR_RES_CM_ID,
976                 .entry = RDMA_NLDEV_ATTR_RES_CM_ID_ENTRY,
977                 .id = RDMA_NLDEV_ATTR_RES_CM_IDN,
978         },
979         [RDMA_RESTRACK_CQ] = {
980                 .fill_res_func = fill_res_cq_entry,
981                 .nldev_cmd = RDMA_NLDEV_CMD_RES_CQ_GET,
982                 .nldev_attr = RDMA_NLDEV_ATTR_RES_CQ,
983                 .flags = NLDEV_PER_DEV,
984                 .entry = RDMA_NLDEV_ATTR_RES_CQ_ENTRY,
985                 .id = RDMA_NLDEV_ATTR_RES_CQN,
986         },
987         [RDMA_RESTRACK_MR] = {
988                 .fill_res_func = fill_res_mr_entry,
989                 .nldev_cmd = RDMA_NLDEV_CMD_RES_MR_GET,
990                 .nldev_attr = RDMA_NLDEV_ATTR_RES_MR,
991                 .flags = NLDEV_PER_DEV,
992                 .entry = RDMA_NLDEV_ATTR_RES_MR_ENTRY,
993                 .id = RDMA_NLDEV_ATTR_RES_MRN,
994         },
995         [RDMA_RESTRACK_PD] = {
996                 .fill_res_func = fill_res_pd_entry,
997                 .nldev_cmd = RDMA_NLDEV_CMD_RES_PD_GET,
998                 .nldev_attr = RDMA_NLDEV_ATTR_RES_PD,
999                 .flags = NLDEV_PER_DEV,
1000                 .entry = RDMA_NLDEV_ATTR_RES_PD_ENTRY,
1001                 .id = RDMA_NLDEV_ATTR_RES_PDN,
1002         },
1003 };
1004
1005 static bool is_visible_in_pid_ns(struct rdma_restrack_entry *res)
1006 {
1007         /*
1008          * 1. Kern resources should be visible in init name space only
1009          * 2. Present only resources visible in the current namespace
1010          */
1011         if (rdma_is_kernel_res(res))
1012                 return task_active_pid_ns(current) == &init_pid_ns;
1013         return task_active_pid_ns(current) == task_active_pid_ns(res->task);
1014 }
1015
1016 static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1017                                struct netlink_ext_ack *extack,
1018                                enum rdma_restrack_type res_type)
1019 {
1020         const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1021         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1022         struct rdma_restrack_entry *res;
1023         struct ib_device *device;
1024         u32 index, id, port = 0;
1025         bool has_cap_net_admin;
1026         struct sk_buff *msg;
1027         int ret;
1028
1029         ret = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1030                                      nldev_policy, extack);
1031         if (ret || !tb[RDMA_NLDEV_ATTR_DEV_INDEX] || !fe->id || !tb[fe->id])
1032                 return -EINVAL;
1033
1034         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1035         device = ib_device_get_by_index(sock_net(skb->sk), index);
1036         if (!device)
1037                 return -EINVAL;
1038
1039         if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1040                 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1041                 if (!rdma_is_port_valid(device, port)) {
1042                         ret = -EINVAL;
1043                         goto err;
1044                 }
1045         }
1046
1047         if ((port && fe->flags & NLDEV_PER_DEV) ||
1048             (!port && ~fe->flags & NLDEV_PER_DEV)) {
1049                 ret = -EINVAL;
1050                 goto err;
1051         }
1052
1053         id = nla_get_u32(tb[fe->id]);
1054         res = rdma_restrack_get_byid(device, res_type, id);
1055         if (IS_ERR(res)) {
1056                 ret = PTR_ERR(res);
1057                 goto err;
1058         }
1059
1060         if (!is_visible_in_pid_ns(res)) {
1061                 ret = -ENOENT;
1062                 goto err_get;
1063         }
1064
1065         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1066         if (!msg) {
1067                 ret = -ENOMEM;
1068                 goto err;
1069         }
1070
1071         nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1072                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
1073                         0, 0);
1074
1075         if (fill_nldev_handle(msg, device)) {
1076                 ret = -EMSGSIZE;
1077                 goto err_free;
1078         }
1079
1080         has_cap_net_admin = netlink_capable(skb, CAP_NET_ADMIN);
1081         ret = fe->fill_res_func(msg, has_cap_net_admin, res, port);
1082         rdma_restrack_put(res);
1083         if (ret)
1084                 goto err_free;
1085
1086         nlmsg_end(msg, nlh);
1087         ib_device_put(device);
1088         return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
1089
1090 err_free:
1091         nlmsg_free(msg);
1092 err_get:
1093         rdma_restrack_put(res);
1094 err:
1095         ib_device_put(device);
1096         return ret;
1097 }
1098
1099 static int res_get_common_dumpit(struct sk_buff *skb,
1100                                  struct netlink_callback *cb,
1101                                  enum rdma_restrack_type res_type)
1102 {
1103         const struct nldev_fill_res_entry *fe = &fill_entries[res_type];
1104         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1105         struct rdma_restrack_entry *res;
1106         struct rdma_restrack_root *rt;
1107         int err, ret = 0, idx = 0;
1108         struct nlattr *table_attr;
1109         struct nlattr *entry_attr;
1110         struct ib_device *device;
1111         int start = cb->args[0];
1112         bool has_cap_net_admin;
1113         struct nlmsghdr *nlh;
1114         unsigned long id;
1115         u32 index, port = 0;
1116         bool filled = false;
1117
1118         err = nlmsg_parse_deprecated(cb->nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1119                                      nldev_policy, NULL);
1120         /*
1121          * Right now, we are expecting the device index to get res information,
1122          * but it is possible to extend this code to return all devices in
1123          * one shot by checking the existence of RDMA_NLDEV_ATTR_DEV_INDEX.
1124          * if it doesn't exist, we will iterate over all devices.
1125          *
1126          * But it is not needed for now.
1127          */
1128         if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1129                 return -EINVAL;
1130
1131         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1132         device = ib_device_get_by_index(sock_net(skb->sk), index);
1133         if (!device)
1134                 return -EINVAL;
1135
1136         /*
1137          * If no PORT_INDEX is supplied, we will return all QPs from that device
1138          */
1139         if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1140                 port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1141                 if (!rdma_is_port_valid(device, port)) {
1142                         ret = -EINVAL;
1143                         goto err_index;
1144                 }
1145         }
1146
1147         nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1148                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, fe->nldev_cmd),
1149                         0, NLM_F_MULTI);
1150
1151         if (fill_nldev_handle(skb, device)) {
1152                 ret = -EMSGSIZE;
1153                 goto err;
1154         }
1155
1156         table_attr = nla_nest_start_noflag(skb, fe->nldev_attr);
1157         if (!table_attr) {
1158                 ret = -EMSGSIZE;
1159                 goto err;
1160         }
1161
1162         has_cap_net_admin = netlink_capable(cb->skb, CAP_NET_ADMIN);
1163
1164         rt = &device->res[res_type];
1165         xa_lock(&rt->xa);
1166         /*
1167          * FIXME: if the skip ahead is something common this loop should
1168          * use xas_for_each & xas_pause to optimize, we can have a lot of
1169          * objects.
1170          */
1171         xa_for_each(&rt->xa, id, res) {
1172                 if (!is_visible_in_pid_ns(res))
1173                         continue;
1174
1175                 if (idx < start || !rdma_restrack_get(res))
1176                         goto next;
1177
1178                 xa_unlock(&rt->xa);
1179
1180                 filled = true;
1181
1182                 entry_attr = nla_nest_start_noflag(skb, fe->entry);
1183                 if (!entry_attr) {
1184                         ret = -EMSGSIZE;
1185                         rdma_restrack_put(res);
1186                         goto msg_full;
1187                 }
1188
1189                 ret = fe->fill_res_func(skb, has_cap_net_admin, res, port);
1190                 rdma_restrack_put(res);
1191
1192                 if (ret) {
1193                         nla_nest_cancel(skb, entry_attr);
1194                         if (ret == -EMSGSIZE)
1195                                 goto msg_full;
1196                         if (ret == -EAGAIN)
1197                                 goto again;
1198                         goto res_err;
1199                 }
1200                 nla_nest_end(skb, entry_attr);
1201 again:          xa_lock(&rt->xa);
1202 next:           idx++;
1203         }
1204         xa_unlock(&rt->xa);
1205
1206 msg_full:
1207         nla_nest_end(skb, table_attr);
1208         nlmsg_end(skb, nlh);
1209         cb->args[0] = idx;
1210
1211         /*
1212          * No more entries to fill, cancel the message and
1213          * return 0 to mark end of dumpit.
1214          */
1215         if (!filled)
1216                 goto err;
1217
1218         ib_device_put(device);
1219         return skb->len;
1220
1221 res_err:
1222         nla_nest_cancel(skb, table_attr);
1223
1224 err:
1225         nlmsg_cancel(skb, nlh);
1226
1227 err_index:
1228         ib_device_put(device);
1229         return ret;
1230 }
1231
1232 #define RES_GET_FUNCS(name, type)                                              \
1233         static int nldev_res_get_##name##_dumpit(struct sk_buff *skb,          \
1234                                                  struct netlink_callback *cb)  \
1235         {                                                                      \
1236                 return res_get_common_dumpit(skb, cb, type);                   \
1237         }                                                                      \
1238         static int nldev_res_get_##name##_doit(struct sk_buff *skb,            \
1239                                                struct nlmsghdr *nlh,           \
1240                                                struct netlink_ext_ack *extack) \
1241         {                                                                      \
1242                 return res_get_common_doit(skb, nlh, extack, type);            \
1243         }
1244
1245 RES_GET_FUNCS(qp, RDMA_RESTRACK_QP);
1246 RES_GET_FUNCS(cm_id, RDMA_RESTRACK_CM_ID);
1247 RES_GET_FUNCS(cq, RDMA_RESTRACK_CQ);
1248 RES_GET_FUNCS(pd, RDMA_RESTRACK_PD);
1249 RES_GET_FUNCS(mr, RDMA_RESTRACK_MR);
1250
1251 static LIST_HEAD(link_ops);
1252 static DECLARE_RWSEM(link_ops_rwsem);
1253
1254 static const struct rdma_link_ops *link_ops_get(const char *type)
1255 {
1256         const struct rdma_link_ops *ops;
1257
1258         list_for_each_entry(ops, &link_ops, list) {
1259                 if (!strcmp(ops->type, type))
1260                         goto out;
1261         }
1262         ops = NULL;
1263 out:
1264         return ops;
1265 }
1266
1267 void rdma_link_register(struct rdma_link_ops *ops)
1268 {
1269         down_write(&link_ops_rwsem);
1270         if (WARN_ON_ONCE(link_ops_get(ops->type)))
1271                 goto out;
1272         list_add(&ops->list, &link_ops);
1273 out:
1274         up_write(&link_ops_rwsem);
1275 }
1276 EXPORT_SYMBOL(rdma_link_register);
1277
1278 void rdma_link_unregister(struct rdma_link_ops *ops)
1279 {
1280         down_write(&link_ops_rwsem);
1281         list_del(&ops->list);
1282         up_write(&link_ops_rwsem);
1283 }
1284 EXPORT_SYMBOL(rdma_link_unregister);
1285
1286 static int nldev_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
1287                           struct netlink_ext_ack *extack)
1288 {
1289         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1290         char ibdev_name[IB_DEVICE_NAME_MAX];
1291         const struct rdma_link_ops *ops;
1292         char ndev_name[IFNAMSIZ];
1293         struct net_device *ndev;
1294         char type[IFNAMSIZ];
1295         int err;
1296
1297         err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1298                                      nldev_policy, extack);
1299         if (err || !tb[RDMA_NLDEV_ATTR_DEV_NAME] ||
1300             !tb[RDMA_NLDEV_ATTR_LINK_TYPE] || !tb[RDMA_NLDEV_ATTR_NDEV_NAME])
1301                 return -EINVAL;
1302
1303         nla_strlcpy(ibdev_name, tb[RDMA_NLDEV_ATTR_DEV_NAME],
1304                     sizeof(ibdev_name));
1305         if (strchr(ibdev_name, '%'))
1306                 return -EINVAL;
1307
1308         nla_strlcpy(type, tb[RDMA_NLDEV_ATTR_LINK_TYPE], sizeof(type));
1309         nla_strlcpy(ndev_name, tb[RDMA_NLDEV_ATTR_NDEV_NAME],
1310                     sizeof(ndev_name));
1311
1312         ndev = dev_get_by_name(&init_net, ndev_name);
1313         if (!ndev)
1314                 return -ENODEV;
1315
1316         down_read(&link_ops_rwsem);
1317         ops = link_ops_get(type);
1318 #ifdef CONFIG_MODULES
1319         if (!ops) {
1320                 up_read(&link_ops_rwsem);
1321                 request_module("rdma-link-%s", type);
1322                 down_read(&link_ops_rwsem);
1323                 ops = link_ops_get(type);
1324         }
1325 #endif
1326         err = ops ? ops->newlink(ibdev_name, ndev) : -EINVAL;
1327         up_read(&link_ops_rwsem);
1328         dev_put(ndev);
1329
1330         return err;
1331 }
1332
1333 static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
1334                           struct netlink_ext_ack *extack)
1335 {
1336         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1337         struct ib_device *device;
1338         u32 index;
1339         int err;
1340
1341         err = nlmsg_parse_deprecated(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1342                                      nldev_policy, extack);
1343         if (err || !tb[RDMA_NLDEV_ATTR_DEV_INDEX])
1344                 return -EINVAL;
1345
1346         index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1347         device = ib_device_get_by_index(sock_net(skb->sk), index);
1348         if (!device)
1349                 return -EINVAL;
1350
1351         if (!(device->attrs.device_cap_flags & IB_DEVICE_ALLOW_USER_UNREG)) {
1352                 ib_device_put(device);
1353                 return -EINVAL;
1354         }
1355
1356         ib_unregister_device_and_put(device);
1357         return 0;
1358 }
1359
1360 static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
1361                              struct netlink_ext_ack *extack)
1362 {
1363         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1364         char client_name[RDMA_NLDEV_ATTR_CHARDEV_TYPE_SIZE];
1365         struct ib_client_nl_info data = {};
1366         struct ib_device *ibdev = NULL;
1367         struct sk_buff *msg;
1368         u32 index;
1369         int err;
1370
1371         err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
1372                           extack);
1373         if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
1374                 return -EINVAL;
1375
1376         nla_strlcpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1377                     sizeof(client_name));
1378
1379         if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
1380                 index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1381                 ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
1382                 if (!ibdev)
1383                         return -EINVAL;
1384
1385                 if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1386                         data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1387                         if (!rdma_is_port_valid(ibdev, data.port)) {
1388                                 err = -EINVAL;
1389                                 goto out_put;
1390                         }
1391                 } else {
1392                         data.port = -1;
1393                 }
1394         } else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1395                 return -EINVAL;
1396         }
1397
1398         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1399         if (!msg) {
1400                 err = -ENOMEM;
1401                 goto out_put;
1402         }
1403         nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1404                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1405                                          RDMA_NLDEV_CMD_GET_CHARDEV),
1406                         0, 0);
1407
1408         data.nl_msg = msg;
1409         err = ib_get_client_nl_info(ibdev, client_name, &data);
1410         if (err)
1411                 goto out_nlmsg;
1412
1413         err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
1414                                 huge_encode_dev(data.cdev->devt),
1415                                 RDMA_NLDEV_ATTR_PAD);
1416         if (err)
1417                 goto out_data;
1418         err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
1419                                 RDMA_NLDEV_ATTR_PAD);
1420         if (err)
1421                 goto out_data;
1422         if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
1423                            dev_name(data.cdev))) {
1424                 err = -EMSGSIZE;
1425                 goto out_data;
1426         }
1427
1428         nlmsg_end(msg, nlh);
1429         put_device(data.cdev);
1430         if (ibdev)
1431                 ib_device_put(ibdev);
1432         return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
1433
1434 out_data:
1435         put_device(data.cdev);
1436 out_nlmsg:
1437         nlmsg_free(msg);
1438 out_put:
1439         if (ibdev)
1440                 ib_device_put(ibdev);
1441         return err;
1442 }
1443
1444 static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1445                               struct netlink_ext_ack *extack)
1446 {
1447         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1448         struct sk_buff *msg;
1449         int err;
1450
1451         err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1452                           nldev_policy, extack);
1453         if (err)
1454                 return err;
1455
1456         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1457         if (!msg)
1458                 return -ENOMEM;
1459
1460         nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1461                         RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1462                                          RDMA_NLDEV_CMD_SYS_GET),
1463                         0, 0);
1464
1465         err = nla_put_u8(msg, RDMA_NLDEV_SYS_ATTR_NETNS_MODE,
1466                          (u8)ib_devices_shared_netns);
1467         if (err) {
1468                 nlmsg_free(msg);
1469                 return err;
1470         }
1471         nlmsg_end(msg, nlh);
1472         return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
1473 }
1474
1475 static int nldev_set_sys_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1476                                   struct netlink_ext_ack *extack)
1477 {
1478         struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1479         u8 enable;
1480         int err;
1481
1482         err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1,
1483                           nldev_policy, extack);
1484         if (err || !tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE])
1485                 return -EINVAL;
1486
1487         enable = nla_get_u8(tb[RDMA_NLDEV_SYS_ATTR_NETNS_MODE]);
1488         /* Only 0 and 1 are supported */
1489         if (enable > 1)
1490                 return -EINVAL;
1491
1492         err = rdma_compatdev_set(enable);
1493         return err;
1494 }
1495
1496 static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
1497         [RDMA_NLDEV_CMD_GET] = {
1498                 .doit = nldev_get_doit,
1499                 .dump = nldev_get_dumpit,
1500         },
1501         [RDMA_NLDEV_CMD_GET_CHARDEV] = {
1502                 .doit = nldev_get_chardev,
1503         },
1504         [RDMA_NLDEV_CMD_SET] = {
1505                 .doit = nldev_set_doit,
1506                 .flags = RDMA_NL_ADMIN_PERM,
1507         },
1508         [RDMA_NLDEV_CMD_NEWLINK] = {
1509                 .doit = nldev_newlink,
1510                 .flags = RDMA_NL_ADMIN_PERM,
1511         },
1512         [RDMA_NLDEV_CMD_DELLINK] = {
1513                 .doit = nldev_dellink,
1514                 .flags = RDMA_NL_ADMIN_PERM,
1515         },
1516         [RDMA_NLDEV_CMD_PORT_GET] = {
1517                 .doit = nldev_port_get_doit,
1518                 .dump = nldev_port_get_dumpit,
1519         },
1520         [RDMA_NLDEV_CMD_RES_GET] = {
1521                 .doit = nldev_res_get_doit,
1522                 .dump = nldev_res_get_dumpit,
1523         },
1524         [RDMA_NLDEV_CMD_RES_QP_GET] = {
1525                 .doit = nldev_res_get_qp_doit,
1526                 .dump = nldev_res_get_qp_dumpit,
1527         },
1528         [RDMA_NLDEV_CMD_RES_CM_ID_GET] = {
1529                 .doit = nldev_res_get_cm_id_doit,
1530                 .dump = nldev_res_get_cm_id_dumpit,
1531         },
1532         [RDMA_NLDEV_CMD_RES_CQ_GET] = {
1533                 .doit = nldev_res_get_cq_doit,
1534                 .dump = nldev_res_get_cq_dumpit,
1535         },
1536         [RDMA_NLDEV_CMD_RES_MR_GET] = {
1537                 .doit = nldev_res_get_mr_doit,
1538                 .dump = nldev_res_get_mr_dumpit,
1539         },
1540         [RDMA_NLDEV_CMD_RES_PD_GET] = {
1541                 .doit = nldev_res_get_pd_doit,
1542                 .dump = nldev_res_get_pd_dumpit,
1543         },
1544         [RDMA_NLDEV_CMD_SYS_GET] = {
1545                 .doit = nldev_sys_get_doit,
1546         },
1547         [RDMA_NLDEV_CMD_SYS_SET] = {
1548                 .doit = nldev_set_sys_set_doit,
1549                 .flags = RDMA_NL_ADMIN_PERM,
1550         },
1551 };
1552
1553 void __init nldev_init(void)
1554 {
1555         rdma_nl_register(RDMA_NL_NLDEV, nldev_cb_table);
1556 }
1557
1558 void __exit nldev_exit(void)
1559 {
1560         rdma_nl_unregister(RDMA_NL_NLDEV);
1561 }
1562
1563 MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_NLDEV, 5);
This page took 0.125929 seconds and 4 git commands to generate.