core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. CMTP implementation for Linux Bluetooth stack (BlueZ).
  3. Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License version 2 as
  6. published by the Free Software Foundation;
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  10. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  11. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  16. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  17. SOFTWARE IS DISCLAIMED.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/poll.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/freezer.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/socket.h>
  30. #include <linux/ioctl.h>
  31. #include <linux/file.h>
  32. #include <linux/init.h>
  33. #include <linux/kthread.h>
  34. #include <net/sock.h>
  35. #include <linux/isdn/capilli.h>
  36. #include <net/bluetooth/bluetooth.h>
  37. #include <net/bluetooth/l2cap.h>
  38. #include "cmtp.h"
  39. #define VERSION "1.0"
  40. static DECLARE_RWSEM(cmtp_session_sem);
  41. static LIST_HEAD(cmtp_session_list);
  42. static struct cmtp_session *__cmtp_get_session(bdaddr_t *bdaddr)
  43. {
  44. struct cmtp_session *session;
  45. struct list_head *p;
  46. BT_DBG("");
  47. list_for_each(p, &cmtp_session_list) {
  48. session = list_entry(p, struct cmtp_session, list);
  49. if (!bacmp(bdaddr, &session->bdaddr))
  50. return session;
  51. }
  52. return NULL;
  53. }
  54. static void __cmtp_link_session(struct cmtp_session *session)
  55. {
  56. __module_get(THIS_MODULE);
  57. list_add(&session->list, &cmtp_session_list);
  58. }
  59. static void __cmtp_unlink_session(struct cmtp_session *session)
  60. {
  61. list_del(&session->list);
  62. module_put(THIS_MODULE);
  63. }
  64. static void __cmtp_copy_session(struct cmtp_session *session, struct cmtp_conninfo *ci)
  65. {
  66. memset(ci, 0, sizeof(*ci));
  67. bacpy(&ci->bdaddr, &session->bdaddr);
  68. ci->flags = session->flags;
  69. ci->state = session->state;
  70. ci->num = session->num;
  71. }
  72. static inline int cmtp_alloc_block_id(struct cmtp_session *session)
  73. {
  74. int i, id = -1;
  75. for (i = 0; i < 16; i++)
  76. if (!test_and_set_bit(i, &session->blockids)) {
  77. id = i;
  78. break;
  79. }
  80. return id;
  81. }
  82. static inline void cmtp_free_block_id(struct cmtp_session *session, int id)
  83. {
  84. clear_bit(id, &session->blockids);
  85. }
  86. static inline void cmtp_add_msgpart(struct cmtp_session *session, int id, const unsigned char *buf, int count)
  87. {
  88. struct sk_buff *skb = session->reassembly[id], *nskb;
  89. int size;
  90. BT_DBG("session %p buf %p count %d", session, buf, count);
  91. size = (skb) ? skb->len + count : count;
  92. nskb = alloc_skb(size, GFP_ATOMIC);
  93. if (!nskb) {
  94. BT_ERR("Can't allocate memory for CAPI message");
  95. return;
  96. }
  97. if (skb && (skb->len > 0))
  98. skb_copy_from_linear_data(skb, skb_put(nskb, skb->len), skb->len);
  99. memcpy(skb_put(nskb, count), buf, count);
  100. session->reassembly[id] = nskb;
  101. kfree_skb(skb);
  102. }
  103. static inline int cmtp_recv_frame(struct cmtp_session *session, struct sk_buff *skb)
  104. {
  105. __u8 hdr, hdrlen, id;
  106. __u16 len;
  107. BT_DBG("session %p skb %p len %d", session, skb, skb->len);
  108. while (skb->len > 0) {
  109. hdr = skb->data[0];
  110. switch (hdr & 0xc0) {
  111. case 0x40:
  112. hdrlen = 2;
  113. len = skb->data[1];
  114. break;
  115. case 0x80:
  116. hdrlen = 3;
  117. len = skb->data[1] | (skb->data[2] << 8);
  118. break;
  119. default:
  120. hdrlen = 1;
  121. len = 0;
  122. break;
  123. }
  124. id = (hdr & 0x3c) >> 2;
  125. BT_DBG("hdr 0x%02x hdrlen %d len %d id %d", hdr, hdrlen, len, id);
  126. if (hdrlen + len > skb->len) {
  127. BT_ERR("Wrong size or header information in CMTP frame");
  128. break;
  129. }
  130. if (len == 0) {
  131. skb_pull(skb, hdrlen);
  132. continue;
  133. }
  134. switch (hdr & 0x03) {
  135. case 0x00:
  136. cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
  137. cmtp_recv_capimsg(session, session->reassembly[id]);
  138. session->reassembly[id] = NULL;
  139. break;
  140. case 0x01:
  141. cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
  142. break;
  143. default:
  144. if (session->reassembly[id] != NULL)
  145. kfree_skb(session->reassembly[id]);
  146. session->reassembly[id] = NULL;
  147. break;
  148. }
  149. skb_pull(skb, hdrlen + len);
  150. }
  151. kfree_skb(skb);
  152. return 0;
  153. }
  154. static int cmtp_send_frame(struct cmtp_session *session, unsigned char *data, int len)
  155. {
  156. struct socket *sock = session->sock;
  157. struct kvec iv = { data, len };
  158. struct msghdr msg;
  159. BT_DBG("session %p data %p len %d", session, data, len);
  160. if (!len)
  161. return 0;
  162. memset(&msg, 0, sizeof(msg));
  163. return kernel_sendmsg(sock, &msg, &iv, 1, len);
  164. }
  165. static void cmtp_process_transmit(struct cmtp_session *session)
  166. {
  167. struct sk_buff *skb, *nskb;
  168. unsigned char *hdr;
  169. unsigned int size, tail;
  170. BT_DBG("session %p", session);
  171. nskb = alloc_skb(session->mtu, GFP_ATOMIC);
  172. if (!nskb) {
  173. BT_ERR("Can't allocate memory for new frame");
  174. return;
  175. }
  176. while ((skb = skb_dequeue(&session->transmit))) {
  177. struct cmtp_scb *scb = (void *) skb->cb;
  178. tail = session->mtu - nskb->len;
  179. if (tail < 5) {
  180. cmtp_send_frame(session, nskb->data, nskb->len);
  181. skb_trim(nskb, 0);
  182. tail = session->mtu;
  183. }
  184. size = min_t(uint, ((tail < 258) ? (tail - 2) : (tail - 3)), skb->len);
  185. if (scb->id < 0) {
  186. scb->id = cmtp_alloc_block_id(session);
  187. if (scb->id < 0) {
  188. skb_queue_head(&session->transmit, skb);
  189. break;
  190. }
  191. }
  192. if (size < 256) {
  193. hdr = skb_put(nskb, 2);
  194. hdr[0] = 0x40
  195. | ((scb->id << 2) & 0x3c)
  196. | ((skb->len == size) ? 0x00 : 0x01);
  197. hdr[1] = size;
  198. } else {
  199. hdr = skb_put(nskb, 3);
  200. hdr[0] = 0x80
  201. | ((scb->id << 2) & 0x3c)
  202. | ((skb->len == size) ? 0x00 : 0x01);
  203. hdr[1] = size & 0xff;
  204. hdr[2] = size >> 8;
  205. }
  206. skb_copy_from_linear_data(skb, skb_put(nskb, size), size);
  207. skb_pull(skb, size);
  208. if (skb->len > 0) {
  209. skb_queue_head(&session->transmit, skb);
  210. } else {
  211. cmtp_free_block_id(session, scb->id);
  212. if (scb->data) {
  213. cmtp_send_frame(session, nskb->data, nskb->len);
  214. skb_trim(nskb, 0);
  215. }
  216. kfree_skb(skb);
  217. }
  218. }
  219. cmtp_send_frame(session, nskb->data, nskb->len);
  220. kfree_skb(nskb);
  221. }
  222. static int cmtp_session(void *arg)
  223. {
  224. struct cmtp_session *session = arg;
  225. struct sock *sk = session->sock->sk;
  226. struct sk_buff *skb;
  227. wait_queue_t wait;
  228. BT_DBG("session %p", session);
  229. set_user_nice(current, -15);
  230. init_waitqueue_entry(&wait, current);
  231. add_wait_queue(sk_sleep(sk), &wait);
  232. while (!kthread_should_stop()) {
  233. set_current_state(TASK_INTERRUPTIBLE);
  234. if (sk->sk_state != BT_CONNECTED)
  235. break;
  236. while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
  237. skb_orphan(skb);
  238. cmtp_recv_frame(session, skb);
  239. }
  240. cmtp_process_transmit(session);
  241. schedule();
  242. }
  243. set_current_state(TASK_RUNNING);
  244. remove_wait_queue(sk_sleep(sk), &wait);
  245. down_write(&cmtp_session_sem);
  246. if (!(session->flags & (1 << CMTP_LOOPBACK)))
  247. cmtp_detach_device(session);
  248. fput(session->sock->file);
  249. __cmtp_unlink_session(session);
  250. up_write(&cmtp_session_sem);
  251. kfree(session);
  252. return 0;
  253. }
  254. int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
  255. {
  256. struct cmtp_session *session, *s;
  257. int i, err;
  258. BT_DBG("");
  259. session = kzalloc(sizeof(struct cmtp_session), GFP_KERNEL);
  260. if (!session)
  261. return -ENOMEM;
  262. down_write(&cmtp_session_sem);
  263. s = __cmtp_get_session(&bt_sk(sock->sk)->dst);
  264. if (s && s->state == BT_CONNECTED) {
  265. err = -EEXIST;
  266. goto failed;
  267. }
  268. bacpy(&session->bdaddr, &bt_sk(sock->sk)->dst);
  269. session->mtu = min_t(uint, l2cap_pi(sock->sk)->chan->omtu,
  270. l2cap_pi(sock->sk)->chan->imtu);
  271. BT_DBG("mtu %d", session->mtu);
  272. sprintf(session->name, "%s", batostr(&bt_sk(sock->sk)->dst));
  273. session->sock = sock;
  274. session->state = BT_CONFIG;
  275. init_waitqueue_head(&session->wait);
  276. session->msgnum = CMTP_INITIAL_MSGNUM;
  277. INIT_LIST_HEAD(&session->applications);
  278. skb_queue_head_init(&session->transmit);
  279. for (i = 0; i < 16; i++)
  280. session->reassembly[i] = NULL;
  281. session->flags = req->flags;
  282. __cmtp_link_session(session);
  283. session->task = kthread_run(cmtp_session, session, "kcmtpd_ctr_%d",
  284. session->num);
  285. if (IS_ERR(session->task)) {
  286. err = PTR_ERR(session->task);
  287. goto unlink;
  288. }
  289. if (!(session->flags & (1 << CMTP_LOOPBACK))) {
  290. err = cmtp_attach_device(session);
  291. if (err < 0)
  292. goto detach;
  293. }
  294. up_write(&cmtp_session_sem);
  295. return 0;
  296. detach:
  297. cmtp_detach_device(session);
  298. unlink:
  299. __cmtp_unlink_session(session);
  300. failed:
  301. up_write(&cmtp_session_sem);
  302. kfree(session);
  303. return err;
  304. }
  305. int cmtp_del_connection(struct cmtp_conndel_req *req)
  306. {
  307. struct cmtp_session *session;
  308. int err = 0;
  309. BT_DBG("");
  310. down_read(&cmtp_session_sem);
  311. session = __cmtp_get_session(&req->bdaddr);
  312. if (session) {
  313. /* Flush the transmit queue */
  314. skb_queue_purge(&session->transmit);
  315. /* Stop session thread */
  316. kthread_stop(session->task);
  317. } else
  318. err = -ENOENT;
  319. up_read(&cmtp_session_sem);
  320. return err;
  321. }
  322. int cmtp_get_connlist(struct cmtp_connlist_req *req)
  323. {
  324. struct list_head *p;
  325. int err = 0, n = 0;
  326. BT_DBG("");
  327. down_read(&cmtp_session_sem);
  328. list_for_each(p, &cmtp_session_list) {
  329. struct cmtp_session *session;
  330. struct cmtp_conninfo ci;
  331. session = list_entry(p, struct cmtp_session, list);
  332. __cmtp_copy_session(session, &ci);
  333. if (copy_to_user(req->ci, &ci, sizeof(ci))) {
  334. err = -EFAULT;
  335. break;
  336. }
  337. if (++n >= req->cnum)
  338. break;
  339. req->ci++;
  340. }
  341. req->cnum = n;
  342. up_read(&cmtp_session_sem);
  343. return err;
  344. }
  345. int cmtp_get_conninfo(struct cmtp_conninfo *ci)
  346. {
  347. struct cmtp_session *session;
  348. int err = 0;
  349. down_read(&cmtp_session_sem);
  350. session = __cmtp_get_session(&ci->bdaddr);
  351. if (session)
  352. __cmtp_copy_session(session, ci);
  353. else
  354. err = -ENOENT;
  355. up_read(&cmtp_session_sem);
  356. return err;
  357. }
  358. static int __init cmtp_init(void)
  359. {
  360. BT_INFO("CMTP (CAPI Emulation) ver %s", VERSION);
  361. cmtp_init_sockets();
  362. return 0;
  363. }
  364. static void __exit cmtp_exit(void)
  365. {
  366. cmtp_cleanup_sockets();
  367. }
  368. module_init(cmtp_init);
  369. module_exit(cmtp_exit);
  370. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  371. MODULE_DESCRIPTION("Bluetooth CMTP ver " VERSION);
  372. MODULE_VERSION(VERSION);
  373. MODULE_LICENSE("GPL");
  374. MODULE_ALIAS("bt-proto-5");