blob: 8772578d56ecb757e960889b1b940864dc8730ed [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"
Mallikarjuna GB2da40bf2011-09-06 17:02:36 +053053/* 1 Byte DLCI, 1 Byte Control filed, 2 Bytes Length, 1 Byte for Credits,
54 * 1 Byte FCS */
55#define RFCOMM_HDR_SIZE 6
Marcel Holtmann56f3a402006-02-13 11:39:57 +010056
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +020057static int disable_cfc;
58static int l2cap_ertm;
Marcel Holtmann98bcd082006-07-14 11:42:12 +020059static int channel_mtu = -1;
Marcel Holtmann56f3a402006-02-13 11:39:57 +010060static unsigned int l2cap_mtu = RFCOMM_MAX_L2CAP_MTU;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static struct task_struct *rfcomm_thread;
63
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080064static DEFINE_MUTEX(rfcomm_mutex);
65#define rfcomm_lock() mutex_lock(&rfcomm_mutex)
66#define rfcomm_unlock() mutex_unlock(&rfcomm_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068static unsigned long rfcomm_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70static LIST_HEAD(session_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len);
73static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci);
74static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci);
75static int rfcomm_queue_disc(struct rfcomm_dlc *d);
76static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type);
77static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d);
78static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig);
79static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len);
80static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits);
81static void rfcomm_make_uih(struct sk_buff *skb, u8 addr);
82
83static void rfcomm_process_connect(struct rfcomm_session *s);
84
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +030085static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
86 bdaddr_t *dst,
87 u8 sec_level,
88 int *err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst);
90static void rfcomm_session_del(struct rfcomm_session *s);
91
92/* ---- RFCOMM frame parsing macros ---- */
93#define __get_dlci(b) ((b & 0xfc) >> 2)
94#define __get_channel(b) ((b & 0xf8) >> 3)
95#define __get_dir(b) ((b & 0x04) >> 2)
96#define __get_type(b) ((b & 0xef))
97
98#define __test_ea(b) ((b & 0x01))
99#define __test_cr(b) ((b & 0x02))
100#define __test_pf(b) ((b & 0x10))
101
102#define __addr(cr, dlci) (((dlci & 0x3f) << 2) | (cr << 1) | 0x01)
103#define __ctrl(type, pf) (((type & 0xef) | (pf << 4)))
104#define __dlci(dir, chn) (((chn & 0x1f) << 1) | dir)
105#define __srv_channel(dlci) (dlci >> 1)
106#define __dir(dlci) (dlci & 0x01)
107
108#define __len8(len) (((len) << 1) | 1)
109#define __len16(len) ((len) << 1)
110
111/* MCC macros */
112#define __mcc_type(cr, type) (((type << 2) | (cr << 1) | 0x01))
113#define __get_mcc_type(b) ((b & 0xfc) >> 2)
114#define __get_mcc_len(b) ((b & 0xfe) >> 1)
115
116/* RPN macros */
J. Suter3a5e9032005-08-09 20:28:46 -0700117#define __rpn_line_settings(data, stop, parity) ((data & 0x3) | ((stop & 0x1) << 2) | ((parity & 0x7) << 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define __get_rpn_data_bits(line) ((line) & 0x3)
119#define __get_rpn_stop_bits(line) (((line) >> 2) & 0x1)
J. Suter3a5e9032005-08-09 20:28:46 -0700120#define __get_rpn_parity(line) (((line) >> 3) & 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300122static inline void rfcomm_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 if (!rfcomm_thread)
125 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126 set_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 wake_up_process(rfcomm_thread);
128}
129
130static inline void rfcomm_session_put(struct rfcomm_session *s)
131{
132 if (atomic_dec_and_test(&s->refcnt))
133 rfcomm_session_del(s);
134}
135
136/* ---- RFCOMM FCS computation ---- */
137
Marcel Holtmann408c1ce2005-10-28 19:20:36 +0200138/* reversed, 8-bit, poly=0x07 */
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900139static unsigned char rfcomm_crc_table[256] = {
Marcel Holtmann408c1ce2005-10-28 19:20:36 +0200140 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
141 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
142 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
143 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
144
145 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
146 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
147 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
148 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
149
150 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
151 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
152 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
153 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
154
155 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
156 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
157 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
158 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
159
160 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
161 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
162 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
163 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
164
165 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
166 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
167 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
168 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
169
170 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
171 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
172 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
173 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
174
175 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
176 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
177 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
178 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
179};
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* CRC on 2 bytes */
182#define __crc(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]])
183
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900184/* FCS on 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static inline u8 __fcs(u8 *data)
186{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000187 return 0xff - __crc(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900190/* FCS on 3 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static inline u8 __fcs2(u8 *data)
192{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000193 return 0xff - rfcomm_crc_table[__crc(data) ^ data[2]];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/* Check FCS */
197static inline int __check_fcs(u8 *data, int type, u8 fcs)
198{
199 u8 f = __crc(data);
200
201 if (type != RFCOMM_UIH)
202 f = rfcomm_crc_table[f ^ data[2]];
203
204 return rfcomm_crc_table[f ^ fcs] != 0xcf;
205}
206
207/* ---- L2CAP callbacks ---- */
208static void rfcomm_l2state_change(struct sock *sk)
209{
210 BT_DBG("%p state %d", sk, sk->sk_state);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300211 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static void rfcomm_l2data_ready(struct sock *sk, int bytes)
215{
216 BT_DBG("%p bytes %d", sk, bytes);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300217 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220static int rfcomm_l2sock_create(struct socket **sock)
221{
222 int err;
223
224 BT_DBG("");
225
226 err = sock_create_kern(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP, sock);
227 if (!err) {
228 struct sock *sk = (*sock)->sk;
229 sk->sk_data_ready = rfcomm_l2data_ready;
230 sk->sk_state_change = rfcomm_l2state_change;
231 }
232 return err;
233}
234
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100235static inline int rfcomm_check_security(struct rfcomm_dlc *d)
Marcel Holtmann77db1982008-07-14 20:13:45 +0200236{
237 struct sock *sk = d->session->sock->sk;
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100238 __u8 auth_type;
Marcel Holtmann77db1982008-07-14 20:13:45 +0200239
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100240 switch (d->sec_level) {
241 case BT_SECURITY_HIGH:
242 auth_type = HCI_AT_GENERAL_BONDING_MITM;
243 break;
244 case BT_SECURITY_MEDIUM:
245 auth_type = HCI_AT_GENERAL_BONDING;
246 break;
247 default:
248 auth_type = HCI_AT_NO_BONDING;
249 break;
250 }
251
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 return hci_conn_security(l2cap_pi(sk)->conn->hcon, d->sec_level,
253 auth_type);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200254}
255
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300256static void rfcomm_session_timeout(unsigned long arg)
257{
258 struct rfcomm_session *s = (void *) arg;
259
260 BT_DBG("session %p state %ld", s, s->state);
261
262 set_bit(RFCOMM_TIMED_OUT, &s->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300263 rfcomm_schedule();
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300264}
265
266static void rfcomm_session_set_timer(struct rfcomm_session *s, long timeout)
267{
268 BT_DBG("session %p state %ld timeout %ld", s, s->state, timeout);
269
270 if (!mod_timer(&s->timer, jiffies + timeout))
271 rfcomm_session_hold(s);
272}
273
274static void rfcomm_session_clear_timer(struct rfcomm_session *s)
275{
276 BT_DBG("session %p state %ld", s, s->state);
277
278 if (timer_pending(&s->timer) && del_timer(&s->timer))
279 rfcomm_session_put(s);
280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/* ---- RFCOMM DLCs ---- */
283static void rfcomm_dlc_timeout(unsigned long arg)
284{
285 struct rfcomm_dlc *d = (void *) arg;
286
287 BT_DBG("dlc %p state %ld", d, d->state);
288
289 set_bit(RFCOMM_TIMED_OUT, &d->flags);
290 rfcomm_dlc_put(d);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300291 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294static void rfcomm_dlc_set_timer(struct rfcomm_dlc *d, long timeout)
295{
296 BT_DBG("dlc %p state %ld timeout %ld", d, d->state, timeout);
297
298 if (!mod_timer(&d->timer, jiffies + timeout))
299 rfcomm_dlc_hold(d);
300}
301
302static void rfcomm_dlc_clear_timer(struct rfcomm_dlc *d)
303{
304 BT_DBG("dlc %p state %ld", d, d->state);
305
306 if (timer_pending(&d->timer) && del_timer(&d->timer))
307 rfcomm_dlc_put(d);
308}
309
310static void rfcomm_dlc_clear_state(struct rfcomm_dlc *d)
311{
312 BT_DBG("%p", d);
313
314 d->state = BT_OPEN;
315 d->flags = 0;
316 d->mscex = 0;
Johan Hedberg183f7322010-12-06 15:56:17 +0200317 d->sec_level = BT_SECURITY_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 d->mtu = RFCOMM_DEFAULT_MTU;
319 d->v24_sig = RFCOMM_V24_RTC | RFCOMM_V24_RTR | RFCOMM_V24_DV;
320
321 d->cfc = RFCOMM_CFC_DISABLED;
322 d->rx_credits = RFCOMM_DEFAULT_CREDITS;
323}
324
Al Virodd0fc662005-10-07 07:46:04 +0100325struct rfcomm_dlc *rfcomm_dlc_alloc(gfp_t prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200327 struct rfcomm_dlc *d = kzalloc(sizeof(*d), prio);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!d)
330 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800332 setup_timer(&d->timer, rfcomm_dlc_timeout, (unsigned long)d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 skb_queue_head_init(&d->tx_queue);
335 spin_lock_init(&d->lock);
336 atomic_set(&d->refcnt, 1);
337
338 rfcomm_dlc_clear_state(d);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 BT_DBG("%p", d);
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return d;
343}
344
345void rfcomm_dlc_free(struct rfcomm_dlc *d)
346{
347 BT_DBG("%p", d);
348
349 skb_queue_purge(&d->tx_queue);
350 kfree(d);
351}
352
353static void rfcomm_dlc_link(struct rfcomm_session *s, struct rfcomm_dlc *d)
354{
355 BT_DBG("dlc %p session %p", d, s);
356
357 rfcomm_session_hold(s);
358
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300359 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 rfcomm_dlc_hold(d);
361 list_add(&d->list, &s->dlcs);
362 d->session = s;
363}
364
365static void rfcomm_dlc_unlink(struct rfcomm_dlc *d)
366{
367 struct rfcomm_session *s = d->session;
368
369 BT_DBG("dlc %p refcnt %d session %p", d, atomic_read(&d->refcnt), s);
370
371 list_del(&d->list);
372 d->session = NULL;
373 rfcomm_dlc_put(d);
374
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300375 if (list_empty(&s->dlcs))
376 rfcomm_session_set_timer(s, RFCOMM_IDLE_TIMEOUT);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 rfcomm_session_put(s);
379}
380
381static struct rfcomm_dlc *rfcomm_dlc_get(struct rfcomm_session *s, u8 dlci)
382{
383 struct rfcomm_dlc *d;
384 struct list_head *p;
385
386 list_for_each(p, &s->dlcs) {
387 d = list_entry(p, struct rfcomm_dlc, list);
388 if (d->dlci == dlci)
389 return d;
390 }
391 return NULL;
392}
393
394static int __rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
395{
396 struct rfcomm_session *s;
397 int err = 0;
398 u8 dlci;
399
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900400 BT_DBG("dlc %p state %ld %s %s channel %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 d, d->state, batostr(src), batostr(dst), channel);
402
403 if (channel < 1 || channel > 30)
404 return -EINVAL;
405
406 if (d->state != BT_OPEN && d->state != BT_CLOSED)
407 return 0;
408
409 s = rfcomm_session_get(src, dst);
410 if (!s) {
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +0300411 s = rfcomm_session_create(src, dst, d->sec_level, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (!s)
413 return err;
414 }
415
416 dlci = __dlci(!s->initiator, channel);
417
418 /* Check if DLCI already exists */
419 if (rfcomm_dlc_get(s, dlci))
420 return -EBUSY;
421
422 rfcomm_dlc_clear_state(d);
423
424 d->dlci = dlci;
425 d->addr = __addr(s->initiator, dlci);
426 d->priority = 7;
427
Marcel Holtmann77db1982008-07-14 20:13:45 +0200428 d->state = BT_CONFIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 rfcomm_dlc_link(s, d);
430
Marcel Holtmann77db1982008-07-14 20:13:45 +0200431 d->out = 1;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 d->mtu = s->mtu;
434 d->cfc = (s->cfc == RFCOMM_CFC_UNKNOWN) ? 0 : s->cfc;
435
Marcel Holtmann77db1982008-07-14 20:13:45 +0200436 if (s->state == BT_CONNECTED) {
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +0100437 if (rfcomm_check_security(d))
Marcel Holtmann77db1982008-07-14 20:13:45 +0200438 rfcomm_send_pn(s, 1, d);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100439 else
440 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200441 }
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT);
Marcel Holtmann77db1982008-07-14 20:13:45 +0200444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
446}
447
448int rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel)
449{
450 int r;
451
452 rfcomm_lock();
453
454 r = __rfcomm_dlc_open(d, src, dst, channel);
455
456 rfcomm_unlock();
457 return r;
458}
459
460static int __rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
461{
462 struct rfcomm_session *s = d->session;
463 if (!s)
464 return 0;
465
466 BT_DBG("dlc %p state %ld dlci %d err %d session %p",
467 d, d->state, d->dlci, err, s);
468
469 switch (d->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 case BT_CONNECT:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100471 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
472 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300473 rfcomm_schedule();
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100474 break;
475 }
476 /* Fall through */
477
478 case BT_CONNECTED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 d->state = BT_DISCONN;
480 if (skb_queue_empty(&d->tx_queue)) {
481 rfcomm_send_disc(s, d->dlci);
482 rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT);
483 } else {
484 rfcomm_queue_disc(d);
485 rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT * 2);
486 }
487 break;
488
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100489 case BT_OPEN:
Marcel Holtmann8bf47942009-02-16 02:59:49 +0100490 case BT_CONNECT2:
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100491 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
492 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300493 rfcomm_schedule();
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +0100494 break;
495 }
496 /* Fall through */
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 default:
499 rfcomm_dlc_clear_timer(d);
500
501 rfcomm_dlc_lock(d);
502 d->state = BT_CLOSED;
Dave Young1905f6c2008-04-01 23:59:06 -0700503 d->state_change(d, err);
Arjan van de Ven4c8411f2008-05-29 01:32:47 -0700504 rfcomm_dlc_unlock(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 skb_queue_purge(&d->tx_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 rfcomm_dlc_unlink(d);
508 }
509
510 return 0;
511}
512
513int rfcomm_dlc_close(struct rfcomm_dlc *d, int err)
514{
515 int r;
516
517 rfcomm_lock();
518
519 r = __rfcomm_dlc_close(d, err);
520
521 rfcomm_unlock();
522 return r;
523}
524
525int rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb)
526{
527 int len = skb->len;
528
529 if (d->state != BT_CONNECTED)
530 return -ENOTCONN;
531
532 BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len);
533
534 if (len > d->mtu)
535 return -EINVAL;
536
537 rfcomm_make_uih(skb, d->addr);
538 skb_queue_tail(&d->tx_queue, skb);
539
540 if (!test_bit(RFCOMM_TX_THROTTLED, &d->flags))
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300541 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return len;
543}
544
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800545void __rfcomm_dlc_throttle(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 BT_DBG("dlc %p state %ld", d, d->state);
548
549 if (!d->cfc) {
550 d->v24_sig |= RFCOMM_V24_FC;
551 set_bit(RFCOMM_MSC_PENDING, &d->flags);
552 }
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300553 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800556void __rfcomm_dlc_unthrottle(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 BT_DBG("dlc %p state %ld", d, d->state);
559
560 if (!d->cfc) {
561 d->v24_sig &= ~RFCOMM_V24_FC;
562 set_bit(RFCOMM_MSC_PENDING, &d->flags);
563 }
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300564 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900567/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 Set/get modem status functions use _local_ status i.e. what we report
569 to the other side.
570 Remote status is provided by dlc->modem_status() callback.
571 */
572int rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig)
573{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900574 BT_DBG("dlc %p state %ld v24_sig 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 d, d->state, v24_sig);
576
577 if (test_bit(RFCOMM_RX_THROTTLED, &d->flags))
578 v24_sig |= RFCOMM_V24_FC;
579 else
580 v24_sig &= ~RFCOMM_V24_FC;
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 d->v24_sig = v24_sig;
583
584 if (!test_and_set_bit(RFCOMM_MSC_PENDING, &d->flags))
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300585 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 return 0;
588}
589
590int rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig)
591{
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900592 BT_DBG("dlc %p state %ld v24_sig 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 d, d->state, d->v24_sig);
594
595 *v24_sig = d->v24_sig;
596 return 0;
597}
598
599/* ---- RFCOMM sessions ---- */
600static struct rfcomm_session *rfcomm_session_add(struct socket *sock, int state)
601{
Marcel Holtmann25ea6db2006-07-06 15:40:09 +0200602 struct rfcomm_session *s = kzalloc(sizeof(*s), GFP_KERNEL);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!s)
605 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 BT_DBG("session %p sock %p", s, sock);
608
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300609 setup_timer(&s->timer, rfcomm_session_timeout, (unsigned long) s);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 INIT_LIST_HEAD(&s->dlcs);
612 s->state = state;
613 s->sock = sock;
614
615 s->mtu = RFCOMM_DEFAULT_MTU;
Marcel Holtmann7c2660b2006-07-03 10:02:51 +0200616 s->cfc = disable_cfc ? RFCOMM_CFC_DISABLED : RFCOMM_CFC_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* Do not increment module usage count for listening sessions.
619 * Otherwise we won't be able to unload the module. */
620 if (state != BT_LISTEN)
621 if (!try_module_get(THIS_MODULE)) {
622 kfree(s);
623 return NULL;
624 }
625
626 list_add(&s->list, &session_list);
627
628 return s;
629}
630
631static void rfcomm_session_del(struct rfcomm_session *s)
632{
633 int state = s->state;
634
635 BT_DBG("session %p state %ld", s, s->state);
636
637 list_del(&s->list);
638
639 if (state == BT_CONNECTED)
640 rfcomm_send_disc(s, 0);
641
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300642 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 sock_release(s->sock);
644 kfree(s);
645
646 if (state != BT_LISTEN)
647 module_put(THIS_MODULE);
648}
649
650static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst)
651{
652 struct rfcomm_session *s;
653 struct list_head *p, *n;
654 struct bt_sock *sk;
655 list_for_each_safe(p, n, &session_list) {
656 s = list_entry(p, struct rfcomm_session, list);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900657 sk = bt_sk(s->sock->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if ((!bacmp(src, BDADDR_ANY) || !bacmp(&sk->src, src)) &&
660 !bacmp(&sk->dst, dst))
661 return s;
662 }
663 return NULL;
664}
665
666static void rfcomm_session_close(struct rfcomm_session *s, int err)
667{
668 struct rfcomm_dlc *d;
669 struct list_head *p, *n;
670
671 BT_DBG("session %p state %ld err %d", s, s->state, err);
672
673 rfcomm_session_hold(s);
674
675 s->state = BT_CLOSED;
676
677 /* Close all dlcs */
678 list_for_each_safe(p, n, &s->dlcs) {
679 d = list_entry(p, struct rfcomm_dlc, list);
680 d->state = BT_CLOSED;
681 __rfcomm_dlc_close(d, err);
682 }
683
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -0300684 rfcomm_session_clear_timer(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 rfcomm_session_put(s);
686}
687
Luiz Augusto von Dentz63ce0902010-08-19 14:06:10 +0300688static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
689 bdaddr_t *dst,
690 u8 sec_level,
691 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct rfcomm_session *s = NULL;
694 struct sockaddr_l2 addr;
695 struct socket *sock;
696 struct sock *sk;
697
698 BT_DBG("%s %s", batostr(src), batostr(dst));
699
700 *err = rfcomm_l2sock_create(&sock);
701 if (*err < 0)
702 return NULL;
703
704 bacpy(&addr.l2_bdaddr, src);
705 addr.l2_family = AF_BLUETOOTH;
706 addr.l2_psm = 0;
Marcel Holtmann37e62f52009-02-17 21:49:33 +0100707 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +0200708 *err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (*err < 0)
710 goto failed;
711
712 /* Set L2CAP options */
713 sk = sock->sk;
714 lock_sock(sk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 l2cap_pi(sk)->imtu = l2cap_mtu;
716 l2cap_pi(sk)->sec_level = sec_level;
Marcel Holtmanneae38ee2009-10-05 12:23:48 +0200717 if (l2cap_ertm)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 l2cap_pi(sk)->mode = L2CAP_MODE_ERTM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 release_sock(sk);
720
721 s = rfcomm_session_add(sock, BT_BOUND);
722 if (!s) {
723 *err = -ENOMEM;
724 goto failed;
725 }
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 s->initiator = 1;
728
729 bacpy(&addr.l2_bdaddr, dst);
730 addr.l2_family = AF_BLUETOOTH;
Marcel Holtmannb4324b52009-06-07 18:06:51 +0200731 addr.l2_psm = cpu_to_le16(RFCOMM_PSM);
Marcel Holtmann37e62f52009-02-17 21:49:33 +0100732 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +0200733 *err = kernel_connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK);
Marcel Holtmannb4c612a2006-09-23 09:54:38 +0200734 if (*err == 0 || *err == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return s;
736
737 rfcomm_session_del(s);
738 return NULL;
739
740failed:
741 sock_release(sock);
742 return NULL;
743}
744
745void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst)
746{
747 struct sock *sk = s->sock->sk;
748 if (src)
749 bacpy(src, &bt_sk(sk)->src);
750 if (dst)
751 bacpy(dst, &bt_sk(sk)->dst);
752}
753
754/* ---- RFCOMM frame sending ---- */
755static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len)
756{
757 struct socket *sock = s->sock;
758 struct kvec iv = { data, len };
759 struct msghdr msg;
760
761 BT_DBG("session %p len %d", s, len);
762
763 memset(&msg, 0, sizeof(msg));
764
765 return kernel_sendmsg(sock, &msg, &iv, 1, len);
766}
767
768static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci)
769{
770 struct rfcomm_cmd cmd;
771
772 BT_DBG("%p dlci %d", s, dlci);
773
774 cmd.addr = __addr(s->initiator, dlci);
775 cmd.ctrl = __ctrl(RFCOMM_SABM, 1);
776 cmd.len = __len8(0);
777 cmd.fcs = __fcs2((u8 *) &cmd);
778
779 return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd));
780}
781
782static int rfcomm_send_ua(struct rfcomm_session *s, u8 dlci)
783{
784 struct rfcomm_cmd cmd;
785
786 BT_DBG("%p dlci %d", s, dlci);
787
788 cmd.addr = __addr(!s->initiator, dlci);
789 cmd.ctrl = __ctrl(RFCOMM_UA, 1);
790 cmd.len = __len8(0);
791 cmd.fcs = __fcs2((u8 *) &cmd);
792
793 return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd));
794}
795
796static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci)
797{
798 struct rfcomm_cmd cmd;
799
800 BT_DBG("%p dlci %d", s, dlci);
801
802 cmd.addr = __addr(s->initiator, dlci);
803 cmd.ctrl = __ctrl(RFCOMM_DISC, 1);
804 cmd.len = __len8(0);
805 cmd.fcs = __fcs2((u8 *) &cmd);
806
807 return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd));
808}
809
810static int rfcomm_queue_disc(struct rfcomm_dlc *d)
811{
812 struct rfcomm_cmd *cmd;
813 struct sk_buff *skb;
814
815 BT_DBG("dlc %p dlci %d", d, d->dlci);
816
817 skb = alloc_skb(sizeof(*cmd), GFP_KERNEL);
818 if (!skb)
819 return -ENOMEM;
820
821 cmd = (void *) __skb_put(skb, sizeof(*cmd));
822 cmd->addr = d->addr;
823 cmd->ctrl = __ctrl(RFCOMM_DISC, 1);
824 cmd->len = __len8(0);
825 cmd->fcs = __fcs2((u8 *) cmd);
826
827 skb_queue_tail(&d->tx_queue, skb);
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +0300828 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830}
831
832static int rfcomm_send_dm(struct rfcomm_session *s, u8 dlci)
833{
834 struct rfcomm_cmd cmd;
835
836 BT_DBG("%p dlci %d", s, dlci);
837
838 cmd.addr = __addr(!s->initiator, dlci);
839 cmd.ctrl = __ctrl(RFCOMM_DM, 1);
840 cmd.len = __len8(0);
841 cmd.fcs = __fcs2((u8 *) &cmd);
842
843 return rfcomm_send_frame(s, (void *) &cmd, sizeof(cmd));
844}
845
846static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type)
847{
848 struct rfcomm_hdr *hdr;
849 struct rfcomm_mcc *mcc;
850 u8 buf[16], *ptr = buf;
851
852 BT_DBG("%p cr %d type %d", s, cr, type);
853
854 hdr = (void *) ptr; ptr += sizeof(*hdr);
855 hdr->addr = __addr(s->initiator, 0);
856 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
857 hdr->len = __len8(sizeof(*mcc) + 1);
858
859 mcc = (void *) ptr; ptr += sizeof(*mcc);
860 mcc->type = __mcc_type(cr, RFCOMM_NSC);
861 mcc->len = __len8(1);
862
863 /* Type that we didn't like */
864 *ptr = __mcc_type(cr, type); ptr++;
865
866 *ptr = __fcs(buf); ptr++;
867
868 return rfcomm_send_frame(s, buf, ptr - buf);
869}
870
871static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d)
872{
873 struct rfcomm_hdr *hdr;
874 struct rfcomm_mcc *mcc;
875 struct rfcomm_pn *pn;
876 u8 buf[16], *ptr = buf;
877
878 BT_DBG("%p cr %d dlci %d mtu %d", s, cr, d->dlci, d->mtu);
879
880 hdr = (void *) ptr; ptr += sizeof(*hdr);
881 hdr->addr = __addr(s->initiator, 0);
882 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
883 hdr->len = __len8(sizeof(*mcc) + sizeof(*pn));
884
885 mcc = (void *) ptr; ptr += sizeof(*mcc);
886 mcc->type = __mcc_type(cr, RFCOMM_PN);
887 mcc->len = __len8(sizeof(*pn));
888
889 pn = (void *) ptr; ptr += sizeof(*pn);
890 pn->dlci = d->dlci;
891 pn->priority = d->priority;
892 pn->ack_timer = 0;
893 pn->max_retrans = 0;
894
895 if (s->cfc) {
896 pn->flow_ctrl = cr ? 0xf0 : 0xe0;
897 pn->credits = RFCOMM_DEFAULT_CREDITS;
898 } else {
899 pn->flow_ctrl = 0;
900 pn->credits = 0;
901 }
902
Marcel Holtmann98bcd082006-07-14 11:42:12 +0200903 if (cr && channel_mtu >= 0)
Marcel Holtmannb4324b52009-06-07 18:06:51 +0200904 pn->mtu = cpu_to_le16(channel_mtu);
Marcel Holtmann98bcd082006-07-14 11:42:12 +0200905 else
Marcel Holtmannb4324b52009-06-07 18:06:51 +0200906 pn->mtu = cpu_to_le16(d->mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 *ptr = __fcs(buf); ptr++;
909
910 return rfcomm_send_frame(s, buf, ptr - buf);
911}
912
J. Suter3a5e9032005-08-09 20:28:46 -0700913int rfcomm_send_rpn(struct rfcomm_session *s, int cr, u8 dlci,
914 u8 bit_rate, u8 data_bits, u8 stop_bits,
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900915 u8 parity, u8 flow_ctrl_settings,
J. Suter3a5e9032005-08-09 20:28:46 -0700916 u8 xon_char, u8 xoff_char, u16 param_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 struct rfcomm_hdr *hdr;
919 struct rfcomm_mcc *mcc;
920 struct rfcomm_rpn *rpn;
921 u8 buf[16], *ptr = buf;
922
923 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 +0900924 " flwc_s 0x%x xon_c 0x%x xoff_c 0x%x p_mask 0x%x",
925 s, cr, dlci, bit_rate, data_bits, stop_bits, parity,
J. Suter3a5e9032005-08-09 20:28:46 -0700926 flow_ctrl_settings, xon_char, xoff_char, param_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 hdr = (void *) ptr; ptr += sizeof(*hdr);
929 hdr->addr = __addr(s->initiator, 0);
930 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
931 hdr->len = __len8(sizeof(*mcc) + sizeof(*rpn));
932
933 mcc = (void *) ptr; ptr += sizeof(*mcc);
934 mcc->type = __mcc_type(cr, RFCOMM_RPN);
935 mcc->len = __len8(sizeof(*rpn));
936
937 rpn = (void *) ptr; ptr += sizeof(*rpn);
938 rpn->dlci = __addr(1, dlci);
939 rpn->bit_rate = bit_rate;
940 rpn->line_settings = __rpn_line_settings(data_bits, stop_bits, parity);
941 rpn->flow_ctrl = flow_ctrl_settings;
942 rpn->xon_char = xon_char;
943 rpn->xoff_char = xoff_char;
Al Viroe8db8c92006-11-08 00:28:44 -0800944 rpn->param_mask = cpu_to_le16(param_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 *ptr = __fcs(buf); ptr++;
947
948 return rfcomm_send_frame(s, buf, ptr - buf);
949}
950
951static int rfcomm_send_rls(struct rfcomm_session *s, int cr, u8 dlci, u8 status)
952{
953 struct rfcomm_hdr *hdr;
954 struct rfcomm_mcc *mcc;
955 struct rfcomm_rls *rls;
956 u8 buf[16], *ptr = buf;
957
958 BT_DBG("%p cr %d status 0x%x", s, cr, status);
959
960 hdr = (void *) ptr; ptr += sizeof(*hdr);
961 hdr->addr = __addr(s->initiator, 0);
962 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
963 hdr->len = __len8(sizeof(*mcc) + sizeof(*rls));
964
965 mcc = (void *) ptr; ptr += sizeof(*mcc);
966 mcc->type = __mcc_type(cr, RFCOMM_RLS);
967 mcc->len = __len8(sizeof(*rls));
968
969 rls = (void *) ptr; ptr += sizeof(*rls);
970 rls->dlci = __addr(1, dlci);
971 rls->status = status;
972
973 *ptr = __fcs(buf); ptr++;
974
975 return rfcomm_send_frame(s, buf, ptr - buf);
976}
977
978static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig)
979{
980 struct rfcomm_hdr *hdr;
981 struct rfcomm_mcc *mcc;
982 struct rfcomm_msc *msc;
983 u8 buf[16], *ptr = buf;
984
985 BT_DBG("%p cr %d v24 0x%x", s, cr, v24_sig);
986
987 hdr = (void *) ptr; ptr += sizeof(*hdr);
988 hdr->addr = __addr(s->initiator, 0);
989 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
990 hdr->len = __len8(sizeof(*mcc) + sizeof(*msc));
991
992 mcc = (void *) ptr; ptr += sizeof(*mcc);
993 mcc->type = __mcc_type(cr, RFCOMM_MSC);
994 mcc->len = __len8(sizeof(*msc));
995
996 msc = (void *) ptr; ptr += sizeof(*msc);
997 msc->dlci = __addr(1, dlci);
998 msc->v24_sig = v24_sig | 0x01;
999
1000 *ptr = __fcs(buf); ptr++;
1001
1002 return rfcomm_send_frame(s, buf, ptr - buf);
1003}
1004
1005static int rfcomm_send_fcoff(struct rfcomm_session *s, int cr)
1006{
1007 struct rfcomm_hdr *hdr;
1008 struct rfcomm_mcc *mcc;
1009 u8 buf[16], *ptr = buf;
1010
1011 BT_DBG("%p cr %d", s, cr);
1012
1013 hdr = (void *) ptr; ptr += sizeof(*hdr);
1014 hdr->addr = __addr(s->initiator, 0);
1015 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1016 hdr->len = __len8(sizeof(*mcc));
1017
1018 mcc = (void *) ptr; ptr += sizeof(*mcc);
1019 mcc->type = __mcc_type(cr, RFCOMM_FCOFF);
1020 mcc->len = __len8(0);
1021
1022 *ptr = __fcs(buf); ptr++;
1023
1024 return rfcomm_send_frame(s, buf, ptr - buf);
1025}
1026
1027static int rfcomm_send_fcon(struct rfcomm_session *s, int cr)
1028{
1029 struct rfcomm_hdr *hdr;
1030 struct rfcomm_mcc *mcc;
1031 u8 buf[16], *ptr = buf;
1032
1033 BT_DBG("%p cr %d", s, cr);
1034
1035 hdr = (void *) ptr; ptr += sizeof(*hdr);
1036 hdr->addr = __addr(s->initiator, 0);
1037 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1038 hdr->len = __len8(sizeof(*mcc));
1039
1040 mcc = (void *) ptr; ptr += sizeof(*mcc);
1041 mcc->type = __mcc_type(cr, RFCOMM_FCON);
1042 mcc->len = __len8(0);
1043
1044 *ptr = __fcs(buf); ptr++;
1045
1046 return rfcomm_send_frame(s, buf, ptr - buf);
1047}
1048
1049static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len)
1050{
1051 struct socket *sock = s->sock;
1052 struct kvec iv[3];
1053 struct msghdr msg;
1054 unsigned char hdr[5], crc[1];
1055
1056 if (len > 125)
1057 return -EINVAL;
1058
1059 BT_DBG("%p cr %d", s, cr);
1060
1061 hdr[0] = __addr(s->initiator, 0);
1062 hdr[1] = __ctrl(RFCOMM_UIH, 0);
1063 hdr[2] = 0x01 | ((len + 2) << 1);
1064 hdr[3] = 0x01 | ((cr & 0x01) << 1) | (RFCOMM_TEST << 2);
1065 hdr[4] = 0x01 | (len << 1);
1066
1067 crc[0] = __fcs(hdr);
1068
1069 iv[0].iov_base = hdr;
1070 iv[0].iov_len = 5;
1071 iv[1].iov_base = pattern;
1072 iv[1].iov_len = len;
1073 iv[2].iov_base = crc;
1074 iv[2].iov_len = 1;
1075
1076 memset(&msg, 0, sizeof(msg));
1077
1078 return kernel_sendmsg(sock, &msg, iv, 3, 6 + len);
1079}
1080
1081static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits)
1082{
1083 struct rfcomm_hdr *hdr;
1084 u8 buf[16], *ptr = buf;
1085
1086 BT_DBG("%p addr %d credits %d", s, addr, credits);
1087
1088 hdr = (void *) ptr; ptr += sizeof(*hdr);
1089 hdr->addr = addr;
1090 hdr->ctrl = __ctrl(RFCOMM_UIH, 1);
1091 hdr->len = __len8(0);
1092
1093 *ptr = credits; ptr++;
1094
1095 *ptr = __fcs(buf); ptr++;
1096
1097 return rfcomm_send_frame(s, buf, ptr - buf);
1098}
1099
1100static void rfcomm_make_uih(struct sk_buff *skb, u8 addr)
1101{
1102 struct rfcomm_hdr *hdr;
1103 int len = skb->len;
1104 u8 *crc;
1105
1106 if (len > 127) {
1107 hdr = (void *) skb_push(skb, 4);
Marcel Holtmannb4324b52009-06-07 18:06:51 +02001108 put_unaligned(cpu_to_le16(__len16(len)), (__le16 *) &hdr->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 } else {
1110 hdr = (void *) skb_push(skb, 3);
1111 hdr->len = __len8(len);
1112 }
1113 hdr->addr = addr;
1114 hdr->ctrl = __ctrl(RFCOMM_UIH, 0);
1115
1116 crc = skb_put(skb, 1);
1117 *crc = __fcs((void *) hdr);
1118}
1119
1120/* ---- RFCOMM frame reception ---- */
1121static int rfcomm_recv_ua(struct rfcomm_session *s, u8 dlci)
1122{
1123 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1124
1125 if (dlci) {
1126 /* Data channel */
1127 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1128 if (!d) {
1129 rfcomm_send_dm(s, dlci);
1130 return 0;
1131 }
1132
1133 switch (d->state) {
1134 case BT_CONNECT:
1135 rfcomm_dlc_clear_timer(d);
1136
1137 rfcomm_dlc_lock(d);
1138 d->state = BT_CONNECTED;
1139 d->state_change(d, 0);
1140 rfcomm_dlc_unlock(d);
1141
1142 rfcomm_send_msc(s, 1, dlci, d->v24_sig);
1143 break;
1144
1145 case BT_DISCONN:
1146 d->state = BT_CLOSED;
1147 __rfcomm_dlc_close(d, 0);
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001148
1149 if (list_empty(&s->dlcs)) {
1150 s->state = BT_DISCONN;
1151 rfcomm_send_disc(s, 0);
1152 }
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
1155 }
1156 } else {
1157 /* Control channel */
1158 switch (s->state) {
1159 case BT_CONNECT:
1160 s->state = BT_CONNECTED;
1161 rfcomm_process_connect(s);
1162 break;
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001163
1164 case BT_DISCONN:
Nick Pelly6c2718d2010-02-03 16:18:36 -08001165 /* When socket is closed and we are not RFCOMM
1166 * initiator rfcomm_process_rx already calls
1167 * rfcomm_session_put() */
1168 if (s->sock->sk->sk_state != BT_CLOSED)
Lukáš Turek683d9492011-01-05 02:43:59 +01001169 if (list_empty(&s->dlcs))
1170 rfcomm_session_put(s);
Marcel Holtmann9cf5b0e2007-05-05 00:36:13 +02001171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173 }
1174 return 0;
1175}
1176
1177static int rfcomm_recv_dm(struct rfcomm_session *s, u8 dlci)
1178{
1179 int err = 0;
1180
1181 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1182
1183 if (dlci) {
1184 /* Data DLC */
1185 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1186 if (d) {
1187 if (d->state == BT_CONNECT || d->state == BT_CONFIG)
1188 err = ECONNREFUSED;
1189 else
1190 err = ECONNRESET;
1191
1192 d->state = BT_CLOSED;
1193 __rfcomm_dlc_close(d, err);
1194 }
1195 } else {
1196 if (s->state == BT_CONNECT)
1197 err = ECONNREFUSED;
1198 else
1199 err = ECONNRESET;
1200
1201 s->state = BT_CLOSED;
1202 rfcomm_session_close(s, err);
1203 }
1204 return 0;
1205}
1206
1207static int rfcomm_recv_disc(struct rfcomm_session *s, u8 dlci)
1208{
1209 int err = 0;
1210
1211 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1212
1213 if (dlci) {
1214 struct rfcomm_dlc *d = rfcomm_dlc_get(s, dlci);
1215 if (d) {
1216 rfcomm_send_ua(s, dlci);
1217
1218 if (d->state == BT_CONNECT || d->state == BT_CONFIG)
1219 err = ECONNREFUSED;
1220 else
1221 err = ECONNRESET;
1222
1223 d->state = BT_CLOSED;
1224 __rfcomm_dlc_close(d, err);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001225 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 rfcomm_send_dm(s, dlci);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 } else {
1229 rfcomm_send_ua(s, 0);
1230
1231 if (s->state == BT_CONNECT)
1232 err = ECONNREFUSED;
1233 else
1234 err = ECONNRESET;
1235
1236 s->state = BT_CLOSED;
1237 rfcomm_session_close(s, err);
1238 }
1239
1240 return 0;
1241}
1242
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001243void rfcomm_dlc_accept(struct rfcomm_dlc *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Marcel Holtmann300b9392006-07-03 10:37:55 +02001245 struct sock *sk = d->session->sock->sk;
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 BT_DBG("dlc %p", d);
1248
1249 rfcomm_send_ua(d->session, d->dlci);
1250
Johan Hedberge2139b32009-03-26 16:41:56 +02001251 rfcomm_dlc_clear_timer(d);
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 rfcomm_dlc_lock(d);
1254 d->state = BT_CONNECTED;
1255 d->state_change(d, 0);
1256 rfcomm_dlc_unlock(d);
1257
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001258 if (d->role_switch)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 hci_conn_switch_role(l2cap_pi(sk)->conn->hcon, 0x00);
Marcel Holtmann300b9392006-07-03 10:37:55 +02001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig);
1262}
1263
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001264static void rfcomm_check_accept(struct rfcomm_dlc *d)
1265{
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001266 if (rfcomm_check_security(d)) {
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001267 if (d->defer_setup) {
1268 set_bit(RFCOMM_DEFER_SETUP, &d->flags);
1269 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8bf47942009-02-16 02:59:49 +01001270
1271 rfcomm_dlc_lock(d);
1272 d->state = BT_CONNECT2;
1273 d->state_change(d, 0);
1274 rfcomm_dlc_unlock(d);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001275 } else
1276 rfcomm_dlc_accept(d);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001277 } else {
1278 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
1279 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001280 }
1281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static int rfcomm_recv_sabm(struct rfcomm_session *s, u8 dlci)
1284{
1285 struct rfcomm_dlc *d;
1286 u8 channel;
1287
1288 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1289
1290 if (!dlci) {
1291 rfcomm_send_ua(s, 0);
1292
1293 if (s->state == BT_OPEN) {
1294 s->state = BT_CONNECTED;
1295 rfcomm_process_connect(s);
1296 }
1297 return 0;
1298 }
1299
1300 /* Check if DLC exists */
1301 d = rfcomm_dlc_get(s, dlci);
1302 if (d) {
1303 if (d->state == BT_OPEN) {
1304 /* DLC was previously opened by PN request */
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001305 rfcomm_check_accept(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 return 0;
1308 }
1309
1310 /* Notify socket layer about incoming connection */
1311 channel = __srv_channel(dlci);
1312 if (rfcomm_connect_ind(s, channel, &d)) {
1313 d->dlci = dlci;
1314 d->addr = __addr(s->initiator, dlci);
1315 rfcomm_dlc_link(s, d);
1316
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001317 rfcomm_check_accept(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 } else {
1319 rfcomm_send_dm(s, dlci);
1320 }
1321
1322 return 0;
1323}
1324
1325static int rfcomm_apply_pn(struct rfcomm_dlc *d, int cr, struct rfcomm_pn *pn)
1326{
1327 struct rfcomm_session *s = d->session;
1328
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001329 BT_DBG("dlc %p state %ld dlci %d mtu %d fc 0x%x credits %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 d, d->state, d->dlci, pn->mtu, pn->flow_ctrl, pn->credits);
1331
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001332 if ((pn->flow_ctrl == 0xf0 && s->cfc != RFCOMM_CFC_DISABLED) ||
1333 pn->flow_ctrl == 0xe0) {
1334 d->cfc = RFCOMM_CFC_ENABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 d->tx_credits = pn->credits;
1336 } else {
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001337 d->cfc = RFCOMM_CFC_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1339 }
1340
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02001341 if (s->cfc == RFCOMM_CFC_UNKNOWN)
1342 s->cfc = d->cfc;
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 d->priority = pn->priority;
1345
Marcel Holtmannb4324b52009-06-07 18:06:51 +02001346 d->mtu = __le16_to_cpu(pn->mtu);
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001347
1348 if (cr && d->mtu > s->mtu)
1349 d->mtu = s->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 return 0;
1352}
1353
1354static int rfcomm_recv_pn(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1355{
1356 struct rfcomm_pn *pn = (void *) skb->data;
1357 struct rfcomm_dlc *d;
1358 u8 dlci = pn->dlci;
1359
1360 BT_DBG("session %p state %ld dlci %d", s, s->state, dlci);
1361
1362 if (!dlci)
1363 return 0;
1364
1365 d = rfcomm_dlc_get(s, dlci);
1366 if (d) {
1367 if (cr) {
1368 /* PN request */
1369 rfcomm_apply_pn(d, cr, pn);
1370 rfcomm_send_pn(s, 0, d);
1371 } else {
1372 /* PN response */
1373 switch (d->state) {
1374 case BT_CONFIG:
1375 rfcomm_apply_pn(d, cr, pn);
1376
1377 d->state = BT_CONNECT;
1378 rfcomm_send_sabm(s, d->dlci);
1379 break;
1380 }
1381 }
1382 } else {
1383 u8 channel = __srv_channel(dlci);
1384
1385 if (!cr)
1386 return 0;
1387
1388 /* PN request for non existing DLC.
1389 * Assume incoming connection. */
1390 if (rfcomm_connect_ind(s, channel, &d)) {
1391 d->dlci = dlci;
1392 d->addr = __addr(s->initiator, dlci);
1393 rfcomm_dlc_link(s, d);
1394
1395 rfcomm_apply_pn(d, cr, pn);
1396
1397 d->state = BT_OPEN;
1398 rfcomm_send_pn(s, 0, d);
1399 } else {
1400 rfcomm_send_dm(s, dlci);
1401 }
1402 }
1403 return 0;
1404}
1405
1406static int rfcomm_recv_rpn(struct rfcomm_session *s, int cr, int len, struct sk_buff *skb)
1407{
1408 struct rfcomm_rpn *rpn = (void *) skb->data;
1409 u8 dlci = __get_dlci(rpn->dlci);
1410
1411 u8 bit_rate = 0;
1412 u8 data_bits = 0;
1413 u8 stop_bits = 0;
1414 u8 parity = 0;
1415 u8 flow_ctrl = 0;
1416 u8 xon_char = 0;
1417 u8 xoff_char = 0;
1418 u16 rpn_mask = RFCOMM_RPN_PM_ALL;
J. Suter3a5e9032005-08-09 20:28:46 -07001419
1420 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",
1421 dlci, cr, len, rpn->bit_rate, rpn->line_settings, rpn->flow_ctrl,
1422 rpn->xon_char, rpn->xoff_char, rpn->param_mask);
1423
1424 if (!cr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
J. Suter3a5e9032005-08-09 20:28:46 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (len == 1) {
Yuri Kululin08601462010-07-23 13:57:12 +04001428 /* This is a request, return default (according to ETSI TS 07.10) settings */
1429 bit_rate = RFCOMM_RPN_BR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 data_bits = RFCOMM_RPN_DATA_8;
1431 stop_bits = RFCOMM_RPN_STOP_1;
1432 parity = RFCOMM_RPN_PARITY_NONE;
1433 flow_ctrl = RFCOMM_RPN_FLOW_NONE;
1434 xon_char = RFCOMM_RPN_XON_CHAR;
1435 xoff_char = RFCOMM_RPN_XOFF_CHAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 goto rpn_out;
1437 }
J. Suter3a5e9032005-08-09 20:28:46 -07001438
1439 /* Check for sane values, ignore/accept bit_rate, 8 bits, 1 stop bit,
1440 * no parity, no flow control lines, normal XON/XOFF chars */
1441
Al Viroe8db8c92006-11-08 00:28:44 -08001442 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_BITRATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 bit_rate = rpn->bit_rate;
Yuri Kululin08601462010-07-23 13:57:12 +04001444 if (bit_rate > RFCOMM_RPN_BR_230400) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 BT_DBG("RPN bit rate mismatch 0x%x", bit_rate);
Yuri Kululin08601462010-07-23 13:57:12 +04001446 bit_rate = RFCOMM_RPN_BR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 rpn_mask ^= RFCOMM_RPN_PM_BITRATE;
1448 }
1449 }
J. Suter3a5e9032005-08-09 20:28:46 -07001450
Al Viroe8db8c92006-11-08 00:28:44 -08001451 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_DATA)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 data_bits = __get_rpn_data_bits(rpn->line_settings);
1453 if (data_bits != RFCOMM_RPN_DATA_8) {
1454 BT_DBG("RPN data bits mismatch 0x%x", data_bits);
1455 data_bits = RFCOMM_RPN_DATA_8;
1456 rpn_mask ^= RFCOMM_RPN_PM_DATA;
1457 }
1458 }
J. Suter3a5e9032005-08-09 20:28:46 -07001459
Al Viroe8db8c92006-11-08 00:28:44 -08001460 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_STOP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 stop_bits = __get_rpn_stop_bits(rpn->line_settings);
1462 if (stop_bits != RFCOMM_RPN_STOP_1) {
1463 BT_DBG("RPN stop bits mismatch 0x%x", stop_bits);
1464 stop_bits = RFCOMM_RPN_STOP_1;
1465 rpn_mask ^= RFCOMM_RPN_PM_STOP;
1466 }
1467 }
J. Suter3a5e9032005-08-09 20:28:46 -07001468
Al Viroe8db8c92006-11-08 00:28:44 -08001469 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_PARITY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 parity = __get_rpn_parity(rpn->line_settings);
1471 if (parity != RFCOMM_RPN_PARITY_NONE) {
1472 BT_DBG("RPN parity mismatch 0x%x", parity);
1473 parity = RFCOMM_RPN_PARITY_NONE;
1474 rpn_mask ^= RFCOMM_RPN_PM_PARITY;
1475 }
1476 }
J. Suter3a5e9032005-08-09 20:28:46 -07001477
Al Viroe8db8c92006-11-08 00:28:44 -08001478 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_FLOW)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 flow_ctrl = rpn->flow_ctrl;
1480 if (flow_ctrl != RFCOMM_RPN_FLOW_NONE) {
1481 BT_DBG("RPN flow ctrl mismatch 0x%x", flow_ctrl);
1482 flow_ctrl = RFCOMM_RPN_FLOW_NONE;
1483 rpn_mask ^= RFCOMM_RPN_PM_FLOW;
1484 }
1485 }
J. Suter3a5e9032005-08-09 20:28:46 -07001486
Al Viroe8db8c92006-11-08 00:28:44 -08001487 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 xon_char = rpn->xon_char;
1489 if (xon_char != RFCOMM_RPN_XON_CHAR) {
1490 BT_DBG("RPN XON char mismatch 0x%x", xon_char);
1491 xon_char = RFCOMM_RPN_XON_CHAR;
1492 rpn_mask ^= RFCOMM_RPN_PM_XON;
1493 }
1494 }
J. Suter3a5e9032005-08-09 20:28:46 -07001495
Al Viroe8db8c92006-11-08 00:28:44 -08001496 if (rpn->param_mask & cpu_to_le16(RFCOMM_RPN_PM_XOFF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 xoff_char = rpn->xoff_char;
1498 if (xoff_char != RFCOMM_RPN_XOFF_CHAR) {
1499 BT_DBG("RPN XOFF char mismatch 0x%x", xoff_char);
1500 xoff_char = RFCOMM_RPN_XOFF_CHAR;
1501 rpn_mask ^= RFCOMM_RPN_PM_XOFF;
1502 }
1503 }
1504
1505rpn_out:
J. Suter3a5e9032005-08-09 20:28:46 -07001506 rfcomm_send_rpn(s, 0, dlci, bit_rate, data_bits, stop_bits,
1507 parity, flow_ctrl, xon_char, xoff_char, rpn_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 return 0;
1510}
1511
1512static int rfcomm_recv_rls(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1513{
1514 struct rfcomm_rls *rls = (void *) skb->data;
1515 u8 dlci = __get_dlci(rls->dlci);
1516
1517 BT_DBG("dlci %d cr %d status 0x%x", dlci, cr, rls->status);
J. Suter3a5e9032005-08-09 20:28:46 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (!cr)
1520 return 0;
1521
J. Suter3a5e9032005-08-09 20:28:46 -07001522 /* We should probably do something with this information here. But
1523 * for now it's sufficient just to reply -- Bluetooth 1.1 says it's
1524 * mandatory to recognise and respond to RLS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 rfcomm_send_rls(s, 0, dlci, rls->status);
1527
1528 return 0;
1529}
1530
1531static int rfcomm_recv_msc(struct rfcomm_session *s, int cr, struct sk_buff *skb)
1532{
1533 struct rfcomm_msc *msc = (void *) skb->data;
1534 struct rfcomm_dlc *d;
1535 u8 dlci = __get_dlci(msc->dlci);
1536
1537 BT_DBG("dlci %d cr %d v24 0x%x", dlci, cr, msc->v24_sig);
1538
1539 d = rfcomm_dlc_get(s, dlci);
J. Suter3a5e9032005-08-09 20:28:46 -07001540 if (!d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return 0;
1542
1543 if (cr) {
1544 if (msc->v24_sig & RFCOMM_V24_FC && !d->cfc)
1545 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1546 else
1547 clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
J. Suter3a5e9032005-08-09 20:28:46 -07001548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 rfcomm_dlc_lock(d);
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +02001550
1551 d->remote_v24_sig = msc->v24_sig;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (d->modem_status)
1554 d->modem_status(d, msc->v24_sig);
Marcel Holtmann8b6b3da2008-07-14 20:13:52 +02001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 rfcomm_dlc_unlock(d);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 rfcomm_send_msc(s, 0, dlci, msc->v24_sig);
1559
1560 d->mscex |= RFCOMM_MSCEX_RX;
J. Suter3a5e9032005-08-09 20:28:46 -07001561 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 d->mscex |= RFCOMM_MSCEX_TX;
1563
1564 return 0;
1565}
1566
1567static int rfcomm_recv_mcc(struct rfcomm_session *s, struct sk_buff *skb)
1568{
1569 struct rfcomm_mcc *mcc = (void *) skb->data;
1570 u8 type, cr, len;
1571
1572 cr = __test_cr(mcc->type);
1573 type = __get_mcc_type(mcc->type);
1574 len = __get_mcc_len(mcc->len);
1575
1576 BT_DBG("%p type 0x%x cr %d", s, type, cr);
1577
1578 skb_pull(skb, 2);
1579
1580 switch (type) {
1581 case RFCOMM_PN:
1582 rfcomm_recv_pn(s, cr, skb);
1583 break;
1584
1585 case RFCOMM_RPN:
1586 rfcomm_recv_rpn(s, cr, len, skb);
1587 break;
1588
1589 case RFCOMM_RLS:
1590 rfcomm_recv_rls(s, cr, skb);
1591 break;
1592
1593 case RFCOMM_MSC:
1594 rfcomm_recv_msc(s, cr, skb);
1595 break;
1596
1597 case RFCOMM_FCOFF:
1598 if (cr) {
1599 set_bit(RFCOMM_TX_THROTTLED, &s->flags);
1600 rfcomm_send_fcoff(s, 0);
1601 }
1602 break;
1603
1604 case RFCOMM_FCON:
1605 if (cr) {
1606 clear_bit(RFCOMM_TX_THROTTLED, &s->flags);
1607 rfcomm_send_fcon(s, 0);
1608 }
1609 break;
1610
1611 case RFCOMM_TEST:
1612 if (cr)
1613 rfcomm_send_test(s, 0, skb->data, skb->len);
1614 break;
1615
1616 case RFCOMM_NSC:
1617 break;
1618
1619 default:
1620 BT_ERR("Unknown control type 0x%02x", type);
1621 rfcomm_send_nsc(s, cr, type);
1622 break;
1623 }
1624 return 0;
1625}
1626
1627static int rfcomm_recv_data(struct rfcomm_session *s, u8 dlci, int pf, struct sk_buff *skb)
1628{
1629 struct rfcomm_dlc *d;
1630
1631 BT_DBG("session %p state %ld dlci %d pf %d", s, s->state, dlci, pf);
1632
1633 d = rfcomm_dlc_get(s, dlci);
1634 if (!d) {
1635 rfcomm_send_dm(s, dlci);
1636 goto drop;
1637 }
1638
1639 if (pf && d->cfc) {
1640 u8 credits = *(u8 *) skb->data; skb_pull(skb, 1);
1641
1642 d->tx_credits += credits;
1643 if (d->tx_credits)
1644 clear_bit(RFCOMM_TX_THROTTLED, &d->flags);
1645 }
1646
1647 if (skb->len && d->state == BT_CONNECTED) {
1648 rfcomm_dlc_lock(d);
1649 d->rx_credits--;
1650 d->data_ready(d, skb);
1651 rfcomm_dlc_unlock(d);
1652 return 0;
1653 }
1654
1655drop:
1656 kfree_skb(skb);
1657 return 0;
1658}
1659
1660static int rfcomm_recv_frame(struct rfcomm_session *s, struct sk_buff *skb)
1661{
1662 struct rfcomm_hdr *hdr = (void *) skb->data;
1663 u8 type, dlci, fcs;
1664
1665 dlci = __get_dlci(hdr->addr);
1666 type = __get_type(hdr->ctrl);
1667
1668 /* Trim FCS */
1669 skb->len--; skb->tail--;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001670 fcs = *(u8 *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 if (__check_fcs(skb->data, type, fcs)) {
1673 BT_ERR("bad checksum in packet");
1674 kfree_skb(skb);
1675 return -EILSEQ;
1676 }
1677
1678 if (__test_ea(hdr->len))
1679 skb_pull(skb, 3);
1680 else
1681 skb_pull(skb, 4);
1682
1683 switch (type) {
1684 case RFCOMM_SABM:
1685 if (__test_pf(hdr->ctrl))
1686 rfcomm_recv_sabm(s, dlci);
1687 break;
1688
1689 case RFCOMM_DISC:
1690 if (__test_pf(hdr->ctrl))
1691 rfcomm_recv_disc(s, dlci);
1692 break;
1693
1694 case RFCOMM_UA:
1695 if (__test_pf(hdr->ctrl))
1696 rfcomm_recv_ua(s, dlci);
1697 break;
1698
1699 case RFCOMM_DM:
1700 rfcomm_recv_dm(s, dlci);
1701 break;
1702
1703 case RFCOMM_UIH:
1704 if (dlci)
1705 return rfcomm_recv_data(s, dlci, __test_pf(hdr->ctrl), skb);
1706
1707 rfcomm_recv_mcc(s, skb);
1708 break;
1709
1710 default:
Andrei Emeltchenko5017d8d2010-09-08 16:26:53 +03001711 BT_ERR("Unknown packet type 0x%02x", type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 break;
1713 }
1714 kfree_skb(skb);
1715 return 0;
1716}
1717
1718/* ---- Connection and data processing ---- */
1719
1720static void rfcomm_process_connect(struct rfcomm_session *s)
1721{
1722 struct rfcomm_dlc *d;
1723 struct list_head *p, *n;
1724
1725 BT_DBG("session %p state %ld", s, s->state);
1726
1727 list_for_each_safe(p, n, &s->dlcs) {
1728 d = list_entry(p, struct rfcomm_dlc, list);
1729 if (d->state == BT_CONFIG) {
1730 d->mtu = s->mtu;
Marcel Holtmann9f2c8a02009-01-15 21:58:40 +01001731 if (rfcomm_check_security(d)) {
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001732 rfcomm_send_pn(s, 1, d);
1733 } else {
Marcel Holtmann77db1982008-07-14 20:13:45 +02001734 set_bit(RFCOMM_AUTH_PENDING, &d->flags);
1735 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738 }
1739}
1740
1741/* Send data queued for the DLC.
1742 * Return number of frames left in the queue.
1743 */
1744static inline int rfcomm_process_tx(struct rfcomm_dlc *d)
1745{
1746 struct sk_buff *skb;
1747 int err;
1748
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001749 BT_DBG("dlc %p state %ld cfc %d rx_credits %d tx_credits %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 d, d->state, d->cfc, d->rx_credits, d->tx_credits);
1751
1752 /* Send pending MSC */
1753 if (test_and_clear_bit(RFCOMM_MSC_PENDING, &d->flags))
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001754 rfcomm_send_msc(d->session, 1, d->dlci, d->v24_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 if (d->cfc) {
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001757 /* CFC enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * Give them some credits */
1759 if (!test_bit(RFCOMM_RX_THROTTLED, &d->flags) &&
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001760 d->rx_credits <= (d->cfc >> 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 rfcomm_send_credits(d->session, d->addr, d->cfc - d->rx_credits);
1762 d->rx_credits = d->cfc;
1763 }
1764 } else {
1765 /* CFC disabled.
1766 * Give ourselves some credits */
1767 d->tx_credits = 5;
1768 }
1769
1770 if (test_bit(RFCOMM_TX_THROTTLED, &d->flags))
1771 return skb_queue_len(&d->tx_queue);
1772
1773 while (d->tx_credits && (skb = skb_dequeue(&d->tx_queue))) {
1774 err = rfcomm_send_frame(d->session, skb->data, skb->len);
1775 if (err < 0) {
1776 skb_queue_head(&d->tx_queue, skb);
1777 break;
1778 }
1779 kfree_skb(skb);
1780 d->tx_credits--;
1781 }
1782
1783 if (d->cfc && !d->tx_credits) {
1784 /* We're out of TX credits.
1785 * Set TX_THROTTLED flag to avoid unnesary wakeups by dlc_send. */
1786 set_bit(RFCOMM_TX_THROTTLED, &d->flags);
1787 }
1788
1789 return skb_queue_len(&d->tx_queue);
1790}
1791
1792static inline void rfcomm_process_dlcs(struct rfcomm_session *s)
1793{
1794 struct rfcomm_dlc *d;
1795 struct list_head *p, *n;
1796
1797 BT_DBG("session %p state %ld", s, s->state);
1798
1799 list_for_each_safe(p, n, &s->dlcs) {
1800 d = list_entry(p, struct rfcomm_dlc, list);
1801
1802 if (test_bit(RFCOMM_TIMED_OUT, &d->flags)) {
1803 __rfcomm_dlc_close(d, ETIMEDOUT);
1804 continue;
1805 }
1806
1807 if (test_and_clear_bit(RFCOMM_AUTH_ACCEPT, &d->flags)) {
1808 rfcomm_dlc_clear_timer(d);
Marcel Holtmann77db1982008-07-14 20:13:45 +02001809 if (d->out) {
1810 rfcomm_send_pn(s, 1, d);
1811 rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001812 } else {
1813 if (d->defer_setup) {
1814 set_bit(RFCOMM_DEFER_SETUP, &d->flags);
1815 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
Marcel Holtmann8bf47942009-02-16 02:59:49 +01001816
1817 rfcomm_dlc_lock(d);
1818 d->state = BT_CONNECT2;
1819 d->state_change(d, 0);
1820 rfcomm_dlc_unlock(d);
Marcel Holtmannbb23c0a2009-01-15 21:56:48 +01001821 } else
1822 rfcomm_dlc_accept(d);
1823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 continue;
1825 } else if (test_and_clear_bit(RFCOMM_AUTH_REJECT, &d->flags)) {
1826 rfcomm_dlc_clear_timer(d);
Marcel Holtmann77db1982008-07-14 20:13:45 +02001827 if (!d->out)
1828 rfcomm_send_dm(s, d->dlci);
1829 else
1830 d->state = BT_CLOSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 __rfcomm_dlc_close(d, ECONNREFUSED);
1832 continue;
1833 }
1834
Jaikumar Ganesh6e1031a2009-02-02 18:03:57 -08001835 if (test_bit(RFCOMM_SEC_PENDING, &d->flags))
1836 continue;
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (test_bit(RFCOMM_TX_THROTTLED, &s->flags))
1839 continue;
1840
1841 if ((d->state == BT_CONNECTED || d->state == BT_DISCONN) &&
Marcel Holtmann77db1982008-07-14 20:13:45 +02001842 d->mscex == RFCOMM_MSCEX_OK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 rfcomm_process_tx(d);
1844 }
1845}
1846
1847static inline void rfcomm_process_rx(struct rfcomm_session *s)
1848{
1849 struct socket *sock = s->sock;
1850 struct sock *sk = sock->sk;
1851 struct sk_buff *skb;
1852
1853 BT_DBG("session %p state %ld qlen %d", s, s->state, skb_queue_len(&sk->sk_receive_queue));
1854
1855 /* Get data directly from socket receive queue without copying it. */
1856 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
1857 skb_orphan(skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858 if (!skb_linearize(skb))
1859 rfcomm_recv_frame(s, skb);
1860 else
1861 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
1863
1864 if (sk->sk_state == BT_CLOSED) {
1865 if (!s->initiator)
1866 rfcomm_session_put(s);
1867
1868 rfcomm_session_close(s, sk->sk_err);
1869 }
1870}
1871
1872static inline void rfcomm_accept_connection(struct rfcomm_session *s)
1873{
1874 struct socket *sock = s->sock, *nsock;
1875 int err;
1876
1877 /* Fast check for a new connection.
1878 * Avoids unnesesary socket allocations. */
1879 if (list_empty(&bt_sk(sock->sk)->accept_q))
1880 return;
1881
1882 BT_DBG("session %p", s);
1883
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02001884 err = kernel_accept(sock, &nsock, O_NONBLOCK);
1885 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return;
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* Set our callbacks */
1889 nsock->sk->sk_data_ready = rfcomm_l2data_ready;
1890 nsock->sk->sk_state_change = rfcomm_l2state_change;
1891
1892 s = rfcomm_session_add(nsock, BT_OPEN);
1893 if (s) {
1894 rfcomm_session_hold(s);
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001895
1896 /* We should adjust MTU on incoming sessions.
Mallikarjuna GB2da40bf2011-09-06 17:02:36 +05301897 * L2CAP MTU minus UIH header and FCS.
1898 * Need to accomodate 1 Byte credits information */
1899 s->mtu = min(l2cap_pi(nsock->sk)->omtu,
1900 l2cap_pi(nsock->sk)->imtu) - RFCOMM_HDR_SIZE;
Marcel Holtmann98bcd082006-07-14 11:42:12 +02001901
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +03001902 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 } else
1904 sock_release(nsock);
1905}
1906
1907static inline void rfcomm_check_connection(struct rfcomm_session *s)
1908{
1909 struct sock *sk = s->sock->sk;
1910
1911 BT_DBG("%p state %ld", s, s->state);
1912
Andrei Emeltchenko285b4e92010-12-01 16:58:23 +02001913 switch (sk->sk_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 case BT_CONNECTED:
1915 s->state = BT_CONNECT;
1916
1917 /* We can adjust MTU on outgoing sessions.
Mallikarjuna GB2da40bf2011-09-06 17:02:36 +05301918 * L2CAP MTU minus UIH header, Credits and FCS. */
1919 s->mtu = min(l2cap_pi(sk)->omtu, l2cap_pi(sk)->imtu) -
1920 RFCOMM_HDR_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 rfcomm_send_sabm(s, 0);
1923 break;
1924
1925 case BT_CLOSED:
1926 s->state = BT_CLOSED;
1927 rfcomm_session_close(s, sk->sk_err);
1928 break;
1929 }
1930}
1931
1932static inline void rfcomm_process_sessions(void)
1933{
1934 struct list_head *p, *n;
1935
1936 rfcomm_lock();
1937
1938 list_for_each_safe(p, n, &session_list) {
1939 struct rfcomm_session *s;
1940 s = list_entry(p, struct rfcomm_session, list);
1941
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -03001942 if (test_and_clear_bit(RFCOMM_TIMED_OUT, &s->flags)) {
1943 s->state = BT_DISCONN;
1944 rfcomm_send_disc(s, 0);
Marcel Holtmann485f1ef2010-02-03 15:52:18 -08001945 rfcomm_session_put(s);
Luiz Augusto von Dentz9e726b12009-07-15 13:50:58 -03001946 continue;
1947 }
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (s->state == BT_LISTEN) {
1950 rfcomm_accept_connection(s);
1951 continue;
1952 }
1953
1954 rfcomm_session_hold(s);
1955
1956 switch (s->state) {
1957 case BT_BOUND:
1958 rfcomm_check_connection(s);
1959 break;
1960
1961 default:
1962 rfcomm_process_rx(s);
1963 break;
1964 }
1965
1966 rfcomm_process_dlcs(s);
1967
1968 rfcomm_session_put(s);
1969 }
1970
1971 rfcomm_unlock();
1972}
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974static int rfcomm_add_listener(bdaddr_t *ba)
1975{
1976 struct sockaddr_l2 addr;
1977 struct socket *sock;
1978 struct sock *sk;
1979 struct rfcomm_session *s;
1980 int err = 0;
1981
1982 /* Create socket */
1983 err = rfcomm_l2sock_create(&sock);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001984 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 BT_ERR("Create socket failed %d", err);
1986 return err;
1987 }
1988
1989 /* Bind socket */
1990 bacpy(&addr.l2_bdaddr, ba);
1991 addr.l2_family = AF_BLUETOOTH;
Marcel Holtmannb4324b52009-06-07 18:06:51 +02001992 addr.l2_psm = cpu_to_le16(RFCOMM_PSM);
Marcel Holtmann37e62f52009-02-17 21:49:33 +01001993 addr.l2_cid = 0;
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02001994 err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (err < 0) {
1996 BT_ERR("Bind failed %d", err);
1997 goto failed;
1998 }
1999
2000 /* Set L2CAP options */
2001 sk = sock->sk;
2002 lock_sock(sk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003 l2cap_pi(sk)->imtu = l2cap_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 release_sock(sk);
2005
2006 /* Start listening on the socket */
Marcel Holtmann48db9ca2007-05-05 00:36:06 +02002007 err = kernel_listen(sock, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (err) {
2009 BT_ERR("Listen failed %d", err);
2010 goto failed;
2011 }
2012
2013 /* Add listening session */
2014 s = rfcomm_session_add(sock, BT_LISTEN);
2015 if (!s)
2016 goto failed;
2017
2018 rfcomm_session_hold(s);
2019 return 0;
2020failed:
2021 sock_release(sock);
2022 return err;
2023}
2024
2025static void rfcomm_kill_listener(void)
2026{
2027 struct rfcomm_session *s;
2028 struct list_head *p, *n;
2029
2030 BT_DBG("");
2031
2032 list_for_each_safe(p, n, &session_list) {
2033 s = list_entry(p, struct rfcomm_session, list);
2034 rfcomm_session_del(s);
2035 }
2036}
2037
2038static int rfcomm_run(void *unused)
2039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 BT_DBG("");
2041
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002042 set_user_nice(current, -10);
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 rfcomm_add_listener(BDADDR_ANY);
2045
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002046 while (!kthread_should_stop()) {
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002047 set_current_state(TASK_INTERRUPTIBLE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002048 if (!test_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event)) {
2049 /* No pending events. Let's sleep.
2050 * Incoming connections and data will wake us up. */
2051 schedule();
2052 }
2053 set_current_state(TASK_RUNNING);
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002054
2055 /* Process stuff */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002056 clear_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event);
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002057 rfcomm_process_sessions();
2058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 rfcomm_kill_listener();
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return 0;
2063}
2064
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002065static void rfcomm_security_cfm(struct hci_conn *conn, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 struct rfcomm_session *s;
2068 struct rfcomm_dlc *d;
2069 struct list_head *p, *n;
2070
2071 BT_DBG("conn %p status 0x%02x encrypt 0x%02x", conn, status, encrypt);
2072
2073 s = rfcomm_session_get(&conn->hdev->bdaddr, &conn->dst);
2074 if (!s)
2075 return;
2076
2077 rfcomm_session_hold(s);
2078
2079 list_for_each_safe(p, n, &s->dlcs) {
2080 d = list_entry(p, struct rfcomm_dlc, list);
2081
Marcel Holtmann8c84b832009-01-16 08:17:51 +01002082 if (test_and_clear_bit(RFCOMM_SEC_PENDING, &d->flags)) {
2083 rfcomm_dlc_clear_timer(d);
2084 if (status || encrypt == 0x00) {
2085 __rfcomm_dlc_close(d, ECONNREFUSED);
2086 continue;
2087 }
2088 }
2089
2090 if (d->state == BT_CONNECTED && !status && encrypt == 0x00) {
2091 if (d->sec_level == BT_SECURITY_MEDIUM) {
2092 set_bit(RFCOMM_SEC_PENDING, &d->flags);
2093 rfcomm_dlc_set_timer(d, RFCOMM_AUTH_TIMEOUT);
2094 continue;
2095 } else if (d->sec_level == BT_SECURITY_HIGH) {
2096 __rfcomm_dlc_close(d, ECONNREFUSED);
2097 continue;
2098 }
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02002099 }
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (!test_and_clear_bit(RFCOMM_AUTH_PENDING, &d->flags))
2102 continue;
2103
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002104 if (!status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 set_bit(RFCOMM_AUTH_ACCEPT, &d->flags);
2106 else
2107 set_bit(RFCOMM_AUTH_REJECT, &d->flags);
2108 }
2109
2110 rfcomm_session_put(s);
2111
Andrei Emeltchenko534c92f2010-10-01 12:05:11 +03002112 rfcomm_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115static struct hci_cb rfcomm_cb = {
2116 .name = "RFCOMM",
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01002117 .security_cfm = rfcomm_security_cfm
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118};
2119
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002120static int rfcomm_dlc_debugfs_show(struct seq_file *f, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 struct rfcomm_session *s;
2123 struct list_head *pp, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 rfcomm_lock();
2126
2127 list_for_each(p, &session_list) {
2128 s = list_entry(p, struct rfcomm_session, list);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002129 list_for_each(pp, &s->dlcs) {
2130 struct sock *sk = s->sock->sk;
2131 struct rfcomm_dlc *d = list_entry(pp, struct rfcomm_dlc, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002133 seq_printf(f, "%s %s %ld %d %d %d %d\n",
2134 batostr(&bt_sk(sk)->src),
2135 batostr(&bt_sk(sk)->dst),
2136 d->state, d->dlci, d->mtu,
2137 d->rx_credits, d->tx_credits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 rfcomm_unlock();
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002142
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002146static int rfcomm_dlc_debugfs_open(struct inode *inode, struct file *file)
2147{
2148 return single_open(file, rfcomm_dlc_debugfs_show, inode->i_private);
2149}
2150
2151static const struct file_operations rfcomm_dlc_debugfs_fops = {
2152 .open = rfcomm_dlc_debugfs_open,
2153 .read = seq_read,
2154 .llseek = seq_lseek,
2155 .release = single_release,
2156};
2157
2158static struct dentry *rfcomm_dlc_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160/* ---- Initialization ---- */
2161static int __init rfcomm_init(void)
2162{
Marcel Holtmann52d18342009-08-22 14:49:36 -07002163 int err;
Dave Youngaf0d3b12009-08-03 04:26:16 +00002164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 hci_register_cb(&rfcomm_cb);
2166
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002167 rfcomm_thread = kthread_run(rfcomm_run, NULL, "krfcommd");
2168 if (IS_ERR(rfcomm_thread)) {
Marcel Holtmann52d18342009-08-22 14:49:36 -07002169 err = PTR_ERR(rfcomm_thread);
2170 goto unregister;
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002173 if (bt_debugfs) {
2174 rfcomm_dlc_debugfs = debugfs_create_file("rfcomm_dlc", 0444,
2175 bt_debugfs, NULL, &rfcomm_dlc_debugfs_fops);
2176 if (!rfcomm_dlc_debugfs)
2177 BT_ERR("Failed to create RFCOMM debug file");
2178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Marcel Holtmann52d18342009-08-22 14:49:36 -07002180 err = rfcomm_init_ttys();
2181 if (err < 0)
2182 goto stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Marcel Holtmann52d18342009-08-22 14:49:36 -07002184 err = rfcomm_init_sockets();
2185 if (err < 0)
2186 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002188 BT_INFO("RFCOMM ver %s", VERSION);
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return 0;
Dave Youngaf0d3b12009-08-03 04:26:16 +00002191
Marcel Holtmann52d18342009-08-22 14:49:36 -07002192cleanup:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002193 rfcomm_cleanup_ttys();
Marcel Holtmann52d18342009-08-22 14:49:36 -07002194
2195stop:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002196 kthread_stop(rfcomm_thread);
Marcel Holtmann52d18342009-08-22 14:49:36 -07002197
2198unregister:
Dave Youngaf0d3b12009-08-03 04:26:16 +00002199 hci_unregister_cb(&rfcomm_cb);
2200
Marcel Holtmann52d18342009-08-22 14:49:36 -07002201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
2204static void __exit rfcomm_exit(void)
2205{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01002206 debugfs_remove(rfcomm_dlc_debugfs);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 hci_unregister_cb(&rfcomm_cb);
2209
Marcel Holtmanna524ecc2007-10-20 21:37:20 +02002210 kthread_stop(rfcomm_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 rfcomm_cleanup_ttys();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 rfcomm_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217module_init(rfcomm_init);
2218module_exit(rfcomm_exit);
2219
Marcel Holtmann7c2660b2006-07-03 10:02:51 +02002220module_param(disable_cfc, bool, 0644);
2221MODULE_PARM_DESC(disable_cfc, "Disable credit based flow control");
2222
Marcel Holtmann98bcd082006-07-14 11:42:12 +02002223module_param(channel_mtu, int, 0644);
2224MODULE_PARM_DESC(channel_mtu, "Default MTU for the RFCOMM channel");
2225
Marcel Holtmann56f3a402006-02-13 11:39:57 +01002226module_param(l2cap_mtu, uint, 0644);
2227MODULE_PARM_DESC(l2cap_mtu, "Default MTU for the L2CAP connection");
2228
Marcel Holtmanneae38ee2009-10-05 12:23:48 +02002229module_param(l2cap_ertm, bool, 0644);
2230MODULE_PARM_DESC(l2cap_ertm, "Use L2CAP ERTM mode for connection");
2231
Marcel Holtmann63fbd242008-08-18 13:23:53 +02002232MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233MODULE_DESCRIPTION("Bluetooth RFCOMM ver " VERSION);
2234MODULE_VERSION(VERSION);
2235MODULE_LICENSE("GPL");
2236MODULE_ALIAS("bt-proto-3");