blob: 278a194e6af488f67197c3725ca937f554989498 [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 CMTP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
8
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090013 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090018 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 SOFTWARE IS DISCLAIMED.
21*/
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24
25#include <linux/types.h>
26#include <linux/errno.h>
27#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/sched.h>
29#include <linux/slab.h>
30#include <linux/poll.h>
31#include <linux/fcntl.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070032#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/skbuff.h>
34#include <linux/socket.h>
35#include <linux/ioctl.h>
36#include <linux/file.h>
37#include <linux/init.h>
Szymon Jancfada4ac2011-03-21 14:20:06 +010038#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <net/sock.h>
40
41#include <linux/isdn/capilli.h>
42
43#include <net/bluetooth/bluetooth.h>
44#include <net/bluetooth/l2cap.h>
45
46#include "cmtp.h"
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define VERSION "1.0"
49
50static DECLARE_RWSEM(cmtp_session_sem);
51static LIST_HEAD(cmtp_session_list);
52
53static struct cmtp_session *__cmtp_get_session(bdaddr_t *bdaddr)
54{
55 struct cmtp_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 BT_DBG("");
58
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +020059 list_for_each_entry(session, &cmtp_session_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (!bacmp(bdaddr, &session->bdaddr))
61 return session;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +020062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return NULL;
64}
65
66static void __cmtp_link_session(struct cmtp_session *session)
67{
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 list_add(&session->list, &cmtp_session_list);
69}
70
71static void __cmtp_unlink_session(struct cmtp_session *session)
72{
73 list_del(&session->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76static void __cmtp_copy_session(struct cmtp_session *session, struct cmtp_conninfo *ci)
77{
Vasiliy Kulikov3185fbd2010-10-30 18:26:26 +040078 memset(ci, 0, sizeof(*ci));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 bacpy(&ci->bdaddr, &session->bdaddr);
80
81 ci->flags = session->flags;
82 ci->state = session->state;
83
84 ci->num = session->num;
85}
86
87
88static inline int cmtp_alloc_block_id(struct cmtp_session *session)
89{
90 int i, id = -1;
91
92 for (i = 0; i < 16; i++)
93 if (!test_and_set_bit(i, &session->blockids)) {
94 id = i;
95 break;
96 }
97
98 return id;
99}
100
101static inline void cmtp_free_block_id(struct cmtp_session *session, int id)
102{
103 clear_bit(id, &session->blockids);
104}
105
106static inline void cmtp_add_msgpart(struct cmtp_session *session, int id, const unsigned char *buf, int count)
107{
108 struct sk_buff *skb = session->reassembly[id], *nskb;
109 int size;
110
111 BT_DBG("session %p buf %p count %d", session, buf, count);
112
113 size = (skb) ? skb->len + count : count;
114
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200115 nskb = alloc_skb(size, GFP_ATOMIC);
116 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 BT_ERR("Can't allocate memory for CAPI message");
118 return;
119 }
120
121 if (skb && (skb->len > 0))
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300122 skb_copy_from_linear_data(skb, skb_put(nskb, skb->len), skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 memcpy(skb_put(nskb, count), buf, count);
125
126 session->reassembly[id] = nskb;
127
Wei Yongjun7585b972009-02-25 18:29:52 +0800128 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static inline int cmtp_recv_frame(struct cmtp_session *session, struct sk_buff *skb)
132{
133 __u8 hdr, hdrlen, id;
134 __u16 len;
135
136 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
137
138 while (skb->len > 0) {
139 hdr = skb->data[0];
140
141 switch (hdr & 0xc0) {
142 case 0x40:
143 hdrlen = 2;
144 len = skb->data[1];
145 break;
146 case 0x80:
147 hdrlen = 3;
148 len = skb->data[1] | (skb->data[2] << 8);
149 break;
150 default:
151 hdrlen = 1;
152 len = 0;
153 break;
154 }
155
156 id = (hdr & 0x3c) >> 2;
157
158 BT_DBG("hdr 0x%02x hdrlen %d len %d id %d", hdr, hdrlen, len, id);
159
160 if (hdrlen + len > skb->len) {
161 BT_ERR("Wrong size or header information in CMTP frame");
162 break;
163 }
164
165 if (len == 0) {
166 skb_pull(skb, hdrlen);
167 continue;
168 }
169
170 switch (hdr & 0x03) {
171 case 0x00:
172 cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
173 cmtp_recv_capimsg(session, session->reassembly[id]);
174 session->reassembly[id] = NULL;
175 break;
176 case 0x01:
177 cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
178 break;
179 default:
180 if (session->reassembly[id] != NULL)
181 kfree_skb(session->reassembly[id]);
182 session->reassembly[id] = NULL;
183 break;
184 }
185
186 skb_pull(skb, hdrlen + len);
187 }
188
189 kfree_skb(skb);
190 return 0;
191}
192
193static int cmtp_send_frame(struct cmtp_session *session, unsigned char *data, int len)
194{
195 struct socket *sock = session->sock;
196 struct kvec iv = { data, len };
197 struct msghdr msg;
198
199 BT_DBG("session %p data %p len %d", session, data, len);
200
201 if (!len)
202 return 0;
203
204 memset(&msg, 0, sizeof(msg));
205
206 return kernel_sendmsg(sock, &msg, &iv, 1, len);
207}
208
David S. Millerb03efcf2005-07-08 14:57:23 -0700209static void cmtp_process_transmit(struct cmtp_session *session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct sk_buff *skb, *nskb;
212 unsigned char *hdr;
213 unsigned int size, tail;
214
215 BT_DBG("session %p", session);
216
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200217 nskb = alloc_skb(session->mtu, GFP_ATOMIC);
218 if (!nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 BT_ERR("Can't allocate memory for new frame");
David S. Millerb03efcf2005-07-08 14:57:23 -0700220 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
223 while ((skb = skb_dequeue(&session->transmit))) {
224 struct cmtp_scb *scb = (void *) skb->cb;
225
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +0200226 tail = session->mtu - nskb->len;
227 if (tail < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 cmtp_send_frame(session, nskb->data, nskb->len);
229 skb_trim(nskb, 0);
230 tail = session->mtu;
231 }
232
233 size = min_t(uint, ((tail < 258) ? (tail - 2) : (tail - 3)), skb->len);
234
Szymon Janc58aac462011-03-21 14:20:03 +0100235 if (scb->id < 0) {
236 scb->id = cmtp_alloc_block_id(session);
237 if (scb->id < 0) {
238 skb_queue_head(&session->transmit, skb);
239 break;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 if (size < 256) {
244 hdr = skb_put(nskb, 2);
245 hdr[0] = 0x40
246 | ((scb->id << 2) & 0x3c)
247 | ((skb->len == size) ? 0x00 : 0x01);
248 hdr[1] = size;
249 } else {
250 hdr = skb_put(nskb, 3);
251 hdr[0] = 0x80
252 | ((scb->id << 2) & 0x3c)
253 | ((skb->len == size) ? 0x00 : 0x01);
254 hdr[1] = size & 0xff;
255 hdr[2] = size >> 8;
256 }
257
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300258 skb_copy_from_linear_data(skb, skb_put(nskb, size), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 skb_pull(skb, size);
260
261 if (skb->len > 0) {
262 skb_queue_head(&session->transmit, skb);
263 } else {
264 cmtp_free_block_id(session, scb->id);
265 if (scb->data) {
266 cmtp_send_frame(session, nskb->data, nskb->len);
267 skb_trim(nskb, 0);
268 }
269 kfree_skb(skb);
270 }
271 }
272
273 cmtp_send_frame(session, nskb->data, nskb->len);
274
275 kfree_skb(nskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278static int cmtp_session(void *arg)
279{
280 struct cmtp_session *session = arg;
281 struct sock *sk = session->sock->sk;
282 struct sk_buff *skb;
283 wait_queue_t wait;
284
285 BT_DBG("session %p", session);
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 set_user_nice(current, -15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 init_waitqueue_entry(&wait, current);
Eric Dumazetaa395142010-04-20 13:03:51 +0000290 add_wait_queue(sk_sleep(sk), &wait);
Peter Hurley3a3f5c72011-07-24 00:11:10 -0400291 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 set_current_state(TASK_INTERRUPTIBLE);
293
Peter Hurley71765222011-08-05 10:44:21 -0400294 if (atomic_read(&session->terminate))
Peter Hurley3a3f5c72011-07-24 00:11:10 -0400295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (sk->sk_state != BT_CONNECTED)
297 break;
298
299 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
300 skb_orphan(skb);
Mat Martineau44935722011-07-22 14:53:58 -0700301 if (!skb_linearize(skb))
302 cmtp_recv_frame(session, skb);
303 else
304 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
307 cmtp_process_transmit(session);
308
309 schedule();
310 }
Peter Hurley3a3f5c72011-07-24 00:11:10 -0400311 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000312 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 down_write(&cmtp_session_sem);
315
316 if (!(session->flags & (1 << CMTP_LOOPBACK)))
317 cmtp_detach_device(session);
318
319 fput(session->sock->file);
320
321 __cmtp_unlink_session(session);
322
323 up_write(&cmtp_session_sem);
324
325 kfree(session);
David Herrmann48b28b82011-11-19 13:23:32 +0100326 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return 0;
328}
329
330int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
331{
332 struct cmtp_session *session, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int i, err;
334
335 BT_DBG("");
336
Al Viro96c26652014-12-19 06:20:58 +0000337 if (!l2cap_is_socket(sock))
338 return -EBADFD;
339
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200340 session = kzalloc(sizeof(struct cmtp_session), GFP_KERNEL);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900341 if (!session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 down_write(&cmtp_session_sem);
345
Marcel Holtmann88f1fd22013-10-13 09:49:55 -0700346 s = __cmtp_get_session(&l2cap_pi(sock->sk)->chan->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (s && s->state == BT_CONNECTED) {
348 err = -EEXIST;
349 goto failed;
350 }
351
Marcel Holtmann88f1fd22013-10-13 09:49:55 -0700352 bacpy(&session->bdaddr, &l2cap_pi(sock->sk)->chan->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300354 session->mtu = min_t(uint, l2cap_pi(sock->sk)->chan->omtu,
355 l2cap_pi(sock->sk)->chan->imtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 BT_DBG("mtu %d", session->mtu);
358
Marcel Holtmann88f1fd22013-10-13 09:49:55 -0700359 sprintf(session->name, "%pMR", &session->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 session->sock = sock;
362 session->state = BT_CONFIG;
363
364 init_waitqueue_head(&session->wait);
365
366 session->msgnum = CMTP_INITIAL_MSGNUM;
367
368 INIT_LIST_HEAD(&session->applications);
369
370 skb_queue_head_init(&session->transmit);
371
372 for (i = 0; i < 16; i++)
373 session->reassembly[i] = NULL;
374
375 session->flags = req->flags;
376
377 __cmtp_link_session(session);
378
David Herrmann48b28b82011-11-19 13:23:32 +0100379 __module_get(THIS_MODULE);
Szymon Jancfada4ac2011-03-21 14:20:06 +0100380 session->task = kthread_run(cmtp_session, session, "kcmtpd_ctr_%d",
381 session->num);
382 if (IS_ERR(session->task)) {
David Herrmann48b28b82011-11-19 13:23:32 +0100383 module_put(THIS_MODULE);
Szymon Jancfada4ac2011-03-21 14:20:06 +0100384 err = PTR_ERR(session->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto unlink;
Szymon Jancfada4ac2011-03-21 14:20:06 +0100386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (!(session->flags & (1 << CMTP_LOOPBACK))) {
389 err = cmtp_attach_device(session);
Peter Hurley687beaa2011-08-05 10:53:52 -0400390 if (err < 0) {
391 atomic_inc(&session->terminate);
392 wake_up_process(session->task);
393 up_write(&cmtp_session_sem);
394 return err;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
398 up_write(&cmtp_session_sem);
399 return 0;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401unlink:
402 __cmtp_unlink_session(session);
403
404failed:
405 up_write(&cmtp_session_sem);
406 kfree(session);
407 return err;
408}
409
410int cmtp_del_connection(struct cmtp_conndel_req *req)
411{
412 struct cmtp_session *session;
413 int err = 0;
414
415 BT_DBG("");
416
417 down_read(&cmtp_session_sem);
418
419 session = __cmtp_get_session(&req->bdaddr);
420 if (session) {
421 /* Flush the transmit queue */
422 skb_queue_purge(&session->transmit);
423
Szymon Jancfada4ac2011-03-21 14:20:06 +0100424 /* Stop session thread */
Peter Hurley71765222011-08-05 10:44:21 -0400425 atomic_inc(&session->terminate);
426 wake_up_process(session->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 } else
428 err = -ENOENT;
429
430 up_read(&cmtp_session_sem);
431 return err;
432}
433
434int cmtp_get_connlist(struct cmtp_connlist_req *req)
435{
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200436 struct cmtp_session *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int err = 0, n = 0;
438
439 BT_DBG("");
440
441 down_read(&cmtp_session_sem);
442
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200443 list_for_each_entry(session, &cmtp_session_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct cmtp_conninfo ci;
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 __cmtp_copy_session(session, &ci);
447
448 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
449 err = -EFAULT;
450 break;
451 }
452
453 if (++n >= req->cnum)
454 break;
455
456 req->ci++;
457 }
458 req->cnum = n;
459
460 up_read(&cmtp_session_sem);
461 return err;
462}
463
464int cmtp_get_conninfo(struct cmtp_conninfo *ci)
465{
466 struct cmtp_session *session;
467 int err = 0;
468
469 down_read(&cmtp_session_sem);
470
471 session = __cmtp_get_session(&ci->bdaddr);
472 if (session)
473 __cmtp_copy_session(session, ci);
474 else
475 err = -ENOENT;
476
477 up_read(&cmtp_session_sem);
478 return err;
479}
480
481
482static int __init cmtp_init(void)
483{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 BT_INFO("CMTP (CAPI Emulation) ver %s", VERSION);
485
486 cmtp_init_sockets();
487
488 return 0;
489}
490
491static void __exit cmtp_exit(void)
492{
493 cmtp_cleanup_sockets();
494}
495
496module_init(cmtp_init);
497module_exit(cmtp_exit);
498
499MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
500MODULE_DESCRIPTION("Bluetooth CMTP ver " VERSION);
501MODULE_VERSION(VERSION);
502MODULE_LICENSE("GPL");
503MODULE_ALIAS("bt-proto-5");