xref: /openbmc/linux/net/bluetooth/l2cap_core.c (revision f1cb9af5)
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;
620a708f8fSGustavo F. Padovan static u8 l2cap_fixed_chan[8] = { 0x02, };
630a708f8fSGustavo F. Padovan 
640a708f8fSGustavo F. Padovan static struct workqueue_struct *_busy_wq;
650a708f8fSGustavo F. Padovan 
66b5ad8b7fSJohannes Berg static LIST_HEAD(chan_list);
67b5ad8b7fSJohannes Berg static DEFINE_RWLOCK(chan_list_lock);
680a708f8fSGustavo F. Padovan 
690a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work);
700a708f8fSGustavo F. Padovan 
710a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
720a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data);
734519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
744519de9aSGustavo F. Padovan 								void *data);
75710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
764519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn,
774519de9aSGustavo F. Padovan 				struct l2cap_chan *chan, int err);
780a708f8fSGustavo F. Padovan 
790a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb);
800a708f8fSGustavo F. Padovan 
810a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */
8271ba0e56SGustavo F. Padovan 
8371ba0e56SGustavo F. Padovan static inline void chan_hold(struct l2cap_chan *c)
8471ba0e56SGustavo F. Padovan {
8571ba0e56SGustavo F. Padovan 	atomic_inc(&c->refcnt);
8671ba0e56SGustavo F. Padovan }
8771ba0e56SGustavo F. Padovan 
8871ba0e56SGustavo F. Padovan static inline void chan_put(struct l2cap_chan *c)
8971ba0e56SGustavo F. Padovan {
9071ba0e56SGustavo F. Padovan 	if (atomic_dec_and_test(&c->refcnt))
9171ba0e56SGustavo F. Padovan 		kfree(c);
9271ba0e56SGustavo F. Padovan }
9371ba0e56SGustavo F. Padovan 
94baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
950a708f8fSGustavo F. Padovan {
9648454079SGustavo F. Padovan 	struct l2cap_chan *c;
97baa7e1faSGustavo F. Padovan 
98baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
99fe4128e0SGustavo F. Padovan 		if (c->dcid == cid)
10048454079SGustavo F. Padovan 			return c;
1010a708f8fSGustavo F. Padovan 	}
102baa7e1faSGustavo F. Padovan 	return NULL;
103baa7e1faSGustavo F. Padovan 
104baa7e1faSGustavo F. Padovan }
1050a708f8fSGustavo F. Padovan 
106baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
1070a708f8fSGustavo F. Padovan {
10848454079SGustavo F. Padovan 	struct l2cap_chan *c;
109baa7e1faSGustavo F. Padovan 
110baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
111fe4128e0SGustavo F. Padovan 		if (c->scid == cid)
11248454079SGustavo F. Padovan 			return c;
1130a708f8fSGustavo F. Padovan 	}
114baa7e1faSGustavo F. Padovan 	return NULL;
115baa7e1faSGustavo F. Padovan }
1160a708f8fSGustavo F. Padovan 
1170a708f8fSGustavo F. Padovan /* Find channel with given SCID.
1180a708f8fSGustavo F. Padovan  * Returns locked socket */
119baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
1200a708f8fSGustavo F. Padovan {
12148454079SGustavo F. Padovan 	struct l2cap_chan *c;
122baa7e1faSGustavo F. Padovan 
123baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
124baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_scid(conn, cid);
12548454079SGustavo F. Padovan 	if (c)
12648454079SGustavo F. Padovan 		bh_lock_sock(c->sk);
127baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
12848454079SGustavo F. Padovan 	return c;
1290a708f8fSGustavo F. Padovan }
1300a708f8fSGustavo F. Padovan 
131baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1320a708f8fSGustavo F. Padovan {
13348454079SGustavo F. Padovan 	struct l2cap_chan *c;
134baa7e1faSGustavo F. Padovan 
135baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
136fc7f8a7eSGustavo F. Padovan 		if (c->ident == ident)
13748454079SGustavo F. Padovan 			return c;
1380a708f8fSGustavo F. Padovan 	}
139baa7e1faSGustavo F. Padovan 	return NULL;
140baa7e1faSGustavo F. Padovan }
1410a708f8fSGustavo F. Padovan 
142baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1430a708f8fSGustavo F. Padovan {
14448454079SGustavo F. Padovan 	struct l2cap_chan *c;
145baa7e1faSGustavo F. Padovan 
146baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
147baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_ident(conn, ident);
14848454079SGustavo F. Padovan 	if (c)
14948454079SGustavo F. Padovan 		bh_lock_sock(c->sk);
150baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
15148454079SGustavo F. Padovan 	return c;
1520a708f8fSGustavo F. Padovan }
1530a708f8fSGustavo F. Padovan 
15423691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
1559e4425ffSGustavo F. Padovan {
15623691d75SGustavo F. Padovan 	struct l2cap_chan *c;
1579e4425ffSGustavo F. Padovan 
15823691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
15923691d75SGustavo F. Padovan 		if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src))
1609e4425ffSGustavo F. Padovan 			goto found;
1619e4425ffSGustavo F. Padovan 	}
1629e4425ffSGustavo F. Padovan 
16323691d75SGustavo F. Padovan 	c = NULL;
1649e4425ffSGustavo F. Padovan found:
16523691d75SGustavo F. Padovan 	return c;
1669e4425ffSGustavo F. Padovan }
1679e4425ffSGustavo F. Padovan 
1689e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
1699e4425ffSGustavo F. Padovan {
17073b2ec18SGustavo F. Padovan 	int err;
17173b2ec18SGustavo F. Padovan 
17223691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
1739e4425ffSGustavo F. Padovan 
17423691d75SGustavo F. Padovan 	if (psm && __l2cap_global_chan_by_addr(psm, src)) {
17573b2ec18SGustavo F. Padovan 		err = -EADDRINUSE;
17673b2ec18SGustavo F. Padovan 		goto done;
1779e4425ffSGustavo F. Padovan 	}
1789e4425ffSGustavo F. Padovan 
17973b2ec18SGustavo F. Padovan 	if (psm) {
1809e4425ffSGustavo F. Padovan 		chan->psm = psm;
1819e4425ffSGustavo F. Padovan 		chan->sport = psm;
18273b2ec18SGustavo F. Padovan 		err = 0;
18373b2ec18SGustavo F. Padovan 	} else {
18473b2ec18SGustavo F. Padovan 		u16 p;
1859e4425ffSGustavo F. Padovan 
18673b2ec18SGustavo F. Padovan 		err = -EINVAL;
18773b2ec18SGustavo F. Padovan 		for (p = 0x1001; p < 0x1100; p += 2)
18823691d75SGustavo F. Padovan 			if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
18973b2ec18SGustavo F. Padovan 				chan->psm   = cpu_to_le16(p);
19073b2ec18SGustavo F. Padovan 				chan->sport = cpu_to_le16(p);
19173b2ec18SGustavo F. Padovan 				err = 0;
19273b2ec18SGustavo F. Padovan 				break;
19373b2ec18SGustavo F. Padovan 			}
19473b2ec18SGustavo F. Padovan 	}
19573b2ec18SGustavo F. Padovan 
19673b2ec18SGustavo F. Padovan done:
19723691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
19873b2ec18SGustavo F. Padovan 	return err;
1999e4425ffSGustavo F. Padovan }
2009e4425ffSGustavo F. Padovan 
2019e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid)
2029e4425ffSGustavo F. Padovan {
20323691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
2049e4425ffSGustavo F. Padovan 
2059e4425ffSGustavo F. Padovan 	chan->scid = scid;
2069e4425ffSGustavo F. Padovan 
20723691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
2089e4425ffSGustavo F. Padovan 
2099e4425ffSGustavo F. Padovan 	return 0;
2109e4425ffSGustavo F. Padovan }
2119e4425ffSGustavo F. Padovan 
212baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
2130a708f8fSGustavo F. Padovan {
2140a708f8fSGustavo F. Padovan 	u16 cid = L2CAP_CID_DYN_START;
2150a708f8fSGustavo F. Padovan 
2160a708f8fSGustavo F. Padovan 	for (; cid < L2CAP_CID_DYN_END; cid++) {
217baa7e1faSGustavo F. Padovan 		if (!__l2cap_get_chan_by_scid(conn, cid))
2180a708f8fSGustavo F. Padovan 			return cid;
2190a708f8fSGustavo F. Padovan 	}
2200a708f8fSGustavo F. Padovan 
2210a708f8fSGustavo F. Padovan 	return 0;
2220a708f8fSGustavo F. Padovan }
2230a708f8fSGustavo F. Padovan 
224c9b66675SGustavo F. Padovan static void l2cap_set_timer(struct l2cap_chan *chan, struct timer_list *timer, long timeout)
225ab07801dSGustavo F. Padovan {
22689bc500eSGustavo F. Padovan        BT_DBG("chan %p state %d timeout %ld", chan->sk, chan->state, timeout);
22789bc500eSGustavo F. Padovan 
228c9b66675SGustavo F. Padovan        if (!mod_timer(timer, jiffies + timeout))
22971ba0e56SGustavo F. Padovan 	       chan_hold(chan);
230ab07801dSGustavo F. Padovan }
231ab07801dSGustavo F. Padovan 
232c9b66675SGustavo F. Padovan static void l2cap_clear_timer(struct l2cap_chan *chan, struct timer_list *timer)
233ab07801dSGustavo F. Padovan {
23489bc500eSGustavo F. Padovan        BT_DBG("chan %p state %d", chan, chan->state);
235ab07801dSGustavo F. Padovan 
236c9b66675SGustavo F. Padovan        if (timer_pending(timer) && del_timer(timer))
23771ba0e56SGustavo F. Padovan 	       chan_put(chan);
238ab07801dSGustavo F. Padovan }
239ab07801dSGustavo F. Padovan 
24089bc500eSGustavo F. Padovan static void l2cap_state_change(struct l2cap_chan *chan, int state)
24189bc500eSGustavo F. Padovan {
24289bc500eSGustavo F. Padovan 	chan->state = state;
24389bc500eSGustavo F. Padovan 	chan->ops->state_change(chan->data, state);
24489bc500eSGustavo F. Padovan }
24589bc500eSGustavo F. Padovan 
246ab07801dSGustavo F. Padovan static void l2cap_chan_timeout(unsigned long arg)
247ab07801dSGustavo F. Padovan {
248ab07801dSGustavo F. Padovan 	struct l2cap_chan *chan = (struct l2cap_chan *) arg;
249ab07801dSGustavo F. Padovan 	struct sock *sk = chan->sk;
250ab07801dSGustavo F. Padovan 	int reason;
251ab07801dSGustavo F. Padovan 
25289bc500eSGustavo F. Padovan 	BT_DBG("chan %p state %d", chan, chan->state);
253ab07801dSGustavo F. Padovan 
254ab07801dSGustavo F. Padovan 	bh_lock_sock(sk);
255ab07801dSGustavo F. Padovan 
256ab07801dSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
257ab07801dSGustavo F. Padovan 		/* sk is owned by user. Try again later */
258c9b66675SGustavo F. Padovan 		__set_chan_timer(chan, HZ / 5);
259ab07801dSGustavo F. Padovan 		bh_unlock_sock(sk);
26071ba0e56SGustavo F. Padovan 		chan_put(chan);
261ab07801dSGustavo F. Padovan 		return;
262ab07801dSGustavo F. Padovan 	}
263ab07801dSGustavo F. Padovan 
26489bc500eSGustavo F. Padovan 	if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
265ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
26689bc500eSGustavo F. Padovan 	else if (chan->state == BT_CONNECT &&
267ab07801dSGustavo F. Padovan 					chan->sec_level != BT_SECURITY_SDP)
268ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
269ab07801dSGustavo F. Padovan 	else
270ab07801dSGustavo F. Padovan 		reason = ETIMEDOUT;
271ab07801dSGustavo F. Padovan 
2720f852724SGustavo F. Padovan 	l2cap_chan_close(chan, reason);
273ab07801dSGustavo F. Padovan 
274ab07801dSGustavo F. Padovan 	bh_unlock_sock(sk);
275ab07801dSGustavo F. Padovan 
276ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
27771ba0e56SGustavo F. Padovan 	chan_put(chan);
278ab07801dSGustavo F. Padovan }
279ab07801dSGustavo F. Padovan 
28023691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk)
2810a708f8fSGustavo F. Padovan {
28248454079SGustavo F. Padovan 	struct l2cap_chan *chan;
2830a708f8fSGustavo F. Padovan 
28448454079SGustavo F. Padovan 	chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
28548454079SGustavo F. Padovan 	if (!chan)
28648454079SGustavo F. Padovan 		return NULL;
2870a708f8fSGustavo F. Padovan 
28848454079SGustavo F. Padovan 	chan->sk = sk;
28948454079SGustavo F. Padovan 
29023691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
29123691d75SGustavo F. Padovan 	list_add(&chan->global_l, &chan_list);
29223691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
29323691d75SGustavo F. Padovan 
294ab07801dSGustavo F. Padovan 	setup_timer(&chan->chan_timer, l2cap_chan_timeout, (unsigned long) chan);
295ab07801dSGustavo F. Padovan 
29689bc500eSGustavo F. Padovan 	chan->state = BT_OPEN;
29789bc500eSGustavo F. Padovan 
29871ba0e56SGustavo F. Padovan 	atomic_set(&chan->refcnt, 1);
29971ba0e56SGustavo F. Padovan 
30048454079SGustavo F. Padovan 	return chan;
3010a708f8fSGustavo F. Padovan }
3020a708f8fSGustavo F. Padovan 
30323691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan)
3046ff5abbfSGustavo F. Padovan {
30523691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
30623691d75SGustavo F. Padovan 	list_del(&chan->global_l);
30723691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
30823691d75SGustavo F. Padovan 
30971ba0e56SGustavo F. Padovan 	chan_put(chan);
3106ff5abbfSGustavo F. Padovan }
3116ff5abbfSGustavo F. Padovan 
31248454079SGustavo F. Padovan static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
3130a708f8fSGustavo F. Padovan {
3140a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
315fe4128e0SGustavo F. Padovan 			chan->psm, chan->dcid);
3160a708f8fSGustavo F. Padovan 
3170a708f8fSGustavo F. Padovan 	conn->disc_reason = 0x13;
3180a708f8fSGustavo F. Padovan 
3198c1d787bSGustavo F. Padovan 	chan->conn = conn;
3200a708f8fSGustavo F. Padovan 
321715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
322b62f328bSVille Tervo 		if (conn->hcon->type == LE_LINK) {
323b62f328bSVille Tervo 			/* LE connection */
3240c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_LE_DEFAULT_MTU;
325fe4128e0SGustavo F. Padovan 			chan->scid = L2CAP_CID_LE_DATA;
326fe4128e0SGustavo F. Padovan 			chan->dcid = L2CAP_CID_LE_DATA;
327b62f328bSVille Tervo 		} else {
3280a708f8fSGustavo F. Padovan 			/* Alloc CID for connection-oriented socket */
329fe4128e0SGustavo F. Padovan 			chan->scid = l2cap_alloc_cid(conn);
3300c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_DEFAULT_MTU;
331b62f328bSVille Tervo 		}
332715ec005SGustavo F. Padovan 	} else if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
3330a708f8fSGustavo F. Padovan 		/* Connectionless socket */
334fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_CONN_LESS;
335fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_CONN_LESS;
3360c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3370a708f8fSGustavo F. Padovan 	} else {
3380a708f8fSGustavo F. Padovan 		/* Raw socket can send/recv signalling messages only */
339fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_SIGNALING;
340fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_SIGNALING;
3410c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3420a708f8fSGustavo F. Padovan 	}
3430a708f8fSGustavo F. Padovan 
34471ba0e56SGustavo F. Padovan 	chan_hold(chan);
345baa7e1faSGustavo F. Padovan 
346baa7e1faSGustavo F. Padovan 	list_add(&chan->list, &conn->chan_l);
3470a708f8fSGustavo F. Padovan }
3480a708f8fSGustavo F. Padovan 
3490a708f8fSGustavo F. Padovan /* Delete channel.
3500a708f8fSGustavo F. Padovan  * Must be called on the locked socket. */
3514519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err)
3520a708f8fSGustavo F. Padovan {
35348454079SGustavo F. Padovan 	struct sock *sk = chan->sk;
3548c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
3550a708f8fSGustavo F. Padovan 	struct sock *parent = bt_sk(sk)->parent;
3560a708f8fSGustavo F. Padovan 
357c9b66675SGustavo F. Padovan 	__clear_chan_timer(chan);
3580a708f8fSGustavo F. Padovan 
35949208c9cSGustavo F. Padovan 	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
3600a708f8fSGustavo F. Padovan 
3610a708f8fSGustavo F. Padovan 	if (conn) {
362baa7e1faSGustavo F. Padovan 		/* Delete from channel list */
363baa7e1faSGustavo F. Padovan 		write_lock_bh(&conn->chan_lock);
364baa7e1faSGustavo F. Padovan 		list_del(&chan->list);
365baa7e1faSGustavo F. Padovan 		write_unlock_bh(&conn->chan_lock);
36671ba0e56SGustavo F. Padovan 		chan_put(chan);
367baa7e1faSGustavo F. Padovan 
3688c1d787bSGustavo F. Padovan 		chan->conn = NULL;
3690a708f8fSGustavo F. Padovan 		hci_conn_put(conn->hcon);
3700a708f8fSGustavo F. Padovan 	}
3710a708f8fSGustavo F. Padovan 
37289bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CLOSED);
3730a708f8fSGustavo F. Padovan 	sock_set_flag(sk, SOCK_ZAPPED);
3740a708f8fSGustavo F. Padovan 
3750a708f8fSGustavo F. Padovan 	if (err)
3760a708f8fSGustavo F. Padovan 		sk->sk_err = err;
3770a708f8fSGustavo F. Padovan 
3780a708f8fSGustavo F. Padovan 	if (parent) {
3790a708f8fSGustavo F. Padovan 		bt_accept_unlink(sk);
3800a708f8fSGustavo F. Padovan 		parent->sk_data_ready(parent, 0);
3810a708f8fSGustavo F. Padovan 	} else
3820a708f8fSGustavo F. Padovan 		sk->sk_state_change(sk);
3830a708f8fSGustavo F. Padovan 
384b4450035SGustavo F. Padovan 	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE &&
385b4450035SGustavo F. Padovan 			chan->conf_state & L2CAP_CONF_INPUT_DONE))
3866ff5abbfSGustavo F. Padovan 		return;
3872ead70b8SGustavo F. Padovan 
38858d35f87SGustavo F. Padovan 	skb_queue_purge(&chan->tx_q);
3890a708f8fSGustavo F. Padovan 
3900c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
3910a708f8fSGustavo F. Padovan 		struct srej_list *l, *tmp;
3920a708f8fSGustavo F. Padovan 
3931a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
3941a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
3951a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
3960a708f8fSGustavo F. Padovan 
397f1c6775bSGustavo F. Padovan 		skb_queue_purge(&chan->srej_q);
398f1c6775bSGustavo F. Padovan 		skb_queue_purge(&chan->busy_q);
3990a708f8fSGustavo F. Padovan 
40039d5a3eeSGustavo F. Padovan 		list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
4010a708f8fSGustavo F. Padovan 			list_del(&l->list);
4020a708f8fSGustavo F. Padovan 			kfree(l);
4030a708f8fSGustavo F. Padovan 		}
4040a708f8fSGustavo F. Padovan 	}
4050a708f8fSGustavo F. Padovan }
4060a708f8fSGustavo F. Padovan 
4074519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent)
4084519de9aSGustavo F. Padovan {
4094519de9aSGustavo F. Padovan 	struct sock *sk;
4104519de9aSGustavo F. Padovan 
4114519de9aSGustavo F. Padovan 	BT_DBG("parent %p", parent);
4124519de9aSGustavo F. Padovan 
4134519de9aSGustavo F. Padovan 	/* Close not yet accepted channels */
4140f852724SGustavo F. Padovan 	while ((sk = bt_accept_dequeue(parent, NULL))) {
415ba3bd0eeSGustavo F. Padovan 		struct l2cap_chan *chan = l2cap_pi(sk)->chan;
416c9b66675SGustavo F. Padovan 		__clear_chan_timer(chan);
4170f852724SGustavo F. Padovan 		lock_sock(sk);
418ba3bd0eeSGustavo F. Padovan 		l2cap_chan_close(chan, ECONNRESET);
4190f852724SGustavo F. Padovan 		release_sock(sk);
420ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
4210f852724SGustavo F. Padovan 	}
4224519de9aSGustavo F. Padovan }
4234519de9aSGustavo F. Padovan 
4240f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason)
4254519de9aSGustavo F. Padovan {
4264519de9aSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
4274519de9aSGustavo F. Padovan 	struct sock *sk = chan->sk;
4284519de9aSGustavo F. Padovan 
42989bc500eSGustavo F. Padovan 	BT_DBG("chan %p state %d socket %p", chan, chan->state, sk->sk_socket);
4304519de9aSGustavo F. Padovan 
43189bc500eSGustavo F. Padovan 	switch (chan->state) {
4324519de9aSGustavo F. Padovan 	case BT_LISTEN:
4334519de9aSGustavo F. Padovan 		l2cap_chan_cleanup_listen(sk);
43489bc500eSGustavo F. Padovan 
43589bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CLOSED);
43689bc500eSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
4374519de9aSGustavo F. Padovan 		break;
4384519de9aSGustavo F. Padovan 
4394519de9aSGustavo F. Padovan 	case BT_CONNECTED:
4404519de9aSGustavo F. Padovan 	case BT_CONFIG:
441715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4424519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
443c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
444c9b66675SGustavo F. Padovan 			__set_chan_timer(chan, sk->sk_sndtimeo);
4454519de9aSGustavo F. Padovan 			l2cap_send_disconn_req(conn, chan, reason);
4464519de9aSGustavo F. Padovan 		} else
4474519de9aSGustavo F. Padovan 			l2cap_chan_del(chan, reason);
4484519de9aSGustavo F. Padovan 		break;
4494519de9aSGustavo F. Padovan 
4504519de9aSGustavo F. Padovan 	case BT_CONNECT2:
451715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4524519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
4534519de9aSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
4544519de9aSGustavo F. Padovan 			__u16 result;
4554519de9aSGustavo F. Padovan 
4564519de9aSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup)
4574519de9aSGustavo F. Padovan 				result = L2CAP_CR_SEC_BLOCK;
4584519de9aSGustavo F. Padovan 			else
4594519de9aSGustavo F. Padovan 				result = L2CAP_CR_BAD_PSM;
46089bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_DISCONN);
4614519de9aSGustavo F. Padovan 
4624519de9aSGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4634519de9aSGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
4644519de9aSGustavo F. Padovan 			rsp.result = cpu_to_le16(result);
4654519de9aSGustavo F. Padovan 			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4664519de9aSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4674519de9aSGustavo F. Padovan 							sizeof(rsp), &rsp);
4684519de9aSGustavo F. Padovan 		}
4694519de9aSGustavo F. Padovan 
4704519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
4714519de9aSGustavo F. Padovan 		break;
4724519de9aSGustavo F. Padovan 
4734519de9aSGustavo F. Padovan 	case BT_CONNECT:
4744519de9aSGustavo F. Padovan 	case BT_DISCONN:
4754519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
4764519de9aSGustavo F. Padovan 		break;
4774519de9aSGustavo F. Padovan 
4784519de9aSGustavo F. Padovan 	default:
4794519de9aSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
4804519de9aSGustavo F. Padovan 		break;
4814519de9aSGustavo F. Padovan 	}
4824519de9aSGustavo F. Padovan }
4834519de9aSGustavo F. Padovan 
4844343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
4850a708f8fSGustavo F. Padovan {
486715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_RAW) {
4874343478fSGustavo F. Padovan 		switch (chan->sec_level) {
4880a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
4890a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING_MITM;
4900a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
4910a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING;
4920a708f8fSGustavo F. Padovan 		default:
4930a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
4940a708f8fSGustavo F. Padovan 		}
495fe4128e0SGustavo F. Padovan 	} else if (chan->psm == cpu_to_le16(0x0001)) {
4964343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_LOW)
4974343478fSGustavo F. Padovan 			chan->sec_level = BT_SECURITY_SDP;
4980a708f8fSGustavo F. Padovan 
4994343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_HIGH)
5000a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING_MITM;
5010a708f8fSGustavo F. Padovan 		else
5020a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
5030a708f8fSGustavo F. Padovan 	} else {
5044343478fSGustavo F. Padovan 		switch (chan->sec_level) {
5050a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
5060a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING_MITM;
5070a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
5080a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING;
5090a708f8fSGustavo F. Padovan 		default:
5100a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
5110a708f8fSGustavo F. Padovan 		}
5120a708f8fSGustavo F. Padovan 	}
5130a708f8fSGustavo F. Padovan }
5140a708f8fSGustavo F. Padovan 
5150a708f8fSGustavo F. Padovan /* Service level security */
5164343478fSGustavo F. Padovan static inline int l2cap_check_security(struct l2cap_chan *chan)
5170a708f8fSGustavo F. Padovan {
5188c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
5190a708f8fSGustavo F. Padovan 	__u8 auth_type;
5200a708f8fSGustavo F. Padovan 
5214343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
5220a708f8fSGustavo F. Padovan 
5234343478fSGustavo F. Padovan 	return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
5240a708f8fSGustavo F. Padovan }
5250a708f8fSGustavo F. Padovan 
526b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn)
5270a708f8fSGustavo F. Padovan {
5280a708f8fSGustavo F. Padovan 	u8 id;
5290a708f8fSGustavo F. Padovan 
5300a708f8fSGustavo F. Padovan 	/* Get next available identificator.
5310a708f8fSGustavo F. Padovan 	 *    1 - 128 are used by kernel.
5320a708f8fSGustavo F. Padovan 	 *  129 - 199 are reserved.
5330a708f8fSGustavo F. Padovan 	 *  200 - 254 are used by utilities like l2ping, etc.
5340a708f8fSGustavo F. Padovan 	 */
5350a708f8fSGustavo F. Padovan 
5360a708f8fSGustavo F. Padovan 	spin_lock_bh(&conn->lock);
5370a708f8fSGustavo F. Padovan 
5380a708f8fSGustavo F. Padovan 	if (++conn->tx_ident > 128)
5390a708f8fSGustavo F. Padovan 		conn->tx_ident = 1;
5400a708f8fSGustavo F. Padovan 
5410a708f8fSGustavo F. Padovan 	id = conn->tx_ident;
5420a708f8fSGustavo F. Padovan 
5430a708f8fSGustavo F. Padovan 	spin_unlock_bh(&conn->lock);
5440a708f8fSGustavo F. Padovan 
5450a708f8fSGustavo F. Padovan 	return id;
5460a708f8fSGustavo F. Padovan }
5470a708f8fSGustavo F. Padovan 
5484519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
5490a708f8fSGustavo F. Padovan {
5500a708f8fSGustavo F. Padovan 	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
5510a708f8fSGustavo F. Padovan 	u8 flags;
5520a708f8fSGustavo F. Padovan 
5530a708f8fSGustavo F. Padovan 	BT_DBG("code 0x%2.2x", code);
5540a708f8fSGustavo F. Padovan 
5550a708f8fSGustavo F. Padovan 	if (!skb)
5560a708f8fSGustavo F. Padovan 		return;
5570a708f8fSGustavo F. Padovan 
5580a708f8fSGustavo F. Padovan 	if (lmp_no_flush_capable(conn->hcon->hdev))
5590a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
5600a708f8fSGustavo F. Padovan 	else
5610a708f8fSGustavo F. Padovan 		flags = ACL_START;
5620a708f8fSGustavo F. Padovan 
56314b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
56414b12d0bSJaikumar Ganesh 
5650a708f8fSGustavo F. Padovan 	hci_send_acl(conn->hcon, skb, flags);
5660a708f8fSGustavo F. Padovan }
5670a708f8fSGustavo F. Padovan 
568525cd185SGustavo F. Padovan static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control)
5690a708f8fSGustavo F. Padovan {
5700a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
5710a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
5728c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
5730a708f8fSGustavo F. Padovan 	int count, hlen = L2CAP_HDR_SIZE + 2;
5740a708f8fSGustavo F. Padovan 	u8 flags;
5750a708f8fSGustavo F. Padovan 
57689bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
5770a708f8fSGustavo F. Padovan 		return;
5780a708f8fSGustavo F. Padovan 
57947d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
5800a708f8fSGustavo F. Padovan 		hlen += 2;
5810a708f8fSGustavo F. Padovan 
58249208c9cSGustavo F. Padovan 	BT_DBG("chan %p, control 0x%2.2x", chan, control);
5830a708f8fSGustavo F. Padovan 
5840a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, hlen);
5850a708f8fSGustavo F. Padovan 	control |= L2CAP_CTRL_FRAME_TYPE;
5860a708f8fSGustavo F. Padovan 
587525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
5880a708f8fSGustavo F. Padovan 		control |= L2CAP_CTRL_FINAL;
589525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
5900a708f8fSGustavo F. Padovan 	}
5910a708f8fSGustavo F. Padovan 
592525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SEND_PBIT) {
5930a708f8fSGustavo F. Padovan 		control |= L2CAP_CTRL_POLL;
594525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SEND_PBIT;
5950a708f8fSGustavo F. Padovan 	}
5960a708f8fSGustavo F. Padovan 
5970a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
5980a708f8fSGustavo F. Padovan 	if (!skb)
5990a708f8fSGustavo F. Padovan 		return;
6000a708f8fSGustavo F. Padovan 
6010a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
6020a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
603fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
6040a708f8fSGustavo F. Padovan 	put_unaligned_le16(control, skb_put(skb, 2));
6050a708f8fSGustavo F. Padovan 
60647d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
6070a708f8fSGustavo F. Padovan 		u16 fcs = crc16(0, (u8 *)lh, count - 2);
6080a708f8fSGustavo F. Padovan 		put_unaligned_le16(fcs, skb_put(skb, 2));
6090a708f8fSGustavo F. Padovan 	}
6100a708f8fSGustavo F. Padovan 
6110a708f8fSGustavo F. Padovan 	if (lmp_no_flush_capable(conn->hcon->hdev))
6120a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
6130a708f8fSGustavo F. Padovan 	else
6140a708f8fSGustavo F. Padovan 		flags = ACL_START;
6150a708f8fSGustavo F. Padovan 
61614b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = chan->force_active;
61714b12d0bSJaikumar Ganesh 
6188c1d787bSGustavo F. Padovan 	hci_send_acl(chan->conn->hcon, skb, flags);
6190a708f8fSGustavo F. Padovan }
6200a708f8fSGustavo F. Padovan 
621525cd185SGustavo F. Padovan static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control)
6220a708f8fSGustavo F. Padovan {
623525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
6240a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_NOT_READY;
625525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_RNR_SENT;
6260a708f8fSGustavo F. Padovan 	} else
6270a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_READY;
6280a708f8fSGustavo F. Padovan 
62942e5c802SGustavo F. Padovan 	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
6300a708f8fSGustavo F. Padovan 
631525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
6320a708f8fSGustavo F. Padovan }
6330a708f8fSGustavo F. Padovan 
634b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
6350a708f8fSGustavo F. Padovan {
636b4450035SGustavo F. Padovan 	return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND);
6370a708f8fSGustavo F. Padovan }
6380a708f8fSGustavo F. Padovan 
639fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan)
6400a708f8fSGustavo F. Padovan {
6418c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
6420a708f8fSGustavo F. Padovan 
6430a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
6440a708f8fSGustavo F. Padovan 		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
6450a708f8fSGustavo F. Padovan 			return;
6460a708f8fSGustavo F. Padovan 
6474343478fSGustavo F. Padovan 		if (l2cap_check_security(chan) &&
6484343478fSGustavo F. Padovan 				__l2cap_no_conn_pending(chan)) {
6490a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
650fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
651fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
6520a708f8fSGustavo F. Padovan 
653fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
654b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
6550a708f8fSGustavo F. Padovan 
656fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
657fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
6580a708f8fSGustavo F. Padovan 		}
6590a708f8fSGustavo F. Padovan 	} else {
6600a708f8fSGustavo F. Padovan 		struct l2cap_info_req req;
6610a708f8fSGustavo F. Padovan 		req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
6620a708f8fSGustavo F. Padovan 
6630a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
6640a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
6650a708f8fSGustavo F. Padovan 
6660a708f8fSGustavo F. Padovan 		mod_timer(&conn->info_timer, jiffies +
6670a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
6680a708f8fSGustavo F. Padovan 
6690a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
6700a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
6710a708f8fSGustavo F. Padovan 	}
6720a708f8fSGustavo F. Padovan }
6730a708f8fSGustavo F. Padovan 
6740a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
6750a708f8fSGustavo F. Padovan {
6760a708f8fSGustavo F. Padovan 	u32 local_feat_mask = l2cap_feat_mask;
6770a708f8fSGustavo F. Padovan 	if (!disable_ertm)
6780a708f8fSGustavo F. Padovan 		local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
6790a708f8fSGustavo F. Padovan 
6800a708f8fSGustavo F. Padovan 	switch (mode) {
6810a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
6820a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
6830a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
6840a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
6850a708f8fSGustavo F. Padovan 	default:
6860a708f8fSGustavo F. Padovan 		return 0x00;
6870a708f8fSGustavo F. Padovan 	}
6880a708f8fSGustavo F. Padovan }
6890a708f8fSGustavo F. Padovan 
6904519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
6910a708f8fSGustavo F. Padovan {
692e92c8e70SGustavo F. Padovan 	struct sock *sk;
6930a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req req;
6940a708f8fSGustavo F. Padovan 
6950a708f8fSGustavo F. Padovan 	if (!conn)
6960a708f8fSGustavo F. Padovan 		return;
6970a708f8fSGustavo F. Padovan 
698e92c8e70SGustavo F. Padovan 	sk = chan->sk;
699e92c8e70SGustavo F. Padovan 
7000c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
7011a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
7021a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
7031a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
7040a708f8fSGustavo F. Padovan 	}
7050a708f8fSGustavo F. Padovan 
706fe4128e0SGustavo F. Padovan 	req.dcid = cpu_to_le16(chan->dcid);
707fe4128e0SGustavo F. Padovan 	req.scid = cpu_to_le16(chan->scid);
7080a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn),
7090a708f8fSGustavo F. Padovan 			L2CAP_DISCONN_REQ, sizeof(req), &req);
7100a708f8fSGustavo F. Padovan 
71189bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_DISCONN);
7120a708f8fSGustavo F. Padovan 	sk->sk_err = err;
7130a708f8fSGustavo F. Padovan }
7140a708f8fSGustavo F. Padovan 
7150a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */
7160a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn)
7170a708f8fSGustavo F. Padovan {
718820ffdb3SGustavo F. Padovan 	struct l2cap_chan *chan, *tmp;
7190a708f8fSGustavo F. Padovan 
7200a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
7210a708f8fSGustavo F. Padovan 
722baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
7230a708f8fSGustavo F. Padovan 
724820ffdb3SGustavo F. Padovan 	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
72548454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
726baa7e1faSGustavo F. Padovan 
7270a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
7280a708f8fSGustavo F. Padovan 
729715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7300a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
7310a708f8fSGustavo F. Padovan 			continue;
7320a708f8fSGustavo F. Padovan 		}
7330a708f8fSGustavo F. Padovan 
73489bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
7350a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
7360a708f8fSGustavo F. Padovan 
7374343478fSGustavo F. Padovan 			if (!l2cap_check_security(chan) ||
738b4450035SGustavo F. Padovan 					!__l2cap_no_conn_pending(chan)) {
7390a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7400a708f8fSGustavo F. Padovan 				continue;
7410a708f8fSGustavo F. Padovan 			}
7420a708f8fSGustavo F. Padovan 
7430c1bc5c6SGustavo F. Padovan 			if (!l2cap_mode_supported(chan->mode,
7440a708f8fSGustavo F. Padovan 					conn->feat_mask)
745b4450035SGustavo F. Padovan 					&& chan->conf_state &
7460a708f8fSGustavo F. Padovan 					L2CAP_CONF_STATE2_DEVICE) {
7470f852724SGustavo F. Padovan 				/* l2cap_chan_close() calls list_del(chan)
748820ffdb3SGustavo F. Padovan 				 * so release the lock */
749820ffdb3SGustavo F. Padovan 				read_unlock_bh(&conn->chan_lock);
7500f852724SGustavo F. Padovan 				l2cap_chan_close(chan, ECONNRESET);
751820ffdb3SGustavo F. Padovan 				read_lock_bh(&conn->chan_lock);
7520a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7530a708f8fSGustavo F. Padovan 				continue;
7540a708f8fSGustavo F. Padovan 			}
7550a708f8fSGustavo F. Padovan 
756fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
757fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
7580a708f8fSGustavo F. Padovan 
759fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
760b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
7610a708f8fSGustavo F. Padovan 
762fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
763fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
7640a708f8fSGustavo F. Padovan 
76589bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
7660a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
7670a708f8fSGustavo F. Padovan 			char buf[128];
768fe4128e0SGustavo F. Padovan 			rsp.scid = cpu_to_le16(chan->dcid);
769fe4128e0SGustavo F. Padovan 			rsp.dcid = cpu_to_le16(chan->scid);
7700a708f8fSGustavo F. Padovan 
7714343478fSGustavo F. Padovan 			if (l2cap_check_security(chan)) {
7720a708f8fSGustavo F. Padovan 				if (bt_sk(sk)->defer_setup) {
7730a708f8fSGustavo F. Padovan 					struct sock *parent = bt_sk(sk)->parent;
7740a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_PEND);
7750a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
7760a708f8fSGustavo F. Padovan 					parent->sk_data_ready(parent, 0);
7770a708f8fSGustavo F. Padovan 
7780a708f8fSGustavo F. Padovan 				} else {
77989bc500eSGustavo F. Padovan 					l2cap_state_change(chan, BT_CONFIG);
7800a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
7810a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
7820a708f8fSGustavo F. Padovan 				}
7830a708f8fSGustavo F. Padovan 			} else {
7840a708f8fSGustavo F. Padovan 				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
7850a708f8fSGustavo F. Padovan 				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
7860a708f8fSGustavo F. Padovan 			}
7870a708f8fSGustavo F. Padovan 
788fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
789fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
7900a708f8fSGustavo F. Padovan 
791b4450035SGustavo F. Padovan 			if (chan->conf_state & L2CAP_CONF_REQ_SENT ||
7920a708f8fSGustavo F. Padovan 					rsp.result != L2CAP_CR_SUCCESS) {
7930a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7940a708f8fSGustavo F. Padovan 				continue;
7950a708f8fSGustavo F. Padovan 			}
7960a708f8fSGustavo F. Padovan 
797b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_REQ_SENT;
7980a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
79973ffa904SGustavo F. Padovan 						l2cap_build_conf_req(chan, buf), buf);
80073ffa904SGustavo F. Padovan 			chan->num_conf_req++;
8010a708f8fSGustavo F. Padovan 		}
8020a708f8fSGustavo F. Padovan 
8030a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
8040a708f8fSGustavo F. Padovan 	}
8050a708f8fSGustavo F. Padovan 
806baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
8070a708f8fSGustavo F. Padovan }
8080a708f8fSGustavo F. Padovan 
809b62f328bSVille Tervo /* Find socket with cid and source bdaddr.
810b62f328bSVille Tervo  * Returns closest match, locked.
811b62f328bSVille Tervo  */
81223691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src)
813b62f328bSVille Tervo {
81423691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
815b62f328bSVille Tervo 
81623691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
817b62f328bSVille Tervo 
81823691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
81923691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
820fe4128e0SGustavo F. Padovan 
82189bc500eSGustavo F. Padovan 		if (state && c->state != state)
822b62f328bSVille Tervo 			continue;
823b62f328bSVille Tervo 
82423691d75SGustavo F. Padovan 		if (c->scid == cid) {
825b62f328bSVille Tervo 			/* Exact match. */
82623691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
82723691d75SGustavo F. Padovan 				read_unlock(&chan_list_lock);
82823691d75SGustavo F. Padovan 				return c;
82923691d75SGustavo F. Padovan 			}
830b62f328bSVille Tervo 
831b62f328bSVille Tervo 			/* Closest match */
832b62f328bSVille Tervo 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
83323691d75SGustavo F. Padovan 				c1 = c;
834b62f328bSVille Tervo 		}
835b62f328bSVille Tervo 	}
836280f294fSGustavo F. Padovan 
83723691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
838b62f328bSVille Tervo 
83923691d75SGustavo F. Padovan 	return c1;
840b62f328bSVille Tervo }
841b62f328bSVille Tervo 
842b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn)
843b62f328bSVille Tervo {
844c916fbe4SGustavo F. Padovan 	struct sock *parent, *sk;
84523691d75SGustavo F. Padovan 	struct l2cap_chan *chan, *pchan;
846b62f328bSVille Tervo 
847b62f328bSVille Tervo 	BT_DBG("");
848b62f328bSVille Tervo 
849b62f328bSVille Tervo 	/* Check if we have socket listening on cid */
85023691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
851b62f328bSVille Tervo 							conn->src);
85223691d75SGustavo F. Padovan 	if (!pchan)
853b62f328bSVille Tervo 		return;
854b62f328bSVille Tervo 
85523691d75SGustavo F. Padovan 	parent = pchan->sk;
85623691d75SGustavo F. Padovan 
85762f3a2cfSGustavo F. Padovan 	bh_lock_sock(parent);
85862f3a2cfSGustavo F. Padovan 
859b62f328bSVille Tervo 	/* Check for backlog size */
860b62f328bSVille Tervo 	if (sk_acceptq_is_full(parent)) {
861b62f328bSVille Tervo 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
862b62f328bSVille Tervo 		goto clean;
863b62f328bSVille Tervo 	}
864b62f328bSVille Tervo 
86580808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
86680808e43SGustavo F. Padovan 	if (!chan)
867b62f328bSVille Tervo 		goto clean;
868b62f328bSVille Tervo 
86980808e43SGustavo F. Padovan 	sk = chan->sk;
8705d41ce1dSGustavo F. Padovan 
871baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
872b62f328bSVille Tervo 
873b62f328bSVille Tervo 	hci_conn_hold(conn->hcon);
874b62f328bSVille Tervo 
875b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->src, conn->src);
876b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->dst, conn->dst);
877b62f328bSVille Tervo 
878d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
879d1010240SGustavo F. Padovan 
88048454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
88148454079SGustavo F. Padovan 
882c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
883b62f328bSVille Tervo 
88489bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CONNECTED);
885b62f328bSVille Tervo 	parent->sk_data_ready(parent, 0);
886b62f328bSVille Tervo 
887baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
888b62f328bSVille Tervo 
889b62f328bSVille Tervo clean:
890b62f328bSVille Tervo 	bh_unlock_sock(parent);
891b62f328bSVille Tervo }
892b62f328bSVille Tervo 
893f1cb9af5SVinicius Costa Gomes static void l2cap_chan_ready(struct sock *sk)
894f1cb9af5SVinicius Costa Gomes {
895f1cb9af5SVinicius Costa Gomes 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
896f1cb9af5SVinicius Costa Gomes 	struct sock *parent = bt_sk(sk)->parent;
897f1cb9af5SVinicius Costa Gomes 
898f1cb9af5SVinicius Costa Gomes 	BT_DBG("sk %p, parent %p", sk, parent);
899f1cb9af5SVinicius Costa Gomes 
900f1cb9af5SVinicius Costa Gomes 	chan->conf_state = 0;
901f1cb9af5SVinicius Costa Gomes 	__clear_chan_timer(chan);
902f1cb9af5SVinicius Costa Gomes 
903f1cb9af5SVinicius Costa Gomes 	sk->sk_state = BT_CONNECTED;
904f1cb9af5SVinicius Costa Gomes 	sk->sk_state_change(sk);
905f1cb9af5SVinicius Costa Gomes 
906f1cb9af5SVinicius Costa Gomes 	if (parent)
907f1cb9af5SVinicius Costa Gomes 		parent->sk_data_ready(parent, 0);
908f1cb9af5SVinicius Costa Gomes }
909f1cb9af5SVinicius Costa Gomes 
9100a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn)
9110a708f8fSGustavo F. Padovan {
91248454079SGustavo F. Padovan 	struct l2cap_chan *chan;
9130a708f8fSGustavo F. Padovan 
9140a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
9150a708f8fSGustavo F. Padovan 
916b62f328bSVille Tervo 	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
917b62f328bSVille Tervo 		l2cap_le_conn_ready(conn);
918b62f328bSVille Tervo 
919baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
9200a708f8fSGustavo F. Padovan 
921baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
92248454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
923baa7e1faSGustavo F. Padovan 
9240a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
9250a708f8fSGustavo F. Padovan 
926f1cb9af5SVinicius Costa Gomes 		if (conn->hcon->type == LE_LINK)
927b501d6a1SAnderson Briglia 			if (smp_conn_security(conn, chan->sec_level))
928f1cb9af5SVinicius Costa Gomes 				l2cap_chan_ready(sk);
929acd7d370SVille Tervo 
930715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
931c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
93289bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_CONNECTED);
9330a708f8fSGustavo F. Padovan 			sk->sk_state_change(sk);
934b501d6a1SAnderson Briglia 
93589bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT)
936fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
9370a708f8fSGustavo F. Padovan 
9380a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
9390a708f8fSGustavo F. Padovan 	}
9400a708f8fSGustavo F. Padovan 
941baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
9420a708f8fSGustavo F. Padovan }
9430a708f8fSGustavo F. Padovan 
9440a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */
9450a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
9460a708f8fSGustavo F. Padovan {
94748454079SGustavo F. Padovan 	struct l2cap_chan *chan;
9480a708f8fSGustavo F. Padovan 
9490a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
9500a708f8fSGustavo F. Padovan 
951baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
9520a708f8fSGustavo F. Padovan 
953baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
95448454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
955baa7e1faSGustavo F. Padovan 
9564343478fSGustavo F. Padovan 		if (chan->force_reliable)
9570a708f8fSGustavo F. Padovan 			sk->sk_err = err;
9580a708f8fSGustavo F. Padovan 	}
9590a708f8fSGustavo F. Padovan 
960baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
9610a708f8fSGustavo F. Padovan }
9620a708f8fSGustavo F. Padovan 
9630a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg)
9640a708f8fSGustavo F. Padovan {
9650a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = (void *) arg;
9660a708f8fSGustavo F. Padovan 
9670a708f8fSGustavo F. Padovan 	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
9680a708f8fSGustavo F. Padovan 	conn->info_ident = 0;
9690a708f8fSGustavo F. Padovan 
9700a708f8fSGustavo F. Padovan 	l2cap_conn_start(conn);
9710a708f8fSGustavo F. Padovan }
9720a708f8fSGustavo F. Padovan 
9730a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
9740a708f8fSGustavo F. Padovan {
9750a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
9760a708f8fSGustavo F. Padovan 
9770a708f8fSGustavo F. Padovan 	if (conn || status)
9780a708f8fSGustavo F. Padovan 		return conn;
9790a708f8fSGustavo F. Padovan 
9800a708f8fSGustavo F. Padovan 	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
9810a708f8fSGustavo F. Padovan 	if (!conn)
9820a708f8fSGustavo F. Padovan 		return NULL;
9830a708f8fSGustavo F. Padovan 
9840a708f8fSGustavo F. Padovan 	hcon->l2cap_data = conn;
9850a708f8fSGustavo F. Padovan 	conn->hcon = hcon;
9860a708f8fSGustavo F. Padovan 
9870a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p conn %p", hcon, conn);
9880a708f8fSGustavo F. Padovan 
989acd7d370SVille Tervo 	if (hcon->hdev->le_mtu && hcon->type == LE_LINK)
990acd7d370SVille Tervo 		conn->mtu = hcon->hdev->le_mtu;
991acd7d370SVille Tervo 	else
9920a708f8fSGustavo F. Padovan 		conn->mtu = hcon->hdev->acl_mtu;
993acd7d370SVille Tervo 
9940a708f8fSGustavo F. Padovan 	conn->src = &hcon->hdev->bdaddr;
9950a708f8fSGustavo F. Padovan 	conn->dst = &hcon->dst;
9960a708f8fSGustavo F. Padovan 
9970a708f8fSGustavo F. Padovan 	conn->feat_mask = 0;
9980a708f8fSGustavo F. Padovan 
9990a708f8fSGustavo F. Padovan 	spin_lock_init(&conn->lock);
1000baa7e1faSGustavo F. Padovan 	rwlock_init(&conn->chan_lock);
1001baa7e1faSGustavo F. Padovan 
1002baa7e1faSGustavo F. Padovan 	INIT_LIST_HEAD(&conn->chan_l);
10030a708f8fSGustavo F. Padovan 
1004b62f328bSVille Tervo 	if (hcon->type != LE_LINK)
10050a708f8fSGustavo F. Padovan 		setup_timer(&conn->info_timer, l2cap_info_timeout,
10060a708f8fSGustavo F. Padovan 						(unsigned long) conn);
10070a708f8fSGustavo F. Padovan 
10080a708f8fSGustavo F. Padovan 	conn->disc_reason = 0x13;
10090a708f8fSGustavo F. Padovan 
10100a708f8fSGustavo F. Padovan 	return conn;
10110a708f8fSGustavo F. Padovan }
10120a708f8fSGustavo F. Padovan 
10130a708f8fSGustavo F. Padovan static void l2cap_conn_del(struct hci_conn *hcon, int err)
10140a708f8fSGustavo F. Padovan {
10150a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
1016baa7e1faSGustavo F. Padovan 	struct l2cap_chan *chan, *l;
10170a708f8fSGustavo F. Padovan 	struct sock *sk;
10180a708f8fSGustavo F. Padovan 
10190a708f8fSGustavo F. Padovan 	if (!conn)
10200a708f8fSGustavo F. Padovan 		return;
10210a708f8fSGustavo F. Padovan 
10220a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
10230a708f8fSGustavo F. Padovan 
10240a708f8fSGustavo F. Padovan 	kfree_skb(conn->rx_skb);
10250a708f8fSGustavo F. Padovan 
10260a708f8fSGustavo F. Padovan 	/* Kill channels */
1027baa7e1faSGustavo F. Padovan 	list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
102848454079SGustavo F. Padovan 		sk = chan->sk;
10290a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
103048454079SGustavo F. Padovan 		l2cap_chan_del(chan, err);
10310a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
1032ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
10330a708f8fSGustavo F. Padovan 	}
10340a708f8fSGustavo F. Padovan 
10350a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
10360a708f8fSGustavo F. Padovan 		del_timer_sync(&conn->info_timer);
10370a708f8fSGustavo F. Padovan 
10380a708f8fSGustavo F. Padovan 	hcon->l2cap_data = NULL;
10390a708f8fSGustavo F. Padovan 	kfree(conn);
10400a708f8fSGustavo F. Padovan }
10410a708f8fSGustavo F. Padovan 
104248454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
10430a708f8fSGustavo F. Padovan {
1044baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
104548454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
1046baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
10470a708f8fSGustavo F. Padovan }
10480a708f8fSGustavo F. Padovan 
10490a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */
10500a708f8fSGustavo F. Padovan 
10510a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr.
10520a708f8fSGustavo F. Padovan  * Returns closest match.
10530a708f8fSGustavo F. Padovan  */
105423691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src)
10550a708f8fSGustavo F. Padovan {
105623691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
10570a708f8fSGustavo F. Padovan 
105823691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
10590a708f8fSGustavo F. Padovan 
106023691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
106123691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
1062fe4128e0SGustavo F. Padovan 
106389bc500eSGustavo F. Padovan 		if (state && c->state != state)
10640a708f8fSGustavo F. Padovan 			continue;
10650a708f8fSGustavo F. Padovan 
106623691d75SGustavo F. Padovan 		if (c->psm == psm) {
10670a708f8fSGustavo F. Padovan 			/* Exact match. */
106823691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
1069a7567b20SJohannes Berg 				read_unlock(&chan_list_lock);
107023691d75SGustavo F. Padovan 				return c;
107123691d75SGustavo F. Padovan 			}
10720a708f8fSGustavo F. Padovan 
10730a708f8fSGustavo F. Padovan 			/* Closest match */
10740a708f8fSGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
107523691d75SGustavo F. Padovan 				c1 = c;
10760a708f8fSGustavo F. Padovan 		}
10770a708f8fSGustavo F. Padovan 	}
10780a708f8fSGustavo F. Padovan 
107923691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
10800a708f8fSGustavo F. Padovan 
108123691d75SGustavo F. Padovan 	return c1;
10820a708f8fSGustavo F. Padovan }
10830a708f8fSGustavo F. Padovan 
108477a74c7eSGustavo F. Padovan int l2cap_chan_connect(struct l2cap_chan *chan)
10850a708f8fSGustavo F. Padovan {
10865d41ce1dSGustavo F. Padovan 	struct sock *sk = chan->sk;
10870a708f8fSGustavo F. Padovan 	bdaddr_t *src = &bt_sk(sk)->src;
10880a708f8fSGustavo F. Padovan 	bdaddr_t *dst = &bt_sk(sk)->dst;
10890a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
10900a708f8fSGustavo F. Padovan 	struct hci_conn *hcon;
10910a708f8fSGustavo F. Padovan 	struct hci_dev *hdev;
10920a708f8fSGustavo F. Padovan 	__u8 auth_type;
10930a708f8fSGustavo F. Padovan 	int err;
10940a708f8fSGustavo F. Padovan 
10950a708f8fSGustavo F. Padovan 	BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
1096fe4128e0SGustavo F. Padovan 							chan->psm);
10970a708f8fSGustavo F. Padovan 
10980a708f8fSGustavo F. Padovan 	hdev = hci_get_route(dst, src);
10990a708f8fSGustavo F. Padovan 	if (!hdev)
11000a708f8fSGustavo F. Padovan 		return -EHOSTUNREACH;
11010a708f8fSGustavo F. Padovan 
11020a708f8fSGustavo F. Padovan 	hci_dev_lock_bh(hdev);
11030a708f8fSGustavo F. Padovan 
11044343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
11050a708f8fSGustavo F. Padovan 
1106fe4128e0SGustavo F. Padovan 	if (chan->dcid == L2CAP_CID_LE_DATA)
1107acd7d370SVille Tervo 		hcon = hci_connect(hdev, LE_LINK, dst,
11084343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1109acd7d370SVille Tervo 	else
11100a708f8fSGustavo F. Padovan 		hcon = hci_connect(hdev, ACL_LINK, dst,
11114343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1112acd7d370SVille Tervo 
111330e76272SVille Tervo 	if (IS_ERR(hcon)) {
111430e76272SVille Tervo 		err = PTR_ERR(hcon);
11150a708f8fSGustavo F. Padovan 		goto done;
111630e76272SVille Tervo 	}
11170a708f8fSGustavo F. Padovan 
11180a708f8fSGustavo F. Padovan 	conn = l2cap_conn_add(hcon, 0);
11190a708f8fSGustavo F. Padovan 	if (!conn) {
11200a708f8fSGustavo F. Padovan 		hci_conn_put(hcon);
112130e76272SVille Tervo 		err = -ENOMEM;
11220a708f8fSGustavo F. Padovan 		goto done;
11230a708f8fSGustavo F. Padovan 	}
11240a708f8fSGustavo F. Padovan 
11250a708f8fSGustavo F. Padovan 	/* Update source addr of the socket */
11260a708f8fSGustavo F. Padovan 	bacpy(src, conn->src);
11270a708f8fSGustavo F. Padovan 
112848454079SGustavo F. Padovan 	l2cap_chan_add(conn, chan);
112948454079SGustavo F. Padovan 
113089bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CONNECT);
1131c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
11320a708f8fSGustavo F. Padovan 
11330a708f8fSGustavo F. Padovan 	if (hcon->state == BT_CONNECTED) {
1134715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1135c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
11364343478fSGustavo F. Padovan 			if (l2cap_check_security(chan))
113789bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONNECTED);
11380a708f8fSGustavo F. Padovan 		} else
1139fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
11400a708f8fSGustavo F. Padovan 	}
11410a708f8fSGustavo F. Padovan 
114230e76272SVille Tervo 	err = 0;
114330e76272SVille Tervo 
11440a708f8fSGustavo F. Padovan done:
11450a708f8fSGustavo F. Padovan 	hci_dev_unlock_bh(hdev);
11460a708f8fSGustavo F. Padovan 	hci_dev_put(hdev);
11470a708f8fSGustavo F. Padovan 	return err;
11480a708f8fSGustavo F. Padovan }
11490a708f8fSGustavo F. Padovan 
1150dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk)
11510a708f8fSGustavo F. Padovan {
11528c1d787bSGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
11530a708f8fSGustavo F. Padovan 	DECLARE_WAITQUEUE(wait, current);
11540a708f8fSGustavo F. Padovan 	int err = 0;
11550a708f8fSGustavo F. Padovan 	int timeo = HZ/5;
11560a708f8fSGustavo F. Padovan 
11570a708f8fSGustavo F. Padovan 	add_wait_queue(sk_sleep(sk), &wait);
11588c1d787bSGustavo F. Padovan 	while ((chan->unacked_frames > 0 && chan->conn)) {
11590a708f8fSGustavo F. Padovan 		set_current_state(TASK_INTERRUPTIBLE);
11600a708f8fSGustavo F. Padovan 
11610a708f8fSGustavo F. Padovan 		if (!timeo)
11620a708f8fSGustavo F. Padovan 			timeo = HZ/5;
11630a708f8fSGustavo F. Padovan 
11640a708f8fSGustavo F. Padovan 		if (signal_pending(current)) {
11650a708f8fSGustavo F. Padovan 			err = sock_intr_errno(timeo);
11660a708f8fSGustavo F. Padovan 			break;
11670a708f8fSGustavo F. Padovan 		}
11680a708f8fSGustavo F. Padovan 
11690a708f8fSGustavo F. Padovan 		release_sock(sk);
11700a708f8fSGustavo F. Padovan 		timeo = schedule_timeout(timeo);
11710a708f8fSGustavo F. Padovan 		lock_sock(sk);
11720a708f8fSGustavo F. Padovan 
11730a708f8fSGustavo F. Padovan 		err = sock_error(sk);
11740a708f8fSGustavo F. Padovan 		if (err)
11750a708f8fSGustavo F. Padovan 			break;
11760a708f8fSGustavo F. Padovan 	}
11770a708f8fSGustavo F. Padovan 	set_current_state(TASK_RUNNING);
11780a708f8fSGustavo F. Padovan 	remove_wait_queue(sk_sleep(sk), &wait);
11790a708f8fSGustavo F. Padovan 	return err;
11800a708f8fSGustavo F. Padovan }
11810a708f8fSGustavo F. Padovan 
11820a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg)
11830a708f8fSGustavo F. Padovan {
1184525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
1185525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
11860a708f8fSGustavo F. Padovan 
1187525cd185SGustavo F. Padovan 	BT_DBG("chan %p", chan);
11880a708f8fSGustavo F. Padovan 
11890a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
11902c03a7a4SGustavo F. Padovan 	if (chan->retry_count >= chan->remote_max_tx) {
11918c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
11920a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
11930a708f8fSGustavo F. Padovan 		return;
11940a708f8fSGustavo F. Padovan 	}
11950a708f8fSGustavo F. Padovan 
11966a026610SGustavo F. Padovan 	chan->retry_count++;
11971a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
11980a708f8fSGustavo F. Padovan 
1199525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
12000a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
12010a708f8fSGustavo F. Padovan }
12020a708f8fSGustavo F. Padovan 
12030a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg)
12040a708f8fSGustavo F. Padovan {
1205525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
1206525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
12070a708f8fSGustavo F. Padovan 
120849208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
12090a708f8fSGustavo F. Padovan 
12100a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
12116a026610SGustavo F. Padovan 	chan->retry_count = 1;
12121a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
12130a708f8fSGustavo F. Padovan 
1214525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_WAIT_F;
12150a708f8fSGustavo F. Padovan 
1216525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
12170a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
12180a708f8fSGustavo F. Padovan }
12190a708f8fSGustavo F. Padovan 
122042e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
12210a708f8fSGustavo F. Padovan {
12220a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
12230a708f8fSGustavo F. Padovan 
122458d35f87SGustavo F. Padovan 	while ((skb = skb_peek(&chan->tx_q)) &&
12256a026610SGustavo F. Padovan 			chan->unacked_frames) {
122642e5c802SGustavo F. Padovan 		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
12270a708f8fSGustavo F. Padovan 			break;
12280a708f8fSGustavo F. Padovan 
122958d35f87SGustavo F. Padovan 		skb = skb_dequeue(&chan->tx_q);
12300a708f8fSGustavo F. Padovan 		kfree_skb(skb);
12310a708f8fSGustavo F. Padovan 
12326a026610SGustavo F. Padovan 		chan->unacked_frames--;
12330a708f8fSGustavo F. Padovan 	}
12340a708f8fSGustavo F. Padovan 
12356a026610SGustavo F. Padovan 	if (!chan->unacked_frames)
12361a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
12370a708f8fSGustavo F. Padovan }
12380a708f8fSGustavo F. Padovan 
12394343478fSGustavo F. Padovan void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
12400a708f8fSGustavo F. Padovan {
12418c1d787bSGustavo F. Padovan 	struct hci_conn *hcon = chan->conn->hcon;
12420a708f8fSGustavo F. Padovan 	u16 flags;
12430a708f8fSGustavo F. Padovan 
12444343478fSGustavo F. Padovan 	BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len);
12450a708f8fSGustavo F. Padovan 
12464343478fSGustavo F. Padovan 	if (!chan->flushable && lmp_no_flush_capable(hcon->hdev))
12470a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
12480a708f8fSGustavo F. Padovan 	else
12490a708f8fSGustavo F. Padovan 		flags = ACL_START;
12500a708f8fSGustavo F. Padovan 
125114b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = chan->force_active;
12520a708f8fSGustavo F. Padovan 	hci_send_acl(hcon, skb, flags);
12530a708f8fSGustavo F. Padovan }
12540a708f8fSGustavo F. Padovan 
125542e5c802SGustavo F. Padovan void l2cap_streaming_send(struct l2cap_chan *chan)
12560a708f8fSGustavo F. Padovan {
12570a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
12580a708f8fSGustavo F. Padovan 	u16 control, fcs;
12590a708f8fSGustavo F. Padovan 
126058d35f87SGustavo F. Padovan 	while ((skb = skb_dequeue(&chan->tx_q))) {
12610a708f8fSGustavo F. Padovan 		control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE);
126242e5c802SGustavo F. Padovan 		control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT;
12630a708f8fSGustavo F. Padovan 		put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE);
12640a708f8fSGustavo F. Padovan 
126547d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
12660a708f8fSGustavo F. Padovan 			fcs = crc16(0, (u8 *)skb->data, skb->len - 2);
12670a708f8fSGustavo F. Padovan 			put_unaligned_le16(fcs, skb->data + skb->len - 2);
12680a708f8fSGustavo F. Padovan 		}
12690a708f8fSGustavo F. Padovan 
12704343478fSGustavo F. Padovan 		l2cap_do_send(chan, skb);
12710a708f8fSGustavo F. Padovan 
127242e5c802SGustavo F. Padovan 		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
12730a708f8fSGustavo F. Padovan 	}
12740a708f8fSGustavo F. Padovan }
12750a708f8fSGustavo F. Padovan 
1276525cd185SGustavo F. Padovan static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq)
12770a708f8fSGustavo F. Padovan {
12780a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
12790a708f8fSGustavo F. Padovan 	u16 control, fcs;
12800a708f8fSGustavo F. Padovan 
128158d35f87SGustavo F. Padovan 	skb = skb_peek(&chan->tx_q);
12820a708f8fSGustavo F. Padovan 	if (!skb)
12830a708f8fSGustavo F. Padovan 		return;
12840a708f8fSGustavo F. Padovan 
12850a708f8fSGustavo F. Padovan 	do {
12860a708f8fSGustavo F. Padovan 		if (bt_cb(skb)->tx_seq == tx_seq)
12870a708f8fSGustavo F. Padovan 			break;
12880a708f8fSGustavo F. Padovan 
128958d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
12900a708f8fSGustavo F. Padovan 			return;
12910a708f8fSGustavo F. Padovan 
129258d35f87SGustavo F. Padovan 	} while ((skb = skb_queue_next(&chan->tx_q, skb)));
12930a708f8fSGustavo F. Padovan 
12942c03a7a4SGustavo F. Padovan 	if (chan->remote_max_tx &&
12952c03a7a4SGustavo F. Padovan 			bt_cb(skb)->retries == chan->remote_max_tx) {
12968c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
12970a708f8fSGustavo F. Padovan 		return;
12980a708f8fSGustavo F. Padovan 	}
12990a708f8fSGustavo F. Padovan 
13000a708f8fSGustavo F. Padovan 	tx_skb = skb_clone(skb, GFP_ATOMIC);
13010a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries++;
13020a708f8fSGustavo F. Padovan 	control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1303a429b519SRuiyi Zhang 	control &= L2CAP_CTRL_SAR;
13040a708f8fSGustavo F. Padovan 
1305525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
13060a708f8fSGustavo F. Padovan 		control |= L2CAP_CTRL_FINAL;
1307525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
13080a708f8fSGustavo F. Padovan 	}
13090a708f8fSGustavo F. Padovan 
131042e5c802SGustavo F. Padovan 	control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
13110a708f8fSGustavo F. Padovan 			| (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
13120a708f8fSGustavo F. Padovan 
13130a708f8fSGustavo F. Padovan 	put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);
13140a708f8fSGustavo F. Padovan 
131547d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
13160a708f8fSGustavo F. Padovan 		fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2);
13170a708f8fSGustavo F. Padovan 		put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2);
13180a708f8fSGustavo F. Padovan 	}
13190a708f8fSGustavo F. Padovan 
13204343478fSGustavo F. Padovan 	l2cap_do_send(chan, tx_skb);
13210a708f8fSGustavo F. Padovan }
13220a708f8fSGustavo F. Padovan 
1323525cd185SGustavo F. Padovan int l2cap_ertm_send(struct l2cap_chan *chan)
13240a708f8fSGustavo F. Padovan {
13250a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
13260a708f8fSGustavo F. Padovan 	u16 control, fcs;
13270a708f8fSGustavo F. Padovan 	int nsent = 0;
13280a708f8fSGustavo F. Padovan 
132989bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
13300a708f8fSGustavo F. Padovan 		return -ENOTCONN;
13310a708f8fSGustavo F. Padovan 
133258d35f87SGustavo F. Padovan 	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
13330a708f8fSGustavo F. Padovan 
13342c03a7a4SGustavo F. Padovan 		if (chan->remote_max_tx &&
13352c03a7a4SGustavo F. Padovan 				bt_cb(skb)->retries == chan->remote_max_tx) {
13368c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
13370a708f8fSGustavo F. Padovan 			break;
13380a708f8fSGustavo F. Padovan 		}
13390a708f8fSGustavo F. Padovan 
13400a708f8fSGustavo F. Padovan 		tx_skb = skb_clone(skb, GFP_ATOMIC);
13410a708f8fSGustavo F. Padovan 
13420a708f8fSGustavo F. Padovan 		bt_cb(skb)->retries++;
13430a708f8fSGustavo F. Padovan 
13440a708f8fSGustavo F. Padovan 		control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
13450a708f8fSGustavo F. Padovan 		control &= L2CAP_CTRL_SAR;
13460a708f8fSGustavo F. Padovan 
1347525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
13480a708f8fSGustavo F. Padovan 			control |= L2CAP_CTRL_FINAL;
1349525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
13500a708f8fSGustavo F. Padovan 		}
135142e5c802SGustavo F. Padovan 		control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
135242e5c802SGustavo F. Padovan 				| (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
13530a708f8fSGustavo F. Padovan 		put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);
13540a708f8fSGustavo F. Padovan 
13550a708f8fSGustavo F. Padovan 
135647d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
13570a708f8fSGustavo F. Padovan 			fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2);
13580a708f8fSGustavo F. Padovan 			put_unaligned_le16(fcs, skb->data + tx_skb->len - 2);
13590a708f8fSGustavo F. Padovan 		}
13600a708f8fSGustavo F. Padovan 
13614343478fSGustavo F. Padovan 		l2cap_do_send(chan, tx_skb);
13620a708f8fSGustavo F. Padovan 
13631a09bcb9SGustavo F. Padovan 		__set_retrans_timer(chan);
13640a708f8fSGustavo F. Padovan 
136542e5c802SGustavo F. Padovan 		bt_cb(skb)->tx_seq = chan->next_tx_seq;
136642e5c802SGustavo F. Padovan 		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
13670a708f8fSGustavo F. Padovan 
136823e9fde2SSuraj Sumangala 		if (bt_cb(skb)->retries == 1)
13696a026610SGustavo F. Padovan 			chan->unacked_frames++;
137023e9fde2SSuraj Sumangala 
13716a026610SGustavo F. Padovan 		chan->frames_sent++;
13720a708f8fSGustavo F. Padovan 
137358d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
137458d35f87SGustavo F. Padovan 			chan->tx_send_head = NULL;
13750a708f8fSGustavo F. Padovan 		else
137658d35f87SGustavo F. Padovan 			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
13770a708f8fSGustavo F. Padovan 
13780a708f8fSGustavo F. Padovan 		nsent++;
13790a708f8fSGustavo F. Padovan 	}
13800a708f8fSGustavo F. Padovan 
13810a708f8fSGustavo F. Padovan 	return nsent;
13820a708f8fSGustavo F. Padovan }
13830a708f8fSGustavo F. Padovan 
1384525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan)
13850a708f8fSGustavo F. Padovan {
13860a708f8fSGustavo F. Padovan 	int ret;
13870a708f8fSGustavo F. Padovan 
138858d35f87SGustavo F. Padovan 	if (!skb_queue_empty(&chan->tx_q))
138958d35f87SGustavo F. Padovan 		chan->tx_send_head = chan->tx_q.next;
13900a708f8fSGustavo F. Padovan 
139142e5c802SGustavo F. Padovan 	chan->next_tx_seq = chan->expected_ack_seq;
1392525cd185SGustavo F. Padovan 	ret = l2cap_ertm_send(chan);
13930a708f8fSGustavo F. Padovan 	return ret;
13940a708f8fSGustavo F. Padovan }
13950a708f8fSGustavo F. Padovan 
1396525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan)
13970a708f8fSGustavo F. Padovan {
13980a708f8fSGustavo F. Padovan 	u16 control = 0;
13990a708f8fSGustavo F. Padovan 
140042e5c802SGustavo F. Padovan 	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
14010a708f8fSGustavo F. Padovan 
1402525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
14030a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_NOT_READY;
1404525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_RNR_SENT;
1405525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
14060a708f8fSGustavo F. Padovan 		return;
14070a708f8fSGustavo F. Padovan 	}
14080a708f8fSGustavo F. Padovan 
1409525cd185SGustavo F. Padovan 	if (l2cap_ertm_send(chan) > 0)
14100a708f8fSGustavo F. Padovan 		return;
14110a708f8fSGustavo F. Padovan 
14120a708f8fSGustavo F. Padovan 	control |= L2CAP_SUPER_RCV_READY;
1413525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
14140a708f8fSGustavo F. Padovan }
14150a708f8fSGustavo F. Padovan 
1416525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan)
14170a708f8fSGustavo F. Padovan {
14180a708f8fSGustavo F. Padovan 	struct srej_list *tail;
14190a708f8fSGustavo F. Padovan 	u16 control;
14200a708f8fSGustavo F. Padovan 
14210a708f8fSGustavo F. Padovan 	control = L2CAP_SUPER_SELECT_REJECT;
14220a708f8fSGustavo F. Padovan 	control |= L2CAP_CTRL_FINAL;
14230a708f8fSGustavo F. Padovan 
142439d5a3eeSGustavo F. Padovan 	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
14250a708f8fSGustavo F. Padovan 	control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
14260a708f8fSGustavo F. Padovan 
1427525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
14280a708f8fSGustavo F. Padovan }
14290a708f8fSGustavo F. Padovan 
14300a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb)
14310a708f8fSGustavo F. Padovan {
14328c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
14330a708f8fSGustavo F. Padovan 	struct sk_buff **frag;
14340a708f8fSGustavo F. Padovan 	int err, sent = 0;
14350a708f8fSGustavo F. Padovan 
14360a708f8fSGustavo F. Padovan 	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
14370a708f8fSGustavo F. Padovan 		return -EFAULT;
14380a708f8fSGustavo F. Padovan 
14390a708f8fSGustavo F. Padovan 	sent += count;
14400a708f8fSGustavo F. Padovan 	len  -= count;
14410a708f8fSGustavo F. Padovan 
14420a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
14430a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
14440a708f8fSGustavo F. Padovan 	while (len) {
14450a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
14460a708f8fSGustavo F. Padovan 
14470a708f8fSGustavo F. Padovan 		*frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err);
14480a708f8fSGustavo F. Padovan 		if (!*frag)
14490a708f8fSGustavo F. Padovan 			return err;
14500a708f8fSGustavo F. Padovan 		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
14510a708f8fSGustavo F. Padovan 			return -EFAULT;
14520a708f8fSGustavo F. Padovan 
14530a708f8fSGustavo F. Padovan 		sent += count;
14540a708f8fSGustavo F. Padovan 		len  -= count;
14550a708f8fSGustavo F. Padovan 
14560a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
14570a708f8fSGustavo F. Padovan 	}
14580a708f8fSGustavo F. Padovan 
14590a708f8fSGustavo F. Padovan 	return sent;
14600a708f8fSGustavo F. Padovan }
14610a708f8fSGustavo F. Padovan 
1462fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
14630a708f8fSGustavo F. Padovan {
1464fe4128e0SGustavo F. Padovan 	struct sock *sk = chan->sk;
14658c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
14660a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
14670a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE + 2;
14680a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
14690a708f8fSGustavo F. Padovan 
14700a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
14710a708f8fSGustavo F. Padovan 
14720a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
14730a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
14740a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
14750a708f8fSGustavo F. Padovan 	if (!skb)
14760a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
14770a708f8fSGustavo F. Padovan 
14780a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
14790a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1480fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
14810a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1482fe4128e0SGustavo F. Padovan 	put_unaligned_le16(chan->psm, skb_put(skb, 2));
14830a708f8fSGustavo F. Padovan 
14840a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
14850a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
14860a708f8fSGustavo F. Padovan 		kfree_skb(skb);
14870a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
14880a708f8fSGustavo F. Padovan 	}
14890a708f8fSGustavo F. Padovan 	return skb;
14900a708f8fSGustavo F. Padovan }
14910a708f8fSGustavo F. Padovan 
1492fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
14930a708f8fSGustavo F. Padovan {
1494fe4128e0SGustavo F. Padovan 	struct sock *sk = chan->sk;
14958c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
14960a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
14970a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE;
14980a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
14990a708f8fSGustavo F. Padovan 
15000a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
15010a708f8fSGustavo F. Padovan 
15020a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
15030a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
15040a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
15050a708f8fSGustavo F. Padovan 	if (!skb)
15060a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15070a708f8fSGustavo F. Padovan 
15080a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
15090a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1510fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
15110a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
15120a708f8fSGustavo F. Padovan 
15130a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
15140a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
15150a708f8fSGustavo F. Padovan 		kfree_skb(skb);
15160a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15170a708f8fSGustavo F. Padovan 	}
15180a708f8fSGustavo F. Padovan 	return skb;
15190a708f8fSGustavo F. Padovan }
15200a708f8fSGustavo F. Padovan 
152147d1ec61SGustavo F. Padovan struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen)
15220a708f8fSGustavo F. Padovan {
152347d1ec61SGustavo F. Padovan 	struct sock *sk = chan->sk;
15248c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
15250a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
15260a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE + 2;
15270a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
15280a708f8fSGustavo F. Padovan 
15290a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
15300a708f8fSGustavo F. Padovan 
15310a708f8fSGustavo F. Padovan 	if (!conn)
15320a708f8fSGustavo F. Padovan 		return ERR_PTR(-ENOTCONN);
15330a708f8fSGustavo F. Padovan 
15340a708f8fSGustavo F. Padovan 	if (sdulen)
15350a708f8fSGustavo F. Padovan 		hlen += 2;
15360a708f8fSGustavo F. Padovan 
153747d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
15380a708f8fSGustavo F. Padovan 		hlen += 2;
15390a708f8fSGustavo F. Padovan 
15400a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
15410a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
15420a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
15430a708f8fSGustavo F. Padovan 	if (!skb)
15440a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15450a708f8fSGustavo F. Padovan 
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));
15500a708f8fSGustavo F. Padovan 	put_unaligned_le16(control, skb_put(skb, 2));
15510a708f8fSGustavo F. Padovan 	if (sdulen)
15520a708f8fSGustavo F. Padovan 		put_unaligned_le16(sdulen, skb_put(skb, 2));
15530a708f8fSGustavo F. Padovan 
15540a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
15550a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
15560a708f8fSGustavo F. Padovan 		kfree_skb(skb);
15570a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15580a708f8fSGustavo F. Padovan 	}
15590a708f8fSGustavo F. Padovan 
156047d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
15610a708f8fSGustavo F. Padovan 		put_unaligned_le16(0, skb_put(skb, 2));
15620a708f8fSGustavo F. Padovan 
15630a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries = 0;
15640a708f8fSGustavo F. Padovan 	return skb;
15650a708f8fSGustavo F. Padovan }
15660a708f8fSGustavo F. Padovan 
15672c03a7a4SGustavo F. Padovan int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
15680a708f8fSGustavo F. Padovan {
15690a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
15700a708f8fSGustavo F. Padovan 	struct sk_buff_head sar_queue;
15710a708f8fSGustavo F. Padovan 	u16 control;
15720a708f8fSGustavo F. Padovan 	size_t size = 0;
15730a708f8fSGustavo F. Padovan 
15740a708f8fSGustavo F. Padovan 	skb_queue_head_init(&sar_queue);
15750a708f8fSGustavo F. Padovan 	control = L2CAP_SDU_START;
157647d1ec61SGustavo F. Padovan 	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
15770a708f8fSGustavo F. Padovan 	if (IS_ERR(skb))
15780a708f8fSGustavo F. Padovan 		return PTR_ERR(skb);
15790a708f8fSGustavo F. Padovan 
15800a708f8fSGustavo F. Padovan 	__skb_queue_tail(&sar_queue, skb);
15812c03a7a4SGustavo F. Padovan 	len -= chan->remote_mps;
15822c03a7a4SGustavo F. Padovan 	size += chan->remote_mps;
15830a708f8fSGustavo F. Padovan 
15840a708f8fSGustavo F. Padovan 	while (len > 0) {
15850a708f8fSGustavo F. Padovan 		size_t buflen;
15860a708f8fSGustavo F. Padovan 
15872c03a7a4SGustavo F. Padovan 		if (len > chan->remote_mps) {
15880a708f8fSGustavo F. Padovan 			control = L2CAP_SDU_CONTINUE;
15892c03a7a4SGustavo F. Padovan 			buflen = chan->remote_mps;
15900a708f8fSGustavo F. Padovan 		} else {
15910a708f8fSGustavo F. Padovan 			control = L2CAP_SDU_END;
15920a708f8fSGustavo F. Padovan 			buflen = len;
15930a708f8fSGustavo F. Padovan 		}
15940a708f8fSGustavo F. Padovan 
159547d1ec61SGustavo F. Padovan 		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
15960a708f8fSGustavo F. Padovan 		if (IS_ERR(skb)) {
15970a708f8fSGustavo F. Padovan 			skb_queue_purge(&sar_queue);
15980a708f8fSGustavo F. Padovan 			return PTR_ERR(skb);
15990a708f8fSGustavo F. Padovan 		}
16000a708f8fSGustavo F. Padovan 
16010a708f8fSGustavo F. Padovan 		__skb_queue_tail(&sar_queue, skb);
16020a708f8fSGustavo F. Padovan 		len -= buflen;
16030a708f8fSGustavo F. Padovan 		size += buflen;
16040a708f8fSGustavo F. Padovan 	}
160558d35f87SGustavo F. Padovan 	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
160658d35f87SGustavo F. Padovan 	if (chan->tx_send_head == NULL)
160758d35f87SGustavo F. Padovan 		chan->tx_send_head = sar_queue.next;
16080a708f8fSGustavo F. Padovan 
16090a708f8fSGustavo F. Padovan 	return size;
16100a708f8fSGustavo F. Padovan }
16110a708f8fSGustavo F. Padovan 
16129a91a04aSGustavo F. Padovan int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
16139a91a04aSGustavo F. Padovan {
16149a91a04aSGustavo F. Padovan 	struct sk_buff *skb;
16159a91a04aSGustavo F. Padovan 	u16 control;
16169a91a04aSGustavo F. Padovan 	int err;
16179a91a04aSGustavo F. Padovan 
16189a91a04aSGustavo F. Padovan 	/* Connectionless channel */
1619715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
16209a91a04aSGustavo F. Padovan 		skb = l2cap_create_connless_pdu(chan, msg, len);
16219a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
16229a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
16239a91a04aSGustavo F. Padovan 
16249a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
16259a91a04aSGustavo F. Padovan 		return len;
16269a91a04aSGustavo F. Padovan 	}
16279a91a04aSGustavo F. Padovan 
16289a91a04aSGustavo F. Padovan 	switch (chan->mode) {
16299a91a04aSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
16309a91a04aSGustavo F. Padovan 		/* Check outgoing MTU */
16319a91a04aSGustavo F. Padovan 		if (len > chan->omtu)
16329a91a04aSGustavo F. Padovan 			return -EMSGSIZE;
16339a91a04aSGustavo F. Padovan 
16349a91a04aSGustavo F. Padovan 		/* Create a basic PDU */
16359a91a04aSGustavo F. Padovan 		skb = l2cap_create_basic_pdu(chan, msg, len);
16369a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
16379a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
16389a91a04aSGustavo F. Padovan 
16399a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
16409a91a04aSGustavo F. Padovan 		err = len;
16419a91a04aSGustavo F. Padovan 		break;
16429a91a04aSGustavo F. Padovan 
16439a91a04aSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
16449a91a04aSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
16459a91a04aSGustavo F. Padovan 		/* Entire SDU fits into one PDU */
16469a91a04aSGustavo F. Padovan 		if (len <= chan->remote_mps) {
16479a91a04aSGustavo F. Padovan 			control = L2CAP_SDU_UNSEGMENTED;
16489a91a04aSGustavo F. Padovan 			skb = l2cap_create_iframe_pdu(chan, msg, len, control,
16499a91a04aSGustavo F. Padovan 									0);
16509a91a04aSGustavo F. Padovan 			if (IS_ERR(skb))
16519a91a04aSGustavo F. Padovan 				return PTR_ERR(skb);
16529a91a04aSGustavo F. Padovan 
16539a91a04aSGustavo F. Padovan 			__skb_queue_tail(&chan->tx_q, skb);
16549a91a04aSGustavo F. Padovan 
16559a91a04aSGustavo F. Padovan 			if (chan->tx_send_head == NULL)
16569a91a04aSGustavo F. Padovan 				chan->tx_send_head = skb;
16579a91a04aSGustavo F. Padovan 
16589a91a04aSGustavo F. Padovan 		} else {
16599a91a04aSGustavo F. Padovan 			/* Segment SDU into multiples PDUs */
16609a91a04aSGustavo F. Padovan 			err = l2cap_sar_segment_sdu(chan, msg, len);
16619a91a04aSGustavo F. Padovan 			if (err < 0)
16629a91a04aSGustavo F. Padovan 				return err;
16639a91a04aSGustavo F. Padovan 		}
16649a91a04aSGustavo F. Padovan 
16659a91a04aSGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_STREAMING) {
16669a91a04aSGustavo F. Padovan 			l2cap_streaming_send(chan);
16679a91a04aSGustavo F. Padovan 			err = len;
16689a91a04aSGustavo F. Padovan 			break;
16699a91a04aSGustavo F. Padovan 		}
16709a91a04aSGustavo F. Padovan 
16719a91a04aSGustavo F. Padovan 		if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
16729a91a04aSGustavo F. Padovan 				(chan->conn_state & L2CAP_CONN_WAIT_F)) {
16739a91a04aSGustavo F. Padovan 			err = len;
16749a91a04aSGustavo F. Padovan 			break;
16759a91a04aSGustavo F. Padovan 		}
16769a91a04aSGustavo F. Padovan 
16779a91a04aSGustavo F. Padovan 		err = l2cap_ertm_send(chan);
16789a91a04aSGustavo F. Padovan 		if (err >= 0)
16799a91a04aSGustavo F. Padovan 			err = len;
16809a91a04aSGustavo F. Padovan 
16819a91a04aSGustavo F. Padovan 		break;
16829a91a04aSGustavo F. Padovan 
16839a91a04aSGustavo F. Padovan 	default:
16849a91a04aSGustavo F. Padovan 		BT_DBG("bad state %1.1x", chan->mode);
16859a91a04aSGustavo F. Padovan 		err = -EBADFD;
16869a91a04aSGustavo F. Padovan 	}
16879a91a04aSGustavo F. Padovan 
16889a91a04aSGustavo F. Padovan 	return err;
16899a91a04aSGustavo F. Padovan }
16909a91a04aSGustavo F. Padovan 
16910a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */
16920a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
16930a708f8fSGustavo F. Padovan {
16940a708f8fSGustavo F. Padovan 	struct sk_buff *nskb;
169548454079SGustavo F. Padovan 	struct l2cap_chan *chan;
16960a708f8fSGustavo F. Padovan 
16970a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
16980a708f8fSGustavo F. Padovan 
1699baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
1700baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
170148454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
1702715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_RAW)
17030a708f8fSGustavo F. Padovan 			continue;
17040a708f8fSGustavo F. Padovan 
17050a708f8fSGustavo F. Padovan 		/* Don't send frame to the socket it came from */
17060a708f8fSGustavo F. Padovan 		if (skb->sk == sk)
17070a708f8fSGustavo F. Padovan 			continue;
17080a708f8fSGustavo F. Padovan 		nskb = skb_clone(skb, GFP_ATOMIC);
17090a708f8fSGustavo F. Padovan 		if (!nskb)
17100a708f8fSGustavo F. Padovan 			continue;
17110a708f8fSGustavo F. Padovan 
171223070494SGustavo F. Padovan 		if (chan->ops->recv(chan->data, nskb))
17130a708f8fSGustavo F. Padovan 			kfree_skb(nskb);
17140a708f8fSGustavo F. Padovan 	}
1715baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
17160a708f8fSGustavo F. Padovan }
17170a708f8fSGustavo F. Padovan 
17180a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */
17190a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
17200a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data)
17210a708f8fSGustavo F. Padovan {
17220a708f8fSGustavo F. Padovan 	struct sk_buff *skb, **frag;
17230a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr *cmd;
17240a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
17250a708f8fSGustavo F. Padovan 	int len, count;
17260a708f8fSGustavo F. Padovan 
17270a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
17280a708f8fSGustavo F. Padovan 			conn, code, ident, dlen);
17290a708f8fSGustavo F. Padovan 
17300a708f8fSGustavo F. Padovan 	len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
17310a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, len);
17320a708f8fSGustavo F. Padovan 
17330a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
17340a708f8fSGustavo F. Padovan 	if (!skb)
17350a708f8fSGustavo F. Padovan 		return NULL;
17360a708f8fSGustavo F. Padovan 
17370a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
17380a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
17393300d9a9SClaudio Takahasi 
17403300d9a9SClaudio Takahasi 	if (conn->hcon->type == LE_LINK)
17413300d9a9SClaudio Takahasi 		lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
17423300d9a9SClaudio Takahasi 	else
17430a708f8fSGustavo F. Padovan 		lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
17440a708f8fSGustavo F. Padovan 
17450a708f8fSGustavo F. Padovan 	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
17460a708f8fSGustavo F. Padovan 	cmd->code  = code;
17470a708f8fSGustavo F. Padovan 	cmd->ident = ident;
17480a708f8fSGustavo F. Padovan 	cmd->len   = cpu_to_le16(dlen);
17490a708f8fSGustavo F. Padovan 
17500a708f8fSGustavo F. Padovan 	if (dlen) {
17510a708f8fSGustavo F. Padovan 		count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
17520a708f8fSGustavo F. Padovan 		memcpy(skb_put(skb, count), data, count);
17530a708f8fSGustavo F. Padovan 		data += count;
17540a708f8fSGustavo F. Padovan 	}
17550a708f8fSGustavo F. Padovan 
17560a708f8fSGustavo F. Padovan 	len -= skb->len;
17570a708f8fSGustavo F. Padovan 
17580a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
17590a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
17600a708f8fSGustavo F. Padovan 	while (len) {
17610a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
17620a708f8fSGustavo F. Padovan 
17630a708f8fSGustavo F. Padovan 		*frag = bt_skb_alloc(count, GFP_ATOMIC);
17640a708f8fSGustavo F. Padovan 		if (!*frag)
17650a708f8fSGustavo F. Padovan 			goto fail;
17660a708f8fSGustavo F. Padovan 
17670a708f8fSGustavo F. Padovan 		memcpy(skb_put(*frag, count), data, count);
17680a708f8fSGustavo F. Padovan 
17690a708f8fSGustavo F. Padovan 		len  -= count;
17700a708f8fSGustavo F. Padovan 		data += count;
17710a708f8fSGustavo F. Padovan 
17720a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
17730a708f8fSGustavo F. Padovan 	}
17740a708f8fSGustavo F. Padovan 
17750a708f8fSGustavo F. Padovan 	return skb;
17760a708f8fSGustavo F. Padovan 
17770a708f8fSGustavo F. Padovan fail:
17780a708f8fSGustavo F. Padovan 	kfree_skb(skb);
17790a708f8fSGustavo F. Padovan 	return NULL;
17800a708f8fSGustavo F. Padovan }
17810a708f8fSGustavo F. Padovan 
17820a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
17830a708f8fSGustavo F. Padovan {
17840a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
17850a708f8fSGustavo F. Padovan 	int len;
17860a708f8fSGustavo F. Padovan 
17870a708f8fSGustavo F. Padovan 	len = L2CAP_CONF_OPT_SIZE + opt->len;
17880a708f8fSGustavo F. Padovan 	*ptr += len;
17890a708f8fSGustavo F. Padovan 
17900a708f8fSGustavo F. Padovan 	*type = opt->type;
17910a708f8fSGustavo F. Padovan 	*olen = opt->len;
17920a708f8fSGustavo F. Padovan 
17930a708f8fSGustavo F. Padovan 	switch (opt->len) {
17940a708f8fSGustavo F. Padovan 	case 1:
17950a708f8fSGustavo F. Padovan 		*val = *((u8 *) opt->val);
17960a708f8fSGustavo F. Padovan 		break;
17970a708f8fSGustavo F. Padovan 
17980a708f8fSGustavo F. Padovan 	case 2:
17990a708f8fSGustavo F. Padovan 		*val = get_unaligned_le16(opt->val);
18000a708f8fSGustavo F. Padovan 		break;
18010a708f8fSGustavo F. Padovan 
18020a708f8fSGustavo F. Padovan 	case 4:
18030a708f8fSGustavo F. Padovan 		*val = get_unaligned_le32(opt->val);
18040a708f8fSGustavo F. Padovan 		break;
18050a708f8fSGustavo F. Padovan 
18060a708f8fSGustavo F. Padovan 	default:
18070a708f8fSGustavo F. Padovan 		*val = (unsigned long) opt->val;
18080a708f8fSGustavo F. Padovan 		break;
18090a708f8fSGustavo F. Padovan 	}
18100a708f8fSGustavo F. Padovan 
18110a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
18120a708f8fSGustavo F. Padovan 	return len;
18130a708f8fSGustavo F. Padovan }
18140a708f8fSGustavo F. Padovan 
18150a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
18160a708f8fSGustavo F. Padovan {
18170a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
18180a708f8fSGustavo F. Padovan 
18190a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
18200a708f8fSGustavo F. Padovan 
18210a708f8fSGustavo F. Padovan 	opt->type = type;
18220a708f8fSGustavo F. Padovan 	opt->len  = len;
18230a708f8fSGustavo F. Padovan 
18240a708f8fSGustavo F. Padovan 	switch (len) {
18250a708f8fSGustavo F. Padovan 	case 1:
18260a708f8fSGustavo F. Padovan 		*((u8 *) opt->val)  = val;
18270a708f8fSGustavo F. Padovan 		break;
18280a708f8fSGustavo F. Padovan 
18290a708f8fSGustavo F. Padovan 	case 2:
18300a708f8fSGustavo F. Padovan 		put_unaligned_le16(val, opt->val);
18310a708f8fSGustavo F. Padovan 		break;
18320a708f8fSGustavo F. Padovan 
18330a708f8fSGustavo F. Padovan 	case 4:
18340a708f8fSGustavo F. Padovan 		put_unaligned_le32(val, opt->val);
18350a708f8fSGustavo F. Padovan 		break;
18360a708f8fSGustavo F. Padovan 
18370a708f8fSGustavo F. Padovan 	default:
18380a708f8fSGustavo F. Padovan 		memcpy(opt->val, (void *) val, len);
18390a708f8fSGustavo F. Padovan 		break;
18400a708f8fSGustavo F. Padovan 	}
18410a708f8fSGustavo F. Padovan 
18420a708f8fSGustavo F. Padovan 	*ptr += L2CAP_CONF_OPT_SIZE + len;
18430a708f8fSGustavo F. Padovan }
18440a708f8fSGustavo F. Padovan 
18450a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg)
18460a708f8fSGustavo F. Padovan {
1847525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
18480a708f8fSGustavo F. Padovan 
1849525cd185SGustavo F. Padovan 	bh_lock_sock(chan->sk);
1850525cd185SGustavo F. Padovan 	l2cap_send_ack(chan);
1851525cd185SGustavo F. Padovan 	bh_unlock_sock(chan->sk);
18520a708f8fSGustavo F. Padovan }
18530a708f8fSGustavo F. Padovan 
1854525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan)
18550a708f8fSGustavo F. Padovan {
1856525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
1857525cd185SGustavo F. Padovan 
185842e5c802SGustavo F. Padovan 	chan->expected_ack_seq = 0;
18596a026610SGustavo F. Padovan 	chan->unacked_frames = 0;
186042e5c802SGustavo F. Padovan 	chan->buffer_seq = 0;
18616a026610SGustavo F. Padovan 	chan->num_acked = 0;
18626a026610SGustavo F. Padovan 	chan->frames_sent = 0;
18630a708f8fSGustavo F. Padovan 
1864e92c8e70SGustavo F. Padovan 	setup_timer(&chan->retrans_timer, l2cap_retrans_timeout,
1865e92c8e70SGustavo F. Padovan 							(unsigned long) chan);
1866e92c8e70SGustavo F. Padovan 	setup_timer(&chan->monitor_timer, l2cap_monitor_timeout,
1867e92c8e70SGustavo F. Padovan 							(unsigned long) chan);
1868e92c8e70SGustavo F. Padovan 	setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan);
18690a708f8fSGustavo F. Padovan 
1870f1c6775bSGustavo F. Padovan 	skb_queue_head_init(&chan->srej_q);
1871f1c6775bSGustavo F. Padovan 	skb_queue_head_init(&chan->busy_q);
18720a708f8fSGustavo F. Padovan 
187339d5a3eeSGustavo F. Padovan 	INIT_LIST_HEAD(&chan->srej_l);
187439d5a3eeSGustavo F. Padovan 
1875311bb895SGustavo F. Padovan 	INIT_WORK(&chan->busy_work, l2cap_busy_work);
18760a708f8fSGustavo F. Padovan 
18770a708f8fSGustavo F. Padovan 	sk->sk_backlog_rcv = l2cap_ertm_data_rcv;
18780a708f8fSGustavo F. Padovan }
18790a708f8fSGustavo F. Padovan 
18800a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
18810a708f8fSGustavo F. Padovan {
18820a708f8fSGustavo F. Padovan 	switch (mode) {
18830a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
18840a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
18850a708f8fSGustavo F. Padovan 		if (l2cap_mode_supported(mode, remote_feat_mask))
18860a708f8fSGustavo F. Padovan 			return mode;
18870a708f8fSGustavo F. Padovan 		/* fall through */
18880a708f8fSGustavo F. Padovan 	default:
18890a708f8fSGustavo F. Padovan 		return L2CAP_MODE_BASIC;
18900a708f8fSGustavo F. Padovan 	}
18910a708f8fSGustavo F. Padovan }
18920a708f8fSGustavo F. Padovan 
1893710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
18940a708f8fSGustavo F. Padovan {
18950a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
18960c1bc5c6SGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
18970a708f8fSGustavo F. Padovan 	void *ptr = req->data;
18980a708f8fSGustavo F. Padovan 
189949208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
19000a708f8fSGustavo F. Padovan 
190173ffa904SGustavo F. Padovan 	if (chan->num_conf_req || chan->num_conf_rsp)
19020a708f8fSGustavo F. Padovan 		goto done;
19030a708f8fSGustavo F. Padovan 
19040c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
19050a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
19060a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
1907b4450035SGustavo F. Padovan 		if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE)
19080a708f8fSGustavo F. Padovan 			break;
19090a708f8fSGustavo F. Padovan 
19100a708f8fSGustavo F. Padovan 		/* fall through */
19110a708f8fSGustavo F. Padovan 	default:
19128c1d787bSGustavo F. Padovan 		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
19130a708f8fSGustavo F. Padovan 		break;
19140a708f8fSGustavo F. Padovan 	}
19150a708f8fSGustavo F. Padovan 
19160a708f8fSGustavo F. Padovan done:
19170c1bc5c6SGustavo F. Padovan 	if (chan->imtu != L2CAP_DEFAULT_MTU)
19180c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
19190a708f8fSGustavo F. Padovan 
19200c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
19210a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
19228c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
19238c1d787bSGustavo F. Padovan 				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
19240a708f8fSGustavo F. Padovan 			break;
19250a708f8fSGustavo F. Padovan 
19260a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_BASIC;
19270a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
19280a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
19290a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
19300a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
19310a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = 0;
19320a708f8fSGustavo F. Padovan 
19330a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
19340a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
19350a708f8fSGustavo F. Padovan 		break;
19360a708f8fSGustavo F. Padovan 
19370a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
19380a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_ERTM;
193947d1ec61SGustavo F. Padovan 		rfc.txwin_size      = chan->tx_win;
194047d1ec61SGustavo F. Padovan 		rfc.max_transmit    = chan->max_tx;
19410a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
19420a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
19430a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
19448c1d787bSGustavo F. Padovan 		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
19458c1d787bSGustavo F. Padovan 			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
19460a708f8fSGustavo F. Padovan 
19470a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
19480a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
19490a708f8fSGustavo F. Padovan 
19508c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
19510a708f8fSGustavo F. Padovan 			break;
19520a708f8fSGustavo F. Padovan 
195347d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
1954b4450035SGustavo F. Padovan 				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
195547d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
195647d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
19570a708f8fSGustavo F. Padovan 		}
19580a708f8fSGustavo F. Padovan 		break;
19590a708f8fSGustavo F. Padovan 
19600a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
19610a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_STREAMING;
19620a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
19630a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
19640a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
19650a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
19660a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
19678c1d787bSGustavo F. Padovan 		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
19688c1d787bSGustavo F. Padovan 			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
19690a708f8fSGustavo F. Padovan 
19700a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
19710a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
19720a708f8fSGustavo F. Padovan 
19738c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
19740a708f8fSGustavo F. Padovan 			break;
19750a708f8fSGustavo F. Padovan 
197647d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
1977b4450035SGustavo F. Padovan 				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
197847d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
197947d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
19800a708f8fSGustavo F. Padovan 		}
19810a708f8fSGustavo F. Padovan 		break;
19820a708f8fSGustavo F. Padovan 	}
19830a708f8fSGustavo F. Padovan 
1984fe4128e0SGustavo F. Padovan 	req->dcid  = cpu_to_le16(chan->dcid);
19850a708f8fSGustavo F. Padovan 	req->flags = cpu_to_le16(0);
19860a708f8fSGustavo F. Padovan 
19870a708f8fSGustavo F. Padovan 	return ptr - data;
19880a708f8fSGustavo F. Padovan }
19890a708f8fSGustavo F. Padovan 
199073ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
19910a708f8fSGustavo F. Padovan {
19920a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
19930a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
199473ffa904SGustavo F. Padovan 	void *req = chan->conf_req;
199573ffa904SGustavo F. Padovan 	int len = chan->conf_len;
19960a708f8fSGustavo F. Padovan 	int type, hint, olen;
19970a708f8fSGustavo F. Padovan 	unsigned long val;
19980a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
19990a708f8fSGustavo F. Padovan 	u16 mtu = L2CAP_DEFAULT_MTU;
20000a708f8fSGustavo F. Padovan 	u16 result = L2CAP_CONF_SUCCESS;
20010a708f8fSGustavo F. Padovan 
200273ffa904SGustavo F. Padovan 	BT_DBG("chan %p", chan);
20030a708f8fSGustavo F. Padovan 
20040a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
20050a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
20060a708f8fSGustavo F. Padovan 
20070a708f8fSGustavo F. Padovan 		hint  = type & L2CAP_CONF_HINT;
20080a708f8fSGustavo F. Padovan 		type &= L2CAP_CONF_MASK;
20090a708f8fSGustavo F. Padovan 
20100a708f8fSGustavo F. Padovan 		switch (type) {
20110a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
20120a708f8fSGustavo F. Padovan 			mtu = val;
20130a708f8fSGustavo F. Padovan 			break;
20140a708f8fSGustavo F. Padovan 
20150a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
20160c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
20170a708f8fSGustavo F. Padovan 			break;
20180a708f8fSGustavo F. Padovan 
20190a708f8fSGustavo F. Padovan 		case L2CAP_CONF_QOS:
20200a708f8fSGustavo F. Padovan 			break;
20210a708f8fSGustavo F. Padovan 
20220a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
20230a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
20240a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *) val, olen);
20250a708f8fSGustavo F. Padovan 			break;
20260a708f8fSGustavo F. Padovan 
20270a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FCS:
20280a708f8fSGustavo F. Padovan 			if (val == L2CAP_FCS_NONE)
2029b4450035SGustavo F. Padovan 				chan->conf_state |= L2CAP_CONF_NO_FCS_RECV;
20300a708f8fSGustavo F. Padovan 
20310a708f8fSGustavo F. Padovan 			break;
20320a708f8fSGustavo F. Padovan 
20330a708f8fSGustavo F. Padovan 		default:
20340a708f8fSGustavo F. Padovan 			if (hint)
20350a708f8fSGustavo F. Padovan 				break;
20360a708f8fSGustavo F. Padovan 
20370a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNKNOWN;
20380a708f8fSGustavo F. Padovan 			*((u8 *) ptr++) = type;
20390a708f8fSGustavo F. Padovan 			break;
20400a708f8fSGustavo F. Padovan 		}
20410a708f8fSGustavo F. Padovan 	}
20420a708f8fSGustavo F. Padovan 
204373ffa904SGustavo F. Padovan 	if (chan->num_conf_rsp || chan->num_conf_req > 1)
20440a708f8fSGustavo F. Padovan 		goto done;
20450a708f8fSGustavo F. Padovan 
20460c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
20470a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
20480a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
2049b4450035SGustavo F. Padovan 		if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) {
20500c1bc5c6SGustavo F. Padovan 			chan->mode = l2cap_select_mode(rfc.mode,
20518c1d787bSGustavo F. Padovan 					chan->conn->feat_mask);
20520a708f8fSGustavo F. Padovan 			break;
20530a708f8fSGustavo F. Padovan 		}
20540a708f8fSGustavo F. Padovan 
20550c1bc5c6SGustavo F. Padovan 		if (chan->mode != rfc.mode)
20560a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
20570a708f8fSGustavo F. Padovan 
20580a708f8fSGustavo F. Padovan 		break;
20590a708f8fSGustavo F. Padovan 	}
20600a708f8fSGustavo F. Padovan 
20610a708f8fSGustavo F. Padovan done:
20620c1bc5c6SGustavo F. Padovan 	if (chan->mode != rfc.mode) {
20630a708f8fSGustavo F. Padovan 		result = L2CAP_CONF_UNACCEPT;
20640c1bc5c6SGustavo F. Padovan 		rfc.mode = chan->mode;
20650a708f8fSGustavo F. Padovan 
206673ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp == 1)
20670a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
20680a708f8fSGustavo F. Padovan 
20690a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
20700a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
20710a708f8fSGustavo F. Padovan 	}
20720a708f8fSGustavo F. Padovan 
20730a708f8fSGustavo F. Padovan 
20740a708f8fSGustavo F. Padovan 	if (result == L2CAP_CONF_SUCCESS) {
20750a708f8fSGustavo F. Padovan 		/* Configure output options and let the other side know
20760a708f8fSGustavo F. Padovan 		 * which ones we don't like. */
20770a708f8fSGustavo F. Padovan 
20780a708f8fSGustavo F. Padovan 		if (mtu < L2CAP_DEFAULT_MIN_MTU)
20790a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
20800a708f8fSGustavo F. Padovan 		else {
20810c1bc5c6SGustavo F. Padovan 			chan->omtu = mtu;
2082b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MTU_DONE;
20830a708f8fSGustavo F. Padovan 		}
20840c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
20850a708f8fSGustavo F. Padovan 
20860a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
20870a708f8fSGustavo F. Padovan 		case L2CAP_MODE_BASIC:
208847d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
2089b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MODE_DONE;
20900a708f8fSGustavo F. Padovan 			break;
20910a708f8fSGustavo F. Padovan 
20920a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
20932c03a7a4SGustavo F. Padovan 			chan->remote_tx_win = rfc.txwin_size;
20942c03a7a4SGustavo F. Padovan 			chan->remote_max_tx = rfc.max_transmit;
20950a708f8fSGustavo F. Padovan 
20968c1d787bSGustavo F. Padovan 			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
20978c1d787bSGustavo F. Padovan 				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
20980a708f8fSGustavo F. Padovan 
20992c03a7a4SGustavo F. Padovan 			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
21000a708f8fSGustavo F. Padovan 
21010a708f8fSGustavo F. Padovan 			rfc.retrans_timeout =
21020a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
21030a708f8fSGustavo F. Padovan 			rfc.monitor_timeout =
21040a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
21050a708f8fSGustavo F. Padovan 
2106b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MODE_DONE;
21070a708f8fSGustavo F. Padovan 
21080a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
21090a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
21100a708f8fSGustavo F. Padovan 
21110a708f8fSGustavo F. Padovan 			break;
21120a708f8fSGustavo F. Padovan 
21130a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
21148c1d787bSGustavo F. Padovan 			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
21158c1d787bSGustavo F. Padovan 				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
21160a708f8fSGustavo F. Padovan 
21172c03a7a4SGustavo F. Padovan 			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
21180a708f8fSGustavo F. Padovan 
2119b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MODE_DONE;
21200a708f8fSGustavo F. Padovan 
21210a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
21220a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
21230a708f8fSGustavo F. Padovan 
21240a708f8fSGustavo F. Padovan 			break;
21250a708f8fSGustavo F. Padovan 
21260a708f8fSGustavo F. Padovan 		default:
21270a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
21280a708f8fSGustavo F. Padovan 
21290a708f8fSGustavo F. Padovan 			memset(&rfc, 0, sizeof(rfc));
21300c1bc5c6SGustavo F. Padovan 			rfc.mode = chan->mode;
21310a708f8fSGustavo F. Padovan 		}
21320a708f8fSGustavo F. Padovan 
21330a708f8fSGustavo F. Padovan 		if (result == L2CAP_CONF_SUCCESS)
2134b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_OUTPUT_DONE;
21350a708f8fSGustavo F. Padovan 	}
2136fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
21370a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
21380a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(0x0000);
21390a708f8fSGustavo F. Padovan 
21400a708f8fSGustavo F. Padovan 	return ptr - data;
21410a708f8fSGustavo F. Padovan }
21420a708f8fSGustavo F. Padovan 
2143b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
21440a708f8fSGustavo F. Padovan {
21450a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
21460a708f8fSGustavo F. Padovan 	void *ptr = req->data;
21470a708f8fSGustavo F. Padovan 	int type, olen;
21480a708f8fSGustavo F. Padovan 	unsigned long val;
21490a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
21500a708f8fSGustavo F. Padovan 
2151fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
21520a708f8fSGustavo F. Padovan 
21530a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
21540a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
21550a708f8fSGustavo F. Padovan 
21560a708f8fSGustavo F. Padovan 		switch (type) {
21570a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
21580a708f8fSGustavo F. Padovan 			if (val < L2CAP_DEFAULT_MIN_MTU) {
21590a708f8fSGustavo F. Padovan 				*result = L2CAP_CONF_UNACCEPT;
21600c1bc5c6SGustavo F. Padovan 				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
21610a708f8fSGustavo F. Padovan 			} else
21620c1bc5c6SGustavo F. Padovan 				chan->imtu = val;
21630c1bc5c6SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
21640a708f8fSGustavo F. Padovan 			break;
21650a708f8fSGustavo F. Padovan 
21660a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
21670c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
21680a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
21690c1bc5c6SGustavo F. Padovan 							2, chan->flush_to);
21700a708f8fSGustavo F. Padovan 			break;
21710a708f8fSGustavo F. Padovan 
21720a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
21730a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
21740a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
21750a708f8fSGustavo F. Padovan 
2176b4450035SGustavo F. Padovan 			if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) &&
21770c1bc5c6SGustavo F. Padovan 							rfc.mode != chan->mode)
21780a708f8fSGustavo F. Padovan 				return -ECONNREFUSED;
21790a708f8fSGustavo F. Padovan 
218047d1ec61SGustavo F. Padovan 			chan->fcs = 0;
21810a708f8fSGustavo F. Padovan 
21820a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
21830a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
21840a708f8fSGustavo F. Padovan 			break;
21850a708f8fSGustavo F. Padovan 		}
21860a708f8fSGustavo F. Padovan 	}
21870a708f8fSGustavo F. Padovan 
21880c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
21890a708f8fSGustavo F. Padovan 		return -ECONNREFUSED;
21900a708f8fSGustavo F. Padovan 
21910c1bc5c6SGustavo F. Padovan 	chan->mode = rfc.mode;
21920a708f8fSGustavo F. Padovan 
21930a708f8fSGustavo F. Padovan 	if (*result == L2CAP_CONF_SUCCESS) {
21940a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
21950a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
219647d1ec61SGustavo F. Padovan 			chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
219747d1ec61SGustavo F. Padovan 			chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
219847d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
21990a708f8fSGustavo F. Padovan 			break;
22000a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
220147d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
22020a708f8fSGustavo F. Padovan 		}
22030a708f8fSGustavo F. Padovan 	}
22040a708f8fSGustavo F. Padovan 
2205fe4128e0SGustavo F. Padovan 	req->dcid   = cpu_to_le16(chan->dcid);
22060a708f8fSGustavo F. Padovan 	req->flags  = cpu_to_le16(0x0000);
22070a708f8fSGustavo F. Padovan 
22080a708f8fSGustavo F. Padovan 	return ptr - data;
22090a708f8fSGustavo F. Padovan }
22100a708f8fSGustavo F. Padovan 
2211fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
22120a708f8fSGustavo F. Padovan {
22130a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
22140a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
22150a708f8fSGustavo F. Padovan 
2216fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p", chan);
22170a708f8fSGustavo F. Padovan 
2218fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
22190a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
22200a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(flags);
22210a708f8fSGustavo F. Padovan 
22220a708f8fSGustavo F. Padovan 	return ptr - data;
22230a708f8fSGustavo F. Padovan }
22240a708f8fSGustavo F. Padovan 
22258c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
2226710f9b0aSGustavo F. Padovan {
2227710f9b0aSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
22288c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
2229710f9b0aSGustavo F. Padovan 	u8 buf[128];
2230710f9b0aSGustavo F. Padovan 
2231fe4128e0SGustavo F. Padovan 	rsp.scid   = cpu_to_le16(chan->dcid);
2232fe4128e0SGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(chan->scid);
2233710f9b0aSGustavo F. Padovan 	rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
2234710f9b0aSGustavo F. Padovan 	rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
2235710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, chan->ident,
2236710f9b0aSGustavo F. Padovan 				L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2237710f9b0aSGustavo F. Padovan 
2238b4450035SGustavo F. Padovan 	if (chan->conf_state & L2CAP_CONF_REQ_SENT)
2239710f9b0aSGustavo F. Padovan 		return;
2240710f9b0aSGustavo F. Padovan 
2241b4450035SGustavo F. Padovan 	chan->conf_state |= L2CAP_CONF_REQ_SENT;
2242710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2243710f9b0aSGustavo F. Padovan 			l2cap_build_conf_req(chan, buf), buf);
2244710f9b0aSGustavo F. Padovan 	chan->num_conf_req++;
2245710f9b0aSGustavo F. Padovan }
2246710f9b0aSGustavo F. Padovan 
224747d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
22480a708f8fSGustavo F. Padovan {
22490a708f8fSGustavo F. Padovan 	int type, olen;
22500a708f8fSGustavo F. Padovan 	unsigned long val;
22510a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
22520a708f8fSGustavo F. Padovan 
225347d1ec61SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
22540a708f8fSGustavo F. Padovan 
22550c1bc5c6SGustavo F. Padovan 	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
22560a708f8fSGustavo F. Padovan 		return;
22570a708f8fSGustavo F. Padovan 
22580a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
22590a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
22600a708f8fSGustavo F. Padovan 
22610a708f8fSGustavo F. Padovan 		switch (type) {
22620a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
22630a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
22640a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
22650a708f8fSGustavo F. Padovan 			goto done;
22660a708f8fSGustavo F. Padovan 		}
22670a708f8fSGustavo F. Padovan 	}
22680a708f8fSGustavo F. Padovan 
22690a708f8fSGustavo F. Padovan done:
22700a708f8fSGustavo F. Padovan 	switch (rfc.mode) {
22710a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
227247d1ec61SGustavo F. Padovan 		chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
227347d1ec61SGustavo F. Padovan 		chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
227447d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
22750a708f8fSGustavo F. Padovan 		break;
22760a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
227747d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
22780a708f8fSGustavo F. Padovan 	}
22790a708f8fSGustavo F. Padovan }
22800a708f8fSGustavo F. Padovan 
22810a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
22820a708f8fSGustavo F. Padovan {
22830a708f8fSGustavo F. Padovan 	struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data;
22840a708f8fSGustavo F. Padovan 
22850a708f8fSGustavo F. Padovan 	if (rej->reason != 0x0000)
22860a708f8fSGustavo F. Padovan 		return 0;
22870a708f8fSGustavo F. Padovan 
22880a708f8fSGustavo F. Padovan 	if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
22890a708f8fSGustavo F. Padovan 					cmd->ident == conn->info_ident) {
22900a708f8fSGustavo F. Padovan 		del_timer(&conn->info_timer);
22910a708f8fSGustavo F. Padovan 
22920a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
22930a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
22940a708f8fSGustavo F. Padovan 
22950a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
22960a708f8fSGustavo F. Padovan 	}
22970a708f8fSGustavo F. Padovan 
22980a708f8fSGustavo F. Padovan 	return 0;
22990a708f8fSGustavo F. Padovan }
23000a708f8fSGustavo F. Padovan 
23010a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
23020a708f8fSGustavo F. Padovan {
23030a708f8fSGustavo F. Padovan 	struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
23040a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
230523691d75SGustavo F. Padovan 	struct l2cap_chan *chan = NULL, *pchan;
23060a708f8fSGustavo F. Padovan 	struct sock *parent, *sk = NULL;
23070a708f8fSGustavo F. Padovan 	int result, status = L2CAP_CS_NO_INFO;
23080a708f8fSGustavo F. Padovan 
23090a708f8fSGustavo F. Padovan 	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
23100a708f8fSGustavo F. Padovan 	__le16 psm = req->psm;
23110a708f8fSGustavo F. Padovan 
23120a708f8fSGustavo F. Padovan 	BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
23130a708f8fSGustavo F. Padovan 
23140a708f8fSGustavo F. Padovan 	/* Check if we have socket listening on psm */
231523691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src);
231623691d75SGustavo F. Padovan 	if (!pchan) {
23170a708f8fSGustavo F. Padovan 		result = L2CAP_CR_BAD_PSM;
23180a708f8fSGustavo F. Padovan 		goto sendresp;
23190a708f8fSGustavo F. Padovan 	}
23200a708f8fSGustavo F. Padovan 
232123691d75SGustavo F. Padovan 	parent = pchan->sk;
232223691d75SGustavo F. Padovan 
23230a708f8fSGustavo F. Padovan 	bh_lock_sock(parent);
23240a708f8fSGustavo F. Padovan 
23250a708f8fSGustavo F. Padovan 	/* Check if the ACL is secure enough (if not SDP) */
23260a708f8fSGustavo F. Padovan 	if (psm != cpu_to_le16(0x0001) &&
23270a708f8fSGustavo F. Padovan 				!hci_conn_check_link_mode(conn->hcon)) {
23280a708f8fSGustavo F. Padovan 		conn->disc_reason = 0x05;
23290a708f8fSGustavo F. Padovan 		result = L2CAP_CR_SEC_BLOCK;
23300a708f8fSGustavo F. Padovan 		goto response;
23310a708f8fSGustavo F. Padovan 	}
23320a708f8fSGustavo F. Padovan 
23330a708f8fSGustavo F. Padovan 	result = L2CAP_CR_NO_MEM;
23340a708f8fSGustavo F. Padovan 
23350a708f8fSGustavo F. Padovan 	/* Check for backlog size */
23360a708f8fSGustavo F. Padovan 	if (sk_acceptq_is_full(parent)) {
23370a708f8fSGustavo F. Padovan 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
23380a708f8fSGustavo F. Padovan 		goto response;
23390a708f8fSGustavo F. Padovan 	}
23400a708f8fSGustavo F. Padovan 
234180808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
234280808e43SGustavo F. Padovan 	if (!chan)
23430a708f8fSGustavo F. Padovan 		goto response;
23440a708f8fSGustavo F. Padovan 
234580808e43SGustavo F. Padovan 	sk = chan->sk;
234680808e43SGustavo F. Padovan 
2347baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
23480a708f8fSGustavo F. Padovan 
23490a708f8fSGustavo F. Padovan 	/* Check if we already have channel with that dcid */
2350baa7e1faSGustavo F. Padovan 	if (__l2cap_get_chan_by_dcid(conn, scid)) {
2351baa7e1faSGustavo F. Padovan 		write_unlock_bh(&conn->chan_lock);
23520a708f8fSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
2353ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
23540a708f8fSGustavo F. Padovan 		goto response;
23550a708f8fSGustavo F. Padovan 	}
23560a708f8fSGustavo F. Padovan 
23570a708f8fSGustavo F. Padovan 	hci_conn_hold(conn->hcon);
23580a708f8fSGustavo F. Padovan 
23590a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->src, conn->src);
23600a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->dst, conn->dst);
2361fe4128e0SGustavo F. Padovan 	chan->psm  = psm;
2362fe4128e0SGustavo F. Padovan 	chan->dcid = scid;
23630a708f8fSGustavo F. Padovan 
2364d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
2365d1010240SGustavo F. Padovan 
236648454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
236748454079SGustavo F. Padovan 
2368fe4128e0SGustavo F. Padovan 	dcid = chan->scid;
23690a708f8fSGustavo F. Padovan 
2370c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
23710a708f8fSGustavo F. Padovan 
2372fc7f8a7eSGustavo F. Padovan 	chan->ident = cmd->ident;
23730a708f8fSGustavo F. Padovan 
23740a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
23754343478fSGustavo F. Padovan 		if (l2cap_check_security(chan)) {
23760a708f8fSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup) {
237789bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONNECT2);
23780a708f8fSGustavo F. Padovan 				result = L2CAP_CR_PEND;
23790a708f8fSGustavo F. Padovan 				status = L2CAP_CS_AUTHOR_PEND;
23800a708f8fSGustavo F. Padovan 				parent->sk_data_ready(parent, 0);
23810a708f8fSGustavo F. Padovan 			} else {
238289bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONFIG);
23830a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SUCCESS;
23840a708f8fSGustavo F. Padovan 				status = L2CAP_CS_NO_INFO;
23850a708f8fSGustavo F. Padovan 			}
23860a708f8fSGustavo F. Padovan 		} else {
238789bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_CONNECT2);
23880a708f8fSGustavo F. Padovan 			result = L2CAP_CR_PEND;
23890a708f8fSGustavo F. Padovan 			status = L2CAP_CS_AUTHEN_PEND;
23900a708f8fSGustavo F. Padovan 		}
23910a708f8fSGustavo F. Padovan 	} else {
239289bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECT2);
23930a708f8fSGustavo F. Padovan 		result = L2CAP_CR_PEND;
23940a708f8fSGustavo F. Padovan 		status = L2CAP_CS_NO_INFO;
23950a708f8fSGustavo F. Padovan 	}
23960a708f8fSGustavo F. Padovan 
2397baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
23980a708f8fSGustavo F. Padovan 
23990a708f8fSGustavo F. Padovan response:
24000a708f8fSGustavo F. Padovan 	bh_unlock_sock(parent);
24010a708f8fSGustavo F. Padovan 
24020a708f8fSGustavo F. Padovan sendresp:
24030a708f8fSGustavo F. Padovan 	rsp.scid   = cpu_to_le16(scid);
24040a708f8fSGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(dcid);
24050a708f8fSGustavo F. Padovan 	rsp.result = cpu_to_le16(result);
24060a708f8fSGustavo F. Padovan 	rsp.status = cpu_to_le16(status);
24070a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
24080a708f8fSGustavo F. Padovan 
24090a708f8fSGustavo F. Padovan 	if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
24100a708f8fSGustavo F. Padovan 		struct l2cap_info_req info;
24110a708f8fSGustavo F. Padovan 		info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
24120a708f8fSGustavo F. Padovan 
24130a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
24140a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
24150a708f8fSGustavo F. Padovan 
24160a708f8fSGustavo F. Padovan 		mod_timer(&conn->info_timer, jiffies +
24170a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
24180a708f8fSGustavo F. Padovan 
24190a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
24200a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(info), &info);
24210a708f8fSGustavo F. Padovan 	}
24220a708f8fSGustavo F. Padovan 
2423b4450035SGustavo F. Padovan 	if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) &&
24240a708f8fSGustavo F. Padovan 				result == L2CAP_CR_SUCCESS) {
24250a708f8fSGustavo F. Padovan 		u8 buf[128];
2426b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_REQ_SENT;
24270a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
242873ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
242973ffa904SGustavo F. Padovan 		chan->num_conf_req++;
24300a708f8fSGustavo F. Padovan 	}
24310a708f8fSGustavo F. Padovan 
24320a708f8fSGustavo F. Padovan 	return 0;
24330a708f8fSGustavo F. Padovan }
24340a708f8fSGustavo F. Padovan 
24350a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
24360a708f8fSGustavo F. Padovan {
24370a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
24380a708f8fSGustavo F. Padovan 	u16 scid, dcid, result, status;
243948454079SGustavo F. Padovan 	struct l2cap_chan *chan;
24400a708f8fSGustavo F. Padovan 	struct sock *sk;
24410a708f8fSGustavo F. Padovan 	u8 req[128];
24420a708f8fSGustavo F. Padovan 
24430a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
24440a708f8fSGustavo F. Padovan 	dcid   = __le16_to_cpu(rsp->dcid);
24450a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
24460a708f8fSGustavo F. Padovan 	status = __le16_to_cpu(rsp->status);
24470a708f8fSGustavo F. Padovan 
24480a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
24490a708f8fSGustavo F. Padovan 
24500a708f8fSGustavo F. Padovan 	if (scid) {
2451baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, scid);
245248454079SGustavo F. Padovan 		if (!chan)
24530a708f8fSGustavo F. Padovan 			return -EFAULT;
24540a708f8fSGustavo F. Padovan 	} else {
2455baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
245648454079SGustavo F. Padovan 		if (!chan)
24570a708f8fSGustavo F. Padovan 			return -EFAULT;
24580a708f8fSGustavo F. Padovan 	}
24590a708f8fSGustavo F. Padovan 
246048454079SGustavo F. Padovan 	sk = chan->sk;
246148454079SGustavo F. Padovan 
24620a708f8fSGustavo F. Padovan 	switch (result) {
24630a708f8fSGustavo F. Padovan 	case L2CAP_CR_SUCCESS:
246489bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONFIG);
2465fc7f8a7eSGustavo F. Padovan 		chan->ident = 0;
2466fe4128e0SGustavo F. Padovan 		chan->dcid = dcid;
2467b4450035SGustavo F. Padovan 		chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND;
24680a708f8fSGustavo F. Padovan 
2469b4450035SGustavo F. Padovan 		if (chan->conf_state & L2CAP_CONF_REQ_SENT)
24700a708f8fSGustavo F. Padovan 			break;
24710a708f8fSGustavo F. Padovan 
2472b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_REQ_SENT;
24730a708f8fSGustavo F. Padovan 
24740a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
247573ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, req), req);
247673ffa904SGustavo F. Padovan 		chan->num_conf_req++;
24770a708f8fSGustavo F. Padovan 		break;
24780a708f8fSGustavo F. Padovan 
24790a708f8fSGustavo F. Padovan 	case L2CAP_CR_PEND:
2480b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
24810a708f8fSGustavo F. Padovan 		break;
24820a708f8fSGustavo F. Padovan 
24830a708f8fSGustavo F. Padovan 	default:
24840a708f8fSGustavo F. Padovan 		/* don't delete l2cap channel if sk is owned by user */
24850a708f8fSGustavo F. Padovan 		if (sock_owned_by_user(sk)) {
248689bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_DISCONN);
2487c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
2488c9b66675SGustavo F. Padovan 			__set_chan_timer(chan, HZ / 5);
24890a708f8fSGustavo F. Padovan 			break;
24900a708f8fSGustavo F. Padovan 		}
24910a708f8fSGustavo F. Padovan 
249248454079SGustavo F. Padovan 		l2cap_chan_del(chan, ECONNREFUSED);
24930a708f8fSGustavo F. Padovan 		break;
24940a708f8fSGustavo F. Padovan 	}
24950a708f8fSGustavo F. Padovan 
24960a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
24970a708f8fSGustavo F. Padovan 	return 0;
24980a708f8fSGustavo F. Padovan }
24990a708f8fSGustavo F. Padovan 
250047d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan)
25010a708f8fSGustavo F. Padovan {
250247d1ec61SGustavo F. Padovan 	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
250347d1ec61SGustavo F. Padovan 
25040a708f8fSGustavo F. Padovan 	/* FCS is enabled only in ERTM or streaming mode, if one or both
25050a708f8fSGustavo F. Padovan 	 * sides request it.
25060a708f8fSGustavo F. Padovan 	 */
25070c1bc5c6SGustavo F. Padovan 	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
250847d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_NONE;
2509b4450035SGustavo F. Padovan 	else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV))
251047d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_CRC16;
25110a708f8fSGustavo F. Padovan }
25120a708f8fSGustavo F. Padovan 
25130a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
25140a708f8fSGustavo F. Padovan {
25150a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
25160a708f8fSGustavo F. Padovan 	u16 dcid, flags;
25170a708f8fSGustavo F. Padovan 	u8 rsp[64];
251848454079SGustavo F. Padovan 	struct l2cap_chan *chan;
25190a708f8fSGustavo F. Padovan 	struct sock *sk;
25200a708f8fSGustavo F. Padovan 	int len;
25210a708f8fSGustavo F. Padovan 
25220a708f8fSGustavo F. Padovan 	dcid  = __le16_to_cpu(req->dcid);
25230a708f8fSGustavo F. Padovan 	flags = __le16_to_cpu(req->flags);
25240a708f8fSGustavo F. Padovan 
25250a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
25260a708f8fSGustavo F. Padovan 
2527baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
252848454079SGustavo F. Padovan 	if (!chan)
25290a708f8fSGustavo F. Padovan 		return -ENOENT;
25300a708f8fSGustavo F. Padovan 
253148454079SGustavo F. Padovan 	sk = chan->sk;
253248454079SGustavo F. Padovan 
253389bc500eSGustavo F. Padovan 	if (chan->state != BT_CONFIG) {
25340a708f8fSGustavo F. Padovan 		struct l2cap_cmd_rej rej;
25350a708f8fSGustavo F. Padovan 
25360a708f8fSGustavo F. Padovan 		rej.reason = cpu_to_le16(0x0002);
25370a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
25380a708f8fSGustavo F. Padovan 				sizeof(rej), &rej);
25390a708f8fSGustavo F. Padovan 		goto unlock;
25400a708f8fSGustavo F. Padovan 	}
25410a708f8fSGustavo F. Padovan 
25420a708f8fSGustavo F. Padovan 	/* Reject if config buffer is too small. */
25430a708f8fSGustavo F. Padovan 	len = cmd_len - sizeof(*req);
254473ffa904SGustavo F. Padovan 	if (chan->conf_len + len > sizeof(chan->conf_req)) {
25450a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2546fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
25470a708f8fSGustavo F. Padovan 					L2CAP_CONF_REJECT, flags), rsp);
25480a708f8fSGustavo F. Padovan 		goto unlock;
25490a708f8fSGustavo F. Padovan 	}
25500a708f8fSGustavo F. Padovan 
25510a708f8fSGustavo F. Padovan 	/* Store config. */
255273ffa904SGustavo F. Padovan 	memcpy(chan->conf_req + chan->conf_len, req->data, len);
255373ffa904SGustavo F. Padovan 	chan->conf_len += len;
25540a708f8fSGustavo F. Padovan 
25550a708f8fSGustavo F. Padovan 	if (flags & 0x0001) {
25560a708f8fSGustavo F. Padovan 		/* Incomplete config. Send empty response. */
25570a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2558fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
25590a708f8fSGustavo F. Padovan 					L2CAP_CONF_SUCCESS, 0x0001), rsp);
25600a708f8fSGustavo F. Padovan 		goto unlock;
25610a708f8fSGustavo F. Padovan 	}
25620a708f8fSGustavo F. Padovan 
25630a708f8fSGustavo F. Padovan 	/* Complete config. */
256473ffa904SGustavo F. Padovan 	len = l2cap_parse_conf_req(chan, rsp);
25650a708f8fSGustavo F. Padovan 	if (len < 0) {
2566e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
25670a708f8fSGustavo F. Padovan 		goto unlock;
25680a708f8fSGustavo F. Padovan 	}
25690a708f8fSGustavo F. Padovan 
25700a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
257173ffa904SGustavo F. Padovan 	chan->num_conf_rsp++;
25720a708f8fSGustavo F. Padovan 
25730a708f8fSGustavo F. Padovan 	/* Reset config buffer. */
257473ffa904SGustavo F. Padovan 	chan->conf_len = 0;
25750a708f8fSGustavo F. Padovan 
2576b4450035SGustavo F. Padovan 	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE))
25770a708f8fSGustavo F. Padovan 		goto unlock;
25780a708f8fSGustavo F. Padovan 
2579b4450035SGustavo F. Padovan 	if (chan->conf_state & L2CAP_CONF_INPUT_DONE) {
258047d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
25810a708f8fSGustavo F. Padovan 
258289bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
25830a708f8fSGustavo F. Padovan 
258442e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
258542e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
258658d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
25870c1bc5c6SGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_ERTM)
2588525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
25890a708f8fSGustavo F. Padovan 
25900a708f8fSGustavo F. Padovan 		l2cap_chan_ready(sk);
25910a708f8fSGustavo F. Padovan 		goto unlock;
25920a708f8fSGustavo F. Padovan 	}
25930a708f8fSGustavo F. Padovan 
2594b4450035SGustavo F. Padovan 	if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) {
25950a708f8fSGustavo F. Padovan 		u8 buf[64];
2596b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_REQ_SENT;
25970a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
259873ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
259973ffa904SGustavo F. Padovan 		chan->num_conf_req++;
26000a708f8fSGustavo F. Padovan 	}
26010a708f8fSGustavo F. Padovan 
26020a708f8fSGustavo F. Padovan unlock:
26030a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
26040a708f8fSGustavo F. Padovan 	return 0;
26050a708f8fSGustavo F. Padovan }
26060a708f8fSGustavo F. Padovan 
26070a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
26080a708f8fSGustavo F. Padovan {
26090a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
26100a708f8fSGustavo F. Padovan 	u16 scid, flags, result;
261148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
26120a708f8fSGustavo F. Padovan 	struct sock *sk;
26130a708f8fSGustavo F. Padovan 	int len = cmd->len - sizeof(*rsp);
26140a708f8fSGustavo F. Padovan 
26150a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
26160a708f8fSGustavo F. Padovan 	flags  = __le16_to_cpu(rsp->flags);
26170a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
26180a708f8fSGustavo F. Padovan 
26190a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
26200a708f8fSGustavo F. Padovan 			scid, flags, result);
26210a708f8fSGustavo F. Padovan 
2622baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
262348454079SGustavo F. Padovan 	if (!chan)
26240a708f8fSGustavo F. Padovan 		return 0;
26250a708f8fSGustavo F. Padovan 
262648454079SGustavo F. Padovan 	sk = chan->sk;
262748454079SGustavo F. Padovan 
26280a708f8fSGustavo F. Padovan 	switch (result) {
26290a708f8fSGustavo F. Padovan 	case L2CAP_CONF_SUCCESS:
263047d1ec61SGustavo F. Padovan 		l2cap_conf_rfc_get(chan, rsp->data, len);
26310a708f8fSGustavo F. Padovan 		break;
26320a708f8fSGustavo F. Padovan 
26330a708f8fSGustavo F. Padovan 	case L2CAP_CONF_UNACCEPT:
263473ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
26350a708f8fSGustavo F. Padovan 			char req[64];
26360a708f8fSGustavo F. Padovan 
26370a708f8fSGustavo F. Padovan 			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2638e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
26390a708f8fSGustavo F. Padovan 				goto done;
26400a708f8fSGustavo F. Padovan 			}
26410a708f8fSGustavo F. Padovan 
26420a708f8fSGustavo F. Padovan 			/* throw out any old stored conf requests */
26430a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_SUCCESS;
2644b4450035SGustavo F. Padovan 			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
2645b4450035SGustavo F. Padovan 								req, &result);
26460a708f8fSGustavo F. Padovan 			if (len < 0) {
2647e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
26480a708f8fSGustavo F. Padovan 				goto done;
26490a708f8fSGustavo F. Padovan 			}
26500a708f8fSGustavo F. Padovan 
26510a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn),
26520a708f8fSGustavo F. Padovan 						L2CAP_CONF_REQ, len, req);
265373ffa904SGustavo F. Padovan 			chan->num_conf_req++;
26540a708f8fSGustavo F. Padovan 			if (result != L2CAP_CONF_SUCCESS)
26550a708f8fSGustavo F. Padovan 				goto done;
26560a708f8fSGustavo F. Padovan 			break;
26570a708f8fSGustavo F. Padovan 		}
26580a708f8fSGustavo F. Padovan 
26590a708f8fSGustavo F. Padovan 	default:
26600a708f8fSGustavo F. Padovan 		sk->sk_err = ECONNRESET;
2661c9b66675SGustavo F. Padovan 		__set_chan_timer(chan, HZ * 5);
2662e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
26630a708f8fSGustavo F. Padovan 		goto done;
26640a708f8fSGustavo F. Padovan 	}
26650a708f8fSGustavo F. Padovan 
26660a708f8fSGustavo F. Padovan 	if (flags & 0x01)
26670a708f8fSGustavo F. Padovan 		goto done;
26680a708f8fSGustavo F. Padovan 
2669b4450035SGustavo F. Padovan 	chan->conf_state |= L2CAP_CONF_INPUT_DONE;
26700a708f8fSGustavo F. Padovan 
2671b4450035SGustavo F. Padovan 	if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) {
267247d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
26730a708f8fSGustavo F. Padovan 
267489bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
267542e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
267642e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
267758d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
26780c1bc5c6SGustavo F. Padovan 		if (chan->mode ==  L2CAP_MODE_ERTM)
2679525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
26800a708f8fSGustavo F. Padovan 
26810a708f8fSGustavo F. Padovan 		l2cap_chan_ready(sk);
26820a708f8fSGustavo F. Padovan 	}
26830a708f8fSGustavo F. Padovan 
26840a708f8fSGustavo F. Padovan done:
26850a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
26860a708f8fSGustavo F. Padovan 	return 0;
26870a708f8fSGustavo F. Padovan }
26880a708f8fSGustavo F. Padovan 
26890a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
26900a708f8fSGustavo F. Padovan {
26910a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
26920a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp rsp;
26930a708f8fSGustavo F. Padovan 	u16 dcid, scid;
269448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
26950a708f8fSGustavo F. Padovan 	struct sock *sk;
26960a708f8fSGustavo F. Padovan 
26970a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(req->scid);
26980a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(req->dcid);
26990a708f8fSGustavo F. Padovan 
27000a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
27010a708f8fSGustavo F. Padovan 
2702baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
270348454079SGustavo F. Padovan 	if (!chan)
27040a708f8fSGustavo F. Padovan 		return 0;
27050a708f8fSGustavo F. Padovan 
270648454079SGustavo F. Padovan 	sk = chan->sk;
270748454079SGustavo F. Padovan 
2708fe4128e0SGustavo F. Padovan 	rsp.dcid = cpu_to_le16(chan->scid);
2709fe4128e0SGustavo F. Padovan 	rsp.scid = cpu_to_le16(chan->dcid);
27100a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
27110a708f8fSGustavo F. Padovan 
27120a708f8fSGustavo F. Padovan 	sk->sk_shutdown = SHUTDOWN_MASK;
27130a708f8fSGustavo F. Padovan 
27140a708f8fSGustavo F. Padovan 	/* don't delete l2cap channel if sk is owned by user */
27150a708f8fSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
271689bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_DISCONN);
2717c9b66675SGustavo F. Padovan 		__clear_chan_timer(chan);
2718c9b66675SGustavo F. Padovan 		__set_chan_timer(chan, HZ / 5);
27190a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
27200a708f8fSGustavo F. Padovan 		return 0;
27210a708f8fSGustavo F. Padovan 	}
27220a708f8fSGustavo F. Padovan 
272348454079SGustavo F. Padovan 	l2cap_chan_del(chan, ECONNRESET);
27240a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
27250a708f8fSGustavo F. Padovan 
2726ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
27270a708f8fSGustavo F. Padovan 	return 0;
27280a708f8fSGustavo F. Padovan }
27290a708f8fSGustavo F. Padovan 
27300a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
27310a708f8fSGustavo F. Padovan {
27320a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
27330a708f8fSGustavo F. Padovan 	u16 dcid, scid;
273448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
27350a708f8fSGustavo F. Padovan 	struct sock *sk;
27360a708f8fSGustavo F. Padovan 
27370a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(rsp->scid);
27380a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(rsp->dcid);
27390a708f8fSGustavo F. Padovan 
27400a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
27410a708f8fSGustavo F. Padovan 
2742baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
274348454079SGustavo F. Padovan 	if (!chan)
27440a708f8fSGustavo F. Padovan 		return 0;
27450a708f8fSGustavo F. Padovan 
274648454079SGustavo F. Padovan 	sk = chan->sk;
274748454079SGustavo F. Padovan 
27480a708f8fSGustavo F. Padovan 	/* don't delete l2cap channel if sk is owned by user */
27490a708f8fSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
275089bc500eSGustavo F. Padovan 		l2cap_state_change(chan,BT_DISCONN);
2751c9b66675SGustavo F. Padovan 		__clear_chan_timer(chan);
2752c9b66675SGustavo F. Padovan 		__set_chan_timer(chan, HZ / 5);
27530a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
27540a708f8fSGustavo F. Padovan 		return 0;
27550a708f8fSGustavo F. Padovan 	}
27560a708f8fSGustavo F. Padovan 
275748454079SGustavo F. Padovan 	l2cap_chan_del(chan, 0);
27580a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
27590a708f8fSGustavo F. Padovan 
2760ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
27610a708f8fSGustavo F. Padovan 	return 0;
27620a708f8fSGustavo F. Padovan }
27630a708f8fSGustavo F. Padovan 
27640a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
27650a708f8fSGustavo F. Padovan {
27660a708f8fSGustavo F. Padovan 	struct l2cap_info_req *req = (struct l2cap_info_req *) data;
27670a708f8fSGustavo F. Padovan 	u16 type;
27680a708f8fSGustavo F. Padovan 
27690a708f8fSGustavo F. Padovan 	type = __le16_to_cpu(req->type);
27700a708f8fSGustavo F. Padovan 
27710a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x", type);
27720a708f8fSGustavo F. Padovan 
27730a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
27740a708f8fSGustavo F. Padovan 		u8 buf[8];
27750a708f8fSGustavo F. Padovan 		u32 feat_mask = l2cap_feat_mask;
27760a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
27770a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FEAT_MASK);
27780a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
27790a708f8fSGustavo F. Padovan 		if (!disable_ertm)
27800a708f8fSGustavo F. Padovan 			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
27810a708f8fSGustavo F. Padovan 							 | L2CAP_FEAT_FCS;
27820a708f8fSGustavo F. Padovan 		put_unaligned_le32(feat_mask, rsp->data);
27830a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
27840a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
27850a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
27860a708f8fSGustavo F. Padovan 		u8 buf[12];
27870a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
27880a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
27890a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
27900a708f8fSGustavo F. Padovan 		memcpy(buf + 4, l2cap_fixed_chan, 8);
27910a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
27920a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
27930a708f8fSGustavo F. Padovan 	} else {
27940a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp rsp;
27950a708f8fSGustavo F. Padovan 		rsp.type   = cpu_to_le16(type);
27960a708f8fSGustavo F. Padovan 		rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
27970a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
27980a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(rsp), &rsp);
27990a708f8fSGustavo F. Padovan 	}
28000a708f8fSGustavo F. Padovan 
28010a708f8fSGustavo F. Padovan 	return 0;
28020a708f8fSGustavo F. Padovan }
28030a708f8fSGustavo F. Padovan 
28040a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
28050a708f8fSGustavo F. Padovan {
28060a708f8fSGustavo F. Padovan 	struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
28070a708f8fSGustavo F. Padovan 	u16 type, result;
28080a708f8fSGustavo F. Padovan 
28090a708f8fSGustavo F. Padovan 	type   = __le16_to_cpu(rsp->type);
28100a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
28110a708f8fSGustavo F. Padovan 
28120a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
28130a708f8fSGustavo F. Padovan 
2814e90165beSAndrei Emeltchenko 	/* L2CAP Info req/rsp are unbound to channels, add extra checks */
2815e90165beSAndrei Emeltchenko 	if (cmd->ident != conn->info_ident ||
2816e90165beSAndrei Emeltchenko 			conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
2817e90165beSAndrei Emeltchenko 		return 0;
2818e90165beSAndrei Emeltchenko 
28190a708f8fSGustavo F. Padovan 	del_timer(&conn->info_timer);
28200a708f8fSGustavo F. Padovan 
28210a708f8fSGustavo F. Padovan 	if (result != L2CAP_IR_SUCCESS) {
28220a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28230a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
28240a708f8fSGustavo F. Padovan 
28250a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
28260a708f8fSGustavo F. Padovan 
28270a708f8fSGustavo F. Padovan 		return 0;
28280a708f8fSGustavo F. Padovan 	}
28290a708f8fSGustavo F. Padovan 
28300a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
28310a708f8fSGustavo F. Padovan 		conn->feat_mask = get_unaligned_le32(rsp->data);
28320a708f8fSGustavo F. Padovan 
28330a708f8fSGustavo F. Padovan 		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
28340a708f8fSGustavo F. Padovan 			struct l2cap_info_req req;
28350a708f8fSGustavo F. Padovan 			req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
28360a708f8fSGustavo F. Padovan 
28370a708f8fSGustavo F. Padovan 			conn->info_ident = l2cap_get_ident(conn);
28380a708f8fSGustavo F. Padovan 
28390a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, conn->info_ident,
28400a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
28410a708f8fSGustavo F. Padovan 		} else {
28420a708f8fSGustavo F. Padovan 			conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28430a708f8fSGustavo F. Padovan 			conn->info_ident = 0;
28440a708f8fSGustavo F. Padovan 
28450a708f8fSGustavo F. Padovan 			l2cap_conn_start(conn);
28460a708f8fSGustavo F. Padovan 		}
28470a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
28480a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28490a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
28500a708f8fSGustavo F. Padovan 
28510a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
28520a708f8fSGustavo F. Padovan 	}
28530a708f8fSGustavo F. Padovan 
28540a708f8fSGustavo F. Padovan 	return 0;
28550a708f8fSGustavo F. Padovan }
28560a708f8fSGustavo F. Padovan 
2857e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
2858de73115aSClaudio Takahasi 							u16 to_multiplier)
2859de73115aSClaudio Takahasi {
2860de73115aSClaudio Takahasi 	u16 max_latency;
2861de73115aSClaudio Takahasi 
2862de73115aSClaudio Takahasi 	if (min > max || min < 6 || max > 3200)
2863de73115aSClaudio Takahasi 		return -EINVAL;
2864de73115aSClaudio Takahasi 
2865de73115aSClaudio Takahasi 	if (to_multiplier < 10 || to_multiplier > 3200)
2866de73115aSClaudio Takahasi 		return -EINVAL;
2867de73115aSClaudio Takahasi 
2868de73115aSClaudio Takahasi 	if (max >= to_multiplier * 8)
2869de73115aSClaudio Takahasi 		return -EINVAL;
2870de73115aSClaudio Takahasi 
2871de73115aSClaudio Takahasi 	max_latency = (to_multiplier * 8 / max) - 1;
2872de73115aSClaudio Takahasi 	if (latency > 499 || latency > max_latency)
2873de73115aSClaudio Takahasi 		return -EINVAL;
2874de73115aSClaudio Takahasi 
2875de73115aSClaudio Takahasi 	return 0;
2876de73115aSClaudio Takahasi }
2877de73115aSClaudio Takahasi 
2878de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
2879de73115aSClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
2880de73115aSClaudio Takahasi {
2881de73115aSClaudio Takahasi 	struct hci_conn *hcon = conn->hcon;
2882de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_req *req;
2883de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_rsp rsp;
2884de73115aSClaudio Takahasi 	u16 min, max, latency, to_multiplier, cmd_len;
28852ce603ebSClaudio Takahasi 	int err;
2886de73115aSClaudio Takahasi 
2887de73115aSClaudio Takahasi 	if (!(hcon->link_mode & HCI_LM_MASTER))
2888de73115aSClaudio Takahasi 		return -EINVAL;
2889de73115aSClaudio Takahasi 
2890de73115aSClaudio Takahasi 	cmd_len = __le16_to_cpu(cmd->len);
2891de73115aSClaudio Takahasi 	if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
2892de73115aSClaudio Takahasi 		return -EPROTO;
2893de73115aSClaudio Takahasi 
2894de73115aSClaudio Takahasi 	req = (struct l2cap_conn_param_update_req *) data;
2895de73115aSClaudio Takahasi 	min		= __le16_to_cpu(req->min);
2896de73115aSClaudio Takahasi 	max		= __le16_to_cpu(req->max);
2897de73115aSClaudio Takahasi 	latency		= __le16_to_cpu(req->latency);
2898de73115aSClaudio Takahasi 	to_multiplier	= __le16_to_cpu(req->to_multiplier);
2899de73115aSClaudio Takahasi 
2900de73115aSClaudio Takahasi 	BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
2901de73115aSClaudio Takahasi 						min, max, latency, to_multiplier);
2902de73115aSClaudio Takahasi 
2903de73115aSClaudio Takahasi 	memset(&rsp, 0, sizeof(rsp));
29042ce603ebSClaudio Takahasi 
29052ce603ebSClaudio Takahasi 	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
29062ce603ebSClaudio Takahasi 	if (err)
2907de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
2908de73115aSClaudio Takahasi 	else
2909de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
2910de73115aSClaudio Takahasi 
2911de73115aSClaudio Takahasi 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
2912de73115aSClaudio Takahasi 							sizeof(rsp), &rsp);
2913de73115aSClaudio Takahasi 
29142ce603ebSClaudio Takahasi 	if (!err)
29152ce603ebSClaudio Takahasi 		hci_le_conn_update(hcon, min, max, latency, to_multiplier);
29162ce603ebSClaudio Takahasi 
2917de73115aSClaudio Takahasi 	return 0;
2918de73115aSClaudio Takahasi }
2919de73115aSClaudio Takahasi 
29203300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
29213300d9a9SClaudio Takahasi 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
29223300d9a9SClaudio Takahasi {
29233300d9a9SClaudio Takahasi 	int err = 0;
29243300d9a9SClaudio Takahasi 
29253300d9a9SClaudio Takahasi 	switch (cmd->code) {
29263300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
29273300d9a9SClaudio Takahasi 		l2cap_command_rej(conn, cmd, data);
29283300d9a9SClaudio Takahasi 		break;
29293300d9a9SClaudio Takahasi 
29303300d9a9SClaudio Takahasi 	case L2CAP_CONN_REQ:
29313300d9a9SClaudio Takahasi 		err = l2cap_connect_req(conn, cmd, data);
29323300d9a9SClaudio Takahasi 		break;
29333300d9a9SClaudio Takahasi 
29343300d9a9SClaudio Takahasi 	case L2CAP_CONN_RSP:
29353300d9a9SClaudio Takahasi 		err = l2cap_connect_rsp(conn, cmd, data);
29363300d9a9SClaudio Takahasi 		break;
29373300d9a9SClaudio Takahasi 
29383300d9a9SClaudio Takahasi 	case L2CAP_CONF_REQ:
29393300d9a9SClaudio Takahasi 		err = l2cap_config_req(conn, cmd, cmd_len, data);
29403300d9a9SClaudio Takahasi 		break;
29413300d9a9SClaudio Takahasi 
29423300d9a9SClaudio Takahasi 	case L2CAP_CONF_RSP:
29433300d9a9SClaudio Takahasi 		err = l2cap_config_rsp(conn, cmd, data);
29443300d9a9SClaudio Takahasi 		break;
29453300d9a9SClaudio Takahasi 
29463300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_REQ:
29473300d9a9SClaudio Takahasi 		err = l2cap_disconnect_req(conn, cmd, data);
29483300d9a9SClaudio Takahasi 		break;
29493300d9a9SClaudio Takahasi 
29503300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_RSP:
29513300d9a9SClaudio Takahasi 		err = l2cap_disconnect_rsp(conn, cmd, data);
29523300d9a9SClaudio Takahasi 		break;
29533300d9a9SClaudio Takahasi 
29543300d9a9SClaudio Takahasi 	case L2CAP_ECHO_REQ:
29553300d9a9SClaudio Takahasi 		l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
29563300d9a9SClaudio Takahasi 		break;
29573300d9a9SClaudio Takahasi 
29583300d9a9SClaudio Takahasi 	case L2CAP_ECHO_RSP:
29593300d9a9SClaudio Takahasi 		break;
29603300d9a9SClaudio Takahasi 
29613300d9a9SClaudio Takahasi 	case L2CAP_INFO_REQ:
29623300d9a9SClaudio Takahasi 		err = l2cap_information_req(conn, cmd, data);
29633300d9a9SClaudio Takahasi 		break;
29643300d9a9SClaudio Takahasi 
29653300d9a9SClaudio Takahasi 	case L2CAP_INFO_RSP:
29663300d9a9SClaudio Takahasi 		err = l2cap_information_rsp(conn, cmd, data);
29673300d9a9SClaudio Takahasi 		break;
29683300d9a9SClaudio Takahasi 
29693300d9a9SClaudio Takahasi 	default:
29703300d9a9SClaudio Takahasi 		BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
29713300d9a9SClaudio Takahasi 		err = -EINVAL;
29723300d9a9SClaudio Takahasi 		break;
29733300d9a9SClaudio Takahasi 	}
29743300d9a9SClaudio Takahasi 
29753300d9a9SClaudio Takahasi 	return err;
29763300d9a9SClaudio Takahasi }
29773300d9a9SClaudio Takahasi 
29783300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
29793300d9a9SClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
29803300d9a9SClaudio Takahasi {
29813300d9a9SClaudio Takahasi 	switch (cmd->code) {
29823300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
29833300d9a9SClaudio Takahasi 		return 0;
29843300d9a9SClaudio Takahasi 
29853300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_REQ:
2986de73115aSClaudio Takahasi 		return l2cap_conn_param_update_req(conn, cmd, data);
29873300d9a9SClaudio Takahasi 
29883300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_RSP:
29893300d9a9SClaudio Takahasi 		return 0;
29903300d9a9SClaudio Takahasi 
29913300d9a9SClaudio Takahasi 	default:
29923300d9a9SClaudio Takahasi 		BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
29933300d9a9SClaudio Takahasi 		return -EINVAL;
29943300d9a9SClaudio Takahasi 	}
29953300d9a9SClaudio Takahasi }
29963300d9a9SClaudio Takahasi 
29973300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn,
29983300d9a9SClaudio Takahasi 							struct sk_buff *skb)
29990a708f8fSGustavo F. Padovan {
30000a708f8fSGustavo F. Padovan 	u8 *data = skb->data;
30010a708f8fSGustavo F. Padovan 	int len = skb->len;
30020a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr cmd;
30033300d9a9SClaudio Takahasi 	int err;
30040a708f8fSGustavo F. Padovan 
30050a708f8fSGustavo F. Padovan 	l2cap_raw_recv(conn, skb);
30060a708f8fSGustavo F. Padovan 
30070a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CMD_HDR_SIZE) {
30080a708f8fSGustavo F. Padovan 		u16 cmd_len;
30090a708f8fSGustavo F. Padovan 		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
30100a708f8fSGustavo F. Padovan 		data += L2CAP_CMD_HDR_SIZE;
30110a708f8fSGustavo F. Padovan 		len  -= L2CAP_CMD_HDR_SIZE;
30120a708f8fSGustavo F. Padovan 
30130a708f8fSGustavo F. Padovan 		cmd_len = le16_to_cpu(cmd.len);
30140a708f8fSGustavo F. Padovan 
30150a708f8fSGustavo F. Padovan 		BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
30160a708f8fSGustavo F. Padovan 
30170a708f8fSGustavo F. Padovan 		if (cmd_len > len || !cmd.ident) {
30180a708f8fSGustavo F. Padovan 			BT_DBG("corrupted command");
30190a708f8fSGustavo F. Padovan 			break;
30200a708f8fSGustavo F. Padovan 		}
30210a708f8fSGustavo F. Padovan 
30223300d9a9SClaudio Takahasi 		if (conn->hcon->type == LE_LINK)
30233300d9a9SClaudio Takahasi 			err = l2cap_le_sig_cmd(conn, &cmd, data);
30243300d9a9SClaudio Takahasi 		else
30253300d9a9SClaudio Takahasi 			err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
30260a708f8fSGustavo F. Padovan 
30270a708f8fSGustavo F. Padovan 		if (err) {
30280a708f8fSGustavo F. Padovan 			struct l2cap_cmd_rej rej;
30292c6d1a2eSGustavo F. Padovan 
30302c6d1a2eSGustavo F. Padovan 			BT_ERR("Wrong link type (%d)", err);
30310a708f8fSGustavo F. Padovan 
30320a708f8fSGustavo F. Padovan 			/* FIXME: Map err to a valid reason */
30330a708f8fSGustavo F. Padovan 			rej.reason = cpu_to_le16(0);
30340a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
30350a708f8fSGustavo F. Padovan 		}
30360a708f8fSGustavo F. Padovan 
30370a708f8fSGustavo F. Padovan 		data += cmd_len;
30380a708f8fSGustavo F. Padovan 		len  -= cmd_len;
30390a708f8fSGustavo F. Padovan 	}
30400a708f8fSGustavo F. Padovan 
30410a708f8fSGustavo F. Padovan 	kfree_skb(skb);
30420a708f8fSGustavo F. Padovan }
30430a708f8fSGustavo F. Padovan 
304447d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
30450a708f8fSGustavo F. Padovan {
30460a708f8fSGustavo F. Padovan 	u16 our_fcs, rcv_fcs;
30470a708f8fSGustavo F. Padovan 	int hdr_size = L2CAP_HDR_SIZE + 2;
30480a708f8fSGustavo F. Padovan 
304947d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
30500a708f8fSGustavo F. Padovan 		skb_trim(skb, skb->len - 2);
30510a708f8fSGustavo F. Padovan 		rcv_fcs = get_unaligned_le16(skb->data + skb->len);
30520a708f8fSGustavo F. Padovan 		our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
30530a708f8fSGustavo F. Padovan 
30540a708f8fSGustavo F. Padovan 		if (our_fcs != rcv_fcs)
30550a708f8fSGustavo F. Padovan 			return -EBADMSG;
30560a708f8fSGustavo F. Padovan 	}
30570a708f8fSGustavo F. Padovan 	return 0;
30580a708f8fSGustavo F. Padovan }
30590a708f8fSGustavo F. Padovan 
3060525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
30610a708f8fSGustavo F. Padovan {
30620a708f8fSGustavo F. Padovan 	u16 control = 0;
30630a708f8fSGustavo F. Padovan 
30646a026610SGustavo F. Padovan 	chan->frames_sent = 0;
30650a708f8fSGustavo F. Padovan 
306642e5c802SGustavo F. Padovan 	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
30670a708f8fSGustavo F. Padovan 
3068525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
30690a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_NOT_READY;
3070525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
3071525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_RNR_SENT;
30720a708f8fSGustavo F. Padovan 	}
30730a708f8fSGustavo F. Padovan 
3074525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY)
3075525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
30760a708f8fSGustavo F. Padovan 
3077525cd185SGustavo F. Padovan 	l2cap_ertm_send(chan);
30780a708f8fSGustavo F. Padovan 
3079525cd185SGustavo F. Padovan 	if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
30806a026610SGustavo F. Padovan 			chan->frames_sent == 0) {
30810a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_READY;
3082525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
30830a708f8fSGustavo F. Padovan 	}
30840a708f8fSGustavo F. Padovan }
30850a708f8fSGustavo F. Padovan 
308642e5c802SGustavo F. Padovan static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar)
30870a708f8fSGustavo F. Padovan {
30880a708f8fSGustavo F. Padovan 	struct sk_buff *next_skb;
30890a708f8fSGustavo F. Padovan 	int tx_seq_offset, next_tx_seq_offset;
30900a708f8fSGustavo F. Padovan 
30910a708f8fSGustavo F. Padovan 	bt_cb(skb)->tx_seq = tx_seq;
30920a708f8fSGustavo F. Padovan 	bt_cb(skb)->sar = sar;
30930a708f8fSGustavo F. Padovan 
3094f1c6775bSGustavo F. Padovan 	next_skb = skb_peek(&chan->srej_q);
30950a708f8fSGustavo F. Padovan 	if (!next_skb) {
3096f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->srej_q, skb);
30970a708f8fSGustavo F. Padovan 		return 0;
30980a708f8fSGustavo F. Padovan 	}
30990a708f8fSGustavo F. Padovan 
310042e5c802SGustavo F. Padovan 	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
31010a708f8fSGustavo F. Padovan 	if (tx_seq_offset < 0)
31020a708f8fSGustavo F. Padovan 		tx_seq_offset += 64;
31030a708f8fSGustavo F. Padovan 
31040a708f8fSGustavo F. Padovan 	do {
31050a708f8fSGustavo F. Padovan 		if (bt_cb(next_skb)->tx_seq == tx_seq)
31060a708f8fSGustavo F. Padovan 			return -EINVAL;
31070a708f8fSGustavo F. Padovan 
31080a708f8fSGustavo F. Padovan 		next_tx_seq_offset = (bt_cb(next_skb)->tx_seq -
310942e5c802SGustavo F. Padovan 						chan->buffer_seq) % 64;
31100a708f8fSGustavo F. Padovan 		if (next_tx_seq_offset < 0)
31110a708f8fSGustavo F. Padovan 			next_tx_seq_offset += 64;
31120a708f8fSGustavo F. Padovan 
31130a708f8fSGustavo F. Padovan 		if (next_tx_seq_offset > tx_seq_offset) {
3114f1c6775bSGustavo F. Padovan 			__skb_queue_before(&chan->srej_q, next_skb, skb);
31150a708f8fSGustavo F. Padovan 			return 0;
31160a708f8fSGustavo F. Padovan 		}
31170a708f8fSGustavo F. Padovan 
3118f1c6775bSGustavo F. Padovan 		if (skb_queue_is_last(&chan->srej_q, next_skb))
31190a708f8fSGustavo F. Padovan 			break;
31200a708f8fSGustavo F. Padovan 
3121f1c6775bSGustavo F. Padovan 	} while ((next_skb = skb_queue_next(&chan->srej_q, next_skb)));
31220a708f8fSGustavo F. Padovan 
3123f1c6775bSGustavo F. Padovan 	__skb_queue_tail(&chan->srej_q, skb);
31240a708f8fSGustavo F. Padovan 
31250a708f8fSGustavo F. Padovan 	return 0;
31260a708f8fSGustavo F. Padovan }
31270a708f8fSGustavo F. Padovan 
3128525cd185SGustavo F. Padovan static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
31290a708f8fSGustavo F. Padovan {
31300a708f8fSGustavo F. Padovan 	struct sk_buff *_skb;
31310a708f8fSGustavo F. Padovan 	int err;
31320a708f8fSGustavo F. Padovan 
31330a708f8fSGustavo F. Padovan 	switch (control & L2CAP_CTRL_SAR) {
31340a708f8fSGustavo F. Padovan 	case L2CAP_SDU_UNSEGMENTED:
3135525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
31360a708f8fSGustavo F. Padovan 			goto drop;
31370a708f8fSGustavo F. Padovan 
313823070494SGustavo F. Padovan 		return chan->ops->recv(chan->data, skb);
31390a708f8fSGustavo F. Padovan 
31400a708f8fSGustavo F. Padovan 	case L2CAP_SDU_START:
3141525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
31420a708f8fSGustavo F. Padovan 			goto drop;
31430a708f8fSGustavo F. Padovan 
31446f61fd47SGustavo F. Padovan 		chan->sdu_len = get_unaligned_le16(skb->data);
31450a708f8fSGustavo F. Padovan 
31460c1bc5c6SGustavo F. Padovan 		if (chan->sdu_len > chan->imtu)
31470a708f8fSGustavo F. Padovan 			goto disconnect;
31480a708f8fSGustavo F. Padovan 
31496f61fd47SGustavo F. Padovan 		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
31506f61fd47SGustavo F. Padovan 		if (!chan->sdu)
31510a708f8fSGustavo F. Padovan 			return -ENOMEM;
31520a708f8fSGustavo F. Padovan 
31530a708f8fSGustavo F. Padovan 		/* pull sdu_len bytes only after alloc, because of Local Busy
31540a708f8fSGustavo F. Padovan 		 * condition we have to be sure that this will be executed
31550a708f8fSGustavo F. Padovan 		 * only once, i.e., when alloc does not fail */
31560a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
31570a708f8fSGustavo F. Padovan 
31586f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
31590a708f8fSGustavo F. Padovan 
3160525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SAR_SDU;
31616f61fd47SGustavo F. Padovan 		chan->partial_sdu_len = skb->len;
31620a708f8fSGustavo F. Padovan 		break;
31630a708f8fSGustavo F. Padovan 
31640a708f8fSGustavo F. Padovan 	case L2CAP_SDU_CONTINUE:
3165525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
31660a708f8fSGustavo F. Padovan 			goto disconnect;
31670a708f8fSGustavo F. Padovan 
31686f61fd47SGustavo F. Padovan 		if (!chan->sdu)
31690a708f8fSGustavo F. Padovan 			goto disconnect;
31700a708f8fSGustavo F. Padovan 
31716f61fd47SGustavo F. Padovan 		chan->partial_sdu_len += skb->len;
31726f61fd47SGustavo F. Padovan 		if (chan->partial_sdu_len > chan->sdu_len)
31730a708f8fSGustavo F. Padovan 			goto drop;
31740a708f8fSGustavo F. Padovan 
31756f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
31760a708f8fSGustavo F. Padovan 
31770a708f8fSGustavo F. Padovan 		break;
31780a708f8fSGustavo F. Padovan 
31790a708f8fSGustavo F. Padovan 	case L2CAP_SDU_END:
3180525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
31810a708f8fSGustavo F. Padovan 			goto disconnect;
31820a708f8fSGustavo F. Padovan 
31836f61fd47SGustavo F. Padovan 		if (!chan->sdu)
31840a708f8fSGustavo F. Padovan 			goto disconnect;
31850a708f8fSGustavo F. Padovan 
3186525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) {
31876f61fd47SGustavo F. Padovan 			chan->partial_sdu_len += skb->len;
31880a708f8fSGustavo F. Padovan 
31890c1bc5c6SGustavo F. Padovan 			if (chan->partial_sdu_len > chan->imtu)
31900a708f8fSGustavo F. Padovan 				goto drop;
31910a708f8fSGustavo F. Padovan 
31926f61fd47SGustavo F. Padovan 			if (chan->partial_sdu_len != chan->sdu_len)
31930a708f8fSGustavo F. Padovan 				goto drop;
31940a708f8fSGustavo F. Padovan 
31956f61fd47SGustavo F. Padovan 			memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
31960a708f8fSGustavo F. Padovan 		}
31970a708f8fSGustavo F. Padovan 
31986f61fd47SGustavo F. Padovan 		_skb = skb_clone(chan->sdu, GFP_ATOMIC);
31990a708f8fSGustavo F. Padovan 		if (!_skb) {
3200525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
32010a708f8fSGustavo F. Padovan 			return -ENOMEM;
32020a708f8fSGustavo F. Padovan 		}
32030a708f8fSGustavo F. Padovan 
320423070494SGustavo F. Padovan 		err = chan->ops->recv(chan->data, _skb);
32050a708f8fSGustavo F. Padovan 		if (err < 0) {
32060a708f8fSGustavo F. Padovan 			kfree_skb(_skb);
3207525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
32080a708f8fSGustavo F. Padovan 			return err;
32090a708f8fSGustavo F. Padovan 		}
32100a708f8fSGustavo F. Padovan 
3211525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SAR_RETRY;
3212525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
32130a708f8fSGustavo F. Padovan 
32146f61fd47SGustavo F. Padovan 		kfree_skb(chan->sdu);
32150a708f8fSGustavo F. Padovan 		break;
32160a708f8fSGustavo F. Padovan 	}
32170a708f8fSGustavo F. Padovan 
32180a708f8fSGustavo F. Padovan 	kfree_skb(skb);
32190a708f8fSGustavo F. Padovan 	return 0;
32200a708f8fSGustavo F. Padovan 
32210a708f8fSGustavo F. Padovan drop:
32226f61fd47SGustavo F. Padovan 	kfree_skb(chan->sdu);
32236f61fd47SGustavo F. Padovan 	chan->sdu = NULL;
32240a708f8fSGustavo F. Padovan 
32250a708f8fSGustavo F. Padovan disconnect:
32268c1d787bSGustavo F. Padovan 	l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
32270a708f8fSGustavo F. Padovan 	kfree_skb(skb);
32280a708f8fSGustavo F. Padovan 	return 0;
32290a708f8fSGustavo F. Padovan }
32300a708f8fSGustavo F. Padovan 
3231525cd185SGustavo F. Padovan static int l2cap_try_push_rx_skb(struct l2cap_chan *chan)
32320a708f8fSGustavo F. Padovan {
32330a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
32340a708f8fSGustavo F. Padovan 	u16 control;
32350a708f8fSGustavo F. Padovan 	int err;
32360a708f8fSGustavo F. Padovan 
3237f1c6775bSGustavo F. Padovan 	while ((skb = skb_dequeue(&chan->busy_q))) {
32380a708f8fSGustavo F. Padovan 		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3239525cd185SGustavo F. Padovan 		err = l2cap_ertm_reassembly_sdu(chan, skb, control);
32400a708f8fSGustavo F. Padovan 		if (err < 0) {
3241f1c6775bSGustavo F. Padovan 			skb_queue_head(&chan->busy_q, skb);
32420a708f8fSGustavo F. Padovan 			return -EBUSY;
32430a708f8fSGustavo F. Padovan 		}
32440a708f8fSGustavo F. Padovan 
324542e5c802SGustavo F. Padovan 		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
32460a708f8fSGustavo F. Padovan 	}
32470a708f8fSGustavo F. Padovan 
3248525cd185SGustavo F. Padovan 	if (!(chan->conn_state & L2CAP_CONN_RNR_SENT))
32490a708f8fSGustavo F. Padovan 		goto done;
32500a708f8fSGustavo F. Padovan 
325142e5c802SGustavo F. Padovan 	control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
32520a708f8fSGustavo F. Padovan 	control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL;
3253525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
32546a026610SGustavo F. Padovan 	chan->retry_count = 1;
32550a708f8fSGustavo F. Padovan 
32561a09bcb9SGustavo F. Padovan 	__clear_retrans_timer(chan);
32571a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
32580a708f8fSGustavo F. Padovan 
3259525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_WAIT_F;
32600a708f8fSGustavo F. Padovan 
32610a708f8fSGustavo F. Padovan done:
3262525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
3263525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_RNR_SENT;
32640a708f8fSGustavo F. Padovan 
326549208c9cSGustavo F. Padovan 	BT_DBG("chan %p, Exit local busy", chan);
32660a708f8fSGustavo F. Padovan 
32670a708f8fSGustavo F. Padovan 	return 0;
32680a708f8fSGustavo F. Padovan }
32690a708f8fSGustavo F. Padovan 
32700a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work)
32710a708f8fSGustavo F. Padovan {
32720a708f8fSGustavo F. Padovan 	DECLARE_WAITQUEUE(wait, current);
3273311bb895SGustavo F. Padovan 	struct l2cap_chan *chan =
3274311bb895SGustavo F. Padovan 		container_of(work, struct l2cap_chan, busy_work);
3275311bb895SGustavo F. Padovan 	struct sock *sk = chan->sk;
32760a708f8fSGustavo F. Padovan 	int n_tries = 0, timeo = HZ/5, err;
32770a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
32780a708f8fSGustavo F. Padovan 
32790a708f8fSGustavo F. Padovan 	lock_sock(sk);
32800a708f8fSGustavo F. Padovan 
32810a708f8fSGustavo F. Padovan 	add_wait_queue(sk_sleep(sk), &wait);
3282311bb895SGustavo F. Padovan 	while ((skb = skb_peek(&chan->busy_q))) {
32830a708f8fSGustavo F. Padovan 		set_current_state(TASK_INTERRUPTIBLE);
32840a708f8fSGustavo F. Padovan 
32850a708f8fSGustavo F. Padovan 		if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) {
32860a708f8fSGustavo F. Padovan 			err = -EBUSY;
32878c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, EBUSY);
32880a708f8fSGustavo F. Padovan 			break;
32890a708f8fSGustavo F. Padovan 		}
32900a708f8fSGustavo F. Padovan 
32910a708f8fSGustavo F. Padovan 		if (!timeo)
32920a708f8fSGustavo F. Padovan 			timeo = HZ/5;
32930a708f8fSGustavo F. Padovan 
32940a708f8fSGustavo F. Padovan 		if (signal_pending(current)) {
32950a708f8fSGustavo F. Padovan 			err = sock_intr_errno(timeo);
32960a708f8fSGustavo F. Padovan 			break;
32970a708f8fSGustavo F. Padovan 		}
32980a708f8fSGustavo F. Padovan 
32990a708f8fSGustavo F. Padovan 		release_sock(sk);
33000a708f8fSGustavo F. Padovan 		timeo = schedule_timeout(timeo);
33010a708f8fSGustavo F. Padovan 		lock_sock(sk);
33020a708f8fSGustavo F. Padovan 
33030a708f8fSGustavo F. Padovan 		err = sock_error(sk);
33040a708f8fSGustavo F. Padovan 		if (err)
33050a708f8fSGustavo F. Padovan 			break;
33060a708f8fSGustavo F. Padovan 
3307311bb895SGustavo F. Padovan 		if (l2cap_try_push_rx_skb(chan) == 0)
33080a708f8fSGustavo F. Padovan 			break;
33090a708f8fSGustavo F. Padovan 	}
33100a708f8fSGustavo F. Padovan 
33110a708f8fSGustavo F. Padovan 	set_current_state(TASK_RUNNING);
33120a708f8fSGustavo F. Padovan 	remove_wait_queue(sk_sleep(sk), &wait);
33130a708f8fSGustavo F. Padovan 
33140a708f8fSGustavo F. Padovan 	release_sock(sk);
33150a708f8fSGustavo F. Padovan }
33160a708f8fSGustavo F. Padovan 
3317525cd185SGustavo F. Padovan static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
33180a708f8fSGustavo F. Padovan {
33190a708f8fSGustavo F. Padovan 	int sctrl, err;
33200a708f8fSGustavo F. Padovan 
3321525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
33220a708f8fSGustavo F. Padovan 		bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3323f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->busy_q, skb);
3324525cd185SGustavo F. Padovan 		return l2cap_try_push_rx_skb(chan);
33250a708f8fSGustavo F. Padovan 
33260a708f8fSGustavo F. Padovan 
33270a708f8fSGustavo F. Padovan 	}
33280a708f8fSGustavo F. Padovan 
3329525cd185SGustavo F. Padovan 	err = l2cap_ertm_reassembly_sdu(chan, skb, control);
33300a708f8fSGustavo F. Padovan 	if (err >= 0) {
333142e5c802SGustavo F. Padovan 		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
33320a708f8fSGustavo F. Padovan 		return err;
33330a708f8fSGustavo F. Padovan 	}
33340a708f8fSGustavo F. Padovan 
33350a708f8fSGustavo F. Padovan 	/* Busy Condition */
3336311bb895SGustavo F. Padovan 	BT_DBG("chan %p, Enter local busy", chan);
33370a708f8fSGustavo F. Padovan 
3338525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_LOCAL_BUSY;
33390a708f8fSGustavo F. Padovan 	bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3340f1c6775bSGustavo F. Padovan 	__skb_queue_tail(&chan->busy_q, skb);
33410a708f8fSGustavo F. Padovan 
334242e5c802SGustavo F. Padovan 	sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
33430a708f8fSGustavo F. Padovan 	sctrl |= L2CAP_SUPER_RCV_NOT_READY;
3344525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, sctrl);
33450a708f8fSGustavo F. Padovan 
3346525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_RNR_SENT;
33470a708f8fSGustavo F. Padovan 
33481a09bcb9SGustavo F. Padovan 	__clear_ack_timer(chan);
33490a708f8fSGustavo F. Padovan 
3350311bb895SGustavo F. Padovan 	queue_work(_busy_wq, &chan->busy_work);
33510a708f8fSGustavo F. Padovan 
33520a708f8fSGustavo F. Padovan 	return err;
33530a708f8fSGustavo F. Padovan }
33540a708f8fSGustavo F. Padovan 
3355525cd185SGustavo F. Padovan static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
33560a708f8fSGustavo F. Padovan {
33570a708f8fSGustavo F. Padovan 	struct sk_buff *_skb;
33580a708f8fSGustavo F. Padovan 	int err = -EINVAL;
33590a708f8fSGustavo F. Padovan 
33600a708f8fSGustavo F. Padovan 	/*
33610a708f8fSGustavo F. Padovan 	 * TODO: We have to notify the userland if some data is lost with the
33620a708f8fSGustavo F. Padovan 	 * Streaming Mode.
33630a708f8fSGustavo F. Padovan 	 */
33640a708f8fSGustavo F. Padovan 
33650a708f8fSGustavo F. Padovan 	switch (control & L2CAP_CTRL_SAR) {
33660a708f8fSGustavo F. Padovan 	case L2CAP_SDU_UNSEGMENTED:
3367525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
33686f61fd47SGustavo F. Padovan 			kfree_skb(chan->sdu);
33690a708f8fSGustavo F. Padovan 			break;
33700a708f8fSGustavo F. Padovan 		}
33710a708f8fSGustavo F. Padovan 
337223070494SGustavo F. Padovan 		err = chan->ops->recv(chan->data, skb);
33730a708f8fSGustavo F. Padovan 		if (!err)
33740a708f8fSGustavo F. Padovan 			return 0;
33750a708f8fSGustavo F. Padovan 
33760a708f8fSGustavo F. Padovan 		break;
33770a708f8fSGustavo F. Padovan 
33780a708f8fSGustavo F. Padovan 	case L2CAP_SDU_START:
3379525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
33806f61fd47SGustavo F. Padovan 			kfree_skb(chan->sdu);
33810a708f8fSGustavo F. Padovan 			break;
33820a708f8fSGustavo F. Padovan 		}
33830a708f8fSGustavo F. Padovan 
33846f61fd47SGustavo F. Padovan 		chan->sdu_len = get_unaligned_le16(skb->data);
33850a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
33860a708f8fSGustavo F. Padovan 
33870c1bc5c6SGustavo F. Padovan 		if (chan->sdu_len > chan->imtu) {
33880a708f8fSGustavo F. Padovan 			err = -EMSGSIZE;
33890a708f8fSGustavo F. Padovan 			break;
33900a708f8fSGustavo F. Padovan 		}
33910a708f8fSGustavo F. Padovan 
33926f61fd47SGustavo F. Padovan 		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
33936f61fd47SGustavo F. Padovan 		if (!chan->sdu) {
33940a708f8fSGustavo F. Padovan 			err = -ENOMEM;
33950a708f8fSGustavo F. Padovan 			break;
33960a708f8fSGustavo F. Padovan 		}
33970a708f8fSGustavo F. Padovan 
33986f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
33990a708f8fSGustavo F. Padovan 
3400525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SAR_SDU;
34016f61fd47SGustavo F. Padovan 		chan->partial_sdu_len = skb->len;
34020a708f8fSGustavo F. Padovan 		err = 0;
34030a708f8fSGustavo F. Padovan 		break;
34040a708f8fSGustavo F. Padovan 
34050a708f8fSGustavo F. Padovan 	case L2CAP_SDU_CONTINUE:
3406525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
34070a708f8fSGustavo F. Padovan 			break;
34080a708f8fSGustavo F. Padovan 
34096f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
34100a708f8fSGustavo F. Padovan 
34116f61fd47SGustavo F. Padovan 		chan->partial_sdu_len += skb->len;
34126f61fd47SGustavo F. Padovan 		if (chan->partial_sdu_len > chan->sdu_len)
34136f61fd47SGustavo F. Padovan 			kfree_skb(chan->sdu);
34140a708f8fSGustavo F. Padovan 		else
34150a708f8fSGustavo F. Padovan 			err = 0;
34160a708f8fSGustavo F. Padovan 
34170a708f8fSGustavo F. Padovan 		break;
34180a708f8fSGustavo F. Padovan 
34190a708f8fSGustavo F. Padovan 	case L2CAP_SDU_END:
3420525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
34210a708f8fSGustavo F. Padovan 			break;
34220a708f8fSGustavo F. Padovan 
34236f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
34240a708f8fSGustavo F. Padovan 
3425525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
34266f61fd47SGustavo F. Padovan 		chan->partial_sdu_len += skb->len;
34270a708f8fSGustavo F. Padovan 
34280c1bc5c6SGustavo F. Padovan 		if (chan->partial_sdu_len > chan->imtu)
34290a708f8fSGustavo F. Padovan 			goto drop;
34300a708f8fSGustavo F. Padovan 
34316f61fd47SGustavo F. Padovan 		if (chan->partial_sdu_len == chan->sdu_len) {
34326f61fd47SGustavo F. Padovan 			_skb = skb_clone(chan->sdu, GFP_ATOMIC);
343323070494SGustavo F. Padovan 			err = chan->ops->recv(chan->data, _skb);
34340a708f8fSGustavo F. Padovan 			if (err < 0)
34350a708f8fSGustavo F. Padovan 				kfree_skb(_skb);
34360a708f8fSGustavo F. Padovan 		}
34370a708f8fSGustavo F. Padovan 		err = 0;
34380a708f8fSGustavo F. Padovan 
34390a708f8fSGustavo F. Padovan drop:
34406f61fd47SGustavo F. Padovan 		kfree_skb(chan->sdu);
34410a708f8fSGustavo F. Padovan 		break;
34420a708f8fSGustavo F. Padovan 	}
34430a708f8fSGustavo F. Padovan 
34440a708f8fSGustavo F. Padovan 	kfree_skb(skb);
34450a708f8fSGustavo F. Padovan 	return err;
34460a708f8fSGustavo F. Padovan }
34470a708f8fSGustavo F. Padovan 
3448525cd185SGustavo F. Padovan static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq)
34490a708f8fSGustavo F. Padovan {
34500a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
34510a708f8fSGustavo F. Padovan 	u16 control;
34520a708f8fSGustavo F. Padovan 
3453f1c6775bSGustavo F. Padovan 	while ((skb = skb_peek(&chan->srej_q))) {
34540a708f8fSGustavo F. Padovan 		if (bt_cb(skb)->tx_seq != tx_seq)
34550a708f8fSGustavo F. Padovan 			break;
34560a708f8fSGustavo F. Padovan 
3457f1c6775bSGustavo F. Padovan 		skb = skb_dequeue(&chan->srej_q);
34580a708f8fSGustavo F. Padovan 		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3459525cd185SGustavo F. Padovan 		l2cap_ertm_reassembly_sdu(chan, skb, control);
346042e5c802SGustavo F. Padovan 		chan->buffer_seq_srej =
346142e5c802SGustavo F. Padovan 			(chan->buffer_seq_srej + 1) % 64;
34620a708f8fSGustavo F. Padovan 		tx_seq = (tx_seq + 1) % 64;
34630a708f8fSGustavo F. Padovan 	}
34640a708f8fSGustavo F. Padovan }
34650a708f8fSGustavo F. Padovan 
3466525cd185SGustavo F. Padovan static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq)
34670a708f8fSGustavo F. Padovan {
34680a708f8fSGustavo F. Padovan 	struct srej_list *l, *tmp;
34690a708f8fSGustavo F. Padovan 	u16 control;
34700a708f8fSGustavo F. Padovan 
347139d5a3eeSGustavo F. Padovan 	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
34720a708f8fSGustavo F. Padovan 		if (l->tx_seq == tx_seq) {
34730a708f8fSGustavo F. Padovan 			list_del(&l->list);
34740a708f8fSGustavo F. Padovan 			kfree(l);
34750a708f8fSGustavo F. Padovan 			return;
34760a708f8fSGustavo F. Padovan 		}
34770a708f8fSGustavo F. Padovan 		control = L2CAP_SUPER_SELECT_REJECT;
34780a708f8fSGustavo F. Padovan 		control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3479525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
34800a708f8fSGustavo F. Padovan 		list_del(&l->list);
348139d5a3eeSGustavo F. Padovan 		list_add_tail(&l->list, &chan->srej_l);
34820a708f8fSGustavo F. Padovan 	}
34830a708f8fSGustavo F. Padovan }
34840a708f8fSGustavo F. Padovan 
3485525cd185SGustavo F. Padovan static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq)
34860a708f8fSGustavo F. Padovan {
34870a708f8fSGustavo F. Padovan 	struct srej_list *new;
34880a708f8fSGustavo F. Padovan 	u16 control;
34890a708f8fSGustavo F. Padovan 
349042e5c802SGustavo F. Padovan 	while (tx_seq != chan->expected_tx_seq) {
34910a708f8fSGustavo F. Padovan 		control = L2CAP_SUPER_SELECT_REJECT;
349242e5c802SGustavo F. Padovan 		control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3493525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
34940a708f8fSGustavo F. Padovan 
34950a708f8fSGustavo F. Padovan 		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
349642e5c802SGustavo F. Padovan 		new->tx_seq = chan->expected_tx_seq;
349742e5c802SGustavo F. Padovan 		chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
349839d5a3eeSGustavo F. Padovan 		list_add_tail(&new->list, &chan->srej_l);
34990a708f8fSGustavo F. Padovan 	}
350042e5c802SGustavo F. Padovan 	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
35010a708f8fSGustavo F. Padovan }
35020a708f8fSGustavo F. Padovan 
3503525cd185SGustavo F. Padovan static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
35040a708f8fSGustavo F. Padovan {
35050a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_txseq(rx_control);
35060a708f8fSGustavo F. Padovan 	u8 req_seq = __get_reqseq(rx_control);
35070a708f8fSGustavo F. Padovan 	u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT;
35080a708f8fSGustavo F. Padovan 	int tx_seq_offset, expected_tx_seq_offset;
350947d1ec61SGustavo F. Padovan 	int num_to_ack = (chan->tx_win/6) + 1;
35100a708f8fSGustavo F. Padovan 	int err = 0;
35110a708f8fSGustavo F. Padovan 
3512525cd185SGustavo F. Padovan 	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len,
3513525cd185SGustavo F. Padovan 							tx_seq, rx_control);
35140a708f8fSGustavo F. Padovan 
35150a708f8fSGustavo F. Padovan 	if (L2CAP_CTRL_FINAL & rx_control &&
3516525cd185SGustavo F. Padovan 			chan->conn_state & L2CAP_CONN_WAIT_F) {
35171a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
35186a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
35191a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
3520525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
35210a708f8fSGustavo F. Padovan 	}
35220a708f8fSGustavo F. Padovan 
352342e5c802SGustavo F. Padovan 	chan->expected_ack_seq = req_seq;
352442e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
35250a708f8fSGustavo F. Padovan 
352642e5c802SGustavo F. Padovan 	if (tx_seq == chan->expected_tx_seq)
35270a708f8fSGustavo F. Padovan 		goto expected;
35280a708f8fSGustavo F. Padovan 
352942e5c802SGustavo F. Padovan 	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
35300a708f8fSGustavo F. Padovan 	if (tx_seq_offset < 0)
35310a708f8fSGustavo F. Padovan 		tx_seq_offset += 64;
35320a708f8fSGustavo F. Padovan 
35330a708f8fSGustavo F. Padovan 	/* invalid tx_seq */
353447d1ec61SGustavo F. Padovan 	if (tx_seq_offset >= chan->tx_win) {
35358c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
35360a708f8fSGustavo F. Padovan 		goto drop;
35370a708f8fSGustavo F. Padovan 	}
35380a708f8fSGustavo F. Padovan 
3539e6949280SMat Martineau 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY)
35400a708f8fSGustavo F. Padovan 		goto drop;
35410a708f8fSGustavo F. Padovan 
3542525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
35430a708f8fSGustavo F. Padovan 		struct srej_list *first;
35440a708f8fSGustavo F. Padovan 
354539d5a3eeSGustavo F. Padovan 		first = list_first_entry(&chan->srej_l,
35460a708f8fSGustavo F. Padovan 				struct srej_list, list);
35470a708f8fSGustavo F. Padovan 		if (tx_seq == first->tx_seq) {
354842e5c802SGustavo F. Padovan 			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3549525cd185SGustavo F. Padovan 			l2cap_check_srej_gap(chan, tx_seq);
35500a708f8fSGustavo F. Padovan 
35510a708f8fSGustavo F. Padovan 			list_del(&first->list);
35520a708f8fSGustavo F. Padovan 			kfree(first);
35530a708f8fSGustavo F. Padovan 
355439d5a3eeSGustavo F. Padovan 			if (list_empty(&chan->srej_l)) {
355542e5c802SGustavo F. Padovan 				chan->buffer_seq = chan->buffer_seq_srej;
3556525cd185SGustavo F. Padovan 				chan->conn_state &= ~L2CAP_CONN_SREJ_SENT;
3557525cd185SGustavo F. Padovan 				l2cap_send_ack(chan);
355849208c9cSGustavo F. Padovan 				BT_DBG("chan %p, Exit SREJ_SENT", chan);
35590a708f8fSGustavo F. Padovan 			}
35600a708f8fSGustavo F. Padovan 		} else {
35610a708f8fSGustavo F. Padovan 			struct srej_list *l;
35620a708f8fSGustavo F. Padovan 
35630a708f8fSGustavo F. Padovan 			/* duplicated tx_seq */
356442e5c802SGustavo F. Padovan 			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
35650a708f8fSGustavo F. Padovan 				goto drop;
35660a708f8fSGustavo F. Padovan 
356739d5a3eeSGustavo F. Padovan 			list_for_each_entry(l, &chan->srej_l, list) {
35680a708f8fSGustavo F. Padovan 				if (l->tx_seq == tx_seq) {
3569525cd185SGustavo F. Padovan 					l2cap_resend_srejframe(chan, tx_seq);
35700a708f8fSGustavo F. Padovan 					return 0;
35710a708f8fSGustavo F. Padovan 				}
35720a708f8fSGustavo F. Padovan 			}
3573525cd185SGustavo F. Padovan 			l2cap_send_srejframe(chan, tx_seq);
35740a708f8fSGustavo F. Padovan 		}
35750a708f8fSGustavo F. Padovan 	} else {
35760a708f8fSGustavo F. Padovan 		expected_tx_seq_offset =
357742e5c802SGustavo F. Padovan 			(chan->expected_tx_seq - chan->buffer_seq) % 64;
35780a708f8fSGustavo F. Padovan 		if (expected_tx_seq_offset < 0)
35790a708f8fSGustavo F. Padovan 			expected_tx_seq_offset += 64;
35800a708f8fSGustavo F. Padovan 
35810a708f8fSGustavo F. Padovan 		/* duplicated tx_seq */
35820a708f8fSGustavo F. Padovan 		if (tx_seq_offset < expected_tx_seq_offset)
35830a708f8fSGustavo F. Padovan 			goto drop;
35840a708f8fSGustavo F. Padovan 
3585525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SREJ_SENT;
35860a708f8fSGustavo F. Padovan 
358749208c9cSGustavo F. Padovan 		BT_DBG("chan %p, Enter SREJ", chan);
35880a708f8fSGustavo F. Padovan 
358939d5a3eeSGustavo F. Padovan 		INIT_LIST_HEAD(&chan->srej_l);
359042e5c802SGustavo F. Padovan 		chan->buffer_seq_srej = chan->buffer_seq;
35910a708f8fSGustavo F. Padovan 
3592f1c6775bSGustavo F. Padovan 		__skb_queue_head_init(&chan->srej_q);
3593f1c6775bSGustavo F. Padovan 		__skb_queue_head_init(&chan->busy_q);
359442e5c802SGustavo F. Padovan 		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
35950a708f8fSGustavo F. Padovan 
3596525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_PBIT;
35970a708f8fSGustavo F. Padovan 
3598525cd185SGustavo F. Padovan 		l2cap_send_srejframe(chan, tx_seq);
35990a708f8fSGustavo F. Padovan 
36001a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
36010a708f8fSGustavo F. Padovan 	}
36020a708f8fSGustavo F. Padovan 	return 0;
36030a708f8fSGustavo F. Padovan 
36040a708f8fSGustavo F. Padovan expected:
360542e5c802SGustavo F. Padovan 	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
36060a708f8fSGustavo F. Padovan 
3607525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
36080a708f8fSGustavo F. Padovan 		bt_cb(skb)->tx_seq = tx_seq;
36090a708f8fSGustavo F. Padovan 		bt_cb(skb)->sar = sar;
3610f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->srej_q, skb);
36110a708f8fSGustavo F. Padovan 		return 0;
36120a708f8fSGustavo F. Padovan 	}
36130a708f8fSGustavo F. Padovan 
3614525cd185SGustavo F. Padovan 	err = l2cap_push_rx_skb(chan, skb, rx_control);
36150a708f8fSGustavo F. Padovan 	if (err < 0)
36160a708f8fSGustavo F. Padovan 		return 0;
36170a708f8fSGustavo F. Padovan 
36180a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_FINAL) {
3619525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
3620525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
36210a708f8fSGustavo F. Padovan 		else
3622525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
36230a708f8fSGustavo F. Padovan 	}
36240a708f8fSGustavo F. Padovan 
36251a09bcb9SGustavo F. Padovan 	__set_ack_timer(chan);
36260a708f8fSGustavo F. Padovan 
36276a026610SGustavo F. Padovan 	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
36286a026610SGustavo F. Padovan 	if (chan->num_acked == num_to_ack - 1)
3629525cd185SGustavo F. Padovan 		l2cap_send_ack(chan);
36300a708f8fSGustavo F. Padovan 
36310a708f8fSGustavo F. Padovan 	return 0;
36320a708f8fSGustavo F. Padovan 
36330a708f8fSGustavo F. Padovan drop:
36340a708f8fSGustavo F. Padovan 	kfree_skb(skb);
36350a708f8fSGustavo F. Padovan 	return 0;
36360a708f8fSGustavo F. Padovan }
36370a708f8fSGustavo F. Padovan 
3638525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control)
36390a708f8fSGustavo F. Padovan {
364049208c9cSGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control),
36410a708f8fSGustavo F. Padovan 						rx_control);
36420a708f8fSGustavo F. Padovan 
364342e5c802SGustavo F. Padovan 	chan->expected_ack_seq = __get_reqseq(rx_control);
364442e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
36450a708f8fSGustavo F. Padovan 
36460a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL) {
3647525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
3648525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
3649525cd185SGustavo F. Padovan 			if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
36506a026610SGustavo F. Padovan 					(chan->unacked_frames > 0))
36511a09bcb9SGustavo F. Padovan 				__set_retrans_timer(chan);
36520a708f8fSGustavo F. Padovan 
3653525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3654525cd185SGustavo F. Padovan 			l2cap_send_srejtail(chan);
36550a708f8fSGustavo F. Padovan 		} else {
3656525cd185SGustavo F. Padovan 			l2cap_send_i_or_rr_or_rnr(chan);
36570a708f8fSGustavo F. Padovan 		}
36580a708f8fSGustavo F. Padovan 
36590a708f8fSGustavo F. Padovan 	} else if (rx_control & L2CAP_CTRL_FINAL) {
3660525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
36610a708f8fSGustavo F. Padovan 
3662525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
3663525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
36640a708f8fSGustavo F. Padovan 		else
3665525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
36660a708f8fSGustavo F. Padovan 
36670a708f8fSGustavo F. Padovan 	} else {
3668525cd185SGustavo F. Padovan 		if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
36696a026610SGustavo F. Padovan 				(chan->unacked_frames > 0))
36701a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
36710a708f8fSGustavo F. Padovan 
3672525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3673525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SREJ_SENT)
3674525cd185SGustavo F. Padovan 			l2cap_send_ack(chan);
36750a708f8fSGustavo F. Padovan 		else
3676525cd185SGustavo F. Padovan 			l2cap_ertm_send(chan);
36770a708f8fSGustavo F. Padovan 	}
36780a708f8fSGustavo F. Padovan }
36790a708f8fSGustavo F. Padovan 
3680525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control)
36810a708f8fSGustavo F. Padovan {
36820a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_reqseq(rx_control);
36830a708f8fSGustavo F. Padovan 
3684525cd185SGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
36850a708f8fSGustavo F. Padovan 
3686525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
36870a708f8fSGustavo F. Padovan 
368842e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
368942e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
36900a708f8fSGustavo F. Padovan 
36910a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_FINAL) {
3692525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
3693525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
36940a708f8fSGustavo F. Padovan 		else
3695525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
36960a708f8fSGustavo F. Padovan 	} else {
3697525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
36980a708f8fSGustavo F. Padovan 
3699525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_WAIT_F)
3700525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_REJ_ACT;
37010a708f8fSGustavo F. Padovan 	}
37020a708f8fSGustavo F. Padovan }
3703525cd185SGustavo F. Padovan static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control)
37040a708f8fSGustavo F. Padovan {
37050a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_reqseq(rx_control);
37060a708f8fSGustavo F. Padovan 
3707525cd185SGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
37080a708f8fSGustavo F. Padovan 
3709525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
37100a708f8fSGustavo F. Padovan 
37110a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL) {
371242e5c802SGustavo F. Padovan 		chan->expected_ack_seq = tx_seq;
371342e5c802SGustavo F. Padovan 		l2cap_drop_acked_frames(chan);
37140a708f8fSGustavo F. Padovan 
3715525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
3716525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
37170a708f8fSGustavo F. Padovan 
3718525cd185SGustavo F. Padovan 		l2cap_ertm_send(chan);
37190a708f8fSGustavo F. Padovan 
3720525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
37216a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
3722525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
37230a708f8fSGustavo F. Padovan 		}
37240a708f8fSGustavo F. Padovan 	} else if (rx_control & L2CAP_CTRL_FINAL) {
3725525cd185SGustavo F. Padovan 		if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) &&
37266a026610SGustavo F. Padovan 				chan->srej_save_reqseq == tx_seq)
3727525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_SREJ_ACT;
37280a708f8fSGustavo F. Padovan 		else
3729525cd185SGustavo F. Padovan 			l2cap_retransmit_one_frame(chan, tx_seq);
37300a708f8fSGustavo F. Padovan 	} else {
3731525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
3732525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
37336a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
3734525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
37350a708f8fSGustavo F. Padovan 		}
37360a708f8fSGustavo F. Padovan 	}
37370a708f8fSGustavo F. Padovan }
37380a708f8fSGustavo F. Padovan 
3739525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control)
37400a708f8fSGustavo F. Padovan {
37410a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_reqseq(rx_control);
37420a708f8fSGustavo F. Padovan 
3743525cd185SGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
37440a708f8fSGustavo F. Padovan 
3745525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_REMOTE_BUSY;
374642e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
374742e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
37480a708f8fSGustavo F. Padovan 
37490a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL)
3750525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
37510a708f8fSGustavo F. Padovan 
3752525cd185SGustavo F. Padovan 	if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) {
37531a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
37540a708f8fSGustavo F. Padovan 		if (rx_control & L2CAP_CTRL_POLL)
3755525cd185SGustavo F. Padovan 			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
37560a708f8fSGustavo F. Padovan 		return;
37570a708f8fSGustavo F. Padovan 	}
37580a708f8fSGustavo F. Padovan 
37590a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL)
3760525cd185SGustavo F. Padovan 		l2cap_send_srejtail(chan);
37610a708f8fSGustavo F. Padovan 	else
3762525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY);
37630a708f8fSGustavo F. Padovan }
37640a708f8fSGustavo F. Padovan 
3765525cd185SGustavo F. Padovan static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
37660a708f8fSGustavo F. Padovan {
3767525cd185SGustavo F. Padovan 	BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len);
37680a708f8fSGustavo F. Padovan 
37690a708f8fSGustavo F. Padovan 	if (L2CAP_CTRL_FINAL & rx_control &&
3770525cd185SGustavo F. Padovan 			chan->conn_state & L2CAP_CONN_WAIT_F) {
37711a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
37726a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
37731a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
3774525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
37750a708f8fSGustavo F. Padovan 	}
37760a708f8fSGustavo F. Padovan 
37770a708f8fSGustavo F. Padovan 	switch (rx_control & L2CAP_CTRL_SUPERVISE) {
37780a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_RCV_READY:
3779525cd185SGustavo F. Padovan 		l2cap_data_channel_rrframe(chan, rx_control);
37800a708f8fSGustavo F. Padovan 		break;
37810a708f8fSGustavo F. Padovan 
37820a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_REJECT:
3783525cd185SGustavo F. Padovan 		l2cap_data_channel_rejframe(chan, rx_control);
37840a708f8fSGustavo F. Padovan 		break;
37850a708f8fSGustavo F. Padovan 
37860a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_SELECT_REJECT:
3787525cd185SGustavo F. Padovan 		l2cap_data_channel_srejframe(chan, rx_control);
37880a708f8fSGustavo F. Padovan 		break;
37890a708f8fSGustavo F. Padovan 
37900a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_RCV_NOT_READY:
3791525cd185SGustavo F. Padovan 		l2cap_data_channel_rnrframe(chan, rx_control);
37920a708f8fSGustavo F. Padovan 		break;
37930a708f8fSGustavo F. Padovan 	}
37940a708f8fSGustavo F. Padovan 
37950a708f8fSGustavo F. Padovan 	kfree_skb(skb);
37960a708f8fSGustavo F. Padovan 	return 0;
37970a708f8fSGustavo F. Padovan }
37980a708f8fSGustavo F. Padovan 
37990a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
38000a708f8fSGustavo F. Padovan {
3801525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
38020a708f8fSGustavo F. Padovan 	u16 control;
38030a708f8fSGustavo F. Padovan 	u8 req_seq;
38040a708f8fSGustavo F. Padovan 	int len, next_tx_seq_offset, req_seq_offset;
38050a708f8fSGustavo F. Padovan 
38060a708f8fSGustavo F. Padovan 	control = get_unaligned_le16(skb->data);
38070a708f8fSGustavo F. Padovan 	skb_pull(skb, 2);
38080a708f8fSGustavo F. Padovan 	len = skb->len;
38090a708f8fSGustavo F. Padovan 
38100a708f8fSGustavo F. Padovan 	/*
38110a708f8fSGustavo F. Padovan 	 * We can just drop the corrupted I-frame here.
38120a708f8fSGustavo F. Padovan 	 * Receiver will miss it and start proper recovery
38130a708f8fSGustavo F. Padovan 	 * procedures and ask retransmission.
38140a708f8fSGustavo F. Padovan 	 */
381547d1ec61SGustavo F. Padovan 	if (l2cap_check_fcs(chan, skb))
38160a708f8fSGustavo F. Padovan 		goto drop;
38170a708f8fSGustavo F. Padovan 
38180a708f8fSGustavo F. Padovan 	if (__is_sar_start(control) && __is_iframe(control))
38190a708f8fSGustavo F. Padovan 		len -= 2;
38200a708f8fSGustavo F. Padovan 
382147d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
38220a708f8fSGustavo F. Padovan 		len -= 2;
38230a708f8fSGustavo F. Padovan 
382447d1ec61SGustavo F. Padovan 	if (len > chan->mps) {
38258c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38260a708f8fSGustavo F. Padovan 		goto drop;
38270a708f8fSGustavo F. Padovan 	}
38280a708f8fSGustavo F. Padovan 
38290a708f8fSGustavo F. Padovan 	req_seq = __get_reqseq(control);
383042e5c802SGustavo F. Padovan 	req_seq_offset = (req_seq - chan->expected_ack_seq) % 64;
38310a708f8fSGustavo F. Padovan 	if (req_seq_offset < 0)
38320a708f8fSGustavo F. Padovan 		req_seq_offset += 64;
38330a708f8fSGustavo F. Padovan 
38340a708f8fSGustavo F. Padovan 	next_tx_seq_offset =
383542e5c802SGustavo F. Padovan 		(chan->next_tx_seq - chan->expected_ack_seq) % 64;
38360a708f8fSGustavo F. Padovan 	if (next_tx_seq_offset < 0)
38370a708f8fSGustavo F. Padovan 		next_tx_seq_offset += 64;
38380a708f8fSGustavo F. Padovan 
38390a708f8fSGustavo F. Padovan 	/* check for invalid req-seq */
38400a708f8fSGustavo F. Padovan 	if (req_seq_offset > next_tx_seq_offset) {
38418c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38420a708f8fSGustavo F. Padovan 		goto drop;
38430a708f8fSGustavo F. Padovan 	}
38440a708f8fSGustavo F. Padovan 
38450a708f8fSGustavo F. Padovan 	if (__is_iframe(control)) {
38460a708f8fSGustavo F. Padovan 		if (len < 0) {
38478c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38480a708f8fSGustavo F. Padovan 			goto drop;
38490a708f8fSGustavo F. Padovan 		}
38500a708f8fSGustavo F. Padovan 
3851525cd185SGustavo F. Padovan 		l2cap_data_channel_iframe(chan, control, skb);
38520a708f8fSGustavo F. Padovan 	} else {
38530a708f8fSGustavo F. Padovan 		if (len != 0) {
38540a708f8fSGustavo F. Padovan 			BT_ERR("%d", len);
38558c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38560a708f8fSGustavo F. Padovan 			goto drop;
38570a708f8fSGustavo F. Padovan 		}
38580a708f8fSGustavo F. Padovan 
3859525cd185SGustavo F. Padovan 		l2cap_data_channel_sframe(chan, control, skb);
38600a708f8fSGustavo F. Padovan 	}
38610a708f8fSGustavo F. Padovan 
38620a708f8fSGustavo F. Padovan 	return 0;
38630a708f8fSGustavo F. Padovan 
38640a708f8fSGustavo F. Padovan drop:
38650a708f8fSGustavo F. Padovan 	kfree_skb(skb);
38660a708f8fSGustavo F. Padovan 	return 0;
38670a708f8fSGustavo F. Padovan }
38680a708f8fSGustavo F. Padovan 
38690a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
38700a708f8fSGustavo F. Padovan {
387148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
3872bf734843SDavid S. Miller 	struct sock *sk = NULL;
38730a708f8fSGustavo F. Padovan 	u16 control;
38740a708f8fSGustavo F. Padovan 	u8 tx_seq;
38750a708f8fSGustavo F. Padovan 	int len;
38760a708f8fSGustavo F. Padovan 
3877baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, cid);
387848454079SGustavo F. Padovan 	if (!chan) {
38790a708f8fSGustavo F. Padovan 		BT_DBG("unknown cid 0x%4.4x", cid);
38800a708f8fSGustavo F. Padovan 		goto drop;
38810a708f8fSGustavo F. Padovan 	}
38820a708f8fSGustavo F. Padovan 
388348454079SGustavo F. Padovan 	sk = chan->sk;
38840a708f8fSGustavo F. Padovan 
388549208c9cSGustavo F. Padovan 	BT_DBG("chan %p, len %d", chan, skb->len);
38860a708f8fSGustavo F. Padovan 
388789bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
38880a708f8fSGustavo F. Padovan 		goto drop;
38890a708f8fSGustavo F. Padovan 
38900c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
38910a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
38920a708f8fSGustavo F. Padovan 		/* If socket recv buffers overflows we drop data here
38930a708f8fSGustavo F. Padovan 		 * which is *bad* because L2CAP has to be reliable.
38940a708f8fSGustavo F. Padovan 		 * But we don't have any other choice. L2CAP doesn't
38950a708f8fSGustavo F. Padovan 		 * provide flow control mechanism. */
38960a708f8fSGustavo F. Padovan 
38970c1bc5c6SGustavo F. Padovan 		if (chan->imtu < skb->len)
38980a708f8fSGustavo F. Padovan 			goto drop;
38990a708f8fSGustavo F. Padovan 
390023070494SGustavo F. Padovan 		if (!chan->ops->recv(chan->data, skb))
39010a708f8fSGustavo F. Padovan 			goto done;
39020a708f8fSGustavo F. Padovan 		break;
39030a708f8fSGustavo F. Padovan 
39040a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
39050a708f8fSGustavo F. Padovan 		if (!sock_owned_by_user(sk)) {
39060a708f8fSGustavo F. Padovan 			l2cap_ertm_data_rcv(sk, skb);
39070a708f8fSGustavo F. Padovan 		} else {
39080a708f8fSGustavo F. Padovan 			if (sk_add_backlog(sk, skb))
39090a708f8fSGustavo F. Padovan 				goto drop;
39100a708f8fSGustavo F. Padovan 		}
39110a708f8fSGustavo F. Padovan 
39120a708f8fSGustavo F. Padovan 		goto done;
39130a708f8fSGustavo F. Padovan 
39140a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
39150a708f8fSGustavo F. Padovan 		control = get_unaligned_le16(skb->data);
39160a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
39170a708f8fSGustavo F. Padovan 		len = skb->len;
39180a708f8fSGustavo F. Padovan 
391947d1ec61SGustavo F. Padovan 		if (l2cap_check_fcs(chan, skb))
39200a708f8fSGustavo F. Padovan 			goto drop;
39210a708f8fSGustavo F. Padovan 
39220a708f8fSGustavo F. Padovan 		if (__is_sar_start(control))
39230a708f8fSGustavo F. Padovan 			len -= 2;
39240a708f8fSGustavo F. Padovan 
392547d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16)
39260a708f8fSGustavo F. Padovan 			len -= 2;
39270a708f8fSGustavo F. Padovan 
392847d1ec61SGustavo F. Padovan 		if (len > chan->mps || len < 0 || __is_sframe(control))
39290a708f8fSGustavo F. Padovan 			goto drop;
39300a708f8fSGustavo F. Padovan 
39310a708f8fSGustavo F. Padovan 		tx_seq = __get_txseq(control);
39320a708f8fSGustavo F. Padovan 
393342e5c802SGustavo F. Padovan 		if (chan->expected_tx_seq == tx_seq)
393442e5c802SGustavo F. Padovan 			chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
39350a708f8fSGustavo F. Padovan 		else
393642e5c802SGustavo F. Padovan 			chan->expected_tx_seq = (tx_seq + 1) % 64;
39370a708f8fSGustavo F. Padovan 
3938525cd185SGustavo F. Padovan 		l2cap_streaming_reassembly_sdu(chan, skb, control);
39390a708f8fSGustavo F. Padovan 
39400a708f8fSGustavo F. Padovan 		goto done;
39410a708f8fSGustavo F. Padovan 
39420a708f8fSGustavo F. Padovan 	default:
39430c1bc5c6SGustavo F. Padovan 		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
39440a708f8fSGustavo F. Padovan 		break;
39450a708f8fSGustavo F. Padovan 	}
39460a708f8fSGustavo F. Padovan 
39470a708f8fSGustavo F. Padovan drop:
39480a708f8fSGustavo F. Padovan 	kfree_skb(skb);
39490a708f8fSGustavo F. Padovan 
39500a708f8fSGustavo F. Padovan done:
39510a708f8fSGustavo F. Padovan 	if (sk)
39520a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
39530a708f8fSGustavo F. Padovan 
39540a708f8fSGustavo F. Padovan 	return 0;
39550a708f8fSGustavo F. Padovan }
39560a708f8fSGustavo F. Padovan 
39570a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
39580a708f8fSGustavo F. Padovan {
39596dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
396023691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
39610a708f8fSGustavo F. Padovan 
396223691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_psm(0, psm, conn->src);
396323691d75SGustavo F. Padovan 	if (!chan)
39640a708f8fSGustavo F. Padovan 		goto drop;
39650a708f8fSGustavo F. Padovan 
396623691d75SGustavo F. Padovan 	sk = chan->sk;
396723691d75SGustavo F. Padovan 
39680a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
39690a708f8fSGustavo F. Padovan 
39700a708f8fSGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
39710a708f8fSGustavo F. Padovan 
397289bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
39730a708f8fSGustavo F. Padovan 		goto drop;
39740a708f8fSGustavo F. Padovan 
39750c1bc5c6SGustavo F. Padovan 	if (l2cap_pi(sk)->chan->imtu < skb->len)
39760a708f8fSGustavo F. Padovan 		goto drop;
39770a708f8fSGustavo F. Padovan 
397823070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
39790a708f8fSGustavo F. Padovan 		goto done;
39800a708f8fSGustavo F. Padovan 
39810a708f8fSGustavo F. Padovan drop:
39820a708f8fSGustavo F. Padovan 	kfree_skb(skb);
39830a708f8fSGustavo F. Padovan 
39840a708f8fSGustavo F. Padovan done:
39850a708f8fSGustavo F. Padovan 	if (sk)
39860a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
39870a708f8fSGustavo F. Padovan 	return 0;
39880a708f8fSGustavo F. Padovan }
39890a708f8fSGustavo F. Padovan 
39909f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb)
39919f69bda6SGustavo F. Padovan {
39926dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
399323691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
39949f69bda6SGustavo F. Padovan 
399523691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_scid(0, cid, conn->src);
399623691d75SGustavo F. Padovan 	if (!chan)
39979f69bda6SGustavo F. Padovan 		goto drop;
39989f69bda6SGustavo F. Padovan 
399923691d75SGustavo F. Padovan 	sk = chan->sk;
400023691d75SGustavo F. Padovan 
40019f69bda6SGustavo F. Padovan 	bh_lock_sock(sk);
40029f69bda6SGustavo F. Padovan 
40039f69bda6SGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
40049f69bda6SGustavo F. Padovan 
400589bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
40069f69bda6SGustavo F. Padovan 		goto drop;
40079f69bda6SGustavo F. Padovan 
40080c1bc5c6SGustavo F. Padovan 	if (l2cap_pi(sk)->chan->imtu < skb->len)
40099f69bda6SGustavo F. Padovan 		goto drop;
40109f69bda6SGustavo F. Padovan 
401123070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
40129f69bda6SGustavo F. Padovan 		goto done;
40139f69bda6SGustavo F. Padovan 
40149f69bda6SGustavo F. Padovan drop:
40159f69bda6SGustavo F. Padovan 	kfree_skb(skb);
40169f69bda6SGustavo F. Padovan 
40179f69bda6SGustavo F. Padovan done:
40189f69bda6SGustavo F. Padovan 	if (sk)
40199f69bda6SGustavo F. Padovan 		bh_unlock_sock(sk);
40209f69bda6SGustavo F. Padovan 	return 0;
40219f69bda6SGustavo F. Padovan }
40229f69bda6SGustavo F. Padovan 
40230a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
40240a708f8fSGustavo F. Padovan {
40250a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh = (void *) skb->data;
40260a708f8fSGustavo F. Padovan 	u16 cid, len;
40270a708f8fSGustavo F. Padovan 	__le16 psm;
40280a708f8fSGustavo F. Padovan 
40290a708f8fSGustavo F. Padovan 	skb_pull(skb, L2CAP_HDR_SIZE);
40300a708f8fSGustavo F. Padovan 	cid = __le16_to_cpu(lh->cid);
40310a708f8fSGustavo F. Padovan 	len = __le16_to_cpu(lh->len);
40320a708f8fSGustavo F. Padovan 
40330a708f8fSGustavo F. Padovan 	if (len != skb->len) {
40340a708f8fSGustavo F. Padovan 		kfree_skb(skb);
40350a708f8fSGustavo F. Padovan 		return;
40360a708f8fSGustavo F. Padovan 	}
40370a708f8fSGustavo F. Padovan 
40380a708f8fSGustavo F. Padovan 	BT_DBG("len %d, cid 0x%4.4x", len, cid);
40390a708f8fSGustavo F. Padovan 
40400a708f8fSGustavo F. Padovan 	switch (cid) {
40413300d9a9SClaudio Takahasi 	case L2CAP_CID_LE_SIGNALING:
40420a708f8fSGustavo F. Padovan 	case L2CAP_CID_SIGNALING:
40430a708f8fSGustavo F. Padovan 		l2cap_sig_channel(conn, skb);
40440a708f8fSGustavo F. Padovan 		break;
40450a708f8fSGustavo F. Padovan 
40460a708f8fSGustavo F. Padovan 	case L2CAP_CID_CONN_LESS:
40470a708f8fSGustavo F. Padovan 		psm = get_unaligned_le16(skb->data);
40480a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
40490a708f8fSGustavo F. Padovan 		l2cap_conless_channel(conn, psm, skb);
40500a708f8fSGustavo F. Padovan 		break;
40510a708f8fSGustavo F. Padovan 
40529f69bda6SGustavo F. Padovan 	case L2CAP_CID_LE_DATA:
40539f69bda6SGustavo F. Padovan 		l2cap_att_channel(conn, cid, skb);
40549f69bda6SGustavo F. Padovan 		break;
40559f69bda6SGustavo F. Padovan 
4056b501d6a1SAnderson Briglia 	case L2CAP_CID_SMP:
4057b501d6a1SAnderson Briglia 		if (smp_sig_channel(conn, skb))
4058b501d6a1SAnderson Briglia 			l2cap_conn_del(conn->hcon, EACCES);
4059b501d6a1SAnderson Briglia 		break;
4060b501d6a1SAnderson Briglia 
40610a708f8fSGustavo F. Padovan 	default:
40620a708f8fSGustavo F. Padovan 		l2cap_data_channel(conn, cid, skb);
40630a708f8fSGustavo F. Padovan 		break;
40640a708f8fSGustavo F. Padovan 	}
40650a708f8fSGustavo F. Padovan }
40660a708f8fSGustavo F. Padovan 
40670a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */
40680a708f8fSGustavo F. Padovan 
40690a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
40700a708f8fSGustavo F. Padovan {
40710a708f8fSGustavo F. Padovan 	int exact = 0, lm1 = 0, lm2 = 0;
407223691d75SGustavo F. Padovan 	struct l2cap_chan *c;
40730a708f8fSGustavo F. Padovan 
40740a708f8fSGustavo F. Padovan 	if (type != ACL_LINK)
40750a708f8fSGustavo F. Padovan 		return -EINVAL;
40760a708f8fSGustavo F. Padovan 
40770a708f8fSGustavo F. Padovan 	BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
40780a708f8fSGustavo F. Padovan 
40790a708f8fSGustavo F. Padovan 	/* Find listening sockets and check their link_mode */
408023691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
408123691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
408223691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
40834343478fSGustavo F. Padovan 
408489bc500eSGustavo F. Padovan 		if (c->state != BT_LISTEN)
40850a708f8fSGustavo F. Padovan 			continue;
40860a708f8fSGustavo F. Padovan 
40870a708f8fSGustavo F. Padovan 		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
40880a708f8fSGustavo F. Padovan 			lm1 |= HCI_LM_ACCEPT;
408923691d75SGustavo F. Padovan 			if (c->role_switch)
40900a708f8fSGustavo F. Padovan 				lm1 |= HCI_LM_MASTER;
40910a708f8fSGustavo F. Padovan 			exact++;
40920a708f8fSGustavo F. Padovan 		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
40930a708f8fSGustavo F. Padovan 			lm2 |= HCI_LM_ACCEPT;
409423691d75SGustavo F. Padovan 			if (c->role_switch)
40950a708f8fSGustavo F. Padovan 				lm2 |= HCI_LM_MASTER;
40960a708f8fSGustavo F. Padovan 		}
40970a708f8fSGustavo F. Padovan 	}
409823691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
40990a708f8fSGustavo F. Padovan 
41000a708f8fSGustavo F. Padovan 	return exact ? lm1 : lm2;
41010a708f8fSGustavo F. Padovan }
41020a708f8fSGustavo F. Padovan 
41030a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
41040a708f8fSGustavo F. Padovan {
41050a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
41060a708f8fSGustavo F. Padovan 
41070a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
41080a708f8fSGustavo F. Padovan 
4109acd7d370SVille Tervo 	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
41100a708f8fSGustavo F. Padovan 		return -EINVAL;
41110a708f8fSGustavo F. Padovan 
41120a708f8fSGustavo F. Padovan 	if (!status) {
41130a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, status);
41140a708f8fSGustavo F. Padovan 		if (conn)
41150a708f8fSGustavo F. Padovan 			l2cap_conn_ready(conn);
41160a708f8fSGustavo F. Padovan 	} else
41170a708f8fSGustavo F. Padovan 		l2cap_conn_del(hcon, bt_err(status));
41180a708f8fSGustavo F. Padovan 
41190a708f8fSGustavo F. Padovan 	return 0;
41200a708f8fSGustavo F. Padovan }
41210a708f8fSGustavo F. Padovan 
41220a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon)
41230a708f8fSGustavo F. Padovan {
41240a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
41250a708f8fSGustavo F. Padovan 
41260a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p", hcon);
41270a708f8fSGustavo F. Padovan 
4128b5694506SGustavo F. Padovan 	if ((hcon->type != ACL_LINK && hcon->type != LE_LINK) || !conn)
41290a708f8fSGustavo F. Padovan 		return 0x13;
41300a708f8fSGustavo F. Padovan 
41310a708f8fSGustavo F. Padovan 	return conn->disc_reason;
41320a708f8fSGustavo F. Padovan }
41330a708f8fSGustavo F. Padovan 
41340a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
41350a708f8fSGustavo F. Padovan {
41360a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p reason %d", hcon, reason);
41370a708f8fSGustavo F. Padovan 
4138acd7d370SVille Tervo 	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
41390a708f8fSGustavo F. Padovan 		return -EINVAL;
41400a708f8fSGustavo F. Padovan 
41410a708f8fSGustavo F. Padovan 	l2cap_conn_del(hcon, bt_err(reason));
41420a708f8fSGustavo F. Padovan 
41430a708f8fSGustavo F. Padovan 	return 0;
41440a708f8fSGustavo F. Padovan }
41450a708f8fSGustavo F. Padovan 
41464343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
41470a708f8fSGustavo F. Padovan {
4148715ec005SGustavo F. Padovan 	if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
41490a708f8fSGustavo F. Padovan 		return;
41500a708f8fSGustavo F. Padovan 
41510a708f8fSGustavo F. Padovan 	if (encrypt == 0x00) {
41524343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM) {
4153c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
4154c9b66675SGustavo F. Padovan 			__set_chan_timer(chan, HZ * 5);
41554343478fSGustavo F. Padovan 		} else if (chan->sec_level == BT_SECURITY_HIGH)
41560f852724SGustavo F. Padovan 			l2cap_chan_close(chan, ECONNREFUSED);
41570a708f8fSGustavo F. Padovan 	} else {
41584343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM)
4159c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
41600a708f8fSGustavo F. Padovan 	}
41610a708f8fSGustavo F. Padovan }
41620a708f8fSGustavo F. Padovan 
41630a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
41640a708f8fSGustavo F. Padovan {
41650a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
416648454079SGustavo F. Padovan 	struct l2cap_chan *chan;
41670a708f8fSGustavo F. Padovan 
41680a708f8fSGustavo F. Padovan 	if (!conn)
41690a708f8fSGustavo F. Padovan 		return 0;
41700a708f8fSGustavo F. Padovan 
41710a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
41720a708f8fSGustavo F. Padovan 
4173baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
41740a708f8fSGustavo F. Padovan 
4175baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
417648454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
4177baa7e1faSGustavo F. Padovan 
41780a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
41790a708f8fSGustavo F. Padovan 
4180f1cb9af5SVinicius Costa Gomes 		BT_DBG("chan->scid %d", chan->scid);
4181f1cb9af5SVinicius Costa Gomes 
4182f1cb9af5SVinicius Costa Gomes 		if (chan->scid == L2CAP_CID_LE_DATA) {
4183f1cb9af5SVinicius Costa Gomes 			if (!status && encrypt) {
4184f1cb9af5SVinicius Costa Gomes 				chan->sec_level = hcon->sec_level;
4185f1cb9af5SVinicius Costa Gomes 				l2cap_chan_ready(sk);
4186f1cb9af5SVinicius Costa Gomes 			}
4187f1cb9af5SVinicius Costa Gomes 
4188f1cb9af5SVinicius Costa Gomes 			bh_unlock_sock(sk);
4189f1cb9af5SVinicius Costa Gomes 			continue;
4190f1cb9af5SVinicius Costa Gomes 		}
4191f1cb9af5SVinicius Costa Gomes 
4192b4450035SGustavo F. Padovan 		if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) {
41930a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
41940a708f8fSGustavo F. Padovan 			continue;
41950a708f8fSGustavo F. Padovan 		}
41960a708f8fSGustavo F. Padovan 
419789bc500eSGustavo F. Padovan 		if (!status && (chan->state == BT_CONNECTED ||
419889bc500eSGustavo F. Padovan 						chan->state == BT_CONFIG)) {
41994343478fSGustavo F. Padovan 			l2cap_check_encryption(chan, encrypt);
42000a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
42010a708f8fSGustavo F. Padovan 			continue;
42020a708f8fSGustavo F. Padovan 		}
42030a708f8fSGustavo F. Padovan 
420489bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
42050a708f8fSGustavo F. Padovan 			if (!status) {
42060a708f8fSGustavo F. Padovan 				struct l2cap_conn_req req;
4207fe4128e0SGustavo F. Padovan 				req.scid = cpu_to_le16(chan->scid);
4208fe4128e0SGustavo F. Padovan 				req.psm  = chan->psm;
42090a708f8fSGustavo F. Padovan 
4210fc7f8a7eSGustavo F. Padovan 				chan->ident = l2cap_get_ident(conn);
4211b4450035SGustavo F. Padovan 				chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
42120a708f8fSGustavo F. Padovan 
4213fc7f8a7eSGustavo F. Padovan 				l2cap_send_cmd(conn, chan->ident,
42140a708f8fSGustavo F. Padovan 					L2CAP_CONN_REQ, sizeof(req), &req);
42150a708f8fSGustavo F. Padovan 			} else {
4216c9b66675SGustavo F. Padovan 				__clear_chan_timer(chan);
4217c9b66675SGustavo F. Padovan 				__set_chan_timer(chan, HZ / 10);
42180a708f8fSGustavo F. Padovan 			}
421989bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
42200a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
42210a708f8fSGustavo F. Padovan 			__u16 result;
42220a708f8fSGustavo F. Padovan 
42230a708f8fSGustavo F. Padovan 			if (!status) {
422489bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONFIG);
42250a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SUCCESS;
42260a708f8fSGustavo F. Padovan 			} else {
422789bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_DISCONN);
4228c9b66675SGustavo F. Padovan 				__set_chan_timer(chan, HZ / 10);
42290a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SEC_BLOCK;
42300a708f8fSGustavo F. Padovan 			}
42310a708f8fSGustavo F. Padovan 
4232fe4128e0SGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4233fe4128e0SGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
42340a708f8fSGustavo F. Padovan 			rsp.result = cpu_to_le16(result);
42350a708f8fSGustavo F. Padovan 			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4236fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4237fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
42380a708f8fSGustavo F. Padovan 		}
42390a708f8fSGustavo F. Padovan 
42400a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
42410a708f8fSGustavo F. Padovan 	}
42420a708f8fSGustavo F. Padovan 
4243baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
42440a708f8fSGustavo F. Padovan 
42450a708f8fSGustavo F. Padovan 	return 0;
42460a708f8fSGustavo F. Padovan }
42470a708f8fSGustavo F. Padovan 
42480a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
42490a708f8fSGustavo F. Padovan {
42500a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
42510a708f8fSGustavo F. Padovan 
42520a708f8fSGustavo F. Padovan 	if (!conn)
42530a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, 0);
42540a708f8fSGustavo F. Padovan 
42550a708f8fSGustavo F. Padovan 	if (!conn)
42560a708f8fSGustavo F. Padovan 		goto drop;
42570a708f8fSGustavo F. Padovan 
42580a708f8fSGustavo F. Padovan 	BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
42590a708f8fSGustavo F. Padovan 
42600a708f8fSGustavo F. Padovan 	if (!(flags & ACL_CONT)) {
42610a708f8fSGustavo F. Padovan 		struct l2cap_hdr *hdr;
426248454079SGustavo F. Padovan 		struct l2cap_chan *chan;
42630a708f8fSGustavo F. Padovan 		u16 cid;
42640a708f8fSGustavo F. Padovan 		int len;
42650a708f8fSGustavo F. Padovan 
42660a708f8fSGustavo F. Padovan 		if (conn->rx_len) {
42670a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected start frame (len %d)", skb->len);
42680a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
42690a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
42700a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
42710a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
42720a708f8fSGustavo F. Padovan 		}
42730a708f8fSGustavo F. Padovan 
42740a708f8fSGustavo F. Padovan 		/* Start fragment always begin with Basic L2CAP header */
42750a708f8fSGustavo F. Padovan 		if (skb->len < L2CAP_HDR_SIZE) {
42760a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too short (len %d)", skb->len);
42770a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
42780a708f8fSGustavo F. Padovan 			goto drop;
42790a708f8fSGustavo F. Padovan 		}
42800a708f8fSGustavo F. Padovan 
42810a708f8fSGustavo F. Padovan 		hdr = (struct l2cap_hdr *) skb->data;
42820a708f8fSGustavo F. Padovan 		len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
42830a708f8fSGustavo F. Padovan 		cid = __le16_to_cpu(hdr->cid);
42840a708f8fSGustavo F. Padovan 
42850a708f8fSGustavo F. Padovan 		if (len == skb->len) {
42860a708f8fSGustavo F. Padovan 			/* Complete frame received */
42870a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, skb);
42880a708f8fSGustavo F. Padovan 			return 0;
42890a708f8fSGustavo F. Padovan 		}
42900a708f8fSGustavo F. Padovan 
42910a708f8fSGustavo F. Padovan 		BT_DBG("Start: total len %d, frag len %d", len, skb->len);
42920a708f8fSGustavo F. Padovan 
42930a708f8fSGustavo F. Padovan 		if (skb->len > len) {
42940a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too long (len %d, expected len %d)",
42950a708f8fSGustavo F. Padovan 				skb->len, len);
42960a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
42970a708f8fSGustavo F. Padovan 			goto drop;
42980a708f8fSGustavo F. Padovan 		}
42990a708f8fSGustavo F. Padovan 
4300baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, cid);
43010a708f8fSGustavo F. Padovan 
430248454079SGustavo F. Padovan 		if (chan && chan->sk) {
430348454079SGustavo F. Padovan 			struct sock *sk = chan->sk;
430448454079SGustavo F. Padovan 
43050c1bc5c6SGustavo F. Padovan 			if (chan->imtu < len - L2CAP_HDR_SIZE) {
430648454079SGustavo F. Padovan 				BT_ERR("Frame exceeding recv MTU (len %d, "
430748454079SGustavo F. Padovan 							"MTU %d)", len,
43080c1bc5c6SGustavo F. Padovan 							chan->imtu);
43090a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
43100a708f8fSGustavo F. Padovan 				l2cap_conn_unreliable(conn, ECOMM);
43110a708f8fSGustavo F. Padovan 				goto drop;
43120a708f8fSGustavo F. Padovan 			}
43130a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
431448454079SGustavo F. Padovan 		}
43150a708f8fSGustavo F. Padovan 
43160a708f8fSGustavo F. Padovan 		/* Allocate skb for the complete frame (with header) */
43170a708f8fSGustavo F. Padovan 		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
43180a708f8fSGustavo F. Padovan 		if (!conn->rx_skb)
43190a708f8fSGustavo F. Padovan 			goto drop;
43200a708f8fSGustavo F. Padovan 
43210a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
43220a708f8fSGustavo F. Padovan 								skb->len);
43230a708f8fSGustavo F. Padovan 		conn->rx_len = len - skb->len;
43240a708f8fSGustavo F. Padovan 	} else {
43250a708f8fSGustavo F. Padovan 		BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
43260a708f8fSGustavo F. Padovan 
43270a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
43280a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected continuation frame (len %d)", skb->len);
43290a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
43300a708f8fSGustavo F. Padovan 			goto drop;
43310a708f8fSGustavo F. Padovan 		}
43320a708f8fSGustavo F. Padovan 
43330a708f8fSGustavo F. Padovan 		if (skb->len > conn->rx_len) {
43340a708f8fSGustavo F. Padovan 			BT_ERR("Fragment is too long (len %d, expected %d)",
43350a708f8fSGustavo F. Padovan 					skb->len, conn->rx_len);
43360a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
43370a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
43380a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
43390a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
43400a708f8fSGustavo F. Padovan 			goto drop;
43410a708f8fSGustavo F. Padovan 		}
43420a708f8fSGustavo F. Padovan 
43430a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
43440a708f8fSGustavo F. Padovan 								skb->len);
43450a708f8fSGustavo F. Padovan 		conn->rx_len -= skb->len;
43460a708f8fSGustavo F. Padovan 
43470a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
43480a708f8fSGustavo F. Padovan 			/* Complete frame received */
43490a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, conn->rx_skb);
43500a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
43510a708f8fSGustavo F. Padovan 		}
43520a708f8fSGustavo F. Padovan 	}
43530a708f8fSGustavo F. Padovan 
43540a708f8fSGustavo F. Padovan drop:
43550a708f8fSGustavo F. Padovan 	kfree_skb(skb);
43560a708f8fSGustavo F. Padovan 	return 0;
43570a708f8fSGustavo F. Padovan }
43580a708f8fSGustavo F. Padovan 
43590a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p)
43600a708f8fSGustavo F. Padovan {
436123691d75SGustavo F. Padovan 	struct l2cap_chan *c;
43620a708f8fSGustavo F. Padovan 
436323691d75SGustavo F. Padovan 	read_lock_bh(&chan_list_lock);
43640a708f8fSGustavo F. Padovan 
436523691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
436623691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
43670a708f8fSGustavo F. Padovan 
4368903d343eSGustavo F. Padovan 		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
43690a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->src),
43700a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->dst),
437189bc500eSGustavo F. Padovan 					c->state, __le16_to_cpu(c->psm),
437223691d75SGustavo F. Padovan 					c->scid, c->dcid, c->imtu, c->omtu,
437323691d75SGustavo F. Padovan 					c->sec_level, c->mode);
43740a708f8fSGustavo F. Padovan 	}
43750a708f8fSGustavo F. Padovan 
437623691d75SGustavo F. Padovan 	read_unlock_bh(&chan_list_lock);
43770a708f8fSGustavo F. Padovan 
43780a708f8fSGustavo F. Padovan 	return 0;
43790a708f8fSGustavo F. Padovan }
43800a708f8fSGustavo F. Padovan 
43810a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file)
43820a708f8fSGustavo F. Padovan {
43830a708f8fSGustavo F. Padovan 	return single_open(file, l2cap_debugfs_show, inode->i_private);
43840a708f8fSGustavo F. Padovan }
43850a708f8fSGustavo F. Padovan 
43860a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = {
43870a708f8fSGustavo F. Padovan 	.open		= l2cap_debugfs_open,
43880a708f8fSGustavo F. Padovan 	.read		= seq_read,
43890a708f8fSGustavo F. Padovan 	.llseek		= seq_lseek,
43900a708f8fSGustavo F. Padovan 	.release	= single_release,
43910a708f8fSGustavo F. Padovan };
43920a708f8fSGustavo F. Padovan 
43930a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs;
43940a708f8fSGustavo F. Padovan 
43950a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = {
43960a708f8fSGustavo F. Padovan 	.name		= "L2CAP",
43970a708f8fSGustavo F. Padovan 	.id		= HCI_PROTO_L2CAP,
43980a708f8fSGustavo F. Padovan 	.connect_ind	= l2cap_connect_ind,
43990a708f8fSGustavo F. Padovan 	.connect_cfm	= l2cap_connect_cfm,
44000a708f8fSGustavo F. Padovan 	.disconn_ind	= l2cap_disconn_ind,
44010a708f8fSGustavo F. Padovan 	.disconn_cfm	= l2cap_disconn_cfm,
44020a708f8fSGustavo F. Padovan 	.security_cfm	= l2cap_security_cfm,
44030a708f8fSGustavo F. Padovan 	.recv_acldata	= l2cap_recv_acldata
44040a708f8fSGustavo F. Padovan };
44050a708f8fSGustavo F. Padovan 
440664274518SGustavo F. Padovan int __init l2cap_init(void)
44070a708f8fSGustavo F. Padovan {
44080a708f8fSGustavo F. Padovan 	int err;
44090a708f8fSGustavo F. Padovan 
4410bb58f747SGustavo F. Padovan 	err = l2cap_init_sockets();
44110a708f8fSGustavo F. Padovan 	if (err < 0)
44120a708f8fSGustavo F. Padovan 		return err;
44130a708f8fSGustavo F. Padovan 
44140a708f8fSGustavo F. Padovan 	_busy_wq = create_singlethread_workqueue("l2cap");
44150a708f8fSGustavo F. Padovan 	if (!_busy_wq) {
4416bb58f747SGustavo F. Padovan 		err = -ENOMEM;
44170a708f8fSGustavo F. Padovan 		goto error;
44180a708f8fSGustavo F. Padovan 	}
44190a708f8fSGustavo F. Padovan 
44200a708f8fSGustavo F. Padovan 	err = hci_register_proto(&l2cap_hci_proto);
44210a708f8fSGustavo F. Padovan 	if (err < 0) {
44220a708f8fSGustavo F. Padovan 		BT_ERR("L2CAP protocol registration failed");
44230a708f8fSGustavo F. Padovan 		bt_sock_unregister(BTPROTO_L2CAP);
44240a708f8fSGustavo F. Padovan 		goto error;
44250a708f8fSGustavo F. Padovan 	}
44260a708f8fSGustavo F. Padovan 
44270a708f8fSGustavo F. Padovan 	if (bt_debugfs) {
44280a708f8fSGustavo F. Padovan 		l2cap_debugfs = debugfs_create_file("l2cap", 0444,
44290a708f8fSGustavo F. Padovan 					bt_debugfs, NULL, &l2cap_debugfs_fops);
44300a708f8fSGustavo F. Padovan 		if (!l2cap_debugfs)
44310a708f8fSGustavo F. Padovan 			BT_ERR("Failed to create L2CAP debug file");
44320a708f8fSGustavo F. Padovan 	}
44330a708f8fSGustavo F. Padovan 
44340a708f8fSGustavo F. Padovan 	return 0;
44350a708f8fSGustavo F. Padovan 
44360a708f8fSGustavo F. Padovan error:
44370a708f8fSGustavo F. Padovan 	destroy_workqueue(_busy_wq);
4438bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
44390a708f8fSGustavo F. Padovan 	return err;
44400a708f8fSGustavo F. Padovan }
44410a708f8fSGustavo F. Padovan 
444264274518SGustavo F. Padovan void l2cap_exit(void)
44430a708f8fSGustavo F. Padovan {
44440a708f8fSGustavo F. Padovan 	debugfs_remove(l2cap_debugfs);
44450a708f8fSGustavo F. Padovan 
44460a708f8fSGustavo F. Padovan 	flush_workqueue(_busy_wq);
44470a708f8fSGustavo F. Padovan 	destroy_workqueue(_busy_wq);
44480a708f8fSGustavo F. Padovan 
44490a708f8fSGustavo F. Padovan 	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
44500a708f8fSGustavo F. Padovan 		BT_ERR("L2CAP protocol unregistration failed");
44510a708f8fSGustavo F. Padovan 
4452bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
44530a708f8fSGustavo F. Padovan }
44540a708f8fSGustavo F. Padovan 
44550a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644);
44560a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
4457