blob: 2146e0f3b6f868dc032fbbd741af55a7956a23bc [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
Gustavo F. Padovance5706b2010-07-13 11:57:11 -03004 Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
Gustavo F. Padovan5d8868f2010-07-16 16:18:39 -03005 Copyright (C) 2010 Google Inc.
Gustavo F. Padovan590051d2011-12-18 13:39:33 -02006 Copyright (C) 2011 ProFUSION Embedded Systems
Mat Martineau422e9252012-04-27 16:50:55 -07007 Copyright (c) 2012 Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License version 2 as
13 published by the Free Software Foundation;
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
18 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090019 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090024 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
25 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 SOFTWARE IS DISCLAIMED.
27*/
28
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -020029/* Bluetooth L2CAP core. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32
Marcel Holtmannaef7d972010-03-21 05:27:45 +010033#include <linux/debugfs.h>
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -030034#include <linux/crc16.h>
Daniel Borkmanndbb50882016-07-27 11:40:14 -070035#include <linux/filter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <net/bluetooth/bluetooth.h>
38#include <net/bluetooth/hci_core.h>
39#include <net/bluetooth/l2cap.h>
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070040
Marcel Holtmannac4b7232013-10-10 14:54:16 -070041#include "smp.h"
Marcel Holtmann70247282013-10-10 14:54:15 -070042#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070043#include "amp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Johan Hedberg0f1bfe42014-01-27 15:11:35 -080045#define LE_FLOWCTL_MAX_CREDITS 65535
46
Mat Martineaud1de6d42012-05-17 20:53:55 -070047bool disable_ertm;
Marcel Holtmannf0709e02007-10-20 13:38:51 +020048
Marcel Holtmann547d1032013-10-12 08:18:19 -070049static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Johannes Bergb5ad8b72011-06-01 08:54:45 +020051static LIST_HEAD(chan_list);
52static DEFINE_RWLOCK(chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +010055 u8 code, u8 ident, u16 dlen, void *data);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -030056static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
Gustavo Padovan2d792812012-10-06 10:07:01 +010057 void *data);
Ben Serie860d2c2017-09-09 23:15:59 +020058static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data, size_t data_size);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +020059static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Gustavo Padovand6603662012-05-21 13:58:22 -030061static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
Gustavo Padovan2d792812012-10-06 10:07:01 +010062 struct sk_buff_head *skbs, u8 event);
Mat Martineau608bcc62012-05-17 20:53:32 -070063
Johan Hedberga250e042015-01-15 13:06:44 +020064static inline u8 bdaddr_type(u8 link_type, u8 bdaddr_type)
Marcel Holtmann4f1654e2013-10-13 08:50:41 -070065{
Johan Hedberga250e042015-01-15 13:06:44 +020066 if (link_type == LE_LINK) {
67 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann4f1654e2013-10-13 08:50:41 -070068 return BDADDR_LE_PUBLIC;
69 else
70 return BDADDR_LE_RANDOM;
71 }
72
73 return BDADDR_BREDR;
74}
75
Johan Hedberga250e042015-01-15 13:06:44 +020076static inline u8 bdaddr_src_type(struct hci_conn *hcon)
77{
78 return bdaddr_type(hcon->type, hcon->src_type);
79}
80
81static inline u8 bdaddr_dst_type(struct hci_conn *hcon)
82{
83 return bdaddr_type(hcon->type, hcon->dst_type);
84}
85
Marcel Holtmann01394182006-07-03 10:02:46 +020086/* ---- L2CAP channels ---- */
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -030087
Gustavo Padovan2d792812012-10-06 10:07:01 +010088static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
89 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +020090{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020091 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030092
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020093 list_for_each_entry(c, &conn->chan_l, list) {
94 if (c->dcid == cid)
95 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +020096 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020097 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +020098}
99
Gustavo Padovan2d792812012-10-06 10:07:01 +0100100static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn,
101 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +0200102{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200103 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300104
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200105 list_for_each_entry(c, &conn->chan_l, list) {
106 if (c->scid == cid)
107 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200108 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200109 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200110}
111
112/* Find channel with given SCID.
Mat Martineauef191ad2012-05-02 09:42:00 -0700113 * Returns locked channel. */
Gustavo Padovan2d792812012-10-06 10:07:01 +0100114static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn,
115 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +0200116{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300117 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300118
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200119 mutex_lock(&conn->chan_lock);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300120 c = __l2cap_get_chan_by_scid(conn, cid);
Mat Martineauef191ad2012-05-02 09:42:00 -0700121 if (c)
122 l2cap_chan_lock(c);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200123 mutex_unlock(&conn->chan_lock);
124
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300125 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200126}
127
Mat Martineaub1a130b2012-10-23 15:24:09 -0700128/* Find channel with given DCID.
129 * Returns locked channel.
130 */
131static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
132 u16 cid)
133{
134 struct l2cap_chan *c;
135
136 mutex_lock(&conn->chan_lock);
137 c = __l2cap_get_chan_by_dcid(conn, cid);
138 if (c)
139 l2cap_chan_lock(c);
140 mutex_unlock(&conn->chan_lock);
141
142 return c;
143}
144
Gustavo Padovan2d792812012-10-06 10:07:01 +0100145static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn,
146 u8 ident)
Marcel Holtmann01394182006-07-03 10:02:46 +0200147{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200148 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300149
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200150 list_for_each_entry(c, &conn->chan_l, list) {
151 if (c->ident == ident)
152 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200153 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200154 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200155}
156
Mat Martineau5b155ef2012-10-23 15:24:14 -0700157static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn,
158 u8 ident)
159{
160 struct l2cap_chan *c;
161
162 mutex_lock(&conn->chan_lock);
163 c = __l2cap_get_chan_by_ident(conn, ident);
164 if (c)
165 l2cap_chan_lock(c);
166 mutex_unlock(&conn->chan_lock);
167
168 return c;
169}
170
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300171static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300172{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300173 struct l2cap_chan *c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300174
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300175 list_for_each_entry(c, &chan_list, global_l) {
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700176 if (c->sport == psm && !bacmp(&c->src, src))
Szymon Janc250938c2011-11-16 09:32:22 +0100177 return c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300178 }
Szymon Janc250938c2011-11-16 09:32:22 +0100179 return NULL;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300180}
181
182int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
183{
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300184 int err;
185
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200186 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300187
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300188 if (psm && __l2cap_global_chan_by_addr(psm, src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300189 err = -EADDRINUSE;
190 goto done;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300191 }
192
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300193 if (psm) {
194 chan->psm = psm;
195 chan->sport = psm;
196 err = 0;
197 } else {
Johan Hedberg92594a52016-01-26 17:19:10 -0500198 u16 p, start, end, incr;
199
200 if (chan->src_type == BDADDR_BREDR) {
201 start = L2CAP_PSM_DYN_START;
202 end = L2CAP_PSM_AUTO_END;
203 incr = 2;
204 } else {
205 start = L2CAP_PSM_LE_DYN_START;
206 end = L2CAP_PSM_LE_DYN_END;
207 incr = 1;
208 }
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300209
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300210 err = -EINVAL;
Johan Hedberg92594a52016-01-26 17:19:10 -0500211 for (p = start; p <= end; p += incr)
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300212 if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300213 chan->psm = cpu_to_le16(p);
214 chan->sport = cpu_to_le16(p);
215 err = 0;
216 break;
217 }
218 }
219
220done:
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200221 write_unlock(&chan_list_lock);
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300222 return err;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300223}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300224EXPORT_SYMBOL_GPL(l2cap_add_psm);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300225
226int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid)
227{
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200228 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300229
Johan Hedberg14824302014-08-07 22:56:50 +0300230 /* Override the defaults (which are for conn-oriented) */
231 chan->omtu = L2CAP_DEFAULT_MTU;
232 chan->chan_type = L2CAP_CHAN_FIXED;
233
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300234 chan->scid = scid;
235
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200236 write_unlock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300237
238 return 0;
239}
240
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300241static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
Marcel Holtmann01394182006-07-03 10:02:46 +0200242{
Johan Hedberge77af752013-10-08 10:31:00 +0200243 u16 cid, dyn_end;
Marcel Holtmann01394182006-07-03 10:02:46 +0200244
Johan Hedberge77af752013-10-08 10:31:00 +0200245 if (conn->hcon->type == LE_LINK)
246 dyn_end = L2CAP_CID_LE_DYN_END;
247 else
248 dyn_end = L2CAP_CID_DYN_END;
249
Johan Hedbergab0c1272015-11-02 14:39:16 +0200250 for (cid = L2CAP_CID_DYN_START; cid <= dyn_end; cid++) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300251 if (!__l2cap_get_chan_by_scid(conn, cid))
Marcel Holtmann01394182006-07-03 10:02:46 +0200252 return cid;
253 }
254
255 return 0;
256}
257
Gustavo Padovanf93fa272013-10-21 14:21:40 -0200258static void l2cap_state_change(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300259{
Andrei Emeltchenko42d2d872012-02-17 11:40:57 +0200260 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100261 state_to_string(state));
Gustavo F. Padovanbadaaa02011-11-23 20:11:46 -0200262
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300263 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300264 chan->ops->state_change(chan, state, 0);
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300265}
266
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300267static inline void l2cap_state_change_and_error(struct l2cap_chan *chan,
268 int state, int err)
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200269{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300270 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300271 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200272}
273
274static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err)
275{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300276 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200277}
278
Mat Martineau4239d162012-05-17 20:53:49 -0700279static void __set_retrans_timer(struct l2cap_chan *chan)
280{
281 if (!delayed_work_pending(&chan->monitor_timer) &&
282 chan->retrans_timeout) {
283 l2cap_set_timer(chan, &chan->retrans_timer,
284 msecs_to_jiffies(chan->retrans_timeout));
285 }
286}
287
288static void __set_monitor_timer(struct l2cap_chan *chan)
289{
290 __clear_retrans_timer(chan);
291 if (chan->monitor_timeout) {
292 l2cap_set_timer(chan, &chan->monitor_timer,
293 msecs_to_jiffies(chan->monitor_timeout));
294 }
295}
296
Mat Martineau608bcc62012-05-17 20:53:32 -0700297static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
298 u16 seq)
299{
300 struct sk_buff *skb;
301
302 skb_queue_walk(head, skb) {
Johan Hedberga4368ff2015-03-30 23:21:01 +0300303 if (bt_cb(skb)->l2cap.txseq == seq)
Mat Martineau608bcc62012-05-17 20:53:32 -0700304 return skb;
305 }
306
307 return NULL;
308}
309
Mat Martineau3c588192012-04-11 10:48:42 -0700310/* ---- L2CAP sequence number lists ---- */
311
312/* For ERTM, ordered lists of sequence numbers must be tracked for
313 * SREJ requests that are received and for frames that are to be
314 * retransmitted. These seq_list functions implement a singly-linked
315 * list in an array, where membership in the list can also be checked
316 * in constant time. Items can also be added to the tail of the list
317 * and removed from the head in constant time, without further memory
318 * allocs or frees.
319 */
320
321static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
322{
323 size_t alloc_size, i;
324
325 /* Allocated size is a power of 2 to map sequence numbers
326 * (which may be up to 14 bits) in to a smaller array that is
327 * sized for the negotiated ERTM transmit windows.
328 */
329 alloc_size = roundup_pow_of_two(size);
330
Kees Cook6da2ec52018-06-12 13:55:00 -0700331 seq_list->list = kmalloc_array(alloc_size, sizeof(u16), GFP_KERNEL);
Mat Martineau3c588192012-04-11 10:48:42 -0700332 if (!seq_list->list)
333 return -ENOMEM;
334
335 seq_list->mask = alloc_size - 1;
336 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
337 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
338 for (i = 0; i < alloc_size; i++)
339 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
340
341 return 0;
342}
343
344static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
345{
346 kfree(seq_list->list);
347}
348
349static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
350 u16 seq)
351{
352 /* Constant-time check for list membership */
353 return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
354}
355
Mat Martineau3c588192012-04-11 10:48:42 -0700356static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
357{
Johan Hedberg03a0c5d2014-01-18 21:32:59 +0200358 u16 seq = seq_list->head;
359 u16 mask = seq_list->mask;
360
361 seq_list->head = seq_list->list[seq & mask];
362 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
363
364 if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
365 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
366 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
367 }
368
369 return seq;
Mat Martineau3c588192012-04-11 10:48:42 -0700370}
371
372static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
373{
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300374 u16 i;
Mat Martineau3c588192012-04-11 10:48:42 -0700375
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300376 if (seq_list->head == L2CAP_SEQ_LIST_CLEAR)
377 return;
378
379 for (i = 0; i <= seq_list->mask; i++)
380 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
381
382 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
383 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
Mat Martineau3c588192012-04-11 10:48:42 -0700384}
385
386static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
387{
388 u16 mask = seq_list->mask;
389
390 /* All appends happen in constant time */
391
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300392 if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR)
393 return;
Mat Martineau3c588192012-04-11 10:48:42 -0700394
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300395 if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
396 seq_list->head = seq;
397 else
398 seq_list->list[seq_list->tail & mask] = seq;
399
400 seq_list->tail = seq;
401 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
Mat Martineau3c588192012-04-11 10:48:42 -0700402}
403
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300404static void l2cap_chan_timeout(struct work_struct *work)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300405{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300406 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100407 chan_timer.work);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200408 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300409 int reason;
410
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200411 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300412
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200413 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200414 l2cap_chan_lock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300415
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300416 if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300417 reason = ECONNREFUSED;
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300418 else if (chan->state == BT_CONNECT &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100419 chan->sec_level != BT_SECURITY_SDP)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300420 reason = ECONNREFUSED;
421 else
422 reason = ETIMEDOUT;
423
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300424 l2cap_chan_close(chan, reason);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300425
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200426 l2cap_chan_unlock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300427
Gustavo Padovan80b98022012-05-27 22:27:51 -0300428 chan->ops->close(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200429 mutex_unlock(&conn->chan_lock);
430
Ulisses Furquim371fd832011-12-21 20:02:36 -0200431 l2cap_chan_put(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300432}
433
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300434struct l2cap_chan *l2cap_chan_create(void)
Marcel Holtmann01394182006-07-03 10:02:46 +0200435{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300436 struct l2cap_chan *chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200437
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300438 chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
439 if (!chan)
440 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200441
Andrei Emeltchenkoc03b3552012-02-21 12:54:56 +0200442 mutex_init(&chan->lock);
443
Johan Hedbergff714112014-11-13 09:46:04 +0200444 /* Set default lock nesting level */
445 atomic_set(&chan->nesting, L2CAP_NESTING_NORMAL);
446
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200447 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300448 list_add(&chan->global_l, &chan_list);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200449 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300450
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300451 INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300452
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300453 chan->state = BT_OPEN;
454
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530455 kref_init(&chan->kref);
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -0300456
Mat Martineau28270112012-05-17 21:14:09 -0700457 /* This flag is cleared in l2cap_chan_ready() */
458 set_bit(CONF_NOT_COMPLETE, &chan->conf_state);
459
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300460 BT_DBG("chan %p", chan);
Szymon Jancabc545b2011-11-03 16:05:44 +0100461
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300462 return chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200463}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300464EXPORT_SYMBOL_GPL(l2cap_chan_create);
Marcel Holtmann01394182006-07-03 10:02:46 +0200465
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530466static void l2cap_chan_destroy(struct kref *kref)
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300467{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530468 struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref);
469
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530470 BT_DBG("chan %p", chan);
471
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200472 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300473 list_del(&chan->global_l);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200474 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300475
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530476 kfree(chan);
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300477}
478
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530479void l2cap_chan_hold(struct l2cap_chan *c)
480{
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100481 BT_DBG("chan %p orig refcnt %d", c, kref_read(&c->kref));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530482
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530483 kref_get(&c->kref);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530484}
485
486void l2cap_chan_put(struct l2cap_chan *c)
487{
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100488 BT_DBG("chan %p orig refcnt %d", c, kref_read(&c->kref));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530489
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530490 kref_put(&c->kref, l2cap_chan_destroy);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530491}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300492EXPORT_SYMBOL_GPL(l2cap_chan_put);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530493
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300494void l2cap_chan_set_defaults(struct l2cap_chan *chan)
495{
496 chan->fcs = L2CAP_FCS_CRC16;
497 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
498 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
499 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300500 chan->remote_max_tx = chan->max_tx;
501 chan->remote_tx_win = chan->tx_win;
Mat Martineauc20f8e32012-07-10 05:47:07 -0700502 chan->ack_win = L2CAP_DEFAULT_TX_WINDOW;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300503 chan->sec_level = BT_SECURITY_LOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300504 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
505 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
506 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
507 chan->conf_state = 0;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300508
509 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
510}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300511EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults);
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300512
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200513static void l2cap_le_flowctl_init(struct l2cap_chan *chan)
Johan Hedberg38319712013-05-17 12:49:23 +0300514{
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200515 chan->sdu = NULL;
516 chan->sdu_last_frag = NULL;
517 chan->sdu_len = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300518 chan->tx_credits = 0;
Luiz Augusto von Dentzfe149312018-09-04 13:39:20 +0300519 /* Derive MPS from connection MTU to stop HCI fragmentation */
520 chan->mps = min_t(u16, chan->imtu, chan->conn->mtu - L2CAP_HDR_SIZE);
Luiz Augusto von Dentz96cd8ea2018-09-04 13:39:21 +0300521 /* Give enough credits for a full packet */
522 chan->rx_credits = (chan->imtu / chan->mps) + 1;
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200523
524 skb_queue_head_init(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300525}
526
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300527void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Marcel Holtmann01394182006-07-03 10:02:46 +0200528{
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -0300529 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
Andrei Emeltchenko097db762012-03-09 14:16:17 +0200530 __le16_to_cpu(chan->psm), chan->dcid);
Marcel Holtmann01394182006-07-03 10:02:46 +0200531
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +0200532 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100533
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300534 chan->conn = conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200535
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200536 switch (chan->chan_type) {
537 case L2CAP_CHAN_CONN_ORIENTED:
Johan Hedberg21626e62014-01-24 10:35:41 +0200538 /* Alloc CID for connection-oriented socket */
539 chan->scid = l2cap_alloc_cid(conn);
540 if (conn->hcon->type == ACL_LINK)
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300541 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200542 break;
543
544 case L2CAP_CHAN_CONN_LESS:
Marcel Holtmann01394182006-07-03 10:02:46 +0200545 /* Connectionless socket */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300546 chan->scid = L2CAP_CID_CONN_LESS;
547 chan->dcid = L2CAP_CID_CONN_LESS;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300548 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200549 break;
550
Johan Hedberg2338a7e2014-01-24 10:35:40 +0200551 case L2CAP_CHAN_FIXED:
552 /* Caller will set CID and CID specific MTU values */
Andrei Emeltchenko416fa752012-05-29 13:59:16 +0300553 break;
554
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200555 default:
Marcel Holtmann01394182006-07-03 10:02:46 +0200556 /* Raw socket can send/recv signalling messages only */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300557 chan->scid = L2CAP_CID_SIGNALING;
558 chan->dcid = L2CAP_CID_SIGNALING;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300559 chan->omtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann01394182006-07-03 10:02:46 +0200560 }
561
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300562 chan->local_id = L2CAP_BESTEFFORT_ID;
563 chan->local_stype = L2CAP_SERV_BESTEFFORT;
564 chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE;
565 chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME;
566 chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT;
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +0300567 chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO;
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300568
Ulisses Furquim371fd832011-12-21 20:02:36 -0200569 l2cap_chan_hold(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300570
Johan Hedbergc16900c2014-08-15 21:17:06 +0300571 /* Only keep a reference for fixed channels if they requested it */
572 if (chan->chan_type != L2CAP_CHAN_FIXED ||
573 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags))
574 hci_conn_hold(conn->hcon);
Johan Hedberg5ee98912013-04-29 19:35:43 +0300575
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200576 list_add(&chan->list, &conn->chan_l);
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200577}
578
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300579void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200580{
581 mutex_lock(&conn->chan_lock);
582 __l2cap_chan_add(conn, chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200583 mutex_unlock(&conn->chan_lock);
Marcel Holtmann01394182006-07-03 10:02:46 +0200584}
585
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300586void l2cap_chan_del(struct l2cap_chan *chan, int err)
Marcel Holtmann01394182006-07-03 10:02:46 +0200587{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300588 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200589
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -0300590 __clear_chan_timer(chan);
Marcel Holtmann01394182006-07-03 10:02:46 +0200591
Johan Hedberg49d11742014-11-13 14:37:50 +0200592 BT_DBG("chan %p, conn %p, err %d, state %s", chan, conn, err,
593 state_to_string(chan->state));
Marcel Holtmann01394182006-07-03 10:02:46 +0200594
Johan Hedberg72847ce2014-08-08 09:28:03 +0300595 chan->ops->teardown(chan, err);
596
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900597 if (conn) {
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300598 struct amp_mgr *mgr = conn->hcon->amp_mgr;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300599 /* Delete from channel list */
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200600 list_del(&chan->list);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -0200601
Ulisses Furquim371fd832011-12-21 20:02:36 -0200602 l2cap_chan_put(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300603
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300604 chan->conn = NULL;
Andrei Emeltchenko3cabbfd2012-05-31 11:01:37 +0300605
Johan Hedbergc16900c2014-08-15 21:17:06 +0300606 /* Reference was only held for non-fixed channels or
607 * fixed channels that explicitly requested it using the
608 * FLAG_HOLD_HCI_CONN flag.
609 */
610 if (chan->chan_type != L2CAP_CHAN_FIXED ||
611 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags))
David Herrmann76a68ba2013-04-06 20:28:37 +0200612 hci_conn_drop(conn->hcon);
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300613
614 if (mgr && mgr->bredr_chan == chan)
615 mgr->bredr_chan = NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200616 }
617
Andrei Emeltchenko419e08c2012-10-31 15:46:34 +0200618 if (chan->hs_hchan) {
619 struct hci_chan *hs_hchan = chan->hs_hchan;
620
621 BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan);
622 amp_disconnect_logical_link(hs_hchan);
623 }
624
Mat Martineau28270112012-05-17 21:14:09 -0700625 if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state))
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300626 return;
Gustavo F. Padovan2ead70b2011-04-01 15:13:36 -0300627
Gustavo Padovanee556f62012-05-18 20:22:38 -0300628 switch(chan->mode) {
629 case L2CAP_MODE_BASIC:
630 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300631
Johan Hedberg38319712013-05-17 12:49:23 +0300632 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +0300633 skb_queue_purge(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300634 break;
635
Gustavo Padovanee556f62012-05-18 20:22:38 -0300636 case L2CAP_MODE_ERTM:
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -0300637 __clear_retrans_timer(chan);
638 __clear_monitor_timer(chan);
639 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300640
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -0300641 skb_queue_purge(&chan->srej_q);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300642
Mat Martineau3c588192012-04-11 10:48:42 -0700643 l2cap_seq_list_free(&chan->srej_list);
644 l2cap_seq_list_free(&chan->retrans_list);
Gustavo Padovanee556f62012-05-18 20:22:38 -0300645
646 /* fall through */
647
648 case L2CAP_MODE_STREAMING:
649 skb_queue_purge(&chan->tx_q);
650 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300651 }
Gustavo Padovanee556f62012-05-18 20:22:38 -0300652
653 return;
Marcel Holtmann01394182006-07-03 10:02:46 +0200654}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300655EXPORT_SYMBOL_GPL(l2cap_chan_del);
Marcel Holtmann01394182006-07-03 10:02:46 +0200656
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300657static void l2cap_conn_update_id_addr(struct work_struct *work)
Johan Hedberg387a33e2014-02-18 21:41:33 +0200658{
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300659 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
660 id_addr_update_work);
661 struct hci_conn *hcon = conn->hcon;
Johan Hedberg387a33e2014-02-18 21:41:33 +0200662 struct l2cap_chan *chan;
663
664 mutex_lock(&conn->chan_lock);
665
666 list_for_each_entry(chan, &conn->chan_l, list) {
667 l2cap_chan_lock(chan);
668 bacpy(&chan->dst, &hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +0200669 chan->dst_type = bdaddr_dst_type(hcon);
Johan Hedberg387a33e2014-02-18 21:41:33 +0200670 l2cap_chan_unlock(chan);
671 }
672
673 mutex_unlock(&conn->chan_lock);
674}
675
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300676static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan)
677{
678 struct l2cap_conn *conn = chan->conn;
679 struct l2cap_le_conn_rsp rsp;
680 u16 result;
681
682 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
Mallikarjun Phulari571f7392018-10-05 14:48:12 +0530683 result = L2CAP_CR_LE_AUTHORIZATION;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300684 else
Mallikarjun Phulari571f7392018-10-05 14:48:12 +0530685 result = L2CAP_CR_LE_BAD_PSM;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300686
687 l2cap_state_change(chan, BT_DISCONN);
688
689 rsp.dcid = cpu_to_le16(chan->scid);
690 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +0200691 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300692 rsp.credits = cpu_to_le16(chan->rx_credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300693 rsp.result = cpu_to_le16(result);
694
695 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
696 &rsp);
697}
698
Johan Hedberg791d60f2013-05-14 22:24:44 +0300699static void l2cap_chan_connect_reject(struct l2cap_chan *chan)
700{
701 struct l2cap_conn *conn = chan->conn;
702 struct l2cap_conn_rsp rsp;
703 u16 result;
704
705 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
706 result = L2CAP_CR_SEC_BLOCK;
707 else
708 result = L2CAP_CR_BAD_PSM;
709
710 l2cap_state_change(chan, BT_DISCONN);
711
712 rsp.scid = cpu_to_le16(chan->dcid);
713 rsp.dcid = cpu_to_le16(chan->scid);
714 rsp.result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700715 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Johan Hedberg791d60f2013-05-14 22:24:44 +0300716
717 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
718}
719
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300720void l2cap_chan_close(struct l2cap_chan *chan, int reason)
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300721{
722 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300723
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700724 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300725
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300726 switch (chan->state) {
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300727 case BT_LISTEN:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100728 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300729 break;
730
731 case BT_CONNECTED:
732 case BT_CONFIG:
Johan Hedberg7b25c9b2014-01-28 15:28:04 -0800733 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
Gustavo Padovan8d836d72013-10-15 19:24:47 -0300734 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +0200735 l2cap_send_disconn_req(chan, reason);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300736 } else
737 l2cap_chan_del(chan, reason);
738 break;
739
740 case BT_CONNECT2:
Johan Hedberg791d60f2013-05-14 22:24:44 +0300741 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
742 if (conn->hcon->type == ACL_LINK)
743 l2cap_chan_connect_reject(chan);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300744 else if (conn->hcon->type == LE_LINK)
745 l2cap_chan_le_connect_reject(chan);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300746 }
747
748 l2cap_chan_del(chan, reason);
749 break;
750
751 case BT_CONNECT:
752 case BT_DISCONN:
753 l2cap_chan_del(chan, reason);
754 break;
755
756 default:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100757 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300758 break;
759 }
760}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300761EXPORT_SYMBOL(l2cap_chan_close);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300762
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300763static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
Johan Hedberg8556edd32011-01-19 12:06:50 +0530764{
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700765 switch (chan->chan_type) {
766 case L2CAP_CHAN_RAW:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300767 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530768 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800769 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530770 return HCI_AT_DEDICATED_BONDING_MITM;
771 case BT_SECURITY_MEDIUM:
772 return HCI_AT_DEDICATED_BONDING;
773 default:
774 return HCI_AT_NO_BONDING;
775 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700776 break;
Marcel Holtmann3124b842013-10-12 07:19:32 -0700777 case L2CAP_CHAN_CONN_LESS:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700778 if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) {
Marcel Holtmann3124b842013-10-12 07:19:32 -0700779 if (chan->sec_level == BT_SECURITY_LOW)
780 chan->sec_level = BT_SECURITY_SDP;
781 }
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800782 if (chan->sec_level == BT_SECURITY_HIGH ||
783 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann3124b842013-10-12 07:19:32 -0700784 return HCI_AT_NO_BONDING_MITM;
785 else
786 return HCI_AT_NO_BONDING;
787 break;
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700788 case L2CAP_CHAN_CONN_ORIENTED:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700789 if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) {
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700790 if (chan->sec_level == BT_SECURITY_LOW)
791 chan->sec_level = BT_SECURITY_SDP;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530792
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800793 if (chan->sec_level == BT_SECURITY_HIGH ||
794 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700795 return HCI_AT_NO_BONDING_MITM;
796 else
797 return HCI_AT_NO_BONDING;
798 }
799 /* fall through */
800 default:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300801 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530802 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800803 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530804 return HCI_AT_GENERAL_BONDING_MITM;
805 case BT_SECURITY_MEDIUM:
806 return HCI_AT_GENERAL_BONDING;
807 default:
808 return HCI_AT_NO_BONDING;
809 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700810 break;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530811 }
812}
813
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200814/* Service level security */
Johan Hedberge7cafc42014-07-17 15:35:38 +0300815int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator)
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200816{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300817 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100818 __u8 auth_type;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200819
Johan Hedberga17de2f2013-05-14 13:25:37 +0300820 if (conn->hcon->type == LE_LINK)
821 return smp_conn_security(conn->hcon, chan->sec_level);
822
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300823 auth_type = l2cap_get_auth_type(chan);
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100824
Johan Hedberge7cafc42014-07-17 15:35:38 +0300825 return hci_conn_security(conn->hcon, chan->sec_level, auth_type,
826 initiator);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200827}
828
Johannes Bergb5ad8b72011-06-01 08:54:45 +0200829static u8 l2cap_get_ident(struct l2cap_conn *conn)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200830{
831 u8 id;
832
833 /* Get next available identificator.
834 * 1 - 128 are used by kernel.
835 * 129 - 199 are reserved.
836 * 200 - 254 are used by utilities like l2ping, etc.
837 */
838
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +0200839 mutex_lock(&conn->ident_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200840
841 if (++conn->tx_ident > 128)
842 conn->tx_ident = 1;
843
844 id = conn->tx_ident;
845
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +0200846 mutex_unlock(&conn->ident_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200847
848 return id;
849}
850
Gustavo Padovan2d792812012-10-06 10:07:01 +0100851static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
852 void *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200853{
854 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200855 u8 flags;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200856
857 BT_DBG("code 0x%2.2x", code);
858
859 if (!skb)
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -0300860 return;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200861
Steven Walterf6af6752014-11-19 09:41:17 -0500862 /* Use NO_FLUSH if supported or we have an LE link (which does
863 * not support auto-flushing packets) */
864 if (lmp_no_flush_capable(conn->hcon->hdev) ||
865 conn->hcon->type == LE_LINK)
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200866 flags = ACL_START_NO_FLUSH;
867 else
868 flags = ACL_START;
869
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700870 bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200871 skb->priority = HCI_PRIO_MAX;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700872
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200873 hci_send_acl(conn->hchan, skb, flags);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200874}
875
Mat Martineau02b0fbb2012-10-23 15:24:10 -0700876static bool __chan_is_moving(struct l2cap_chan *chan)
877{
878 return chan->move_state != L2CAP_MOVE_STABLE &&
879 chan->move_state != L2CAP_MOVE_WAIT_PREPARE;
880}
881
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200882static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
883{
884 struct hci_conn *hcon = chan->conn->hcon;
885 u16 flags;
886
887 BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100888 skb->priority);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200889
Mat Martineaud5f8a752012-10-23 15:24:18 -0700890 if (chan->hs_hcon && !__chan_is_moving(chan)) {
891 if (chan->hs_hchan)
892 hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE);
893 else
894 kfree_skb(skb);
895
896 return;
897 }
898
Steven Walterf6af6752014-11-19 09:41:17 -0500899 /* Use NO_FLUSH for LE links (where this is the only option) or
900 * if the BR/EDR link supports it and flushing has not been
901 * explicitly requested (through FLAG_FLUSHABLE).
902 */
903 if (hcon->type == LE_LINK ||
904 (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
905 lmp_no_flush_capable(hcon->hdev)))
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200906 flags = ACL_START_NO_FLUSH;
907 else
908 flags = ACL_START;
909
910 bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
911 hci_send_acl(chan->conn->hchan, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700914static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control)
915{
916 control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT;
917 control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT;
918
919 if (enh & L2CAP_CTRL_FRAME_TYPE) {
920 /* S-Frame */
921 control->sframe = 1;
922 control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT;
923 control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT;
924
925 control->sar = 0;
926 control->txseq = 0;
927 } else {
928 /* I-Frame */
929 control->sframe = 0;
930 control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
931 control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT;
932
933 control->poll = 0;
934 control->super = 0;
935 }
936}
937
938static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control)
939{
940 control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT;
941 control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT;
942
943 if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) {
944 /* S-Frame */
945 control->sframe = 1;
946 control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT;
947 control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT;
948
949 control->sar = 0;
950 control->txseq = 0;
951 } else {
952 /* I-Frame */
953 control->sframe = 0;
954 control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
955 control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT;
956
957 control->poll = 0;
958 control->super = 0;
959 }
960}
961
962static inline void __unpack_control(struct l2cap_chan *chan,
963 struct sk_buff *skb)
964{
965 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
966 __unpack_extended_control(get_unaligned_le32(skb->data),
Johan Hedberga4368ff2015-03-30 23:21:01 +0300967 &bt_cb(skb)->l2cap);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700968 skb_pull(skb, L2CAP_EXT_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700969 } else {
970 __unpack_enhanced_control(get_unaligned_le16(skb->data),
Johan Hedberga4368ff2015-03-30 23:21:01 +0300971 &bt_cb(skb)->l2cap);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700972 skb_pull(skb, L2CAP_ENH_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700973 }
974}
975
976static u32 __pack_extended_control(struct l2cap_ctrl *control)
977{
978 u32 packed;
979
980 packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT;
981 packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT;
982
983 if (control->sframe) {
984 packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT;
985 packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT;
986 packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
987 } else {
988 packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT;
989 packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT;
990 }
991
992 return packed;
993}
994
995static u16 __pack_enhanced_control(struct l2cap_ctrl *control)
996{
997 u16 packed;
998
999 packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT;
1000 packed |= control->final << L2CAP_CTRL_FINAL_SHIFT;
1001
1002 if (control->sframe) {
1003 packed |= control->poll << L2CAP_CTRL_POLL_SHIFT;
1004 packed |= control->super << L2CAP_CTRL_SUPER_SHIFT;
1005 packed |= L2CAP_CTRL_FRAME_TYPE;
1006 } else {
1007 packed |= control->sar << L2CAP_CTRL_SAR_SHIFT;
1008 packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT;
1009 }
1010
1011 return packed;
1012}
1013
1014static inline void __pack_control(struct l2cap_chan *chan,
1015 struct l2cap_ctrl *control,
1016 struct sk_buff *skb)
1017{
1018 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
1019 put_unaligned_le32(__pack_extended_control(control),
1020 skb->data + L2CAP_HDR_SIZE);
1021 } else {
1022 put_unaligned_le16(__pack_enhanced_control(control),
1023 skb->data + L2CAP_HDR_SIZE);
1024 }
1025}
1026
Gustavo Padovanba7aa642012-05-29 13:29:16 -03001027static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan)
1028{
1029 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1030 return L2CAP_EXT_HDR_SIZE;
1031 else
1032 return L2CAP_ENH_HDR_SIZE;
1033}
1034
Mat Martineaua67d7f62012-05-17 20:53:35 -07001035static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan,
1036 u32 control)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001037{
1038 struct sk_buff *skb;
1039 struct l2cap_hdr *lh;
Gustavo Padovanba7aa642012-05-29 13:29:16 -03001040 int hlen = __ertm_hdr_size(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001041
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001042 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03001043 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001044
Mat Martineaua67d7f62012-05-17 20:53:35 -07001045 skb = bt_skb_alloc(hlen, GFP_KERNEL);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001046
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001047 if (!skb)
Mat Martineaua67d7f62012-05-17 20:53:35 -07001048 return ERR_PTR(-ENOMEM);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001049
Johannes Berg4df864c2017-06-16 14:29:21 +02001050 lh = skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001051 lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001052 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03001053
Mat Martineaua67d7f62012-05-17 20:53:35 -07001054 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1055 put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
1056 else
1057 put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001058
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001059 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineaua67d7f62012-05-17 20:53:35 -07001060 u16 fcs = crc16(0, (u8 *)skb->data, skb->len);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03001061 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001062 }
1063
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001064 skb->priority = HCI_PRIO_MAX;
Mat Martineaua67d7f62012-05-17 20:53:35 -07001065 return skb;
1066}
1067
1068static void l2cap_send_sframe(struct l2cap_chan *chan,
1069 struct l2cap_ctrl *control)
1070{
1071 struct sk_buff *skb;
1072 u32 control_field;
1073
1074 BT_DBG("chan %p, control %p", chan, control);
1075
1076 if (!control->sframe)
1077 return;
1078
Mat Martineaub99e13a2012-10-23 15:24:19 -07001079 if (__chan_is_moving(chan))
1080 return;
1081
Mat Martineaua67d7f62012-05-17 20:53:35 -07001082 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) &&
1083 !control->poll)
1084 control->final = 1;
1085
1086 if (control->super == L2CAP_SUPER_RR)
1087 clear_bit(CONN_RNR_SENT, &chan->conn_state);
1088 else if (control->super == L2CAP_SUPER_RNR)
1089 set_bit(CONN_RNR_SENT, &chan->conn_state);
1090
1091 if (control->super != L2CAP_SUPER_SREJ) {
1092 chan->last_acked_seq = control->reqseq;
1093 __clear_ack_timer(chan);
1094 }
1095
1096 BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq,
1097 control->final, control->poll, control->super);
1098
1099 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1100 control_field = __pack_extended_control(control);
1101 else
1102 control_field = __pack_enhanced_control(control);
1103
1104 skb = l2cap_create_sframe_pdu(chan, control_field);
1105 if (!IS_ERR(skb))
1106 l2cap_do_send(chan, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001107}
1108
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001109static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll)
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001110{
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001111 struct l2cap_ctrl control;
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001112
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001113 BT_DBG("chan %p, poll %d", chan, poll);
1114
1115 memset(&control, 0, sizeof(control));
1116 control.sframe = 1;
1117 control.poll = poll;
1118
1119 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
1120 control.super = L2CAP_SUPER_RNR;
1121 else
1122 control.super = L2CAP_SUPER_RR;
1123
1124 control.reqseq = chan->buffer_seq;
1125 l2cap_send_sframe(chan, &control);
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001126}
1127
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001128static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001129{
Johan Hedberg5ff6f342014-08-07 22:56:43 +03001130 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
1131 return true;
1132
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001133 return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001134}
1135
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001136static bool __amp_capable(struct l2cap_chan *chan)
1137{
1138 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001139 struct hci_dev *hdev;
1140 bool amp_available = false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001141
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02001142 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001143 return false;
1144
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02001145 if (!(conn->remote_fixed_chan & L2CAP_FC_A2MP))
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001146 return false;
1147
1148 read_lock(&hci_dev_list_lock);
1149 list_for_each_entry(hdev, &hci_dev_list, list) {
1150 if (hdev->amp_type != AMP_TYPE_BREDR &&
1151 test_bit(HCI_UP, &hdev->flags)) {
1152 amp_available = true;
1153 break;
1154 }
1155 }
1156 read_unlock(&hci_dev_list_lock);
1157
1158 if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED)
1159 return amp_available;
Marcel Holtmann848566b2013-10-01 22:59:22 -07001160
1161 return false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001162}
1163
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02001164static bool l2cap_check_efs(struct l2cap_chan *chan)
1165{
1166 /* Check EFS parameters */
1167 return true;
1168}
1169
Andrei Emeltchenko2766be42012-09-27 17:26:21 +03001170void l2cap_send_conn_req(struct l2cap_chan *chan)
Andrei Emeltchenko9b27f352012-02-24 16:00:00 +02001171{
1172 struct l2cap_conn *conn = chan->conn;
1173 struct l2cap_conn_req req;
1174
1175 req.scid = cpu_to_le16(chan->scid);
1176 req.psm = chan->psm;
1177
1178 chan->ident = l2cap_get_ident(conn);
1179
1180 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
1181
1182 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req);
1183}
1184
Mat Martineau8eb200b2012-10-23 15:24:17 -07001185static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id)
1186{
1187 struct l2cap_create_chan_req req;
1188 req.scid = cpu_to_le16(chan->scid);
1189 req.psm = chan->psm;
1190 req.amp_id = amp_id;
1191
1192 chan->ident = l2cap_get_ident(chan->conn);
1193
1194 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ,
1195 sizeof(req), &req);
1196}
1197
Mat Martineau02b0fbb2012-10-23 15:24:10 -07001198static void l2cap_move_setup(struct l2cap_chan *chan)
1199{
1200 struct sk_buff *skb;
1201
1202 BT_DBG("chan %p", chan);
1203
1204 if (chan->mode != L2CAP_MODE_ERTM)
1205 return;
1206
1207 __clear_retrans_timer(chan);
1208 __clear_monitor_timer(chan);
1209 __clear_ack_timer(chan);
1210
1211 chan->retry_count = 0;
1212 skb_queue_walk(&chan->tx_q, skb) {
Johan Hedberga4368ff2015-03-30 23:21:01 +03001213 if (bt_cb(skb)->l2cap.retries)
1214 bt_cb(skb)->l2cap.retries = 1;
Mat Martineau02b0fbb2012-10-23 15:24:10 -07001215 else
1216 break;
1217 }
1218
1219 chan->expected_tx_seq = chan->buffer_seq;
1220
1221 clear_bit(CONN_REJ_ACT, &chan->conn_state);
1222 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
1223 l2cap_seq_list_clear(&chan->retrans_list);
1224 l2cap_seq_list_clear(&chan->srej_list);
1225 skb_queue_purge(&chan->srej_q);
1226
1227 chan->tx_state = L2CAP_TX_STATE_XMIT;
1228 chan->rx_state = L2CAP_RX_STATE_MOVE;
1229
1230 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
1231}
1232
Mat Martineau5f3847a2012-10-23 15:24:12 -07001233static void l2cap_move_done(struct l2cap_chan *chan)
1234{
1235 u8 move_role = chan->move_role;
1236 BT_DBG("chan %p", chan);
1237
1238 chan->move_state = L2CAP_MOVE_STABLE;
1239 chan->move_role = L2CAP_MOVE_ROLE_NONE;
1240
1241 if (chan->mode != L2CAP_MODE_ERTM)
1242 return;
1243
1244 switch (move_role) {
1245 case L2CAP_MOVE_ROLE_INITIATOR:
1246 l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL);
1247 chan->rx_state = L2CAP_RX_STATE_WAIT_F;
1248 break;
1249 case L2CAP_MOVE_ROLE_RESPONDER:
1250 chan->rx_state = L2CAP_RX_STATE_WAIT_P;
1251 break;
1252 }
1253}
1254
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001255static void l2cap_chan_ready(struct l2cap_chan *chan)
1256{
Johan Hedberg315917e2015-02-16 11:42:11 +02001257 /* The channel may have already been flagged as connected in
1258 * case of receiving data before the L2CAP info req/rsp
1259 * procedure is complete.
1260 */
1261 if (chan->state == BT_CONNECTED)
1262 return;
1263
Mat Martineau28270112012-05-17 21:14:09 -07001264 /* This clears all conf flags, including CONF_NOT_COMPLETE */
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001265 chan->conf_state = 0;
1266 __clear_chan_timer(chan);
1267
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02001268 if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits)
1269 chan->ops->suspend(chan);
Johan Hedberg177f8f22013-05-31 17:54:51 +03001270
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001271 chan->state = BT_CONNECTED;
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001272
Andrei Emeltchenkofd83e2c2012-05-30 09:55:32 +03001273 chan->ops->ready(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001274}
1275
Johan Hedbergf1496de2013-05-13 14:15:56 +03001276static void l2cap_le_connect(struct l2cap_chan *chan)
1277{
1278 struct l2cap_conn *conn = chan->conn;
1279 struct l2cap_le_conn_req req;
1280
Johan Hedberg595177f2013-12-02 22:12:22 +02001281 if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags))
1282 return;
1283
Luiz Augusto von Dentzfe149312018-09-04 13:39:20 +03001284 l2cap_le_flowctl_init(chan);
1285
Johan Hedbergf1496de2013-05-13 14:15:56 +03001286 req.psm = chan->psm;
1287 req.scid = cpu_to_le16(chan->scid);
1288 req.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02001289 req.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03001290 req.credits = cpu_to_le16(chan->rx_credits);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001291
1292 chan->ident = l2cap_get_ident(conn);
1293
1294 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ,
1295 sizeof(req), &req);
1296}
1297
1298static void l2cap_le_start(struct l2cap_chan *chan)
1299{
1300 struct l2cap_conn *conn = chan->conn;
1301
1302 if (!smp_conn_security(conn->hcon, chan->sec_level))
1303 return;
1304
1305 if (!chan->psm) {
1306 l2cap_chan_ready(chan);
1307 return;
1308 }
1309
1310 if (chan->state == BT_CONNECT)
1311 l2cap_le_connect(chan);
1312}
1313
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001314static void l2cap_start_connection(struct l2cap_chan *chan)
1315{
1316 if (__amp_capable(chan)) {
1317 BT_DBG("chan %p AMP capable: discover AMPs", chan);
1318 a2mp_discover_amp(chan);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001319 } else if (chan->conn->hcon->type == LE_LINK) {
1320 l2cap_le_start(chan);
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001321 } else {
1322 l2cap_send_conn_req(chan);
1323 }
1324}
1325
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001326static void l2cap_request_info(struct l2cap_conn *conn)
1327{
1328 struct l2cap_info_req req;
1329
1330 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
1331 return;
1332
1333 req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
1334
1335 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
1336 conn->info_ident = l2cap_get_ident(conn);
1337
1338 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
1339
1340 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
1341 sizeof(req), &req);
1342}
1343
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001344static void l2cap_do_start(struct l2cap_chan *chan)
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001345{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001346 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001347
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001348 if (conn->hcon->type == LE_LINK) {
Johan Hedberg96ac34f2013-05-13 11:15:07 +03001349 l2cap_le_start(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001350 return;
1351 }
1352
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001353 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)) {
1354 l2cap_request_info(conn);
1355 return;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001356 }
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001357
1358 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
1359 return;
1360
1361 if (l2cap_chan_check_security(chan, true) &&
1362 __l2cap_no_conn_pending(chan))
1363 l2cap_start_connection(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001364}
1365
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001366static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
1367{
1368 u32 local_feat_mask = l2cap_feat_mask;
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03001369 if (!disable_ertm)
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001370 local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
1371
1372 switch (mode) {
1373 case L2CAP_MODE_ERTM:
1374 return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
1375 case L2CAP_MODE_STREAMING:
1376 return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
1377 default:
1378 return 0x00;
1379 }
1380}
1381
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001382static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001383{
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001384 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001385 struct l2cap_disconn_req req;
1386
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001387 if (!conn)
1388 return;
1389
Andrei Emeltchenkoaad3d0e2012-09-06 15:05:42 +03001390 if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) {
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001391 __clear_retrans_timer(chan);
1392 __clear_monitor_timer(chan);
1393 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001394 }
1395
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001396 if (chan->scid == L2CAP_CID_A2MP) {
Gustavo Padovand1177732012-10-06 11:47:38 +01001397 l2cap_state_change(chan, BT_DISCONN);
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001398 return;
1399 }
1400
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001401 req.dcid = cpu_to_le16(chan->dcid);
1402 req.scid = cpu_to_le16(chan->scid);
Gustavo Padovan2d792812012-10-06 10:07:01 +01001403 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ,
1404 sizeof(req), &req);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001405
Gustavo Padovanf8e73012013-10-15 19:24:46 -03001406 l2cap_state_change_and_error(chan, BT_DISCONN, err);
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001407}
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409/* ---- L2CAP connections ---- */
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001410static void l2cap_conn_start(struct l2cap_conn *conn)
1411{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001412 struct l2cap_chan *chan, *tmp;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001413
1414 BT_DBG("conn %p", conn);
1415
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001416 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001417
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001418 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001419 l2cap_chan_lock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001420
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001421 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001422 l2cap_chan_ready(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001423 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001424 continue;
1425 }
1426
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001427 if (chan->state == BT_CONNECT) {
Johan Hedberge7cafc42014-07-17 15:35:38 +03001428 if (!l2cap_chan_check_security(chan, true) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001429 !__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001430 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001431 continue;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02001432 }
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001433
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001434 if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
Gustavo Padovan2d792812012-10-06 10:07:01 +01001435 && test_bit(CONF_STATE2_DEVICE,
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001436 &chan->conf_state)) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001437 l2cap_chan_close(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001438 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001439 continue;
1440 }
1441
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001442 l2cap_start_connection(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001443
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001444 } else if (chan->state == BT_CONNECT2) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001445 struct l2cap_conn_rsp rsp;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001446 char buf[128];
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001447 rsp.scid = cpu_to_le16(chan->dcid);
1448 rsp.dcid = cpu_to_le16(chan->scid);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001449
Johan Hedberge7cafc42014-07-17 15:35:38 +03001450 if (l2cap_chan_check_security(chan, false)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07001451 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001452 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1453 rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001454 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001455
1456 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02001457 l2cap_state_change(chan, BT_CONFIG);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001458 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
1459 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001460 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001461 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001462 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1463 rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001464 }
1465
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001466 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001467 sizeof(rsp), &rsp);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001468
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001469 if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001470 rsp.result != L2CAP_CR_SUCCESS) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001471 l2cap_chan_unlock(chan);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001472 continue;
1473 }
1474
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001475 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001476 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Ben Serie860d2c2017-09-09 23:15:59 +02001477 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03001478 chan->num_conf_req++;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001479 }
1480
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001481 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001482 }
1483
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001484 mutex_unlock(&conn->chan_lock);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001485}
1486
Ville Tervob62f3282011-02-10 22:38:50 -03001487static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1488{
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001489 struct hci_conn *hcon = conn->hcon;
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001490 struct hci_dev *hdev = hcon->hdev;
Ville Tervob62f3282011-02-10 22:38:50 -03001491
Johan Hedberge760ec12014-08-07 22:56:47 +03001492 BT_DBG("%s conn %p", hdev->name, conn);
Ville Tervob62f3282011-02-10 22:38:50 -03001493
Johan Hedberge760ec12014-08-07 22:56:47 +03001494 /* For outgoing pairing which doesn't necessarily have an
1495 * associated socket (e.g. mgmt_pair_device).
1496 */
1497 if (hcon->out)
1498 smp_conn_security(hcon, hcon->pending_sec_level);
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001499
Marcel Holtmann80afeb62014-06-23 12:18:51 +02001500 /* For LE slave connections, make sure the connection interval
1501 * is in the range of the minium and maximum interval that has
1502 * been configured for this connection. If not, then trigger
1503 * the connection update procedure.
1504 */
Johan Hedberg40bef302014-07-16 11:42:27 +03001505 if (hcon->role == HCI_ROLE_SLAVE &&
Marcel Holtmann80afeb62014-06-23 12:18:51 +02001506 (hcon->le_conn_interval < hcon->le_conn_min_interval ||
1507 hcon->le_conn_interval > hcon->le_conn_max_interval)) {
1508 struct l2cap_conn_param_update_req req;
1509
1510 req.min = cpu_to_le16(hcon->le_conn_min_interval);
1511 req.max = cpu_to_le16(hcon->le_conn_max_interval);
1512 req.latency = cpu_to_le16(hcon->le_conn_latency);
1513 req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout);
1514
1515 l2cap_send_cmd(conn, l2cap_get_ident(conn),
1516 L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req);
1517 }
Ville Tervob62f3282011-02-10 22:38:50 -03001518}
1519
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001520static void l2cap_conn_ready(struct l2cap_conn *conn)
1521{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001522 struct l2cap_chan *chan;
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001523 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001524
1525 BT_DBG("conn %p", conn);
1526
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001527 if (hcon->type == ACL_LINK)
1528 l2cap_request_info(conn);
1529
Johan Hedberge760ec12014-08-07 22:56:47 +03001530 mutex_lock(&conn->chan_lock);
1531
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001532 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03001533
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001534 l2cap_chan_lock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001535
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001536 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001537 l2cap_chan_unlock(chan);
1538 continue;
1539 }
1540
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001541 if (hcon->type == LE_LINK) {
Johan Hedbergf1496de2013-05-13 14:15:56 +03001542 l2cap_le_start(chan);
Vinicius Costa Gomes63128452011-06-17 22:46:26 -03001543 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001544 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
1545 l2cap_chan_ready(chan);
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001546 } else if (chan->state == BT_CONNECT) {
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001547 l2cap_do_start(chan);
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001548 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001549
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001550 l2cap_chan_unlock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001551 }
1552
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001553 mutex_unlock(&conn->chan_lock);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001554
Johan Hedberg79a05722014-08-08 09:28:04 +03001555 if (hcon->type == LE_LINK)
1556 l2cap_le_conn_ready(conn);
1557
Johan Hedberg61a939c2014-01-17 20:45:11 +02001558 queue_work(hcon->hdev->workqueue, &conn->pending_rx_work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001559}
1560
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001561/* Notify sockets that we cannot guaranty reliability anymore */
1562static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
1563{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001564 struct l2cap_chan *chan;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001565
1566 BT_DBG("conn %p", conn);
1567
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001568 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001569
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001570 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +03001571 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo Padovan1d8b1fd2012-10-06 11:34:52 +01001572 l2cap_chan_set_err(chan, err);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001573 }
1574
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001575 mutex_unlock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001576}
1577
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001578static void l2cap_info_timeout(struct work_struct *work)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001579{
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001580 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001581 info_timer.work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001582
Marcel Holtmann984947d2009-02-06 23:35:19 +01001583 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01001584 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01001585
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001586 l2cap_conn_start(conn);
1587}
1588
David Herrmann2c8e1412013-04-06 20:28:45 +02001589/*
1590 * l2cap_user
1591 * External modules can register l2cap_user objects on l2cap_conn. The ->probe
1592 * callback is called during registration. The ->remove callback is called
1593 * during unregistration.
1594 * An l2cap_user object can either be explicitly unregistered or when the
1595 * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon,
1596 * l2cap->hchan, .. are valid as long as the remove callback hasn't been called.
1597 * External modules must own a reference to the l2cap_conn object if they intend
1598 * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at
1599 * any time if they don't.
1600 */
1601
1602int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user)
1603{
1604 struct hci_dev *hdev = conn->hcon->hdev;
1605 int ret;
1606
1607 /* We need to check whether l2cap_conn is registered. If it is not, we
1608 * must not register the l2cap_user. l2cap_conn_del() is unregisters
1609 * l2cap_conn objects, but doesn't provide its own locking. Instead, it
1610 * relies on the parent hci_conn object to be locked. This itself relies
1611 * on the hci_dev object to be locked. So we must lock the hci device
1612 * here, too. */
1613
1614 hci_dev_lock(hdev);
1615
Alexey Dobriyan835a6a22015-06-10 20:28:33 +03001616 if (!list_empty(&user->list)) {
David Herrmann2c8e1412013-04-06 20:28:45 +02001617 ret = -EINVAL;
1618 goto out_unlock;
1619 }
1620
1621 /* conn->hchan is NULL after l2cap_conn_del() was called */
1622 if (!conn->hchan) {
1623 ret = -ENODEV;
1624 goto out_unlock;
1625 }
1626
1627 ret = user->probe(conn, user);
1628 if (ret)
1629 goto out_unlock;
1630
1631 list_add(&user->list, &conn->users);
1632 ret = 0;
1633
1634out_unlock:
1635 hci_dev_unlock(hdev);
1636 return ret;
1637}
1638EXPORT_SYMBOL(l2cap_register_user);
1639
1640void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user)
1641{
1642 struct hci_dev *hdev = conn->hcon->hdev;
1643
1644 hci_dev_lock(hdev);
1645
Alexey Dobriyan835a6a22015-06-10 20:28:33 +03001646 if (list_empty(&user->list))
David Herrmann2c8e1412013-04-06 20:28:45 +02001647 goto out_unlock;
1648
Tedd Ho-Jeong Anab944c82015-06-30 11:43:40 -07001649 list_del_init(&user->list);
David Herrmann2c8e1412013-04-06 20:28:45 +02001650 user->remove(conn, user);
1651
1652out_unlock:
1653 hci_dev_unlock(hdev);
1654}
1655EXPORT_SYMBOL(l2cap_unregister_user);
1656
1657static void l2cap_unregister_all_users(struct l2cap_conn *conn)
1658{
1659 struct l2cap_user *user;
1660
1661 while (!list_empty(&conn->users)) {
1662 user = list_first_entry(&conn->users, struct l2cap_user, list);
Tedd Ho-Jeong Anab944c82015-06-30 11:43:40 -07001663 list_del_init(&user->list);
David Herrmann2c8e1412013-04-06 20:28:45 +02001664 user->remove(conn, user);
1665 }
1666}
1667
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001668static void l2cap_conn_del(struct hci_conn *hcon, int err)
1669{
1670 struct l2cap_conn *conn = hcon->l2cap_data;
1671 struct l2cap_chan *chan, *l;
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001672
1673 if (!conn)
1674 return;
1675
1676 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
1677
1678 kfree_skb(conn->rx_skb);
1679
Johan Hedberg61a939c2014-01-17 20:45:11 +02001680 skb_queue_purge(&conn->pending_rx);
Jukka Taimisto7ab56c32014-06-12 10:15:13 +00001681
1682 /* We can not call flush_work(&conn->pending_rx_work) here since we
1683 * might block if we are running on a worker from the same workqueue
1684 * pending_rx_work is waiting on.
1685 */
1686 if (work_pending(&conn->pending_rx_work))
1687 cancel_work_sync(&conn->pending_rx_work);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001688
Johan Hedbergf3d82d02014-09-05 22:19:50 +03001689 if (work_pending(&conn->id_addr_update_work))
1690 cancel_work_sync(&conn->id_addr_update_work);
1691
David Herrmann2c8e1412013-04-06 20:28:45 +02001692 l2cap_unregister_all_users(conn);
1693
Johan Hedberge31fb862014-08-18 20:33:28 +03001694 /* Force the connection to be immediately dropped */
1695 hcon->disc_timeout = 0;
1696
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001697 mutex_lock(&conn->chan_lock);
1698
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001699 /* Kill channels */
1700 list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
Mat Martineau61d6ef32012-04-27 16:50:50 -07001701 l2cap_chan_hold(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001702 l2cap_chan_lock(chan);
1703
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001704 l2cap_chan_del(chan, err);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001705
1706 l2cap_chan_unlock(chan);
1707
Gustavo Padovan80b98022012-05-27 22:27:51 -03001708 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07001709 l2cap_chan_put(chan);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001710 }
1711
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001712 mutex_unlock(&conn->chan_lock);
1713
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001714 hci_chan_del(conn->hchan);
1715
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001716 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
Ulisses Furquim127074b2012-01-30 18:26:29 -02001717 cancel_delayed_work_sync(&conn->info_timer);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001718
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001719 hcon->l2cap_data = NULL;
David Herrmann9c903e32013-04-06 20:28:44 +02001720 conn->hchan = NULL;
1721 l2cap_conn_put(conn);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001722}
1723
David Herrmann9c903e32013-04-06 20:28:44 +02001724static void l2cap_conn_free(struct kref *ref)
1725{
1726 struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref);
1727
1728 hci_conn_put(conn->hcon);
1729 kfree(conn);
1730}
1731
Johan Hedberg51bb84572014-08-15 21:06:57 +03001732struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn)
David Herrmann9c903e32013-04-06 20:28:44 +02001733{
1734 kref_get(&conn->ref);
Johan Hedberg51bb84572014-08-15 21:06:57 +03001735 return conn;
David Herrmann9c903e32013-04-06 20:28:44 +02001736}
1737EXPORT_SYMBOL(l2cap_conn_get);
1738
1739void l2cap_conn_put(struct l2cap_conn *conn)
1740{
1741 kref_put(&conn->ref, l2cap_conn_free);
1742}
1743EXPORT_SYMBOL(l2cap_conn_put);
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745/* ---- Socket interface ---- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Ido Yarivc2287682012-04-20 15:46:07 -03001747/* Find socket with psm and source / destination bdaddr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * Returns closest match.
1749 */
Ido Yarivc2287682012-04-20 15:46:07 -03001750static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1751 bdaddr_t *src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001752 bdaddr_t *dst,
1753 u8 link_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001755 struct l2cap_chan *c, *c1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001757 read_lock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001758
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001759 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001760 if (state && c->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 continue;
1762
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001763 if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR)
1764 continue;
1765
1766 if (link_type == LE_LINK && c->src_type == BDADDR_BREDR)
1767 continue;
1768
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001769 if (c->psm == psm) {
Ido Yarivc2287682012-04-20 15:46:07 -03001770 int src_match, dst_match;
1771 int src_any, dst_any;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /* Exact match. */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001774 src_match = !bacmp(&c->src, src);
1775 dst_match = !bacmp(&c->dst, dst);
Ido Yarivc2287682012-04-20 15:46:07 -03001776 if (src_match && dst_match) {
Johan Hedberga24cce12014-08-07 22:56:42 +03001777 l2cap_chan_hold(c);
Johannes Berga7567b22011-06-01 08:29:54 +02001778 read_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001779 return c;
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /* Closest match */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001783 src_any = !bacmp(&c->src, BDADDR_ANY);
1784 dst_any = !bacmp(&c->dst, BDADDR_ANY);
Ido Yarivc2287682012-04-20 15:46:07 -03001785 if ((src_match && dst_any) || (src_any && dst_match) ||
1786 (src_any && dst_any))
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001787 c1 = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Johan Hedberga24cce12014-08-07 22:56:42 +03001791 if (c1)
1792 l2cap_chan_hold(c1);
1793
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001794 read_unlock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001795
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001796 return c1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001799static void l2cap_monitor_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001800{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001801 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001802 monitor_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001803
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03001804 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001805
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001806 l2cap_chan_lock(chan);
1807
Mat Martineau80909e02012-05-17 20:53:50 -07001808 if (!chan->conn) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001809 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001810 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001811 return;
1812 }
1813
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001814 l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001815
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001816 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001817 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001818}
1819
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001820static void l2cap_retrans_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001821{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001822 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001823 retrans_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001824
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03001825 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001826
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001827 l2cap_chan_lock(chan);
1828
Mat Martineau80909e02012-05-17 20:53:50 -07001829 if (!chan->conn) {
1830 l2cap_chan_unlock(chan);
1831 l2cap_chan_put(chan);
1832 return;
1833 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001834
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001835 l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001836 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001837 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001838}
1839
Gustavo Padovand6603662012-05-21 13:58:22 -03001840static void l2cap_streaming_send(struct l2cap_chan *chan,
1841 struct sk_buff_head *skbs)
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001842{
Gustavo F. Padovanccbb84a2010-08-30 18:44:44 -03001843 struct sk_buff *skb;
Mat Martineau37339372012-05-17 20:53:33 -07001844 struct l2cap_ctrl *control;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001845
Mat Martineau37339372012-05-17 20:53:33 -07001846 BT_DBG("chan %p, skbs %p", chan, skbs);
1847
Mat Martineaub99e13a2012-10-23 15:24:19 -07001848 if (__chan_is_moving(chan))
1849 return;
1850
Mat Martineau37339372012-05-17 20:53:33 -07001851 skb_queue_splice_tail_init(skbs, &chan->tx_q);
1852
1853 while (!skb_queue_empty(&chan->tx_q)) {
1854
1855 skb = skb_dequeue(&chan->tx_q);
1856
Johan Hedberga4368ff2015-03-30 23:21:01 +03001857 bt_cb(skb)->l2cap.retries = 1;
1858 control = &bt_cb(skb)->l2cap;
Mat Martineau37339372012-05-17 20:53:33 -07001859
1860 control->reqseq = 0;
1861 control->txseq = chan->next_tx_seq;
1862
1863 __pack_control(chan, control, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001864
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001865 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau37339372012-05-17 20:53:33 -07001866 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1867 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001868 }
1869
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001870 l2cap_do_send(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001871
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001872 BT_DBG("Sent txseq %u", control->txseq);
Mat Martineau37339372012-05-17 20:53:33 -07001873
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001874 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau37339372012-05-17 20:53:33 -07001875 chan->frames_sent++;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001876 }
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001877}
1878
Szymon Janc67c9e842011-07-28 16:24:33 +02001879static int l2cap_ertm_send(struct l2cap_chan *chan)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001880{
1881 struct sk_buff *skb, *tx_skb;
Mat Martineau18a48e72012-05-17 20:53:34 -07001882 struct l2cap_ctrl *control;
1883 int sent = 0;
1884
1885 BT_DBG("chan %p", chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001886
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001887 if (chan->state != BT_CONNECTED)
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001888 return -ENOTCONN;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001889
Mat Martineau94122bb2012-05-02 09:42:02 -07001890 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1891 return 0;
1892
Mat Martineaub99e13a2012-10-23 15:24:19 -07001893 if (__chan_is_moving(chan))
1894 return 0;
1895
Mat Martineau18a48e72012-05-17 20:53:34 -07001896 while (chan->tx_send_head &&
1897 chan->unacked_frames < chan->remote_tx_win &&
1898 chan->tx_state == L2CAP_TX_STATE_XMIT) {
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001899
Mat Martineau18a48e72012-05-17 20:53:34 -07001900 skb = chan->tx_send_head;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001901
Johan Hedberga4368ff2015-03-30 23:21:01 +03001902 bt_cb(skb)->l2cap.retries = 1;
1903 control = &bt_cb(skb)->l2cap;
Gustavo F. Padovan95ffa972010-06-18 20:37:33 -03001904
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001905 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
Mat Martineau18a48e72012-05-17 20:53:34 -07001906 control->final = 1;
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001907
Mat Martineau18a48e72012-05-17 20:53:34 -07001908 control->reqseq = chan->buffer_seq;
1909 chan->last_acked_seq = chan->buffer_seq;
1910 control->txseq = chan->next_tx_seq;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001911
Mat Martineau18a48e72012-05-17 20:53:34 -07001912 __pack_control(chan, control, skb);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001913
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001914 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau18a48e72012-05-17 20:53:34 -07001915 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1916 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001917 }
1918
Mat Martineau18a48e72012-05-17 20:53:34 -07001919 /* Clone after data has been modified. Data is assumed to be
1920 read-only (for locking purposes) on cloned sk_buffs.
1921 */
1922 tx_skb = skb_clone(skb, GFP_KERNEL);
1923
1924 if (!tx_skb)
1925 break;
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -03001926
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001927 __set_retrans_timer(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001928
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001929 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau18a48e72012-05-17 20:53:34 -07001930 chan->unacked_frames++;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03001931 chan->frames_sent++;
Mat Martineau18a48e72012-05-17 20:53:34 -07001932 sent++;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001933
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001934 if (skb_queue_is_last(&chan->tx_q, skb))
1935 chan->tx_send_head = NULL;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001936 else
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001937 chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
Mat Martineau18a48e72012-05-17 20:53:34 -07001938
1939 l2cap_do_send(chan, tx_skb);
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001940 BT_DBG("Sent txseq %u", control->txseq);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001941 }
1942
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001943 BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent,
1944 chan->unacked_frames, skb_queue_len(&chan->tx_q));
Mat Martineau18a48e72012-05-17 20:53:34 -07001945
1946 return sent;
Gustavo F. Padovan9e917af2010-05-01 16:15:37 -03001947}
1948
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001949static void l2cap_ertm_resend(struct l2cap_chan *chan)
1950{
1951 struct l2cap_ctrl control;
1952 struct sk_buff *skb;
1953 struct sk_buff *tx_skb;
1954 u16 seq;
1955
1956 BT_DBG("chan %p", chan);
1957
1958 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1959 return;
1960
Mat Martineaub99e13a2012-10-23 15:24:19 -07001961 if (__chan_is_moving(chan))
1962 return;
1963
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001964 while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) {
1965 seq = l2cap_seq_list_pop(&chan->retrans_list);
1966
1967 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq);
1968 if (!skb) {
1969 BT_DBG("Error: Can't retransmit seq %d, frame missing",
Gustavo Padovan2d792812012-10-06 10:07:01 +01001970 seq);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001971 continue;
1972 }
1973
Johan Hedberga4368ff2015-03-30 23:21:01 +03001974 bt_cb(skb)->l2cap.retries++;
1975 control = bt_cb(skb)->l2cap;
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001976
1977 if (chan->max_tx != 0 &&
Johan Hedberga4368ff2015-03-30 23:21:01 +03001978 bt_cb(skb)->l2cap.retries > chan->max_tx) {
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001979 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001980 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001981 l2cap_seq_list_clear(&chan->retrans_list);
1982 break;
1983 }
1984
1985 control.reqseq = chan->buffer_seq;
1986 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1987 control.final = 1;
1988 else
1989 control.final = 0;
1990
1991 if (skb_cloned(skb)) {
1992 /* Cloned sk_buffs are read-only, so we need a
1993 * writeable copy
1994 */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001995 tx_skb = skb_copy(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001996 } else {
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001997 tx_skb = skb_clone(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001998 }
1999
2000 if (!tx_skb) {
2001 l2cap_seq_list_clear(&chan->retrans_list);
2002 break;
2003 }
2004
2005 /* Update skb contents */
2006 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
2007 put_unaligned_le32(__pack_extended_control(&control),
2008 tx_skb->data + L2CAP_HDR_SIZE);
2009 } else {
2010 put_unaligned_le16(__pack_enhanced_control(&control),
2011 tx_skb->data + L2CAP_HDR_SIZE);
2012 }
2013
Lukasz Rymanowski13cac152014-08-14 09:35:34 +02002014 /* Update FCS */
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002015 if (chan->fcs == L2CAP_FCS_CRC16) {
Lukasz Rymanowski13cac152014-08-14 09:35:34 +02002016 u16 fcs = crc16(0, (u8 *) tx_skb->data,
2017 tx_skb->len - L2CAP_FCS_SIZE);
2018 put_unaligned_le16(fcs, skb_tail_pointer(tx_skb) -
2019 L2CAP_FCS_SIZE);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002020 }
2021
2022 l2cap_do_send(chan, tx_skb);
2023
2024 BT_DBG("Resent txseq %d", control.txseq);
2025
2026 chan->last_acked_seq = chan->buffer_seq;
2027 }
2028}
2029
Mat Martineauf80842a2012-05-17 20:53:46 -07002030static void l2cap_retransmit(struct l2cap_chan *chan,
2031 struct l2cap_ctrl *control)
2032{
2033 BT_DBG("chan %p, control %p", chan, control);
2034
2035 l2cap_seq_list_append(&chan->retrans_list, control->reqseq);
2036 l2cap_ertm_resend(chan);
2037}
2038
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002039static void l2cap_retransmit_all(struct l2cap_chan *chan,
2040 struct l2cap_ctrl *control)
2041{
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002042 struct sk_buff *skb;
2043
2044 BT_DBG("chan %p, control %p", chan, control);
2045
2046 if (control->poll)
2047 set_bit(CONN_SEND_FBIT, &chan->conn_state);
2048
2049 l2cap_seq_list_clear(&chan->retrans_list);
2050
2051 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2052 return;
2053
2054 if (chan->unacked_frames) {
2055 skb_queue_walk(&chan->tx_q, skb) {
Johan Hedberga4368ff2015-03-30 23:21:01 +03002056 if (bt_cb(skb)->l2cap.txseq == control->reqseq ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01002057 skb == chan->tx_send_head)
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002058 break;
2059 }
2060
2061 skb_queue_walk_from(&chan->tx_q, skb) {
2062 if (skb == chan->tx_send_head)
2063 break;
2064
2065 l2cap_seq_list_append(&chan->retrans_list,
Johan Hedberga4368ff2015-03-30 23:21:01 +03002066 bt_cb(skb)->l2cap.txseq);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002067 }
2068
2069 l2cap_ertm_resend(chan);
2070 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002071}
2072
Szymon Jancb17e73b2012-01-11 10:59:47 +01002073static void l2cap_send_ack(struct l2cap_chan *chan)
2074{
Mat Martineau0a0aba42012-05-17 20:53:39 -07002075 struct l2cap_ctrl control;
2076 u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
2077 chan->last_acked_seq);
2078 int threshold;
2079
2080 BT_DBG("chan %p last_acked_seq %d buffer_seq %d",
2081 chan, chan->last_acked_seq, chan->buffer_seq);
2082
2083 memset(&control, 0, sizeof(control));
2084 control.sframe = 1;
2085
2086 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
2087 chan->rx_state == L2CAP_RX_STATE_RECV) {
2088 __clear_ack_timer(chan);
2089 control.super = L2CAP_SUPER_RNR;
2090 control.reqseq = chan->buffer_seq;
2091 l2cap_send_sframe(chan, &control);
2092 } else {
2093 if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) {
2094 l2cap_ertm_send(chan);
2095 /* If any i-frames were sent, they included an ack */
2096 if (chan->buffer_seq == chan->last_acked_seq)
2097 frames_to_ack = 0;
2098 }
2099
Mat Martineauc20f8e32012-07-10 05:47:07 -07002100 /* Ack now if the window is 3/4ths full.
Mat Martineau0a0aba42012-05-17 20:53:39 -07002101 * Calculate without mul or div
2102 */
Mat Martineauc20f8e32012-07-10 05:47:07 -07002103 threshold = chan->ack_win;
Mat Martineau0a0aba42012-05-17 20:53:39 -07002104 threshold += threshold << 1;
2105 threshold >>= 2;
2106
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002107 BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack,
Mat Martineau0a0aba42012-05-17 20:53:39 -07002108 threshold);
2109
2110 if (frames_to_ack >= threshold) {
2111 __clear_ack_timer(chan);
2112 control.super = L2CAP_SUPER_RR;
2113 control.reqseq = chan->buffer_seq;
2114 l2cap_send_sframe(chan, &control);
2115 frames_to_ack = 0;
2116 }
2117
2118 if (frames_to_ack)
2119 __set_ack_timer(chan);
2120 }
Szymon Jancb17e73b2012-01-11 10:59:47 +01002121}
2122
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002123static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan,
2124 struct msghdr *msg, int len,
2125 int count, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002127 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002128 struct sk_buff **frag;
Gustavo Padovan90338942012-04-06 20:15:47 -03002129 int sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Al Virocbbd26b2016-11-01 22:09:04 -04002131 if (!copy_from_iter_full(skb_put(skb, count), count, &msg->msg_iter))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002132 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 sent += count;
2135 len -= count;
2136
2137 /* Continuation fragments (no L2CAP header) */
2138 frag = &skb_shinfo(skb)->frag_list;
2139 while (len) {
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002140 struct sk_buff *tmp;
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 count = min_t(unsigned int, conn->mtu, len);
2143
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002144 tmp = chan->ops->alloc_skb(chan, 0, count,
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002145 msg->msg_flags & MSG_DONTWAIT);
2146 if (IS_ERR(tmp))
2147 return PTR_ERR(tmp);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002148
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002149 *frag = tmp;
2150
Al Virocbbd26b2016-11-01 22:09:04 -04002151 if (!copy_from_iter_full(skb_put(*frag, count), count,
2152 &msg->msg_iter))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002153 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 sent += count;
2156 len -= count;
2157
Gustavo Padovan2d0ed3d2012-05-11 13:16:12 -03002158 skb->len += (*frag)->len;
2159 skb->data_len += (*frag)->len;
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 frag = &(*frag)->next;
2162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 return sent;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002167static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002168 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002169{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002170 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002171 struct sk_buff *skb;
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002172 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002173 struct l2cap_hdr *lh;
2174
Marcel Holtmann8d463212014-06-05 15:22:51 +02002175 BT_DBG("chan %p psm 0x%2.2x len %zu", chan,
2176 __le16_to_cpu(chan->psm), len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002177
2178 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002179
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002180 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002181 msg->msg_flags & MSG_DONTWAIT);
2182 if (IS_ERR(skb))
2183 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002184
2185 /* Create L2CAP header */
Johannes Berg4df864c2017-06-16 14:29:21 +02002186 lh = skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002187 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenkodaf6a78c2012-05-03 10:55:52 +03002188 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE);
Marcel Holtmann43b1b8d2013-10-12 06:01:26 -07002189 put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002190
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002191 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002192 if (unlikely(err < 0)) {
2193 kfree_skb(skb);
2194 return ERR_PTR(err);
2195 }
2196 return skb;
2197}
2198
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002199static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002200 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002201{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002202 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002203 struct sk_buff *skb;
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002204 int err, count;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002205 struct l2cap_hdr *lh;
2206
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002207 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002208
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002209 count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002210
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002211 skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002212 msg->msg_flags & MSG_DONTWAIT);
2213 if (IS_ERR(skb))
2214 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002215
2216 /* Create L2CAP header */
Johannes Berg4df864c2017-06-16 14:29:21 +02002217 lh = skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002218 lh->cid = cpu_to_le16(chan->dcid);
Gustavo Padovan6ff9b5e2012-05-02 11:56:17 -03002219 lh->len = cpu_to_le16(len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002220
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002221 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002222 if (unlikely(err < 0)) {
2223 kfree_skb(skb);
2224 return ERR_PTR(err);
2225 }
2226 return skb;
2227}
2228
Luiz Augusto von Dentzab0ff762011-09-12 20:00:50 +03002229static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002230 struct msghdr *msg, size_t len,
2231 u16 sdulen)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002232{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002233 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002234 struct sk_buff *skb;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002235 int err, count, hlen;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002236 struct l2cap_hdr *lh;
2237
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002238 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002239
Gustavo F. Padovan0ee0d202010-05-01 16:15:41 -03002240 if (!conn)
2241 return ERR_PTR(-ENOTCONN);
2242
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002243 hlen = __ertm_hdr_size(chan);
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002244
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002245 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002246 hlen += L2CAP_SDULEN_SIZE;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002247
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03002248 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002249 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03002250
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002251 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002252
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002253 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002254 msg->msg_flags & MSG_DONTWAIT);
2255 if (IS_ERR(skb))
2256 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002257
2258 /* Create L2CAP header */
Johannes Berg4df864c2017-06-16 14:29:21 +02002259 lh = skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002260 lh->cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002261 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002262
Mat Martineau18a48e72012-05-17 20:53:34 -07002263 /* Control header is populated later */
2264 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
2265 put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
2266 else
2267 put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002268
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002269 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002270 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002271
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002272 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002273 if (unlikely(err < 0)) {
2274 kfree_skb(skb);
2275 return ERR_PTR(err);
2276 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03002277
Johan Hedberga4368ff2015-03-30 23:21:01 +03002278 bt_cb(skb)->l2cap.fcs = chan->fcs;
2279 bt_cb(skb)->l2cap.retries = 0;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002280 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
Mat Martineau94122bb2012-05-02 09:42:02 -07002283static int l2cap_segment_sdu(struct l2cap_chan *chan,
2284 struct sk_buff_head *seg_queue,
2285 struct msghdr *msg, size_t len)
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002286{
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002287 struct sk_buff *skb;
Mat Martineau94122bb2012-05-02 09:42:02 -07002288 u16 sdu_len;
2289 size_t pdu_len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002290 u8 sar;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002291
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002292 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002293
Mat Martineau94122bb2012-05-02 09:42:02 -07002294 /* It is critical that ERTM PDUs fit in a single HCI fragment,
2295 * so fragmented skbs are not used. The HCI layer's handling
2296 * of fragmented skbs is not compatible with ERTM's queueing.
2297 */
2298
2299 /* PDU size is derived from the HCI MTU */
2300 pdu_len = chan->conn->mtu;
2301
Mat Martineaua5495742012-10-23 15:24:21 -07002302 /* Constrain PDU size for BR/EDR connections */
2303 if (!chan->hs_hcon)
2304 pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
Mat Martineau94122bb2012-05-02 09:42:02 -07002305
2306 /* Adjust for largest possible L2CAP overhead. */
Gustavo Padovan35d401d2012-05-25 18:57:05 -03002307 if (chan->fcs)
2308 pdu_len -= L2CAP_FCS_SIZE;
2309
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002310 pdu_len -= __ertm_hdr_size(chan);
Mat Martineau94122bb2012-05-02 09:42:02 -07002311
2312 /* Remote device may have requested smaller PDUs */
2313 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2314
2315 if (len <= pdu_len) {
2316 sar = L2CAP_SAR_UNSEGMENTED;
2317 sdu_len = 0;
2318 pdu_len = len;
2319 } else {
2320 sar = L2CAP_SAR_START;
2321 sdu_len = len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002322 }
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002323
2324 while (len > 0) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002325 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002326
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002327 if (IS_ERR(skb)) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002328 __skb_queue_purge(seg_queue);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002329 return PTR_ERR(skb);
2330 }
2331
Johan Hedberga4368ff2015-03-30 23:21:01 +03002332 bt_cb(skb)->l2cap.sar = sar;
Mat Martineau94122bb2012-05-02 09:42:02 -07002333 __skb_queue_tail(seg_queue, skb);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002334
Mat Martineau94122bb2012-05-02 09:42:02 -07002335 len -= pdu_len;
Lukasz Rymanowski069cb272014-08-13 16:01:41 +02002336 if (sdu_len)
Mat Martineau94122bb2012-05-02 09:42:02 -07002337 sdu_len = 0;
Mat Martineau94122bb2012-05-02 09:42:02 -07002338
2339 if (len <= pdu_len) {
2340 sar = L2CAP_SAR_END;
2341 pdu_len = len;
2342 } else {
2343 sar = L2CAP_SAR_CONTINUE;
2344 }
2345 }
2346
Gustavo Padovanf0f62792012-05-29 13:29:17 -03002347 return 0;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002348}
2349
Johan Hedberg177f8f22013-05-31 17:54:51 +03002350static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan,
2351 struct msghdr *msg,
2352 size_t len, u16 sdulen)
2353{
2354 struct l2cap_conn *conn = chan->conn;
2355 struct sk_buff *skb;
2356 int err, count, hlen;
2357 struct l2cap_hdr *lh;
2358
2359 BT_DBG("chan %p len %zu", chan, len);
2360
2361 if (!conn)
2362 return ERR_PTR(-ENOTCONN);
2363
2364 hlen = L2CAP_HDR_SIZE;
2365
2366 if (sdulen)
2367 hlen += L2CAP_SDULEN_SIZE;
2368
2369 count = min_t(unsigned int, (conn->mtu - hlen), len);
2370
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002371 skb = chan->ops->alloc_skb(chan, hlen, count,
Johan Hedberg177f8f22013-05-31 17:54:51 +03002372 msg->msg_flags & MSG_DONTWAIT);
2373 if (IS_ERR(skb))
2374 return skb;
2375
2376 /* Create L2CAP header */
Johannes Berg4df864c2017-06-16 14:29:21 +02002377 lh = skb_put(skb, L2CAP_HDR_SIZE);
Johan Hedberg177f8f22013-05-31 17:54:51 +03002378 lh->cid = cpu_to_le16(chan->dcid);
2379 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
2380
2381 if (sdulen)
2382 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
2383
2384 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2385 if (unlikely(err < 0)) {
2386 kfree_skb(skb);
2387 return ERR_PTR(err);
2388 }
2389
2390 return skb;
2391}
2392
2393static int l2cap_segment_le_sdu(struct l2cap_chan *chan,
2394 struct sk_buff_head *seg_queue,
2395 struct msghdr *msg, size_t len)
2396{
2397 struct sk_buff *skb;
2398 size_t pdu_len;
2399 u16 sdu_len;
2400
2401 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
2402
Johan Hedberg177f8f22013-05-31 17:54:51 +03002403 sdu_len = len;
Johan Hedberg72c6fb92014-08-15 21:06:51 +03002404 pdu_len = chan->remote_mps - L2CAP_SDULEN_SIZE;
Johan Hedberg177f8f22013-05-31 17:54:51 +03002405
2406 while (len > 0) {
2407 if (len <= pdu_len)
2408 pdu_len = len;
2409
2410 skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len);
2411 if (IS_ERR(skb)) {
2412 __skb_queue_purge(seg_queue);
2413 return PTR_ERR(skb);
2414 }
2415
2416 __skb_queue_tail(seg_queue, skb);
2417
2418 len -= pdu_len;
2419
2420 if (sdu_len) {
2421 sdu_len = 0;
2422 pdu_len += L2CAP_SDULEN_SIZE;
2423 }
2424 }
2425
2426 return 0;
2427}
2428
Luiz Augusto von Dentz8a505b72017-04-11 22:21:01 +03002429static void l2cap_le_flowctl_send(struct l2cap_chan *chan)
2430{
2431 int sent = 0;
2432
2433 BT_DBG("chan %p", chan);
2434
2435 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
2436 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
2437 chan->tx_credits--;
2438 sent++;
2439 }
2440
2441 BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits,
2442 skb_queue_len(&chan->tx_q));
2443}
2444
Marcel Holtmann8d463212014-06-05 15:22:51 +02002445int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002446{
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002447 struct sk_buff *skb;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002448 int err;
Mat Martineau94122bb2012-05-02 09:42:02 -07002449 struct sk_buff_head seg_queue;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002450
Seung-Woo Kim31e8ce82013-11-05 18:46:33 +09002451 if (!chan->conn)
2452 return -ENOTCONN;
2453
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002454 /* Connectionless channel */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002455 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
Marcel Holtmann8d463212014-06-05 15:22:51 +02002456 skb = l2cap_create_connless_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002457 if (IS_ERR(skb))
2458 return PTR_ERR(skb);
2459
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002460 /* Channel lock is released before requesting new skb and then
2461 * reacquired thus we need to recheck channel state.
2462 */
2463 if (chan->state != BT_CONNECTED) {
2464 kfree_skb(skb);
2465 return -ENOTCONN;
2466 }
2467
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002468 l2cap_do_send(chan, skb);
2469 return len;
2470 }
2471
2472 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03002473 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +03002474 /* Check outgoing MTU */
2475 if (len > chan->omtu)
2476 return -EMSGSIZE;
2477
Johan Hedberg177f8f22013-05-31 17:54:51 +03002478 __skb_queue_head_init(&seg_queue);
2479
2480 err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len);
2481
2482 if (chan->state != BT_CONNECTED) {
2483 __skb_queue_purge(&seg_queue);
2484 err = -ENOTCONN;
2485 }
2486
2487 if (err)
2488 return err;
2489
2490 skb_queue_splice_tail_init(&seg_queue, &chan->tx_q);
2491
Luiz Augusto von Dentz8a505b72017-04-11 22:21:01 +03002492 l2cap_le_flowctl_send(chan);
Johan Hedberg177f8f22013-05-31 17:54:51 +03002493
2494 if (!chan->tx_credits)
2495 chan->ops->suspend(chan);
2496
2497 err = len;
2498
2499 break;
2500
Johan Hedbergfad5fc82013-12-05 09:45:01 +02002501 case L2CAP_MODE_BASIC:
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002502 /* Check outgoing MTU */
2503 if (len > chan->omtu)
2504 return -EMSGSIZE;
2505
2506 /* Create a basic PDU */
Marcel Holtmann8d463212014-06-05 15:22:51 +02002507 skb = l2cap_create_basic_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002508 if (IS_ERR(skb))
2509 return PTR_ERR(skb);
2510
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002511 /* Channel lock is released before requesting new skb and then
2512 * reacquired thus we need to recheck channel state.
2513 */
2514 if (chan->state != BT_CONNECTED) {
2515 kfree_skb(skb);
2516 return -ENOTCONN;
2517 }
2518
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002519 l2cap_do_send(chan, skb);
2520 err = len;
2521 break;
2522
2523 case L2CAP_MODE_ERTM:
2524 case L2CAP_MODE_STREAMING:
Mat Martineau94122bb2012-05-02 09:42:02 -07002525 /* Check outgoing MTU */
2526 if (len > chan->omtu) {
2527 err = -EMSGSIZE;
2528 break;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002529 }
2530
Mat Martineau94122bb2012-05-02 09:42:02 -07002531 __skb_queue_head_init(&seg_queue);
2532
2533 /* Do segmentation before calling in to the state machine,
2534 * since it's possible to block while waiting for memory
2535 * allocation.
2536 */
2537 err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
2538
2539 /* The channel could have been closed while segmenting,
2540 * check that it is still connected.
2541 */
2542 if (chan->state != BT_CONNECTED) {
2543 __skb_queue_purge(&seg_queue);
2544 err = -ENOTCONN;
2545 }
2546
2547 if (err)
2548 break;
2549
Mat Martineau37339372012-05-17 20:53:33 -07002550 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo Padovand6603662012-05-21 13:58:22 -03002551 l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST);
Mat Martineau37339372012-05-17 20:53:33 -07002552 else
Gustavo Padovand6603662012-05-21 13:58:22 -03002553 l2cap_streaming_send(chan, &seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002554
Gustavo Padovand6603662012-05-21 13:58:22 -03002555 err = len;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002556
Mat Martineau94122bb2012-05-02 09:42:02 -07002557 /* If the skbs were not queued for sending, they'll still be in
2558 * seg_queue and need to be purged.
2559 */
2560 __skb_queue_purge(&seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002561 break;
2562
2563 default:
2564 BT_DBG("bad state %1.1x", chan->mode);
2565 err = -EBADFD;
2566 }
2567
2568 return err;
2569}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03002570EXPORT_SYMBOL_GPL(l2cap_chan_send);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002571
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002572static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq)
2573{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002574 struct l2cap_ctrl control;
2575 u16 seq;
2576
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002577 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002578
2579 memset(&control, 0, sizeof(control));
2580 control.sframe = 1;
2581 control.super = L2CAP_SUPER_SREJ;
2582
2583 for (seq = chan->expected_tx_seq; seq != txseq;
2584 seq = __next_seq(chan, seq)) {
2585 if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) {
2586 control.reqseq = seq;
2587 l2cap_send_sframe(chan, &control);
2588 l2cap_seq_list_append(&chan->srej_list, seq);
2589 }
2590 }
2591
2592 chan->expected_tx_seq = __next_seq(chan, txseq);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002593}
2594
2595static void l2cap_send_srej_tail(struct l2cap_chan *chan)
2596{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002597 struct l2cap_ctrl control;
2598
2599 BT_DBG("chan %p", chan);
2600
2601 if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
2602 return;
2603
2604 memset(&control, 0, sizeof(control));
2605 control.sframe = 1;
2606 control.super = L2CAP_SUPER_SREJ;
2607 control.reqseq = chan->srej_list.tail;
2608 l2cap_send_sframe(chan, &control);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002609}
2610
2611static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq)
2612{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002613 struct l2cap_ctrl control;
2614 u16 initial_head;
2615 u16 seq;
2616
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002617 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002618
2619 memset(&control, 0, sizeof(control));
2620 control.sframe = 1;
2621 control.super = L2CAP_SUPER_SREJ;
2622
2623 /* Capture initial list head to allow only one pass through the list. */
2624 initial_head = chan->srej_list.head;
2625
2626 do {
2627 seq = l2cap_seq_list_pop(&chan->srej_list);
2628 if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR)
2629 break;
2630
2631 control.reqseq = seq;
2632 l2cap_send_sframe(chan, &control);
2633 l2cap_seq_list_append(&chan->srej_list, seq);
2634 } while (chan->srej_list.head != initial_head);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002635}
2636
Mat Martineau608bcc62012-05-17 20:53:32 -07002637static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq)
2638{
2639 struct sk_buff *acked_skb;
2640 u16 ackseq;
2641
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002642 BT_DBG("chan %p, reqseq %u", chan, reqseq);
Mat Martineau608bcc62012-05-17 20:53:32 -07002643
2644 if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq)
2645 return;
2646
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002647 BT_DBG("expected_ack_seq %u, unacked_frames %u",
Mat Martineau608bcc62012-05-17 20:53:32 -07002648 chan->expected_ack_seq, chan->unacked_frames);
2649
2650 for (ackseq = chan->expected_ack_seq; ackseq != reqseq;
2651 ackseq = __next_seq(chan, ackseq)) {
2652
2653 acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq);
2654 if (acked_skb) {
2655 skb_unlink(acked_skb, &chan->tx_q);
2656 kfree_skb(acked_skb);
2657 chan->unacked_frames--;
2658 }
2659 }
2660
2661 chan->expected_ack_seq = reqseq;
2662
2663 if (chan->unacked_frames == 0)
2664 __clear_retrans_timer(chan);
2665
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002666 BT_DBG("unacked_frames %u", chan->unacked_frames);
Mat Martineau608bcc62012-05-17 20:53:32 -07002667}
2668
2669static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan)
2670{
2671 BT_DBG("chan %p", chan);
2672
2673 chan->expected_tx_seq = chan->buffer_seq;
2674 l2cap_seq_list_clear(&chan->srej_list);
2675 skb_queue_purge(&chan->srej_q);
2676 chan->rx_state = L2CAP_RX_STATE_RECV;
2677}
2678
Gustavo Padovand6603662012-05-21 13:58:22 -03002679static void l2cap_tx_state_xmit(struct l2cap_chan *chan,
2680 struct l2cap_ctrl *control,
2681 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002682{
Mat Martineau608bcc62012-05-17 20:53:32 -07002683 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2684 event);
2685
2686 switch (event) {
2687 case L2CAP_EV_DATA_REQUEST:
2688 if (chan->tx_send_head == NULL)
2689 chan->tx_send_head = skb_peek(skbs);
2690
2691 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2692 l2cap_ertm_send(chan);
2693 break;
2694 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2695 BT_DBG("Enter LOCAL_BUSY");
2696 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2697
2698 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2699 /* The SREJ_SENT state must be aborted if we are to
2700 * enter the LOCAL_BUSY state.
2701 */
2702 l2cap_abort_rx_srej_sent(chan);
2703 }
2704
2705 l2cap_send_ack(chan);
2706
2707 break;
2708 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2709 BT_DBG("Exit LOCAL_BUSY");
2710 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2711
2712 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2713 struct l2cap_ctrl local_control;
2714
2715 memset(&local_control, 0, sizeof(local_control));
2716 local_control.sframe = 1;
2717 local_control.super = L2CAP_SUPER_RR;
2718 local_control.poll = 1;
2719 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002720 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002721
2722 chan->retry_count = 1;
2723 __set_monitor_timer(chan);
2724 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2725 }
2726 break;
2727 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2728 l2cap_process_reqseq(chan, control->reqseq);
2729 break;
2730 case L2CAP_EV_EXPLICIT_POLL:
2731 l2cap_send_rr_or_rnr(chan, 1);
2732 chan->retry_count = 1;
2733 __set_monitor_timer(chan);
2734 __clear_ack_timer(chan);
2735 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2736 break;
2737 case L2CAP_EV_RETRANS_TO:
2738 l2cap_send_rr_or_rnr(chan, 1);
2739 chan->retry_count = 1;
2740 __set_monitor_timer(chan);
2741 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2742 break;
2743 case L2CAP_EV_RECV_FBIT:
2744 /* Nothing to process */
2745 break;
2746 default:
2747 break;
2748 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002749}
2750
Gustavo Padovand6603662012-05-21 13:58:22 -03002751static void l2cap_tx_state_wait_f(struct l2cap_chan *chan,
2752 struct l2cap_ctrl *control,
2753 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002754{
Mat Martineau608bcc62012-05-17 20:53:32 -07002755 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2756 event);
2757
2758 switch (event) {
2759 case L2CAP_EV_DATA_REQUEST:
2760 if (chan->tx_send_head == NULL)
2761 chan->tx_send_head = skb_peek(skbs);
2762 /* Queue data, but don't send. */
2763 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2764 break;
2765 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2766 BT_DBG("Enter LOCAL_BUSY");
2767 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2768
2769 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2770 /* The SREJ_SENT state must be aborted if we are to
2771 * enter the LOCAL_BUSY state.
2772 */
2773 l2cap_abort_rx_srej_sent(chan);
2774 }
2775
2776 l2cap_send_ack(chan);
2777
2778 break;
2779 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2780 BT_DBG("Exit LOCAL_BUSY");
2781 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2782
2783 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2784 struct l2cap_ctrl local_control;
2785 memset(&local_control, 0, sizeof(local_control));
2786 local_control.sframe = 1;
2787 local_control.super = L2CAP_SUPER_RR;
2788 local_control.poll = 1;
2789 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002790 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002791
2792 chan->retry_count = 1;
2793 __set_monitor_timer(chan);
2794 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2795 }
2796 break;
2797 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2798 l2cap_process_reqseq(chan, control->reqseq);
2799
2800 /* Fall through */
2801
2802 case L2CAP_EV_RECV_FBIT:
2803 if (control && control->final) {
2804 __clear_monitor_timer(chan);
2805 if (chan->unacked_frames > 0)
2806 __set_retrans_timer(chan);
2807 chan->retry_count = 0;
2808 chan->tx_state = L2CAP_TX_STATE_XMIT;
2809 BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state);
2810 }
2811 break;
2812 case L2CAP_EV_EXPLICIT_POLL:
2813 /* Ignore */
2814 break;
2815 case L2CAP_EV_MONITOR_TO:
2816 if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) {
2817 l2cap_send_rr_or_rnr(chan, 1);
2818 __set_monitor_timer(chan);
2819 chan->retry_count++;
2820 } else {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02002821 l2cap_send_disconn_req(chan, ECONNABORTED);
Mat Martineau608bcc62012-05-17 20:53:32 -07002822 }
2823 break;
2824 default:
2825 break;
2826 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002827}
2828
Gustavo Padovand6603662012-05-21 13:58:22 -03002829static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
2830 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002831{
Mat Martineau608bcc62012-05-17 20:53:32 -07002832 BT_DBG("chan %p, control %p, skbs %p, event %d, state %d",
2833 chan, control, skbs, event, chan->tx_state);
2834
2835 switch (chan->tx_state) {
2836 case L2CAP_TX_STATE_XMIT:
Gustavo Padovand6603662012-05-21 13:58:22 -03002837 l2cap_tx_state_xmit(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002838 break;
2839 case L2CAP_TX_STATE_WAIT_F:
Gustavo Padovand6603662012-05-21 13:58:22 -03002840 l2cap_tx_state_wait_f(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002841 break;
2842 default:
2843 /* Ignore event */
2844 break;
2845 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002846}
2847
Mat Martineau4b51dae92012-05-17 20:53:37 -07002848static void l2cap_pass_to_tx(struct l2cap_chan *chan,
2849 struct l2cap_ctrl *control)
2850{
2851 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002852 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT);
Mat Martineau4b51dae92012-05-17 20:53:37 -07002853}
2854
Mat Martineauf80842a2012-05-17 20:53:46 -07002855static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan,
2856 struct l2cap_ctrl *control)
2857{
2858 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002859 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT);
Mat Martineauf80842a2012-05-17 20:53:46 -07002860}
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862/* Copy frame to all raw sockets on that connection */
2863static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2864{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct sk_buff *nskb;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03002866 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 BT_DBG("conn %p", conn);
2869
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002870 mutex_lock(&conn->chan_lock);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002871
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002872 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002873 if (chan->chan_type != L2CAP_CHAN_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 continue;
2875
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002876 /* Don't send frame to the channel it came from */
Johan Hedberga4368ff2015-03-30 23:21:01 +03002877 if (bt_cb(skb)->l2cap.chan == chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 continue;
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002879
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002880 nskb = skb_clone(skb, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03002881 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 continue;
Gustavo Padovan80b98022012-05-27 22:27:51 -03002883 if (chan->ops->recv(chan, nskb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 kfree_skb(nskb);
2885 }
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002886
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002887 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
2890/* ---- L2CAP signalling commands ---- */
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002891static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code,
2892 u8 ident, u16 dlen, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
2894 struct sk_buff *skb, **frag;
2895 struct l2cap_cmd_hdr *cmd;
2896 struct l2cap_hdr *lh;
2897 int len, count;
2898
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002899 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u",
2900 conn, code, ident, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Anderson Lizardo300b9622013-06-02 16:30:40 -04002902 if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
2903 return NULL;
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
2906 count = min_t(unsigned int, conn->mtu, len);
2907
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002908 skb = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (!skb)
2910 return NULL;
2911
Johannes Berg4df864c2017-06-16 14:29:21 +02002912 lh = skb_put(skb, L2CAP_HDR_SIZE);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002913 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002914
2915 if (conn->hcon->type == LE_LINK)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002916 lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002917 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002918 lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Johannes Berg4df864c2017-06-16 14:29:21 +02002920 cmd = skb_put(skb, L2CAP_CMD_HDR_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 cmd->code = code;
2922 cmd->ident = ident;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002923 cmd->len = cpu_to_le16(dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 if (dlen) {
2926 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
Johannes Berg59ae1d12017-06-16 14:29:20 +02002927 skb_put_data(skb, data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 data += count;
2929 }
2930
2931 len -= skb->len;
2932
2933 /* Continuation fragments (no L2CAP header) */
2934 frag = &skb_shinfo(skb)->frag_list;
2935 while (len) {
2936 count = min_t(unsigned int, conn->mtu, len);
2937
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002938 *frag = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 if (!*frag)
2940 goto fail;
2941
Johannes Berg59ae1d12017-06-16 14:29:20 +02002942 skb_put_data(*frag, data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 len -= count;
2945 data += count;
2946
2947 frag = &(*frag)->next;
2948 }
2949
2950 return skb;
2951
2952fail:
2953 kfree_skb(skb);
2954 return NULL;
2955}
2956
Gustavo Padovan2d792812012-10-06 10:07:01 +01002957static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
2958 unsigned long *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
2960 struct l2cap_conf_opt *opt = *ptr;
2961 int len;
2962
2963 len = L2CAP_CONF_OPT_SIZE + opt->len;
2964 *ptr += len;
2965
2966 *type = opt->type;
2967 *olen = opt->len;
2968
2969 switch (opt->len) {
2970 case 1:
2971 *val = *((u8 *) opt->val);
2972 break;
2973
2974 case 2:
steven miaobfaaeb32010-10-16 18:29:47 -04002975 *val = get_unaligned_le16(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 break;
2977
2978 case 4:
steven miaobfaaeb32010-10-16 18:29:47 -04002979 *val = get_unaligned_le32(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 break;
2981
2982 default:
2983 *val = (unsigned long) opt->val;
2984 break;
2985 }
2986
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002987 BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return len;
2989}
2990
Ben Serie860d2c2017-09-09 23:15:59 +02002991static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
2993 struct l2cap_conf_opt *opt = *ptr;
2994
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002995 BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Ben Serie860d2c2017-09-09 23:15:59 +02002997 if (size < L2CAP_CONF_OPT_SIZE + len)
2998 return;
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 opt->type = type;
3001 opt->len = len;
3002
3003 switch (len) {
3004 case 1:
3005 *((u8 *) opt->val) = val;
3006 break;
3007
3008 case 2:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02003009 put_unaligned_le16(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 break;
3011
3012 case 4:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02003013 put_unaligned_le32(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 break;
3015
3016 default:
3017 memcpy(opt->val, (void *) val, len);
3018 break;
3019 }
3020
3021 *ptr += L2CAP_CONF_OPT_SIZE + len;
3022}
3023
Ben Serie860d2c2017-09-09 23:15:59 +02003024static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan, size_t size)
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003025{
3026 struct l2cap_conf_efs efs;
3027
Szymon Janc1ec918c2011-11-16 09:32:21 +01003028 switch (chan->mode) {
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003029 case L2CAP_MODE_ERTM:
3030 efs.id = chan->local_id;
3031 efs.stype = chan->local_stype;
3032 efs.msdu = cpu_to_le16(chan->local_msdu);
3033 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003034 efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
3035 efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003036 break;
3037
3038 case L2CAP_MODE_STREAMING:
3039 efs.id = 1;
3040 efs.stype = L2CAP_SERV_BESTEFFORT;
3041 efs.msdu = cpu_to_le16(chan->local_msdu);
3042 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
3043 efs.acc_lat = 0;
3044 efs.flush_to = 0;
3045 break;
3046
3047 default:
3048 return;
3049 }
3050
3051 l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
Ben Serie860d2c2017-09-09 23:15:59 +02003052 (unsigned long) &efs, size);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003053}
3054
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003055static void l2cap_ack_timeout(struct work_struct *work)
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003056{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003057 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau03625202012-05-17 20:53:51 -07003058 ack_timer.work);
3059 u16 frames_to_ack;
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003060
Gustavo F. Padovan2fb9b3d2011-12-22 16:56:05 -02003061 BT_DBG("chan %p", chan);
3062
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003063 l2cap_chan_lock(chan);
3064
Mat Martineau03625202012-05-17 20:53:51 -07003065 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
3066 chan->last_acked_seq);
3067
3068 if (frames_to_ack)
3069 l2cap_send_rr_or_rnr(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003070
3071 l2cap_chan_unlock(chan);
Szymon Janc09bfb2e2012-01-11 10:59:49 +01003072 l2cap_chan_put(chan);
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003073}
3074
Andrei Emeltchenko466f8002012-05-29 13:59:01 +03003075int l2cap_ertm_init(struct l2cap_chan *chan)
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003076{
Mat Martineau3c588192012-04-11 10:48:42 -07003077 int err;
3078
Mat Martineau105bdf92012-04-27 16:50:48 -07003079 chan->next_tx_seq = 0;
3080 chan->expected_tx_seq = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003081 chan->expected_ack_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003082 chan->unacked_frames = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003083 chan->buffer_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003084 chan->frames_sent = 0;
Mat Martineau105bdf92012-04-27 16:50:48 -07003085 chan->last_acked_seq = 0;
3086 chan->sdu = NULL;
3087 chan->sdu_last_frag = NULL;
3088 chan->sdu_len = 0;
3089
Mat Martineaud34c34f2012-05-14 14:49:27 -07003090 skb_queue_head_init(&chan->tx_q);
3091
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003092 chan->local_amp_id = AMP_ID_BREDR;
3093 chan->move_id = AMP_ID_BREDR;
Mat Martineau08333282012-10-23 15:24:06 -07003094 chan->move_state = L2CAP_MOVE_STABLE;
3095 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3096
Mat Martineau105bdf92012-04-27 16:50:48 -07003097 if (chan->mode != L2CAP_MODE_ERTM)
3098 return 0;
3099
3100 chan->rx_state = L2CAP_RX_STATE_RECV;
3101 chan->tx_state = L2CAP_TX_STATE_XMIT;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003102
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003103 INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
3104 INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
3105 INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003106
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -03003107 skb_queue_head_init(&chan->srej_q);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03003108
Mat Martineau3c588192012-04-11 10:48:42 -07003109 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
3110 if (err < 0)
3111 return err;
3112
Mat Martineau9dc9aff2012-05-17 16:20:14 -07003113 err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win);
3114 if (err < 0)
3115 l2cap_seq_list_free(&chan->srej_list);
3116
3117 return err;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003118}
3119
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003120static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3121{
3122 switch (mode) {
3123 case L2CAP_MODE_STREAMING:
3124 case L2CAP_MODE_ERTM:
3125 if (l2cap_mode_supported(mode, remote_feat_mask))
3126 return mode;
3127 /* fall through */
3128 default:
3129 return L2CAP_MODE_BASIC;
3130 }
3131}
3132
Marcel Holtmann848566b2013-10-01 22:59:22 -07003133static inline bool __l2cap_ews_supported(struct l2cap_conn *conn)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003134{
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02003135 return ((conn->local_fixed_chan & L2CAP_FC_A2MP) &&
3136 (conn->feat_mask & L2CAP_FEAT_EXT_WINDOW));
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003137}
3138
Marcel Holtmann848566b2013-10-01 22:59:22 -07003139static inline bool __l2cap_efs_supported(struct l2cap_conn *conn)
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003140{
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02003141 return ((conn->local_fixed_chan & L2CAP_FC_A2MP) &&
3142 (conn->feat_mask & L2CAP_FEAT_EXT_FLOW));
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003143}
3144
Mat Martineau36c86c82012-10-23 15:24:20 -07003145static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3146 struct l2cap_conf_rfc *rfc)
3147{
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003148 if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) {
Mat Martineau36c86c82012-10-23 15:24:20 -07003149 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3150
3151 /* Class 1 devices have must have ERTM timeouts
3152 * exceeding the Link Supervision Timeout. The
3153 * default Link Supervision Timeout for AMP
3154 * controllers is 10 seconds.
3155 *
3156 * Class 1 devices use 0xffffffff for their
3157 * best-effort flush timeout, so the clamping logic
3158 * will result in a timeout that meets the above
3159 * requirement. ERTM timeouts are 16-bit values, so
3160 * the maximum timeout is 65.535 seconds.
3161 */
3162
3163 /* Convert timeout to milliseconds and round */
3164 ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000);
3165
3166 /* This is the recommended formula for class 2 devices
3167 * that start ERTM timers when packets are sent to the
3168 * controller.
3169 */
3170 ertm_to = 3 * ertm_to + 500;
3171
3172 if (ertm_to > 0xffff)
3173 ertm_to = 0xffff;
3174
3175 rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
3176 rfc->monitor_timeout = rfc->retrans_timeout;
3177 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003178 rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
3179 rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
Mat Martineau36c86c82012-10-23 15:24:20 -07003180 }
3181}
3182
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003183static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3184{
3185 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
Marcel Holtmann848566b2013-10-01 22:59:22 -07003186 __l2cap_ews_supported(chan->conn)) {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003187 /* use extended control field */
3188 set_bit(FLAG_EXT_CTRL, &chan->flags);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003189 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
3190 } else {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003191 chan->tx_win = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003192 L2CAP_DEFAULT_TX_WINDOW);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003193 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
3194 }
Mat Martineauc20f8e32012-07-10 05:47:07 -07003195 chan->ack_win = chan->tx_win;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003196}
3197
Ben Serie860d2c2017-09-09 23:15:59 +02003198static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data, size_t data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 struct l2cap_conf_req *req = data;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003201 struct l2cap_conf_rfc rfc = { .mode = chan->mode };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 void *ptr = req->data;
Ben Serie860d2c2017-09-09 23:15:59 +02003203 void *endptr = data + data_size;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003204 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03003206 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003208 if (chan->num_conf_req || chan->num_conf_rsp)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003209 goto done;
3210
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003211 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003212 case L2CAP_MODE_STREAMING:
3213 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003214 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003215 break;
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003216
Marcel Holtmann848566b2013-10-01 22:59:22 -07003217 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003218 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3219
Gustavo F. Padovan2ba13ed2010-06-09 16:39:05 -03003220 /* fall through */
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003221 default:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003222 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003223 break;
3224 }
3225
3226done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003227 if (chan->imtu != L2CAP_DEFAULT_MTU)
Ben Serie860d2c2017-09-09 23:15:59 +02003228 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu, endptr - ptr);
Gustavo F. Padovan7990681c2011-01-24 16:01:43 -02003229
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003230 switch (chan->mode) {
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003231 case L2CAP_MODE_BASIC:
Marcel Holtmann6fea7ad2014-07-09 11:53:35 +02003232 if (disable_ertm)
3233 break;
3234
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003235 if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003236 !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003237 break;
3238
Gustavo F. Padovan62547752010-06-08 20:05:31 -03003239 rfc.mode = L2CAP_MODE_BASIC;
3240 rfc.txwin_size = 0;
3241 rfc.max_transmit = 0;
3242 rfc.retrans_timeout = 0;
3243 rfc.monitor_timeout = 0;
3244 rfc.max_pdu_size = 0;
3245
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003246 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Ben Serie860d2c2017-09-09 23:15:59 +02003247 (unsigned long) &rfc, endptr - ptr);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003248 break;
3249
3250 case L2CAP_MODE_ERTM:
3251 rfc.mode = L2CAP_MODE_ERTM;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003252 rfc.max_transmit = chan->max_tx;
Mat Martineau36c86c82012-10-23 15:24:20 -07003253
3254 __l2cap_set_ertm_timeouts(chan, &rfc);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003255
3256 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003257 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3258 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003259 rfc.max_pdu_size = cpu_to_le16(size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003260
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003261 l2cap_txwin_setup(chan);
3262
3263 rfc.txwin_size = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003264 L2CAP_DEFAULT_TX_WINDOW);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003265
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003266 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Ben Serie860d2c2017-09-09 23:15:59 +02003267 (unsigned long) &rfc, endptr - ptr);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003268
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003269 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
Ben Serie860d2c2017-09-09 23:15:59 +02003270 l2cap_add_opt_efs(&ptr, chan, endptr - ptr);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003271
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003272 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3273 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Ben Serie860d2c2017-09-09 23:15:59 +02003274 chan->tx_win, endptr - ptr);
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003275
3276 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3277 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003278 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003279 chan->fcs = L2CAP_FCS_NONE;
3280 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
Ben Serie860d2c2017-09-09 23:15:59 +02003281 chan->fcs, endptr - ptr);
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003282 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003283 break;
3284
3285 case L2CAP_MODE_STREAMING:
Mat Martineau273759e2012-05-17 20:53:53 -07003286 l2cap_txwin_setup(chan);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003287 rfc.mode = L2CAP_MODE_STREAMING;
3288 rfc.txwin_size = 0;
3289 rfc.max_transmit = 0;
3290 rfc.retrans_timeout = 0;
3291 rfc.monitor_timeout = 0;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003292
3293 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003294 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3295 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003296 rfc.max_pdu_size = cpu_to_le16(size);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003297
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003298 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Ben Serie860d2c2017-09-09 23:15:59 +02003299 (unsigned long) &rfc, endptr - ptr);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003300
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003301 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
Ben Serie860d2c2017-09-09 23:15:59 +02003302 l2cap_add_opt_efs(&ptr, chan, endptr - ptr);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003303
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003304 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3305 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003306 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003307 chan->fcs = L2CAP_FCS_NONE;
3308 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
Ben Serie860d2c2017-09-09 23:15:59 +02003309 chan->fcs, endptr - ptr);
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003310 }
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003311 break;
3312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003314 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003315 req->flags = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 return ptr - data;
3318}
3319
Ben Serie860d2c2017-09-09 23:15:59 +02003320static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data, size_t data_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003322 struct l2cap_conf_rsp *rsp = data;
3323 void *ptr = rsp->data;
Ben Serie860d2c2017-09-09 23:15:59 +02003324 void *endptr = data + data_size;
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003325 void *req = chan->conf_req;
3326 int len = chan->conf_len;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003327 int type, hint, olen;
3328 unsigned long val;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003329 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003330 struct l2cap_conf_efs efs;
3331 u8 remote_efs = 0;
Marcel Holtmann861d6882007-10-20 13:37:06 +02003332 u16 mtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003333 u16 result = L2CAP_CONF_SUCCESS;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003334 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003336 BT_DBG("chan %p", chan);
Marcel Holtmann820ae1b2006-11-18 22:15:00 +01003337
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003338 while (len >= L2CAP_CONF_OPT_SIZE) {
3339 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
Gustavo F. Padovan589d2742009-04-20 01:31:07 -03003341 hint = type & L2CAP_CONF_HINT;
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07003342 type &= L2CAP_CONF_MASK;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003343
3344 switch (type) {
3345 case L2CAP_CONF_MTU:
Marcel Holtmann861d6882007-10-20 13:37:06 +02003346 mtu = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003347 break;
3348
3349 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003350 chan->flush_to = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003351 break;
3352
3353 case L2CAP_CONF_QOS:
3354 break;
3355
Marcel Holtmann6464f352007-10-20 13:39:51 +02003356 case L2CAP_CONF_RFC:
3357 if (olen == sizeof(rfc))
3358 memcpy(&rfc, (void *) val, olen);
3359 break;
3360
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003361 case L2CAP_CONF_FCS:
3362 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003363 set_bit(CONF_RECV_NO_FCS, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003364 break;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003365
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003366 case L2CAP_CONF_EFS:
Ben Seri06e7e772017-12-08 15:14:47 +01003367 if (olen == sizeof(efs)) {
3368 remote_efs = 1;
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003369 memcpy(&efs, (void *) val, olen);
Ben Seri06e7e772017-12-08 15:14:47 +01003370 }
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003371 break;
3372
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003373 case L2CAP_CONF_EWS:
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02003374 if (!(chan->conn->local_fixed_chan & L2CAP_FC_A2MP))
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003375 return -ECONNREFUSED;
3376
3377 set_bit(FLAG_EXT_CTRL, &chan->flags);
3378 set_bit(CONF_EWS_RECV, &chan->conf_state);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003379 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003380 chan->remote_tx_win = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003381 break;
3382
3383 default:
3384 if (hint)
3385 break;
3386
3387 result = L2CAP_CONF_UNKNOWN;
3388 *((u8 *) ptr++) = type;
3389 break;
3390 }
3391 }
3392
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003393 if (chan->num_conf_rsp || chan->num_conf_req > 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003394 goto done;
3395
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003396 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003397 case L2CAP_MODE_STREAMING:
3398 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003399 if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003400 chan->mode = l2cap_select_mode(rfc.mode,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003401 chan->conn->feat_mask);
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003402 break;
3403 }
3404
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003405 if (remote_efs) {
Marcel Holtmann848566b2013-10-01 22:59:22 -07003406 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003407 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3408 else
3409 return -ECONNREFUSED;
3410 }
3411
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003412 if (chan->mode != rfc.mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003413 return -ECONNREFUSED;
Gustavo F. Padovan742e5192010-06-08 19:09:48 -03003414
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003415 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003416 }
3417
3418done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003419 if (chan->mode != rfc.mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003420 result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003421 rfc.mode = chan->mode;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003422
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003423 if (chan->num_conf_rsp == 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003424 return -ECONNREFUSED;
3425
Gustavo Padovan2d792812012-10-06 10:07:01 +01003426 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Ben Serie860d2c2017-09-09 23:15:59 +02003427 (unsigned long) &rfc, endptr - ptr);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003428 }
3429
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003430 if (result == L2CAP_CONF_SUCCESS) {
3431 /* Configure output options and let the other side know
3432 * which ones we don't like. */
3433
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003434 if (mtu < L2CAP_DEFAULT_MIN_MTU)
3435 result = L2CAP_CONF_UNACCEPT;
3436 else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003437 chan->omtu = mtu;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003438 set_bit(CONF_MTU_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003439 }
Ben Serie860d2c2017-09-09 23:15:59 +02003440 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu, endptr - ptr);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003441
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003442 if (remote_efs) {
3443 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003444 efs.stype != L2CAP_SERV_NOTRAFIC &&
3445 efs.stype != chan->local_stype) {
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003446
3447 result = L2CAP_CONF_UNACCEPT;
3448
3449 if (chan->num_conf_req >= 1)
3450 return -ECONNREFUSED;
3451
3452 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003453 sizeof(efs),
Ben Serie860d2c2017-09-09 23:15:59 +02003454 (unsigned long) &efs, endptr - ptr);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003455 } else {
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003456 /* Send PENDING Conf Rsp */
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003457 result = L2CAP_CONF_PENDING;
3458 set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003459 }
3460 }
3461
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003462 switch (rfc.mode) {
3463 case L2CAP_MODE_BASIC:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003464 chan->fcs = L2CAP_FCS_NONE;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003465 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003466 break;
3467
3468 case L2CAP_MODE_ERTM:
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003469 if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
3470 chan->remote_tx_win = rfc.txwin_size;
3471 else
3472 rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
3473
Gustavo F. Padovan2c03a7a2011-03-25 20:15:28 -03003474 chan->remote_max_tx = rfc.max_transmit;
Mat Martineau86b1b262010-08-05 15:54:22 -07003475
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003476 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003477 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3478 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003479 rfc.max_pdu_size = cpu_to_le16(size);
3480 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003481
Mat Martineau36c86c82012-10-23 15:24:20 -07003482 __l2cap_set_ertm_timeouts(chan, &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003483
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003484 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003485
3486 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Ben Serie860d2c2017-09-09 23:15:59 +02003487 sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003488
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003489 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3490 chan->remote_id = efs.id;
3491 chan->remote_stype = efs.stype;
3492 chan->remote_msdu = le16_to_cpu(efs.msdu);
3493 chan->remote_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003494 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003495 chan->remote_acc_lat =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003496 le32_to_cpu(efs.acc_lat);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003497 chan->remote_sdu_itime =
3498 le32_to_cpu(efs.sdu_itime);
3499 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003500 sizeof(efs),
Ben Serie860d2c2017-09-09 23:15:59 +02003501 (unsigned long) &efs, endptr - ptr);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003502 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003503 break;
3504
3505 case L2CAP_MODE_STREAMING:
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003506 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003507 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3508 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003509 rfc.max_pdu_size = cpu_to_le16(size);
3510 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003511
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003512 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003513
Gustavo Padovan2d792812012-10-06 10:07:01 +01003514 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Ben Serie860d2c2017-09-09 23:15:59 +02003515 (unsigned long) &rfc, endptr - ptr);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003516
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003517 break;
3518
3519 default:
Marcel Holtmann6464f352007-10-20 13:39:51 +02003520 result = L2CAP_CONF_UNACCEPT;
3521
3522 memset(&rfc, 0, sizeof(rfc));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003523 rfc.mode = chan->mode;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003524 }
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003525
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003526 if (result == L2CAP_CONF_SUCCESS)
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003527 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003528 }
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003529 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003530 rsp->result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003531 rsp->flags = cpu_to_le16(0);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003532
3533 return ptr - data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Gustavo Padovan2d792812012-10-06 10:07:01 +01003536static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
Ben Serie860d2c2017-09-09 23:15:59 +02003537 void *data, size_t size, u16 *result)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003538{
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003539 struct l2cap_conf_req *req = data;
3540 void *ptr = req->data;
Ben Serie860d2c2017-09-09 23:15:59 +02003541 void *endptr = data + size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003542 int type, olen;
3543 unsigned long val;
Mat Martineau36e999a2011-12-08 17:23:21 -08003544 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003545 struct l2cap_conf_efs efs;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003546
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003547 BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003548
3549 while (len >= L2CAP_CONF_OPT_SIZE) {
3550 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3551
3552 switch (type) {
3553 case L2CAP_CONF_MTU:
3554 if (val < L2CAP_DEFAULT_MIN_MTU) {
3555 *result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003556 chan->imtu = L2CAP_DEFAULT_MIN_MTU;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003557 } else
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003558 chan->imtu = val;
Ben Serie860d2c2017-09-09 23:15:59 +02003559 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu, endptr - ptr);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003560 break;
3561
3562 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003563 chan->flush_to = val;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003564 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
Ben Serie860d2c2017-09-09 23:15:59 +02003565 2, chan->flush_to, endptr - ptr);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003566 break;
3567
3568 case L2CAP_CONF_RFC:
3569 if (olen == sizeof(rfc))
3570 memcpy(&rfc, (void *)val, olen);
3571
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003572 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003573 rfc.mode != chan->mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003574 return -ECONNREFUSED;
3575
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003576 chan->fcs = 0;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003577
3578 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Ben Serie860d2c2017-09-09 23:15:59 +02003579 sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003580 break;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003581
3582 case L2CAP_CONF_EWS:
Mat Martineauc20f8e32012-07-10 05:47:07 -07003583 chan->ack_win = min_t(u16, val, chan->ack_win);
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003584 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Ben Serie860d2c2017-09-09 23:15:59 +02003585 chan->tx_win, endptr - ptr);
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003586 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003587
3588 case L2CAP_CONF_EFS:
Ben Seri06e7e772017-12-08 15:14:47 +01003589 if (olen == sizeof(efs)) {
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003590 memcpy(&efs, (void *)val, olen);
3591
Ben Seri06e7e772017-12-08 15:14:47 +01003592 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
3593 efs.stype != L2CAP_SERV_NOTRAFIC &&
3594 efs.stype != chan->local_stype)
3595 return -ECONNREFUSED;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003596
Ben Seri06e7e772017-12-08 15:14:47 +01003597 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs),
3598 (unsigned long) &efs, endptr - ptr);
3599 }
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003600 break;
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003601
3602 case L2CAP_CONF_FCS:
3603 if (*result == L2CAP_CONF_PENDING)
3604 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003605 set_bit(CONF_RECV_NO_FCS,
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003606 &chan->conf_state);
3607 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003608 }
3609 }
3610
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003611 if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003612 return -ECONNREFUSED;
3613
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003614 chan->mode = rfc.mode;
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003615
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003616 if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003617 switch (rfc.mode) {
3618 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003619 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3620 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
3621 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003622 if (!test_bit(FLAG_EXT_CTRL, &chan->flags))
3623 chan->ack_win = min_t(u16, chan->ack_win,
3624 rfc.txwin_size);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003625
3626 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3627 chan->local_msdu = le16_to_cpu(efs.msdu);
3628 chan->local_sdu_itime =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003629 le32_to_cpu(efs.sdu_itime);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003630 chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
3631 chan->local_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003632 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003633 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003634 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003635
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003636 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003637 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003638 }
3639 }
3640
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003641 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003642 req->flags = cpu_to_le16(0);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003643
3644 return ptr - data;
3645}
3646
Gustavo Padovan2d792812012-10-06 10:07:01 +01003647static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
3648 u16 result, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
3650 struct l2cap_conf_rsp *rsp = data;
3651 void *ptr = rsp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003653 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003655 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003656 rsp->result = cpu_to_le16(result);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003657 rsp->flags = cpu_to_le16(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
3659 return ptr - data;
3660}
3661
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003662void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
3663{
3664 struct l2cap_le_conn_rsp rsp;
3665 struct l2cap_conn *conn = chan->conn;
3666
3667 BT_DBG("chan %p", chan);
3668
3669 rsp.dcid = cpu_to_le16(chan->scid);
3670 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02003671 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03003672 rsp.credits = cpu_to_le16(chan->rx_credits);
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05303673 rsp.result = cpu_to_le16(L2CAP_CR_LE_SUCCESS);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003674
3675 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
3676 &rsp);
3677}
3678
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003679void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003680{
3681 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003682 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003683 u8 buf[128];
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003684 u8 rsp_code;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003685
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003686 rsp.scid = cpu_to_le16(chan->dcid);
3687 rsp.dcid = cpu_to_le16(chan->scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003688 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
3689 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003690
3691 if (chan->hs_hcon)
3692 rsp_code = L2CAP_CREATE_CHAN_RSP;
3693 else
3694 rsp_code = L2CAP_CONN_RSP;
3695
3696 BT_DBG("chan %p rsp_code %u", chan, rsp_code);
3697
3698 l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003699
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003700 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003701 return;
3702
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003703 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Ben Serie860d2c2017-09-09 23:15:59 +02003704 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003705 chan->num_conf_req++;
3706}
3707
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003708static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003709{
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003710 int type, olen;
3711 unsigned long val;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003712 /* Use sane default values in case a misbehaving remote device
3713 * did not send an RFC or extended window size option.
3714 */
3715 u16 txwin_ext = chan->ack_win;
3716 struct l2cap_conf_rfc rfc = {
3717 .mode = chan->mode,
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003718 .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO),
3719 .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO),
Mat Martineauc20f8e32012-07-10 05:47:07 -07003720 .max_pdu_size = cpu_to_le16(chan->imtu),
3721 .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW),
3722 };
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003723
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003724 BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003725
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003726 if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003727 return;
3728
3729 while (len >= L2CAP_CONF_OPT_SIZE) {
3730 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3731
Mat Martineauc20f8e32012-07-10 05:47:07 -07003732 switch (type) {
3733 case L2CAP_CONF_RFC:
3734 if (olen == sizeof(rfc))
3735 memcpy(&rfc, (void *)val, olen);
Szymon Janc8f321f82012-06-08 11:33:33 +02003736 break;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003737 case L2CAP_CONF_EWS:
3738 txwin_ext = val;
3739 break;
3740 }
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003741 }
3742
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003743 switch (rfc.mode) {
3744 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003745 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3746 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003747 chan->mps = le16_to_cpu(rfc.max_pdu_size);
3748 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3749 chan->ack_win = min_t(u16, chan->ack_win, txwin_ext);
3750 else
3751 chan->ack_win = min_t(u16, chan->ack_win,
3752 rfc.txwin_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003753 break;
3754 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003755 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003756 }
3757}
3758
Gustavo Padovan2d792812012-10-06 10:07:01 +01003759static inline int l2cap_command_rej(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003760 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3761 u8 *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003762{
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003763 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003764
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003765 if (cmd_len < sizeof(*rej))
3766 return -EPROTO;
3767
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003768 if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003769 return 0;
3770
3771 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003772 cmd->ident == conn->info_ident) {
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02003773 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann984947d2009-02-06 23:35:19 +01003774
3775 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01003776 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01003777
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003778 l2cap_conn_start(conn);
3779 }
3780
3781 return 0;
3782}
3783
Mat Martineau17009152012-10-23 15:24:07 -07003784static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3785 struct l2cap_cmd_hdr *cmd,
3786 u8 *data, u8 rsp_code, u8 amp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
3789 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003790 struct l2cap_chan *chan = NULL, *pchan;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003791 int result, status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003794 __le16 psm = req->psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Andrei Emeltchenko097db762012-03-09 14:16:17 +02003796 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 /* Check if we have socket listening on psm */
Marcel Holtmann6f59b902013-10-13 05:24:01 -07003799 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03003800 &conn->hcon->dst, ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003801 if (!pchan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 result = L2CAP_CR_BAD_PSM;
3803 goto sendresp;
3804 }
3805
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003806 mutex_lock(&conn->chan_lock);
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003807 l2cap_chan_lock(pchan);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00003808
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003809 /* Check if the ACL is secure enough (if not SDP) */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003810 if (psm != cpu_to_le16(L2CAP_PSM_SDP) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003811 !hci_conn_check_link_mode(conn->hcon)) {
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003812 conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003813 result = L2CAP_CR_SEC_BLOCK;
3814 goto response;
3815 }
3816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 result = L2CAP_CR_NO_MEM;
3818
Mallikarjun Phularidd1a8f82018-10-05 14:48:13 +05303819 /* Check for valid dynamic CID range (as per Erratum 3253) */
3820 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_DYN_END) {
3821 result = L2CAP_CR_INVALID_SCID;
Gustavo Padovan2dfa1002012-05-27 22:27:58 -03003822 goto response;
Mallikarjun Phularidd1a8f82018-10-05 14:48:13 +05303823 }
3824
3825 /* Check if we already have channel with that dcid */
3826 if (__l2cap_get_chan_by_dcid(conn, scid)) {
3827 result = L2CAP_CR_SCID_IN_USE;
3828 goto response;
3829 }
Gustavo Padovan2dfa1002012-05-27 22:27:58 -03003830
Gustavo Padovan80b98022012-05-27 22:27:51 -03003831 chan = pchan->ops->new_connection(pchan);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03003832 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 goto response;
3834
Syam Sidhardhan330b6c12013-08-06 01:59:12 +09003835 /* For certain devices (ex: HID mouse), support for authentication,
3836 * pairing and bonding is optional. For such devices, inorder to avoid
3837 * the ACL alive for too long after L2CAP disconnection, reset the ACL
3838 * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect.
3839 */
3840 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
3841
Marcel Holtmann7eafc592013-10-13 08:12:47 -07003842 bacpy(&chan->src, &conn->hcon->src);
3843 bacpy(&chan->dst, &conn->hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +02003844 chan->src_type = bdaddr_src_type(conn->hcon);
3845 chan->dst_type = bdaddr_dst_type(conn->hcon);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003846 chan->psm = psm;
3847 chan->dcid = scid;
Mat Martineau17009152012-10-23 15:24:07 -07003848 chan->local_amp_id = amp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003850 __l2cap_chan_add(conn, chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003851
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003852 dcid = chan->scid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Gustavo Padovan8d836d72013-10-15 19:24:47 -03003854 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003856 chan->ident = cmd->ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Marcel Holtmann984947d2009-02-06 23:35:19 +01003858 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
Johan Hedberge7cafc42014-07-17 15:35:38 +03003859 if (l2cap_chan_check_security(chan, false)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07003860 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003861 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003862 result = L2CAP_CR_PEND;
3863 status = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08003864 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003865 } else {
Mat Martineau17009152012-10-23 15:24:07 -07003866 /* Force pending result for AMP controllers.
3867 * The connection will succeed after the
3868 * physical link is up.
3869 */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003870 if (amp_id == AMP_ID_BREDR) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003871 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau17009152012-10-23 15:24:07 -07003872 result = L2CAP_CR_SUCCESS;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003873 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003874 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003875 result = L2CAP_CR_PEND;
Mat Martineau17009152012-10-23 15:24:07 -07003876 }
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003877 status = L2CAP_CS_NO_INFO;
3878 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003879 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003880 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003881 result = L2CAP_CR_PEND;
3882 status = L2CAP_CS_AUTHEN_PEND;
3883 }
3884 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003885 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003886 result = L2CAP_CR_PEND;
3887 status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 }
3889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890response:
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003891 l2cap_chan_unlock(pchan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003892 mutex_unlock(&conn->chan_lock);
Johan Hedberga24cce12014-08-07 22:56:42 +03003893 l2cap_chan_put(pchan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
3895sendresp:
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003896 rsp.scid = cpu_to_le16(scid);
3897 rsp.dcid = cpu_to_le16(dcid);
3898 rsp.result = cpu_to_le16(result);
3899 rsp.status = cpu_to_le16(status);
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003900 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003901
3902 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
3903 struct l2cap_info_req info;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003904 info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003905
3906 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
3907 conn->info_ident = l2cap_get_ident(conn);
3908
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08003909 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003910
Gustavo Padovan2d792812012-10-06 10:07:01 +01003911 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
3912 sizeof(info), &info);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003913 }
3914
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003915 if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003916 result == L2CAP_CR_SUCCESS) {
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003917 u8 buf[128];
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003918 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003919 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Ben Serie860d2c2017-09-09 23:15:59 +02003920 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003921 chan->num_conf_req++;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003922 }
Mat Martineau17009152012-10-23 15:24:07 -07003923
3924 return chan;
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003925}
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003926
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003927static int l2cap_connect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003928 struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003929{
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303930 struct hci_dev *hdev = conn->hcon->hdev;
3931 struct hci_conn *hcon = conn->hcon;
3932
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003933 if (cmd_len < sizeof(struct l2cap_conn_req))
3934 return -EPROTO;
3935
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303936 hci_dev_lock(hdev);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003937 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303938 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00003939 mgmt_device_connected(hdev, hcon, 0, NULL, 0);
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303940 hci_dev_unlock(hdev);
3941
Gustavo Padovan300229f2012-10-12 19:40:40 +08003942 l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 return 0;
3944}
3945
Mat Martineau5909cf32012-10-23 15:24:08 -07003946static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003947 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3948 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
3950 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
3951 u16 scid, dcid, result, status;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003952 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 u8 req[128];
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003954 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003956 if (cmd_len < sizeof(*rsp))
3957 return -EPROTO;
3958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 scid = __le16_to_cpu(rsp->scid);
3960 dcid = __le16_to_cpu(rsp->dcid);
3961 result = __le16_to_cpu(rsp->result);
3962 status = __le16_to_cpu(rsp->status);
3963
Andrei Emeltchenko1b009c92012-02-21 12:54:54 +02003964 BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x",
Gustavo Padovan2d792812012-10-06 10:07:01 +01003965 dcid, scid, result, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003967 mutex_lock(&conn->chan_lock);
3968
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 if (scid) {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003970 chan = __l2cap_get_chan_by_scid(conn, scid);
3971 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003972 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003973 goto unlock;
3974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 } else {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003976 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
3977 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003978 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003979 goto unlock;
3980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 }
3982
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003983 err = 0;
3984
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003985 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 switch (result) {
3988 case L2CAP_CR_SUCCESS:
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03003989 l2cap_state_change(chan, BT_CONFIG);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003990 chan->ident = 0;
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003991 chan->dcid = dcid;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003992 clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01003993
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003994 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003995 break;
3996
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Ben Serie860d2c2017-09-09 23:15:59 +02003998 l2cap_build_conf_req(chan, req, sizeof(req)), req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003999 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 break;
4001
4002 case L2CAP_CR_PEND:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004003 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 break;
4005
4006 default:
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004007 l2cap_chan_del(chan, ECONNREFUSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 break;
4009 }
4010
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004011 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004012
4013unlock:
4014 mutex_unlock(&conn->chan_lock);
4015
4016 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004019static inline void set_default_fcs(struct l2cap_chan *chan)
Mat Martineau8c462b62010-08-24 15:35:42 -07004020{
4021 /* FCS is enabled only in ERTM or streaming mode, if one or both
4022 * sides request it.
4023 */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03004024 if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004025 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02004026 else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state))
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004027 chan->fcs = L2CAP_FCS_CRC16;
Mat Martineau8c462b62010-08-24 15:35:42 -07004028}
4029
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004030static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
4031 u8 ident, u16 flags)
4032{
4033 struct l2cap_conn *conn = chan->conn;
4034
4035 BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident,
4036 flags);
4037
4038 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
4039 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
4040
4041 l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP,
4042 l2cap_build_conf_rsp(chan, data,
4043 L2CAP_CONF_SUCCESS, flags), data);
4044}
4045
Johan Hedberg662d6522013-10-16 11:20:47 +03004046static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
4047 u16 scid, u16 dcid)
4048{
4049 struct l2cap_cmd_rej_cid rej;
4050
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004051 rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
Johan Hedberg662d6522013-10-16 11:20:47 +03004052 rej.scid = __cpu_to_le16(scid);
4053 rej.dcid = __cpu_to_le16(dcid);
4054
4055 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
4056}
4057
Gustavo Padovan2d792812012-10-06 10:07:01 +01004058static inline int l2cap_config_req(struct l2cap_conn *conn,
4059 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4060 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
4062 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
4063 u16 dcid, flags;
4064 u8 rsp[64];
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004065 struct l2cap_chan *chan;
Mat Martineau3c588192012-04-11 10:48:42 -07004066 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004068 if (cmd_len < sizeof(*req))
4069 return -EPROTO;
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 dcid = __le16_to_cpu(req->dcid);
4072 flags = __le16_to_cpu(req->flags);
4073
4074 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
4075
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004076 chan = l2cap_get_chan_by_scid(conn, dcid);
Johan Hedberg662d6522013-10-16 11:20:47 +03004077 if (!chan) {
4078 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0);
4079 return 0;
4080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
David S. Miller033b1142011-07-21 13:38:42 -07004082 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
Johan Hedberg662d6522013-10-16 11:20:47 +03004083 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4084 chan->dcid);
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004085 goto unlock;
Gustavo F. Padovandf6bd742010-06-14 02:26:15 -03004086 }
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004087
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004088 /* Reject if config buffer is too small. */
Al Viro88219a02007-07-29 00:17:25 -07004089 len = cmd_len - sizeof(*req);
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004090 if (chan->conf_len + len > sizeof(chan->conf_req)) {
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004091 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004092 l2cap_build_conf_rsp(chan, rsp,
4093 L2CAP_CONF_REJECT, flags), rsp);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004094 goto unlock;
4095 }
4096
4097 /* Store config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004098 memcpy(chan->conf_req + chan->conf_len, req->data, len);
4099 chan->conf_len += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004101 if (flags & L2CAP_CONF_FLAG_CONTINUATION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 /* Incomplete config. Send empty response. */
4103 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004104 l2cap_build_conf_rsp(chan, rsp,
4105 L2CAP_CONF_SUCCESS, flags), rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 goto unlock;
4107 }
4108
4109 /* Complete config. */
Ben Serie860d2c2017-09-09 23:15:59 +02004110 len = l2cap_parse_conf_req(chan, rsp, sizeof(rsp));
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004111 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004112 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 goto unlock;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Mat Martineau1500109b2012-10-23 15:24:15 -07004116 chan->ident = cmd->ident;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004117 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004118 chan->num_conf_rsp++;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004119
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004120 /* Reset config buffer. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004121 chan->conf_len = 0;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004122
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004123 if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
Marcel Holtmann876d9482007-10-20 13:35:42 +02004124 goto unlock;
4125
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004126 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004127 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004128
Mat Martineau105bdf92012-04-27 16:50:48 -07004129 if (chan->mode == L2CAP_MODE_ERTM ||
4130 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004131 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004132
Mat Martineau3c588192012-04-11 10:48:42 -07004133 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004134 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004135 else
4136 l2cap_chan_ready(chan);
4137
Marcel Holtmann876d9482007-10-20 13:35:42 +02004138 goto unlock;
4139 }
4140
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004141 if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02004142 u8 buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Ben Serie860d2c2017-09-09 23:15:59 +02004144 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004145 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 }
4147
Stephen Hemminger49c922b2014-10-27 21:12:20 -07004148 /* Got Conf Rsp PENDING from remote side and assume we sent
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004149 Conf Rsp PENDING in the code above */
4150 if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004151 test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004152
4153 /* check compatibility */
4154
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004155 /* Send rsp for BR/EDR channel */
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004156 if (!chan->hs_hcon)
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004157 l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags);
4158 else
4159 chan->ident = cmd->ident;
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004160 }
4161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162unlock:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004163 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Gustavo Padovan2d792812012-10-06 10:07:01 +01004167static inline int l2cap_config_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004168 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4169 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170{
4171 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
4172 u16 scid, flags, result;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004173 struct l2cap_chan *chan;
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004174 int len = cmd_len - sizeof(*rsp);
Mat Martineau3c588192012-04-11 10:48:42 -07004175 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004177 if (cmd_len < sizeof(*rsp))
4178 return -EPROTO;
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 scid = __le16_to_cpu(rsp->scid);
4181 flags = __le16_to_cpu(rsp->flags);
4182 result = __le16_to_cpu(rsp->result);
4183
Andrei Emeltchenko61386cb2012-03-12 12:13:07 +02004184 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
4185 result, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004187 chan = l2cap_get_chan_by_scid(conn, scid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004188 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return 0;
4190
4191 switch (result) {
4192 case L2CAP_CONF_SUCCESS:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004193 l2cap_conf_rfc_get(chan, rsp->data, len);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004194 clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 break;
4196
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004197 case L2CAP_CONF_PENDING:
4198 set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
4199
4200 if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
4201 char buf[64];
4202
4203 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Ben Serie860d2c2017-09-09 23:15:59 +02004204 buf, sizeof(buf), &result);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004205 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004206 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004207 goto done;
4208 }
4209
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004210 if (!chan->hs_hcon) {
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004211 l2cap_send_efs_conf_rsp(chan, buf, cmd->ident,
4212 0);
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02004213 } else {
4214 if (l2cap_check_efs(chan)) {
4215 amp_create_logical_link(chan);
4216 chan->ident = cmd->ident;
4217 }
4218 }
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004219 }
4220 goto done;
4221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 case L2CAP_CONF_UNACCEPT:
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004223 if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004224 char req[64];
4225
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004226 if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004227 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004228 goto done;
4229 }
4230
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004231 /* throw out any old stored conf requests */
4232 result = L2CAP_CONF_SUCCESS;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03004233 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Ben Serie860d2c2017-09-09 23:15:59 +02004234 req, sizeof(req), &result);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004235 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004236 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004237 goto done;
4238 }
4239
4240 l2cap_send_cmd(conn, l2cap_get_ident(conn),
Gustavo Padovan2d792812012-10-06 10:07:01 +01004241 L2CAP_CONF_REQ, len, req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004242 chan->num_conf_req++;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004243 if (result != L2CAP_CONF_SUCCESS)
4244 goto done;
4245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
4247
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09004248 default:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004249 l2cap_chan_set_err(chan, ECONNRESET);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +02004250
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08004251 __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004252 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 goto done;
4254 }
4255
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004256 if (flags & L2CAP_CONF_FLAG_CONTINUATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 goto done;
4258
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004259 set_bit(CONF_INPUT_DONE, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004261 if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004262 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004263
Mat Martineau105bdf92012-04-27 16:50:48 -07004264 if (chan->mode == L2CAP_MODE_ERTM ||
4265 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004266 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004267
Mat Martineau3c588192012-04-11 10:48:42 -07004268 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004269 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004270 else
4271 l2cap_chan_ready(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 }
4273
4274done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004275 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277}
4278
Gustavo Padovan2d792812012-10-06 10:07:01 +01004279static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004280 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4281 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282{
4283 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
4284 struct l2cap_disconn_rsp rsp;
4285 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004286 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004288 if (cmd_len != sizeof(*req))
4289 return -EPROTO;
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 scid = __le16_to_cpu(req->scid);
4292 dcid = __le16_to_cpu(req->dcid);
4293
4294 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
4295
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004296 mutex_lock(&conn->chan_lock);
4297
4298 chan = __l2cap_get_chan_by_scid(conn, dcid);
4299 if (!chan) {
4300 mutex_unlock(&conn->chan_lock);
Johan Hedberg662d6522013-10-16 11:20:47 +03004301 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid);
4302 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004305 l2cap_chan_lock(chan);
4306
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03004307 rsp.dcid = cpu_to_le16(chan->scid);
4308 rsp.scid = cpu_to_le16(chan->dcid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4310
Gustavo Padovan5ec1bbe2013-10-15 19:24:48 -03004311 chan->ops->set_shutdown(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Mat Martineau61d6ef32012-04-27 16:50:50 -07004313 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004314 l2cap_chan_del(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004315
4316 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Gustavo Padovan80b98022012-05-27 22:27:51 -03004318 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004319 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004320
4321 mutex_unlock(&conn->chan_lock);
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return 0;
4324}
4325
Gustavo Padovan2d792812012-10-06 10:07:01 +01004326static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004327 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4328 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
4330 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
4331 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004332 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004334 if (cmd_len != sizeof(*rsp))
4335 return -EPROTO;
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 scid = __le16_to_cpu(rsp->scid);
4338 dcid = __le16_to_cpu(rsp->dcid);
4339
4340 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
4341
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004342 mutex_lock(&conn->chan_lock);
4343
4344 chan = __l2cap_get_chan_by_scid(conn, scid);
4345 if (!chan) {
4346 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004350 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004351
Mat Martineau61d6ef32012-04-27 16:50:50 -07004352 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004353 l2cap_chan_del(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004354
4355 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Gustavo Padovan80b98022012-05-27 22:27:51 -03004357 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004358 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004359
4360 mutex_unlock(&conn->chan_lock);
4361
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 return 0;
4363}
4364
Gustavo Padovan2d792812012-10-06 10:07:01 +01004365static inline int l2cap_information_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004366 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4367 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
4369 struct l2cap_info_req *req = (struct l2cap_info_req *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 u16 type;
4371
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004372 if (cmd_len != sizeof(*req))
4373 return -EPROTO;
4374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 type = __le16_to_cpu(req->type);
4376
4377 BT_DBG("type 0x%4.4x", type);
4378
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004379 if (type == L2CAP_IT_FEAT_MASK) {
4380 u8 buf[8];
Marcel Holtmann44dd46d2009-05-02 19:09:01 -07004381 u32 feat_mask = l2cap_feat_mask;
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004382 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004383 rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
4384 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03004385 if (!disable_ertm)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004386 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
Gustavo Padovan2d792812012-10-06 10:07:01 +01004387 | L2CAP_FEAT_FCS;
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004388 if (conn->local_fixed_chan & L2CAP_FC_A2MP)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03004389 feat_mask |= L2CAP_FEAT_EXT_FLOW
Gustavo Padovan2d792812012-10-06 10:07:01 +01004390 | L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenkoa5fd6f32011-09-16 16:26:32 +03004391
Gustavo F. Padovan1b7bf4e2009-08-24 00:45:20 -03004392 put_unaligned_le32(feat_mask, rsp->data);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004393 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4394 buf);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004395 } else if (type == L2CAP_IT_FIXED_CHAN) {
4396 u8 buf[12];
4397 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Mat Martineau50a147c2011-11-02 16:18:34 -07004398
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004399 rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
4400 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004401 rsp->data[0] = conn->local_fixed_chan;
4402 memset(rsp->data + 1, 0, 7);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004403 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4404 buf);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004405 } else {
4406 struct l2cap_info_rsp rsp;
4407 rsp.type = cpu_to_le16(type);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004408 rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004409 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp),
4410 &rsp);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
4413 return 0;
4414}
4415
Gustavo Padovan2d792812012-10-06 10:07:01 +01004416static inline int l2cap_information_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004417 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4418 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
4420 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
4421 u16 type, result;
4422
Jaganath Kanakkassery3f6fa3d2013-06-21 19:55:11 +05304423 if (cmd_len < sizeof(*rsp))
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004424 return -EPROTO;
4425
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 type = __le16_to_cpu(rsp->type);
4427 result = __le16_to_cpu(rsp->result);
4428
4429 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
4430
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004431 /* L2CAP Info req/rsp are unbound to channels, add extra checks */
4432 if (cmd->ident != conn->info_ident ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01004433 conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004434 return 0;
4435
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02004436 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004437
Ville Tervoadb08ed2010-08-04 09:43:33 +03004438 if (result != L2CAP_IR_SUCCESS) {
4439 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4440 conn->info_ident = 0;
4441
4442 l2cap_conn_start(conn);
4443
4444 return 0;
4445 }
4446
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004447 switch (type) {
4448 case L2CAP_IT_FEAT_MASK:
Harvey Harrison83985312008-05-02 16:25:46 -07004449 conn->feat_mask = get_unaligned_le32(rsp->data);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004450
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07004451 if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004452 struct l2cap_info_req req;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004453 req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004454
4455 conn->info_ident = l2cap_get_ident(conn);
4456
4457 l2cap_send_cmd(conn, conn->info_ident,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004458 L2CAP_INFO_REQ, sizeof(req), &req);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004459 } else {
4460 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4461 conn->info_ident = 0;
4462
4463 l2cap_conn_start(conn);
4464 }
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004465 break;
4466
4467 case L2CAP_IT_FIXED_CHAN:
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004468 conn->remote_fixed_chan = rsp->data[0];
Marcel Holtmann984947d2009-02-06 23:35:19 +01004469 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004470 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004471
4472 l2cap_conn_start(conn);
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004473 break;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004474 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return 0;
4477}
4478
Mat Martineau17009152012-10-23 15:24:07 -07004479static int l2cap_create_channel_req(struct l2cap_conn *conn,
4480 struct l2cap_cmd_hdr *cmd,
4481 u16 cmd_len, void *data)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004482{
4483 struct l2cap_create_chan_req *req = data;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004484 struct l2cap_create_chan_rsp rsp;
Mat Martineau17009152012-10-23 15:24:07 -07004485 struct l2cap_chan *chan;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004486 struct hci_dev *hdev;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004487 u16 psm, scid;
4488
4489 if (cmd_len != sizeof(*req))
4490 return -EPROTO;
4491
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004492 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004493 return -EINVAL;
4494
4495 psm = le16_to_cpu(req->psm);
4496 scid = le16_to_cpu(req->scid);
4497
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004498 BT_DBG("psm 0x%2.2x, scid 0x%4.4x, amp_id %d", psm, scid, req->amp_id);
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004499
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004500 /* For controller id 0 make BR/EDR connection */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004501 if (req->amp_id == AMP_ID_BREDR) {
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004502 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4503 req->amp_id);
4504 return 0;
4505 }
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004506
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004507 /* Validate AMP controller id */
4508 hdev = hci_dev_get(req->amp_id);
4509 if (!hdev)
4510 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004511
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004512 if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) {
Mat Martineau17009152012-10-23 15:24:07 -07004513 hci_dev_put(hdev);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004514 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004515 }
4516
4517 chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4518 req->amp_id);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004519 if (chan) {
4520 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4521 struct hci_conn *hs_hcon;
4522
Marcel Holtmann98e0f7e2013-10-13 02:23:40 -07004523 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
4524 &conn->hcon->dst);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004525 if (!hs_hcon) {
4526 hci_dev_put(hdev);
Johan Hedberg662d6522013-10-16 11:20:47 +03004527 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4528 chan->dcid);
4529 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004530 }
4531
4532 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
4533
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004534 mgr->bredr_chan = chan;
4535 chan->hs_hcon = hs_hcon;
Andrei Emeltchenkofd45bf42012-11-20 17:16:22 +02004536 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004537 conn->mtu = hdev->block_mtu;
4538 }
4539
4540 hci_dev_put(hdev);
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004541
4542 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004543
4544error:
4545 rsp.dcid = 0;
4546 rsp.scid = cpu_to_le16(scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004547 rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP);
4548 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004549
4550 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4551 sizeof(rsp), &rsp);
4552
Johan Hedbergdc280802013-09-16 13:05:13 +03004553 return 0;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004554}
4555
Mat Martineau8eb200b2012-10-23 15:24:17 -07004556static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
4557{
4558 struct l2cap_move_chan_req req;
4559 u8 ident;
4560
4561 BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id);
4562
4563 ident = l2cap_get_ident(chan->conn);
4564 chan->ident = ident;
4565
4566 req.icid = cpu_to_le16(chan->scid);
4567 req.dest_amp_id = dest_amp_id;
4568
4569 l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req),
4570 &req);
4571
4572 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4573}
4574
Mat Martineau1500109b2012-10-23 15:24:15 -07004575static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004576{
4577 struct l2cap_move_chan_rsp rsp;
4578
Mat Martineau1500109b2012-10-23 15:24:15 -07004579 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004580
Mat Martineau1500109b2012-10-23 15:24:15 -07004581 rsp.icid = cpu_to_le16(chan->dcid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004582 rsp.result = cpu_to_le16(result);
4583
Mat Martineau1500109b2012-10-23 15:24:15 -07004584 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP,
4585 sizeof(rsp), &rsp);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004586}
4587
Mat Martineau5b155ef2012-10-23 15:24:14 -07004588static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004589{
4590 struct l2cap_move_chan_cfm cfm;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004591
Mat Martineau5b155ef2012-10-23 15:24:14 -07004592 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004593
Mat Martineau5b155ef2012-10-23 15:24:14 -07004594 chan->ident = l2cap_get_ident(chan->conn);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004595
Mat Martineau5b155ef2012-10-23 15:24:14 -07004596 cfm.icid = cpu_to_le16(chan->scid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004597 cfm.result = cpu_to_le16(result);
4598
Mat Martineau5b155ef2012-10-23 15:24:14 -07004599 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM,
4600 sizeof(cfm), &cfm);
4601
4602 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4603}
4604
4605static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid)
4606{
4607 struct l2cap_move_chan_cfm cfm;
4608
4609 BT_DBG("conn %p, icid 0x%4.4x", conn, icid);
4610
4611 cfm.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004612 cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED);
Mat Martineau5b155ef2012-10-23 15:24:14 -07004613
4614 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM,
4615 sizeof(cfm), &cfm);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004616}
4617
4618static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004619 u16 icid)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004620{
4621 struct l2cap_move_chan_cfm_rsp rsp;
4622
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004623 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004624
4625 rsp.icid = cpu_to_le16(icid);
4626 l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
4627}
4628
Mat Martineau5f3847a2012-10-23 15:24:12 -07004629static void __release_logical_link(struct l2cap_chan *chan)
4630{
4631 chan->hs_hchan = NULL;
4632 chan->hs_hcon = NULL;
4633
4634 /* Placeholder - release the logical link */
4635}
4636
Mat Martineau1500109b2012-10-23 15:24:15 -07004637static void l2cap_logical_fail(struct l2cap_chan *chan)
4638{
4639 /* Logical link setup failed */
4640 if (chan->state != BT_CONNECTED) {
4641 /* Create channel failure, disconnect */
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004642 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineau1500109b2012-10-23 15:24:15 -07004643 return;
4644 }
4645
4646 switch (chan->move_role) {
4647 case L2CAP_MOVE_ROLE_RESPONDER:
4648 l2cap_move_done(chan);
4649 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP);
4650 break;
4651 case L2CAP_MOVE_ROLE_INITIATOR:
4652 if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP ||
4653 chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) {
4654 /* Remote has only sent pending or
4655 * success responses, clean up
4656 */
4657 l2cap_move_done(chan);
4658 }
4659
4660 /* Other amp move states imply that the move
4661 * has already aborted
4662 */
4663 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
4664 break;
4665 }
4666}
4667
4668static void l2cap_logical_finish_create(struct l2cap_chan *chan,
4669 struct hci_chan *hchan)
4670{
4671 struct l2cap_conf_rsp rsp;
Mat Martineau1500109b2012-10-23 15:24:15 -07004672
Andrei Emeltchenko336178a2012-10-31 15:46:27 +02004673 chan->hs_hchan = hchan;
Mat Martineau1500109b2012-10-23 15:24:15 -07004674 chan->hs_hcon->l2cap_data = chan->conn;
4675
Andrei Emeltchenko35ba9562012-10-25 15:20:43 +03004676 l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0);
Mat Martineau1500109b2012-10-23 15:24:15 -07004677
4678 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Andrei Emeltchenkofe79c6f2012-10-25 15:20:42 +03004679 int err;
Mat Martineau1500109b2012-10-23 15:24:15 -07004680
4681 set_default_fcs(chan);
4682
4683 err = l2cap_ertm_init(chan);
4684 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004685 l2cap_send_disconn_req(chan, -err);
Mat Martineau1500109b2012-10-23 15:24:15 -07004686 else
4687 l2cap_chan_ready(chan);
4688 }
4689}
4690
4691static void l2cap_logical_finish_move(struct l2cap_chan *chan,
4692 struct hci_chan *hchan)
4693{
4694 chan->hs_hcon = hchan->conn;
4695 chan->hs_hcon->l2cap_data = chan->conn;
4696
4697 BT_DBG("move_state %d", chan->move_state);
4698
4699 switch (chan->move_state) {
4700 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4701 /* Move confirm will be sent after a success
4702 * response is received
4703 */
4704 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4705 break;
4706 case L2CAP_MOVE_WAIT_LOGICAL_CFM:
4707 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4708 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4709 } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
4710 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4711 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4712 } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4713 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4714 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4715 }
4716 break;
4717 default:
4718 /* Move was not in expected state, free the channel */
4719 __release_logical_link(chan);
4720
4721 chan->move_state = L2CAP_MOVE_STABLE;
4722 }
4723}
4724
4725/* Call with chan locked */
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004726void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
4727 u8 status)
Mat Martineau5b155ef2012-10-23 15:24:14 -07004728{
Mat Martineau1500109b2012-10-23 15:24:15 -07004729 BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status);
4730
4731 if (status) {
4732 l2cap_logical_fail(chan);
4733 __release_logical_link(chan);
4734 return;
4735 }
4736
4737 if (chan->state != BT_CONNECTED) {
4738 /* Ignore logical link if channel is on BR/EDR */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004739 if (chan->local_amp_id != AMP_ID_BREDR)
Mat Martineau1500109b2012-10-23 15:24:15 -07004740 l2cap_logical_finish_create(chan, hchan);
4741 } else {
4742 l2cap_logical_finish_move(chan, hchan);
4743 }
Mat Martineau5b155ef2012-10-23 15:24:14 -07004744}
4745
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004746void l2cap_move_start(struct l2cap_chan *chan)
4747{
4748 BT_DBG("chan %p", chan);
4749
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004750 if (chan->local_amp_id == AMP_ID_BREDR) {
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004751 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
4752 return;
4753 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4754 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4755 /* Placeholder - start physical link setup */
4756 } else {
4757 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4758 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4759 chan->move_id = 0;
4760 l2cap_move_setup(chan);
4761 l2cap_send_move_chan_req(chan, 0);
4762 }
4763}
4764
Mat Martineau8eb200b2012-10-23 15:24:17 -07004765static void l2cap_do_create(struct l2cap_chan *chan, int result,
4766 u8 local_amp_id, u8 remote_amp_id)
4767{
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004768 BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state),
4769 local_amp_id, remote_amp_id);
4770
Andrei Emeltchenko12d6cc62012-11-14 17:39:31 +02004771 chan->fcs = L2CAP_FCS_NONE;
4772
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004773 /* Outgoing channel on AMP */
4774 if (chan->state == BT_CONNECT) {
4775 if (result == L2CAP_CR_SUCCESS) {
4776 chan->local_amp_id = local_amp_id;
4777 l2cap_send_create_chan_req(chan, remote_amp_id);
4778 } else {
4779 /* Revert to BR/EDR connect */
4780 l2cap_send_conn_req(chan);
4781 }
4782
4783 return;
4784 }
4785
4786 /* Incoming channel on AMP */
4787 if (__l2cap_no_conn_pending(chan)) {
Mat Martineau8eb200b2012-10-23 15:24:17 -07004788 struct l2cap_conn_rsp rsp;
4789 char buf[128];
4790 rsp.scid = cpu_to_le16(chan->dcid);
4791 rsp.dcid = cpu_to_le16(chan->scid);
4792
Mat Martineau8eb200b2012-10-23 15:24:17 -07004793 if (result == L2CAP_CR_SUCCESS) {
4794 /* Send successful response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004795 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
4796 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004797 } else {
4798 /* Send negative response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004799 rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM);
4800 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004801 }
4802
4803 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP,
4804 sizeof(rsp), &rsp);
4805
4806 if (result == L2CAP_CR_SUCCESS) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02004807 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004808 set_bit(CONF_REQ_SENT, &chan->conf_state);
4809 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
4810 L2CAP_CONF_REQ,
Ben Serie860d2c2017-09-09 23:15:59 +02004811 l2cap_build_conf_req(chan, buf, sizeof(buf)), buf);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004812 chan->num_conf_req++;
4813 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004814 }
4815}
4816
4817static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id,
4818 u8 remote_amp_id)
4819{
4820 l2cap_move_setup(chan);
4821 chan->move_id = local_amp_id;
4822 chan->move_state = L2CAP_MOVE_WAIT_RSP;
4823
4824 l2cap_send_move_chan_req(chan, remote_amp_id);
4825}
4826
4827static void l2cap_do_move_respond(struct l2cap_chan *chan, int result)
4828{
4829 struct hci_chan *hchan = NULL;
4830
4831 /* Placeholder - get hci_chan for logical link */
4832
4833 if (hchan) {
4834 if (hchan->state == BT_CONNECTED) {
4835 /* Logical link is ready to go */
4836 chan->hs_hcon = hchan->conn;
4837 chan->hs_hcon->l2cap_data = chan->conn;
4838 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4839 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4840
4841 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
4842 } else {
4843 /* Wait for logical link to be ready */
4844 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4845 }
4846 } else {
4847 /* Logical link not available */
4848 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED);
4849 }
4850}
4851
4852static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result)
4853{
4854 if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4855 u8 rsp_result;
4856 if (result == -EINVAL)
4857 rsp_result = L2CAP_MR_BAD_ID;
4858 else
4859 rsp_result = L2CAP_MR_NOT_ALLOWED;
4860
4861 l2cap_send_move_chan_rsp(chan, rsp_result);
4862 }
4863
4864 chan->move_role = L2CAP_MOVE_ROLE_NONE;
4865 chan->move_state = L2CAP_MOVE_STABLE;
4866
4867 /* Restart data transmission */
4868 l2cap_ertm_send(chan);
4869}
4870
Andrei Emeltchenkoa514b172012-11-14 17:39:30 +02004871/* Invoke with locked chan */
4872void __l2cap_physical_cfm(struct l2cap_chan *chan, int result)
Mat Martineau8eb200b2012-10-23 15:24:17 -07004873{
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004874 u8 local_amp_id = chan->local_amp_id;
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +02004875 u8 remote_amp_id = chan->remote_amp_id;
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004876
Mat Martineau8eb200b2012-10-23 15:24:17 -07004877 BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d",
4878 chan, result, local_amp_id, remote_amp_id);
4879
Mat Martineau8eb200b2012-10-23 15:24:17 -07004880 if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) {
4881 l2cap_chan_unlock(chan);
4882 return;
4883 }
4884
4885 if (chan->state != BT_CONNECTED) {
4886 l2cap_do_create(chan, result, local_amp_id, remote_amp_id);
4887 } else if (result != L2CAP_MR_SUCCESS) {
4888 l2cap_do_move_cancel(chan, result);
4889 } else {
4890 switch (chan->move_role) {
4891 case L2CAP_MOVE_ROLE_INITIATOR:
4892 l2cap_do_move_initiate(chan, local_amp_id,
4893 remote_amp_id);
4894 break;
4895 case L2CAP_MOVE_ROLE_RESPONDER:
4896 l2cap_do_move_respond(chan, result);
4897 break;
4898 default:
4899 l2cap_do_move_cancel(chan, result);
4900 break;
4901 }
4902 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004903}
4904
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004905static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004906 struct l2cap_cmd_hdr *cmd,
4907 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004908{
4909 struct l2cap_move_chan_req *req = data;
Mat Martineau1500109b2012-10-23 15:24:15 -07004910 struct l2cap_move_chan_rsp rsp;
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004911 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004912 u16 icid = 0;
4913 u16 result = L2CAP_MR_NOT_ALLOWED;
4914
4915 if (cmd_len != sizeof(*req))
4916 return -EPROTO;
4917
4918 icid = le16_to_cpu(req->icid);
4919
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004920 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004921
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004922 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004923 return -EINVAL;
4924
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004925 chan = l2cap_get_chan_by_dcid(conn, icid);
4926 if (!chan) {
Mat Martineau1500109b2012-10-23 15:24:15 -07004927 rsp.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004928 rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED);
Mat Martineau1500109b2012-10-23 15:24:15 -07004929 l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP,
4930 sizeof(rsp), &rsp);
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004931 return 0;
4932 }
4933
Mat Martineau1500109b2012-10-23 15:24:15 -07004934 chan->ident = cmd->ident;
4935
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004936 if (chan->scid < L2CAP_CID_DYN_START ||
4937 chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY ||
4938 (chan->mode != L2CAP_MODE_ERTM &&
4939 chan->mode != L2CAP_MODE_STREAMING)) {
4940 result = L2CAP_MR_NOT_ALLOWED;
4941 goto send_move_response;
4942 }
4943
4944 if (chan->local_amp_id == req->dest_amp_id) {
4945 result = L2CAP_MR_SAME_ID;
4946 goto send_move_response;
4947 }
4948
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004949 if (req->dest_amp_id != AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004950 struct hci_dev *hdev;
4951 hdev = hci_dev_get(req->dest_amp_id);
4952 if (!hdev || hdev->dev_type != HCI_AMP ||
4953 !test_bit(HCI_UP, &hdev->flags)) {
4954 if (hdev)
4955 hci_dev_put(hdev);
4956
4957 result = L2CAP_MR_BAD_ID;
4958 goto send_move_response;
4959 }
4960 hci_dev_put(hdev);
4961 }
4962
4963 /* Detect a move collision. Only send a collision response
4964 * if this side has "lost", otherwise proceed with the move.
4965 * The winner has the larger bd_addr.
4966 */
4967 if ((__chan_is_moving(chan) ||
4968 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
Marcel Holtmann6f59b902013-10-13 05:24:01 -07004969 bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004970 result = L2CAP_MR_COLLISION;
4971 goto send_move_response;
4972 }
4973
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004974 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
4975 l2cap_move_setup(chan);
4976 chan->move_id = req->dest_amp_id;
4977 icid = chan->dcid;
4978
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004979 if (req->dest_amp_id == AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004980 /* Moving to BR/EDR */
4981 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4982 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4983 result = L2CAP_MR_PEND;
4984 } else {
4985 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4986 result = L2CAP_MR_SUCCESS;
4987 }
4988 } else {
4989 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4990 /* Placeholder - uncomment when amp functions are available */
4991 /*amp_accept_physical(chan, req->dest_amp_id);*/
4992 result = L2CAP_MR_PEND;
4993 }
4994
4995send_move_response:
Mat Martineau1500109b2012-10-23 15:24:15 -07004996 l2cap_send_move_chan_rsp(chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004997
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004998 l2cap_chan_unlock(chan);
4999
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005000 return 0;
5001}
5002
Mat Martineau5b155ef2012-10-23 15:24:14 -07005003static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result)
5004{
5005 struct l2cap_chan *chan;
5006 struct hci_chan *hchan = NULL;
5007
5008 chan = l2cap_get_chan_by_scid(conn, icid);
5009 if (!chan) {
5010 l2cap_send_move_chan_cfm_icid(conn, icid);
5011 return;
5012 }
5013
5014 __clear_chan_timer(chan);
5015 if (result == L2CAP_MR_PEND)
5016 __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT);
5017
5018 switch (chan->move_state) {
5019 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
5020 /* Move confirm will be sent when logical link
5021 * is complete.
5022 */
5023 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5024 break;
5025 case L2CAP_MOVE_WAIT_RSP_SUCCESS:
5026 if (result == L2CAP_MR_PEND) {
5027 break;
5028 } else if (test_bit(CONN_LOCAL_BUSY,
5029 &chan->conn_state)) {
5030 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
5031 } else {
5032 /* Logical link is up or moving to BR/EDR,
5033 * proceed with move
5034 */
5035 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
5036 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5037 }
5038 break;
5039 case L2CAP_MOVE_WAIT_RSP:
5040 /* Moving to AMP */
5041 if (result == L2CAP_MR_SUCCESS) {
5042 /* Remote is ready, send confirm immediately
5043 * after logical link is ready
5044 */
5045 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5046 } else {
5047 /* Both logical link and move success
5048 * are required to confirm
5049 */
5050 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP;
5051 }
5052
5053 /* Placeholder - get hci_chan for logical link */
5054 if (!hchan) {
5055 /* Logical link not available */
5056 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5057 break;
5058 }
5059
5060 /* If the logical link is not yet connected, do not
5061 * send confirmation.
5062 */
5063 if (hchan->state != BT_CONNECTED)
5064 break;
5065
5066 /* Logical link is already ready to go */
5067
5068 chan->hs_hcon = hchan->conn;
5069 chan->hs_hcon->l2cap_data = chan->conn;
5070
5071 if (result == L2CAP_MR_SUCCESS) {
5072 /* Can confirm now */
5073 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5074 } else {
5075 /* Now only need move success
5076 * to confirm
5077 */
5078 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
5079 }
5080
5081 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
5082 break;
5083 default:
5084 /* Any other amp move state means the move failed. */
5085 chan->move_id = chan->local_amp_id;
5086 l2cap_move_done(chan);
5087 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5088 }
5089
5090 l2cap_chan_unlock(chan);
5091}
5092
5093static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid,
5094 u16 result)
5095{
5096 struct l2cap_chan *chan;
5097
5098 chan = l2cap_get_chan_by_ident(conn, ident);
5099 if (!chan) {
5100 /* Could not locate channel, icid is best guess */
5101 l2cap_send_move_chan_cfm_icid(conn, icid);
5102 return;
5103 }
5104
5105 __clear_chan_timer(chan);
5106
5107 if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
5108 if (result == L2CAP_MR_COLLISION) {
5109 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
5110 } else {
5111 /* Cleanup - cancel move */
5112 chan->move_id = chan->local_amp_id;
5113 l2cap_move_done(chan);
5114 }
5115 }
5116
5117 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5118
5119 l2cap_chan_unlock(chan);
5120}
5121
5122static int l2cap_move_channel_rsp(struct l2cap_conn *conn,
5123 struct l2cap_cmd_hdr *cmd,
5124 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005125{
5126 struct l2cap_move_chan_rsp *rsp = data;
5127 u16 icid, result;
5128
5129 if (cmd_len != sizeof(*rsp))
5130 return -EPROTO;
5131
5132 icid = le16_to_cpu(rsp->icid);
5133 result = le16_to_cpu(rsp->result);
5134
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005135 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005136
Mat Martineau5b155ef2012-10-23 15:24:14 -07005137 if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND)
5138 l2cap_move_continue(conn, icid, result);
5139 else
5140 l2cap_move_fail(conn, cmd->ident, icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005141
5142 return 0;
5143}
5144
Mat Martineau5f3847a2012-10-23 15:24:12 -07005145static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5146 struct l2cap_cmd_hdr *cmd,
5147 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005148{
5149 struct l2cap_move_chan_cfm *cfm = data;
Mat Martineau5f3847a2012-10-23 15:24:12 -07005150 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005151 u16 icid, result;
5152
5153 if (cmd_len != sizeof(*cfm))
5154 return -EPROTO;
5155
5156 icid = le16_to_cpu(cfm->icid);
5157 result = le16_to_cpu(cfm->result);
5158
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005159 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005160
Mat Martineau5f3847a2012-10-23 15:24:12 -07005161 chan = l2cap_get_chan_by_dcid(conn, icid);
5162 if (!chan) {
5163 /* Spec requires a response even if the icid was not found */
5164 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5165 return 0;
5166 }
5167
5168 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5169 if (result == L2CAP_MC_CONFIRMED) {
5170 chan->local_amp_id = chan->move_id;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005171 if (chan->local_amp_id == AMP_ID_BREDR)
Mat Martineau5f3847a2012-10-23 15:24:12 -07005172 __release_logical_link(chan);
5173 } else {
5174 chan->move_id = chan->local_amp_id;
5175 }
5176
5177 l2cap_move_done(chan);
5178 }
5179
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005180 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5181
Mat Martineau5f3847a2012-10-23 15:24:12 -07005182 l2cap_chan_unlock(chan);
5183
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005184 return 0;
5185}
5186
5187static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005188 struct l2cap_cmd_hdr *cmd,
5189 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005190{
5191 struct l2cap_move_chan_cfm_rsp *rsp = data;
Mat Martineau3fd71a02012-10-23 15:24:16 -07005192 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005193 u16 icid;
5194
5195 if (cmd_len != sizeof(*rsp))
5196 return -EPROTO;
5197
5198 icid = le16_to_cpu(rsp->icid);
5199
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005200 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005201
Mat Martineau3fd71a02012-10-23 15:24:16 -07005202 chan = l2cap_get_chan_by_scid(conn, icid);
5203 if (!chan)
5204 return 0;
5205
5206 __clear_chan_timer(chan);
5207
5208 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5209 chan->local_amp_id = chan->move_id;
5210
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005211 if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan)
Mat Martineau3fd71a02012-10-23 15:24:16 -07005212 __release_logical_link(chan);
5213
5214 l2cap_move_done(chan);
5215 }
5216
5217 l2cap_chan_unlock(chan);
5218
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005219 return 0;
5220}
5221
Claudio Takahaside731152011-02-11 19:28:55 -02005222static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005223 struct l2cap_cmd_hdr *cmd,
Johan Hedberg203e6392013-05-15 10:07:15 +03005224 u16 cmd_len, u8 *data)
Claudio Takahaside731152011-02-11 19:28:55 -02005225{
5226 struct hci_conn *hcon = conn->hcon;
5227 struct l2cap_conn_param_update_req *req;
5228 struct l2cap_conn_param_update_rsp rsp;
Johan Hedberg203e6392013-05-15 10:07:15 +03005229 u16 min, max, latency, to_multiplier;
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005230 int err;
Claudio Takahaside731152011-02-11 19:28:55 -02005231
Johan Hedberg40bef302014-07-16 11:42:27 +03005232 if (hcon->role != HCI_ROLE_MASTER)
Claudio Takahaside731152011-02-11 19:28:55 -02005233 return -EINVAL;
5234
Claudio Takahaside731152011-02-11 19:28:55 -02005235 if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
5236 return -EPROTO;
5237
5238 req = (struct l2cap_conn_param_update_req *) data;
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005239 min = __le16_to_cpu(req->min);
5240 max = __le16_to_cpu(req->max);
Claudio Takahaside731152011-02-11 19:28:55 -02005241 latency = __le16_to_cpu(req->latency);
5242 to_multiplier = __le16_to_cpu(req->to_multiplier);
5243
5244 BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
Gustavo Padovan2d792812012-10-06 10:07:01 +01005245 min, max, latency, to_multiplier);
Claudio Takahaside731152011-02-11 19:28:55 -02005246
5247 memset(&rsp, 0, sizeof(rsp));
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005248
Andre Guedesd4905f22014-06-25 21:52:52 -03005249 err = hci_check_conn_params(min, max, latency, to_multiplier);
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005250 if (err)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005251 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005252 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005253 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005254
5255 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005256 sizeof(rsp), &rsp);
Claudio Takahaside731152011-02-11 19:28:55 -02005257
Andre Guedesffb5a8272014-07-01 18:10:11 -03005258 if (!err) {
Johan Hedbergf4869e22014-07-02 17:37:32 +03005259 u8 store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03005260
Johan Hedbergf4869e22014-07-02 17:37:32 +03005261 store_hint = hci_le_conn_update(hcon, min, max, latency,
5262 to_multiplier);
5263 mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type,
5264 store_hint, min, max, latency,
5265 to_multiplier);
5266
Andre Guedesffb5a8272014-07-01 18:10:11 -03005267 }
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005268
Claudio Takahaside731152011-02-11 19:28:55 -02005269 return 0;
5270}
5271
Johan Hedbergf1496de2013-05-13 14:15:56 +03005272static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
5273 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5274 u8 *data)
5275{
5276 struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data;
Johan Hedberg3e64b7b2014-11-13 10:55:19 +02005277 struct hci_conn *hcon = conn->hcon;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005278 u16 dcid, mtu, mps, credits, result;
5279 struct l2cap_chan *chan;
Johan Hedberg3e64b7b2014-11-13 10:55:19 +02005280 int err, sec_level;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005281
5282 if (cmd_len < sizeof(*rsp))
5283 return -EPROTO;
5284
5285 dcid = __le16_to_cpu(rsp->dcid);
5286 mtu = __le16_to_cpu(rsp->mtu);
5287 mps = __le16_to_cpu(rsp->mps);
5288 credits = __le16_to_cpu(rsp->credits);
5289 result = __le16_to_cpu(rsp->result);
5290
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05305291 if (result == L2CAP_CR_LE_SUCCESS && (mtu < 23 || mps < 23 ||
Johan Hedberg40624182015-11-02 14:39:17 +02005292 dcid < L2CAP_CID_DYN_START ||
5293 dcid > L2CAP_CID_LE_DYN_END))
Johan Hedbergf1496de2013-05-13 14:15:56 +03005294 return -EPROTO;
5295
5296 BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x",
5297 dcid, mtu, mps, credits, result);
5298
5299 mutex_lock(&conn->chan_lock);
5300
5301 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5302 if (!chan) {
5303 err = -EBADSLT;
5304 goto unlock;
5305 }
5306
5307 err = 0;
5308
5309 l2cap_chan_lock(chan);
5310
5311 switch (result) {
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05305312 case L2CAP_CR_LE_SUCCESS:
Johan Hedberg40624182015-11-02 14:39:17 +02005313 if (__l2cap_get_chan_by_dcid(conn, dcid)) {
5314 err = -EBADSLT;
5315 break;
5316 }
5317
Johan Hedbergf1496de2013-05-13 14:15:56 +03005318 chan->ident = 0;
5319 chan->dcid = dcid;
5320 chan->omtu = mtu;
5321 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005322 chan->tx_credits = credits;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005323 l2cap_chan_ready(chan);
5324 break;
5325
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05305326 case L2CAP_CR_LE_AUTHENTICATION:
5327 case L2CAP_CR_LE_ENCRYPTION:
Johan Hedberg3e64b7b2014-11-13 10:55:19 +02005328 /* If we already have MITM protection we can't do
5329 * anything.
5330 */
5331 if (hcon->sec_level > BT_SECURITY_MEDIUM) {
5332 l2cap_chan_del(chan, ECONNREFUSED);
5333 break;
5334 }
5335
5336 sec_level = hcon->sec_level + 1;
5337 if (chan->sec_level < sec_level)
5338 chan->sec_level = sec_level;
5339
5340 /* We'll need to send a new Connect Request */
5341 clear_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags);
5342
5343 smp_conn_security(hcon, chan->sec_level);
5344 break;
5345
Johan Hedbergf1496de2013-05-13 14:15:56 +03005346 default:
5347 l2cap_chan_del(chan, ECONNREFUSED);
5348 break;
5349 }
5350
5351 l2cap_chan_unlock(chan);
5352
5353unlock:
5354 mutex_unlock(&conn->chan_lock);
5355
5356 return err;
5357}
5358
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005359static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005360 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5361 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005362{
5363 int err = 0;
5364
5365 switch (cmd->code) {
5366 case L2CAP_COMMAND_REJ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005367 l2cap_command_rej(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005368 break;
5369
5370 case L2CAP_CONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005371 err = l2cap_connect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005372 break;
5373
5374 case L2CAP_CONN_RSP:
Mat Martineauf5a25982012-10-11 17:48:21 +03005375 case L2CAP_CREATE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005376 l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005377 break;
5378
5379 case L2CAP_CONF_REQ:
5380 err = l2cap_config_req(conn, cmd, cmd_len, data);
5381 break;
5382
5383 case L2CAP_CONF_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005384 l2cap_config_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005385 break;
5386
5387 case L2CAP_DISCONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005388 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005389 break;
5390
5391 case L2CAP_DISCONN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005392 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005393 break;
5394
5395 case L2CAP_ECHO_REQ:
5396 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
5397 break;
5398
5399 case L2CAP_ECHO_RSP:
5400 break;
5401
5402 case L2CAP_INFO_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005403 err = l2cap_information_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005404 break;
5405
5406 case L2CAP_INFO_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005407 l2cap_information_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005408 break;
5409
Mat Martineauf94ff6f2011-11-02 16:18:32 -07005410 case L2CAP_CREATE_CHAN_REQ:
5411 err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
5412 break;
5413
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005414 case L2CAP_MOVE_CHAN_REQ:
5415 err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
5416 break;
5417
5418 case L2CAP_MOVE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005419 l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005420 break;
5421
5422 case L2CAP_MOVE_CHAN_CFM:
5423 err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
5424 break;
5425
5426 case L2CAP_MOVE_CHAN_CFM_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005427 l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005428 break;
5429
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005430 default:
5431 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
5432 err = -EINVAL;
5433 break;
5434 }
5435
5436 return err;
5437}
5438
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005439static int l2cap_le_connect_req(struct l2cap_conn *conn,
5440 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5441 u8 *data)
5442{
5443 struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data;
5444 struct l2cap_le_conn_rsp rsp;
5445 struct l2cap_chan *chan, *pchan;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005446 u16 dcid, scid, credits, mtu, mps;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005447 __le16 psm;
5448 u8 result;
5449
5450 if (cmd_len != sizeof(*req))
5451 return -EPROTO;
5452
5453 scid = __le16_to_cpu(req->scid);
5454 mtu = __le16_to_cpu(req->mtu);
5455 mps = __le16_to_cpu(req->mps);
5456 psm = req->psm;
5457 dcid = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005458 credits = 0;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005459
5460 if (mtu < 23 || mps < 23)
5461 return -EPROTO;
5462
5463 BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm),
5464 scid, mtu, mps);
5465
5466 /* Check if we have socket listening on psm */
5467 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
5468 &conn->hcon->dst, LE_LINK);
5469 if (!pchan) {
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05305470 result = L2CAP_CR_LE_BAD_PSM;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005471 chan = NULL;
5472 goto response;
5473 }
5474
5475 mutex_lock(&conn->chan_lock);
5476 l2cap_chan_lock(pchan);
5477
Johan Hedberg35dc6f82014-11-13 10:55:18 +02005478 if (!smp_sufficient_security(conn->hcon, pchan->sec_level,
5479 SMP_ALLOW_STK)) {
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05305480 result = L2CAP_CR_LE_AUTHENTICATION;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005481 chan = NULL;
5482 goto response_unlock;
5483 }
5484
Johan Hedberg8a7889c2015-11-02 14:39:15 +02005485 /* Check for valid dynamic CID range */
5486 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_LE_DYN_END) {
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05305487 result = L2CAP_CR_LE_INVALID_SCID;
Johan Hedberg8a7889c2015-11-02 14:39:15 +02005488 chan = NULL;
5489 goto response_unlock;
5490 }
5491
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005492 /* Check if we already have channel with that dcid */
5493 if (__l2cap_get_chan_by_dcid(conn, scid)) {
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05305494 result = L2CAP_CR_LE_SCID_IN_USE;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005495 chan = NULL;
5496 goto response_unlock;
5497 }
5498
5499 chan = pchan->ops->new_connection(pchan);
5500 if (!chan) {
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05305501 result = L2CAP_CR_LE_NO_MEM;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005502 goto response_unlock;
5503 }
5504
5505 bacpy(&chan->src, &conn->hcon->src);
5506 bacpy(&chan->dst, &conn->hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +02005507 chan->src_type = bdaddr_src_type(conn->hcon);
5508 chan->dst_type = bdaddr_dst_type(conn->hcon);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005509 chan->psm = psm;
5510 chan->dcid = scid;
5511 chan->omtu = mtu;
5512 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005513 chan->tx_credits = __le16_to_cpu(req->credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005514
5515 __l2cap_chan_add(conn, chan);
Luiz Augusto von Dentzfe149312018-09-04 13:39:20 +03005516
5517 l2cap_le_flowctl_init(chan);
5518
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005519 dcid = chan->scid;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005520 credits = chan->rx_credits;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005521
5522 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
5523
5524 chan->ident = cmd->ident;
5525
5526 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
5527 l2cap_state_change(chan, BT_CONNECT2);
Johan Hedberg434714d2014-09-01 09:45:03 +03005528 /* The following result value is actually not defined
5529 * for LE CoC but we use it to let the function know
5530 * that it should bail out after doing its cleanup
5531 * instead of sending a response.
5532 */
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005533 result = L2CAP_CR_PEND;
5534 chan->ops->defer(chan);
5535 } else {
5536 l2cap_chan_ready(chan);
Mallikarjun Phulari571f7392018-10-05 14:48:12 +05305537 result = L2CAP_CR_LE_SUCCESS;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005538 }
5539
5540response_unlock:
5541 l2cap_chan_unlock(pchan);
5542 mutex_unlock(&conn->chan_lock);
Johan Hedberga24cce12014-08-07 22:56:42 +03005543 l2cap_chan_put(pchan);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005544
5545 if (result == L2CAP_CR_PEND)
5546 return 0;
5547
5548response:
5549 if (chan) {
5550 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02005551 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005552 } else {
5553 rsp.mtu = 0;
5554 rsp.mps = 0;
5555 }
5556
5557 rsp.dcid = cpu_to_le16(dcid);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005558 rsp.credits = cpu_to_le16(credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005559 rsp.result = cpu_to_le16(result);
5560
5561 l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp);
5562
5563 return 0;
5564}
5565
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005566static inline int l2cap_le_credits(struct l2cap_conn *conn,
5567 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5568 u8 *data)
5569{
5570 struct l2cap_le_credits *pkt;
5571 struct l2cap_chan *chan;
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005572 u16 cid, credits, max_credits;
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005573
5574 if (cmd_len != sizeof(*pkt))
5575 return -EPROTO;
5576
5577 pkt = (struct l2cap_le_credits *) data;
5578 cid = __le16_to_cpu(pkt->cid);
5579 credits = __le16_to_cpu(pkt->credits);
5580
5581 BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits);
5582
5583 chan = l2cap_get_chan_by_dcid(conn, cid);
5584 if (!chan)
5585 return -EBADSLT;
5586
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005587 max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
5588 if (credits > max_credits) {
5589 BT_ERR("LE credits overflow");
5590 l2cap_send_disconn_req(chan, ECONNRESET);
Martin Townsendee930532014-10-13 19:24:45 +01005591 l2cap_chan_unlock(chan);
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005592
5593 /* Return 0 so that we don't trigger an unnecessary
5594 * command reject packet.
5595 */
5596 return 0;
5597 }
5598
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005599 chan->tx_credits += credits;
5600
Luiz Augusto von Dentz8a505b72017-04-11 22:21:01 +03005601 /* Resume sending */
5602 l2cap_le_flowctl_send(chan);
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005603
5604 if (chan->tx_credits)
5605 chan->ops->resume(chan);
5606
5607 l2cap_chan_unlock(chan);
5608
5609 return 0;
5610}
5611
Johan Hedberg71fb4192013-12-10 10:52:48 +02005612static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
5613 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5614 u8 *data)
5615{
5616 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
5617 struct l2cap_chan *chan;
5618
5619 if (cmd_len < sizeof(*rej))
5620 return -EPROTO;
5621
5622 mutex_lock(&conn->chan_lock);
5623
5624 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5625 if (!chan)
5626 goto done;
5627
5628 l2cap_chan_lock(chan);
5629 l2cap_chan_del(chan, ECONNREFUSED);
5630 l2cap_chan_unlock(chan);
5631
5632done:
5633 mutex_unlock(&conn->chan_lock);
5634 return 0;
5635}
5636
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005637static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
Johan Hedberg203e6392013-05-15 10:07:15 +03005638 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5639 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005640{
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005641 int err = 0;
5642
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005643 switch (cmd->code) {
5644 case L2CAP_COMMAND_REJ:
Johan Hedberg71fb4192013-12-10 10:52:48 +02005645 l2cap_le_command_rej(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005646 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005647
5648 case L2CAP_CONN_PARAM_UPDATE_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005649 err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data);
5650 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005651
5652 case L2CAP_CONN_PARAM_UPDATE_RSP:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005653 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005654
Johan Hedbergf1496de2013-05-13 14:15:56 +03005655 case L2CAP_LE_CONN_RSP:
5656 l2cap_le_connect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005657 break;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005658
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005659 case L2CAP_LE_CONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005660 err = l2cap_le_connect_req(conn, cmd, cmd_len, data);
5661 break;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005662
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005663 case L2CAP_LE_CREDITS:
5664 err = l2cap_le_credits(conn, cmd, cmd_len, data);
5665 break;
5666
Johan Hedberg3defe012013-05-15 10:16:06 +03005667 case L2CAP_DISCONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005668 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
5669 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005670
5671 case L2CAP_DISCONN_RSP:
5672 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005673 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005674
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005675 default:
5676 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005677 err = -EINVAL;
5678 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005679 }
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005680
5681 return err;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005682}
5683
Johan Hedbergc5623552013-04-29 19:35:33 +03005684static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
5685 struct sk_buff *skb)
5686{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005687 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005688 struct l2cap_cmd_hdr *cmd;
5689 u16 len;
Johan Hedbergc5623552013-04-29 19:35:33 +03005690 int err;
5691
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005692 if (hcon->type != LE_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005693 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005694
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005695 if (skb->len < L2CAP_CMD_HDR_SIZE)
5696 goto drop;
Johan Hedbergc5623552013-04-29 19:35:33 +03005697
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005698 cmd = (void *) skb->data;
5699 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
Johan Hedbergc5623552013-04-29 19:35:33 +03005700
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005701 len = le16_to_cpu(cmd->len);
Johan Hedbergc5623552013-04-29 19:35:33 +03005702
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005703 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident);
Johan Hedbergc5623552013-04-29 19:35:33 +03005704
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005705 if (len != skb->len || !cmd->ident) {
5706 BT_DBG("corrupted command");
5707 goto drop;
5708 }
Johan Hedbergc5623552013-04-29 19:35:33 +03005709
Johan Hedberg203e6392013-05-15 10:07:15 +03005710 err = l2cap_le_sig_cmd(conn, cmd, len, skb->data);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005711 if (err) {
5712 struct l2cap_cmd_rej_unk rej;
Johan Hedbergc5623552013-04-29 19:35:33 +03005713
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005714 BT_ERR("Wrong link type (%d)", err);
Johan Hedbergc5623552013-04-29 19:35:33 +03005715
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005716 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005717 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
5718 sizeof(rej), &rej);
Johan Hedbergc5623552013-04-29 19:35:33 +03005719 }
5720
Marcel Holtmann3b166292013-10-02 08:28:21 -07005721drop:
Johan Hedbergc5623552013-04-29 19:35:33 +03005722 kfree_skb(skb);
5723}
5724
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005725static inline void l2cap_sig_channel(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005726 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005728 struct hci_conn *hcon = conn->hcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 u8 *data = skb->data;
5730 int len = skb->len;
5731 struct l2cap_cmd_hdr cmd;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005732 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
5734 l2cap_raw_recv(conn, skb);
5735
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005736 if (hcon->type != ACL_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005737 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005738
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 while (len >= L2CAP_CMD_HDR_SIZE) {
Al Viro88219a02007-07-29 00:17:25 -07005740 u16 cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5742 data += L2CAP_CMD_HDR_SIZE;
5743 len -= L2CAP_CMD_HDR_SIZE;
5744
Al Viro88219a02007-07-29 00:17:25 -07005745 cmd_len = le16_to_cpu(cmd.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Gustavo Padovan2d792812012-10-06 10:07:01 +01005747 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len,
5748 cmd.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749
Al Viro88219a02007-07-29 00:17:25 -07005750 if (cmd_len > len || !cmd.ident) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 BT_DBG("corrupted command");
5752 break;
5753 }
5754
Johan Hedbergc5623552013-04-29 19:35:33 +03005755 err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 if (err) {
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03005757 struct l2cap_cmd_rej_unk rej;
Gustavo F. Padovan2c6d1a22011-03-23 14:38:32 -03005758
5759 BT_ERR("Wrong link type (%d)", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005761 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Gustavo Padovan2d792812012-10-06 10:07:01 +01005762 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5763 sizeof(rej), &rej);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
5765
Al Viro88219a02007-07-29 00:17:25 -07005766 data += cmd_len;
5767 len -= cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 }
5769
Marcel Holtmann3b166292013-10-02 08:28:21 -07005770drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 kfree_skb(skb);
5772}
5773
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005774static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005775{
5776 u16 our_fcs, rcv_fcs;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03005777 int hdr_size;
5778
5779 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
5780 hdr_size = L2CAP_EXT_HDR_SIZE;
5781 else
5782 hdr_size = L2CAP_ENH_HDR_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005783
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005784 if (chan->fcs == L2CAP_FCS_CRC16) {
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005785 skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005786 rcv_fcs = get_unaligned_le16(skb->data + skb->len);
5787 our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
5788
5789 if (our_fcs != rcv_fcs)
João Paulo Rechi Vita7a560e52010-06-22 13:56:27 -03005790 return -EBADMSG;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005791 }
5792 return 0;
5793}
5794
Mat Martineau6ea00482012-05-17 20:53:52 -07005795static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005796{
Mat Martineaue31f7632012-05-17 20:53:41 -07005797 struct l2cap_ctrl control;
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005798
Mat Martineaue31f7632012-05-17 20:53:41 -07005799 BT_DBG("chan %p", chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005800
Mat Martineaue31f7632012-05-17 20:53:41 -07005801 memset(&control, 0, sizeof(control));
5802 control.sframe = 1;
5803 control.final = 1;
5804 control.reqseq = chan->buffer_seq;
5805 set_bit(CONN_SEND_FBIT, &chan->conn_state);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005806
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005807 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
Mat Martineaue31f7632012-05-17 20:53:41 -07005808 control.super = L2CAP_SUPER_RNR;
5809 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005810 }
5811
Mat Martineaue31f7632012-05-17 20:53:41 -07005812 if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
5813 chan->unacked_frames > 0)
5814 __set_retrans_timer(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005815
Mat Martineaue31f7632012-05-17 20:53:41 -07005816 /* Send pending iframes */
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03005817 l2cap_ertm_send(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005818
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005819 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
Mat Martineaue31f7632012-05-17 20:53:41 -07005820 test_bit(CONN_SEND_FBIT, &chan->conn_state)) {
5821 /* F-bit wasn't sent in an s-frame or i-frame yet, so
5822 * send it now.
5823 */
5824 control.super = L2CAP_SUPER_RR;
5825 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005826 }
5827}
5828
Gustavo Padovan2d792812012-10-06 10:07:01 +01005829static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
5830 struct sk_buff **last_frag)
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005831{
Mat Martineau84084a32011-07-22 14:54:00 -07005832 /* skb->len reflects data in skb as well as all fragments
5833 * skb->data_len reflects only data in fragments
5834 */
5835 if (!skb_has_frag_list(skb))
5836 skb_shinfo(skb)->frag_list = new_frag;
5837
5838 new_frag->next = NULL;
5839
5840 (*last_frag)->next = new_frag;
5841 *last_frag = new_frag;
5842
5843 skb->len += new_frag->len;
5844 skb->data_len += new_frag->len;
5845 skb->truesize += new_frag->truesize;
5846}
5847
Mat Martineau4b51dae92012-05-17 20:53:37 -07005848static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
5849 struct l2cap_ctrl *control)
Mat Martineau84084a32011-07-22 14:54:00 -07005850{
5851 int err = -EINVAL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005852
Mat Martineau4b51dae92012-05-17 20:53:37 -07005853 switch (control->sar) {
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005854 case L2CAP_SAR_UNSEGMENTED:
Mat Martineau84084a32011-07-22 14:54:00 -07005855 if (chan->sdu)
5856 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005857
Gustavo Padovan80b98022012-05-27 22:27:51 -03005858 err = chan->ops->recv(chan, skb);
Mat Martineau84084a32011-07-22 14:54:00 -07005859 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005860
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005861 case L2CAP_SAR_START:
Mat Martineau84084a32011-07-22 14:54:00 -07005862 if (chan->sdu)
5863 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005864
Daniel Borkmanndbb50882016-07-27 11:40:14 -07005865 if (!pskb_may_pull(skb, L2CAP_SDULEN_SIZE))
5866 break;
5867
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005868 chan->sdu_len = get_unaligned_le16(skb->data);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005869 skb_pull(skb, L2CAP_SDULEN_SIZE);
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005870
Mat Martineau84084a32011-07-22 14:54:00 -07005871 if (chan->sdu_len > chan->imtu) {
5872 err = -EMSGSIZE;
5873 break;
5874 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005875
Mat Martineau84084a32011-07-22 14:54:00 -07005876 if (skb->len >= chan->sdu_len)
5877 break;
5878
5879 chan->sdu = skb;
5880 chan->sdu_last_frag = skb;
5881
5882 skb = NULL;
5883 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005884 break;
5885
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005886 case L2CAP_SAR_CONTINUE:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005887 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005888 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005889
Mat Martineau84084a32011-07-22 14:54:00 -07005890 append_skb_frag(chan->sdu, skb,
5891 &chan->sdu_last_frag);
5892 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005893
Mat Martineau84084a32011-07-22 14:54:00 -07005894 if (chan->sdu->len >= chan->sdu_len)
5895 break;
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005896
Mat Martineau84084a32011-07-22 14:54:00 -07005897 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005898 break;
5899
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005900 case L2CAP_SAR_END:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005901 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005902 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005903
Mat Martineau84084a32011-07-22 14:54:00 -07005904 append_skb_frag(chan->sdu, skb,
5905 &chan->sdu_last_frag);
5906 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005907
Mat Martineau84084a32011-07-22 14:54:00 -07005908 if (chan->sdu->len != chan->sdu_len)
5909 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005910
Gustavo Padovan80b98022012-05-27 22:27:51 -03005911 err = chan->ops->recv(chan, chan->sdu);
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005912
Mat Martineau84084a32011-07-22 14:54:00 -07005913 if (!err) {
5914 /* Reassembly complete */
5915 chan->sdu = NULL;
5916 chan->sdu_last_frag = NULL;
5917 chan->sdu_len = 0;
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005918 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005919 break;
5920 }
5921
Mat Martineau84084a32011-07-22 14:54:00 -07005922 if (err) {
5923 kfree_skb(skb);
5924 kfree_skb(chan->sdu);
5925 chan->sdu = NULL;
5926 chan->sdu_last_frag = NULL;
5927 chan->sdu_len = 0;
5928 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005929
Mat Martineau84084a32011-07-22 14:54:00 -07005930 return err;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005931}
5932
Mat Martineau32b32732012-10-23 15:24:11 -07005933static int l2cap_resegment(struct l2cap_chan *chan)
5934{
5935 /* Placeholder */
5936 return 0;
5937}
5938
Mat Martineaue3281402011-07-07 09:39:02 -07005939void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
Gustavo F. Padovan712132eb2010-06-21 19:39:50 -03005940{
Mat Martineau61aa4f52012-05-17 20:53:40 -07005941 u8 event;
5942
5943 if (chan->mode != L2CAP_MODE_ERTM)
5944 return;
5945
5946 event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR;
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03005947 l2cap_tx(chan, NULL, NULL, event);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005948}
5949
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005950static int l2cap_rx_queued_iframes(struct l2cap_chan *chan)
5951{
Mat Martineau63838722012-05-17 20:53:45 -07005952 int err = 0;
5953 /* Pass sequential frames to l2cap_reassemble_sdu()
5954 * until a gap is encountered.
5955 */
5956
5957 BT_DBG("chan %p", chan);
5958
5959 while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5960 struct sk_buff *skb;
5961 BT_DBG("Searching for skb with txseq %d (queue len %d)",
5962 chan->buffer_seq, skb_queue_len(&chan->srej_q));
5963
5964 skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq);
5965
5966 if (!skb)
5967 break;
5968
5969 skb_unlink(skb, &chan->srej_q);
5970 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
Johan Hedberga4368ff2015-03-30 23:21:01 +03005971 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->l2cap);
Mat Martineau63838722012-05-17 20:53:45 -07005972 if (err)
5973 break;
5974 }
5975
5976 if (skb_queue_empty(&chan->srej_q)) {
5977 chan->rx_state = L2CAP_RX_STATE_RECV;
5978 l2cap_send_ack(chan);
5979 }
5980
5981 return err;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005982}
5983
5984static void l2cap_handle_srej(struct l2cap_chan *chan,
5985 struct l2cap_ctrl *control)
5986{
Mat Martineauf80842a2012-05-17 20:53:46 -07005987 struct sk_buff *skb;
5988
5989 BT_DBG("chan %p, control %p", chan, control);
5990
5991 if (control->reqseq == chan->next_tx_seq) {
5992 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005993 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005994 return;
5995 }
5996
5997 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5998
5999 if (skb == NULL) {
6000 BT_DBG("Seq %d not available for retransmission",
6001 control->reqseq);
6002 return;
6003 }
6004
Johan Hedberga4368ff2015-03-30 23:21:01 +03006005 if (chan->max_tx != 0 && bt_cb(skb)->l2cap.retries >= chan->max_tx) {
Mat Martineauf80842a2012-05-17 20:53:46 -07006006 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006007 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07006008 return;
6009 }
6010
6011 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6012
6013 if (control->poll) {
6014 l2cap_pass_to_tx(chan, control);
6015
6016 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6017 l2cap_retransmit(chan, control);
6018 l2cap_ertm_send(chan);
6019
6020 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
6021 set_bit(CONN_SREJ_ACT, &chan->conn_state);
6022 chan->srej_save_reqseq = control->reqseq;
6023 }
6024 } else {
6025 l2cap_pass_to_tx_fbit(chan, control);
6026
6027 if (control->final) {
6028 if (chan->srej_save_reqseq != control->reqseq ||
6029 !test_and_clear_bit(CONN_SREJ_ACT,
6030 &chan->conn_state))
6031 l2cap_retransmit(chan, control);
6032 } else {
6033 l2cap_retransmit(chan, control);
6034 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
6035 set_bit(CONN_SREJ_ACT, &chan->conn_state);
6036 chan->srej_save_reqseq = control->reqseq;
6037 }
6038 }
6039 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006040}
6041
6042static void l2cap_handle_rej(struct l2cap_chan *chan,
6043 struct l2cap_ctrl *control)
6044{
Mat Martineaufcd289d2012-05-17 20:53:47 -07006045 struct sk_buff *skb;
6046
6047 BT_DBG("chan %p, control %p", chan, control);
6048
6049 if (control->reqseq == chan->next_tx_seq) {
6050 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006051 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07006052 return;
6053 }
6054
6055 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
6056
6057 if (chan->max_tx && skb &&
Johan Hedberga4368ff2015-03-30 23:21:01 +03006058 bt_cb(skb)->l2cap.retries >= chan->max_tx) {
Mat Martineaufcd289d2012-05-17 20:53:47 -07006059 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006060 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07006061 return;
6062 }
6063
6064 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6065
6066 l2cap_pass_to_tx(chan, control);
6067
6068 if (control->final) {
6069 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
6070 l2cap_retransmit_all(chan, control);
6071 } else {
6072 l2cap_retransmit_all(chan, control);
6073 l2cap_ertm_send(chan);
6074 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F)
6075 set_bit(CONN_REJ_ACT, &chan->conn_state);
6076 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006077}
6078
Mat Martineau4b51dae92012-05-17 20:53:37 -07006079static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq)
6080{
6081 BT_DBG("chan %p, txseq %d", chan, txseq);
6082
6083 BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq,
6084 chan->expected_tx_seq);
6085
6086 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
6087 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
Gustavo Padovan2d792812012-10-06 10:07:01 +01006088 chan->tx_win) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006089 /* See notes below regarding "double poll" and
6090 * invalid packets.
6091 */
6092 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6093 BT_DBG("Invalid/Ignore - after SREJ");
6094 return L2CAP_TXSEQ_INVALID_IGNORE;
6095 } else {
6096 BT_DBG("Invalid - in window after SREJ sent");
6097 return L2CAP_TXSEQ_INVALID;
6098 }
6099 }
6100
6101 if (chan->srej_list.head == txseq) {
6102 BT_DBG("Expected SREJ");
6103 return L2CAP_TXSEQ_EXPECTED_SREJ;
6104 }
6105
6106 if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) {
6107 BT_DBG("Duplicate SREJ - txseq already stored");
6108 return L2CAP_TXSEQ_DUPLICATE_SREJ;
6109 }
6110
6111 if (l2cap_seq_list_contains(&chan->srej_list, txseq)) {
6112 BT_DBG("Unexpected SREJ - not requested");
6113 return L2CAP_TXSEQ_UNEXPECTED_SREJ;
6114 }
6115 }
6116
6117 if (chan->expected_tx_seq == txseq) {
6118 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
6119 chan->tx_win) {
6120 BT_DBG("Invalid - txseq outside tx window");
6121 return L2CAP_TXSEQ_INVALID;
6122 } else {
6123 BT_DBG("Expected");
6124 return L2CAP_TXSEQ_EXPECTED;
6125 }
6126 }
6127
6128 if (__seq_offset(chan, txseq, chan->last_acked_seq) <
Gustavo Padovan2d792812012-10-06 10:07:01 +01006129 __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006130 BT_DBG("Duplicate - expected_tx_seq later than txseq");
6131 return L2CAP_TXSEQ_DUPLICATE;
6132 }
6133
6134 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) {
6135 /* A source of invalid packets is a "double poll" condition,
6136 * where delays cause us to send multiple poll packets. If
6137 * the remote stack receives and processes both polls,
6138 * sequence numbers can wrap around in such a way that a
6139 * resent frame has a sequence number that looks like new data
6140 * with a sequence gap. This would trigger an erroneous SREJ
6141 * request.
6142 *
6143 * Fortunately, this is impossible with a tx window that's
6144 * less than half of the maximum sequence number, which allows
6145 * invalid frames to be safely ignored.
6146 *
6147 * With tx window sizes greater than half of the tx window
6148 * maximum, the frame is invalid and cannot be ignored. This
6149 * causes a disconnect.
6150 */
6151
6152 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6153 BT_DBG("Invalid/Ignore - txseq outside tx window");
6154 return L2CAP_TXSEQ_INVALID_IGNORE;
6155 } else {
6156 BT_DBG("Invalid - txseq outside tx window");
6157 return L2CAP_TXSEQ_INVALID;
6158 }
6159 } else {
6160 BT_DBG("Unexpected - txseq indicates missing frames");
6161 return L2CAP_TXSEQ_UNEXPECTED;
6162 }
6163}
6164
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006165static int l2cap_rx_state_recv(struct l2cap_chan *chan,
6166 struct l2cap_ctrl *control,
6167 struct sk_buff *skb, u8 event)
6168{
6169 int err = 0;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006170 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006171
6172 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6173 event);
6174
6175 switch (event) {
6176 case L2CAP_EV_RECV_IFRAME:
6177 switch (l2cap_classify_txseq(chan, control->txseq)) {
6178 case L2CAP_TXSEQ_EXPECTED:
6179 l2cap_pass_to_tx(chan, control);
6180
6181 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6182 BT_DBG("Busy, discarding expected seq %d",
6183 control->txseq);
6184 break;
6185 }
6186
6187 chan->expected_tx_seq = __next_seq(chan,
6188 control->txseq);
6189
6190 chan->buffer_seq = chan->expected_tx_seq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006191 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006192
6193 err = l2cap_reassemble_sdu(chan, skb, control);
6194 if (err)
6195 break;
6196
6197 if (control->final) {
6198 if (!test_and_clear_bit(CONN_REJ_ACT,
6199 &chan->conn_state)) {
6200 control->final = 0;
6201 l2cap_retransmit_all(chan, control);
6202 l2cap_ertm_send(chan);
6203 }
6204 }
6205
6206 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
6207 l2cap_send_ack(chan);
6208 break;
6209 case L2CAP_TXSEQ_UNEXPECTED:
6210 l2cap_pass_to_tx(chan, control);
6211
6212 /* Can't issue SREJ frames in the local busy state.
6213 * Drop this frame, it will be seen as missing
6214 * when local busy is exited.
6215 */
6216 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6217 BT_DBG("Busy, discarding unexpected seq %d",
6218 control->txseq);
6219 break;
6220 }
6221
6222 /* There was a gap in the sequence, so an SREJ
6223 * must be sent for each missing frame. The
6224 * current frame is stored for later use.
6225 */
6226 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006227 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006228 BT_DBG("Queued %p (queue len %d)", skb,
6229 skb_queue_len(&chan->srej_q));
6230
6231 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
6232 l2cap_seq_list_clear(&chan->srej_list);
6233 l2cap_send_srej(chan, control->txseq);
6234
6235 chan->rx_state = L2CAP_RX_STATE_SREJ_SENT;
6236 break;
6237 case L2CAP_TXSEQ_DUPLICATE:
6238 l2cap_pass_to_tx(chan, control);
6239 break;
6240 case L2CAP_TXSEQ_INVALID_IGNORE:
6241 break;
6242 case L2CAP_TXSEQ_INVALID:
6243 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006244 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006245 break;
6246 }
6247 break;
6248 case L2CAP_EV_RECV_RR:
6249 l2cap_pass_to_tx(chan, control);
6250 if (control->final) {
6251 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6252
Mat Martineaue6a3ee62012-10-23 15:24:22 -07006253 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) &&
6254 !__chan_is_moving(chan)) {
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006255 control->final = 0;
6256 l2cap_retransmit_all(chan, control);
6257 }
6258
6259 l2cap_ertm_send(chan);
6260 } else if (control->poll) {
6261 l2cap_send_i_or_rr_or_rnr(chan);
6262 } else {
6263 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6264 &chan->conn_state) &&
6265 chan->unacked_frames)
6266 __set_retrans_timer(chan);
6267
6268 l2cap_ertm_send(chan);
6269 }
6270 break;
6271 case L2CAP_EV_RECV_RNR:
6272 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6273 l2cap_pass_to_tx(chan, control);
6274 if (control && control->poll) {
6275 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6276 l2cap_send_rr_or_rnr(chan, 0);
6277 }
6278 __clear_retrans_timer(chan);
6279 l2cap_seq_list_clear(&chan->retrans_list);
6280 break;
6281 case L2CAP_EV_RECV_REJ:
6282 l2cap_handle_rej(chan, control);
6283 break;
6284 case L2CAP_EV_RECV_SREJ:
6285 l2cap_handle_srej(chan, control);
6286 break;
6287 default:
6288 break;
6289 }
6290
6291 if (skb && !skb_in_use) {
6292 BT_DBG("Freeing %p", skb);
6293 kfree_skb(skb);
6294 }
6295
6296 return err;
6297}
6298
6299static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
6300 struct l2cap_ctrl *control,
6301 struct sk_buff *skb, u8 event)
6302{
6303 int err = 0;
6304 u16 txseq = control->txseq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006305 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006306
6307 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6308 event);
6309
6310 switch (event) {
6311 case L2CAP_EV_RECV_IFRAME:
6312 switch (l2cap_classify_txseq(chan, txseq)) {
6313 case L2CAP_TXSEQ_EXPECTED:
6314 /* Keep frame for reassembly later */
6315 l2cap_pass_to_tx(chan, control);
6316 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006317 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006318 BT_DBG("Queued %p (queue len %d)", skb,
6319 skb_queue_len(&chan->srej_q));
6320
6321 chan->expected_tx_seq = __next_seq(chan, txseq);
6322 break;
6323 case L2CAP_TXSEQ_EXPECTED_SREJ:
6324 l2cap_seq_list_pop(&chan->srej_list);
6325
6326 l2cap_pass_to_tx(chan, control);
6327 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006328 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006329 BT_DBG("Queued %p (queue len %d)", skb,
6330 skb_queue_len(&chan->srej_q));
6331
6332 err = l2cap_rx_queued_iframes(chan);
6333 if (err)
6334 break;
6335
6336 break;
6337 case L2CAP_TXSEQ_UNEXPECTED:
6338 /* Got a frame that can't be reassembled yet.
6339 * Save it for later, and send SREJs to cover
6340 * the missing frames.
6341 */
6342 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006343 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006344 BT_DBG("Queued %p (queue len %d)", skb,
6345 skb_queue_len(&chan->srej_q));
6346
6347 l2cap_pass_to_tx(chan, control);
6348 l2cap_send_srej(chan, control->txseq);
6349 break;
6350 case L2CAP_TXSEQ_UNEXPECTED_SREJ:
6351 /* This frame was requested with an SREJ, but
6352 * some expected retransmitted frames are
6353 * missing. Request retransmission of missing
6354 * SREJ'd frames.
6355 */
6356 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006357 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006358 BT_DBG("Queued %p (queue len %d)", skb,
6359 skb_queue_len(&chan->srej_q));
6360
6361 l2cap_pass_to_tx(chan, control);
6362 l2cap_send_srej_list(chan, control->txseq);
6363 break;
6364 case L2CAP_TXSEQ_DUPLICATE_SREJ:
6365 /* We've already queued this frame. Drop this copy. */
6366 l2cap_pass_to_tx(chan, control);
6367 break;
6368 case L2CAP_TXSEQ_DUPLICATE:
6369 /* Expecting a later sequence number, so this frame
6370 * was already received. Ignore it completely.
6371 */
6372 break;
6373 case L2CAP_TXSEQ_INVALID_IGNORE:
6374 break;
6375 case L2CAP_TXSEQ_INVALID:
6376 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006377 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006378 break;
6379 }
6380 break;
6381 case L2CAP_EV_RECV_RR:
6382 l2cap_pass_to_tx(chan, control);
6383 if (control->final) {
6384 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6385
6386 if (!test_and_clear_bit(CONN_REJ_ACT,
6387 &chan->conn_state)) {
6388 control->final = 0;
6389 l2cap_retransmit_all(chan, control);
6390 }
6391
6392 l2cap_ertm_send(chan);
6393 } else if (control->poll) {
6394 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6395 &chan->conn_state) &&
6396 chan->unacked_frames) {
6397 __set_retrans_timer(chan);
6398 }
6399
6400 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6401 l2cap_send_srej_tail(chan);
6402 } else {
6403 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6404 &chan->conn_state) &&
6405 chan->unacked_frames)
6406 __set_retrans_timer(chan);
6407
6408 l2cap_send_ack(chan);
6409 }
6410 break;
6411 case L2CAP_EV_RECV_RNR:
6412 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6413 l2cap_pass_to_tx(chan, control);
6414 if (control->poll) {
6415 l2cap_send_srej_tail(chan);
6416 } else {
6417 struct l2cap_ctrl rr_control;
6418 memset(&rr_control, 0, sizeof(rr_control));
6419 rr_control.sframe = 1;
6420 rr_control.super = L2CAP_SUPER_RR;
6421 rr_control.reqseq = chan->buffer_seq;
6422 l2cap_send_sframe(chan, &rr_control);
6423 }
6424
6425 break;
6426 case L2CAP_EV_RECV_REJ:
6427 l2cap_handle_rej(chan, control);
6428 break;
6429 case L2CAP_EV_RECV_SREJ:
6430 l2cap_handle_srej(chan, control);
6431 break;
6432 }
6433
6434 if (skb && !skb_in_use) {
6435 BT_DBG("Freeing %p", skb);
6436 kfree_skb(skb);
6437 }
6438
6439 return err;
6440}
6441
Mat Martineau32b32732012-10-23 15:24:11 -07006442static int l2cap_finish_move(struct l2cap_chan *chan)
6443{
6444 BT_DBG("chan %p", chan);
6445
6446 chan->rx_state = L2CAP_RX_STATE_RECV;
6447
6448 if (chan->hs_hcon)
6449 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6450 else
6451 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6452
6453 return l2cap_resegment(chan);
6454}
6455
6456static int l2cap_rx_state_wait_p(struct l2cap_chan *chan,
6457 struct l2cap_ctrl *control,
6458 struct sk_buff *skb, u8 event)
6459{
6460 int err;
6461
6462 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6463 event);
6464
6465 if (!control->poll)
6466 return -EPROTO;
6467
6468 l2cap_process_reqseq(chan, control->reqseq);
6469
6470 if (!skb_queue_empty(&chan->tx_q))
6471 chan->tx_send_head = skb_peek(&chan->tx_q);
6472 else
6473 chan->tx_send_head = NULL;
6474
6475 /* Rewind next_tx_seq to the point expected
6476 * by the receiver.
6477 */
6478 chan->next_tx_seq = control->reqseq;
6479 chan->unacked_frames = 0;
6480
6481 err = l2cap_finish_move(chan);
6482 if (err)
6483 return err;
6484
6485 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6486 l2cap_send_i_or_rr_or_rnr(chan);
6487
6488 if (event == L2CAP_EV_RECV_IFRAME)
6489 return -EPROTO;
6490
6491 return l2cap_rx_state_recv(chan, control, NULL, event);
6492}
6493
6494static int l2cap_rx_state_wait_f(struct l2cap_chan *chan,
6495 struct l2cap_ctrl *control,
6496 struct sk_buff *skb, u8 event)
6497{
6498 int err;
6499
6500 if (!control->final)
6501 return -EPROTO;
6502
6503 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6504
6505 chan->rx_state = L2CAP_RX_STATE_RECV;
6506 l2cap_process_reqseq(chan, control->reqseq);
6507
6508 if (!skb_queue_empty(&chan->tx_q))
6509 chan->tx_send_head = skb_peek(&chan->tx_q);
6510 else
6511 chan->tx_send_head = NULL;
6512
6513 /* Rewind next_tx_seq to the point expected
6514 * by the receiver.
6515 */
6516 chan->next_tx_seq = control->reqseq;
6517 chan->unacked_frames = 0;
6518
6519 if (chan->hs_hcon)
6520 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6521 else
6522 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6523
6524 err = l2cap_resegment(chan);
6525
6526 if (!err)
6527 err = l2cap_rx_state_recv(chan, control, skb, event);
6528
6529 return err;
6530}
6531
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006532static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq)
6533{
6534 /* Make sure reqseq is for a packet that has been sent but not acked */
6535 u16 unacked;
6536
6537 unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq);
6538 return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked;
6539}
6540
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006541static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6542 struct sk_buff *skb, u8 event)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006543{
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006544 int err = 0;
6545
6546 BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan,
6547 control, skb, event, chan->rx_state);
6548
6549 if (__valid_reqseq(chan, control->reqseq)) {
6550 switch (chan->rx_state) {
6551 case L2CAP_RX_STATE_RECV:
6552 err = l2cap_rx_state_recv(chan, control, skb, event);
6553 break;
6554 case L2CAP_RX_STATE_SREJ_SENT:
6555 err = l2cap_rx_state_srej_sent(chan, control, skb,
6556 event);
6557 break;
Mat Martineau32b32732012-10-23 15:24:11 -07006558 case L2CAP_RX_STATE_WAIT_P:
6559 err = l2cap_rx_state_wait_p(chan, control, skb, event);
6560 break;
6561 case L2CAP_RX_STATE_WAIT_F:
6562 err = l2cap_rx_state_wait_f(chan, control, skb, event);
6563 break;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006564 default:
6565 /* shut it down */
6566 break;
6567 }
6568 } else {
6569 BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
6570 control->reqseq, chan->next_tx_seq,
6571 chan->expected_ack_seq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006572 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006573 }
6574
6575 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006576}
6577
6578static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6579 struct sk_buff *skb)
6580{
Mat Martineau4b51dae92012-05-17 20:53:37 -07006581 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
6582 chan->rx_state);
6583
6584 if (l2cap_classify_txseq(chan, control->txseq) ==
6585 L2CAP_TXSEQ_EXPECTED) {
6586 l2cap_pass_to_tx(chan, control);
6587
6588 BT_DBG("buffer_seq %d->%d", chan->buffer_seq,
6589 __next_seq(chan, chan->buffer_seq));
6590
6591 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
6592
6593 l2cap_reassemble_sdu(chan, skb, control);
6594 } else {
6595 if (chan->sdu) {
6596 kfree_skb(chan->sdu);
6597 chan->sdu = NULL;
6598 }
6599 chan->sdu_last_frag = NULL;
6600 chan->sdu_len = 0;
6601
6602 if (skb) {
6603 BT_DBG("Freeing %p", skb);
6604 kfree_skb(skb);
6605 }
6606 }
6607
6608 chan->last_acked_seq = control->txseq;
6609 chan->expected_tx_seq = __next_seq(chan, control->txseq);
6610
Prasanna Karthik9a544212015-11-19 12:05:35 +00006611 return 0;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006612}
6613
6614static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6615{
Johan Hedberga4368ff2015-03-30 23:21:01 +03006616 struct l2cap_ctrl *control = &bt_cb(skb)->l2cap;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006617 u16 len;
6618 u8 event;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006619
Mat Martineaub76bbd62012-04-11 10:48:43 -07006620 __unpack_control(chan, skb);
6621
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006622 len = skb->len;
6623
6624 /*
6625 * We can just drop the corrupted I-frame here.
6626 * Receiver will miss it and start proper recovery
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006627 * procedures and ask for retransmission.
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006628 */
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006629 if (l2cap_check_fcs(chan, skb))
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006630 goto drop;
6631
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006632 if (!control->sframe && control->sar == L2CAP_SAR_START)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006633 len -= L2CAP_SDULEN_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006634
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006635 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006636 len -= L2CAP_FCS_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006637
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006638 if (len > chan->mps) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006639 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006640 goto drop;
6641 }
6642
Daniel Borkmanndbb50882016-07-27 11:40:14 -07006643 if ((chan->mode == L2CAP_MODE_ERTM ||
6644 chan->mode == L2CAP_MODE_STREAMING) && sk_filter(chan->data, skb))
6645 goto drop;
6646
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006647 if (!control->sframe) {
6648 int err;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006649
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006650 BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
6651 control->sar, control->reqseq, control->final,
6652 control->txseq);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03006653
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006654 /* Validate F-bit - F=0 always valid, F=1 only
6655 * valid in TX WAIT_F
6656 */
6657 if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006658 goto drop;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006659
6660 if (chan->mode != L2CAP_MODE_STREAMING) {
6661 event = L2CAP_EV_RECV_IFRAME;
6662 err = l2cap_rx(chan, control, skb, event);
6663 } else {
6664 err = l2cap_stream_rx(chan, control, skb);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006665 }
6666
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006667 if (err)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006668 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006669 } else {
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006670 const u8 rx_func_to_event[4] = {
6671 L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ,
6672 L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ
6673 };
6674
6675 /* Only I-frames are expected in streaming mode */
6676 if (chan->mode == L2CAP_MODE_STREAMING)
6677 goto drop;
6678
6679 BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
6680 control->reqseq, control->final, control->poll,
6681 control->super);
6682
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006683 if (len != 0) {
Andrei Emeltchenko1bb166e2012-11-20 17:16:21 +02006684 BT_ERR("Trailing bytes: %d in sframe", len);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006685 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006686 goto drop;
6687 }
6688
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006689 /* Validate F and P bits */
6690 if (control->final && (control->poll ||
6691 chan->tx_state != L2CAP_TX_STATE_WAIT_F))
6692 goto drop;
6693
6694 event = rx_func_to_event[control->super];
6695 if (l2cap_rx(chan, control, skb, event))
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006696 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006697 }
6698
6699 return 0;
6700
6701drop:
6702 kfree_skb(skb);
6703 return 0;
6704}
6705
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006706static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
6707{
6708 struct l2cap_conn *conn = chan->conn;
6709 struct l2cap_le_credits pkt;
6710 u16 return_credits;
6711
Luiz Augusto von Dentz96cd8ea2018-09-04 13:39:21 +03006712 return_credits = ((chan->imtu / chan->mps) + 1) - chan->rx_credits;
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006713
Luiz Augusto von Dentz96cd8ea2018-09-04 13:39:21 +03006714 if (!return_credits)
6715 return;
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006716
6717 BT_DBG("chan %p returning %u credits to sender", chan, return_credits);
6718
6719 chan->rx_credits += return_credits;
6720
6721 pkt.cid = cpu_to_le16(chan->scid);
6722 pkt.credits = cpu_to_le16(return_credits);
6723
6724 chan->ident = l2cap_get_ident(conn);
6725
6726 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
6727}
6728
Luiz Augusto von Dentz96cd8ea2018-09-04 13:39:21 +03006729static int l2cap_le_recv(struct l2cap_chan *chan, struct sk_buff *skb)
6730{
6731 int err;
6732
6733 BT_DBG("SDU reassemble complete: chan %p skb->len %u", chan, skb->len);
6734
6735 /* Wait recv to confirm reception before updating the credits */
6736 err = chan->ops->recv(chan, skb);
6737
6738 /* Update credits whenever an SDU is received */
6739 l2cap_chan_le_send_credits(chan);
6740
6741 return err;
6742}
6743
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006744static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6745{
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006746 int err;
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006747
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006748 if (!chan->rx_credits) {
6749 BT_ERR("No credits to receive LE L2CAP data");
Johan Hedbergdfd97742014-01-27 15:11:34 -08006750 l2cap_send_disconn_req(chan, ECONNRESET);
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006751 return -ENOBUFS;
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006752 }
6753
6754 if (chan->imtu < skb->len) {
6755 BT_ERR("Too big LE L2CAP PDU");
6756 return -ENOBUFS;
6757 }
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006758
6759 chan->rx_credits--;
6760 BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
6761
Luiz Augusto von Dentz96cd8ea2018-09-04 13:39:21 +03006762 /* Update if remote had run out of credits, this should only happens
6763 * if the remote is not using the entire MPS.
6764 */
6765 if (!chan->rx_credits)
6766 l2cap_chan_le_send_credits(chan);
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006767
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006768 err = 0;
6769
6770 if (!chan->sdu) {
6771 u16 sdu_len;
6772
6773 sdu_len = get_unaligned_le16(skb->data);
6774 skb_pull(skb, L2CAP_SDULEN_SIZE);
6775
6776 BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u",
6777 sdu_len, skb->len, chan->imtu);
6778
6779 if (sdu_len > chan->imtu) {
6780 BT_ERR("Too big LE L2CAP SDU length received");
6781 err = -EMSGSIZE;
6782 goto failed;
6783 }
6784
6785 if (skb->len > sdu_len) {
6786 BT_ERR("Too much LE L2CAP data received");
6787 err = -EINVAL;
6788 goto failed;
6789 }
6790
6791 if (skb->len == sdu_len)
Luiz Augusto von Dentz96cd8ea2018-09-04 13:39:21 +03006792 return l2cap_le_recv(chan, skb);
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006793
6794 chan->sdu = skb;
6795 chan->sdu_len = sdu_len;
6796 chan->sdu_last_frag = skb;
6797
Luiz Augusto von Dentza5c30212018-09-04 13:39:22 +03006798 /* Detect if remote is not able to use the selected MPS */
6799 if (skb->len + L2CAP_SDULEN_SIZE < chan->mps) {
6800 u16 mps_len = skb->len + L2CAP_SDULEN_SIZE;
6801
6802 /* Adjust the number of credits */
6803 BT_DBG("chan->mps %u -> %u", chan->mps, mps_len);
6804 chan->mps = mps_len;
6805 l2cap_chan_le_send_credits(chan);
6806 }
6807
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006808 return 0;
6809 }
6810
6811 BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u",
6812 chan->sdu->len, skb->len, chan->sdu_len);
6813
6814 if (chan->sdu->len + skb->len > chan->sdu_len) {
6815 BT_ERR("Too much LE L2CAP data received");
6816 err = -EINVAL;
6817 goto failed;
6818 }
6819
6820 append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag);
6821 skb = NULL;
6822
6823 if (chan->sdu->len == chan->sdu_len) {
Luiz Augusto von Dentz96cd8ea2018-09-04 13:39:21 +03006824 err = l2cap_le_recv(chan, chan->sdu);
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006825 if (!err) {
6826 chan->sdu = NULL;
6827 chan->sdu_last_frag = NULL;
6828 chan->sdu_len = 0;
6829 }
6830 }
6831
6832failed:
6833 if (err) {
6834 kfree_skb(skb);
6835 kfree_skb(chan->sdu);
6836 chan->sdu = NULL;
6837 chan->sdu_last_frag = NULL;
6838 chan->sdu_len = 0;
6839 }
6840
6841 /* We can't return an error here since we took care of the skb
6842 * freeing internally. An error return would cause the caller to
6843 * do a double-free of the skb.
6844 */
6845 return 0;
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006846}
6847
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006848static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
6849 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006851 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03006853 chan = l2cap_get_chan_by_scid(conn, cid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006854 if (!chan) {
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006855 if (cid == L2CAP_CID_A2MP) {
6856 chan = a2mp_channel_create(conn, skb);
6857 if (!chan) {
6858 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006859 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006860 }
6861
6862 l2cap_chan_lock(chan);
6863 } else {
6864 BT_DBG("unknown cid 0x%4.4x", cid);
6865 /* Drop packet and return */
6866 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006867 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 }
6870
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03006871 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
Johan Hedberg315917e2015-02-16 11:42:11 +02006873 /* If we receive data on a fixed channel before the info req/rsp
6874 * procdure is done simply assume that the channel is supported
6875 * and mark it as ready.
6876 */
6877 if (chan->chan_type == L2CAP_CHAN_FIXED)
6878 l2cap_chan_ready(chan);
6879
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03006880 if (chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 goto drop;
6882
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006883 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03006884 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006885 if (l2cap_le_data_rcv(chan, skb) < 0)
6886 goto drop;
6887
6888 goto done;
6889
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006890 case L2CAP_MODE_BASIC:
6891 /* If socket recv buffers overflows we drop data here
6892 * which is *bad* because L2CAP has to be reliable.
6893 * But we don't have any other choice. L2CAP doesn't
6894 * provide flow control mechanism. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
Szymon Janc2c96e032014-02-18 20:48:34 +01006896 if (chan->imtu < skb->len) {
6897 BT_ERR("Dropping L2CAP data: receive buffer overflow");
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006898 goto drop;
Szymon Janc2c96e032014-02-18 20:48:34 +01006899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900
Gustavo Padovan80b98022012-05-27 22:27:51 -03006901 if (!chan->ops->recv(chan, skb))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006902 goto done;
6903 break;
6904
6905 case L2CAP_MODE_ERTM:
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006906 case L2CAP_MODE_STREAMING:
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006907 l2cap_data_rcv(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006908 goto done;
6909
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006910 default:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006911 BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006912 break;
6913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
6915drop:
6916 kfree_skb(skb);
6917
6918done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006919 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920}
6921
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006922static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6923 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924{
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006925 struct hci_conn *hcon = conn->hcon;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006926 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006928 if (hcon->type != ACL_LINK)
Johan Hedberga24cce12014-08-07 22:56:42 +03006929 goto free_skb;
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006930
Johan Hedbergbf20fd42013-05-14 13:23:13 +03006931 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst,
6932 ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006933 if (!chan)
Johan Hedberga24cce12014-08-07 22:56:42 +03006934 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006936 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03006938 if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 goto drop;
6940
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006941 if (chan->imtu < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 goto drop;
6943
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006944 /* Store remote BD_ADDR and PSM for msg_name */
Johan Hedberga4368ff2015-03-30 23:21:01 +03006945 bacpy(&bt_cb(skb)->l2cap.bdaddr, &hcon->dst);
6946 bt_cb(skb)->l2cap.psm = psm;
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006947
Johan Hedberga24cce12014-08-07 22:56:42 +03006948 if (!chan->ops->recv(chan, skb)) {
6949 l2cap_chan_put(chan);
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006950 return;
Johan Hedberga24cce12014-08-07 22:56:42 +03006951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952
6953drop:
Johan Hedberga24cce12014-08-07 22:56:42 +03006954 l2cap_chan_put(chan);
6955free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957}
6958
6959static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6960{
6961 struct l2cap_hdr *lh = (void *) skb->data;
Johan Hedberg61a939c2014-01-17 20:45:11 +02006962 struct hci_conn *hcon = conn->hcon;
Al Viro8e036fc2007-07-29 00:16:36 -07006963 u16 cid, len;
6964 __le16 psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
Johan Hedberg61a939c2014-01-17 20:45:11 +02006966 if (hcon->state != BT_CONNECTED) {
6967 BT_DBG("queueing pending rx skb");
6968 skb_queue_tail(&conn->pending_rx, skb);
6969 return;
6970 }
6971
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 skb_pull(skb, L2CAP_HDR_SIZE);
6973 cid = __le16_to_cpu(lh->cid);
6974 len = __le16_to_cpu(lh->len);
6975
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006976 if (len != skb->len) {
6977 kfree_skb(skb);
6978 return;
6979 }
6980
Johan Hedberg9e1d7e12014-07-06 11:03:36 +03006981 /* Since we can't actively block incoming LE connections we must
6982 * at least ensure that we ignore incoming data from them.
6983 */
6984 if (hcon->type == LE_LINK &&
Johan Hedbergdcc36c12014-07-09 12:59:13 +03006985 hci_bdaddr_list_lookup(&hcon->hdev->blacklist, &hcon->dst,
Johan Hedberga250e042015-01-15 13:06:44 +02006986 bdaddr_dst_type(hcon))) {
Johan Hedberge4931502014-07-02 09:36:21 +03006987 kfree_skb(skb);
6988 return;
6989 }
6990
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 BT_DBG("len %d, cid 0x%4.4x", len, cid);
6992
6993 switch (cid) {
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006994 case L2CAP_CID_SIGNALING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 l2cap_sig_channel(conn, skb);
6996 break;
6997
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006998 case L2CAP_CID_CONN_LESS:
Andrei Emeltchenko097db762012-03-09 14:16:17 +02006999 psm = get_unaligned((__le16 *) skb->data);
Andrei Emeltchenko0181a702012-05-29 10:04:05 +03007000 skb_pull(skb, L2CAP_PSMLEN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 l2cap_conless_channel(conn, psm, skb);
7002 break;
7003
Marcel Holtmanna2877622013-10-02 23:46:54 -07007004 case L2CAP_CID_LE_SIGNALING:
7005 l2cap_le_sig_channel(conn, skb);
7006 break;
7007
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 default:
7009 l2cap_data_channel(conn, cid, skb);
7010 break;
7011 }
7012}
7013
Johan Hedberg61a939c2014-01-17 20:45:11 +02007014static void process_pending_rx(struct work_struct *work)
7015{
7016 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
7017 pending_rx_work);
7018 struct sk_buff *skb;
7019
7020 BT_DBG("");
7021
7022 while ((skb = skb_dequeue(&conn->pending_rx)))
7023 l2cap_recv_frame(conn, skb);
7024}
7025
Johan Hedberg162b49e2014-01-17 20:45:10 +02007026static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
7027{
7028 struct l2cap_conn *conn = hcon->l2cap_data;
7029 struct hci_chan *hchan;
7030
7031 if (conn)
7032 return conn;
7033
7034 hchan = hci_chan_create(hcon);
7035 if (!hchan)
7036 return NULL;
7037
Johan Hedberg27f70f32014-07-21 10:50:06 +03007038 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007039 if (!conn) {
7040 hci_chan_del(hchan);
7041 return NULL;
7042 }
7043
7044 kref_init(&conn->ref);
7045 hcon->l2cap_data = conn;
Johan Hedberg51bb84572014-08-15 21:06:57 +03007046 conn->hcon = hci_conn_get(hcon);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007047 conn->hchan = hchan;
7048
7049 BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
7050
7051 switch (hcon->type) {
7052 case LE_LINK:
7053 if (hcon->hdev->le_mtu) {
7054 conn->mtu = hcon->hdev->le_mtu;
7055 break;
7056 }
7057 /* fall through */
7058 default:
7059 conn->mtu = hcon->hdev->acl_mtu;
7060 break;
7061 }
7062
7063 conn->feat_mask = 0;
7064
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02007065 conn->local_fixed_chan = L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS;
7066
7067 if (hcon->type == ACL_LINK &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007068 hci_dev_test_flag(hcon->hdev, HCI_HS_ENABLED))
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02007069 conn->local_fixed_chan |= L2CAP_FC_A2MP;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007070
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007071 if (hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED) &&
Marcel Holtmannf9be9e82014-12-06 00:35:45 +01007072 (bredr_sc_enabled(hcon->hdev) ||
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07007073 hci_dev_test_flag(hcon->hdev, HCI_FORCE_BREDR_SMP)))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03007074 conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR;
7075
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +02007076 mutex_init(&conn->ident_lock);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007077 mutex_init(&conn->chan_lock);
7078
7079 INIT_LIST_HEAD(&conn->chan_l);
7080 INIT_LIST_HEAD(&conn->users);
7081
Johan Hedberg276d8072014-08-11 22:06:41 +03007082 INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007083
Johan Hedberg61a939c2014-01-17 20:45:11 +02007084 skb_queue_head_init(&conn->pending_rx);
7085 INIT_WORK(&conn->pending_rx_work, process_pending_rx);
Johan Hedbergf3d82d02014-09-05 22:19:50 +03007086 INIT_WORK(&conn->id_addr_update_work, l2cap_conn_update_id_addr);
Johan Hedberg61a939c2014-01-17 20:45:11 +02007087
Johan Hedberg162b49e2014-01-17 20:45:10 +02007088 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
7089
7090 return conn;
7091}
7092
7093static bool is_valid_psm(u16 psm, u8 dst_type) {
7094 if (!psm)
7095 return false;
7096
7097 if (bdaddr_type_is_le(dst_type))
7098 return (psm <= 0x00ff);
7099
7100 /* PSM must be odd and lsb of upper byte must be 0 */
7101 return ((psm & 0x0101) == 0x0001);
7102}
7103
7104int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
7105 bdaddr_t *dst, u8 dst_type)
7106{
7107 struct l2cap_conn *conn;
7108 struct hci_conn *hcon;
7109 struct hci_dev *hdev;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007110 int err;
7111
7112 BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst,
7113 dst_type, __le16_to_cpu(psm));
7114
Johan Hedberg39385cb2016-11-12 17:03:07 +02007115 hdev = hci_get_route(dst, &chan->src, chan->src_type);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007116 if (!hdev)
7117 return -EHOSTUNREACH;
7118
7119 hci_dev_lock(hdev);
7120
Johan Hedberg162b49e2014-01-17 20:45:10 +02007121 if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid &&
7122 chan->chan_type != L2CAP_CHAN_RAW) {
7123 err = -EINVAL;
7124 goto done;
7125 }
7126
Johan Hedberg21626e62014-01-24 10:35:41 +02007127 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) {
7128 err = -EINVAL;
7129 goto done;
7130 }
7131
7132 if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) {
Johan Hedberg162b49e2014-01-17 20:45:10 +02007133 err = -EINVAL;
7134 goto done;
7135 }
7136
7137 switch (chan->mode) {
7138 case L2CAP_MODE_BASIC:
7139 break;
7140 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg162b49e2014-01-17 20:45:10 +02007141 break;
7142 case L2CAP_MODE_ERTM:
7143 case L2CAP_MODE_STREAMING:
7144 if (!disable_ertm)
7145 break;
7146 /* fall through */
7147 default:
Johan Hedbergbeb19e42014-07-18 11:15:26 +03007148 err = -EOPNOTSUPP;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007149 goto done;
7150 }
7151
7152 switch (chan->state) {
7153 case BT_CONNECT:
7154 case BT_CONNECT2:
7155 case BT_CONFIG:
7156 /* Already connecting */
7157 err = 0;
7158 goto done;
7159
7160 case BT_CONNECTED:
7161 /* Already connected */
7162 err = -EISCONN;
7163 goto done;
7164
7165 case BT_OPEN:
7166 case BT_BOUND:
7167 /* Can connect */
7168 break;
7169
7170 default:
7171 err = -EBADFD;
7172 goto done;
7173 }
7174
7175 /* Set destination address and psm */
7176 bacpy(&chan->dst, dst);
7177 chan->dst_type = dst_type;
7178
7179 chan->psm = psm;
7180 chan->dcid = cid;
7181
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007182 if (bdaddr_type_is_le(dst_type)) {
7183 /* Convert from L2CAP channel address type to HCI address type
7184 */
7185 if (dst_type == BDADDR_LE_PUBLIC)
7186 dst_type = ADDR_LE_DEV_PUBLIC;
7187 else
7188 dst_type = ADDR_LE_DEV_RANDOM;
7189
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007190 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedberg0ad06aa2015-11-11 14:44:57 +02007191 hcon = hci_connect_le(hdev, dst, dst_type,
7192 chan->sec_level,
7193 HCI_LE_CONN_TIMEOUT,
Szymon Janc082f2302018-04-03 13:40:06 +02007194 HCI_ROLE_SLAVE, NULL);
Johan Hedberge804d252014-07-16 11:42:28 +03007195 else
Johan Hedberg0ad06aa2015-11-11 14:44:57 +02007196 hcon = hci_connect_le_scan(hdev, dst, dst_type,
7197 chan->sec_level,
7198 HCI_LE_CONN_TIMEOUT);
Johan Hedbergcdd62752014-07-07 15:02:28 +03007199
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007200 } else {
Johan Hedbergd93375a2014-07-07 15:02:27 +03007201 u8 auth_type = l2cap_get_auth_type(chan);
Andre Guedes04a6c582014-02-26 20:21:44 -03007202 hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007203 }
Johan Hedberg162b49e2014-01-17 20:45:10 +02007204
7205 if (IS_ERR(hcon)) {
7206 err = PTR_ERR(hcon);
7207 goto done;
7208 }
7209
7210 conn = l2cap_conn_add(hcon);
7211 if (!conn) {
7212 hci_conn_drop(hcon);
7213 err = -ENOMEM;
7214 goto done;
7215 }
7216
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007217 mutex_lock(&conn->chan_lock);
7218 l2cap_chan_lock(chan);
7219
Johan Hedberg162b49e2014-01-17 20:45:10 +02007220 if (cid && __l2cap_get_chan_by_dcid(conn, cid)) {
7221 hci_conn_drop(hcon);
7222 err = -EBUSY;
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007223 goto chan_unlock;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007224 }
7225
7226 /* Update source addr of the socket */
7227 bacpy(&chan->src, &hcon->src);
Johan Hedberga250e042015-01-15 13:06:44 +02007228 chan->src_type = bdaddr_src_type(hcon);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007229
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007230 __l2cap_chan_add(conn, chan);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007231
7232 /* l2cap_chan_add takes its own ref so we can drop this one */
7233 hci_conn_drop(hcon);
7234
7235 l2cap_state_change(chan, BT_CONNECT);
7236 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
7237
Johan Hedberg61202e42014-01-28 15:16:48 -08007238 /* Release chan->sport so that it can be reused by other
7239 * sockets (as it's only used for listening sockets).
7240 */
7241 write_lock(&chan_list_lock);
7242 chan->sport = 0;
7243 write_unlock(&chan_list_lock);
7244
Johan Hedberg162b49e2014-01-17 20:45:10 +02007245 if (hcon->state == BT_CONNECTED) {
7246 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7247 __clear_chan_timer(chan);
Johan Hedberge7cafc42014-07-17 15:35:38 +03007248 if (l2cap_chan_check_security(chan, true))
Johan Hedberg162b49e2014-01-17 20:45:10 +02007249 l2cap_state_change(chan, BT_CONNECTED);
7250 } else
7251 l2cap_do_start(chan);
7252 }
7253
7254 err = 0;
7255
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007256chan_unlock:
Johan Hedberg162b49e2014-01-17 20:45:10 +02007257 l2cap_chan_unlock(chan);
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007258 mutex_unlock(&conn->chan_lock);
7259done:
Johan Hedberg162b49e2014-01-17 20:45:10 +02007260 hci_dev_unlock(hdev);
7261 hci_dev_put(hdev);
7262 return err;
7263}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03007264EXPORT_SYMBOL_GPL(l2cap_chan_connect);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007265
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266/* ---- L2CAP interface with lower layer (HCI) ---- */
7267
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007268int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269{
7270 int exact = 0, lm1 = 0, lm2 = 0;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007271 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007273 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274
7275 /* Find listening sockets and check their link_mode */
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007276 read_lock(&chan_list_lock);
7277 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007278 if (c->state != BT_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 continue;
7280
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007281 if (!bacmp(&c->src, &hdev->bdaddr)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007282 lm1 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007283 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007284 lm1 |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 exact++;
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007286 } else if (!bacmp(&c->src, BDADDR_ANY)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007287 lm2 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007288 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007289 lm2 |= HCI_LM_MASTER;
7290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 }
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007292 read_unlock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293
7294 return exact ? lm1 : lm2;
7295}
7296
Johan Hedberge760ec12014-08-07 22:56:47 +03007297/* Find the next fixed channel in BT_LISTEN state, continue iteration
7298 * from an existing channel in the list or from the beginning of the
7299 * global list (by passing NULL as first parameter).
7300 */
7301static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c,
Johan Hedberg327a7192015-01-15 13:06:45 +02007302 struct hci_conn *hcon)
Johan Hedberge760ec12014-08-07 22:56:47 +03007303{
Johan Hedberg327a7192015-01-15 13:06:45 +02007304 u8 src_type = bdaddr_src_type(hcon);
7305
Johan Hedberge760ec12014-08-07 22:56:47 +03007306 read_lock(&chan_list_lock);
7307
7308 if (c)
7309 c = list_next_entry(c, global_l);
7310 else
7311 c = list_entry(chan_list.next, typeof(*c), global_l);
7312
7313 list_for_each_entry_from(c, &chan_list, global_l) {
7314 if (c->chan_type != L2CAP_CHAN_FIXED)
7315 continue;
7316 if (c->state != BT_LISTEN)
7317 continue;
Johan Hedberg327a7192015-01-15 13:06:45 +02007318 if (bacmp(&c->src, &hcon->src) && bacmp(&c->src, BDADDR_ANY))
Johan Hedberge760ec12014-08-07 22:56:47 +03007319 continue;
Johan Hedberg327a7192015-01-15 13:06:45 +02007320 if (src_type != c->src_type)
Johan Hedberg54a1b622014-08-07 22:56:48 +03007321 continue;
Johan Hedberge760ec12014-08-07 22:56:47 +03007322
7323 l2cap_chan_hold(c);
7324 read_unlock(&chan_list_lock);
7325 return c;
7326 }
7327
7328 read_unlock(&chan_list_lock);
7329
7330 return NULL;
7331}
7332
Johan Hedberg539c4962015-02-18 14:53:57 +02007333static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334{
Johan Hedberge760ec12014-08-07 22:56:47 +03007335 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann01394182006-07-03 10:02:46 +02007336 struct l2cap_conn *conn;
Johan Hedberge760ec12014-08-07 22:56:47 +03007337 struct l2cap_chan *pchan;
7338 u8 dst_type;
Marcel Holtmann01394182006-07-03 10:02:46 +02007339
Johan Hedberg539c4962015-02-18 14:53:57 +02007340 if (hcon->type != ACL_LINK && hcon->type != LE_LINK)
7341 return;
7342
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007343 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007345 if (status) {
Joe Perchese1750722011-06-29 18:18:29 -07007346 l2cap_conn_del(hcon, bt_to_errno(status));
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007347 return;
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02007348 }
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007349
7350 conn = l2cap_conn_add(hcon);
7351 if (!conn)
7352 return;
7353
Johan Hedberga250e042015-01-15 13:06:44 +02007354 dst_type = bdaddr_dst_type(hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007355
7356 /* If device is blocked, do not create channels for it */
7357 if (hci_bdaddr_list_lookup(&hdev->blacklist, &hcon->dst, dst_type))
7358 return;
7359
7360 /* Find fixed channels and notify them of the new connection. We
7361 * use multiple individual lookups, continuing each time where
7362 * we left off, because the list lock would prevent calling the
7363 * potentially sleeping l2cap_chan_lock() function.
7364 */
Johan Hedberg327a7192015-01-15 13:06:45 +02007365 pchan = l2cap_global_fixed_chan(NULL, hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007366 while (pchan) {
7367 struct l2cap_chan *chan, *next;
7368
7369 /* Client fixed channels should override server ones */
7370 if (__l2cap_get_chan_by_dcid(conn, pchan->scid))
7371 goto next;
7372
7373 l2cap_chan_lock(pchan);
7374 chan = pchan->ops->new_connection(pchan);
7375 if (chan) {
7376 bacpy(&chan->src, &hcon->src);
7377 bacpy(&chan->dst, &hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +02007378 chan->src_type = bdaddr_src_type(hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007379 chan->dst_type = dst_type;
7380
7381 __l2cap_chan_add(conn, chan);
7382 }
7383
7384 l2cap_chan_unlock(pchan);
7385next:
Johan Hedberg327a7192015-01-15 13:06:45 +02007386 next = l2cap_global_fixed_chan(pchan, hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007387 l2cap_chan_put(pchan);
7388 pchan = next;
7389 }
7390
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007391 l2cap_conn_ready(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392}
7393
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007394int l2cap_disconn_ind(struct hci_conn *hcon)
Marcel Holtmann2950f212009-02-12 14:02:50 +01007395{
7396 struct l2cap_conn *conn = hcon->l2cap_data;
7397
7398 BT_DBG("hcon %p", hcon);
7399
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007400 if (!conn)
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02007401 return HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +01007402 return conn->disc_reason;
7403}
7404
Johan Hedberg3a6d5762015-02-18 14:53:58 +02007405static void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406{
Johan Hedberg3a6d5762015-02-18 14:53:58 +02007407 if (hcon->type != ACL_LINK && hcon->type != LE_LINK)
7408 return;
7409
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 BT_DBG("hcon %p reason %d", hcon, reason);
7411
Joe Perchese1750722011-06-29 18:18:29 -07007412 l2cap_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413}
7414
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007415static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007416{
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03007417 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
Marcel Holtmann255c7602009-02-04 21:07:19 +01007418 return;
7419
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007420 if (encrypt == 0x00) {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007421 if (chan->sec_level == BT_SECURITY_MEDIUM) {
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007422 __set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
Marcel Holtmann7d513e92014-01-15 22:37:40 -08007423 } else if (chan->sec_level == BT_SECURITY_HIGH ||
7424 chan->sec_level == BT_SECURITY_FIPS)
Gustavo F. Padovan0f852722011-05-04 19:42:50 -03007425 l2cap_chan_close(chan, ECONNREFUSED);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007426 } else {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007427 if (chan->sec_level == BT_SECURITY_MEDIUM)
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03007428 __clear_chan_timer(chan);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007429 }
7430}
7431
Johan Hedberg354fe802015-02-18 14:53:56 +02007432static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433{
Marcel Holtmann40be4922008-07-14 20:13:50 +02007434 struct l2cap_conn *conn = hcon->l2cap_data;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03007435 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436
Marcel Holtmann01394182006-07-03 10:02:46 +02007437 if (!conn)
Johan Hedberg354fe802015-02-18 14:53:56 +02007438 return;
Marcel Holtmann01394182006-07-03 10:02:46 +02007439
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007440 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007442 mutex_lock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007444 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007445 l2cap_chan_lock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007447 BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
7448 state_to_string(chan->state));
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007449
Johan Hedberg2338a7e2014-01-24 10:35:40 +02007450 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko78eb2f92012-07-19 17:03:47 +03007451 l2cap_chan_unlock(chan);
7452 continue;
7453 }
7454
Johan Hedberg191eb392014-08-07 22:56:45 +03007455 if (!status && encrypt)
7456 chan->sec_level = hcon->sec_level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007457
Andrei Emeltchenko96eff462012-11-15 18:14:53 +02007458 if (!__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007459 l2cap_chan_unlock(chan);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01007460 continue;
7461 }
7462
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007463 if (!status && (chan->state == BT_CONNECTED ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01007464 chan->state == BT_CONFIG)) {
Marcel Holtmannd97c8992013-10-14 02:53:54 -07007465 chan->ops->resume(chan);
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007466 l2cap_check_encryption(chan, encrypt);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007467 l2cap_chan_unlock(chan);
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02007468 continue;
7469 }
7470
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007471 if (chan->state == BT_CONNECT) {
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007472 if (!status)
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03007473 l2cap_start_connection(chan);
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007474 else
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007475 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergfa37c1a2014-11-13 10:55:17 +02007476 } else if (chan->state == BT_CONNECT2 &&
7477 chan->mode != L2CAP_MODE_LE_FLOWCTL) {
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007478 struct l2cap_conn_rsp rsp;
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007479 __u16 res, stat;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007480
7481 if (!status) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07007482 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007483 res = L2CAP_CR_PEND;
7484 stat = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08007485 chan->ops->defer(chan);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007486 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007487 l2cap_state_change(chan, BT_CONFIG);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007488 res = L2CAP_CR_SUCCESS;
7489 stat = L2CAP_CS_NO_INFO;
7490 }
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007491 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007492 l2cap_state_change(chan, BT_DISCONN);
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007493 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007494 res = L2CAP_CR_SEC_BLOCK;
7495 stat = L2CAP_CS_NO_INFO;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007496 }
7497
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03007498 rsp.scid = cpu_to_le16(chan->dcid);
7499 rsp.dcid = cpu_to_le16(chan->scid);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007500 rsp.result = cpu_to_le16(res);
7501 rsp.status = cpu_to_le16(stat);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03007502 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01007503 sizeof(rsp), &rsp);
Mat Martineau2d369352012-05-23 14:59:30 -07007504
7505 if (!test_bit(CONF_REQ_SENT, &chan->conf_state) &&
7506 res == L2CAP_CR_SUCCESS) {
7507 char buf[128];
7508 set_bit(CONF_REQ_SENT, &chan->conf_state);
7509 l2cap_send_cmd(conn, l2cap_get_ident(conn),
7510 L2CAP_CONF_REQ,
Ben Serie860d2c2017-09-09 23:15:59 +02007511 l2cap_build_conf_req(chan, buf, sizeof(buf)),
Mat Martineau2d369352012-05-23 14:59:30 -07007512 buf);
7513 chan->num_conf_req++;
7514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 }
7516
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007517 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 }
7519
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007520 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521}
7522
Arron Wang9b4c3332015-06-09 17:47:22 +08007523void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524{
7525 struct l2cap_conn *conn = hcon->l2cap_data;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007526 struct l2cap_hdr *hdr;
7527 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528
Andrei Emeltchenko1d13a252012-10-15 11:58:41 +03007529 /* For AMP controller do not create l2cap conn */
Marcel Holtmannca8bee52016-07-05 14:30:14 +02007530 if (!conn && hcon->hdev->dev_type != HCI_PRIMARY)
Andrei Emeltchenko1d13a252012-10-15 11:58:41 +03007531 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007533 if (!conn)
Claudio Takahasibaf43252013-04-11 13:55:50 -03007534 conn = l2cap_conn_add(hcon);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007535
7536 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 goto drop;
7538
7539 BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
7540
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007541 switch (flags) {
7542 case ACL_START:
7543 case ACL_START_NO_FLUSH:
7544 case ACL_COMPLETE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 if (conn->rx_len) {
7546 BT_ERR("Unexpected start frame (len %d)", skb->len);
7547 kfree_skb(conn->rx_skb);
7548 conn->rx_skb = NULL;
7549 conn->rx_len = 0;
7550 l2cap_conn_unreliable(conn, ECOMM);
7551 }
7552
Andrei Emeltchenkoaae7fe22010-09-15 14:28:43 +03007553 /* Start fragment always begin with Basic L2CAP header */
7554 if (skb->len < L2CAP_HDR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 BT_ERR("Frame is too short (len %d)", skb->len);
7556 l2cap_conn_unreliable(conn, ECOMM);
7557 goto drop;
7558 }
7559
7560 hdr = (struct l2cap_hdr *) skb->data;
7561 len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
7562
7563 if (len == skb->len) {
7564 /* Complete frame received */
7565 l2cap_recv_frame(conn, skb);
Arron Wang9b4c3332015-06-09 17:47:22 +08007566 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 }
7568
7569 BT_DBG("Start: total len %d, frag len %d", len, skb->len);
7570
7571 if (skb->len > len) {
7572 BT_ERR("Frame is too long (len %d, expected len %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007573 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574 l2cap_conn_unreliable(conn, ECOMM);
7575 goto drop;
7576 }
7577
7578 /* Allocate skb for the complete frame (with header) */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03007579 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03007580 if (!conn->rx_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 goto drop;
7582
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007583 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007584 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 conn->rx_len = len - skb->len;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007586 break;
7587
7588 case ACL_CONT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
7590
7591 if (!conn->rx_len) {
7592 BT_ERR("Unexpected continuation frame (len %d)", skb->len);
7593 l2cap_conn_unreliable(conn, ECOMM);
7594 goto drop;
7595 }
7596
7597 if (skb->len > conn->rx_len) {
7598 BT_ERR("Fragment is too long (len %d, expected %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007599 skb->len, conn->rx_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600 kfree_skb(conn->rx_skb);
7601 conn->rx_skb = NULL;
7602 conn->rx_len = 0;
7603 l2cap_conn_unreliable(conn, ECOMM);
7604 goto drop;
7605 }
7606
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007607 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007608 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609 conn->rx_len -= skb->len;
7610
7611 if (!conn->rx_len) {
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007612 /* Complete frame received. l2cap_recv_frame
7613 * takes ownership of the skb so set the global
7614 * rx_skb pointer to NULL first.
7615 */
7616 struct sk_buff *rx_skb = conn->rx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 conn->rx_skb = NULL;
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007618 l2cap_recv_frame(conn, rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 }
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007620 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621 }
7622
7623drop:
7624 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625}
7626
Johan Hedberg354fe802015-02-18 14:53:56 +02007627static struct hci_cb l2cap_cb = {
7628 .name = "L2CAP",
Johan Hedberg539c4962015-02-18 14:53:57 +02007629 .connect_cfm = l2cap_connect_cfm,
Johan Hedberg3a6d5762015-02-18 14:53:58 +02007630 .disconn_cfm = l2cap_disconn_cfm,
Johan Hedberg354fe802015-02-18 14:53:56 +02007631 .security_cfm = l2cap_security_cfm,
7632};
7633
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007634static int l2cap_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007636 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007638 read_lock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007640 list_for_each_entry(c, &chan_list, global_l) {
Marcel Holtmanneeb5a062015-01-14 13:44:21 -08007641 seq_printf(f, "%pMR (%u) %pMR (%u) %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
7642 &c->src, c->src_type, &c->dst, c->dst_type,
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03007643 c->state, __le16_to_cpu(c->psm),
7644 c->scid, c->dcid, c->imtu, c->omtu,
7645 c->sec_level, c->mode);
Andrei Emeltchenko61e1b4b2012-01-19 11:19:50 +02007646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007648 read_unlock(&chan_list_lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007649
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651}
7652
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007653static int l2cap_debugfs_open(struct inode *inode, struct file *file)
7654{
7655 return single_open(file, l2cap_debugfs_show, inode->i_private);
7656}
7657
7658static const struct file_operations l2cap_debugfs_fops = {
7659 .open = l2cap_debugfs_open,
7660 .read = seq_read,
7661 .llseek = seq_lseek,
7662 .release = single_release,
7663};
7664
7665static struct dentry *l2cap_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007667int __init l2cap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668{
7669 int err;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007670
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007671 err = l2cap_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 if (err < 0)
7673 return err;
7674
Johan Hedberg354fe802015-02-18 14:53:56 +02007675 hci_register_cb(&l2cap_cb);
7676
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07007677 if (IS_ERR_OR_NULL(bt_debugfs))
7678 return 0;
7679
7680 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
7681 NULL, &l2cap_debugfs_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684}
7685
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007686void l2cap_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007688 debugfs_remove(l2cap_debugfs);
Johan Hedberg354fe802015-02-18 14:53:56 +02007689 hci_unregister_cb(&l2cap_cb);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007690 l2cap_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691}
7692
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03007693module_param(disable_ertm, bool, 0644);
7694MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");