connector.c 10 KB

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