xref: /openbmc/linux/net/bluetooth/l2cap_core.c (revision 80808e43)
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 ---- */
81baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
820a708f8fSGustavo F. Padovan {
8348454079SGustavo F. Padovan 	struct l2cap_chan *c;
84baa7e1faSGustavo F. Padovan 
85baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
86fe4128e0SGustavo F. Padovan 		if (c->dcid == cid)
8748454079SGustavo F. Padovan 			return c;
880a708f8fSGustavo F. Padovan 	}
89baa7e1faSGustavo F. Padovan 	return NULL;
90baa7e1faSGustavo F. Padovan 
91baa7e1faSGustavo F. Padovan }
920a708f8fSGustavo F. Padovan 
93baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(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->scid == cid)
9948454079SGustavo F. Padovan 			return c;
1000a708f8fSGustavo F. Padovan 	}
101baa7e1faSGustavo F. Padovan 	return NULL;
102baa7e1faSGustavo F. Padovan }
1030a708f8fSGustavo F. Padovan 
1040a708f8fSGustavo F. Padovan /* Find channel with given SCID.
1050a708f8fSGustavo F. Padovan  * Returns locked socket */
106baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
1070a708f8fSGustavo F. Padovan {
10848454079SGustavo F. Padovan 	struct l2cap_chan *c;
109baa7e1faSGustavo F. Padovan 
110baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
111baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_scid(conn, cid);
11248454079SGustavo F. Padovan 	if (c)
11348454079SGustavo F. Padovan 		bh_lock_sock(c->sk);
114baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
11548454079SGustavo F. Padovan 	return c;
1160a708f8fSGustavo F. Padovan }
1170a708f8fSGustavo F. Padovan 
118baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1190a708f8fSGustavo F. Padovan {
12048454079SGustavo F. Padovan 	struct l2cap_chan *c;
121baa7e1faSGustavo F. Padovan 
122baa7e1faSGustavo F. Padovan 	list_for_each_entry(c, &conn->chan_l, list) {
123fc7f8a7eSGustavo F. Padovan 		if (c->ident == ident)
12448454079SGustavo F. Padovan 			return c;
1250a708f8fSGustavo F. Padovan 	}
126baa7e1faSGustavo F. Padovan 	return NULL;
127baa7e1faSGustavo F. Padovan }
1280a708f8fSGustavo F. Padovan 
129baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1300a708f8fSGustavo F. Padovan {
13148454079SGustavo F. Padovan 	struct l2cap_chan *c;
132baa7e1faSGustavo F. Padovan 
133baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
134baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_ident(conn, ident);
13548454079SGustavo F. Padovan 	if (c)
13648454079SGustavo F. Padovan 		bh_lock_sock(c->sk);
137baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
13848454079SGustavo F. Padovan 	return c;
1390a708f8fSGustavo F. Padovan }
1400a708f8fSGustavo F. Padovan 
14123691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
1429e4425ffSGustavo F. Padovan {
14323691d75SGustavo F. Padovan 	struct l2cap_chan *c;
1449e4425ffSGustavo F. Padovan 
14523691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
14623691d75SGustavo F. Padovan 		if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src))
1479e4425ffSGustavo F. Padovan 			goto found;
1489e4425ffSGustavo F. Padovan 	}
1499e4425ffSGustavo F. Padovan 
15023691d75SGustavo F. Padovan 	c = NULL;
1519e4425ffSGustavo F. Padovan found:
15223691d75SGustavo F. Padovan 	return c;
1539e4425ffSGustavo F. Padovan }
1549e4425ffSGustavo F. Padovan 
1559e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
1569e4425ffSGustavo F. Padovan {
15773b2ec18SGustavo F. Padovan 	int err;
15873b2ec18SGustavo F. Padovan 
15923691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
1609e4425ffSGustavo F. Padovan 
16123691d75SGustavo F. Padovan 	if (psm && __l2cap_global_chan_by_addr(psm, src)) {
16273b2ec18SGustavo F. Padovan 		err = -EADDRINUSE;
16373b2ec18SGustavo F. Padovan 		goto done;
1649e4425ffSGustavo F. Padovan 	}
1659e4425ffSGustavo F. Padovan 
16673b2ec18SGustavo F. Padovan 	if (psm) {
1679e4425ffSGustavo F. Padovan 		chan->psm = psm;
1689e4425ffSGustavo F. Padovan 		chan->sport = psm;
16973b2ec18SGustavo F. Padovan 		err = 0;
17073b2ec18SGustavo F. Padovan 	} else {
17173b2ec18SGustavo F. Padovan 		u16 p;
1729e4425ffSGustavo F. Padovan 
17373b2ec18SGustavo F. Padovan 		err = -EINVAL;
17473b2ec18SGustavo F. Padovan 		for (p = 0x1001; p < 0x1100; p += 2)
17523691d75SGustavo F. Padovan 			if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
17673b2ec18SGustavo F. Padovan 				chan->psm   = cpu_to_le16(p);
17773b2ec18SGustavo F. Padovan 				chan->sport = cpu_to_le16(p);
17873b2ec18SGustavo F. Padovan 				err = 0;
17973b2ec18SGustavo F. Padovan 				break;
18073b2ec18SGustavo F. Padovan 			}
18173b2ec18SGustavo F. Padovan 	}
18273b2ec18SGustavo F. Padovan 
18373b2ec18SGustavo F. Padovan done:
18423691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
18573b2ec18SGustavo F. Padovan 	return err;
1869e4425ffSGustavo F. Padovan }
1879e4425ffSGustavo F. Padovan 
1889e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid)
1899e4425ffSGustavo F. Padovan {
19023691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
1919e4425ffSGustavo F. Padovan 
1929e4425ffSGustavo F. Padovan 	chan->scid = scid;
1939e4425ffSGustavo F. Padovan 
19423691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
1959e4425ffSGustavo F. Padovan 
1969e4425ffSGustavo F. Padovan 	return 0;
1979e4425ffSGustavo F. Padovan }
1989e4425ffSGustavo F. Padovan 
199baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
2000a708f8fSGustavo F. Padovan {
2010a708f8fSGustavo F. Padovan 	u16 cid = L2CAP_CID_DYN_START;
2020a708f8fSGustavo F. Padovan 
2030a708f8fSGustavo F. Padovan 	for (; cid < L2CAP_CID_DYN_END; cid++) {
204baa7e1faSGustavo F. Padovan 		if (!__l2cap_get_chan_by_scid(conn, cid))
2050a708f8fSGustavo F. Padovan 			return cid;
2060a708f8fSGustavo F. Padovan 	}
2070a708f8fSGustavo F. Padovan 
2080a708f8fSGustavo F. Padovan 	return 0;
2090a708f8fSGustavo F. Padovan }
2100a708f8fSGustavo F. Padovan 
211ab07801dSGustavo F. Padovan static void l2cap_chan_set_timer(struct l2cap_chan *chan, long timeout)
212ab07801dSGustavo F. Padovan {
213ab07801dSGustavo F. Padovan        BT_DBG("chan %p state %d timeout %ld", chan->sk, chan->sk->sk_state,
214ab07801dSGustavo F. Padovan 								 timeout);
215ab07801dSGustavo F. Padovan        if (!mod_timer(&chan->chan_timer, jiffies + timeout))
216ab07801dSGustavo F. Padovan 	       sock_hold(chan->sk);
217ab07801dSGustavo F. Padovan }
218ab07801dSGustavo F. Padovan 
219500698d3SGustavo F. Padovan static void l2cap_chan_clear_timer(struct l2cap_chan *chan)
220ab07801dSGustavo F. Padovan {
221ab07801dSGustavo F. Padovan        BT_DBG("chan %p state %d", chan, chan->sk->sk_state);
222ab07801dSGustavo F. Padovan 
223ab07801dSGustavo F. Padovan        if (timer_pending(&chan->chan_timer) && del_timer(&chan->chan_timer))
224ab07801dSGustavo F. Padovan 	       __sock_put(chan->sk);
225ab07801dSGustavo F. Padovan }
226ab07801dSGustavo F. Padovan 
227ab07801dSGustavo F. Padovan static void l2cap_chan_timeout(unsigned long arg)
228ab07801dSGustavo F. Padovan {
229ab07801dSGustavo F. Padovan 	struct l2cap_chan *chan = (struct l2cap_chan *) arg;
230ab07801dSGustavo F. Padovan 	struct sock *sk = chan->sk;
231ab07801dSGustavo F. Padovan 	int reason;
232ab07801dSGustavo F. Padovan 
233ab07801dSGustavo F. Padovan 	BT_DBG("chan %p state %d", chan, sk->sk_state);
234ab07801dSGustavo F. Padovan 
235ab07801dSGustavo F. Padovan 	bh_lock_sock(sk);
236ab07801dSGustavo F. Padovan 
237ab07801dSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
238ab07801dSGustavo F. Padovan 		/* sk is owned by user. Try again later */
239ab07801dSGustavo F. Padovan 		l2cap_chan_set_timer(chan, HZ / 5);
240ab07801dSGustavo F. Padovan 		bh_unlock_sock(sk);
241ab07801dSGustavo F. Padovan 		sock_put(sk);
242ab07801dSGustavo F. Padovan 		return;
243ab07801dSGustavo F. Padovan 	}
244ab07801dSGustavo F. Padovan 
245ab07801dSGustavo F. Padovan 	if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG)
246ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
247ab07801dSGustavo F. Padovan 	else if (sk->sk_state == BT_CONNECT &&
248ab07801dSGustavo F. Padovan 					chan->sec_level != BT_SECURITY_SDP)
249ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
250ab07801dSGustavo F. Padovan 	else
251ab07801dSGustavo F. Padovan 		reason = ETIMEDOUT;
252ab07801dSGustavo F. Padovan 
2530f852724SGustavo F. Padovan 	l2cap_chan_close(chan, reason);
254ab07801dSGustavo F. Padovan 
255ab07801dSGustavo F. Padovan 	bh_unlock_sock(sk);
256ab07801dSGustavo F. Padovan 
257ab07801dSGustavo F. Padovan 	l2cap_sock_kill(sk);
258ab07801dSGustavo F. Padovan 	sock_put(sk);
259ab07801dSGustavo F. Padovan }
260ab07801dSGustavo F. Padovan 
26123691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk)
2620a708f8fSGustavo F. Padovan {
26348454079SGustavo F. Padovan 	struct l2cap_chan *chan;
2640a708f8fSGustavo F. Padovan 
26548454079SGustavo F. Padovan 	chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
26648454079SGustavo F. Padovan 	if (!chan)
26748454079SGustavo F. Padovan 		return NULL;
2680a708f8fSGustavo F. Padovan 
26948454079SGustavo F. Padovan 	chan->sk = sk;
27048454079SGustavo F. Padovan 
27123691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
27223691d75SGustavo F. Padovan 	list_add(&chan->global_l, &chan_list);
27323691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
27423691d75SGustavo F. Padovan 
275ab07801dSGustavo F. Padovan 	setup_timer(&chan->chan_timer, l2cap_chan_timeout, (unsigned long) chan);
276ab07801dSGustavo F. Padovan 
27748454079SGustavo F. Padovan 	return chan;
2780a708f8fSGustavo F. Padovan }
2790a708f8fSGustavo F. Padovan 
28023691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan)
2816ff5abbfSGustavo F. Padovan {
28223691d75SGustavo F. Padovan 	write_lock_bh(&chan_list_lock);
28323691d75SGustavo F. Padovan 	list_del(&chan->global_l);
28423691d75SGustavo F. Padovan 	write_unlock_bh(&chan_list_lock);
28523691d75SGustavo F. Padovan 
2866ff5abbfSGustavo F. Padovan 	kfree(chan);
2876ff5abbfSGustavo F. Padovan }
2886ff5abbfSGustavo F. Padovan 
28948454079SGustavo F. Padovan static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
2900a708f8fSGustavo F. Padovan {
29148454079SGustavo F. Padovan 	struct sock *sk = chan->sk;
2920a708f8fSGustavo F. Padovan 
2930a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
294fe4128e0SGustavo F. Padovan 			chan->psm, chan->dcid);
2950a708f8fSGustavo F. Padovan 
2960a708f8fSGustavo F. Padovan 	conn->disc_reason = 0x13;
2970a708f8fSGustavo F. Padovan 
2988c1d787bSGustavo F. Padovan 	chan->conn = conn;
2990a708f8fSGustavo F. Padovan 
300715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
301b62f328bSVille Tervo 		if (conn->hcon->type == LE_LINK) {
302b62f328bSVille Tervo 			/* LE connection */
3030c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_LE_DEFAULT_MTU;
304fe4128e0SGustavo F. Padovan 			chan->scid = L2CAP_CID_LE_DATA;
305fe4128e0SGustavo F. Padovan 			chan->dcid = L2CAP_CID_LE_DATA;
306b62f328bSVille Tervo 		} else {
3070a708f8fSGustavo F. Padovan 			/* Alloc CID for connection-oriented socket */
308fe4128e0SGustavo F. Padovan 			chan->scid = l2cap_alloc_cid(conn);
3090c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_DEFAULT_MTU;
310b62f328bSVille Tervo 		}
311715ec005SGustavo F. Padovan 	} else if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
3120a708f8fSGustavo F. Padovan 		/* Connectionless socket */
313fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_CONN_LESS;
314fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_CONN_LESS;
3150c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3160a708f8fSGustavo F. Padovan 	} else {
3170a708f8fSGustavo F. Padovan 		/* Raw socket can send/recv signalling messages only */
318fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_SIGNALING;
319fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_SIGNALING;
3200c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
3210a708f8fSGustavo F. Padovan 	}
3220a708f8fSGustavo F. Padovan 
323baa7e1faSGustavo F. Padovan 	sock_hold(sk);
324baa7e1faSGustavo F. Padovan 
325baa7e1faSGustavo F. Padovan 	list_add(&chan->list, &conn->chan_l);
3260a708f8fSGustavo F. Padovan }
3270a708f8fSGustavo F. Padovan 
3280a708f8fSGustavo F. Padovan /* Delete channel.
3290a708f8fSGustavo F. Padovan  * Must be called on the locked socket. */
3304519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err)
3310a708f8fSGustavo F. Padovan {
33248454079SGustavo F. Padovan 	struct sock *sk = chan->sk;
3338c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
3340a708f8fSGustavo F. Padovan 	struct sock *parent = bt_sk(sk)->parent;
3350a708f8fSGustavo F. Padovan 
336ab07801dSGustavo F. Padovan 	l2cap_chan_clear_timer(chan);
3370a708f8fSGustavo F. Padovan 
33849208c9cSGustavo F. Padovan 	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
3390a708f8fSGustavo F. Padovan 
3400a708f8fSGustavo F. Padovan 	if (conn) {
341baa7e1faSGustavo F. Padovan 		/* Delete from channel list */
342baa7e1faSGustavo F. Padovan 		write_lock_bh(&conn->chan_lock);
343baa7e1faSGustavo F. Padovan 		list_del(&chan->list);
344baa7e1faSGustavo F. Padovan 		write_unlock_bh(&conn->chan_lock);
345baa7e1faSGustavo F. Padovan 		__sock_put(sk);
346baa7e1faSGustavo F. Padovan 
3478c1d787bSGustavo F. Padovan 		chan->conn = NULL;
3480a708f8fSGustavo F. Padovan 		hci_conn_put(conn->hcon);
3490a708f8fSGustavo F. Padovan 	}
3500a708f8fSGustavo F. Padovan 
3510a708f8fSGustavo F. Padovan 	sk->sk_state = BT_CLOSED;
3520a708f8fSGustavo F. Padovan 	sock_set_flag(sk, SOCK_ZAPPED);
3530a708f8fSGustavo F. Padovan 
3540a708f8fSGustavo F. Padovan 	if (err)
3550a708f8fSGustavo F. Padovan 		sk->sk_err = err;
3560a708f8fSGustavo F. Padovan 
3570a708f8fSGustavo F. Padovan 	if (parent) {
3580a708f8fSGustavo F. Padovan 		bt_accept_unlink(sk);
3590a708f8fSGustavo F. Padovan 		parent->sk_data_ready(parent, 0);
3600a708f8fSGustavo F. Padovan 	} else
3610a708f8fSGustavo F. Padovan 		sk->sk_state_change(sk);
3620a708f8fSGustavo F. Padovan 
363b4450035SGustavo F. Padovan 	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE &&
364b4450035SGustavo F. Padovan 			chan->conf_state & L2CAP_CONF_INPUT_DONE))
3656ff5abbfSGustavo F. Padovan 		return;
3662ead70b8SGustavo F. Padovan 
36758d35f87SGustavo F. Padovan 	skb_queue_purge(&chan->tx_q);
3680a708f8fSGustavo F. Padovan 
3690c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
3700a708f8fSGustavo F. Padovan 		struct srej_list *l, *tmp;
3710a708f8fSGustavo F. Padovan 
372e92c8e70SGustavo F. Padovan 		del_timer(&chan->retrans_timer);
373e92c8e70SGustavo F. Padovan 		del_timer(&chan->monitor_timer);
374e92c8e70SGustavo F. Padovan 		del_timer(&chan->ack_timer);
3750a708f8fSGustavo F. Padovan 
376f1c6775bSGustavo F. Padovan 		skb_queue_purge(&chan->srej_q);
377f1c6775bSGustavo F. Padovan 		skb_queue_purge(&chan->busy_q);
3780a708f8fSGustavo F. Padovan 
37939d5a3eeSGustavo F. Padovan 		list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
3800a708f8fSGustavo F. Padovan 			list_del(&l->list);
3810a708f8fSGustavo F. Padovan 			kfree(l);
3820a708f8fSGustavo F. Padovan 		}
3830a708f8fSGustavo F. Padovan 	}
3840a708f8fSGustavo F. Padovan }
3850a708f8fSGustavo F. Padovan 
3864519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent)
3874519de9aSGustavo F. Padovan {
3884519de9aSGustavo F. Padovan 	struct sock *sk;
3894519de9aSGustavo F. Padovan 
3904519de9aSGustavo F. Padovan 	BT_DBG("parent %p", parent);
3914519de9aSGustavo F. Padovan 
3924519de9aSGustavo F. Padovan 	/* Close not yet accepted channels */
3930f852724SGustavo F. Padovan 	while ((sk = bt_accept_dequeue(parent, NULL))) {
3940f852724SGustavo F. Padovan 		l2cap_chan_clear_timer(l2cap_pi(sk)->chan);
3950f852724SGustavo F. Padovan 		lock_sock(sk);
3960f852724SGustavo F. Padovan 		l2cap_chan_close(l2cap_pi(sk)->chan, ECONNRESET);
3970f852724SGustavo F. Padovan 		release_sock(sk);
3980f852724SGustavo F. Padovan 		l2cap_sock_kill(sk);
3990f852724SGustavo F. Padovan 	}
4004519de9aSGustavo F. Padovan 
4014519de9aSGustavo F. Padovan 	parent->sk_state = BT_CLOSED;
4024519de9aSGustavo F. Padovan 	sock_set_flag(parent, SOCK_ZAPPED);
4034519de9aSGustavo F. Padovan }
4044519de9aSGustavo F. Padovan 
4050f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason)
4064519de9aSGustavo F. Padovan {
4074519de9aSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
4084519de9aSGustavo F. Padovan 	struct sock *sk = chan->sk;
4094519de9aSGustavo F. Padovan 
4104519de9aSGustavo F. Padovan 	BT_DBG("chan %p state %d socket %p", chan, sk->sk_state, sk->sk_socket);
4114519de9aSGustavo F. Padovan 
4124519de9aSGustavo F. Padovan 	switch (sk->sk_state) {
4134519de9aSGustavo F. Padovan 	case BT_LISTEN:
4144519de9aSGustavo F. Padovan 		l2cap_chan_cleanup_listen(sk);
4154519de9aSGustavo F. Padovan 		break;
4164519de9aSGustavo F. Padovan 
4174519de9aSGustavo F. Padovan 	case BT_CONNECTED:
4184519de9aSGustavo F. Padovan 	case BT_CONFIG:
419715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4204519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
421500698d3SGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
422ab07801dSGustavo F. Padovan 			l2cap_chan_set_timer(chan, sk->sk_sndtimeo);
4234519de9aSGustavo F. Padovan 			l2cap_send_disconn_req(conn, chan, reason);
4244519de9aSGustavo F. Padovan 		} else
4254519de9aSGustavo F. Padovan 			l2cap_chan_del(chan, reason);
4264519de9aSGustavo F. Padovan 		break;
4274519de9aSGustavo F. Padovan 
4284519de9aSGustavo F. Padovan 	case BT_CONNECT2:
429715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
4304519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
4314519de9aSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
4324519de9aSGustavo F. Padovan 			__u16 result;
4334519de9aSGustavo F. Padovan 
4344519de9aSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup)
4354519de9aSGustavo F. Padovan 				result = L2CAP_CR_SEC_BLOCK;
4364519de9aSGustavo F. Padovan 			else
4374519de9aSGustavo F. Padovan 				result = L2CAP_CR_BAD_PSM;
438d8d69c54SMat Martineau 			sk->sk_state = BT_DISCONN;
4394519de9aSGustavo F. Padovan 
4404519de9aSGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4414519de9aSGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
4424519de9aSGustavo F. Padovan 			rsp.result = cpu_to_le16(result);
4434519de9aSGustavo F. Padovan 			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4444519de9aSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4454519de9aSGustavo F. Padovan 							sizeof(rsp), &rsp);
4464519de9aSGustavo F. Padovan 		}
4474519de9aSGustavo F. Padovan 
4484519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
4494519de9aSGustavo F. Padovan 		break;
4504519de9aSGustavo F. Padovan 
4514519de9aSGustavo F. Padovan 	case BT_CONNECT:
4524519de9aSGustavo F. Padovan 	case BT_DISCONN:
4534519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
4544519de9aSGustavo F. Padovan 		break;
4554519de9aSGustavo F. Padovan 
4564519de9aSGustavo F. Padovan 	default:
4574519de9aSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
4584519de9aSGustavo F. Padovan 		break;
4594519de9aSGustavo F. Padovan 	}
4604519de9aSGustavo F. Padovan }
4614519de9aSGustavo F. Padovan 
4624343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
4630a708f8fSGustavo F. Padovan {
464715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_RAW) {
4654343478fSGustavo F. Padovan 		switch (chan->sec_level) {
4660a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
4670a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING_MITM;
4680a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
4690a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING;
4700a708f8fSGustavo F. Padovan 		default:
4710a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
4720a708f8fSGustavo F. Padovan 		}
473fe4128e0SGustavo F. Padovan 	} else if (chan->psm == cpu_to_le16(0x0001)) {
4744343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_LOW)
4754343478fSGustavo F. Padovan 			chan->sec_level = BT_SECURITY_SDP;
4760a708f8fSGustavo F. Padovan 
4774343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_HIGH)
4780a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING_MITM;
4790a708f8fSGustavo F. Padovan 		else
4800a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
4810a708f8fSGustavo F. Padovan 	} else {
4824343478fSGustavo F. Padovan 		switch (chan->sec_level) {
4830a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
4840a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING_MITM;
4850a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
4860a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING;
4870a708f8fSGustavo F. Padovan 		default:
4880a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
4890a708f8fSGustavo F. Padovan 		}
4900a708f8fSGustavo F. Padovan 	}
4910a708f8fSGustavo F. Padovan }
4920a708f8fSGustavo F. Padovan 
4930a708f8fSGustavo F. Padovan /* Service level security */
4944343478fSGustavo F. Padovan static inline int l2cap_check_security(struct l2cap_chan *chan)
4950a708f8fSGustavo F. Padovan {
4968c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
4970a708f8fSGustavo F. Padovan 	__u8 auth_type;
4980a708f8fSGustavo F. Padovan 
4994343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
5000a708f8fSGustavo F. Padovan 
5014343478fSGustavo F. Padovan 	return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
5020a708f8fSGustavo F. Padovan }
5030a708f8fSGustavo F. Padovan 
504b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn)
5050a708f8fSGustavo F. Padovan {
5060a708f8fSGustavo F. Padovan 	u8 id;
5070a708f8fSGustavo F. Padovan 
5080a708f8fSGustavo F. Padovan 	/* Get next available identificator.
5090a708f8fSGustavo F. Padovan 	 *    1 - 128 are used by kernel.
5100a708f8fSGustavo F. Padovan 	 *  129 - 199 are reserved.
5110a708f8fSGustavo F. Padovan 	 *  200 - 254 are used by utilities like l2ping, etc.
5120a708f8fSGustavo F. Padovan 	 */
5130a708f8fSGustavo F. Padovan 
5140a708f8fSGustavo F. Padovan 	spin_lock_bh(&conn->lock);
5150a708f8fSGustavo F. Padovan 
5160a708f8fSGustavo F. Padovan 	if (++conn->tx_ident > 128)
5170a708f8fSGustavo F. Padovan 		conn->tx_ident = 1;
5180a708f8fSGustavo F. Padovan 
5190a708f8fSGustavo F. Padovan 	id = conn->tx_ident;
5200a708f8fSGustavo F. Padovan 
5210a708f8fSGustavo F. Padovan 	spin_unlock_bh(&conn->lock);
5220a708f8fSGustavo F. Padovan 
5230a708f8fSGustavo F. Padovan 	return id;
5240a708f8fSGustavo F. Padovan }
5250a708f8fSGustavo F. Padovan 
5264519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
5270a708f8fSGustavo F. Padovan {
5280a708f8fSGustavo F. Padovan 	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
5290a708f8fSGustavo F. Padovan 	u8 flags;
5300a708f8fSGustavo F. Padovan 
5310a708f8fSGustavo F. Padovan 	BT_DBG("code 0x%2.2x", code);
5320a708f8fSGustavo F. Padovan 
5330a708f8fSGustavo F. Padovan 	if (!skb)
5340a708f8fSGustavo F. Padovan 		return;
5350a708f8fSGustavo F. Padovan 
5360a708f8fSGustavo F. Padovan 	if (lmp_no_flush_capable(conn->hcon->hdev))
5370a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
5380a708f8fSGustavo F. Padovan 	else
5390a708f8fSGustavo F. Padovan 		flags = ACL_START;
5400a708f8fSGustavo F. Padovan 
54114b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
54214b12d0bSJaikumar Ganesh 
5430a708f8fSGustavo F. Padovan 	hci_send_acl(conn->hcon, skb, flags);
5440a708f8fSGustavo F. Padovan }
5450a708f8fSGustavo F. Padovan 
546525cd185SGustavo F. Padovan static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control)
5470a708f8fSGustavo F. Padovan {
5480a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
5490a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
550525cd185SGustavo F. Padovan 	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
5518c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
5520a708f8fSGustavo F. Padovan 	struct sock *sk = (struct sock *)pi;
5530a708f8fSGustavo F. Padovan 	int count, hlen = L2CAP_HDR_SIZE + 2;
5540a708f8fSGustavo F. Padovan 	u8 flags;
5550a708f8fSGustavo F. Padovan 
5560a708f8fSGustavo F. Padovan 	if (sk->sk_state != BT_CONNECTED)
5570a708f8fSGustavo F. Padovan 		return;
5580a708f8fSGustavo F. Padovan 
55947d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
5600a708f8fSGustavo F. Padovan 		hlen += 2;
5610a708f8fSGustavo F. Padovan 
56249208c9cSGustavo F. Padovan 	BT_DBG("chan %p, control 0x%2.2x", chan, control);
5630a708f8fSGustavo F. Padovan 
5640a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, hlen);
5650a708f8fSGustavo F. Padovan 	control |= L2CAP_CTRL_FRAME_TYPE;
5660a708f8fSGustavo F. Padovan 
567525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
5680a708f8fSGustavo F. Padovan 		control |= L2CAP_CTRL_FINAL;
569525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
5700a708f8fSGustavo F. Padovan 	}
5710a708f8fSGustavo F. Padovan 
572525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SEND_PBIT) {
5730a708f8fSGustavo F. Padovan 		control |= L2CAP_CTRL_POLL;
574525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SEND_PBIT;
5750a708f8fSGustavo F. Padovan 	}
5760a708f8fSGustavo F. Padovan 
5770a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
5780a708f8fSGustavo F. Padovan 	if (!skb)
5790a708f8fSGustavo F. Padovan 		return;
5800a708f8fSGustavo F. Padovan 
5810a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
5820a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
583fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
5840a708f8fSGustavo F. Padovan 	put_unaligned_le16(control, skb_put(skb, 2));
5850a708f8fSGustavo F. Padovan 
58647d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
5870a708f8fSGustavo F. Padovan 		u16 fcs = crc16(0, (u8 *)lh, count - 2);
5880a708f8fSGustavo F. Padovan 		put_unaligned_le16(fcs, skb_put(skb, 2));
5890a708f8fSGustavo F. Padovan 	}
5900a708f8fSGustavo F. Padovan 
5910a708f8fSGustavo F. Padovan 	if (lmp_no_flush_capable(conn->hcon->hdev))
5920a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
5930a708f8fSGustavo F. Padovan 	else
5940a708f8fSGustavo F. Padovan 		flags = ACL_START;
5950a708f8fSGustavo F. Padovan 
59614b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = chan->force_active;
59714b12d0bSJaikumar Ganesh 
5988c1d787bSGustavo F. Padovan 	hci_send_acl(chan->conn->hcon, skb, flags);
5990a708f8fSGustavo F. Padovan }
6000a708f8fSGustavo F. Padovan 
601525cd185SGustavo F. Padovan static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control)
6020a708f8fSGustavo F. Padovan {
603525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
6040a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_NOT_READY;
605525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_RNR_SENT;
6060a708f8fSGustavo F. Padovan 	} else
6070a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_READY;
6080a708f8fSGustavo F. Padovan 
60942e5c802SGustavo F. Padovan 	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
6100a708f8fSGustavo F. Padovan 
611525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
6120a708f8fSGustavo F. Padovan }
6130a708f8fSGustavo F. Padovan 
614b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
6150a708f8fSGustavo F. Padovan {
616b4450035SGustavo F. Padovan 	return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND);
6170a708f8fSGustavo F. Padovan }
6180a708f8fSGustavo F. Padovan 
619fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan)
6200a708f8fSGustavo F. Padovan {
6218c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
6220a708f8fSGustavo F. Padovan 
6230a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
6240a708f8fSGustavo F. Padovan 		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
6250a708f8fSGustavo F. Padovan 			return;
6260a708f8fSGustavo F. Padovan 
6274343478fSGustavo F. Padovan 		if (l2cap_check_security(chan) &&
6284343478fSGustavo F. Padovan 				__l2cap_no_conn_pending(chan)) {
6290a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
630fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
631fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
6320a708f8fSGustavo F. Padovan 
633fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
634b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
6350a708f8fSGustavo F. Padovan 
636fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
637fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
6380a708f8fSGustavo F. Padovan 		}
6390a708f8fSGustavo F. Padovan 	} else {
6400a708f8fSGustavo F. Padovan 		struct l2cap_info_req req;
6410a708f8fSGustavo F. Padovan 		req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
6420a708f8fSGustavo F. Padovan 
6430a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
6440a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
6450a708f8fSGustavo F. Padovan 
6460a708f8fSGustavo F. Padovan 		mod_timer(&conn->info_timer, jiffies +
6470a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
6480a708f8fSGustavo F. Padovan 
6490a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
6500a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
6510a708f8fSGustavo F. Padovan 	}
6520a708f8fSGustavo F. Padovan }
6530a708f8fSGustavo F. Padovan 
6540a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
6550a708f8fSGustavo F. Padovan {
6560a708f8fSGustavo F. Padovan 	u32 local_feat_mask = l2cap_feat_mask;
6570a708f8fSGustavo F. Padovan 	if (!disable_ertm)
6580a708f8fSGustavo F. Padovan 		local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
6590a708f8fSGustavo F. Padovan 
6600a708f8fSGustavo F. Padovan 	switch (mode) {
6610a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
6620a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
6630a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
6640a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
6650a708f8fSGustavo F. Padovan 	default:
6660a708f8fSGustavo F. Padovan 		return 0x00;
6670a708f8fSGustavo F. Padovan 	}
6680a708f8fSGustavo F. Padovan }
6690a708f8fSGustavo F. Padovan 
6704519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
6710a708f8fSGustavo F. Padovan {
672e92c8e70SGustavo F. Padovan 	struct sock *sk;
6730a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req req;
6740a708f8fSGustavo F. Padovan 
6750a708f8fSGustavo F. Padovan 	if (!conn)
6760a708f8fSGustavo F. Padovan 		return;
6770a708f8fSGustavo F. Padovan 
678e92c8e70SGustavo F. Padovan 	sk = chan->sk;
679e92c8e70SGustavo F. Padovan 
6800c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
681e92c8e70SGustavo F. Padovan 		del_timer(&chan->retrans_timer);
682e92c8e70SGustavo F. Padovan 		del_timer(&chan->monitor_timer);
683e92c8e70SGustavo F. Padovan 		del_timer(&chan->ack_timer);
6840a708f8fSGustavo F. Padovan 	}
6850a708f8fSGustavo F. Padovan 
686fe4128e0SGustavo F. Padovan 	req.dcid = cpu_to_le16(chan->dcid);
687fe4128e0SGustavo F. Padovan 	req.scid = cpu_to_le16(chan->scid);
6880a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn),
6890a708f8fSGustavo F. Padovan 			L2CAP_DISCONN_REQ, sizeof(req), &req);
6900a708f8fSGustavo F. Padovan 
6910a708f8fSGustavo F. Padovan 	sk->sk_state = BT_DISCONN;
6920a708f8fSGustavo F. Padovan 	sk->sk_err = err;
6930a708f8fSGustavo F. Padovan }
6940a708f8fSGustavo F. Padovan 
6950a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */
6960a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn)
6970a708f8fSGustavo F. Padovan {
698820ffdb3SGustavo F. Padovan 	struct l2cap_chan *chan, *tmp;
6990a708f8fSGustavo F. Padovan 
7000a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
7010a708f8fSGustavo F. Padovan 
702baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
7030a708f8fSGustavo F. Padovan 
704820ffdb3SGustavo F. Padovan 	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
70548454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
706baa7e1faSGustavo F. Padovan 
7070a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
7080a708f8fSGustavo F. Padovan 
709715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7100a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
7110a708f8fSGustavo F. Padovan 			continue;
7120a708f8fSGustavo F. Padovan 		}
7130a708f8fSGustavo F. Padovan 
7140a708f8fSGustavo F. Padovan 		if (sk->sk_state == BT_CONNECT) {
7150a708f8fSGustavo F. Padovan 			struct l2cap_conn_req req;
7160a708f8fSGustavo F. Padovan 
7174343478fSGustavo F. Padovan 			if (!l2cap_check_security(chan) ||
718b4450035SGustavo F. Padovan 					!__l2cap_no_conn_pending(chan)) {
7190a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7200a708f8fSGustavo F. Padovan 				continue;
7210a708f8fSGustavo F. Padovan 			}
7220a708f8fSGustavo F. Padovan 
7230c1bc5c6SGustavo F. Padovan 			if (!l2cap_mode_supported(chan->mode,
7240a708f8fSGustavo F. Padovan 					conn->feat_mask)
725b4450035SGustavo F. Padovan 					&& chan->conf_state &
7260a708f8fSGustavo F. Padovan 					L2CAP_CONF_STATE2_DEVICE) {
7270f852724SGustavo F. Padovan 				/* l2cap_chan_close() calls list_del(chan)
728820ffdb3SGustavo F. Padovan 				 * so release the lock */
729820ffdb3SGustavo F. Padovan 				read_unlock_bh(&conn->chan_lock);
7300f852724SGustavo F. Padovan 				 l2cap_chan_close(chan, ECONNRESET);
731820ffdb3SGustavo F. Padovan 				read_lock_bh(&conn->chan_lock);
7320a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7330a708f8fSGustavo F. Padovan 				continue;
7340a708f8fSGustavo F. Padovan 			}
7350a708f8fSGustavo F. Padovan 
736fe4128e0SGustavo F. Padovan 			req.scid = cpu_to_le16(chan->scid);
737fe4128e0SGustavo F. Padovan 			req.psm  = chan->psm;
7380a708f8fSGustavo F. Padovan 
739fc7f8a7eSGustavo F. Padovan 			chan->ident = l2cap_get_ident(conn);
740b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
7410a708f8fSGustavo F. Padovan 
742fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ,
743fc7f8a7eSGustavo F. Padovan 							sizeof(req), &req);
7440a708f8fSGustavo F. Padovan 
7450a708f8fSGustavo F. Padovan 		} else if (sk->sk_state == BT_CONNECT2) {
7460a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
7470a708f8fSGustavo F. Padovan 			char buf[128];
748fe4128e0SGustavo F. Padovan 			rsp.scid = cpu_to_le16(chan->dcid);
749fe4128e0SGustavo F. Padovan 			rsp.dcid = cpu_to_le16(chan->scid);
7500a708f8fSGustavo F. Padovan 
7514343478fSGustavo F. Padovan 			if (l2cap_check_security(chan)) {
7520a708f8fSGustavo F. Padovan 				if (bt_sk(sk)->defer_setup) {
7530a708f8fSGustavo F. Padovan 					struct sock *parent = bt_sk(sk)->parent;
7540a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_PEND);
7550a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
7560a708f8fSGustavo F. Padovan 					parent->sk_data_ready(parent, 0);
7570a708f8fSGustavo F. Padovan 
7580a708f8fSGustavo F. Padovan 				} else {
7590a708f8fSGustavo F. Padovan 					sk->sk_state = BT_CONFIG;
7600a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
7610a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
7620a708f8fSGustavo F. Padovan 				}
7630a708f8fSGustavo F. Padovan 			} else {
7640a708f8fSGustavo F. Padovan 				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
7650a708f8fSGustavo F. Padovan 				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
7660a708f8fSGustavo F. Padovan 			}
7670a708f8fSGustavo F. Padovan 
768fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
769fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
7700a708f8fSGustavo F. Padovan 
771b4450035SGustavo F. Padovan 			if (chan->conf_state & L2CAP_CONF_REQ_SENT ||
7720a708f8fSGustavo F. Padovan 					rsp.result != L2CAP_CR_SUCCESS) {
7730a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
7740a708f8fSGustavo F. Padovan 				continue;
7750a708f8fSGustavo F. Padovan 			}
7760a708f8fSGustavo F. Padovan 
777b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_REQ_SENT;
7780a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
77973ffa904SGustavo F. Padovan 						l2cap_build_conf_req(chan, buf), buf);
78073ffa904SGustavo F. Padovan 			chan->num_conf_req++;
7810a708f8fSGustavo F. Padovan 		}
7820a708f8fSGustavo F. Padovan 
7830a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
7840a708f8fSGustavo F. Padovan 	}
7850a708f8fSGustavo F. Padovan 
786baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
7870a708f8fSGustavo F. Padovan }
7880a708f8fSGustavo F. Padovan 
789b62f328bSVille Tervo /* Find socket with cid and source bdaddr.
790b62f328bSVille Tervo  * Returns closest match, locked.
791b62f328bSVille Tervo  */
79223691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src)
793b62f328bSVille Tervo {
79423691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
795b62f328bSVille Tervo 
79623691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
797b62f328bSVille Tervo 
79823691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
79923691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
800fe4128e0SGustavo F. Padovan 
801b62f328bSVille Tervo 		if (state && sk->sk_state != state)
802b62f328bSVille Tervo 			continue;
803b62f328bSVille Tervo 
80423691d75SGustavo F. Padovan 		if (c->scid == cid) {
805b62f328bSVille Tervo 			/* Exact match. */
80623691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
80723691d75SGustavo F. Padovan 				read_unlock(&chan_list_lock);
80823691d75SGustavo F. Padovan 				return c;
80923691d75SGustavo F. Padovan 			}
810b62f328bSVille Tervo 
811b62f328bSVille Tervo 			/* Closest match */
812b62f328bSVille Tervo 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
81323691d75SGustavo F. Padovan 				c1 = c;
814b62f328bSVille Tervo 		}
815b62f328bSVille Tervo 	}
816280f294fSGustavo F. Padovan 
81723691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
818b62f328bSVille Tervo 
81923691d75SGustavo F. Padovan 	return c1;
820b62f328bSVille Tervo }
821b62f328bSVille Tervo 
822b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn)
823b62f328bSVille Tervo {
824c916fbe4SGustavo F. Padovan 	struct sock *parent, *sk;
82523691d75SGustavo F. Padovan 	struct l2cap_chan *chan, *pchan;
826b62f328bSVille Tervo 
827b62f328bSVille Tervo 	BT_DBG("");
828b62f328bSVille Tervo 
829b62f328bSVille Tervo 	/* Check if we have socket listening on cid */
83023691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
831b62f328bSVille Tervo 							conn->src);
83223691d75SGustavo F. Padovan 	if (!pchan)
833b62f328bSVille Tervo 		return;
834b62f328bSVille Tervo 
83523691d75SGustavo F. Padovan 	parent = pchan->sk;
83623691d75SGustavo F. Padovan 
83762f3a2cfSGustavo F. Padovan 	bh_lock_sock(parent);
83862f3a2cfSGustavo F. Padovan 
839b62f328bSVille Tervo 	/* Check for backlog size */
840b62f328bSVille Tervo 	if (sk_acceptq_is_full(parent)) {
841b62f328bSVille Tervo 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
842b62f328bSVille Tervo 		goto clean;
843b62f328bSVille Tervo 	}
844b62f328bSVille Tervo 
84580808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
84680808e43SGustavo F. Padovan 	if (!chan)
847b62f328bSVille Tervo 		goto clean;
848b62f328bSVille Tervo 
84980808e43SGustavo F. Padovan 	sk = chan->sk;
8505d41ce1dSGustavo F. Padovan 
851baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
852b62f328bSVille Tervo 
853b62f328bSVille Tervo 	hci_conn_hold(conn->hcon);
854b62f328bSVille Tervo 
855b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->src, conn->src);
856b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->dst, conn->dst);
857b62f328bSVille Tervo 
858d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
859d1010240SGustavo F. Padovan 
86048454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
86148454079SGustavo F. Padovan 
862ab07801dSGustavo F. Padovan 	l2cap_chan_set_timer(chan, sk->sk_sndtimeo);
863b62f328bSVille Tervo 
864b62f328bSVille Tervo 	sk->sk_state = BT_CONNECTED;
865b62f328bSVille Tervo 	parent->sk_data_ready(parent, 0);
866b62f328bSVille Tervo 
867baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
868b62f328bSVille Tervo 
869b62f328bSVille Tervo clean:
870b62f328bSVille Tervo 	bh_unlock_sock(parent);
871b62f328bSVille Tervo }
872b62f328bSVille Tervo 
8730a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn)
8740a708f8fSGustavo F. Padovan {
87548454079SGustavo F. Padovan 	struct l2cap_chan *chan;
8760a708f8fSGustavo F. Padovan 
8770a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
8780a708f8fSGustavo F. Padovan 
879b62f328bSVille Tervo 	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
880b62f328bSVille Tervo 		l2cap_le_conn_ready(conn);
881b62f328bSVille Tervo 
882baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
8830a708f8fSGustavo F. Padovan 
884baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
88548454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
886baa7e1faSGustavo F. Padovan 
8870a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
8880a708f8fSGustavo F. Padovan 
889acd7d370SVille Tervo 		if (conn->hcon->type == LE_LINK) {
890ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
891acd7d370SVille Tervo 			sk->sk_state = BT_CONNECTED;
892acd7d370SVille Tervo 			sk->sk_state_change(sk);
893acd7d370SVille Tervo 		}
894acd7d370SVille Tervo 
895715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
896ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
8970a708f8fSGustavo F. Padovan 			sk->sk_state = BT_CONNECTED;
8980a708f8fSGustavo F. Padovan 			sk->sk_state_change(sk);
8990a708f8fSGustavo F. Padovan 		} else if (sk->sk_state == BT_CONNECT)
900fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
9010a708f8fSGustavo F. Padovan 
9020a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
9030a708f8fSGustavo F. Padovan 	}
9040a708f8fSGustavo F. Padovan 
905baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
9060a708f8fSGustavo F. Padovan }
9070a708f8fSGustavo F. Padovan 
9080a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */
9090a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
9100a708f8fSGustavo F. Padovan {
91148454079SGustavo F. Padovan 	struct l2cap_chan *chan;
9120a708f8fSGustavo F. Padovan 
9130a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
9140a708f8fSGustavo F. Padovan 
915baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
9160a708f8fSGustavo F. Padovan 
917baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
91848454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
919baa7e1faSGustavo F. Padovan 
9204343478fSGustavo F. Padovan 		if (chan->force_reliable)
9210a708f8fSGustavo F. Padovan 			sk->sk_err = err;
9220a708f8fSGustavo F. Padovan 	}
9230a708f8fSGustavo F. Padovan 
924baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
9250a708f8fSGustavo F. Padovan }
9260a708f8fSGustavo F. Padovan 
9270a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg)
9280a708f8fSGustavo F. Padovan {
9290a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = (void *) arg;
9300a708f8fSGustavo F. Padovan 
9310a708f8fSGustavo F. Padovan 	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
9320a708f8fSGustavo F. Padovan 	conn->info_ident = 0;
9330a708f8fSGustavo F. Padovan 
9340a708f8fSGustavo F. Padovan 	l2cap_conn_start(conn);
9350a708f8fSGustavo F. Padovan }
9360a708f8fSGustavo F. Padovan 
9370a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
9380a708f8fSGustavo F. Padovan {
9390a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
9400a708f8fSGustavo F. Padovan 
9410a708f8fSGustavo F. Padovan 	if (conn || status)
9420a708f8fSGustavo F. Padovan 		return conn;
9430a708f8fSGustavo F. Padovan 
9440a708f8fSGustavo F. Padovan 	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
9450a708f8fSGustavo F. Padovan 	if (!conn)
9460a708f8fSGustavo F. Padovan 		return NULL;
9470a708f8fSGustavo F. Padovan 
9480a708f8fSGustavo F. Padovan 	hcon->l2cap_data = conn;
9490a708f8fSGustavo F. Padovan 	conn->hcon = hcon;
9500a708f8fSGustavo F. Padovan 
9510a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p conn %p", hcon, conn);
9520a708f8fSGustavo F. Padovan 
953acd7d370SVille Tervo 	if (hcon->hdev->le_mtu && hcon->type == LE_LINK)
954acd7d370SVille Tervo 		conn->mtu = hcon->hdev->le_mtu;
955acd7d370SVille Tervo 	else
9560a708f8fSGustavo F. Padovan 		conn->mtu = hcon->hdev->acl_mtu;
957acd7d370SVille Tervo 
9580a708f8fSGustavo F. Padovan 	conn->src = &hcon->hdev->bdaddr;
9590a708f8fSGustavo F. Padovan 	conn->dst = &hcon->dst;
9600a708f8fSGustavo F. Padovan 
9610a708f8fSGustavo F. Padovan 	conn->feat_mask = 0;
9620a708f8fSGustavo F. Padovan 
9630a708f8fSGustavo F. Padovan 	spin_lock_init(&conn->lock);
964baa7e1faSGustavo F. Padovan 	rwlock_init(&conn->chan_lock);
965baa7e1faSGustavo F. Padovan 
966baa7e1faSGustavo F. Padovan 	INIT_LIST_HEAD(&conn->chan_l);
9670a708f8fSGustavo F. Padovan 
968b62f328bSVille Tervo 	if (hcon->type != LE_LINK)
9690a708f8fSGustavo F. Padovan 		setup_timer(&conn->info_timer, l2cap_info_timeout,
9700a708f8fSGustavo F. Padovan 						(unsigned long) conn);
9710a708f8fSGustavo F. Padovan 
9720a708f8fSGustavo F. Padovan 	conn->disc_reason = 0x13;
9730a708f8fSGustavo F. Padovan 
9740a708f8fSGustavo F. Padovan 	return conn;
9750a708f8fSGustavo F. Padovan }
9760a708f8fSGustavo F. Padovan 
9770a708f8fSGustavo F. Padovan static void l2cap_conn_del(struct hci_conn *hcon, int err)
9780a708f8fSGustavo F. Padovan {
9790a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
980baa7e1faSGustavo F. Padovan 	struct l2cap_chan *chan, *l;
9810a708f8fSGustavo F. Padovan 	struct sock *sk;
9820a708f8fSGustavo F. Padovan 
9830a708f8fSGustavo F. Padovan 	if (!conn)
9840a708f8fSGustavo F. Padovan 		return;
9850a708f8fSGustavo F. Padovan 
9860a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
9870a708f8fSGustavo F. Padovan 
9880a708f8fSGustavo F. Padovan 	kfree_skb(conn->rx_skb);
9890a708f8fSGustavo F. Padovan 
9900a708f8fSGustavo F. Padovan 	/* Kill channels */
991baa7e1faSGustavo F. Padovan 	list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
99248454079SGustavo F. Padovan 		sk = chan->sk;
9930a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
99448454079SGustavo F. Padovan 		l2cap_chan_del(chan, err);
9950a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
9960a708f8fSGustavo F. Padovan 		l2cap_sock_kill(sk);
9970a708f8fSGustavo F. Padovan 	}
9980a708f8fSGustavo F. Padovan 
9990a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
10000a708f8fSGustavo F. Padovan 		del_timer_sync(&conn->info_timer);
10010a708f8fSGustavo F. Padovan 
10020a708f8fSGustavo F. Padovan 	hcon->l2cap_data = NULL;
10030a708f8fSGustavo F. Padovan 	kfree(conn);
10040a708f8fSGustavo F. Padovan }
10050a708f8fSGustavo F. Padovan 
100648454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
10070a708f8fSGustavo F. Padovan {
1008baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
100948454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
1010baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
10110a708f8fSGustavo F. Padovan }
10120a708f8fSGustavo F. Padovan 
10130a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */
10140a708f8fSGustavo F. Padovan 
10150a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr.
10160a708f8fSGustavo F. Padovan  * Returns closest match.
10170a708f8fSGustavo F. Padovan  */
101823691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src)
10190a708f8fSGustavo F. Padovan {
102023691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
10210a708f8fSGustavo F. Padovan 
102223691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
10230a708f8fSGustavo F. Padovan 
102423691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
102523691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
1026fe4128e0SGustavo F. Padovan 
10270a708f8fSGustavo F. Padovan 		if (state && sk->sk_state != state)
10280a708f8fSGustavo F. Padovan 			continue;
10290a708f8fSGustavo F. Padovan 
103023691d75SGustavo F. Padovan 		if (c->psm == psm) {
10310a708f8fSGustavo F. Padovan 			/* Exact match. */
103223691d75SGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, src)) {
1033a7567b20SJohannes Berg 				read_unlock(&chan_list_lock);
103423691d75SGustavo F. Padovan 				return c;
103523691d75SGustavo F. Padovan 			}
10360a708f8fSGustavo F. Padovan 
10370a708f8fSGustavo F. Padovan 			/* Closest match */
10380a708f8fSGustavo F. Padovan 			if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
103923691d75SGustavo F. Padovan 				c1 = c;
10400a708f8fSGustavo F. Padovan 		}
10410a708f8fSGustavo F. Padovan 	}
10420a708f8fSGustavo F. Padovan 
104323691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
10440a708f8fSGustavo F. Padovan 
104523691d75SGustavo F. Padovan 	return c1;
10460a708f8fSGustavo F. Padovan }
10470a708f8fSGustavo F. Padovan 
104877a74c7eSGustavo F. Padovan int l2cap_chan_connect(struct l2cap_chan *chan)
10490a708f8fSGustavo F. Padovan {
10505d41ce1dSGustavo F. Padovan 	struct sock *sk = chan->sk;
10510a708f8fSGustavo F. Padovan 	bdaddr_t *src = &bt_sk(sk)->src;
10520a708f8fSGustavo F. Padovan 	bdaddr_t *dst = &bt_sk(sk)->dst;
10530a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
10540a708f8fSGustavo F. Padovan 	struct hci_conn *hcon;
10550a708f8fSGustavo F. Padovan 	struct hci_dev *hdev;
10560a708f8fSGustavo F. Padovan 	__u8 auth_type;
10570a708f8fSGustavo F. Padovan 	int err;
10580a708f8fSGustavo F. Padovan 
10590a708f8fSGustavo F. Padovan 	BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
1060fe4128e0SGustavo F. Padovan 							chan->psm);
10610a708f8fSGustavo F. Padovan 
10620a708f8fSGustavo F. Padovan 	hdev = hci_get_route(dst, src);
10630a708f8fSGustavo F. Padovan 	if (!hdev)
10640a708f8fSGustavo F. Padovan 		return -EHOSTUNREACH;
10650a708f8fSGustavo F. Padovan 
10660a708f8fSGustavo F. Padovan 	hci_dev_lock_bh(hdev);
10670a708f8fSGustavo F. Padovan 
10684343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
10690a708f8fSGustavo F. Padovan 
1070fe4128e0SGustavo F. Padovan 	if (chan->dcid == L2CAP_CID_LE_DATA)
1071acd7d370SVille Tervo 		hcon = hci_connect(hdev, LE_LINK, dst,
10724343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1073acd7d370SVille Tervo 	else
10740a708f8fSGustavo F. Padovan 		hcon = hci_connect(hdev, ACL_LINK, dst,
10754343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1076acd7d370SVille Tervo 
107730e76272SVille Tervo 	if (IS_ERR(hcon)) {
107830e76272SVille Tervo 		err = PTR_ERR(hcon);
10790a708f8fSGustavo F. Padovan 		goto done;
108030e76272SVille Tervo 	}
10810a708f8fSGustavo F. Padovan 
10820a708f8fSGustavo F. Padovan 	conn = l2cap_conn_add(hcon, 0);
10830a708f8fSGustavo F. Padovan 	if (!conn) {
10840a708f8fSGustavo F. Padovan 		hci_conn_put(hcon);
108530e76272SVille Tervo 		err = -ENOMEM;
10860a708f8fSGustavo F. Padovan 		goto done;
10870a708f8fSGustavo F. Padovan 	}
10880a708f8fSGustavo F. Padovan 
10890a708f8fSGustavo F. Padovan 	/* Update source addr of the socket */
10900a708f8fSGustavo F. Padovan 	bacpy(src, conn->src);
10910a708f8fSGustavo F. Padovan 
109248454079SGustavo F. Padovan 	l2cap_chan_add(conn, chan);
109348454079SGustavo F. Padovan 
10940a708f8fSGustavo F. Padovan 	sk->sk_state = BT_CONNECT;
1095ab07801dSGustavo F. Padovan 	l2cap_chan_set_timer(chan, sk->sk_sndtimeo);
10960a708f8fSGustavo F. Padovan 
10970a708f8fSGustavo F. Padovan 	if (hcon->state == BT_CONNECTED) {
1098715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1099ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
11004343478fSGustavo F. Padovan 			if (l2cap_check_security(chan))
11010a708f8fSGustavo F. Padovan 				sk->sk_state = BT_CONNECTED;
11020a708f8fSGustavo F. Padovan 		} else
1103fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
11040a708f8fSGustavo F. Padovan 	}
11050a708f8fSGustavo F. Padovan 
110630e76272SVille Tervo 	err = 0;
110730e76272SVille Tervo 
11080a708f8fSGustavo F. Padovan done:
11090a708f8fSGustavo F. Padovan 	hci_dev_unlock_bh(hdev);
11100a708f8fSGustavo F. Padovan 	hci_dev_put(hdev);
11110a708f8fSGustavo F. Padovan 	return err;
11120a708f8fSGustavo F. Padovan }
11130a708f8fSGustavo F. Padovan 
1114dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk)
11150a708f8fSGustavo F. Padovan {
11168c1d787bSGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
11170a708f8fSGustavo F. Padovan 	DECLARE_WAITQUEUE(wait, current);
11180a708f8fSGustavo F. Padovan 	int err = 0;
11190a708f8fSGustavo F. Padovan 	int timeo = HZ/5;
11200a708f8fSGustavo F. Padovan 
11210a708f8fSGustavo F. Padovan 	add_wait_queue(sk_sleep(sk), &wait);
11228c1d787bSGustavo F. Padovan 	while ((chan->unacked_frames > 0 && chan->conn)) {
11230a708f8fSGustavo F. Padovan 		set_current_state(TASK_INTERRUPTIBLE);
11240a708f8fSGustavo F. Padovan 
11250a708f8fSGustavo F. Padovan 		if (!timeo)
11260a708f8fSGustavo F. Padovan 			timeo = HZ/5;
11270a708f8fSGustavo F. Padovan 
11280a708f8fSGustavo F. Padovan 		if (signal_pending(current)) {
11290a708f8fSGustavo F. Padovan 			err = sock_intr_errno(timeo);
11300a708f8fSGustavo F. Padovan 			break;
11310a708f8fSGustavo F. Padovan 		}
11320a708f8fSGustavo F. Padovan 
11330a708f8fSGustavo F. Padovan 		release_sock(sk);
11340a708f8fSGustavo F. Padovan 		timeo = schedule_timeout(timeo);
11350a708f8fSGustavo F. Padovan 		lock_sock(sk);
11360a708f8fSGustavo F. Padovan 
11370a708f8fSGustavo F. Padovan 		err = sock_error(sk);
11380a708f8fSGustavo F. Padovan 		if (err)
11390a708f8fSGustavo F. Padovan 			break;
11400a708f8fSGustavo F. Padovan 	}
11410a708f8fSGustavo F. Padovan 	set_current_state(TASK_RUNNING);
11420a708f8fSGustavo F. Padovan 	remove_wait_queue(sk_sleep(sk), &wait);
11430a708f8fSGustavo F. Padovan 	return err;
11440a708f8fSGustavo F. Padovan }
11450a708f8fSGustavo F. Padovan 
11460a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg)
11470a708f8fSGustavo F. Padovan {
1148525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
1149525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
11500a708f8fSGustavo F. Padovan 
1151525cd185SGustavo F. Padovan 	BT_DBG("chan %p", chan);
11520a708f8fSGustavo F. Padovan 
11530a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
11542c03a7a4SGustavo F. Padovan 	if (chan->retry_count >= chan->remote_max_tx) {
11558c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
11560a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
11570a708f8fSGustavo F. Padovan 		return;
11580a708f8fSGustavo F. Padovan 	}
11590a708f8fSGustavo F. Padovan 
11606a026610SGustavo F. Padovan 	chan->retry_count++;
11610a708f8fSGustavo F. Padovan 	__mod_monitor_timer();
11620a708f8fSGustavo F. Padovan 
1163525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
11640a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
11650a708f8fSGustavo F. Padovan }
11660a708f8fSGustavo F. Padovan 
11670a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg)
11680a708f8fSGustavo F. Padovan {
1169525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
1170525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
11710a708f8fSGustavo F. Padovan 
117249208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
11730a708f8fSGustavo F. Padovan 
11740a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
11756a026610SGustavo F. Padovan 	chan->retry_count = 1;
11760a708f8fSGustavo F. Padovan 	__mod_monitor_timer();
11770a708f8fSGustavo F. Padovan 
1178525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_WAIT_F;
11790a708f8fSGustavo F. Padovan 
1180525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
11810a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
11820a708f8fSGustavo F. Padovan }
11830a708f8fSGustavo F. Padovan 
118442e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
11850a708f8fSGustavo F. Padovan {
11860a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
11870a708f8fSGustavo F. Padovan 
118858d35f87SGustavo F. Padovan 	while ((skb = skb_peek(&chan->tx_q)) &&
11896a026610SGustavo F. Padovan 			chan->unacked_frames) {
119042e5c802SGustavo F. Padovan 		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
11910a708f8fSGustavo F. Padovan 			break;
11920a708f8fSGustavo F. Padovan 
119358d35f87SGustavo F. Padovan 		skb = skb_dequeue(&chan->tx_q);
11940a708f8fSGustavo F. Padovan 		kfree_skb(skb);
11950a708f8fSGustavo F. Padovan 
11966a026610SGustavo F. Padovan 		chan->unacked_frames--;
11970a708f8fSGustavo F. Padovan 	}
11980a708f8fSGustavo F. Padovan 
11996a026610SGustavo F. Padovan 	if (!chan->unacked_frames)
1200e92c8e70SGustavo F. Padovan 		del_timer(&chan->retrans_timer);
12010a708f8fSGustavo F. Padovan }
12020a708f8fSGustavo F. Padovan 
12034343478fSGustavo F. Padovan void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
12040a708f8fSGustavo F. Padovan {
12058c1d787bSGustavo F. Padovan 	struct hci_conn *hcon = chan->conn->hcon;
12060a708f8fSGustavo F. Padovan 	u16 flags;
12070a708f8fSGustavo F. Padovan 
12084343478fSGustavo F. Padovan 	BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len);
12090a708f8fSGustavo F. Padovan 
12104343478fSGustavo F. Padovan 	if (!chan->flushable && lmp_no_flush_capable(hcon->hdev))
12110a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
12120a708f8fSGustavo F. Padovan 	else
12130a708f8fSGustavo F. Padovan 		flags = ACL_START;
12140a708f8fSGustavo F. Padovan 
121514b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = chan->force_active;
12160a708f8fSGustavo F. Padovan 	hci_send_acl(hcon, skb, flags);
12170a708f8fSGustavo F. Padovan }
12180a708f8fSGustavo F. Padovan 
121942e5c802SGustavo F. Padovan void l2cap_streaming_send(struct l2cap_chan *chan)
12200a708f8fSGustavo F. Padovan {
12210a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
12220a708f8fSGustavo F. Padovan 	u16 control, fcs;
12230a708f8fSGustavo F. Padovan 
122458d35f87SGustavo F. Padovan 	while ((skb = skb_dequeue(&chan->tx_q))) {
12250a708f8fSGustavo F. Padovan 		control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE);
122642e5c802SGustavo F. Padovan 		control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT;
12270a708f8fSGustavo F. Padovan 		put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE);
12280a708f8fSGustavo F. Padovan 
122947d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
12300a708f8fSGustavo F. Padovan 			fcs = crc16(0, (u8 *)skb->data, skb->len - 2);
12310a708f8fSGustavo F. Padovan 			put_unaligned_le16(fcs, skb->data + skb->len - 2);
12320a708f8fSGustavo F. Padovan 		}
12330a708f8fSGustavo F. Padovan 
12344343478fSGustavo F. Padovan 		l2cap_do_send(chan, skb);
12350a708f8fSGustavo F. Padovan 
123642e5c802SGustavo F. Padovan 		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
12370a708f8fSGustavo F. Padovan 	}
12380a708f8fSGustavo F. Padovan }
12390a708f8fSGustavo F. Padovan 
1240525cd185SGustavo F. Padovan static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq)
12410a708f8fSGustavo F. Padovan {
12420a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
12430a708f8fSGustavo F. Padovan 	u16 control, fcs;
12440a708f8fSGustavo F. Padovan 
124558d35f87SGustavo F. Padovan 	skb = skb_peek(&chan->tx_q);
12460a708f8fSGustavo F. Padovan 	if (!skb)
12470a708f8fSGustavo F. Padovan 		return;
12480a708f8fSGustavo F. Padovan 
12490a708f8fSGustavo F. Padovan 	do {
12500a708f8fSGustavo F. Padovan 		if (bt_cb(skb)->tx_seq == tx_seq)
12510a708f8fSGustavo F. Padovan 			break;
12520a708f8fSGustavo F. Padovan 
125358d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
12540a708f8fSGustavo F. Padovan 			return;
12550a708f8fSGustavo F. Padovan 
125658d35f87SGustavo F. Padovan 	} while ((skb = skb_queue_next(&chan->tx_q, skb)));
12570a708f8fSGustavo F. Padovan 
12582c03a7a4SGustavo F. Padovan 	if (chan->remote_max_tx &&
12592c03a7a4SGustavo F. Padovan 			bt_cb(skb)->retries == chan->remote_max_tx) {
12608c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
12610a708f8fSGustavo F. Padovan 		return;
12620a708f8fSGustavo F. Padovan 	}
12630a708f8fSGustavo F. Padovan 
12640a708f8fSGustavo F. Padovan 	tx_skb = skb_clone(skb, GFP_ATOMIC);
12650a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries++;
12660a708f8fSGustavo F. Padovan 	control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
1267a429b519SRuiyi Zhang 	control &= L2CAP_CTRL_SAR;
12680a708f8fSGustavo F. Padovan 
1269525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
12700a708f8fSGustavo F. Padovan 		control |= L2CAP_CTRL_FINAL;
1271525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
12720a708f8fSGustavo F. Padovan 	}
12730a708f8fSGustavo F. Padovan 
127442e5c802SGustavo F. Padovan 	control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
12750a708f8fSGustavo F. Padovan 			| (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
12760a708f8fSGustavo F. Padovan 
12770a708f8fSGustavo F. Padovan 	put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);
12780a708f8fSGustavo F. Padovan 
127947d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
12800a708f8fSGustavo F. Padovan 		fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2);
12810a708f8fSGustavo F. Padovan 		put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2);
12820a708f8fSGustavo F. Padovan 	}
12830a708f8fSGustavo F. Padovan 
12844343478fSGustavo F. Padovan 	l2cap_do_send(chan, tx_skb);
12850a708f8fSGustavo F. Padovan }
12860a708f8fSGustavo F. Padovan 
1287525cd185SGustavo F. Padovan int l2cap_ertm_send(struct l2cap_chan *chan)
12880a708f8fSGustavo F. Padovan {
12890a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
1290525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
12910a708f8fSGustavo F. Padovan 	u16 control, fcs;
12920a708f8fSGustavo F. Padovan 	int nsent = 0;
12930a708f8fSGustavo F. Padovan 
12940a708f8fSGustavo F. Padovan 	if (sk->sk_state != BT_CONNECTED)
12950a708f8fSGustavo F. Padovan 		return -ENOTCONN;
12960a708f8fSGustavo F. Padovan 
129758d35f87SGustavo F. Padovan 	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
12980a708f8fSGustavo F. Padovan 
12992c03a7a4SGustavo F. Padovan 		if (chan->remote_max_tx &&
13002c03a7a4SGustavo F. Padovan 				bt_cb(skb)->retries == chan->remote_max_tx) {
13018c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
13020a708f8fSGustavo F. Padovan 			break;
13030a708f8fSGustavo F. Padovan 		}
13040a708f8fSGustavo F. Padovan 
13050a708f8fSGustavo F. Padovan 		tx_skb = skb_clone(skb, GFP_ATOMIC);
13060a708f8fSGustavo F. Padovan 
13070a708f8fSGustavo F. Padovan 		bt_cb(skb)->retries++;
13080a708f8fSGustavo F. Padovan 
13090a708f8fSGustavo F. Padovan 		control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE);
13100a708f8fSGustavo F. Padovan 		control &= L2CAP_CTRL_SAR;
13110a708f8fSGustavo F. Padovan 
1312525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SEND_FBIT) {
13130a708f8fSGustavo F. Padovan 			control |= L2CAP_CTRL_FINAL;
1314525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_SEND_FBIT;
13150a708f8fSGustavo F. Padovan 		}
131642e5c802SGustavo F. Padovan 		control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT)
131742e5c802SGustavo F. Padovan 				| (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT);
13180a708f8fSGustavo F. Padovan 		put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE);
13190a708f8fSGustavo F. Padovan 
13200a708f8fSGustavo F. Padovan 
132147d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
13220a708f8fSGustavo F. Padovan 			fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2);
13230a708f8fSGustavo F. Padovan 			put_unaligned_le16(fcs, skb->data + tx_skb->len - 2);
13240a708f8fSGustavo F. Padovan 		}
13250a708f8fSGustavo F. Padovan 
13264343478fSGustavo F. Padovan 		l2cap_do_send(chan, tx_skb);
13270a708f8fSGustavo F. Padovan 
13280a708f8fSGustavo F. Padovan 		__mod_retrans_timer();
13290a708f8fSGustavo F. Padovan 
133042e5c802SGustavo F. Padovan 		bt_cb(skb)->tx_seq = chan->next_tx_seq;
133142e5c802SGustavo F. Padovan 		chan->next_tx_seq = (chan->next_tx_seq + 1) % 64;
13320a708f8fSGustavo F. Padovan 
133323e9fde2SSuraj Sumangala 		if (bt_cb(skb)->retries == 1)
13346a026610SGustavo F. Padovan 			chan->unacked_frames++;
133523e9fde2SSuraj Sumangala 
13366a026610SGustavo F. Padovan 		chan->frames_sent++;
13370a708f8fSGustavo F. Padovan 
133858d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
133958d35f87SGustavo F. Padovan 			chan->tx_send_head = NULL;
13400a708f8fSGustavo F. Padovan 		else
134158d35f87SGustavo F. Padovan 			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
13420a708f8fSGustavo F. Padovan 
13430a708f8fSGustavo F. Padovan 		nsent++;
13440a708f8fSGustavo F. Padovan 	}
13450a708f8fSGustavo F. Padovan 
13460a708f8fSGustavo F. Padovan 	return nsent;
13470a708f8fSGustavo F. Padovan }
13480a708f8fSGustavo F. Padovan 
1349525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan)
13500a708f8fSGustavo F. Padovan {
13510a708f8fSGustavo F. Padovan 	int ret;
13520a708f8fSGustavo F. Padovan 
135358d35f87SGustavo F. Padovan 	if (!skb_queue_empty(&chan->tx_q))
135458d35f87SGustavo F. Padovan 		chan->tx_send_head = chan->tx_q.next;
13550a708f8fSGustavo F. Padovan 
135642e5c802SGustavo F. Padovan 	chan->next_tx_seq = chan->expected_ack_seq;
1357525cd185SGustavo F. Padovan 	ret = l2cap_ertm_send(chan);
13580a708f8fSGustavo F. Padovan 	return ret;
13590a708f8fSGustavo F. Padovan }
13600a708f8fSGustavo F. Padovan 
1361525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan)
13620a708f8fSGustavo F. Padovan {
13630a708f8fSGustavo F. Padovan 	u16 control = 0;
13640a708f8fSGustavo F. Padovan 
136542e5c802SGustavo F. Padovan 	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
13660a708f8fSGustavo F. Padovan 
1367525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
13680a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_NOT_READY;
1369525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_RNR_SENT;
1370525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
13710a708f8fSGustavo F. Padovan 		return;
13720a708f8fSGustavo F. Padovan 	}
13730a708f8fSGustavo F. Padovan 
1374525cd185SGustavo F. Padovan 	if (l2cap_ertm_send(chan) > 0)
13750a708f8fSGustavo F. Padovan 		return;
13760a708f8fSGustavo F. Padovan 
13770a708f8fSGustavo F. Padovan 	control |= L2CAP_SUPER_RCV_READY;
1378525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
13790a708f8fSGustavo F. Padovan }
13800a708f8fSGustavo F. Padovan 
1381525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan)
13820a708f8fSGustavo F. Padovan {
13830a708f8fSGustavo F. Padovan 	struct srej_list *tail;
13840a708f8fSGustavo F. Padovan 	u16 control;
13850a708f8fSGustavo F. Padovan 
13860a708f8fSGustavo F. Padovan 	control = L2CAP_SUPER_SELECT_REJECT;
13870a708f8fSGustavo F. Padovan 	control |= L2CAP_CTRL_FINAL;
13880a708f8fSGustavo F. Padovan 
138939d5a3eeSGustavo F. Padovan 	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
13900a708f8fSGustavo F. Padovan 	control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
13910a708f8fSGustavo F. Padovan 
1392525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
13930a708f8fSGustavo F. Padovan }
13940a708f8fSGustavo F. Padovan 
13950a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb)
13960a708f8fSGustavo F. Padovan {
13978c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
13980a708f8fSGustavo F. Padovan 	struct sk_buff **frag;
13990a708f8fSGustavo F. Padovan 	int err, sent = 0;
14000a708f8fSGustavo F. Padovan 
14010a708f8fSGustavo F. Padovan 	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
14020a708f8fSGustavo F. Padovan 		return -EFAULT;
14030a708f8fSGustavo F. Padovan 
14040a708f8fSGustavo F. Padovan 	sent += count;
14050a708f8fSGustavo F. Padovan 	len  -= count;
14060a708f8fSGustavo F. Padovan 
14070a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
14080a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
14090a708f8fSGustavo F. Padovan 	while (len) {
14100a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
14110a708f8fSGustavo F. Padovan 
14120a708f8fSGustavo F. Padovan 		*frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err);
14130a708f8fSGustavo F. Padovan 		if (!*frag)
14140a708f8fSGustavo F. Padovan 			return err;
14150a708f8fSGustavo F. Padovan 		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
14160a708f8fSGustavo F. Padovan 			return -EFAULT;
14170a708f8fSGustavo F. Padovan 
14180a708f8fSGustavo F. Padovan 		sent += count;
14190a708f8fSGustavo F. Padovan 		len  -= count;
14200a708f8fSGustavo F. Padovan 
14210a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
14220a708f8fSGustavo F. Padovan 	}
14230a708f8fSGustavo F. Padovan 
14240a708f8fSGustavo F. Padovan 	return sent;
14250a708f8fSGustavo F. Padovan }
14260a708f8fSGustavo F. Padovan 
1427fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
14280a708f8fSGustavo F. Padovan {
1429fe4128e0SGustavo F. Padovan 	struct sock *sk = chan->sk;
14308c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
14310a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
14320a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE + 2;
14330a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
14340a708f8fSGustavo F. Padovan 
14350a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
14360a708f8fSGustavo F. Padovan 
14370a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
14380a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
14390a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
14400a708f8fSGustavo F. Padovan 	if (!skb)
14410a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
14420a708f8fSGustavo F. Padovan 
14430a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
14440a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1445fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
14460a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1447fe4128e0SGustavo F. Padovan 	put_unaligned_le16(chan->psm, skb_put(skb, 2));
14480a708f8fSGustavo F. Padovan 
14490a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
14500a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
14510a708f8fSGustavo F. Padovan 		kfree_skb(skb);
14520a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
14530a708f8fSGustavo F. Padovan 	}
14540a708f8fSGustavo F. Padovan 	return skb;
14550a708f8fSGustavo F. Padovan }
14560a708f8fSGustavo F. Padovan 
1457fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
14580a708f8fSGustavo F. Padovan {
1459fe4128e0SGustavo F. Padovan 	struct sock *sk = chan->sk;
14608c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
14610a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
14620a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE;
14630a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
14640a708f8fSGustavo F. Padovan 
14650a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
14660a708f8fSGustavo F. Padovan 
14670a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
14680a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
14690a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
14700a708f8fSGustavo F. Padovan 	if (!skb)
14710a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
14720a708f8fSGustavo F. Padovan 
14730a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
14740a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1475fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
14760a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
14770a708f8fSGustavo F. Padovan 
14780a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
14790a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
14800a708f8fSGustavo F. Padovan 		kfree_skb(skb);
14810a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
14820a708f8fSGustavo F. Padovan 	}
14830a708f8fSGustavo F. Padovan 	return skb;
14840a708f8fSGustavo F. Padovan }
14850a708f8fSGustavo F. Padovan 
148647d1ec61SGustavo F. Padovan struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen)
14870a708f8fSGustavo F. Padovan {
148847d1ec61SGustavo F. Padovan 	struct sock *sk = chan->sk;
14898c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
14900a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
14910a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE + 2;
14920a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
14930a708f8fSGustavo F. Padovan 
14940a708f8fSGustavo F. Padovan 	BT_DBG("sk %p len %d", sk, (int)len);
14950a708f8fSGustavo F. Padovan 
14960a708f8fSGustavo F. Padovan 	if (!conn)
14970a708f8fSGustavo F. Padovan 		return ERR_PTR(-ENOTCONN);
14980a708f8fSGustavo F. Padovan 
14990a708f8fSGustavo F. Padovan 	if (sdulen)
15000a708f8fSGustavo F. Padovan 		hlen += 2;
15010a708f8fSGustavo F. Padovan 
150247d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
15030a708f8fSGustavo F. Padovan 		hlen += 2;
15040a708f8fSGustavo F. Padovan 
15050a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
15060a708f8fSGustavo F. Padovan 	skb = bt_skb_send_alloc(sk, count + hlen,
15070a708f8fSGustavo F. Padovan 			msg->msg_flags & MSG_DONTWAIT, &err);
15080a708f8fSGustavo F. Padovan 	if (!skb)
15090a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15100a708f8fSGustavo F. Padovan 
15110a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
15120a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1513fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
15140a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
15150a708f8fSGustavo F. Padovan 	put_unaligned_le16(control, skb_put(skb, 2));
15160a708f8fSGustavo F. Padovan 	if (sdulen)
15170a708f8fSGustavo F. Padovan 		put_unaligned_le16(sdulen, skb_put(skb, 2));
15180a708f8fSGustavo F. Padovan 
15190a708f8fSGustavo F. Padovan 	err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb);
15200a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
15210a708f8fSGustavo F. Padovan 		kfree_skb(skb);
15220a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
15230a708f8fSGustavo F. Padovan 	}
15240a708f8fSGustavo F. Padovan 
152547d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
15260a708f8fSGustavo F. Padovan 		put_unaligned_le16(0, skb_put(skb, 2));
15270a708f8fSGustavo F. Padovan 
15280a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries = 0;
15290a708f8fSGustavo F. Padovan 	return skb;
15300a708f8fSGustavo F. Padovan }
15310a708f8fSGustavo F. Padovan 
15322c03a7a4SGustavo F. Padovan int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
15330a708f8fSGustavo F. Padovan {
15340a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
15350a708f8fSGustavo F. Padovan 	struct sk_buff_head sar_queue;
15360a708f8fSGustavo F. Padovan 	u16 control;
15370a708f8fSGustavo F. Padovan 	size_t size = 0;
15380a708f8fSGustavo F. Padovan 
15390a708f8fSGustavo F. Padovan 	skb_queue_head_init(&sar_queue);
15400a708f8fSGustavo F. Padovan 	control = L2CAP_SDU_START;
154147d1ec61SGustavo F. Padovan 	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
15420a708f8fSGustavo F. Padovan 	if (IS_ERR(skb))
15430a708f8fSGustavo F. Padovan 		return PTR_ERR(skb);
15440a708f8fSGustavo F. Padovan 
15450a708f8fSGustavo F. Padovan 	__skb_queue_tail(&sar_queue, skb);
15462c03a7a4SGustavo F. Padovan 	len -= chan->remote_mps;
15472c03a7a4SGustavo F. Padovan 	size += chan->remote_mps;
15480a708f8fSGustavo F. Padovan 
15490a708f8fSGustavo F. Padovan 	while (len > 0) {
15500a708f8fSGustavo F. Padovan 		size_t buflen;
15510a708f8fSGustavo F. Padovan 
15522c03a7a4SGustavo F. Padovan 		if (len > chan->remote_mps) {
15530a708f8fSGustavo F. Padovan 			control = L2CAP_SDU_CONTINUE;
15542c03a7a4SGustavo F. Padovan 			buflen = chan->remote_mps;
15550a708f8fSGustavo F. Padovan 		} else {
15560a708f8fSGustavo F. Padovan 			control = L2CAP_SDU_END;
15570a708f8fSGustavo F. Padovan 			buflen = len;
15580a708f8fSGustavo F. Padovan 		}
15590a708f8fSGustavo F. Padovan 
156047d1ec61SGustavo F. Padovan 		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
15610a708f8fSGustavo F. Padovan 		if (IS_ERR(skb)) {
15620a708f8fSGustavo F. Padovan 			skb_queue_purge(&sar_queue);
15630a708f8fSGustavo F. Padovan 			return PTR_ERR(skb);
15640a708f8fSGustavo F. Padovan 		}
15650a708f8fSGustavo F. Padovan 
15660a708f8fSGustavo F. Padovan 		__skb_queue_tail(&sar_queue, skb);
15670a708f8fSGustavo F. Padovan 		len -= buflen;
15680a708f8fSGustavo F. Padovan 		size += buflen;
15690a708f8fSGustavo F. Padovan 	}
157058d35f87SGustavo F. Padovan 	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
157158d35f87SGustavo F. Padovan 	if (chan->tx_send_head == NULL)
157258d35f87SGustavo F. Padovan 		chan->tx_send_head = sar_queue.next;
15730a708f8fSGustavo F. Padovan 
15740a708f8fSGustavo F. Padovan 	return size;
15750a708f8fSGustavo F. Padovan }
15760a708f8fSGustavo F. Padovan 
15779a91a04aSGustavo F. Padovan int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
15789a91a04aSGustavo F. Padovan {
15799a91a04aSGustavo F. Padovan 	struct sk_buff *skb;
15809a91a04aSGustavo F. Padovan 	u16 control;
15819a91a04aSGustavo F. Padovan 	int err;
15829a91a04aSGustavo F. Padovan 
15839a91a04aSGustavo F. Padovan 	/* Connectionless channel */
1584715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
15859a91a04aSGustavo F. Padovan 		skb = l2cap_create_connless_pdu(chan, msg, len);
15869a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
15879a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
15889a91a04aSGustavo F. Padovan 
15899a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
15909a91a04aSGustavo F. Padovan 		return len;
15919a91a04aSGustavo F. Padovan 	}
15929a91a04aSGustavo F. Padovan 
15939a91a04aSGustavo F. Padovan 	switch (chan->mode) {
15949a91a04aSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
15959a91a04aSGustavo F. Padovan 		/* Check outgoing MTU */
15969a91a04aSGustavo F. Padovan 		if (len > chan->omtu)
15979a91a04aSGustavo F. Padovan 			return -EMSGSIZE;
15989a91a04aSGustavo F. Padovan 
15999a91a04aSGustavo F. Padovan 		/* Create a basic PDU */
16009a91a04aSGustavo F. Padovan 		skb = l2cap_create_basic_pdu(chan, msg, len);
16019a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
16029a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
16039a91a04aSGustavo F. Padovan 
16049a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
16059a91a04aSGustavo F. Padovan 		err = len;
16069a91a04aSGustavo F. Padovan 		break;
16079a91a04aSGustavo F. Padovan 
16089a91a04aSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
16099a91a04aSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
16109a91a04aSGustavo F. Padovan 		/* Entire SDU fits into one PDU */
16119a91a04aSGustavo F. Padovan 		if (len <= chan->remote_mps) {
16129a91a04aSGustavo F. Padovan 			control = L2CAP_SDU_UNSEGMENTED;
16139a91a04aSGustavo F. Padovan 			skb = l2cap_create_iframe_pdu(chan, msg, len, control,
16149a91a04aSGustavo F. Padovan 									0);
16159a91a04aSGustavo F. Padovan 			if (IS_ERR(skb))
16169a91a04aSGustavo F. Padovan 				return PTR_ERR(skb);
16179a91a04aSGustavo F. Padovan 
16189a91a04aSGustavo F. Padovan 			__skb_queue_tail(&chan->tx_q, skb);
16199a91a04aSGustavo F. Padovan 
16209a91a04aSGustavo F. Padovan 			if (chan->tx_send_head == NULL)
16219a91a04aSGustavo F. Padovan 				chan->tx_send_head = skb;
16229a91a04aSGustavo F. Padovan 
16239a91a04aSGustavo F. Padovan 		} else {
16249a91a04aSGustavo F. Padovan 			/* Segment SDU into multiples PDUs */
16259a91a04aSGustavo F. Padovan 			err = l2cap_sar_segment_sdu(chan, msg, len);
16269a91a04aSGustavo F. Padovan 			if (err < 0)
16279a91a04aSGustavo F. Padovan 				return err;
16289a91a04aSGustavo F. Padovan 		}
16299a91a04aSGustavo F. Padovan 
16309a91a04aSGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_STREAMING) {
16319a91a04aSGustavo F. Padovan 			l2cap_streaming_send(chan);
16329a91a04aSGustavo F. Padovan 			err = len;
16339a91a04aSGustavo F. Padovan 			break;
16349a91a04aSGustavo F. Padovan 		}
16359a91a04aSGustavo F. Padovan 
16369a91a04aSGustavo F. Padovan 		if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
16379a91a04aSGustavo F. Padovan 				(chan->conn_state & L2CAP_CONN_WAIT_F)) {
16389a91a04aSGustavo F. Padovan 			err = len;
16399a91a04aSGustavo F. Padovan 			break;
16409a91a04aSGustavo F. Padovan 		}
16419a91a04aSGustavo F. Padovan 
16429a91a04aSGustavo F. Padovan 		err = l2cap_ertm_send(chan);
16439a91a04aSGustavo F. Padovan 		if (err >= 0)
16449a91a04aSGustavo F. Padovan 			err = len;
16459a91a04aSGustavo F. Padovan 
16469a91a04aSGustavo F. Padovan 		break;
16479a91a04aSGustavo F. Padovan 
16489a91a04aSGustavo F. Padovan 	default:
16499a91a04aSGustavo F. Padovan 		BT_DBG("bad state %1.1x", chan->mode);
16509a91a04aSGustavo F. Padovan 		err = -EBADFD;
16519a91a04aSGustavo F. Padovan 	}
16529a91a04aSGustavo F. Padovan 
16539a91a04aSGustavo F. Padovan 	return err;
16549a91a04aSGustavo F. Padovan }
16559a91a04aSGustavo F. Padovan 
16560a708f8fSGustavo F. Padovan static void l2cap_chan_ready(struct sock *sk)
16570a708f8fSGustavo F. Padovan {
16580a708f8fSGustavo F. Padovan 	struct sock *parent = bt_sk(sk)->parent;
1659b4450035SGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
16600a708f8fSGustavo F. Padovan 
16610a708f8fSGustavo F. Padovan 	BT_DBG("sk %p, parent %p", sk, parent);
16620a708f8fSGustavo F. Padovan 
1663b4450035SGustavo F. Padovan 	chan->conf_state = 0;
1664ab07801dSGustavo F. Padovan 	l2cap_chan_clear_timer(chan);
16650a708f8fSGustavo F. Padovan 
16660a708f8fSGustavo F. Padovan 	if (!parent) {
16670a708f8fSGustavo F. Padovan 		/* Outgoing channel.
16680a708f8fSGustavo F. Padovan 		 * Wake up socket sleeping on connect.
16690a708f8fSGustavo F. Padovan 		 */
16700a708f8fSGustavo F. Padovan 		sk->sk_state = BT_CONNECTED;
16710a708f8fSGustavo F. Padovan 		sk->sk_state_change(sk);
16720a708f8fSGustavo F. Padovan 	} else {
16730a708f8fSGustavo F. Padovan 		/* Incoming channel.
16740a708f8fSGustavo F. Padovan 		 * Wake up socket sleeping on accept.
16750a708f8fSGustavo F. Padovan 		 */
16760a708f8fSGustavo F. Padovan 		parent->sk_data_ready(parent, 0);
16770a708f8fSGustavo F. Padovan 	}
16780a708f8fSGustavo F. Padovan }
16790a708f8fSGustavo F. Padovan 
16800a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */
16810a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
16820a708f8fSGustavo F. Padovan {
16830a708f8fSGustavo F. Padovan 	struct sk_buff *nskb;
168448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
16850a708f8fSGustavo F. Padovan 
16860a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
16870a708f8fSGustavo F. Padovan 
1688baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
1689baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
169048454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
1691715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_RAW)
16920a708f8fSGustavo F. Padovan 			continue;
16930a708f8fSGustavo F. Padovan 
16940a708f8fSGustavo F. Padovan 		/* Don't send frame to the socket it came from */
16950a708f8fSGustavo F. Padovan 		if (skb->sk == sk)
16960a708f8fSGustavo F. Padovan 			continue;
16970a708f8fSGustavo F. Padovan 		nskb = skb_clone(skb, GFP_ATOMIC);
16980a708f8fSGustavo F. Padovan 		if (!nskb)
16990a708f8fSGustavo F. Padovan 			continue;
17000a708f8fSGustavo F. Padovan 
17010a708f8fSGustavo F. Padovan 		if (sock_queue_rcv_skb(sk, nskb))
17020a708f8fSGustavo F. Padovan 			kfree_skb(nskb);
17030a708f8fSGustavo F. Padovan 	}
1704baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
17050a708f8fSGustavo F. Padovan }
17060a708f8fSGustavo F. Padovan 
17070a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */
17080a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
17090a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data)
17100a708f8fSGustavo F. Padovan {
17110a708f8fSGustavo F. Padovan 	struct sk_buff *skb, **frag;
17120a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr *cmd;
17130a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
17140a708f8fSGustavo F. Padovan 	int len, count;
17150a708f8fSGustavo F. Padovan 
17160a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
17170a708f8fSGustavo F. Padovan 			conn, code, ident, dlen);
17180a708f8fSGustavo F. Padovan 
17190a708f8fSGustavo F. Padovan 	len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
17200a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, len);
17210a708f8fSGustavo F. Padovan 
17220a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
17230a708f8fSGustavo F. Padovan 	if (!skb)
17240a708f8fSGustavo F. Padovan 		return NULL;
17250a708f8fSGustavo F. Padovan 
17260a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
17270a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
17283300d9a9SClaudio Takahasi 
17293300d9a9SClaudio Takahasi 	if (conn->hcon->type == LE_LINK)
17303300d9a9SClaudio Takahasi 		lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
17313300d9a9SClaudio Takahasi 	else
17320a708f8fSGustavo F. Padovan 		lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
17330a708f8fSGustavo F. Padovan 
17340a708f8fSGustavo F. Padovan 	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
17350a708f8fSGustavo F. Padovan 	cmd->code  = code;
17360a708f8fSGustavo F. Padovan 	cmd->ident = ident;
17370a708f8fSGustavo F. Padovan 	cmd->len   = cpu_to_le16(dlen);
17380a708f8fSGustavo F. Padovan 
17390a708f8fSGustavo F. Padovan 	if (dlen) {
17400a708f8fSGustavo F. Padovan 		count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
17410a708f8fSGustavo F. Padovan 		memcpy(skb_put(skb, count), data, count);
17420a708f8fSGustavo F. Padovan 		data += count;
17430a708f8fSGustavo F. Padovan 	}
17440a708f8fSGustavo F. Padovan 
17450a708f8fSGustavo F. Padovan 	len -= skb->len;
17460a708f8fSGustavo F. Padovan 
17470a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
17480a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
17490a708f8fSGustavo F. Padovan 	while (len) {
17500a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
17510a708f8fSGustavo F. Padovan 
17520a708f8fSGustavo F. Padovan 		*frag = bt_skb_alloc(count, GFP_ATOMIC);
17530a708f8fSGustavo F. Padovan 		if (!*frag)
17540a708f8fSGustavo F. Padovan 			goto fail;
17550a708f8fSGustavo F. Padovan 
17560a708f8fSGustavo F. Padovan 		memcpy(skb_put(*frag, count), data, count);
17570a708f8fSGustavo F. Padovan 
17580a708f8fSGustavo F. Padovan 		len  -= count;
17590a708f8fSGustavo F. Padovan 		data += count;
17600a708f8fSGustavo F. Padovan 
17610a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
17620a708f8fSGustavo F. Padovan 	}
17630a708f8fSGustavo F. Padovan 
17640a708f8fSGustavo F. Padovan 	return skb;
17650a708f8fSGustavo F. Padovan 
17660a708f8fSGustavo F. Padovan fail:
17670a708f8fSGustavo F. Padovan 	kfree_skb(skb);
17680a708f8fSGustavo F. Padovan 	return NULL;
17690a708f8fSGustavo F. Padovan }
17700a708f8fSGustavo F. Padovan 
17710a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
17720a708f8fSGustavo F. Padovan {
17730a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
17740a708f8fSGustavo F. Padovan 	int len;
17750a708f8fSGustavo F. Padovan 
17760a708f8fSGustavo F. Padovan 	len = L2CAP_CONF_OPT_SIZE + opt->len;
17770a708f8fSGustavo F. Padovan 	*ptr += len;
17780a708f8fSGustavo F. Padovan 
17790a708f8fSGustavo F. Padovan 	*type = opt->type;
17800a708f8fSGustavo F. Padovan 	*olen = opt->len;
17810a708f8fSGustavo F. Padovan 
17820a708f8fSGustavo F. Padovan 	switch (opt->len) {
17830a708f8fSGustavo F. Padovan 	case 1:
17840a708f8fSGustavo F. Padovan 		*val = *((u8 *) opt->val);
17850a708f8fSGustavo F. Padovan 		break;
17860a708f8fSGustavo F. Padovan 
17870a708f8fSGustavo F. Padovan 	case 2:
17880a708f8fSGustavo F. Padovan 		*val = get_unaligned_le16(opt->val);
17890a708f8fSGustavo F. Padovan 		break;
17900a708f8fSGustavo F. Padovan 
17910a708f8fSGustavo F. Padovan 	case 4:
17920a708f8fSGustavo F. Padovan 		*val = get_unaligned_le32(opt->val);
17930a708f8fSGustavo F. Padovan 		break;
17940a708f8fSGustavo F. Padovan 
17950a708f8fSGustavo F. Padovan 	default:
17960a708f8fSGustavo F. Padovan 		*val = (unsigned long) opt->val;
17970a708f8fSGustavo F. Padovan 		break;
17980a708f8fSGustavo F. Padovan 	}
17990a708f8fSGustavo F. Padovan 
18000a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
18010a708f8fSGustavo F. Padovan 	return len;
18020a708f8fSGustavo F. Padovan }
18030a708f8fSGustavo F. Padovan 
18040a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
18050a708f8fSGustavo F. Padovan {
18060a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
18070a708f8fSGustavo F. Padovan 
18080a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
18090a708f8fSGustavo F. Padovan 
18100a708f8fSGustavo F. Padovan 	opt->type = type;
18110a708f8fSGustavo F. Padovan 	opt->len  = len;
18120a708f8fSGustavo F. Padovan 
18130a708f8fSGustavo F. Padovan 	switch (len) {
18140a708f8fSGustavo F. Padovan 	case 1:
18150a708f8fSGustavo F. Padovan 		*((u8 *) opt->val)  = val;
18160a708f8fSGustavo F. Padovan 		break;
18170a708f8fSGustavo F. Padovan 
18180a708f8fSGustavo F. Padovan 	case 2:
18190a708f8fSGustavo F. Padovan 		put_unaligned_le16(val, opt->val);
18200a708f8fSGustavo F. Padovan 		break;
18210a708f8fSGustavo F. Padovan 
18220a708f8fSGustavo F. Padovan 	case 4:
18230a708f8fSGustavo F. Padovan 		put_unaligned_le32(val, opt->val);
18240a708f8fSGustavo F. Padovan 		break;
18250a708f8fSGustavo F. Padovan 
18260a708f8fSGustavo F. Padovan 	default:
18270a708f8fSGustavo F. Padovan 		memcpy(opt->val, (void *) val, len);
18280a708f8fSGustavo F. Padovan 		break;
18290a708f8fSGustavo F. Padovan 	}
18300a708f8fSGustavo F. Padovan 
18310a708f8fSGustavo F. Padovan 	*ptr += L2CAP_CONF_OPT_SIZE + len;
18320a708f8fSGustavo F. Padovan }
18330a708f8fSGustavo F. Padovan 
18340a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg)
18350a708f8fSGustavo F. Padovan {
1836525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = (void *) arg;
18370a708f8fSGustavo F. Padovan 
1838525cd185SGustavo F. Padovan 	bh_lock_sock(chan->sk);
1839525cd185SGustavo F. Padovan 	l2cap_send_ack(chan);
1840525cd185SGustavo F. Padovan 	bh_unlock_sock(chan->sk);
18410a708f8fSGustavo F. Padovan }
18420a708f8fSGustavo F. Padovan 
1843525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan)
18440a708f8fSGustavo F. Padovan {
1845525cd185SGustavo F. Padovan 	struct sock *sk = chan->sk;
1846525cd185SGustavo F. Padovan 
184742e5c802SGustavo F. Padovan 	chan->expected_ack_seq = 0;
18486a026610SGustavo F. Padovan 	chan->unacked_frames = 0;
184942e5c802SGustavo F. Padovan 	chan->buffer_seq = 0;
18506a026610SGustavo F. Padovan 	chan->num_acked = 0;
18516a026610SGustavo F. Padovan 	chan->frames_sent = 0;
18520a708f8fSGustavo F. Padovan 
1853e92c8e70SGustavo F. Padovan 	setup_timer(&chan->retrans_timer, l2cap_retrans_timeout,
1854e92c8e70SGustavo F. Padovan 							(unsigned long) chan);
1855e92c8e70SGustavo F. Padovan 	setup_timer(&chan->monitor_timer, l2cap_monitor_timeout,
1856e92c8e70SGustavo F. Padovan 							(unsigned long) chan);
1857e92c8e70SGustavo F. Padovan 	setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan);
18580a708f8fSGustavo F. Padovan 
1859f1c6775bSGustavo F. Padovan 	skb_queue_head_init(&chan->srej_q);
1860f1c6775bSGustavo F. Padovan 	skb_queue_head_init(&chan->busy_q);
18610a708f8fSGustavo F. Padovan 
186239d5a3eeSGustavo F. Padovan 	INIT_LIST_HEAD(&chan->srej_l);
186339d5a3eeSGustavo F. Padovan 
1864311bb895SGustavo F. Padovan 	INIT_WORK(&chan->busy_work, l2cap_busy_work);
18650a708f8fSGustavo F. Padovan 
18660a708f8fSGustavo F. Padovan 	sk->sk_backlog_rcv = l2cap_ertm_data_rcv;
18670a708f8fSGustavo F. Padovan }
18680a708f8fSGustavo F. Padovan 
18690a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
18700a708f8fSGustavo F. Padovan {
18710a708f8fSGustavo F. Padovan 	switch (mode) {
18720a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
18730a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
18740a708f8fSGustavo F. Padovan 		if (l2cap_mode_supported(mode, remote_feat_mask))
18750a708f8fSGustavo F. Padovan 			return mode;
18760a708f8fSGustavo F. Padovan 		/* fall through */
18770a708f8fSGustavo F. Padovan 	default:
18780a708f8fSGustavo F. Padovan 		return L2CAP_MODE_BASIC;
18790a708f8fSGustavo F. Padovan 	}
18800a708f8fSGustavo F. Padovan }
18810a708f8fSGustavo F. Padovan 
1882710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
18830a708f8fSGustavo F. Padovan {
18840a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
18850c1bc5c6SGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
18860a708f8fSGustavo F. Padovan 	void *ptr = req->data;
18870a708f8fSGustavo F. Padovan 
188849208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
18890a708f8fSGustavo F. Padovan 
189073ffa904SGustavo F. Padovan 	if (chan->num_conf_req || chan->num_conf_rsp)
18910a708f8fSGustavo F. Padovan 		goto done;
18920a708f8fSGustavo F. Padovan 
18930c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
18940a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
18950a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
1896b4450035SGustavo F. Padovan 		if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE)
18970a708f8fSGustavo F. Padovan 			break;
18980a708f8fSGustavo F. Padovan 
18990a708f8fSGustavo F. Padovan 		/* fall through */
19000a708f8fSGustavo F. Padovan 	default:
19018c1d787bSGustavo F. Padovan 		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
19020a708f8fSGustavo F. Padovan 		break;
19030a708f8fSGustavo F. Padovan 	}
19040a708f8fSGustavo F. Padovan 
19050a708f8fSGustavo F. Padovan done:
19060c1bc5c6SGustavo F. Padovan 	if (chan->imtu != L2CAP_DEFAULT_MTU)
19070c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
19080a708f8fSGustavo F. Padovan 
19090c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
19100a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
19118c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
19128c1d787bSGustavo F. Padovan 				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
19130a708f8fSGustavo F. Padovan 			break;
19140a708f8fSGustavo F. Padovan 
19150a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_BASIC;
19160a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
19170a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
19180a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
19190a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
19200a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = 0;
19210a708f8fSGustavo F. Padovan 
19220a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
19230a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
19240a708f8fSGustavo F. Padovan 		break;
19250a708f8fSGustavo F. Padovan 
19260a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
19270a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_ERTM;
192847d1ec61SGustavo F. Padovan 		rfc.txwin_size      = chan->tx_win;
192947d1ec61SGustavo F. Padovan 		rfc.max_transmit    = chan->max_tx;
19300a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
19310a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
19320a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
19338c1d787bSGustavo F. Padovan 		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
19348c1d787bSGustavo F. Padovan 			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
19350a708f8fSGustavo F. Padovan 
19360a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
19370a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
19380a708f8fSGustavo F. Padovan 
19398c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
19400a708f8fSGustavo F. Padovan 			break;
19410a708f8fSGustavo F. Padovan 
194247d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
1943b4450035SGustavo F. Padovan 				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
194447d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
194547d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
19460a708f8fSGustavo F. Padovan 		}
19470a708f8fSGustavo F. Padovan 		break;
19480a708f8fSGustavo F. Padovan 
19490a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
19500a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_STREAMING;
19510a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
19520a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
19530a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
19540a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
19550a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
19568c1d787bSGustavo F. Padovan 		if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10)
19578c1d787bSGustavo F. Padovan 			rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
19580a708f8fSGustavo F. Padovan 
19590a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
19600a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
19610a708f8fSGustavo F. Padovan 
19628c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
19630a708f8fSGustavo F. Padovan 			break;
19640a708f8fSGustavo F. Padovan 
196547d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
1966b4450035SGustavo F. Padovan 				chan->conf_state & L2CAP_CONF_NO_FCS_RECV) {
196747d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
196847d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
19690a708f8fSGustavo F. Padovan 		}
19700a708f8fSGustavo F. Padovan 		break;
19710a708f8fSGustavo F. Padovan 	}
19720a708f8fSGustavo F. Padovan 
1973fe4128e0SGustavo F. Padovan 	req->dcid  = cpu_to_le16(chan->dcid);
19740a708f8fSGustavo F. Padovan 	req->flags = cpu_to_le16(0);
19750a708f8fSGustavo F. Padovan 
19760a708f8fSGustavo F. Padovan 	return ptr - data;
19770a708f8fSGustavo F. Padovan }
19780a708f8fSGustavo F. Padovan 
197973ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
19800a708f8fSGustavo F. Padovan {
19810a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
19820a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
198373ffa904SGustavo F. Padovan 	void *req = chan->conf_req;
198473ffa904SGustavo F. Padovan 	int len = chan->conf_len;
19850a708f8fSGustavo F. Padovan 	int type, hint, olen;
19860a708f8fSGustavo F. Padovan 	unsigned long val;
19870a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
19880a708f8fSGustavo F. Padovan 	u16 mtu = L2CAP_DEFAULT_MTU;
19890a708f8fSGustavo F. Padovan 	u16 result = L2CAP_CONF_SUCCESS;
19900a708f8fSGustavo F. Padovan 
199173ffa904SGustavo F. Padovan 	BT_DBG("chan %p", chan);
19920a708f8fSGustavo F. Padovan 
19930a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
19940a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
19950a708f8fSGustavo F. Padovan 
19960a708f8fSGustavo F. Padovan 		hint  = type & L2CAP_CONF_HINT;
19970a708f8fSGustavo F. Padovan 		type &= L2CAP_CONF_MASK;
19980a708f8fSGustavo F. Padovan 
19990a708f8fSGustavo F. Padovan 		switch (type) {
20000a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
20010a708f8fSGustavo F. Padovan 			mtu = val;
20020a708f8fSGustavo F. Padovan 			break;
20030a708f8fSGustavo F. Padovan 
20040a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
20050c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
20060a708f8fSGustavo F. Padovan 			break;
20070a708f8fSGustavo F. Padovan 
20080a708f8fSGustavo F. Padovan 		case L2CAP_CONF_QOS:
20090a708f8fSGustavo F. Padovan 			break;
20100a708f8fSGustavo F. Padovan 
20110a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
20120a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
20130a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *) val, olen);
20140a708f8fSGustavo F. Padovan 			break;
20150a708f8fSGustavo F. Padovan 
20160a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FCS:
20170a708f8fSGustavo F. Padovan 			if (val == L2CAP_FCS_NONE)
2018b4450035SGustavo F. Padovan 				chan->conf_state |= L2CAP_CONF_NO_FCS_RECV;
20190a708f8fSGustavo F. Padovan 
20200a708f8fSGustavo F. Padovan 			break;
20210a708f8fSGustavo F. Padovan 
20220a708f8fSGustavo F. Padovan 		default:
20230a708f8fSGustavo F. Padovan 			if (hint)
20240a708f8fSGustavo F. Padovan 				break;
20250a708f8fSGustavo F. Padovan 
20260a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNKNOWN;
20270a708f8fSGustavo F. Padovan 			*((u8 *) ptr++) = type;
20280a708f8fSGustavo F. Padovan 			break;
20290a708f8fSGustavo F. Padovan 		}
20300a708f8fSGustavo F. Padovan 	}
20310a708f8fSGustavo F. Padovan 
203273ffa904SGustavo F. Padovan 	if (chan->num_conf_rsp || chan->num_conf_req > 1)
20330a708f8fSGustavo F. Padovan 		goto done;
20340a708f8fSGustavo F. Padovan 
20350c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
20360a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
20370a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
2038b4450035SGustavo F. Padovan 		if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) {
20390c1bc5c6SGustavo F. Padovan 			chan->mode = l2cap_select_mode(rfc.mode,
20408c1d787bSGustavo F. Padovan 					chan->conn->feat_mask);
20410a708f8fSGustavo F. Padovan 			break;
20420a708f8fSGustavo F. Padovan 		}
20430a708f8fSGustavo F. Padovan 
20440c1bc5c6SGustavo F. Padovan 		if (chan->mode != rfc.mode)
20450a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
20460a708f8fSGustavo F. Padovan 
20470a708f8fSGustavo F. Padovan 		break;
20480a708f8fSGustavo F. Padovan 	}
20490a708f8fSGustavo F. Padovan 
20500a708f8fSGustavo F. Padovan done:
20510c1bc5c6SGustavo F. Padovan 	if (chan->mode != rfc.mode) {
20520a708f8fSGustavo F. Padovan 		result = L2CAP_CONF_UNACCEPT;
20530c1bc5c6SGustavo F. Padovan 		rfc.mode = chan->mode;
20540a708f8fSGustavo F. Padovan 
205573ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp == 1)
20560a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
20570a708f8fSGustavo F. Padovan 
20580a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
20590a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
20600a708f8fSGustavo F. Padovan 	}
20610a708f8fSGustavo F. Padovan 
20620a708f8fSGustavo F. Padovan 
20630a708f8fSGustavo F. Padovan 	if (result == L2CAP_CONF_SUCCESS) {
20640a708f8fSGustavo F. Padovan 		/* Configure output options and let the other side know
20650a708f8fSGustavo F. Padovan 		 * which ones we don't like. */
20660a708f8fSGustavo F. Padovan 
20670a708f8fSGustavo F. Padovan 		if (mtu < L2CAP_DEFAULT_MIN_MTU)
20680a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
20690a708f8fSGustavo F. Padovan 		else {
20700c1bc5c6SGustavo F. Padovan 			chan->omtu = mtu;
2071b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MTU_DONE;
20720a708f8fSGustavo F. Padovan 		}
20730c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
20740a708f8fSGustavo F. Padovan 
20750a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
20760a708f8fSGustavo F. Padovan 		case L2CAP_MODE_BASIC:
207747d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
2078b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MODE_DONE;
20790a708f8fSGustavo F. Padovan 			break;
20800a708f8fSGustavo F. Padovan 
20810a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
20822c03a7a4SGustavo F. Padovan 			chan->remote_tx_win = rfc.txwin_size;
20832c03a7a4SGustavo F. Padovan 			chan->remote_max_tx = rfc.max_transmit;
20840a708f8fSGustavo F. Padovan 
20858c1d787bSGustavo F. Padovan 			if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10)
20868c1d787bSGustavo F. Padovan 				rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10);
20870a708f8fSGustavo F. Padovan 
20882c03a7a4SGustavo F. Padovan 			chan->remote_mps = le16_to_cpu(rfc.max_pdu_size);
20890a708f8fSGustavo F. Padovan 
20900a708f8fSGustavo F. Padovan 			rfc.retrans_timeout =
20910a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO);
20920a708f8fSGustavo F. Padovan 			rfc.monitor_timeout =
20930a708f8fSGustavo F. Padovan 				le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO);
20940a708f8fSGustavo F. Padovan 
2095b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MODE_DONE;
20960a708f8fSGustavo F. Padovan 
20970a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
20980a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
20990a708f8fSGustavo F. Padovan 
21000a708f8fSGustavo F. Padovan 			break;
21010a708f8fSGustavo F. Padovan 
21020a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
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 
2108b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_MODE_DONE;
21090a708f8fSGustavo F. Padovan 
21100a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
21110a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
21120a708f8fSGustavo F. Padovan 
21130a708f8fSGustavo F. Padovan 			break;
21140a708f8fSGustavo F. Padovan 
21150a708f8fSGustavo F. Padovan 		default:
21160a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
21170a708f8fSGustavo F. Padovan 
21180a708f8fSGustavo F. Padovan 			memset(&rfc, 0, sizeof(rfc));
21190c1bc5c6SGustavo F. Padovan 			rfc.mode = chan->mode;
21200a708f8fSGustavo F. Padovan 		}
21210a708f8fSGustavo F. Padovan 
21220a708f8fSGustavo F. Padovan 		if (result == L2CAP_CONF_SUCCESS)
2123b4450035SGustavo F. Padovan 			chan->conf_state |= L2CAP_CONF_OUTPUT_DONE;
21240a708f8fSGustavo F. Padovan 	}
2125fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
21260a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
21270a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(0x0000);
21280a708f8fSGustavo F. Padovan 
21290a708f8fSGustavo F. Padovan 	return ptr - data;
21300a708f8fSGustavo F. Padovan }
21310a708f8fSGustavo F. Padovan 
2132b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
21330a708f8fSGustavo F. Padovan {
21340a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
21350a708f8fSGustavo F. Padovan 	void *ptr = req->data;
21360a708f8fSGustavo F. Padovan 	int type, olen;
21370a708f8fSGustavo F. Padovan 	unsigned long val;
21380a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
21390a708f8fSGustavo F. Padovan 
2140fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
21410a708f8fSGustavo F. Padovan 
21420a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
21430a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
21440a708f8fSGustavo F. Padovan 
21450a708f8fSGustavo F. Padovan 		switch (type) {
21460a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
21470a708f8fSGustavo F. Padovan 			if (val < L2CAP_DEFAULT_MIN_MTU) {
21480a708f8fSGustavo F. Padovan 				*result = L2CAP_CONF_UNACCEPT;
21490c1bc5c6SGustavo F. Padovan 				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
21500a708f8fSGustavo F. Padovan 			} else
21510c1bc5c6SGustavo F. Padovan 				chan->imtu = val;
21520c1bc5c6SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
21530a708f8fSGustavo F. Padovan 			break;
21540a708f8fSGustavo F. Padovan 
21550a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
21560c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
21570a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
21580c1bc5c6SGustavo F. Padovan 							2, chan->flush_to);
21590a708f8fSGustavo F. Padovan 			break;
21600a708f8fSGustavo F. Padovan 
21610a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
21620a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
21630a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
21640a708f8fSGustavo F. Padovan 
2165b4450035SGustavo F. Padovan 			if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) &&
21660c1bc5c6SGustavo F. Padovan 							rfc.mode != chan->mode)
21670a708f8fSGustavo F. Padovan 				return -ECONNREFUSED;
21680a708f8fSGustavo F. Padovan 
216947d1ec61SGustavo F. Padovan 			chan->fcs = 0;
21700a708f8fSGustavo F. Padovan 
21710a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
21720a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
21730a708f8fSGustavo F. Padovan 			break;
21740a708f8fSGustavo F. Padovan 		}
21750a708f8fSGustavo F. Padovan 	}
21760a708f8fSGustavo F. Padovan 
21770c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
21780a708f8fSGustavo F. Padovan 		return -ECONNREFUSED;
21790a708f8fSGustavo F. Padovan 
21800c1bc5c6SGustavo F. Padovan 	chan->mode = rfc.mode;
21810a708f8fSGustavo F. Padovan 
21820a708f8fSGustavo F. Padovan 	if (*result == L2CAP_CONF_SUCCESS) {
21830a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
21840a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
218547d1ec61SGustavo F. Padovan 			chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
218647d1ec61SGustavo F. Padovan 			chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
218747d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
21880a708f8fSGustavo F. Padovan 			break;
21890a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
219047d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
21910a708f8fSGustavo F. Padovan 		}
21920a708f8fSGustavo F. Padovan 	}
21930a708f8fSGustavo F. Padovan 
2194fe4128e0SGustavo F. Padovan 	req->dcid   = cpu_to_le16(chan->dcid);
21950a708f8fSGustavo F. Padovan 	req->flags  = cpu_to_le16(0x0000);
21960a708f8fSGustavo F. Padovan 
21970a708f8fSGustavo F. Padovan 	return ptr - data;
21980a708f8fSGustavo F. Padovan }
21990a708f8fSGustavo F. Padovan 
2200fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
22010a708f8fSGustavo F. Padovan {
22020a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
22030a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
22040a708f8fSGustavo F. Padovan 
2205fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p", chan);
22060a708f8fSGustavo F. Padovan 
2207fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
22080a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
22090a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(flags);
22100a708f8fSGustavo F. Padovan 
22110a708f8fSGustavo F. Padovan 	return ptr - data;
22120a708f8fSGustavo F. Padovan }
22130a708f8fSGustavo F. Padovan 
22148c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
2215710f9b0aSGustavo F. Padovan {
2216710f9b0aSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
22178c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
2218710f9b0aSGustavo F. Padovan 	u8 buf[128];
2219710f9b0aSGustavo F. Padovan 
2220fe4128e0SGustavo F. Padovan 	rsp.scid   = cpu_to_le16(chan->dcid);
2221fe4128e0SGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(chan->scid);
2222710f9b0aSGustavo F. Padovan 	rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
2223710f9b0aSGustavo F. Padovan 	rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
2224710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, chan->ident,
2225710f9b0aSGustavo F. Padovan 				L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2226710f9b0aSGustavo F. Padovan 
2227b4450035SGustavo F. Padovan 	if (chan->conf_state & L2CAP_CONF_REQ_SENT)
2228710f9b0aSGustavo F. Padovan 		return;
2229710f9b0aSGustavo F. Padovan 
2230b4450035SGustavo F. Padovan 	chan->conf_state |= L2CAP_CONF_REQ_SENT;
2231710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2232710f9b0aSGustavo F. Padovan 			l2cap_build_conf_req(chan, buf), buf);
2233710f9b0aSGustavo F. Padovan 	chan->num_conf_req++;
2234710f9b0aSGustavo F. Padovan }
2235710f9b0aSGustavo F. Padovan 
223647d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
22370a708f8fSGustavo F. Padovan {
22380a708f8fSGustavo F. Padovan 	int type, olen;
22390a708f8fSGustavo F. Padovan 	unsigned long val;
22400a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
22410a708f8fSGustavo F. Padovan 
224247d1ec61SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
22430a708f8fSGustavo F. Padovan 
22440c1bc5c6SGustavo F. Padovan 	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
22450a708f8fSGustavo F. Padovan 		return;
22460a708f8fSGustavo F. Padovan 
22470a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
22480a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
22490a708f8fSGustavo F. Padovan 
22500a708f8fSGustavo F. Padovan 		switch (type) {
22510a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
22520a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
22530a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
22540a708f8fSGustavo F. Padovan 			goto done;
22550a708f8fSGustavo F. Padovan 		}
22560a708f8fSGustavo F. Padovan 	}
22570a708f8fSGustavo F. Padovan 
22580a708f8fSGustavo F. Padovan done:
22590a708f8fSGustavo F. Padovan 	switch (rfc.mode) {
22600a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
226147d1ec61SGustavo F. Padovan 		chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
226247d1ec61SGustavo F. Padovan 		chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
226347d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
22640a708f8fSGustavo F. Padovan 		break;
22650a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
226647d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
22670a708f8fSGustavo F. Padovan 	}
22680a708f8fSGustavo F. Padovan }
22690a708f8fSGustavo F. Padovan 
22700a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
22710a708f8fSGustavo F. Padovan {
22720a708f8fSGustavo F. Padovan 	struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data;
22730a708f8fSGustavo F. Padovan 
22740a708f8fSGustavo F. Padovan 	if (rej->reason != 0x0000)
22750a708f8fSGustavo F. Padovan 		return 0;
22760a708f8fSGustavo F. Padovan 
22770a708f8fSGustavo F. Padovan 	if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
22780a708f8fSGustavo F. Padovan 					cmd->ident == conn->info_ident) {
22790a708f8fSGustavo F. Padovan 		del_timer(&conn->info_timer);
22800a708f8fSGustavo F. Padovan 
22810a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
22820a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
22830a708f8fSGustavo F. Padovan 
22840a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
22850a708f8fSGustavo F. Padovan 	}
22860a708f8fSGustavo F. Padovan 
22870a708f8fSGustavo F. Padovan 	return 0;
22880a708f8fSGustavo F. Padovan }
22890a708f8fSGustavo F. Padovan 
22900a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
22910a708f8fSGustavo F. Padovan {
22920a708f8fSGustavo F. Padovan 	struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
22930a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
229423691d75SGustavo F. Padovan 	struct l2cap_chan *chan = NULL, *pchan;
22950a708f8fSGustavo F. Padovan 	struct sock *parent, *sk = NULL;
22960a708f8fSGustavo F. Padovan 	int result, status = L2CAP_CS_NO_INFO;
22970a708f8fSGustavo F. Padovan 
22980a708f8fSGustavo F. Padovan 	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
22990a708f8fSGustavo F. Padovan 	__le16 psm = req->psm;
23000a708f8fSGustavo F. Padovan 
23010a708f8fSGustavo F. Padovan 	BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
23020a708f8fSGustavo F. Padovan 
23030a708f8fSGustavo F. Padovan 	/* Check if we have socket listening on psm */
230423691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src);
230523691d75SGustavo F. Padovan 	if (!pchan) {
23060a708f8fSGustavo F. Padovan 		result = L2CAP_CR_BAD_PSM;
23070a708f8fSGustavo F. Padovan 		goto sendresp;
23080a708f8fSGustavo F. Padovan 	}
23090a708f8fSGustavo F. Padovan 
231023691d75SGustavo F. Padovan 	parent = pchan->sk;
231123691d75SGustavo F. Padovan 
23120a708f8fSGustavo F. Padovan 	bh_lock_sock(parent);
23130a708f8fSGustavo F. Padovan 
23140a708f8fSGustavo F. Padovan 	/* Check if the ACL is secure enough (if not SDP) */
23150a708f8fSGustavo F. Padovan 	if (psm != cpu_to_le16(0x0001) &&
23160a708f8fSGustavo F. Padovan 				!hci_conn_check_link_mode(conn->hcon)) {
23170a708f8fSGustavo F. Padovan 		conn->disc_reason = 0x05;
23180a708f8fSGustavo F. Padovan 		result = L2CAP_CR_SEC_BLOCK;
23190a708f8fSGustavo F. Padovan 		goto response;
23200a708f8fSGustavo F. Padovan 	}
23210a708f8fSGustavo F. Padovan 
23220a708f8fSGustavo F. Padovan 	result = L2CAP_CR_NO_MEM;
23230a708f8fSGustavo F. Padovan 
23240a708f8fSGustavo F. Padovan 	/* Check for backlog size */
23250a708f8fSGustavo F. Padovan 	if (sk_acceptq_is_full(parent)) {
23260a708f8fSGustavo F. Padovan 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
23270a708f8fSGustavo F. Padovan 		goto response;
23280a708f8fSGustavo F. Padovan 	}
23290a708f8fSGustavo F. Padovan 
233080808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
233180808e43SGustavo F. Padovan 	if (!chan)
23320a708f8fSGustavo F. Padovan 		goto response;
23330a708f8fSGustavo F. Padovan 
233480808e43SGustavo F. Padovan 	sk = chan->sk;
233580808e43SGustavo F. Padovan 
2336baa7e1faSGustavo F. Padovan 	write_lock_bh(&conn->chan_lock);
23370a708f8fSGustavo F. Padovan 
23380a708f8fSGustavo F. Padovan 	/* Check if we already have channel with that dcid */
2339baa7e1faSGustavo F. Padovan 	if (__l2cap_get_chan_by_dcid(conn, scid)) {
2340baa7e1faSGustavo F. Padovan 		write_unlock_bh(&conn->chan_lock);
23410a708f8fSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
23420a708f8fSGustavo F. Padovan 		l2cap_sock_kill(sk);
23430a708f8fSGustavo F. Padovan 		goto response;
23440a708f8fSGustavo F. Padovan 	}
23450a708f8fSGustavo F. Padovan 
23460a708f8fSGustavo F. Padovan 	hci_conn_hold(conn->hcon);
23470a708f8fSGustavo F. Padovan 
23480a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->src, conn->src);
23490a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->dst, conn->dst);
2350fe4128e0SGustavo F. Padovan 	chan->psm  = psm;
2351fe4128e0SGustavo F. Padovan 	chan->dcid = scid;
23520a708f8fSGustavo F. Padovan 
2353d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
2354d1010240SGustavo F. Padovan 
235548454079SGustavo F. Padovan 	__l2cap_chan_add(conn, chan);
235648454079SGustavo F. Padovan 
2357fe4128e0SGustavo F. Padovan 	dcid = chan->scid;
23580a708f8fSGustavo F. Padovan 
2359ab07801dSGustavo F. Padovan 	l2cap_chan_set_timer(chan, sk->sk_sndtimeo);
23600a708f8fSGustavo F. Padovan 
2361fc7f8a7eSGustavo F. Padovan 	chan->ident = cmd->ident;
23620a708f8fSGustavo F. Padovan 
23630a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
23644343478fSGustavo F. Padovan 		if (l2cap_check_security(chan)) {
23650a708f8fSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup) {
23660a708f8fSGustavo F. Padovan 				sk->sk_state = BT_CONNECT2;
23670a708f8fSGustavo F. Padovan 				result = L2CAP_CR_PEND;
23680a708f8fSGustavo F. Padovan 				status = L2CAP_CS_AUTHOR_PEND;
23690a708f8fSGustavo F. Padovan 				parent->sk_data_ready(parent, 0);
23700a708f8fSGustavo F. Padovan 			} else {
23710a708f8fSGustavo F. Padovan 				sk->sk_state = BT_CONFIG;
23720a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SUCCESS;
23730a708f8fSGustavo F. Padovan 				status = L2CAP_CS_NO_INFO;
23740a708f8fSGustavo F. Padovan 			}
23750a708f8fSGustavo F. Padovan 		} else {
23760a708f8fSGustavo F. Padovan 			sk->sk_state = BT_CONNECT2;
23770a708f8fSGustavo F. Padovan 			result = L2CAP_CR_PEND;
23780a708f8fSGustavo F. Padovan 			status = L2CAP_CS_AUTHEN_PEND;
23790a708f8fSGustavo F. Padovan 		}
23800a708f8fSGustavo F. Padovan 	} else {
23810a708f8fSGustavo F. Padovan 		sk->sk_state = BT_CONNECT2;
23820a708f8fSGustavo F. Padovan 		result = L2CAP_CR_PEND;
23830a708f8fSGustavo F. Padovan 		status = L2CAP_CS_NO_INFO;
23840a708f8fSGustavo F. Padovan 	}
23850a708f8fSGustavo F. Padovan 
2386baa7e1faSGustavo F. Padovan 	write_unlock_bh(&conn->chan_lock);
23870a708f8fSGustavo F. Padovan 
23880a708f8fSGustavo F. Padovan response:
23890a708f8fSGustavo F. Padovan 	bh_unlock_sock(parent);
23900a708f8fSGustavo F. Padovan 
23910a708f8fSGustavo F. Padovan sendresp:
23920a708f8fSGustavo F. Padovan 	rsp.scid   = cpu_to_le16(scid);
23930a708f8fSGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(dcid);
23940a708f8fSGustavo F. Padovan 	rsp.result = cpu_to_le16(result);
23950a708f8fSGustavo F. Padovan 	rsp.status = cpu_to_le16(status);
23960a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
23970a708f8fSGustavo F. Padovan 
23980a708f8fSGustavo F. Padovan 	if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
23990a708f8fSGustavo F. Padovan 		struct l2cap_info_req info;
24000a708f8fSGustavo F. Padovan 		info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
24010a708f8fSGustavo F. Padovan 
24020a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
24030a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
24040a708f8fSGustavo F. Padovan 
24050a708f8fSGustavo F. Padovan 		mod_timer(&conn->info_timer, jiffies +
24060a708f8fSGustavo F. Padovan 					msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
24070a708f8fSGustavo F. Padovan 
24080a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
24090a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(info), &info);
24100a708f8fSGustavo F. Padovan 	}
24110a708f8fSGustavo F. Padovan 
2412b4450035SGustavo F. Padovan 	if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) &&
24130a708f8fSGustavo F. Padovan 				result == L2CAP_CR_SUCCESS) {
24140a708f8fSGustavo F. Padovan 		u8 buf[128];
2415b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_REQ_SENT;
24160a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
241773ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
241873ffa904SGustavo F. Padovan 		chan->num_conf_req++;
24190a708f8fSGustavo F. Padovan 	}
24200a708f8fSGustavo F. Padovan 
24210a708f8fSGustavo F. Padovan 	return 0;
24220a708f8fSGustavo F. Padovan }
24230a708f8fSGustavo F. Padovan 
24240a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
24250a708f8fSGustavo F. Padovan {
24260a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
24270a708f8fSGustavo F. Padovan 	u16 scid, dcid, result, status;
242848454079SGustavo F. Padovan 	struct l2cap_chan *chan;
24290a708f8fSGustavo F. Padovan 	struct sock *sk;
24300a708f8fSGustavo F. Padovan 	u8 req[128];
24310a708f8fSGustavo F. Padovan 
24320a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
24330a708f8fSGustavo F. Padovan 	dcid   = __le16_to_cpu(rsp->dcid);
24340a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
24350a708f8fSGustavo F. Padovan 	status = __le16_to_cpu(rsp->status);
24360a708f8fSGustavo F. Padovan 
24370a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
24380a708f8fSGustavo F. Padovan 
24390a708f8fSGustavo F. Padovan 	if (scid) {
2440baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, scid);
244148454079SGustavo F. Padovan 		if (!chan)
24420a708f8fSGustavo F. Padovan 			return -EFAULT;
24430a708f8fSGustavo F. Padovan 	} else {
2444baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_ident(conn, cmd->ident);
244548454079SGustavo F. Padovan 		if (!chan)
24460a708f8fSGustavo F. Padovan 			return -EFAULT;
24470a708f8fSGustavo F. Padovan 	}
24480a708f8fSGustavo F. Padovan 
244948454079SGustavo F. Padovan 	sk = chan->sk;
245048454079SGustavo F. Padovan 
24510a708f8fSGustavo F. Padovan 	switch (result) {
24520a708f8fSGustavo F. Padovan 	case L2CAP_CR_SUCCESS:
24530a708f8fSGustavo F. Padovan 		sk->sk_state = BT_CONFIG;
2454fc7f8a7eSGustavo F. Padovan 		chan->ident = 0;
2455fe4128e0SGustavo F. Padovan 		chan->dcid = dcid;
2456b4450035SGustavo F. Padovan 		chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND;
24570a708f8fSGustavo F. Padovan 
2458b4450035SGustavo F. Padovan 		if (chan->conf_state & L2CAP_CONF_REQ_SENT)
24590a708f8fSGustavo F. Padovan 			break;
24600a708f8fSGustavo F. Padovan 
2461b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_REQ_SENT;
24620a708f8fSGustavo F. Padovan 
24630a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
246473ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, req), req);
246573ffa904SGustavo F. Padovan 		chan->num_conf_req++;
24660a708f8fSGustavo F. Padovan 		break;
24670a708f8fSGustavo F. Padovan 
24680a708f8fSGustavo F. Padovan 	case L2CAP_CR_PEND:
2469b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
24700a708f8fSGustavo F. Padovan 		break;
24710a708f8fSGustavo F. Padovan 
24720a708f8fSGustavo F. Padovan 	default:
24730a708f8fSGustavo F. Padovan 		/* don't delete l2cap channel if sk is owned by user */
24740a708f8fSGustavo F. Padovan 		if (sock_owned_by_user(sk)) {
24750a708f8fSGustavo F. Padovan 			sk->sk_state = BT_DISCONN;
2476ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
2477ab07801dSGustavo F. Padovan 			l2cap_chan_set_timer(chan, HZ / 5);
24780a708f8fSGustavo F. Padovan 			break;
24790a708f8fSGustavo F. Padovan 		}
24800a708f8fSGustavo F. Padovan 
248148454079SGustavo F. Padovan 		l2cap_chan_del(chan, ECONNREFUSED);
24820a708f8fSGustavo F. Padovan 		break;
24830a708f8fSGustavo F. Padovan 	}
24840a708f8fSGustavo F. Padovan 
24850a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
24860a708f8fSGustavo F. Padovan 	return 0;
24870a708f8fSGustavo F. Padovan }
24880a708f8fSGustavo F. Padovan 
248947d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan)
24900a708f8fSGustavo F. Padovan {
249147d1ec61SGustavo F. Padovan 	struct l2cap_pinfo *pi = l2cap_pi(chan->sk);
249247d1ec61SGustavo F. Padovan 
24930a708f8fSGustavo F. Padovan 	/* FCS is enabled only in ERTM or streaming mode, if one or both
24940a708f8fSGustavo F. Padovan 	 * sides request it.
24950a708f8fSGustavo F. Padovan 	 */
24960c1bc5c6SGustavo F. Padovan 	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
249747d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_NONE;
2498b4450035SGustavo F. Padovan 	else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV))
249947d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_CRC16;
25000a708f8fSGustavo F. Padovan }
25010a708f8fSGustavo F. Padovan 
25020a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
25030a708f8fSGustavo F. Padovan {
25040a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
25050a708f8fSGustavo F. Padovan 	u16 dcid, flags;
25060a708f8fSGustavo F. Padovan 	u8 rsp[64];
250748454079SGustavo F. Padovan 	struct l2cap_chan *chan;
25080a708f8fSGustavo F. Padovan 	struct sock *sk;
25090a708f8fSGustavo F. Padovan 	int len;
25100a708f8fSGustavo F. Padovan 
25110a708f8fSGustavo F. Padovan 	dcid  = __le16_to_cpu(req->dcid);
25120a708f8fSGustavo F. Padovan 	flags = __le16_to_cpu(req->flags);
25130a708f8fSGustavo F. Padovan 
25140a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
25150a708f8fSGustavo F. Padovan 
2516baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
251748454079SGustavo F. Padovan 	if (!chan)
25180a708f8fSGustavo F. Padovan 		return -ENOENT;
25190a708f8fSGustavo F. Padovan 
252048454079SGustavo F. Padovan 	sk = chan->sk;
252148454079SGustavo F. Padovan 
25220a708f8fSGustavo F. Padovan 	if (sk->sk_state != BT_CONFIG) {
25230a708f8fSGustavo F. Padovan 		struct l2cap_cmd_rej rej;
25240a708f8fSGustavo F. Padovan 
25250a708f8fSGustavo F. Padovan 		rej.reason = cpu_to_le16(0x0002);
25260a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
25270a708f8fSGustavo F. Padovan 				sizeof(rej), &rej);
25280a708f8fSGustavo F. Padovan 		goto unlock;
25290a708f8fSGustavo F. Padovan 	}
25300a708f8fSGustavo F. Padovan 
25310a708f8fSGustavo F. Padovan 	/* Reject if config buffer is too small. */
25320a708f8fSGustavo F. Padovan 	len = cmd_len - sizeof(*req);
253373ffa904SGustavo F. Padovan 	if (chan->conf_len + len > sizeof(chan->conf_req)) {
25340a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2535fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
25360a708f8fSGustavo F. Padovan 					L2CAP_CONF_REJECT, flags), rsp);
25370a708f8fSGustavo F. Padovan 		goto unlock;
25380a708f8fSGustavo F. Padovan 	}
25390a708f8fSGustavo F. Padovan 
25400a708f8fSGustavo F. Padovan 	/* Store config. */
254173ffa904SGustavo F. Padovan 	memcpy(chan->conf_req + chan->conf_len, req->data, len);
254273ffa904SGustavo F. Padovan 	chan->conf_len += len;
25430a708f8fSGustavo F. Padovan 
25440a708f8fSGustavo F. Padovan 	if (flags & 0x0001) {
25450a708f8fSGustavo F. Padovan 		/* Incomplete config. Send empty response. */
25460a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
2547fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
25480a708f8fSGustavo F. Padovan 					L2CAP_CONF_SUCCESS, 0x0001), rsp);
25490a708f8fSGustavo F. Padovan 		goto unlock;
25500a708f8fSGustavo F. Padovan 	}
25510a708f8fSGustavo F. Padovan 
25520a708f8fSGustavo F. Padovan 	/* Complete config. */
255373ffa904SGustavo F. Padovan 	len = l2cap_parse_conf_req(chan, rsp);
25540a708f8fSGustavo F. Padovan 	if (len < 0) {
2555e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
25560a708f8fSGustavo F. Padovan 		goto unlock;
25570a708f8fSGustavo F. Padovan 	}
25580a708f8fSGustavo F. Padovan 
25590a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
256073ffa904SGustavo F. Padovan 	chan->num_conf_rsp++;
25610a708f8fSGustavo F. Padovan 
25620a708f8fSGustavo F. Padovan 	/* Reset config buffer. */
256373ffa904SGustavo F. Padovan 	chan->conf_len = 0;
25640a708f8fSGustavo F. Padovan 
2565b4450035SGustavo F. Padovan 	if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE))
25660a708f8fSGustavo F. Padovan 		goto unlock;
25670a708f8fSGustavo F. Padovan 
2568b4450035SGustavo F. Padovan 	if (chan->conf_state & L2CAP_CONF_INPUT_DONE) {
256947d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
25700a708f8fSGustavo F. Padovan 
25710a708f8fSGustavo F. Padovan 		sk->sk_state = BT_CONNECTED;
25720a708f8fSGustavo F. Padovan 
257342e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
257442e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
257558d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
25760c1bc5c6SGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_ERTM)
2577525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
25780a708f8fSGustavo F. Padovan 
25790a708f8fSGustavo F. Padovan 		l2cap_chan_ready(sk);
25800a708f8fSGustavo F. Padovan 		goto unlock;
25810a708f8fSGustavo F. Padovan 	}
25820a708f8fSGustavo F. Padovan 
2583b4450035SGustavo F. Padovan 	if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) {
25840a708f8fSGustavo F. Padovan 		u8 buf[64];
2585b4450035SGustavo F. Padovan 		chan->conf_state |= L2CAP_CONF_REQ_SENT;
25860a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
258773ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
258873ffa904SGustavo F. Padovan 		chan->num_conf_req++;
25890a708f8fSGustavo F. Padovan 	}
25900a708f8fSGustavo F. Padovan 
25910a708f8fSGustavo F. Padovan unlock:
25920a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
25930a708f8fSGustavo F. Padovan 	return 0;
25940a708f8fSGustavo F. Padovan }
25950a708f8fSGustavo F. Padovan 
25960a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
25970a708f8fSGustavo F. Padovan {
25980a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
25990a708f8fSGustavo F. Padovan 	u16 scid, flags, result;
260048454079SGustavo F. Padovan 	struct l2cap_chan *chan;
26010a708f8fSGustavo F. Padovan 	struct sock *sk;
26020a708f8fSGustavo F. Padovan 	int len = cmd->len - sizeof(*rsp);
26030a708f8fSGustavo F. Padovan 
26040a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
26050a708f8fSGustavo F. Padovan 	flags  = __le16_to_cpu(rsp->flags);
26060a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
26070a708f8fSGustavo F. Padovan 
26080a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
26090a708f8fSGustavo F. Padovan 			scid, flags, result);
26100a708f8fSGustavo F. Padovan 
2611baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
261248454079SGustavo F. Padovan 	if (!chan)
26130a708f8fSGustavo F. Padovan 		return 0;
26140a708f8fSGustavo F. Padovan 
261548454079SGustavo F. Padovan 	sk = chan->sk;
261648454079SGustavo F. Padovan 
26170a708f8fSGustavo F. Padovan 	switch (result) {
26180a708f8fSGustavo F. Padovan 	case L2CAP_CONF_SUCCESS:
261947d1ec61SGustavo F. Padovan 		l2cap_conf_rfc_get(chan, rsp->data, len);
26200a708f8fSGustavo F. Padovan 		break;
26210a708f8fSGustavo F. Padovan 
26220a708f8fSGustavo F. Padovan 	case L2CAP_CONF_UNACCEPT:
262373ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
26240a708f8fSGustavo F. Padovan 			char req[64];
26250a708f8fSGustavo F. Padovan 
26260a708f8fSGustavo F. Padovan 			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
2627e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
26280a708f8fSGustavo F. Padovan 				goto done;
26290a708f8fSGustavo F. Padovan 			}
26300a708f8fSGustavo F. Padovan 
26310a708f8fSGustavo F. Padovan 			/* throw out any old stored conf requests */
26320a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_SUCCESS;
2633b4450035SGustavo F. Padovan 			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
2634b4450035SGustavo F. Padovan 								req, &result);
26350a708f8fSGustavo F. Padovan 			if (len < 0) {
2636e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
26370a708f8fSGustavo F. Padovan 				goto done;
26380a708f8fSGustavo F. Padovan 			}
26390a708f8fSGustavo F. Padovan 
26400a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn),
26410a708f8fSGustavo F. Padovan 						L2CAP_CONF_REQ, len, req);
264273ffa904SGustavo F. Padovan 			chan->num_conf_req++;
26430a708f8fSGustavo F. Padovan 			if (result != L2CAP_CONF_SUCCESS)
26440a708f8fSGustavo F. Padovan 				goto done;
26450a708f8fSGustavo F. Padovan 			break;
26460a708f8fSGustavo F. Padovan 		}
26470a708f8fSGustavo F. Padovan 
26480a708f8fSGustavo F. Padovan 	default:
26490a708f8fSGustavo F. Padovan 		sk->sk_err = ECONNRESET;
2650ab07801dSGustavo F. Padovan 		l2cap_chan_set_timer(chan, HZ * 5);
2651e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
26520a708f8fSGustavo F. Padovan 		goto done;
26530a708f8fSGustavo F. Padovan 	}
26540a708f8fSGustavo F. Padovan 
26550a708f8fSGustavo F. Padovan 	if (flags & 0x01)
26560a708f8fSGustavo F. Padovan 		goto done;
26570a708f8fSGustavo F. Padovan 
2658b4450035SGustavo F. Padovan 	chan->conf_state |= L2CAP_CONF_INPUT_DONE;
26590a708f8fSGustavo F. Padovan 
2660b4450035SGustavo F. Padovan 	if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) {
266147d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
26620a708f8fSGustavo F. Padovan 
26630a708f8fSGustavo F. Padovan 		sk->sk_state = BT_CONNECTED;
266442e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
266542e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
266658d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
26670c1bc5c6SGustavo F. Padovan 		if (chan->mode ==  L2CAP_MODE_ERTM)
2668525cd185SGustavo F. Padovan 			l2cap_ertm_init(chan);
26690a708f8fSGustavo F. Padovan 
26700a708f8fSGustavo F. Padovan 		l2cap_chan_ready(sk);
26710a708f8fSGustavo F. Padovan 	}
26720a708f8fSGustavo F. Padovan 
26730a708f8fSGustavo F. Padovan done:
26740a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
26750a708f8fSGustavo F. Padovan 	return 0;
26760a708f8fSGustavo F. Padovan }
26770a708f8fSGustavo F. Padovan 
26780a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
26790a708f8fSGustavo F. Padovan {
26800a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
26810a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp rsp;
26820a708f8fSGustavo F. Padovan 	u16 dcid, scid;
268348454079SGustavo F. Padovan 	struct l2cap_chan *chan;
26840a708f8fSGustavo F. Padovan 	struct sock *sk;
26850a708f8fSGustavo F. Padovan 
26860a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(req->scid);
26870a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(req->dcid);
26880a708f8fSGustavo F. Padovan 
26890a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
26900a708f8fSGustavo F. Padovan 
2691baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
269248454079SGustavo F. Padovan 	if (!chan)
26930a708f8fSGustavo F. Padovan 		return 0;
26940a708f8fSGustavo F. Padovan 
269548454079SGustavo F. Padovan 	sk = chan->sk;
269648454079SGustavo F. Padovan 
2697fe4128e0SGustavo F. Padovan 	rsp.dcid = cpu_to_le16(chan->scid);
2698fe4128e0SGustavo F. Padovan 	rsp.scid = cpu_to_le16(chan->dcid);
26990a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
27000a708f8fSGustavo F. Padovan 
27010a708f8fSGustavo F. Padovan 	sk->sk_shutdown = SHUTDOWN_MASK;
27020a708f8fSGustavo F. Padovan 
27030a708f8fSGustavo F. Padovan 	/* don't delete l2cap channel if sk is owned by user */
27040a708f8fSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
27050a708f8fSGustavo F. Padovan 		sk->sk_state = BT_DISCONN;
2706ab07801dSGustavo F. Padovan 		l2cap_chan_clear_timer(chan);
2707ab07801dSGustavo F. Padovan 		l2cap_chan_set_timer(chan, HZ / 5);
27080a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
27090a708f8fSGustavo F. Padovan 		return 0;
27100a708f8fSGustavo F. Padovan 	}
27110a708f8fSGustavo F. Padovan 
271248454079SGustavo F. Padovan 	l2cap_chan_del(chan, ECONNRESET);
27130a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
27140a708f8fSGustavo F. Padovan 
27150a708f8fSGustavo F. Padovan 	l2cap_sock_kill(sk);
27160a708f8fSGustavo F. Padovan 	return 0;
27170a708f8fSGustavo F. Padovan }
27180a708f8fSGustavo F. Padovan 
27190a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
27200a708f8fSGustavo F. Padovan {
27210a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
27220a708f8fSGustavo F. Padovan 	u16 dcid, scid;
272348454079SGustavo F. Padovan 	struct l2cap_chan *chan;
27240a708f8fSGustavo F. Padovan 	struct sock *sk;
27250a708f8fSGustavo F. Padovan 
27260a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(rsp->scid);
27270a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(rsp->dcid);
27280a708f8fSGustavo F. Padovan 
27290a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
27300a708f8fSGustavo F. Padovan 
2731baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
273248454079SGustavo F. Padovan 	if (!chan)
27330a708f8fSGustavo F. Padovan 		return 0;
27340a708f8fSGustavo F. Padovan 
273548454079SGustavo F. Padovan 	sk = chan->sk;
273648454079SGustavo F. Padovan 
27370a708f8fSGustavo F. Padovan 	/* don't delete l2cap channel if sk is owned by user */
27380a708f8fSGustavo F. Padovan 	if (sock_owned_by_user(sk)) {
27390a708f8fSGustavo F. Padovan 		sk->sk_state = BT_DISCONN;
2740ab07801dSGustavo F. Padovan 		l2cap_chan_clear_timer(chan);
2741ab07801dSGustavo F. Padovan 		l2cap_chan_set_timer(chan, HZ / 5);
27420a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
27430a708f8fSGustavo F. Padovan 		return 0;
27440a708f8fSGustavo F. Padovan 	}
27450a708f8fSGustavo F. Padovan 
274648454079SGustavo F. Padovan 	l2cap_chan_del(chan, 0);
27470a708f8fSGustavo F. Padovan 	bh_unlock_sock(sk);
27480a708f8fSGustavo F. Padovan 
27490a708f8fSGustavo F. Padovan 	l2cap_sock_kill(sk);
27500a708f8fSGustavo F. Padovan 	return 0;
27510a708f8fSGustavo F. Padovan }
27520a708f8fSGustavo F. Padovan 
27530a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
27540a708f8fSGustavo F. Padovan {
27550a708f8fSGustavo F. Padovan 	struct l2cap_info_req *req = (struct l2cap_info_req *) data;
27560a708f8fSGustavo F. Padovan 	u16 type;
27570a708f8fSGustavo F. Padovan 
27580a708f8fSGustavo F. Padovan 	type = __le16_to_cpu(req->type);
27590a708f8fSGustavo F. Padovan 
27600a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x", type);
27610a708f8fSGustavo F. Padovan 
27620a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
27630a708f8fSGustavo F. Padovan 		u8 buf[8];
27640a708f8fSGustavo F. Padovan 		u32 feat_mask = l2cap_feat_mask;
27650a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
27660a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FEAT_MASK);
27670a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
27680a708f8fSGustavo F. Padovan 		if (!disable_ertm)
27690a708f8fSGustavo F. Padovan 			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
27700a708f8fSGustavo F. Padovan 							 | L2CAP_FEAT_FCS;
27710a708f8fSGustavo F. Padovan 		put_unaligned_le32(feat_mask, rsp->data);
27720a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
27730a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
27740a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
27750a708f8fSGustavo F. Padovan 		u8 buf[12];
27760a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
27770a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
27780a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
27790a708f8fSGustavo F. Padovan 		memcpy(buf + 4, l2cap_fixed_chan, 8);
27800a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
27810a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
27820a708f8fSGustavo F. Padovan 	} else {
27830a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp rsp;
27840a708f8fSGustavo F. Padovan 		rsp.type   = cpu_to_le16(type);
27850a708f8fSGustavo F. Padovan 		rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
27860a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
27870a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(rsp), &rsp);
27880a708f8fSGustavo F. Padovan 	}
27890a708f8fSGustavo F. Padovan 
27900a708f8fSGustavo F. Padovan 	return 0;
27910a708f8fSGustavo F. Padovan }
27920a708f8fSGustavo F. Padovan 
27930a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
27940a708f8fSGustavo F. Padovan {
27950a708f8fSGustavo F. Padovan 	struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
27960a708f8fSGustavo F. Padovan 	u16 type, result;
27970a708f8fSGustavo F. Padovan 
27980a708f8fSGustavo F. Padovan 	type   = __le16_to_cpu(rsp->type);
27990a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
28000a708f8fSGustavo F. Padovan 
28010a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
28020a708f8fSGustavo F. Padovan 
2803e90165beSAndrei Emeltchenko 	/* L2CAP Info req/rsp are unbound to channels, add extra checks */
2804e90165beSAndrei Emeltchenko 	if (cmd->ident != conn->info_ident ||
2805e90165beSAndrei Emeltchenko 			conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
2806e90165beSAndrei Emeltchenko 		return 0;
2807e90165beSAndrei Emeltchenko 
28080a708f8fSGustavo F. Padovan 	del_timer(&conn->info_timer);
28090a708f8fSGustavo F. Padovan 
28100a708f8fSGustavo F. Padovan 	if (result != L2CAP_IR_SUCCESS) {
28110a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28120a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
28130a708f8fSGustavo F. Padovan 
28140a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
28150a708f8fSGustavo F. Padovan 
28160a708f8fSGustavo F. Padovan 		return 0;
28170a708f8fSGustavo F. Padovan 	}
28180a708f8fSGustavo F. Padovan 
28190a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
28200a708f8fSGustavo F. Padovan 		conn->feat_mask = get_unaligned_le32(rsp->data);
28210a708f8fSGustavo F. Padovan 
28220a708f8fSGustavo F. Padovan 		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
28230a708f8fSGustavo F. Padovan 			struct l2cap_info_req req;
28240a708f8fSGustavo F. Padovan 			req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
28250a708f8fSGustavo F. Padovan 
28260a708f8fSGustavo F. Padovan 			conn->info_ident = l2cap_get_ident(conn);
28270a708f8fSGustavo F. Padovan 
28280a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, conn->info_ident,
28290a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
28300a708f8fSGustavo F. Padovan 		} else {
28310a708f8fSGustavo F. Padovan 			conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28320a708f8fSGustavo F. Padovan 			conn->info_ident = 0;
28330a708f8fSGustavo F. Padovan 
28340a708f8fSGustavo F. Padovan 			l2cap_conn_start(conn);
28350a708f8fSGustavo F. Padovan 		}
28360a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
28370a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28380a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
28390a708f8fSGustavo F. Padovan 
28400a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
28410a708f8fSGustavo F. Padovan 	}
28420a708f8fSGustavo F. Padovan 
28430a708f8fSGustavo F. Padovan 	return 0;
28440a708f8fSGustavo F. Padovan }
28450a708f8fSGustavo F. Padovan 
2846e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
2847de73115aSClaudio Takahasi 							u16 to_multiplier)
2848de73115aSClaudio Takahasi {
2849de73115aSClaudio Takahasi 	u16 max_latency;
2850de73115aSClaudio Takahasi 
2851de73115aSClaudio Takahasi 	if (min > max || min < 6 || max > 3200)
2852de73115aSClaudio Takahasi 		return -EINVAL;
2853de73115aSClaudio Takahasi 
2854de73115aSClaudio Takahasi 	if (to_multiplier < 10 || to_multiplier > 3200)
2855de73115aSClaudio Takahasi 		return -EINVAL;
2856de73115aSClaudio Takahasi 
2857de73115aSClaudio Takahasi 	if (max >= to_multiplier * 8)
2858de73115aSClaudio Takahasi 		return -EINVAL;
2859de73115aSClaudio Takahasi 
2860de73115aSClaudio Takahasi 	max_latency = (to_multiplier * 8 / max) - 1;
2861de73115aSClaudio Takahasi 	if (latency > 499 || latency > max_latency)
2862de73115aSClaudio Takahasi 		return -EINVAL;
2863de73115aSClaudio Takahasi 
2864de73115aSClaudio Takahasi 	return 0;
2865de73115aSClaudio Takahasi }
2866de73115aSClaudio Takahasi 
2867de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
2868de73115aSClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
2869de73115aSClaudio Takahasi {
2870de73115aSClaudio Takahasi 	struct hci_conn *hcon = conn->hcon;
2871de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_req *req;
2872de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_rsp rsp;
2873de73115aSClaudio Takahasi 	u16 min, max, latency, to_multiplier, cmd_len;
28742ce603ebSClaudio Takahasi 	int err;
2875de73115aSClaudio Takahasi 
2876de73115aSClaudio Takahasi 	if (!(hcon->link_mode & HCI_LM_MASTER))
2877de73115aSClaudio Takahasi 		return -EINVAL;
2878de73115aSClaudio Takahasi 
2879de73115aSClaudio Takahasi 	cmd_len = __le16_to_cpu(cmd->len);
2880de73115aSClaudio Takahasi 	if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
2881de73115aSClaudio Takahasi 		return -EPROTO;
2882de73115aSClaudio Takahasi 
2883de73115aSClaudio Takahasi 	req = (struct l2cap_conn_param_update_req *) data;
2884de73115aSClaudio Takahasi 	min		= __le16_to_cpu(req->min);
2885de73115aSClaudio Takahasi 	max		= __le16_to_cpu(req->max);
2886de73115aSClaudio Takahasi 	latency		= __le16_to_cpu(req->latency);
2887de73115aSClaudio Takahasi 	to_multiplier	= __le16_to_cpu(req->to_multiplier);
2888de73115aSClaudio Takahasi 
2889de73115aSClaudio Takahasi 	BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
2890de73115aSClaudio Takahasi 						min, max, latency, to_multiplier);
2891de73115aSClaudio Takahasi 
2892de73115aSClaudio Takahasi 	memset(&rsp, 0, sizeof(rsp));
28932ce603ebSClaudio Takahasi 
28942ce603ebSClaudio Takahasi 	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
28952ce603ebSClaudio Takahasi 	if (err)
2896de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
2897de73115aSClaudio Takahasi 	else
2898de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
2899de73115aSClaudio Takahasi 
2900de73115aSClaudio Takahasi 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
2901de73115aSClaudio Takahasi 							sizeof(rsp), &rsp);
2902de73115aSClaudio Takahasi 
29032ce603ebSClaudio Takahasi 	if (!err)
29042ce603ebSClaudio Takahasi 		hci_le_conn_update(hcon, min, max, latency, to_multiplier);
29052ce603ebSClaudio Takahasi 
2906de73115aSClaudio Takahasi 	return 0;
2907de73115aSClaudio Takahasi }
2908de73115aSClaudio Takahasi 
29093300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
29103300d9a9SClaudio Takahasi 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
29113300d9a9SClaudio Takahasi {
29123300d9a9SClaudio Takahasi 	int err = 0;
29133300d9a9SClaudio Takahasi 
29143300d9a9SClaudio Takahasi 	switch (cmd->code) {
29153300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
29163300d9a9SClaudio Takahasi 		l2cap_command_rej(conn, cmd, data);
29173300d9a9SClaudio Takahasi 		break;
29183300d9a9SClaudio Takahasi 
29193300d9a9SClaudio Takahasi 	case L2CAP_CONN_REQ:
29203300d9a9SClaudio Takahasi 		err = l2cap_connect_req(conn, cmd, data);
29213300d9a9SClaudio Takahasi 		break;
29223300d9a9SClaudio Takahasi 
29233300d9a9SClaudio Takahasi 	case L2CAP_CONN_RSP:
29243300d9a9SClaudio Takahasi 		err = l2cap_connect_rsp(conn, cmd, data);
29253300d9a9SClaudio Takahasi 		break;
29263300d9a9SClaudio Takahasi 
29273300d9a9SClaudio Takahasi 	case L2CAP_CONF_REQ:
29283300d9a9SClaudio Takahasi 		err = l2cap_config_req(conn, cmd, cmd_len, data);
29293300d9a9SClaudio Takahasi 		break;
29303300d9a9SClaudio Takahasi 
29313300d9a9SClaudio Takahasi 	case L2CAP_CONF_RSP:
29323300d9a9SClaudio Takahasi 		err = l2cap_config_rsp(conn, cmd, data);
29333300d9a9SClaudio Takahasi 		break;
29343300d9a9SClaudio Takahasi 
29353300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_REQ:
29363300d9a9SClaudio Takahasi 		err = l2cap_disconnect_req(conn, cmd, data);
29373300d9a9SClaudio Takahasi 		break;
29383300d9a9SClaudio Takahasi 
29393300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_RSP:
29403300d9a9SClaudio Takahasi 		err = l2cap_disconnect_rsp(conn, cmd, data);
29413300d9a9SClaudio Takahasi 		break;
29423300d9a9SClaudio Takahasi 
29433300d9a9SClaudio Takahasi 	case L2CAP_ECHO_REQ:
29443300d9a9SClaudio Takahasi 		l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
29453300d9a9SClaudio Takahasi 		break;
29463300d9a9SClaudio Takahasi 
29473300d9a9SClaudio Takahasi 	case L2CAP_ECHO_RSP:
29483300d9a9SClaudio Takahasi 		break;
29493300d9a9SClaudio Takahasi 
29503300d9a9SClaudio Takahasi 	case L2CAP_INFO_REQ:
29513300d9a9SClaudio Takahasi 		err = l2cap_information_req(conn, cmd, data);
29523300d9a9SClaudio Takahasi 		break;
29533300d9a9SClaudio Takahasi 
29543300d9a9SClaudio Takahasi 	case L2CAP_INFO_RSP:
29553300d9a9SClaudio Takahasi 		err = l2cap_information_rsp(conn, cmd, data);
29563300d9a9SClaudio Takahasi 		break;
29573300d9a9SClaudio Takahasi 
29583300d9a9SClaudio Takahasi 	default:
29593300d9a9SClaudio Takahasi 		BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
29603300d9a9SClaudio Takahasi 		err = -EINVAL;
29613300d9a9SClaudio Takahasi 		break;
29623300d9a9SClaudio Takahasi 	}
29633300d9a9SClaudio Takahasi 
29643300d9a9SClaudio Takahasi 	return err;
29653300d9a9SClaudio Takahasi }
29663300d9a9SClaudio Takahasi 
29673300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
29683300d9a9SClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
29693300d9a9SClaudio Takahasi {
29703300d9a9SClaudio Takahasi 	switch (cmd->code) {
29713300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
29723300d9a9SClaudio Takahasi 		return 0;
29733300d9a9SClaudio Takahasi 
29743300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_REQ:
2975de73115aSClaudio Takahasi 		return l2cap_conn_param_update_req(conn, cmd, data);
29763300d9a9SClaudio Takahasi 
29773300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_RSP:
29783300d9a9SClaudio Takahasi 		return 0;
29793300d9a9SClaudio Takahasi 
29803300d9a9SClaudio Takahasi 	default:
29813300d9a9SClaudio Takahasi 		BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
29823300d9a9SClaudio Takahasi 		return -EINVAL;
29833300d9a9SClaudio Takahasi 	}
29843300d9a9SClaudio Takahasi }
29853300d9a9SClaudio Takahasi 
29863300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn,
29873300d9a9SClaudio Takahasi 							struct sk_buff *skb)
29880a708f8fSGustavo F. Padovan {
29890a708f8fSGustavo F. Padovan 	u8 *data = skb->data;
29900a708f8fSGustavo F. Padovan 	int len = skb->len;
29910a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr cmd;
29923300d9a9SClaudio Takahasi 	int err;
29930a708f8fSGustavo F. Padovan 
29940a708f8fSGustavo F. Padovan 	l2cap_raw_recv(conn, skb);
29950a708f8fSGustavo F. Padovan 
29960a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CMD_HDR_SIZE) {
29970a708f8fSGustavo F. Padovan 		u16 cmd_len;
29980a708f8fSGustavo F. Padovan 		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
29990a708f8fSGustavo F. Padovan 		data += L2CAP_CMD_HDR_SIZE;
30000a708f8fSGustavo F. Padovan 		len  -= L2CAP_CMD_HDR_SIZE;
30010a708f8fSGustavo F. Padovan 
30020a708f8fSGustavo F. Padovan 		cmd_len = le16_to_cpu(cmd.len);
30030a708f8fSGustavo F. Padovan 
30040a708f8fSGustavo F. Padovan 		BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
30050a708f8fSGustavo F. Padovan 
30060a708f8fSGustavo F. Padovan 		if (cmd_len > len || !cmd.ident) {
30070a708f8fSGustavo F. Padovan 			BT_DBG("corrupted command");
30080a708f8fSGustavo F. Padovan 			break;
30090a708f8fSGustavo F. Padovan 		}
30100a708f8fSGustavo F. Padovan 
30113300d9a9SClaudio Takahasi 		if (conn->hcon->type == LE_LINK)
30123300d9a9SClaudio Takahasi 			err = l2cap_le_sig_cmd(conn, &cmd, data);
30133300d9a9SClaudio Takahasi 		else
30143300d9a9SClaudio Takahasi 			err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
30150a708f8fSGustavo F. Padovan 
30160a708f8fSGustavo F. Padovan 		if (err) {
30170a708f8fSGustavo F. Padovan 			struct l2cap_cmd_rej rej;
30182c6d1a2eSGustavo F. Padovan 
30192c6d1a2eSGustavo F. Padovan 			BT_ERR("Wrong link type (%d)", err);
30200a708f8fSGustavo F. Padovan 
30210a708f8fSGustavo F. Padovan 			/* FIXME: Map err to a valid reason */
30220a708f8fSGustavo F. Padovan 			rej.reason = cpu_to_le16(0);
30230a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
30240a708f8fSGustavo F. Padovan 		}
30250a708f8fSGustavo F. Padovan 
30260a708f8fSGustavo F. Padovan 		data += cmd_len;
30270a708f8fSGustavo F. Padovan 		len  -= cmd_len;
30280a708f8fSGustavo F. Padovan 	}
30290a708f8fSGustavo F. Padovan 
30300a708f8fSGustavo F. Padovan 	kfree_skb(skb);
30310a708f8fSGustavo F. Padovan }
30320a708f8fSGustavo F. Padovan 
303347d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
30340a708f8fSGustavo F. Padovan {
30350a708f8fSGustavo F. Padovan 	u16 our_fcs, rcv_fcs;
30360a708f8fSGustavo F. Padovan 	int hdr_size = L2CAP_HDR_SIZE + 2;
30370a708f8fSGustavo F. Padovan 
303847d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
30390a708f8fSGustavo F. Padovan 		skb_trim(skb, skb->len - 2);
30400a708f8fSGustavo F. Padovan 		rcv_fcs = get_unaligned_le16(skb->data + skb->len);
30410a708f8fSGustavo F. Padovan 		our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
30420a708f8fSGustavo F. Padovan 
30430a708f8fSGustavo F. Padovan 		if (our_fcs != rcv_fcs)
30440a708f8fSGustavo F. Padovan 			return -EBADMSG;
30450a708f8fSGustavo F. Padovan 	}
30460a708f8fSGustavo F. Padovan 	return 0;
30470a708f8fSGustavo F. Padovan }
30480a708f8fSGustavo F. Padovan 
3049525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
30500a708f8fSGustavo F. Padovan {
30510a708f8fSGustavo F. Padovan 	u16 control = 0;
30520a708f8fSGustavo F. Padovan 
30536a026610SGustavo F. Padovan 	chan->frames_sent = 0;
30540a708f8fSGustavo F. Padovan 
305542e5c802SGustavo F. Padovan 	control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
30560a708f8fSGustavo F. Padovan 
3057525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
30580a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_NOT_READY;
3059525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
3060525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_RNR_SENT;
30610a708f8fSGustavo F. Padovan 	}
30620a708f8fSGustavo F. Padovan 
3063525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY)
3064525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
30650a708f8fSGustavo F. Padovan 
3066525cd185SGustavo F. Padovan 	l2cap_ertm_send(chan);
30670a708f8fSGustavo F. Padovan 
3068525cd185SGustavo F. Padovan 	if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
30696a026610SGustavo F. Padovan 			chan->frames_sent == 0) {
30700a708f8fSGustavo F. Padovan 		control |= L2CAP_SUPER_RCV_READY;
3071525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
30720a708f8fSGustavo F. Padovan 	}
30730a708f8fSGustavo F. Padovan }
30740a708f8fSGustavo F. Padovan 
307542e5c802SGustavo F. Padovan static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar)
30760a708f8fSGustavo F. Padovan {
30770a708f8fSGustavo F. Padovan 	struct sk_buff *next_skb;
30780a708f8fSGustavo F. Padovan 	int tx_seq_offset, next_tx_seq_offset;
30790a708f8fSGustavo F. Padovan 
30800a708f8fSGustavo F. Padovan 	bt_cb(skb)->tx_seq = tx_seq;
30810a708f8fSGustavo F. Padovan 	bt_cb(skb)->sar = sar;
30820a708f8fSGustavo F. Padovan 
3083f1c6775bSGustavo F. Padovan 	next_skb = skb_peek(&chan->srej_q);
30840a708f8fSGustavo F. Padovan 	if (!next_skb) {
3085f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->srej_q, skb);
30860a708f8fSGustavo F. Padovan 		return 0;
30870a708f8fSGustavo F. Padovan 	}
30880a708f8fSGustavo F. Padovan 
308942e5c802SGustavo F. Padovan 	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
30900a708f8fSGustavo F. Padovan 	if (tx_seq_offset < 0)
30910a708f8fSGustavo F. Padovan 		tx_seq_offset += 64;
30920a708f8fSGustavo F. Padovan 
30930a708f8fSGustavo F. Padovan 	do {
30940a708f8fSGustavo F. Padovan 		if (bt_cb(next_skb)->tx_seq == tx_seq)
30950a708f8fSGustavo F. Padovan 			return -EINVAL;
30960a708f8fSGustavo F. Padovan 
30970a708f8fSGustavo F. Padovan 		next_tx_seq_offset = (bt_cb(next_skb)->tx_seq -
309842e5c802SGustavo F. Padovan 						chan->buffer_seq) % 64;
30990a708f8fSGustavo F. Padovan 		if (next_tx_seq_offset < 0)
31000a708f8fSGustavo F. Padovan 			next_tx_seq_offset += 64;
31010a708f8fSGustavo F. Padovan 
31020a708f8fSGustavo F. Padovan 		if (next_tx_seq_offset > tx_seq_offset) {
3103f1c6775bSGustavo F. Padovan 			__skb_queue_before(&chan->srej_q, next_skb, skb);
31040a708f8fSGustavo F. Padovan 			return 0;
31050a708f8fSGustavo F. Padovan 		}
31060a708f8fSGustavo F. Padovan 
3107f1c6775bSGustavo F. Padovan 		if (skb_queue_is_last(&chan->srej_q, next_skb))
31080a708f8fSGustavo F. Padovan 			break;
31090a708f8fSGustavo F. Padovan 
3110f1c6775bSGustavo F. Padovan 	} while ((next_skb = skb_queue_next(&chan->srej_q, next_skb)));
31110a708f8fSGustavo F. Padovan 
3112f1c6775bSGustavo F. Padovan 	__skb_queue_tail(&chan->srej_q, skb);
31130a708f8fSGustavo F. Padovan 
31140a708f8fSGustavo F. Padovan 	return 0;
31150a708f8fSGustavo F. Padovan }
31160a708f8fSGustavo F. Padovan 
3117525cd185SGustavo F. Padovan static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
31180a708f8fSGustavo F. Padovan {
31190a708f8fSGustavo F. Padovan 	struct sk_buff *_skb;
31200a708f8fSGustavo F. Padovan 	int err;
31210a708f8fSGustavo F. Padovan 
31220a708f8fSGustavo F. Padovan 	switch (control & L2CAP_CTRL_SAR) {
31230a708f8fSGustavo F. Padovan 	case L2CAP_SDU_UNSEGMENTED:
3124525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
31250a708f8fSGustavo F. Padovan 			goto drop;
31260a708f8fSGustavo F. Padovan 
3127224f8af0SRuiyi Zhang 		return sock_queue_rcv_skb(chan->sk, skb);
31280a708f8fSGustavo F. Padovan 
31290a708f8fSGustavo F. Padovan 	case L2CAP_SDU_START:
3130525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU)
31310a708f8fSGustavo F. Padovan 			goto drop;
31320a708f8fSGustavo F. Padovan 
31336f61fd47SGustavo F. Padovan 		chan->sdu_len = get_unaligned_le16(skb->data);
31340a708f8fSGustavo F. Padovan 
31350c1bc5c6SGustavo F. Padovan 		if (chan->sdu_len > chan->imtu)
31360a708f8fSGustavo F. Padovan 			goto disconnect;
31370a708f8fSGustavo F. Padovan 
31386f61fd47SGustavo F. Padovan 		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
31396f61fd47SGustavo F. Padovan 		if (!chan->sdu)
31400a708f8fSGustavo F. Padovan 			return -ENOMEM;
31410a708f8fSGustavo F. Padovan 
31420a708f8fSGustavo F. Padovan 		/* pull sdu_len bytes only after alloc, because of Local Busy
31430a708f8fSGustavo F. Padovan 		 * condition we have to be sure that this will be executed
31440a708f8fSGustavo F. Padovan 		 * only once, i.e., when alloc does not fail */
31450a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
31460a708f8fSGustavo F. Padovan 
31476f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
31480a708f8fSGustavo F. Padovan 
3149525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SAR_SDU;
31506f61fd47SGustavo F. Padovan 		chan->partial_sdu_len = skb->len;
31510a708f8fSGustavo F. Padovan 		break;
31520a708f8fSGustavo F. Padovan 
31530a708f8fSGustavo F. Padovan 	case L2CAP_SDU_CONTINUE:
3154525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
31550a708f8fSGustavo F. Padovan 			goto disconnect;
31560a708f8fSGustavo F. Padovan 
31576f61fd47SGustavo F. Padovan 		if (!chan->sdu)
31580a708f8fSGustavo F. Padovan 			goto disconnect;
31590a708f8fSGustavo F. Padovan 
31606f61fd47SGustavo F. Padovan 		chan->partial_sdu_len += skb->len;
31616f61fd47SGustavo F. Padovan 		if (chan->partial_sdu_len > chan->sdu_len)
31620a708f8fSGustavo F. Padovan 			goto drop;
31630a708f8fSGustavo F. Padovan 
31646f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
31650a708f8fSGustavo F. Padovan 
31660a708f8fSGustavo F. Padovan 		break;
31670a708f8fSGustavo F. Padovan 
31680a708f8fSGustavo F. Padovan 	case L2CAP_SDU_END:
3169525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
31700a708f8fSGustavo F. Padovan 			goto disconnect;
31710a708f8fSGustavo F. Padovan 
31726f61fd47SGustavo F. Padovan 		if (!chan->sdu)
31730a708f8fSGustavo F. Padovan 			goto disconnect;
31740a708f8fSGustavo F. Padovan 
3175525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) {
31766f61fd47SGustavo F. Padovan 			chan->partial_sdu_len += skb->len;
31770a708f8fSGustavo F. Padovan 
31780c1bc5c6SGustavo F. Padovan 			if (chan->partial_sdu_len > chan->imtu)
31790a708f8fSGustavo F. Padovan 				goto drop;
31800a708f8fSGustavo F. Padovan 
31816f61fd47SGustavo F. Padovan 			if (chan->partial_sdu_len != chan->sdu_len)
31820a708f8fSGustavo F. Padovan 				goto drop;
31830a708f8fSGustavo F. Padovan 
31846f61fd47SGustavo F. Padovan 			memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
31850a708f8fSGustavo F. Padovan 		}
31860a708f8fSGustavo F. Padovan 
31876f61fd47SGustavo F. Padovan 		_skb = skb_clone(chan->sdu, GFP_ATOMIC);
31880a708f8fSGustavo F. Padovan 		if (!_skb) {
3189525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
31900a708f8fSGustavo F. Padovan 			return -ENOMEM;
31910a708f8fSGustavo F. Padovan 		}
31920a708f8fSGustavo F. Padovan 
3193525cd185SGustavo F. Padovan 		err = sock_queue_rcv_skb(chan->sk, _skb);
31940a708f8fSGustavo F. Padovan 		if (err < 0) {
31950a708f8fSGustavo F. Padovan 			kfree_skb(_skb);
3196525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SAR_RETRY;
31970a708f8fSGustavo F. Padovan 			return err;
31980a708f8fSGustavo F. Padovan 		}
31990a708f8fSGustavo F. Padovan 
3200525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SAR_RETRY;
3201525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
32020a708f8fSGustavo F. Padovan 
32036f61fd47SGustavo F. Padovan 		kfree_skb(chan->sdu);
32040a708f8fSGustavo F. Padovan 		break;
32050a708f8fSGustavo F. Padovan 	}
32060a708f8fSGustavo F. Padovan 
32070a708f8fSGustavo F. Padovan 	kfree_skb(skb);
32080a708f8fSGustavo F. Padovan 	return 0;
32090a708f8fSGustavo F. Padovan 
32100a708f8fSGustavo F. Padovan drop:
32116f61fd47SGustavo F. Padovan 	kfree_skb(chan->sdu);
32126f61fd47SGustavo F. Padovan 	chan->sdu = NULL;
32130a708f8fSGustavo F. Padovan 
32140a708f8fSGustavo F. Padovan disconnect:
32158c1d787bSGustavo F. Padovan 	l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
32160a708f8fSGustavo F. Padovan 	kfree_skb(skb);
32170a708f8fSGustavo F. Padovan 	return 0;
32180a708f8fSGustavo F. Padovan }
32190a708f8fSGustavo F. Padovan 
3220525cd185SGustavo F. Padovan static int l2cap_try_push_rx_skb(struct l2cap_chan *chan)
32210a708f8fSGustavo F. Padovan {
32220a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
32230a708f8fSGustavo F. Padovan 	u16 control;
32240a708f8fSGustavo F. Padovan 	int err;
32250a708f8fSGustavo F. Padovan 
3226f1c6775bSGustavo F. Padovan 	while ((skb = skb_dequeue(&chan->busy_q))) {
32270a708f8fSGustavo F. Padovan 		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3228525cd185SGustavo F. Padovan 		err = l2cap_ertm_reassembly_sdu(chan, skb, control);
32290a708f8fSGustavo F. Padovan 		if (err < 0) {
3230f1c6775bSGustavo F. Padovan 			skb_queue_head(&chan->busy_q, skb);
32310a708f8fSGustavo F. Padovan 			return -EBUSY;
32320a708f8fSGustavo F. Padovan 		}
32330a708f8fSGustavo F. Padovan 
323442e5c802SGustavo F. Padovan 		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
32350a708f8fSGustavo F. Padovan 	}
32360a708f8fSGustavo F. Padovan 
3237525cd185SGustavo F. Padovan 	if (!(chan->conn_state & L2CAP_CONN_RNR_SENT))
32380a708f8fSGustavo F. Padovan 		goto done;
32390a708f8fSGustavo F. Padovan 
324042e5c802SGustavo F. Padovan 	control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
32410a708f8fSGustavo F. Padovan 	control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL;
3242525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
32436a026610SGustavo F. Padovan 	chan->retry_count = 1;
32440a708f8fSGustavo F. Padovan 
3245e92c8e70SGustavo F. Padovan 	del_timer(&chan->retrans_timer);
32460a708f8fSGustavo F. Padovan 	__mod_monitor_timer();
32470a708f8fSGustavo F. Padovan 
3248525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_WAIT_F;
32490a708f8fSGustavo F. Padovan 
32500a708f8fSGustavo F. Padovan done:
3251525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
3252525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_RNR_SENT;
32530a708f8fSGustavo F. Padovan 
325449208c9cSGustavo F. Padovan 	BT_DBG("chan %p, Exit local busy", chan);
32550a708f8fSGustavo F. Padovan 
32560a708f8fSGustavo F. Padovan 	return 0;
32570a708f8fSGustavo F. Padovan }
32580a708f8fSGustavo F. Padovan 
32590a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work)
32600a708f8fSGustavo F. Padovan {
32610a708f8fSGustavo F. Padovan 	DECLARE_WAITQUEUE(wait, current);
3262311bb895SGustavo F. Padovan 	struct l2cap_chan *chan =
3263311bb895SGustavo F. Padovan 		container_of(work, struct l2cap_chan, busy_work);
3264311bb895SGustavo F. Padovan 	struct sock *sk = chan->sk;
32650a708f8fSGustavo F. Padovan 	int n_tries = 0, timeo = HZ/5, err;
32660a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
32670a708f8fSGustavo F. Padovan 
32680a708f8fSGustavo F. Padovan 	lock_sock(sk);
32690a708f8fSGustavo F. Padovan 
32700a708f8fSGustavo F. Padovan 	add_wait_queue(sk_sleep(sk), &wait);
3271311bb895SGustavo F. Padovan 	while ((skb = skb_peek(&chan->busy_q))) {
32720a708f8fSGustavo F. Padovan 		set_current_state(TASK_INTERRUPTIBLE);
32730a708f8fSGustavo F. Padovan 
32740a708f8fSGustavo F. Padovan 		if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) {
32750a708f8fSGustavo F. Padovan 			err = -EBUSY;
32768c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, EBUSY);
32770a708f8fSGustavo F. Padovan 			break;
32780a708f8fSGustavo F. Padovan 		}
32790a708f8fSGustavo F. Padovan 
32800a708f8fSGustavo F. Padovan 		if (!timeo)
32810a708f8fSGustavo F. Padovan 			timeo = HZ/5;
32820a708f8fSGustavo F. Padovan 
32830a708f8fSGustavo F. Padovan 		if (signal_pending(current)) {
32840a708f8fSGustavo F. Padovan 			err = sock_intr_errno(timeo);
32850a708f8fSGustavo F. Padovan 			break;
32860a708f8fSGustavo F. Padovan 		}
32870a708f8fSGustavo F. Padovan 
32880a708f8fSGustavo F. Padovan 		release_sock(sk);
32890a708f8fSGustavo F. Padovan 		timeo = schedule_timeout(timeo);
32900a708f8fSGustavo F. Padovan 		lock_sock(sk);
32910a708f8fSGustavo F. Padovan 
32920a708f8fSGustavo F. Padovan 		err = sock_error(sk);
32930a708f8fSGustavo F. Padovan 		if (err)
32940a708f8fSGustavo F. Padovan 			break;
32950a708f8fSGustavo F. Padovan 
3296311bb895SGustavo F. Padovan 		if (l2cap_try_push_rx_skb(chan) == 0)
32970a708f8fSGustavo F. Padovan 			break;
32980a708f8fSGustavo F. Padovan 	}
32990a708f8fSGustavo F. Padovan 
33000a708f8fSGustavo F. Padovan 	set_current_state(TASK_RUNNING);
33010a708f8fSGustavo F. Padovan 	remove_wait_queue(sk_sleep(sk), &wait);
33020a708f8fSGustavo F. Padovan 
33030a708f8fSGustavo F. Padovan 	release_sock(sk);
33040a708f8fSGustavo F. Padovan }
33050a708f8fSGustavo F. Padovan 
3306525cd185SGustavo F. Padovan static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
33070a708f8fSGustavo F. Padovan {
33080a708f8fSGustavo F. Padovan 	int sctrl, err;
33090a708f8fSGustavo F. Padovan 
3310525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) {
33110a708f8fSGustavo F. Padovan 		bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3312f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->busy_q, skb);
3313525cd185SGustavo F. Padovan 		return l2cap_try_push_rx_skb(chan);
33140a708f8fSGustavo F. Padovan 
33150a708f8fSGustavo F. Padovan 
33160a708f8fSGustavo F. Padovan 	}
33170a708f8fSGustavo F. Padovan 
3318525cd185SGustavo F. Padovan 	err = l2cap_ertm_reassembly_sdu(chan, skb, control);
33190a708f8fSGustavo F. Padovan 	if (err >= 0) {
332042e5c802SGustavo F. Padovan 		chan->buffer_seq = (chan->buffer_seq + 1) % 64;
33210a708f8fSGustavo F. Padovan 		return err;
33220a708f8fSGustavo F. Padovan 	}
33230a708f8fSGustavo F. Padovan 
33240a708f8fSGustavo F. Padovan 	/* Busy Condition */
3325311bb895SGustavo F. Padovan 	BT_DBG("chan %p, Enter local busy", chan);
33260a708f8fSGustavo F. Padovan 
3327525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_LOCAL_BUSY;
33280a708f8fSGustavo F. Padovan 	bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT;
3329f1c6775bSGustavo F. Padovan 	__skb_queue_tail(&chan->busy_q, skb);
33300a708f8fSGustavo F. Padovan 
333142e5c802SGustavo F. Padovan 	sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT;
33320a708f8fSGustavo F. Padovan 	sctrl |= L2CAP_SUPER_RCV_NOT_READY;
3333525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, sctrl);
33340a708f8fSGustavo F. Padovan 
3335525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_RNR_SENT;
33360a708f8fSGustavo F. Padovan 
3337e92c8e70SGustavo F. Padovan 	del_timer(&chan->ack_timer);
33380a708f8fSGustavo F. Padovan 
3339311bb895SGustavo F. Padovan 	queue_work(_busy_wq, &chan->busy_work);
33400a708f8fSGustavo F. Padovan 
33410a708f8fSGustavo F. Padovan 	return err;
33420a708f8fSGustavo F. Padovan }
33430a708f8fSGustavo F. Padovan 
3344525cd185SGustavo F. Padovan static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control)
33450a708f8fSGustavo F. Padovan {
33460a708f8fSGustavo F. Padovan 	struct sk_buff *_skb;
33470a708f8fSGustavo F. Padovan 	int err = -EINVAL;
33480a708f8fSGustavo F. Padovan 
33490a708f8fSGustavo F. Padovan 	/*
33500a708f8fSGustavo F. Padovan 	 * TODO: We have to notify the userland if some data is lost with the
33510a708f8fSGustavo F. Padovan 	 * Streaming Mode.
33520a708f8fSGustavo F. Padovan 	 */
33530a708f8fSGustavo F. Padovan 
33540a708f8fSGustavo F. Padovan 	switch (control & L2CAP_CTRL_SAR) {
33550a708f8fSGustavo F. Padovan 	case L2CAP_SDU_UNSEGMENTED:
3356525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
33576f61fd47SGustavo F. Padovan 			kfree_skb(chan->sdu);
33580a708f8fSGustavo F. Padovan 			break;
33590a708f8fSGustavo F. Padovan 		}
33600a708f8fSGustavo F. Padovan 
3361525cd185SGustavo F. Padovan 		err = sock_queue_rcv_skb(chan->sk, skb);
33620a708f8fSGustavo F. Padovan 		if (!err)
33630a708f8fSGustavo F. Padovan 			return 0;
33640a708f8fSGustavo F. Padovan 
33650a708f8fSGustavo F. Padovan 		break;
33660a708f8fSGustavo F. Padovan 
33670a708f8fSGustavo F. Padovan 	case L2CAP_SDU_START:
3368525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SAR_SDU) {
33696f61fd47SGustavo F. Padovan 			kfree_skb(chan->sdu);
33700a708f8fSGustavo F. Padovan 			break;
33710a708f8fSGustavo F. Padovan 		}
33720a708f8fSGustavo F. Padovan 
33736f61fd47SGustavo F. Padovan 		chan->sdu_len = get_unaligned_le16(skb->data);
33740a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
33750a708f8fSGustavo F. Padovan 
33760c1bc5c6SGustavo F. Padovan 		if (chan->sdu_len > chan->imtu) {
33770a708f8fSGustavo F. Padovan 			err = -EMSGSIZE;
33780a708f8fSGustavo F. Padovan 			break;
33790a708f8fSGustavo F. Padovan 		}
33800a708f8fSGustavo F. Padovan 
33816f61fd47SGustavo F. Padovan 		chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC);
33826f61fd47SGustavo F. Padovan 		if (!chan->sdu) {
33830a708f8fSGustavo F. Padovan 			err = -ENOMEM;
33840a708f8fSGustavo F. Padovan 			break;
33850a708f8fSGustavo F. Padovan 		}
33860a708f8fSGustavo F. Padovan 
33876f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
33880a708f8fSGustavo F. Padovan 
3389525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SAR_SDU;
33906f61fd47SGustavo F. Padovan 		chan->partial_sdu_len = skb->len;
33910a708f8fSGustavo F. Padovan 		err = 0;
33920a708f8fSGustavo F. Padovan 		break;
33930a708f8fSGustavo F. Padovan 
33940a708f8fSGustavo F. Padovan 	case L2CAP_SDU_CONTINUE:
3395525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
33960a708f8fSGustavo F. Padovan 			break;
33970a708f8fSGustavo F. Padovan 
33986f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
33990a708f8fSGustavo F. Padovan 
34006f61fd47SGustavo F. Padovan 		chan->partial_sdu_len += skb->len;
34016f61fd47SGustavo F. Padovan 		if (chan->partial_sdu_len > chan->sdu_len)
34026f61fd47SGustavo F. Padovan 			kfree_skb(chan->sdu);
34030a708f8fSGustavo F. Padovan 		else
34040a708f8fSGustavo F. Padovan 			err = 0;
34050a708f8fSGustavo F. Padovan 
34060a708f8fSGustavo F. Padovan 		break;
34070a708f8fSGustavo F. Padovan 
34080a708f8fSGustavo F. Padovan 	case L2CAP_SDU_END:
3409525cd185SGustavo F. Padovan 		if (!(chan->conn_state & L2CAP_CONN_SAR_SDU))
34100a708f8fSGustavo F. Padovan 			break;
34110a708f8fSGustavo F. Padovan 
34126f61fd47SGustavo F. Padovan 		memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len);
34130a708f8fSGustavo F. Padovan 
3414525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_SAR_SDU;
34156f61fd47SGustavo F. Padovan 		chan->partial_sdu_len += skb->len;
34160a708f8fSGustavo F. Padovan 
34170c1bc5c6SGustavo F. Padovan 		if (chan->partial_sdu_len > chan->imtu)
34180a708f8fSGustavo F. Padovan 			goto drop;
34190a708f8fSGustavo F. Padovan 
34206f61fd47SGustavo F. Padovan 		if (chan->partial_sdu_len == chan->sdu_len) {
34216f61fd47SGustavo F. Padovan 			_skb = skb_clone(chan->sdu, GFP_ATOMIC);
3422525cd185SGustavo F. Padovan 			err = sock_queue_rcv_skb(chan->sk, _skb);
34230a708f8fSGustavo F. Padovan 			if (err < 0)
34240a708f8fSGustavo F. Padovan 				kfree_skb(_skb);
34250a708f8fSGustavo F. Padovan 		}
34260a708f8fSGustavo F. Padovan 		err = 0;
34270a708f8fSGustavo F. Padovan 
34280a708f8fSGustavo F. Padovan drop:
34296f61fd47SGustavo F. Padovan 		kfree_skb(chan->sdu);
34300a708f8fSGustavo F. Padovan 		break;
34310a708f8fSGustavo F. Padovan 	}
34320a708f8fSGustavo F. Padovan 
34330a708f8fSGustavo F. Padovan 	kfree_skb(skb);
34340a708f8fSGustavo F. Padovan 	return err;
34350a708f8fSGustavo F. Padovan }
34360a708f8fSGustavo F. Padovan 
3437525cd185SGustavo F. Padovan static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq)
34380a708f8fSGustavo F. Padovan {
34390a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
34400a708f8fSGustavo F. Padovan 	u16 control;
34410a708f8fSGustavo F. Padovan 
3442f1c6775bSGustavo F. Padovan 	while ((skb = skb_peek(&chan->srej_q))) {
34430a708f8fSGustavo F. Padovan 		if (bt_cb(skb)->tx_seq != tx_seq)
34440a708f8fSGustavo F. Padovan 			break;
34450a708f8fSGustavo F. Padovan 
3446f1c6775bSGustavo F. Padovan 		skb = skb_dequeue(&chan->srej_q);
34470a708f8fSGustavo F. Padovan 		control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT;
3448525cd185SGustavo F. Padovan 		l2cap_ertm_reassembly_sdu(chan, skb, control);
344942e5c802SGustavo F. Padovan 		chan->buffer_seq_srej =
345042e5c802SGustavo F. Padovan 			(chan->buffer_seq_srej + 1) % 64;
34510a708f8fSGustavo F. Padovan 		tx_seq = (tx_seq + 1) % 64;
34520a708f8fSGustavo F. Padovan 	}
34530a708f8fSGustavo F. Padovan }
34540a708f8fSGustavo F. Padovan 
3455525cd185SGustavo F. Padovan static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq)
34560a708f8fSGustavo F. Padovan {
34570a708f8fSGustavo F. Padovan 	struct srej_list *l, *tmp;
34580a708f8fSGustavo F. Padovan 	u16 control;
34590a708f8fSGustavo F. Padovan 
346039d5a3eeSGustavo F. Padovan 	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
34610a708f8fSGustavo F. Padovan 		if (l->tx_seq == tx_seq) {
34620a708f8fSGustavo F. Padovan 			list_del(&l->list);
34630a708f8fSGustavo F. Padovan 			kfree(l);
34640a708f8fSGustavo F. Padovan 			return;
34650a708f8fSGustavo F. Padovan 		}
34660a708f8fSGustavo F. Padovan 		control = L2CAP_SUPER_SELECT_REJECT;
34670a708f8fSGustavo F. Padovan 		control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3468525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
34690a708f8fSGustavo F. Padovan 		list_del(&l->list);
347039d5a3eeSGustavo F. Padovan 		list_add_tail(&l->list, &chan->srej_l);
34710a708f8fSGustavo F. Padovan 	}
34720a708f8fSGustavo F. Padovan }
34730a708f8fSGustavo F. Padovan 
3474525cd185SGustavo F. Padovan static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq)
34750a708f8fSGustavo F. Padovan {
34760a708f8fSGustavo F. Padovan 	struct srej_list *new;
34770a708f8fSGustavo F. Padovan 	u16 control;
34780a708f8fSGustavo F. Padovan 
347942e5c802SGustavo F. Padovan 	while (tx_seq != chan->expected_tx_seq) {
34800a708f8fSGustavo F. Padovan 		control = L2CAP_SUPER_SELECT_REJECT;
348142e5c802SGustavo F. Padovan 		control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT;
3482525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
34830a708f8fSGustavo F. Padovan 
34840a708f8fSGustavo F. Padovan 		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
348542e5c802SGustavo F. Padovan 		new->tx_seq = chan->expected_tx_seq;
348642e5c802SGustavo F. Padovan 		chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
348739d5a3eeSGustavo F. Padovan 		list_add_tail(&new->list, &chan->srej_l);
34880a708f8fSGustavo F. Padovan 	}
348942e5c802SGustavo F. Padovan 	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
34900a708f8fSGustavo F. Padovan }
34910a708f8fSGustavo F. Padovan 
3492525cd185SGustavo F. Padovan static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
34930a708f8fSGustavo F. Padovan {
34940a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_txseq(rx_control);
34950a708f8fSGustavo F. Padovan 	u8 req_seq = __get_reqseq(rx_control);
34960a708f8fSGustavo F. Padovan 	u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT;
34970a708f8fSGustavo F. Padovan 	int tx_seq_offset, expected_tx_seq_offset;
349847d1ec61SGustavo F. Padovan 	int num_to_ack = (chan->tx_win/6) + 1;
34990a708f8fSGustavo F. Padovan 	int err = 0;
35000a708f8fSGustavo F. Padovan 
3501525cd185SGustavo F. Padovan 	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len,
3502525cd185SGustavo F. Padovan 							tx_seq, rx_control);
35030a708f8fSGustavo F. Padovan 
35040a708f8fSGustavo F. Padovan 	if (L2CAP_CTRL_FINAL & rx_control &&
3505525cd185SGustavo F. Padovan 			chan->conn_state & L2CAP_CONN_WAIT_F) {
3506e92c8e70SGustavo F. Padovan 		del_timer(&chan->monitor_timer);
35076a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
35080a708f8fSGustavo F. Padovan 			__mod_retrans_timer();
3509525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
35100a708f8fSGustavo F. Padovan 	}
35110a708f8fSGustavo F. Padovan 
351242e5c802SGustavo F. Padovan 	chan->expected_ack_seq = req_seq;
351342e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
35140a708f8fSGustavo F. Padovan 
351542e5c802SGustavo F. Padovan 	if (tx_seq == chan->expected_tx_seq)
35160a708f8fSGustavo F. Padovan 		goto expected;
35170a708f8fSGustavo F. Padovan 
351842e5c802SGustavo F. Padovan 	tx_seq_offset = (tx_seq - chan->buffer_seq) % 64;
35190a708f8fSGustavo F. Padovan 	if (tx_seq_offset < 0)
35200a708f8fSGustavo F. Padovan 		tx_seq_offset += 64;
35210a708f8fSGustavo F. Padovan 
35220a708f8fSGustavo F. Padovan 	/* invalid tx_seq */
352347d1ec61SGustavo F. Padovan 	if (tx_seq_offset >= chan->tx_win) {
35248c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
35250a708f8fSGustavo F. Padovan 		goto drop;
35260a708f8fSGustavo F. Padovan 	}
35270a708f8fSGustavo F. Padovan 
3528e6949280SMat Martineau 	if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY)
35290a708f8fSGustavo F. Padovan 		goto drop;
35300a708f8fSGustavo F. Padovan 
3531525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
35320a708f8fSGustavo F. Padovan 		struct srej_list *first;
35330a708f8fSGustavo F. Padovan 
353439d5a3eeSGustavo F. Padovan 		first = list_first_entry(&chan->srej_l,
35350a708f8fSGustavo F. Padovan 				struct srej_list, list);
35360a708f8fSGustavo F. Padovan 		if (tx_seq == first->tx_seq) {
353742e5c802SGustavo F. Padovan 			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
3538525cd185SGustavo F. Padovan 			l2cap_check_srej_gap(chan, tx_seq);
35390a708f8fSGustavo F. Padovan 
35400a708f8fSGustavo F. Padovan 			list_del(&first->list);
35410a708f8fSGustavo F. Padovan 			kfree(first);
35420a708f8fSGustavo F. Padovan 
354339d5a3eeSGustavo F. Padovan 			if (list_empty(&chan->srej_l)) {
354442e5c802SGustavo F. Padovan 				chan->buffer_seq = chan->buffer_seq_srej;
3545525cd185SGustavo F. Padovan 				chan->conn_state &= ~L2CAP_CONN_SREJ_SENT;
3546525cd185SGustavo F. Padovan 				l2cap_send_ack(chan);
354749208c9cSGustavo F. Padovan 				BT_DBG("chan %p, Exit SREJ_SENT", chan);
35480a708f8fSGustavo F. Padovan 			}
35490a708f8fSGustavo F. Padovan 		} else {
35500a708f8fSGustavo F. Padovan 			struct srej_list *l;
35510a708f8fSGustavo F. Padovan 
35520a708f8fSGustavo F. Padovan 			/* duplicated tx_seq */
355342e5c802SGustavo F. Padovan 			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
35540a708f8fSGustavo F. Padovan 				goto drop;
35550a708f8fSGustavo F. Padovan 
355639d5a3eeSGustavo F. Padovan 			list_for_each_entry(l, &chan->srej_l, list) {
35570a708f8fSGustavo F. Padovan 				if (l->tx_seq == tx_seq) {
3558525cd185SGustavo F. Padovan 					l2cap_resend_srejframe(chan, tx_seq);
35590a708f8fSGustavo F. Padovan 					return 0;
35600a708f8fSGustavo F. Padovan 				}
35610a708f8fSGustavo F. Padovan 			}
3562525cd185SGustavo F. Padovan 			l2cap_send_srejframe(chan, tx_seq);
35630a708f8fSGustavo F. Padovan 		}
35640a708f8fSGustavo F. Padovan 	} else {
35650a708f8fSGustavo F. Padovan 		expected_tx_seq_offset =
356642e5c802SGustavo F. Padovan 			(chan->expected_tx_seq - chan->buffer_seq) % 64;
35670a708f8fSGustavo F. Padovan 		if (expected_tx_seq_offset < 0)
35680a708f8fSGustavo F. Padovan 			expected_tx_seq_offset += 64;
35690a708f8fSGustavo F. Padovan 
35700a708f8fSGustavo F. Padovan 		/* duplicated tx_seq */
35710a708f8fSGustavo F. Padovan 		if (tx_seq_offset < expected_tx_seq_offset)
35720a708f8fSGustavo F. Padovan 			goto drop;
35730a708f8fSGustavo F. Padovan 
3574525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SREJ_SENT;
35750a708f8fSGustavo F. Padovan 
357649208c9cSGustavo F. Padovan 		BT_DBG("chan %p, Enter SREJ", chan);
35770a708f8fSGustavo F. Padovan 
357839d5a3eeSGustavo F. Padovan 		INIT_LIST_HEAD(&chan->srej_l);
357942e5c802SGustavo F. Padovan 		chan->buffer_seq_srej = chan->buffer_seq;
35800a708f8fSGustavo F. Padovan 
3581f1c6775bSGustavo F. Padovan 		__skb_queue_head_init(&chan->srej_q);
3582f1c6775bSGustavo F. Padovan 		__skb_queue_head_init(&chan->busy_q);
358342e5c802SGustavo F. Padovan 		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
35840a708f8fSGustavo F. Padovan 
3585525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_PBIT;
35860a708f8fSGustavo F. Padovan 
3587525cd185SGustavo F. Padovan 		l2cap_send_srejframe(chan, tx_seq);
35880a708f8fSGustavo F. Padovan 
3589e92c8e70SGustavo F. Padovan 		del_timer(&chan->ack_timer);
35900a708f8fSGustavo F. Padovan 	}
35910a708f8fSGustavo F. Padovan 	return 0;
35920a708f8fSGustavo F. Padovan 
35930a708f8fSGustavo F. Padovan expected:
359442e5c802SGustavo F. Padovan 	chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
35950a708f8fSGustavo F. Padovan 
3596525cd185SGustavo F. Padovan 	if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
35970a708f8fSGustavo F. Padovan 		bt_cb(skb)->tx_seq = tx_seq;
35980a708f8fSGustavo F. Padovan 		bt_cb(skb)->sar = sar;
3599f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->srej_q, skb);
36000a708f8fSGustavo F. Padovan 		return 0;
36010a708f8fSGustavo F. Padovan 	}
36020a708f8fSGustavo F. Padovan 
3603525cd185SGustavo F. Padovan 	err = l2cap_push_rx_skb(chan, skb, rx_control);
36040a708f8fSGustavo F. Padovan 	if (err < 0)
36050a708f8fSGustavo F. Padovan 		return 0;
36060a708f8fSGustavo F. Padovan 
36070a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_FINAL) {
3608525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
3609525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
36100a708f8fSGustavo F. Padovan 		else
3611525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
36120a708f8fSGustavo F. Padovan 	}
36130a708f8fSGustavo F. Padovan 
36140a708f8fSGustavo F. Padovan 	__mod_ack_timer();
36150a708f8fSGustavo F. Padovan 
36166a026610SGustavo F. Padovan 	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
36176a026610SGustavo F. Padovan 	if (chan->num_acked == num_to_ack - 1)
3618525cd185SGustavo F. Padovan 		l2cap_send_ack(chan);
36190a708f8fSGustavo F. Padovan 
36200a708f8fSGustavo F. Padovan 	return 0;
36210a708f8fSGustavo F. Padovan 
36220a708f8fSGustavo F. Padovan drop:
36230a708f8fSGustavo F. Padovan 	kfree_skb(skb);
36240a708f8fSGustavo F. Padovan 	return 0;
36250a708f8fSGustavo F. Padovan }
36260a708f8fSGustavo F. Padovan 
3627525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control)
36280a708f8fSGustavo F. Padovan {
362949208c9cSGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control),
36300a708f8fSGustavo F. Padovan 						rx_control);
36310a708f8fSGustavo F. Padovan 
363242e5c802SGustavo F. Padovan 	chan->expected_ack_seq = __get_reqseq(rx_control);
363342e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
36340a708f8fSGustavo F. Padovan 
36350a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL) {
3636525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
3637525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SREJ_SENT) {
3638525cd185SGustavo F. Padovan 			if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
36396a026610SGustavo F. Padovan 					(chan->unacked_frames > 0))
36400a708f8fSGustavo F. Padovan 				__mod_retrans_timer();
36410a708f8fSGustavo F. Padovan 
3642525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3643525cd185SGustavo F. Padovan 			l2cap_send_srejtail(chan);
36440a708f8fSGustavo F. Padovan 		} else {
3645525cd185SGustavo F. Padovan 			l2cap_send_i_or_rr_or_rnr(chan);
36460a708f8fSGustavo F. Padovan 		}
36470a708f8fSGustavo F. Padovan 
36480a708f8fSGustavo F. Padovan 	} else if (rx_control & L2CAP_CTRL_FINAL) {
3649525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
36500a708f8fSGustavo F. Padovan 
3651525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
3652525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
36530a708f8fSGustavo F. Padovan 		else
3654525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
36550a708f8fSGustavo F. Padovan 
36560a708f8fSGustavo F. Padovan 	} else {
3657525cd185SGustavo F. Padovan 		if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
36586a026610SGustavo F. Padovan 				(chan->unacked_frames > 0))
36590a708f8fSGustavo F. Padovan 			__mod_retrans_timer();
36600a708f8fSGustavo F. Padovan 
3661525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
3662525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_SREJ_SENT)
3663525cd185SGustavo F. Padovan 			l2cap_send_ack(chan);
36640a708f8fSGustavo F. Padovan 		else
3665525cd185SGustavo F. Padovan 			l2cap_ertm_send(chan);
36660a708f8fSGustavo F. Padovan 	}
36670a708f8fSGustavo F. Padovan }
36680a708f8fSGustavo F. Padovan 
3669525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control)
36700a708f8fSGustavo F. Padovan {
36710a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_reqseq(rx_control);
36720a708f8fSGustavo F. Padovan 
3673525cd185SGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
36740a708f8fSGustavo F. Padovan 
3675525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
36760a708f8fSGustavo F. Padovan 
367742e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
367842e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
36790a708f8fSGustavo F. Padovan 
36800a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_FINAL) {
3681525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_REJ_ACT)
3682525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_REJ_ACT;
36830a708f8fSGustavo F. Padovan 		else
3684525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
36850a708f8fSGustavo F. Padovan 	} else {
3686525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
36870a708f8fSGustavo F. Padovan 
3688525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_WAIT_F)
3689525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_REJ_ACT;
36900a708f8fSGustavo F. Padovan 	}
36910a708f8fSGustavo F. Padovan }
3692525cd185SGustavo F. Padovan static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control)
36930a708f8fSGustavo F. Padovan {
36940a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_reqseq(rx_control);
36950a708f8fSGustavo F. Padovan 
3696525cd185SGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
36970a708f8fSGustavo F. Padovan 
3698525cd185SGustavo F. Padovan 	chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
36990a708f8fSGustavo F. Padovan 
37000a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL) {
370142e5c802SGustavo F. Padovan 		chan->expected_ack_seq = tx_seq;
370242e5c802SGustavo F. Padovan 		l2cap_drop_acked_frames(chan);
37030a708f8fSGustavo F. Padovan 
3704525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
3705525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
37060a708f8fSGustavo F. Padovan 
3707525cd185SGustavo F. Padovan 		l2cap_ertm_send(chan);
37080a708f8fSGustavo F. Padovan 
3709525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
37106a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
3711525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
37120a708f8fSGustavo F. Padovan 		}
37130a708f8fSGustavo F. Padovan 	} else if (rx_control & L2CAP_CTRL_FINAL) {
3714525cd185SGustavo F. Padovan 		if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) &&
37156a026610SGustavo F. Padovan 				chan->srej_save_reqseq == tx_seq)
3716525cd185SGustavo F. Padovan 			chan->conn_state &= ~L2CAP_CONN_SREJ_ACT;
37170a708f8fSGustavo F. Padovan 		else
3718525cd185SGustavo F. Padovan 			l2cap_retransmit_one_frame(chan, tx_seq);
37190a708f8fSGustavo F. Padovan 	} else {
3720525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
3721525cd185SGustavo F. Padovan 		if (chan->conn_state & L2CAP_CONN_WAIT_F) {
37226a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
3723525cd185SGustavo F. Padovan 			chan->conn_state |= L2CAP_CONN_SREJ_ACT;
37240a708f8fSGustavo F. Padovan 		}
37250a708f8fSGustavo F. Padovan 	}
37260a708f8fSGustavo F. Padovan }
37270a708f8fSGustavo F. Padovan 
3728525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control)
37290a708f8fSGustavo F. Padovan {
37300a708f8fSGustavo F. Padovan 	u8 tx_seq = __get_reqseq(rx_control);
37310a708f8fSGustavo F. Padovan 
3732525cd185SGustavo F. Padovan 	BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control);
37330a708f8fSGustavo F. Padovan 
3734525cd185SGustavo F. Padovan 	chan->conn_state |= L2CAP_CONN_REMOTE_BUSY;
373542e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
373642e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
37370a708f8fSGustavo F. Padovan 
37380a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL)
3739525cd185SGustavo F. Padovan 		chan->conn_state |= L2CAP_CONN_SEND_FBIT;
37400a708f8fSGustavo F. Padovan 
3741525cd185SGustavo F. Padovan 	if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) {
3742e92c8e70SGustavo F. Padovan 		del_timer(&chan->retrans_timer);
37430a708f8fSGustavo F. Padovan 		if (rx_control & L2CAP_CTRL_POLL)
3744525cd185SGustavo F. Padovan 			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
37450a708f8fSGustavo F. Padovan 		return;
37460a708f8fSGustavo F. Padovan 	}
37470a708f8fSGustavo F. Padovan 
37480a708f8fSGustavo F. Padovan 	if (rx_control & L2CAP_CTRL_POLL)
3749525cd185SGustavo F. Padovan 		l2cap_send_srejtail(chan);
37500a708f8fSGustavo F. Padovan 	else
3751525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY);
37520a708f8fSGustavo F. Padovan }
37530a708f8fSGustavo F. Padovan 
3754525cd185SGustavo F. Padovan static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb)
37550a708f8fSGustavo F. Padovan {
3756525cd185SGustavo F. Padovan 	BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len);
37570a708f8fSGustavo F. Padovan 
37580a708f8fSGustavo F. Padovan 	if (L2CAP_CTRL_FINAL & rx_control &&
3759525cd185SGustavo F. Padovan 			chan->conn_state & L2CAP_CONN_WAIT_F) {
3760e92c8e70SGustavo F. Padovan 		del_timer(&chan->monitor_timer);
37616a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
37620a708f8fSGustavo F. Padovan 			__mod_retrans_timer();
3763525cd185SGustavo F. Padovan 		chan->conn_state &= ~L2CAP_CONN_WAIT_F;
37640a708f8fSGustavo F. Padovan 	}
37650a708f8fSGustavo F. Padovan 
37660a708f8fSGustavo F. Padovan 	switch (rx_control & L2CAP_CTRL_SUPERVISE) {
37670a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_RCV_READY:
3768525cd185SGustavo F. Padovan 		l2cap_data_channel_rrframe(chan, rx_control);
37690a708f8fSGustavo F. Padovan 		break;
37700a708f8fSGustavo F. Padovan 
37710a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_REJECT:
3772525cd185SGustavo F. Padovan 		l2cap_data_channel_rejframe(chan, rx_control);
37730a708f8fSGustavo F. Padovan 		break;
37740a708f8fSGustavo F. Padovan 
37750a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_SELECT_REJECT:
3776525cd185SGustavo F. Padovan 		l2cap_data_channel_srejframe(chan, rx_control);
37770a708f8fSGustavo F. Padovan 		break;
37780a708f8fSGustavo F. Padovan 
37790a708f8fSGustavo F. Padovan 	case L2CAP_SUPER_RCV_NOT_READY:
3780525cd185SGustavo F. Padovan 		l2cap_data_channel_rnrframe(chan, rx_control);
37810a708f8fSGustavo F. Padovan 		break;
37820a708f8fSGustavo F. Padovan 	}
37830a708f8fSGustavo F. Padovan 
37840a708f8fSGustavo F. Padovan 	kfree_skb(skb);
37850a708f8fSGustavo F. Padovan 	return 0;
37860a708f8fSGustavo F. Padovan }
37870a708f8fSGustavo F. Padovan 
37880a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb)
37890a708f8fSGustavo F. Padovan {
3790525cd185SGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
37910a708f8fSGustavo F. Padovan 	u16 control;
37920a708f8fSGustavo F. Padovan 	u8 req_seq;
37930a708f8fSGustavo F. Padovan 	int len, next_tx_seq_offset, req_seq_offset;
37940a708f8fSGustavo F. Padovan 
37950a708f8fSGustavo F. Padovan 	control = get_unaligned_le16(skb->data);
37960a708f8fSGustavo F. Padovan 	skb_pull(skb, 2);
37970a708f8fSGustavo F. Padovan 	len = skb->len;
37980a708f8fSGustavo F. Padovan 
37990a708f8fSGustavo F. Padovan 	/*
38000a708f8fSGustavo F. Padovan 	 * We can just drop the corrupted I-frame here.
38010a708f8fSGustavo F. Padovan 	 * Receiver will miss it and start proper recovery
38020a708f8fSGustavo F. Padovan 	 * procedures and ask retransmission.
38030a708f8fSGustavo F. Padovan 	 */
380447d1ec61SGustavo F. Padovan 	if (l2cap_check_fcs(chan, skb))
38050a708f8fSGustavo F. Padovan 		goto drop;
38060a708f8fSGustavo F. Padovan 
38070a708f8fSGustavo F. Padovan 	if (__is_sar_start(control) && __is_iframe(control))
38080a708f8fSGustavo F. Padovan 		len -= 2;
38090a708f8fSGustavo F. Padovan 
381047d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
38110a708f8fSGustavo F. Padovan 		len -= 2;
38120a708f8fSGustavo F. Padovan 
381347d1ec61SGustavo F. Padovan 	if (len > chan->mps) {
38148c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38150a708f8fSGustavo F. Padovan 		goto drop;
38160a708f8fSGustavo F. Padovan 	}
38170a708f8fSGustavo F. Padovan 
38180a708f8fSGustavo F. Padovan 	req_seq = __get_reqseq(control);
381942e5c802SGustavo F. Padovan 	req_seq_offset = (req_seq - chan->expected_ack_seq) % 64;
38200a708f8fSGustavo F. Padovan 	if (req_seq_offset < 0)
38210a708f8fSGustavo F. Padovan 		req_seq_offset += 64;
38220a708f8fSGustavo F. Padovan 
38230a708f8fSGustavo F. Padovan 	next_tx_seq_offset =
382442e5c802SGustavo F. Padovan 		(chan->next_tx_seq - chan->expected_ack_seq) % 64;
38250a708f8fSGustavo F. Padovan 	if (next_tx_seq_offset < 0)
38260a708f8fSGustavo F. Padovan 		next_tx_seq_offset += 64;
38270a708f8fSGustavo F. Padovan 
38280a708f8fSGustavo F. Padovan 	/* check for invalid req-seq */
38290a708f8fSGustavo F. Padovan 	if (req_seq_offset > next_tx_seq_offset) {
38308c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38310a708f8fSGustavo F. Padovan 		goto drop;
38320a708f8fSGustavo F. Padovan 	}
38330a708f8fSGustavo F. Padovan 
38340a708f8fSGustavo F. Padovan 	if (__is_iframe(control)) {
38350a708f8fSGustavo F. Padovan 		if (len < 0) {
38368c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38370a708f8fSGustavo F. Padovan 			goto drop;
38380a708f8fSGustavo F. Padovan 		}
38390a708f8fSGustavo F. Padovan 
3840525cd185SGustavo F. Padovan 		l2cap_data_channel_iframe(chan, control, skb);
38410a708f8fSGustavo F. Padovan 	} else {
38420a708f8fSGustavo F. Padovan 		if (len != 0) {
38430a708f8fSGustavo F. Padovan 			BT_ERR("%d", len);
38448c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
38450a708f8fSGustavo F. Padovan 			goto drop;
38460a708f8fSGustavo F. Padovan 		}
38470a708f8fSGustavo F. Padovan 
3848525cd185SGustavo F. Padovan 		l2cap_data_channel_sframe(chan, control, skb);
38490a708f8fSGustavo F. Padovan 	}
38500a708f8fSGustavo F. Padovan 
38510a708f8fSGustavo F. Padovan 	return 0;
38520a708f8fSGustavo F. Padovan 
38530a708f8fSGustavo F. Padovan drop:
38540a708f8fSGustavo F. Padovan 	kfree_skb(skb);
38550a708f8fSGustavo F. Padovan 	return 0;
38560a708f8fSGustavo F. Padovan }
38570a708f8fSGustavo F. Padovan 
38580a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
38590a708f8fSGustavo F. Padovan {
386048454079SGustavo F. Padovan 	struct l2cap_chan *chan;
3861bf734843SDavid S. Miller 	struct sock *sk = NULL;
38620a708f8fSGustavo F. Padovan 	u16 control;
38630a708f8fSGustavo F. Padovan 	u8 tx_seq;
38640a708f8fSGustavo F. Padovan 	int len;
38650a708f8fSGustavo F. Padovan 
3866baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, cid);
386748454079SGustavo F. Padovan 	if (!chan) {
38680a708f8fSGustavo F. Padovan 		BT_DBG("unknown cid 0x%4.4x", cid);
38690a708f8fSGustavo F. Padovan 		goto drop;
38700a708f8fSGustavo F. Padovan 	}
38710a708f8fSGustavo F. Padovan 
387248454079SGustavo F. Padovan 	sk = chan->sk;
38730a708f8fSGustavo F. Padovan 
387449208c9cSGustavo F. Padovan 	BT_DBG("chan %p, len %d", chan, skb->len);
38750a708f8fSGustavo F. Padovan 
38760a708f8fSGustavo F. Padovan 	if (sk->sk_state != BT_CONNECTED)
38770a708f8fSGustavo F. Padovan 		goto drop;
38780a708f8fSGustavo F. Padovan 
38790c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
38800a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
38810a708f8fSGustavo F. Padovan 		/* If socket recv buffers overflows we drop data here
38820a708f8fSGustavo F. Padovan 		 * which is *bad* because L2CAP has to be reliable.
38830a708f8fSGustavo F. Padovan 		 * But we don't have any other choice. L2CAP doesn't
38840a708f8fSGustavo F. Padovan 		 * provide flow control mechanism. */
38850a708f8fSGustavo F. Padovan 
38860c1bc5c6SGustavo F. Padovan 		if (chan->imtu < skb->len)
38870a708f8fSGustavo F. Padovan 			goto drop;
38880a708f8fSGustavo F. Padovan 
38890a708f8fSGustavo F. Padovan 		if (!sock_queue_rcv_skb(sk, skb))
38900a708f8fSGustavo F. Padovan 			goto done;
38910a708f8fSGustavo F. Padovan 		break;
38920a708f8fSGustavo F. Padovan 
38930a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
38940a708f8fSGustavo F. Padovan 		if (!sock_owned_by_user(sk)) {
38950a708f8fSGustavo F. Padovan 			l2cap_ertm_data_rcv(sk, skb);
38960a708f8fSGustavo F. Padovan 		} else {
38970a708f8fSGustavo F. Padovan 			if (sk_add_backlog(sk, skb))
38980a708f8fSGustavo F. Padovan 				goto drop;
38990a708f8fSGustavo F. Padovan 		}
39000a708f8fSGustavo F. Padovan 
39010a708f8fSGustavo F. Padovan 		goto done;
39020a708f8fSGustavo F. Padovan 
39030a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
39040a708f8fSGustavo F. Padovan 		control = get_unaligned_le16(skb->data);
39050a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
39060a708f8fSGustavo F. Padovan 		len = skb->len;
39070a708f8fSGustavo F. Padovan 
390847d1ec61SGustavo F. Padovan 		if (l2cap_check_fcs(chan, skb))
39090a708f8fSGustavo F. Padovan 			goto drop;
39100a708f8fSGustavo F. Padovan 
39110a708f8fSGustavo F. Padovan 		if (__is_sar_start(control))
39120a708f8fSGustavo F. Padovan 			len -= 2;
39130a708f8fSGustavo F. Padovan 
391447d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16)
39150a708f8fSGustavo F. Padovan 			len -= 2;
39160a708f8fSGustavo F. Padovan 
391747d1ec61SGustavo F. Padovan 		if (len > chan->mps || len < 0 || __is_sframe(control))
39180a708f8fSGustavo F. Padovan 			goto drop;
39190a708f8fSGustavo F. Padovan 
39200a708f8fSGustavo F. Padovan 		tx_seq = __get_txseq(control);
39210a708f8fSGustavo F. Padovan 
392242e5c802SGustavo F. Padovan 		if (chan->expected_tx_seq == tx_seq)
392342e5c802SGustavo F. Padovan 			chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64;
39240a708f8fSGustavo F. Padovan 		else
392542e5c802SGustavo F. Padovan 			chan->expected_tx_seq = (tx_seq + 1) % 64;
39260a708f8fSGustavo F. Padovan 
3927525cd185SGustavo F. Padovan 		l2cap_streaming_reassembly_sdu(chan, skb, control);
39280a708f8fSGustavo F. Padovan 
39290a708f8fSGustavo F. Padovan 		goto done;
39300a708f8fSGustavo F. Padovan 
39310a708f8fSGustavo F. Padovan 	default:
39320c1bc5c6SGustavo F. Padovan 		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
39330a708f8fSGustavo F. Padovan 		break;
39340a708f8fSGustavo F. Padovan 	}
39350a708f8fSGustavo F. Padovan 
39360a708f8fSGustavo F. Padovan drop:
39370a708f8fSGustavo F. Padovan 	kfree_skb(skb);
39380a708f8fSGustavo F. Padovan 
39390a708f8fSGustavo F. Padovan done:
39400a708f8fSGustavo F. Padovan 	if (sk)
39410a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
39420a708f8fSGustavo F. Padovan 
39430a708f8fSGustavo F. Padovan 	return 0;
39440a708f8fSGustavo F. Padovan }
39450a708f8fSGustavo F. Padovan 
39460a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
39470a708f8fSGustavo F. Padovan {
39486dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
394923691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
39500a708f8fSGustavo F. Padovan 
395123691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_psm(0, psm, conn->src);
395223691d75SGustavo F. Padovan 	if (!chan)
39530a708f8fSGustavo F. Padovan 		goto drop;
39540a708f8fSGustavo F. Padovan 
395523691d75SGustavo F. Padovan 	sk = chan->sk;
395623691d75SGustavo F. Padovan 
39570a708f8fSGustavo F. Padovan 	bh_lock_sock(sk);
39580a708f8fSGustavo F. Padovan 
39590a708f8fSGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
39600a708f8fSGustavo F. Padovan 
39610a708f8fSGustavo F. Padovan 	if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED)
39620a708f8fSGustavo F. Padovan 		goto drop;
39630a708f8fSGustavo F. Padovan 
39640c1bc5c6SGustavo F. Padovan 	if (l2cap_pi(sk)->chan->imtu < skb->len)
39650a708f8fSGustavo F. Padovan 		goto drop;
39660a708f8fSGustavo F. Padovan 
39670a708f8fSGustavo F. Padovan 	if (!sock_queue_rcv_skb(sk, skb))
39680a708f8fSGustavo F. Padovan 		goto done;
39690a708f8fSGustavo F. Padovan 
39700a708f8fSGustavo F. Padovan drop:
39710a708f8fSGustavo F. Padovan 	kfree_skb(skb);
39720a708f8fSGustavo F. Padovan 
39730a708f8fSGustavo F. Padovan done:
39740a708f8fSGustavo F. Padovan 	if (sk)
39750a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
39760a708f8fSGustavo F. Padovan 	return 0;
39770a708f8fSGustavo F. Padovan }
39780a708f8fSGustavo F. Padovan 
39799f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb)
39809f69bda6SGustavo F. Padovan {
39816dcae1eaSDavid S. Miller 	struct sock *sk = NULL;
398223691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
39839f69bda6SGustavo F. Padovan 
398423691d75SGustavo F. Padovan 	chan = l2cap_global_chan_by_scid(0, cid, conn->src);
398523691d75SGustavo F. Padovan 	if (!chan)
39869f69bda6SGustavo F. Padovan 		goto drop;
39879f69bda6SGustavo F. Padovan 
398823691d75SGustavo F. Padovan 	sk = chan->sk;
398923691d75SGustavo F. Padovan 
39909f69bda6SGustavo F. Padovan 	bh_lock_sock(sk);
39919f69bda6SGustavo F. Padovan 
39929f69bda6SGustavo F. Padovan 	BT_DBG("sk %p, len %d", sk, skb->len);
39939f69bda6SGustavo F. Padovan 
39949f69bda6SGustavo F. Padovan 	if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED)
39959f69bda6SGustavo F. Padovan 		goto drop;
39969f69bda6SGustavo F. Padovan 
39970c1bc5c6SGustavo F. Padovan 	if (l2cap_pi(sk)->chan->imtu < skb->len)
39989f69bda6SGustavo F. Padovan 		goto drop;
39999f69bda6SGustavo F. Padovan 
40009f69bda6SGustavo F. Padovan 	if (!sock_queue_rcv_skb(sk, skb))
40019f69bda6SGustavo F. Padovan 		goto done;
40029f69bda6SGustavo F. Padovan 
40039f69bda6SGustavo F. Padovan drop:
40049f69bda6SGustavo F. Padovan 	kfree_skb(skb);
40059f69bda6SGustavo F. Padovan 
40069f69bda6SGustavo F. Padovan done:
40079f69bda6SGustavo F. Padovan 	if (sk)
40089f69bda6SGustavo F. Padovan 		bh_unlock_sock(sk);
40099f69bda6SGustavo F. Padovan 	return 0;
40109f69bda6SGustavo F. Padovan }
40119f69bda6SGustavo F. Padovan 
40120a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
40130a708f8fSGustavo F. Padovan {
40140a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh = (void *) skb->data;
40150a708f8fSGustavo F. Padovan 	u16 cid, len;
40160a708f8fSGustavo F. Padovan 	__le16 psm;
40170a708f8fSGustavo F. Padovan 
40180a708f8fSGustavo F. Padovan 	skb_pull(skb, L2CAP_HDR_SIZE);
40190a708f8fSGustavo F. Padovan 	cid = __le16_to_cpu(lh->cid);
40200a708f8fSGustavo F. Padovan 	len = __le16_to_cpu(lh->len);
40210a708f8fSGustavo F. Padovan 
40220a708f8fSGustavo F. Padovan 	if (len != skb->len) {
40230a708f8fSGustavo F. Padovan 		kfree_skb(skb);
40240a708f8fSGustavo F. Padovan 		return;
40250a708f8fSGustavo F. Padovan 	}
40260a708f8fSGustavo F. Padovan 
40270a708f8fSGustavo F. Padovan 	BT_DBG("len %d, cid 0x%4.4x", len, cid);
40280a708f8fSGustavo F. Padovan 
40290a708f8fSGustavo F. Padovan 	switch (cid) {
40303300d9a9SClaudio Takahasi 	case L2CAP_CID_LE_SIGNALING:
40310a708f8fSGustavo F. Padovan 	case L2CAP_CID_SIGNALING:
40320a708f8fSGustavo F. Padovan 		l2cap_sig_channel(conn, skb);
40330a708f8fSGustavo F. Padovan 		break;
40340a708f8fSGustavo F. Padovan 
40350a708f8fSGustavo F. Padovan 	case L2CAP_CID_CONN_LESS:
40360a708f8fSGustavo F. Padovan 		psm = get_unaligned_le16(skb->data);
40370a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
40380a708f8fSGustavo F. Padovan 		l2cap_conless_channel(conn, psm, skb);
40390a708f8fSGustavo F. Padovan 		break;
40400a708f8fSGustavo F. Padovan 
40419f69bda6SGustavo F. Padovan 	case L2CAP_CID_LE_DATA:
40429f69bda6SGustavo F. Padovan 		l2cap_att_channel(conn, cid, skb);
40439f69bda6SGustavo F. Padovan 		break;
40449f69bda6SGustavo F. Padovan 
40450a708f8fSGustavo F. Padovan 	default:
40460a708f8fSGustavo F. Padovan 		l2cap_data_channel(conn, cid, skb);
40470a708f8fSGustavo F. Padovan 		break;
40480a708f8fSGustavo F. Padovan 	}
40490a708f8fSGustavo F. Padovan }
40500a708f8fSGustavo F. Padovan 
40510a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */
40520a708f8fSGustavo F. Padovan 
40530a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
40540a708f8fSGustavo F. Padovan {
40550a708f8fSGustavo F. Padovan 	int exact = 0, lm1 = 0, lm2 = 0;
405623691d75SGustavo F. Padovan 	struct l2cap_chan *c;
40570a708f8fSGustavo F. Padovan 
40580a708f8fSGustavo F. Padovan 	if (type != ACL_LINK)
40590a708f8fSGustavo F. Padovan 		return -EINVAL;
40600a708f8fSGustavo F. Padovan 
40610a708f8fSGustavo F. Padovan 	BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
40620a708f8fSGustavo F. Padovan 
40630a708f8fSGustavo F. Padovan 	/* Find listening sockets and check their link_mode */
406423691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
406523691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
406623691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
40674343478fSGustavo F. Padovan 
40680a708f8fSGustavo F. Padovan 		if (sk->sk_state != BT_LISTEN)
40690a708f8fSGustavo F. Padovan 			continue;
40700a708f8fSGustavo F. Padovan 
40710a708f8fSGustavo F. Padovan 		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
40720a708f8fSGustavo F. Padovan 			lm1 |= HCI_LM_ACCEPT;
407323691d75SGustavo F. Padovan 			if (c->role_switch)
40740a708f8fSGustavo F. Padovan 				lm1 |= HCI_LM_MASTER;
40750a708f8fSGustavo F. Padovan 			exact++;
40760a708f8fSGustavo F. Padovan 		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
40770a708f8fSGustavo F. Padovan 			lm2 |= HCI_LM_ACCEPT;
407823691d75SGustavo F. Padovan 			if (c->role_switch)
40790a708f8fSGustavo F. Padovan 				lm2 |= HCI_LM_MASTER;
40800a708f8fSGustavo F. Padovan 		}
40810a708f8fSGustavo F. Padovan 	}
408223691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
40830a708f8fSGustavo F. Padovan 
40840a708f8fSGustavo F. Padovan 	return exact ? lm1 : lm2;
40850a708f8fSGustavo F. Padovan }
40860a708f8fSGustavo F. Padovan 
40870a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
40880a708f8fSGustavo F. Padovan {
40890a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
40900a708f8fSGustavo F. Padovan 
40910a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
40920a708f8fSGustavo F. Padovan 
4093acd7d370SVille Tervo 	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
40940a708f8fSGustavo F. Padovan 		return -EINVAL;
40950a708f8fSGustavo F. Padovan 
40960a708f8fSGustavo F. Padovan 	if (!status) {
40970a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, status);
40980a708f8fSGustavo F. Padovan 		if (conn)
40990a708f8fSGustavo F. Padovan 			l2cap_conn_ready(conn);
41000a708f8fSGustavo F. Padovan 	} else
41010a708f8fSGustavo F. Padovan 		l2cap_conn_del(hcon, bt_err(status));
41020a708f8fSGustavo F. Padovan 
41030a708f8fSGustavo F. Padovan 	return 0;
41040a708f8fSGustavo F. Padovan }
41050a708f8fSGustavo F. Padovan 
41060a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon)
41070a708f8fSGustavo F. Padovan {
41080a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
41090a708f8fSGustavo F. Padovan 
41100a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p", hcon);
41110a708f8fSGustavo F. Padovan 
41120a708f8fSGustavo F. Padovan 	if (hcon->type != ACL_LINK || !conn)
41130a708f8fSGustavo F. Padovan 		return 0x13;
41140a708f8fSGustavo F. Padovan 
41150a708f8fSGustavo F. Padovan 	return conn->disc_reason;
41160a708f8fSGustavo F. Padovan }
41170a708f8fSGustavo F. Padovan 
41180a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
41190a708f8fSGustavo F. Padovan {
41200a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p reason %d", hcon, reason);
41210a708f8fSGustavo F. Padovan 
4122acd7d370SVille Tervo 	if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
41230a708f8fSGustavo F. Padovan 		return -EINVAL;
41240a708f8fSGustavo F. Padovan 
41250a708f8fSGustavo F. Padovan 	l2cap_conn_del(hcon, bt_err(reason));
41260a708f8fSGustavo F. Padovan 
41270a708f8fSGustavo F. Padovan 	return 0;
41280a708f8fSGustavo F. Padovan }
41290a708f8fSGustavo F. Padovan 
41304343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
41310a708f8fSGustavo F. Padovan {
4132715ec005SGustavo F. Padovan 	if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
41330a708f8fSGustavo F. Padovan 		return;
41340a708f8fSGustavo F. Padovan 
41350a708f8fSGustavo F. Padovan 	if (encrypt == 0x00) {
41364343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM) {
4137ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
4138ab07801dSGustavo F. Padovan 			l2cap_chan_set_timer(chan, HZ * 5);
41394343478fSGustavo F. Padovan 		} else if (chan->sec_level == BT_SECURITY_HIGH)
41400f852724SGustavo F. Padovan 			l2cap_chan_close(chan, ECONNREFUSED);
41410a708f8fSGustavo F. Padovan 	} else {
41424343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM)
4143ab07801dSGustavo F. Padovan 			l2cap_chan_clear_timer(chan);
41440a708f8fSGustavo F. Padovan 	}
41450a708f8fSGustavo F. Padovan }
41460a708f8fSGustavo F. Padovan 
41470a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
41480a708f8fSGustavo F. Padovan {
41490a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
415048454079SGustavo F. Padovan 	struct l2cap_chan *chan;
41510a708f8fSGustavo F. Padovan 
41520a708f8fSGustavo F. Padovan 	if (!conn)
41530a708f8fSGustavo F. Padovan 		return 0;
41540a708f8fSGustavo F. Padovan 
41550a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
41560a708f8fSGustavo F. Padovan 
4157baa7e1faSGustavo F. Padovan 	read_lock(&conn->chan_lock);
41580a708f8fSGustavo F. Padovan 
4159baa7e1faSGustavo F. Padovan 	list_for_each_entry(chan, &conn->chan_l, list) {
416048454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
4161baa7e1faSGustavo F. Padovan 
41620a708f8fSGustavo F. Padovan 		bh_lock_sock(sk);
41630a708f8fSGustavo F. Padovan 
4164b4450035SGustavo F. Padovan 		if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) {
41650a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
41660a708f8fSGustavo F. Padovan 			continue;
41670a708f8fSGustavo F. Padovan 		}
41680a708f8fSGustavo F. Padovan 
41690a708f8fSGustavo F. Padovan 		if (!status && (sk->sk_state == BT_CONNECTED ||
41700a708f8fSGustavo F. Padovan 						sk->sk_state == BT_CONFIG)) {
41714343478fSGustavo F. Padovan 			l2cap_check_encryption(chan, encrypt);
41720a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
41730a708f8fSGustavo F. Padovan 			continue;
41740a708f8fSGustavo F. Padovan 		}
41750a708f8fSGustavo F. Padovan 
41760a708f8fSGustavo F. Padovan 		if (sk->sk_state == BT_CONNECT) {
41770a708f8fSGustavo F. Padovan 			if (!status) {
41780a708f8fSGustavo F. Padovan 				struct l2cap_conn_req req;
4179fe4128e0SGustavo F. Padovan 				req.scid = cpu_to_le16(chan->scid);
4180fe4128e0SGustavo F. Padovan 				req.psm  = chan->psm;
41810a708f8fSGustavo F. Padovan 
4182fc7f8a7eSGustavo F. Padovan 				chan->ident = l2cap_get_ident(conn);
4183b4450035SGustavo F. Padovan 				chan->conf_state |= L2CAP_CONF_CONNECT_PEND;
41840a708f8fSGustavo F. Padovan 
4185fc7f8a7eSGustavo F. Padovan 				l2cap_send_cmd(conn, chan->ident,
41860a708f8fSGustavo F. Padovan 					L2CAP_CONN_REQ, sizeof(req), &req);
41870a708f8fSGustavo F. Padovan 			} else {
4188ab07801dSGustavo F. Padovan 				l2cap_chan_clear_timer(chan);
4189ab07801dSGustavo F. Padovan 				l2cap_chan_set_timer(chan, HZ / 10);
41900a708f8fSGustavo F. Padovan 			}
41910a708f8fSGustavo F. Padovan 		} else if (sk->sk_state == BT_CONNECT2) {
41920a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
41930a708f8fSGustavo F. Padovan 			__u16 result;
41940a708f8fSGustavo F. Padovan 
41950a708f8fSGustavo F. Padovan 			if (!status) {
41960a708f8fSGustavo F. Padovan 				sk->sk_state = BT_CONFIG;
41970a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SUCCESS;
41980a708f8fSGustavo F. Padovan 			} else {
41990a708f8fSGustavo F. Padovan 				sk->sk_state = BT_DISCONN;
4200ab07801dSGustavo F. Padovan 				l2cap_chan_set_timer(chan, HZ / 10);
42010a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SEC_BLOCK;
42020a708f8fSGustavo F. Padovan 			}
42030a708f8fSGustavo F. Padovan 
4204fe4128e0SGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4205fe4128e0SGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
42060a708f8fSGustavo F. Padovan 			rsp.result = cpu_to_le16(result);
42070a708f8fSGustavo F. Padovan 			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
4208fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4209fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
42100a708f8fSGustavo F. Padovan 		}
42110a708f8fSGustavo F. Padovan 
42120a708f8fSGustavo F. Padovan 		bh_unlock_sock(sk);
42130a708f8fSGustavo F. Padovan 	}
42140a708f8fSGustavo F. Padovan 
4215baa7e1faSGustavo F. Padovan 	read_unlock(&conn->chan_lock);
42160a708f8fSGustavo F. Padovan 
42170a708f8fSGustavo F. Padovan 	return 0;
42180a708f8fSGustavo F. Padovan }
42190a708f8fSGustavo F. Padovan 
42200a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
42210a708f8fSGustavo F. Padovan {
42220a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
42230a708f8fSGustavo F. Padovan 
42240a708f8fSGustavo F. Padovan 	if (!conn)
42250a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, 0);
42260a708f8fSGustavo F. Padovan 
42270a708f8fSGustavo F. Padovan 	if (!conn)
42280a708f8fSGustavo F. Padovan 		goto drop;
42290a708f8fSGustavo F. Padovan 
42300a708f8fSGustavo F. Padovan 	BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
42310a708f8fSGustavo F. Padovan 
42320a708f8fSGustavo F. Padovan 	if (!(flags & ACL_CONT)) {
42330a708f8fSGustavo F. Padovan 		struct l2cap_hdr *hdr;
423448454079SGustavo F. Padovan 		struct l2cap_chan *chan;
42350a708f8fSGustavo F. Padovan 		u16 cid;
42360a708f8fSGustavo F. Padovan 		int len;
42370a708f8fSGustavo F. Padovan 
42380a708f8fSGustavo F. Padovan 		if (conn->rx_len) {
42390a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected start frame (len %d)", skb->len);
42400a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
42410a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
42420a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
42430a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
42440a708f8fSGustavo F. Padovan 		}
42450a708f8fSGustavo F. Padovan 
42460a708f8fSGustavo F. Padovan 		/* Start fragment always begin with Basic L2CAP header */
42470a708f8fSGustavo F. Padovan 		if (skb->len < L2CAP_HDR_SIZE) {
42480a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too short (len %d)", skb->len);
42490a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
42500a708f8fSGustavo F. Padovan 			goto drop;
42510a708f8fSGustavo F. Padovan 		}
42520a708f8fSGustavo F. Padovan 
42530a708f8fSGustavo F. Padovan 		hdr = (struct l2cap_hdr *) skb->data;
42540a708f8fSGustavo F. Padovan 		len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
42550a708f8fSGustavo F. Padovan 		cid = __le16_to_cpu(hdr->cid);
42560a708f8fSGustavo F. Padovan 
42570a708f8fSGustavo F. Padovan 		if (len == skb->len) {
42580a708f8fSGustavo F. Padovan 			/* Complete frame received */
42590a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, skb);
42600a708f8fSGustavo F. Padovan 			return 0;
42610a708f8fSGustavo F. Padovan 		}
42620a708f8fSGustavo F. Padovan 
42630a708f8fSGustavo F. Padovan 		BT_DBG("Start: total len %d, frag len %d", len, skb->len);
42640a708f8fSGustavo F. Padovan 
42650a708f8fSGustavo F. Padovan 		if (skb->len > len) {
42660a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too long (len %d, expected len %d)",
42670a708f8fSGustavo F. Padovan 				skb->len, len);
42680a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
42690a708f8fSGustavo F. Padovan 			goto drop;
42700a708f8fSGustavo F. Padovan 		}
42710a708f8fSGustavo F. Padovan 
4272baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, cid);
42730a708f8fSGustavo F. Padovan 
427448454079SGustavo F. Padovan 		if (chan && chan->sk) {
427548454079SGustavo F. Padovan 			struct sock *sk = chan->sk;
427648454079SGustavo F. Padovan 
42770c1bc5c6SGustavo F. Padovan 			if (chan->imtu < len - L2CAP_HDR_SIZE) {
427848454079SGustavo F. Padovan 				BT_ERR("Frame exceeding recv MTU (len %d, "
427948454079SGustavo F. Padovan 							"MTU %d)", len,
42800c1bc5c6SGustavo F. Padovan 							chan->imtu);
42810a708f8fSGustavo F. Padovan 				bh_unlock_sock(sk);
42820a708f8fSGustavo F. Padovan 				l2cap_conn_unreliable(conn, ECOMM);
42830a708f8fSGustavo F. Padovan 				goto drop;
42840a708f8fSGustavo F. Padovan 			}
42850a708f8fSGustavo F. Padovan 			bh_unlock_sock(sk);
428648454079SGustavo F. Padovan 		}
42870a708f8fSGustavo F. Padovan 
42880a708f8fSGustavo F. Padovan 		/* Allocate skb for the complete frame (with header) */
42890a708f8fSGustavo F. Padovan 		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
42900a708f8fSGustavo F. Padovan 		if (!conn->rx_skb)
42910a708f8fSGustavo F. Padovan 			goto drop;
42920a708f8fSGustavo F. Padovan 
42930a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
42940a708f8fSGustavo F. Padovan 								skb->len);
42950a708f8fSGustavo F. Padovan 		conn->rx_len = len - skb->len;
42960a708f8fSGustavo F. Padovan 	} else {
42970a708f8fSGustavo F. Padovan 		BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
42980a708f8fSGustavo F. Padovan 
42990a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
43000a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected continuation frame (len %d)", skb->len);
43010a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
43020a708f8fSGustavo F. Padovan 			goto drop;
43030a708f8fSGustavo F. Padovan 		}
43040a708f8fSGustavo F. Padovan 
43050a708f8fSGustavo F. Padovan 		if (skb->len > conn->rx_len) {
43060a708f8fSGustavo F. Padovan 			BT_ERR("Fragment is too long (len %d, expected %d)",
43070a708f8fSGustavo F. Padovan 					skb->len, conn->rx_len);
43080a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
43090a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
43100a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
43110a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
43120a708f8fSGustavo F. Padovan 			goto drop;
43130a708f8fSGustavo F. Padovan 		}
43140a708f8fSGustavo F. Padovan 
43150a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
43160a708f8fSGustavo F. Padovan 								skb->len);
43170a708f8fSGustavo F. Padovan 		conn->rx_len -= skb->len;
43180a708f8fSGustavo F. Padovan 
43190a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
43200a708f8fSGustavo F. Padovan 			/* Complete frame received */
43210a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, conn->rx_skb);
43220a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
43230a708f8fSGustavo F. Padovan 		}
43240a708f8fSGustavo F. Padovan 	}
43250a708f8fSGustavo F. Padovan 
43260a708f8fSGustavo F. Padovan drop:
43270a708f8fSGustavo F. Padovan 	kfree_skb(skb);
43280a708f8fSGustavo F. Padovan 	return 0;
43290a708f8fSGustavo F. Padovan }
43300a708f8fSGustavo F. Padovan 
43310a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p)
43320a708f8fSGustavo F. Padovan {
433323691d75SGustavo F. Padovan 	struct l2cap_chan *c;
43340a708f8fSGustavo F. Padovan 
433523691d75SGustavo F. Padovan 	read_lock_bh(&chan_list_lock);
43360a708f8fSGustavo F. Padovan 
433723691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
433823691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
43390a708f8fSGustavo F. Padovan 
4340903d343eSGustavo F. Padovan 		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
43410a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->src),
43420a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->dst),
434323691d75SGustavo F. Padovan 					sk->sk_state, __le16_to_cpu(c->psm),
434423691d75SGustavo F. Padovan 					c->scid, c->dcid, c->imtu, c->omtu,
434523691d75SGustavo F. Padovan 					c->sec_level, c->mode);
43460a708f8fSGustavo F. Padovan 	}
43470a708f8fSGustavo F. Padovan 
434823691d75SGustavo F. Padovan 	read_unlock_bh(&chan_list_lock);
43490a708f8fSGustavo F. Padovan 
43500a708f8fSGustavo F. Padovan 	return 0;
43510a708f8fSGustavo F. Padovan }
43520a708f8fSGustavo F. Padovan 
43530a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file)
43540a708f8fSGustavo F. Padovan {
43550a708f8fSGustavo F. Padovan 	return single_open(file, l2cap_debugfs_show, inode->i_private);
43560a708f8fSGustavo F. Padovan }
43570a708f8fSGustavo F. Padovan 
43580a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = {
43590a708f8fSGustavo F. Padovan 	.open		= l2cap_debugfs_open,
43600a708f8fSGustavo F. Padovan 	.read		= seq_read,
43610a708f8fSGustavo F. Padovan 	.llseek		= seq_lseek,
43620a708f8fSGustavo F. Padovan 	.release	= single_release,
43630a708f8fSGustavo F. Padovan };
43640a708f8fSGustavo F. Padovan 
43650a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs;
43660a708f8fSGustavo F. Padovan 
43670a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = {
43680a708f8fSGustavo F. Padovan 	.name		= "L2CAP",
43690a708f8fSGustavo F. Padovan 	.id		= HCI_PROTO_L2CAP,
43700a708f8fSGustavo F. Padovan 	.connect_ind	= l2cap_connect_ind,
43710a708f8fSGustavo F. Padovan 	.connect_cfm	= l2cap_connect_cfm,
43720a708f8fSGustavo F. Padovan 	.disconn_ind	= l2cap_disconn_ind,
43730a708f8fSGustavo F. Padovan 	.disconn_cfm	= l2cap_disconn_cfm,
43740a708f8fSGustavo F. Padovan 	.security_cfm	= l2cap_security_cfm,
43750a708f8fSGustavo F. Padovan 	.recv_acldata	= l2cap_recv_acldata
43760a708f8fSGustavo F. Padovan };
43770a708f8fSGustavo F. Padovan 
437864274518SGustavo F. Padovan int __init l2cap_init(void)
43790a708f8fSGustavo F. Padovan {
43800a708f8fSGustavo F. Padovan 	int err;
43810a708f8fSGustavo F. Padovan 
4382bb58f747SGustavo F. Padovan 	err = l2cap_init_sockets();
43830a708f8fSGustavo F. Padovan 	if (err < 0)
43840a708f8fSGustavo F. Padovan 		return err;
43850a708f8fSGustavo F. Padovan 
43860a708f8fSGustavo F. Padovan 	_busy_wq = create_singlethread_workqueue("l2cap");
43870a708f8fSGustavo F. Padovan 	if (!_busy_wq) {
4388bb58f747SGustavo F. Padovan 		err = -ENOMEM;
43890a708f8fSGustavo F. Padovan 		goto error;
43900a708f8fSGustavo F. Padovan 	}
43910a708f8fSGustavo F. Padovan 
43920a708f8fSGustavo F. Padovan 	err = hci_register_proto(&l2cap_hci_proto);
43930a708f8fSGustavo F. Padovan 	if (err < 0) {
43940a708f8fSGustavo F. Padovan 		BT_ERR("L2CAP protocol registration failed");
43950a708f8fSGustavo F. Padovan 		bt_sock_unregister(BTPROTO_L2CAP);
43960a708f8fSGustavo F. Padovan 		goto error;
43970a708f8fSGustavo F. Padovan 	}
43980a708f8fSGustavo F. Padovan 
43990a708f8fSGustavo F. Padovan 	if (bt_debugfs) {
44000a708f8fSGustavo F. Padovan 		l2cap_debugfs = debugfs_create_file("l2cap", 0444,
44010a708f8fSGustavo F. Padovan 					bt_debugfs, NULL, &l2cap_debugfs_fops);
44020a708f8fSGustavo F. Padovan 		if (!l2cap_debugfs)
44030a708f8fSGustavo F. Padovan 			BT_ERR("Failed to create L2CAP debug file");
44040a708f8fSGustavo F. Padovan 	}
44050a708f8fSGustavo F. Padovan 
44060a708f8fSGustavo F. Padovan 	return 0;
44070a708f8fSGustavo F. Padovan 
44080a708f8fSGustavo F. Padovan error:
44090a708f8fSGustavo F. Padovan 	destroy_workqueue(_busy_wq);
4410bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
44110a708f8fSGustavo F. Padovan 	return err;
44120a708f8fSGustavo F. Padovan }
44130a708f8fSGustavo F. Padovan 
441464274518SGustavo F. Padovan void l2cap_exit(void)
44150a708f8fSGustavo F. Padovan {
44160a708f8fSGustavo F. Padovan 	debugfs_remove(l2cap_debugfs);
44170a708f8fSGustavo F. Padovan 
44180a708f8fSGustavo F. Padovan 	flush_workqueue(_busy_wq);
44190a708f8fSGustavo F. Padovan 	destroy_workqueue(_busy_wq);
44200a708f8fSGustavo F. Padovan 
44210a708f8fSGustavo F. Padovan 	if (hci_unregister_proto(&l2cap_hci_proto) < 0)
44220a708f8fSGustavo F. Padovan 		BT_ERR("L2CAP protocol unregistration failed");
44230a708f8fSGustavo F. Padovan 
4424bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
44250a708f8fSGustavo F. Padovan }
44260a708f8fSGustavo F. Padovan 
44270a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644);
44280a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
4429