]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/infiniband/core/netlink.c
davinci_cpdma: make cpdma_chan_split_pool static
[linux.git] / drivers / infiniband / core / netlink.c
1 /*
2  * Copyright (c) 2017 Mellanox Technologies Inc.  All rights reserved.
3  * Copyright (c) 2010 Voltaire Inc.  All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
35
36 #include <linux/export.h>
37 #include <net/netlink.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
40 #include <net/sock.h>
41 #include <rdma/rdma_netlink.h>
42 #include <linux/module.h>
43 #include "core_priv.h"
44
45 static DEFINE_MUTEX(rdma_nl_mutex);
46 static struct {
47         const struct rdma_nl_cbs   *cb_table;
48 } rdma_nl_types[RDMA_NL_NUM_CLIENTS];
49
50 bool rdma_nl_chk_listeners(unsigned int group)
51 {
52         struct rdma_dev_net *rnet = rdma_net_to_dev_net(&init_net);
53
54         return netlink_has_listeners(rnet->nl_sock, group);
55 }
56 EXPORT_SYMBOL(rdma_nl_chk_listeners);
57
58 static bool is_nl_msg_valid(unsigned int type, unsigned int op)
59 {
60         static const unsigned int max_num_ops[RDMA_NL_NUM_CLIENTS] = {
61                 [RDMA_NL_IWCM] = RDMA_NL_IWPM_NUM_OPS,
62                 [RDMA_NL_LS] = RDMA_NL_LS_NUM_OPS,
63                 [RDMA_NL_NLDEV] = RDMA_NLDEV_NUM_OPS,
64         };
65
66         /*
67          * This BUILD_BUG_ON is intended to catch addition of new
68          * RDMA netlink protocol without updating the array above.
69          */
70         BUILD_BUG_ON(RDMA_NL_NUM_CLIENTS != 6);
71
72         if (type >= RDMA_NL_NUM_CLIENTS)
73                 return false;
74
75         return (op < max_num_ops[type]) ? true : false;
76 }
77
78 static bool
79 is_nl_valid(const struct sk_buff *skb, unsigned int type, unsigned int op)
80 {
81         const struct rdma_nl_cbs *cb_table;
82
83         if (!is_nl_msg_valid(type, op))
84                 return false;
85
86         /*
87          * Currently only NLDEV client is supporting netlink commands in
88          * non init_net net namespace.
89          */
90         if (sock_net(skb->sk) != &init_net && type != RDMA_NL_NLDEV)
91                 return false;
92
93         if (!rdma_nl_types[type].cb_table) {
94                 mutex_unlock(&rdma_nl_mutex);
95                 request_module("rdma-netlink-subsys-%d", type);
96                 mutex_lock(&rdma_nl_mutex);
97         }
98
99         cb_table = rdma_nl_types[type].cb_table;
100
101         if (!cb_table || (!cb_table[op].dump && !cb_table[op].doit))
102                 return false;
103         return true;
104 }
105
106 void rdma_nl_register(unsigned int index,
107                       const struct rdma_nl_cbs cb_table[])
108 {
109         mutex_lock(&rdma_nl_mutex);
110         if (!is_nl_msg_valid(index, 0)) {
111                 /*
112                  * All clients are not interesting in success/failure of
113                  * this call. They want to see the print to error log and
114                  * continue their initialization. Print warning for them,
115                  * because it is programmer's error to be here.
116                  */
117                 mutex_unlock(&rdma_nl_mutex);
118                 WARN(true,
119                      "The not-valid %u index was supplied to RDMA netlink\n",
120                      index);
121                 return;
122         }
123
124         if (rdma_nl_types[index].cb_table) {
125                 mutex_unlock(&rdma_nl_mutex);
126                 WARN(true,
127                      "The %u index is already registered in RDMA netlink\n",
128                      index);
129                 return;
130         }
131
132         rdma_nl_types[index].cb_table = cb_table;
133         mutex_unlock(&rdma_nl_mutex);
134 }
135 EXPORT_SYMBOL(rdma_nl_register);
136
137 void rdma_nl_unregister(unsigned int index)
138 {
139         mutex_lock(&rdma_nl_mutex);
140         rdma_nl_types[index].cb_table = NULL;
141         mutex_unlock(&rdma_nl_mutex);
142 }
143 EXPORT_SYMBOL(rdma_nl_unregister);
144
145 void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
146                    int len, int client, int op, int flags)
147 {
148         *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op), len, flags);
149         if (!*nlh)
150                 return NULL;
151         return nlmsg_data(*nlh);
152 }
153 EXPORT_SYMBOL(ibnl_put_msg);
154
155 int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
156                   int len, void *data, int type)
157 {
158         if (nla_put(skb, type, len, data)) {
159                 nlmsg_cancel(skb, nlh);
160                 return -EMSGSIZE;
161         }
162         return 0;
163 }
164 EXPORT_SYMBOL(ibnl_put_attr);
165
166 static int rdma_nl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
167                            struct netlink_ext_ack *extack)
168 {
169         int type = nlh->nlmsg_type;
170         unsigned int index = RDMA_NL_GET_CLIENT(type);
171         unsigned int op = RDMA_NL_GET_OP(type);
172         const struct rdma_nl_cbs *cb_table;
173
174         if (!is_nl_valid(skb, index, op))
175                 return -EINVAL;
176
177         cb_table = rdma_nl_types[index].cb_table;
178
179         if ((cb_table[op].flags & RDMA_NL_ADMIN_PERM) &&
180             !netlink_capable(skb, CAP_NET_ADMIN))
181                 return -EPERM;
182
183         /*
184          * LS responses overload the 0x100 (NLM_F_ROOT) flag.  Don't
185          * mistakenly call the .dump() function.
186          */
187         if (index == RDMA_NL_LS) {
188                 if (cb_table[op].doit)
189                         return cb_table[op].doit(skb, nlh, extack);
190                 return -EINVAL;
191         }
192         /* FIXME: Convert IWCM to properly handle doit callbacks */
193         if ((nlh->nlmsg_flags & NLM_F_DUMP) || index == RDMA_NL_IWCM) {
194                 struct netlink_dump_control c = {
195                         .dump = cb_table[op].dump,
196                 };
197                 if (c.dump)
198                         return netlink_dump_start(skb->sk, skb, nlh, &c);
199                 return -EINVAL;
200         }
201
202         if (cb_table[op].doit)
203                 return cb_table[op].doit(skb, nlh, extack);
204
205         return 0;
206 }
207
208 /*
209  * This function is similar to netlink_rcv_skb with one exception:
210  * It calls to the callback for the netlink messages without NLM_F_REQUEST
211  * flag. These messages are intended for RDMA_NL_LS consumer, so it is allowed
212  * for that consumer only.
213  */
214 static int rdma_nl_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
215                                                    struct nlmsghdr *,
216                                                    struct netlink_ext_ack *))
217 {
218         struct netlink_ext_ack extack = {};
219         struct nlmsghdr *nlh;
220         int err;
221
222         while (skb->len >= nlmsg_total_size(0)) {
223                 int msglen;
224
225                 nlh = nlmsg_hdr(skb);
226                 err = 0;
227
228                 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
229                         return 0;
230
231                 /*
232                  * Generally speaking, the only requests are handled
233                  * by the kernel, but RDMA_NL_LS is different, because it
234                  * runs backward netlink scheme. Kernel initiates messages
235                  * and waits for reply with data to keep pathrecord cache
236                  * in sync.
237                  */
238                 if (!(nlh->nlmsg_flags & NLM_F_REQUEST) &&
239                     (RDMA_NL_GET_CLIENT(nlh->nlmsg_type) != RDMA_NL_LS))
240                         goto ack;
241
242                 /* Skip control messages */
243                 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
244                         goto ack;
245
246                 err = cb(skb, nlh, &extack);
247                 if (err == -EINTR)
248                         goto skip;
249
250 ack:
251                 if (nlh->nlmsg_flags & NLM_F_ACK || err)
252                         netlink_ack(skb, nlh, err, &extack);
253
254 skip:
255                 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
256                 if (msglen > skb->len)
257                         msglen = skb->len;
258                 skb_pull(skb, msglen);
259         }
260
261         return 0;
262 }
263
264 static void rdma_nl_rcv(struct sk_buff *skb)
265 {
266         mutex_lock(&rdma_nl_mutex);
267         rdma_nl_rcv_skb(skb, &rdma_nl_rcv_msg);
268         mutex_unlock(&rdma_nl_mutex);
269 }
270
271 int rdma_nl_unicast(struct net *net, struct sk_buff *skb, u32 pid)
272 {
273         struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
274         int err;
275
276         err = netlink_unicast(rnet->nl_sock, skb, pid, MSG_DONTWAIT);
277         return (err < 0) ? err : 0;
278 }
279 EXPORT_SYMBOL(rdma_nl_unicast);
280
281 int rdma_nl_unicast_wait(struct net *net, struct sk_buff *skb, __u32 pid)
282 {
283         struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
284         int err;
285
286         err = netlink_unicast(rnet->nl_sock, skb, pid, 0);
287         return (err < 0) ? err : 0;
288 }
289 EXPORT_SYMBOL(rdma_nl_unicast_wait);
290
291 int rdma_nl_multicast(struct net *net, struct sk_buff *skb,
292                       unsigned int group, gfp_t flags)
293 {
294         struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
295
296         return nlmsg_multicast(rnet->nl_sock, skb, 0, group, flags);
297 }
298 EXPORT_SYMBOL(rdma_nl_multicast);
299
300 void rdma_nl_exit(void)
301 {
302         int idx;
303
304         for (idx = 0; idx < RDMA_NL_NUM_CLIENTS; idx++)
305                 WARN(rdma_nl_types[idx].cb_table,
306                      "Netlink client %d wasn't released prior to unloading %s\n",
307                      idx, KBUILD_MODNAME);
308 }
309
310 int rdma_nl_net_init(struct rdma_dev_net *rnet)
311 {
312         struct net *net = read_pnet(&rnet->net);
313         struct netlink_kernel_cfg cfg = {
314                 .input  = rdma_nl_rcv,
315         };
316         struct sock *nls;
317
318         nls = netlink_kernel_create(net, NETLINK_RDMA, &cfg);
319         if (!nls)
320                 return -ENOMEM;
321
322         nls->sk_sndtimeo = 10 * HZ;
323         rnet->nl_sock = nls;
324         return 0;
325 }
326
327 void rdma_nl_net_exit(struct rdma_dev_net *rnet)
328 {
329         netlink_kernel_release(rnet->nl_sock);
330 }
331
332 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_RDMA);