blob: 23dd5a5c7597122de45eddf8211c736bedae553e [file] [log] [blame]
Roland Dreierb2cbae22011-05-20 11:46:11 -07001/*
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
Paul Gortmakerb108d972011-05-27 15:29:33 -040035#include <linux/export.h>
Roland Dreierb2cbae22011-05-20 11:46:11 -070036#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,
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -0500106 int len, int client, int op, int flags)
Roland Dreierb2cbae22011-05-20 11:46:11 -0700107{
108 unsigned char *prev_tail;
109
110 prev_tail = skb_tail_pointer(skb);
David S. Millere0527332012-06-26 21:43:19 -0700111 *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op),
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -0500112 len, flags);
David S. Millere0527332012-06-26 21:43:19 -0700113 if (!*nlh)
114 goto out_nlmsg_trim;
Roland Dreierb2cbae22011-05-20 11:46:11 -0700115 (*nlh)->nlmsg_len = skb_tail_pointer(skb) - prev_tail;
David S. Millere0527332012-06-26 21:43:19 -0700116 return nlmsg_data(*nlh);
Roland Dreierb2cbae22011-05-20 11:46:11 -0700117
David S. Millere0527332012-06-26 21:43:19 -0700118out_nlmsg_trim:
Roland Dreierb2cbae22011-05-20 11:46:11 -0700119 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);
David S. Miller4e24ffa2012-04-01 20:19:38 -0400130 if (nla_put(skb, type, len, data))
131 goto nla_put_failure;
Roland Dreierb2cbae22011-05-20 11:46:11 -0700132 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 ||
Mathias Krause54767812013-09-30 22:03:54 +0200151 !client->cb_table[op].dump)
Roland Dreierb2cbae22011-05-20 11:46:11 -0700152 return -EINVAL;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000153
154 {
155 struct netlink_dump_control c = {
156 .dump = client->cb_table[op].dump,
Gao feng809d5fc2012-10-04 20:15:49 +0000157 .module = client->cb_table[op].module,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000158 };
159 return netlink_dump_start(nls, skb, nlh, &c);
160 }
Roland Dreierb2cbae22011-05-20 11:46:11 -0700161 }
162 }
163
164 pr_info("Index %d wasn't found in client list\n", index);
165 return -EINVAL;
166}
167
168static void ibnl_rcv(struct sk_buff *skb)
169{
170 mutex_lock(&ibnl_mutex);
171 netlink_rcv_skb(skb, &ibnl_rcv_msg);
172 mutex_unlock(&ibnl_mutex);
173}
174
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -0500175int ibnl_unicast(struct sk_buff *skb, struct nlmsghdr *nlh,
176 __u32 pid)
177{
178 return nlmsg_unicast(nls, skb, pid);
179}
180EXPORT_SYMBOL(ibnl_unicast);
181
182int ibnl_multicast(struct sk_buff *skb, struct nlmsghdr *nlh,
183 unsigned int group, gfp_t flags)
184{
185 return nlmsg_multicast(nls, skb, 0, group, flags);
186}
187EXPORT_SYMBOL(ibnl_multicast);
188
Roland Dreierb2cbae22011-05-20 11:46:11 -0700189int __init ibnl_init(void)
190{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000191 struct netlink_kernel_cfg cfg = {
192 .input = ibnl_rcv,
193 };
194
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000195 nls = netlink_kernel_create(&init_net, NETLINK_RDMA, &cfg);
Roland Dreierb2cbae22011-05-20 11:46:11 -0700196 if (!nls) {
197 pr_warn("Failed to create netlink socket\n");
198 return -ENOMEM;
199 }
200
201 return 0;
202}
203
204void ibnl_cleanup(void)
205{
206 struct ibnl_client *cur, *next;
207
208 mutex_lock(&ibnl_mutex);
209 list_for_each_entry_safe(cur, next, &client_list, list) {
210 list_del(&(cur->list));
211 kfree(cur);
212 }
213 mutex_unlock(&ibnl_mutex);
214
215 netlink_kernel_release(nls);
216}