blob: e5ddef081e69d50b344fd81700b5ed048c818e03 [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 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>
5
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;
9
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
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090014 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090019 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 SOFTWARE IS DISCLAIMED.
22*/
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
25 * Bluetooth RFCOMM core.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Marcel Holtmannbe9d1222005-11-08 09:57:38 -080035#include <linux/device.h>
Marcel Holtmannaef7d972010-03-21 05:27:45 +010036#include <linux/debugfs.h>
37#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/net.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080039#include <linux/mutex.h>
Marcel Holtmanna524ecc2007-10-20 21:37:20 +020040#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <net/sock.h>
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020044#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/unaligned.h>
46
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>
51
Marcel Holtmann5f9018a2009-01-16 10:09:50 +010052#define VERSION "1.11"
Marcel Holtmann56f3a402006-02-13 11:39:57 +010053
Rusty Russelleb939922011-12-19 14:08:01 +000054static bool disable_cfc;
55static bool l2cap_ertm;
Marcel Holtmann98bcd082006-07-14 11:42:12 +020056static int channel_mtu = -1;
Marcel Holtmann56f3a402006-02-13 11:39:57 +010057static unsigned int l2cap_mtu = RFCOMM_MAX_L2CAP_MTU;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static struct task_struct *rfcomm_thread;
60
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080061static DEFINE_MUTEX(rfcomm_mutex);
62#define rfcomm_lock() mutex_lock(&rfcomm_mutex)
63#define rfcomm_unlock() mutex_unlock(&rfcomm_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66static LIST_HEAD(session_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +020068static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len,
69 u32 priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci);
71static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci);
72static int rfcomm_queue_disc(struct rfcomm_dlc *d);
73static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type);
74static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d);
75static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig);
76static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len);
77static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits);
78static void rfcomm_make_uih(struct sk_buff *skb, u8 addr);
79
80static void rfcomm_process_connect(struct rfcomm_session *s);
81
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +030082static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
83 bdaddr_t *dst,
84 u8 sec_level,
85 int *err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst);
87static void rfcomm_session_del(struct rfcomm_session *s);
88
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))
94
95#define __test_ea(b) ((b & 0x01))
96#define __test_cr(b) ((b & 0x02))
97#define __test_pf(b) ((b & 0x10))
98
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)
104
105#define __len8(len) (((len) << 1) | 1)
106#define __len16(len) ((len) << 1)
107
108/* MCC macros */
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)
112
113/* RPN macros */
J. Suter3a5e9032005-08-09 20:28:46 -0700114#define __rpn_line_settings(data, stop, parity) ((data & 0x3) | ((stop & 0x1) << 2) | ((parity & 0x7) << 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define __get_rpn_data_bits(line) ((line) & 0x3)
116#define __get_rpn_stop_bits(line) (((line) >> 2) & 0x1)
J. Suter3a5e9032005-08-09 20:28:46 -0700117#define __get_rpn_parity(line) (((line) >> 3) & 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300119static inline void rfcomm_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 if (!rfcomm_thread)
122 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 wake_up_process(rfcomm_thread);
124}
125
126static inline void rfcomm_session_put(struct rfcomm_session *s)
127{
128 if (atomic_dec_and_test(&s->refcnt))
129 rfcomm_session_del(s);
130}
131
132/* ---- RFCOMM FCS computation ---- */
133
Marcel Holtmann408c1ce2005-10-28 19:20:36 +0200134/* reversed, 8-bit, poly=0x07 */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900135static unsigned char rfcomm_crc_table[256] = {
Marcel Holtmann408c1ce2005-10-28 19:20:36 +0200136 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
137 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
138 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
139 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
140
141 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
142 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
143 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
144 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
145
146 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
147 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
148 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
149 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
150
151 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
152 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
153 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
154 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
155
156 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
157 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
158 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
159 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
160
161 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
162 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
163 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
164 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
165
166 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
167 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
168 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
169 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
170
171 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
172 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
173 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
174 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
175};
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/* CRC on 2 bytes */
178#define __crc(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]])
179
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900180/* FCS on 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static inline u8 __fcs(u8 *data)
182{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000183 return 0xff - __crc(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900186/* FCS on 3 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static inline u8 __fcs2(u8 *data)
188{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000189 return 0xff - rfcomm_crc_table[__crc(data) ^ data[2]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192/* Check FCS */
193static inline int __check_fcs(u8 *data, int type, u8 fcs)
194{
195 u8 f = __crc(data);
196
197 if (type != RFCOMM_UIH)
198 f = rfcomm_crc_table[f ^ data[2]];
199
200 return rfcomm_crc_table[f ^ fcs] != 0xcf;
201}
202
203/* ---- L2CAP callbacks ---- */
204static void rfcomm_l2state_change(struct sock *sk)
205{
206 BT_DBG("%p state %d", sk, sk->sk_state);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300207 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210static void rfcomm_l2data_ready(struct sock *sk, int bytes)
211{
212 BT_DBG("%p bytes %d", sk, bytes);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300213 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216static int rfcomm_l2sock_create(struct socket **sock)
217{
218 int err;
219
220 BT_DBG("");
221
222 err = sock_create_kern(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP, sock);
223 if (!err) {
224 struct sock *sk = (*sock)->sk;
225 sk->sk_data_ready = rfcomm_l2data_ready;
226 sk->sk_state_change = rfcomm_l2state_change;
227 }
228 return err;
229}
230
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100231static inline int rfcomm_check_security(struct rfcomm_dlc *d)
Marcel Holtmann77db1982008-07-14 20:13:45 +0200232{
233 struct sock *sk = d->session->sock->sk;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300234 struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
235
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100236 __u8 auth_type;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200237
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100238 switch (d->sec_level) {
239 case BT_SECURITY_HIGH:
240 auth_type = HCI_AT_GENERAL_BONDING_MITM;
241 break;
242 case BT_SECURITY_MEDIUM:
243 auth_type = HCI_AT_GENERAL_BONDING;
244 break;
245 default:
246 auth_type = HCI_AT_NO_BONDING;
247 break;
248 }
249
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300250 return hci_conn_security(conn->hcon, d->sec_level, auth_type);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200251}
252
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300253static void rfcomm_session_timeout(unsigned long arg)
254{
255 struct rfcomm_session *s = (void *) arg;
256
257 BT_DBG("session %p state %ld", s, s->state);
258
259 set_bit(RFCOMM_TIMED_OUT, &s->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300260 rfcomm_schedule();
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300261}
262
263static void rfcomm_session_set_timer(struct rfcomm_session *s, long timeout)
264{
265 BT_DBG("session %p state %ld timeout %ld", s, s->state, timeout);
266
267 if (!mod_timer(&s->timer, jiffies + timeout))
268 rfcomm_session_hold(s);
269}
270
271static void rfcomm_session_clear_timer(struct rfcomm_session *s)
272{
273 BT_DBG("session %p state %ld", s, s->state);
274
275 if (timer_pending(&s->timer) && del_timer(&s->timer))
276 rfcomm_session_put(s);
277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/* ---- RFCOMM DLCs ---- */
280static void rfcomm_dlc_timeout(unsigned long arg)
281{
282 struct rfcomm_dlc *d = (void *) arg;
283
284 BT_DBG("dlc %p state %ld", d, d->state);
285
286 set_bit(RFCOMM_TIMED_OUT, &d->flags);
287 rfcomm_dlc_put(d);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300288 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291static void rfcomm_dlc_set_timer(struct rfcomm_dlc *d, long timeout)
292{
293 BT_DBG("dlc %p state %ld timeout %ld", d, d->state, timeout);
294
295 if (!mod_timer(&d->timer, jiffies + timeout))
296 rfcomm_dlc_hold(d);
297}
298
299static void rfcomm_dlc_clear_timer(struct rfcomm_dlc *d)
300{
301 BT_DBG("dlc %p state %ld", d, d->state);
302
303 if (timer_pending(&d->timer) && del_timer(&d->timer))
304 rfcomm_dlc_put(d);
305}
306
307static void rfcomm_dlc_clear_state(struct rfcomm_dlc *d)
308{
309 BT_DBG("%p", d);
310
311 d->state = BT_OPEN;
312 d->flags = 0;
313 d->mscex = 0;
Johan Hedberg183f7322010-12-06 15:56:17 +0200314 d->sec_level = BT_SECURITY_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 d->mtu = RFCOMM_DEFAULT_MTU;
316 d->v24_sig = RFCOMM_V24_RTC | RFCOMM_V24_RTR | RFCOMM_V24_DV;
317
318 d->cfc = RFCOMM_CFC_DISABLED;
319 d->rx_credits = RFCOMM_DEFAULT_CREDITS;
320}
321
Al Virodd0fc662005-10-07 07:46:04 +0100322struct rfcomm_dlc *rfcomm_dlc_alloc(gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200324 struct rfcomm_dlc *d = kzalloc(sizeof(*d), prio);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!d)
327 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800329 setup_timer(&d->timer, rfcomm_dlc_timeout, (unsigned long)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 skb_queue_head_init(&d->tx_queue);
332 spin_lock_init(&d->lock);
333 atomic_set(&d->refcnt, 1);
334
335 rfcomm_dlc_clear_state(d);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 BT_DBG("%p", d);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return d;
340}
341
342void rfcomm_dlc_free(struct rfcomm_dlc *d)
343{
344 BT_DBG("%p", d);
345
346 skb_queue_purge(&d->tx_queue);
347 kfree(d);
348}
349
350static void rfcomm_dlc_link(struct rfcomm_session *s, struct rfcomm_dlc *d)
351{
352 BT_DBG("dlc %p session %p", d, s);
353
354 rfcomm_session_hold(s);
355
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300356 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 rfcomm_dlc_hold(d);
358 list_add(&d->list, &s->dlcs);
359 d->session = s;
360}
361
362static void rfcomm_dlc_unlink(struct rfcomm_dlc *d)
363{
364 struct rfcomm_session *s = d->session;
365
366 BT_DBG("dlc %p refcnt %d session %p", d, atomic_read(&d->refcnt), s);
367
368 list_del(&d->list);
369 d->session = NULL;
370 rfcomm_dlc_put(d);
371
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300372 if (list_empty(&s->dlcs))
373 rfcomm_session_set_timer(s, RFCOMM_IDLE_TIMEOUT);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 rfcomm_session_put(s);
376}
377
378static struct rfcomm_dlc *rfcomm_dlc_get(struct rfcomm_session *s, u8 dlci)
379{
380 struct rfcomm_dlc *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200382 list_for_each_entry(d, &s->dlcs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (d->dlci == dlci)
384 return d;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return NULL;
387}
388
389static int __rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
390{
391 struct rfcomm_session *s;
392 int err = 0;
393 u8 dlci;
394
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900395 BT_DBG("dlc %p state %ld %s %s channel %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 d, d->state, batostr(src), batostr(dst), channel);
397
398 if (channel < 1 || channel > 30)
399 return -EINVAL;
400
401 if (d->state != BT_OPEN && d->state != BT_CLOSED)
402 return 0;
403
404 s = rfcomm_session_get(src, dst);
405 if (!s) {
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +0300406 s = rfcomm_session_create(src, dst, d->sec_level, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!s)
408 return err;
409 }
410
411 dlci = __dlci(!s->initiator, channel);
412
413 /* Check if DLCI already exists */
414 if (rfcomm_dlc_get(s, dlci))
415 return -EBUSY;
416
417 rfcomm_dlc_clear_state(d);
418
419 d->dlci = dlci;
420 d->addr = __addr(s->initiator, dlci);
421 d->priority = 7;
422
Marcel Holtmann77db1982008-07-14 20:13:45 +0200423 d->state = BT_CONFIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 rfcomm_dlc_link(s, d);
425
Marcel Holtmann77db1982008-07-14 20:13:45 +0200426 d->out = 1;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 d->mtu = s->mtu;
429 d->cfc = (s->cfc == RFCOMM_CFC_UNKNOWN) ? 0 : s->cfc;
430
Marcel Holtmann77db1982008-07-14 20:13:45 +0200431 if (s->state == BT_CONNECTED) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100432 if (rfcomm_check_security(d))
Marcel Holtmann77db1982008-07-14 20:13:45 +0200433 rfcomm_send_pn(s, 1, d);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100434 else
435 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200436 }
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return 0;
441}
442
443int rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
444{
445 int r;
446
447 rfcomm_lock();
448
449 r = __rfcomm_dlc_open(d, src, dst, channel);
450
451 rfcomm_unlock();
452 return r;
453}
454
455static int __rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
456{
457 struct rfcomm_session *s = d->session;
458 if (!s)
459 return 0;
460
461 BT_DBG("dlc %p state %ld dlci %d err %d session %p",
462 d, d->state, d->dlci, err, s);
463
464 switch (d->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 case BT_CONNECT:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100466 case BT_CONFIG:
467 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
468 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300469 rfcomm_schedule();
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100470 break;
471 }
472 /* Fall through */
473
474 case BT_CONNECTED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 d->state = BT_DISCONN;
476 if (skb_queue_empty(&d->tx_queue)) {
477 rfcomm_send_disc(s, d->dlci);
478 rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT);
479 } else {
480 rfcomm_queue_disc(d);
481 rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT * 2);
482 }
483 break;
484
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100485 case BT_OPEN:
Marcel Holtmann8bf47942009-02-16 02:59:49 +0100486 case BT_CONNECT2:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100487 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
488 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300489 rfcomm_schedule();
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100490 break;
491 }
492 /* Fall through */
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 default:
495 rfcomm_dlc_clear_timer(d);
496
497 rfcomm_dlc_lock(d);
498 d->state = BT_CLOSED;
Dave Young1905f6c2008-04-01 23:59:06 -0700499 d->state_change(d, err);
Arjan van de Ven4c8411f2008-05-29 01:32:47 -0700500 rfcomm_dlc_unlock(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 skb_queue_purge(&d->tx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 rfcomm_dlc_unlink(d);
504 }
505
506 return 0;
507}
508
509int rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
510{
511 int r;
512
513 rfcomm_lock();
514
515 r = __rfcomm_dlc_close(d, err);
516
517 rfcomm_unlock();
518 return r;
519}
520
521int rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb)
522{
523 int len = skb->len;
524
525 if (d->state != BT_CONNECTED)
526 return -ENOTCONN;
527
528 BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len);
529
530 if (len > d->mtu)
531 return -EINVAL;
532
533 rfcomm_make_uih(skb, d->addr);
534 skb_queue_tail(&d->tx_queue, skb);
535
536 if (!test_bit(RFCOMM_TX_THROTTLED, &d->flags))
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300537 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return len;
539}
540
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800541void __rfcomm_dlc_throttle(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 BT_DBG("dlc %p state %ld", d, d->state);
544
545 if (!d->cfc) {
546 d->v24_sig |= RFCOMM_V24_FC;
547 set_bit(RFCOMM_MSC_PENDING, &d->flags);
548 }
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300549 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800552void __rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 BT_DBG("dlc %p state %ld", d, d->state);
555
556 if (!d->cfc) {
557 d->v24_sig &= ~RFCOMM_V24_FC;
558 set_bit(RFCOMM_MSC_PENDING, &d->flags);
559 }
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300560 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900563/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 Set/get modem status functions use _local_ status i.e. what we report
565 to the other side.
566 Remote status is provided by dlc->modem_status() callback.
567 */
568int rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig)
569{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900570 BT_DBG("dlc %p state %ld v24_sig 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 d, d->state, v24_sig);
572
573 if (test_bit(RFCOMM_RX_THROTTLED, &d->flags))
574 v24_sig |= RFCOMM_V24_FC;
575 else
576 v24_sig &= ~RFCOMM_V24_FC;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 d->v24_sig = v24_sig;
579
580 if (!test_and_set_bit(RFCOMM_MSC_PENDING, &d->flags))
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300581 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 return 0;
584}
585
586int rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig)
587{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900588 BT_DBG("dlc %p state %ld v24_sig 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 d, d->state, d->v24_sig);
590
591 *v24_sig = d->v24_sig;
592 return 0;
593}
594
595/* ---- RFCOMM sessions ---- */
596static struct rfcomm_session *rfcomm_session_add(struct socket *sock, int state)
597{
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200598 struct rfcomm_session *s = kzalloc(sizeof(*s), GFP_KERNEL);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!s)
601 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 BT_DBG("session %p sock %p", s, sock);
604
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300605 setup_timer(&s->timer, rfcomm_session_timeout, (unsigned long) s);
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 INIT_LIST_HEAD(&s->dlcs);
608 s->state = state;
609 s->sock = sock;
610
611 s->mtu = RFCOMM_DEFAULT_MTU;
Marcel Holtmann7c2660b2006-07-03 10:02:51 +0200612 s->cfc = disable_cfc ? RFCOMM_CFC_DISABLED : RFCOMM_CFC_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /* Do not increment module usage count for listening sessions.
615 * Otherwise we won't be able to unload the module. */
616 if (state != BT_LISTEN)
617 if (!try_module_get(THIS_MODULE)) {
618 kfree(s);
619 return NULL;
620 }
621
622 list_add(&s->list, &session_list);
623
624 return s;
625}
626
627static void rfcomm_session_del(struct rfcomm_session *s)
628{
629 int state = s->state;
630
631 BT_DBG("session %p state %ld", s, s->state);
632
633 list_del(&s->list);
634
635 if (state == BT_CONNECTED)
636 rfcomm_send_disc(s, 0);
637
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300638 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 sock_release(s->sock);
640 kfree(s);
641
642 if (state != BT_LISTEN)
643 module_put(THIS_MODULE);
644}
645
646static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst)
647{
648 struct rfcomm_session *s;
649 struct list_head *p, *n;
650 struct bt_sock *sk;
651 list_for_each_safe(p, n, &session_list) {
652 s = list_entry(p, struct rfcomm_session, list);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900653 sk = bt_sk(s->sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if ((!bacmp(src, BDADDR_ANY) || !bacmp(&sk->src, src)) &&
656 !bacmp(&sk->dst, dst))
657 return s;
658 }
659 return NULL;
660}
661
662static void rfcomm_session_close(struct rfcomm_session *s, int err)
663{
664 struct rfcomm_dlc *d;
665 struct list_head *p, *n;
666
667 BT_DBG("session %p state %ld err %d", s, s->state, err);
668
669 rfcomm_session_hold(s);
670
671 s->state = BT_CLOSED;
672
673 /* Close all dlcs */
674 list_for_each_safe(p, n, &s->dlcs) {
675 d = list_entry(p, struct rfcomm_dlc, list);
676 d->state = BT_CLOSED;
677 __rfcomm_dlc_close(d, err);
678 }
679
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300680 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 rfcomm_session_put(s);
682}
683
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +0300684static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
685 bdaddr_t *dst,
686 u8 sec_level,
687 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 struct rfcomm_session *s = NULL;
690 struct sockaddr_l2 addr;
691 struct socket *sock;
692 struct sock *sk;
693
694 BT_DBG("%s %s", batostr(src), batostr(dst));
695
696 *err = rfcomm_l2sock_create(&sock);
697 if (*err < 0)
698 return NULL;
699
700 bacpy(&addr.l2_bdaddr, src);
701 addr.l2_family = AF_BLUETOOTH;
702 addr.l2_psm = 0;
Marcel Holtmann37e62f52009-02-17 21:49:33 +0100703 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +0200704 *err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (*err < 0)
706 goto failed;
707
708 /* Set L2CAP options */
709 sk = sock->sk;
710 lock_sock(sk);
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300711 l2cap_pi(sk)->chan->imtu = l2cap_mtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300712 l2cap_pi(sk)->chan->sec_level = sec_level;
Marcel Holtmanneae38ee2009-10-05 12:23:48 +0200713 if (l2cap_ertm)
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300714 l2cap_pi(sk)->chan->mode = L2CAP_MODE_ERTM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 release_sock(sk);
716
717 s = rfcomm_session_add(sock, BT_BOUND);
718 if (!s) {
719 *err = -ENOMEM;
720 goto failed;
721 }
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 s->initiator = 1;
724
725 bacpy(&addr.l2_bdaddr, dst);
726 addr.l2_family = AF_BLUETOOTH;
Marcel Holtmannb4324b52009-06-07 18:06:51 +0200727 addr.l2_psm = cpu_to_le16(RFCOMM_PSM);
Marcel Holtmann37e62f52009-02-17 21:49:33 +0100728 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +0200729 *err = kernel_connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK);
Marcel Holtmannb4c612a2006-09-23 09:54:38 +0200730 if (*err == 0 || *err == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return s;
732
733 rfcomm_session_del(s);
734 return NULL;
735
736failed:
737 sock_release(sock);
738 return NULL;
739}
740
741void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst)
742{
743 struct sock *sk = s->sock->sk;
744 if (src)
745 bacpy(src, &bt_sk(sk)->src);
746 if (dst)
747 bacpy(dst, &bt_sk(sk)->dst);
748}
749
750/* ---- RFCOMM frame sending ---- */
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200751static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len,
752 u32 priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct socket *sock = s->sock;
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200755 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct kvec iv = { data, len };
757 struct msghdr msg;
758
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200759 BT_DBG("session %p len %d priority %u", s, len, priority);
760
761 if (sk->sk_priority != priority) {
762 lock_sock(sk);
763 sk->sk_priority = priority;
764 release_sock(sk);
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 memset(&msg, 0, sizeof(msg));
768
769 return kernel_sendmsg(sock, &msg, &iv, 1, len);
770}
771
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200772static int rfcomm_send_cmd(struct rfcomm_session *s, struct rfcomm_cmd *cmd)
773{
774 BT_DBG("%p cmd %u", s, cmd->ctrl);
775
776 return rfcomm_send_frame(s, (void *) cmd, sizeof(*cmd), HCI_PRIO_MAX);
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci)
780{
781 struct rfcomm_cmd cmd;
782
783 BT_DBG("%p dlci %d", s, dlci);
784
785 cmd.addr = __addr(s->initiator, dlci);
786 cmd.ctrl = __ctrl(RFCOMM_SABM, 1);
787 cmd.len = __len8(0);
788 cmd.fcs = __fcs2((u8 *) &cmd);
789
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200790 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793static int rfcomm_send_ua(struct rfcomm_session *s, u8 dlci)
794{
795 struct rfcomm_cmd cmd;
796
797 BT_DBG("%p dlci %d", s, dlci);
798
799 cmd.addr = __addr(!s->initiator, dlci);
800 cmd.ctrl = __ctrl(RFCOMM_UA, 1);
801 cmd.len = __len8(0);
802 cmd.fcs = __fcs2((u8 *) &cmd);
803
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200804 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci)
808{
809 struct rfcomm_cmd cmd;
810
811 BT_DBG("%p dlci %d", s, dlci);
812
813 cmd.addr = __addr(s->initiator, dlci);
814 cmd.ctrl = __ctrl(RFCOMM_DISC, 1);
815 cmd.len = __len8(0);
816 cmd.fcs = __fcs2((u8 *) &cmd);
817
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200818 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821static int rfcomm_queue_disc(struct rfcomm_dlc *d)
822{
823 struct rfcomm_cmd *cmd;
824 struct sk_buff *skb;
825
826 BT_DBG("dlc %p dlci %d", d, d->dlci);
827
828 skb = alloc_skb(sizeof(*cmd), GFP_KERNEL);
829 if (!skb)
830 return -ENOMEM;
831
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200832 skb->priority = HCI_PRIO_MAX;
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 cmd = (void *) __skb_put(skb, sizeof(*cmd));
835 cmd->addr = d->addr;
836 cmd->ctrl = __ctrl(RFCOMM_DISC, 1);
837 cmd->len = __len8(0);
838 cmd->fcs = __fcs2((u8 *) cmd);
839
840 skb_queue_tail(&d->tx_queue, skb);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300841 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return 0;
843}
844
845static int rfcomm_send_dm(struct rfcomm_session *s, u8 dlci)
846{
847 struct rfcomm_cmd cmd;
848
849 BT_DBG("%p dlci %d", s, dlci);
850
851 cmd.addr = __addr(!s->initiator, dlci);
852 cmd.ctrl = __ctrl(RFCOMM_DM, 1);
853 cmd.len = __len8(0);
854 cmd.fcs = __fcs2((u8 *) &cmd);
855
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200856 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type)
860{
861 struct rfcomm_hdr *hdr;
862 struct rfcomm_mcc *mcc;
863 u8 buf[16], *ptr = buf;
864
865 BT_DBG("%p cr %d type %d", s, cr, type);
866
867 hdr = (void *) ptr; ptr += sizeof(*hdr);
868 hdr->addr = __addr(s->initiator, 0);
869 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
870 hdr->len = __len8(sizeof(*mcc) + 1);
871
872 mcc = (void *) ptr; ptr += sizeof(*mcc);
873 mcc->type = __mcc_type(cr, RFCOMM_NSC);
874 mcc->len = __len8(1);
875
876 /* Type that we didn't like */
877 *ptr = __mcc_type(cr, type); ptr++;
878
879 *ptr = __fcs(buf); ptr++;
880
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200881 return rfcomm_send_frame(s, buf, ptr - buf, HCI_PRIO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d)
885{
886 struct rfcomm_hdr *hdr;
887 struct rfcomm_mcc *mcc;
888 struct rfcomm_pn *pn;
889 u8 buf[16], *ptr = buf;
890
891 BT_DBG("%p cr %d dlci %d mtu %d", s, cr, d->dlci, d->mtu);
892
893 hdr = (void *) ptr; ptr += sizeof(*hdr);
894 hdr->addr = __addr(s->initiator, 0);
895 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
896 hdr->len = __len8(sizeof(*mcc) + sizeof(*pn));
897
898 mcc = (void *) ptr; ptr += sizeof(*mcc);
899 mcc->type = __mcc_type(cr, RFCOMM_PN);
900 mcc->len = __len8(sizeof(*pn));
901
902 pn = (void *) ptr; ptr += sizeof(*pn);
903 pn->dlci = d->dlci;
904 pn->priority = d->priority;
905 pn->ack_timer = 0;
906 pn->max_retrans = 0;
907
908 if (s->cfc) {
909 pn->flow_ctrl = cr ? 0xf0 : 0xe0;
910 pn->credits = RFCOMM_DEFAULT_CREDITS;
911 } else {
912 pn->flow_ctrl = 0;
913 pn->credits = 0;
914 }
915
Marcel Holtmann98bcd082006-07-14 11:42:12 +0200916 if (cr && channel_mtu >= 0)
Marcel Holtmannb4324b52009-06-07 18:06:51 +0200917 pn->mtu = cpu_to_le16(channel_mtu);
Marcel Holtmann98bcd082006-07-14 11:42:12 +0200918 else
Marcel Holtmannb4324b52009-06-07 18:06:51 +0200919 pn->mtu = cpu_to_le16(d->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 *ptr = __fcs(buf); ptr++;
922
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200923 return rfcomm_send_frame(s, buf, ptr - buf, HCI_PRIO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
J. Suter3a5e9032005-08-09 20:28:46 -0700926int rfcomm_send_rpn(struct rfcomm_session *s, int cr, u8 dlci,
927 u8 bit_rate, u8 data_bits, u8 stop_bits,
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900928 u8 parity, u8 flow_ctrl_settings,
J. Suter3a5e9032005-08-09 20:28:46 -0700929 u8 xon_char, u8 xoff_char, u16 param_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 struct rfcomm_hdr *hdr;
932 struct rfcomm_mcc *mcc;
933 struct rfcomm_rpn *rpn;
934 u8 buf[16], *ptr = buf;
935
936 BT_DBG("%p cr %d dlci %d bit_r 0x%x data_b 0x%x stop_b 0x%x parity 0x%x"
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900937 " flwc_s 0x%x xon_c 0x%x xoff_c 0x%x p_mask 0x%x",
938 s, cr, dlci, bit_rate, data_bits, stop_bits, parity,
J. Suter3a5e9032005-08-09 20:28:46 -0700939 flow_ctrl_settings, xon_char, xoff_char, param_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 hdr = (void *) ptr; ptr += sizeof(*hdr);
942 hdr->addr = __addr(s->initiator, 0);
943 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
944 hdr->len = __len8(sizeof(*mcc) + sizeof(*rpn));
945
946 mcc = (void *) ptr; ptr += sizeof(*mcc);
947 mcc->type = __mcc_type(cr, RFCOMM_RPN);
948 mcc->len = __len8(sizeof(*rpn));
949
950 rpn = (void *) ptr; ptr += sizeof(*rpn);
951 rpn->dlci = __addr(1, dlci);
952 rpn->bit_rate = bit_rate;
953 rpn->line_settings = __rpn_line_settings(data_bits, stop_bits, parity);
954 rpn->flow_ctrl = flow_ctrl_settings;
955 rpn->xon_char = xon_char;
956 rpn->xoff_char = xoff_char;
Al Viroe8db8c92006-11-08 00:28:44 -0800957 rpn->param_mask = cpu_to_le16(param_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 *ptr = __fcs(buf); ptr++;
960
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200961 return rfcomm_send_frame(s, buf, ptr - buf, HCI_PRIO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964static int rfcomm_send_rls(struct rfcomm_session *s, int cr, u8 dlci, u8 status)
965{
966 struct rfcomm_hdr *hdr;
967 struct rfcomm_mcc *mcc;
968 struct rfcomm_rls *rls;
969 u8 buf[16], *ptr = buf;
970
971 BT_DBG("%p cr %d status 0x%x", s, cr, status);
972
973 hdr = (void *) ptr; ptr += sizeof(*hdr);
974 hdr->addr = __addr(s->initiator, 0);
975 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
976 hdr->len = __len8(sizeof(*mcc) + sizeof(*rls));
977
978 mcc = (void *) ptr; ptr += sizeof(*mcc);
979 mcc->type = __mcc_type(cr, RFCOMM_RLS);
980 mcc->len = __len8(sizeof(*rls));
981
982 rls = (void *) ptr; ptr += sizeof(*rls);
983 rls->dlci = __addr(1, dlci);
984 rls->status = status;
985
986 *ptr = __fcs(buf); ptr++;
987
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200988 return rfcomm_send_frame(s, buf, ptr - buf, HCI_PRIO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig)
992{
993 struct rfcomm_hdr *hdr;
994 struct rfcomm_mcc *mcc;
995 struct rfcomm_msc *msc;
996 u8 buf[16], *ptr = buf;
997
998 BT_DBG("%p cr %d v24 0x%x", s, cr, v24_sig);
999
1000 hdr = (void *) ptr; ptr += sizeof(*hdr);
1001 hdr->addr = __addr(s->initiator, 0);
1002 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1003 hdr->len = __len8(sizeof(*mcc) + sizeof(*msc));
1004
1005 mcc = (void *) ptr; ptr += sizeof(*mcc);
1006 mcc->type = __mcc_type(cr, RFCOMM_MSC);
1007 mcc->len = __len8(sizeof(*msc));
1008
1009 msc = (void *) ptr; ptr += sizeof(*msc);
1010 msc->dlci = __addr(1, dlci);
1011 msc->v24_sig = v24_sig | 0x01;
1012
1013 *ptr = __fcs(buf); ptr++;
1014
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +02001015 return rfcomm_send_frame(s, buf, ptr - buf, HCI_PRIO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018static int rfcomm_send_fcoff(struct rfcomm_session *s, int cr)
1019{
1020 struct rfcomm_hdr *hdr;
1021 struct rfcomm_mcc *mcc;
1022 u8 buf[16], *ptr = buf;
1023
1024 BT_DBG("%p cr %d", s, cr);
1025
1026 hdr = (void *) ptr; ptr += sizeof(*hdr);
1027 hdr->addr = __addr(s->initiator, 0);
1028 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1029 hdr->len = __len8(sizeof(*mcc));
1030
1031 mcc = (void *) ptr; ptr += sizeof(*mcc);
1032 mcc->type = __mcc_type(cr, RFCOMM_FCOFF);
1033 mcc->len = __len8(0);
1034
1035 *ptr = __fcs(buf); ptr++;
1036
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +02001037 return rfcomm_send_frame(s, buf, ptr - buf, HCI_PRIO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040static int rfcomm_send_fcon(struct rfcomm_session *s, int cr)
1041{
1042 struct rfcomm_hdr *hdr;
1043 struct rfcomm_mcc *mcc;
1044 u8 buf[16], *ptr = buf;
1045
1046 BT_DBG("%p cr %d", s, cr);
1047
1048 hdr = (void *) ptr; ptr += sizeof(*hdr);
1049 hdr->addr = __addr(s->initiator, 0);
1050 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1051 hdr->len = __len8(sizeof(*mcc));
1052
1053 mcc = (void *) ptr; ptr += sizeof(*mcc);
1054 mcc->type = __mcc_type(cr, RFCOMM_FCON);
1055 mcc->len = __len8(0);
1056
1057 *ptr = __fcs(buf); ptr++;
1058
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +02001059 return rfcomm_send_frame(s, buf, ptr - buf, HCI_PRIO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len)
1063{
1064 struct socket *sock = s->sock;
1065 struct kvec iv[3];
1066 struct msghdr msg;
1067 unsigned char hdr[5], crc[1];
1068
1069 if (len > 125)
1070 return -EINVAL;
1071
1072 BT_DBG("%p cr %d", s, cr);
1073
1074 hdr[0] = __addr(s->initiator, 0);
1075 hdr[1] = __ctrl(RFCOMM_UIH, 0);
1076 hdr[2] = 0x01 | ((len + 2) << 1);
1077 hdr[3] = 0x01 | ((cr & 0x01) << 1) | (RFCOMM_TEST << 2);
1078 hdr[4] = 0x01 | (len << 1);
1079
1080 crc[0] = __fcs(hdr);
1081
1082 iv[0].iov_base = hdr;
1083 iv[0].iov_len = 5;
1084 iv[1].iov_base = pattern;
1085 iv[1].iov_len = len;
1086 iv[2].iov_base = crc;
1087 iv[2].iov_len = 1;
1088
1089 memset(&msg, 0, sizeof(msg));
1090
1091 return kernel_sendmsg(sock, &msg, iv, 3, 6 + len);
1092}
1093
1094static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits)
1095{
1096 struct rfcomm_hdr *hdr;
1097 u8 buf[16], *ptr = buf;
1098
1099 BT_DBG("%p addr %d credits %d", s, addr, credits);
1100
1101 hdr = (void *) ptr; ptr += sizeof(*hdr);
1102 hdr->addr = addr;
1103 hdr->ctrl = __ctrl(RFCOMM_UIH, 1);
1104 hdr->len = __len8(0);
1105
1106 *ptr = credits; ptr++;
1107
1108 *ptr = __fcs(buf); ptr++;
1109
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +02001110 return rfcomm_send_frame(s, buf, ptr - buf, HCI_PRIO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113static void rfcomm_make_uih(struct sk_buff *skb, u8 addr)
1114{
1115 struct rfcomm_hdr *hdr;
1116 int len = skb->len;
1117 u8 *crc;
1118
1119 if (len > 127) {
1120 hdr = (void *) skb_push(skb, 4);
Marcel Holtmannb4324b52009-06-07 18:06:51 +02001121 put_unaligned(cpu_to_le16(__len16(len)), (__le16 *) &hdr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 } else {
1123 hdr = (void *) skb_push(skb, 3);
1124 hdr->len = __len8(len);
1125 }
1126 hdr->addr = addr;
1127 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1128
1129 crc = skb_put(skb, 1);
1130 *crc = __fcs((void *) hdr);
1131}
1132
1133/* ---- RFCOMM frame reception ---- */
1134static int rfcomm_recv_ua(struct rfcomm_session *s, u8 dlci)
1135{
1136 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1137
1138 if (dlci) {
1139 /* Data channel */
1140 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1141 if (!d) {
1142 rfcomm_send_dm(s, dlci);
1143 return 0;
1144 }
1145
1146 switch (d->state) {
1147 case BT_CONNECT:
1148 rfcomm_dlc_clear_timer(d);
1149
1150 rfcomm_dlc_lock(d);
1151 d->state = BT_CONNECTED;
1152 d->state_change(d, 0);
1153 rfcomm_dlc_unlock(d);
1154
1155 rfcomm_send_msc(s, 1, dlci, d->v24_sig);
1156 break;
1157
1158 case BT_DISCONN:
1159 d->state = BT_CLOSED;
1160 __rfcomm_dlc_close(d, 0);
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001161
1162 if (list_empty(&s->dlcs)) {
1163 s->state = BT_DISCONN;
1164 rfcomm_send_disc(s, 0);
1165 }
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168 }
1169 } else {
1170 /* Control channel */
1171 switch (s->state) {
1172 case BT_CONNECT:
1173 s->state = BT_CONNECTED;
1174 rfcomm_process_connect(s);
1175 break;
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001176
1177 case BT_DISCONN:
Nick Pelly6c2718d2010-02-03 16:18:36 -08001178 /* When socket is closed and we are not RFCOMM
1179 * initiator rfcomm_process_rx already calls
1180 * rfcomm_session_put() */
1181 if (s->sock->sk->sk_state != BT_CLOSED)
Lukáš Turek683d9492011-01-05 02:43:59 +01001182 if (list_empty(&s->dlcs))
1183 rfcomm_session_put(s);
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001184 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 }
1187 return 0;
1188}
1189
1190static int rfcomm_recv_dm(struct rfcomm_session *s, u8 dlci)
1191{
1192 int err = 0;
1193
1194 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1195
1196 if (dlci) {
1197 /* Data DLC */
1198 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1199 if (d) {
1200 if (d->state == BT_CONNECT || d->state == BT_CONFIG)
1201 err = ECONNREFUSED;
1202 else
1203 err = ECONNRESET;
1204
1205 d->state = BT_CLOSED;
1206 __rfcomm_dlc_close(d, err);
1207 }
1208 } else {
1209 if (s->state == BT_CONNECT)
1210 err = ECONNREFUSED;
1211 else
1212 err = ECONNRESET;
1213
1214 s->state = BT_CLOSED;
1215 rfcomm_session_close(s, err);
1216 }
1217 return 0;
1218}
1219
1220static int rfcomm_recv_disc(struct rfcomm_session *s, u8 dlci)
1221{
1222 int err = 0;
1223
1224 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1225
1226 if (dlci) {
1227 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1228 if (d) {
1229 rfcomm_send_ua(s, dlci);
1230
1231 if (d->state == BT_CONNECT || d->state == BT_CONFIG)
1232 err = ECONNREFUSED;
1233 else
1234 err = ECONNRESET;
1235
1236 d->state = BT_CLOSED;
1237 __rfcomm_dlc_close(d, err);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001238 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 rfcomm_send_dm(s, dlci);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 } else {
1242 rfcomm_send_ua(s, 0);
1243
1244 if (s->state == BT_CONNECT)
1245 err = ECONNREFUSED;
1246 else
1247 err = ECONNRESET;
1248
1249 s->state = BT_CLOSED;
1250 rfcomm_session_close(s, err);
1251 }
1252
1253 return 0;
1254}
1255
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001256void rfcomm_dlc_accept(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Marcel Holtmann300b9392006-07-03 10:37:55 +02001258 struct sock *sk = d->session->sock->sk;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001259 struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
Marcel Holtmann300b9392006-07-03 10:37:55 +02001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 BT_DBG("dlc %p", d);
1262
1263 rfcomm_send_ua(d->session, d->dlci);
1264
Johan Hedberge2139b32009-03-26 16:41:56 +02001265 rfcomm_dlc_clear_timer(d);
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 rfcomm_dlc_lock(d);
1268 d->state = BT_CONNECTED;
1269 d->state_change(d, 0);
1270 rfcomm_dlc_unlock(d);
1271
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001272 if (d->role_switch)
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001273 hci_conn_switch_role(conn->hcon, 0x00);
Marcel Holtmann300b9392006-07-03 10:37:55 +02001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig);
1276}
1277
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001278static void rfcomm_check_accept(struct rfcomm_dlc *d)
1279{
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001280 if (rfcomm_check_security(d)) {
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001281 if (d->defer_setup) {
1282 set_bit(RFCOMM_DEFER_SETUP, &d->flags);
1283 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8bf47942009-02-16 02:59:49 +01001284
1285 rfcomm_dlc_lock(d);
1286 d->state = BT_CONNECT2;
1287 d->state_change(d, 0);
1288 rfcomm_dlc_unlock(d);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001289 } else
1290 rfcomm_dlc_accept(d);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001291 } else {
1292 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
1293 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001294 }
1295}
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297static int rfcomm_recv_sabm(struct rfcomm_session *s, u8 dlci)
1298{
1299 struct rfcomm_dlc *d;
1300 u8 channel;
1301
1302 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1303
1304 if (!dlci) {
1305 rfcomm_send_ua(s, 0);
1306
1307 if (s->state == BT_OPEN) {
1308 s->state = BT_CONNECTED;
1309 rfcomm_process_connect(s);
1310 }
1311 return 0;
1312 }
1313
1314 /* Check if DLC exists */
1315 d = rfcomm_dlc_get(s, dlci);
1316 if (d) {
1317 if (d->state == BT_OPEN) {
1318 /* DLC was previously opened by PN request */
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001319 rfcomm_check_accept(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321 return 0;
1322 }
1323
1324 /* Notify socket layer about incoming connection */
1325 channel = __srv_channel(dlci);
1326 if (rfcomm_connect_ind(s, channel, &d)) {
1327 d->dlci = dlci;
1328 d->addr = __addr(s->initiator, dlci);
1329 rfcomm_dlc_link(s, d);
1330
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001331 rfcomm_check_accept(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 } else {
1333 rfcomm_send_dm(s, dlci);
1334 }
1335
1336 return 0;
1337}
1338
1339static int rfcomm_apply_pn(struct rfcomm_dlc *d, int cr, struct rfcomm_pn *pn)
1340{
1341 struct rfcomm_session *s = d->session;
1342
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001343 BT_DBG("dlc %p state %ld dlci %d mtu %d fc 0x%x credits %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 d, d->state, d->dlci, pn->mtu, pn->flow_ctrl, pn->credits);
1345
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001346 if ((pn->flow_ctrl == 0xf0 && s->cfc != RFCOMM_CFC_DISABLED) ||
1347 pn->flow_ctrl == 0xe0) {
1348 d->cfc = RFCOMM_CFC_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 d->tx_credits = pn->credits;
1350 } else {
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001351 d->cfc = RFCOMM_CFC_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1353 }
1354
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001355 if (s->cfc == RFCOMM_CFC_UNKNOWN)
1356 s->cfc = d->cfc;
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 d->priority = pn->priority;
1359
Marcel Holtmannb4324b52009-06-07 18:06:51 +02001360 d->mtu = __le16_to_cpu(pn->mtu);
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001361
1362 if (cr && d->mtu > s->mtu)
1363 d->mtu = s->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 return 0;
1366}
1367
1368static int rfcomm_recv_pn(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1369{
1370 struct rfcomm_pn *pn = (void *) skb->data;
1371 struct rfcomm_dlc *d;
1372 u8 dlci = pn->dlci;
1373
1374 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1375
1376 if (!dlci)
1377 return 0;
1378
1379 d = rfcomm_dlc_get(s, dlci);
1380 if (d) {
1381 if (cr) {
1382 /* PN request */
1383 rfcomm_apply_pn(d, cr, pn);
1384 rfcomm_send_pn(s, 0, d);
1385 } else {
1386 /* PN response */
1387 switch (d->state) {
1388 case BT_CONFIG:
1389 rfcomm_apply_pn(d, cr, pn);
1390
1391 d->state = BT_CONNECT;
1392 rfcomm_send_sabm(s, d->dlci);
1393 break;
1394 }
1395 }
1396 } else {
1397 u8 channel = __srv_channel(dlci);
1398
1399 if (!cr)
1400 return 0;
1401
1402 /* PN request for non existing DLC.
1403 * Assume incoming connection. */
1404 if (rfcomm_connect_ind(s, channel, &d)) {
1405 d->dlci = dlci;
1406 d->addr = __addr(s->initiator, dlci);
1407 rfcomm_dlc_link(s, d);
1408
1409 rfcomm_apply_pn(d, cr, pn);
1410
1411 d->state = BT_OPEN;
1412 rfcomm_send_pn(s, 0, d);
1413 } else {
1414 rfcomm_send_dm(s, dlci);
1415 }
1416 }
1417 return 0;
1418}
1419
1420static int rfcomm_recv_rpn(struct rfcomm_session *s, int cr, int len, struct sk_buff *skb)
1421{
1422 struct rfcomm_rpn *rpn = (void *) skb->data;
1423 u8 dlci = __get_dlci(rpn->dlci);
1424
1425 u8 bit_rate = 0;
1426 u8 data_bits = 0;
1427 u8 stop_bits = 0;
1428 u8 parity = 0;
1429 u8 flow_ctrl = 0;
1430 u8 xon_char = 0;
1431 u8 xoff_char = 0;
1432 u16 rpn_mask = RFCOMM_RPN_PM_ALL;
J. Suter3a5e9032005-08-09 20:28:46 -07001433
1434 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",
1435 dlci, cr, len, rpn->bit_rate, rpn->line_settings, rpn->flow_ctrl,
1436 rpn->xon_char, rpn->xoff_char, rpn->param_mask);
1437
1438 if (!cr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return 0;
J. Suter3a5e9032005-08-09 20:28:46 -07001440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (len == 1) {
Yuri Kululin08601462010-07-23 13:57:12 +04001442 /* This is a request, return default (according to ETSI TS 07.10) settings */
1443 bit_rate = RFCOMM_RPN_BR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 data_bits = RFCOMM_RPN_DATA_8;
1445 stop_bits = RFCOMM_RPN_STOP_1;
1446 parity = RFCOMM_RPN_PARITY_NONE;
1447 flow_ctrl = RFCOMM_RPN_FLOW_NONE;
1448 xon_char = RFCOMM_RPN_XON_CHAR;
1449 xoff_char = RFCOMM_RPN_XOFF_CHAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 goto rpn_out;
1451 }
J. Suter3a5e9032005-08-09 20:28:46 -07001452
1453 /* Check for sane values, ignore/accept bit_rate, 8 bits, 1 stop bit,
1454 * no parity, no flow control lines, normal XON/XOFF chars */
1455
Al Viroe8db8c92006-11-08 00:28:44 -08001456 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_BITRATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 bit_rate = rpn->bit_rate;
Yuri Kululin08601462010-07-23 13:57:12 +04001458 if (bit_rate > RFCOMM_RPN_BR_230400) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 BT_DBG("RPN bit rate mismatch 0x%x", bit_rate);
Yuri Kululin08601462010-07-23 13:57:12 +04001460 bit_rate = RFCOMM_RPN_BR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 rpn_mask ^= RFCOMM_RPN_PM_BITRATE;
1462 }
1463 }
J. Suter3a5e9032005-08-09 20:28:46 -07001464
Al Viroe8db8c92006-11-08 00:28:44 -08001465 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 data_bits = __get_rpn_data_bits(rpn->line_settings);
1467 if (data_bits != RFCOMM_RPN_DATA_8) {
1468 BT_DBG("RPN data bits mismatch 0x%x", data_bits);
1469 data_bits = RFCOMM_RPN_DATA_8;
1470 rpn_mask ^= RFCOMM_RPN_PM_DATA;
1471 }
1472 }
J. Suter3a5e9032005-08-09 20:28:46 -07001473
Al Viroe8db8c92006-11-08 00:28:44 -08001474 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_STOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 stop_bits = __get_rpn_stop_bits(rpn->line_settings);
1476 if (stop_bits != RFCOMM_RPN_STOP_1) {
1477 BT_DBG("RPN stop bits mismatch 0x%x", stop_bits);
1478 stop_bits = RFCOMM_RPN_STOP_1;
1479 rpn_mask ^= RFCOMM_RPN_PM_STOP;
1480 }
1481 }
J. Suter3a5e9032005-08-09 20:28:46 -07001482
Al Viroe8db8c92006-11-08 00:28:44 -08001483 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_PARITY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 parity = __get_rpn_parity(rpn->line_settings);
1485 if (parity != RFCOMM_RPN_PARITY_NONE) {
1486 BT_DBG("RPN parity mismatch 0x%x", parity);
1487 parity = RFCOMM_RPN_PARITY_NONE;
1488 rpn_mask ^= RFCOMM_RPN_PM_PARITY;
1489 }
1490 }
J. Suter3a5e9032005-08-09 20:28:46 -07001491
Al Viroe8db8c92006-11-08 00:28:44 -08001492 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_FLOW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 flow_ctrl = rpn->flow_ctrl;
1494 if (flow_ctrl != RFCOMM_RPN_FLOW_NONE) {
1495 BT_DBG("RPN flow ctrl mismatch 0x%x", flow_ctrl);
1496 flow_ctrl = RFCOMM_RPN_FLOW_NONE;
1497 rpn_mask ^= RFCOMM_RPN_PM_FLOW;
1498 }
1499 }
J. Suter3a5e9032005-08-09 20:28:46 -07001500
Al Viroe8db8c92006-11-08 00:28:44 -08001501 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 xon_char = rpn->xon_char;
1503 if (xon_char != RFCOMM_RPN_XON_CHAR) {
1504 BT_DBG("RPN XON char mismatch 0x%x", xon_char);
1505 xon_char = RFCOMM_RPN_XON_CHAR;
1506 rpn_mask ^= RFCOMM_RPN_PM_XON;
1507 }
1508 }
J. Suter3a5e9032005-08-09 20:28:46 -07001509
Al Viroe8db8c92006-11-08 00:28:44 -08001510 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XOFF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 xoff_char = rpn->xoff_char;
1512 if (xoff_char != RFCOMM_RPN_XOFF_CHAR) {
1513 BT_DBG("RPN XOFF char mismatch 0x%x", xoff_char);
1514 xoff_char = RFCOMM_RPN_XOFF_CHAR;
1515 rpn_mask ^= RFCOMM_RPN_PM_XOFF;
1516 }
1517 }
1518
1519rpn_out:
J. Suter3a5e9032005-08-09 20:28:46 -07001520 rfcomm_send_rpn(s, 0, dlci, bit_rate, data_bits, stop_bits,
1521 parity, flow_ctrl, xon_char, xoff_char, rpn_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 return 0;
1524}
1525
1526static int rfcomm_recv_rls(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1527{
1528 struct rfcomm_rls *rls = (void *) skb->data;
1529 u8 dlci = __get_dlci(rls->dlci);
1530
1531 BT_DBG("dlci %d cr %d status 0x%x", dlci, cr, rls->status);
J. Suter3a5e9032005-08-09 20:28:46 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (!cr)
1534 return 0;
1535
J. Suter3a5e9032005-08-09 20:28:46 -07001536 /* We should probably do something with this information here. But
1537 * for now it's sufficient just to reply -- Bluetooth 1.1 says it's
1538 * mandatory to recognise and respond to RLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 rfcomm_send_rls(s, 0, dlci, rls->status);
1541
1542 return 0;
1543}
1544
1545static int rfcomm_recv_msc(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1546{
1547 struct rfcomm_msc *msc = (void *) skb->data;
1548 struct rfcomm_dlc *d;
1549 u8 dlci = __get_dlci(msc->dlci);
1550
1551 BT_DBG("dlci %d cr %d v24 0x%x", dlci, cr, msc->v24_sig);
1552
1553 d = rfcomm_dlc_get(s, dlci);
J. Suter3a5e9032005-08-09 20:28:46 -07001554 if (!d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return 0;
1556
1557 if (cr) {
1558 if (msc->v24_sig & RFCOMM_V24_FC && !d->cfc)
1559 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1560 else
1561 clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
J. Suter3a5e9032005-08-09 20:28:46 -07001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 rfcomm_dlc_lock(d);
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +02001564
1565 d->remote_v24_sig = msc->v24_sig;
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (d->modem_status)
1568 d->modem_status(d, msc->v24_sig);
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +02001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 rfcomm_dlc_unlock(d);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 rfcomm_send_msc(s, 0, dlci, msc->v24_sig);
1573
1574 d->mscex |= RFCOMM_MSCEX_RX;
J. Suter3a5e9032005-08-09 20:28:46 -07001575 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 d->mscex |= RFCOMM_MSCEX_TX;
1577
1578 return 0;
1579}
1580
1581static int rfcomm_recv_mcc(struct rfcomm_session *s, struct sk_buff *skb)
1582{
1583 struct rfcomm_mcc *mcc = (void *) skb->data;
1584 u8 type, cr, len;
1585
1586 cr = __test_cr(mcc->type);
1587 type = __get_mcc_type(mcc->type);
1588 len = __get_mcc_len(mcc->len);
1589
1590 BT_DBG("%p type 0x%x cr %d", s, type, cr);
1591
1592 skb_pull(skb, 2);
1593
1594 switch (type) {
1595 case RFCOMM_PN:
1596 rfcomm_recv_pn(s, cr, skb);
1597 break;
1598
1599 case RFCOMM_RPN:
1600 rfcomm_recv_rpn(s, cr, len, skb);
1601 break;
1602
1603 case RFCOMM_RLS:
1604 rfcomm_recv_rls(s, cr, skb);
1605 break;
1606
1607 case RFCOMM_MSC:
1608 rfcomm_recv_msc(s, cr, skb);
1609 break;
1610
1611 case RFCOMM_FCOFF:
1612 if (cr) {
1613 set_bit(RFCOMM_TX_THROTTLED, &s->flags);
1614 rfcomm_send_fcoff(s, 0);
1615 }
1616 break;
1617
1618 case RFCOMM_FCON:
1619 if (cr) {
1620 clear_bit(RFCOMM_TX_THROTTLED, &s->flags);
1621 rfcomm_send_fcon(s, 0);
1622 }
1623 break;
1624
1625 case RFCOMM_TEST:
1626 if (cr)
1627 rfcomm_send_test(s, 0, skb->data, skb->len);
1628 break;
1629
1630 case RFCOMM_NSC:
1631 break;
1632
1633 default:
1634 BT_ERR("Unknown control type 0x%02x", type);
1635 rfcomm_send_nsc(s, cr, type);
1636 break;
1637 }
1638 return 0;
1639}
1640
1641static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk_buff *skb)
1642{
1643 struct rfcomm_dlc *d;
1644
1645 BT_DBG("session %p state %ld dlci %d pf %d", s, s->state, dlci, pf);
1646
1647 d = rfcomm_dlc_get(s, dlci);
1648 if (!d) {
1649 rfcomm_send_dm(s, dlci);
1650 goto drop;
1651 }
1652
1653 if (pf && d->cfc) {
1654 u8 credits = *(u8 *) skb->data; skb_pull(skb, 1);
1655
1656 d->tx_credits += credits;
1657 if (d->tx_credits)
1658 clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
1659 }
1660
1661 if (skb->len && d->state == BT_CONNECTED) {
1662 rfcomm_dlc_lock(d);
1663 d->rx_credits--;
1664 d->data_ready(d, skb);
1665 rfcomm_dlc_unlock(d);
1666 return 0;
1667 }
1668
1669drop:
1670 kfree_skb(skb);
1671 return 0;
1672}
1673
1674static int rfcomm_recv_frame(struct rfcomm_session *s, struct sk_buff *skb)
1675{
1676 struct rfcomm_hdr *hdr = (void *) skb->data;
1677 u8 type, dlci, fcs;
1678
1679 dlci = __get_dlci(hdr->addr);
1680 type = __get_type(hdr->ctrl);
1681
1682 /* Trim FCS */
1683 skb->len--; skb->tail--;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001684 fcs = *(u8 *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 if (__check_fcs(skb->data, type, fcs)) {
1687 BT_ERR("bad checksum in packet");
1688 kfree_skb(skb);
1689 return -EILSEQ;
1690 }
1691
1692 if (__test_ea(hdr->len))
1693 skb_pull(skb, 3);
1694 else
1695 skb_pull(skb, 4);
1696
1697 switch (type) {
1698 case RFCOMM_SABM:
1699 if (__test_pf(hdr->ctrl))
1700 rfcomm_recv_sabm(s, dlci);
1701 break;
1702
1703 case RFCOMM_DISC:
1704 if (__test_pf(hdr->ctrl))
1705 rfcomm_recv_disc(s, dlci);
1706 break;
1707
1708 case RFCOMM_UA:
1709 if (__test_pf(hdr->ctrl))
1710 rfcomm_recv_ua(s, dlci);
1711 break;
1712
1713 case RFCOMM_DM:
1714 rfcomm_recv_dm(s, dlci);
1715 break;
1716
1717 case RFCOMM_UIH:
1718 if (dlci)
1719 return rfcomm_recv_data(s, dlci, __test_pf(hdr->ctrl), skb);
1720
1721 rfcomm_recv_mcc(s, skb);
1722 break;
1723
1724 default:
Andrei Emeltchenko5017d8d2010-09-08 16:26:53 +03001725 BT_ERR("Unknown packet type 0x%02x", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 break;
1727 }
1728 kfree_skb(skb);
1729 return 0;
1730}
1731
1732/* ---- Connection and data processing ---- */
1733
1734static void rfcomm_process_connect(struct rfcomm_session *s)
1735{
1736 struct rfcomm_dlc *d;
1737 struct list_head *p, *n;
1738
1739 BT_DBG("session %p state %ld", s, s->state);
1740
1741 list_for_each_safe(p, n, &s->dlcs) {
1742 d = list_entry(p, struct rfcomm_dlc, list);
1743 if (d->state == BT_CONFIG) {
1744 d->mtu = s->mtu;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001745 if (rfcomm_check_security(d)) {
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001746 rfcomm_send_pn(s, 1, d);
1747 } else {
Marcel Holtmann77db1982008-07-14 20:13:45 +02001748 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
1749 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752 }
1753}
1754
1755/* Send data queued for the DLC.
1756 * Return number of frames left in the queue.
1757 */
1758static inline int rfcomm_process_tx(struct rfcomm_dlc *d)
1759{
1760 struct sk_buff *skb;
1761 int err;
1762
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001763 BT_DBG("dlc %p state %ld cfc %d rx_credits %d tx_credits %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 d, d->state, d->cfc, d->rx_credits, d->tx_credits);
1765
1766 /* Send pending MSC */
1767 if (test_and_clear_bit(RFCOMM_MSC_PENDING, &d->flags))
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001768 rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 if (d->cfc) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001771 /* CFC enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 * Give them some credits */
1773 if (!test_bit(RFCOMM_RX_THROTTLED, &d->flags) &&
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001774 d->rx_credits <= (d->cfc >> 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 rfcomm_send_credits(d->session, d->addr, d->cfc - d->rx_credits);
1776 d->rx_credits = d->cfc;
1777 }
1778 } else {
1779 /* CFC disabled.
1780 * Give ourselves some credits */
1781 d->tx_credits = 5;
1782 }
1783
1784 if (test_bit(RFCOMM_TX_THROTTLED, &d->flags))
1785 return skb_queue_len(&d->tx_queue);
1786
1787 while (d->tx_credits && (skb = skb_dequeue(&d->tx_queue))) {
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +02001788 err = rfcomm_send_frame(d->session, skb->data, skb->len,
1789 skb->priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (err < 0) {
1791 skb_queue_head(&d->tx_queue, skb);
1792 break;
1793 }
1794 kfree_skb(skb);
1795 d->tx_credits--;
1796 }
1797
1798 if (d->cfc && !d->tx_credits) {
1799 /* We're out of TX credits.
1800 * Set TX_THROTTLED flag to avoid unnesary wakeups by dlc_send. */
1801 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1802 }
1803
1804 return skb_queue_len(&d->tx_queue);
1805}
1806
1807static inline void rfcomm_process_dlcs(struct rfcomm_session *s)
1808{
1809 struct rfcomm_dlc *d;
1810 struct list_head *p, *n;
1811
1812 BT_DBG("session %p state %ld", s, s->state);
1813
1814 list_for_each_safe(p, n, &s->dlcs) {
1815 d = list_entry(p, struct rfcomm_dlc, list);
1816
1817 if (test_bit(RFCOMM_TIMED_OUT, &d->flags)) {
1818 __rfcomm_dlc_close(d, ETIMEDOUT);
1819 continue;
1820 }
1821
Szymon Jancdb544672011-09-26 14:19:47 +02001822 if (test_bit(RFCOMM_ENC_DROP, &d->flags)) {
1823 __rfcomm_dlc_close(d, ECONNREFUSED);
1824 continue;
1825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (test_and_clear_bit(RFCOMM_AUTH_ACCEPT, &d->flags)) {
1828 rfcomm_dlc_clear_timer(d);
Marcel Holtmann77db1982008-07-14 20:13:45 +02001829 if (d->out) {
1830 rfcomm_send_pn(s, 1, d);
1831 rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001832 } else {
1833 if (d->defer_setup) {
1834 set_bit(RFCOMM_DEFER_SETUP, &d->flags);
1835 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8bf47942009-02-16 02:59:49 +01001836
1837 rfcomm_dlc_lock(d);
1838 d->state = BT_CONNECT2;
1839 d->state_change(d, 0);
1840 rfcomm_dlc_unlock(d);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001841 } else
1842 rfcomm_dlc_accept(d);
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 continue;
1845 } else if (test_and_clear_bit(RFCOMM_AUTH_REJECT, &d->flags)) {
1846 rfcomm_dlc_clear_timer(d);
Marcel Holtmann77db1982008-07-14 20:13:45 +02001847 if (!d->out)
1848 rfcomm_send_dm(s, d->dlci);
1849 else
1850 d->state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 __rfcomm_dlc_close(d, ECONNREFUSED);
1852 continue;
1853 }
1854
Jaikumar Ganesh6e1031a2009-02-02 18:03:57 -08001855 if (test_bit(RFCOMM_SEC_PENDING, &d->flags))
1856 continue;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (test_bit(RFCOMM_TX_THROTTLED, &s->flags))
1859 continue;
1860
1861 if ((d->state == BT_CONNECTED || d->state == BT_DISCONN) &&
Marcel Holtmann77db1982008-07-14 20:13:45 +02001862 d->mscex == RFCOMM_MSCEX_OK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 rfcomm_process_tx(d);
1864 }
1865}
1866
1867static inline void rfcomm_process_rx(struct rfcomm_session *s)
1868{
1869 struct socket *sock = s->sock;
1870 struct sock *sk = sock->sk;
1871 struct sk_buff *skb;
1872
1873 BT_DBG("session %p state %ld qlen %d", s, s->state, skb_queue_len(&sk->sk_receive_queue));
1874
1875 /* Get data directly from socket receive queue without copying it. */
1876 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
1877 skb_orphan(skb);
Mat Martineau44935722011-07-22 14:53:58 -07001878 if (!skb_linearize(skb))
1879 rfcomm_recv_frame(s, skb);
1880 else
1881 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
1884 if (sk->sk_state == BT_CLOSED) {
1885 if (!s->initiator)
1886 rfcomm_session_put(s);
1887
1888 rfcomm_session_close(s, sk->sk_err);
1889 }
1890}
1891
1892static inline void rfcomm_accept_connection(struct rfcomm_session *s)
1893{
1894 struct socket *sock = s->sock, *nsock;
1895 int err;
1896
1897 /* Fast check for a new connection.
1898 * Avoids unnesesary socket allocations. */
1899 if (list_empty(&bt_sk(sock->sk)->accept_q))
1900 return;
1901
1902 BT_DBG("session %p", s);
1903
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02001904 err = kernel_accept(sock, &nsock, O_NONBLOCK);
1905 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return;
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /* Set our callbacks */
1909 nsock->sk->sk_data_ready = rfcomm_l2data_ready;
1910 nsock->sk->sk_state_change = rfcomm_l2state_change;
1911
1912 s = rfcomm_session_add(nsock, BT_OPEN);
1913 if (s) {
1914 rfcomm_session_hold(s);
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001915
1916 /* We should adjust MTU on incoming sessions.
1917 * L2CAP MTU minus UIH header and FCS. */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001918 s->mtu = min(l2cap_pi(nsock->sk)->chan->omtu,
1919 l2cap_pi(nsock->sk)->chan->imtu) - 5;
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001920
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +03001921 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 } else
1923 sock_release(nsock);
1924}
1925
1926static inline void rfcomm_check_connection(struct rfcomm_session *s)
1927{
1928 struct sock *sk = s->sock->sk;
1929
1930 BT_DBG("%p state %ld", s, s->state);
1931
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +02001932 switch (sk->sk_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 case BT_CONNECTED:
1934 s->state = BT_CONNECT;
1935
1936 /* We can adjust MTU on outgoing sessions.
1937 * L2CAP MTU minus UIH header and FCS. */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001938 s->mtu = min(l2cap_pi(sk)->chan->omtu, l2cap_pi(sk)->chan->imtu) - 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 rfcomm_send_sabm(s, 0);
1941 break;
1942
1943 case BT_CLOSED:
1944 s->state = BT_CLOSED;
1945 rfcomm_session_close(s, sk->sk_err);
1946 break;
1947 }
1948}
1949
1950static inline void rfcomm_process_sessions(void)
1951{
1952 struct list_head *p, *n;
1953
1954 rfcomm_lock();
1955
1956 list_for_each_safe(p, n, &session_list) {
1957 struct rfcomm_session *s;
1958 s = list_entry(p, struct rfcomm_session, list);
1959
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -03001960 if (test_and_clear_bit(RFCOMM_TIMED_OUT, &s->flags)) {
1961 s->state = BT_DISCONN;
1962 rfcomm_send_disc(s, 0);
Marcel Holtmann485f1ef2010-02-03 15:52:18 -08001963 rfcomm_session_put(s);
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -03001964 continue;
1965 }
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (s->state == BT_LISTEN) {
1968 rfcomm_accept_connection(s);
1969 continue;
1970 }
1971
1972 rfcomm_session_hold(s);
1973
1974 switch (s->state) {
1975 case BT_BOUND:
1976 rfcomm_check_connection(s);
1977 break;
1978
1979 default:
1980 rfcomm_process_rx(s);
1981 break;
1982 }
1983
1984 rfcomm_process_dlcs(s);
1985
1986 rfcomm_session_put(s);
1987 }
1988
1989 rfcomm_unlock();
1990}
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992static int rfcomm_add_listener(bdaddr_t *ba)
1993{
1994 struct sockaddr_l2 addr;
1995 struct socket *sock;
1996 struct sock *sk;
1997 struct rfcomm_session *s;
1998 int err = 0;
1999
2000 /* Create socket */
2001 err = rfcomm_l2sock_create(&sock);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002002 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 BT_ERR("Create socket failed %d", err);
2004 return err;
2005 }
2006
2007 /* Bind socket */
2008 bacpy(&addr.l2_bdaddr, ba);
2009 addr.l2_family = AF_BLUETOOTH;
Marcel Holtmannb4324b52009-06-07 18:06:51 +02002010 addr.l2_psm = cpu_to_le16(RFCOMM_PSM);
Marcel Holtmann37e62f52009-02-17 21:49:33 +01002011 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02002012 err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (err < 0) {
2014 BT_ERR("Bind failed %d", err);
2015 goto failed;
2016 }
2017
2018 /* Set L2CAP options */
2019 sk = sock->sk;
2020 lock_sock(sk);
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03002021 l2cap_pi(sk)->chan->imtu = l2cap_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 release_sock(sk);
2023
2024 /* Start listening on the socket */
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02002025 err = kernel_listen(sock, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (err) {
2027 BT_ERR("Listen failed %d", err);
2028 goto failed;
2029 }
2030
2031 /* Add listening session */
2032 s = rfcomm_session_add(sock, BT_LISTEN);
2033 if (!s)
2034 goto failed;
2035
2036 rfcomm_session_hold(s);
2037 return 0;
2038failed:
2039 sock_release(sock);
2040 return err;
2041}
2042
2043static void rfcomm_kill_listener(void)
2044{
2045 struct rfcomm_session *s;
2046 struct list_head *p, *n;
2047
2048 BT_DBG("");
2049
2050 list_for_each_safe(p, n, &session_list) {
2051 s = list_entry(p, struct rfcomm_session, list);
2052 rfcomm_session_del(s);
2053 }
2054}
2055
2056static int rfcomm_run(void *unused)
2057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 BT_DBG("");
2059
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002060 set_user_nice(current, -10);
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 rfcomm_add_listener(BDADDR_ANY);
2063
Peter Hurleye5842cd2011-07-24 00:10:35 -04002064 while (1) {
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002065 set_current_state(TASK_INTERRUPTIBLE);
Peter Hurleye5842cd2011-07-24 00:10:35 -04002066
2067 if (kthread_should_stop())
2068 break;
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002069
2070 /* Process stuff */
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002071 rfcomm_process_sessions();
Peter Hurleye5842cd2011-07-24 00:10:35 -04002072
2073 schedule();
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002074 }
Peter Hurleye5842cd2011-07-24 00:10:35 -04002075 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 rfcomm_kill_listener();
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 0;
2080}
2081
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002082static void rfcomm_security_cfm(struct hci_conn *conn, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct rfcomm_session *s;
2085 struct rfcomm_dlc *d;
2086 struct list_head *p, *n;
2087
2088 BT_DBG("conn %p status 0x%02x encrypt 0x%02x", conn, status, encrypt);
2089
2090 s = rfcomm_session_get(&conn->hdev->bdaddr, &conn->dst);
2091 if (!s)
2092 return;
2093
2094 rfcomm_session_hold(s);
2095
2096 list_for_each_safe(p, n, &s->dlcs) {
2097 d = list_entry(p, struct rfcomm_dlc, list);
2098
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002099 if (test_and_clear_bit(RFCOMM_SEC_PENDING, &d->flags)) {
2100 rfcomm_dlc_clear_timer(d);
2101 if (status || encrypt == 0x00) {
Szymon Jancdb544672011-09-26 14:19:47 +02002102 set_bit(RFCOMM_ENC_DROP, &d->flags);
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002103 continue;
2104 }
2105 }
2106
2107 if (d->state == BT_CONNECTED && !status && encrypt == 0x00) {
2108 if (d->sec_level == BT_SECURITY_MEDIUM) {
2109 set_bit(RFCOMM_SEC_PENDING, &d->flags);
2110 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
2111 continue;
2112 } else if (d->sec_level == BT_SECURITY_HIGH) {
Szymon Jancdb544672011-09-26 14:19:47 +02002113 set_bit(RFCOMM_ENC_DROP, &d->flags);
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002114 continue;
2115 }
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02002116 }
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (!test_and_clear_bit(RFCOMM_AUTH_PENDING, &d->flags))
2119 continue;
2120
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +02002121 if (!status && hci_conn_check_secure(conn, d->sec_level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 set_bit(RFCOMM_AUTH_ACCEPT, &d->flags);
2123 else
2124 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
2125 }
2126
2127 rfcomm_session_put(s);
2128
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +03002129 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
2132static struct hci_cb rfcomm_cb = {
2133 .name = "RFCOMM",
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002134 .security_cfm = rfcomm_security_cfm
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135};
2136
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002137static int rfcomm_dlc_debugfs_show(struct seq_file *f, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
2139 struct rfcomm_session *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 rfcomm_lock();
2142
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002143 list_for_each_entry(s, &session_list, list) {
2144 struct rfcomm_dlc *d;
2145 list_for_each_entry(d, &s->dlcs, list) {
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002146 struct sock *sk = s->sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002148 seq_printf(f, "%s %s %ld %d %d %d %d\n",
2149 batostr(&bt_sk(sk)->src),
2150 batostr(&bt_sk(sk)->dst),
2151 d->state, d->dlci, d->mtu,
2152 d->rx_credits, d->tx_credits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 rfcomm_unlock();
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002157
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002161static int rfcomm_dlc_debugfs_open(struct inode *inode, struct file *file)
2162{
2163 return single_open(file, rfcomm_dlc_debugfs_show, inode->i_private);
2164}
2165
2166static const struct file_operations rfcomm_dlc_debugfs_fops = {
2167 .open = rfcomm_dlc_debugfs_open,
2168 .read = seq_read,
2169 .llseek = seq_lseek,
2170 .release = single_release,
2171};
2172
2173static struct dentry *rfcomm_dlc_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175/* ---- Initialization ---- */
2176static int __init rfcomm_init(void)
2177{
Marcel Holtmann52d18342009-08-22 14:49:36 -07002178 int err;
Dave Youngaf0d3b12009-08-03 04:26:16 +00002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 hci_register_cb(&rfcomm_cb);
2181
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002182 rfcomm_thread = kthread_run(rfcomm_run, NULL, "krfcommd");
2183 if (IS_ERR(rfcomm_thread)) {
Marcel Holtmann52d18342009-08-22 14:49:36 -07002184 err = PTR_ERR(rfcomm_thread);
2185 goto unregister;
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002188 if (bt_debugfs) {
2189 rfcomm_dlc_debugfs = debugfs_create_file("rfcomm_dlc", 0444,
2190 bt_debugfs, NULL, &rfcomm_dlc_debugfs_fops);
2191 if (!rfcomm_dlc_debugfs)
2192 BT_ERR("Failed to create RFCOMM debug file");
2193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Marcel Holtmann52d18342009-08-22 14:49:36 -07002195 err = rfcomm_init_ttys();
2196 if (err < 0)
2197 goto stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Marcel Holtmann52d18342009-08-22 14:49:36 -07002199 err = rfcomm_init_sockets();
2200 if (err < 0)
2201 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002203 BT_INFO("RFCOMM ver %s", VERSION);
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return 0;
Dave Youngaf0d3b12009-08-03 04:26:16 +00002206
Marcel Holtmann52d18342009-08-22 14:49:36 -07002207cleanup:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002208 rfcomm_cleanup_ttys();
Marcel Holtmann52d18342009-08-22 14:49:36 -07002209
2210stop:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002211 kthread_stop(rfcomm_thread);
Marcel Holtmann52d18342009-08-22 14:49:36 -07002212
2213unregister:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002214 hci_unregister_cb(&rfcomm_cb);
2215
Marcel Holtmann52d18342009-08-22 14:49:36 -07002216 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
2219static void __exit rfcomm_exit(void)
2220{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002221 debugfs_remove(rfcomm_dlc_debugfs);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 hci_unregister_cb(&rfcomm_cb);
2224
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002225 kthread_stop(rfcomm_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 rfcomm_cleanup_ttys();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 rfcomm_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
2232module_init(rfcomm_init);
2233module_exit(rfcomm_exit);
2234
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02002235module_param(disable_cfc, bool, 0644);
2236MODULE_PARM_DESC(disable_cfc, "Disable credit based flow control");
2237
Marcel Holtmann98bcd082006-07-14 11:42:12 +02002238module_param(channel_mtu, int, 0644);
2239MODULE_PARM_DESC(channel_mtu, "Default MTU for the RFCOMM channel");
2240
Marcel Holtmann56f3a402006-02-13 11:39:57 +01002241module_param(l2cap_mtu, uint, 0644);
2242MODULE_PARM_DESC(l2cap_mtu, "Default MTU for the L2CAP connection");
2243
Marcel Holtmanneae38ee2009-10-05 12:23:48 +02002244module_param(l2cap_ertm, bool, 0644);
2245MODULE_PARM_DESC(l2cap_ertm, "Use L2CAP ERTM mode for connection");
2246
Marcel Holtmann63fbd242008-08-18 13:23:53 +02002247MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248MODULE_DESCRIPTION("Bluetooth RFCOMM ver " VERSION);
2249MODULE_VERSION(VERSION);
2250MODULE_LICENSE("GPL");
2251MODULE_ALIAS("bt-proto-3");