Merge branch 'kvm-ppc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus...
[sfrench/cifs-2.6.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/sock.h>
40 #include <rdma/rdma_netlink.h>
41 #include <linux/module.h>
42 #include "core_priv.h"
43
44 #include "core_priv.h"
45
46 static DEFINE_MUTEX(rdma_nl_mutex);
47 static struct sock *nls;
48 static struct {
49         const struct rdma_nl_cbs   *cb_table;
50 } rdma_nl_types[RDMA_NL_NUM_CLIENTS];
51
52 int rdma_nl_chk_listeners(unsigned int group)
53 {
54         return (netlink_has_listeners(nls, group)) ? 0 : -1;
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_RDMA_CM] = RDMA_NL_RDMA_CM_NUM_OPS,
62                 [RDMA_NL_IWCM] = RDMA_NL_IWPM_NUM_OPS,
63                 [RDMA_NL_LS] = RDMA_NL_LS_NUM_OPS,
64                 [RDMA_NL_NLDEV] = RDMA_NLDEV_NUM_OPS,
65         };
66
67         /*
68          * This BUILD_BUG_ON is intended to catch addition of new
69          * RDMA netlink protocol without updating the array above.
70          */
71         BUILD_BUG_ON(RDMA_NL_NUM_CLIENTS != 6);
72
73         if (type >= RDMA_NL_NUM_CLIENTS)
74                 return false;
75
76         return (op < max_num_ops[type]) ? true : false;
77 }
78
79 static bool is_nl_valid(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         cb_table = rdma_nl_types[type].cb_table;
87 #ifdef CONFIG_MODULES
88         if (!cb_table) {
89                 mutex_unlock(&rdma_nl_mutex);
90                 request_module("rdma-netlink-subsys-%d", type);
91                 mutex_lock(&rdma_nl_mutex);
92                 cb_table = rdma_nl_types[type].cb_table;
93         }
94 #endif
95
96         if (!cb_table || (!cb_table[op].dump && !cb_table[op].doit))
97                 return false;
98         return true;
99 }
100
101 void rdma_nl_register(unsigned int index,
102                       const struct rdma_nl_cbs cb_table[])
103 {
104         mutex_lock(&rdma_nl_mutex);
105         if (!is_nl_msg_valid(index, 0)) {
106                 /*
107                  * All clients are not interesting in success/failure of
108                  * this call. They want to see the print to error log and
109                  * continue their initialization. Print warning for them,
110                  * because it is programmer's error to be here.
111                  */
112                 mutex_unlock(&rdma_nl_mutex);
113                 WARN(true,
114                      "The not-valid %u index was supplied to RDMA netlink\n",
115                      index);
116                 return;
117         }
118
119         if (rdma_nl_types[index].cb_table) {
120                 mutex_unlock(&rdma_nl_mutex);
121                 WARN(true,
122                      "The %u index is already registered in RDMA netlink\n",
123                      index);
124                 return;
125         }
126
127         rdma_nl_types[index].cb_table = cb_table;
128         mutex_unlock(&rdma_nl_mutex);
129 }
130 EXPORT_SYMBOL(rdma_nl_register);
131
132 void rdma_nl_unregister(unsigned int index)
133 {
134         mutex_lock(&rdma_nl_mutex);
135         rdma_nl_types[index].cb_table = NULL;
136         mutex_unlock(&rdma_nl_mutex);
137 }
138 EXPORT_SYMBOL(rdma_nl_unregister);
139
140 void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
141                    int len, int client, int op, int flags)
142 {
143         *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op), len, flags);
144         if (!*nlh)
145                 return NULL;
146         return nlmsg_data(*nlh);
147 }
148 EXPORT_SYMBOL(ibnl_put_msg);
149
150 int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
151                   int len, void *data, int type)
152 {
153         if (nla_put(skb, type, len, data)) {
154                 nlmsg_cancel(skb, nlh);
155                 return -EMSGSIZE;
156         }
157         return 0;
158 }
159 EXPORT_SYMBOL(ibnl_put_attr);
160
161 static int rdma_nl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
162                            struct netlink_ext_ack *extack)
163 {
164         int type = nlh->nlmsg_type;
165         unsigned int index = RDMA_NL_GET_CLIENT(type);
166         unsigned int op = RDMA_NL_GET_OP(type);
167         const struct rdma_nl_cbs *cb_table;
168
169         if (!is_nl_valid(index, op))
170                 return -EINVAL;
171
172         cb_table = rdma_nl_types[index].cb_table;
173
174         if ((cb_table[op].flags & RDMA_NL_ADMIN_PERM) &&
175             !netlink_capable(skb, CAP_NET_ADMIN))
176                 return -EPERM;
177
178         /* FIXME: Convert IWCM to properly handle doit callbacks */
179         if ((nlh->nlmsg_flags & NLM_F_DUMP) || index == RDMA_NL_RDMA_CM ||
180             index == RDMA_NL_IWCM) {
181                 struct netlink_dump_control c = {
182                         .dump = cb_table[op].dump,
183                 };
184                 return netlink_dump_start(nls, skb, nlh, &c);
185         }
186
187         if (cb_table[op].doit)
188                 return cb_table[op].doit(skb, nlh, extack);
189
190         return 0;
191 }
192
193 /*
194  * This function is similar to netlink_rcv_skb with one exception:
195  * It calls to the callback for the netlink messages without NLM_F_REQUEST
196  * flag. These messages are intended for RDMA_NL_LS consumer, so it is allowed
197  * for that consumer only.
198  */
199 static int rdma_nl_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
200                                                    struct nlmsghdr *,
201                                                    struct netlink_ext_ack *))
202 {
203         struct netlink_ext_ack extack = {};
204         struct nlmsghdr *nlh;
205         int err;
206
207         while (skb->len >= nlmsg_total_size(0)) {
208                 int msglen;
209
210                 nlh = nlmsg_hdr(skb);
211                 err = 0;
212
213                 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
214                         return 0;
215
216                 /*
217                  * Generally speaking, the only requests are handled
218                  * by the kernel, but RDMA_NL_LS is different, because it
219                  * runs backward netlink scheme. Kernel initiates messages
220                  * and waits for reply with data to keep pathrecord cache
221                  * in sync.
222                  */
223                 if (!(nlh->nlmsg_flags & NLM_F_REQUEST) &&
224                     (RDMA_NL_GET_CLIENT(nlh->nlmsg_type) != RDMA_NL_LS))
225                         goto ack;
226
227                 /* Skip control messages */
228                 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
229                         goto ack;
230
231                 err = cb(skb, nlh, &extack);
232                 if (err == -EINTR)
233                         goto skip;
234
235 ack:
236                 if (nlh->nlmsg_flags & NLM_F_ACK || err)
237                         netlink_ack(skb, nlh, err, &extack);
238
239 skip:
240                 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
241                 if (msglen > skb->len)
242                         msglen = skb->len;
243                 skb_pull(skb, msglen);
244         }
245
246         return 0;
247 }
248
249 static void rdma_nl_rcv(struct sk_buff *skb)
250 {
251         mutex_lock(&rdma_nl_mutex);
252         rdma_nl_rcv_skb(skb, &rdma_nl_rcv_msg);
253         mutex_unlock(&rdma_nl_mutex);
254 }
255
256 int rdma_nl_unicast(struct sk_buff *skb, u32 pid)
257 {
258         int err;
259
260         err = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
261         return (err < 0) ? err : 0;
262 }
263 EXPORT_SYMBOL(rdma_nl_unicast);
264
265 int rdma_nl_unicast_wait(struct sk_buff *skb, __u32 pid)
266 {
267         int err;
268
269         err = netlink_unicast(nls, skb, pid, 0);
270         return (err < 0) ? err : 0;
271 }
272 EXPORT_SYMBOL(rdma_nl_unicast_wait);
273
274 int rdma_nl_multicast(struct sk_buff *skb, unsigned int group, gfp_t flags)
275 {
276         return nlmsg_multicast(nls, skb, 0, group, flags);
277 }
278 EXPORT_SYMBOL(rdma_nl_multicast);
279
280 int __init rdma_nl_init(void)
281 {
282         struct netlink_kernel_cfg cfg = {
283                 .input  = rdma_nl_rcv,
284         };
285
286         nls = netlink_kernel_create(&init_net, NETLINK_RDMA, &cfg);
287         if (!nls)
288                 return -ENOMEM;
289
290         nls->sk_sndtimeo = 10 * HZ;
291         return 0;
292 }
293
294 void rdma_nl_exit(void)
295 {
296         int idx;
297
298         for (idx = 0; idx < RDMA_NL_NUM_CLIENTS; idx++)
299                 rdma_nl_unregister(idx);
300
301         netlink_kernel_release(nls);
302 }
303
304 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_RDMA);