connector.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 <linux/netlink.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/connector.h>
  28. #include <linux/mutex.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/spinlock.h>
  31. #include <net/sock.h>
  32. MODULE_LICENSE("GPL");
  33. MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
  34. MODULE_DESCRIPTION("Generic userspace <-> kernelspace connector.");
  35. static u32 cn_idx = CN_IDX_CONNECTOR;
  36. static u32 cn_val = CN_VAL_CONNECTOR;
  37. module_param(cn_idx, uint, 0);
  38. module_param(cn_val, uint, 0);
  39. MODULE_PARM_DESC(cn_idx, "Connector's main device idx.");
  40. MODULE_PARM_DESC(cn_val, "Connector's main device val.");
  41. static DEFINE_MUTEX(notify_lock);
  42. static LIST_HEAD(notify_list);
  43. static struct cn_dev cdev;
  44. static int cn_already_initialized;
  45. /*
  46. * msg->seq and msg->ack are used to determine message genealogy.
  47. * When someone sends message it puts there locally unique sequence
  48. * and random acknowledge numbers. Sequence number may be copied into
  49. * nlmsghdr->nlmsg_seq too.
  50. *
  51. * Sequence number is incremented with each message to be sent.
  52. *
  53. * If we expect reply to our message then the sequence number in
  54. * received message MUST be the same as in original message, and
  55. * acknowledge number MUST be the same + 1.
  56. *
  57. * If we receive a message and its sequence number is not equal to the
  58. * one we are expecting then it is a new message.
  59. *
  60. * If we receive a message and its sequence number is the same as one
  61. * we are expecting but it's acknowledgement number is not equal to
  62. * the acknowledgement number in the original message + 1, then it is
  63. * a new message.
  64. *
  65. */
  66. int cn_netlink_send(struct cn_msg *msg, u32 __group, gfp_t gfp_mask)
  67. {
  68. struct cn_callback_entry *__cbq;
  69. unsigned int size;
  70. struct sk_buff *skb;
  71. struct nlmsghdr *nlh;
  72. struct cn_msg *data;
  73. struct cn_dev *dev = &cdev;
  74. u32 group = 0;
  75. int found = 0;
  76. if (!__group) {
  77. spin_lock_bh(&dev->cbdev->queue_lock);
  78. list_for_each_entry(__cbq, &dev->cbdev->queue_list,
  79. callback_entry) {
  80. if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
  81. found = 1;
  82. group = __cbq->group;
  83. break;
  84. }
  85. }
  86. spin_unlock_bh(&dev->cbdev->queue_lock);
  87. if (!found)
  88. return -ENODEV;
  89. } else {
  90. group = __group;
  91. }
  92. if (!netlink_has_listeners(dev->nls, group))
  93. return -ESRCH;
  94. size = NLMSG_SPACE(sizeof(*msg) + msg->len);
  95. skb = alloc_skb(size, gfp_mask);
  96. if (!skb)
  97. return -ENOMEM;
  98. nlh = NLMSG_PUT(skb, 0, msg->seq, NLMSG_DONE, size - sizeof(*nlh));
  99. data = NLMSG_DATA(nlh);
  100. memcpy(data, msg, sizeof(*data) + msg->len);
  101. NETLINK_CB(skb).dst_group = group;
  102. return netlink_broadcast(dev->nls, skb, 0, group, gfp_mask);
  103. nlmsg_failure:
  104. kfree_skb(skb);
  105. return -EINVAL;
  106. }
  107. EXPORT_SYMBOL_GPL(cn_netlink_send);
  108. /*
  109. * Callback helper - queues work and setup destructor for given data.
  110. */
  111. static int cn_call_callback(struct cn_msg *msg, void (*destruct_data)(void *), void *data)
  112. {
  113. struct cn_callback_entry *__cbq, *__new_cbq;
  114. struct cn_dev *dev = &cdev;
  115. int err = -ENODEV;
  116. spin_lock_bh(&dev->cbdev->queue_lock);
  117. list_for_each_entry(__cbq, &dev->cbdev->queue_list, callback_entry) {
  118. if (cn_cb_equal(&__cbq->id.id, &msg->id)) {
  119. if (likely(!work_pending(&__cbq->work) &&
  120. __cbq->data.ddata == NULL)) {
  121. __cbq->data.callback_priv = msg;
  122. __cbq->data.ddata = data;
  123. __cbq->data.destruct_data = destruct_data;
  124. if (queue_cn_work(__cbq, &__cbq->work))
  125. err = 0;
  126. else
  127. err = -EINVAL;
  128. } else {
  129. struct cn_callback_data *d;
  130. err = -ENOMEM;
  131. __new_cbq = kzalloc(sizeof(struct cn_callback_entry), GFP_ATOMIC);
  132. if (__new_cbq) {
  133. d = &__new_cbq->data;
  134. d->callback_priv = msg;
  135. d->callback = __cbq->data.callback;
  136. d->ddata = data;
  137. d->destruct_data = destruct_data;
  138. d->free = __new_cbq;
  139. __new_cbq->pdev = __cbq->pdev;
  140. INIT_WORK(&__new_cbq->work,
  141. &cn_queue_wrapper);
  142. if (queue_cn_work(__new_cbq,
  143. &__new_cbq->work))
  144. err = 0;
  145. else {
  146. kfree(__new_cbq);
  147. err = -EINVAL;
  148. }
  149. }
  150. }
  151. break;
  152. }
  153. }
  154. spin_unlock_bh(&dev->cbdev->queue_lock);
  155. return err;
  156. }
  157. /*
  158. * Main netlink receiving function.
  159. *
  160. * It checks skb, netlink header and msg sizes, and calls callback helper.
  161. */
  162. static void cn_rx_skb(struct sk_buff *__skb)
  163. {
  164. struct cn_msg *msg;
  165. struct nlmsghdr *nlh;
  166. int err;
  167. struct sk_buff *skb;
  168. skb = skb_get(__skb);
  169. if (skb->len >= NLMSG_SPACE(0)) {
  170. nlh = nlmsg_hdr(skb);
  171. if (nlh->nlmsg_len < sizeof(struct cn_msg) ||
  172. skb->len < nlh->nlmsg_len ||
  173. nlh->nlmsg_len > CONNECTOR_MAX_MSG_SIZE) {
  174. kfree_skb(skb);
  175. return;
  176. }
  177. msg = NLMSG_DATA(nlh);
  178. err = cn_call_callback(msg, (void (*)(void *))kfree_skb, skb);
  179. if (err < 0)
  180. kfree_skb(skb);
  181. }
  182. }
  183. /*
  184. * Notification routing.
  185. *
  186. * Gets id and checks if there are notification request for it's idx
  187. * and val. If there are such requests notify the listeners with the
  188. * given notify event.
  189. *
  190. */
  191. static void cn_notify(struct cb_id *id, u32 notify_event)
  192. {
  193. struct cn_ctl_entry *ent;
  194. mutex_lock(&notify_lock);
  195. list_for_each_entry(ent, &notify_list, notify_entry) {
  196. int i;
  197. struct cn_notify_req *req;
  198. struct cn_ctl_msg *ctl = ent->msg;
  199. int idx_found, val_found;
  200. idx_found = val_found = 0;
  201. req = (struct cn_notify_req *)ctl->data;
  202. for (i = 0; i < ctl->idx_notify_num; ++i, ++req) {
  203. if (id->idx >= req->first &&
  204. id->idx < req->first + req->range) {
  205. idx_found = 1;
  206. break;
  207. }
  208. }
  209. for (i = 0; i < ctl->val_notify_num; ++i, ++req) {
  210. if (id->val >= req->first &&
  211. id->val < req->first + req->range) {
  212. val_found = 1;
  213. break;
  214. }
  215. }
  216. if (idx_found && val_found) {
  217. struct cn_msg m = { .ack = notify_event, };
  218. memcpy(&m.id, id, sizeof(m.id));
  219. cn_netlink_send(&m, ctl->group, GFP_KERNEL);
  220. }
  221. }
  222. mutex_unlock(&notify_lock);
  223. }
  224. /*
  225. * Callback add routing - adds callback with given ID and name.
  226. * If there is registered callback with the same ID it will not be added.
  227. *
  228. * May sleep.
  229. */
  230. int cn_add_callback(struct cb_id *id, char *name, void (*callback)(void *))
  231. {
  232. int err;
  233. struct cn_dev *dev = &cdev;
  234. if (!cn_already_initialized)
  235. return -EAGAIN;
  236. err = cn_queue_add_callback(dev->cbdev, name, id, callback);
  237. if (err)
  238. return err;
  239. cn_notify(id, 0);
  240. return 0;
  241. }
  242. EXPORT_SYMBOL_GPL(cn_add_callback);
  243. /*
  244. * Callback remove routing - removes callback
  245. * with given ID.
  246. * If there is no registered callback with given
  247. * ID nothing happens.
  248. *
  249. * May sleep while waiting for reference counter to become zero.
  250. */
  251. void cn_del_callback(struct cb_id *id)
  252. {
  253. struct cn_dev *dev = &cdev;
  254. cn_queue_del_callback(dev->cbdev, id);
  255. cn_notify(id, 1);
  256. }
  257. EXPORT_SYMBOL_GPL(cn_del_callback);
  258. /*
  259. * Checks two connector's control messages to be the same.
  260. * Returns 1 if they are the same or if the first one is corrupted.
  261. */
  262. static int cn_ctl_msg_equals(struct cn_ctl_msg *m1, struct cn_ctl_msg *m2)
  263. {
  264. int i;
  265. struct cn_notify_req *req1, *req2;
  266. if (m1->idx_notify_num != m2->idx_notify_num)
  267. return 0;
  268. if (m1->val_notify_num != m2->val_notify_num)
  269. return 0;
  270. if (m1->len != m2->len)
  271. return 0;
  272. if ((m1->idx_notify_num + m1->val_notify_num) * sizeof(*req1) !=
  273. m1->len)
  274. return 1;
  275. req1 = (struct cn_notify_req *)m1->data;
  276. req2 = (struct cn_notify_req *)m2->data;
  277. for (i = 0; i < m1->idx_notify_num; ++i) {
  278. if (req1->first != req2->first || req1->range != req2->range)
  279. return 0;
  280. req1++;
  281. req2++;
  282. }
  283. for (i = 0; i < m1->val_notify_num; ++i) {
  284. if (req1->first != req2->first || req1->range != req2->range)
  285. return 0;
  286. req1++;
  287. req2++;
  288. }
  289. return 1;
  290. }
  291. /*
  292. * Main connector device's callback.
  293. *
  294. * Used for notification of a request's processing.
  295. */
  296. static void cn_callback(void *data)
  297. {
  298. struct cn_msg *msg = data;
  299. struct cn_ctl_msg *ctl;
  300. struct cn_ctl_entry *ent;
  301. u32 size;
  302. if (msg->len < sizeof(*ctl))
  303. return;
  304. ctl = (struct cn_ctl_msg *)msg->data;
  305. size = (sizeof(*ctl) + ((ctl->idx_notify_num +
  306. ctl->val_notify_num) *
  307. sizeof(struct cn_notify_req)));
  308. if (msg->len != size)
  309. return;
  310. if (ctl->len + sizeof(*ctl) != msg->len)
  311. return;
  312. /*
  313. * Remove notification.
  314. */
  315. if (ctl->group == 0) {
  316. struct cn_ctl_entry *n;
  317. mutex_lock(&notify_lock);
  318. list_for_each_entry_safe(ent, n, &notify_list, notify_entry) {
  319. if (cn_ctl_msg_equals(ent->msg, ctl)) {
  320. list_del(&ent->notify_entry);
  321. kfree(ent);
  322. }
  323. }
  324. mutex_unlock(&notify_lock);
  325. return;
  326. }
  327. size += sizeof(*ent);
  328. ent = kzalloc(size, GFP_KERNEL);
  329. if (!ent)
  330. return;
  331. ent->msg = (struct cn_ctl_msg *)(ent + 1);
  332. memcpy(ent->msg, ctl, size - sizeof(*ent));
  333. mutex_lock(&notify_lock);
  334. list_add(&ent->notify_entry, &notify_list);
  335. mutex_unlock(&notify_lock);
  336. }
  337. static int cn_proc_show(struct seq_file *m, void *v)
  338. {
  339. struct cn_queue_dev *dev = cdev.cbdev;
  340. struct cn_callback_entry *cbq;
  341. seq_printf(m, "Name ID\n");
  342. spin_lock_bh(&dev->queue_lock);
  343. list_for_each_entry(cbq, &dev->queue_list, callback_entry) {
  344. seq_printf(m, "%-15s %u:%u\n",
  345. cbq->id.name,
  346. cbq->id.id.idx,
  347. cbq->id.id.val);
  348. }
  349. spin_unlock_bh(&dev->queue_lock);
  350. return 0;
  351. }
  352. static int cn_proc_open(struct inode *inode, struct file *file)
  353. {
  354. return single_open(file, cn_proc_show, NULL);
  355. }
  356. static const struct file_operations cn_file_ops = {
  357. .owner = THIS_MODULE,
  358. .open = cn_proc_open,
  359. .read = seq_read,
  360. .llseek = seq_lseek,
  361. .release = single_release
  362. };
  363. static int __devinit cn_init(void)
  364. {
  365. struct cn_dev *dev = &cdev;
  366. int err;
  367. dev->input = cn_rx_skb;
  368. dev->id.idx = cn_idx;
  369. dev->id.val = cn_val;
  370. dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR,
  371. CN_NETLINK_USERS + 0xf,
  372. dev->input, NULL, THIS_MODULE);
  373. if (!dev->nls)
  374. return -EIO;
  375. dev->cbdev = cn_queue_alloc_dev("cqueue", dev->nls);
  376. if (!dev->cbdev) {
  377. netlink_kernel_release(dev->nls);
  378. return -EINVAL;
  379. }
  380. cn_already_initialized = 1;
  381. err = cn_add_callback(&dev->id, "connector", &cn_callback);
  382. if (err) {
  383. cn_already_initialized = 0;
  384. cn_queue_free_dev(dev->cbdev);
  385. netlink_kernel_release(dev->nls);
  386. return -EINVAL;
  387. }
  388. proc_net_fops_create(&init_net, "connector", S_IRUGO, &cn_file_ops);
  389. return 0;
  390. }
  391. static void __devexit cn_fini(void)
  392. {
  393. struct cn_dev *dev = &cdev;
  394. cn_already_initialized = 0;
  395. proc_net_remove(&init_net, "connector");
  396. cn_del_callback(&dev->id);
  397. cn_queue_free_dev(dev->cbdev);
  398. netlink_kernel_release(dev->nls);
  399. }
  400. subsys_initcall(cn_init);
  401. module_exit(cn_fini);