netlink.c 8.0 KB

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