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
;
60 list_for_each(p
, &cmtp_session_list
) {
61 session
= list_entry(p
, struct cmtp_session
, list
);
62 if (!bacmp(bdaddr
, &session
->bdaddr
))
68 static void __cmtp_link_session(struct cmtp_session
*session
)
70 __module_get(THIS_MODULE
);
71 list_add(&session
->list
, &cmtp_session_list
);
74 static void __cmtp_unlink_session(struct cmtp_session
*session
)
76 list_del(&session
->list
);
77 module_put(THIS_MODULE
);
80 static void __cmtp_copy_session(struct cmtp_session
*session
, struct cmtp_conninfo
*ci
)
82 memset(ci
, 0, sizeof(*ci
));
83 bacpy(&ci
->bdaddr
, &session
->bdaddr
);
85 ci
->flags
= session
->flags
;
86 ci
->state
= session
->state
;
88 ci
->num
= session
->num
;
92 static inline int cmtp_alloc_block_id(struct cmtp_session
*session
)
96 for (i
= 0; i
< 16; i
++)
97 if (!test_and_set_bit(i
, &session
->blockids
)) {
105 static inline void cmtp_free_block_id(struct cmtp_session
*session
, int id
)
107 clear_bit(id
, &session
->blockids
);
110 static inline void cmtp_add_msgpart(struct cmtp_session
*session
, int id
, const unsigned char *buf
, int count
)
112 struct sk_buff
*skb
= session
->reassembly
[id
], *nskb
;
115 BT_DBG("session %p buf %p count %d", session
, buf
, count
);
117 size
= (skb
) ? skb
->len
+ count
: count
;
119 nskb
= alloc_skb(size
, GFP_ATOMIC
);
121 BT_ERR("Can't allocate memory for CAPI message");
125 if (skb
&& (skb
->len
> 0))
126 skb_copy_from_linear_data(skb
, skb_put(nskb
, skb
->len
), skb
->len
);
128 memcpy(skb_put(nskb
, count
), buf
, count
);
130 session
->reassembly
[id
] = nskb
;
135 static inline int cmtp_recv_frame(struct cmtp_session
*session
, struct sk_buff
*skb
)
137 __u8 hdr
, hdrlen
, id
;
140 BT_DBG("session %p skb %p len %d", session
, skb
, skb
->len
);
142 while (skb
->len
> 0) {
145 switch (hdr
& 0xc0) {
152 len
= skb
->data
[1] | (skb
->data
[2] << 8);
160 id
= (hdr
& 0x3c) >> 2;
162 BT_DBG("hdr 0x%02x hdrlen %d len %d id %d", hdr
, hdrlen
, len
, id
);
164 if (hdrlen
+ len
> skb
->len
) {
165 BT_ERR("Wrong size or header information in CMTP frame");
170 skb_pull(skb
, hdrlen
);
174 switch (hdr
& 0x03) {
176 cmtp_add_msgpart(session
, id
, skb
->data
+ hdrlen
, len
);
177 cmtp_recv_capimsg(session
, session
->reassembly
[id
]);
178 session
->reassembly
[id
] = NULL
;
181 cmtp_add_msgpart(session
, id
, skb
->data
+ hdrlen
, len
);
184 if (session
->reassembly
[id
] != NULL
)
185 kfree_skb(session
->reassembly
[id
]);
186 session
->reassembly
[id
] = NULL
;
190 skb_pull(skb
, hdrlen
+ len
);
197 static int cmtp_send_frame(struct cmtp_session
*session
, unsigned char *data
, int len
)
199 struct socket
*sock
= session
->sock
;
200 struct kvec iv
= { data
, len
};
203 BT_DBG("session %p data %p len %d", session
, data
, len
);
208 memset(&msg
, 0, sizeof(msg
));
210 return kernel_sendmsg(sock
, &msg
, &iv
, 1, len
);
213 static void cmtp_process_transmit(struct cmtp_session
*session
)
215 struct sk_buff
*skb
, *nskb
;
217 unsigned int size
, tail
;
219 BT_DBG("session %p", session
);
221 nskb
= alloc_skb(session
->mtu
, GFP_ATOMIC
);
223 BT_ERR("Can't allocate memory for new frame");
227 while ((skb
= skb_dequeue(&session
->transmit
))) {
228 struct cmtp_scb
*scb
= (void *) skb
->cb
;
230 tail
= session
->mtu
- nskb
->len
;
232 cmtp_send_frame(session
, nskb
->data
, nskb
->len
);
237 size
= min_t(uint
, ((tail
< 258) ? (tail
- 2) : (tail
- 3)), skb
->len
);
240 scb
->id
= cmtp_alloc_block_id(session
);
242 skb_queue_head(&session
->transmit
, skb
);
248 hdr
= skb_put(nskb
, 2);
250 | ((scb
->id
<< 2) & 0x3c)
251 | ((skb
->len
== size
) ? 0x00 : 0x01);
254 hdr
= skb_put(nskb
, 3);
256 | ((scb
->id
<< 2) & 0x3c)
257 | ((skb
->len
== size
) ? 0x00 : 0x01);
258 hdr
[1] = size
& 0xff;
262 skb_copy_from_linear_data(skb
, skb_put(nskb
, size
), size
);
266 skb_queue_head(&session
->transmit
, skb
);
268 cmtp_free_block_id(session
, scb
->id
);
270 cmtp_send_frame(session
, nskb
->data
, nskb
->len
);
277 cmtp_send_frame(session
, nskb
->data
, nskb
->len
);
282 static int cmtp_session(void *arg
)
284 struct cmtp_session
*session
= arg
;
285 struct sock
*sk
= session
->sock
->sk
;
289 BT_DBG("session %p", session
);
291 set_user_nice(current
, -15);
293 init_waitqueue_entry(&wait
, current
);
294 add_wait_queue(sk_sleep(sk
), &wait
);
295 while (!kthread_should_stop()) {
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_sleep(sk
), &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
;
335 session
= kzalloc(sizeof(struct cmtp_session
), GFP_KERNEL
);
339 down_write(&cmtp_session_sem
);
341 s
= __cmtp_get_session(&bt_sk(sock
->sk
)->dst
);
342 if (s
&& s
->state
== BT_CONNECTED
) {
347 bacpy(&session
->bdaddr
, &bt_sk(sock
->sk
)->dst
);
349 session
->mtu
= min_t(uint
, l2cap_pi(sock
->sk
)->chan
->omtu
,
350 l2cap_pi(sock
->sk
)->chan
->imtu
);
352 BT_DBG("mtu %d", session
->mtu
);
354 sprintf(session
->name
, "%s", batostr(&bt_sk(sock
->sk
)->dst
));
356 session
->sock
= sock
;
357 session
->state
= BT_CONFIG
;
359 init_waitqueue_head(&session
->wait
);
361 session
->msgnum
= CMTP_INITIAL_MSGNUM
;
363 INIT_LIST_HEAD(&session
->applications
);
365 skb_queue_head_init(&session
->transmit
);
367 for (i
= 0; i
< 16; i
++)
368 session
->reassembly
[i
] = NULL
;
370 session
->flags
= req
->flags
;
372 __cmtp_link_session(session
);
374 session
->task
= kthread_run(cmtp_session
, session
, "kcmtpd_ctr_%d",
376 if (IS_ERR(session
->task
)) {
377 err
= PTR_ERR(session
->task
);
381 if (!(session
->flags
& (1 << CMTP_LOOPBACK
))) {
382 err
= cmtp_attach_device(session
);
387 up_write(&cmtp_session_sem
);
391 cmtp_detach_device(session
);
394 __cmtp_unlink_session(session
);
397 up_write(&cmtp_session_sem
);
402 int cmtp_del_connection(struct cmtp_conndel_req
*req
)
404 struct cmtp_session
*session
;
409 down_read(&cmtp_session_sem
);
411 session
= __cmtp_get_session(&req
->bdaddr
);
413 /* Flush the transmit queue */
414 skb_queue_purge(&session
->transmit
);
416 /* Stop session thread */
417 kthread_stop(session
->task
);
421 up_read(&cmtp_session_sem
);
425 int cmtp_get_connlist(struct cmtp_connlist_req
*req
)
432 down_read(&cmtp_session_sem
);
434 list_for_each(p
, &cmtp_session_list
) {
435 struct cmtp_session
*session
;
436 struct cmtp_conninfo ci
;
438 session
= list_entry(p
, struct cmtp_session
, list
);
440 __cmtp_copy_session(session
, &ci
);
442 if (copy_to_user(req
->ci
, &ci
, sizeof(ci
))) {
447 if (++n
>= req
->cnum
)
454 up_read(&cmtp_session_sem
);
458 int cmtp_get_conninfo(struct cmtp_conninfo
*ci
)
460 struct cmtp_session
*session
;
463 down_read(&cmtp_session_sem
);
465 session
= __cmtp_get_session(&ci
->bdaddr
);
467 __cmtp_copy_session(session
, ci
);
471 up_read(&cmtp_session_sem
);
476 static int __init
cmtp_init(void)
478 BT_INFO("CMTP (CAPI Emulation) ver %s", VERSION
);
485 static void __exit
cmtp_exit(void)
487 cmtp_cleanup_sockets();
490 module_init(cmtp_init
);
491 module_exit(cmtp_exit
);
493 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
494 MODULE_DESCRIPTION("Bluetooth CMTP ver " VERSION
);
495 MODULE_VERSION(VERSION
);
496 MODULE_LICENSE("GPL");
497 MODULE_ALIAS("bt-proto-5");