netlink.c 8.0 KB

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