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
41struct ibnl_client {
42 struct list_head list;
43 int index;
44 int nops;
45 const struct ibnl_client_cbs *cb_table;
46};
47
48static DEFINE_MUTEX(ibnl_mutex);
49static struct sock *nls;
50static LIST_HEAD(client_list);
51
52int ibnl_add_client(int index, int nops,
53 const struct ibnl_client_cbs cb_table[])
54{
55 struct ibnl_client *cur;
56 struct ibnl_client *nl_client;
57
58 nl_client = kmalloc(sizeof *nl_client, GFP_KERNEL);
59 if (!nl_client)
60 return -ENOMEM;
61
62 nl_client->index = index;
63 nl_client->nops = nops;
64 nl_client->cb_table = cb_table;
65
66 mutex_lock(&ibnl_mutex);
67
68 list_for_each_entry(cur, &client_list, list) {
69 if (cur->index == index) {
70 pr_warn("Client for %d already exists\n", index);
71 mutex_unlock(&ibnl_mutex);
72 kfree(nl_client);
73 return -EINVAL;
74 }
75 }
76
77 list_add_tail(&nl_client->list, &client_list);
78
79 mutex_unlock(&ibnl_mutex);
80
81 return 0;
82}
83EXPORT_SYMBOL(ibnl_add_client);
84
85int ibnl_remove_client(int index)
86{
87 struct ibnl_client *cur, *next;
88
89 mutex_lock(&ibnl_mutex);
90 list_for_each_entry_safe(cur, next, &client_list, list) {
91 if (cur->index == index) {
92 list_del(&(cur->list));
93 mutex_unlock(&ibnl_mutex);
94 kfree(cur);
95 return 0;
96 }
97 }
98 pr_warn("Can't remove callback for client idx %d. Not found\n", index);
99 mutex_unlock(&ibnl_mutex);
100
101 return -EINVAL;
102}
103EXPORT_SYMBOL(ibnl_remove_client);
104
105void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
106 int len, int client, int op)
107{
108 unsigned char *prev_tail;
109
110 prev_tail = skb_tail_pointer(skb);
111 *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op),
112 len, NLM_F_MULTI);
113 if (!*nlh)
114 goto out_nlmsg_trim;
115 (*nlh)->nlmsg_len = skb_tail_pointer(skb) - prev_tail;
116 return nlmsg_data(*nlh);
117
118out_nlmsg_trim:
119 nlmsg_trim(skb, prev_tail);
120 return NULL;
121}
122EXPORT_SYMBOL(ibnl_put_msg);
123
124int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
125 int len, void *data, int type)
126{
127 unsigned char *prev_tail;
128
129 prev_tail = skb_tail_pointer(skb);
130 if (nla_put(skb, type, len, data))
131 goto nla_put_failure;
132 nlh->nlmsg_len += skb_tail_pointer(skb) - prev_tail;
133 return 0;
134
135nla_put_failure:
136 nlmsg_trim(skb, prev_tail - nlh->nlmsg_len);
137 return -EMSGSIZE;
138}
139EXPORT_SYMBOL(ibnl_put_attr);
140
141static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
142{
143 struct ibnl_client *client;
144 int type = nlh->nlmsg_type;
145 int index = RDMA_NL_GET_CLIENT(type);
146 int op = RDMA_NL_GET_OP(type);
147
148 list_for_each_entry(client, &client_list, list) {
149 if (client->index == index) {
150 if (op < 0 || op >= client->nops ||
151 !client->cb_table[RDMA_NL_GET_OP(op)].dump)
152 return -EINVAL;
153
154 {
155 struct netlink_dump_control c = {
156 .dump = client->cb_table[op].dump,
157 };
158 return netlink_dump_start(nls, skb, nlh, &c);
159 }
160 }
161 }
162
163 pr_info("Index %d wasn't found in client list\n", index);
164 return -EINVAL;
165}
166
167static void ibnl_rcv(struct sk_buff *skb)
168{
169 mutex_lock(&ibnl_mutex);
170 netlink_rcv_skb(skb, &ibnl_rcv_msg);
171 mutex_unlock(&ibnl_mutex);
172}
173
174int __init ibnl_init(void)
175{
176 struct netlink_kernel_cfg cfg = {
177 .input = ibnl_rcv,
178 };
179
180 nls = netlink_kernel_create(&init_net, NETLINK_RDMA, THIS_MODULE, &cfg);
181 if (!nls) {
182 pr_warn("Failed to create netlink socket\n");
183 return -ENOMEM;
184 }
185
186 return 0;
187}
188
189void ibnl_cleanup(void)
190{
191 struct ibnl_client *cur, *next;
192
193 mutex_lock(&ibnl_mutex);
194 list_for_each_entry_safe(cur, next, &client_list, list) {
195 list_del(&(cur->list));
196 kfree(cur);
197 }
198 mutex_unlock(&ibnl_mutex);
199
200 netlink_kernel_release(nls);
201}
202