2 CMTP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2002-2003 Marcel Holtmann <marcel@holtmann.org>
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;
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
13 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
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
23 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/poll.h>
31 #include <linux/fcntl.h>
32 #include <linux/freezer.h>
33 #include <linux/skbuff.h>
34 #include <linux/socket.h>
35 #include <linux/ioctl.h>
36 #include <linux/file.h>
37 #include <linux/init.h>
40 #include <linux/isdn/capilli.h>
42 #include <net/bluetooth/bluetooth.h>
43 #include <net/bluetooth/l2cap.h>
49 static DECLARE_RWSEM(cmtp_session_sem);
50 static LIST_HEAD(cmtp_session_list);
52 static struct cmtp_session *__cmtp_get_session(bdaddr_t *bdaddr)
54 struct cmtp_session *session;
59 list_for_each(p, &cmtp_session_list) {
60 session = list_entry(p, struct cmtp_session, list);
61 if (!bacmp(bdaddr, &session->bdaddr))
67 static void __cmtp_link_session(struct cmtp_session *session)
69 __module_get(THIS_MODULE);
70 list_add(&session->list, &cmtp_session_list);
73 static void __cmtp_unlink_session(struct cmtp_session *session)
75 list_del(&session->list);
76 module_put(THIS_MODULE);
79 static void __cmtp_copy_session(struct cmtp_session *session, struct cmtp_conninfo *ci)
81 bacpy(&ci->bdaddr, &session->bdaddr);
83 ci->flags = session->flags;
84 ci->state = session->state;
86 ci->num = session->num;
90 static inline int cmtp_alloc_block_id(struct cmtp_session *session)
94 for (i = 0; i < 16; i++)
95 if (!test_and_set_bit(i, &session->blockids)) {
103 static inline void cmtp_free_block_id(struct cmtp_session *session, int id)
105 clear_bit(id, &session->blockids);
108 static inline void cmtp_add_msgpart(struct cmtp_session *session, int id, const unsigned char *buf, int count)
110 struct sk_buff *skb = session->reassembly[id], *nskb;
113 BT_DBG("session %p buf %p count %d", session, buf, count);
115 size = (skb) ? skb->len + count : count;
117 if (!(nskb = alloc_skb(size, GFP_ATOMIC))) {
118 BT_ERR("Can't allocate memory for CAPI message");
122 if (skb && (skb->len > 0))
123 skb_copy_from_linear_data(skb, skb_put(nskb, skb->len), skb->len);
125 memcpy(skb_put(nskb, count), buf, count);
127 session->reassembly[id] = nskb;
133 static inline int cmtp_recv_frame(struct cmtp_session *session, struct sk_buff *skb)
135 __u8 hdr, hdrlen, id;
138 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
140 while (skb->len > 0) {
143 switch (hdr & 0xc0) {
150 len = skb->data[1] | (skb->data[2] << 8);
158 id = (hdr & 0x3c) >> 2;
160 BT_DBG("hdr 0x%02x hdrlen %d len %d id %d", hdr, hdrlen, len, id);
162 if (hdrlen + len > skb->len) {
163 BT_ERR("Wrong size or header information in CMTP frame");
168 skb_pull(skb, hdrlen);
172 switch (hdr & 0x03) {
174 cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
175 cmtp_recv_capimsg(session, session->reassembly[id]);
176 session->reassembly[id] = NULL;
179 cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
182 if (session->reassembly[id] != NULL)
183 kfree_skb(session->reassembly[id]);
184 session->reassembly[id] = NULL;
188 skb_pull(skb, hdrlen + len);
195 static int cmtp_send_frame(struct cmtp_session *session, unsigned char *data, int len)
197 struct socket *sock = session->sock;
198 struct kvec iv = { data, len };
201 BT_DBG("session %p data %p len %d", session, data, len);
206 memset(&msg, 0, sizeof(msg));
208 return kernel_sendmsg(sock, &msg, &iv, 1, len);
211 static void cmtp_process_transmit(struct cmtp_session *session)
213 struct sk_buff *skb, *nskb;
215 unsigned int size, tail;
217 BT_DBG("session %p", session);
219 if (!(nskb = alloc_skb(session->mtu, GFP_ATOMIC))) {
220 BT_ERR("Can't allocate memory for new frame");
224 while ((skb = skb_dequeue(&session->transmit))) {
225 struct cmtp_scb *scb = (void *) skb->cb;
227 if ((tail = (session->mtu - nskb->len)) < 5) {
228 cmtp_send_frame(session, nskb->data, nskb->len);
233 size = min_t(uint, ((tail < 258) ? (tail - 2) : (tail - 3)), skb->len);
235 if ((scb->id < 0) && ((scb->id = cmtp_alloc_block_id(session)) < 0)) {
236 skb_queue_head(&session->transmit, skb);
241 hdr = skb_put(nskb, 2);
243 | ((scb->id << 2) & 0x3c)
244 | ((skb->len == size) ? 0x00 : 0x01);
247 hdr = skb_put(nskb, 3);
249 | ((scb->id << 2) & 0x3c)
250 | ((skb->len == size) ? 0x00 : 0x01);
251 hdr[1] = size & 0xff;
255 skb_copy_from_linear_data(skb, skb_put(nskb, size), size);
259 skb_queue_head(&session->transmit, skb);
261 cmtp_free_block_id(session, scb->id);
263 cmtp_send_frame(session, nskb->data, nskb->len);
270 cmtp_send_frame(session, nskb->data, nskb->len);
275 static int cmtp_session(void *arg)
277 struct cmtp_session *session = arg;
278 struct sock *sk = session->sock->sk;
282 BT_DBG("session %p", session);
284 daemonize("kcmtpd_ctr_%d", session->num);
285 set_user_nice(current, -15);
287 init_waitqueue_entry(&wait, current);
288 add_wait_queue(sk->sk_sleep, &wait);
289 while (!atomic_read(&session->terminate)) {
290 set_current_state(TASK_INTERRUPTIBLE);
292 if (sk->sk_state != BT_CONNECTED)
295 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
297 cmtp_recv_frame(session, skb);
300 cmtp_process_transmit(session);
304 set_current_state(TASK_RUNNING);
305 remove_wait_queue(sk->sk_sleep, &wait);
307 down_write(&cmtp_session_sem);
309 if (!(session->flags & (1 << CMTP_LOOPBACK)))
310 cmtp_detach_device(session);
312 fput(session->sock->file);
314 __cmtp_unlink_session(session);
316 up_write(&cmtp_session_sem);
322 int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
324 struct cmtp_session *session, *s;
330 baswap(&src, &bt_sk(sock->sk)->src);
331 baswap(&dst, &bt_sk(sock->sk)->dst);
333 session = kzalloc(sizeof(struct cmtp_session), GFP_KERNEL);
337 down_write(&cmtp_session_sem);
339 s = __cmtp_get_session(&bt_sk(sock->sk)->dst);
340 if (s && s->state == BT_CONNECTED) {
345 bacpy(&session->bdaddr, &bt_sk(sock->sk)->dst);
347 session->mtu = min_t(uint, l2cap_pi(sock->sk)->omtu, l2cap_pi(sock->sk)->imtu);
349 BT_DBG("mtu %d", session->mtu);
351 sprintf(session->name, "%s", batostr(&dst));
353 session->sock = sock;
354 session->state = BT_CONFIG;
356 init_waitqueue_head(&session->wait);
358 session->msgnum = CMTP_INITIAL_MSGNUM;
360 INIT_LIST_HEAD(&session->applications);
362 skb_queue_head_init(&session->transmit);
364 for (i = 0; i < 16; i++)
365 session->reassembly[i] = NULL;
367 session->flags = req->flags;
369 __cmtp_link_session(session);
371 err = kernel_thread(cmtp_session, session, CLONE_KERNEL);
375 if (!(session->flags & (1 << CMTP_LOOPBACK))) {
376 err = cmtp_attach_device(session);
381 up_write(&cmtp_session_sem);
385 cmtp_detach_device(session);
388 __cmtp_unlink_session(session);
391 up_write(&cmtp_session_sem);
396 int cmtp_del_connection(struct cmtp_conndel_req *req)
398 struct cmtp_session *session;
403 down_read(&cmtp_session_sem);
405 session = __cmtp_get_session(&req->bdaddr);
407 /* Flush the transmit queue */
408 skb_queue_purge(&session->transmit);
410 /* Kill session thread */
411 atomic_inc(&session->terminate);
412 cmtp_schedule(session);
416 up_read(&cmtp_session_sem);
420 int cmtp_get_connlist(struct cmtp_connlist_req *req)
427 down_read(&cmtp_session_sem);
429 list_for_each(p, &cmtp_session_list) {
430 struct cmtp_session *session;
431 struct cmtp_conninfo ci;
433 session = list_entry(p, struct cmtp_session, list);
435 __cmtp_copy_session(session, &ci);
437 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
442 if (++n >= req->cnum)
449 up_read(&cmtp_session_sem);
453 int cmtp_get_conninfo(struct cmtp_conninfo *ci)
455 struct cmtp_session *session;
458 down_read(&cmtp_session_sem);
460 session = __cmtp_get_session(&ci->bdaddr);
462 __cmtp_copy_session(session, ci);
466 up_read(&cmtp_session_sem);
471 static int __init cmtp_init(void)
475 BT_INFO("CMTP (CAPI Emulation) ver %s", VERSION);
482 static void __exit cmtp_exit(void)
484 cmtp_cleanup_sockets();
487 module_init(cmtp_init);
488 module_exit(cmtp_exit);
490 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
491 MODULE_DESCRIPTION("Bluetooth CMTP ver " VERSION);
492 MODULE_VERSION(VERSION);
493 MODULE_LICENSE("GPL");
494 MODULE_ALIAS("bt-proto-5");