netlink.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/netns/generic.h>
  38. #include <net/sock.h>
  39. #include <rdma/rdma_netlink.h>
  40. #include <linux/module.h>
  41. #include "core_priv.h"
  42. static struct {
  43. const struct rdma_nl_cbs *cb_table;
  44. /* Synchronizes between ongoing netlink commands and netlink client
  45. * unregistration.
  46. */
  47. struct rw_semaphore sem;
  48. } rdma_nl_types[RDMA_NL_NUM_CLIENTS];
  49. bool rdma_nl_chk_listeners(unsigned int group)
  50. {
  51. struct rdma_dev_net *rnet = rdma_net_to_dev_net(&init_net);
  52. return netlink_has_listeners(rnet->nl_sock, group);
  53. }
  54. EXPORT_SYMBOL(rdma_nl_chk_listeners);
  55. static bool is_nl_msg_valid(unsigned int type, unsigned int op)
  56. {
  57. static const unsigned int max_num_ops[RDMA_NL_NUM_CLIENTS] = {
  58. [RDMA_NL_IWCM] = RDMA_NL_IWPM_NUM_OPS,
  59. [RDMA_NL_LS] = RDMA_NL_LS_NUM_OPS,
  60. [RDMA_NL_NLDEV] = RDMA_NLDEV_NUM_OPS,
  61. };
  62. /*
  63. * This BUILD_BUG_ON is intended to catch addition of new
  64. * RDMA netlink protocol without updating the array above.
  65. */
  66. BUILD_BUG_ON(RDMA_NL_NUM_CLIENTS != 6);
  67. if (type >= RDMA_NL_NUM_CLIENTS)
  68. return false;
  69. return (op < max_num_ops[type]) ? true : false;
  70. }
  71. static const struct rdma_nl_cbs *
  72. get_cb_table(const struct sk_buff *skb, unsigned int type, unsigned int op)
  73. {
  74. const struct rdma_nl_cbs *cb_table;
  75. /*
  76. * Currently only NLDEV client is supporting netlink commands in
  77. * non init_net net namespace.
  78. */
  79. if (sock_net(skb->sk) != &init_net && type != RDMA_NL_NLDEV)
  80. return NULL;
  81. cb_table = READ_ONCE(rdma_nl_types[type].cb_table);
  82. if (!cb_table) {
  83. /*
  84. * Didn't get valid reference of the table, attempt module
  85. * load once.
  86. */
  87. up_read(&rdma_nl_types[type].sem);
  88. request_module("rdma-netlink-subsys-%d", type);
  89. down_read(&rdma_nl_types[type].sem);
  90. cb_table = READ_ONCE(rdma_nl_types[type].cb_table);
  91. }
  92. if (!cb_table || (!cb_table[op].dump && !cb_table[op].doit))
  93. return NULL;
  94. return cb_table;
  95. }
  96. void rdma_nl_register(unsigned int index,
  97. const struct rdma_nl_cbs cb_table[])
  98. {
  99. if (WARN_ON(!is_nl_msg_valid(index, 0)) ||
  100. WARN_ON(READ_ONCE(rdma_nl_types[index].cb_table)))
  101. return;
  102. /* Pairs with the READ_ONCE in is_nl_valid() */
  103. smp_store_release(&rdma_nl_types[index].cb_table, cb_table);
  104. }
  105. EXPORT_SYMBOL(rdma_nl_register);
  106. void rdma_nl_unregister(unsigned int index)
  107. {
  108. down_write(&rdma_nl_types[index].sem);
  109. rdma_nl_types[index].cb_table = NULL;
  110. up_write(&rdma_nl_types[index].sem);
  111. }
  112. EXPORT_SYMBOL(rdma_nl_unregister);
  113. void *ibnl_put_msg(struct sk_buff *skb, struct nlmsghdr **nlh, int seq,
  114. int len, int client, int op, int flags)
  115. {
  116. *nlh = nlmsg_put(skb, 0, seq, RDMA_NL_GET_TYPE(client, op), len, flags);
  117. if (!*nlh)
  118. return NULL;
  119. return nlmsg_data(*nlh);
  120. }
  121. EXPORT_SYMBOL(ibnl_put_msg);
  122. int ibnl_put_attr(struct sk_buff *skb, struct nlmsghdr *nlh,
  123. int len, void *data, int type)
  124. {
  125. if (nla_put(skb, type, len, data)) {
  126. nlmsg_cancel(skb, nlh);
  127. return -EMSGSIZE;
  128. }
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(ibnl_put_attr);
  132. static int rdma_nl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  133. struct netlink_ext_ack *extack)
  134. {
  135. int type = nlh->nlmsg_type;
  136. unsigned int index = RDMA_NL_GET_CLIENT(type);
  137. unsigned int op = RDMA_NL_GET_OP(type);
  138. const struct rdma_nl_cbs *cb_table;
  139. int err = -EINVAL;
  140. if (!is_nl_msg_valid(index, op))
  141. return -EINVAL;
  142. down_read(&rdma_nl_types[index].sem);
  143. cb_table = get_cb_table(skb, index, op);
  144. if (!cb_table)
  145. goto done;
  146. if ((cb_table[op].flags & RDMA_NL_ADMIN_PERM) &&
  147. !netlink_capable(skb, CAP_NET_ADMIN)) {
  148. err = -EPERM;
  149. goto done;
  150. }
  151. /*
  152. * LS responses overload the 0x100 (NLM_F_ROOT) flag. Don't
  153. * mistakenly call the .dump() function.
  154. */
  155. if (index == RDMA_NL_LS) {
  156. if (cb_table[op].doit)
  157. err = cb_table[op].doit(skb, nlh, extack);
  158. goto done;
  159. }
  160. /* FIXME: Convert IWCM to properly handle doit callbacks */
  161. if ((nlh->nlmsg_flags & NLM_F_DUMP) || index == RDMA_NL_IWCM) {
  162. struct netlink_dump_control c = {
  163. .dump = cb_table[op].dump,
  164. };
  165. if (c.dump)
  166. err = netlink_dump_start(skb->sk, skb, nlh, &c);
  167. goto done;
  168. }
  169. if (cb_table[op].doit)
  170. err = cb_table[op].doit(skb, nlh, extack);
  171. done:
  172. up_read(&rdma_nl_types[index].sem);
  173. return err;
  174. }
  175. /*
  176. * This function is similar to netlink_rcv_skb with one exception:
  177. * It calls to the callback for the netlink messages without NLM_F_REQUEST
  178. * flag. These messages are intended for RDMA_NL_LS consumer, so it is allowed
  179. * for that consumer only.
  180. */
  181. static int rdma_nl_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
  182. struct nlmsghdr *,
  183. struct netlink_ext_ack *))
  184. {
  185. struct netlink_ext_ack extack = {};
  186. struct nlmsghdr *nlh;
  187. int err;
  188. while (skb->len >= nlmsg_total_size(0)) {
  189. int msglen;
  190. nlh = nlmsg_hdr(skb);
  191. err = 0;
  192. if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
  193. return 0;
  194. /*
  195. * Generally speaking, the only requests are handled
  196. * by the kernel, but RDMA_NL_LS is different, because it
  197. * runs backward netlink scheme. Kernel initiates messages
  198. * and waits for reply with data to keep pathrecord cache
  199. * in sync.
  200. */
  201. if (!(nlh->nlmsg_flags & NLM_F_REQUEST) &&
  202. (RDMA_NL_GET_CLIENT(nlh->nlmsg_type) != RDMA_NL_LS))
  203. goto ack;
  204. /* Skip control messages */
  205. if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
  206. goto ack;
  207. err = cb(skb, nlh, &extack);
  208. if (err == -EINTR)
  209. goto skip;
  210. ack:
  211. if (nlh->nlmsg_flags & NLM_F_ACK || err)
  212. netlink_ack(skb, nlh, err, &extack);
  213. skip:
  214. msglen = NLMSG_ALIGN(nlh->nlmsg_len);
  215. if (msglen > skb->len)
  216. msglen = skb->len;
  217. skb_pull(skb, msglen);
  218. }
  219. return 0;
  220. }
  221. static void rdma_nl_rcv(struct sk_buff *skb)
  222. {
  223. rdma_nl_rcv_skb(skb, &rdma_nl_rcv_msg);
  224. }
  225. int rdma_nl_unicast(struct net *net, struct sk_buff *skb, u32 pid)
  226. {
  227. struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
  228. int err;
  229. err = netlink_unicast(rnet->nl_sock, skb, pid, MSG_DONTWAIT);
  230. return (err < 0) ? err : 0;
  231. }
  232. EXPORT_SYMBOL(rdma_nl_unicast);
  233. int rdma_nl_unicast_wait(struct net *net, struct sk_buff *skb, __u32 pid)
  234. {
  235. struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
  236. int err;
  237. err = netlink_unicast(rnet->nl_sock, skb, pid, 0);
  238. return (err < 0) ? err : 0;
  239. }
  240. EXPORT_SYMBOL(rdma_nl_unicast_wait);
  241. int rdma_nl_multicast(struct net *net, struct sk_buff *skb,
  242. unsigned int group, gfp_t flags)
  243. {
  244. struct rdma_dev_net *rnet = rdma_net_to_dev_net(net);
  245. return nlmsg_multicast(rnet->nl_sock, skb, 0, group, flags);
  246. }
  247. EXPORT_SYMBOL(rdma_nl_multicast);
  248. void rdma_nl_init(void)
  249. {
  250. int idx;
  251. for (idx = 0; idx < RDMA_NL_NUM_CLIENTS; idx++)
  252. init_rwsem(&rdma_nl_types[idx].sem);
  253. }
  254. void rdma_nl_exit(void)
  255. {
  256. int idx;
  257. for (idx = 0; idx < RDMA_NL_NUM_CLIENTS; idx++)
  258. WARN(rdma_nl_types[idx].cb_table,
  259. "Netlink client %d wasn't released prior to unloading %s\n",
  260. idx, KBUILD_MODNAME);
  261. }
  262. int rdma_nl_net_init(struct rdma_dev_net *rnet)
  263. {
  264. struct net *net = read_pnet(&rnet->net);
  265. struct netlink_kernel_cfg cfg = {
  266. .input = rdma_nl_rcv,
  267. };
  268. struct sock *nls;
  269. nls = netlink_kernel_create(net, NETLINK_RDMA, &cfg);
  270. if (!nls)
  271. return -ENOMEM;
  272. nls->sk_sndtimeo = 10 * HZ;
  273. rnet->nl_sock = nls;
  274. return 0;
  275. }
  276. void rdma_nl_net_exit(struct rdma_dev_net *rnet)
  277. {
  278. netlink_kernel_release(rnet->nl_sock);
  279. }
  280. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_RDMA);