2 RFCOMM implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
4 Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation;
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
25 * Bluetooth RFCOMM core.
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/signal.h>
33 #include <linux/init.h>
34 #include <linux/wait.h>
35 #include <linux/device.h>
36 #include <linux/debugfs.h>
37 #include <linux/seq_file.h>
38 #include <linux/net.h>
39 #include <linux/mutex.h>
40 #include <linux/kthread.h>
41 #include <linux/slab.h>
44 #include <linux/uaccess.h>
45 #include <asm/unaligned.h>
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
49 #include <net/bluetooth/l2cap.h>
50 #include <net/bluetooth/rfcomm.h>
52 #define VERSION "1.11"
54 static int disable_cfc
;
55 static int l2cap_ertm
;
56 static int channel_mtu
= -1;
57 static unsigned int l2cap_mtu
= RFCOMM_MAX_L2CAP_MTU
;
59 static struct task_struct
*rfcomm_thread
;
61 static DEFINE_MUTEX(rfcomm_mutex
);
62 #define rfcomm_lock() mutex_lock(&rfcomm_mutex)
63 #define rfcomm_unlock() mutex_unlock(&rfcomm_mutex)
65 static unsigned long rfcomm_event
;
67 static LIST_HEAD(session_list
);
69 static int rfcomm_send_frame(struct rfcomm_session
*s
, u8
*data
, int len
);
70 static int rfcomm_send_sabm(struct rfcomm_session
*s
, u8 dlci
);
71 static int rfcomm_send_disc(struct rfcomm_session
*s
, u8 dlci
);
72 static int rfcomm_queue_disc(struct rfcomm_dlc
*d
);
73 static int rfcomm_send_nsc(struct rfcomm_session
*s
, int cr
, u8 type
);
74 static int rfcomm_send_pn(struct rfcomm_session
*s
, int cr
, struct rfcomm_dlc
*d
);
75 static int rfcomm_send_msc(struct rfcomm_session
*s
, int cr
, u8 dlci
, u8 v24_sig
);
76 static int rfcomm_send_test(struct rfcomm_session
*s
, int cr
, u8
*pattern
, int len
);
77 static int rfcomm_send_credits(struct rfcomm_session
*s
, u8 addr
, u8 credits
);
78 static void rfcomm_make_uih(struct sk_buff
*skb
, u8 addr
);
80 static void rfcomm_process_connect(struct rfcomm_session
*s
);
82 static struct rfcomm_session
*rfcomm_session_create(bdaddr_t
*src
,
86 static struct rfcomm_session
*rfcomm_session_get(bdaddr_t
*src
, bdaddr_t
*dst
);
87 static void rfcomm_session_del(struct rfcomm_session
*s
);
89 /* ---- RFCOMM frame parsing macros ---- */
90 #define __get_dlci(b) ((b & 0xfc) >> 2)
91 #define __get_channel(b) ((b & 0xf8) >> 3)
92 #define __get_dir(b) ((b & 0x04) >> 2)
93 #define __get_type(b) ((b & 0xef))
95 #define __test_ea(b) ((b & 0x01))
96 #define __test_cr(b) ((b & 0x02))
97 #define __test_pf(b) ((b & 0x10))
99 #define __addr(cr, dlci) (((dlci & 0x3f) << 2) | (cr << 1) | 0x01)
100 #define __ctrl(type, pf) (((type & 0xef) | (pf << 4)))
101 #define __dlci(dir, chn) (((chn & 0x1f) << 1) | dir)
102 #define __srv_channel(dlci) (dlci >> 1)
103 #define __dir(dlci) (dlci & 0x01)
105 #define __len8(len) (((len) << 1) | 1)
106 #define __len16(len) ((len) << 1)
109 #define __mcc_type(cr, type) (((type << 2) | (cr << 1) | 0x01))
110 #define __get_mcc_type(b) ((b & 0xfc) >> 2)
111 #define __get_mcc_len(b) ((b & 0xfe) >> 1)
114 #define __rpn_line_settings(data, stop, parity) ((data & 0x3) | ((stop & 0x1) << 2) | ((parity & 0x7) << 3))
115 #define __get_rpn_data_bits(line) ((line) & 0x3)
116 #define __get_rpn_stop_bits(line) (((line) >> 2) & 0x1)
117 #define __get_rpn_parity(line) (((line) >> 3) & 0x7)
119 static inline void rfcomm_schedule(void)
123 set_bit(RFCOMM_SCHED_WAKEUP
, &rfcomm_event
);
124 wake_up_process(rfcomm_thread
);
127 static inline void rfcomm_session_put(struct rfcomm_session
*s
)
129 if (atomic_dec_and_test(&s
->refcnt
))
130 rfcomm_session_del(s
);
133 /* ---- RFCOMM FCS computation ---- */
135 /* reversed, 8-bit, poly=0x07 */
136 static unsigned char rfcomm_crc_table
[256] = {
137 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
138 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
139 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
140 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
142 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
143 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
144 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
145 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
147 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
148 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
149 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
150 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
152 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
153 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
154 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
155 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
157 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
158 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
159 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
160 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
162 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
163 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
164 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
165 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
167 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
168 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
169 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
170 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
172 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
173 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
174 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
175 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
179 #define __crc(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]])
182 static inline u8
__fcs(u8
*data
)
184 return 0xff - __crc(data
);
188 static inline u8
__fcs2(u8
*data
)
190 return 0xff - rfcomm_crc_table
[__crc(data
) ^ data
[2]];
194 static inline int __check_fcs(u8
*data
, int type
, u8 fcs
)
198 if (type
!= RFCOMM_UIH
)
199 f
= rfcomm_crc_table
[f
^ data
[2]];
201 return rfcomm_crc_table
[f
^ fcs
] != 0xcf;
204 /* ---- L2CAP callbacks ---- */
205 static void rfcomm_l2state_change(struct sock
*sk
)
207 BT_DBG("%p state %d", sk
, sk
->sk_state
);
211 static void rfcomm_l2data_ready(struct sock
*sk
, int bytes
)
213 BT_DBG("%p bytes %d", sk
, bytes
);
217 static int rfcomm_l2sock_create(struct socket
**sock
)
223 err
= sock_create_kern(PF_BLUETOOTH
, SOCK_SEQPACKET
, BTPROTO_L2CAP
, sock
);
225 struct sock
*sk
= (*sock
)->sk
;
226 sk
->sk_data_ready
= rfcomm_l2data_ready
;
227 sk
->sk_state_change
= rfcomm_l2state_change
;
232 static inline int rfcomm_check_security(struct rfcomm_dlc
*d
)
234 struct sock
*sk
= d
->session
->sock
->sk
;
237 switch (d
->sec_level
) {
238 case BT_SECURITY_HIGH
:
239 auth_type
= HCI_AT_GENERAL_BONDING_MITM
;
241 case BT_SECURITY_MEDIUM
:
242 auth_type
= HCI_AT_GENERAL_BONDING
;
245 auth_type
= HCI_AT_NO_BONDING
;
249 return hci_conn_security(l2cap_pi(sk
)->conn
->hcon
, d
->sec_level
,
253 static void rfcomm_session_timeout(unsigned long arg
)
255 struct rfcomm_session
*s
= (void *) arg
;
257 BT_DBG("session %p state %ld", s
, s
->state
);
259 set_bit(RFCOMM_TIMED_OUT
, &s
->flags
);
263 static void rfcomm_session_set_timer(struct rfcomm_session
*s
, long timeout
)
265 BT_DBG("session %p state %ld timeout %ld", s
, s
->state
, timeout
);
267 if (!mod_timer(&s
->timer
, jiffies
+ timeout
))
268 rfcomm_session_hold(s
);
271 static void rfcomm_session_clear_timer(struct rfcomm_session
*s
)
273 BT_DBG("session %p state %ld", s
, s
->state
);
275 if (timer_pending(&s
->timer
) && del_timer(&s
->timer
))
276 rfcomm_session_put(s
);
279 /* ---- RFCOMM DLCs ---- */
280 static void rfcomm_dlc_timeout(unsigned long arg
)
282 struct rfcomm_dlc
*d
= (void *) arg
;
284 BT_DBG("dlc %p state %ld", d
, d
->state
);
286 set_bit(RFCOMM_TIMED_OUT
, &d
->flags
);
291 static void rfcomm_dlc_set_timer(struct rfcomm_dlc
*d
, long timeout
)
293 BT_DBG("dlc %p state %ld timeout %ld", d
, d
->state
, timeout
);
295 if (!mod_timer(&d
->timer
, jiffies
+ timeout
))
299 static void rfcomm_dlc_clear_timer(struct rfcomm_dlc
*d
)
301 BT_DBG("dlc %p state %ld", d
, d
->state
);
303 if (timer_pending(&d
->timer
) && del_timer(&d
->timer
))
307 static void rfcomm_dlc_clear_state(struct rfcomm_dlc
*d
)
314 d
->sec_level
= BT_SECURITY_LOW
;
315 d
->mtu
= RFCOMM_DEFAULT_MTU
;
316 d
->v24_sig
= RFCOMM_V24_RTC
| RFCOMM_V24_RTR
| RFCOMM_V24_DV
;
318 d
->cfc
= RFCOMM_CFC_DISABLED
;
319 d
->rx_credits
= RFCOMM_DEFAULT_CREDITS
;
322 struct rfcomm_dlc
*rfcomm_dlc_alloc(gfp_t prio
)
324 struct rfcomm_dlc
*d
= kzalloc(sizeof(*d
), prio
);
329 setup_timer(&d
->timer
, rfcomm_dlc_timeout
, (unsigned long)d
);
331 skb_queue_head_init(&d
->tx_queue
);
332 spin_lock_init(&d
->lock
);
333 atomic_set(&d
->refcnt
, 1);
335 rfcomm_dlc_clear_state(d
);
342 void rfcomm_dlc_free(struct rfcomm_dlc
*d
)
346 skb_queue_purge(&d
->tx_queue
);
350 static void rfcomm_dlc_link(struct rfcomm_session
*s
, struct rfcomm_dlc
*d
)
352 BT_DBG("dlc %p session %p", d
, s
);
354 rfcomm_session_hold(s
);
356 rfcomm_session_clear_timer(s
);
358 list_add(&d
->list
, &s
->dlcs
);
362 static void rfcomm_dlc_unlink(struct rfcomm_dlc
*d
)
364 struct rfcomm_session
*s
= d
->session
;
366 BT_DBG("dlc %p refcnt %d session %p", d
, atomic_read(&d
->refcnt
), s
);
372 if (list_empty(&s
->dlcs
))
373 rfcomm_session_set_timer(s
, RFCOMM_IDLE_TIMEOUT
);
375 rfcomm_session_put(s
);
378 static struct rfcomm_dlc
*rfcomm_dlc_get(struct rfcomm_session
*s
, u8 dlci
)
380 struct rfcomm_dlc
*d
;
383 list_for_each(p
, &s
->dlcs
) {
384 d
= list_entry(p
, struct rfcomm_dlc
, list
);
391 static int __rfcomm_dlc_open(struct rfcomm_dlc
*d
, bdaddr_t
*src
, bdaddr_t
*dst
, u8 channel
)
393 struct rfcomm_session
*s
;
397 BT_DBG("dlc %p state %ld %s %s channel %d",
398 d
, d
->state
, batostr(src
), batostr(dst
), channel
);
400 if (channel
< 1 || channel
> 30)
403 if (d
->state
!= BT_OPEN
&& d
->state
!= BT_CLOSED
)
406 s
= rfcomm_session_get(src
, dst
);
408 s
= rfcomm_session_create(src
, dst
, d
->sec_level
, &err
);
413 dlci
= __dlci(!s
->initiator
, channel
);
415 /* Check if DLCI already exists */
416 if (rfcomm_dlc_get(s
, dlci
))
419 rfcomm_dlc_clear_state(d
);
422 d
->addr
= __addr(s
->initiator
, dlci
);
425 d
->state
= BT_CONFIG
;
426 rfcomm_dlc_link(s
, d
);
431 d
->cfc
= (s
->cfc
== RFCOMM_CFC_UNKNOWN
) ? 0 : s
->cfc
;
433 if (s
->state
== BT_CONNECTED
) {
434 if (rfcomm_check_security(d
))
435 rfcomm_send_pn(s
, 1, d
);
437 set_bit(RFCOMM_AUTH_PENDING
, &d
->flags
);
440 rfcomm_dlc_set_timer(d
, RFCOMM_CONN_TIMEOUT
);
445 int rfcomm_dlc_open(struct rfcomm_dlc
*d
, bdaddr_t
*src
, bdaddr_t
*dst
, u8 channel
)
451 r
= __rfcomm_dlc_open(d
, src
, dst
, channel
);
457 static int __rfcomm_dlc_close(struct rfcomm_dlc
*d
, int err
)
459 struct rfcomm_session
*s
= d
->session
;
463 BT_DBG("dlc %p state %ld dlci %d err %d session %p",
464 d
, d
->state
, d
->dlci
, err
, s
);
469 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
470 set_bit(RFCOMM_AUTH_REJECT
, &d
->flags
);
477 d
->state
= BT_DISCONN
;
478 if (skb_queue_empty(&d
->tx_queue
)) {
479 rfcomm_send_disc(s
, d
->dlci
);
480 rfcomm_dlc_set_timer(d
, RFCOMM_DISC_TIMEOUT
);
482 rfcomm_queue_disc(d
);
483 rfcomm_dlc_set_timer(d
, RFCOMM_DISC_TIMEOUT
* 2);
489 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
490 set_bit(RFCOMM_AUTH_REJECT
, &d
->flags
);
497 rfcomm_dlc_clear_timer(d
);
500 d
->state
= BT_CLOSED
;
501 d
->state_change(d
, err
);
502 rfcomm_dlc_unlock(d
);
504 skb_queue_purge(&d
->tx_queue
);
505 rfcomm_dlc_unlink(d
);
511 int rfcomm_dlc_close(struct rfcomm_dlc
*d
, int err
)
517 r
= __rfcomm_dlc_close(d
, err
);
523 int rfcomm_dlc_send(struct rfcomm_dlc
*d
, struct sk_buff
*skb
)
527 if (d
->state
!= BT_CONNECTED
)
530 BT_DBG("dlc %p mtu %d len %d", d
, d
->mtu
, len
);
535 rfcomm_make_uih(skb
, d
->addr
);
536 skb_queue_tail(&d
->tx_queue
, skb
);
538 if (!test_bit(RFCOMM_TX_THROTTLED
, &d
->flags
))
543 void __rfcomm_dlc_throttle(struct rfcomm_dlc
*d
)
545 BT_DBG("dlc %p state %ld", d
, d
->state
);
548 d
->v24_sig
|= RFCOMM_V24_FC
;
549 set_bit(RFCOMM_MSC_PENDING
, &d
->flags
);
554 void __rfcomm_dlc_unthrottle(struct rfcomm_dlc
*d
)
556 BT_DBG("dlc %p state %ld", d
, d
->state
);
559 d
->v24_sig
&= ~RFCOMM_V24_FC
;
560 set_bit(RFCOMM_MSC_PENDING
, &d
->flags
);
566 Set/get modem status functions use _local_ status i.e. what we report
568 Remote status is provided by dlc->modem_status() callback.
570 int rfcomm_dlc_set_modem_status(struct rfcomm_dlc
*d
, u8 v24_sig
)
572 BT_DBG("dlc %p state %ld v24_sig 0x%x",
573 d
, d
->state
, v24_sig
);
575 if (test_bit(RFCOMM_RX_THROTTLED
, &d
->flags
))
576 v24_sig
|= RFCOMM_V24_FC
;
578 v24_sig
&= ~RFCOMM_V24_FC
;
580 d
->v24_sig
= v24_sig
;
582 if (!test_and_set_bit(RFCOMM_MSC_PENDING
, &d
->flags
))
588 int rfcomm_dlc_get_modem_status(struct rfcomm_dlc
*d
, u8
*v24_sig
)
590 BT_DBG("dlc %p state %ld v24_sig 0x%x",
591 d
, d
->state
, d
->v24_sig
);
593 *v24_sig
= d
->v24_sig
;
597 /* ---- RFCOMM sessions ---- */
598 static struct rfcomm_session
*rfcomm_session_add(struct socket
*sock
, int state
)
600 struct rfcomm_session
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
605 BT_DBG("session %p sock %p", s
, sock
);
607 setup_timer(&s
->timer
, rfcomm_session_timeout
, (unsigned long) s
);
609 INIT_LIST_HEAD(&s
->dlcs
);
613 s
->mtu
= RFCOMM_DEFAULT_MTU
;
614 s
->cfc
= disable_cfc
? RFCOMM_CFC_DISABLED
: RFCOMM_CFC_UNKNOWN
;
616 /* Do not increment module usage count for listening sessions.
617 * Otherwise we won't be able to unload the module. */
618 if (state
!= BT_LISTEN
)
619 if (!try_module_get(THIS_MODULE
)) {
624 list_add(&s
->list
, &session_list
);
629 static void rfcomm_session_del(struct rfcomm_session
*s
)
631 int state
= s
->state
;
633 BT_DBG("session %p state %ld", s
, s
->state
);
637 if (state
== BT_CONNECTED
)
638 rfcomm_send_disc(s
, 0);
640 rfcomm_session_clear_timer(s
);
641 sock_release(s
->sock
);
644 if (state
!= BT_LISTEN
)
645 module_put(THIS_MODULE
);
648 static struct rfcomm_session
*rfcomm_session_get(bdaddr_t
*src
, bdaddr_t
*dst
)
650 struct rfcomm_session
*s
;
651 struct list_head
*p
, *n
;
653 list_for_each_safe(p
, n
, &session_list
) {
654 s
= list_entry(p
, struct rfcomm_session
, list
);
655 sk
= bt_sk(s
->sock
->sk
);
657 if ((!bacmp(src
, BDADDR_ANY
) || !bacmp(&sk
->src
, src
)) &&
658 !bacmp(&sk
->dst
, dst
))
664 static void rfcomm_session_close(struct rfcomm_session
*s
, int err
)
666 struct rfcomm_dlc
*d
;
667 struct list_head
*p
, *n
;
669 BT_DBG("session %p state %ld err %d", s
, s
->state
, err
);
671 rfcomm_session_hold(s
);
673 s
->state
= BT_CLOSED
;
676 list_for_each_safe(p
, n
, &s
->dlcs
) {
677 d
= list_entry(p
, struct rfcomm_dlc
, list
);
678 d
->state
= BT_CLOSED
;
679 __rfcomm_dlc_close(d
, err
);
682 rfcomm_session_clear_timer(s
);
683 rfcomm_session_put(s
);
686 static struct rfcomm_session
*rfcomm_session_create(bdaddr_t
*src
,
691 struct rfcomm_session
*s
= NULL
;
692 struct sockaddr_l2 addr
;
696 BT_DBG("%s %s", batostr(src
), batostr(dst
));
698 *err
= rfcomm_l2sock_create(&sock
);
702 bacpy(&addr
.l2_bdaddr
, src
);
703 addr
.l2_family
= AF_BLUETOOTH
;
706 *err
= kernel_bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
710 /* Set L2CAP options */
713 l2cap_pi(sk
)->imtu
= l2cap_mtu
;
714 l2cap_pi(sk
)->sec_level
= sec_level
;
716 l2cap_pi(sk
)->mode
= L2CAP_MODE_ERTM
;
719 s
= rfcomm_session_add(sock
, BT_BOUND
);
727 bacpy(&addr
.l2_bdaddr
, dst
);
728 addr
.l2_family
= AF_BLUETOOTH
;
729 addr
.l2_psm
= cpu_to_le16(RFCOMM_PSM
);
731 *err
= kernel_connect(sock
, (struct sockaddr
*) &addr
, sizeof(addr
), O_NONBLOCK
);
732 if (*err
== 0 || *err
== -EINPROGRESS
)
735 rfcomm_session_del(s
);
743 void rfcomm_session_getaddr(struct rfcomm_session
*s
, bdaddr_t
*src
, bdaddr_t
*dst
)
745 struct sock
*sk
= s
->sock
->sk
;
747 bacpy(src
, &bt_sk(sk
)->src
);
749 bacpy(dst
, &bt_sk(sk
)->dst
);
752 /* ---- RFCOMM frame sending ---- */
753 static int rfcomm_send_frame(struct rfcomm_session
*s
, u8
*data
, int len
)
755 struct socket
*sock
= s
->sock
;
756 struct kvec iv
= { data
, len
};
759 BT_DBG("session %p len %d", s
, len
);
761 memset(&msg
, 0, sizeof(msg
));
763 return kernel_sendmsg(sock
, &msg
, &iv
, 1, len
);
766 static int rfcomm_send_sabm(struct rfcomm_session
*s
, u8 dlci
)
768 struct rfcomm_cmd cmd
;
770 BT_DBG("%p dlci %d", s
, dlci
);
772 cmd
.addr
= __addr(s
->initiator
, dlci
);
773 cmd
.ctrl
= __ctrl(RFCOMM_SABM
, 1);
775 cmd
.fcs
= __fcs2((u8
*) &cmd
);
777 return rfcomm_send_frame(s
, (void *) &cmd
, sizeof(cmd
));
780 static int rfcomm_send_ua(struct rfcomm_session
*s
, u8 dlci
)
782 struct rfcomm_cmd cmd
;
784 BT_DBG("%p dlci %d", s
, dlci
);
786 cmd
.addr
= __addr(!s
->initiator
, dlci
);
787 cmd
.ctrl
= __ctrl(RFCOMM_UA
, 1);
789 cmd
.fcs
= __fcs2((u8
*) &cmd
);
791 return rfcomm_send_frame(s
, (void *) &cmd
, sizeof(cmd
));
794 static int rfcomm_send_disc(struct rfcomm_session
*s
, u8 dlci
)
796 struct rfcomm_cmd cmd
;
798 BT_DBG("%p dlci %d", s
, dlci
);
800 cmd
.addr
= __addr(s
->initiator
, dlci
);
801 cmd
.ctrl
= __ctrl(RFCOMM_DISC
, 1);
803 cmd
.fcs
= __fcs2((u8
*) &cmd
);
805 return rfcomm_send_frame(s
, (void *) &cmd
, sizeof(cmd
));
808 static int rfcomm_queue_disc(struct rfcomm_dlc
*d
)
810 struct rfcomm_cmd
*cmd
;
813 BT_DBG("dlc %p dlci %d", d
, d
->dlci
);
815 skb
= alloc_skb(sizeof(*cmd
), GFP_KERNEL
);
819 cmd
= (void *) __skb_put(skb
, sizeof(*cmd
));
821 cmd
->ctrl
= __ctrl(RFCOMM_DISC
, 1);
822 cmd
->len
= __len8(0);
823 cmd
->fcs
= __fcs2((u8
*) cmd
);
825 skb_queue_tail(&d
->tx_queue
, skb
);
830 static int rfcomm_send_dm(struct rfcomm_session
*s
, u8 dlci
)
832 struct rfcomm_cmd cmd
;
834 BT_DBG("%p dlci %d", s
, dlci
);
836 cmd
.addr
= __addr(!s
->initiator
, dlci
);
837 cmd
.ctrl
= __ctrl(RFCOMM_DM
, 1);
839 cmd
.fcs
= __fcs2((u8
*) &cmd
);
841 return rfcomm_send_frame(s
, (void *) &cmd
, sizeof(cmd
));
844 static int rfcomm_send_nsc(struct rfcomm_session
*s
, int cr
, u8 type
)
846 struct rfcomm_hdr
*hdr
;
847 struct rfcomm_mcc
*mcc
;
848 u8 buf
[16], *ptr
= buf
;
850 BT_DBG("%p cr %d type %d", s
, cr
, type
);
852 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
853 hdr
->addr
= __addr(s
->initiator
, 0);
854 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
855 hdr
->len
= __len8(sizeof(*mcc
) + 1);
857 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
858 mcc
->type
= __mcc_type(cr
, RFCOMM_NSC
);
859 mcc
->len
= __len8(1);
861 /* Type that we didn't like */
862 *ptr
= __mcc_type(cr
, type
); ptr
++;
864 *ptr
= __fcs(buf
); ptr
++;
866 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
869 static int rfcomm_send_pn(struct rfcomm_session
*s
, int cr
, struct rfcomm_dlc
*d
)
871 struct rfcomm_hdr
*hdr
;
872 struct rfcomm_mcc
*mcc
;
873 struct rfcomm_pn
*pn
;
874 u8 buf
[16], *ptr
= buf
;
876 BT_DBG("%p cr %d dlci %d mtu %d", s
, cr
, d
->dlci
, d
->mtu
);
878 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
879 hdr
->addr
= __addr(s
->initiator
, 0);
880 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
881 hdr
->len
= __len8(sizeof(*mcc
) + sizeof(*pn
));
883 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
884 mcc
->type
= __mcc_type(cr
, RFCOMM_PN
);
885 mcc
->len
= __len8(sizeof(*pn
));
887 pn
= (void *) ptr
; ptr
+= sizeof(*pn
);
889 pn
->priority
= d
->priority
;
894 pn
->flow_ctrl
= cr
? 0xf0 : 0xe0;
895 pn
->credits
= RFCOMM_DEFAULT_CREDITS
;
901 if (cr
&& channel_mtu
>= 0)
902 pn
->mtu
= cpu_to_le16(channel_mtu
);
904 pn
->mtu
= cpu_to_le16(d
->mtu
);
906 *ptr
= __fcs(buf
); ptr
++;
908 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
911 int rfcomm_send_rpn(struct rfcomm_session
*s
, int cr
, u8 dlci
,
912 u8 bit_rate
, u8 data_bits
, u8 stop_bits
,
913 u8 parity
, u8 flow_ctrl_settings
,
914 u8 xon_char
, u8 xoff_char
, u16 param_mask
)
916 struct rfcomm_hdr
*hdr
;
917 struct rfcomm_mcc
*mcc
;
918 struct rfcomm_rpn
*rpn
;
919 u8 buf
[16], *ptr
= buf
;
921 BT_DBG("%p cr %d dlci %d bit_r 0x%x data_b 0x%x stop_b 0x%x parity 0x%x"
922 " flwc_s 0x%x xon_c 0x%x xoff_c 0x%x p_mask 0x%x",
923 s
, cr
, dlci
, bit_rate
, data_bits
, stop_bits
, parity
,
924 flow_ctrl_settings
, xon_char
, xoff_char
, param_mask
);
926 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
927 hdr
->addr
= __addr(s
->initiator
, 0);
928 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
929 hdr
->len
= __len8(sizeof(*mcc
) + sizeof(*rpn
));
931 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
932 mcc
->type
= __mcc_type(cr
, RFCOMM_RPN
);
933 mcc
->len
= __len8(sizeof(*rpn
));
935 rpn
= (void *) ptr
; ptr
+= sizeof(*rpn
);
936 rpn
->dlci
= __addr(1, dlci
);
937 rpn
->bit_rate
= bit_rate
;
938 rpn
->line_settings
= __rpn_line_settings(data_bits
, stop_bits
, parity
);
939 rpn
->flow_ctrl
= flow_ctrl_settings
;
940 rpn
->xon_char
= xon_char
;
941 rpn
->xoff_char
= xoff_char
;
942 rpn
->param_mask
= cpu_to_le16(param_mask
);
944 *ptr
= __fcs(buf
); ptr
++;
946 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
949 static int rfcomm_send_rls(struct rfcomm_session
*s
, int cr
, u8 dlci
, u8 status
)
951 struct rfcomm_hdr
*hdr
;
952 struct rfcomm_mcc
*mcc
;
953 struct rfcomm_rls
*rls
;
954 u8 buf
[16], *ptr
= buf
;
956 BT_DBG("%p cr %d status 0x%x", s
, cr
, status
);
958 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
959 hdr
->addr
= __addr(s
->initiator
, 0);
960 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
961 hdr
->len
= __len8(sizeof(*mcc
) + sizeof(*rls
));
963 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
964 mcc
->type
= __mcc_type(cr
, RFCOMM_RLS
);
965 mcc
->len
= __len8(sizeof(*rls
));
967 rls
= (void *) ptr
; ptr
+= sizeof(*rls
);
968 rls
->dlci
= __addr(1, dlci
);
969 rls
->status
= status
;
971 *ptr
= __fcs(buf
); ptr
++;
973 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
976 static int rfcomm_send_msc(struct rfcomm_session
*s
, int cr
, u8 dlci
, u8 v24_sig
)
978 struct rfcomm_hdr
*hdr
;
979 struct rfcomm_mcc
*mcc
;
980 struct rfcomm_msc
*msc
;
981 u8 buf
[16], *ptr
= buf
;
983 BT_DBG("%p cr %d v24 0x%x", s
, cr
, v24_sig
);
985 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
986 hdr
->addr
= __addr(s
->initiator
, 0);
987 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
988 hdr
->len
= __len8(sizeof(*mcc
) + sizeof(*msc
));
990 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
991 mcc
->type
= __mcc_type(cr
, RFCOMM_MSC
);
992 mcc
->len
= __len8(sizeof(*msc
));
994 msc
= (void *) ptr
; ptr
+= sizeof(*msc
);
995 msc
->dlci
= __addr(1, dlci
);
996 msc
->v24_sig
= v24_sig
| 0x01;
998 *ptr
= __fcs(buf
); ptr
++;
1000 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
1003 static int rfcomm_send_fcoff(struct rfcomm_session
*s
, int cr
)
1005 struct rfcomm_hdr
*hdr
;
1006 struct rfcomm_mcc
*mcc
;
1007 u8 buf
[16], *ptr
= buf
;
1009 BT_DBG("%p cr %d", s
, cr
);
1011 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
1012 hdr
->addr
= __addr(s
->initiator
, 0);
1013 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
1014 hdr
->len
= __len8(sizeof(*mcc
));
1016 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
1017 mcc
->type
= __mcc_type(cr
, RFCOMM_FCOFF
);
1018 mcc
->len
= __len8(0);
1020 *ptr
= __fcs(buf
); ptr
++;
1022 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
1025 static int rfcomm_send_fcon(struct rfcomm_session
*s
, int cr
)
1027 struct rfcomm_hdr
*hdr
;
1028 struct rfcomm_mcc
*mcc
;
1029 u8 buf
[16], *ptr
= buf
;
1031 BT_DBG("%p cr %d", s
, cr
);
1033 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
1034 hdr
->addr
= __addr(s
->initiator
, 0);
1035 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
1036 hdr
->len
= __len8(sizeof(*mcc
));
1038 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
1039 mcc
->type
= __mcc_type(cr
, RFCOMM_FCON
);
1040 mcc
->len
= __len8(0);
1042 *ptr
= __fcs(buf
); ptr
++;
1044 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
1047 static int rfcomm_send_test(struct rfcomm_session
*s
, int cr
, u8
*pattern
, int len
)
1049 struct socket
*sock
= s
->sock
;
1052 unsigned char hdr
[5], crc
[1];
1057 BT_DBG("%p cr %d", s
, cr
);
1059 hdr
[0] = __addr(s
->initiator
, 0);
1060 hdr
[1] = __ctrl(RFCOMM_UIH
, 0);
1061 hdr
[2] = 0x01 | ((len
+ 2) << 1);
1062 hdr
[3] = 0x01 | ((cr
& 0x01) << 1) | (RFCOMM_TEST
<< 2);
1063 hdr
[4] = 0x01 | (len
<< 1);
1065 crc
[0] = __fcs(hdr
);
1067 iv
[0].iov_base
= hdr
;
1069 iv
[1].iov_base
= pattern
;
1070 iv
[1].iov_len
= len
;
1071 iv
[2].iov_base
= crc
;
1074 memset(&msg
, 0, sizeof(msg
));
1076 return kernel_sendmsg(sock
, &msg
, iv
, 3, 6 + len
);
1079 static int rfcomm_send_credits(struct rfcomm_session
*s
, u8 addr
, u8 credits
)
1081 struct rfcomm_hdr
*hdr
;
1082 u8 buf
[16], *ptr
= buf
;
1084 BT_DBG("%p addr %d credits %d", s
, addr
, credits
);
1086 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
1088 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 1);
1089 hdr
->len
= __len8(0);
1091 *ptr
= credits
; ptr
++;
1093 *ptr
= __fcs(buf
); ptr
++;
1095 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
1098 static void rfcomm_make_uih(struct sk_buff
*skb
, u8 addr
)
1100 struct rfcomm_hdr
*hdr
;
1105 hdr
= (void *) skb_push(skb
, 4);
1106 put_unaligned(cpu_to_le16(__len16(len
)), (__le16
*) &hdr
->len
);
1108 hdr
= (void *) skb_push(skb
, 3);
1109 hdr
->len
= __len8(len
);
1112 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
1114 crc
= skb_put(skb
, 1);
1115 *crc
= __fcs((void *) hdr
);
1118 /* ---- RFCOMM frame reception ---- */
1119 static int rfcomm_recv_ua(struct rfcomm_session
*s
, u8 dlci
)
1121 BT_DBG("session %p state %ld dlci %d", s
, s
->state
, dlci
);
1125 struct rfcomm_dlc
*d
= rfcomm_dlc_get(s
, dlci
);
1127 rfcomm_send_dm(s
, dlci
);
1133 rfcomm_dlc_clear_timer(d
);
1136 d
->state
= BT_CONNECTED
;
1137 d
->state_change(d
, 0);
1138 rfcomm_dlc_unlock(d
);
1140 rfcomm_send_msc(s
, 1, dlci
, d
->v24_sig
);
1144 d
->state
= BT_CLOSED
;
1145 __rfcomm_dlc_close(d
, 0);
1147 if (list_empty(&s
->dlcs
)) {
1148 s
->state
= BT_DISCONN
;
1149 rfcomm_send_disc(s
, 0);
1155 /* Control channel */
1158 s
->state
= BT_CONNECTED
;
1159 rfcomm_process_connect(s
);
1163 /* When socket is closed and we are not RFCOMM
1164 * initiator rfcomm_process_rx already calls
1165 * rfcomm_session_put() */
1166 if (s
->sock
->sk
->sk_state
!= BT_CLOSED
)
1167 rfcomm_session_put(s
);
1174 static int rfcomm_recv_dm(struct rfcomm_session
*s
, u8 dlci
)
1178 BT_DBG("session %p state %ld dlci %d", s
, s
->state
, dlci
);
1182 struct rfcomm_dlc
*d
= rfcomm_dlc_get(s
, dlci
);
1184 if (d
->state
== BT_CONNECT
|| d
->state
== BT_CONFIG
)
1189 d
->state
= BT_CLOSED
;
1190 __rfcomm_dlc_close(d
, err
);
1193 if (s
->state
== BT_CONNECT
)
1198 s
->state
= BT_CLOSED
;
1199 rfcomm_session_close(s
, err
);
1204 static int rfcomm_recv_disc(struct rfcomm_session
*s
, u8 dlci
)
1208 BT_DBG("session %p state %ld dlci %d", s
, s
->state
, dlci
);
1211 struct rfcomm_dlc
*d
= rfcomm_dlc_get(s
, dlci
);
1213 rfcomm_send_ua(s
, dlci
);
1215 if (d
->state
== BT_CONNECT
|| d
->state
== BT_CONFIG
)
1220 d
->state
= BT_CLOSED
;
1221 __rfcomm_dlc_close(d
, err
);
1223 rfcomm_send_dm(s
, dlci
);
1226 rfcomm_send_ua(s
, 0);
1228 if (s
->state
== BT_CONNECT
)
1233 s
->state
= BT_CLOSED
;
1234 rfcomm_session_close(s
, err
);
1240 void rfcomm_dlc_accept(struct rfcomm_dlc
*d
)
1242 struct sock
*sk
= d
->session
->sock
->sk
;
1244 BT_DBG("dlc %p", d
);
1246 rfcomm_send_ua(d
->session
, d
->dlci
);
1248 rfcomm_dlc_clear_timer(d
);
1251 d
->state
= BT_CONNECTED
;
1252 d
->state_change(d
, 0);
1253 rfcomm_dlc_unlock(d
);
1256 hci_conn_switch_role(l2cap_pi(sk
)->conn
->hcon
, 0x00);
1258 rfcomm_send_msc(d
->session
, 1, d
->dlci
, d
->v24_sig
);
1261 static void rfcomm_check_accept(struct rfcomm_dlc
*d
)
1263 if (rfcomm_check_security(d
)) {
1264 if (d
->defer_setup
) {
1265 set_bit(RFCOMM_DEFER_SETUP
, &d
->flags
);
1266 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
1269 d
->state
= BT_CONNECT2
;
1270 d
->state_change(d
, 0);
1271 rfcomm_dlc_unlock(d
);
1273 rfcomm_dlc_accept(d
);
1275 set_bit(RFCOMM_AUTH_PENDING
, &d
->flags
);
1276 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
1280 static int rfcomm_recv_sabm(struct rfcomm_session
*s
, u8 dlci
)
1282 struct rfcomm_dlc
*d
;
1285 BT_DBG("session %p state %ld dlci %d", s
, s
->state
, dlci
);
1288 rfcomm_send_ua(s
, 0);
1290 if (s
->state
== BT_OPEN
) {
1291 s
->state
= BT_CONNECTED
;
1292 rfcomm_process_connect(s
);
1297 /* Check if DLC exists */
1298 d
= rfcomm_dlc_get(s
, dlci
);
1300 if (d
->state
== BT_OPEN
) {
1301 /* DLC was previously opened by PN request */
1302 rfcomm_check_accept(d
);
1307 /* Notify socket layer about incoming connection */
1308 channel
= __srv_channel(dlci
);
1309 if (rfcomm_connect_ind(s
, channel
, &d
)) {
1311 d
->addr
= __addr(s
->initiator
, dlci
);
1312 rfcomm_dlc_link(s
, d
);
1314 rfcomm_check_accept(d
);
1316 rfcomm_send_dm(s
, dlci
);
1322 static int rfcomm_apply_pn(struct rfcomm_dlc
*d
, int cr
, struct rfcomm_pn
*pn
)
1324 struct rfcomm_session
*s
= d
->session
;
1326 BT_DBG("dlc %p state %ld dlci %d mtu %d fc 0x%x credits %d",
1327 d
, d
->state
, d
->dlci
, pn
->mtu
, pn
->flow_ctrl
, pn
->credits
);
1329 if ((pn
->flow_ctrl
== 0xf0 && s
->cfc
!= RFCOMM_CFC_DISABLED
) ||
1330 pn
->flow_ctrl
== 0xe0) {
1331 d
->cfc
= RFCOMM_CFC_ENABLED
;
1332 d
->tx_credits
= pn
->credits
;
1334 d
->cfc
= RFCOMM_CFC_DISABLED
;
1335 set_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1338 if (s
->cfc
== RFCOMM_CFC_UNKNOWN
)
1341 d
->priority
= pn
->priority
;
1343 d
->mtu
= __le16_to_cpu(pn
->mtu
);
1345 if (cr
&& d
->mtu
> s
->mtu
)
1351 static int rfcomm_recv_pn(struct rfcomm_session
*s
, int cr
, struct sk_buff
*skb
)
1353 struct rfcomm_pn
*pn
= (void *) skb
->data
;
1354 struct rfcomm_dlc
*d
;
1357 BT_DBG("session %p state %ld dlci %d", s
, s
->state
, dlci
);
1362 d
= rfcomm_dlc_get(s
, dlci
);
1366 rfcomm_apply_pn(d
, cr
, pn
);
1367 rfcomm_send_pn(s
, 0, d
);
1372 rfcomm_apply_pn(d
, cr
, pn
);
1374 d
->state
= BT_CONNECT
;
1375 rfcomm_send_sabm(s
, d
->dlci
);
1380 u8 channel
= __srv_channel(dlci
);
1385 /* PN request for non existing DLC.
1386 * Assume incoming connection. */
1387 if (rfcomm_connect_ind(s
, channel
, &d
)) {
1389 d
->addr
= __addr(s
->initiator
, dlci
);
1390 rfcomm_dlc_link(s
, d
);
1392 rfcomm_apply_pn(d
, cr
, pn
);
1395 rfcomm_send_pn(s
, 0, d
);
1397 rfcomm_send_dm(s
, dlci
);
1403 static int rfcomm_recv_rpn(struct rfcomm_session
*s
, int cr
, int len
, struct sk_buff
*skb
)
1405 struct rfcomm_rpn
*rpn
= (void *) skb
->data
;
1406 u8 dlci
= __get_dlci(rpn
->dlci
);
1415 u16 rpn_mask
= RFCOMM_RPN_PM_ALL
;
1417 BT_DBG("dlci %d cr %d len 0x%x bitr 0x%x line 0x%x flow 0x%x xonc 0x%x xoffc 0x%x pm 0x%x",
1418 dlci
, cr
, len
, rpn
->bit_rate
, rpn
->line_settings
, rpn
->flow_ctrl
,
1419 rpn
->xon_char
, rpn
->xoff_char
, rpn
->param_mask
);
1425 /* This is a request, return default (according to ETSI TS 07.10) settings */
1426 bit_rate
= RFCOMM_RPN_BR_9600
;
1427 data_bits
= RFCOMM_RPN_DATA_8
;
1428 stop_bits
= RFCOMM_RPN_STOP_1
;
1429 parity
= RFCOMM_RPN_PARITY_NONE
;
1430 flow_ctrl
= RFCOMM_RPN_FLOW_NONE
;
1431 xon_char
= RFCOMM_RPN_XON_CHAR
;
1432 xoff_char
= RFCOMM_RPN_XOFF_CHAR
;
1436 /* Check for sane values, ignore/accept bit_rate, 8 bits, 1 stop bit,
1437 * no parity, no flow control lines, normal XON/XOFF chars */
1439 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_BITRATE
)) {
1440 bit_rate
= rpn
->bit_rate
;
1441 if (bit_rate
> RFCOMM_RPN_BR_230400
) {
1442 BT_DBG("RPN bit rate mismatch 0x%x", bit_rate
);
1443 bit_rate
= RFCOMM_RPN_BR_9600
;
1444 rpn_mask
^= RFCOMM_RPN_PM_BITRATE
;
1448 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_DATA
)) {
1449 data_bits
= __get_rpn_data_bits(rpn
->line_settings
);
1450 if (data_bits
!= RFCOMM_RPN_DATA_8
) {
1451 BT_DBG("RPN data bits mismatch 0x%x", data_bits
);
1452 data_bits
= RFCOMM_RPN_DATA_8
;
1453 rpn_mask
^= RFCOMM_RPN_PM_DATA
;
1457 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_STOP
)) {
1458 stop_bits
= __get_rpn_stop_bits(rpn
->line_settings
);
1459 if (stop_bits
!= RFCOMM_RPN_STOP_1
) {
1460 BT_DBG("RPN stop bits mismatch 0x%x", stop_bits
);
1461 stop_bits
= RFCOMM_RPN_STOP_1
;
1462 rpn_mask
^= RFCOMM_RPN_PM_STOP
;
1466 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_PARITY
)) {
1467 parity
= __get_rpn_parity(rpn
->line_settings
);
1468 if (parity
!= RFCOMM_RPN_PARITY_NONE
) {
1469 BT_DBG("RPN parity mismatch 0x%x", parity
);
1470 parity
= RFCOMM_RPN_PARITY_NONE
;
1471 rpn_mask
^= RFCOMM_RPN_PM_PARITY
;
1475 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_FLOW
)) {
1476 flow_ctrl
= rpn
->flow_ctrl
;
1477 if (flow_ctrl
!= RFCOMM_RPN_FLOW_NONE
) {
1478 BT_DBG("RPN flow ctrl mismatch 0x%x", flow_ctrl
);
1479 flow_ctrl
= RFCOMM_RPN_FLOW_NONE
;
1480 rpn_mask
^= RFCOMM_RPN_PM_FLOW
;
1484 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_XON
)) {
1485 xon_char
= rpn
->xon_char
;
1486 if (xon_char
!= RFCOMM_RPN_XON_CHAR
) {
1487 BT_DBG("RPN XON char mismatch 0x%x", xon_char
);
1488 xon_char
= RFCOMM_RPN_XON_CHAR
;
1489 rpn_mask
^= RFCOMM_RPN_PM_XON
;
1493 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_XOFF
)) {
1494 xoff_char
= rpn
->xoff_char
;
1495 if (xoff_char
!= RFCOMM_RPN_XOFF_CHAR
) {
1496 BT_DBG("RPN XOFF char mismatch 0x%x", xoff_char
);
1497 xoff_char
= RFCOMM_RPN_XOFF_CHAR
;
1498 rpn_mask
^= RFCOMM_RPN_PM_XOFF
;
1503 rfcomm_send_rpn(s
, 0, dlci
, bit_rate
, data_bits
, stop_bits
,
1504 parity
, flow_ctrl
, xon_char
, xoff_char
, rpn_mask
);
1509 static int rfcomm_recv_rls(struct rfcomm_session
*s
, int cr
, struct sk_buff
*skb
)
1511 struct rfcomm_rls
*rls
= (void *) skb
->data
;
1512 u8 dlci
= __get_dlci(rls
->dlci
);
1514 BT_DBG("dlci %d cr %d status 0x%x", dlci
, cr
, rls
->status
);
1519 /* We should probably do something with this information here. But
1520 * for now it's sufficient just to reply -- Bluetooth 1.1 says it's
1521 * mandatory to recognise and respond to RLS */
1523 rfcomm_send_rls(s
, 0, dlci
, rls
->status
);
1528 static int rfcomm_recv_msc(struct rfcomm_session
*s
, int cr
, struct sk_buff
*skb
)
1530 struct rfcomm_msc
*msc
= (void *) skb
->data
;
1531 struct rfcomm_dlc
*d
;
1532 u8 dlci
= __get_dlci(msc
->dlci
);
1534 BT_DBG("dlci %d cr %d v24 0x%x", dlci
, cr
, msc
->v24_sig
);
1536 d
= rfcomm_dlc_get(s
, dlci
);
1541 if (msc
->v24_sig
& RFCOMM_V24_FC
&& !d
->cfc
)
1542 set_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1544 clear_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1548 d
->remote_v24_sig
= msc
->v24_sig
;
1550 if (d
->modem_status
)
1551 d
->modem_status(d
, msc
->v24_sig
);
1553 rfcomm_dlc_unlock(d
);
1555 rfcomm_send_msc(s
, 0, dlci
, msc
->v24_sig
);
1557 d
->mscex
|= RFCOMM_MSCEX_RX
;
1559 d
->mscex
|= RFCOMM_MSCEX_TX
;
1564 static int rfcomm_recv_mcc(struct rfcomm_session
*s
, struct sk_buff
*skb
)
1566 struct rfcomm_mcc
*mcc
= (void *) skb
->data
;
1569 cr
= __test_cr(mcc
->type
);
1570 type
= __get_mcc_type(mcc
->type
);
1571 len
= __get_mcc_len(mcc
->len
);
1573 BT_DBG("%p type 0x%x cr %d", s
, type
, cr
);
1579 rfcomm_recv_pn(s
, cr
, skb
);
1583 rfcomm_recv_rpn(s
, cr
, len
, skb
);
1587 rfcomm_recv_rls(s
, cr
, skb
);
1591 rfcomm_recv_msc(s
, cr
, skb
);
1596 set_bit(RFCOMM_TX_THROTTLED
, &s
->flags
);
1597 rfcomm_send_fcoff(s
, 0);
1603 clear_bit(RFCOMM_TX_THROTTLED
, &s
->flags
);
1604 rfcomm_send_fcon(s
, 0);
1610 rfcomm_send_test(s
, 0, skb
->data
, skb
->len
);
1617 BT_ERR("Unknown control type 0x%02x", type
);
1618 rfcomm_send_nsc(s
, cr
, type
);
1624 static int rfcomm_recv_data(struct rfcomm_session
*s
, u8 dlci
, int pf
, struct sk_buff
*skb
)
1626 struct rfcomm_dlc
*d
;
1628 BT_DBG("session %p state %ld dlci %d pf %d", s
, s
->state
, dlci
, pf
);
1630 d
= rfcomm_dlc_get(s
, dlci
);
1632 rfcomm_send_dm(s
, dlci
);
1637 u8 credits
= *(u8
*) skb
->data
; skb_pull(skb
, 1);
1639 d
->tx_credits
+= credits
;
1641 clear_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1644 if (skb
->len
&& d
->state
== BT_CONNECTED
) {
1647 d
->data_ready(d
, skb
);
1648 rfcomm_dlc_unlock(d
);
1657 static int rfcomm_recv_frame(struct rfcomm_session
*s
, struct sk_buff
*skb
)
1659 struct rfcomm_hdr
*hdr
= (void *) skb
->data
;
1662 dlci
= __get_dlci(hdr
->addr
);
1663 type
= __get_type(hdr
->ctrl
);
1666 skb
->len
--; skb
->tail
--;
1667 fcs
= *(u8
*)skb_tail_pointer(skb
);
1669 if (__check_fcs(skb
->data
, type
, fcs
)) {
1670 BT_ERR("bad checksum in packet");
1675 if (__test_ea(hdr
->len
))
1682 if (__test_pf(hdr
->ctrl
))
1683 rfcomm_recv_sabm(s
, dlci
);
1687 if (__test_pf(hdr
->ctrl
))
1688 rfcomm_recv_disc(s
, dlci
);
1692 if (__test_pf(hdr
->ctrl
))
1693 rfcomm_recv_ua(s
, dlci
);
1697 rfcomm_recv_dm(s
, dlci
);
1702 return rfcomm_recv_data(s
, dlci
, __test_pf(hdr
->ctrl
), skb
);
1704 rfcomm_recv_mcc(s
, skb
);
1708 BT_ERR("Unknown packet type 0x%02x", type
);
1715 /* ---- Connection and data processing ---- */
1717 static void rfcomm_process_connect(struct rfcomm_session
*s
)
1719 struct rfcomm_dlc
*d
;
1720 struct list_head
*p
, *n
;
1722 BT_DBG("session %p state %ld", s
, s
->state
);
1724 list_for_each_safe(p
, n
, &s
->dlcs
) {
1725 d
= list_entry(p
, struct rfcomm_dlc
, list
);
1726 if (d
->state
== BT_CONFIG
) {
1728 if (rfcomm_check_security(d
)) {
1729 rfcomm_send_pn(s
, 1, d
);
1731 set_bit(RFCOMM_AUTH_PENDING
, &d
->flags
);
1732 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
1738 /* Send data queued for the DLC.
1739 * Return number of frames left in the queue.
1741 static inline int rfcomm_process_tx(struct rfcomm_dlc
*d
)
1743 struct sk_buff
*skb
;
1746 BT_DBG("dlc %p state %ld cfc %d rx_credits %d tx_credits %d",
1747 d
, d
->state
, d
->cfc
, d
->rx_credits
, d
->tx_credits
);
1749 /* Send pending MSC */
1750 if (test_and_clear_bit(RFCOMM_MSC_PENDING
, &d
->flags
))
1751 rfcomm_send_msc(d
->session
, 1, d
->dlci
, d
->v24_sig
);
1755 * Give them some credits */
1756 if (!test_bit(RFCOMM_RX_THROTTLED
, &d
->flags
) &&
1757 d
->rx_credits
<= (d
->cfc
>> 2)) {
1758 rfcomm_send_credits(d
->session
, d
->addr
, d
->cfc
- d
->rx_credits
);
1759 d
->rx_credits
= d
->cfc
;
1763 * Give ourselves some credits */
1767 if (test_bit(RFCOMM_TX_THROTTLED
, &d
->flags
))
1768 return skb_queue_len(&d
->tx_queue
);
1770 while (d
->tx_credits
&& (skb
= skb_dequeue(&d
->tx_queue
))) {
1771 err
= rfcomm_send_frame(d
->session
, skb
->data
, skb
->len
);
1773 skb_queue_head(&d
->tx_queue
, skb
);
1780 if (d
->cfc
&& !d
->tx_credits
) {
1781 /* We're out of TX credits.
1782 * Set TX_THROTTLED flag to avoid unnesary wakeups by dlc_send. */
1783 set_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1786 return skb_queue_len(&d
->tx_queue
);
1789 static inline void rfcomm_process_dlcs(struct rfcomm_session
*s
)
1791 struct rfcomm_dlc
*d
;
1792 struct list_head
*p
, *n
;
1794 BT_DBG("session %p state %ld", s
, s
->state
);
1796 list_for_each_safe(p
, n
, &s
->dlcs
) {
1797 d
= list_entry(p
, struct rfcomm_dlc
, list
);
1799 if (test_bit(RFCOMM_TIMED_OUT
, &d
->flags
)) {
1800 __rfcomm_dlc_close(d
, ETIMEDOUT
);
1804 if (test_and_clear_bit(RFCOMM_AUTH_ACCEPT
, &d
->flags
)) {
1805 rfcomm_dlc_clear_timer(d
);
1807 rfcomm_send_pn(s
, 1, d
);
1808 rfcomm_dlc_set_timer(d
, RFCOMM_CONN_TIMEOUT
);
1810 if (d
->defer_setup
) {
1811 set_bit(RFCOMM_DEFER_SETUP
, &d
->flags
);
1812 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
1815 d
->state
= BT_CONNECT2
;
1816 d
->state_change(d
, 0);
1817 rfcomm_dlc_unlock(d
);
1819 rfcomm_dlc_accept(d
);
1822 } else if (test_and_clear_bit(RFCOMM_AUTH_REJECT
, &d
->flags
)) {
1823 rfcomm_dlc_clear_timer(d
);
1825 rfcomm_send_dm(s
, d
->dlci
);
1827 d
->state
= BT_CLOSED
;
1828 __rfcomm_dlc_close(d
, ECONNREFUSED
);
1832 if (test_bit(RFCOMM_SEC_PENDING
, &d
->flags
))
1835 if (test_bit(RFCOMM_TX_THROTTLED
, &s
->flags
))
1838 if ((d
->state
== BT_CONNECTED
|| d
->state
== BT_DISCONN
) &&
1839 d
->mscex
== RFCOMM_MSCEX_OK
)
1840 rfcomm_process_tx(d
);
1844 static inline void rfcomm_process_rx(struct rfcomm_session
*s
)
1846 struct socket
*sock
= s
->sock
;
1847 struct sock
*sk
= sock
->sk
;
1848 struct sk_buff
*skb
;
1850 BT_DBG("session %p state %ld qlen %d", s
, s
->state
, skb_queue_len(&sk
->sk_receive_queue
));
1852 /* Get data directly from socket receive queue without copying it. */
1853 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
))) {
1855 rfcomm_recv_frame(s
, skb
);
1858 if (sk
->sk_state
== BT_CLOSED
) {
1860 rfcomm_session_put(s
);
1862 rfcomm_session_close(s
, sk
->sk_err
);
1866 static inline void rfcomm_accept_connection(struct rfcomm_session
*s
)
1868 struct socket
*sock
= s
->sock
, *nsock
;
1871 /* Fast check for a new connection.
1872 * Avoids unnesesary socket allocations. */
1873 if (list_empty(&bt_sk(sock
->sk
)->accept_q
))
1876 BT_DBG("session %p", s
);
1878 err
= kernel_accept(sock
, &nsock
, O_NONBLOCK
);
1882 /* Set our callbacks */
1883 nsock
->sk
->sk_data_ready
= rfcomm_l2data_ready
;
1884 nsock
->sk
->sk_state_change
= rfcomm_l2state_change
;
1886 s
= rfcomm_session_add(nsock
, BT_OPEN
);
1888 rfcomm_session_hold(s
);
1890 /* We should adjust MTU on incoming sessions.
1891 * L2CAP MTU minus UIH header and FCS. */
1892 s
->mtu
= min(l2cap_pi(nsock
->sk
)->omtu
, l2cap_pi(nsock
->sk
)->imtu
) - 5;
1896 sock_release(nsock
);
1899 static inline void rfcomm_check_connection(struct rfcomm_session
*s
)
1901 struct sock
*sk
= s
->sock
->sk
;
1903 BT_DBG("%p state %ld", s
, s
->state
);
1905 switch (sk
->sk_state
) {
1907 s
->state
= BT_CONNECT
;
1909 /* We can adjust MTU on outgoing sessions.
1910 * L2CAP MTU minus UIH header and FCS. */
1911 s
->mtu
= min(l2cap_pi(sk
)->omtu
, l2cap_pi(sk
)->imtu
) - 5;
1913 rfcomm_send_sabm(s
, 0);
1917 s
->state
= BT_CLOSED
;
1918 rfcomm_session_close(s
, sk
->sk_err
);
1923 static inline void rfcomm_process_sessions(void)
1925 struct list_head
*p
, *n
;
1929 list_for_each_safe(p
, n
, &session_list
) {
1930 struct rfcomm_session
*s
;
1931 s
= list_entry(p
, struct rfcomm_session
, list
);
1933 if (test_and_clear_bit(RFCOMM_TIMED_OUT
, &s
->flags
)) {
1934 s
->state
= BT_DISCONN
;
1935 rfcomm_send_disc(s
, 0);
1936 rfcomm_session_put(s
);
1940 if (s
->state
== BT_LISTEN
) {
1941 rfcomm_accept_connection(s
);
1945 rfcomm_session_hold(s
);
1949 rfcomm_check_connection(s
);
1953 rfcomm_process_rx(s
);
1957 rfcomm_process_dlcs(s
);
1959 rfcomm_session_put(s
);
1965 static int rfcomm_add_listener(bdaddr_t
*ba
)
1967 struct sockaddr_l2 addr
;
1968 struct socket
*sock
;
1970 struct rfcomm_session
*s
;
1974 err
= rfcomm_l2sock_create(&sock
);
1976 BT_ERR("Create socket failed %d", err
);
1981 bacpy(&addr
.l2_bdaddr
, ba
);
1982 addr
.l2_family
= AF_BLUETOOTH
;
1983 addr
.l2_psm
= cpu_to_le16(RFCOMM_PSM
);
1985 err
= kernel_bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
1987 BT_ERR("Bind failed %d", err
);
1991 /* Set L2CAP options */
1994 l2cap_pi(sk
)->imtu
= l2cap_mtu
;
1997 /* Start listening on the socket */
1998 err
= kernel_listen(sock
, 10);
2000 BT_ERR("Listen failed %d", err
);
2004 /* Add listening session */
2005 s
= rfcomm_session_add(sock
, BT_LISTEN
);
2009 rfcomm_session_hold(s
);
2016 static void rfcomm_kill_listener(void)
2018 struct rfcomm_session
*s
;
2019 struct list_head
*p
, *n
;
2023 list_for_each_safe(p
, n
, &session_list
) {
2024 s
= list_entry(p
, struct rfcomm_session
, list
);
2025 rfcomm_session_del(s
);
2029 static int rfcomm_run(void *unused
)
2033 set_user_nice(current
, -10);
2035 rfcomm_add_listener(BDADDR_ANY
);
2037 while (!kthread_should_stop()) {
2038 set_current_state(TASK_INTERRUPTIBLE
);
2039 if (!test_bit(RFCOMM_SCHED_WAKEUP
, &rfcomm_event
)) {
2040 /* No pending events. Let's sleep.
2041 * Incoming connections and data will wake us up. */
2044 set_current_state(TASK_RUNNING
);
2047 clear_bit(RFCOMM_SCHED_WAKEUP
, &rfcomm_event
);
2048 rfcomm_process_sessions();
2051 rfcomm_kill_listener();
2056 static void rfcomm_security_cfm(struct hci_conn
*conn
, u8 status
, u8 encrypt
)
2058 struct rfcomm_session
*s
;
2059 struct rfcomm_dlc
*d
;
2060 struct list_head
*p
, *n
;
2062 BT_DBG("conn %p status 0x%02x encrypt 0x%02x", conn
, status
, encrypt
);
2064 s
= rfcomm_session_get(&conn
->hdev
->bdaddr
, &conn
->dst
);
2068 rfcomm_session_hold(s
);
2070 list_for_each_safe(p
, n
, &s
->dlcs
) {
2071 d
= list_entry(p
, struct rfcomm_dlc
, list
);
2073 if (test_and_clear_bit(RFCOMM_SEC_PENDING
, &d
->flags
)) {
2074 rfcomm_dlc_clear_timer(d
);
2075 if (status
|| encrypt
== 0x00) {
2076 __rfcomm_dlc_close(d
, ECONNREFUSED
);
2081 if (d
->state
== BT_CONNECTED
&& !status
&& encrypt
== 0x00) {
2082 if (d
->sec_level
== BT_SECURITY_MEDIUM
) {
2083 set_bit(RFCOMM_SEC_PENDING
, &d
->flags
);
2084 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
2086 } else if (d
->sec_level
== BT_SECURITY_HIGH
) {
2087 __rfcomm_dlc_close(d
, ECONNREFUSED
);
2092 if (!test_and_clear_bit(RFCOMM_AUTH_PENDING
, &d
->flags
))
2096 set_bit(RFCOMM_AUTH_ACCEPT
, &d
->flags
);
2098 set_bit(RFCOMM_AUTH_REJECT
, &d
->flags
);
2101 rfcomm_session_put(s
);
2106 static struct hci_cb rfcomm_cb
= {
2108 .security_cfm
= rfcomm_security_cfm
2111 static int rfcomm_dlc_debugfs_show(struct seq_file
*f
, void *x
)
2113 struct rfcomm_session
*s
;
2114 struct list_head
*pp
, *p
;
2118 list_for_each(p
, &session_list
) {
2119 s
= list_entry(p
, struct rfcomm_session
, list
);
2120 list_for_each(pp
, &s
->dlcs
) {
2121 struct sock
*sk
= s
->sock
->sk
;
2122 struct rfcomm_dlc
*d
= list_entry(pp
, struct rfcomm_dlc
, list
);
2124 seq_printf(f
, "%s %s %ld %d %d %d %d\n",
2125 batostr(&bt_sk(sk
)->src
),
2126 batostr(&bt_sk(sk
)->dst
),
2127 d
->state
, d
->dlci
, d
->mtu
,
2128 d
->rx_credits
, d
->tx_credits
);
2137 static int rfcomm_dlc_debugfs_open(struct inode
*inode
, struct file
*file
)
2139 return single_open(file
, rfcomm_dlc_debugfs_show
, inode
->i_private
);
2142 static const struct file_operations rfcomm_dlc_debugfs_fops
= {
2143 .open
= rfcomm_dlc_debugfs_open
,
2145 .llseek
= seq_lseek
,
2146 .release
= single_release
,
2149 static struct dentry
*rfcomm_dlc_debugfs
;
2151 /* ---- Initialization ---- */
2152 static int __init
rfcomm_init(void)
2158 hci_register_cb(&rfcomm_cb
);
2160 rfcomm_thread
= kthread_run(rfcomm_run
, NULL
, "krfcommd");
2161 if (IS_ERR(rfcomm_thread
)) {
2162 err
= PTR_ERR(rfcomm_thread
);
2167 rfcomm_dlc_debugfs
= debugfs_create_file("rfcomm_dlc", 0444,
2168 bt_debugfs
, NULL
, &rfcomm_dlc_debugfs_fops
);
2169 if (!rfcomm_dlc_debugfs
)
2170 BT_ERR("Failed to create RFCOMM debug file");
2173 err
= rfcomm_init_ttys();
2177 err
= rfcomm_init_sockets();
2181 BT_INFO("RFCOMM ver %s", VERSION
);
2186 rfcomm_cleanup_ttys();
2189 kthread_stop(rfcomm_thread
);
2192 hci_unregister_cb(&rfcomm_cb
);
2197 static void __exit
rfcomm_exit(void)
2199 debugfs_remove(rfcomm_dlc_debugfs
);
2201 hci_unregister_cb(&rfcomm_cb
);
2203 kthread_stop(rfcomm_thread
);
2205 rfcomm_cleanup_ttys();
2207 rfcomm_cleanup_sockets();
2210 module_init(rfcomm_init
);
2211 module_exit(rfcomm_exit
);
2213 module_param(disable_cfc
, bool, 0644);
2214 MODULE_PARM_DESC(disable_cfc
, "Disable credit based flow control");
2216 module_param(channel_mtu
, int, 0644);
2217 MODULE_PARM_DESC(channel_mtu
, "Default MTU for the RFCOMM channel");
2219 module_param(l2cap_mtu
, uint
, 0644);
2220 MODULE_PARM_DESC(l2cap_mtu
, "Default MTU for the L2CAP connection");
2222 module_param(l2cap_ertm
, bool, 0644);
2223 MODULE_PARM_DESC(l2cap_ertm
, "Use L2CAP ERTM mode for connection");
2225 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2226 MODULE_DESCRIPTION("Bluetooth RFCOMM ver " VERSION
);
2227 MODULE_VERSION(VERSION
);
2228 MODULE_LICENSE("GPL");
2229 MODULE_ALIAS("bt-proto-3");