connector.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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)
  57. {
  58. struct cn_callback_entry *__cbq;
  59. unsigned int size;
  60. struct sk_buff *skb;
  61. struct nlmsghdr *nlh;
  62. struct cn_msg *data;
  63. struct cn_dev *dev = &cdev;
  64. u32 group = 0;
  65. int found = 0;
  66. if (portid || __group) {
  67. group = __group;
  68. } else {
  69. spin_lock_bh(&dev->cbdev->queue_lock);
  70. list_for_each_entry(__cbq, &dev->cbdev->queue_list,
  71. callback_entry) {
  72. if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
  73. found = 1;
  74. group = __cbq->group;
  75. break;
  76. }
  77. }
  78. spin_unlock_bh(&dev->cbdev->queue_lock);
  79. if (!found)
  80. return -ENODEV;
  81. }
  82. if (!portid && !netlink_has_listeners(dev->nls, group))
  83. return -ESRCH;
  84. size = sizeof(*msg) + len;
  85. skb = nlmsg_new(size, gfp_mask);
  86. if (!skb)
  87. return -ENOMEM;
  88. nlh = nlmsg_put(skb, 0, msg->seq, NLMSG_DONE, size, 0);
  89. if (!nlh) {
  90. kfree_skb(skb);
  91. return -EMSGSIZE;
  92. }
  93. data = nlmsg_data(nlh);
  94. memcpy(data, msg, size);
  95. NETLINK_CB(skb).dst_group = group;
  96. if (group)
  97. return netlink_broadcast(dev->nls, skb, portid, group,
  98. gfp_mask);
  99. return netlink_unicast(dev->nls, skb, portid,
  100. !gfpflags_allow_blocking(gfp_mask));
  101. }
  102. EXPORT_SYMBOL_GPL(cn_netlink_send_mult);
  103. /* same as cn_netlink_send_mult except msg->len is used for len */
  104. int cn_netlink_send(struct cn_msg *msg, u32 portid, u32 __group,
  105. gfp_t gfp_mask)
  106. {
  107. return cn_netlink_send_mult(msg, msg->len, portid, __group, gfp_mask);
  108. }
  109. EXPORT_SYMBOL_GPL(cn_netlink_send);
  110. /*
  111. * Callback helper - queues work and setup destructor for given data.
  112. */
  113. static int cn_call_callback(struct sk_buff *skb)
  114. {
  115. struct nlmsghdr *nlh;
  116. struct cn_callback_entry *i, *cbq = NULL;
  117. struct cn_dev *dev = &cdev;
  118. struct cn_msg *msg = nlmsg_data(nlmsg_hdr(skb));
  119. struct netlink_skb_parms *nsp = &NETLINK_CB(skb);
  120. int err = -ENODEV;
  121. /* verify msg->len is within skb */
  122. nlh = nlmsg_hdr(skb);
  123. if (nlh->nlmsg_len < NLMSG_HDRLEN + sizeof(struct cn_msg) + msg->len)
  124. return -EINVAL;
  125. spin_lock_bh(&dev->cbdev->queue_lock);
  126. list_for_each_entry(i, &dev->cbdev->queue_list, callback_entry) {
  127. if (cn_cb_equal(&i->id.id, &msg->id)) {
  128. refcount_inc(&i->refcnt);
  129. cbq = i;
  130. break;
  131. }
  132. }
  133. spin_unlock_bh(&dev->cbdev->queue_lock);
  134. if (cbq != NULL) {
  135. cbq->callback(msg, nsp);
  136. kfree_skb(skb);
  137. cn_queue_release_callback(cbq);
  138. err = 0;
  139. }
  140. return err;
  141. }
  142. /*
  143. * Main netlink receiving function.
  144. *
  145. * It checks skb, netlink header and msg sizes, and calls callback helper.
  146. */
  147. static void cn_rx_skb(struct sk_buff *skb)
  148. {
  149. struct nlmsghdr *nlh;
  150. int len, err;
  151. if (skb->len >= NLMSG_HDRLEN) {
  152. nlh = nlmsg_hdr(skb);
  153. len = nlmsg_len(nlh);
  154. if (len < (int)sizeof(struct cn_msg) ||
  155. skb->len < nlh->nlmsg_len ||
  156. len > CONNECTOR_MAX_MSG_SIZE)
  157. return;
  158. err = cn_call_callback(skb_get(skb));
  159. if (err < 0)
  160. kfree_skb(skb);
  161. }
  162. }
  163. /*
  164. * Callback add routing - adds callback with given ID and name.
  165. * If there is registered callback with the same ID it will not be added.
  166. *
  167. * May sleep.
  168. */
  169. int cn_add_callback(struct cb_id *id, const char *name,
  170. void (*callback)(struct cn_msg *,
  171. struct netlink_skb_parms *))
  172. {
  173. int err;
  174. struct cn_dev *dev = &cdev;
  175. if (!cn_already_initialized)
  176. return -EAGAIN;
  177. err = cn_queue_add_callback(dev->cbdev, name, id, callback);
  178. if (err)
  179. return err;
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(cn_add_callback);
  183. /*
  184. * Callback remove routing - removes callback
  185. * with given ID.
  186. * If there is no registered callback with given
  187. * ID nothing happens.
  188. *
  189. * May sleep while waiting for reference counter to become zero.
  190. */
  191. void cn_del_callback(struct cb_id *id)
  192. {
  193. struct cn_dev *dev = &cdev;
  194. cn_queue_del_callback(dev->cbdev, id);
  195. }
  196. EXPORT_SYMBOL_GPL(cn_del_callback);
  197. static int __maybe_unused cn_proc_show(struct seq_file *m, void *v)
  198. {
  199. struct cn_queue_dev *dev = cdev.cbdev;
  200. struct cn_callback_entry *cbq;
  201. seq_printf(m, "Name ID\n");
  202. spin_lock_bh(&dev->queue_lock);
  203. list_for_each_entry(cbq, &dev->queue_list, callback_entry) {
  204. seq_printf(m, "%-15s %u:%u\n",
  205. cbq->id.name,
  206. cbq->id.id.idx,
  207. cbq->id.id.val);
  208. }
  209. spin_unlock_bh(&dev->queue_lock);
  210. return 0;
  211. }
  212. static int cn_init(void)
  213. {
  214. struct cn_dev *dev = &cdev;
  215. struct netlink_kernel_cfg cfg = {
  216. .groups = CN_NETLINK_USERS + 0xf,
  217. .input = cn_rx_skb,
  218. };
  219. dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg);
  220. if (!dev->nls)
  221. return -EIO;
  222. dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
  223. if (!dev->cbdev) {
  224. netlink_kernel_release(dev->nls);
  225. return -EINVAL;
  226. }
  227. cn_already_initialized = 1;
  228. proc_create_single("connector", S_IRUGO, init_net.proc_net, cn_proc_show);
  229. return 0;
  230. }
  231. static void cn_fini(void)
  232. {
  233. struct cn_dev *dev = &cdev;
  234. cn_already_initialized = 0;
  235. remove_proc_entry("connector", init_net.proc_net);
  236. cn_queue_free_dev(dev->cbdev);
  237. netlink_kernel_release(dev->nls);
  238. }
  239. subsys_initcall(cn_init);
  240. module_exit(cn_fini);