connector.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * connector.c
  4. *
  5. * 2004+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
  6. * All rights reserved.
  7. */
  8. #include <linux/compiler.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/list.h>
  12. #include <linux/skbuff.h>
  13. #include <net/netlink.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/connector.h>
  16. #include <linux/slab.h>
  17. #include <linux/mutex.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/spinlock.h>
  20. #include <net/sock.h>
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  23. MODULE_DESCRIPTION("Generic userspace <-> kernelspace connector.");
  24. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_CONNECTOR);
  25. static struct cn_dev cdev;
  26. static int cn_already_initialized;
  27. /*
  28. * Sends mult (multiple) cn_msg at a time.
  29. *
  30. * msg->seq and msg->ack are used to determine message genealogy.
  31. * When someone sends message it puts there locally unique sequence
  32. * and random acknowledge numbers. Sequence number may be copied into
  33. * nlmsghdr->nlmsg_seq too.
  34. *
  35. * Sequence number is incremented with each message to be sent.
  36. *
  37. * If we expect a reply to our message then the sequence number in
  38. * received message MUST be the same as in original message, and
  39. * acknowledge number MUST be the same + 1.
  40. *
  41. * If we receive a message and its sequence number is not equal to the
  42. * one we are expecting then it is a new message.
  43. *
  44. * If we receive a message and its sequence number is the same as one
  45. * we are expecting but it's acknowledgement number is not equal to
  46. * the acknowledgement number in the original message + 1, then it is
  47. * a new message.
  48. *
  49. * If msg->len != len, then additional cn_msg messages are expected following
  50. * the first msg.
  51. *
  52. * The message is sent to, the portid if given, the group if given, both if
  53. * both, or if both are zero then the group is looked up and sent there.
  54. */
  55. int cn_netlink_send_mult(struct cn_msg *msg, u16 len, u32 portid, u32 __group,
  56. gfp_t gfp_mask, netlink_filter_fn filter,
  57. void *filter_data)
  58. {
  59. struct cn_callback_entry *__cbq;
  60. unsigned int size;
  61. struct sk_buff *skb;
  62. struct nlmsghdr *nlh;
  63. struct cn_msg *data;
  64. struct cn_dev *dev = &cdev;
  65. u32 group = 0;
  66. int found = 0;
  67. if (portid || __group) {
  68. group = __group;
  69. } else {
  70. spin_lock_bh(&dev->cbdev->queue_lock);
  71. list_for_each_entry(__cbq, &dev->cbdev->queue_list,
  72. callback_entry) {
  73. if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
  74. found = 1;
  75. group = __cbq->group;
  76. break;
  77. }
  78. }
  79. spin_unlock_bh(&dev->cbdev->queue_lock);
  80. if (!found)
  81. return -ENODEV;
  82. }
  83. if (!portid && !netlink_has_listeners(dev->nls, group))
  84. return -ESRCH;
  85. size = sizeof(*msg) + len;
  86. skb = nlmsg_new(size, gfp_mask);
  87. if (!skb)
  88. return -ENOMEM;
  89. nlh = nlmsg_put(skb, 0, msg->seq, NLMSG_DONE, size, 0);
  90. if (!nlh) {
  91. kfree_skb(skb);
  92. return -EMSGSIZE;
  93. }
  94. data = nlmsg_data(nlh);
  95. memcpy(data, msg, size);
  96. NETLINK_CB(skb).dst_group = group;
  97. if (group)
  98. return netlink_broadcast_filtered(dev->nls, skb, portid, group,
  99. gfp_mask, filter,
  100. (void *)filter_data);
  101. return netlink_unicast(dev->nls, skb, portid,
  102. !gfpflags_allow_blocking(gfp_mask));
  103. }
  104. EXPORT_SYMBOL_GPL(cn_netlink_send_mult);
  105. /* same as cn_netlink_send_mult except msg->len is used for len */
  106. int cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group,
  107. gfp_t gfp_mask)
  108. {
  109. return cn_netlink_send_mult(msg, msg->len, portid, __group, gfp_mask,
  110. NULL, NULL);
  111. }
  112. EXPORT_SYMBOL_GPL(cn_netlink_send);
  113. /*
  114. * Callback helper - queues work and setup destructor for given data.
  115. */
  116. static int cn_call_callback(struct sk_buff *skb)
  117. {
  118. struct nlmsghdr *nlh;
  119. struct cn_callback_entry *i, *cbq = NULL;
  120. struct cn_dev *dev = &cdev;
  121. struct cn_msg *msg = nlmsg_data(nlmsg_hdr(skb));
  122. struct netlink_skb_parms *nsp = &NETLINK_CB(skb);
  123. int err = -ENODEV;
  124. /* verify msg->len is within skb */
  125. nlh = nlmsg_hdr(skb);
  126. if (nlh->nlmsg_len < NLMSG_HDRLEN + sizeof(struct cn_msg) + msg->len)
  127. return -EINVAL;
  128. spin_lock_bh(&dev->cbdev->queue_lock);
  129. list_for_each_entry(i, &dev->cbdev->queue_list, callback_entry) {
  130. if (cn_cb_equal(&i->id.id, &msg->id)) {
  131. refcount_inc(&i->refcnt);
  132. cbq = i;
  133. break;
  134. }
  135. }
  136. spin_unlock_bh(&dev->cbdev->queue_lock);
  137. if (cbq != NULL) {
  138. cbq->callback(msg, nsp);
  139. kfree_skb(skb);
  140. cn_queue_release_callback(cbq);
  141. err = 0;
  142. }
  143. return err;
  144. }
  145. /*
  146. * Allow non-root access for NETLINK_CONNECTOR family having CN_IDX_PROC
  147. * multicast group.
  148. */
  149. static int cn_bind(struct net *net, int group)
  150. {
  151. unsigned long groups = (unsigned long) group;
  152. if (ns_capable(net->user_ns, CAP_NET_ADMIN))
  153. return 0;
  154. if (test_bit(CN_IDX_PROC - 1, &groups))
  155. return 0;
  156. return -EPERM;
  157. }
  158. static void cn_release(struct sock *sk, unsigned long *groups)
  159. {
  160. if (groups && test_bit(CN_IDX_PROC - 1, groups)) {
  161. kfree(sk->sk_user_data);
  162. sk->sk_user_data = NULL;
  163. }
  164. }
  165. /*
  166. * Main netlink receiving function.
  167. *
  168. * It checks skb, netlink header and msg sizes, and calls callback helper.
  169. */
  170. static void cn_rx_skb(struct sk_buff *skb)
  171. {
  172. struct nlmsghdr *nlh;
  173. int len, err;
  174. if (skb->len >= NLMSG_HDRLEN) {
  175. nlh = nlmsg_hdr(skb);
  176. len = nlmsg_len(nlh);
  177. if (len < (int)sizeof(struct cn_msg) ||
  178. skb->len < nlh->nlmsg_len ||
  179. len > CONNECTOR_MAX_MSG_SIZE)
  180. return;
  181. err = cn_call_callback(skb_get(skb));
  182. if (err < 0)
  183. kfree_skb(skb);
  184. }
  185. }
  186. /*
  187. * Callback add routing - adds callback with given ID and name.
  188. * If there is registered callback with the same ID it will not be added.
  189. *
  190. * May sleep.
  191. */
  192. int cn_add_callback(const struct cb_id *id, const char *name,
  193. void (*callback)(struct cn_msg *,
  194. struct netlink_skb_parms *))
  195. {
  196. struct cn_dev *dev = &cdev;
  197. if (!cn_already_initialized)
  198. return -EAGAIN;
  199. return cn_queue_add_callback(dev->cbdev, name, id, callback);
  200. }
  201. EXPORT_SYMBOL_GPL(cn_add_callback);
  202. /*
  203. * Callback remove routing - removes callback
  204. * with given ID.
  205. * If there is no registered callback with given
  206. * ID nothing happens.
  207. *
  208. * May sleep while waiting for reference counter to become zero.
  209. */
  210. void cn_del_callback(const struct cb_id *id)
  211. {
  212. struct cn_dev *dev = &cdev;
  213. cn_queue_del_callback(dev->cbdev, id);
  214. }
  215. EXPORT_SYMBOL_GPL(cn_del_callback);
  216. static int __maybe_unused cn_proc_show(struct seq_file *m, void *v)
  217. {
  218. struct cn_queue_dev *dev = cdev.cbdev;
  219. struct cn_callback_entry *cbq;
  220. seq_printf(m, "Name ID\n");
  221. spin_lock_bh(&dev->queue_lock);
  222. list_for_each_entry(cbq, &dev->queue_list, callback_entry) {
  223. seq_printf(m, "%-15s %u:%u\n",
  224. cbq->id.name,
  225. cbq->id.id.idx,
  226. cbq->id.id.val);
  227. }
  228. spin_unlock_bh(&dev->queue_lock);
  229. return 0;
  230. }
  231. static int cn_init(void)
  232. {
  233. struct cn_dev *dev = &cdev;
  234. struct netlink_kernel_cfg cfg = {
  235. .groups = CN_NETLINK_USERS + 0xf,
  236. .input = cn_rx_skb,
  237. .flags = NL_CFG_F_NONROOT_RECV,
  238. .bind = cn_bind,
  239. .release = cn_release,
  240. };
  241. dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg);
  242. if (!dev->nls)
  243. return -EIO;
  244. dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
  245. if (!dev->cbdev) {
  246. netlink_kernel_release(dev->nls);
  247. return -EINVAL;
  248. }
  249. cn_already_initialized = 1;
  250. proc_create_single("connector", S_IRUGO, init_net.proc_net, cn_proc_show);
  251. return 0;
  252. }
  253. static void cn_fini(void)
  254. {
  255. struct cn_dev *dev = &cdev;
  256. cn_already_initialized = 0;
  257. remove_proc_entry("connector", init_net.proc_net);
  258. cn_queue_free_dev(dev->cbdev);
  259. netlink_kernel_release(dev->nls);
  260. }
  261. subsys_initcall(cn_init);
  262. module_exit(cn_fini);