netlink.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include "core_priv.h"
  39. #include "core_priv.h"
  40. static DEFINE_MUTEX(rdma_nl_mutex);
  41. static struct sock *nls;
  42. static struct {
  43. const struct ibnl_client_cbs *cb_table;
  44. } rdma_nl_types[RDMA_NL_NUM_CLIENTS];
  45. int rdma_nl_chk_listeners(unsigned int group)
  46. {
  47. return (netlink_has_listeners(nls, group)) ? 0 : -1;
  48. }
  49. EXPORT_SYMBOL(rdma_nl_chk_listeners);
  50. static bool is_nl_msg_valid(unsigned int type, unsigned int op)
  51. {
  52. static const unsigned int max_num_ops[RDMA_NL_NUM_CLIENTS - 1] = {
  53. RDMA_NL_RDMA_CM_NUM_OPS,
  54. RDMA_NL_IWPM_NUM_OPS,
  55. 0,
  56. RDMA_NL_LS_NUM_OPS,
  57. 0 };
  58. /*
  59. * This BUILD_BUG_ON is intended to catch addition of new
  60. * RDMA netlink protocol without updating the array above.
  61. */
  62. BUILD_BUG_ON(RDMA_NL_NUM_CLIENTS != 6);
  63. if (type > RDMA_NL_NUM_CLIENTS - 1)
  64. return false;
  65. return (op < max_num_ops[type - 1]) ? true : false;
  66. }
  67. static bool is_nl_valid(unsigned int type, unsigned int op)
  68. {
  69. if (!is_nl_msg_valid(type, op) ||
  70. !rdma_nl_types[type].cb_table ||
  71. !rdma_nl_types[type].cb_table[op].dump)
  72. return false;
  73. return true;
  74. }
  75. void rdma_nl_register(unsigned int index,
  76. const struct ibnl_client_cbs cb_table[])
  77. {
  78. mutex_lock(&rdma_nl_mutex);
  79. if (!is_nl_msg_valid(index, 0)) {
  80. /*
  81. * All clients are not interesting in success/failure of
  82. * this call. They want to see the print to error log and
  83. * continue their initialization. Print warning for them,
  84. * because it is programmer's error to be here.
  85. */
  86. mutex_unlock(&rdma_nl_mutex);
  87. WARN(true,
  88. "The not-valid %u index was supplied to RDMA netlink\n",
  89. index);
  90. return;
  91. }
  92. if (rdma_nl_types[index].cb_table) {
  93. mutex_unlock(&rdma_nl_mutex);
  94. WARN(true,
  95. "The %u index is already registered in RDMA netlink\n",
  96. index);
  97. return;
  98. }
  99. rdma_nl_types[index].cb_table = cb_table;
  100. mutex_unlock(&rdma_nl_mutex);
  101. }
  102. EXPORT_SYMBOL(rdma_nl_register);
  103. void rdma_nl_unregister(unsigned int index)
  104. {
  105. mutex_lock(&rdma_nl_mutex);
  106. rdma_nl_types[index].cb_table = NULL;
  107. mutex_unlock(&rdma_nl_mutex);
  108. }
  109. EXPORT_SYMBOL(rdma_nl_unregister);
  110. void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
  111. int len, int client, int op, int flags)
  112. {
  113. *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op), len, flags);
  114. if (!*nlh)
  115. return NULL;
  116. return nlmsg_data(*nlh);
  117. }
  118. EXPORT_SYMBOL(ibnl_put_msg);
  119. int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
  120. int len, void *data, int type)
  121. {
  122. if (nla_put(skb, type, len, data)) {
  123. nlmsg_cancel(skb, nlh);
  124. return -EMSGSIZE;
  125. }
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(ibnl_put_attr);
  129. static int rdma_nl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  130. struct netlink_ext_ack *extack)
  131. {
  132. int type = nlh->nlmsg_type;
  133. unsigned int index = RDMA_NL_GET_CLIENT(type);
  134. unsigned int op = RDMA_NL_GET_OP(type);
  135. struct netlink_callback cb = {};
  136. struct netlink_dump_control c = {};
  137. if (!is_nl_valid(index, op))
  138. return -EINVAL;
  139. if ((rdma_nl_types[index].cb_table[op].flags & RDMA_NL_ADMIN_PERM) &&
  140. !netlink_capable(skb, CAP_NET_ADMIN))
  141. return -EPERM;
  142. /*
  143. * For response or local service set_timeout request,
  144. * there is no need to use netlink_dump_start.
  145. */
  146. if (!(nlh->nlmsg_flags & NLM_F_REQUEST) ||
  147. (index == RDMA_NL_LS && op == RDMA_NL_LS_OP_SET_TIMEOUT)) {
  148. cb.skb = skb;
  149. cb.nlh = nlh;
  150. cb.dump = rdma_nl_types[index].cb_table[op].dump;
  151. return cb.dump(skb, &cb);
  152. }
  153. c.dump = rdma_nl_types[index].cb_table[op].dump;
  154. return netlink_dump_start(nls, skb, nlh, &c);
  155. }
  156. /*
  157. * This function is similar to netlink_rcv_skb with one exception:
  158. * It calls to the callback for the netlink messages without NLM_F_REQUEST
  159. * flag. These messages are intended for RDMA_NL_LS consumer, so it is allowed
  160. * for that consumer only.
  161. */
  162. static int rdma_nl_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  163. struct nlmsghdr *,
  164. struct netlink_ext_ack *))
  165. {
  166. struct netlink_ext_ack extack = {};
  167. struct nlmsghdr *nlh;
  168. int err;
  169. while (skb->len >= nlmsg_total_size(0)) {
  170. int msglen;
  171. nlh = nlmsg_hdr(skb);
  172. err = 0;
  173. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  174. return 0;
  175. /*
  176. * Generally speaking, the only requests are handled
  177. * by the kernel, but RDMA_NL_LS is different, because it
  178. * runs backward netlink scheme. Kernel initiates messages
  179. * and waits for reply with data to keep pathrecord cache
  180. * in sync.
  181. */
  182. if (!(nlh->nlmsg_flags & NLM_F_REQUEST) &&
  183. (RDMA_NL_GET_CLIENT(nlh->nlmsg_type) != RDMA_NL_LS))
  184. goto ack;
  185. /* Skip control messages */
  186. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  187. goto ack;
  188. err = cb(skb, nlh, &extack);
  189. if (err == -EINTR)
  190. goto skip;
  191. ack:
  192. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  193. netlink_ack(skb, nlh, err, &extack);
  194. skip:
  195. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  196. if (msglen > skb->len)
  197. msglen = skb->len;
  198. skb_pull(skb, msglen);
  199. }
  200. return 0;
  201. }
  202. static void rdma_nl_rcv(struct sk_buff *skb)
  203. {
  204. mutex_lock(&rdma_nl_mutex);
  205. rdma_nl_rcv_skb(skb, &rdma_nl_rcv_msg);
  206. mutex_unlock(&rdma_nl_mutex);
  207. }
  208. int rdma_nl_unicast(struct sk_buff *skb, u32 pid)
  209. {
  210. int err;
  211. err = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
  212. return (err < 0) ? err : 0;
  213. }
  214. EXPORT_SYMBOL(rdma_nl_unicast);
  215. int rdma_nl_unicast_wait(struct sk_buff *skb, __u32 pid)
  216. {
  217. int err;
  218. err = netlink_unicast(nls, skb, pid, 0);
  219. return (err < 0) ? err : 0;
  220. }
  221. EXPORT_SYMBOL(rdma_nl_unicast_wait);
  222. int rdma_nl_multicast(struct sk_buff *skb, unsigned int group, gfp_t flags)
  223. {
  224. return nlmsg_multicast(nls, skb, 0, group, flags);
  225. }
  226. EXPORT_SYMBOL(rdma_nl_multicast);
  227. int __init rdma_nl_init(void)
  228. {
  229. struct netlink_kernel_cfg cfg = {
  230. .input = rdma_nl_rcv,
  231. };
  232. nls = netlink_kernel_create(&init_net, NETLINK_RDMA, &cfg);
  233. if (!nls)
  234. return -ENOMEM;
  235. nls->sk_sndtimeo = 10 * HZ;
  236. return 0;
  237. }
  238. void rdma_nl_exit(void)
  239. {
  240. int idx;
  241. for (idx = 0; idx < RDMA_NL_NUM_CLIENTS; idx++)
  242. rdma_nl_unregister(idx);
  243. netlink_kernel_release(nls);
  244. }