Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / infiniband / core / netlink.c
blobd1c8196d15d7e9b33cafd71606c2cb2383055623
1 /*
2 * Copyright (c) 2010 Voltaire Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
33 #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
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>
41 struct ibnl_client {
42 struct list_head list;
43 int index;
44 int nops;
45 const struct ibnl_client_cbs *cb_table;
48 static DEFINE_MUTEX(ibnl_mutex);
49 static struct sock *nls;
50 static LIST_HEAD(client_list);
52 int ibnl_add_client(int index, int nops,
53 const struct ibnl_client_cbs cb_table[])
55 struct ibnl_client *cur;
56 struct ibnl_client *nl_client;
58 nl_client = kmalloc(sizeof *nl_client, GFP_KERNEL);
59 if (!nl_client)
60 return -ENOMEM;
62 nl_client->index = index;
63 nl_client->nops = nops;
64 nl_client->cb_table = cb_table;
66 mutex_lock(&ibnl_mutex);
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;
77 list_add_tail(&nl_client->list, &client_list);
79 mutex_unlock(&ibnl_mutex);
81 return 0;
83 EXPORT_SYMBOL(ibnl_add_client);
85 int ibnl_remove_client(int index)
87 struct ibnl_client *cur, *next;
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;
98 pr_warn("Can't remove callback for client idx %d. Not found\n", index);
99 mutex_unlock(&ibnl_mutex);
101 return -EINVAL;
103 EXPORT_SYMBOL(ibnl_remove_client);
105 void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
106 int len, int client, int op)
108 unsigned char *prev_tail;
110 prev_tail = skb_tail_pointer(skb);
111 *nlh = NLMSG_NEW(skb, 0, seq, RDMA_NL_GET_TYPE(client, op),
112 len, NLM_F_MULTI);
113 (*nlh)->nlmsg_len = skb_tail_pointer(skb) - prev_tail;
114 return NLMSG_DATA(*nlh);
116 nlmsg_failure:
117 nlmsg_trim(skb, prev_tail);
118 return NULL;
120 EXPORT_SYMBOL(ibnl_put_msg);
122 int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
123 int len, void *data, int type)
125 unsigned char *prev_tail;
127 prev_tail = skb_tail_pointer(skb);
128 NLA_PUT(skb, type, len, data);
129 nlh->nlmsg_len += skb_tail_pointer(skb) - prev_tail;
130 return 0;
132 nla_put_failure:
133 nlmsg_trim(skb, prev_tail - nlh->nlmsg_len);
134 return -EMSGSIZE;
136 EXPORT_SYMBOL(ibnl_put_attr);
138 static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
140 struct ibnl_client *client;
141 int type = nlh->nlmsg_type;
142 int index = RDMA_NL_GET_CLIENT(type);
143 int op = RDMA_NL_GET_OP(type);
145 list_for_each_entry(client, &client_list, list) {
146 if (client->index == index) {
147 if (op < 0 || op >= client->nops ||
148 !client->cb_table[RDMA_NL_GET_OP(op)].dump)
149 return -EINVAL;
150 return netlink_dump_start(nls, skb, nlh,
151 client->cb_table[op].dump,
152 NULL, 0);
156 pr_info("Index %d wasn't found in client list\n", index);
157 return -EINVAL;
160 static void ibnl_rcv(struct sk_buff *skb)
162 mutex_lock(&ibnl_mutex);
163 netlink_rcv_skb(skb, &ibnl_rcv_msg);
164 mutex_unlock(&ibnl_mutex);
167 int __init ibnl_init(void)
169 nls = netlink_kernel_create(&init_net, NETLINK_RDMA, 0, ibnl_rcv,
170 NULL, THIS_MODULE);
171 if (!nls) {
172 pr_warn("Failed to create netlink socket\n");
173 return -ENOMEM;
176 return 0;
179 void ibnl_cleanup(void)
181 struct ibnl_client *cur, *next;
183 mutex_lock(&ibnl_mutex);
184 list_for_each_entry_safe(cur, next, &client_list, list) {
185 list_del(&(cur->list));
186 kfree(cur);
188 mutex_unlock(&ibnl_mutex);
190 netlink_kernel_release(nls);