xref: /openbmc/linux/net/bluetooth/l2cap_core.c (revision c2287681)
10a708f8fSGustavo F. Padovan /*
20a708f8fSGustavo F. Padovan    BlueZ - Bluetooth protocol stack for Linux
30a708f8fSGustavo F. Padovan    Copyright (C) 2000-2001 Qualcomm Incorporated
40a708f8fSGustavo F. Padovan    Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
50a708f8fSGustavo F. Padovan    Copyright (C) 2010 Google Inc.
6590051deSGustavo F. Padovan    Copyright (C) 2011 ProFUSION Embedded Systems
70a708f8fSGustavo F. Padovan 
80a708f8fSGustavo F. Padovan    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
90a708f8fSGustavo F. Padovan 
100a708f8fSGustavo F. Padovan    This program is free software; you can redistribute it and/or modify
110a708f8fSGustavo F. Padovan    it under the terms of the GNU General Public License version 2 as
120a708f8fSGustavo F. Padovan    published by the Free Software Foundation;
130a708f8fSGustavo F. Padovan 
140a708f8fSGustavo F. Padovan    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
150a708f8fSGustavo F. Padovan    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
160a708f8fSGustavo F. Padovan    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
170a708f8fSGustavo F. Padovan    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
180a708f8fSGustavo F. Padovan    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
190a708f8fSGustavo F. Padovan    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
200a708f8fSGustavo F. Padovan    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
210a708f8fSGustavo F. Padovan    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
220a708f8fSGustavo F. Padovan 
230a708f8fSGustavo F. Padovan    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
240a708f8fSGustavo F. Padovan    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
250a708f8fSGustavo F. Padovan    SOFTWARE IS DISCLAIMED.
260a708f8fSGustavo F. Padovan */
270a708f8fSGustavo F. Padovan 
28bb58f747SGustavo F. Padovan /* Bluetooth L2CAP core. */
290a708f8fSGustavo F. Padovan 
300a708f8fSGustavo F. Padovan #include <linux/module.h>
310a708f8fSGustavo F. Padovan 
320a708f8fSGustavo F. Padovan #include <linux/types.h>
330a708f8fSGustavo F. Padovan #include <linux/capability.h>
340a708f8fSGustavo F. Padovan #include <linux/errno.h>
350a708f8fSGustavo F. Padovan #include <linux/kernel.h>
360a708f8fSGustavo F. Padovan #include <linux/sched.h>
370a708f8fSGustavo F. Padovan #include <linux/slab.h>
380a708f8fSGustavo F. Padovan #include <linux/poll.h>
390a708f8fSGustavo F. Padovan #include <linux/fcntl.h>
400a708f8fSGustavo F. Padovan #include <linux/init.h>
410a708f8fSGustavo F. Padovan #include <linux/interrupt.h>
420a708f8fSGustavo F. Padovan #include <linux/socket.h>
430a708f8fSGustavo F. Padovan #include <linux/skbuff.h>
440a708f8fSGustavo F. Padovan #include <linux/list.h>
450a708f8fSGustavo F. Padovan #include <linux/device.h>
460a708f8fSGustavo F. Padovan #include <linux/debugfs.h>
470a708f8fSGustavo F. Padovan #include <linux/seq_file.h>
480a708f8fSGustavo F. Padovan #include <linux/uaccess.h>
490a708f8fSGustavo F. Padovan #include <linux/crc16.h>
500a708f8fSGustavo F. Padovan #include <net/sock.h>
510a708f8fSGustavo F. Padovan 
520a708f8fSGustavo F. Padovan #include <asm/unaligned.h>
530a708f8fSGustavo F. Padovan 
540a708f8fSGustavo F. Padovan #include <net/bluetooth/bluetooth.h>
550a708f8fSGustavo F. Padovan #include <net/bluetooth/hci_core.h>
560a708f8fSGustavo F. Padovan #include <net/bluetooth/l2cap.h>
57b501d6a1SAnderson Briglia #include <net/bluetooth/smp.h>
580a708f8fSGustavo F. Padovan 
59eb939922SRusty Russell bool disable_ertm;
600a708f8fSGustavo F. Padovan 
610a708f8fSGustavo F. Padovan static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN;
6250a147cdSMat Martineau static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, };
630a708f8fSGustavo F. Padovan 
64b5ad8b7fSJohannes Berg static LIST_HEAD(chan_list);
65b5ad8b7fSJohannes Berg static DEFINE_RWLOCK(chan_list_lock);
660a708f8fSGustavo F. Padovan 
670a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
680a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data);
694519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
704519de9aSGustavo F. Padovan 								void *data);
71710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
724519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn,
734519de9aSGustavo F. Padovan 				struct l2cap_chan *chan, int err);
740a708f8fSGustavo F. Padovan 
750a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */
7671ba0e56SGustavo F. Padovan 
77baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid)
780a708f8fSGustavo F. Padovan {
793df91ea2SAndrei Emeltchenko 	struct l2cap_chan *c;
80baa7e1faSGustavo F. Padovan 
813df91ea2SAndrei Emeltchenko 	list_for_each_entry(c, &conn->chan_l, list) {
823df91ea2SAndrei Emeltchenko 		if (c->dcid == cid)
833df91ea2SAndrei Emeltchenko 			return c;
840a708f8fSGustavo F. Padovan 	}
853df91ea2SAndrei Emeltchenko 	return NULL;
86baa7e1faSGustavo F. Padovan }
870a708f8fSGustavo F. Padovan 
88baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
890a708f8fSGustavo F. Padovan {
903df91ea2SAndrei Emeltchenko 	struct l2cap_chan *c;
91baa7e1faSGustavo F. Padovan 
923df91ea2SAndrei Emeltchenko 	list_for_each_entry(c, &conn->chan_l, list) {
933df91ea2SAndrei Emeltchenko 		if (c->scid == cid)
943df91ea2SAndrei Emeltchenko 			return c;
950a708f8fSGustavo F. Padovan 	}
963df91ea2SAndrei Emeltchenko 	return NULL;
97baa7e1faSGustavo F. Padovan }
980a708f8fSGustavo F. Padovan 
990a708f8fSGustavo F. Padovan /* Find channel with given SCID.
1000a708f8fSGustavo F. Padovan  * Returns locked socket */
101baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid)
1020a708f8fSGustavo F. Padovan {
10348454079SGustavo F. Padovan 	struct l2cap_chan *c;
104baa7e1faSGustavo F. Padovan 
1053df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
106baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_scid(conn, cid);
1073df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
1083df91ea2SAndrei Emeltchenko 
10948454079SGustavo F. Padovan 	return c;
1100a708f8fSGustavo F. Padovan }
1110a708f8fSGustavo F. Padovan 
112baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1130a708f8fSGustavo F. Padovan {
1143df91ea2SAndrei Emeltchenko 	struct l2cap_chan *c;
115baa7e1faSGustavo F. Padovan 
1163df91ea2SAndrei Emeltchenko 	list_for_each_entry(c, &conn->chan_l, list) {
1173df91ea2SAndrei Emeltchenko 		if (c->ident == ident)
1183df91ea2SAndrei Emeltchenko 			return c;
1190a708f8fSGustavo F. Padovan 	}
1203df91ea2SAndrei Emeltchenko 	return NULL;
121baa7e1faSGustavo F. Padovan }
1220a708f8fSGustavo F. Padovan 
123baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident)
1240a708f8fSGustavo F. Padovan {
12548454079SGustavo F. Padovan 	struct l2cap_chan *c;
126baa7e1faSGustavo F. Padovan 
1273df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
128baa7e1faSGustavo F. Padovan 	c = __l2cap_get_chan_by_ident(conn, ident);
1293df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
1303df91ea2SAndrei Emeltchenko 
13148454079SGustavo F. Padovan 	return c;
1320a708f8fSGustavo F. Padovan }
1330a708f8fSGustavo F. Padovan 
13423691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
1359e4425ffSGustavo F. Padovan {
13623691d75SGustavo F. Padovan 	struct l2cap_chan *c;
1379e4425ffSGustavo F. Padovan 
13823691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
13923691d75SGustavo F. Padovan 		if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src))
14023691d75SGustavo F. Padovan 			return c;
1419e4425ffSGustavo F. Padovan 	}
142250938cbSSzymon Janc 	return NULL;
143250938cbSSzymon Janc }
1449e4425ffSGustavo F. Padovan 
1459e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
1469e4425ffSGustavo F. Padovan {
14773b2ec18SGustavo F. Padovan 	int err;
14873b2ec18SGustavo F. Padovan 
149333055f2SGustavo F. Padovan 	write_lock(&chan_list_lock);
1509e4425ffSGustavo F. Padovan 
15123691d75SGustavo F. Padovan 	if (psm && __l2cap_global_chan_by_addr(psm, src)) {
15273b2ec18SGustavo F. Padovan 		err = -EADDRINUSE;
15373b2ec18SGustavo F. Padovan 		goto done;
1549e4425ffSGustavo F. Padovan 	}
1559e4425ffSGustavo F. Padovan 
15673b2ec18SGustavo F. Padovan 	if (psm) {
1579e4425ffSGustavo F. Padovan 		chan->psm = psm;
1589e4425ffSGustavo F. Padovan 		chan->sport = psm;
15973b2ec18SGustavo F. Padovan 		err = 0;
16073b2ec18SGustavo F. Padovan 	} else {
16173b2ec18SGustavo F. Padovan 		u16 p;
1629e4425ffSGustavo F. Padovan 
16373b2ec18SGustavo F. Padovan 		err = -EINVAL;
16473b2ec18SGustavo F. Padovan 		for (p = 0x1001; p < 0x1100; p += 2)
16523691d75SGustavo F. Padovan 			if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
16673b2ec18SGustavo F. Padovan 				chan->psm   = cpu_to_le16(p);
16773b2ec18SGustavo F. Padovan 				chan->sport = cpu_to_le16(p);
16873b2ec18SGustavo F. Padovan 				err = 0;
16973b2ec18SGustavo F. Padovan 				break;
17073b2ec18SGustavo F. Padovan 			}
17173b2ec18SGustavo F. Padovan 	}
17273b2ec18SGustavo F. Padovan 
17373b2ec18SGustavo F. Padovan done:
174333055f2SGustavo F. Padovan 	write_unlock(&chan_list_lock);
17573b2ec18SGustavo F. Padovan 	return err;
1769e4425ffSGustavo F. Padovan }
1779e4425ffSGustavo F. Padovan 
1789e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan,  __u16 scid)
1799e4425ffSGustavo F. Padovan {
180333055f2SGustavo F. Padovan 	write_lock(&chan_list_lock);
1819e4425ffSGustavo F. Padovan 
1829e4425ffSGustavo F. Padovan 	chan->scid = scid;
1839e4425ffSGustavo F. Padovan 
184333055f2SGustavo F. Padovan 	write_unlock(&chan_list_lock);
1859e4425ffSGustavo F. Padovan 
1869e4425ffSGustavo F. Padovan 	return 0;
1879e4425ffSGustavo F. Padovan }
1889e4425ffSGustavo F. Padovan 
189baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
1900a708f8fSGustavo F. Padovan {
1910a708f8fSGustavo F. Padovan 	u16 cid = L2CAP_CID_DYN_START;
1920a708f8fSGustavo F. Padovan 
1930a708f8fSGustavo F. Padovan 	for (; cid < L2CAP_CID_DYN_END; cid++) {
194baa7e1faSGustavo F. Padovan 		if (!__l2cap_get_chan_by_scid(conn, cid))
1950a708f8fSGustavo F. Padovan 			return cid;
1960a708f8fSGustavo F. Padovan 	}
1970a708f8fSGustavo F. Padovan 
1980a708f8fSGustavo F. Padovan 	return 0;
1990a708f8fSGustavo F. Padovan }
2000a708f8fSGustavo F. Padovan 
2010e587be7SAndrei Emeltchenko static void __l2cap_state_change(struct l2cap_chan *chan, int state)
20289bc500eSGustavo F. Padovan {
20342d2d87cSAndrei Emeltchenko 	BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state),
204badaaa00SGustavo F. Padovan 						state_to_string(state));
205badaaa00SGustavo F. Padovan 
20689bc500eSGustavo F. Padovan 	chan->state = state;
20789bc500eSGustavo F. Padovan 	chan->ops->state_change(chan->data, state);
20889bc500eSGustavo F. Padovan }
20989bc500eSGustavo F. Padovan 
2100e587be7SAndrei Emeltchenko static void l2cap_state_change(struct l2cap_chan *chan, int state)
2110e587be7SAndrei Emeltchenko {
2120e587be7SAndrei Emeltchenko 	struct sock *sk = chan->sk;
2130e587be7SAndrei Emeltchenko 
2140e587be7SAndrei Emeltchenko 	lock_sock(sk);
2150e587be7SAndrei Emeltchenko 	__l2cap_state_change(chan, state);
2160e587be7SAndrei Emeltchenko 	release_sock(sk);
2170e587be7SAndrei Emeltchenko }
2180e587be7SAndrei Emeltchenko 
2192e0052e4SAndrei Emeltchenko static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err)
2202e0052e4SAndrei Emeltchenko {
2212e0052e4SAndrei Emeltchenko 	struct sock *sk = chan->sk;
2222e0052e4SAndrei Emeltchenko 
2232e0052e4SAndrei Emeltchenko 	sk->sk_err = err;
2242e0052e4SAndrei Emeltchenko }
2252e0052e4SAndrei Emeltchenko 
2262e0052e4SAndrei Emeltchenko static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err)
2272e0052e4SAndrei Emeltchenko {
2282e0052e4SAndrei Emeltchenko 	struct sock *sk = chan->sk;
2292e0052e4SAndrei Emeltchenko 
2302e0052e4SAndrei Emeltchenko 	lock_sock(sk);
2312e0052e4SAndrei Emeltchenko 	__l2cap_chan_set_err(chan, err);
2322e0052e4SAndrei Emeltchenko 	release_sock(sk);
2332e0052e4SAndrei Emeltchenko }
2342e0052e4SAndrei Emeltchenko 
2353c588192SMat Martineau /* ---- L2CAP sequence number lists ---- */
2363c588192SMat Martineau 
2373c588192SMat Martineau /* For ERTM, ordered lists of sequence numbers must be tracked for
2383c588192SMat Martineau  * SREJ requests that are received and for frames that are to be
2393c588192SMat Martineau  * retransmitted. These seq_list functions implement a singly-linked
2403c588192SMat Martineau  * list in an array, where membership in the list can also be checked
2413c588192SMat Martineau  * in constant time. Items can also be added to the tail of the list
2423c588192SMat Martineau  * and removed from the head in constant time, without further memory
2433c588192SMat Martineau  * allocs or frees.
2443c588192SMat Martineau  */
2453c588192SMat Martineau 
2463c588192SMat Martineau static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
2473c588192SMat Martineau {
2483c588192SMat Martineau 	size_t alloc_size, i;
2493c588192SMat Martineau 
2503c588192SMat Martineau 	/* Allocated size is a power of 2 to map sequence numbers
2513c588192SMat Martineau 	 * (which may be up to 14 bits) in to a smaller array that is
2523c588192SMat Martineau 	 * sized for the negotiated ERTM transmit windows.
2533c588192SMat Martineau 	 */
2543c588192SMat Martineau 	alloc_size = roundup_pow_of_two(size);
2553c588192SMat Martineau 
2563c588192SMat Martineau 	seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL);
2573c588192SMat Martineau 	if (!seq_list->list)
2583c588192SMat Martineau 		return -ENOMEM;
2593c588192SMat Martineau 
2603c588192SMat Martineau 	seq_list->mask = alloc_size - 1;
2613c588192SMat Martineau 	seq_list->head = L2CAP_SEQ_LIST_CLEAR;
2623c588192SMat Martineau 	seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
2633c588192SMat Martineau 	for (i = 0; i < alloc_size; i++)
2643c588192SMat Martineau 		seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
2653c588192SMat Martineau 
2663c588192SMat Martineau 	return 0;
2673c588192SMat Martineau }
2683c588192SMat Martineau 
2693c588192SMat Martineau static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
2703c588192SMat Martineau {
2713c588192SMat Martineau 	kfree(seq_list->list);
2723c588192SMat Martineau }
2733c588192SMat Martineau 
2743c588192SMat Martineau static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
2753c588192SMat Martineau 					   u16 seq)
2763c588192SMat Martineau {
2773c588192SMat Martineau 	/* Constant-time check for list membership */
2783c588192SMat Martineau 	return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
2793c588192SMat Martineau }
2803c588192SMat Martineau 
2813c588192SMat Martineau static u16 l2cap_seq_list_remove(struct l2cap_seq_list *seq_list, u16 seq)
2823c588192SMat Martineau {
2833c588192SMat Martineau 	u16 mask = seq_list->mask;
2843c588192SMat Martineau 
2853c588192SMat Martineau 	if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) {
2863c588192SMat Martineau 		/* In case someone tries to pop the head of an empty list */
2873c588192SMat Martineau 		return L2CAP_SEQ_LIST_CLEAR;
2883c588192SMat Martineau 	} else if (seq_list->head == seq) {
2893c588192SMat Martineau 		/* Head can be removed in constant time */
2903c588192SMat Martineau 		seq_list->head = seq_list->list[seq & mask];
2913c588192SMat Martineau 		seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
2923c588192SMat Martineau 
2933c588192SMat Martineau 		if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
2943c588192SMat Martineau 			seq_list->head = L2CAP_SEQ_LIST_CLEAR;
2953c588192SMat Martineau 			seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
2963c588192SMat Martineau 		}
2973c588192SMat Martineau 	} else {
2983c588192SMat Martineau 		/* Walk the list to find the sequence number */
2993c588192SMat Martineau 		u16 prev = seq_list->head;
3003c588192SMat Martineau 		while (seq_list->list[prev & mask] != seq) {
3013c588192SMat Martineau 			prev = seq_list->list[prev & mask];
3023c588192SMat Martineau 			if (prev == L2CAP_SEQ_LIST_TAIL)
3033c588192SMat Martineau 				return L2CAP_SEQ_LIST_CLEAR;
3043c588192SMat Martineau 		}
3053c588192SMat Martineau 
3063c588192SMat Martineau 		/* Unlink the number from the list and clear it */
3073c588192SMat Martineau 		seq_list->list[prev & mask] = seq_list->list[seq & mask];
3083c588192SMat Martineau 		seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
3093c588192SMat Martineau 		if (seq_list->tail == seq)
3103c588192SMat Martineau 			seq_list->tail = prev;
3113c588192SMat Martineau 	}
3123c588192SMat Martineau 	return seq;
3133c588192SMat Martineau }
3143c588192SMat Martineau 
3153c588192SMat Martineau static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
3163c588192SMat Martineau {
3173c588192SMat Martineau 	/* Remove the head in constant time */
3183c588192SMat Martineau 	return l2cap_seq_list_remove(seq_list, seq_list->head);
3193c588192SMat Martineau }
3203c588192SMat Martineau 
3213c588192SMat Martineau static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
3223c588192SMat Martineau {
3233c588192SMat Martineau 	if (seq_list->head != L2CAP_SEQ_LIST_CLEAR) {
3243c588192SMat Martineau 		u16 i;
3253c588192SMat Martineau 		for (i = 0; i <= seq_list->mask; i++)
3263c588192SMat Martineau 			seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
3273c588192SMat Martineau 
3283c588192SMat Martineau 		seq_list->head = L2CAP_SEQ_LIST_CLEAR;
3293c588192SMat Martineau 		seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
3303c588192SMat Martineau 	}
3313c588192SMat Martineau }
3323c588192SMat Martineau 
3333c588192SMat Martineau static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
3343c588192SMat Martineau {
3353c588192SMat Martineau 	u16 mask = seq_list->mask;
3363c588192SMat Martineau 
3373c588192SMat Martineau 	/* All appends happen in constant time */
3383c588192SMat Martineau 
3393c588192SMat Martineau 	if (seq_list->list[seq & mask] == L2CAP_SEQ_LIST_CLEAR) {
3403c588192SMat Martineau 		if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
3413c588192SMat Martineau 			seq_list->head = seq;
3423c588192SMat Martineau 		else
3433c588192SMat Martineau 			seq_list->list[seq_list->tail & mask] = seq;
3443c588192SMat Martineau 
3453c588192SMat Martineau 		seq_list->tail = seq;
3463c588192SMat Martineau 		seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
3473c588192SMat Martineau 	}
3483c588192SMat Martineau }
3493c588192SMat Martineau 
350721c4181SGustavo F. Padovan static void l2cap_chan_timeout(struct work_struct *work)
351ab07801dSGustavo F. Padovan {
352721c4181SGustavo F. Padovan 	struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
353721c4181SGustavo F. Padovan 							chan_timer.work);
3543df91ea2SAndrei Emeltchenko 	struct l2cap_conn *conn = chan->conn;
355ab07801dSGustavo F. Padovan 	int reason;
356ab07801dSGustavo F. Padovan 
357e05dcc32SAndrei Emeltchenko 	BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
358ab07801dSGustavo F. Padovan 
3593df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
3606be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
361ab07801dSGustavo F. Padovan 
36289bc500eSGustavo F. Padovan 	if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
363ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
36489bc500eSGustavo F. Padovan 	else if (chan->state == BT_CONNECT &&
365ab07801dSGustavo F. Padovan 					chan->sec_level != BT_SECURITY_SDP)
366ab07801dSGustavo F. Padovan 		reason = ECONNREFUSED;
367ab07801dSGustavo F. Padovan 	else
368ab07801dSGustavo F. Padovan 		reason = ETIMEDOUT;
369ab07801dSGustavo F. Padovan 
3700f852724SGustavo F. Padovan 	l2cap_chan_close(chan, reason);
371ab07801dSGustavo F. Padovan 
3726be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
373ab07801dSGustavo F. Padovan 
374ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
3753df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
3763df91ea2SAndrei Emeltchenko 
377371fd835SUlisses Furquim 	l2cap_chan_put(chan);
378ab07801dSGustavo F. Padovan }
379ab07801dSGustavo F. Padovan 
380eef1d9b6SGustavo Padovan struct l2cap_chan *l2cap_chan_create(void)
3810a708f8fSGustavo F. Padovan {
38248454079SGustavo F. Padovan 	struct l2cap_chan *chan;
3830a708f8fSGustavo F. Padovan 
38448454079SGustavo F. Padovan 	chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
38548454079SGustavo F. Padovan 	if (!chan)
38648454079SGustavo F. Padovan 		return NULL;
3870a708f8fSGustavo F. Padovan 
388c03b355eSAndrei Emeltchenko 	mutex_init(&chan->lock);
389c03b355eSAndrei Emeltchenko 
390333055f2SGustavo F. Padovan 	write_lock(&chan_list_lock);
39123691d75SGustavo F. Padovan 	list_add(&chan->global_l, &chan_list);
392333055f2SGustavo F. Padovan 	write_unlock(&chan_list_lock);
39323691d75SGustavo F. Padovan 
394721c4181SGustavo F. Padovan 	INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout);
395ab07801dSGustavo F. Padovan 
39689bc500eSGustavo F. Padovan 	chan->state = BT_OPEN;
39789bc500eSGustavo F. Padovan 
39871ba0e56SGustavo F. Padovan 	atomic_set(&chan->refcnt, 1);
39971ba0e56SGustavo F. Padovan 
400eef1d9b6SGustavo Padovan 	BT_DBG("chan %p", chan);
401abc545b8SSzymon Janc 
40248454079SGustavo F. Padovan 	return chan;
4030a708f8fSGustavo F. Padovan }
4040a708f8fSGustavo F. Padovan 
40523691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan)
4066ff5abbfSGustavo F. Padovan {
407333055f2SGustavo F. Padovan 	write_lock(&chan_list_lock);
40823691d75SGustavo F. Padovan 	list_del(&chan->global_l);
409333055f2SGustavo F. Padovan 	write_unlock(&chan_list_lock);
41023691d75SGustavo F. Padovan 
411371fd835SUlisses Furquim 	l2cap_chan_put(chan);
4126ff5abbfSGustavo F. Padovan }
4136ff5abbfSGustavo F. Padovan 
414bd4b1653SAndrei Emeltchenko void l2cap_chan_set_defaults(struct l2cap_chan *chan)
415bd4b1653SAndrei Emeltchenko {
416bd4b1653SAndrei Emeltchenko 	chan->fcs  = L2CAP_FCS_CRC16;
417bd4b1653SAndrei Emeltchenko 	chan->max_tx = L2CAP_DEFAULT_MAX_TX;
418bd4b1653SAndrei Emeltchenko 	chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
419bd4b1653SAndrei Emeltchenko 	chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
420bd4b1653SAndrei Emeltchenko 	chan->sec_level = BT_SECURITY_LOW;
421bd4b1653SAndrei Emeltchenko 
422bd4b1653SAndrei Emeltchenko 	set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
423bd4b1653SAndrei Emeltchenko }
424bd4b1653SAndrei Emeltchenko 
42514a28491SAndrei Emeltchenko static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
4260a708f8fSGustavo F. Padovan {
4270a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
428097db76cSAndrei Emeltchenko 	       __le16_to_cpu(chan->psm), chan->dcid);
4290a708f8fSGustavo F. Padovan 
4309f5a0d7bSAndrei Emeltchenko 	conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
4310a708f8fSGustavo F. Padovan 
4328c1d787bSGustavo F. Padovan 	chan->conn = conn;
4330a708f8fSGustavo F. Padovan 
4345491120eSAndrei Emeltchenko 	switch (chan->chan_type) {
4355491120eSAndrei Emeltchenko 	case L2CAP_CHAN_CONN_ORIENTED:
436b62f328bSVille Tervo 		if (conn->hcon->type == LE_LINK) {
437b62f328bSVille Tervo 			/* LE connection */
4380c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_LE_DEFAULT_MTU;
439fe4128e0SGustavo F. Padovan 			chan->scid = L2CAP_CID_LE_DATA;
440fe4128e0SGustavo F. Padovan 			chan->dcid = L2CAP_CID_LE_DATA;
441b62f328bSVille Tervo 		} else {
4420a708f8fSGustavo F. Padovan 			/* Alloc CID for connection-oriented socket */
443fe4128e0SGustavo F. Padovan 			chan->scid = l2cap_alloc_cid(conn);
4440c1bc5c6SGustavo F. Padovan 			chan->omtu = L2CAP_DEFAULT_MTU;
445b62f328bSVille Tervo 		}
4465491120eSAndrei Emeltchenko 		break;
4475491120eSAndrei Emeltchenko 
4485491120eSAndrei Emeltchenko 	case L2CAP_CHAN_CONN_LESS:
4490a708f8fSGustavo F. Padovan 		/* Connectionless socket */
450fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_CONN_LESS;
451fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_CONN_LESS;
4520c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
4535491120eSAndrei Emeltchenko 		break;
4545491120eSAndrei Emeltchenko 
4555491120eSAndrei Emeltchenko 	default:
4560a708f8fSGustavo F. Padovan 		/* Raw socket can send/recv signalling messages only */
457fe4128e0SGustavo F. Padovan 		chan->scid = L2CAP_CID_SIGNALING;
458fe4128e0SGustavo F. Padovan 		chan->dcid = L2CAP_CID_SIGNALING;
4590c1bc5c6SGustavo F. Padovan 		chan->omtu = L2CAP_DEFAULT_MTU;
4600a708f8fSGustavo F. Padovan 	}
4610a708f8fSGustavo F. Padovan 
4628f7975b1SAndrei Emeltchenko 	chan->local_id		= L2CAP_BESTEFFORT_ID;
4638f7975b1SAndrei Emeltchenko 	chan->local_stype	= L2CAP_SERV_BESTEFFORT;
4648f7975b1SAndrei Emeltchenko 	chan->local_msdu	= L2CAP_DEFAULT_MAX_SDU_SIZE;
4658f7975b1SAndrei Emeltchenko 	chan->local_sdu_itime	= L2CAP_DEFAULT_SDU_ITIME;
4668f7975b1SAndrei Emeltchenko 	chan->local_acc_lat	= L2CAP_DEFAULT_ACC_LAT;
4678f7975b1SAndrei Emeltchenko 	chan->local_flush_to	= L2CAP_DEFAULT_FLUSH_TO;
4688f7975b1SAndrei Emeltchenko 
469371fd835SUlisses Furquim 	l2cap_chan_hold(chan);
470baa7e1faSGustavo F. Padovan 
4713df91ea2SAndrei Emeltchenko 	list_add(&chan->list, &conn->chan_l);
472643162a8SAndrei Emeltchenko }
473643162a8SAndrei Emeltchenko 
47414a28491SAndrei Emeltchenko static void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
475643162a8SAndrei Emeltchenko {
476643162a8SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
477643162a8SAndrei Emeltchenko 	__l2cap_chan_add(conn, chan);
4783df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
4790a708f8fSGustavo F. Padovan }
4800a708f8fSGustavo F. Padovan 
4814519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err)
4820a708f8fSGustavo F. Padovan {
48348454079SGustavo F. Padovan 	struct sock *sk = chan->sk;
4848c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
4850a708f8fSGustavo F. Padovan 	struct sock *parent = bt_sk(sk)->parent;
4860a708f8fSGustavo F. Padovan 
487c9b66675SGustavo F. Padovan 	__clear_chan_timer(chan);
4880a708f8fSGustavo F. Padovan 
48949208c9cSGustavo F. Padovan 	BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
4900a708f8fSGustavo F. Padovan 
4910a708f8fSGustavo F. Padovan 	if (conn) {
492baa7e1faSGustavo F. Padovan 		/* Delete from channel list */
4933df91ea2SAndrei Emeltchenko 		list_del(&chan->list);
4943d57dc68SGustavo F. Padovan 
495371fd835SUlisses Furquim 		l2cap_chan_put(chan);
496baa7e1faSGustavo F. Padovan 
4978c1d787bSGustavo F. Padovan 		chan->conn = NULL;
4980a708f8fSGustavo F. Padovan 		hci_conn_put(conn->hcon);
4990a708f8fSGustavo F. Padovan 	}
5000a708f8fSGustavo F. Padovan 
5016be36555SAndrei Emeltchenko 	lock_sock(sk);
5026be36555SAndrei Emeltchenko 
5030e587be7SAndrei Emeltchenko 	__l2cap_state_change(chan, BT_CLOSED);
5040a708f8fSGustavo F. Padovan 	sock_set_flag(sk, SOCK_ZAPPED);
5050a708f8fSGustavo F. Padovan 
5060a708f8fSGustavo F. Padovan 	if (err)
5072e0052e4SAndrei Emeltchenko 		__l2cap_chan_set_err(chan, err);
5080a708f8fSGustavo F. Padovan 
5090a708f8fSGustavo F. Padovan 	if (parent) {
5100a708f8fSGustavo F. Padovan 		bt_accept_unlink(sk);
5110a708f8fSGustavo F. Padovan 		parent->sk_data_ready(parent, 0);
5120a708f8fSGustavo F. Padovan 	} else
5130a708f8fSGustavo F. Padovan 		sk->sk_state_change(sk);
5140a708f8fSGustavo F. Padovan 
5156be36555SAndrei Emeltchenko 	release_sock(sk);
5166be36555SAndrei Emeltchenko 
517c1360a1cSGustavo F. Padovan 	if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) &&
518c1360a1cSGustavo F. Padovan 			test_bit(CONF_INPUT_DONE, &chan->conf_state)))
5196ff5abbfSGustavo F. Padovan 		return;
5202ead70b8SGustavo F. Padovan 
52158d35f87SGustavo F. Padovan 	skb_queue_purge(&chan->tx_q);
5220a708f8fSGustavo F. Padovan 
5230c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
5240a708f8fSGustavo F. Padovan 		struct srej_list *l, *tmp;
5250a708f8fSGustavo F. Padovan 
5261a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
5271a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
5281a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
5290a708f8fSGustavo F. Padovan 
530f1c6775bSGustavo F. Padovan 		skb_queue_purge(&chan->srej_q);
5310a708f8fSGustavo F. Padovan 
5323c588192SMat Martineau 		l2cap_seq_list_free(&chan->srej_list);
5333c588192SMat Martineau 		l2cap_seq_list_free(&chan->retrans_list);
53439d5a3eeSGustavo F. Padovan 		list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
5350a708f8fSGustavo F. Padovan 			list_del(&l->list);
5360a708f8fSGustavo F. Padovan 			kfree(l);
5370a708f8fSGustavo F. Padovan 		}
5380a708f8fSGustavo F. Padovan 	}
5390a708f8fSGustavo F. Padovan }
5400a708f8fSGustavo F. Padovan 
5414519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent)
5424519de9aSGustavo F. Padovan {
5434519de9aSGustavo F. Padovan 	struct sock *sk;
5444519de9aSGustavo F. Padovan 
5454519de9aSGustavo F. Padovan 	BT_DBG("parent %p", parent);
5464519de9aSGustavo F. Padovan 
5474519de9aSGustavo F. Padovan 	/* Close not yet accepted channels */
5480f852724SGustavo F. Padovan 	while ((sk = bt_accept_dequeue(parent, NULL))) {
549ba3bd0eeSGustavo F. Padovan 		struct l2cap_chan *chan = l2cap_pi(sk)->chan;
5503df91ea2SAndrei Emeltchenko 
5516be36555SAndrei Emeltchenko 		l2cap_chan_lock(chan);
552c9b66675SGustavo F. Padovan 		__clear_chan_timer(chan);
553ba3bd0eeSGustavo F. Padovan 		l2cap_chan_close(chan, ECONNRESET);
5546be36555SAndrei Emeltchenko 		l2cap_chan_unlock(chan);
5553df91ea2SAndrei Emeltchenko 
556ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
5570f852724SGustavo F. Padovan 	}
5584519de9aSGustavo F. Padovan }
5594519de9aSGustavo F. Padovan 
5600f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason)
5614519de9aSGustavo F. Padovan {
5624519de9aSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
5634519de9aSGustavo F. Padovan 	struct sock *sk = chan->sk;
5644519de9aSGustavo F. Padovan 
565e05dcc32SAndrei Emeltchenko 	BT_DBG("chan %p state %s sk %p", chan,
566e05dcc32SAndrei Emeltchenko 					state_to_string(chan->state), sk);
5674519de9aSGustavo F. Padovan 
56889bc500eSGustavo F. Padovan 	switch (chan->state) {
5694519de9aSGustavo F. Padovan 	case BT_LISTEN:
5706be36555SAndrei Emeltchenko 		lock_sock(sk);
5714519de9aSGustavo F. Padovan 		l2cap_chan_cleanup_listen(sk);
57289bc500eSGustavo F. Padovan 
5730e587be7SAndrei Emeltchenko 		__l2cap_state_change(chan, BT_CLOSED);
57489bc500eSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
5756be36555SAndrei Emeltchenko 		release_sock(sk);
5764519de9aSGustavo F. Padovan 		break;
5774519de9aSGustavo F. Padovan 
5784519de9aSGustavo F. Padovan 	case BT_CONNECTED:
5794519de9aSGustavo F. Padovan 	case BT_CONFIG:
580715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
5814519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
582c9b66675SGustavo F. Padovan 			__set_chan_timer(chan, sk->sk_sndtimeo);
5834519de9aSGustavo F. Padovan 			l2cap_send_disconn_req(conn, chan, reason);
5844519de9aSGustavo F. Padovan 		} else
5854519de9aSGustavo F. Padovan 			l2cap_chan_del(chan, reason);
5864519de9aSGustavo F. Padovan 		break;
5874519de9aSGustavo F. Padovan 
5884519de9aSGustavo F. Padovan 	case BT_CONNECT2:
589715ec005SGustavo F. Padovan 		if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
5904519de9aSGustavo F. Padovan 					conn->hcon->type == ACL_LINK) {
5914519de9aSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
5924519de9aSGustavo F. Padovan 			__u16 result;
5934519de9aSGustavo F. Padovan 
5944519de9aSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup)
5954519de9aSGustavo F. Padovan 				result = L2CAP_CR_SEC_BLOCK;
5964519de9aSGustavo F. Padovan 			else
5974519de9aSGustavo F. Padovan 				result = L2CAP_CR_BAD_PSM;
59889bc500eSGustavo F. Padovan 			l2cap_state_change(chan, BT_DISCONN);
5994519de9aSGustavo F. Padovan 
6004519de9aSGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
6014519de9aSGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
6024519de9aSGustavo F. Padovan 			rsp.result = cpu_to_le16(result);
6034519de9aSGustavo F. Padovan 			rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
6044519de9aSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
6054519de9aSGustavo F. Padovan 							sizeof(rsp), &rsp);
6064519de9aSGustavo F. Padovan 		}
6074519de9aSGustavo F. Padovan 
6084519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
6094519de9aSGustavo F. Padovan 		break;
6104519de9aSGustavo F. Padovan 
6114519de9aSGustavo F. Padovan 	case BT_CONNECT:
6124519de9aSGustavo F. Padovan 	case BT_DISCONN:
6134519de9aSGustavo F. Padovan 		l2cap_chan_del(chan, reason);
6144519de9aSGustavo F. Padovan 		break;
6154519de9aSGustavo F. Padovan 
6164519de9aSGustavo F. Padovan 	default:
6176be36555SAndrei Emeltchenko 		lock_sock(sk);
6184519de9aSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
6196be36555SAndrei Emeltchenko 		release_sock(sk);
6204519de9aSGustavo F. Padovan 		break;
6214519de9aSGustavo F. Padovan 	}
6224519de9aSGustavo F. Padovan }
6234519de9aSGustavo F. Padovan 
6244343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
6250a708f8fSGustavo F. Padovan {
626715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_RAW) {
6274343478fSGustavo F. Padovan 		switch (chan->sec_level) {
6280a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
6290a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING_MITM;
6300a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
6310a708f8fSGustavo F. Padovan 			return HCI_AT_DEDICATED_BONDING;
6320a708f8fSGustavo F. Padovan 		default:
6330a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
6340a708f8fSGustavo F. Padovan 		}
635fe4128e0SGustavo F. Padovan 	} else if (chan->psm == cpu_to_le16(0x0001)) {
6364343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_LOW)
6374343478fSGustavo F. Padovan 			chan->sec_level = BT_SECURITY_SDP;
6380a708f8fSGustavo F. Padovan 
6394343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_HIGH)
6400a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING_MITM;
6410a708f8fSGustavo F. Padovan 		else
6420a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
6430a708f8fSGustavo F. Padovan 	} else {
6444343478fSGustavo F. Padovan 		switch (chan->sec_level) {
6450a708f8fSGustavo F. Padovan 		case BT_SECURITY_HIGH:
6460a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING_MITM;
6470a708f8fSGustavo F. Padovan 		case BT_SECURITY_MEDIUM:
6480a708f8fSGustavo F. Padovan 			return HCI_AT_GENERAL_BONDING;
6490a708f8fSGustavo F. Padovan 		default:
6500a708f8fSGustavo F. Padovan 			return HCI_AT_NO_BONDING;
6510a708f8fSGustavo F. Padovan 		}
6520a708f8fSGustavo F. Padovan 	}
6530a708f8fSGustavo F. Padovan }
6540a708f8fSGustavo F. Padovan 
6550a708f8fSGustavo F. Padovan /* Service level security */
656d45fc423SGustavo F. Padovan int l2cap_chan_check_security(struct l2cap_chan *chan)
6570a708f8fSGustavo F. Padovan {
6588c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
6590a708f8fSGustavo F. Padovan 	__u8 auth_type;
6600a708f8fSGustavo F. Padovan 
6614343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
6620a708f8fSGustavo F. Padovan 
6634343478fSGustavo F. Padovan 	return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
6640a708f8fSGustavo F. Padovan }
6650a708f8fSGustavo F. Padovan 
666b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn)
6670a708f8fSGustavo F. Padovan {
6680a708f8fSGustavo F. Padovan 	u8 id;
6690a708f8fSGustavo F. Padovan 
6700a708f8fSGustavo F. Padovan 	/* Get next available identificator.
6710a708f8fSGustavo F. Padovan 	 *    1 - 128 are used by kernel.
6720a708f8fSGustavo F. Padovan 	 *  129 - 199 are reserved.
6730a708f8fSGustavo F. Padovan 	 *  200 - 254 are used by utilities like l2ping, etc.
6740a708f8fSGustavo F. Padovan 	 */
6750a708f8fSGustavo F. Padovan 
676333055f2SGustavo F. Padovan 	spin_lock(&conn->lock);
6770a708f8fSGustavo F. Padovan 
6780a708f8fSGustavo F. Padovan 	if (++conn->tx_ident > 128)
6790a708f8fSGustavo F. Padovan 		conn->tx_ident = 1;
6800a708f8fSGustavo F. Padovan 
6810a708f8fSGustavo F. Padovan 	id = conn->tx_ident;
6820a708f8fSGustavo F. Padovan 
683333055f2SGustavo F. Padovan 	spin_unlock(&conn->lock);
6840a708f8fSGustavo F. Padovan 
6850a708f8fSGustavo F. Padovan 	return id;
6860a708f8fSGustavo F. Padovan }
6870a708f8fSGustavo F. Padovan 
6884519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
6890a708f8fSGustavo F. Padovan {
6900a708f8fSGustavo F. Padovan 	struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
6910a708f8fSGustavo F. Padovan 	u8 flags;
6920a708f8fSGustavo F. Padovan 
6930a708f8fSGustavo F. Padovan 	BT_DBG("code 0x%2.2x", code);
6940a708f8fSGustavo F. Padovan 
6950a708f8fSGustavo F. Padovan 	if (!skb)
6960a708f8fSGustavo F. Padovan 		return;
6970a708f8fSGustavo F. Padovan 
6980a708f8fSGustavo F. Padovan 	if (lmp_no_flush_capable(conn->hcon->hdev))
6990a708f8fSGustavo F. Padovan 		flags = ACL_START_NO_FLUSH;
7000a708f8fSGustavo F. Padovan 	else
7010a708f8fSGustavo F. Padovan 		flags = ACL_START;
7020a708f8fSGustavo F. Padovan 
70314b12d0bSJaikumar Ganesh 	bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
7045e59b791SLuiz Augusto von Dentz 	skb->priority = HCI_PRIO_MAX;
70514b12d0bSJaikumar Ganesh 
70673d80debSLuiz Augusto von Dentz 	hci_send_acl(conn->hchan, skb, flags);
7070a708f8fSGustavo F. Padovan }
7080a708f8fSGustavo F. Padovan 
70973d80debSLuiz Augusto von Dentz static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
71073d80debSLuiz Augusto von Dentz {
71173d80debSLuiz Augusto von Dentz 	struct hci_conn *hcon = chan->conn->hcon;
71273d80debSLuiz Augusto von Dentz 	u16 flags;
71373d80debSLuiz Augusto von Dentz 
71473d80debSLuiz Augusto von Dentz 	BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
71573d80debSLuiz Augusto von Dentz 							skb->priority);
71673d80debSLuiz Augusto von Dentz 
71773d80debSLuiz Augusto von Dentz 	if (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
71873d80debSLuiz Augusto von Dentz 					lmp_no_flush_capable(hcon->hdev))
71973d80debSLuiz Augusto von Dentz 		flags = ACL_START_NO_FLUSH;
72073d80debSLuiz Augusto von Dentz 	else
72173d80debSLuiz Augusto von Dentz 		flags = ACL_START;
72273d80debSLuiz Augusto von Dentz 
72373d80debSLuiz Augusto von Dentz 	bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
72473d80debSLuiz Augusto von Dentz 	hci_send_acl(chan->conn->hchan, skb, flags);
7250a708f8fSGustavo F. Padovan }
7260a708f8fSGustavo F. Padovan 
72788843ab0SAndrei Emeltchenko static inline void l2cap_send_sframe(struct l2cap_chan *chan, u32 control)
7280a708f8fSGustavo F. Padovan {
7290a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
7300a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
7318c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
732e4ca6d98SAndrei Emeltchenko 	int count, hlen;
7330a708f8fSGustavo F. Padovan 
73489bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
7350a708f8fSGustavo F. Padovan 		return;
7360a708f8fSGustavo F. Padovan 
737e4ca6d98SAndrei Emeltchenko 	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
738e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_EXT_HDR_SIZE;
739e4ca6d98SAndrei Emeltchenko 	else
740e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_ENH_HDR_SIZE;
7410a708f8fSGustavo F. Padovan 
7420a708f8fSGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
74303a51213SAndrei Emeltchenko 		hlen += L2CAP_FCS_SIZE;
7440a708f8fSGustavo F. Padovan 
74588843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, control 0x%8.8x", chan, control);
7460a708f8fSGustavo F. Padovan 
7470a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, hlen);
748793c2f1cSAndrei Emeltchenko 
749793c2f1cSAndrei Emeltchenko 	control |= __set_sframe(chan);
7500a708f8fSGustavo F. Padovan 
751e2ab4353SGustavo F. Padovan 	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
75203f6715dSAndrei Emeltchenko 		control |= __set_ctrl_final(chan);
7530a708f8fSGustavo F. Padovan 
754e2ab4353SGustavo F. Padovan 	if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state))
755e3781735SAndrei Emeltchenko 		control |= __set_ctrl_poll(chan);
7560a708f8fSGustavo F. Padovan 
7570a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
7580a708f8fSGustavo F. Padovan 	if (!skb)
7590a708f8fSGustavo F. Padovan 		return;
7600a708f8fSGustavo F. Padovan 
7610a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
7620a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
763fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
76488843ab0SAndrei Emeltchenko 
76588843ab0SAndrei Emeltchenko 	__put_control(chan, control, skb_put(skb, __ctrl_size(chan)));
7660a708f8fSGustavo F. Padovan 
76747d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
76803a51213SAndrei Emeltchenko 		u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE);
76903a51213SAndrei Emeltchenko 		put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
7700a708f8fSGustavo F. Padovan 	}
7710a708f8fSGustavo F. Padovan 
77273d80debSLuiz Augusto von Dentz 	skb->priority = HCI_PRIO_MAX;
77373d80debSLuiz Augusto von Dentz 	l2cap_do_send(chan, skb);
7740a708f8fSGustavo F. Padovan }
7750a708f8fSGustavo F. Padovan 
77688843ab0SAndrei Emeltchenko static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u32 control)
7770a708f8fSGustavo F. Padovan {
778e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
779ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
780e2ab4353SGustavo F. Padovan 		set_bit(CONN_RNR_SENT, &chan->conn_state);
7810a708f8fSGustavo F. Padovan 	} else
782ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
7830a708f8fSGustavo F. Padovan 
7840b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
7850a708f8fSGustavo F. Padovan 
786525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
7870a708f8fSGustavo F. Padovan }
7880a708f8fSGustavo F. Padovan 
789b76bbd66SMat Martineau static u16 __pack_enhanced_control(struct l2cap_ctrl *control)
790b76bbd66SMat Martineau {
791b76bbd66SMat Martineau 	u16 packed;
792b76bbd66SMat Martineau 
793b76bbd66SMat Martineau 	packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT;
794b76bbd66SMat Martineau 	packed |= control->final << L2CAP_CTRL_FINAL_SHIFT;
795b76bbd66SMat Martineau 
796b76bbd66SMat Martineau 	if (control->sframe) {
797b76bbd66SMat Martineau 		packed |= control->poll << L2CAP_CTRL_POLL_SHIFT;
798b76bbd66SMat Martineau 		packed |= control->super << L2CAP_CTRL_SUPER_SHIFT;
799b76bbd66SMat Martineau 		packed |= L2CAP_CTRL_FRAME_TYPE;
800b76bbd66SMat Martineau 	} else {
801b76bbd66SMat Martineau 		packed |= control->sar << L2CAP_CTRL_SAR_SHIFT;
802b76bbd66SMat Martineau 		packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT;
803b76bbd66SMat Martineau 	}
804b76bbd66SMat Martineau 
805b76bbd66SMat Martineau 	return packed;
806b76bbd66SMat Martineau }
807b76bbd66SMat Martineau 
808b76bbd66SMat Martineau static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control)
809b76bbd66SMat Martineau {
810b76bbd66SMat Martineau 	control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT;
811b76bbd66SMat Martineau 	control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT;
812b76bbd66SMat Martineau 
813b76bbd66SMat Martineau 	if (enh & L2CAP_CTRL_FRAME_TYPE) {
814b76bbd66SMat Martineau 		/* S-Frame */
815b76bbd66SMat Martineau 		control->sframe = 1;
816b76bbd66SMat Martineau 		control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT;
817b76bbd66SMat Martineau 		control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT;
818b76bbd66SMat Martineau 
819b76bbd66SMat Martineau 		control->sar = 0;
820b76bbd66SMat Martineau 		control->txseq = 0;
821b76bbd66SMat Martineau 	} else {
822b76bbd66SMat Martineau 		/* I-Frame */
823b76bbd66SMat Martineau 		control->sframe = 0;
824b76bbd66SMat Martineau 		control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
825b76bbd66SMat Martineau 		control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT;
826b76bbd66SMat Martineau 
827b76bbd66SMat Martineau 		control->poll = 0;
828b76bbd66SMat Martineau 		control->super = 0;
829b76bbd66SMat Martineau 	}
830b76bbd66SMat Martineau }
831b76bbd66SMat Martineau 
832b76bbd66SMat Martineau static u32 __pack_extended_control(struct l2cap_ctrl *control)
833b76bbd66SMat Martineau {
834b76bbd66SMat Martineau 	u32 packed;
835b76bbd66SMat Martineau 
836b76bbd66SMat Martineau 	packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT;
837b76bbd66SMat Martineau 	packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT;
838b76bbd66SMat Martineau 
839b76bbd66SMat Martineau 	if (control->sframe) {
840b76bbd66SMat Martineau 		packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT;
841b76bbd66SMat Martineau 		packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT;
842b76bbd66SMat Martineau 		packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
843b76bbd66SMat Martineau 	} else {
844b76bbd66SMat Martineau 		packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT;
845b76bbd66SMat Martineau 		packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT;
846b76bbd66SMat Martineau 	}
847b76bbd66SMat Martineau 
848b76bbd66SMat Martineau 	return packed;
849b76bbd66SMat Martineau }
850b76bbd66SMat Martineau 
851b76bbd66SMat Martineau static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control)
852b76bbd66SMat Martineau {
853b76bbd66SMat Martineau 	control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT;
854b76bbd66SMat Martineau 	control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT;
855b76bbd66SMat Martineau 
856b76bbd66SMat Martineau 	if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) {
857b76bbd66SMat Martineau 		/* S-Frame */
858b76bbd66SMat Martineau 		control->sframe = 1;
859b76bbd66SMat Martineau 		control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT;
860b76bbd66SMat Martineau 		control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT;
861b76bbd66SMat Martineau 
862b76bbd66SMat Martineau 		control->sar = 0;
863b76bbd66SMat Martineau 		control->txseq = 0;
864b76bbd66SMat Martineau 	} else {
865b76bbd66SMat Martineau 		/* I-Frame */
866b76bbd66SMat Martineau 		control->sframe = 0;
867b76bbd66SMat Martineau 		control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
868b76bbd66SMat Martineau 		control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT;
869b76bbd66SMat Martineau 
870b76bbd66SMat Martineau 		control->poll = 0;
871b76bbd66SMat Martineau 		control->super = 0;
872b76bbd66SMat Martineau 	}
873b76bbd66SMat Martineau }
874b76bbd66SMat Martineau 
875b76bbd66SMat Martineau static inline void __unpack_control(struct l2cap_chan *chan,
876b76bbd66SMat Martineau 				    struct sk_buff *skb)
877b76bbd66SMat Martineau {
878b76bbd66SMat Martineau 	if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
879b76bbd66SMat Martineau 		__unpack_extended_control(get_unaligned_le32(skb->data),
880b76bbd66SMat Martineau 					  &bt_cb(skb)->control);
881b76bbd66SMat Martineau 	} else {
882b76bbd66SMat Martineau 		__unpack_enhanced_control(get_unaligned_le16(skb->data),
883b76bbd66SMat Martineau 					  &bt_cb(skb)->control);
884b76bbd66SMat Martineau 	}
885b76bbd66SMat Martineau }
886b76bbd66SMat Martineau 
887b76bbd66SMat Martineau static inline void __pack_control(struct l2cap_chan *chan,
888b76bbd66SMat Martineau 				  struct l2cap_ctrl *control,
889b76bbd66SMat Martineau 				  struct sk_buff *skb)
890b76bbd66SMat Martineau {
891b76bbd66SMat Martineau 	if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
892b76bbd66SMat Martineau 		put_unaligned_le32(__pack_extended_control(control),
893b76bbd66SMat Martineau 				   skb->data + L2CAP_HDR_SIZE);
894b76bbd66SMat Martineau 	} else {
895b76bbd66SMat Martineau 		put_unaligned_le16(__pack_enhanced_control(control),
896b76bbd66SMat Martineau 				   skb->data + L2CAP_HDR_SIZE);
897b76bbd66SMat Martineau 	}
898b76bbd66SMat Martineau }
899b76bbd66SMat Martineau 
900b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
9010a708f8fSGustavo F. Padovan {
902c1360a1cSGustavo F. Padovan 	return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
9030a708f8fSGustavo F. Padovan }
9040a708f8fSGustavo F. Padovan 
9059b27f350SAndrei Emeltchenko static void l2cap_send_conn_req(struct l2cap_chan *chan)
9069b27f350SAndrei Emeltchenko {
9079b27f350SAndrei Emeltchenko 	struct l2cap_conn *conn = chan->conn;
9089b27f350SAndrei Emeltchenko 	struct l2cap_conn_req req;
9099b27f350SAndrei Emeltchenko 
9109b27f350SAndrei Emeltchenko 	req.scid = cpu_to_le16(chan->scid);
9119b27f350SAndrei Emeltchenko 	req.psm  = chan->psm;
9129b27f350SAndrei Emeltchenko 
9139b27f350SAndrei Emeltchenko 	chan->ident = l2cap_get_ident(conn);
9149b27f350SAndrei Emeltchenko 
9159b27f350SAndrei Emeltchenko 	set_bit(CONF_CONNECT_PEND, &chan->conf_state);
9169b27f350SAndrei Emeltchenko 
9179b27f350SAndrei Emeltchenko 	l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req);
9189b27f350SAndrei Emeltchenko }
9199b27f350SAndrei Emeltchenko 
920fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan)
9210a708f8fSGustavo F. Padovan {
9228c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
9230a708f8fSGustavo F. Padovan 
9240a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
9250a708f8fSGustavo F. Padovan 		if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
9260a708f8fSGustavo F. Padovan 			return;
9270a708f8fSGustavo F. Padovan 
928d45fc423SGustavo F. Padovan 		if (l2cap_chan_check_security(chan) &&
9299b27f350SAndrei Emeltchenko 				__l2cap_no_conn_pending(chan))
9309b27f350SAndrei Emeltchenko 			l2cap_send_conn_req(chan);
9310a708f8fSGustavo F. Padovan 	} else {
9320a708f8fSGustavo F. Padovan 		struct l2cap_info_req req;
9330a708f8fSGustavo F. Padovan 		req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
9340a708f8fSGustavo F. Padovan 
9350a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
9360a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
9370a708f8fSGustavo F. Padovan 
938ba13ccd9SMarcel Holtmann 		schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
9390a708f8fSGustavo F. Padovan 
9400a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
9410a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
9420a708f8fSGustavo F. Padovan 	}
9430a708f8fSGustavo F. Padovan }
9440a708f8fSGustavo F. Padovan 
9450a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
9460a708f8fSGustavo F. Padovan {
9470a708f8fSGustavo F. Padovan 	u32 local_feat_mask = l2cap_feat_mask;
9480a708f8fSGustavo F. Padovan 	if (!disable_ertm)
9490a708f8fSGustavo F. Padovan 		local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
9500a708f8fSGustavo F. Padovan 
9510a708f8fSGustavo F. Padovan 	switch (mode) {
9520a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
9530a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
9540a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
9550a708f8fSGustavo F. Padovan 		return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
9560a708f8fSGustavo F. Padovan 	default:
9570a708f8fSGustavo F. Padovan 		return 0x00;
9580a708f8fSGustavo F. Padovan 	}
9590a708f8fSGustavo F. Padovan }
9600a708f8fSGustavo F. Padovan 
9614519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err)
9620a708f8fSGustavo F. Padovan {
9636be36555SAndrei Emeltchenko 	struct sock *sk = chan->sk;
9640a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req req;
9650a708f8fSGustavo F. Padovan 
9660a708f8fSGustavo F. Padovan 	if (!conn)
9670a708f8fSGustavo F. Padovan 		return;
9680a708f8fSGustavo F. Padovan 
9690c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_ERTM) {
9701a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
9711a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
9721a09bcb9SGustavo F. Padovan 		__clear_ack_timer(chan);
9730a708f8fSGustavo F. Padovan 	}
9740a708f8fSGustavo F. Padovan 
975fe4128e0SGustavo F. Padovan 	req.dcid = cpu_to_le16(chan->dcid);
976fe4128e0SGustavo F. Padovan 	req.scid = cpu_to_le16(chan->scid);
9770a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn),
9780a708f8fSGustavo F. Padovan 			L2CAP_DISCONN_REQ, sizeof(req), &req);
9790a708f8fSGustavo F. Padovan 
9806be36555SAndrei Emeltchenko 	lock_sock(sk);
9810e587be7SAndrei Emeltchenko 	__l2cap_state_change(chan, BT_DISCONN);
9822e0052e4SAndrei Emeltchenko 	__l2cap_chan_set_err(chan, err);
9836be36555SAndrei Emeltchenko 	release_sock(sk);
9840a708f8fSGustavo F. Padovan }
9850a708f8fSGustavo F. Padovan 
9860a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */
9870a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn)
9880a708f8fSGustavo F. Padovan {
9893df91ea2SAndrei Emeltchenko 	struct l2cap_chan *chan, *tmp;
9900a708f8fSGustavo F. Padovan 
9910a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
9920a708f8fSGustavo F. Padovan 
9933df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
9940a708f8fSGustavo F. Padovan 
9953df91ea2SAndrei Emeltchenko 	list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
99648454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
997baa7e1faSGustavo F. Padovan 
9986be36555SAndrei Emeltchenko 		l2cap_chan_lock(chan);
9990a708f8fSGustavo F. Padovan 
1000715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
10016be36555SAndrei Emeltchenko 			l2cap_chan_unlock(chan);
10020a708f8fSGustavo F. Padovan 			continue;
10030a708f8fSGustavo F. Padovan 		}
10040a708f8fSGustavo F. Padovan 
100589bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
1006d45fc423SGustavo F. Padovan 			if (!l2cap_chan_check_security(chan) ||
1007b4450035SGustavo F. Padovan 					!__l2cap_no_conn_pending(chan)) {
10086be36555SAndrei Emeltchenko 				l2cap_chan_unlock(chan);
10090a708f8fSGustavo F. Padovan 				continue;
10100a708f8fSGustavo F. Padovan 			}
10110a708f8fSGustavo F. Padovan 
1012c1360a1cSGustavo F. Padovan 			if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
1013c1360a1cSGustavo F. Padovan 					&& test_bit(CONF_STATE2_DEVICE,
1014c1360a1cSGustavo F. Padovan 					&chan->conf_state)) {
10150f852724SGustavo F. Padovan 				l2cap_chan_close(chan, ECONNRESET);
10166be36555SAndrei Emeltchenko 				l2cap_chan_unlock(chan);
10170a708f8fSGustavo F. Padovan 				continue;
10180a708f8fSGustavo F. Padovan 			}
10190a708f8fSGustavo F. Padovan 
10209b27f350SAndrei Emeltchenko 			l2cap_send_conn_req(chan);
10210a708f8fSGustavo F. Padovan 
102289bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
10230a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
10240a708f8fSGustavo F. Padovan 			char buf[128];
1025fe4128e0SGustavo F. Padovan 			rsp.scid = cpu_to_le16(chan->dcid);
1026fe4128e0SGustavo F. Padovan 			rsp.dcid = cpu_to_le16(chan->scid);
10270a708f8fSGustavo F. Padovan 
1028d45fc423SGustavo F. Padovan 			if (l2cap_chan_check_security(chan)) {
10296be36555SAndrei Emeltchenko 				lock_sock(sk);
10300a708f8fSGustavo F. Padovan 				if (bt_sk(sk)->defer_setup) {
10310a708f8fSGustavo F. Padovan 					struct sock *parent = bt_sk(sk)->parent;
10320a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_PEND);
10330a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
103405e9a2f6SIlia Kolomisnky 					if (parent)
10350a708f8fSGustavo F. Padovan 						parent->sk_data_ready(parent, 0);
10360a708f8fSGustavo F. Padovan 
10370a708f8fSGustavo F. Padovan 				} else {
10380e587be7SAndrei Emeltchenko 					__l2cap_state_change(chan, BT_CONFIG);
10390a708f8fSGustavo F. Padovan 					rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
10400a708f8fSGustavo F. Padovan 					rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
10410a708f8fSGustavo F. Padovan 				}
10426be36555SAndrei Emeltchenko 				release_sock(sk);
10430a708f8fSGustavo F. Padovan 			} else {
10440a708f8fSGustavo F. Padovan 				rsp.result = cpu_to_le16(L2CAP_CR_PEND);
10450a708f8fSGustavo F. Padovan 				rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
10460a708f8fSGustavo F. Padovan 			}
10470a708f8fSGustavo F. Padovan 
1048fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
1049fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
10500a708f8fSGustavo F. Padovan 
1051c1360a1cSGustavo F. Padovan 			if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
10520a708f8fSGustavo F. Padovan 					rsp.result != L2CAP_CR_SUCCESS) {
10536be36555SAndrei Emeltchenko 				l2cap_chan_unlock(chan);
10540a708f8fSGustavo F. Padovan 				continue;
10550a708f8fSGustavo F. Padovan 			}
10560a708f8fSGustavo F. Padovan 
1057c1360a1cSGustavo F. Padovan 			set_bit(CONF_REQ_SENT, &chan->conf_state);
10580a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
105973ffa904SGustavo F. Padovan 						l2cap_build_conf_req(chan, buf), buf);
106073ffa904SGustavo F. Padovan 			chan->num_conf_req++;
10610a708f8fSGustavo F. Padovan 		}
10620a708f8fSGustavo F. Padovan 
10636be36555SAndrei Emeltchenko 		l2cap_chan_unlock(chan);
10640a708f8fSGustavo F. Padovan 	}
10650a708f8fSGustavo F. Padovan 
10663df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
10670a708f8fSGustavo F. Padovan }
10680a708f8fSGustavo F. Padovan 
1069c2287681SIdo Yariv /* Find socket with cid and source/destination bdaddr.
1070b62f328bSVille Tervo  * Returns closest match, locked.
1071b62f328bSVille Tervo  */
1072d9b88702SAndrei Emeltchenko static struct l2cap_chan *l2cap_global_chan_by_scid(int state, u16 cid,
1073c2287681SIdo Yariv 						    bdaddr_t *src,
1074c2287681SIdo Yariv 						    bdaddr_t *dst)
1075b62f328bSVille Tervo {
107623691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
1077b62f328bSVille Tervo 
107823691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
1079b62f328bSVille Tervo 
108023691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
108123691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
1082fe4128e0SGustavo F. Padovan 
108389bc500eSGustavo F. Padovan 		if (state && c->state != state)
1084b62f328bSVille Tervo 			continue;
1085b62f328bSVille Tervo 
108623691d75SGustavo F. Padovan 		if (c->scid == cid) {
1087c2287681SIdo Yariv 			int src_match, dst_match;
1088c2287681SIdo Yariv 			int src_any, dst_any;
1089c2287681SIdo Yariv 
1090b62f328bSVille Tervo 			/* Exact match. */
1091c2287681SIdo Yariv 			src_match = !bacmp(&bt_sk(sk)->src, src);
1092c2287681SIdo Yariv 			dst_match = !bacmp(&bt_sk(sk)->dst, dst);
1093c2287681SIdo Yariv 			if (src_match && dst_match) {
109423691d75SGustavo F. Padovan 				read_unlock(&chan_list_lock);
109523691d75SGustavo F. Padovan 				return c;
109623691d75SGustavo F. Padovan 			}
1097b62f328bSVille Tervo 
1098b62f328bSVille Tervo 			/* Closest match */
1099c2287681SIdo Yariv 			src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY);
1100c2287681SIdo Yariv 			dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY);
1101c2287681SIdo Yariv 			if ((src_match && dst_any) || (src_any && dst_match) ||
1102c2287681SIdo Yariv 			    (src_any && dst_any))
110323691d75SGustavo F. Padovan 				c1 = c;
1104b62f328bSVille Tervo 		}
1105b62f328bSVille Tervo 	}
1106280f294fSGustavo F. Padovan 
110723691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
1108b62f328bSVille Tervo 
110923691d75SGustavo F. Padovan 	return c1;
1110b62f328bSVille Tervo }
1111b62f328bSVille Tervo 
1112b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1113b62f328bSVille Tervo {
1114c916fbe4SGustavo F. Padovan 	struct sock *parent, *sk;
111523691d75SGustavo F. Padovan 	struct l2cap_chan *chan, *pchan;
1116b62f328bSVille Tervo 
1117b62f328bSVille Tervo 	BT_DBG("");
1118b62f328bSVille Tervo 
1119b62f328bSVille Tervo 	/* Check if we have socket listening on cid */
112023691d75SGustavo F. Padovan 	pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
1121c2287681SIdo Yariv 					  conn->src, conn->dst);
112223691d75SGustavo F. Padovan 	if (!pchan)
1123b62f328bSVille Tervo 		return;
1124b62f328bSVille Tervo 
112523691d75SGustavo F. Padovan 	parent = pchan->sk;
112623691d75SGustavo F. Padovan 
1127aa2ac881SGustavo F. Padovan 	lock_sock(parent);
112862f3a2cfSGustavo F. Padovan 
1129b62f328bSVille Tervo 	/* Check for backlog size */
1130b62f328bSVille Tervo 	if (sk_acceptq_is_full(parent)) {
1131b62f328bSVille Tervo 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
1132b62f328bSVille Tervo 		goto clean;
1133b62f328bSVille Tervo 	}
1134b62f328bSVille Tervo 
113580808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
113680808e43SGustavo F. Padovan 	if (!chan)
1137b62f328bSVille Tervo 		goto clean;
1138b62f328bSVille Tervo 
113980808e43SGustavo F. Padovan 	sk = chan->sk;
11405d41ce1dSGustavo F. Padovan 
1141b62f328bSVille Tervo 	hci_conn_hold(conn->hcon);
1142b62f328bSVille Tervo 
1143b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->src, conn->src);
1144b62f328bSVille Tervo 	bacpy(&bt_sk(sk)->dst, conn->dst);
1145b62f328bSVille Tervo 
1146d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
1147d1010240SGustavo F. Padovan 
11483d57dc68SGustavo F. Padovan 	l2cap_chan_add(conn, chan);
114948454079SGustavo F. Padovan 
1150c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
1151b62f328bSVille Tervo 
11520e587be7SAndrei Emeltchenko 	__l2cap_state_change(chan, BT_CONNECTED);
1153b62f328bSVille Tervo 	parent->sk_data_ready(parent, 0);
1154b62f328bSVille Tervo 
1155b62f328bSVille Tervo clean:
1156aa2ac881SGustavo F. Padovan 	release_sock(parent);
1157b62f328bSVille Tervo }
1158b62f328bSVille Tervo 
1159cf4cd009SAndrei Emeltchenko static void l2cap_chan_ready(struct l2cap_chan *chan)
1160f1cb9af5SVinicius Costa Gomes {
1161cf4cd009SAndrei Emeltchenko 	struct sock *sk = chan->sk;
11626be36555SAndrei Emeltchenko 	struct sock *parent;
11636be36555SAndrei Emeltchenko 
11646be36555SAndrei Emeltchenko 	lock_sock(sk);
11656be36555SAndrei Emeltchenko 
11666be36555SAndrei Emeltchenko 	parent = bt_sk(sk)->parent;
1167f1cb9af5SVinicius Costa Gomes 
1168f1cb9af5SVinicius Costa Gomes 	BT_DBG("sk %p, parent %p", sk, parent);
1169f1cb9af5SVinicius Costa Gomes 
1170f1cb9af5SVinicius Costa Gomes 	chan->conf_state = 0;
1171f1cb9af5SVinicius Costa Gomes 	__clear_chan_timer(chan);
1172f1cb9af5SVinicius Costa Gomes 
11730e587be7SAndrei Emeltchenko 	__l2cap_state_change(chan, BT_CONNECTED);
1174f1cb9af5SVinicius Costa Gomes 	sk->sk_state_change(sk);
1175f1cb9af5SVinicius Costa Gomes 
1176f1cb9af5SVinicius Costa Gomes 	if (parent)
1177f1cb9af5SVinicius Costa Gomes 		parent->sk_data_ready(parent, 0);
11786be36555SAndrei Emeltchenko 
11796be36555SAndrei Emeltchenko 	release_sock(sk);
1180f1cb9af5SVinicius Costa Gomes }
1181f1cb9af5SVinicius Costa Gomes 
11820a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn)
11830a708f8fSGustavo F. Padovan {
118448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
11850a708f8fSGustavo F. Padovan 
11860a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
11870a708f8fSGustavo F. Padovan 
1188b62f328bSVille Tervo 	if (!conn->hcon->out && conn->hcon->type == LE_LINK)
1189b62f328bSVille Tervo 		l2cap_le_conn_ready(conn);
1190b62f328bSVille Tervo 
1191160dc6acSVinicius Costa Gomes 	if (conn->hcon->out && conn->hcon->type == LE_LINK)
1192160dc6acSVinicius Costa Gomes 		smp_conn_security(conn, conn->hcon->pending_sec_level);
1193160dc6acSVinicius Costa Gomes 
11943df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
11950a708f8fSGustavo F. Padovan 
11963df91ea2SAndrei Emeltchenko 	list_for_each_entry(chan, &conn->chan_l, list) {
1197baa7e1faSGustavo F. Padovan 
11986be36555SAndrei Emeltchenko 		l2cap_chan_lock(chan);
11990a708f8fSGustavo F. Padovan 
120063128451SVinicius Costa Gomes 		if (conn->hcon->type == LE_LINK) {
1201b501d6a1SAnderson Briglia 			if (smp_conn_security(conn, chan->sec_level))
1202cf4cd009SAndrei Emeltchenko 				l2cap_chan_ready(chan);
1203acd7d370SVille Tervo 
120463128451SVinicius Costa Gomes 		} else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
12056be36555SAndrei Emeltchenko 			struct sock *sk = chan->sk;
1206c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
12076be36555SAndrei Emeltchenko 			lock_sock(sk);
12080e587be7SAndrei Emeltchenko 			__l2cap_state_change(chan, BT_CONNECTED);
12090a708f8fSGustavo F. Padovan 			sk->sk_state_change(sk);
12106be36555SAndrei Emeltchenko 			release_sock(sk);
1211b501d6a1SAnderson Briglia 
121289bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT)
1213fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
12140a708f8fSGustavo F. Padovan 
12156be36555SAndrei Emeltchenko 		l2cap_chan_unlock(chan);
12160a708f8fSGustavo F. Padovan 	}
12170a708f8fSGustavo F. Padovan 
12183df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
12190a708f8fSGustavo F. Padovan }
12200a708f8fSGustavo F. Padovan 
12210a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */
12220a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
12230a708f8fSGustavo F. Padovan {
122448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
12250a708f8fSGustavo F. Padovan 
12260a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
12270a708f8fSGustavo F. Padovan 
12283df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
12290a708f8fSGustavo F. Padovan 
12303df91ea2SAndrei Emeltchenko 	list_for_each_entry(chan, &conn->chan_l, list) {
1231ecf61bdbSAndrei Emeltchenko 		if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
12322e0052e4SAndrei Emeltchenko 			__l2cap_chan_set_err(chan, err);
12330a708f8fSGustavo F. Padovan 	}
12340a708f8fSGustavo F. Padovan 
12353df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
12360a708f8fSGustavo F. Padovan }
12370a708f8fSGustavo F. Padovan 
1238f878fcadSGustavo F. Padovan static void l2cap_info_timeout(struct work_struct *work)
12390a708f8fSGustavo F. Padovan {
1240f878fcadSGustavo F. Padovan 	struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
1241030013d8SGustavo F. Padovan 							info_timer.work);
12420a708f8fSGustavo F. Padovan 
12430a708f8fSGustavo F. Padovan 	conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
12440a708f8fSGustavo F. Padovan 	conn->info_ident = 0;
12450a708f8fSGustavo F. Padovan 
12460a708f8fSGustavo F. Padovan 	l2cap_conn_start(conn);
12470a708f8fSGustavo F. Padovan }
12480a708f8fSGustavo F. Padovan 
12495d3de7dfSVinicius Costa Gomes static void l2cap_conn_del(struct hci_conn *hcon, int err)
12505d3de7dfSVinicius Costa Gomes {
12515d3de7dfSVinicius Costa Gomes 	struct l2cap_conn *conn = hcon->l2cap_data;
12525d3de7dfSVinicius Costa Gomes 	struct l2cap_chan *chan, *l;
12535d3de7dfSVinicius Costa Gomes 
12545d3de7dfSVinicius Costa Gomes 	if (!conn)
12555d3de7dfSVinicius Costa Gomes 		return;
12565d3de7dfSVinicius Costa Gomes 
12575d3de7dfSVinicius Costa Gomes 	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
12585d3de7dfSVinicius Costa Gomes 
12595d3de7dfSVinicius Costa Gomes 	kfree_skb(conn->rx_skb);
12605d3de7dfSVinicius Costa Gomes 
12613df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
12623df91ea2SAndrei Emeltchenko 
12635d3de7dfSVinicius Costa Gomes 	/* Kill channels */
12645d3de7dfSVinicius Costa Gomes 	list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
12656be36555SAndrei Emeltchenko 		l2cap_chan_lock(chan);
12666be36555SAndrei Emeltchenko 
12675d3de7dfSVinicius Costa Gomes 		l2cap_chan_del(chan, err);
12686be36555SAndrei Emeltchenko 
12696be36555SAndrei Emeltchenko 		l2cap_chan_unlock(chan);
12706be36555SAndrei Emeltchenko 
12715d3de7dfSVinicius Costa Gomes 		chan->ops->close(chan->data);
12725d3de7dfSVinicius Costa Gomes 	}
12735d3de7dfSVinicius Costa Gomes 
12743df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
12753df91ea2SAndrei Emeltchenko 
127673d80debSLuiz Augusto von Dentz 	hci_chan_del(conn->hchan);
127773d80debSLuiz Augusto von Dentz 
12785d3de7dfSVinicius Costa Gomes 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
1279127074bfSUlisses Furquim 		cancel_delayed_work_sync(&conn->info_timer);
12805d3de7dfSVinicius Costa Gomes 
128151a8efd7SJohan Hedberg 	if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) {
1282127074bfSUlisses Furquim 		cancel_delayed_work_sync(&conn->security_timer);
12838aab4757SVinicius Costa Gomes 		smp_chan_destroy(conn);
1284d26a2345SVinicius Costa Gomes 	}
12855d3de7dfSVinicius Costa Gomes 
12865d3de7dfSVinicius Costa Gomes 	hcon->l2cap_data = NULL;
12875d3de7dfSVinicius Costa Gomes 	kfree(conn);
12885d3de7dfSVinicius Costa Gomes }
12895d3de7dfSVinicius Costa Gomes 
12906c9d42a1SGustavo F. Padovan static void security_timeout(struct work_struct *work)
12915d3de7dfSVinicius Costa Gomes {
12926c9d42a1SGustavo F. Padovan 	struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
12936c9d42a1SGustavo F. Padovan 						security_timer.work);
12945d3de7dfSVinicius Costa Gomes 
12955d3de7dfSVinicius Costa Gomes 	l2cap_conn_del(conn->hcon, ETIMEDOUT);
12965d3de7dfSVinicius Costa Gomes }
12975d3de7dfSVinicius Costa Gomes 
12980a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
12990a708f8fSGustavo F. Padovan {
13000a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
130173d80debSLuiz Augusto von Dentz 	struct hci_chan *hchan;
13020a708f8fSGustavo F. Padovan 
13030a708f8fSGustavo F. Padovan 	if (conn || status)
13040a708f8fSGustavo F. Padovan 		return conn;
13050a708f8fSGustavo F. Padovan 
130673d80debSLuiz Augusto von Dentz 	hchan = hci_chan_create(hcon);
130773d80debSLuiz Augusto von Dentz 	if (!hchan)
13080a708f8fSGustavo F. Padovan 		return NULL;
13090a708f8fSGustavo F. Padovan 
131073d80debSLuiz Augusto von Dentz 	conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
131173d80debSLuiz Augusto von Dentz 	if (!conn) {
131273d80debSLuiz Augusto von Dentz 		hci_chan_del(hchan);
131373d80debSLuiz Augusto von Dentz 		return NULL;
131473d80debSLuiz Augusto von Dentz 	}
131573d80debSLuiz Augusto von Dentz 
13160a708f8fSGustavo F. Padovan 	hcon->l2cap_data = conn;
13170a708f8fSGustavo F. Padovan 	conn->hcon = hcon;
131873d80debSLuiz Augusto von Dentz 	conn->hchan = hchan;
13190a708f8fSGustavo F. Padovan 
132073d80debSLuiz Augusto von Dentz 	BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
13210a708f8fSGustavo F. Padovan 
1322acd7d370SVille Tervo 	if (hcon->hdev->le_mtu && hcon->type == LE_LINK)
1323acd7d370SVille Tervo 		conn->mtu = hcon->hdev->le_mtu;
1324acd7d370SVille Tervo 	else
13250a708f8fSGustavo F. Padovan 		conn->mtu = hcon->hdev->acl_mtu;
1326acd7d370SVille Tervo 
13270a708f8fSGustavo F. Padovan 	conn->src = &hcon->hdev->bdaddr;
13280a708f8fSGustavo F. Padovan 	conn->dst = &hcon->dst;
13290a708f8fSGustavo F. Padovan 
13300a708f8fSGustavo F. Padovan 	conn->feat_mask = 0;
13310a708f8fSGustavo F. Padovan 
13320a708f8fSGustavo F. Padovan 	spin_lock_init(&conn->lock);
13333df91ea2SAndrei Emeltchenko 	mutex_init(&conn->chan_lock);
1334baa7e1faSGustavo F. Padovan 
1335baa7e1faSGustavo F. Padovan 	INIT_LIST_HEAD(&conn->chan_l);
13360a708f8fSGustavo F. Padovan 
13375d3de7dfSVinicius Costa Gomes 	if (hcon->type == LE_LINK)
13386c9d42a1SGustavo F. Padovan 		INIT_DELAYED_WORK(&conn->security_timer, security_timeout);
13395d3de7dfSVinicius Costa Gomes 	else
1340030013d8SGustavo F. Padovan 		INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
13410a708f8fSGustavo F. Padovan 
13429f5a0d7bSAndrei Emeltchenko 	conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
13430a708f8fSGustavo F. Padovan 
13440a708f8fSGustavo F. Padovan 	return conn;
13450a708f8fSGustavo F. Padovan }
13460a708f8fSGustavo F. Padovan 
13470a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */
13480a708f8fSGustavo F. Padovan 
1349c2287681SIdo Yariv /* Find socket with psm and source / destination bdaddr.
13500a708f8fSGustavo F. Padovan  * Returns closest match.
13510a708f8fSGustavo F. Padovan  */
1352c2287681SIdo Yariv static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1353c2287681SIdo Yariv 						   bdaddr_t *src,
1354c2287681SIdo Yariv 						   bdaddr_t *dst)
13550a708f8fSGustavo F. Padovan {
135623691d75SGustavo F. Padovan 	struct l2cap_chan *c, *c1 = NULL;
13570a708f8fSGustavo F. Padovan 
135823691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
13590a708f8fSGustavo F. Padovan 
136023691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
136123691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
1362fe4128e0SGustavo F. Padovan 
136389bc500eSGustavo F. Padovan 		if (state && c->state != state)
13640a708f8fSGustavo F. Padovan 			continue;
13650a708f8fSGustavo F. Padovan 
136623691d75SGustavo F. Padovan 		if (c->psm == psm) {
1367c2287681SIdo Yariv 			int src_match, dst_match;
1368c2287681SIdo Yariv 			int src_any, dst_any;
1369c2287681SIdo Yariv 
13700a708f8fSGustavo F. Padovan 			/* Exact match. */
1371c2287681SIdo Yariv 			src_match = !bacmp(&bt_sk(sk)->src, src);
1372c2287681SIdo Yariv 			dst_match = !bacmp(&bt_sk(sk)->dst, dst);
1373c2287681SIdo Yariv 			if (src_match && dst_match) {
1374a7567b20SJohannes Berg 				read_unlock(&chan_list_lock);
137523691d75SGustavo F. Padovan 				return c;
137623691d75SGustavo F. Padovan 			}
13770a708f8fSGustavo F. Padovan 
13780a708f8fSGustavo F. Padovan 			/* Closest match */
1379c2287681SIdo Yariv 			src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY);
1380c2287681SIdo Yariv 			dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY);
1381c2287681SIdo Yariv 			if ((src_match && dst_any) || (src_any && dst_match) ||
1382c2287681SIdo Yariv 			    (src_any && dst_any))
138323691d75SGustavo F. Padovan 				c1 = c;
13840a708f8fSGustavo F. Padovan 		}
13850a708f8fSGustavo F. Padovan 	}
13860a708f8fSGustavo F. Padovan 
138723691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
13880a708f8fSGustavo F. Padovan 
138923691d75SGustavo F. Padovan 	return c1;
13900a708f8fSGustavo F. Padovan }
13910a708f8fSGustavo F. Padovan 
1392cbe8fed4SJohan Hedberg int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, bdaddr_t *dst)
13930a708f8fSGustavo F. Padovan {
13945d41ce1dSGustavo F. Padovan 	struct sock *sk = chan->sk;
13950a708f8fSGustavo F. Padovan 	bdaddr_t *src = &bt_sk(sk)->src;
13960a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
13970a708f8fSGustavo F. Padovan 	struct hci_conn *hcon;
13980a708f8fSGustavo F. Padovan 	struct hci_dev *hdev;
13990a708f8fSGustavo F. Padovan 	__u8 auth_type;
14000a708f8fSGustavo F. Padovan 	int err;
14010a708f8fSGustavo F. Padovan 
14020a708f8fSGustavo F. Padovan 	BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
1403097db76cSAndrei Emeltchenko 	       __le16_to_cpu(chan->psm));
14040a708f8fSGustavo F. Padovan 
14050a708f8fSGustavo F. Padovan 	hdev = hci_get_route(dst, src);
14060a708f8fSGustavo F. Padovan 	if (!hdev)
14070a708f8fSGustavo F. Padovan 		return -EHOSTUNREACH;
14080a708f8fSGustavo F. Padovan 
140909fd0de5SGustavo F. Padovan 	hci_dev_lock(hdev);
14100a708f8fSGustavo F. Padovan 
14116be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
141203a00194SGustavo F. Padovan 
141303a00194SGustavo F. Padovan 	/* PSM must be odd and lsb of upper byte must be 0 */
141403a00194SGustavo F. Padovan 	if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid &&
141503a00194SGustavo F. Padovan 					chan->chan_type != L2CAP_CHAN_RAW) {
141603a00194SGustavo F. Padovan 		err = -EINVAL;
141703a00194SGustavo F. Padovan 		goto done;
141803a00194SGustavo F. Padovan 	}
141903a00194SGustavo F. Padovan 
142003a00194SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) {
142103a00194SGustavo F. Padovan 		err = -EINVAL;
142203a00194SGustavo F. Padovan 		goto done;
142303a00194SGustavo F. Padovan 	}
142403a00194SGustavo F. Padovan 
142503a00194SGustavo F. Padovan 	switch (chan->mode) {
142603a00194SGustavo F. Padovan 	case L2CAP_MODE_BASIC:
142703a00194SGustavo F. Padovan 		break;
142803a00194SGustavo F. Padovan 	case L2CAP_MODE_ERTM:
142903a00194SGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
143003a00194SGustavo F. Padovan 		if (!disable_ertm)
143103a00194SGustavo F. Padovan 			break;
143203a00194SGustavo F. Padovan 		/* fall through */
143303a00194SGustavo F. Padovan 	default:
143403a00194SGustavo F. Padovan 		err = -ENOTSUPP;
143503a00194SGustavo F. Padovan 		goto done;
143603a00194SGustavo F. Padovan 	}
143703a00194SGustavo F. Padovan 
14386be36555SAndrei Emeltchenko 	lock_sock(sk);
14396be36555SAndrei Emeltchenko 
144003a00194SGustavo F. Padovan 	switch (sk->sk_state) {
144103a00194SGustavo F. Padovan 	case BT_CONNECT:
144203a00194SGustavo F. Padovan 	case BT_CONNECT2:
144303a00194SGustavo F. Padovan 	case BT_CONFIG:
144403a00194SGustavo F. Padovan 		/* Already connecting */
144503a00194SGustavo F. Padovan 		err = 0;
14466be36555SAndrei Emeltchenko 		release_sock(sk);
144703a00194SGustavo F. Padovan 		goto done;
144803a00194SGustavo F. Padovan 
144903a00194SGustavo F. Padovan 	case BT_CONNECTED:
145003a00194SGustavo F. Padovan 		/* Already connected */
145103a00194SGustavo F. Padovan 		err = -EISCONN;
14526be36555SAndrei Emeltchenko 		release_sock(sk);
145303a00194SGustavo F. Padovan 		goto done;
145403a00194SGustavo F. Padovan 
145503a00194SGustavo F. Padovan 	case BT_OPEN:
145603a00194SGustavo F. Padovan 	case BT_BOUND:
145703a00194SGustavo F. Padovan 		/* Can connect */
145803a00194SGustavo F. Padovan 		break;
145903a00194SGustavo F. Padovan 
146003a00194SGustavo F. Padovan 	default:
146103a00194SGustavo F. Padovan 		err = -EBADFD;
14626be36555SAndrei Emeltchenko 		release_sock(sk);
146303a00194SGustavo F. Padovan 		goto done;
146403a00194SGustavo F. Padovan 	}
146503a00194SGustavo F. Padovan 
146603a00194SGustavo F. Padovan 	/* Set destination address and psm */
14679219b2a0SGustavo F. Padovan 	bacpy(&bt_sk(sk)->dst, dst);
14686be36555SAndrei Emeltchenko 
14696be36555SAndrei Emeltchenko 	release_sock(sk);
14706be36555SAndrei Emeltchenko 
147103a00194SGustavo F. Padovan 	chan->psm = psm;
147203a00194SGustavo F. Padovan 	chan->dcid = cid;
14730a708f8fSGustavo F. Padovan 
14744343478fSGustavo F. Padovan 	auth_type = l2cap_get_auth_type(chan);
14750a708f8fSGustavo F. Padovan 
1476fe4128e0SGustavo F. Padovan 	if (chan->dcid == L2CAP_CID_LE_DATA)
1477acd7d370SVille Tervo 		hcon = hci_connect(hdev, LE_LINK, dst,
14784343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1479acd7d370SVille Tervo 	else
14800a708f8fSGustavo F. Padovan 		hcon = hci_connect(hdev, ACL_LINK, dst,
14814343478fSGustavo F. Padovan 					chan->sec_level, auth_type);
1482acd7d370SVille Tervo 
148330e76272SVille Tervo 	if (IS_ERR(hcon)) {
148430e76272SVille Tervo 		err = PTR_ERR(hcon);
14850a708f8fSGustavo F. Padovan 		goto done;
148630e76272SVille Tervo 	}
14870a708f8fSGustavo F. Padovan 
14880a708f8fSGustavo F. Padovan 	conn = l2cap_conn_add(hcon, 0);
14890a708f8fSGustavo F. Padovan 	if (!conn) {
14900a708f8fSGustavo F. Padovan 		hci_conn_put(hcon);
149130e76272SVille Tervo 		err = -ENOMEM;
14920a708f8fSGustavo F. Padovan 		goto done;
14930a708f8fSGustavo F. Padovan 	}
14940a708f8fSGustavo F. Padovan 
14950a708f8fSGustavo F. Padovan 	/* Update source addr of the socket */
14960a708f8fSGustavo F. Padovan 	bacpy(src, conn->src);
14970a708f8fSGustavo F. Padovan 
14986be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
149948454079SGustavo F. Padovan 	l2cap_chan_add(conn, chan);
15006be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
150148454079SGustavo F. Padovan 
15026be36555SAndrei Emeltchenko 	l2cap_state_change(chan, BT_CONNECT);
1503c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
15040a708f8fSGustavo F. Padovan 
15050a708f8fSGustavo F. Padovan 	if (hcon->state == BT_CONNECTED) {
1506715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1507c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
1508d45fc423SGustavo F. Padovan 			if (l2cap_chan_check_security(chan))
15096be36555SAndrei Emeltchenko 				l2cap_state_change(chan, BT_CONNECTED);
15100a708f8fSGustavo F. Padovan 		} else
1511fc7f8a7eSGustavo F. Padovan 			l2cap_do_start(chan);
15120a708f8fSGustavo F. Padovan 	}
15130a708f8fSGustavo F. Padovan 
151430e76272SVille Tervo 	err = 0;
151530e76272SVille Tervo 
15160a708f8fSGustavo F. Padovan done:
15176be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
151809fd0de5SGustavo F. Padovan 	hci_dev_unlock(hdev);
15190a708f8fSGustavo F. Padovan 	hci_dev_put(hdev);
15200a708f8fSGustavo F. Padovan 	return err;
15210a708f8fSGustavo F. Padovan }
15220a708f8fSGustavo F. Padovan 
1523dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk)
15240a708f8fSGustavo F. Padovan {
15258c1d787bSGustavo F. Padovan 	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
15260a708f8fSGustavo F. Padovan 	DECLARE_WAITQUEUE(wait, current);
15270a708f8fSGustavo F. Padovan 	int err = 0;
15280a708f8fSGustavo F. Padovan 	int timeo = HZ/5;
15290a708f8fSGustavo F. Padovan 
15300a708f8fSGustavo F. Padovan 	add_wait_queue(sk_sleep(sk), &wait);
15310a708f8fSGustavo F. Padovan 	set_current_state(TASK_INTERRUPTIBLE);
1532a71a0cf4SPeter Hurley 	while (chan->unacked_frames > 0 && chan->conn) {
15330a708f8fSGustavo F. Padovan 		if (!timeo)
15340a708f8fSGustavo F. Padovan 			timeo = HZ/5;
15350a708f8fSGustavo F. Padovan 
15360a708f8fSGustavo F. Padovan 		if (signal_pending(current)) {
15370a708f8fSGustavo F. Padovan 			err = sock_intr_errno(timeo);
15380a708f8fSGustavo F. Padovan 			break;
15390a708f8fSGustavo F. Padovan 		}
15400a708f8fSGustavo F. Padovan 
15410a708f8fSGustavo F. Padovan 		release_sock(sk);
15420a708f8fSGustavo F. Padovan 		timeo = schedule_timeout(timeo);
15430a708f8fSGustavo F. Padovan 		lock_sock(sk);
1544a71a0cf4SPeter Hurley 		set_current_state(TASK_INTERRUPTIBLE);
15450a708f8fSGustavo F. Padovan 
15460a708f8fSGustavo F. Padovan 		err = sock_error(sk);
15470a708f8fSGustavo F. Padovan 		if (err)
15480a708f8fSGustavo F. Padovan 			break;
15490a708f8fSGustavo F. Padovan 	}
15500a708f8fSGustavo F. Padovan 	set_current_state(TASK_RUNNING);
15510a708f8fSGustavo F. Padovan 	remove_wait_queue(sk_sleep(sk), &wait);
15520a708f8fSGustavo F. Padovan 	return err;
15530a708f8fSGustavo F. Padovan }
15540a708f8fSGustavo F. Padovan 
1555721c4181SGustavo F. Padovan static void l2cap_monitor_timeout(struct work_struct *work)
15560a708f8fSGustavo F. Padovan {
1557721c4181SGustavo F. Padovan 	struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
1558721c4181SGustavo F. Padovan 							monitor_timer.work);
15590a708f8fSGustavo F. Padovan 
1560525cd185SGustavo F. Padovan 	BT_DBG("chan %p", chan);
15610a708f8fSGustavo F. Padovan 
15626be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
15636be36555SAndrei Emeltchenko 
15642c03a7a4SGustavo F. Padovan 	if (chan->retry_count >= chan->remote_max_tx) {
15658c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
15666be36555SAndrei Emeltchenko 		l2cap_chan_unlock(chan);
15678d7e1c7fSAndrei Emeltchenko 		l2cap_chan_put(chan);
15680a708f8fSGustavo F. Padovan 		return;
15690a708f8fSGustavo F. Padovan 	}
15700a708f8fSGustavo F. Padovan 
15716a026610SGustavo F. Padovan 	chan->retry_count++;
15721a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
15730a708f8fSGustavo F. Padovan 
1574525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
15756be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
15768d7e1c7fSAndrei Emeltchenko 	l2cap_chan_put(chan);
15770a708f8fSGustavo F. Padovan }
15780a708f8fSGustavo F. Padovan 
1579721c4181SGustavo F. Padovan static void l2cap_retrans_timeout(struct work_struct *work)
15800a708f8fSGustavo F. Padovan {
1581721c4181SGustavo F. Padovan 	struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
1582721c4181SGustavo F. Padovan 							retrans_timer.work);
15830a708f8fSGustavo F. Padovan 
158449208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
15850a708f8fSGustavo F. Padovan 
15866be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
15876be36555SAndrei Emeltchenko 
15886a026610SGustavo F. Padovan 	chan->retry_count = 1;
15891a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
15900a708f8fSGustavo F. Padovan 
1591e2ab4353SGustavo F. Padovan 	set_bit(CONN_WAIT_F, &chan->conn_state);
15920a708f8fSGustavo F. Padovan 
1593525cd185SGustavo F. Padovan 	l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL);
15946be36555SAndrei Emeltchenko 
15956be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
15968d7e1c7fSAndrei Emeltchenko 	l2cap_chan_put(chan);
15970a708f8fSGustavo F. Padovan }
15980a708f8fSGustavo F. Padovan 
159942e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan)
16000a708f8fSGustavo F. Padovan {
16010a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
16020a708f8fSGustavo F. Padovan 
160358d35f87SGustavo F. Padovan 	while ((skb = skb_peek(&chan->tx_q)) &&
16046a026610SGustavo F. Padovan 			chan->unacked_frames) {
160542e5c802SGustavo F. Padovan 		if (bt_cb(skb)->tx_seq == chan->expected_ack_seq)
16060a708f8fSGustavo F. Padovan 			break;
16070a708f8fSGustavo F. Padovan 
160858d35f87SGustavo F. Padovan 		skb = skb_dequeue(&chan->tx_q);
16090a708f8fSGustavo F. Padovan 		kfree_skb(skb);
16100a708f8fSGustavo F. Padovan 
16116a026610SGustavo F. Padovan 		chan->unacked_frames--;
16120a708f8fSGustavo F. Padovan 	}
16130a708f8fSGustavo F. Padovan 
16146a026610SGustavo F. Padovan 	if (!chan->unacked_frames)
16151a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
16160a708f8fSGustavo F. Padovan }
16170a708f8fSGustavo F. Padovan 
161867c9e840SSzymon Janc static void l2cap_streaming_send(struct l2cap_chan *chan)
16190a708f8fSGustavo F. Padovan {
16200a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
162188843ab0SAndrei Emeltchenko 	u32 control;
162288843ab0SAndrei Emeltchenko 	u16 fcs;
16230a708f8fSGustavo F. Padovan 
162458d35f87SGustavo F. Padovan 	while ((skb = skb_dequeue(&chan->tx_q))) {
162588843ab0SAndrei Emeltchenko 		control = __get_control(chan, skb->data + L2CAP_HDR_SIZE);
1626fb45de7dSAndrei Emeltchenko 		control |= __set_txseq(chan, chan->next_tx_seq);
162788843ab0SAndrei Emeltchenko 		__put_control(chan, control, skb->data + L2CAP_HDR_SIZE);
16280a708f8fSGustavo F. Padovan 
162947d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
163003a51213SAndrei Emeltchenko 			fcs = crc16(0, (u8 *)skb->data,
163103a51213SAndrei Emeltchenko 						skb->len - L2CAP_FCS_SIZE);
163203a51213SAndrei Emeltchenko 			put_unaligned_le16(fcs,
163303a51213SAndrei Emeltchenko 					skb->data + skb->len - L2CAP_FCS_SIZE);
16340a708f8fSGustavo F. Padovan 		}
16350a708f8fSGustavo F. Padovan 
16364343478fSGustavo F. Padovan 		l2cap_do_send(chan, skb);
16370a708f8fSGustavo F. Padovan 
1638836be934SAndrei Emeltchenko 		chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
16390a708f8fSGustavo F. Padovan 	}
16400a708f8fSGustavo F. Padovan }
16410a708f8fSGustavo F. Padovan 
1642fb45de7dSAndrei Emeltchenko static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq)
16430a708f8fSGustavo F. Padovan {
16440a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
164588843ab0SAndrei Emeltchenko 	u16 fcs;
164688843ab0SAndrei Emeltchenko 	u32 control;
16470a708f8fSGustavo F. Padovan 
164858d35f87SGustavo F. Padovan 	skb = skb_peek(&chan->tx_q);
16490a708f8fSGustavo F. Padovan 	if (!skb)
16500a708f8fSGustavo F. Padovan 		return;
16510a708f8fSGustavo F. Padovan 
1652d1726b6dSSzymon Janc 	while (bt_cb(skb)->tx_seq != tx_seq) {
165358d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
16540a708f8fSGustavo F. Padovan 			return;
16550a708f8fSGustavo F. Padovan 
1656d1726b6dSSzymon Janc 		skb = skb_queue_next(&chan->tx_q, skb);
1657d1726b6dSSzymon Janc 	}
16580a708f8fSGustavo F. Padovan 
16592c03a7a4SGustavo F. Padovan 	if (chan->remote_max_tx &&
16602c03a7a4SGustavo F. Padovan 			bt_cb(skb)->retries == chan->remote_max_tx) {
16618c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
16620a708f8fSGustavo F. Padovan 		return;
16630a708f8fSGustavo F. Padovan 	}
16640a708f8fSGustavo F. Padovan 
16650a708f8fSGustavo F. Padovan 	tx_skb = skb_clone(skb, GFP_ATOMIC);
16660a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries++;
166788843ab0SAndrei Emeltchenko 
166888843ab0SAndrei Emeltchenko 	control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE);
16697e0ef6eeSAndrei Emeltchenko 	control &= __get_sar_mask(chan);
16700a708f8fSGustavo F. Padovan 
1671e2ab4353SGustavo F. Padovan 	if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
167203f6715dSAndrei Emeltchenko 		control |= __set_ctrl_final(chan);
16730a708f8fSGustavo F. Padovan 
16740b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
1675fb45de7dSAndrei Emeltchenko 	control |= __set_txseq(chan, tx_seq);
16760a708f8fSGustavo F. Padovan 
167788843ab0SAndrei Emeltchenko 	__put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE);
16780a708f8fSGustavo F. Padovan 
167947d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
168003a51213SAndrei Emeltchenko 		fcs = crc16(0, (u8 *)tx_skb->data,
168103a51213SAndrei Emeltchenko 						tx_skb->len - L2CAP_FCS_SIZE);
168203a51213SAndrei Emeltchenko 		put_unaligned_le16(fcs,
168303a51213SAndrei Emeltchenko 				tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE);
16840a708f8fSGustavo F. Padovan 	}
16850a708f8fSGustavo F. Padovan 
16864343478fSGustavo F. Padovan 	l2cap_do_send(chan, tx_skb);
16870a708f8fSGustavo F. Padovan }
16880a708f8fSGustavo F. Padovan 
168967c9e840SSzymon Janc static int l2cap_ertm_send(struct l2cap_chan *chan)
16900a708f8fSGustavo F. Padovan {
16910a708f8fSGustavo F. Padovan 	struct sk_buff *skb, *tx_skb;
169288843ab0SAndrei Emeltchenko 	u16 fcs;
169388843ab0SAndrei Emeltchenko 	u32 control;
16940a708f8fSGustavo F. Padovan 	int nsent = 0;
16950a708f8fSGustavo F. Padovan 
169689bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
16970a708f8fSGustavo F. Padovan 		return -ENOTCONN;
16980a708f8fSGustavo F. Padovan 
169958d35f87SGustavo F. Padovan 	while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) {
17000a708f8fSGustavo F. Padovan 
17012c03a7a4SGustavo F. Padovan 		if (chan->remote_max_tx &&
17022c03a7a4SGustavo F. Padovan 				bt_cb(skb)->retries == chan->remote_max_tx) {
17038c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED);
17040a708f8fSGustavo F. Padovan 			break;
17050a708f8fSGustavo F. Padovan 		}
17060a708f8fSGustavo F. Padovan 
17070a708f8fSGustavo F. Padovan 		tx_skb = skb_clone(skb, GFP_ATOMIC);
17080a708f8fSGustavo F. Padovan 
17090a708f8fSGustavo F. Padovan 		bt_cb(skb)->retries++;
17100a708f8fSGustavo F. Padovan 
171188843ab0SAndrei Emeltchenko 		control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE);
17127e0ef6eeSAndrei Emeltchenko 		control &= __get_sar_mask(chan);
17130a708f8fSGustavo F. Padovan 
1714e2ab4353SGustavo F. Padovan 		if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
171503f6715dSAndrei Emeltchenko 			control |= __set_ctrl_final(chan);
1716e2ab4353SGustavo F. Padovan 
17170b209faeSAndrei Emeltchenko 		control |= __set_reqseq(chan, chan->buffer_seq);
1718fb45de7dSAndrei Emeltchenko 		control |= __set_txseq(chan, chan->next_tx_seq);
17190a708f8fSGustavo F. Padovan 
172088843ab0SAndrei Emeltchenko 		__put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE);
17210a708f8fSGustavo F. Padovan 
172247d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16) {
172303a51213SAndrei Emeltchenko 			fcs = crc16(0, (u8 *)skb->data,
172403a51213SAndrei Emeltchenko 						tx_skb->len - L2CAP_FCS_SIZE);
172503a51213SAndrei Emeltchenko 			put_unaligned_le16(fcs, skb->data +
172603a51213SAndrei Emeltchenko 						tx_skb->len - L2CAP_FCS_SIZE);
17270a708f8fSGustavo F. Padovan 		}
17280a708f8fSGustavo F. Padovan 
17294343478fSGustavo F. Padovan 		l2cap_do_send(chan, tx_skb);
17300a708f8fSGustavo F. Padovan 
17311a09bcb9SGustavo F. Padovan 		__set_retrans_timer(chan);
17320a708f8fSGustavo F. Padovan 
173342e5c802SGustavo F. Padovan 		bt_cb(skb)->tx_seq = chan->next_tx_seq;
1734836be934SAndrei Emeltchenko 
1735836be934SAndrei Emeltchenko 		chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
17360a708f8fSGustavo F. Padovan 
17378ed7a0aeSSzymon Janc 		if (bt_cb(skb)->retries == 1) {
17386a026610SGustavo F. Padovan 			chan->unacked_frames++;
1739930fa4aeSSzymon Janc 
1740930fa4aeSSzymon Janc 			if (!nsent++)
1741930fa4aeSSzymon Janc 				__clear_ack_timer(chan);
17428ed7a0aeSSzymon Janc 		}
174323e9fde2SSuraj Sumangala 
17446a026610SGustavo F. Padovan 		chan->frames_sent++;
17450a708f8fSGustavo F. Padovan 
174658d35f87SGustavo F. Padovan 		if (skb_queue_is_last(&chan->tx_q, skb))
174758d35f87SGustavo F. Padovan 			chan->tx_send_head = NULL;
17480a708f8fSGustavo F. Padovan 		else
174958d35f87SGustavo F. Padovan 			chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
17500a708f8fSGustavo F. Padovan 	}
17510a708f8fSGustavo F. Padovan 
17520a708f8fSGustavo F. Padovan 	return nsent;
17530a708f8fSGustavo F. Padovan }
17540a708f8fSGustavo F. Padovan 
1755525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan)
17560a708f8fSGustavo F. Padovan {
17570a708f8fSGustavo F. Padovan 	int ret;
17580a708f8fSGustavo F. Padovan 
175958d35f87SGustavo F. Padovan 	if (!skb_queue_empty(&chan->tx_q))
176058d35f87SGustavo F. Padovan 		chan->tx_send_head = chan->tx_q.next;
17610a708f8fSGustavo F. Padovan 
176242e5c802SGustavo F. Padovan 	chan->next_tx_seq = chan->expected_ack_seq;
1763525cd185SGustavo F. Padovan 	ret = l2cap_ertm_send(chan);
17640a708f8fSGustavo F. Padovan 	return ret;
17650a708f8fSGustavo F. Padovan }
17660a708f8fSGustavo F. Padovan 
1767b17e73bbSSzymon Janc static void __l2cap_send_ack(struct l2cap_chan *chan)
17680a708f8fSGustavo F. Padovan {
176988843ab0SAndrei Emeltchenko 	u32 control = 0;
17700a708f8fSGustavo F. Padovan 
17710b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
17720a708f8fSGustavo F. Padovan 
1773e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
1774ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
1775e2ab4353SGustavo F. Padovan 		set_bit(CONN_RNR_SENT, &chan->conn_state);
1776525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
17770a708f8fSGustavo F. Padovan 		return;
17780a708f8fSGustavo F. Padovan 	}
17790a708f8fSGustavo F. Padovan 
1780525cd185SGustavo F. Padovan 	if (l2cap_ertm_send(chan) > 0)
17810a708f8fSGustavo F. Padovan 		return;
17820a708f8fSGustavo F. Padovan 
1783ab784b73SAndrei Emeltchenko 	control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
1784525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
17850a708f8fSGustavo F. Padovan }
17860a708f8fSGustavo F. Padovan 
1787b17e73bbSSzymon Janc static void l2cap_send_ack(struct l2cap_chan *chan)
1788b17e73bbSSzymon Janc {
1789b17e73bbSSzymon Janc 	__clear_ack_timer(chan);
1790b17e73bbSSzymon Janc 	__l2cap_send_ack(chan);
1791b17e73bbSSzymon Janc }
1792b17e73bbSSzymon Janc 
1793525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan)
17940a708f8fSGustavo F. Padovan {
17950a708f8fSGustavo F. Padovan 	struct srej_list *tail;
179688843ab0SAndrei Emeltchenko 	u32 control;
17970a708f8fSGustavo F. Padovan 
1798ab784b73SAndrei Emeltchenko 	control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
179903f6715dSAndrei Emeltchenko 	control |= __set_ctrl_final(chan);
18000a708f8fSGustavo F. Padovan 
180139d5a3eeSGustavo F. Padovan 	tail = list_entry((&chan->srej_l)->prev, struct srej_list, list);
18020b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, tail->tx_seq);
18030a708f8fSGustavo F. Padovan 
1804525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
18050a708f8fSGustavo F. Padovan }
18060a708f8fSGustavo F. Padovan 
180704124681SGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan,
180804124681SGustavo F. Padovan 					 struct msghdr *msg, int len,
180904124681SGustavo F. Padovan 					 int count, struct sk_buff *skb)
18100a708f8fSGustavo F. Padovan {
18110952a57aSAndrei Emeltchenko 	struct l2cap_conn *conn = chan->conn;
18120a708f8fSGustavo F. Padovan 	struct sk_buff **frag;
181390338947SGustavo Padovan 	int sent = 0;
18140a708f8fSGustavo F. Padovan 
18150a708f8fSGustavo F. Padovan 	if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
18160a708f8fSGustavo F. Padovan 		return -EFAULT;
18170a708f8fSGustavo F. Padovan 
18180a708f8fSGustavo F. Padovan 	sent += count;
18190a708f8fSGustavo F. Padovan 	len  -= count;
18200a708f8fSGustavo F. Padovan 
18210a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
18220a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
18230a708f8fSGustavo F. Padovan 	while (len) {
18240a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
18250a708f8fSGustavo F. Padovan 
18262f7719ceSAndrei Emeltchenko 		*frag = chan->ops->alloc_skb(chan, count,
182790338947SGustavo Padovan 					     msg->msg_flags & MSG_DONTWAIT);
18282f7719ceSAndrei Emeltchenko 
182990338947SGustavo Padovan 		if (IS_ERR(*frag))
183090338947SGustavo Padovan 			return PTR_ERR(*frag);
18310a708f8fSGustavo F. Padovan 		if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
18320a708f8fSGustavo F. Padovan 			return -EFAULT;
18330a708f8fSGustavo F. Padovan 
18345e59b791SLuiz Augusto von Dentz 		(*frag)->priority = skb->priority;
18355e59b791SLuiz Augusto von Dentz 
18360a708f8fSGustavo F. Padovan 		sent += count;
18370a708f8fSGustavo F. Padovan 		len  -= count;
18380a708f8fSGustavo F. Padovan 
18390a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
18400a708f8fSGustavo F. Padovan 	}
18410a708f8fSGustavo F. Padovan 
18420a708f8fSGustavo F. Padovan 	return sent;
18430a708f8fSGustavo F. Padovan }
18440a708f8fSGustavo F. Padovan 
18455e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
18465e59b791SLuiz Augusto von Dentz 						struct msghdr *msg, size_t len,
18475e59b791SLuiz Augusto von Dentz 						u32 priority)
18480a708f8fSGustavo F. Padovan {
18498c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
18500a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
185103a51213SAndrei Emeltchenko 	int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
18520a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
18530a708f8fSGustavo F. Padovan 
18546d5922b0SAndrei Emeltchenko 	BT_DBG("chan %p len %d priority %u", chan, (int)len, priority);
18550a708f8fSGustavo F. Padovan 
18560a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
18572f7719ceSAndrei Emeltchenko 
18582f7719ceSAndrei Emeltchenko 	skb = chan->ops->alloc_skb(chan, count + hlen,
185990338947SGustavo Padovan 				   msg->msg_flags & MSG_DONTWAIT);
186090338947SGustavo Padovan 	if (IS_ERR(skb))
186190338947SGustavo Padovan 		return skb;
18620a708f8fSGustavo F. Padovan 
18635e59b791SLuiz Augusto von Dentz 	skb->priority = priority;
18645e59b791SLuiz Augusto von Dentz 
18650a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
18660a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1867fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
18680a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
1869097db76cSAndrei Emeltchenko 	put_unaligned(chan->psm, skb_put(skb, 2));
18700a708f8fSGustavo F. Padovan 
18710952a57aSAndrei Emeltchenko 	err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
18720a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
18730a708f8fSGustavo F. Padovan 		kfree_skb(skb);
18740a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
18750a708f8fSGustavo F. Padovan 	}
18760a708f8fSGustavo F. Padovan 	return skb;
18770a708f8fSGustavo F. Padovan }
18780a708f8fSGustavo F. Padovan 
18795e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
18805e59b791SLuiz Augusto von Dentz 						struct msghdr *msg, size_t len,
18815e59b791SLuiz Augusto von Dentz 						u32 priority)
18820a708f8fSGustavo F. Padovan {
18838c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
18840a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
18850a708f8fSGustavo F. Padovan 	int err, count, hlen = L2CAP_HDR_SIZE;
18860a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
18870a708f8fSGustavo F. Padovan 
18886d5922b0SAndrei Emeltchenko 	BT_DBG("chan %p len %d", chan, (int)len);
18890a708f8fSGustavo F. Padovan 
18900a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
18912f7719ceSAndrei Emeltchenko 
18922f7719ceSAndrei Emeltchenko 	skb = chan->ops->alloc_skb(chan, count + hlen,
189390338947SGustavo Padovan 				   msg->msg_flags & MSG_DONTWAIT);
189490338947SGustavo Padovan 	if (IS_ERR(skb))
189590338947SGustavo Padovan 		return skb;
18960a708f8fSGustavo F. Padovan 
18975e59b791SLuiz Augusto von Dentz 	skb->priority = priority;
18985e59b791SLuiz Augusto von Dentz 
18990a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
19000a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1901fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
19020a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
19030a708f8fSGustavo F. Padovan 
19040952a57aSAndrei Emeltchenko 	err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
19050a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
19060a708f8fSGustavo F. Padovan 		kfree_skb(skb);
19070a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
19080a708f8fSGustavo F. Padovan 	}
19090a708f8fSGustavo F. Padovan 	return skb;
19100a708f8fSGustavo F. Padovan }
19110a708f8fSGustavo F. Padovan 
1912ab0ff76dSLuiz Augusto von Dentz static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
1913ab0ff76dSLuiz Augusto von Dentz 						struct msghdr *msg, size_t len,
191488843ab0SAndrei Emeltchenko 						u32 control, u16 sdulen)
19150a708f8fSGustavo F. Padovan {
19168c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
19170a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
1918e4ca6d98SAndrei Emeltchenko 	int err, count, hlen;
19190a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
19200a708f8fSGustavo F. Padovan 
19216d5922b0SAndrei Emeltchenko 	BT_DBG("chan %p len %d", chan, (int)len);
19220a708f8fSGustavo F. Padovan 
19230a708f8fSGustavo F. Padovan 	if (!conn)
19240a708f8fSGustavo F. Padovan 		return ERR_PTR(-ENOTCONN);
19250a708f8fSGustavo F. Padovan 
1926e4ca6d98SAndrei Emeltchenko 	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1927e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_EXT_HDR_SIZE;
1928e4ca6d98SAndrei Emeltchenko 	else
1929e4ca6d98SAndrei Emeltchenko 		hlen = L2CAP_ENH_HDR_SIZE;
1930e4ca6d98SAndrei Emeltchenko 
19310a708f8fSGustavo F. Padovan 	if (sdulen)
193203a51213SAndrei Emeltchenko 		hlen += L2CAP_SDULEN_SIZE;
19330a708f8fSGustavo F. Padovan 
193447d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
193503a51213SAndrei Emeltchenko 		hlen += L2CAP_FCS_SIZE;
19360a708f8fSGustavo F. Padovan 
19370a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, (conn->mtu - hlen), len);
19382f7719ceSAndrei Emeltchenko 
19392f7719ceSAndrei Emeltchenko 	skb = chan->ops->alloc_skb(chan, count + hlen,
194090338947SGustavo Padovan 				   msg->msg_flags & MSG_DONTWAIT);
194190338947SGustavo Padovan 	if (IS_ERR(skb))
194290338947SGustavo Padovan 		return skb;
19430a708f8fSGustavo F. Padovan 
19440a708f8fSGustavo F. Padovan 	/* Create L2CAP header */
19450a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
1946fe4128e0SGustavo F. Padovan 	lh->cid = cpu_to_le16(chan->dcid);
19470a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
194888843ab0SAndrei Emeltchenko 
194988843ab0SAndrei Emeltchenko 	__put_control(chan, control, skb_put(skb, __ctrl_size(chan)));
195088843ab0SAndrei Emeltchenko 
19510a708f8fSGustavo F. Padovan 	if (sdulen)
195203a51213SAndrei Emeltchenko 		put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
19530a708f8fSGustavo F. Padovan 
19540952a57aSAndrei Emeltchenko 	err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
19550a708f8fSGustavo F. Padovan 	if (unlikely(err < 0)) {
19560a708f8fSGustavo F. Padovan 		kfree_skb(skb);
19570a708f8fSGustavo F. Padovan 		return ERR_PTR(err);
19580a708f8fSGustavo F. Padovan 	}
19590a708f8fSGustavo F. Padovan 
196047d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
196103a51213SAndrei Emeltchenko 		put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE));
19620a708f8fSGustavo F. Padovan 
19630a708f8fSGustavo F. Padovan 	bt_cb(skb)->retries = 0;
19640a708f8fSGustavo F. Padovan 	return skb;
19650a708f8fSGustavo F. Padovan }
19660a708f8fSGustavo F. Padovan 
196767c9e840SSzymon Janc static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
19680a708f8fSGustavo F. Padovan {
19690a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
19700a708f8fSGustavo F. Padovan 	struct sk_buff_head sar_queue;
197188843ab0SAndrei Emeltchenko 	u32 control;
19720a708f8fSGustavo F. Padovan 	size_t size = 0;
19730a708f8fSGustavo F. Padovan 
19740a708f8fSGustavo F. Padovan 	skb_queue_head_init(&sar_queue);
19757e0ef6eeSAndrei Emeltchenko 	control = __set_ctrl_sar(chan, L2CAP_SAR_START);
197647d1ec61SGustavo F. Padovan 	skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len);
19770a708f8fSGustavo F. Padovan 	if (IS_ERR(skb))
19780a708f8fSGustavo F. Padovan 		return PTR_ERR(skb);
19790a708f8fSGustavo F. Padovan 
19800a708f8fSGustavo F. Padovan 	__skb_queue_tail(&sar_queue, skb);
19812c03a7a4SGustavo F. Padovan 	len -= chan->remote_mps;
19822c03a7a4SGustavo F. Padovan 	size += chan->remote_mps;
19830a708f8fSGustavo F. Padovan 
19840a708f8fSGustavo F. Padovan 	while (len > 0) {
19850a708f8fSGustavo F. Padovan 		size_t buflen;
19860a708f8fSGustavo F. Padovan 
19872c03a7a4SGustavo F. Padovan 		if (len > chan->remote_mps) {
19887e0ef6eeSAndrei Emeltchenko 			control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE);
19892c03a7a4SGustavo F. Padovan 			buflen = chan->remote_mps;
19900a708f8fSGustavo F. Padovan 		} else {
19917e0ef6eeSAndrei Emeltchenko 			control = __set_ctrl_sar(chan, L2CAP_SAR_END);
19920a708f8fSGustavo F. Padovan 			buflen = len;
19930a708f8fSGustavo F. Padovan 		}
19940a708f8fSGustavo F. Padovan 
199547d1ec61SGustavo F. Padovan 		skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0);
19960a708f8fSGustavo F. Padovan 		if (IS_ERR(skb)) {
19970a708f8fSGustavo F. Padovan 			skb_queue_purge(&sar_queue);
19980a708f8fSGustavo F. Padovan 			return PTR_ERR(skb);
19990a708f8fSGustavo F. Padovan 		}
20000a708f8fSGustavo F. Padovan 
20010a708f8fSGustavo F. Padovan 		__skb_queue_tail(&sar_queue, skb);
20020a708f8fSGustavo F. Padovan 		len -= buflen;
20030a708f8fSGustavo F. Padovan 		size += buflen;
20040a708f8fSGustavo F. Padovan 	}
200558d35f87SGustavo F. Padovan 	skb_queue_splice_tail(&sar_queue, &chan->tx_q);
200658d35f87SGustavo F. Padovan 	if (chan->tx_send_head == NULL)
200758d35f87SGustavo F. Padovan 		chan->tx_send_head = sar_queue.next;
20080a708f8fSGustavo F. Padovan 
20090a708f8fSGustavo F. Padovan 	return size;
20100a708f8fSGustavo F. Padovan }
20110a708f8fSGustavo F. Padovan 
20125e59b791SLuiz Augusto von Dentz int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
20135e59b791SLuiz Augusto von Dentz 								u32 priority)
20149a91a04aSGustavo F. Padovan {
20159a91a04aSGustavo F. Padovan 	struct sk_buff *skb;
201688843ab0SAndrei Emeltchenko 	u32 control;
20179a91a04aSGustavo F. Padovan 	int err;
20189a91a04aSGustavo F. Padovan 
20199a91a04aSGustavo F. Padovan 	/* Connectionless channel */
2020715ec005SGustavo F. Padovan 	if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
20215e59b791SLuiz Augusto von Dentz 		skb = l2cap_create_connless_pdu(chan, msg, len, priority);
20229a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
20239a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
20249a91a04aSGustavo F. Padovan 
20259a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
20269a91a04aSGustavo F. Padovan 		return len;
20279a91a04aSGustavo F. Padovan 	}
20289a91a04aSGustavo F. Padovan 
20299a91a04aSGustavo F. Padovan 	switch (chan->mode) {
20309a91a04aSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
20319a91a04aSGustavo F. Padovan 		/* Check outgoing MTU */
20329a91a04aSGustavo F. Padovan 		if (len > chan->omtu)
20339a91a04aSGustavo F. Padovan 			return -EMSGSIZE;
20349a91a04aSGustavo F. Padovan 
20359a91a04aSGustavo F. Padovan 		/* Create a basic PDU */
20365e59b791SLuiz Augusto von Dentz 		skb = l2cap_create_basic_pdu(chan, msg, len, priority);
20379a91a04aSGustavo F. Padovan 		if (IS_ERR(skb))
20389a91a04aSGustavo F. Padovan 			return PTR_ERR(skb);
20399a91a04aSGustavo F. Padovan 
20409a91a04aSGustavo F. Padovan 		l2cap_do_send(chan, skb);
20419a91a04aSGustavo F. Padovan 		err = len;
20429a91a04aSGustavo F. Padovan 		break;
20439a91a04aSGustavo F. Padovan 
20449a91a04aSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
20459a91a04aSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
20469a91a04aSGustavo F. Padovan 		/* Entire SDU fits into one PDU */
20479a91a04aSGustavo F. Padovan 		if (len <= chan->remote_mps) {
20487e0ef6eeSAndrei Emeltchenko 			control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED);
20499a91a04aSGustavo F. Padovan 			skb = l2cap_create_iframe_pdu(chan, msg, len, control,
20509a91a04aSGustavo F. Padovan 									0);
20519a91a04aSGustavo F. Padovan 			if (IS_ERR(skb))
20529a91a04aSGustavo F. Padovan 				return PTR_ERR(skb);
20539a91a04aSGustavo F. Padovan 
20549a91a04aSGustavo F. Padovan 			__skb_queue_tail(&chan->tx_q, skb);
20559a91a04aSGustavo F. Padovan 
20569a91a04aSGustavo F. Padovan 			if (chan->tx_send_head == NULL)
20579a91a04aSGustavo F. Padovan 				chan->tx_send_head = skb;
20589a91a04aSGustavo F. Padovan 
20599a91a04aSGustavo F. Padovan 		} else {
20609a91a04aSGustavo F. Padovan 			/* Segment SDU into multiples PDUs */
20619a91a04aSGustavo F. Padovan 			err = l2cap_sar_segment_sdu(chan, msg, len);
20629a91a04aSGustavo F. Padovan 			if (err < 0)
20639a91a04aSGustavo F. Padovan 				return err;
20649a91a04aSGustavo F. Padovan 		}
20659a91a04aSGustavo F. Padovan 
20669a91a04aSGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_STREAMING) {
20679a91a04aSGustavo F. Padovan 			l2cap_streaming_send(chan);
20689a91a04aSGustavo F. Padovan 			err = len;
20699a91a04aSGustavo F. Padovan 			break;
20709a91a04aSGustavo F. Padovan 		}
20719a91a04aSGustavo F. Padovan 
2072e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
2073e2ab4353SGustavo F. Padovan 				test_bit(CONN_WAIT_F, &chan->conn_state)) {
20749a91a04aSGustavo F. Padovan 			err = len;
20759a91a04aSGustavo F. Padovan 			break;
20769a91a04aSGustavo F. Padovan 		}
20779a91a04aSGustavo F. Padovan 
20789a91a04aSGustavo F. Padovan 		err = l2cap_ertm_send(chan);
20799a91a04aSGustavo F. Padovan 		if (err >= 0)
20809a91a04aSGustavo F. Padovan 			err = len;
20819a91a04aSGustavo F. Padovan 
20829a91a04aSGustavo F. Padovan 		break;
20839a91a04aSGustavo F. Padovan 
20849a91a04aSGustavo F. Padovan 	default:
20859a91a04aSGustavo F. Padovan 		BT_DBG("bad state %1.1x", chan->mode);
20869a91a04aSGustavo F. Padovan 		err = -EBADFD;
20879a91a04aSGustavo F. Padovan 	}
20889a91a04aSGustavo F. Padovan 
20899a91a04aSGustavo F. Padovan 	return err;
20909a91a04aSGustavo F. Padovan }
20919a91a04aSGustavo F. Padovan 
20920a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */
20930a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
20940a708f8fSGustavo F. Padovan {
20950a708f8fSGustavo F. Padovan 	struct sk_buff *nskb;
209648454079SGustavo F. Padovan 	struct l2cap_chan *chan;
20970a708f8fSGustavo F. Padovan 
20980a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
20990a708f8fSGustavo F. Padovan 
21003df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
21013d57dc68SGustavo F. Padovan 
21023df91ea2SAndrei Emeltchenko 	list_for_each_entry(chan, &conn->chan_l, list) {
210348454079SGustavo F. Padovan 		struct sock *sk = chan->sk;
2104715ec005SGustavo F. Padovan 		if (chan->chan_type != L2CAP_CHAN_RAW)
21050a708f8fSGustavo F. Padovan 			continue;
21060a708f8fSGustavo F. Padovan 
21070a708f8fSGustavo F. Padovan 		/* Don't send frame to the socket it came from */
21080a708f8fSGustavo F. Padovan 		if (skb->sk == sk)
21090a708f8fSGustavo F. Padovan 			continue;
21100a708f8fSGustavo F. Padovan 		nskb = skb_clone(skb, GFP_ATOMIC);
21110a708f8fSGustavo F. Padovan 		if (!nskb)
21120a708f8fSGustavo F. Padovan 			continue;
21130a708f8fSGustavo F. Padovan 
211423070494SGustavo F. Padovan 		if (chan->ops->recv(chan->data, nskb))
21150a708f8fSGustavo F. Padovan 			kfree_skb(nskb);
21160a708f8fSGustavo F. Padovan 	}
21173d57dc68SGustavo F. Padovan 
21183df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
21190a708f8fSGustavo F. Padovan }
21200a708f8fSGustavo F. Padovan 
21210a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */
21220a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
21230a708f8fSGustavo F. Padovan 				u8 code, u8 ident, u16 dlen, void *data)
21240a708f8fSGustavo F. Padovan {
21250a708f8fSGustavo F. Padovan 	struct sk_buff *skb, **frag;
21260a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr *cmd;
21270a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh;
21280a708f8fSGustavo F. Padovan 	int len, count;
21290a708f8fSGustavo F. Padovan 
21300a708f8fSGustavo F. Padovan 	BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
21310a708f8fSGustavo F. Padovan 			conn, code, ident, dlen);
21320a708f8fSGustavo F. Padovan 
21330a708f8fSGustavo F. Padovan 	len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
21340a708f8fSGustavo F. Padovan 	count = min_t(unsigned int, conn->mtu, len);
21350a708f8fSGustavo F. Padovan 
21360a708f8fSGustavo F. Padovan 	skb = bt_skb_alloc(count, GFP_ATOMIC);
21370a708f8fSGustavo F. Padovan 	if (!skb)
21380a708f8fSGustavo F. Padovan 		return NULL;
21390a708f8fSGustavo F. Padovan 
21400a708f8fSGustavo F. Padovan 	lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
21410a708f8fSGustavo F. Padovan 	lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
21423300d9a9SClaudio Takahasi 
21433300d9a9SClaudio Takahasi 	if (conn->hcon->type == LE_LINK)
21443300d9a9SClaudio Takahasi 		lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
21453300d9a9SClaudio Takahasi 	else
21460a708f8fSGustavo F. Padovan 		lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
21470a708f8fSGustavo F. Padovan 
21480a708f8fSGustavo F. Padovan 	cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
21490a708f8fSGustavo F. Padovan 	cmd->code  = code;
21500a708f8fSGustavo F. Padovan 	cmd->ident = ident;
21510a708f8fSGustavo F. Padovan 	cmd->len   = cpu_to_le16(dlen);
21520a708f8fSGustavo F. Padovan 
21530a708f8fSGustavo F. Padovan 	if (dlen) {
21540a708f8fSGustavo F. Padovan 		count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
21550a708f8fSGustavo F. Padovan 		memcpy(skb_put(skb, count), data, count);
21560a708f8fSGustavo F. Padovan 		data += count;
21570a708f8fSGustavo F. Padovan 	}
21580a708f8fSGustavo F. Padovan 
21590a708f8fSGustavo F. Padovan 	len -= skb->len;
21600a708f8fSGustavo F. Padovan 
21610a708f8fSGustavo F. Padovan 	/* Continuation fragments (no L2CAP header) */
21620a708f8fSGustavo F. Padovan 	frag = &skb_shinfo(skb)->frag_list;
21630a708f8fSGustavo F. Padovan 	while (len) {
21640a708f8fSGustavo F. Padovan 		count = min_t(unsigned int, conn->mtu, len);
21650a708f8fSGustavo F. Padovan 
21660a708f8fSGustavo F. Padovan 		*frag = bt_skb_alloc(count, GFP_ATOMIC);
21670a708f8fSGustavo F. Padovan 		if (!*frag)
21680a708f8fSGustavo F. Padovan 			goto fail;
21690a708f8fSGustavo F. Padovan 
21700a708f8fSGustavo F. Padovan 		memcpy(skb_put(*frag, count), data, count);
21710a708f8fSGustavo F. Padovan 
21720a708f8fSGustavo F. Padovan 		len  -= count;
21730a708f8fSGustavo F. Padovan 		data += count;
21740a708f8fSGustavo F. Padovan 
21750a708f8fSGustavo F. Padovan 		frag = &(*frag)->next;
21760a708f8fSGustavo F. Padovan 	}
21770a708f8fSGustavo F. Padovan 
21780a708f8fSGustavo F. Padovan 	return skb;
21790a708f8fSGustavo F. Padovan 
21800a708f8fSGustavo F. Padovan fail:
21810a708f8fSGustavo F. Padovan 	kfree_skb(skb);
21820a708f8fSGustavo F. Padovan 	return NULL;
21830a708f8fSGustavo F. Padovan }
21840a708f8fSGustavo F. Padovan 
21850a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
21860a708f8fSGustavo F. Padovan {
21870a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
21880a708f8fSGustavo F. Padovan 	int len;
21890a708f8fSGustavo F. Padovan 
21900a708f8fSGustavo F. Padovan 	len = L2CAP_CONF_OPT_SIZE + opt->len;
21910a708f8fSGustavo F. Padovan 	*ptr += len;
21920a708f8fSGustavo F. Padovan 
21930a708f8fSGustavo F. Padovan 	*type = opt->type;
21940a708f8fSGustavo F. Padovan 	*olen = opt->len;
21950a708f8fSGustavo F. Padovan 
21960a708f8fSGustavo F. Padovan 	switch (opt->len) {
21970a708f8fSGustavo F. Padovan 	case 1:
21980a708f8fSGustavo F. Padovan 		*val = *((u8 *) opt->val);
21990a708f8fSGustavo F. Padovan 		break;
22000a708f8fSGustavo F. Padovan 
22010a708f8fSGustavo F. Padovan 	case 2:
22020a708f8fSGustavo F. Padovan 		*val = get_unaligned_le16(opt->val);
22030a708f8fSGustavo F. Padovan 		break;
22040a708f8fSGustavo F. Padovan 
22050a708f8fSGustavo F. Padovan 	case 4:
22060a708f8fSGustavo F. Padovan 		*val = get_unaligned_le32(opt->val);
22070a708f8fSGustavo F. Padovan 		break;
22080a708f8fSGustavo F. Padovan 
22090a708f8fSGustavo F. Padovan 	default:
22100a708f8fSGustavo F. Padovan 		*val = (unsigned long) opt->val;
22110a708f8fSGustavo F. Padovan 		break;
22120a708f8fSGustavo F. Padovan 	}
22130a708f8fSGustavo F. Padovan 
22140a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
22150a708f8fSGustavo F. Padovan 	return len;
22160a708f8fSGustavo F. Padovan }
22170a708f8fSGustavo F. Padovan 
22180a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
22190a708f8fSGustavo F. Padovan {
22200a708f8fSGustavo F. Padovan 	struct l2cap_conf_opt *opt = *ptr;
22210a708f8fSGustavo F. Padovan 
22220a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
22230a708f8fSGustavo F. Padovan 
22240a708f8fSGustavo F. Padovan 	opt->type = type;
22250a708f8fSGustavo F. Padovan 	opt->len  = len;
22260a708f8fSGustavo F. Padovan 
22270a708f8fSGustavo F. Padovan 	switch (len) {
22280a708f8fSGustavo F. Padovan 	case 1:
22290a708f8fSGustavo F. Padovan 		*((u8 *) opt->val)  = val;
22300a708f8fSGustavo F. Padovan 		break;
22310a708f8fSGustavo F. Padovan 
22320a708f8fSGustavo F. Padovan 	case 2:
22330a708f8fSGustavo F. Padovan 		put_unaligned_le16(val, opt->val);
22340a708f8fSGustavo F. Padovan 		break;
22350a708f8fSGustavo F. Padovan 
22360a708f8fSGustavo F. Padovan 	case 4:
22370a708f8fSGustavo F. Padovan 		put_unaligned_le32(val, opt->val);
22380a708f8fSGustavo F. Padovan 		break;
22390a708f8fSGustavo F. Padovan 
22400a708f8fSGustavo F. Padovan 	default:
22410a708f8fSGustavo F. Padovan 		memcpy(opt->val, (void *) val, len);
22420a708f8fSGustavo F. Padovan 		break;
22430a708f8fSGustavo F. Padovan 	}
22440a708f8fSGustavo F. Padovan 
22450a708f8fSGustavo F. Padovan 	*ptr += L2CAP_CONF_OPT_SIZE + len;
22460a708f8fSGustavo F. Padovan }
22470a708f8fSGustavo F. Padovan 
2248f89cef09SAndrei Emeltchenko static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
2249f89cef09SAndrei Emeltchenko {
2250f89cef09SAndrei Emeltchenko 	struct l2cap_conf_efs efs;
2251f89cef09SAndrei Emeltchenko 
2252f89cef09SAndrei Emeltchenko 	switch (chan->mode) {
2253f89cef09SAndrei Emeltchenko 	case L2CAP_MODE_ERTM:
2254f89cef09SAndrei Emeltchenko 		efs.id		= chan->local_id;
2255f89cef09SAndrei Emeltchenko 		efs.stype	= chan->local_stype;
2256f89cef09SAndrei Emeltchenko 		efs.msdu	= cpu_to_le16(chan->local_msdu);
2257f89cef09SAndrei Emeltchenko 		efs.sdu_itime	= cpu_to_le32(chan->local_sdu_itime);
2258f89cef09SAndrei Emeltchenko 		efs.acc_lat	= cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
2259f89cef09SAndrei Emeltchenko 		efs.flush_to	= cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO);
2260f89cef09SAndrei Emeltchenko 		break;
2261f89cef09SAndrei Emeltchenko 
2262f89cef09SAndrei Emeltchenko 	case L2CAP_MODE_STREAMING:
2263f89cef09SAndrei Emeltchenko 		efs.id		= 1;
2264f89cef09SAndrei Emeltchenko 		efs.stype	= L2CAP_SERV_BESTEFFORT;
2265f89cef09SAndrei Emeltchenko 		efs.msdu	= cpu_to_le16(chan->local_msdu);
2266f89cef09SAndrei Emeltchenko 		efs.sdu_itime	= cpu_to_le32(chan->local_sdu_itime);
2267f89cef09SAndrei Emeltchenko 		efs.acc_lat	= 0;
2268f89cef09SAndrei Emeltchenko 		efs.flush_to	= 0;
2269f89cef09SAndrei Emeltchenko 		break;
2270f89cef09SAndrei Emeltchenko 
2271f89cef09SAndrei Emeltchenko 	default:
2272f89cef09SAndrei Emeltchenko 		return;
2273f89cef09SAndrei Emeltchenko 	}
2274f89cef09SAndrei Emeltchenko 
2275f89cef09SAndrei Emeltchenko 	l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
2276f89cef09SAndrei Emeltchenko 							(unsigned long) &efs);
2277f89cef09SAndrei Emeltchenko }
2278f89cef09SAndrei Emeltchenko 
2279721c4181SGustavo F. Padovan static void l2cap_ack_timeout(struct work_struct *work)
22800a708f8fSGustavo F. Padovan {
2281721c4181SGustavo F. Padovan 	struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
2282721c4181SGustavo F. Padovan 							ack_timer.work);
22830a708f8fSGustavo F. Padovan 
22842fb9b3d4SGustavo F. Padovan 	BT_DBG("chan %p", chan);
22852fb9b3d4SGustavo F. Padovan 
22866be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
22876be36555SAndrei Emeltchenko 
2288b17e73bbSSzymon Janc 	__l2cap_send_ack(chan);
22896be36555SAndrei Emeltchenko 
22906be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
229109bfb2eeSSzymon Janc 
229209bfb2eeSSzymon Janc 	l2cap_chan_put(chan);
22930a708f8fSGustavo F. Padovan }
22940a708f8fSGustavo F. Padovan 
22953c588192SMat Martineau static inline int l2cap_ertm_init(struct l2cap_chan *chan)
22960a708f8fSGustavo F. Padovan {
22973c588192SMat Martineau 	int err;
22983c588192SMat Martineau 
229942e5c802SGustavo F. Padovan 	chan->expected_ack_seq = 0;
23006a026610SGustavo F. Padovan 	chan->unacked_frames = 0;
230142e5c802SGustavo F. Padovan 	chan->buffer_seq = 0;
23026a026610SGustavo F. Padovan 	chan->num_acked = 0;
23036a026610SGustavo F. Padovan 	chan->frames_sent = 0;
23040a708f8fSGustavo F. Padovan 
2305721c4181SGustavo F. Padovan 	INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
2306721c4181SGustavo F. Padovan 	INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
2307721c4181SGustavo F. Padovan 	INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
23080a708f8fSGustavo F. Padovan 
2309f1c6775bSGustavo F. Padovan 	skb_queue_head_init(&chan->srej_q);
23100a708f8fSGustavo F. Padovan 
231139d5a3eeSGustavo F. Padovan 	INIT_LIST_HEAD(&chan->srej_l);
23123c588192SMat Martineau 	err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
23133c588192SMat Martineau 	if (err < 0)
23143c588192SMat Martineau 		return err;
23153c588192SMat Martineau 
23163c588192SMat Martineau 	return l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win);
23170a708f8fSGustavo F. Padovan }
23180a708f8fSGustavo F. Padovan 
23190a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
23200a708f8fSGustavo F. Padovan {
23210a708f8fSGustavo F. Padovan 	switch (mode) {
23220a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
23230a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
23240a708f8fSGustavo F. Padovan 		if (l2cap_mode_supported(mode, remote_feat_mask))
23250a708f8fSGustavo F. Padovan 			return mode;
23260a708f8fSGustavo F. Padovan 		/* fall through */
23270a708f8fSGustavo F. Padovan 	default:
23280a708f8fSGustavo F. Padovan 		return L2CAP_MODE_BASIC;
23290a708f8fSGustavo F. Padovan 	}
23300a708f8fSGustavo F. Padovan }
23310a708f8fSGustavo F. Padovan 
23326327eb98SAndrei Emeltchenko static inline bool __l2cap_ews_supported(struct l2cap_chan *chan)
23336327eb98SAndrei Emeltchenko {
23346327eb98SAndrei Emeltchenko 	return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
23356327eb98SAndrei Emeltchenko }
23366327eb98SAndrei Emeltchenko 
2337f89cef09SAndrei Emeltchenko static inline bool __l2cap_efs_supported(struct l2cap_chan *chan)
2338f89cef09SAndrei Emeltchenko {
2339f89cef09SAndrei Emeltchenko 	return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
2340f89cef09SAndrei Emeltchenko }
2341f89cef09SAndrei Emeltchenko 
23426327eb98SAndrei Emeltchenko static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
23436327eb98SAndrei Emeltchenko {
23446327eb98SAndrei Emeltchenko 	if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
2345836be934SAndrei Emeltchenko 						__l2cap_ews_supported(chan)) {
23466327eb98SAndrei Emeltchenko 		/* use extended control field */
23476327eb98SAndrei Emeltchenko 		set_bit(FLAG_EXT_CTRL, &chan->flags);
2348836be934SAndrei Emeltchenko 		chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
2349836be934SAndrei Emeltchenko 	} else {
23506327eb98SAndrei Emeltchenko 		chan->tx_win = min_t(u16, chan->tx_win,
23516327eb98SAndrei Emeltchenko 						L2CAP_DEFAULT_TX_WINDOW);
2352836be934SAndrei Emeltchenko 		chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
2353836be934SAndrei Emeltchenko 	}
23546327eb98SAndrei Emeltchenko }
23556327eb98SAndrei Emeltchenko 
2356710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
23570a708f8fSGustavo F. Padovan {
23580a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
23590c1bc5c6SGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = chan->mode };
23600a708f8fSGustavo F. Padovan 	void *ptr = req->data;
2361c8f79162SAndrei Emeltchenko 	u16 size;
23620a708f8fSGustavo F. Padovan 
236349208c9cSGustavo F. Padovan 	BT_DBG("chan %p", chan);
23640a708f8fSGustavo F. Padovan 
236573ffa904SGustavo F. Padovan 	if (chan->num_conf_req || chan->num_conf_rsp)
23660a708f8fSGustavo F. Padovan 		goto done;
23670a708f8fSGustavo F. Padovan 
23680c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
23690a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
23700a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
2371c1360a1cSGustavo F. Padovan 		if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
23720a708f8fSGustavo F. Padovan 			break;
23730a708f8fSGustavo F. Padovan 
2374f89cef09SAndrei Emeltchenko 		if (__l2cap_efs_supported(chan))
2375f89cef09SAndrei Emeltchenko 			set_bit(FLAG_EFS_ENABLE, &chan->flags);
2376f89cef09SAndrei Emeltchenko 
23770a708f8fSGustavo F. Padovan 		/* fall through */
23780a708f8fSGustavo F. Padovan 	default:
23798c1d787bSGustavo F. Padovan 		chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
23800a708f8fSGustavo F. Padovan 		break;
23810a708f8fSGustavo F. Padovan 	}
23820a708f8fSGustavo F. Padovan 
23830a708f8fSGustavo F. Padovan done:
23840c1bc5c6SGustavo F. Padovan 	if (chan->imtu != L2CAP_DEFAULT_MTU)
23850c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
23860a708f8fSGustavo F. Padovan 
23870c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
23880a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
23898c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
23908c1d787bSGustavo F. Padovan 				!(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
23910a708f8fSGustavo F. Padovan 			break;
23920a708f8fSGustavo F. Padovan 
23930a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_BASIC;
23940a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
23950a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
23960a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
23970a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
23980a708f8fSGustavo F. Padovan 		rfc.max_pdu_size    = 0;
23990a708f8fSGustavo F. Padovan 
24000a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
24010a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
24020a708f8fSGustavo F. Padovan 		break;
24030a708f8fSGustavo F. Padovan 
24040a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
24050a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_ERTM;
240647d1ec61SGustavo F. Padovan 		rfc.max_transmit    = chan->max_tx;
24070a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
24080a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
2409c8f79162SAndrei Emeltchenko 
2410c8f79162SAndrei Emeltchenko 		size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
2411c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2412c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2413c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2414c8f79162SAndrei Emeltchenko 		rfc.max_pdu_size = cpu_to_le16(size);
24150a708f8fSGustavo F. Padovan 
24166327eb98SAndrei Emeltchenko 		l2cap_txwin_setup(chan);
24176327eb98SAndrei Emeltchenko 
24186327eb98SAndrei Emeltchenko 		rfc.txwin_size = min_t(u16, chan->tx_win,
24196327eb98SAndrei Emeltchenko 						L2CAP_DEFAULT_TX_WINDOW);
24200a708f8fSGustavo F. Padovan 
24210a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
24220a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
24230a708f8fSGustavo F. Padovan 
2424f89cef09SAndrei Emeltchenko 		if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
2425f89cef09SAndrei Emeltchenko 			l2cap_add_opt_efs(&ptr, chan);
2426f89cef09SAndrei Emeltchenko 
24278c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
24280a708f8fSGustavo F. Padovan 			break;
24290a708f8fSGustavo F. Padovan 
243047d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
2431c1360a1cSGustavo F. Padovan 				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
243247d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
243347d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
24340a708f8fSGustavo F. Padovan 		}
24356327eb98SAndrei Emeltchenko 
24366327eb98SAndrei Emeltchenko 		if (test_bit(FLAG_EXT_CTRL, &chan->flags))
24376327eb98SAndrei Emeltchenko 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
24386327eb98SAndrei Emeltchenko 								chan->tx_win);
24390a708f8fSGustavo F. Padovan 		break;
24400a708f8fSGustavo F. Padovan 
24410a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
24420a708f8fSGustavo F. Padovan 		rfc.mode            = L2CAP_MODE_STREAMING;
24430a708f8fSGustavo F. Padovan 		rfc.txwin_size      = 0;
24440a708f8fSGustavo F. Padovan 		rfc.max_transmit    = 0;
24450a708f8fSGustavo F. Padovan 		rfc.retrans_timeout = 0;
24460a708f8fSGustavo F. Padovan 		rfc.monitor_timeout = 0;
2447c8f79162SAndrei Emeltchenko 
2448c8f79162SAndrei Emeltchenko 		size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
2449c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2450c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2451c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2452c8f79162SAndrei Emeltchenko 		rfc.max_pdu_size = cpu_to_le16(size);
24530a708f8fSGustavo F. Padovan 
24540a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
24550a708f8fSGustavo F. Padovan 							(unsigned long) &rfc);
24560a708f8fSGustavo F. Padovan 
2457f89cef09SAndrei Emeltchenko 		if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
2458f89cef09SAndrei Emeltchenko 			l2cap_add_opt_efs(&ptr, chan);
2459f89cef09SAndrei Emeltchenko 
24608c1d787bSGustavo F. Padovan 		if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS))
24610a708f8fSGustavo F. Padovan 			break;
24620a708f8fSGustavo F. Padovan 
246347d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_NONE ||
2464c1360a1cSGustavo F. Padovan 				test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) {
246547d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
246647d1ec61SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs);
24670a708f8fSGustavo F. Padovan 		}
24680a708f8fSGustavo F. Padovan 		break;
24690a708f8fSGustavo F. Padovan 	}
24700a708f8fSGustavo F. Padovan 
2471fe4128e0SGustavo F. Padovan 	req->dcid  = cpu_to_le16(chan->dcid);
24720a708f8fSGustavo F. Padovan 	req->flags = cpu_to_le16(0);
24730a708f8fSGustavo F. Padovan 
24740a708f8fSGustavo F. Padovan 	return ptr - data;
24750a708f8fSGustavo F. Padovan }
24760a708f8fSGustavo F. Padovan 
247773ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
24780a708f8fSGustavo F. Padovan {
24790a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
24800a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
248173ffa904SGustavo F. Padovan 	void *req = chan->conf_req;
248273ffa904SGustavo F. Padovan 	int len = chan->conf_len;
24830a708f8fSGustavo F. Padovan 	int type, hint, olen;
24840a708f8fSGustavo F. Padovan 	unsigned long val;
24850a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
248642dceae2SAndrei Emeltchenko 	struct l2cap_conf_efs efs;
248742dceae2SAndrei Emeltchenko 	u8 remote_efs = 0;
24880a708f8fSGustavo F. Padovan 	u16 mtu = L2CAP_DEFAULT_MTU;
24890a708f8fSGustavo F. Padovan 	u16 result = L2CAP_CONF_SUCCESS;
2490c8f79162SAndrei Emeltchenko 	u16 size;
24910a708f8fSGustavo F. Padovan 
249273ffa904SGustavo F. Padovan 	BT_DBG("chan %p", chan);
24930a708f8fSGustavo F. Padovan 
24940a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
24950a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
24960a708f8fSGustavo F. Padovan 
24970a708f8fSGustavo F. Padovan 		hint  = type & L2CAP_CONF_HINT;
24980a708f8fSGustavo F. Padovan 		type &= L2CAP_CONF_MASK;
24990a708f8fSGustavo F. Padovan 
25000a708f8fSGustavo F. Padovan 		switch (type) {
25010a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
25020a708f8fSGustavo F. Padovan 			mtu = val;
25030a708f8fSGustavo F. Padovan 			break;
25040a708f8fSGustavo F. Padovan 
25050a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
25060c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
25070a708f8fSGustavo F. Padovan 			break;
25080a708f8fSGustavo F. Padovan 
25090a708f8fSGustavo F. Padovan 		case L2CAP_CONF_QOS:
25100a708f8fSGustavo F. Padovan 			break;
25110a708f8fSGustavo F. Padovan 
25120a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
25130a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
25140a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *) val, olen);
25150a708f8fSGustavo F. Padovan 			break;
25160a708f8fSGustavo F. Padovan 
25170a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FCS:
25180a708f8fSGustavo F. Padovan 			if (val == L2CAP_FCS_NONE)
2519c1360a1cSGustavo F. Padovan 				set_bit(CONF_NO_FCS_RECV, &chan->conf_state);
252042dceae2SAndrei Emeltchenko 			break;
25210a708f8fSGustavo F. Padovan 
252242dceae2SAndrei Emeltchenko 		case L2CAP_CONF_EFS:
252342dceae2SAndrei Emeltchenko 			remote_efs = 1;
252442dceae2SAndrei Emeltchenko 			if (olen == sizeof(efs))
252542dceae2SAndrei Emeltchenko 				memcpy(&efs, (void *) val, olen);
25260a708f8fSGustavo F. Padovan 			break;
25270a708f8fSGustavo F. Padovan 
25286327eb98SAndrei Emeltchenko 		case L2CAP_CONF_EWS:
25296327eb98SAndrei Emeltchenko 			if (!enable_hs)
25306327eb98SAndrei Emeltchenko 				return -ECONNREFUSED;
25316327eb98SAndrei Emeltchenko 
25326327eb98SAndrei Emeltchenko 			set_bit(FLAG_EXT_CTRL, &chan->flags);
25336327eb98SAndrei Emeltchenko 			set_bit(CONF_EWS_RECV, &chan->conf_state);
2534836be934SAndrei Emeltchenko 			chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
25356327eb98SAndrei Emeltchenko 			chan->remote_tx_win = val;
25360a708f8fSGustavo F. Padovan 			break;
25370a708f8fSGustavo F. Padovan 
25380a708f8fSGustavo F. Padovan 		default:
25390a708f8fSGustavo F. Padovan 			if (hint)
25400a708f8fSGustavo F. Padovan 				break;
25410a708f8fSGustavo F. Padovan 
25420a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNKNOWN;
25430a708f8fSGustavo F. Padovan 			*((u8 *) ptr++) = type;
25440a708f8fSGustavo F. Padovan 			break;
25450a708f8fSGustavo F. Padovan 		}
25460a708f8fSGustavo F. Padovan 	}
25470a708f8fSGustavo F. Padovan 
254873ffa904SGustavo F. Padovan 	if (chan->num_conf_rsp || chan->num_conf_req > 1)
25490a708f8fSGustavo F. Padovan 		goto done;
25500a708f8fSGustavo F. Padovan 
25510c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
25520a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
25530a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
2554c1360a1cSGustavo F. Padovan 		if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
25550c1bc5c6SGustavo F. Padovan 			chan->mode = l2cap_select_mode(rfc.mode,
25568c1d787bSGustavo F. Padovan 					chan->conn->feat_mask);
25570a708f8fSGustavo F. Padovan 			break;
25580a708f8fSGustavo F. Padovan 		}
25590a708f8fSGustavo F. Padovan 
256042dceae2SAndrei Emeltchenko 		if (remote_efs) {
256142dceae2SAndrei Emeltchenko 			if (__l2cap_efs_supported(chan))
256242dceae2SAndrei Emeltchenko 				set_bit(FLAG_EFS_ENABLE, &chan->flags);
256342dceae2SAndrei Emeltchenko 			else
256442dceae2SAndrei Emeltchenko 				return -ECONNREFUSED;
256542dceae2SAndrei Emeltchenko 		}
256642dceae2SAndrei Emeltchenko 
25670c1bc5c6SGustavo F. Padovan 		if (chan->mode != rfc.mode)
25680a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
25690a708f8fSGustavo F. Padovan 
25700a708f8fSGustavo F. Padovan 		break;
25710a708f8fSGustavo F. Padovan 	}
25720a708f8fSGustavo F. Padovan 
25730a708f8fSGustavo F. Padovan done:
25740c1bc5c6SGustavo F. Padovan 	if (chan->mode != rfc.mode) {
25750a708f8fSGustavo F. Padovan 		result = L2CAP_CONF_UNACCEPT;
25760c1bc5c6SGustavo F. Padovan 		rfc.mode = chan->mode;
25770a708f8fSGustavo F. Padovan 
257873ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp == 1)
25790a708f8fSGustavo F. Padovan 			return -ECONNREFUSED;
25800a708f8fSGustavo F. Padovan 
25810a708f8fSGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
25820a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
25830a708f8fSGustavo F. Padovan 	}
25840a708f8fSGustavo F. Padovan 
25850a708f8fSGustavo F. Padovan 	if (result == L2CAP_CONF_SUCCESS) {
25860a708f8fSGustavo F. Padovan 		/* Configure output options and let the other side know
25870a708f8fSGustavo F. Padovan 		 * which ones we don't like. */
25880a708f8fSGustavo F. Padovan 
25890a708f8fSGustavo F. Padovan 		if (mtu < L2CAP_DEFAULT_MIN_MTU)
25900a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
25910a708f8fSGustavo F. Padovan 		else {
25920c1bc5c6SGustavo F. Padovan 			chan->omtu = mtu;
2593c1360a1cSGustavo F. Padovan 			set_bit(CONF_MTU_DONE, &chan->conf_state);
25940a708f8fSGustavo F. Padovan 		}
25950c1bc5c6SGustavo F. Padovan 		l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
25960a708f8fSGustavo F. Padovan 
259742dceae2SAndrei Emeltchenko 		if (remote_efs) {
259842dceae2SAndrei Emeltchenko 			if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
259942dceae2SAndrei Emeltchenko 					efs.stype != L2CAP_SERV_NOTRAFIC &&
260042dceae2SAndrei Emeltchenko 					efs.stype != chan->local_stype) {
260142dceae2SAndrei Emeltchenko 
260242dceae2SAndrei Emeltchenko 				result = L2CAP_CONF_UNACCEPT;
260342dceae2SAndrei Emeltchenko 
260442dceae2SAndrei Emeltchenko 				if (chan->num_conf_req >= 1)
260542dceae2SAndrei Emeltchenko 					return -ECONNREFUSED;
260642dceae2SAndrei Emeltchenko 
260742dceae2SAndrei Emeltchenko 				l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
260842dceae2SAndrei Emeltchenko 							sizeof(efs),
260942dceae2SAndrei Emeltchenko 							(unsigned long) &efs);
26100e8b207eSAndrei Emeltchenko 			} else {
26113e6b3b95SGustavo F. Padovan 				/* Send PENDING Conf Rsp */
26120e8b207eSAndrei Emeltchenko 				result = L2CAP_CONF_PENDING;
26130e8b207eSAndrei Emeltchenko 				set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
261442dceae2SAndrei Emeltchenko 			}
261542dceae2SAndrei Emeltchenko 		}
261642dceae2SAndrei Emeltchenko 
26170a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
26180a708f8fSGustavo F. Padovan 		case L2CAP_MODE_BASIC:
261947d1ec61SGustavo F. Padovan 			chan->fcs = L2CAP_FCS_NONE;
2620c1360a1cSGustavo F. Padovan 			set_bit(CONF_MODE_DONE, &chan->conf_state);
26210a708f8fSGustavo F. Padovan 			break;
26220a708f8fSGustavo F. Padovan 
26230a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
26246327eb98SAndrei Emeltchenko 			if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
26252c03a7a4SGustavo F. Padovan 				chan->remote_tx_win = rfc.txwin_size;
26266327eb98SAndrei Emeltchenko 			else
26276327eb98SAndrei Emeltchenko 				rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
26286327eb98SAndrei Emeltchenko 
26292c03a7a4SGustavo F. Padovan 			chan->remote_max_tx = rfc.max_transmit;
26300a708f8fSGustavo F. Padovan 
2631c8f79162SAndrei Emeltchenko 			size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
2632c8f79162SAndrei Emeltchenko 						chan->conn->mtu -
2633c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2634c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2635c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2636c8f79162SAndrei Emeltchenko 			rfc.max_pdu_size = cpu_to_le16(size);
2637c8f79162SAndrei Emeltchenko 			chan->remote_mps = size;
26380a708f8fSGustavo F. Padovan 
26390a708f8fSGustavo F. Padovan 			rfc.retrans_timeout =
26404fd21a88SAndrei Emeltchenko 				__constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
26410a708f8fSGustavo F. Padovan 			rfc.monitor_timeout =
26424fd21a88SAndrei Emeltchenko 				__constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
26430a708f8fSGustavo F. Padovan 
2644c1360a1cSGustavo F. Padovan 			set_bit(CONF_MODE_DONE, &chan->conf_state);
26450a708f8fSGustavo F. Padovan 
26460a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
26470a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
26480a708f8fSGustavo F. Padovan 
264942dceae2SAndrei Emeltchenko 			if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
265042dceae2SAndrei Emeltchenko 				chan->remote_id = efs.id;
265142dceae2SAndrei Emeltchenko 				chan->remote_stype = efs.stype;
265242dceae2SAndrei Emeltchenko 				chan->remote_msdu = le16_to_cpu(efs.msdu);
265342dceae2SAndrei Emeltchenko 				chan->remote_flush_to =
265442dceae2SAndrei Emeltchenko 						le32_to_cpu(efs.flush_to);
265542dceae2SAndrei Emeltchenko 				chan->remote_acc_lat =
265642dceae2SAndrei Emeltchenko 						le32_to_cpu(efs.acc_lat);
265742dceae2SAndrei Emeltchenko 				chan->remote_sdu_itime =
265842dceae2SAndrei Emeltchenko 					le32_to_cpu(efs.sdu_itime);
265942dceae2SAndrei Emeltchenko 				l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
266042dceae2SAndrei Emeltchenko 					sizeof(efs), (unsigned long) &efs);
266142dceae2SAndrei Emeltchenko 			}
26620a708f8fSGustavo F. Padovan 			break;
26630a708f8fSGustavo F. Padovan 
26640a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
2665c8f79162SAndrei Emeltchenko 			size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
2666c8f79162SAndrei Emeltchenko 						chan->conn->mtu -
2667c8f79162SAndrei Emeltchenko 						L2CAP_EXT_HDR_SIZE -
2668c8f79162SAndrei Emeltchenko 						L2CAP_SDULEN_SIZE -
2669c8f79162SAndrei Emeltchenko 						L2CAP_FCS_SIZE);
2670c8f79162SAndrei Emeltchenko 			rfc.max_pdu_size = cpu_to_le16(size);
2671c8f79162SAndrei Emeltchenko 			chan->remote_mps = size;
26720a708f8fSGustavo F. Padovan 
2673c1360a1cSGustavo F. Padovan 			set_bit(CONF_MODE_DONE, &chan->conf_state);
26740a708f8fSGustavo F. Padovan 
26750a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
26760a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
26770a708f8fSGustavo F. Padovan 
26780a708f8fSGustavo F. Padovan 			break;
26790a708f8fSGustavo F. Padovan 
26800a708f8fSGustavo F. Padovan 		default:
26810a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_UNACCEPT;
26820a708f8fSGustavo F. Padovan 
26830a708f8fSGustavo F. Padovan 			memset(&rfc, 0, sizeof(rfc));
26840c1bc5c6SGustavo F. Padovan 			rfc.mode = chan->mode;
26850a708f8fSGustavo F. Padovan 		}
26860a708f8fSGustavo F. Padovan 
26870a708f8fSGustavo F. Padovan 		if (result == L2CAP_CONF_SUCCESS)
2688c1360a1cSGustavo F. Padovan 			set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
26890a708f8fSGustavo F. Padovan 	}
2690fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
26910a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
26920a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(0x0000);
26930a708f8fSGustavo F. Padovan 
26940a708f8fSGustavo F. Padovan 	return ptr - data;
26950a708f8fSGustavo F. Padovan }
26960a708f8fSGustavo F. Padovan 
2697b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result)
26980a708f8fSGustavo F. Padovan {
26990a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = data;
27000a708f8fSGustavo F. Padovan 	void *ptr = req->data;
27010a708f8fSGustavo F. Padovan 	int type, olen;
27020a708f8fSGustavo F. Padovan 	unsigned long val;
270336e999a8SMat Martineau 	struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
270466af7aafSAndrei Emeltchenko 	struct l2cap_conf_efs efs;
27050a708f8fSGustavo F. Padovan 
2706fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
27070a708f8fSGustavo F. Padovan 
27080a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
27090a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
27100a708f8fSGustavo F. Padovan 
27110a708f8fSGustavo F. Padovan 		switch (type) {
27120a708f8fSGustavo F. Padovan 		case L2CAP_CONF_MTU:
27130a708f8fSGustavo F. Padovan 			if (val < L2CAP_DEFAULT_MIN_MTU) {
27140a708f8fSGustavo F. Padovan 				*result = L2CAP_CONF_UNACCEPT;
27150c1bc5c6SGustavo F. Padovan 				chan->imtu = L2CAP_DEFAULT_MIN_MTU;
27160a708f8fSGustavo F. Padovan 			} else
27170c1bc5c6SGustavo F. Padovan 				chan->imtu = val;
27180c1bc5c6SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
27190a708f8fSGustavo F. Padovan 			break;
27200a708f8fSGustavo F. Padovan 
27210a708f8fSGustavo F. Padovan 		case L2CAP_CONF_FLUSH_TO:
27220c1bc5c6SGustavo F. Padovan 			chan->flush_to = val;
27230a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
27240c1bc5c6SGustavo F. Padovan 							2, chan->flush_to);
27250a708f8fSGustavo F. Padovan 			break;
27260a708f8fSGustavo F. Padovan 
27270a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
27280a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
27290a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
27300a708f8fSGustavo F. Padovan 
2731c1360a1cSGustavo F. Padovan 			if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
27320c1bc5c6SGustavo F. Padovan 							rfc.mode != chan->mode)
27330a708f8fSGustavo F. Padovan 				return -ECONNREFUSED;
27340a708f8fSGustavo F. Padovan 
273547d1ec61SGustavo F. Padovan 			chan->fcs = 0;
27360a708f8fSGustavo F. Padovan 
27370a708f8fSGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
27380a708f8fSGustavo F. Padovan 					sizeof(rfc), (unsigned long) &rfc);
27390a708f8fSGustavo F. Padovan 			break;
27406327eb98SAndrei Emeltchenko 
27416327eb98SAndrei Emeltchenko 		case L2CAP_CONF_EWS:
27426327eb98SAndrei Emeltchenko 			chan->tx_win = min_t(u16, val,
27436327eb98SAndrei Emeltchenko 						L2CAP_DEFAULT_EXT_WINDOW);
27443e6b3b95SGustavo F. Padovan 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
27453e6b3b95SGustavo F. Padovan 							chan->tx_win);
27466327eb98SAndrei Emeltchenko 			break;
274766af7aafSAndrei Emeltchenko 
274866af7aafSAndrei Emeltchenko 		case L2CAP_CONF_EFS:
274966af7aafSAndrei Emeltchenko 			if (olen == sizeof(efs))
275066af7aafSAndrei Emeltchenko 				memcpy(&efs, (void *)val, olen);
275166af7aafSAndrei Emeltchenko 
275266af7aafSAndrei Emeltchenko 			if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
275366af7aafSAndrei Emeltchenko 					efs.stype != L2CAP_SERV_NOTRAFIC &&
275466af7aafSAndrei Emeltchenko 					efs.stype != chan->local_stype)
275566af7aafSAndrei Emeltchenko 				return -ECONNREFUSED;
275666af7aafSAndrei Emeltchenko 
275766af7aafSAndrei Emeltchenko 			l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
275866af7aafSAndrei Emeltchenko 					sizeof(efs), (unsigned long) &efs);
275966af7aafSAndrei Emeltchenko 			break;
27600a708f8fSGustavo F. Padovan 		}
27610a708f8fSGustavo F. Padovan 	}
27620a708f8fSGustavo F. Padovan 
27630c1bc5c6SGustavo F. Padovan 	if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
27640a708f8fSGustavo F. Padovan 		return -ECONNREFUSED;
27650a708f8fSGustavo F. Padovan 
27660c1bc5c6SGustavo F. Padovan 	chan->mode = rfc.mode;
27670a708f8fSGustavo F. Padovan 
27680e8b207eSAndrei Emeltchenko 	if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
27690a708f8fSGustavo F. Padovan 		switch (rfc.mode) {
27700a708f8fSGustavo F. Padovan 		case L2CAP_MODE_ERTM:
277147d1ec61SGustavo F. Padovan 			chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
277247d1ec61SGustavo F. Padovan 			chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
277347d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
277466af7aafSAndrei Emeltchenko 
277566af7aafSAndrei Emeltchenko 			if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
277666af7aafSAndrei Emeltchenko 				chan->local_msdu = le16_to_cpu(efs.msdu);
277766af7aafSAndrei Emeltchenko 				chan->local_sdu_itime =
277866af7aafSAndrei Emeltchenko 						le32_to_cpu(efs.sdu_itime);
277966af7aafSAndrei Emeltchenko 				chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
278066af7aafSAndrei Emeltchenko 				chan->local_flush_to =
278166af7aafSAndrei Emeltchenko 						le32_to_cpu(efs.flush_to);
278266af7aafSAndrei Emeltchenko 			}
27830a708f8fSGustavo F. Padovan 			break;
278466af7aafSAndrei Emeltchenko 
27850a708f8fSGustavo F. Padovan 		case L2CAP_MODE_STREAMING:
278647d1ec61SGustavo F. Padovan 			chan->mps    = le16_to_cpu(rfc.max_pdu_size);
27870a708f8fSGustavo F. Padovan 		}
27880a708f8fSGustavo F. Padovan 	}
27890a708f8fSGustavo F. Padovan 
2790fe4128e0SGustavo F. Padovan 	req->dcid   = cpu_to_le16(chan->dcid);
27910a708f8fSGustavo F. Padovan 	req->flags  = cpu_to_le16(0x0000);
27920a708f8fSGustavo F. Padovan 
27930a708f8fSGustavo F. Padovan 	return ptr - data;
27940a708f8fSGustavo F. Padovan }
27950a708f8fSGustavo F. Padovan 
2796fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags)
27970a708f8fSGustavo F. Padovan {
27980a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = data;
27990a708f8fSGustavo F. Padovan 	void *ptr = rsp->data;
28000a708f8fSGustavo F. Padovan 
2801fe4128e0SGustavo F. Padovan 	BT_DBG("chan %p", chan);
28020a708f8fSGustavo F. Padovan 
2803fe4128e0SGustavo F. Padovan 	rsp->scid   = cpu_to_le16(chan->dcid);
28040a708f8fSGustavo F. Padovan 	rsp->result = cpu_to_le16(result);
28050a708f8fSGustavo F. Padovan 	rsp->flags  = cpu_to_le16(flags);
28060a708f8fSGustavo F. Padovan 
28070a708f8fSGustavo F. Padovan 	return ptr - data;
28080a708f8fSGustavo F. Padovan }
28090a708f8fSGustavo F. Padovan 
28108c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
2811710f9b0aSGustavo F. Padovan {
2812710f9b0aSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
28138c1d787bSGustavo F. Padovan 	struct l2cap_conn *conn = chan->conn;
2814710f9b0aSGustavo F. Padovan 	u8 buf[128];
2815710f9b0aSGustavo F. Padovan 
2816fe4128e0SGustavo F. Padovan 	rsp.scid   = cpu_to_le16(chan->dcid);
2817fe4128e0SGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(chan->scid);
2818710f9b0aSGustavo F. Padovan 	rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
2819710f9b0aSGustavo F. Padovan 	rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
2820710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, chan->ident,
2821710f9b0aSGustavo F. Padovan 				L2CAP_CONN_RSP, sizeof(rsp), &rsp);
2822710f9b0aSGustavo F. Padovan 
2823c1360a1cSGustavo F. Padovan 	if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
2824710f9b0aSGustavo F. Padovan 		return;
2825710f9b0aSGustavo F. Padovan 
2826710f9b0aSGustavo F. Padovan 	l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
2827710f9b0aSGustavo F. Padovan 			l2cap_build_conf_req(chan, buf), buf);
2828710f9b0aSGustavo F. Padovan 	chan->num_conf_req++;
2829710f9b0aSGustavo F. Padovan }
2830710f9b0aSGustavo F. Padovan 
283147d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
28320a708f8fSGustavo F. Padovan {
28330a708f8fSGustavo F. Padovan 	int type, olen;
28340a708f8fSGustavo F. Padovan 	unsigned long val;
28350a708f8fSGustavo F. Padovan 	struct l2cap_conf_rfc rfc;
28360a708f8fSGustavo F. Padovan 
283747d1ec61SGustavo F. Padovan 	BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
28380a708f8fSGustavo F. Padovan 
28390c1bc5c6SGustavo F. Padovan 	if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
28400a708f8fSGustavo F. Padovan 		return;
28410a708f8fSGustavo F. Padovan 
28420a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CONF_OPT_SIZE) {
28430a708f8fSGustavo F. Padovan 		len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
28440a708f8fSGustavo F. Padovan 
28450a708f8fSGustavo F. Padovan 		switch (type) {
28460a708f8fSGustavo F. Padovan 		case L2CAP_CONF_RFC:
28470a708f8fSGustavo F. Padovan 			if (olen == sizeof(rfc))
28480a708f8fSGustavo F. Padovan 				memcpy(&rfc, (void *)val, olen);
28490a708f8fSGustavo F. Padovan 			goto done;
28500a708f8fSGustavo F. Padovan 		}
28510a708f8fSGustavo F. Padovan 	}
28520a708f8fSGustavo F. Padovan 
285336e999a8SMat Martineau 	/* Use sane default values in case a misbehaving remote device
285436e999a8SMat Martineau 	 * did not send an RFC option.
285536e999a8SMat Martineau 	 */
285636e999a8SMat Martineau 	rfc.mode = chan->mode;
285736e999a8SMat Martineau 	rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
285836e999a8SMat Martineau 	rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
285936e999a8SMat Martineau 	rfc.max_pdu_size = cpu_to_le16(chan->imtu);
286036e999a8SMat Martineau 
286136e999a8SMat Martineau 	BT_ERR("Expected RFC option was not found, using defaults");
286236e999a8SMat Martineau 
28630a708f8fSGustavo F. Padovan done:
28640a708f8fSGustavo F. Padovan 	switch (rfc.mode) {
28650a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
286647d1ec61SGustavo F. Padovan 		chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
286747d1ec61SGustavo F. Padovan 		chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
286847d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
28690a708f8fSGustavo F. Padovan 		break;
28700a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
287147d1ec61SGustavo F. Padovan 		chan->mps    = le16_to_cpu(rfc.max_pdu_size);
28720a708f8fSGustavo F. Padovan 	}
28730a708f8fSGustavo F. Padovan }
28740a708f8fSGustavo F. Padovan 
28750a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
28760a708f8fSGustavo F. Padovan {
2877e2fd318eSIlia Kolomisnky 	struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
28780a708f8fSGustavo F. Padovan 
2879e2fd318eSIlia Kolomisnky 	if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
28800a708f8fSGustavo F. Padovan 		return 0;
28810a708f8fSGustavo F. Padovan 
28820a708f8fSGustavo F. Padovan 	if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
28830a708f8fSGustavo F. Padovan 					cmd->ident == conn->info_ident) {
288417cd3f37SUlisses Furquim 		cancel_delayed_work(&conn->info_timer);
28850a708f8fSGustavo F. Padovan 
28860a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
28870a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
28880a708f8fSGustavo F. Padovan 
28890a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
28900a708f8fSGustavo F. Padovan 	}
28910a708f8fSGustavo F. Padovan 
28920a708f8fSGustavo F. Padovan 	return 0;
28930a708f8fSGustavo F. Padovan }
28940a708f8fSGustavo F. Padovan 
28950a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
28960a708f8fSGustavo F. Padovan {
28970a708f8fSGustavo F. Padovan 	struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
28980a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp rsp;
289923691d75SGustavo F. Padovan 	struct l2cap_chan *chan = NULL, *pchan;
29000a708f8fSGustavo F. Padovan 	struct sock *parent, *sk = NULL;
29010a708f8fSGustavo F. Padovan 	int result, status = L2CAP_CS_NO_INFO;
29020a708f8fSGustavo F. Padovan 
29030a708f8fSGustavo F. Padovan 	u16 dcid = 0, scid = __le16_to_cpu(req->scid);
29040a708f8fSGustavo F. Padovan 	__le16 psm = req->psm;
29050a708f8fSGustavo F. Padovan 
2906097db76cSAndrei Emeltchenko 	BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
29070a708f8fSGustavo F. Padovan 
29080a708f8fSGustavo F. Padovan 	/* Check if we have socket listening on psm */
2909c2287681SIdo Yariv 	pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src, conn->dst);
291023691d75SGustavo F. Padovan 	if (!pchan) {
29110a708f8fSGustavo F. Padovan 		result = L2CAP_CR_BAD_PSM;
29120a708f8fSGustavo F. Padovan 		goto sendresp;
29130a708f8fSGustavo F. Padovan 	}
29140a708f8fSGustavo F. Padovan 
291523691d75SGustavo F. Padovan 	parent = pchan->sk;
291623691d75SGustavo F. Padovan 
29173df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
2918aa2ac881SGustavo F. Padovan 	lock_sock(parent);
29190a708f8fSGustavo F. Padovan 
29200a708f8fSGustavo F. Padovan 	/* Check if the ACL is secure enough (if not SDP) */
29210a708f8fSGustavo F. Padovan 	if (psm != cpu_to_le16(0x0001) &&
29220a708f8fSGustavo F. Padovan 				!hci_conn_check_link_mode(conn->hcon)) {
29239f5a0d7bSAndrei Emeltchenko 		conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
29240a708f8fSGustavo F. Padovan 		result = L2CAP_CR_SEC_BLOCK;
29250a708f8fSGustavo F. Padovan 		goto response;
29260a708f8fSGustavo F. Padovan 	}
29270a708f8fSGustavo F. Padovan 
29280a708f8fSGustavo F. Padovan 	result = L2CAP_CR_NO_MEM;
29290a708f8fSGustavo F. Padovan 
29300a708f8fSGustavo F. Padovan 	/* Check for backlog size */
29310a708f8fSGustavo F. Padovan 	if (sk_acceptq_is_full(parent)) {
29320a708f8fSGustavo F. Padovan 		BT_DBG("backlog full %d", parent->sk_ack_backlog);
29330a708f8fSGustavo F. Padovan 		goto response;
29340a708f8fSGustavo F. Padovan 	}
29350a708f8fSGustavo F. Padovan 
293680808e43SGustavo F. Padovan 	chan = pchan->ops->new_connection(pchan->data);
293780808e43SGustavo F. Padovan 	if (!chan)
29380a708f8fSGustavo F. Padovan 		goto response;
29390a708f8fSGustavo F. Padovan 
294080808e43SGustavo F. Padovan 	sk = chan->sk;
294180808e43SGustavo F. Padovan 
29420a708f8fSGustavo F. Padovan 	/* Check if we already have channel with that dcid */
2943baa7e1faSGustavo F. Padovan 	if (__l2cap_get_chan_by_dcid(conn, scid)) {
29440a708f8fSGustavo F. Padovan 		sock_set_flag(sk, SOCK_ZAPPED);
2945ba3bd0eeSGustavo F. Padovan 		chan->ops->close(chan->data);
29460a708f8fSGustavo F. Padovan 		goto response;
29470a708f8fSGustavo F. Padovan 	}
29480a708f8fSGustavo F. Padovan 
29490a708f8fSGustavo F. Padovan 	hci_conn_hold(conn->hcon);
29500a708f8fSGustavo F. Padovan 
29510a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->src, conn->src);
29520a708f8fSGustavo F. Padovan 	bacpy(&bt_sk(sk)->dst, conn->dst);
2953fe4128e0SGustavo F. Padovan 	chan->psm  = psm;
2954fe4128e0SGustavo F. Padovan 	chan->dcid = scid;
29550a708f8fSGustavo F. Padovan 
2956d1010240SGustavo F. Padovan 	bt_accept_enqueue(parent, sk);
2957d1010240SGustavo F. Padovan 
29586be36555SAndrei Emeltchenko 	__l2cap_chan_add(conn, chan);
295948454079SGustavo F. Padovan 
2960fe4128e0SGustavo F. Padovan 	dcid = chan->scid;
29610a708f8fSGustavo F. Padovan 
2962c9b66675SGustavo F. Padovan 	__set_chan_timer(chan, sk->sk_sndtimeo);
29630a708f8fSGustavo F. Padovan 
2964fc7f8a7eSGustavo F. Padovan 	chan->ident = cmd->ident;
29650a708f8fSGustavo F. Padovan 
29660a708f8fSGustavo F. Padovan 	if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
2967d45fc423SGustavo F. Padovan 		if (l2cap_chan_check_security(chan)) {
29680a708f8fSGustavo F. Padovan 			if (bt_sk(sk)->defer_setup) {
29690e587be7SAndrei Emeltchenko 				__l2cap_state_change(chan, BT_CONNECT2);
29700a708f8fSGustavo F. Padovan 				result = L2CAP_CR_PEND;
29710a708f8fSGustavo F. Padovan 				status = L2CAP_CS_AUTHOR_PEND;
29720a708f8fSGustavo F. Padovan 				parent->sk_data_ready(parent, 0);
29730a708f8fSGustavo F. Padovan 			} else {
29740e587be7SAndrei Emeltchenko 				__l2cap_state_change(chan, BT_CONFIG);
29750a708f8fSGustavo F. Padovan 				result = L2CAP_CR_SUCCESS;
29760a708f8fSGustavo F. Padovan 				status = L2CAP_CS_NO_INFO;
29770a708f8fSGustavo F. Padovan 			}
29780a708f8fSGustavo F. Padovan 		} else {
29790e587be7SAndrei Emeltchenko 			__l2cap_state_change(chan, BT_CONNECT2);
29800a708f8fSGustavo F. Padovan 			result = L2CAP_CR_PEND;
29810a708f8fSGustavo F. Padovan 			status = L2CAP_CS_AUTHEN_PEND;
29820a708f8fSGustavo F. Padovan 		}
29830a708f8fSGustavo F. Padovan 	} else {
29840e587be7SAndrei Emeltchenko 		__l2cap_state_change(chan, BT_CONNECT2);
29850a708f8fSGustavo F. Padovan 		result = L2CAP_CR_PEND;
29860a708f8fSGustavo F. Padovan 		status = L2CAP_CS_NO_INFO;
29870a708f8fSGustavo F. Padovan 	}
29880a708f8fSGustavo F. Padovan 
29890a708f8fSGustavo F. Padovan response:
2990aa2ac881SGustavo F. Padovan 	release_sock(parent);
29913df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
29920a708f8fSGustavo F. Padovan 
29930a708f8fSGustavo F. Padovan sendresp:
29940a708f8fSGustavo F. Padovan 	rsp.scid   = cpu_to_le16(scid);
29950a708f8fSGustavo F. Padovan 	rsp.dcid   = cpu_to_le16(dcid);
29960a708f8fSGustavo F. Padovan 	rsp.result = cpu_to_le16(result);
29970a708f8fSGustavo F. Padovan 	rsp.status = cpu_to_le16(status);
29980a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
29990a708f8fSGustavo F. Padovan 
30000a708f8fSGustavo F. Padovan 	if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
30010a708f8fSGustavo F. Padovan 		struct l2cap_info_req info;
30020a708f8fSGustavo F. Padovan 		info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
30030a708f8fSGustavo F. Padovan 
30040a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
30050a708f8fSGustavo F. Padovan 		conn->info_ident = l2cap_get_ident(conn);
30060a708f8fSGustavo F. Padovan 
3007ba13ccd9SMarcel Holtmann 		schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
30080a708f8fSGustavo F. Padovan 
30090a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, conn->info_ident,
30100a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(info), &info);
30110a708f8fSGustavo F. Padovan 	}
30120a708f8fSGustavo F. Padovan 
3013c1360a1cSGustavo F. Padovan 	if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
30140a708f8fSGustavo F. Padovan 				result == L2CAP_CR_SUCCESS) {
30150a708f8fSGustavo F. Padovan 		u8 buf[128];
3016c1360a1cSGustavo F. Padovan 		set_bit(CONF_REQ_SENT, &chan->conf_state);
30170a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
301873ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
301973ffa904SGustavo F. Padovan 		chan->num_conf_req++;
30200a708f8fSGustavo F. Padovan 	}
30210a708f8fSGustavo F. Padovan 
30220a708f8fSGustavo F. Padovan 	return 0;
30230a708f8fSGustavo F. Padovan }
30240a708f8fSGustavo F. Padovan 
30250a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
30260a708f8fSGustavo F. Padovan {
30270a708f8fSGustavo F. Padovan 	struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
30280a708f8fSGustavo F. Padovan 	u16 scid, dcid, result, status;
302948454079SGustavo F. Padovan 	struct l2cap_chan *chan;
30300a708f8fSGustavo F. Padovan 	u8 req[128];
30313df91ea2SAndrei Emeltchenko 	int err;
30320a708f8fSGustavo F. Padovan 
30330a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
30340a708f8fSGustavo F. Padovan 	dcid   = __le16_to_cpu(rsp->dcid);
30350a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
30360a708f8fSGustavo F. Padovan 	status = __le16_to_cpu(rsp->status);
30370a708f8fSGustavo F. Padovan 
30381b009c98SAndrei Emeltchenko 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x",
30391b009c98SAndrei Emeltchenko 						dcid, scid, result, status);
30400a708f8fSGustavo F. Padovan 
30413df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
30423df91ea2SAndrei Emeltchenko 
30430a708f8fSGustavo F. Padovan 	if (scid) {
30443df91ea2SAndrei Emeltchenko 		chan = __l2cap_get_chan_by_scid(conn, scid);
30453df91ea2SAndrei Emeltchenko 		if (!chan) {
30463df91ea2SAndrei Emeltchenko 			err = -EFAULT;
30473df91ea2SAndrei Emeltchenko 			goto unlock;
30483df91ea2SAndrei Emeltchenko 		}
30490a708f8fSGustavo F. Padovan 	} else {
30503df91ea2SAndrei Emeltchenko 		chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
30513df91ea2SAndrei Emeltchenko 		if (!chan) {
30523df91ea2SAndrei Emeltchenko 			err = -EFAULT;
30533df91ea2SAndrei Emeltchenko 			goto unlock;
30543df91ea2SAndrei Emeltchenko 		}
30550a708f8fSGustavo F. Padovan 	}
30560a708f8fSGustavo F. Padovan 
30573df91ea2SAndrei Emeltchenko 	err = 0;
30583df91ea2SAndrei Emeltchenko 
30596be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
306048454079SGustavo F. Padovan 
30610a708f8fSGustavo F. Padovan 	switch (result) {
30620a708f8fSGustavo F. Padovan 	case L2CAP_CR_SUCCESS:
306389bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONFIG);
3064fc7f8a7eSGustavo F. Padovan 		chan->ident = 0;
3065fe4128e0SGustavo F. Padovan 		chan->dcid = dcid;
3066c1360a1cSGustavo F. Padovan 		clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
30670a708f8fSGustavo F. Padovan 
3068c1360a1cSGustavo F. Padovan 		if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
30690a708f8fSGustavo F. Padovan 			break;
30700a708f8fSGustavo F. Padovan 
30710a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
307273ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, req), req);
307373ffa904SGustavo F. Padovan 		chan->num_conf_req++;
30740a708f8fSGustavo F. Padovan 		break;
30750a708f8fSGustavo F. Padovan 
30760a708f8fSGustavo F. Padovan 	case L2CAP_CR_PEND:
3077c1360a1cSGustavo F. Padovan 		set_bit(CONF_CONNECT_PEND, &chan->conf_state);
30780a708f8fSGustavo F. Padovan 		break;
30790a708f8fSGustavo F. Padovan 
30800a708f8fSGustavo F. Padovan 	default:
308148454079SGustavo F. Padovan 		l2cap_chan_del(chan, ECONNREFUSED);
30820a708f8fSGustavo F. Padovan 		break;
30830a708f8fSGustavo F. Padovan 	}
30840a708f8fSGustavo F. Padovan 
30856be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
30863df91ea2SAndrei Emeltchenko 
30873df91ea2SAndrei Emeltchenko unlock:
30883df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
30893df91ea2SAndrei Emeltchenko 
30903df91ea2SAndrei Emeltchenko 	return err;
30910a708f8fSGustavo F. Padovan }
30920a708f8fSGustavo F. Padovan 
309347d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan)
30940a708f8fSGustavo F. Padovan {
30950a708f8fSGustavo F. Padovan 	/* FCS is enabled only in ERTM or streaming mode, if one or both
30960a708f8fSGustavo F. Padovan 	 * sides request it.
30970a708f8fSGustavo F. Padovan 	 */
30980c1bc5c6SGustavo F. Padovan 	if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
309947d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_NONE;
3100c1360a1cSGustavo F. Padovan 	else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state))
310147d1ec61SGustavo F. Padovan 		chan->fcs = L2CAP_FCS_CRC16;
31020a708f8fSGustavo F. Padovan }
31030a708f8fSGustavo F. Padovan 
31040a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
31050a708f8fSGustavo F. Padovan {
31060a708f8fSGustavo F. Padovan 	struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
31070a708f8fSGustavo F. Padovan 	u16 dcid, flags;
31080a708f8fSGustavo F. Padovan 	u8 rsp[64];
310948454079SGustavo F. Padovan 	struct l2cap_chan *chan;
31103c588192SMat Martineau 	int len, err = 0;
31110a708f8fSGustavo F. Padovan 
31120a708f8fSGustavo F. Padovan 	dcid  = __le16_to_cpu(req->dcid);
31130a708f8fSGustavo F. Padovan 	flags = __le16_to_cpu(req->flags);
31140a708f8fSGustavo F. Padovan 
31150a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
31160a708f8fSGustavo F. Padovan 
3117baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, dcid);
311848454079SGustavo F. Padovan 	if (!chan)
31190a708f8fSGustavo F. Padovan 		return -ENOENT;
31200a708f8fSGustavo F. Padovan 
31216be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
312248454079SGustavo F. Padovan 
3123033b1142SDavid S. Miller 	if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
3124e2fd318eSIlia Kolomisnky 		struct l2cap_cmd_rej_cid rej;
31250a708f8fSGustavo F. Padovan 
3126e2fd318eSIlia Kolomisnky 		rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
3127e2fd318eSIlia Kolomisnky 		rej.scid = cpu_to_le16(chan->scid);
3128e2fd318eSIlia Kolomisnky 		rej.dcid = cpu_to_le16(chan->dcid);
3129e2fd318eSIlia Kolomisnky 
31300a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
31310a708f8fSGustavo F. Padovan 				sizeof(rej), &rej);
31320a708f8fSGustavo F. Padovan 		goto unlock;
31330a708f8fSGustavo F. Padovan 	}
31340a708f8fSGustavo F. Padovan 
31350a708f8fSGustavo F. Padovan 	/* Reject if config buffer is too small. */
31360a708f8fSGustavo F. Padovan 	len = cmd_len - sizeof(*req);
31377ac28817SDan Rosenberg 	if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) {
31380a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
3139fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
31400a708f8fSGustavo F. Padovan 					L2CAP_CONF_REJECT, flags), rsp);
31410a708f8fSGustavo F. Padovan 		goto unlock;
31420a708f8fSGustavo F. Padovan 	}
31430a708f8fSGustavo F. Padovan 
31440a708f8fSGustavo F. Padovan 	/* Store config. */
314573ffa904SGustavo F. Padovan 	memcpy(chan->conf_req + chan->conf_len, req->data, len);
314673ffa904SGustavo F. Padovan 	chan->conf_len += len;
31470a708f8fSGustavo F. Padovan 
31480a708f8fSGustavo F. Padovan 	if (flags & 0x0001) {
31490a708f8fSGustavo F. Padovan 		/* Incomplete config. Send empty response. */
31500a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
3151fe4128e0SGustavo F. Padovan 				l2cap_build_conf_rsp(chan, rsp,
31520a708f8fSGustavo F. Padovan 					L2CAP_CONF_SUCCESS, 0x0001), rsp);
31530a708f8fSGustavo F. Padovan 		goto unlock;
31540a708f8fSGustavo F. Padovan 	}
31550a708f8fSGustavo F. Padovan 
31560a708f8fSGustavo F. Padovan 	/* Complete config. */
315773ffa904SGustavo F. Padovan 	len = l2cap_parse_conf_req(chan, rsp);
31580a708f8fSGustavo F. Padovan 	if (len < 0) {
3159e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
31600a708f8fSGustavo F. Padovan 		goto unlock;
31610a708f8fSGustavo F. Padovan 	}
31620a708f8fSGustavo F. Padovan 
31630a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
316473ffa904SGustavo F. Padovan 	chan->num_conf_rsp++;
31650a708f8fSGustavo F. Padovan 
31660a708f8fSGustavo F. Padovan 	/* Reset config buffer. */
316773ffa904SGustavo F. Padovan 	chan->conf_len = 0;
31680a708f8fSGustavo F. Padovan 
3169c1360a1cSGustavo F. Padovan 	if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
31700a708f8fSGustavo F. Padovan 		goto unlock;
31710a708f8fSGustavo F. Padovan 
3172c1360a1cSGustavo F. Padovan 	if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
317347d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
31740a708f8fSGustavo F. Padovan 
317589bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
31760a708f8fSGustavo F. Padovan 
317742e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
317842e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
317958d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
31800c1bc5c6SGustavo F. Padovan 		if (chan->mode == L2CAP_MODE_ERTM)
31813c588192SMat Martineau 			err = l2cap_ertm_init(chan);
31820a708f8fSGustavo F. Padovan 
31833c588192SMat Martineau 		if (err < 0)
31843c588192SMat Martineau 			l2cap_send_disconn_req(chan->conn, chan, -err);
31853c588192SMat Martineau 		else
3186cf4cd009SAndrei Emeltchenko 			l2cap_chan_ready(chan);
31873c588192SMat Martineau 
31880a708f8fSGustavo F. Padovan 		goto unlock;
31890a708f8fSGustavo F. Padovan 	}
31900a708f8fSGustavo F. Padovan 
3191c1360a1cSGustavo F. Padovan 	if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
31920a708f8fSGustavo F. Padovan 		u8 buf[64];
31930a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
319473ffa904SGustavo F. Padovan 					l2cap_build_conf_req(chan, buf), buf);
319573ffa904SGustavo F. Padovan 		chan->num_conf_req++;
31960a708f8fSGustavo F. Padovan 	}
31970a708f8fSGustavo F. Padovan 
31980e8b207eSAndrei Emeltchenko 	/* Got Conf Rsp PENDING from remote side and asume we sent
31990e8b207eSAndrei Emeltchenko 	   Conf Rsp PENDING in the code above */
32000e8b207eSAndrei Emeltchenko 	if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
32010e8b207eSAndrei Emeltchenko 			test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
32020e8b207eSAndrei Emeltchenko 
32030e8b207eSAndrei Emeltchenko 		/* check compatibility */
32040e8b207eSAndrei Emeltchenko 
32050e8b207eSAndrei Emeltchenko 		clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
32060e8b207eSAndrei Emeltchenko 		set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
32070e8b207eSAndrei Emeltchenko 
32080e8b207eSAndrei Emeltchenko 		l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
32090e8b207eSAndrei Emeltchenko 					l2cap_build_conf_rsp(chan, rsp,
32100e8b207eSAndrei Emeltchenko 					L2CAP_CONF_SUCCESS, 0x0000), rsp);
32110e8b207eSAndrei Emeltchenko 	}
32120e8b207eSAndrei Emeltchenko 
32130a708f8fSGustavo F. Padovan unlock:
32146be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
32153c588192SMat Martineau 	return err;
32160a708f8fSGustavo F. Padovan }
32170a708f8fSGustavo F. Padovan 
32180a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
32190a708f8fSGustavo F. Padovan {
32200a708f8fSGustavo F. Padovan 	struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
32210a708f8fSGustavo F. Padovan 	u16 scid, flags, result;
322248454079SGustavo F. Padovan 	struct l2cap_chan *chan;
322361386cbaSAndrei Emeltchenko 	int len = le16_to_cpu(cmd->len) - sizeof(*rsp);
32243c588192SMat Martineau 	int err = 0;
32250a708f8fSGustavo F. Padovan 
32260a708f8fSGustavo F. Padovan 	scid   = __le16_to_cpu(rsp->scid);
32270a708f8fSGustavo F. Padovan 	flags  = __le16_to_cpu(rsp->flags);
32280a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
32290a708f8fSGustavo F. Padovan 
323061386cbaSAndrei Emeltchenko 	BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
323161386cbaSAndrei Emeltchenko 	       result, len);
32320a708f8fSGustavo F. Padovan 
3233baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, scid);
323448454079SGustavo F. Padovan 	if (!chan)
32350a708f8fSGustavo F. Padovan 		return 0;
32360a708f8fSGustavo F. Padovan 
32376be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
323848454079SGustavo F. Padovan 
32390a708f8fSGustavo F. Padovan 	switch (result) {
32400a708f8fSGustavo F. Padovan 	case L2CAP_CONF_SUCCESS:
324147d1ec61SGustavo F. Padovan 		l2cap_conf_rfc_get(chan, rsp->data, len);
32420e8b207eSAndrei Emeltchenko 		clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
32430a708f8fSGustavo F. Padovan 		break;
32440a708f8fSGustavo F. Padovan 
32450e8b207eSAndrei Emeltchenko 	case L2CAP_CONF_PENDING:
32460e8b207eSAndrei Emeltchenko 		set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
32470e8b207eSAndrei Emeltchenko 
32480e8b207eSAndrei Emeltchenko 		if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
32490e8b207eSAndrei Emeltchenko 			char buf[64];
32500e8b207eSAndrei Emeltchenko 
32510e8b207eSAndrei Emeltchenko 			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
32520e8b207eSAndrei Emeltchenko 								buf, &result);
32530e8b207eSAndrei Emeltchenko 			if (len < 0) {
32540e8b207eSAndrei Emeltchenko 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
32550e8b207eSAndrei Emeltchenko 				goto done;
32560e8b207eSAndrei Emeltchenko 			}
32570e8b207eSAndrei Emeltchenko 
32580e8b207eSAndrei Emeltchenko 			/* check compatibility */
32590e8b207eSAndrei Emeltchenko 
32600e8b207eSAndrei Emeltchenko 			clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
32610e8b207eSAndrei Emeltchenko 			set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
32620e8b207eSAndrei Emeltchenko 
32630e8b207eSAndrei Emeltchenko 			l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
32640e8b207eSAndrei Emeltchenko 						l2cap_build_conf_rsp(chan, buf,
32650e8b207eSAndrei Emeltchenko 						L2CAP_CONF_SUCCESS, 0x0000), buf);
32660e8b207eSAndrei Emeltchenko 		}
32670e8b207eSAndrei Emeltchenko 		goto done;
32680e8b207eSAndrei Emeltchenko 
32690a708f8fSGustavo F. Padovan 	case L2CAP_CONF_UNACCEPT:
327073ffa904SGustavo F. Padovan 		if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
32710a708f8fSGustavo F. Padovan 			char req[64];
32720a708f8fSGustavo F. Padovan 
32730a708f8fSGustavo F. Padovan 			if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
3274e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
32750a708f8fSGustavo F. Padovan 				goto done;
32760a708f8fSGustavo F. Padovan 			}
32770a708f8fSGustavo F. Padovan 
32780a708f8fSGustavo F. Padovan 			/* throw out any old stored conf requests */
32790a708f8fSGustavo F. Padovan 			result = L2CAP_CONF_SUCCESS;
3280b4450035SGustavo F. Padovan 			len = l2cap_parse_conf_rsp(chan, rsp->data, len,
3281b4450035SGustavo F. Padovan 								req, &result);
32820a708f8fSGustavo F. Padovan 			if (len < 0) {
3283e92c8e70SGustavo F. Padovan 				l2cap_send_disconn_req(conn, chan, ECONNRESET);
32840a708f8fSGustavo F. Padovan 				goto done;
32850a708f8fSGustavo F. Padovan 			}
32860a708f8fSGustavo F. Padovan 
32870a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, l2cap_get_ident(conn),
32880a708f8fSGustavo F. Padovan 						L2CAP_CONF_REQ, len, req);
328973ffa904SGustavo F. Padovan 			chan->num_conf_req++;
32900a708f8fSGustavo F. Padovan 			if (result != L2CAP_CONF_SUCCESS)
32910a708f8fSGustavo F. Padovan 				goto done;
32920a708f8fSGustavo F. Padovan 			break;
32930a708f8fSGustavo F. Padovan 		}
32940a708f8fSGustavo F. Padovan 
32950a708f8fSGustavo F. Padovan 	default:
32966be36555SAndrei Emeltchenko 		l2cap_chan_set_err(chan, ECONNRESET);
32972e0052e4SAndrei Emeltchenko 
3298ba13ccd9SMarcel Holtmann 		__set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
3299e92c8e70SGustavo F. Padovan 		l2cap_send_disconn_req(conn, chan, ECONNRESET);
33000a708f8fSGustavo F. Padovan 		goto done;
33010a708f8fSGustavo F. Padovan 	}
33020a708f8fSGustavo F. Padovan 
33030a708f8fSGustavo F. Padovan 	if (flags & 0x01)
33040a708f8fSGustavo F. Padovan 		goto done;
33050a708f8fSGustavo F. Padovan 
3306c1360a1cSGustavo F. Padovan 	set_bit(CONF_INPUT_DONE, &chan->conf_state);
33070a708f8fSGustavo F. Padovan 
3308c1360a1cSGustavo F. Padovan 	if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
330947d1ec61SGustavo F. Padovan 		set_default_fcs(chan);
33100a708f8fSGustavo F. Padovan 
331189bc500eSGustavo F. Padovan 		l2cap_state_change(chan, BT_CONNECTED);
331242e5c802SGustavo F. Padovan 		chan->next_tx_seq = 0;
331342e5c802SGustavo F. Padovan 		chan->expected_tx_seq = 0;
331458d35f87SGustavo F. Padovan 		skb_queue_head_init(&chan->tx_q);
33150c1bc5c6SGustavo F. Padovan 		if (chan->mode ==  L2CAP_MODE_ERTM)
33163c588192SMat Martineau 			err = l2cap_ertm_init(chan);
33170a708f8fSGustavo F. Padovan 
33183c588192SMat Martineau 		if (err < 0)
33193c588192SMat Martineau 			l2cap_send_disconn_req(chan->conn, chan, -err);
33203c588192SMat Martineau 		else
3321cf4cd009SAndrei Emeltchenko 			l2cap_chan_ready(chan);
33220a708f8fSGustavo F. Padovan 	}
33230a708f8fSGustavo F. Padovan 
33240a708f8fSGustavo F. Padovan done:
33256be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
33263c588192SMat Martineau 	return err;
33270a708f8fSGustavo F. Padovan }
33280a708f8fSGustavo F. Padovan 
33290a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
33300a708f8fSGustavo F. Padovan {
33310a708f8fSGustavo F. Padovan 	struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
33320a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp rsp;
33330a708f8fSGustavo F. Padovan 	u16 dcid, scid;
333448454079SGustavo F. Padovan 	struct l2cap_chan *chan;
33350a708f8fSGustavo F. Padovan 	struct sock *sk;
33360a708f8fSGustavo F. Padovan 
33370a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(req->scid);
33380a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(req->dcid);
33390a708f8fSGustavo F. Padovan 
33400a708f8fSGustavo F. Padovan 	BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
33410a708f8fSGustavo F. Padovan 
33423df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
33433df91ea2SAndrei Emeltchenko 
33443df91ea2SAndrei Emeltchenko 	chan = __l2cap_get_chan_by_scid(conn, dcid);
33453df91ea2SAndrei Emeltchenko 	if (!chan) {
33463df91ea2SAndrei Emeltchenko 		mutex_unlock(&conn->chan_lock);
33470a708f8fSGustavo F. Padovan 		return 0;
33483df91ea2SAndrei Emeltchenko 	}
33490a708f8fSGustavo F. Padovan 
33506be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
33516be36555SAndrei Emeltchenko 
335248454079SGustavo F. Padovan 	sk = chan->sk;
335348454079SGustavo F. Padovan 
3354fe4128e0SGustavo F. Padovan 	rsp.dcid = cpu_to_le16(chan->scid);
3355fe4128e0SGustavo F. Padovan 	rsp.scid = cpu_to_le16(chan->dcid);
33560a708f8fSGustavo F. Padovan 	l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
33570a708f8fSGustavo F. Padovan 
33586be36555SAndrei Emeltchenko 	lock_sock(sk);
33590a708f8fSGustavo F. Padovan 	sk->sk_shutdown = SHUTDOWN_MASK;
33606be36555SAndrei Emeltchenko 	release_sock(sk);
33610a708f8fSGustavo F. Padovan 
336248454079SGustavo F. Padovan 	l2cap_chan_del(chan, ECONNRESET);
33636be36555SAndrei Emeltchenko 
33646be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
33650a708f8fSGustavo F. Padovan 
3366ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
33673df91ea2SAndrei Emeltchenko 
33683df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
33693df91ea2SAndrei Emeltchenko 
33700a708f8fSGustavo F. Padovan 	return 0;
33710a708f8fSGustavo F. Padovan }
33720a708f8fSGustavo F. Padovan 
33730a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
33740a708f8fSGustavo F. Padovan {
33750a708f8fSGustavo F. Padovan 	struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
33760a708f8fSGustavo F. Padovan 	u16 dcid, scid;
337748454079SGustavo F. Padovan 	struct l2cap_chan *chan;
33780a708f8fSGustavo F. Padovan 
33790a708f8fSGustavo F. Padovan 	scid = __le16_to_cpu(rsp->scid);
33800a708f8fSGustavo F. Padovan 	dcid = __le16_to_cpu(rsp->dcid);
33810a708f8fSGustavo F. Padovan 
33820a708f8fSGustavo F. Padovan 	BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
33830a708f8fSGustavo F. Padovan 
33843df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
33853df91ea2SAndrei Emeltchenko 
33863df91ea2SAndrei Emeltchenko 	chan = __l2cap_get_chan_by_scid(conn, scid);
33873df91ea2SAndrei Emeltchenko 	if (!chan) {
33883df91ea2SAndrei Emeltchenko 		mutex_unlock(&conn->chan_lock);
33890a708f8fSGustavo F. Padovan 		return 0;
33903df91ea2SAndrei Emeltchenko 	}
33910a708f8fSGustavo F. Padovan 
33926be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
339348454079SGustavo F. Padovan 
339448454079SGustavo F. Padovan 	l2cap_chan_del(chan, 0);
33956be36555SAndrei Emeltchenko 
33966be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
33970a708f8fSGustavo F. Padovan 
3398ba3bd0eeSGustavo F. Padovan 	chan->ops->close(chan->data);
33993df91ea2SAndrei Emeltchenko 
34003df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
34013df91ea2SAndrei Emeltchenko 
34020a708f8fSGustavo F. Padovan 	return 0;
34030a708f8fSGustavo F. Padovan }
34040a708f8fSGustavo F. Padovan 
34050a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
34060a708f8fSGustavo F. Padovan {
34070a708f8fSGustavo F. Padovan 	struct l2cap_info_req *req = (struct l2cap_info_req *) data;
34080a708f8fSGustavo F. Padovan 	u16 type;
34090a708f8fSGustavo F. Padovan 
34100a708f8fSGustavo F. Padovan 	type = __le16_to_cpu(req->type);
34110a708f8fSGustavo F. Padovan 
34120a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x", type);
34130a708f8fSGustavo F. Padovan 
34140a708f8fSGustavo F. Padovan 	if (type == L2CAP_IT_FEAT_MASK) {
34150a708f8fSGustavo F. Padovan 		u8 buf[8];
34160a708f8fSGustavo F. Padovan 		u32 feat_mask = l2cap_feat_mask;
34170a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
34180a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FEAT_MASK);
34190a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
34200a708f8fSGustavo F. Padovan 		if (!disable_ertm)
34210a708f8fSGustavo F. Padovan 			feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
34220a708f8fSGustavo F. Padovan 							 | L2CAP_FEAT_FCS;
3423a5fd6f30SAndrei Emeltchenko 		if (enable_hs)
34246327eb98SAndrei Emeltchenko 			feat_mask |= L2CAP_FEAT_EXT_FLOW
34256327eb98SAndrei Emeltchenko 						| L2CAP_FEAT_EXT_WINDOW;
3426a5fd6f30SAndrei Emeltchenko 
34270a708f8fSGustavo F. Padovan 		put_unaligned_le32(feat_mask, rsp->data);
34280a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
34290a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
34300a708f8fSGustavo F. Padovan 	} else if (type == L2CAP_IT_FIXED_CHAN) {
34310a708f8fSGustavo F. Padovan 		u8 buf[12];
34320a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
343350a147cdSMat Martineau 
343450a147cdSMat Martineau 		if (enable_hs)
343550a147cdSMat Martineau 			l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
343650a147cdSMat Martineau 		else
343750a147cdSMat Martineau 			l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;
343850a147cdSMat Martineau 
34390a708f8fSGustavo F. Padovan 		rsp->type   = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
34400a708f8fSGustavo F. Padovan 		rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
3441c6337ea6SAndrei Emeltchenko 		memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan));
34420a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
34430a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(buf), buf);
34440a708f8fSGustavo F. Padovan 	} else {
34450a708f8fSGustavo F. Padovan 		struct l2cap_info_rsp rsp;
34460a708f8fSGustavo F. Padovan 		rsp.type   = cpu_to_le16(type);
34470a708f8fSGustavo F. Padovan 		rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
34480a708f8fSGustavo F. Padovan 		l2cap_send_cmd(conn, cmd->ident,
34490a708f8fSGustavo F. Padovan 					L2CAP_INFO_RSP, sizeof(rsp), &rsp);
34500a708f8fSGustavo F. Padovan 	}
34510a708f8fSGustavo F. Padovan 
34520a708f8fSGustavo F. Padovan 	return 0;
34530a708f8fSGustavo F. Padovan }
34540a708f8fSGustavo F. Padovan 
34550a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data)
34560a708f8fSGustavo F. Padovan {
34570a708f8fSGustavo F. Padovan 	struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
34580a708f8fSGustavo F. Padovan 	u16 type, result;
34590a708f8fSGustavo F. Padovan 
34600a708f8fSGustavo F. Padovan 	type   = __le16_to_cpu(rsp->type);
34610a708f8fSGustavo F. Padovan 	result = __le16_to_cpu(rsp->result);
34620a708f8fSGustavo F. Padovan 
34630a708f8fSGustavo F. Padovan 	BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
34640a708f8fSGustavo F. Padovan 
3465e90165beSAndrei Emeltchenko 	/* L2CAP Info req/rsp are unbound to channels, add extra checks */
3466e90165beSAndrei Emeltchenko 	if (cmd->ident != conn->info_ident ||
3467e90165beSAndrei Emeltchenko 			conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
3468e90165beSAndrei Emeltchenko 		return 0;
3469e90165beSAndrei Emeltchenko 
347017cd3f37SUlisses Furquim 	cancel_delayed_work(&conn->info_timer);
34710a708f8fSGustavo F. Padovan 
34720a708f8fSGustavo F. Padovan 	if (result != L2CAP_IR_SUCCESS) {
34730a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
34740a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
34750a708f8fSGustavo F. Padovan 
34760a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
34770a708f8fSGustavo F. Padovan 
34780a708f8fSGustavo F. Padovan 		return 0;
34790a708f8fSGustavo F. Padovan 	}
34800a708f8fSGustavo F. Padovan 
3481978c93b9SAndrei Emeltchenko 	switch (type) {
3482978c93b9SAndrei Emeltchenko 	case L2CAP_IT_FEAT_MASK:
34830a708f8fSGustavo F. Padovan 		conn->feat_mask = get_unaligned_le32(rsp->data);
34840a708f8fSGustavo F. Padovan 
34850a708f8fSGustavo F. Padovan 		if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
34860a708f8fSGustavo F. Padovan 			struct l2cap_info_req req;
34870a708f8fSGustavo F. Padovan 			req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
34880a708f8fSGustavo F. Padovan 
34890a708f8fSGustavo F. Padovan 			conn->info_ident = l2cap_get_ident(conn);
34900a708f8fSGustavo F. Padovan 
34910a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, conn->info_ident,
34920a708f8fSGustavo F. Padovan 					L2CAP_INFO_REQ, sizeof(req), &req);
34930a708f8fSGustavo F. Padovan 		} else {
34940a708f8fSGustavo F. Padovan 			conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
34950a708f8fSGustavo F. Padovan 			conn->info_ident = 0;
34960a708f8fSGustavo F. Padovan 
34970a708f8fSGustavo F. Padovan 			l2cap_conn_start(conn);
34980a708f8fSGustavo F. Padovan 		}
3499978c93b9SAndrei Emeltchenko 		break;
3500978c93b9SAndrei Emeltchenko 
3501978c93b9SAndrei Emeltchenko 	case L2CAP_IT_FIXED_CHAN:
3502978c93b9SAndrei Emeltchenko 		conn->fixed_chan_mask = rsp->data[0];
35030a708f8fSGustavo F. Padovan 		conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
35040a708f8fSGustavo F. Padovan 		conn->info_ident = 0;
35050a708f8fSGustavo F. Padovan 
35060a708f8fSGustavo F. Padovan 		l2cap_conn_start(conn);
3507978c93b9SAndrei Emeltchenko 		break;
35080a708f8fSGustavo F. Padovan 	}
35090a708f8fSGustavo F. Padovan 
35100a708f8fSGustavo F. Padovan 	return 0;
35110a708f8fSGustavo F. Padovan }
35120a708f8fSGustavo F. Padovan 
3513f94ff6ffSMat Martineau static inline int l2cap_create_channel_req(struct l2cap_conn *conn,
3514f94ff6ffSMat Martineau 					struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3515f94ff6ffSMat Martineau 					void *data)
3516f94ff6ffSMat Martineau {
3517f94ff6ffSMat Martineau 	struct l2cap_create_chan_req *req = data;
3518f94ff6ffSMat Martineau 	struct l2cap_create_chan_rsp rsp;
3519f94ff6ffSMat Martineau 	u16 psm, scid;
3520f94ff6ffSMat Martineau 
3521f94ff6ffSMat Martineau 	if (cmd_len != sizeof(*req))
3522f94ff6ffSMat Martineau 		return -EPROTO;
3523f94ff6ffSMat Martineau 
3524f94ff6ffSMat Martineau 	if (!enable_hs)
3525f94ff6ffSMat Martineau 		return -EINVAL;
3526f94ff6ffSMat Martineau 
3527f94ff6ffSMat Martineau 	psm = le16_to_cpu(req->psm);
3528f94ff6ffSMat Martineau 	scid = le16_to_cpu(req->scid);
3529f94ff6ffSMat Martineau 
3530f94ff6ffSMat Martineau 	BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id);
3531f94ff6ffSMat Martineau 
3532f94ff6ffSMat Martineau 	/* Placeholder: Always reject */
3533f94ff6ffSMat Martineau 	rsp.dcid = 0;
3534f94ff6ffSMat Martineau 	rsp.scid = cpu_to_le16(scid);
35358ce0c498SAndrei Emeltchenko 	rsp.result = __constant_cpu_to_le16(L2CAP_CR_NO_MEM);
35368ce0c498SAndrei Emeltchenko 	rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO);
3537f94ff6ffSMat Martineau 
3538f94ff6ffSMat Martineau 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
3539f94ff6ffSMat Martineau 		       sizeof(rsp), &rsp);
3540f94ff6ffSMat Martineau 
3541f94ff6ffSMat Martineau 	return 0;
3542f94ff6ffSMat Martineau }
3543f94ff6ffSMat Martineau 
3544f94ff6ffSMat Martineau static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn,
3545f94ff6ffSMat Martineau 					struct l2cap_cmd_hdr *cmd, void *data)
3546f94ff6ffSMat Martineau {
3547f94ff6ffSMat Martineau 	BT_DBG("conn %p", conn);
3548f94ff6ffSMat Martineau 
3549f94ff6ffSMat Martineau 	return l2cap_connect_rsp(conn, cmd, data);
3550f94ff6ffSMat Martineau }
3551f94ff6ffSMat Martineau 
35528d5a04a1SMat Martineau static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident,
35538d5a04a1SMat Martineau 							u16 icid, u16 result)
35548d5a04a1SMat Martineau {
35558d5a04a1SMat Martineau 	struct l2cap_move_chan_rsp rsp;
35568d5a04a1SMat Martineau 
35578d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
35588d5a04a1SMat Martineau 
35598d5a04a1SMat Martineau 	rsp.icid = cpu_to_le16(icid);
35608d5a04a1SMat Martineau 	rsp.result = cpu_to_le16(result);
35618d5a04a1SMat Martineau 
35628d5a04a1SMat Martineau 	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp);
35638d5a04a1SMat Martineau }
35648d5a04a1SMat Martineau 
35658d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn,
35668d5a04a1SMat Martineau 				struct l2cap_chan *chan, u16 icid, u16 result)
35678d5a04a1SMat Martineau {
35688d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm cfm;
35698d5a04a1SMat Martineau 	u8 ident;
35708d5a04a1SMat Martineau 
35718d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
35728d5a04a1SMat Martineau 
35738d5a04a1SMat Martineau 	ident = l2cap_get_ident(conn);
35748d5a04a1SMat Martineau 	if (chan)
35758d5a04a1SMat Martineau 		chan->ident = ident;
35768d5a04a1SMat Martineau 
35778d5a04a1SMat Martineau 	cfm.icid = cpu_to_le16(icid);
35788d5a04a1SMat Martineau 	cfm.result = cpu_to_le16(result);
35798d5a04a1SMat Martineau 
35808d5a04a1SMat Martineau 	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm);
35818d5a04a1SMat Martineau }
35828d5a04a1SMat Martineau 
35838d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
35848d5a04a1SMat Martineau 								u16 icid)
35858d5a04a1SMat Martineau {
35868d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm_rsp rsp;
35878d5a04a1SMat Martineau 
35888d5a04a1SMat Martineau 	BT_DBG("icid %d", icid);
35898d5a04a1SMat Martineau 
35908d5a04a1SMat Martineau 	rsp.icid = cpu_to_le16(icid);
35918d5a04a1SMat Martineau 	l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
35928d5a04a1SMat Martineau }
35938d5a04a1SMat Martineau 
35948d5a04a1SMat Martineau static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
35958d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
35968d5a04a1SMat Martineau {
35978d5a04a1SMat Martineau 	struct l2cap_move_chan_req *req = data;
35988d5a04a1SMat Martineau 	u16 icid = 0;
35998d5a04a1SMat Martineau 	u16 result = L2CAP_MR_NOT_ALLOWED;
36008d5a04a1SMat Martineau 
36018d5a04a1SMat Martineau 	if (cmd_len != sizeof(*req))
36028d5a04a1SMat Martineau 		return -EPROTO;
36038d5a04a1SMat Martineau 
36048d5a04a1SMat Martineau 	icid = le16_to_cpu(req->icid);
36058d5a04a1SMat Martineau 
36068d5a04a1SMat Martineau 	BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id);
36078d5a04a1SMat Martineau 
36088d5a04a1SMat Martineau 	if (!enable_hs)
36098d5a04a1SMat Martineau 		return -EINVAL;
36108d5a04a1SMat Martineau 
36118d5a04a1SMat Martineau 	/* Placeholder: Always refuse */
36128d5a04a1SMat Martineau 	l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result);
36138d5a04a1SMat Martineau 
36148d5a04a1SMat Martineau 	return 0;
36158d5a04a1SMat Martineau }
36168d5a04a1SMat Martineau 
36178d5a04a1SMat Martineau static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn,
36188d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
36198d5a04a1SMat Martineau {
36208d5a04a1SMat Martineau 	struct l2cap_move_chan_rsp *rsp = data;
36218d5a04a1SMat Martineau 	u16 icid, result;
36228d5a04a1SMat Martineau 
36238d5a04a1SMat Martineau 	if (cmd_len != sizeof(*rsp))
36248d5a04a1SMat Martineau 		return -EPROTO;
36258d5a04a1SMat Martineau 
36268d5a04a1SMat Martineau 	icid = le16_to_cpu(rsp->icid);
36278d5a04a1SMat Martineau 	result = le16_to_cpu(rsp->result);
36288d5a04a1SMat Martineau 
36298d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
36308d5a04a1SMat Martineau 
36318d5a04a1SMat Martineau 	/* Placeholder: Always unconfirmed */
36328d5a04a1SMat Martineau 	l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED);
36338d5a04a1SMat Martineau 
36348d5a04a1SMat Martineau 	return 0;
36358d5a04a1SMat Martineau }
36368d5a04a1SMat Martineau 
36378d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn,
36388d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
36398d5a04a1SMat Martineau {
36408d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm *cfm = data;
36418d5a04a1SMat Martineau 	u16 icid, result;
36428d5a04a1SMat Martineau 
36438d5a04a1SMat Martineau 	if (cmd_len != sizeof(*cfm))
36448d5a04a1SMat Martineau 		return -EPROTO;
36458d5a04a1SMat Martineau 
36468d5a04a1SMat Martineau 	icid = le16_to_cpu(cfm->icid);
36478d5a04a1SMat Martineau 	result = le16_to_cpu(cfm->result);
36488d5a04a1SMat Martineau 
36498d5a04a1SMat Martineau 	BT_DBG("icid %d, result %d", icid, result);
36508d5a04a1SMat Martineau 
36518d5a04a1SMat Martineau 	l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
36528d5a04a1SMat Martineau 
36538d5a04a1SMat Martineau 	return 0;
36548d5a04a1SMat Martineau }
36558d5a04a1SMat Martineau 
36568d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
36578d5a04a1SMat Martineau 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data)
36588d5a04a1SMat Martineau {
36598d5a04a1SMat Martineau 	struct l2cap_move_chan_cfm_rsp *rsp = data;
36608d5a04a1SMat Martineau 	u16 icid;
36618d5a04a1SMat Martineau 
36628d5a04a1SMat Martineau 	if (cmd_len != sizeof(*rsp))
36638d5a04a1SMat Martineau 		return -EPROTO;
36648d5a04a1SMat Martineau 
36658d5a04a1SMat Martineau 	icid = le16_to_cpu(rsp->icid);
36668d5a04a1SMat Martineau 
36678d5a04a1SMat Martineau 	BT_DBG("icid %d", icid);
36688d5a04a1SMat Martineau 
36698d5a04a1SMat Martineau 	return 0;
36708d5a04a1SMat Martineau }
36718d5a04a1SMat Martineau 
3672e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
3673de73115aSClaudio Takahasi 							u16 to_multiplier)
3674de73115aSClaudio Takahasi {
3675de73115aSClaudio Takahasi 	u16 max_latency;
3676de73115aSClaudio Takahasi 
3677de73115aSClaudio Takahasi 	if (min > max || min < 6 || max > 3200)
3678de73115aSClaudio Takahasi 		return -EINVAL;
3679de73115aSClaudio Takahasi 
3680de73115aSClaudio Takahasi 	if (to_multiplier < 10 || to_multiplier > 3200)
3681de73115aSClaudio Takahasi 		return -EINVAL;
3682de73115aSClaudio Takahasi 
3683de73115aSClaudio Takahasi 	if (max >= to_multiplier * 8)
3684de73115aSClaudio Takahasi 		return -EINVAL;
3685de73115aSClaudio Takahasi 
3686de73115aSClaudio Takahasi 	max_latency = (to_multiplier * 8 / max) - 1;
3687de73115aSClaudio Takahasi 	if (latency > 499 || latency > max_latency)
3688de73115aSClaudio Takahasi 		return -EINVAL;
3689de73115aSClaudio Takahasi 
3690de73115aSClaudio Takahasi 	return 0;
3691de73115aSClaudio Takahasi }
3692de73115aSClaudio Takahasi 
3693de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
3694de73115aSClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
3695de73115aSClaudio Takahasi {
3696de73115aSClaudio Takahasi 	struct hci_conn *hcon = conn->hcon;
3697de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_req *req;
3698de73115aSClaudio Takahasi 	struct l2cap_conn_param_update_rsp rsp;
3699de73115aSClaudio Takahasi 	u16 min, max, latency, to_multiplier, cmd_len;
37002ce603ebSClaudio Takahasi 	int err;
3701de73115aSClaudio Takahasi 
3702de73115aSClaudio Takahasi 	if (!(hcon->link_mode & HCI_LM_MASTER))
3703de73115aSClaudio Takahasi 		return -EINVAL;
3704de73115aSClaudio Takahasi 
3705de73115aSClaudio Takahasi 	cmd_len = __le16_to_cpu(cmd->len);
3706de73115aSClaudio Takahasi 	if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
3707de73115aSClaudio Takahasi 		return -EPROTO;
3708de73115aSClaudio Takahasi 
3709de73115aSClaudio Takahasi 	req = (struct l2cap_conn_param_update_req *) data;
3710de73115aSClaudio Takahasi 	min		= __le16_to_cpu(req->min);
3711de73115aSClaudio Takahasi 	max		= __le16_to_cpu(req->max);
3712de73115aSClaudio Takahasi 	latency		= __le16_to_cpu(req->latency);
3713de73115aSClaudio Takahasi 	to_multiplier	= __le16_to_cpu(req->to_multiplier);
3714de73115aSClaudio Takahasi 
3715de73115aSClaudio Takahasi 	BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
3716de73115aSClaudio Takahasi 						min, max, latency, to_multiplier);
3717de73115aSClaudio Takahasi 
3718de73115aSClaudio Takahasi 	memset(&rsp, 0, sizeof(rsp));
37192ce603ebSClaudio Takahasi 
37202ce603ebSClaudio Takahasi 	err = l2cap_check_conn_param(min, max, latency, to_multiplier);
37212ce603ebSClaudio Takahasi 	if (err)
3722de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
3723de73115aSClaudio Takahasi 	else
3724de73115aSClaudio Takahasi 		rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
3725de73115aSClaudio Takahasi 
3726de73115aSClaudio Takahasi 	l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
3727de73115aSClaudio Takahasi 							sizeof(rsp), &rsp);
3728de73115aSClaudio Takahasi 
37292ce603ebSClaudio Takahasi 	if (!err)
37302ce603ebSClaudio Takahasi 		hci_le_conn_update(hcon, min, max, latency, to_multiplier);
37312ce603ebSClaudio Takahasi 
3732de73115aSClaudio Takahasi 	return 0;
3733de73115aSClaudio Takahasi }
3734de73115aSClaudio Takahasi 
37353300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
37363300d9a9SClaudio Takahasi 			struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
37373300d9a9SClaudio Takahasi {
37383300d9a9SClaudio Takahasi 	int err = 0;
37393300d9a9SClaudio Takahasi 
37403300d9a9SClaudio Takahasi 	switch (cmd->code) {
37413300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
37423300d9a9SClaudio Takahasi 		l2cap_command_rej(conn, cmd, data);
37433300d9a9SClaudio Takahasi 		break;
37443300d9a9SClaudio Takahasi 
37453300d9a9SClaudio Takahasi 	case L2CAP_CONN_REQ:
37463300d9a9SClaudio Takahasi 		err = l2cap_connect_req(conn, cmd, data);
37473300d9a9SClaudio Takahasi 		break;
37483300d9a9SClaudio Takahasi 
37493300d9a9SClaudio Takahasi 	case L2CAP_CONN_RSP:
37503300d9a9SClaudio Takahasi 		err = l2cap_connect_rsp(conn, cmd, data);
37513300d9a9SClaudio Takahasi 		break;
37523300d9a9SClaudio Takahasi 
37533300d9a9SClaudio Takahasi 	case L2CAP_CONF_REQ:
37543300d9a9SClaudio Takahasi 		err = l2cap_config_req(conn, cmd, cmd_len, data);
37553300d9a9SClaudio Takahasi 		break;
37563300d9a9SClaudio Takahasi 
37573300d9a9SClaudio Takahasi 	case L2CAP_CONF_RSP:
37583300d9a9SClaudio Takahasi 		err = l2cap_config_rsp(conn, cmd, data);
37593300d9a9SClaudio Takahasi 		break;
37603300d9a9SClaudio Takahasi 
37613300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_REQ:
37623300d9a9SClaudio Takahasi 		err = l2cap_disconnect_req(conn, cmd, data);
37633300d9a9SClaudio Takahasi 		break;
37643300d9a9SClaudio Takahasi 
37653300d9a9SClaudio Takahasi 	case L2CAP_DISCONN_RSP:
37663300d9a9SClaudio Takahasi 		err = l2cap_disconnect_rsp(conn, cmd, data);
37673300d9a9SClaudio Takahasi 		break;
37683300d9a9SClaudio Takahasi 
37693300d9a9SClaudio Takahasi 	case L2CAP_ECHO_REQ:
37703300d9a9SClaudio Takahasi 		l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
37713300d9a9SClaudio Takahasi 		break;
37723300d9a9SClaudio Takahasi 
37733300d9a9SClaudio Takahasi 	case L2CAP_ECHO_RSP:
37743300d9a9SClaudio Takahasi 		break;
37753300d9a9SClaudio Takahasi 
37763300d9a9SClaudio Takahasi 	case L2CAP_INFO_REQ:
37773300d9a9SClaudio Takahasi 		err = l2cap_information_req(conn, cmd, data);
37783300d9a9SClaudio Takahasi 		break;
37793300d9a9SClaudio Takahasi 
37803300d9a9SClaudio Takahasi 	case L2CAP_INFO_RSP:
37813300d9a9SClaudio Takahasi 		err = l2cap_information_rsp(conn, cmd, data);
37823300d9a9SClaudio Takahasi 		break;
37833300d9a9SClaudio Takahasi 
3784f94ff6ffSMat Martineau 	case L2CAP_CREATE_CHAN_REQ:
3785f94ff6ffSMat Martineau 		err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
3786f94ff6ffSMat Martineau 		break;
3787f94ff6ffSMat Martineau 
3788f94ff6ffSMat Martineau 	case L2CAP_CREATE_CHAN_RSP:
3789f94ff6ffSMat Martineau 		err = l2cap_create_channel_rsp(conn, cmd, data);
3790f94ff6ffSMat Martineau 		break;
3791f94ff6ffSMat Martineau 
37928d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_REQ:
37938d5a04a1SMat Martineau 		err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
37948d5a04a1SMat Martineau 		break;
37958d5a04a1SMat Martineau 
37968d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_RSP:
37978d5a04a1SMat Martineau 		err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
37988d5a04a1SMat Martineau 		break;
37998d5a04a1SMat Martineau 
38008d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_CFM:
38018d5a04a1SMat Martineau 		err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
38028d5a04a1SMat Martineau 		break;
38038d5a04a1SMat Martineau 
38048d5a04a1SMat Martineau 	case L2CAP_MOVE_CHAN_CFM_RSP:
38058d5a04a1SMat Martineau 		err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
38068d5a04a1SMat Martineau 		break;
38078d5a04a1SMat Martineau 
38083300d9a9SClaudio Takahasi 	default:
38093300d9a9SClaudio Takahasi 		BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
38103300d9a9SClaudio Takahasi 		err = -EINVAL;
38113300d9a9SClaudio Takahasi 		break;
38123300d9a9SClaudio Takahasi 	}
38133300d9a9SClaudio Takahasi 
38143300d9a9SClaudio Takahasi 	return err;
38153300d9a9SClaudio Takahasi }
38163300d9a9SClaudio Takahasi 
38173300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
38183300d9a9SClaudio Takahasi 					struct l2cap_cmd_hdr *cmd, u8 *data)
38193300d9a9SClaudio Takahasi {
38203300d9a9SClaudio Takahasi 	switch (cmd->code) {
38213300d9a9SClaudio Takahasi 	case L2CAP_COMMAND_REJ:
38223300d9a9SClaudio Takahasi 		return 0;
38233300d9a9SClaudio Takahasi 
38243300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_REQ:
3825de73115aSClaudio Takahasi 		return l2cap_conn_param_update_req(conn, cmd, data);
38263300d9a9SClaudio Takahasi 
38273300d9a9SClaudio Takahasi 	case L2CAP_CONN_PARAM_UPDATE_RSP:
38283300d9a9SClaudio Takahasi 		return 0;
38293300d9a9SClaudio Takahasi 
38303300d9a9SClaudio Takahasi 	default:
38313300d9a9SClaudio Takahasi 		BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
38323300d9a9SClaudio Takahasi 		return -EINVAL;
38333300d9a9SClaudio Takahasi 	}
38343300d9a9SClaudio Takahasi }
38353300d9a9SClaudio Takahasi 
38363300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn,
38373300d9a9SClaudio Takahasi 							struct sk_buff *skb)
38380a708f8fSGustavo F. Padovan {
38390a708f8fSGustavo F. Padovan 	u8 *data = skb->data;
38400a708f8fSGustavo F. Padovan 	int len = skb->len;
38410a708f8fSGustavo F. Padovan 	struct l2cap_cmd_hdr cmd;
38423300d9a9SClaudio Takahasi 	int err;
38430a708f8fSGustavo F. Padovan 
38440a708f8fSGustavo F. Padovan 	l2cap_raw_recv(conn, skb);
38450a708f8fSGustavo F. Padovan 
38460a708f8fSGustavo F. Padovan 	while (len >= L2CAP_CMD_HDR_SIZE) {
38470a708f8fSGustavo F. Padovan 		u16 cmd_len;
38480a708f8fSGustavo F. Padovan 		memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
38490a708f8fSGustavo F. Padovan 		data += L2CAP_CMD_HDR_SIZE;
38500a708f8fSGustavo F. Padovan 		len  -= L2CAP_CMD_HDR_SIZE;
38510a708f8fSGustavo F. Padovan 
38520a708f8fSGustavo F. Padovan 		cmd_len = le16_to_cpu(cmd.len);
38530a708f8fSGustavo F. Padovan 
38540a708f8fSGustavo F. Padovan 		BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
38550a708f8fSGustavo F. Padovan 
38560a708f8fSGustavo F. Padovan 		if (cmd_len > len || !cmd.ident) {
38570a708f8fSGustavo F. Padovan 			BT_DBG("corrupted command");
38580a708f8fSGustavo F. Padovan 			break;
38590a708f8fSGustavo F. Padovan 		}
38600a708f8fSGustavo F. Padovan 
38613300d9a9SClaudio Takahasi 		if (conn->hcon->type == LE_LINK)
38623300d9a9SClaudio Takahasi 			err = l2cap_le_sig_cmd(conn, &cmd, data);
38633300d9a9SClaudio Takahasi 		else
38643300d9a9SClaudio Takahasi 			err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
38650a708f8fSGustavo F. Padovan 
38660a708f8fSGustavo F. Padovan 		if (err) {
3867e2fd318eSIlia Kolomisnky 			struct l2cap_cmd_rej_unk rej;
38682c6d1a2eSGustavo F. Padovan 
38692c6d1a2eSGustavo F. Padovan 			BT_ERR("Wrong link type (%d)", err);
38700a708f8fSGustavo F. Padovan 
38710a708f8fSGustavo F. Padovan 			/* FIXME: Map err to a valid reason */
3872e2fd318eSIlia Kolomisnky 			rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
38730a708f8fSGustavo F. Padovan 			l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
38740a708f8fSGustavo F. Padovan 		}
38750a708f8fSGustavo F. Padovan 
38760a708f8fSGustavo F. Padovan 		data += cmd_len;
38770a708f8fSGustavo F. Padovan 		len  -= cmd_len;
38780a708f8fSGustavo F. Padovan 	}
38790a708f8fSGustavo F. Padovan 
38800a708f8fSGustavo F. Padovan 	kfree_skb(skb);
38810a708f8fSGustavo F. Padovan }
38820a708f8fSGustavo F. Padovan 
388347d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan,  struct sk_buff *skb)
38840a708f8fSGustavo F. Padovan {
38850a708f8fSGustavo F. Padovan 	u16 our_fcs, rcv_fcs;
3886e4ca6d98SAndrei Emeltchenko 	int hdr_size;
3887e4ca6d98SAndrei Emeltchenko 
3888e4ca6d98SAndrei Emeltchenko 	if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3889e4ca6d98SAndrei Emeltchenko 		hdr_size = L2CAP_EXT_HDR_SIZE;
3890e4ca6d98SAndrei Emeltchenko 	else
3891e4ca6d98SAndrei Emeltchenko 		hdr_size = L2CAP_ENH_HDR_SIZE;
38920a708f8fSGustavo F. Padovan 
389347d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16) {
389403a51213SAndrei Emeltchenko 		skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
38950a708f8fSGustavo F. Padovan 		rcv_fcs = get_unaligned_le16(skb->data + skb->len);
38960a708f8fSGustavo F. Padovan 		our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
38970a708f8fSGustavo F. Padovan 
38980a708f8fSGustavo F. Padovan 		if (our_fcs != rcv_fcs)
38990a708f8fSGustavo F. Padovan 			return -EBADMSG;
39000a708f8fSGustavo F. Padovan 	}
39010a708f8fSGustavo F. Padovan 	return 0;
39020a708f8fSGustavo F. Padovan }
39030a708f8fSGustavo F. Padovan 
3904525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
39050a708f8fSGustavo F. Padovan {
390688843ab0SAndrei Emeltchenko 	u32 control = 0;
39070a708f8fSGustavo F. Padovan 
39086a026610SGustavo F. Padovan 	chan->frames_sent = 0;
39090a708f8fSGustavo F. Padovan 
39100b209faeSAndrei Emeltchenko 	control |= __set_reqseq(chan, chan->buffer_seq);
39110a708f8fSGustavo F. Padovan 
3912e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
3913ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR);
3914525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
3915e2ab4353SGustavo F. Padovan 		set_bit(CONN_RNR_SENT, &chan->conn_state);
39160a708f8fSGustavo F. Padovan 	}
39170a708f8fSGustavo F. Padovan 
3918e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
3919525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
39200a708f8fSGustavo F. Padovan 
3921525cd185SGustavo F. Padovan 	l2cap_ertm_send(chan);
39220a708f8fSGustavo F. Padovan 
3923e2ab4353SGustavo F. Padovan 	if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
39246a026610SGustavo F. Padovan 			chan->frames_sent == 0) {
3925ab784b73SAndrei Emeltchenko 		control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
3926525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
39270a708f8fSGustavo F. Padovan 	}
39280a708f8fSGustavo F. Padovan }
39290a708f8fSGustavo F. Padovan 
3930fb45de7dSAndrei Emeltchenko static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar)
39310a708f8fSGustavo F. Padovan {
39320a708f8fSGustavo F. Padovan 	struct sk_buff *next_skb;
39330a708f8fSGustavo F. Padovan 	int tx_seq_offset, next_tx_seq_offset;
39340a708f8fSGustavo F. Padovan 
39350a708f8fSGustavo F. Padovan 	bt_cb(skb)->tx_seq = tx_seq;
39360a708f8fSGustavo F. Padovan 	bt_cb(skb)->sar = sar;
39370a708f8fSGustavo F. Padovan 
3938f1c6775bSGustavo F. Padovan 	next_skb = skb_peek(&chan->srej_q);
39390a708f8fSGustavo F. Padovan 
3940836be934SAndrei Emeltchenko 	tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq);
39410a708f8fSGustavo F. Padovan 
3942039d9572SSzymon Janc 	while (next_skb) {
39430a708f8fSGustavo F. Padovan 		if (bt_cb(next_skb)->tx_seq == tx_seq)
39440a708f8fSGustavo F. Padovan 			return -EINVAL;
39450a708f8fSGustavo F. Padovan 
3946836be934SAndrei Emeltchenko 		next_tx_seq_offset = __seq_offset(chan,
3947836be934SAndrei Emeltchenko 				bt_cb(next_skb)->tx_seq, chan->buffer_seq);
39480a708f8fSGustavo F. Padovan 
39490a708f8fSGustavo F. Padovan 		if (next_tx_seq_offset > tx_seq_offset) {
3950f1c6775bSGustavo F. Padovan 			__skb_queue_before(&chan->srej_q, next_skb, skb);
39510a708f8fSGustavo F. Padovan 			return 0;
39520a708f8fSGustavo F. Padovan 		}
39530a708f8fSGustavo F. Padovan 
3954f1c6775bSGustavo F. Padovan 		if (skb_queue_is_last(&chan->srej_q, next_skb))
3955039d9572SSzymon Janc 			next_skb = NULL;
3956039d9572SSzymon Janc 		else
3957039d9572SSzymon Janc 			next_skb = skb_queue_next(&chan->srej_q, next_skb);
3958039d9572SSzymon Janc 	}
39590a708f8fSGustavo F. Padovan 
3960f1c6775bSGustavo F. Padovan 	__skb_queue_tail(&chan->srej_q, skb);
39610a708f8fSGustavo F. Padovan 
39620a708f8fSGustavo F. Padovan 	return 0;
39630a708f8fSGustavo F. Padovan }
39640a708f8fSGustavo F. Padovan 
396584084a31SMat Martineau static void append_skb_frag(struct sk_buff *skb,
396684084a31SMat Martineau 			struct sk_buff *new_frag, struct sk_buff **last_frag)
39670a708f8fSGustavo F. Padovan {
396884084a31SMat Martineau 	/* skb->len reflects data in skb as well as all fragments
396984084a31SMat Martineau 	 * skb->data_len reflects only data in fragments
397084084a31SMat Martineau 	 */
397184084a31SMat Martineau 	if (!skb_has_frag_list(skb))
397284084a31SMat Martineau 		skb_shinfo(skb)->frag_list = new_frag;
397384084a31SMat Martineau 
397484084a31SMat Martineau 	new_frag->next = NULL;
397584084a31SMat Martineau 
397684084a31SMat Martineau 	(*last_frag)->next = new_frag;
397784084a31SMat Martineau 	*last_frag = new_frag;
397884084a31SMat Martineau 
397984084a31SMat Martineau 	skb->len += new_frag->len;
398084084a31SMat Martineau 	skb->data_len += new_frag->len;
398184084a31SMat Martineau 	skb->truesize += new_frag->truesize;
398284084a31SMat Martineau }
398384084a31SMat Martineau 
398488843ab0SAndrei Emeltchenko static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u32 control)
398584084a31SMat Martineau {
398684084a31SMat Martineau 	int err = -EINVAL;
39870a708f8fSGustavo F. Padovan 
39887e0ef6eeSAndrei Emeltchenko 	switch (__get_ctrl_sar(chan, control)) {
39897e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_UNSEGMENTED:
399084084a31SMat Martineau 		if (chan->sdu)
399184084a31SMat Martineau 			break;
39920a708f8fSGustavo F. Padovan 
399384084a31SMat Martineau 		err = chan->ops->recv(chan->data, skb);
399484084a31SMat Martineau 		break;
39950a708f8fSGustavo F. Padovan 
39967e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_START:
399784084a31SMat Martineau 		if (chan->sdu)
399884084a31SMat Martineau 			break;
39990a708f8fSGustavo F. Padovan 
40006f61fd47SGustavo F. Padovan 		chan->sdu_len = get_unaligned_le16(skb->data);
400103a51213SAndrei Emeltchenko 		skb_pull(skb, L2CAP_SDULEN_SIZE);
40020a708f8fSGustavo F. Padovan 
400384084a31SMat Martineau 		if (chan->sdu_len > chan->imtu) {
400484084a31SMat Martineau 			err = -EMSGSIZE;
400584084a31SMat Martineau 			break;
400684084a31SMat Martineau 		}
40070a708f8fSGustavo F. Padovan 
400884084a31SMat Martineau 		if (skb->len >= chan->sdu_len)
400984084a31SMat Martineau 			break;
401084084a31SMat Martineau 
401184084a31SMat Martineau 		chan->sdu = skb;
401284084a31SMat Martineau 		chan->sdu_last_frag = skb;
401384084a31SMat Martineau 
401484084a31SMat Martineau 		skb = NULL;
401584084a31SMat Martineau 		err = 0;
40160a708f8fSGustavo F. Padovan 		break;
40170a708f8fSGustavo F. Padovan 
40187e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_CONTINUE:
40196f61fd47SGustavo F. Padovan 		if (!chan->sdu)
402084084a31SMat Martineau 			break;
40210a708f8fSGustavo F. Padovan 
402284084a31SMat Martineau 		append_skb_frag(chan->sdu, skb,
402384084a31SMat Martineau 				&chan->sdu_last_frag);
402484084a31SMat Martineau 		skb = NULL;
40250a708f8fSGustavo F. Padovan 
402684084a31SMat Martineau 		if (chan->sdu->len >= chan->sdu_len)
402784084a31SMat Martineau 			break;
40280a708f8fSGustavo F. Padovan 
402984084a31SMat Martineau 		err = 0;
40300a708f8fSGustavo F. Padovan 		break;
40310a708f8fSGustavo F. Padovan 
40327e0ef6eeSAndrei Emeltchenko 	case L2CAP_SAR_END:
40336f61fd47SGustavo F. Padovan 		if (!chan->sdu)
403484084a31SMat Martineau 			break;
40350a708f8fSGustavo F. Padovan 
403684084a31SMat Martineau 		append_skb_frag(chan->sdu, skb,
403784084a31SMat Martineau 				&chan->sdu_last_frag);
403884084a31SMat Martineau 		skb = NULL;
40390a708f8fSGustavo F. Padovan 
404084084a31SMat Martineau 		if (chan->sdu->len != chan->sdu_len)
404184084a31SMat Martineau 			break;
40420a708f8fSGustavo F. Padovan 
404384084a31SMat Martineau 		err = chan->ops->recv(chan->data, chan->sdu);
40440a708f8fSGustavo F. Padovan 
404584084a31SMat Martineau 		if (!err) {
404684084a31SMat Martineau 			/* Reassembly complete */
404784084a31SMat Martineau 			chan->sdu = NULL;
404884084a31SMat Martineau 			chan->sdu_last_frag = NULL;
404984084a31SMat Martineau 			chan->sdu_len = 0;
40500a708f8fSGustavo F. Padovan 		}
40510a708f8fSGustavo F. Padovan 		break;
40520a708f8fSGustavo F. Padovan 	}
40530a708f8fSGustavo F. Padovan 
405484084a31SMat Martineau 	if (err) {
40550a708f8fSGustavo F. Padovan 		kfree_skb(skb);
40566f61fd47SGustavo F. Padovan 		kfree_skb(chan->sdu);
40576f61fd47SGustavo F. Padovan 		chan->sdu = NULL;
405884084a31SMat Martineau 		chan->sdu_last_frag = NULL;
405984084a31SMat Martineau 		chan->sdu_len = 0;
406084084a31SMat Martineau 	}
40610a708f8fSGustavo F. Padovan 
406284084a31SMat Martineau 	return err;
40630a708f8fSGustavo F. Padovan }
40640a708f8fSGustavo F. Padovan 
406526f880d2SMat Martineau static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan)
40660a708f8fSGustavo F. Padovan {
406726f880d2SMat Martineau 	BT_DBG("chan %p, Enter local busy", chan);
406826f880d2SMat Martineau 
406926f880d2SMat Martineau 	set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
40703c588192SMat Martineau 	l2cap_seq_list_clear(&chan->srej_list);
407126f880d2SMat Martineau 
407277f918bcSSzymon Janc 	__set_ack_timer(chan);
40730a708f8fSGustavo F. Padovan }
40740a708f8fSGustavo F. Padovan 
407526f880d2SMat Martineau static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan)
407626f880d2SMat Martineau {
407788843ab0SAndrei Emeltchenko 	u32 control;
40780a708f8fSGustavo F. Padovan 
4079e2ab4353SGustavo F. Padovan 	if (!test_bit(CONN_RNR_SENT, &chan->conn_state))
40800a708f8fSGustavo F. Padovan 		goto done;
40810a708f8fSGustavo F. Padovan 
40820b209faeSAndrei Emeltchenko 	control = __set_reqseq(chan, chan->buffer_seq);
4083e3781735SAndrei Emeltchenko 	control |= __set_ctrl_poll(chan);
4084ab784b73SAndrei Emeltchenko 	control |= __set_ctrl_super(chan, L2CAP_SUPER_RR);
4085525cd185SGustavo F. Padovan 	l2cap_send_sframe(chan, control);
40866a026610SGustavo F. Padovan 	chan->retry_count = 1;
40870a708f8fSGustavo F. Padovan 
40881a09bcb9SGustavo F. Padovan 	__clear_retrans_timer(chan);
40891a09bcb9SGustavo F. Padovan 	__set_monitor_timer(chan);
40900a708f8fSGustavo F. Padovan 
4091e2ab4353SGustavo F. Padovan 	set_bit(CONN_WAIT_F, &chan->conn_state);
40920a708f8fSGustavo F. Padovan 
40930a708f8fSGustavo F. Padovan done:
4094e2ab4353SGustavo F. Padovan 	clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
4095e2ab4353SGustavo F. Padovan 	clear_bit(CONN_RNR_SENT, &chan->conn_state);
40960a708f8fSGustavo F. Padovan 
409749208c9cSGustavo F. Padovan 	BT_DBG("chan %p, Exit local busy", chan);
40980a708f8fSGustavo F. Padovan }
40990a708f8fSGustavo F. Padovan 
4100e328140fSMat Martineau void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
41010a708f8fSGustavo F. Padovan {
4102e328140fSMat Martineau 	if (chan->mode == L2CAP_MODE_ERTM) {
4103e328140fSMat Martineau 		if (busy)
410426f880d2SMat Martineau 			l2cap_ertm_enter_local_busy(chan);
4105e328140fSMat Martineau 		else
4106e328140fSMat Martineau 			l2cap_ertm_exit_local_busy(chan);
41070a708f8fSGustavo F. Padovan 	}
41080a708f8fSGustavo F. Padovan }
41090a708f8fSGustavo F. Padovan 
4110fb45de7dSAndrei Emeltchenko static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq)
41110a708f8fSGustavo F. Padovan {
41120a708f8fSGustavo F. Padovan 	struct sk_buff *skb;
411388843ab0SAndrei Emeltchenko 	u32 control;
41140a708f8fSGustavo F. Padovan 
4115e328140fSMat Martineau 	while ((skb = skb_peek(&chan->srej_q)) &&
4116e328140fSMat Martineau 			!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4117e328140fSMat Martineau 		int err;
4118e328140fSMat Martineau 
41190a708f8fSGustavo F. Padovan 		if (bt_cb(skb)->tx_seq != tx_seq)
41200a708f8fSGustavo F. Padovan 			break;
41210a708f8fSGustavo F. Padovan 
4122f1c6775bSGustavo F. Padovan 		skb = skb_dequeue(&chan->srej_q);
41237e0ef6eeSAndrei Emeltchenko 		control = __set_ctrl_sar(chan, bt_cb(skb)->sar);
412484084a31SMat Martineau 		err = l2cap_reassemble_sdu(chan, skb, control);
4125e328140fSMat Martineau 
4126e328140fSMat Martineau 		if (err < 0) {
4127e328140fSMat Martineau 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
4128e328140fSMat Martineau 			break;
4129e328140fSMat Martineau 		}
4130e328140fSMat Martineau 
4131836be934SAndrei Emeltchenko 		chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej);
4132836be934SAndrei Emeltchenko 		tx_seq = __next_seq(chan, tx_seq);
41330a708f8fSGustavo F. Padovan 	}
41340a708f8fSGustavo F. Padovan }
41350a708f8fSGustavo F. Padovan 
4136fb45de7dSAndrei Emeltchenko static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq)
41370a708f8fSGustavo F. Padovan {
41380a708f8fSGustavo F. Padovan 	struct srej_list *l, *tmp;
413988843ab0SAndrei Emeltchenko 	u32 control;
41400a708f8fSGustavo F. Padovan 
414139d5a3eeSGustavo F. Padovan 	list_for_each_entry_safe(l, tmp, &chan->srej_l, list) {
41420a708f8fSGustavo F. Padovan 		if (l->tx_seq == tx_seq) {
41430a708f8fSGustavo F. Padovan 			list_del(&l->list);
41440a708f8fSGustavo F. Padovan 			kfree(l);
41450a708f8fSGustavo F. Padovan 			return;
41460a708f8fSGustavo F. Padovan 		}
4147ab784b73SAndrei Emeltchenko 		control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
41480b209faeSAndrei Emeltchenko 		control |= __set_reqseq(chan, l->tx_seq);
4149525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
41500a708f8fSGustavo F. Padovan 		list_del(&l->list);
415139d5a3eeSGustavo F. Padovan 		list_add_tail(&l->list, &chan->srej_l);
41520a708f8fSGustavo F. Padovan 	}
41530a708f8fSGustavo F. Padovan }
41540a708f8fSGustavo F. Padovan 
4155aef89f21SSzymon Janc static int l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq)
41560a708f8fSGustavo F. Padovan {
41570a708f8fSGustavo F. Padovan 	struct srej_list *new;
415888843ab0SAndrei Emeltchenko 	u32 control;
41590a708f8fSGustavo F. Padovan 
416042e5c802SGustavo F. Padovan 	while (tx_seq != chan->expected_tx_seq) {
4161ab784b73SAndrei Emeltchenko 		control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ);
41620b209faeSAndrei Emeltchenko 		control |= __set_reqseq(chan, chan->expected_tx_seq);
41633c588192SMat Martineau 		l2cap_seq_list_append(&chan->srej_list, chan->expected_tx_seq);
4164525cd185SGustavo F. Padovan 		l2cap_send_sframe(chan, control);
41650a708f8fSGustavo F. Padovan 
41660a708f8fSGustavo F. Padovan 		new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC);
4167aef89f21SSzymon Janc 		if (!new)
4168aef89f21SSzymon Janc 			return -ENOMEM;
4169aef89f21SSzymon Janc 
417042e5c802SGustavo F. Padovan 		new->tx_seq = chan->expected_tx_seq;
4171836be934SAndrei Emeltchenko 
4172836be934SAndrei Emeltchenko 		chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
4173836be934SAndrei Emeltchenko 
417439d5a3eeSGustavo F. Padovan 		list_add_tail(&new->list, &chan->srej_l);
41750a708f8fSGustavo F. Padovan 	}
4176836be934SAndrei Emeltchenko 
4177836be934SAndrei Emeltchenko 	chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
4178aef89f21SSzymon Janc 
4179aef89f21SSzymon Janc 	return 0;
41800a708f8fSGustavo F. Padovan }
41810a708f8fSGustavo F. Padovan 
418288843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb)
41830a708f8fSGustavo F. Padovan {
4184fb45de7dSAndrei Emeltchenko 	u16 tx_seq = __get_txseq(chan, rx_control);
41850b209faeSAndrei Emeltchenko 	u16 req_seq = __get_reqseq(chan, rx_control);
41867e0ef6eeSAndrei Emeltchenko 	u8 sar = __get_ctrl_sar(chan, rx_control);
41870a708f8fSGustavo F. Padovan 	int tx_seq_offset, expected_tx_seq_offset;
418847d1ec61SGustavo F. Padovan 	int num_to_ack = (chan->tx_win/6) + 1;
41890a708f8fSGustavo F. Padovan 	int err = 0;
41900a708f8fSGustavo F. Padovan 
419188843ab0SAndrei Emeltchenko 	BT_DBG("chan %p len %d tx_seq %d rx_control 0x%8.8x", chan, skb->len,
4192525cd185SGustavo F. Padovan 							tx_seq, rx_control);
41930a708f8fSGustavo F. Padovan 
419403f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control) &&
4195e2ab4353SGustavo F. Padovan 			test_bit(CONN_WAIT_F, &chan->conn_state)) {
41961a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
41976a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
41981a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
4199e2ab4353SGustavo F. Padovan 		clear_bit(CONN_WAIT_F, &chan->conn_state);
42000a708f8fSGustavo F. Padovan 	}
42010a708f8fSGustavo F. Padovan 
420242e5c802SGustavo F. Padovan 	chan->expected_ack_seq = req_seq;
420342e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
42040a708f8fSGustavo F. Padovan 
4205836be934SAndrei Emeltchenko 	tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq);
42060a708f8fSGustavo F. Padovan 
42070a708f8fSGustavo F. Padovan 	/* invalid tx_seq */
420847d1ec61SGustavo F. Padovan 	if (tx_seq_offset >= chan->tx_win) {
42098c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
42100a708f8fSGustavo F. Padovan 		goto drop;
42110a708f8fSGustavo F. Padovan 	}
42120a708f8fSGustavo F. Padovan 
421377f918bcSSzymon Janc 	if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
421477f918bcSSzymon Janc 		if (!test_bit(CONN_RNR_SENT, &chan->conn_state))
421577f918bcSSzymon Janc 			l2cap_send_ack(chan);
42160a708f8fSGustavo F. Padovan 		goto drop;
421777f918bcSSzymon Janc 	}
42180a708f8fSGustavo F. Padovan 
421902f1b641SMat Martineau 	if (tx_seq == chan->expected_tx_seq)
422002f1b641SMat Martineau 		goto expected;
422102f1b641SMat Martineau 
4222e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
42230a708f8fSGustavo F. Padovan 		struct srej_list *first;
42240a708f8fSGustavo F. Padovan 
422539d5a3eeSGustavo F. Padovan 		first = list_first_entry(&chan->srej_l,
42260a708f8fSGustavo F. Padovan 				struct srej_list, list);
42270a708f8fSGustavo F. Padovan 		if (tx_seq == first->tx_seq) {
422842e5c802SGustavo F. Padovan 			l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
4229525cd185SGustavo F. Padovan 			l2cap_check_srej_gap(chan, tx_seq);
42300a708f8fSGustavo F. Padovan 
42310a708f8fSGustavo F. Padovan 			list_del(&first->list);
42320a708f8fSGustavo F. Padovan 			kfree(first);
42330a708f8fSGustavo F. Padovan 
423439d5a3eeSGustavo F. Padovan 			if (list_empty(&chan->srej_l)) {
423542e5c802SGustavo F. Padovan 				chan->buffer_seq = chan->buffer_seq_srej;
4236e2ab4353SGustavo F. Padovan 				clear_bit(CONN_SREJ_SENT, &chan->conn_state);
4237525cd185SGustavo F. Padovan 				l2cap_send_ack(chan);
423849208c9cSGustavo F. Padovan 				BT_DBG("chan %p, Exit SREJ_SENT", chan);
42390a708f8fSGustavo F. Padovan 			}
42400a708f8fSGustavo F. Padovan 		} else {
42410a708f8fSGustavo F. Padovan 			struct srej_list *l;
42420a708f8fSGustavo F. Padovan 
42430a708f8fSGustavo F. Padovan 			/* duplicated tx_seq */
424442e5c802SGustavo F. Padovan 			if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0)
42450a708f8fSGustavo F. Padovan 				goto drop;
42460a708f8fSGustavo F. Padovan 
424739d5a3eeSGustavo F. Padovan 			list_for_each_entry(l, &chan->srej_l, list) {
42480a708f8fSGustavo F. Padovan 				if (l->tx_seq == tx_seq) {
4249525cd185SGustavo F. Padovan 					l2cap_resend_srejframe(chan, tx_seq);
42500a708f8fSGustavo F. Padovan 					return 0;
42510a708f8fSGustavo F. Padovan 				}
42520a708f8fSGustavo F. Padovan 			}
4253aef89f21SSzymon Janc 
4254aef89f21SSzymon Janc 			err = l2cap_send_srejframe(chan, tx_seq);
4255aef89f21SSzymon Janc 			if (err < 0) {
4256aef89f21SSzymon Janc 				l2cap_send_disconn_req(chan->conn, chan, -err);
4257aef89f21SSzymon Janc 				return err;
4258aef89f21SSzymon Janc 			}
42590a708f8fSGustavo F. Padovan 		}
42600a708f8fSGustavo F. Padovan 	} else {
4261836be934SAndrei Emeltchenko 		expected_tx_seq_offset = __seq_offset(chan,
4262836be934SAndrei Emeltchenko 				chan->expected_tx_seq, chan->buffer_seq);
42630a708f8fSGustavo F. Padovan 
42640a708f8fSGustavo F. Padovan 		/* duplicated tx_seq */
42650a708f8fSGustavo F. Padovan 		if (tx_seq_offset < expected_tx_seq_offset)
42660a708f8fSGustavo F. Padovan 			goto drop;
42670a708f8fSGustavo F. Padovan 
4268e2ab4353SGustavo F. Padovan 		set_bit(CONN_SREJ_SENT, &chan->conn_state);
42690a708f8fSGustavo F. Padovan 
427049208c9cSGustavo F. Padovan 		BT_DBG("chan %p, Enter SREJ", chan);
42710a708f8fSGustavo F. Padovan 
427239d5a3eeSGustavo F. Padovan 		INIT_LIST_HEAD(&chan->srej_l);
427342e5c802SGustavo F. Padovan 		chan->buffer_seq_srej = chan->buffer_seq;
42740a708f8fSGustavo F. Padovan 
4275f1c6775bSGustavo F. Padovan 		__skb_queue_head_init(&chan->srej_q);
427642e5c802SGustavo F. Padovan 		l2cap_add_to_srej_queue(chan, skb, tx_seq, sar);
42770a708f8fSGustavo F. Padovan 
42780ef3ef0fSSzymon Janc 		/* Set P-bit only if there are some I-frames to ack. */
42790ef3ef0fSSzymon Janc 		if (__clear_ack_timer(chan))
4280e2ab4353SGustavo F. Padovan 			set_bit(CONN_SEND_PBIT, &chan->conn_state);
42810a708f8fSGustavo F. Padovan 
4282aef89f21SSzymon Janc 		err = l2cap_send_srejframe(chan, tx_seq);
4283aef89f21SSzymon Janc 		if (err < 0) {
4284aef89f21SSzymon Janc 			l2cap_send_disconn_req(chan->conn, chan, -err);
4285aef89f21SSzymon Janc 			return err;
4286aef89f21SSzymon Janc 		}
42870a708f8fSGustavo F. Padovan 	}
42880a708f8fSGustavo F. Padovan 	return 0;
42890a708f8fSGustavo F. Padovan 
42900a708f8fSGustavo F. Padovan expected:
4291836be934SAndrei Emeltchenko 	chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq);
42920a708f8fSGustavo F. Padovan 
4293e2ab4353SGustavo F. Padovan 	if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
42940a708f8fSGustavo F. Padovan 		bt_cb(skb)->tx_seq = tx_seq;
42950a708f8fSGustavo F. Padovan 		bt_cb(skb)->sar = sar;
4296f1c6775bSGustavo F. Padovan 		__skb_queue_tail(&chan->srej_q, skb);
42970a708f8fSGustavo F. Padovan 		return 0;
42980a708f8fSGustavo F. Padovan 	}
42990a708f8fSGustavo F. Padovan 
430084084a31SMat Martineau 	err = l2cap_reassemble_sdu(chan, skb, rx_control);
4301836be934SAndrei Emeltchenko 	chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
4302836be934SAndrei Emeltchenko 
4303e328140fSMat Martineau 	if (err < 0) {
4304e328140fSMat Martineau 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
4305e328140fSMat Martineau 		return err;
4306e328140fSMat Martineau 	}
43070a708f8fSGustavo F. Padovan 
430803f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control)) {
4309e2ab4353SGustavo F. Padovan 		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
4310525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
43110a708f8fSGustavo F. Padovan 	}
43120a708f8fSGustavo F. Padovan 
43130a708f8fSGustavo F. Padovan 
43146a026610SGustavo F. Padovan 	chan->num_acked = (chan->num_acked + 1) % num_to_ack;
43156a026610SGustavo F. Padovan 	if (chan->num_acked == num_to_ack - 1)
4316525cd185SGustavo F. Padovan 		l2cap_send_ack(chan);
43174d611e4dSGustavo F. Padovan 	else
43184d611e4dSGustavo F. Padovan 		__set_ack_timer(chan);
43190a708f8fSGustavo F. Padovan 
43200a708f8fSGustavo F. Padovan 	return 0;
43210a708f8fSGustavo F. Padovan 
43220a708f8fSGustavo F. Padovan drop:
43230a708f8fSGustavo F. Padovan 	kfree_skb(skb);
43240a708f8fSGustavo F. Padovan 	return 0;
43250a708f8fSGustavo F. Padovan }
43260a708f8fSGustavo F. Padovan 
432788843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u32 rx_control)
43280a708f8fSGustavo F. Padovan {
432988843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan,
43300b209faeSAndrei Emeltchenko 				__get_reqseq(chan, rx_control), rx_control);
43310a708f8fSGustavo F. Padovan 
43320b209faeSAndrei Emeltchenko 	chan->expected_ack_seq = __get_reqseq(chan, rx_control);
433342e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
43340a708f8fSGustavo F. Padovan 
4335e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control)) {
4336e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_FBIT, &chan->conn_state);
4337e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
4338e2ab4353SGustavo F. Padovan 			if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
43396a026610SGustavo F. Padovan 					(chan->unacked_frames > 0))
43401a09bcb9SGustavo F. Padovan 				__set_retrans_timer(chan);
43410a708f8fSGustavo F. Padovan 
4342e2ab4353SGustavo F. Padovan 			clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
4343525cd185SGustavo F. Padovan 			l2cap_send_srejtail(chan);
43440a708f8fSGustavo F. Padovan 		} else {
4345525cd185SGustavo F. Padovan 			l2cap_send_i_or_rr_or_rnr(chan);
43460a708f8fSGustavo F. Padovan 		}
43470a708f8fSGustavo F. Padovan 
434803f6715dSAndrei Emeltchenko 	} else if (__is_ctrl_final(chan, rx_control)) {
4349e2ab4353SGustavo F. Padovan 		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
43500a708f8fSGustavo F. Padovan 
4351e2ab4353SGustavo F. Padovan 		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
4352525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
43530a708f8fSGustavo F. Padovan 
43540a708f8fSGustavo F. Padovan 	} else {
4355e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
43566a026610SGustavo F. Padovan 				(chan->unacked_frames > 0))
43571a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
43580a708f8fSGustavo F. Padovan 
4359e2ab4353SGustavo F. Padovan 		clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
4360e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_SREJ_SENT, &chan->conn_state))
4361525cd185SGustavo F. Padovan 			l2cap_send_ack(chan);
43620a708f8fSGustavo F. Padovan 		else
4363525cd185SGustavo F. Padovan 			l2cap_ertm_send(chan);
43640a708f8fSGustavo F. Padovan 	}
43650a708f8fSGustavo F. Padovan }
43660a708f8fSGustavo F. Padovan 
436788843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u32 rx_control)
43680a708f8fSGustavo F. Padovan {
43690b209faeSAndrei Emeltchenko 	u16 tx_seq = __get_reqseq(chan, rx_control);
43700a708f8fSGustavo F. Padovan 
437188843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
43720a708f8fSGustavo F. Padovan 
4373e2ab4353SGustavo F. Padovan 	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
43740a708f8fSGustavo F. Padovan 
437542e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
437642e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
43770a708f8fSGustavo F. Padovan 
437803f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control)) {
4379e2ab4353SGustavo F. Padovan 		if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
4380525cd185SGustavo F. Padovan 			l2cap_retransmit_frames(chan);
43810a708f8fSGustavo F. Padovan 	} else {
4382525cd185SGustavo F. Padovan 		l2cap_retransmit_frames(chan);
43830a708f8fSGustavo F. Padovan 
4384e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_WAIT_F, &chan->conn_state))
4385e2ab4353SGustavo F. Padovan 			set_bit(CONN_REJ_ACT, &chan->conn_state);
43860a708f8fSGustavo F. Padovan 	}
43870a708f8fSGustavo F. Padovan }
438888843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u32 rx_control)
43890a708f8fSGustavo F. Padovan {
43900b209faeSAndrei Emeltchenko 	u16 tx_seq = __get_reqseq(chan, rx_control);
43910a708f8fSGustavo F. Padovan 
439288843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
43930a708f8fSGustavo F. Padovan 
4394e2ab4353SGustavo F. Padovan 	clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
43950a708f8fSGustavo F. Padovan 
4396e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control)) {
439742e5c802SGustavo F. Padovan 		chan->expected_ack_seq = tx_seq;
439842e5c802SGustavo F. Padovan 		l2cap_drop_acked_frames(chan);
43990a708f8fSGustavo F. Padovan 
4400e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_FBIT, &chan->conn_state);
4401525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
44020a708f8fSGustavo F. Padovan 
4403525cd185SGustavo F. Padovan 		l2cap_ertm_send(chan);
44040a708f8fSGustavo F. Padovan 
4405e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
44066a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
4407e2ab4353SGustavo F. Padovan 			set_bit(CONN_SREJ_ACT, &chan->conn_state);
44080a708f8fSGustavo F. Padovan 		}
440903f6715dSAndrei Emeltchenko 	} else if (__is_ctrl_final(chan, rx_control)) {
4410e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_SREJ_ACT, &chan->conn_state) &&
44116a026610SGustavo F. Padovan 				chan->srej_save_reqseq == tx_seq)
4412e2ab4353SGustavo F. Padovan 			clear_bit(CONN_SREJ_ACT, &chan->conn_state);
44130a708f8fSGustavo F. Padovan 		else
4414525cd185SGustavo F. Padovan 			l2cap_retransmit_one_frame(chan, tx_seq);
44150a708f8fSGustavo F. Padovan 	} else {
4416525cd185SGustavo F. Padovan 		l2cap_retransmit_one_frame(chan, tx_seq);
4417e2ab4353SGustavo F. Padovan 		if (test_bit(CONN_WAIT_F, &chan->conn_state)) {
44186a026610SGustavo F. Padovan 			chan->srej_save_reqseq = tx_seq;
4419e2ab4353SGustavo F. Padovan 			set_bit(CONN_SREJ_ACT, &chan->conn_state);
44200a708f8fSGustavo F. Padovan 		}
44210a708f8fSGustavo F. Padovan 	}
44220a708f8fSGustavo F. Padovan }
44230a708f8fSGustavo F. Padovan 
442488843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u32 rx_control)
44250a708f8fSGustavo F. Padovan {
44260b209faeSAndrei Emeltchenko 	u16 tx_seq = __get_reqseq(chan, rx_control);
44270a708f8fSGustavo F. Padovan 
442888843ab0SAndrei Emeltchenko 	BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control);
44290a708f8fSGustavo F. Padovan 
4430e2ab4353SGustavo F. Padovan 	set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
443142e5c802SGustavo F. Padovan 	chan->expected_ack_seq = tx_seq;
443242e5c802SGustavo F. Padovan 	l2cap_drop_acked_frames(chan);
44330a708f8fSGustavo F. Padovan 
4434e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control))
4435e2ab4353SGustavo F. Padovan 		set_bit(CONN_SEND_FBIT, &chan->conn_state);
44360a708f8fSGustavo F. Padovan 
4437e2ab4353SGustavo F. Padovan 	if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) {
44381a09bcb9SGustavo F. Padovan 		__clear_retrans_timer(chan);
4439e3781735SAndrei Emeltchenko 		if (__is_ctrl_poll(chan, rx_control))
4440525cd185SGustavo F. Padovan 			l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL);
44410a708f8fSGustavo F. Padovan 		return;
44420a708f8fSGustavo F. Padovan 	}
44430a708f8fSGustavo F. Padovan 
4444e3781735SAndrei Emeltchenko 	if (__is_ctrl_poll(chan, rx_control)) {
4445525cd185SGustavo F. Padovan 		l2cap_send_srejtail(chan);
4446ab784b73SAndrei Emeltchenko 	} else {
4447ab784b73SAndrei Emeltchenko 		rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR);
4448ab784b73SAndrei Emeltchenko 		l2cap_send_sframe(chan, rx_control);
4449ab784b73SAndrei Emeltchenko 	}
44500a708f8fSGustavo F. Padovan }
44510a708f8fSGustavo F. Padovan 
445288843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb)
44530a708f8fSGustavo F. Padovan {
445488843ab0SAndrei Emeltchenko 	BT_DBG("chan %p rx_control 0x%8.8x len %d", chan, rx_control, skb->len);
44550a708f8fSGustavo F. Padovan 
445603f6715dSAndrei Emeltchenko 	if (__is_ctrl_final(chan, rx_control) &&
4457e2ab4353SGustavo F. Padovan 			test_bit(CONN_WAIT_F, &chan->conn_state)) {
44581a09bcb9SGustavo F. Padovan 		__clear_monitor_timer(chan);
44596a026610SGustavo F. Padovan 		if (chan->unacked_frames > 0)
44601a09bcb9SGustavo F. Padovan 			__set_retrans_timer(chan);
4461e2ab4353SGustavo F. Padovan 		clear_bit(CONN_WAIT_F, &chan->conn_state);
44620a708f8fSGustavo F. Padovan 	}
44630a708f8fSGustavo F. Padovan 
4464ab784b73SAndrei Emeltchenko 	switch (__get_ctrl_super(chan, rx_control)) {
4465ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_RR:
4466525cd185SGustavo F. Padovan 		l2cap_data_channel_rrframe(chan, rx_control);
44670a708f8fSGustavo F. Padovan 		break;
44680a708f8fSGustavo F. Padovan 
4469ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_REJ:
4470525cd185SGustavo F. Padovan 		l2cap_data_channel_rejframe(chan, rx_control);
44710a708f8fSGustavo F. Padovan 		break;
44720a708f8fSGustavo F. Padovan 
4473ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_SREJ:
4474525cd185SGustavo F. Padovan 		l2cap_data_channel_srejframe(chan, rx_control);
44750a708f8fSGustavo F. Padovan 		break;
44760a708f8fSGustavo F. Padovan 
4477ab784b73SAndrei Emeltchenko 	case L2CAP_SUPER_RNR:
4478525cd185SGustavo F. Padovan 		l2cap_data_channel_rnrframe(chan, rx_control);
44790a708f8fSGustavo F. Padovan 		break;
44800a708f8fSGustavo F. Padovan 	}
44810a708f8fSGustavo F. Padovan 
44820a708f8fSGustavo F. Padovan 	kfree_skb(skb);
44830a708f8fSGustavo F. Padovan 	return 0;
44840a708f8fSGustavo F. Padovan }
44850a708f8fSGustavo F. Padovan 
4486cad8f1d0SSzymon Janc static int l2cap_ertm_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
44870a708f8fSGustavo F. Padovan {
448888843ab0SAndrei Emeltchenko 	u32 control;
44890b209faeSAndrei Emeltchenko 	u16 req_seq;
44900a708f8fSGustavo F. Padovan 	int len, next_tx_seq_offset, req_seq_offset;
44910a708f8fSGustavo F. Padovan 
4492b76bbd66SMat Martineau 	__unpack_control(chan, skb);
4493b76bbd66SMat Martineau 
449488843ab0SAndrei Emeltchenko 	control = __get_control(chan, skb->data);
449588843ab0SAndrei Emeltchenko 	skb_pull(skb, __ctrl_size(chan));
44960a708f8fSGustavo F. Padovan 	len = skb->len;
44970a708f8fSGustavo F. Padovan 
44980a708f8fSGustavo F. Padovan 	/*
44990a708f8fSGustavo F. Padovan 	 * We can just drop the corrupted I-frame here.
45000a708f8fSGustavo F. Padovan 	 * Receiver will miss it and start proper recovery
45010a708f8fSGustavo F. Padovan 	 * procedures and ask retransmission.
45020a708f8fSGustavo F. Padovan 	 */
450347d1ec61SGustavo F. Padovan 	if (l2cap_check_fcs(chan, skb))
45040a708f8fSGustavo F. Padovan 		goto drop;
45050a708f8fSGustavo F. Padovan 
4506793c2f1cSAndrei Emeltchenko 	if (__is_sar_start(chan, control) && !__is_sframe(chan, control))
450703a51213SAndrei Emeltchenko 		len -= L2CAP_SDULEN_SIZE;
45080a708f8fSGustavo F. Padovan 
450947d1ec61SGustavo F. Padovan 	if (chan->fcs == L2CAP_FCS_CRC16)
451003a51213SAndrei Emeltchenko 		len -= L2CAP_FCS_SIZE;
45110a708f8fSGustavo F. Padovan 
451247d1ec61SGustavo F. Padovan 	if (len > chan->mps) {
45138c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
45140a708f8fSGustavo F. Padovan 		goto drop;
45150a708f8fSGustavo F. Padovan 	}
45160a708f8fSGustavo F. Padovan 
45170b209faeSAndrei Emeltchenko 	req_seq = __get_reqseq(chan, control);
45180a708f8fSGustavo F. Padovan 
4519836be934SAndrei Emeltchenko 	req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq);
4520836be934SAndrei Emeltchenko 
4521836be934SAndrei Emeltchenko 	next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq,
4522836be934SAndrei Emeltchenko 						chan->expected_ack_seq);
45230a708f8fSGustavo F. Padovan 
45240a708f8fSGustavo F. Padovan 	/* check for invalid req-seq */
45250a708f8fSGustavo F. Padovan 	if (req_seq_offset > next_tx_seq_offset) {
45268c1d787bSGustavo F. Padovan 		l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
45270a708f8fSGustavo F. Padovan 		goto drop;
45280a708f8fSGustavo F. Padovan 	}
45290a708f8fSGustavo F. Padovan 
4530793c2f1cSAndrei Emeltchenko 	if (!__is_sframe(chan, control)) {
45310a708f8fSGustavo F. Padovan 		if (len < 0) {
45328c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
45330a708f8fSGustavo F. Padovan 			goto drop;
45340a708f8fSGustavo F. Padovan 		}
45350a708f8fSGustavo F. Padovan 
4536525cd185SGustavo F. Padovan 		l2cap_data_channel_iframe(chan, control, skb);
45370a708f8fSGustavo F. Padovan 	} else {
45380a708f8fSGustavo F. Padovan 		if (len != 0) {
45390a708f8fSGustavo F. Padovan 			BT_ERR("%d", len);
45408c1d787bSGustavo F. Padovan 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
45410a708f8fSGustavo F. Padovan 			goto drop;
45420a708f8fSGustavo F. Padovan 		}
45430a708f8fSGustavo F. Padovan 
4544525cd185SGustavo F. Padovan 		l2cap_data_channel_sframe(chan, control, skb);
45450a708f8fSGustavo F. Padovan 	}
45460a708f8fSGustavo F. Padovan 
45470a708f8fSGustavo F. Padovan 	return 0;
45480a708f8fSGustavo F. Padovan 
45490a708f8fSGustavo F. Padovan drop:
45500a708f8fSGustavo F. Padovan 	kfree_skb(skb);
45510a708f8fSGustavo F. Padovan 	return 0;
45520a708f8fSGustavo F. Padovan }
45530a708f8fSGustavo F. Padovan 
45540a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb)
45550a708f8fSGustavo F. Padovan {
455648454079SGustavo F. Padovan 	struct l2cap_chan *chan;
455788843ab0SAndrei Emeltchenko 	u32 control;
4558fb45de7dSAndrei Emeltchenko 	u16 tx_seq;
45590a708f8fSGustavo F. Padovan 	int len;
45600a708f8fSGustavo F. Padovan 
4561baa7e1faSGustavo F. Padovan 	chan = l2cap_get_chan_by_scid(conn, cid);
456248454079SGustavo F. Padovan 	if (!chan) {
45630a708f8fSGustavo F. Padovan 		BT_DBG("unknown cid 0x%4.4x", cid);
45646be36555SAndrei Emeltchenko 		/* Drop packet and return */
45653379013bSDan Carpenter 		kfree_skb(skb);
45666be36555SAndrei Emeltchenko 		return 0;
45670a708f8fSGustavo F. Padovan 	}
45680a708f8fSGustavo F. Padovan 
45696be36555SAndrei Emeltchenko 	l2cap_chan_lock(chan);
45700a708f8fSGustavo F. Padovan 
457149208c9cSGustavo F. Padovan 	BT_DBG("chan %p, len %d", chan, skb->len);
45720a708f8fSGustavo F. Padovan 
457389bc500eSGustavo F. Padovan 	if (chan->state != BT_CONNECTED)
45740a708f8fSGustavo F. Padovan 		goto drop;
45750a708f8fSGustavo F. Padovan 
45760c1bc5c6SGustavo F. Padovan 	switch (chan->mode) {
45770a708f8fSGustavo F. Padovan 	case L2CAP_MODE_BASIC:
45780a708f8fSGustavo F. Padovan 		/* If socket recv buffers overflows we drop data here
45790a708f8fSGustavo F. Padovan 		 * which is *bad* because L2CAP has to be reliable.
45800a708f8fSGustavo F. Padovan 		 * But we don't have any other choice. L2CAP doesn't
45810a708f8fSGustavo F. Padovan 		 * provide flow control mechanism. */
45820a708f8fSGustavo F. Padovan 
45830c1bc5c6SGustavo F. Padovan 		if (chan->imtu < skb->len)
45840a708f8fSGustavo F. Padovan 			goto drop;
45850a708f8fSGustavo F. Padovan 
458623070494SGustavo F. Padovan 		if (!chan->ops->recv(chan->data, skb))
45870a708f8fSGustavo F. Padovan 			goto done;
45880a708f8fSGustavo F. Padovan 		break;
45890a708f8fSGustavo F. Padovan 
45900a708f8fSGustavo F. Padovan 	case L2CAP_MODE_ERTM:
45915ef8cb9eSAndrei Emeltchenko 		l2cap_ertm_data_rcv(chan, skb);
45920a708f8fSGustavo F. Padovan 
45930a708f8fSGustavo F. Padovan 		goto done;
45940a708f8fSGustavo F. Padovan 
45950a708f8fSGustavo F. Padovan 	case L2CAP_MODE_STREAMING:
459688843ab0SAndrei Emeltchenko 		control = __get_control(chan, skb->data);
459788843ab0SAndrei Emeltchenko 		skb_pull(skb, __ctrl_size(chan));
45980a708f8fSGustavo F. Padovan 		len = skb->len;
45990a708f8fSGustavo F. Padovan 
460047d1ec61SGustavo F. Padovan 		if (l2cap_check_fcs(chan, skb))
46010a708f8fSGustavo F. Padovan 			goto drop;
46020a708f8fSGustavo F. Padovan 
46037e0ef6eeSAndrei Emeltchenko 		if (__is_sar_start(chan, control))
460403a51213SAndrei Emeltchenko 			len -= L2CAP_SDULEN_SIZE;
46050a708f8fSGustavo F. Padovan 
460647d1ec61SGustavo F. Padovan 		if (chan->fcs == L2CAP_FCS_CRC16)
460703a51213SAndrei Emeltchenko 			len -= L2CAP_FCS_SIZE;
46080a708f8fSGustavo F. Padovan 
4609793c2f1cSAndrei Emeltchenko 		if (len > chan->mps || len < 0 || __is_sframe(chan, control))
46100a708f8fSGustavo F. Padovan 			goto drop;
46110a708f8fSGustavo F. Padovan 
4612fb45de7dSAndrei Emeltchenko 		tx_seq = __get_txseq(chan, control);
46130a708f8fSGustavo F. Padovan 
461484084a31SMat Martineau 		if (chan->expected_tx_seq != tx_seq) {
461584084a31SMat Martineau 			/* Frame(s) missing - must discard partial SDU */
461684084a31SMat Martineau 			kfree_skb(chan->sdu);
461784084a31SMat Martineau 			chan->sdu = NULL;
461884084a31SMat Martineau 			chan->sdu_last_frag = NULL;
461984084a31SMat Martineau 			chan->sdu_len = 0;
462084084a31SMat Martineau 
462184084a31SMat Martineau 			/* TODO: Notify userland of missing data */
462284084a31SMat Martineau 		}
462384084a31SMat Martineau 
4624836be934SAndrei Emeltchenko 		chan->expected_tx_seq = __next_seq(chan, tx_seq);
46250a708f8fSGustavo F. Padovan 
462684084a31SMat Martineau 		if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE)
462784084a31SMat Martineau 			l2cap_send_disconn_req(chan->conn, chan, ECONNRESET);
46280a708f8fSGustavo F. Padovan 
46290a708f8fSGustavo F. Padovan 		goto done;
46300a708f8fSGustavo F. Padovan 
46310a708f8fSGustavo F. Padovan 	default:
46320c1bc5c6SGustavo F. Padovan 		BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
46330a708f8fSGustavo F. Padovan 		break;
46340a708f8fSGustavo F. Padovan 	}
46350a708f8fSGustavo F. Padovan 
46360a708f8fSGustavo F. Padovan drop:
46370a708f8fSGustavo F. Padovan 	kfree_skb(skb);
46380a708f8fSGustavo F. Padovan 
46390a708f8fSGustavo F. Padovan done:
46406be36555SAndrei Emeltchenko 	l2cap_chan_unlock(chan);
46410a708f8fSGustavo F. Padovan 
46420a708f8fSGustavo F. Padovan 	return 0;
46430a708f8fSGustavo F. Padovan }
46440a708f8fSGustavo F. Padovan 
46450a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
46460a708f8fSGustavo F. Padovan {
464723691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
46480a708f8fSGustavo F. Padovan 
4649c2287681SIdo Yariv 	chan = l2cap_global_chan_by_psm(0, psm, conn->src, conn->dst);
465023691d75SGustavo F. Padovan 	if (!chan)
46510a708f8fSGustavo F. Padovan 		goto drop;
46520a708f8fSGustavo F. Padovan 
46535b4cedaaSAndrei Emeltchenko 	BT_DBG("chan %p, len %d", chan, skb->len);
46540a708f8fSGustavo F. Padovan 
465589bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
46560a708f8fSGustavo F. Padovan 		goto drop;
46570a708f8fSGustavo F. Padovan 
4658e13e21dcSVinicius Costa Gomes 	if (chan->imtu < skb->len)
46590a708f8fSGustavo F. Padovan 		goto drop;
46600a708f8fSGustavo F. Padovan 
466123070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
46625b4cedaaSAndrei Emeltchenko 		return 0;
46630a708f8fSGustavo F. Padovan 
46640a708f8fSGustavo F. Padovan drop:
46650a708f8fSGustavo F. Padovan 	kfree_skb(skb);
46660a708f8fSGustavo F. Padovan 
46670a708f8fSGustavo F. Padovan 	return 0;
46680a708f8fSGustavo F. Padovan }
46690a708f8fSGustavo F. Padovan 
4670d9b88702SAndrei Emeltchenko static inline int l2cap_att_channel(struct l2cap_conn *conn, u16 cid,
4671d9b88702SAndrei Emeltchenko 				    struct sk_buff *skb)
46729f69bda6SGustavo F. Padovan {
467323691d75SGustavo F. Padovan 	struct l2cap_chan *chan;
46749f69bda6SGustavo F. Padovan 
4675c2287681SIdo Yariv 	chan = l2cap_global_chan_by_scid(0, cid, conn->src, conn->dst);
467623691d75SGustavo F. Padovan 	if (!chan)
46779f69bda6SGustavo F. Padovan 		goto drop;
46789f69bda6SGustavo F. Padovan 
46795b4cedaaSAndrei Emeltchenko 	BT_DBG("chan %p, len %d", chan, skb->len);
46809f69bda6SGustavo F. Padovan 
468189bc500eSGustavo F. Padovan 	if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
46829f69bda6SGustavo F. Padovan 		goto drop;
46839f69bda6SGustavo F. Padovan 
4684e13e21dcSVinicius Costa Gomes 	if (chan->imtu < skb->len)
46859f69bda6SGustavo F. Padovan 		goto drop;
46869f69bda6SGustavo F. Padovan 
468723070494SGustavo F. Padovan 	if (!chan->ops->recv(chan->data, skb))
46885b4cedaaSAndrei Emeltchenko 		return 0;
46899f69bda6SGustavo F. Padovan 
46909f69bda6SGustavo F. Padovan drop:
46919f69bda6SGustavo F. Padovan 	kfree_skb(skb);
46929f69bda6SGustavo F. Padovan 
46939f69bda6SGustavo F. Padovan 	return 0;
46949f69bda6SGustavo F. Padovan }
46959f69bda6SGustavo F. Padovan 
46960a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
46970a708f8fSGustavo F. Padovan {
46980a708f8fSGustavo F. Padovan 	struct l2cap_hdr *lh = (void *) skb->data;
46990a708f8fSGustavo F. Padovan 	u16 cid, len;
47000a708f8fSGustavo F. Padovan 	__le16 psm;
47010a708f8fSGustavo F. Padovan 
47020a708f8fSGustavo F. Padovan 	skb_pull(skb, L2CAP_HDR_SIZE);
47030a708f8fSGustavo F. Padovan 	cid = __le16_to_cpu(lh->cid);
47040a708f8fSGustavo F. Padovan 	len = __le16_to_cpu(lh->len);
47050a708f8fSGustavo F. Padovan 
47060a708f8fSGustavo F. Padovan 	if (len != skb->len) {
47070a708f8fSGustavo F. Padovan 		kfree_skb(skb);
47080a708f8fSGustavo F. Padovan 		return;
47090a708f8fSGustavo F. Padovan 	}
47100a708f8fSGustavo F. Padovan 
47110a708f8fSGustavo F. Padovan 	BT_DBG("len %d, cid 0x%4.4x", len, cid);
47120a708f8fSGustavo F. Padovan 
47130a708f8fSGustavo F. Padovan 	switch (cid) {
47143300d9a9SClaudio Takahasi 	case L2CAP_CID_LE_SIGNALING:
47150a708f8fSGustavo F. Padovan 	case L2CAP_CID_SIGNALING:
47160a708f8fSGustavo F. Padovan 		l2cap_sig_channel(conn, skb);
47170a708f8fSGustavo F. Padovan 		break;
47180a708f8fSGustavo F. Padovan 
47190a708f8fSGustavo F. Padovan 	case L2CAP_CID_CONN_LESS:
4720097db76cSAndrei Emeltchenko 		psm = get_unaligned((__le16 *) skb->data);
47210a708f8fSGustavo F. Padovan 		skb_pull(skb, 2);
47220a708f8fSGustavo F. Padovan 		l2cap_conless_channel(conn, psm, skb);
47230a708f8fSGustavo F. Padovan 		break;
47240a708f8fSGustavo F. Padovan 
47259f69bda6SGustavo F. Padovan 	case L2CAP_CID_LE_DATA:
47269f69bda6SGustavo F. Padovan 		l2cap_att_channel(conn, cid, skb);
47279f69bda6SGustavo F. Padovan 		break;
47289f69bda6SGustavo F. Padovan 
4729b501d6a1SAnderson Briglia 	case L2CAP_CID_SMP:
4730b501d6a1SAnderson Briglia 		if (smp_sig_channel(conn, skb))
4731b501d6a1SAnderson Briglia 			l2cap_conn_del(conn->hcon, EACCES);
4732b501d6a1SAnderson Briglia 		break;
4733b501d6a1SAnderson Briglia 
47340a708f8fSGustavo F. Padovan 	default:
47350a708f8fSGustavo F. Padovan 		l2cap_data_channel(conn, cid, skb);
47360a708f8fSGustavo F. Padovan 		break;
47370a708f8fSGustavo F. Padovan 	}
47380a708f8fSGustavo F. Padovan }
47390a708f8fSGustavo F. Padovan 
47400a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */
47410a708f8fSGustavo F. Padovan 
4742686ebf28SUlisses Furquim int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
47430a708f8fSGustavo F. Padovan {
47440a708f8fSGustavo F. Padovan 	int exact = 0, lm1 = 0, lm2 = 0;
474523691d75SGustavo F. Padovan 	struct l2cap_chan *c;
47460a708f8fSGustavo F. Padovan 
47470a708f8fSGustavo F. Padovan 	BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
47480a708f8fSGustavo F. Padovan 
47490a708f8fSGustavo F. Padovan 	/* Find listening sockets and check their link_mode */
475023691d75SGustavo F. Padovan 	read_lock(&chan_list_lock);
475123691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
475223691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
47534343478fSGustavo F. Padovan 
475489bc500eSGustavo F. Padovan 		if (c->state != BT_LISTEN)
47550a708f8fSGustavo F. Padovan 			continue;
47560a708f8fSGustavo F. Padovan 
47570a708f8fSGustavo F. Padovan 		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
47580a708f8fSGustavo F. Padovan 			lm1 |= HCI_LM_ACCEPT;
475943bd0f32SAndrei Emeltchenko 			if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
47600a708f8fSGustavo F. Padovan 				lm1 |= HCI_LM_MASTER;
47610a708f8fSGustavo F. Padovan 			exact++;
47620a708f8fSGustavo F. Padovan 		} else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
47630a708f8fSGustavo F. Padovan 			lm2 |= HCI_LM_ACCEPT;
476443bd0f32SAndrei Emeltchenko 			if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
47650a708f8fSGustavo F. Padovan 				lm2 |= HCI_LM_MASTER;
47660a708f8fSGustavo F. Padovan 		}
47670a708f8fSGustavo F. Padovan 	}
476823691d75SGustavo F. Padovan 	read_unlock(&chan_list_lock);
47690a708f8fSGustavo F. Padovan 
47700a708f8fSGustavo F. Padovan 	return exact ? lm1 : lm2;
47710a708f8fSGustavo F. Padovan }
47720a708f8fSGustavo F. Padovan 
4773686ebf28SUlisses Furquim int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
47740a708f8fSGustavo F. Padovan {
47750a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn;
47760a708f8fSGustavo F. Padovan 
47770a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
47780a708f8fSGustavo F. Padovan 
47790a708f8fSGustavo F. Padovan 	if (!status) {
47800a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, status);
47810a708f8fSGustavo F. Padovan 		if (conn)
47820a708f8fSGustavo F. Padovan 			l2cap_conn_ready(conn);
47830a708f8fSGustavo F. Padovan 	} else
4784e175072fSJoe Perches 		l2cap_conn_del(hcon, bt_to_errno(status));
47850a708f8fSGustavo F. Padovan 
47860a708f8fSGustavo F. Padovan 	return 0;
47870a708f8fSGustavo F. Padovan }
47880a708f8fSGustavo F. Padovan 
4789686ebf28SUlisses Furquim int l2cap_disconn_ind(struct hci_conn *hcon)
47900a708f8fSGustavo F. Padovan {
47910a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
47920a708f8fSGustavo F. Padovan 
47930a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p", hcon);
47940a708f8fSGustavo F. Padovan 
4795686ebf28SUlisses Furquim 	if (!conn)
47969f5a0d7bSAndrei Emeltchenko 		return HCI_ERROR_REMOTE_USER_TERM;
47970a708f8fSGustavo F. Padovan 	return conn->disc_reason;
47980a708f8fSGustavo F. Padovan }
47990a708f8fSGustavo F. Padovan 
4800686ebf28SUlisses Furquim int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
48010a708f8fSGustavo F. Padovan {
48020a708f8fSGustavo F. Padovan 	BT_DBG("hcon %p reason %d", hcon, reason);
48030a708f8fSGustavo F. Padovan 
4804e175072fSJoe Perches 	l2cap_conn_del(hcon, bt_to_errno(reason));
48050a708f8fSGustavo F. Padovan 	return 0;
48060a708f8fSGustavo F. Padovan }
48070a708f8fSGustavo F. Padovan 
48084343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
48090a708f8fSGustavo F. Padovan {
4810715ec005SGustavo F. Padovan 	if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
48110a708f8fSGustavo F. Padovan 		return;
48120a708f8fSGustavo F. Padovan 
48130a708f8fSGustavo F. Padovan 	if (encrypt == 0x00) {
48144343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM) {
4815ba13ccd9SMarcel Holtmann 			__set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
48164343478fSGustavo F. Padovan 		} else if (chan->sec_level == BT_SECURITY_HIGH)
48170f852724SGustavo F. Padovan 			l2cap_chan_close(chan, ECONNREFUSED);
48180a708f8fSGustavo F. Padovan 	} else {
48194343478fSGustavo F. Padovan 		if (chan->sec_level == BT_SECURITY_MEDIUM)
4820c9b66675SGustavo F. Padovan 			__clear_chan_timer(chan);
48210a708f8fSGustavo F. Padovan 	}
48220a708f8fSGustavo F. Padovan }
48230a708f8fSGustavo F. Padovan 
4824686ebf28SUlisses Furquim int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
48250a708f8fSGustavo F. Padovan {
48260a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
482748454079SGustavo F. Padovan 	struct l2cap_chan *chan;
48280a708f8fSGustavo F. Padovan 
48290a708f8fSGustavo F. Padovan 	if (!conn)
48300a708f8fSGustavo F. Padovan 		return 0;
48310a708f8fSGustavo F. Padovan 
48320a708f8fSGustavo F. Padovan 	BT_DBG("conn %p", conn);
48330a708f8fSGustavo F. Padovan 
4834160dc6acSVinicius Costa Gomes 	if (hcon->type == LE_LINK) {
483535d4adccSHemant Gupta 		if (!status && encrypt)
4836160dc6acSVinicius Costa Gomes 			smp_distribute_keys(conn, 0);
483717cd3f37SUlisses Furquim 		cancel_delayed_work(&conn->security_timer);
4838160dc6acSVinicius Costa Gomes 	}
4839160dc6acSVinicius Costa Gomes 
48403df91ea2SAndrei Emeltchenko 	mutex_lock(&conn->chan_lock);
48410a708f8fSGustavo F. Padovan 
48423df91ea2SAndrei Emeltchenko 	list_for_each_entry(chan, &conn->chan_l, list) {
48436be36555SAndrei Emeltchenko 		l2cap_chan_lock(chan);
48440a708f8fSGustavo F. Padovan 
4845f1cb9af5SVinicius Costa Gomes 		BT_DBG("chan->scid %d", chan->scid);
4846f1cb9af5SVinicius Costa Gomes 
4847f1cb9af5SVinicius Costa Gomes 		if (chan->scid == L2CAP_CID_LE_DATA) {
4848f1cb9af5SVinicius Costa Gomes 			if (!status && encrypt) {
4849f1cb9af5SVinicius Costa Gomes 				chan->sec_level = hcon->sec_level;
4850cf4cd009SAndrei Emeltchenko 				l2cap_chan_ready(chan);
4851f1cb9af5SVinicius Costa Gomes 			}
4852f1cb9af5SVinicius Costa Gomes 
48536be36555SAndrei Emeltchenko 			l2cap_chan_unlock(chan);
4854f1cb9af5SVinicius Costa Gomes 			continue;
4855f1cb9af5SVinicius Costa Gomes 		}
4856f1cb9af5SVinicius Costa Gomes 
4857c1360a1cSGustavo F. Padovan 		if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) {
48586be36555SAndrei Emeltchenko 			l2cap_chan_unlock(chan);
48590a708f8fSGustavo F. Padovan 			continue;
48600a708f8fSGustavo F. Padovan 		}
48610a708f8fSGustavo F. Padovan 
486289bc500eSGustavo F. Padovan 		if (!status && (chan->state == BT_CONNECTED ||
486389bc500eSGustavo F. Padovan 						chan->state == BT_CONFIG)) {
48644343478fSGustavo F. Padovan 			l2cap_check_encryption(chan, encrypt);
48656be36555SAndrei Emeltchenko 			l2cap_chan_unlock(chan);
48660a708f8fSGustavo F. Padovan 			continue;
48670a708f8fSGustavo F. Padovan 		}
48680a708f8fSGustavo F. Padovan 
486989bc500eSGustavo F. Padovan 		if (chan->state == BT_CONNECT) {
48700a708f8fSGustavo F. Padovan 			if (!status) {
48719b27f350SAndrei Emeltchenko 				l2cap_send_conn_req(chan);
48720a708f8fSGustavo F. Padovan 			} else {
4873ba13ccd9SMarcel Holtmann 				__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
48740a708f8fSGustavo F. Padovan 			}
487589bc500eSGustavo F. Padovan 		} else if (chan->state == BT_CONNECT2) {
48766be36555SAndrei Emeltchenko 			struct sock *sk = chan->sk;
48770a708f8fSGustavo F. Padovan 			struct l2cap_conn_rsp rsp;
4878df3c3931SJohan Hedberg 			__u16 res, stat;
48790a708f8fSGustavo F. Padovan 
48806be36555SAndrei Emeltchenko 			lock_sock(sk);
48816be36555SAndrei Emeltchenko 
48820a708f8fSGustavo F. Padovan 			if (!status) {
4883df3c3931SJohan Hedberg 				if (bt_sk(sk)->defer_setup) {
4884df3c3931SJohan Hedberg 					struct sock *parent = bt_sk(sk)->parent;
4885df3c3931SJohan Hedberg 					res = L2CAP_CR_PEND;
4886df3c3931SJohan Hedberg 					stat = L2CAP_CS_AUTHOR_PEND;
488705e9a2f6SIlia Kolomisnky 					if (parent)
4888df3c3931SJohan Hedberg 						parent->sk_data_ready(parent, 0);
4889df3c3931SJohan Hedberg 				} else {
48900e587be7SAndrei Emeltchenko 					__l2cap_state_change(chan, BT_CONFIG);
4891df3c3931SJohan Hedberg 					res = L2CAP_CR_SUCCESS;
4892df3c3931SJohan Hedberg 					stat = L2CAP_CS_NO_INFO;
4893df3c3931SJohan Hedberg 				}
48940a708f8fSGustavo F. Padovan 			} else {
48950e587be7SAndrei Emeltchenko 				__l2cap_state_change(chan, BT_DISCONN);
4896ba13ccd9SMarcel Holtmann 				__set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
4897df3c3931SJohan Hedberg 				res = L2CAP_CR_SEC_BLOCK;
4898df3c3931SJohan Hedberg 				stat = L2CAP_CS_NO_INFO;
48990a708f8fSGustavo F. Padovan 			}
49000a708f8fSGustavo F. Padovan 
49016be36555SAndrei Emeltchenko 			release_sock(sk);
49026be36555SAndrei Emeltchenko 
4903fe4128e0SGustavo F. Padovan 			rsp.scid   = cpu_to_le16(chan->dcid);
4904fe4128e0SGustavo F. Padovan 			rsp.dcid   = cpu_to_le16(chan->scid);
4905df3c3931SJohan Hedberg 			rsp.result = cpu_to_le16(res);
4906df3c3931SJohan Hedberg 			rsp.status = cpu_to_le16(stat);
4907fc7f8a7eSGustavo F. Padovan 			l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
4908fc7f8a7eSGustavo F. Padovan 							sizeof(rsp), &rsp);
49090a708f8fSGustavo F. Padovan 		}
49100a708f8fSGustavo F. Padovan 
49116be36555SAndrei Emeltchenko 		l2cap_chan_unlock(chan);
49120a708f8fSGustavo F. Padovan 	}
49130a708f8fSGustavo F. Padovan 
49143df91ea2SAndrei Emeltchenko 	mutex_unlock(&conn->chan_lock);
49150a708f8fSGustavo F. Padovan 
49160a708f8fSGustavo F. Padovan 	return 0;
49170a708f8fSGustavo F. Padovan }
49180a708f8fSGustavo F. Padovan 
4919686ebf28SUlisses Furquim int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
49200a708f8fSGustavo F. Padovan {
49210a708f8fSGustavo F. Padovan 	struct l2cap_conn *conn = hcon->l2cap_data;
49220a708f8fSGustavo F. Padovan 
49230a708f8fSGustavo F. Padovan 	if (!conn)
49240a708f8fSGustavo F. Padovan 		conn = l2cap_conn_add(hcon, 0);
49250a708f8fSGustavo F. Padovan 
49260a708f8fSGustavo F. Padovan 	if (!conn)
49270a708f8fSGustavo F. Padovan 		goto drop;
49280a708f8fSGustavo F. Padovan 
49290a708f8fSGustavo F. Padovan 	BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
49300a708f8fSGustavo F. Padovan 
49310a708f8fSGustavo F. Padovan 	if (!(flags & ACL_CONT)) {
49320a708f8fSGustavo F. Padovan 		struct l2cap_hdr *hdr;
493348454079SGustavo F. Padovan 		struct l2cap_chan *chan;
49340a708f8fSGustavo F. Padovan 		u16 cid;
49350a708f8fSGustavo F. Padovan 		int len;
49360a708f8fSGustavo F. Padovan 
49370a708f8fSGustavo F. Padovan 		if (conn->rx_len) {
49380a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected start frame (len %d)", skb->len);
49390a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
49400a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
49410a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
49420a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
49430a708f8fSGustavo F. Padovan 		}
49440a708f8fSGustavo F. Padovan 
49450a708f8fSGustavo F. Padovan 		/* Start fragment always begin with Basic L2CAP header */
49460a708f8fSGustavo F. Padovan 		if (skb->len < L2CAP_HDR_SIZE) {
49470a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too short (len %d)", skb->len);
49480a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
49490a708f8fSGustavo F. Padovan 			goto drop;
49500a708f8fSGustavo F. Padovan 		}
49510a708f8fSGustavo F. Padovan 
49520a708f8fSGustavo F. Padovan 		hdr = (struct l2cap_hdr *) skb->data;
49530a708f8fSGustavo F. Padovan 		len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
49540a708f8fSGustavo F. Padovan 		cid = __le16_to_cpu(hdr->cid);
49550a708f8fSGustavo F. Padovan 
49560a708f8fSGustavo F. Padovan 		if (len == skb->len) {
49570a708f8fSGustavo F. Padovan 			/* Complete frame received */
49580a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, skb);
49590a708f8fSGustavo F. Padovan 			return 0;
49600a708f8fSGustavo F. Padovan 		}
49610a708f8fSGustavo F. Padovan 
49620a708f8fSGustavo F. Padovan 		BT_DBG("Start: total len %d, frag len %d", len, skb->len);
49630a708f8fSGustavo F. Padovan 
49640a708f8fSGustavo F. Padovan 		if (skb->len > len) {
49650a708f8fSGustavo F. Padovan 			BT_ERR("Frame is too long (len %d, expected len %d)",
49660a708f8fSGustavo F. Padovan 				skb->len, len);
49670a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
49680a708f8fSGustavo F. Padovan 			goto drop;
49690a708f8fSGustavo F. Padovan 		}
49700a708f8fSGustavo F. Padovan 
4971baa7e1faSGustavo F. Padovan 		chan = l2cap_get_chan_by_scid(conn, cid);
49720a708f8fSGustavo F. Padovan 
497348454079SGustavo F. Padovan 		if (chan && chan->sk) {
497448454079SGustavo F. Padovan 			struct sock *sk = chan->sk;
49753df91ea2SAndrei Emeltchenko 			lock_sock(sk);
497648454079SGustavo F. Padovan 
49770c1bc5c6SGustavo F. Padovan 			if (chan->imtu < len - L2CAP_HDR_SIZE) {
497848454079SGustavo F. Padovan 				BT_ERR("Frame exceeding recv MTU (len %d, "
497948454079SGustavo F. Padovan 							"MTU %d)", len,
49800c1bc5c6SGustavo F. Padovan 							chan->imtu);
4981aa2ac881SGustavo F. Padovan 				release_sock(sk);
49820a708f8fSGustavo F. Padovan 				l2cap_conn_unreliable(conn, ECOMM);
49830a708f8fSGustavo F. Padovan 				goto drop;
49840a708f8fSGustavo F. Padovan 			}
4985aa2ac881SGustavo F. Padovan 			release_sock(sk);
498648454079SGustavo F. Padovan 		}
49870a708f8fSGustavo F. Padovan 
49880a708f8fSGustavo F. Padovan 		/* Allocate skb for the complete frame (with header) */
49890a708f8fSGustavo F. Padovan 		conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
49900a708f8fSGustavo F. Padovan 		if (!conn->rx_skb)
49910a708f8fSGustavo F. Padovan 			goto drop;
49920a708f8fSGustavo F. Padovan 
49930a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
49940a708f8fSGustavo F. Padovan 								skb->len);
49950a708f8fSGustavo F. Padovan 		conn->rx_len = len - skb->len;
49960a708f8fSGustavo F. Padovan 	} else {
49970a708f8fSGustavo F. Padovan 		BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
49980a708f8fSGustavo F. Padovan 
49990a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
50000a708f8fSGustavo F. Padovan 			BT_ERR("Unexpected continuation frame (len %d)", skb->len);
50010a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
50020a708f8fSGustavo F. Padovan 			goto drop;
50030a708f8fSGustavo F. Padovan 		}
50040a708f8fSGustavo F. Padovan 
50050a708f8fSGustavo F. Padovan 		if (skb->len > conn->rx_len) {
50060a708f8fSGustavo F. Padovan 			BT_ERR("Fragment is too long (len %d, expected %d)",
50070a708f8fSGustavo F. Padovan 					skb->len, conn->rx_len);
50080a708f8fSGustavo F. Padovan 			kfree_skb(conn->rx_skb);
50090a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
50100a708f8fSGustavo F. Padovan 			conn->rx_len = 0;
50110a708f8fSGustavo F. Padovan 			l2cap_conn_unreliable(conn, ECOMM);
50120a708f8fSGustavo F. Padovan 			goto drop;
50130a708f8fSGustavo F. Padovan 		}
50140a708f8fSGustavo F. Padovan 
50150a708f8fSGustavo F. Padovan 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
50160a708f8fSGustavo F. Padovan 								skb->len);
50170a708f8fSGustavo F. Padovan 		conn->rx_len -= skb->len;
50180a708f8fSGustavo F. Padovan 
50190a708f8fSGustavo F. Padovan 		if (!conn->rx_len) {
50200a708f8fSGustavo F. Padovan 			/* Complete frame received */
50210a708f8fSGustavo F. Padovan 			l2cap_recv_frame(conn, conn->rx_skb);
50220a708f8fSGustavo F. Padovan 			conn->rx_skb = NULL;
50230a708f8fSGustavo F. Padovan 		}
50240a708f8fSGustavo F. Padovan 	}
50250a708f8fSGustavo F. Padovan 
50260a708f8fSGustavo F. Padovan drop:
50270a708f8fSGustavo F. Padovan 	kfree_skb(skb);
50280a708f8fSGustavo F. Padovan 	return 0;
50290a708f8fSGustavo F. Padovan }
50300a708f8fSGustavo F. Padovan 
50310a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p)
50320a708f8fSGustavo F. Padovan {
503323691d75SGustavo F. Padovan 	struct l2cap_chan *c;
50340a708f8fSGustavo F. Padovan 
5035333055f2SGustavo F. Padovan 	read_lock(&chan_list_lock);
50360a708f8fSGustavo F. Padovan 
503723691d75SGustavo F. Padovan 	list_for_each_entry(c, &chan_list, global_l) {
503823691d75SGustavo F. Padovan 		struct sock *sk = c->sk;
50390a708f8fSGustavo F. Padovan 
5040903d343eSGustavo F. Padovan 		seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
50410a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->src),
50420a708f8fSGustavo F. Padovan 					batostr(&bt_sk(sk)->dst),
504389bc500eSGustavo F. Padovan 					c->state, __le16_to_cpu(c->psm),
504423691d75SGustavo F. Padovan 					c->scid, c->dcid, c->imtu, c->omtu,
504523691d75SGustavo F. Padovan 					c->sec_level, c->mode);
50460a708f8fSGustavo F. Padovan 	}
50470a708f8fSGustavo F. Padovan 
5048333055f2SGustavo F. Padovan 	read_unlock(&chan_list_lock);
50490a708f8fSGustavo F. Padovan 
50500a708f8fSGustavo F. Padovan 	return 0;
50510a708f8fSGustavo F. Padovan }
50520a708f8fSGustavo F. Padovan 
50530a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file)
50540a708f8fSGustavo F. Padovan {
50550a708f8fSGustavo F. Padovan 	return single_open(file, l2cap_debugfs_show, inode->i_private);
50560a708f8fSGustavo F. Padovan }
50570a708f8fSGustavo F. Padovan 
50580a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = {
50590a708f8fSGustavo F. Padovan 	.open		= l2cap_debugfs_open,
50600a708f8fSGustavo F. Padovan 	.read		= seq_read,
50610a708f8fSGustavo F. Padovan 	.llseek		= seq_lseek,
50620a708f8fSGustavo F. Padovan 	.release	= single_release,
50630a708f8fSGustavo F. Padovan };
50640a708f8fSGustavo F. Padovan 
50650a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs;
50660a708f8fSGustavo F. Padovan 
506764274518SGustavo F. Padovan int __init l2cap_init(void)
50680a708f8fSGustavo F. Padovan {
50690a708f8fSGustavo F. Padovan 	int err;
50700a708f8fSGustavo F. Padovan 
5071bb58f747SGustavo F. Padovan 	err = l2cap_init_sockets();
50720a708f8fSGustavo F. Padovan 	if (err < 0)
50730a708f8fSGustavo F. Padovan 		return err;
50740a708f8fSGustavo F. Padovan 
50750a708f8fSGustavo F. Padovan 	if (bt_debugfs) {
50760a708f8fSGustavo F. Padovan 		l2cap_debugfs = debugfs_create_file("l2cap", 0444,
50770a708f8fSGustavo F. Padovan 					bt_debugfs, NULL, &l2cap_debugfs_fops);
50780a708f8fSGustavo F. Padovan 		if (!l2cap_debugfs)
50790a708f8fSGustavo F. Padovan 			BT_ERR("Failed to create L2CAP debug file");
50800a708f8fSGustavo F. Padovan 	}
50810a708f8fSGustavo F. Padovan 
50820a708f8fSGustavo F. Padovan 	return 0;
50830a708f8fSGustavo F. Padovan }
50840a708f8fSGustavo F. Padovan 
508564274518SGustavo F. Padovan void l2cap_exit(void)
50860a708f8fSGustavo F. Padovan {
50870a708f8fSGustavo F. Padovan 	debugfs_remove(l2cap_debugfs);
5088bb58f747SGustavo F. Padovan 	l2cap_cleanup_sockets();
50890a708f8fSGustavo F. Padovan }
50900a708f8fSGustavo F. Padovan 
50910a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644);
50920a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
5093