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>
38 #include <linux/kthread.h>
41 #include <linux/isdn/capilli.h>
43 #include <net/bluetooth/bluetooth.h>
44 #include <net/bluetooth/l2cap.h>
50 static DECLARE_RWSEM(cmtp_session_sem
);
51 static LIST_HEAD(cmtp_session_list
);
53 static struct cmtp_session
*__cmtp_get_session(bdaddr_t
*bdaddr
)
55 struct cmtp_session
*session
;
59 list_for_each_entry(session
, &cmtp_session_list
, list
)
60 if (!bacmp(bdaddr
, &session
->bdaddr
))
66 static void __cmtp_link_session(struct cmtp_session
*session
)
68 list_add(&session
->list
, &cmtp_session_list
);
71 static void __cmtp_unlink_session(struct cmtp_session
*session
)
73 list_del(&session
->list
);
76 static void __cmtp_copy_session(struct cmtp_session
*session
, struct cmtp_conninfo
*ci
)
78 memset(ci
, 0, sizeof(*ci
));
79 bacpy(&ci
->bdaddr
, &session
->bdaddr
);
81 ci
->flags
= session
->flags
;
82 ci
->state
= session
->state
;
84 ci
->num
= session
->num
;
88 static inline int cmtp_alloc_block_id(struct cmtp_session
*session
)
92 for (i
= 0; i
< 16; i
++)
93 if (!test_and_set_bit(i
, &session
->blockids
)) {
101 static inline void cmtp_free_block_id(struct cmtp_session
*session
, int id
)
103 clear_bit(id
, &session
->blockids
);
106 static inline void cmtp_add_msgpart(struct cmtp_session
*session
, int id
, const unsigned char *buf
, int count
)
108 struct sk_buff
*skb
= session
->reassembly
[id
], *nskb
;
111 BT_DBG("session %p buf %p count %d", session
, buf
, count
);
113 size
= (skb
) ? skb
->len
+ count
: count
;
115 nskb
= alloc_skb(size
, GFP_ATOMIC
);
117 BT_ERR("Can't allocate memory for CAPI message");
121 if (skb
&& (skb
->len
> 0))
122 skb_copy_from_linear_data(skb
, skb_put(nskb
, skb
->len
), skb
->len
);
124 memcpy(skb_put(nskb
, count
), buf
, count
);
126 session
->reassembly
[id
] = nskb
;
131 static inline int cmtp_recv_frame(struct cmtp_session
*session
, struct sk_buff
*skb
)
133 __u8 hdr
, hdrlen
, id
;
136 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
138 while (skb
->len
> 0) {
141 switch (hdr
& 0xc0) {
148 len
= skb
->data
[1] | (skb
->data
[2] << 8);
156 id
= (hdr
& 0x3c) >> 2;
158 BT_DBG("hdr 0x%02x hdrlen %d len %d id %d", hdr
, hdrlen
, len
, id
);
160 if (hdrlen
+ len
> skb
->len
) {
161 BT_ERR("Wrong size or header information in CMTP frame");
166 skb_pull(skb
, hdrlen
);
170 switch (hdr
& 0x03) {
172 cmtp_add_msgpart(session
, id
, skb
->data
+ hdrlen
, len
);
173 cmtp_recv_capimsg(session
, session
->reassembly
[id
]);
174 session
->reassembly
[id
] = NULL
;
177 cmtp_add_msgpart(session
, id
, skb
->data
+ hdrlen
, len
);
180 if (session
->reassembly
[id
] != NULL
)
181 kfree_skb(session
->reassembly
[id
]);
182 session
->reassembly
[id
] = NULL
;
186 skb_pull(skb
, hdrlen
+ len
);
193 static int cmtp_send_frame(struct cmtp_session
*session
, unsigned char *data
, int len
)
195 struct socket
*sock
= session
->sock
;
196 struct kvec iv
= { data
, len
};
199 BT_DBG("session %p data %p len %d", session
, data
, len
);
204 memset(&msg
, 0, sizeof(msg
));
206 return kernel_sendmsg(sock
, &msg
, &iv
, 1, len
);
209 static void cmtp_process_transmit(struct cmtp_session
*session
)
211 struct sk_buff
*skb
, *nskb
;
213 unsigned int size
, tail
;
215 BT_DBG("session %p", session
);
217 nskb
= alloc_skb(session
->mtu
, GFP_ATOMIC
);
219 BT_ERR("Can't allocate memory for new frame");
223 while ((skb
= skb_dequeue(&session
->transmit
))) {
224 struct cmtp_scb
*scb
= (void *) skb
->cb
;
226 tail
= session
->mtu
- nskb
->len
;
228 cmtp_send_frame(session
, nskb
->data
, nskb
->len
);
233 size
= min_t(uint
, ((tail
< 258) ? (tail
- 2) : (tail
- 3)), skb
->len
);
236 scb
->id
= cmtp_alloc_block_id(session
);
238 skb_queue_head(&session
->transmit
, skb
);
244 hdr
= skb_put(nskb
, 2);
246 | ((scb
->id
<< 2) & 0x3c)
247 | ((skb
->len
== size
) ? 0x00 : 0x01);
250 hdr
= skb_put(nskb
, 3);
252 | ((scb
->id
<< 2) & 0x3c)
253 | ((skb
->len
== size
) ? 0x00 : 0x01);
254 hdr
[1] = size
& 0xff;
258 skb_copy_from_linear_data(skb
, skb_put(nskb
, size
), size
);
262 skb_queue_head(&session
->transmit
, skb
);
264 cmtp_free_block_id(session
, scb
->id
);
266 cmtp_send_frame(session
, nskb
->data
, nskb
->len
);
273 cmtp_send_frame(session
, nskb
->data
, nskb
->len
);
278 static int cmtp_session(void *arg
)
280 struct cmtp_session
*session
= arg
;
281 struct sock
*sk
= session
->sock
->sk
;
285 BT_DBG("session %p", session
);
287 set_user_nice(current
, -15);
289 init_waitqueue_entry(&wait
, current
);
290 add_wait_queue(sk_sleep(sk
), &wait
);
292 set_current_state(TASK_INTERRUPTIBLE
);
294 if (atomic_read(&session
->terminate
))
296 if (sk
->sk_state
!= BT_CONNECTED
)
299 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
))) {
301 if (!skb_linearize(skb
))
302 cmtp_recv_frame(session
, skb
);
307 cmtp_process_transmit(session
);
311 __set_current_state(TASK_RUNNING
);
312 remove_wait_queue(sk_sleep(sk
), &wait
);
314 down_write(&cmtp_session_sem
);
316 if (!(session
->flags
& (1 << CMTP_LOOPBACK
)))
317 cmtp_detach_device(session
);
319 fput(session
->sock
->file
);
321 __cmtp_unlink_session(session
);
323 up_write(&cmtp_session_sem
);
326 module_put_and_exit(0);
330 int cmtp_add_connection(struct cmtp_connadd_req
*req
, struct socket
*sock
)
332 struct cmtp_session
*session
, *s
;
337 session
= kzalloc(sizeof(struct cmtp_session
), GFP_KERNEL
);
341 down_write(&cmtp_session_sem
);
343 s
= __cmtp_get_session(&l2cap_pi(sock
->sk
)->chan
->dst
);
344 if (s
&& s
->state
== BT_CONNECTED
) {
349 bacpy(&session
->bdaddr
, &l2cap_pi(sock
->sk
)->chan
->dst
);
351 session
->mtu
= min_t(uint
, l2cap_pi(sock
->sk
)->chan
->omtu
,
352 l2cap_pi(sock
->sk
)->chan
->imtu
);
354 BT_DBG("mtu %d", session
->mtu
);
356 sprintf(session
->name
, "%pMR", &session
->bdaddr
);
358 session
->sock
= sock
;
359 session
->state
= BT_CONFIG
;
361 init_waitqueue_head(&session
->wait
);
363 session
->msgnum
= CMTP_INITIAL_MSGNUM
;
365 INIT_LIST_HEAD(&session
->applications
);
367 skb_queue_head_init(&session
->transmit
);
369 for (i
= 0; i
< 16; i
++)
370 session
->reassembly
[i
] = NULL
;
372 session
->flags
= req
->flags
;
374 __cmtp_link_session(session
);
376 __module_get(THIS_MODULE
);
377 session
->task
= kthread_run(cmtp_session
, session
, "kcmtpd_ctr_%d",
379 if (IS_ERR(session
->task
)) {
380 module_put(THIS_MODULE
);
381 err
= PTR_ERR(session
->task
);
385 if (!(session
->flags
& (1 << CMTP_LOOPBACK
))) {
386 err
= cmtp_attach_device(session
);
388 atomic_inc(&session
->terminate
);
389 wake_up_process(session
->task
);
390 up_write(&cmtp_session_sem
);
395 up_write(&cmtp_session_sem
);
399 __cmtp_unlink_session(session
);
402 up_write(&cmtp_session_sem
);
407 int cmtp_del_connection(struct cmtp_conndel_req
*req
)
409 struct cmtp_session
*session
;
414 down_read(&cmtp_session_sem
);
416 session
= __cmtp_get_session(&req
->bdaddr
);
418 /* Flush the transmit queue */
419 skb_queue_purge(&session
->transmit
);
421 /* Stop session thread */
422 atomic_inc(&session
->terminate
);
423 wake_up_process(session
->task
);
427 up_read(&cmtp_session_sem
);
431 int cmtp_get_connlist(struct cmtp_connlist_req
*req
)
433 struct cmtp_session
*session
;
438 down_read(&cmtp_session_sem
);
440 list_for_each_entry(session
, &cmtp_session_list
, list
) {
441 struct cmtp_conninfo ci
;
443 __cmtp_copy_session(session
, &ci
);
445 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
450 if (++n
>= req
->cnum
)
457 up_read(&cmtp_session_sem
);
461 int cmtp_get_conninfo(struct cmtp_conninfo
*ci
)
463 struct cmtp_session
*session
;
466 down_read(&cmtp_session_sem
);
468 session
= __cmtp_get_session(&ci
->bdaddr
);
470 __cmtp_copy_session(session
, ci
);
474 up_read(&cmtp_session_sem
);
479 static int __init
cmtp_init(void)
481 BT_INFO("CMTP (CAPI Emulation) ver %s", VERSION
);
488 static void __exit
cmtp_exit(void)
490 cmtp_cleanup_sockets();
493 module_init(cmtp_init
);
494 module_exit(cmtp_exit
);
496 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
497 MODULE_DESCRIPTION("Bluetooth CMTP ver " VERSION
);
498 MODULE_VERSION(VERSION
);
499 MODULE_LICENSE("GPL");
500 MODULE_ALIAS("bt-proto-5");