connector.c 7.6 KB

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