xref: /openbmc/linux/net/bluetooth/l2cap_core.c (revision e05dcc32)
10a708f8fSGustavo F. Padovan /*
20a708f8fSGustavo F. Padovan    BlueZ - Bluetooth protocol stack for Linux
30a708f8fSGustavo F. Padovan    Copyright (C) 2000-2001 Qualcomm Incorporated
40a708f8fSGustavo F. Padovan    Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
50a708f8fSGustavo F. Padovan    Copyright (C) 2010 Google Inc.
6590051deSGustavo F. Padovan    Copyright (C) 2011 ProFUSION Embedded Systems
70a708f8fSGustavo F. Padovan 
80a708f8fSGustavo F. Padovan    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
90a708f8fSGustavo F. Padovan 
100a708f8fSGustavo F. Padovan    This program is free software; you can redistribute it and/or modify
110a708f8fSGustavo F. Padovan    it under the terms of the GNU General Public License version 2 as
120a708f8fSGustavo F. Padovan    published by the Free Software Foundation;
130a708f8fSGustavo F. Padovan 
140a708f8fSGustavo F. Padovan    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
150a708f8fSGustavo F. Padovan    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
160a708f8fSGustavo F. Padovan    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
170a708f8fSGustavo F. Padovan    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
180a708f8fSGustavo F. Padovan    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
190a708f8fSGustavo F. Padovan    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
200a708f8fSGustavo F. Padovan    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
210a708f8fSGustavo F. Padovan    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
220a708f8fSGustavo F. Padovan 
230a708f8fSGustavo F. Padovan    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
240a708f8fSGustavo F. Padovan    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
250a708f8fSGustavo F. Padovan    SOFTWARE IS DISCLAIMED.
260a708f8fSGustavo F. Padovan */
270a708f8fSGustavo F. Padovan 
28bb58f747SGustavo F. Padovan /* Bluetooth L2CAP core. */
290a708f8fSGustavo F. Padovan 
300a708f8fSGustavo F. Padovan #include <linux/module.h>
310a708f8fSGustavo F. Padovan 
320a708f8fSGustavo F. Padovan #include <linux/types.h>
330a708f8fSGustavo F. Padovan #include <linux/capability.h>
340a708f8fSGustavo F. Padovan #include <linux/errno.h>
350a708f8fSGustavo F. Padovan #include <linux/kernel.h>
360a708f8fSGustavo F. Padovan #include <linux/sched.h>
370a708f8fSGustavo F. Padovan #include <linux/slab.h>
380a708f8fSGustavo F. Padovan #include <linux/poll.h>
390a708f8fSGustavo F. Padovan #include <linux/fcntl.h>
400a708f8fSGustavo F. Padovan #include <linux/init.h>
410a708f8fSGustavo F. Padovan #include <linux/interrupt.h>
420a708f8fSGustavo F. Padovan #include <linux/socket.h>
430a708f8fSGustavo F. Padovan #include <linux/skbuff.h>
440a708f8fSGustavo F. Padovan #include <linux/list.h>
450a708f8fSGustavo F. Padovan #include <linux/device.h>
460a708f8fSGustavo F. Padovan #include <linux/debugfs.h>
470a708f8fSGustavo F. Padovan #include <linux/seq_file.h>
480a708f8fSGustavo F. Padovan #include <linux/uaccess.h>
490a708f8fSGustavo F. Padovan #include <linux/crc16.h>
500a708f8fSGustavo F. Padovan #include <net/sock.h>
510a708f8fSGustavo F. Padovan 
520a708f8fSGustavo F. Padovan #include <asm/system.h>
530a708f8fSGustavo F. Padovan #include <asm/unaligned.h>
540a708f8fSGustavo F. Padovan 
550a708f8fSGustavo F. Padovan #include <net/bluetooth/bluetooth.h>
560a708f8fSGustavo F. Padovan #include <net/bluetooth/hci_core.h>
570a708f8fSGustavo F. Padovan #include <net/bluetooth/l2cap.h>
58b501d6a1SAnderson Briglia #include <net/bluetooth/smp.h>
590a708f8fSGustavo F. Padovan 
60eb939922SRusty Russell bool disable_ertm;
610a708f8fSGustavo F. Padovan 
620a708f8fSGustavo F. Padovan static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN;
6350a147cdSMat Martineau static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, };
640a708f8fSGustavo F. Padovan 
65b5ad8b7fSJohannes Berg static LIST_HEAD(chan_list);
66b5ad8b7fSJohannes Berg static DEFINE_RWLOCK(chan_list_lock);
670a708f8fSGustavo F. Padovan 
680a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
690a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data);
704519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
714519de9aSGustavo F. Padovan 								void *data);
72710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
734519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn,
744519de9aSGustavo F. Padovan 				struct l2cap_chan *chan, int err);
750a708f8fSGustavo F. Padovan 
760a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */
7771ba0e56SGustavo F. Padovan 
78baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
790a708f8fSGustavo F. Padovan {
803d57dc68SGustavo F. Padovan 	struct l2cap_chan *c, *r = NULL;
81baa7e1faSGustavo F. Padovan 
823d57dc68SGustavo F. Padovan 	rcu_read_lock();
833d57dc68SGustavo F. Padovan 
843d57dc68SGustavo F. Padovan 	list_for_each_entry_rcu(c, &conn->chan_l, list) {
853d57dc68SGustavo F. Padovan 		if (c->dcid == cid) {
863d57dc68SGustavo F. Padovan 			r = c;
873d57dc68SGustavo F. Padovan 			break;
880a708f8fSGustavo F. Padovan 		}
893d57dc68SGustavo F. Padovan 	}
903d57dc68SGustavo F. Padovan 
913d57dc68SGustavo F. Padovan 	rcu_read_unlock();
923d57dc68SGustavo F. Padovan 	return r;
93baa7e1faSGustavo F. Padovan }
940a708f8fSGustavo F. Padovan 
95baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
960a708f8fSGustavo F. Padovan {
973d57dc68SGustavo F. Padovan 	struct l2cap_chan *c, *r = NULL;
98baa7e1faSGustavo F. Padovan 
993d57dc68SGustavo F. Padovan 	rcu_read_lock();
1003d57dc68SGustavo F. Padovan 
1013d57dc68SGustavo F. Padovan 	list_for_each_entry_rcu(c, &conn->chan_l, list) {
1023d57dc68SGustavo F. Padovan 		if (c->scid == cid) {
1033d57dc68SGustavo F. Padovan 			r = c;
1043d57dc68SGustavo F. Padovan 			break;
1050a708f8fSGustavo F. Padovan 		}
1063d57dc68SGustavo F. Padovan 	}
1073d57dc68SGustavo F. Padovan 
1083d57dc68SGustavo F. Padovan 	rcu_read_unlock();
1093d57dc68SGustavo F. Padovan 	return r;
110baa7e1faSGustavo F. Padovan }
1110a708f8fSGustavo F. Padovan 
1120a708f8fSGustavo F. Padovan /* Find channel with given SCID.
1130a708f8fSGustavo F. Padovan  * Returns locked socket */
114baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
1150a708f8fSGustavo F. Padovan {
11648454079SGustavo F. Padovan 	struct l2cap_chan *c;
117baa7e1faSGustavo F. Padovan 
118baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_scid(conn, cid);
11948454079SGustavo F. Padovan 	if (c)
120aa2ac881SGustavo F. Padovan 		lock_sock(c->sk);
12148454079SGustavo F. Padovan 	return c;
1220a708f8fSGustavo F. Padovan }
1230a708f8fSGustavo F. Padovan 
124baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1250a708f8fSGustavo F. Padovan {
1263d57dc68SGustavo F. Padovan 	struct l2cap_chan *c, *r = NULL;
127baa7e1faSGustavo F. Padovan 
1283d57dc68SGustavo F. Padovan 	rcu_read_lock();
1293d57dc68SGustavo F. Padovan 
1303d57dc68SGustavo F. Padovan 	list_for_each_entry_rcu(c, &conn->chan_l, list) {
1313d57dc68SGustavo F. Padovan 		if (c->ident == ident) {
1323d57dc68SGustavo F. Padovan 			r = c;
1333d57dc68SGustavo F. Padovan 			break;
1340a708f8fSGustavo F. Padovan 		}
1353d57dc68SGustavo F. Padovan 	}
1363d57dc68SGustavo F. Padovan 
1373d57dc68SGustavo F. Padovan 	rcu_read_unlock();
1383d57dc68SGustavo F. Padovan 	return r;
139baa7e1faSGustavo F. Padovan }
1400a708f8fSGustavo F. Padovan 
141baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1420a708f8fSGustavo F. Padovan {
14348454079SGustavo F. Padovan 	struct l2cap_chan *c;
144baa7e1faSGustavo F. Padovan 
145baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_ident(conn, ident);
14648454079SGustavo F. Padovan 	if (c)
147aa2ac881SGustavo F. Padovan 		lock_sock(c->sk);
14848454079SGustavo F. Padovan 	return c;
1490a708f8fSGustavo F. Padovan }
1500a708f8fSGustavo F. Padovan 
15123691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
1529e4425ffSGustavo F. Padovan {
15323691d75SGustavo F. Padovan 	struct l2cap_chan *c;
1549e4425ffSGustavo F. Padovan 
15523691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
15623691d75SGustavo F. Padovan 		if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src))
15723691d75SGustavo F. Padovan 			return c;
1589e4425ffSGustavo F. Padovan 	}
159250938cbSSzymon Janc 	return NULL;
160250938cbSSzymon Janc }
1619e4425ffSGustavo F. Padovan 
1629e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
1639e4425ffSGustavo F. Padovan {
16473b2ec18SGustavo F. Padovan 	int err;
16573b2ec18SGustavo F. Padovan 
166333055f2SGustavo F. Padovan 	write_lock(&chan_list_lock);
1679e4425ffSGustavo F. Padovan 
16823691d75SGustavo F. Padovan 	if (psm && __l2cap_global_chan_by_addr(psm, src)) {
16973b2ec18SGustavo F. Padovan 		err = -EADDRINUSE;
17073b2ec18SGustavo F. Padovan 		goto done;
1719e4425ffSGustavo F. Padovan 	}
1729e4425ffSGustavo F. Padovan 
17373b2ec18SGustavo F. Padovan 	if (psm) {
1749e4425ffSGustavo F. Padovan 		chan->psm = psm;
1759e4425ffSGustavo F. Padovan 		chan->sport = psm;
17673b2ec18SGustavo F. Padovan 		err = 0;
17773b2ec18SGustavo F. Padovan 	} else {
17873b2ec18SGustavo F. Padovan 		u16 p;
1799e4425ffSGustavo F. Padovan 
18073b2ec18SGustavo F. Padovan 		err = -EINVAL;
18173b2ec18SGustavo F. Padovan 		for (p = 0x1001; p < 0x1100; p += 2)
18223691d75SGustavo F. Padovan 			if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
18373b2ec18SGustavo F. Padovan 				chan->psm   = cpu_to_le16(p);
18473b2ec18SGustavo F. Padovan 				chan->sport = cpu_to_le16(p);
18573b2ec18SGustavo F. Padovan 				err = 0;
18673b2ec18SGustavo F. Padovan 				break;
18773b2ec18SGustavo F. Padovan 			}
18873b2ec18SGustavo F. Padovan 	}
18973b2ec18SGustavo F. Padovan 
19073b2ec18SGustavo F. Padovan done:
191333055f2SGustavo F. Padovan 	write_unlock(&chan_list_lock);
19273b2ec18SGustavo F. Padovan 	return err;
1939e4425ffSGustavo F. Padovan }
1949e4425ffSGustavo F. Padovan 
1959e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid)
1969e4425ffSGustavo F. Padovan {
197333055f2SGustavo F. Padovan 	write_lock(&chan_list_lock);
1989e4425ffSGustavo F. Padovan 
1999e4425ffSGustavo F. Padovan 	chan->scid = scid;
2009e4425ffSGustavo F. Padovan 
201333055f2SGustavo F. Padovan 	write_unlock(&chan_list_lock);
2029e4425ffSGustavo F. Padovan 
2039e4425ffSGustavo F. Padovan 	return 0;
2049e4425ffSGustavo F. Padovan }
2059e4425ffSGustavo F. Padovan 
206baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
2070a708f8fSGustavo F. Padovan {
2080a708f8fSGustavo F. Padovan 	u16 cid = L2CAP_CID_DYN_START;
2090a708f8fSGustavo F. Padovan 
2100a708f8fSGustavo F. Padovan 	for (; cid < L2CAP_CID_DYN_END; cid++) {
211baa7e1faSGustavo F. Padovan 		if (!__l2cap_get_chan_by_scid(conn, cid))
2120a708f8fSGustavo F. Padovan 			return cid;
2130a708f8fSGustavo F. Padovan 	}
2140a708f8fSGustavo F. Padovan 
2150a708f8fSGustavo F. Padovan 	return 0;
2160a708f8fSGustavo F. Padovan }
2170a708f8fSGustavo F. Padovan 
21889bc500eSGustavo F. Padovan static void l2cap_state_change(struct l2cap_chan *chan, int state)
21989bc500eSGustavo F. Padovan {
220badaaa00SGustavo F. Padovan 	BT_DBG("%p %s -> %s", chan, state_to_string(chan->state),
221badaaa00SGustavo F. Padovan 						state_to_string(state));
222badaaa00SGustavo F. Padovan 
22389bc500eSGustavo F. Padovan 	chan->state = state;
22489bc500eSGustavo F. Padovan 	chan->ops->state_change(chan->data, state);
22589bc500eSGustavo F. Padovan }
22689bc500eSGustavo F. Padovan 
227721c4181SGustavo F. Padovan static void l2cap_chan_timeout(struct work_struct *work)
228ab07801dSGustavo F. Padovan {
229721c4181SGustavo F. Padovan 	struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
230721c4181SGustavo F. Padovan 							chan_timer.work);
231ab07801dSGustavo F. Padovan 	struct sock *sk = chan->sk;
232ab07801dSGustavo F. Padovan 	int reason;
233ab07801dSGustavo F. Padovan 
234*e05dcc32SAndrei Emeltchenko 	BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
235ab07801dSGustavo F. Padovan 
236721c4181SGustavo F. Padovan 	lock_sock(sk);
237ab07801dSGustavo F. Padovan 
23889bc500eSGustavo F. Padovan 	if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
239ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
24089bc500eSGustavo F. Padovan 	else if (chan->state == BT_CONNECT &&
241ab07801dSGustavo F. Padovan 					chan->sec_level != BT_SECURITY_SDP)
242ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
243ab07801dSGustavo F. Padovan 	else
244ab07801dSGustavo F. Padovan 		reason = ETIMEDOUT;
245ab07801dSGustavo F. Padovan 
2460f852724SGustavo F. Padovan 	l2cap_chan_close(chan, reason);
247ab07801dSGustavo F. Padovan 
248721c4181SGustavo F. Padovan 	release_sock(sk);
249ab07801dSGustavo F. Padovan 
250ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
251371fd835SUlisses Furquim 	l2cap_chan_put(chan);
252ab07801dSGustavo F. Padovan }
253ab07801dSGustavo F. Padovan 
25423691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk)
2550a708f8fSGustavo F. Padovan {
25648454079SGustavo F. Padovan 	struct l2cap_chan *chan;
2570a708f8fSGustavo F. Padovan 
25848454079SGustavo F. Padovan 	chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
25948454079SGustavo F. Padovan 	if (!chan)
26048454079SGustavo F. Padovan 		return NULL;
2610a708f8fSGustavo F. Padovan 
26248454079SGustavo F. Padovan 	chan->sk = sk;
26348454079SGustavo F. Padovan 
264333055f2SGustavo F. Padovan 	write_lock(&chan_list_lock);
26523691d75SGustavo F. Padovan 	list_add(&chan->global_l, &chan_list);
266333055f2SGustavo F. Padovan 	write_unlock(&chan_list_lock);
26723691d75SGustavo F. Padovan 
268721c4181SGustavo F. Padovan 	INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout);
269ab07801dSGustavo F. Padovan 
27089bc500eSGustavo F. Padovan 	chan->state = BT_OPEN;
27189bc500eSGustavo F. Padovan 
27271ba0e56SGustavo F. Padovan 	atomic_set(&chan->refcnt, 1);
27371ba0e56SGustavo F. Padovan 
274abc545b8SSzymon Janc 	BT_DBG("sk %p chan %p", sk, chan);
275abc545b8SSzymon Janc 
27648454079SGustavo F. Padovan 	return chan;
2770a708f8fSGustavo F. Padovan }
2780a708f8fSGustavo F. Padovan 
27923691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan)
2806ff5abbfSGustavo F. Padovan {
281333055f2SGustavo F. Padovan 	write_lock(&chan_list_lock);
28223691d75SGustavo F. Padovan 	list_del(&chan->global_l);
283333055f2SGustavo F. Padovan 	write_unlock(&chan_list_lock);
28423691d75SGustavo F. Padovan 
285371fd835SUlisses Furquim 	l2cap_chan_put(chan);
2866ff5abbfSGustavo F. Padovan }
2876ff5abbfSGustavo F. Padovan 
2883d57dc68SGustavo F. Padovan static void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
2890a708f8fSGustavo F. Padovan {
2900a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
291fe4128e0SGustavo F. Padovan 			chan->psm, chan->dcid);
2920a708f8fSGustavo F. Padovan 
2939f5a0d7bSAndrei Emeltchenko 	conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
2940a708f8fSGustavo F. Padovan 
2958c1d787bSGustavo F. Padovan 	chan->conn = conn;
2960a708f8fSGustavo F. Padovan 
2975491120eSAndrei Emeltchenko 	switch (chan->chan_type) {
2985491120eSAndrei Emeltchenko 	case L2CAP_CHAN_CONN_ORIENTED:
299b62f328bSVille Tervo 		if (conn->hcon->type == LE_LINK) {
300b62f328bSVille Tervo 			/* LE connection */
3010c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_LE_DEFAULT_MTU;
302fe4128e0SGustavo F. Padovan 			chan->scid = L2CAP_CID_LE_DATA;
303fe4128e0SGustavo F. Padovan 			chan->dcid = L2CAP_CID_LE_DATA;
304b62f328bSVille Tervo 		} else {
3050a708f8fSGustavo F. Padovan 			/* Alloc CID for connection-oriented socket */
306fe4128e0SGustavo F. Padovan 			chan->scid = l2cap_alloc_cid(conn);
3070c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_DEFAULT_MTU;
308b62f328bSVille Tervo 		}
3095491120eSAndrei Emeltchenko 		break;
3105491120eSAndrei Emeltchenko 
3115491120eSAndrei Emeltchenko 	case L2CAP_CHAN_CONN_LESS:
3120a708f8fSGustavo F. Padovan 		/* Connectionless socket */
313fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_CONN_LESS;
314fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_CONN_LESS;
3150c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3165491120eSAndrei Emeltchenko 		break;
3175491120eSAndrei Emeltchenko 
3185491120eSAndrei Emeltchenko 	default:
3190a708f8fSGustavo F. Padovan 		/* Raw socket can send/recv signalling messages only */
320fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_SIGNALING;
321fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_SIGNALING;
3220c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3230a708f8fSGustavo F. Padovan 	}
3240a708f8fSGustavo F. Padovan 
3258f7975b1SAndrei Emeltchenko 	chan->local_id		= L2CAP_BESTEFFORT_ID;
3268f7975b1SAndrei Emeltchenko 	chan->local_stype	= L2CAP_SERV_BESTEFFORT;
3278f7975b1SAndrei Emeltchenko 	chan->local_msdu	= L2CAP_DEFAULT_MAX_SDU_SIZE;
3288f7975b1SAndrei Emeltchenko 	chan->local_sdu_itime	= L2CAP_DEFAULT_SDU_ITIME;
3298f7975b1SAndrei Emeltchenko 	chan->local_acc_lat	= L2CAP_DEFAULT_ACC_LAT;
3308f7975b1SAndrei Emeltchenko 	chan->local_flush_to	= L2CAP_DEFAULT_FLUSH_TO;
3318f7975b1SAndrei Emeltchenko 
332371fd835SUlisses Furquim 	l2cap_chan_hold(chan);
333baa7e1faSGustavo F. Padovan 
3343d57dc68SGustavo F. Padovan 	list_add_rcu(&chan->list, &conn->chan_l);
3350a708f8fSGustavo F. Padovan }
3360a708f8fSGustavo F. Padovan 
3370a708f8fSGustavo F. Padovan /* Delete channel.
3380a708f8fSGustavo F. Padovan  * Must be called on the locked socket. */
3394519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err)
3400a708f8fSGustavo F. Padovan {
34148454079SGustavo F. Padovan 	struct sock *sk = chan->sk;
3428c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
3430a708f8fSGustavo F. Padovan 	struct sock *parent = bt_sk(sk)->parent;
3440a708f8fSGustavo F. Padovan 
345c9b66675SGustavo F. Padovan 	__clear_chan_timer(chan);
3460a708f8fSGustavo F. Padovan 
34749208c9cSGustavo F. Padovan 	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
3480a708f8fSGustavo F. Padovan 
3490a708f8fSGustavo F. Padovan 	if (conn) {
350baa7e1faSGustavo F. Padovan 		/* Delete from channel list */
3513d57dc68SGustavo F. Padovan 		list_del_rcu(&chan->list);
3523d57dc68SGustavo F. Padovan 		synchronize_rcu();
3533d57dc68SGustavo F. Padovan 
354371fd835SUlisses Furquim 		l2cap_chan_put(chan);
355baa7e1faSGustavo F. Padovan 
3568c1d787bSGustavo F. Padovan 		chan->conn = NULL;
3570a708f8fSGustavo F. Padovan 		hci_conn_put(conn->hcon);
3580a708f8fSGustavo F. Padovan 	}
3590a708f8fSGustavo F. Padovan 
36089bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CLOSED);
3610a708f8fSGustavo F. Padovan 	sock_set_flag(sk, SOCK_ZAPPED);
3620a708f8fSGustavo F. Padovan 
3630a708f8fSGustavo F. Padovan 	if (err)
3640a708f8fSGustavo F. Padovan 		sk->sk_err = err;
3650a708f8fSGustavo F. Padovan 
3660a708f8fSGustavo F. Padovan 	if (parent) {
3670a708f8fSGustavo F. Padovan 		bt_accept_unlink(sk);
3680a708f8fSGustavo F. Padovan 		parent->sk_data_ready(parent, 0);
3690a708f8fSGustavo F. Padovan 	} else
3700a708f8fSGustavo F. Padovan 		sk->sk_state_change(sk);
3710a708f8fSGustavo F. Padovan 
372c1360a1cSGustavo F. Padovan 	if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) &&
373c1360a1cSGustavo F. Padovan 			test_bit(CONF_INPUT_DONE, &chan->conf_state)))
3746ff5abbfSGustavo F. Padovan 		return;
3752ead70b8SGustavo F. Padovan 
37658d35f87SGustavo F. Padovan 	skb_queue_purge(&chan->tx_q);
3770a708f8fSGustavo F. Padovan 
3780c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
3790a708f8fSGustavo F. Padovan 		struct srej_list *l, *tmp;
3800a708f8fSGustavo F. Padovan 
3811a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
3821a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
3831a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
3840a708f8fSGustavo F. Padovan 
385f1c6775bSGustavo F. Padovan 		skb_queue_purge(&chan->srej_q);
3860a708f8fSGustavo F. Padovan 
38739d5a3eeSGustavo F. Padovan 		list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
3880a708f8fSGustavo F. Padovan 			list_del(&l->list);
3890a708f8fSGustavo F. Padovan 			kfree(l);
3900a708f8fSGustavo F. Padovan 		}
3910a708f8fSGustavo F. Padovan 	}
3920a708f8fSGustavo F. Padovan }
3930a708f8fSGustavo F. Padovan 
3944519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent)
3954519de9aSGustavo F. Padovan {
3964519de9aSGustavo F. Padovan 	struct sock *sk;
3974519de9aSGustavo F. Padovan 
3984519de9aSGustavo F. Padovan 	BT_DBG("parent %p", parent);
3994519de9aSGustavo F. Padovan 
4004519de9aSGustavo F. Padovan 	/* Close not yet accepted channels */
4010f852724SGustavo F. Padovan 	while ((sk = bt_accept_dequeue(parent, NULL))) {
402ba3bd0eeSGustavo F. Padovan 		struct l2cap_chan *chan = l2cap_pi(sk)->chan;
403c9b66675SGustavo F. Padovan 		__clear_chan_timer(chan);
4040f852724SGustavo F. Padovan 		lock_sock(sk);
405ba3bd0eeSGustavo F. Padovan 		l2cap_chan_close(chan, ECONNRESET);
4060f852724SGustavo F. Padovan 		release_sock(sk);
407ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
4080f852724SGustavo F. Padovan 	}
4094519de9aSGustavo F. Padovan }
4104519de9aSGustavo F. Padovan 
4110f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason)
4124519de9aSGustavo F. Padovan {
4134519de9aSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
4144519de9aSGustavo F. Padovan 	struct sock *sk = chan->sk;
4154519de9aSGustavo F. Padovan 
416*e05dcc32SAndrei Emeltchenko 	BT_DBG("chan %p state %s sk %p", chan,
417*e05dcc32SAndrei Emeltchenko 					state_to_string(chan->state), sk);
4184519de9aSGustavo F. Padovan 
41989bc500eSGustavo F. Padovan 	switch (chan->state) {
4204519de9aSGustavo F. Padovan 	case BT_LISTEN:
4214519de9aSGustavo F. Padovan 		l2cap_chan_cleanup_listen(sk);
42289bc500eSGustavo F. Padovan 
42389bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CLOSED);
42489bc500eSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
4254519de9aSGustavo F. Padovan 		break;
4264519de9aSGustavo F. Padovan 
4274519de9aSGustavo F. Padovan 	case BT_CONNECTED:
4284519de9aSGustavo F. Padovan 	case BT_CONFIG:
429715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4304519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
431c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
432c9b66675SGustavo F. Padovan 			__set_chan_timer(chan, sk->sk_sndtimeo);
4334519de9aSGustavo F. Padovan 			l2cap_send_disconn_req(conn, chan, reason);
4344519de9aSGustavo F. Padovan 		} else
4354519de9aSGustavo F. Padovan 			l2cap_chan_del(chan, reason);
4364519de9aSGustavo F. Padovan 		break;
4374519de9aSGustavo F. Padovan 
4384519de9aSGustavo F. Padovan 	case BT_CONNECT2:
439715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4404519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
4414519de9aSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
4424519de9aSGustavo F. Padovan 			__u16 result;
4434519de9aSGustavo F. Padovan 
4444519de9aSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup)
4454519de9aSGustavo F. Padovan 				result = L2CAP_CR_SEC_BLOCK;
4464519de9aSGustavo F. Padovan 			else
4474519de9aSGustavo F. Padovan 				result = L2CAP_CR_BAD_PSM;
44889bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_DISCONN);
4494519de9aSGustavo F. Padovan 
4504519de9aSGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4514519de9aSGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
4524519de9aSGustavo F. Padovan 			rsp.result = cpu_to_le16(result);
4534519de9aSGustavo F. Padovan 			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4544519de9aSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4554519de9aSGustavo F. Padovan 							sizeof(rsp), &rsp);
4564519de9aSGustavo F. Padovan 		}
4574519de9aSGustavo F. Padovan 
4584519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
4594519de9aSGustavo F. Padovan 		break;
4604519de9aSGustavo F. Padovan 
4614519de9aSGustavo F. Padovan 	case BT_CONNECT:
4624519de9aSGustavo F. Padovan 	case BT_DISCONN:
4634519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
4644519de9aSGustavo F. Padovan 		break;
4654519de9aSGustavo F. Padovan 
4664519de9aSGustavo F. Padovan 	default:
4674519de9aSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
4684519de9aSGustavo F. Padovan 		break;
4694519de9aSGustavo F. Padovan 	}
4704519de9aSGustavo F. Padovan }
4714519de9aSGustavo F. Padovan 
4724343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
4730a708f8fSGustavo F. Padovan {
474715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_RAW) {
4754343478fSGustavo F. Padovan 		switch (chan->sec_level) {
4760a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
4770a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING_MITM;
4780a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
4790a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING;
4800a708f8fSGustavo F. Padovan 		default:
4810a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
4820a708f8fSGustavo F. Padovan 		}
483fe4128e0SGustavo F. Padovan 	} else if (chan->psm == cpu_to_le16(0x0001)) {
4844343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_LOW)
4854343478fSGustavo F. Padovan 			chan->sec_level = BT_SECURITY_SDP;
4860a708f8fSGustavo F. Padovan 
4874343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_HIGH)
4880a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING_MITM;
4890a708f8fSGustavo F. Padovan 		else
4900a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
4910a708f8fSGustavo F. Padovan 	} else {
4924343478fSGustavo F. Padovan 		switch (chan->sec_level) {
4930a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
4940a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING_MITM;
4950a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
4960a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING;
4970a708f8fSGustavo F. Padovan 		default:
4980a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
4990a708f8fSGustavo F. Padovan 		}
5000a708f8fSGustavo F. Padovan 	}
5010a708f8fSGustavo F. Padovan }
5020a708f8fSGustavo F. Padovan 
5030a708f8fSGustavo F. Padovan /* Service level security */
504d45fc423SGustavo F. Padovan int l2cap_chan_check_security(struct l2cap_chan *chan)
5050a708f8fSGustavo F. Padovan {
5068c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
5070a708f8fSGustavo F. Padovan 	__u8 auth_type;
5080a708f8fSGustavo F. Padovan 
5094343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
5100a708f8fSGustavo F. Padovan 
5114343478fSGustavo F. Padovan 	return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
5120a708f8fSGustavo F. Padovan }
5130a708f8fSGustavo F. Padovan 
514b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn)
5150a708f8fSGustavo F. Padovan {
5160a708f8fSGustavo F. Padovan 	u8 id;
5170a708f8fSGustavo F. Padovan 
5180a708f8fSGustavo F. Padovan 	/* Get next available identificator.
5190a708f8fSGustavo F. Padovan 	 *    1 - 128 are used by kernel.
5200a708f8fSGustavo F. Padovan 	 *  129 - 199 are reserved.
5210a708f8fSGustavo F. Padovan 	 *  200 - 254 are used by utilities like l2ping, etc.
5220a708f8fSGustavo F. Padovan 	 */
5230a708f8fSGustavo F. Padovan 
524333055f2SGustavo F. Padovan 	spin_lock(&conn->lock);
5250a708f8fSGustavo F. Padovan 
5260a708f8fSGustavo F. Padovan 	if (++conn->tx_ident > 128)
5270a708f8fSGustavo F. Padovan 		conn->tx_ident = 1;
5280a708f8fSGustavo F. Padovan 
5290a708f8fSGustavo F. Padovan 	id = conn->tx_ident;
5300a708f8fSGustavo F. Padovan 
531333055f2SGustavo F. Padovan 	spin_unlock(&conn->lock);
5320a708f8fSGustavo F. Padovan 
5330a708f8fSGustavo F. Padovan 	return id;
5340a708f8fSGustavo F. Padovan }
5350a708f8fSGustavo F. Padovan 
5364519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
5370a708f8fSGustavo F. Padovan {
5380a708f8fSGustavo F. Padovan 	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
5390a708f8fSGustavo F. Padovan 	u8 flags;
5400a708f8fSGustavo F. Padovan 
5410a708f8fSGustavo F. Padovan 	BT_DBG("code 0x%2.2x", code);
5420a708f8fSGustavo F. Padovan 
5430a708f8fSGustavo F. Padovan 	if (!skb)
5440a708f8fSGustavo F. Padovan 		return;
5450a708f8fSGustavo F. Padovan 
5460a708f8fSGustavo F. Padovan 	if (lmp_no_flush_capable(conn->hcon->hdev))
5470a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
5480a708f8fSGustavo F. Padovan 	else
5490a708f8fSGustavo F. Padovan 		flags = ACL_START;
5500a708f8fSGustavo F. Padovan 
55114b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
5525e59b791SLuiz Augusto von Dentz 	skb->priority = HCI_PRIO_MAX;
55314b12d0bSJaikumar Ganesh 
55473d80debSLuiz Augusto von Dentz 	hci_send_acl(conn->hchan, skb, flags);
5550a708f8fSGustavo F. Padovan }
5560a708f8fSGustavo F. Padovan 
55773d80debSLuiz Augusto von Dentz static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
55873d80debSLuiz Augusto von Dentz {
55973d80debSLuiz Augusto von Dentz 	struct hci_conn *hcon = chan->conn->hcon;
56073d80debSLuiz Augusto von Dentz 	u16 flags;
56173d80debSLuiz Augusto von Dentz 
56273d80debSLuiz Augusto von Dentz 	BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
56373d80debSLuiz Augusto von Dentz 							skb->priority);
56473d80debSLuiz Augusto von Dentz 
56573d80debSLuiz Augusto von Dentz 	if (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
56673d80debSLuiz Augusto von Dentz 					lmp_no_flush_capable(hcon->hdev))
56773d80debSLuiz Augusto von Dentz 		flags = ACL_START_NO_FLUSH;
56873d80debSLuiz Augusto von Dentz 	else
56973d80debSLuiz Augusto von Dentz 		flags = ACL_START;
57073d80debSLuiz Augusto von Dentz 
57173d80debSLuiz Augusto von Dentz 	bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
57273d80debSLuiz Augusto von Dentz 	hci_send_acl(chan->conn->hchan, skb, flags);
5730a708f8fSGustavo F. Padovan }
5740a708f8fSGustavo F. Padovan 
57588843ab0SAndrei Emeltchenko static inline void l2cap_send_sframe(struct l2cap_chan *chan, u32 control)
5760a708f8fSGustavo F. Padovan {
5770a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
5780a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
5798c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
580e4ca6d98SAndrei Emeltchenko 	int count, hlen;
5810a708f8fSGustavo F. Padovan 
58289bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
5830a708f8fSGustavo F. Padovan 		return;
5840a708f8fSGustavo F. Padovan 
585e4ca6d98SAndrei Emeltchenko 	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
586e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_EXT_HDR_SIZE;
587e4ca6d98SAndrei Emeltchenko 	else
588e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_ENH_HDR_SIZE;
5890a708f8fSGustavo F. Padovan 
5900a708f8fSGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
59103a51213SAndrei Emeltchenko 		hlen += L2CAP_FCS_SIZE;
5920a708f8fSGustavo F. Padovan 
59388843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, control 0x%8.8x", chan, control);
5940a708f8fSGustavo F. Padovan 
5950a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, hlen);
596793c2f1cSAndrei Emeltchenko 
597793c2f1cSAndrei Emeltchenko 	control |= __set_sframe(chan);
5980a708f8fSGustavo F. Padovan 
599e2ab4353SGustavo F. Padovan 	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
60003f6715dSAndrei Emeltchenko 		control |= __set_ctrl_final(chan);
6010a708f8fSGustavo F. Padovan 
602e2ab4353SGustavo F. Padovan 	if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state))
603e3781735SAndrei Emeltchenko 		control |= __set_ctrl_poll(chan);
6040a708f8fSGustavo F. Padovan 
6050a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
6060a708f8fSGustavo F. Padovan 	if (!skb)
6070a708f8fSGustavo F. Padovan 		return;
6080a708f8fSGustavo F. Padovan 
6090a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
6100a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
611fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
61288843ab0SAndrei Emeltchenko 
61388843ab0SAndrei Emeltchenko 	__put_control(chan, control, skb_put(skb, __ctrl_size(chan)));
6140a708f8fSGustavo F. Padovan 
61547d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
61603a51213SAndrei Emeltchenko 		u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE);
61703a51213SAndrei Emeltchenko 		put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
6180a708f8fSGustavo F. Padovan 	}
6190a708f8fSGustavo F. Padovan 
62073d80debSLuiz Augusto von Dentz 	skb->priority = HCI_PRIO_MAX;
62173d80debSLuiz Augusto von Dentz 	l2cap_do_send(chan, skb);
6220a708f8fSGustavo F. Padovan }
6230a708f8fSGustavo F. Padovan 
62488843ab0SAndrei Emeltchenko static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u32 control)
6250a708f8fSGustavo F. Padovan {
626e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
627ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
628e2ab4353SGustavo F. Padovan 		set_bit(CONN_RNR_SENT, &chan->conn_state);
6290a708f8fSGustavo F. Padovan 	} else
630ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
6310a708f8fSGustavo F. Padovan 
6320b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
6330a708f8fSGustavo F. Padovan 
634525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
6350a708f8fSGustavo F. Padovan }
6360a708f8fSGustavo F. Padovan 
637b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
6380a708f8fSGustavo F. Padovan {
639c1360a1cSGustavo F. Padovan 	return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
6400a708f8fSGustavo F. Padovan }
6410a708f8fSGustavo F. Padovan 
642fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan)
6430a708f8fSGustavo F. Padovan {
6448c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
6450a708f8fSGustavo F. Padovan 
6460a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
6470a708f8fSGustavo F. Padovan 		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
6480a708f8fSGustavo F. Padovan 			return;
6490a708f8fSGustavo F. Padovan 
650d45fc423SGustavo F. Padovan 		if (l2cap_chan_check_security(chan) &&
6514343478fSGustavo F. Padovan 				__l2cap_no_conn_pending(chan)) {
6520a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
653fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
654fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
6550a708f8fSGustavo F. Padovan 
656fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
657c1360a1cSGustavo F. Padovan 			set_bit(CONF_CONNECT_PEND, &chan->conf_state);
6580a708f8fSGustavo F. Padovan 
659fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
660fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
6610a708f8fSGustavo F. Padovan 		}
6620a708f8fSGustavo F. Padovan 	} else {
6630a708f8fSGustavo F. Padovan 		struct l2cap_info_req req;
6640a708f8fSGustavo F. Padovan 		req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
6650a708f8fSGustavo F. Padovan 
6660a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
6670a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
6680a708f8fSGustavo F. Padovan 
669030013d8SGustavo F. Padovan 		schedule_delayed_work(&conn->info_timer,
6700a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
6710a708f8fSGustavo F. Padovan 
6720a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
6730a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
6740a708f8fSGustavo F. Padovan 	}
6750a708f8fSGustavo F. Padovan }
6760a708f8fSGustavo F. Padovan 
6770a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
6780a708f8fSGustavo F. Padovan {
6790a708f8fSGustavo F. Padovan 	u32 local_feat_mask = l2cap_feat_mask;
6800a708f8fSGustavo F. Padovan 	if (!disable_ertm)
6810a708f8fSGustavo F. Padovan 		local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
6820a708f8fSGustavo F. Padovan 
6830a708f8fSGustavo F. Padovan 	switch (mode) {
6840a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
6850a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
6860a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
6870a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
6880a708f8fSGustavo F. Padovan 	default:
6890a708f8fSGustavo F. Padovan 		return 0x00;
6900a708f8fSGustavo F. Padovan 	}
6910a708f8fSGustavo F. Padovan }
6920a708f8fSGustavo F. Padovan 
6934519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
6940a708f8fSGustavo F. Padovan {
695e92c8e70SGustavo F. Padovan 	struct sock *sk;
6960a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req req;
6970a708f8fSGustavo F. Padovan 
6980a708f8fSGustavo F. Padovan 	if (!conn)
6990a708f8fSGustavo F. Padovan 		return;
7000a708f8fSGustavo F. Padovan 
701e92c8e70SGustavo F. Padovan 	sk = chan->sk;
702e92c8e70SGustavo F. Padovan 
7030c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
7041a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
7051a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
7061a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
7070a708f8fSGustavo F. Padovan 	}
7080a708f8fSGustavo F. Padovan 
709fe4128e0SGustavo F. Padovan 	req.dcid = cpu_to_le16(chan->dcid);
710fe4128e0SGustavo F. Padovan 	req.scid = cpu_to_le16(chan->scid);
7110a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn),
7120a708f8fSGustavo F. Padovan 			L2CAP_DISCONN_REQ, sizeof(req), &req);
7130a708f8fSGustavo F. Padovan 
71489bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_DISCONN);
7150a708f8fSGustavo F. Padovan 	sk->sk_err = err;
7160a708f8fSGustavo F. Padovan }
7170a708f8fSGustavo F. Padovan 
7180a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */
7190a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn)
7200a708f8fSGustavo F. Padovan {
7213d57dc68SGustavo F. Padovan 	struct l2cap_chan *chan;
7220a708f8fSGustavo F. Padovan 
7230a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
7240a708f8fSGustavo F. Padovan 
7253d57dc68SGustavo F. Padovan 	rcu_read_lock();
7260a708f8fSGustavo F. Padovan 
7273d57dc68SGustavo F. Padovan 	list_for_each_entry_rcu(chan, &conn->chan_l, list) {
72848454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
729baa7e1faSGustavo F. Padovan 
7300a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
7310a708f8fSGustavo F. Padovan 
732715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7330a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
7340a708f8fSGustavo F. Padovan 			continue;
7350a708f8fSGustavo F. Padovan 		}
7360a708f8fSGustavo F. Padovan 
73789bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
7380a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
7390a708f8fSGustavo F. Padovan 
740d45fc423SGustavo F. Padovan 			if (!l2cap_chan_check_security(chan) ||
741b4450035SGustavo F. Padovan 					!__l2cap_no_conn_pending(chan)) {
7420a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7430a708f8fSGustavo F. Padovan 				continue;
7440a708f8fSGustavo F. Padovan 			}
7450a708f8fSGustavo F. Padovan 
746c1360a1cSGustavo F. Padovan 			if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
747c1360a1cSGustavo F. Padovan 					&& test_bit(CONF_STATE2_DEVICE,
748c1360a1cSGustavo F. Padovan 					&chan->conf_state)) {
7490f852724SGustavo F. Padovan 				/* l2cap_chan_close() calls list_del(chan)
750820ffdb3SGustavo F. Padovan 				 * so release the lock */
7510f852724SGustavo F. Padovan 				l2cap_chan_close(chan, ECONNRESET);
7520a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7530a708f8fSGustavo F. Padovan 				continue;
7540a708f8fSGustavo F. Padovan 			}
7550a708f8fSGustavo F. Padovan 
756fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
757fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
7580a708f8fSGustavo F. Padovan 
759fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
760c1360a1cSGustavo F. Padovan 			set_bit(CONF_CONNECT_PEND, &chan->conf_state);
7610a708f8fSGustavo F. Padovan 
762fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
763fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
7640a708f8fSGustavo F. Padovan 
76589bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
7660a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
7670a708f8fSGustavo F. Padovan 			char buf[128];
768fe4128e0SGustavo F. Padovan 			rsp.scid = cpu_to_le16(chan->dcid);
769fe4128e0SGustavo F. Padovan 			rsp.dcid = cpu_to_le16(chan->scid);
7700a708f8fSGustavo F. Padovan 
771d45fc423SGustavo F. Padovan 			if (l2cap_chan_check_security(chan)) {
7720a708f8fSGustavo F. Padovan 				if (bt_sk(sk)->defer_setup) {
7730a708f8fSGustavo F. Padovan 					struct sock *parent = bt_sk(sk)->parent;
7740a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_PEND);
7750a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
77605e9a2f6SIlia Kolomisnky 					if (parent)
7770a708f8fSGustavo F. Padovan 						parent->sk_data_ready(parent, 0);
7780a708f8fSGustavo F. Padovan 
7790a708f8fSGustavo F. Padovan 				} else {
78089bc500eSGustavo F. Padovan 					l2cap_state_change(chan, BT_CONFIG);
7810a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
7820a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
7830a708f8fSGustavo F. Padovan 				}
7840a708f8fSGustavo F. Padovan 			} else {
7850a708f8fSGustavo F. Padovan 				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
7860a708f8fSGustavo F. Padovan 				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
7870a708f8fSGustavo F. Padovan 			}
7880a708f8fSGustavo F. Padovan 
789fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
790fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
7910a708f8fSGustavo F. Padovan 
792c1360a1cSGustavo F. Padovan 			if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
7930a708f8fSGustavo F. Padovan 					rsp.result != L2CAP_CR_SUCCESS) {
7940a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7950a708f8fSGustavo F. Padovan 				continue;
7960a708f8fSGustavo F. Padovan 			}
7970a708f8fSGustavo F. Padovan 
798c1360a1cSGustavo F. Padovan 			set_bit(CONF_REQ_SENT, &chan->conf_state);
7990a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
80073ffa904SGustavo F. Padovan 						l2cap_build_conf_req(chan, buf), buf);
80173ffa904SGustavo F. Padovan 			chan->num_conf_req++;
8020a708f8fSGustavo F. Padovan 		}
8030a708f8fSGustavo F. Padovan 
8040a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
8050a708f8fSGustavo F. Padovan 	}
8060a708f8fSGustavo F. Padovan 
8073d57dc68SGustavo F. Padovan 	rcu_read_unlock();
8080a708f8fSGustavo F. Padovan }
8090a708f8fSGustavo F. Padovan 
810b62f328bSVille Tervo /* Find socket with cid and source bdaddr.
811b62f328bSVille Tervo  * Returns closest match, locked.
812b62f328bSVille Tervo  */
81323691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src)
814b62f328bSVille Tervo {
81523691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
816b62f328bSVille Tervo 
81723691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
818b62f328bSVille Tervo 
81923691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
82023691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
821fe4128e0SGustavo F. Padovan 
82289bc500eSGustavo F. Padovan 		if (state && c->state != state)
823b62f328bSVille Tervo 			continue;
824b62f328bSVille Tervo 
82523691d75SGustavo F. Padovan 		if (c->scid == cid) {
826b62f328bSVille Tervo 			/* Exact match. */
82723691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
82823691d75SGustavo F. Padovan 				read_unlock(&chan_list_lock);
82923691d75SGustavo F. Padovan 				return c;
83023691d75SGustavo F. Padovan 			}
831b62f328bSVille Tervo 
832b62f328bSVille Tervo 			/* Closest match */
833b62f328bSVille Tervo 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
83423691d75SGustavo F. Padovan 				c1 = c;
835b62f328bSVille Tervo 		}
836b62f328bSVille Tervo 	}
837280f294fSGustavo F. Padovan 
83823691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
839b62f328bSVille Tervo 
84023691d75SGustavo F. Padovan 	return c1;
841b62f328bSVille Tervo }
842b62f328bSVille Tervo 
843b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn)
844b62f328bSVille Tervo {
845c916fbe4SGustavo F. Padovan 	struct sock *parent, *sk;
84623691d75SGustavo F. Padovan 	struct l2cap_chan *chan, *pchan;
847b62f328bSVille Tervo 
848b62f328bSVille Tervo 	BT_DBG("");
849b62f328bSVille Tervo 
850b62f328bSVille Tervo 	/* Check if we have socket listening on cid */
85123691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
852b62f328bSVille Tervo 							conn->src);
85323691d75SGustavo F. Padovan 	if (!pchan)
854b62f328bSVille Tervo 		return;
855b62f328bSVille Tervo 
85623691d75SGustavo F. Padovan 	parent = pchan->sk;
85723691d75SGustavo F. Padovan 
858aa2ac881SGustavo F. Padovan 	lock_sock(parent);
85962f3a2cfSGustavo F. Padovan 
860b62f328bSVille Tervo 	/* Check for backlog size */
861b62f328bSVille Tervo 	if (sk_acceptq_is_full(parent)) {
862b62f328bSVille Tervo 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
863b62f328bSVille Tervo 		goto clean;
864b62f328bSVille Tervo 	}
865b62f328bSVille Tervo 
86680808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
86780808e43SGustavo F. Padovan 	if (!chan)
868b62f328bSVille Tervo 		goto clean;
869b62f328bSVille Tervo 
87080808e43SGustavo F. Padovan 	sk = chan->sk;
8715d41ce1dSGustavo F. Padovan 
872b62f328bSVille Tervo 	hci_conn_hold(conn->hcon);
873b62f328bSVille Tervo 
874b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->src, conn->src);
875b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->dst, conn->dst);
876b62f328bSVille Tervo 
877d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
878d1010240SGustavo F. Padovan 
8793d57dc68SGustavo F. Padovan 	l2cap_chan_add(conn, chan);
88048454079SGustavo F. Padovan 
881c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
882b62f328bSVille Tervo 
88389bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CONNECTED);
884b62f328bSVille Tervo 	parent->sk_data_ready(parent, 0);
885b62f328bSVille Tervo 
886b62f328bSVille Tervo clean:
887aa2ac881SGustavo F. Padovan 	release_sock(parent);
888b62f328bSVille Tervo }
889b62f328bSVille Tervo 
890cf4cd009SAndrei Emeltchenko static void l2cap_chan_ready(struct l2cap_chan *chan)
891f1cb9af5SVinicius Costa Gomes {
892cf4cd009SAndrei Emeltchenko 	struct sock *sk = chan->sk;
893f1cb9af5SVinicius Costa Gomes 	struct sock *parent = bt_sk(sk)->parent;
894f1cb9af5SVinicius Costa Gomes 
895f1cb9af5SVinicius Costa Gomes 	BT_DBG("sk %p, parent %p", sk, parent);
896f1cb9af5SVinicius Costa Gomes 
897f1cb9af5SVinicius Costa Gomes 	chan->conf_state = 0;
898f1cb9af5SVinicius Costa Gomes 	__clear_chan_timer(chan);
899f1cb9af5SVinicius Costa Gomes 
90043f3dc41SVinicius Costa Gomes 	l2cap_state_change(chan, BT_CONNECTED);
901f1cb9af5SVinicius Costa Gomes 	sk->sk_state_change(sk);
902f1cb9af5SVinicius Costa Gomes 
903f1cb9af5SVinicius Costa Gomes 	if (parent)
904f1cb9af5SVinicius Costa Gomes 		parent->sk_data_ready(parent, 0);
905f1cb9af5SVinicius Costa Gomes }
906f1cb9af5SVinicius Costa Gomes 
9070a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn)
9080a708f8fSGustavo F. Padovan {
90948454079SGustavo F. Padovan 	struct l2cap_chan *chan;
9100a708f8fSGustavo F. Padovan 
9110a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
9120a708f8fSGustavo F. Padovan 
913b62f328bSVille Tervo 	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
914b62f328bSVille Tervo 		l2cap_le_conn_ready(conn);
915b62f328bSVille Tervo 
916160dc6acSVinicius Costa Gomes 	if (conn->hcon->out && conn->hcon->type == LE_LINK)
917160dc6acSVinicius Costa Gomes 		smp_conn_security(conn, conn->hcon->pending_sec_level);
918160dc6acSVinicius Costa Gomes 
9193d57dc68SGustavo F. Padovan 	rcu_read_lock();
9200a708f8fSGustavo F. Padovan 
9213d57dc68SGustavo F. Padovan 	list_for_each_entry_rcu(chan, &conn->chan_l, list) {
92248454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
923baa7e1faSGustavo F. Padovan 
9240a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
9250a708f8fSGustavo F. Padovan 
92663128451SVinicius Costa Gomes 		if (conn->hcon->type == LE_LINK) {
927b501d6a1SAnderson Briglia 			if (smp_conn_security(conn, chan->sec_level))
928cf4cd009SAndrei Emeltchenko 				l2cap_chan_ready(chan);
929acd7d370SVille Tervo 
93063128451SVinicius Costa Gomes 		} else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
931c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
93289bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_CONNECTED);
9330a708f8fSGustavo F. Padovan 			sk->sk_state_change(sk);
934b501d6a1SAnderson Briglia 
93589bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT)
936fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
9370a708f8fSGustavo F. Padovan 
9380a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
9390a708f8fSGustavo F. Padovan 	}
9400a708f8fSGustavo F. Padovan 
9413d57dc68SGustavo F. Padovan 	rcu_read_unlock();
9420a708f8fSGustavo F. Padovan }
9430a708f8fSGustavo F. Padovan 
9440a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */
9450a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
9460a708f8fSGustavo F. Padovan {
94748454079SGustavo F. Padovan 	struct l2cap_chan *chan;
9480a708f8fSGustavo F. Padovan 
9490a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
9500a708f8fSGustavo F. Padovan 
9513d57dc68SGustavo F. Padovan 	rcu_read_lock();
9520a708f8fSGustavo F. Padovan 
9533d57dc68SGustavo F. Padovan 	list_for_each_entry_rcu(chan, &conn->chan_l, list) {
95448454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
955baa7e1faSGustavo F. Padovan 
956ecf61bdbSAndrei Emeltchenko 		if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
9570a708f8fSGustavo F. Padovan 			sk->sk_err = err;
9580a708f8fSGustavo F. Padovan 	}
9590a708f8fSGustavo F. Padovan 
9603d57dc68SGustavo F. Padovan 	rcu_read_unlock();
9610a708f8fSGustavo F. Padovan }
9620a708f8fSGustavo F. Padovan 
963f878fcadSGustavo F. Padovan static void l2cap_info_timeout(struct work_struct *work)
9640a708f8fSGustavo F. Padovan {
965f878fcadSGustavo F. Padovan 	struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
966030013d8SGustavo F. Padovan 							info_timer.work);
9670a708f8fSGustavo F. Padovan 
9680a708f8fSGustavo F. Padovan 	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
9690a708f8fSGustavo F. Padovan 	conn->info_ident = 0;
9700a708f8fSGustavo F. Padovan 
9710a708f8fSGustavo F. Padovan 	l2cap_conn_start(conn);
9720a708f8fSGustavo F. Padovan }
9730a708f8fSGustavo F. Padovan 
9745d3de7dfSVinicius Costa Gomes static void l2cap_conn_del(struct hci_conn *hcon, int err)
9755d3de7dfSVinicius Costa Gomes {
9765d3de7dfSVinicius Costa Gomes 	struct l2cap_conn *conn = hcon->l2cap_data;
9775d3de7dfSVinicius Costa Gomes 	struct l2cap_chan *chan, *l;
9785d3de7dfSVinicius Costa Gomes 	struct sock *sk;
9795d3de7dfSVinicius Costa Gomes 
9805d3de7dfSVinicius Costa Gomes 	if (!conn)
9815d3de7dfSVinicius Costa Gomes 		return;
9825d3de7dfSVinicius Costa Gomes 
9835d3de7dfSVinicius Costa Gomes 	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
9845d3de7dfSVinicius Costa Gomes 
9855d3de7dfSVinicius Costa Gomes 	kfree_skb(conn->rx_skb);
9865d3de7dfSVinicius Costa Gomes 
9875d3de7dfSVinicius Costa Gomes 	/* Kill channels */
9885d3de7dfSVinicius Costa Gomes 	list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
9895d3de7dfSVinicius Costa Gomes 		sk = chan->sk;
990aa2ac881SGustavo F. Padovan 		lock_sock(sk);
9915d3de7dfSVinicius Costa Gomes 		l2cap_chan_del(chan, err);
992aa2ac881SGustavo F. Padovan 		release_sock(sk);
9935d3de7dfSVinicius Costa Gomes 		chan->ops->close(chan->data);
9945d3de7dfSVinicius Costa Gomes 	}
9955d3de7dfSVinicius Costa Gomes 
99673d80debSLuiz Augusto von Dentz 	hci_chan_del(conn->hchan);
99773d80debSLuiz Augusto von Dentz 
9985d3de7dfSVinicius Costa Gomes 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
999127074bfSUlisses Furquim 		cancel_delayed_work_sync(&conn->info_timer);
10005d3de7dfSVinicius Costa Gomes 
100151a8efd7SJohan Hedberg 	if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) {
1002127074bfSUlisses Furquim 		cancel_delayed_work_sync(&conn->security_timer);
10038aab4757SVinicius Costa Gomes 		smp_chan_destroy(conn);
1004d26a2345SVinicius Costa Gomes 	}
10055d3de7dfSVinicius Costa Gomes 
10065d3de7dfSVinicius Costa Gomes 	hcon->l2cap_data = NULL;
10075d3de7dfSVinicius Costa Gomes 	kfree(conn);
10085d3de7dfSVinicius Costa Gomes }
10095d3de7dfSVinicius Costa Gomes 
10106c9d42a1SGustavo F. Padovan static void security_timeout(struct work_struct *work)
10115d3de7dfSVinicius Costa Gomes {
10126c9d42a1SGustavo F. Padovan 	struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
10136c9d42a1SGustavo F. Padovan 						security_timer.work);
10145d3de7dfSVinicius Costa Gomes 
10155d3de7dfSVinicius Costa Gomes 	l2cap_conn_del(conn->hcon, ETIMEDOUT);
10165d3de7dfSVinicius Costa Gomes }
10175d3de7dfSVinicius Costa Gomes 
10180a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
10190a708f8fSGustavo F. Padovan {
10200a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
102173d80debSLuiz Augusto von Dentz 	struct hci_chan *hchan;
10220a708f8fSGustavo F. Padovan 
10230a708f8fSGustavo F. Padovan 	if (conn || status)
10240a708f8fSGustavo F. Padovan 		return conn;
10250a708f8fSGustavo F. Padovan 
102673d80debSLuiz Augusto von Dentz 	hchan = hci_chan_create(hcon);
102773d80debSLuiz Augusto von Dentz 	if (!hchan)
10280a708f8fSGustavo F. Padovan 		return NULL;
10290a708f8fSGustavo F. Padovan 
103073d80debSLuiz Augusto von Dentz 	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
103173d80debSLuiz Augusto von Dentz 	if (!conn) {
103273d80debSLuiz Augusto von Dentz 		hci_chan_del(hchan);
103373d80debSLuiz Augusto von Dentz 		return NULL;
103473d80debSLuiz Augusto von Dentz 	}
103573d80debSLuiz Augusto von Dentz 
10360a708f8fSGustavo F. Padovan 	hcon->l2cap_data = conn;
10370a708f8fSGustavo F. Padovan 	conn->hcon = hcon;
103873d80debSLuiz Augusto von Dentz 	conn->hchan = hchan;
10390a708f8fSGustavo F. Padovan 
104073d80debSLuiz Augusto von Dentz 	BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
10410a708f8fSGustavo F. Padovan 
1042acd7d370SVille Tervo 	if (hcon->hdev->le_mtu && hcon->type == LE_LINK)
1043acd7d370SVille Tervo 		conn->mtu = hcon->hdev->le_mtu;
1044acd7d370SVille Tervo 	else
10450a708f8fSGustavo F. Padovan 		conn->mtu = hcon->hdev->acl_mtu;
1046acd7d370SVille Tervo 
10470a708f8fSGustavo F. Padovan 	conn->src = &hcon->hdev->bdaddr;
10480a708f8fSGustavo F. Padovan 	conn->dst = &hcon->dst;
10490a708f8fSGustavo F. Padovan 
10500a708f8fSGustavo F. Padovan 	conn->feat_mask = 0;
10510a708f8fSGustavo F. Padovan 
10520a708f8fSGustavo F. Padovan 	spin_lock_init(&conn->lock);
1053baa7e1faSGustavo F. Padovan 
1054baa7e1faSGustavo F. Padovan 	INIT_LIST_HEAD(&conn->chan_l);
10550a708f8fSGustavo F. Padovan 
10565d3de7dfSVinicius Costa Gomes 	if (hcon->type == LE_LINK)
10576c9d42a1SGustavo F. Padovan 		INIT_DELAYED_WORK(&conn->security_timer, security_timeout);
10585d3de7dfSVinicius Costa Gomes 	else
1059030013d8SGustavo F. Padovan 		INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
10600a708f8fSGustavo F. Padovan 
10619f5a0d7bSAndrei Emeltchenko 	conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
10620a708f8fSGustavo F. Padovan 
10630a708f8fSGustavo F. Padovan 	return conn;
10640a708f8fSGustavo F. Padovan }
10650a708f8fSGustavo F. Padovan 
10660a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */
10670a708f8fSGustavo F. Padovan 
10680a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr.
10690a708f8fSGustavo F. Padovan  * Returns closest match.
10700a708f8fSGustavo F. Padovan  */
107123691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src)
10720a708f8fSGustavo F. Padovan {
107323691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
10740a708f8fSGustavo F. Padovan 
107523691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
10760a708f8fSGustavo F. Padovan 
107723691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
107823691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
1079fe4128e0SGustavo F. Padovan 
108089bc500eSGustavo F. Padovan 		if (state && c->state != state)
10810a708f8fSGustavo F. Padovan 			continue;
10820a708f8fSGustavo F. Padovan 
108323691d75SGustavo F. Padovan 		if (c->psm == psm) {
10840a708f8fSGustavo F. Padovan 			/* Exact match. */
108523691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
1086a7567b20SJohannes Berg 				read_unlock(&chan_list_lock);
108723691d75SGustavo F. Padovan 				return c;
108823691d75SGustavo F. Padovan 			}
10890a708f8fSGustavo F. Padovan 
10900a708f8fSGustavo F. Padovan 			/* Closest match */
10910a708f8fSGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
109223691d75SGustavo F. Padovan 				c1 = c;
10930a708f8fSGustavo F. Padovan 		}
10940a708f8fSGustavo F. Padovan 	}
10950a708f8fSGustavo F. Padovan 
109623691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
10970a708f8fSGustavo F. Padovan 
109823691d75SGustavo F. Padovan 	return c1;
10990a708f8fSGustavo F. Padovan }
11000a708f8fSGustavo F. Padovan 
1101cbe8fed4SJohan Hedberg int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, bdaddr_t *dst)
11020a708f8fSGustavo F. Padovan {
11035d41ce1dSGustavo F. Padovan 	struct sock *sk = chan->sk;
11040a708f8fSGustavo F. Padovan 	bdaddr_t *src = &bt_sk(sk)->src;
11050a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
11060a708f8fSGustavo F. Padovan 	struct hci_conn *hcon;
11070a708f8fSGustavo F. Padovan 	struct hci_dev *hdev;
11080a708f8fSGustavo F. Padovan 	__u8 auth_type;
11090a708f8fSGustavo F. Padovan 	int err;
11100a708f8fSGustavo F. Padovan 
11110a708f8fSGustavo F. Padovan 	BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
1112fe4128e0SGustavo F. Padovan 							chan->psm);
11130a708f8fSGustavo F. Padovan 
11140a708f8fSGustavo F. Padovan 	hdev = hci_get_route(dst, src);
11150a708f8fSGustavo F. Padovan 	if (!hdev)
11160a708f8fSGustavo F. Padovan 		return -EHOSTUNREACH;
11170a708f8fSGustavo F. Padovan 
111809fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
11190a708f8fSGustavo F. Padovan 
112003a00194SGustavo F. Padovan 	lock_sock(sk);
112103a00194SGustavo F. Padovan 
112203a00194SGustavo F. Padovan 	/* PSM must be odd and lsb of upper byte must be 0 */
112303a00194SGustavo F. Padovan 	if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid &&
112403a00194SGustavo F. Padovan 					chan->chan_type != L2CAP_CHAN_RAW) {
112503a00194SGustavo F. Padovan 		err = -EINVAL;
112603a00194SGustavo F. Padovan 		goto done;
112703a00194SGustavo F. Padovan 	}
112803a00194SGustavo F. Padovan 
112903a00194SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) {
113003a00194SGustavo F. Padovan 		err = -EINVAL;
113103a00194SGustavo F. Padovan 		goto done;
113203a00194SGustavo F. Padovan 	}
113303a00194SGustavo F. Padovan 
113403a00194SGustavo F. Padovan 	switch (chan->mode) {
113503a00194SGustavo F. Padovan 	case L2CAP_MODE_BASIC:
113603a00194SGustavo F. Padovan 		break;
113703a00194SGustavo F. Padovan 	case L2CAP_MODE_ERTM:
113803a00194SGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
113903a00194SGustavo F. Padovan 		if (!disable_ertm)
114003a00194SGustavo F. Padovan 			break;
114103a00194SGustavo F. Padovan 		/* fall through */
114203a00194SGustavo F. Padovan 	default:
114303a00194SGustavo F. Padovan 		err = -ENOTSUPP;
114403a00194SGustavo F. Padovan 		goto done;
114503a00194SGustavo F. Padovan 	}
114603a00194SGustavo F. Padovan 
114703a00194SGustavo F. Padovan 	switch (sk->sk_state) {
114803a00194SGustavo F. Padovan 	case BT_CONNECT:
114903a00194SGustavo F. Padovan 	case BT_CONNECT2:
115003a00194SGustavo F. Padovan 	case BT_CONFIG:
115103a00194SGustavo F. Padovan 		/* Already connecting */
115203a00194SGustavo F. Padovan 		err = 0;
115303a00194SGustavo F. Padovan 		goto done;
115403a00194SGustavo F. Padovan 
115503a00194SGustavo F. Padovan 	case BT_CONNECTED:
115603a00194SGustavo F. Padovan 		/* Already connected */
115703a00194SGustavo F. Padovan 		err = -EISCONN;
115803a00194SGustavo F. Padovan 		goto done;
115903a00194SGustavo F. Padovan 
116003a00194SGustavo F. Padovan 	case BT_OPEN:
116103a00194SGustavo F. Padovan 	case BT_BOUND:
116203a00194SGustavo F. Padovan 		/* Can connect */
116303a00194SGustavo F. Padovan 		break;
116403a00194SGustavo F. Padovan 
116503a00194SGustavo F. Padovan 	default:
116603a00194SGustavo F. Padovan 		err = -EBADFD;
116703a00194SGustavo F. Padovan 		goto done;
116803a00194SGustavo F. Padovan 	}
116903a00194SGustavo F. Padovan 
117003a00194SGustavo F. Padovan 	/* Set destination address and psm */
11719219b2a0SGustavo F. Padovan 	bacpy(&bt_sk(sk)->dst, dst);
117203a00194SGustavo F. Padovan 	chan->psm = psm;
117303a00194SGustavo F. Padovan 	chan->dcid = cid;
11740a708f8fSGustavo F. Padovan 
11754343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
11760a708f8fSGustavo F. Padovan 
1177fe4128e0SGustavo F. Padovan 	if (chan->dcid == L2CAP_CID_LE_DATA)
1178acd7d370SVille Tervo 		hcon = hci_connect(hdev, LE_LINK, dst,
11794343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1180acd7d370SVille Tervo 	else
11810a708f8fSGustavo F. Padovan 		hcon = hci_connect(hdev, ACL_LINK, dst,
11824343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1183acd7d370SVille Tervo 
118430e76272SVille Tervo 	if (IS_ERR(hcon)) {
118530e76272SVille Tervo 		err = PTR_ERR(hcon);
11860a708f8fSGustavo F. Padovan 		goto done;
118730e76272SVille Tervo 	}
11880a708f8fSGustavo F. Padovan 
11890a708f8fSGustavo F. Padovan 	conn = l2cap_conn_add(hcon, 0);
11900a708f8fSGustavo F. Padovan 	if (!conn) {
11910a708f8fSGustavo F. Padovan 		hci_conn_put(hcon);
119230e76272SVille Tervo 		err = -ENOMEM;
11930a708f8fSGustavo F. Padovan 		goto done;
11940a708f8fSGustavo F. Padovan 	}
11950a708f8fSGustavo F. Padovan 
11960a708f8fSGustavo F. Padovan 	/* Update source addr of the socket */
11970a708f8fSGustavo F. Padovan 	bacpy(src, conn->src);
11980a708f8fSGustavo F. Padovan 
119948454079SGustavo F. Padovan 	l2cap_chan_add(conn, chan);
120048454079SGustavo F. Padovan 
120189bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CONNECT);
1202c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
12030a708f8fSGustavo F. Padovan 
12040a708f8fSGustavo F. Padovan 	if (hcon->state == BT_CONNECTED) {
1205715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1206c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
1207d45fc423SGustavo F. Padovan 			if (l2cap_chan_check_security(chan))
120889bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONNECTED);
12090a708f8fSGustavo F. Padovan 		} else
1210fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
12110a708f8fSGustavo F. Padovan 	}
12120a708f8fSGustavo F. Padovan 
121330e76272SVille Tervo 	err = 0;
121430e76272SVille Tervo 
12150a708f8fSGustavo F. Padovan done:
121609fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
12170a708f8fSGustavo F. Padovan 	hci_dev_put(hdev);
12180a708f8fSGustavo F. Padovan 	return err;
12190a708f8fSGustavo F. Padovan }
12200a708f8fSGustavo F. Padovan 
1221dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk)
12220a708f8fSGustavo F. Padovan {
12238c1d787bSGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
12240a708f8fSGustavo F. Padovan 	DECLARE_WAITQUEUE(wait, current);
12250a708f8fSGustavo F. Padovan 	int err = 0;
12260a708f8fSGustavo F. Padovan 	int timeo = HZ/5;
12270a708f8fSGustavo F. Padovan 
12280a708f8fSGustavo F. Padovan 	add_wait_queue(sk_sleep(sk), &wait);
12290a708f8fSGustavo F. Padovan 	set_current_state(TASK_INTERRUPTIBLE);
1230a71a0cf4SPeter Hurley 	while (chan->unacked_frames > 0 && chan->conn) {
12310a708f8fSGustavo F. Padovan 		if (!timeo)
12320a708f8fSGustavo F. Padovan 			timeo = HZ/5;
12330a708f8fSGustavo F. Padovan 
12340a708f8fSGustavo F. Padovan 		if (signal_pending(current)) {
12350a708f8fSGustavo F. Padovan 			err = sock_intr_errno(timeo);
12360a708f8fSGustavo F. Padovan 			break;
12370a708f8fSGustavo F. Padovan 		}
12380a708f8fSGustavo F. Padovan 
12390a708f8fSGustavo F. Padovan 		release_sock(sk);
12400a708f8fSGustavo F. Padovan 		timeo = schedule_timeout(timeo);
12410a708f8fSGustavo F. Padovan 		lock_sock(sk);
1242a71a0cf4SPeter Hurley 		set_current_state(TASK_INTERRUPTIBLE);
12430a708f8fSGustavo F. Padovan 
12440a708f8fSGustavo F. Padovan 		err = sock_error(sk);
12450a708f8fSGustavo F. Padovan 		if (err)
12460a708f8fSGustavo F. Padovan 			break;
12470a708f8fSGustavo F. Padovan 	}
12480a708f8fSGustavo F. Padovan 	set_current_state(TASK_RUNNING);
12490a708f8fSGustavo F. Padovan 	remove_wait_queue(sk_sleep(sk), &wait);
12500a708f8fSGustavo F. Padovan 	return err;
12510a708f8fSGustavo F. Padovan }
12520a708f8fSGustavo F. Padovan 
1253721c4181SGustavo F. Padovan static void l2cap_monitor_timeout(struct work_struct *work)
12540a708f8fSGustavo F. Padovan {
1255721c4181SGustavo F. Padovan 	struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
1256721c4181SGustavo F. Padovan 							monitor_timer.work);
1257525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
12580a708f8fSGustavo F. Padovan 
1259525cd185SGustavo F. Padovan 	BT_DBG("chan %p", chan);
12600a708f8fSGustavo F. Padovan 
1261721c4181SGustavo F. Padovan 	lock_sock(sk);
12622c03a7a4SGustavo F. Padovan 	if (chan->retry_count >= chan->remote_max_tx) {
12638c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
1264721c4181SGustavo F. Padovan 		release_sock(sk);
12650a708f8fSGustavo F. Padovan 		return;
12660a708f8fSGustavo F. Padovan 	}
12670a708f8fSGustavo F. Padovan 
12686a026610SGustavo F. Padovan 	chan->retry_count++;
12691a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
12700a708f8fSGustavo F. Padovan 
1271525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
1272721c4181SGustavo F. Padovan 	release_sock(sk);
12730a708f8fSGustavo F. Padovan }
12740a708f8fSGustavo F. Padovan 
1275721c4181SGustavo F. Padovan static void l2cap_retrans_timeout(struct work_struct *work)
12760a708f8fSGustavo F. Padovan {
1277721c4181SGustavo F. Padovan 	struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
1278721c4181SGustavo F. Padovan 							retrans_timer.work);
1279525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
12800a708f8fSGustavo F. Padovan 
128149208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
12820a708f8fSGustavo F. Padovan 
1283721c4181SGustavo F. Padovan 	lock_sock(sk);
12846a026610SGustavo F. Padovan 	chan->retry_count = 1;
12851a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
12860a708f8fSGustavo F. Padovan 
1287e2ab4353SGustavo F. Padovan 	set_bit(CONN_WAIT_F, &chan->conn_state);
12880a708f8fSGustavo F. Padovan 
1289525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
1290721c4181SGustavo F. Padovan 	release_sock(sk);
12910a708f8fSGustavo F. Padovan }
12920a708f8fSGustavo F. Padovan 
129342e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
12940a708f8fSGustavo F. Padovan {
12950a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
12960a708f8fSGustavo F. Padovan 
129758d35f87SGustavo F. Padovan 	while ((skb = skb_peek(&chan->tx_q)) &&
12986a026610SGustavo F. Padovan 			chan->unacked_frames) {
129942e5c802SGustavo F. Padovan 		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
13000a708f8fSGustavo F. Padovan 			break;
13010a708f8fSGustavo F. Padovan 
130258d35f87SGustavo F. Padovan 		skb = skb_dequeue(&chan->tx_q);
13030a708f8fSGustavo F. Padovan 		kfree_skb(skb);
13040a708f8fSGustavo F. Padovan 
13056a026610SGustavo F. Padovan 		chan->unacked_frames--;
13060a708f8fSGustavo F. Padovan 	}
13070a708f8fSGustavo F. Padovan 
13086a026610SGustavo F. Padovan 	if (!chan->unacked_frames)
13091a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
13100a708f8fSGustavo F. Padovan }
13110a708f8fSGustavo F. Padovan 
131267c9e840SSzymon Janc static void l2cap_streaming_send(struct l2cap_chan *chan)
13130a708f8fSGustavo F. Padovan {
13140a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
131588843ab0SAndrei Emeltchenko 	u32 control;
131688843ab0SAndrei Emeltchenko 	u16 fcs;
13170a708f8fSGustavo F. Padovan 
131858d35f87SGustavo F. Padovan 	while ((skb = skb_dequeue(&chan->tx_q))) {
131988843ab0SAndrei Emeltchenko 		control = __get_control(chan, skb->data + L2CAP_HDR_SIZE);
1320fb45de7dSAndrei Emeltchenko 		control |= __set_txseq(chan, chan->next_tx_seq);
132188843ab0SAndrei Emeltchenko 		__put_control(chan, control, skb->data + L2CAP_HDR_SIZE);
13220a708f8fSGustavo F. Padovan 
132347d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
132403a51213SAndrei Emeltchenko 			fcs = crc16(0, (u8 *)skb->data,
132503a51213SAndrei Emeltchenko 						skb->len - L2CAP_FCS_SIZE);
132603a51213SAndrei Emeltchenko 			put_unaligned_le16(fcs,
132703a51213SAndrei Emeltchenko 					skb->data + skb->len - L2CAP_FCS_SIZE);
13280a708f8fSGustavo F. Padovan 		}
13290a708f8fSGustavo F. Padovan 
13304343478fSGustavo F. Padovan 		l2cap_do_send(chan, skb);
13310a708f8fSGustavo F. Padovan 
1332836be934SAndrei Emeltchenko 		chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
13330a708f8fSGustavo F. Padovan 	}
13340a708f8fSGustavo F. Padovan }
13350a708f8fSGustavo F. Padovan 
1336fb45de7dSAndrei Emeltchenko static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq)
13370a708f8fSGustavo F. Padovan {
13380a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
133988843ab0SAndrei Emeltchenko 	u16 fcs;
134088843ab0SAndrei Emeltchenko 	u32 control;
13410a708f8fSGustavo F. Padovan 
134258d35f87SGustavo F. Padovan 	skb = skb_peek(&chan->tx_q);
13430a708f8fSGustavo F. Padovan 	if (!skb)
13440a708f8fSGustavo F. Padovan 		return;
13450a708f8fSGustavo F. Padovan 
1346d1726b6dSSzymon Janc 	while (bt_cb(skb)->tx_seq != tx_seq) {
134758d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
13480a708f8fSGustavo F. Padovan 			return;
13490a708f8fSGustavo F. Padovan 
1350d1726b6dSSzymon Janc 		skb = skb_queue_next(&chan->tx_q, skb);
1351d1726b6dSSzymon Janc 	}
13520a708f8fSGustavo F. Padovan 
13532c03a7a4SGustavo F. Padovan 	if (chan->remote_max_tx &&
13542c03a7a4SGustavo F. Padovan 			bt_cb(skb)->retries == chan->remote_max_tx) {
13558c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
13560a708f8fSGustavo F. Padovan 		return;
13570a708f8fSGustavo F. Padovan 	}
13580a708f8fSGustavo F. Padovan 
13590a708f8fSGustavo F. Padovan 	tx_skb = skb_clone(skb, GFP_ATOMIC);
13600a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries++;
136188843ab0SAndrei Emeltchenko 
136288843ab0SAndrei Emeltchenko 	control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE);
13637e0ef6eeSAndrei Emeltchenko 	control &= __get_sar_mask(chan);
13640a708f8fSGustavo F. Padovan 
1365e2ab4353SGustavo F. Padovan 	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
136603f6715dSAndrei Emeltchenko 		control |= __set_ctrl_final(chan);
13670a708f8fSGustavo F. Padovan 
13680b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
1369fb45de7dSAndrei Emeltchenko 	control |= __set_txseq(chan, tx_seq);
13700a708f8fSGustavo F. Padovan 
137188843ab0SAndrei Emeltchenko 	__put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE);
13720a708f8fSGustavo F. Padovan 
137347d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
137403a51213SAndrei Emeltchenko 		fcs = crc16(0, (u8 *)tx_skb->data,
137503a51213SAndrei Emeltchenko 						tx_skb->len - L2CAP_FCS_SIZE);
137603a51213SAndrei Emeltchenko 		put_unaligned_le16(fcs,
137703a51213SAndrei Emeltchenko 				tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE);
13780a708f8fSGustavo F. Padovan 	}
13790a708f8fSGustavo F. Padovan 
13804343478fSGustavo F. Padovan 	l2cap_do_send(chan, tx_skb);
13810a708f8fSGustavo F. Padovan }
13820a708f8fSGustavo F. Padovan 
138367c9e840SSzymon Janc static int l2cap_ertm_send(struct l2cap_chan *chan)
13840a708f8fSGustavo F. Padovan {
13850a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
138688843ab0SAndrei Emeltchenko 	u16 fcs;
138788843ab0SAndrei Emeltchenko 	u32 control;
13880a708f8fSGustavo F. Padovan 	int nsent = 0;
13890a708f8fSGustavo F. Padovan 
139089bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
13910a708f8fSGustavo F. Padovan 		return -ENOTCONN;
13920a708f8fSGustavo F. Padovan 
139358d35f87SGustavo F. Padovan 	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
13940a708f8fSGustavo F. Padovan 
13952c03a7a4SGustavo F. Padovan 		if (chan->remote_max_tx &&
13962c03a7a4SGustavo F. Padovan 				bt_cb(skb)->retries == chan->remote_max_tx) {
13978c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
13980a708f8fSGustavo F. Padovan 			break;
13990a708f8fSGustavo F. Padovan 		}
14000a708f8fSGustavo F. Padovan 
14010a708f8fSGustavo F. Padovan 		tx_skb = skb_clone(skb, GFP_ATOMIC);
14020a708f8fSGustavo F. Padovan 
14030a708f8fSGustavo F. Padovan 		bt_cb(skb)->retries++;
14040a708f8fSGustavo F. Padovan 
140588843ab0SAndrei Emeltchenko 		control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE);
14067e0ef6eeSAndrei Emeltchenko 		control &= __get_sar_mask(chan);
14070a708f8fSGustavo F. Padovan 
1408e2ab4353SGustavo F. Padovan 		if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
140903f6715dSAndrei Emeltchenko 			control |= __set_ctrl_final(chan);
1410e2ab4353SGustavo F. Padovan 
14110b209faeSAndrei Emeltchenko 		control |= __set_reqseq(chan, chan->buffer_seq);
1412fb45de7dSAndrei Emeltchenko 		control |= __set_txseq(chan, chan->next_tx_seq);
14130a708f8fSGustavo F. Padovan 
141488843ab0SAndrei Emeltchenko 		__put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE);
14150a708f8fSGustavo F. Padovan 
141647d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
141703a51213SAndrei Emeltchenko 			fcs = crc16(0, (u8 *)skb->data,
141803a51213SAndrei Emeltchenko 						tx_skb->len - L2CAP_FCS_SIZE);
141903a51213SAndrei Emeltchenko 			put_unaligned_le16(fcs, skb->data +
142003a51213SAndrei Emeltchenko 						tx_skb->len - L2CAP_FCS_SIZE);
14210a708f8fSGustavo F. Padovan 		}
14220a708f8fSGustavo F. Padovan 
14234343478fSGustavo F. Padovan 		l2cap_do_send(chan, tx_skb);
14240a708f8fSGustavo F. Padovan 
14251a09bcb9SGustavo F. Padovan 		__set_retrans_timer(chan);
14260a708f8fSGustavo F. Padovan 
142742e5c802SGustavo F. Padovan 		bt_cb(skb)->tx_seq = chan->next_tx_seq;
1428836be934SAndrei Emeltchenko 
1429836be934SAndrei Emeltchenko 		chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
14300a708f8fSGustavo F. Padovan 
14318ed7a0aeSSzymon Janc 		if (bt_cb(skb)->retries == 1) {
14326a026610SGustavo F. Padovan 			chan->unacked_frames++;
1433930fa4aeSSzymon Janc 
1434930fa4aeSSzymon Janc 			if (!nsent++)
1435930fa4aeSSzymon Janc 				__clear_ack_timer(chan);
14368ed7a0aeSSzymon Janc 		}
143723e9fde2SSuraj Sumangala 
14386a026610SGustavo F. Padovan 		chan->frames_sent++;
14390a708f8fSGustavo F. Padovan 
144058d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
144158d35f87SGustavo F. Padovan 			chan->tx_send_head = NULL;
14420a708f8fSGustavo F. Padovan 		else
144358d35f87SGustavo F. Padovan 			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
14440a708f8fSGustavo F. Padovan 	}
14450a708f8fSGustavo F. Padovan 
14460a708f8fSGustavo F. Padovan 	return nsent;
14470a708f8fSGustavo F. Padovan }
14480a708f8fSGustavo F. Padovan 
1449525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan)
14500a708f8fSGustavo F. Padovan {
14510a708f8fSGustavo F. Padovan 	int ret;
14520a708f8fSGustavo F. Padovan 
145358d35f87SGustavo F. Padovan 	if (!skb_queue_empty(&chan->tx_q))
145458d35f87SGustavo F. Padovan 		chan->tx_send_head = chan->tx_q.next;
14550a708f8fSGustavo F. Padovan 
145642e5c802SGustavo F. Padovan 	chan->next_tx_seq = chan->expected_ack_seq;
1457525cd185SGustavo F. Padovan 	ret = l2cap_ertm_send(chan);
14580a708f8fSGustavo F. Padovan 	return ret;
14590a708f8fSGustavo F. Padovan }
14600a708f8fSGustavo F. Padovan 
1461b17e73bbSSzymon Janc static void __l2cap_send_ack(struct l2cap_chan *chan)
14620a708f8fSGustavo F. Padovan {
146388843ab0SAndrei Emeltchenko 	u32 control = 0;
14640a708f8fSGustavo F. Padovan 
14650b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
14660a708f8fSGustavo F. Padovan 
1467e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
1468ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
1469e2ab4353SGustavo F. Padovan 		set_bit(CONN_RNR_SENT, &chan->conn_state);
1470525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
14710a708f8fSGustavo F. Padovan 		return;
14720a708f8fSGustavo F. Padovan 	}
14730a708f8fSGustavo F. Padovan 
1474525cd185SGustavo F. Padovan 	if (l2cap_ertm_send(chan) > 0)
14750a708f8fSGustavo F. Padovan 		return;
14760a708f8fSGustavo F. Padovan 
1477ab784b73SAndrei Emeltchenko 	control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
1478525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
14790a708f8fSGustavo F. Padovan }
14800a708f8fSGustavo F. Padovan 
1481b17e73bbSSzymon Janc static void l2cap_send_ack(struct l2cap_chan *chan)
1482b17e73bbSSzymon Janc {
1483b17e73bbSSzymon Janc 	__clear_ack_timer(chan);
1484b17e73bbSSzymon Janc 	__l2cap_send_ack(chan);
1485b17e73bbSSzymon Janc }
1486b17e73bbSSzymon Janc 
1487525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan)
14880a708f8fSGustavo F. Padovan {
14890a708f8fSGustavo F. Padovan 	struct srej_list *tail;
149088843ab0SAndrei Emeltchenko 	u32 control;
14910a708f8fSGustavo F. Padovan 
1492ab784b73SAndrei Emeltchenko 	control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
149303f6715dSAndrei Emeltchenko 	control |= __set_ctrl_final(chan);
14940a708f8fSGustavo F. Padovan 
149539d5a3eeSGustavo F. Padovan 	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
14960b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, tail->tx_seq);
14970a708f8fSGustavo F. Padovan 
1498525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
14990a708f8fSGustavo F. Padovan }
15000a708f8fSGustavo F. Padovan 
15010952a57aSAndrei Emeltchenko static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, struct msghdr *msg, int len, int count, struct sk_buff *skb)
15020a708f8fSGustavo F. Padovan {
15030952a57aSAndrei Emeltchenko 	struct l2cap_conn *conn = chan->conn;
15040a708f8fSGustavo F. Padovan 	struct sk_buff **frag;
15050a708f8fSGustavo F. Padovan 	int err, sent = 0;
15060a708f8fSGustavo F. Padovan 
15070a708f8fSGustavo F. Padovan 	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
15080a708f8fSGustavo F. Padovan 		return -EFAULT;
15090a708f8fSGustavo F. Padovan 
15100a708f8fSGustavo F. Padovan 	sent += count;
15110a708f8fSGustavo F. Padovan 	len  -= count;
15120a708f8fSGustavo F. Padovan 
15130a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
15140a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
15150a708f8fSGustavo F. Padovan 	while (len) {
15160a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
15170a708f8fSGustavo F. Padovan 
15182f7719ceSAndrei Emeltchenko 		*frag = chan->ops->alloc_skb(chan, count,
15192f7719ceSAndrei Emeltchenko 					msg->msg_flags & MSG_DONTWAIT, &err);
15202f7719ceSAndrei Emeltchenko 
15210a708f8fSGustavo F. Padovan 		if (!*frag)
15220a708f8fSGustavo F. Padovan 			return err;
15230a708f8fSGustavo F. Padovan 		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
15240a708f8fSGustavo F. Padovan 			return -EFAULT;
15250a708f8fSGustavo F. Padovan 
15265e59b791SLuiz Augusto von Dentz 		(*frag)->priority = skb->priority;
15275e59b791SLuiz Augusto von Dentz 
15280a708f8fSGustavo F. Padovan 		sent += count;
15290a708f8fSGustavo F. Padovan 		len  -= count;
15300a708f8fSGustavo F. Padovan 
15310a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
15320a708f8fSGustavo F. Padovan 	}
15330a708f8fSGustavo F. Padovan 
15340a708f8fSGustavo F. Padovan 	return sent;
15350a708f8fSGustavo F. Padovan }
15360a708f8fSGustavo F. Padovan 
15375e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
15385e59b791SLuiz Augusto von Dentz 						struct msghdr *msg, size_t len,
15395e59b791SLuiz Augusto von Dentz 						u32 priority)
15400a708f8fSGustavo F. Padovan {
15418c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
15420a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
154303a51213SAndrei Emeltchenko 	int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
15440a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
15450a708f8fSGustavo F. Padovan 
15466d5922b0SAndrei Emeltchenko 	BT_DBG("chan %p len %d priority %u", chan, (int)len, priority);
15470a708f8fSGustavo F. Padovan 
15480a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
15492f7719ceSAndrei Emeltchenko 
15502f7719ceSAndrei Emeltchenko 	skb = chan->ops->alloc_skb(chan, count + hlen,
15510a708f8fSGustavo F. Padovan 					msg->msg_flags & MSG_DONTWAIT, &err);
15522f7719ceSAndrei Emeltchenko 
15530a708f8fSGustavo F. Padovan 	if (!skb)
15540a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15550a708f8fSGustavo F. Padovan 
15565e59b791SLuiz Augusto von Dentz 	skb->priority = priority;
15575e59b791SLuiz Augusto von Dentz 
15580a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
15590a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1560fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
15610a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1562fe4128e0SGustavo F. Padovan 	put_unaligned_le16(chan->psm, skb_put(skb, 2));
15630a708f8fSGustavo F. Padovan 
15640952a57aSAndrei Emeltchenko 	err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
15650a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
15660a708f8fSGustavo F. Padovan 		kfree_skb(skb);
15670a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15680a708f8fSGustavo F. Padovan 	}
15690a708f8fSGustavo F. Padovan 	return skb;
15700a708f8fSGustavo F. Padovan }
15710a708f8fSGustavo F. Padovan 
15725e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
15735e59b791SLuiz Augusto von Dentz 						struct msghdr *msg, size_t len,
15745e59b791SLuiz Augusto von Dentz 						u32 priority)
15750a708f8fSGustavo F. Padovan {
15768c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
15770a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
15780a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE;
15790a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
15800a708f8fSGustavo F. Padovan 
15816d5922b0SAndrei Emeltchenko 	BT_DBG("chan %p len %d", chan, (int)len);
15820a708f8fSGustavo F. Padovan 
15830a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
15842f7719ceSAndrei Emeltchenko 
15852f7719ceSAndrei Emeltchenko 	skb = chan->ops->alloc_skb(chan, count + hlen,
15860a708f8fSGustavo F. Padovan 					msg->msg_flags & MSG_DONTWAIT, &err);
15872f7719ceSAndrei Emeltchenko 
15880a708f8fSGustavo F. Padovan 	if (!skb)
15890a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15900a708f8fSGustavo F. Padovan 
15915e59b791SLuiz Augusto von Dentz 	skb->priority = priority;
15925e59b791SLuiz Augusto von Dentz 
15930a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
15940a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1595fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
15960a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
15970a708f8fSGustavo F. Padovan 
15980952a57aSAndrei Emeltchenko 	err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
15990a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
16000a708f8fSGustavo F. Padovan 		kfree_skb(skb);
16010a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
16020a708f8fSGustavo F. Padovan 	}
16030a708f8fSGustavo F. Padovan 	return skb;
16040a708f8fSGustavo F. Padovan }
16050a708f8fSGustavo F. Padovan 
1606ab0ff76dSLuiz Augusto von Dentz static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
1607ab0ff76dSLuiz Augusto von Dentz 						struct msghdr *msg, size_t len,
160888843ab0SAndrei Emeltchenko 						u32 control, u16 sdulen)
16090a708f8fSGustavo F. Padovan {
16108c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
16110a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
1612e4ca6d98SAndrei Emeltchenko 	int err, count, hlen;
16130a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
16140a708f8fSGustavo F. Padovan 
16156d5922b0SAndrei Emeltchenko 	BT_DBG("chan %p len %d", chan, (int)len);
16160a708f8fSGustavo F. Padovan 
16170a708f8fSGustavo F. Padovan 	if (!conn)
16180a708f8fSGustavo F. Padovan 		return ERR_PTR(-ENOTCONN);
16190a708f8fSGustavo F. Padovan 
1620e4ca6d98SAndrei Emeltchenko 	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1621e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_EXT_HDR_SIZE;
1622e4ca6d98SAndrei Emeltchenko 	else
1623e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_ENH_HDR_SIZE;
1624e4ca6d98SAndrei Emeltchenko 
16250a708f8fSGustavo F. Padovan 	if (sdulen)
162603a51213SAndrei Emeltchenko 		hlen += L2CAP_SDULEN_SIZE;
16270a708f8fSGustavo F. Padovan 
162847d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
162903a51213SAndrei Emeltchenko 		hlen += L2CAP_FCS_SIZE;
16300a708f8fSGustavo F. Padovan 
16310a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
16322f7719ceSAndrei Emeltchenko 
16332f7719ceSAndrei Emeltchenko 	skb = chan->ops->alloc_skb(chan, count + hlen,
16340a708f8fSGustavo F. Padovan 					msg->msg_flags & MSG_DONTWAIT, &err);
16352f7719ceSAndrei Emeltchenko 
16360a708f8fSGustavo F. Padovan 	if (!skb)
16370a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
16380a708f8fSGustavo F. Padovan 
16390a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
16400a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1641fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
16420a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
164388843ab0SAndrei Emeltchenko 
164488843ab0SAndrei Emeltchenko 	__put_control(chan, control, skb_put(skb, __ctrl_size(chan)));
164588843ab0SAndrei Emeltchenko 
16460a708f8fSGustavo F. Padovan 	if (sdulen)
164703a51213SAndrei Emeltchenko 		put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
16480a708f8fSGustavo F. Padovan 
16490952a57aSAndrei Emeltchenko 	err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
16500a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
16510a708f8fSGustavo F. Padovan 		kfree_skb(skb);
16520a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
16530a708f8fSGustavo F. Padovan 	}
16540a708f8fSGustavo F. Padovan 
165547d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
165603a51213SAndrei Emeltchenko 		put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE));
16570a708f8fSGustavo F. Padovan 
16580a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries = 0;
16590a708f8fSGustavo F. Padovan 	return skb;
16600a708f8fSGustavo F. Padovan }
16610a708f8fSGustavo F. Padovan 
166267c9e840SSzymon Janc static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
16630a708f8fSGustavo F. Padovan {
16640a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
16650a708f8fSGustavo F. Padovan 	struct sk_buff_head sar_queue;
166688843ab0SAndrei Emeltchenko 	u32 control;
16670a708f8fSGustavo F. Padovan 	size_t size = 0;
16680a708f8fSGustavo F. Padovan 
16690a708f8fSGustavo F. Padovan 	skb_queue_head_init(&sar_queue);
16707e0ef6eeSAndrei Emeltchenko 	control = __set_ctrl_sar(chan, L2CAP_SAR_START);
167147d1ec61SGustavo F. Padovan 	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
16720a708f8fSGustavo F. Padovan 	if (IS_ERR(skb))
16730a708f8fSGustavo F. Padovan 		return PTR_ERR(skb);
16740a708f8fSGustavo F. Padovan 
16750a708f8fSGustavo F. Padovan 	__skb_queue_tail(&sar_queue, skb);
16762c03a7a4SGustavo F. Padovan 	len -= chan->remote_mps;
16772c03a7a4SGustavo F. Padovan 	size += chan->remote_mps;
16780a708f8fSGustavo F. Padovan 
16790a708f8fSGustavo F. Padovan 	while (len > 0) {
16800a708f8fSGustavo F. Padovan 		size_t buflen;
16810a708f8fSGustavo F. Padovan 
16822c03a7a4SGustavo F. Padovan 		if (len > chan->remote_mps) {
16837e0ef6eeSAndrei Emeltchenko 			control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE);
16842c03a7a4SGustavo F. Padovan 			buflen = chan->remote_mps;
16850a708f8fSGustavo F. Padovan 		} else {
16867e0ef6eeSAndrei Emeltchenko 			control = __set_ctrl_sar(chan, L2CAP_SAR_END);
16870a708f8fSGustavo F. Padovan 			buflen = len;
16880a708f8fSGustavo F. Padovan 		}
16890a708f8fSGustavo F. Padovan 
169047d1ec61SGustavo F. Padovan 		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
16910a708f8fSGustavo F. Padovan 		if (IS_ERR(skb)) {
16920a708f8fSGustavo F. Padovan 			skb_queue_purge(&sar_queue);
16930a708f8fSGustavo F. Padovan 			return PTR_ERR(skb);
16940a708f8fSGustavo F. Padovan 		}
16950a708f8fSGustavo F. Padovan 
16960a708f8fSGustavo F. Padovan 		__skb_queue_tail(&sar_queue, skb);
16970a708f8fSGustavo F. Padovan 		len -= buflen;
16980a708f8fSGustavo F. Padovan 		size += buflen;
16990a708f8fSGustavo F. Padovan 	}
170058d35f87SGustavo F. Padovan 	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
170158d35f87SGustavo F. Padovan 	if (chan->tx_send_head == NULL)
170258d35f87SGustavo F. Padovan 		chan->tx_send_head = sar_queue.next;
17030a708f8fSGustavo F. Padovan 
17040a708f8fSGustavo F. Padovan 	return size;
17050a708f8fSGustavo F. Padovan }
17060a708f8fSGustavo F. Padovan 
17075e59b791SLuiz Augusto von Dentz int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
17085e59b791SLuiz Augusto von Dentz 								u32 priority)
17099a91a04aSGustavo F. Padovan {
17109a91a04aSGustavo F. Padovan 	struct sk_buff *skb;
171188843ab0SAndrei Emeltchenko 	u32 control;
17129a91a04aSGustavo F. Padovan 	int err;
17139a91a04aSGustavo F. Padovan 
17149a91a04aSGustavo F. Padovan 	/* Connectionless channel */
1715715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
17165e59b791SLuiz Augusto von Dentz 		skb = l2cap_create_connless_pdu(chan, msg, len, priority);
17179a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
17189a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
17199a91a04aSGustavo F. Padovan 
17209a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
17219a91a04aSGustavo F. Padovan 		return len;
17229a91a04aSGustavo F. Padovan 	}
17239a91a04aSGustavo F. Padovan 
17249a91a04aSGustavo F. Padovan 	switch (chan->mode) {
17259a91a04aSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
17269a91a04aSGustavo F. Padovan 		/* Check outgoing MTU */
17279a91a04aSGustavo F. Padovan 		if (len > chan->omtu)
17289a91a04aSGustavo F. Padovan 			return -EMSGSIZE;
17299a91a04aSGustavo F. Padovan 
17309a91a04aSGustavo F. Padovan 		/* Create a basic PDU */
17315e59b791SLuiz Augusto von Dentz 		skb = l2cap_create_basic_pdu(chan, msg, len, priority);
17329a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
17339a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
17349a91a04aSGustavo F. Padovan 
17359a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
17369a91a04aSGustavo F. Padovan 		err = len;
17379a91a04aSGustavo F. Padovan 		break;
17389a91a04aSGustavo F. Padovan 
17399a91a04aSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
17409a91a04aSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
17419a91a04aSGustavo F. Padovan 		/* Entire SDU fits into one PDU */
17429a91a04aSGustavo F. Padovan 		if (len <= chan->remote_mps) {
17437e0ef6eeSAndrei Emeltchenko 			control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED);
17449a91a04aSGustavo F. Padovan 			skb = l2cap_create_iframe_pdu(chan, msg, len, control,
17459a91a04aSGustavo F. Padovan 									0);
17469a91a04aSGustavo F. Padovan 			if (IS_ERR(skb))
17479a91a04aSGustavo F. Padovan 				return PTR_ERR(skb);
17489a91a04aSGustavo F. Padovan 
17499a91a04aSGustavo F. Padovan 			__skb_queue_tail(&chan->tx_q, skb);
17509a91a04aSGustavo F. Padovan 
17519a91a04aSGustavo F. Padovan 			if (chan->tx_send_head == NULL)
17529a91a04aSGustavo F. Padovan 				chan->tx_send_head = skb;
17539a91a04aSGustavo F. Padovan 
17549a91a04aSGustavo F. Padovan 		} else {
17559a91a04aSGustavo F. Padovan 			/* Segment SDU into multiples PDUs */
17569a91a04aSGustavo F. Padovan 			err = l2cap_sar_segment_sdu(chan, msg, len);
17579a91a04aSGustavo F. Padovan 			if (err < 0)
17589a91a04aSGustavo F. Padovan 				return err;
17599a91a04aSGustavo F. Padovan 		}
17609a91a04aSGustavo F. Padovan 
17619a91a04aSGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_STREAMING) {
17629a91a04aSGustavo F. Padovan 			l2cap_streaming_send(chan);
17639a91a04aSGustavo F. Padovan 			err = len;
17649a91a04aSGustavo F. Padovan 			break;
17659a91a04aSGustavo F. Padovan 		}
17669a91a04aSGustavo F. Padovan 
1767e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
1768e2ab4353SGustavo F. Padovan 				test_bit(CONN_WAIT_F, &chan->conn_state)) {
17699a91a04aSGustavo F. Padovan 			err = len;
17709a91a04aSGustavo F. Padovan 			break;
17719a91a04aSGustavo F. Padovan 		}
17729a91a04aSGustavo F. Padovan 
17739a91a04aSGustavo F. Padovan 		err = l2cap_ertm_send(chan);
17749a91a04aSGustavo F. Padovan 		if (err >= 0)
17759a91a04aSGustavo F. Padovan 			err = len;
17769a91a04aSGustavo F. Padovan 
17779a91a04aSGustavo F. Padovan 		break;
17789a91a04aSGustavo F. Padovan 
17799a91a04aSGustavo F. Padovan 	default:
17809a91a04aSGustavo F. Padovan 		BT_DBG("bad state %1.1x", chan->mode);
17819a91a04aSGustavo F. Padovan 		err = -EBADFD;
17829a91a04aSGustavo F. Padovan 	}
17839a91a04aSGustavo F. Padovan 
17849a91a04aSGustavo F. Padovan 	return err;
17859a91a04aSGustavo F. Padovan }
17869a91a04aSGustavo F. Padovan 
17870a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */
17880a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
17890a708f8fSGustavo F. Padovan {
17900a708f8fSGustavo F. Padovan 	struct sk_buff *nskb;
179148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
17920a708f8fSGustavo F. Padovan 
17930a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
17940a708f8fSGustavo F. Padovan 
17953d57dc68SGustavo F. Padovan 	rcu_read_lock();
17963d57dc68SGustavo F. Padovan 
17973d57dc68SGustavo F. Padovan 	list_for_each_entry_rcu(chan, &conn->chan_l, list) {
179848454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
1799715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_RAW)
18000a708f8fSGustavo F. Padovan 			continue;
18010a708f8fSGustavo F. Padovan 
18020a708f8fSGustavo F. Padovan 		/* Don't send frame to the socket it came from */
18030a708f8fSGustavo F. Padovan 		if (skb->sk == sk)
18040a708f8fSGustavo F. Padovan 			continue;
18050a708f8fSGustavo F. Padovan 		nskb = skb_clone(skb, GFP_ATOMIC);
18060a708f8fSGustavo F. Padovan 		if (!nskb)
18070a708f8fSGustavo F. Padovan 			continue;
18080a708f8fSGustavo F. Padovan 
180923070494SGustavo F. Padovan 		if (chan->ops->recv(chan->data, nskb))
18100a708f8fSGustavo F. Padovan 			kfree_skb(nskb);
18110a708f8fSGustavo F. Padovan 	}
18123d57dc68SGustavo F. Padovan 
18133d57dc68SGustavo F. Padovan 	rcu_read_unlock();
18140a708f8fSGustavo F. Padovan }
18150a708f8fSGustavo F. Padovan 
18160a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */
18170a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
18180a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data)
18190a708f8fSGustavo F. Padovan {
18200a708f8fSGustavo F. Padovan 	struct sk_buff *skb, **frag;
18210a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr *cmd;
18220a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
18230a708f8fSGustavo F. Padovan 	int len, count;
18240a708f8fSGustavo F. Padovan 
18250a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
18260a708f8fSGustavo F. Padovan 			conn, code, ident, dlen);
18270a708f8fSGustavo F. Padovan 
18280a708f8fSGustavo F. Padovan 	len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
18290a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, len);
18300a708f8fSGustavo F. Padovan 
18310a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
18320a708f8fSGustavo F. Padovan 	if (!skb)
18330a708f8fSGustavo F. Padovan 		return NULL;
18340a708f8fSGustavo F. Padovan 
18350a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
18360a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
18373300d9a9SClaudio Takahasi 
18383300d9a9SClaudio Takahasi 	if (conn->hcon->type == LE_LINK)
18393300d9a9SClaudio Takahasi 		lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
18403300d9a9SClaudio Takahasi 	else
18410a708f8fSGustavo F. Padovan 		lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
18420a708f8fSGustavo F. Padovan 
18430a708f8fSGustavo F. Padovan 	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
18440a708f8fSGustavo F. Padovan 	cmd->code  = code;
18450a708f8fSGustavo F. Padovan 	cmd->ident = ident;
18460a708f8fSGustavo F. Padovan 	cmd->len   = cpu_to_le16(dlen);
18470a708f8fSGustavo F. Padovan 
18480a708f8fSGustavo F. Padovan 	if (dlen) {
18490a708f8fSGustavo F. Padovan 		count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
18500a708f8fSGustavo F. Padovan 		memcpy(skb_put(skb, count), data, count);
18510a708f8fSGustavo F. Padovan 		data += count;
18520a708f8fSGustavo F. Padovan 	}
18530a708f8fSGustavo F. Padovan 
18540a708f8fSGustavo F. Padovan 	len -= skb->len;
18550a708f8fSGustavo F. Padovan 
18560a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
18570a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
18580a708f8fSGustavo F. Padovan 	while (len) {
18590a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
18600a708f8fSGustavo F. Padovan 
18610a708f8fSGustavo F. Padovan 		*frag = bt_skb_alloc(count, GFP_ATOMIC);
18620a708f8fSGustavo F. Padovan 		if (!*frag)
18630a708f8fSGustavo F. Padovan 			goto fail;
18640a708f8fSGustavo F. Padovan 
18650a708f8fSGustavo F. Padovan 		memcpy(skb_put(*frag, count), data, count);
18660a708f8fSGustavo F. Padovan 
18670a708f8fSGustavo F. Padovan 		len  -= count;
18680a708f8fSGustavo F. Padovan 		data += count;
18690a708f8fSGustavo F. Padovan 
18700a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
18710a708f8fSGustavo F. Padovan 	}
18720a708f8fSGustavo F. Padovan 
18730a708f8fSGustavo F. Padovan 	return skb;
18740a708f8fSGustavo F. Padovan 
18750a708f8fSGustavo F. Padovan fail:
18760a708f8fSGustavo F. Padovan 	kfree_skb(skb);
18770a708f8fSGustavo F. Padovan 	return NULL;
18780a708f8fSGustavo F. Padovan }
18790a708f8fSGustavo F. Padovan 
18800a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
18810a708f8fSGustavo F. Padovan {
18820a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
18830a708f8fSGustavo F. Padovan 	int len;
18840a708f8fSGustavo F. Padovan 
18850a708f8fSGustavo F. Padovan 	len = L2CAP_CONF_OPT_SIZE + opt->len;
18860a708f8fSGustavo F. Padovan 	*ptr += len;
18870a708f8fSGustavo F. Padovan 
18880a708f8fSGustavo F. Padovan 	*type = opt->type;
18890a708f8fSGustavo F. Padovan 	*olen = opt->len;
18900a708f8fSGustavo F. Padovan 
18910a708f8fSGustavo F. Padovan 	switch (opt->len) {
18920a708f8fSGustavo F. Padovan 	case 1:
18930a708f8fSGustavo F. Padovan 		*val = *((u8 *) opt->val);
18940a708f8fSGustavo F. Padovan 		break;
18950a708f8fSGustavo F. Padovan 
18960a708f8fSGustavo F. Padovan 	case 2:
18970a708f8fSGustavo F. Padovan 		*val = get_unaligned_le16(opt->val);
18980a708f8fSGustavo F. Padovan 		break;
18990a708f8fSGustavo F. Padovan 
19000a708f8fSGustavo F. Padovan 	case 4:
19010a708f8fSGustavo F. Padovan 		*val = get_unaligned_le32(opt->val);
19020a708f8fSGustavo F. Padovan 		break;
19030a708f8fSGustavo F. Padovan 
19040a708f8fSGustavo F. Padovan 	default:
19050a708f8fSGustavo F. Padovan 		*val = (unsigned long) opt->val;
19060a708f8fSGustavo F. Padovan 		break;
19070a708f8fSGustavo F. Padovan 	}
19080a708f8fSGustavo F. Padovan 
19090a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
19100a708f8fSGustavo F. Padovan 	return len;
19110a708f8fSGustavo F. Padovan }
19120a708f8fSGustavo F. Padovan 
19130a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
19140a708f8fSGustavo F. Padovan {
19150a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
19160a708f8fSGustavo F. Padovan 
19170a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
19180a708f8fSGustavo F. Padovan 
19190a708f8fSGustavo F. Padovan 	opt->type = type;
19200a708f8fSGustavo F. Padovan 	opt->len  = len;
19210a708f8fSGustavo F. Padovan 
19220a708f8fSGustavo F. Padovan 	switch (len) {
19230a708f8fSGustavo F. Padovan 	case 1:
19240a708f8fSGustavo F. Padovan 		*((u8 *) opt->val)  = val;
19250a708f8fSGustavo F. Padovan 		break;
19260a708f8fSGustavo F. Padovan 
19270a708f8fSGustavo F. Padovan 	case 2:
19280a708f8fSGustavo F. Padovan 		put_unaligned_le16(val, opt->val);
19290a708f8fSGustavo F. Padovan 		break;
19300a708f8fSGustavo F. Padovan 
19310a708f8fSGustavo F. Padovan 	case 4:
19320a708f8fSGustavo F. Padovan 		put_unaligned_le32(val, opt->val);
19330a708f8fSGustavo F. Padovan 		break;
19340a708f8fSGustavo F. Padovan 
19350a708f8fSGustavo F. Padovan 	default:
19360a708f8fSGustavo F. Padovan 		memcpy(opt->val, (void *) val, len);
19370a708f8fSGustavo F. Padovan 		break;
19380a708f8fSGustavo F. Padovan 	}
19390a708f8fSGustavo F. Padovan 
19400a708f8fSGustavo F. Padovan 	*ptr += L2CAP_CONF_OPT_SIZE + len;
19410a708f8fSGustavo F. Padovan }
19420a708f8fSGustavo F. Padovan 
1943f89cef09SAndrei Emeltchenko static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
1944f89cef09SAndrei Emeltchenko {
1945f89cef09SAndrei Emeltchenko 	struct l2cap_conf_efs efs;
1946f89cef09SAndrei Emeltchenko 
1947f89cef09SAndrei Emeltchenko 	switch (chan->mode) {
1948f89cef09SAndrei Emeltchenko 	case L2CAP_MODE_ERTM:
1949f89cef09SAndrei Emeltchenko 		efs.id		= chan->local_id;
1950f89cef09SAndrei Emeltchenko 		efs.stype	= chan->local_stype;
1951f89cef09SAndrei Emeltchenko 		efs.msdu	= cpu_to_le16(chan->local_msdu);
1952f89cef09SAndrei Emeltchenko 		efs.sdu_itime	= cpu_to_le32(chan->local_sdu_itime);
1953f89cef09SAndrei Emeltchenko 		efs.acc_lat	= cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
1954f89cef09SAndrei Emeltchenko 		efs.flush_to	= cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO);
1955f89cef09SAndrei Emeltchenko 		break;
1956f89cef09SAndrei Emeltchenko 
1957f89cef09SAndrei Emeltchenko 	case L2CAP_MODE_STREAMING:
1958f89cef09SAndrei Emeltchenko 		efs.id		= 1;
1959f89cef09SAndrei Emeltchenko 		efs.stype	= L2CAP_SERV_BESTEFFORT;
1960f89cef09SAndrei Emeltchenko 		efs.msdu	= cpu_to_le16(chan->local_msdu);
1961f89cef09SAndrei Emeltchenko 		efs.sdu_itime	= cpu_to_le32(chan->local_sdu_itime);
1962f89cef09SAndrei Emeltchenko 		efs.acc_lat	= 0;
1963f89cef09SAndrei Emeltchenko 		efs.flush_to	= 0;
1964f89cef09SAndrei Emeltchenko 		break;
1965f89cef09SAndrei Emeltchenko 
1966f89cef09SAndrei Emeltchenko 	default:
1967f89cef09SAndrei Emeltchenko 		return;
1968f89cef09SAndrei Emeltchenko 	}
1969f89cef09SAndrei Emeltchenko 
1970f89cef09SAndrei Emeltchenko 	l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
1971f89cef09SAndrei Emeltchenko 							(unsigned long) &efs);
1972f89cef09SAndrei Emeltchenko }
1973f89cef09SAndrei Emeltchenko 
1974721c4181SGustavo F. Padovan static void l2cap_ack_timeout(struct work_struct *work)
19750a708f8fSGustavo F. Padovan {
1976721c4181SGustavo F. Padovan 	struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
1977721c4181SGustavo F. Padovan 							ack_timer.work);
19780a708f8fSGustavo F. Padovan 
19792fb9b3d4SGustavo F. Padovan 	BT_DBG("chan %p", chan);
19802fb9b3d4SGustavo F. Padovan 
1981721c4181SGustavo F. Padovan 	lock_sock(chan->sk);
1982b17e73bbSSzymon Janc 	__l2cap_send_ack(chan);
1983721c4181SGustavo F. Padovan 	release_sock(chan->sk);
198409bfb2eeSSzymon Janc 
198509bfb2eeSSzymon Janc 	l2cap_chan_put(chan);
19860a708f8fSGustavo F. Padovan }
19870a708f8fSGustavo F. Padovan 
1988525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan)
19890a708f8fSGustavo F. Padovan {
199042e5c802SGustavo F. Padovan 	chan->expected_ack_seq = 0;
19916a026610SGustavo F. Padovan 	chan->unacked_frames = 0;
199242e5c802SGustavo F. Padovan 	chan->buffer_seq = 0;
19936a026610SGustavo F. Padovan 	chan->num_acked = 0;
19946a026610SGustavo F. Padovan 	chan->frames_sent = 0;
19950a708f8fSGustavo F. Padovan 
1996721c4181SGustavo F. Padovan 	INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
1997721c4181SGustavo F. Padovan 	INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
1998721c4181SGustavo F. Padovan 	INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
19990a708f8fSGustavo F. Padovan 
2000f1c6775bSGustavo F. Padovan 	skb_queue_head_init(&chan->srej_q);
20010a708f8fSGustavo F. Padovan 
200239d5a3eeSGustavo F. Padovan 	INIT_LIST_HEAD(&chan->srej_l);
20030a708f8fSGustavo F. Padovan }
20040a708f8fSGustavo F. Padovan 
20050a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
20060a708f8fSGustavo F. Padovan {
20070a708f8fSGustavo F. Padovan 	switch (mode) {
20080a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
20090a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
20100a708f8fSGustavo F. Padovan 		if (l2cap_mode_supported(mode, remote_feat_mask))
20110a708f8fSGustavo F. Padovan 			return mode;
20120a708f8fSGustavo F. Padovan 		/* fall through */
20130a708f8fSGustavo F. Padovan 	default:
20140a708f8fSGustavo F. Padovan 		return L2CAP_MODE_BASIC;
20150a708f8fSGustavo F. Padovan 	}
20160a708f8fSGustavo F. Padovan }
20170a708f8fSGustavo F. Padovan 
20186327eb98SAndrei Emeltchenko static inline bool __l2cap_ews_supported(struct l2cap_chan *chan)
20196327eb98SAndrei Emeltchenko {
20206327eb98SAndrei Emeltchenko 	return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
20216327eb98SAndrei Emeltchenko }
20226327eb98SAndrei Emeltchenko 
2023f89cef09SAndrei Emeltchenko static inline bool __l2cap_efs_supported(struct l2cap_chan *chan)
2024f89cef09SAndrei Emeltchenko {
2025f89cef09SAndrei Emeltchenko 	return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
2026f89cef09SAndrei Emeltchenko }
2027f89cef09SAndrei Emeltchenko 
20286327eb98SAndrei Emeltchenko static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
20296327eb98SAndrei Emeltchenko {
20306327eb98SAndrei Emeltchenko 	if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
2031836be934SAndrei Emeltchenko 						__l2cap_ews_supported(chan)) {
20326327eb98SAndrei Emeltchenko 		/* use extended control field */
20336327eb98SAndrei Emeltchenko 		set_bit(FLAG_EXT_CTRL, &chan->flags);
2034836be934SAndrei Emeltchenko 		chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
2035836be934SAndrei Emeltchenko 	} else {
20366327eb98SAndrei Emeltchenko 		chan->tx_win = min_t(u16, chan->tx_win,
20376327eb98SAndrei Emeltchenko 						L2CAP_DEFAULT_TX_WINDOW);
2038836be934SAndrei Emeltchenko 		chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
2039836be934SAndrei Emeltchenko 	}
20406327eb98SAndrei Emeltchenko }
20416327eb98SAndrei Emeltchenko 
2042710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
20430a708f8fSGustavo F. Padovan {
20440a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
20450c1bc5c6SGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
20460a708f8fSGustavo F. Padovan 	void *ptr = req->data;
2047c8f79162SAndrei Emeltchenko 	u16 size;
20480a708f8fSGustavo F. Padovan 
204949208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
20500a708f8fSGustavo F. Padovan 
205173ffa904SGustavo F. Padovan 	if (chan->num_conf_req || chan->num_conf_rsp)
20520a708f8fSGustavo F. Padovan 		goto done;
20530a708f8fSGustavo F. Padovan 
20540c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
20550a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
20560a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
2057c1360a1cSGustavo F. Padovan 		if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
20580a708f8fSGustavo F. Padovan 			break;
20590a708f8fSGustavo F. Padovan 
2060f89cef09SAndrei Emeltchenko 		if (__l2cap_efs_supported(chan))
2061f89cef09SAndrei Emeltchenko 			set_bit(FLAG_EFS_ENABLE, &chan->flags);
2062f89cef09SAndrei Emeltchenko 
20630a708f8fSGustavo F. Padovan 		/* fall through */
20640a708f8fSGustavo F. Padovan 	default:
20658c1d787bSGustavo F. Padovan 		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
20660a708f8fSGustavo F. Padovan 		break;
20670a708f8fSGustavo F. Padovan 	}
20680a708f8fSGustavo F. Padovan 
20690a708f8fSGustavo F. Padovan done:
20700c1bc5c6SGustavo F. Padovan 	if (chan->imtu != L2CAP_DEFAULT_MTU)
20710c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
20720a708f8fSGustavo F. Padovan 
20730c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
20740a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
20758c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
20768c1d787bSGustavo F. Padovan 				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
20770a708f8fSGustavo F. Padovan 			break;
20780a708f8fSGustavo F. Padovan 
20790a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_BASIC;
20800a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
20810a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
20820a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
20830a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
20840a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = 0;
20850a708f8fSGustavo F. Padovan 
20860a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
20870a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
20880a708f8fSGustavo F. Padovan 		break;
20890a708f8fSGustavo F. Padovan 
20900a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
20910a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_ERTM;
209247d1ec61SGustavo F. Padovan 		rfc.max_transmit    = chan->max_tx;
20930a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
20940a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
2095c8f79162SAndrei Emeltchenko 
2096c8f79162SAndrei Emeltchenko 		size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
2097c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2098c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2099c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2100c8f79162SAndrei Emeltchenko 		rfc.max_pdu_size = cpu_to_le16(size);
21010a708f8fSGustavo F. Padovan 
21026327eb98SAndrei Emeltchenko 		l2cap_txwin_setup(chan);
21036327eb98SAndrei Emeltchenko 
21046327eb98SAndrei Emeltchenko 		rfc.txwin_size = min_t(u16, chan->tx_win,
21056327eb98SAndrei Emeltchenko 						L2CAP_DEFAULT_TX_WINDOW);
21060a708f8fSGustavo F. Padovan 
21070a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
21080a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
21090a708f8fSGustavo F. Padovan 
2110f89cef09SAndrei Emeltchenko 		if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
2111f89cef09SAndrei Emeltchenko 			l2cap_add_opt_efs(&ptr, chan);
2112f89cef09SAndrei Emeltchenko 
21138c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
21140a708f8fSGustavo F. Padovan 			break;
21150a708f8fSGustavo F. Padovan 
211647d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
2117c1360a1cSGustavo F. Padovan 				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
211847d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
211947d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
21200a708f8fSGustavo F. Padovan 		}
21216327eb98SAndrei Emeltchenko 
21226327eb98SAndrei Emeltchenko 		if (test_bit(FLAG_EXT_CTRL, &chan->flags))
21236327eb98SAndrei Emeltchenko 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
21246327eb98SAndrei Emeltchenko 								chan->tx_win);
21250a708f8fSGustavo F. Padovan 		break;
21260a708f8fSGustavo F. Padovan 
21270a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
21280a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_STREAMING;
21290a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
21300a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
21310a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
21320a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
2133c8f79162SAndrei Emeltchenko 
2134c8f79162SAndrei Emeltchenko 		size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
2135c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2136c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2137c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2138c8f79162SAndrei Emeltchenko 		rfc.max_pdu_size = cpu_to_le16(size);
21390a708f8fSGustavo F. Padovan 
21400a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
21410a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
21420a708f8fSGustavo F. Padovan 
2143f89cef09SAndrei Emeltchenko 		if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
2144f89cef09SAndrei Emeltchenko 			l2cap_add_opt_efs(&ptr, chan);
2145f89cef09SAndrei Emeltchenko 
21468c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
21470a708f8fSGustavo F. Padovan 			break;
21480a708f8fSGustavo F. Padovan 
214947d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
2150c1360a1cSGustavo F. Padovan 				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
215147d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
215247d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
21530a708f8fSGustavo F. Padovan 		}
21540a708f8fSGustavo F. Padovan 		break;
21550a708f8fSGustavo F. Padovan 	}
21560a708f8fSGustavo F. Padovan 
2157fe4128e0SGustavo F. Padovan 	req->dcid  = cpu_to_le16(chan->dcid);
21580a708f8fSGustavo F. Padovan 	req->flags = cpu_to_le16(0);
21590a708f8fSGustavo F. Padovan 
21600a708f8fSGustavo F. Padovan 	return ptr - data;
21610a708f8fSGustavo F. Padovan }
21620a708f8fSGustavo F. Padovan 
216373ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
21640a708f8fSGustavo F. Padovan {
21650a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
21660a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
216773ffa904SGustavo F. Padovan 	void *req = chan->conf_req;
216873ffa904SGustavo F. Padovan 	int len = chan->conf_len;
21690a708f8fSGustavo F. Padovan 	int type, hint, olen;
21700a708f8fSGustavo F. Padovan 	unsigned long val;
21710a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
217242dceae2SAndrei Emeltchenko 	struct l2cap_conf_efs efs;
217342dceae2SAndrei Emeltchenko 	u8 remote_efs = 0;
21740a708f8fSGustavo F. Padovan 	u16 mtu = L2CAP_DEFAULT_MTU;
21750a708f8fSGustavo F. Padovan 	u16 result = L2CAP_CONF_SUCCESS;
2176c8f79162SAndrei Emeltchenko 	u16 size;
21770a708f8fSGustavo F. Padovan 
217873ffa904SGustavo F. Padovan 	BT_DBG("chan %p", chan);
21790a708f8fSGustavo F. Padovan 
21800a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
21810a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
21820a708f8fSGustavo F. Padovan 
21830a708f8fSGustavo F. Padovan 		hint  = type & L2CAP_CONF_HINT;
21840a708f8fSGustavo F. Padovan 		type &= L2CAP_CONF_MASK;
21850a708f8fSGustavo F. Padovan 
21860a708f8fSGustavo F. Padovan 		switch (type) {
21870a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
21880a708f8fSGustavo F. Padovan 			mtu = val;
21890a708f8fSGustavo F. Padovan 			break;
21900a708f8fSGustavo F. Padovan 
21910a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
21920c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
21930a708f8fSGustavo F. Padovan 			break;
21940a708f8fSGustavo F. Padovan 
21950a708f8fSGustavo F. Padovan 		case L2CAP_CONF_QOS:
21960a708f8fSGustavo F. Padovan 			break;
21970a708f8fSGustavo F. Padovan 
21980a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
21990a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
22000a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *) val, olen);
22010a708f8fSGustavo F. Padovan 			break;
22020a708f8fSGustavo F. Padovan 
22030a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FCS:
22040a708f8fSGustavo F. Padovan 			if (val == L2CAP_FCS_NONE)
2205c1360a1cSGustavo F. Padovan 				set_bit(CONF_NO_FCS_RECV, &chan->conf_state);
220642dceae2SAndrei Emeltchenko 			break;
22070a708f8fSGustavo F. Padovan 
220842dceae2SAndrei Emeltchenko 		case L2CAP_CONF_EFS:
220942dceae2SAndrei Emeltchenko 			remote_efs = 1;
221042dceae2SAndrei Emeltchenko 			if (olen == sizeof(efs))
221142dceae2SAndrei Emeltchenko 				memcpy(&efs, (void *) val, olen);
22120a708f8fSGustavo F. Padovan 			break;
22130a708f8fSGustavo F. Padovan 
22146327eb98SAndrei Emeltchenko 		case L2CAP_CONF_EWS:
22156327eb98SAndrei Emeltchenko 			if (!enable_hs)
22166327eb98SAndrei Emeltchenko 				return -ECONNREFUSED;
22176327eb98SAndrei Emeltchenko 
22186327eb98SAndrei Emeltchenko 			set_bit(FLAG_EXT_CTRL, &chan->flags);
22196327eb98SAndrei Emeltchenko 			set_bit(CONF_EWS_RECV, &chan->conf_state);
2220836be934SAndrei Emeltchenko 			chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
22216327eb98SAndrei Emeltchenko 			chan->remote_tx_win = val;
22220a708f8fSGustavo F. Padovan 			break;
22230a708f8fSGustavo F. Padovan 
22240a708f8fSGustavo F. Padovan 		default:
22250a708f8fSGustavo F. Padovan 			if (hint)
22260a708f8fSGustavo F. Padovan 				break;
22270a708f8fSGustavo F. Padovan 
22280a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNKNOWN;
22290a708f8fSGustavo F. Padovan 			*((u8 *) ptr++) = type;
22300a708f8fSGustavo F. Padovan 			break;
22310a708f8fSGustavo F. Padovan 		}
22320a708f8fSGustavo F. Padovan 	}
22330a708f8fSGustavo F. Padovan 
223473ffa904SGustavo F. Padovan 	if (chan->num_conf_rsp || chan->num_conf_req > 1)
22350a708f8fSGustavo F. Padovan 		goto done;
22360a708f8fSGustavo F. Padovan 
22370c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
22380a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
22390a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
2240c1360a1cSGustavo F. Padovan 		if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
22410c1bc5c6SGustavo F. Padovan 			chan->mode = l2cap_select_mode(rfc.mode,
22428c1d787bSGustavo F. Padovan 					chan->conn->feat_mask);
22430a708f8fSGustavo F. Padovan 			break;
22440a708f8fSGustavo F. Padovan 		}
22450a708f8fSGustavo F. Padovan 
224642dceae2SAndrei Emeltchenko 		if (remote_efs) {
224742dceae2SAndrei Emeltchenko 			if (__l2cap_efs_supported(chan))
224842dceae2SAndrei Emeltchenko 				set_bit(FLAG_EFS_ENABLE, &chan->flags);
224942dceae2SAndrei Emeltchenko 			else
225042dceae2SAndrei Emeltchenko 				return -ECONNREFUSED;
225142dceae2SAndrei Emeltchenko 		}
225242dceae2SAndrei Emeltchenko 
22530c1bc5c6SGustavo F. Padovan 		if (chan->mode != rfc.mode)
22540a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
22550a708f8fSGustavo F. Padovan 
22560a708f8fSGustavo F. Padovan 		break;
22570a708f8fSGustavo F. Padovan 	}
22580a708f8fSGustavo F. Padovan 
22590a708f8fSGustavo F. Padovan done:
22600c1bc5c6SGustavo F. Padovan 	if (chan->mode != rfc.mode) {
22610a708f8fSGustavo F. Padovan 		result = L2CAP_CONF_UNACCEPT;
22620c1bc5c6SGustavo F. Padovan 		rfc.mode = chan->mode;
22630a708f8fSGustavo F. Padovan 
226473ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp == 1)
22650a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
22660a708f8fSGustavo F. Padovan 
22670a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
22680a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
22690a708f8fSGustavo F. Padovan 	}
22700a708f8fSGustavo F. Padovan 
22710a708f8fSGustavo F. Padovan 	if (result == L2CAP_CONF_SUCCESS) {
22720a708f8fSGustavo F. Padovan 		/* Configure output options and let the other side know
22730a708f8fSGustavo F. Padovan 		 * which ones we don't like. */
22740a708f8fSGustavo F. Padovan 
22750a708f8fSGustavo F. Padovan 		if (mtu < L2CAP_DEFAULT_MIN_MTU)
22760a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
22770a708f8fSGustavo F. Padovan 		else {
22780c1bc5c6SGustavo F. Padovan 			chan->omtu = mtu;
2279c1360a1cSGustavo F. Padovan 			set_bit(CONF_MTU_DONE, &chan->conf_state);
22800a708f8fSGustavo F. Padovan 		}
22810c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
22820a708f8fSGustavo F. Padovan 
228342dceae2SAndrei Emeltchenko 		if (remote_efs) {
228442dceae2SAndrei Emeltchenko 			if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
228542dceae2SAndrei Emeltchenko 					efs.stype != L2CAP_SERV_NOTRAFIC &&
228642dceae2SAndrei Emeltchenko 					efs.stype != chan->local_stype) {
228742dceae2SAndrei Emeltchenko 
228842dceae2SAndrei Emeltchenko 				result = L2CAP_CONF_UNACCEPT;
228942dceae2SAndrei Emeltchenko 
229042dceae2SAndrei Emeltchenko 				if (chan->num_conf_req >= 1)
229142dceae2SAndrei Emeltchenko 					return -ECONNREFUSED;
229242dceae2SAndrei Emeltchenko 
229342dceae2SAndrei Emeltchenko 				l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
229442dceae2SAndrei Emeltchenko 							sizeof(efs),
229542dceae2SAndrei Emeltchenko 							(unsigned long) &efs);
22960e8b207eSAndrei Emeltchenko 			} else {
22973e6b3b95SGustavo F. Padovan 				/* Send PENDING Conf Rsp */
22980e8b207eSAndrei Emeltchenko 				result = L2CAP_CONF_PENDING;
22990e8b207eSAndrei Emeltchenko 				set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
230042dceae2SAndrei Emeltchenko 			}
230142dceae2SAndrei Emeltchenko 		}
230242dceae2SAndrei Emeltchenko 
23030a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
23040a708f8fSGustavo F. Padovan 		case L2CAP_MODE_BASIC:
230547d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
2306c1360a1cSGustavo F. Padovan 			set_bit(CONF_MODE_DONE, &chan->conf_state);
23070a708f8fSGustavo F. Padovan 			break;
23080a708f8fSGustavo F. Padovan 
23090a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
23106327eb98SAndrei Emeltchenko 			if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
23112c03a7a4SGustavo F. Padovan 				chan->remote_tx_win = rfc.txwin_size;
23126327eb98SAndrei Emeltchenko 			else
23136327eb98SAndrei Emeltchenko 				rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
23146327eb98SAndrei Emeltchenko 
23152c03a7a4SGustavo F. Padovan 			chan->remote_max_tx = rfc.max_transmit;
23160a708f8fSGustavo F. Padovan 
2317c8f79162SAndrei Emeltchenko 			size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
2318c8f79162SAndrei Emeltchenko 						chan->conn->mtu -
2319c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2320c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2321c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2322c8f79162SAndrei Emeltchenko 			rfc.max_pdu_size = cpu_to_le16(size);
2323c8f79162SAndrei Emeltchenko 			chan->remote_mps = size;
23240a708f8fSGustavo F. Padovan 
23250a708f8fSGustavo F. Padovan 			rfc.retrans_timeout =
23260a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
23270a708f8fSGustavo F. Padovan 			rfc.monitor_timeout =
23280a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
23290a708f8fSGustavo F. Padovan 
2330c1360a1cSGustavo F. Padovan 			set_bit(CONF_MODE_DONE, &chan->conf_state);
23310a708f8fSGustavo F. Padovan 
23320a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
23330a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
23340a708f8fSGustavo F. Padovan 
233542dceae2SAndrei Emeltchenko 			if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
233642dceae2SAndrei Emeltchenko 				chan->remote_id = efs.id;
233742dceae2SAndrei Emeltchenko 				chan->remote_stype = efs.stype;
233842dceae2SAndrei Emeltchenko 				chan->remote_msdu = le16_to_cpu(efs.msdu);
233942dceae2SAndrei Emeltchenko 				chan->remote_flush_to =
234042dceae2SAndrei Emeltchenko 						le32_to_cpu(efs.flush_to);
234142dceae2SAndrei Emeltchenko 				chan->remote_acc_lat =
234242dceae2SAndrei Emeltchenko 						le32_to_cpu(efs.acc_lat);
234342dceae2SAndrei Emeltchenko 				chan->remote_sdu_itime =
234442dceae2SAndrei Emeltchenko 					le32_to_cpu(efs.sdu_itime);
234542dceae2SAndrei Emeltchenko 				l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
234642dceae2SAndrei Emeltchenko 					sizeof(efs), (unsigned long) &efs);
234742dceae2SAndrei Emeltchenko 			}
23480a708f8fSGustavo F. Padovan 			break;
23490a708f8fSGustavo F. Padovan 
23500a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
2351c8f79162SAndrei Emeltchenko 			size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
2352c8f79162SAndrei Emeltchenko 						chan->conn->mtu -
2353c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2354c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2355c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2356c8f79162SAndrei Emeltchenko 			rfc.max_pdu_size = cpu_to_le16(size);
2357c8f79162SAndrei Emeltchenko 			chan->remote_mps = size;
23580a708f8fSGustavo F. Padovan 
2359c1360a1cSGustavo F. Padovan 			set_bit(CONF_MODE_DONE, &chan->conf_state);
23600a708f8fSGustavo F. Padovan 
23610a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
23620a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
23630a708f8fSGustavo F. Padovan 
23640a708f8fSGustavo F. Padovan 			break;
23650a708f8fSGustavo F. Padovan 
23660a708f8fSGustavo F. Padovan 		default:
23670a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
23680a708f8fSGustavo F. Padovan 
23690a708f8fSGustavo F. Padovan 			memset(&rfc, 0, sizeof(rfc));
23700c1bc5c6SGustavo F. Padovan 			rfc.mode = chan->mode;
23710a708f8fSGustavo F. Padovan 		}
23720a708f8fSGustavo F. Padovan 
23730a708f8fSGustavo F. Padovan 		if (result == L2CAP_CONF_SUCCESS)
2374c1360a1cSGustavo F. Padovan 			set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
23750a708f8fSGustavo F. Padovan 	}
2376fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
23770a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
23780a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(0x0000);
23790a708f8fSGustavo F. Padovan 
23800a708f8fSGustavo F. Padovan 	return ptr - data;
23810a708f8fSGustavo F. Padovan }
23820a708f8fSGustavo F. Padovan 
2383b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
23840a708f8fSGustavo F. Padovan {
23850a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
23860a708f8fSGustavo F. Padovan 	void *ptr = req->data;
23870a708f8fSGustavo F. Padovan 	int type, olen;
23880a708f8fSGustavo F. Padovan 	unsigned long val;
238936e999a8SMat Martineau 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
239066af7aafSAndrei Emeltchenko 	struct l2cap_conf_efs efs;
23910a708f8fSGustavo F. Padovan 
2392fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
23930a708f8fSGustavo F. Padovan 
23940a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
23950a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
23960a708f8fSGustavo F. Padovan 
23970a708f8fSGustavo F. Padovan 		switch (type) {
23980a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
23990a708f8fSGustavo F. Padovan 			if (val < L2CAP_DEFAULT_MIN_MTU) {
24000a708f8fSGustavo F. Padovan 				*result = L2CAP_CONF_UNACCEPT;
24010c1bc5c6SGustavo F. Padovan 				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
24020a708f8fSGustavo F. Padovan 			} else
24030c1bc5c6SGustavo F. Padovan 				chan->imtu = val;
24040c1bc5c6SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
24050a708f8fSGustavo F. Padovan 			break;
24060a708f8fSGustavo F. Padovan 
24070a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
24080c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
24090a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
24100c1bc5c6SGustavo F. Padovan 							2, chan->flush_to);
24110a708f8fSGustavo F. Padovan 			break;
24120a708f8fSGustavo F. Padovan 
24130a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
24140a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
24150a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
24160a708f8fSGustavo F. Padovan 
2417c1360a1cSGustavo F. Padovan 			if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
24180c1bc5c6SGustavo F. Padovan 							rfc.mode != chan->mode)
24190a708f8fSGustavo F. Padovan 				return -ECONNREFUSED;
24200a708f8fSGustavo F. Padovan 
242147d1ec61SGustavo F. Padovan 			chan->fcs = 0;
24220a708f8fSGustavo F. Padovan 
24230a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
24240a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
24250a708f8fSGustavo F. Padovan 			break;
24266327eb98SAndrei Emeltchenko 
24276327eb98SAndrei Emeltchenko 		case L2CAP_CONF_EWS:
24286327eb98SAndrei Emeltchenko 			chan->tx_win = min_t(u16, val,
24296327eb98SAndrei Emeltchenko 						L2CAP_DEFAULT_EXT_WINDOW);
24303e6b3b95SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
24313e6b3b95SGustavo F. Padovan 							chan->tx_win);
24326327eb98SAndrei Emeltchenko 			break;
243366af7aafSAndrei Emeltchenko 
243466af7aafSAndrei Emeltchenko 		case L2CAP_CONF_EFS:
243566af7aafSAndrei Emeltchenko 			if (olen == sizeof(efs))
243666af7aafSAndrei Emeltchenko 				memcpy(&efs, (void *)val, olen);
243766af7aafSAndrei Emeltchenko 
243866af7aafSAndrei Emeltchenko 			if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
243966af7aafSAndrei Emeltchenko 					efs.stype != L2CAP_SERV_NOTRAFIC &&
244066af7aafSAndrei Emeltchenko 					efs.stype != chan->local_stype)
244166af7aafSAndrei Emeltchenko 				return -ECONNREFUSED;
244266af7aafSAndrei Emeltchenko 
244366af7aafSAndrei Emeltchenko 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
244466af7aafSAndrei Emeltchenko 					sizeof(efs), (unsigned long) &efs);
244566af7aafSAndrei Emeltchenko 			break;
24460a708f8fSGustavo F. Padovan 		}
24470a708f8fSGustavo F. Padovan 	}
24480a708f8fSGustavo F. Padovan 
24490c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
24500a708f8fSGustavo F. Padovan 		return -ECONNREFUSED;
24510a708f8fSGustavo F. Padovan 
24520c1bc5c6SGustavo F. Padovan 	chan->mode = rfc.mode;
24530a708f8fSGustavo F. Padovan 
24540e8b207eSAndrei Emeltchenko 	if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
24550a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
24560a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
245747d1ec61SGustavo F. Padovan 			chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
245847d1ec61SGustavo F. Padovan 			chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
245947d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
246066af7aafSAndrei Emeltchenko 
246166af7aafSAndrei Emeltchenko 			if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
246266af7aafSAndrei Emeltchenko 				chan->local_msdu = le16_to_cpu(efs.msdu);
246366af7aafSAndrei Emeltchenko 				chan->local_sdu_itime =
246466af7aafSAndrei Emeltchenko 						le32_to_cpu(efs.sdu_itime);
246566af7aafSAndrei Emeltchenko 				chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
246666af7aafSAndrei Emeltchenko 				chan->local_flush_to =
246766af7aafSAndrei Emeltchenko 						le32_to_cpu(efs.flush_to);
246866af7aafSAndrei Emeltchenko 			}
24690a708f8fSGustavo F. Padovan 			break;
247066af7aafSAndrei Emeltchenko 
24710a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
247247d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
24730a708f8fSGustavo F. Padovan 		}
24740a708f8fSGustavo F. Padovan 	}
24750a708f8fSGustavo F. Padovan 
2476fe4128e0SGustavo F. Padovan 	req->dcid   = cpu_to_le16(chan->dcid);
24770a708f8fSGustavo F. Padovan 	req->flags  = cpu_to_le16(0x0000);
24780a708f8fSGustavo F. Padovan 
24790a708f8fSGustavo F. Padovan 	return ptr - data;
24800a708f8fSGustavo F. Padovan }
24810a708f8fSGustavo F. Padovan 
2482fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
24830a708f8fSGustavo F. Padovan {
24840a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
24850a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
24860a708f8fSGustavo F. Padovan 
2487fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p", chan);
24880a708f8fSGustavo F. Padovan 
2489fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
24900a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
24910a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(flags);
24920a708f8fSGustavo F. Padovan 
24930a708f8fSGustavo F. Padovan 	return ptr - data;
24940a708f8fSGustavo F. Padovan }
24950a708f8fSGustavo F. Padovan 
24968c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
2497710f9b0aSGustavo F. Padovan {
2498710f9b0aSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
24998c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
2500710f9b0aSGustavo F. Padovan 	u8 buf[128];
2501710f9b0aSGustavo F. Padovan 
2502fe4128e0SGustavo F. Padovan 	rsp.scid   = cpu_to_le16(chan->dcid);
2503fe4128e0SGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(chan->scid);
2504710f9b0aSGustavo F. Padovan 	rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
2505710f9b0aSGustavo F. Padovan 	rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
2506710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, chan->ident,
2507710f9b0aSGustavo F. Padovan 				L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2508710f9b0aSGustavo F. Padovan 
2509c1360a1cSGustavo F. Padovan 	if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
2510710f9b0aSGustavo F. Padovan 		return;
2511710f9b0aSGustavo F. Padovan 
2512710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2513710f9b0aSGustavo F. Padovan 			l2cap_build_conf_req(chan, buf), buf);
2514710f9b0aSGustavo F. Padovan 	chan->num_conf_req++;
2515710f9b0aSGustavo F. Padovan }
2516710f9b0aSGustavo F. Padovan 
251747d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
25180a708f8fSGustavo F. Padovan {
25190a708f8fSGustavo F. Padovan 	int type, olen;
25200a708f8fSGustavo F. Padovan 	unsigned long val;
25210a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
25220a708f8fSGustavo F. Padovan 
252347d1ec61SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
25240a708f8fSGustavo F. Padovan 
25250c1bc5c6SGustavo F. Padovan 	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
25260a708f8fSGustavo F. Padovan 		return;
25270a708f8fSGustavo F. Padovan 
25280a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
25290a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
25300a708f8fSGustavo F. Padovan 
25310a708f8fSGustavo F. Padovan 		switch (type) {
25320a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
25330a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
25340a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
25350a708f8fSGustavo F. Padovan 			goto done;
25360a708f8fSGustavo F. Padovan 		}
25370a708f8fSGustavo F. Padovan 	}
25380a708f8fSGustavo F. Padovan 
253936e999a8SMat Martineau 	/* Use sane default values in case a misbehaving remote device
254036e999a8SMat Martineau 	 * did not send an RFC option.
254136e999a8SMat Martineau 	 */
254236e999a8SMat Martineau 	rfc.mode = chan->mode;
254336e999a8SMat Martineau 	rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
254436e999a8SMat Martineau 	rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
254536e999a8SMat Martineau 	rfc.max_pdu_size = cpu_to_le16(chan->imtu);
254636e999a8SMat Martineau 
254736e999a8SMat Martineau 	BT_ERR("Expected RFC option was not found, using defaults");
254836e999a8SMat Martineau 
25490a708f8fSGustavo F. Padovan done:
25500a708f8fSGustavo F. Padovan 	switch (rfc.mode) {
25510a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
255247d1ec61SGustavo F. Padovan 		chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
255347d1ec61SGustavo F. Padovan 		chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
255447d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
25550a708f8fSGustavo F. Padovan 		break;
25560a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
255747d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
25580a708f8fSGustavo F. Padovan 	}
25590a708f8fSGustavo F. Padovan }
25600a708f8fSGustavo F. Padovan 
25610a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
25620a708f8fSGustavo F. Padovan {
2563e2fd318eSIlia Kolomisnky 	struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
25640a708f8fSGustavo F. Padovan 
2565e2fd318eSIlia Kolomisnky 	if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
25660a708f8fSGustavo F. Padovan 		return 0;
25670a708f8fSGustavo F. Padovan 
25680a708f8fSGustavo F. Padovan 	if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
25690a708f8fSGustavo F. Padovan 					cmd->ident == conn->info_ident) {
257017cd3f37SUlisses Furquim 		cancel_delayed_work(&conn->info_timer);
25710a708f8fSGustavo F. Padovan 
25720a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
25730a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
25740a708f8fSGustavo F. Padovan 
25750a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
25760a708f8fSGustavo F. Padovan 	}
25770a708f8fSGustavo F. Padovan 
25780a708f8fSGustavo F. Padovan 	return 0;
25790a708f8fSGustavo F. Padovan }
25800a708f8fSGustavo F. Padovan 
25810a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
25820a708f8fSGustavo F. Padovan {
25830a708f8fSGustavo F. Padovan 	struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
25840a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
258523691d75SGustavo F. Padovan 	struct l2cap_chan *chan = NULL, *pchan;
25860a708f8fSGustavo F. Padovan 	struct sock *parent, *sk = NULL;
25870a708f8fSGustavo F. Padovan 	int result, status = L2CAP_CS_NO_INFO;
25880a708f8fSGustavo F. Padovan 
25890a708f8fSGustavo F. Padovan 	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
25900a708f8fSGustavo F. Padovan 	__le16 psm = req->psm;
25910a708f8fSGustavo F. Padovan 
25920a708f8fSGustavo F. Padovan 	BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
25930a708f8fSGustavo F. Padovan 
25940a708f8fSGustavo F. Padovan 	/* Check if we have socket listening on psm */
259523691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src);
259623691d75SGustavo F. Padovan 	if (!pchan) {
25970a708f8fSGustavo F. Padovan 		result = L2CAP_CR_BAD_PSM;
25980a708f8fSGustavo F. Padovan 		goto sendresp;
25990a708f8fSGustavo F. Padovan 	}
26000a708f8fSGustavo F. Padovan 
260123691d75SGustavo F. Padovan 	parent = pchan->sk;
260223691d75SGustavo F. Padovan 
2603aa2ac881SGustavo F. Padovan 	lock_sock(parent);
26040a708f8fSGustavo F. Padovan 
26050a708f8fSGustavo F. Padovan 	/* Check if the ACL is secure enough (if not SDP) */
26060a708f8fSGustavo F. Padovan 	if (psm != cpu_to_le16(0x0001) &&
26070a708f8fSGustavo F. Padovan 				!hci_conn_check_link_mode(conn->hcon)) {
26089f5a0d7bSAndrei Emeltchenko 		conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
26090a708f8fSGustavo F. Padovan 		result = L2CAP_CR_SEC_BLOCK;
26100a708f8fSGustavo F. Padovan 		goto response;
26110a708f8fSGustavo F. Padovan 	}
26120a708f8fSGustavo F. Padovan 
26130a708f8fSGustavo F. Padovan 	result = L2CAP_CR_NO_MEM;
26140a708f8fSGustavo F. Padovan 
26150a708f8fSGustavo F. Padovan 	/* Check for backlog size */
26160a708f8fSGustavo F. Padovan 	if (sk_acceptq_is_full(parent)) {
26170a708f8fSGustavo F. Padovan 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
26180a708f8fSGustavo F. Padovan 		goto response;
26190a708f8fSGustavo F. Padovan 	}
26200a708f8fSGustavo F. Padovan 
262180808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
262280808e43SGustavo F. Padovan 	if (!chan)
26230a708f8fSGustavo F. Padovan 		goto response;
26240a708f8fSGustavo F. Padovan 
262580808e43SGustavo F. Padovan 	sk = chan->sk;
262680808e43SGustavo F. Padovan 
26270a708f8fSGustavo F. Padovan 	/* Check if we already have channel with that dcid */
2628baa7e1faSGustavo F. Padovan 	if (__l2cap_get_chan_by_dcid(conn, scid)) {
26290a708f8fSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
2630ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
26310a708f8fSGustavo F. Padovan 		goto response;
26320a708f8fSGustavo F. Padovan 	}
26330a708f8fSGustavo F. Padovan 
26340a708f8fSGustavo F. Padovan 	hci_conn_hold(conn->hcon);
26350a708f8fSGustavo F. Padovan 
26360a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->src, conn->src);
26370a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->dst, conn->dst);
2638fe4128e0SGustavo F. Padovan 	chan->psm  = psm;
2639fe4128e0SGustavo F. Padovan 	chan->dcid = scid;
26400a708f8fSGustavo F. Padovan 
2641d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
2642d1010240SGustavo F. Padovan 
26433d57dc68SGustavo F. Padovan 	l2cap_chan_add(conn, chan);
264448454079SGustavo F. Padovan 
2645fe4128e0SGustavo F. Padovan 	dcid = chan->scid;
26460a708f8fSGustavo F. Padovan 
2647c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
26480a708f8fSGustavo F. Padovan 
2649fc7f8a7eSGustavo F. Padovan 	chan->ident = cmd->ident;
26500a708f8fSGustavo F. Padovan 
26510a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
2652d45fc423SGustavo F. Padovan 		if (l2cap_chan_check_security(chan)) {
26530a708f8fSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup) {
265489bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONNECT2);
26550a708f8fSGustavo F. Padovan 				result = L2CAP_CR_PEND;
26560a708f8fSGustavo F. Padovan 				status = L2CAP_CS_AUTHOR_PEND;
26570a708f8fSGustavo F. Padovan 				parent->sk_data_ready(parent, 0);
26580a708f8fSGustavo F. Padovan 			} else {
265989bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONFIG);
26600a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SUCCESS;
26610a708f8fSGustavo F. Padovan 				status = L2CAP_CS_NO_INFO;
26620a708f8fSGustavo F. Padovan 			}
26630a708f8fSGustavo F. Padovan 		} else {
266489bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_CONNECT2);
26650a708f8fSGustavo F. Padovan 			result = L2CAP_CR_PEND;
26660a708f8fSGustavo F. Padovan 			status = L2CAP_CS_AUTHEN_PEND;
26670a708f8fSGustavo F. Padovan 		}
26680a708f8fSGustavo F. Padovan 	} else {
266989bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECT2);
26700a708f8fSGustavo F. Padovan 		result = L2CAP_CR_PEND;
26710a708f8fSGustavo F. Padovan 		status = L2CAP_CS_NO_INFO;
26720a708f8fSGustavo F. Padovan 	}
26730a708f8fSGustavo F. Padovan 
26740a708f8fSGustavo F. Padovan response:
2675aa2ac881SGustavo F. Padovan 	release_sock(parent);
26760a708f8fSGustavo F. Padovan 
26770a708f8fSGustavo F. Padovan sendresp:
26780a708f8fSGustavo F. Padovan 	rsp.scid   = cpu_to_le16(scid);
26790a708f8fSGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(dcid);
26800a708f8fSGustavo F. Padovan 	rsp.result = cpu_to_le16(result);
26810a708f8fSGustavo F. Padovan 	rsp.status = cpu_to_le16(status);
26820a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
26830a708f8fSGustavo F. Padovan 
26840a708f8fSGustavo F. Padovan 	if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
26850a708f8fSGustavo F. Padovan 		struct l2cap_info_req info;
26860a708f8fSGustavo F. Padovan 		info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
26870a708f8fSGustavo F. Padovan 
26880a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
26890a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
26900a708f8fSGustavo F. Padovan 
2691030013d8SGustavo F. Padovan 		schedule_delayed_work(&conn->info_timer,
26920a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
26930a708f8fSGustavo F. Padovan 
26940a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
26950a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(info), &info);
26960a708f8fSGustavo F. Padovan 	}
26970a708f8fSGustavo F. Padovan 
2698c1360a1cSGustavo F. Padovan 	if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
26990a708f8fSGustavo F. Padovan 				result == L2CAP_CR_SUCCESS) {
27000a708f8fSGustavo F. Padovan 		u8 buf[128];
2701c1360a1cSGustavo F. Padovan 		set_bit(CONF_REQ_SENT, &chan->conf_state);
27020a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
270373ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
270473ffa904SGustavo F. Padovan 		chan->num_conf_req++;
27050a708f8fSGustavo F. Padovan 	}
27060a708f8fSGustavo F. Padovan 
27070a708f8fSGustavo F. Padovan 	return 0;
27080a708f8fSGustavo F. Padovan }
27090a708f8fSGustavo F. Padovan 
27100a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
27110a708f8fSGustavo F. Padovan {
27120a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
27130a708f8fSGustavo F. Padovan 	u16 scid, dcid, result, status;
271448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
27150a708f8fSGustavo F. Padovan 	struct sock *sk;
27160a708f8fSGustavo F. Padovan 	u8 req[128];
27170a708f8fSGustavo F. Padovan 
27180a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
27190a708f8fSGustavo F. Padovan 	dcid   = __le16_to_cpu(rsp->dcid);
27200a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
27210a708f8fSGustavo F. Padovan 	status = __le16_to_cpu(rsp->status);
27220a708f8fSGustavo F. Padovan 
27230a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
27240a708f8fSGustavo F. Padovan 
27250a708f8fSGustavo F. Padovan 	if (scid) {
2726baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, scid);
272748454079SGustavo F. Padovan 		if (!chan)
27280a708f8fSGustavo F. Padovan 			return -EFAULT;
27290a708f8fSGustavo F. Padovan 	} else {
2730baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
273148454079SGustavo F. Padovan 		if (!chan)
27320a708f8fSGustavo F. Padovan 			return -EFAULT;
27330a708f8fSGustavo F. Padovan 	}
27340a708f8fSGustavo F. Padovan 
273548454079SGustavo F. Padovan 	sk = chan->sk;
273648454079SGustavo F. Padovan 
27370a708f8fSGustavo F. Padovan 	switch (result) {
27380a708f8fSGustavo F. Padovan 	case L2CAP_CR_SUCCESS:
273989bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONFIG);
2740fc7f8a7eSGustavo F. Padovan 		chan->ident = 0;
2741fe4128e0SGustavo F. Padovan 		chan->dcid = dcid;
2742c1360a1cSGustavo F. Padovan 		clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
27430a708f8fSGustavo F. Padovan 
2744c1360a1cSGustavo F. Padovan 		if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
27450a708f8fSGustavo F. Padovan 			break;
27460a708f8fSGustavo F. Padovan 
27470a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
274873ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, req), req);
274973ffa904SGustavo F. Padovan 		chan->num_conf_req++;
27500a708f8fSGustavo F. Padovan 		break;
27510a708f8fSGustavo F. Padovan 
27520a708f8fSGustavo F. Padovan 	case L2CAP_CR_PEND:
2753c1360a1cSGustavo F. Padovan 		set_bit(CONF_CONNECT_PEND, &chan->conf_state);
27540a708f8fSGustavo F. Padovan 		break;
27550a708f8fSGustavo F. Padovan 
27560a708f8fSGustavo F. Padovan 	default:
275748454079SGustavo F. Padovan 		l2cap_chan_del(chan, ECONNREFUSED);
27580a708f8fSGustavo F. Padovan 		break;
27590a708f8fSGustavo F. Padovan 	}
27600a708f8fSGustavo F. Padovan 
2761aa2ac881SGustavo F. Padovan 	release_sock(sk);
27620a708f8fSGustavo F. Padovan 	return 0;
27630a708f8fSGustavo F. Padovan }
27640a708f8fSGustavo F. Padovan 
276547d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan)
27660a708f8fSGustavo F. Padovan {
27670a708f8fSGustavo F. Padovan 	/* FCS is enabled only in ERTM or streaming mode, if one or both
27680a708f8fSGustavo F. Padovan 	 * sides request it.
27690a708f8fSGustavo F. Padovan 	 */
27700c1bc5c6SGustavo F. Padovan 	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
277147d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_NONE;
2772c1360a1cSGustavo F. Padovan 	else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state))
277347d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_CRC16;
27740a708f8fSGustavo F. Padovan }
27750a708f8fSGustavo F. Padovan 
27760a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
27770a708f8fSGustavo F. Padovan {
27780a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
27790a708f8fSGustavo F. Padovan 	u16 dcid, flags;
27800a708f8fSGustavo F. Padovan 	u8 rsp[64];
278148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
27820a708f8fSGustavo F. Padovan 	struct sock *sk;
27830a708f8fSGustavo F. Padovan 	int len;
27840a708f8fSGustavo F. Padovan 
27850a708f8fSGustavo F. Padovan 	dcid  = __le16_to_cpu(req->dcid);
27860a708f8fSGustavo F. Padovan 	flags = __le16_to_cpu(req->flags);
27870a708f8fSGustavo F. Padovan 
27880a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
27890a708f8fSGustavo F. Padovan 
2790baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
279148454079SGustavo F. Padovan 	if (!chan)
27920a708f8fSGustavo F. Padovan 		return -ENOENT;
27930a708f8fSGustavo F. Padovan 
279448454079SGustavo F. Padovan 	sk = chan->sk;
279548454079SGustavo F. Padovan 
2796033b1142SDavid S. Miller 	if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
2797e2fd318eSIlia Kolomisnky 		struct l2cap_cmd_rej_cid rej;
27980a708f8fSGustavo F. Padovan 
2799e2fd318eSIlia Kolomisnky 		rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
2800e2fd318eSIlia Kolomisnky 		rej.scid = cpu_to_le16(chan->scid);
2801e2fd318eSIlia Kolomisnky 		rej.dcid = cpu_to_le16(chan->dcid);
2802e2fd318eSIlia Kolomisnky 
28030a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
28040a708f8fSGustavo F. Padovan 				sizeof(rej), &rej);
28050a708f8fSGustavo F. Padovan 		goto unlock;
28060a708f8fSGustavo F. Padovan 	}
28070a708f8fSGustavo F. Padovan 
28080a708f8fSGustavo F. Padovan 	/* Reject if config buffer is too small. */
28090a708f8fSGustavo F. Padovan 	len = cmd_len - sizeof(*req);
28107ac28817SDan Rosenberg 	if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) {
28110a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2812fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
28130a708f8fSGustavo F. Padovan 					L2CAP_CONF_REJECT, flags), rsp);
28140a708f8fSGustavo F. Padovan 		goto unlock;
28150a708f8fSGustavo F. Padovan 	}
28160a708f8fSGustavo F. Padovan 
28170a708f8fSGustavo F. Padovan 	/* Store config. */
281873ffa904SGustavo F. Padovan 	memcpy(chan->conf_req + chan->conf_len, req->data, len);
281973ffa904SGustavo F. Padovan 	chan->conf_len += len;
28200a708f8fSGustavo F. Padovan 
28210a708f8fSGustavo F. Padovan 	if (flags & 0x0001) {
28220a708f8fSGustavo F. Padovan 		/* Incomplete config. Send empty response. */
28230a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2824fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
28250a708f8fSGustavo F. Padovan 					L2CAP_CONF_SUCCESS, 0x0001), rsp);
28260a708f8fSGustavo F. Padovan 		goto unlock;
28270a708f8fSGustavo F. Padovan 	}
28280a708f8fSGustavo F. Padovan 
28290a708f8fSGustavo F. Padovan 	/* Complete config. */
283073ffa904SGustavo F. Padovan 	len = l2cap_parse_conf_req(chan, rsp);
28310a708f8fSGustavo F. Padovan 	if (len < 0) {
2832e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
28330a708f8fSGustavo F. Padovan 		goto unlock;
28340a708f8fSGustavo F. Padovan 	}
28350a708f8fSGustavo F. Padovan 
28360a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
283773ffa904SGustavo F. Padovan 	chan->num_conf_rsp++;
28380a708f8fSGustavo F. Padovan 
28390a708f8fSGustavo F. Padovan 	/* Reset config buffer. */
284073ffa904SGustavo F. Padovan 	chan->conf_len = 0;
28410a708f8fSGustavo F. Padovan 
2842c1360a1cSGustavo F. Padovan 	if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
28430a708f8fSGustavo F. Padovan 		goto unlock;
28440a708f8fSGustavo F. Padovan 
2845c1360a1cSGustavo F. Padovan 	if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
284647d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
28470a708f8fSGustavo F. Padovan 
284889bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
28490a708f8fSGustavo F. Padovan 
285042e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
285142e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
285258d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
28530c1bc5c6SGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_ERTM)
2854525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
28550a708f8fSGustavo F. Padovan 
2856cf4cd009SAndrei Emeltchenko 		l2cap_chan_ready(chan);
28570a708f8fSGustavo F. Padovan 		goto unlock;
28580a708f8fSGustavo F. Padovan 	}
28590a708f8fSGustavo F. Padovan 
2860c1360a1cSGustavo F. Padovan 	if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
28610a708f8fSGustavo F. Padovan 		u8 buf[64];
28620a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
286373ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
286473ffa904SGustavo F. Padovan 		chan->num_conf_req++;
28650a708f8fSGustavo F. Padovan 	}
28660a708f8fSGustavo F. Padovan 
28670e8b207eSAndrei Emeltchenko 	/* Got Conf Rsp PENDING from remote side and asume we sent
28680e8b207eSAndrei Emeltchenko 	   Conf Rsp PENDING in the code above */
28690e8b207eSAndrei Emeltchenko 	if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
28700e8b207eSAndrei Emeltchenko 			test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
28710e8b207eSAndrei Emeltchenko 
28720e8b207eSAndrei Emeltchenko 		/* check compatibility */
28730e8b207eSAndrei Emeltchenko 
28740e8b207eSAndrei Emeltchenko 		clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
28750e8b207eSAndrei Emeltchenko 		set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
28760e8b207eSAndrei Emeltchenko 
28770e8b207eSAndrei Emeltchenko 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
28780e8b207eSAndrei Emeltchenko 					l2cap_build_conf_rsp(chan, rsp,
28790e8b207eSAndrei Emeltchenko 					L2CAP_CONF_SUCCESS, 0x0000), rsp);
28800e8b207eSAndrei Emeltchenko 	}
28810e8b207eSAndrei Emeltchenko 
28820a708f8fSGustavo F. Padovan unlock:
2883aa2ac881SGustavo F. Padovan 	release_sock(sk);
28840a708f8fSGustavo F. Padovan 	return 0;
28850a708f8fSGustavo F. Padovan }
28860a708f8fSGustavo F. Padovan 
28870a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
28880a708f8fSGustavo F. Padovan {
28890a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
28900a708f8fSGustavo F. Padovan 	u16 scid, flags, result;
289148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
28920a708f8fSGustavo F. Padovan 	struct sock *sk;
28930a708f8fSGustavo F. Padovan 	int len = cmd->len - sizeof(*rsp);
28940a708f8fSGustavo F. Padovan 
28950a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
28960a708f8fSGustavo F. Padovan 	flags  = __le16_to_cpu(rsp->flags);
28970a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
28980a708f8fSGustavo F. Padovan 
28990a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
29000a708f8fSGustavo F. Padovan 			scid, flags, result);
29010a708f8fSGustavo F. Padovan 
2902baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
290348454079SGustavo F. Padovan 	if (!chan)
29040a708f8fSGustavo F. Padovan 		return 0;
29050a708f8fSGustavo F. Padovan 
290648454079SGustavo F. Padovan 	sk = chan->sk;
290748454079SGustavo F. Padovan 
29080a708f8fSGustavo F. Padovan 	switch (result) {
29090a708f8fSGustavo F. Padovan 	case L2CAP_CONF_SUCCESS:
291047d1ec61SGustavo F. Padovan 		l2cap_conf_rfc_get(chan, rsp->data, len);
29110e8b207eSAndrei Emeltchenko 		clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
29120a708f8fSGustavo F. Padovan 		break;
29130a708f8fSGustavo F. Padovan 
29140e8b207eSAndrei Emeltchenko 	case L2CAP_CONF_PENDING:
29150e8b207eSAndrei Emeltchenko 		set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
29160e8b207eSAndrei Emeltchenko 
29170e8b207eSAndrei Emeltchenko 		if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
29180e8b207eSAndrei Emeltchenko 			char buf[64];
29190e8b207eSAndrei Emeltchenko 
29200e8b207eSAndrei Emeltchenko 			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
29210e8b207eSAndrei Emeltchenko 								buf, &result);
29220e8b207eSAndrei Emeltchenko 			if (len < 0) {
29230e8b207eSAndrei Emeltchenko 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
29240e8b207eSAndrei Emeltchenko 				goto done;
29250e8b207eSAndrei Emeltchenko 			}
29260e8b207eSAndrei Emeltchenko 
29270e8b207eSAndrei Emeltchenko 			/* check compatibility */
29280e8b207eSAndrei Emeltchenko 
29290e8b207eSAndrei Emeltchenko 			clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
29300e8b207eSAndrei Emeltchenko 			set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
29310e8b207eSAndrei Emeltchenko 
29320e8b207eSAndrei Emeltchenko 			l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
29330e8b207eSAndrei Emeltchenko 						l2cap_build_conf_rsp(chan, buf,
29340e8b207eSAndrei Emeltchenko 						L2CAP_CONF_SUCCESS, 0x0000), buf);
29350e8b207eSAndrei Emeltchenko 		}
29360e8b207eSAndrei Emeltchenko 		goto done;
29370e8b207eSAndrei Emeltchenko 
29380a708f8fSGustavo F. Padovan 	case L2CAP_CONF_UNACCEPT:
293973ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
29400a708f8fSGustavo F. Padovan 			char req[64];
29410a708f8fSGustavo F. Padovan 
29420a708f8fSGustavo F. Padovan 			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2943e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
29440a708f8fSGustavo F. Padovan 				goto done;
29450a708f8fSGustavo F. Padovan 			}
29460a708f8fSGustavo F. Padovan 
29470a708f8fSGustavo F. Padovan 			/* throw out any old stored conf requests */
29480a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_SUCCESS;
2949b4450035SGustavo F. Padovan 			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
2950b4450035SGustavo F. Padovan 								req, &result);
29510a708f8fSGustavo F. Padovan 			if (len < 0) {
2952e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
29530a708f8fSGustavo F. Padovan 				goto done;
29540a708f8fSGustavo F. Padovan 			}
29550a708f8fSGustavo F. Padovan 
29560a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn),
29570a708f8fSGustavo F. Padovan 						L2CAP_CONF_REQ, len, req);
295873ffa904SGustavo F. Padovan 			chan->num_conf_req++;
29590a708f8fSGustavo F. Padovan 			if (result != L2CAP_CONF_SUCCESS)
29600a708f8fSGustavo F. Padovan 				goto done;
29610a708f8fSGustavo F. Padovan 			break;
29620a708f8fSGustavo F. Padovan 		}
29630a708f8fSGustavo F. Padovan 
29640a708f8fSGustavo F. Padovan 	default:
29650a708f8fSGustavo F. Padovan 		sk->sk_err = ECONNRESET;
2966b83ddfe2SAndrzej Kaczmarek 		__set_chan_timer(chan,
2967b83ddfe2SAndrzej Kaczmarek 				msecs_to_jiffies(L2CAP_DISC_REJ_TIMEOUT));
2968e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
29690a708f8fSGustavo F. Padovan 		goto done;
29700a708f8fSGustavo F. Padovan 	}
29710a708f8fSGustavo F. Padovan 
29720a708f8fSGustavo F. Padovan 	if (flags & 0x01)
29730a708f8fSGustavo F. Padovan 		goto done;
29740a708f8fSGustavo F. Padovan 
2975c1360a1cSGustavo F. Padovan 	set_bit(CONF_INPUT_DONE, &chan->conf_state);
29760a708f8fSGustavo F. Padovan 
2977c1360a1cSGustavo F. Padovan 	if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
297847d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
29790a708f8fSGustavo F. Padovan 
298089bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
298142e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
298242e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
298358d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
29840c1bc5c6SGustavo F. Padovan 		if (chan->mode ==  L2CAP_MODE_ERTM)
2985525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
29860a708f8fSGustavo F. Padovan 
2987cf4cd009SAndrei Emeltchenko 		l2cap_chan_ready(chan);
29880a708f8fSGustavo F. Padovan 	}
29890a708f8fSGustavo F. Padovan 
29900a708f8fSGustavo F. Padovan done:
2991aa2ac881SGustavo F. Padovan 	release_sock(sk);
29920a708f8fSGustavo F. Padovan 	return 0;
29930a708f8fSGustavo F. Padovan }
29940a708f8fSGustavo F. Padovan 
29950a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
29960a708f8fSGustavo F. Padovan {
29970a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
29980a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp rsp;
29990a708f8fSGustavo F. Padovan 	u16 dcid, scid;
300048454079SGustavo F. Padovan 	struct l2cap_chan *chan;
30010a708f8fSGustavo F. Padovan 	struct sock *sk;
30020a708f8fSGustavo F. Padovan 
30030a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(req->scid);
30040a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(req->dcid);
30050a708f8fSGustavo F. Padovan 
30060a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
30070a708f8fSGustavo F. Padovan 
3008baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
300948454079SGustavo F. Padovan 	if (!chan)
30100a708f8fSGustavo F. Padovan 		return 0;
30110a708f8fSGustavo F. Padovan 
301248454079SGustavo F. Padovan 	sk = chan->sk;
301348454079SGustavo F. Padovan 
3014fe4128e0SGustavo F. Padovan 	rsp.dcid = cpu_to_le16(chan->scid);
3015fe4128e0SGustavo F. Padovan 	rsp.scid = cpu_to_le16(chan->dcid);
30160a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
30170a708f8fSGustavo F. Padovan 
30180a708f8fSGustavo F. Padovan 	sk->sk_shutdown = SHUTDOWN_MASK;
30190a708f8fSGustavo F. Padovan 
302048454079SGustavo F. Padovan 	l2cap_chan_del(chan, ECONNRESET);
3021aa2ac881SGustavo F. Padovan 	release_sock(sk);
30220a708f8fSGustavo F. Padovan 
3023ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
30240a708f8fSGustavo F. Padovan 	return 0;
30250a708f8fSGustavo F. Padovan }
30260a708f8fSGustavo F. Padovan 
30270a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
30280a708f8fSGustavo F. Padovan {
30290a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
30300a708f8fSGustavo F. Padovan 	u16 dcid, scid;
303148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
30320a708f8fSGustavo F. Padovan 	struct sock *sk;
30330a708f8fSGustavo F. Padovan 
30340a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(rsp->scid);
30350a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(rsp->dcid);
30360a708f8fSGustavo F. Padovan 
30370a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
30380a708f8fSGustavo F. Padovan 
3039baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
304048454079SGustavo F. Padovan 	if (!chan)
30410a708f8fSGustavo F. Padovan 		return 0;
30420a708f8fSGustavo F. Padovan 
304348454079SGustavo F. Padovan 	sk = chan->sk;
304448454079SGustavo F. Padovan 
304548454079SGustavo F. Padovan 	l2cap_chan_del(chan, 0);
3046aa2ac881SGustavo F. Padovan 	release_sock(sk);
30470a708f8fSGustavo F. Padovan 
3048ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
30490a708f8fSGustavo F. Padovan 	return 0;
30500a708f8fSGustavo F. Padovan }
30510a708f8fSGustavo F. Padovan 
30520a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
30530a708f8fSGustavo F. Padovan {
30540a708f8fSGustavo F. Padovan 	struct l2cap_info_req *req = (struct l2cap_info_req *) data;
30550a708f8fSGustavo F. Padovan 	u16 type;
30560a708f8fSGustavo F. Padovan 
30570a708f8fSGustavo F. Padovan 	type = __le16_to_cpu(req->type);
30580a708f8fSGustavo F. Padovan 
30590a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x", type);
30600a708f8fSGustavo F. Padovan 
30610a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
30620a708f8fSGustavo F. Padovan 		u8 buf[8];
30630a708f8fSGustavo F. Padovan 		u32 feat_mask = l2cap_feat_mask;
30640a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
30650a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FEAT_MASK);
30660a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
30670a708f8fSGustavo F. Padovan 		if (!disable_ertm)
30680a708f8fSGustavo F. Padovan 			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
30690a708f8fSGustavo F. Padovan 							 | L2CAP_FEAT_FCS;
3070a5fd6f30SAndrei Emeltchenko 		if (enable_hs)
30716327eb98SAndrei Emeltchenko 			feat_mask |= L2CAP_FEAT_EXT_FLOW
30726327eb98SAndrei Emeltchenko 						| L2CAP_FEAT_EXT_WINDOW;
3073a5fd6f30SAndrei Emeltchenko 
30740a708f8fSGustavo F. Padovan 		put_unaligned_le32(feat_mask, rsp->data);
30750a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
30760a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
30770a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
30780a708f8fSGustavo F. Padovan 		u8 buf[12];
30790a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
308050a147cdSMat Martineau 
308150a147cdSMat Martineau 		if (enable_hs)
308250a147cdSMat Martineau 			l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
308350a147cdSMat Martineau 		else
308450a147cdSMat Martineau 			l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;
308550a147cdSMat Martineau 
30860a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
30870a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
3088c6337ea6SAndrei Emeltchenko 		memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan));
30890a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
30900a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
30910a708f8fSGustavo F. Padovan 	} else {
30920a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp rsp;
30930a708f8fSGustavo F. Padovan 		rsp.type   = cpu_to_le16(type);
30940a708f8fSGustavo F. Padovan 		rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
30950a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
30960a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(rsp), &rsp);
30970a708f8fSGustavo F. Padovan 	}
30980a708f8fSGustavo F. Padovan 
30990a708f8fSGustavo F. Padovan 	return 0;
31000a708f8fSGustavo F. Padovan }
31010a708f8fSGustavo F. Padovan 
31020a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
31030a708f8fSGustavo F. Padovan {
31040a708f8fSGustavo F. Padovan 	struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
31050a708f8fSGustavo F. Padovan 	u16 type, result;
31060a708f8fSGustavo F. Padovan 
31070a708f8fSGustavo F. Padovan 	type   = __le16_to_cpu(rsp->type);
31080a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
31090a708f8fSGustavo F. Padovan 
31100a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
31110a708f8fSGustavo F. Padovan 
3112e90165beSAndrei Emeltchenko 	/* L2CAP Info req/rsp are unbound to channels, add extra checks */
3113e90165beSAndrei Emeltchenko 	if (cmd->ident != conn->info_ident ||
3114e90165beSAndrei Emeltchenko 			conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
3115e90165beSAndrei Emeltchenko 		return 0;
3116e90165beSAndrei Emeltchenko 
311717cd3f37SUlisses Furquim 	cancel_delayed_work(&conn->info_timer);
31180a708f8fSGustavo F. Padovan 
31190a708f8fSGustavo F. Padovan 	if (result != L2CAP_IR_SUCCESS) {
31200a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
31210a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
31220a708f8fSGustavo F. Padovan 
31230a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
31240a708f8fSGustavo F. Padovan 
31250a708f8fSGustavo F. Padovan 		return 0;
31260a708f8fSGustavo F. Padovan 	}
31270a708f8fSGustavo F. Padovan 
31280a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
31290a708f8fSGustavo F. Padovan 		conn->feat_mask = get_unaligned_le32(rsp->data);
31300a708f8fSGustavo F. Padovan 
31310a708f8fSGustavo F. Padovan 		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
31320a708f8fSGustavo F. Padovan 			struct l2cap_info_req req;
31330a708f8fSGustavo F. Padovan 			req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
31340a708f8fSGustavo F. Padovan 
31350a708f8fSGustavo F. Padovan 			conn->info_ident = l2cap_get_ident(conn);
31360a708f8fSGustavo F. Padovan 
31370a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, conn->info_ident,
31380a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
31390a708f8fSGustavo F. Padovan 		} else {
31400a708f8fSGustavo F. Padovan 			conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
31410a708f8fSGustavo F. Padovan 			conn->info_ident = 0;
31420a708f8fSGustavo F. Padovan 
31430a708f8fSGustavo F. Padovan 			l2cap_conn_start(conn);
31440a708f8fSGustavo F. Padovan 		}
31450a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
31460a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
31470a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
31480a708f8fSGustavo F. Padovan 
31490a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
31500a708f8fSGustavo F. Padovan 	}
31510a708f8fSGustavo F. Padovan 
31520a708f8fSGustavo F. Padovan 	return 0;
31530a708f8fSGustavo F. Padovan }
31540a708f8fSGustavo F. Padovan 
3155f94ff6ffSMat Martineau static inline int l2cap_create_channel_req(struct l2cap_conn *conn,
3156f94ff6ffSMat Martineau 					struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3157f94ff6ffSMat Martineau 					void *data)
3158f94ff6ffSMat Martineau {
3159f94ff6ffSMat Martineau 	struct l2cap_create_chan_req *req = data;
3160f94ff6ffSMat Martineau 	struct l2cap_create_chan_rsp rsp;
3161f94ff6ffSMat Martineau 	u16 psm, scid;
3162f94ff6ffSMat Martineau 
3163f94ff6ffSMat Martineau 	if (cmd_len != sizeof(*req))
3164f94ff6ffSMat Martineau 		return -EPROTO;
3165f94ff6ffSMat Martineau 
3166f94ff6ffSMat Martineau 	if (!enable_hs)
3167f94ff6ffSMat Martineau 		return -EINVAL;
3168f94ff6ffSMat Martineau 
3169f94ff6ffSMat Martineau 	psm = le16_to_cpu(req->psm);
3170f94ff6ffSMat Martineau 	scid = le16_to_cpu(req->scid);
3171f94ff6ffSMat Martineau 
3172f94ff6ffSMat Martineau 	BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id);
3173f94ff6ffSMat Martineau 
3174f94ff6ffSMat Martineau 	/* Placeholder: Always reject */
3175f94ff6ffSMat Martineau 	rsp.dcid = 0;
3176f94ff6ffSMat Martineau 	rsp.scid = cpu_to_le16(scid);
3177f94ff6ffSMat Martineau 	rsp.result = L2CAP_CR_NO_MEM;
3178f94ff6ffSMat Martineau 	rsp.status = L2CAP_CS_NO_INFO;
3179f94ff6ffSMat Martineau 
3180f94ff6ffSMat Martineau 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
3181f94ff6ffSMat Martineau 		       sizeof(rsp), &rsp);
3182f94ff6ffSMat Martineau 
3183f94ff6ffSMat Martineau 	return 0;
3184f94ff6ffSMat Martineau }
3185f94ff6ffSMat Martineau 
3186f94ff6ffSMat Martineau static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn,
3187f94ff6ffSMat Martineau 					struct l2cap_cmd_hdr *cmd, void *data)
3188f94ff6ffSMat Martineau {
3189f94ff6ffSMat Martineau 	BT_DBG("conn %p", conn);
3190f94ff6ffSMat Martineau 
3191f94ff6ffSMat Martineau 	return l2cap_connect_rsp(conn, cmd, data);
3192f94ff6ffSMat Martineau }
3193f94ff6ffSMat Martineau 
31948d5a04a1SMat Martineau static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident,
31958d5a04a1SMat Martineau 							u16 icid, u16 result)
31968d5a04a1SMat Martineau {
31978d5a04a1SMat Martineau 	struct l2cap_move_chan_rsp rsp;
31988d5a04a1SMat Martineau 
31998d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
32008d5a04a1SMat Martineau 
32018d5a04a1SMat Martineau 	rsp.icid = cpu_to_le16(icid);
32028d5a04a1SMat Martineau 	rsp.result = cpu_to_le16(result);
32038d5a04a1SMat Martineau 
32048d5a04a1SMat Martineau 	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp);
32058d5a04a1SMat Martineau }
32068d5a04a1SMat Martineau 
32078d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn,
32088d5a04a1SMat Martineau 				struct l2cap_chan *chan, u16 icid, u16 result)
32098d5a04a1SMat Martineau {
32108d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm cfm;
32118d5a04a1SMat Martineau 	u8 ident;
32128d5a04a1SMat Martineau 
32138d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
32148d5a04a1SMat Martineau 
32158d5a04a1SMat Martineau 	ident = l2cap_get_ident(conn);
32168d5a04a1SMat Martineau 	if (chan)
32178d5a04a1SMat Martineau 		chan->ident = ident;
32188d5a04a1SMat Martineau 
32198d5a04a1SMat Martineau 	cfm.icid = cpu_to_le16(icid);
32208d5a04a1SMat Martineau 	cfm.result = cpu_to_le16(result);
32218d5a04a1SMat Martineau 
32228d5a04a1SMat Martineau 	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm);
32238d5a04a1SMat Martineau }
32248d5a04a1SMat Martineau 
32258d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
32268d5a04a1SMat Martineau 								u16 icid)
32278d5a04a1SMat Martineau {
32288d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm_rsp rsp;
32298d5a04a1SMat Martineau 
32308d5a04a1SMat Martineau 	BT_DBG("icid %d", icid);
32318d5a04a1SMat Martineau 
32328d5a04a1SMat Martineau 	rsp.icid = cpu_to_le16(icid);
32338d5a04a1SMat Martineau 	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
32348d5a04a1SMat Martineau }
32358d5a04a1SMat Martineau 
32368d5a04a1SMat Martineau static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
32378d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
32388d5a04a1SMat Martineau {
32398d5a04a1SMat Martineau 	struct l2cap_move_chan_req *req = data;
32408d5a04a1SMat Martineau 	u16 icid = 0;
32418d5a04a1SMat Martineau 	u16 result = L2CAP_MR_NOT_ALLOWED;
32428d5a04a1SMat Martineau 
32438d5a04a1SMat Martineau 	if (cmd_len != sizeof(*req))
32448d5a04a1SMat Martineau 		return -EPROTO;
32458d5a04a1SMat Martineau 
32468d5a04a1SMat Martineau 	icid = le16_to_cpu(req->icid);
32478d5a04a1SMat Martineau 
32488d5a04a1SMat Martineau 	BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id);
32498d5a04a1SMat Martineau 
32508d5a04a1SMat Martineau 	if (!enable_hs)
32518d5a04a1SMat Martineau 		return -EINVAL;
32528d5a04a1SMat Martineau 
32538d5a04a1SMat Martineau 	/* Placeholder: Always refuse */
32548d5a04a1SMat Martineau 	l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result);
32558d5a04a1SMat Martineau 
32568d5a04a1SMat Martineau 	return 0;
32578d5a04a1SMat Martineau }
32588d5a04a1SMat Martineau 
32598d5a04a1SMat Martineau static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn,
32608d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
32618d5a04a1SMat Martineau {
32628d5a04a1SMat Martineau 	struct l2cap_move_chan_rsp *rsp = data;
32638d5a04a1SMat Martineau 	u16 icid, result;
32648d5a04a1SMat Martineau 
32658d5a04a1SMat Martineau 	if (cmd_len != sizeof(*rsp))
32668d5a04a1SMat Martineau 		return -EPROTO;
32678d5a04a1SMat Martineau 
32688d5a04a1SMat Martineau 	icid = le16_to_cpu(rsp->icid);
32698d5a04a1SMat Martineau 	result = le16_to_cpu(rsp->result);
32708d5a04a1SMat Martineau 
32718d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
32728d5a04a1SMat Martineau 
32738d5a04a1SMat Martineau 	/* Placeholder: Always unconfirmed */
32748d5a04a1SMat Martineau 	l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED);
32758d5a04a1SMat Martineau 
32768d5a04a1SMat Martineau 	return 0;
32778d5a04a1SMat Martineau }
32788d5a04a1SMat Martineau 
32798d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn,
32808d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
32818d5a04a1SMat Martineau {
32828d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm *cfm = data;
32838d5a04a1SMat Martineau 	u16 icid, result;
32848d5a04a1SMat Martineau 
32858d5a04a1SMat Martineau 	if (cmd_len != sizeof(*cfm))
32868d5a04a1SMat Martineau 		return -EPROTO;
32878d5a04a1SMat Martineau 
32888d5a04a1SMat Martineau 	icid = le16_to_cpu(cfm->icid);
32898d5a04a1SMat Martineau 	result = le16_to_cpu(cfm->result);
32908d5a04a1SMat Martineau 
32918d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
32928d5a04a1SMat Martineau 
32938d5a04a1SMat Martineau 	l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
32948d5a04a1SMat Martineau 
32958d5a04a1SMat Martineau 	return 0;
32968d5a04a1SMat Martineau }
32978d5a04a1SMat Martineau 
32988d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
32998d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
33008d5a04a1SMat Martineau {
33018d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm_rsp *rsp = data;
33028d5a04a1SMat Martineau 	u16 icid;
33038d5a04a1SMat Martineau 
33048d5a04a1SMat Martineau 	if (cmd_len != sizeof(*rsp))
33058d5a04a1SMat Martineau 		return -EPROTO;
33068d5a04a1SMat Martineau 
33078d5a04a1SMat Martineau 	icid = le16_to_cpu(rsp->icid);
33088d5a04a1SMat Martineau 
33098d5a04a1SMat Martineau 	BT_DBG("icid %d", icid);
33108d5a04a1SMat Martineau 
33118d5a04a1SMat Martineau 	return 0;
33128d5a04a1SMat Martineau }
33138d5a04a1SMat Martineau 
3314e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
3315de73115aSClaudio Takahasi 							u16 to_multiplier)
3316de73115aSClaudio Takahasi {
3317de73115aSClaudio Takahasi 	u16 max_latency;
3318de73115aSClaudio Takahasi 
3319de73115aSClaudio Takahasi 	if (min > max || min < 6 || max > 3200)
3320de73115aSClaudio Takahasi 		return -EINVAL;
3321de73115aSClaudio Takahasi 
3322de73115aSClaudio Takahasi 	if (to_multiplier < 10 || to_multiplier > 3200)
3323de73115aSClaudio Takahasi 		return -EINVAL;
3324de73115aSClaudio Takahasi 
3325de73115aSClaudio Takahasi 	if (max >= to_multiplier * 8)
3326de73115aSClaudio Takahasi 		return -EINVAL;
3327de73115aSClaudio Takahasi 
3328de73115aSClaudio Takahasi 	max_latency = (to_multiplier * 8 / max) - 1;
3329de73115aSClaudio Takahasi 	if (latency > 499 || latency > max_latency)
3330de73115aSClaudio Takahasi 		return -EINVAL;
3331de73115aSClaudio Takahasi 
3332de73115aSClaudio Takahasi 	return 0;
3333de73115aSClaudio Takahasi }
3334de73115aSClaudio Takahasi 
3335de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
3336de73115aSClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
3337de73115aSClaudio Takahasi {
3338de73115aSClaudio Takahasi 	struct hci_conn *hcon = conn->hcon;
3339de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_req *req;
3340de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_rsp rsp;
3341de73115aSClaudio Takahasi 	u16 min, max, latency, to_multiplier, cmd_len;
33422ce603ebSClaudio Takahasi 	int err;
3343de73115aSClaudio Takahasi 
3344de73115aSClaudio Takahasi 	if (!(hcon->link_mode & HCI_LM_MASTER))
3345de73115aSClaudio Takahasi 		return -EINVAL;
3346de73115aSClaudio Takahasi 
3347de73115aSClaudio Takahasi 	cmd_len = __le16_to_cpu(cmd->len);
3348de73115aSClaudio Takahasi 	if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
3349de73115aSClaudio Takahasi 		return -EPROTO;
3350de73115aSClaudio Takahasi 
3351de73115aSClaudio Takahasi 	req = (struct l2cap_conn_param_update_req *) data;
3352de73115aSClaudio Takahasi 	min		= __le16_to_cpu(req->min);
3353de73115aSClaudio Takahasi 	max		= __le16_to_cpu(req->max);
3354de73115aSClaudio Takahasi 	latency		= __le16_to_cpu(req->latency);
3355de73115aSClaudio Takahasi 	to_multiplier	= __le16_to_cpu(req->to_multiplier);
3356de73115aSClaudio Takahasi 
3357de73115aSClaudio Takahasi 	BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
3358de73115aSClaudio Takahasi 						min, max, latency, to_multiplier);
3359de73115aSClaudio Takahasi 
3360de73115aSClaudio Takahasi 	memset(&rsp, 0, sizeof(rsp));
33612ce603ebSClaudio Takahasi 
33622ce603ebSClaudio Takahasi 	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
33632ce603ebSClaudio Takahasi 	if (err)
3364de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
3365de73115aSClaudio Takahasi 	else
3366de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
3367de73115aSClaudio Takahasi 
3368de73115aSClaudio Takahasi 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
3369de73115aSClaudio Takahasi 							sizeof(rsp), &rsp);
3370de73115aSClaudio Takahasi 
33712ce603ebSClaudio Takahasi 	if (!err)
33722ce603ebSClaudio Takahasi 		hci_le_conn_update(hcon, min, max, latency, to_multiplier);
33732ce603ebSClaudio Takahasi 
3374de73115aSClaudio Takahasi 	return 0;
3375de73115aSClaudio Takahasi }
3376de73115aSClaudio Takahasi 
33773300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
33783300d9a9SClaudio Takahasi 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
33793300d9a9SClaudio Takahasi {
33803300d9a9SClaudio Takahasi 	int err = 0;
33813300d9a9SClaudio Takahasi 
33823300d9a9SClaudio Takahasi 	switch (cmd->code) {
33833300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
33843300d9a9SClaudio Takahasi 		l2cap_command_rej(conn, cmd, data);
33853300d9a9SClaudio Takahasi 		break;
33863300d9a9SClaudio Takahasi 
33873300d9a9SClaudio Takahasi 	case L2CAP_CONN_REQ:
33883300d9a9SClaudio Takahasi 		err = l2cap_connect_req(conn, cmd, data);
33893300d9a9SClaudio Takahasi 		break;
33903300d9a9SClaudio Takahasi 
33913300d9a9SClaudio Takahasi 	case L2CAP_CONN_RSP:
33923300d9a9SClaudio Takahasi 		err = l2cap_connect_rsp(conn, cmd, data);
33933300d9a9SClaudio Takahasi 		break;
33943300d9a9SClaudio Takahasi 
33953300d9a9SClaudio Takahasi 	case L2CAP_CONF_REQ:
33963300d9a9SClaudio Takahasi 		err = l2cap_config_req(conn, cmd, cmd_len, data);
33973300d9a9SClaudio Takahasi 		break;
33983300d9a9SClaudio Takahasi 
33993300d9a9SClaudio Takahasi 	case L2CAP_CONF_RSP:
34003300d9a9SClaudio Takahasi 		err = l2cap_config_rsp(conn, cmd, data);
34013300d9a9SClaudio Takahasi 		break;
34023300d9a9SClaudio Takahasi 
34033300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_REQ:
34043300d9a9SClaudio Takahasi 		err = l2cap_disconnect_req(conn, cmd, data);
34053300d9a9SClaudio Takahasi 		break;
34063300d9a9SClaudio Takahasi 
34073300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_RSP:
34083300d9a9SClaudio Takahasi 		err = l2cap_disconnect_rsp(conn, cmd, data);
34093300d9a9SClaudio Takahasi 		break;
34103300d9a9SClaudio Takahasi 
34113300d9a9SClaudio Takahasi 	case L2CAP_ECHO_REQ:
34123300d9a9SClaudio Takahasi 		l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
34133300d9a9SClaudio Takahasi 		break;
34143300d9a9SClaudio Takahasi 
34153300d9a9SClaudio Takahasi 	case L2CAP_ECHO_RSP:
34163300d9a9SClaudio Takahasi 		break;
34173300d9a9SClaudio Takahasi 
34183300d9a9SClaudio Takahasi 	case L2CAP_INFO_REQ:
34193300d9a9SClaudio Takahasi 		err = l2cap_information_req(conn, cmd, data);
34203300d9a9SClaudio Takahasi 		break;
34213300d9a9SClaudio Takahasi 
34223300d9a9SClaudio Takahasi 	case L2CAP_INFO_RSP:
34233300d9a9SClaudio Takahasi 		err = l2cap_information_rsp(conn, cmd, data);
34243300d9a9SClaudio Takahasi 		break;
34253300d9a9SClaudio Takahasi 
3426f94ff6ffSMat Martineau 	case L2CAP_CREATE_CHAN_REQ:
3427f94ff6ffSMat Martineau 		err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
3428f94ff6ffSMat Martineau 		break;
3429f94ff6ffSMat Martineau 
3430f94ff6ffSMat Martineau 	case L2CAP_CREATE_CHAN_RSP:
3431f94ff6ffSMat Martineau 		err = l2cap_create_channel_rsp(conn, cmd, data);
3432f94ff6ffSMat Martineau 		break;
3433f94ff6ffSMat Martineau 
34348d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_REQ:
34358d5a04a1SMat Martineau 		err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
34368d5a04a1SMat Martineau 		break;
34378d5a04a1SMat Martineau 
34388d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_RSP:
34398d5a04a1SMat Martineau 		err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
34408d5a04a1SMat Martineau 		break;
34418d5a04a1SMat Martineau 
34428d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_CFM:
34438d5a04a1SMat Martineau 		err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
34448d5a04a1SMat Martineau 		break;
34458d5a04a1SMat Martineau 
34468d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_CFM_RSP:
34478d5a04a1SMat Martineau 		err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
34488d5a04a1SMat Martineau 		break;
34498d5a04a1SMat Martineau 
34503300d9a9SClaudio Takahasi 	default:
34513300d9a9SClaudio Takahasi 		BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
34523300d9a9SClaudio Takahasi 		err = -EINVAL;
34533300d9a9SClaudio Takahasi 		break;
34543300d9a9SClaudio Takahasi 	}
34553300d9a9SClaudio Takahasi 
34563300d9a9SClaudio Takahasi 	return err;
34573300d9a9SClaudio Takahasi }
34583300d9a9SClaudio Takahasi 
34593300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
34603300d9a9SClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
34613300d9a9SClaudio Takahasi {
34623300d9a9SClaudio Takahasi 	switch (cmd->code) {
34633300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
34643300d9a9SClaudio Takahasi 		return 0;
34653300d9a9SClaudio Takahasi 
34663300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_REQ:
3467de73115aSClaudio Takahasi 		return l2cap_conn_param_update_req(conn, cmd, data);
34683300d9a9SClaudio Takahasi 
34693300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_RSP:
34703300d9a9SClaudio Takahasi 		return 0;
34713300d9a9SClaudio Takahasi 
34723300d9a9SClaudio Takahasi 	default:
34733300d9a9SClaudio Takahasi 		BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
34743300d9a9SClaudio Takahasi 		return -EINVAL;
34753300d9a9SClaudio Takahasi 	}
34763300d9a9SClaudio Takahasi }
34773300d9a9SClaudio Takahasi 
34783300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn,
34793300d9a9SClaudio Takahasi 							struct sk_buff *skb)
34800a708f8fSGustavo F. Padovan {
34810a708f8fSGustavo F. Padovan 	u8 *data = skb->data;
34820a708f8fSGustavo F. Padovan 	int len = skb->len;
34830a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr cmd;
34843300d9a9SClaudio Takahasi 	int err;
34850a708f8fSGustavo F. Padovan 
34860a708f8fSGustavo F. Padovan 	l2cap_raw_recv(conn, skb);
34870a708f8fSGustavo F. Padovan 
34880a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CMD_HDR_SIZE) {
34890a708f8fSGustavo F. Padovan 		u16 cmd_len;
34900a708f8fSGustavo F. Padovan 		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
34910a708f8fSGustavo F. Padovan 		data += L2CAP_CMD_HDR_SIZE;
34920a708f8fSGustavo F. Padovan 		len  -= L2CAP_CMD_HDR_SIZE;
34930a708f8fSGustavo F. Padovan 
34940a708f8fSGustavo F. Padovan 		cmd_len = le16_to_cpu(cmd.len);
34950a708f8fSGustavo F. Padovan 
34960a708f8fSGustavo F. Padovan 		BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
34970a708f8fSGustavo F. Padovan 
34980a708f8fSGustavo F. Padovan 		if (cmd_len > len || !cmd.ident) {
34990a708f8fSGustavo F. Padovan 			BT_DBG("corrupted command");
35000a708f8fSGustavo F. Padovan 			break;
35010a708f8fSGustavo F. Padovan 		}
35020a708f8fSGustavo F. Padovan 
35033300d9a9SClaudio Takahasi 		if (conn->hcon->type == LE_LINK)
35043300d9a9SClaudio Takahasi 			err = l2cap_le_sig_cmd(conn, &cmd, data);
35053300d9a9SClaudio Takahasi 		else
35063300d9a9SClaudio Takahasi 			err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
35070a708f8fSGustavo F. Padovan 
35080a708f8fSGustavo F. Padovan 		if (err) {
3509e2fd318eSIlia Kolomisnky 			struct l2cap_cmd_rej_unk rej;
35102c6d1a2eSGustavo F. Padovan 
35112c6d1a2eSGustavo F. Padovan 			BT_ERR("Wrong link type (%d)", err);
35120a708f8fSGustavo F. Padovan 
35130a708f8fSGustavo F. Padovan 			/* FIXME: Map err to a valid reason */
3514e2fd318eSIlia Kolomisnky 			rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
35150a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
35160a708f8fSGustavo F. Padovan 		}
35170a708f8fSGustavo F. Padovan 
35180a708f8fSGustavo F. Padovan 		data += cmd_len;
35190a708f8fSGustavo F. Padovan 		len  -= cmd_len;
35200a708f8fSGustavo F. Padovan 	}
35210a708f8fSGustavo F. Padovan 
35220a708f8fSGustavo F. Padovan 	kfree_skb(skb);
35230a708f8fSGustavo F. Padovan }
35240a708f8fSGustavo F. Padovan 
352547d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
35260a708f8fSGustavo F. Padovan {
35270a708f8fSGustavo F. Padovan 	u16 our_fcs, rcv_fcs;
3528e4ca6d98SAndrei Emeltchenko 	int hdr_size;
3529e4ca6d98SAndrei Emeltchenko 
3530e4ca6d98SAndrei Emeltchenko 	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3531e4ca6d98SAndrei Emeltchenko 		hdr_size = L2CAP_EXT_HDR_SIZE;
3532e4ca6d98SAndrei Emeltchenko 	else
3533e4ca6d98SAndrei Emeltchenko 		hdr_size = L2CAP_ENH_HDR_SIZE;
35340a708f8fSGustavo F. Padovan 
353547d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
353603a51213SAndrei Emeltchenko 		skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
35370a708f8fSGustavo F. Padovan 		rcv_fcs = get_unaligned_le16(skb->data + skb->len);
35380a708f8fSGustavo F. Padovan 		our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
35390a708f8fSGustavo F. Padovan 
35400a708f8fSGustavo F. Padovan 		if (our_fcs != rcv_fcs)
35410a708f8fSGustavo F. Padovan 			return -EBADMSG;
35420a708f8fSGustavo F. Padovan 	}
35430a708f8fSGustavo F. Padovan 	return 0;
35440a708f8fSGustavo F. Padovan }
35450a708f8fSGustavo F. Padovan 
3546525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
35470a708f8fSGustavo F. Padovan {
354888843ab0SAndrei Emeltchenko 	u32 control = 0;
35490a708f8fSGustavo F. Padovan 
35506a026610SGustavo F. Padovan 	chan->frames_sent = 0;
35510a708f8fSGustavo F. Padovan 
35520b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
35530a708f8fSGustavo F. Padovan 
3554e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
3555ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
3556525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
3557e2ab4353SGustavo F. Padovan 		set_bit(CONN_RNR_SENT, &chan->conn_state);
35580a708f8fSGustavo F. Padovan 	}
35590a708f8fSGustavo F. Padovan 
3560e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
3561525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
35620a708f8fSGustavo F. Padovan 
3563525cd185SGustavo F. Padovan 	l2cap_ertm_send(chan);
35640a708f8fSGustavo F. Padovan 
3565e2ab4353SGustavo F. Padovan 	if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
35666a026610SGustavo F. Padovan 			chan->frames_sent == 0) {
3567ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
3568525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
35690a708f8fSGustavo F. Padovan 	}
35700a708f8fSGustavo F. Padovan }
35710a708f8fSGustavo F. Padovan 
3572fb45de7dSAndrei Emeltchenko static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar)
35730a708f8fSGustavo F. Padovan {
35740a708f8fSGustavo F. Padovan 	struct sk_buff *next_skb;
35750a708f8fSGustavo F. Padovan 	int tx_seq_offset, next_tx_seq_offset;
35760a708f8fSGustavo F. Padovan 
35770a708f8fSGustavo F. Padovan 	bt_cb(skb)->tx_seq = tx_seq;
35780a708f8fSGustavo F. Padovan 	bt_cb(skb)->sar = sar;
35790a708f8fSGustavo F. Padovan 
3580f1c6775bSGustavo F. Padovan 	next_skb = skb_peek(&chan->srej_q);
35810a708f8fSGustavo F. Padovan 
3582836be934SAndrei Emeltchenko 	tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq);
35830a708f8fSGustavo F. Padovan 
3584039d9572SSzymon Janc 	while (next_skb) {
35850a708f8fSGustavo F. Padovan 		if (bt_cb(next_skb)->tx_seq == tx_seq)
35860a708f8fSGustavo F. Padovan 			return -EINVAL;
35870a708f8fSGustavo F. Padovan 
3588836be934SAndrei Emeltchenko 		next_tx_seq_offset = __seq_offset(chan,
3589836be934SAndrei Emeltchenko 				bt_cb(next_skb)->tx_seq, chan->buffer_seq);
35900a708f8fSGustavo F. Padovan 
35910a708f8fSGustavo F. Padovan 		if (next_tx_seq_offset > tx_seq_offset) {
3592f1c6775bSGustavo F. Padovan 			__skb_queue_before(&chan->srej_q, next_skb, skb);
35930a708f8fSGustavo F. Padovan 			return 0;
35940a708f8fSGustavo F. Padovan 		}
35950a708f8fSGustavo F. Padovan 
3596f1c6775bSGustavo F. Padovan 		if (skb_queue_is_last(&chan->srej_q, next_skb))
3597039d9572SSzymon Janc 			next_skb = NULL;
3598039d9572SSzymon Janc 		else
3599039d9572SSzymon Janc 			next_skb = skb_queue_next(&chan->srej_q, next_skb);
3600039d9572SSzymon Janc 	}
36010a708f8fSGustavo F. Padovan 
3602f1c6775bSGustavo F. Padovan 	__skb_queue_tail(&chan->srej_q, skb);
36030a708f8fSGustavo F. Padovan 
36040a708f8fSGustavo F. Padovan 	return 0;
36050a708f8fSGustavo F. Padovan }
36060a708f8fSGustavo F. Padovan 
360784084a31SMat Martineau static void append_skb_frag(struct sk_buff *skb,
360884084a31SMat Martineau 			struct sk_buff *new_frag, struct sk_buff **last_frag)
36090a708f8fSGustavo F. Padovan {
361084084a31SMat Martineau 	/* skb->len reflects data in skb as well as all fragments
361184084a31SMat Martineau 	 * skb->data_len reflects only data in fragments
361284084a31SMat Martineau 	 */
361384084a31SMat Martineau 	if (!skb_has_frag_list(skb))
361484084a31SMat Martineau 		skb_shinfo(skb)->frag_list = new_frag;
361584084a31SMat Martineau 
361684084a31SMat Martineau 	new_frag->next = NULL;
361784084a31SMat Martineau 
361884084a31SMat Martineau 	(*last_frag)->next = new_frag;
361984084a31SMat Martineau 	*last_frag = new_frag;
362084084a31SMat Martineau 
362184084a31SMat Martineau 	skb->len += new_frag->len;
362284084a31SMat Martineau 	skb->data_len += new_frag->len;
362384084a31SMat Martineau 	skb->truesize += new_frag->truesize;
362484084a31SMat Martineau }
362584084a31SMat Martineau 
362688843ab0SAndrei Emeltchenko static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u32 control)
362784084a31SMat Martineau {
362884084a31SMat Martineau 	int err = -EINVAL;
36290a708f8fSGustavo F. Padovan 
36307e0ef6eeSAndrei Emeltchenko 	switch (__get_ctrl_sar(chan, control)) {
36317e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_UNSEGMENTED:
363284084a31SMat Martineau 		if (chan->sdu)
363384084a31SMat Martineau 			break;
36340a708f8fSGustavo F. Padovan 
363584084a31SMat Martineau 		err = chan->ops->recv(chan->data, skb);
363684084a31SMat Martineau 		break;
36370a708f8fSGustavo F. Padovan 
36387e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_START:
363984084a31SMat Martineau 		if (chan->sdu)
364084084a31SMat Martineau 			break;
36410a708f8fSGustavo F. Padovan 
36426f61fd47SGustavo F. Padovan 		chan->sdu_len = get_unaligned_le16(skb->data);
364303a51213SAndrei Emeltchenko 		skb_pull(skb, L2CAP_SDULEN_SIZE);
36440a708f8fSGustavo F. Padovan 
364584084a31SMat Martineau 		if (chan->sdu_len > chan->imtu) {
364684084a31SMat Martineau 			err = -EMSGSIZE;
364784084a31SMat Martineau 			break;
364884084a31SMat Martineau 		}
36490a708f8fSGustavo F. Padovan 
365084084a31SMat Martineau 		if (skb->len >= chan->sdu_len)
365184084a31SMat Martineau 			break;
365284084a31SMat Martineau 
365384084a31SMat Martineau 		chan->sdu = skb;
365484084a31SMat Martineau 		chan->sdu_last_frag = skb;
365584084a31SMat Martineau 
365684084a31SMat Martineau 		skb = NULL;
365784084a31SMat Martineau 		err = 0;
36580a708f8fSGustavo F. Padovan 		break;
36590a708f8fSGustavo F. Padovan 
36607e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_CONTINUE:
36616f61fd47SGustavo F. Padovan 		if (!chan->sdu)
366284084a31SMat Martineau 			break;
36630a708f8fSGustavo F. Padovan 
366484084a31SMat Martineau 		append_skb_frag(chan->sdu, skb,
366584084a31SMat Martineau 				&chan->sdu_last_frag);
366684084a31SMat Martineau 		skb = NULL;
36670a708f8fSGustavo F. Padovan 
366884084a31SMat Martineau 		if (chan->sdu->len >= chan->sdu_len)
366984084a31SMat Martineau 			break;
36700a708f8fSGustavo F. Padovan 
367184084a31SMat Martineau 		err = 0;
36720a708f8fSGustavo F. Padovan 		break;
36730a708f8fSGustavo F. Padovan 
36747e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_END:
36756f61fd47SGustavo F. Padovan 		if (!chan->sdu)
367684084a31SMat Martineau 			break;
36770a708f8fSGustavo F. Padovan 
367884084a31SMat Martineau 		append_skb_frag(chan->sdu, skb,
367984084a31SMat Martineau 				&chan->sdu_last_frag);
368084084a31SMat Martineau 		skb = NULL;
36810a708f8fSGustavo F. Padovan 
368284084a31SMat Martineau 		if (chan->sdu->len != chan->sdu_len)
368384084a31SMat Martineau 			break;
36840a708f8fSGustavo F. Padovan 
368584084a31SMat Martineau 		err = chan->ops->recv(chan->data, chan->sdu);
36860a708f8fSGustavo F. Padovan 
368784084a31SMat Martineau 		if (!err) {
368884084a31SMat Martineau 			/* Reassembly complete */
368984084a31SMat Martineau 			chan->sdu = NULL;
369084084a31SMat Martineau 			chan->sdu_last_frag = NULL;
369184084a31SMat Martineau 			chan->sdu_len = 0;
36920a708f8fSGustavo F. Padovan 		}
36930a708f8fSGustavo F. Padovan 		break;
36940a708f8fSGustavo F. Padovan 	}
36950a708f8fSGustavo F. Padovan 
369684084a31SMat Martineau 	if (err) {
36970a708f8fSGustavo F. Padovan 		kfree_skb(skb);
36986f61fd47SGustavo F. Padovan 		kfree_skb(chan->sdu);
36996f61fd47SGustavo F. Padovan 		chan->sdu = NULL;
370084084a31SMat Martineau 		chan->sdu_last_frag = NULL;
370184084a31SMat Martineau 		chan->sdu_len = 0;
370284084a31SMat Martineau 	}
37030a708f8fSGustavo F. Padovan 
370484084a31SMat Martineau 	return err;
37050a708f8fSGustavo F. Padovan }
37060a708f8fSGustavo F. Padovan 
370726f880d2SMat Martineau static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan)
37080a708f8fSGustavo F. Padovan {
370926f880d2SMat Martineau 	BT_DBG("chan %p, Enter local busy", chan);
371026f880d2SMat Martineau 
371126f880d2SMat Martineau 	set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
371226f880d2SMat Martineau 
371377f918bcSSzymon Janc 	__set_ack_timer(chan);
37140a708f8fSGustavo F. Padovan }
37150a708f8fSGustavo F. Padovan 
371626f880d2SMat Martineau static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan)
371726f880d2SMat Martineau {
371888843ab0SAndrei Emeltchenko 	u32 control;
37190a708f8fSGustavo F. Padovan 
3720e2ab4353SGustavo F. Padovan 	if (!test_bit(CONN_RNR_SENT, &chan->conn_state))
37210a708f8fSGustavo F. Padovan 		goto done;
37220a708f8fSGustavo F. Padovan 
37230b209faeSAndrei Emeltchenko 	control = __set_reqseq(chan, chan->buffer_seq);
3724e3781735SAndrei Emeltchenko 	control |= __set_ctrl_poll(chan);
3725ab784b73SAndrei Emeltchenko 	control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
3726525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
37276a026610SGustavo F. Padovan 	chan->retry_count = 1;
37280a708f8fSGustavo F. Padovan 
37291a09bcb9SGustavo F. Padovan 	__clear_retrans_timer(chan);
37301a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
37310a708f8fSGustavo F. Padovan 
3732e2ab4353SGustavo F. Padovan 	set_bit(CONN_WAIT_F, &chan->conn_state);
37330a708f8fSGustavo F. Padovan 
37340a708f8fSGustavo F. Padovan done:
3735e2ab4353SGustavo F. Padovan 	clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
3736e2ab4353SGustavo F. Padovan 	clear_bit(CONN_RNR_SENT, &chan->conn_state);
37370a708f8fSGustavo F. Padovan 
373849208c9cSGustavo F. Padovan 	BT_DBG("chan %p, Exit local busy", chan);
37390a708f8fSGustavo F. Padovan }
37400a708f8fSGustavo F. Padovan 
3741e328140fSMat Martineau void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
37420a708f8fSGustavo F. Padovan {
3743e328140fSMat Martineau 	if (chan->mode == L2CAP_MODE_ERTM) {
3744e328140fSMat Martineau 		if (busy)
374526f880d2SMat Martineau 			l2cap_ertm_enter_local_busy(chan);
3746e328140fSMat Martineau 		else
3747e328140fSMat Martineau 			l2cap_ertm_exit_local_busy(chan);
37480a708f8fSGustavo F. Padovan 	}
37490a708f8fSGustavo F. Padovan }
37500a708f8fSGustavo F. Padovan 
3751fb45de7dSAndrei Emeltchenko static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq)
37520a708f8fSGustavo F. Padovan {
37530a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
375488843ab0SAndrei Emeltchenko 	u32 control;
37550a708f8fSGustavo F. Padovan 
3756e328140fSMat Martineau 	while ((skb = skb_peek(&chan->srej_q)) &&
3757e328140fSMat Martineau 			!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
3758e328140fSMat Martineau 		int err;
3759e328140fSMat Martineau 
37600a708f8fSGustavo F. Padovan 		if (bt_cb(skb)->tx_seq != tx_seq)
37610a708f8fSGustavo F. Padovan 			break;
37620a708f8fSGustavo F. Padovan 
3763f1c6775bSGustavo F. Padovan 		skb = skb_dequeue(&chan->srej_q);
37647e0ef6eeSAndrei Emeltchenko 		control = __set_ctrl_sar(chan, bt_cb(skb)->sar);
376584084a31SMat Martineau 		err = l2cap_reassemble_sdu(chan, skb, control);
3766e328140fSMat Martineau 
3767e328140fSMat Martineau 		if (err < 0) {
3768e328140fSMat Martineau 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3769e328140fSMat Martineau 			break;
3770e328140fSMat Martineau 		}
3771e328140fSMat Martineau 
3772836be934SAndrei Emeltchenko 		chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej);
3773836be934SAndrei Emeltchenko 		tx_seq = __next_seq(chan, tx_seq);
37740a708f8fSGustavo F. Padovan 	}
37750a708f8fSGustavo F. Padovan }
37760a708f8fSGustavo F. Padovan 
3777fb45de7dSAndrei Emeltchenko static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq)
37780a708f8fSGustavo F. Padovan {
37790a708f8fSGustavo F. Padovan 	struct srej_list *l, *tmp;
378088843ab0SAndrei Emeltchenko 	u32 control;
37810a708f8fSGustavo F. Padovan 
378239d5a3eeSGustavo F. Padovan 	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
37830a708f8fSGustavo F. Padovan 		if (l->tx_seq == tx_seq) {
37840a708f8fSGustavo F. Padovan 			list_del(&l->list);
37850a708f8fSGustavo F. Padovan 			kfree(l);
37860a708f8fSGustavo F. Padovan 			return;
37870a708f8fSGustavo F. Padovan 		}
3788ab784b73SAndrei Emeltchenko 		control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
37890b209faeSAndrei Emeltchenko 		control |= __set_reqseq(chan, l->tx_seq);
3790525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
37910a708f8fSGustavo F. Padovan 		list_del(&l->list);
379239d5a3eeSGustavo F. Padovan 		list_add_tail(&l->list, &chan->srej_l);
37930a708f8fSGustavo F. Padovan 	}
37940a708f8fSGustavo F. Padovan }
37950a708f8fSGustavo F. Padovan 
3796aef89f21SSzymon Janc static int l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq)
37970a708f8fSGustavo F. Padovan {
37980a708f8fSGustavo F. Padovan 	struct srej_list *new;
379988843ab0SAndrei Emeltchenko 	u32 control;
38000a708f8fSGustavo F. Padovan 
380142e5c802SGustavo F. Padovan 	while (tx_seq != chan->expected_tx_seq) {
3802ab784b73SAndrei Emeltchenko 		control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
38030b209faeSAndrei Emeltchenko 		control |= __set_reqseq(chan, chan->expected_tx_seq);
3804525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
38050a708f8fSGustavo F. Padovan 
38060a708f8fSGustavo F. Padovan 		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
3807aef89f21SSzymon Janc 		if (!new)
3808aef89f21SSzymon Janc 			return -ENOMEM;
3809aef89f21SSzymon Janc 
381042e5c802SGustavo F. Padovan 		new->tx_seq = chan->expected_tx_seq;
3811836be934SAndrei Emeltchenko 
3812836be934SAndrei Emeltchenko 		chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
3813836be934SAndrei Emeltchenko 
381439d5a3eeSGustavo F. Padovan 		list_add_tail(&new->list, &chan->srej_l);
38150a708f8fSGustavo F. Padovan 	}
3816836be934SAndrei Emeltchenko 
3817836be934SAndrei Emeltchenko 	chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
3818aef89f21SSzymon Janc 
3819aef89f21SSzymon Janc 	return 0;
38200a708f8fSGustavo F. Padovan }
38210a708f8fSGustavo F. Padovan 
382288843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb)
38230a708f8fSGustavo F. Padovan {
3824fb45de7dSAndrei Emeltchenko 	u16 tx_seq = __get_txseq(chan, rx_control);
38250b209faeSAndrei Emeltchenko 	u16 req_seq = __get_reqseq(chan, rx_control);
38267e0ef6eeSAndrei Emeltchenko 	u8 sar = __get_ctrl_sar(chan, rx_control);
38270a708f8fSGustavo F. Padovan 	int tx_seq_offset, expected_tx_seq_offset;
382847d1ec61SGustavo F. Padovan 	int num_to_ack = (chan->tx_win/6) + 1;
38290a708f8fSGustavo F. Padovan 	int err = 0;
38300a708f8fSGustavo F. Padovan 
383188843ab0SAndrei Emeltchenko 	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%8.8x", chan, skb->len,
3832525cd185SGustavo F. Padovan 							tx_seq, rx_control);
38330a708f8fSGustavo F. Padovan 
383403f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control) &&
3835e2ab4353SGustavo F. Padovan 			test_bit(CONN_WAIT_F, &chan->conn_state)) {
38361a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
38376a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
38381a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
3839e2ab4353SGustavo F. Padovan 		clear_bit(CONN_WAIT_F, &chan->conn_state);
38400a708f8fSGustavo F. Padovan 	}
38410a708f8fSGustavo F. Padovan 
384242e5c802SGustavo F. Padovan 	chan->expected_ack_seq = req_seq;
384342e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
38440a708f8fSGustavo F. Padovan 
3845836be934SAndrei Emeltchenko 	tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq);
38460a708f8fSGustavo F. Padovan 
38470a708f8fSGustavo F. Padovan 	/* invalid tx_seq */
384847d1ec61SGustavo F. Padovan 	if (tx_seq_offset >= chan->tx_win) {
38498c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38500a708f8fSGustavo F. Padovan 		goto drop;
38510a708f8fSGustavo F. Padovan 	}
38520a708f8fSGustavo F. Padovan 
385377f918bcSSzymon Janc 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
385477f918bcSSzymon Janc 		if (!test_bit(CONN_RNR_SENT, &chan->conn_state))
385577f918bcSSzymon Janc 			l2cap_send_ack(chan);
38560a708f8fSGustavo F. Padovan 		goto drop;
385777f918bcSSzymon Janc 	}
38580a708f8fSGustavo F. Padovan 
385902f1b641SMat Martineau 	if (tx_seq == chan->expected_tx_seq)
386002f1b641SMat Martineau 		goto expected;
386102f1b641SMat Martineau 
3862e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
38630a708f8fSGustavo F. Padovan 		struct srej_list *first;
38640a708f8fSGustavo F. Padovan 
386539d5a3eeSGustavo F. Padovan 		first = list_first_entry(&chan->srej_l,
38660a708f8fSGustavo F. Padovan 				struct srej_list, list);
38670a708f8fSGustavo F. Padovan 		if (tx_seq == first->tx_seq) {
386842e5c802SGustavo F. Padovan 			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3869525cd185SGustavo F. Padovan 			l2cap_check_srej_gap(chan, tx_seq);
38700a708f8fSGustavo F. Padovan 
38710a708f8fSGustavo F. Padovan 			list_del(&first->list);
38720a708f8fSGustavo F. Padovan 			kfree(first);
38730a708f8fSGustavo F. Padovan 
387439d5a3eeSGustavo F. Padovan 			if (list_empty(&chan->srej_l)) {
387542e5c802SGustavo F. Padovan 				chan->buffer_seq = chan->buffer_seq_srej;
3876e2ab4353SGustavo F. Padovan 				clear_bit(CONN_SREJ_SENT, &chan->conn_state);
3877525cd185SGustavo F. Padovan 				l2cap_send_ack(chan);
387849208c9cSGustavo F. Padovan 				BT_DBG("chan %p, Exit SREJ_SENT", chan);
38790a708f8fSGustavo F. Padovan 			}
38800a708f8fSGustavo F. Padovan 		} else {
38810a708f8fSGustavo F. Padovan 			struct srej_list *l;
38820a708f8fSGustavo F. Padovan 
38830a708f8fSGustavo F. Padovan 			/* duplicated tx_seq */
388442e5c802SGustavo F. Padovan 			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
38850a708f8fSGustavo F. Padovan 				goto drop;
38860a708f8fSGustavo F. Padovan 
388739d5a3eeSGustavo F. Padovan 			list_for_each_entry(l, &chan->srej_l, list) {
38880a708f8fSGustavo F. Padovan 				if (l->tx_seq == tx_seq) {
3889525cd185SGustavo F. Padovan 					l2cap_resend_srejframe(chan, tx_seq);
38900a708f8fSGustavo F. Padovan 					return 0;
38910a708f8fSGustavo F. Padovan 				}
38920a708f8fSGustavo F. Padovan 			}
3893aef89f21SSzymon Janc 
3894aef89f21SSzymon Janc 			err = l2cap_send_srejframe(chan, tx_seq);
3895aef89f21SSzymon Janc 			if (err < 0) {
3896aef89f21SSzymon Janc 				l2cap_send_disconn_req(chan->conn, chan, -err);
3897aef89f21SSzymon Janc 				return err;
3898aef89f21SSzymon Janc 			}
38990a708f8fSGustavo F. Padovan 		}
39000a708f8fSGustavo F. Padovan 	} else {
3901836be934SAndrei Emeltchenko 		expected_tx_seq_offset = __seq_offset(chan,
3902836be934SAndrei Emeltchenko 				chan->expected_tx_seq, chan->buffer_seq);
39030a708f8fSGustavo F. Padovan 
39040a708f8fSGustavo F. Padovan 		/* duplicated tx_seq */
39050a708f8fSGustavo F. Padovan 		if (tx_seq_offset < expected_tx_seq_offset)
39060a708f8fSGustavo F. Padovan 			goto drop;
39070a708f8fSGustavo F. Padovan 
3908e2ab4353SGustavo F. Padovan 		set_bit(CONN_SREJ_SENT, &chan->conn_state);
39090a708f8fSGustavo F. Padovan 
391049208c9cSGustavo F. Padovan 		BT_DBG("chan %p, Enter SREJ", chan);
39110a708f8fSGustavo F. Padovan 
391239d5a3eeSGustavo F. Padovan 		INIT_LIST_HEAD(&chan->srej_l);
391342e5c802SGustavo F. Padovan 		chan->buffer_seq_srej = chan->buffer_seq;
39140a708f8fSGustavo F. Padovan 
3915f1c6775bSGustavo F. Padovan 		__skb_queue_head_init(&chan->srej_q);
391642e5c802SGustavo F. Padovan 		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
39170a708f8fSGustavo F. Padovan 
39180ef3ef0fSSzymon Janc 		/* Set P-bit only if there are some I-frames to ack. */
39190ef3ef0fSSzymon Janc 		if (__clear_ack_timer(chan))
3920e2ab4353SGustavo F. Padovan 			set_bit(CONN_SEND_PBIT, &chan->conn_state);
39210a708f8fSGustavo F. Padovan 
3922aef89f21SSzymon Janc 		err = l2cap_send_srejframe(chan, tx_seq);
3923aef89f21SSzymon Janc 		if (err < 0) {
3924aef89f21SSzymon Janc 			l2cap_send_disconn_req(chan->conn, chan, -err);
3925aef89f21SSzymon Janc 			return err;
3926aef89f21SSzymon Janc 		}
39270a708f8fSGustavo F. Padovan 	}
39280a708f8fSGustavo F. Padovan 	return 0;
39290a708f8fSGustavo F. Padovan 
39300a708f8fSGustavo F. Padovan expected:
3931836be934SAndrei Emeltchenko 	chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
39320a708f8fSGustavo F. Padovan 
3933e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
39340a708f8fSGustavo F. Padovan 		bt_cb(skb)->tx_seq = tx_seq;
39350a708f8fSGustavo F. Padovan 		bt_cb(skb)->sar = sar;
3936f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->srej_q, skb);
39370a708f8fSGustavo F. Padovan 		return 0;
39380a708f8fSGustavo F. Padovan 	}
39390a708f8fSGustavo F. Padovan 
394084084a31SMat Martineau 	err = l2cap_reassemble_sdu(chan, skb, rx_control);
3941836be934SAndrei Emeltchenko 	chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
3942836be934SAndrei Emeltchenko 
3943e328140fSMat Martineau 	if (err < 0) {
3944e328140fSMat Martineau 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3945e328140fSMat Martineau 		return err;
3946e328140fSMat Martineau 	}
39470a708f8fSGustavo F. Padovan 
394803f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control)) {
3949e2ab4353SGustavo F. Padovan 		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
3950525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
39510a708f8fSGustavo F. Padovan 	}
39520a708f8fSGustavo F. Padovan 
39530a708f8fSGustavo F. Padovan 
39546a026610SGustavo F. Padovan 	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
39556a026610SGustavo F. Padovan 	if (chan->num_acked == num_to_ack - 1)
3956525cd185SGustavo F. Padovan 		l2cap_send_ack(chan);
39574d611e4dSGustavo F. Padovan 	else
39584d611e4dSGustavo F. Padovan 		__set_ack_timer(chan);
39590a708f8fSGustavo F. Padovan 
39600a708f8fSGustavo F. Padovan 	return 0;
39610a708f8fSGustavo F. Padovan 
39620a708f8fSGustavo F. Padovan drop:
39630a708f8fSGustavo F. Padovan 	kfree_skb(skb);
39640a708f8fSGustavo F. Padovan 	return 0;
39650a708f8fSGustavo F. Padovan }
39660a708f8fSGustavo F. Padovan 
396788843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u32 rx_control)
39680a708f8fSGustavo F. Padovan {
396988843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan,
39700b209faeSAndrei Emeltchenko 				__get_reqseq(chan, rx_control), rx_control);
39710a708f8fSGustavo F. Padovan 
39720b209faeSAndrei Emeltchenko 	chan->expected_ack_seq = __get_reqseq(chan, rx_control);
397342e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
39740a708f8fSGustavo F. Padovan 
3975e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control)) {
3976e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_FBIT, &chan->conn_state);
3977e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
3978e2ab4353SGustavo F. Padovan 			if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
39796a026610SGustavo F. Padovan 					(chan->unacked_frames > 0))
39801a09bcb9SGustavo F. Padovan 				__set_retrans_timer(chan);
39810a708f8fSGustavo F. Padovan 
3982e2ab4353SGustavo F. Padovan 			clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3983525cd185SGustavo F. Padovan 			l2cap_send_srejtail(chan);
39840a708f8fSGustavo F. Padovan 		} else {
3985525cd185SGustavo F. Padovan 			l2cap_send_i_or_rr_or_rnr(chan);
39860a708f8fSGustavo F. Padovan 		}
39870a708f8fSGustavo F. Padovan 
398803f6715dSAndrei Emeltchenko 	} else if (__is_ctrl_final(chan, rx_control)) {
3989e2ab4353SGustavo F. Padovan 		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
39900a708f8fSGustavo F. Padovan 
3991e2ab4353SGustavo F. Padovan 		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
3992525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
39930a708f8fSGustavo F. Padovan 
39940a708f8fSGustavo F. Padovan 	} else {
3995e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
39966a026610SGustavo F. Padovan 				(chan->unacked_frames > 0))
39971a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
39980a708f8fSGustavo F. Padovan 
3999e2ab4353SGustavo F. Padovan 		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
4000e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_SREJ_SENT, &chan->conn_state))
4001525cd185SGustavo F. Padovan 			l2cap_send_ack(chan);
40020a708f8fSGustavo F. Padovan 		else
4003525cd185SGustavo F. Padovan 			l2cap_ertm_send(chan);
40040a708f8fSGustavo F. Padovan 	}
40050a708f8fSGustavo F. Padovan }
40060a708f8fSGustavo F. Padovan 
400788843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u32 rx_control)
40080a708f8fSGustavo F. Padovan {
40090b209faeSAndrei Emeltchenko 	u16 tx_seq = __get_reqseq(chan, rx_control);
40100a708f8fSGustavo F. Padovan 
401188843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
40120a708f8fSGustavo F. Padovan 
4013e2ab4353SGustavo F. Padovan 	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
40140a708f8fSGustavo F. Padovan 
401542e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
401642e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
40170a708f8fSGustavo F. Padovan 
401803f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control)) {
4019e2ab4353SGustavo F. Padovan 		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
4020525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
40210a708f8fSGustavo F. Padovan 	} else {
4022525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
40230a708f8fSGustavo F. Padovan 
4024e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_WAIT_F, &chan->conn_state))
4025e2ab4353SGustavo F. Padovan 			set_bit(CONN_REJ_ACT, &chan->conn_state);
40260a708f8fSGustavo F. Padovan 	}
40270a708f8fSGustavo F. Padovan }
402888843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u32 rx_control)
40290a708f8fSGustavo F. Padovan {
40300b209faeSAndrei Emeltchenko 	u16 tx_seq = __get_reqseq(chan, rx_control);
40310a708f8fSGustavo F. Padovan 
403288843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
40330a708f8fSGustavo F. Padovan 
4034e2ab4353SGustavo F. Padovan 	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
40350a708f8fSGustavo F. Padovan 
4036e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control)) {
403742e5c802SGustavo F. Padovan 		chan->expected_ack_seq = tx_seq;
403842e5c802SGustavo F. Padovan 		l2cap_drop_acked_frames(chan);
40390a708f8fSGustavo F. Padovan 
4040e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_FBIT, &chan->conn_state);
4041525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
40420a708f8fSGustavo F. Padovan 
4043525cd185SGustavo F. Padovan 		l2cap_ertm_send(chan);
40440a708f8fSGustavo F. Padovan 
4045e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
40466a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
4047e2ab4353SGustavo F. Padovan 			set_bit(CONN_SREJ_ACT, &chan->conn_state);
40480a708f8fSGustavo F. Padovan 		}
404903f6715dSAndrei Emeltchenko 	} else if (__is_ctrl_final(chan, rx_control)) {
4050e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_SREJ_ACT, &chan->conn_state) &&
40516a026610SGustavo F. Padovan 				chan->srej_save_reqseq == tx_seq)
4052e2ab4353SGustavo F. Padovan 			clear_bit(CONN_SREJ_ACT, &chan->conn_state);
40530a708f8fSGustavo F. Padovan 		else
4054525cd185SGustavo F. Padovan 			l2cap_retransmit_one_frame(chan, tx_seq);
40550a708f8fSGustavo F. Padovan 	} else {
4056525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
4057e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
40586a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
4059e2ab4353SGustavo F. Padovan 			set_bit(CONN_SREJ_ACT, &chan->conn_state);
40600a708f8fSGustavo F. Padovan 		}
40610a708f8fSGustavo F. Padovan 	}
40620a708f8fSGustavo F. Padovan }
40630a708f8fSGustavo F. Padovan 
406488843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u32 rx_control)
40650a708f8fSGustavo F. Padovan {
40660b209faeSAndrei Emeltchenko 	u16 tx_seq = __get_reqseq(chan, rx_control);
40670a708f8fSGustavo F. Padovan 
406888843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
40690a708f8fSGustavo F. Padovan 
4070e2ab4353SGustavo F. Padovan 	set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
407142e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
407242e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
40730a708f8fSGustavo F. Padovan 
4074e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control))
4075e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_FBIT, &chan->conn_state);
40760a708f8fSGustavo F. Padovan 
4077e2ab4353SGustavo F. Padovan 	if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
40781a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
4079e3781735SAndrei Emeltchenko 		if (__is_ctrl_poll(chan, rx_control))
4080525cd185SGustavo F. Padovan 			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
40810a708f8fSGustavo F. Padovan 		return;
40820a708f8fSGustavo F. Padovan 	}
40830a708f8fSGustavo F. Padovan 
4084e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control)) {
4085525cd185SGustavo F. Padovan 		l2cap_send_srejtail(chan);
4086ab784b73SAndrei Emeltchenko 	} else {
4087ab784b73SAndrei Emeltchenko 		rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR);
4088ab784b73SAndrei Emeltchenko 		l2cap_send_sframe(chan, rx_control);
4089ab784b73SAndrei Emeltchenko 	}
40900a708f8fSGustavo F. Padovan }
40910a708f8fSGustavo F. Padovan 
409288843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb)
40930a708f8fSGustavo F. Padovan {
409488843ab0SAndrei Emeltchenko 	BT_DBG("chan %p rx_control 0x%8.8x len %d", chan, rx_control, skb->len);
40950a708f8fSGustavo F. Padovan 
409603f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control) &&
4097e2ab4353SGustavo F. Padovan 			test_bit(CONN_WAIT_F, &chan->conn_state)) {
40981a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
40996a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
41001a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
4101e2ab4353SGustavo F. Padovan 		clear_bit(CONN_WAIT_F, &chan->conn_state);
41020a708f8fSGustavo F. Padovan 	}
41030a708f8fSGustavo F. Padovan 
4104ab784b73SAndrei Emeltchenko 	switch (__get_ctrl_super(chan, rx_control)) {
4105ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_RR:
4106525cd185SGustavo F. Padovan 		l2cap_data_channel_rrframe(chan, rx_control);
41070a708f8fSGustavo F. Padovan 		break;
41080a708f8fSGustavo F. Padovan 
4109ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_REJ:
4110525cd185SGustavo F. Padovan 		l2cap_data_channel_rejframe(chan, rx_control);
41110a708f8fSGustavo F. Padovan 		break;
41120a708f8fSGustavo F. Padovan 
4113ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_SREJ:
4114525cd185SGustavo F. Padovan 		l2cap_data_channel_srejframe(chan, rx_control);
41150a708f8fSGustavo F. Padovan 		break;
41160a708f8fSGustavo F. Padovan 
4117ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_RNR:
4118525cd185SGustavo F. Padovan 		l2cap_data_channel_rnrframe(chan, rx_control);
41190a708f8fSGustavo F. Padovan 		break;
41200a708f8fSGustavo F. Padovan 	}
41210a708f8fSGustavo F. Padovan 
41220a708f8fSGustavo F. Padovan 	kfree_skb(skb);
41230a708f8fSGustavo F. Padovan 	return 0;
41240a708f8fSGustavo F. Padovan }
41250a708f8fSGustavo F. Padovan 
4126cad8f1d0SSzymon Janc static int l2cap_ertm_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
41270a708f8fSGustavo F. Padovan {
412888843ab0SAndrei Emeltchenko 	u32 control;
41290b209faeSAndrei Emeltchenko 	u16 req_seq;
41300a708f8fSGustavo F. Padovan 	int len, next_tx_seq_offset, req_seq_offset;
41310a708f8fSGustavo F. Padovan 
413288843ab0SAndrei Emeltchenko 	control = __get_control(chan, skb->data);
413388843ab0SAndrei Emeltchenko 	skb_pull(skb, __ctrl_size(chan));
41340a708f8fSGustavo F. Padovan 	len = skb->len;
41350a708f8fSGustavo F. Padovan 
41360a708f8fSGustavo F. Padovan 	/*
41370a708f8fSGustavo F. Padovan 	 * We can just drop the corrupted I-frame here.
41380a708f8fSGustavo F. Padovan 	 * Receiver will miss it and start proper recovery
41390a708f8fSGustavo F. Padovan 	 * procedures and ask retransmission.
41400a708f8fSGustavo F. Padovan 	 */
414147d1ec61SGustavo F. Padovan 	if (l2cap_check_fcs(chan, skb))
41420a708f8fSGustavo F. Padovan 		goto drop;
41430a708f8fSGustavo F. Padovan 
4144793c2f1cSAndrei Emeltchenko 	if (__is_sar_start(chan, control) && !__is_sframe(chan, control))
414503a51213SAndrei Emeltchenko 		len -= L2CAP_SDULEN_SIZE;
41460a708f8fSGustavo F. Padovan 
414747d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
414803a51213SAndrei Emeltchenko 		len -= L2CAP_FCS_SIZE;
41490a708f8fSGustavo F. Padovan 
415047d1ec61SGustavo F. Padovan 	if (len > chan->mps) {
41518c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
41520a708f8fSGustavo F. Padovan 		goto drop;
41530a708f8fSGustavo F. Padovan 	}
41540a708f8fSGustavo F. Padovan 
41550b209faeSAndrei Emeltchenko 	req_seq = __get_reqseq(chan, control);
41560a708f8fSGustavo F. Padovan 
4157836be934SAndrei Emeltchenko 	req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq);
4158836be934SAndrei Emeltchenko 
4159836be934SAndrei Emeltchenko 	next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq,
4160836be934SAndrei Emeltchenko 						chan->expected_ack_seq);
41610a708f8fSGustavo F. Padovan 
41620a708f8fSGustavo F. Padovan 	/* check for invalid req-seq */
41630a708f8fSGustavo F. Padovan 	if (req_seq_offset > next_tx_seq_offset) {
41648c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
41650a708f8fSGustavo F. Padovan 		goto drop;
41660a708f8fSGustavo F. Padovan 	}
41670a708f8fSGustavo F. Padovan 
4168793c2f1cSAndrei Emeltchenko 	if (!__is_sframe(chan, control)) {
41690a708f8fSGustavo F. Padovan 		if (len < 0) {
41708c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
41710a708f8fSGustavo F. Padovan 			goto drop;
41720a708f8fSGustavo F. Padovan 		}
41730a708f8fSGustavo F. Padovan 
4174525cd185SGustavo F. Padovan 		l2cap_data_channel_iframe(chan, control, skb);
41750a708f8fSGustavo F. Padovan 	} else {
41760a708f8fSGustavo F. Padovan 		if (len != 0) {
41770a708f8fSGustavo F. Padovan 			BT_ERR("%d", len);
41788c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
41790a708f8fSGustavo F. Padovan 			goto drop;
41800a708f8fSGustavo F. Padovan 		}
41810a708f8fSGustavo F. Padovan 
4182525cd185SGustavo F. Padovan 		l2cap_data_channel_sframe(chan, control, skb);
41830a708f8fSGustavo F. Padovan 	}
41840a708f8fSGustavo F. Padovan 
41850a708f8fSGustavo F. Padovan 	return 0;
41860a708f8fSGustavo F. Padovan 
41870a708f8fSGustavo F. Padovan drop:
41880a708f8fSGustavo F. Padovan 	kfree_skb(skb);
41890a708f8fSGustavo F. Padovan 	return 0;
41900a708f8fSGustavo F. Padovan }
41910a708f8fSGustavo F. Padovan 
41920a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
41930a708f8fSGustavo F. Padovan {
419448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
4195bf734843SDavid S. Miller 	struct sock *sk = NULL;
419688843ab0SAndrei Emeltchenko 	u32 control;
4197fb45de7dSAndrei Emeltchenko 	u16 tx_seq;
41980a708f8fSGustavo F. Padovan 	int len;
41990a708f8fSGustavo F. Padovan 
4200baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, cid);
420148454079SGustavo F. Padovan 	if (!chan) {
42020a708f8fSGustavo F. Padovan 		BT_DBG("unknown cid 0x%4.4x", cid);
42030a708f8fSGustavo F. Padovan 		goto drop;
42040a708f8fSGustavo F. Padovan 	}
42050a708f8fSGustavo F. Padovan 
420648454079SGustavo F. Padovan 	sk = chan->sk;
42070a708f8fSGustavo F. Padovan 
420849208c9cSGustavo F. Padovan 	BT_DBG("chan %p, len %d", chan, skb->len);
42090a708f8fSGustavo F. Padovan 
421089bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
42110a708f8fSGustavo F. Padovan 		goto drop;
42120a708f8fSGustavo F. Padovan 
42130c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
42140a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
42150a708f8fSGustavo F. Padovan 		/* If socket recv buffers overflows we drop data here
42160a708f8fSGustavo F. Padovan 		 * which is *bad* because L2CAP has to be reliable.
42170a708f8fSGustavo F. Padovan 		 * But we don't have any other choice. L2CAP doesn't
42180a708f8fSGustavo F. Padovan 		 * provide flow control mechanism. */
42190a708f8fSGustavo F. Padovan 
42200c1bc5c6SGustavo F. Padovan 		if (chan->imtu < skb->len)
42210a708f8fSGustavo F. Padovan 			goto drop;
42220a708f8fSGustavo F. Padovan 
422323070494SGustavo F. Padovan 		if (!chan->ops->recv(chan->data, skb))
42240a708f8fSGustavo F. Padovan 			goto done;
42250a708f8fSGustavo F. Padovan 		break;
42260a708f8fSGustavo F. Padovan 
42270a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
42285ef8cb9eSAndrei Emeltchenko 		l2cap_ertm_data_rcv(chan, skb);
42290a708f8fSGustavo F. Padovan 
42300a708f8fSGustavo F. Padovan 		goto done;
42310a708f8fSGustavo F. Padovan 
42320a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
423388843ab0SAndrei Emeltchenko 		control = __get_control(chan, skb->data);
423488843ab0SAndrei Emeltchenko 		skb_pull(skb, __ctrl_size(chan));
42350a708f8fSGustavo F. Padovan 		len = skb->len;
42360a708f8fSGustavo F. Padovan 
423747d1ec61SGustavo F. Padovan 		if (l2cap_check_fcs(chan, skb))
42380a708f8fSGustavo F. Padovan 			goto drop;
42390a708f8fSGustavo F. Padovan 
42407e0ef6eeSAndrei Emeltchenko 		if (__is_sar_start(chan, control))
424103a51213SAndrei Emeltchenko 			len -= L2CAP_SDULEN_SIZE;
42420a708f8fSGustavo F. Padovan 
424347d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16)
424403a51213SAndrei Emeltchenko 			len -= L2CAP_FCS_SIZE;
42450a708f8fSGustavo F. Padovan 
4246793c2f1cSAndrei Emeltchenko 		if (len > chan->mps || len < 0 || __is_sframe(chan, control))
42470a708f8fSGustavo F. Padovan 			goto drop;
42480a708f8fSGustavo F. Padovan 
4249fb45de7dSAndrei Emeltchenko 		tx_seq = __get_txseq(chan, control);
42500a708f8fSGustavo F. Padovan 
425184084a31SMat Martineau 		if (chan->expected_tx_seq != tx_seq) {
425284084a31SMat Martineau 			/* Frame(s) missing - must discard partial SDU */
425384084a31SMat Martineau 			kfree_skb(chan->sdu);
425484084a31SMat Martineau 			chan->sdu = NULL;
425584084a31SMat Martineau 			chan->sdu_last_frag = NULL;
425684084a31SMat Martineau 			chan->sdu_len = 0;
425784084a31SMat Martineau 
425884084a31SMat Martineau 			/* TODO: Notify userland of missing data */
425984084a31SMat Martineau 		}
426084084a31SMat Martineau 
4261836be934SAndrei Emeltchenko 		chan->expected_tx_seq = __next_seq(chan, tx_seq);
42620a708f8fSGustavo F. Padovan 
426384084a31SMat Martineau 		if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE)
426484084a31SMat Martineau 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
42650a708f8fSGustavo F. Padovan 
42660a708f8fSGustavo F. Padovan 		goto done;
42670a708f8fSGustavo F. Padovan 
42680a708f8fSGustavo F. Padovan 	default:
42690c1bc5c6SGustavo F. Padovan 		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
42700a708f8fSGustavo F. Padovan 		break;
42710a708f8fSGustavo F. Padovan 	}
42720a708f8fSGustavo F. Padovan 
42730a708f8fSGustavo F. Padovan drop:
42740a708f8fSGustavo F. Padovan 	kfree_skb(skb);
42750a708f8fSGustavo F. Padovan 
42760a708f8fSGustavo F. Padovan done:
42770a708f8fSGustavo F. Padovan 	if (sk)
4278aa2ac881SGustavo F. Padovan 		release_sock(sk);
42790a708f8fSGustavo F. Padovan 
42800a708f8fSGustavo F. Padovan 	return 0;
42810a708f8fSGustavo F. Padovan }
42820a708f8fSGustavo F. Padovan 
42830a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
42840a708f8fSGustavo F. Padovan {
42856dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
428623691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
42870a708f8fSGustavo F. Padovan 
428823691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_psm(0, psm, conn->src);
428923691d75SGustavo F. Padovan 	if (!chan)
42900a708f8fSGustavo F. Padovan 		goto drop;
42910a708f8fSGustavo F. Padovan 
429223691d75SGustavo F. Padovan 	sk = chan->sk;
429323691d75SGustavo F. Padovan 
4294aa2ac881SGustavo F. Padovan 	lock_sock(sk);
42950a708f8fSGustavo F. Padovan 
42960a708f8fSGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
42970a708f8fSGustavo F. Padovan 
429889bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
42990a708f8fSGustavo F. Padovan 		goto drop;
43000a708f8fSGustavo F. Padovan 
4301e13e21dcSVinicius Costa Gomes 	if (chan->imtu < skb->len)
43020a708f8fSGustavo F. Padovan 		goto drop;
43030a708f8fSGustavo F. Padovan 
430423070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
43050a708f8fSGustavo F. Padovan 		goto done;
43060a708f8fSGustavo F. Padovan 
43070a708f8fSGustavo F. Padovan drop:
43080a708f8fSGustavo F. Padovan 	kfree_skb(skb);
43090a708f8fSGustavo F. Padovan 
43100a708f8fSGustavo F. Padovan done:
43110a708f8fSGustavo F. Padovan 	if (sk)
4312aa2ac881SGustavo F. Padovan 		release_sock(sk);
43130a708f8fSGustavo F. Padovan 	return 0;
43140a708f8fSGustavo F. Padovan }
43150a708f8fSGustavo F. Padovan 
43169f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb)
43179f69bda6SGustavo F. Padovan {
43186dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
431923691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
43209f69bda6SGustavo F. Padovan 
432123691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_scid(0, cid, conn->src);
432223691d75SGustavo F. Padovan 	if (!chan)
43239f69bda6SGustavo F. Padovan 		goto drop;
43249f69bda6SGustavo F. Padovan 
432523691d75SGustavo F. Padovan 	sk = chan->sk;
432623691d75SGustavo F. Padovan 
4327aa2ac881SGustavo F. Padovan 	lock_sock(sk);
43289f69bda6SGustavo F. Padovan 
43299f69bda6SGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
43309f69bda6SGustavo F. Padovan 
433189bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
43329f69bda6SGustavo F. Padovan 		goto drop;
43339f69bda6SGustavo F. Padovan 
4334e13e21dcSVinicius Costa Gomes 	if (chan->imtu < skb->len)
43359f69bda6SGustavo F. Padovan 		goto drop;
43369f69bda6SGustavo F. Padovan 
433723070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
43389f69bda6SGustavo F. Padovan 		goto done;
43399f69bda6SGustavo F. Padovan 
43409f69bda6SGustavo F. Padovan drop:
43419f69bda6SGustavo F. Padovan 	kfree_skb(skb);
43429f69bda6SGustavo F. Padovan 
43439f69bda6SGustavo F. Padovan done:
43449f69bda6SGustavo F. Padovan 	if (sk)
4345aa2ac881SGustavo F. Padovan 		release_sock(sk);
43469f69bda6SGustavo F. Padovan 	return 0;
43479f69bda6SGustavo F. Padovan }
43489f69bda6SGustavo F. Padovan 
43490a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
43500a708f8fSGustavo F. Padovan {
43510a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh = (void *) skb->data;
43520a708f8fSGustavo F. Padovan 	u16 cid, len;
43530a708f8fSGustavo F. Padovan 	__le16 psm;
43540a708f8fSGustavo F. Padovan 
43550a708f8fSGustavo F. Padovan 	skb_pull(skb, L2CAP_HDR_SIZE);
43560a708f8fSGustavo F. Padovan 	cid = __le16_to_cpu(lh->cid);
43570a708f8fSGustavo F. Padovan 	len = __le16_to_cpu(lh->len);
43580a708f8fSGustavo F. Padovan 
43590a708f8fSGustavo F. Padovan 	if (len != skb->len) {
43600a708f8fSGustavo F. Padovan 		kfree_skb(skb);
43610a708f8fSGustavo F. Padovan 		return;
43620a708f8fSGustavo F. Padovan 	}
43630a708f8fSGustavo F. Padovan 
43640a708f8fSGustavo F. Padovan 	BT_DBG("len %d, cid 0x%4.4x", len, cid);
43650a708f8fSGustavo F. Padovan 
43660a708f8fSGustavo F. Padovan 	switch (cid) {
43673300d9a9SClaudio Takahasi 	case L2CAP_CID_LE_SIGNALING:
43680a708f8fSGustavo F. Padovan 	case L2CAP_CID_SIGNALING:
43690a708f8fSGustavo F. Padovan 		l2cap_sig_channel(conn, skb);
43700a708f8fSGustavo F. Padovan 		break;
43710a708f8fSGustavo F. Padovan 
43720a708f8fSGustavo F. Padovan 	case L2CAP_CID_CONN_LESS:
43730a708f8fSGustavo F. Padovan 		psm = get_unaligned_le16(skb->data);
43740a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
43750a708f8fSGustavo F. Padovan 		l2cap_conless_channel(conn, psm, skb);
43760a708f8fSGustavo F. Padovan 		break;
43770a708f8fSGustavo F. Padovan 
43789f69bda6SGustavo F. Padovan 	case L2CAP_CID_LE_DATA:
43799f69bda6SGustavo F. Padovan 		l2cap_att_channel(conn, cid, skb);
43809f69bda6SGustavo F. Padovan 		break;
43819f69bda6SGustavo F. Padovan 
4382b501d6a1SAnderson Briglia 	case L2CAP_CID_SMP:
4383b501d6a1SAnderson Briglia 		if (smp_sig_channel(conn, skb))
4384b501d6a1SAnderson Briglia 			l2cap_conn_del(conn->hcon, EACCES);
4385b501d6a1SAnderson Briglia 		break;
4386b501d6a1SAnderson Briglia 
43870a708f8fSGustavo F. Padovan 	default:
43880a708f8fSGustavo F. Padovan 		l2cap_data_channel(conn, cid, skb);
43890a708f8fSGustavo F. Padovan 		break;
43900a708f8fSGustavo F. Padovan 	}
43910a708f8fSGustavo F. Padovan }
43920a708f8fSGustavo F. Padovan 
43930a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */
43940a708f8fSGustavo F. Padovan 
4395686ebf28SUlisses Furquim int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
43960a708f8fSGustavo F. Padovan {
43970a708f8fSGustavo F. Padovan 	int exact = 0, lm1 = 0, lm2 = 0;
439823691d75SGustavo F. Padovan 	struct l2cap_chan *c;
43990a708f8fSGustavo F. Padovan 
44000a708f8fSGustavo F. Padovan 	BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
44010a708f8fSGustavo F. Padovan 
44020a708f8fSGustavo F. Padovan 	/* Find listening sockets and check their link_mode */
440323691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
440423691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
440523691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
44064343478fSGustavo F. Padovan 
440789bc500eSGustavo F. Padovan 		if (c->state != BT_LISTEN)
44080a708f8fSGustavo F. Padovan 			continue;
44090a708f8fSGustavo F. Padovan 
44100a708f8fSGustavo F. Padovan 		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
44110a708f8fSGustavo F. Padovan 			lm1 |= HCI_LM_ACCEPT;
441243bd0f32SAndrei Emeltchenko 			if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
44130a708f8fSGustavo F. Padovan 				lm1 |= HCI_LM_MASTER;
44140a708f8fSGustavo F. Padovan 			exact++;
44150a708f8fSGustavo F. Padovan 		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
44160a708f8fSGustavo F. Padovan 			lm2 |= HCI_LM_ACCEPT;
441743bd0f32SAndrei Emeltchenko 			if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
44180a708f8fSGustavo F. Padovan 				lm2 |= HCI_LM_MASTER;
44190a708f8fSGustavo F. Padovan 		}
44200a708f8fSGustavo F. Padovan 	}
442123691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
44220a708f8fSGustavo F. Padovan 
44230a708f8fSGustavo F. Padovan 	return exact ? lm1 : lm2;
44240a708f8fSGustavo F. Padovan }
44250a708f8fSGustavo F. Padovan 
4426686ebf28SUlisses Furquim int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
44270a708f8fSGustavo F. Padovan {
44280a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
44290a708f8fSGustavo F. Padovan 
44300a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
44310a708f8fSGustavo F. Padovan 
44320a708f8fSGustavo F. Padovan 	if (!status) {
44330a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, status);
44340a708f8fSGustavo F. Padovan 		if (conn)
44350a708f8fSGustavo F. Padovan 			l2cap_conn_ready(conn);
44360a708f8fSGustavo F. Padovan 	} else
4437e175072fSJoe Perches 		l2cap_conn_del(hcon, bt_to_errno(status));
44380a708f8fSGustavo F. Padovan 
44390a708f8fSGustavo F. Padovan 	return 0;
44400a708f8fSGustavo F. Padovan }
44410a708f8fSGustavo F. Padovan 
4442686ebf28SUlisses Furquim int l2cap_disconn_ind(struct hci_conn *hcon)
44430a708f8fSGustavo F. Padovan {
44440a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
44450a708f8fSGustavo F. Padovan 
44460a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p", hcon);
44470a708f8fSGustavo F. Padovan 
4448686ebf28SUlisses Furquim 	if (!conn)
44499f5a0d7bSAndrei Emeltchenko 		return HCI_ERROR_REMOTE_USER_TERM;
44500a708f8fSGustavo F. Padovan 	return conn->disc_reason;
44510a708f8fSGustavo F. Padovan }
44520a708f8fSGustavo F. Padovan 
4453686ebf28SUlisses Furquim int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
44540a708f8fSGustavo F. Padovan {
44550a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p reason %d", hcon, reason);
44560a708f8fSGustavo F. Padovan 
4457e175072fSJoe Perches 	l2cap_conn_del(hcon, bt_to_errno(reason));
44580a708f8fSGustavo F. Padovan 	return 0;
44590a708f8fSGustavo F. Padovan }
44600a708f8fSGustavo F. Padovan 
44614343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
44620a708f8fSGustavo F. Padovan {
4463715ec005SGustavo F. Padovan 	if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
44640a708f8fSGustavo F. Padovan 		return;
44650a708f8fSGustavo F. Padovan 
44660a708f8fSGustavo F. Padovan 	if (encrypt == 0x00) {
44674343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM) {
4468c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
4469b83ddfe2SAndrzej Kaczmarek 			__set_chan_timer(chan,
4470b83ddfe2SAndrzej Kaczmarek 					msecs_to_jiffies(L2CAP_ENC_TIMEOUT));
44714343478fSGustavo F. Padovan 		} else if (chan->sec_level == BT_SECURITY_HIGH)
44720f852724SGustavo F. Padovan 			l2cap_chan_close(chan, ECONNREFUSED);
44730a708f8fSGustavo F. Padovan 	} else {
44744343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM)
4475c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
44760a708f8fSGustavo F. Padovan 	}
44770a708f8fSGustavo F. Padovan }
44780a708f8fSGustavo F. Padovan 
4479686ebf28SUlisses Furquim int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
44800a708f8fSGustavo F. Padovan {
44810a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
448248454079SGustavo F. Padovan 	struct l2cap_chan *chan;
44830a708f8fSGustavo F. Padovan 
44840a708f8fSGustavo F. Padovan 	if (!conn)
44850a708f8fSGustavo F. Padovan 		return 0;
44860a708f8fSGustavo F. Padovan 
44870a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
44880a708f8fSGustavo F. Padovan 
4489160dc6acSVinicius Costa Gomes 	if (hcon->type == LE_LINK) {
4490160dc6acSVinicius Costa Gomes 		smp_distribute_keys(conn, 0);
449117cd3f37SUlisses Furquim 		cancel_delayed_work(&conn->security_timer);
4492160dc6acSVinicius Costa Gomes 	}
4493160dc6acSVinicius Costa Gomes 
44943d57dc68SGustavo F. Padovan 	rcu_read_lock();
44950a708f8fSGustavo F. Padovan 
44963d57dc68SGustavo F. Padovan 	list_for_each_entry_rcu(chan, &conn->chan_l, list) {
449748454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
4498baa7e1faSGustavo F. Padovan 
44990a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
45000a708f8fSGustavo F. Padovan 
4501f1cb9af5SVinicius Costa Gomes 		BT_DBG("chan->scid %d", chan->scid);
4502f1cb9af5SVinicius Costa Gomes 
4503f1cb9af5SVinicius Costa Gomes 		if (chan->scid == L2CAP_CID_LE_DATA) {
4504f1cb9af5SVinicius Costa Gomes 			if (!status && encrypt) {
4505f1cb9af5SVinicius Costa Gomes 				chan->sec_level = hcon->sec_level;
4506cf4cd009SAndrei Emeltchenko 				l2cap_chan_ready(chan);
4507f1cb9af5SVinicius Costa Gomes 			}
4508f1cb9af5SVinicius Costa Gomes 
4509f1cb9af5SVinicius Costa Gomes 			bh_unlock_sock(sk);
4510f1cb9af5SVinicius Costa Gomes 			continue;
4511f1cb9af5SVinicius Costa Gomes 		}
4512f1cb9af5SVinicius Costa Gomes 
4513c1360a1cSGustavo F. Padovan 		if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) {
45140a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
45150a708f8fSGustavo F. Padovan 			continue;
45160a708f8fSGustavo F. Padovan 		}
45170a708f8fSGustavo F. Padovan 
451889bc500eSGustavo F. Padovan 		if (!status && (chan->state == BT_CONNECTED ||
451989bc500eSGustavo F. Padovan 						chan->state == BT_CONFIG)) {
45204343478fSGustavo F. Padovan 			l2cap_check_encryption(chan, encrypt);
45210a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
45220a708f8fSGustavo F. Padovan 			continue;
45230a708f8fSGustavo F. Padovan 		}
45240a708f8fSGustavo F. Padovan 
452589bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
45260a708f8fSGustavo F. Padovan 			if (!status) {
45270a708f8fSGustavo F. Padovan 				struct l2cap_conn_req req;
4528fe4128e0SGustavo F. Padovan 				req.scid = cpu_to_le16(chan->scid);
4529fe4128e0SGustavo F. Padovan 				req.psm  = chan->psm;
45300a708f8fSGustavo F. Padovan 
4531fc7f8a7eSGustavo F. Padovan 				chan->ident = l2cap_get_ident(conn);
4532c1360a1cSGustavo F. Padovan 				set_bit(CONF_CONNECT_PEND, &chan->conf_state);
45330a708f8fSGustavo F. Padovan 
4534fc7f8a7eSGustavo F. Padovan 				l2cap_send_cmd(conn, chan->ident,
45350a708f8fSGustavo F. Padovan 					L2CAP_CONN_REQ, sizeof(req), &req);
45360a708f8fSGustavo F. Padovan 			} else {
4537c9b66675SGustavo F. Padovan 				__clear_chan_timer(chan);
4538b83ddfe2SAndrzej Kaczmarek 				__set_chan_timer(chan,
4539b83ddfe2SAndrzej Kaczmarek 					msecs_to_jiffies(L2CAP_DISC_TIMEOUT));
45400a708f8fSGustavo F. Padovan 			}
454189bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
45420a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
4543df3c3931SJohan Hedberg 			__u16 res, stat;
45440a708f8fSGustavo F. Padovan 
45450a708f8fSGustavo F. Padovan 			if (!status) {
4546df3c3931SJohan Hedberg 				if (bt_sk(sk)->defer_setup) {
4547df3c3931SJohan Hedberg 					struct sock *parent = bt_sk(sk)->parent;
4548df3c3931SJohan Hedberg 					res = L2CAP_CR_PEND;
4549df3c3931SJohan Hedberg 					stat = L2CAP_CS_AUTHOR_PEND;
455005e9a2f6SIlia Kolomisnky 					if (parent)
4551df3c3931SJohan Hedberg 						parent->sk_data_ready(parent, 0);
4552df3c3931SJohan Hedberg 				} else {
455389bc500eSGustavo F. Padovan 					l2cap_state_change(chan, BT_CONFIG);
4554df3c3931SJohan Hedberg 					res = L2CAP_CR_SUCCESS;
4555df3c3931SJohan Hedberg 					stat = L2CAP_CS_NO_INFO;
4556df3c3931SJohan Hedberg 				}
45570a708f8fSGustavo F. Padovan 			} else {
455889bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_DISCONN);
4559b83ddfe2SAndrzej Kaczmarek 				__set_chan_timer(chan,
4560b83ddfe2SAndrzej Kaczmarek 					msecs_to_jiffies(L2CAP_DISC_TIMEOUT));
4561df3c3931SJohan Hedberg 				res = L2CAP_CR_SEC_BLOCK;
4562df3c3931SJohan Hedberg 				stat = L2CAP_CS_NO_INFO;
45630a708f8fSGustavo F. Padovan 			}
45640a708f8fSGustavo F. Padovan 
4565fe4128e0SGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4566fe4128e0SGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
4567df3c3931SJohan Hedberg 			rsp.result = cpu_to_le16(res);
4568df3c3931SJohan Hedberg 			rsp.status = cpu_to_le16(stat);
4569fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4570fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
45710a708f8fSGustavo F. Padovan 		}
45720a708f8fSGustavo F. Padovan 
45730a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
45740a708f8fSGustavo F. Padovan 	}
45750a708f8fSGustavo F. Padovan 
45763d57dc68SGustavo F. Padovan 	rcu_read_unlock();
45770a708f8fSGustavo F. Padovan 
45780a708f8fSGustavo F. Padovan 	return 0;
45790a708f8fSGustavo F. Padovan }
45800a708f8fSGustavo F. Padovan 
4581686ebf28SUlisses Furquim int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
45820a708f8fSGustavo F. Padovan {
45830a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
45840a708f8fSGustavo F. Padovan 
45850a708f8fSGustavo F. Padovan 	if (!conn)
45860a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, 0);
45870a708f8fSGustavo F. Padovan 
45880a708f8fSGustavo F. Padovan 	if (!conn)
45890a708f8fSGustavo F. Padovan 		goto drop;
45900a708f8fSGustavo F. Padovan 
45910a708f8fSGustavo F. Padovan 	BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
45920a708f8fSGustavo F. Padovan 
45930a708f8fSGustavo F. Padovan 	if (!(flags & ACL_CONT)) {
45940a708f8fSGustavo F. Padovan 		struct l2cap_hdr *hdr;
459548454079SGustavo F. Padovan 		struct l2cap_chan *chan;
45960a708f8fSGustavo F. Padovan 		u16 cid;
45970a708f8fSGustavo F. Padovan 		int len;
45980a708f8fSGustavo F. Padovan 
45990a708f8fSGustavo F. Padovan 		if (conn->rx_len) {
46000a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected start frame (len %d)", skb->len);
46010a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
46020a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
46030a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
46040a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46050a708f8fSGustavo F. Padovan 		}
46060a708f8fSGustavo F. Padovan 
46070a708f8fSGustavo F. Padovan 		/* Start fragment always begin with Basic L2CAP header */
46080a708f8fSGustavo F. Padovan 		if (skb->len < L2CAP_HDR_SIZE) {
46090a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too short (len %d)", skb->len);
46100a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46110a708f8fSGustavo F. Padovan 			goto drop;
46120a708f8fSGustavo F. Padovan 		}
46130a708f8fSGustavo F. Padovan 
46140a708f8fSGustavo F. Padovan 		hdr = (struct l2cap_hdr *) skb->data;
46150a708f8fSGustavo F. Padovan 		len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
46160a708f8fSGustavo F. Padovan 		cid = __le16_to_cpu(hdr->cid);
46170a708f8fSGustavo F. Padovan 
46180a708f8fSGustavo F. Padovan 		if (len == skb->len) {
46190a708f8fSGustavo F. Padovan 			/* Complete frame received */
46200a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, skb);
46210a708f8fSGustavo F. Padovan 			return 0;
46220a708f8fSGustavo F. Padovan 		}
46230a708f8fSGustavo F. Padovan 
46240a708f8fSGustavo F. Padovan 		BT_DBG("Start: total len %d, frag len %d", len, skb->len);
46250a708f8fSGustavo F. Padovan 
46260a708f8fSGustavo F. Padovan 		if (skb->len > len) {
46270a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too long (len %d, expected len %d)",
46280a708f8fSGustavo F. Padovan 				skb->len, len);
46290a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46300a708f8fSGustavo F. Padovan 			goto drop;
46310a708f8fSGustavo F. Padovan 		}
46320a708f8fSGustavo F. Padovan 
4633baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, cid);
46340a708f8fSGustavo F. Padovan 
463548454079SGustavo F. Padovan 		if (chan && chan->sk) {
463648454079SGustavo F. Padovan 			struct sock *sk = chan->sk;
463748454079SGustavo F. Padovan 
46380c1bc5c6SGustavo F. Padovan 			if (chan->imtu < len - L2CAP_HDR_SIZE) {
463948454079SGustavo F. Padovan 				BT_ERR("Frame exceeding recv MTU (len %d, "
464048454079SGustavo F. Padovan 							"MTU %d)", len,
46410c1bc5c6SGustavo F. Padovan 							chan->imtu);
4642aa2ac881SGustavo F. Padovan 				release_sock(sk);
46430a708f8fSGustavo F. Padovan 				l2cap_conn_unreliable(conn, ECOMM);
46440a708f8fSGustavo F. Padovan 				goto drop;
46450a708f8fSGustavo F. Padovan 			}
4646aa2ac881SGustavo F. Padovan 			release_sock(sk);
464748454079SGustavo F. Padovan 		}
46480a708f8fSGustavo F. Padovan 
46490a708f8fSGustavo F. Padovan 		/* Allocate skb for the complete frame (with header) */
46500a708f8fSGustavo F. Padovan 		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
46510a708f8fSGustavo F. Padovan 		if (!conn->rx_skb)
46520a708f8fSGustavo F. Padovan 			goto drop;
46530a708f8fSGustavo F. Padovan 
46540a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
46550a708f8fSGustavo F. Padovan 								skb->len);
46560a708f8fSGustavo F. Padovan 		conn->rx_len = len - skb->len;
46570a708f8fSGustavo F. Padovan 	} else {
46580a708f8fSGustavo F. Padovan 		BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
46590a708f8fSGustavo F. Padovan 
46600a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
46610a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected continuation frame (len %d)", skb->len);
46620a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46630a708f8fSGustavo F. Padovan 			goto drop;
46640a708f8fSGustavo F. Padovan 		}
46650a708f8fSGustavo F. Padovan 
46660a708f8fSGustavo F. Padovan 		if (skb->len > conn->rx_len) {
46670a708f8fSGustavo F. Padovan 			BT_ERR("Fragment is too long (len %d, expected %d)",
46680a708f8fSGustavo F. Padovan 					skb->len, conn->rx_len);
46690a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
46700a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
46710a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
46720a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46730a708f8fSGustavo F. Padovan 			goto drop;
46740a708f8fSGustavo F. Padovan 		}
46750a708f8fSGustavo F. Padovan 
46760a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
46770a708f8fSGustavo F. Padovan 								skb->len);
46780a708f8fSGustavo F. Padovan 		conn->rx_len -= skb->len;
46790a708f8fSGustavo F. Padovan 
46800a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
46810a708f8fSGustavo F. Padovan 			/* Complete frame received */
46820a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, conn->rx_skb);
46830a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
46840a708f8fSGustavo F. Padovan 		}
46850a708f8fSGustavo F. Padovan 	}
46860a708f8fSGustavo F. Padovan 
46870a708f8fSGustavo F. Padovan drop:
46880a708f8fSGustavo F. Padovan 	kfree_skb(skb);
46890a708f8fSGustavo F. Padovan 	return 0;
46900a708f8fSGustavo F. Padovan }
46910a708f8fSGustavo F. Padovan 
46920a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p)
46930a708f8fSGustavo F. Padovan {
469423691d75SGustavo F. Padovan 	struct l2cap_chan *c;
46950a708f8fSGustavo F. Padovan 
4696333055f2SGustavo F. Padovan 	read_lock(&chan_list_lock);
46970a708f8fSGustavo F. Padovan 
469823691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
469923691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
47000a708f8fSGustavo F. Padovan 
4701903d343eSGustavo F. Padovan 		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
47020a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->src),
47030a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->dst),
470489bc500eSGustavo F. Padovan 					c->state, __le16_to_cpu(c->psm),
470523691d75SGustavo F. Padovan 					c->scid, c->dcid, c->imtu, c->omtu,
470623691d75SGustavo F. Padovan 					c->sec_level, c->mode);
47070a708f8fSGustavo F. Padovan 	}
47080a708f8fSGustavo F. Padovan 
4709333055f2SGustavo F. Padovan 	read_unlock(&chan_list_lock);
47100a708f8fSGustavo F. Padovan 
47110a708f8fSGustavo F. Padovan 	return 0;
47120a708f8fSGustavo F. Padovan }
47130a708f8fSGustavo F. Padovan 
47140a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file)
47150a708f8fSGustavo F. Padovan {
47160a708f8fSGustavo F. Padovan 	return single_open(file, l2cap_debugfs_show, inode->i_private);
47170a708f8fSGustavo F. Padovan }
47180a708f8fSGustavo F. Padovan 
47190a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = {
47200a708f8fSGustavo F. Padovan 	.open		= l2cap_debugfs_open,
47210a708f8fSGustavo F. Padovan 	.read		= seq_read,
47220a708f8fSGustavo F. Padovan 	.llseek		= seq_lseek,
47230a708f8fSGustavo F. Padovan 	.release	= single_release,
47240a708f8fSGustavo F. Padovan };
47250a708f8fSGustavo F. Padovan 
47260a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs;
47270a708f8fSGustavo F. Padovan 
472864274518SGustavo F. Padovan int __init l2cap_init(void)
47290a708f8fSGustavo F. Padovan {
47300a708f8fSGustavo F. Padovan 	int err;
47310a708f8fSGustavo F. Padovan 
4732bb58f747SGustavo F. Padovan 	err = l2cap_init_sockets();
47330a708f8fSGustavo F. Padovan 	if (err < 0)
47340a708f8fSGustavo F. Padovan 		return err;
47350a708f8fSGustavo F. Padovan 
47360a708f8fSGustavo F. Padovan 	if (bt_debugfs) {
47370a708f8fSGustavo F. Padovan 		l2cap_debugfs = debugfs_create_file("l2cap", 0444,
47380a708f8fSGustavo F. Padovan 					bt_debugfs, NULL, &l2cap_debugfs_fops);
47390a708f8fSGustavo F. Padovan 		if (!l2cap_debugfs)
47400a708f8fSGustavo F. Padovan 			BT_ERR("Failed to create L2CAP debug file");
47410a708f8fSGustavo F. Padovan 	}
47420a708f8fSGustavo F. Padovan 
47430a708f8fSGustavo F. Padovan 	return 0;
47440a708f8fSGustavo F. Padovan }
47450a708f8fSGustavo F. Padovan 
474664274518SGustavo F. Padovan void l2cap_exit(void)
47470a708f8fSGustavo F. Padovan {
47480a708f8fSGustavo F. Padovan 	debugfs_remove(l2cap_debugfs);
4749bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
47500a708f8fSGustavo F. Padovan }
47510a708f8fSGustavo F. Padovan 
47520a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644);
47530a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
4754