blob: 2b5c543638ba044e938254e29cc9995cfa9c5d64 [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>
Marcel Holtmannaef7d972010-03-21 05:27:45 +010029#include <linux/debugfs.h>
Marcel Holtmanna524ecc2007-10-20 21:37:20 +020030#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/unaligned.h>
32
33#include <net/bluetooth/bluetooth.h>
34#include <net/bluetooth/hci_core.h>
35#include <net/bluetooth/l2cap.h>
36#include <net/bluetooth/rfcomm.h>
37
Marcel Holtmann5f9018a2009-01-16 10:09:50 +010038#define VERSION "1.11"
Marcel Holtmann56f3a402006-02-13 11:39:57 +010039
Rusty Russelleb939922011-12-19 14:08:01 +000040static bool disable_cfc;
41static bool l2cap_ertm;
Marcel Holtmann98bcd082006-07-14 11:42:12 +020042static int channel_mtu = -1;
Marcel Holtmann56f3a402006-02-13 11:39:57 +010043static unsigned int l2cap_mtu = RFCOMM_MAX_L2CAP_MTU;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static struct task_struct *rfcomm_thread;
46
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080047static DEFINE_MUTEX(rfcomm_mutex);
48#define rfcomm_lock() mutex_lock(&rfcomm_mutex)
49#define rfcomm_unlock() mutex_unlock(&rfcomm_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52static LIST_HEAD(session_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Gustavo F. Padovan54365382011-12-20 16:30:44 -020054static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci);
56static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci);
57static int rfcomm_queue_disc(struct rfcomm_dlc *d);
58static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type);
59static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d);
60static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig);
61static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len);
62static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits);
63static void rfcomm_make_uih(struct sk_buff *skb, u8 addr);
64
65static void rfcomm_process_connect(struct rfcomm_session *s);
66
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +030067static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
68 bdaddr_t *dst,
69 u8 sec_level,
70 int *err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst);
Dean Jenkins8ff52f72013-02-28 14:21:55 +000072static struct rfcomm_session *rfcomm_session_del(struct rfcomm_session *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/* ---- RFCOMM frame parsing macros ---- */
75#define __get_dlci(b) ((b & 0xfc) >> 2)
76#define __get_channel(b) ((b & 0xf8) >> 3)
77#define __get_dir(b) ((b & 0x04) >> 2)
78#define __get_type(b) ((b & 0xef))
79
80#define __test_ea(b) ((b & 0x01))
81#define __test_cr(b) ((b & 0x02))
82#define __test_pf(b) ((b & 0x10))
83
84#define __addr(cr, dlci) (((dlci & 0x3f) << 2) | (cr << 1) | 0x01)
85#define __ctrl(type, pf) (((type & 0xef) | (pf << 4)))
86#define __dlci(dir, chn) (((chn & 0x1f) << 1) | dir)
87#define __srv_channel(dlci) (dlci >> 1)
88#define __dir(dlci) (dlci & 0x01)
89
90#define __len8(len) (((len) << 1) | 1)
91#define __len16(len) ((len) << 1)
92
93/* MCC macros */
94#define __mcc_type(cr, type) (((type << 2) | (cr << 1) | 0x01))
95#define __get_mcc_type(b) ((b & 0xfc) >> 2)
96#define __get_mcc_len(b) ((b & 0xfe) >> 1)
97
98/* RPN macros */
J. Suter3a5e9032005-08-09 20:28:46 -070099#define __rpn_line_settings(data, stop, parity) ((data & 0x3) | ((stop & 0x1) << 2) | ((parity & 0x7) << 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define __get_rpn_data_bits(line) ((line) & 0x3)
101#define __get_rpn_stop_bits(line) (((line) >> 2) & 0x1)
J. Suter3a5e9032005-08-09 20:28:46 -0700102#define __get_rpn_parity(line) (((line) >> 3) & 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300104static void rfcomm_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 if (!rfcomm_thread)
107 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 wake_up_process(rfcomm_thread);
109}
110
Dean Jenkins8ff52f72013-02-28 14:21:55 +0000111static struct rfcomm_session *rfcomm_session_put(struct rfcomm_session *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Dean Jenkins8ff52f72013-02-28 14:21:55 +0000113 if (s && atomic_dec_and_test(&s->refcnt))
114 s = rfcomm_session_del(s);
115
116 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119/* ---- RFCOMM FCS computation ---- */
120
Marcel Holtmann408c1ce2005-10-28 19:20:36 +0200121/* reversed, 8-bit, poly=0x07 */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900122static unsigned char rfcomm_crc_table[256] = {
Marcel Holtmann408c1ce2005-10-28 19:20:36 +0200123 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
124 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
125 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
126 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
127
128 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
129 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
130 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
131 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
132
133 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
134 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
135 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
136 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
137
138 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
139 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
140 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
141 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
142
143 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
144 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
145 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
146 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
147
148 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
149 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
150 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
151 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
152
153 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
154 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
155 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
156 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
157
158 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
159 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
160 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
161 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
162};
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* CRC on 2 bytes */
165#define __crc(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]])
166
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900167/* FCS on 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static inline u8 __fcs(u8 *data)
169{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000170 return 0xff - __crc(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900173/* FCS on 3 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static inline u8 __fcs2(u8 *data)
175{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000176 return 0xff - rfcomm_crc_table[__crc(data) ^ data[2]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179/* Check FCS */
180static inline int __check_fcs(u8 *data, int type, u8 fcs)
181{
182 u8 f = __crc(data);
183
184 if (type != RFCOMM_UIH)
185 f = rfcomm_crc_table[f ^ data[2]];
186
187 return rfcomm_crc_table[f ^ fcs] != 0xcf;
188}
189
190/* ---- L2CAP callbacks ---- */
191static void rfcomm_l2state_change(struct sock *sk)
192{
193 BT_DBG("%p state %d", sk, sk->sk_state);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300194 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197static void rfcomm_l2data_ready(struct sock *sk, int bytes)
198{
199 BT_DBG("%p bytes %d", sk, bytes);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300200 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203static int rfcomm_l2sock_create(struct socket **sock)
204{
205 int err;
206
207 BT_DBG("");
208
209 err = sock_create_kern(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP, sock);
210 if (!err) {
211 struct sock *sk = (*sock)->sk;
212 sk->sk_data_ready = rfcomm_l2data_ready;
213 sk->sk_state_change = rfcomm_l2state_change;
214 }
215 return err;
216}
217
Gustavo Padovan6039aa72012-05-23 04:04:18 -0300218static int rfcomm_check_security(struct rfcomm_dlc *d)
Marcel Holtmann77db1982008-07-14 20:13:45 +0200219{
220 struct sock *sk = d->session->sock->sk;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300221 struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
222
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100223 __u8 auth_type;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200224
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100225 switch (d->sec_level) {
226 case BT_SECURITY_HIGH:
227 auth_type = HCI_AT_GENERAL_BONDING_MITM;
228 break;
229 case BT_SECURITY_MEDIUM:
230 auth_type = HCI_AT_GENERAL_BONDING;
231 break;
232 default:
233 auth_type = HCI_AT_NO_BONDING;
234 break;
235 }
236
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300237 return hci_conn_security(conn->hcon, d->sec_level, auth_type);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200238}
239
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300240static void rfcomm_session_timeout(unsigned long arg)
241{
242 struct rfcomm_session *s = (void *) arg;
243
244 BT_DBG("session %p state %ld", s, s->state);
245
246 set_bit(RFCOMM_TIMED_OUT, &s->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300247 rfcomm_schedule();
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300248}
249
250static void rfcomm_session_set_timer(struct rfcomm_session *s, long timeout)
251{
252 BT_DBG("session %p state %ld timeout %ld", s, s->state, timeout);
253
254 if (!mod_timer(&s->timer, jiffies + timeout))
255 rfcomm_session_hold(s);
256}
257
258static void rfcomm_session_clear_timer(struct rfcomm_session *s)
259{
260 BT_DBG("session %p state %ld", s, s->state);
261
Dean Jenkinsfea7b022013-02-28 14:21:53 +0000262 if (del_timer_sync(&s->timer))
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300263 rfcomm_session_put(s);
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/* ---- RFCOMM DLCs ---- */
267static void rfcomm_dlc_timeout(unsigned long arg)
268{
269 struct rfcomm_dlc *d = (void *) arg;
270
271 BT_DBG("dlc %p state %ld", d, d->state);
272
273 set_bit(RFCOMM_TIMED_OUT, &d->flags);
274 rfcomm_dlc_put(d);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300275 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278static void rfcomm_dlc_set_timer(struct rfcomm_dlc *d, long timeout)
279{
280 BT_DBG("dlc %p state %ld timeout %ld", d, d->state, timeout);
281
282 if (!mod_timer(&d->timer, jiffies + timeout))
283 rfcomm_dlc_hold(d);
284}
285
286static void rfcomm_dlc_clear_timer(struct rfcomm_dlc *d)
287{
288 BT_DBG("dlc %p state %ld", d, d->state);
289
Ying Xue25cc4ae2013-02-03 20:32:57 +0000290 if (del_timer(&d->timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 rfcomm_dlc_put(d);
292}
293
294static void rfcomm_dlc_clear_state(struct rfcomm_dlc *d)
295{
296 BT_DBG("%p", d);
297
298 d->state = BT_OPEN;
299 d->flags = 0;
300 d->mscex = 0;
Johan Hedberg183f7322010-12-06 15:56:17 +0200301 d->sec_level = BT_SECURITY_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 d->mtu = RFCOMM_DEFAULT_MTU;
303 d->v24_sig = RFCOMM_V24_RTC | RFCOMM_V24_RTR | RFCOMM_V24_DV;
304
305 d->cfc = RFCOMM_CFC_DISABLED;
306 d->rx_credits = RFCOMM_DEFAULT_CREDITS;
307}
308
Al Virodd0fc662005-10-07 07:46:04 +0100309struct rfcomm_dlc *rfcomm_dlc_alloc(gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200311 struct rfcomm_dlc *d = kzalloc(sizeof(*d), prio);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!d)
314 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800316 setup_timer(&d->timer, rfcomm_dlc_timeout, (unsigned long)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 skb_queue_head_init(&d->tx_queue);
319 spin_lock_init(&d->lock);
320 atomic_set(&d->refcnt, 1);
321
322 rfcomm_dlc_clear_state(d);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 BT_DBG("%p", d);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return d;
327}
328
329void rfcomm_dlc_free(struct rfcomm_dlc *d)
330{
331 BT_DBG("%p", d);
332
333 skb_queue_purge(&d->tx_queue);
334 kfree(d);
335}
336
337static void rfcomm_dlc_link(struct rfcomm_session *s, struct rfcomm_dlc *d)
338{
339 BT_DBG("dlc %p session %p", d, s);
340
341 rfcomm_session_hold(s);
342
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300343 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 rfcomm_dlc_hold(d);
345 list_add(&d->list, &s->dlcs);
346 d->session = s;
347}
348
349static void rfcomm_dlc_unlink(struct rfcomm_dlc *d)
350{
351 struct rfcomm_session *s = d->session;
352
353 BT_DBG("dlc %p refcnt %d session %p", d, atomic_read(&d->refcnt), s);
354
355 list_del(&d->list);
356 d->session = NULL;
357 rfcomm_dlc_put(d);
358
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300359 if (list_empty(&s->dlcs))
360 rfcomm_session_set_timer(s, RFCOMM_IDLE_TIMEOUT);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 rfcomm_session_put(s);
363}
364
365static struct rfcomm_dlc *rfcomm_dlc_get(struct rfcomm_session *s, u8 dlci)
366{
367 struct rfcomm_dlc *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200369 list_for_each_entry(d, &s->dlcs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (d->dlci == dlci)
371 return d;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return NULL;
374}
375
376static int __rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
377{
378 struct rfcomm_session *s;
379 int err = 0;
380 u8 dlci;
381
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300382 BT_DBG("dlc %p state %ld %pMR -> %pMR channel %d",
383 d, d->state, src, dst, channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (channel < 1 || channel > 30)
386 return -EINVAL;
387
388 if (d->state != BT_OPEN && d->state != BT_CLOSED)
389 return 0;
390
391 s = rfcomm_session_get(src, dst);
392 if (!s) {
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +0300393 s = rfcomm_session_create(src, dst, d->sec_level, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!s)
395 return err;
396 }
397
398 dlci = __dlci(!s->initiator, channel);
399
400 /* Check if DLCI already exists */
401 if (rfcomm_dlc_get(s, dlci))
402 return -EBUSY;
403
404 rfcomm_dlc_clear_state(d);
405
406 d->dlci = dlci;
407 d->addr = __addr(s->initiator, dlci);
408 d->priority = 7;
409
Marcel Holtmann77db1982008-07-14 20:13:45 +0200410 d->state = BT_CONFIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 rfcomm_dlc_link(s, d);
412
Marcel Holtmann77db1982008-07-14 20:13:45 +0200413 d->out = 1;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 d->mtu = s->mtu;
416 d->cfc = (s->cfc == RFCOMM_CFC_UNKNOWN) ? 0 : s->cfc;
417
Marcel Holtmann77db1982008-07-14 20:13:45 +0200418 if (s->state == BT_CONNECTED) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100419 if (rfcomm_check_security(d))
Marcel Holtmann77db1982008-07-14 20:13:45 +0200420 rfcomm_send_pn(s, 1, d);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100421 else
422 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200423 }
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428}
429
430int rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
431{
432 int r;
433
434 rfcomm_lock();
435
436 r = __rfcomm_dlc_open(d, src, dst, channel);
437
438 rfcomm_unlock();
439 return r;
440}
441
442static int __rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
443{
444 struct rfcomm_session *s = d->session;
445 if (!s)
446 return 0;
447
448 BT_DBG("dlc %p state %ld dlci %d err %d session %p",
449 d, d->state, d->dlci, err, s);
450
451 switch (d->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 case BT_CONNECT:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100453 case BT_CONFIG:
454 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
455 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300456 rfcomm_schedule();
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100457 break;
458 }
459 /* Fall through */
460
461 case BT_CONNECTED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 d->state = BT_DISCONN;
463 if (skb_queue_empty(&d->tx_queue)) {
464 rfcomm_send_disc(s, d->dlci);
465 rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT);
466 } else {
467 rfcomm_queue_disc(d);
468 rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT * 2);
469 }
470 break;
471
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100472 case BT_OPEN:
Marcel Holtmann8bf47942009-02-16 02:59:49 +0100473 case BT_CONNECT2:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100474 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
475 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300476 rfcomm_schedule();
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100477 break;
478 }
479 /* Fall through */
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 default:
482 rfcomm_dlc_clear_timer(d);
483
484 rfcomm_dlc_lock(d);
485 d->state = BT_CLOSED;
Dave Young1905f6c2008-04-01 23:59:06 -0700486 d->state_change(d, err);
Arjan van de Ven4c8411f2008-05-29 01:32:47 -0700487 rfcomm_dlc_unlock(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 skb_queue_purge(&d->tx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 rfcomm_dlc_unlink(d);
491 }
492
493 return 0;
494}
495
496int rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
497{
Dean Jenkinsc06f7d52013-02-28 14:21:54 +0000498 int r = 0;
499 struct rfcomm_dlc *d_list;
500 struct rfcomm_session *s, *s_list;
501
502 BT_DBG("dlc %p state %ld dlci %d err %d", d, d->state, d->dlci, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 rfcomm_lock();
505
Dean Jenkinsc06f7d52013-02-28 14:21:54 +0000506 s = d->session;
507 if (!s)
508 goto no_session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Dean Jenkinsc06f7d52013-02-28 14:21:54 +0000510 /* after waiting on the mutex check the session still exists
511 * then check the dlc still exists
512 */
513 list_for_each_entry(s_list, &session_list, list) {
514 if (s_list == s) {
515 list_for_each_entry(d_list, &s->dlcs, list) {
516 if (d_list == d) {
517 r = __rfcomm_dlc_close(d, err);
518 break;
519 }
520 }
521 break;
522 }
523 }
524
525no_session:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 rfcomm_unlock();
527 return r;
528}
529
530int rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb)
531{
532 int len = skb->len;
533
534 if (d->state != BT_CONNECTED)
535 return -ENOTCONN;
536
537 BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len);
538
539 if (len > d->mtu)
540 return -EINVAL;
541
542 rfcomm_make_uih(skb, d->addr);
543 skb_queue_tail(&d->tx_queue, skb);
544
545 if (!test_bit(RFCOMM_TX_THROTTLED, &d->flags))
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300546 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return len;
548}
549
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800550void __rfcomm_dlc_throttle(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 BT_DBG("dlc %p state %ld", d, d->state);
553
554 if (!d->cfc) {
555 d->v24_sig |= RFCOMM_V24_FC;
556 set_bit(RFCOMM_MSC_PENDING, &d->flags);
557 }
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300558 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800561void __rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 BT_DBG("dlc %p state %ld", d, d->state);
564
565 if (!d->cfc) {
566 d->v24_sig &= ~RFCOMM_V24_FC;
567 set_bit(RFCOMM_MSC_PENDING, &d->flags);
568 }
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300569 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900572/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 Set/get modem status functions use _local_ status i.e. what we report
574 to the other side.
575 Remote status is provided by dlc->modem_status() callback.
576 */
577int rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig)
578{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900579 BT_DBG("dlc %p state %ld v24_sig 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 d, d->state, v24_sig);
581
582 if (test_bit(RFCOMM_RX_THROTTLED, &d->flags))
583 v24_sig |= RFCOMM_V24_FC;
584 else
585 v24_sig &= ~RFCOMM_V24_FC;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 d->v24_sig = v24_sig;
588
589 if (!test_and_set_bit(RFCOMM_MSC_PENDING, &d->flags))
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300590 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 return 0;
593}
594
595int rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig)
596{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900597 BT_DBG("dlc %p state %ld v24_sig 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 d, d->state, d->v24_sig);
599
600 *v24_sig = d->v24_sig;
601 return 0;
602}
603
604/* ---- RFCOMM sessions ---- */
605static struct rfcomm_session *rfcomm_session_add(struct socket *sock, int state)
606{
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200607 struct rfcomm_session *s = kzalloc(sizeof(*s), GFP_KERNEL);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (!s)
610 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 BT_DBG("session %p sock %p", s, sock);
613
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300614 setup_timer(&s->timer, rfcomm_session_timeout, (unsigned long) s);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 INIT_LIST_HEAD(&s->dlcs);
617 s->state = state;
618 s->sock = sock;
619
620 s->mtu = RFCOMM_DEFAULT_MTU;
Marcel Holtmann7c2660b2006-07-03 10:02:51 +0200621 s->cfc = disable_cfc ? RFCOMM_CFC_DISABLED : RFCOMM_CFC_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* Do not increment module usage count for listening sessions.
624 * Otherwise we won't be able to unload the module. */
625 if (state != BT_LISTEN)
626 if (!try_module_get(THIS_MODULE)) {
627 kfree(s);
628 return NULL;
629 }
630
631 list_add(&s->list, &session_list);
632
633 return s;
634}
635
Dean Jenkins8ff52f72013-02-28 14:21:55 +0000636static struct rfcomm_session *rfcomm_session_del(struct rfcomm_session *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 int state = s->state;
639
640 BT_DBG("session %p state %ld", s, s->state);
641
642 list_del(&s->list);
643
644 if (state == BT_CONNECTED)
645 rfcomm_send_disc(s, 0);
646
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300647 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 sock_release(s->sock);
649 kfree(s);
650
651 if (state != BT_LISTEN)
652 module_put(THIS_MODULE);
Dean Jenkins8ff52f72013-02-28 14:21:55 +0000653
654 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst)
658{
659 struct rfcomm_session *s;
660 struct list_head *p, *n;
661 struct bt_sock *sk;
662 list_for_each_safe(p, n, &session_list) {
663 s = list_entry(p, struct rfcomm_session, list);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900664 sk = bt_sk(s->sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if ((!bacmp(src, BDADDR_ANY) || !bacmp(&sk->src, src)) &&
667 !bacmp(&sk->dst, dst))
668 return s;
669 }
670 return NULL;
671}
672
Dean Jenkins8ff52f72013-02-28 14:21:55 +0000673static struct rfcomm_session *rfcomm_session_close(struct rfcomm_session *s,
674 int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct rfcomm_dlc *d;
677 struct list_head *p, *n;
678
679 BT_DBG("session %p state %ld err %d", s, s->state, err);
680
681 rfcomm_session_hold(s);
682
683 s->state = BT_CLOSED;
684
685 /* Close all dlcs */
686 list_for_each_safe(p, n, &s->dlcs) {
687 d = list_entry(p, struct rfcomm_dlc, list);
688 d->state = BT_CLOSED;
689 __rfcomm_dlc_close(d, err);
690 }
691
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300692 rfcomm_session_clear_timer(s);
Dean Jenkins8ff52f72013-02-28 14:21:55 +0000693 return rfcomm_session_put(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +0300696static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
697 bdaddr_t *dst,
698 u8 sec_level,
699 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 struct rfcomm_session *s = NULL;
702 struct sockaddr_l2 addr;
703 struct socket *sock;
704 struct sock *sk;
705
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +0300706 BT_DBG("%pMR -> %pMR", src, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 *err = rfcomm_l2sock_create(&sock);
709 if (*err < 0)
710 return NULL;
711
712 bacpy(&addr.l2_bdaddr, src);
713 addr.l2_family = AF_BLUETOOTH;
714 addr.l2_psm = 0;
Marcel Holtmann37e62f52009-02-17 21:49:33 +0100715 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +0200716 *err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (*err < 0)
718 goto failed;
719
720 /* Set L2CAP options */
721 sk = sock->sk;
722 lock_sock(sk);
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300723 l2cap_pi(sk)->chan->imtu = l2cap_mtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300724 l2cap_pi(sk)->chan->sec_level = sec_level;
Marcel Holtmanneae38ee2009-10-05 12:23:48 +0200725 if (l2cap_ertm)
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300726 l2cap_pi(sk)->chan->mode = L2CAP_MODE_ERTM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 release_sock(sk);
728
729 s = rfcomm_session_add(sock, BT_BOUND);
730 if (!s) {
731 *err = -ENOMEM;
732 goto failed;
733 }
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 s->initiator = 1;
736
737 bacpy(&addr.l2_bdaddr, dst);
738 addr.l2_family = AF_BLUETOOTH;
Syam Sidhardhan5bcb8092012-10-10 22:09:29 +0530739 addr.l2_psm = __constant_cpu_to_le16(RFCOMM_PSM);
Marcel Holtmann37e62f52009-02-17 21:49:33 +0100740 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +0200741 *err = kernel_connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK);
Marcel Holtmannb4c612a2006-09-23 09:54:38 +0200742 if (*err == 0 || *err == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return s;
744
Dean Jenkins8ff52f72013-02-28 14:21:55 +0000745 return rfcomm_session_del(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747failed:
748 sock_release(sock);
749 return NULL;
750}
751
752void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst)
753{
754 struct sock *sk = s->sock->sk;
755 if (src)
756 bacpy(src, &bt_sk(sk)->src);
757 if (dst)
758 bacpy(dst, &bt_sk(sk)->dst);
759}
760
761/* ---- RFCOMM frame sending ---- */
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200762static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct kvec iv = { data, len };
765 struct msghdr msg;
766
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200767 BT_DBG("session %p len %d", s, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 memset(&msg, 0, sizeof(msg));
770
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200771 return kernel_sendmsg(s->sock, &msg, &iv, 1, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200774static int rfcomm_send_cmd(struct rfcomm_session *s, struct rfcomm_cmd *cmd)
775{
776 BT_DBG("%p cmd %u", s, cmd->ctrl);
777
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200778 return rfcomm_send_frame(s, (void *) cmd, sizeof(*cmd));
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci)
782{
783 struct rfcomm_cmd cmd;
784
785 BT_DBG("%p dlci %d", s, dlci);
786
787 cmd.addr = __addr(s->initiator, dlci);
788 cmd.ctrl = __ctrl(RFCOMM_SABM, 1);
789 cmd.len = __len8(0);
790 cmd.fcs = __fcs2((u8 *) &cmd);
791
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200792 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795static int rfcomm_send_ua(struct rfcomm_session *s, u8 dlci)
796{
797 struct rfcomm_cmd cmd;
798
799 BT_DBG("%p dlci %d", s, dlci);
800
801 cmd.addr = __addr(!s->initiator, dlci);
802 cmd.ctrl = __ctrl(RFCOMM_UA, 1);
803 cmd.len = __len8(0);
804 cmd.fcs = __fcs2((u8 *) &cmd);
805
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200806 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci)
810{
811 struct rfcomm_cmd cmd;
812
813 BT_DBG("%p dlci %d", s, dlci);
814
815 cmd.addr = __addr(s->initiator, dlci);
816 cmd.ctrl = __ctrl(RFCOMM_DISC, 1);
817 cmd.len = __len8(0);
818 cmd.fcs = __fcs2((u8 *) &cmd);
819
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200820 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823static int rfcomm_queue_disc(struct rfcomm_dlc *d)
824{
825 struct rfcomm_cmd *cmd;
826 struct sk_buff *skb;
827
828 BT_DBG("dlc %p dlci %d", d, d->dlci);
829
830 skb = alloc_skb(sizeof(*cmd), GFP_KERNEL);
831 if (!skb)
832 return -ENOMEM;
833
834 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
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200881 return rfcomm_send_frame(s, buf, ptr - buf);
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
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200923 return rfcomm_send_frame(s, buf, ptr - buf);
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
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200961 return rfcomm_send_frame(s, buf, ptr - buf);
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
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200988 return rfcomm_send_frame(s, buf, ptr - buf);
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
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001015 return rfcomm_send_frame(s, buf, ptr - buf);
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
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001037 return rfcomm_send_frame(s, buf, ptr - buf);
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
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001059 return rfcomm_send_frame(s, buf, ptr - buf);
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
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001110 return rfcomm_send_frame(s, buf, ptr - buf);
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 ---- */
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001134static struct rfcomm_session *rfcomm_recv_ua(struct rfcomm_session *s, u8 dlci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
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);
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001143 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
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);
Mat Martineau79e65472011-12-06 16:23:26 -08001165 rfcomm_session_clear_timer(s);
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001166 }
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 break;
1169 }
1170 } else {
1171 /* Control channel */
1172 switch (s->state) {
1173 case BT_CONNECT:
1174 s->state = BT_CONNECTED;
1175 rfcomm_process_connect(s);
1176 break;
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001177
1178 case BT_DISCONN:
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001179 s = rfcomm_session_close(s, ECONNRESET);
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182 }
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001183 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001186static struct rfcomm_session *rfcomm_recv_dm(struct rfcomm_session *s, u8 dlci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 int err = 0;
1189
1190 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1191
1192 if (dlci) {
1193 /* Data DLC */
1194 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1195 if (d) {
1196 if (d->state == BT_CONNECT || d->state == BT_CONFIG)
1197 err = ECONNREFUSED;
1198 else
1199 err = ECONNRESET;
1200
1201 d->state = BT_CLOSED;
1202 __rfcomm_dlc_close(d, err);
1203 }
1204 } else {
1205 if (s->state == BT_CONNECT)
1206 err = ECONNREFUSED;
1207 else
1208 err = ECONNRESET;
1209
1210 s->state = BT_CLOSED;
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001211 s = rfcomm_session_close(s, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001213 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001216static struct rfcomm_session *rfcomm_recv_disc(struct rfcomm_session *s,
1217 u8 dlci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 int err = 0;
1220
1221 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1222
1223 if (dlci) {
1224 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1225 if (d) {
1226 rfcomm_send_ua(s, dlci);
1227
1228 if (d->state == BT_CONNECT || d->state == BT_CONFIG)
1229 err = ECONNREFUSED;
1230 else
1231 err = ECONNRESET;
1232
1233 d->state = BT_CLOSED;
1234 __rfcomm_dlc_close(d, err);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001235 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 rfcomm_send_dm(s, dlci);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 } else {
1239 rfcomm_send_ua(s, 0);
1240
1241 if (s->state == BT_CONNECT)
1242 err = ECONNREFUSED;
1243 else
1244 err = ECONNRESET;
1245
1246 s->state = BT_CLOSED;
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001247 s = rfcomm_session_close(s, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001249 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001252void rfcomm_dlc_accept(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Marcel Holtmann300b9392006-07-03 10:37:55 +02001254 struct sock *sk = d->session->sock->sk;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001255 struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
Marcel Holtmann300b9392006-07-03 10:37:55 +02001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 BT_DBG("dlc %p", d);
1258
1259 rfcomm_send_ua(d->session, d->dlci);
1260
Johan Hedberge2139b32009-03-26 16:41:56 +02001261 rfcomm_dlc_clear_timer(d);
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 rfcomm_dlc_lock(d);
1264 d->state = BT_CONNECTED;
1265 d->state_change(d, 0);
1266 rfcomm_dlc_unlock(d);
1267
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001268 if (d->role_switch)
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001269 hci_conn_switch_role(conn->hcon, 0x00);
Marcel Holtmann300b9392006-07-03 10:37:55 +02001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig);
1272}
1273
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001274static void rfcomm_check_accept(struct rfcomm_dlc *d)
1275{
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001276 if (rfcomm_check_security(d)) {
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001277 if (d->defer_setup) {
1278 set_bit(RFCOMM_DEFER_SETUP, &d->flags);
1279 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8bf47942009-02-16 02:59:49 +01001280
1281 rfcomm_dlc_lock(d);
1282 d->state = BT_CONNECT2;
1283 d->state_change(d, 0);
1284 rfcomm_dlc_unlock(d);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001285 } else
1286 rfcomm_dlc_accept(d);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001287 } else {
1288 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
1289 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001290 }
1291}
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293static int rfcomm_recv_sabm(struct rfcomm_session *s, u8 dlci)
1294{
1295 struct rfcomm_dlc *d;
1296 u8 channel;
1297
1298 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1299
1300 if (!dlci) {
1301 rfcomm_send_ua(s, 0);
1302
1303 if (s->state == BT_OPEN) {
1304 s->state = BT_CONNECTED;
1305 rfcomm_process_connect(s);
1306 }
1307 return 0;
1308 }
1309
1310 /* Check if DLC exists */
1311 d = rfcomm_dlc_get(s, dlci);
1312 if (d) {
1313 if (d->state == BT_OPEN) {
1314 /* DLC was previously opened by PN request */
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001315 rfcomm_check_accept(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 return 0;
1318 }
1319
1320 /* Notify socket layer about incoming connection */
1321 channel = __srv_channel(dlci);
1322 if (rfcomm_connect_ind(s, channel, &d)) {
1323 d->dlci = dlci;
1324 d->addr = __addr(s->initiator, dlci);
1325 rfcomm_dlc_link(s, d);
1326
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001327 rfcomm_check_accept(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 } else {
1329 rfcomm_send_dm(s, dlci);
1330 }
1331
1332 return 0;
1333}
1334
1335static int rfcomm_apply_pn(struct rfcomm_dlc *d, int cr, struct rfcomm_pn *pn)
1336{
1337 struct rfcomm_session *s = d->session;
1338
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001339 BT_DBG("dlc %p state %ld dlci %d mtu %d fc 0x%x credits %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 d, d->state, d->dlci, pn->mtu, pn->flow_ctrl, pn->credits);
1341
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001342 if ((pn->flow_ctrl == 0xf0 && s->cfc != RFCOMM_CFC_DISABLED) ||
1343 pn->flow_ctrl == 0xe0) {
1344 d->cfc = RFCOMM_CFC_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 d->tx_credits = pn->credits;
1346 } else {
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001347 d->cfc = RFCOMM_CFC_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1349 }
1350
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001351 if (s->cfc == RFCOMM_CFC_UNKNOWN)
1352 s->cfc = d->cfc;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 d->priority = pn->priority;
1355
Marcel Holtmannb4324b52009-06-07 18:06:51 +02001356 d->mtu = __le16_to_cpu(pn->mtu);
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001357
1358 if (cr && d->mtu > s->mtu)
1359 d->mtu = s->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 return 0;
1362}
1363
1364static int rfcomm_recv_pn(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1365{
1366 struct rfcomm_pn *pn = (void *) skb->data;
1367 struct rfcomm_dlc *d;
1368 u8 dlci = pn->dlci;
1369
1370 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1371
1372 if (!dlci)
1373 return 0;
1374
1375 d = rfcomm_dlc_get(s, dlci);
1376 if (d) {
1377 if (cr) {
1378 /* PN request */
1379 rfcomm_apply_pn(d, cr, pn);
1380 rfcomm_send_pn(s, 0, d);
1381 } else {
1382 /* PN response */
1383 switch (d->state) {
1384 case BT_CONFIG:
1385 rfcomm_apply_pn(d, cr, pn);
1386
1387 d->state = BT_CONNECT;
1388 rfcomm_send_sabm(s, d->dlci);
1389 break;
1390 }
1391 }
1392 } else {
1393 u8 channel = __srv_channel(dlci);
1394
1395 if (!cr)
1396 return 0;
1397
1398 /* PN request for non existing DLC.
1399 * Assume incoming connection. */
1400 if (rfcomm_connect_ind(s, channel, &d)) {
1401 d->dlci = dlci;
1402 d->addr = __addr(s->initiator, dlci);
1403 rfcomm_dlc_link(s, d);
1404
1405 rfcomm_apply_pn(d, cr, pn);
1406
1407 d->state = BT_OPEN;
1408 rfcomm_send_pn(s, 0, d);
1409 } else {
1410 rfcomm_send_dm(s, dlci);
1411 }
1412 }
1413 return 0;
1414}
1415
1416static int rfcomm_recv_rpn(struct rfcomm_session *s, int cr, int len, struct sk_buff *skb)
1417{
1418 struct rfcomm_rpn *rpn = (void *) skb->data;
1419 u8 dlci = __get_dlci(rpn->dlci);
1420
1421 u8 bit_rate = 0;
1422 u8 data_bits = 0;
1423 u8 stop_bits = 0;
1424 u8 parity = 0;
1425 u8 flow_ctrl = 0;
1426 u8 xon_char = 0;
1427 u8 xoff_char = 0;
1428 u16 rpn_mask = RFCOMM_RPN_PM_ALL;
J. Suter3a5e9032005-08-09 20:28:46 -07001429
1430 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",
1431 dlci, cr, len, rpn->bit_rate, rpn->line_settings, rpn->flow_ctrl,
1432 rpn->xon_char, rpn->xoff_char, rpn->param_mask);
1433
1434 if (!cr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return 0;
J. Suter3a5e9032005-08-09 20:28:46 -07001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (len == 1) {
Yuri Kululin08601462010-07-23 13:57:12 +04001438 /* This is a request, return default (according to ETSI TS 07.10) settings */
1439 bit_rate = RFCOMM_RPN_BR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 data_bits = RFCOMM_RPN_DATA_8;
1441 stop_bits = RFCOMM_RPN_STOP_1;
1442 parity = RFCOMM_RPN_PARITY_NONE;
1443 flow_ctrl = RFCOMM_RPN_FLOW_NONE;
1444 xon_char = RFCOMM_RPN_XON_CHAR;
1445 xoff_char = RFCOMM_RPN_XOFF_CHAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto rpn_out;
1447 }
J. Suter3a5e9032005-08-09 20:28:46 -07001448
1449 /* Check for sane values, ignore/accept bit_rate, 8 bits, 1 stop bit,
1450 * no parity, no flow control lines, normal XON/XOFF chars */
1451
Al Viroe8db8c92006-11-08 00:28:44 -08001452 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_BITRATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 bit_rate = rpn->bit_rate;
Yuri Kululin08601462010-07-23 13:57:12 +04001454 if (bit_rate > RFCOMM_RPN_BR_230400) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 BT_DBG("RPN bit rate mismatch 0x%x", bit_rate);
Yuri Kululin08601462010-07-23 13:57:12 +04001456 bit_rate = RFCOMM_RPN_BR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 rpn_mask ^= RFCOMM_RPN_PM_BITRATE;
1458 }
1459 }
J. Suter3a5e9032005-08-09 20:28:46 -07001460
Al Viroe8db8c92006-11-08 00:28:44 -08001461 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 data_bits = __get_rpn_data_bits(rpn->line_settings);
1463 if (data_bits != RFCOMM_RPN_DATA_8) {
1464 BT_DBG("RPN data bits mismatch 0x%x", data_bits);
1465 data_bits = RFCOMM_RPN_DATA_8;
1466 rpn_mask ^= RFCOMM_RPN_PM_DATA;
1467 }
1468 }
J. Suter3a5e9032005-08-09 20:28:46 -07001469
Al Viroe8db8c92006-11-08 00:28:44 -08001470 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_STOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 stop_bits = __get_rpn_stop_bits(rpn->line_settings);
1472 if (stop_bits != RFCOMM_RPN_STOP_1) {
1473 BT_DBG("RPN stop bits mismatch 0x%x", stop_bits);
1474 stop_bits = RFCOMM_RPN_STOP_1;
1475 rpn_mask ^= RFCOMM_RPN_PM_STOP;
1476 }
1477 }
J. Suter3a5e9032005-08-09 20:28:46 -07001478
Al Viroe8db8c92006-11-08 00:28:44 -08001479 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_PARITY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 parity = __get_rpn_parity(rpn->line_settings);
1481 if (parity != RFCOMM_RPN_PARITY_NONE) {
1482 BT_DBG("RPN parity mismatch 0x%x", parity);
1483 parity = RFCOMM_RPN_PARITY_NONE;
1484 rpn_mask ^= RFCOMM_RPN_PM_PARITY;
1485 }
1486 }
J. Suter3a5e9032005-08-09 20:28:46 -07001487
Al Viroe8db8c92006-11-08 00:28:44 -08001488 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_FLOW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 flow_ctrl = rpn->flow_ctrl;
1490 if (flow_ctrl != RFCOMM_RPN_FLOW_NONE) {
1491 BT_DBG("RPN flow ctrl mismatch 0x%x", flow_ctrl);
1492 flow_ctrl = RFCOMM_RPN_FLOW_NONE;
1493 rpn_mask ^= RFCOMM_RPN_PM_FLOW;
1494 }
1495 }
J. Suter3a5e9032005-08-09 20:28:46 -07001496
Al Viroe8db8c92006-11-08 00:28:44 -08001497 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 xon_char = rpn->xon_char;
1499 if (xon_char != RFCOMM_RPN_XON_CHAR) {
1500 BT_DBG("RPN XON char mismatch 0x%x", xon_char);
1501 xon_char = RFCOMM_RPN_XON_CHAR;
1502 rpn_mask ^= RFCOMM_RPN_PM_XON;
1503 }
1504 }
J. Suter3a5e9032005-08-09 20:28:46 -07001505
Al Viroe8db8c92006-11-08 00:28:44 -08001506 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XOFF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 xoff_char = rpn->xoff_char;
1508 if (xoff_char != RFCOMM_RPN_XOFF_CHAR) {
1509 BT_DBG("RPN XOFF char mismatch 0x%x", xoff_char);
1510 xoff_char = RFCOMM_RPN_XOFF_CHAR;
1511 rpn_mask ^= RFCOMM_RPN_PM_XOFF;
1512 }
1513 }
1514
1515rpn_out:
J. Suter3a5e9032005-08-09 20:28:46 -07001516 rfcomm_send_rpn(s, 0, dlci, bit_rate, data_bits, stop_bits,
1517 parity, flow_ctrl, xon_char, xoff_char, rpn_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 return 0;
1520}
1521
1522static int rfcomm_recv_rls(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1523{
1524 struct rfcomm_rls *rls = (void *) skb->data;
1525 u8 dlci = __get_dlci(rls->dlci);
1526
1527 BT_DBG("dlci %d cr %d status 0x%x", dlci, cr, rls->status);
J. Suter3a5e9032005-08-09 20:28:46 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (!cr)
1530 return 0;
1531
J. Suter3a5e9032005-08-09 20:28:46 -07001532 /* We should probably do something with this information here. But
1533 * for now it's sufficient just to reply -- Bluetooth 1.1 says it's
1534 * mandatory to recognise and respond to RLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 rfcomm_send_rls(s, 0, dlci, rls->status);
1537
1538 return 0;
1539}
1540
1541static int rfcomm_recv_msc(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1542{
1543 struct rfcomm_msc *msc = (void *) skb->data;
1544 struct rfcomm_dlc *d;
1545 u8 dlci = __get_dlci(msc->dlci);
1546
1547 BT_DBG("dlci %d cr %d v24 0x%x", dlci, cr, msc->v24_sig);
1548
1549 d = rfcomm_dlc_get(s, dlci);
J. Suter3a5e9032005-08-09 20:28:46 -07001550 if (!d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return 0;
1552
1553 if (cr) {
1554 if (msc->v24_sig & RFCOMM_V24_FC && !d->cfc)
1555 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1556 else
1557 clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
J. Suter3a5e9032005-08-09 20:28:46 -07001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 rfcomm_dlc_lock(d);
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +02001560
1561 d->remote_v24_sig = msc->v24_sig;
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (d->modem_status)
1564 d->modem_status(d, msc->v24_sig);
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +02001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 rfcomm_dlc_unlock(d);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 rfcomm_send_msc(s, 0, dlci, msc->v24_sig);
1569
1570 d->mscex |= RFCOMM_MSCEX_RX;
J. Suter3a5e9032005-08-09 20:28:46 -07001571 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 d->mscex |= RFCOMM_MSCEX_TX;
1573
1574 return 0;
1575}
1576
1577static int rfcomm_recv_mcc(struct rfcomm_session *s, struct sk_buff *skb)
1578{
1579 struct rfcomm_mcc *mcc = (void *) skb->data;
1580 u8 type, cr, len;
1581
1582 cr = __test_cr(mcc->type);
1583 type = __get_mcc_type(mcc->type);
1584 len = __get_mcc_len(mcc->len);
1585
1586 BT_DBG("%p type 0x%x cr %d", s, type, cr);
1587
1588 skb_pull(skb, 2);
1589
1590 switch (type) {
1591 case RFCOMM_PN:
1592 rfcomm_recv_pn(s, cr, skb);
1593 break;
1594
1595 case RFCOMM_RPN:
1596 rfcomm_recv_rpn(s, cr, len, skb);
1597 break;
1598
1599 case RFCOMM_RLS:
1600 rfcomm_recv_rls(s, cr, skb);
1601 break;
1602
1603 case RFCOMM_MSC:
1604 rfcomm_recv_msc(s, cr, skb);
1605 break;
1606
1607 case RFCOMM_FCOFF:
1608 if (cr) {
1609 set_bit(RFCOMM_TX_THROTTLED, &s->flags);
1610 rfcomm_send_fcoff(s, 0);
1611 }
1612 break;
1613
1614 case RFCOMM_FCON:
1615 if (cr) {
1616 clear_bit(RFCOMM_TX_THROTTLED, &s->flags);
1617 rfcomm_send_fcon(s, 0);
1618 }
1619 break;
1620
1621 case RFCOMM_TEST:
1622 if (cr)
1623 rfcomm_send_test(s, 0, skb->data, skb->len);
1624 break;
1625
1626 case RFCOMM_NSC:
1627 break;
1628
1629 default:
1630 BT_ERR("Unknown control type 0x%02x", type);
1631 rfcomm_send_nsc(s, cr, type);
1632 break;
1633 }
1634 return 0;
1635}
1636
1637static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk_buff *skb)
1638{
1639 struct rfcomm_dlc *d;
1640
1641 BT_DBG("session %p state %ld dlci %d pf %d", s, s->state, dlci, pf);
1642
1643 d = rfcomm_dlc_get(s, dlci);
1644 if (!d) {
1645 rfcomm_send_dm(s, dlci);
1646 goto drop;
1647 }
1648
1649 if (pf && d->cfc) {
1650 u8 credits = *(u8 *) skb->data; skb_pull(skb, 1);
1651
1652 d->tx_credits += credits;
1653 if (d->tx_credits)
1654 clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
1655 }
1656
1657 if (skb->len && d->state == BT_CONNECTED) {
1658 rfcomm_dlc_lock(d);
1659 d->rx_credits--;
1660 d->data_ready(d, skb);
1661 rfcomm_dlc_unlock(d);
1662 return 0;
1663 }
1664
1665drop:
1666 kfree_skb(skb);
1667 return 0;
1668}
1669
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001670static struct rfcomm_session *rfcomm_recv_frame(struct rfcomm_session *s,
1671 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct rfcomm_hdr *hdr = (void *) skb->data;
1674 u8 type, dlci, fcs;
1675
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001676 if (!s) {
1677 /* no session, so free socket data */
1678 kfree_skb(skb);
1679 return s;
1680 }
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 dlci = __get_dlci(hdr->addr);
1683 type = __get_type(hdr->ctrl);
1684
1685 /* Trim FCS */
1686 skb->len--; skb->tail--;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001687 fcs = *(u8 *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 if (__check_fcs(skb->data, type, fcs)) {
1690 BT_ERR("bad checksum in packet");
1691 kfree_skb(skb);
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001692 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694
1695 if (__test_ea(hdr->len))
1696 skb_pull(skb, 3);
1697 else
1698 skb_pull(skb, 4);
1699
1700 switch (type) {
1701 case RFCOMM_SABM:
1702 if (__test_pf(hdr->ctrl))
1703 rfcomm_recv_sabm(s, dlci);
1704 break;
1705
1706 case RFCOMM_DISC:
1707 if (__test_pf(hdr->ctrl))
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001708 s = rfcomm_recv_disc(s, dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 break;
1710
1711 case RFCOMM_UA:
1712 if (__test_pf(hdr->ctrl))
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001713 s = rfcomm_recv_ua(s, dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 break;
1715
1716 case RFCOMM_DM:
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001717 s = rfcomm_recv_dm(s, dlci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 break;
1719
1720 case RFCOMM_UIH:
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001721 if (dlci) {
1722 rfcomm_recv_data(s, dlci, __test_pf(hdr->ctrl), skb);
1723 return s;
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 rfcomm_recv_mcc(s, skb);
1726 break;
1727
1728 default:
Andrei Emeltchenko5017d8d2010-09-08 16:26:53 +03001729 BT_ERR("Unknown packet type 0x%02x", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 break;
1731 }
1732 kfree_skb(skb);
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001733 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
1736/* ---- Connection and data processing ---- */
1737
1738static void rfcomm_process_connect(struct rfcomm_session *s)
1739{
1740 struct rfcomm_dlc *d;
1741 struct list_head *p, *n;
1742
1743 BT_DBG("session %p state %ld", s, s->state);
1744
1745 list_for_each_safe(p, n, &s->dlcs) {
1746 d = list_entry(p, struct rfcomm_dlc, list);
1747 if (d->state == BT_CONFIG) {
1748 d->mtu = s->mtu;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001749 if (rfcomm_check_security(d)) {
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001750 rfcomm_send_pn(s, 1, d);
1751 } else {
Marcel Holtmann77db1982008-07-14 20:13:45 +02001752 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
1753 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 }
1757}
1758
1759/* Send data queued for the DLC.
1760 * Return number of frames left in the queue.
1761 */
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001762static int rfcomm_process_tx(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 struct sk_buff *skb;
1765 int err;
1766
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001767 BT_DBG("dlc %p state %ld cfc %d rx_credits %d tx_credits %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 d, d->state, d->cfc, d->rx_credits, d->tx_credits);
1769
1770 /* Send pending MSC */
1771 if (test_and_clear_bit(RFCOMM_MSC_PENDING, &d->flags))
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001772 rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 if (d->cfc) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001775 /* CFC enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * Give them some credits */
1777 if (!test_bit(RFCOMM_RX_THROTTLED, &d->flags) &&
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001778 d->rx_credits <= (d->cfc >> 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 rfcomm_send_credits(d->session, d->addr, d->cfc - d->rx_credits);
1780 d->rx_credits = d->cfc;
1781 }
1782 } else {
1783 /* CFC disabled.
1784 * Give ourselves some credits */
1785 d->tx_credits = 5;
1786 }
1787
1788 if (test_bit(RFCOMM_TX_THROTTLED, &d->flags))
1789 return skb_queue_len(&d->tx_queue);
1790
1791 while (d->tx_credits && (skb = skb_dequeue(&d->tx_queue))) {
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001792 err = rfcomm_send_frame(d->session, skb->data, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (err < 0) {
1794 skb_queue_head(&d->tx_queue, skb);
1795 break;
1796 }
1797 kfree_skb(skb);
1798 d->tx_credits--;
1799 }
1800
1801 if (d->cfc && !d->tx_credits) {
1802 /* We're out of TX credits.
1803 * Set TX_THROTTLED flag to avoid unnesary wakeups by dlc_send. */
1804 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1805 }
1806
1807 return skb_queue_len(&d->tx_queue);
1808}
1809
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001810static void rfcomm_process_dlcs(struct rfcomm_session *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 struct rfcomm_dlc *d;
1813 struct list_head *p, *n;
1814
1815 BT_DBG("session %p state %ld", s, s->state);
1816
1817 list_for_each_safe(p, n, &s->dlcs) {
1818 d = list_entry(p, struct rfcomm_dlc, list);
1819
1820 if (test_bit(RFCOMM_TIMED_OUT, &d->flags)) {
1821 __rfcomm_dlc_close(d, ETIMEDOUT);
1822 continue;
1823 }
1824
Szymon Jancdb544672011-09-26 14:19:47 +02001825 if (test_bit(RFCOMM_ENC_DROP, &d->flags)) {
1826 __rfcomm_dlc_close(d, ECONNREFUSED);
1827 continue;
1828 }
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (test_and_clear_bit(RFCOMM_AUTH_ACCEPT, &d->flags)) {
1831 rfcomm_dlc_clear_timer(d);
Marcel Holtmann77db1982008-07-14 20:13:45 +02001832 if (d->out) {
1833 rfcomm_send_pn(s, 1, d);
1834 rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001835 } else {
1836 if (d->defer_setup) {
1837 set_bit(RFCOMM_DEFER_SETUP, &d->flags);
1838 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8bf47942009-02-16 02:59:49 +01001839
1840 rfcomm_dlc_lock(d);
1841 d->state = BT_CONNECT2;
1842 d->state_change(d, 0);
1843 rfcomm_dlc_unlock(d);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001844 } else
1845 rfcomm_dlc_accept(d);
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 continue;
1848 } else if (test_and_clear_bit(RFCOMM_AUTH_REJECT, &d->flags)) {
1849 rfcomm_dlc_clear_timer(d);
Marcel Holtmann77db1982008-07-14 20:13:45 +02001850 if (!d->out)
1851 rfcomm_send_dm(s, d->dlci);
1852 else
1853 d->state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 __rfcomm_dlc_close(d, ECONNREFUSED);
1855 continue;
1856 }
1857
Jaikumar Ganesh6e1031a2009-02-02 18:03:57 -08001858 if (test_bit(RFCOMM_SEC_PENDING, &d->flags))
1859 continue;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (test_bit(RFCOMM_TX_THROTTLED, &s->flags))
1862 continue;
1863
1864 if ((d->state == BT_CONNECTED || d->state == BT_DISCONN) &&
Marcel Holtmann77db1982008-07-14 20:13:45 +02001865 d->mscex == RFCOMM_MSCEX_OK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 rfcomm_process_tx(d);
1867 }
1868}
1869
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001870static struct rfcomm_session *rfcomm_process_rx(struct rfcomm_session *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 struct socket *sock = s->sock;
1873 struct sock *sk = sock->sk;
1874 struct sk_buff *skb;
1875
1876 BT_DBG("session %p state %ld qlen %d", s, s->state, skb_queue_len(&sk->sk_receive_queue));
1877
1878 /* Get data directly from socket receive queue without copying it. */
1879 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
1880 skb_orphan(skb);
Mat Martineau44935722011-07-22 14:53:58 -07001881 if (!skb_linearize(skb))
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001882 s = rfcomm_recv_frame(s, skb);
Mat Martineau44935722011-07-22 14:53:58 -07001883 else
1884 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001887 if (s && (sk->sk_state == BT_CLOSED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (!s->initiator)
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001889 s = rfcomm_session_put(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001891 if (s)
1892 s = rfcomm_session_close(s, sk->sk_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001894
1895 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001898static void rfcomm_accept_connection(struct rfcomm_session *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 struct socket *sock = s->sock, *nsock;
1901 int err;
1902
1903 /* Fast check for a new connection.
1904 * Avoids unnesesary socket allocations. */
1905 if (list_empty(&bt_sk(sock->sk)->accept_q))
1906 return;
1907
1908 BT_DBG("session %p", s);
1909
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02001910 err = kernel_accept(sock, &nsock, O_NONBLOCK);
1911 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return;
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /* Set our callbacks */
1915 nsock->sk->sk_data_ready = rfcomm_l2data_ready;
1916 nsock->sk->sk_state_change = rfcomm_l2state_change;
1917
1918 s = rfcomm_session_add(nsock, BT_OPEN);
1919 if (s) {
1920 rfcomm_session_hold(s);
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001921
1922 /* We should adjust MTU on incoming sessions.
1923 * L2CAP MTU minus UIH header and FCS. */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001924 s->mtu = min(l2cap_pi(nsock->sk)->chan->omtu,
1925 l2cap_pi(nsock->sk)->chan->imtu) - 5;
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001926
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +03001927 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 } else
1929 sock_release(nsock);
1930}
1931
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001932static struct rfcomm_session *rfcomm_check_connection(struct rfcomm_session *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 struct sock *sk = s->sock->sk;
1935
1936 BT_DBG("%p state %ld", s, s->state);
1937
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +02001938 switch (sk->sk_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 case BT_CONNECTED:
1940 s->state = BT_CONNECT;
1941
1942 /* We can adjust MTU on outgoing sessions.
1943 * L2CAP MTU minus UIH header and FCS. */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001944 s->mtu = min(l2cap_pi(sk)->chan->omtu, l2cap_pi(sk)->chan->imtu) - 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 rfcomm_send_sabm(s, 0);
1947 break;
1948
1949 case BT_CLOSED:
1950 s->state = BT_CLOSED;
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001951 s = rfcomm_session_close(s, sk->sk_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
1953 }
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001954 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001957static void rfcomm_process_sessions(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
1959 struct list_head *p, *n;
1960
1961 rfcomm_lock();
1962
1963 list_for_each_safe(p, n, &session_list) {
1964 struct rfcomm_session *s;
1965 s = list_entry(p, struct rfcomm_session, list);
1966
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -03001967 if (test_and_clear_bit(RFCOMM_TIMED_OUT, &s->flags)) {
1968 s->state = BT_DISCONN;
1969 rfcomm_send_disc(s, 0);
Marcel Holtmann485f1ef2010-02-03 15:52:18 -08001970 rfcomm_session_put(s);
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -03001971 continue;
1972 }
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (s->state == BT_LISTEN) {
1975 rfcomm_accept_connection(s);
1976 continue;
1977 }
1978
1979 rfcomm_session_hold(s);
1980
1981 switch (s->state) {
1982 case BT_BOUND:
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001983 s = rfcomm_check_connection(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 break;
1985
1986 default:
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001987 s = rfcomm_process_rx(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 break;
1989 }
1990
Dean Jenkins8ff52f72013-02-28 14:21:55 +00001991 if (s)
1992 rfcomm_process_dlcs(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 rfcomm_session_put(s);
1995 }
1996
1997 rfcomm_unlock();
1998}
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000static int rfcomm_add_listener(bdaddr_t *ba)
2001{
2002 struct sockaddr_l2 addr;
2003 struct socket *sock;
2004 struct sock *sk;
2005 struct rfcomm_session *s;
2006 int err = 0;
2007
2008 /* Create socket */
2009 err = rfcomm_l2sock_create(&sock);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09002010 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 BT_ERR("Create socket failed %d", err);
2012 return err;
2013 }
2014
2015 /* Bind socket */
2016 bacpy(&addr.l2_bdaddr, ba);
2017 addr.l2_family = AF_BLUETOOTH;
Syam Sidhardhan5bcb8092012-10-10 22:09:29 +05302018 addr.l2_psm = __constant_cpu_to_le16(RFCOMM_PSM);
Marcel Holtmann37e62f52009-02-17 21:49:33 +01002019 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02002020 err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (err < 0) {
2022 BT_ERR("Bind failed %d", err);
2023 goto failed;
2024 }
2025
2026 /* Set L2CAP options */
2027 sk = sock->sk;
2028 lock_sock(sk);
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03002029 l2cap_pi(sk)->chan->imtu = l2cap_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 release_sock(sk);
2031
2032 /* Start listening on the socket */
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02002033 err = kernel_listen(sock, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (err) {
2035 BT_ERR("Listen failed %d", err);
2036 goto failed;
2037 }
2038
2039 /* Add listening session */
2040 s = rfcomm_session_add(sock, BT_LISTEN);
2041 if (!s)
2042 goto failed;
2043
2044 rfcomm_session_hold(s);
2045 return 0;
2046failed:
2047 sock_release(sock);
2048 return err;
2049}
2050
2051static void rfcomm_kill_listener(void)
2052{
2053 struct rfcomm_session *s;
2054 struct list_head *p, *n;
2055
2056 BT_DBG("");
2057
2058 list_for_each_safe(p, n, &session_list) {
2059 s = list_entry(p, struct rfcomm_session, list);
2060 rfcomm_session_del(s);
2061 }
2062}
2063
2064static int rfcomm_run(void *unused)
2065{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 BT_DBG("");
2067
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002068 set_user_nice(current, -10);
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 rfcomm_add_listener(BDADDR_ANY);
2071
Peter Hurleye5842cd2011-07-24 00:10:35 -04002072 while (1) {
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002073 set_current_state(TASK_INTERRUPTIBLE);
Peter Hurleye5842cd2011-07-24 00:10:35 -04002074
2075 if (kthread_should_stop())
2076 break;
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002077
2078 /* Process stuff */
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002079 rfcomm_process_sessions();
Peter Hurleye5842cd2011-07-24 00:10:35 -04002080
2081 schedule();
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002082 }
Peter Hurleye5842cd2011-07-24 00:10:35 -04002083 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 rfcomm_kill_listener();
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return 0;
2088}
2089
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002090static void rfcomm_security_cfm(struct hci_conn *conn, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 struct rfcomm_session *s;
2093 struct rfcomm_dlc *d;
2094 struct list_head *p, *n;
2095
2096 BT_DBG("conn %p status 0x%02x encrypt 0x%02x", conn, status, encrypt);
2097
2098 s = rfcomm_session_get(&conn->hdev->bdaddr, &conn->dst);
2099 if (!s)
2100 return;
2101
2102 rfcomm_session_hold(s);
2103
2104 list_for_each_safe(p, n, &s->dlcs) {
2105 d = list_entry(p, struct rfcomm_dlc, list);
2106
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002107 if (test_and_clear_bit(RFCOMM_SEC_PENDING, &d->flags)) {
2108 rfcomm_dlc_clear_timer(d);
2109 if (status || encrypt == 0x00) {
Szymon Jancdb544672011-09-26 14:19:47 +02002110 set_bit(RFCOMM_ENC_DROP, &d->flags);
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002111 continue;
2112 }
2113 }
2114
2115 if (d->state == BT_CONNECTED && !status && encrypt == 0x00) {
2116 if (d->sec_level == BT_SECURITY_MEDIUM) {
2117 set_bit(RFCOMM_SEC_PENDING, &d->flags);
2118 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
2119 continue;
2120 } else if (d->sec_level == BT_SECURITY_HIGH) {
Szymon Jancdb544672011-09-26 14:19:47 +02002121 set_bit(RFCOMM_ENC_DROP, &d->flags);
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002122 continue;
2123 }
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02002124 }
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (!test_and_clear_bit(RFCOMM_AUTH_PENDING, &d->flags))
2127 continue;
2128
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +02002129 if (!status && hci_conn_check_secure(conn, d->sec_level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 set_bit(RFCOMM_AUTH_ACCEPT, &d->flags);
2131 else
2132 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
2133 }
2134
2135 rfcomm_session_put(s);
2136
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +03002137 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
2140static struct hci_cb rfcomm_cb = {
2141 .name = "RFCOMM",
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002142 .security_cfm = rfcomm_security_cfm
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143};
2144
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002145static int rfcomm_dlc_debugfs_show(struct seq_file *f, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 struct rfcomm_session *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 rfcomm_lock();
2150
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002151 list_for_each_entry(s, &session_list, list) {
2152 struct rfcomm_dlc *d;
2153 list_for_each_entry(d, &s->dlcs, list) {
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002154 struct sock *sk = s->sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03002156 seq_printf(f, "%pMR %pMR %ld %d %d %d %d\n",
2157 &bt_sk(sk)->src, &bt_sk(sk)->dst,
2158 d->state, d->dlci, d->mtu,
2159 d->rx_credits, d->tx_credits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 rfcomm_unlock();
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002164
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002168static int rfcomm_dlc_debugfs_open(struct inode *inode, struct file *file)
2169{
2170 return single_open(file, rfcomm_dlc_debugfs_show, inode->i_private);
2171}
2172
2173static const struct file_operations rfcomm_dlc_debugfs_fops = {
2174 .open = rfcomm_dlc_debugfs_open,
2175 .read = seq_read,
2176 .llseek = seq_lseek,
2177 .release = single_release,
2178};
2179
2180static struct dentry *rfcomm_dlc_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182/* ---- Initialization ---- */
2183static int __init rfcomm_init(void)
2184{
Marcel Holtmann52d18342009-08-22 14:49:36 -07002185 int err;
Dave Youngaf0d3b12009-08-03 04:26:16 +00002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 hci_register_cb(&rfcomm_cb);
2188
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002189 rfcomm_thread = kthread_run(rfcomm_run, NULL, "krfcommd");
2190 if (IS_ERR(rfcomm_thread)) {
Marcel Holtmann52d18342009-08-22 14:49:36 -07002191 err = PTR_ERR(rfcomm_thread);
2192 goto unregister;
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002195 if (bt_debugfs) {
2196 rfcomm_dlc_debugfs = debugfs_create_file("rfcomm_dlc", 0444,
2197 bt_debugfs, NULL, &rfcomm_dlc_debugfs_fops);
2198 if (!rfcomm_dlc_debugfs)
2199 BT_ERR("Failed to create RFCOMM debug file");
2200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Marcel Holtmann52d18342009-08-22 14:49:36 -07002202 err = rfcomm_init_ttys();
2203 if (err < 0)
2204 goto stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Marcel Holtmann52d18342009-08-22 14:49:36 -07002206 err = rfcomm_init_sockets();
2207 if (err < 0)
2208 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002210 BT_INFO("RFCOMM ver %s", VERSION);
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return 0;
Dave Youngaf0d3b12009-08-03 04:26:16 +00002213
Marcel Holtmann52d18342009-08-22 14:49:36 -07002214cleanup:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002215 rfcomm_cleanup_ttys();
Marcel Holtmann52d18342009-08-22 14:49:36 -07002216
2217stop:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002218 kthread_stop(rfcomm_thread);
Marcel Holtmann52d18342009-08-22 14:49:36 -07002219
2220unregister:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002221 hci_unregister_cb(&rfcomm_cb);
2222
Marcel Holtmann52d18342009-08-22 14:49:36 -07002223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
2226static void __exit rfcomm_exit(void)
2227{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002228 debugfs_remove(rfcomm_dlc_debugfs);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 hci_unregister_cb(&rfcomm_cb);
2231
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002232 kthread_stop(rfcomm_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 rfcomm_cleanup_ttys();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 rfcomm_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
2239module_init(rfcomm_init);
2240module_exit(rfcomm_exit);
2241
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02002242module_param(disable_cfc, bool, 0644);
2243MODULE_PARM_DESC(disable_cfc, "Disable credit based flow control");
2244
Marcel Holtmann98bcd082006-07-14 11:42:12 +02002245module_param(channel_mtu, int, 0644);
2246MODULE_PARM_DESC(channel_mtu, "Default MTU for the RFCOMM channel");
2247
Marcel Holtmann56f3a402006-02-13 11:39:57 +01002248module_param(l2cap_mtu, uint, 0644);
2249MODULE_PARM_DESC(l2cap_mtu, "Default MTU for the L2CAP connection");
2250
Marcel Holtmanneae38ee2009-10-05 12:23:48 +02002251module_param(l2cap_ertm, bool, 0644);
2252MODULE_PARM_DESC(l2cap_ertm, "Use L2CAP ERTM mode for connection");
2253
Marcel Holtmann63fbd242008-08-18 13:23:53 +02002254MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255MODULE_DESCRIPTION("Bluetooth RFCOMM ver " VERSION);
2256MODULE_VERSION(VERSION);
2257MODULE_LICENSE("GPL");
2258MODULE_ALIAS("bt-proto-3");