blob: 501649bf5596d0373d6b6dd3be791dd1345ad33e [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
Gustavo F. Padovan54365382011-12-20 16:30:44 -020068static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci);
70static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci);
71static int rfcomm_queue_disc(struct rfcomm_dlc *d);
72static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type);
73static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d);
74static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig);
75static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len);
76static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits);
77static void rfcomm_make_uih(struct sk_buff *skb, u8 addr);
78
79static void rfcomm_process_connect(struct rfcomm_session *s);
80
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +030081static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
82 bdaddr_t *dst,
83 u8 sec_level,
84 int *err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst);
86static void rfcomm_session_del(struct rfcomm_session *s);
87
88/* ---- RFCOMM frame parsing macros ---- */
89#define __get_dlci(b) ((b & 0xfc) >> 2)
90#define __get_channel(b) ((b & 0xf8) >> 3)
91#define __get_dir(b) ((b & 0x04) >> 2)
92#define __get_type(b) ((b & 0xef))
93
94#define __test_ea(b) ((b & 0x01))
95#define __test_cr(b) ((b & 0x02))
96#define __test_pf(b) ((b & 0x10))
97
98#define __addr(cr, dlci) (((dlci & 0x3f) << 2) | (cr << 1) | 0x01)
99#define __ctrl(type, pf) (((type & 0xef) | (pf << 4)))
100#define __dlci(dir, chn) (((chn & 0x1f) << 1) | dir)
101#define __srv_channel(dlci) (dlci >> 1)
102#define __dir(dlci) (dlci & 0x01)
103
104#define __len8(len) (((len) << 1) | 1)
105#define __len16(len) ((len) << 1)
106
107/* MCC macros */
108#define __mcc_type(cr, type) (((type << 2) | (cr << 1) | 0x01))
109#define __get_mcc_type(b) ((b & 0xfc) >> 2)
110#define __get_mcc_len(b) ((b & 0xfe) >> 1)
111
112/* RPN macros */
J. Suter3a5e9032005-08-09 20:28:46 -0700113#define __rpn_line_settings(data, stop, parity) ((data & 0x3) | ((stop & 0x1) << 2) | ((parity & 0x7) << 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define __get_rpn_data_bits(line) ((line) & 0x3)
115#define __get_rpn_stop_bits(line) (((line) >> 2) & 0x1)
J. Suter3a5e9032005-08-09 20:28:46 -0700116#define __get_rpn_parity(line) (((line) >> 3) & 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300118static inline void rfcomm_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 if (!rfcomm_thread)
121 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 wake_up_process(rfcomm_thread);
123}
124
125static inline void rfcomm_session_put(struct rfcomm_session *s)
126{
127 if (atomic_dec_and_test(&s->refcnt))
128 rfcomm_session_del(s);
129}
130
131/* ---- RFCOMM FCS computation ---- */
132
Marcel Holtmann408c1ce2005-10-28 19:20:36 +0200133/* reversed, 8-bit, poly=0x07 */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900134static unsigned char rfcomm_crc_table[256] = {
Marcel Holtmann408c1ce2005-10-28 19:20:36 +0200135 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
136 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
137 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
138 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
139
140 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
141 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
142 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
143 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
144
145 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
146 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
147 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
148 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
149
150 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
151 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
152 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
153 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
154
155 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
156 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
157 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
158 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
159
160 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
161 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
162 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
163 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
164
165 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
166 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
167 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
168 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
169
170 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
171 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
172 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
173 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
174};
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/* CRC on 2 bytes */
177#define __crc(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]])
178
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900179/* FCS on 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static inline u8 __fcs(u8 *data)
181{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000182 return 0xff - __crc(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900185/* FCS on 3 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static inline u8 __fcs2(u8 *data)
187{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000188 return 0xff - rfcomm_crc_table[__crc(data) ^ data[2]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/* Check FCS */
192static inline int __check_fcs(u8 *data, int type, u8 fcs)
193{
194 u8 f = __crc(data);
195
196 if (type != RFCOMM_UIH)
197 f = rfcomm_crc_table[f ^ data[2]];
198
199 return rfcomm_crc_table[f ^ fcs] != 0xcf;
200}
201
202/* ---- L2CAP callbacks ---- */
203static void rfcomm_l2state_change(struct sock *sk)
204{
205 BT_DBG("%p state %d", sk, sk->sk_state);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300206 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209static void rfcomm_l2data_ready(struct sock *sk, int bytes)
210{
211 BT_DBG("%p bytes %d", sk, bytes);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300212 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215static int rfcomm_l2sock_create(struct socket **sock)
216{
217 int err;
218
219 BT_DBG("");
220
221 err = sock_create_kern(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP, sock);
222 if (!err) {
223 struct sock *sk = (*sock)->sk;
224 sk->sk_data_ready = rfcomm_l2data_ready;
225 sk->sk_state_change = rfcomm_l2state_change;
226 }
227 return err;
228}
229
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100230static inline int rfcomm_check_security(struct rfcomm_dlc *d)
Marcel Holtmann77db1982008-07-14 20:13:45 +0200231{
232 struct sock *sk = d->session->sock->sk;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300233 struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
234
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100235 __u8 auth_type;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200236
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100237 switch (d->sec_level) {
238 case BT_SECURITY_HIGH:
239 auth_type = HCI_AT_GENERAL_BONDING_MITM;
240 break;
241 case BT_SECURITY_MEDIUM:
242 auth_type = HCI_AT_GENERAL_BONDING;
243 break;
244 default:
245 auth_type = HCI_AT_NO_BONDING;
246 break;
247 }
248
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300249 return hci_conn_security(conn->hcon, d->sec_level, auth_type);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200250}
251
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300252static void rfcomm_session_timeout(unsigned long arg)
253{
254 struct rfcomm_session *s = (void *) arg;
255
256 BT_DBG("session %p state %ld", s, s->state);
257
258 set_bit(RFCOMM_TIMED_OUT, &s->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300259 rfcomm_schedule();
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300260}
261
262static void rfcomm_session_set_timer(struct rfcomm_session *s, long timeout)
263{
264 BT_DBG("session %p state %ld timeout %ld", s, s->state, timeout);
265
266 if (!mod_timer(&s->timer, jiffies + timeout))
267 rfcomm_session_hold(s);
268}
269
270static void rfcomm_session_clear_timer(struct rfcomm_session *s)
271{
272 BT_DBG("session %p state %ld", s, s->state);
273
274 if (timer_pending(&s->timer) && del_timer(&s->timer))
275 rfcomm_session_put(s);
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/* ---- RFCOMM DLCs ---- */
279static void rfcomm_dlc_timeout(unsigned long arg)
280{
281 struct rfcomm_dlc *d = (void *) arg;
282
283 BT_DBG("dlc %p state %ld", d, d->state);
284
285 set_bit(RFCOMM_TIMED_OUT, &d->flags);
286 rfcomm_dlc_put(d);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300287 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static void rfcomm_dlc_set_timer(struct rfcomm_dlc *d, long timeout)
291{
292 BT_DBG("dlc %p state %ld timeout %ld", d, d->state, timeout);
293
294 if (!mod_timer(&d->timer, jiffies + timeout))
295 rfcomm_dlc_hold(d);
296}
297
298static void rfcomm_dlc_clear_timer(struct rfcomm_dlc *d)
299{
300 BT_DBG("dlc %p state %ld", d, d->state);
301
302 if (timer_pending(&d->timer) && del_timer(&d->timer))
303 rfcomm_dlc_put(d);
304}
305
306static void rfcomm_dlc_clear_state(struct rfcomm_dlc *d)
307{
308 BT_DBG("%p", d);
309
310 d->state = BT_OPEN;
311 d->flags = 0;
312 d->mscex = 0;
Johan Hedberg183f7322010-12-06 15:56:17 +0200313 d->sec_level = BT_SECURITY_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 d->mtu = RFCOMM_DEFAULT_MTU;
315 d->v24_sig = RFCOMM_V24_RTC | RFCOMM_V24_RTR | RFCOMM_V24_DV;
316
317 d->cfc = RFCOMM_CFC_DISABLED;
318 d->rx_credits = RFCOMM_DEFAULT_CREDITS;
319}
320
Al Virodd0fc662005-10-07 07:46:04 +0100321struct rfcomm_dlc *rfcomm_dlc_alloc(gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200323 struct rfcomm_dlc *d = kzalloc(sizeof(*d), prio);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (!d)
326 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800328 setup_timer(&d->timer, rfcomm_dlc_timeout, (unsigned long)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 skb_queue_head_init(&d->tx_queue);
331 spin_lock_init(&d->lock);
332 atomic_set(&d->refcnt, 1);
333
334 rfcomm_dlc_clear_state(d);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 BT_DBG("%p", d);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return d;
339}
340
341void rfcomm_dlc_free(struct rfcomm_dlc *d)
342{
343 BT_DBG("%p", d);
344
345 skb_queue_purge(&d->tx_queue);
346 kfree(d);
347}
348
349static void rfcomm_dlc_link(struct rfcomm_session *s, struct rfcomm_dlc *d)
350{
351 BT_DBG("dlc %p session %p", d, s);
352
353 rfcomm_session_hold(s);
354
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300355 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 rfcomm_dlc_hold(d);
357 list_add(&d->list, &s->dlcs);
358 d->session = s;
359}
360
361static void rfcomm_dlc_unlink(struct rfcomm_dlc *d)
362{
363 struct rfcomm_session *s = d->session;
364
365 BT_DBG("dlc %p refcnt %d session %p", d, atomic_read(&d->refcnt), s);
366
367 list_del(&d->list);
368 d->session = NULL;
369 rfcomm_dlc_put(d);
370
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300371 if (list_empty(&s->dlcs))
372 rfcomm_session_set_timer(s, RFCOMM_IDLE_TIMEOUT);
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 rfcomm_session_put(s);
375}
376
377static struct rfcomm_dlc *rfcomm_dlc_get(struct rfcomm_session *s, u8 dlci)
378{
379 struct rfcomm_dlc *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200381 list_for_each_entry(d, &s->dlcs, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (d->dlci == dlci)
383 return d;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return NULL;
386}
387
388static int __rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
389{
390 struct rfcomm_session *s;
391 int err = 0;
392 u8 dlci;
393
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900394 BT_DBG("dlc %p state %ld %s %s channel %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 d, d->state, batostr(src), batostr(dst), channel);
396
397 if (channel < 1 || channel > 30)
398 return -EINVAL;
399
400 if (d->state != BT_OPEN && d->state != BT_CLOSED)
401 return 0;
402
403 s = rfcomm_session_get(src, dst);
404 if (!s) {
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +0300405 s = rfcomm_session_create(src, dst, d->sec_level, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (!s)
407 return err;
408 }
409
410 dlci = __dlci(!s->initiator, channel);
411
412 /* Check if DLCI already exists */
413 if (rfcomm_dlc_get(s, dlci))
414 return -EBUSY;
415
416 rfcomm_dlc_clear_state(d);
417
418 d->dlci = dlci;
419 d->addr = __addr(s->initiator, dlci);
420 d->priority = 7;
421
Marcel Holtmann77db1982008-07-14 20:13:45 +0200422 d->state = BT_CONFIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 rfcomm_dlc_link(s, d);
424
Marcel Holtmann77db1982008-07-14 20:13:45 +0200425 d->out = 1;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 d->mtu = s->mtu;
428 d->cfc = (s->cfc == RFCOMM_CFC_UNKNOWN) ? 0 : s->cfc;
429
Marcel Holtmann77db1982008-07-14 20:13:45 +0200430 if (s->state == BT_CONNECTED) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100431 if (rfcomm_check_security(d))
Marcel Holtmann77db1982008-07-14 20:13:45 +0200432 rfcomm_send_pn(s, 1, d);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100433 else
434 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200435 }
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440}
441
442int rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
443{
444 int r;
445
446 rfcomm_lock();
447
448 r = __rfcomm_dlc_open(d, src, dst, channel);
449
450 rfcomm_unlock();
451 return r;
452}
453
454static int __rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
455{
456 struct rfcomm_session *s = d->session;
457 if (!s)
458 return 0;
459
460 BT_DBG("dlc %p state %ld dlci %d err %d session %p",
461 d, d->state, d->dlci, err, s);
462
463 switch (d->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 case BT_CONNECT:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100465 case BT_CONFIG:
466 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
467 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300468 rfcomm_schedule();
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100469 break;
470 }
471 /* Fall through */
472
473 case BT_CONNECTED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 d->state = BT_DISCONN;
475 if (skb_queue_empty(&d->tx_queue)) {
476 rfcomm_send_disc(s, d->dlci);
477 rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT);
478 } else {
479 rfcomm_queue_disc(d);
480 rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT * 2);
481 }
482 break;
483
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100484 case BT_OPEN:
Marcel Holtmann8bf47942009-02-16 02:59:49 +0100485 case BT_CONNECT2:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100486 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
487 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300488 rfcomm_schedule();
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100489 break;
490 }
491 /* Fall through */
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 default:
494 rfcomm_dlc_clear_timer(d);
495
496 rfcomm_dlc_lock(d);
497 d->state = BT_CLOSED;
Dave Young1905f6c2008-04-01 23:59:06 -0700498 d->state_change(d, err);
Arjan van de Ven4c8411f2008-05-29 01:32:47 -0700499 rfcomm_dlc_unlock(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 skb_queue_purge(&d->tx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 rfcomm_dlc_unlink(d);
503 }
504
505 return 0;
506}
507
508int rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
509{
510 int r;
511
512 rfcomm_lock();
513
514 r = __rfcomm_dlc_close(d, err);
515
516 rfcomm_unlock();
517 return r;
518}
519
520int rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb)
521{
522 int len = skb->len;
523
524 if (d->state != BT_CONNECTED)
525 return -ENOTCONN;
526
527 BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len);
528
529 if (len > d->mtu)
530 return -EINVAL;
531
532 rfcomm_make_uih(skb, d->addr);
533 skb_queue_tail(&d->tx_queue, skb);
534
535 if (!test_bit(RFCOMM_TX_THROTTLED, &d->flags))
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300536 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return len;
538}
539
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800540void __rfcomm_dlc_throttle(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 BT_DBG("dlc %p state %ld", d, d->state);
543
544 if (!d->cfc) {
545 d->v24_sig |= RFCOMM_V24_FC;
546 set_bit(RFCOMM_MSC_PENDING, &d->flags);
547 }
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300548 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800551void __rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 BT_DBG("dlc %p state %ld", d, d->state);
554
555 if (!d->cfc) {
556 d->v24_sig &= ~RFCOMM_V24_FC;
557 set_bit(RFCOMM_MSC_PENDING, &d->flags);
558 }
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300559 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900562/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 Set/get modem status functions use _local_ status i.e. what we report
564 to the other side.
565 Remote status is provided by dlc->modem_status() callback.
566 */
567int rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig)
568{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900569 BT_DBG("dlc %p state %ld v24_sig 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 d, d->state, v24_sig);
571
572 if (test_bit(RFCOMM_RX_THROTTLED, &d->flags))
573 v24_sig |= RFCOMM_V24_FC;
574 else
575 v24_sig &= ~RFCOMM_V24_FC;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 d->v24_sig = v24_sig;
578
579 if (!test_and_set_bit(RFCOMM_MSC_PENDING, &d->flags))
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300580 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 return 0;
583}
584
585int rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig)
586{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900587 BT_DBG("dlc %p state %ld v24_sig 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 d, d->state, d->v24_sig);
589
590 *v24_sig = d->v24_sig;
591 return 0;
592}
593
594/* ---- RFCOMM sessions ---- */
595static struct rfcomm_session *rfcomm_session_add(struct socket *sock, int state)
596{
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200597 struct rfcomm_session *s = kzalloc(sizeof(*s), GFP_KERNEL);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (!s)
600 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 BT_DBG("session %p sock %p", s, sock);
603
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300604 setup_timer(&s->timer, rfcomm_session_timeout, (unsigned long) s);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 INIT_LIST_HEAD(&s->dlcs);
607 s->state = state;
608 s->sock = sock;
609
610 s->mtu = RFCOMM_DEFAULT_MTU;
Marcel Holtmann7c2660b2006-07-03 10:02:51 +0200611 s->cfc = disable_cfc ? RFCOMM_CFC_DISABLED : RFCOMM_CFC_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* Do not increment module usage count for listening sessions.
614 * Otherwise we won't be able to unload the module. */
615 if (state != BT_LISTEN)
616 if (!try_module_get(THIS_MODULE)) {
617 kfree(s);
618 return NULL;
619 }
620
621 list_add(&s->list, &session_list);
622
623 return s;
624}
625
626static void rfcomm_session_del(struct rfcomm_session *s)
627{
628 int state = s->state;
629
630 BT_DBG("session %p state %ld", s, s->state);
631
632 list_del(&s->list);
633
634 if (state == BT_CONNECTED)
635 rfcomm_send_disc(s, 0);
636
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300637 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 sock_release(s->sock);
639 kfree(s);
640
641 if (state != BT_LISTEN)
642 module_put(THIS_MODULE);
643}
644
645static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst)
646{
647 struct rfcomm_session *s;
648 struct list_head *p, *n;
649 struct bt_sock *sk;
650 list_for_each_safe(p, n, &session_list) {
651 s = list_entry(p, struct rfcomm_session, list);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900652 sk = bt_sk(s->sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if ((!bacmp(src, BDADDR_ANY) || !bacmp(&sk->src, src)) &&
655 !bacmp(&sk->dst, dst))
656 return s;
657 }
658 return NULL;
659}
660
661static void rfcomm_session_close(struct rfcomm_session *s, int err)
662{
663 struct rfcomm_dlc *d;
664 struct list_head *p, *n;
665
666 BT_DBG("session %p state %ld err %d", s, s->state, err);
667
668 rfcomm_session_hold(s);
669
670 s->state = BT_CLOSED;
671
672 /* Close all dlcs */
673 list_for_each_safe(p, n, &s->dlcs) {
674 d = list_entry(p, struct rfcomm_dlc, list);
675 d->state = BT_CLOSED;
676 __rfcomm_dlc_close(d, err);
677 }
678
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300679 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 rfcomm_session_put(s);
681}
682
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +0300683static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
684 bdaddr_t *dst,
685 u8 sec_level,
686 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct rfcomm_session *s = NULL;
689 struct sockaddr_l2 addr;
690 struct socket *sock;
691 struct sock *sk;
692
693 BT_DBG("%s %s", batostr(src), batostr(dst));
694
695 *err = rfcomm_l2sock_create(&sock);
696 if (*err < 0)
697 return NULL;
698
699 bacpy(&addr.l2_bdaddr, src);
700 addr.l2_family = AF_BLUETOOTH;
701 addr.l2_psm = 0;
Marcel Holtmann37e62f52009-02-17 21:49:33 +0100702 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +0200703 *err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (*err < 0)
705 goto failed;
706
707 /* Set L2CAP options */
708 sk = sock->sk;
709 lock_sock(sk);
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300710 l2cap_pi(sk)->chan->imtu = l2cap_mtu;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300711 l2cap_pi(sk)->chan->sec_level = sec_level;
Marcel Holtmanneae38ee2009-10-05 12:23:48 +0200712 if (l2cap_ertm)
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300713 l2cap_pi(sk)->chan->mode = L2CAP_MODE_ERTM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 release_sock(sk);
715
716 s = rfcomm_session_add(sock, BT_BOUND);
717 if (!s) {
718 *err = -ENOMEM;
719 goto failed;
720 }
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 s->initiator = 1;
723
724 bacpy(&addr.l2_bdaddr, dst);
725 addr.l2_family = AF_BLUETOOTH;
Marcel Holtmannb4324b52009-06-07 18:06:51 +0200726 addr.l2_psm = cpu_to_le16(RFCOMM_PSM);
Marcel Holtmann37e62f52009-02-17 21:49:33 +0100727 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +0200728 *err = kernel_connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK);
Marcel Holtmannb4c612a2006-09-23 09:54:38 +0200729 if (*err == 0 || *err == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return s;
731
732 rfcomm_session_del(s);
733 return NULL;
734
735failed:
736 sock_release(sock);
737 return NULL;
738}
739
740void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst)
741{
742 struct sock *sk = s->sock->sk;
743 if (src)
744 bacpy(src, &bt_sk(sk)->src);
745 if (dst)
746 bacpy(dst, &bt_sk(sk)->dst);
747}
748
749/* ---- RFCOMM frame sending ---- */
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200750static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct kvec iv = { data, len };
753 struct msghdr msg;
754
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200755 BT_DBG("session %p len %d", s, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 memset(&msg, 0, sizeof(msg));
758
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200759 return kernel_sendmsg(s->sock, &msg, &iv, 1, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200762static int rfcomm_send_cmd(struct rfcomm_session *s, struct rfcomm_cmd *cmd)
763{
764 BT_DBG("%p cmd %u", s, cmd->ctrl);
765
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200766 return rfcomm_send_frame(s, (void *) cmd, sizeof(*cmd));
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci)
770{
771 struct rfcomm_cmd cmd;
772
773 BT_DBG("%p dlci %d", s, dlci);
774
775 cmd.addr = __addr(s->initiator, dlci);
776 cmd.ctrl = __ctrl(RFCOMM_SABM, 1);
777 cmd.len = __len8(0);
778 cmd.fcs = __fcs2((u8 *) &cmd);
779
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200780 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783static int rfcomm_send_ua(struct rfcomm_session *s, u8 dlci)
784{
785 struct rfcomm_cmd cmd;
786
787 BT_DBG("%p dlci %d", s, dlci);
788
789 cmd.addr = __addr(!s->initiator, dlci);
790 cmd.ctrl = __ctrl(RFCOMM_UA, 1);
791 cmd.len = __len8(0);
792 cmd.fcs = __fcs2((u8 *) &cmd);
793
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200794 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci)
798{
799 struct rfcomm_cmd cmd;
800
801 BT_DBG("%p dlci %d", s, dlci);
802
803 cmd.addr = __addr(s->initiator, dlci);
804 cmd.ctrl = __ctrl(RFCOMM_DISC, 1);
805 cmd.len = __len8(0);
806 cmd.fcs = __fcs2((u8 *) &cmd);
807
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200808 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811static int rfcomm_queue_disc(struct rfcomm_dlc *d)
812{
813 struct rfcomm_cmd *cmd;
814 struct sk_buff *skb;
815
816 BT_DBG("dlc %p dlci %d", d, d->dlci);
817
818 skb = alloc_skb(sizeof(*cmd), GFP_KERNEL);
819 if (!skb)
820 return -ENOMEM;
821
822 cmd = (void *) __skb_put(skb, sizeof(*cmd));
823 cmd->addr = d->addr;
824 cmd->ctrl = __ctrl(RFCOMM_DISC, 1);
825 cmd->len = __len8(0);
826 cmd->fcs = __fcs2((u8 *) cmd);
827
828 skb_queue_tail(&d->tx_queue, skb);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300829 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return 0;
831}
832
833static int rfcomm_send_dm(struct rfcomm_session *s, u8 dlci)
834{
835 struct rfcomm_cmd cmd;
836
837 BT_DBG("%p dlci %d", s, dlci);
838
839 cmd.addr = __addr(!s->initiator, dlci);
840 cmd.ctrl = __ctrl(RFCOMM_DM, 1);
841 cmd.len = __len8(0);
842 cmd.fcs = __fcs2((u8 *) &cmd);
843
Luiz Augusto von Dentz262038f2011-11-01 10:58:58 +0200844 return rfcomm_send_cmd(s, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type)
848{
849 struct rfcomm_hdr *hdr;
850 struct rfcomm_mcc *mcc;
851 u8 buf[16], *ptr = buf;
852
853 BT_DBG("%p cr %d type %d", s, cr, type);
854
855 hdr = (void *) ptr; ptr += sizeof(*hdr);
856 hdr->addr = __addr(s->initiator, 0);
857 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
858 hdr->len = __len8(sizeof(*mcc) + 1);
859
860 mcc = (void *) ptr; ptr += sizeof(*mcc);
861 mcc->type = __mcc_type(cr, RFCOMM_NSC);
862 mcc->len = __len8(1);
863
864 /* Type that we didn't like */
865 *ptr = __mcc_type(cr, type); ptr++;
866
867 *ptr = __fcs(buf); ptr++;
868
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200869 return rfcomm_send_frame(s, buf, ptr - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d)
873{
874 struct rfcomm_hdr *hdr;
875 struct rfcomm_mcc *mcc;
876 struct rfcomm_pn *pn;
877 u8 buf[16], *ptr = buf;
878
879 BT_DBG("%p cr %d dlci %d mtu %d", s, cr, d->dlci, d->mtu);
880
881 hdr = (void *) ptr; ptr += sizeof(*hdr);
882 hdr->addr = __addr(s->initiator, 0);
883 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
884 hdr->len = __len8(sizeof(*mcc) + sizeof(*pn));
885
886 mcc = (void *) ptr; ptr += sizeof(*mcc);
887 mcc->type = __mcc_type(cr, RFCOMM_PN);
888 mcc->len = __len8(sizeof(*pn));
889
890 pn = (void *) ptr; ptr += sizeof(*pn);
891 pn->dlci = d->dlci;
892 pn->priority = d->priority;
893 pn->ack_timer = 0;
894 pn->max_retrans = 0;
895
896 if (s->cfc) {
897 pn->flow_ctrl = cr ? 0xf0 : 0xe0;
898 pn->credits = RFCOMM_DEFAULT_CREDITS;
899 } else {
900 pn->flow_ctrl = 0;
901 pn->credits = 0;
902 }
903
Marcel Holtmann98bcd082006-07-14 11:42:12 +0200904 if (cr && channel_mtu >= 0)
Marcel Holtmannb4324b52009-06-07 18:06:51 +0200905 pn->mtu = cpu_to_le16(channel_mtu);
Marcel Holtmann98bcd082006-07-14 11:42:12 +0200906 else
Marcel Holtmannb4324b52009-06-07 18:06:51 +0200907 pn->mtu = cpu_to_le16(d->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 *ptr = __fcs(buf); ptr++;
910
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200911 return rfcomm_send_frame(s, buf, ptr - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
J. Suter3a5e9032005-08-09 20:28:46 -0700914int rfcomm_send_rpn(struct rfcomm_session *s, int cr, u8 dlci,
915 u8 bit_rate, u8 data_bits, u8 stop_bits,
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900916 u8 parity, u8 flow_ctrl_settings,
J. Suter3a5e9032005-08-09 20:28:46 -0700917 u8 xon_char, u8 xoff_char, u16 param_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 struct rfcomm_hdr *hdr;
920 struct rfcomm_mcc *mcc;
921 struct rfcomm_rpn *rpn;
922 u8 buf[16], *ptr = buf;
923
924 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 +0900925 " flwc_s 0x%x xon_c 0x%x xoff_c 0x%x p_mask 0x%x",
926 s, cr, dlci, bit_rate, data_bits, stop_bits, parity,
J. Suter3a5e9032005-08-09 20:28:46 -0700927 flow_ctrl_settings, xon_char, xoff_char, param_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 hdr = (void *) ptr; ptr += sizeof(*hdr);
930 hdr->addr = __addr(s->initiator, 0);
931 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
932 hdr->len = __len8(sizeof(*mcc) + sizeof(*rpn));
933
934 mcc = (void *) ptr; ptr += sizeof(*mcc);
935 mcc->type = __mcc_type(cr, RFCOMM_RPN);
936 mcc->len = __len8(sizeof(*rpn));
937
938 rpn = (void *) ptr; ptr += sizeof(*rpn);
939 rpn->dlci = __addr(1, dlci);
940 rpn->bit_rate = bit_rate;
941 rpn->line_settings = __rpn_line_settings(data_bits, stop_bits, parity);
942 rpn->flow_ctrl = flow_ctrl_settings;
943 rpn->xon_char = xon_char;
944 rpn->xoff_char = xoff_char;
Al Viroe8db8c92006-11-08 00:28:44 -0800945 rpn->param_mask = cpu_to_le16(param_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 *ptr = __fcs(buf); ptr++;
948
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200949 return rfcomm_send_frame(s, buf, ptr - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952static int rfcomm_send_rls(struct rfcomm_session *s, int cr, u8 dlci, u8 status)
953{
954 struct rfcomm_hdr *hdr;
955 struct rfcomm_mcc *mcc;
956 struct rfcomm_rls *rls;
957 u8 buf[16], *ptr = buf;
958
959 BT_DBG("%p cr %d status 0x%x", s, cr, status);
960
961 hdr = (void *) ptr; ptr += sizeof(*hdr);
962 hdr->addr = __addr(s->initiator, 0);
963 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
964 hdr->len = __len8(sizeof(*mcc) + sizeof(*rls));
965
966 mcc = (void *) ptr; ptr += sizeof(*mcc);
967 mcc->type = __mcc_type(cr, RFCOMM_RLS);
968 mcc->len = __len8(sizeof(*rls));
969
970 rls = (void *) ptr; ptr += sizeof(*rls);
971 rls->dlci = __addr(1, dlci);
972 rls->status = status;
973
974 *ptr = __fcs(buf); ptr++;
975
Gustavo F. Padovan54365382011-12-20 16:30:44 -0200976 return rfcomm_send_frame(s, buf, ptr - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig)
980{
981 struct rfcomm_hdr *hdr;
982 struct rfcomm_mcc *mcc;
983 struct rfcomm_msc *msc;
984 u8 buf[16], *ptr = buf;
985
986 BT_DBG("%p cr %d v24 0x%x", s, cr, v24_sig);
987
988 hdr = (void *) ptr; ptr += sizeof(*hdr);
989 hdr->addr = __addr(s->initiator, 0);
990 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
991 hdr->len = __len8(sizeof(*mcc) + sizeof(*msc));
992
993 mcc = (void *) ptr; ptr += sizeof(*mcc);
994 mcc->type = __mcc_type(cr, RFCOMM_MSC);
995 mcc->len = __len8(sizeof(*msc));
996
997 msc = (void *) ptr; ptr += sizeof(*msc);
998 msc->dlci = __addr(1, dlci);
999 msc->v24_sig = v24_sig | 0x01;
1000
1001 *ptr = __fcs(buf); ptr++;
1002
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001003 return rfcomm_send_frame(s, buf, ptr - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006static int rfcomm_send_fcoff(struct rfcomm_session *s, int cr)
1007{
1008 struct rfcomm_hdr *hdr;
1009 struct rfcomm_mcc *mcc;
1010 u8 buf[16], *ptr = buf;
1011
1012 BT_DBG("%p cr %d", s, cr);
1013
1014 hdr = (void *) ptr; ptr += sizeof(*hdr);
1015 hdr->addr = __addr(s->initiator, 0);
1016 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1017 hdr->len = __len8(sizeof(*mcc));
1018
1019 mcc = (void *) ptr; ptr += sizeof(*mcc);
1020 mcc->type = __mcc_type(cr, RFCOMM_FCOFF);
1021 mcc->len = __len8(0);
1022
1023 *ptr = __fcs(buf); ptr++;
1024
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001025 return rfcomm_send_frame(s, buf, ptr - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028static int rfcomm_send_fcon(struct rfcomm_session *s, int cr)
1029{
1030 struct rfcomm_hdr *hdr;
1031 struct rfcomm_mcc *mcc;
1032 u8 buf[16], *ptr = buf;
1033
1034 BT_DBG("%p cr %d", s, cr);
1035
1036 hdr = (void *) ptr; ptr += sizeof(*hdr);
1037 hdr->addr = __addr(s->initiator, 0);
1038 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1039 hdr->len = __len8(sizeof(*mcc));
1040
1041 mcc = (void *) ptr; ptr += sizeof(*mcc);
1042 mcc->type = __mcc_type(cr, RFCOMM_FCON);
1043 mcc->len = __len8(0);
1044
1045 *ptr = __fcs(buf); ptr++;
1046
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001047 return rfcomm_send_frame(s, buf, ptr - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len)
1051{
1052 struct socket *sock = s->sock;
1053 struct kvec iv[3];
1054 struct msghdr msg;
1055 unsigned char hdr[5], crc[1];
1056
1057 if (len > 125)
1058 return -EINVAL;
1059
1060 BT_DBG("%p cr %d", s, cr);
1061
1062 hdr[0] = __addr(s->initiator, 0);
1063 hdr[1] = __ctrl(RFCOMM_UIH, 0);
1064 hdr[2] = 0x01 | ((len + 2) << 1);
1065 hdr[3] = 0x01 | ((cr & 0x01) << 1) | (RFCOMM_TEST << 2);
1066 hdr[4] = 0x01 | (len << 1);
1067
1068 crc[0] = __fcs(hdr);
1069
1070 iv[0].iov_base = hdr;
1071 iv[0].iov_len = 5;
1072 iv[1].iov_base = pattern;
1073 iv[1].iov_len = len;
1074 iv[2].iov_base = crc;
1075 iv[2].iov_len = 1;
1076
1077 memset(&msg, 0, sizeof(msg));
1078
1079 return kernel_sendmsg(sock, &msg, iv, 3, 6 + len);
1080}
1081
1082static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits)
1083{
1084 struct rfcomm_hdr *hdr;
1085 u8 buf[16], *ptr = buf;
1086
1087 BT_DBG("%p addr %d credits %d", s, addr, credits);
1088
1089 hdr = (void *) ptr; ptr += sizeof(*hdr);
1090 hdr->addr = addr;
1091 hdr->ctrl = __ctrl(RFCOMM_UIH, 1);
1092 hdr->len = __len8(0);
1093
1094 *ptr = credits; ptr++;
1095
1096 *ptr = __fcs(buf); ptr++;
1097
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001098 return rfcomm_send_frame(s, buf, ptr - buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101static void rfcomm_make_uih(struct sk_buff *skb, u8 addr)
1102{
1103 struct rfcomm_hdr *hdr;
1104 int len = skb->len;
1105 u8 *crc;
1106
1107 if (len > 127) {
1108 hdr = (void *) skb_push(skb, 4);
Marcel Holtmannb4324b52009-06-07 18:06:51 +02001109 put_unaligned(cpu_to_le16(__len16(len)), (__le16 *) &hdr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 } else {
1111 hdr = (void *) skb_push(skb, 3);
1112 hdr->len = __len8(len);
1113 }
1114 hdr->addr = addr;
1115 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1116
1117 crc = skb_put(skb, 1);
1118 *crc = __fcs((void *) hdr);
1119}
1120
1121/* ---- RFCOMM frame reception ---- */
1122static int rfcomm_recv_ua(struct rfcomm_session *s, u8 dlci)
1123{
1124 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1125
1126 if (dlci) {
1127 /* Data channel */
1128 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1129 if (!d) {
1130 rfcomm_send_dm(s, dlci);
1131 return 0;
1132 }
1133
1134 switch (d->state) {
1135 case BT_CONNECT:
1136 rfcomm_dlc_clear_timer(d);
1137
1138 rfcomm_dlc_lock(d);
1139 d->state = BT_CONNECTED;
1140 d->state_change(d, 0);
1141 rfcomm_dlc_unlock(d);
1142
1143 rfcomm_send_msc(s, 1, dlci, d->v24_sig);
1144 break;
1145
1146 case BT_DISCONN:
1147 d->state = BT_CLOSED;
1148 __rfcomm_dlc_close(d, 0);
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001149
1150 if (list_empty(&s->dlcs)) {
1151 s->state = BT_DISCONN;
1152 rfcomm_send_disc(s, 0);
Mat Martineau79e65472011-12-06 16:23:26 -08001153 rfcomm_session_clear_timer(s);
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001154 }
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 break;
1157 }
1158 } else {
1159 /* Control channel */
1160 switch (s->state) {
1161 case BT_CONNECT:
1162 s->state = BT_CONNECTED;
1163 rfcomm_process_connect(s);
1164 break;
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001165
1166 case BT_DISCONN:
Nick Pelly6c2718d2010-02-03 16:18:36 -08001167 /* When socket is closed and we are not RFCOMM
1168 * initiator rfcomm_process_rx already calls
1169 * rfcomm_session_put() */
1170 if (s->sock->sk->sk_state != BT_CLOSED)
Lukáš Turek683d9492011-01-05 02:43:59 +01001171 if (list_empty(&s->dlcs))
1172 rfcomm_session_put(s);
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175 }
1176 return 0;
1177}
1178
1179static int rfcomm_recv_dm(struct rfcomm_session *s, u8 dlci)
1180{
1181 int err = 0;
1182
1183 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1184
1185 if (dlci) {
1186 /* Data DLC */
1187 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1188 if (d) {
1189 if (d->state == BT_CONNECT || d->state == BT_CONFIG)
1190 err = ECONNREFUSED;
1191 else
1192 err = ECONNRESET;
1193
1194 d->state = BT_CLOSED;
1195 __rfcomm_dlc_close(d, err);
1196 }
1197 } else {
1198 if (s->state == BT_CONNECT)
1199 err = ECONNREFUSED;
1200 else
1201 err = ECONNRESET;
1202
1203 s->state = BT_CLOSED;
1204 rfcomm_session_close(s, err);
1205 }
1206 return 0;
1207}
1208
1209static int rfcomm_recv_disc(struct rfcomm_session *s, u8 dlci)
1210{
1211 int err = 0;
1212
1213 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1214
1215 if (dlci) {
1216 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1217 if (d) {
1218 rfcomm_send_ua(s, dlci);
1219
1220 if (d->state == BT_CONNECT || d->state == BT_CONFIG)
1221 err = ECONNREFUSED;
1222 else
1223 err = ECONNRESET;
1224
1225 d->state = BT_CLOSED;
1226 __rfcomm_dlc_close(d, err);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001227 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 rfcomm_send_dm(s, dlci);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 } else {
1231 rfcomm_send_ua(s, 0);
1232
1233 if (s->state == BT_CONNECT)
1234 err = ECONNREFUSED;
1235 else
1236 err = ECONNRESET;
1237
1238 s->state = BT_CLOSED;
1239 rfcomm_session_close(s, err);
1240 }
1241
1242 return 0;
1243}
1244
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001245void rfcomm_dlc_accept(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Marcel Holtmann300b9392006-07-03 10:37:55 +02001247 struct sock *sk = d->session->sock->sk;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001248 struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
Marcel Holtmann300b9392006-07-03 10:37:55 +02001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 BT_DBG("dlc %p", d);
1251
1252 rfcomm_send_ua(d->session, d->dlci);
1253
Johan Hedberge2139b32009-03-26 16:41:56 +02001254 rfcomm_dlc_clear_timer(d);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 rfcomm_dlc_lock(d);
1257 d->state = BT_CONNECTED;
1258 d->state_change(d, 0);
1259 rfcomm_dlc_unlock(d);
1260
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001261 if (d->role_switch)
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001262 hci_conn_switch_role(conn->hcon, 0x00);
Marcel Holtmann300b9392006-07-03 10:37:55 +02001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig);
1265}
1266
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001267static void rfcomm_check_accept(struct rfcomm_dlc *d)
1268{
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001269 if (rfcomm_check_security(d)) {
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001270 if (d->defer_setup) {
1271 set_bit(RFCOMM_DEFER_SETUP, &d->flags);
1272 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8bf47942009-02-16 02:59:49 +01001273
1274 rfcomm_dlc_lock(d);
1275 d->state = BT_CONNECT2;
1276 d->state_change(d, 0);
1277 rfcomm_dlc_unlock(d);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001278 } else
1279 rfcomm_dlc_accept(d);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001280 } else {
1281 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
1282 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001283 }
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286static int rfcomm_recv_sabm(struct rfcomm_session *s, u8 dlci)
1287{
1288 struct rfcomm_dlc *d;
1289 u8 channel;
1290
1291 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1292
1293 if (!dlci) {
1294 rfcomm_send_ua(s, 0);
1295
1296 if (s->state == BT_OPEN) {
1297 s->state = BT_CONNECTED;
1298 rfcomm_process_connect(s);
1299 }
1300 return 0;
1301 }
1302
1303 /* Check if DLC exists */
1304 d = rfcomm_dlc_get(s, dlci);
1305 if (d) {
1306 if (d->state == BT_OPEN) {
1307 /* DLC was previously opened by PN request */
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001308 rfcomm_check_accept(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310 return 0;
1311 }
1312
1313 /* Notify socket layer about incoming connection */
1314 channel = __srv_channel(dlci);
1315 if (rfcomm_connect_ind(s, channel, &d)) {
1316 d->dlci = dlci;
1317 d->addr = __addr(s->initiator, dlci);
1318 rfcomm_dlc_link(s, d);
1319
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001320 rfcomm_check_accept(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 } else {
1322 rfcomm_send_dm(s, dlci);
1323 }
1324
1325 return 0;
1326}
1327
1328static int rfcomm_apply_pn(struct rfcomm_dlc *d, int cr, struct rfcomm_pn *pn)
1329{
1330 struct rfcomm_session *s = d->session;
1331
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001332 BT_DBG("dlc %p state %ld dlci %d mtu %d fc 0x%x credits %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 d, d->state, d->dlci, pn->mtu, pn->flow_ctrl, pn->credits);
1334
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001335 if ((pn->flow_ctrl == 0xf0 && s->cfc != RFCOMM_CFC_DISABLED) ||
1336 pn->flow_ctrl == 0xe0) {
1337 d->cfc = RFCOMM_CFC_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 d->tx_credits = pn->credits;
1339 } else {
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001340 d->cfc = RFCOMM_CFC_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1342 }
1343
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001344 if (s->cfc == RFCOMM_CFC_UNKNOWN)
1345 s->cfc = d->cfc;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 d->priority = pn->priority;
1348
Marcel Holtmannb4324b52009-06-07 18:06:51 +02001349 d->mtu = __le16_to_cpu(pn->mtu);
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001350
1351 if (cr && d->mtu > s->mtu)
1352 d->mtu = s->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 return 0;
1355}
1356
1357static int rfcomm_recv_pn(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1358{
1359 struct rfcomm_pn *pn = (void *) skb->data;
1360 struct rfcomm_dlc *d;
1361 u8 dlci = pn->dlci;
1362
1363 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1364
1365 if (!dlci)
1366 return 0;
1367
1368 d = rfcomm_dlc_get(s, dlci);
1369 if (d) {
1370 if (cr) {
1371 /* PN request */
1372 rfcomm_apply_pn(d, cr, pn);
1373 rfcomm_send_pn(s, 0, d);
1374 } else {
1375 /* PN response */
1376 switch (d->state) {
1377 case BT_CONFIG:
1378 rfcomm_apply_pn(d, cr, pn);
1379
1380 d->state = BT_CONNECT;
1381 rfcomm_send_sabm(s, d->dlci);
1382 break;
1383 }
1384 }
1385 } else {
1386 u8 channel = __srv_channel(dlci);
1387
1388 if (!cr)
1389 return 0;
1390
1391 /* PN request for non existing DLC.
1392 * Assume incoming connection. */
1393 if (rfcomm_connect_ind(s, channel, &d)) {
1394 d->dlci = dlci;
1395 d->addr = __addr(s->initiator, dlci);
1396 rfcomm_dlc_link(s, d);
1397
1398 rfcomm_apply_pn(d, cr, pn);
1399
1400 d->state = BT_OPEN;
1401 rfcomm_send_pn(s, 0, d);
1402 } else {
1403 rfcomm_send_dm(s, dlci);
1404 }
1405 }
1406 return 0;
1407}
1408
1409static int rfcomm_recv_rpn(struct rfcomm_session *s, int cr, int len, struct sk_buff *skb)
1410{
1411 struct rfcomm_rpn *rpn = (void *) skb->data;
1412 u8 dlci = __get_dlci(rpn->dlci);
1413
1414 u8 bit_rate = 0;
1415 u8 data_bits = 0;
1416 u8 stop_bits = 0;
1417 u8 parity = 0;
1418 u8 flow_ctrl = 0;
1419 u8 xon_char = 0;
1420 u8 xoff_char = 0;
1421 u16 rpn_mask = RFCOMM_RPN_PM_ALL;
J. Suter3a5e9032005-08-09 20:28:46 -07001422
1423 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",
1424 dlci, cr, len, rpn->bit_rate, rpn->line_settings, rpn->flow_ctrl,
1425 rpn->xon_char, rpn->xoff_char, rpn->param_mask);
1426
1427 if (!cr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
J. Suter3a5e9032005-08-09 20:28:46 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (len == 1) {
Yuri Kululin08601462010-07-23 13:57:12 +04001431 /* This is a request, return default (according to ETSI TS 07.10) settings */
1432 bit_rate = RFCOMM_RPN_BR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 data_bits = RFCOMM_RPN_DATA_8;
1434 stop_bits = RFCOMM_RPN_STOP_1;
1435 parity = RFCOMM_RPN_PARITY_NONE;
1436 flow_ctrl = RFCOMM_RPN_FLOW_NONE;
1437 xon_char = RFCOMM_RPN_XON_CHAR;
1438 xoff_char = RFCOMM_RPN_XOFF_CHAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 goto rpn_out;
1440 }
J. Suter3a5e9032005-08-09 20:28:46 -07001441
1442 /* Check for sane values, ignore/accept bit_rate, 8 bits, 1 stop bit,
1443 * no parity, no flow control lines, normal XON/XOFF chars */
1444
Al Viroe8db8c92006-11-08 00:28:44 -08001445 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_BITRATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 bit_rate = rpn->bit_rate;
Yuri Kululin08601462010-07-23 13:57:12 +04001447 if (bit_rate > RFCOMM_RPN_BR_230400) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 BT_DBG("RPN bit rate mismatch 0x%x", bit_rate);
Yuri Kululin08601462010-07-23 13:57:12 +04001449 bit_rate = RFCOMM_RPN_BR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 rpn_mask ^= RFCOMM_RPN_PM_BITRATE;
1451 }
1452 }
J. Suter3a5e9032005-08-09 20:28:46 -07001453
Al Viroe8db8c92006-11-08 00:28:44 -08001454 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 data_bits = __get_rpn_data_bits(rpn->line_settings);
1456 if (data_bits != RFCOMM_RPN_DATA_8) {
1457 BT_DBG("RPN data bits mismatch 0x%x", data_bits);
1458 data_bits = RFCOMM_RPN_DATA_8;
1459 rpn_mask ^= RFCOMM_RPN_PM_DATA;
1460 }
1461 }
J. Suter3a5e9032005-08-09 20:28:46 -07001462
Al Viroe8db8c92006-11-08 00:28:44 -08001463 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_STOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 stop_bits = __get_rpn_stop_bits(rpn->line_settings);
1465 if (stop_bits != RFCOMM_RPN_STOP_1) {
1466 BT_DBG("RPN stop bits mismatch 0x%x", stop_bits);
1467 stop_bits = RFCOMM_RPN_STOP_1;
1468 rpn_mask ^= RFCOMM_RPN_PM_STOP;
1469 }
1470 }
J. Suter3a5e9032005-08-09 20:28:46 -07001471
Al Viroe8db8c92006-11-08 00:28:44 -08001472 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_PARITY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 parity = __get_rpn_parity(rpn->line_settings);
1474 if (parity != RFCOMM_RPN_PARITY_NONE) {
1475 BT_DBG("RPN parity mismatch 0x%x", parity);
1476 parity = RFCOMM_RPN_PARITY_NONE;
1477 rpn_mask ^= RFCOMM_RPN_PM_PARITY;
1478 }
1479 }
J. Suter3a5e9032005-08-09 20:28:46 -07001480
Al Viroe8db8c92006-11-08 00:28:44 -08001481 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_FLOW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 flow_ctrl = rpn->flow_ctrl;
1483 if (flow_ctrl != RFCOMM_RPN_FLOW_NONE) {
1484 BT_DBG("RPN flow ctrl mismatch 0x%x", flow_ctrl);
1485 flow_ctrl = RFCOMM_RPN_FLOW_NONE;
1486 rpn_mask ^= RFCOMM_RPN_PM_FLOW;
1487 }
1488 }
J. Suter3a5e9032005-08-09 20:28:46 -07001489
Al Viroe8db8c92006-11-08 00:28:44 -08001490 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 xon_char = rpn->xon_char;
1492 if (xon_char != RFCOMM_RPN_XON_CHAR) {
1493 BT_DBG("RPN XON char mismatch 0x%x", xon_char);
1494 xon_char = RFCOMM_RPN_XON_CHAR;
1495 rpn_mask ^= RFCOMM_RPN_PM_XON;
1496 }
1497 }
J. Suter3a5e9032005-08-09 20:28:46 -07001498
Al Viroe8db8c92006-11-08 00:28:44 -08001499 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XOFF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 xoff_char = rpn->xoff_char;
1501 if (xoff_char != RFCOMM_RPN_XOFF_CHAR) {
1502 BT_DBG("RPN XOFF char mismatch 0x%x", xoff_char);
1503 xoff_char = RFCOMM_RPN_XOFF_CHAR;
1504 rpn_mask ^= RFCOMM_RPN_PM_XOFF;
1505 }
1506 }
1507
1508rpn_out:
J. Suter3a5e9032005-08-09 20:28:46 -07001509 rfcomm_send_rpn(s, 0, dlci, bit_rate, data_bits, stop_bits,
1510 parity, flow_ctrl, xon_char, xoff_char, rpn_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 return 0;
1513}
1514
1515static int rfcomm_recv_rls(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1516{
1517 struct rfcomm_rls *rls = (void *) skb->data;
1518 u8 dlci = __get_dlci(rls->dlci);
1519
1520 BT_DBG("dlci %d cr %d status 0x%x", dlci, cr, rls->status);
J. Suter3a5e9032005-08-09 20:28:46 -07001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (!cr)
1523 return 0;
1524
J. Suter3a5e9032005-08-09 20:28:46 -07001525 /* We should probably do something with this information here. But
1526 * for now it's sufficient just to reply -- Bluetooth 1.1 says it's
1527 * mandatory to recognise and respond to RLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 rfcomm_send_rls(s, 0, dlci, rls->status);
1530
1531 return 0;
1532}
1533
1534static int rfcomm_recv_msc(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1535{
1536 struct rfcomm_msc *msc = (void *) skb->data;
1537 struct rfcomm_dlc *d;
1538 u8 dlci = __get_dlci(msc->dlci);
1539
1540 BT_DBG("dlci %d cr %d v24 0x%x", dlci, cr, msc->v24_sig);
1541
1542 d = rfcomm_dlc_get(s, dlci);
J. Suter3a5e9032005-08-09 20:28:46 -07001543 if (!d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return 0;
1545
1546 if (cr) {
1547 if (msc->v24_sig & RFCOMM_V24_FC && !d->cfc)
1548 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1549 else
1550 clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
J. Suter3a5e9032005-08-09 20:28:46 -07001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 rfcomm_dlc_lock(d);
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +02001553
1554 d->remote_v24_sig = msc->v24_sig;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (d->modem_status)
1557 d->modem_status(d, msc->v24_sig);
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +02001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 rfcomm_dlc_unlock(d);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 rfcomm_send_msc(s, 0, dlci, msc->v24_sig);
1562
1563 d->mscex |= RFCOMM_MSCEX_RX;
J. Suter3a5e9032005-08-09 20:28:46 -07001564 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 d->mscex |= RFCOMM_MSCEX_TX;
1566
1567 return 0;
1568}
1569
1570static int rfcomm_recv_mcc(struct rfcomm_session *s, struct sk_buff *skb)
1571{
1572 struct rfcomm_mcc *mcc = (void *) skb->data;
1573 u8 type, cr, len;
1574
1575 cr = __test_cr(mcc->type);
1576 type = __get_mcc_type(mcc->type);
1577 len = __get_mcc_len(mcc->len);
1578
1579 BT_DBG("%p type 0x%x cr %d", s, type, cr);
1580
1581 skb_pull(skb, 2);
1582
1583 switch (type) {
1584 case RFCOMM_PN:
1585 rfcomm_recv_pn(s, cr, skb);
1586 break;
1587
1588 case RFCOMM_RPN:
1589 rfcomm_recv_rpn(s, cr, len, skb);
1590 break;
1591
1592 case RFCOMM_RLS:
1593 rfcomm_recv_rls(s, cr, skb);
1594 break;
1595
1596 case RFCOMM_MSC:
1597 rfcomm_recv_msc(s, cr, skb);
1598 break;
1599
1600 case RFCOMM_FCOFF:
1601 if (cr) {
1602 set_bit(RFCOMM_TX_THROTTLED, &s->flags);
1603 rfcomm_send_fcoff(s, 0);
1604 }
1605 break;
1606
1607 case RFCOMM_FCON:
1608 if (cr) {
1609 clear_bit(RFCOMM_TX_THROTTLED, &s->flags);
1610 rfcomm_send_fcon(s, 0);
1611 }
1612 break;
1613
1614 case RFCOMM_TEST:
1615 if (cr)
1616 rfcomm_send_test(s, 0, skb->data, skb->len);
1617 break;
1618
1619 case RFCOMM_NSC:
1620 break;
1621
1622 default:
1623 BT_ERR("Unknown control type 0x%02x", type);
1624 rfcomm_send_nsc(s, cr, type);
1625 break;
1626 }
1627 return 0;
1628}
1629
1630static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk_buff *skb)
1631{
1632 struct rfcomm_dlc *d;
1633
1634 BT_DBG("session %p state %ld dlci %d pf %d", s, s->state, dlci, pf);
1635
1636 d = rfcomm_dlc_get(s, dlci);
1637 if (!d) {
1638 rfcomm_send_dm(s, dlci);
1639 goto drop;
1640 }
1641
1642 if (pf && d->cfc) {
1643 u8 credits = *(u8 *) skb->data; skb_pull(skb, 1);
1644
1645 d->tx_credits += credits;
1646 if (d->tx_credits)
1647 clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
1648 }
1649
1650 if (skb->len && d->state == BT_CONNECTED) {
1651 rfcomm_dlc_lock(d);
1652 d->rx_credits--;
1653 d->data_ready(d, skb);
1654 rfcomm_dlc_unlock(d);
1655 return 0;
1656 }
1657
1658drop:
1659 kfree_skb(skb);
1660 return 0;
1661}
1662
1663static int rfcomm_recv_frame(struct rfcomm_session *s, struct sk_buff *skb)
1664{
1665 struct rfcomm_hdr *hdr = (void *) skb->data;
1666 u8 type, dlci, fcs;
1667
1668 dlci = __get_dlci(hdr->addr);
1669 type = __get_type(hdr->ctrl);
1670
1671 /* Trim FCS */
1672 skb->len--; skb->tail--;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001673 fcs = *(u8 *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 if (__check_fcs(skb->data, type, fcs)) {
1676 BT_ERR("bad checksum in packet");
1677 kfree_skb(skb);
1678 return -EILSEQ;
1679 }
1680
1681 if (__test_ea(hdr->len))
1682 skb_pull(skb, 3);
1683 else
1684 skb_pull(skb, 4);
1685
1686 switch (type) {
1687 case RFCOMM_SABM:
1688 if (__test_pf(hdr->ctrl))
1689 rfcomm_recv_sabm(s, dlci);
1690 break;
1691
1692 case RFCOMM_DISC:
1693 if (__test_pf(hdr->ctrl))
1694 rfcomm_recv_disc(s, dlci);
1695 break;
1696
1697 case RFCOMM_UA:
1698 if (__test_pf(hdr->ctrl))
1699 rfcomm_recv_ua(s, dlci);
1700 break;
1701
1702 case RFCOMM_DM:
1703 rfcomm_recv_dm(s, dlci);
1704 break;
1705
1706 case RFCOMM_UIH:
1707 if (dlci)
1708 return rfcomm_recv_data(s, dlci, __test_pf(hdr->ctrl), skb);
1709
1710 rfcomm_recv_mcc(s, skb);
1711 break;
1712
1713 default:
Andrei Emeltchenko5017d8d2010-09-08 16:26:53 +03001714 BT_ERR("Unknown packet type 0x%02x", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 break;
1716 }
1717 kfree_skb(skb);
1718 return 0;
1719}
1720
1721/* ---- Connection and data processing ---- */
1722
1723static void rfcomm_process_connect(struct rfcomm_session *s)
1724{
1725 struct rfcomm_dlc *d;
1726 struct list_head *p, *n;
1727
1728 BT_DBG("session %p state %ld", s, s->state);
1729
1730 list_for_each_safe(p, n, &s->dlcs) {
1731 d = list_entry(p, struct rfcomm_dlc, list);
1732 if (d->state == BT_CONFIG) {
1733 d->mtu = s->mtu;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001734 if (rfcomm_check_security(d)) {
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001735 rfcomm_send_pn(s, 1, d);
1736 } else {
Marcel Holtmann77db1982008-07-14 20:13:45 +02001737 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
1738 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741 }
1742}
1743
1744/* Send data queued for the DLC.
1745 * Return number of frames left in the queue.
1746 */
1747static inline int rfcomm_process_tx(struct rfcomm_dlc *d)
1748{
1749 struct sk_buff *skb;
1750 int err;
1751
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001752 BT_DBG("dlc %p state %ld cfc %d rx_credits %d tx_credits %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 d, d->state, d->cfc, d->rx_credits, d->tx_credits);
1754
1755 /* Send pending MSC */
1756 if (test_and_clear_bit(RFCOMM_MSC_PENDING, &d->flags))
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001757 rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 if (d->cfc) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001760 /* CFC enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 * Give them some credits */
1762 if (!test_bit(RFCOMM_RX_THROTTLED, &d->flags) &&
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001763 d->rx_credits <= (d->cfc >> 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 rfcomm_send_credits(d->session, d->addr, d->cfc - d->rx_credits);
1765 d->rx_credits = d->cfc;
1766 }
1767 } else {
1768 /* CFC disabled.
1769 * Give ourselves some credits */
1770 d->tx_credits = 5;
1771 }
1772
1773 if (test_bit(RFCOMM_TX_THROTTLED, &d->flags))
1774 return skb_queue_len(&d->tx_queue);
1775
1776 while (d->tx_credits && (skb = skb_dequeue(&d->tx_queue))) {
Gustavo F. Padovan54365382011-12-20 16:30:44 -02001777 err = rfcomm_send_frame(d->session, skb->data, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (err < 0) {
1779 skb_queue_head(&d->tx_queue, skb);
1780 break;
1781 }
1782 kfree_skb(skb);
1783 d->tx_credits--;
1784 }
1785
1786 if (d->cfc && !d->tx_credits) {
1787 /* We're out of TX credits.
1788 * Set TX_THROTTLED flag to avoid unnesary wakeups by dlc_send. */
1789 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1790 }
1791
1792 return skb_queue_len(&d->tx_queue);
1793}
1794
1795static inline void rfcomm_process_dlcs(struct rfcomm_session *s)
1796{
1797 struct rfcomm_dlc *d;
1798 struct list_head *p, *n;
1799
1800 BT_DBG("session %p state %ld", s, s->state);
1801
1802 list_for_each_safe(p, n, &s->dlcs) {
1803 d = list_entry(p, struct rfcomm_dlc, list);
1804
1805 if (test_bit(RFCOMM_TIMED_OUT, &d->flags)) {
1806 __rfcomm_dlc_close(d, ETIMEDOUT);
1807 continue;
1808 }
1809
Szymon Jancdb544672011-09-26 14:19:47 +02001810 if (test_bit(RFCOMM_ENC_DROP, &d->flags)) {
1811 __rfcomm_dlc_close(d, ECONNREFUSED);
1812 continue;
1813 }
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (test_and_clear_bit(RFCOMM_AUTH_ACCEPT, &d->flags)) {
1816 rfcomm_dlc_clear_timer(d);
Marcel Holtmann77db1982008-07-14 20:13:45 +02001817 if (d->out) {
1818 rfcomm_send_pn(s, 1, d);
1819 rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001820 } else {
1821 if (d->defer_setup) {
1822 set_bit(RFCOMM_DEFER_SETUP, &d->flags);
1823 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8bf47942009-02-16 02:59:49 +01001824
1825 rfcomm_dlc_lock(d);
1826 d->state = BT_CONNECT2;
1827 d->state_change(d, 0);
1828 rfcomm_dlc_unlock(d);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001829 } else
1830 rfcomm_dlc_accept(d);
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 continue;
1833 } else if (test_and_clear_bit(RFCOMM_AUTH_REJECT, &d->flags)) {
1834 rfcomm_dlc_clear_timer(d);
Marcel Holtmann77db1982008-07-14 20:13:45 +02001835 if (!d->out)
1836 rfcomm_send_dm(s, d->dlci);
1837 else
1838 d->state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 __rfcomm_dlc_close(d, ECONNREFUSED);
1840 continue;
1841 }
1842
Jaikumar Ganesh6e1031a2009-02-02 18:03:57 -08001843 if (test_bit(RFCOMM_SEC_PENDING, &d->flags))
1844 continue;
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (test_bit(RFCOMM_TX_THROTTLED, &s->flags))
1847 continue;
1848
1849 if ((d->state == BT_CONNECTED || d->state == BT_DISCONN) &&
Marcel Holtmann77db1982008-07-14 20:13:45 +02001850 d->mscex == RFCOMM_MSCEX_OK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 rfcomm_process_tx(d);
1852 }
1853}
1854
1855static inline void rfcomm_process_rx(struct rfcomm_session *s)
1856{
1857 struct socket *sock = s->sock;
1858 struct sock *sk = sock->sk;
1859 struct sk_buff *skb;
1860
1861 BT_DBG("session %p state %ld qlen %d", s, s->state, skb_queue_len(&sk->sk_receive_queue));
1862
1863 /* Get data directly from socket receive queue without copying it. */
1864 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
1865 skb_orphan(skb);
Mat Martineau44935722011-07-22 14:53:58 -07001866 if (!skb_linearize(skb))
1867 rfcomm_recv_frame(s, skb);
1868 else
1869 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871
1872 if (sk->sk_state == BT_CLOSED) {
1873 if (!s->initiator)
1874 rfcomm_session_put(s);
1875
1876 rfcomm_session_close(s, sk->sk_err);
1877 }
1878}
1879
1880static inline void rfcomm_accept_connection(struct rfcomm_session *s)
1881{
1882 struct socket *sock = s->sock, *nsock;
1883 int err;
1884
1885 /* Fast check for a new connection.
1886 * Avoids unnesesary socket allocations. */
1887 if (list_empty(&bt_sk(sock->sk)->accept_q))
1888 return;
1889
1890 BT_DBG("session %p", s);
1891
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02001892 err = kernel_accept(sock, &nsock, O_NONBLOCK);
1893 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return;
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /* Set our callbacks */
1897 nsock->sk->sk_data_ready = rfcomm_l2data_ready;
1898 nsock->sk->sk_state_change = rfcomm_l2state_change;
1899
1900 s = rfcomm_session_add(nsock, BT_OPEN);
1901 if (s) {
1902 rfcomm_session_hold(s);
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001903
1904 /* We should adjust MTU on incoming sessions.
1905 * L2CAP MTU minus UIH header and FCS. */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001906 s->mtu = min(l2cap_pi(nsock->sk)->chan->omtu,
1907 l2cap_pi(nsock->sk)->chan->imtu) - 5;
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001908
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +03001909 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 } else
1911 sock_release(nsock);
1912}
1913
1914static inline void rfcomm_check_connection(struct rfcomm_session *s)
1915{
1916 struct sock *sk = s->sock->sk;
1917
1918 BT_DBG("%p state %ld", s, s->state);
1919
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +02001920 switch (sk->sk_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 case BT_CONNECTED:
1922 s->state = BT_CONNECT;
1923
1924 /* We can adjust MTU on outgoing sessions.
1925 * L2CAP MTU minus UIH header and FCS. */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03001926 s->mtu = min(l2cap_pi(sk)->chan->omtu, l2cap_pi(sk)->chan->imtu) - 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 rfcomm_send_sabm(s, 0);
1929 break;
1930
1931 case BT_CLOSED:
1932 s->state = BT_CLOSED;
1933 rfcomm_session_close(s, sk->sk_err);
1934 break;
1935 }
1936}
1937
1938static inline void rfcomm_process_sessions(void)
1939{
1940 struct list_head *p, *n;
1941
1942 rfcomm_lock();
1943
1944 list_for_each_safe(p, n, &session_list) {
1945 struct rfcomm_session *s;
1946 s = list_entry(p, struct rfcomm_session, list);
1947
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -03001948 if (test_and_clear_bit(RFCOMM_TIMED_OUT, &s->flags)) {
1949 s->state = BT_DISCONN;
1950 rfcomm_send_disc(s, 0);
Marcel Holtmann485f1ef2010-02-03 15:52:18 -08001951 rfcomm_session_put(s);
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -03001952 continue;
1953 }
1954
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (s->state == BT_LISTEN) {
1956 rfcomm_accept_connection(s);
1957 continue;
1958 }
1959
1960 rfcomm_session_hold(s);
1961
1962 switch (s->state) {
1963 case BT_BOUND:
1964 rfcomm_check_connection(s);
1965 break;
1966
1967 default:
1968 rfcomm_process_rx(s);
1969 break;
1970 }
1971
1972 rfcomm_process_dlcs(s);
1973
1974 rfcomm_session_put(s);
1975 }
1976
1977 rfcomm_unlock();
1978}
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980static int rfcomm_add_listener(bdaddr_t *ba)
1981{
1982 struct sockaddr_l2 addr;
1983 struct socket *sock;
1984 struct sock *sk;
1985 struct rfcomm_session *s;
1986 int err = 0;
1987
1988 /* Create socket */
1989 err = rfcomm_l2sock_create(&sock);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001990 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 BT_ERR("Create socket failed %d", err);
1992 return err;
1993 }
1994
1995 /* Bind socket */
1996 bacpy(&addr.l2_bdaddr, ba);
1997 addr.l2_family = AF_BLUETOOTH;
Marcel Holtmannb4324b52009-06-07 18:06:51 +02001998 addr.l2_psm = cpu_to_le16(RFCOMM_PSM);
Marcel Holtmann37e62f52009-02-17 21:49:33 +01001999 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02002000 err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (err < 0) {
2002 BT_ERR("Bind failed %d", err);
2003 goto failed;
2004 }
2005
2006 /* Set L2CAP options */
2007 sk = sock->sk;
2008 lock_sock(sk);
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03002009 l2cap_pi(sk)->chan->imtu = l2cap_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 release_sock(sk);
2011
2012 /* Start listening on the socket */
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02002013 err = kernel_listen(sock, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (err) {
2015 BT_ERR("Listen failed %d", err);
2016 goto failed;
2017 }
2018
2019 /* Add listening session */
2020 s = rfcomm_session_add(sock, BT_LISTEN);
2021 if (!s)
2022 goto failed;
2023
2024 rfcomm_session_hold(s);
2025 return 0;
2026failed:
2027 sock_release(sock);
2028 return err;
2029}
2030
2031static void rfcomm_kill_listener(void)
2032{
2033 struct rfcomm_session *s;
2034 struct list_head *p, *n;
2035
2036 BT_DBG("");
2037
2038 list_for_each_safe(p, n, &session_list) {
2039 s = list_entry(p, struct rfcomm_session, list);
2040 rfcomm_session_del(s);
2041 }
2042}
2043
2044static int rfcomm_run(void *unused)
2045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 BT_DBG("");
2047
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002048 set_user_nice(current, -10);
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 rfcomm_add_listener(BDADDR_ANY);
2051
Peter Hurleye5842cd2011-07-24 00:10:35 -04002052 while (1) {
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002053 set_current_state(TASK_INTERRUPTIBLE);
Peter Hurleye5842cd2011-07-24 00:10:35 -04002054
2055 if (kthread_should_stop())
2056 break;
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002057
2058 /* Process stuff */
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002059 rfcomm_process_sessions();
Peter Hurleye5842cd2011-07-24 00:10:35 -04002060
2061 schedule();
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002062 }
Peter Hurleye5842cd2011-07-24 00:10:35 -04002063 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 rfcomm_kill_listener();
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return 0;
2068}
2069
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002070static void rfcomm_security_cfm(struct hci_conn *conn, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
2072 struct rfcomm_session *s;
2073 struct rfcomm_dlc *d;
2074 struct list_head *p, *n;
2075
2076 BT_DBG("conn %p status 0x%02x encrypt 0x%02x", conn, status, encrypt);
2077
2078 s = rfcomm_session_get(&conn->hdev->bdaddr, &conn->dst);
2079 if (!s)
2080 return;
2081
2082 rfcomm_session_hold(s);
2083
2084 list_for_each_safe(p, n, &s->dlcs) {
2085 d = list_entry(p, struct rfcomm_dlc, list);
2086
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002087 if (test_and_clear_bit(RFCOMM_SEC_PENDING, &d->flags)) {
2088 rfcomm_dlc_clear_timer(d);
2089 if (status || encrypt == 0x00) {
Szymon Jancdb544672011-09-26 14:19:47 +02002090 set_bit(RFCOMM_ENC_DROP, &d->flags);
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002091 continue;
2092 }
2093 }
2094
2095 if (d->state == BT_CONNECTED && !status && encrypt == 0x00) {
2096 if (d->sec_level == BT_SECURITY_MEDIUM) {
2097 set_bit(RFCOMM_SEC_PENDING, &d->flags);
2098 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
2099 continue;
2100 } else if (d->sec_level == BT_SECURITY_HIGH) {
Szymon Jancdb544672011-09-26 14:19:47 +02002101 set_bit(RFCOMM_ENC_DROP, &d->flags);
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002102 continue;
2103 }
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02002104 }
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (!test_and_clear_bit(RFCOMM_AUTH_PENDING, &d->flags))
2107 continue;
2108
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +02002109 if (!status && hci_conn_check_secure(conn, d->sec_level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 set_bit(RFCOMM_AUTH_ACCEPT, &d->flags);
2111 else
2112 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
2113 }
2114
2115 rfcomm_session_put(s);
2116
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +03002117 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120static struct hci_cb rfcomm_cb = {
2121 .name = "RFCOMM",
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002122 .security_cfm = rfcomm_security_cfm
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123};
2124
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002125static int rfcomm_dlc_debugfs_show(struct seq_file *f, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 struct rfcomm_session *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 rfcomm_lock();
2130
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002131 list_for_each_entry(s, &session_list, list) {
2132 struct rfcomm_dlc *d;
2133 list_for_each_entry(d, &s->dlcs, list) {
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002134 struct sock *sk = s->sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002136 seq_printf(f, "%s %s %ld %d %d %d %d\n",
2137 batostr(&bt_sk(sk)->src),
2138 batostr(&bt_sk(sk)->dst),
2139 d->state, d->dlci, d->mtu,
2140 d->rx_credits, d->tx_credits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 rfcomm_unlock();
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002145
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002149static int rfcomm_dlc_debugfs_open(struct inode *inode, struct file *file)
2150{
2151 return single_open(file, rfcomm_dlc_debugfs_show, inode->i_private);
2152}
2153
2154static const struct file_operations rfcomm_dlc_debugfs_fops = {
2155 .open = rfcomm_dlc_debugfs_open,
2156 .read = seq_read,
2157 .llseek = seq_lseek,
2158 .release = single_release,
2159};
2160
2161static struct dentry *rfcomm_dlc_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163/* ---- Initialization ---- */
2164static int __init rfcomm_init(void)
2165{
Marcel Holtmann52d18342009-08-22 14:49:36 -07002166 int err;
Dave Youngaf0d3b12009-08-03 04:26:16 +00002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 hci_register_cb(&rfcomm_cb);
2169
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002170 rfcomm_thread = kthread_run(rfcomm_run, NULL, "krfcommd");
2171 if (IS_ERR(rfcomm_thread)) {
Marcel Holtmann52d18342009-08-22 14:49:36 -07002172 err = PTR_ERR(rfcomm_thread);
2173 goto unregister;
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002176 if (bt_debugfs) {
2177 rfcomm_dlc_debugfs = debugfs_create_file("rfcomm_dlc", 0444,
2178 bt_debugfs, NULL, &rfcomm_dlc_debugfs_fops);
2179 if (!rfcomm_dlc_debugfs)
2180 BT_ERR("Failed to create RFCOMM debug file");
2181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Marcel Holtmann52d18342009-08-22 14:49:36 -07002183 err = rfcomm_init_ttys();
2184 if (err < 0)
2185 goto stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Marcel Holtmann52d18342009-08-22 14:49:36 -07002187 err = rfcomm_init_sockets();
2188 if (err < 0)
2189 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002191 BT_INFO("RFCOMM ver %s", VERSION);
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return 0;
Dave Youngaf0d3b12009-08-03 04:26:16 +00002194
Marcel Holtmann52d18342009-08-22 14:49:36 -07002195cleanup:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002196 rfcomm_cleanup_ttys();
Marcel Holtmann52d18342009-08-22 14:49:36 -07002197
2198stop:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002199 kthread_stop(rfcomm_thread);
Marcel Holtmann52d18342009-08-22 14:49:36 -07002200
2201unregister:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002202 hci_unregister_cb(&rfcomm_cb);
2203
Marcel Holtmann52d18342009-08-22 14:49:36 -07002204 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
2207static void __exit rfcomm_exit(void)
2208{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002209 debugfs_remove(rfcomm_dlc_debugfs);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 hci_unregister_cb(&rfcomm_cb);
2212
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002213 kthread_stop(rfcomm_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 rfcomm_cleanup_ttys();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 rfcomm_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
2220module_init(rfcomm_init);
2221module_exit(rfcomm_exit);
2222
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02002223module_param(disable_cfc, bool, 0644);
2224MODULE_PARM_DESC(disable_cfc, "Disable credit based flow control");
2225
Marcel Holtmann98bcd082006-07-14 11:42:12 +02002226module_param(channel_mtu, int, 0644);
2227MODULE_PARM_DESC(channel_mtu, "Default MTU for the RFCOMM channel");
2228
Marcel Holtmann56f3a402006-02-13 11:39:57 +01002229module_param(l2cap_mtu, uint, 0644);
2230MODULE_PARM_DESC(l2cap_mtu, "Default MTU for the L2CAP connection");
2231
Marcel Holtmanneae38ee2009-10-05 12:23:48 +02002232module_param(l2cap_ertm, bool, 0644);
2233MODULE_PARM_DESC(l2cap_ertm, "Use L2CAP ERTM mode for connection");
2234
Marcel Holtmann63fbd242008-08-18 13:23:53 +02002235MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236MODULE_DESCRIPTION("Bluetooth RFCOMM ver " VERSION);
2237MODULE_VERSION(VERSION);
2238MODULE_LICENSE("GPL");
2239MODULE_ALIAS("bt-proto-3");