connector.c 7.4 KB

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