Commit | Line | Data |
---|---|---|
b2cbae2c RD |
1 | /* |
2 | * Copyright (c) 2010 Voltaire Inc. All rights reserved. | |
3 | * | |
4 | * This software is available to you under a choice of one of two | |
5 | * licenses. You may choose to be licensed under the terms of the GNU | |
6 | * General Public License (GPL) Version 2, available from the file | |
7 | * COPYING in the main directory of this source tree, or the | |
8 | * OpenIB.org BSD license below: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or | |
11 | * without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistributions of source code must retain the above | |
15 | * copyright notice, this list of conditions and the following | |
16 | * disclaimer. | |
17 | * | |
18 | * - Redistributions in binary form must reproduce the above | |
19 | * copyright notice, this list of conditions and the following | |
20 | * disclaimer in the documentation and/or other materials | |
21 | * provided with the distribution. | |
22 | * | |
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
30 | * SOFTWARE. | |
31 | */ | |
32 | ||
33 | #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__ | |
34 | ||
b108d976 | 35 | #include <linux/export.h> |
b2cbae2c RD |
36 | #include <net/netlink.h> |
37 | #include <net/net_namespace.h> | |
38 | #include <net/sock.h> | |
39 | #include <rdma/rdma_netlink.h> | |
233c1955 | 40 | #include "core_priv.h" |
b2cbae2c | 41 | |
c9901724 | 42 | #include "core_priv.h" |
b2cbae2c | 43 | |
c9901724 | 44 | static DEFINE_MUTEX(rdma_nl_mutex); |
b2cbae2c | 45 | static struct sock *nls; |
c9901724 LR |
46 | static struct { |
47 | const struct ibnl_client_cbs *cb_table; | |
48 | } rdma_nl_types[RDMA_NL_NUM_CLIENTS]; | |
b2cbae2c | 49 | |
bc10ed7d KW |
50 | int ibnl_chk_listeners(unsigned int group) |
51 | { | |
52 | if (netlink_has_listeners(nls, group) == 0) | |
53 | return -1; | |
54 | return 0; | |
55 | } | |
bc10ed7d | 56 | |
c9901724 | 57 | static bool is_nl_msg_valid(unsigned int type, unsigned int op) |
b2cbae2c | 58 | { |
c9901724 LR |
59 | static const unsigned int max_num_ops[RDMA_NL_NUM_CLIENTS - 1] = { |
60 | RDMA_NL_RDMA_CM_NUM_OPS, | |
61 | RDMA_NL_IWPM_NUM_OPS, | |
62 | 0, | |
63 | RDMA_NL_LS_NUM_OPS, | |
64 | 0 }; | |
b2cbae2c | 65 | |
c9901724 LR |
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); | |
b2cbae2c | 71 | |
c9901724 LR |
72 | if (type > RDMA_NL_NUM_CLIENTS - 1) |
73 | return false; | |
b2cbae2c | 74 | |
c9901724 LR |
75 | return (op < max_num_ops[type - 1]) ? true : false; |
76 | } | |
b2cbae2c | 77 | |
c9901724 LR |
78 | static bool is_nl_valid(unsigned int type, unsigned int op) |
79 | { | |
80 | if (!is_nl_msg_valid(type, op) || | |
81 | !rdma_nl_types[type].cb_table || | |
82 | !rdma_nl_types[type].cb_table[op].dump) | |
83 | return false; | |
84 | return true; | |
85 | } | |
b2cbae2c | 86 | |
c9901724 LR |
87 | void rdma_nl_register(unsigned int index, |
88 | const struct ibnl_client_cbs cb_table[]) | |
89 | { | |
90 | mutex_lock(&rdma_nl_mutex); | |
91 | if (!is_nl_msg_valid(index, 0)) { | |
92 | /* | |
93 | * All clients are not interesting in success/failure of | |
94 | * this call. They want to see the print to error log and | |
95 | * continue their initialization. Print warning for them, | |
96 | * because it is programmer's error to be here. | |
97 | */ | |
98 | mutex_unlock(&rdma_nl_mutex); | |
99 | WARN(true, | |
100 | "The not-valid %u index was supplied to RDMA netlink\n", | |
101 | index); | |
102 | return; | |
103 | } | |
b2cbae2c | 104 | |
c9901724 LR |
105 | if (rdma_nl_types[index].cb_table) { |
106 | mutex_unlock(&rdma_nl_mutex); | |
107 | WARN(true, | |
108 | "The %u index is already registered in RDMA netlink\n", | |
109 | index); | |
110 | return; | |
111 | } | |
b2cbae2c | 112 | |
c9901724 LR |
113 | rdma_nl_types[index].cb_table = cb_table; |
114 | mutex_unlock(&rdma_nl_mutex); | |
b2cbae2c | 115 | } |
c9901724 | 116 | EXPORT_SYMBOL(rdma_nl_register); |
b2cbae2c | 117 | |
c9901724 | 118 | void rdma_nl_unregister(unsigned int index) |
b2cbae2c | 119 | { |
c9901724 LR |
120 | mutex_lock(&rdma_nl_mutex); |
121 | rdma_nl_types[index].cb_table = NULL; | |
122 | mutex_unlock(&rdma_nl_mutex); | |
b2cbae2c | 123 | } |
c9901724 | 124 | EXPORT_SYMBOL(rdma_nl_unregister); |
b2cbae2c RD |
125 | |
126 | void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq, | |
30dc5e63 | 127 | int len, int client, int op, int flags) |
b2cbae2c | 128 | { |
1a1c116f | 129 | *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op), len, flags); |
e0527334 | 130 | if (!*nlh) |
1a1c116f | 131 | return NULL; |
e0527334 | 132 | return nlmsg_data(*nlh); |
b2cbae2c RD |
133 | } |
134 | EXPORT_SYMBOL(ibnl_put_msg); | |
135 | ||
136 | int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh, | |
137 | int len, void *data, int type) | |
138 | { | |
1a1c116f LR |
139 | if (nla_put(skb, type, len, data)) { |
140 | nlmsg_cancel(skb, nlh); | |
141 | return -EMSGSIZE; | |
142 | } | |
b2cbae2c | 143 | return 0; |
b2cbae2c RD |
144 | } |
145 | EXPORT_SYMBOL(ibnl_put_attr); | |
146 | ||
3c3e75d5 LR |
147 | static int rdma_nl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
148 | struct netlink_ext_ack *extack) | |
b2cbae2c | 149 | { |
b2cbae2c | 150 | int type = nlh->nlmsg_type; |
c9901724 | 151 | unsigned int index = RDMA_NL_GET_CLIENT(type); |
1ae5ccc7 | 152 | unsigned int op = RDMA_NL_GET_OP(type); |
c9901724 LR |
153 | struct netlink_callback cb = {}; |
154 | struct netlink_dump_control c = {}; | |
b2cbae2c | 155 | |
c9901724 LR |
156 | if (!is_nl_valid(index, op)) |
157 | return -EINVAL; | |
158 | ||
e3a2b93d LR |
159 | if ((rdma_nl_types[index].cb_table[op].flags & RDMA_NL_ADMIN_PERM) && |
160 | !netlink_capable(skb, CAP_NET_ADMIN)) | |
161 | return -EPERM; | |
162 | ||
c9901724 LR |
163 | /* |
164 | * For response or local service set_timeout request, | |
165 | * there is no need to use netlink_dump_start. | |
166 | */ | |
167 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST) || | |
168 | (index == RDMA_NL_LS && op == RDMA_NL_LS_OP_SET_TIMEOUT)) { | |
169 | cb.skb = skb; | |
170 | cb.nlh = nlh; | |
171 | cb.dump = rdma_nl_types[index].cb_table[op].dump; | |
c9901724 | 172 | return cb.dump(skb, &cb); |
b2cbae2c RD |
173 | } |
174 | ||
c9901724 | 175 | c.dump = rdma_nl_types[index].cb_table[op].dump; |
c9901724 | 176 | return netlink_dump_start(nls, skb, nlh, &c); |
b2cbae2c RD |
177 | } |
178 | ||
3c3e75d5 LR |
179 | /* |
180 | * This function is similar to netlink_rcv_skb with one exception: | |
181 | * It calls to the callback for the netlink messages without NLM_F_REQUEST | |
182 | * flag. These messages are intended for RDMA_NL_LS consumer, so it is allowed | |
183 | * for that consumer only. | |
184 | */ | |
185 | static int rdma_nl_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, | |
186 | struct nlmsghdr *, | |
187 | struct netlink_ext_ack *)) | |
bc10ed7d | 188 | { |
3c3e75d5 | 189 | struct netlink_ext_ack extack = {}; |
bc10ed7d | 190 | struct nlmsghdr *nlh; |
3c3e75d5 | 191 | int err; |
bc10ed7d | 192 | |
bc10ed7d | 193 | while (skb->len >= nlmsg_total_size(0)) { |
3c3e75d5 LR |
194 | int msglen; |
195 | ||
bc10ed7d | 196 | nlh = nlmsg_hdr(skb); |
3c3e75d5 | 197 | err = 0; |
bc10ed7d KW |
198 | |
199 | if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) | |
3c3e75d5 | 200 | return 0; |
bc10ed7d | 201 | |
3c3e75d5 LR |
202 | /* |
203 | * Generally speaking, the only requests are handled | |
204 | * by the kernel, but RDMA_NL_LS is different, because it | |
205 | * runs backward netlink scheme. Kernel initiates messages | |
206 | * and waits for reply with data to keep pathrecord cache | |
207 | * in sync. | |
208 | */ | |
209 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST) && | |
210 | (RDMA_NL_GET_CLIENT(nlh->nlmsg_type) != RDMA_NL_LS)) | |
211 | goto ack; | |
212 | ||
213 | /* Skip control messages */ | |
214 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) | |
215 | goto ack; | |
bc10ed7d | 216 | |
3c3e75d5 LR |
217 | err = cb(skb, nlh, &extack); |
218 | if (err == -EINTR) | |
219 | goto skip; | |
bc10ed7d | 220 | |
3c3e75d5 LR |
221 | ack: |
222 | if (nlh->nlmsg_flags & NLM_F_ACK || err) | |
223 | netlink_ack(skb, nlh, err, &extack); | |
224 | ||
225 | skip: | |
bc10ed7d KW |
226 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
227 | if (msglen > skb->len) | |
228 | msglen = skb->len; | |
229 | skb_pull(skb, msglen); | |
230 | } | |
3c3e75d5 LR |
231 | |
232 | return 0; | |
bc10ed7d KW |
233 | } |
234 | ||
3c3e75d5 | 235 | static void rdma_nl_rcv(struct sk_buff *skb) |
b2cbae2c | 236 | { |
c9901724 | 237 | mutex_lock(&rdma_nl_mutex); |
3c3e75d5 | 238 | rdma_nl_rcv_skb(skb, &rdma_nl_rcv_msg); |
c9901724 | 239 | mutex_unlock(&rdma_nl_mutex); |
b2cbae2c RD |
240 | } |
241 | ||
f00e6463 | 242 | int rdma_nl_unicast(struct sk_buff *skb, u32 pid) |
30dc5e63 | 243 | { |
cea05ead MI |
244 | int err; |
245 | ||
9047811b | 246 | err = netlink_unicast(nls, skb, pid, MSG_DONTWAIT); |
cea05ead | 247 | return (err < 0) ? err : 0; |
30dc5e63 | 248 | } |
f00e6463 | 249 | EXPORT_SYMBOL(rdma_nl_unicast); |
30dc5e63 | 250 | |
f00e6463 | 251 | int rdma_nl_unicast_wait(struct sk_buff *skb, __u32 pid) |
9047811b IM |
252 | { |
253 | int err; | |
254 | ||
255 | err = netlink_unicast(nls, skb, pid, 0); | |
256 | return (err < 0) ? err : 0; | |
257 | } | |
f00e6463 | 258 | EXPORT_SYMBOL(rdma_nl_unicast_wait); |
9047811b | 259 | |
4d7f693a | 260 | int rdma_nl_multicast(struct sk_buff *skb, unsigned int group, gfp_t flags) |
30dc5e63 TN |
261 | { |
262 | return nlmsg_multicast(nls, skb, 0, group, flags); | |
263 | } | |
4d7f693a | 264 | EXPORT_SYMBOL(rdma_nl_multicast); |
30dc5e63 | 265 | |
c9901724 | 266 | int __init rdma_nl_init(void) |
b2cbae2c | 267 | { |
a31f2d17 | 268 | struct netlink_kernel_cfg cfg = { |
3c3e75d5 | 269 | .input = rdma_nl_rcv, |
a31f2d17 PNA |
270 | }; |
271 | ||
9f00d977 | 272 | nls = netlink_kernel_create(&init_net, NETLINK_RDMA, &cfg); |
c9901724 | 273 | if (!nls) |
b2cbae2c | 274 | return -ENOMEM; |
b2cbae2c | 275 | |
cea05ead | 276 | nls->sk_sndtimeo = 10 * HZ; |
b2cbae2c RD |
277 | return 0; |
278 | } | |
279 | ||
c9901724 | 280 | void rdma_nl_exit(void) |
b2cbae2c | 281 | { |
c9901724 | 282 | int idx; |
b2cbae2c | 283 | |
c9901724 LR |
284 | for (idx = 0; idx < RDMA_NL_NUM_CLIENTS; idx++) |
285 | rdma_nl_unregister(idx); | |
b2cbae2c RD |
286 | |
287 | netlink_kernel_release(nls); | |
288 | } |