netlink.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
  33. #include <linux/export.h>
  34. #include <net/netlink.h>
  35. #include <net/net_namespace.h>
  36. #include <net/sock.h>
  37. #include <rdma/rdma_netlink.h>
  38. struct ibnl_client {
  39. struct list_head list;
  40. int index;
  41. int nops;
  42. const struct ibnl_client_cbs *cb_table;
  43. };
  44. static DEFINE_MUTEX(ibnl_mutex);
  45. static struct sock *nls;
  46. static LIST_HEAD(client_list);
  47. int ibnl_chk_listeners(unsigned int group)
  48. {
  49. if (netlink_has_listeners(nls, group) == 0)
  50. return -1;
  51. return 0;
  52. }
  53. EXPORT_SYMBOL(ibnl_chk_listeners);
  54. int ibnl_add_client(int index, int nops,
  55. const struct ibnl_client_cbs cb_table[])
  56. {
  57. struct ibnl_client *cur;
  58. struct ibnl_client *nl_client;
  59. nl_client = kmalloc(sizeof *nl_client, GFP_KERNEL);
  60. if (!nl_client)
  61. return -ENOMEM;
  62. nl_client->index = index;
  63. nl_client->nops = nops;
  64. nl_client->cb_table = cb_table;
  65. mutex_lock(&ibnl_mutex);
  66. list_for_each_entry(cur, &client_list, list) {
  67. if (cur->index == index) {
  68. pr_warn("Client for %d already exists\n", index);
  69. mutex_unlock(&ibnl_mutex);
  70. kfree(nl_client);
  71. return -EINVAL;
  72. }
  73. }
  74. list_add_tail(&nl_client->list, &client_list);
  75. mutex_unlock(&ibnl_mutex);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL(ibnl_add_client);
  79. int ibnl_remove_client(int index)
  80. {
  81. struct ibnl_client *cur, *next;
  82. mutex_lock(&ibnl_mutex);
  83. list_for_each_entry_safe(cur, next, &client_list, list) {
  84. if (cur->index == index) {
  85. list_del(&(cur->list));
  86. mutex_unlock(&ibnl_mutex);
  87. kfree(cur);
  88. return 0;
  89. }
  90. }
  91. pr_warn("Can't remove callback for client idx %d. Not found\n", index);
  92. mutex_unlock(&ibnl_mutex);
  93. return -EINVAL;
  94. }
  95. EXPORT_SYMBOL(ibnl_remove_client);
  96. void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
  97. int len, int client, int op, int flags)
  98. {
  99. unsigned char *prev_tail;
  100. prev_tail = skb_tail_pointer(skb);
  101. *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op),
  102. len, flags);
  103. if (!*nlh)
  104. goto out_nlmsg_trim;
  105. (*nlh)->nlmsg_len = skb_tail_pointer(skb) - prev_tail;
  106. return nlmsg_data(*nlh);
  107. out_nlmsg_trim:
  108. nlmsg_trim(skb, prev_tail);
  109. return NULL;
  110. }
  111. EXPORT_SYMBOL(ibnl_put_msg);
  112. int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
  113. int len, void *data, int type)
  114. {
  115. unsigned char *prev_tail;
  116. prev_tail = skb_tail_pointer(skb);
  117. if (nla_put(skb, type, len, data))
  118. goto nla_put_failure;
  119. nlh->nlmsg_len += skb_tail_pointer(skb) - prev_tail;
  120. return 0;
  121. nla_put_failure:
  122. nlmsg_trim(skb, prev_tail - nlh->nlmsg_len);
  123. return -EMSGSIZE;
  124. }
  125. EXPORT_SYMBOL(ibnl_put_attr);
  126. static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  127. {
  128. struct ibnl_client *client;
  129. int type = nlh->nlmsg_type;
  130. int index = RDMA_NL_GET_CLIENT(type);
  131. int op = RDMA_NL_GET_OP(type);
  132. list_for_each_entry(client, &client_list, list) {
  133. if (client->index == index) {
  134. if (op < 0 || op >= client->nops ||
  135. !client->cb_table[op].dump)
  136. return -EINVAL;
  137. /*
  138. * For response or local service set_timeout request,
  139. * there is no need to use netlink_dump_start.
  140. */
  141. if (!(nlh->nlmsg_flags & NLM_F_REQUEST) ||
  142. (index == RDMA_NL_LS &&
  143. op == RDMA_NL_LS_OP_SET_TIMEOUT)) {
  144. struct netlink_callback cb = {
  145. .skb = skb,
  146. .nlh = nlh,
  147. .dump = client->cb_table[op].dump,
  148. .module = client->cb_table[op].module,
  149. };
  150. return cb.dump(skb, &cb);
  151. }
  152. {
  153. struct netlink_dump_control c = {
  154. .dump = client->cb_table[op].dump,
  155. .module = client->cb_table[op].module,
  156. };
  157. return netlink_dump_start(nls, skb, nlh, &c);
  158. }
  159. }
  160. }
  161. pr_info("Index %d wasn't found in client list\n", index);
  162. return -EINVAL;
  163. }
  164. static void ibnl_rcv_reply_skb(struct sk_buff *skb)
  165. {
  166. struct nlmsghdr *nlh;
  167. int msglen;
  168. /*
  169. * Process responses until there is no more message or the first
  170. * request. Generally speaking, it is not recommended to mix responses
  171. * with requests.
  172. */
  173. while (skb->len >= nlmsg_total_size(0)) {
  174. nlh = nlmsg_hdr(skb);
  175. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  176. return;
  177. /* Handle response only */
  178. if (nlh->nlmsg_flags & NLM_F_REQUEST)
  179. return;
  180. ibnl_rcv_msg(skb, nlh);
  181. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  182. if (msglen > skb->len)
  183. msglen = skb->len;
  184. skb_pull(skb, msglen);
  185. }
  186. }
  187. static void ibnl_rcv(struct sk_buff *skb)
  188. {
  189. mutex_lock(&ibnl_mutex);
  190. ibnl_rcv_reply_skb(skb);
  191. netlink_rcv_skb(skb, &ibnl_rcv_msg);
  192. mutex_unlock(&ibnl_mutex);
  193. }
  194. int ibnl_unicast(struct sk_buff *skb, struct nlmsghdr *nlh,
  195. __u32 pid)
  196. {
  197. return nlmsg_unicast(nls, skb, pid);
  198. }
  199. EXPORT_SYMBOL(ibnl_unicast);
  200. int ibnl_multicast(struct sk_buff *skb, struct nlmsghdr *nlh,
  201. unsigned int group, gfp_t flags)
  202. {
  203. return nlmsg_multicast(nls, skb, 0, group, flags);
  204. }
  205. EXPORT_SYMBOL(ibnl_multicast);
  206. int __init ibnl_init(void)
  207. {
  208. struct netlink_kernel_cfg cfg = {
  209. .input = ibnl_rcv,
  210. };
  211. nls = netlink_kernel_create(&init_net, NETLINK_RDMA, &cfg);
  212. if (!nls) {
  213. pr_warn("Failed to create netlink socket\n");
  214. return -ENOMEM;
  215. }
  216. return 0;
  217. }
  218. void ibnl_cleanup(void)
  219. {
  220. struct ibnl_client *cur, *next;
  221. mutex_lock(&ibnl_mutex);
  222. list_for_each_entry_safe(cur, next, &client_list, list) {
  223. list_del(&(cur->list));
  224. kfree(cur);
  225. }
  226. mutex_unlock(&ibnl_mutex);
  227. netlink_kernel_release(nls);
  228. }