1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
34
35#include <linux/export.h>
36#include <net/netlink.h>
37#include <net/net_namespace.h>
38#include <net/sock.h>
39#include <rdma/rdma_netlink.h>
40#include "core_priv.h"
41
42struct ibnl_client {
43 struct list_head list;
44 int index;
45 int nops;
46 const struct ibnl_client_cbs *cb_table;
47};
48
49static DEFINE_MUTEX(ibnl_mutex);
50static struct sock *nls;
51static LIST_HEAD(client_list);
52
53int ibnl_chk_listeners(unsigned int group)
54{
55 if (netlink_has_listeners(nls, group) == 0)
56 return -1;
57 return 0;
58}
59
60int ibnl_add_client(int index, int nops,
61 const struct ibnl_client_cbs cb_table[])
62{
63 struct ibnl_client *cur;
64 struct ibnl_client *nl_client;
65
66 nl_client = kmalloc(sizeof *nl_client, GFP_KERNEL);
67 if (!nl_client)
68 return -ENOMEM;
69
70 nl_client->index = index;
71 nl_client->nops = nops;
72 nl_client->cb_table = cb_table;
73
74 mutex_lock(&ibnl_mutex);
75
76 list_for_each_entry(cur, &client_list, list) {
77 if (cur->index == index) {
78 pr_warn("Client for %d already exists\n", index);
79 mutex_unlock(&ibnl_mutex);
80 kfree(nl_client);
81 return -EINVAL;
82 }
83 }
84
85 list_add_tail(&nl_client->list, &client_list);
86
87 mutex_unlock(&ibnl_mutex);
88
89 return 0;
90}
91EXPORT_SYMBOL(ibnl_add_client);
92
93int ibnl_remove_client(int index)
94{
95 struct ibnl_client *cur, *next;
96
97 mutex_lock(&ibnl_mutex);
98 list_for_each_entry_safe(cur, next, &client_list, list) {
99 if (cur->index == index) {
100 list_del(&(cur->list));
101 mutex_unlock(&ibnl_mutex);
102 kfree(cur);
103 return 0;
104 }
105 }
106 pr_warn("Can't remove callback for client idx %d. Not found\n", index);
107 mutex_unlock(&ibnl_mutex);
108
109 return -EINVAL;
110}
111EXPORT_SYMBOL(ibnl_remove_client);
112
113void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
114 int len, int client, int op, int flags)
115{
116 unsigned char *prev_tail;
117
118 prev_tail = skb_tail_pointer(skb);
119 *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op),
120 len, flags);
121 if (!*nlh)
122 goto out_nlmsg_trim;
123 (*nlh)->nlmsg_len = skb_tail_pointer(skb) - prev_tail;
124 return nlmsg_data(*nlh);
125
126out_nlmsg_trim:
127 nlmsg_trim(skb, prev_tail);
128 return NULL;
129}
130EXPORT_SYMBOL(ibnl_put_msg);
131
132int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
133 int len, void *data, int type)
134{
135 unsigned char *prev_tail;
136
137 prev_tail = skb_tail_pointer(skb);
138 if (nla_put(skb, type, len, data))
139 goto nla_put_failure;
140 nlh->nlmsg_len += skb_tail_pointer(skb) - prev_tail;
141 return 0;
142
143nla_put_failure:
144 nlmsg_trim(skb, prev_tail - nlh->nlmsg_len);
145 return -EMSGSIZE;
146}
147EXPORT_SYMBOL(ibnl_put_attr);
148
149static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
150 struct netlink_ext_ack *extack)
151{
152 struct ibnl_client *client;
153 int type = nlh->nlmsg_type;
154 int index = RDMA_NL_GET_CLIENT(type);
155 unsigned int op = RDMA_NL_GET_OP(type);
156
157 list_for_each_entry(client, &client_list, list) {
158 if (client->index == index) {
159 if (op >= client->nops || !client->cb_table[op].dump)
160 return -EINVAL;
161
162
163
164
165
166 if (!(nlh->nlmsg_flags & NLM_F_REQUEST) ||
167 (index == RDMA_NL_LS &&
168 op == RDMA_NL_LS_OP_SET_TIMEOUT)) {
169 struct netlink_callback cb = {
170 .skb = skb,
171 .nlh = nlh,
172 .dump = client->cb_table[op].dump,
173 .module = client->cb_table[op].module,
174 };
175
176 return cb.dump(skb, &cb);
177 }
178
179 {
180 struct netlink_dump_control c = {
181 .dump = client->cb_table[op].dump,
182 .module = client->cb_table[op].module,
183 };
184 return netlink_dump_start(nls, skb, nlh, &c);
185 }
186 }
187 }
188
189 pr_info("Index %d wasn't found in client list\n", index);
190 return -EINVAL;
191}
192
193static void ibnl_rcv_reply_skb(struct sk_buff *skb)
194{
195 struct nlmsghdr *nlh;
196 int msglen;
197
198
199
200
201
202
203 while (skb->len >= nlmsg_total_size(0)) {
204 nlh = nlmsg_hdr(skb);
205
206 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
207 return;
208
209
210 if (nlh->nlmsg_flags & NLM_F_REQUEST)
211 return;
212
213 ibnl_rcv_msg(skb, nlh, NULL);
214
215 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
216 if (msglen > skb->len)
217 msglen = skb->len;
218 skb_pull(skb, msglen);
219 }
220}
221
222static void ibnl_rcv(struct sk_buff *skb)
223{
224 mutex_lock(&ibnl_mutex);
225 ibnl_rcv_reply_skb(skb);
226 netlink_rcv_skb(skb, &ibnl_rcv_msg);
227 mutex_unlock(&ibnl_mutex);
228}
229
230int ibnl_unicast(struct sk_buff *skb, struct nlmsghdr *nlh,
231 __u32 pid)
232{
233 int err;
234
235 err = netlink_unicast(nls, skb, pid, 0);
236 return (err < 0) ? err : 0;
237}
238EXPORT_SYMBOL(ibnl_unicast);
239
240int ibnl_multicast(struct sk_buff *skb, struct nlmsghdr *nlh,
241 unsigned int group, gfp_t flags)
242{
243 return nlmsg_multicast(nls, skb, 0, group, flags);
244}
245EXPORT_SYMBOL(ibnl_multicast);
246
247int __init ibnl_init(void)
248{
249 struct netlink_kernel_cfg cfg = {
250 .input = ibnl_rcv,
251 };
252
253 nls = netlink_kernel_create(&init_net, NETLINK_RDMA, &cfg);
254 if (!nls) {
255 pr_warn("Failed to create netlink socket\n");
256 return -ENOMEM;
257 }
258
259 nls->sk_sndtimeo = 10 * HZ;
260 return 0;
261}
262
263void ibnl_cleanup(void)
264{
265 struct ibnl_client *cur, *next;
266
267 mutex_lock(&ibnl_mutex);
268 list_for_each_entry_safe(cur, next, &client_list, list) {
269 list_del(&(cur->list));
270 kfree(cur);
271 }
272 mutex_unlock(&ibnl_mutex);
273
274 netlink_kernel_release(nls);
275}
276