connector.c 7.5 KB

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