Commit | Line | Data |
---|---|---|
b2cbae2c | 1 | /* |
8bc67414 | 2 | * Copyright (c) 2017 Mellanox Technologies Inc. All rights reserved. |
b2cbae2c RD |
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 | ||
b108d976 | 36 | #include <linux/export.h> |
b2cbae2c RD |
37 | #include <net/netlink.h> |
38 | #include <net/net_namespace.h> | |
39 | #include <net/sock.h> | |
40 | #include <rdma/rdma_netlink.h> | |
1eb5be0e | 41 | #include <linux/module.h> |
233c1955 | 42 | #include "core_priv.h" |
b2cbae2c | 43 | |
c9901724 | 44 | #include "core_priv.h" |
b2cbae2c | 45 | |
c9901724 | 46 | static DEFINE_MUTEX(rdma_nl_mutex); |
b2cbae2c | 47 | static struct sock *nls; |
c9901724 | 48 | static struct { |
3250b4db | 49 | const struct rdma_nl_cbs *cb_table; |
c9901724 | 50 | } rdma_nl_types[RDMA_NL_NUM_CLIENTS]; |
b2cbae2c | 51 | |
ff61c425 | 52 | int rdma_nl_chk_listeners(unsigned int group) |
bc10ed7d | 53 | { |
ff61c425 | 54 | return (netlink_has_listeners(nls, group)) ? 0 : -1; |
bc10ed7d | 55 | } |
ff61c425 | 56 | EXPORT_SYMBOL(rdma_nl_chk_listeners); |
bc10ed7d | 57 | |
c9901724 | 58 | static bool is_nl_msg_valid(unsigned int type, unsigned int op) |
b2cbae2c | 59 | { |
8b2c7e7a | 60 | static const unsigned int max_num_ops[RDMA_NL_NUM_CLIENTS] = { |
015a9e66 LT |
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 | }; | |
b2cbae2c | 66 | |
c9901724 LR |
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); | |
b2cbae2c | 72 | |
8b2c7e7a | 73 | if (type >= RDMA_NL_NUM_CLIENTS) |
c9901724 | 74 | return false; |
b2cbae2c | 75 | |
8b2c7e7a | 76 | return (op < max_num_ops[type]) ? true : false; |
c9901724 | 77 | } |
b2cbae2c | 78 | |
c9901724 LR |
79 | static bool is_nl_valid(unsigned int type, unsigned int op) |
80 | { | |
1830ba21 LR |
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; | |
e3bf14bd JG |
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 | ||
1830ba21 | 96 | if (!cb_table || (!cb_table[op].dump && !cb_table[op].doit)) |
c9901724 LR |
97 | return false; |
98 | return true; | |
99 | } | |
b2cbae2c | 100 | |
c9901724 | 101 | void rdma_nl_register(unsigned int index, |
3250b4db | 102 | const struct rdma_nl_cbs cb_table[]) |
c9901724 LR |
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 | } | |
b2cbae2c | 118 | |
c9901724 LR |
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 | } | |
b2cbae2c | 126 | |
c9901724 LR |
127 | rdma_nl_types[index].cb_table = cb_table; |
128 | mutex_unlock(&rdma_nl_mutex); | |
b2cbae2c | 129 | } |
c9901724 | 130 | EXPORT_SYMBOL(rdma_nl_register); |
b2cbae2c | 131 | |
c9901724 | 132 | void rdma_nl_unregister(unsigned int index) |
b2cbae2c | 133 | { |
c9901724 LR |
134 | mutex_lock(&rdma_nl_mutex); |
135 | rdma_nl_types[index].cb_table = NULL; | |
136 | mutex_unlock(&rdma_nl_mutex); | |
b2cbae2c | 137 | } |
c9901724 | 138 | EXPORT_SYMBOL(rdma_nl_unregister); |
b2cbae2c RD |
139 | |
140 | void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq, | |
30dc5e63 | 141 | int len, int client, int op, int flags) |
b2cbae2c | 142 | { |
1a1c116f | 143 | *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op), len, flags); |
e0527334 | 144 | if (!*nlh) |
1a1c116f | 145 | return NULL; |
e0527334 | 146 | return nlmsg_data(*nlh); |
b2cbae2c RD |
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 | { | |
1a1c116f LR |
153 | if (nla_put(skb, type, len, data)) { |
154 | nlmsg_cancel(skb, nlh); | |
155 | return -EMSGSIZE; | |
156 | } | |
b2cbae2c | 157 | return 0; |
b2cbae2c RD |
158 | } |
159 | EXPORT_SYMBOL(ibnl_put_attr); | |
160 | ||
3c3e75d5 LR |
161 | static int rdma_nl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
162 | struct netlink_ext_ack *extack) | |
b2cbae2c | 163 | { |
b2cbae2c | 164 | int type = nlh->nlmsg_type; |
c9901724 | 165 | unsigned int index = RDMA_NL_GET_CLIENT(type); |
1ae5ccc7 | 166 | unsigned int op = RDMA_NL_GET_OP(type); |
c729943a | 167 | const struct rdma_nl_cbs *cb_table; |
b2cbae2c | 168 | |
c9901724 LR |
169 | if (!is_nl_valid(index, op)) |
170 | return -EINVAL; | |
171 | ||
647c75ac | 172 | cb_table = rdma_nl_types[index].cb_table; |
c729943a LR |
173 | |
174 | if ((cb_table[op].flags & RDMA_NL_ADMIN_PERM) && | |
e3a2b93d LR |
175 | !netlink_capable(skb, CAP_NET_ADMIN)) |
176 | return -EPERM; | |
177 | ||
b4d91aeb MR |
178 | /* |
179 | * LS responses overload the 0x100 (NLM_F_ROOT) flag. Don't | |
180 | * mistakenly call the .dump() function. | |
181 | */ | |
182 | if (index == RDMA_NL_LS) { | |
183 | if (cb_table[op].doit) | |
184 | return cb_table[op].doit(skb, nlh, extack); | |
185 | return -EINVAL; | |
186 | } | |
647c75ac LR |
187 | /* FIXME: Convert IWCM to properly handle doit callbacks */ |
188 | if ((nlh->nlmsg_flags & NLM_F_DUMP) || index == RDMA_NL_RDMA_CM || | |
189 | index == RDMA_NL_IWCM) { | |
190 | struct netlink_dump_control c = { | |
191 | .dump = cb_table[op].dump, | |
192 | }; | |
b4d91aeb MR |
193 | if (c.dump) |
194 | return netlink_dump_start(nls, skb, nlh, &c); | |
195 | return -EINVAL; | |
b2cbae2c | 196 | } |
647c75ac | 197 | |
c729943a | 198 | if (cb_table[op].doit) |
647c75ac | 199 | return cb_table[op].doit(skb, nlh, extack); |
b2cbae2c | 200 | |
647c75ac | 201 | return 0; |
b2cbae2c RD |
202 | } |
203 | ||
3c3e75d5 LR |
204 | /* |
205 | * This function is similar to netlink_rcv_skb with one exception: | |
206 | * It calls to the callback for the netlink messages without NLM_F_REQUEST | |
207 | * flag. These messages are intended for RDMA_NL_LS consumer, so it is allowed | |
208 | * for that consumer only. | |
209 | */ | |
210 | static int rdma_nl_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, | |
211 | struct nlmsghdr *, | |
212 | struct netlink_ext_ack *)) | |
bc10ed7d | 213 | { |
3c3e75d5 | 214 | struct netlink_ext_ack extack = {}; |
bc10ed7d | 215 | struct nlmsghdr *nlh; |
3c3e75d5 | 216 | int err; |
bc10ed7d | 217 | |
bc10ed7d | 218 | while (skb->len >= nlmsg_total_size(0)) { |
3c3e75d5 LR |
219 | int msglen; |
220 | ||
bc10ed7d | 221 | nlh = nlmsg_hdr(skb); |
3c3e75d5 | 222 | err = 0; |
bc10ed7d KW |
223 | |
224 | if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) | |
3c3e75d5 | 225 | return 0; |
bc10ed7d | 226 | |
3c3e75d5 LR |
227 | /* |
228 | * Generally speaking, the only requests are handled | |
229 | * by the kernel, but RDMA_NL_LS is different, because it | |
230 | * runs backward netlink scheme. Kernel initiates messages | |
231 | * and waits for reply with data to keep pathrecord cache | |
232 | * in sync. | |
233 | */ | |
234 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST) && | |
235 | (RDMA_NL_GET_CLIENT(nlh->nlmsg_type) != RDMA_NL_LS)) | |
236 | goto ack; | |
237 | ||
238 | /* Skip control messages */ | |
239 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) | |
240 | goto ack; | |
bc10ed7d | 241 | |
3c3e75d5 LR |
242 | err = cb(skb, nlh, &extack); |
243 | if (err == -EINTR) | |
244 | goto skip; | |
bc10ed7d | 245 | |
3c3e75d5 LR |
246 | ack: |
247 | if (nlh->nlmsg_flags & NLM_F_ACK || err) | |
248 | netlink_ack(skb, nlh, err, &extack); | |
249 | ||
250 | skip: | |
bc10ed7d KW |
251 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
252 | if (msglen > skb->len) | |
253 | msglen = skb->len; | |
254 | skb_pull(skb, msglen); | |
255 | } | |
3c3e75d5 LR |
256 | |
257 | return 0; | |
bc10ed7d KW |
258 | } |
259 | ||
3c3e75d5 | 260 | static void rdma_nl_rcv(struct sk_buff *skb) |
b2cbae2c | 261 | { |
c9901724 | 262 | mutex_lock(&rdma_nl_mutex); |
3c3e75d5 | 263 | rdma_nl_rcv_skb(skb, &rdma_nl_rcv_msg); |
c9901724 | 264 | mutex_unlock(&rdma_nl_mutex); |
b2cbae2c RD |
265 | } |
266 | ||
f00e6463 | 267 | int rdma_nl_unicast(struct sk_buff *skb, u32 pid) |
30dc5e63 | 268 | { |
cea05ead MI |
269 | int err; |
270 | ||
9047811b | 271 | err = netlink_unicast(nls, skb, pid, MSG_DONTWAIT); |
cea05ead | 272 | return (err < 0) ? err : 0; |
30dc5e63 | 273 | } |
f00e6463 | 274 | EXPORT_SYMBOL(rdma_nl_unicast); |
30dc5e63 | 275 | |
f00e6463 | 276 | int rdma_nl_unicast_wait(struct sk_buff *skb, __u32 pid) |
9047811b IM |
277 | { |
278 | int err; | |
279 | ||
280 | err = netlink_unicast(nls, skb, pid, 0); | |
281 | return (err < 0) ? err : 0; | |
282 | } | |
f00e6463 | 283 | EXPORT_SYMBOL(rdma_nl_unicast_wait); |
9047811b | 284 | |
4d7f693a | 285 | int rdma_nl_multicast(struct sk_buff *skb, unsigned int group, gfp_t flags) |
30dc5e63 TN |
286 | { |
287 | return nlmsg_multicast(nls, skb, 0, group, flags); | |
288 | } | |
4d7f693a | 289 | EXPORT_SYMBOL(rdma_nl_multicast); |
30dc5e63 | 290 | |
c9901724 | 291 | int __init rdma_nl_init(void) |
b2cbae2c | 292 | { |
a31f2d17 | 293 | struct netlink_kernel_cfg cfg = { |
3c3e75d5 | 294 | .input = rdma_nl_rcv, |
a31f2d17 PNA |
295 | }; |
296 | ||
9f00d977 | 297 | nls = netlink_kernel_create(&init_net, NETLINK_RDMA, &cfg); |
c9901724 | 298 | if (!nls) |
b2cbae2c | 299 | return -ENOMEM; |
b2cbae2c | 300 | |
cea05ead | 301 | nls->sk_sndtimeo = 10 * HZ; |
b2cbae2c RD |
302 | return 0; |
303 | } | |
304 | ||
c9901724 | 305 | void rdma_nl_exit(void) |
b2cbae2c | 306 | { |
c9901724 | 307 | int idx; |
b2cbae2c | 308 | |
c9901724 LR |
309 | for (idx = 0; idx < RDMA_NL_NUM_CLIENTS; idx++) |
310 | rdma_nl_unregister(idx); | |
b2cbae2c RD |
311 | |
312 | netlink_kernel_release(nls); | |
313 | } | |
1eb5be0e JG |
314 | |
315 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_RDMA); |