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/config.h>
24 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/errno.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/poll.h>
32 #include <linux/fcntl.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>
47 #ifndef CONFIG_BT_CMTP_DEBUG
54 static DECLARE_RWSEM(cmtp_session_sem);
55 static LIST_HEAD(cmtp_session_list);
57 static struct cmtp_session *__cmtp_get_session(bdaddr_t *bdaddr)
59 struct cmtp_session *session;
64 list_for_each(p, &cmtp_session_list) {
65 session = list_entry(p, struct cmtp_session, list);
66 if (!bacmp(bdaddr, &session->bdaddr))
72 static void __cmtp_link_session(struct cmtp_session *session)
74 __module_get(THIS_MODULE);
75 list_add(&session->list, &cmtp_session_list);
78 static void __cmtp_unlink_session(struct cmtp_session *session)
80 list_del(&session->list);
81 module_put(THIS_MODULE);
84 static void __cmtp_copy_session(struct cmtp_session *session, struct cmtp_conninfo *ci)
86 bacpy(&ci->bdaddr, &session->bdaddr);
88 ci->flags = session->flags;
89 ci->state = session->state;
91 ci->num = session->num;
95 static inline int cmtp_alloc_block_id(struct cmtp_session *session)
99 for (i = 0; i < 16; i++)
100 if (!test_and_set_bit(i, &session->blockids)) {
108 static inline void cmtp_free_block_id(struct cmtp_session *session, int id)
110 clear_bit(id, &session->blockids);
113 static inline void cmtp_add_msgpart(struct cmtp_session *session, int id, const unsigned char *buf, int count)
115 struct sk_buff *skb = session->reassembly[id], *nskb;
118 BT_DBG("session %p buf %p count %d", session, buf, count);
120 size = (skb) ? skb->len + count : count;
122 if (!(nskb = alloc_skb(size, GFP_ATOMIC))) {
123 BT_ERR("Can't allocate memory for CAPI message");
127 if (skb && (skb->len > 0))
128 memcpy(skb_put(nskb, skb->len), skb->data, skb->len);
130 memcpy(skb_put(nskb, count), buf, count);
132 session->reassembly[id] = nskb;
138 static inline int cmtp_recv_frame(struct cmtp_session *session, struct sk_buff *skb)
140 __u8 hdr, hdrlen, id;
143 BT_DBG("session %p skb %p len %d", session, skb, skb->len);
145 while (skb->len > 0) {
148 switch (hdr & 0xc0) {
155 len = skb->data[1] | (skb->data[2] << 8);
163 id = (hdr & 0x3c) >> 2;
165 BT_DBG("hdr 0x%02x hdrlen %d len %d id %d", hdr, hdrlen, len, id);
167 if (hdrlen + len > skb->len) {
168 BT_ERR("Wrong size or header information in CMTP frame");
173 skb_pull(skb, hdrlen);
177 switch (hdr & 0x03) {
179 cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
180 cmtp_recv_capimsg(session, session->reassembly[id]);
181 session->reassembly[id] = NULL;
184 cmtp_add_msgpart(session, id, skb->data + hdrlen, len);
187 if (session->reassembly[id] != NULL)
188 kfree_skb(session->reassembly[id]);
189 session->reassembly[id] = NULL;
193 skb_pull(skb, hdrlen + len);
200 static int cmtp_send_frame(struct cmtp_session *session, unsigned char *data, int len)
202 struct socket *sock = session->sock;
203 struct kvec iv = { data, len };
206 BT_DBG("session %p data %p len %d", session, data, len);
211 memset(&msg, 0, sizeof(msg));
213 return kernel_sendmsg(sock, &msg, &iv, 1, len);
216 static void cmtp_process_transmit(struct cmtp_session *session)
218 struct sk_buff *skb, *nskb;
220 unsigned int size, tail;
222 BT_DBG("session %p", session);
224 if (!(nskb = alloc_skb(session->mtu, GFP_ATOMIC))) {
225 BT_ERR("Can't allocate memory for new frame");
229 while ((skb = skb_dequeue(&session->transmit))) {
230 struct cmtp_scb *scb = (void *) skb->cb;
232 if ((tail = (session->mtu - nskb->len)) < 5) {
233 cmtp_send_frame(session, nskb->data, nskb->len);
238 size = min_t(uint, ((tail < 258) ? (tail - 2) : (tail - 3)), skb->len);
240 if ((scb->id < 0) && ((scb->id = cmtp_alloc_block_id(session)) < 0)) {
241 skb_queue_head(&session->transmit, skb);
246 hdr = skb_put(nskb, 2);
248 | ((scb->id << 2) & 0x3c)
249 | ((skb->len == size) ? 0x00 : 0x01);
252 hdr = skb_put(nskb, 3);
254 | ((scb->id << 2) & 0x3c)
255 | ((skb->len == size) ? 0x00 : 0x01);
256 hdr[1] = size & 0xff;
260 memcpy(skb_put(nskb, size), skb->data, size);
264 skb_queue_head(&session->transmit, skb);
266 cmtp_free_block_id(session, scb->id);
268 cmtp_send_frame(session, nskb->data, nskb->len);
275 cmtp_send_frame(session, nskb->data, nskb->len);
280 static int cmtp_session(void *arg)
282 struct cmtp_session *session = arg;
283 struct sock *sk = session->sock->sk;
287 BT_DBG("session %p", session);
289 daemonize("kcmtpd_ctr_%d", session->num);
290 set_user_nice(current, -15);
291 current->flags |= PF_NOFREEZE;
293 init_waitqueue_entry(&wait, current);
294 add_wait_queue(sk->sk_sleep, &wait);
295 while (!atomic_read(&session->terminate)) {
296 set_current_state(TASK_INTERRUPTIBLE);
298 if (sk->sk_state != BT_CONNECTED)
301 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
303 cmtp_recv_frame(session, skb);
306 cmtp_process_transmit(session);
310 set_current_state(TASK_RUNNING);
311 remove_wait_queue(sk->sk_sleep, &wait);
313 down_write(&cmtp_session_sem);
315 if (!(session->flags & (1 << CMTP_LOOPBACK)))
316 cmtp_detach_device(session);
318 fput(session->sock->file);
320 __cmtp_unlink_session(session);
322 up_write(&cmtp_session_sem);
328 int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
330 struct cmtp_session *session, *s;
336 baswap(&src, &bt_sk(sock->sk)->src);
337 baswap(&dst, &bt_sk(sock->sk)->dst);
339 session = kmalloc(sizeof(struct cmtp_session), GFP_KERNEL);
342 memset(session, 0, sizeof(struct cmtp_session));
344 down_write(&cmtp_session_sem);
346 s = __cmtp_get_session(&bt_sk(sock->sk)->dst);
347 if (s && s->state == BT_CONNECTED) {
352 bacpy(&session->bdaddr, &bt_sk(sock->sk)->dst);
354 session->mtu = min_t(uint, l2cap_pi(sock->sk)->omtu, l2cap_pi(sock->sk)->imtu);
356 BT_DBG("mtu %d", session->mtu);
358 sprintf(session->name, "%s", batostr(&dst));
360 session->sock = sock;
361 session->state = BT_CONFIG;
363 init_waitqueue_head(&session->wait);
365 session->msgnum = CMTP_INITIAL_MSGNUM;
367 INIT_LIST_HEAD(&session->applications);
369 skb_queue_head_init(&session->transmit);
371 for (i = 0; i < 16; i++)
372 session->reassembly[i] = NULL;
374 session->flags = req->flags;
376 __cmtp_link_session(session);
378 err = kernel_thread(cmtp_session, session, CLONE_KERNEL);
382 if (!(session->flags & (1 << CMTP_LOOPBACK))) {
383 err = cmtp_attach_device(session);
388 up_write(&cmtp_session_sem);
392 cmtp_detach_device(session);
395 __cmtp_unlink_session(session);
398 up_write(&cmtp_session_sem);
403 int cmtp_del_connection(struct cmtp_conndel_req *req)
405 struct cmtp_session *session;
410 down_read(&cmtp_session_sem);
412 session = __cmtp_get_session(&req->bdaddr);
414 /* Flush the transmit queue */
415 skb_queue_purge(&session->transmit);
417 /* Kill session thread */
418 atomic_inc(&session->terminate);
419 cmtp_schedule(session);
423 up_read(&cmtp_session_sem);
427 int cmtp_get_connlist(struct cmtp_connlist_req *req)
434 down_read(&cmtp_session_sem);
436 list_for_each(p, &cmtp_session_list) {
437 struct cmtp_session *session;
438 struct cmtp_conninfo ci;
440 session = list_entry(p, struct cmtp_session, list);
442 __cmtp_copy_session(session, &ci);
444 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
449 if (++n >= req->cnum)
456 up_read(&cmtp_session_sem);
460 int cmtp_get_conninfo(struct cmtp_conninfo *ci)
462 struct cmtp_session *session;
465 down_read(&cmtp_session_sem);
467 session = __cmtp_get_session(&ci->bdaddr);
469 __cmtp_copy_session(session, ci);
473 up_read(&cmtp_session_sem);
478 static int __init cmtp_init(void)
482 BT_INFO("CMTP (CAPI Emulation) ver %s", VERSION);
489 static void __exit cmtp_exit(void)
491 cmtp_cleanup_sockets();
494 module_init(cmtp_init);
495 module_exit(cmtp_exit);
497 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
498 MODULE_DESCRIPTION("Bluetooth CMTP ver " VERSION);
499 MODULE_VERSION(VERSION);
500 MODULE_LICENSE("GPL");
501 MODULE_ALIAS("bt-proto-5");