netlink.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_add_client(int index, int nops,
  48. const struct ibnl_client_cbs cb_table[])
  49. {
  50. struct ibnl_client *cur;
  51. struct ibnl_client *nl_client;
  52. nl_client = kmalloc(sizeof *nl_client, GFP_KERNEL);
  53. if (!nl_client)
  54. return -ENOMEM;
  55. nl_client->index = index;
  56. nl_client->nops = nops;
  57. nl_client->cb_table = cb_table;
  58. mutex_lock(&ibnl_mutex);
  59. list_for_each_entry(cur, &client_list, list) {
  60. if (cur->index == index) {
  61. pr_warn("Client for %d already exists\n", index);
  62. mutex_unlock(&ibnl_mutex);
  63. kfree(nl_client);
  64. return -EINVAL;
  65. }
  66. }
  67. list_add_tail(&nl_client->list, &client_list);
  68. mutex_unlock(&ibnl_mutex);
  69. return 0;
  70. }
  71. EXPORT_SYMBOL(ibnl_add_client);
  72. int ibnl_remove_client(int index)
  73. {
  74. struct ibnl_client *cur, *next;
  75. mutex_lock(&ibnl_mutex);
  76. list_for_each_entry_safe(cur, next, &client_list, list) {
  77. if (cur->index == index) {
  78. list_del(&(cur->list));
  79. mutex_unlock(&ibnl_mutex);
  80. kfree(cur);
  81. return 0;
  82. }
  83. }
  84. pr_warn("Can't remove callback for client idx %d. Not found\n", index);
  85. mutex_unlock(&ibnl_mutex);
  86. return -EINVAL;
  87. }
  88. EXPORT_SYMBOL(ibnl_remove_client);
  89. void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
  90. int len, int client, int op)
  91. {
  92. unsigned char *prev_tail;
  93. prev_tail = skb_tail_pointer(skb);
  94. *nlh = NLMSG_NEW(skb, 0, seq, RDMA_NL_GET_TYPE(client, op),
  95. len, NLM_F_MULTI);
  96. (*nlh)->nlmsg_len = skb_tail_pointer(skb) - prev_tail;
  97. return NLMSG_DATA(*nlh);
  98. nlmsg_failure:
  99. nlmsg_trim(skb, prev_tail);
  100. return NULL;
  101. }
  102. EXPORT_SYMBOL(ibnl_put_msg);
  103. int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
  104. int len, void *data, int type)
  105. {
  106. unsigned char *prev_tail;
  107. prev_tail = skb_tail_pointer(skb);
  108. NLA_PUT(skb, type, len, data);
  109. nlh->nlmsg_len += skb_tail_pointer(skb) - prev_tail;
  110. return 0;
  111. nla_put_failure:
  112. nlmsg_trim(skb, prev_tail - nlh->nlmsg_len);
  113. return -EMSGSIZE;
  114. }
  115. EXPORT_SYMBOL(ibnl_put_attr);
  116. static int ibnl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  117. {
  118. struct ibnl_client *client;
  119. int type = nlh->nlmsg_type;
  120. int index = RDMA_NL_GET_CLIENT(type);
  121. int op = RDMA_NL_GET_OP(type);
  122. list_for_each_entry(client, &client_list, list) {
  123. if (client->index == index) {
  124. if (op < 0 || op >= client->nops ||
  125. !client->cb_table[RDMA_NL_GET_OP(op)].dump)
  126. return -EINVAL;
  127. {
  128. struct netlink_dump_control c = {
  129. .dump = client->cb_table[op].dump,
  130. .module = client->cb_table[op].module,
  131. };
  132. return netlink_dump_start(nls, skb, nlh, &c);
  133. }
  134. }
  135. }
  136. pr_info("Index %d wasn't found in client list\n", index);
  137. return -EINVAL;
  138. }
  139. static void ibnl_rcv(struct sk_buff *skb)
  140. {
  141. mutex_lock(&ibnl_mutex);
  142. netlink_rcv_skb(skb, &ibnl_rcv_msg);
  143. mutex_unlock(&ibnl_mutex);
  144. }
  145. int __init ibnl_init(void)
  146. {
  147. nls = netlink_kernel_create(&init_net, NETLINK_RDMA, 0, ibnl_rcv,
  148. NULL, THIS_MODULE);
  149. if (!nls) {
  150. pr_warn("Failed to create netlink socket\n");
  151. return -ENOMEM;
  152. }
  153. return 0;
  154. }
  155. void ibnl_cleanup(void)
  156. {
  157. struct ibnl_client *cur, *next;
  158. mutex_lock(&ibnl_mutex);
  159. list_for_each_entry_safe(cur, next, &client_list, list) {
  160. list_del(&(cur->list));
  161. kfree(cur);
  162. }
  163. mutex_unlock(&ibnl_mutex);
  164. netlink_kernel_release(nls);
  165. }