netlink.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2017 Mellanox Technologies Inc. All rights reserved.
  3. * Copyright (c) 2010 Voltaire Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
  34. #include <linux/export.h>
  35. #include <net/netlink.h>
  36. #include <net/net_namespace.h>
  37. #include <net/sock.h>
  38. #include <rdma/rdma_netlink.h>
  39. #include <linux/module.h>
  40. #include "core_priv.h"
  41. #include "core_priv.h"
  42. static DEFINE_MUTEX(rdma_nl_mutex);
  43. static struct sock *nls;
  44. static struct {
  45. const struct rdma_nl_cbs *cb_table;
  46. } rdma_nl_types[RDMA_NL_NUM_CLIENTS];
  47. int rdma_nl_chk_listeners(unsigned int group)
  48. {
  49. return (netlink_has_listeners(nls, group)) ? 0 : -1;
  50. }
  51. EXPORT_SYMBOL(rdma_nl_chk_listeners);
  52. static bool is_nl_msg_valid(unsigned int type, unsigned int op)
  53. {
  54. static const unsigned int max_num_ops[RDMA_NL_NUM_CLIENTS] = {
  55. [RDMA_NL_RDMA_CM] = RDMA_NL_RDMA_CM_NUM_OPS,
  56. [RDMA_NL_IWCM] = RDMA_NL_IWPM_NUM_OPS,
  57. [RDMA_NL_LS] = RDMA_NL_LS_NUM_OPS,
  58. [RDMA_NL_NLDEV] = RDMA_NLDEV_NUM_OPS,
  59. };
  60. /*
  61. * This BUILD_BUG_ON is intended to catch addition of new
  62. * RDMA netlink protocol without updating the array above.
  63. */
  64. BUILD_BUG_ON(RDMA_NL_NUM_CLIENTS != 6);
  65. if (type >= RDMA_NL_NUM_CLIENTS)
  66. return false;
  67. return (op < max_num_ops[type]) ? true : false;
  68. }
  69. static bool is_nl_valid(unsigned int type, unsigned int op)
  70. {
  71. const struct rdma_nl_cbs *cb_table;
  72. if (!is_nl_msg_valid(type, op))
  73. return false;
  74. cb_table = rdma_nl_types[type].cb_table;
  75. #ifdef CONFIG_MODULES
  76. if (!cb_table) {
  77. mutex_unlock(&rdma_nl_mutex);
  78. request_module("rdma-netlink-subsys-%d", type);
  79. mutex_lock(&rdma_nl_mutex);
  80. cb_table = rdma_nl_types[type].cb_table;
  81. }
  82. #endif
  83. if (!cb_table || (!cb_table[op].dump && !cb_table[op].doit))
  84. return false;
  85. return true;
  86. }
  87. void rdma_nl_register(unsigned int index,
  88. const struct rdma_nl_cbs cb_table[])
  89. {
  90. mutex_lock(&rdma_nl_mutex);
  91. if (!is_nl_msg_valid(index, 0)) {
  92. /*
  93. * All clients are not interesting in success/failure of
  94. * this call. They want to see the print to error log and
  95. * continue their initialization. Print warning for them,
  96. * because it is programmer's error to be here.
  97. */
  98. mutex_unlock(&rdma_nl_mutex);
  99. WARN(true,
  100. "The not-valid %u index was supplied to RDMA netlink\n",
  101. index);
  102. return;
  103. }
  104. if (rdma_nl_types[index].cb_table) {
  105. mutex_unlock(&rdma_nl_mutex);
  106. WARN(true,
  107. "The %u index is already registered in RDMA netlink\n",
  108. index);
  109. return;
  110. }
  111. rdma_nl_types[index].cb_table = cb_table;
  112. mutex_unlock(&rdma_nl_mutex);
  113. }
  114. EXPORT_SYMBOL(rdma_nl_register);
  115. void rdma_nl_unregister(unsigned int index)
  116. {
  117. mutex_lock(&rdma_nl_mutex);
  118. rdma_nl_types[index].cb_table = NULL;
  119. mutex_unlock(&rdma_nl_mutex);
  120. }
  121. EXPORT_SYMBOL(rdma_nl_unregister);
  122. void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
  123. int len, int client, int op, int flags)
  124. {
  125. *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op), len, flags);
  126. if (!*nlh)
  127. return NULL;
  128. return nlmsg_data(*nlh);
  129. }
  130. EXPORT_SYMBOL(ibnl_put_msg);
  131. int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
  132. int len, void *data, int type)
  133. {
  134. if (nla_put(skb, type, len, data)) {
  135. nlmsg_cancel(skb, nlh);
  136. return -EMSGSIZE;
  137. }
  138. return 0;
  139. }
  140. EXPORT_SYMBOL(ibnl_put_attr);
  141. static int rdma_nl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  142. struct netlink_ext_ack *extack)
  143. {
  144. int type = nlh->nlmsg_type;
  145. unsigned int index = RDMA_NL_GET_CLIENT(type);
  146. unsigned int op = RDMA_NL_GET_OP(type);
  147. const struct rdma_nl_cbs *cb_table;
  148. if (!is_nl_valid(index, op))
  149. return -EINVAL;
  150. cb_table = rdma_nl_types[index].cb_table;
  151. if ((cb_table[op].flags & RDMA_NL_ADMIN_PERM) &&
  152. !netlink_capable(skb, CAP_NET_ADMIN))
  153. return -EPERM;
  154. /*
  155. * LS responses overload the 0x100 (NLM_F_ROOT) flag. Don't
  156. * mistakenly call the .dump() function.
  157. */
  158. if (index == RDMA_NL_LS) {
  159. if (cb_table[op].doit)
  160. return cb_table[op].doit(skb, nlh, extack);
  161. return -EINVAL;
  162. }
  163. /* FIXME: Convert IWCM to properly handle doit callbacks */
  164. if ((nlh->nlmsg_flags & NLM_F_DUMP) || index == RDMA_NL_RDMA_CM ||
  165. index == RDMA_NL_IWCM) {
  166. struct netlink_dump_control c = {
  167. .dump = cb_table[op].dump,
  168. };
  169. if (c.dump)
  170. return netlink_dump_start(nls, skb, nlh, &c);
  171. return -EINVAL;
  172. }
  173. if (cb_table[op].doit)
  174. return cb_table[op].doit(skb, nlh, extack);
  175. return 0;
  176. }
  177. /*
  178. * This function is similar to netlink_rcv_skb with one exception:
  179. * It calls to the callback for the netlink messages without NLM_F_REQUEST
  180. * flag. These messages are intended for RDMA_NL_LS consumer, so it is allowed
  181. * for that consumer only.
  182. */
  183. static int rdma_nl_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  184. struct nlmsghdr *,
  185. struct netlink_ext_ack *))
  186. {
  187. struct netlink_ext_ack extack = {};
  188. struct nlmsghdr *nlh;
  189. int err;
  190. while (skb->len >= nlmsg_total_size(0)) {
  191. int msglen;
  192. nlh = nlmsg_hdr(skb);
  193. err = 0;
  194. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  195. return 0;
  196. /*
  197. * Generally speaking, the only requests are handled
  198. * by the kernel, but RDMA_NL_LS is different, because it
  199. * runs backward netlink scheme. Kernel initiates messages
  200. * and waits for reply with data to keep pathrecord cache
  201. * in sync.
  202. */
  203. if (!(nlh->nlmsg_flags & NLM_F_REQUEST) &&
  204. (RDMA_NL_GET_CLIENT(nlh->nlmsg_type) != RDMA_NL_LS))
  205. goto ack;
  206. /* Skip control messages */
  207. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  208. goto ack;
  209. err = cb(skb, nlh, &extack);
  210. if (err == -EINTR)
  211. goto skip;
  212. ack:
  213. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  214. netlink_ack(skb, nlh, err, &extack);
  215. skip:
  216. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  217. if (msglen > skb->len)
  218. msglen = skb->len;
  219. skb_pull(skb, msglen);
  220. }
  221. return 0;
  222. }
  223. static void rdma_nl_rcv(struct sk_buff *skb)
  224. {
  225. mutex_lock(&rdma_nl_mutex);
  226. rdma_nl_rcv_skb(skb, &rdma_nl_rcv_msg);
  227. mutex_unlock(&rdma_nl_mutex);
  228. }
  229. int rdma_nl_unicast(struct sk_buff *skb, u32 pid)
  230. {
  231. int err;
  232. err = netlink_unicast(nls, skb, pid, MSG_DONTWAIT);
  233. return (err < 0) ? err : 0;
  234. }
  235. EXPORT_SYMBOL(rdma_nl_unicast);
  236. int rdma_nl_unicast_wait(struct sk_buff *skb, __u32 pid)
  237. {
  238. int err;
  239. err = netlink_unicast(nls, skb, pid, 0);
  240. return (err < 0) ? err : 0;
  241. }
  242. EXPORT_SYMBOL(rdma_nl_unicast_wait);
  243. int rdma_nl_multicast(struct sk_buff *skb, unsigned int group, gfp_t flags)
  244. {
  245. return nlmsg_multicast(nls, skb, 0, group, flags);
  246. }
  247. EXPORT_SYMBOL(rdma_nl_multicast);
  248. int __init rdma_nl_init(void)
  249. {
  250. struct netlink_kernel_cfg cfg = {
  251. .input = rdma_nl_rcv,
  252. };
  253. nls = netlink_kernel_create(&init_net, NETLINK_RDMA, &cfg);
  254. if (!nls)
  255. return -ENOMEM;
  256. nls->sk_sndtimeo = 10 * HZ;
  257. return 0;
  258. }
  259. void rdma_nl_exit(void)
  260. {
  261. int idx;
  262. for (idx = 0; idx < RDMA_NL_NUM_CLIENTS; idx++)
  263. rdma_nl_unregister(idx);
  264. netlink_kernel_release(nls);
  265. }
  266. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_RDMA);