connector.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 nlmsghdr *nlh;
  127. struct cn_callback_entry *i, *cbq = NULL;
  128. struct cn_dev *dev = &cdev;
  129. struct cn_msg *msg = nlmsg_data(nlmsg_hdr(skb));
  130. struct netlink_skb_parms *nsp = &NETLINK_CB(skb);
  131. int err = -ENODEV;
  132. /* verify msg->len is within skb */
  133. nlh = nlmsg_hdr(skb);
  134. if (nlh->nlmsg_len < NLMSG_HDRLEN + sizeof(struct cn_msg) + msg->len)
  135. return -EINVAL;
  136. spin_lock_bh(&dev->cbdev->queue_lock);
  137. list_for_each_entry(i, &dev->cbdev->queue_list, callback_entry) {
  138. if (cn_cb_equal(&i->id.id, &msg->id)) {
  139. atomic_inc(&i->refcnt);
  140. cbq = i;
  141. break;
  142. }
  143. }
  144. spin_unlock_bh(&dev->cbdev->queue_lock);
  145. if (cbq != NULL) {
  146. cbq->callback(msg, nsp);
  147. kfree_skb(skb);
  148. cn_queue_release_callback(cbq);
  149. err = 0;
  150. }
  151. return err;
  152. }
  153. /*
  154. * Main netlink receiving function.
  155. *
  156. * It checks skb, netlink header and msg sizes, and calls callback helper.
  157. */
  158. static void cn_rx_skb(struct sk_buff *__skb)
  159. {
  160. struct nlmsghdr *nlh;
  161. struct sk_buff *skb;
  162. int len, err;
  163. skb = skb_get(__skb);
  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. kfree_skb(skb);
  171. return;
  172. }
  173. err = cn_call_callback(skb);
  174. if (err < 0)
  175. kfree_skb(skb);
  176. }
  177. }
  178. /*
  179. * Callback add routing - adds callback with given ID and name.
  180. * If there is registered callback with the same ID it will not be added.
  181. *
  182. * May sleep.
  183. */
  184. int cn_add_callback(struct cb_id *id, const char *name,
  185. void (*callback)(struct cn_msg *,
  186. struct netlink_skb_parms *))
  187. {
  188. int err;
  189. struct cn_dev *dev = &cdev;
  190. if (!cn_already_initialized)
  191. return -EAGAIN;
  192. err = cn_queue_add_callback(dev->cbdev, name, id, callback);
  193. if (err)
  194. return err;
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(cn_add_callback);
  198. /*
  199. * Callback remove routing - removes callback
  200. * with given ID.
  201. * If there is no registered callback with given
  202. * ID nothing happens.
  203. *
  204. * May sleep while waiting for reference counter to become zero.
  205. */
  206. void cn_del_callback(struct cb_id *id)
  207. {
  208. struct cn_dev *dev = &cdev;
  209. cn_queue_del_callback(dev->cbdev, id);
  210. }
  211. EXPORT_SYMBOL_GPL(cn_del_callback);
  212. static int cn_proc_show(struct seq_file *m, void *v)
  213. {
  214. struct cn_queue_dev *dev = cdev.cbdev;
  215. struct cn_callback_entry *cbq;
  216. seq_printf(m, "Name ID\n");
  217. spin_lock_bh(&dev->queue_lock);
  218. list_for_each_entry(cbq, &dev->queue_list, callback_entry) {
  219. seq_printf(m, "%-15s %u:%u\n",
  220. cbq->id.name,
  221. cbq->id.id.idx,
  222. cbq->id.id.val);
  223. }
  224. spin_unlock_bh(&dev->queue_lock);
  225. return 0;
  226. }
  227. static int cn_proc_open(struct inode *inode, struct file *file)
  228. {
  229. return single_open(file, cn_proc_show, NULL);
  230. }
  231. static const struct file_operations cn_file_ops = {
  232. .owner = THIS_MODULE,
  233. .open = cn_proc_open,
  234. .read = seq_read,
  235. .llseek = seq_lseek,
  236. .release = single_release
  237. };
  238. static struct cn_dev cdev = {
  239. .input = cn_rx_skb,
  240. };
  241. static int cn_init(void)
  242. {
  243. struct cn_dev *dev = &cdev;
  244. struct netlink_kernel_cfg cfg = {
  245. .groups = CN_NETLINK_USERS + 0xf,
  246. .input = dev->input,
  247. };
  248. dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, &cfg);
  249. if (!dev->nls)
  250. return -EIO;
  251. dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
  252. if (!dev->cbdev) {
  253. netlink_kernel_release(dev->nls);
  254. return -EINVAL;
  255. }
  256. cn_already_initialized = 1;
  257. proc_create("connector", S_IRUGO, init_net.proc_net, &cn_file_ops);
  258. return 0;
  259. }
  260. static void cn_fini(void)
  261. {
  262. struct cn_dev *dev = &cdev;
  263. cn_already_initialized = 0;
  264. remove_proc_entry("connector", init_net.proc_net);
  265. cn_queue_free_dev(dev->cbdev);
  266. netlink_kernel_release(dev->nls);
  267. }
  268. subsys_initcall(cn_init);
  269. module_exit(cn_fini);