xref: /openbmc/linux/net/bluetooth/l2cap_core.c (revision 09fd0de5)
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.
60a708f8fSGustavo F. Padovan 
70a708f8fSGustavo F. Padovan    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
80a708f8fSGustavo F. Padovan 
90a708f8fSGustavo F. Padovan    This program is free software; you can redistribute it and/or modify
100a708f8fSGustavo F. Padovan    it under the terms of the GNU General Public License version 2 as
110a708f8fSGustavo F. Padovan    published by the Free Software Foundation;
120a708f8fSGustavo F. Padovan 
130a708f8fSGustavo F. Padovan    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
140a708f8fSGustavo F. Padovan    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150a708f8fSGustavo F. Padovan    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
160a708f8fSGustavo F. Padovan    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
170a708f8fSGustavo F. Padovan    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
180a708f8fSGustavo F. Padovan    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
190a708f8fSGustavo F. Padovan    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
200a708f8fSGustavo F. Padovan    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
210a708f8fSGustavo F. Padovan 
220a708f8fSGustavo F. Padovan    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
230a708f8fSGustavo F. Padovan    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
240a708f8fSGustavo F. Padovan    SOFTWARE IS DISCLAIMED.
250a708f8fSGustavo F. Padovan */
260a708f8fSGustavo F. Padovan 
27bb58f747SGustavo F. Padovan /* Bluetooth L2CAP core. */
280a708f8fSGustavo F. Padovan 
290a708f8fSGustavo F. Padovan #include <linux/module.h>
300a708f8fSGustavo F. Padovan 
310a708f8fSGustavo F. Padovan #include <linux/types.h>
320a708f8fSGustavo F. Padovan #include <linux/capability.h>
330a708f8fSGustavo F. Padovan #include <linux/errno.h>
340a708f8fSGustavo F. Padovan #include <linux/kernel.h>
350a708f8fSGustavo F. Padovan #include <linux/sched.h>
360a708f8fSGustavo F. Padovan #include <linux/slab.h>
370a708f8fSGustavo F. Padovan #include <linux/poll.h>
380a708f8fSGustavo F. Padovan #include <linux/fcntl.h>
390a708f8fSGustavo F. Padovan #include <linux/init.h>
400a708f8fSGustavo F. Padovan #include <linux/interrupt.h>
410a708f8fSGustavo F. Padovan #include <linux/socket.h>
420a708f8fSGustavo F. Padovan #include <linux/skbuff.h>
430a708f8fSGustavo F. Padovan #include <linux/list.h>
440a708f8fSGustavo F. Padovan #include <linux/device.h>
450a708f8fSGustavo F. Padovan #include <linux/debugfs.h>
460a708f8fSGustavo F. Padovan #include <linux/seq_file.h>
470a708f8fSGustavo F. Padovan #include <linux/uaccess.h>
480a708f8fSGustavo F. Padovan #include <linux/crc16.h>
490a708f8fSGustavo F. Padovan #include <net/sock.h>
500a708f8fSGustavo F. Padovan 
510a708f8fSGustavo F. Padovan #include <asm/system.h>
520a708f8fSGustavo F. Padovan #include <asm/unaligned.h>
530a708f8fSGustavo F. Padovan 
540a708f8fSGustavo F. Padovan #include <net/bluetooth/bluetooth.h>
550a708f8fSGustavo F. Padovan #include <net/bluetooth/hci_core.h>
560a708f8fSGustavo F. Padovan #include <net/bluetooth/l2cap.h>
57b501d6a1SAnderson Briglia #include <net/bluetooth/smp.h>
580a708f8fSGustavo F. Padovan 
59bb58f747SGustavo F. Padovan int disable_ertm;
600a708f8fSGustavo F. Padovan 
610a708f8fSGustavo F. Padovan static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN;
6250a147cdSMat Martineau static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, };
630a708f8fSGustavo F. Padovan 
64b5ad8b7fSJohannes Berg static LIST_HEAD(chan_list);
65b5ad8b7fSJohannes Berg static DEFINE_RWLOCK(chan_list_lock);
660a708f8fSGustavo F. Padovan 
670a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
680a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data);
694519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
704519de9aSGustavo F. Padovan 								void *data);
71710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
724519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn,
734519de9aSGustavo F. Padovan 				struct l2cap_chan *chan, int err);
740a708f8fSGustavo F. Padovan 
750a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb);
760a708f8fSGustavo F. Padovan 
770a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */
7871ba0e56SGustavo F. Padovan 
7971ba0e56SGustavo F. Padovan static inline void chan_hold(struct l2cap_chan *c)
8071ba0e56SGustavo F. Padovan {
8171ba0e56SGustavo F. Padovan 	atomic_inc(&c->refcnt);
8271ba0e56SGustavo F. Padovan }
8371ba0e56SGustavo F. Padovan 
8471ba0e56SGustavo F. Padovan static inline void chan_put(struct l2cap_chan *c)
8571ba0e56SGustavo F. Padovan {
8671ba0e56SGustavo F. Padovan 	if (atomic_dec_and_test(&c->refcnt))
8771ba0e56SGustavo F. Padovan 		kfree(c);
8871ba0e56SGustavo F. Padovan }
8971ba0e56SGustavo F. Padovan 
90baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
910a708f8fSGustavo F. Padovan {
9248454079SGustavo F. Padovan 	struct l2cap_chan *c;
93baa7e1faSGustavo F. Padovan 
94baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
95fe4128e0SGustavo F. Padovan 		if (c->dcid == cid)
9648454079SGustavo F. Padovan 			return c;
970a708f8fSGustavo F. Padovan 	}
98baa7e1faSGustavo F. Padovan 	return NULL;
99baa7e1faSGustavo F. Padovan }
1000a708f8fSGustavo F. Padovan 
101baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
1020a708f8fSGustavo F. Padovan {
10348454079SGustavo F. Padovan 	struct l2cap_chan *c;
104baa7e1faSGustavo F. Padovan 
105baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
106fe4128e0SGustavo F. Padovan 		if (c->scid == cid)
10748454079SGustavo F. Padovan 			return c;
1080a708f8fSGustavo F. Padovan 	}
109baa7e1faSGustavo F. Padovan 	return NULL;
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 	read_lock(&conn->chan_lock);
119baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_scid(conn, cid);
12048454079SGustavo F. Padovan 	if (c)
12148454079SGustavo F. Padovan 		bh_lock_sock(c->sk);
122baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
12348454079SGustavo F. Padovan 	return c;
1240a708f8fSGustavo F. Padovan }
1250a708f8fSGustavo F. Padovan 
126baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1270a708f8fSGustavo F. Padovan {
12848454079SGustavo F. Padovan 	struct l2cap_chan *c;
129baa7e1faSGustavo F. Padovan 
130baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
131fc7f8a7eSGustavo F. Padovan 		if (c->ident == ident)
13248454079SGustavo F. Padovan 			return c;
1330a708f8fSGustavo F. Padovan 	}
134baa7e1faSGustavo F. Padovan 	return NULL;
135baa7e1faSGustavo F. Padovan }
1360a708f8fSGustavo F. Padovan 
137baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1380a708f8fSGustavo F. Padovan {
13948454079SGustavo F. Padovan 	struct l2cap_chan *c;
140baa7e1faSGustavo F. Padovan 
141baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
142baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_ident(conn, ident);
14348454079SGustavo F. Padovan 	if (c)
14448454079SGustavo F. Padovan 		bh_lock_sock(c->sk);
145baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
14648454079SGustavo F. Padovan 	return c;
1470a708f8fSGustavo F. Padovan }
1480a708f8fSGustavo F. Padovan 
14923691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
1509e4425ffSGustavo F. Padovan {
15123691d75SGustavo F. Padovan 	struct l2cap_chan *c;
1529e4425ffSGustavo F. Padovan 
15323691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
15423691d75SGustavo F. Padovan 		if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src))
15523691d75SGustavo F. Padovan 			return c;
1569e4425ffSGustavo F. Padovan 	}
157250938cbSSzymon Janc 	return NULL;
158250938cbSSzymon Janc }
1599e4425ffSGustavo F. Padovan 
1609e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
1619e4425ffSGustavo F. Padovan {
16273b2ec18SGustavo F. Padovan 	int err;
16373b2ec18SGustavo F. Padovan 
16423691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
1659e4425ffSGustavo F. Padovan 
16623691d75SGustavo F. Padovan 	if (psm && __l2cap_global_chan_by_addr(psm, src)) {
16773b2ec18SGustavo F. Padovan 		err = -EADDRINUSE;
16873b2ec18SGustavo F. Padovan 		goto done;
1699e4425ffSGustavo F. Padovan 	}
1709e4425ffSGustavo F. Padovan 
17173b2ec18SGustavo F. Padovan 	if (psm) {
1729e4425ffSGustavo F. Padovan 		chan->psm = psm;
1739e4425ffSGustavo F. Padovan 		chan->sport = psm;
17473b2ec18SGustavo F. Padovan 		err = 0;
17573b2ec18SGustavo F. Padovan 	} else {
17673b2ec18SGustavo F. Padovan 		u16 p;
1779e4425ffSGustavo F. Padovan 
17873b2ec18SGustavo F. Padovan 		err = -EINVAL;
17973b2ec18SGustavo F. Padovan 		for (p = 0x1001; p < 0x1100; p += 2)
18023691d75SGustavo F. Padovan 			if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
18173b2ec18SGustavo F. Padovan 				chan->psm   = cpu_to_le16(p);
18273b2ec18SGustavo F. Padovan 				chan->sport = cpu_to_le16(p);
18373b2ec18SGustavo F. Padovan 				err = 0;
18473b2ec18SGustavo F. Padovan 				break;
18573b2ec18SGustavo F. Padovan 			}
18673b2ec18SGustavo F. Padovan 	}
18773b2ec18SGustavo F. Padovan 
18873b2ec18SGustavo F. Padovan done:
18923691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
19073b2ec18SGustavo F. Padovan 	return err;
1919e4425ffSGustavo F. Padovan }
1929e4425ffSGustavo F. Padovan 
1939e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid)
1949e4425ffSGustavo F. Padovan {
19523691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
1969e4425ffSGustavo F. Padovan 
1979e4425ffSGustavo F. Padovan 	chan->scid = scid;
1989e4425ffSGustavo F. Padovan 
19923691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
2009e4425ffSGustavo F. Padovan 
2019e4425ffSGustavo F. Padovan 	return 0;
2029e4425ffSGustavo F. Padovan }
2039e4425ffSGustavo F. Padovan 
204baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
2050a708f8fSGustavo F. Padovan {
2060a708f8fSGustavo F. Padovan 	u16 cid = L2CAP_CID_DYN_START;
2070a708f8fSGustavo F. Padovan 
2080a708f8fSGustavo F. Padovan 	for (; cid < L2CAP_CID_DYN_END; cid++) {
209baa7e1faSGustavo F. Padovan 		if (!__l2cap_get_chan_by_scid(conn, cid))
2100a708f8fSGustavo F. Padovan 			return cid;
2110a708f8fSGustavo F. Padovan 	}
2120a708f8fSGustavo F. Padovan 
2130a708f8fSGustavo F. Padovan 	return 0;
2140a708f8fSGustavo F. Padovan }
2150a708f8fSGustavo F. Padovan 
216c9b66675SGustavo F. Padovan static void l2cap_set_timer(struct l2cap_chan *chan, struct timer_list *timer, long timeout)
217ab07801dSGustavo F. Padovan {
218457f4850SAndrei Emeltchenko 	BT_DBG("chan %p state %d timeout %ld", chan, chan->state, timeout);
21989bc500eSGustavo F. Padovan 
220942ecc9cSMat Martineau 	if (!mod_timer(timer, jiffies + msecs_to_jiffies(timeout)))
22171ba0e56SGustavo F. Padovan 		chan_hold(chan);
222ab07801dSGustavo F. Padovan }
223ab07801dSGustavo F. Padovan 
224c9b66675SGustavo F. Padovan static void l2cap_clear_timer(struct l2cap_chan *chan, struct timer_list *timer)
225ab07801dSGustavo F. Padovan {
22689bc500eSGustavo F. Padovan 	BT_DBG("chan %p state %d", chan, chan->state);
227ab07801dSGustavo F. Padovan 
228c9b66675SGustavo F. Padovan 	if (timer_pending(timer) && del_timer(timer))
22971ba0e56SGustavo F. Padovan 		chan_put(chan);
230ab07801dSGustavo F. Padovan }
231ab07801dSGustavo F. Padovan 
232badaaa00SGustavo F. Padovan static char *state_to_string(int state)
233badaaa00SGustavo F. Padovan {
234badaaa00SGustavo F. Padovan 	switch(state) {
235badaaa00SGustavo F. Padovan 	case BT_CONNECTED:
236badaaa00SGustavo F. Padovan 		return "BT_CONNECTED";
237badaaa00SGustavo F. Padovan 	case BT_OPEN:
238badaaa00SGustavo F. Padovan 		return "BT_OPEN";
239badaaa00SGustavo F. Padovan 	case BT_BOUND:
240badaaa00SGustavo F. Padovan 		return "BT_BOUND";
241badaaa00SGustavo F. Padovan 	case BT_LISTEN:
242badaaa00SGustavo F. Padovan 		return "BT_LISTEN";
243badaaa00SGustavo F. Padovan 	case BT_CONNECT:
244badaaa00SGustavo F. Padovan 		return "BT_CONNECT";
245badaaa00SGustavo F. Padovan 	case BT_CONNECT2:
246badaaa00SGustavo F. Padovan 		return "BT_CONNECT2";
247badaaa00SGustavo F. Padovan 	case BT_CONFIG:
248badaaa00SGustavo F. Padovan 		return "BT_CONFIG";
249badaaa00SGustavo F. Padovan 	case BT_DISCONN:
250badaaa00SGustavo F. Padovan 		return "BT_DISCONN";
251badaaa00SGustavo F. Padovan 	case BT_CLOSED:
252badaaa00SGustavo F. Padovan 		return "BT_CLOSED";
253badaaa00SGustavo F. Padovan 	}
254badaaa00SGustavo F. Padovan 
255badaaa00SGustavo F. Padovan 	return "invalid state";
256badaaa00SGustavo F. Padovan }
257badaaa00SGustavo F. Padovan 
25889bc500eSGustavo F. Padovan static void l2cap_state_change(struct l2cap_chan *chan, int state)
25989bc500eSGustavo F. Padovan {
260badaaa00SGustavo F. Padovan 	BT_DBG("%p %s -> %s", chan, state_to_string(chan->state),
261badaaa00SGustavo F. Padovan 						state_to_string(state));
262badaaa00SGustavo F. Padovan 
26389bc500eSGustavo F. Padovan 	chan->state = state;
26489bc500eSGustavo F. Padovan 	chan->ops->state_change(chan->data, state);
26589bc500eSGustavo F. Padovan }
26689bc500eSGustavo F. Padovan 
267ab07801dSGustavo F. Padovan static void l2cap_chan_timeout(unsigned long arg)
268ab07801dSGustavo F. Padovan {
269ab07801dSGustavo F. Padovan 	struct l2cap_chan *chan = (struct l2cap_chan *) arg;
270ab07801dSGustavo F. Padovan 	struct sock *sk = chan->sk;
271ab07801dSGustavo F. Padovan 	int reason;
272ab07801dSGustavo F. Padovan 
27389bc500eSGustavo F. Padovan 	BT_DBG("chan %p state %d", chan, chan->state);
274ab07801dSGustavo F. Padovan 
275ab07801dSGustavo F. Padovan 	bh_lock_sock(sk);
276ab07801dSGustavo F. Padovan 
277ab07801dSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
278ab07801dSGustavo F. Padovan 		/* sk is owned by user. Try again later */
279f3f668b0SAndrzej Kaczmarek 		__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
280ab07801dSGustavo F. Padovan 		bh_unlock_sock(sk);
28171ba0e56SGustavo F. Padovan 		chan_put(chan);
282ab07801dSGustavo F. Padovan 		return;
283ab07801dSGustavo F. Padovan 	}
284ab07801dSGustavo F. Padovan 
28589bc500eSGustavo F. Padovan 	if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
286ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
28789bc500eSGustavo F. Padovan 	else if (chan->state == BT_CONNECT &&
288ab07801dSGustavo F. Padovan 					chan->sec_level != BT_SECURITY_SDP)
289ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
290ab07801dSGustavo F. Padovan 	else
291ab07801dSGustavo F. Padovan 		reason = ETIMEDOUT;
292ab07801dSGustavo F. Padovan 
2930f852724SGustavo F. Padovan 	l2cap_chan_close(chan, reason);
294ab07801dSGustavo F. Padovan 
295ab07801dSGustavo F. Padovan 	bh_unlock_sock(sk);
296ab07801dSGustavo F. Padovan 
297ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
29871ba0e56SGustavo F. Padovan 	chan_put(chan);
299ab07801dSGustavo F. Padovan }
300ab07801dSGustavo F. Padovan 
30123691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk)
3020a708f8fSGustavo F. Padovan {
30348454079SGustavo F. Padovan 	struct l2cap_chan *chan;
3040a708f8fSGustavo F. Padovan 
30548454079SGustavo F. Padovan 	chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
30648454079SGustavo F. Padovan 	if (!chan)
30748454079SGustavo F. Padovan 		return NULL;
3080a708f8fSGustavo F. Padovan 
30948454079SGustavo F. Padovan 	chan->sk = sk;
31048454079SGustavo F. Padovan 
31123691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
31223691d75SGustavo F. Padovan 	list_add(&chan->global_l, &chan_list);
31323691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
31423691d75SGustavo F. Padovan 
315ab07801dSGustavo F. Padovan 	setup_timer(&chan->chan_timer, l2cap_chan_timeout, (unsigned long) chan);
316ab07801dSGustavo F. Padovan 
31789bc500eSGustavo F. Padovan 	chan->state = BT_OPEN;
31889bc500eSGustavo F. Padovan 
31971ba0e56SGustavo F. Padovan 	atomic_set(&chan->refcnt, 1);
32071ba0e56SGustavo F. Padovan 
321abc545b8SSzymon Janc 	BT_DBG("sk %p chan %p", sk, chan);
322abc545b8SSzymon Janc 
32348454079SGustavo F. Padovan 	return chan;
3240a708f8fSGustavo F. Padovan }
3250a708f8fSGustavo F. Padovan 
32623691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan)
3276ff5abbfSGustavo F. Padovan {
32823691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
32923691d75SGustavo F. Padovan 	list_del(&chan->global_l);
33023691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
33123691d75SGustavo F. Padovan 
33271ba0e56SGustavo F. Padovan 	chan_put(chan);
3336ff5abbfSGustavo F. Padovan }
3346ff5abbfSGustavo F. Padovan 
33548454079SGustavo F. Padovan static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
3360a708f8fSGustavo F. Padovan {
3370a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
338fe4128e0SGustavo F. Padovan 			chan->psm, chan->dcid);
3390a708f8fSGustavo F. Padovan 
3409f5a0d7bSAndrei Emeltchenko 	conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
3410a708f8fSGustavo F. Padovan 
3428c1d787bSGustavo F. Padovan 	chan->conn = conn;
3430a708f8fSGustavo F. Padovan 
344715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
345b62f328bSVille Tervo 		if (conn->hcon->type == LE_LINK) {
346b62f328bSVille Tervo 			/* LE connection */
3470c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_LE_DEFAULT_MTU;
348fe4128e0SGustavo F. Padovan 			chan->scid = L2CAP_CID_LE_DATA;
349fe4128e0SGustavo F. Padovan 			chan->dcid = L2CAP_CID_LE_DATA;
350b62f328bSVille Tervo 		} else {
3510a708f8fSGustavo F. Padovan 			/* Alloc CID for connection-oriented socket */
352fe4128e0SGustavo F. Padovan 			chan->scid = l2cap_alloc_cid(conn);
3530c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_DEFAULT_MTU;
354b62f328bSVille Tervo 		}
355715ec005SGustavo F. Padovan 	} else if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
3560a708f8fSGustavo F. Padovan 		/* Connectionless socket */
357fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_CONN_LESS;
358fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_CONN_LESS;
3590c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3600a708f8fSGustavo F. Padovan 	} else {
3610a708f8fSGustavo F. Padovan 		/* Raw socket can send/recv signalling messages only */
362fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_SIGNALING;
363fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_SIGNALING;
3640c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3650a708f8fSGustavo F. Padovan 	}
3660a708f8fSGustavo F. Padovan 
3678f7975b1SAndrei Emeltchenko 	chan->local_id		= L2CAP_BESTEFFORT_ID;
3688f7975b1SAndrei Emeltchenko 	chan->local_stype	= L2CAP_SERV_BESTEFFORT;
3698f7975b1SAndrei Emeltchenko 	chan->local_msdu	= L2CAP_DEFAULT_MAX_SDU_SIZE;
3708f7975b1SAndrei Emeltchenko 	chan->local_sdu_itime	= L2CAP_DEFAULT_SDU_ITIME;
3718f7975b1SAndrei Emeltchenko 	chan->local_acc_lat	= L2CAP_DEFAULT_ACC_LAT;
3728f7975b1SAndrei Emeltchenko 	chan->local_flush_to	= L2CAP_DEFAULT_FLUSH_TO;
3738f7975b1SAndrei Emeltchenko 
37471ba0e56SGustavo F. Padovan 	chan_hold(chan);
375baa7e1faSGustavo F. Padovan 
376baa7e1faSGustavo F. Padovan 	list_add(&chan->list, &conn->chan_l);
3770a708f8fSGustavo F. Padovan }
3780a708f8fSGustavo F. Padovan 
3790a708f8fSGustavo F. Padovan /* Delete channel.
3800a708f8fSGustavo F. Padovan  * Must be called on the locked socket. */
3814519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err)
3820a708f8fSGustavo F. Padovan {
38348454079SGustavo F. Padovan 	struct sock *sk = chan->sk;
3848c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
3850a708f8fSGustavo F. Padovan 	struct sock *parent = bt_sk(sk)->parent;
3860a708f8fSGustavo F. Padovan 
387c9b66675SGustavo F. Padovan 	__clear_chan_timer(chan);
3880a708f8fSGustavo F. Padovan 
38949208c9cSGustavo F. Padovan 	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
3900a708f8fSGustavo F. Padovan 
3910a708f8fSGustavo F. Padovan 	if (conn) {
392baa7e1faSGustavo F. Padovan 		/* Delete from channel list */
393baa7e1faSGustavo F. Padovan 		write_lock_bh(&conn->chan_lock);
394baa7e1faSGustavo F. Padovan 		list_del(&chan->list);
395baa7e1faSGustavo F. Padovan 		write_unlock_bh(&conn->chan_lock);
39671ba0e56SGustavo F. Padovan 		chan_put(chan);
397baa7e1faSGustavo F. Padovan 
3988c1d787bSGustavo F. Padovan 		chan->conn = NULL;
3990a708f8fSGustavo F. Padovan 		hci_conn_put(conn->hcon);
4000a708f8fSGustavo F. Padovan 	}
4010a708f8fSGustavo F. Padovan 
40289bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CLOSED);
4030a708f8fSGustavo F. Padovan 	sock_set_flag(sk, SOCK_ZAPPED);
4040a708f8fSGustavo F. Padovan 
4050a708f8fSGustavo F. Padovan 	if (err)
4060a708f8fSGustavo F. Padovan 		sk->sk_err = err;
4070a708f8fSGustavo F. Padovan 
4080a708f8fSGustavo F. Padovan 	if (parent) {
4090a708f8fSGustavo F. Padovan 		bt_accept_unlink(sk);
4100a708f8fSGustavo F. Padovan 		parent->sk_data_ready(parent, 0);
4110a708f8fSGustavo F. Padovan 	} else
4120a708f8fSGustavo F. Padovan 		sk->sk_state_change(sk);
4130a708f8fSGustavo F. Padovan 
414c1360a1cSGustavo F. Padovan 	if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) &&
415c1360a1cSGustavo F. Padovan 			test_bit(CONF_INPUT_DONE, &chan->conf_state)))
4166ff5abbfSGustavo F. Padovan 		return;
4172ead70b8SGustavo F. Padovan 
41858d35f87SGustavo F. Padovan 	skb_queue_purge(&chan->tx_q);
4190a708f8fSGustavo F. Padovan 
4200c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
4210a708f8fSGustavo F. Padovan 		struct srej_list *l, *tmp;
4220a708f8fSGustavo F. Padovan 
4231a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
4241a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
4251a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
4260a708f8fSGustavo F. Padovan 
427f1c6775bSGustavo F. Padovan 		skb_queue_purge(&chan->srej_q);
4280a708f8fSGustavo F. Padovan 
42939d5a3eeSGustavo F. Padovan 		list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
4300a708f8fSGustavo F. Padovan 			list_del(&l->list);
4310a708f8fSGustavo F. Padovan 			kfree(l);
4320a708f8fSGustavo F. Padovan 		}
4330a708f8fSGustavo F. Padovan 	}
4340a708f8fSGustavo F. Padovan }
4350a708f8fSGustavo F. Padovan 
4364519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent)
4374519de9aSGustavo F. Padovan {
4384519de9aSGustavo F. Padovan 	struct sock *sk;
4394519de9aSGustavo F. Padovan 
4404519de9aSGustavo F. Padovan 	BT_DBG("parent %p", parent);
4414519de9aSGustavo F. Padovan 
4424519de9aSGustavo F. Padovan 	/* Close not yet accepted channels */
4430f852724SGustavo F. Padovan 	while ((sk = bt_accept_dequeue(parent, NULL))) {
444ba3bd0eeSGustavo F. Padovan 		struct l2cap_chan *chan = l2cap_pi(sk)->chan;
445c9b66675SGustavo F. Padovan 		__clear_chan_timer(chan);
4460f852724SGustavo F. Padovan 		lock_sock(sk);
447ba3bd0eeSGustavo F. Padovan 		l2cap_chan_close(chan, ECONNRESET);
4480f852724SGustavo F. Padovan 		release_sock(sk);
449ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
4500f852724SGustavo F. Padovan 	}
4514519de9aSGustavo F. Padovan }
4524519de9aSGustavo F. Padovan 
4530f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason)
4544519de9aSGustavo F. Padovan {
4554519de9aSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
4564519de9aSGustavo F. Padovan 	struct sock *sk = chan->sk;
4574519de9aSGustavo F. Padovan 
45889bc500eSGustavo F. Padovan 	BT_DBG("chan %p state %d socket %p", chan, chan->state, sk->sk_socket);
4594519de9aSGustavo F. Padovan 
46089bc500eSGustavo F. Padovan 	switch (chan->state) {
4614519de9aSGustavo F. Padovan 	case BT_LISTEN:
4624519de9aSGustavo F. Padovan 		l2cap_chan_cleanup_listen(sk);
46389bc500eSGustavo F. Padovan 
46489bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CLOSED);
46589bc500eSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
4664519de9aSGustavo F. Padovan 		break;
4674519de9aSGustavo F. Padovan 
4684519de9aSGustavo F. Padovan 	case BT_CONNECTED:
4694519de9aSGustavo F. Padovan 	case BT_CONFIG:
470715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4714519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
472c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
473c9b66675SGustavo F. Padovan 			__set_chan_timer(chan, sk->sk_sndtimeo);
4744519de9aSGustavo F. Padovan 			l2cap_send_disconn_req(conn, chan, reason);
4754519de9aSGustavo F. Padovan 		} else
4764519de9aSGustavo F. Padovan 			l2cap_chan_del(chan, reason);
4774519de9aSGustavo F. Padovan 		break;
4784519de9aSGustavo F. Padovan 
4794519de9aSGustavo F. Padovan 	case BT_CONNECT2:
480715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4814519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
4824519de9aSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
4834519de9aSGustavo F. Padovan 			__u16 result;
4844519de9aSGustavo F. Padovan 
4854519de9aSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup)
4864519de9aSGustavo F. Padovan 				result = L2CAP_CR_SEC_BLOCK;
4874519de9aSGustavo F. Padovan 			else
4884519de9aSGustavo F. Padovan 				result = L2CAP_CR_BAD_PSM;
48989bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_DISCONN);
4904519de9aSGustavo F. Padovan 
4914519de9aSGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4924519de9aSGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
4934519de9aSGustavo F. Padovan 			rsp.result = cpu_to_le16(result);
4944519de9aSGustavo F. Padovan 			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4954519de9aSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4964519de9aSGustavo F. Padovan 							sizeof(rsp), &rsp);
4974519de9aSGustavo F. Padovan 		}
4984519de9aSGustavo F. Padovan 
4994519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
5004519de9aSGustavo F. Padovan 		break;
5014519de9aSGustavo F. Padovan 
5024519de9aSGustavo F. Padovan 	case BT_CONNECT:
5034519de9aSGustavo F. Padovan 	case BT_DISCONN:
5044519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
5054519de9aSGustavo F. Padovan 		break;
5064519de9aSGustavo F. Padovan 
5074519de9aSGustavo F. Padovan 	default:
5084519de9aSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
5094519de9aSGustavo F. Padovan 		break;
5104519de9aSGustavo F. Padovan 	}
5114519de9aSGustavo F. Padovan }
5124519de9aSGustavo F. Padovan 
5134343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
5140a708f8fSGustavo F. Padovan {
515715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_RAW) {
5164343478fSGustavo F. Padovan 		switch (chan->sec_level) {
5170a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
5180a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING_MITM;
5190a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
5200a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING;
5210a708f8fSGustavo F. Padovan 		default:
5220a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
5230a708f8fSGustavo F. Padovan 		}
524fe4128e0SGustavo F. Padovan 	} else if (chan->psm == cpu_to_le16(0x0001)) {
5254343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_LOW)
5264343478fSGustavo F. Padovan 			chan->sec_level = BT_SECURITY_SDP;
5270a708f8fSGustavo F. Padovan 
5284343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_HIGH)
5290a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING_MITM;
5300a708f8fSGustavo F. Padovan 		else
5310a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
5320a708f8fSGustavo F. Padovan 	} else {
5334343478fSGustavo F. Padovan 		switch (chan->sec_level) {
5340a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
5350a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING_MITM;
5360a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
5370a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING;
5380a708f8fSGustavo F. Padovan 		default:
5390a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
5400a708f8fSGustavo F. Padovan 		}
5410a708f8fSGustavo F. Padovan 	}
5420a708f8fSGustavo F. Padovan }
5430a708f8fSGustavo F. Padovan 
5440a708f8fSGustavo F. Padovan /* Service level security */
545d45fc423SGustavo F. Padovan int l2cap_chan_check_security(struct l2cap_chan *chan)
5460a708f8fSGustavo F. Padovan {
5478c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
5480a708f8fSGustavo F. Padovan 	__u8 auth_type;
5490a708f8fSGustavo F. Padovan 
5504343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
5510a708f8fSGustavo F. Padovan 
5524343478fSGustavo F. Padovan 	return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
5530a708f8fSGustavo F. Padovan }
5540a708f8fSGustavo F. Padovan 
555b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn)
5560a708f8fSGustavo F. Padovan {
5570a708f8fSGustavo F. Padovan 	u8 id;
5580a708f8fSGustavo F. Padovan 
5590a708f8fSGustavo F. Padovan 	/* Get next available identificator.
5600a708f8fSGustavo F. Padovan 	 *    1 - 128 are used by kernel.
5610a708f8fSGustavo F. Padovan 	 *  129 - 199 are reserved.
5620a708f8fSGustavo F. Padovan 	 *  200 - 254 are used by utilities like l2ping, etc.
5630a708f8fSGustavo F. Padovan 	 */
5640a708f8fSGustavo F. Padovan 
5650a708f8fSGustavo F. Padovan 	spin_lock_bh(&conn->lock);
5660a708f8fSGustavo F. Padovan 
5670a708f8fSGustavo F. Padovan 	if (++conn->tx_ident > 128)
5680a708f8fSGustavo F. Padovan 		conn->tx_ident = 1;
5690a708f8fSGustavo F. Padovan 
5700a708f8fSGustavo F. Padovan 	id = conn->tx_ident;
5710a708f8fSGustavo F. Padovan 
5720a708f8fSGustavo F. Padovan 	spin_unlock_bh(&conn->lock);
5730a708f8fSGustavo F. Padovan 
5740a708f8fSGustavo F. Padovan 	return id;
5750a708f8fSGustavo F. Padovan }
5760a708f8fSGustavo F. Padovan 
5774519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
5780a708f8fSGustavo F. Padovan {
5790a708f8fSGustavo F. Padovan 	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
5800a708f8fSGustavo F. Padovan 	u8 flags;
5810a708f8fSGustavo F. Padovan 
5820a708f8fSGustavo F. Padovan 	BT_DBG("code 0x%2.2x", code);
5830a708f8fSGustavo F. Padovan 
5840a708f8fSGustavo F. Padovan 	if (!skb)
5850a708f8fSGustavo F. Padovan 		return;
5860a708f8fSGustavo F. Padovan 
5870a708f8fSGustavo F. Padovan 	if (lmp_no_flush_capable(conn->hcon->hdev))
5880a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
5890a708f8fSGustavo F. Padovan 	else
5900a708f8fSGustavo F. Padovan 		flags = ACL_START;
5910a708f8fSGustavo F. Padovan 
59214b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
5935e59b791SLuiz Augusto von Dentz 	skb->priority = HCI_PRIO_MAX;
59414b12d0bSJaikumar Ganesh 
59573d80debSLuiz Augusto von Dentz 	hci_send_acl(conn->hchan, skb, flags);
5960a708f8fSGustavo F. Padovan }
5970a708f8fSGustavo F. Padovan 
59873d80debSLuiz Augusto von Dentz static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
59973d80debSLuiz Augusto von Dentz {
60073d80debSLuiz Augusto von Dentz 	struct hci_conn *hcon = chan->conn->hcon;
60173d80debSLuiz Augusto von Dentz 	u16 flags;
60273d80debSLuiz Augusto von Dentz 
60373d80debSLuiz Augusto von Dentz 	BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
60473d80debSLuiz Augusto von Dentz 							skb->priority);
60573d80debSLuiz Augusto von Dentz 
60673d80debSLuiz Augusto von Dentz 	if (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
60773d80debSLuiz Augusto von Dentz 					lmp_no_flush_capable(hcon->hdev))
60873d80debSLuiz Augusto von Dentz 		flags = ACL_START_NO_FLUSH;
60973d80debSLuiz Augusto von Dentz 	else
61073d80debSLuiz Augusto von Dentz 		flags = ACL_START;
61173d80debSLuiz Augusto von Dentz 
61273d80debSLuiz Augusto von Dentz 	bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
61373d80debSLuiz Augusto von Dentz 	hci_send_acl(chan->conn->hchan, skb, flags);
6140a708f8fSGustavo F. Padovan }
6150a708f8fSGustavo F. Padovan 
61688843ab0SAndrei Emeltchenko static inline void l2cap_send_sframe(struct l2cap_chan *chan, u32 control)
6170a708f8fSGustavo F. Padovan {
6180a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
6190a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
6208c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
621e4ca6d98SAndrei Emeltchenko 	int count, hlen;
6220a708f8fSGustavo F. Padovan 
62389bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
6240a708f8fSGustavo F. Padovan 		return;
6250a708f8fSGustavo F. Padovan 
626e4ca6d98SAndrei Emeltchenko 	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
627e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_EXT_HDR_SIZE;
628e4ca6d98SAndrei Emeltchenko 	else
629e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_ENH_HDR_SIZE;
6300a708f8fSGustavo F. Padovan 
6310a708f8fSGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
63203a51213SAndrei Emeltchenko 		hlen += L2CAP_FCS_SIZE;
6330a708f8fSGustavo F. Padovan 
63488843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, control 0x%8.8x", chan, control);
6350a708f8fSGustavo F. Padovan 
6360a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, hlen);
637793c2f1cSAndrei Emeltchenko 
638793c2f1cSAndrei Emeltchenko 	control |= __set_sframe(chan);
6390a708f8fSGustavo F. Padovan 
640e2ab4353SGustavo F. Padovan 	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
64103f6715dSAndrei Emeltchenko 		control |= __set_ctrl_final(chan);
6420a708f8fSGustavo F. Padovan 
643e2ab4353SGustavo F. Padovan 	if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state))
644e3781735SAndrei Emeltchenko 		control |= __set_ctrl_poll(chan);
6450a708f8fSGustavo F. Padovan 
6460a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
6470a708f8fSGustavo F. Padovan 	if (!skb)
6480a708f8fSGustavo F. Padovan 		return;
6490a708f8fSGustavo F. Padovan 
6500a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
6510a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
652fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
65388843ab0SAndrei Emeltchenko 
65488843ab0SAndrei Emeltchenko 	__put_control(chan, control, skb_put(skb, __ctrl_size(chan)));
6550a708f8fSGustavo F. Padovan 
65647d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
65703a51213SAndrei Emeltchenko 		u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE);
65803a51213SAndrei Emeltchenko 		put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
6590a708f8fSGustavo F. Padovan 	}
6600a708f8fSGustavo F. Padovan 
66173d80debSLuiz Augusto von Dentz 	skb->priority = HCI_PRIO_MAX;
66273d80debSLuiz Augusto von Dentz 	l2cap_do_send(chan, skb);
6630a708f8fSGustavo F. Padovan }
6640a708f8fSGustavo F. Padovan 
66588843ab0SAndrei Emeltchenko static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u32 control)
6660a708f8fSGustavo F. Padovan {
667e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
668ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
669e2ab4353SGustavo F. Padovan 		set_bit(CONN_RNR_SENT, &chan->conn_state);
6700a708f8fSGustavo F. Padovan 	} else
671ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
6720a708f8fSGustavo F. Padovan 
6730b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
6740a708f8fSGustavo F. Padovan 
675525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
6760a708f8fSGustavo F. Padovan }
6770a708f8fSGustavo F. Padovan 
678b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
6790a708f8fSGustavo F. Padovan {
680c1360a1cSGustavo F. Padovan 	return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
6810a708f8fSGustavo F. Padovan }
6820a708f8fSGustavo F. Padovan 
683fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan)
6840a708f8fSGustavo F. Padovan {
6858c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
6860a708f8fSGustavo F. Padovan 
6870a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
6880a708f8fSGustavo F. Padovan 		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
6890a708f8fSGustavo F. Padovan 			return;
6900a708f8fSGustavo F. Padovan 
691d45fc423SGustavo F. Padovan 		if (l2cap_chan_check_security(chan) &&
6924343478fSGustavo F. Padovan 				__l2cap_no_conn_pending(chan)) {
6930a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
694fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
695fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
6960a708f8fSGustavo F. Padovan 
697fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
698c1360a1cSGustavo F. Padovan 			set_bit(CONF_CONNECT_PEND, &chan->conf_state);
6990a708f8fSGustavo F. Padovan 
700fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
701fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
7020a708f8fSGustavo F. Padovan 		}
7030a708f8fSGustavo F. Padovan 	} else {
7040a708f8fSGustavo F. Padovan 		struct l2cap_info_req req;
7050a708f8fSGustavo F. Padovan 		req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
7060a708f8fSGustavo F. Padovan 
7070a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
7080a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
7090a708f8fSGustavo F. Padovan 
7100a708f8fSGustavo F. Padovan 		mod_timer(&conn->info_timer, jiffies +
7110a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
7120a708f8fSGustavo F. Padovan 
7130a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
7140a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
7150a708f8fSGustavo F. Padovan 	}
7160a708f8fSGustavo F. Padovan }
7170a708f8fSGustavo F. Padovan 
7180a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
7190a708f8fSGustavo F. Padovan {
7200a708f8fSGustavo F. Padovan 	u32 local_feat_mask = l2cap_feat_mask;
7210a708f8fSGustavo F. Padovan 	if (!disable_ertm)
7220a708f8fSGustavo F. Padovan 		local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
7230a708f8fSGustavo F. Padovan 
7240a708f8fSGustavo F. Padovan 	switch (mode) {
7250a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
7260a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
7270a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
7280a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
7290a708f8fSGustavo F. Padovan 	default:
7300a708f8fSGustavo F. Padovan 		return 0x00;
7310a708f8fSGustavo F. Padovan 	}
7320a708f8fSGustavo F. Padovan }
7330a708f8fSGustavo F. Padovan 
7344519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
7350a708f8fSGustavo F. Padovan {
736e92c8e70SGustavo F. Padovan 	struct sock *sk;
7370a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req req;
7380a708f8fSGustavo F. Padovan 
7390a708f8fSGustavo F. Padovan 	if (!conn)
7400a708f8fSGustavo F. Padovan 		return;
7410a708f8fSGustavo F. Padovan 
742e92c8e70SGustavo F. Padovan 	sk = chan->sk;
743e92c8e70SGustavo F. Padovan 
7440c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
7451a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
7461a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
7471a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
7480a708f8fSGustavo F. Padovan 	}
7490a708f8fSGustavo F. Padovan 
750fe4128e0SGustavo F. Padovan 	req.dcid = cpu_to_le16(chan->dcid);
751fe4128e0SGustavo F. Padovan 	req.scid = cpu_to_le16(chan->scid);
7520a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn),
7530a708f8fSGustavo F. Padovan 			L2CAP_DISCONN_REQ, sizeof(req), &req);
7540a708f8fSGustavo F. Padovan 
75589bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_DISCONN);
7560a708f8fSGustavo F. Padovan 	sk->sk_err = err;
7570a708f8fSGustavo F. Padovan }
7580a708f8fSGustavo F. Padovan 
7590a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */
7600a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn)
7610a708f8fSGustavo F. Padovan {
762820ffdb3SGustavo F. Padovan 	struct l2cap_chan *chan, *tmp;
7630a708f8fSGustavo F. Padovan 
7640a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
7650a708f8fSGustavo F. Padovan 
766baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
7670a708f8fSGustavo F. Padovan 
768820ffdb3SGustavo F. Padovan 	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
76948454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
770baa7e1faSGustavo F. Padovan 
7710a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
7720a708f8fSGustavo F. Padovan 
773715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7740a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
7750a708f8fSGustavo F. Padovan 			continue;
7760a708f8fSGustavo F. Padovan 		}
7770a708f8fSGustavo F. Padovan 
77889bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
7790a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
7800a708f8fSGustavo F. Padovan 
781d45fc423SGustavo F. Padovan 			if (!l2cap_chan_check_security(chan) ||
782b4450035SGustavo F. Padovan 					!__l2cap_no_conn_pending(chan)) {
7830a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7840a708f8fSGustavo F. Padovan 				continue;
7850a708f8fSGustavo F. Padovan 			}
7860a708f8fSGustavo F. Padovan 
787c1360a1cSGustavo F. Padovan 			if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
788c1360a1cSGustavo F. Padovan 					&& test_bit(CONF_STATE2_DEVICE,
789c1360a1cSGustavo F. Padovan 					&chan->conf_state)) {
7900f852724SGustavo F. Padovan 				/* l2cap_chan_close() calls list_del(chan)
791820ffdb3SGustavo F. Padovan 				 * so release the lock */
7922461daacSGustavo F. Padovan 				read_unlock(&conn->chan_lock);
7930f852724SGustavo F. Padovan 				l2cap_chan_close(chan, ECONNRESET);
7942461daacSGustavo F. Padovan 				read_lock(&conn->chan_lock);
7950a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7960a708f8fSGustavo F. Padovan 				continue;
7970a708f8fSGustavo F. Padovan 			}
7980a708f8fSGustavo F. Padovan 
799fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
800fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
8010a708f8fSGustavo F. Padovan 
802fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
803c1360a1cSGustavo F. Padovan 			set_bit(CONF_CONNECT_PEND, &chan->conf_state);
8040a708f8fSGustavo F. Padovan 
805fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
806fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
8070a708f8fSGustavo F. Padovan 
80889bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
8090a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
8100a708f8fSGustavo F. Padovan 			char buf[128];
811fe4128e0SGustavo F. Padovan 			rsp.scid = cpu_to_le16(chan->dcid);
812fe4128e0SGustavo F. Padovan 			rsp.dcid = cpu_to_le16(chan->scid);
8130a708f8fSGustavo F. Padovan 
814d45fc423SGustavo F. Padovan 			if (l2cap_chan_check_security(chan)) {
8150a708f8fSGustavo F. Padovan 				if (bt_sk(sk)->defer_setup) {
8160a708f8fSGustavo F. Padovan 					struct sock *parent = bt_sk(sk)->parent;
8170a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_PEND);
8180a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
81905e9a2f6SIlia Kolomisnky 					if (parent)
8200a708f8fSGustavo F. Padovan 						parent->sk_data_ready(parent, 0);
8210a708f8fSGustavo F. Padovan 
8220a708f8fSGustavo F. Padovan 				} else {
82389bc500eSGustavo F. Padovan 					l2cap_state_change(chan, BT_CONFIG);
8240a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
8250a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
8260a708f8fSGustavo F. Padovan 				}
8270a708f8fSGustavo F. Padovan 			} else {
8280a708f8fSGustavo F. Padovan 				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
8290a708f8fSGustavo F. Padovan 				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
8300a708f8fSGustavo F. Padovan 			}
8310a708f8fSGustavo F. Padovan 
832fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
833fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
8340a708f8fSGustavo F. Padovan 
835c1360a1cSGustavo F. Padovan 			if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
8360a708f8fSGustavo F. Padovan 					rsp.result != L2CAP_CR_SUCCESS) {
8370a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
8380a708f8fSGustavo F. Padovan 				continue;
8390a708f8fSGustavo F. Padovan 			}
8400a708f8fSGustavo F. Padovan 
841c1360a1cSGustavo F. Padovan 			set_bit(CONF_REQ_SENT, &chan->conf_state);
8420a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
84373ffa904SGustavo F. Padovan 						l2cap_build_conf_req(chan, buf), buf);
84473ffa904SGustavo F. Padovan 			chan->num_conf_req++;
8450a708f8fSGustavo F. Padovan 		}
8460a708f8fSGustavo F. Padovan 
8470a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
8480a708f8fSGustavo F. Padovan 	}
8490a708f8fSGustavo F. Padovan 
850baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
8510a708f8fSGustavo F. Padovan }
8520a708f8fSGustavo F. Padovan 
853b62f328bSVille Tervo /* Find socket with cid and source bdaddr.
854b62f328bSVille Tervo  * Returns closest match, locked.
855b62f328bSVille Tervo  */
85623691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src)
857b62f328bSVille Tervo {
85823691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
859b62f328bSVille Tervo 
86023691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
861b62f328bSVille Tervo 
86223691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
86323691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
864fe4128e0SGustavo F. Padovan 
86589bc500eSGustavo F. Padovan 		if (state && c->state != state)
866b62f328bSVille Tervo 			continue;
867b62f328bSVille Tervo 
86823691d75SGustavo F. Padovan 		if (c->scid == cid) {
869b62f328bSVille Tervo 			/* Exact match. */
87023691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
87123691d75SGustavo F. Padovan 				read_unlock(&chan_list_lock);
87223691d75SGustavo F. Padovan 				return c;
87323691d75SGustavo F. Padovan 			}
874b62f328bSVille Tervo 
875b62f328bSVille Tervo 			/* Closest match */
876b62f328bSVille Tervo 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
87723691d75SGustavo F. Padovan 				c1 = c;
878b62f328bSVille Tervo 		}
879b62f328bSVille Tervo 	}
880280f294fSGustavo F. Padovan 
88123691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
882b62f328bSVille Tervo 
88323691d75SGustavo F. Padovan 	return c1;
884b62f328bSVille Tervo }
885b62f328bSVille Tervo 
886b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn)
887b62f328bSVille Tervo {
888c916fbe4SGustavo F. Padovan 	struct sock *parent, *sk;
88923691d75SGustavo F. Padovan 	struct l2cap_chan *chan, *pchan;
890b62f328bSVille Tervo 
891b62f328bSVille Tervo 	BT_DBG("");
892b62f328bSVille Tervo 
893b62f328bSVille Tervo 	/* Check if we have socket listening on cid */
89423691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
895b62f328bSVille Tervo 							conn->src);
89623691d75SGustavo F. Padovan 	if (!pchan)
897b62f328bSVille Tervo 		return;
898b62f328bSVille Tervo 
89923691d75SGustavo F. Padovan 	parent = pchan->sk;
90023691d75SGustavo F. Padovan 
90162f3a2cfSGustavo F. Padovan 	bh_lock_sock(parent);
90262f3a2cfSGustavo F. Padovan 
903b62f328bSVille Tervo 	/* Check for backlog size */
904b62f328bSVille Tervo 	if (sk_acceptq_is_full(parent)) {
905b62f328bSVille Tervo 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
906b62f328bSVille Tervo 		goto clean;
907b62f328bSVille Tervo 	}
908b62f328bSVille Tervo 
90980808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
91080808e43SGustavo F. Padovan 	if (!chan)
911b62f328bSVille Tervo 		goto clean;
912b62f328bSVille Tervo 
91380808e43SGustavo F. Padovan 	sk = chan->sk;
9145d41ce1dSGustavo F. Padovan 
915baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
916b62f328bSVille Tervo 
917b62f328bSVille Tervo 	hci_conn_hold(conn->hcon);
918b62f328bSVille Tervo 
919b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->src, conn->src);
920b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->dst, conn->dst);
921b62f328bSVille Tervo 
922d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
923d1010240SGustavo F. Padovan 
92448454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
92548454079SGustavo F. Padovan 
926c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
927b62f328bSVille Tervo 
92889bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CONNECTED);
929b62f328bSVille Tervo 	parent->sk_data_ready(parent, 0);
930b62f328bSVille Tervo 
931baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
932b62f328bSVille Tervo 
933b62f328bSVille Tervo clean:
934b62f328bSVille Tervo 	bh_unlock_sock(parent);
935b62f328bSVille Tervo }
936b62f328bSVille Tervo 
937f1cb9af5SVinicius Costa Gomes static void l2cap_chan_ready(struct sock *sk)
938f1cb9af5SVinicius Costa Gomes {
939f1cb9af5SVinicius Costa Gomes 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
940f1cb9af5SVinicius Costa Gomes 	struct sock *parent = bt_sk(sk)->parent;
941f1cb9af5SVinicius Costa Gomes 
942f1cb9af5SVinicius Costa Gomes 	BT_DBG("sk %p, parent %p", sk, parent);
943f1cb9af5SVinicius Costa Gomes 
944f1cb9af5SVinicius Costa Gomes 	chan->conf_state = 0;
945f1cb9af5SVinicius Costa Gomes 	__clear_chan_timer(chan);
946f1cb9af5SVinicius Costa Gomes 
94743f3dc41SVinicius Costa Gomes 	l2cap_state_change(chan, BT_CONNECTED);
948f1cb9af5SVinicius Costa Gomes 	sk->sk_state_change(sk);
949f1cb9af5SVinicius Costa Gomes 
950f1cb9af5SVinicius Costa Gomes 	if (parent)
951f1cb9af5SVinicius Costa Gomes 		parent->sk_data_ready(parent, 0);
952f1cb9af5SVinicius Costa Gomes }
953f1cb9af5SVinicius Costa Gomes 
9540a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn)
9550a708f8fSGustavo F. Padovan {
95648454079SGustavo F. Padovan 	struct l2cap_chan *chan;
9570a708f8fSGustavo F. Padovan 
9580a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
9590a708f8fSGustavo F. Padovan 
960b62f328bSVille Tervo 	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
961b62f328bSVille Tervo 		l2cap_le_conn_ready(conn);
962b62f328bSVille Tervo 
963160dc6acSVinicius Costa Gomes 	if (conn->hcon->out && conn->hcon->type == LE_LINK)
964160dc6acSVinicius Costa Gomes 		smp_conn_security(conn, conn->hcon->pending_sec_level);
965160dc6acSVinicius Costa Gomes 
966baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
9670a708f8fSGustavo F. Padovan 
968baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
96948454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
970baa7e1faSGustavo F. Padovan 
9710a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
9720a708f8fSGustavo F. Padovan 
97363128451SVinicius Costa Gomes 		if (conn->hcon->type == LE_LINK) {
974b501d6a1SAnderson Briglia 			if (smp_conn_security(conn, chan->sec_level))
975f1cb9af5SVinicius Costa Gomes 				l2cap_chan_ready(sk);
976acd7d370SVille Tervo 
97763128451SVinicius Costa Gomes 		} else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
978c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
97989bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_CONNECTED);
9800a708f8fSGustavo F. Padovan 			sk->sk_state_change(sk);
981b501d6a1SAnderson Briglia 
98289bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT)
983fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
9840a708f8fSGustavo F. Padovan 
9850a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
9860a708f8fSGustavo F. Padovan 	}
9870a708f8fSGustavo F. Padovan 
988baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
9890a708f8fSGustavo F. Padovan }
9900a708f8fSGustavo F. Padovan 
9910a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */
9920a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
9930a708f8fSGustavo F. Padovan {
99448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
9950a708f8fSGustavo F. Padovan 
9960a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
9970a708f8fSGustavo F. Padovan 
998baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
9990a708f8fSGustavo F. Padovan 
1000baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
100148454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
1002baa7e1faSGustavo F. Padovan 
1003ecf61bdbSAndrei Emeltchenko 		if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
10040a708f8fSGustavo F. Padovan 			sk->sk_err = err;
10050a708f8fSGustavo F. Padovan 	}
10060a708f8fSGustavo F. Padovan 
1007baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
10080a708f8fSGustavo F. Padovan }
10090a708f8fSGustavo F. Padovan 
10100a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg)
10110a708f8fSGustavo F. Padovan {
10120a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = (void *) arg;
10130a708f8fSGustavo F. Padovan 
10140a708f8fSGustavo F. Padovan 	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
10150a708f8fSGustavo F. Padovan 	conn->info_ident = 0;
10160a708f8fSGustavo F. Padovan 
10170a708f8fSGustavo F. Padovan 	l2cap_conn_start(conn);
10180a708f8fSGustavo F. Padovan }
10190a708f8fSGustavo F. Padovan 
10205d3de7dfSVinicius Costa Gomes static void l2cap_conn_del(struct hci_conn *hcon, int err)
10215d3de7dfSVinicius Costa Gomes {
10225d3de7dfSVinicius Costa Gomes 	struct l2cap_conn *conn = hcon->l2cap_data;
10235d3de7dfSVinicius Costa Gomes 	struct l2cap_chan *chan, *l;
10245d3de7dfSVinicius Costa Gomes 	struct sock *sk;
10255d3de7dfSVinicius Costa Gomes 
10265d3de7dfSVinicius Costa Gomes 	if (!conn)
10275d3de7dfSVinicius Costa Gomes 		return;
10285d3de7dfSVinicius Costa Gomes 
10295d3de7dfSVinicius Costa Gomes 	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
10305d3de7dfSVinicius Costa Gomes 
10315d3de7dfSVinicius Costa Gomes 	kfree_skb(conn->rx_skb);
10325d3de7dfSVinicius Costa Gomes 
10335d3de7dfSVinicius Costa Gomes 	/* Kill channels */
10345d3de7dfSVinicius Costa Gomes 	list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
10355d3de7dfSVinicius Costa Gomes 		sk = chan->sk;
10365d3de7dfSVinicius Costa Gomes 		bh_lock_sock(sk);
10375d3de7dfSVinicius Costa Gomes 		l2cap_chan_del(chan, err);
10385d3de7dfSVinicius Costa Gomes 		bh_unlock_sock(sk);
10395d3de7dfSVinicius Costa Gomes 		chan->ops->close(chan->data);
10405d3de7dfSVinicius Costa Gomes 	}
10415d3de7dfSVinicius Costa Gomes 
104273d80debSLuiz Augusto von Dentz 	hci_chan_del(conn->hchan);
104373d80debSLuiz Augusto von Dentz 
10445d3de7dfSVinicius Costa Gomes 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
10455d3de7dfSVinicius Costa Gomes 		del_timer_sync(&conn->info_timer);
10465d3de7dfSVinicius Costa Gomes 
1047d26a2345SVinicius Costa Gomes 	if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend)) {
10485d3de7dfSVinicius Costa Gomes 		del_timer(&conn->security_timer);
10498aab4757SVinicius Costa Gomes 		smp_chan_destroy(conn);
1050d26a2345SVinicius Costa Gomes 	}
10515d3de7dfSVinicius Costa Gomes 
10525d3de7dfSVinicius Costa Gomes 	hcon->l2cap_data = NULL;
10535d3de7dfSVinicius Costa Gomes 	kfree(conn);
10545d3de7dfSVinicius Costa Gomes }
10555d3de7dfSVinicius Costa Gomes 
10565d3de7dfSVinicius Costa Gomes static void security_timeout(unsigned long arg)
10575d3de7dfSVinicius Costa Gomes {
10585d3de7dfSVinicius Costa Gomes 	struct l2cap_conn *conn = (void *) arg;
10595d3de7dfSVinicius Costa Gomes 
10605d3de7dfSVinicius Costa Gomes 	l2cap_conn_del(conn->hcon, ETIMEDOUT);
10615d3de7dfSVinicius Costa Gomes }
10625d3de7dfSVinicius Costa Gomes 
10630a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
10640a708f8fSGustavo F. Padovan {
10650a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
106673d80debSLuiz Augusto von Dentz 	struct hci_chan *hchan;
10670a708f8fSGustavo F. Padovan 
10680a708f8fSGustavo F. Padovan 	if (conn || status)
10690a708f8fSGustavo F. Padovan 		return conn;
10700a708f8fSGustavo F. Padovan 
107173d80debSLuiz Augusto von Dentz 	hchan = hci_chan_create(hcon);
107273d80debSLuiz Augusto von Dentz 	if (!hchan)
10730a708f8fSGustavo F. Padovan 		return NULL;
10740a708f8fSGustavo F. Padovan 
107573d80debSLuiz Augusto von Dentz 	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
107673d80debSLuiz Augusto von Dentz 	if (!conn) {
107773d80debSLuiz Augusto von Dentz 		hci_chan_del(hchan);
107873d80debSLuiz Augusto von Dentz 		return NULL;
107973d80debSLuiz Augusto von Dentz 	}
108073d80debSLuiz Augusto von Dentz 
10810a708f8fSGustavo F. Padovan 	hcon->l2cap_data = conn;
10820a708f8fSGustavo F. Padovan 	conn->hcon = hcon;
108373d80debSLuiz Augusto von Dentz 	conn->hchan = hchan;
10840a708f8fSGustavo F. Padovan 
108573d80debSLuiz Augusto von Dentz 	BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
10860a708f8fSGustavo F. Padovan 
1087acd7d370SVille Tervo 	if (hcon->hdev->le_mtu && hcon->type == LE_LINK)
1088acd7d370SVille Tervo 		conn->mtu = hcon->hdev->le_mtu;
1089acd7d370SVille Tervo 	else
10900a708f8fSGustavo F. Padovan 		conn->mtu = hcon->hdev->acl_mtu;
1091acd7d370SVille Tervo 
10920a708f8fSGustavo F. Padovan 	conn->src = &hcon->hdev->bdaddr;
10930a708f8fSGustavo F. Padovan 	conn->dst = &hcon->dst;
10940a708f8fSGustavo F. Padovan 
10950a708f8fSGustavo F. Padovan 	conn->feat_mask = 0;
10960a708f8fSGustavo F. Padovan 
10970a708f8fSGustavo F. Padovan 	spin_lock_init(&conn->lock);
1098baa7e1faSGustavo F. Padovan 	rwlock_init(&conn->chan_lock);
1099baa7e1faSGustavo F. Padovan 
1100baa7e1faSGustavo F. Padovan 	INIT_LIST_HEAD(&conn->chan_l);
11010a708f8fSGustavo F. Padovan 
11025d3de7dfSVinicius Costa Gomes 	if (hcon->type == LE_LINK)
11035d3de7dfSVinicius Costa Gomes 		setup_timer(&conn->security_timer, security_timeout,
11045d3de7dfSVinicius Costa Gomes 						(unsigned long) conn);
11055d3de7dfSVinicius Costa Gomes 	else
11060a708f8fSGustavo F. Padovan 		setup_timer(&conn->info_timer, l2cap_info_timeout,
11070a708f8fSGustavo F. Padovan 						(unsigned long) conn);
11080a708f8fSGustavo F. Padovan 
11099f5a0d7bSAndrei Emeltchenko 	conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
11100a708f8fSGustavo F. Padovan 
11110a708f8fSGustavo F. Padovan 	return conn;
11120a708f8fSGustavo F. Padovan }
11130a708f8fSGustavo F. Padovan 
111448454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
11150a708f8fSGustavo F. Padovan {
1116baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
111748454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
1118baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
11190a708f8fSGustavo F. Padovan }
11200a708f8fSGustavo F. Padovan 
11210a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */
11220a708f8fSGustavo F. Padovan 
11230a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr.
11240a708f8fSGustavo F. Padovan  * Returns closest match.
11250a708f8fSGustavo F. Padovan  */
112623691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src)
11270a708f8fSGustavo F. Padovan {
112823691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
11290a708f8fSGustavo F. Padovan 
113023691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
11310a708f8fSGustavo F. Padovan 
113223691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
113323691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
1134fe4128e0SGustavo F. Padovan 
113589bc500eSGustavo F. Padovan 		if (state && c->state != state)
11360a708f8fSGustavo F. Padovan 			continue;
11370a708f8fSGustavo F. Padovan 
113823691d75SGustavo F. Padovan 		if (c->psm == psm) {
11390a708f8fSGustavo F. Padovan 			/* Exact match. */
114023691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
1141a7567b20SJohannes Berg 				read_unlock(&chan_list_lock);
114223691d75SGustavo F. Padovan 				return c;
114323691d75SGustavo F. Padovan 			}
11440a708f8fSGustavo F. Padovan 
11450a708f8fSGustavo F. Padovan 			/* Closest match */
11460a708f8fSGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
114723691d75SGustavo F. Padovan 				c1 = c;
11480a708f8fSGustavo F. Padovan 		}
11490a708f8fSGustavo F. Padovan 	}
11500a708f8fSGustavo F. Padovan 
115123691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
11520a708f8fSGustavo F. Padovan 
115323691d75SGustavo F. Padovan 	return c1;
11540a708f8fSGustavo F. Padovan }
11550a708f8fSGustavo F. Padovan 
115677a74c7eSGustavo F. Padovan int l2cap_chan_connect(struct l2cap_chan *chan)
11570a708f8fSGustavo F. Padovan {
11585d41ce1dSGustavo F. Padovan 	struct sock *sk = chan->sk;
11590a708f8fSGustavo F. Padovan 	bdaddr_t *src = &bt_sk(sk)->src;
11600a708f8fSGustavo F. Padovan 	bdaddr_t *dst = &bt_sk(sk)->dst;
11610a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
11620a708f8fSGustavo F. Padovan 	struct hci_conn *hcon;
11630a708f8fSGustavo F. Padovan 	struct hci_dev *hdev;
11640a708f8fSGustavo F. Padovan 	__u8 auth_type;
11650a708f8fSGustavo F. Padovan 	int err;
11660a708f8fSGustavo F. Padovan 
11670a708f8fSGustavo F. Padovan 	BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
1168fe4128e0SGustavo F. Padovan 							chan->psm);
11690a708f8fSGustavo F. Padovan 
11700a708f8fSGustavo F. Padovan 	hdev = hci_get_route(dst, src);
11710a708f8fSGustavo F. Padovan 	if (!hdev)
11720a708f8fSGustavo F. Padovan 		return -EHOSTUNREACH;
11730a708f8fSGustavo F. Padovan 
117409fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
11750a708f8fSGustavo F. Padovan 
11764343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
11770a708f8fSGustavo F. Padovan 
1178fe4128e0SGustavo F. Padovan 	if (chan->dcid == L2CAP_CID_LE_DATA)
1179acd7d370SVille Tervo 		hcon = hci_connect(hdev, LE_LINK, dst,
11804343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1181acd7d370SVille Tervo 	else
11820a708f8fSGustavo F. Padovan 		hcon = hci_connect(hdev, ACL_LINK, dst,
11834343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1184acd7d370SVille Tervo 
118530e76272SVille Tervo 	if (IS_ERR(hcon)) {
118630e76272SVille Tervo 		err = PTR_ERR(hcon);
11870a708f8fSGustavo F. Padovan 		goto done;
118830e76272SVille Tervo 	}
11890a708f8fSGustavo F. Padovan 
11900a708f8fSGustavo F. Padovan 	conn = l2cap_conn_add(hcon, 0);
11910a708f8fSGustavo F. Padovan 	if (!conn) {
11920a708f8fSGustavo F. Padovan 		hci_conn_put(hcon);
119330e76272SVille Tervo 		err = -ENOMEM;
11940a708f8fSGustavo F. Padovan 		goto done;
11950a708f8fSGustavo F. Padovan 	}
11960a708f8fSGustavo F. Padovan 
11970a708f8fSGustavo F. Padovan 	/* Update source addr of the socket */
11980a708f8fSGustavo F. Padovan 	bacpy(src, conn->src);
11990a708f8fSGustavo F. Padovan 
120048454079SGustavo F. Padovan 	l2cap_chan_add(conn, chan);
120148454079SGustavo F. Padovan 
120289bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CONNECT);
1203c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
12040a708f8fSGustavo F. Padovan 
12050a708f8fSGustavo F. Padovan 	if (hcon->state == BT_CONNECTED) {
1206715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1207c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
1208d45fc423SGustavo F. Padovan 			if (l2cap_chan_check_security(chan))
120989bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONNECTED);
12100a708f8fSGustavo F. Padovan 		} else
1211fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
12120a708f8fSGustavo F. Padovan 	}
12130a708f8fSGustavo F. Padovan 
121430e76272SVille Tervo 	err = 0;
121530e76272SVille Tervo 
12160a708f8fSGustavo F. Padovan done:
121709fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
12180a708f8fSGustavo F. Padovan 	hci_dev_put(hdev);
12190a708f8fSGustavo F. Padovan 	return err;
12200a708f8fSGustavo F. Padovan }
12210a708f8fSGustavo F. Padovan 
1222dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk)
12230a708f8fSGustavo F. Padovan {
12248c1d787bSGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
12250a708f8fSGustavo F. Padovan 	DECLARE_WAITQUEUE(wait, current);
12260a708f8fSGustavo F. Padovan 	int err = 0;
12270a708f8fSGustavo F. Padovan 	int timeo = HZ/5;
12280a708f8fSGustavo F. Padovan 
12290a708f8fSGustavo F. Padovan 	add_wait_queue(sk_sleep(sk), &wait);
12300a708f8fSGustavo F. Padovan 	set_current_state(TASK_INTERRUPTIBLE);
1231a71a0cf4SPeter Hurley 	while (chan->unacked_frames > 0 && chan->conn) {
12320a708f8fSGustavo F. Padovan 		if (!timeo)
12330a708f8fSGustavo F. Padovan 			timeo = HZ/5;
12340a708f8fSGustavo F. Padovan 
12350a708f8fSGustavo F. Padovan 		if (signal_pending(current)) {
12360a708f8fSGustavo F. Padovan 			err = sock_intr_errno(timeo);
12370a708f8fSGustavo F. Padovan 			break;
12380a708f8fSGustavo F. Padovan 		}
12390a708f8fSGustavo F. Padovan 
12400a708f8fSGustavo F. Padovan 		release_sock(sk);
12410a708f8fSGustavo F. Padovan 		timeo = schedule_timeout(timeo);
12420a708f8fSGustavo F. Padovan 		lock_sock(sk);
1243a71a0cf4SPeter Hurley 		set_current_state(TASK_INTERRUPTIBLE);
12440a708f8fSGustavo F. Padovan 
12450a708f8fSGustavo F. Padovan 		err = sock_error(sk);
12460a708f8fSGustavo F. Padovan 		if (err)
12470a708f8fSGustavo F. Padovan 			break;
12480a708f8fSGustavo F. Padovan 	}
12490a708f8fSGustavo F. Padovan 	set_current_state(TASK_RUNNING);
12500a708f8fSGustavo F. Padovan 	remove_wait_queue(sk_sleep(sk), &wait);
12510a708f8fSGustavo F. Padovan 	return err;
12520a708f8fSGustavo F. Padovan }
12530a708f8fSGustavo F. Padovan 
12540a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg)
12550a708f8fSGustavo F. Padovan {
1256525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
1257525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
12580a708f8fSGustavo F. Padovan 
1259525cd185SGustavo F. Padovan 	BT_DBG("chan %p", chan);
12600a708f8fSGustavo F. Padovan 
12610a708f8fSGustavo F. Padovan 	bh_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);
12640a708f8fSGustavo F. Padovan 		bh_unlock_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);
12720a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
12730a708f8fSGustavo F. Padovan }
12740a708f8fSGustavo F. Padovan 
12750a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg)
12760a708f8fSGustavo F. Padovan {
1277525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
1278525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
12790a708f8fSGustavo F. Padovan 
128049208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
12810a708f8fSGustavo F. Padovan 
12820a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
12836a026610SGustavo F. Padovan 	chan->retry_count = 1;
12841a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
12850a708f8fSGustavo F. Padovan 
1286e2ab4353SGustavo F. Padovan 	set_bit(CONN_WAIT_F, &chan->conn_state);
12870a708f8fSGustavo F. Padovan 
1288525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
12890a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
12900a708f8fSGustavo F. Padovan }
12910a708f8fSGustavo F. Padovan 
129242e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
12930a708f8fSGustavo F. Padovan {
12940a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
12950a708f8fSGustavo F. Padovan 
129658d35f87SGustavo F. Padovan 	while ((skb = skb_peek(&chan->tx_q)) &&
12976a026610SGustavo F. Padovan 			chan->unacked_frames) {
129842e5c802SGustavo F. Padovan 		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
12990a708f8fSGustavo F. Padovan 			break;
13000a708f8fSGustavo F. Padovan 
130158d35f87SGustavo F. Padovan 		skb = skb_dequeue(&chan->tx_q);
13020a708f8fSGustavo F. Padovan 		kfree_skb(skb);
13030a708f8fSGustavo F. Padovan 
13046a026610SGustavo F. Padovan 		chan->unacked_frames--;
13050a708f8fSGustavo F. Padovan 	}
13060a708f8fSGustavo F. Padovan 
13076a026610SGustavo F. Padovan 	if (!chan->unacked_frames)
13081a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
13090a708f8fSGustavo F. Padovan }
13100a708f8fSGustavo F. Padovan 
131167c9e840SSzymon Janc static void l2cap_streaming_send(struct l2cap_chan *chan)
13120a708f8fSGustavo F. Padovan {
13130a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
131488843ab0SAndrei Emeltchenko 	u32 control;
131588843ab0SAndrei Emeltchenko 	u16 fcs;
13160a708f8fSGustavo F. Padovan 
131758d35f87SGustavo F. Padovan 	while ((skb = skb_dequeue(&chan->tx_q))) {
131888843ab0SAndrei Emeltchenko 		control = __get_control(chan, skb->data + L2CAP_HDR_SIZE);
1319fb45de7dSAndrei Emeltchenko 		control |= __set_txseq(chan, chan->next_tx_seq);
132088843ab0SAndrei Emeltchenko 		__put_control(chan, control, skb->data + L2CAP_HDR_SIZE);
13210a708f8fSGustavo F. Padovan 
132247d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
132303a51213SAndrei Emeltchenko 			fcs = crc16(0, (u8 *)skb->data,
132403a51213SAndrei Emeltchenko 						skb->len - L2CAP_FCS_SIZE);
132503a51213SAndrei Emeltchenko 			put_unaligned_le16(fcs,
132603a51213SAndrei Emeltchenko 					skb->data + skb->len - L2CAP_FCS_SIZE);
13270a708f8fSGustavo F. Padovan 		}
13280a708f8fSGustavo F. Padovan 
13294343478fSGustavo F. Padovan 		l2cap_do_send(chan, skb);
13300a708f8fSGustavo F. Padovan 
1331836be934SAndrei Emeltchenko 		chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
13320a708f8fSGustavo F. Padovan 	}
13330a708f8fSGustavo F. Padovan }
13340a708f8fSGustavo F. Padovan 
1335fb45de7dSAndrei Emeltchenko static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq)
13360a708f8fSGustavo F. Padovan {
13370a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
133888843ab0SAndrei Emeltchenko 	u16 fcs;
133988843ab0SAndrei Emeltchenko 	u32 control;
13400a708f8fSGustavo F. Padovan 
134158d35f87SGustavo F. Padovan 	skb = skb_peek(&chan->tx_q);
13420a708f8fSGustavo F. Padovan 	if (!skb)
13430a708f8fSGustavo F. Padovan 		return;
13440a708f8fSGustavo F. Padovan 
1345d1726b6dSSzymon Janc 	while (bt_cb(skb)->tx_seq != tx_seq) {
134658d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
13470a708f8fSGustavo F. Padovan 			return;
13480a708f8fSGustavo F. Padovan 
1349d1726b6dSSzymon Janc 		skb = skb_queue_next(&chan->tx_q, skb);
1350d1726b6dSSzymon Janc 	}
13510a708f8fSGustavo F. Padovan 
13522c03a7a4SGustavo F. Padovan 	if (chan->remote_max_tx &&
13532c03a7a4SGustavo F. Padovan 			bt_cb(skb)->retries == chan->remote_max_tx) {
13548c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
13550a708f8fSGustavo F. Padovan 		return;
13560a708f8fSGustavo F. Padovan 	}
13570a708f8fSGustavo F. Padovan 
13580a708f8fSGustavo F. Padovan 	tx_skb = skb_clone(skb, GFP_ATOMIC);
13590a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries++;
136088843ab0SAndrei Emeltchenko 
136188843ab0SAndrei Emeltchenko 	control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE);
13627e0ef6eeSAndrei Emeltchenko 	control &= __get_sar_mask(chan);
13630a708f8fSGustavo F. Padovan 
1364e2ab4353SGustavo F. Padovan 	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
136503f6715dSAndrei Emeltchenko 		control |= __set_ctrl_final(chan);
13660a708f8fSGustavo F. Padovan 
13670b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
1368fb45de7dSAndrei Emeltchenko 	control |= __set_txseq(chan, tx_seq);
13690a708f8fSGustavo F. Padovan 
137088843ab0SAndrei Emeltchenko 	__put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE);
13710a708f8fSGustavo F. Padovan 
137247d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
137303a51213SAndrei Emeltchenko 		fcs = crc16(0, (u8 *)tx_skb->data,
137403a51213SAndrei Emeltchenko 						tx_skb->len - L2CAP_FCS_SIZE);
137503a51213SAndrei Emeltchenko 		put_unaligned_le16(fcs,
137603a51213SAndrei Emeltchenko 				tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE);
13770a708f8fSGustavo F. Padovan 	}
13780a708f8fSGustavo F. Padovan 
13794343478fSGustavo F. Padovan 	l2cap_do_send(chan, tx_skb);
13800a708f8fSGustavo F. Padovan }
13810a708f8fSGustavo F. Padovan 
138267c9e840SSzymon Janc static int l2cap_ertm_send(struct l2cap_chan *chan)
13830a708f8fSGustavo F. Padovan {
13840a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
138588843ab0SAndrei Emeltchenko 	u16 fcs;
138688843ab0SAndrei Emeltchenko 	u32 control;
13870a708f8fSGustavo F. Padovan 	int nsent = 0;
13880a708f8fSGustavo F. Padovan 
138989bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
13900a708f8fSGustavo F. Padovan 		return -ENOTCONN;
13910a708f8fSGustavo F. Padovan 
139258d35f87SGustavo F. Padovan 	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
13930a708f8fSGustavo F. Padovan 
13942c03a7a4SGustavo F. Padovan 		if (chan->remote_max_tx &&
13952c03a7a4SGustavo F. Padovan 				bt_cb(skb)->retries == chan->remote_max_tx) {
13968c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
13970a708f8fSGustavo F. Padovan 			break;
13980a708f8fSGustavo F. Padovan 		}
13990a708f8fSGustavo F. Padovan 
14000a708f8fSGustavo F. Padovan 		tx_skb = skb_clone(skb, GFP_ATOMIC);
14010a708f8fSGustavo F. Padovan 
14020a708f8fSGustavo F. Padovan 		bt_cb(skb)->retries++;
14030a708f8fSGustavo F. Padovan 
140488843ab0SAndrei Emeltchenko 		control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE);
14057e0ef6eeSAndrei Emeltchenko 		control &= __get_sar_mask(chan);
14060a708f8fSGustavo F. Padovan 
1407e2ab4353SGustavo F. Padovan 		if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
140803f6715dSAndrei Emeltchenko 			control |= __set_ctrl_final(chan);
1409e2ab4353SGustavo F. Padovan 
14100b209faeSAndrei Emeltchenko 		control |= __set_reqseq(chan, chan->buffer_seq);
1411fb45de7dSAndrei Emeltchenko 		control |= __set_txseq(chan, chan->next_tx_seq);
14120a708f8fSGustavo F. Padovan 
141388843ab0SAndrei Emeltchenko 		__put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE);
14140a708f8fSGustavo F. Padovan 
141547d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
141603a51213SAndrei Emeltchenko 			fcs = crc16(0, (u8 *)skb->data,
141703a51213SAndrei Emeltchenko 						tx_skb->len - L2CAP_FCS_SIZE);
141803a51213SAndrei Emeltchenko 			put_unaligned_le16(fcs, skb->data +
141903a51213SAndrei Emeltchenko 						tx_skb->len - L2CAP_FCS_SIZE);
14200a708f8fSGustavo F. Padovan 		}
14210a708f8fSGustavo F. Padovan 
14224343478fSGustavo F. Padovan 		l2cap_do_send(chan, tx_skb);
14230a708f8fSGustavo F. Padovan 
14241a09bcb9SGustavo F. Padovan 		__set_retrans_timer(chan);
14250a708f8fSGustavo F. Padovan 
142642e5c802SGustavo F. Padovan 		bt_cb(skb)->tx_seq = chan->next_tx_seq;
1427836be934SAndrei Emeltchenko 
1428836be934SAndrei Emeltchenko 		chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
14290a708f8fSGustavo F. Padovan 
143023e9fde2SSuraj Sumangala 		if (bt_cb(skb)->retries == 1)
14316a026610SGustavo F. Padovan 			chan->unacked_frames++;
143223e9fde2SSuraj Sumangala 
14336a026610SGustavo F. Padovan 		chan->frames_sent++;
14340a708f8fSGustavo F. Padovan 
143558d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
143658d35f87SGustavo F. Padovan 			chan->tx_send_head = NULL;
14370a708f8fSGustavo F. Padovan 		else
143858d35f87SGustavo F. Padovan 			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
14390a708f8fSGustavo F. Padovan 
14400a708f8fSGustavo F. Padovan 		nsent++;
14410a708f8fSGustavo F. Padovan 	}
14420a708f8fSGustavo F. Padovan 
14430a708f8fSGustavo F. Padovan 	return nsent;
14440a708f8fSGustavo F. Padovan }
14450a708f8fSGustavo F. Padovan 
1446525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan)
14470a708f8fSGustavo F. Padovan {
14480a708f8fSGustavo F. Padovan 	int ret;
14490a708f8fSGustavo F. Padovan 
145058d35f87SGustavo F. Padovan 	if (!skb_queue_empty(&chan->tx_q))
145158d35f87SGustavo F. Padovan 		chan->tx_send_head = chan->tx_q.next;
14520a708f8fSGustavo F. Padovan 
145342e5c802SGustavo F. Padovan 	chan->next_tx_seq = chan->expected_ack_seq;
1454525cd185SGustavo F. Padovan 	ret = l2cap_ertm_send(chan);
14550a708f8fSGustavo F. Padovan 	return ret;
14560a708f8fSGustavo F. Padovan }
14570a708f8fSGustavo F. Padovan 
1458525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan)
14590a708f8fSGustavo F. Padovan {
146088843ab0SAndrei Emeltchenko 	u32 control = 0;
14610a708f8fSGustavo F. Padovan 
14620b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
14630a708f8fSGustavo F. Padovan 
1464e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
1465ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
1466e2ab4353SGustavo F. Padovan 		set_bit(CONN_RNR_SENT, &chan->conn_state);
1467525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
14680a708f8fSGustavo F. Padovan 		return;
14690a708f8fSGustavo F. Padovan 	}
14700a708f8fSGustavo F. Padovan 
1471525cd185SGustavo F. Padovan 	if (l2cap_ertm_send(chan) > 0)
14720a708f8fSGustavo F. Padovan 		return;
14730a708f8fSGustavo F. Padovan 
1474ab784b73SAndrei Emeltchenko 	control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
1475525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
14760a708f8fSGustavo F. Padovan }
14770a708f8fSGustavo F. Padovan 
1478525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan)
14790a708f8fSGustavo F. Padovan {
14800a708f8fSGustavo F. Padovan 	struct srej_list *tail;
148188843ab0SAndrei Emeltchenko 	u32 control;
14820a708f8fSGustavo F. Padovan 
1483ab784b73SAndrei Emeltchenko 	control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
148403f6715dSAndrei Emeltchenko 	control |= __set_ctrl_final(chan);
14850a708f8fSGustavo F. Padovan 
148639d5a3eeSGustavo F. Padovan 	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
14870b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, tail->tx_seq);
14880a708f8fSGustavo F. Padovan 
1489525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
14900a708f8fSGustavo F. Padovan }
14910a708f8fSGustavo F. Padovan 
14920a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb)
14930a708f8fSGustavo F. Padovan {
14948c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
14950a708f8fSGustavo F. Padovan 	struct sk_buff **frag;
14960a708f8fSGustavo F. Padovan 	int err, sent = 0;
14970a708f8fSGustavo F. Padovan 
14980a708f8fSGustavo F. Padovan 	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
14990a708f8fSGustavo F. Padovan 		return -EFAULT;
15000a708f8fSGustavo F. Padovan 
15010a708f8fSGustavo F. Padovan 	sent += count;
15020a708f8fSGustavo F. Padovan 	len  -= count;
15030a708f8fSGustavo F. Padovan 
15040a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
15050a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
15060a708f8fSGustavo F. Padovan 	while (len) {
15070a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
15080a708f8fSGustavo F. Padovan 
15090a708f8fSGustavo F. Padovan 		*frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err);
15100a708f8fSGustavo F. Padovan 		if (!*frag)
15110a708f8fSGustavo F. Padovan 			return err;
15120a708f8fSGustavo F. Padovan 		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
15130a708f8fSGustavo F. Padovan 			return -EFAULT;
15140a708f8fSGustavo F. Padovan 
15155e59b791SLuiz Augusto von Dentz 		(*frag)->priority = skb->priority;
15165e59b791SLuiz Augusto von Dentz 
15170a708f8fSGustavo F. Padovan 		sent += count;
15180a708f8fSGustavo F. Padovan 		len  -= count;
15190a708f8fSGustavo F. Padovan 
15200a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
15210a708f8fSGustavo F. Padovan 	}
15220a708f8fSGustavo F. Padovan 
15230a708f8fSGustavo F. Padovan 	return sent;
15240a708f8fSGustavo F. Padovan }
15250a708f8fSGustavo F. Padovan 
15265e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
15275e59b791SLuiz Augusto von Dentz 						struct msghdr *msg, size_t len,
15285e59b791SLuiz Augusto von Dentz 						u32 priority)
15290a708f8fSGustavo F. Padovan {
1530fe4128e0SGustavo F. Padovan 	struct sock *sk = chan->sk;
15318c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
15320a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
153303a51213SAndrei Emeltchenko 	int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
15340a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
15350a708f8fSGustavo F. Padovan 
15365e59b791SLuiz Augusto von Dentz 	BT_DBG("sk %p len %d priority %u", sk, (int)len, priority);
15370a708f8fSGustavo F. Padovan 
15380a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
15390a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
15400a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
15410a708f8fSGustavo F. Padovan 	if (!skb)
15420a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15430a708f8fSGustavo F. Padovan 
15445e59b791SLuiz Augusto von Dentz 	skb->priority = priority;
15455e59b791SLuiz Augusto von Dentz 
15460a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
15470a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1548fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
15490a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1550fe4128e0SGustavo F. Padovan 	put_unaligned_le16(chan->psm, skb_put(skb, 2));
15510a708f8fSGustavo F. Padovan 
15520a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
15530a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
15540a708f8fSGustavo F. Padovan 		kfree_skb(skb);
15550a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15560a708f8fSGustavo F. Padovan 	}
15570a708f8fSGustavo F. Padovan 	return skb;
15580a708f8fSGustavo F. Padovan }
15590a708f8fSGustavo F. Padovan 
15605e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
15615e59b791SLuiz Augusto von Dentz 						struct msghdr *msg, size_t len,
15625e59b791SLuiz Augusto von Dentz 						u32 priority)
15630a708f8fSGustavo F. Padovan {
1564fe4128e0SGustavo F. Padovan 	struct sock *sk = chan->sk;
15658c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
15660a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
15670a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE;
15680a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
15690a708f8fSGustavo F. Padovan 
15700a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
15710a708f8fSGustavo F. Padovan 
15720a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
15730a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
15740a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
15750a708f8fSGustavo F. Padovan 	if (!skb)
15760a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15770a708f8fSGustavo F. Padovan 
15785e59b791SLuiz Augusto von Dentz 	skb->priority = priority;
15795e59b791SLuiz Augusto von Dentz 
15800a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
15810a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1582fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
15830a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
15840a708f8fSGustavo F. Padovan 
15850a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
15860a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
15870a708f8fSGustavo F. Padovan 		kfree_skb(skb);
15880a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15890a708f8fSGustavo F. Padovan 	}
15900a708f8fSGustavo F. Padovan 	return skb;
15910a708f8fSGustavo F. Padovan }
15920a708f8fSGustavo F. Padovan 
1593ab0ff76dSLuiz Augusto von Dentz static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
1594ab0ff76dSLuiz Augusto von Dentz 						struct msghdr *msg, size_t len,
159588843ab0SAndrei Emeltchenko 						u32 control, u16 sdulen)
15960a708f8fSGustavo F. Padovan {
159747d1ec61SGustavo F. Padovan 	struct sock *sk = chan->sk;
15988c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
15990a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
1600e4ca6d98SAndrei Emeltchenko 	int err, count, hlen;
16010a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
16020a708f8fSGustavo F. Padovan 
16030a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
16040a708f8fSGustavo F. Padovan 
16050a708f8fSGustavo F. Padovan 	if (!conn)
16060a708f8fSGustavo F. Padovan 		return ERR_PTR(-ENOTCONN);
16070a708f8fSGustavo F. Padovan 
1608e4ca6d98SAndrei Emeltchenko 	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1609e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_EXT_HDR_SIZE;
1610e4ca6d98SAndrei Emeltchenko 	else
1611e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_ENH_HDR_SIZE;
1612e4ca6d98SAndrei Emeltchenko 
16130a708f8fSGustavo F. Padovan 	if (sdulen)
161403a51213SAndrei Emeltchenko 		hlen += L2CAP_SDULEN_SIZE;
16150a708f8fSGustavo F. Padovan 
161647d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
161703a51213SAndrei Emeltchenko 		hlen += L2CAP_FCS_SIZE;
16180a708f8fSGustavo F. Padovan 
16190a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
16200a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
16210a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
16220a708f8fSGustavo F. Padovan 	if (!skb)
16230a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
16240a708f8fSGustavo F. Padovan 
16250a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
16260a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1627fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
16280a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
162988843ab0SAndrei Emeltchenko 
163088843ab0SAndrei Emeltchenko 	__put_control(chan, control, skb_put(skb, __ctrl_size(chan)));
163188843ab0SAndrei Emeltchenko 
16320a708f8fSGustavo F. Padovan 	if (sdulen)
163303a51213SAndrei Emeltchenko 		put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
16340a708f8fSGustavo F. Padovan 
16350a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
16360a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
16370a708f8fSGustavo F. Padovan 		kfree_skb(skb);
16380a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
16390a708f8fSGustavo F. Padovan 	}
16400a708f8fSGustavo F. Padovan 
164147d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
164203a51213SAndrei Emeltchenko 		put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE));
16430a708f8fSGustavo F. Padovan 
16440a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries = 0;
16450a708f8fSGustavo F. Padovan 	return skb;
16460a708f8fSGustavo F. Padovan }
16470a708f8fSGustavo F. Padovan 
164867c9e840SSzymon Janc static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
16490a708f8fSGustavo F. Padovan {
16500a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
16510a708f8fSGustavo F. Padovan 	struct sk_buff_head sar_queue;
165288843ab0SAndrei Emeltchenko 	u32 control;
16530a708f8fSGustavo F. Padovan 	size_t size = 0;
16540a708f8fSGustavo F. Padovan 
16550a708f8fSGustavo F. Padovan 	skb_queue_head_init(&sar_queue);
16567e0ef6eeSAndrei Emeltchenko 	control = __set_ctrl_sar(chan, L2CAP_SAR_START);
165747d1ec61SGustavo F. Padovan 	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
16580a708f8fSGustavo F. Padovan 	if (IS_ERR(skb))
16590a708f8fSGustavo F. Padovan 		return PTR_ERR(skb);
16600a708f8fSGustavo F. Padovan 
16610a708f8fSGustavo F. Padovan 	__skb_queue_tail(&sar_queue, skb);
16622c03a7a4SGustavo F. Padovan 	len -= chan->remote_mps;
16632c03a7a4SGustavo F. Padovan 	size += chan->remote_mps;
16640a708f8fSGustavo F. Padovan 
16650a708f8fSGustavo F. Padovan 	while (len > 0) {
16660a708f8fSGustavo F. Padovan 		size_t buflen;
16670a708f8fSGustavo F. Padovan 
16682c03a7a4SGustavo F. Padovan 		if (len > chan->remote_mps) {
16697e0ef6eeSAndrei Emeltchenko 			control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE);
16702c03a7a4SGustavo F. Padovan 			buflen = chan->remote_mps;
16710a708f8fSGustavo F. Padovan 		} else {
16727e0ef6eeSAndrei Emeltchenko 			control = __set_ctrl_sar(chan, L2CAP_SAR_END);
16730a708f8fSGustavo F. Padovan 			buflen = len;
16740a708f8fSGustavo F. Padovan 		}
16750a708f8fSGustavo F. Padovan 
167647d1ec61SGustavo F. Padovan 		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
16770a708f8fSGustavo F. Padovan 		if (IS_ERR(skb)) {
16780a708f8fSGustavo F. Padovan 			skb_queue_purge(&sar_queue);
16790a708f8fSGustavo F. Padovan 			return PTR_ERR(skb);
16800a708f8fSGustavo F. Padovan 		}
16810a708f8fSGustavo F. Padovan 
16820a708f8fSGustavo F. Padovan 		__skb_queue_tail(&sar_queue, skb);
16830a708f8fSGustavo F. Padovan 		len -= buflen;
16840a708f8fSGustavo F. Padovan 		size += buflen;
16850a708f8fSGustavo F. Padovan 	}
168658d35f87SGustavo F. Padovan 	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
168758d35f87SGustavo F. Padovan 	if (chan->tx_send_head == NULL)
168858d35f87SGustavo F. Padovan 		chan->tx_send_head = sar_queue.next;
16890a708f8fSGustavo F. Padovan 
16900a708f8fSGustavo F. Padovan 	return size;
16910a708f8fSGustavo F. Padovan }
16920a708f8fSGustavo F. Padovan 
16935e59b791SLuiz Augusto von Dentz int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
16945e59b791SLuiz Augusto von Dentz 								u32 priority)
16959a91a04aSGustavo F. Padovan {
16969a91a04aSGustavo F. Padovan 	struct sk_buff *skb;
169788843ab0SAndrei Emeltchenko 	u32 control;
16989a91a04aSGustavo F. Padovan 	int err;
16999a91a04aSGustavo F. Padovan 
17009a91a04aSGustavo F. Padovan 	/* Connectionless channel */
1701715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
17025e59b791SLuiz Augusto von Dentz 		skb = l2cap_create_connless_pdu(chan, msg, len, priority);
17039a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
17049a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
17059a91a04aSGustavo F. Padovan 
17069a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
17079a91a04aSGustavo F. Padovan 		return len;
17089a91a04aSGustavo F. Padovan 	}
17099a91a04aSGustavo F. Padovan 
17109a91a04aSGustavo F. Padovan 	switch (chan->mode) {
17119a91a04aSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
17129a91a04aSGustavo F. Padovan 		/* Check outgoing MTU */
17139a91a04aSGustavo F. Padovan 		if (len > chan->omtu)
17149a91a04aSGustavo F. Padovan 			return -EMSGSIZE;
17159a91a04aSGustavo F. Padovan 
17169a91a04aSGustavo F. Padovan 		/* Create a basic PDU */
17175e59b791SLuiz Augusto von Dentz 		skb = l2cap_create_basic_pdu(chan, msg, len, priority);
17189a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
17199a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
17209a91a04aSGustavo F. Padovan 
17219a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
17229a91a04aSGustavo F. Padovan 		err = len;
17239a91a04aSGustavo F. Padovan 		break;
17249a91a04aSGustavo F. Padovan 
17259a91a04aSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
17269a91a04aSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
17279a91a04aSGustavo F. Padovan 		/* Entire SDU fits into one PDU */
17289a91a04aSGustavo F. Padovan 		if (len <= chan->remote_mps) {
17297e0ef6eeSAndrei Emeltchenko 			control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED);
17309a91a04aSGustavo F. Padovan 			skb = l2cap_create_iframe_pdu(chan, msg, len, control,
17319a91a04aSGustavo F. Padovan 									0);
17329a91a04aSGustavo F. Padovan 			if (IS_ERR(skb))
17339a91a04aSGustavo F. Padovan 				return PTR_ERR(skb);
17349a91a04aSGustavo F. Padovan 
17359a91a04aSGustavo F. Padovan 			__skb_queue_tail(&chan->tx_q, skb);
17369a91a04aSGustavo F. Padovan 
17379a91a04aSGustavo F. Padovan 			if (chan->tx_send_head == NULL)
17389a91a04aSGustavo F. Padovan 				chan->tx_send_head = skb;
17399a91a04aSGustavo F. Padovan 
17409a91a04aSGustavo F. Padovan 		} else {
17419a91a04aSGustavo F. Padovan 			/* Segment SDU into multiples PDUs */
17429a91a04aSGustavo F. Padovan 			err = l2cap_sar_segment_sdu(chan, msg, len);
17439a91a04aSGustavo F. Padovan 			if (err < 0)
17449a91a04aSGustavo F. Padovan 				return err;
17459a91a04aSGustavo F. Padovan 		}
17469a91a04aSGustavo F. Padovan 
17479a91a04aSGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_STREAMING) {
17489a91a04aSGustavo F. Padovan 			l2cap_streaming_send(chan);
17499a91a04aSGustavo F. Padovan 			err = len;
17509a91a04aSGustavo F. Padovan 			break;
17519a91a04aSGustavo F. Padovan 		}
17529a91a04aSGustavo F. Padovan 
1753e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
1754e2ab4353SGustavo F. Padovan 				test_bit(CONN_WAIT_F, &chan->conn_state)) {
17559a91a04aSGustavo F. Padovan 			err = len;
17569a91a04aSGustavo F. Padovan 			break;
17579a91a04aSGustavo F. Padovan 		}
17589a91a04aSGustavo F. Padovan 
17599a91a04aSGustavo F. Padovan 		err = l2cap_ertm_send(chan);
17609a91a04aSGustavo F. Padovan 		if (err >= 0)
17619a91a04aSGustavo F. Padovan 			err = len;
17629a91a04aSGustavo F. Padovan 
17639a91a04aSGustavo F. Padovan 		break;
17649a91a04aSGustavo F. Padovan 
17659a91a04aSGustavo F. Padovan 	default:
17669a91a04aSGustavo F. Padovan 		BT_DBG("bad state %1.1x", chan->mode);
17679a91a04aSGustavo F. Padovan 		err = -EBADFD;
17689a91a04aSGustavo F. Padovan 	}
17699a91a04aSGustavo F. Padovan 
17709a91a04aSGustavo F. Padovan 	return err;
17719a91a04aSGustavo F. Padovan }
17729a91a04aSGustavo F. Padovan 
17730a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */
17740a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
17750a708f8fSGustavo F. Padovan {
17760a708f8fSGustavo F. Padovan 	struct sk_buff *nskb;
177748454079SGustavo F. Padovan 	struct l2cap_chan *chan;
17780a708f8fSGustavo F. Padovan 
17790a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
17800a708f8fSGustavo F. Padovan 
1781baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
1782baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
178348454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
1784715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_RAW)
17850a708f8fSGustavo F. Padovan 			continue;
17860a708f8fSGustavo F. Padovan 
17870a708f8fSGustavo F. Padovan 		/* Don't send frame to the socket it came from */
17880a708f8fSGustavo F. Padovan 		if (skb->sk == sk)
17890a708f8fSGustavo F. Padovan 			continue;
17900a708f8fSGustavo F. Padovan 		nskb = skb_clone(skb, GFP_ATOMIC);
17910a708f8fSGustavo F. Padovan 		if (!nskb)
17920a708f8fSGustavo F. Padovan 			continue;
17930a708f8fSGustavo F. Padovan 
179423070494SGustavo F. Padovan 		if (chan->ops->recv(chan->data, nskb))
17950a708f8fSGustavo F. Padovan 			kfree_skb(nskb);
17960a708f8fSGustavo F. Padovan 	}
1797baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
17980a708f8fSGustavo F. Padovan }
17990a708f8fSGustavo F. Padovan 
18000a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */
18010a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
18020a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data)
18030a708f8fSGustavo F. Padovan {
18040a708f8fSGustavo F. Padovan 	struct sk_buff *skb, **frag;
18050a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr *cmd;
18060a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
18070a708f8fSGustavo F. Padovan 	int len, count;
18080a708f8fSGustavo F. Padovan 
18090a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
18100a708f8fSGustavo F. Padovan 			conn, code, ident, dlen);
18110a708f8fSGustavo F. Padovan 
18120a708f8fSGustavo F. Padovan 	len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
18130a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, len);
18140a708f8fSGustavo F. Padovan 
18150a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
18160a708f8fSGustavo F. Padovan 	if (!skb)
18170a708f8fSGustavo F. Padovan 		return NULL;
18180a708f8fSGustavo F. Padovan 
18190a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
18200a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
18213300d9a9SClaudio Takahasi 
18223300d9a9SClaudio Takahasi 	if (conn->hcon->type == LE_LINK)
18233300d9a9SClaudio Takahasi 		lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
18243300d9a9SClaudio Takahasi 	else
18250a708f8fSGustavo F. Padovan 		lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
18260a708f8fSGustavo F. Padovan 
18270a708f8fSGustavo F. Padovan 	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
18280a708f8fSGustavo F. Padovan 	cmd->code  = code;
18290a708f8fSGustavo F. Padovan 	cmd->ident = ident;
18300a708f8fSGustavo F. Padovan 	cmd->len   = cpu_to_le16(dlen);
18310a708f8fSGustavo F. Padovan 
18320a708f8fSGustavo F. Padovan 	if (dlen) {
18330a708f8fSGustavo F. Padovan 		count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
18340a708f8fSGustavo F. Padovan 		memcpy(skb_put(skb, count), data, count);
18350a708f8fSGustavo F. Padovan 		data += count;
18360a708f8fSGustavo F. Padovan 	}
18370a708f8fSGustavo F. Padovan 
18380a708f8fSGustavo F. Padovan 	len -= skb->len;
18390a708f8fSGustavo F. Padovan 
18400a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
18410a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
18420a708f8fSGustavo F. Padovan 	while (len) {
18430a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
18440a708f8fSGustavo F. Padovan 
18450a708f8fSGustavo F. Padovan 		*frag = bt_skb_alloc(count, GFP_ATOMIC);
18460a708f8fSGustavo F. Padovan 		if (!*frag)
18470a708f8fSGustavo F. Padovan 			goto fail;
18480a708f8fSGustavo F. Padovan 
18490a708f8fSGustavo F. Padovan 		memcpy(skb_put(*frag, count), data, count);
18500a708f8fSGustavo F. Padovan 
18510a708f8fSGustavo F. Padovan 		len  -= count;
18520a708f8fSGustavo F. Padovan 		data += count;
18530a708f8fSGustavo F. Padovan 
18540a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
18550a708f8fSGustavo F. Padovan 	}
18560a708f8fSGustavo F. Padovan 
18570a708f8fSGustavo F. Padovan 	return skb;
18580a708f8fSGustavo F. Padovan 
18590a708f8fSGustavo F. Padovan fail:
18600a708f8fSGustavo F. Padovan 	kfree_skb(skb);
18610a708f8fSGustavo F. Padovan 	return NULL;
18620a708f8fSGustavo F. Padovan }
18630a708f8fSGustavo F. Padovan 
18640a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
18650a708f8fSGustavo F. Padovan {
18660a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
18670a708f8fSGustavo F. Padovan 	int len;
18680a708f8fSGustavo F. Padovan 
18690a708f8fSGustavo F. Padovan 	len = L2CAP_CONF_OPT_SIZE + opt->len;
18700a708f8fSGustavo F. Padovan 	*ptr += len;
18710a708f8fSGustavo F. Padovan 
18720a708f8fSGustavo F. Padovan 	*type = opt->type;
18730a708f8fSGustavo F. Padovan 	*olen = opt->len;
18740a708f8fSGustavo F. Padovan 
18750a708f8fSGustavo F. Padovan 	switch (opt->len) {
18760a708f8fSGustavo F. Padovan 	case 1:
18770a708f8fSGustavo F. Padovan 		*val = *((u8 *) opt->val);
18780a708f8fSGustavo F. Padovan 		break;
18790a708f8fSGustavo F. Padovan 
18800a708f8fSGustavo F. Padovan 	case 2:
18810a708f8fSGustavo F. Padovan 		*val = get_unaligned_le16(opt->val);
18820a708f8fSGustavo F. Padovan 		break;
18830a708f8fSGustavo F. Padovan 
18840a708f8fSGustavo F. Padovan 	case 4:
18850a708f8fSGustavo F. Padovan 		*val = get_unaligned_le32(opt->val);
18860a708f8fSGustavo F. Padovan 		break;
18870a708f8fSGustavo F. Padovan 
18880a708f8fSGustavo F. Padovan 	default:
18890a708f8fSGustavo F. Padovan 		*val = (unsigned long) opt->val;
18900a708f8fSGustavo F. Padovan 		break;
18910a708f8fSGustavo F. Padovan 	}
18920a708f8fSGustavo F. Padovan 
18930a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
18940a708f8fSGustavo F. Padovan 	return len;
18950a708f8fSGustavo F. Padovan }
18960a708f8fSGustavo F. Padovan 
18970a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
18980a708f8fSGustavo F. Padovan {
18990a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
19000a708f8fSGustavo F. Padovan 
19010a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
19020a708f8fSGustavo F. Padovan 
19030a708f8fSGustavo F. Padovan 	opt->type = type;
19040a708f8fSGustavo F. Padovan 	opt->len  = len;
19050a708f8fSGustavo F. Padovan 
19060a708f8fSGustavo F. Padovan 	switch (len) {
19070a708f8fSGustavo F. Padovan 	case 1:
19080a708f8fSGustavo F. Padovan 		*((u8 *) opt->val)  = val;
19090a708f8fSGustavo F. Padovan 		break;
19100a708f8fSGustavo F. Padovan 
19110a708f8fSGustavo F. Padovan 	case 2:
19120a708f8fSGustavo F. Padovan 		put_unaligned_le16(val, opt->val);
19130a708f8fSGustavo F. Padovan 		break;
19140a708f8fSGustavo F. Padovan 
19150a708f8fSGustavo F. Padovan 	case 4:
19160a708f8fSGustavo F. Padovan 		put_unaligned_le32(val, opt->val);
19170a708f8fSGustavo F. Padovan 		break;
19180a708f8fSGustavo F. Padovan 
19190a708f8fSGustavo F. Padovan 	default:
19200a708f8fSGustavo F. Padovan 		memcpy(opt->val, (void *) val, len);
19210a708f8fSGustavo F. Padovan 		break;
19220a708f8fSGustavo F. Padovan 	}
19230a708f8fSGustavo F. Padovan 
19240a708f8fSGustavo F. Padovan 	*ptr += L2CAP_CONF_OPT_SIZE + len;
19250a708f8fSGustavo F. Padovan }
19260a708f8fSGustavo F. Padovan 
1927f89cef09SAndrei Emeltchenko static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
1928f89cef09SAndrei Emeltchenko {
1929f89cef09SAndrei Emeltchenko 	struct l2cap_conf_efs efs;
1930f89cef09SAndrei Emeltchenko 
1931f89cef09SAndrei Emeltchenko 	switch (chan->mode) {
1932f89cef09SAndrei Emeltchenko 	case L2CAP_MODE_ERTM:
1933f89cef09SAndrei Emeltchenko 		efs.id		= chan->local_id;
1934f89cef09SAndrei Emeltchenko 		efs.stype	= chan->local_stype;
1935f89cef09SAndrei Emeltchenko 		efs.msdu	= cpu_to_le16(chan->local_msdu);
1936f89cef09SAndrei Emeltchenko 		efs.sdu_itime	= cpu_to_le32(chan->local_sdu_itime);
1937f89cef09SAndrei Emeltchenko 		efs.acc_lat	= cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
1938f89cef09SAndrei Emeltchenko 		efs.flush_to	= cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO);
1939f89cef09SAndrei Emeltchenko 		break;
1940f89cef09SAndrei Emeltchenko 
1941f89cef09SAndrei Emeltchenko 	case L2CAP_MODE_STREAMING:
1942f89cef09SAndrei Emeltchenko 		efs.id		= 1;
1943f89cef09SAndrei Emeltchenko 		efs.stype	= L2CAP_SERV_BESTEFFORT;
1944f89cef09SAndrei Emeltchenko 		efs.msdu	= cpu_to_le16(chan->local_msdu);
1945f89cef09SAndrei Emeltchenko 		efs.sdu_itime	= cpu_to_le32(chan->local_sdu_itime);
1946f89cef09SAndrei Emeltchenko 		efs.acc_lat	= 0;
1947f89cef09SAndrei Emeltchenko 		efs.flush_to	= 0;
1948f89cef09SAndrei Emeltchenko 		break;
1949f89cef09SAndrei Emeltchenko 
1950f89cef09SAndrei Emeltchenko 	default:
1951f89cef09SAndrei Emeltchenko 		return;
1952f89cef09SAndrei Emeltchenko 	}
1953f89cef09SAndrei Emeltchenko 
1954f89cef09SAndrei Emeltchenko 	l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
1955f89cef09SAndrei Emeltchenko 							(unsigned long) &efs);
1956f89cef09SAndrei Emeltchenko }
1957f89cef09SAndrei Emeltchenko 
19580a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg)
19590a708f8fSGustavo F. Padovan {
1960525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
19610a708f8fSGustavo F. Padovan 
1962525cd185SGustavo F. Padovan 	bh_lock_sock(chan->sk);
1963525cd185SGustavo F. Padovan 	l2cap_send_ack(chan);
1964525cd185SGustavo F. Padovan 	bh_unlock_sock(chan->sk);
19650a708f8fSGustavo F. Padovan }
19660a708f8fSGustavo F. Padovan 
1967525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan)
19680a708f8fSGustavo F. Padovan {
1969525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
1970525cd185SGustavo F. Padovan 
197142e5c802SGustavo F. Padovan 	chan->expected_ack_seq = 0;
19726a026610SGustavo F. Padovan 	chan->unacked_frames = 0;
197342e5c802SGustavo F. Padovan 	chan->buffer_seq = 0;
19746a026610SGustavo F. Padovan 	chan->num_acked = 0;
19756a026610SGustavo F. Padovan 	chan->frames_sent = 0;
19760a708f8fSGustavo F. Padovan 
1977e92c8e70SGustavo F. Padovan 	setup_timer(&chan->retrans_timer, l2cap_retrans_timeout,
1978e92c8e70SGustavo F. Padovan 							(unsigned long) chan);
1979e92c8e70SGustavo F. Padovan 	setup_timer(&chan->monitor_timer, l2cap_monitor_timeout,
1980e92c8e70SGustavo F. Padovan 							(unsigned long) chan);
1981e92c8e70SGustavo F. Padovan 	setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan);
19820a708f8fSGustavo F. Padovan 
1983f1c6775bSGustavo F. Padovan 	skb_queue_head_init(&chan->srej_q);
19840a708f8fSGustavo F. Padovan 
198539d5a3eeSGustavo F. Padovan 	INIT_LIST_HEAD(&chan->srej_l);
198639d5a3eeSGustavo F. Padovan 
19870a708f8fSGustavo F. Padovan 
19880a708f8fSGustavo F. Padovan 	sk->sk_backlog_rcv = l2cap_ertm_data_rcv;
19890a708f8fSGustavo F. Padovan }
19900a708f8fSGustavo F. Padovan 
19910a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
19920a708f8fSGustavo F. Padovan {
19930a708f8fSGustavo F. Padovan 	switch (mode) {
19940a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
19950a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
19960a708f8fSGustavo F. Padovan 		if (l2cap_mode_supported(mode, remote_feat_mask))
19970a708f8fSGustavo F. Padovan 			return mode;
19980a708f8fSGustavo F. Padovan 		/* fall through */
19990a708f8fSGustavo F. Padovan 	default:
20000a708f8fSGustavo F. Padovan 		return L2CAP_MODE_BASIC;
20010a708f8fSGustavo F. Padovan 	}
20020a708f8fSGustavo F. Padovan }
20030a708f8fSGustavo F. Padovan 
20046327eb98SAndrei Emeltchenko static inline bool __l2cap_ews_supported(struct l2cap_chan *chan)
20056327eb98SAndrei Emeltchenko {
20066327eb98SAndrei Emeltchenko 	return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
20076327eb98SAndrei Emeltchenko }
20086327eb98SAndrei Emeltchenko 
2009f89cef09SAndrei Emeltchenko static inline bool __l2cap_efs_supported(struct l2cap_chan *chan)
2010f89cef09SAndrei Emeltchenko {
2011f89cef09SAndrei Emeltchenko 	return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
2012f89cef09SAndrei Emeltchenko }
2013f89cef09SAndrei Emeltchenko 
20146327eb98SAndrei Emeltchenko static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
20156327eb98SAndrei Emeltchenko {
20166327eb98SAndrei Emeltchenko 	if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
2017836be934SAndrei Emeltchenko 						__l2cap_ews_supported(chan)) {
20186327eb98SAndrei Emeltchenko 		/* use extended control field */
20196327eb98SAndrei Emeltchenko 		set_bit(FLAG_EXT_CTRL, &chan->flags);
2020836be934SAndrei Emeltchenko 		chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
2021836be934SAndrei Emeltchenko 	} else {
20226327eb98SAndrei Emeltchenko 		chan->tx_win = min_t(u16, chan->tx_win,
20236327eb98SAndrei Emeltchenko 						L2CAP_DEFAULT_TX_WINDOW);
2024836be934SAndrei Emeltchenko 		chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
2025836be934SAndrei Emeltchenko 	}
20266327eb98SAndrei Emeltchenko }
20276327eb98SAndrei Emeltchenko 
2028710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
20290a708f8fSGustavo F. Padovan {
20300a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
20310c1bc5c6SGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
20320a708f8fSGustavo F. Padovan 	void *ptr = req->data;
2033c8f79162SAndrei Emeltchenko 	u16 size;
20340a708f8fSGustavo F. Padovan 
203549208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
20360a708f8fSGustavo F. Padovan 
203773ffa904SGustavo F. Padovan 	if (chan->num_conf_req || chan->num_conf_rsp)
20380a708f8fSGustavo F. Padovan 		goto done;
20390a708f8fSGustavo F. Padovan 
20400c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
20410a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
20420a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
2043c1360a1cSGustavo F. Padovan 		if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
20440a708f8fSGustavo F. Padovan 			break;
20450a708f8fSGustavo F. Padovan 
2046f89cef09SAndrei Emeltchenko 		if (__l2cap_efs_supported(chan))
2047f89cef09SAndrei Emeltchenko 			set_bit(FLAG_EFS_ENABLE, &chan->flags);
2048f89cef09SAndrei Emeltchenko 
20490a708f8fSGustavo F. Padovan 		/* fall through */
20500a708f8fSGustavo F. Padovan 	default:
20518c1d787bSGustavo F. Padovan 		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
20520a708f8fSGustavo F. Padovan 		break;
20530a708f8fSGustavo F. Padovan 	}
20540a708f8fSGustavo F. Padovan 
20550a708f8fSGustavo F. Padovan done:
20560c1bc5c6SGustavo F. Padovan 	if (chan->imtu != L2CAP_DEFAULT_MTU)
20570c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
20580a708f8fSGustavo F. Padovan 
20590c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
20600a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
20618c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
20628c1d787bSGustavo F. Padovan 				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
20630a708f8fSGustavo F. Padovan 			break;
20640a708f8fSGustavo F. Padovan 
20650a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_BASIC;
20660a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
20670a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
20680a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
20690a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
20700a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = 0;
20710a708f8fSGustavo F. Padovan 
20720a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
20730a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
20740a708f8fSGustavo F. Padovan 		break;
20750a708f8fSGustavo F. Padovan 
20760a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
20770a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_ERTM;
207847d1ec61SGustavo F. Padovan 		rfc.max_transmit    = chan->max_tx;
20790a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
20800a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
2081c8f79162SAndrei Emeltchenko 
2082c8f79162SAndrei Emeltchenko 		size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
2083c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2084c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2085c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2086c8f79162SAndrei Emeltchenko 		rfc.max_pdu_size = cpu_to_le16(size);
20870a708f8fSGustavo F. Padovan 
20886327eb98SAndrei Emeltchenko 		l2cap_txwin_setup(chan);
20896327eb98SAndrei Emeltchenko 
20906327eb98SAndrei Emeltchenko 		rfc.txwin_size = min_t(u16, chan->tx_win,
20916327eb98SAndrei Emeltchenko 						L2CAP_DEFAULT_TX_WINDOW);
20920a708f8fSGustavo F. Padovan 
20930a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
20940a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
20950a708f8fSGustavo F. Padovan 
2096f89cef09SAndrei Emeltchenko 		if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
2097f89cef09SAndrei Emeltchenko 			l2cap_add_opt_efs(&ptr, chan);
2098f89cef09SAndrei Emeltchenko 
20998c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
21000a708f8fSGustavo F. Padovan 			break;
21010a708f8fSGustavo F. Padovan 
210247d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
2103c1360a1cSGustavo F. Padovan 				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
210447d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
210547d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
21060a708f8fSGustavo F. Padovan 		}
21076327eb98SAndrei Emeltchenko 
21086327eb98SAndrei Emeltchenko 		if (test_bit(FLAG_EXT_CTRL, &chan->flags))
21096327eb98SAndrei Emeltchenko 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
21106327eb98SAndrei Emeltchenko 								chan->tx_win);
21110a708f8fSGustavo F. Padovan 		break;
21120a708f8fSGustavo F. Padovan 
21130a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
21140a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_STREAMING;
21150a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
21160a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
21170a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
21180a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
2119c8f79162SAndrei Emeltchenko 
2120c8f79162SAndrei Emeltchenko 		size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
2121c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2122c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2123c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2124c8f79162SAndrei Emeltchenko 		rfc.max_pdu_size = cpu_to_le16(size);
21250a708f8fSGustavo F. Padovan 
21260a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
21270a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
21280a708f8fSGustavo F. Padovan 
2129f89cef09SAndrei Emeltchenko 		if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
2130f89cef09SAndrei Emeltchenko 			l2cap_add_opt_efs(&ptr, chan);
2131f89cef09SAndrei Emeltchenko 
21328c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
21330a708f8fSGustavo F. Padovan 			break;
21340a708f8fSGustavo F. Padovan 
213547d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
2136c1360a1cSGustavo F. Padovan 				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
213747d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
213847d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
21390a708f8fSGustavo F. Padovan 		}
21400a708f8fSGustavo F. Padovan 		break;
21410a708f8fSGustavo F. Padovan 	}
21420a708f8fSGustavo F. Padovan 
2143fe4128e0SGustavo F. Padovan 	req->dcid  = cpu_to_le16(chan->dcid);
21440a708f8fSGustavo F. Padovan 	req->flags = cpu_to_le16(0);
21450a708f8fSGustavo F. Padovan 
21460a708f8fSGustavo F. Padovan 	return ptr - data;
21470a708f8fSGustavo F. Padovan }
21480a708f8fSGustavo F. Padovan 
214973ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
21500a708f8fSGustavo F. Padovan {
21510a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
21520a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
215373ffa904SGustavo F. Padovan 	void *req = chan->conf_req;
215473ffa904SGustavo F. Padovan 	int len = chan->conf_len;
21550a708f8fSGustavo F. Padovan 	int type, hint, olen;
21560a708f8fSGustavo F. Padovan 	unsigned long val;
21570a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
215842dceae2SAndrei Emeltchenko 	struct l2cap_conf_efs efs;
215942dceae2SAndrei Emeltchenko 	u8 remote_efs = 0;
21600a708f8fSGustavo F. Padovan 	u16 mtu = L2CAP_DEFAULT_MTU;
21610a708f8fSGustavo F. Padovan 	u16 result = L2CAP_CONF_SUCCESS;
2162c8f79162SAndrei Emeltchenko 	u16 size;
21630a708f8fSGustavo F. Padovan 
216473ffa904SGustavo F. Padovan 	BT_DBG("chan %p", chan);
21650a708f8fSGustavo F. Padovan 
21660a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
21670a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
21680a708f8fSGustavo F. Padovan 
21690a708f8fSGustavo F. Padovan 		hint  = type & L2CAP_CONF_HINT;
21700a708f8fSGustavo F. Padovan 		type &= L2CAP_CONF_MASK;
21710a708f8fSGustavo F. Padovan 
21720a708f8fSGustavo F. Padovan 		switch (type) {
21730a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
21740a708f8fSGustavo F. Padovan 			mtu = val;
21750a708f8fSGustavo F. Padovan 			break;
21760a708f8fSGustavo F. Padovan 
21770a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
21780c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
21790a708f8fSGustavo F. Padovan 			break;
21800a708f8fSGustavo F. Padovan 
21810a708f8fSGustavo F. Padovan 		case L2CAP_CONF_QOS:
21820a708f8fSGustavo F. Padovan 			break;
21830a708f8fSGustavo F. Padovan 
21840a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
21850a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
21860a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *) val, olen);
21870a708f8fSGustavo F. Padovan 			break;
21880a708f8fSGustavo F. Padovan 
21890a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FCS:
21900a708f8fSGustavo F. Padovan 			if (val == L2CAP_FCS_NONE)
2191c1360a1cSGustavo F. Padovan 				set_bit(CONF_NO_FCS_RECV, &chan->conf_state);
219242dceae2SAndrei Emeltchenko 			break;
21930a708f8fSGustavo F. Padovan 
219442dceae2SAndrei Emeltchenko 		case L2CAP_CONF_EFS:
219542dceae2SAndrei Emeltchenko 			remote_efs = 1;
219642dceae2SAndrei Emeltchenko 			if (olen == sizeof(efs))
219742dceae2SAndrei Emeltchenko 				memcpy(&efs, (void *) val, olen);
21980a708f8fSGustavo F. Padovan 			break;
21990a708f8fSGustavo F. Padovan 
22006327eb98SAndrei Emeltchenko 		case L2CAP_CONF_EWS:
22016327eb98SAndrei Emeltchenko 			if (!enable_hs)
22026327eb98SAndrei Emeltchenko 				return -ECONNREFUSED;
22036327eb98SAndrei Emeltchenko 
22046327eb98SAndrei Emeltchenko 			set_bit(FLAG_EXT_CTRL, &chan->flags);
22056327eb98SAndrei Emeltchenko 			set_bit(CONF_EWS_RECV, &chan->conf_state);
2206836be934SAndrei Emeltchenko 			chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
22076327eb98SAndrei Emeltchenko 			chan->remote_tx_win = val;
22080a708f8fSGustavo F. Padovan 			break;
22090a708f8fSGustavo F. Padovan 
22100a708f8fSGustavo F. Padovan 		default:
22110a708f8fSGustavo F. Padovan 			if (hint)
22120a708f8fSGustavo F. Padovan 				break;
22130a708f8fSGustavo F. Padovan 
22140a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNKNOWN;
22150a708f8fSGustavo F. Padovan 			*((u8 *) ptr++) = type;
22160a708f8fSGustavo F. Padovan 			break;
22170a708f8fSGustavo F. Padovan 		}
22180a708f8fSGustavo F. Padovan 	}
22190a708f8fSGustavo F. Padovan 
222073ffa904SGustavo F. Padovan 	if (chan->num_conf_rsp || chan->num_conf_req > 1)
22210a708f8fSGustavo F. Padovan 		goto done;
22220a708f8fSGustavo F. Padovan 
22230c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
22240a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
22250a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
2226c1360a1cSGustavo F. Padovan 		if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
22270c1bc5c6SGustavo F. Padovan 			chan->mode = l2cap_select_mode(rfc.mode,
22288c1d787bSGustavo F. Padovan 					chan->conn->feat_mask);
22290a708f8fSGustavo F. Padovan 			break;
22300a708f8fSGustavo F. Padovan 		}
22310a708f8fSGustavo F. Padovan 
223242dceae2SAndrei Emeltchenko 		if (remote_efs) {
223342dceae2SAndrei Emeltchenko 			if (__l2cap_efs_supported(chan))
223442dceae2SAndrei Emeltchenko 				set_bit(FLAG_EFS_ENABLE, &chan->flags);
223542dceae2SAndrei Emeltchenko 			else
223642dceae2SAndrei Emeltchenko 				return -ECONNREFUSED;
223742dceae2SAndrei Emeltchenko 		}
223842dceae2SAndrei Emeltchenko 
22390c1bc5c6SGustavo F. Padovan 		if (chan->mode != rfc.mode)
22400a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
22410a708f8fSGustavo F. Padovan 
22420a708f8fSGustavo F. Padovan 		break;
22430a708f8fSGustavo F. Padovan 	}
22440a708f8fSGustavo F. Padovan 
22450a708f8fSGustavo F. Padovan done:
22460c1bc5c6SGustavo F. Padovan 	if (chan->mode != rfc.mode) {
22470a708f8fSGustavo F. Padovan 		result = L2CAP_CONF_UNACCEPT;
22480c1bc5c6SGustavo F. Padovan 		rfc.mode = chan->mode;
22490a708f8fSGustavo F. Padovan 
225073ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp == 1)
22510a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
22520a708f8fSGustavo F. Padovan 
22530a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
22540a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
22550a708f8fSGustavo F. Padovan 	}
22560a708f8fSGustavo F. Padovan 
22570a708f8fSGustavo F. Padovan 	if (result == L2CAP_CONF_SUCCESS) {
22580a708f8fSGustavo F. Padovan 		/* Configure output options and let the other side know
22590a708f8fSGustavo F. Padovan 		 * which ones we don't like. */
22600a708f8fSGustavo F. Padovan 
22610a708f8fSGustavo F. Padovan 		if (mtu < L2CAP_DEFAULT_MIN_MTU)
22620a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
22630a708f8fSGustavo F. Padovan 		else {
22640c1bc5c6SGustavo F. Padovan 			chan->omtu = mtu;
2265c1360a1cSGustavo F. Padovan 			set_bit(CONF_MTU_DONE, &chan->conf_state);
22660a708f8fSGustavo F. Padovan 		}
22670c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
22680a708f8fSGustavo F. Padovan 
226942dceae2SAndrei Emeltchenko 		if (remote_efs) {
227042dceae2SAndrei Emeltchenko 			if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
227142dceae2SAndrei Emeltchenko 					efs.stype != L2CAP_SERV_NOTRAFIC &&
227242dceae2SAndrei Emeltchenko 					efs.stype != chan->local_stype) {
227342dceae2SAndrei Emeltchenko 
227442dceae2SAndrei Emeltchenko 				result = L2CAP_CONF_UNACCEPT;
227542dceae2SAndrei Emeltchenko 
227642dceae2SAndrei Emeltchenko 				if (chan->num_conf_req >= 1)
227742dceae2SAndrei Emeltchenko 					return -ECONNREFUSED;
227842dceae2SAndrei Emeltchenko 
227942dceae2SAndrei Emeltchenko 				l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
228042dceae2SAndrei Emeltchenko 							sizeof(efs),
228142dceae2SAndrei Emeltchenko 							(unsigned long) &efs);
22820e8b207eSAndrei Emeltchenko 			} else {
22833e6b3b95SGustavo F. Padovan 				/* Send PENDING Conf Rsp */
22840e8b207eSAndrei Emeltchenko 				result = L2CAP_CONF_PENDING;
22850e8b207eSAndrei Emeltchenko 				set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
228642dceae2SAndrei Emeltchenko 			}
228742dceae2SAndrei Emeltchenko 		}
228842dceae2SAndrei Emeltchenko 
22890a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
22900a708f8fSGustavo F. Padovan 		case L2CAP_MODE_BASIC:
229147d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
2292c1360a1cSGustavo F. Padovan 			set_bit(CONF_MODE_DONE, &chan->conf_state);
22930a708f8fSGustavo F. Padovan 			break;
22940a708f8fSGustavo F. Padovan 
22950a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
22966327eb98SAndrei Emeltchenko 			if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
22972c03a7a4SGustavo F. Padovan 				chan->remote_tx_win = rfc.txwin_size;
22986327eb98SAndrei Emeltchenko 			else
22996327eb98SAndrei Emeltchenko 				rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
23006327eb98SAndrei Emeltchenko 
23012c03a7a4SGustavo F. Padovan 			chan->remote_max_tx = rfc.max_transmit;
23020a708f8fSGustavo F. Padovan 
2303c8f79162SAndrei Emeltchenko 			size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
2304c8f79162SAndrei Emeltchenko 						chan->conn->mtu -
2305c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2306c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2307c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2308c8f79162SAndrei Emeltchenko 			rfc.max_pdu_size = cpu_to_le16(size);
2309c8f79162SAndrei Emeltchenko 			chan->remote_mps = size;
23100a708f8fSGustavo F. Padovan 
23110a708f8fSGustavo F. Padovan 			rfc.retrans_timeout =
23120a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
23130a708f8fSGustavo F. Padovan 			rfc.monitor_timeout =
23140a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
23150a708f8fSGustavo F. Padovan 
2316c1360a1cSGustavo F. Padovan 			set_bit(CONF_MODE_DONE, &chan->conf_state);
23170a708f8fSGustavo F. Padovan 
23180a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
23190a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
23200a708f8fSGustavo F. Padovan 
232142dceae2SAndrei Emeltchenko 			if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
232242dceae2SAndrei Emeltchenko 				chan->remote_id = efs.id;
232342dceae2SAndrei Emeltchenko 				chan->remote_stype = efs.stype;
232442dceae2SAndrei Emeltchenko 				chan->remote_msdu = le16_to_cpu(efs.msdu);
232542dceae2SAndrei Emeltchenko 				chan->remote_flush_to =
232642dceae2SAndrei Emeltchenko 						le32_to_cpu(efs.flush_to);
232742dceae2SAndrei Emeltchenko 				chan->remote_acc_lat =
232842dceae2SAndrei Emeltchenko 						le32_to_cpu(efs.acc_lat);
232942dceae2SAndrei Emeltchenko 				chan->remote_sdu_itime =
233042dceae2SAndrei Emeltchenko 					le32_to_cpu(efs.sdu_itime);
233142dceae2SAndrei Emeltchenko 				l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
233242dceae2SAndrei Emeltchenko 					sizeof(efs), (unsigned long) &efs);
233342dceae2SAndrei Emeltchenko 			}
23340a708f8fSGustavo F. Padovan 			break;
23350a708f8fSGustavo F. Padovan 
23360a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
2337c8f79162SAndrei Emeltchenko 			size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
2338c8f79162SAndrei Emeltchenko 						chan->conn->mtu -
2339c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2340c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2341c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2342c8f79162SAndrei Emeltchenko 			rfc.max_pdu_size = cpu_to_le16(size);
2343c8f79162SAndrei Emeltchenko 			chan->remote_mps = size;
23440a708f8fSGustavo F. Padovan 
2345c1360a1cSGustavo F. Padovan 			set_bit(CONF_MODE_DONE, &chan->conf_state);
23460a708f8fSGustavo F. Padovan 
23470a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
23480a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
23490a708f8fSGustavo F. Padovan 
23500a708f8fSGustavo F. Padovan 			break;
23510a708f8fSGustavo F. Padovan 
23520a708f8fSGustavo F. Padovan 		default:
23530a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
23540a708f8fSGustavo F. Padovan 
23550a708f8fSGustavo F. Padovan 			memset(&rfc, 0, sizeof(rfc));
23560c1bc5c6SGustavo F. Padovan 			rfc.mode = chan->mode;
23570a708f8fSGustavo F. Padovan 		}
23580a708f8fSGustavo F. Padovan 
23590a708f8fSGustavo F. Padovan 		if (result == L2CAP_CONF_SUCCESS)
2360c1360a1cSGustavo F. Padovan 			set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
23610a708f8fSGustavo F. Padovan 	}
2362fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
23630a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
23640a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(0x0000);
23650a708f8fSGustavo F. Padovan 
23660a708f8fSGustavo F. Padovan 	return ptr - data;
23670a708f8fSGustavo F. Padovan }
23680a708f8fSGustavo F. Padovan 
2369b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
23700a708f8fSGustavo F. Padovan {
23710a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
23720a708f8fSGustavo F. Padovan 	void *ptr = req->data;
23730a708f8fSGustavo F. Padovan 	int type, olen;
23740a708f8fSGustavo F. Padovan 	unsigned long val;
23750a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
237666af7aafSAndrei Emeltchenko 	struct l2cap_conf_efs efs;
23770a708f8fSGustavo F. Padovan 
2378fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
23790a708f8fSGustavo F. Padovan 
23800a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
23810a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
23820a708f8fSGustavo F. Padovan 
23830a708f8fSGustavo F. Padovan 		switch (type) {
23840a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
23850a708f8fSGustavo F. Padovan 			if (val < L2CAP_DEFAULT_MIN_MTU) {
23860a708f8fSGustavo F. Padovan 				*result = L2CAP_CONF_UNACCEPT;
23870c1bc5c6SGustavo F. Padovan 				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
23880a708f8fSGustavo F. Padovan 			} else
23890c1bc5c6SGustavo F. Padovan 				chan->imtu = val;
23900c1bc5c6SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
23910a708f8fSGustavo F. Padovan 			break;
23920a708f8fSGustavo F. Padovan 
23930a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
23940c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
23950a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
23960c1bc5c6SGustavo F. Padovan 							2, chan->flush_to);
23970a708f8fSGustavo F. Padovan 			break;
23980a708f8fSGustavo F. Padovan 
23990a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
24000a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
24010a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
24020a708f8fSGustavo F. Padovan 
2403c1360a1cSGustavo F. Padovan 			if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
24040c1bc5c6SGustavo F. Padovan 							rfc.mode != chan->mode)
24050a708f8fSGustavo F. Padovan 				return -ECONNREFUSED;
24060a708f8fSGustavo F. Padovan 
240747d1ec61SGustavo F. Padovan 			chan->fcs = 0;
24080a708f8fSGustavo F. Padovan 
24090a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
24100a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
24110a708f8fSGustavo F. Padovan 			break;
24126327eb98SAndrei Emeltchenko 
24136327eb98SAndrei Emeltchenko 		case L2CAP_CONF_EWS:
24146327eb98SAndrei Emeltchenko 			chan->tx_win = min_t(u16, val,
24156327eb98SAndrei Emeltchenko 						L2CAP_DEFAULT_EXT_WINDOW);
24163e6b3b95SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
24173e6b3b95SGustavo F. Padovan 							chan->tx_win);
24186327eb98SAndrei Emeltchenko 			break;
241966af7aafSAndrei Emeltchenko 
242066af7aafSAndrei Emeltchenko 		case L2CAP_CONF_EFS:
242166af7aafSAndrei Emeltchenko 			if (olen == sizeof(efs))
242266af7aafSAndrei Emeltchenko 				memcpy(&efs, (void *)val, olen);
242366af7aafSAndrei Emeltchenko 
242466af7aafSAndrei Emeltchenko 			if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
242566af7aafSAndrei Emeltchenko 					efs.stype != L2CAP_SERV_NOTRAFIC &&
242666af7aafSAndrei Emeltchenko 					efs.stype != chan->local_stype)
242766af7aafSAndrei Emeltchenko 				return -ECONNREFUSED;
242866af7aafSAndrei Emeltchenko 
242966af7aafSAndrei Emeltchenko 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
243066af7aafSAndrei Emeltchenko 					sizeof(efs), (unsigned long) &efs);
243166af7aafSAndrei Emeltchenko 			break;
24320a708f8fSGustavo F. Padovan 		}
24330a708f8fSGustavo F. Padovan 	}
24340a708f8fSGustavo F. Padovan 
24350c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
24360a708f8fSGustavo F. Padovan 		return -ECONNREFUSED;
24370a708f8fSGustavo F. Padovan 
24380c1bc5c6SGustavo F. Padovan 	chan->mode = rfc.mode;
24390a708f8fSGustavo F. Padovan 
24400e8b207eSAndrei Emeltchenko 	if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
24410a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
24420a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
244347d1ec61SGustavo F. Padovan 			chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
244447d1ec61SGustavo F. Padovan 			chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
244547d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
244666af7aafSAndrei Emeltchenko 
244766af7aafSAndrei Emeltchenko 			if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
244866af7aafSAndrei Emeltchenko 				chan->local_msdu = le16_to_cpu(efs.msdu);
244966af7aafSAndrei Emeltchenko 				chan->local_sdu_itime =
245066af7aafSAndrei Emeltchenko 						le32_to_cpu(efs.sdu_itime);
245166af7aafSAndrei Emeltchenko 				chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
245266af7aafSAndrei Emeltchenko 				chan->local_flush_to =
245366af7aafSAndrei Emeltchenko 						le32_to_cpu(efs.flush_to);
245466af7aafSAndrei Emeltchenko 			}
24550a708f8fSGustavo F. Padovan 			break;
245666af7aafSAndrei Emeltchenko 
24570a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
245847d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
24590a708f8fSGustavo F. Padovan 		}
24600a708f8fSGustavo F. Padovan 	}
24610a708f8fSGustavo F. Padovan 
2462fe4128e0SGustavo F. Padovan 	req->dcid   = cpu_to_le16(chan->dcid);
24630a708f8fSGustavo F. Padovan 	req->flags  = cpu_to_le16(0x0000);
24640a708f8fSGustavo F. Padovan 
24650a708f8fSGustavo F. Padovan 	return ptr - data;
24660a708f8fSGustavo F. Padovan }
24670a708f8fSGustavo F. Padovan 
2468fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
24690a708f8fSGustavo F. Padovan {
24700a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
24710a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
24720a708f8fSGustavo F. Padovan 
2473fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p", chan);
24740a708f8fSGustavo F. Padovan 
2475fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
24760a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
24770a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(flags);
24780a708f8fSGustavo F. Padovan 
24790a708f8fSGustavo F. Padovan 	return ptr - data;
24800a708f8fSGustavo F. Padovan }
24810a708f8fSGustavo F. Padovan 
24828c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
2483710f9b0aSGustavo F. Padovan {
2484710f9b0aSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
24858c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
2486710f9b0aSGustavo F. Padovan 	u8 buf[128];
2487710f9b0aSGustavo F. Padovan 
2488fe4128e0SGustavo F. Padovan 	rsp.scid   = cpu_to_le16(chan->dcid);
2489fe4128e0SGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(chan->scid);
2490710f9b0aSGustavo F. Padovan 	rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
2491710f9b0aSGustavo F. Padovan 	rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
2492710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, chan->ident,
2493710f9b0aSGustavo F. Padovan 				L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2494710f9b0aSGustavo F. Padovan 
2495c1360a1cSGustavo F. Padovan 	if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
2496710f9b0aSGustavo F. Padovan 		return;
2497710f9b0aSGustavo F. Padovan 
2498710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2499710f9b0aSGustavo F. Padovan 			l2cap_build_conf_req(chan, buf), buf);
2500710f9b0aSGustavo F. Padovan 	chan->num_conf_req++;
2501710f9b0aSGustavo F. Padovan }
2502710f9b0aSGustavo F. Padovan 
250347d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
25040a708f8fSGustavo F. Padovan {
25050a708f8fSGustavo F. Padovan 	int type, olen;
25060a708f8fSGustavo F. Padovan 	unsigned long val;
25070a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
25080a708f8fSGustavo F. Padovan 
250947d1ec61SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
25100a708f8fSGustavo F. Padovan 
25110c1bc5c6SGustavo F. Padovan 	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
25120a708f8fSGustavo F. Padovan 		return;
25130a708f8fSGustavo F. Padovan 
25140a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
25150a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
25160a708f8fSGustavo F. Padovan 
25170a708f8fSGustavo F. Padovan 		switch (type) {
25180a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
25190a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
25200a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
25210a708f8fSGustavo F. Padovan 			goto done;
25220a708f8fSGustavo F. Padovan 		}
25230a708f8fSGustavo F. Padovan 	}
25240a708f8fSGustavo F. Padovan 
25250a708f8fSGustavo F. Padovan done:
25260a708f8fSGustavo F. Padovan 	switch (rfc.mode) {
25270a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
252847d1ec61SGustavo F. Padovan 		chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
252947d1ec61SGustavo F. Padovan 		chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
253047d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
25310a708f8fSGustavo F. Padovan 		break;
25320a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
253347d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
25340a708f8fSGustavo F. Padovan 	}
25350a708f8fSGustavo F. Padovan }
25360a708f8fSGustavo F. Padovan 
25370a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
25380a708f8fSGustavo F. Padovan {
2539e2fd318eSIlia Kolomisnky 	struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
25400a708f8fSGustavo F. Padovan 
2541e2fd318eSIlia Kolomisnky 	if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
25420a708f8fSGustavo F. Padovan 		return 0;
25430a708f8fSGustavo F. Padovan 
25440a708f8fSGustavo F. Padovan 	if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
25450a708f8fSGustavo F. Padovan 					cmd->ident == conn->info_ident) {
25460a708f8fSGustavo F. Padovan 		del_timer(&conn->info_timer);
25470a708f8fSGustavo F. Padovan 
25480a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
25490a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
25500a708f8fSGustavo F. Padovan 
25510a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
25520a708f8fSGustavo F. Padovan 	}
25530a708f8fSGustavo F. Padovan 
25540a708f8fSGustavo F. Padovan 	return 0;
25550a708f8fSGustavo F. Padovan }
25560a708f8fSGustavo F. Padovan 
25570a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
25580a708f8fSGustavo F. Padovan {
25590a708f8fSGustavo F. Padovan 	struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
25600a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
256123691d75SGustavo F. Padovan 	struct l2cap_chan *chan = NULL, *pchan;
25620a708f8fSGustavo F. Padovan 	struct sock *parent, *sk = NULL;
25630a708f8fSGustavo F. Padovan 	int result, status = L2CAP_CS_NO_INFO;
25640a708f8fSGustavo F. Padovan 
25650a708f8fSGustavo F. Padovan 	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
25660a708f8fSGustavo F. Padovan 	__le16 psm = req->psm;
25670a708f8fSGustavo F. Padovan 
25680a708f8fSGustavo F. Padovan 	BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
25690a708f8fSGustavo F. Padovan 
25700a708f8fSGustavo F. Padovan 	/* Check if we have socket listening on psm */
257123691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src);
257223691d75SGustavo F. Padovan 	if (!pchan) {
25730a708f8fSGustavo F. Padovan 		result = L2CAP_CR_BAD_PSM;
25740a708f8fSGustavo F. Padovan 		goto sendresp;
25750a708f8fSGustavo F. Padovan 	}
25760a708f8fSGustavo F. Padovan 
257723691d75SGustavo F. Padovan 	parent = pchan->sk;
257823691d75SGustavo F. Padovan 
25790a708f8fSGustavo F. Padovan 	bh_lock_sock(parent);
25800a708f8fSGustavo F. Padovan 
25810a708f8fSGustavo F. Padovan 	/* Check if the ACL is secure enough (if not SDP) */
25820a708f8fSGustavo F. Padovan 	if (psm != cpu_to_le16(0x0001) &&
25830a708f8fSGustavo F. Padovan 				!hci_conn_check_link_mode(conn->hcon)) {
25849f5a0d7bSAndrei Emeltchenko 		conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
25850a708f8fSGustavo F. Padovan 		result = L2CAP_CR_SEC_BLOCK;
25860a708f8fSGustavo F. Padovan 		goto response;
25870a708f8fSGustavo F. Padovan 	}
25880a708f8fSGustavo F. Padovan 
25890a708f8fSGustavo F. Padovan 	result = L2CAP_CR_NO_MEM;
25900a708f8fSGustavo F. Padovan 
25910a708f8fSGustavo F. Padovan 	/* Check for backlog size */
25920a708f8fSGustavo F. Padovan 	if (sk_acceptq_is_full(parent)) {
25930a708f8fSGustavo F. Padovan 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
25940a708f8fSGustavo F. Padovan 		goto response;
25950a708f8fSGustavo F. Padovan 	}
25960a708f8fSGustavo F. Padovan 
259780808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
259880808e43SGustavo F. Padovan 	if (!chan)
25990a708f8fSGustavo F. Padovan 		goto response;
26000a708f8fSGustavo F. Padovan 
260180808e43SGustavo F. Padovan 	sk = chan->sk;
260280808e43SGustavo F. Padovan 
2603baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
26040a708f8fSGustavo F. Padovan 
26050a708f8fSGustavo F. Padovan 	/* Check if we already have channel with that dcid */
2606baa7e1faSGustavo F. Padovan 	if (__l2cap_get_chan_by_dcid(conn, scid)) {
2607baa7e1faSGustavo F. Padovan 		write_unlock_bh(&conn->chan_lock);
26080a708f8fSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
2609ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
26100a708f8fSGustavo F. Padovan 		goto response;
26110a708f8fSGustavo F. Padovan 	}
26120a708f8fSGustavo F. Padovan 
26130a708f8fSGustavo F. Padovan 	hci_conn_hold(conn->hcon);
26140a708f8fSGustavo F. Padovan 
26150a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->src, conn->src);
26160a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->dst, conn->dst);
2617fe4128e0SGustavo F. Padovan 	chan->psm  = psm;
2618fe4128e0SGustavo F. Padovan 	chan->dcid = scid;
26190a708f8fSGustavo F. Padovan 
2620d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
2621d1010240SGustavo F. Padovan 
262248454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
262348454079SGustavo F. Padovan 
2624fe4128e0SGustavo F. Padovan 	dcid = chan->scid;
26250a708f8fSGustavo F. Padovan 
2626c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
26270a708f8fSGustavo F. Padovan 
2628fc7f8a7eSGustavo F. Padovan 	chan->ident = cmd->ident;
26290a708f8fSGustavo F. Padovan 
26300a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
2631d45fc423SGustavo F. Padovan 		if (l2cap_chan_check_security(chan)) {
26320a708f8fSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup) {
263389bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONNECT2);
26340a708f8fSGustavo F. Padovan 				result = L2CAP_CR_PEND;
26350a708f8fSGustavo F. Padovan 				status = L2CAP_CS_AUTHOR_PEND;
26360a708f8fSGustavo F. Padovan 				parent->sk_data_ready(parent, 0);
26370a708f8fSGustavo F. Padovan 			} else {
263889bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONFIG);
26390a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SUCCESS;
26400a708f8fSGustavo F. Padovan 				status = L2CAP_CS_NO_INFO;
26410a708f8fSGustavo F. Padovan 			}
26420a708f8fSGustavo F. Padovan 		} else {
264389bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_CONNECT2);
26440a708f8fSGustavo F. Padovan 			result = L2CAP_CR_PEND;
26450a708f8fSGustavo F. Padovan 			status = L2CAP_CS_AUTHEN_PEND;
26460a708f8fSGustavo F. Padovan 		}
26470a708f8fSGustavo F. Padovan 	} else {
264889bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECT2);
26490a708f8fSGustavo F. Padovan 		result = L2CAP_CR_PEND;
26500a708f8fSGustavo F. Padovan 		status = L2CAP_CS_NO_INFO;
26510a708f8fSGustavo F. Padovan 	}
26520a708f8fSGustavo F. Padovan 
2653baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
26540a708f8fSGustavo F. Padovan 
26550a708f8fSGustavo F. Padovan response:
26560a708f8fSGustavo F. Padovan 	bh_unlock_sock(parent);
26570a708f8fSGustavo F. Padovan 
26580a708f8fSGustavo F. Padovan sendresp:
26590a708f8fSGustavo F. Padovan 	rsp.scid   = cpu_to_le16(scid);
26600a708f8fSGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(dcid);
26610a708f8fSGustavo F. Padovan 	rsp.result = cpu_to_le16(result);
26620a708f8fSGustavo F. Padovan 	rsp.status = cpu_to_le16(status);
26630a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
26640a708f8fSGustavo F. Padovan 
26650a708f8fSGustavo F. Padovan 	if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
26660a708f8fSGustavo F. Padovan 		struct l2cap_info_req info;
26670a708f8fSGustavo F. Padovan 		info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
26680a708f8fSGustavo F. Padovan 
26690a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
26700a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
26710a708f8fSGustavo F. Padovan 
26720a708f8fSGustavo F. Padovan 		mod_timer(&conn->info_timer, jiffies +
26730a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
26740a708f8fSGustavo F. Padovan 
26750a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
26760a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(info), &info);
26770a708f8fSGustavo F. Padovan 	}
26780a708f8fSGustavo F. Padovan 
2679c1360a1cSGustavo F. Padovan 	if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
26800a708f8fSGustavo F. Padovan 				result == L2CAP_CR_SUCCESS) {
26810a708f8fSGustavo F. Padovan 		u8 buf[128];
2682c1360a1cSGustavo F. Padovan 		set_bit(CONF_REQ_SENT, &chan->conf_state);
26830a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
268473ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
268573ffa904SGustavo F. Padovan 		chan->num_conf_req++;
26860a708f8fSGustavo F. Padovan 	}
26870a708f8fSGustavo F. Padovan 
26880a708f8fSGustavo F. Padovan 	return 0;
26890a708f8fSGustavo F. Padovan }
26900a708f8fSGustavo F. Padovan 
26910a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
26920a708f8fSGustavo F. Padovan {
26930a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
26940a708f8fSGustavo F. Padovan 	u16 scid, dcid, result, status;
269548454079SGustavo F. Padovan 	struct l2cap_chan *chan;
26960a708f8fSGustavo F. Padovan 	struct sock *sk;
26970a708f8fSGustavo F. Padovan 	u8 req[128];
26980a708f8fSGustavo F. Padovan 
26990a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
27000a708f8fSGustavo F. Padovan 	dcid   = __le16_to_cpu(rsp->dcid);
27010a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
27020a708f8fSGustavo F. Padovan 	status = __le16_to_cpu(rsp->status);
27030a708f8fSGustavo F. Padovan 
27040a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
27050a708f8fSGustavo F. Padovan 
27060a708f8fSGustavo F. Padovan 	if (scid) {
2707baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, scid);
270848454079SGustavo F. Padovan 		if (!chan)
27090a708f8fSGustavo F. Padovan 			return -EFAULT;
27100a708f8fSGustavo F. Padovan 	} else {
2711baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
271248454079SGustavo F. Padovan 		if (!chan)
27130a708f8fSGustavo F. Padovan 			return -EFAULT;
27140a708f8fSGustavo F. Padovan 	}
27150a708f8fSGustavo F. Padovan 
271648454079SGustavo F. Padovan 	sk = chan->sk;
271748454079SGustavo F. Padovan 
27180a708f8fSGustavo F. Padovan 	switch (result) {
27190a708f8fSGustavo F. Padovan 	case L2CAP_CR_SUCCESS:
272089bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONFIG);
2721fc7f8a7eSGustavo F. Padovan 		chan->ident = 0;
2722fe4128e0SGustavo F. Padovan 		chan->dcid = dcid;
2723c1360a1cSGustavo F. Padovan 		clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
27240a708f8fSGustavo F. Padovan 
2725c1360a1cSGustavo F. Padovan 		if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
27260a708f8fSGustavo F. Padovan 			break;
27270a708f8fSGustavo F. Padovan 
27280a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
272973ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, req), req);
273073ffa904SGustavo F. Padovan 		chan->num_conf_req++;
27310a708f8fSGustavo F. Padovan 		break;
27320a708f8fSGustavo F. Padovan 
27330a708f8fSGustavo F. Padovan 	case L2CAP_CR_PEND:
2734c1360a1cSGustavo F. Padovan 		set_bit(CONF_CONNECT_PEND, &chan->conf_state);
27350a708f8fSGustavo F. Padovan 		break;
27360a708f8fSGustavo F. Padovan 
27370a708f8fSGustavo F. Padovan 	default:
27380a708f8fSGustavo F. Padovan 		/* don't delete l2cap channel if sk is owned by user */
27390a708f8fSGustavo F. Padovan 		if (sock_owned_by_user(sk)) {
274089bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_DISCONN);
2741c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
2742f3f668b0SAndrzej Kaczmarek 			__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
27430a708f8fSGustavo F. Padovan 			break;
27440a708f8fSGustavo F. Padovan 		}
27450a708f8fSGustavo F. Padovan 
274648454079SGustavo F. Padovan 		l2cap_chan_del(chan, ECONNREFUSED);
27470a708f8fSGustavo F. Padovan 		break;
27480a708f8fSGustavo F. Padovan 	}
27490a708f8fSGustavo F. Padovan 
27500a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
27510a708f8fSGustavo F. Padovan 	return 0;
27520a708f8fSGustavo F. Padovan }
27530a708f8fSGustavo F. Padovan 
275447d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan)
27550a708f8fSGustavo F. Padovan {
27560a708f8fSGustavo F. Padovan 	/* FCS is enabled only in ERTM or streaming mode, if one or both
27570a708f8fSGustavo F. Padovan 	 * sides request it.
27580a708f8fSGustavo F. Padovan 	 */
27590c1bc5c6SGustavo F. Padovan 	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
276047d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_NONE;
2761c1360a1cSGustavo F. Padovan 	else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state))
276247d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_CRC16;
27630a708f8fSGustavo F. Padovan }
27640a708f8fSGustavo F. Padovan 
27650a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
27660a708f8fSGustavo F. Padovan {
27670a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
27680a708f8fSGustavo F. Padovan 	u16 dcid, flags;
27690a708f8fSGustavo F. Padovan 	u8 rsp[64];
277048454079SGustavo F. Padovan 	struct l2cap_chan *chan;
27710a708f8fSGustavo F. Padovan 	struct sock *sk;
27720a708f8fSGustavo F. Padovan 	int len;
27730a708f8fSGustavo F. Padovan 
27740a708f8fSGustavo F. Padovan 	dcid  = __le16_to_cpu(req->dcid);
27750a708f8fSGustavo F. Padovan 	flags = __le16_to_cpu(req->flags);
27760a708f8fSGustavo F. Padovan 
27770a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
27780a708f8fSGustavo F. Padovan 
2779baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
278048454079SGustavo F. Padovan 	if (!chan)
27810a708f8fSGustavo F. Padovan 		return -ENOENT;
27820a708f8fSGustavo F. Padovan 
278348454079SGustavo F. Padovan 	sk = chan->sk;
278448454079SGustavo F. Padovan 
2785033b1142SDavid S. Miller 	if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
2786e2fd318eSIlia Kolomisnky 		struct l2cap_cmd_rej_cid rej;
27870a708f8fSGustavo F. Padovan 
2788e2fd318eSIlia Kolomisnky 		rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
2789e2fd318eSIlia Kolomisnky 		rej.scid = cpu_to_le16(chan->scid);
2790e2fd318eSIlia Kolomisnky 		rej.dcid = cpu_to_le16(chan->dcid);
2791e2fd318eSIlia Kolomisnky 
27920a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
27930a708f8fSGustavo F. Padovan 				sizeof(rej), &rej);
27940a708f8fSGustavo F. Padovan 		goto unlock;
27950a708f8fSGustavo F. Padovan 	}
27960a708f8fSGustavo F. Padovan 
27970a708f8fSGustavo F. Padovan 	/* Reject if config buffer is too small. */
27980a708f8fSGustavo F. Padovan 	len = cmd_len - sizeof(*req);
27997ac28817SDan Rosenberg 	if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) {
28000a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2801fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
28020a708f8fSGustavo F. Padovan 					L2CAP_CONF_REJECT, flags), rsp);
28030a708f8fSGustavo F. Padovan 		goto unlock;
28040a708f8fSGustavo F. Padovan 	}
28050a708f8fSGustavo F. Padovan 
28060a708f8fSGustavo F. Padovan 	/* Store config. */
280773ffa904SGustavo F. Padovan 	memcpy(chan->conf_req + chan->conf_len, req->data, len);
280873ffa904SGustavo F. Padovan 	chan->conf_len += len;
28090a708f8fSGustavo F. Padovan 
28100a708f8fSGustavo F. Padovan 	if (flags & 0x0001) {
28110a708f8fSGustavo F. Padovan 		/* Incomplete config. Send empty response. */
28120a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2813fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
28140a708f8fSGustavo F. Padovan 					L2CAP_CONF_SUCCESS, 0x0001), rsp);
28150a708f8fSGustavo F. Padovan 		goto unlock;
28160a708f8fSGustavo F. Padovan 	}
28170a708f8fSGustavo F. Padovan 
28180a708f8fSGustavo F. Padovan 	/* Complete config. */
281973ffa904SGustavo F. Padovan 	len = l2cap_parse_conf_req(chan, rsp);
28200a708f8fSGustavo F. Padovan 	if (len < 0) {
2821e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
28220a708f8fSGustavo F. Padovan 		goto unlock;
28230a708f8fSGustavo F. Padovan 	}
28240a708f8fSGustavo F. Padovan 
28250a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
282673ffa904SGustavo F. Padovan 	chan->num_conf_rsp++;
28270a708f8fSGustavo F. Padovan 
28280a708f8fSGustavo F. Padovan 	/* Reset config buffer. */
282973ffa904SGustavo F. Padovan 	chan->conf_len = 0;
28300a708f8fSGustavo F. Padovan 
2831c1360a1cSGustavo F. Padovan 	if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
28320a708f8fSGustavo F. Padovan 		goto unlock;
28330a708f8fSGustavo F. Padovan 
2834c1360a1cSGustavo F. Padovan 	if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
283547d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
28360a708f8fSGustavo F. Padovan 
283789bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
28380a708f8fSGustavo F. Padovan 
283942e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
284042e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
284158d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
28420c1bc5c6SGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_ERTM)
2843525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
28440a708f8fSGustavo F. Padovan 
28450a708f8fSGustavo F. Padovan 		l2cap_chan_ready(sk);
28460a708f8fSGustavo F. Padovan 		goto unlock;
28470a708f8fSGustavo F. Padovan 	}
28480a708f8fSGustavo F. Padovan 
2849c1360a1cSGustavo F. Padovan 	if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
28500a708f8fSGustavo F. Padovan 		u8 buf[64];
28510a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
285273ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
285373ffa904SGustavo F. Padovan 		chan->num_conf_req++;
28540a708f8fSGustavo F. Padovan 	}
28550a708f8fSGustavo F. Padovan 
28560e8b207eSAndrei Emeltchenko 	/* Got Conf Rsp PENDING from remote side and asume we sent
28570e8b207eSAndrei Emeltchenko 	   Conf Rsp PENDING in the code above */
28580e8b207eSAndrei Emeltchenko 	if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
28590e8b207eSAndrei Emeltchenko 			test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
28600e8b207eSAndrei Emeltchenko 
28610e8b207eSAndrei Emeltchenko 		/* check compatibility */
28620e8b207eSAndrei Emeltchenko 
28630e8b207eSAndrei Emeltchenko 		clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
28640e8b207eSAndrei Emeltchenko 		set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
28650e8b207eSAndrei Emeltchenko 
28660e8b207eSAndrei Emeltchenko 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
28670e8b207eSAndrei Emeltchenko 					l2cap_build_conf_rsp(chan, rsp,
28680e8b207eSAndrei Emeltchenko 					L2CAP_CONF_SUCCESS, 0x0000), rsp);
28690e8b207eSAndrei Emeltchenko 	}
28700e8b207eSAndrei Emeltchenko 
28710a708f8fSGustavo F. Padovan unlock:
28720a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
28730a708f8fSGustavo F. Padovan 	return 0;
28740a708f8fSGustavo F. Padovan }
28750a708f8fSGustavo F. Padovan 
28760a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
28770a708f8fSGustavo F. Padovan {
28780a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
28790a708f8fSGustavo F. Padovan 	u16 scid, flags, result;
288048454079SGustavo F. Padovan 	struct l2cap_chan *chan;
28810a708f8fSGustavo F. Padovan 	struct sock *sk;
28820a708f8fSGustavo F. Padovan 	int len = cmd->len - sizeof(*rsp);
28830a708f8fSGustavo F. Padovan 
28840a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
28850a708f8fSGustavo F. Padovan 	flags  = __le16_to_cpu(rsp->flags);
28860a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
28870a708f8fSGustavo F. Padovan 
28880a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
28890a708f8fSGustavo F. Padovan 			scid, flags, result);
28900a708f8fSGustavo F. Padovan 
2891baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
289248454079SGustavo F. Padovan 	if (!chan)
28930a708f8fSGustavo F. Padovan 		return 0;
28940a708f8fSGustavo F. Padovan 
289548454079SGustavo F. Padovan 	sk = chan->sk;
289648454079SGustavo F. Padovan 
28970a708f8fSGustavo F. Padovan 	switch (result) {
28980a708f8fSGustavo F. Padovan 	case L2CAP_CONF_SUCCESS:
289947d1ec61SGustavo F. Padovan 		l2cap_conf_rfc_get(chan, rsp->data, len);
29000e8b207eSAndrei Emeltchenko 		clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
29010a708f8fSGustavo F. Padovan 		break;
29020a708f8fSGustavo F. Padovan 
29030e8b207eSAndrei Emeltchenko 	case L2CAP_CONF_PENDING:
29040e8b207eSAndrei Emeltchenko 		set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
29050e8b207eSAndrei Emeltchenko 
29060e8b207eSAndrei Emeltchenko 		if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
29070e8b207eSAndrei Emeltchenko 			char buf[64];
29080e8b207eSAndrei Emeltchenko 
29090e8b207eSAndrei Emeltchenko 			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
29100e8b207eSAndrei Emeltchenko 								buf, &result);
29110e8b207eSAndrei Emeltchenko 			if (len < 0) {
29120e8b207eSAndrei Emeltchenko 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
29130e8b207eSAndrei Emeltchenko 				goto done;
29140e8b207eSAndrei Emeltchenko 			}
29150e8b207eSAndrei Emeltchenko 
29160e8b207eSAndrei Emeltchenko 			/* check compatibility */
29170e8b207eSAndrei Emeltchenko 
29180e8b207eSAndrei Emeltchenko 			clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
29190e8b207eSAndrei Emeltchenko 			set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
29200e8b207eSAndrei Emeltchenko 
29210e8b207eSAndrei Emeltchenko 			l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
29220e8b207eSAndrei Emeltchenko 						l2cap_build_conf_rsp(chan, buf,
29230e8b207eSAndrei Emeltchenko 						L2CAP_CONF_SUCCESS, 0x0000), buf);
29240e8b207eSAndrei Emeltchenko 		}
29250e8b207eSAndrei Emeltchenko 		goto done;
29260e8b207eSAndrei Emeltchenko 
29270a708f8fSGustavo F. Padovan 	case L2CAP_CONF_UNACCEPT:
292873ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
29290a708f8fSGustavo F. Padovan 			char req[64];
29300a708f8fSGustavo F. Padovan 
29310a708f8fSGustavo F. Padovan 			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2932e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
29330a708f8fSGustavo F. Padovan 				goto done;
29340a708f8fSGustavo F. Padovan 			}
29350a708f8fSGustavo F. Padovan 
29360a708f8fSGustavo F. Padovan 			/* throw out any old stored conf requests */
29370a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_SUCCESS;
2938b4450035SGustavo F. Padovan 			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
2939b4450035SGustavo F. Padovan 								req, &result);
29400a708f8fSGustavo F. Padovan 			if (len < 0) {
2941e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
29420a708f8fSGustavo F. Padovan 				goto done;
29430a708f8fSGustavo F. Padovan 			}
29440a708f8fSGustavo F. Padovan 
29450a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn),
29460a708f8fSGustavo F. Padovan 						L2CAP_CONF_REQ, len, req);
294773ffa904SGustavo F. Padovan 			chan->num_conf_req++;
29480a708f8fSGustavo F. Padovan 			if (result != L2CAP_CONF_SUCCESS)
29490a708f8fSGustavo F. Padovan 				goto done;
29500a708f8fSGustavo F. Padovan 			break;
29510a708f8fSGustavo F. Padovan 		}
29520a708f8fSGustavo F. Padovan 
29530a708f8fSGustavo F. Padovan 	default:
29540a708f8fSGustavo F. Padovan 		sk->sk_err = ECONNRESET;
2955f3f668b0SAndrzej Kaczmarek 		__set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
2956e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
29570a708f8fSGustavo F. Padovan 		goto done;
29580a708f8fSGustavo F. Padovan 	}
29590a708f8fSGustavo F. Padovan 
29600a708f8fSGustavo F. Padovan 	if (flags & 0x01)
29610a708f8fSGustavo F. Padovan 		goto done;
29620a708f8fSGustavo F. Padovan 
2963c1360a1cSGustavo F. Padovan 	set_bit(CONF_INPUT_DONE, &chan->conf_state);
29640a708f8fSGustavo F. Padovan 
2965c1360a1cSGustavo F. Padovan 	if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
296647d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
29670a708f8fSGustavo F. Padovan 
296889bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
296942e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
297042e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
297158d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
29720c1bc5c6SGustavo F. Padovan 		if (chan->mode ==  L2CAP_MODE_ERTM)
2973525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
29740a708f8fSGustavo F. Padovan 
29750a708f8fSGustavo F. Padovan 		l2cap_chan_ready(sk);
29760a708f8fSGustavo F. Padovan 	}
29770a708f8fSGustavo F. Padovan 
29780a708f8fSGustavo F. Padovan done:
29790a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
29800a708f8fSGustavo F. Padovan 	return 0;
29810a708f8fSGustavo F. Padovan }
29820a708f8fSGustavo F. Padovan 
29830a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
29840a708f8fSGustavo F. Padovan {
29850a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
29860a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp rsp;
29870a708f8fSGustavo F. Padovan 	u16 dcid, scid;
298848454079SGustavo F. Padovan 	struct l2cap_chan *chan;
29890a708f8fSGustavo F. Padovan 	struct sock *sk;
29900a708f8fSGustavo F. Padovan 
29910a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(req->scid);
29920a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(req->dcid);
29930a708f8fSGustavo F. Padovan 
29940a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
29950a708f8fSGustavo F. Padovan 
2996baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
299748454079SGustavo F. Padovan 	if (!chan)
29980a708f8fSGustavo F. Padovan 		return 0;
29990a708f8fSGustavo F. Padovan 
300048454079SGustavo F. Padovan 	sk = chan->sk;
300148454079SGustavo F. Padovan 
3002fe4128e0SGustavo F. Padovan 	rsp.dcid = cpu_to_le16(chan->scid);
3003fe4128e0SGustavo F. Padovan 	rsp.scid = cpu_to_le16(chan->dcid);
30040a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
30050a708f8fSGustavo F. Padovan 
30060a708f8fSGustavo F. Padovan 	sk->sk_shutdown = SHUTDOWN_MASK;
30070a708f8fSGustavo F. Padovan 
30080a708f8fSGustavo F. Padovan 	/* don't delete l2cap channel if sk is owned by user */
30090a708f8fSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
301089bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_DISCONN);
3011c9b66675SGustavo F. Padovan 		__clear_chan_timer(chan);
3012f3f668b0SAndrzej Kaczmarek 		__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
30130a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
30140a708f8fSGustavo F. Padovan 		return 0;
30150a708f8fSGustavo F. Padovan 	}
30160a708f8fSGustavo F. Padovan 
301748454079SGustavo F. Padovan 	l2cap_chan_del(chan, ECONNRESET);
30180a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
30190a708f8fSGustavo F. Padovan 
3020ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
30210a708f8fSGustavo F. Padovan 	return 0;
30220a708f8fSGustavo F. Padovan }
30230a708f8fSGustavo F. Padovan 
30240a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
30250a708f8fSGustavo F. Padovan {
30260a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
30270a708f8fSGustavo F. Padovan 	u16 dcid, scid;
302848454079SGustavo F. Padovan 	struct l2cap_chan *chan;
30290a708f8fSGustavo F. Padovan 	struct sock *sk;
30300a708f8fSGustavo F. Padovan 
30310a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(rsp->scid);
30320a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(rsp->dcid);
30330a708f8fSGustavo F. Padovan 
30340a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
30350a708f8fSGustavo F. Padovan 
3036baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
303748454079SGustavo F. Padovan 	if (!chan)
30380a708f8fSGustavo F. Padovan 		return 0;
30390a708f8fSGustavo F. Padovan 
304048454079SGustavo F. Padovan 	sk = chan->sk;
304148454079SGustavo F. Padovan 
30420a708f8fSGustavo F. Padovan 	/* don't delete l2cap channel if sk is owned by user */
30430a708f8fSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
304489bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_DISCONN);
3045c9b66675SGustavo F. Padovan 		__clear_chan_timer(chan);
3046f3f668b0SAndrzej Kaczmarek 		__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
30470a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
30480a708f8fSGustavo F. Padovan 		return 0;
30490a708f8fSGustavo F. Padovan 	}
30500a708f8fSGustavo F. Padovan 
305148454079SGustavo F. Padovan 	l2cap_chan_del(chan, 0);
30520a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
30530a708f8fSGustavo F. Padovan 
3054ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
30550a708f8fSGustavo F. Padovan 	return 0;
30560a708f8fSGustavo F. Padovan }
30570a708f8fSGustavo F. Padovan 
30580a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
30590a708f8fSGustavo F. Padovan {
30600a708f8fSGustavo F. Padovan 	struct l2cap_info_req *req = (struct l2cap_info_req *) data;
30610a708f8fSGustavo F. Padovan 	u16 type;
30620a708f8fSGustavo F. Padovan 
30630a708f8fSGustavo F. Padovan 	type = __le16_to_cpu(req->type);
30640a708f8fSGustavo F. Padovan 
30650a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x", type);
30660a708f8fSGustavo F. Padovan 
30670a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
30680a708f8fSGustavo F. Padovan 		u8 buf[8];
30690a708f8fSGustavo F. Padovan 		u32 feat_mask = l2cap_feat_mask;
30700a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
30710a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FEAT_MASK);
30720a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
30730a708f8fSGustavo F. Padovan 		if (!disable_ertm)
30740a708f8fSGustavo F. Padovan 			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
30750a708f8fSGustavo F. Padovan 							 | L2CAP_FEAT_FCS;
3076a5fd6f30SAndrei Emeltchenko 		if (enable_hs)
30776327eb98SAndrei Emeltchenko 			feat_mask |= L2CAP_FEAT_EXT_FLOW
30786327eb98SAndrei Emeltchenko 						| L2CAP_FEAT_EXT_WINDOW;
3079a5fd6f30SAndrei Emeltchenko 
30800a708f8fSGustavo F. Padovan 		put_unaligned_le32(feat_mask, rsp->data);
30810a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
30820a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
30830a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
30840a708f8fSGustavo F. Padovan 		u8 buf[12];
30850a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
308650a147cdSMat Martineau 
308750a147cdSMat Martineau 		if (enable_hs)
308850a147cdSMat Martineau 			l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
308950a147cdSMat Martineau 		else
309050a147cdSMat Martineau 			l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;
309150a147cdSMat Martineau 
30920a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
30930a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
3094c6337ea6SAndrei Emeltchenko 		memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan));
30950a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
30960a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
30970a708f8fSGustavo F. Padovan 	} else {
30980a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp rsp;
30990a708f8fSGustavo F. Padovan 		rsp.type   = cpu_to_le16(type);
31000a708f8fSGustavo F. Padovan 		rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
31010a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
31020a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(rsp), &rsp);
31030a708f8fSGustavo F. Padovan 	}
31040a708f8fSGustavo F. Padovan 
31050a708f8fSGustavo F. Padovan 	return 0;
31060a708f8fSGustavo F. Padovan }
31070a708f8fSGustavo F. Padovan 
31080a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
31090a708f8fSGustavo F. Padovan {
31100a708f8fSGustavo F. Padovan 	struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
31110a708f8fSGustavo F. Padovan 	u16 type, result;
31120a708f8fSGustavo F. Padovan 
31130a708f8fSGustavo F. Padovan 	type   = __le16_to_cpu(rsp->type);
31140a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
31150a708f8fSGustavo F. Padovan 
31160a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
31170a708f8fSGustavo F. Padovan 
3118e90165beSAndrei Emeltchenko 	/* L2CAP Info req/rsp are unbound to channels, add extra checks */
3119e90165beSAndrei Emeltchenko 	if (cmd->ident != conn->info_ident ||
3120e90165beSAndrei Emeltchenko 			conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
3121e90165beSAndrei Emeltchenko 		return 0;
3122e90165beSAndrei Emeltchenko 
31230a708f8fSGustavo F. Padovan 	del_timer(&conn->info_timer);
31240a708f8fSGustavo F. Padovan 
31250a708f8fSGustavo F. Padovan 	if (result != L2CAP_IR_SUCCESS) {
31260a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
31270a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
31280a708f8fSGustavo F. Padovan 
31290a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
31300a708f8fSGustavo F. Padovan 
31310a708f8fSGustavo F. Padovan 		return 0;
31320a708f8fSGustavo F. Padovan 	}
31330a708f8fSGustavo F. Padovan 
31340a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
31350a708f8fSGustavo F. Padovan 		conn->feat_mask = get_unaligned_le32(rsp->data);
31360a708f8fSGustavo F. Padovan 
31370a708f8fSGustavo F. Padovan 		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
31380a708f8fSGustavo F. Padovan 			struct l2cap_info_req req;
31390a708f8fSGustavo F. Padovan 			req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
31400a708f8fSGustavo F. Padovan 
31410a708f8fSGustavo F. Padovan 			conn->info_ident = l2cap_get_ident(conn);
31420a708f8fSGustavo F. Padovan 
31430a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, conn->info_ident,
31440a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
31450a708f8fSGustavo F. Padovan 		} else {
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 	} else if (type == L2CAP_IT_FIXED_CHAN) {
31520a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
31530a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
31540a708f8fSGustavo F. Padovan 
31550a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
31560a708f8fSGustavo F. Padovan 	}
31570a708f8fSGustavo F. Padovan 
31580a708f8fSGustavo F. Padovan 	return 0;
31590a708f8fSGustavo F. Padovan }
31600a708f8fSGustavo F. Padovan 
3161f94ff6ffSMat Martineau static inline int l2cap_create_channel_req(struct l2cap_conn *conn,
3162f94ff6ffSMat Martineau 					struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3163f94ff6ffSMat Martineau 					void *data)
3164f94ff6ffSMat Martineau {
3165f94ff6ffSMat Martineau 	struct l2cap_create_chan_req *req = data;
3166f94ff6ffSMat Martineau 	struct l2cap_create_chan_rsp rsp;
3167f94ff6ffSMat Martineau 	u16 psm, scid;
3168f94ff6ffSMat Martineau 
3169f94ff6ffSMat Martineau 	if (cmd_len != sizeof(*req))
3170f94ff6ffSMat Martineau 		return -EPROTO;
3171f94ff6ffSMat Martineau 
3172f94ff6ffSMat Martineau 	if (!enable_hs)
3173f94ff6ffSMat Martineau 		return -EINVAL;
3174f94ff6ffSMat Martineau 
3175f94ff6ffSMat Martineau 	psm = le16_to_cpu(req->psm);
3176f94ff6ffSMat Martineau 	scid = le16_to_cpu(req->scid);
3177f94ff6ffSMat Martineau 
3178f94ff6ffSMat Martineau 	BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id);
3179f94ff6ffSMat Martineau 
3180f94ff6ffSMat Martineau 	/* Placeholder: Always reject */
3181f94ff6ffSMat Martineau 	rsp.dcid = 0;
3182f94ff6ffSMat Martineau 	rsp.scid = cpu_to_le16(scid);
3183f94ff6ffSMat Martineau 	rsp.result = L2CAP_CR_NO_MEM;
3184f94ff6ffSMat Martineau 	rsp.status = L2CAP_CS_NO_INFO;
3185f94ff6ffSMat Martineau 
3186f94ff6ffSMat Martineau 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
3187f94ff6ffSMat Martineau 		       sizeof(rsp), &rsp);
3188f94ff6ffSMat Martineau 
3189f94ff6ffSMat Martineau 	return 0;
3190f94ff6ffSMat Martineau }
3191f94ff6ffSMat Martineau 
3192f94ff6ffSMat Martineau static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn,
3193f94ff6ffSMat Martineau 					struct l2cap_cmd_hdr *cmd, void *data)
3194f94ff6ffSMat Martineau {
3195f94ff6ffSMat Martineau 	BT_DBG("conn %p", conn);
3196f94ff6ffSMat Martineau 
3197f94ff6ffSMat Martineau 	return l2cap_connect_rsp(conn, cmd, data);
3198f94ff6ffSMat Martineau }
3199f94ff6ffSMat Martineau 
32008d5a04a1SMat Martineau static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident,
32018d5a04a1SMat Martineau 							u16 icid, u16 result)
32028d5a04a1SMat Martineau {
32038d5a04a1SMat Martineau 	struct l2cap_move_chan_rsp rsp;
32048d5a04a1SMat Martineau 
32058d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
32068d5a04a1SMat Martineau 
32078d5a04a1SMat Martineau 	rsp.icid = cpu_to_le16(icid);
32088d5a04a1SMat Martineau 	rsp.result = cpu_to_le16(result);
32098d5a04a1SMat Martineau 
32108d5a04a1SMat Martineau 	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp);
32118d5a04a1SMat Martineau }
32128d5a04a1SMat Martineau 
32138d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn,
32148d5a04a1SMat Martineau 				struct l2cap_chan *chan, u16 icid, u16 result)
32158d5a04a1SMat Martineau {
32168d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm cfm;
32178d5a04a1SMat Martineau 	u8 ident;
32188d5a04a1SMat Martineau 
32198d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
32208d5a04a1SMat Martineau 
32218d5a04a1SMat Martineau 	ident = l2cap_get_ident(conn);
32228d5a04a1SMat Martineau 	if (chan)
32238d5a04a1SMat Martineau 		chan->ident = ident;
32248d5a04a1SMat Martineau 
32258d5a04a1SMat Martineau 	cfm.icid = cpu_to_le16(icid);
32268d5a04a1SMat Martineau 	cfm.result = cpu_to_le16(result);
32278d5a04a1SMat Martineau 
32288d5a04a1SMat Martineau 	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm);
32298d5a04a1SMat Martineau }
32308d5a04a1SMat Martineau 
32318d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
32328d5a04a1SMat Martineau 								u16 icid)
32338d5a04a1SMat Martineau {
32348d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm_rsp rsp;
32358d5a04a1SMat Martineau 
32368d5a04a1SMat Martineau 	BT_DBG("icid %d", icid);
32378d5a04a1SMat Martineau 
32388d5a04a1SMat Martineau 	rsp.icid = cpu_to_le16(icid);
32398d5a04a1SMat Martineau 	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
32408d5a04a1SMat Martineau }
32418d5a04a1SMat Martineau 
32428d5a04a1SMat Martineau static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
32438d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
32448d5a04a1SMat Martineau {
32458d5a04a1SMat Martineau 	struct l2cap_move_chan_req *req = data;
32468d5a04a1SMat Martineau 	u16 icid = 0;
32478d5a04a1SMat Martineau 	u16 result = L2CAP_MR_NOT_ALLOWED;
32488d5a04a1SMat Martineau 
32498d5a04a1SMat Martineau 	if (cmd_len != sizeof(*req))
32508d5a04a1SMat Martineau 		return -EPROTO;
32518d5a04a1SMat Martineau 
32528d5a04a1SMat Martineau 	icid = le16_to_cpu(req->icid);
32538d5a04a1SMat Martineau 
32548d5a04a1SMat Martineau 	BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id);
32558d5a04a1SMat Martineau 
32568d5a04a1SMat Martineau 	if (!enable_hs)
32578d5a04a1SMat Martineau 		return -EINVAL;
32588d5a04a1SMat Martineau 
32598d5a04a1SMat Martineau 	/* Placeholder: Always refuse */
32608d5a04a1SMat Martineau 	l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result);
32618d5a04a1SMat Martineau 
32628d5a04a1SMat Martineau 	return 0;
32638d5a04a1SMat Martineau }
32648d5a04a1SMat Martineau 
32658d5a04a1SMat Martineau static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn,
32668d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
32678d5a04a1SMat Martineau {
32688d5a04a1SMat Martineau 	struct l2cap_move_chan_rsp *rsp = data;
32698d5a04a1SMat Martineau 	u16 icid, result;
32708d5a04a1SMat Martineau 
32718d5a04a1SMat Martineau 	if (cmd_len != sizeof(*rsp))
32728d5a04a1SMat Martineau 		return -EPROTO;
32738d5a04a1SMat Martineau 
32748d5a04a1SMat Martineau 	icid = le16_to_cpu(rsp->icid);
32758d5a04a1SMat Martineau 	result = le16_to_cpu(rsp->result);
32768d5a04a1SMat Martineau 
32778d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
32788d5a04a1SMat Martineau 
32798d5a04a1SMat Martineau 	/* Placeholder: Always unconfirmed */
32808d5a04a1SMat Martineau 	l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED);
32818d5a04a1SMat Martineau 
32828d5a04a1SMat Martineau 	return 0;
32838d5a04a1SMat Martineau }
32848d5a04a1SMat Martineau 
32858d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn,
32868d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
32878d5a04a1SMat Martineau {
32888d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm *cfm = data;
32898d5a04a1SMat Martineau 	u16 icid, result;
32908d5a04a1SMat Martineau 
32918d5a04a1SMat Martineau 	if (cmd_len != sizeof(*cfm))
32928d5a04a1SMat Martineau 		return -EPROTO;
32938d5a04a1SMat Martineau 
32948d5a04a1SMat Martineau 	icid = le16_to_cpu(cfm->icid);
32958d5a04a1SMat Martineau 	result = le16_to_cpu(cfm->result);
32968d5a04a1SMat Martineau 
32978d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
32988d5a04a1SMat Martineau 
32998d5a04a1SMat Martineau 	l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
33008d5a04a1SMat Martineau 
33018d5a04a1SMat Martineau 	return 0;
33028d5a04a1SMat Martineau }
33038d5a04a1SMat Martineau 
33048d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
33058d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
33068d5a04a1SMat Martineau {
33078d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm_rsp *rsp = data;
33088d5a04a1SMat Martineau 	u16 icid;
33098d5a04a1SMat Martineau 
33108d5a04a1SMat Martineau 	if (cmd_len != sizeof(*rsp))
33118d5a04a1SMat Martineau 		return -EPROTO;
33128d5a04a1SMat Martineau 
33138d5a04a1SMat Martineau 	icid = le16_to_cpu(rsp->icid);
33148d5a04a1SMat Martineau 
33158d5a04a1SMat Martineau 	BT_DBG("icid %d", icid);
33168d5a04a1SMat Martineau 
33178d5a04a1SMat Martineau 	return 0;
33188d5a04a1SMat Martineau }
33198d5a04a1SMat Martineau 
3320e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
3321de73115aSClaudio Takahasi 							u16 to_multiplier)
3322de73115aSClaudio Takahasi {
3323de73115aSClaudio Takahasi 	u16 max_latency;
3324de73115aSClaudio Takahasi 
3325de73115aSClaudio Takahasi 	if (min > max || min < 6 || max > 3200)
3326de73115aSClaudio Takahasi 		return -EINVAL;
3327de73115aSClaudio Takahasi 
3328de73115aSClaudio Takahasi 	if (to_multiplier < 10 || to_multiplier > 3200)
3329de73115aSClaudio Takahasi 		return -EINVAL;
3330de73115aSClaudio Takahasi 
3331de73115aSClaudio Takahasi 	if (max >= to_multiplier * 8)
3332de73115aSClaudio Takahasi 		return -EINVAL;
3333de73115aSClaudio Takahasi 
3334de73115aSClaudio Takahasi 	max_latency = (to_multiplier * 8 / max) - 1;
3335de73115aSClaudio Takahasi 	if (latency > 499 || latency > max_latency)
3336de73115aSClaudio Takahasi 		return -EINVAL;
3337de73115aSClaudio Takahasi 
3338de73115aSClaudio Takahasi 	return 0;
3339de73115aSClaudio Takahasi }
3340de73115aSClaudio Takahasi 
3341de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
3342de73115aSClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
3343de73115aSClaudio Takahasi {
3344de73115aSClaudio Takahasi 	struct hci_conn *hcon = conn->hcon;
3345de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_req *req;
3346de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_rsp rsp;
3347de73115aSClaudio Takahasi 	u16 min, max, latency, to_multiplier, cmd_len;
33482ce603ebSClaudio Takahasi 	int err;
3349de73115aSClaudio Takahasi 
3350de73115aSClaudio Takahasi 	if (!(hcon->link_mode & HCI_LM_MASTER))
3351de73115aSClaudio Takahasi 		return -EINVAL;
3352de73115aSClaudio Takahasi 
3353de73115aSClaudio Takahasi 	cmd_len = __le16_to_cpu(cmd->len);
3354de73115aSClaudio Takahasi 	if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
3355de73115aSClaudio Takahasi 		return -EPROTO;
3356de73115aSClaudio Takahasi 
3357de73115aSClaudio Takahasi 	req = (struct l2cap_conn_param_update_req *) data;
3358de73115aSClaudio Takahasi 	min		= __le16_to_cpu(req->min);
3359de73115aSClaudio Takahasi 	max		= __le16_to_cpu(req->max);
3360de73115aSClaudio Takahasi 	latency		= __le16_to_cpu(req->latency);
3361de73115aSClaudio Takahasi 	to_multiplier	= __le16_to_cpu(req->to_multiplier);
3362de73115aSClaudio Takahasi 
3363de73115aSClaudio Takahasi 	BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
3364de73115aSClaudio Takahasi 						min, max, latency, to_multiplier);
3365de73115aSClaudio Takahasi 
3366de73115aSClaudio Takahasi 	memset(&rsp, 0, sizeof(rsp));
33672ce603ebSClaudio Takahasi 
33682ce603ebSClaudio Takahasi 	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
33692ce603ebSClaudio Takahasi 	if (err)
3370de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
3371de73115aSClaudio Takahasi 	else
3372de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
3373de73115aSClaudio Takahasi 
3374de73115aSClaudio Takahasi 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
3375de73115aSClaudio Takahasi 							sizeof(rsp), &rsp);
3376de73115aSClaudio Takahasi 
33772ce603ebSClaudio Takahasi 	if (!err)
33782ce603ebSClaudio Takahasi 		hci_le_conn_update(hcon, min, max, latency, to_multiplier);
33792ce603ebSClaudio Takahasi 
3380de73115aSClaudio Takahasi 	return 0;
3381de73115aSClaudio Takahasi }
3382de73115aSClaudio Takahasi 
33833300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
33843300d9a9SClaudio Takahasi 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
33853300d9a9SClaudio Takahasi {
33863300d9a9SClaudio Takahasi 	int err = 0;
33873300d9a9SClaudio Takahasi 
33883300d9a9SClaudio Takahasi 	switch (cmd->code) {
33893300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
33903300d9a9SClaudio Takahasi 		l2cap_command_rej(conn, cmd, data);
33913300d9a9SClaudio Takahasi 		break;
33923300d9a9SClaudio Takahasi 
33933300d9a9SClaudio Takahasi 	case L2CAP_CONN_REQ:
33943300d9a9SClaudio Takahasi 		err = l2cap_connect_req(conn, cmd, data);
33953300d9a9SClaudio Takahasi 		break;
33963300d9a9SClaudio Takahasi 
33973300d9a9SClaudio Takahasi 	case L2CAP_CONN_RSP:
33983300d9a9SClaudio Takahasi 		err = l2cap_connect_rsp(conn, cmd, data);
33993300d9a9SClaudio Takahasi 		break;
34003300d9a9SClaudio Takahasi 
34013300d9a9SClaudio Takahasi 	case L2CAP_CONF_REQ:
34023300d9a9SClaudio Takahasi 		err = l2cap_config_req(conn, cmd, cmd_len, data);
34033300d9a9SClaudio Takahasi 		break;
34043300d9a9SClaudio Takahasi 
34053300d9a9SClaudio Takahasi 	case L2CAP_CONF_RSP:
34063300d9a9SClaudio Takahasi 		err = l2cap_config_rsp(conn, cmd, data);
34073300d9a9SClaudio Takahasi 		break;
34083300d9a9SClaudio Takahasi 
34093300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_REQ:
34103300d9a9SClaudio Takahasi 		err = l2cap_disconnect_req(conn, cmd, data);
34113300d9a9SClaudio Takahasi 		break;
34123300d9a9SClaudio Takahasi 
34133300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_RSP:
34143300d9a9SClaudio Takahasi 		err = l2cap_disconnect_rsp(conn, cmd, data);
34153300d9a9SClaudio Takahasi 		break;
34163300d9a9SClaudio Takahasi 
34173300d9a9SClaudio Takahasi 	case L2CAP_ECHO_REQ:
34183300d9a9SClaudio Takahasi 		l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
34193300d9a9SClaudio Takahasi 		break;
34203300d9a9SClaudio Takahasi 
34213300d9a9SClaudio Takahasi 	case L2CAP_ECHO_RSP:
34223300d9a9SClaudio Takahasi 		break;
34233300d9a9SClaudio Takahasi 
34243300d9a9SClaudio Takahasi 	case L2CAP_INFO_REQ:
34253300d9a9SClaudio Takahasi 		err = l2cap_information_req(conn, cmd, data);
34263300d9a9SClaudio Takahasi 		break;
34273300d9a9SClaudio Takahasi 
34283300d9a9SClaudio Takahasi 	case L2CAP_INFO_RSP:
34293300d9a9SClaudio Takahasi 		err = l2cap_information_rsp(conn, cmd, data);
34303300d9a9SClaudio Takahasi 		break;
34313300d9a9SClaudio Takahasi 
3432f94ff6ffSMat Martineau 	case L2CAP_CREATE_CHAN_REQ:
3433f94ff6ffSMat Martineau 		err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
3434f94ff6ffSMat Martineau 		break;
3435f94ff6ffSMat Martineau 
3436f94ff6ffSMat Martineau 	case L2CAP_CREATE_CHAN_RSP:
3437f94ff6ffSMat Martineau 		err = l2cap_create_channel_rsp(conn, cmd, data);
3438f94ff6ffSMat Martineau 		break;
3439f94ff6ffSMat Martineau 
34408d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_REQ:
34418d5a04a1SMat Martineau 		err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
34428d5a04a1SMat Martineau 		break;
34438d5a04a1SMat Martineau 
34448d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_RSP:
34458d5a04a1SMat Martineau 		err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
34468d5a04a1SMat Martineau 		break;
34478d5a04a1SMat Martineau 
34488d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_CFM:
34498d5a04a1SMat Martineau 		err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
34508d5a04a1SMat Martineau 		break;
34518d5a04a1SMat Martineau 
34528d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_CFM_RSP:
34538d5a04a1SMat Martineau 		err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
34548d5a04a1SMat Martineau 		break;
34558d5a04a1SMat Martineau 
34563300d9a9SClaudio Takahasi 	default:
34573300d9a9SClaudio Takahasi 		BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
34583300d9a9SClaudio Takahasi 		err = -EINVAL;
34593300d9a9SClaudio Takahasi 		break;
34603300d9a9SClaudio Takahasi 	}
34613300d9a9SClaudio Takahasi 
34623300d9a9SClaudio Takahasi 	return err;
34633300d9a9SClaudio Takahasi }
34643300d9a9SClaudio Takahasi 
34653300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
34663300d9a9SClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
34673300d9a9SClaudio Takahasi {
34683300d9a9SClaudio Takahasi 	switch (cmd->code) {
34693300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
34703300d9a9SClaudio Takahasi 		return 0;
34713300d9a9SClaudio Takahasi 
34723300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_REQ:
3473de73115aSClaudio Takahasi 		return l2cap_conn_param_update_req(conn, cmd, data);
34743300d9a9SClaudio Takahasi 
34753300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_RSP:
34763300d9a9SClaudio Takahasi 		return 0;
34773300d9a9SClaudio Takahasi 
34783300d9a9SClaudio Takahasi 	default:
34793300d9a9SClaudio Takahasi 		BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
34803300d9a9SClaudio Takahasi 		return -EINVAL;
34813300d9a9SClaudio Takahasi 	}
34823300d9a9SClaudio Takahasi }
34833300d9a9SClaudio Takahasi 
34843300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn,
34853300d9a9SClaudio Takahasi 							struct sk_buff *skb)
34860a708f8fSGustavo F. Padovan {
34870a708f8fSGustavo F. Padovan 	u8 *data = skb->data;
34880a708f8fSGustavo F. Padovan 	int len = skb->len;
34890a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr cmd;
34903300d9a9SClaudio Takahasi 	int err;
34910a708f8fSGustavo F. Padovan 
34920a708f8fSGustavo F. Padovan 	l2cap_raw_recv(conn, skb);
34930a708f8fSGustavo F. Padovan 
34940a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CMD_HDR_SIZE) {
34950a708f8fSGustavo F. Padovan 		u16 cmd_len;
34960a708f8fSGustavo F. Padovan 		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
34970a708f8fSGustavo F. Padovan 		data += L2CAP_CMD_HDR_SIZE;
34980a708f8fSGustavo F. Padovan 		len  -= L2CAP_CMD_HDR_SIZE;
34990a708f8fSGustavo F. Padovan 
35000a708f8fSGustavo F. Padovan 		cmd_len = le16_to_cpu(cmd.len);
35010a708f8fSGustavo F. Padovan 
35020a708f8fSGustavo F. Padovan 		BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
35030a708f8fSGustavo F. Padovan 
35040a708f8fSGustavo F. Padovan 		if (cmd_len > len || !cmd.ident) {
35050a708f8fSGustavo F. Padovan 			BT_DBG("corrupted command");
35060a708f8fSGustavo F. Padovan 			break;
35070a708f8fSGustavo F. Padovan 		}
35080a708f8fSGustavo F. Padovan 
35093300d9a9SClaudio Takahasi 		if (conn->hcon->type == LE_LINK)
35103300d9a9SClaudio Takahasi 			err = l2cap_le_sig_cmd(conn, &cmd, data);
35113300d9a9SClaudio Takahasi 		else
35123300d9a9SClaudio Takahasi 			err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
35130a708f8fSGustavo F. Padovan 
35140a708f8fSGustavo F. Padovan 		if (err) {
3515e2fd318eSIlia Kolomisnky 			struct l2cap_cmd_rej_unk rej;
35162c6d1a2eSGustavo F. Padovan 
35172c6d1a2eSGustavo F. Padovan 			BT_ERR("Wrong link type (%d)", err);
35180a708f8fSGustavo F. Padovan 
35190a708f8fSGustavo F. Padovan 			/* FIXME: Map err to a valid reason */
3520e2fd318eSIlia Kolomisnky 			rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
35210a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
35220a708f8fSGustavo F. Padovan 		}
35230a708f8fSGustavo F. Padovan 
35240a708f8fSGustavo F. Padovan 		data += cmd_len;
35250a708f8fSGustavo F. Padovan 		len  -= cmd_len;
35260a708f8fSGustavo F. Padovan 	}
35270a708f8fSGustavo F. Padovan 
35280a708f8fSGustavo F. Padovan 	kfree_skb(skb);
35290a708f8fSGustavo F. Padovan }
35300a708f8fSGustavo F. Padovan 
353147d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
35320a708f8fSGustavo F. Padovan {
35330a708f8fSGustavo F. Padovan 	u16 our_fcs, rcv_fcs;
3534e4ca6d98SAndrei Emeltchenko 	int hdr_size;
3535e4ca6d98SAndrei Emeltchenko 
3536e4ca6d98SAndrei Emeltchenko 	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3537e4ca6d98SAndrei Emeltchenko 		hdr_size = L2CAP_EXT_HDR_SIZE;
3538e4ca6d98SAndrei Emeltchenko 	else
3539e4ca6d98SAndrei Emeltchenko 		hdr_size = L2CAP_ENH_HDR_SIZE;
35400a708f8fSGustavo F. Padovan 
354147d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
354203a51213SAndrei Emeltchenko 		skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
35430a708f8fSGustavo F. Padovan 		rcv_fcs = get_unaligned_le16(skb->data + skb->len);
35440a708f8fSGustavo F. Padovan 		our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
35450a708f8fSGustavo F. Padovan 
35460a708f8fSGustavo F. Padovan 		if (our_fcs != rcv_fcs)
35470a708f8fSGustavo F. Padovan 			return -EBADMSG;
35480a708f8fSGustavo F. Padovan 	}
35490a708f8fSGustavo F. Padovan 	return 0;
35500a708f8fSGustavo F. Padovan }
35510a708f8fSGustavo F. Padovan 
3552525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
35530a708f8fSGustavo F. Padovan {
355488843ab0SAndrei Emeltchenko 	u32 control = 0;
35550a708f8fSGustavo F. Padovan 
35566a026610SGustavo F. Padovan 	chan->frames_sent = 0;
35570a708f8fSGustavo F. Padovan 
35580b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
35590a708f8fSGustavo F. Padovan 
3560e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
3561ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
3562525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
3563e2ab4353SGustavo F. Padovan 		set_bit(CONN_RNR_SENT, &chan->conn_state);
35640a708f8fSGustavo F. Padovan 	}
35650a708f8fSGustavo F. Padovan 
3566e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
3567525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
35680a708f8fSGustavo F. Padovan 
3569525cd185SGustavo F. Padovan 	l2cap_ertm_send(chan);
35700a708f8fSGustavo F. Padovan 
3571e2ab4353SGustavo F. Padovan 	if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
35726a026610SGustavo F. Padovan 			chan->frames_sent == 0) {
3573ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
3574525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
35750a708f8fSGustavo F. Padovan 	}
35760a708f8fSGustavo F. Padovan }
35770a708f8fSGustavo F. Padovan 
3578fb45de7dSAndrei Emeltchenko static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar)
35790a708f8fSGustavo F. Padovan {
35800a708f8fSGustavo F. Padovan 	struct sk_buff *next_skb;
35810a708f8fSGustavo F. Padovan 	int tx_seq_offset, next_tx_seq_offset;
35820a708f8fSGustavo F. Padovan 
35830a708f8fSGustavo F. Padovan 	bt_cb(skb)->tx_seq = tx_seq;
35840a708f8fSGustavo F. Padovan 	bt_cb(skb)->sar = sar;
35850a708f8fSGustavo F. Padovan 
3586f1c6775bSGustavo F. Padovan 	next_skb = skb_peek(&chan->srej_q);
35870a708f8fSGustavo F. Padovan 
3588836be934SAndrei Emeltchenko 	tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq);
35890a708f8fSGustavo F. Padovan 
3590039d9572SSzymon Janc 	while (next_skb) {
35910a708f8fSGustavo F. Padovan 		if (bt_cb(next_skb)->tx_seq == tx_seq)
35920a708f8fSGustavo F. Padovan 			return -EINVAL;
35930a708f8fSGustavo F. Padovan 
3594836be934SAndrei Emeltchenko 		next_tx_seq_offset = __seq_offset(chan,
3595836be934SAndrei Emeltchenko 				bt_cb(next_skb)->tx_seq, chan->buffer_seq);
35960a708f8fSGustavo F. Padovan 
35970a708f8fSGustavo F. Padovan 		if (next_tx_seq_offset > tx_seq_offset) {
3598f1c6775bSGustavo F. Padovan 			__skb_queue_before(&chan->srej_q, next_skb, skb);
35990a708f8fSGustavo F. Padovan 			return 0;
36000a708f8fSGustavo F. Padovan 		}
36010a708f8fSGustavo F. Padovan 
3602f1c6775bSGustavo F. Padovan 		if (skb_queue_is_last(&chan->srej_q, next_skb))
3603039d9572SSzymon Janc 			next_skb = NULL;
3604039d9572SSzymon Janc 		else
3605039d9572SSzymon Janc 			next_skb = skb_queue_next(&chan->srej_q, next_skb);
3606039d9572SSzymon Janc 	}
36070a708f8fSGustavo F. Padovan 
3608f1c6775bSGustavo F. Padovan 	__skb_queue_tail(&chan->srej_q, skb);
36090a708f8fSGustavo F. Padovan 
36100a708f8fSGustavo F. Padovan 	return 0;
36110a708f8fSGustavo F. Padovan }
36120a708f8fSGustavo F. Padovan 
361384084a31SMat Martineau static void append_skb_frag(struct sk_buff *skb,
361484084a31SMat Martineau 			struct sk_buff *new_frag, struct sk_buff **last_frag)
36150a708f8fSGustavo F. Padovan {
361684084a31SMat Martineau 	/* skb->len reflects data in skb as well as all fragments
361784084a31SMat Martineau 	 * skb->data_len reflects only data in fragments
361884084a31SMat Martineau 	 */
361984084a31SMat Martineau 	if (!skb_has_frag_list(skb))
362084084a31SMat Martineau 		skb_shinfo(skb)->frag_list = new_frag;
362184084a31SMat Martineau 
362284084a31SMat Martineau 	new_frag->next = NULL;
362384084a31SMat Martineau 
362484084a31SMat Martineau 	(*last_frag)->next = new_frag;
362584084a31SMat Martineau 	*last_frag = new_frag;
362684084a31SMat Martineau 
362784084a31SMat Martineau 	skb->len += new_frag->len;
362884084a31SMat Martineau 	skb->data_len += new_frag->len;
362984084a31SMat Martineau 	skb->truesize += new_frag->truesize;
363084084a31SMat Martineau }
363184084a31SMat Martineau 
363288843ab0SAndrei Emeltchenko static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u32 control)
363384084a31SMat Martineau {
363484084a31SMat Martineau 	int err = -EINVAL;
36350a708f8fSGustavo F. Padovan 
36367e0ef6eeSAndrei Emeltchenko 	switch (__get_ctrl_sar(chan, control)) {
36377e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_UNSEGMENTED:
363884084a31SMat Martineau 		if (chan->sdu)
363984084a31SMat Martineau 			break;
36400a708f8fSGustavo F. Padovan 
364184084a31SMat Martineau 		err = chan->ops->recv(chan->data, skb);
364284084a31SMat Martineau 		break;
36430a708f8fSGustavo F. Padovan 
36447e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_START:
364584084a31SMat Martineau 		if (chan->sdu)
364684084a31SMat Martineau 			break;
36470a708f8fSGustavo F. Padovan 
36486f61fd47SGustavo F. Padovan 		chan->sdu_len = get_unaligned_le16(skb->data);
364903a51213SAndrei Emeltchenko 		skb_pull(skb, L2CAP_SDULEN_SIZE);
36500a708f8fSGustavo F. Padovan 
365184084a31SMat Martineau 		if (chan->sdu_len > chan->imtu) {
365284084a31SMat Martineau 			err = -EMSGSIZE;
365384084a31SMat Martineau 			break;
365484084a31SMat Martineau 		}
36550a708f8fSGustavo F. Padovan 
365684084a31SMat Martineau 		if (skb->len >= chan->sdu_len)
365784084a31SMat Martineau 			break;
365884084a31SMat Martineau 
365984084a31SMat Martineau 		chan->sdu = skb;
366084084a31SMat Martineau 		chan->sdu_last_frag = skb;
366184084a31SMat Martineau 
366284084a31SMat Martineau 		skb = NULL;
366384084a31SMat Martineau 		err = 0;
36640a708f8fSGustavo F. Padovan 		break;
36650a708f8fSGustavo F. Padovan 
36667e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_CONTINUE:
36676f61fd47SGustavo F. Padovan 		if (!chan->sdu)
366884084a31SMat Martineau 			break;
36690a708f8fSGustavo F. Padovan 
367084084a31SMat Martineau 		append_skb_frag(chan->sdu, skb,
367184084a31SMat Martineau 				&chan->sdu_last_frag);
367284084a31SMat Martineau 		skb = NULL;
36730a708f8fSGustavo F. Padovan 
367484084a31SMat Martineau 		if (chan->sdu->len >= chan->sdu_len)
367584084a31SMat Martineau 			break;
36760a708f8fSGustavo F. Padovan 
367784084a31SMat Martineau 		err = 0;
36780a708f8fSGustavo F. Padovan 		break;
36790a708f8fSGustavo F. Padovan 
36807e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_END:
36816f61fd47SGustavo F. Padovan 		if (!chan->sdu)
368284084a31SMat Martineau 			break;
36830a708f8fSGustavo F. Padovan 
368484084a31SMat Martineau 		append_skb_frag(chan->sdu, skb,
368584084a31SMat Martineau 				&chan->sdu_last_frag);
368684084a31SMat Martineau 		skb = NULL;
36870a708f8fSGustavo F. Padovan 
368884084a31SMat Martineau 		if (chan->sdu->len != chan->sdu_len)
368984084a31SMat Martineau 			break;
36900a708f8fSGustavo F. Padovan 
369184084a31SMat Martineau 		err = chan->ops->recv(chan->data, chan->sdu);
36920a708f8fSGustavo F. Padovan 
369384084a31SMat Martineau 		if (!err) {
369484084a31SMat Martineau 			/* Reassembly complete */
369584084a31SMat Martineau 			chan->sdu = NULL;
369684084a31SMat Martineau 			chan->sdu_last_frag = NULL;
369784084a31SMat Martineau 			chan->sdu_len = 0;
36980a708f8fSGustavo F. Padovan 		}
36990a708f8fSGustavo F. Padovan 		break;
37000a708f8fSGustavo F. Padovan 	}
37010a708f8fSGustavo F. Padovan 
370284084a31SMat Martineau 	if (err) {
37030a708f8fSGustavo F. Padovan 		kfree_skb(skb);
37046f61fd47SGustavo F. Padovan 		kfree_skb(chan->sdu);
37056f61fd47SGustavo F. Padovan 		chan->sdu = NULL;
370684084a31SMat Martineau 		chan->sdu_last_frag = NULL;
370784084a31SMat Martineau 		chan->sdu_len = 0;
370884084a31SMat Martineau 	}
37090a708f8fSGustavo F. Padovan 
371084084a31SMat Martineau 	return err;
37110a708f8fSGustavo F. Padovan }
37120a708f8fSGustavo F. Padovan 
371326f880d2SMat Martineau static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan)
37140a708f8fSGustavo F. Padovan {
371588843ab0SAndrei Emeltchenko 	u32 control;
37160a708f8fSGustavo F. Padovan 
371726f880d2SMat Martineau 	BT_DBG("chan %p, Enter local busy", chan);
371826f880d2SMat Martineau 
371926f880d2SMat Martineau 	set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
372026f880d2SMat Martineau 
37210b209faeSAndrei Emeltchenko 	control = __set_reqseq(chan, chan->buffer_seq);
3722ab784b73SAndrei Emeltchenko 	control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
372326f880d2SMat Martineau 	l2cap_send_sframe(chan, control);
372426f880d2SMat Martineau 
372526f880d2SMat Martineau 	set_bit(CONN_RNR_SENT, &chan->conn_state);
372626f880d2SMat Martineau 
372726f880d2SMat Martineau 	__clear_ack_timer(chan);
37280a708f8fSGustavo F. Padovan }
37290a708f8fSGustavo F. Padovan 
373026f880d2SMat Martineau static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan)
373126f880d2SMat Martineau {
373288843ab0SAndrei Emeltchenko 	u32 control;
37330a708f8fSGustavo F. Padovan 
3734e2ab4353SGustavo F. Padovan 	if (!test_bit(CONN_RNR_SENT, &chan->conn_state))
37350a708f8fSGustavo F. Padovan 		goto done;
37360a708f8fSGustavo F. Padovan 
37370b209faeSAndrei Emeltchenko 	control = __set_reqseq(chan, chan->buffer_seq);
3738e3781735SAndrei Emeltchenko 	control |= __set_ctrl_poll(chan);
3739ab784b73SAndrei Emeltchenko 	control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
3740525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
37416a026610SGustavo F. Padovan 	chan->retry_count = 1;
37420a708f8fSGustavo F. Padovan 
37431a09bcb9SGustavo F. Padovan 	__clear_retrans_timer(chan);
37441a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
37450a708f8fSGustavo F. Padovan 
3746e2ab4353SGustavo F. Padovan 	set_bit(CONN_WAIT_F, &chan->conn_state);
37470a708f8fSGustavo F. Padovan 
37480a708f8fSGustavo F. Padovan done:
3749e2ab4353SGustavo F. Padovan 	clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
3750e2ab4353SGustavo F. Padovan 	clear_bit(CONN_RNR_SENT, &chan->conn_state);
37510a708f8fSGustavo F. Padovan 
375249208c9cSGustavo F. Padovan 	BT_DBG("chan %p, Exit local busy", chan);
37530a708f8fSGustavo F. Padovan }
37540a708f8fSGustavo F. Padovan 
3755e328140fSMat Martineau void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
37560a708f8fSGustavo F. Padovan {
3757e328140fSMat Martineau 	if (chan->mode == L2CAP_MODE_ERTM) {
3758e328140fSMat Martineau 		if (busy)
375926f880d2SMat Martineau 			l2cap_ertm_enter_local_busy(chan);
3760e328140fSMat Martineau 		else
3761e328140fSMat Martineau 			l2cap_ertm_exit_local_busy(chan);
37620a708f8fSGustavo F. Padovan 	}
37630a708f8fSGustavo F. Padovan }
37640a708f8fSGustavo F. Padovan 
3765fb45de7dSAndrei Emeltchenko static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq)
37660a708f8fSGustavo F. Padovan {
37670a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
376888843ab0SAndrei Emeltchenko 	u32 control;
37690a708f8fSGustavo F. Padovan 
3770e328140fSMat Martineau 	while ((skb = skb_peek(&chan->srej_q)) &&
3771e328140fSMat Martineau 			!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
3772e328140fSMat Martineau 		int err;
3773e328140fSMat Martineau 
37740a708f8fSGustavo F. Padovan 		if (bt_cb(skb)->tx_seq != tx_seq)
37750a708f8fSGustavo F. Padovan 			break;
37760a708f8fSGustavo F. Padovan 
3777f1c6775bSGustavo F. Padovan 		skb = skb_dequeue(&chan->srej_q);
37787e0ef6eeSAndrei Emeltchenko 		control = __set_ctrl_sar(chan, bt_cb(skb)->sar);
377984084a31SMat Martineau 		err = l2cap_reassemble_sdu(chan, skb, control);
3780e328140fSMat Martineau 
3781e328140fSMat Martineau 		if (err < 0) {
3782e328140fSMat Martineau 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3783e328140fSMat Martineau 			break;
3784e328140fSMat Martineau 		}
3785e328140fSMat Martineau 
3786836be934SAndrei Emeltchenko 		chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej);
3787836be934SAndrei Emeltchenko 		tx_seq = __next_seq(chan, tx_seq);
37880a708f8fSGustavo F. Padovan 	}
37890a708f8fSGustavo F. Padovan }
37900a708f8fSGustavo F. Padovan 
3791fb45de7dSAndrei Emeltchenko static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq)
37920a708f8fSGustavo F. Padovan {
37930a708f8fSGustavo F. Padovan 	struct srej_list *l, *tmp;
379488843ab0SAndrei Emeltchenko 	u32 control;
37950a708f8fSGustavo F. Padovan 
379639d5a3eeSGustavo F. Padovan 	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
37970a708f8fSGustavo F. Padovan 		if (l->tx_seq == tx_seq) {
37980a708f8fSGustavo F. Padovan 			list_del(&l->list);
37990a708f8fSGustavo F. Padovan 			kfree(l);
38000a708f8fSGustavo F. Padovan 			return;
38010a708f8fSGustavo F. Padovan 		}
3802ab784b73SAndrei Emeltchenko 		control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
38030b209faeSAndrei Emeltchenko 		control |= __set_reqseq(chan, l->tx_seq);
3804525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
38050a708f8fSGustavo F. Padovan 		list_del(&l->list);
380639d5a3eeSGustavo F. Padovan 		list_add_tail(&l->list, &chan->srej_l);
38070a708f8fSGustavo F. Padovan 	}
38080a708f8fSGustavo F. Padovan }
38090a708f8fSGustavo F. Padovan 
3810aef89f21SSzymon Janc static int l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq)
38110a708f8fSGustavo F. Padovan {
38120a708f8fSGustavo F. Padovan 	struct srej_list *new;
381388843ab0SAndrei Emeltchenko 	u32 control;
38140a708f8fSGustavo F. Padovan 
381542e5c802SGustavo F. Padovan 	while (tx_seq != chan->expected_tx_seq) {
3816ab784b73SAndrei Emeltchenko 		control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
38170b209faeSAndrei Emeltchenko 		control |= __set_reqseq(chan, chan->expected_tx_seq);
3818525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
38190a708f8fSGustavo F. Padovan 
38200a708f8fSGustavo F. Padovan 		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
3821aef89f21SSzymon Janc 		if (!new)
3822aef89f21SSzymon Janc 			return -ENOMEM;
3823aef89f21SSzymon Janc 
382442e5c802SGustavo F. Padovan 		new->tx_seq = chan->expected_tx_seq;
3825836be934SAndrei Emeltchenko 
3826836be934SAndrei Emeltchenko 		chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
3827836be934SAndrei Emeltchenko 
382839d5a3eeSGustavo F. Padovan 		list_add_tail(&new->list, &chan->srej_l);
38290a708f8fSGustavo F. Padovan 	}
3830836be934SAndrei Emeltchenko 
3831836be934SAndrei Emeltchenko 	chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
3832aef89f21SSzymon Janc 
3833aef89f21SSzymon Janc 	return 0;
38340a708f8fSGustavo F. Padovan }
38350a708f8fSGustavo F. Padovan 
383688843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb)
38370a708f8fSGustavo F. Padovan {
3838fb45de7dSAndrei Emeltchenko 	u16 tx_seq = __get_txseq(chan, rx_control);
38390b209faeSAndrei Emeltchenko 	u16 req_seq = __get_reqseq(chan, rx_control);
38407e0ef6eeSAndrei Emeltchenko 	u8 sar = __get_ctrl_sar(chan, rx_control);
38410a708f8fSGustavo F. Padovan 	int tx_seq_offset, expected_tx_seq_offset;
384247d1ec61SGustavo F. Padovan 	int num_to_ack = (chan->tx_win/6) + 1;
38430a708f8fSGustavo F. Padovan 	int err = 0;
38440a708f8fSGustavo F. Padovan 
384588843ab0SAndrei Emeltchenko 	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%8.8x", chan, skb->len,
3846525cd185SGustavo F. Padovan 							tx_seq, rx_control);
38470a708f8fSGustavo F. Padovan 
384803f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control) &&
3849e2ab4353SGustavo F. Padovan 			test_bit(CONN_WAIT_F, &chan->conn_state)) {
38501a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
38516a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
38521a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
3853e2ab4353SGustavo F. Padovan 		clear_bit(CONN_WAIT_F, &chan->conn_state);
38540a708f8fSGustavo F. Padovan 	}
38550a708f8fSGustavo F. Padovan 
385642e5c802SGustavo F. Padovan 	chan->expected_ack_seq = req_seq;
385742e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
38580a708f8fSGustavo F. Padovan 
3859836be934SAndrei Emeltchenko 	tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq);
38600a708f8fSGustavo F. Padovan 
38610a708f8fSGustavo F. Padovan 	/* invalid tx_seq */
386247d1ec61SGustavo F. Padovan 	if (tx_seq_offset >= chan->tx_win) {
38638c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38640a708f8fSGustavo F. Padovan 		goto drop;
38650a708f8fSGustavo F. Padovan 	}
38660a708f8fSGustavo F. Padovan 
3867e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
38680a708f8fSGustavo F. Padovan 		goto drop;
38690a708f8fSGustavo F. Padovan 
387002f1b641SMat Martineau 	if (tx_seq == chan->expected_tx_seq)
387102f1b641SMat Martineau 		goto expected;
387202f1b641SMat Martineau 
3873e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
38740a708f8fSGustavo F. Padovan 		struct srej_list *first;
38750a708f8fSGustavo F. Padovan 
387639d5a3eeSGustavo F. Padovan 		first = list_first_entry(&chan->srej_l,
38770a708f8fSGustavo F. Padovan 				struct srej_list, list);
38780a708f8fSGustavo F. Padovan 		if (tx_seq == first->tx_seq) {
387942e5c802SGustavo F. Padovan 			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3880525cd185SGustavo F. Padovan 			l2cap_check_srej_gap(chan, tx_seq);
38810a708f8fSGustavo F. Padovan 
38820a708f8fSGustavo F. Padovan 			list_del(&first->list);
38830a708f8fSGustavo F. Padovan 			kfree(first);
38840a708f8fSGustavo F. Padovan 
388539d5a3eeSGustavo F. Padovan 			if (list_empty(&chan->srej_l)) {
388642e5c802SGustavo F. Padovan 				chan->buffer_seq = chan->buffer_seq_srej;
3887e2ab4353SGustavo F. Padovan 				clear_bit(CONN_SREJ_SENT, &chan->conn_state);
3888525cd185SGustavo F. Padovan 				l2cap_send_ack(chan);
388949208c9cSGustavo F. Padovan 				BT_DBG("chan %p, Exit SREJ_SENT", chan);
38900a708f8fSGustavo F. Padovan 			}
38910a708f8fSGustavo F. Padovan 		} else {
38920a708f8fSGustavo F. Padovan 			struct srej_list *l;
38930a708f8fSGustavo F. Padovan 
38940a708f8fSGustavo F. Padovan 			/* duplicated tx_seq */
389542e5c802SGustavo F. Padovan 			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
38960a708f8fSGustavo F. Padovan 				goto drop;
38970a708f8fSGustavo F. Padovan 
389839d5a3eeSGustavo F. Padovan 			list_for_each_entry(l, &chan->srej_l, list) {
38990a708f8fSGustavo F. Padovan 				if (l->tx_seq == tx_seq) {
3900525cd185SGustavo F. Padovan 					l2cap_resend_srejframe(chan, tx_seq);
39010a708f8fSGustavo F. Padovan 					return 0;
39020a708f8fSGustavo F. Padovan 				}
39030a708f8fSGustavo F. Padovan 			}
3904aef89f21SSzymon Janc 
3905aef89f21SSzymon Janc 			err = l2cap_send_srejframe(chan, tx_seq);
3906aef89f21SSzymon Janc 			if (err < 0) {
3907aef89f21SSzymon Janc 				l2cap_send_disconn_req(chan->conn, chan, -err);
3908aef89f21SSzymon Janc 				return err;
3909aef89f21SSzymon Janc 			}
39100a708f8fSGustavo F. Padovan 		}
39110a708f8fSGustavo F. Padovan 	} else {
3912836be934SAndrei Emeltchenko 		expected_tx_seq_offset = __seq_offset(chan,
3913836be934SAndrei Emeltchenko 				chan->expected_tx_seq, chan->buffer_seq);
39140a708f8fSGustavo F. Padovan 
39150a708f8fSGustavo F. Padovan 		/* duplicated tx_seq */
39160a708f8fSGustavo F. Padovan 		if (tx_seq_offset < expected_tx_seq_offset)
39170a708f8fSGustavo F. Padovan 			goto drop;
39180a708f8fSGustavo F. Padovan 
3919e2ab4353SGustavo F. Padovan 		set_bit(CONN_SREJ_SENT, &chan->conn_state);
39200a708f8fSGustavo F. Padovan 
392149208c9cSGustavo F. Padovan 		BT_DBG("chan %p, Enter SREJ", chan);
39220a708f8fSGustavo F. Padovan 
392339d5a3eeSGustavo F. Padovan 		INIT_LIST_HEAD(&chan->srej_l);
392442e5c802SGustavo F. Padovan 		chan->buffer_seq_srej = chan->buffer_seq;
39250a708f8fSGustavo F. Padovan 
3926f1c6775bSGustavo F. Padovan 		__skb_queue_head_init(&chan->srej_q);
392742e5c802SGustavo F. Padovan 		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
39280a708f8fSGustavo F. Padovan 
3929e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_PBIT, &chan->conn_state);
39300a708f8fSGustavo F. Padovan 
3931aef89f21SSzymon Janc 		err = l2cap_send_srejframe(chan, tx_seq);
3932aef89f21SSzymon Janc 		if (err < 0) {
3933aef89f21SSzymon Janc 			l2cap_send_disconn_req(chan->conn, chan, -err);
3934aef89f21SSzymon Janc 			return err;
3935aef89f21SSzymon Janc 		}
39360a708f8fSGustavo F. Padovan 
39371a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
39380a708f8fSGustavo F. Padovan 	}
39390a708f8fSGustavo F. Padovan 	return 0;
39400a708f8fSGustavo F. Padovan 
39410a708f8fSGustavo F. Padovan expected:
3942836be934SAndrei Emeltchenko 	chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
39430a708f8fSGustavo F. Padovan 
3944e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
39450a708f8fSGustavo F. Padovan 		bt_cb(skb)->tx_seq = tx_seq;
39460a708f8fSGustavo F. Padovan 		bt_cb(skb)->sar = sar;
3947f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->srej_q, skb);
39480a708f8fSGustavo F. Padovan 		return 0;
39490a708f8fSGustavo F. Padovan 	}
39500a708f8fSGustavo F. Padovan 
395184084a31SMat Martineau 	err = l2cap_reassemble_sdu(chan, skb, rx_control);
3952836be934SAndrei Emeltchenko 	chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
3953836be934SAndrei Emeltchenko 
3954e328140fSMat Martineau 	if (err < 0) {
3955e328140fSMat Martineau 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
3956e328140fSMat Martineau 		return err;
3957e328140fSMat Martineau 	}
39580a708f8fSGustavo F. Padovan 
395903f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control)) {
3960e2ab4353SGustavo F. Padovan 		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
3961525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
39620a708f8fSGustavo F. Padovan 	}
39630a708f8fSGustavo F. Padovan 
39640a708f8fSGustavo F. Padovan 
39656a026610SGustavo F. Padovan 	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
39666a026610SGustavo F. Padovan 	if (chan->num_acked == num_to_ack - 1)
3967525cd185SGustavo F. Padovan 		l2cap_send_ack(chan);
39684d611e4dSGustavo F. Padovan 	else
39694d611e4dSGustavo F. Padovan 		__set_ack_timer(chan);
39700a708f8fSGustavo F. Padovan 
39710a708f8fSGustavo F. Padovan 	return 0;
39720a708f8fSGustavo F. Padovan 
39730a708f8fSGustavo F. Padovan drop:
39740a708f8fSGustavo F. Padovan 	kfree_skb(skb);
39750a708f8fSGustavo F. Padovan 	return 0;
39760a708f8fSGustavo F. Padovan }
39770a708f8fSGustavo F. Padovan 
397888843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u32 rx_control)
39790a708f8fSGustavo F. Padovan {
398088843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan,
39810b209faeSAndrei Emeltchenko 				__get_reqseq(chan, rx_control), rx_control);
39820a708f8fSGustavo F. Padovan 
39830b209faeSAndrei Emeltchenko 	chan->expected_ack_seq = __get_reqseq(chan, rx_control);
398442e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
39850a708f8fSGustavo F. Padovan 
3986e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control)) {
3987e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_FBIT, &chan->conn_state);
3988e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
3989e2ab4353SGustavo F. Padovan 			if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
39906a026610SGustavo F. Padovan 					(chan->unacked_frames > 0))
39911a09bcb9SGustavo F. Padovan 				__set_retrans_timer(chan);
39920a708f8fSGustavo F. Padovan 
3993e2ab4353SGustavo F. Padovan 			clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
3994525cd185SGustavo F. Padovan 			l2cap_send_srejtail(chan);
39950a708f8fSGustavo F. Padovan 		} else {
3996525cd185SGustavo F. Padovan 			l2cap_send_i_or_rr_or_rnr(chan);
39970a708f8fSGustavo F. Padovan 		}
39980a708f8fSGustavo F. Padovan 
399903f6715dSAndrei Emeltchenko 	} else if (__is_ctrl_final(chan, rx_control)) {
4000e2ab4353SGustavo F. Padovan 		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
40010a708f8fSGustavo F. Padovan 
4002e2ab4353SGustavo F. Padovan 		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
4003525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
40040a708f8fSGustavo F. Padovan 
40050a708f8fSGustavo F. Padovan 	} else {
4006e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
40076a026610SGustavo F. Padovan 				(chan->unacked_frames > 0))
40081a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
40090a708f8fSGustavo F. Padovan 
4010e2ab4353SGustavo F. Padovan 		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
4011e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_SREJ_SENT, &chan->conn_state))
4012525cd185SGustavo F. Padovan 			l2cap_send_ack(chan);
40130a708f8fSGustavo F. Padovan 		else
4014525cd185SGustavo F. Padovan 			l2cap_ertm_send(chan);
40150a708f8fSGustavo F. Padovan 	}
40160a708f8fSGustavo F. Padovan }
40170a708f8fSGustavo F. Padovan 
401888843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u32 rx_control)
40190a708f8fSGustavo F. Padovan {
40200b209faeSAndrei Emeltchenko 	u16 tx_seq = __get_reqseq(chan, rx_control);
40210a708f8fSGustavo F. Padovan 
402288843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
40230a708f8fSGustavo F. Padovan 
4024e2ab4353SGustavo F. Padovan 	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
40250a708f8fSGustavo F. Padovan 
402642e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
402742e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
40280a708f8fSGustavo F. Padovan 
402903f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control)) {
4030e2ab4353SGustavo F. Padovan 		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
4031525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
40320a708f8fSGustavo F. Padovan 	} else {
4033525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
40340a708f8fSGustavo F. Padovan 
4035e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_WAIT_F, &chan->conn_state))
4036e2ab4353SGustavo F. Padovan 			set_bit(CONN_REJ_ACT, &chan->conn_state);
40370a708f8fSGustavo F. Padovan 	}
40380a708f8fSGustavo F. Padovan }
403988843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u32 rx_control)
40400a708f8fSGustavo F. Padovan {
40410b209faeSAndrei Emeltchenko 	u16 tx_seq = __get_reqseq(chan, rx_control);
40420a708f8fSGustavo F. Padovan 
404388843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
40440a708f8fSGustavo F. Padovan 
4045e2ab4353SGustavo F. Padovan 	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
40460a708f8fSGustavo F. Padovan 
4047e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control)) {
404842e5c802SGustavo F. Padovan 		chan->expected_ack_seq = tx_seq;
404942e5c802SGustavo F. Padovan 		l2cap_drop_acked_frames(chan);
40500a708f8fSGustavo F. Padovan 
4051e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_FBIT, &chan->conn_state);
4052525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
40530a708f8fSGustavo F. Padovan 
4054525cd185SGustavo F. Padovan 		l2cap_ertm_send(chan);
40550a708f8fSGustavo F. Padovan 
4056e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
40576a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
4058e2ab4353SGustavo F. Padovan 			set_bit(CONN_SREJ_ACT, &chan->conn_state);
40590a708f8fSGustavo F. Padovan 		}
406003f6715dSAndrei Emeltchenko 	} else if (__is_ctrl_final(chan, rx_control)) {
4061e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_SREJ_ACT, &chan->conn_state) &&
40626a026610SGustavo F. Padovan 				chan->srej_save_reqseq == tx_seq)
4063e2ab4353SGustavo F. Padovan 			clear_bit(CONN_SREJ_ACT, &chan->conn_state);
40640a708f8fSGustavo F. Padovan 		else
4065525cd185SGustavo F. Padovan 			l2cap_retransmit_one_frame(chan, tx_seq);
40660a708f8fSGustavo F. Padovan 	} else {
4067525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
4068e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
40696a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
4070e2ab4353SGustavo F. Padovan 			set_bit(CONN_SREJ_ACT, &chan->conn_state);
40710a708f8fSGustavo F. Padovan 		}
40720a708f8fSGustavo F. Padovan 	}
40730a708f8fSGustavo F. Padovan }
40740a708f8fSGustavo F. Padovan 
407588843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u32 rx_control)
40760a708f8fSGustavo F. Padovan {
40770b209faeSAndrei Emeltchenko 	u16 tx_seq = __get_reqseq(chan, rx_control);
40780a708f8fSGustavo F. Padovan 
407988843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
40800a708f8fSGustavo F. Padovan 
4081e2ab4353SGustavo F. Padovan 	set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
408242e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
408342e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
40840a708f8fSGustavo F. Padovan 
4085e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control))
4086e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_FBIT, &chan->conn_state);
40870a708f8fSGustavo F. Padovan 
4088e2ab4353SGustavo F. Padovan 	if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
40891a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
4090e3781735SAndrei Emeltchenko 		if (__is_ctrl_poll(chan, rx_control))
4091525cd185SGustavo F. Padovan 			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
40920a708f8fSGustavo F. Padovan 		return;
40930a708f8fSGustavo F. Padovan 	}
40940a708f8fSGustavo F. Padovan 
4095e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control)) {
4096525cd185SGustavo F. Padovan 		l2cap_send_srejtail(chan);
4097ab784b73SAndrei Emeltchenko 	} else {
4098ab784b73SAndrei Emeltchenko 		rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR);
4099ab784b73SAndrei Emeltchenko 		l2cap_send_sframe(chan, rx_control);
4100ab784b73SAndrei Emeltchenko 	}
41010a708f8fSGustavo F. Padovan }
41020a708f8fSGustavo F. Padovan 
410388843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb)
41040a708f8fSGustavo F. Padovan {
410588843ab0SAndrei Emeltchenko 	BT_DBG("chan %p rx_control 0x%8.8x len %d", chan, rx_control, skb->len);
41060a708f8fSGustavo F. Padovan 
410703f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control) &&
4108e2ab4353SGustavo F. Padovan 			test_bit(CONN_WAIT_F, &chan->conn_state)) {
41091a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
41106a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
41111a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
4112e2ab4353SGustavo F. Padovan 		clear_bit(CONN_WAIT_F, &chan->conn_state);
41130a708f8fSGustavo F. Padovan 	}
41140a708f8fSGustavo F. Padovan 
4115ab784b73SAndrei Emeltchenko 	switch (__get_ctrl_super(chan, rx_control)) {
4116ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_RR:
4117525cd185SGustavo F. Padovan 		l2cap_data_channel_rrframe(chan, rx_control);
41180a708f8fSGustavo F. Padovan 		break;
41190a708f8fSGustavo F. Padovan 
4120ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_REJ:
4121525cd185SGustavo F. Padovan 		l2cap_data_channel_rejframe(chan, rx_control);
41220a708f8fSGustavo F. Padovan 		break;
41230a708f8fSGustavo F. Padovan 
4124ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_SREJ:
4125525cd185SGustavo F. Padovan 		l2cap_data_channel_srejframe(chan, rx_control);
41260a708f8fSGustavo F. Padovan 		break;
41270a708f8fSGustavo F. Padovan 
4128ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_RNR:
4129525cd185SGustavo F. Padovan 		l2cap_data_channel_rnrframe(chan, rx_control);
41300a708f8fSGustavo F. Padovan 		break;
41310a708f8fSGustavo F. Padovan 	}
41320a708f8fSGustavo F. Padovan 
41330a708f8fSGustavo F. Padovan 	kfree_skb(skb);
41340a708f8fSGustavo F. Padovan 	return 0;
41350a708f8fSGustavo F. Padovan }
41360a708f8fSGustavo F. Padovan 
41370a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
41380a708f8fSGustavo F. Padovan {
4139525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
414088843ab0SAndrei Emeltchenko 	u32 control;
41410b209faeSAndrei Emeltchenko 	u16 req_seq;
41420a708f8fSGustavo F. Padovan 	int len, next_tx_seq_offset, req_seq_offset;
41430a708f8fSGustavo F. Padovan 
414488843ab0SAndrei Emeltchenko 	control = __get_control(chan, skb->data);
414588843ab0SAndrei Emeltchenko 	skb_pull(skb, __ctrl_size(chan));
41460a708f8fSGustavo F. Padovan 	len = skb->len;
41470a708f8fSGustavo F. Padovan 
41480a708f8fSGustavo F. Padovan 	/*
41490a708f8fSGustavo F. Padovan 	 * We can just drop the corrupted I-frame here.
41500a708f8fSGustavo F. Padovan 	 * Receiver will miss it and start proper recovery
41510a708f8fSGustavo F. Padovan 	 * procedures and ask retransmission.
41520a708f8fSGustavo F. Padovan 	 */
415347d1ec61SGustavo F. Padovan 	if (l2cap_check_fcs(chan, skb))
41540a708f8fSGustavo F. Padovan 		goto drop;
41550a708f8fSGustavo F. Padovan 
4156793c2f1cSAndrei Emeltchenko 	if (__is_sar_start(chan, control) && !__is_sframe(chan, control))
415703a51213SAndrei Emeltchenko 		len -= L2CAP_SDULEN_SIZE;
41580a708f8fSGustavo F. Padovan 
415947d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
416003a51213SAndrei Emeltchenko 		len -= L2CAP_FCS_SIZE;
41610a708f8fSGustavo F. Padovan 
416247d1ec61SGustavo F. Padovan 	if (len > chan->mps) {
41638c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
41640a708f8fSGustavo F. Padovan 		goto drop;
41650a708f8fSGustavo F. Padovan 	}
41660a708f8fSGustavo F. Padovan 
41670b209faeSAndrei Emeltchenko 	req_seq = __get_reqseq(chan, control);
41680a708f8fSGustavo F. Padovan 
4169836be934SAndrei Emeltchenko 	req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq);
4170836be934SAndrei Emeltchenko 
4171836be934SAndrei Emeltchenko 	next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq,
4172836be934SAndrei Emeltchenko 						chan->expected_ack_seq);
41730a708f8fSGustavo F. Padovan 
41740a708f8fSGustavo F. Padovan 	/* check for invalid req-seq */
41750a708f8fSGustavo F. Padovan 	if (req_seq_offset > next_tx_seq_offset) {
41768c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
41770a708f8fSGustavo F. Padovan 		goto drop;
41780a708f8fSGustavo F. Padovan 	}
41790a708f8fSGustavo F. Padovan 
4180793c2f1cSAndrei Emeltchenko 	if (!__is_sframe(chan, control)) {
41810a708f8fSGustavo F. Padovan 		if (len < 0) {
41828c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
41830a708f8fSGustavo F. Padovan 			goto drop;
41840a708f8fSGustavo F. Padovan 		}
41850a708f8fSGustavo F. Padovan 
4186525cd185SGustavo F. Padovan 		l2cap_data_channel_iframe(chan, control, skb);
41870a708f8fSGustavo F. Padovan 	} else {
41880a708f8fSGustavo F. Padovan 		if (len != 0) {
41890a708f8fSGustavo F. Padovan 			BT_ERR("%d", len);
41908c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
41910a708f8fSGustavo F. Padovan 			goto drop;
41920a708f8fSGustavo F. Padovan 		}
41930a708f8fSGustavo F. Padovan 
4194525cd185SGustavo F. Padovan 		l2cap_data_channel_sframe(chan, control, skb);
41950a708f8fSGustavo F. Padovan 	}
41960a708f8fSGustavo F. Padovan 
41970a708f8fSGustavo F. Padovan 	return 0;
41980a708f8fSGustavo F. Padovan 
41990a708f8fSGustavo F. Padovan drop:
42000a708f8fSGustavo F. Padovan 	kfree_skb(skb);
42010a708f8fSGustavo F. Padovan 	return 0;
42020a708f8fSGustavo F. Padovan }
42030a708f8fSGustavo F. Padovan 
42040a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
42050a708f8fSGustavo F. Padovan {
420648454079SGustavo F. Padovan 	struct l2cap_chan *chan;
4207bf734843SDavid S. Miller 	struct sock *sk = NULL;
420888843ab0SAndrei Emeltchenko 	u32 control;
4209fb45de7dSAndrei Emeltchenko 	u16 tx_seq;
42100a708f8fSGustavo F. Padovan 	int len;
42110a708f8fSGustavo F. Padovan 
4212baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, cid);
421348454079SGustavo F. Padovan 	if (!chan) {
42140a708f8fSGustavo F. Padovan 		BT_DBG("unknown cid 0x%4.4x", cid);
42150a708f8fSGustavo F. Padovan 		goto drop;
42160a708f8fSGustavo F. Padovan 	}
42170a708f8fSGustavo F. Padovan 
421848454079SGustavo F. Padovan 	sk = chan->sk;
42190a708f8fSGustavo F. Padovan 
422049208c9cSGustavo F. Padovan 	BT_DBG("chan %p, len %d", chan, skb->len);
42210a708f8fSGustavo F. Padovan 
422289bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
42230a708f8fSGustavo F. Padovan 		goto drop;
42240a708f8fSGustavo F. Padovan 
42250c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
42260a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
42270a708f8fSGustavo F. Padovan 		/* If socket recv buffers overflows we drop data here
42280a708f8fSGustavo F. Padovan 		 * which is *bad* because L2CAP has to be reliable.
42290a708f8fSGustavo F. Padovan 		 * But we don't have any other choice. L2CAP doesn't
42300a708f8fSGustavo F. Padovan 		 * provide flow control mechanism. */
42310a708f8fSGustavo F. Padovan 
42320c1bc5c6SGustavo F. Padovan 		if (chan->imtu < skb->len)
42330a708f8fSGustavo F. Padovan 			goto drop;
42340a708f8fSGustavo F. Padovan 
423523070494SGustavo F. Padovan 		if (!chan->ops->recv(chan->data, skb))
42360a708f8fSGustavo F. Padovan 			goto done;
42370a708f8fSGustavo F. Padovan 		break;
42380a708f8fSGustavo F. Padovan 
42390a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
42400a708f8fSGustavo F. Padovan 		if (!sock_owned_by_user(sk)) {
42410a708f8fSGustavo F. Padovan 			l2cap_ertm_data_rcv(sk, skb);
42420a708f8fSGustavo F. Padovan 		} else {
42430a708f8fSGustavo F. Padovan 			if (sk_add_backlog(sk, skb))
42440a708f8fSGustavo F. Padovan 				goto drop;
42450a708f8fSGustavo F. Padovan 		}
42460a708f8fSGustavo F. Padovan 
42470a708f8fSGustavo F. Padovan 		goto done;
42480a708f8fSGustavo F. Padovan 
42490a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
425088843ab0SAndrei Emeltchenko 		control = __get_control(chan, skb->data);
425188843ab0SAndrei Emeltchenko 		skb_pull(skb, __ctrl_size(chan));
42520a708f8fSGustavo F. Padovan 		len = skb->len;
42530a708f8fSGustavo F. Padovan 
425447d1ec61SGustavo F. Padovan 		if (l2cap_check_fcs(chan, skb))
42550a708f8fSGustavo F. Padovan 			goto drop;
42560a708f8fSGustavo F. Padovan 
42577e0ef6eeSAndrei Emeltchenko 		if (__is_sar_start(chan, control))
425803a51213SAndrei Emeltchenko 			len -= L2CAP_SDULEN_SIZE;
42590a708f8fSGustavo F. Padovan 
426047d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16)
426103a51213SAndrei Emeltchenko 			len -= L2CAP_FCS_SIZE;
42620a708f8fSGustavo F. Padovan 
4263793c2f1cSAndrei Emeltchenko 		if (len > chan->mps || len < 0 || __is_sframe(chan, control))
42640a708f8fSGustavo F. Padovan 			goto drop;
42650a708f8fSGustavo F. Padovan 
4266fb45de7dSAndrei Emeltchenko 		tx_seq = __get_txseq(chan, control);
42670a708f8fSGustavo F. Padovan 
426884084a31SMat Martineau 		if (chan->expected_tx_seq != tx_seq) {
426984084a31SMat Martineau 			/* Frame(s) missing - must discard partial SDU */
427084084a31SMat Martineau 			kfree_skb(chan->sdu);
427184084a31SMat Martineau 			chan->sdu = NULL;
427284084a31SMat Martineau 			chan->sdu_last_frag = NULL;
427384084a31SMat Martineau 			chan->sdu_len = 0;
427484084a31SMat Martineau 
427584084a31SMat Martineau 			/* TODO: Notify userland of missing data */
427684084a31SMat Martineau 		}
427784084a31SMat Martineau 
4278836be934SAndrei Emeltchenko 		chan->expected_tx_seq = __next_seq(chan, tx_seq);
42790a708f8fSGustavo F. Padovan 
428084084a31SMat Martineau 		if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE)
428184084a31SMat Martineau 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
42820a708f8fSGustavo F. Padovan 
42830a708f8fSGustavo F. Padovan 		goto done;
42840a708f8fSGustavo F. Padovan 
42850a708f8fSGustavo F. Padovan 	default:
42860c1bc5c6SGustavo F. Padovan 		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
42870a708f8fSGustavo F. Padovan 		break;
42880a708f8fSGustavo F. Padovan 	}
42890a708f8fSGustavo F. Padovan 
42900a708f8fSGustavo F. Padovan drop:
42910a708f8fSGustavo F. Padovan 	kfree_skb(skb);
42920a708f8fSGustavo F. Padovan 
42930a708f8fSGustavo F. Padovan done:
42940a708f8fSGustavo F. Padovan 	if (sk)
42950a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
42960a708f8fSGustavo F. Padovan 
42970a708f8fSGustavo F. Padovan 	return 0;
42980a708f8fSGustavo F. Padovan }
42990a708f8fSGustavo F. Padovan 
43000a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
43010a708f8fSGustavo F. Padovan {
43026dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
430323691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
43040a708f8fSGustavo F. Padovan 
430523691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_psm(0, psm, conn->src);
430623691d75SGustavo F. Padovan 	if (!chan)
43070a708f8fSGustavo F. Padovan 		goto drop;
43080a708f8fSGustavo F. Padovan 
430923691d75SGustavo F. Padovan 	sk = chan->sk;
431023691d75SGustavo F. Padovan 
43110a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
43120a708f8fSGustavo F. Padovan 
43130a708f8fSGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
43140a708f8fSGustavo F. Padovan 
431589bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
43160a708f8fSGustavo F. Padovan 		goto drop;
43170a708f8fSGustavo F. Padovan 
4318e13e21dcSVinicius Costa Gomes 	if (chan->imtu < skb->len)
43190a708f8fSGustavo F. Padovan 		goto drop;
43200a708f8fSGustavo F. Padovan 
432123070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
43220a708f8fSGustavo F. Padovan 		goto done;
43230a708f8fSGustavo F. Padovan 
43240a708f8fSGustavo F. Padovan drop:
43250a708f8fSGustavo F. Padovan 	kfree_skb(skb);
43260a708f8fSGustavo F. Padovan 
43270a708f8fSGustavo F. Padovan done:
43280a708f8fSGustavo F. Padovan 	if (sk)
43290a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
43300a708f8fSGustavo F. Padovan 	return 0;
43310a708f8fSGustavo F. Padovan }
43320a708f8fSGustavo F. Padovan 
43339f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb)
43349f69bda6SGustavo F. Padovan {
43356dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
433623691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
43379f69bda6SGustavo F. Padovan 
433823691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_scid(0, cid, conn->src);
433923691d75SGustavo F. Padovan 	if (!chan)
43409f69bda6SGustavo F. Padovan 		goto drop;
43419f69bda6SGustavo F. Padovan 
434223691d75SGustavo F. Padovan 	sk = chan->sk;
434323691d75SGustavo F. Padovan 
43449f69bda6SGustavo F. Padovan 	bh_lock_sock(sk);
43459f69bda6SGustavo F. Padovan 
43469f69bda6SGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
43479f69bda6SGustavo F. Padovan 
434889bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
43499f69bda6SGustavo F. Padovan 		goto drop;
43509f69bda6SGustavo F. Padovan 
4351e13e21dcSVinicius Costa Gomes 	if (chan->imtu < skb->len)
43529f69bda6SGustavo F. Padovan 		goto drop;
43539f69bda6SGustavo F. Padovan 
435423070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
43559f69bda6SGustavo F. Padovan 		goto done;
43569f69bda6SGustavo F. Padovan 
43579f69bda6SGustavo F. Padovan drop:
43589f69bda6SGustavo F. Padovan 	kfree_skb(skb);
43599f69bda6SGustavo F. Padovan 
43609f69bda6SGustavo F. Padovan done:
43619f69bda6SGustavo F. Padovan 	if (sk)
43629f69bda6SGustavo F. Padovan 		bh_unlock_sock(sk);
43639f69bda6SGustavo F. Padovan 	return 0;
43649f69bda6SGustavo F. Padovan }
43659f69bda6SGustavo F. Padovan 
43660a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
43670a708f8fSGustavo F. Padovan {
43680a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh = (void *) skb->data;
43690a708f8fSGustavo F. Padovan 	u16 cid, len;
43700a708f8fSGustavo F. Padovan 	__le16 psm;
43710a708f8fSGustavo F. Padovan 
43720a708f8fSGustavo F. Padovan 	skb_pull(skb, L2CAP_HDR_SIZE);
43730a708f8fSGustavo F. Padovan 	cid = __le16_to_cpu(lh->cid);
43740a708f8fSGustavo F. Padovan 	len = __le16_to_cpu(lh->len);
43750a708f8fSGustavo F. Padovan 
43760a708f8fSGustavo F. Padovan 	if (len != skb->len) {
43770a708f8fSGustavo F. Padovan 		kfree_skb(skb);
43780a708f8fSGustavo F. Padovan 		return;
43790a708f8fSGustavo F. Padovan 	}
43800a708f8fSGustavo F. Padovan 
43810a708f8fSGustavo F. Padovan 	BT_DBG("len %d, cid 0x%4.4x", len, cid);
43820a708f8fSGustavo F. Padovan 
43830a708f8fSGustavo F. Padovan 	switch (cid) {
43843300d9a9SClaudio Takahasi 	case L2CAP_CID_LE_SIGNALING:
43850a708f8fSGustavo F. Padovan 	case L2CAP_CID_SIGNALING:
43860a708f8fSGustavo F. Padovan 		l2cap_sig_channel(conn, skb);
43870a708f8fSGustavo F. Padovan 		break;
43880a708f8fSGustavo F. Padovan 
43890a708f8fSGustavo F. Padovan 	case L2CAP_CID_CONN_LESS:
43900a708f8fSGustavo F. Padovan 		psm = get_unaligned_le16(skb->data);
43910a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
43920a708f8fSGustavo F. Padovan 		l2cap_conless_channel(conn, psm, skb);
43930a708f8fSGustavo F. Padovan 		break;
43940a708f8fSGustavo F. Padovan 
43959f69bda6SGustavo F. Padovan 	case L2CAP_CID_LE_DATA:
43969f69bda6SGustavo F. Padovan 		l2cap_att_channel(conn, cid, skb);
43979f69bda6SGustavo F. Padovan 		break;
43989f69bda6SGustavo F. Padovan 
4399b501d6a1SAnderson Briglia 	case L2CAP_CID_SMP:
4400b501d6a1SAnderson Briglia 		if (smp_sig_channel(conn, skb))
4401b501d6a1SAnderson Briglia 			l2cap_conn_del(conn->hcon, EACCES);
4402b501d6a1SAnderson Briglia 		break;
4403b501d6a1SAnderson Briglia 
44040a708f8fSGustavo F. Padovan 	default:
44050a708f8fSGustavo F. Padovan 		l2cap_data_channel(conn, cid, skb);
44060a708f8fSGustavo F. Padovan 		break;
44070a708f8fSGustavo F. Padovan 	}
44080a708f8fSGustavo F. Padovan }
44090a708f8fSGustavo F. Padovan 
44100a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */
44110a708f8fSGustavo F. Padovan 
44120a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
44130a708f8fSGustavo F. Padovan {
44140a708f8fSGustavo F. Padovan 	int exact = 0, lm1 = 0, lm2 = 0;
441523691d75SGustavo F. Padovan 	struct l2cap_chan *c;
44160a708f8fSGustavo F. Padovan 
44170a708f8fSGustavo F. Padovan 	if (type != ACL_LINK)
44180a708f8fSGustavo F. Padovan 		return -EINVAL;
44190a708f8fSGustavo F. Padovan 
44200a708f8fSGustavo F. Padovan 	BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
44210a708f8fSGustavo F. Padovan 
44220a708f8fSGustavo F. Padovan 	/* Find listening sockets and check their link_mode */
442323691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
442423691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
442523691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
44264343478fSGustavo F. Padovan 
442789bc500eSGustavo F. Padovan 		if (c->state != BT_LISTEN)
44280a708f8fSGustavo F. Padovan 			continue;
44290a708f8fSGustavo F. Padovan 
44300a708f8fSGustavo F. Padovan 		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
44310a708f8fSGustavo F. Padovan 			lm1 |= HCI_LM_ACCEPT;
443243bd0f32SAndrei Emeltchenko 			if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
44330a708f8fSGustavo F. Padovan 				lm1 |= HCI_LM_MASTER;
44340a708f8fSGustavo F. Padovan 			exact++;
44350a708f8fSGustavo F. Padovan 		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
44360a708f8fSGustavo F. Padovan 			lm2 |= HCI_LM_ACCEPT;
443743bd0f32SAndrei Emeltchenko 			if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
44380a708f8fSGustavo F. Padovan 				lm2 |= HCI_LM_MASTER;
44390a708f8fSGustavo F. Padovan 		}
44400a708f8fSGustavo F. Padovan 	}
444123691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
44420a708f8fSGustavo F. Padovan 
44430a708f8fSGustavo F. Padovan 	return exact ? lm1 : lm2;
44440a708f8fSGustavo F. Padovan }
44450a708f8fSGustavo F. Padovan 
44460a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
44470a708f8fSGustavo F. Padovan {
44480a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
44490a708f8fSGustavo F. Padovan 
44500a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
44510a708f8fSGustavo F. Padovan 
4452acd7d370SVille Tervo 	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
44530a708f8fSGustavo F. Padovan 		return -EINVAL;
44540a708f8fSGustavo F. Padovan 
44550a708f8fSGustavo F. Padovan 	if (!status) {
44560a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, status);
44570a708f8fSGustavo F. Padovan 		if (conn)
44580a708f8fSGustavo F. Padovan 			l2cap_conn_ready(conn);
44590a708f8fSGustavo F. Padovan 	} else
4460e175072fSJoe Perches 		l2cap_conn_del(hcon, bt_to_errno(status));
44610a708f8fSGustavo F. Padovan 
44620a708f8fSGustavo F. Padovan 	return 0;
44630a708f8fSGustavo F. Padovan }
44640a708f8fSGustavo F. Padovan 
44650a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon)
44660a708f8fSGustavo F. Padovan {
44670a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
44680a708f8fSGustavo F. Padovan 
44690a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p", hcon);
44700a708f8fSGustavo F. Padovan 
4471b5694506SGustavo F. Padovan 	if ((hcon->type != ACL_LINK && hcon->type != LE_LINK) || !conn)
44729f5a0d7bSAndrei Emeltchenko 		return HCI_ERROR_REMOTE_USER_TERM;
44730a708f8fSGustavo F. Padovan 
44740a708f8fSGustavo F. Padovan 	return conn->disc_reason;
44750a708f8fSGustavo F. Padovan }
44760a708f8fSGustavo F. Padovan 
44770a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
44780a708f8fSGustavo F. Padovan {
44790a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p reason %d", hcon, reason);
44800a708f8fSGustavo F. Padovan 
4481acd7d370SVille Tervo 	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
44820a708f8fSGustavo F. Padovan 		return -EINVAL;
44830a708f8fSGustavo F. Padovan 
4484e175072fSJoe Perches 	l2cap_conn_del(hcon, bt_to_errno(reason));
44850a708f8fSGustavo F. Padovan 
44860a708f8fSGustavo F. Padovan 	return 0;
44870a708f8fSGustavo F. Padovan }
44880a708f8fSGustavo F. Padovan 
44894343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
44900a708f8fSGustavo F. Padovan {
4491715ec005SGustavo F. Padovan 	if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
44920a708f8fSGustavo F. Padovan 		return;
44930a708f8fSGustavo F. Padovan 
44940a708f8fSGustavo F. Padovan 	if (encrypt == 0x00) {
44954343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM) {
4496c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
4497f3f668b0SAndrzej Kaczmarek 			__set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
44984343478fSGustavo F. Padovan 		} else if (chan->sec_level == BT_SECURITY_HIGH)
44990f852724SGustavo F. Padovan 			l2cap_chan_close(chan, ECONNREFUSED);
45000a708f8fSGustavo F. Padovan 	} else {
45014343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM)
4502c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
45030a708f8fSGustavo F. Padovan 	}
45040a708f8fSGustavo F. Padovan }
45050a708f8fSGustavo F. Padovan 
45060a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
45070a708f8fSGustavo F. Padovan {
45080a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
450948454079SGustavo F. Padovan 	struct l2cap_chan *chan;
45100a708f8fSGustavo F. Padovan 
45110a708f8fSGustavo F. Padovan 	if (!conn)
45120a708f8fSGustavo F. Padovan 		return 0;
45130a708f8fSGustavo F. Padovan 
45140a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
45150a708f8fSGustavo F. Padovan 
4516160dc6acSVinicius Costa Gomes 	if (hcon->type == LE_LINK) {
4517160dc6acSVinicius Costa Gomes 		smp_distribute_keys(conn, 0);
4518160dc6acSVinicius Costa Gomes 		del_timer(&conn->security_timer);
4519160dc6acSVinicius Costa Gomes 	}
4520160dc6acSVinicius Costa Gomes 
4521baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
45220a708f8fSGustavo F. Padovan 
4523baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
452448454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
4525baa7e1faSGustavo F. Padovan 
45260a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
45270a708f8fSGustavo F. Padovan 
4528f1cb9af5SVinicius Costa Gomes 		BT_DBG("chan->scid %d", chan->scid);
4529f1cb9af5SVinicius Costa Gomes 
4530f1cb9af5SVinicius Costa Gomes 		if (chan->scid == L2CAP_CID_LE_DATA) {
4531f1cb9af5SVinicius Costa Gomes 			if (!status && encrypt) {
4532f1cb9af5SVinicius Costa Gomes 				chan->sec_level = hcon->sec_level;
4533f1cb9af5SVinicius Costa Gomes 				l2cap_chan_ready(sk);
4534f1cb9af5SVinicius Costa Gomes 			}
4535f1cb9af5SVinicius Costa Gomes 
4536f1cb9af5SVinicius Costa Gomes 			bh_unlock_sock(sk);
4537f1cb9af5SVinicius Costa Gomes 			continue;
4538f1cb9af5SVinicius Costa Gomes 		}
4539f1cb9af5SVinicius Costa Gomes 
4540c1360a1cSGustavo F. Padovan 		if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) {
45410a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
45420a708f8fSGustavo F. Padovan 			continue;
45430a708f8fSGustavo F. Padovan 		}
45440a708f8fSGustavo F. Padovan 
454589bc500eSGustavo F. Padovan 		if (!status && (chan->state == BT_CONNECTED ||
454689bc500eSGustavo F. Padovan 						chan->state == BT_CONFIG)) {
45474343478fSGustavo F. Padovan 			l2cap_check_encryption(chan, encrypt);
45480a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
45490a708f8fSGustavo F. Padovan 			continue;
45500a708f8fSGustavo F. Padovan 		}
45510a708f8fSGustavo F. Padovan 
455289bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
45530a708f8fSGustavo F. Padovan 			if (!status) {
45540a708f8fSGustavo F. Padovan 				struct l2cap_conn_req req;
4555fe4128e0SGustavo F. Padovan 				req.scid = cpu_to_le16(chan->scid);
4556fe4128e0SGustavo F. Padovan 				req.psm  = chan->psm;
45570a708f8fSGustavo F. Padovan 
4558fc7f8a7eSGustavo F. Padovan 				chan->ident = l2cap_get_ident(conn);
4559c1360a1cSGustavo F. Padovan 				set_bit(CONF_CONNECT_PEND, &chan->conf_state);
45600a708f8fSGustavo F. Padovan 
4561fc7f8a7eSGustavo F. Padovan 				l2cap_send_cmd(conn, chan->ident,
45620a708f8fSGustavo F. Padovan 					L2CAP_CONN_REQ, sizeof(req), &req);
45630a708f8fSGustavo F. Padovan 			} else {
4564c9b66675SGustavo F. Padovan 				__clear_chan_timer(chan);
4565f3f668b0SAndrzej Kaczmarek 				__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
45660a708f8fSGustavo F. Padovan 			}
456789bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
45680a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
4569df3c3931SJohan Hedberg 			__u16 res, stat;
45700a708f8fSGustavo F. Padovan 
45710a708f8fSGustavo F. Padovan 			if (!status) {
4572df3c3931SJohan Hedberg 				if (bt_sk(sk)->defer_setup) {
4573df3c3931SJohan Hedberg 					struct sock *parent = bt_sk(sk)->parent;
4574df3c3931SJohan Hedberg 					res = L2CAP_CR_PEND;
4575df3c3931SJohan Hedberg 					stat = L2CAP_CS_AUTHOR_PEND;
457605e9a2f6SIlia Kolomisnky 					if (parent)
4577df3c3931SJohan Hedberg 						parent->sk_data_ready(parent, 0);
4578df3c3931SJohan Hedberg 				} else {
457989bc500eSGustavo F. Padovan 					l2cap_state_change(chan, BT_CONFIG);
4580df3c3931SJohan Hedberg 					res = L2CAP_CR_SUCCESS;
4581df3c3931SJohan Hedberg 					stat = L2CAP_CS_NO_INFO;
4582df3c3931SJohan Hedberg 				}
45830a708f8fSGustavo F. Padovan 			} else {
458489bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_DISCONN);
4585f3f668b0SAndrzej Kaczmarek 				__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
4586df3c3931SJohan Hedberg 				res = L2CAP_CR_SEC_BLOCK;
4587df3c3931SJohan Hedberg 				stat = L2CAP_CS_NO_INFO;
45880a708f8fSGustavo F. Padovan 			}
45890a708f8fSGustavo F. Padovan 
4590fe4128e0SGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4591fe4128e0SGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
4592df3c3931SJohan Hedberg 			rsp.result = cpu_to_le16(res);
4593df3c3931SJohan Hedberg 			rsp.status = cpu_to_le16(stat);
4594fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4595fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
45960a708f8fSGustavo F. Padovan 		}
45970a708f8fSGustavo F. Padovan 
45980a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
45990a708f8fSGustavo F. Padovan 	}
46000a708f8fSGustavo F. Padovan 
4601baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
46020a708f8fSGustavo F. Padovan 
46030a708f8fSGustavo F. Padovan 	return 0;
46040a708f8fSGustavo F. Padovan }
46050a708f8fSGustavo F. Padovan 
46060a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
46070a708f8fSGustavo F. Padovan {
46080a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
46090a708f8fSGustavo F. Padovan 
46100a708f8fSGustavo F. Padovan 	if (!conn)
46110a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, 0);
46120a708f8fSGustavo F. Padovan 
46130a708f8fSGustavo F. Padovan 	if (!conn)
46140a708f8fSGustavo F. Padovan 		goto drop;
46150a708f8fSGustavo F. Padovan 
46160a708f8fSGustavo F. Padovan 	BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
46170a708f8fSGustavo F. Padovan 
46180a708f8fSGustavo F. Padovan 	if (!(flags & ACL_CONT)) {
46190a708f8fSGustavo F. Padovan 		struct l2cap_hdr *hdr;
462048454079SGustavo F. Padovan 		struct l2cap_chan *chan;
46210a708f8fSGustavo F. Padovan 		u16 cid;
46220a708f8fSGustavo F. Padovan 		int len;
46230a708f8fSGustavo F. Padovan 
46240a708f8fSGustavo F. Padovan 		if (conn->rx_len) {
46250a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected start frame (len %d)", skb->len);
46260a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
46270a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
46280a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
46290a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46300a708f8fSGustavo F. Padovan 		}
46310a708f8fSGustavo F. Padovan 
46320a708f8fSGustavo F. Padovan 		/* Start fragment always begin with Basic L2CAP header */
46330a708f8fSGustavo F. Padovan 		if (skb->len < L2CAP_HDR_SIZE) {
46340a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too short (len %d)", skb->len);
46350a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46360a708f8fSGustavo F. Padovan 			goto drop;
46370a708f8fSGustavo F. Padovan 		}
46380a708f8fSGustavo F. Padovan 
46390a708f8fSGustavo F. Padovan 		hdr = (struct l2cap_hdr *) skb->data;
46400a708f8fSGustavo F. Padovan 		len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
46410a708f8fSGustavo F. Padovan 		cid = __le16_to_cpu(hdr->cid);
46420a708f8fSGustavo F. Padovan 
46430a708f8fSGustavo F. Padovan 		if (len == skb->len) {
46440a708f8fSGustavo F. Padovan 			/* Complete frame received */
46450a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, skb);
46460a708f8fSGustavo F. Padovan 			return 0;
46470a708f8fSGustavo F. Padovan 		}
46480a708f8fSGustavo F. Padovan 
46490a708f8fSGustavo F. Padovan 		BT_DBG("Start: total len %d, frag len %d", len, skb->len);
46500a708f8fSGustavo F. Padovan 
46510a708f8fSGustavo F. Padovan 		if (skb->len > len) {
46520a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too long (len %d, expected len %d)",
46530a708f8fSGustavo F. Padovan 				skb->len, len);
46540a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46550a708f8fSGustavo F. Padovan 			goto drop;
46560a708f8fSGustavo F. Padovan 		}
46570a708f8fSGustavo F. Padovan 
4658baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, cid);
46590a708f8fSGustavo F. Padovan 
466048454079SGustavo F. Padovan 		if (chan && chan->sk) {
466148454079SGustavo F. Padovan 			struct sock *sk = chan->sk;
466248454079SGustavo F. Padovan 
46630c1bc5c6SGustavo F. Padovan 			if (chan->imtu < len - L2CAP_HDR_SIZE) {
466448454079SGustavo F. Padovan 				BT_ERR("Frame exceeding recv MTU (len %d, "
466548454079SGustavo F. Padovan 							"MTU %d)", len,
46660c1bc5c6SGustavo F. Padovan 							chan->imtu);
46670a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
46680a708f8fSGustavo F. Padovan 				l2cap_conn_unreliable(conn, ECOMM);
46690a708f8fSGustavo F. Padovan 				goto drop;
46700a708f8fSGustavo F. Padovan 			}
46710a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
467248454079SGustavo F. Padovan 		}
46730a708f8fSGustavo F. Padovan 
46740a708f8fSGustavo F. Padovan 		/* Allocate skb for the complete frame (with header) */
46750a708f8fSGustavo F. Padovan 		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
46760a708f8fSGustavo F. Padovan 		if (!conn->rx_skb)
46770a708f8fSGustavo F. Padovan 			goto drop;
46780a708f8fSGustavo F. Padovan 
46790a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
46800a708f8fSGustavo F. Padovan 								skb->len);
46810a708f8fSGustavo F. Padovan 		conn->rx_len = len - skb->len;
46820a708f8fSGustavo F. Padovan 	} else {
46830a708f8fSGustavo F. Padovan 		BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
46840a708f8fSGustavo F. Padovan 
46850a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
46860a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected continuation frame (len %d)", skb->len);
46870a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46880a708f8fSGustavo F. Padovan 			goto drop;
46890a708f8fSGustavo F. Padovan 		}
46900a708f8fSGustavo F. Padovan 
46910a708f8fSGustavo F. Padovan 		if (skb->len > conn->rx_len) {
46920a708f8fSGustavo F. Padovan 			BT_ERR("Fragment is too long (len %d, expected %d)",
46930a708f8fSGustavo F. Padovan 					skb->len, conn->rx_len);
46940a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
46950a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
46960a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
46970a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
46980a708f8fSGustavo F. Padovan 			goto drop;
46990a708f8fSGustavo F. Padovan 		}
47000a708f8fSGustavo F. Padovan 
47010a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
47020a708f8fSGustavo F. Padovan 								skb->len);
47030a708f8fSGustavo F. Padovan 		conn->rx_len -= skb->len;
47040a708f8fSGustavo F. Padovan 
47050a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
47060a708f8fSGustavo F. Padovan 			/* Complete frame received */
47070a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, conn->rx_skb);
47080a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
47090a708f8fSGustavo F. Padovan 		}
47100a708f8fSGustavo F. Padovan 	}
47110a708f8fSGustavo F. Padovan 
47120a708f8fSGustavo F. Padovan drop:
47130a708f8fSGustavo F. Padovan 	kfree_skb(skb);
47140a708f8fSGustavo F. Padovan 	return 0;
47150a708f8fSGustavo F. Padovan }
47160a708f8fSGustavo F. Padovan 
47170a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p)
47180a708f8fSGustavo F. Padovan {
471923691d75SGustavo F. Padovan 	struct l2cap_chan *c;
47200a708f8fSGustavo F. Padovan 
472123691d75SGustavo F. Padovan 	read_lock_bh(&chan_list_lock);
47220a708f8fSGustavo F. Padovan 
472323691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
472423691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
47250a708f8fSGustavo F. Padovan 
4726903d343eSGustavo F. Padovan 		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
47270a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->src),
47280a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->dst),
472989bc500eSGustavo F. Padovan 					c->state, __le16_to_cpu(c->psm),
473023691d75SGustavo F. Padovan 					c->scid, c->dcid, c->imtu, c->omtu,
473123691d75SGustavo F. Padovan 					c->sec_level, c->mode);
47320a708f8fSGustavo F. Padovan }
47330a708f8fSGustavo F. Padovan 
473423691d75SGustavo F. Padovan 	read_unlock_bh(&chan_list_lock);
47350a708f8fSGustavo F. Padovan 
47360a708f8fSGustavo F. Padovan 	return 0;
47370a708f8fSGustavo F. Padovan }
47380a708f8fSGustavo F. Padovan 
47390a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file)
47400a708f8fSGustavo F. Padovan {
47410a708f8fSGustavo F. Padovan 	return single_open(file, l2cap_debugfs_show, inode->i_private);
47420a708f8fSGustavo F. Padovan }
47430a708f8fSGustavo F. Padovan 
47440a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = {
47450a708f8fSGustavo F. Padovan 	.open		= l2cap_debugfs_open,
47460a708f8fSGustavo F. Padovan 	.read		= seq_read,
47470a708f8fSGustavo F. Padovan 	.llseek		= seq_lseek,
47480a708f8fSGustavo F. Padovan 	.release	= single_release,
47490a708f8fSGustavo F. Padovan };
47500a708f8fSGustavo F. Padovan 
47510a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs;
47520a708f8fSGustavo F. Padovan 
47530a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = {
47540a708f8fSGustavo F. Padovan 	.name		= "L2CAP",
47550a708f8fSGustavo F. Padovan 	.id		= HCI_PROTO_L2CAP,
47560a708f8fSGustavo F. Padovan 	.connect_ind	= l2cap_connect_ind,
47570a708f8fSGustavo F. Padovan 	.connect_cfm	= l2cap_connect_cfm,
47580a708f8fSGustavo F. Padovan 	.disconn_ind	= l2cap_disconn_ind,
47590a708f8fSGustavo F. Padovan 	.disconn_cfm	= l2cap_disconn_cfm,
47600a708f8fSGustavo F. Padovan 	.security_cfm	= l2cap_security_cfm,
47610a708f8fSGustavo F. Padovan 	.recv_acldata	= l2cap_recv_acldata
47620a708f8fSGustavo F. Padovan };
47630a708f8fSGustavo F. Padovan 
476464274518SGustavo F. Padovan int __init l2cap_init(void)
47650a708f8fSGustavo F. Padovan {
47660a708f8fSGustavo F. Padovan 	int err;
47670a708f8fSGustavo F. Padovan 
4768bb58f747SGustavo F. Padovan 	err = l2cap_init_sockets();
47690a708f8fSGustavo F. Padovan 	if (err < 0)
47700a708f8fSGustavo F. Padovan 		return err;
47710a708f8fSGustavo F. Padovan 
47720a708f8fSGustavo F. Padovan 	err = hci_register_proto(&l2cap_hci_proto);
47730a708f8fSGustavo F. Padovan 	if (err < 0) {
47740a708f8fSGustavo F. Padovan 		BT_ERR("L2CAP protocol registration failed");
47750a708f8fSGustavo F. Padovan 		bt_sock_unregister(BTPROTO_L2CAP);
47760a708f8fSGustavo F. Padovan 		goto error;
47770a708f8fSGustavo F. Padovan 	}
47780a708f8fSGustavo F. Padovan 
47790a708f8fSGustavo F. Padovan 	if (bt_debugfs) {
47800a708f8fSGustavo F. Padovan 		l2cap_debugfs = debugfs_create_file("l2cap", 0444,
47810a708f8fSGustavo F. Padovan 					bt_debugfs, NULL, &l2cap_debugfs_fops);
47820a708f8fSGustavo F. Padovan 		if (!l2cap_debugfs)
47830a708f8fSGustavo F. Padovan 			BT_ERR("Failed to create L2CAP debug file");
47840a708f8fSGustavo F. Padovan 	}
47850a708f8fSGustavo F. Padovan 
47860a708f8fSGustavo F. Padovan 	return 0;
47870a708f8fSGustavo F. Padovan 
47880a708f8fSGustavo F. Padovan error:
4789bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
47900a708f8fSGustavo F. Padovan 	return err;
47910a708f8fSGustavo F. Padovan }
47920a708f8fSGustavo F. Padovan 
479364274518SGustavo F. Padovan void l2cap_exit(void)
47940a708f8fSGustavo F. Padovan {
47950a708f8fSGustavo F. Padovan 	debugfs_remove(l2cap_debugfs);
47960a708f8fSGustavo F. Padovan 
47970a708f8fSGustavo F. Padovan 	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
47980a708f8fSGustavo F. Padovan 		BT_ERR("L2CAP protocol unregistration failed");
47990a708f8fSGustavo F. Padovan 
4800bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
48010a708f8fSGustavo F. Padovan }
48020a708f8fSGustavo F. Padovan 
48030a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644);
48040a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
4805