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)
66 static LIST_HEAD(session_list
);
68 static int rfcomm_send_frame(struct rfcomm_session
*s
, u8
*data
, int len
);
69 static int rfcomm_send_sabm(struct rfcomm_session
*s
, u8 dlci
);
70 static int rfcomm_send_disc(struct rfcomm_session
*s
, u8 dlci
);
71 static int rfcomm_queue_disc(struct rfcomm_dlc
*d
);
72 static int rfcomm_send_nsc(struct rfcomm_session
*s
, int cr
, u8 type
);
73 static int rfcomm_send_pn(struct rfcomm_session
*s
, int cr
, struct rfcomm_dlc
*d
);
74 static int rfcomm_send_msc(struct rfcomm_session
*s
, int cr
, u8 dlci
, u8 v24_sig
);
75 static int rfcomm_send_test(struct rfcomm_session
*s
, int cr
, u8
*pattern
, int len
);
76 static int rfcomm_send_credits(struct rfcomm_session
*s
, u8 addr
, u8 credits
);
77 static void rfcomm_make_uih(struct sk_buff
*skb
, u8 addr
);
79 static void rfcomm_process_connect(struct rfcomm_session
*s
);
81 static struct rfcomm_session
*rfcomm_session_create(bdaddr_t
*src
,
85 static struct rfcomm_session
*rfcomm_session_get(bdaddr_t
*src
, bdaddr_t
*dst
);
86 static void rfcomm_session_del(struct rfcomm_session
*s
);
88 /* ---- RFCOMM frame parsing macros ---- */
89 #define __get_dlci(b) ((b & 0xfc) >> 2)
90 #define __get_channel(b) ((b & 0xf8) >> 3)
91 #define __get_dir(b) ((b & 0x04) >> 2)
92 #define __get_type(b) ((b & 0xef))
94 #define __test_ea(b) ((b & 0x01))
95 #define __test_cr(b) ((b & 0x02))
96 #define __test_pf(b) ((b & 0x10))
98 #define __addr(cr, dlci) (((dlci & 0x3f) << 2) | (cr << 1) | 0x01)
99 #define __ctrl(type, pf) (((type & 0xef) | (pf << 4)))
100 #define __dlci(dir, chn) (((chn & 0x1f) << 1) | dir)
101 #define __srv_channel(dlci) (dlci >> 1)
102 #define __dir(dlci) (dlci & 0x01)
104 #define __len8(len) (((len) << 1) | 1)
105 #define __len16(len) ((len) << 1)
108 #define __mcc_type(cr, type) (((type << 2) | (cr << 1) | 0x01))
109 #define __get_mcc_type(b) ((b & 0xfc) >> 2)
110 #define __get_mcc_len(b) ((b & 0xfe) >> 1)
113 #define __rpn_line_settings(data, stop, parity) ((data & 0x3) | ((stop & 0x1) << 2) | ((parity & 0x7) << 3))
114 #define __get_rpn_data_bits(line) ((line) & 0x3)
115 #define __get_rpn_stop_bits(line) (((line) >> 2) & 0x1)
116 #define __get_rpn_parity(line) (((line) >> 3) & 0x7)
118 static inline void rfcomm_schedule(void)
122 wake_up_process(rfcomm_thread
);
125 static inline void rfcomm_session_put(struct rfcomm_session
*s
)
127 if (atomic_dec_and_test(&s
->refcnt
))
128 rfcomm_session_del(s
);
131 /* ---- RFCOMM FCS computation ---- */
133 /* reversed, 8-bit, poly=0x07 */
134 static unsigned char rfcomm_crc_table
[256] = {
135 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
136 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
137 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
138 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
140 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
141 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
142 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
143 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
145 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
146 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
147 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
148 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
150 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
151 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
152 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
153 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
155 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
156 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
157 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
158 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
160 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
161 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
162 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
163 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
165 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
166 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
167 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
168 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
170 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
171 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
172 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
173 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
177 #define __crc(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]])
180 static inline u8
__fcs(u8
*data
)
182 return 0xff - __crc(data
);
186 static inline u8
__fcs2(u8
*data
)
188 return 0xff - rfcomm_crc_table
[__crc(data
) ^ data
[2]];
192 static inline int __check_fcs(u8
*data
, int type
, u8 fcs
)
196 if (type
!= RFCOMM_UIH
)
197 f
= rfcomm_crc_table
[f
^ data
[2]];
199 return rfcomm_crc_table
[f
^ fcs
] != 0xcf;
202 /* ---- L2CAP callbacks ---- */
203 static void rfcomm_l2state_change(struct sock
*sk
)
205 BT_DBG("%p state %d", sk
, sk
->sk_state
);
209 static void rfcomm_l2data_ready(struct sock
*sk
, int bytes
)
211 BT_DBG("%p bytes %d", sk
, bytes
);
215 static int rfcomm_l2sock_create(struct socket
**sock
)
221 err
= sock_create_kern(PF_BLUETOOTH
, SOCK_SEQPACKET
, BTPROTO_L2CAP
, sock
);
223 struct sock
*sk
= (*sock
)->sk
;
224 sk
->sk_data_ready
= rfcomm_l2data_ready
;
225 sk
->sk_state_change
= rfcomm_l2state_change
;
230 static inline int rfcomm_check_security(struct rfcomm_dlc
*d
)
232 struct sock
*sk
= d
->session
->sock
->sk
;
233 struct l2cap_conn
*conn
= l2cap_pi(sk
)->chan
->conn
;
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(conn
->hcon
, d
->sec_level
, auth_type
);
252 static void rfcomm_session_timeout(unsigned long arg
)
254 struct rfcomm_session
*s
= (void *) arg
;
256 BT_DBG("session %p state %ld", s
, s
->state
);
258 set_bit(RFCOMM_TIMED_OUT
, &s
->flags
);
262 static void rfcomm_session_set_timer(struct rfcomm_session
*s
, long timeout
)
264 BT_DBG("session %p state %ld timeout %ld", s
, s
->state
, timeout
);
266 if (!mod_timer(&s
->timer
, jiffies
+ timeout
))
267 rfcomm_session_hold(s
);
270 static void rfcomm_session_clear_timer(struct rfcomm_session
*s
)
272 BT_DBG("session %p state %ld", s
, s
->state
);
274 if (timer_pending(&s
->timer
) && del_timer(&s
->timer
))
275 rfcomm_session_put(s
);
278 /* ---- RFCOMM DLCs ---- */
279 static void rfcomm_dlc_timeout(unsigned long arg
)
281 struct rfcomm_dlc
*d
= (void *) arg
;
283 BT_DBG("dlc %p state %ld", d
, d
->state
);
285 set_bit(RFCOMM_TIMED_OUT
, &d
->flags
);
290 static void rfcomm_dlc_set_timer(struct rfcomm_dlc
*d
, long timeout
)
292 BT_DBG("dlc %p state %ld timeout %ld", d
, d
->state
, timeout
);
294 if (!mod_timer(&d
->timer
, jiffies
+ timeout
))
298 static void rfcomm_dlc_clear_timer(struct rfcomm_dlc
*d
)
300 BT_DBG("dlc %p state %ld", d
, d
->state
);
302 if (timer_pending(&d
->timer
) && del_timer(&d
->timer
))
306 static void rfcomm_dlc_clear_state(struct rfcomm_dlc
*d
)
313 d
->sec_level
= BT_SECURITY_LOW
;
314 d
->mtu
= RFCOMM_DEFAULT_MTU
;
315 d
->v24_sig
= RFCOMM_V24_RTC
| RFCOMM_V24_RTR
| RFCOMM_V24_DV
;
317 d
->cfc
= RFCOMM_CFC_DISABLED
;
318 d
->rx_credits
= RFCOMM_DEFAULT_CREDITS
;
321 struct rfcomm_dlc
*rfcomm_dlc_alloc(gfp_t prio
)
323 struct rfcomm_dlc
*d
= kzalloc(sizeof(*d
), prio
);
328 setup_timer(&d
->timer
, rfcomm_dlc_timeout
, (unsigned long)d
);
330 skb_queue_head_init(&d
->tx_queue
);
331 spin_lock_init(&d
->lock
);
332 atomic_set(&d
->refcnt
, 1);
334 rfcomm_dlc_clear_state(d
);
341 void rfcomm_dlc_free(struct rfcomm_dlc
*d
)
345 skb_queue_purge(&d
->tx_queue
);
349 static void rfcomm_dlc_link(struct rfcomm_session
*s
, struct rfcomm_dlc
*d
)
351 BT_DBG("dlc %p session %p", d
, s
);
353 rfcomm_session_hold(s
);
355 rfcomm_session_clear_timer(s
);
357 list_add(&d
->list
, &s
->dlcs
);
361 static void rfcomm_dlc_unlink(struct rfcomm_dlc
*d
)
363 struct rfcomm_session
*s
= d
->session
;
365 BT_DBG("dlc %p refcnt %d session %p", d
, atomic_read(&d
->refcnt
), s
);
371 if (list_empty(&s
->dlcs
))
372 rfcomm_session_set_timer(s
, RFCOMM_IDLE_TIMEOUT
);
374 rfcomm_session_put(s
);
377 static struct rfcomm_dlc
*rfcomm_dlc_get(struct rfcomm_session
*s
, u8 dlci
)
379 struct rfcomm_dlc
*d
;
382 list_for_each(p
, &s
->dlcs
) {
383 d
= list_entry(p
, struct rfcomm_dlc
, list
);
390 static int __rfcomm_dlc_open(struct rfcomm_dlc
*d
, bdaddr_t
*src
, bdaddr_t
*dst
, u8 channel
)
392 struct rfcomm_session
*s
;
396 BT_DBG("dlc %p state %ld %s %s channel %d",
397 d
, d
->state
, batostr(src
), batostr(dst
), channel
);
399 if (channel
< 1 || channel
> 30)
402 if (d
->state
!= BT_OPEN
&& d
->state
!= BT_CLOSED
)
405 s
= rfcomm_session_get(src
, dst
);
407 s
= rfcomm_session_create(src
, dst
, d
->sec_level
, &err
);
412 dlci
= __dlci(!s
->initiator
, channel
);
414 /* Check if DLCI already exists */
415 if (rfcomm_dlc_get(s
, dlci
))
418 rfcomm_dlc_clear_state(d
);
421 d
->addr
= __addr(s
->initiator
, dlci
);
424 d
->state
= BT_CONFIG
;
425 rfcomm_dlc_link(s
, d
);
430 d
->cfc
= (s
->cfc
== RFCOMM_CFC_UNKNOWN
) ? 0 : s
->cfc
;
432 if (s
->state
== BT_CONNECTED
) {
433 if (rfcomm_check_security(d
))
434 rfcomm_send_pn(s
, 1, d
);
436 set_bit(RFCOMM_AUTH_PENDING
, &d
->flags
);
439 rfcomm_dlc_set_timer(d
, RFCOMM_CONN_TIMEOUT
);
444 int rfcomm_dlc_open(struct rfcomm_dlc
*d
, bdaddr_t
*src
, bdaddr_t
*dst
, u8 channel
)
450 r
= __rfcomm_dlc_open(d
, src
, dst
, channel
);
456 static int __rfcomm_dlc_close(struct rfcomm_dlc
*d
, int err
)
458 struct rfcomm_session
*s
= d
->session
;
462 BT_DBG("dlc %p state %ld dlci %d err %d session %p",
463 d
, d
->state
, d
->dlci
, err
, s
);
468 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
469 set_bit(RFCOMM_AUTH_REJECT
, &d
->flags
);
476 d
->state
= BT_DISCONN
;
477 if (skb_queue_empty(&d
->tx_queue
)) {
478 rfcomm_send_disc(s
, d
->dlci
);
479 rfcomm_dlc_set_timer(d
, RFCOMM_DISC_TIMEOUT
);
481 rfcomm_queue_disc(d
);
482 rfcomm_dlc_set_timer(d
, RFCOMM_DISC_TIMEOUT
* 2);
488 if (test_and_clear_bit(RFCOMM_DEFER_SETUP
, &d
->flags
)) {
489 set_bit(RFCOMM_AUTH_REJECT
, &d
->flags
);
496 rfcomm_dlc_clear_timer(d
);
499 d
->state
= BT_CLOSED
;
500 d
->state_change(d
, err
);
501 rfcomm_dlc_unlock(d
);
503 skb_queue_purge(&d
->tx_queue
);
504 rfcomm_dlc_unlink(d
);
510 int rfcomm_dlc_close(struct rfcomm_dlc
*d
, int err
)
516 r
= __rfcomm_dlc_close(d
, err
);
522 int rfcomm_dlc_send(struct rfcomm_dlc
*d
, struct sk_buff
*skb
)
526 if (d
->state
!= BT_CONNECTED
)
529 BT_DBG("dlc %p mtu %d len %d", d
, d
->mtu
, len
);
534 rfcomm_make_uih(skb
, d
->addr
);
535 skb_queue_tail(&d
->tx_queue
, skb
);
537 if (!test_bit(RFCOMM_TX_THROTTLED
, &d
->flags
))
542 void __rfcomm_dlc_throttle(struct rfcomm_dlc
*d
)
544 BT_DBG("dlc %p state %ld", d
, d
->state
);
547 d
->v24_sig
|= RFCOMM_V24_FC
;
548 set_bit(RFCOMM_MSC_PENDING
, &d
->flags
);
553 void __rfcomm_dlc_unthrottle(struct rfcomm_dlc
*d
)
555 BT_DBG("dlc %p state %ld", d
, d
->state
);
558 d
->v24_sig
&= ~RFCOMM_V24_FC
;
559 set_bit(RFCOMM_MSC_PENDING
, &d
->flags
);
565 Set/get modem status functions use _local_ status i.e. what we report
567 Remote status is provided by dlc->modem_status() callback.
569 int rfcomm_dlc_set_modem_status(struct rfcomm_dlc
*d
, u8 v24_sig
)
571 BT_DBG("dlc %p state %ld v24_sig 0x%x",
572 d
, d
->state
, v24_sig
);
574 if (test_bit(RFCOMM_RX_THROTTLED
, &d
->flags
))
575 v24_sig
|= RFCOMM_V24_FC
;
577 v24_sig
&= ~RFCOMM_V24_FC
;
579 d
->v24_sig
= v24_sig
;
581 if (!test_and_set_bit(RFCOMM_MSC_PENDING
, &d
->flags
))
587 int rfcomm_dlc_get_modem_status(struct rfcomm_dlc
*d
, u8
*v24_sig
)
589 BT_DBG("dlc %p state %ld v24_sig 0x%x",
590 d
, d
->state
, d
->v24_sig
);
592 *v24_sig
= d
->v24_sig
;
596 /* ---- RFCOMM sessions ---- */
597 static struct rfcomm_session
*rfcomm_session_add(struct socket
*sock
, int state
)
599 struct rfcomm_session
*s
= kzalloc(sizeof(*s
), GFP_KERNEL
);
604 BT_DBG("session %p sock %p", s
, sock
);
606 setup_timer(&s
->timer
, rfcomm_session_timeout
, (unsigned long) s
);
608 INIT_LIST_HEAD(&s
->dlcs
);
612 s
->mtu
= RFCOMM_DEFAULT_MTU
;
613 s
->cfc
= disable_cfc
? RFCOMM_CFC_DISABLED
: RFCOMM_CFC_UNKNOWN
;
615 /* Do not increment module usage count for listening sessions.
616 * Otherwise we won't be able to unload the module. */
617 if (state
!= BT_LISTEN
)
618 if (!try_module_get(THIS_MODULE
)) {
623 list_add(&s
->list
, &session_list
);
628 static void rfcomm_session_del(struct rfcomm_session
*s
)
630 int state
= s
->state
;
632 BT_DBG("session %p state %ld", s
, s
->state
);
636 if (state
== BT_CONNECTED
)
637 rfcomm_send_disc(s
, 0);
639 rfcomm_session_clear_timer(s
);
640 sock_release(s
->sock
);
643 if (state
!= BT_LISTEN
)
644 module_put(THIS_MODULE
);
647 static struct rfcomm_session
*rfcomm_session_get(bdaddr_t
*src
, bdaddr_t
*dst
)
649 struct rfcomm_session
*s
;
650 struct list_head
*p
, *n
;
652 list_for_each_safe(p
, n
, &session_list
) {
653 s
= list_entry(p
, struct rfcomm_session
, list
);
654 sk
= bt_sk(s
->sock
->sk
);
656 if ((!bacmp(src
, BDADDR_ANY
) || !bacmp(&sk
->src
, src
)) &&
657 !bacmp(&sk
->dst
, dst
))
663 static void rfcomm_session_close(struct rfcomm_session
*s
, int err
)
665 struct rfcomm_dlc
*d
;
666 struct list_head
*p
, *n
;
668 BT_DBG("session %p state %ld err %d", s
, s
->state
, err
);
670 rfcomm_session_hold(s
);
672 s
->state
= BT_CLOSED
;
675 list_for_each_safe(p
, n
, &s
->dlcs
) {
676 d
= list_entry(p
, struct rfcomm_dlc
, list
);
677 d
->state
= BT_CLOSED
;
678 __rfcomm_dlc_close(d
, err
);
681 rfcomm_session_clear_timer(s
);
682 rfcomm_session_put(s
);
685 static struct rfcomm_session
*rfcomm_session_create(bdaddr_t
*src
,
690 struct rfcomm_session
*s
= NULL
;
691 struct sockaddr_l2 addr
;
695 BT_DBG("%s %s", batostr(src
), batostr(dst
));
697 *err
= rfcomm_l2sock_create(&sock
);
701 bacpy(&addr
.l2_bdaddr
, src
);
702 addr
.l2_family
= AF_BLUETOOTH
;
705 *err
= kernel_bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
709 /* Set L2CAP options */
712 l2cap_pi(sk
)->chan
->imtu
= l2cap_mtu
;
713 l2cap_pi(sk
)->chan
->sec_level
= sec_level
;
715 l2cap_pi(sk
)->chan
->mode
= L2CAP_MODE_ERTM
;
718 s
= rfcomm_session_add(sock
, BT_BOUND
);
726 bacpy(&addr
.l2_bdaddr
, dst
);
727 addr
.l2_family
= AF_BLUETOOTH
;
728 addr
.l2_psm
= cpu_to_le16(RFCOMM_PSM
);
730 *err
= kernel_connect(sock
, (struct sockaddr
*) &addr
, sizeof(addr
), O_NONBLOCK
);
731 if (*err
== 0 || *err
== -EINPROGRESS
)
734 rfcomm_session_del(s
);
742 void rfcomm_session_getaddr(struct rfcomm_session
*s
, bdaddr_t
*src
, bdaddr_t
*dst
)
744 struct sock
*sk
= s
->sock
->sk
;
746 bacpy(src
, &bt_sk(sk
)->src
);
748 bacpy(dst
, &bt_sk(sk
)->dst
);
751 /* ---- RFCOMM frame sending ---- */
752 static int rfcomm_send_frame(struct rfcomm_session
*s
, u8
*data
, int len
)
754 struct socket
*sock
= s
->sock
;
755 struct kvec iv
= { data
, len
};
758 BT_DBG("session %p len %d", s
, len
);
760 memset(&msg
, 0, sizeof(msg
));
762 return kernel_sendmsg(sock
, &msg
, &iv
, 1, len
);
765 static int rfcomm_send_sabm(struct rfcomm_session
*s
, u8 dlci
)
767 struct rfcomm_cmd cmd
;
769 BT_DBG("%p dlci %d", s
, dlci
);
771 cmd
.addr
= __addr(s
->initiator
, dlci
);
772 cmd
.ctrl
= __ctrl(RFCOMM_SABM
, 1);
774 cmd
.fcs
= __fcs2((u8
*) &cmd
);
776 return rfcomm_send_frame(s
, (void *) &cmd
, sizeof(cmd
));
779 static int rfcomm_send_ua(struct rfcomm_session
*s
, u8 dlci
)
781 struct rfcomm_cmd cmd
;
783 BT_DBG("%p dlci %d", s
, dlci
);
785 cmd
.addr
= __addr(!s
->initiator
, dlci
);
786 cmd
.ctrl
= __ctrl(RFCOMM_UA
, 1);
788 cmd
.fcs
= __fcs2((u8
*) &cmd
);
790 return rfcomm_send_frame(s
, (void *) &cmd
, sizeof(cmd
));
793 static int rfcomm_send_disc(struct rfcomm_session
*s
, u8 dlci
)
795 struct rfcomm_cmd cmd
;
797 BT_DBG("%p dlci %d", s
, dlci
);
799 cmd
.addr
= __addr(s
->initiator
, dlci
);
800 cmd
.ctrl
= __ctrl(RFCOMM_DISC
, 1);
802 cmd
.fcs
= __fcs2((u8
*) &cmd
);
804 return rfcomm_send_frame(s
, (void *) &cmd
, sizeof(cmd
));
807 static int rfcomm_queue_disc(struct rfcomm_dlc
*d
)
809 struct rfcomm_cmd
*cmd
;
812 BT_DBG("dlc %p dlci %d", d
, d
->dlci
);
814 skb
= alloc_skb(sizeof(*cmd
), GFP_KERNEL
);
818 cmd
= (void *) __skb_put(skb
, sizeof(*cmd
));
820 cmd
->ctrl
= __ctrl(RFCOMM_DISC
, 1);
821 cmd
->len
= __len8(0);
822 cmd
->fcs
= __fcs2((u8
*) cmd
);
824 skb_queue_tail(&d
->tx_queue
, skb
);
829 static int rfcomm_send_dm(struct rfcomm_session
*s
, u8 dlci
)
831 struct rfcomm_cmd cmd
;
833 BT_DBG("%p dlci %d", s
, dlci
);
835 cmd
.addr
= __addr(!s
->initiator
, dlci
);
836 cmd
.ctrl
= __ctrl(RFCOMM_DM
, 1);
838 cmd
.fcs
= __fcs2((u8
*) &cmd
);
840 return rfcomm_send_frame(s
, (void *) &cmd
, sizeof(cmd
));
843 static int rfcomm_send_nsc(struct rfcomm_session
*s
, int cr
, u8 type
)
845 struct rfcomm_hdr
*hdr
;
846 struct rfcomm_mcc
*mcc
;
847 u8 buf
[16], *ptr
= buf
;
849 BT_DBG("%p cr %d type %d", s
, cr
, type
);
851 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
852 hdr
->addr
= __addr(s
->initiator
, 0);
853 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
854 hdr
->len
= __len8(sizeof(*mcc
) + 1);
856 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
857 mcc
->type
= __mcc_type(cr
, RFCOMM_NSC
);
858 mcc
->len
= __len8(1);
860 /* Type that we didn't like */
861 *ptr
= __mcc_type(cr
, type
); ptr
++;
863 *ptr
= __fcs(buf
); ptr
++;
865 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
868 static int rfcomm_send_pn(struct rfcomm_session
*s
, int cr
, struct rfcomm_dlc
*d
)
870 struct rfcomm_hdr
*hdr
;
871 struct rfcomm_mcc
*mcc
;
872 struct rfcomm_pn
*pn
;
873 u8 buf
[16], *ptr
= buf
;
875 BT_DBG("%p cr %d dlci %d mtu %d", s
, cr
, d
->dlci
, d
->mtu
);
877 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
878 hdr
->addr
= __addr(s
->initiator
, 0);
879 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
880 hdr
->len
= __len8(sizeof(*mcc
) + sizeof(*pn
));
882 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
883 mcc
->type
= __mcc_type(cr
, RFCOMM_PN
);
884 mcc
->len
= __len8(sizeof(*pn
));
886 pn
= (void *) ptr
; ptr
+= sizeof(*pn
);
888 pn
->priority
= d
->priority
;
893 pn
->flow_ctrl
= cr
? 0xf0 : 0xe0;
894 pn
->credits
= RFCOMM_DEFAULT_CREDITS
;
900 if (cr
&& channel_mtu
>= 0)
901 pn
->mtu
= cpu_to_le16(channel_mtu
);
903 pn
->mtu
= cpu_to_le16(d
->mtu
);
905 *ptr
= __fcs(buf
); ptr
++;
907 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
910 int rfcomm_send_rpn(struct rfcomm_session
*s
, int cr
, u8 dlci
,
911 u8 bit_rate
, u8 data_bits
, u8 stop_bits
,
912 u8 parity
, u8 flow_ctrl_settings
,
913 u8 xon_char
, u8 xoff_char
, u16 param_mask
)
915 struct rfcomm_hdr
*hdr
;
916 struct rfcomm_mcc
*mcc
;
917 struct rfcomm_rpn
*rpn
;
918 u8 buf
[16], *ptr
= buf
;
920 BT_DBG("%p cr %d dlci %d bit_r 0x%x data_b 0x%x stop_b 0x%x parity 0x%x"
921 " flwc_s 0x%x xon_c 0x%x xoff_c 0x%x p_mask 0x%x",
922 s
, cr
, dlci
, bit_rate
, data_bits
, stop_bits
, parity
,
923 flow_ctrl_settings
, xon_char
, xoff_char
, param_mask
);
925 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
926 hdr
->addr
= __addr(s
->initiator
, 0);
927 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
928 hdr
->len
= __len8(sizeof(*mcc
) + sizeof(*rpn
));
930 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
931 mcc
->type
= __mcc_type(cr
, RFCOMM_RPN
);
932 mcc
->len
= __len8(sizeof(*rpn
));
934 rpn
= (void *) ptr
; ptr
+= sizeof(*rpn
);
935 rpn
->dlci
= __addr(1, dlci
);
936 rpn
->bit_rate
= bit_rate
;
937 rpn
->line_settings
= __rpn_line_settings(data_bits
, stop_bits
, parity
);
938 rpn
->flow_ctrl
= flow_ctrl_settings
;
939 rpn
->xon_char
= xon_char
;
940 rpn
->xoff_char
= xoff_char
;
941 rpn
->param_mask
= cpu_to_le16(param_mask
);
943 *ptr
= __fcs(buf
); ptr
++;
945 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
948 static int rfcomm_send_rls(struct rfcomm_session
*s
, int cr
, u8 dlci
, u8 status
)
950 struct rfcomm_hdr
*hdr
;
951 struct rfcomm_mcc
*mcc
;
952 struct rfcomm_rls
*rls
;
953 u8 buf
[16], *ptr
= buf
;
955 BT_DBG("%p cr %d status 0x%x", s
, cr
, status
);
957 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
958 hdr
->addr
= __addr(s
->initiator
, 0);
959 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
960 hdr
->len
= __len8(sizeof(*mcc
) + sizeof(*rls
));
962 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
963 mcc
->type
= __mcc_type(cr
, RFCOMM_RLS
);
964 mcc
->len
= __len8(sizeof(*rls
));
966 rls
= (void *) ptr
; ptr
+= sizeof(*rls
);
967 rls
->dlci
= __addr(1, dlci
);
968 rls
->status
= status
;
970 *ptr
= __fcs(buf
); ptr
++;
972 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
975 static int rfcomm_send_msc(struct rfcomm_session
*s
, int cr
, u8 dlci
, u8 v24_sig
)
977 struct rfcomm_hdr
*hdr
;
978 struct rfcomm_mcc
*mcc
;
979 struct rfcomm_msc
*msc
;
980 u8 buf
[16], *ptr
= buf
;
982 BT_DBG("%p cr %d v24 0x%x", s
, cr
, v24_sig
);
984 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
985 hdr
->addr
= __addr(s
->initiator
, 0);
986 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
987 hdr
->len
= __len8(sizeof(*mcc
) + sizeof(*msc
));
989 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
990 mcc
->type
= __mcc_type(cr
, RFCOMM_MSC
);
991 mcc
->len
= __len8(sizeof(*msc
));
993 msc
= (void *) ptr
; ptr
+= sizeof(*msc
);
994 msc
->dlci
= __addr(1, dlci
);
995 msc
->v24_sig
= v24_sig
| 0x01;
997 *ptr
= __fcs(buf
); ptr
++;
999 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
1002 static int rfcomm_send_fcoff(struct rfcomm_session
*s
, int cr
)
1004 struct rfcomm_hdr
*hdr
;
1005 struct rfcomm_mcc
*mcc
;
1006 u8 buf
[16], *ptr
= buf
;
1008 BT_DBG("%p cr %d", s
, cr
);
1010 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
1011 hdr
->addr
= __addr(s
->initiator
, 0);
1012 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
1013 hdr
->len
= __len8(sizeof(*mcc
));
1015 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
1016 mcc
->type
= __mcc_type(cr
, RFCOMM_FCOFF
);
1017 mcc
->len
= __len8(0);
1019 *ptr
= __fcs(buf
); ptr
++;
1021 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
1024 static int rfcomm_send_fcon(struct rfcomm_session
*s
, int cr
)
1026 struct rfcomm_hdr
*hdr
;
1027 struct rfcomm_mcc
*mcc
;
1028 u8 buf
[16], *ptr
= buf
;
1030 BT_DBG("%p cr %d", s
, cr
);
1032 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
1033 hdr
->addr
= __addr(s
->initiator
, 0);
1034 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
1035 hdr
->len
= __len8(sizeof(*mcc
));
1037 mcc
= (void *) ptr
; ptr
+= sizeof(*mcc
);
1038 mcc
->type
= __mcc_type(cr
, RFCOMM_FCON
);
1039 mcc
->len
= __len8(0);
1041 *ptr
= __fcs(buf
); ptr
++;
1043 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
1046 static int rfcomm_send_test(struct rfcomm_session
*s
, int cr
, u8
*pattern
, int len
)
1048 struct socket
*sock
= s
->sock
;
1051 unsigned char hdr
[5], crc
[1];
1056 BT_DBG("%p cr %d", s
, cr
);
1058 hdr
[0] = __addr(s
->initiator
, 0);
1059 hdr
[1] = __ctrl(RFCOMM_UIH
, 0);
1060 hdr
[2] = 0x01 | ((len
+ 2) << 1);
1061 hdr
[3] = 0x01 | ((cr
& 0x01) << 1) | (RFCOMM_TEST
<< 2);
1062 hdr
[4] = 0x01 | (len
<< 1);
1064 crc
[0] = __fcs(hdr
);
1066 iv
[0].iov_base
= hdr
;
1068 iv
[1].iov_base
= pattern
;
1069 iv
[1].iov_len
= len
;
1070 iv
[2].iov_base
= crc
;
1073 memset(&msg
, 0, sizeof(msg
));
1075 return kernel_sendmsg(sock
, &msg
, iv
, 3, 6 + len
);
1078 static int rfcomm_send_credits(struct rfcomm_session
*s
, u8 addr
, u8 credits
)
1080 struct rfcomm_hdr
*hdr
;
1081 u8 buf
[16], *ptr
= buf
;
1083 BT_DBG("%p addr %d credits %d", s
, addr
, credits
);
1085 hdr
= (void *) ptr
; ptr
+= sizeof(*hdr
);
1087 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 1);
1088 hdr
->len
= __len8(0);
1090 *ptr
= credits
; ptr
++;
1092 *ptr
= __fcs(buf
); ptr
++;
1094 return rfcomm_send_frame(s
, buf
, ptr
- buf
);
1097 static void rfcomm_make_uih(struct sk_buff
*skb
, u8 addr
)
1099 struct rfcomm_hdr
*hdr
;
1104 hdr
= (void *) skb_push(skb
, 4);
1105 put_unaligned(cpu_to_le16(__len16(len
)), (__le16
*) &hdr
->len
);
1107 hdr
= (void *) skb_push(skb
, 3);
1108 hdr
->len
= __len8(len
);
1111 hdr
->ctrl
= __ctrl(RFCOMM_UIH
, 0);
1113 crc
= skb_put(skb
, 1);
1114 *crc
= __fcs((void *) hdr
);
1117 /* ---- RFCOMM frame reception ---- */
1118 static int rfcomm_recv_ua(struct rfcomm_session
*s
, u8 dlci
)
1120 BT_DBG("session %p state %ld dlci %d", s
, s
->state
, dlci
);
1124 struct rfcomm_dlc
*d
= rfcomm_dlc_get(s
, dlci
);
1126 rfcomm_send_dm(s
, dlci
);
1132 rfcomm_dlc_clear_timer(d
);
1135 d
->state
= BT_CONNECTED
;
1136 d
->state_change(d
, 0);
1137 rfcomm_dlc_unlock(d
);
1139 rfcomm_send_msc(s
, 1, dlci
, d
->v24_sig
);
1143 d
->state
= BT_CLOSED
;
1144 __rfcomm_dlc_close(d
, 0);
1146 if (list_empty(&s
->dlcs
)) {
1147 s
->state
= BT_DISCONN
;
1148 rfcomm_send_disc(s
, 0);
1154 /* Control channel */
1157 s
->state
= BT_CONNECTED
;
1158 rfcomm_process_connect(s
);
1162 /* When socket is closed and we are not RFCOMM
1163 * initiator rfcomm_process_rx already calls
1164 * rfcomm_session_put() */
1165 if (s
->sock
->sk
->sk_state
!= BT_CLOSED
)
1166 if (list_empty(&s
->dlcs
))
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
;
1243 struct l2cap_conn
*conn
= l2cap_pi(sk
)->chan
->conn
;
1245 BT_DBG("dlc %p", d
);
1247 rfcomm_send_ua(d
->session
, d
->dlci
);
1249 rfcomm_dlc_clear_timer(d
);
1252 d
->state
= BT_CONNECTED
;
1253 d
->state_change(d
, 0);
1254 rfcomm_dlc_unlock(d
);
1257 hci_conn_switch_role(conn
->hcon
, 0x00);
1259 rfcomm_send_msc(d
->session
, 1, d
->dlci
, d
->v24_sig
);
1262 static void rfcomm_check_accept(struct rfcomm_dlc
*d
)
1264 if (rfcomm_check_security(d
)) {
1265 if (d
->defer_setup
) {
1266 set_bit(RFCOMM_DEFER_SETUP
, &d
->flags
);
1267 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
1270 d
->state
= BT_CONNECT2
;
1271 d
->state_change(d
, 0);
1272 rfcomm_dlc_unlock(d
);
1274 rfcomm_dlc_accept(d
);
1276 set_bit(RFCOMM_AUTH_PENDING
, &d
->flags
);
1277 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
1281 static int rfcomm_recv_sabm(struct rfcomm_session
*s
, u8 dlci
)
1283 struct rfcomm_dlc
*d
;
1286 BT_DBG("session %p state %ld dlci %d", s
, s
->state
, dlci
);
1289 rfcomm_send_ua(s
, 0);
1291 if (s
->state
== BT_OPEN
) {
1292 s
->state
= BT_CONNECTED
;
1293 rfcomm_process_connect(s
);
1298 /* Check if DLC exists */
1299 d
= rfcomm_dlc_get(s
, dlci
);
1301 if (d
->state
== BT_OPEN
) {
1302 /* DLC was previously opened by PN request */
1303 rfcomm_check_accept(d
);
1308 /* Notify socket layer about incoming connection */
1309 channel
= __srv_channel(dlci
);
1310 if (rfcomm_connect_ind(s
, channel
, &d
)) {
1312 d
->addr
= __addr(s
->initiator
, dlci
);
1313 rfcomm_dlc_link(s
, d
);
1315 rfcomm_check_accept(d
);
1317 rfcomm_send_dm(s
, dlci
);
1323 static int rfcomm_apply_pn(struct rfcomm_dlc
*d
, int cr
, struct rfcomm_pn
*pn
)
1325 struct rfcomm_session
*s
= d
->session
;
1327 BT_DBG("dlc %p state %ld dlci %d mtu %d fc 0x%x credits %d",
1328 d
, d
->state
, d
->dlci
, pn
->mtu
, pn
->flow_ctrl
, pn
->credits
);
1330 if ((pn
->flow_ctrl
== 0xf0 && s
->cfc
!= RFCOMM_CFC_DISABLED
) ||
1331 pn
->flow_ctrl
== 0xe0) {
1332 d
->cfc
= RFCOMM_CFC_ENABLED
;
1333 d
->tx_credits
= pn
->credits
;
1335 d
->cfc
= RFCOMM_CFC_DISABLED
;
1336 set_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1339 if (s
->cfc
== RFCOMM_CFC_UNKNOWN
)
1342 d
->priority
= pn
->priority
;
1344 d
->mtu
= __le16_to_cpu(pn
->mtu
);
1346 if (cr
&& d
->mtu
> s
->mtu
)
1352 static int rfcomm_recv_pn(struct rfcomm_session
*s
, int cr
, struct sk_buff
*skb
)
1354 struct rfcomm_pn
*pn
= (void *) skb
->data
;
1355 struct rfcomm_dlc
*d
;
1358 BT_DBG("session %p state %ld dlci %d", s
, s
->state
, dlci
);
1363 d
= rfcomm_dlc_get(s
, dlci
);
1367 rfcomm_apply_pn(d
, cr
, pn
);
1368 rfcomm_send_pn(s
, 0, d
);
1373 rfcomm_apply_pn(d
, cr
, pn
);
1375 d
->state
= BT_CONNECT
;
1376 rfcomm_send_sabm(s
, d
->dlci
);
1381 u8 channel
= __srv_channel(dlci
);
1386 /* PN request for non existing DLC.
1387 * Assume incoming connection. */
1388 if (rfcomm_connect_ind(s
, channel
, &d
)) {
1390 d
->addr
= __addr(s
->initiator
, dlci
);
1391 rfcomm_dlc_link(s
, d
);
1393 rfcomm_apply_pn(d
, cr
, pn
);
1396 rfcomm_send_pn(s
, 0, d
);
1398 rfcomm_send_dm(s
, dlci
);
1404 static int rfcomm_recv_rpn(struct rfcomm_session
*s
, int cr
, int len
, struct sk_buff
*skb
)
1406 struct rfcomm_rpn
*rpn
= (void *) skb
->data
;
1407 u8 dlci
= __get_dlci(rpn
->dlci
);
1416 u16 rpn_mask
= RFCOMM_RPN_PM_ALL
;
1418 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",
1419 dlci
, cr
, len
, rpn
->bit_rate
, rpn
->line_settings
, rpn
->flow_ctrl
,
1420 rpn
->xon_char
, rpn
->xoff_char
, rpn
->param_mask
);
1426 /* This is a request, return default (according to ETSI TS 07.10) settings */
1427 bit_rate
= RFCOMM_RPN_BR_9600
;
1428 data_bits
= RFCOMM_RPN_DATA_8
;
1429 stop_bits
= RFCOMM_RPN_STOP_1
;
1430 parity
= RFCOMM_RPN_PARITY_NONE
;
1431 flow_ctrl
= RFCOMM_RPN_FLOW_NONE
;
1432 xon_char
= RFCOMM_RPN_XON_CHAR
;
1433 xoff_char
= RFCOMM_RPN_XOFF_CHAR
;
1437 /* Check for sane values, ignore/accept bit_rate, 8 bits, 1 stop bit,
1438 * no parity, no flow control lines, normal XON/XOFF chars */
1440 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_BITRATE
)) {
1441 bit_rate
= rpn
->bit_rate
;
1442 if (bit_rate
> RFCOMM_RPN_BR_230400
) {
1443 BT_DBG("RPN bit rate mismatch 0x%x", bit_rate
);
1444 bit_rate
= RFCOMM_RPN_BR_9600
;
1445 rpn_mask
^= RFCOMM_RPN_PM_BITRATE
;
1449 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_DATA
)) {
1450 data_bits
= __get_rpn_data_bits(rpn
->line_settings
);
1451 if (data_bits
!= RFCOMM_RPN_DATA_8
) {
1452 BT_DBG("RPN data bits mismatch 0x%x", data_bits
);
1453 data_bits
= RFCOMM_RPN_DATA_8
;
1454 rpn_mask
^= RFCOMM_RPN_PM_DATA
;
1458 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_STOP
)) {
1459 stop_bits
= __get_rpn_stop_bits(rpn
->line_settings
);
1460 if (stop_bits
!= RFCOMM_RPN_STOP_1
) {
1461 BT_DBG("RPN stop bits mismatch 0x%x", stop_bits
);
1462 stop_bits
= RFCOMM_RPN_STOP_1
;
1463 rpn_mask
^= RFCOMM_RPN_PM_STOP
;
1467 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_PARITY
)) {
1468 parity
= __get_rpn_parity(rpn
->line_settings
);
1469 if (parity
!= RFCOMM_RPN_PARITY_NONE
) {
1470 BT_DBG("RPN parity mismatch 0x%x", parity
);
1471 parity
= RFCOMM_RPN_PARITY_NONE
;
1472 rpn_mask
^= RFCOMM_RPN_PM_PARITY
;
1476 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_FLOW
)) {
1477 flow_ctrl
= rpn
->flow_ctrl
;
1478 if (flow_ctrl
!= RFCOMM_RPN_FLOW_NONE
) {
1479 BT_DBG("RPN flow ctrl mismatch 0x%x", flow_ctrl
);
1480 flow_ctrl
= RFCOMM_RPN_FLOW_NONE
;
1481 rpn_mask
^= RFCOMM_RPN_PM_FLOW
;
1485 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_XON
)) {
1486 xon_char
= rpn
->xon_char
;
1487 if (xon_char
!= RFCOMM_RPN_XON_CHAR
) {
1488 BT_DBG("RPN XON char mismatch 0x%x", xon_char
);
1489 xon_char
= RFCOMM_RPN_XON_CHAR
;
1490 rpn_mask
^= RFCOMM_RPN_PM_XON
;
1494 if (rpn
->param_mask
& cpu_to_le16(RFCOMM_RPN_PM_XOFF
)) {
1495 xoff_char
= rpn
->xoff_char
;
1496 if (xoff_char
!= RFCOMM_RPN_XOFF_CHAR
) {
1497 BT_DBG("RPN XOFF char mismatch 0x%x", xoff_char
);
1498 xoff_char
= RFCOMM_RPN_XOFF_CHAR
;
1499 rpn_mask
^= RFCOMM_RPN_PM_XOFF
;
1504 rfcomm_send_rpn(s
, 0, dlci
, bit_rate
, data_bits
, stop_bits
,
1505 parity
, flow_ctrl
, xon_char
, xoff_char
, rpn_mask
);
1510 static int rfcomm_recv_rls(struct rfcomm_session
*s
, int cr
, struct sk_buff
*skb
)
1512 struct rfcomm_rls
*rls
= (void *) skb
->data
;
1513 u8 dlci
= __get_dlci(rls
->dlci
);
1515 BT_DBG("dlci %d cr %d status 0x%x", dlci
, cr
, rls
->status
);
1520 /* We should probably do something with this information here. But
1521 * for now it's sufficient just to reply -- Bluetooth 1.1 says it's
1522 * mandatory to recognise and respond to RLS */
1524 rfcomm_send_rls(s
, 0, dlci
, rls
->status
);
1529 static int rfcomm_recv_msc(struct rfcomm_session
*s
, int cr
, struct sk_buff
*skb
)
1531 struct rfcomm_msc
*msc
= (void *) skb
->data
;
1532 struct rfcomm_dlc
*d
;
1533 u8 dlci
= __get_dlci(msc
->dlci
);
1535 BT_DBG("dlci %d cr %d v24 0x%x", dlci
, cr
, msc
->v24_sig
);
1537 d
= rfcomm_dlc_get(s
, dlci
);
1542 if (msc
->v24_sig
& RFCOMM_V24_FC
&& !d
->cfc
)
1543 set_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1545 clear_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1549 d
->remote_v24_sig
= msc
->v24_sig
;
1551 if (d
->modem_status
)
1552 d
->modem_status(d
, msc
->v24_sig
);
1554 rfcomm_dlc_unlock(d
);
1556 rfcomm_send_msc(s
, 0, dlci
, msc
->v24_sig
);
1558 d
->mscex
|= RFCOMM_MSCEX_RX
;
1560 d
->mscex
|= RFCOMM_MSCEX_TX
;
1565 static int rfcomm_recv_mcc(struct rfcomm_session
*s
, struct sk_buff
*skb
)
1567 struct rfcomm_mcc
*mcc
= (void *) skb
->data
;
1570 cr
= __test_cr(mcc
->type
);
1571 type
= __get_mcc_type(mcc
->type
);
1572 len
= __get_mcc_len(mcc
->len
);
1574 BT_DBG("%p type 0x%x cr %d", s
, type
, cr
);
1580 rfcomm_recv_pn(s
, cr
, skb
);
1584 rfcomm_recv_rpn(s
, cr
, len
, skb
);
1588 rfcomm_recv_rls(s
, cr
, skb
);
1592 rfcomm_recv_msc(s
, cr
, skb
);
1597 set_bit(RFCOMM_TX_THROTTLED
, &s
->flags
);
1598 rfcomm_send_fcoff(s
, 0);
1604 clear_bit(RFCOMM_TX_THROTTLED
, &s
->flags
);
1605 rfcomm_send_fcon(s
, 0);
1611 rfcomm_send_test(s
, 0, skb
->data
, skb
->len
);
1618 BT_ERR("Unknown control type 0x%02x", type
);
1619 rfcomm_send_nsc(s
, cr
, type
);
1625 static int rfcomm_recv_data(struct rfcomm_session
*s
, u8 dlci
, int pf
, struct sk_buff
*skb
)
1627 struct rfcomm_dlc
*d
;
1629 BT_DBG("session %p state %ld dlci %d pf %d", s
, s
->state
, dlci
, pf
);
1631 d
= rfcomm_dlc_get(s
, dlci
);
1633 rfcomm_send_dm(s
, dlci
);
1638 u8 credits
= *(u8
*) skb
->data
; skb_pull(skb
, 1);
1640 d
->tx_credits
+= credits
;
1642 clear_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1645 if (skb
->len
&& d
->state
== BT_CONNECTED
) {
1648 d
->data_ready(d
, skb
);
1649 rfcomm_dlc_unlock(d
);
1658 static int rfcomm_recv_frame(struct rfcomm_session
*s
, struct sk_buff
*skb
)
1660 struct rfcomm_hdr
*hdr
= (void *) skb
->data
;
1663 dlci
= __get_dlci(hdr
->addr
);
1664 type
= __get_type(hdr
->ctrl
);
1667 skb
->len
--; skb
->tail
--;
1668 fcs
= *(u8
*)skb_tail_pointer(skb
);
1670 if (__check_fcs(skb
->data
, type
, fcs
)) {
1671 BT_ERR("bad checksum in packet");
1676 if (__test_ea(hdr
->len
))
1683 if (__test_pf(hdr
->ctrl
))
1684 rfcomm_recv_sabm(s
, dlci
);
1688 if (__test_pf(hdr
->ctrl
))
1689 rfcomm_recv_disc(s
, dlci
);
1693 if (__test_pf(hdr
->ctrl
))
1694 rfcomm_recv_ua(s
, dlci
);
1698 rfcomm_recv_dm(s
, dlci
);
1703 return rfcomm_recv_data(s
, dlci
, __test_pf(hdr
->ctrl
), skb
);
1705 rfcomm_recv_mcc(s
, skb
);
1709 BT_ERR("Unknown packet type 0x%02x", type
);
1716 /* ---- Connection and data processing ---- */
1718 static void rfcomm_process_connect(struct rfcomm_session
*s
)
1720 struct rfcomm_dlc
*d
;
1721 struct list_head
*p
, *n
;
1723 BT_DBG("session %p state %ld", s
, s
->state
);
1725 list_for_each_safe(p
, n
, &s
->dlcs
) {
1726 d
= list_entry(p
, struct rfcomm_dlc
, list
);
1727 if (d
->state
== BT_CONFIG
) {
1729 if (rfcomm_check_security(d
)) {
1730 rfcomm_send_pn(s
, 1, d
);
1732 set_bit(RFCOMM_AUTH_PENDING
, &d
->flags
);
1733 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
1739 /* Send data queued for the DLC.
1740 * Return number of frames left in the queue.
1742 static inline int rfcomm_process_tx(struct rfcomm_dlc
*d
)
1744 struct sk_buff
*skb
;
1747 BT_DBG("dlc %p state %ld cfc %d rx_credits %d tx_credits %d",
1748 d
, d
->state
, d
->cfc
, d
->rx_credits
, d
->tx_credits
);
1750 /* Send pending MSC */
1751 if (test_and_clear_bit(RFCOMM_MSC_PENDING
, &d
->flags
))
1752 rfcomm_send_msc(d
->session
, 1, d
->dlci
, d
->v24_sig
);
1756 * Give them some credits */
1757 if (!test_bit(RFCOMM_RX_THROTTLED
, &d
->flags
) &&
1758 d
->rx_credits
<= (d
->cfc
>> 2)) {
1759 rfcomm_send_credits(d
->session
, d
->addr
, d
->cfc
- d
->rx_credits
);
1760 d
->rx_credits
= d
->cfc
;
1764 * Give ourselves some credits */
1768 if (test_bit(RFCOMM_TX_THROTTLED
, &d
->flags
))
1769 return skb_queue_len(&d
->tx_queue
);
1771 while (d
->tx_credits
&& (skb
= skb_dequeue(&d
->tx_queue
))) {
1772 err
= rfcomm_send_frame(d
->session
, skb
->data
, skb
->len
);
1774 skb_queue_head(&d
->tx_queue
, skb
);
1781 if (d
->cfc
&& !d
->tx_credits
) {
1782 /* We're out of TX credits.
1783 * Set TX_THROTTLED flag to avoid unnesary wakeups by dlc_send. */
1784 set_bit(RFCOMM_TX_THROTTLED
, &d
->flags
);
1787 return skb_queue_len(&d
->tx_queue
);
1790 static inline void rfcomm_process_dlcs(struct rfcomm_session
*s
)
1792 struct rfcomm_dlc
*d
;
1793 struct list_head
*p
, *n
;
1795 BT_DBG("session %p state %ld", s
, s
->state
);
1797 list_for_each_safe(p
, n
, &s
->dlcs
) {
1798 d
= list_entry(p
, struct rfcomm_dlc
, list
);
1800 if (test_bit(RFCOMM_TIMED_OUT
, &d
->flags
)) {
1801 __rfcomm_dlc_close(d
, ETIMEDOUT
);
1805 if (test_and_clear_bit(RFCOMM_AUTH_ACCEPT
, &d
->flags
)) {
1806 rfcomm_dlc_clear_timer(d
);
1808 rfcomm_send_pn(s
, 1, d
);
1809 rfcomm_dlc_set_timer(d
, RFCOMM_CONN_TIMEOUT
);
1811 if (d
->defer_setup
) {
1812 set_bit(RFCOMM_DEFER_SETUP
, &d
->flags
);
1813 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
1816 d
->state
= BT_CONNECT2
;
1817 d
->state_change(d
, 0);
1818 rfcomm_dlc_unlock(d
);
1820 rfcomm_dlc_accept(d
);
1823 } else if (test_and_clear_bit(RFCOMM_AUTH_REJECT
, &d
->flags
)) {
1824 rfcomm_dlc_clear_timer(d
);
1826 rfcomm_send_dm(s
, d
->dlci
);
1828 d
->state
= BT_CLOSED
;
1829 __rfcomm_dlc_close(d
, ECONNREFUSED
);
1833 if (test_bit(RFCOMM_SEC_PENDING
, &d
->flags
))
1836 if (test_bit(RFCOMM_TX_THROTTLED
, &s
->flags
))
1839 if ((d
->state
== BT_CONNECTED
|| d
->state
== BT_DISCONN
) &&
1840 d
->mscex
== RFCOMM_MSCEX_OK
)
1841 rfcomm_process_tx(d
);
1845 static inline void rfcomm_process_rx(struct rfcomm_session
*s
)
1847 struct socket
*sock
= s
->sock
;
1848 struct sock
*sk
= sock
->sk
;
1849 struct sk_buff
*skb
;
1851 BT_DBG("session %p state %ld qlen %d", s
, s
->state
, skb_queue_len(&sk
->sk_receive_queue
));
1853 /* Get data directly from socket receive queue without copying it. */
1854 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
))) {
1856 if (!skb_linearize(skb
))
1857 rfcomm_recv_frame(s
, skb
);
1862 if (sk
->sk_state
== BT_CLOSED
) {
1864 rfcomm_session_put(s
);
1866 rfcomm_session_close(s
, sk
->sk_err
);
1870 static inline void rfcomm_accept_connection(struct rfcomm_session
*s
)
1872 struct socket
*sock
= s
->sock
, *nsock
;
1875 /* Fast check for a new connection.
1876 * Avoids unnesesary socket allocations. */
1877 if (list_empty(&bt_sk(sock
->sk
)->accept_q
))
1880 BT_DBG("session %p", s
);
1882 err
= kernel_accept(sock
, &nsock
, O_NONBLOCK
);
1886 /* Set our callbacks */
1887 nsock
->sk
->sk_data_ready
= rfcomm_l2data_ready
;
1888 nsock
->sk
->sk_state_change
= rfcomm_l2state_change
;
1890 s
= rfcomm_session_add(nsock
, BT_OPEN
);
1892 rfcomm_session_hold(s
);
1894 /* We should adjust MTU on incoming sessions.
1895 * L2CAP MTU minus UIH header and FCS. */
1896 s
->mtu
= min(l2cap_pi(nsock
->sk
)->chan
->omtu
,
1897 l2cap_pi(nsock
->sk
)->chan
->imtu
) - 5;
1901 sock_release(nsock
);
1904 static inline void rfcomm_check_connection(struct rfcomm_session
*s
)
1906 struct sock
*sk
= s
->sock
->sk
;
1908 BT_DBG("%p state %ld", s
, s
->state
);
1910 switch (sk
->sk_state
) {
1912 s
->state
= BT_CONNECT
;
1914 /* We can adjust MTU on outgoing sessions.
1915 * L2CAP MTU minus UIH header and FCS. */
1916 s
->mtu
= min(l2cap_pi(sk
)->chan
->omtu
, l2cap_pi(sk
)->chan
->imtu
) - 5;
1918 rfcomm_send_sabm(s
, 0);
1922 s
->state
= BT_CLOSED
;
1923 rfcomm_session_close(s
, sk
->sk_err
);
1928 static inline void rfcomm_process_sessions(void)
1930 struct list_head
*p
, *n
;
1934 list_for_each_safe(p
, n
, &session_list
) {
1935 struct rfcomm_session
*s
;
1936 s
= list_entry(p
, struct rfcomm_session
, list
);
1938 if (test_and_clear_bit(RFCOMM_TIMED_OUT
, &s
->flags
)) {
1939 s
->state
= BT_DISCONN
;
1940 rfcomm_send_disc(s
, 0);
1941 rfcomm_session_put(s
);
1945 if (s
->state
== BT_LISTEN
) {
1946 rfcomm_accept_connection(s
);
1950 rfcomm_session_hold(s
);
1954 rfcomm_check_connection(s
);
1958 rfcomm_process_rx(s
);
1962 rfcomm_process_dlcs(s
);
1964 rfcomm_session_put(s
);
1970 static int rfcomm_add_listener(bdaddr_t
*ba
)
1972 struct sockaddr_l2 addr
;
1973 struct socket
*sock
;
1975 struct rfcomm_session
*s
;
1979 err
= rfcomm_l2sock_create(&sock
);
1981 BT_ERR("Create socket failed %d", err
);
1986 bacpy(&addr
.l2_bdaddr
, ba
);
1987 addr
.l2_family
= AF_BLUETOOTH
;
1988 addr
.l2_psm
= cpu_to_le16(RFCOMM_PSM
);
1990 err
= kernel_bind(sock
, (struct sockaddr
*) &addr
, sizeof(addr
));
1992 BT_ERR("Bind failed %d", err
);
1996 /* Set L2CAP options */
1999 l2cap_pi(sk
)->chan
->imtu
= l2cap_mtu
;
2002 /* Start listening on the socket */
2003 err
= kernel_listen(sock
, 10);
2005 BT_ERR("Listen failed %d", err
);
2009 /* Add listening session */
2010 s
= rfcomm_session_add(sock
, BT_LISTEN
);
2014 rfcomm_session_hold(s
);
2021 static void rfcomm_kill_listener(void)
2023 struct rfcomm_session
*s
;
2024 struct list_head
*p
, *n
;
2028 list_for_each_safe(p
, n
, &session_list
) {
2029 s
= list_entry(p
, struct rfcomm_session
, list
);
2030 rfcomm_session_del(s
);
2034 static int rfcomm_run(void *unused
)
2038 set_user_nice(current
, -10);
2040 rfcomm_add_listener(BDADDR_ANY
);
2043 set_current_state(TASK_INTERRUPTIBLE
);
2045 if (kthread_should_stop())
2049 rfcomm_process_sessions();
2053 __set_current_state(TASK_RUNNING
);
2055 rfcomm_kill_listener();
2060 static void rfcomm_security_cfm(struct hci_conn
*conn
, u8 status
, u8 encrypt
)
2062 struct rfcomm_session
*s
;
2063 struct rfcomm_dlc
*d
;
2064 struct list_head
*p
, *n
;
2066 BT_DBG("conn %p status 0x%02x encrypt 0x%02x", conn
, status
, encrypt
);
2068 s
= rfcomm_session_get(&conn
->hdev
->bdaddr
, &conn
->dst
);
2072 rfcomm_session_hold(s
);
2074 list_for_each_safe(p
, n
, &s
->dlcs
) {
2075 d
= list_entry(p
, struct rfcomm_dlc
, list
);
2077 if (test_and_clear_bit(RFCOMM_SEC_PENDING
, &d
->flags
)) {
2078 rfcomm_dlc_clear_timer(d
);
2079 if (status
|| encrypt
== 0x00) {
2080 __rfcomm_dlc_close(d
, ECONNREFUSED
);
2085 if (d
->state
== BT_CONNECTED
&& !status
&& encrypt
== 0x00) {
2086 if (d
->sec_level
== BT_SECURITY_MEDIUM
) {
2087 set_bit(RFCOMM_SEC_PENDING
, &d
->flags
);
2088 rfcomm_dlc_set_timer(d
, RFCOMM_AUTH_TIMEOUT
);
2090 } else if (d
->sec_level
== BT_SECURITY_HIGH
) {
2091 __rfcomm_dlc_close(d
, ECONNREFUSED
);
2096 if (!test_and_clear_bit(RFCOMM_AUTH_PENDING
, &d
->flags
))
2099 if (!status
&& hci_conn_check_secure(conn
, d
->sec_level
))
2100 set_bit(RFCOMM_AUTH_ACCEPT
, &d
->flags
);
2102 set_bit(RFCOMM_AUTH_REJECT
, &d
->flags
);
2105 rfcomm_session_put(s
);
2110 static struct hci_cb rfcomm_cb
= {
2112 .security_cfm
= rfcomm_security_cfm
2115 static int rfcomm_dlc_debugfs_show(struct seq_file
*f
, void *x
)
2117 struct rfcomm_session
*s
;
2118 struct list_head
*pp
, *p
;
2122 list_for_each(p
, &session_list
) {
2123 s
= list_entry(p
, struct rfcomm_session
, list
);
2124 list_for_each(pp
, &s
->dlcs
) {
2125 struct sock
*sk
= s
->sock
->sk
;
2126 struct rfcomm_dlc
*d
= list_entry(pp
, struct rfcomm_dlc
, list
);
2128 seq_printf(f
, "%s %s %ld %d %d %d %d\n",
2129 batostr(&bt_sk(sk
)->src
),
2130 batostr(&bt_sk(sk
)->dst
),
2131 d
->state
, d
->dlci
, d
->mtu
,
2132 d
->rx_credits
, d
->tx_credits
);
2141 static int rfcomm_dlc_debugfs_open(struct inode
*inode
, struct file
*file
)
2143 return single_open(file
, rfcomm_dlc_debugfs_show
, inode
->i_private
);
2146 static const struct file_operations rfcomm_dlc_debugfs_fops
= {
2147 .open
= rfcomm_dlc_debugfs_open
,
2149 .llseek
= seq_lseek
,
2150 .release
= single_release
,
2153 static struct dentry
*rfcomm_dlc_debugfs
;
2155 /* ---- Initialization ---- */
2156 static int __init
rfcomm_init(void)
2160 hci_register_cb(&rfcomm_cb
);
2162 rfcomm_thread
= kthread_run(rfcomm_run
, NULL
, "krfcommd");
2163 if (IS_ERR(rfcomm_thread
)) {
2164 err
= PTR_ERR(rfcomm_thread
);
2169 rfcomm_dlc_debugfs
= debugfs_create_file("rfcomm_dlc", 0444,
2170 bt_debugfs
, NULL
, &rfcomm_dlc_debugfs_fops
);
2171 if (!rfcomm_dlc_debugfs
)
2172 BT_ERR("Failed to create RFCOMM debug file");
2175 err
= rfcomm_init_ttys();
2179 err
= rfcomm_init_sockets();
2183 BT_INFO("RFCOMM ver %s", VERSION
);
2188 rfcomm_cleanup_ttys();
2191 kthread_stop(rfcomm_thread
);
2194 hci_unregister_cb(&rfcomm_cb
);
2199 static void __exit
rfcomm_exit(void)
2201 debugfs_remove(rfcomm_dlc_debugfs
);
2203 hci_unregister_cb(&rfcomm_cb
);
2205 kthread_stop(rfcomm_thread
);
2207 rfcomm_cleanup_ttys();
2209 rfcomm_cleanup_sockets();
2212 module_init(rfcomm_init
);
2213 module_exit(rfcomm_exit
);
2215 module_param(disable_cfc
, bool, 0644);
2216 MODULE_PARM_DESC(disable_cfc
, "Disable credit based flow control");
2218 module_param(channel_mtu
, int, 0644);
2219 MODULE_PARM_DESC(channel_mtu
, "Default MTU for the RFCOMM channel");
2221 module_param(l2cap_mtu
, uint
, 0644);
2222 MODULE_PARM_DESC(l2cap_mtu
, "Default MTU for the L2CAP connection");
2224 module_param(l2cap_ertm
, bool, 0644);
2225 MODULE_PARM_DESC(l2cap_ertm
, "Use L2CAP ERTM mode for connection");
2227 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2228 MODULE_DESCRIPTION("Bluetooth RFCOMM ver " VERSION
);
2229 MODULE_VERSION(VERSION
);
2230 MODULE_LICENSE("GPL");
2231 MODULE_ALIAS("bt-proto-3");