xref: /openbmc/linux/net/bluetooth/l2cap_core.c (revision 71ba0e56)
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>
570a708f8fSGustavo F. Padovan 
58bb58f747SGustavo F. Padovan int disable_ertm;
590a708f8fSGustavo F. Padovan 
600a708f8fSGustavo F. Padovan static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN;
610a708f8fSGustavo F. Padovan static u8 l2cap_fixed_chan[8] = { 0x02, };
620a708f8fSGustavo F. Padovan 
630a708f8fSGustavo F. Padovan static struct workqueue_struct *_busy_wq;
640a708f8fSGustavo F. Padovan 
65b5ad8b7fSJohannes Berg static LIST_HEAD(chan_list);
66b5ad8b7fSJohannes Berg static DEFINE_RWLOCK(chan_list_lock);
670a708f8fSGustavo F. Padovan 
680a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work);
690a708f8fSGustavo F. Padovan 
700a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
710a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data);
724519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
734519de9aSGustavo F. Padovan 								void *data);
74710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
754519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn,
764519de9aSGustavo F. Padovan 				struct l2cap_chan *chan, int err);
770a708f8fSGustavo F. Padovan 
780a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb);
790a708f8fSGustavo F. Padovan 
800a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */
8171ba0e56SGustavo F. Padovan 
8271ba0e56SGustavo F. Padovan static inline void chan_hold(struct l2cap_chan *c)
8371ba0e56SGustavo F. Padovan {
8471ba0e56SGustavo F. Padovan 	atomic_inc(&c->refcnt);
8571ba0e56SGustavo F. Padovan }
8671ba0e56SGustavo F. Padovan 
8771ba0e56SGustavo F. Padovan static inline void chan_put(struct l2cap_chan *c)
8871ba0e56SGustavo F. Padovan {
8971ba0e56SGustavo F. Padovan 	if (atomic_dec_and_test(&c->refcnt))
9071ba0e56SGustavo F. Padovan 		kfree(c);
9171ba0e56SGustavo F. Padovan }
9271ba0e56SGustavo F. Padovan 
93baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
940a708f8fSGustavo F. Padovan {
9548454079SGustavo F. Padovan 	struct l2cap_chan *c;
96baa7e1faSGustavo F. Padovan 
97baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
98fe4128e0SGustavo F. Padovan 		if (c->dcid == cid)
9948454079SGustavo F. Padovan 			return c;
1000a708f8fSGustavo F. Padovan 	}
101baa7e1faSGustavo F. Padovan 	return NULL;
102baa7e1faSGustavo F. Padovan 
103baa7e1faSGustavo F. Padovan }
1040a708f8fSGustavo F. Padovan 
105baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
1060a708f8fSGustavo F. Padovan {
10748454079SGustavo F. Padovan 	struct l2cap_chan *c;
108baa7e1faSGustavo F. Padovan 
109baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
110fe4128e0SGustavo F. Padovan 		if (c->scid == cid)
11148454079SGustavo F. Padovan 			return c;
1120a708f8fSGustavo F. Padovan 	}
113baa7e1faSGustavo F. Padovan 	return NULL;
114baa7e1faSGustavo F. Padovan }
1150a708f8fSGustavo F. Padovan 
1160a708f8fSGustavo F. Padovan /* Find channel with given SCID.
1170a708f8fSGustavo F. Padovan  * Returns locked socket */
118baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
1190a708f8fSGustavo F. Padovan {
12048454079SGustavo F. Padovan 	struct l2cap_chan *c;
121baa7e1faSGustavo F. Padovan 
122baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
123baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_scid(conn, cid);
12448454079SGustavo F. Padovan 	if (c)
12548454079SGustavo F. Padovan 		bh_lock_sock(c->sk);
126baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
12748454079SGustavo F. Padovan 	return c;
1280a708f8fSGustavo F. Padovan }
1290a708f8fSGustavo F. Padovan 
130baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1310a708f8fSGustavo F. Padovan {
13248454079SGustavo F. Padovan 	struct l2cap_chan *c;
133baa7e1faSGustavo F. Padovan 
134baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
135fc7f8a7eSGustavo F. Padovan 		if (c->ident == ident)
13648454079SGustavo F. Padovan 			return c;
1370a708f8fSGustavo F. Padovan 	}
138baa7e1faSGustavo F. Padovan 	return NULL;
139baa7e1faSGustavo F. Padovan }
1400a708f8fSGustavo F. Padovan 
141baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1420a708f8fSGustavo F. Padovan {
14348454079SGustavo F. Padovan 	struct l2cap_chan *c;
144baa7e1faSGustavo F. Padovan 
145baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
146baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_ident(conn, ident);
14748454079SGustavo F. Padovan 	if (c)
14848454079SGustavo F. Padovan 		bh_lock_sock(c->sk);
149baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
15048454079SGustavo F. Padovan 	return c;
1510a708f8fSGustavo F. Padovan }
1520a708f8fSGustavo F. Padovan 
15323691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
1549e4425ffSGustavo F. Padovan {
15523691d75SGustavo F. Padovan 	struct l2cap_chan *c;
1569e4425ffSGustavo F. Padovan 
15723691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
15823691d75SGustavo F. Padovan 		if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src))
1599e4425ffSGustavo F. Padovan 			goto found;
1609e4425ffSGustavo F. Padovan 	}
1619e4425ffSGustavo F. Padovan 
16223691d75SGustavo F. Padovan 	c = NULL;
1639e4425ffSGustavo F. Padovan found:
16423691d75SGustavo F. Padovan 	return c;
1659e4425ffSGustavo F. Padovan }
1669e4425ffSGustavo F. Padovan 
1679e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
1689e4425ffSGustavo F. Padovan {
16973b2ec18SGustavo F. Padovan 	int err;
17073b2ec18SGustavo F. Padovan 
17123691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
1729e4425ffSGustavo F. Padovan 
17323691d75SGustavo F. Padovan 	if (psm && __l2cap_global_chan_by_addr(psm, src)) {
17473b2ec18SGustavo F. Padovan 		err = -EADDRINUSE;
17573b2ec18SGustavo F. Padovan 		goto done;
1769e4425ffSGustavo F. Padovan 	}
1779e4425ffSGustavo F. Padovan 
17873b2ec18SGustavo F. Padovan 	if (psm) {
1799e4425ffSGustavo F. Padovan 		chan->psm = psm;
1809e4425ffSGustavo F. Padovan 		chan->sport = psm;
18173b2ec18SGustavo F. Padovan 		err = 0;
18273b2ec18SGustavo F. Padovan 	} else {
18373b2ec18SGustavo F. Padovan 		u16 p;
1849e4425ffSGustavo F. Padovan 
18573b2ec18SGustavo F. Padovan 		err = -EINVAL;
18673b2ec18SGustavo F. Padovan 		for (p = 0x1001; p < 0x1100; p += 2)
18723691d75SGustavo F. Padovan 			if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
18873b2ec18SGustavo F. Padovan 				chan->psm   = cpu_to_le16(p);
18973b2ec18SGustavo F. Padovan 				chan->sport = cpu_to_le16(p);
19073b2ec18SGustavo F. Padovan 				err = 0;
19173b2ec18SGustavo F. Padovan 				break;
19273b2ec18SGustavo F. Padovan 			}
19373b2ec18SGustavo F. Padovan 	}
19473b2ec18SGustavo F. Padovan 
19573b2ec18SGustavo F. Padovan done:
19623691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
19773b2ec18SGustavo F. Padovan 	return err;
1989e4425ffSGustavo F. Padovan }
1999e4425ffSGustavo F. Padovan 
2009e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid)
2019e4425ffSGustavo F. Padovan {
20223691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
2039e4425ffSGustavo F. Padovan 
2049e4425ffSGustavo F. Padovan 	chan->scid = scid;
2059e4425ffSGustavo F. Padovan 
20623691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
2079e4425ffSGustavo F. Padovan 
2089e4425ffSGustavo F. Padovan 	return 0;
2099e4425ffSGustavo F. Padovan }
2109e4425ffSGustavo F. Padovan 
211baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
2120a708f8fSGustavo F. Padovan {
2130a708f8fSGustavo F. Padovan 	u16 cid = L2CAP_CID_DYN_START;
2140a708f8fSGustavo F. Padovan 
2150a708f8fSGustavo F. Padovan 	for (; cid < L2CAP_CID_DYN_END; cid++) {
216baa7e1faSGustavo F. Padovan 		if (!__l2cap_get_chan_by_scid(conn, cid))
2170a708f8fSGustavo F. Padovan 			return cid;
2180a708f8fSGustavo F. Padovan 	}
2190a708f8fSGustavo F. Padovan 
2200a708f8fSGustavo F. Padovan 	return 0;
2210a708f8fSGustavo F. Padovan }
2220a708f8fSGustavo F. Padovan 
223ab07801dSGustavo F. Padovan static void l2cap_chan_set_timer(struct l2cap_chan *chan, long timeout)
224ab07801dSGustavo F. Padovan {
22589bc500eSGustavo F. Padovan        BT_DBG("chan %p state %d timeout %ld", chan->sk, chan->state, timeout);
22689bc500eSGustavo F. Padovan 
227ab07801dSGustavo F. Padovan        if (!mod_timer(&chan->chan_timer, jiffies + timeout))
22871ba0e56SGustavo F. Padovan 	       chan_hold(chan);
229ab07801dSGustavo F. Padovan }
230ab07801dSGustavo F. Padovan 
231500698d3SGustavo F. Padovan static void l2cap_chan_clear_timer(struct l2cap_chan *chan)
232ab07801dSGustavo F. Padovan {
23389bc500eSGustavo F. Padovan        BT_DBG("chan %p state %d", chan, chan->state);
234ab07801dSGustavo F. Padovan 
235ab07801dSGustavo F. Padovan        if (timer_pending(&chan->chan_timer) && del_timer(&chan->chan_timer))
23671ba0e56SGustavo F. Padovan 	       chan_put(chan);
237ab07801dSGustavo F. Padovan }
238ab07801dSGustavo F. Padovan 
23989bc500eSGustavo F. Padovan static void l2cap_state_change(struct l2cap_chan *chan, int state)
24089bc500eSGustavo F. Padovan {
24189bc500eSGustavo F. Padovan 	chan->state = state;
24289bc500eSGustavo F. Padovan 	chan->ops->state_change(chan->data, state);
24389bc500eSGustavo F. Padovan }
24489bc500eSGustavo F. Padovan 
245ab07801dSGustavo F. Padovan static void l2cap_chan_timeout(unsigned long arg)
246ab07801dSGustavo F. Padovan {
247ab07801dSGustavo F. Padovan 	struct l2cap_chan *chan = (struct l2cap_chan *) arg;
248ab07801dSGustavo F. Padovan 	struct sock *sk = chan->sk;
249ab07801dSGustavo F. Padovan 	int reason;
250ab07801dSGustavo F. Padovan 
25189bc500eSGustavo F. Padovan 	BT_DBG("chan %p state %d", chan, chan->state);
252ab07801dSGustavo F. Padovan 
253ab07801dSGustavo F. Padovan 	bh_lock_sock(sk);
254ab07801dSGustavo F. Padovan 
255ab07801dSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
256ab07801dSGustavo F. Padovan 		/* sk is owned by user. Try again later */
257ab07801dSGustavo F. Padovan 		l2cap_chan_set_timer(chan, HZ / 5);
258ab07801dSGustavo F. Padovan 		bh_unlock_sock(sk);
25971ba0e56SGustavo F. Padovan 		chan_put(chan);
260ab07801dSGustavo F. Padovan 		return;
261ab07801dSGustavo F. Padovan 	}
262ab07801dSGustavo F. Padovan 
26389bc500eSGustavo F. Padovan 	if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
264ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
26589bc500eSGustavo F. Padovan 	else if (chan->state == BT_CONNECT &&
266ab07801dSGustavo F. Padovan 					chan->sec_level != BT_SECURITY_SDP)
267ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
268ab07801dSGustavo F. Padovan 	else
269ab07801dSGustavo F. Padovan 		reason = ETIMEDOUT;
270ab07801dSGustavo F. Padovan 
2710f852724SGustavo F. Padovan 	l2cap_chan_close(chan, reason);
272ab07801dSGustavo F. Padovan 
273ab07801dSGustavo F. Padovan 	bh_unlock_sock(sk);
274ab07801dSGustavo F. Padovan 
275ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
27671ba0e56SGustavo F. Padovan 	chan_put(chan);
277ab07801dSGustavo F. Padovan }
278ab07801dSGustavo F. Padovan 
27923691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk)
2800a708f8fSGustavo F. Padovan {
28148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
2820a708f8fSGustavo F. Padovan 
28348454079SGustavo F. Padovan 	chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
28448454079SGustavo F. Padovan 	if (!chan)
28548454079SGustavo F. Padovan 		return NULL;
2860a708f8fSGustavo F. Padovan 
28748454079SGustavo F. Padovan 	chan->sk = sk;
28848454079SGustavo F. Padovan 
28923691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
29023691d75SGustavo F. Padovan 	list_add(&chan->global_l, &chan_list);
29123691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
29223691d75SGustavo F. Padovan 
293ab07801dSGustavo F. Padovan 	setup_timer(&chan->chan_timer, l2cap_chan_timeout, (unsigned long) chan);
294ab07801dSGustavo F. Padovan 
29589bc500eSGustavo F. Padovan 	chan->state = BT_OPEN;
29689bc500eSGustavo F. Padovan 
29771ba0e56SGustavo F. Padovan 	atomic_set(&chan->refcnt, 1);
29871ba0e56SGustavo F. Padovan 
29948454079SGustavo F. Padovan 	return chan;
3000a708f8fSGustavo F. Padovan }
3010a708f8fSGustavo F. Padovan 
30223691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan)
3036ff5abbfSGustavo F. Padovan {
30423691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
30523691d75SGustavo F. Padovan 	list_del(&chan->global_l);
30623691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
30723691d75SGustavo F. Padovan 
30871ba0e56SGustavo F. Padovan 	chan_put(chan);
3096ff5abbfSGustavo F. Padovan }
3106ff5abbfSGustavo F. Padovan 
31148454079SGustavo F. Padovan static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
3120a708f8fSGustavo F. Padovan {
3130a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
314fe4128e0SGustavo F. Padovan 			chan->psm, chan->dcid);
3150a708f8fSGustavo F. Padovan 
3160a708f8fSGustavo F. Padovan 	conn->disc_reason = 0x13;
3170a708f8fSGustavo F. Padovan 
3188c1d787bSGustavo F. Padovan 	chan->conn = conn;
3190a708f8fSGustavo F. Padovan 
320715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
321b62f328bSVille Tervo 		if (conn->hcon->type == LE_LINK) {
322b62f328bSVille Tervo 			/* LE connection */
3230c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_LE_DEFAULT_MTU;
324fe4128e0SGustavo F. Padovan 			chan->scid = L2CAP_CID_LE_DATA;
325fe4128e0SGustavo F. Padovan 			chan->dcid = L2CAP_CID_LE_DATA;
326b62f328bSVille Tervo 		} else {
3270a708f8fSGustavo F. Padovan 			/* Alloc CID for connection-oriented socket */
328fe4128e0SGustavo F. Padovan 			chan->scid = l2cap_alloc_cid(conn);
3290c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_DEFAULT_MTU;
330b62f328bSVille Tervo 		}
331715ec005SGustavo F. Padovan 	} else if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
3320a708f8fSGustavo F. Padovan 		/* Connectionless socket */
333fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_CONN_LESS;
334fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_CONN_LESS;
3350c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3360a708f8fSGustavo F. Padovan 	} else {
3370a708f8fSGustavo F. Padovan 		/* Raw socket can send/recv signalling messages only */
338fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_SIGNALING;
339fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_SIGNALING;
3400c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3410a708f8fSGustavo F. Padovan 	}
3420a708f8fSGustavo F. Padovan 
34371ba0e56SGustavo F. Padovan 	chan_hold(chan);
344baa7e1faSGustavo F. Padovan 
345baa7e1faSGustavo F. Padovan 	list_add(&chan->list, &conn->chan_l);
3460a708f8fSGustavo F. Padovan }
3470a708f8fSGustavo F. Padovan 
3480a708f8fSGustavo F. Padovan /* Delete channel.
3490a708f8fSGustavo F. Padovan  * Must be called on the locked socket. */
3504519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err)
3510a708f8fSGustavo F. Padovan {
35248454079SGustavo F. Padovan 	struct sock *sk = chan->sk;
3538c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
3540a708f8fSGustavo F. Padovan 	struct sock *parent = bt_sk(sk)->parent;
3550a708f8fSGustavo F. Padovan 
356ab07801dSGustavo F. Padovan 	l2cap_chan_clear_timer(chan);
3570a708f8fSGustavo F. Padovan 
35849208c9cSGustavo F. Padovan 	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
3590a708f8fSGustavo F. Padovan 
3600a708f8fSGustavo F. Padovan 	if (conn) {
361baa7e1faSGustavo F. Padovan 		/* Delete from channel list */
362baa7e1faSGustavo F. Padovan 		write_lock_bh(&conn->chan_lock);
363baa7e1faSGustavo F. Padovan 		list_del(&chan->list);
364baa7e1faSGustavo F. Padovan 		write_unlock_bh(&conn->chan_lock);
36571ba0e56SGustavo F. Padovan 		chan_put(chan);
366baa7e1faSGustavo F. Padovan 
3678c1d787bSGustavo F. Padovan 		chan->conn = NULL;
3680a708f8fSGustavo F. Padovan 		hci_conn_put(conn->hcon);
3690a708f8fSGustavo F. Padovan 	}
3700a708f8fSGustavo F. Padovan 
37189bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CLOSED);
3720a708f8fSGustavo F. Padovan 	sock_set_flag(sk, SOCK_ZAPPED);
3730a708f8fSGustavo F. Padovan 
3740a708f8fSGustavo F. Padovan 	if (err)
3750a708f8fSGustavo F. Padovan 		sk->sk_err = err;
3760a708f8fSGustavo F. Padovan 
3770a708f8fSGustavo F. Padovan 	if (parent) {
3780a708f8fSGustavo F. Padovan 		bt_accept_unlink(sk);
3790a708f8fSGustavo F. Padovan 		parent->sk_data_ready(parent, 0);
3800a708f8fSGustavo F. Padovan 	} else
3810a708f8fSGustavo F. Padovan 		sk->sk_state_change(sk);
3820a708f8fSGustavo F. Padovan 
383b4450035SGustavo F. Padovan 	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE &&
384b4450035SGustavo F. Padovan 			chan->conf_state & L2CAP_CONF_INPUT_DONE))
3856ff5abbfSGustavo F. Padovan 		return;
3862ead70b8SGustavo F. Padovan 
38758d35f87SGustavo F. Padovan 	skb_queue_purge(&chan->tx_q);
3880a708f8fSGustavo F. Padovan 
3890c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
3900a708f8fSGustavo F. Padovan 		struct srej_list *l, *tmp;
3910a708f8fSGustavo F. Padovan 
392e92c8e70SGustavo F. Padovan 		del_timer(&chan->retrans_timer);
393e92c8e70SGustavo F. Padovan 		del_timer(&chan->monitor_timer);
394e92c8e70SGustavo F. Padovan 		del_timer(&chan->ack_timer);
3950a708f8fSGustavo F. Padovan 
396f1c6775bSGustavo F. Padovan 		skb_queue_purge(&chan->srej_q);
397f1c6775bSGustavo F. Padovan 		skb_queue_purge(&chan->busy_q);
3980a708f8fSGustavo F. Padovan 
39939d5a3eeSGustavo F. Padovan 		list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
4000a708f8fSGustavo F. Padovan 			list_del(&l->list);
4010a708f8fSGustavo F. Padovan 			kfree(l);
4020a708f8fSGustavo F. Padovan 		}
4030a708f8fSGustavo F. Padovan 	}
4040a708f8fSGustavo F. Padovan }
4050a708f8fSGustavo F. Padovan 
4064519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent)
4074519de9aSGustavo F. Padovan {
4084519de9aSGustavo F. Padovan 	struct sock *sk;
4094519de9aSGustavo F. Padovan 
4104519de9aSGustavo F. Padovan 	BT_DBG("parent %p", parent);
4114519de9aSGustavo F. Padovan 
4124519de9aSGustavo F. Padovan 	/* Close not yet accepted channels */
4130f852724SGustavo F. Padovan 	while ((sk = bt_accept_dequeue(parent, NULL))) {
414ba3bd0eeSGustavo F. Padovan 		struct l2cap_chan *chan = l2cap_pi(sk)->chan;
415ba3bd0eeSGustavo F. Padovan 		l2cap_chan_clear_timer(chan);
4160f852724SGustavo F. Padovan 		lock_sock(sk);
417ba3bd0eeSGustavo F. Padovan 		l2cap_chan_close(chan, ECONNRESET);
4180f852724SGustavo F. Padovan 		release_sock(sk);
419ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
4200f852724SGustavo F. Padovan 	}
4214519de9aSGustavo F. Padovan }
4224519de9aSGustavo F. Padovan 
4230f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason)
4244519de9aSGustavo F. Padovan {
4254519de9aSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
4264519de9aSGustavo F. Padovan 	struct sock *sk = chan->sk;
4274519de9aSGustavo F. Padovan 
42889bc500eSGustavo F. Padovan 	BT_DBG("chan %p state %d socket %p", chan, chan->state, sk->sk_socket);
4294519de9aSGustavo F. Padovan 
43089bc500eSGustavo F. Padovan 	switch (chan->state) {
4314519de9aSGustavo F. Padovan 	case BT_LISTEN:
4324519de9aSGustavo F. Padovan 		l2cap_chan_cleanup_listen(sk);
43389bc500eSGustavo F. Padovan 
43489bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CLOSED);
43589bc500eSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
4364519de9aSGustavo F. Padovan 		break;
4374519de9aSGustavo F. Padovan 
4384519de9aSGustavo F. Padovan 	case BT_CONNECTED:
4394519de9aSGustavo F. Padovan 	case BT_CONFIG:
440715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4414519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
442500698d3SGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
443ab07801dSGustavo F. Padovan 			l2cap_chan_set_timer(chan, sk->sk_sndtimeo);
4444519de9aSGustavo F. Padovan 			l2cap_send_disconn_req(conn, chan, reason);
4454519de9aSGustavo F. Padovan 		} else
4464519de9aSGustavo F. Padovan 			l2cap_chan_del(chan, reason);
4474519de9aSGustavo F. Padovan 		break;
4484519de9aSGustavo F. Padovan 
4494519de9aSGustavo F. Padovan 	case BT_CONNECT2:
450715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4514519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
4524519de9aSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
4534519de9aSGustavo F. Padovan 			__u16 result;
4544519de9aSGustavo F. Padovan 
4554519de9aSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup)
4564519de9aSGustavo F. Padovan 				result = L2CAP_CR_SEC_BLOCK;
4574519de9aSGustavo F. Padovan 			else
4584519de9aSGustavo F. Padovan 				result = L2CAP_CR_BAD_PSM;
45989bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_DISCONN);
4604519de9aSGustavo F. Padovan 
4614519de9aSGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4624519de9aSGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
4634519de9aSGustavo F. Padovan 			rsp.result = cpu_to_le16(result);
4644519de9aSGustavo F. Padovan 			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4654519de9aSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4664519de9aSGustavo F. Padovan 							sizeof(rsp), &rsp);
4674519de9aSGustavo F. Padovan 		}
4684519de9aSGustavo F. Padovan 
4694519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
4704519de9aSGustavo F. Padovan 		break;
4714519de9aSGustavo F. Padovan 
4724519de9aSGustavo F. Padovan 	case BT_CONNECT:
4734519de9aSGustavo F. Padovan 	case BT_DISCONN:
4744519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
4754519de9aSGustavo F. Padovan 		break;
4764519de9aSGustavo F. Padovan 
4774519de9aSGustavo F. Padovan 	default:
4784519de9aSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
4794519de9aSGustavo F. Padovan 		break;
4804519de9aSGustavo F. Padovan 	}
4814519de9aSGustavo F. Padovan }
4824519de9aSGustavo F. Padovan 
4834343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
4840a708f8fSGustavo F. Padovan {
485715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_RAW) {
4864343478fSGustavo F. Padovan 		switch (chan->sec_level) {
4870a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
4880a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING_MITM;
4890a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
4900a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING;
4910a708f8fSGustavo F. Padovan 		default:
4920a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
4930a708f8fSGustavo F. Padovan 		}
494fe4128e0SGustavo F. Padovan 	} else if (chan->psm == cpu_to_le16(0x0001)) {
4954343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_LOW)
4964343478fSGustavo F. Padovan 			chan->sec_level = BT_SECURITY_SDP;
4970a708f8fSGustavo F. Padovan 
4984343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_HIGH)
4990a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING_MITM;
5000a708f8fSGustavo F. Padovan 		else
5010a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
5020a708f8fSGustavo F. Padovan 	} else {
5034343478fSGustavo F. Padovan 		switch (chan->sec_level) {
5040a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
5050a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING_MITM;
5060a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
5070a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING;
5080a708f8fSGustavo F. Padovan 		default:
5090a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
5100a708f8fSGustavo F. Padovan 		}
5110a708f8fSGustavo F. Padovan 	}
5120a708f8fSGustavo F. Padovan }
5130a708f8fSGustavo F. Padovan 
5140a708f8fSGustavo F. Padovan /* Service level security */
5154343478fSGustavo F. Padovan static inline int l2cap_check_security(struct l2cap_chan *chan)
5160a708f8fSGustavo F. Padovan {
5178c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
5180a708f8fSGustavo F. Padovan 	__u8 auth_type;
5190a708f8fSGustavo F. Padovan 
5204343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
5210a708f8fSGustavo F. Padovan 
5224343478fSGustavo F. Padovan 	return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
5230a708f8fSGustavo F. Padovan }
5240a708f8fSGustavo F. Padovan 
525b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn)
5260a708f8fSGustavo F. Padovan {
5270a708f8fSGustavo F. Padovan 	u8 id;
5280a708f8fSGustavo F. Padovan 
5290a708f8fSGustavo F. Padovan 	/* Get next available identificator.
5300a708f8fSGustavo F. Padovan 	 *    1 - 128 are used by kernel.
5310a708f8fSGustavo F. Padovan 	 *  129 - 199 are reserved.
5320a708f8fSGustavo F. Padovan 	 *  200 - 254 are used by utilities like l2ping, etc.
5330a708f8fSGustavo F. Padovan 	 */
5340a708f8fSGustavo F. Padovan 
5350a708f8fSGustavo F. Padovan 	spin_lock_bh(&conn->lock);
5360a708f8fSGustavo F. Padovan 
5370a708f8fSGustavo F. Padovan 	if (++conn->tx_ident > 128)
5380a708f8fSGustavo F. Padovan 		conn->tx_ident = 1;
5390a708f8fSGustavo F. Padovan 
5400a708f8fSGustavo F. Padovan 	id = conn->tx_ident;
5410a708f8fSGustavo F. Padovan 
5420a708f8fSGustavo F. Padovan 	spin_unlock_bh(&conn->lock);
5430a708f8fSGustavo F. Padovan 
5440a708f8fSGustavo F. Padovan 	return id;
5450a708f8fSGustavo F. Padovan }
5460a708f8fSGustavo F. Padovan 
5474519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
5480a708f8fSGustavo F. Padovan {
5490a708f8fSGustavo F. Padovan 	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
5500a708f8fSGustavo F. Padovan 	u8 flags;
5510a708f8fSGustavo F. Padovan 
5520a708f8fSGustavo F. Padovan 	BT_DBG("code 0x%2.2x", code);
5530a708f8fSGustavo F. Padovan 
5540a708f8fSGustavo F. Padovan 	if (!skb)
5550a708f8fSGustavo F. Padovan 		return;
5560a708f8fSGustavo F. Padovan 
5570a708f8fSGustavo F. Padovan 	if (lmp_no_flush_capable(conn->hcon->hdev))
5580a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
5590a708f8fSGustavo F. Padovan 	else
5600a708f8fSGustavo F. Padovan 		flags = ACL_START;
5610a708f8fSGustavo F. Padovan 
56214b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
56314b12d0bSJaikumar Ganesh 
5640a708f8fSGustavo F. Padovan 	hci_send_acl(conn->hcon, skb, flags);
5650a708f8fSGustavo F. Padovan }
5660a708f8fSGustavo F. Padovan 
567525cd185SGustavo F. Padovan static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control)
5680a708f8fSGustavo F. Padovan {
5690a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
5700a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
5718c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
5720a708f8fSGustavo F. Padovan 	int count, hlen = L2CAP_HDR_SIZE + 2;
5730a708f8fSGustavo F. Padovan 	u8 flags;
5740a708f8fSGustavo F. Padovan 
57589bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
5760a708f8fSGustavo F. Padovan 		return;
5770a708f8fSGustavo F. Padovan 
57847d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
5790a708f8fSGustavo F. Padovan 		hlen += 2;
5800a708f8fSGustavo F. Padovan 
58149208c9cSGustavo F. Padovan 	BT_DBG("chan %p, control 0x%2.2x", chan, control);
5820a708f8fSGustavo F. Padovan 
5830a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, hlen);
5840a708f8fSGustavo F. Padovan 	control |= L2CAP_CTRL_FRAME_TYPE;
5850a708f8fSGustavo F. Padovan 
586525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
5870a708f8fSGustavo F. Padovan 		control |= L2CAP_CTRL_FINAL;
588525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
5890a708f8fSGustavo F. Padovan 	}
5900a708f8fSGustavo F. Padovan 
591525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SEND_PBIT) {
5920a708f8fSGustavo F. Padovan 		control |= L2CAP_CTRL_POLL;
593525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SEND_PBIT;
5940a708f8fSGustavo F. Padovan 	}
5950a708f8fSGustavo F. Padovan 
5960a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
5970a708f8fSGustavo F. Padovan 	if (!skb)
5980a708f8fSGustavo F. Padovan 		return;
5990a708f8fSGustavo F. Padovan 
6000a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
6010a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
602fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
6030a708f8fSGustavo F. Padovan 	put_unaligned_le16(control, skb_put(skb, 2));
6040a708f8fSGustavo F. Padovan 
60547d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
6060a708f8fSGustavo F. Padovan 		u16 fcs = crc16(0, (u8 *)lh, count - 2);
6070a708f8fSGustavo F. Padovan 		put_unaligned_le16(fcs, skb_put(skb, 2));
6080a708f8fSGustavo F. Padovan 	}
6090a708f8fSGustavo F. Padovan 
6100a708f8fSGustavo F. Padovan 	if (lmp_no_flush_capable(conn->hcon->hdev))
6110a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
6120a708f8fSGustavo F. Padovan 	else
6130a708f8fSGustavo F. Padovan 		flags = ACL_START;
6140a708f8fSGustavo F. Padovan 
61514b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = chan->force_active;
61614b12d0bSJaikumar Ganesh 
6178c1d787bSGustavo F. Padovan 	hci_send_acl(chan->conn->hcon, skb, flags);
6180a708f8fSGustavo F. Padovan }
6190a708f8fSGustavo F. Padovan 
620525cd185SGustavo F. Padovan static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control)
6210a708f8fSGustavo F. Padovan {
622525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
6230a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_NOT_READY;
624525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_RNR_SENT;
6250a708f8fSGustavo F. Padovan 	} else
6260a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_READY;
6270a708f8fSGustavo F. Padovan 
62842e5c802SGustavo F. Padovan 	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
6290a708f8fSGustavo F. Padovan 
630525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
6310a708f8fSGustavo F. Padovan }
6320a708f8fSGustavo F. Padovan 
633b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
6340a708f8fSGustavo F. Padovan {
635b4450035SGustavo F. Padovan 	return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND);
6360a708f8fSGustavo F. Padovan }
6370a708f8fSGustavo F. Padovan 
638fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan)
6390a708f8fSGustavo F. Padovan {
6408c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
6410a708f8fSGustavo F. Padovan 
6420a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
6430a708f8fSGustavo F. Padovan 		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
6440a708f8fSGustavo F. Padovan 			return;
6450a708f8fSGustavo F. Padovan 
6464343478fSGustavo F. Padovan 		if (l2cap_check_security(chan) &&
6474343478fSGustavo F. Padovan 				__l2cap_no_conn_pending(chan)) {
6480a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
649fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
650fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
6510a708f8fSGustavo F. Padovan 
652fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
653b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
6540a708f8fSGustavo F. Padovan 
655fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
656fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
6570a708f8fSGustavo F. Padovan 		}
6580a708f8fSGustavo F. Padovan 	} else {
6590a708f8fSGustavo F. Padovan 		struct l2cap_info_req req;
6600a708f8fSGustavo F. Padovan 		req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
6610a708f8fSGustavo F. Padovan 
6620a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
6630a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
6640a708f8fSGustavo F. Padovan 
6650a708f8fSGustavo F. Padovan 		mod_timer(&conn->info_timer, jiffies +
6660a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
6670a708f8fSGustavo F. Padovan 
6680a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
6690a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
6700a708f8fSGustavo F. Padovan 	}
6710a708f8fSGustavo F. Padovan }
6720a708f8fSGustavo F. Padovan 
6730a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
6740a708f8fSGustavo F. Padovan {
6750a708f8fSGustavo F. Padovan 	u32 local_feat_mask = l2cap_feat_mask;
6760a708f8fSGustavo F. Padovan 	if (!disable_ertm)
6770a708f8fSGustavo F. Padovan 		local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
6780a708f8fSGustavo F. Padovan 
6790a708f8fSGustavo F. Padovan 	switch (mode) {
6800a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
6810a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
6820a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
6830a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
6840a708f8fSGustavo F. Padovan 	default:
6850a708f8fSGustavo F. Padovan 		return 0x00;
6860a708f8fSGustavo F. Padovan 	}
6870a708f8fSGustavo F. Padovan }
6880a708f8fSGustavo F. Padovan 
6894519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
6900a708f8fSGustavo F. Padovan {
691e92c8e70SGustavo F. Padovan 	struct sock *sk;
6920a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req req;
6930a708f8fSGustavo F. Padovan 
6940a708f8fSGustavo F. Padovan 	if (!conn)
6950a708f8fSGustavo F. Padovan 		return;
6960a708f8fSGustavo F. Padovan 
697e92c8e70SGustavo F. Padovan 	sk = chan->sk;
698e92c8e70SGustavo F. Padovan 
6990c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
700e92c8e70SGustavo F. Padovan 		del_timer(&chan->retrans_timer);
701e92c8e70SGustavo F. Padovan 		del_timer(&chan->monitor_timer);
702e92c8e70SGustavo F. Padovan 		del_timer(&chan->ack_timer);
7030a708f8fSGustavo F. Padovan 	}
7040a708f8fSGustavo F. Padovan 
705fe4128e0SGustavo F. Padovan 	req.dcid = cpu_to_le16(chan->dcid);
706fe4128e0SGustavo F. Padovan 	req.scid = cpu_to_le16(chan->scid);
7070a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn),
7080a708f8fSGustavo F. Padovan 			L2CAP_DISCONN_REQ, sizeof(req), &req);
7090a708f8fSGustavo F. Padovan 
71089bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_DISCONN);
7110a708f8fSGustavo F. Padovan 	sk->sk_err = err;
7120a708f8fSGustavo F. Padovan }
7130a708f8fSGustavo F. Padovan 
7140a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */
7150a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn)
7160a708f8fSGustavo F. Padovan {
717820ffdb3SGustavo F. Padovan 	struct l2cap_chan *chan, *tmp;
7180a708f8fSGustavo F. Padovan 
7190a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
7200a708f8fSGustavo F. Padovan 
721baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
7220a708f8fSGustavo F. Padovan 
723820ffdb3SGustavo F. Padovan 	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
72448454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
725baa7e1faSGustavo F. Padovan 
7260a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
7270a708f8fSGustavo F. Padovan 
728715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7290a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
7300a708f8fSGustavo F. Padovan 			continue;
7310a708f8fSGustavo F. Padovan 		}
7320a708f8fSGustavo F. Padovan 
73389bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
7340a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
7350a708f8fSGustavo F. Padovan 
7364343478fSGustavo F. Padovan 			if (!l2cap_check_security(chan) ||
737b4450035SGustavo F. Padovan 					!__l2cap_no_conn_pending(chan)) {
7380a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7390a708f8fSGustavo F. Padovan 				continue;
7400a708f8fSGustavo F. Padovan 			}
7410a708f8fSGustavo F. Padovan 
7420c1bc5c6SGustavo F. Padovan 			if (!l2cap_mode_supported(chan->mode,
7430a708f8fSGustavo F. Padovan 					conn->feat_mask)
744b4450035SGustavo F. Padovan 					&& chan->conf_state &
7450a708f8fSGustavo F. Padovan 					L2CAP_CONF_STATE2_DEVICE) {
7460f852724SGustavo F. Padovan 				/* l2cap_chan_close() calls list_del(chan)
747820ffdb3SGustavo F. Padovan 				 * so release the lock */
748820ffdb3SGustavo F. Padovan 				read_unlock_bh(&conn->chan_lock);
7490f852724SGustavo F. Padovan 				l2cap_chan_close(chan, ECONNRESET);
750820ffdb3SGustavo F. Padovan 				read_lock_bh(&conn->chan_lock);
7510a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7520a708f8fSGustavo F. Padovan 				continue;
7530a708f8fSGustavo F. Padovan 			}
7540a708f8fSGustavo F. Padovan 
755fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
756fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
7570a708f8fSGustavo F. Padovan 
758fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
759b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
7600a708f8fSGustavo F. Padovan 
761fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
762fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
7630a708f8fSGustavo F. Padovan 
76489bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
7650a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
7660a708f8fSGustavo F. Padovan 			char buf[128];
767fe4128e0SGustavo F. Padovan 			rsp.scid = cpu_to_le16(chan->dcid);
768fe4128e0SGustavo F. Padovan 			rsp.dcid = cpu_to_le16(chan->scid);
7690a708f8fSGustavo F. Padovan 
7704343478fSGustavo F. Padovan 			if (l2cap_check_security(chan)) {
7710a708f8fSGustavo F. Padovan 				if (bt_sk(sk)->defer_setup) {
7720a708f8fSGustavo F. Padovan 					struct sock *parent = bt_sk(sk)->parent;
7730a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_PEND);
7740a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
7750a708f8fSGustavo F. Padovan 					parent->sk_data_ready(parent, 0);
7760a708f8fSGustavo F. Padovan 
7770a708f8fSGustavo F. Padovan 				} else {
77889bc500eSGustavo F. Padovan 					l2cap_state_change(chan, BT_CONFIG);
7790a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
7800a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
7810a708f8fSGustavo F. Padovan 				}
7820a708f8fSGustavo F. Padovan 			} else {
7830a708f8fSGustavo F. Padovan 				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
7840a708f8fSGustavo F. Padovan 				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
7850a708f8fSGustavo F. Padovan 			}
7860a708f8fSGustavo F. Padovan 
787fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
788fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
7890a708f8fSGustavo F. Padovan 
790b4450035SGustavo F. Padovan 			if (chan->conf_state & L2CAP_CONF_REQ_SENT ||
7910a708f8fSGustavo F. Padovan 					rsp.result != L2CAP_CR_SUCCESS) {
7920a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7930a708f8fSGustavo F. Padovan 				continue;
7940a708f8fSGustavo F. Padovan 			}
7950a708f8fSGustavo F. Padovan 
796b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_REQ_SENT;
7970a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
79873ffa904SGustavo F. Padovan 						l2cap_build_conf_req(chan, buf), buf);
79973ffa904SGustavo F. Padovan 			chan->num_conf_req++;
8000a708f8fSGustavo F. Padovan 		}
8010a708f8fSGustavo F. Padovan 
8020a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
8030a708f8fSGustavo F. Padovan 	}
8040a708f8fSGustavo F. Padovan 
805baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
8060a708f8fSGustavo F. Padovan }
8070a708f8fSGustavo F. Padovan 
808b62f328bSVille Tervo /* Find socket with cid and source bdaddr.
809b62f328bSVille Tervo  * Returns closest match, locked.
810b62f328bSVille Tervo  */
81123691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src)
812b62f328bSVille Tervo {
81323691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
814b62f328bSVille Tervo 
81523691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
816b62f328bSVille Tervo 
81723691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
81823691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
819fe4128e0SGustavo F. Padovan 
82089bc500eSGustavo F. Padovan 		if (state && c->state != state)
821b62f328bSVille Tervo 			continue;
822b62f328bSVille Tervo 
82323691d75SGustavo F. Padovan 		if (c->scid == cid) {
824b62f328bSVille Tervo 			/* Exact match. */
82523691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
82623691d75SGustavo F. Padovan 				read_unlock(&chan_list_lock);
82723691d75SGustavo F. Padovan 				return c;
82823691d75SGustavo F. Padovan 			}
829b62f328bSVille Tervo 
830b62f328bSVille Tervo 			/* Closest match */
831b62f328bSVille Tervo 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
83223691d75SGustavo F. Padovan 				c1 = c;
833b62f328bSVille Tervo 		}
834b62f328bSVille Tervo 	}
835280f294fSGustavo F. Padovan 
83623691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
837b62f328bSVille Tervo 
83823691d75SGustavo F. Padovan 	return c1;
839b62f328bSVille Tervo }
840b62f328bSVille Tervo 
841b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn)
842b62f328bSVille Tervo {
843c916fbe4SGustavo F. Padovan 	struct sock *parent, *sk;
84423691d75SGustavo F. Padovan 	struct l2cap_chan *chan, *pchan;
845b62f328bSVille Tervo 
846b62f328bSVille Tervo 	BT_DBG("");
847b62f328bSVille Tervo 
848b62f328bSVille Tervo 	/* Check if we have socket listening on cid */
84923691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
850b62f328bSVille Tervo 							conn->src);
85123691d75SGustavo F. Padovan 	if (!pchan)
852b62f328bSVille Tervo 		return;
853b62f328bSVille Tervo 
85423691d75SGustavo F. Padovan 	parent = pchan->sk;
85523691d75SGustavo F. Padovan 
85662f3a2cfSGustavo F. Padovan 	bh_lock_sock(parent);
85762f3a2cfSGustavo F. Padovan 
858b62f328bSVille Tervo 	/* Check for backlog size */
859b62f328bSVille Tervo 	if (sk_acceptq_is_full(parent)) {
860b62f328bSVille Tervo 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
861b62f328bSVille Tervo 		goto clean;
862b62f328bSVille Tervo 	}
863b62f328bSVille Tervo 
86480808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
86580808e43SGustavo F. Padovan 	if (!chan)
866b62f328bSVille Tervo 		goto clean;
867b62f328bSVille Tervo 
86880808e43SGustavo F. Padovan 	sk = chan->sk;
8695d41ce1dSGustavo F. Padovan 
870baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
871b62f328bSVille Tervo 
872b62f328bSVille Tervo 	hci_conn_hold(conn->hcon);
873b62f328bSVille Tervo 
874b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->src, conn->src);
875b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->dst, conn->dst);
876b62f328bSVille Tervo 
877d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
878d1010240SGustavo F. Padovan 
87948454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
88048454079SGustavo F. Padovan 
881ab07801dSGustavo F. Padovan 	l2cap_chan_set_timer(chan, sk->sk_sndtimeo);
882b62f328bSVille Tervo 
88389bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CONNECTED);
884b62f328bSVille Tervo 	parent->sk_data_ready(parent, 0);
885b62f328bSVille Tervo 
886baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
887b62f328bSVille Tervo 
888b62f328bSVille Tervo clean:
889b62f328bSVille Tervo 	bh_unlock_sock(parent);
890b62f328bSVille Tervo }
891b62f328bSVille Tervo 
8920a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn)
8930a708f8fSGustavo F. Padovan {
89448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
8950a708f8fSGustavo F. Padovan 
8960a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
8970a708f8fSGustavo F. Padovan 
898b62f328bSVille Tervo 	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
899b62f328bSVille Tervo 		l2cap_le_conn_ready(conn);
900b62f328bSVille Tervo 
901baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
9020a708f8fSGustavo F. Padovan 
903baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
90448454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
905baa7e1faSGustavo F. Padovan 
9060a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
9070a708f8fSGustavo F. Padovan 
908acd7d370SVille Tervo 		if (conn->hcon->type == LE_LINK) {
909ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
91089bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_CONNECTED);
911acd7d370SVille Tervo 			sk->sk_state_change(sk);
912acd7d370SVille Tervo 		}
913acd7d370SVille Tervo 
914715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
915ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
91689bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_CONNECTED);
9170a708f8fSGustavo F. Padovan 			sk->sk_state_change(sk);
91889bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT)
919fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
9200a708f8fSGustavo F. Padovan 
9210a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
9220a708f8fSGustavo F. Padovan 	}
9230a708f8fSGustavo F. Padovan 
924baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
9250a708f8fSGustavo F. Padovan }
9260a708f8fSGustavo F. Padovan 
9270a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */
9280a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
9290a708f8fSGustavo F. Padovan {
93048454079SGustavo F. Padovan 	struct l2cap_chan *chan;
9310a708f8fSGustavo F. Padovan 
9320a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
9330a708f8fSGustavo F. Padovan 
934baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
9350a708f8fSGustavo F. Padovan 
936baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
93748454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
938baa7e1faSGustavo F. Padovan 
9394343478fSGustavo F. Padovan 		if (chan->force_reliable)
9400a708f8fSGustavo F. Padovan 			sk->sk_err = err;
9410a708f8fSGustavo F. Padovan 	}
9420a708f8fSGustavo F. Padovan 
943baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
9440a708f8fSGustavo F. Padovan }
9450a708f8fSGustavo F. Padovan 
9460a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg)
9470a708f8fSGustavo F. Padovan {
9480a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = (void *) arg;
9490a708f8fSGustavo F. Padovan 
9500a708f8fSGustavo F. Padovan 	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
9510a708f8fSGustavo F. Padovan 	conn->info_ident = 0;
9520a708f8fSGustavo F. Padovan 
9530a708f8fSGustavo F. Padovan 	l2cap_conn_start(conn);
9540a708f8fSGustavo F. Padovan }
9550a708f8fSGustavo F. Padovan 
9560a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
9570a708f8fSGustavo F. Padovan {
9580a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
9590a708f8fSGustavo F. Padovan 
9600a708f8fSGustavo F. Padovan 	if (conn || status)
9610a708f8fSGustavo F. Padovan 		return conn;
9620a708f8fSGustavo F. Padovan 
9630a708f8fSGustavo F. Padovan 	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
9640a708f8fSGustavo F. Padovan 	if (!conn)
9650a708f8fSGustavo F. Padovan 		return NULL;
9660a708f8fSGustavo F. Padovan 
9670a708f8fSGustavo F. Padovan 	hcon->l2cap_data = conn;
9680a708f8fSGustavo F. Padovan 	conn->hcon = hcon;
9690a708f8fSGustavo F. Padovan 
9700a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p conn %p", hcon, conn);
9710a708f8fSGustavo F. Padovan 
972acd7d370SVille Tervo 	if (hcon->hdev->le_mtu && hcon->type == LE_LINK)
973acd7d370SVille Tervo 		conn->mtu = hcon->hdev->le_mtu;
974acd7d370SVille Tervo 	else
9750a708f8fSGustavo F. Padovan 		conn->mtu = hcon->hdev->acl_mtu;
976acd7d370SVille Tervo 
9770a708f8fSGustavo F. Padovan 	conn->src = &hcon->hdev->bdaddr;
9780a708f8fSGustavo F. Padovan 	conn->dst = &hcon->dst;
9790a708f8fSGustavo F. Padovan 
9800a708f8fSGustavo F. Padovan 	conn->feat_mask = 0;
9810a708f8fSGustavo F. Padovan 
9820a708f8fSGustavo F. Padovan 	spin_lock_init(&conn->lock);
983baa7e1faSGustavo F. Padovan 	rwlock_init(&conn->chan_lock);
984baa7e1faSGustavo F. Padovan 
985baa7e1faSGustavo F. Padovan 	INIT_LIST_HEAD(&conn->chan_l);
9860a708f8fSGustavo F. Padovan 
987b62f328bSVille Tervo 	if (hcon->type != LE_LINK)
9880a708f8fSGustavo F. Padovan 		setup_timer(&conn->info_timer, l2cap_info_timeout,
9890a708f8fSGustavo F. Padovan 						(unsigned long) conn);
9900a708f8fSGustavo F. Padovan 
9910a708f8fSGustavo F. Padovan 	conn->disc_reason = 0x13;
9920a708f8fSGustavo F. Padovan 
9930a708f8fSGustavo F. Padovan 	return conn;
9940a708f8fSGustavo F. Padovan }
9950a708f8fSGustavo F. Padovan 
9960a708f8fSGustavo F. Padovan static void l2cap_conn_del(struct hci_conn *hcon, int err)
9970a708f8fSGustavo F. Padovan {
9980a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
999baa7e1faSGustavo F. Padovan 	struct l2cap_chan *chan, *l;
10000a708f8fSGustavo F. Padovan 	struct sock *sk;
10010a708f8fSGustavo F. Padovan 
10020a708f8fSGustavo F. Padovan 	if (!conn)
10030a708f8fSGustavo F. Padovan 		return;
10040a708f8fSGustavo F. Padovan 
10050a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
10060a708f8fSGustavo F. Padovan 
10070a708f8fSGustavo F. Padovan 	kfree_skb(conn->rx_skb);
10080a708f8fSGustavo F. Padovan 
10090a708f8fSGustavo F. Padovan 	/* Kill channels */
1010baa7e1faSGustavo F. Padovan 	list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
101148454079SGustavo F. Padovan 		sk = chan->sk;
10120a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
101348454079SGustavo F. Padovan 		l2cap_chan_del(chan, err);
10140a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
1015ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
10160a708f8fSGustavo F. Padovan 	}
10170a708f8fSGustavo F. Padovan 
10180a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
10190a708f8fSGustavo F. Padovan 		del_timer_sync(&conn->info_timer);
10200a708f8fSGustavo F. Padovan 
10210a708f8fSGustavo F. Padovan 	hcon->l2cap_data = NULL;
10220a708f8fSGustavo F. Padovan 	kfree(conn);
10230a708f8fSGustavo F. Padovan }
10240a708f8fSGustavo F. Padovan 
102548454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
10260a708f8fSGustavo F. Padovan {
1027baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
102848454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
1029baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
10300a708f8fSGustavo F. Padovan }
10310a708f8fSGustavo F. Padovan 
10320a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */
10330a708f8fSGustavo F. Padovan 
10340a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr.
10350a708f8fSGustavo F. Padovan  * Returns closest match.
10360a708f8fSGustavo F. Padovan  */
103723691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src)
10380a708f8fSGustavo F. Padovan {
103923691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
10400a708f8fSGustavo F. Padovan 
104123691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
10420a708f8fSGustavo F. Padovan 
104323691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
104423691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
1045fe4128e0SGustavo F. Padovan 
104689bc500eSGustavo F. Padovan 		if (state && c->state != state)
10470a708f8fSGustavo F. Padovan 			continue;
10480a708f8fSGustavo F. Padovan 
104923691d75SGustavo F. Padovan 		if (c->psm == psm) {
10500a708f8fSGustavo F. Padovan 			/* Exact match. */
105123691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
1052a7567b20SJohannes Berg 				read_unlock(&chan_list_lock);
105323691d75SGustavo F. Padovan 				return c;
105423691d75SGustavo F. Padovan 			}
10550a708f8fSGustavo F. Padovan 
10560a708f8fSGustavo F. Padovan 			/* Closest match */
10570a708f8fSGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
105823691d75SGustavo F. Padovan 				c1 = c;
10590a708f8fSGustavo F. Padovan 		}
10600a708f8fSGustavo F. Padovan 	}
10610a708f8fSGustavo F. Padovan 
106223691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
10630a708f8fSGustavo F. Padovan 
106423691d75SGustavo F. Padovan 	return c1;
10650a708f8fSGustavo F. Padovan }
10660a708f8fSGustavo F. Padovan 
106777a74c7eSGustavo F. Padovan int l2cap_chan_connect(struct l2cap_chan *chan)
10680a708f8fSGustavo F. Padovan {
10695d41ce1dSGustavo F. Padovan 	struct sock *sk = chan->sk;
10700a708f8fSGustavo F. Padovan 	bdaddr_t *src = &bt_sk(sk)->src;
10710a708f8fSGustavo F. Padovan 	bdaddr_t *dst = &bt_sk(sk)->dst;
10720a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
10730a708f8fSGustavo F. Padovan 	struct hci_conn *hcon;
10740a708f8fSGustavo F. Padovan 	struct hci_dev *hdev;
10750a708f8fSGustavo F. Padovan 	__u8 auth_type;
10760a708f8fSGustavo F. Padovan 	int err;
10770a708f8fSGustavo F. Padovan 
10780a708f8fSGustavo F. Padovan 	BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
1079fe4128e0SGustavo F. Padovan 							chan->psm);
10800a708f8fSGustavo F. Padovan 
10810a708f8fSGustavo F. Padovan 	hdev = hci_get_route(dst, src);
10820a708f8fSGustavo F. Padovan 	if (!hdev)
10830a708f8fSGustavo F. Padovan 		return -EHOSTUNREACH;
10840a708f8fSGustavo F. Padovan 
10850a708f8fSGustavo F. Padovan 	hci_dev_lock_bh(hdev);
10860a708f8fSGustavo F. Padovan 
10874343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
10880a708f8fSGustavo F. Padovan 
1089fe4128e0SGustavo F. Padovan 	if (chan->dcid == L2CAP_CID_LE_DATA)
1090acd7d370SVille Tervo 		hcon = hci_connect(hdev, LE_LINK, dst,
10914343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1092acd7d370SVille Tervo 	else
10930a708f8fSGustavo F. Padovan 		hcon = hci_connect(hdev, ACL_LINK, dst,
10944343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1095acd7d370SVille Tervo 
109630e76272SVille Tervo 	if (IS_ERR(hcon)) {
109730e76272SVille Tervo 		err = PTR_ERR(hcon);
10980a708f8fSGustavo F. Padovan 		goto done;
109930e76272SVille Tervo 	}
11000a708f8fSGustavo F. Padovan 
11010a708f8fSGustavo F. Padovan 	conn = l2cap_conn_add(hcon, 0);
11020a708f8fSGustavo F. Padovan 	if (!conn) {
11030a708f8fSGustavo F. Padovan 		hci_conn_put(hcon);
110430e76272SVille Tervo 		err = -ENOMEM;
11050a708f8fSGustavo F. Padovan 		goto done;
11060a708f8fSGustavo F. Padovan 	}
11070a708f8fSGustavo F. Padovan 
11080a708f8fSGustavo F. Padovan 	/* Update source addr of the socket */
11090a708f8fSGustavo F. Padovan 	bacpy(src, conn->src);
11100a708f8fSGustavo F. Padovan 
111148454079SGustavo F. Padovan 	l2cap_chan_add(conn, chan);
111248454079SGustavo F. Padovan 
111389bc500eSGustavo F. Padovan 	l2cap_state_change(chan, BT_CONNECT);
1114ab07801dSGustavo F. Padovan 	l2cap_chan_set_timer(chan, sk->sk_sndtimeo);
11150a708f8fSGustavo F. Padovan 
11160a708f8fSGustavo F. Padovan 	if (hcon->state == BT_CONNECTED) {
1117715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1118ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
11194343478fSGustavo F. Padovan 			if (l2cap_check_security(chan))
112089bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONNECTED);
11210a708f8fSGustavo F. Padovan 		} else
1122fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
11230a708f8fSGustavo F. Padovan 	}
11240a708f8fSGustavo F. Padovan 
112530e76272SVille Tervo 	err = 0;
112630e76272SVille Tervo 
11270a708f8fSGustavo F. Padovan done:
11280a708f8fSGustavo F. Padovan 	hci_dev_unlock_bh(hdev);
11290a708f8fSGustavo F. Padovan 	hci_dev_put(hdev);
11300a708f8fSGustavo F. Padovan 	return err;
11310a708f8fSGustavo F. Padovan }
11320a708f8fSGustavo F. Padovan 
1133dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk)
11340a708f8fSGustavo F. Padovan {
11358c1d787bSGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
11360a708f8fSGustavo F. Padovan 	DECLARE_WAITQUEUE(wait, current);
11370a708f8fSGustavo F. Padovan 	int err = 0;
11380a708f8fSGustavo F. Padovan 	int timeo = HZ/5;
11390a708f8fSGustavo F. Padovan 
11400a708f8fSGustavo F. Padovan 	add_wait_queue(sk_sleep(sk), &wait);
11418c1d787bSGustavo F. Padovan 	while ((chan->unacked_frames > 0 && chan->conn)) {
11420a708f8fSGustavo F. Padovan 		set_current_state(TASK_INTERRUPTIBLE);
11430a708f8fSGustavo F. Padovan 
11440a708f8fSGustavo F. Padovan 		if (!timeo)
11450a708f8fSGustavo F. Padovan 			timeo = HZ/5;
11460a708f8fSGustavo F. Padovan 
11470a708f8fSGustavo F. Padovan 		if (signal_pending(current)) {
11480a708f8fSGustavo F. Padovan 			err = sock_intr_errno(timeo);
11490a708f8fSGustavo F. Padovan 			break;
11500a708f8fSGustavo F. Padovan 		}
11510a708f8fSGustavo F. Padovan 
11520a708f8fSGustavo F. Padovan 		release_sock(sk);
11530a708f8fSGustavo F. Padovan 		timeo = schedule_timeout(timeo);
11540a708f8fSGustavo F. Padovan 		lock_sock(sk);
11550a708f8fSGustavo F. Padovan 
11560a708f8fSGustavo F. Padovan 		err = sock_error(sk);
11570a708f8fSGustavo F. Padovan 		if (err)
11580a708f8fSGustavo F. Padovan 			break;
11590a708f8fSGustavo F. Padovan 	}
11600a708f8fSGustavo F. Padovan 	set_current_state(TASK_RUNNING);
11610a708f8fSGustavo F. Padovan 	remove_wait_queue(sk_sleep(sk), &wait);
11620a708f8fSGustavo F. Padovan 	return err;
11630a708f8fSGustavo F. Padovan }
11640a708f8fSGustavo F. Padovan 
11650a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg)
11660a708f8fSGustavo F. Padovan {
1167525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
1168525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
11690a708f8fSGustavo F. Padovan 
1170525cd185SGustavo F. Padovan 	BT_DBG("chan %p", chan);
11710a708f8fSGustavo F. Padovan 
11720a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
11732c03a7a4SGustavo F. Padovan 	if (chan->retry_count >= chan->remote_max_tx) {
11748c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
11750a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
11760a708f8fSGustavo F. Padovan 		return;
11770a708f8fSGustavo F. Padovan 	}
11780a708f8fSGustavo F. Padovan 
11796a026610SGustavo F. Padovan 	chan->retry_count++;
11800a708f8fSGustavo F. Padovan 	__mod_monitor_timer();
11810a708f8fSGustavo F. Padovan 
1182525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
11830a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
11840a708f8fSGustavo F. Padovan }
11850a708f8fSGustavo F. Padovan 
11860a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg)
11870a708f8fSGustavo F. Padovan {
1188525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
1189525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
11900a708f8fSGustavo F. Padovan 
119149208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
11920a708f8fSGustavo F. Padovan 
11930a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
11946a026610SGustavo F. Padovan 	chan->retry_count = 1;
11950a708f8fSGustavo F. Padovan 	__mod_monitor_timer();
11960a708f8fSGustavo F. Padovan 
1197525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_WAIT_F;
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 
120342e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
12040a708f8fSGustavo F. Padovan {
12050a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
12060a708f8fSGustavo F. Padovan 
120758d35f87SGustavo F. Padovan 	while ((skb = skb_peek(&chan->tx_q)) &&
12086a026610SGustavo F. Padovan 			chan->unacked_frames) {
120942e5c802SGustavo F. Padovan 		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
12100a708f8fSGustavo F. Padovan 			break;
12110a708f8fSGustavo F. Padovan 
121258d35f87SGustavo F. Padovan 		skb = skb_dequeue(&chan->tx_q);
12130a708f8fSGustavo F. Padovan 		kfree_skb(skb);
12140a708f8fSGustavo F. Padovan 
12156a026610SGustavo F. Padovan 		chan->unacked_frames--;
12160a708f8fSGustavo F. Padovan 	}
12170a708f8fSGustavo F. Padovan 
12186a026610SGustavo F. Padovan 	if (!chan->unacked_frames)
1219e92c8e70SGustavo F. Padovan 		del_timer(&chan->retrans_timer);
12200a708f8fSGustavo F. Padovan }
12210a708f8fSGustavo F. Padovan 
12224343478fSGustavo F. Padovan void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
12230a708f8fSGustavo F. Padovan {
12248c1d787bSGustavo F. Padovan 	struct hci_conn *hcon = chan->conn->hcon;
12250a708f8fSGustavo F. Padovan 	u16 flags;
12260a708f8fSGustavo F. Padovan 
12274343478fSGustavo F. Padovan 	BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len);
12280a708f8fSGustavo F. Padovan 
12294343478fSGustavo F. Padovan 	if (!chan->flushable && lmp_no_flush_capable(hcon->hdev))
12300a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
12310a708f8fSGustavo F. Padovan 	else
12320a708f8fSGustavo F. Padovan 		flags = ACL_START;
12330a708f8fSGustavo F. Padovan 
123414b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = chan->force_active;
12350a708f8fSGustavo F. Padovan 	hci_send_acl(hcon, skb, flags);
12360a708f8fSGustavo F. Padovan }
12370a708f8fSGustavo F. Padovan 
123842e5c802SGustavo F. Padovan void l2cap_streaming_send(struct l2cap_chan *chan)
12390a708f8fSGustavo F. Padovan {
12400a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
12410a708f8fSGustavo F. Padovan 	u16 control, fcs;
12420a708f8fSGustavo F. Padovan 
124358d35f87SGustavo F. Padovan 	while ((skb = skb_dequeue(&chan->tx_q))) {
12440a708f8fSGustavo F. Padovan 		control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE);
124542e5c802SGustavo F. Padovan 		control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT;
12460a708f8fSGustavo F. Padovan 		put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE);
12470a708f8fSGustavo F. Padovan 
124847d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
12490a708f8fSGustavo F. Padovan 			fcs = crc16(0, (u8 *)skb->data, skb->len - 2);
12500a708f8fSGustavo F. Padovan 			put_unaligned_le16(fcs, skb->data + skb->len - 2);
12510a708f8fSGustavo F. Padovan 		}
12520a708f8fSGustavo F. Padovan 
12534343478fSGustavo F. Padovan 		l2cap_do_send(chan, skb);
12540a708f8fSGustavo F. Padovan 
125542e5c802SGustavo F. Padovan 		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
12560a708f8fSGustavo F. Padovan 	}
12570a708f8fSGustavo F. Padovan }
12580a708f8fSGustavo F. Padovan 
1259525cd185SGustavo F. Padovan static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq)
12600a708f8fSGustavo F. Padovan {
12610a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
12620a708f8fSGustavo F. Padovan 	u16 control, fcs;
12630a708f8fSGustavo F. Padovan 
126458d35f87SGustavo F. Padovan 	skb = skb_peek(&chan->tx_q);
12650a708f8fSGustavo F. Padovan 	if (!skb)
12660a708f8fSGustavo F. Padovan 		return;
12670a708f8fSGustavo F. Padovan 
12680a708f8fSGustavo F. Padovan 	do {
12690a708f8fSGustavo F. Padovan 		if (bt_cb(skb)->tx_seq == tx_seq)
12700a708f8fSGustavo F. Padovan 			break;
12710a708f8fSGustavo F. Padovan 
127258d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
12730a708f8fSGustavo F. Padovan 			return;
12740a708f8fSGustavo F. Padovan 
127558d35f87SGustavo F. Padovan 	} while ((skb = skb_queue_next(&chan->tx_q, skb)));
12760a708f8fSGustavo F. Padovan 
12772c03a7a4SGustavo F. Padovan 	if (chan->remote_max_tx &&
12782c03a7a4SGustavo F. Padovan 			bt_cb(skb)->retries == chan->remote_max_tx) {
12798c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
12800a708f8fSGustavo F. Padovan 		return;
12810a708f8fSGustavo F. Padovan 	}
12820a708f8fSGustavo F. Padovan 
12830a708f8fSGustavo F. Padovan 	tx_skb = skb_clone(skb, GFP_ATOMIC);
12840a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries++;
12850a708f8fSGustavo F. Padovan 	control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1286a429b519SRuiyi Zhang 	control &= L2CAP_CTRL_SAR;
12870a708f8fSGustavo F. Padovan 
1288525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
12890a708f8fSGustavo F. Padovan 		control |= L2CAP_CTRL_FINAL;
1290525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
12910a708f8fSGustavo F. Padovan 	}
12920a708f8fSGustavo F. Padovan 
129342e5c802SGustavo F. Padovan 	control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
12940a708f8fSGustavo F. Padovan 			| (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
12950a708f8fSGustavo F. Padovan 
12960a708f8fSGustavo F. Padovan 	put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);
12970a708f8fSGustavo F. Padovan 
129847d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
12990a708f8fSGustavo F. Padovan 		fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2);
13000a708f8fSGustavo F. Padovan 		put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2);
13010a708f8fSGustavo F. Padovan 	}
13020a708f8fSGustavo F. Padovan 
13034343478fSGustavo F. Padovan 	l2cap_do_send(chan, tx_skb);
13040a708f8fSGustavo F. Padovan }
13050a708f8fSGustavo F. Padovan 
1306525cd185SGustavo F. Padovan int l2cap_ertm_send(struct l2cap_chan *chan)
13070a708f8fSGustavo F. Padovan {
13080a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
13090a708f8fSGustavo F. Padovan 	u16 control, fcs;
13100a708f8fSGustavo F. Padovan 	int nsent = 0;
13110a708f8fSGustavo F. Padovan 
131289bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
13130a708f8fSGustavo F. Padovan 		return -ENOTCONN;
13140a708f8fSGustavo F. Padovan 
131558d35f87SGustavo F. Padovan 	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
13160a708f8fSGustavo F. Padovan 
13172c03a7a4SGustavo F. Padovan 		if (chan->remote_max_tx &&
13182c03a7a4SGustavo F. Padovan 				bt_cb(skb)->retries == chan->remote_max_tx) {
13198c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
13200a708f8fSGustavo F. Padovan 			break;
13210a708f8fSGustavo F. Padovan 		}
13220a708f8fSGustavo F. Padovan 
13230a708f8fSGustavo F. Padovan 		tx_skb = skb_clone(skb, GFP_ATOMIC);
13240a708f8fSGustavo F. Padovan 
13250a708f8fSGustavo F. Padovan 		bt_cb(skb)->retries++;
13260a708f8fSGustavo F. Padovan 
13270a708f8fSGustavo F. Padovan 		control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
13280a708f8fSGustavo F. Padovan 		control &= L2CAP_CTRL_SAR;
13290a708f8fSGustavo F. Padovan 
1330525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
13310a708f8fSGustavo F. Padovan 			control |= L2CAP_CTRL_FINAL;
1332525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
13330a708f8fSGustavo F. Padovan 		}
133442e5c802SGustavo F. Padovan 		control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
133542e5c802SGustavo F. Padovan 				| (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
13360a708f8fSGustavo F. Padovan 		put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);
13370a708f8fSGustavo F. Padovan 
13380a708f8fSGustavo F. Padovan 
133947d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
13400a708f8fSGustavo F. Padovan 			fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2);
13410a708f8fSGustavo F. Padovan 			put_unaligned_le16(fcs, skb->data + tx_skb->len - 2);
13420a708f8fSGustavo F. Padovan 		}
13430a708f8fSGustavo F. Padovan 
13444343478fSGustavo F. Padovan 		l2cap_do_send(chan, tx_skb);
13450a708f8fSGustavo F. Padovan 
13460a708f8fSGustavo F. Padovan 		__mod_retrans_timer();
13470a708f8fSGustavo F. Padovan 
134842e5c802SGustavo F. Padovan 		bt_cb(skb)->tx_seq = chan->next_tx_seq;
134942e5c802SGustavo F. Padovan 		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
13500a708f8fSGustavo F. Padovan 
135123e9fde2SSuraj Sumangala 		if (bt_cb(skb)->retries == 1)
13526a026610SGustavo F. Padovan 			chan->unacked_frames++;
135323e9fde2SSuraj Sumangala 
13546a026610SGustavo F. Padovan 		chan->frames_sent++;
13550a708f8fSGustavo F. Padovan 
135658d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
135758d35f87SGustavo F. Padovan 			chan->tx_send_head = NULL;
13580a708f8fSGustavo F. Padovan 		else
135958d35f87SGustavo F. Padovan 			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
13600a708f8fSGustavo F. Padovan 
13610a708f8fSGustavo F. Padovan 		nsent++;
13620a708f8fSGustavo F. Padovan 	}
13630a708f8fSGustavo F. Padovan 
13640a708f8fSGustavo F. Padovan 	return nsent;
13650a708f8fSGustavo F. Padovan }
13660a708f8fSGustavo F. Padovan 
1367525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan)
13680a708f8fSGustavo F. Padovan {
13690a708f8fSGustavo F. Padovan 	int ret;
13700a708f8fSGustavo F. Padovan 
137158d35f87SGustavo F. Padovan 	if (!skb_queue_empty(&chan->tx_q))
137258d35f87SGustavo F. Padovan 		chan->tx_send_head = chan->tx_q.next;
13730a708f8fSGustavo F. Padovan 
137442e5c802SGustavo F. Padovan 	chan->next_tx_seq = chan->expected_ack_seq;
1375525cd185SGustavo F. Padovan 	ret = l2cap_ertm_send(chan);
13760a708f8fSGustavo F. Padovan 	return ret;
13770a708f8fSGustavo F. Padovan }
13780a708f8fSGustavo F. Padovan 
1379525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan)
13800a708f8fSGustavo F. Padovan {
13810a708f8fSGustavo F. Padovan 	u16 control = 0;
13820a708f8fSGustavo F. Padovan 
138342e5c802SGustavo F. Padovan 	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
13840a708f8fSGustavo F. Padovan 
1385525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
13860a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_NOT_READY;
1387525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_RNR_SENT;
1388525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
13890a708f8fSGustavo F. Padovan 		return;
13900a708f8fSGustavo F. Padovan 	}
13910a708f8fSGustavo F. Padovan 
1392525cd185SGustavo F. Padovan 	if (l2cap_ertm_send(chan) > 0)
13930a708f8fSGustavo F. Padovan 		return;
13940a708f8fSGustavo F. Padovan 
13950a708f8fSGustavo F. Padovan 	control |= L2CAP_SUPER_RCV_READY;
1396525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
13970a708f8fSGustavo F. Padovan }
13980a708f8fSGustavo F. Padovan 
1399525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan)
14000a708f8fSGustavo F. Padovan {
14010a708f8fSGustavo F. Padovan 	struct srej_list *tail;
14020a708f8fSGustavo F. Padovan 	u16 control;
14030a708f8fSGustavo F. Padovan 
14040a708f8fSGustavo F. Padovan 	control = L2CAP_SUPER_SELECT_REJECT;
14050a708f8fSGustavo F. Padovan 	control |= L2CAP_CTRL_FINAL;
14060a708f8fSGustavo F. Padovan 
140739d5a3eeSGustavo F. Padovan 	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
14080a708f8fSGustavo F. Padovan 	control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
14090a708f8fSGustavo F. Padovan 
1410525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
14110a708f8fSGustavo F. Padovan }
14120a708f8fSGustavo F. Padovan 
14130a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb)
14140a708f8fSGustavo F. Padovan {
14158c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
14160a708f8fSGustavo F. Padovan 	struct sk_buff **frag;
14170a708f8fSGustavo F. Padovan 	int err, sent = 0;
14180a708f8fSGustavo F. Padovan 
14190a708f8fSGustavo F. Padovan 	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
14200a708f8fSGustavo F. Padovan 		return -EFAULT;
14210a708f8fSGustavo F. Padovan 
14220a708f8fSGustavo F. Padovan 	sent += count;
14230a708f8fSGustavo F. Padovan 	len  -= count;
14240a708f8fSGustavo F. Padovan 
14250a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
14260a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
14270a708f8fSGustavo F. Padovan 	while (len) {
14280a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
14290a708f8fSGustavo F. Padovan 
14300a708f8fSGustavo F. Padovan 		*frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err);
14310a708f8fSGustavo F. Padovan 		if (!*frag)
14320a708f8fSGustavo F. Padovan 			return err;
14330a708f8fSGustavo F. Padovan 		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
14340a708f8fSGustavo F. Padovan 			return -EFAULT;
14350a708f8fSGustavo F. Padovan 
14360a708f8fSGustavo F. Padovan 		sent += count;
14370a708f8fSGustavo F. Padovan 		len  -= count;
14380a708f8fSGustavo F. Padovan 
14390a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
14400a708f8fSGustavo F. Padovan 	}
14410a708f8fSGustavo F. Padovan 
14420a708f8fSGustavo F. Padovan 	return sent;
14430a708f8fSGustavo F. Padovan }
14440a708f8fSGustavo F. Padovan 
1445fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
14460a708f8fSGustavo F. Padovan {
1447fe4128e0SGustavo F. Padovan 	struct sock *sk = chan->sk;
14488c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
14490a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
14500a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE + 2;
14510a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
14520a708f8fSGustavo F. Padovan 
14530a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
14540a708f8fSGustavo F. Padovan 
14550a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
14560a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
14570a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
14580a708f8fSGustavo F. Padovan 	if (!skb)
14590a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
14600a708f8fSGustavo F. Padovan 
14610a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
14620a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1463fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
14640a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1465fe4128e0SGustavo F. Padovan 	put_unaligned_le16(chan->psm, skb_put(skb, 2));
14660a708f8fSGustavo F. Padovan 
14670a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
14680a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
14690a708f8fSGustavo F. Padovan 		kfree_skb(skb);
14700a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
14710a708f8fSGustavo F. Padovan 	}
14720a708f8fSGustavo F. Padovan 	return skb;
14730a708f8fSGustavo F. Padovan }
14740a708f8fSGustavo F. Padovan 
1475fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
14760a708f8fSGustavo F. Padovan {
1477fe4128e0SGustavo F. Padovan 	struct sock *sk = chan->sk;
14788c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
14790a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
14800a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE;
14810a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
14820a708f8fSGustavo F. Padovan 
14830a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
14840a708f8fSGustavo F. Padovan 
14850a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
14860a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
14870a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
14880a708f8fSGustavo F. Padovan 	if (!skb)
14890a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
14900a708f8fSGustavo F. Padovan 
14910a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
14920a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1493fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
14940a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
14950a708f8fSGustavo F. Padovan 
14960a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
14970a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
14980a708f8fSGustavo F. Padovan 		kfree_skb(skb);
14990a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15000a708f8fSGustavo F. Padovan 	}
15010a708f8fSGustavo F. Padovan 	return skb;
15020a708f8fSGustavo F. Padovan }
15030a708f8fSGustavo F. Padovan 
150447d1ec61SGustavo F. Padovan struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen)
15050a708f8fSGustavo F. Padovan {
150647d1ec61SGustavo F. Padovan 	struct sock *sk = chan->sk;
15078c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
15080a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
15090a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE + 2;
15100a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
15110a708f8fSGustavo F. Padovan 
15120a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
15130a708f8fSGustavo F. Padovan 
15140a708f8fSGustavo F. Padovan 	if (!conn)
15150a708f8fSGustavo F. Padovan 		return ERR_PTR(-ENOTCONN);
15160a708f8fSGustavo F. Padovan 
15170a708f8fSGustavo F. Padovan 	if (sdulen)
15180a708f8fSGustavo F. Padovan 		hlen += 2;
15190a708f8fSGustavo F. Padovan 
152047d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
15210a708f8fSGustavo F. Padovan 		hlen += 2;
15220a708f8fSGustavo F. Padovan 
15230a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
15240a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
15250a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
15260a708f8fSGustavo F. Padovan 	if (!skb)
15270a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15280a708f8fSGustavo F. Padovan 
15290a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
15300a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1531fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
15320a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
15330a708f8fSGustavo F. Padovan 	put_unaligned_le16(control, skb_put(skb, 2));
15340a708f8fSGustavo F. Padovan 	if (sdulen)
15350a708f8fSGustavo F. Padovan 		put_unaligned_le16(sdulen, skb_put(skb, 2));
15360a708f8fSGustavo F. Padovan 
15370a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
15380a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
15390a708f8fSGustavo F. Padovan 		kfree_skb(skb);
15400a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15410a708f8fSGustavo F. Padovan 	}
15420a708f8fSGustavo F. Padovan 
154347d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
15440a708f8fSGustavo F. Padovan 		put_unaligned_le16(0, skb_put(skb, 2));
15450a708f8fSGustavo F. Padovan 
15460a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries = 0;
15470a708f8fSGustavo F. Padovan 	return skb;
15480a708f8fSGustavo F. Padovan }
15490a708f8fSGustavo F. Padovan 
15502c03a7a4SGustavo F. Padovan int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
15510a708f8fSGustavo F. Padovan {
15520a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
15530a708f8fSGustavo F. Padovan 	struct sk_buff_head sar_queue;
15540a708f8fSGustavo F. Padovan 	u16 control;
15550a708f8fSGustavo F. Padovan 	size_t size = 0;
15560a708f8fSGustavo F. Padovan 
15570a708f8fSGustavo F. Padovan 	skb_queue_head_init(&sar_queue);
15580a708f8fSGustavo F. Padovan 	control = L2CAP_SDU_START;
155947d1ec61SGustavo F. Padovan 	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
15600a708f8fSGustavo F. Padovan 	if (IS_ERR(skb))
15610a708f8fSGustavo F. Padovan 		return PTR_ERR(skb);
15620a708f8fSGustavo F. Padovan 
15630a708f8fSGustavo F. Padovan 	__skb_queue_tail(&sar_queue, skb);
15642c03a7a4SGustavo F. Padovan 	len -= chan->remote_mps;
15652c03a7a4SGustavo F. Padovan 	size += chan->remote_mps;
15660a708f8fSGustavo F. Padovan 
15670a708f8fSGustavo F. Padovan 	while (len > 0) {
15680a708f8fSGustavo F. Padovan 		size_t buflen;
15690a708f8fSGustavo F. Padovan 
15702c03a7a4SGustavo F. Padovan 		if (len > chan->remote_mps) {
15710a708f8fSGustavo F. Padovan 			control = L2CAP_SDU_CONTINUE;
15722c03a7a4SGustavo F. Padovan 			buflen = chan->remote_mps;
15730a708f8fSGustavo F. Padovan 		} else {
15740a708f8fSGustavo F. Padovan 			control = L2CAP_SDU_END;
15750a708f8fSGustavo F. Padovan 			buflen = len;
15760a708f8fSGustavo F. Padovan 		}
15770a708f8fSGustavo F. Padovan 
157847d1ec61SGustavo F. Padovan 		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
15790a708f8fSGustavo F. Padovan 		if (IS_ERR(skb)) {
15800a708f8fSGustavo F. Padovan 			skb_queue_purge(&sar_queue);
15810a708f8fSGustavo F. Padovan 			return PTR_ERR(skb);
15820a708f8fSGustavo F. Padovan 		}
15830a708f8fSGustavo F. Padovan 
15840a708f8fSGustavo F. Padovan 		__skb_queue_tail(&sar_queue, skb);
15850a708f8fSGustavo F. Padovan 		len -= buflen;
15860a708f8fSGustavo F. Padovan 		size += buflen;
15870a708f8fSGustavo F. Padovan 	}
158858d35f87SGustavo F. Padovan 	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
158958d35f87SGustavo F. Padovan 	if (chan->tx_send_head == NULL)
159058d35f87SGustavo F. Padovan 		chan->tx_send_head = sar_queue.next;
15910a708f8fSGustavo F. Padovan 
15920a708f8fSGustavo F. Padovan 	return size;
15930a708f8fSGustavo F. Padovan }
15940a708f8fSGustavo F. Padovan 
15959a91a04aSGustavo F. Padovan int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
15969a91a04aSGustavo F. Padovan {
15979a91a04aSGustavo F. Padovan 	struct sk_buff *skb;
15989a91a04aSGustavo F. Padovan 	u16 control;
15999a91a04aSGustavo F. Padovan 	int err;
16009a91a04aSGustavo F. Padovan 
16019a91a04aSGustavo F. Padovan 	/* Connectionless channel */
1602715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
16039a91a04aSGustavo F. Padovan 		skb = l2cap_create_connless_pdu(chan, msg, len);
16049a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
16059a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
16069a91a04aSGustavo F. Padovan 
16079a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
16089a91a04aSGustavo F. Padovan 		return len;
16099a91a04aSGustavo F. Padovan 	}
16109a91a04aSGustavo F. Padovan 
16119a91a04aSGustavo F. Padovan 	switch (chan->mode) {
16129a91a04aSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
16139a91a04aSGustavo F. Padovan 		/* Check outgoing MTU */
16149a91a04aSGustavo F. Padovan 		if (len > chan->omtu)
16159a91a04aSGustavo F. Padovan 			return -EMSGSIZE;
16169a91a04aSGustavo F. Padovan 
16179a91a04aSGustavo F. Padovan 		/* Create a basic PDU */
16189a91a04aSGustavo F. Padovan 		skb = l2cap_create_basic_pdu(chan, msg, len);
16199a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
16209a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
16219a91a04aSGustavo F. Padovan 
16229a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
16239a91a04aSGustavo F. Padovan 		err = len;
16249a91a04aSGustavo F. Padovan 		break;
16259a91a04aSGustavo F. Padovan 
16269a91a04aSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
16279a91a04aSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
16289a91a04aSGustavo F. Padovan 		/* Entire SDU fits into one PDU */
16299a91a04aSGustavo F. Padovan 		if (len <= chan->remote_mps) {
16309a91a04aSGustavo F. Padovan 			control = L2CAP_SDU_UNSEGMENTED;
16319a91a04aSGustavo F. Padovan 			skb = l2cap_create_iframe_pdu(chan, msg, len, control,
16329a91a04aSGustavo F. Padovan 									0);
16339a91a04aSGustavo F. Padovan 			if (IS_ERR(skb))
16349a91a04aSGustavo F. Padovan 				return PTR_ERR(skb);
16359a91a04aSGustavo F. Padovan 
16369a91a04aSGustavo F. Padovan 			__skb_queue_tail(&chan->tx_q, skb);
16379a91a04aSGustavo F. Padovan 
16389a91a04aSGustavo F. Padovan 			if (chan->tx_send_head == NULL)
16399a91a04aSGustavo F. Padovan 				chan->tx_send_head = skb;
16409a91a04aSGustavo F. Padovan 
16419a91a04aSGustavo F. Padovan 		} else {
16429a91a04aSGustavo F. Padovan 			/* Segment SDU into multiples PDUs */
16439a91a04aSGustavo F. Padovan 			err = l2cap_sar_segment_sdu(chan, msg, len);
16449a91a04aSGustavo F. Padovan 			if (err < 0)
16459a91a04aSGustavo F. Padovan 				return err;
16469a91a04aSGustavo F. Padovan 		}
16479a91a04aSGustavo F. Padovan 
16489a91a04aSGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_STREAMING) {
16499a91a04aSGustavo F. Padovan 			l2cap_streaming_send(chan);
16509a91a04aSGustavo F. Padovan 			err = len;
16519a91a04aSGustavo F. Padovan 			break;
16529a91a04aSGustavo F. Padovan 		}
16539a91a04aSGustavo F. Padovan 
16549a91a04aSGustavo F. Padovan 		if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
16559a91a04aSGustavo F. Padovan 				(chan->conn_state & L2CAP_CONN_WAIT_F)) {
16569a91a04aSGustavo F. Padovan 			err = len;
16579a91a04aSGustavo F. Padovan 			break;
16589a91a04aSGustavo F. Padovan 		}
16599a91a04aSGustavo F. Padovan 
16609a91a04aSGustavo F. Padovan 		err = l2cap_ertm_send(chan);
16619a91a04aSGustavo F. Padovan 		if (err >= 0)
16629a91a04aSGustavo F. Padovan 			err = len;
16639a91a04aSGustavo F. Padovan 
16649a91a04aSGustavo F. Padovan 		break;
16659a91a04aSGustavo F. Padovan 
16669a91a04aSGustavo F. Padovan 	default:
16679a91a04aSGustavo F. Padovan 		BT_DBG("bad state %1.1x", chan->mode);
16689a91a04aSGustavo F. Padovan 		err = -EBADFD;
16699a91a04aSGustavo F. Padovan 	}
16709a91a04aSGustavo F. Padovan 
16719a91a04aSGustavo F. Padovan 	return err;
16729a91a04aSGustavo F. Padovan }
16739a91a04aSGustavo F. Padovan 
16740a708f8fSGustavo F. Padovan static void l2cap_chan_ready(struct sock *sk)
16750a708f8fSGustavo F. Padovan {
16760a708f8fSGustavo F. Padovan 	struct sock *parent = bt_sk(sk)->parent;
1677b4450035SGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
16780a708f8fSGustavo F. Padovan 
16790a708f8fSGustavo F. Padovan 	BT_DBG("sk %p, parent %p", sk, parent);
16800a708f8fSGustavo F. Padovan 
1681b4450035SGustavo F. Padovan 	chan->conf_state = 0;
1682ab07801dSGustavo F. Padovan 	l2cap_chan_clear_timer(chan);
16830a708f8fSGustavo F. Padovan 
16840a708f8fSGustavo F. Padovan 	if (!parent) {
16850a708f8fSGustavo F. Padovan 		/* Outgoing channel.
16860a708f8fSGustavo F. Padovan 		 * Wake up socket sleeping on connect.
16870a708f8fSGustavo F. Padovan 		 */
168889bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
16890a708f8fSGustavo F. Padovan 		sk->sk_state_change(sk);
16900a708f8fSGustavo F. Padovan 	} else {
16910a708f8fSGustavo F. Padovan 		/* Incoming channel.
16920a708f8fSGustavo F. Padovan 		 * Wake up socket sleeping on accept.
16930a708f8fSGustavo F. Padovan 		 */
16940a708f8fSGustavo F. Padovan 		parent->sk_data_ready(parent, 0);
16950a708f8fSGustavo F. Padovan 	}
16960a708f8fSGustavo F. Padovan }
16970a708f8fSGustavo F. Padovan 
16980a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */
16990a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
17000a708f8fSGustavo F. Padovan {
17010a708f8fSGustavo F. Padovan 	struct sk_buff *nskb;
170248454079SGustavo F. Padovan 	struct l2cap_chan *chan;
17030a708f8fSGustavo F. Padovan 
17040a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
17050a708f8fSGustavo F. Padovan 
1706baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
1707baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
170848454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
1709715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_RAW)
17100a708f8fSGustavo F. Padovan 			continue;
17110a708f8fSGustavo F. Padovan 
17120a708f8fSGustavo F. Padovan 		/* Don't send frame to the socket it came from */
17130a708f8fSGustavo F. Padovan 		if (skb->sk == sk)
17140a708f8fSGustavo F. Padovan 			continue;
17150a708f8fSGustavo F. Padovan 		nskb = skb_clone(skb, GFP_ATOMIC);
17160a708f8fSGustavo F. Padovan 		if (!nskb)
17170a708f8fSGustavo F. Padovan 			continue;
17180a708f8fSGustavo F. Padovan 
171923070494SGustavo F. Padovan 		if (chan->ops->recv(chan->data, nskb))
17200a708f8fSGustavo F. Padovan 			kfree_skb(nskb);
17210a708f8fSGustavo F. Padovan 	}
1722baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
17230a708f8fSGustavo F. Padovan }
17240a708f8fSGustavo F. Padovan 
17250a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */
17260a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
17270a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data)
17280a708f8fSGustavo F. Padovan {
17290a708f8fSGustavo F. Padovan 	struct sk_buff *skb, **frag;
17300a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr *cmd;
17310a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
17320a708f8fSGustavo F. Padovan 	int len, count;
17330a708f8fSGustavo F. Padovan 
17340a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
17350a708f8fSGustavo F. Padovan 			conn, code, ident, dlen);
17360a708f8fSGustavo F. Padovan 
17370a708f8fSGustavo F. Padovan 	len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
17380a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, len);
17390a708f8fSGustavo F. Padovan 
17400a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
17410a708f8fSGustavo F. Padovan 	if (!skb)
17420a708f8fSGustavo F. Padovan 		return NULL;
17430a708f8fSGustavo F. Padovan 
17440a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
17450a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
17463300d9a9SClaudio Takahasi 
17473300d9a9SClaudio Takahasi 	if (conn->hcon->type == LE_LINK)
17483300d9a9SClaudio Takahasi 		lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
17493300d9a9SClaudio Takahasi 	else
17500a708f8fSGustavo F. Padovan 		lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
17510a708f8fSGustavo F. Padovan 
17520a708f8fSGustavo F. Padovan 	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
17530a708f8fSGustavo F. Padovan 	cmd->code  = code;
17540a708f8fSGustavo F. Padovan 	cmd->ident = ident;
17550a708f8fSGustavo F. Padovan 	cmd->len   = cpu_to_le16(dlen);
17560a708f8fSGustavo F. Padovan 
17570a708f8fSGustavo F. Padovan 	if (dlen) {
17580a708f8fSGustavo F. Padovan 		count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
17590a708f8fSGustavo F. Padovan 		memcpy(skb_put(skb, count), data, count);
17600a708f8fSGustavo F. Padovan 		data += count;
17610a708f8fSGustavo F. Padovan 	}
17620a708f8fSGustavo F. Padovan 
17630a708f8fSGustavo F. Padovan 	len -= skb->len;
17640a708f8fSGustavo F. Padovan 
17650a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
17660a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
17670a708f8fSGustavo F. Padovan 	while (len) {
17680a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
17690a708f8fSGustavo F. Padovan 
17700a708f8fSGustavo F. Padovan 		*frag = bt_skb_alloc(count, GFP_ATOMIC);
17710a708f8fSGustavo F. Padovan 		if (!*frag)
17720a708f8fSGustavo F. Padovan 			goto fail;
17730a708f8fSGustavo F. Padovan 
17740a708f8fSGustavo F. Padovan 		memcpy(skb_put(*frag, count), data, count);
17750a708f8fSGustavo F. Padovan 
17760a708f8fSGustavo F. Padovan 		len  -= count;
17770a708f8fSGustavo F. Padovan 		data += count;
17780a708f8fSGustavo F. Padovan 
17790a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
17800a708f8fSGustavo F. Padovan 	}
17810a708f8fSGustavo F. Padovan 
17820a708f8fSGustavo F. Padovan 	return skb;
17830a708f8fSGustavo F. Padovan 
17840a708f8fSGustavo F. Padovan fail:
17850a708f8fSGustavo F. Padovan 	kfree_skb(skb);
17860a708f8fSGustavo F. Padovan 	return NULL;
17870a708f8fSGustavo F. Padovan }
17880a708f8fSGustavo F. Padovan 
17890a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
17900a708f8fSGustavo F. Padovan {
17910a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
17920a708f8fSGustavo F. Padovan 	int len;
17930a708f8fSGustavo F. Padovan 
17940a708f8fSGustavo F. Padovan 	len = L2CAP_CONF_OPT_SIZE + opt->len;
17950a708f8fSGustavo F. Padovan 	*ptr += len;
17960a708f8fSGustavo F. Padovan 
17970a708f8fSGustavo F. Padovan 	*type = opt->type;
17980a708f8fSGustavo F. Padovan 	*olen = opt->len;
17990a708f8fSGustavo F. Padovan 
18000a708f8fSGustavo F. Padovan 	switch (opt->len) {
18010a708f8fSGustavo F. Padovan 	case 1:
18020a708f8fSGustavo F. Padovan 		*val = *((u8 *) opt->val);
18030a708f8fSGustavo F. Padovan 		break;
18040a708f8fSGustavo F. Padovan 
18050a708f8fSGustavo F. Padovan 	case 2:
18060a708f8fSGustavo F. Padovan 		*val = get_unaligned_le16(opt->val);
18070a708f8fSGustavo F. Padovan 		break;
18080a708f8fSGustavo F. Padovan 
18090a708f8fSGustavo F. Padovan 	case 4:
18100a708f8fSGustavo F. Padovan 		*val = get_unaligned_le32(opt->val);
18110a708f8fSGustavo F. Padovan 		break;
18120a708f8fSGustavo F. Padovan 
18130a708f8fSGustavo F. Padovan 	default:
18140a708f8fSGustavo F. Padovan 		*val = (unsigned long) opt->val;
18150a708f8fSGustavo F. Padovan 		break;
18160a708f8fSGustavo F. Padovan 	}
18170a708f8fSGustavo F. Padovan 
18180a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
18190a708f8fSGustavo F. Padovan 	return len;
18200a708f8fSGustavo F. Padovan }
18210a708f8fSGustavo F. Padovan 
18220a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
18230a708f8fSGustavo F. Padovan {
18240a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
18250a708f8fSGustavo F. Padovan 
18260a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
18270a708f8fSGustavo F. Padovan 
18280a708f8fSGustavo F. Padovan 	opt->type = type;
18290a708f8fSGustavo F. Padovan 	opt->len  = len;
18300a708f8fSGustavo F. Padovan 
18310a708f8fSGustavo F. Padovan 	switch (len) {
18320a708f8fSGustavo F. Padovan 	case 1:
18330a708f8fSGustavo F. Padovan 		*((u8 *) opt->val)  = val;
18340a708f8fSGustavo F. Padovan 		break;
18350a708f8fSGustavo F. Padovan 
18360a708f8fSGustavo F. Padovan 	case 2:
18370a708f8fSGustavo F. Padovan 		put_unaligned_le16(val, opt->val);
18380a708f8fSGustavo F. Padovan 		break;
18390a708f8fSGustavo F. Padovan 
18400a708f8fSGustavo F. Padovan 	case 4:
18410a708f8fSGustavo F. Padovan 		put_unaligned_le32(val, opt->val);
18420a708f8fSGustavo F. Padovan 		break;
18430a708f8fSGustavo F. Padovan 
18440a708f8fSGustavo F. Padovan 	default:
18450a708f8fSGustavo F. Padovan 		memcpy(opt->val, (void *) val, len);
18460a708f8fSGustavo F. Padovan 		break;
18470a708f8fSGustavo F. Padovan 	}
18480a708f8fSGustavo F. Padovan 
18490a708f8fSGustavo F. Padovan 	*ptr += L2CAP_CONF_OPT_SIZE + len;
18500a708f8fSGustavo F. Padovan }
18510a708f8fSGustavo F. Padovan 
18520a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg)
18530a708f8fSGustavo F. Padovan {
1854525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
18550a708f8fSGustavo F. Padovan 
1856525cd185SGustavo F. Padovan 	bh_lock_sock(chan->sk);
1857525cd185SGustavo F. Padovan 	l2cap_send_ack(chan);
1858525cd185SGustavo F. Padovan 	bh_unlock_sock(chan->sk);
18590a708f8fSGustavo F. Padovan }
18600a708f8fSGustavo F. Padovan 
1861525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan)
18620a708f8fSGustavo F. Padovan {
1863525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
1864525cd185SGustavo F. Padovan 
186542e5c802SGustavo F. Padovan 	chan->expected_ack_seq = 0;
18666a026610SGustavo F. Padovan 	chan->unacked_frames = 0;
186742e5c802SGustavo F. Padovan 	chan->buffer_seq = 0;
18686a026610SGustavo F. Padovan 	chan->num_acked = 0;
18696a026610SGustavo F. Padovan 	chan->frames_sent = 0;
18700a708f8fSGustavo F. Padovan 
1871e92c8e70SGustavo F. Padovan 	setup_timer(&chan->retrans_timer, l2cap_retrans_timeout,
1872e92c8e70SGustavo F. Padovan 							(unsigned long) chan);
1873e92c8e70SGustavo F. Padovan 	setup_timer(&chan->monitor_timer, l2cap_monitor_timeout,
1874e92c8e70SGustavo F. Padovan 							(unsigned long) chan);
1875e92c8e70SGustavo F. Padovan 	setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan);
18760a708f8fSGustavo F. Padovan 
1877f1c6775bSGustavo F. Padovan 	skb_queue_head_init(&chan->srej_q);
1878f1c6775bSGustavo F. Padovan 	skb_queue_head_init(&chan->busy_q);
18790a708f8fSGustavo F. Padovan 
188039d5a3eeSGustavo F. Padovan 	INIT_LIST_HEAD(&chan->srej_l);
188139d5a3eeSGustavo F. Padovan 
1882311bb895SGustavo F. Padovan 	INIT_WORK(&chan->busy_work, l2cap_busy_work);
18830a708f8fSGustavo F. Padovan 
18840a708f8fSGustavo F. Padovan 	sk->sk_backlog_rcv = l2cap_ertm_data_rcv;
18850a708f8fSGustavo F. Padovan }
18860a708f8fSGustavo F. Padovan 
18870a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
18880a708f8fSGustavo F. Padovan {
18890a708f8fSGustavo F. Padovan 	switch (mode) {
18900a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
18910a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
18920a708f8fSGustavo F. Padovan 		if (l2cap_mode_supported(mode, remote_feat_mask))
18930a708f8fSGustavo F. Padovan 			return mode;
18940a708f8fSGustavo F. Padovan 		/* fall through */
18950a708f8fSGustavo F. Padovan 	default:
18960a708f8fSGustavo F. Padovan 		return L2CAP_MODE_BASIC;
18970a708f8fSGustavo F. Padovan 	}
18980a708f8fSGustavo F. Padovan }
18990a708f8fSGustavo F. Padovan 
1900710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
19010a708f8fSGustavo F. Padovan {
19020a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
19030c1bc5c6SGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
19040a708f8fSGustavo F. Padovan 	void *ptr = req->data;
19050a708f8fSGustavo F. Padovan 
190649208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
19070a708f8fSGustavo F. Padovan 
190873ffa904SGustavo F. Padovan 	if (chan->num_conf_req || chan->num_conf_rsp)
19090a708f8fSGustavo F. Padovan 		goto done;
19100a708f8fSGustavo F. Padovan 
19110c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
19120a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
19130a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
1914b4450035SGustavo F. Padovan 		if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE)
19150a708f8fSGustavo F. Padovan 			break;
19160a708f8fSGustavo F. Padovan 
19170a708f8fSGustavo F. Padovan 		/* fall through */
19180a708f8fSGustavo F. Padovan 	default:
19198c1d787bSGustavo F. Padovan 		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
19200a708f8fSGustavo F. Padovan 		break;
19210a708f8fSGustavo F. Padovan 	}
19220a708f8fSGustavo F. Padovan 
19230a708f8fSGustavo F. Padovan done:
19240c1bc5c6SGustavo F. Padovan 	if (chan->imtu != L2CAP_DEFAULT_MTU)
19250c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
19260a708f8fSGustavo F. Padovan 
19270c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
19280a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
19298c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
19308c1d787bSGustavo F. Padovan 				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
19310a708f8fSGustavo F. Padovan 			break;
19320a708f8fSGustavo F. Padovan 
19330a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_BASIC;
19340a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
19350a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
19360a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
19370a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
19380a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = 0;
19390a708f8fSGustavo F. Padovan 
19400a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
19410a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
19420a708f8fSGustavo F. Padovan 		break;
19430a708f8fSGustavo F. Padovan 
19440a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
19450a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_ERTM;
194647d1ec61SGustavo F. Padovan 		rfc.txwin_size      = chan->tx_win;
194747d1ec61SGustavo F. Padovan 		rfc.max_transmit    = chan->max_tx;
19480a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
19490a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
19500a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
19518c1d787bSGustavo F. Padovan 		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
19528c1d787bSGustavo F. Padovan 			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
19530a708f8fSGustavo F. Padovan 
19540a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
19550a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
19560a708f8fSGustavo F. Padovan 
19578c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
19580a708f8fSGustavo F. Padovan 			break;
19590a708f8fSGustavo F. Padovan 
196047d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
1961b4450035SGustavo F. Padovan 				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
196247d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
196347d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
19640a708f8fSGustavo F. Padovan 		}
19650a708f8fSGustavo F. Padovan 		break;
19660a708f8fSGustavo F. Padovan 
19670a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
19680a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_STREAMING;
19690a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
19700a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
19710a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
19720a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
19730a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
19748c1d787bSGustavo F. Padovan 		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
19758c1d787bSGustavo F. Padovan 			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
19760a708f8fSGustavo F. Padovan 
19770a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
19780a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
19790a708f8fSGustavo F. Padovan 
19808c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
19810a708f8fSGustavo F. Padovan 			break;
19820a708f8fSGustavo F. Padovan 
198347d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
1984b4450035SGustavo F. Padovan 				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
198547d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
198647d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
19870a708f8fSGustavo F. Padovan 		}
19880a708f8fSGustavo F. Padovan 		break;
19890a708f8fSGustavo F. Padovan 	}
19900a708f8fSGustavo F. Padovan 
1991fe4128e0SGustavo F. Padovan 	req->dcid  = cpu_to_le16(chan->dcid);
19920a708f8fSGustavo F. Padovan 	req->flags = cpu_to_le16(0);
19930a708f8fSGustavo F. Padovan 
19940a708f8fSGustavo F. Padovan 	return ptr - data;
19950a708f8fSGustavo F. Padovan }
19960a708f8fSGustavo F. Padovan 
199773ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
19980a708f8fSGustavo F. Padovan {
19990a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
20000a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
200173ffa904SGustavo F. Padovan 	void *req = chan->conf_req;
200273ffa904SGustavo F. Padovan 	int len = chan->conf_len;
20030a708f8fSGustavo F. Padovan 	int type, hint, olen;
20040a708f8fSGustavo F. Padovan 	unsigned long val;
20050a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
20060a708f8fSGustavo F. Padovan 	u16 mtu = L2CAP_DEFAULT_MTU;
20070a708f8fSGustavo F. Padovan 	u16 result = L2CAP_CONF_SUCCESS;
20080a708f8fSGustavo F. Padovan 
200973ffa904SGustavo F. Padovan 	BT_DBG("chan %p", chan);
20100a708f8fSGustavo F. Padovan 
20110a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
20120a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
20130a708f8fSGustavo F. Padovan 
20140a708f8fSGustavo F. Padovan 		hint  = type & L2CAP_CONF_HINT;
20150a708f8fSGustavo F. Padovan 		type &= L2CAP_CONF_MASK;
20160a708f8fSGustavo F. Padovan 
20170a708f8fSGustavo F. Padovan 		switch (type) {
20180a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
20190a708f8fSGustavo F. Padovan 			mtu = val;
20200a708f8fSGustavo F. Padovan 			break;
20210a708f8fSGustavo F. Padovan 
20220a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
20230c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
20240a708f8fSGustavo F. Padovan 			break;
20250a708f8fSGustavo F. Padovan 
20260a708f8fSGustavo F. Padovan 		case L2CAP_CONF_QOS:
20270a708f8fSGustavo F. Padovan 			break;
20280a708f8fSGustavo F. Padovan 
20290a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
20300a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
20310a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *) val, olen);
20320a708f8fSGustavo F. Padovan 			break;
20330a708f8fSGustavo F. Padovan 
20340a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FCS:
20350a708f8fSGustavo F. Padovan 			if (val == L2CAP_FCS_NONE)
2036b4450035SGustavo F. Padovan 				chan->conf_state |= L2CAP_CONF_NO_FCS_RECV;
20370a708f8fSGustavo F. Padovan 
20380a708f8fSGustavo F. Padovan 			break;
20390a708f8fSGustavo F. Padovan 
20400a708f8fSGustavo F. Padovan 		default:
20410a708f8fSGustavo F. Padovan 			if (hint)
20420a708f8fSGustavo F. Padovan 				break;
20430a708f8fSGustavo F. Padovan 
20440a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNKNOWN;
20450a708f8fSGustavo F. Padovan 			*((u8 *) ptr++) = type;
20460a708f8fSGustavo F. Padovan 			break;
20470a708f8fSGustavo F. Padovan 		}
20480a708f8fSGustavo F. Padovan 	}
20490a708f8fSGustavo F. Padovan 
205073ffa904SGustavo F. Padovan 	if (chan->num_conf_rsp || chan->num_conf_req > 1)
20510a708f8fSGustavo F. Padovan 		goto done;
20520a708f8fSGustavo F. Padovan 
20530c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
20540a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
20550a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
2056b4450035SGustavo F. Padovan 		if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) {
20570c1bc5c6SGustavo F. Padovan 			chan->mode = l2cap_select_mode(rfc.mode,
20588c1d787bSGustavo F. Padovan 					chan->conn->feat_mask);
20590a708f8fSGustavo F. Padovan 			break;
20600a708f8fSGustavo F. Padovan 		}
20610a708f8fSGustavo F. Padovan 
20620c1bc5c6SGustavo F. Padovan 		if (chan->mode != rfc.mode)
20630a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
20640a708f8fSGustavo F. Padovan 
20650a708f8fSGustavo F. Padovan 		break;
20660a708f8fSGustavo F. Padovan 	}
20670a708f8fSGustavo F. Padovan 
20680a708f8fSGustavo F. Padovan done:
20690c1bc5c6SGustavo F. Padovan 	if (chan->mode != rfc.mode) {
20700a708f8fSGustavo F. Padovan 		result = L2CAP_CONF_UNACCEPT;
20710c1bc5c6SGustavo F. Padovan 		rfc.mode = chan->mode;
20720a708f8fSGustavo F. Padovan 
207373ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp == 1)
20740a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
20750a708f8fSGustavo F. Padovan 
20760a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
20770a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
20780a708f8fSGustavo F. Padovan 	}
20790a708f8fSGustavo F. Padovan 
20800a708f8fSGustavo F. Padovan 
20810a708f8fSGustavo F. Padovan 	if (result == L2CAP_CONF_SUCCESS) {
20820a708f8fSGustavo F. Padovan 		/* Configure output options and let the other side know
20830a708f8fSGustavo F. Padovan 		 * which ones we don't like. */
20840a708f8fSGustavo F. Padovan 
20850a708f8fSGustavo F. Padovan 		if (mtu < L2CAP_DEFAULT_MIN_MTU)
20860a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
20870a708f8fSGustavo F. Padovan 		else {
20880c1bc5c6SGustavo F. Padovan 			chan->omtu = mtu;
2089b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MTU_DONE;
20900a708f8fSGustavo F. Padovan 		}
20910c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
20920a708f8fSGustavo F. Padovan 
20930a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
20940a708f8fSGustavo F. Padovan 		case L2CAP_MODE_BASIC:
209547d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
2096b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MODE_DONE;
20970a708f8fSGustavo F. Padovan 			break;
20980a708f8fSGustavo F. Padovan 
20990a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
21002c03a7a4SGustavo F. Padovan 			chan->remote_tx_win = rfc.txwin_size;
21012c03a7a4SGustavo F. Padovan 			chan->remote_max_tx = rfc.max_transmit;
21020a708f8fSGustavo F. Padovan 
21038c1d787bSGustavo F. Padovan 			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
21048c1d787bSGustavo F. Padovan 				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
21050a708f8fSGustavo F. Padovan 
21062c03a7a4SGustavo F. Padovan 			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
21070a708f8fSGustavo F. Padovan 
21080a708f8fSGustavo F. Padovan 			rfc.retrans_timeout =
21090a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
21100a708f8fSGustavo F. Padovan 			rfc.monitor_timeout =
21110a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
21120a708f8fSGustavo F. Padovan 
2113b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MODE_DONE;
21140a708f8fSGustavo F. Padovan 
21150a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
21160a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
21170a708f8fSGustavo F. Padovan 
21180a708f8fSGustavo F. Padovan 			break;
21190a708f8fSGustavo F. Padovan 
21200a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
21218c1d787bSGustavo F. Padovan 			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
21228c1d787bSGustavo F. Padovan 				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
21230a708f8fSGustavo F. Padovan 
21242c03a7a4SGustavo F. Padovan 			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
21250a708f8fSGustavo F. Padovan 
2126b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MODE_DONE;
21270a708f8fSGustavo F. Padovan 
21280a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
21290a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
21300a708f8fSGustavo F. Padovan 
21310a708f8fSGustavo F. Padovan 			break;
21320a708f8fSGustavo F. Padovan 
21330a708f8fSGustavo F. Padovan 		default:
21340a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
21350a708f8fSGustavo F. Padovan 
21360a708f8fSGustavo F. Padovan 			memset(&rfc, 0, sizeof(rfc));
21370c1bc5c6SGustavo F. Padovan 			rfc.mode = chan->mode;
21380a708f8fSGustavo F. Padovan 		}
21390a708f8fSGustavo F. Padovan 
21400a708f8fSGustavo F. Padovan 		if (result == L2CAP_CONF_SUCCESS)
2141b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_OUTPUT_DONE;
21420a708f8fSGustavo F. Padovan 	}
2143fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
21440a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
21450a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(0x0000);
21460a708f8fSGustavo F. Padovan 
21470a708f8fSGustavo F. Padovan 	return ptr - data;
21480a708f8fSGustavo F. Padovan }
21490a708f8fSGustavo F. Padovan 
2150b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
21510a708f8fSGustavo F. Padovan {
21520a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
21530a708f8fSGustavo F. Padovan 	void *ptr = req->data;
21540a708f8fSGustavo F. Padovan 	int type, olen;
21550a708f8fSGustavo F. Padovan 	unsigned long val;
21560a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
21570a708f8fSGustavo F. Padovan 
2158fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
21590a708f8fSGustavo F. Padovan 
21600a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
21610a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
21620a708f8fSGustavo F. Padovan 
21630a708f8fSGustavo F. Padovan 		switch (type) {
21640a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
21650a708f8fSGustavo F. Padovan 			if (val < L2CAP_DEFAULT_MIN_MTU) {
21660a708f8fSGustavo F. Padovan 				*result = L2CAP_CONF_UNACCEPT;
21670c1bc5c6SGustavo F. Padovan 				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
21680a708f8fSGustavo F. Padovan 			} else
21690c1bc5c6SGustavo F. Padovan 				chan->imtu = val;
21700c1bc5c6SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
21710a708f8fSGustavo F. Padovan 			break;
21720a708f8fSGustavo F. Padovan 
21730a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
21740c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
21750a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
21760c1bc5c6SGustavo F. Padovan 							2, chan->flush_to);
21770a708f8fSGustavo F. Padovan 			break;
21780a708f8fSGustavo F. Padovan 
21790a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
21800a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
21810a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
21820a708f8fSGustavo F. Padovan 
2183b4450035SGustavo F. Padovan 			if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) &&
21840c1bc5c6SGustavo F. Padovan 							rfc.mode != chan->mode)
21850a708f8fSGustavo F. Padovan 				return -ECONNREFUSED;
21860a708f8fSGustavo F. Padovan 
218747d1ec61SGustavo F. Padovan 			chan->fcs = 0;
21880a708f8fSGustavo F. Padovan 
21890a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
21900a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
21910a708f8fSGustavo F. Padovan 			break;
21920a708f8fSGustavo F. Padovan 		}
21930a708f8fSGustavo F. Padovan 	}
21940a708f8fSGustavo F. Padovan 
21950c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
21960a708f8fSGustavo F. Padovan 		return -ECONNREFUSED;
21970a708f8fSGustavo F. Padovan 
21980c1bc5c6SGustavo F. Padovan 	chan->mode = rfc.mode;
21990a708f8fSGustavo F. Padovan 
22000a708f8fSGustavo F. Padovan 	if (*result == L2CAP_CONF_SUCCESS) {
22010a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
22020a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
220347d1ec61SGustavo F. Padovan 			chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
220447d1ec61SGustavo F. Padovan 			chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
220547d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
22060a708f8fSGustavo F. Padovan 			break;
22070a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
220847d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
22090a708f8fSGustavo F. Padovan 		}
22100a708f8fSGustavo F. Padovan 	}
22110a708f8fSGustavo F. Padovan 
2212fe4128e0SGustavo F. Padovan 	req->dcid   = cpu_to_le16(chan->dcid);
22130a708f8fSGustavo F. Padovan 	req->flags  = cpu_to_le16(0x0000);
22140a708f8fSGustavo F. Padovan 
22150a708f8fSGustavo F. Padovan 	return ptr - data;
22160a708f8fSGustavo F. Padovan }
22170a708f8fSGustavo F. Padovan 
2218fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
22190a708f8fSGustavo F. Padovan {
22200a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
22210a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
22220a708f8fSGustavo F. Padovan 
2223fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p", chan);
22240a708f8fSGustavo F. Padovan 
2225fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
22260a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
22270a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(flags);
22280a708f8fSGustavo F. Padovan 
22290a708f8fSGustavo F. Padovan 	return ptr - data;
22300a708f8fSGustavo F. Padovan }
22310a708f8fSGustavo F. Padovan 
22328c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
2233710f9b0aSGustavo F. Padovan {
2234710f9b0aSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
22358c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
2236710f9b0aSGustavo F. Padovan 	u8 buf[128];
2237710f9b0aSGustavo F. Padovan 
2238fe4128e0SGustavo F. Padovan 	rsp.scid   = cpu_to_le16(chan->dcid);
2239fe4128e0SGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(chan->scid);
2240710f9b0aSGustavo F. Padovan 	rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
2241710f9b0aSGustavo F. Padovan 	rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
2242710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, chan->ident,
2243710f9b0aSGustavo F. Padovan 				L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2244710f9b0aSGustavo F. Padovan 
2245b4450035SGustavo F. Padovan 	if (chan->conf_state & L2CAP_CONF_REQ_SENT)
2246710f9b0aSGustavo F. Padovan 		return;
2247710f9b0aSGustavo F. Padovan 
2248b4450035SGustavo F. Padovan 	chan->conf_state |= L2CAP_CONF_REQ_SENT;
2249710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2250710f9b0aSGustavo F. Padovan 			l2cap_build_conf_req(chan, buf), buf);
2251710f9b0aSGustavo F. Padovan 	chan->num_conf_req++;
2252710f9b0aSGustavo F. Padovan }
2253710f9b0aSGustavo F. Padovan 
225447d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
22550a708f8fSGustavo F. Padovan {
22560a708f8fSGustavo F. Padovan 	int type, olen;
22570a708f8fSGustavo F. Padovan 	unsigned long val;
22580a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
22590a708f8fSGustavo F. Padovan 
226047d1ec61SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
22610a708f8fSGustavo F. Padovan 
22620c1bc5c6SGustavo F. Padovan 	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
22630a708f8fSGustavo F. Padovan 		return;
22640a708f8fSGustavo F. Padovan 
22650a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
22660a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
22670a708f8fSGustavo F. Padovan 
22680a708f8fSGustavo F. Padovan 		switch (type) {
22690a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
22700a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
22710a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
22720a708f8fSGustavo F. Padovan 			goto done;
22730a708f8fSGustavo F. Padovan 		}
22740a708f8fSGustavo F. Padovan 	}
22750a708f8fSGustavo F. Padovan 
22760a708f8fSGustavo F. Padovan done:
22770a708f8fSGustavo F. Padovan 	switch (rfc.mode) {
22780a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
227947d1ec61SGustavo F. Padovan 		chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
228047d1ec61SGustavo F. Padovan 		chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
228147d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
22820a708f8fSGustavo F. Padovan 		break;
22830a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
228447d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
22850a708f8fSGustavo F. Padovan 	}
22860a708f8fSGustavo F. Padovan }
22870a708f8fSGustavo F. Padovan 
22880a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
22890a708f8fSGustavo F. Padovan {
22900a708f8fSGustavo F. Padovan 	struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data;
22910a708f8fSGustavo F. Padovan 
22920a708f8fSGustavo F. Padovan 	if (rej->reason != 0x0000)
22930a708f8fSGustavo F. Padovan 		return 0;
22940a708f8fSGustavo F. Padovan 
22950a708f8fSGustavo F. Padovan 	if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
22960a708f8fSGustavo F. Padovan 					cmd->ident == conn->info_ident) {
22970a708f8fSGustavo F. Padovan 		del_timer(&conn->info_timer);
22980a708f8fSGustavo F. Padovan 
22990a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
23000a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
23010a708f8fSGustavo F. Padovan 
23020a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
23030a708f8fSGustavo F. Padovan 	}
23040a708f8fSGustavo F. Padovan 
23050a708f8fSGustavo F. Padovan 	return 0;
23060a708f8fSGustavo F. Padovan }
23070a708f8fSGustavo F. Padovan 
23080a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
23090a708f8fSGustavo F. Padovan {
23100a708f8fSGustavo F. Padovan 	struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
23110a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
231223691d75SGustavo F. Padovan 	struct l2cap_chan *chan = NULL, *pchan;
23130a708f8fSGustavo F. Padovan 	struct sock *parent, *sk = NULL;
23140a708f8fSGustavo F. Padovan 	int result, status = L2CAP_CS_NO_INFO;
23150a708f8fSGustavo F. Padovan 
23160a708f8fSGustavo F. Padovan 	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
23170a708f8fSGustavo F. Padovan 	__le16 psm = req->psm;
23180a708f8fSGustavo F. Padovan 
23190a708f8fSGustavo F. Padovan 	BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
23200a708f8fSGustavo F. Padovan 
23210a708f8fSGustavo F. Padovan 	/* Check if we have socket listening on psm */
232223691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src);
232323691d75SGustavo F. Padovan 	if (!pchan) {
23240a708f8fSGustavo F. Padovan 		result = L2CAP_CR_BAD_PSM;
23250a708f8fSGustavo F. Padovan 		goto sendresp;
23260a708f8fSGustavo F. Padovan 	}
23270a708f8fSGustavo F. Padovan 
232823691d75SGustavo F. Padovan 	parent = pchan->sk;
232923691d75SGustavo F. Padovan 
23300a708f8fSGustavo F. Padovan 	bh_lock_sock(parent);
23310a708f8fSGustavo F. Padovan 
23320a708f8fSGustavo F. Padovan 	/* Check if the ACL is secure enough (if not SDP) */
23330a708f8fSGustavo F. Padovan 	if (psm != cpu_to_le16(0x0001) &&
23340a708f8fSGustavo F. Padovan 				!hci_conn_check_link_mode(conn->hcon)) {
23350a708f8fSGustavo F. Padovan 		conn->disc_reason = 0x05;
23360a708f8fSGustavo F. Padovan 		result = L2CAP_CR_SEC_BLOCK;
23370a708f8fSGustavo F. Padovan 		goto response;
23380a708f8fSGustavo F. Padovan 	}
23390a708f8fSGustavo F. Padovan 
23400a708f8fSGustavo F. Padovan 	result = L2CAP_CR_NO_MEM;
23410a708f8fSGustavo F. Padovan 
23420a708f8fSGustavo F. Padovan 	/* Check for backlog size */
23430a708f8fSGustavo F. Padovan 	if (sk_acceptq_is_full(parent)) {
23440a708f8fSGustavo F. Padovan 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
23450a708f8fSGustavo F. Padovan 		goto response;
23460a708f8fSGustavo F. Padovan 	}
23470a708f8fSGustavo F. Padovan 
234880808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
234980808e43SGustavo F. Padovan 	if (!chan)
23500a708f8fSGustavo F. Padovan 		goto response;
23510a708f8fSGustavo F. Padovan 
235280808e43SGustavo F. Padovan 	sk = chan->sk;
235380808e43SGustavo F. Padovan 
2354baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
23550a708f8fSGustavo F. Padovan 
23560a708f8fSGustavo F. Padovan 	/* Check if we already have channel with that dcid */
2357baa7e1faSGustavo F. Padovan 	if (__l2cap_get_chan_by_dcid(conn, scid)) {
2358baa7e1faSGustavo F. Padovan 		write_unlock_bh(&conn->chan_lock);
23590a708f8fSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
2360ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
23610a708f8fSGustavo F. Padovan 		goto response;
23620a708f8fSGustavo F. Padovan 	}
23630a708f8fSGustavo F. Padovan 
23640a708f8fSGustavo F. Padovan 	hci_conn_hold(conn->hcon);
23650a708f8fSGustavo F. Padovan 
23660a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->src, conn->src);
23670a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->dst, conn->dst);
2368fe4128e0SGustavo F. Padovan 	chan->psm  = psm;
2369fe4128e0SGustavo F. Padovan 	chan->dcid = scid;
23700a708f8fSGustavo F. Padovan 
2371d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
2372d1010240SGustavo F. Padovan 
237348454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
237448454079SGustavo F. Padovan 
2375fe4128e0SGustavo F. Padovan 	dcid = chan->scid;
23760a708f8fSGustavo F. Padovan 
2377ab07801dSGustavo F. Padovan 	l2cap_chan_set_timer(chan, sk->sk_sndtimeo);
23780a708f8fSGustavo F. Padovan 
2379fc7f8a7eSGustavo F. Padovan 	chan->ident = cmd->ident;
23800a708f8fSGustavo F. Padovan 
23810a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
23824343478fSGustavo F. Padovan 		if (l2cap_check_security(chan)) {
23830a708f8fSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup) {
238489bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONNECT2);
23850a708f8fSGustavo F. Padovan 				result = L2CAP_CR_PEND;
23860a708f8fSGustavo F. Padovan 				status = L2CAP_CS_AUTHOR_PEND;
23870a708f8fSGustavo F. Padovan 				parent->sk_data_ready(parent, 0);
23880a708f8fSGustavo F. Padovan 			} else {
238989bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONFIG);
23900a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SUCCESS;
23910a708f8fSGustavo F. Padovan 				status = L2CAP_CS_NO_INFO;
23920a708f8fSGustavo F. Padovan 			}
23930a708f8fSGustavo F. Padovan 		} else {
239489bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_CONNECT2);
23950a708f8fSGustavo F. Padovan 			result = L2CAP_CR_PEND;
23960a708f8fSGustavo F. Padovan 			status = L2CAP_CS_AUTHEN_PEND;
23970a708f8fSGustavo F. Padovan 		}
23980a708f8fSGustavo F. Padovan 	} else {
239989bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECT2);
24000a708f8fSGustavo F. Padovan 		result = L2CAP_CR_PEND;
24010a708f8fSGustavo F. Padovan 		status = L2CAP_CS_NO_INFO;
24020a708f8fSGustavo F. Padovan 	}
24030a708f8fSGustavo F. Padovan 
2404baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
24050a708f8fSGustavo F. Padovan 
24060a708f8fSGustavo F. Padovan response:
24070a708f8fSGustavo F. Padovan 	bh_unlock_sock(parent);
24080a708f8fSGustavo F. Padovan 
24090a708f8fSGustavo F. Padovan sendresp:
24100a708f8fSGustavo F. Padovan 	rsp.scid   = cpu_to_le16(scid);
24110a708f8fSGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(dcid);
24120a708f8fSGustavo F. Padovan 	rsp.result = cpu_to_le16(result);
24130a708f8fSGustavo F. Padovan 	rsp.status = cpu_to_le16(status);
24140a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
24150a708f8fSGustavo F. Padovan 
24160a708f8fSGustavo F. Padovan 	if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
24170a708f8fSGustavo F. Padovan 		struct l2cap_info_req info;
24180a708f8fSGustavo F. Padovan 		info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
24190a708f8fSGustavo F. Padovan 
24200a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
24210a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
24220a708f8fSGustavo F. Padovan 
24230a708f8fSGustavo F. Padovan 		mod_timer(&conn->info_timer, jiffies +
24240a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
24250a708f8fSGustavo F. Padovan 
24260a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
24270a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(info), &info);
24280a708f8fSGustavo F. Padovan 	}
24290a708f8fSGustavo F. Padovan 
2430b4450035SGustavo F. Padovan 	if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) &&
24310a708f8fSGustavo F. Padovan 				result == L2CAP_CR_SUCCESS) {
24320a708f8fSGustavo F. Padovan 		u8 buf[128];
2433b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_REQ_SENT;
24340a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
243573ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
243673ffa904SGustavo F. Padovan 		chan->num_conf_req++;
24370a708f8fSGustavo F. Padovan 	}
24380a708f8fSGustavo F. Padovan 
24390a708f8fSGustavo F. Padovan 	return 0;
24400a708f8fSGustavo F. Padovan }
24410a708f8fSGustavo F. Padovan 
24420a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
24430a708f8fSGustavo F. Padovan {
24440a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
24450a708f8fSGustavo F. Padovan 	u16 scid, dcid, result, status;
244648454079SGustavo F. Padovan 	struct l2cap_chan *chan;
24470a708f8fSGustavo F. Padovan 	struct sock *sk;
24480a708f8fSGustavo F. Padovan 	u8 req[128];
24490a708f8fSGustavo F. Padovan 
24500a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
24510a708f8fSGustavo F. Padovan 	dcid   = __le16_to_cpu(rsp->dcid);
24520a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
24530a708f8fSGustavo F. Padovan 	status = __le16_to_cpu(rsp->status);
24540a708f8fSGustavo F. Padovan 
24550a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
24560a708f8fSGustavo F. Padovan 
24570a708f8fSGustavo F. Padovan 	if (scid) {
2458baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, scid);
245948454079SGustavo F. Padovan 		if (!chan)
24600a708f8fSGustavo F. Padovan 			return -EFAULT;
24610a708f8fSGustavo F. Padovan 	} else {
2462baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
246348454079SGustavo F. Padovan 		if (!chan)
24640a708f8fSGustavo F. Padovan 			return -EFAULT;
24650a708f8fSGustavo F. Padovan 	}
24660a708f8fSGustavo F. Padovan 
246748454079SGustavo F. Padovan 	sk = chan->sk;
246848454079SGustavo F. Padovan 
24690a708f8fSGustavo F. Padovan 	switch (result) {
24700a708f8fSGustavo F. Padovan 	case L2CAP_CR_SUCCESS:
247189bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONFIG);
2472fc7f8a7eSGustavo F. Padovan 		chan->ident = 0;
2473fe4128e0SGustavo F. Padovan 		chan->dcid = dcid;
2474b4450035SGustavo F. Padovan 		chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND;
24750a708f8fSGustavo F. Padovan 
2476b4450035SGustavo F. Padovan 		if (chan->conf_state & L2CAP_CONF_REQ_SENT)
24770a708f8fSGustavo F. Padovan 			break;
24780a708f8fSGustavo F. Padovan 
2479b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_REQ_SENT;
24800a708f8fSGustavo F. Padovan 
24810a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
248273ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, req), req);
248373ffa904SGustavo F. Padovan 		chan->num_conf_req++;
24840a708f8fSGustavo F. Padovan 		break;
24850a708f8fSGustavo F. Padovan 
24860a708f8fSGustavo F. Padovan 	case L2CAP_CR_PEND:
2487b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
24880a708f8fSGustavo F. Padovan 		break;
24890a708f8fSGustavo F. Padovan 
24900a708f8fSGustavo F. Padovan 	default:
24910a708f8fSGustavo F. Padovan 		/* don't delete l2cap channel if sk is owned by user */
24920a708f8fSGustavo F. Padovan 		if (sock_owned_by_user(sk)) {
249389bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_DISCONN);
2494ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
2495ab07801dSGustavo F. Padovan 			l2cap_chan_set_timer(chan, HZ / 5);
24960a708f8fSGustavo F. Padovan 			break;
24970a708f8fSGustavo F. Padovan 		}
24980a708f8fSGustavo F. Padovan 
249948454079SGustavo F. Padovan 		l2cap_chan_del(chan, ECONNREFUSED);
25000a708f8fSGustavo F. Padovan 		break;
25010a708f8fSGustavo F. Padovan 	}
25020a708f8fSGustavo F. Padovan 
25030a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
25040a708f8fSGustavo F. Padovan 	return 0;
25050a708f8fSGustavo F. Padovan }
25060a708f8fSGustavo F. Padovan 
250747d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan)
25080a708f8fSGustavo F. Padovan {
250947d1ec61SGustavo F. Padovan 	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
251047d1ec61SGustavo F. Padovan 
25110a708f8fSGustavo F. Padovan 	/* FCS is enabled only in ERTM or streaming mode, if one or both
25120a708f8fSGustavo F. Padovan 	 * sides request it.
25130a708f8fSGustavo F. Padovan 	 */
25140c1bc5c6SGustavo F. Padovan 	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
251547d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_NONE;
2516b4450035SGustavo F. Padovan 	else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV))
251747d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_CRC16;
25180a708f8fSGustavo F. Padovan }
25190a708f8fSGustavo F. Padovan 
25200a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
25210a708f8fSGustavo F. Padovan {
25220a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
25230a708f8fSGustavo F. Padovan 	u16 dcid, flags;
25240a708f8fSGustavo F. Padovan 	u8 rsp[64];
252548454079SGustavo F. Padovan 	struct l2cap_chan *chan;
25260a708f8fSGustavo F. Padovan 	struct sock *sk;
25270a708f8fSGustavo F. Padovan 	int len;
25280a708f8fSGustavo F. Padovan 
25290a708f8fSGustavo F. Padovan 	dcid  = __le16_to_cpu(req->dcid);
25300a708f8fSGustavo F. Padovan 	flags = __le16_to_cpu(req->flags);
25310a708f8fSGustavo F. Padovan 
25320a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
25330a708f8fSGustavo F. Padovan 
2534baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
253548454079SGustavo F. Padovan 	if (!chan)
25360a708f8fSGustavo F. Padovan 		return -ENOENT;
25370a708f8fSGustavo F. Padovan 
253848454079SGustavo F. Padovan 	sk = chan->sk;
253948454079SGustavo F. Padovan 
254089bc500eSGustavo F. Padovan 	if (chan->state != BT_CONFIG) {
25410a708f8fSGustavo F. Padovan 		struct l2cap_cmd_rej rej;
25420a708f8fSGustavo F. Padovan 
25430a708f8fSGustavo F. Padovan 		rej.reason = cpu_to_le16(0x0002);
25440a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
25450a708f8fSGustavo F. Padovan 				sizeof(rej), &rej);
25460a708f8fSGustavo F. Padovan 		goto unlock;
25470a708f8fSGustavo F. Padovan 	}
25480a708f8fSGustavo F. Padovan 
25490a708f8fSGustavo F. Padovan 	/* Reject if config buffer is too small. */
25500a708f8fSGustavo F. Padovan 	len = cmd_len - sizeof(*req);
255173ffa904SGustavo F. Padovan 	if (chan->conf_len + len > sizeof(chan->conf_req)) {
25520a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2553fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
25540a708f8fSGustavo F. Padovan 					L2CAP_CONF_REJECT, flags), rsp);
25550a708f8fSGustavo F. Padovan 		goto unlock;
25560a708f8fSGustavo F. Padovan 	}
25570a708f8fSGustavo F. Padovan 
25580a708f8fSGustavo F. Padovan 	/* Store config. */
255973ffa904SGustavo F. Padovan 	memcpy(chan->conf_req + chan->conf_len, req->data, len);
256073ffa904SGustavo F. Padovan 	chan->conf_len += len;
25610a708f8fSGustavo F. Padovan 
25620a708f8fSGustavo F. Padovan 	if (flags & 0x0001) {
25630a708f8fSGustavo F. Padovan 		/* Incomplete config. Send empty response. */
25640a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2565fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
25660a708f8fSGustavo F. Padovan 					L2CAP_CONF_SUCCESS, 0x0001), rsp);
25670a708f8fSGustavo F. Padovan 		goto unlock;
25680a708f8fSGustavo F. Padovan 	}
25690a708f8fSGustavo F. Padovan 
25700a708f8fSGustavo F. Padovan 	/* Complete config. */
257173ffa904SGustavo F. Padovan 	len = l2cap_parse_conf_req(chan, rsp);
25720a708f8fSGustavo F. Padovan 	if (len < 0) {
2573e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
25740a708f8fSGustavo F. Padovan 		goto unlock;
25750a708f8fSGustavo F. Padovan 	}
25760a708f8fSGustavo F. Padovan 
25770a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
257873ffa904SGustavo F. Padovan 	chan->num_conf_rsp++;
25790a708f8fSGustavo F. Padovan 
25800a708f8fSGustavo F. Padovan 	/* Reset config buffer. */
258173ffa904SGustavo F. Padovan 	chan->conf_len = 0;
25820a708f8fSGustavo F. Padovan 
2583b4450035SGustavo F. Padovan 	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE))
25840a708f8fSGustavo F. Padovan 		goto unlock;
25850a708f8fSGustavo F. Padovan 
2586b4450035SGustavo F. Padovan 	if (chan->conf_state & L2CAP_CONF_INPUT_DONE) {
258747d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
25880a708f8fSGustavo F. Padovan 
258989bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
25900a708f8fSGustavo F. Padovan 
259142e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
259242e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
259358d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
25940c1bc5c6SGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_ERTM)
2595525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
25960a708f8fSGustavo F. Padovan 
25970a708f8fSGustavo F. Padovan 		l2cap_chan_ready(sk);
25980a708f8fSGustavo F. Padovan 		goto unlock;
25990a708f8fSGustavo F. Padovan 	}
26000a708f8fSGustavo F. Padovan 
2601b4450035SGustavo F. Padovan 	if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) {
26020a708f8fSGustavo F. Padovan 		u8 buf[64];
2603b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_REQ_SENT;
26040a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
260573ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
260673ffa904SGustavo F. Padovan 		chan->num_conf_req++;
26070a708f8fSGustavo F. Padovan 	}
26080a708f8fSGustavo F. Padovan 
26090a708f8fSGustavo F. Padovan unlock:
26100a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
26110a708f8fSGustavo F. Padovan 	return 0;
26120a708f8fSGustavo F. Padovan }
26130a708f8fSGustavo F. Padovan 
26140a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
26150a708f8fSGustavo F. Padovan {
26160a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
26170a708f8fSGustavo F. Padovan 	u16 scid, flags, result;
261848454079SGustavo F. Padovan 	struct l2cap_chan *chan;
26190a708f8fSGustavo F. Padovan 	struct sock *sk;
26200a708f8fSGustavo F. Padovan 	int len = cmd->len - sizeof(*rsp);
26210a708f8fSGustavo F. Padovan 
26220a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
26230a708f8fSGustavo F. Padovan 	flags  = __le16_to_cpu(rsp->flags);
26240a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
26250a708f8fSGustavo F. Padovan 
26260a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
26270a708f8fSGustavo F. Padovan 			scid, flags, result);
26280a708f8fSGustavo F. Padovan 
2629baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
263048454079SGustavo F. Padovan 	if (!chan)
26310a708f8fSGustavo F. Padovan 		return 0;
26320a708f8fSGustavo F. Padovan 
263348454079SGustavo F. Padovan 	sk = chan->sk;
263448454079SGustavo F. Padovan 
26350a708f8fSGustavo F. Padovan 	switch (result) {
26360a708f8fSGustavo F. Padovan 	case L2CAP_CONF_SUCCESS:
263747d1ec61SGustavo F. Padovan 		l2cap_conf_rfc_get(chan, rsp->data, len);
26380a708f8fSGustavo F. Padovan 		break;
26390a708f8fSGustavo F. Padovan 
26400a708f8fSGustavo F. Padovan 	case L2CAP_CONF_UNACCEPT:
264173ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
26420a708f8fSGustavo F. Padovan 			char req[64];
26430a708f8fSGustavo F. Padovan 
26440a708f8fSGustavo F. Padovan 			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2645e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
26460a708f8fSGustavo F. Padovan 				goto done;
26470a708f8fSGustavo F. Padovan 			}
26480a708f8fSGustavo F. Padovan 
26490a708f8fSGustavo F. Padovan 			/* throw out any old stored conf requests */
26500a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_SUCCESS;
2651b4450035SGustavo F. Padovan 			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
2652b4450035SGustavo F. Padovan 								req, &result);
26530a708f8fSGustavo F. Padovan 			if (len < 0) {
2654e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
26550a708f8fSGustavo F. Padovan 				goto done;
26560a708f8fSGustavo F. Padovan 			}
26570a708f8fSGustavo F. Padovan 
26580a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn),
26590a708f8fSGustavo F. Padovan 						L2CAP_CONF_REQ, len, req);
266073ffa904SGustavo F. Padovan 			chan->num_conf_req++;
26610a708f8fSGustavo F. Padovan 			if (result != L2CAP_CONF_SUCCESS)
26620a708f8fSGustavo F. Padovan 				goto done;
26630a708f8fSGustavo F. Padovan 			break;
26640a708f8fSGustavo F. Padovan 		}
26650a708f8fSGustavo F. Padovan 
26660a708f8fSGustavo F. Padovan 	default:
26670a708f8fSGustavo F. Padovan 		sk->sk_err = ECONNRESET;
2668ab07801dSGustavo F. Padovan 		l2cap_chan_set_timer(chan, HZ * 5);
2669e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
26700a708f8fSGustavo F. Padovan 		goto done;
26710a708f8fSGustavo F. Padovan 	}
26720a708f8fSGustavo F. Padovan 
26730a708f8fSGustavo F. Padovan 	if (flags & 0x01)
26740a708f8fSGustavo F. Padovan 		goto done;
26750a708f8fSGustavo F. Padovan 
2676b4450035SGustavo F. Padovan 	chan->conf_state |= L2CAP_CONF_INPUT_DONE;
26770a708f8fSGustavo F. Padovan 
2678b4450035SGustavo F. Padovan 	if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) {
267947d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
26800a708f8fSGustavo F. Padovan 
268189bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
268242e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
268342e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
268458d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
26850c1bc5c6SGustavo F. Padovan 		if (chan->mode ==  L2CAP_MODE_ERTM)
2686525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
26870a708f8fSGustavo F. Padovan 
26880a708f8fSGustavo F. Padovan 		l2cap_chan_ready(sk);
26890a708f8fSGustavo F. Padovan 	}
26900a708f8fSGustavo F. Padovan 
26910a708f8fSGustavo F. Padovan done:
26920a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
26930a708f8fSGustavo F. Padovan 	return 0;
26940a708f8fSGustavo F. Padovan }
26950a708f8fSGustavo F. Padovan 
26960a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
26970a708f8fSGustavo F. Padovan {
26980a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
26990a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp rsp;
27000a708f8fSGustavo F. Padovan 	u16 dcid, scid;
270148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
27020a708f8fSGustavo F. Padovan 	struct sock *sk;
27030a708f8fSGustavo F. Padovan 
27040a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(req->scid);
27050a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(req->dcid);
27060a708f8fSGustavo F. Padovan 
27070a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
27080a708f8fSGustavo F. Padovan 
2709baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
271048454079SGustavo F. Padovan 	if (!chan)
27110a708f8fSGustavo F. Padovan 		return 0;
27120a708f8fSGustavo F. Padovan 
271348454079SGustavo F. Padovan 	sk = chan->sk;
271448454079SGustavo F. Padovan 
2715fe4128e0SGustavo F. Padovan 	rsp.dcid = cpu_to_le16(chan->scid);
2716fe4128e0SGustavo F. Padovan 	rsp.scid = cpu_to_le16(chan->dcid);
27170a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
27180a708f8fSGustavo F. Padovan 
27190a708f8fSGustavo F. Padovan 	sk->sk_shutdown = SHUTDOWN_MASK;
27200a708f8fSGustavo F. Padovan 
27210a708f8fSGustavo F. Padovan 	/* don't delete l2cap channel if sk is owned by user */
27220a708f8fSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
272389bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_DISCONN);
2724ab07801dSGustavo F. Padovan 		l2cap_chan_clear_timer(chan);
2725ab07801dSGustavo F. Padovan 		l2cap_chan_set_timer(chan, HZ / 5);
27260a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
27270a708f8fSGustavo F. Padovan 		return 0;
27280a708f8fSGustavo F. Padovan 	}
27290a708f8fSGustavo F. Padovan 
273048454079SGustavo F. Padovan 	l2cap_chan_del(chan, ECONNRESET);
27310a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
27320a708f8fSGustavo F. Padovan 
2733ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
27340a708f8fSGustavo F. Padovan 	return 0;
27350a708f8fSGustavo F. Padovan }
27360a708f8fSGustavo F. Padovan 
27370a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
27380a708f8fSGustavo F. Padovan {
27390a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
27400a708f8fSGustavo F. Padovan 	u16 dcid, scid;
274148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
27420a708f8fSGustavo F. Padovan 	struct sock *sk;
27430a708f8fSGustavo F. Padovan 
27440a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(rsp->scid);
27450a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(rsp->dcid);
27460a708f8fSGustavo F. Padovan 
27470a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
27480a708f8fSGustavo F. Padovan 
2749baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
275048454079SGustavo F. Padovan 	if (!chan)
27510a708f8fSGustavo F. Padovan 		return 0;
27520a708f8fSGustavo F. Padovan 
275348454079SGustavo F. Padovan 	sk = chan->sk;
275448454079SGustavo F. Padovan 
27550a708f8fSGustavo F. Padovan 	/* don't delete l2cap channel if sk is owned by user */
27560a708f8fSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
275789bc500eSGustavo F. Padovan 		l2cap_state_change(chan,BT_DISCONN);
2758ab07801dSGustavo F. Padovan 		l2cap_chan_clear_timer(chan);
2759ab07801dSGustavo F. Padovan 		l2cap_chan_set_timer(chan, HZ / 5);
27600a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
27610a708f8fSGustavo F. Padovan 		return 0;
27620a708f8fSGustavo F. Padovan 	}
27630a708f8fSGustavo F. Padovan 
276448454079SGustavo F. Padovan 	l2cap_chan_del(chan, 0);
27650a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
27660a708f8fSGustavo F. Padovan 
2767ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
27680a708f8fSGustavo F. Padovan 	return 0;
27690a708f8fSGustavo F. Padovan }
27700a708f8fSGustavo F. Padovan 
27710a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
27720a708f8fSGustavo F. Padovan {
27730a708f8fSGustavo F. Padovan 	struct l2cap_info_req *req = (struct l2cap_info_req *) data;
27740a708f8fSGustavo F. Padovan 	u16 type;
27750a708f8fSGustavo F. Padovan 
27760a708f8fSGustavo F. Padovan 	type = __le16_to_cpu(req->type);
27770a708f8fSGustavo F. Padovan 
27780a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x", type);
27790a708f8fSGustavo F. Padovan 
27800a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
27810a708f8fSGustavo F. Padovan 		u8 buf[8];
27820a708f8fSGustavo F. Padovan 		u32 feat_mask = l2cap_feat_mask;
27830a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
27840a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FEAT_MASK);
27850a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
27860a708f8fSGustavo F. Padovan 		if (!disable_ertm)
27870a708f8fSGustavo F. Padovan 			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
27880a708f8fSGustavo F. Padovan 							 | L2CAP_FEAT_FCS;
27890a708f8fSGustavo F. Padovan 		put_unaligned_le32(feat_mask, rsp->data);
27900a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
27910a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
27920a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
27930a708f8fSGustavo F. Padovan 		u8 buf[12];
27940a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
27950a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
27960a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
27970a708f8fSGustavo F. Padovan 		memcpy(buf + 4, l2cap_fixed_chan, 8);
27980a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
27990a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
28000a708f8fSGustavo F. Padovan 	} else {
28010a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp rsp;
28020a708f8fSGustavo F. Padovan 		rsp.type   = cpu_to_le16(type);
28030a708f8fSGustavo F. Padovan 		rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
28040a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
28050a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(rsp), &rsp);
28060a708f8fSGustavo F. Padovan 	}
28070a708f8fSGustavo F. Padovan 
28080a708f8fSGustavo F. Padovan 	return 0;
28090a708f8fSGustavo F. Padovan }
28100a708f8fSGustavo F. Padovan 
28110a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
28120a708f8fSGustavo F. Padovan {
28130a708f8fSGustavo F. Padovan 	struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
28140a708f8fSGustavo F. Padovan 	u16 type, result;
28150a708f8fSGustavo F. Padovan 
28160a708f8fSGustavo F. Padovan 	type   = __le16_to_cpu(rsp->type);
28170a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
28180a708f8fSGustavo F. Padovan 
28190a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
28200a708f8fSGustavo F. Padovan 
2821e90165beSAndrei Emeltchenko 	/* L2CAP Info req/rsp are unbound to channels, add extra checks */
2822e90165beSAndrei Emeltchenko 	if (cmd->ident != conn->info_ident ||
2823e90165beSAndrei Emeltchenko 			conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
2824e90165beSAndrei Emeltchenko 		return 0;
2825e90165beSAndrei Emeltchenko 
28260a708f8fSGustavo F. Padovan 	del_timer(&conn->info_timer);
28270a708f8fSGustavo F. Padovan 
28280a708f8fSGustavo F. Padovan 	if (result != L2CAP_IR_SUCCESS) {
28290a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28300a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
28310a708f8fSGustavo F. Padovan 
28320a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
28330a708f8fSGustavo F. Padovan 
28340a708f8fSGustavo F. Padovan 		return 0;
28350a708f8fSGustavo F. Padovan 	}
28360a708f8fSGustavo F. Padovan 
28370a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
28380a708f8fSGustavo F. Padovan 		conn->feat_mask = get_unaligned_le32(rsp->data);
28390a708f8fSGustavo F. Padovan 
28400a708f8fSGustavo F. Padovan 		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
28410a708f8fSGustavo F. Padovan 			struct l2cap_info_req req;
28420a708f8fSGustavo F. Padovan 			req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
28430a708f8fSGustavo F. Padovan 
28440a708f8fSGustavo F. Padovan 			conn->info_ident = l2cap_get_ident(conn);
28450a708f8fSGustavo F. Padovan 
28460a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, conn->info_ident,
28470a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
28480a708f8fSGustavo F. Padovan 		} else {
28490a708f8fSGustavo F. Padovan 			conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28500a708f8fSGustavo F. Padovan 			conn->info_ident = 0;
28510a708f8fSGustavo F. Padovan 
28520a708f8fSGustavo F. Padovan 			l2cap_conn_start(conn);
28530a708f8fSGustavo F. Padovan 		}
28540a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
28550a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28560a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
28570a708f8fSGustavo F. Padovan 
28580a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
28590a708f8fSGustavo F. Padovan 	}
28600a708f8fSGustavo F. Padovan 
28610a708f8fSGustavo F. Padovan 	return 0;
28620a708f8fSGustavo F. Padovan }
28630a708f8fSGustavo F. Padovan 
2864e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
2865de73115aSClaudio Takahasi 							u16 to_multiplier)
2866de73115aSClaudio Takahasi {
2867de73115aSClaudio Takahasi 	u16 max_latency;
2868de73115aSClaudio Takahasi 
2869de73115aSClaudio Takahasi 	if (min > max || min < 6 || max > 3200)
2870de73115aSClaudio Takahasi 		return -EINVAL;
2871de73115aSClaudio Takahasi 
2872de73115aSClaudio Takahasi 	if (to_multiplier < 10 || to_multiplier > 3200)
2873de73115aSClaudio Takahasi 		return -EINVAL;
2874de73115aSClaudio Takahasi 
2875de73115aSClaudio Takahasi 	if (max >= to_multiplier * 8)
2876de73115aSClaudio Takahasi 		return -EINVAL;
2877de73115aSClaudio Takahasi 
2878de73115aSClaudio Takahasi 	max_latency = (to_multiplier * 8 / max) - 1;
2879de73115aSClaudio Takahasi 	if (latency > 499 || latency > max_latency)
2880de73115aSClaudio Takahasi 		return -EINVAL;
2881de73115aSClaudio Takahasi 
2882de73115aSClaudio Takahasi 	return 0;
2883de73115aSClaudio Takahasi }
2884de73115aSClaudio Takahasi 
2885de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
2886de73115aSClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
2887de73115aSClaudio Takahasi {
2888de73115aSClaudio Takahasi 	struct hci_conn *hcon = conn->hcon;
2889de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_req *req;
2890de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_rsp rsp;
2891de73115aSClaudio Takahasi 	u16 min, max, latency, to_multiplier, cmd_len;
28922ce603ebSClaudio Takahasi 	int err;
2893de73115aSClaudio Takahasi 
2894de73115aSClaudio Takahasi 	if (!(hcon->link_mode & HCI_LM_MASTER))
2895de73115aSClaudio Takahasi 		return -EINVAL;
2896de73115aSClaudio Takahasi 
2897de73115aSClaudio Takahasi 	cmd_len = __le16_to_cpu(cmd->len);
2898de73115aSClaudio Takahasi 	if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
2899de73115aSClaudio Takahasi 		return -EPROTO;
2900de73115aSClaudio Takahasi 
2901de73115aSClaudio Takahasi 	req = (struct l2cap_conn_param_update_req *) data;
2902de73115aSClaudio Takahasi 	min		= __le16_to_cpu(req->min);
2903de73115aSClaudio Takahasi 	max		= __le16_to_cpu(req->max);
2904de73115aSClaudio Takahasi 	latency		= __le16_to_cpu(req->latency);
2905de73115aSClaudio Takahasi 	to_multiplier	= __le16_to_cpu(req->to_multiplier);
2906de73115aSClaudio Takahasi 
2907de73115aSClaudio Takahasi 	BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
2908de73115aSClaudio Takahasi 						min, max, latency, to_multiplier);
2909de73115aSClaudio Takahasi 
2910de73115aSClaudio Takahasi 	memset(&rsp, 0, sizeof(rsp));
29112ce603ebSClaudio Takahasi 
29122ce603ebSClaudio Takahasi 	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
29132ce603ebSClaudio Takahasi 	if (err)
2914de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
2915de73115aSClaudio Takahasi 	else
2916de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
2917de73115aSClaudio Takahasi 
2918de73115aSClaudio Takahasi 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
2919de73115aSClaudio Takahasi 							sizeof(rsp), &rsp);
2920de73115aSClaudio Takahasi 
29212ce603ebSClaudio Takahasi 	if (!err)
29222ce603ebSClaudio Takahasi 		hci_le_conn_update(hcon, min, max, latency, to_multiplier);
29232ce603ebSClaudio Takahasi 
2924de73115aSClaudio Takahasi 	return 0;
2925de73115aSClaudio Takahasi }
2926de73115aSClaudio Takahasi 
29273300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
29283300d9a9SClaudio Takahasi 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
29293300d9a9SClaudio Takahasi {
29303300d9a9SClaudio Takahasi 	int err = 0;
29313300d9a9SClaudio Takahasi 
29323300d9a9SClaudio Takahasi 	switch (cmd->code) {
29333300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
29343300d9a9SClaudio Takahasi 		l2cap_command_rej(conn, cmd, data);
29353300d9a9SClaudio Takahasi 		break;
29363300d9a9SClaudio Takahasi 
29373300d9a9SClaudio Takahasi 	case L2CAP_CONN_REQ:
29383300d9a9SClaudio Takahasi 		err = l2cap_connect_req(conn, cmd, data);
29393300d9a9SClaudio Takahasi 		break;
29403300d9a9SClaudio Takahasi 
29413300d9a9SClaudio Takahasi 	case L2CAP_CONN_RSP:
29423300d9a9SClaudio Takahasi 		err = l2cap_connect_rsp(conn, cmd, data);
29433300d9a9SClaudio Takahasi 		break;
29443300d9a9SClaudio Takahasi 
29453300d9a9SClaudio Takahasi 	case L2CAP_CONF_REQ:
29463300d9a9SClaudio Takahasi 		err = l2cap_config_req(conn, cmd, cmd_len, data);
29473300d9a9SClaudio Takahasi 		break;
29483300d9a9SClaudio Takahasi 
29493300d9a9SClaudio Takahasi 	case L2CAP_CONF_RSP:
29503300d9a9SClaudio Takahasi 		err = l2cap_config_rsp(conn, cmd, data);
29513300d9a9SClaudio Takahasi 		break;
29523300d9a9SClaudio Takahasi 
29533300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_REQ:
29543300d9a9SClaudio Takahasi 		err = l2cap_disconnect_req(conn, cmd, data);
29553300d9a9SClaudio Takahasi 		break;
29563300d9a9SClaudio Takahasi 
29573300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_RSP:
29583300d9a9SClaudio Takahasi 		err = l2cap_disconnect_rsp(conn, cmd, data);
29593300d9a9SClaudio Takahasi 		break;
29603300d9a9SClaudio Takahasi 
29613300d9a9SClaudio Takahasi 	case L2CAP_ECHO_REQ:
29623300d9a9SClaudio Takahasi 		l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
29633300d9a9SClaudio Takahasi 		break;
29643300d9a9SClaudio Takahasi 
29653300d9a9SClaudio Takahasi 	case L2CAP_ECHO_RSP:
29663300d9a9SClaudio Takahasi 		break;
29673300d9a9SClaudio Takahasi 
29683300d9a9SClaudio Takahasi 	case L2CAP_INFO_REQ:
29693300d9a9SClaudio Takahasi 		err = l2cap_information_req(conn, cmd, data);
29703300d9a9SClaudio Takahasi 		break;
29713300d9a9SClaudio Takahasi 
29723300d9a9SClaudio Takahasi 	case L2CAP_INFO_RSP:
29733300d9a9SClaudio Takahasi 		err = l2cap_information_rsp(conn, cmd, data);
29743300d9a9SClaudio Takahasi 		break;
29753300d9a9SClaudio Takahasi 
29763300d9a9SClaudio Takahasi 	default:
29773300d9a9SClaudio Takahasi 		BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
29783300d9a9SClaudio Takahasi 		err = -EINVAL;
29793300d9a9SClaudio Takahasi 		break;
29803300d9a9SClaudio Takahasi 	}
29813300d9a9SClaudio Takahasi 
29823300d9a9SClaudio Takahasi 	return err;
29833300d9a9SClaudio Takahasi }
29843300d9a9SClaudio Takahasi 
29853300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
29863300d9a9SClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
29873300d9a9SClaudio Takahasi {
29883300d9a9SClaudio Takahasi 	switch (cmd->code) {
29893300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
29903300d9a9SClaudio Takahasi 		return 0;
29913300d9a9SClaudio Takahasi 
29923300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_REQ:
2993de73115aSClaudio Takahasi 		return l2cap_conn_param_update_req(conn, cmd, data);
29943300d9a9SClaudio Takahasi 
29953300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_RSP:
29963300d9a9SClaudio Takahasi 		return 0;
29973300d9a9SClaudio Takahasi 
29983300d9a9SClaudio Takahasi 	default:
29993300d9a9SClaudio Takahasi 		BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
30003300d9a9SClaudio Takahasi 		return -EINVAL;
30013300d9a9SClaudio Takahasi 	}
30023300d9a9SClaudio Takahasi }
30033300d9a9SClaudio Takahasi 
30043300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn,
30053300d9a9SClaudio Takahasi 							struct sk_buff *skb)
30060a708f8fSGustavo F. Padovan {
30070a708f8fSGustavo F. Padovan 	u8 *data = skb->data;
30080a708f8fSGustavo F. Padovan 	int len = skb->len;
30090a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr cmd;
30103300d9a9SClaudio Takahasi 	int err;
30110a708f8fSGustavo F. Padovan 
30120a708f8fSGustavo F. Padovan 	l2cap_raw_recv(conn, skb);
30130a708f8fSGustavo F. Padovan 
30140a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CMD_HDR_SIZE) {
30150a708f8fSGustavo F. Padovan 		u16 cmd_len;
30160a708f8fSGustavo F. Padovan 		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
30170a708f8fSGustavo F. Padovan 		data += L2CAP_CMD_HDR_SIZE;
30180a708f8fSGustavo F. Padovan 		len  -= L2CAP_CMD_HDR_SIZE;
30190a708f8fSGustavo F. Padovan 
30200a708f8fSGustavo F. Padovan 		cmd_len = le16_to_cpu(cmd.len);
30210a708f8fSGustavo F. Padovan 
30220a708f8fSGustavo F. Padovan 		BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
30230a708f8fSGustavo F. Padovan 
30240a708f8fSGustavo F. Padovan 		if (cmd_len > len || !cmd.ident) {
30250a708f8fSGustavo F. Padovan 			BT_DBG("corrupted command");
30260a708f8fSGustavo F. Padovan 			break;
30270a708f8fSGustavo F. Padovan 		}
30280a708f8fSGustavo F. Padovan 
30293300d9a9SClaudio Takahasi 		if (conn->hcon->type == LE_LINK)
30303300d9a9SClaudio Takahasi 			err = l2cap_le_sig_cmd(conn, &cmd, data);
30313300d9a9SClaudio Takahasi 		else
30323300d9a9SClaudio Takahasi 			err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
30330a708f8fSGustavo F. Padovan 
30340a708f8fSGustavo F. Padovan 		if (err) {
30350a708f8fSGustavo F. Padovan 			struct l2cap_cmd_rej rej;
30362c6d1a2eSGustavo F. Padovan 
30372c6d1a2eSGustavo F. Padovan 			BT_ERR("Wrong link type (%d)", err);
30380a708f8fSGustavo F. Padovan 
30390a708f8fSGustavo F. Padovan 			/* FIXME: Map err to a valid reason */
30400a708f8fSGustavo F. Padovan 			rej.reason = cpu_to_le16(0);
30410a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
30420a708f8fSGustavo F. Padovan 		}
30430a708f8fSGustavo F. Padovan 
30440a708f8fSGustavo F. Padovan 		data += cmd_len;
30450a708f8fSGustavo F. Padovan 		len  -= cmd_len;
30460a708f8fSGustavo F. Padovan 	}
30470a708f8fSGustavo F. Padovan 
30480a708f8fSGustavo F. Padovan 	kfree_skb(skb);
30490a708f8fSGustavo F. Padovan }
30500a708f8fSGustavo F. Padovan 
305147d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
30520a708f8fSGustavo F. Padovan {
30530a708f8fSGustavo F. Padovan 	u16 our_fcs, rcv_fcs;
30540a708f8fSGustavo F. Padovan 	int hdr_size = L2CAP_HDR_SIZE + 2;
30550a708f8fSGustavo F. Padovan 
305647d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
30570a708f8fSGustavo F. Padovan 		skb_trim(skb, skb->len - 2);
30580a708f8fSGustavo F. Padovan 		rcv_fcs = get_unaligned_le16(skb->data + skb->len);
30590a708f8fSGustavo F. Padovan 		our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
30600a708f8fSGustavo F. Padovan 
30610a708f8fSGustavo F. Padovan 		if (our_fcs != rcv_fcs)
30620a708f8fSGustavo F. Padovan 			return -EBADMSG;
30630a708f8fSGustavo F. Padovan 	}
30640a708f8fSGustavo F. Padovan 	return 0;
30650a708f8fSGustavo F. Padovan }
30660a708f8fSGustavo F. Padovan 
3067525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
30680a708f8fSGustavo F. Padovan {
30690a708f8fSGustavo F. Padovan 	u16 control = 0;
30700a708f8fSGustavo F. Padovan 
30716a026610SGustavo F. Padovan 	chan->frames_sent = 0;
30720a708f8fSGustavo F. Padovan 
307342e5c802SGustavo F. Padovan 	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
30740a708f8fSGustavo F. Padovan 
3075525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
30760a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_NOT_READY;
3077525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
3078525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_RNR_SENT;
30790a708f8fSGustavo F. Padovan 	}
30800a708f8fSGustavo F. Padovan 
3081525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY)
3082525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
30830a708f8fSGustavo F. Padovan 
3084525cd185SGustavo F. Padovan 	l2cap_ertm_send(chan);
30850a708f8fSGustavo F. Padovan 
3086525cd185SGustavo F. Padovan 	if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
30876a026610SGustavo F. Padovan 			chan->frames_sent == 0) {
30880a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_READY;
3089525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
30900a708f8fSGustavo F. Padovan 	}
30910a708f8fSGustavo F. Padovan }
30920a708f8fSGustavo F. Padovan 
309342e5c802SGustavo F. Padovan static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar)
30940a708f8fSGustavo F. Padovan {
30950a708f8fSGustavo F. Padovan 	struct sk_buff *next_skb;
30960a708f8fSGustavo F. Padovan 	int tx_seq_offset, next_tx_seq_offset;
30970a708f8fSGustavo F. Padovan 
30980a708f8fSGustavo F. Padovan 	bt_cb(skb)->tx_seq = tx_seq;
30990a708f8fSGustavo F. Padovan 	bt_cb(skb)->sar = sar;
31000a708f8fSGustavo F. Padovan 
3101f1c6775bSGustavo F. Padovan 	next_skb = skb_peek(&chan->srej_q);
31020a708f8fSGustavo F. Padovan 	if (!next_skb) {
3103f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->srej_q, skb);
31040a708f8fSGustavo F. Padovan 		return 0;
31050a708f8fSGustavo F. Padovan 	}
31060a708f8fSGustavo F. Padovan 
310742e5c802SGustavo F. Padovan 	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
31080a708f8fSGustavo F. Padovan 	if (tx_seq_offset < 0)
31090a708f8fSGustavo F. Padovan 		tx_seq_offset += 64;
31100a708f8fSGustavo F. Padovan 
31110a708f8fSGustavo F. Padovan 	do {
31120a708f8fSGustavo F. Padovan 		if (bt_cb(next_skb)->tx_seq == tx_seq)
31130a708f8fSGustavo F. Padovan 			return -EINVAL;
31140a708f8fSGustavo F. Padovan 
31150a708f8fSGustavo F. Padovan 		next_tx_seq_offset = (bt_cb(next_skb)->tx_seq -
311642e5c802SGustavo F. Padovan 						chan->buffer_seq) % 64;
31170a708f8fSGustavo F. Padovan 		if (next_tx_seq_offset < 0)
31180a708f8fSGustavo F. Padovan 			next_tx_seq_offset += 64;
31190a708f8fSGustavo F. Padovan 
31200a708f8fSGustavo F. Padovan 		if (next_tx_seq_offset > tx_seq_offset) {
3121f1c6775bSGustavo F. Padovan 			__skb_queue_before(&chan->srej_q, next_skb, skb);
31220a708f8fSGustavo F. Padovan 			return 0;
31230a708f8fSGustavo F. Padovan 		}
31240a708f8fSGustavo F. Padovan 
3125f1c6775bSGustavo F. Padovan 		if (skb_queue_is_last(&chan->srej_q, next_skb))
31260a708f8fSGustavo F. Padovan 			break;
31270a708f8fSGustavo F. Padovan 
3128f1c6775bSGustavo F. Padovan 	} while ((next_skb = skb_queue_next(&chan->srej_q, next_skb)));
31290a708f8fSGustavo F. Padovan 
3130f1c6775bSGustavo F. Padovan 	__skb_queue_tail(&chan->srej_q, skb);
31310a708f8fSGustavo F. Padovan 
31320a708f8fSGustavo F. Padovan 	return 0;
31330a708f8fSGustavo F. Padovan }
31340a708f8fSGustavo F. Padovan 
3135525cd185SGustavo F. Padovan static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
31360a708f8fSGustavo F. Padovan {
31370a708f8fSGustavo F. Padovan 	struct sk_buff *_skb;
31380a708f8fSGustavo F. Padovan 	int err;
31390a708f8fSGustavo F. Padovan 
31400a708f8fSGustavo F. Padovan 	switch (control & L2CAP_CTRL_SAR) {
31410a708f8fSGustavo F. Padovan 	case L2CAP_SDU_UNSEGMENTED:
3142525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
31430a708f8fSGustavo F. Padovan 			goto drop;
31440a708f8fSGustavo F. Padovan 
314523070494SGustavo F. Padovan 		return chan->ops->recv(chan->data, skb);
31460a708f8fSGustavo F. Padovan 
31470a708f8fSGustavo F. Padovan 	case L2CAP_SDU_START:
3148525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
31490a708f8fSGustavo F. Padovan 			goto drop;
31500a708f8fSGustavo F. Padovan 
31516f61fd47SGustavo F. Padovan 		chan->sdu_len = get_unaligned_le16(skb->data);
31520a708f8fSGustavo F. Padovan 
31530c1bc5c6SGustavo F. Padovan 		if (chan->sdu_len > chan->imtu)
31540a708f8fSGustavo F. Padovan 			goto disconnect;
31550a708f8fSGustavo F. Padovan 
31566f61fd47SGustavo F. Padovan 		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
31576f61fd47SGustavo F. Padovan 		if (!chan->sdu)
31580a708f8fSGustavo F. Padovan 			return -ENOMEM;
31590a708f8fSGustavo F. Padovan 
31600a708f8fSGustavo F. Padovan 		/* pull sdu_len bytes only after alloc, because of Local Busy
31610a708f8fSGustavo F. Padovan 		 * condition we have to be sure that this will be executed
31620a708f8fSGustavo F. Padovan 		 * only once, i.e., when alloc does not fail */
31630a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
31640a708f8fSGustavo F. Padovan 
31656f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
31660a708f8fSGustavo F. Padovan 
3167525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SAR_SDU;
31686f61fd47SGustavo F. Padovan 		chan->partial_sdu_len = skb->len;
31690a708f8fSGustavo F. Padovan 		break;
31700a708f8fSGustavo F. Padovan 
31710a708f8fSGustavo F. Padovan 	case L2CAP_SDU_CONTINUE:
3172525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
31730a708f8fSGustavo F. Padovan 			goto disconnect;
31740a708f8fSGustavo F. Padovan 
31756f61fd47SGustavo F. Padovan 		if (!chan->sdu)
31760a708f8fSGustavo F. Padovan 			goto disconnect;
31770a708f8fSGustavo F. Padovan 
31786f61fd47SGustavo F. Padovan 		chan->partial_sdu_len += skb->len;
31796f61fd47SGustavo F. Padovan 		if (chan->partial_sdu_len > chan->sdu_len)
31800a708f8fSGustavo F. Padovan 			goto drop;
31810a708f8fSGustavo F. Padovan 
31826f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
31830a708f8fSGustavo F. Padovan 
31840a708f8fSGustavo F. Padovan 		break;
31850a708f8fSGustavo F. Padovan 
31860a708f8fSGustavo F. Padovan 	case L2CAP_SDU_END:
3187525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
31880a708f8fSGustavo F. Padovan 			goto disconnect;
31890a708f8fSGustavo F. Padovan 
31906f61fd47SGustavo F. Padovan 		if (!chan->sdu)
31910a708f8fSGustavo F. Padovan 			goto disconnect;
31920a708f8fSGustavo F. Padovan 
3193525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) {
31946f61fd47SGustavo F. Padovan 			chan->partial_sdu_len += skb->len;
31950a708f8fSGustavo F. Padovan 
31960c1bc5c6SGustavo F. Padovan 			if (chan->partial_sdu_len > chan->imtu)
31970a708f8fSGustavo F. Padovan 				goto drop;
31980a708f8fSGustavo F. Padovan 
31996f61fd47SGustavo F. Padovan 			if (chan->partial_sdu_len != chan->sdu_len)
32000a708f8fSGustavo F. Padovan 				goto drop;
32010a708f8fSGustavo F. Padovan 
32026f61fd47SGustavo F. Padovan 			memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
32030a708f8fSGustavo F. Padovan 		}
32040a708f8fSGustavo F. Padovan 
32056f61fd47SGustavo F. Padovan 		_skb = skb_clone(chan->sdu, GFP_ATOMIC);
32060a708f8fSGustavo F. Padovan 		if (!_skb) {
3207525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
32080a708f8fSGustavo F. Padovan 			return -ENOMEM;
32090a708f8fSGustavo F. Padovan 		}
32100a708f8fSGustavo F. Padovan 
321123070494SGustavo F. Padovan 		err = chan->ops->recv(chan->data, _skb);
32120a708f8fSGustavo F. Padovan 		if (err < 0) {
32130a708f8fSGustavo F. Padovan 			kfree_skb(_skb);
3214525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
32150a708f8fSGustavo F. Padovan 			return err;
32160a708f8fSGustavo F. Padovan 		}
32170a708f8fSGustavo F. Padovan 
3218525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SAR_RETRY;
3219525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
32200a708f8fSGustavo F. Padovan 
32216f61fd47SGustavo F. Padovan 		kfree_skb(chan->sdu);
32220a708f8fSGustavo F. Padovan 		break;
32230a708f8fSGustavo F. Padovan 	}
32240a708f8fSGustavo F. Padovan 
32250a708f8fSGustavo F. Padovan 	kfree_skb(skb);
32260a708f8fSGustavo F. Padovan 	return 0;
32270a708f8fSGustavo F. Padovan 
32280a708f8fSGustavo F. Padovan drop:
32296f61fd47SGustavo F. Padovan 	kfree_skb(chan->sdu);
32306f61fd47SGustavo F. Padovan 	chan->sdu = NULL;
32310a708f8fSGustavo F. Padovan 
32320a708f8fSGustavo F. Padovan disconnect:
32338c1d787bSGustavo F. Padovan 	l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
32340a708f8fSGustavo F. Padovan 	kfree_skb(skb);
32350a708f8fSGustavo F. Padovan 	return 0;
32360a708f8fSGustavo F. Padovan }
32370a708f8fSGustavo F. Padovan 
3238525cd185SGustavo F. Padovan static int l2cap_try_push_rx_skb(struct l2cap_chan *chan)
32390a708f8fSGustavo F. Padovan {
32400a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
32410a708f8fSGustavo F. Padovan 	u16 control;
32420a708f8fSGustavo F. Padovan 	int err;
32430a708f8fSGustavo F. Padovan 
3244f1c6775bSGustavo F. Padovan 	while ((skb = skb_dequeue(&chan->busy_q))) {
32450a708f8fSGustavo F. Padovan 		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3246525cd185SGustavo F. Padovan 		err = l2cap_ertm_reassembly_sdu(chan, skb, control);
32470a708f8fSGustavo F. Padovan 		if (err < 0) {
3248f1c6775bSGustavo F. Padovan 			skb_queue_head(&chan->busy_q, skb);
32490a708f8fSGustavo F. Padovan 			return -EBUSY;
32500a708f8fSGustavo F. Padovan 		}
32510a708f8fSGustavo F. Padovan 
325242e5c802SGustavo F. Padovan 		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
32530a708f8fSGustavo F. Padovan 	}
32540a708f8fSGustavo F. Padovan 
3255525cd185SGustavo F. Padovan 	if (!(chan->conn_state & L2CAP_CONN_RNR_SENT))
32560a708f8fSGustavo F. Padovan 		goto done;
32570a708f8fSGustavo F. Padovan 
325842e5c802SGustavo F. Padovan 	control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
32590a708f8fSGustavo F. Padovan 	control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL;
3260525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
32616a026610SGustavo F. Padovan 	chan->retry_count = 1;
32620a708f8fSGustavo F. Padovan 
3263e92c8e70SGustavo F. Padovan 	del_timer(&chan->retrans_timer);
32640a708f8fSGustavo F. Padovan 	__mod_monitor_timer();
32650a708f8fSGustavo F. Padovan 
3266525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_WAIT_F;
32670a708f8fSGustavo F. Padovan 
32680a708f8fSGustavo F. Padovan done:
3269525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
3270525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_RNR_SENT;
32710a708f8fSGustavo F. Padovan 
327249208c9cSGustavo F. Padovan 	BT_DBG("chan %p, Exit local busy", chan);
32730a708f8fSGustavo F. Padovan 
32740a708f8fSGustavo F. Padovan 	return 0;
32750a708f8fSGustavo F. Padovan }
32760a708f8fSGustavo F. Padovan 
32770a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work)
32780a708f8fSGustavo F. Padovan {
32790a708f8fSGustavo F. Padovan 	DECLARE_WAITQUEUE(wait, current);
3280311bb895SGustavo F. Padovan 	struct l2cap_chan *chan =
3281311bb895SGustavo F. Padovan 		container_of(work, struct l2cap_chan, busy_work);
3282311bb895SGustavo F. Padovan 	struct sock *sk = chan->sk;
32830a708f8fSGustavo F. Padovan 	int n_tries = 0, timeo = HZ/5, err;
32840a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
32850a708f8fSGustavo F. Padovan 
32860a708f8fSGustavo F. Padovan 	lock_sock(sk);
32870a708f8fSGustavo F. Padovan 
32880a708f8fSGustavo F. Padovan 	add_wait_queue(sk_sleep(sk), &wait);
3289311bb895SGustavo F. Padovan 	while ((skb = skb_peek(&chan->busy_q))) {
32900a708f8fSGustavo F. Padovan 		set_current_state(TASK_INTERRUPTIBLE);
32910a708f8fSGustavo F. Padovan 
32920a708f8fSGustavo F. Padovan 		if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) {
32930a708f8fSGustavo F. Padovan 			err = -EBUSY;
32948c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, EBUSY);
32950a708f8fSGustavo F. Padovan 			break;
32960a708f8fSGustavo F. Padovan 		}
32970a708f8fSGustavo F. Padovan 
32980a708f8fSGustavo F. Padovan 		if (!timeo)
32990a708f8fSGustavo F. Padovan 			timeo = HZ/5;
33000a708f8fSGustavo F. Padovan 
33010a708f8fSGustavo F. Padovan 		if (signal_pending(current)) {
33020a708f8fSGustavo F. Padovan 			err = sock_intr_errno(timeo);
33030a708f8fSGustavo F. Padovan 			break;
33040a708f8fSGustavo F. Padovan 		}
33050a708f8fSGustavo F. Padovan 
33060a708f8fSGustavo F. Padovan 		release_sock(sk);
33070a708f8fSGustavo F. Padovan 		timeo = schedule_timeout(timeo);
33080a708f8fSGustavo F. Padovan 		lock_sock(sk);
33090a708f8fSGustavo F. Padovan 
33100a708f8fSGustavo F. Padovan 		err = sock_error(sk);
33110a708f8fSGustavo F. Padovan 		if (err)
33120a708f8fSGustavo F. Padovan 			break;
33130a708f8fSGustavo F. Padovan 
3314311bb895SGustavo F. Padovan 		if (l2cap_try_push_rx_skb(chan) == 0)
33150a708f8fSGustavo F. Padovan 			break;
33160a708f8fSGustavo F. Padovan 	}
33170a708f8fSGustavo F. Padovan 
33180a708f8fSGustavo F. Padovan 	set_current_state(TASK_RUNNING);
33190a708f8fSGustavo F. Padovan 	remove_wait_queue(sk_sleep(sk), &wait);
33200a708f8fSGustavo F. Padovan 
33210a708f8fSGustavo F. Padovan 	release_sock(sk);
33220a708f8fSGustavo F. Padovan }
33230a708f8fSGustavo F. Padovan 
3324525cd185SGustavo F. Padovan static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
33250a708f8fSGustavo F. Padovan {
33260a708f8fSGustavo F. Padovan 	int sctrl, err;
33270a708f8fSGustavo F. Padovan 
3328525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
33290a708f8fSGustavo F. Padovan 		bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3330f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->busy_q, skb);
3331525cd185SGustavo F. Padovan 		return l2cap_try_push_rx_skb(chan);
33320a708f8fSGustavo F. Padovan 
33330a708f8fSGustavo F. Padovan 
33340a708f8fSGustavo F. Padovan 	}
33350a708f8fSGustavo F. Padovan 
3336525cd185SGustavo F. Padovan 	err = l2cap_ertm_reassembly_sdu(chan, skb, control);
33370a708f8fSGustavo F. Padovan 	if (err >= 0) {
333842e5c802SGustavo F. Padovan 		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
33390a708f8fSGustavo F. Padovan 		return err;
33400a708f8fSGustavo F. Padovan 	}
33410a708f8fSGustavo F. Padovan 
33420a708f8fSGustavo F. Padovan 	/* Busy Condition */
3343311bb895SGustavo F. Padovan 	BT_DBG("chan %p, Enter local busy", chan);
33440a708f8fSGustavo F. Padovan 
3345525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_LOCAL_BUSY;
33460a708f8fSGustavo F. Padovan 	bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3347f1c6775bSGustavo F. Padovan 	__skb_queue_tail(&chan->busy_q, skb);
33480a708f8fSGustavo F. Padovan 
334942e5c802SGustavo F. Padovan 	sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
33500a708f8fSGustavo F. Padovan 	sctrl |= L2CAP_SUPER_RCV_NOT_READY;
3351525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, sctrl);
33520a708f8fSGustavo F. Padovan 
3353525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_RNR_SENT;
33540a708f8fSGustavo F. Padovan 
3355e92c8e70SGustavo F. Padovan 	del_timer(&chan->ack_timer);
33560a708f8fSGustavo F. Padovan 
3357311bb895SGustavo F. Padovan 	queue_work(_busy_wq, &chan->busy_work);
33580a708f8fSGustavo F. Padovan 
33590a708f8fSGustavo F. Padovan 	return err;
33600a708f8fSGustavo F. Padovan }
33610a708f8fSGustavo F. Padovan 
3362525cd185SGustavo F. Padovan static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
33630a708f8fSGustavo F. Padovan {
33640a708f8fSGustavo F. Padovan 	struct sk_buff *_skb;
33650a708f8fSGustavo F. Padovan 	int err = -EINVAL;
33660a708f8fSGustavo F. Padovan 
33670a708f8fSGustavo F. Padovan 	/*
33680a708f8fSGustavo F. Padovan 	 * TODO: We have to notify the userland if some data is lost with the
33690a708f8fSGustavo F. Padovan 	 * Streaming Mode.
33700a708f8fSGustavo F. Padovan 	 */
33710a708f8fSGustavo F. Padovan 
33720a708f8fSGustavo F. Padovan 	switch (control & L2CAP_CTRL_SAR) {
33730a708f8fSGustavo F. Padovan 	case L2CAP_SDU_UNSEGMENTED:
3374525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
33756f61fd47SGustavo F. Padovan 			kfree_skb(chan->sdu);
33760a708f8fSGustavo F. Padovan 			break;
33770a708f8fSGustavo F. Padovan 		}
33780a708f8fSGustavo F. Padovan 
337923070494SGustavo F. Padovan 		err = chan->ops->recv(chan->data, skb);
33800a708f8fSGustavo F. Padovan 		if (!err)
33810a708f8fSGustavo F. Padovan 			return 0;
33820a708f8fSGustavo F. Padovan 
33830a708f8fSGustavo F. Padovan 		break;
33840a708f8fSGustavo F. Padovan 
33850a708f8fSGustavo F. Padovan 	case L2CAP_SDU_START:
3386525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
33876f61fd47SGustavo F. Padovan 			kfree_skb(chan->sdu);
33880a708f8fSGustavo F. Padovan 			break;
33890a708f8fSGustavo F. Padovan 		}
33900a708f8fSGustavo F. Padovan 
33916f61fd47SGustavo F. Padovan 		chan->sdu_len = get_unaligned_le16(skb->data);
33920a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
33930a708f8fSGustavo F. Padovan 
33940c1bc5c6SGustavo F. Padovan 		if (chan->sdu_len > chan->imtu) {
33950a708f8fSGustavo F. Padovan 			err = -EMSGSIZE;
33960a708f8fSGustavo F. Padovan 			break;
33970a708f8fSGustavo F. Padovan 		}
33980a708f8fSGustavo F. Padovan 
33996f61fd47SGustavo F. Padovan 		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
34006f61fd47SGustavo F. Padovan 		if (!chan->sdu) {
34010a708f8fSGustavo F. Padovan 			err = -ENOMEM;
34020a708f8fSGustavo F. Padovan 			break;
34030a708f8fSGustavo F. Padovan 		}
34040a708f8fSGustavo F. Padovan 
34056f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
34060a708f8fSGustavo F. Padovan 
3407525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SAR_SDU;
34086f61fd47SGustavo F. Padovan 		chan->partial_sdu_len = skb->len;
34090a708f8fSGustavo F. Padovan 		err = 0;
34100a708f8fSGustavo F. Padovan 		break;
34110a708f8fSGustavo F. Padovan 
34120a708f8fSGustavo F. Padovan 	case L2CAP_SDU_CONTINUE:
3413525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
34140a708f8fSGustavo F. Padovan 			break;
34150a708f8fSGustavo F. Padovan 
34166f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
34170a708f8fSGustavo F. Padovan 
34186f61fd47SGustavo F. Padovan 		chan->partial_sdu_len += skb->len;
34196f61fd47SGustavo F. Padovan 		if (chan->partial_sdu_len > chan->sdu_len)
34206f61fd47SGustavo F. Padovan 			kfree_skb(chan->sdu);
34210a708f8fSGustavo F. Padovan 		else
34220a708f8fSGustavo F. Padovan 			err = 0;
34230a708f8fSGustavo F. Padovan 
34240a708f8fSGustavo F. Padovan 		break;
34250a708f8fSGustavo F. Padovan 
34260a708f8fSGustavo F. Padovan 	case L2CAP_SDU_END:
3427525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
34280a708f8fSGustavo F. Padovan 			break;
34290a708f8fSGustavo F. Padovan 
34306f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
34310a708f8fSGustavo F. Padovan 
3432525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
34336f61fd47SGustavo F. Padovan 		chan->partial_sdu_len += skb->len;
34340a708f8fSGustavo F. Padovan 
34350c1bc5c6SGustavo F. Padovan 		if (chan->partial_sdu_len > chan->imtu)
34360a708f8fSGustavo F. Padovan 			goto drop;
34370a708f8fSGustavo F. Padovan 
34386f61fd47SGustavo F. Padovan 		if (chan->partial_sdu_len == chan->sdu_len) {
34396f61fd47SGustavo F. Padovan 			_skb = skb_clone(chan->sdu, GFP_ATOMIC);
344023070494SGustavo F. Padovan 			err = chan->ops->recv(chan->data, _skb);
34410a708f8fSGustavo F. Padovan 			if (err < 0)
34420a708f8fSGustavo F. Padovan 				kfree_skb(_skb);
34430a708f8fSGustavo F. Padovan 		}
34440a708f8fSGustavo F. Padovan 		err = 0;
34450a708f8fSGustavo F. Padovan 
34460a708f8fSGustavo F. Padovan drop:
34476f61fd47SGustavo F. Padovan 		kfree_skb(chan->sdu);
34480a708f8fSGustavo F. Padovan 		break;
34490a708f8fSGustavo F. Padovan 	}
34500a708f8fSGustavo F. Padovan 
34510a708f8fSGustavo F. Padovan 	kfree_skb(skb);
34520a708f8fSGustavo F. Padovan 	return err;
34530a708f8fSGustavo F. Padovan }
34540a708f8fSGustavo F. Padovan 
3455525cd185SGustavo F. Padovan static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq)
34560a708f8fSGustavo F. Padovan {
34570a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
34580a708f8fSGustavo F. Padovan 	u16 control;
34590a708f8fSGustavo F. Padovan 
3460f1c6775bSGustavo F. Padovan 	while ((skb = skb_peek(&chan->srej_q))) {
34610a708f8fSGustavo F. Padovan 		if (bt_cb(skb)->tx_seq != tx_seq)
34620a708f8fSGustavo F. Padovan 			break;
34630a708f8fSGustavo F. Padovan 
3464f1c6775bSGustavo F. Padovan 		skb = skb_dequeue(&chan->srej_q);
34650a708f8fSGustavo F. Padovan 		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3466525cd185SGustavo F. Padovan 		l2cap_ertm_reassembly_sdu(chan, skb, control);
346742e5c802SGustavo F. Padovan 		chan->buffer_seq_srej =
346842e5c802SGustavo F. Padovan 			(chan->buffer_seq_srej + 1) % 64;
34690a708f8fSGustavo F. Padovan 		tx_seq = (tx_seq + 1) % 64;
34700a708f8fSGustavo F. Padovan 	}
34710a708f8fSGustavo F. Padovan }
34720a708f8fSGustavo F. Padovan 
3473525cd185SGustavo F. Padovan static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq)
34740a708f8fSGustavo F. Padovan {
34750a708f8fSGustavo F. Padovan 	struct srej_list *l, *tmp;
34760a708f8fSGustavo F. Padovan 	u16 control;
34770a708f8fSGustavo F. Padovan 
347839d5a3eeSGustavo F. Padovan 	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
34790a708f8fSGustavo F. Padovan 		if (l->tx_seq == tx_seq) {
34800a708f8fSGustavo F. Padovan 			list_del(&l->list);
34810a708f8fSGustavo F. Padovan 			kfree(l);
34820a708f8fSGustavo F. Padovan 			return;
34830a708f8fSGustavo F. Padovan 		}
34840a708f8fSGustavo F. Padovan 		control = L2CAP_SUPER_SELECT_REJECT;
34850a708f8fSGustavo F. Padovan 		control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3486525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
34870a708f8fSGustavo F. Padovan 		list_del(&l->list);
348839d5a3eeSGustavo F. Padovan 		list_add_tail(&l->list, &chan->srej_l);
34890a708f8fSGustavo F. Padovan 	}
34900a708f8fSGustavo F. Padovan }
34910a708f8fSGustavo F. Padovan 
3492525cd185SGustavo F. Padovan static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq)
34930a708f8fSGustavo F. Padovan {
34940a708f8fSGustavo F. Padovan 	struct srej_list *new;
34950a708f8fSGustavo F. Padovan 	u16 control;
34960a708f8fSGustavo F. Padovan 
349742e5c802SGustavo F. Padovan 	while (tx_seq != chan->expected_tx_seq) {
34980a708f8fSGustavo F. Padovan 		control = L2CAP_SUPER_SELECT_REJECT;
349942e5c802SGustavo F. Padovan 		control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3500525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
35010a708f8fSGustavo F. Padovan 
35020a708f8fSGustavo F. Padovan 		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
350342e5c802SGustavo F. Padovan 		new->tx_seq = chan->expected_tx_seq;
350442e5c802SGustavo F. Padovan 		chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
350539d5a3eeSGustavo F. Padovan 		list_add_tail(&new->list, &chan->srej_l);
35060a708f8fSGustavo F. Padovan 	}
350742e5c802SGustavo F. Padovan 	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
35080a708f8fSGustavo F. Padovan }
35090a708f8fSGustavo F. Padovan 
3510525cd185SGustavo F. Padovan static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
35110a708f8fSGustavo F. Padovan {
35120a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_txseq(rx_control);
35130a708f8fSGustavo F. Padovan 	u8 req_seq = __get_reqseq(rx_control);
35140a708f8fSGustavo F. Padovan 	u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT;
35150a708f8fSGustavo F. Padovan 	int tx_seq_offset, expected_tx_seq_offset;
351647d1ec61SGustavo F. Padovan 	int num_to_ack = (chan->tx_win/6) + 1;
35170a708f8fSGustavo F. Padovan 	int err = 0;
35180a708f8fSGustavo F. Padovan 
3519525cd185SGustavo F. Padovan 	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len,
3520525cd185SGustavo F. Padovan 							tx_seq, rx_control);
35210a708f8fSGustavo F. Padovan 
35220a708f8fSGustavo F. Padovan 	if (L2CAP_CTRL_FINAL & rx_control &&
3523525cd185SGustavo F. Padovan 			chan->conn_state & L2CAP_CONN_WAIT_F) {
3524e92c8e70SGustavo F. Padovan 		del_timer(&chan->monitor_timer);
35256a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
35260a708f8fSGustavo F. Padovan 			__mod_retrans_timer();
3527525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
35280a708f8fSGustavo F. Padovan 	}
35290a708f8fSGustavo F. Padovan 
353042e5c802SGustavo F. Padovan 	chan->expected_ack_seq = req_seq;
353142e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
35320a708f8fSGustavo F. Padovan 
353342e5c802SGustavo F. Padovan 	if (tx_seq == chan->expected_tx_seq)
35340a708f8fSGustavo F. Padovan 		goto expected;
35350a708f8fSGustavo F. Padovan 
353642e5c802SGustavo F. Padovan 	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
35370a708f8fSGustavo F. Padovan 	if (tx_seq_offset < 0)
35380a708f8fSGustavo F. Padovan 		tx_seq_offset += 64;
35390a708f8fSGustavo F. Padovan 
35400a708f8fSGustavo F. Padovan 	/* invalid tx_seq */
354147d1ec61SGustavo F. Padovan 	if (tx_seq_offset >= chan->tx_win) {
35428c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
35430a708f8fSGustavo F. Padovan 		goto drop;
35440a708f8fSGustavo F. Padovan 	}
35450a708f8fSGustavo F. Padovan 
3546e6949280SMat Martineau 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY)
35470a708f8fSGustavo F. Padovan 		goto drop;
35480a708f8fSGustavo F. Padovan 
3549525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
35500a708f8fSGustavo F. Padovan 		struct srej_list *first;
35510a708f8fSGustavo F. Padovan 
355239d5a3eeSGustavo F. Padovan 		first = list_first_entry(&chan->srej_l,
35530a708f8fSGustavo F. Padovan 				struct srej_list, list);
35540a708f8fSGustavo F. Padovan 		if (tx_seq == first->tx_seq) {
355542e5c802SGustavo F. Padovan 			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3556525cd185SGustavo F. Padovan 			l2cap_check_srej_gap(chan, tx_seq);
35570a708f8fSGustavo F. Padovan 
35580a708f8fSGustavo F. Padovan 			list_del(&first->list);
35590a708f8fSGustavo F. Padovan 			kfree(first);
35600a708f8fSGustavo F. Padovan 
356139d5a3eeSGustavo F. Padovan 			if (list_empty(&chan->srej_l)) {
356242e5c802SGustavo F. Padovan 				chan->buffer_seq = chan->buffer_seq_srej;
3563525cd185SGustavo F. Padovan 				chan->conn_state &= ~L2CAP_CONN_SREJ_SENT;
3564525cd185SGustavo F. Padovan 				l2cap_send_ack(chan);
356549208c9cSGustavo F. Padovan 				BT_DBG("chan %p, Exit SREJ_SENT", chan);
35660a708f8fSGustavo F. Padovan 			}
35670a708f8fSGustavo F. Padovan 		} else {
35680a708f8fSGustavo F. Padovan 			struct srej_list *l;
35690a708f8fSGustavo F. Padovan 
35700a708f8fSGustavo F. Padovan 			/* duplicated tx_seq */
357142e5c802SGustavo F. Padovan 			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
35720a708f8fSGustavo F. Padovan 				goto drop;
35730a708f8fSGustavo F. Padovan 
357439d5a3eeSGustavo F. Padovan 			list_for_each_entry(l, &chan->srej_l, list) {
35750a708f8fSGustavo F. Padovan 				if (l->tx_seq == tx_seq) {
3576525cd185SGustavo F. Padovan 					l2cap_resend_srejframe(chan, tx_seq);
35770a708f8fSGustavo F. Padovan 					return 0;
35780a708f8fSGustavo F. Padovan 				}
35790a708f8fSGustavo F. Padovan 			}
3580525cd185SGustavo F. Padovan 			l2cap_send_srejframe(chan, tx_seq);
35810a708f8fSGustavo F. Padovan 		}
35820a708f8fSGustavo F. Padovan 	} else {
35830a708f8fSGustavo F. Padovan 		expected_tx_seq_offset =
358442e5c802SGustavo F. Padovan 			(chan->expected_tx_seq - chan->buffer_seq) % 64;
35850a708f8fSGustavo F. Padovan 		if (expected_tx_seq_offset < 0)
35860a708f8fSGustavo F. Padovan 			expected_tx_seq_offset += 64;
35870a708f8fSGustavo F. Padovan 
35880a708f8fSGustavo F. Padovan 		/* duplicated tx_seq */
35890a708f8fSGustavo F. Padovan 		if (tx_seq_offset < expected_tx_seq_offset)
35900a708f8fSGustavo F. Padovan 			goto drop;
35910a708f8fSGustavo F. Padovan 
3592525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SREJ_SENT;
35930a708f8fSGustavo F. Padovan 
359449208c9cSGustavo F. Padovan 		BT_DBG("chan %p, Enter SREJ", chan);
35950a708f8fSGustavo F. Padovan 
359639d5a3eeSGustavo F. Padovan 		INIT_LIST_HEAD(&chan->srej_l);
359742e5c802SGustavo F. Padovan 		chan->buffer_seq_srej = chan->buffer_seq;
35980a708f8fSGustavo F. Padovan 
3599f1c6775bSGustavo F. Padovan 		__skb_queue_head_init(&chan->srej_q);
3600f1c6775bSGustavo F. Padovan 		__skb_queue_head_init(&chan->busy_q);
360142e5c802SGustavo F. Padovan 		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
36020a708f8fSGustavo F. Padovan 
3603525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_PBIT;
36040a708f8fSGustavo F. Padovan 
3605525cd185SGustavo F. Padovan 		l2cap_send_srejframe(chan, tx_seq);
36060a708f8fSGustavo F. Padovan 
3607e92c8e70SGustavo F. Padovan 		del_timer(&chan->ack_timer);
36080a708f8fSGustavo F. Padovan 	}
36090a708f8fSGustavo F. Padovan 	return 0;
36100a708f8fSGustavo F. Padovan 
36110a708f8fSGustavo F. Padovan expected:
361242e5c802SGustavo F. Padovan 	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
36130a708f8fSGustavo F. Padovan 
3614525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
36150a708f8fSGustavo F. Padovan 		bt_cb(skb)->tx_seq = tx_seq;
36160a708f8fSGustavo F. Padovan 		bt_cb(skb)->sar = sar;
3617f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->srej_q, skb);
36180a708f8fSGustavo F. Padovan 		return 0;
36190a708f8fSGustavo F. Padovan 	}
36200a708f8fSGustavo F. Padovan 
3621525cd185SGustavo F. Padovan 	err = l2cap_push_rx_skb(chan, skb, rx_control);
36220a708f8fSGustavo F. Padovan 	if (err < 0)
36230a708f8fSGustavo F. Padovan 		return 0;
36240a708f8fSGustavo F. Padovan 
36250a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_FINAL) {
3626525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
3627525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
36280a708f8fSGustavo F. Padovan 		else
3629525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
36300a708f8fSGustavo F. Padovan 	}
36310a708f8fSGustavo F. Padovan 
36320a708f8fSGustavo F. Padovan 	__mod_ack_timer();
36330a708f8fSGustavo F. Padovan 
36346a026610SGustavo F. Padovan 	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
36356a026610SGustavo F. Padovan 	if (chan->num_acked == num_to_ack - 1)
3636525cd185SGustavo F. Padovan 		l2cap_send_ack(chan);
36370a708f8fSGustavo F. Padovan 
36380a708f8fSGustavo F. Padovan 	return 0;
36390a708f8fSGustavo F. Padovan 
36400a708f8fSGustavo F. Padovan drop:
36410a708f8fSGustavo F. Padovan 	kfree_skb(skb);
36420a708f8fSGustavo F. Padovan 	return 0;
36430a708f8fSGustavo F. Padovan }
36440a708f8fSGustavo F. Padovan 
3645525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control)
36460a708f8fSGustavo F. Padovan {
364749208c9cSGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control),
36480a708f8fSGustavo F. Padovan 						rx_control);
36490a708f8fSGustavo F. Padovan 
365042e5c802SGustavo F. Padovan 	chan->expected_ack_seq = __get_reqseq(rx_control);
365142e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
36520a708f8fSGustavo F. Padovan 
36530a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL) {
3654525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
3655525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
3656525cd185SGustavo F. Padovan 			if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
36576a026610SGustavo F. Padovan 					(chan->unacked_frames > 0))
36580a708f8fSGustavo F. Padovan 				__mod_retrans_timer();
36590a708f8fSGustavo F. Padovan 
3660525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3661525cd185SGustavo F. Padovan 			l2cap_send_srejtail(chan);
36620a708f8fSGustavo F. Padovan 		} else {
3663525cd185SGustavo F. Padovan 			l2cap_send_i_or_rr_or_rnr(chan);
36640a708f8fSGustavo F. Padovan 		}
36650a708f8fSGustavo F. Padovan 
36660a708f8fSGustavo F. Padovan 	} else if (rx_control & L2CAP_CTRL_FINAL) {
3667525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
36680a708f8fSGustavo F. Padovan 
3669525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
3670525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
36710a708f8fSGustavo F. Padovan 		else
3672525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
36730a708f8fSGustavo F. Padovan 
36740a708f8fSGustavo F. Padovan 	} else {
3675525cd185SGustavo F. Padovan 		if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
36766a026610SGustavo F. Padovan 				(chan->unacked_frames > 0))
36770a708f8fSGustavo F. Padovan 			__mod_retrans_timer();
36780a708f8fSGustavo F. Padovan 
3679525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3680525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SREJ_SENT)
3681525cd185SGustavo F. Padovan 			l2cap_send_ack(chan);
36820a708f8fSGustavo F. Padovan 		else
3683525cd185SGustavo F. Padovan 			l2cap_ertm_send(chan);
36840a708f8fSGustavo F. Padovan 	}
36850a708f8fSGustavo F. Padovan }
36860a708f8fSGustavo F. Padovan 
3687525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control)
36880a708f8fSGustavo F. Padovan {
36890a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_reqseq(rx_control);
36900a708f8fSGustavo F. Padovan 
3691525cd185SGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
36920a708f8fSGustavo F. Padovan 
3693525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
36940a708f8fSGustavo F. Padovan 
369542e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
369642e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
36970a708f8fSGustavo F. Padovan 
36980a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_FINAL) {
3699525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
3700525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
37010a708f8fSGustavo F. Padovan 		else
3702525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
37030a708f8fSGustavo F. Padovan 	} else {
3704525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
37050a708f8fSGustavo F. Padovan 
3706525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_WAIT_F)
3707525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_REJ_ACT;
37080a708f8fSGustavo F. Padovan 	}
37090a708f8fSGustavo F. Padovan }
3710525cd185SGustavo F. Padovan static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control)
37110a708f8fSGustavo F. Padovan {
37120a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_reqseq(rx_control);
37130a708f8fSGustavo F. Padovan 
3714525cd185SGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
37150a708f8fSGustavo F. Padovan 
3716525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
37170a708f8fSGustavo F. Padovan 
37180a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL) {
371942e5c802SGustavo F. Padovan 		chan->expected_ack_seq = tx_seq;
372042e5c802SGustavo F. Padovan 		l2cap_drop_acked_frames(chan);
37210a708f8fSGustavo F. Padovan 
3722525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
3723525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
37240a708f8fSGustavo F. Padovan 
3725525cd185SGustavo F. Padovan 		l2cap_ertm_send(chan);
37260a708f8fSGustavo F. Padovan 
3727525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
37286a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
3729525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
37300a708f8fSGustavo F. Padovan 		}
37310a708f8fSGustavo F. Padovan 	} else if (rx_control & L2CAP_CTRL_FINAL) {
3732525cd185SGustavo F. Padovan 		if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) &&
37336a026610SGustavo F. Padovan 				chan->srej_save_reqseq == tx_seq)
3734525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_SREJ_ACT;
37350a708f8fSGustavo F. Padovan 		else
3736525cd185SGustavo F. Padovan 			l2cap_retransmit_one_frame(chan, tx_seq);
37370a708f8fSGustavo F. Padovan 	} else {
3738525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
3739525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
37406a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
3741525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
37420a708f8fSGustavo F. Padovan 		}
37430a708f8fSGustavo F. Padovan 	}
37440a708f8fSGustavo F. Padovan }
37450a708f8fSGustavo F. Padovan 
3746525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control)
37470a708f8fSGustavo F. Padovan {
37480a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_reqseq(rx_control);
37490a708f8fSGustavo F. Padovan 
3750525cd185SGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
37510a708f8fSGustavo F. Padovan 
3752525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_REMOTE_BUSY;
375342e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
375442e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
37550a708f8fSGustavo F. Padovan 
37560a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL)
3757525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
37580a708f8fSGustavo F. Padovan 
3759525cd185SGustavo F. Padovan 	if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) {
3760e92c8e70SGustavo F. Padovan 		del_timer(&chan->retrans_timer);
37610a708f8fSGustavo F. Padovan 		if (rx_control & L2CAP_CTRL_POLL)
3762525cd185SGustavo F. Padovan 			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
37630a708f8fSGustavo F. Padovan 		return;
37640a708f8fSGustavo F. Padovan 	}
37650a708f8fSGustavo F. Padovan 
37660a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL)
3767525cd185SGustavo F. Padovan 		l2cap_send_srejtail(chan);
37680a708f8fSGustavo F. Padovan 	else
3769525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY);
37700a708f8fSGustavo F. Padovan }
37710a708f8fSGustavo F. Padovan 
3772525cd185SGustavo F. Padovan static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
37730a708f8fSGustavo F. Padovan {
3774525cd185SGustavo F. Padovan 	BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len);
37750a708f8fSGustavo F. Padovan 
37760a708f8fSGustavo F. Padovan 	if (L2CAP_CTRL_FINAL & rx_control &&
3777525cd185SGustavo F. Padovan 			chan->conn_state & L2CAP_CONN_WAIT_F) {
3778e92c8e70SGustavo F. Padovan 		del_timer(&chan->monitor_timer);
37796a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
37800a708f8fSGustavo F. Padovan 			__mod_retrans_timer();
3781525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
37820a708f8fSGustavo F. Padovan 	}
37830a708f8fSGustavo F. Padovan 
37840a708f8fSGustavo F. Padovan 	switch (rx_control & L2CAP_CTRL_SUPERVISE) {
37850a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_RCV_READY:
3786525cd185SGustavo F. Padovan 		l2cap_data_channel_rrframe(chan, rx_control);
37870a708f8fSGustavo F. Padovan 		break;
37880a708f8fSGustavo F. Padovan 
37890a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_REJECT:
3790525cd185SGustavo F. Padovan 		l2cap_data_channel_rejframe(chan, rx_control);
37910a708f8fSGustavo F. Padovan 		break;
37920a708f8fSGustavo F. Padovan 
37930a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_SELECT_REJECT:
3794525cd185SGustavo F. Padovan 		l2cap_data_channel_srejframe(chan, rx_control);
37950a708f8fSGustavo F. Padovan 		break;
37960a708f8fSGustavo F. Padovan 
37970a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_RCV_NOT_READY:
3798525cd185SGustavo F. Padovan 		l2cap_data_channel_rnrframe(chan, rx_control);
37990a708f8fSGustavo F. Padovan 		break;
38000a708f8fSGustavo F. Padovan 	}
38010a708f8fSGustavo F. Padovan 
38020a708f8fSGustavo F. Padovan 	kfree_skb(skb);
38030a708f8fSGustavo F. Padovan 	return 0;
38040a708f8fSGustavo F. Padovan }
38050a708f8fSGustavo F. Padovan 
38060a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
38070a708f8fSGustavo F. Padovan {
3808525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
38090a708f8fSGustavo F. Padovan 	u16 control;
38100a708f8fSGustavo F. Padovan 	u8 req_seq;
38110a708f8fSGustavo F. Padovan 	int len, next_tx_seq_offset, req_seq_offset;
38120a708f8fSGustavo F. Padovan 
38130a708f8fSGustavo F. Padovan 	control = get_unaligned_le16(skb->data);
38140a708f8fSGustavo F. Padovan 	skb_pull(skb, 2);
38150a708f8fSGustavo F. Padovan 	len = skb->len;
38160a708f8fSGustavo F. Padovan 
38170a708f8fSGustavo F. Padovan 	/*
38180a708f8fSGustavo F. Padovan 	 * We can just drop the corrupted I-frame here.
38190a708f8fSGustavo F. Padovan 	 * Receiver will miss it and start proper recovery
38200a708f8fSGustavo F. Padovan 	 * procedures and ask retransmission.
38210a708f8fSGustavo F. Padovan 	 */
382247d1ec61SGustavo F. Padovan 	if (l2cap_check_fcs(chan, skb))
38230a708f8fSGustavo F. Padovan 		goto drop;
38240a708f8fSGustavo F. Padovan 
38250a708f8fSGustavo F. Padovan 	if (__is_sar_start(control) && __is_iframe(control))
38260a708f8fSGustavo F. Padovan 		len -= 2;
38270a708f8fSGustavo F. Padovan 
382847d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
38290a708f8fSGustavo F. Padovan 		len -= 2;
38300a708f8fSGustavo F. Padovan 
383147d1ec61SGustavo F. Padovan 	if (len > chan->mps) {
38328c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38330a708f8fSGustavo F. Padovan 		goto drop;
38340a708f8fSGustavo F. Padovan 	}
38350a708f8fSGustavo F. Padovan 
38360a708f8fSGustavo F. Padovan 	req_seq = __get_reqseq(control);
383742e5c802SGustavo F. Padovan 	req_seq_offset = (req_seq - chan->expected_ack_seq) % 64;
38380a708f8fSGustavo F. Padovan 	if (req_seq_offset < 0)
38390a708f8fSGustavo F. Padovan 		req_seq_offset += 64;
38400a708f8fSGustavo F. Padovan 
38410a708f8fSGustavo F. Padovan 	next_tx_seq_offset =
384242e5c802SGustavo F. Padovan 		(chan->next_tx_seq - chan->expected_ack_seq) % 64;
38430a708f8fSGustavo F. Padovan 	if (next_tx_seq_offset < 0)
38440a708f8fSGustavo F. Padovan 		next_tx_seq_offset += 64;
38450a708f8fSGustavo F. Padovan 
38460a708f8fSGustavo F. Padovan 	/* check for invalid req-seq */
38470a708f8fSGustavo F. Padovan 	if (req_seq_offset > next_tx_seq_offset) {
38488c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38490a708f8fSGustavo F. Padovan 		goto drop;
38500a708f8fSGustavo F. Padovan 	}
38510a708f8fSGustavo F. Padovan 
38520a708f8fSGustavo F. Padovan 	if (__is_iframe(control)) {
38530a708f8fSGustavo F. Padovan 		if (len < 0) {
38548c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38550a708f8fSGustavo F. Padovan 			goto drop;
38560a708f8fSGustavo F. Padovan 		}
38570a708f8fSGustavo F. Padovan 
3858525cd185SGustavo F. Padovan 		l2cap_data_channel_iframe(chan, control, skb);
38590a708f8fSGustavo F. Padovan 	} else {
38600a708f8fSGustavo F. Padovan 		if (len != 0) {
38610a708f8fSGustavo F. Padovan 			BT_ERR("%d", len);
38628c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38630a708f8fSGustavo F. Padovan 			goto drop;
38640a708f8fSGustavo F. Padovan 		}
38650a708f8fSGustavo F. Padovan 
3866525cd185SGustavo F. Padovan 		l2cap_data_channel_sframe(chan, control, skb);
38670a708f8fSGustavo F. Padovan 	}
38680a708f8fSGustavo F. Padovan 
38690a708f8fSGustavo F. Padovan 	return 0;
38700a708f8fSGustavo F. Padovan 
38710a708f8fSGustavo F. Padovan drop:
38720a708f8fSGustavo F. Padovan 	kfree_skb(skb);
38730a708f8fSGustavo F. Padovan 	return 0;
38740a708f8fSGustavo F. Padovan }
38750a708f8fSGustavo F. Padovan 
38760a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
38770a708f8fSGustavo F. Padovan {
387848454079SGustavo F. Padovan 	struct l2cap_chan *chan;
3879bf734843SDavid S. Miller 	struct sock *sk = NULL;
38800a708f8fSGustavo F. Padovan 	u16 control;
38810a708f8fSGustavo F. Padovan 	u8 tx_seq;
38820a708f8fSGustavo F. Padovan 	int len;
38830a708f8fSGustavo F. Padovan 
3884baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, cid);
388548454079SGustavo F. Padovan 	if (!chan) {
38860a708f8fSGustavo F. Padovan 		BT_DBG("unknown cid 0x%4.4x", cid);
38870a708f8fSGustavo F. Padovan 		goto drop;
38880a708f8fSGustavo F. Padovan 	}
38890a708f8fSGustavo F. Padovan 
389048454079SGustavo F. Padovan 	sk = chan->sk;
38910a708f8fSGustavo F. Padovan 
389249208c9cSGustavo F. Padovan 	BT_DBG("chan %p, len %d", chan, skb->len);
38930a708f8fSGustavo F. Padovan 
389489bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
38950a708f8fSGustavo F. Padovan 		goto drop;
38960a708f8fSGustavo F. Padovan 
38970c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
38980a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
38990a708f8fSGustavo F. Padovan 		/* If socket recv buffers overflows we drop data here
39000a708f8fSGustavo F. Padovan 		 * which is *bad* because L2CAP has to be reliable.
39010a708f8fSGustavo F. Padovan 		 * But we don't have any other choice. L2CAP doesn't
39020a708f8fSGustavo F. Padovan 		 * provide flow control mechanism. */
39030a708f8fSGustavo F. Padovan 
39040c1bc5c6SGustavo F. Padovan 		if (chan->imtu < skb->len)
39050a708f8fSGustavo F. Padovan 			goto drop;
39060a708f8fSGustavo F. Padovan 
390723070494SGustavo F. Padovan 		if (!chan->ops->recv(chan->data, skb))
39080a708f8fSGustavo F. Padovan 			goto done;
39090a708f8fSGustavo F. Padovan 		break;
39100a708f8fSGustavo F. Padovan 
39110a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
39120a708f8fSGustavo F. Padovan 		if (!sock_owned_by_user(sk)) {
39130a708f8fSGustavo F. Padovan 			l2cap_ertm_data_rcv(sk, skb);
39140a708f8fSGustavo F. Padovan 		} else {
39150a708f8fSGustavo F. Padovan 			if (sk_add_backlog(sk, skb))
39160a708f8fSGustavo F. Padovan 				goto drop;
39170a708f8fSGustavo F. Padovan 		}
39180a708f8fSGustavo F. Padovan 
39190a708f8fSGustavo F. Padovan 		goto done;
39200a708f8fSGustavo F. Padovan 
39210a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
39220a708f8fSGustavo F. Padovan 		control = get_unaligned_le16(skb->data);
39230a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
39240a708f8fSGustavo F. Padovan 		len = skb->len;
39250a708f8fSGustavo F. Padovan 
392647d1ec61SGustavo F. Padovan 		if (l2cap_check_fcs(chan, skb))
39270a708f8fSGustavo F. Padovan 			goto drop;
39280a708f8fSGustavo F. Padovan 
39290a708f8fSGustavo F. Padovan 		if (__is_sar_start(control))
39300a708f8fSGustavo F. Padovan 			len -= 2;
39310a708f8fSGustavo F. Padovan 
393247d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16)
39330a708f8fSGustavo F. Padovan 			len -= 2;
39340a708f8fSGustavo F. Padovan 
393547d1ec61SGustavo F. Padovan 		if (len > chan->mps || len < 0 || __is_sframe(control))
39360a708f8fSGustavo F. Padovan 			goto drop;
39370a708f8fSGustavo F. Padovan 
39380a708f8fSGustavo F. Padovan 		tx_seq = __get_txseq(control);
39390a708f8fSGustavo F. Padovan 
394042e5c802SGustavo F. Padovan 		if (chan->expected_tx_seq == tx_seq)
394142e5c802SGustavo F. Padovan 			chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
39420a708f8fSGustavo F. Padovan 		else
394342e5c802SGustavo F. Padovan 			chan->expected_tx_seq = (tx_seq + 1) % 64;
39440a708f8fSGustavo F. Padovan 
3945525cd185SGustavo F. Padovan 		l2cap_streaming_reassembly_sdu(chan, skb, control);
39460a708f8fSGustavo F. Padovan 
39470a708f8fSGustavo F. Padovan 		goto done;
39480a708f8fSGustavo F. Padovan 
39490a708f8fSGustavo F. Padovan 	default:
39500c1bc5c6SGustavo F. Padovan 		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
39510a708f8fSGustavo F. Padovan 		break;
39520a708f8fSGustavo F. Padovan 	}
39530a708f8fSGustavo F. Padovan 
39540a708f8fSGustavo F. Padovan drop:
39550a708f8fSGustavo F. Padovan 	kfree_skb(skb);
39560a708f8fSGustavo F. Padovan 
39570a708f8fSGustavo F. Padovan done:
39580a708f8fSGustavo F. Padovan 	if (sk)
39590a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
39600a708f8fSGustavo F. Padovan 
39610a708f8fSGustavo F. Padovan 	return 0;
39620a708f8fSGustavo F. Padovan }
39630a708f8fSGustavo F. Padovan 
39640a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
39650a708f8fSGustavo F. Padovan {
39666dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
396723691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
39680a708f8fSGustavo F. Padovan 
396923691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_psm(0, psm, conn->src);
397023691d75SGustavo F. Padovan 	if (!chan)
39710a708f8fSGustavo F. Padovan 		goto drop;
39720a708f8fSGustavo F. Padovan 
397323691d75SGustavo F. Padovan 	sk = chan->sk;
397423691d75SGustavo F. Padovan 
39750a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
39760a708f8fSGustavo F. Padovan 
39770a708f8fSGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
39780a708f8fSGustavo F. Padovan 
397989bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
39800a708f8fSGustavo F. Padovan 		goto drop;
39810a708f8fSGustavo F. Padovan 
39820c1bc5c6SGustavo F. Padovan 	if (l2cap_pi(sk)->chan->imtu < skb->len)
39830a708f8fSGustavo F. Padovan 		goto drop;
39840a708f8fSGustavo F. Padovan 
398523070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
39860a708f8fSGustavo F. Padovan 		goto done;
39870a708f8fSGustavo F. Padovan 
39880a708f8fSGustavo F. Padovan drop:
39890a708f8fSGustavo F. Padovan 	kfree_skb(skb);
39900a708f8fSGustavo F. Padovan 
39910a708f8fSGustavo F. Padovan done:
39920a708f8fSGustavo F. Padovan 	if (sk)
39930a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
39940a708f8fSGustavo F. Padovan 	return 0;
39950a708f8fSGustavo F. Padovan }
39960a708f8fSGustavo F. Padovan 
39979f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb)
39989f69bda6SGustavo F. Padovan {
39996dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
400023691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
40019f69bda6SGustavo F. Padovan 
400223691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_scid(0, cid, conn->src);
400323691d75SGustavo F. Padovan 	if (!chan)
40049f69bda6SGustavo F. Padovan 		goto drop;
40059f69bda6SGustavo F. Padovan 
400623691d75SGustavo F. Padovan 	sk = chan->sk;
400723691d75SGustavo F. Padovan 
40089f69bda6SGustavo F. Padovan 	bh_lock_sock(sk);
40099f69bda6SGustavo F. Padovan 
40109f69bda6SGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
40119f69bda6SGustavo F. Padovan 
401289bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
40139f69bda6SGustavo F. Padovan 		goto drop;
40149f69bda6SGustavo F. Padovan 
40150c1bc5c6SGustavo F. Padovan 	if (l2cap_pi(sk)->chan->imtu < skb->len)
40169f69bda6SGustavo F. Padovan 		goto drop;
40179f69bda6SGustavo F. Padovan 
401823070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
40199f69bda6SGustavo F. Padovan 		goto done;
40209f69bda6SGustavo F. Padovan 
40219f69bda6SGustavo F. Padovan drop:
40229f69bda6SGustavo F. Padovan 	kfree_skb(skb);
40239f69bda6SGustavo F. Padovan 
40249f69bda6SGustavo F. Padovan done:
40259f69bda6SGustavo F. Padovan 	if (sk)
40269f69bda6SGustavo F. Padovan 		bh_unlock_sock(sk);
40279f69bda6SGustavo F. Padovan 	return 0;
40289f69bda6SGustavo F. Padovan }
40299f69bda6SGustavo F. Padovan 
40300a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
40310a708f8fSGustavo F. Padovan {
40320a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh = (void *) skb->data;
40330a708f8fSGustavo F. Padovan 	u16 cid, len;
40340a708f8fSGustavo F. Padovan 	__le16 psm;
40350a708f8fSGustavo F. Padovan 
40360a708f8fSGustavo F. Padovan 	skb_pull(skb, L2CAP_HDR_SIZE);
40370a708f8fSGustavo F. Padovan 	cid = __le16_to_cpu(lh->cid);
40380a708f8fSGustavo F. Padovan 	len = __le16_to_cpu(lh->len);
40390a708f8fSGustavo F. Padovan 
40400a708f8fSGustavo F. Padovan 	if (len != skb->len) {
40410a708f8fSGustavo F. Padovan 		kfree_skb(skb);
40420a708f8fSGustavo F. Padovan 		return;
40430a708f8fSGustavo F. Padovan 	}
40440a708f8fSGustavo F. Padovan 
40450a708f8fSGustavo F. Padovan 	BT_DBG("len %d, cid 0x%4.4x", len, cid);
40460a708f8fSGustavo F. Padovan 
40470a708f8fSGustavo F. Padovan 	switch (cid) {
40483300d9a9SClaudio Takahasi 	case L2CAP_CID_LE_SIGNALING:
40490a708f8fSGustavo F. Padovan 	case L2CAP_CID_SIGNALING:
40500a708f8fSGustavo F. Padovan 		l2cap_sig_channel(conn, skb);
40510a708f8fSGustavo F. Padovan 		break;
40520a708f8fSGustavo F. Padovan 
40530a708f8fSGustavo F. Padovan 	case L2CAP_CID_CONN_LESS:
40540a708f8fSGustavo F. Padovan 		psm = get_unaligned_le16(skb->data);
40550a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
40560a708f8fSGustavo F. Padovan 		l2cap_conless_channel(conn, psm, skb);
40570a708f8fSGustavo F. Padovan 		break;
40580a708f8fSGustavo F. Padovan 
40599f69bda6SGustavo F. Padovan 	case L2CAP_CID_LE_DATA:
40609f69bda6SGustavo F. Padovan 		l2cap_att_channel(conn, cid, skb);
40619f69bda6SGustavo F. Padovan 		break;
40629f69bda6SGustavo F. Padovan 
40630a708f8fSGustavo F. Padovan 	default:
40640a708f8fSGustavo F. Padovan 		l2cap_data_channel(conn, cid, skb);
40650a708f8fSGustavo F. Padovan 		break;
40660a708f8fSGustavo F. Padovan 	}
40670a708f8fSGustavo F. Padovan }
40680a708f8fSGustavo F. Padovan 
40690a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */
40700a708f8fSGustavo F. Padovan 
40710a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
40720a708f8fSGustavo F. Padovan {
40730a708f8fSGustavo F. Padovan 	int exact = 0, lm1 = 0, lm2 = 0;
407423691d75SGustavo F. Padovan 	struct l2cap_chan *c;
40750a708f8fSGustavo F. Padovan 
40760a708f8fSGustavo F. Padovan 	if (type != ACL_LINK)
40770a708f8fSGustavo F. Padovan 		return -EINVAL;
40780a708f8fSGustavo F. Padovan 
40790a708f8fSGustavo F. Padovan 	BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
40800a708f8fSGustavo F. Padovan 
40810a708f8fSGustavo F. Padovan 	/* Find listening sockets and check their link_mode */
408223691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
408323691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
408423691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
40854343478fSGustavo F. Padovan 
408689bc500eSGustavo F. Padovan 		if (c->state != BT_LISTEN)
40870a708f8fSGustavo F. Padovan 			continue;
40880a708f8fSGustavo F. Padovan 
40890a708f8fSGustavo F. Padovan 		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
40900a708f8fSGustavo F. Padovan 			lm1 |= HCI_LM_ACCEPT;
409123691d75SGustavo F. Padovan 			if (c->role_switch)
40920a708f8fSGustavo F. Padovan 				lm1 |= HCI_LM_MASTER;
40930a708f8fSGustavo F. Padovan 			exact++;
40940a708f8fSGustavo F. Padovan 		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
40950a708f8fSGustavo F. Padovan 			lm2 |= HCI_LM_ACCEPT;
409623691d75SGustavo F. Padovan 			if (c->role_switch)
40970a708f8fSGustavo F. Padovan 				lm2 |= HCI_LM_MASTER;
40980a708f8fSGustavo F. Padovan 		}
40990a708f8fSGustavo F. Padovan 	}
410023691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
41010a708f8fSGustavo F. Padovan 
41020a708f8fSGustavo F. Padovan 	return exact ? lm1 : lm2;
41030a708f8fSGustavo F. Padovan }
41040a708f8fSGustavo F. Padovan 
41050a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
41060a708f8fSGustavo F. Padovan {
41070a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
41080a708f8fSGustavo F. Padovan 
41090a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
41100a708f8fSGustavo F. Padovan 
4111acd7d370SVille Tervo 	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
41120a708f8fSGustavo F. Padovan 		return -EINVAL;
41130a708f8fSGustavo F. Padovan 
41140a708f8fSGustavo F. Padovan 	if (!status) {
41150a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, status);
41160a708f8fSGustavo F. Padovan 		if (conn)
41170a708f8fSGustavo F. Padovan 			l2cap_conn_ready(conn);
41180a708f8fSGustavo F. Padovan 	} else
41190a708f8fSGustavo F. Padovan 		l2cap_conn_del(hcon, bt_err(status));
41200a708f8fSGustavo F. Padovan 
41210a708f8fSGustavo F. Padovan 	return 0;
41220a708f8fSGustavo F. Padovan }
41230a708f8fSGustavo F. Padovan 
41240a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon)
41250a708f8fSGustavo F. Padovan {
41260a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
41270a708f8fSGustavo F. Padovan 
41280a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p", hcon);
41290a708f8fSGustavo F. Padovan 
41300a708f8fSGustavo F. Padovan 	if (hcon->type != ACL_LINK || !conn)
41310a708f8fSGustavo F. Padovan 		return 0x13;
41320a708f8fSGustavo F. Padovan 
41330a708f8fSGustavo F. Padovan 	return conn->disc_reason;
41340a708f8fSGustavo F. Padovan }
41350a708f8fSGustavo F. Padovan 
41360a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
41370a708f8fSGustavo F. Padovan {
41380a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p reason %d", hcon, reason);
41390a708f8fSGustavo F. Padovan 
4140acd7d370SVille Tervo 	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
41410a708f8fSGustavo F. Padovan 		return -EINVAL;
41420a708f8fSGustavo F. Padovan 
41430a708f8fSGustavo F. Padovan 	l2cap_conn_del(hcon, bt_err(reason));
41440a708f8fSGustavo F. Padovan 
41450a708f8fSGustavo F. Padovan 	return 0;
41460a708f8fSGustavo F. Padovan }
41470a708f8fSGustavo F. Padovan 
41484343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
41490a708f8fSGustavo F. Padovan {
4150715ec005SGustavo F. Padovan 	if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
41510a708f8fSGustavo F. Padovan 		return;
41520a708f8fSGustavo F. Padovan 
41530a708f8fSGustavo F. Padovan 	if (encrypt == 0x00) {
41544343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM) {
4155ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
4156ab07801dSGustavo F. Padovan 			l2cap_chan_set_timer(chan, HZ * 5);
41574343478fSGustavo F. Padovan 		} else if (chan->sec_level == BT_SECURITY_HIGH)
41580f852724SGustavo F. Padovan 			l2cap_chan_close(chan, ECONNREFUSED);
41590a708f8fSGustavo F. Padovan 	} else {
41604343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM)
4161ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
41620a708f8fSGustavo F. Padovan 	}
41630a708f8fSGustavo F. Padovan }
41640a708f8fSGustavo F. Padovan 
41650a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
41660a708f8fSGustavo F. Padovan {
41670a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
416848454079SGustavo F. Padovan 	struct l2cap_chan *chan;
41690a708f8fSGustavo F. Padovan 
41700a708f8fSGustavo F. Padovan 	if (!conn)
41710a708f8fSGustavo F. Padovan 		return 0;
41720a708f8fSGustavo F. Padovan 
41730a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
41740a708f8fSGustavo F. Padovan 
4175baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
41760a708f8fSGustavo F. Padovan 
4177baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
417848454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
4179baa7e1faSGustavo F. Padovan 
41800a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
41810a708f8fSGustavo F. Padovan 
4182b4450035SGustavo F. Padovan 		if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) {
41830a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
41840a708f8fSGustavo F. Padovan 			continue;
41850a708f8fSGustavo F. Padovan 		}
41860a708f8fSGustavo F. Padovan 
418789bc500eSGustavo F. Padovan 		if (!status && (chan->state == BT_CONNECTED ||
418889bc500eSGustavo F. Padovan 						chan->state == BT_CONFIG)) {
41894343478fSGustavo F. Padovan 			l2cap_check_encryption(chan, encrypt);
41900a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
41910a708f8fSGustavo F. Padovan 			continue;
41920a708f8fSGustavo F. Padovan 		}
41930a708f8fSGustavo F. Padovan 
419489bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
41950a708f8fSGustavo F. Padovan 			if (!status) {
41960a708f8fSGustavo F. Padovan 				struct l2cap_conn_req req;
4197fe4128e0SGustavo F. Padovan 				req.scid = cpu_to_le16(chan->scid);
4198fe4128e0SGustavo F. Padovan 				req.psm  = chan->psm;
41990a708f8fSGustavo F. Padovan 
4200fc7f8a7eSGustavo F. Padovan 				chan->ident = l2cap_get_ident(conn);
4201b4450035SGustavo F. Padovan 				chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
42020a708f8fSGustavo F. Padovan 
4203fc7f8a7eSGustavo F. Padovan 				l2cap_send_cmd(conn, chan->ident,
42040a708f8fSGustavo F. Padovan 					L2CAP_CONN_REQ, sizeof(req), &req);
42050a708f8fSGustavo F. Padovan 			} else {
4206ab07801dSGustavo F. Padovan 				l2cap_chan_clear_timer(chan);
4207ab07801dSGustavo F. Padovan 				l2cap_chan_set_timer(chan, HZ / 10);
42080a708f8fSGustavo F. Padovan 			}
420989bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
42100a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
42110a708f8fSGustavo F. Padovan 			__u16 result;
42120a708f8fSGustavo F. Padovan 
42130a708f8fSGustavo F. Padovan 			if (!status) {
421489bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_CONFIG);
42150a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SUCCESS;
42160a708f8fSGustavo F. Padovan 			} else {
421789bc500eSGustavo F. Padovan 				l2cap_state_change(chan, BT_DISCONN);
4218ab07801dSGustavo F. Padovan 				l2cap_chan_set_timer(chan, HZ / 10);
42190a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SEC_BLOCK;
42200a708f8fSGustavo F. Padovan 			}
42210a708f8fSGustavo F. Padovan 
4222fe4128e0SGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4223fe4128e0SGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
42240a708f8fSGustavo F. Padovan 			rsp.result = cpu_to_le16(result);
42250a708f8fSGustavo F. Padovan 			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4226fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4227fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
42280a708f8fSGustavo F. Padovan 		}
42290a708f8fSGustavo F. Padovan 
42300a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
42310a708f8fSGustavo F. Padovan 	}
42320a708f8fSGustavo F. Padovan 
4233baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
42340a708f8fSGustavo F. Padovan 
42350a708f8fSGustavo F. Padovan 	return 0;
42360a708f8fSGustavo F. Padovan }
42370a708f8fSGustavo F. Padovan 
42380a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
42390a708f8fSGustavo F. Padovan {
42400a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
42410a708f8fSGustavo F. Padovan 
42420a708f8fSGustavo F. Padovan 	if (!conn)
42430a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, 0);
42440a708f8fSGustavo F. Padovan 
42450a708f8fSGustavo F. Padovan 	if (!conn)
42460a708f8fSGustavo F. Padovan 		goto drop;
42470a708f8fSGustavo F. Padovan 
42480a708f8fSGustavo F. Padovan 	BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
42490a708f8fSGustavo F. Padovan 
42500a708f8fSGustavo F. Padovan 	if (!(flags & ACL_CONT)) {
42510a708f8fSGustavo F. Padovan 		struct l2cap_hdr *hdr;
425248454079SGustavo F. Padovan 		struct l2cap_chan *chan;
42530a708f8fSGustavo F. Padovan 		u16 cid;
42540a708f8fSGustavo F. Padovan 		int len;
42550a708f8fSGustavo F. Padovan 
42560a708f8fSGustavo F. Padovan 		if (conn->rx_len) {
42570a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected start frame (len %d)", skb->len);
42580a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
42590a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
42600a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
42610a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
42620a708f8fSGustavo F. Padovan 		}
42630a708f8fSGustavo F. Padovan 
42640a708f8fSGustavo F. Padovan 		/* Start fragment always begin with Basic L2CAP header */
42650a708f8fSGustavo F. Padovan 		if (skb->len < L2CAP_HDR_SIZE) {
42660a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too short (len %d)", skb->len);
42670a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
42680a708f8fSGustavo F. Padovan 			goto drop;
42690a708f8fSGustavo F. Padovan 		}
42700a708f8fSGustavo F. Padovan 
42710a708f8fSGustavo F. Padovan 		hdr = (struct l2cap_hdr *) skb->data;
42720a708f8fSGustavo F. Padovan 		len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
42730a708f8fSGustavo F. Padovan 		cid = __le16_to_cpu(hdr->cid);
42740a708f8fSGustavo F. Padovan 
42750a708f8fSGustavo F. Padovan 		if (len == skb->len) {
42760a708f8fSGustavo F. Padovan 			/* Complete frame received */
42770a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, skb);
42780a708f8fSGustavo F. Padovan 			return 0;
42790a708f8fSGustavo F. Padovan 		}
42800a708f8fSGustavo F. Padovan 
42810a708f8fSGustavo F. Padovan 		BT_DBG("Start: total len %d, frag len %d", len, skb->len);
42820a708f8fSGustavo F. Padovan 
42830a708f8fSGustavo F. Padovan 		if (skb->len > len) {
42840a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too long (len %d, expected len %d)",
42850a708f8fSGustavo F. Padovan 				skb->len, len);
42860a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
42870a708f8fSGustavo F. Padovan 			goto drop;
42880a708f8fSGustavo F. Padovan 		}
42890a708f8fSGustavo F. Padovan 
4290baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, cid);
42910a708f8fSGustavo F. Padovan 
429248454079SGustavo F. Padovan 		if (chan && chan->sk) {
429348454079SGustavo F. Padovan 			struct sock *sk = chan->sk;
429448454079SGustavo F. Padovan 
42950c1bc5c6SGustavo F. Padovan 			if (chan->imtu < len - L2CAP_HDR_SIZE) {
429648454079SGustavo F. Padovan 				BT_ERR("Frame exceeding recv MTU (len %d, "
429748454079SGustavo F. Padovan 							"MTU %d)", len,
42980c1bc5c6SGustavo F. Padovan 							chan->imtu);
42990a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
43000a708f8fSGustavo F. Padovan 				l2cap_conn_unreliable(conn, ECOMM);
43010a708f8fSGustavo F. Padovan 				goto drop;
43020a708f8fSGustavo F. Padovan 			}
43030a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
430448454079SGustavo F. Padovan 		}
43050a708f8fSGustavo F. Padovan 
43060a708f8fSGustavo F. Padovan 		/* Allocate skb for the complete frame (with header) */
43070a708f8fSGustavo F. Padovan 		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
43080a708f8fSGustavo F. Padovan 		if (!conn->rx_skb)
43090a708f8fSGustavo F. Padovan 			goto drop;
43100a708f8fSGustavo F. Padovan 
43110a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
43120a708f8fSGustavo F. Padovan 								skb->len);
43130a708f8fSGustavo F. Padovan 		conn->rx_len = len - skb->len;
43140a708f8fSGustavo F. Padovan 	} else {
43150a708f8fSGustavo F. Padovan 		BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
43160a708f8fSGustavo F. Padovan 
43170a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
43180a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected continuation frame (len %d)", skb->len);
43190a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
43200a708f8fSGustavo F. Padovan 			goto drop;
43210a708f8fSGustavo F. Padovan 		}
43220a708f8fSGustavo F. Padovan 
43230a708f8fSGustavo F. Padovan 		if (skb->len > conn->rx_len) {
43240a708f8fSGustavo F. Padovan 			BT_ERR("Fragment is too long (len %d, expected %d)",
43250a708f8fSGustavo F. Padovan 					skb->len, conn->rx_len);
43260a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
43270a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
43280a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
43290a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
43300a708f8fSGustavo F. Padovan 			goto drop;
43310a708f8fSGustavo F. Padovan 		}
43320a708f8fSGustavo F. Padovan 
43330a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
43340a708f8fSGustavo F. Padovan 								skb->len);
43350a708f8fSGustavo F. Padovan 		conn->rx_len -= skb->len;
43360a708f8fSGustavo F. Padovan 
43370a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
43380a708f8fSGustavo F. Padovan 			/* Complete frame received */
43390a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, conn->rx_skb);
43400a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
43410a708f8fSGustavo F. Padovan 		}
43420a708f8fSGustavo F. Padovan 	}
43430a708f8fSGustavo F. Padovan 
43440a708f8fSGustavo F. Padovan drop:
43450a708f8fSGustavo F. Padovan 	kfree_skb(skb);
43460a708f8fSGustavo F. Padovan 	return 0;
43470a708f8fSGustavo F. Padovan }
43480a708f8fSGustavo F. Padovan 
43490a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p)
43500a708f8fSGustavo F. Padovan {
435123691d75SGustavo F. Padovan 	struct l2cap_chan *c;
43520a708f8fSGustavo F. Padovan 
435323691d75SGustavo F. Padovan 	read_lock_bh(&chan_list_lock);
43540a708f8fSGustavo F. Padovan 
435523691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
435623691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
43570a708f8fSGustavo F. Padovan 
4358903d343eSGustavo F. Padovan 		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
43590a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->src),
43600a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->dst),
436189bc500eSGustavo F. Padovan 					c->state, __le16_to_cpu(c->psm),
436223691d75SGustavo F. Padovan 					c->scid, c->dcid, c->imtu, c->omtu,
436323691d75SGustavo F. Padovan 					c->sec_level, c->mode);
43640a708f8fSGustavo F. Padovan 	}
43650a708f8fSGustavo F. Padovan 
436623691d75SGustavo F. Padovan 	read_unlock_bh(&chan_list_lock);
43670a708f8fSGustavo F. Padovan 
43680a708f8fSGustavo F. Padovan 	return 0;
43690a708f8fSGustavo F. Padovan }
43700a708f8fSGustavo F. Padovan 
43710a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file)
43720a708f8fSGustavo F. Padovan {
43730a708f8fSGustavo F. Padovan 	return single_open(file, l2cap_debugfs_show, inode->i_private);
43740a708f8fSGustavo F. Padovan }
43750a708f8fSGustavo F. Padovan 
43760a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = {
43770a708f8fSGustavo F. Padovan 	.open		= l2cap_debugfs_open,
43780a708f8fSGustavo F. Padovan 	.read		= seq_read,
43790a708f8fSGustavo F. Padovan 	.llseek		= seq_lseek,
43800a708f8fSGustavo F. Padovan 	.release	= single_release,
43810a708f8fSGustavo F. Padovan };
43820a708f8fSGustavo F. Padovan 
43830a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs;
43840a708f8fSGustavo F. Padovan 
43850a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = {
43860a708f8fSGustavo F. Padovan 	.name		= "L2CAP",
43870a708f8fSGustavo F. Padovan 	.id		= HCI_PROTO_L2CAP,
43880a708f8fSGustavo F. Padovan 	.connect_ind	= l2cap_connect_ind,
43890a708f8fSGustavo F. Padovan 	.connect_cfm	= l2cap_connect_cfm,
43900a708f8fSGustavo F. Padovan 	.disconn_ind	= l2cap_disconn_ind,
43910a708f8fSGustavo F. Padovan 	.disconn_cfm	= l2cap_disconn_cfm,
43920a708f8fSGustavo F. Padovan 	.security_cfm	= l2cap_security_cfm,
43930a708f8fSGustavo F. Padovan 	.recv_acldata	= l2cap_recv_acldata
43940a708f8fSGustavo F. Padovan };
43950a708f8fSGustavo F. Padovan 
439664274518SGustavo F. Padovan int __init l2cap_init(void)
43970a708f8fSGustavo F. Padovan {
43980a708f8fSGustavo F. Padovan 	int err;
43990a708f8fSGustavo F. Padovan 
4400bb58f747SGustavo F. Padovan 	err = l2cap_init_sockets();
44010a708f8fSGustavo F. Padovan 	if (err < 0)
44020a708f8fSGustavo F. Padovan 		return err;
44030a708f8fSGustavo F. Padovan 
44040a708f8fSGustavo F. Padovan 	_busy_wq = create_singlethread_workqueue("l2cap");
44050a708f8fSGustavo F. Padovan 	if (!_busy_wq) {
4406bb58f747SGustavo F. Padovan 		err = -ENOMEM;
44070a708f8fSGustavo F. Padovan 		goto error;
44080a708f8fSGustavo F. Padovan 	}
44090a708f8fSGustavo F. Padovan 
44100a708f8fSGustavo F. Padovan 	err = hci_register_proto(&l2cap_hci_proto);
44110a708f8fSGustavo F. Padovan 	if (err < 0) {
44120a708f8fSGustavo F. Padovan 		BT_ERR("L2CAP protocol registration failed");
44130a708f8fSGustavo F. Padovan 		bt_sock_unregister(BTPROTO_L2CAP);
44140a708f8fSGustavo F. Padovan 		goto error;
44150a708f8fSGustavo F. Padovan 	}
44160a708f8fSGustavo F. Padovan 
44170a708f8fSGustavo F. Padovan 	if (bt_debugfs) {
44180a708f8fSGustavo F. Padovan 		l2cap_debugfs = debugfs_create_file("l2cap", 0444,
44190a708f8fSGustavo F. Padovan 					bt_debugfs, NULL, &l2cap_debugfs_fops);
44200a708f8fSGustavo F. Padovan 		if (!l2cap_debugfs)
44210a708f8fSGustavo F. Padovan 			BT_ERR("Failed to create L2CAP debug file");
44220a708f8fSGustavo F. Padovan 	}
44230a708f8fSGustavo F. Padovan 
44240a708f8fSGustavo F. Padovan 	return 0;
44250a708f8fSGustavo F. Padovan 
44260a708f8fSGustavo F. Padovan error:
44270a708f8fSGustavo F. Padovan 	destroy_workqueue(_busy_wq);
4428bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
44290a708f8fSGustavo F. Padovan 	return err;
44300a708f8fSGustavo F. Padovan }
44310a708f8fSGustavo F. Padovan 
443264274518SGustavo F. Padovan void l2cap_exit(void)
44330a708f8fSGustavo F. Padovan {
44340a708f8fSGustavo F. Padovan 	debugfs_remove(l2cap_debugfs);
44350a708f8fSGustavo F. Padovan 
44360a708f8fSGustavo F. Padovan 	flush_workqueue(_busy_wq);
44370a708f8fSGustavo F. Padovan 	destroy_workqueue(_busy_wq);
44380a708f8fSGustavo F. Padovan 
44390a708f8fSGustavo F. Padovan 	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
44400a708f8fSGustavo F. Padovan 		BT_ERR("L2CAP protocol unregistration failed");
44410a708f8fSGustavo F. Padovan 
4442bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
44430a708f8fSGustavo F. Padovan }
44440a708f8fSGustavo F. Padovan 
44450a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644);
44460a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
4447