connector.c 11 KB

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